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