1 //===-- Interpreter.h ------------------------------------------*- C++ -*--===//
3 // This header file defines the interpreter structure
5 //===----------------------------------------------------------------------===//
7 #ifndef LLI_INTERPRETER_H
8 #define LLI_INTERPRETER_H
10 // Uncomment this line to enable profiling of structure field accesses.
11 #define PROFILE_STRUCTURE_FIELDS 1
14 #include "llvm/Module.h"
15 #include "llvm/Method.h"
16 #include "Support/DataTypes.h"
17 #include "llvm/Assembly/CachedWriter.h"
19 extern CachedWriter CW; // Object to accellerate printing of LLVM
21 struct MethodInfo; // Defined in ExecutionAnnotations.h
27 typedef uint64_t PointerTy;
31 unsigned char UByteVal;
33 unsigned short UShortVal;
34 signed short ShortVal;
44 typedef vector<GenericValue> ValuePlaneTy;
46 // ExecutionContext struct - This struct represents one stack frame currently
49 struct ExecutionContext {
50 Method *CurMethod; // The currently executing method
51 BasicBlock *CurBB; // The currently executing BB
52 BasicBlock::iterator CurInst; // The next instruction to execute
53 MethodInfo *MethInfo; // The MethInfo annotation for the method
54 vector<ValuePlaneTy> Values; // ValuePlanes for each type
56 BasicBlock *PrevBB; // The previous BB or null if in first BB
57 CallInst *Caller; // Holds the call that called subframes.
58 // NULL if main func or debugger invoked fn
61 // Interpreter - This class represents the entirety of the interpreter.
64 Module *CurMod; // The current Module being executed (0 if none)
65 int ExitCode; // The exit code to be returned by the lli util
66 bool Profile; // Profiling enabled?
67 bool Trace; // Tracing enabled?
68 int CurFrame; // The current stack frame being inspected
70 // The runtime stack of executing code. The top of the stack is the current
72 vector<ExecutionContext> ECStack;
76 inline ~Interpreter() { CW.setModule(0); delete CurMod; }
78 // getExitCode - return the code that should be the exit code for the lli
80 inline int getExitCode() const { return ExitCode; }
82 // enableProfiling() - Turn profiling on, clear stats?
83 void enableProfiling() { Profile = true; }
84 void enableTracing() { Trace = true; }
86 void handleUserInput();
88 // User Interation Methods...
89 void loadModule(const string &Filename);
91 bool callMethod(const string &Name); // return true on failure
92 void setBreakpoint(const string &Name);
93 void infoValue(const string &Name);
94 void print(const string &Name);
95 static void print(const Type *Ty, GenericValue V);
96 static void printValue(const Type *Ty, GenericValue V);
98 // Hack until we can parse command line args...
99 bool callMainMethod(const string &MainName,
100 const vector<string> &InputFilename);
102 void list(); // Do the 'list' command
103 void printStackTrace(); // Do the 'backtrace' command
105 // Code execution methods...
106 void callMethod (Method *Meth, const vector<GenericValue> &ArgVals);
107 bool executeInstruction(); // Execute one instruction...
109 void stepInstruction(); // Do the 'step' command
110 void nextInstruction(); // Do the 'next' command
111 void run(); // Do the 'run' command
112 void finish(); // Do the 'finish' command
114 // Opcode Implementations
115 void executeCallInst(CallInst *I, ExecutionContext &SF);
116 void executeRetInst(ReturnInst *I, ExecutionContext &SF);
117 void executeBrInst(BranchInst *I, ExecutionContext &SF);
118 void executeAllocInst(AllocationInst *I, ExecutionContext &SF);
119 GenericValue callExternalMethod(Method *Meth,
120 const vector<GenericValue> &ArgVals);
121 void exitCalled(GenericValue GV);
123 // getCurrentMethod - Return the currently executing method
124 inline Method *getCurrentMethod() const {
125 return CurFrame < 0 ? 0 : ECStack[CurFrame].CurMethod;
128 // isStopped - Return true if a program is stopped. Return false if no
129 // program is running.
131 inline bool isStopped() const { return !ECStack.empty(); }
133 private: // Helper functions
134 // getCurrentExecutablePath() - Return the directory that the lli executable
137 string getCurrentExecutablePath() const;
139 // printCurrentInstruction - Print out the instruction that the virtual PC is
140 // at, or fail silently if no program is running.
142 void printCurrentInstruction();
144 // printStackFrame - Print information about the specified stack frame, or -1
145 // for the default one.
147 void printStackFrame(int FrameNo = -1);
149 // LookupMatchingNames - Search the current method namespace, then the global
150 // namespace looking for values that match the specified name. Return ALL
151 // matches to that name. This is obviously slow, and should only be used for
154 vector<Value*> LookupMatchingNames(const string &Name);
156 // ChooseOneOption - Prompt the user to choose among the specified options to
157 // pick one value. If no options are provided, emit an error. If a single
158 // option is provided, just return that option.
160 Value *ChooseOneOption(const string &Name, const vector<Value*> &Opts);
163 void initializeExecutionEngine();
164 void initializeExternalMethods();