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"
11 #include "llvm/Transforms/Linker.h"
19 Print, Info, List, StackTrace, Up, Down, // Inspection
20 Next, Step, Run, Finish, Call, // Control flow changes
21 Break, Watch, // Debugging
23 TraceOpt, ProfileOpt // Toggle features
26 // CommandTable - Build a lookup table for the commands available to the user...
27 static struct CommandTableElement {
31 inline bool operator<(const CommandTableElement &E) const {
32 return string(Name) < string(E.Name);
34 inline bool operator==(const string &S) const {
35 return string(Name) == S;
38 { "quit" , Quit }, { "q", Quit }, { "", Quit }, // Empty str = eof
39 { "help" , Help }, { "h", Help },
41 { "print" , Print }, { "p", Print },
44 { "backtrace", StackTrace }, { "bt", StackTrace }, { "where", StackTrace },
48 { "next" , Next }, { "n", Next },
49 { "step" , Step }, { "s", Step },
51 { "finish" , Finish },
54 { "break" , Break }, { "b", Break },
60 { "trace" , TraceOpt },
61 { "profile" , ProfileOpt },
63 static CommandTableElement *CommandTableEnd =
64 CommandTable+sizeof(CommandTable)/sizeof(CommandTable[0]);
67 //===----------------------------------------------------------------------===//
68 // handleUserInput - Enter the input loop for the interpreter. This function
69 // returns when the user quits the interpreter.
71 void Interpreter::handleUserInput() {
72 bool UserQuit = false;
75 std::sort(CommandTable, CommandTableEnd);
77 // Print the instruction that we are stopped at...
78 printCurrentInstruction();
82 cout << "lli> " << std::flush;
85 CommandTableElement *E = find(CommandTable, CommandTableEnd, Command);
87 if (E == CommandTableEnd) {
88 cout << "Error: '" << Command << "' not recognized!\n";
93 case Quit: UserQuit = true; break;
98 case Flush: flushModule(); break;
108 case List: list(); break;
109 case StackTrace: printStackTrace(); break;
111 if (CurFrame > 0) { --CurFrame; printStackFrame(); }
112 else cout << "Error: Already at root of stack!\n";
115 if ((unsigned)CurFrame < ECStack.size()-1) {
119 cout << "Error: Already at bottom of stack!\n";
121 case Next: nextInstruction(); break;
122 case Step: stepInstruction(); break;
123 case Run: run(); break;
124 case Finish: finish(); break;
127 callMethod(Command); // Enter the specified function
128 finish(); // Run until it's complete
133 cout << "Tracing " << (Trace ? "enabled\n" : "disabled\n");
138 cout << "Profiling " << (Trace ? "enabled\n" : "disabled\n");
142 cout << "Command '" << Command << "' unimplemented!\n";
149 //===----------------------------------------------------------------------===//
150 // loadModule - Load a new module to execute...
152 void Interpreter::loadModule(const string &Filename) {
154 if (CurMod && !flushModule()) return; // Kill current execution
156 CurMod = ParseBytecodeFile(Filename, &ErrorMsg);
158 cout << "Error parsing '" << Filename << "': No module loaded: "
162 CW.setModule(CurMod); // Update Writer
165 string RuntimeLib = getCurrentExecutablePath();
166 if (!RuntimeLib.empty()) RuntimeLib += "/";
167 RuntimeLib += "RuntimeLib.bc";
169 if (Module *SupportLib = ParseBytecodeFile(RuntimeLib, &ErrorMsg)) {
170 if (LinkModules(CurMod, SupportLib, &ErrorMsg))
171 std::cerr << "Error Linking runtime library into current module: "
174 std::cerr << "Error loading runtime library '"+RuntimeLib+"': "
181 //===----------------------------------------------------------------------===//
182 // flushModule - Return true if the current program has been unloaded.
184 bool Interpreter::flushModule() {
186 cout << "Error flushing: No module loaded!\n";
190 if (!ECStack.empty()) {
191 // TODO: if use is not sure, return false
192 cout << "Killing current execution!\n";
204 //===----------------------------------------------------------------------===//
205 // setBreakpoint - Enable a breakpoint at the specified location
207 void Interpreter::setBreakpoint(const string &Name) {
208 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
209 // TODO: Set a breakpoint on PickedVal
212 //===----------------------------------------------------------------------===//
213 // callMethod - Enter the specified method...
215 bool Interpreter::callMethod(const string &Name) {
216 std::vector<Value*> Options = LookupMatchingNames(Name);
218 for (unsigned i = 0; i < Options.size(); ++i) { // Remove non-fn matches...
219 if (!isa<Function>(Options[i])) {
220 Options.erase(Options.begin()+i);
225 Value *PickedMeth = ChooseOneOption(Name, Options);
229 Function *F = cast<Function>(PickedMeth);
231 std::vector<GenericValue> Args;
232 // TODO, get args from user...
234 callMethod(F, Args); // Start executing it...
236 // Reset the current frame location to the top of stack
237 CurFrame = ECStack.size()-1;
242 static void *CreateArgv(const std::vector<string> &InputArgv) {
243 // Pointers are 64 bits...
244 uint64_t *Result = new PointerTy[InputArgv.size()+1];
246 for (unsigned i = 0; i < InputArgv.size(); ++i) {
247 unsigned Size = InputArgv[i].size()+1;
248 char *Dest = new char[Size];
249 copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
251 Result[i] = (PointerTy)Dest;
254 Result[InputArgv.size()] = 0;
259 // callMainMethod - This is a nasty gross hack that will dissapear when
260 // callMethod can parse command line options and stuff for us.
262 bool Interpreter::callMainMethod(const string &Name,
263 const std::vector<string> &InputArgv) {
264 std::vector<Value*> Options = LookupMatchingNames(Name);
266 for (unsigned i = 0; i < Options.size(); ++i) { // Remove non-fn matches...
267 if (!isa<Function>(Options[i])) {
268 Options.erase(Options.begin()+i);
273 Value *PickedMeth = ChooseOneOption(Name, Options);
277 Function *M = cast<Function>(PickedMeth);
278 const FunctionType *MT = M->getFunctionType();
280 std::vector<GenericValue> Args;
281 switch (MT->getParamTypes().size()) {
283 cout << "Unknown number of arguments to synthesize for '" << Name << "'!\n";
286 PointerType *SPP = PointerType::get(PointerType::get(Type::SByteTy));
287 if (MT->getParamTypes()[1] != SPP) {
288 CW << "Second argument of '" << Name << "' should have type: '"
293 GenericValue GV; GV.PointerVal = (uint64_t)CreateArgv(InputArgv);
298 if (!MT->getParamTypes()[0]->isIntegral()) {
299 cout << "First argument of '" << Name << "' should be integral!\n";
302 GenericValue GV; GV.UIntVal = InputArgv.size();
303 Args.insert(Args.begin(), GV);
310 callMethod(M, Args); // Start executing it...
312 // Reset the current frame location to the top of stack
313 CurFrame = ECStack.size()-1;
320 void Interpreter::list() {
322 cout << "Error: No program executing!\n";
324 CW << ECStack[CurFrame].CurMethod; // Just print the function out...
327 void Interpreter::printStackTrace() {
328 if (ECStack.empty()) cout << "No program executing!\n";
330 for (unsigned i = 0; i < ECStack.size(); ++i) {
331 printStackFrame((int)i);