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