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. |
| 11 | #define PROFILE_STRUCTURE_FIELDS 1 |
| 12 | |
| 13 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 14 | #include "llvm/Module.h" |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame^] | 15 | #include "llvm/Function.h" |
Chris Lattner | 221d688 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 16 | #include "llvm/BasicBlock.h" |
Chris Lattner | 360e17e | 2001-11-26 23:04:08 +0000 | [diff] [blame] | 17 | #include "Support/DataTypes.h" |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 18 | #include "llvm/Assembly/CachedWriter.h" |
| 19 | |
| 20 | extern CachedWriter CW; // Object to accellerate 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 | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 26 | class AllocationInst; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 27 | |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 28 | typedef uint64_t PointerTy; |
| 29 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 30 | union GenericValue { |
| 31 | bool BoolVal; |
| 32 | unsigned char UByteVal; |
| 33 | signed char SByteVal; |
| 34 | unsigned short UShortVal; |
| 35 | signed short ShortVal; |
| 36 | unsigned int UIntVal; |
| 37 | signed int IntVal; |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 38 | uint64_t ULongVal; |
| 39 | int64_t LongVal; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 40 | double DoubleVal; |
| 41 | float FloatVal; |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 42 | PointerTy PointerVal; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
Chris Lattner | 9bffa73 | 2002-02-19 18:50:09 +0000 | [diff] [blame] | 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. |
| 48 | // |
| 49 | class AllocaHolder { |
| 50 | friend class AllocaHolderHandle; |
| 51 | std::vector<void*> Allocations; |
| 52 | unsigned RefCnt; |
| 53 | public: |
| 54 | AllocaHolder() : RefCnt(0) {} |
| 55 | void add(void *mem) { Allocations.push_back(mem); } |
| 56 | ~AllocaHolder() { |
| 57 | for (unsigned i = 0; i < Allocations.size(); ++i) |
| 58 | free(Allocations[i]); |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | // AllocaHolderHandle gives AllocaHolder value semantics so we can stick it into |
| 63 | // a vector... |
| 64 | // |
| 65 | class AllocaHolderHandle { |
| 66 | AllocaHolder *H; |
| 67 | public: |
| 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; } |
| 71 | |
| 72 | void add(void *mem) { H->add(mem); } |
| 73 | }; |
| 74 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 75 | typedef std::vector<GenericValue> ValuePlaneTy; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 76 | |
| 77 | // ExecutionContext struct - This struct represents one stack frame currently |
| 78 | // executing. |
| 79 | // |
| 80 | struct ExecutionContext { |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame^] | 81 | Function *CurMethod; // The currently executing function |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 82 | BasicBlock *CurBB; // The currently executing BB |
| 83 | BasicBlock::iterator CurInst; // The next instruction to execute |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame^] | 84 | MethodInfo *MethInfo; // The MethInfo annotation for the function |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 85 | std::vector<ValuePlaneTy> Values;// ValuePlanes for each type |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 86 | |
| 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 |
Chris Lattner | 9bffa73 | 2002-02-19 18:50:09 +0000 | [diff] [blame] | 90 | AllocaHolderHandle Allocas; // Track memory allocated by alloca |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 93 | // Interpreter - This class represents the entirety of the interpreter. |
| 94 | // |
| 95 | class 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? |
Chris Lattner | 43e3f7c | 2001-10-27 08:43:52 +0000 | [diff] [blame] | 99 | bool Trace; // Tracing enabled? |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 100 | int CurFrame; // The current stack frame being inspected |
| 101 | |
| 102 | // 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^] | 103 | // function record. |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 104 | std::vector<ExecutionContext> ECStack; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 105 | |
| 106 | public: |
| 107 | Interpreter(); |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 108 | inline ~Interpreter() { CW.setModule(0); delete CurMod; } |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 109 | |
| 110 | // getExitCode - return the code that should be the exit code for the lli |
| 111 | // utility. |
| 112 | inline int getExitCode() const { return ExitCode; } |
| 113 | |
| 114 | // enableProfiling() - Turn profiling on, clear stats? |
| 115 | void enableProfiling() { Profile = true; } |
Chris Lattner | 43e3f7c | 2001-10-27 08:43:52 +0000 | [diff] [blame] | 116 | void enableTracing() { Trace = true; } |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 117 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 118 | void handleUserInput(); |
| 119 | |
| 120 | // User Interation Methods... |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 121 | void loadModule(const std::string &Filename); |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 122 | bool flushModule(); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 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); |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 127 | static void print(const Type *Ty, GenericValue V); |
Chris Lattner | 365a76e | 2001-09-10 04:49:44 +0000 | [diff] [blame] | 128 | static void printValue(const Type *Ty, GenericValue V); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 129 | |
Chris Lattner | f8f2afb | 2001-10-18 21:55:32 +0000 | [diff] [blame] | 130 | // Hack until we can parse command line args... |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 131 | bool callMainMethod(const std::string &MainName, |
| 132 | const std::vector<std::string> &InputFilename); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 133 | |
| 134 | void list(); // Do the 'list' command |
| 135 | void printStackTrace(); // Do the 'backtrace' command |
| 136 | |
| 137 | // Code execution methods... |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame^] | 138 | void callMethod(Function *F, const std::vector<GenericValue> &ArgVals); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 139 | bool executeInstruction(); // Execute one instruction... |
| 140 | |
| 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 |
| 145 | |
| 146 | // Opcode Implementations |
| 147 | void executeCallInst(CallInst *I, ExecutionContext &SF); |
| 148 | void executeRetInst(ReturnInst *I, ExecutionContext &SF); |
| 149 | void executeBrInst(BranchInst *I, ExecutionContext &SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 150 | void executeAllocInst(AllocationInst *I, ExecutionContext &SF); |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame^] | 151 | GenericValue callExternalMethod(Function *F, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 152 | const std::vector<GenericValue> &ArgVals); |
Chris Lattner | e43db88 | 2001-10-27 04:15:57 +0000 | [diff] [blame] | 153 | void exitCalled(GenericValue GV); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 154 | |
| 155 | // getCurrentMethod - Return the currently executing method |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame^] | 156 | inline Function *getCurrentMethod() const { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 157 | return CurFrame < 0 ? 0 : ECStack[CurFrame].CurMethod; |
| 158 | } |
| 159 | |
| 160 | // isStopped - Return true if a program is stopped. Return false if no |
| 161 | // program is running. |
| 162 | // |
| 163 | inline bool isStopped() const { return !ECStack.empty(); } |
| 164 | |
| 165 | private: // Helper functions |
Chris Lattner | e43db88 | 2001-10-27 04:15:57 +0000 | [diff] [blame] | 166 | // getCurrentExecutablePath() - Return the directory that the lli executable |
| 167 | // lives in. |
| 168 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 169 | std::string getCurrentExecutablePath() const; |
Chris Lattner | e43db88 | 2001-10-27 04:15:57 +0000 | [diff] [blame] | 170 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 171 | // printCurrentInstruction - Print out the instruction that the virtual PC is |
| 172 | // at, or fail silently if no program is running. |
| 173 | // |
| 174 | void printCurrentInstruction(); |
| 175 | |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 176 | // printStackFrame - Print information about the specified stack frame, or -1 |
| 177 | // for the default one. |
| 178 | // |
| 179 | void printStackFrame(int FrameNo = -1); |
| 180 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame^] | 181 | // LookupMatchingNames - Search the current function namespace, then the |
| 182 | // global namespace looking for values that match the specified name. Return |
| 183 | // ALL matches to that name. This is obviously slow, and should only be used |
| 184 | // for user interaction. |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 185 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 186 | std::vector<Value*> LookupMatchingNames(const std::string &Name); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 187 | |
| 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. |
| 191 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 192 | Value *ChooseOneOption(const std::string &Name, |
| 193 | const std::vector<Value*> &Opts); |
Chris Lattner | 5deea3c | 2001-10-30 20:28:23 +0000 | [diff] [blame] | 194 | |
| 195 | |
| 196 | void initializeExecutionEngine(); |
| 197 | void initializeExternalMethods(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | #endif |