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/Assembly/Writer.h"
10 #include "llvm/DerivedTypes.h"
15 Print, Info, List, StackTrace, Up, Down, // Inspection
16 Next, Step, Run, Finish, Call, // Control flow changes
17 Break, Watch, // Debugging
21 // CommandTable - Build a lookup table for the commands available to the user...
22 static struct CommandTableElement {
26 inline bool operator<(const CommandTableElement &E) const {
27 return string(Name) < string(E.Name);
29 inline bool operator==(const string &S) const {
30 return string(Name) == S;
33 { "quit" , Quit }, { "q", Quit }, { "", Quit }, // Empty str = eof
34 { "help" , Help }, { "h", Help },
36 { "print" , Print }, { "p", Print },
39 { "backtrace", StackTrace }, { "bt", StackTrace }, { "where", StackTrace },
43 { "next" , Next }, { "n", Next },
44 { "step" , Step }, { "s", Step },
46 { "finish" , Finish },
49 { "break" , Break }, { "b", Break },
55 static CommandTableElement *CommandTableEnd =
56 CommandTable+sizeof(CommandTable)/sizeof(CommandTable[0]);
59 //===----------------------------------------------------------------------===//
60 // handleUserInput - Enter the input loop for the interpreter. This function
61 // returns when the user quits the interpreter.
63 void Interpreter::handleUserInput() {
64 bool UserQuit = false;
67 sort(CommandTable, CommandTableEnd);
69 // Print the instruction that we are stopped at...
70 printCurrentInstruction();
74 cout << "lli> " << flush;
77 CommandTableElement *E = find(CommandTable, CommandTableEnd, Command);
79 if (E == CommandTableEnd) {
80 cout << "Error: '" << Command << "' not recognized!\n";
85 case Quit: UserQuit = true; break;
90 case Flush: flushModule(); break;
100 case List: list(); break;
101 case StackTrace: printStackTrace(); break;
103 if (CurFrame > 0) --CurFrame;
104 else cout << "Error: Already at root of stack!\n";
107 if ((unsigned)CurFrame < ECStack.size()-1) ++CurFrame;
108 else cout << "Error: Already at bottom of stack!\n";
110 case Next: nextInstruction(); break;
111 case Step: stepInstruction(); break;
112 case Run: run(); break;
113 case Finish: finish(); break;
116 callMethod(Command); // Enter the specified method
117 finish(); // Run until it's complete
121 cout << "Command '" << Command << "' unimplemented!\n";
128 //===----------------------------------------------------------------------===//
129 // loadModule - Load a new module to execute...
131 void Interpreter::loadModule(const string &Filename) {
132 if (CurMod && !flushModule()) return; // Kill current execution
134 CurMod = ParseBytecodeFile(Filename);
136 cout << "Error parsing '" << Filename << "': No module loaded.\n";
140 // TODO: link in support library...
144 //===----------------------------------------------------------------------===//
145 // flushModule - Return true if the current program has been unloaded.
147 bool Interpreter::flushModule() {
149 cout << "Error flushing: No module loaded!\n";
153 if (!ECStack.empty()) {
154 // TODO: if use is not sure, return false
155 cout << "Killing current execution!\n";
166 //===----------------------------------------------------------------------===//
167 // setBreakpoint - Enable a breakpoint at the specified location
169 void Interpreter::setBreakpoint(const string &Name) {
170 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
171 // TODO: Set a breakpoint on PickedVal
174 //===----------------------------------------------------------------------===//
175 // callMethod - Enter the specified method...
177 bool Interpreter::callMethod(const string &Name) {
178 vector<Value*> Options = LookupMatchingNames(Name);
180 for (unsigned i = 0; i < Options.size(); ++i) { // Remove nonmethod matches...
181 if (!isa<Method>(Options[i])) {
182 Options.erase(Options.begin()+i);
187 Value *PickedMeth = ChooseOneOption(Name, Options);
191 Method *M = cast<Method>(PickedMeth);
193 vector<GenericValue> Args;
194 // TODO, get args from user...
196 callMethod(M, Args); // Start executing it...
198 // Reset the current frame location to the top of stack
199 CurFrame = ECStack.size()-1;
205 // callMainMethod - This is a nasty gross hack that will dissapear when
206 // callMethod can parse command line options and stuff for us.
208 bool Interpreter::callMainMethod(const string &Name,
209 const string &InputFilename) {
210 vector<Value*> Options = LookupMatchingNames(Name);
212 for (unsigned i = 0; i < Options.size(); ++i) { // Remove nonmethod matches...
213 if (!isa<Method>(Options[i])) {
214 Options.erase(Options.begin()+i);
219 Value *PickedMeth = ChooseOneOption(Name, Options);
223 Method *M = cast<Method>(PickedMeth);
224 const MethodType *MT = M->getMethodType();
226 vector<GenericValue> Args;
227 switch (MT->getParamTypes().size()) {
229 cout << "Unknown number of arguments to synthesize for '" << Name << "'!\n";
232 PointerType *SPP = PointerType::get(PointerType::get(Type::SByteTy));
233 if (MT->getParamTypes()[1] != SPP) {
234 cout << "Second argument of '" << Name << "' should have type: '"
235 << SPP->getDescription() << "'!\n";
239 GenericValue GV; GV.PointerVal = 0;
244 if (!MT->getParamTypes()[0]->isIntegral()) {
245 cout << "First argument of '" << Name << "' should be integral!\n";
248 GenericValue GV; GV.IntVal = 1;
249 Args.insert(Args.begin(), GV);
256 callMethod(M, Args); // Start executing it...
258 // Reset the current frame location to the top of stack
259 CurFrame = ECStack.size()-1;