Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1 | //===-- UserInput.cpp - Interpreter Input Loop support --------------------===// |
| 2 | // |
| 3 | // This file implements the interpreter Input I/O loop. |
| 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
| 7 | #include "Interpreter.h" |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 8 | #include "llvm/Bytecode/Reader.h" |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 9 | #include "llvm/Assembly/Writer.h" |
| 10 | #include <algorithm> |
| 11 | |
| 12 | enum CommandID { |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 13 | Quit, Help, // Basics |
| 14 | Print, Info, List, StackTrace, Up, Down, // Inspection |
| 15 | Next, Step, Run, Finish, Call, // Control flow changes |
| 16 | Break, Watch, // Debugging |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 17 | Load, Flush |
| 18 | }; |
| 19 | |
| 20 | // CommandTable - Build a lookup table for the commands available to the user... |
| 21 | static struct CommandTableElement { |
| 22 | const char *Name; |
| 23 | enum CommandID CID; |
| 24 | |
| 25 | inline bool operator<(const CommandTableElement &E) const { |
| 26 | return string(Name) < string(E.Name); |
| 27 | } |
| 28 | inline bool operator==(const string &S) const { |
| 29 | return string(Name) == S; |
| 30 | } |
| 31 | } CommandTable[] = { |
| 32 | { "quit" , Quit }, { "q", Quit }, { "", Quit }, // Empty str = eof |
| 33 | { "help" , Help }, { "h", Help }, |
| 34 | |
| 35 | { "print" , Print }, { "p", Print }, |
| 36 | { "list" , List }, |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 37 | { "info" , Info }, |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 38 | { "backtrace", StackTrace }, { "bt", StackTrace }, { "where", StackTrace }, |
| 39 | { "up" , Up }, |
| 40 | { "down" , Down }, |
| 41 | |
| 42 | { "next" , Next }, { "n", Next }, |
| 43 | { "step" , Step }, { "s", Step }, |
| 44 | { "run" , Run }, |
| 45 | { "finish" , Finish }, |
| 46 | { "call" , Call }, |
| 47 | |
| 48 | { "break" , Break }, { "b", Break }, |
| 49 | { "watch" , Watch }, |
| 50 | |
| 51 | { "load" , Load }, |
| 52 | { "flush" , Flush }, |
| 53 | }; |
| 54 | static CommandTableElement *CommandTableEnd = |
| 55 | CommandTable+sizeof(CommandTable)/sizeof(CommandTable[0]); |
| 56 | |
| 57 | |
| 58 | //===----------------------------------------------------------------------===// |
| 59 | // handleUserInput - Enter the input loop for the interpreter. This function |
| 60 | // returns when the user quits the interpreter. |
| 61 | // |
| 62 | void Interpreter::handleUserInput() { |
| 63 | bool UserQuit = false; |
| 64 | |
| 65 | // Sort the table... |
| 66 | sort(CommandTable, CommandTableEnd); |
| 67 | |
| 68 | // Print the instruction that we are stopped at... |
| 69 | printCurrentInstruction(); |
| 70 | |
| 71 | do { |
| 72 | string Command; |
| 73 | cout << "lli> " << flush; |
| 74 | cin >> Command; |
| 75 | |
| 76 | CommandTableElement *E = find(CommandTable, CommandTableEnd, Command); |
| 77 | |
| 78 | if (E == CommandTableEnd) { |
| 79 | cout << "Error: '" << Command << "' not recognized!\n"; |
| 80 | continue; |
| 81 | } |
| 82 | |
| 83 | switch (E->CID) { |
| 84 | case Quit: UserQuit = true; break; |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 85 | case Load: |
| 86 | cin >> Command; |
| 87 | loadModule(Command); |
| 88 | break; |
| 89 | case Flush: flushModule(); break; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 90 | case Print: |
| 91 | cin >> Command; |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 92 | print(Command); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 93 | break; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 94 | case Info: |
| 95 | cin >> Command; |
| 96 | infoValue(Command); |
| 97 | break; |
| 98 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 99 | case List: list(); break; |
| 100 | case StackTrace: printStackTrace(); break; |
| 101 | case Up: |
| 102 | if (CurFrame > 0) --CurFrame; |
| 103 | else cout << "Error: Already at root of stack!\n"; |
| 104 | break; |
| 105 | case Down: |
| 106 | if ((unsigned)CurFrame < ECStack.size()-1) ++CurFrame; |
| 107 | else cout << "Error: Already at bottom of stack!\n"; |
| 108 | break; |
| 109 | case Next: nextInstruction(); break; |
| 110 | case Step: stepInstruction(); break; |
| 111 | case Run: run(); break; |
| 112 | case Finish: finish(); break; |
| 113 | case Call: |
| 114 | cin >> Command; |
| 115 | callMethod(Command); // Enter the specified method |
| 116 | finish(); // Run until it's complete |
| 117 | break; |
| 118 | |
| 119 | default: |
| 120 | cout << "Command '" << Command << "' unimplemented!\n"; |
| 121 | break; |
| 122 | } |
| 123 | |
| 124 | } while (!UserQuit); |
| 125 | } |
| 126 | |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 127 | //===----------------------------------------------------------------------===// |
| 128 | // loadModule - Load a new module to execute... |
| 129 | // |
| 130 | void Interpreter::loadModule(const string &Filename) { |
| 131 | if (CurMod && !flushModule()) return; // Kill current execution |
| 132 | |
| 133 | CurMod = ParseBytecodeFile(Filename); |
| 134 | if (CurMod == 0) { |
| 135 | cout << "Error parsing '" << Filename << "': No module loaded.\n"; |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | // TODO: link in support library... |
| 140 | } |
| 141 | |
| 142 | |
| 143 | //===----------------------------------------------------------------------===// |
| 144 | // flushModule - Return true if the current program has been unloaded. |
| 145 | // |
| 146 | bool Interpreter::flushModule() { |
| 147 | if (CurMod == 0) { |
| 148 | cout << "Error flushing: No module loaded!\n"; |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | if (!ECStack.empty()) { |
| 153 | // TODO: if use is not sure, return false |
| 154 | cout << "Killing current execution!\n"; |
| 155 | ECStack.clear(); |
| 156 | CurFrame = -1; |
| 157 | } |
| 158 | |
| 159 | delete CurMod; |
| 160 | CurMod = 0; |
| 161 | ExitCode = 0; |
| 162 | return true; |
| 163 | } |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 164 | |
| 165 | //===----------------------------------------------------------------------===// |
| 166 | // setBreakpoint - Enable a breakpoint at the specified location |
| 167 | // |
| 168 | void Interpreter::setBreakpoint(const string &Name) { |
| 169 | Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name)); |
| 170 | // TODO: Set a breakpoint on PickedVal |
| 171 | } |
| 172 | |
| 173 | //===----------------------------------------------------------------------===// |
| 174 | // callMethod - Enter the specified method... |
| 175 | // |
| 176 | bool Interpreter::callMethod(const string &Name) { |
| 177 | vector<Value*> Options = LookupMatchingNames(Name); |
| 178 | |
| 179 | for (unsigned i = 0; i < Options.size(); ++i) { // Remove nonmethod matches... |
Chris Lattner | 1d87bcf | 2001-10-01 20:11:19 +0000 | [diff] [blame] | 180 | if (!isa<Method>(Options[i])) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 181 | Options.erase(Options.begin()+i); |
| 182 | --i; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | Value *PickedMeth = ChooseOneOption(Name, Options); |
| 187 | if (PickedMeth == 0) |
| 188 | return true; |
| 189 | |
Chris Lattner | 9636a91 | 2001-10-01 16:18:37 +0000 | [diff] [blame] | 190 | Method *M = cast<Method>(PickedMeth); |
Chris Lattner | 365a76e | 2001-09-10 04:49:44 +0000 | [diff] [blame] | 191 | |
| 192 | vector<GenericValue> Args; |
| 193 | // TODO, get args from user... |
| 194 | |
| 195 | callMethod(M, Args); // Start executing it... |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 196 | |
| 197 | // Reset the current frame location to the top of stack |
| 198 | CurFrame = ECStack.size()-1; |
| 199 | |
| 200 | return false; |
| 201 | } |