1 //===-- Interpreter.h ------------------------------------------*- C++ -*--===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This header file defines the interpreter structure 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_LIB_EXECUTIONENGINE_INTERPRETER_INTERPRETER_H 15 #define LLVM_LIB_EXECUTIONENGINE_INTERPRETER_INTERPRETER_H 16 17 #include "llvm/ExecutionEngine/ExecutionEngine.h" 18 #include "llvm/ExecutionEngine/GenericValue.h" 19 #include "llvm/IR/CallSite.h" 20 #include "llvm/IR/DataLayout.h" 21 #include "llvm/IR/Function.h" 22 #include "llvm/IR/InstVisitor.h" 23 #include "llvm/Support/DataTypes.h" 24 #include "llvm/Support/ErrorHandling.h" 25 #include "llvm/Support/raw_ostream.h" 26 namespace llvm { 27 28 class IntrinsicLowering; 29 struct FunctionInfo; 30 template<typename T> class generic_gep_type_iterator; 31 class ConstantExpr; 32 typedef generic_gep_type_iterator<User::const_op_iterator> gep_type_iterator; 33 34 35 // AllocaHolder - Object to track all of the blocks of memory allocated by 36 // alloca. When the function returns, this object is popped off the execution 37 // stack, which causes the dtor to be run, which frees all the alloca'd memory. 38 // 39 class AllocaHolder { 40 std::vector<void *> Allocations; 41 42 public: AllocaHolder()43 AllocaHolder() {} 44 45 // Make this type move-only. Define explicit move special members for MSVC. AllocaHolder(AllocaHolder && RHS)46 AllocaHolder(AllocaHolder &&RHS) : Allocations(std::move(RHS.Allocations)) {} 47 AllocaHolder &operator=(AllocaHolder &&RHS) { 48 Allocations = std::move(RHS.Allocations); 49 return *this; 50 } 51 ~AllocaHolder()52 ~AllocaHolder() { 53 for (void *Allocation : Allocations) 54 free(Allocation); 55 } 56 add(void * Mem)57 void add(void *Mem) { Allocations.push_back(Mem); } 58 }; 59 60 typedef std::vector<GenericValue> ValuePlaneTy; 61 62 // ExecutionContext struct - This struct represents one stack frame currently 63 // executing. 64 // 65 struct ExecutionContext { 66 Function *CurFunction;// The currently executing function 67 BasicBlock *CurBB; // The currently executing BB 68 BasicBlock::iterator CurInst; // The next instruction to execute 69 CallSite Caller; // Holds the call that called subframes. 70 // NULL if main func or debugger invoked fn 71 std::map<Value *, GenericValue> Values; // LLVM values used in this invocation 72 std::vector<GenericValue> VarArgs; // Values passed through an ellipsis 73 AllocaHolder Allocas; // Track memory allocated by alloca 74 ExecutionContextExecutionContext75 ExecutionContext() : CurFunction(nullptr), CurBB(nullptr), CurInst(nullptr) {} 76 ExecutionContextExecutionContext77 ExecutionContext(ExecutionContext &&O) 78 : CurFunction(O.CurFunction), CurBB(O.CurBB), CurInst(O.CurInst), 79 Caller(O.Caller), Values(std::move(O.Values)), 80 VarArgs(std::move(O.VarArgs)), Allocas(std::move(O.Allocas)) {} 81 82 ExecutionContext &operator=(ExecutionContext &&O) { 83 CurFunction = O.CurFunction; 84 CurBB = O.CurBB; 85 CurInst = O.CurInst; 86 Caller = O.Caller; 87 Values = std::move(O.Values); 88 VarArgs = std::move(O.VarArgs); 89 Allocas = std::move(O.Allocas); 90 return *this; 91 } 92 }; 93 94 // Interpreter - This class represents the entirety of the interpreter. 95 // 96 class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> { 97 GenericValue ExitValue; // The return value of the called function 98 DataLayout TD; 99 IntrinsicLowering *IL; 100 101 // The runtime stack of executing code. The top of the stack is the current 102 // function record. 103 std::vector<ExecutionContext> ECStack; 104 105 // AtExitHandlers - List of functions to call when the program exits, 106 // registered with the atexit() library function. 107 std::vector<Function*> AtExitHandlers; 108 109 public: 110 explicit Interpreter(std::unique_ptr<Module> M); 111 ~Interpreter() override; 112 113 /// runAtExitHandlers - Run any functions registered by the program's calls to 114 /// atexit(3), which we intercept and store in AtExitHandlers. 115 /// 116 void runAtExitHandlers(); 117 Register()118 static void Register() { 119 InterpCtor = create; 120 } 121 122 /// Create an interpreter ExecutionEngine. 123 /// 124 static ExecutionEngine *create(std::unique_ptr<Module> M, 125 std::string *ErrorStr = nullptr); 126 127 /// run - Start execution with the specified function and arguments. 128 /// 129 GenericValue runFunction(Function *F, 130 const std::vector<GenericValue> &ArgValues) override; 131 132 void *getPointerToNamedFunction(StringRef Name, 133 bool AbortOnFailure = true) override { 134 // FIXME: not implemented. 135 return nullptr; 136 } 137 138 // Methods used to execute code: 139 // Place a call on the stack 140 void callFunction(Function *F, const std::vector<GenericValue> &ArgVals); 141 void run(); // Execute instructions until nothing left to do 142 143 // Opcode Implementations 144 void visitReturnInst(ReturnInst &I); 145 void visitBranchInst(BranchInst &I); 146 void visitSwitchInst(SwitchInst &I); 147 void visitIndirectBrInst(IndirectBrInst &I); 148 149 void visitBinaryOperator(BinaryOperator &I); 150 void visitICmpInst(ICmpInst &I); 151 void visitFCmpInst(FCmpInst &I); 152 void visitAllocaInst(AllocaInst &I); 153 void visitLoadInst(LoadInst &I); 154 void visitStoreInst(StoreInst &I); 155 void visitGetElementPtrInst(GetElementPtrInst &I); visitPHINode(PHINode & PN)156 void visitPHINode(PHINode &PN) { 157 llvm_unreachable("PHI nodes already handled!"); 158 } 159 void visitTruncInst(TruncInst &I); 160 void visitZExtInst(ZExtInst &I); 161 void visitSExtInst(SExtInst &I); 162 void visitFPTruncInst(FPTruncInst &I); 163 void visitFPExtInst(FPExtInst &I); 164 void visitUIToFPInst(UIToFPInst &I); 165 void visitSIToFPInst(SIToFPInst &I); 166 void visitFPToUIInst(FPToUIInst &I); 167 void visitFPToSIInst(FPToSIInst &I); 168 void visitPtrToIntInst(PtrToIntInst &I); 169 void visitIntToPtrInst(IntToPtrInst &I); 170 void visitBitCastInst(BitCastInst &I); 171 void visitSelectInst(SelectInst &I); 172 173 174 void visitCallSite(CallSite CS); visitCallInst(CallInst & I)175 void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); } visitInvokeInst(InvokeInst & I)176 void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); } 177 void visitUnreachableInst(UnreachableInst &I); 178 179 void visitShl(BinaryOperator &I); 180 void visitLShr(BinaryOperator &I); 181 void visitAShr(BinaryOperator &I); 182 183 void visitVAArgInst(VAArgInst &I); 184 void visitExtractElementInst(ExtractElementInst &I); 185 void visitInsertElementInst(InsertElementInst &I); 186 void visitShuffleVectorInst(ShuffleVectorInst &I); 187 188 void visitExtractValueInst(ExtractValueInst &I); 189 void visitInsertValueInst(InsertValueInst &I); 190 visitInstruction(Instruction & I)191 void visitInstruction(Instruction &I) { 192 errs() << I << "\n"; 193 llvm_unreachable("Instruction not interpretable yet!"); 194 } 195 196 GenericValue callExternalFunction(Function *F, 197 const std::vector<GenericValue> &ArgVals); 198 void exitCalled(GenericValue GV); 199 addAtExitHandler(Function * F)200 void addAtExitHandler(Function *F) { 201 AtExitHandlers.push_back(F); 202 } 203 getFirstVarArg()204 GenericValue *getFirstVarArg () { 205 return &(ECStack.back ().VarArgs[0]); 206 } 207 208 private: // Helper functions 209 GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I, 210 gep_type_iterator E, ExecutionContext &SF); 211 212 // SwitchToNewBasicBlock - Start execution in a new basic block and run any 213 // PHI nodes in the top of the block. This is used for intraprocedural 214 // control flow. 215 // 216 void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF); 217 getPointerToFunction(Function * F)218 void *getPointerToFunction(Function *F) override { return (void*)F; } 219 initializeExecutionEngine()220 void initializeExecutionEngine() { } 221 void initializeExternalFunctions(); 222 GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF); 223 GenericValue getOperandValue(Value *V, ExecutionContext &SF); 224 GenericValue executeTruncInst(Value *SrcVal, Type *DstTy, 225 ExecutionContext &SF); 226 GenericValue executeSExtInst(Value *SrcVal, Type *DstTy, 227 ExecutionContext &SF); 228 GenericValue executeZExtInst(Value *SrcVal, Type *DstTy, 229 ExecutionContext &SF); 230 GenericValue executeFPTruncInst(Value *SrcVal, Type *DstTy, 231 ExecutionContext &SF); 232 GenericValue executeFPExtInst(Value *SrcVal, Type *DstTy, 233 ExecutionContext &SF); 234 GenericValue executeFPToUIInst(Value *SrcVal, Type *DstTy, 235 ExecutionContext &SF); 236 GenericValue executeFPToSIInst(Value *SrcVal, Type *DstTy, 237 ExecutionContext &SF); 238 GenericValue executeUIToFPInst(Value *SrcVal, Type *DstTy, 239 ExecutionContext &SF); 240 GenericValue executeSIToFPInst(Value *SrcVal, Type *DstTy, 241 ExecutionContext &SF); 242 GenericValue executePtrToIntInst(Value *SrcVal, Type *DstTy, 243 ExecutionContext &SF); 244 GenericValue executeIntToPtrInst(Value *SrcVal, Type *DstTy, 245 ExecutionContext &SF); 246 GenericValue executeBitCastInst(Value *SrcVal, Type *DstTy, 247 ExecutionContext &SF); 248 GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal, 249 Type *Ty, ExecutionContext &SF); 250 void popStackAndReturnValueToCaller(Type *RetTy, GenericValue Result); 251 252 }; 253 254 } // End llvm namespace 255 256 #endif 257