Genericize support for calling functions a bit
[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   bool callMethod(const string &Name);      // return true on failure
78   void setBreakpoint(const string &Name);
79   void infoValue(const string &Name);
80   void printValue(const string &Name);
81   static void printValue(const Type *Ty, GenericValue V);
82
83
84   void list();             // Do the 'list' command
85   void printStackTrace();  // Do the 'backtrace' command
86
87   // Code execution methods...
88   void callMethod        (Method *Meth, const vector<GenericValue> &ArgVals);
89   void callExternalMethod(Method *Meth, const vector<GenericValue> &ArgVals);
90   bool executeInstruction(); // Execute one instruction...
91
92   void stepInstruction();  // Do the 'step' command
93   void nextInstruction();  // Do the 'next' command
94   void run();              // Do the 'run' command
95   void finish();           // Do the 'finish' command
96
97   // Opcode Implementations
98   void executeCallInst(CallInst *I, ExecutionContext &SF);
99   void executeRetInst(ReturnInst *I, ExecutionContext &SF);
100   void executeBrInst(BranchInst *I, ExecutionContext &SF);
101   void executeAllocInst(AllocationInst *I, ExecutionContext &SF);
102
103   // getCurrentMethod - Return the currently executing method
104   inline Method *getCurrentMethod() const {
105     return CurFrame < 0 ? 0 : ECStack[CurFrame].CurMethod;
106   }
107
108   // isStopped - Return true if a program is stopped.  Return false if no
109   // program is running.
110   //
111   inline bool isStopped() const { return !ECStack.empty(); }
112
113 private:  // Helper functions
114   // printCurrentInstruction - Print out the instruction that the virtual PC is
115   // at, or fail silently if no program is running.
116   //
117   void printCurrentInstruction();
118
119   // LookupMatchingNames - Search the current method namespace, then the global
120   // namespace looking for values that match the specified name.  Return ALL
121   // matches to that name.  This is obviously slow, and should only be used for
122   // user interaction.
123   //
124   vector<Value*> LookupMatchingNames(const string &Name);
125
126   // ChooseOneOption - Prompt the user to choose among the specified options to
127   // pick one value.  If no options are provided, emit an error.  If a single 
128   // option is provided, just return that option.
129   //
130   Value *ChooseOneOption(const string &Name, const vector<Value*> &Opts);
131 };
132
133 #endif