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/ADT/APInt.h" 00021 #include "llvm/Support/InstVisitor.h" 00022 #include "llvm/Support/CallSite.h" 00023 #include "llvm/Target/TargetData.h" 00024 #include "llvm/Support/DataTypes.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 bool Fast /*unused*/ = 0); 00113 00114 /// run - Start execution with the specified function and arguments. 00115 /// 00116 virtual GenericValue runFunction(Function *F, 00117 const std::vector<GenericValue> &ArgValues); 00118 00119 /// recompileAndRelinkFunction - For the interpreter, functions are always 00120 /// up-to-date. 00121 /// 00122 virtual void *recompileAndRelinkFunction(Function *F) { 00123 return getPointerToFunction(F); 00124 } 00125 00126 /// freeMachineCodeForFunction - The interpreter does not generate any code. 00127 /// 00128 void freeMachineCodeForFunction(Function *F) { } 00129 00130 // Methods used to execute code: 00131 // Place a call on the stack 00132 void callFunction(Function *F, const std::vector<GenericValue> &ArgVals); 00133 void run(); // Execute instructions until nothing left to do 00134 00135 // Opcode Implementations 00136 void visitReturnInst(ReturnInst &I); 00137 void visitBranchInst(BranchInst &I); 00138 void visitSwitchInst(SwitchInst &I); 00139 00140 void visitBinaryOperator(BinaryOperator &I); 00141 void visitICmpInst(ICmpInst &I); 00142 void visitFCmpInst(FCmpInst &I); 00143 void visitAllocationInst(AllocationInst &I); 00144 void visitFreeInst(FreeInst &I); 00145 void visitLoadInst(LoadInst &I); 00146 void visitStoreInst(StoreInst &I); 00147 void visitGetElementPtrInst(GetElementPtrInst &I); 00148 void visitPHINode(PHINode &PN) { assert(0 && "PHI nodes already handled!"); } 00149 void visitTruncInst(TruncInst &I); 00150 void visitZExtInst(ZExtInst &I); 00151 void visitSExtInst(SExtInst &I); 00152 void visitFPTruncInst(FPTruncInst &I); 00153 void visitFPExtInst(FPExtInst &I); 00154 void visitUIToFPInst(UIToFPInst &I); 00155 void visitSIToFPInst(SIToFPInst &I); 00156 void visitFPToUIInst(FPToUIInst &I); 00157 void visitFPToSIInst(FPToSIInst &I); 00158 void visitPtrToIntInst(PtrToIntInst &I); 00159 void visitIntToPtrInst(IntToPtrInst &I); 00160 void visitBitCastInst(BitCastInst &I); 00161 void visitSelectInst(SelectInst &I); 00162 00163 00164 void visitCallSite(CallSite CS); 00165 void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); } 00166 void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); } 00167 void visitUnwindInst(UnwindInst &I); 00168 void visitUnreachableInst(UnreachableInst &I); 00169 00170 void visitShl(BinaryOperator &I); 00171 void visitLShr(BinaryOperator &I); 00172 void visitAShr(BinaryOperator &I); 00173 00174 void visitVAArgInst(VAArgInst &I); 00175 void visitInstruction(Instruction &I) { 00176 cerr << I; 00177 assert(0 && "Instruction not interpretable yet!"); 00178 } 00179 00180 GenericValue callExternalFunction(Function *F, 00181 const std::vector<GenericValue> &ArgVals); 00182 void exitCalled(GenericValue GV); 00183 00184 void addAtExitHandler(Function *F) { 00185 AtExitHandlers.push_back(F); 00186 } 00187 00188 GenericValue *getFirstVarArg () { 00189 return &(ECStack.back ().VarArgs[0]); 00190 } 00191 00192 //FIXME: private: 00193 public: 00194 GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I, 00195 gep_type_iterator E, ExecutionContext &SF); 00196 00197 private: // Helper functions 00198 // SwitchToNewBasicBlock - Start execution in a new basic block and run any 00199 // PHI nodes in the top of the block. This is used for intraprocedural 00200 // control flow. 00201 // 00202 void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF); 00203 00204 void *getPointerToFunction(Function *F) { return (void*)F; } 00205 00206 void initializeExecutionEngine(); 00207 void initializeExternalFunctions(); 00208 GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF); 00209 GenericValue getOperandValue(Value *V, ExecutionContext &SF); 00210 GenericValue executeTruncInst(Value *SrcVal, const Type *DstTy, 00211 ExecutionContext &SF); 00212 GenericValue executeSExtInst(Value *SrcVal, const Type *DstTy, 00213 ExecutionContext &SF); 00214 GenericValue executeZExtInst(Value *SrcVal, const Type *DstTy, 00215 ExecutionContext &SF); 00216 GenericValue executeFPTruncInst(Value *SrcVal, const Type *DstTy, 00217 ExecutionContext &SF); 00218 GenericValue executeFPExtInst(Value *SrcVal, const Type *DstTy, 00219 ExecutionContext &SF); 00220 GenericValue executeFPToUIInst(Value *SrcVal, const Type *DstTy, 00221 ExecutionContext &SF); 00222 GenericValue executeFPToSIInst(Value *SrcVal, const Type *DstTy, 00223 ExecutionContext &SF); 00224 GenericValue executeUIToFPInst(Value *SrcVal, const Type *DstTy, 00225 ExecutionContext &SF); 00226 GenericValue executeSIToFPInst(Value *SrcVal, const Type *DstTy, 00227 ExecutionContext &SF); 00228 GenericValue executePtrToIntInst(Value *SrcVal, const Type *DstTy, 00229 ExecutionContext &SF); 00230 GenericValue executeIntToPtrInst(Value *SrcVal, const Type *DstTy, 00231 ExecutionContext &SF); 00232 GenericValue executeBitCastInst(Value *SrcVal, const Type *DstTy, 00233 ExecutionContext &SF); 00234 GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal, 00235 const Type *Ty, ExecutionContext &SF); 00236 void popStackAndReturnValueToCaller(const Type *RetTy, GenericValue Result); 00237 00238 }; 00239 00240 } // End llvm namespace 00241 00242 #endif