Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 1 | //===-- ValueSymbolTable.cpp - Implement the ValueSymbolTable class -------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 21f0c31 | 2006-01-11 05:39:45 +0000 | [diff] [blame] | 5 | // This file was developed by the LLVM research group. It is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the ValueSymbolTable class for the VMCore library. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 14 | #define DEBUG_TYPE "valuesymtab" |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 15 | #include "llvm/GlobalValue.h" |
| 16 | #include "llvm/Type.h" |
| 17 | #include "llvm/ValueSymbolTable.h" |
| 18 | #include "llvm/ADT/StringExtras.h" |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 20 | #include <algorithm> |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 23 | // Class destructor |
| 24 | ValueSymbolTable::~ValueSymbolTable() { |
| 25 | #ifndef NDEBUG // Only do this in -g mode... |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 26 | for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ++VI) |
Chris Lattner | 86fc081 | 2007-02-07 06:28:48 +0000 | [diff] [blame^] | 27 | DEBUG(DOUT << "Value still in symbol table! Type = '" |
| 28 | << VI->second->getType()->getDescription() << "' Name = '" |
| 29 | << VI->first << "'\n"); |
| 30 | assert(vmap.empty() && "Values remain in symbol table!"); |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 31 | #endif |
| 32 | } |
| 33 | |
| 34 | // getUniqueName - Given a base name, return a string that is either equal to |
| 35 | // it (or derived from it) that does not already occur in the symbol table for |
| 36 | // the specified type. |
| 37 | // |
| 38 | std::string ValueSymbolTable::getUniqueName(const std::string &BaseName) const { |
| 39 | std::string TryName = BaseName; |
| 40 | const_iterator End = vmap.end(); |
| 41 | |
| 42 | // See if the name exists |
| 43 | while (vmap.find(TryName) != End) // Loop until we find a free |
| 44 | TryName = BaseName + utostr(++LastUnique); // name in the symbol table |
| 45 | return TryName; |
| 46 | } |
| 47 | |
| 48 | |
| 49 | // lookup a value - Returns null on failure... |
| 50 | // |
| 51 | Value *ValueSymbolTable::lookup(const std::string &Name) const { |
| 52 | const_iterator VI = vmap.find(Name); |
| 53 | if (VI != vmap.end()) // We found the symbol |
| 54 | return const_cast<Value*>(VI->second); |
| 55 | return 0; |
| 56 | } |
| 57 | |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 58 | // Insert a value into the symbol table with the specified name... |
| 59 | // |
| 60 | void ValueSymbolTable::insert(Value* V) { |
| 61 | assert(V && "Can't insert null Value into symbol table!"); |
| 62 | assert(V->hasName() && "Can't insert nameless Value into symbol table"); |
| 63 | |
Chris Lattner | 770dadf | 2007-02-07 05:22:49 +0000 | [diff] [blame] | 64 | // Try inserting the name, assuming it won't conflict. |
| 65 | if (vmap.insert(make_pair(V->Name, V)).second) { |
| 66 | DOUT << " Inserted value: " << V->Name << ": " << *V << "\n"; |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | // Otherwise, there is a naming conflict. Rename this value. |
Chris Lattner | 71a60ba | 2007-02-07 05:52:51 +0000 | [diff] [blame] | 71 | std::string UniqueName = V->getName(); |
| 72 | unsigned BaseSize = UniqueName.size(); |
| 73 | do { |
| 74 | // Trim any suffix off. |
| 75 | UniqueName.resize(BaseSize); |
| 76 | UniqueName += utostr(++LastUnique); |
Chris Lattner | d17dbe9 | 2007-02-07 06:25:36 +0000 | [diff] [blame] | 77 | // Try insert the vmap entry with this suffix. |
Chris Lattner | 71a60ba | 2007-02-07 05:52:51 +0000 | [diff] [blame] | 78 | } while (!vmap.insert(make_pair(UniqueName, V)).second); |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 79 | |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 80 | V->Name = UniqueName; |
Chris Lattner | d17dbe9 | 2007-02-07 06:25:36 +0000 | [diff] [blame] | 81 | |
| 82 | DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n"); |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | // Remove a value |
Chris Lattner | f5df485 | 2007-02-07 06:13:49 +0000 | [diff] [blame] | 86 | void ValueSymbolTable::remove(Value *V) { |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 87 | assert(V->hasName() && "Value doesn't have name!"); |
| 88 | iterator Entry = vmap.find(V->getName()); |
Chris Lattner | f5df485 | 2007-02-07 06:13:49 +0000 | [diff] [blame] | 89 | assert(Entry != vmap.end() && "Entry was not in the symtab!"); |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 90 | |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 91 | DEBUG(DOUT << " Removing Value: " << Entry->second->getName() << "\n"); |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 92 | |
| 93 | // Remove the value from the plane... |
| 94 | vmap.erase(Entry); |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | // DumpVal - a std::for_each function for dumping a value |
| 98 | // |
| 99 | static void DumpVal(const std::pair<const std::string, Value *> &V) { |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 100 | DOUT << " '" << V.first << "' = "; |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 101 | V.second->dump(); |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 102 | DOUT << "\n"; |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | // dump - print out the symbol table |
| 106 | // |
| 107 | void ValueSymbolTable::dump() const { |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 108 | DOUT << "ValueSymbolTable:\n"; |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 109 | for_each(vmap.begin(), vmap.end(), DumpVal); |
| 110 | } |