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/Function.h"
11 #include "llvm/Transforms/Utils/Linker.h"
16 Print, Info, List, StackTrace, Up, Down, // Inspection
17 Next, Step, Run, Finish, Call, // Control flow changes
18 Break, Watch, // Debugging
20 TraceOpt, // Toggle features
23 // CommandTable - Build a lookup table for the commands available to the user...
24 static struct CommandTableElement {
28 inline bool operator<(const CommandTableElement &E) const {
29 return std::string(Name) < std::string(E.Name);
31 inline bool operator==(const std::string &S) const {
32 return std::string(Name) == S;
35 { "quit" , Quit }, { "q", Quit }, { "", Quit }, // Empty str = eof
36 { "help" , Help }, { "h", Help },
38 { "print" , Print }, { "p", Print },
41 { "backtrace", StackTrace }, { "bt", StackTrace }, { "where", StackTrace },
45 { "next" , Next }, { "n", Next },
46 { "step" , Step }, { "s", Step },
48 { "finish" , Finish },
51 { "break" , Break }, { "b", Break },
56 { "trace" , TraceOpt },
58 static CommandTableElement *CommandTableEnd =
59 CommandTable+sizeof(CommandTable)/sizeof(CommandTable[0]);
62 //===----------------------------------------------------------------------===//
63 // handleUserInput - Enter the input loop for the interpreter. This function
64 // returns when the user quits the interpreter.
66 void Interpreter::handleUserInput() {
67 bool UserQuit = false;
70 std::sort(CommandTable, CommandTableEnd);
72 // Print the instruction that we are stopped at...
73 printCurrentInstruction();
77 std::cout << "lli> " << std::flush;
80 CommandTableElement *E = find(CommandTable, CommandTableEnd, Command);
82 if (E == CommandTableEnd) {
83 std::cout << "Error: '" << Command << "' not recognized!\n";
88 case Quit: UserQuit = true; break;
98 case List: list(); break;
99 case StackTrace: printStackTrace(); break;
101 if (CurFrame > 0) { --CurFrame; printStackFrame(); }
102 else std::cout << "Error: Already at root of stack!\n";
105 if ((unsigned)CurFrame < ECStack.size()-1) {
109 std::cout << "Error: Already at bottom of stack!\n";
111 case Next: nextInstruction(); break;
112 case Step: stepInstruction(); break;
113 case Run: run(); break;
114 case Finish: finish(); break;
117 callFunction(Command); // Enter the specified function
118 finish(); // Run until it's complete
123 std::cout << "Tracing " << (Trace ? "enabled\n" : "disabled\n");
127 std::cout << "Command '" << Command << "' unimplemented!\n";
132 AtExitHandlers.clear();
135 //===----------------------------------------------------------------------===//
136 // setBreakpoint - Enable a breakpoint at the specified location
138 void Interpreter::setBreakpoint(const std::string &Name) {
139 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
140 // TODO: Set a breakpoint on PickedVal
143 //===----------------------------------------------------------------------===//
144 // callFunction - Enter the specified function...
146 bool Interpreter::callFunction(const std::string &Name) {
147 std::vector<Value*> Options = LookupMatchingNames(Name);
149 for (unsigned i = 0; i < Options.size(); ++i) { // Remove non-fn matches...
150 if (!isa<Function>(Options[i])) {
151 Options.erase(Options.begin()+i);
156 Value *PickedMeth = ChooseOneOption(Name, Options);
160 Function *F = cast<Function>(PickedMeth);
162 std::vector<GenericValue> Args;
163 // TODO, get args from user...
165 callFunction(F, Args); // Start executing it...
167 // Reset the current frame location to the top of stack
168 CurFrame = ECStack.size()-1;
173 // callMainFunction - This is a nasty gross hack that will dissapear when
174 // callFunction can parse command line options and stuff for us.
176 bool Interpreter::callMainFunction(const std::string &Name,
177 const std::vector<std::string> &InputArgv) {
178 std::vector<Value*> Options = LookupMatchingNames(Name);
180 for (unsigned i = 0; i < Options.size(); ++i) { // Remove non-fn matches...
181 if (!isa<Function>(Options[i])) {
182 Options.erase(Options.begin()+i);
187 Value *PickedMeth = ChooseOneOption(Name, Options);
191 Function *M = cast<Function>(PickedMeth);
192 const FunctionType *MT = M->getFunctionType();
194 std::vector<GenericValue> Args;
195 switch (MT->getParamTypes().size()) {
197 std::cout << "Unknown number of arguments to synthesize for '" << Name
201 PointerType *SPP = PointerType::get(PointerType::get(Type::SByteTy));
202 if (MT->getParamTypes()[1] != SPP) {
203 CW << "Second argument of '" << Name << "' should have type: '"
208 Args.push_back(PTOGV(CreateArgv(InputArgv)));
212 if (!MT->getParamTypes()[0]->isInteger()) {
213 std::cout << "First argument of '" << Name << "' should be an integer!\n";
216 GenericValue GV; GV.UIntVal = InputArgv.size();
217 Args.insert(Args.begin(), GV);
224 callFunction(M, Args); // Start executing it...
226 // Reset the current frame location to the top of stack
227 CurFrame = ECStack.size()-1;
234 void Interpreter::list() {
236 std::cout << "Error: No program executing!\n";
238 CW << ECStack[CurFrame].CurFunction; // Just print the function out...
241 void Interpreter::printStackTrace() {
242 if (ECStack.empty()) std::cout << "No program executing!\n";
244 for (unsigned i = 0; i < ECStack.size(); ++i) {
245 printStackFrame((int)i);