LLVM API Documentation
00001 //===-- Interpreter.h ------------------------------------------*- C++ -*--===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This header file defines the interpreter structure 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLI_INTERPRETER_H 00015 #define LLI_INTERPRETER_H 00016 00017 #include "llvm/Function.h" 00018 #include "llvm/ExecutionEngine/ExecutionEngine.h" 00019 #include "llvm/ExecutionEngine/GenericValue.h" 00020 #include "llvm/Support/InstVisitor.h" 00021 #include "llvm/Support/CallSite.h" 00022 #include "llvm/Target/TargetData.h" 00023 #include "llvm/Support/DataTypes.h" 00024 00025 namespace llvm { 00026 00027 class IntrinsicLowering; 00028 struct FunctionInfo; 00029 template<typename T> class generic_gep_type_iterator; 00030 class ConstantExpr; 00031 typedef generic_gep_type_iterator<User::const_op_iterator> gep_type_iterator; 00032 00033 00034 // AllocaHolder - Object to track all of the blocks of memory allocated by 00035 // alloca. When the function returns, this object is popped off the execution 00036 // stack, which causes the dtor to be run, which frees all the alloca'd memory. 00037 // 00038 class AllocaHolder { 00039 friend class AllocaHolderHandle; 00040 std::vector<void*> Allocations; 00041 unsigned RefCnt; 00042 public: 00043 AllocaHolder() : RefCnt(0) {} 00044 void add(void *mem) { Allocations.push_back(mem); } 00045 ~AllocaHolder() { 00046 for (unsigned i = 0; i < Allocations.size(); ++i) 00047 free(Allocations[i]); 00048 } 00049 }; 00050 00051 // AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into 00052 // a vector... 00053 // 00054 class AllocaHolderHandle { 00055 AllocaHolder *H; 00056 public: 00057 AllocaHolderHandle() : H(new AllocaHolder()) { H->RefCnt++; } 00058 AllocaHolderHandle(const AllocaHolderHandle &AH) : H(AH.H) { H->RefCnt++; } 00059 ~AllocaHolderHandle() { if (--H->RefCnt == 0) delete H; } 00060 00061 void add(void *mem) { H->add(mem); } 00062 }; 00063 00064 typedef std::vector<GenericValue> ValuePlaneTy; 00065 00066 // ExecutionContext struct - This struct represents one stack frame currently 00067 // executing. 00068 // 00069 struct ExecutionContext { 00070 Function *CurFunction;// The currently executing function 00071 BasicBlock *CurBB; // The currently executing BB 00072 BasicBlock::iterator CurInst; // The next instruction to execute 00073 std::map<Value *, GenericValue> Values; // LLVM values used in this invocation 00074 std::vector<GenericValue> VarArgs; // Values passed through an ellipsis 00075 CallSite Caller; // Holds the call that called subframes. 00076 // NULL if main func or debugger invoked fn 00077 AllocaHolderHandle Allocas; // Track memory allocated by alloca 00078 }; 00079 00080 // Interpreter - This class represents the entirety of the interpreter. 00081 // 00082 class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> { 00083 GenericValue ExitValue; // The return value of the called function 00084 TargetData TD; 00085 IntrinsicLowering *IL; 00086 00087 // The runtime stack of executing code. The top of the stack is the current 00088 // function record. 00089 std::vector<ExecutionContext> ECStack; 00090 00091 // AtExitHandlers - List of functions to call when the program exits, 00092 // registered with the atexit() library function. 00093 std::vector<Function*> AtExitHandlers; 00094 00095 public: 00096 explicit Interpreter(ModuleProvider *M); 00097 ~Interpreter(); 00098 00099 /// runAtExitHandlers - Run any functions registered by the program's calls to 00100 /// atexit(3), which we intercept and store in AtExitHandlers. 00101 /// 00102 void runAtExitHandlers(); 00103 00104 static void Register() { 00105 InterpCtor = create; 00106 } 00107 00108 /// create - Create an interpreter ExecutionEngine. This can never fail. 00109 /// 00110 static ExecutionEngine *create(ModuleProvider *M, std::string *ErrorStr = 0, 00111 bool Fast /*unused*/ = 0); 00112 00113 /// run - Start execution with the specified function and arguments. 00114 /// 00115 virtual GenericValue runFunction(Function *F, 00116 const std::vector<GenericValue> &ArgValues); 00117 00118 /// recompileAndRelinkFunction - For the interpreter, functions are always 00119 /// up-to-date. 00120 /// 00121 virtual void *recompileAndRelinkFunction(Function *F) { 00122 return getPointerToFunction(F); 00123 } 00124 00125 /// freeMachineCodeForFunction - The interpreter does not generate any code. 00126 /// 00127 void freeMachineCodeForFunction(Function *F) { } 00128 00129 // Methods used to execute code: 00130 // Place a call on the stack 00131 void callFunction(Function *F, const std::vector<GenericValue> &ArgVals); 00132 void run(); // Execute instructions until nothing left to do 00133 00134 // Opcode Implementations 00135 void visitReturnInst(ReturnInst &I); 00136 void visitBranchInst(BranchInst &I); 00137 void visitSwitchInst(SwitchInst &I); 00138 00139 void visitBinaryOperator(BinaryOperator &I); 00140 void visitICmpInst(ICmpInst &I); 00141 void visitFCmpInst(FCmpInst &I); 00142 void visitAllocationInst(AllocationInst &I); 00143 void visitFreeInst(FreeInst &I); 00144 void visitLoadInst(LoadInst &I); 00145 void visitStoreInst(StoreInst &I); 00146 void visitGetElementPtrInst(GetElementPtrInst &I); 00147 void visitPHINode(PHINode &PN) { assert(0 && "PHI nodes already handled!"); } 00148 void visitTruncInst(TruncInst &I); 00149 void visitZExtInst(ZExtInst &I); 00150 void visitSExtInst(SExtInst &I); 00151 void visitFPTruncInst(FPTruncInst &I); 00152 void visitFPExtInst(FPExtInst &I); 00153 void visitUIToFPInst(UIToFPInst &I); 00154 void visitSIToFPInst(SIToFPInst &I); 00155 void visitFPToUIInst(FPToUIInst &I); 00156 void visitFPToSIInst(FPToSIInst &I); 00157 void visitPtrToIntInst(PtrToIntInst &I); 00158 void visitIntToPtrInst(IntToPtrInst &I); 00159 void visitBitCastInst(BitCastInst &I); 00160 void visitSelectInst(SelectInst &I); 00161 00162 00163 void visitCallSite(CallSite CS); 00164 void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); } 00165 void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); } 00166 void visitUnwindInst(UnwindInst &I); 00167 void visitUnreachableInst(UnreachableInst &I); 00168 00169 void visitShl(BinaryOperator &I); 00170 void visitLShr(BinaryOperator &I); 00171 void visitAShr(BinaryOperator &I); 00172 00173 void visitVAArgInst(VAArgInst &I); 00174 void visitInstruction(Instruction &I) { 00175 cerr << I; 00176 assert(0 && "Instruction not interpretable yet!"); 00177 } 00178 00179 GenericValue callExternalFunction(Function *F, 00180 const std::vector<GenericValue> &ArgVals); 00181 void exitCalled(GenericValue GV); 00182 00183 void addAtExitHandler(Function *F) { 00184 AtExitHandlers.push_back(F); 00185 } 00186 00187 GenericValue *getFirstVarArg () { 00188 return &(ECStack.back ().VarArgs[0]); 00189 } 00190 00191 //FIXME: private: 00192 public: 00193 GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I, 00194 gep_type_iterator E, ExecutionContext &SF); 00195 00196 private: // Helper functions 00197 // SwitchToNewBasicBlock - Start execution in a new basic block and run any 00198 // PHI nodes in the top of the block. This is used for intraprocedural 00199 // control flow. 00200 // 00201 void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF); 00202 00203 void *getPointerToFunction(Function *F) { return (void*)F; } 00204 00205 void initializeExecutionEngine(); 00206 void initializeExternalFunctions(); 00207 GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF); 00208 GenericValue getOperandValue(Value *V, ExecutionContext &SF); 00209 GenericValue executeTruncInst(Value *SrcVal, const Type *DstTy, 00210 ExecutionContext &SF); 00211 GenericValue executeSExtInst(Value *SrcVal, const Type *DstTy, 00212 ExecutionContext &SF); 00213 GenericValue executeZExtInst(Value *SrcVal, const Type *DstTy, 00214 ExecutionContext &SF); 00215 GenericValue executeFPTruncInst(Value *SrcVal, const Type *DstTy, 00216 ExecutionContext &SF); 00217 GenericValue executeFPExtInst(Value *SrcVal, const Type *DstTy, 00218 ExecutionContext &SF); 00219 GenericValue executeFPToUIInst(Value *SrcVal, const Type *DstTy, 00220 ExecutionContext &SF); 00221 GenericValue executeFPToSIInst(Value *SrcVal, const Type *DstTy, 00222 ExecutionContext &SF); 00223 GenericValue executeUIToFPInst(Value *SrcVal, const Type *DstTy, 00224 ExecutionContext &SF); 00225 GenericValue executeSIToFPInst(Value *SrcVal, const Type *DstTy, 00226 ExecutionContext &SF); 00227 GenericValue executePtrToIntInst(Value *SrcVal, const Type *DstTy, 00228 ExecutionContext &SF); 00229 GenericValue executeIntToPtrInst(Value *SrcVal, const Type *DstTy, 00230 ExecutionContext &SF); 00231 GenericValue executeBitCastInst(Value *SrcVal, const Type *DstTy, 00232 ExecutionContext &SF); 00233 GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal, 00234 const Type *Ty, ExecutionContext &SF); 00235 void popStackAndReturnValueToCaller(const Type *RetTy, GenericValue Result); 00236 00237 }; 00238 00239 } // End llvm namespace 00240 00241 #endif