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