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