blob: 97bba9cf7dcdfe30e6605311d952be225fccdeed [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"
Chris Lattnerf8f2afb2001-10-18 21:55:32 +000010#include "llvm/DerivedTypes.h"
Chris Lattnere43db882001-10-27 04:15:57 +000011#include "llvm/Transforms/Linker.h"
Chris Lattner92101ac2001-08-23 17:05:04 +000012#include <algorithm>
13
14enum CommandID {
Chris Lattner86660982001-08-27 05:16:50 +000015 Quit, Help, // Basics
16 Print, Info, List, StackTrace, Up, Down, // Inspection
17 Next, Step, Run, Finish, Call, // Control flow changes
18 Break, Watch, // Debugging
Chris Lattner43e3f7c2001-10-27 08:43:52 +000019 Load, Flush,
20 TraceOpt, ProfileOpt // Toggle features
Chris Lattner92101ac2001-08-23 17:05:04 +000021};
22
23// CommandTable - Build a lookup table for the commands available to the user...
24static struct CommandTableElement {
25 const char *Name;
26 enum CommandID CID;
27
28 inline bool operator<(const CommandTableElement &E) const {
29 return string(Name) < string(E.Name);
30 }
31 inline bool operator==(const string &S) const {
32 return string(Name) == S;
33 }
34} CommandTable[] = {
35 { "quit" , Quit }, { "q", Quit }, { "", Quit }, // Empty str = eof
36 { "help" , Help }, { "h", Help },
37
38 { "print" , Print }, { "p", Print },
39 { "list" , List },
Chris Lattner86660982001-08-27 05:16:50 +000040 { "info" , Info },
Chris Lattner92101ac2001-08-23 17:05:04 +000041 { "backtrace", StackTrace }, { "bt", StackTrace }, { "where", StackTrace },
42 { "up" , Up },
43 { "down" , Down },
44
45 { "next" , Next }, { "n", Next },
46 { "step" , Step }, { "s", Step },
47 { "run" , Run },
48 { "finish" , Finish },
49 { "call" , Call },
50
51 { "break" , Break }, { "b", Break },
52 { "watch" , Watch },
53
54 { "load" , Load },
55 { "flush" , Flush },
Chris Lattner43e3f7c2001-10-27 08:43:52 +000056
57 { "trace" , TraceOpt },
58 { "profile" , ProfileOpt },
Chris Lattner92101ac2001-08-23 17:05:04 +000059};
60static CommandTableElement *CommandTableEnd =
61 CommandTable+sizeof(CommandTable)/sizeof(CommandTable[0]);
62
63
64//===----------------------------------------------------------------------===//
65// handleUserInput - Enter the input loop for the interpreter. This function
66// returns when the user quits the interpreter.
67//
68void Interpreter::handleUserInput() {
69 bool UserQuit = false;
70
71 // Sort the table...
72 sort(CommandTable, CommandTableEnd);
73
74 // Print the instruction that we are stopped at...
75 printCurrentInstruction();
76
77 do {
78 string Command;
79 cout << "lli> " << flush;
80 cin >> Command;
81
82 CommandTableElement *E = find(CommandTable, CommandTableEnd, Command);
83
84 if (E == CommandTableEnd) {
85 cout << "Error: '" << Command << "' not recognized!\n";
86 continue;
87 }
88
89 switch (E->CID) {
90 case Quit: UserQuit = true; break;
Chris Lattner2e42d3a2001-10-15 05:51:48 +000091 case Load:
92 cin >> Command;
93 loadModule(Command);
94 break;
95 case Flush: flushModule(); break;
Chris Lattner92101ac2001-08-23 17:05:04 +000096 case Print:
97 cin >> Command;
Chris Lattner2e42d3a2001-10-15 05:51:48 +000098 print(Command);
Chris Lattner92101ac2001-08-23 17:05:04 +000099 break;
Chris Lattner86660982001-08-27 05:16:50 +0000100 case Info:
101 cin >> Command;
102 infoValue(Command);
103 break;
104
Chris Lattner92101ac2001-08-23 17:05:04 +0000105 case List: list(); break;
106 case StackTrace: printStackTrace(); break;
107 case Up:
108 if (CurFrame > 0) --CurFrame;
109 else cout << "Error: Already at root of stack!\n";
110 break;
111 case Down:
112 if ((unsigned)CurFrame < ECStack.size()-1) ++CurFrame;
113 else cout << "Error: Already at bottom of stack!\n";
114 break;
115 case Next: nextInstruction(); break;
116 case Step: stepInstruction(); break;
117 case Run: run(); break;
118 case Finish: finish(); break;
119 case Call:
120 cin >> Command;
121 callMethod(Command); // Enter the specified method
122 finish(); // Run until it's complete
123 break;
124
Chris Lattner43e3f7c2001-10-27 08:43:52 +0000125 case TraceOpt:
126 Trace = !Trace;
127 cout << "Tracing " << (Trace ? "enabled\n" : "disabled\n");
128 break;
129
130 case ProfileOpt:
131 Profile = !Profile;
132 cout << "Profiling " << (Trace ? "enabled\n" : "disabled\n");
133 break;
134
Chris Lattner92101ac2001-08-23 17:05:04 +0000135 default:
136 cout << "Command '" << Command << "' unimplemented!\n";
137 break;
138 }
139
140 } while (!UserQuit);
141}
142
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000143//===----------------------------------------------------------------------===//
144// loadModule - Load a new module to execute...
145//
146void Interpreter::loadModule(const string &Filename) {
Chris Lattnere43db882001-10-27 04:15:57 +0000147 string ErrorMsg;
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000148 if (CurMod && !flushModule()) return; // Kill current execution
149
Chris Lattnere43db882001-10-27 04:15:57 +0000150 CurMod = ParseBytecodeFile(Filename, &ErrorMsg);
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000151 if (CurMod == 0) {
Chris Lattnere43db882001-10-27 04:15:57 +0000152 cout << "Error parsing '" << Filename << "': No module loaded: "
153 << ErrorMsg << "\n";
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000154 return;
155 }
156
Chris Lattnere43db882001-10-27 04:15:57 +0000157 string RuntimeLib = getCurrentExecutablePath() + "/RuntimeLib.bc";
158 if (Module *SupportLib = ParseBytecodeFile(RuntimeLib, &ErrorMsg)) {
159 if (LinkModules(CurMod, SupportLib, &ErrorMsg))
160 cerr << "Error Linking runtime library into current module: "
161 << ErrorMsg << endl;
162 } else {
163 cerr << "Error loading runtime library '"+RuntimeLib+"': "
164 << ErrorMsg << "\n";
165 }
Chris Lattner2e42d3a2001-10-15 05:51:48 +0000166}
167
168
169//===----------------------------------------------------------------------===//
170// flushModule - Return true if the current program has been unloaded.
171//
172bool Interpreter::flushModule() {
173 if (CurMod == 0) {
174 cout << "Error flushing: No module loaded!\n";
175 return false;
176 }
177
178 if (!ECStack.empty()) {
179 // TODO: if use is not sure, return false
180 cout << "Killing current execution!\n";
181 ECStack.clear();
182 CurFrame = -1;
183 }
184
185 delete CurMod;
186 CurMod = 0;
187 ExitCode = 0;
188 return true;
189}
Chris Lattner92101ac2001-08-23 17:05:04 +0000190
191//===----------------------------------------------------------------------===//
192// setBreakpoint - Enable a breakpoint at the specified location
193//
194void Interpreter::setBreakpoint(const string &Name) {
195 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
196 // TODO: Set a breakpoint on PickedVal
197}
198
199//===----------------------------------------------------------------------===//
200// callMethod - Enter the specified method...
201//
202bool Interpreter::callMethod(const string &Name) {
203 vector<Value*> Options = LookupMatchingNames(Name);
204
205 for (unsigned i = 0; i < Options.size(); ++i) { // Remove nonmethod matches...
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000206 if (!isa<Method>(Options[i])) {
Chris Lattner92101ac2001-08-23 17:05:04 +0000207 Options.erase(Options.begin()+i);
208 --i;
209 }
210 }
211
212 Value *PickedMeth = ChooseOneOption(Name, Options);
213 if (PickedMeth == 0)
214 return true;
215
Chris Lattner9636a912001-10-01 16:18:37 +0000216 Method *M = cast<Method>(PickedMeth);
Chris Lattner365a76e2001-09-10 04:49:44 +0000217
218 vector<GenericValue> Args;
219 // TODO, get args from user...
220
221 callMethod(M, Args); // Start executing it...
Chris Lattner92101ac2001-08-23 17:05:04 +0000222
223 // Reset the current frame location to the top of stack
224 CurFrame = ECStack.size()-1;
225
226 return false;
227}
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000228
Chris Lattner204eec32001-10-27 05:54:31 +0000229static void *CreateArgv(const vector<string> &InputArgv) {
230 // Pointers are 64 bits...
231 uint64_t *Result = new uint64_t[InputArgv.size()+1];
232
233 for (unsigned i = 0; i < InputArgv.size(); ++i) {
234 unsigned Size = InputArgv[i].size()+1;
235 char *Dest = new char[Size];
236 copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
237 Dest[Size-1] = 0;
238 Result[i] = (uint64_t)Dest;
239 }
240
241 Result[InputArgv.size()] = 0;
242 return Result;
243}
244
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000245
246// callMainMethod - This is a nasty gross hack that will dissapear when
247// callMethod can parse command line options and stuff for us.
248//
249bool Interpreter::callMainMethod(const string &Name,
Chris Lattner204eec32001-10-27 05:54:31 +0000250 const vector<string> &InputArgv) {
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000251 vector<Value*> Options = LookupMatchingNames(Name);
252
253 for (unsigned i = 0; i < Options.size(); ++i) { // Remove nonmethod matches...
254 if (!isa<Method>(Options[i])) {
255 Options.erase(Options.begin()+i);
256 --i;
257 }
258 }
259
260 Value *PickedMeth = ChooseOneOption(Name, Options);
261 if (PickedMeth == 0)
262 return true;
263
264 Method *M = cast<Method>(PickedMeth);
265 const MethodType *MT = M->getMethodType();
266
267 vector<GenericValue> Args;
268 switch (MT->getParamTypes().size()) {
269 default:
270 cout << "Unknown number of arguments to synthesize for '" << Name << "'!\n";
271 return true;
272 case 2: {
273 PointerType *SPP = PointerType::get(PointerType::get(Type::SByteTy));
274 if (MT->getParamTypes()[1] != SPP) {
275 cout << "Second argument of '" << Name << "' should have type: '"
276 << SPP->getDescription() << "'!\n";
277 return true;
278 }
Chris Lattner204eec32001-10-27 05:54:31 +0000279
Chris Lattnerc2593162001-10-27 08:28:11 +0000280 GenericValue GV; GV.PointerVal = (uint64_t)CreateArgv(InputArgv);
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000281 Args.push_back(GV);
282 }
283 // fallthrough
284 case 1:
285 if (!MT->getParamTypes()[0]->isIntegral()) {
286 cout << "First argument of '" << Name << "' should be integral!\n";
287 return true;
288 } else {
Chris Lattner204eec32001-10-27 05:54:31 +0000289 GenericValue GV; GV.UIntVal = InputArgv.size();
Chris Lattnerf8f2afb2001-10-18 21:55:32 +0000290 Args.insert(Args.begin(), GV);
291 }
292 // fallthrough
293 case 0:
294 break;
295 }
296
297 callMethod(M, Args); // Start executing it...
298
299 // Reset the current frame location to the top of stack
300 CurFrame = ECStack.size()-1;
301
302 return false;
303}