blob: 4c2faf6ca166016e51583d68794a21a3dba6c8fd [file] [log] [blame]
Chris Lattner92101ac2001-08-23 17:05:04 +00001//===-- 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 Lattner2e42d3a2001-10-15 05:51:48 +00008#include "llvm/Bytecode/Reader.h"
Chris Lattner92101ac2001-08-23 17:05:04 +00009#include "llvm/Assembly/Writer.h"
10#include <algorithm>
11
12enum CommandID {
Chris Lattner86660982001-08-27 05:16:50 +000013 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 Lattner92101ac2001-08-23 17:05:04 +000017 Load, Flush
18};
19
20// CommandTable - Build a lookup table for the commands available to the user...
21static 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 Lattner86660982001-08-27 05:16:50 +000037 { "info" , Info },
Chris Lattner92101ac2001-08-23 17:05:04 +000038 { "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};
54static 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//
62void 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 Lattner2e42d3a2001-10-15 05:51:48 +000085 case Load:
86 cin >> Command;
87 loadModule(Command);
88 break;
89 case Flush: flushModule(); break;
Chris Lattner92101ac2001-08-23 17:05:04 +000090 case Print:
91 cin >> Command;
Chris Lattner2e42d3a2001-10-15 05:51:48 +000092 print(Command);
Chris Lattner92101ac2001-08-23 17:05:04 +000093 break;
Chris Lattner86660982001-08-27 05:16:50 +000094 case Info:
95 cin >> Command;
96 infoValue(Command);
97 break;
98
Chris Lattner92101ac2001-08-23 17:05:04 +000099 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 Lattner2e42d3a2001-10-15 05:51:48 +0000127//===----------------------------------------------------------------------===//
128// loadModule - Load a new module to execute...
129//
130void 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//
146bool 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 Lattner92101ac2001-08-23 17:05:04 +0000164
165//===----------------------------------------------------------------------===//
166// setBreakpoint - Enable a breakpoint at the specified location
167//
168void 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//
176bool 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 Lattner1d87bcf2001-10-01 20:11:19 +0000180 if (!isa<Method>(Options[i])) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000181 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 Lattner9636a912001-10-01 16:18:37 +0000190 Method *M = cast<Method>(PickedMeth);
Chris Lattner365a76e2001-09-10 04:49:44 +0000191
192 vector<GenericValue> Args;
193 // TODO, get args from user...
194
195 callMethod(M, Args); // Start executing it...
Chris Lattner92101ac2001-08-23 17:05:04 +0000196
197 // Reset the current frame location to the top of stack
198 CurFrame = ECStack.size()-1;
199
200 return false;
201}