Put all LLVM code into the llvm namespace, as per bug 109.
[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;        // 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   std::map<Value *, GenericValue> Values; // LLVM values used in this invocation
69   std::vector<GenericValue>  VarArgs; // Values passed through an ellipsis
70   CallSite             Caller;     // Holds the call that called subframes.
71                                    // NULL if main func or debugger invoked fn
72   AllocaHolderHandle    Allocas;    // Track memory allocated by alloca
73 };
74
75 // Interpreter - This class represents the entirety of the interpreter.
76 //
77 class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
78   int ExitCode;                // The exit code to be returned by the lli util
79   TargetData TD;
80
81   // The runtime stack of executing code.  The top of the stack is the current
82   // function record.
83   std::vector<ExecutionContext> ECStack;
84
85   // AtExitHandlers - List of functions to call when the program exits,
86   // registered with the atexit() library function.
87   std::vector<Function*> AtExitHandlers;
88
89 public:
90   Interpreter(Module *M, bool isLittleEndian, bool isLongPointer);
91   inline ~Interpreter() { }
92
93   /// runAtExitHandlers - Run any functions registered by the
94   /// program's calls to atexit(3), which we intercept and store in
95   /// AtExitHandlers.
96   ///
97   void runAtExitHandlers ();
98
99   /// create - Create an interpreter ExecutionEngine. This can never fail.
100   ///
101   static ExecutionEngine *create(Module *M);
102
103   /// run - Start execution with the specified function and arguments.
104   ///
105   virtual GenericValue run(Function *F,
106                            const std::vector<GenericValue> &ArgValues);
107
108   // Methods used to execute code:
109   // Place a call on the stack
110   void callFunction(Function *F, const std::vector<GenericValue> &ArgVals);
111   void run();                // Execute instructions until nothing left to do
112
113   // Opcode Implementations
114   void visitReturnInst(ReturnInst &I);
115   void visitBranchInst(BranchInst &I);
116   void visitSwitchInst(SwitchInst &I);
117
118   void visitBinaryOperator(BinaryOperator &I);
119   void visitAllocationInst(AllocationInst &I);
120   void visitFreeInst(FreeInst &I);
121   void visitLoadInst(LoadInst &I);
122   void visitStoreInst(StoreInst &I);
123   void visitGetElementPtrInst(GetElementPtrInst &I);
124   void visitPHINode(PHINode &PN) { assert(0 && "PHI nodes already handled!"); }
125   void visitCastInst(CastInst &I);
126
127   void visitCallSite(CallSite CS);
128   void visitCallInst(CallInst &I) { visitCallSite (CallSite (&I)); }
129   void visitInvokeInst(InvokeInst &I) { visitCallSite (CallSite (&I)); }
130   void visitUnwindInst(UnwindInst &I);
131
132   void visitShl(ShiftInst &I);
133   void visitShr(ShiftInst &I);
134   void visitVANextInst(VANextInst &I);
135   void visitVAArgInst(VAArgInst &I);
136   void visitInstruction(Instruction &I) {
137     std::cerr << I;
138     assert(0 && "Instruction not interpretable yet!");
139   }
140
141   GenericValue callExternalFunction(Function *F, 
142                                     const std::vector<GenericValue> &ArgVals);
143   void exitCalled(GenericValue GV);
144
145   void addAtExitHandler(Function *F) {
146     AtExitHandlers.push_back(F);
147   }
148
149   //FIXME: private:
150 public:
151   GenericValue executeGEPOperation(Value *Ptr, User::op_iterator I,
152                                    User::op_iterator E, ExecutionContext &SF);
153
154 private:  // Helper functions
155   // SwitchToNewBasicBlock - Start execution in a new basic block and run any
156   // PHI nodes in the top of the block.  This is used for intraprocedural
157   // control flow.
158   // 
159   void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
160
161   void *getPointerToFunction(Function *F) { return (void*)F; }
162
163   void initializeExecutionEngine();
164   void initializeExternalFunctions();
165   GenericValue getOperandValue(Value *V, ExecutionContext &SF);
166   GenericValue executeCastOperation(Value *SrcVal, const Type *Ty,
167                                     ExecutionContext &SF);
168   void popStackAndReturnValueToCaller(const Type *RetTy, GenericValue Result);
169 };
170
171 } // End llvm namespace
172
173 #endif