Interpreter.h 9.2 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. 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. IntrinsicLowering *IL;
  84. // The runtime stack of executing code. The top of the stack is the current
  85. // function record.
  86. std::vector<ExecutionContext> ECStack;
  87. // AtExitHandlers - List of functions to call when the program exits,
  88. // registered with the atexit() library function.
  89. std::vector<Function*> AtExitHandlers;
  90. public:
  91. explicit Interpreter(std::unique_ptr<Module> M);
  92. ~Interpreter() override;
  93. /// runAtExitHandlers - Run any functions registered by the program's calls to
  94. /// atexit(3), which we intercept and store in AtExitHandlers.
  95. ///
  96. void runAtExitHandlers();
  97. static void Register() {
  98. InterpCtor = create;
  99. }
  100. /// Create an interpreter ExecutionEngine.
  101. ///
  102. static ExecutionEngine *create(std::unique_ptr<Module> M,
  103. std::string *ErrorStr = nullptr);
  104. /// run - Start execution with the specified function and arguments.
  105. ///
  106. GenericValue runFunction(Function *F,
  107. ArrayRef<GenericValue> ArgValues) override;
  108. void *getPointerToNamedFunction(StringRef Name,
  109. bool AbortOnFailure = true) override {
  110. // FIXME: not implemented.
  111. return nullptr;
  112. }
  113. // Methods used to execute code:
  114. // Place a call on the stack
  115. void callFunction(Function *F, ArrayRef<GenericValue> ArgVals);
  116. void run(); // Execute instructions until nothing left to do
  117. // Opcode Implementations
  118. void visitReturnInst(ReturnInst &I);
  119. void visitBranchInst(BranchInst &I);
  120. void visitSwitchInst(SwitchInst &I);
  121. void visitIndirectBrInst(IndirectBrInst &I);
  122. void visitBinaryOperator(BinaryOperator &I);
  123. void visitICmpInst(ICmpInst &I);
  124. void visitFCmpInst(FCmpInst &I);
  125. void visitAllocaInst(AllocaInst &I);
  126. void visitLoadInst(LoadInst &I);
  127. void visitStoreInst(StoreInst &I);
  128. void visitGetElementPtrInst(GetElementPtrInst &I);
  129. void visitPHINode(PHINode &PN) {
  130. llvm_unreachable("PHI nodes already handled!");
  131. }
  132. void visitTruncInst(TruncInst &I);
  133. void visitZExtInst(ZExtInst &I);
  134. void visitSExtInst(SExtInst &I);
  135. void visitFPTruncInst(FPTruncInst &I);
  136. void visitFPExtInst(FPExtInst &I);
  137. void visitUIToFPInst(UIToFPInst &I);
  138. void visitSIToFPInst(SIToFPInst &I);
  139. void visitFPToUIInst(FPToUIInst &I);
  140. void visitFPToSIInst(FPToSIInst &I);
  141. void visitPtrToIntInst(PtrToIntInst &I);
  142. void visitIntToPtrInst(IntToPtrInst &I);
  143. void visitBitCastInst(BitCastInst &I);
  144. void visitSelectInst(SelectInst &I);
  145. void visitCallSite(CallSite CS);
  146. void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
  147. void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
  148. void visitUnreachableInst(UnreachableInst &I);
  149. void visitShl(BinaryOperator &I);
  150. void visitLShr(BinaryOperator &I);
  151. void visitAShr(BinaryOperator &I);
  152. void visitVAArgInst(VAArgInst &I);
  153. void visitExtractElementInst(ExtractElementInst &I);
  154. void visitInsertElementInst(InsertElementInst &I);
  155. void visitShuffleVectorInst(ShuffleVectorInst &I);
  156. void visitExtractValueInst(ExtractValueInst &I);
  157. void visitInsertValueInst(InsertValueInst &I);
  158. void visitInstruction(Instruction &I) {
  159. errs() << I << "\n";
  160. llvm_unreachable("Instruction not interpretable yet!");
  161. }
  162. GenericValue callExternalFunction(Function *F,
  163. ArrayRef<GenericValue> ArgVals);
  164. void exitCalled(GenericValue GV);
  165. void addAtExitHandler(Function *F) {
  166. AtExitHandlers.push_back(F);
  167. }
  168. GenericValue *getFirstVarArg () {
  169. return &(ECStack.back ().VarArgs[0]);
  170. }
  171. private: // Helper functions
  172. GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
  173. gep_type_iterator E, ExecutionContext &SF);
  174. // SwitchToNewBasicBlock - Start execution in a new basic block and run any
  175. // PHI nodes in the top of the block. This is used for intraprocedural
  176. // control flow.
  177. //
  178. void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
  179. void *getPointerToFunction(Function *F) override { return (void*)F; }
  180. void initializeExecutionEngine() { }
  181. void initializeExternalFunctions();
  182. GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
  183. GenericValue getOperandValue(Value *V, ExecutionContext &SF);
  184. GenericValue executeTruncInst(Value *SrcVal, Type *DstTy,
  185. ExecutionContext &SF);
  186. GenericValue executeSExtInst(Value *SrcVal, Type *DstTy,
  187. ExecutionContext &SF);
  188. GenericValue executeZExtInst(Value *SrcVal, Type *DstTy,
  189. ExecutionContext &SF);
  190. GenericValue executeFPTruncInst(Value *SrcVal, Type *DstTy,
  191. ExecutionContext &SF);
  192. GenericValue executeFPExtInst(Value *SrcVal, Type *DstTy,
  193. ExecutionContext &SF);
  194. GenericValue executeFPToUIInst(Value *SrcVal, Type *DstTy,
  195. ExecutionContext &SF);
  196. GenericValue executeFPToSIInst(Value *SrcVal, Type *DstTy,
  197. ExecutionContext &SF);
  198. GenericValue executeUIToFPInst(Value *SrcVal, Type *DstTy,
  199. ExecutionContext &SF);
  200. GenericValue executeSIToFPInst(Value *SrcVal, Type *DstTy,
  201. ExecutionContext &SF);
  202. GenericValue executePtrToIntInst(Value *SrcVal, Type *DstTy,
  203. ExecutionContext &SF);
  204. GenericValue executeIntToPtrInst(Value *SrcVal, Type *DstTy,
  205. ExecutionContext &SF);
  206. GenericValue executeBitCastInst(Value *SrcVal, Type *DstTy,
  207. ExecutionContext &SF);
  208. GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal,
  209. Type *Ty, ExecutionContext &SF);
  210. void popStackAndReturnValueToCaller(Type *RetTy, GenericValue Result);
  211. };
  212. } // End llvm namespace
  213. #endif