Genericize support for calling functions a bit
[oota-llvm.git] / lib / ExecutionEngine / Interpreter / UserInput.cpp
1 //===-- 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
11 enum CommandID {
12   Quit, Help,                                 // Basics
13   Print, Info, 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...
20 static 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   { "info"     , Info       },
37   { "backtrace", StackTrace }, { "bt", StackTrace }, { "where", StackTrace },
38   { "up"       , Up         },
39   { "down"     , Down       },
40
41   { "next"     , Next       }, { "n", Next },
42   { "step"     , Step       }, { "s", Step },
43   { "run"      , Run        },
44   { "finish"   , Finish     },
45   { "call"     , Call       },
46
47   { "break"    , Break      }, { "b", Break },
48   { "watch"    , Watch      },
49
50   { "load"     , Load       },
51   { "flush"    , Flush      },
52 };
53 static CommandTableElement *CommandTableEnd = 
54    CommandTable+sizeof(CommandTable)/sizeof(CommandTable[0]);
55
56
57 //===----------------------------------------------------------------------===//
58 // handleUserInput - Enter the input loop for the interpreter.  This function
59 // returns when the user quits the interpreter.
60 //
61 void Interpreter::handleUserInput() {
62   bool UserQuit = false;
63
64   // Sort the table...
65   sort(CommandTable, CommandTableEnd);
66
67   // Print the instruction that we are stopped at...
68   printCurrentInstruction();
69
70   do {
71     string Command;
72     cout << "lli> " << flush;
73     cin >> Command;
74
75     CommandTableElement *E = find(CommandTable, CommandTableEnd, Command);
76
77     if (E == CommandTableEnd) {
78       cout << "Error: '" << Command << "' not recognized!\n";
79       continue;
80     }
81
82     switch (E->CID) {
83     case Quit:       UserQuit = true;   break;
84     case Print:
85       cin >> Command;
86       printValue(Command);
87       break;
88     case Info:
89       cin >> Command;
90       infoValue(Command);
91       break;
92      
93     case List:       list();            break;
94     case StackTrace: printStackTrace(); break;
95     case Up: 
96       if (CurFrame > 0) --CurFrame;
97       else cout << "Error: Already at root of stack!\n";
98       break;
99     case Down:
100       if ((unsigned)CurFrame < ECStack.size()-1) ++CurFrame;
101       else cout << "Error: Already at bottom of stack!\n";
102       break;
103     case Next:       nextInstruction(); break;
104     case Step:       stepInstruction(); break;
105     case Run:        run();             break;
106     case Finish:     finish();          break;
107     case Call:
108       cin >> Command;
109       callMethod(Command);    // Enter the specified method
110       finish();               // Run until it's complete
111       break;
112
113     default:
114       cout << "Command '" << Command << "' unimplemented!\n";
115       break;
116     }
117
118   } while (!UserQuit);
119 }
120
121
122 //===----------------------------------------------------------------------===//
123 // setBreakpoint - Enable a breakpoint at the specified location
124 //
125 void Interpreter::setBreakpoint(const string &Name) {
126   Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
127   // TODO: Set a breakpoint on PickedVal
128 }
129
130 //===----------------------------------------------------------------------===//
131 // callMethod - Enter the specified method...
132 //
133 bool Interpreter::callMethod(const string &Name) {
134   vector<Value*> Options = LookupMatchingNames(Name);
135
136   for (unsigned i = 0; i < Options.size(); ++i) { // Remove nonmethod matches...
137     if (!Options[i]->isMethod()) {
138       Options.erase(Options.begin()+i);
139       --i;
140     }
141   }
142
143   Value *PickedMeth = ChooseOneOption(Name, Options);
144   if (PickedMeth == 0)
145     return true;
146
147   Method *M = PickedMeth->castMethodAsserting();
148
149   vector<GenericValue> Args;
150   // TODO, get args from user...
151
152   callMethod(M, Args);  // Start executing it...
153
154   // Reset the current frame location to the top of stack
155   CurFrame = ECStack.size()-1;
156
157   return false;
158 }