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