1 //===-- Support.cpp - Support routines for interpreter --------------------===//
3 // This file contains support routines for the interpreter core.
5 //===----------------------------------------------------------------------===//
7 #include "Interpreter.h"
8 #include "llvm/SymbolTable.h"
9 #include "llvm/Assembly/Writer.h"
10 #include "llvm/Module.h"
12 //===----------------------------------------------------------------------===//
14 // LookupMatchingNames helper - Search a symbol table for values matching Name.
16 static inline void LookupMatchingNames(const std::string &Name,
18 std::vector<Value*> &Results) {
19 // Loop over all of the type planes in the symbol table...
20 for (SymbolTable::iterator I = SymTab.begin(), E = SymTab.end(); I != E; ++I){
21 SymbolTable::VarMap &Plane = I->second;
23 // Search the symbol table plane for this name...
24 SymbolTable::VarMap::iterator Val = Plane.find(Name);
25 if (Val != Plane.end())
26 Results.push_back(Val->second); // Found a name match!
30 // LookupMatchingNames - Search the current function namespace, then the global
31 // namespace looking for values that match the specified name. Return ALL
32 // matches to that name. This is obviously slow, and should only be used for
35 std::vector<Value*> Interpreter::LookupMatchingNames(const std::string &Name) {
36 std::vector<Value*> Results;
37 Function *CurFunc = getCurrentFunction();
39 if (CurFunc) ::LookupMatchingNames(Name, CurFunc->getSymbolTable(), Results);
40 ::LookupMatchingNames(Name, getModule().getSymbolTable(), Results);
44 // ChooseOneOption - Prompt the user to choose among the specified options to
45 // pick one value. If no options are provided, emit an error. If a single
46 // option is provided, just return that option.
48 Value *Interpreter::ChooseOneOption(const std::string &Name,
49 const std::vector<Value*> &Opts) {
50 switch (Opts.size()) {
51 case 1: return Opts[0];
53 std::cout << "Error: no entities named '" << Name << "' found!\n";
55 default: break; // Must prompt user...
58 std::cout << "Multiple entities named '" << Name
59 << "' found! Please choose:\n";
60 std::cout << " 0. Cancel operation\n";
61 for (unsigned i = 0; i < Opts.size(); ++i) {
62 std::cout << " " << (i+1) << ".";
63 WriteAsOperand(std::cout, Opts[i]) << "\n";
68 std::cout << "lli> " << std::flush;
70 if (Option > Opts.size())
71 std::cout << "Invalid selection: Please choose from 0 to " << Opts.size()
73 } while (Option > Opts.size());
75 if (Option == 0) return 0;
76 return Opts[Option-1];