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