62ac1f0d3fd588566a01a6b0ab0901de3d81f9b5
[oota-llvm.git] / lib / ExecutionEngine / Interpreter / Interpreter.h
1 //===-- Interpreter.h ------------------------------------------*- C++ -*--===//
2 //
3 // This header file defines the interpreter structure
4 //
5 //===----------------------------------------------------------------------===//
6
7 #ifndef LLI_INTERPRETER_H
8 #define LLI_INTERPRETER_H
9
10 #include "llvm/BasicBlock.h"
11 #include "llvm/Assembly/CachedWriter.h"
12 #include "llvm/ExecutionEngine/ExecutionEngine.h"
13 #include "llvm/ExecutionEngine/GenericValue.h"
14 #include "llvm/Support/InstVisitor.h"
15 #include "llvm/Target/TargetData.h"
16 #include "Support/DataTypes.h"
17
18 extern CachedWriter CW;     // Object to accelerate printing of LLVM
19
20 struct FunctionInfo;        // Defined in ExecutionAnnotations.h
21
22 // AllocaHolder - Object to track all of the blocks of memory allocated by
23 // alloca.  When the function returns, this object is poped off the execution
24 // stack, which causes the dtor to be run, which frees all the alloca'd memory.
25 //
26 class AllocaHolder {
27   friend class AllocaHolderHandle;
28   std::vector<void*> Allocations;
29   unsigned RefCnt;
30 public:
31   AllocaHolder() : RefCnt(0) {}
32   void add(void *mem) { Allocations.push_back(mem); }
33   ~AllocaHolder() {
34     for (unsigned i = 0; i < Allocations.size(); ++i)
35       free(Allocations[i]);
36   }
37 };
38
39 // AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into
40 // a vector...
41 //
42 class AllocaHolderHandle {
43   AllocaHolder *H;
44 public:
45   AllocaHolderHandle() : H(new AllocaHolder()) { H->RefCnt++; }
46   AllocaHolderHandle(const AllocaHolderHandle &AH) : H(AH.H) { H->RefCnt++; }
47   ~AllocaHolderHandle() { if (--H->RefCnt == 0) delete H; }
48
49   void add(void *mem) { H->add(mem); }
50 };
51
52 typedef std::vector<GenericValue> ValuePlaneTy;
53
54 // ExecutionContext struct - This struct represents one stack frame currently
55 // executing.
56 //
57 struct ExecutionContext {
58   Function             *CurFunction;// The currently executing function
59   BasicBlock           *CurBB;      // The currently executing BB
60   BasicBlock::iterator  CurInst;    // The next instruction to execute
61   FunctionInfo         *FuncInfo;   // The FuncInfo annotation for the function
62   std::vector<ValuePlaneTy>  Values;// ValuePlanes for each type
63   std::vector<GenericValue>  VarArgs; // Values passed through an ellipsis
64
65   CallInst             *Caller;     // Holds the call that called subframes.
66                                     // NULL if main func or debugger invoked fn
67   AllocaHolderHandle    Allocas;    // Track memory allocated by alloca
68 };
69
70 // Interpreter - This class represents the entirety of the interpreter.
71 //
72 class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
73   int ExitCode;                // The exit code to be returned by the lli util
74   bool Trace;                  // Tracing enabled?
75   int CurFrame;                // The current stack frame being inspected
76   TargetData TD;
77
78   // The runtime stack of executing code.  The top of the stack is the current
79   // function record.
80   std::vector<ExecutionContext> ECStack;
81
82   // AtExitHandlers - List of functions to call when the program exits,
83   // registered with the atexit() library function.
84   std::vector<Function*> AtExitHandlers;
85
86   std::map<Function*, FunctionInfo*> FunctionInfoMap;
87 public:
88   Interpreter(Module *M, bool isLittleEndian, bool isLongPointer,
89               bool TraceMode);
90   inline ~Interpreter() { CW.setModule(0); }
91
92   /// runAtExitHandlers - Run any functions registered by the
93   /// program's calls to atexit(3), which we intercept and store in
94   /// AtExitHandlers.
95   ///
96   void runAtExitHandlers ();
97
98   /// create - Create an interpreter ExecutionEngine. This can never fail.
99   ///
100   static ExecutionEngine *create(Module *M, bool TraceMode);
101
102   /// run - Start execution with the specified function and arguments.
103   ///
104   virtual GenericValue run(Function *F,
105                            const std::vector<GenericValue> &ArgValues);
106
107   // Methods used for debug printouts:
108   static void print(const Type *Ty, GenericValue V);
109   static void printValue(const Type *Ty, GenericValue V);
110
111   // Methods used to execute code:
112   // Place a call on the stack
113   void callFunction(Function *F, const std::vector<GenericValue> &ArgVals);
114   void executeInstruction(); // Execute one instruction
115   void run();                // Execute instructions until nothing left to do
116
117   // Opcode Implementations
118   void visitReturnInst(ReturnInst &I);
119   void visitBranchInst(BranchInst &I);
120   void visitSwitchInst(SwitchInst &I);
121
122   void visitBinaryOperator(BinaryOperator &I);
123   void visitAllocationInst(AllocationInst &I);
124   void visitFreeInst(FreeInst &I);
125   void visitLoadInst(LoadInst &I);
126   void visitStoreInst(StoreInst &I);
127   void visitGetElementPtrInst(GetElementPtrInst &I);
128
129   void visitPHINode(PHINode &PN) { assert(0 && "PHI nodes already handled!"); }
130   void visitCastInst(CastInst &I);
131   void visitCallInst(CallInst &I);
132   void visitShl(ShiftInst &I);
133   void visitShr(ShiftInst &I);
134   void visitVANextInst(VANextInst &I);
135   void visitInstruction(Instruction &I) {
136     std::cerr << I;
137     assert(0 && "Instruction not interpretable yet!");
138   }
139
140   GenericValue callExternalFunction(Function *F, 
141                                     const std::vector<GenericValue> &ArgVals);
142   void exitCalled(GenericValue GV);
143
144   void addAtExitHandler(Function *F) {
145     AtExitHandlers.push_back(F);
146   }
147
148   //FIXME: private:
149 public:
150   GenericValue executeGEPOperation(Value *Ptr, User::op_iterator I,
151                                    User::op_iterator E, ExecutionContext &SF);
152
153 private:  // Helper functions
154   // SwitchToNewBasicBlock - Start execution in a new basic block and run any
155   // PHI nodes in the top of the block.  This is used for intraprocedural
156   // control flow.
157   // 
158   void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
159
160   void *getPointerToFunction(Function *F) { return (void*)F; }
161
162   void initializeExecutionEngine();
163   void initializeExternalFunctions();
164   GenericValue getOperandValue(Value *V, ExecutionContext &SF);
165   GenericValue executeCastOperation(Value *SrcVal, const Type *Ty,
166                                     ExecutionContext &SF);
167 };
168
169 #endif