1193323Sed//===-- Interpreter.h ------------------------------------------*- C++ -*--===// 2193323Sed// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6193323Sed// 7193323Sed//===----------------------------------------------------------------------===// 8193323Sed// 9193323Sed// This header file defines the interpreter structure 10193323Sed// 11193323Sed//===----------------------------------------------------------------------===// 12193323Sed 13280031Sdim#ifndef LLVM_LIB_EXECUTIONENGINE_INTERPRETER_INTERPRETER_H 14280031Sdim#define LLVM_LIB_EXECUTIONENGINE_INTERPRETER_INTERPRETER_H 15193323Sed 16193323Sed#include "llvm/ExecutionEngine/ExecutionEngine.h" 17193323Sed#include "llvm/ExecutionEngine/GenericValue.h" 18276479Sdim#include "llvm/IR/CallSite.h" 19249423Sdim#include "llvm/IR/DataLayout.h" 20249423Sdim#include "llvm/IR/Function.h" 21276479Sdim#include "llvm/IR/InstVisitor.h" 22218893Sdim#include "llvm/Support/DataTypes.h" 23198090Srdivacky#include "llvm/Support/ErrorHandling.h" 24198090Srdivacky#include "llvm/Support/raw_ostream.h" 25193323Sednamespace llvm { 26193323Sed 27193323Sedclass IntrinsicLowering; 28193323Sedtemplate<typename T> class generic_gep_type_iterator; 29193323Sedclass ConstantExpr; 30193323Sedtypedef generic_gep_type_iterator<User::const_op_iterator> gep_type_iterator; 31193323Sed 32193323Sed 33193323Sed// AllocaHolder - Object to track all of the blocks of memory allocated by 34193323Sed// alloca. When the function returns, this object is popped off the execution 35193323Sed// stack, which causes the dtor to be run, which frees all the alloca'd memory. 36193323Sed// 37193323Sedclass AllocaHolder { 38280031Sdim std::vector<void *> Allocations; 39280031Sdim 40193323Sedpublic: 41280031Sdim AllocaHolder() {} 42280031Sdim 43314564Sdim // Make this type move-only. 44314564Sdim AllocaHolder(AllocaHolder &&) = default; 45314564Sdim AllocaHolder &operator=(AllocaHolder &&RHS) = default; 46280031Sdim 47193323Sed ~AllocaHolder() { 48280031Sdim for (void *Allocation : Allocations) 49280031Sdim free(Allocation); 50193323Sed } 51193323Sed 52280031Sdim void add(void *Mem) { Allocations.push_back(Mem); } 53193323Sed}; 54193323Sed 55193323Sedtypedef std::vector<GenericValue> ValuePlaneTy; 56193323Sed 57193323Sed// ExecutionContext struct - This struct represents one stack frame currently 58193323Sed// executing. 59193323Sed// 60193323Sedstruct ExecutionContext { 61193323Sed Function *CurFunction;// The currently executing function 62193323Sed BasicBlock *CurBB; // The currently executing BB 63193323Sed BasicBlock::iterator CurInst; // The next instruction to execute 64280031Sdim CallSite Caller; // Holds the call that called subframes. 65280031Sdim // NULL if main func or debugger invoked fn 66193323Sed std::map<Value *, GenericValue> Values; // LLVM values used in this invocation 67193323Sed std::vector<GenericValue> VarArgs; // Values passed through an ellipsis 68280031Sdim AllocaHolder Allocas; // Track memory allocated by alloca 69280031Sdim 70280031Sdim ExecutionContext() : CurFunction(nullptr), CurBB(nullptr), CurInst(nullptr) {} 71193323Sed}; 72193323Sed 73193323Sed// Interpreter - This class represents the entirety of the interpreter. 74193323Sed// 75193323Sedclass Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> { 76193323Sed GenericValue ExitValue; // The return value of the called function 77193323Sed IntrinsicLowering *IL; 78193323Sed 79193323Sed // The runtime stack of executing code. The top of the stack is the current 80193323Sed // function record. 81193323Sed std::vector<ExecutionContext> ECStack; 82193323Sed 83193323Sed // AtExitHandlers - List of functions to call when the program exits, 84193323Sed // registered with the atexit() library function. 85193323Sed std::vector<Function*> AtExitHandlers; 86193323Sed 87193323Sedpublic: 88280031Sdim explicit Interpreter(std::unique_ptr<Module> M); 89288943Sdim ~Interpreter() override; 90193323Sed 91193323Sed /// runAtExitHandlers - Run any functions registered by the program's calls to 92193323Sed /// atexit(3), which we intercept and store in AtExitHandlers. 93193323Sed /// 94193323Sed void runAtExitHandlers(); 95193323Sed 96193323Sed static void Register() { 97193323Sed InterpCtor = create; 98193323Sed } 99280031Sdim 100280031Sdim /// Create an interpreter ExecutionEngine. 101193323Sed /// 102280031Sdim static ExecutionEngine *create(std::unique_ptr<Module> M, 103280031Sdim std::string *ErrorStr = nullptr); 104193323Sed 105193323Sed /// run - Start execution with the specified function and arguments. 106193323Sed /// 107276479Sdim GenericValue runFunction(Function *F, 108288943Sdim ArrayRef<GenericValue> ArgValues) override; 109193323Sed 110280031Sdim void *getPointerToNamedFunction(StringRef Name, 111276479Sdim bool AbortOnFailure = true) override { 112234353Sdim // FIXME: not implemented. 113276479Sdim return nullptr; 114234353Sdim } 115234353Sdim 116193323Sed // Methods used to execute code: 117193323Sed // Place a call on the stack 118288943Sdim void callFunction(Function *F, ArrayRef<GenericValue> ArgVals); 119193323Sed void run(); // Execute instructions until nothing left to do 120193323Sed 121193323Sed // Opcode Implementations 122193323Sed void visitReturnInst(ReturnInst &I); 123193323Sed void visitBranchInst(BranchInst &I); 124193323Sed void visitSwitchInst(SwitchInst &I); 125198892Srdivacky void visitIndirectBrInst(IndirectBrInst &I); 126193323Sed 127353358Sdim void visitUnaryOperator(UnaryOperator &I); 128193323Sed void visitBinaryOperator(BinaryOperator &I); 129193323Sed void visitICmpInst(ICmpInst &I); 130193323Sed void visitFCmpInst(FCmpInst &I); 131198892Srdivacky void visitAllocaInst(AllocaInst &I); 132193323Sed void visitLoadInst(LoadInst &I); 133193323Sed void visitStoreInst(StoreInst &I); 134193323Sed void visitGetElementPtrInst(GetElementPtrInst &I); 135341825Sdim void visitPHINode(PHINode &PN) { 136341825Sdim llvm_unreachable("PHI nodes already handled!"); 137198090Srdivacky } 138193323Sed void visitTruncInst(TruncInst &I); 139193323Sed void visitZExtInst(ZExtInst &I); 140193323Sed void visitSExtInst(SExtInst &I); 141193323Sed void visitFPTruncInst(FPTruncInst &I); 142193323Sed void visitFPExtInst(FPExtInst &I); 143193323Sed void visitUIToFPInst(UIToFPInst &I); 144193323Sed void visitSIToFPInst(SIToFPInst &I); 145193323Sed void visitFPToUIInst(FPToUIInst &I); 146193323Sed void visitFPToSIInst(FPToSIInst &I); 147193323Sed void visitPtrToIntInst(PtrToIntInst &I); 148193323Sed void visitIntToPtrInst(IntToPtrInst &I); 149193323Sed void visitBitCastInst(BitCastInst &I); 150193323Sed void visitSelectInst(SelectInst &I); 151193323Sed 152193323Sed 153193323Sed void visitCallSite(CallSite CS); 154193323Sed void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); } 155193323Sed void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); } 156193323Sed void visitUnreachableInst(UnreachableInst &I); 157193323Sed 158193323Sed void visitShl(BinaryOperator &I); 159193323Sed void visitLShr(BinaryOperator &I); 160193323Sed void visitAShr(BinaryOperator &I); 161193323Sed 162193323Sed void visitVAArgInst(VAArgInst &I); 163249423Sdim void visitExtractElementInst(ExtractElementInst &I); 164261991Sdim void visitInsertElementInst(InsertElementInst &I); 165261991Sdim void visitShuffleVectorInst(ShuffleVectorInst &I); 166261991Sdim 167261991Sdim void visitExtractValueInst(ExtractValueInst &I); 168261991Sdim void visitInsertValueInst(InsertValueInst &I); 169261991Sdim 170193323Sed void visitInstruction(Instruction &I) { 171226633Sdim errs() << I << "\n"; 172198090Srdivacky llvm_unreachable("Instruction not interpretable yet!"); 173193323Sed } 174193323Sed 175193323Sed GenericValue callExternalFunction(Function *F, 176288943Sdim ArrayRef<GenericValue> ArgVals); 177193323Sed void exitCalled(GenericValue GV); 178193323Sed 179193323Sed void addAtExitHandler(Function *F) { 180193323Sed AtExitHandlers.push_back(F); 181193323Sed } 182193323Sed 183193323Sed GenericValue *getFirstVarArg () { 184193323Sed return &(ECStack.back ().VarArgs[0]); 185193323Sed } 186193323Sed 187207618Srdivackyprivate: // Helper functions 188193323Sed GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I, 189193323Sed gep_type_iterator E, ExecutionContext &SF); 190193323Sed 191193323Sed // SwitchToNewBasicBlock - Start execution in a new basic block and run any 192193323Sed // PHI nodes in the top of the block. This is used for intraprocedural 193193323Sed // control flow. 194193323Sed // 195193323Sed void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF); 196193323Sed 197276479Sdim void *getPointerToFunction(Function *F) override { return (void*)F; } 198193323Sed 199195098Sed void initializeExecutionEngine() { } 200193323Sed void initializeExternalFunctions(); 201193323Sed GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF); 202193323Sed GenericValue getOperandValue(Value *V, ExecutionContext &SF); 203226633Sdim GenericValue executeTruncInst(Value *SrcVal, Type *DstTy, 204193323Sed ExecutionContext &SF); 205226633Sdim GenericValue executeSExtInst(Value *SrcVal, Type *DstTy, 206193323Sed ExecutionContext &SF); 207226633Sdim GenericValue executeZExtInst(Value *SrcVal, Type *DstTy, 208193323Sed ExecutionContext &SF); 209226633Sdim GenericValue executeFPTruncInst(Value *SrcVal, Type *DstTy, 210193323Sed ExecutionContext &SF); 211226633Sdim GenericValue executeFPExtInst(Value *SrcVal, Type *DstTy, 212193323Sed ExecutionContext &SF); 213226633Sdim GenericValue executeFPToUIInst(Value *SrcVal, Type *DstTy, 214193323Sed ExecutionContext &SF); 215226633Sdim GenericValue executeFPToSIInst(Value *SrcVal, Type *DstTy, 216193323Sed ExecutionContext &SF); 217226633Sdim GenericValue executeUIToFPInst(Value *SrcVal, Type *DstTy, 218193323Sed ExecutionContext &SF); 219226633Sdim GenericValue executeSIToFPInst(Value *SrcVal, Type *DstTy, 220193323Sed ExecutionContext &SF); 221226633Sdim GenericValue executePtrToIntInst(Value *SrcVal, Type *DstTy, 222193323Sed ExecutionContext &SF); 223226633Sdim GenericValue executeIntToPtrInst(Value *SrcVal, Type *DstTy, 224193323Sed ExecutionContext &SF); 225226633Sdim GenericValue executeBitCastInst(Value *SrcVal, Type *DstTy, 226193323Sed ExecutionContext &SF); 227341825Sdim GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal, 228226633Sdim Type *Ty, ExecutionContext &SF); 229226633Sdim void popStackAndReturnValueToCaller(Type *RetTy, GenericValue Result); 230193323Sed 231193323Sed}; 232193323Sed 233193323Sed} // End llvm namespace 234193323Sed 235193323Sed#endif 236