Interpreter.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. //===-- Interpreter.h ------------------------------------------*- C++ -*--===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This header file defines the interpreter structure
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_LIB_EXECUTIONENGINE_INTERPRETER_INTERPRETER_H
  14. #define LLVM_LIB_EXECUTIONENGINE_INTERPRETER_INTERPRETER_H
  15. #include "llvm/ExecutionEngine/ExecutionEngine.h"
  16. #include "llvm/ExecutionEngine/GenericValue.h"
  17. #include "llvm/IR/CallSite.h"
  18. #include "llvm/IR/DataLayout.h"
  19. #include "llvm/IR/Function.h"
  20. #include "llvm/IR/InstVisitor.h"
  21. #include "llvm/Support/DataTypes.h"
  22. #include "llvm/Support/ErrorHandling.h"
  23. #include "llvm/Support/raw_ostream.h"
  24. namespace llvm {
  25. class IntrinsicLowering;
  26. struct FunctionInfo;
  27. template<typename T> class generic_gep_type_iterator;
  28. class ConstantExpr;
  29. typedef generic_gep_type_iterator<User::const_op_iterator> gep_type_iterator;
  30. // AllocaHolder - Object to track all of the blocks of memory allocated by
  31. // alloca. When the function returns, this object is popped off the execution
  32. // stack, which causes the dtor to be run, which frees all the alloca'd memory.
  33. //
  34. class AllocaHolder {
  35. friend class AllocaHolderHandle;
  36. std::vector<void*> Allocations;
  37. unsigned RefCnt;
  38. public:
  39. AllocaHolder() : RefCnt(0) {}
  40. void add(void *mem) { Allocations.push_back(mem); }
  41. ~AllocaHolder() {
  42. for (unsigned i = 0; i < Allocations.size(); ++i)
  43. free(Allocations[i]);
  44. }
  45. };
  46. // AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into
  47. // a vector...
  48. //
  49. class AllocaHolderHandle {
  50. AllocaHolder *H;
  51. public:
  52. AllocaHolderHandle() : H(new AllocaHolder()) { H->RefCnt++; }
  53. AllocaHolderHandle(const AllocaHolderHandle &AH) : H(AH.H) { H->RefCnt++; }
  54. ~AllocaHolderHandle() { if (--H->RefCnt == 0) delete H; }
  55. void add(void *mem) { H->add(mem); }
  56. };
  57. typedef std::vector<GenericValue> ValuePlaneTy;
  58. // ExecutionContext struct - This struct represents one stack frame currently
  59. // executing.
  60. //
  61. struct ExecutionContext {
  62. Function *CurFunction;// The currently executing function
  63. BasicBlock *CurBB; // The currently executing BB
  64. BasicBlock::iterator CurInst; // The next instruction to execute
  65. std::map<Value *, GenericValue> Values; // LLVM values used in this invocation
  66. std::vector<GenericValue> VarArgs; // Values passed through an ellipsis
  67. CallSite Caller; // Holds the call that called subframes.
  68. // NULL if main func or debugger invoked fn
  69. AllocaHolderHandle Allocas; // Track memory allocated by alloca
  70. };
  71. // Interpreter - This class represents the entirety of the interpreter.
  72. //
  73. class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
  74. GenericValue ExitValue; // The return value of the called function
  75. DataLayout TD;
  76. IntrinsicLowering *IL;
  77. // The runtime stack of executing code. The top of the stack is the current
  78. // function record.
  79. std::vector<ExecutionContext> ECStack;
  80. // AtExitHandlers - List of functions to call when the program exits,
  81. // registered with the atexit() library function.
  82. std::vector<Function*> AtExitHandlers;
  83. public:
  84. explicit Interpreter(std::unique_ptr<Module> M);
  85. ~Interpreter();
  86. /// runAtExitHandlers - Run any functions registered by the program's calls to
  87. /// atexit(3), which we intercept and store in AtExitHandlers.
  88. ///
  89. void runAtExitHandlers();
  90. static void Register() {
  91. InterpCtor = create;
  92. }
  93. /// Create an interpreter ExecutionEngine.
  94. ///
  95. static ExecutionEngine *create(std::unique_ptr<Module> M,
  96. std::string *ErrorStr = nullptr);
  97. /// run - Start execution with the specified function and arguments.
  98. ///
  99. GenericValue runFunction(Function *F,
  100. const std::vector<GenericValue> &ArgValues) override;
  101. void *getPointerToNamedFunction(const std::string &Name,
  102. bool AbortOnFailure = true) override {
  103. // FIXME: not implemented.
  104. return nullptr;
  105. }
  106. /// recompileAndRelinkFunction - For the interpreter, functions are always
  107. /// up-to-date.
  108. ///
  109. void *recompileAndRelinkFunction(Function *F) override {
  110. return getPointerToFunction(F);
  111. }
  112. /// freeMachineCodeForFunction - The interpreter does not generate any code.
  113. ///
  114. void freeMachineCodeForFunction(Function *F) override { }
  115. // Methods used to execute code:
  116. // Place a call on the stack
  117. void callFunction(Function *F, const std::vector<GenericValue> &ArgVals);
  118. void run(); // Execute instructions until nothing left to do
  119. // Opcode Implementations
  120. void visitReturnInst(ReturnInst &I);
  121. void visitBranchInst(BranchInst &I);
  122. void visitSwitchInst(SwitchInst &I);
  123. void visitIndirectBrInst(IndirectBrInst &I);
  124. void visitBinaryOperator(BinaryOperator &I);
  125. void visitICmpInst(ICmpInst &I);
  126. void visitFCmpInst(FCmpInst &I);
  127. void visitAllocaInst(AllocaInst &I);
  128. void visitLoadInst(LoadInst &I);
  129. void visitStoreInst(StoreInst &I);
  130. void visitGetElementPtrInst(GetElementPtrInst &I);
  131. void visitPHINode(PHINode &PN) {
  132. llvm_unreachable("PHI nodes already handled!");
  133. }
  134. void visitTruncInst(TruncInst &I);
  135. void visitZExtInst(ZExtInst &I);
  136. void visitSExtInst(SExtInst &I);
  137. void visitFPTruncInst(FPTruncInst &I);
  138. void visitFPExtInst(FPExtInst &I);
  139. void visitUIToFPInst(UIToFPInst &I);
  140. void visitSIToFPInst(SIToFPInst &I);
  141. void visitFPToUIInst(FPToUIInst &I);
  142. void visitFPToSIInst(FPToSIInst &I);
  143. void visitPtrToIntInst(PtrToIntInst &I);
  144. void visitIntToPtrInst(IntToPtrInst &I);
  145. void visitBitCastInst(BitCastInst &I);
  146. void visitSelectInst(SelectInst &I);
  147. void visitCallSite(CallSite CS);
  148. void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
  149. void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
  150. void visitUnreachableInst(UnreachableInst &I);
  151. void visitShl(BinaryOperator &I);
  152. void visitLShr(BinaryOperator &I);
  153. void visitAShr(BinaryOperator &I);
  154. void visitVAArgInst(VAArgInst &I);
  155. void visitExtractElementInst(ExtractElementInst &I);
  156. void visitInsertElementInst(InsertElementInst &I);
  157. void visitShuffleVectorInst(ShuffleVectorInst &I);
  158. void visitExtractValueInst(ExtractValueInst &I);
  159. void visitInsertValueInst(InsertValueInst &I);
  160. void visitInstruction(Instruction &I) {
  161. errs() << I << "\n";
  162. llvm_unreachable("Instruction not interpretable yet!");
  163. }
  164. GenericValue callExternalFunction(Function *F,
  165. const std::vector<GenericValue> &ArgVals);
  166. void exitCalled(GenericValue GV);
  167. void addAtExitHandler(Function *F) {
  168. AtExitHandlers.push_back(F);
  169. }
  170. GenericValue *getFirstVarArg () {
  171. return &(ECStack.back ().VarArgs[0]);
  172. }
  173. private: // Helper functions
  174. GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
  175. gep_type_iterator E, ExecutionContext &SF);
  176. // SwitchToNewBasicBlock - Start execution in a new basic block and run any
  177. // PHI nodes in the top of the block. This is used for intraprocedural
  178. // control flow.
  179. //
  180. void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
  181. void *getPointerToFunction(Function *F) override { return (void*)F; }
  182. void *getPointerToBasicBlock(BasicBlock *BB) override { return (void*)BB; }
  183. void initializeExecutionEngine() { }
  184. void initializeExternalFunctions();
  185. GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
  186. GenericValue getOperandValue(Value *V, ExecutionContext &SF);
  187. GenericValue executeTruncInst(Value *SrcVal, Type *DstTy,
  188. ExecutionContext &SF);
  189. GenericValue executeSExtInst(Value *SrcVal, Type *DstTy,
  190. ExecutionContext &SF);
  191. GenericValue executeZExtInst(Value *SrcVal, Type *DstTy,
  192. ExecutionContext &SF);
  193. GenericValue executeFPTruncInst(Value *SrcVal, Type *DstTy,
  194. ExecutionContext &SF);
  195. GenericValue executeFPExtInst(Value *SrcVal, Type *DstTy,
  196. ExecutionContext &SF);
  197. GenericValue executeFPToUIInst(Value *SrcVal, Type *DstTy,
  198. ExecutionContext &SF);
  199. GenericValue executeFPToSIInst(Value *SrcVal, Type *DstTy,
  200. ExecutionContext &SF);
  201. GenericValue executeUIToFPInst(Value *SrcVal, Type *DstTy,
  202. ExecutionContext &SF);
  203. GenericValue executeSIToFPInst(Value *SrcVal, Type *DstTy,
  204. ExecutionContext &SF);
  205. GenericValue executePtrToIntInst(Value *SrcVal, Type *DstTy,
  206. ExecutionContext &SF);
  207. GenericValue executeIntToPtrInst(Value *SrcVal, Type *DstTy,
  208. ExecutionContext &SF);
  209. GenericValue executeBitCastInst(Value *SrcVal, Type *DstTy,
  210. ExecutionContext &SF);
  211. GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal,
  212. Type *Ty, ExecutionContext &SF);
  213. void popStackAndReturnValueToCaller(Type *RetTy, GenericValue Result);
  214. };
  215. } // End llvm namespace
  216. #endif