1 //===-- UserInput.cpp - Interpreter Input Loop support --------------------===//
3 // This file implements the interpreter Input I/O loop.
5 //===----------------------------------------------------------------------===//
7 #include "Interpreter.h"
8 #include "llvm/Bytecode/Reader.h"
9 #include "llvm/DerivedTypes.h"
10 #include "llvm/Transforms/Linker.h"
18 Print, Info, List, StackTrace, Up, Down, // Inspection
19 Next, Step, Run, Finish, Call, // Control flow changes
20 Break, Watch, // Debugging
22 TraceOpt, ProfileOpt // Toggle features
25 // CommandTable - Build a lookup table for the commands available to the user...
26 static struct CommandTableElement {
30 inline bool operator<(const CommandTableElement &E) const {
31 return string(Name) < string(E.Name);
33 inline bool operator==(const string &S) const {
34 return string(Name) == S;
37 { "quit" , Quit }, { "q", Quit }, { "", Quit }, // Empty str = eof
38 { "help" , Help }, { "h", Help },
40 { "print" , Print }, { "p", Print },
43 { "backtrace", StackTrace }, { "bt", StackTrace }, { "where", StackTrace },
47 { "next" , Next }, { "n", Next },
48 { "step" , Step }, { "s", Step },
50 { "finish" , Finish },
53 { "break" , Break }, { "b", Break },
59 { "trace" , TraceOpt },
60 { "profile" , ProfileOpt },
62 static CommandTableElement *CommandTableEnd =
63 CommandTable+sizeof(CommandTable)/sizeof(CommandTable[0]);
66 //===----------------------------------------------------------------------===//
67 // handleUserInput - Enter the input loop for the interpreter. This function
68 // returns when the user quits the interpreter.
70 void Interpreter::handleUserInput() {
71 bool UserQuit = false;
74 std::sort(CommandTable, CommandTableEnd);
76 // Print the instruction that we are stopped at...
77 printCurrentInstruction();
81 cout << "lli> " << std::flush;
84 CommandTableElement *E = find(CommandTable, CommandTableEnd, Command);
86 if (E == CommandTableEnd) {
87 cout << "Error: '" << Command << "' not recognized!\n";
92 case Quit: UserQuit = true; break;
97 case Flush: flushModule(); break;
107 case List: list(); break;
108 case StackTrace: printStackTrace(); break;
110 if (CurFrame > 0) { --CurFrame; printStackFrame(); }
111 else cout << "Error: Already at root of stack!\n";
114 if ((unsigned)CurFrame < ECStack.size()-1) {
118 cout << "Error: Already at bottom of stack!\n";
120 case Next: nextInstruction(); break;
121 case Step: stepInstruction(); break;
122 case Run: run(); break;
123 case Finish: finish(); break;
126 callMethod(Command); // Enter the specified function
127 finish(); // Run until it's complete
132 cout << "Tracing " << (Trace ? "enabled\n" : "disabled\n");
137 cout << "Profiling " << (Trace ? "enabled\n" : "disabled\n");
141 cout << "Command '" << Command << "' unimplemented!\n";
148 //===----------------------------------------------------------------------===//
149 // loadModule - Load a new module to execute...
151 void Interpreter::loadModule(const string &Filename) {
153 if (CurMod && !flushModule()) return; // Kill current execution
155 CurMod = ParseBytecodeFile(Filename, &ErrorMsg);
157 cout << "Error parsing '" << Filename << "': No module loaded: "
161 CW.setModule(CurMod); // Update Writer
164 string RuntimeLib = getCurrentExecutablePath();
165 if (!RuntimeLib.empty()) RuntimeLib += "/";
166 RuntimeLib += "RuntimeLib.bc";
168 if (Module *SupportLib = ParseBytecodeFile(RuntimeLib, &ErrorMsg)) {
169 if (LinkModules(CurMod, SupportLib, &ErrorMsg))
170 std::cerr << "Error Linking runtime library into current module: "
173 std::cerr << "Error loading runtime library '"+RuntimeLib+"': "
180 //===----------------------------------------------------------------------===//
181 // flushModule - Return true if the current program has been unloaded.
183 bool Interpreter::flushModule() {
185 cout << "Error flushing: No module loaded!\n";
189 if (!ECStack.empty()) {
190 // TODO: if use is not sure, return false
191 cout << "Killing current execution!\n";
203 //===----------------------------------------------------------------------===//
204 // setBreakpoint - Enable a breakpoint at the specified location
206 void Interpreter::setBreakpoint(const string &Name) {
207 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
208 // TODO: Set a breakpoint on PickedVal
211 //===----------------------------------------------------------------------===//
212 // callMethod - Enter the specified method...
214 bool Interpreter::callMethod(const string &Name) {
215 std::vector<Value*> Options = LookupMatchingNames(Name);
217 for (unsigned i = 0; i < Options.size(); ++i) { // Remove non-fn matches...
218 if (!isa<Function>(Options[i])) {
219 Options.erase(Options.begin()+i);
224 Value *PickedMeth = ChooseOneOption(Name, Options);
228 Function *F = cast<Function>(PickedMeth);
230 std::vector<GenericValue> Args;
231 // TODO, get args from user...
233 callMethod(F, Args); // Start executing it...
235 // Reset the current frame location to the top of stack
236 CurFrame = ECStack.size()-1;
241 static void *CreateArgv(const std::vector<string> &InputArgv) {
242 // Pointers are 64 bits...
243 uint64_t *Result = new PointerTy[InputArgv.size()+1];
245 for (unsigned i = 0; i < InputArgv.size(); ++i) {
246 unsigned Size = InputArgv[i].size()+1;
247 char *Dest = new char[Size];
248 copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
250 Result[i] = (PointerTy)Dest;
253 Result[InputArgv.size()] = 0;
258 // callMainMethod - This is a nasty gross hack that will dissapear when
259 // callMethod can parse command line options and stuff for us.
261 bool Interpreter::callMainMethod(const string &Name,
262 const std::vector<string> &InputArgv) {
263 std::vector<Value*> Options = LookupMatchingNames(Name);
265 for (unsigned i = 0; i < Options.size(); ++i) { // Remove non-fn matches...
266 if (!isa<Function>(Options[i])) {
267 Options.erase(Options.begin()+i);
272 Value *PickedMeth = ChooseOneOption(Name, Options);
276 Function *M = cast<Function>(PickedMeth);
277 const FunctionType *MT = M->getFunctionType();
279 std::vector<GenericValue> Args;
280 switch (MT->getParamTypes().size()) {
282 cout << "Unknown number of arguments to synthesize for '" << Name << "'!\n";
285 PointerType *SPP = PointerType::get(PointerType::get(Type::SByteTy));
286 if (MT->getParamTypes()[1] != SPP) {
287 CW << "Second argument of '" << Name << "' should have type: '"
292 GenericValue GV; GV.PointerVal = (uint64_t)CreateArgv(InputArgv);
297 if (!MT->getParamTypes()[0]->isIntegral()) {
298 cout << "First argument of '" << Name << "' should be integral!\n";
301 GenericValue GV; GV.UIntVal = InputArgv.size();
302 Args.insert(Args.begin(), GV);
309 callMethod(M, Args); // Start executing it...
311 // Reset the current frame location to the top of stack
312 CurFrame = ECStack.size()-1;
319 void Interpreter::list() {
321 cout << "Error: No program executing!\n";
323 CW << ECStack[CurFrame].CurMethod; // Just print the function out...
326 void Interpreter::printStackTrace() {
327 if (ECStack.empty()) cout << "No program executing!\n";
329 for (unsigned i = 0; i < ECStack.size(); ++i) {
330 printStackFrame((int)i);