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
20 unsigned char UByteVal;
22 unsigned short UShortVal;
23 signed short ShortVal;
28 GenericValue *PointerVal;
31 typedef vector<GenericValue> ValuePlaneTy;
33 // ExecutionContext struct - This struct represents one stack frame currently
36 struct ExecutionContext {
37 Method *CurMethod; // The currently executing method
38 BasicBlock *CurBB; // The currently executing BB
39 BasicBlock::iterator CurInst; // The next instruction to execute
40 MethodInfo *MethInfo; // The MethInfo annotation for the method
41 vector<ValuePlaneTy> Values; // ValuePlanes for each type
43 BasicBlock *PrevBB; // The previous BB or null if in first BB
44 CallInst *Caller; // Holds the call that called subframes.
45 // NULL if main func or debugger invoked fn
49 // Interpreter - This class represents the entirety of the interpreter.
52 Module *CurMod; // The current Module being executed (0 if none)
53 int ExitCode; // The exit code to be returned by the lli util
54 bool Profile; // Profiling enabled?
55 int CurFrame; // The current stack frame being inspected
57 // The runtime stack of executing code. The top of the stack is the current
59 vector<ExecutionContext> ECStack;
63 inline ~Interpreter() { delete CurMod; }
65 // getExitCode - return the code that should be the exit code for the lli
67 inline int getExitCode() const { return ExitCode; }
69 // enableProfiling() - Turn profiling on, clear stats?
70 void enableProfiling() { Profile = true; }
72 void initializeExecutionEngine();
73 void handleUserInput();
75 // User Interation Methods...
76 bool callMethod(const string &Name); // return true on failure
77 void setBreakpoint(const string &Name);
78 void printValue(const string &Name);
79 void printValue(const Type *Ty, GenericValue V);
82 void list(); // Do the 'list' command
83 void printStackTrace(); // Do the 'backtrace' command
85 // Code execution methods...
86 void callMethod(Method *Meth, ExecutionContext *SF = 0);
87 bool executeInstruction(); // Execute one instruction...
89 void stepInstruction(); // Do the 'step' command
90 void nextInstruction(); // Do the 'next' command
91 void run(); // Do the 'run' command
92 void finish(); // Do the 'finish' command
94 // Opcode Implementations
95 void executeCallInst(CallInst *I, ExecutionContext &SF);
96 void executeRetInst(ReturnInst *I, ExecutionContext &SF);
97 void executeBrInst(BranchInst *I, ExecutionContext &SF);
99 // getCurrentMethod - Return the currently executing method
100 inline Method *getCurrentMethod() const {
101 return CurFrame < 0 ? 0 : ECStack[CurFrame].CurMethod;
104 // isStopped - Return true if a program is stopped. Return false if no
105 // program is running.
107 inline bool isStopped() const { return !ECStack.empty(); }
109 private: // Helper functions
110 // printCurrentInstruction - Print out the instruction that the virtual PC is
111 // at, or fail silently if no program is running.
113 void printCurrentInstruction();
115 // LookupMatchingNames - Search the current method namespace, then the global
116 // namespace looking for values that match the specified name. Return ALL
117 // matches to that name. This is obviously slow, and should only be used for
120 vector<Value*> LookupMatchingNames(const string &Name);
122 // ChooseOneOption - Prompt the user to choose among the specified options to
123 // pick one value. If no options are provided, emit an error. If a single
124 // option is provided, just return that option.
126 Value *ChooseOneOption(const string &Name, const vector<Value*> &Opts);