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"
12 #include "llvm/Support/DataTypes.h"
14 struct MethodInfo; // Defined in ExecutionAnnotations.h
22 unsigned char UByteVal;
24 unsigned short UShortVal;
25 signed short ShortVal;
35 typedef vector<GenericValue> ValuePlaneTy;
37 // ExecutionContext struct - This struct represents one stack frame currently
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
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
53 // Interpreter - This class represents the entirety of the 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 bool Trace; // Tracing enabled?
60 int CurFrame; // The current stack frame being inspected
62 // The runtime stack of executing code. The top of the stack is the current
64 vector<ExecutionContext> ECStack;
68 inline ~Interpreter() { delete CurMod; }
70 // getExitCode - return the code that should be the exit code for the lli
72 inline int getExitCode() const { return ExitCode; }
74 // enableProfiling() - Turn profiling on, clear stats?
75 void enableProfiling() { Profile = true; }
76 void enableTracing() { Trace = true; }
78 void initializeExecutionEngine();
79 void handleUserInput();
81 // User Interation Methods...
82 void loadModule(const string &Filename);
84 bool callMethod(const string &Name); // return true on failure
85 void setBreakpoint(const string &Name);
86 void infoValue(const string &Name);
87 void print(const string &Name);
88 static void print(const Type *Ty, GenericValue V);
89 static void printValue(const Type *Ty, GenericValue V);
91 // Hack until we can parse command line args...
92 bool callMainMethod(const string &MainName,
93 const vector<string> &InputFilename);
95 void list(); // Do the 'list' command
96 void printStackTrace(); // Do the 'backtrace' command
98 // Code execution methods...
99 void callMethod (Method *Meth, const vector<GenericValue> &ArgVals);
100 void callExternalMethod(Method *Meth, const vector<GenericValue> &ArgVals);
101 bool executeInstruction(); // Execute one instruction...
103 void stepInstruction(); // Do the 'step' command
104 void nextInstruction(); // Do the 'next' command
105 void run(); // Do the 'run' command
106 void finish(); // Do the 'finish' command
108 // Opcode Implementations
109 void executeCallInst(CallInst *I, ExecutionContext &SF);
110 void executeRetInst(ReturnInst *I, ExecutionContext &SF);
111 void executeBrInst(BranchInst *I, ExecutionContext &SF);
112 void executeAllocInst(AllocationInst *I, ExecutionContext &SF);
113 void exitCalled(GenericValue GV);
115 // getCurrentMethod - Return the currently executing method
116 inline Method *getCurrentMethod() const {
117 return CurFrame < 0 ? 0 : ECStack[CurFrame].CurMethod;
120 // isStopped - Return true if a program is stopped. Return false if no
121 // program is running.
123 inline bool isStopped() const { return !ECStack.empty(); }
125 private: // Helper functions
126 // getCurrentExecutablePath() - Return the directory that the lli executable
129 string getCurrentExecutablePath() const;
131 // printCurrentInstruction - Print out the instruction that the virtual PC is
132 // at, or fail silently if no program is running.
134 void printCurrentInstruction();
136 // LookupMatchingNames - Search the current method namespace, then the global
137 // namespace looking for values that match the specified name. Return ALL
138 // matches to that name. This is obviously slow, and should only be used for
141 vector<Value*> LookupMatchingNames(const string &Name);
143 // ChooseOneOption - Prompt the user to choose among the specified options to
144 // pick one value. If no options are provided, emit an error. If a single
145 // option is provided, just return that option.
147 Value *ChooseOneOption(const string &Name, const vector<Value*> &Opts);