Remove support for breakpoints (not used).
[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 "../ExecutionEngine.h"
11 #include "../GenericValue.h"
12 #include "Support/DataTypes.h"
13 #include "llvm/Assembly/CachedWriter.h"
14 #include "llvm/Target/TargetData.h"
15 #include "llvm/BasicBlock.h"
16 #include "llvm/Support/InstVisitor.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 Profile;                // Profiling enabled?
75   bool Trace;                  // Tracing enabled?
76   int CurFrame;                // The current stack frame being inspected
77   TargetData TD;
78
79   // The runtime stack of executing code.  The top of the stack is the current
80   // function record.
81   std::vector<ExecutionContext> ECStack;
82
83   // AtExitHandlers - List of functions to call when the program exits.
84   std::vector<Function*> AtExitHandlers;
85 public:
86   Interpreter(Module *M, bool isLittleEndian, bool isLongPointer,
87               bool TraceMode);
88   inline ~Interpreter() { CW.setModule(0); }
89
90   /// create - Create an interpreter ExecutionEngine. This can never fail.
91   ///
92   static ExecutionEngine *create(Module *M, bool TraceMode);
93
94   /// getExitCode - return the code that should be the exit code for the lli
95   /// utility.
96   ///
97   inline int getExitCode() const { return ExitCode; }
98
99   /// run - Start execution with the specified function and arguments.
100   ///
101   virtual int run(const std::string &FnName,
102                   const std::vector<std::string> &Args,
103                   const char ** envp);
104  
105
106   // enableProfiling() - Turn profiling on, clear stats?
107   void enableProfiling() { Profile = true; }
108   void enableTracing() { Trace = true; }
109
110   void handleUserInput();
111
112   // User Interation Methods...
113   bool callFunction(const std::string &Name);      // return true on failure
114   void infoValue(const std::string &Name);
115   void print(const std::string &Name);
116   static void print(const Type *Ty, GenericValue V);
117   static void printValue(const Type *Ty, GenericValue V);
118
119   bool callMainFunction(const std::string &MainName,
120                         const std::vector<std::string> &InputFilename);
121
122   void list();             // Do the 'list' command
123   void printStackTrace();  // Do the 'backtrace' command
124
125   // Code execution methods...
126   void callFunction(Function *F, const std::vector<GenericValue> &ArgVals);
127   void executeInstruction(); // Execute one instruction...
128
129   void stepInstruction();  // Do the 'step' command
130   void nextInstruction();  // Do the 'next' command
131   void run();              // Do the 'run' command
132   void finish();           // Do the 'finish' command
133
134   // Opcode Implementations
135   void visitReturnInst(ReturnInst &I);
136   void visitBranchInst(BranchInst &I);
137   void visitSwitchInst(SwitchInst &I);
138
139   void visitBinaryOperator(BinaryOperator &I);
140   void visitAllocationInst(AllocationInst &I);
141   void visitFreeInst(FreeInst &I);
142   void visitLoadInst(LoadInst &I);
143   void visitStoreInst(StoreInst &I);
144   void visitGetElementPtrInst(GetElementPtrInst &I);
145
146   void visitPHINode(PHINode &PN) { assert(0 && "PHI nodes already handled!"); }
147   void visitCastInst(CastInst &I);
148   void visitCallInst(CallInst &I);
149   void visitShl(ShiftInst &I);
150   void visitShr(ShiftInst &I);
151   void visitVarArgInst(VarArgInst &I);
152   void visitInstruction(Instruction &I) {
153     std::cerr << I;
154     assert(0 && "Instruction not interpretable yet!");
155   }
156
157   GenericValue callExternalFunction(Function *F, 
158                                     const std::vector<GenericValue> &ArgVals);
159   void exitCalled(GenericValue GV);
160
161   // getCurrentFunction - Return the currently executing function
162   inline Function *getCurrentFunction() const {
163     return CurFrame < 0 ? 0 : ECStack[CurFrame].CurFunction;
164   }
165
166   // isStopped - Return true if a program is stopped.  Return false if no
167   // program is running.
168   //
169   inline bool isStopped() const { return !ECStack.empty(); }
170
171   void addAtExitHandler(Function *F) {
172     AtExitHandlers.push_back(F);
173   }
174
175   //FIXME: private:
176 public:
177   GenericValue executeGEPOperation(Value *Ptr, User::op_iterator I,
178                                    User::op_iterator E, ExecutionContext &SF);
179
180 private:  // Helper functions
181   // SwitchToNewBasicBlock - Start execution in a new basic block and run any
182   // PHI nodes in the top of the block.  This is used for intraprocedural
183   // control flow.
184   // 
185   void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
186
187   void *getPointerToFunction(Function *F) { return (void*)F; }
188
189   // getCurrentExecutablePath() - Return the directory that the lli executable
190   // lives in.
191   //
192   std::string getCurrentExecutablePath() const;
193
194   // printCurrentInstruction - Print out the instruction that the virtual PC is
195   // at, or fail silently if no program is running.
196   //
197   void printCurrentInstruction();
198
199   // printStackFrame - Print information about the specified stack frame, or -1
200   // for the default one.
201   //
202   void printStackFrame(int FrameNo = -1);
203
204   // LookupMatchingNames - Search the current function namespace, then the
205   // global namespace looking for values that match the specified name.  Return
206   // ALL matches to that name.  This is obviously slow, and should only be used
207   // for user interaction.
208   //
209   std::vector<Value*> LookupMatchingNames(const std::string &Name);
210
211   // ChooseOneOption - Prompt the user to choose among the specified options to
212   // pick one value.  If no options are provided, emit an error.  If a single 
213   // option is provided, just return that option.
214   //
215   Value *ChooseOneOption(const std::string &Name,
216                          const std::vector<Value*> &Opts);
217
218   void initializeExecutionEngine();
219   void initializeExternalFunctions();
220 };
221
222 #endif