Protect the GC table in Function.cpp
[oota-llvm.git] / lib / VMCore / TypeSymbolTable.cpp
1 //===-- TypeSymbolTable.cpp - Implement the TypeSymbolTable class ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the TypeSymbolTable class for the VMCore library.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/TypeSymbolTable.h"
15 #include "llvm/DerivedTypes.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/Support/ManagedStatic.h"
18 #include "llvm/Support/Streams.h"
19 #include "llvm/Support/Threading.h"
20 #include "llvm/System/RWMutex.h"
21 #include <algorithm>
22 using namespace llvm;
23
24 #define DEBUG_SYMBOL_TABLE 0
25 #define DEBUG_ABSTYPE 0
26
27 static ManagedStatic<sys::RWMutex> TypeSymbolTableLock;
28
29 TypeSymbolTable::~TypeSymbolTable() {
30   // Drop all abstract type references in the type plane...
31   for (iterator TI = tmap.begin(), TE = tmap.end(); TI != TE; ++TI) {
32     if (TI->second->isAbstract())   // If abstract, drop the reference...
33       cast<DerivedType>(TI->second)->removeAbstractTypeUser(this);
34   }
35 }
36
37 std::string TypeSymbolTable::getUniqueName(const std::string &BaseName) const {
38   std::string TryName = BaseName;
39   
40   if (llvm_is_multithreaded()) TypeSymbolTableLock->reader_acquire();
41   
42   const_iterator End = tmap.end();
43
44   // See if the name exists
45   while (tmap.find(TryName) != End)            // Loop until we find a free
46     TryName = BaseName + utostr(++LastUnique); // name in the symbol table
47   
48   if (llvm_is_multithreaded()) TypeSymbolTableLock->reader_release();
49   
50   return TryName;
51 }
52
53 // lookup a type by name - returns null on failure
54 Type* TypeSymbolTable::lookup(const std::string& Name) const {
55   if (llvm_is_multithreaded()) TypeSymbolTableLock->reader_acquire();
56   
57   const_iterator TI = tmap.find(Name);
58   Type* result = 0;
59   if (TI != tmap.end())
60     result = const_cast<Type*>(TI->second);
61     
62   if (llvm_is_multithreaded()) TypeSymbolTableLock->reader_release();
63   
64   return result;
65 }
66
67 // remove - Remove a type from the symbol table...
68 Type* TypeSymbolTable::remove(iterator Entry) {
69   if (llvm_is_multithreaded()) TypeSymbolTableLock->writer_acquire();
70   
71   assert(Entry != tmap.end() && "Invalid entry to remove!");
72   const Type* Result = Entry->second;
73
74 #if DEBUG_SYMBOL_TABLE
75   dump();
76   cerr << " Removing Value: " << Result->getName() << "\n";
77 #endif
78
79   tmap.erase(Entry);
80   
81   if (llvm_is_multithreaded()) TypeSymbolTableLock->writer_release();
82
83   // If we are removing an abstract type, remove the symbol table from it's use
84   // list...
85   if (Result->isAbstract()) {
86 #if DEBUG_ABSTYPE
87     cerr << "Removing abstract type from symtab"
88          << Result->getDescription()
89          << "\n";
90 #endif
91     cast<DerivedType>(Result)->removeAbstractTypeUser(this);
92   }
93
94   return const_cast<Type*>(Result);
95 }
96
97
98 // insert - Insert a type into the symbol table with the specified name...
99 void TypeSymbolTable::insert(const std::string& Name, const Type* T) {
100   assert(T && "Can't insert null type into symbol table!");
101
102   if (llvm_is_multithreaded()) TypeSymbolTableLock->writer_acquire();
103
104   if (tmap.insert(make_pair(Name, T)).second) {
105     // Type inserted fine with no conflict.
106     
107 #if DEBUG_SYMBOL_TABLE
108     dump();
109     cerr << " Inserted type: " << Name << ": " << T->getDescription() << "\n";
110 #endif
111   } else {
112     // If there is a name conflict...
113     
114     // Check to see if there is a naming conflict.  If so, rename this type!
115     std::string UniqueName = Name;
116     if (lookup(Name))
117       UniqueName = getUniqueName(Name);
118     
119 #if DEBUG_SYMBOL_TABLE
120     dump();
121     cerr << " Inserting type: " << UniqueName << ": "
122         << T->getDescription() << "\n";
123 #endif
124
125     // Insert the tmap entry
126     tmap.insert(make_pair(UniqueName, T));
127   }
128   
129   if (llvm_is_multithreaded()) TypeSymbolTableLock->writer_release();
130
131   // If we are adding an abstract type, add the symbol table to it's use list.
132   if (T->isAbstract()) {
133     cast<DerivedType>(T)->addAbstractTypeUser(this);
134 #if DEBUG_ABSTYPE
135     cerr << "Added abstract type to ST: " << T->getDescription() << "\n";
136 #endif
137   }
138 }
139
140 // This function is called when one of the types in the type plane are refined
141 void TypeSymbolTable::refineAbstractType(const DerivedType *OldType,
142                                          const Type *NewType) {
143   if (llvm_is_multithreaded()) TypeSymbolTableLock->reader_acquire();
144
145   // Loop over all of the types in the symbol table, replacing any references
146   // to OldType with references to NewType.  Note that there may be multiple
147   // occurrences, and although we only need to remove one at a time, it's
148   // faster to remove them all in one pass.
149   //
150   for (iterator I = begin(), E = end(); I != E; ++I) {
151     if (I->second == (Type*)OldType) {  // FIXME when Types aren't const.
152 #if DEBUG_ABSTYPE
153       cerr << "Removing type " << OldType->getDescription() << "\n";
154 #endif
155       OldType->removeAbstractTypeUser(this);
156
157       I->second = (Type*)NewType;  // TODO FIXME when types aren't const
158       if (NewType->isAbstract()) {
159 #if DEBUG_ABSTYPE
160         cerr << "Added type " << NewType->getDescription() << "\n";
161 #endif
162         cast<DerivedType>(NewType)->addAbstractTypeUser(this);
163       }
164     }
165   }
166   
167   if (llvm_is_multithreaded()) TypeSymbolTableLock->reader_release();
168 }
169
170
171 // Handle situation where type becomes Concreate from Abstract
172 void TypeSymbolTable::typeBecameConcrete(const DerivedType *AbsTy) {
173   // Loop over all of the types in the symbol table, dropping any abstract
174   // type user entries for AbsTy which occur because there are names for the
175   // type.
176   if (llvm_is_multithreaded()) TypeSymbolTableLock->reader_acquire();
177   for (iterator TI = begin(), TE = end(); TI != TE; ++TI)
178     if (TI->second == const_cast<Type*>(static_cast<const Type*>(AbsTy)))
179       AbsTy->removeAbstractTypeUser(this);
180   if (llvm_is_multithreaded()) TypeSymbolTableLock->reader_release();
181 }
182
183 static void DumpTypes(const std::pair<const std::string, const Type*>& T ) {
184   cerr << "  '" << T.first << "' = ";
185   T.second->dump();
186   cerr << "\n";
187 }
188
189 void TypeSymbolTable::dump() const {
190   cerr << "TypeSymbolPlane: ";
191   if (llvm_is_multithreaded()) TypeSymbolTableLock->reader_acquire();
192   for_each(tmap.begin(), tmap.end(), DumpTypes);
193   if (llvm_is_multithreaded()) TypeSymbolTableLock->reader_release();
194 }
195
196 // vim: sw=2 ai