Move DataTypes.h from llvm/Support to just Support
[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 // Uncomment this line to enable profiling of structure field accesses.
11 #define PROFILE_STRUCTURE_FIELDS 1
12
13
14 #include "llvm/Module.h"
15 #include "llvm/Method.h"
16 #include "Support/DataTypes.h"
17 #include "llvm/Assembly/CachedWriter.h"
18
19 extern CachedWriter CW;     // Object to accellerate printing of LLVM
20
21 struct MethodInfo;          // Defined in ExecutionAnnotations.h
22 class CallInst;
23 class ReturnInst;
24 class BranchInst;
25 class AllocationInst;
26
27 typedef uint64_t PointerTy;
28
29 union GenericValue {
30   bool            BoolVal;
31   unsigned char   UByteVal;
32   signed   char   SByteVal;
33   unsigned short  UShortVal;
34   signed   short  ShortVal;
35   unsigned int    UIntVal;
36   signed   int    IntVal;
37   uint64_t        ULongVal;
38   int64_t         LongVal;
39   double          DoubleVal;
40   float           FloatVal;
41   PointerTy       PointerVal;
42 };
43
44 typedef vector<GenericValue> ValuePlaneTy;
45
46 // ExecutionContext struct - This struct represents one stack frame currently
47 // executing.
48 //
49 struct ExecutionContext {
50   Method               *CurMethod;  // The currently executing method
51   BasicBlock           *CurBB;      // The currently executing BB
52   BasicBlock::iterator  CurInst;    // The next instruction to execute
53   MethodInfo           *MethInfo;   // The MethInfo annotation for the method
54   vector<ValuePlaneTy>  Values;     // ValuePlanes for each type
55
56   BasicBlock           *PrevBB;     // The previous BB or null if in first BB
57   CallInst             *Caller;     // Holds the call that called subframes.
58                                     // NULL if main func or debugger invoked fn
59 };
60
61 // Interpreter - This class represents the entirety of the interpreter.
62 //
63 class Interpreter {
64   Module *CurMod;              // The current Module being executed (0 if none)
65   int ExitCode;                // The exit code to be returned by the lli util
66   bool Profile;                // Profiling enabled?
67   bool Trace;                  // Tracing enabled?
68   int CurFrame;                // The current stack frame being inspected
69
70   // The runtime stack of executing code.  The top of the stack is the current
71   // method record.
72   vector<ExecutionContext> ECStack;
73
74 public:
75   Interpreter();
76   inline ~Interpreter() { CW.setModule(0); delete CurMod; }
77
78   // getExitCode - return the code that should be the exit code for the lli
79   // utility.
80   inline int getExitCode() const { return ExitCode; }
81
82   // enableProfiling() - Turn profiling on, clear stats?
83   void enableProfiling() { Profile = true; }
84   void enableTracing() { Trace = true; }
85
86   void handleUserInput();
87
88   // User Interation Methods...
89   void loadModule(const string &Filename);
90   bool flushModule();
91   bool callMethod(const string &Name);      // return true on failure
92   void setBreakpoint(const string &Name);
93   void infoValue(const string &Name);
94   void print(const string &Name);
95   static void print(const Type *Ty, GenericValue V);
96   static void printValue(const Type *Ty, GenericValue V);
97
98   // Hack until we can parse command line args...
99   bool callMainMethod(const string &MainName,
100                       const vector<string> &InputFilename);
101
102   void list();             // Do the 'list' command
103   void printStackTrace();  // Do the 'backtrace' command
104
105   // Code execution methods...
106   void callMethod        (Method *Meth, const vector<GenericValue> &ArgVals);
107   bool executeInstruction(); // Execute one instruction...
108
109   void stepInstruction();  // Do the 'step' command
110   void nextInstruction();  // Do the 'next' command
111   void run();              // Do the 'run' command
112   void finish();           // Do the 'finish' command
113
114   // Opcode Implementations
115   void executeCallInst(CallInst *I, ExecutionContext &SF);
116   void executeRetInst(ReturnInst *I, ExecutionContext &SF);
117   void executeBrInst(BranchInst *I, ExecutionContext &SF);
118   void executeAllocInst(AllocationInst *I, ExecutionContext &SF);
119   GenericValue callExternalMethod(Method *Meth, 
120                                   const vector<GenericValue> &ArgVals);
121   void exitCalled(GenericValue GV);
122
123   // getCurrentMethod - Return the currently executing method
124   inline Method *getCurrentMethod() const {
125     return CurFrame < 0 ? 0 : ECStack[CurFrame].CurMethod;
126   }
127
128   // isStopped - Return true if a program is stopped.  Return false if no
129   // program is running.
130   //
131   inline bool isStopped() const { return !ECStack.empty(); }
132
133 private:  // Helper functions
134   // getCurrentExecutablePath() - Return the directory that the lli executable
135   // lives in.
136   //
137   string getCurrentExecutablePath() const;
138
139   // printCurrentInstruction - Print out the instruction that the virtual PC is
140   // at, or fail silently if no program is running.
141   //
142   void printCurrentInstruction();
143
144   // printStackFrame - Print information about the specified stack frame, or -1
145   // for the default one.
146   //
147   void printStackFrame(int FrameNo = -1);
148
149   // LookupMatchingNames - Search the current method namespace, then the global
150   // namespace looking for values that match the specified name.  Return ALL
151   // matches to that name.  This is obviously slow, and should only be used for
152   // user interaction.
153   //
154   vector<Value*> LookupMatchingNames(const string &Name);
155
156   // ChooseOneOption - Prompt the user to choose among the specified options to
157   // pick one value.  If no options are provided, emit an error.  If a single 
158   // option is provided, just return that option.
159   //
160   Value *ChooseOneOption(const string &Name, const vector<Value*> &Opts);
161
162
163   void initializeExecutionEngine();
164   void initializeExternalMethods();
165 };
166
167 #endif