Interpreter.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 LLI_INTERPRETER_H
  14. #define LLI_INTERPRETER_H
  15. #include "llvm/Function.h"
  16. #include "llvm/ExecutionEngine/ExecutionEngine.h"
  17. #include "llvm/ExecutionEngine/GenericValue.h"
  18. #include "llvm/Target/TargetData.h"
  19. #include "llvm/Support/CallSite.h"
  20. #include "llvm/Support/DataTypes.h"
  21. #include "llvm/Support/ErrorHandling.h"
  22. #include "llvm/Support/InstVisitor.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. TargetData 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(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 - Create an interpreter ExecutionEngine. This can never fail.
  94. ///
  95. static ExecutionEngine *create(Module *M, std::string *ErrorStr = 0);
  96. /// run - Start execution with the specified function and arguments.
  97. ///
  98. virtual GenericValue runFunction(Function *F,
  99. const std::vector<GenericValue> &ArgValues);
  100. virtual void *getPointerToNamedFunction(const std::string &Name,
  101. bool AbortOnFailure = true) {
  102. // FIXME: not implemented.
  103. return 0;
  104. }
  105. /// recompileAndRelinkFunction - For the interpreter, functions are always
  106. /// up-to-date.
  107. ///
  108. virtual void *recompileAndRelinkFunction(Function *F) {
  109. return getPointerToFunction(F);
  110. }
  111. /// freeMachineCodeForFunction - The interpreter does not generate any code.
  112. ///
  113. void freeMachineCodeForFunction(Function *F) { }
  114. // Methods used to execute code:
  115. // Place a call on the stack
  116. void callFunction(Function *F, const std::vector<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 visitInstruction(Instruction &I) {
  155. errs() << I << "\n";
  156. llvm_unreachable("Instruction not interpretable yet!");
  157. }
  158. GenericValue callExternalFunction(Function *F,
  159. const std::vector<GenericValue> &ArgVals);
  160. void exitCalled(GenericValue GV);
  161. void addAtExitHandler(Function *F) {
  162. AtExitHandlers.push_back(F);
  163. }
  164. GenericValue *getFirstVarArg () {
  165. return &(ECStack.back ().VarArgs[0]);
  166. }
  167. private: // Helper functions
  168. GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
  169. gep_type_iterator E, ExecutionContext &SF);
  170. // SwitchToNewBasicBlock - Start execution in a new basic block and run any
  171. // PHI nodes in the top of the block. This is used for intraprocedural
  172. // control flow.
  173. //
  174. void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
  175. void *getPointerToFunction(Function *F) { return (void*)F; }
  176. void *getPointerToBasicBlock(BasicBlock *BB) { return (void*)BB; }
  177. void initializeExecutionEngine() { }
  178. void initializeExternalFunctions();
  179. GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
  180. GenericValue getOperandValue(Value *V, ExecutionContext &SF);
  181. GenericValue executeTruncInst(Value *SrcVal, Type *DstTy,
  182. ExecutionContext &SF);
  183. GenericValue executeSExtInst(Value *SrcVal, Type *DstTy,
  184. ExecutionContext &SF);
  185. GenericValue executeZExtInst(Value *SrcVal, Type *DstTy,
  186. ExecutionContext &SF);
  187. GenericValue executeFPTruncInst(Value *SrcVal, Type *DstTy,
  188. ExecutionContext &SF);
  189. GenericValue executeFPExtInst(Value *SrcVal, Type *DstTy,
  190. ExecutionContext &SF);
  191. GenericValue executeFPToUIInst(Value *SrcVal, Type *DstTy,
  192. ExecutionContext &SF);
  193. GenericValue executeFPToSIInst(Value *SrcVal, Type *DstTy,
  194. ExecutionContext &SF);
  195. GenericValue executeUIToFPInst(Value *SrcVal, Type *DstTy,
  196. ExecutionContext &SF);
  197. GenericValue executeSIToFPInst(Value *SrcVal, Type *DstTy,
  198. ExecutionContext &SF);
  199. GenericValue executePtrToIntInst(Value *SrcVal, Type *DstTy,
  200. ExecutionContext &SF);
  201. GenericValue executeIntToPtrInst(Value *SrcVal, Type *DstTy,
  202. ExecutionContext &SF);
  203. GenericValue executeBitCastInst(Value *SrcVal, Type *DstTy,
  204. ExecutionContext &SF);
  205. GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal,
  206. Type *Ty, ExecutionContext &SF);
  207. void popStackAndReturnValueToCaller(Type *RetTy, GenericValue Result);
  208. };
  209. } // End llvm namespace
  210. #endif