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