Interpreter.h revision 203954
1219820Sjeff//===-- Interpreter.h ------------------------------------------*- C++ -*--===// 2219820Sjeff// 3219820Sjeff// The LLVM Compiler Infrastructure 4219820Sjeff// 5219820Sjeff// This file is distributed under the University of Illinois Open Source 6219820Sjeff// License. See LICENSE.TXT for details. 7219820Sjeff// 8219820Sjeff//===----------------------------------------------------------------------===// 9219820Sjeff// 10219820Sjeff// This header file defines the interpreter structure 11219820Sjeff// 12219820Sjeff//===----------------------------------------------------------------------===// 13219820Sjeff 14219820Sjeff#ifndef LLI_INTERPRETER_H 15219820Sjeff#define LLI_INTERPRETER_H 16219820Sjeff 17219820Sjeff#include "llvm/Function.h" 18219820Sjeff#include "llvm/ExecutionEngine/ExecutionEngine.h" 19219820Sjeff#include "llvm/ExecutionEngine/GenericValue.h" 20219820Sjeff#include "llvm/Target/TargetData.h" 21219820Sjeff#include "llvm/Support/CallSite.h" 22219820Sjeff#include "llvm/System/DataTypes.h" 23219820Sjeff#include "llvm/Support/ErrorHandling.h" 24219820Sjeff#include "llvm/Support/InstVisitor.h" 25219820Sjeff#include "llvm/Support/raw_ostream.h" 26219820Sjeffnamespace llvm { 27219820Sjeff 28219820Sjeffclass IntrinsicLowering; 29219820Sjeffstruct FunctionInfo; 30219820Sjefftemplate<typename T> class generic_gep_type_iterator; 31219820Sjeffclass ConstantExpr; 32219820Sjefftypedef generic_gep_type_iterator<User::const_op_iterator> gep_type_iterator; 33219820Sjeff 34219820Sjeff 35219820Sjeff// AllocaHolder - Object to track all of the blocks of memory allocated by 36219820Sjeff// alloca. When the function returns, this object is popped off the execution 37219820Sjeff// stack, which causes the dtor to be run, which frees all the alloca'd memory. 38219820Sjeff// 39219820Sjeffclass AllocaHolder { 40219820Sjeff friend class AllocaHolderHandle; 41219820Sjeff std::vector<void*> Allocations; 42219820Sjeff unsigned RefCnt; 43219820Sjeffpublic: 44219820Sjeff AllocaHolder() : RefCnt(0) {} 45219820Sjeff void add(void *mem) { Allocations.push_back(mem); } 46219820Sjeff ~AllocaHolder() { 47219820Sjeff for (unsigned i = 0; i < Allocations.size(); ++i) 48219820Sjeff free(Allocations[i]); 49219820Sjeff } 50219820Sjeff}; 51219820Sjeff 52219820Sjeff// AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into 53219820Sjeff// a vector... 54219820Sjeff// 55219820Sjeffclass AllocaHolderHandle { 56219820Sjeff AllocaHolder *H; 57219820Sjeffpublic: 58219820Sjeff AllocaHolderHandle() : H(new AllocaHolder()) { H->RefCnt++; } 59219820Sjeff AllocaHolderHandle(const AllocaHolderHandle &AH) : H(AH.H) { H->RefCnt++; } 60219820Sjeff ~AllocaHolderHandle() { if (--H->RefCnt == 0) delete H; } 61219820Sjeff 62219820Sjeff void add(void *mem) { H->add(mem); } 63219820Sjeff}; 64219820Sjeff 65219820Sjefftypedef std::vector<GenericValue> ValuePlaneTy; 66219820Sjeff 67219820Sjeff// ExecutionContext struct - This struct represents one stack frame currently 68219820Sjeff// executing. 69219820Sjeff// 70219820Sjeffstruct ExecutionContext { 71219820Sjeff Function *CurFunction;// The currently executing function 72219820Sjeff BasicBlock *CurBB; // The currently executing BB 73219820Sjeff BasicBlock::iterator CurInst; // The next instruction to execute 74219820Sjeff std::map<Value *, GenericValue> Values; // LLVM values used in this invocation 75219820Sjeff std::vector<GenericValue> VarArgs; // Values passed through an ellipsis 76219820Sjeff CallSite Caller; // Holds the call that called subframes. 77219820Sjeff // NULL if main func or debugger invoked fn 78219820Sjeff AllocaHolderHandle Allocas; // Track memory allocated by alloca 79219820Sjeff}; 80219820Sjeff 81219820Sjeff// Interpreter - This class represents the entirety of the interpreter. 82219820Sjeff// 83219820Sjeffclass Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> { 84219820Sjeff GenericValue ExitValue; // The return value of the called function 85219820Sjeff TargetData TD; 86219820Sjeff IntrinsicLowering *IL; 87219820Sjeff 88219820Sjeff // The runtime stack of executing code. The top of the stack is the current 89219820Sjeff // function record. 90219820Sjeff std::vector<ExecutionContext> ECStack; 91219820Sjeff 92219820Sjeff // AtExitHandlers - List of functions to call when the program exits, 93219820Sjeff // registered with the atexit() library function. 94219820Sjeff std::vector<Function*> AtExitHandlers; 95219820Sjeff 96219820Sjeffpublic: 97219820Sjeff explicit Interpreter(Module *M); 98219820Sjeff ~Interpreter(); 99219820Sjeff 100219820Sjeff /// runAtExitHandlers - Run any functions registered by the program's calls to 101219820Sjeff /// atexit(3), which we intercept and store in AtExitHandlers. 102219820Sjeff /// 103219820Sjeff void runAtExitHandlers(); 104219820Sjeff 105219820Sjeff static void Register() { 106219820Sjeff InterpCtor = create; 107219820Sjeff } 108219820Sjeff 109219820Sjeff /// create - Create an interpreter ExecutionEngine. This can never fail. 110219820Sjeff /// 111219820Sjeff static ExecutionEngine *create(Module *M, std::string *ErrorStr = 0); 112219820Sjeff 113219820Sjeff /// run - Start execution with the specified function and arguments. 114219820Sjeff /// 115219820Sjeff virtual GenericValue runFunction(Function *F, 116219820Sjeff const std::vector<GenericValue> &ArgValues); 117219820Sjeff 118219820Sjeff /// recompileAndRelinkFunction - For the interpreter, functions are always 119219820Sjeff /// up-to-date. 120219820Sjeff /// 121219820Sjeff virtual void *recompileAndRelinkFunction(Function *F) { 122219820Sjeff return getPointerToFunction(F); 123219820Sjeff } 124219820Sjeff 125219820Sjeff /// freeMachineCodeForFunction - The interpreter does not generate any code. 126219820Sjeff /// 127219820Sjeff void freeMachineCodeForFunction(Function *F) { } 128219820Sjeff 129219820Sjeff // Methods used to execute code: 130219820Sjeff // Place a call on the stack 131219820Sjeff void callFunction(Function *F, const std::vector<GenericValue> &ArgVals); 132219820Sjeff void run(); // Execute instructions until nothing left to do 133219820Sjeff 134219820Sjeff // Opcode Implementations 135219820Sjeff void visitReturnInst(ReturnInst &I); 136219820Sjeff void visitBranchInst(BranchInst &I); 137219820Sjeff void visitSwitchInst(SwitchInst &I); 138219820Sjeff void visitIndirectBrInst(IndirectBrInst &I); 139219820Sjeff 140219820Sjeff void visitBinaryOperator(BinaryOperator &I); 141219820Sjeff void visitICmpInst(ICmpInst &I); 142219820Sjeff void visitFCmpInst(FCmpInst &I); 143219820Sjeff void visitAllocaInst(AllocaInst &I); 144219820Sjeff void visitLoadInst(LoadInst &I); 145219820Sjeff void visitStoreInst(StoreInst &I); 146219820Sjeff void visitGetElementPtrInst(GetElementPtrInst &I); 147219820Sjeff void visitPHINode(PHINode &PN) { 148219820Sjeff llvm_unreachable("PHI nodes already handled!"); 149219820Sjeff } 150219820Sjeff void visitTruncInst(TruncInst &I); 151219820Sjeff void visitZExtInst(ZExtInst &I); 152219820Sjeff void visitSExtInst(SExtInst &I); 153219820Sjeff void visitFPTruncInst(FPTruncInst &I); 154219820Sjeff void visitFPExtInst(FPExtInst &I); 155219820Sjeff void visitUIToFPInst(UIToFPInst &I); 156219820Sjeff void visitSIToFPInst(SIToFPInst &I); 157219820Sjeff void visitFPToUIInst(FPToUIInst &I); 158219820Sjeff void visitFPToSIInst(FPToSIInst &I); 159219820Sjeff void visitPtrToIntInst(PtrToIntInst &I); 160219820Sjeff void visitIntToPtrInst(IntToPtrInst &I); 161219820Sjeff void visitBitCastInst(BitCastInst &I); 162219820Sjeff void visitSelectInst(SelectInst &I); 163219820Sjeff 164219820Sjeff 165219820Sjeff void visitCallSite(CallSite CS); 166219820Sjeff void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); } 167219820Sjeff void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); } 168219820Sjeff void visitUnwindInst(UnwindInst &I); 169219820Sjeff void visitUnreachableInst(UnreachableInst &I); 170219820Sjeff 171219820Sjeff void visitShl(BinaryOperator &I); 172219820Sjeff void visitLShr(BinaryOperator &I); 173219820Sjeff void visitAShr(BinaryOperator &I); 174219820Sjeff 175219820Sjeff void visitVAArgInst(VAArgInst &I); 176219820Sjeff void visitInstruction(Instruction &I) { 177219820Sjeff errs() << I; 178219820Sjeff llvm_unreachable("Instruction not interpretable yet!"); 179219820Sjeff } 180219820Sjeff 181219820Sjeff GenericValue callExternalFunction(Function *F, 182219820Sjeff const std::vector<GenericValue> &ArgVals); 183219820Sjeff void exitCalled(GenericValue GV); 184219820Sjeff 185219820Sjeff void addAtExitHandler(Function *F) { 186219820Sjeff AtExitHandlers.push_back(F); 187219820Sjeff } 188219820Sjeff 189219820Sjeff GenericValue *getFirstVarArg () { 190219820Sjeff return &(ECStack.back ().VarArgs[0]); 191219820Sjeff } 192219820Sjeff 193219820Sjeff //FIXME: private: 194219820Sjeffpublic: 195219820Sjeff GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I, 196219820Sjeff gep_type_iterator E, ExecutionContext &SF); 197219820Sjeff 198219820Sjeffprivate: // Helper functions 199219820Sjeff // SwitchToNewBasicBlock - Start execution in a new basic block and run any 200219820Sjeff // PHI nodes in the top of the block. This is used for intraprocedural 201219820Sjeff // control flow. 202219820Sjeff // 203219820Sjeff void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF); 204219820Sjeff 205219820Sjeff void *getPointerToFunction(Function *F) { return (void*)F; } 206219820Sjeff void *getPointerToBasicBlock(BasicBlock *BB) { return (void*)BB; } 207219820Sjeff 208219820Sjeff void initializeExecutionEngine() { } 209219820Sjeff void initializeExternalFunctions(); 210219820Sjeff GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF); 211219820Sjeff GenericValue getOperandValue(Value *V, ExecutionContext &SF); 212219820Sjeff GenericValue executeTruncInst(Value *SrcVal, const Type *DstTy, 213219820Sjeff ExecutionContext &SF); 214219820Sjeff GenericValue executeSExtInst(Value *SrcVal, const Type *DstTy, 215219820Sjeff ExecutionContext &SF); 216219820Sjeff GenericValue executeZExtInst(Value *SrcVal, const Type *DstTy, 217219820Sjeff ExecutionContext &SF); 218219820Sjeff GenericValue executeFPTruncInst(Value *SrcVal, const Type *DstTy, 219219820Sjeff ExecutionContext &SF); 220219820Sjeff GenericValue executeFPExtInst(Value *SrcVal, const Type *DstTy, 221219820Sjeff ExecutionContext &SF); 222219820Sjeff GenericValue executeFPToUIInst(Value *SrcVal, const Type *DstTy, 223219820Sjeff ExecutionContext &SF); 224219820Sjeff GenericValue executeFPToSIInst(Value *SrcVal, const Type *DstTy, 225219820Sjeff ExecutionContext &SF); 226219820Sjeff GenericValue executeUIToFPInst(Value *SrcVal, const Type *DstTy, 227219820Sjeff ExecutionContext &SF); 228219820Sjeff GenericValue executeSIToFPInst(Value *SrcVal, const Type *DstTy, 229219820Sjeff ExecutionContext &SF); 230219820Sjeff GenericValue executePtrToIntInst(Value *SrcVal, const Type *DstTy, 231219820Sjeff ExecutionContext &SF); 232219820Sjeff GenericValue executeIntToPtrInst(Value *SrcVal, const Type *DstTy, 233219820Sjeff ExecutionContext &SF); 234219820Sjeff GenericValue executeBitCastInst(Value *SrcVal, const Type *DstTy, 235219820Sjeff ExecutionContext &SF); 236219820Sjeff GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal, 237219820Sjeff const Type *Ty, ExecutionContext &SF); 238219820Sjeff void popStackAndReturnValueToCaller(const Type *RetTy, GenericValue Result); 239219820Sjeff 240219820Sjeff}; 241219820Sjeff 242219820Sjeff} // End llvm namespace 243219820Sjeff 244219820Sjeff#endif 245219820Sjeff