f9d4770d4bd4dc484b36830c7f65b40ce8da5097
[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 LLI_INTERPRETER_H
15 #define LLI_INTERPRETER_H
16
17 #include "llvm/Function.h"
18 #include "llvm/ExecutionEngine/ExecutionEngine.h"
19 #include "llvm/ExecutionEngine/GenericValue.h"
20 #include "llvm/Support/InstVisitor.h"
21 #include "llvm/Support/CallSite.h"
22 #include "llvm/Target/TargetData.h"
23 #include "llvm/Support/DataTypes.h"
24 #include "llvm/Support/ErrorHandling.h"
25
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   TargetData 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(ModuleProvider *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 - Create an interpreter ExecutionEngine. This can never fail.
110   ///
111   static ExecutionEngine *create(ModuleProvider *M, std::string *ErrorStr = 0,
112                                  CodeGenOpt::Level = CodeGenOpt::Default,
113                                  bool GVsWithCode = true);
114
115   /// run - Start execution with the specified function and arguments.
116   ///
117   virtual GenericValue runFunction(Function *F,
118                                    const std::vector<GenericValue> &ArgValues);
119
120   /// recompileAndRelinkFunction - For the interpreter, functions are always
121   /// up-to-date.
122   ///
123   virtual void *recompileAndRelinkFunction(Function *F) {
124     return getPointerToFunction(F);
125   }
126
127   /// freeMachineCodeForFunction - The interpreter does not generate any code.
128   ///
129   void freeMachineCodeForFunction(Function *F) { }
130
131   // Methods used to execute code:
132   // Place a call on the stack
133   void callFunction(Function *F, const std::vector<GenericValue> &ArgVals);
134   void run();                // Execute instructions until nothing left to do
135
136   // Opcode Implementations
137   void visitReturnInst(ReturnInst &I);
138   void visitBranchInst(BranchInst &I);
139   void visitSwitchInst(SwitchInst &I);
140
141   void visitBinaryOperator(BinaryOperator &I);
142   void visitICmpInst(ICmpInst &I);
143   void visitFCmpInst(FCmpInst &I);
144   void visitAllocationInst(AllocationInst &I);
145   void visitFreeInst(FreeInst &I);
146   void visitLoadInst(LoadInst &I);
147   void visitStoreInst(StoreInst &I);
148   void visitGetElementPtrInst(GetElementPtrInst &I);
149   void visitPHINode(PHINode &PN) { 
150     LLVM_UNREACHABLE("PHI nodes already handled!"); 
151   }
152   void visitTruncInst(TruncInst &I);
153   void visitZExtInst(ZExtInst &I);
154   void visitSExtInst(SExtInst &I);
155   void visitFPTruncInst(FPTruncInst &I);
156   void visitFPExtInst(FPExtInst &I);
157   void visitUIToFPInst(UIToFPInst &I);
158   void visitSIToFPInst(SIToFPInst &I);
159   void visitFPToUIInst(FPToUIInst &I);
160   void visitFPToSIInst(FPToSIInst &I);
161   void visitPtrToIntInst(PtrToIntInst &I);
162   void visitIntToPtrInst(IntToPtrInst &I);
163   void visitBitCastInst(BitCastInst &I);
164   void visitSelectInst(SelectInst &I);
165
166
167   void visitCallSite(CallSite CS);
168   void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
169   void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
170   void visitUnwindInst(UnwindInst &I);
171   void visitUnreachableInst(UnreachableInst &I);
172
173   void visitShl(BinaryOperator &I);
174   void visitLShr(BinaryOperator &I);
175   void visitAShr(BinaryOperator &I);
176
177   void visitVAArgInst(VAArgInst &I);
178   void visitInstruction(Instruction &I) {
179     cerr << I;
180     LLVM_UNREACHABLE("Instruction not interpretable yet!");
181   }
182
183   GenericValue callExternalFunction(Function *F,
184                                     const std::vector<GenericValue> &ArgVals);
185   void exitCalled(GenericValue GV);
186
187   void addAtExitHandler(Function *F) {
188     AtExitHandlers.push_back(F);
189   }
190
191   GenericValue *getFirstVarArg () {
192     return &(ECStack.back ().VarArgs[0]);
193   }
194
195   //FIXME: private:
196 public:
197   GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
198                                    gep_type_iterator E, ExecutionContext &SF);
199
200 private:  // Helper functions
201   // SwitchToNewBasicBlock - Start execution in a new basic block and run any
202   // PHI nodes in the top of the block.  This is used for intraprocedural
203   // control flow.
204   //
205   void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
206
207   void *getPointerToFunction(Function *F) { return (void*)F; }
208
209   void initializeExecutionEngine() { }
210   void initializeExternalFunctions();
211   GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
212   GenericValue getOperandValue(Value *V, ExecutionContext &SF);
213   GenericValue executeTruncInst(Value *SrcVal, const Type *DstTy,
214                                 ExecutionContext &SF);
215   GenericValue executeSExtInst(Value *SrcVal, const Type *DstTy,
216                                ExecutionContext &SF);
217   GenericValue executeZExtInst(Value *SrcVal, const Type *DstTy,
218                                ExecutionContext &SF);
219   GenericValue executeFPTruncInst(Value *SrcVal, const Type *DstTy,
220                                   ExecutionContext &SF);
221   GenericValue executeFPExtInst(Value *SrcVal, const Type *DstTy,
222                                 ExecutionContext &SF);
223   GenericValue executeFPToUIInst(Value *SrcVal, const Type *DstTy,
224                                  ExecutionContext &SF);
225   GenericValue executeFPToSIInst(Value *SrcVal, const Type *DstTy,
226                                  ExecutionContext &SF);
227   GenericValue executeUIToFPInst(Value *SrcVal, const Type *DstTy,
228                                  ExecutionContext &SF);
229   GenericValue executeSIToFPInst(Value *SrcVal, const Type *DstTy,
230                                  ExecutionContext &SF);
231   GenericValue executePtrToIntInst(Value *SrcVal, const Type *DstTy,
232                                    ExecutionContext &SF);
233   GenericValue executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
234                                    ExecutionContext &SF);
235   GenericValue executeBitCastInst(Value *SrcVal, const Type *DstTy,
236                                   ExecutionContext &SF);
237   GenericValue executeCastOperation(Instruction::CastOps opcode, Value *SrcVal, 
238                                     const Type *Ty, ExecutionContext &SF);
239   void popStackAndReturnValueToCaller(const Type *RetTy, GenericValue Result);
240
241 };
242
243 } // End llvm namespace
244
245 #endif