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