Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1 | //===-- Interpreter.h ------------------------------------------*- C++ -*--===// |
| 2 | // |
| 3 | // This header file defines the interpreter structure |
| 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
| 7 | #ifndef LLI_INTERPRETER_H |
| 8 | #define LLI_INTERPRETER_H |
| 9 | |
Chris Lattner | e240906 | 2001-11-12 16:19:45 +0000 | [diff] [blame] | 10 | // Uncomment this line to enable profiling of structure field accesses. |
Chris Lattner | 849735c | 2002-10-02 21:11:16 +0000 | [diff] [blame] | 11 | //#define PROFILE_STRUCTURE_FIELDS 1 |
Chris Lattner | e240906 | 2001-11-12 16:19:45 +0000 | [diff] [blame] | 12 | |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 13 | #include "../ExecutionEngine.h" |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 14 | #include "../GenericValue.h" |
Chris Lattner | 360e17e | 2001-11-26 23:04:08 +0000 | [diff] [blame] | 15 | #include "Support/DataTypes.h" |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 16 | #include "llvm/Assembly/CachedWriter.h" |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 17 | #include "llvm/Target/TargetData.h" |
| 18 | #include "llvm/BasicBlock.h" |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 19 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 20 | |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 21 | extern CachedWriter CW; // Object to accelerate printing of LLVM |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 22 | |
Chris Lattner | da82ed5 | 2003-05-08 16:18:31 +0000 | [diff] [blame] | 23 | struct FunctionInfo; // Defined in ExecutionAnnotations.h |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 9bffa73 | 2002-02-19 18:50:09 +0000 | [diff] [blame] | 25 | // AllocaHolder - Object to track all of the blocks of memory allocated by |
| 26 | // alloca. When the function returns, this object is poped off the execution |
| 27 | // stack, which causes the dtor to be run, which frees all the alloca'd memory. |
| 28 | // |
| 29 | class AllocaHolder { |
| 30 | friend class AllocaHolderHandle; |
| 31 | std::vector<void*> Allocations; |
| 32 | unsigned RefCnt; |
| 33 | public: |
| 34 | AllocaHolder() : RefCnt(0) {} |
| 35 | void add(void *mem) { Allocations.push_back(mem); } |
| 36 | ~AllocaHolder() { |
| 37 | for (unsigned i = 0; i < Allocations.size(); ++i) |
| 38 | free(Allocations[i]); |
| 39 | } |
| 40 | }; |
| 41 | |
| 42 | // AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into |
| 43 | // a vector... |
| 44 | // |
| 45 | class AllocaHolderHandle { |
| 46 | AllocaHolder *H; |
| 47 | public: |
| 48 | AllocaHolderHandle() : H(new AllocaHolder()) { H->RefCnt++; } |
| 49 | AllocaHolderHandle(const AllocaHolderHandle &AH) : H(AH.H) { H->RefCnt++; } |
| 50 | ~AllocaHolderHandle() { if (--H->RefCnt == 0) delete H; } |
| 51 | |
| 52 | void add(void *mem) { H->add(mem); } |
| 53 | }; |
| 54 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 55 | typedef std::vector<GenericValue> ValuePlaneTy; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 56 | |
| 57 | // ExecutionContext struct - This struct represents one stack frame currently |
| 58 | // executing. |
| 59 | // |
| 60 | struct ExecutionContext { |
Chris Lattner | da82ed5 | 2003-05-08 16:18:31 +0000 | [diff] [blame] | 61 | Function *CurFunction;// The currently executing function |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 62 | BasicBlock *CurBB; // The currently executing BB |
| 63 | BasicBlock::iterator CurInst; // The next instruction to execute |
Chris Lattner | da82ed5 | 2003-05-08 16:18:31 +0000 | [diff] [blame] | 64 | FunctionInfo *FuncInfo; // The FuncInfo annotation for the function |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 65 | std::vector<ValuePlaneTy> Values;// ValuePlanes for each type |
Chris Lattner | cdf5178 | 2003-05-08 16:06:52 +0000 | [diff] [blame] | 66 | std::vector<GenericValue> VarArgs; // Values passed through an ellipsis |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 67 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 68 | CallInst *Caller; // Holds the call that called subframes. |
| 69 | // NULL if main func or debugger invoked fn |
Chris Lattner | 9bffa73 | 2002-02-19 18:50:09 +0000 | [diff] [blame] | 70 | AllocaHolderHandle Allocas; // Track memory allocated by alloca |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 73 | // Interpreter - This class represents the entirety of the interpreter. |
| 74 | // |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 75 | class Interpreter : public ExecutionEngine, public InstVisitor<Interpreter> { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 76 | int ExitCode; // The exit code to be returned by the lli util |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 77 | bool Debug; // Debug mode enabled? |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 78 | bool Profile; // Profiling enabled? |
Chris Lattner | 43e3f7c | 2001-10-27 08:43:52 +0000 | [diff] [blame] | 79 | bool Trace; // Tracing enabled? |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 80 | int CurFrame; // The current stack frame being inspected |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 81 | TargetData TD; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 82 | |
| 83 | // The runtime stack of executing code. The top of the stack is the current |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 84 | // function record. |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 85 | std::vector<ExecutionContext> ECStack; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 86 | |
Chris Lattner | 44edb6b | 2003-05-14 14:21:30 +0000 | [diff] [blame] | 87 | // AtExitHandlers - List of functions to call when the program exits. |
| 88 | std::vector<Function*> AtExitHandlers; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 89 | public: |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 90 | Interpreter(Module *M, unsigned Config, bool DebugMode, bool TraceMode); |
| 91 | inline ~Interpreter() { CW.setModule(0); } |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 92 | |
| 93 | // getExitCode - return the code that should be the exit code for the lli |
| 94 | // utility. |
| 95 | inline int getExitCode() const { return ExitCode; } |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 96 | |
| 97 | /// run - Start execution with the specified function and arguments. |
| 98 | /// |
| 99 | virtual int run(const std::string &FnName, |
| 100 | const std::vector<std::string> &Args); |
| 101 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 102 | |
| 103 | // enableProfiling() - Turn profiling on, clear stats? |
| 104 | void enableProfiling() { Profile = true; } |
Chris Lattner | 43e3f7c | 2001-10-27 08:43:52 +0000 | [diff] [blame] | 105 | void enableTracing() { Trace = true; } |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 107 | void handleUserInput(); |
| 108 | |
| 109 | // User Interation Methods... |
Chris Lattner | da82ed5 | 2003-05-08 16:18:31 +0000 | [diff] [blame] | 110 | bool callFunction(const std::string &Name); // return true on failure |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 111 | void setBreakpoint(const std::string &Name); |
| 112 | void infoValue(const std::string &Name); |
| 113 | void print(const std::string &Name); |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 114 | static void print(const Type *Ty, GenericValue V); |
Chris Lattner | 365a76e | 2001-09-10 04:49:44 +0000 | [diff] [blame] | 115 | static void printValue(const Type *Ty, GenericValue V); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 116 | |
Chris Lattner | da82ed5 | 2003-05-08 16:18:31 +0000 | [diff] [blame] | 117 | bool callMainFunction(const std::string &MainName, |
| 118 | const std::vector<std::string> &InputFilename); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 119 | |
| 120 | void list(); // Do the 'list' command |
| 121 | void printStackTrace(); // Do the 'backtrace' command |
| 122 | |
| 123 | // Code execution methods... |
Chris Lattner | da82ed5 | 2003-05-08 16:18:31 +0000 | [diff] [blame] | 124 | void callFunction(Function *F, const std::vector<GenericValue> &ArgVals); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 125 | bool executeInstruction(); // Execute one instruction... |
| 126 | |
| 127 | void stepInstruction(); // Do the 'step' command |
| 128 | void nextInstruction(); // Do the 'next' command |
| 129 | void run(); // Do the 'run' command |
| 130 | void finish(); // Do the 'finish' command |
| 131 | |
| 132 | // Opcode Implementations |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 133 | void visitReturnInst(ReturnInst &I); |
| 134 | void visitBranchInst(BranchInst &I); |
| 135 | void visitSwitchInst(SwitchInst &I); |
| 136 | |
| 137 | void visitBinaryOperator(BinaryOperator &I); |
| 138 | void visitAllocationInst(AllocationInst &I); |
| 139 | void visitFreeInst(FreeInst &I); |
| 140 | void visitLoadInst(LoadInst &I); |
| 141 | void visitStoreInst(StoreInst &I); |
| 142 | void visitGetElementPtrInst(GetElementPtrInst &I); |
| 143 | |
| 144 | void visitPHINode(PHINode &PN) { assert(0 && "PHI nodes already handled!"); } |
| 145 | void visitCastInst(CastInst &I); |
| 146 | void visitCallInst(CallInst &I); |
| 147 | void visitShl(ShiftInst &I); |
| 148 | void visitShr(ShiftInst &I); |
| 149 | void visitVarArgInst(VarArgInst &I); |
| 150 | void visitInstruction(Instruction &I) { |
| 151 | std::cerr << I; |
| 152 | assert(0 && "Instruction not interpretable yet!"); |
| 153 | } |
| 154 | |
Chris Lattner | da82ed5 | 2003-05-08 16:18:31 +0000 | [diff] [blame] | 155 | GenericValue callExternalFunction(Function *F, |
| 156 | const std::vector<GenericValue> &ArgVals); |
Chris Lattner | e43db88 | 2001-10-27 04:15:57 +0000 | [diff] [blame] | 157 | void exitCalled(GenericValue GV); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 158 | |
Chris Lattner | da82ed5 | 2003-05-08 16:18:31 +0000 | [diff] [blame] | 159 | // getCurrentFunction - Return the currently executing function |
| 160 | inline Function *getCurrentFunction() const { |
| 161 | return CurFrame < 0 ? 0 : ECStack[CurFrame].CurFunction; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | // isStopped - Return true if a program is stopped. Return false if no |
| 165 | // program is running. |
| 166 | // |
| 167 | inline bool isStopped() const { return !ECStack.empty(); } |
| 168 | |
Chris Lattner | 44edb6b | 2003-05-14 14:21:30 +0000 | [diff] [blame] | 169 | void addAtExitHandler(Function *F) { |
| 170 | AtExitHandlers.push_back(F); |
| 171 | } |
| 172 | |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 173 | //FIXME: private: |
| 174 | public: |
| 175 | GenericValue executeGEPOperation(Value *Ptr, User::op_iterator I, |
| 176 | User::op_iterator E, ExecutionContext &SF); |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 177 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 178 | private: // Helper functions |
Chris Lattner | 77113b6 | 2003-05-10 20:21:16 +0000 | [diff] [blame] | 179 | // SwitchToNewBasicBlock - Start execution in a new basic block and run any |
| 180 | // PHI nodes in the top of the block. This is used for intraprocedural |
| 181 | // control flow. |
| 182 | // |
| 183 | void SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF); |
| 184 | |
Brian Gaeke | fb0ef2e | 2003-08-13 18:17:54 +0000 | [diff] [blame^] | 185 | void *getPointerToFunction(Function *F) { return (void*)F; } |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 186 | |
Chris Lattner | e43db88 | 2001-10-27 04:15:57 +0000 | [diff] [blame] | 187 | // getCurrentExecutablePath() - Return the directory that the lli executable |
| 188 | // lives in. |
| 189 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 190 | std::string getCurrentExecutablePath() const; |
Chris Lattner | e43db88 | 2001-10-27 04:15:57 +0000 | [diff] [blame] | 191 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 192 | // printCurrentInstruction - Print out the instruction that the virtual PC is |
| 193 | // at, or fail silently if no program is running. |
| 194 | // |
| 195 | void printCurrentInstruction(); |
| 196 | |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 197 | // printStackFrame - Print information about the specified stack frame, or -1 |
| 198 | // for the default one. |
| 199 | // |
| 200 | void printStackFrame(int FrameNo = -1); |
| 201 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 202 | // LookupMatchingNames - Search the current function namespace, then the |
| 203 | // global namespace looking for values that match the specified name. Return |
| 204 | // ALL matches to that name. This is obviously slow, and should only be used |
| 205 | // for user interaction. |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 206 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 207 | std::vector<Value*> LookupMatchingNames(const std::string &Name); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 208 | |
| 209 | // ChooseOneOption - Prompt the user to choose among the specified options to |
| 210 | // pick one value. If no options are provided, emit an error. If a single |
| 211 | // option is provided, just return that option. |
| 212 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 213 | Value *ChooseOneOption(const std::string &Name, |
| 214 | const std::vector<Value*> &Opts); |
Chris Lattner | 5deea3c | 2001-10-30 20:28:23 +0000 | [diff] [blame] | 215 | |
Chris Lattner | 44edb6b | 2003-05-14 14:21:30 +0000 | [diff] [blame] | 216 | // PerformExitStuff - Print out counters and profiling information if |
| 217 | // applicable... |
| 218 | void PerformExitStuff(); |
Chris Lattner | 5deea3c | 2001-10-30 20:28:23 +0000 | [diff] [blame] | 219 | |
| 220 | void initializeExecutionEngine(); |
Chris Lattner | da82ed5 | 2003-05-08 16:18:31 +0000 | [diff] [blame] | 221 | void initializeExternalFunctions(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 222 | }; |
| 223 | |
| 224 | #endif |