1 //===-- Interpreter.h ------------------------------------------*- C++ -*--===//
3 // This header file defines the interpreter structure
5 //===----------------------------------------------------------------------===//
7 #ifndef LLI_INTERPRETER_H
8 #define LLI_INTERPRETER_H
10 #include "llvm/Module.h"
11 #include "llvm/Method.h"
13 struct MethodInfo; // Defined in ExecutionAnnotations.h
21 unsigned char UByteVal;
23 unsigned short UShortVal;
24 signed short ShortVal;
29 GenericValue *PointerVal;
32 typedef vector<GenericValue> ValuePlaneTy;
34 // ExecutionContext struct - This struct represents one stack frame currently
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
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
50 // Interpreter - This class represents the entirety of the 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
58 // The runtime stack of executing code. The top of the stack is the current
60 vector<ExecutionContext> ECStack;
64 inline ~Interpreter() { delete CurMod; }
66 // getExitCode - return the code that should be the exit code for the lli
68 inline int getExitCode() const { return ExitCode; }
70 // enableProfiling() - Turn profiling on, clear stats?
71 void enableProfiling() { Profile = true; }
73 void initializeExecutionEngine();
74 void handleUserInput();
76 // User Interation Methods...
77 void loadModule(const string &Filename);
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);
87 void list(); // Do the 'list' command
88 void printStackTrace(); // Do the 'backtrace' command
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...
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
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);
106 // getCurrentMethod - Return the currently executing method
107 inline Method *getCurrentMethod() const {
108 return CurFrame < 0 ? 0 : ECStack[CurFrame].CurMethod;
111 // isStopped - Return true if a program is stopped. Return false if no
112 // program is running.
114 inline bool isStopped() const { return !ECStack.empty(); }
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.
120 void printCurrentInstruction();
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
127 vector<Value*> LookupMatchingNames(const string &Name);
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.
133 Value *ChooseOneOption(const string &Name, const vector<Value*> &Opts);