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
13 #include "llvm/Module.h"
14 #include "Support/DataTypes.h"
15 #include "llvm/Assembly/CachedWriter.h"
17 extern CachedWriter CW; // Object to accellerate printing of LLVM
19 struct MethodInfo; // Defined in ExecutionAnnotations.h
25 typedef uint64_t PointerTy;
29 unsigned char UByteVal;
31 unsigned short UShortVal;
32 signed short ShortVal;
40 unsigned char Untyped[8];
43 // AllocaHolder - Object to track all of the blocks of memory allocated by
44 // alloca. When the function returns, this object is poped off the execution
45 // stack, which causes the dtor to be run, which frees all the alloca'd memory.
48 friend class AllocaHolderHandle;
49 std::vector<void*> Allocations;
52 AllocaHolder() : RefCnt(0) {}
53 void add(void *mem) { Allocations.push_back(mem); }
55 for (unsigned i = 0; i < Allocations.size(); ++i)
60 // AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into
63 class AllocaHolderHandle {
66 AllocaHolderHandle() : H(new AllocaHolder()) { H->RefCnt++; }
67 AllocaHolderHandle(const AllocaHolderHandle &AH) : H(AH.H) { H->RefCnt++; }
68 ~AllocaHolderHandle() { if (--H->RefCnt == 0) delete H; }
70 void add(void *mem) { H->add(mem); }
73 typedef std::vector<GenericValue> ValuePlaneTy;
75 // ExecutionContext struct - This struct represents one stack frame currently
78 struct ExecutionContext {
79 Function *CurMethod; // The currently executing function
80 BasicBlock *CurBB; // The currently executing BB
81 BasicBlock::iterator CurInst; // The next instruction to execute
82 MethodInfo *MethInfo; // The MethInfo annotation for the function
83 std::vector<ValuePlaneTy> Values;// ValuePlanes for each type
85 BasicBlock *PrevBB; // The previous BB or null if in first BB
86 CallInst *Caller; // Holds the call that called subframes.
87 // NULL if main func or debugger invoked fn
88 AllocaHolderHandle Allocas; // Track memory allocated by alloca
91 // Interpreter - This class represents the entirety of the interpreter.
94 Module *CurMod; // The current Module being executed (0 if none)
95 int ExitCode; // The exit code to be returned by the lli util
96 bool Profile; // Profiling enabled?
97 bool Trace; // Tracing enabled?
98 int CurFrame; // The current stack frame being inspected
100 // The runtime stack of executing code. The top of the stack is the current
102 std::vector<ExecutionContext> ECStack;
106 inline ~Interpreter() { CW.setModule(0); delete CurMod; }
108 // getExitCode - return the code that should be the exit code for the lli
110 inline int getExitCode() const { return ExitCode; }
111 inline Module *getModule() const { return CurMod; }
113 // enableProfiling() - Turn profiling on, clear stats?
114 void enableProfiling() { Profile = true; }
115 void enableTracing() { Trace = true; }
117 void handleUserInput();
119 // User Interation Methods...
120 void loadModule(const std::string &Filename);
122 bool callMethod(const std::string &Name); // return true on failure
123 void setBreakpoint(const std::string &Name);
124 void infoValue(const std::string &Name);
125 void print(const std::string &Name);
126 static void print(const Type *Ty, GenericValue V);
127 static void printValue(const Type *Ty, GenericValue V);
129 bool callMainMethod(const std::string &MainName,
130 const std::vector<std::string> &InputFilename);
131 GenericValue CreateArgv(const std::vector<std::string> &InputArgv);
133 void list(); // Do the 'list' command
134 void printStackTrace(); // Do the 'backtrace' command
136 // Code execution methods...
137 void callMethod(Function *F, const std::vector<GenericValue> &ArgVals);
138 bool executeInstruction(); // Execute one instruction...
140 void stepInstruction(); // Do the 'step' command
141 void nextInstruction(); // Do the 'next' command
142 void run(); // Do the 'run' command
143 void finish(); // Do the 'finish' command
145 // Opcode Implementations
146 void executeCallInst(CallInst &I, ExecutionContext &SF);
147 void executeRetInst(ReturnInst &I, ExecutionContext &SF);
148 void executeBrInst(BranchInst &I, ExecutionContext &SF);
149 void executeAllocInst(AllocationInst &I, ExecutionContext &SF);
150 GenericValue callExternalMethod(Function *F,
151 const std::vector<GenericValue> &ArgVals);
152 void exitCalled(GenericValue GV);
154 // getCurrentMethod - Return the currently executing method
155 inline Function *getCurrentMethod() const {
156 return CurFrame < 0 ? 0 : ECStack[CurFrame].CurMethod;
159 // isStopped - Return true if a program is stopped. Return false if no
160 // program is running.
162 inline bool isStopped() const { return !ECStack.empty(); }
164 private: // Helper functions
165 // getCurrentExecutablePath() - Return the directory that the lli executable
168 std::string getCurrentExecutablePath() const;
170 // printCurrentInstruction - Print out the instruction that the virtual PC is
171 // at, or fail silently if no program is running.
173 void printCurrentInstruction();
175 // printStackFrame - Print information about the specified stack frame, or -1
176 // for the default one.
178 void printStackFrame(int FrameNo = -1);
180 // LookupMatchingNames - Search the current function namespace, then the
181 // global namespace looking for values that match the specified name. Return
182 // ALL matches to that name. This is obviously slow, and should only be used
183 // for user interaction.
185 std::vector<Value*> LookupMatchingNames(const std::string &Name);
187 // ChooseOneOption - Prompt the user to choose among the specified options to
188 // pick one value. If no options are provided, emit an error. If a single
189 // option is provided, just return that option.
191 Value *ChooseOneOption(const std::string &Name,
192 const std::vector<Value*> &Opts);
195 void initializeExecutionEngine();
196 void initializeExternalMethods();