Handle value promotion properly to work with tracing better
[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 "llvm/Module.h"
11 #include <iostream>
12 using std::cout;
13
14 //===----------------------------------------------------------------------===//
15 //
16 // LookupMatchingNames helper - Search a symbol table for values matching Name.
17 //
18 static inline void LookupMatchingNames(const std::string &Name,
19                                        SymbolTable &SymTab,
20                                        std::vector<Value*> &Results) {
21   // Loop over all of the type planes in the symbol table...
22   for (SymbolTable::iterator I = SymTab.begin(), E = SymTab.end(); 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 function 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 std::vector<Value*> Interpreter::LookupMatchingNames(const std::string &Name) {
38   std::vector<Value*> Results;
39   Function *CurMeth = getCurrentMethod();
40   
41   if (CurMeth) ::LookupMatchingNames(Name, CurMeth->getSymbolTable(), Results);
42   ::LookupMatchingNames(Name, getModule().getSymbolTable(), 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 std::string &Name,
51                                     const std::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]) << "\n";
65   }
66
67   unsigned Option;
68   do {
69     cout << "lli> " << std::flush;
70     std::cin >> Option;
71     if (Option > Opts.size())
72       cout << "Invalid selection: Please choose from 0 to " << Opts.size()
73            << "\n";
74   } while (Option > Opts.size());
75
76   if (Option == 0) return 0;
77   return Opts[Option-1];
78 }