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 #include "llvm/Support/ErrorHandling.h" 00025 00026 namespace llvm { 00027 00028 class IntrinsicLowering; 00029 struct FunctionInfo; 00030 template<typename T> class generic_gep_type_iterator; 00031 class ConstantExpr; 00032 typedef generic_gep_type_iterator<User::const_op_iterator> gep_type_iterator; 00033 00034 00035 // AllocaHolder - Object to track all of the blocks of memory allocated by 00036 // alloca. When the function returns, this object is popped off the execution 00037 // stack, which causes the dtor to be run, which frees all the alloca'd memory. 00038 // 00039 class AllocaHolder { 00040 friend class AllocaHolderHandle; 00041 std::vector<void*> Allocations; 00042 unsigned RefCnt; 00043 public: 00044 AllocaHolder() : RefCnt(0) {} 00045 void add(void *mem) { Allocations.push_back(mem); } 00046 ~AllocaHolder() { 00047 for (unsigned i = 0; i < Allocations.size(); ++i) 00048 free(Allocations[i]); 00049 } 00050 }; 00051 00052 // AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into 00053 // a vector... 00054 // 00055 class AllocaHolderHandle { 00056 AllocaHolder *H; 00057 public: 00058 AllocaHolderHandle() : H(new AllocaHolder()) { H->RefCnt++; } 00059 AllocaHolderHandle(const AllocaHolderHandle &AH) : H(AH.H) { H->RefCnt++; } 00060 ~AllocaHolderHandle() { if (--H->RefCnt == 0) delete H; } 00061 00062 void add(void *mem) { H->add(mem); } 00063 }; 00064 00065 typedef std::vector<GenericValue> ValuePlaneTy; 00066 00067 // ExecutionContext struct - This struct represents one stack frame currently 00068 // executing. 00069 // 00070 struct ExecutionContext { 00071 Function *CurFunction;// The currently executing function 00072 BasicBlock *CurBB; // The currently executing BB 00073 BasicBlock::iterator CurInst; // The next instruction to execute 00074 std::map<Value *, GenericValue> Values; // LLVM values used in this invocation 00075 std::vector<GenericValue> VarArgs; // Values passed through an ellipsis 00076 CallSite Caller; // Holds the call that called subframes. 00077 // NULL if main func or debugger invoked fn 00078 AllocaHolderHandle Allocas; // Track memory allocated by alloca 00079 }; 00080 00081 // Interpreter - This class represents the entirety of the interpreter. 00082 // 00083 class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> { 00084 GenericValue ExitValue; // The return value of the called function 00085 TargetData TD; 00086 IntrinsicLowering *IL; 00087 00088 // The runtime stack of executing code. The top of the stack is the current 00089 // function record. 00090 std::vector<ExecutionContext> ECStack; 00091 00092 // AtExitHandlers - List of functions to call when the program exits, 00093 // registered with the atexit() library function. 00094 std::vector<Function*> AtExitHandlers; 00095 00096 public: 00097 explicit Interpreter(ModuleProvider *M); 00098 ~Interpreter(); 00099 00100 /// runAtExitHandlers - Run any functions registered by the program's calls to 00101 /// atexit(3), which we intercept and store in AtExitHandlers. 00102 /// 00103 void runAtExitHandlers(); 00104 00105 static void Register() { 00106 InterpCtor = create; 00107 } 00108 00109 /// create - Create an interpreter ExecutionEngine. This can never fail. 00110 /// 00111 static ExecutionEngine *create(ModuleProvider *M, std::string *ErrorStr = 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) { 00148 llvm_unreachable("PHI nodes already handled!"); 00149 } 00150 void visitTruncInst(TruncInst &I); 00151 void visitZExtInst(ZExtInst &I); 00152 void visitSExtInst(SExtInst &I); 00153 void visitFPTruncInst(FPTruncInst &I); 00154 void visitFPExtInst(FPExtInst &I); 00155 void visitUIToFPInst(UIToFPInst &I); 00156 void visitSIToFPInst(SIToFPInst &I); 00157 void visitFPToUIInst(FPToUIInst &I); 00158 void visitFPToSIInst(FPToSIInst &I); 00159 void visitPtrToIntInst(PtrToIntInst &I); 00160 void visitIntToPtrInst(IntToPtrInst &I); 00161 void visitBitCastInst(BitCastInst &I); 00162 void visitSelectInst(SelectInst &I); 00163 00164 00165 void visitCallSite(CallSite CS); 00166 void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); } 00167 void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); } 00168 void visitUnwindInst(UnwindInst &I); 00169 void visitUnreachableInst(UnreachableInst &I); 00170 00171 void visitShl(BinaryOperator &I); 00172 void visitLShr(BinaryOperator &I); 00173 void visitAShr(BinaryOperator &I); 00174 00175 void visitVAArgInst(VAArgInst &I); 00176 void visitInstruction(Instruction &I) { 00177 cerr << I; 00178 llvm_unreachable("Instruction not interpretable yet!"); 00179 } 00180 00181 GenericValue callExternalFunction(Function *F, 00182 const std::vector<GenericValue> &ArgVals); 00183 void exitCalled(GenericValue GV); 00184 00185 void addAtExitHandler(Function *F) { 00186 AtExitHandlers.push_back(F); 00187 } 00188 00189 GenericValue *getFirstVarArg () { 00190 return &(ECStack.back ().VarArgs[0]); 00191 } 00192 00193 //FIXME: private: 00194 public: 00195 GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I, 00196 gep_type_iterator E, ExecutionContext &SF); 00197 00198 private: // Helper functions 00199 // SwitchToNewBasicBlock - Start execution in a new basic block and run any 00200 // PHI nodes in the top of the block. This is used for intraprocedural 00201 // control flow. 00202 // 00203 void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF); 00204 00205 void *getPointerToFunction(Function *F) { return (void*)F; } 00206 00207 void initializeExecutionEngine() { } 00208 void initializeExternalFunctions(); 00209 GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF); 00210 GenericValue getOperandValue(Value *V, ExecutionContext &SF); 00211 GenericValue executeTruncInst(Value *SrcVal, const Type *DstTy, 00212 ExecutionContext &SF); 00213 GenericValue executeSExtInst(Value *SrcVal, const Type *DstTy, 00214 ExecutionContext &SF); 00215 GenericValue executeZExtInst(Value *SrcVal, const Type *DstTy, 00216 ExecutionContext &SF); 00217 GenericValue executeFPTruncInst(Value *SrcVal, const Type *DstTy, 00218 ExecutionContext &SF); 00219 GenericValue executeFPExtInst(Value *SrcVal, const Type *DstTy, 00220 ExecutionContext &SF); 00221 GenericValue executeFPToUIInst(Value *SrcVal, const Type *DstTy, 00222 ExecutionContext &SF); 00223 GenericValue executeFPToSIInst(Value *SrcVal, const Type *DstTy, 00224 ExecutionContext &SF); 00225 GenericValue executeUIToFPInst(Value *SrcVal, const Type *DstTy, 00226 ExecutionContext &SF); 00227 GenericValue executeSIToFPInst(Value *SrcVal, const Type *DstTy, 00228 ExecutionContext &SF); 00229 GenericValue executePtrToIntInst(Value *SrcVal, const Type *DstTy, 00230 ExecutionContext &SF); 00231 GenericValue executeIntToPtrInst(Value *SrcVal, const Type *DstTy, 00232 ExecutionContext &SF); 00233 GenericValue executeBitCastInst(Value *SrcVal, const Type *DstTy, 00234 ExecutionContext &SF); 00235 GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal, 00236 const Type *Ty, ExecutionContext &SF); 00237 void popStackAndReturnValueToCaller(const Type *RetTy, GenericValue Result); 00238 00239 }; 00240 00241 } // End llvm namespace 00242 00243 #endif