LLVM API Documentation

Interpreter.h

Go to the documentation of this file.
00001 //===-- Interpreter.h ------------------------------------------*- C++ -*--===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source 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 <iostream>
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   Interpreter(Module *M, bool isLittleEndian, bool isLongPointer);
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);
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 visitAllocationInst(AllocationInst &I);
00141   void visitFreeInst(FreeInst &I);
00142   void visitLoadInst(LoadInst &I);
00143   void visitStoreInst(StoreInst &I);
00144   void visitGetElementPtrInst(GetElementPtrInst &I);
00145   void visitPHINode(PHINode &PN) { assert(0 && "PHI nodes already handled!"); }
00146   void visitCastInst(CastInst &I);
00147   void visitSelectInst(SelectInst &I);
00148 
00149 
00150   void visitCallSite(CallSite CS);
00151   void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
00152   void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
00153   void visitUnwindInst(UnwindInst &I);
00154   void visitUnreachableInst(UnreachableInst &I);
00155 
00156   void visitShl(ShiftInst &I);
00157   void visitShr(ShiftInst &I);
00158   void visitVAArgInst(VAArgInst &I);
00159   void visitInstruction(Instruction &I) {
00160     std::cerr << I;
00161     assert(0 && "Instruction not interpretable yet!");
00162   }
00163 
00164   GenericValue callExternalFunction(Function *F,
00165                                     const std::vector<GenericValue> &ArgVals);
00166   void exitCalled(GenericValue GV);
00167 
00168   void addAtExitHandler(Function *F) {
00169     AtExitHandlers.push_back(F);
00170   }
00171 
00172   GenericValue *getFirstVarArg () {
00173     return &(ECStack.back ().VarArgs[0]);
00174   }
00175 
00176   //FIXME: private:
00177 public:
00178   GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
00179                                    gep_type_iterator E, ExecutionContext &SF);
00180 
00181 private:  // Helper functions
00182   // SwitchToNewBasicBlock - Start execution in a new basic block and run any
00183   // PHI nodes in the top of the block.  This is used for intraprocedural
00184   // control flow.
00185   //
00186   void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
00187 
00188   void *getPointerToFunction(Function *F) { return (void*)F; }
00189 
00190   void initializeExecutionEngine();
00191   void initializeExternalFunctions();
00192   GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
00193   GenericValue getOperandValue(Value *V, ExecutionContext &SF);
00194   GenericValue executeCastOperation(Value *SrcVal, const Type *Ty,
00195                                     ExecutionContext &SF);
00196   void popStackAndReturnValueToCaller(const Type *RetTy, GenericValue Result);
00197 };
00198 
00199 } // End llvm namespace
00200 
00201 #endif