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 "../ExecutionEngine.h"
11 #include "../GenericValue.h"
12 #include "Support/DataTypes.h"
13 #include "llvm/Assembly/CachedWriter.h"
14 #include "llvm/Target/TargetData.h"
15 #include "llvm/BasicBlock.h"
16 #include "llvm/Support/InstVisitor.h"
18 extern CachedWriter CW; // Object to accelerate printing of LLVM
20 struct FunctionInfo; // Defined in ExecutionAnnotations.h
22 // AllocaHolder - Object to track all of the blocks of memory allocated by
23 // alloca. When the function returns, this object is poped off the execution
24 // stack, which causes the dtor to be run, which frees all the alloca'd memory.
27 friend class AllocaHolderHandle;
28 std::vector<void*> Allocations;
31 AllocaHolder() : RefCnt(0) {}
32 void add(void *mem) { Allocations.push_back(mem); }
34 for (unsigned i = 0; i < Allocations.size(); ++i)
39 // AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into
42 class AllocaHolderHandle {
45 AllocaHolderHandle() : H(new AllocaHolder()) { H->RefCnt++; }
46 AllocaHolderHandle(const AllocaHolderHandle &AH) : H(AH.H) { H->RefCnt++; }
47 ~AllocaHolderHandle() { if (--H->RefCnt == 0) delete H; }
49 void add(void *mem) { H->add(mem); }
52 typedef std::vector<GenericValue> ValuePlaneTy;
54 // ExecutionContext struct - This struct represents one stack frame currently
57 struct ExecutionContext {
58 Function *CurFunction;// The currently executing function
59 BasicBlock *CurBB; // The currently executing BB
60 BasicBlock::iterator CurInst; // The next instruction to execute
61 FunctionInfo *FuncInfo; // The FuncInfo annotation for the function
62 std::vector<ValuePlaneTy> Values;// ValuePlanes for each type
63 std::vector<GenericValue> VarArgs; // Values passed through an ellipsis
65 CallInst *Caller; // Holds the call that called subframes.
66 // NULL if main func or debugger invoked fn
67 AllocaHolderHandle Allocas; // Track memory allocated by alloca
70 // Interpreter - This class represents the entirety of the interpreter.
72 class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> {
73 int ExitCode; // The exit code to be returned by the lli util
74 bool Profile; // Profiling enabled?
75 bool Trace; // Tracing enabled?
76 int CurFrame; // The current stack frame being inspected
79 // The runtime stack of executing code. The top of the stack is the current
81 std::vector<ExecutionContext> ECStack;
83 // AtExitHandlers - List of functions to call when the program exits.
84 std::vector<Function*> AtExitHandlers;
86 Interpreter(Module *M, bool isLittleEndian, bool isLongPointer,
88 inline ~Interpreter() { CW.setModule(0); }
90 /// create - Create an interpreter ExecutionEngine. This can never fail.
92 static ExecutionEngine *create(Module *M, bool TraceMode);
94 /// getExitCode - return the code that should be the exit code for the lli
97 inline int getExitCode() const { return ExitCode; }
99 /// run - Start execution with the specified function and arguments.
101 virtual int run(const std::string &FnName,
102 const std::vector<std::string> &Args,
106 // enableProfiling() - Turn profiling on, clear stats?
107 void enableProfiling() { Profile = true; }
108 void enableTracing() { Trace = true; }
110 void handleUserInput();
112 // User Interation Methods...
113 bool callFunction(const std::string &Name); // return true on failure
114 void setBreakpoint(const std::string &Name);
115 void infoValue(const std::string &Name);
116 void print(const std::string &Name);
117 static void print(const Type *Ty, GenericValue V);
118 static void printValue(const Type *Ty, GenericValue V);
120 bool callMainFunction(const std::string &MainName,
121 const std::vector<std::string> &InputFilename);
123 void list(); // Do the 'list' command
124 void printStackTrace(); // Do the 'backtrace' command
126 // Code execution methods...
127 void callFunction(Function *F, const std::vector<GenericValue> &ArgVals);
128 bool executeInstruction(); // Execute one instruction...
130 void stepInstruction(); // Do the 'step' command
131 void nextInstruction(); // Do the 'next' command
132 void run(); // Do the 'run' command
133 void finish(); // Do the 'finish' command
135 // Opcode Implementations
136 void visitReturnInst(ReturnInst &I);
137 void visitBranchInst(BranchInst &I);
138 void visitSwitchInst(SwitchInst &I);
140 void visitBinaryOperator(BinaryOperator &I);
141 void visitAllocationInst(AllocationInst &I);
142 void visitFreeInst(FreeInst &I);
143 void visitLoadInst(LoadInst &I);
144 void visitStoreInst(StoreInst &I);
145 void visitGetElementPtrInst(GetElementPtrInst &I);
147 void visitPHINode(PHINode &PN) { assert(0 && "PHI nodes already handled!"); }
148 void visitCastInst(CastInst &I);
149 void visitCallInst(CallInst &I);
150 void visitShl(ShiftInst &I);
151 void visitShr(ShiftInst &I);
152 void visitVarArgInst(VarArgInst &I);
153 void visitInstruction(Instruction &I) {
155 assert(0 && "Instruction not interpretable yet!");
158 GenericValue callExternalFunction(Function *F,
159 const std::vector<GenericValue> &ArgVals);
160 void exitCalled(GenericValue GV);
162 // getCurrentFunction - Return the currently executing function
163 inline Function *getCurrentFunction() const {
164 return CurFrame < 0 ? 0 : ECStack[CurFrame].CurFunction;
167 // isStopped - Return true if a program is stopped. Return false if no
168 // program is running.
170 inline bool isStopped() const { return !ECStack.empty(); }
172 void addAtExitHandler(Function *F) {
173 AtExitHandlers.push_back(F);
178 GenericValue executeGEPOperation(Value *Ptr, User::op_iterator I,
179 User::op_iterator E, ExecutionContext &SF);
181 private: // Helper functions
182 // SwitchToNewBasicBlock - Start execution in a new basic block and run any
183 // PHI nodes in the top of the block. This is used for intraprocedural
186 void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF);
188 void *getPointerToFunction(Function *F) { return (void*)F; }
190 // getCurrentExecutablePath() - Return the directory that the lli executable
193 std::string getCurrentExecutablePath() const;
195 // printCurrentInstruction - Print out the instruction that the virtual PC is
196 // at, or fail silently if no program is running.
198 void printCurrentInstruction();
200 // printStackFrame - Print information about the specified stack frame, or -1
201 // for the default one.
203 void printStackFrame(int FrameNo = -1);
205 // LookupMatchingNames - Search the current function namespace, then the
206 // global namespace looking for values that match the specified name. Return
207 // ALL matches to that name. This is obviously slow, and should only be used
208 // for user interaction.
210 std::vector<Value*> LookupMatchingNames(const std::string &Name);
212 // ChooseOneOption - Prompt the user to choose among the specified options to
213 // pick one value. If no options are provided, emit an error. If a single
214 // option is provided, just return that option.
216 Value *ChooseOneOption(const std::string &Name,
217 const std::vector<Value*> &Opts);
219 void initializeExecutionEngine();
220 void initializeExternalFunctions();