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/Assembly/Writer.h"
13 Print, Info, List, StackTrace, Up, Down, // Inspection
14 Next, Step, Run, Finish, Call, // Control flow changes
15 Break, Watch, // Debugging
19 // CommandTable - Build a lookup table for the commands available to the user...
20 static struct CommandTableElement {
24 inline bool operator<(const CommandTableElement &E) const {
25 return string(Name) < string(E.Name);
27 inline bool operator==(const string &S) const {
28 return string(Name) == S;
31 { "quit" , Quit }, { "q", Quit }, { "", Quit }, // Empty str = eof
32 { "help" , Help }, { "h", Help },
34 { "print" , Print }, { "p", Print },
37 { "backtrace", StackTrace }, { "bt", StackTrace }, { "where", StackTrace },
41 { "next" , Next }, { "n", Next },
42 { "step" , Step }, { "s", Step },
44 { "finish" , Finish },
47 { "break" , Break }, { "b", Break },
53 static CommandTableElement *CommandTableEnd =
54 CommandTable+sizeof(CommandTable)/sizeof(CommandTable[0]);
57 //===----------------------------------------------------------------------===//
58 // handleUserInput - Enter the input loop for the interpreter. This function
59 // returns when the user quits the interpreter.
61 void Interpreter::handleUserInput() {
62 bool UserQuit = false;
65 sort(CommandTable, CommandTableEnd);
67 // Print the instruction that we are stopped at...
68 printCurrentInstruction();
72 cout << "lli> " << flush;
75 CommandTableElement *E = find(CommandTable, CommandTableEnd, Command);
77 if (E == CommandTableEnd) {
78 cout << "Error: '" << Command << "' not recognized!\n";
83 case Quit: UserQuit = true; break;
93 case List: list(); break;
94 case StackTrace: printStackTrace(); break;
96 if (CurFrame > 0) --CurFrame;
97 else cout << "Error: Already at root of stack!\n";
100 if ((unsigned)CurFrame < ECStack.size()-1) ++CurFrame;
101 else cout << "Error: Already at bottom of stack!\n";
103 case Next: nextInstruction(); break;
104 case Step: stepInstruction(); break;
105 case Run: run(); break;
106 case Finish: finish(); break;
109 callMethod(Command); // Enter the specified method
110 finish(); // Run until it's complete
114 cout << "Command '" << Command << "' unimplemented!\n";
122 //===----------------------------------------------------------------------===//
123 // setBreakpoint - Enable a breakpoint at the specified location
125 void Interpreter::setBreakpoint(const string &Name) {
126 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
127 // TODO: Set a breakpoint on PickedVal
130 //===----------------------------------------------------------------------===//
131 // callMethod - Enter the specified method...
133 bool Interpreter::callMethod(const string &Name) {
134 vector<Value*> Options = LookupMatchingNames(Name);
136 for (unsigned i = 0; i < Options.size(); ++i) { // Remove nonmethod matches...
137 if (!Options[i]->isMethod()) {
138 Options.erase(Options.begin()+i);
143 Value *PickedMeth = ChooseOneOption(Name, Options);
147 Method *M = PickedMeth->castMethodAsserting();
149 vector<GenericValue> Args;
150 // TODO, get args from user...
152 callMethod(M, Args); // Start executing it...
154 // Reset the current frame location to the top of stack
155 CurFrame = ECStack.size()-1;