LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

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 struct FunctionInfo;
00029 template<typename T> class generic_gep_type_iterator;
00030 class ConstantExpr;
00031 typedef generic_gep_type_iterator<User::const_op_iterator> gep_type_iterator;
00032 
00033 
00034 // AllocaHolder - Object to track all of the blocks of memory allocated by
00035 // alloca.  When the function returns, this object is popped off the execution
00036 // stack, which causes the dtor to be run, which frees all the alloca'd memory.
00037 //
00038 class AllocaHolder {
00039   friend class AllocaHolderHandle;
00040   std::vector<void*> Allocations;
00041   unsigned RefCnt;
00042 public:
00043   AllocaHolder() : RefCnt(0) {}
00044   void add(void *mem) { Allocations.push_back(mem); }
00045   ~AllocaHolder() {
00046     for (unsigned i = 0; i < Allocations.size(); ++i)
00047       free(Allocations[i]);
00048   }
00049 };
00050 
00051 // AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into
00052 // a vector...
00053 //
00054 class AllocaHolderHandle {
00055   AllocaHolder *H;
00056 public:
00057   AllocaHolderHandle() : H(new AllocaHolder()) { H->RefCnt++; }
00058   AllocaHolderHandle(const AllocaHolderHandle &AH) : H(AH.H) { H->RefCnt++; }
00059   ~AllocaHolderHandle() { if (--H->RefCnt == 0) delete H; }
00060 
00061   void add(void *mem) { H->add(mem); }
00062 };
00063 
00064 typedef std::vector<GenericValue> ValuePlaneTy;
00065 
00066 // ExecutionContext struct - This struct represents one stack frame currently
00067 // executing.
00068 //
00069 struct ExecutionContext {
00070   Function             *CurFunction;// The currently executing function
00071   BasicBlock           *CurBB;      // The currently executing BB
00072   BasicBlock::iterator  CurInst;    // The next instruction to execute
00073   std::map<Value *, GenericValue> Values; // LLVM values used in this invocation
00074   std::vector<GenericValue>  VarArgs; // Values passed through an ellipsis
00075   CallSite             Caller;     // Holds the call that called subframes.
00076                                    // NULL if main func or debugger invoked fn
00077   AllocaHolderHandle    Allocas;    // Track memory allocated by alloca
00078 };
00079 
00080 // Interpreter - This class represents the entirety of the interpreter.
00081 //
00082 class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
00083   int ExitCode;                // The exit code to be returned by the lli util
00084   TargetData TD;
00085   IntrinsicLowering *IL;
00086 
00087   // The runtime stack of executing code.  The top of the stack is the current
00088   // function record.
00089   std::vector<ExecutionContext> ECStack;
00090 
00091   // AtExitHandlers - List of functions to call when the program exits,
00092   // registered with the atexit() library function.
00093   std::vector<Function*> AtExitHandlers;
00094 
00095 public:
00096   Interpreter(Module *M, bool isLittleEndian, bool isLongPointer,
00097               IntrinsicLowering *IL);
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   /// create - Create an interpreter ExecutionEngine. This can never fail.  The
00106   /// specified IntrinsicLowering implementation will be deleted when the
00107   /// Interpreter execution engine is destroyed.
00108   ///
00109   static ExecutionEngine *create(Module *M, IntrinsicLowering *IL);
00110 
00111   /// run - Start execution with the specified function and arguments.
00112   ///
00113   virtual GenericValue runFunction(Function *F,
00114                                    const std::vector<GenericValue> &ArgValues);
00115 
00116   /// recompileAndRelinkFunction - For the interpreter, functions are always
00117   /// up-to-date.
00118   ///
00119   virtual void *recompileAndRelinkFunction(Function *F) {
00120     return getPointerToFunction(F);
00121   }
00122 
00123   /// freeMachineCodeForFunction - The interpreter does not generate any code.
00124   ///
00125   void freeMachineCodeForFunction(Function *F) { }
00126   
00127   // Methods used to execute code:
00128   // Place a call on the stack
00129   void callFunction(Function *F, const std::vector<GenericValue> &ArgVals);
00130   void run();                // Execute instructions until nothing left to do
00131 
00132   // Opcode Implementations
00133   void visitReturnInst(ReturnInst &I);
00134   void visitBranchInst(BranchInst &I);
00135   void visitSwitchInst(SwitchInst &I);
00136 
00137   void visitBinaryOperator(BinaryOperator &I);
00138   void visitAllocationInst(AllocationInst &I);
00139   void visitFreeInst(FreeInst &I);
00140   void visitLoadInst(LoadInst &I);
00141   void visitStoreInst(StoreInst &I);
00142   void visitGetElementPtrInst(GetElementPtrInst &I);
00143   void visitPHINode(PHINode &PN) { assert(0 && "PHI nodes already handled!"); }
00144   void visitCastInst(CastInst &I);
00145   void visitSelectInst(SelectInst &I);
00146 
00147 
00148   void visitCallSite(CallSite CS);
00149   void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
00150   void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
00151   void visitUnwindInst(UnwindInst &I);
00152   void visitUnreachableInst(UnreachableInst &I);
00153 
00154   void visitShl(ShiftInst &I);
00155   void visitShr(ShiftInst &I);
00156   void visitVANextInst(VANextInst &I);
00157   void visitVAArgInst(VAArgInst &I);
00158   void visitInstruction(Instruction &I) {
00159     std::cerr << I;
00160     assert(0 && "Instruction not interpretable yet!");
00161   }
00162 
00163   GenericValue callExternalFunction(Function *F, 
00164                                     const std::vector<GenericValue> &ArgVals);
00165   void exitCalled(GenericValue GV);
00166 
00167   void addAtExitHandler(Function *F) {
00168     AtExitHandlers.push_back(F);
00169   }
00170 
00171   GenericValue *getFirstVarArg () {
00172     return &(ECStack.back ().VarArgs[0]);
00173   }
00174 
00175   //FIXME: private:
00176 public:
00177   GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
00178            gep_type_iterator E, ExecutionContext &SF);
00179 
00180 private:  // Helper functions
00181   // SwitchToNewBasicBlock - Start execution in a new basic block and run any
00182   // PHI nodes in the top of the block.  This is used for intraprocedural
00183   // control flow.
00184   // 
00185   void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
00186 
00187   void *getPointerToFunction(Function *F) { return (void*)F; }
00188 
00189   void initializeExecutionEngine();
00190   void initializeExternalFunctions();
00191   GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
00192   GenericValue getOperandValue(Value *V, ExecutionContext &SF);
00193   GenericValue executeCastOperation(Value *SrcVal, const Type *Ty,
00194             ExecutionContext &SF);
00195   void popStackAndReturnValueToCaller(const Type *RetTy, GenericValue Result);
00196 };
00197 
00198 } // End llvm namespace
00199 
00200 #endif