Interpreter.h revision 195098
1193323Sed//===-- Interpreter.h ------------------------------------------*- C++ -*--===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This header file defines the interpreter structure 11193323Sed// 12193323Sed//===----------------------------------------------------------------------===// 13193323Sed 14193323Sed#ifndef LLI_INTERPRETER_H 15193323Sed#define LLI_INTERPRETER_H 16193323Sed 17193323Sed#include "llvm/Function.h" 18193323Sed#include "llvm/ExecutionEngine/ExecutionEngine.h" 19193323Sed#include "llvm/ExecutionEngine/GenericValue.h" 20193323Sed#include "llvm/Support/InstVisitor.h" 21193323Sed#include "llvm/Support/CallSite.h" 22193323Sed#include "llvm/Target/TargetData.h" 23193323Sed#include "llvm/Support/DataTypes.h" 24193323Sed 25193323Sednamespace llvm { 26193323Sed 27193323Sedclass IntrinsicLowering; 28193323Sedstruct FunctionInfo; 29193323Sedtemplate<typename T> class generic_gep_type_iterator; 30193323Sedclass ConstantExpr; 31193323Sedtypedef generic_gep_type_iterator<User::const_op_iterator> gep_type_iterator; 32193323Sed 33193323Sed 34193323Sed// AllocaHolder - Object to track all of the blocks of memory allocated by 35193323Sed// alloca. When the function returns, this object is popped off the execution 36193323Sed// stack, which causes the dtor to be run, which frees all the alloca'd memory. 37193323Sed// 38193323Sedclass AllocaHolder { 39193323Sed friend class AllocaHolderHandle; 40193323Sed std::vector<void*> Allocations; 41193323Sed unsigned RefCnt; 42193323Sedpublic: 43193323Sed AllocaHolder() : RefCnt(0) {} 44193323Sed void add(void *mem) { Allocations.push_back(mem); } 45193323Sed ~AllocaHolder() { 46193323Sed for (unsigned i = 0; i < Allocations.size(); ++i) 47193323Sed free(Allocations[i]); 48193323Sed } 49193323Sed}; 50193323Sed 51193323Sed// AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into 52193323Sed// a vector... 53193323Sed// 54193323Sedclass AllocaHolderHandle { 55193323Sed AllocaHolder *H; 56193323Sedpublic: 57193323Sed AllocaHolderHandle() : H(new AllocaHolder()) { H->RefCnt++; } 58193323Sed AllocaHolderHandle(const AllocaHolderHandle &AH) : H(AH.H) { H->RefCnt++; } 59193323Sed ~AllocaHolderHandle() { if (--H->RefCnt == 0) delete H; } 60193323Sed 61193323Sed void add(void *mem) { H->add(mem); } 62193323Sed}; 63193323Sed 64193323Sedtypedef std::vector<GenericValue> ValuePlaneTy; 65193323Sed 66193323Sed// ExecutionContext struct - This struct represents one stack frame currently 67193323Sed// executing. 68193323Sed// 69193323Sedstruct ExecutionContext { 70193323Sed Function *CurFunction;// The currently executing function 71193323Sed BasicBlock *CurBB; // The currently executing BB 72193323Sed BasicBlock::iterator CurInst; // The next instruction to execute 73193323Sed std::map<Value *, GenericValue> Values; // LLVM values used in this invocation 74193323Sed std::vector<GenericValue> VarArgs; // Values passed through an ellipsis 75193323Sed CallSite Caller; // Holds the call that called subframes. 76193323Sed // NULL if main func or debugger invoked fn 77193323Sed AllocaHolderHandle Allocas; // Track memory allocated by alloca 78193323Sed}; 79193323Sed 80193323Sed// Interpreter - This class represents the entirety of the interpreter. 81193323Sed// 82193323Sedclass Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> { 83193323Sed GenericValue ExitValue; // The return value of the called function 84193323Sed TargetData TD; 85193323Sed IntrinsicLowering *IL; 86193323Sed 87193323Sed // The runtime stack of executing code. The top of the stack is the current 88193323Sed // function record. 89193323Sed std::vector<ExecutionContext> ECStack; 90193323Sed 91193323Sed // AtExitHandlers - List of functions to call when the program exits, 92193323Sed // registered with the atexit() library function. 93193323Sed std::vector<Function*> AtExitHandlers; 94193323Sed 95193323Sedpublic: 96193323Sed explicit Interpreter(ModuleProvider *M); 97193323Sed ~Interpreter(); 98193323Sed 99193323Sed /// runAtExitHandlers - Run any functions registered by the program's calls to 100193323Sed /// atexit(3), which we intercept and store in AtExitHandlers. 101193323Sed /// 102193323Sed void runAtExitHandlers(); 103193323Sed 104193323Sed static void Register() { 105193323Sed InterpCtor = create; 106193323Sed } 107193323Sed 108193323Sed /// create - Create an interpreter ExecutionEngine. This can never fail. 109193323Sed /// 110193323Sed static ExecutionEngine *create(ModuleProvider *M, std::string *ErrorStr = 0, 111193323Sed CodeGenOpt::Level = CodeGenOpt::Default); 112193323Sed 113193323Sed /// run - Start execution with the specified function and arguments. 114193323Sed /// 115193323Sed virtual GenericValue runFunction(Function *F, 116193323Sed const std::vector<GenericValue> &ArgValues); 117193323Sed 118193323Sed /// recompileAndRelinkFunction - For the interpreter, functions are always 119193323Sed /// up-to-date. 120193323Sed /// 121193323Sed virtual void *recompileAndRelinkFunction(Function *F) { 122193323Sed return getPointerToFunction(F); 123193323Sed } 124193323Sed 125193323Sed /// freeMachineCodeForFunction - The interpreter does not generate any code. 126193323Sed /// 127193323Sed void freeMachineCodeForFunction(Function *F) { } 128193323Sed 129193323Sed // Methods used to execute code: 130193323Sed // Place a call on the stack 131193323Sed void callFunction(Function *F, const std::vector<GenericValue> &ArgVals); 132193323Sed void run(); // Execute instructions until nothing left to do 133193323Sed 134193323Sed // Opcode Implementations 135193323Sed void visitReturnInst(ReturnInst &I); 136193323Sed void visitBranchInst(BranchInst &I); 137193323Sed void visitSwitchInst(SwitchInst &I); 138193323Sed 139193323Sed void visitBinaryOperator(BinaryOperator &I); 140193323Sed void visitICmpInst(ICmpInst &I); 141193323Sed void visitFCmpInst(FCmpInst &I); 142193323Sed void visitAllocationInst(AllocationInst &I); 143193323Sed void visitFreeInst(FreeInst &I); 144193323Sed void visitLoadInst(LoadInst &I); 145193323Sed void visitStoreInst(StoreInst &I); 146193323Sed void visitGetElementPtrInst(GetElementPtrInst &I); 147193323Sed void visitPHINode(PHINode &PN) { assert(0 && "PHI nodes already handled!"); } 148193323Sed void visitTruncInst(TruncInst &I); 149193323Sed void visitZExtInst(ZExtInst &I); 150193323Sed void visitSExtInst(SExtInst &I); 151193323Sed void visitFPTruncInst(FPTruncInst &I); 152193323Sed void visitFPExtInst(FPExtInst &I); 153193323Sed void visitUIToFPInst(UIToFPInst &I); 154193323Sed void visitSIToFPInst(SIToFPInst &I); 155193323Sed void visitFPToUIInst(FPToUIInst &I); 156193323Sed void visitFPToSIInst(FPToSIInst &I); 157193323Sed void visitPtrToIntInst(PtrToIntInst &I); 158193323Sed void visitIntToPtrInst(IntToPtrInst &I); 159193323Sed void visitBitCastInst(BitCastInst &I); 160193323Sed void visitSelectInst(SelectInst &I); 161193323Sed 162193323Sed 163193323Sed void visitCallSite(CallSite CS); 164193323Sed void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); } 165193323Sed void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); } 166193323Sed void visitUnwindInst(UnwindInst &I); 167193323Sed void visitUnreachableInst(UnreachableInst &I); 168193323Sed 169193323Sed void visitShl(BinaryOperator &I); 170193323Sed void visitLShr(BinaryOperator &I); 171193323Sed void visitAShr(BinaryOperator &I); 172193323Sed 173193323Sed void visitVAArgInst(VAArgInst &I); 174193323Sed void visitInstruction(Instruction &I) { 175193323Sed cerr << I; 176193323Sed assert(0 && "Instruction not interpretable yet!"); 177193323Sed } 178193323Sed 179193323Sed GenericValue callExternalFunction(Function *F, 180193323Sed const std::vector<GenericValue> &ArgVals); 181193323Sed void exitCalled(GenericValue GV); 182193323Sed 183193323Sed void addAtExitHandler(Function *F) { 184193323Sed AtExitHandlers.push_back(F); 185193323Sed } 186193323Sed 187193323Sed GenericValue *getFirstVarArg () { 188193323Sed return &(ECStack.back ().VarArgs[0]); 189193323Sed } 190193323Sed 191193323Sed //FIXME: private: 192193323Sedpublic: 193193323Sed GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I, 194193323Sed gep_type_iterator E, ExecutionContext &SF); 195193323Sed 196193323Sedprivate: // Helper functions 197193323Sed // SwitchToNewBasicBlock - Start execution in a new basic block and run any 198193323Sed // PHI nodes in the top of the block. This is used for intraprocedural 199193323Sed // control flow. 200193323Sed // 201193323Sed void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF); 202193323Sed 203193323Sed void *getPointerToFunction(Function *F) { return (void*)F; } 204193323Sed 205195098Sed void initializeExecutionEngine() { } 206193323Sed void initializeExternalFunctions(); 207193323Sed GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF); 208193323Sed GenericValue getOperandValue(Value *V, ExecutionContext &SF); 209193323Sed GenericValue executeTruncInst(Value *SrcVal, const Type *DstTy, 210193323Sed ExecutionContext &SF); 211193323Sed GenericValue executeSExtInst(Value *SrcVal, const Type *DstTy, 212193323Sed ExecutionContext &SF); 213193323Sed GenericValue executeZExtInst(Value *SrcVal, const Type *DstTy, 214193323Sed ExecutionContext &SF); 215193323Sed GenericValue executeFPTruncInst(Value *SrcVal, const Type *DstTy, 216193323Sed ExecutionContext &SF); 217193323Sed GenericValue executeFPExtInst(Value *SrcVal, const Type *DstTy, 218193323Sed ExecutionContext &SF); 219193323Sed GenericValue executeFPToUIInst(Value *SrcVal, const Type *DstTy, 220193323Sed ExecutionContext &SF); 221193323Sed GenericValue executeFPToSIInst(Value *SrcVal, const Type *DstTy, 222193323Sed ExecutionContext &SF); 223193323Sed GenericValue executeUIToFPInst(Value *SrcVal, const Type *DstTy, 224193323Sed ExecutionContext &SF); 225193323Sed GenericValue executeSIToFPInst(Value *SrcVal, const Type *DstTy, 226193323Sed ExecutionContext &SF); 227193323Sed GenericValue executePtrToIntInst(Value *SrcVal, const Type *DstTy, 228193323Sed ExecutionContext &SF); 229193323Sed GenericValue executeIntToPtrInst(Value *SrcVal, const Type *DstTy, 230193323Sed ExecutionContext &SF); 231193323Sed GenericValue executeBitCastInst(Value *SrcVal, const Type *DstTy, 232193323Sed ExecutionContext &SF); 233193323Sed GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal, 234193323Sed const Type *Ty, ExecutionContext &SF); 235193323Sed void popStackAndReturnValueToCaller(const Type *RetTy, GenericValue Result); 236193323Sed 237193323Sed}; 238193323Sed 239193323Sed} // End llvm namespace 240193323Sed 241193323Sed#endif 242