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