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