Interpreter.h 8.6 KB

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