Adjust to new interface
[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 "Support/DataTypes.h"
24
25 namespace llvm {
26
27 struct FunctionInfo;
28 template<typename T> class generic_gep_type_iterator;
29 class ConstantExpr;
30 typedef generic_gep_type_iterator<User::const_op_iterator> gep_type_iterator;
31
32
33 // AllocaHolder - Object to track all of the blocks of memory allocated by
34 // alloca.  When the function returns, this object is popped off the execution
35 // stack, which causes the dtor to be run, which frees all the alloca'd memory.
36 //
37 class AllocaHolder {
38   friend class AllocaHolderHandle;
39   std::vector<void*> Allocations;
40   unsigned RefCnt;
41 public:
42   AllocaHolder() : RefCnt(0) {}
43   void add(void *mem) { Allocations.push_back(mem); }
44   ~AllocaHolder() {
45     for (unsigned i = 0; i < Allocations.size(); ++i)
46       free(Allocations[i]);
47   }
48 };
49
50 // AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into
51 // a vector...
52 //
53 class AllocaHolderHandle {
54   AllocaHolder *H;
55 public:
56   AllocaHolderHandle() : H(new AllocaHolder()) { H->RefCnt++; }
57   AllocaHolderHandle(const AllocaHolderHandle &AH) : H(AH.H) { H->RefCnt++; }
58   ~AllocaHolderHandle() { if (--H->RefCnt == 0) delete H; }
59
60   void add(void *mem) { H->add(mem); }
61 };
62
63 typedef std::vector<GenericValue> ValuePlaneTy;
64
65 // ExecutionContext struct - This struct represents one stack frame currently
66 // executing.
67 //
68 struct ExecutionContext {
69   Function             *CurFunction;// The currently executing function
70   BasicBlock           *CurBB;      // The currently executing BB
71   BasicBlock::iterator  CurInst;    // The next instruction to execute
72   std::map<Value *, GenericValue> Values; // LLVM values used in this invocation
73   std::vector<GenericValue>  VarArgs; // Values passed through an ellipsis
74   CallSite             Caller;     // Holds the call that called subframes.
75                                    // NULL if main func or debugger invoked fn
76   AllocaHolderHandle    Allocas;    // Track memory allocated by alloca
77 };
78
79 // Interpreter - This class represents the entirety of the interpreter.
80 //
81 class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
82   int ExitCode;                // The exit code to be returned by the lli util
83   TargetData TD;
84   IntrinsicLowering *IL;
85
86   // The runtime stack of executing code.  The top of the stack is the current
87   // function record.
88   std::vector<ExecutionContext> ECStack;
89
90   // AtExitHandlers - List of functions to call when the program exits,
91   // registered with the atexit() library function.
92   std::vector<Function*> AtExitHandlers;
93
94 public:
95   Interpreter(Module *M, bool isLittleEndian, bool isLongPointer,
96               IntrinsicLowering *IL);
97   ~Interpreter();
98
99   /// runAtExitHandlers - Run any functions registered by the program's calls to
100   /// atexit(3), which we intercept and store in AtExitHandlers.
101   ///
102   void runAtExitHandlers();
103
104   /// create - Create an interpreter ExecutionEngine. This can never fail.  The
105   /// specified IntrinsicLowering implementation will be deleted when the
106   /// Interpreter execution engine is destroyed.
107   ///
108   static ExecutionEngine *create(Module *M, IntrinsicLowering *IL);
109
110   /// run - Start execution with the specified function and arguments.
111   ///
112   virtual GenericValue runFunction(Function *F,
113                                    const std::vector<GenericValue> &ArgValues);
114
115   /// recompileAndRelinkFunction - For the interpreter, functions are always
116   /// up-to-date.
117   ///
118   virtual void *recompileAndRelinkFunction(Function *F) {
119     return getPointerToFunction(F);
120   }
121
122   // Methods used to execute code:
123   // Place a call on the stack
124   void callFunction(Function *F, const std::vector<GenericValue> &ArgVals);
125   void run();                // Execute instructions until nothing left to do
126
127   // Opcode Implementations
128   void visitReturnInst(ReturnInst &I);
129   void visitBranchInst(BranchInst &I);
130   void visitSwitchInst(SwitchInst &I);
131
132   void visitBinaryOperator(BinaryOperator &I);
133   void visitAllocationInst(AllocationInst &I);
134   void visitFreeInst(FreeInst &I);
135   void visitLoadInst(LoadInst &I);
136   void visitStoreInst(StoreInst &I);
137   void visitGetElementPtrInst(GetElementPtrInst &I);
138   void visitPHINode(PHINode &PN) { assert(0 && "PHI nodes already handled!"); }
139   void visitCastInst(CastInst &I);
140
141   void visitCallSite(CallSite CS);
142   void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
143   void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
144   void visitUnwindInst(UnwindInst &I);
145
146   void visitShl(ShiftInst &I);
147   void visitShr(ShiftInst &I);
148   void visitVANextInst(VANextInst &I);
149   void visitVAArgInst(VAArgInst &I);
150   void visitInstruction(Instruction &I) {
151     std::cerr << I;
152     assert(0 && "Instruction not interpretable yet!");
153   }
154
155   GenericValue callExternalFunction(Function *F, 
156                                     const std::vector<GenericValue> &ArgVals);
157   void exitCalled(GenericValue GV);
158
159   void addAtExitHandler(Function *F) {
160     AtExitHandlers.push_back(F);
161   }
162
163   GenericValue *getFirstVarArg () {
164     return &(ECStack.back ().VarArgs[0]);
165   }
166
167   //FIXME: private:
168 public:
169   GenericValue executeGEPOperation(Value *Ptr, gep_type_iterator I,
170                                    gep_type_iterator E, ExecutionContext &SF);
171
172 private:  // Helper functions
173   // SwitchToNewBasicBlock - Start execution in a new basic block and run any
174   // PHI nodes in the top of the block.  This is used for intraprocedural
175   // control flow.
176   // 
177   void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
178
179   void *getPointerToFunction(Function *F) { return (void*)F; }
180
181   void initializeExecutionEngine();
182   void initializeExternalFunctions();
183   GenericValue getConstantExprValue(ConstantExpr *CE, ExecutionContext &SF);
184   GenericValue getOperandValue(Value *V, ExecutionContext &SF);
185   GenericValue executeCastOperation(Value *SrcVal, const Type *Ty,
186                                     ExecutionContext &SF);
187   void popStackAndReturnValueToCaller(const Type *RetTy, GenericValue Result);
188 };
189
190 } // End llvm namespace
191
192 #endif