LLVM API Documentation

lib/ExecutionEngine/Interpreter/Interpreter.h
Go to the documentation of this file.
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 LLVM_LIB_EXECUTIONENGINE_INTERPRETER_INTERPRETER_H
00015 #define LLVM_LIB_EXECUTIONENGINE_INTERPRETER_INTERPRETER_H
00016 
00017 #include "llvm/ExecutionEngine/ExecutionEngine.h"
00018 #include "llvm/ExecutionEngine/GenericValue.h"
00019 #include "llvm/IR/CallSite.h"
00020 #include "llvm/IR/DataLayout.h"
00021 #include "llvm/IR/Function.h"
00022 #include "llvm/IR/InstVisitor.h"
00023 #include "llvm/Support/DataTypes.h"
00024 #include "llvm/Support/ErrorHandling.h"
00025 #include "llvm/Support/raw_ostream.h"
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   std::vector<void *> Allocations;
00041 
00042 public:
00043   AllocaHolder() {}
00044 
00045   // Make this type move-only. Define explicit move special members for MSVC.
00046   AllocaHolder(AllocaHolder &&RHS) : Allocations(std::move(RHS.Allocations)) {}
00047   AllocaHolder &operator=(AllocaHolder &&RHS) {
00048     Allocations = std::move(RHS.Allocations);
00049     return *this;
00050   }
00051 
00052   ~AllocaHolder() {
00053     for (void *Allocation : Allocations)
00054       free(Allocation);
00055   }
00056 
00057   void add(void *Mem) { Allocations.push_back(Mem); }
00058 };
00059 
00060 typedef std::vector<GenericValue> ValuePlaneTy;
00061 
00062 // ExecutionContext struct - This struct represents one stack frame currently
00063 // executing.
00064 //
00065 struct ExecutionContext {
00066   Function             *CurFunction;// The currently executing function
00067   BasicBlock           *CurBB;      // The currently executing BB
00068   BasicBlock::iterator  CurInst;    // The next instruction to execute
00069   CallSite             Caller;     // Holds the call that called subframes.
00070                                    // NULL if main func or debugger invoked fn
00071   std::map<Value *, GenericValue> Values; // LLVM values used in this invocation
00072   std::vector<GenericValue>  VarArgs; // Values passed through an ellipsis
00073   AllocaHolder Allocas;            // Track memory allocated by alloca
00074 
00075   ExecutionContext() : CurFunction(nullptr), CurBB(nullptr), CurInst(nullptr) {}
00076 
00077   ExecutionContext(ExecutionContext &&O)
00078       : CurFunction(O.CurFunction), CurBB(O.CurBB), CurInst(O.CurInst),
00079         Caller(O.Caller), Values(std::move(O.Values)),
00080         VarArgs(std::move(O.VarArgs)), Allocas(std::move(O.Allocas)) {}
00081 
00082   ExecutionContext &operator=(ExecutionContext &&O) {
00083     CurFunction = O.CurFunction;
00084     CurBB = O.CurBB;
00085     CurInst = O.CurInst;
00086     Caller = O.Caller;
00087     Values = std::move(O.Values);
00088     VarArgs = std::move(O.VarArgs);
00089     Allocas = std::move(O.Allocas);
00090     return *this;
00091   }
00092 };
00093 
00094 // Interpreter - This class represents the entirety of the interpreter.
00095 //
00096 class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
00097   GenericValue ExitValue;          // The return value of the called function
00098   DataLayout TD;
00099   IntrinsicLowering *IL;
00100 
00101   // The runtime stack of executing code.  The top of the stack is the current
00102   // function record.
00103   std::vector<ExecutionContext> ECStack;
00104 
00105   // AtExitHandlers - List of functions to call when the program exits,
00106   // registered with the atexit() library function.
00107   std::vector<Function*> AtExitHandlers;
00108 
00109 public:
00110   explicit Interpreter(std::unique_ptr<Module> M);
00111   ~Interpreter();
00112 
00113   /// runAtExitHandlers - Run any functions registered by the program's calls to
00114   /// atexit(3), which we intercept and store in AtExitHandlers.
00115   ///
00116   void runAtExitHandlers();
00117 
00118   static void Register() {
00119     InterpCtor = create;
00120   }
00121 
00122   /// Create an interpreter ExecutionEngine.
00123   ///
00124   static ExecutionEngine *create(std::unique_ptr<Module> M,
00125                                  std::string *ErrorStr = nullptr);
00126 
00127   /// run - Start execution with the specified function and arguments.
00128   ///
00129   GenericValue runFunction(Function *F,
00130                            const std::vector<GenericValue> &ArgValues) override;
00131 
00132   void *getPointerToNamedFunction(StringRef Name,
00133                                   bool AbortOnFailure = true) override {
00134     // FIXME: not implemented.
00135     return nullptr;
00136   }
00137 
00138   // Methods used to execute code:
00139   // Place a call on the stack
00140   void callFunction(Function *F, const std::vector<GenericValue> &ArgVals);
00141   void run();                // Execute instructions until nothing left to do
00142 
00143   // Opcode Implementations
00144   void visitReturnInst(ReturnInst &I);
00145   void visitBranchInst(BranchInst &I);
00146   void visitSwitchInst(SwitchInst &I);
00147   void visitIndirectBrInst(IndirectBrInst &I);
00148 
00149   void visitBinaryOperator(BinaryOperator &I);
00150   void visitICmpInst(ICmpInst &I);
00151   void visitFCmpInst(FCmpInst &I);
00152   void visitAllocaInst(AllocaInst &I);
00153   void visitLoadInst(LoadInst &I);
00154   void visitStoreInst(StoreInst &I);
00155   void visitGetElementPtrInst(GetElementPtrInst &I);
00156   void visitPHINode(PHINode &PN) { 
00157     llvm_unreachable("PHI nodes already handled!"); 
00158   }
00159   void visitTruncInst(TruncInst &I);
00160   void visitZExtInst(ZExtInst &I);
00161   void visitSExtInst(SExtInst &I);
00162   void visitFPTruncInst(FPTruncInst &I);
00163   void visitFPExtInst(FPExtInst &I);
00164   void visitUIToFPInst(UIToFPInst &I);
00165   void visitSIToFPInst(SIToFPInst &I);
00166   void visitFPToUIInst(FPToUIInst &I);
00167   void visitFPToSIInst(FPToSIInst &I);
00168   void visitPtrToIntInst(PtrToIntInst &I);
00169   void visitIntToPtrInst(IntToPtrInst &I);
00170   void visitBitCastInst(BitCastInst &I);
00171   void visitSelectInst(SelectInst &I);
00172 
00173 
00174   void visitCallSite(CallSite CS);
00175   void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
00176   void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
00177   void visitUnreachableInst(UnreachableInst &I);
00178 
00179   void visitShl(BinaryOperator &I);
00180   void visitLShr(BinaryOperator &I);
00181   void visitAShr(BinaryOperator &I);
00182 
00183   void visitVAArgInst(VAArgInst &I);
00184   void visitExtractElementInst(ExtractElementInst &I);
00185   void visitInsertElementInst(InsertElementInst &I);
00186   void visitShuffleVectorInst(ShuffleVectorInst &I);
00187 
00188   void visitExtractValueInst(ExtractValueInst &I);
00189   void visitInsertValueInst(InsertValueInst &I);
00190 
00191   void visitInstruction(Instruction &I) {
00192     errs() << I << "\n";
00193     llvm_unreachable("Instruction not interpretable yet!");
00194   }
00195 
00196   GenericValue callExternalFunction(Function *F,
00197                                     const std::vector<GenericValue> &ArgVals);
00198   void exitCalled(GenericValue GV);
00199 
00200   void addAtExitHandler(Function *F) {
00201     AtExitHandlers.push_back(F);
00202   }
00203 
00204   GenericValue *getFirstVarArg () {
00205     return &(ECStack.back ().VarArgs[0]);
00206   }
00207 
00208 private:  // Helper functions
00209   GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
00210                                    gep_type_iterator E, ExecutionContext &SF);
00211 
00212   // SwitchToNewBasicBlock - Start execution in a new basic block and run any
00213   // PHI nodes in the top of the block.  This is used for intraprocedural
00214   // control flow.
00215   //
00216   void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
00217 
00218   void *getPointerToFunction(Function *F) override { return (void*)F; }
00219 
00220   void initializeExecutionEngine() { }
00221   void initializeExternalFunctions();
00222   GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
00223   GenericValue getOperandValue(Value *V, ExecutionContext &SF);
00224   GenericValue executeTruncInst(Value *SrcVal, Type *DstTy,
00225                                 ExecutionContext &SF);
00226   GenericValue executeSExtInst(Value *SrcVal, Type *DstTy,
00227                                ExecutionContext &SF);
00228   GenericValue executeZExtInst(Value *SrcVal, Type *DstTy,
00229                                ExecutionContext &SF);
00230   GenericValue executeFPTruncInst(Value *SrcVal, Type *DstTy,
00231                                   ExecutionContext &SF);
00232   GenericValue executeFPExtInst(Value *SrcVal, Type *DstTy,
00233                                 ExecutionContext &SF);
00234   GenericValue executeFPToUIInst(Value *SrcVal, Type *DstTy,
00235                                  ExecutionContext &SF);
00236   GenericValue executeFPToSIInst(Value *SrcVal, Type *DstTy,
00237                                  ExecutionContext &SF);
00238   GenericValue executeUIToFPInst(Value *SrcVal, Type *DstTy,
00239                                  ExecutionContext &SF);
00240   GenericValue executeSIToFPInst(Value *SrcVal, Type *DstTy,
00241                                  ExecutionContext &SF);
00242   GenericValue executePtrToIntInst(Value *SrcVal, Type *DstTy,
00243                                    ExecutionContext &SF);
00244   GenericValue executeIntToPtrInst(Value *SrcVal, Type *DstTy,
00245                                    ExecutionContext &SF);
00246   GenericValue executeBitCastInst(Value *SrcVal, Type *DstTy,
00247                                   ExecutionContext &SF);
00248   GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal, 
00249                                     Type *Ty, ExecutionContext &SF);
00250   void popStackAndReturnValueToCaller(Type *RetTy, GenericValue Result);
00251 
00252 };
00253 
00254 } // End llvm namespace
00255 
00256 #endif