Initial checkin of interpreter
[oota-llvm.git] / lib / ExecutionEngine / Interpreter / Support.cpp
1 //===-- Support.cpp - Support routines for interpreter --------------------===//
2 // 
3 //  This file contains support routines for the interpreter core.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "Interpreter.h"
8 #include "llvm/SymbolTable.h"
9 #include "llvm/Assembly/Writer.h"
10
11 //===----------------------------------------------------------------------===//
12 //
13 // LookupMatchingNames helper - Search a symbol table for values matching Name.
14 //
15 static inline void LookupMatchingNames(const string &Name, SymTabValue &STV,
16                                        vector<Value*> &Results) {
17   SymbolTable *SymTab = STV.getSymbolTable();
18   if (SymTab == 0) return;                         // No symbolic values :(
19
20   // Loop over all of the type planes in the symbol table...
21   for (SymbolTable::iterator I = SymTab->begin(), E = SymTab->end();
22        I != E; ++I) {
23     SymbolTable::VarMap &Plane = I->second;
24     
25     // Search the symbol table plane for this name...
26     SymbolTable::VarMap::iterator Val = Plane.find(Name);
27     if (Val != Plane.end())
28       Results.push_back(Val->second);                    // Found a name match!
29   }
30 }
31
32 // LookupMatchingNames - Search the current method namespace, then the global
33 // namespace looking for values that match the specified name.  Return ALL
34 // matches to that name.  This is obviously slow, and should only be used for
35 // user interaction.
36 //
37 vector<Value*> Interpreter::LookupMatchingNames(const string &Name) {
38   vector<Value*> Results;
39   Method *CurMeth = getCurrentMethod();
40   
41   if (CurMeth) ::LookupMatchingNames(Name, *CurMeth, Results);
42   if (CurMod ) ::LookupMatchingNames(Name, *CurMod , Results);
43   return Results;
44 }
45
46 // ChooseOneOption - Prompt the user to choose among the specified options to
47 // pick one value.  If no options are provided, emit an error.  If a single 
48 // option is provided, just return that option.
49 //
50 Value *Interpreter::ChooseOneOption(const string &Name,
51                                     const vector<Value*> &Opts) {
52   switch (Opts.size()) {
53   case 1: return Opts[0];
54   case 0: 
55     cout << "Error: no entities named '" << Name << "' found!\n";
56     return 0;
57   default: break;  // Must prompt user...
58   }
59
60   cout << "Multiple entities named '" << Name << "' found!  Please choose:\n";
61   cout << "  0. Cancel operation\n";
62   for (unsigned i = 0; i < Opts.size(); ++i) {
63     cout << "  " << (i+1) << ".";
64     WriteAsOperand(cout, Opts[i]) << endl;
65   }
66
67   unsigned Option;
68   do {
69     cout << "lli> " << flush;
70     cin >> Option;
71     if (Option > Opts.size())
72       cout << "Invalid selection: Please choose from 0 to " << Opts.size()
73            << endl;
74   } while (Option > Opts.size());
75
76   if (Option == 0) return 0;
77   return Opts[Option-1];
78 }