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 | 360e17e | 2001-11-26 23:04:08 +0000 | [diff] [blame] | 14 | #include "Support/DataTypes.h" |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 15 | #include "llvm/Assembly/CachedWriter.h" |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 16 | #include "llvm/Target/TargetData.h" |
| 17 | #include "llvm/BasicBlock.h" |
| 18 | #include "../GenericValue.h" |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 19 | |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 20 | extern CachedWriter CW; // Object to accelerate printing of LLVM |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 21 | |
| 22 | struct MethodInfo; // Defined in ExecutionAnnotations.h |
| 23 | class CallInst; |
| 24 | class ReturnInst; |
| 25 | class BranchInst; |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 26 | class LoadInst; |
| 27 | class StoreInst; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 28 | class AllocationInst; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 9bffa73 | 2002-02-19 18:50:09 +0000 | [diff] [blame] | 30 | // AllocaHolder - Object to track all of the blocks of memory allocated by |
| 31 | // alloca. When the function returns, this object is poped off the execution |
| 32 | // stack, which causes the dtor to be run, which frees all the alloca'd memory. |
| 33 | // |
| 34 | class AllocaHolder { |
| 35 | friend class AllocaHolderHandle; |
| 36 | std::vector<void*> Allocations; |
| 37 | unsigned RefCnt; |
| 38 | public: |
| 39 | AllocaHolder() : RefCnt(0) {} |
| 40 | void add(void *mem) { Allocations.push_back(mem); } |
| 41 | ~AllocaHolder() { |
| 42 | for (unsigned i = 0; i < Allocations.size(); ++i) |
| 43 | free(Allocations[i]); |
| 44 | } |
| 45 | }; |
| 46 | |
| 47 | // AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into |
| 48 | // a vector... |
| 49 | // |
| 50 | class AllocaHolderHandle { |
| 51 | AllocaHolder *H; |
| 52 | public: |
| 53 | AllocaHolderHandle() : H(new AllocaHolder()) { H->RefCnt++; } |
| 54 | AllocaHolderHandle(const AllocaHolderHandle &AH) : H(AH.H) { H->RefCnt++; } |
| 55 | ~AllocaHolderHandle() { if (--H->RefCnt == 0) delete H; } |
| 56 | |
| 57 | void add(void *mem) { H->add(mem); } |
| 58 | }; |
| 59 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 60 | typedef std::vector<GenericValue> ValuePlaneTy; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 61 | |
| 62 | // ExecutionContext struct - This struct represents one stack frame currently |
| 63 | // executing. |
| 64 | // |
| 65 | struct ExecutionContext { |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 66 | Function *CurMethod; // The currently executing function |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 67 | BasicBlock *CurBB; // The currently executing BB |
| 68 | BasicBlock::iterator CurInst; // The next instruction to execute |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 69 | MethodInfo *MethInfo; // The MethInfo annotation for the function |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 70 | std::vector<ValuePlaneTy> Values;// ValuePlanes for each type |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 71 | |
| 72 | BasicBlock *PrevBB; // The previous BB or null if in first BB |
| 73 | CallInst *Caller; // Holds the call that called subframes. |
| 74 | // NULL if main func or debugger invoked fn |
Chris Lattner | 9bffa73 | 2002-02-19 18:50:09 +0000 | [diff] [blame] | 75 | AllocaHolderHandle Allocas; // Track memory allocated by alloca |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 78 | // Interpreter - This class represents the entirety of the interpreter. |
| 79 | // |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 80 | class Interpreter : public ExecutionEngine { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 81 | int ExitCode; // The exit code to be returned by the lli util |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 82 | bool Debug; // Debug mode enabled? |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 83 | bool Profile; // Profiling enabled? |
Chris Lattner | 43e3f7c | 2001-10-27 08:43:52 +0000 | [diff] [blame] | 84 | bool Trace; // Tracing enabled? |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 85 | int CurFrame; // The current stack frame being inspected |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 86 | TargetData TD; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 87 | |
| 88 | // 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] | 89 | // function record. |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 90 | std::vector<ExecutionContext> ECStack; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 91 | |
| 92 | public: |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 93 | Interpreter(Module *M, unsigned Config, bool DebugMode, bool TraceMode); |
| 94 | inline ~Interpreter() { CW.setModule(0); } |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 95 | |
| 96 | // getExitCode - return the code that should be the exit code for the lli |
| 97 | // utility. |
| 98 | inline int getExitCode() const { return ExitCode; } |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 99 | |
| 100 | /// run - Start execution with the specified function and arguments. |
| 101 | /// |
| 102 | virtual int run(const std::string &FnName, |
| 103 | const std::vector<std::string> &Args); |
| 104 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 105 | |
| 106 | // enableProfiling() - Turn profiling on, clear stats? |
| 107 | void enableProfiling() { Profile = true; } |
Chris Lattner | 43e3f7c | 2001-10-27 08:43:52 +0000 | [diff] [blame] | 108 | void enableTracing() { Trace = true; } |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 109 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 110 | void handleUserInput(); |
| 111 | |
| 112 | // User Interation Methods... |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 113 | bool callMethod(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); |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 117 | static void print(const Type *Ty, GenericValue V); |
Chris Lattner | 365a76e | 2001-09-10 04:49:44 +0000 | [diff] [blame] | 118 | static void printValue(const Type *Ty, GenericValue V); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 119 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 120 | bool callMainMethod(const std::string &MainName, |
| 121 | const std::vector<std::string> &InputFilename); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 122 | |
| 123 | void list(); // Do the 'list' command |
| 124 | void printStackTrace(); // Do the 'backtrace' command |
| 125 | |
| 126 | // Code execution methods... |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 127 | void callMethod(Function *F, const std::vector<GenericValue> &ArgVals); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 128 | bool executeInstruction(); // Execute one instruction... |
| 129 | |
| 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 |
| 134 | |
| 135 | // Opcode Implementations |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 136 | void executeCallInst(CallInst &I, ExecutionContext &SF); |
| 137 | void executeRetInst(ReturnInst &I, ExecutionContext &SF); |
| 138 | void executeBrInst(BranchInst &I, ExecutionContext &SF); |
| 139 | void executeAllocInst(AllocationInst &I, ExecutionContext &SF); |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 140 | GenericValue callExternalMethod(Function *F, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 141 | const std::vector<GenericValue> &ArgVals); |
Chris Lattner | e43db88 | 2001-10-27 04:15:57 +0000 | [diff] [blame] | 142 | void exitCalled(GenericValue GV); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 143 | |
| 144 | // getCurrentMethod - Return the currently executing method |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 145 | inline Function *getCurrentMethod() const { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 146 | return CurFrame < 0 ? 0 : ECStack[CurFrame].CurMethod; |
| 147 | } |
| 148 | |
| 149 | // isStopped - Return true if a program is stopped. Return false if no |
| 150 | // program is running. |
| 151 | // |
| 152 | inline bool isStopped() const { return !ECStack.empty(); } |
| 153 | |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 154 | //FIXME: private: |
| 155 | public: |
| 156 | GenericValue executeGEPOperation(Value *Ptr, User::op_iterator I, |
| 157 | User::op_iterator E, ExecutionContext &SF); |
| 158 | void executeLoadInst(LoadInst &I, ExecutionContext &SF); |
| 159 | void executeStoreInst(StoreInst &I, ExecutionContext &SF); |
| 160 | |
| 161 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 162 | private: // Helper functions |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 163 | void *getPointerToFunction(const Function *F) { return (void*)F; } |
| 164 | |
Chris Lattner | e43db88 | 2001-10-27 04:15:57 +0000 | [diff] [blame] | 165 | // getCurrentExecutablePath() - Return the directory that the lli executable |
| 166 | // lives in. |
| 167 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 168 | std::string getCurrentExecutablePath() const; |
Chris Lattner | e43db88 | 2001-10-27 04:15:57 +0000 | [diff] [blame] | 169 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 170 | // printCurrentInstruction - Print out the instruction that the virtual PC is |
| 171 | // at, or fail silently if no program is running. |
| 172 | // |
| 173 | void printCurrentInstruction(); |
| 174 | |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 175 | // printStackFrame - Print information about the specified stack frame, or -1 |
| 176 | // for the default one. |
| 177 | // |
| 178 | void printStackFrame(int FrameNo = -1); |
| 179 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 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. |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 184 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 185 | std::vector<Value*> LookupMatchingNames(const std::string &Name); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 186 | |
| 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. |
| 190 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 191 | Value *ChooseOneOption(const std::string &Name, |
| 192 | const std::vector<Value*> &Opts); |
Chris Lattner | 5deea3c | 2001-10-30 20:28:23 +0000 | [diff] [blame] | 193 | |
| 194 | |
| 195 | void initializeExecutionEngine(); |
| 196 | void initializeExternalMethods(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | #endif |