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 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);
84 void list(); // Do the 'list' command
85 void printStackTrace(); // Do the 'backtrace' command
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...
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
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);
103 // getCurrentMethod - Return the currently executing method
104 inline Method *getCurrentMethod() const {
105 return CurFrame < 0 ? 0 : ECStack[CurFrame].CurMethod;
108 // isStopped - Return true if a program is stopped. Return false if no
109 // program is running.
111 inline bool isStopped() const { return !ECStack.empty(); }
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.
117 void printCurrentInstruction();
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
124 vector<Value*> LookupMatchingNames(const string &Name);
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.
130 Value *ChooseOneOption(const string &Name, const vector<Value*> &Opts);