Eliminate the dependency of ExecutionEngine on the JIT/Interpreter libraries.
[oota-llvm.git] / lib / ExecutionEngine / Interpreter / Interpreter.h
1 //===-- Interpreter.h ------------------------------------------*- C++ -*--===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 <iostream>
25
26 namespace llvm {
27
28 struct FunctionInfo;
29 template<typename T> class generic_gep_type_iterator;
30 class ConstantExpr;
31 typedef generic_gep_type_iterator<User::const_op_iterator> gep_type_iterator;
32
33
34 // AllocaHolder - Object to track all of the blocks of memory allocated by
35 // alloca.  When the function returns, this object is popped off the execution
36 // stack, which causes the dtor to be run, which frees all the alloca'd memory.
37 //
38 class AllocaHolder {
39   friend class AllocaHolderHandle;
40   std::vector<void*> Allocations;
41   unsigned RefCnt;
42 public:
43   AllocaHolder() : RefCnt(0) {}
44   void add(void *mem) { Allocations.push_back(mem); }
45   ~AllocaHolder() {
46     for (unsigned i = 0; i < Allocations.size(); ++i)
47       free(Allocations[i]);
48   }
49 };
50
51 // AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into
52 // a vector...
53 //
54 class AllocaHolderHandle {
55   AllocaHolder *H;
56 public:
57   AllocaHolderHandle() : H(new AllocaHolder()) { H->RefCnt++; }
58   AllocaHolderHandle(const AllocaHolderHandle &AH) : H(AH.H) { H->RefCnt++; }
59   ~AllocaHolderHandle() { if (--H->RefCnt == 0) delete H; }
60
61   void add(void *mem) { H->add(mem); }
62 };
63
64 typedef std::vector<GenericValue> ValuePlaneTy;
65
66 // ExecutionContext struct - This struct represents one stack frame currently
67 // executing.
68 //
69 struct ExecutionContext {
70   Function             *CurFunction;// The currently executing function
71   BasicBlock           *CurBB;      // The currently executing BB
72   BasicBlock::iterator  CurInst;    // The next instruction to execute
73   std::map<Value *, GenericValue> Values; // LLVM values used in this invocation
74   std::vector<GenericValue>  VarArgs; // Values passed through an ellipsis
75   CallSite             Caller;     // Holds the call that called subframes.
76                                    // NULL if main func or debugger invoked fn
77   AllocaHolderHandle    Allocas;    // Track memory allocated by alloca
78 };
79
80 // Interpreter - This class represents the entirety of the interpreter.
81 //
82 class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
83   GenericValue ExitValue;          // The return value of the called function
84   TargetData TD;
85   IntrinsicLowering *IL;
86
87   // The runtime stack of executing code.  The top of the stack is the current
88   // function record.
89   std::vector<ExecutionContext> ECStack;
90
91   // AtExitHandlers - List of functions to call when the program exits,
92   // registered with the atexit() library function.
93   std::vector<Function*> AtExitHandlers;
94
95 public:
96   Interpreter(Module *M, bool isLittleEndian, bool isLongPointer,
97               IntrinsicLowering *IL);
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.  The
110   /// specified IntrinsicLowering implementation will be deleted when the
111   /// Interpreter execution engine is destroyed.
112   ///
113   static ExecutionEngine *create(ModuleProvider *M, IntrinsicLowering *IL);
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 visitAllocationInst(AllocationInst &I);
143   void visitFreeInst(FreeInst &I);
144   void visitLoadInst(LoadInst &I);
145   void visitStoreInst(StoreInst &I);
146   void visitGetElementPtrInst(GetElementPtrInst &I);
147   void visitPHINode(PHINode &PN) { assert(0 && "PHI nodes already handled!"); }
148   void visitCastInst(CastInst &I);
149   void visitSelectInst(SelectInst &I);
150
151
152   void visitCallSite(CallSite CS);
153   void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
154   void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
155   void visitUnwindInst(UnwindInst &I);
156   void visitUnreachableInst(UnreachableInst &I);
157
158   void visitShl(ShiftInst &I);
159   void visitShr(ShiftInst &I);
160   void visitVAArgInst(VAArgInst &I);
161   void visitInstruction(Instruction &I) {
162     std::cerr << I;
163     assert(0 && "Instruction not interpretable yet!");
164   }
165
166   GenericValue callExternalFunction(Function *F,
167                                     const std::vector<GenericValue> &ArgVals);
168   void exitCalled(GenericValue GV);
169
170   void addAtExitHandler(Function *F) {
171     AtExitHandlers.push_back(F);
172   }
173
174   GenericValue *getFirstVarArg () {
175     return &(ECStack.back ().VarArgs[0]);
176   }
177
178   //FIXME: private:
179 public:
180   GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
181                                    gep_type_iterator E, ExecutionContext &SF);
182
183 private:  // Helper functions
184   // SwitchToNewBasicBlock - Start execution in a new basic block and run any
185   // PHI nodes in the top of the block.  This is used for intraprocedural
186   // control flow.
187   //
188   void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
189
190   void *getPointerToFunction(Function *F) { return (void*)F; }
191
192   void initializeExecutionEngine();
193   void initializeExternalFunctions();
194   GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
195   GenericValue getOperandValue(Value *V, ExecutionContext &SF);
196   GenericValue executeCastOperation(Value *SrcVal, const Type *Ty,
197                                     ExecutionContext &SF);
198   void popStackAndReturnValueToCaller(const Type *RetTy, GenericValue Result);
199 };
200
201 } // End llvm namespace
202
203 #endif