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 LLI_INTERPRETER_H 15 #define LLI_INTERPRETER_H 16 17 #include "llvm/ExecutionEngine/ExecutionEngine.h" 18 #include "llvm/ExecutionEngine/GenericValue.h" 19 #include "llvm/IR/DataLayout.h" 20 #include "llvm/IR/Function.h" 21 #include "llvm/InstVisitor.h" 22 #include "llvm/Support/CallSite.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 friend class AllocaHolderHandle; 41 std::vector<void*> Allocations; 42 unsigned RefCnt; 43 public: 44 AllocaHolder() : RefCnt(0) {} 45 void add(void *mem) { Allocations.push_back(mem); } 46 ~AllocaHolder() { 47 for (unsigned i = 0; i < Allocations.size(); ++i) 48 free(Allocations[i]); 49 } 50 }; 51 52 // AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into 53 // a vector... 54 // 55 class AllocaHolderHandle { 56 AllocaHolder *H; 57 public: 58 AllocaHolderHandle() : H(new AllocaHolder()) { H->RefCnt++; } 59 AllocaHolderHandle(const AllocaHolderHandle &AH) : H(AH.H) { H->RefCnt++; } 60 ~AllocaHolderHandle() { if (--H->RefCnt == 0) delete H; } 61 62 void add(void *mem) { H->add(mem); } 63 }; 64 65 typedef std::vector<GenericValue> ValuePlaneTy; 66 67 // ExecutionContext struct - This struct represents one stack frame currently 68 // executing. 69 // 70 struct ExecutionContext { 71 Function *CurFunction;// The currently executing function 72 BasicBlock *CurBB; // The currently executing BB 73 BasicBlock::iterator CurInst; // The next instruction to execute 74 std::map<Value *, GenericValue> Values; // LLVM values used in this invocation 75 std::vector<GenericValue> VarArgs; // Values passed through an ellipsis 76 CallSite Caller; // Holds the call that called subframes. 77 // NULL if main func or debugger invoked fn 78 AllocaHolderHandle Allocas; // Track memory allocated by alloca 79 }; 80 81 // Interpreter - This class represents the entirety of the interpreter. 82 // 83 class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> { 84 GenericValue ExitValue; // The return value of the called function 85 DataLayout TD; 86 IntrinsicLowering *IL; 87 88 // The runtime stack of executing code. The top of the stack is the current 89 // function record. 90 std::vector<ExecutionContext> ECStack; 91 92 // AtExitHandlers - List of functions to call when the program exits, 93 // registered with the atexit() library function. 94 std::vector<Function*> AtExitHandlers; 95 96 public: 97 explicit Interpreter(Module *M); 98 ~Interpreter(); 99 100 /// runAtExitHandlers - Run any functions registered by the program's calls to 101 /// atexit(3), which we intercept and store in AtExitHandlers. 102 /// 103 void runAtExitHandlers(); 104 105 static void Register() { 106 InterpCtor = create; 107 } 108 109 /// create - Create an interpreter ExecutionEngine. This can never fail. 110 /// 111 static ExecutionEngine *create(Module *M, std::string *ErrorStr = 0); 112 113 /// run - Start execution with the specified function and arguments. 114 /// 115 virtual GenericValue runFunction(Function *F, 116 const std::vector<GenericValue> &ArgValues); 117 118 virtual void *getPointerToNamedFunction(const std::string &Name, 119 bool AbortOnFailure = true) { 120 // FIXME: not implemented. 121 return 0; 122 } 123 124 /// recompileAndRelinkFunction - For the interpreter, functions are always 125 /// up-to-date. 126 /// 127 virtual void *recompileAndRelinkFunction(Function *F) { 128 return getPointerToFunction(F); 129 } 130 131 /// freeMachineCodeForFunction - The interpreter does not generate any code. 132 /// 133 void freeMachineCodeForFunction(Function *F) { } 134 135 // Methods used to execute code: 136 // Place a call on the stack 137 void callFunction(Function *F, const std::vector<GenericValue> &ArgVals); 138 void run(); // Execute instructions until nothing left to do 139 140 // Opcode Implementations 141 void visitReturnInst(ReturnInst &I); 142 void visitBranchInst(BranchInst &I); 143 void visitSwitchInst(SwitchInst &I); 144 void visitIndirectBrInst(IndirectBrInst &I); 145 146 void visitBinaryOperator(BinaryOperator &I); 147 void visitICmpInst(ICmpInst &I); 148 void visitFCmpInst(FCmpInst &I); 149 void visitAllocaInst(AllocaInst &I); 150 void visitLoadInst(LoadInst &I); 151 void visitStoreInst(StoreInst &I); 152 void visitGetElementPtrInst(GetElementPtrInst &I); 153 void visitPHINode(PHINode &PN) { 154 llvm_unreachable("PHI nodes already handled!"); 155 } 156 void visitTruncInst(TruncInst &I); 157 void visitZExtInst(ZExtInst &I); 158 void visitSExtInst(SExtInst &I); 159 void visitFPTruncInst(FPTruncInst &I); 160 void visitFPExtInst(FPExtInst &I); 161 void visitUIToFPInst(UIToFPInst &I); 162 void visitSIToFPInst(SIToFPInst &I); 163 void visitFPToUIInst(FPToUIInst &I); 164 void visitFPToSIInst(FPToSIInst &I); 165 void visitPtrToIntInst(PtrToIntInst &I); 166 void visitIntToPtrInst(IntToPtrInst &I); 167 void visitBitCastInst(BitCastInst &I); 168 void visitSelectInst(SelectInst &I); 169 170 171 void visitCallSite(CallSite CS); 172 void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); } 173 void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); } 174 void visitUnreachableInst(UnreachableInst &I); 175 176 void visitShl(BinaryOperator &I); 177 void visitLShr(BinaryOperator &I); 178 void visitAShr(BinaryOperator &I); 179 180 void visitVAArgInst(VAArgInst &I); 181 void visitExtractElementInst(ExtractElementInst &I); 182 void visitInstruction(Instruction &I) { 183 errs() << I << "\n"; 184 llvm_unreachable("Instruction not interpretable yet!"); 185 } 186 187 GenericValue callExternalFunction(Function *F, 188 const std::vector<GenericValue> &ArgVals); 189 void exitCalled(GenericValue GV); 190 191 void addAtExitHandler(Function *F) { 192 AtExitHandlers.push_back(F); 193 } 194 195 GenericValue *getFirstVarArg () { 196 return &(ECStack.back ().VarArgs[0]); 197 } 198 199 private: // Helper functions 200 GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I, 201 gep_type_iterator E, ExecutionContext &SF); 202 203 // SwitchToNewBasicBlock - Start execution in a new basic block and run any 204 // PHI nodes in the top of the block. This is used for intraprocedural 205 // control flow. 206 // 207 void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF); 208 209 void *getPointerToFunction(Function *F) { return (void*)F; } 210 void *getPointerToBasicBlock(BasicBlock *BB) { return (void*)BB; } 211 212 void initializeExecutionEngine() { } 213 void initializeExternalFunctions(); 214 GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF); 215 GenericValue getOperandValue(Value *V, ExecutionContext &SF); 216 GenericValue executeTruncInst(Value *SrcVal, Type *DstTy, 217 ExecutionContext &SF); 218 GenericValue executeSExtInst(Value *SrcVal, Type *DstTy, 219 ExecutionContext &SF); 220 GenericValue executeZExtInst(Value *SrcVal, Type *DstTy, 221 ExecutionContext &SF); 222 GenericValue executeFPTruncInst(Value *SrcVal, Type *DstTy, 223 ExecutionContext &SF); 224 GenericValue executeFPExtInst(Value *SrcVal, Type *DstTy, 225 ExecutionContext &SF); 226 GenericValue executeFPToUIInst(Value *SrcVal, Type *DstTy, 227 ExecutionContext &SF); 228 GenericValue executeFPToSIInst(Value *SrcVal, Type *DstTy, 229 ExecutionContext &SF); 230 GenericValue executeUIToFPInst(Value *SrcVal, Type *DstTy, 231 ExecutionContext &SF); 232 GenericValue executeSIToFPInst(Value *SrcVal, Type *DstTy, 233 ExecutionContext &SF); 234 GenericValue executePtrToIntInst(Value *SrcVal, Type *DstTy, 235 ExecutionContext &SF); 236 GenericValue executeIntToPtrInst(Value *SrcVal, Type *DstTy, 237 ExecutionContext &SF); 238 GenericValue executeBitCastInst(Value *SrcVal, Type *DstTy, 239 ExecutionContext &SF); 240 GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal, 241 Type *Ty, ExecutionContext &SF); 242 void popStackAndReturnValueToCaller(Type *RetTy, GenericValue Result); 243 244 }; 245 246 } // End llvm namespace 247 248 #endif 249