blob: cfa74f1ffa5824fd0b6e7b46ffb289f3bbbd13b0 [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"
8#include "llvm/Assembly/Writer.h"
9#include <algorithm>
10
11enum CommandID {
12 Quit, Help, // Basics
13 Print, List, StackTrace, Up, Down, // Inspection
14 Next, Step, Run, Finish, Call, // Control flow changes
15 Break, Watch, // Debugging
16 Load, Flush
17};
18
19// CommandTable - Build a lookup table for the commands available to the user...
20static struct CommandTableElement {
21 const char *Name;
22 enum CommandID CID;
23
24 inline bool operator<(const CommandTableElement &E) const {
25 return string(Name) < string(E.Name);
26 }
27 inline bool operator==(const string &S) const {
28 return string(Name) == S;
29 }
30} CommandTable[] = {
31 { "quit" , Quit }, { "q", Quit }, { "", Quit }, // Empty str = eof
32 { "help" , Help }, { "h", Help },
33
34 { "print" , Print }, { "p", Print },
35 { "list" , List },
36 { "backtrace", StackTrace }, { "bt", StackTrace }, { "where", StackTrace },
37 { "up" , Up },
38 { "down" , Down },
39
40 { "next" , Next }, { "n", Next },
41 { "step" , Step }, { "s", Step },
42 { "run" , Run },
43 { "finish" , Finish },
44 { "call" , Call },
45
46 { "break" , Break }, { "b", Break },
47 { "watch" , Watch },
48
49 { "load" , Load },
50 { "flush" , Flush },
51};
52static CommandTableElement *CommandTableEnd =
53 CommandTable+sizeof(CommandTable)/sizeof(CommandTable[0]);
54
55
56//===----------------------------------------------------------------------===//
57// handleUserInput - Enter the input loop for the interpreter. This function
58// returns when the user quits the interpreter.
59//
60void Interpreter::handleUserInput() {
61 bool UserQuit = false;
62
63 // Sort the table...
64 sort(CommandTable, CommandTableEnd);
65
66 // Print the instruction that we are stopped at...
67 printCurrentInstruction();
68
69 do {
70 string Command;
71 cout << "lli> " << flush;
72 cin >> Command;
73
74 CommandTableElement *E = find(CommandTable, CommandTableEnd, Command);
75
76 if (E == CommandTableEnd) {
77 cout << "Error: '" << Command << "' not recognized!\n";
78 continue;
79 }
80
81 switch (E->CID) {
82 case Quit: UserQuit = true; break;
83 case Print:
84 cin >> Command;
85 printValue(Command);
86 break;
87 case List: list(); break;
88 case StackTrace: printStackTrace(); break;
89 case Up:
90 if (CurFrame > 0) --CurFrame;
91 else cout << "Error: Already at root of stack!\n";
92 break;
93 case Down:
94 if ((unsigned)CurFrame < ECStack.size()-1) ++CurFrame;
95 else cout << "Error: Already at bottom of stack!\n";
96 break;
97 case Next: nextInstruction(); break;
98 case Step: stepInstruction(); break;
99 case Run: run(); break;
100 case Finish: finish(); break;
101 case Call:
102 cin >> Command;
103 callMethod(Command); // Enter the specified method
104 finish(); // Run until it's complete
105 break;
106
107 default:
108 cout << "Command '" << Command << "' unimplemented!\n";
109 break;
110 }
111
112 } while (!UserQuit);
113}
114
115
116//===----------------------------------------------------------------------===//
117// setBreakpoint - Enable a breakpoint at the specified location
118//
119void Interpreter::setBreakpoint(const string &Name) {
120 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
121 // TODO: Set a breakpoint on PickedVal
122}
123
124//===----------------------------------------------------------------------===//
125// callMethod - Enter the specified method...
126//
127bool Interpreter::callMethod(const string &Name) {
128 vector<Value*> Options = LookupMatchingNames(Name);
129
130 for (unsigned i = 0; i < Options.size(); ++i) { // Remove nonmethod matches...
131 if (!Options[i]->isMethod()) {
132 Options.erase(Options.begin()+i);
133 --i;
134 }
135 }
136
137 Value *PickedMeth = ChooseOneOption(Name, Options);
138 if (PickedMeth == 0)
139 return true;
140
141 callMethod(PickedMeth->castMethodAsserting()); // Start executing it...
142
143 // Reset the current frame location to the top of stack
144 CurFrame = ECStack.size()-1;
145
146 return false;
147}