9ff83365dbee7bab7eacb17a78c353e5ddba9bd2
[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/Module.h"
11 #include "llvm/Method.h"
12
13 struct MethodInfo;          // Defined in ExecutionAnnotations.h
14 class CallInst;
15 class ReturnInst;
16 class BranchInst;
17
18 union GenericValue {
19   bool            BoolVal;
20   unsigned char   UByteVal;
21   signed   char   SByteVal;
22   unsigned short  UShortVal;
23   signed   short  ShortVal;
24   unsigned int    UIntVal;
25   signed   int    IntVal;
26   double          DoubleVal;
27   float           FloatVal;
28   GenericValue *PointerVal;
29 };
30
31 typedef vector<GenericValue> ValuePlaneTy;
32
33 // ExecutionContext struct - This struct represents one stack frame currently
34 // executing.
35 //
36 struct ExecutionContext {
37   Method               *CurMethod;  // The currently executing method
38   BasicBlock           *CurBB;      // The currently executing BB
39   BasicBlock::iterator  CurInst;    // The next instruction to execute
40   MethodInfo           *MethInfo;   // The MethInfo annotation for the method
41   vector<ValuePlaneTy>  Values;     // ValuePlanes for each type
42
43   BasicBlock           *PrevBB;     // The previous BB or null if in first BB
44   CallInst             *Caller;     // Holds the call that called subframes.
45                                     // NULL if main func or debugger invoked fn
46 };
47
48
49 // Interpreter - This class represents the entirety of the interpreter.
50 //
51 class Interpreter {
52   Module *CurMod;              // The current Module being executed (0 if none)
53   int ExitCode;                // The exit code to be returned by the lli util
54   bool Profile;                // Profiling enabled?
55   int CurFrame;                // The current stack frame being inspected
56
57   // The runtime stack of executing code.  The top of the stack is the current
58   // method record.
59   vector<ExecutionContext> ECStack;
60
61 public:
62   Interpreter();
63   inline ~Interpreter() { delete CurMod; }
64
65   // getExitCode - return the code that should be the exit code for the lli
66   // utility.
67   inline int getExitCode() const { return ExitCode; }
68
69   // enableProfiling() - Turn profiling on, clear stats?
70   void enableProfiling() { Profile = true; }
71
72   void initializeExecutionEngine();
73   void handleUserInput();
74
75   // User Interation Methods...
76   bool callMethod(const string &Name);      // return true on failure
77   void setBreakpoint(const string &Name);
78   void printValue(const string &Name);
79   void printValue(const Type *Ty, GenericValue V);
80
81
82   void list();             // Do the 'list' command
83   void printStackTrace();  // Do the 'backtrace' command
84
85   // Code execution methods...
86   void callMethod(Method *Meth, ExecutionContext *SF = 0);
87   bool executeInstruction(); // Execute one instruction...
88
89   void stepInstruction();  // Do the 'step' command
90   void nextInstruction();  // Do the 'next' command
91   void run();              // Do the 'run' command
92   void finish();           // Do the 'finish' command
93
94   // Opcode Implementations
95   void executeCallInst(CallInst *I, ExecutionContext &SF);
96   void executeRetInst(ReturnInst *I, ExecutionContext &SF);
97   void executeBrInst(BranchInst *I, ExecutionContext &SF);
98
99   // getCurrentMethod - Return the currently executing method
100   inline Method *getCurrentMethod() const {
101     return CurFrame < 0 ? 0 : ECStack[CurFrame].CurMethod;
102   }
103
104   // isStopped - Return true if a program is stopped.  Return false if no
105   // program is running.
106   //
107   inline bool isStopped() const { return !ECStack.empty(); }
108
109 private:  // Helper functions
110   // printCurrentInstruction - Print out the instruction that the virtual PC is
111   // at, or fail silently if no program is running.
112   //
113   void printCurrentInstruction();
114
115   // LookupMatchingNames - Search the current method namespace, then the global
116   // namespace looking for values that match the specified name.  Return ALL
117   // matches to that name.  This is obviously slow, and should only be used for
118   // user interaction.
119   //
120   vector<Value*> LookupMatchingNames(const string &Name);
121
122   // ChooseOneOption - Prompt the user to choose among the specified options to
123   // pick one value.  If no options are provided, emit an error.  If a single 
124   // option is provided, just return that option.
125   //
126   Value *ChooseOneOption(const string &Name, const vector<Value*> &Opts);
127 };
128
129 #endif