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 | |
| 10 | #include "llvm/Module.h" |
| 11 | #include "llvm/Method.h" |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 12 | #include "llvm/Support/DataTypes.h" |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 13 | #include "llvm/Assembly/CachedWriter.h" |
| 14 | |
| 15 | extern CachedWriter CW; // Object to accellerate printing of LLVM |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 16 | |
| 17 | struct MethodInfo; // Defined in ExecutionAnnotations.h |
| 18 | class CallInst; |
| 19 | class ReturnInst; |
| 20 | class BranchInst; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 21 | class AllocationInst; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 22 | |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame^] | 23 | typedef uint64_t PointerTy; |
| 24 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 25 | union GenericValue { |
| 26 | bool BoolVal; |
| 27 | unsigned char UByteVal; |
| 28 | signed char SByteVal; |
| 29 | unsigned short UShortVal; |
| 30 | signed short ShortVal; |
| 31 | unsigned int UIntVal; |
| 32 | signed int IntVal; |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 33 | uint64_t ULongVal; |
| 34 | int64_t LongVal; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 35 | double DoubleVal; |
| 36 | float FloatVal; |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame^] | 37 | PointerTy PointerVal; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | typedef vector<GenericValue> ValuePlaneTy; |
| 41 | |
| 42 | // ExecutionContext struct - This struct represents one stack frame currently |
| 43 | // executing. |
| 44 | // |
| 45 | struct ExecutionContext { |
| 46 | Method *CurMethod; // The currently executing method |
| 47 | BasicBlock *CurBB; // The currently executing BB |
| 48 | BasicBlock::iterator CurInst; // The next instruction to execute |
| 49 | MethodInfo *MethInfo; // The MethInfo annotation for the method |
| 50 | vector<ValuePlaneTy> Values; // ValuePlanes for each type |
| 51 | |
| 52 | BasicBlock *PrevBB; // The previous BB or null if in first BB |
| 53 | CallInst *Caller; // Holds the call that called subframes. |
| 54 | // NULL if main func or debugger invoked fn |
| 55 | }; |
| 56 | |
| 57 | |
| 58 | // Interpreter - This class represents the entirety of the interpreter. |
| 59 | // |
| 60 | class Interpreter { |
| 61 | Module *CurMod; // The current Module being executed (0 if none) |
| 62 | int ExitCode; // The exit code to be returned by the lli util |
| 63 | bool Profile; // Profiling enabled? |
Chris Lattner | 43e3f7c | 2001-10-27 08:43:52 +0000 | [diff] [blame] | 64 | bool Trace; // Tracing enabled? |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 65 | int CurFrame; // The current stack frame being inspected |
| 66 | |
| 67 | // The runtime stack of executing code. The top of the stack is the current |
| 68 | // method record. |
| 69 | vector<ExecutionContext> ECStack; |
| 70 | |
| 71 | public: |
| 72 | Interpreter(); |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 73 | inline ~Interpreter() { CW.setModule(0); delete CurMod; } |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 74 | |
| 75 | // getExitCode - return the code that should be the exit code for the lli |
| 76 | // utility. |
| 77 | inline int getExitCode() const { return ExitCode; } |
| 78 | |
| 79 | // enableProfiling() - Turn profiling on, clear stats? |
| 80 | void enableProfiling() { Profile = true; } |
Chris Lattner | 43e3f7c | 2001-10-27 08:43:52 +0000 | [diff] [blame] | 81 | void enableTracing() { Trace = true; } |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 83 | void handleUserInput(); |
| 84 | |
| 85 | // User Interation Methods... |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 86 | void loadModule(const string &Filename); |
| 87 | bool flushModule(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 88 | bool callMethod(const string &Name); // return true on failure |
| 89 | void setBreakpoint(const string &Name); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 90 | void infoValue(const string &Name); |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 91 | void print(const string &Name); |
| 92 | static void print(const Type *Ty, GenericValue V); |
Chris Lattner | 365a76e | 2001-09-10 04:49:44 +0000 | [diff] [blame] | 93 | static void printValue(const Type *Ty, GenericValue V); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 94 | |
Chris Lattner | f8f2afb | 2001-10-18 21:55:32 +0000 | [diff] [blame] | 95 | // Hack until we can parse command line args... |
| 96 | bool callMainMethod(const string &MainName, |
Chris Lattner | 204eec3 | 2001-10-27 05:54:31 +0000 | [diff] [blame] | 97 | const vector<string> &InputFilename); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 98 | |
| 99 | void list(); // Do the 'list' command |
| 100 | void printStackTrace(); // Do the 'backtrace' command |
| 101 | |
| 102 | // Code execution methods... |
Chris Lattner | 365a76e | 2001-09-10 04:49:44 +0000 | [diff] [blame] | 103 | void callMethod (Method *Meth, const vector<GenericValue> &ArgVals); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 104 | bool executeInstruction(); // Execute one instruction... |
| 105 | |
| 106 | void stepInstruction(); // Do the 'step' command |
| 107 | void nextInstruction(); // Do the 'next' command |
| 108 | void run(); // Do the 'run' command |
| 109 | void finish(); // Do the 'finish' command |
| 110 | |
| 111 | // Opcode Implementations |
| 112 | void executeCallInst(CallInst *I, ExecutionContext &SF); |
| 113 | void executeRetInst(ReturnInst *I, ExecutionContext &SF); |
| 114 | void executeBrInst(BranchInst *I, ExecutionContext &SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 115 | void executeAllocInst(AllocationInst *I, ExecutionContext &SF); |
Chris Lattner | 5deea3c | 2001-10-30 20:28:23 +0000 | [diff] [blame] | 116 | GenericValue callExternalMethod(Method *Meth, |
| 117 | const vector<GenericValue> &ArgVals); |
Chris Lattner | e43db88 | 2001-10-27 04:15:57 +0000 | [diff] [blame] | 118 | void exitCalled(GenericValue GV); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 119 | |
| 120 | // getCurrentMethod - Return the currently executing method |
| 121 | inline Method *getCurrentMethod() const { |
| 122 | return CurFrame < 0 ? 0 : ECStack[CurFrame].CurMethod; |
| 123 | } |
| 124 | |
| 125 | // isStopped - Return true if a program is stopped. Return false if no |
| 126 | // program is running. |
| 127 | // |
| 128 | inline bool isStopped() const { return !ECStack.empty(); } |
| 129 | |
| 130 | private: // Helper functions |
Chris Lattner | e43db88 | 2001-10-27 04:15:57 +0000 | [diff] [blame] | 131 | // getCurrentExecutablePath() - Return the directory that the lli executable |
| 132 | // lives in. |
| 133 | // |
| 134 | string getCurrentExecutablePath() const; |
| 135 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 136 | // printCurrentInstruction - Print out the instruction that the virtual PC is |
| 137 | // at, or fail silently if no program is running. |
| 138 | // |
| 139 | void printCurrentInstruction(); |
| 140 | |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 141 | // printStackFrame - Print information about the specified stack frame, or -1 |
| 142 | // for the default one. |
| 143 | // |
| 144 | void printStackFrame(int FrameNo = -1); |
| 145 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 146 | // LookupMatchingNames - Search the current method namespace, then the global |
| 147 | // namespace looking for values that match the specified name. Return ALL |
| 148 | // matches to that name. This is obviously slow, and should only be used for |
| 149 | // user interaction. |
| 150 | // |
| 151 | vector<Value*> LookupMatchingNames(const string &Name); |
| 152 | |
| 153 | // ChooseOneOption - Prompt the user to choose among the specified options to |
| 154 | // pick one value. If no options are provided, emit an error. If a single |
| 155 | // option is provided, just return that option. |
| 156 | // |
| 157 | Value *ChooseOneOption(const string &Name, const vector<Value*> &Opts); |
Chris Lattner | 5deea3c | 2001-10-30 20:28:23 +0000 | [diff] [blame] | 158 | |
| 159 | |
| 160 | void initializeExecutionEngine(); |
| 161 | void initializeExternalMethods(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | #endif |