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 "llvm/BasicBlock.h"
17 #include "Support/DataTypes.h"
18 #include "llvm/Assembly/CachedWriter.h"
20 extern CachedWriter CW; // Object to accellerate printing of LLVM
22 struct MethodInfo; // Defined in ExecutionAnnotations.h
28 typedef uint64_t PointerTy;
32 unsigned char UByteVal;
34 unsigned short UShortVal;
35 signed short ShortVal;
45 // AllocaHolder - Object to track all of the blocks of memory allocated by
46 // alloca. When the function returns, this object is poped off the execution
47 // stack, which causes the dtor to be run, which frees all the alloca'd memory.
50 friend class AllocaHolderHandle;
51 std::vector<void*> Allocations;
54 AllocaHolder() : RefCnt(0) {}
55 void add(void *mem) { Allocations.push_back(mem); }
57 for (unsigned i = 0; i < Allocations.size(); ++i)
62 // AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into
65 class AllocaHolderHandle {
68 AllocaHolderHandle() : H(new AllocaHolder()) { H->RefCnt++; }
69 AllocaHolderHandle(const AllocaHolderHandle &AH) : H(AH.H) { H->RefCnt++; }
70 ~AllocaHolderHandle() { if (--H->RefCnt == 0) delete H; }
72 void add(void *mem) { H->add(mem); }
75 typedef std::vector<GenericValue> ValuePlaneTy;
77 // ExecutionContext struct - This struct represents one stack frame currently
80 struct ExecutionContext {
81 Method *CurMethod; // The currently executing method
82 BasicBlock *CurBB; // The currently executing BB
83 BasicBlock::iterator CurInst; // The next instruction to execute
84 MethodInfo *MethInfo; // The MethInfo annotation for the method
85 std::vector<ValuePlaneTy> Values;// ValuePlanes for each type
87 BasicBlock *PrevBB; // The previous BB or null if in first BB
88 CallInst *Caller; // Holds the call that called subframes.
89 // NULL if main func or debugger invoked fn
90 AllocaHolderHandle Allocas; // Track memory allocated by alloca
93 // Interpreter - This class represents the entirety of the interpreter.
96 Module *CurMod; // The current Module being executed (0 if none)
97 int ExitCode; // The exit code to be returned by the lli util
98 bool Profile; // Profiling enabled?
99 bool Trace; // Tracing enabled?
100 int CurFrame; // The current stack frame being inspected
102 // The runtime stack of executing code. The top of the stack is the current
104 std::vector<ExecutionContext> ECStack;
108 inline ~Interpreter() { CW.setModule(0); delete CurMod; }
110 // getExitCode - return the code that should be the exit code for the lli
112 inline int getExitCode() const { return ExitCode; }
114 // enableProfiling() - Turn profiling on, clear stats?
115 void enableProfiling() { Profile = true; }
116 void enableTracing() { Trace = true; }
118 void handleUserInput();
120 // User Interation Methods...
121 void loadModule(const std::string &Filename);
123 bool callMethod(const std::string &Name); // return true on failure
124 void setBreakpoint(const std::string &Name);
125 void infoValue(const std::string &Name);
126 void print(const std::string &Name);
127 static void print(const Type *Ty, GenericValue V);
128 static void printValue(const Type *Ty, GenericValue V);
130 // Hack until we can parse command line args...
131 bool callMainMethod(const std::string &MainName,
132 const std::vector<std::string> &InputFilename);
134 void list(); // Do the 'list' command
135 void printStackTrace(); // Do the 'backtrace' command
137 // Code execution methods...
138 void callMethod(Method *Meth, const std::vector<GenericValue> &ArgVals);
139 bool executeInstruction(); // Execute one instruction...
141 void stepInstruction(); // Do the 'step' command
142 void nextInstruction(); // Do the 'next' command
143 void run(); // Do the 'run' command
144 void finish(); // Do the 'finish' command
146 // Opcode Implementations
147 void executeCallInst(CallInst *I, ExecutionContext &SF);
148 void executeRetInst(ReturnInst *I, ExecutionContext &SF);
149 void executeBrInst(BranchInst *I, ExecutionContext &SF);
150 void executeAllocInst(AllocationInst *I, ExecutionContext &SF);
151 GenericValue callExternalMethod(Method *Meth,
152 const std::vector<GenericValue> &ArgVals);
153 void exitCalled(GenericValue GV);
155 // getCurrentMethod - Return the currently executing method
156 inline Method *getCurrentMethod() const {
157 return CurFrame < 0 ? 0 : ECStack[CurFrame].CurMethod;
160 // isStopped - Return true if a program is stopped. Return false if no
161 // program is running.
163 inline bool isStopped() const { return !ECStack.empty(); }
165 private: // Helper functions
166 // getCurrentExecutablePath() - Return the directory that the lli executable
169 std::string getCurrentExecutablePath() const;
171 // printCurrentInstruction - Print out the instruction that the virtual PC is
172 // at, or fail silently if no program is running.
174 void printCurrentInstruction();
176 // printStackFrame - Print information about the specified stack frame, or -1
177 // for the default one.
179 void printStackFrame(int FrameNo = -1);
181 // LookupMatchingNames - Search the current method namespace, then the global
182 // namespace looking for values that match the specified name. Return ALL
183 // matches to that name. This is obviously slow, and should only be used for
186 std::vector<Value*> LookupMatchingNames(const std::string &Name);
188 // ChooseOneOption - Prompt the user to choose among the specified options to
189 // pick one value. If no options are provided, emit an error. If a single
190 // option is provided, just return that option.
192 Value *ChooseOneOption(const std::string &Name,
193 const std::vector<Value*> &Opts);
196 void initializeExecutionEngine();
197 void initializeExternalMethods();