Interpreter.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. std::vector<void *> Allocations;
  36. public:
  37. AllocaHolder() {}
  38. // Make this type move-only. Define explicit move special members for MSVC.
  39. AllocaHolder(AllocaHolder &&RHS) : Allocations(std::move(RHS.Allocations)) {}
  40. AllocaHolder &operator=(AllocaHolder &&RHS) {
  41. Allocations = std::move(RHS.Allocations);
  42. return *this;
  43. }
  44. ~AllocaHolder() {
  45. for (void *Allocation : Allocations)
  46. free(Allocation);
  47. }
  48. void add(void *Mem) { Allocations.push_back(Mem); }
  49. };
  50. typedef std::vector<GenericValue> ValuePlaneTy;
  51. // ExecutionContext struct - This struct represents one stack frame currently
  52. // executing.
  53. //
  54. struct ExecutionContext {
  55. Function *CurFunction;// The currently executing function
  56. BasicBlock *CurBB; // The currently executing BB
  57. BasicBlock::iterator CurInst; // The next instruction to execute
  58. CallSite Caller; // Holds the call that called subframes.
  59. // NULL if main func or debugger invoked fn
  60. std::map<Value *, GenericValue> Values; // LLVM values used in this invocation
  61. std::vector<GenericValue> VarArgs; // Values passed through an ellipsis
  62. AllocaHolder Allocas; // Track memory allocated by alloca
  63. ExecutionContext() : CurFunction(nullptr), CurBB(nullptr), CurInst(nullptr) {}
  64. ExecutionContext(ExecutionContext &&O)
  65. : CurFunction(O.CurFunction), CurBB(O.CurBB), CurInst(O.CurInst),
  66. Caller(O.Caller), Values(std::move(O.Values)),
  67. VarArgs(std::move(O.VarArgs)), Allocas(std::move(O.Allocas)) {}
  68. ExecutionContext &operator=(ExecutionContext &&O) {
  69. CurFunction = O.CurFunction;
  70. CurBB = O.CurBB;
  71. CurInst = O.CurInst;
  72. Caller = O.Caller;
  73. Values = std::move(O.Values);
  74. VarArgs = std::move(O.VarArgs);
  75. Allocas = std::move(O.Allocas);
  76. return *this;
  77. }
  78. };
  79. // Interpreter - This class represents the entirety of the interpreter.
  80. //
  81. class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
  82. GenericValue ExitValue; // The return value of the called function
  83. DataLayout TD;
  84. IntrinsicLowering *IL;
  85. // The runtime stack of executing code. The top of the stack is the current
  86. // function record.
  87. std::vector<ExecutionContext> ECStack;
  88. // AtExitHandlers - List of functions to call when the program exits,
  89. // registered with the atexit() library function.
  90. std::vector<Function*> AtExitHandlers;
  91. public:
  92. explicit Interpreter(std::unique_ptr<Module> M);
  93. ~Interpreter() override;
  94. /// runAtExitHandlers - Run any functions registered by the program's calls to
  95. /// atexit(3), which we intercept and store in AtExitHandlers.
  96. ///
  97. void runAtExitHandlers();
  98. static void Register() {
  99. InterpCtor = create;
  100. }
  101. /// Create an interpreter ExecutionEngine.
  102. ///
  103. static ExecutionEngine *create(std::unique_ptr<Module> M,
  104. std::string *ErrorStr = nullptr);
  105. /// run - Start execution with the specified function and arguments.
  106. ///
  107. GenericValue runFunction(Function *F,
  108. ArrayRef<GenericValue> ArgValues) override;
  109. void *getPointerToNamedFunction(StringRef Name,
  110. bool AbortOnFailure = true) override {
  111. // FIXME: not implemented.
  112. return nullptr;
  113. }
  114. // Methods used to execute code:
  115. // Place a call on the stack
  116. void callFunction(Function *F, ArrayRef<GenericValue> ArgVals);
  117. void run(); // Execute instructions until nothing left to do
  118. // Opcode Implementations
  119. void visitReturnInst(ReturnInst &I);
  120. void visitBranchInst(BranchInst &I);
  121. void visitSwitchInst(SwitchInst &I);
  122. void visitIndirectBrInst(IndirectBrInst &I);
  123. void visitBinaryOperator(BinaryOperator &I);
  124. void visitICmpInst(ICmpInst &I);
  125. void visitFCmpInst(FCmpInst &I);
  126. void visitAllocaInst(AllocaInst &I);
  127. void visitLoadInst(LoadInst &I);
  128. void visitStoreInst(StoreInst &I);
  129. void visitGetElementPtrInst(GetElementPtrInst &I);
  130. void visitPHINode(PHINode &PN) {
  131. llvm_unreachable("PHI nodes already handled!");
  132. }
  133. void visitTruncInst(TruncInst &I);
  134. void visitZExtInst(ZExtInst &I);
  135. void visitSExtInst(SExtInst &I);
  136. void visitFPTruncInst(FPTruncInst &I);
  137. void visitFPExtInst(FPExtInst &I);
  138. void visitUIToFPInst(UIToFPInst &I);
  139. void visitSIToFPInst(SIToFPInst &I);
  140. void visitFPToUIInst(FPToUIInst &I);
  141. void visitFPToSIInst(FPToSIInst &I);
  142. void visitPtrToIntInst(PtrToIntInst &I);
  143. void visitIntToPtrInst(IntToPtrInst &I);
  144. void visitBitCastInst(BitCastInst &I);
  145. void visitSelectInst(SelectInst &I);
  146. void visitCallSite(CallSite CS);
  147. void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
  148. void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
  149. void visitUnreachableInst(UnreachableInst &I);
  150. void visitShl(BinaryOperator &I);
  151. void visitLShr(BinaryOperator &I);
  152. void visitAShr(BinaryOperator &I);
  153. void visitVAArgInst(VAArgInst &I);
  154. void visitExtractElementInst(ExtractElementInst &I);
  155. void visitInsertElementInst(InsertElementInst &I);
  156. void visitShuffleVectorInst(ShuffleVectorInst &I);
  157. void visitExtractValueInst(ExtractValueInst &I);
  158. void visitInsertValueInst(InsertValueInst &I);
  159. void visitInstruction(Instruction &I) {
  160. errs() << I << "\n";
  161. llvm_unreachable("Instruction not interpretable yet!");
  162. }
  163. GenericValue callExternalFunction(Function *F,
  164. ArrayRef<GenericValue> ArgVals);
  165. void exitCalled(GenericValue GV);
  166. void addAtExitHandler(Function *F) {
  167. AtExitHandlers.push_back(F);
  168. }
  169. GenericValue *getFirstVarArg () {
  170. return &(ECStack.back ().VarArgs[0]);
  171. }
  172. private: // Helper functions
  173. GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
  174. gep_type_iterator E, ExecutionContext &SF);
  175. // SwitchToNewBasicBlock - Start execution in a new basic block and run any
  176. // PHI nodes in the top of the block. This is used for intraprocedural
  177. // control flow.
  178. //
  179. void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
  180. void *getPointerToFunction(Function *F) override { return (void*)F; }
  181. void initializeExecutionEngine() { }
  182. void initializeExternalFunctions();
  183. GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
  184. GenericValue getOperandValue(Value *V, ExecutionContext &SF);
  185. GenericValue executeTruncInst(Value *SrcVal, Type *DstTy,
  186. ExecutionContext &SF);
  187. GenericValue executeSExtInst(Value *SrcVal, Type *DstTy,
  188. ExecutionContext &SF);
  189. GenericValue executeZExtInst(Value *SrcVal, Type *DstTy,
  190. ExecutionContext &SF);
  191. GenericValue executeFPTruncInst(Value *SrcVal, Type *DstTy,
  192. ExecutionContext &SF);
  193. GenericValue executeFPExtInst(Value *SrcVal, Type *DstTy,
  194. ExecutionContext &SF);
  195. GenericValue executeFPToUIInst(Value *SrcVal, Type *DstTy,
  196. ExecutionContext &SF);
  197. GenericValue executeFPToSIInst(Value *SrcVal, Type *DstTy,
  198. ExecutionContext &SF);
  199. GenericValue executeUIToFPInst(Value *SrcVal, Type *DstTy,
  200. ExecutionContext &SF);
  201. GenericValue executeSIToFPInst(Value *SrcVal, Type *DstTy,
  202. ExecutionContext &SF);
  203. GenericValue executePtrToIntInst(Value *SrcVal, Type *DstTy,
  204. ExecutionContext &SF);
  205. GenericValue executeIntToPtrInst(Value *SrcVal, Type *DstTy,
  206. ExecutionContext &SF);
  207. GenericValue executeBitCastInst(Value *SrcVal, Type *DstTy,
  208. ExecutionContext &SF);
  209. GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal,
  210. Type *Ty, ExecutionContext &SF);
  211. void popStackAndReturnValueToCaller(Type *RetTy, GenericValue Result);
  212. };
  213. } // End llvm namespace
  214. #endif