LLVM API Documentation
00001 //===-- Module.cpp - Implement the Module class ---------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the Module class for the VMCore library. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/Module.h" 00015 #include "llvm/InstrTypes.h" 00016 #include "llvm/Constants.h" 00017 #include "llvm/DerivedTypes.h" 00018 #include "llvm/ADT/STLExtras.h" 00019 #include "llvm/ADT/StringExtras.h" 00020 #include "llvm/Support/LeakDetector.h" 00021 #include "SymbolTableListTraitsImpl.h" 00022 #include "llvm/TypeSymbolTable.h" 00023 #include <algorithm> 00024 #include <cstdarg> 00025 #include <cstdlib> 00026 using namespace llvm; 00027 00028 //===----------------------------------------------------------------------===// 00029 // Methods to implement the globals and functions lists. 00030 // 00031 00032 Function *ilist_traits<Function>::createSentinel() { 00033 FunctionType *FTy = 00034 FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false); 00035 Function *Ret = Function::Create(FTy, GlobalValue::ExternalLinkage); 00036 // This should not be garbage monitored. 00037 LeakDetector::removeGarbageObject(Ret); 00038 return Ret; 00039 } 00040 GlobalVariable *ilist_traits<GlobalVariable>::createSentinel() { 00041 GlobalVariable *Ret = new GlobalVariable(Type::Int32Ty, false, 00042 GlobalValue::ExternalLinkage); 00043 // This should not be garbage monitored. 00044 LeakDetector::removeGarbageObject(Ret); 00045 return Ret; 00046 } 00047 GlobalAlias *ilist_traits<GlobalAlias>::createSentinel() { 00048 GlobalAlias *Ret = new GlobalAlias(Type::Int32Ty, 00049 GlobalValue::ExternalLinkage); 00050 // This should not be garbage monitored. 00051 LeakDetector::removeGarbageObject(Ret); 00052 return Ret; 00053 } 00054 00055 iplist<Function> &ilist_traits<Function>::getList(Module *M) { 00056 return M->getFunctionList(); 00057 } 00058 iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) { 00059 return M->getGlobalList(); 00060 } 00061 iplist<GlobalAlias> &ilist_traits<GlobalAlias>::getList(Module *M) { 00062 return M->getAliasList(); 00063 } 00064 00065 // Explicit instantiations of SymbolTableListTraits since some of the methods 00066 // are not in the public header file. 00067 template class SymbolTableListTraits<GlobalVariable, Module>; 00068 template class SymbolTableListTraits<Function, Module>; 00069 template class SymbolTableListTraits<GlobalAlias, Module>; 00070 00071 //===----------------------------------------------------------------------===// 00072 // Primitive Module methods. 00073 // 00074 00075 Module::Module(const std::string &MID) 00076 : ModuleID(MID), DataLayout("") { 00077 ValSymTab = new ValueSymbolTable(); 00078 TypeSymTab = new TypeSymbolTable(); 00079 } 00080 00081 Module::~Module() { 00082 dropAllReferences(); 00083 GlobalList.clear(); 00084 FunctionList.clear(); 00085 AliasList.clear(); 00086 LibraryList.clear(); 00087 delete ValSymTab; 00088 delete TypeSymTab; 00089 } 00090 00091 /// Target endian information... 00092 Module::Endianness Module::getEndianness() const { 00093 std::string temp = DataLayout; 00094 Module::Endianness ret = AnyEndianness; 00095 00096 while (!temp.empty()) { 00097 std::string token = getToken(temp, "-"); 00098 00099 if (token[0] == 'e') { 00100 ret = LittleEndian; 00101 } else if (token[0] == 'E') { 00102 ret = BigEndian; 00103 } 00104 } 00105 00106 return ret; 00107 } 00108 00109 /// Target Pointer Size information... 00110 Module::PointerSize Module::getPointerSize() const { 00111 std::string temp = DataLayout; 00112 Module::PointerSize ret = AnyPointerSize; 00113 00114 while (!temp.empty()) { 00115 std::string token = getToken(temp, "-"); 00116 char signal = getToken(token, ":")[0]; 00117 00118 if (signal == 'p') { 00119 int size = atoi(getToken(token, ":").c_str()); 00120 if (size == 32) 00121 ret = Pointer32; 00122 else if (size == 64) 00123 ret = Pointer64; 00124 } 00125 } 00126 00127 return ret; 00128 } 00129 00130 //===----------------------------------------------------------------------===// 00131 // Methods for easy access to the functions in the module. 00132 // 00133 00134 // getOrInsertFunction - Look up the specified function in the module symbol 00135 // table. If it does not exist, add a prototype for the function and return 00136 // it. This is nice because it allows most passes to get away with not handling 00137 // the symbol table directly for this common task. 00138 // 00139 Constant *Module::getOrInsertFunction(const std::string &Name, 00140 const FunctionType *Ty, 00141 AttrListPtr AttributeList) { 00142 ValueSymbolTable &SymTab = getValueSymbolTable(); 00143 00144 // See if we have a definition for the specified function already. 00145 GlobalValue *F = dyn_cast_or_null<GlobalValue>(SymTab.lookup(Name)); 00146 if (F == 0) { 00147 // Nope, add it 00148 Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name); 00149 if (!New->isIntrinsic()) // Intrinsics get attrs set on construction 00150 New->setAttributes(AttributeList); 00151 FunctionList.push_back(New); 00152 return New; // Return the new prototype. 00153 } 00154 00155 // Okay, the function exists. Does it have externally visible linkage? 00156 if (F->hasInternalLinkage()) { 00157 // Clear the function's name. 00158 F->setName(""); 00159 // Retry, now there won't be a conflict. 00160 Constant *NewF = getOrInsertFunction(Name, Ty); 00161 F->setName(&Name[0], Name.size()); 00162 return NewF; 00163 } 00164 00165 // If the function exists but has the wrong type, return a bitcast to the 00166 // right type. 00167 if (F->getType() != PointerType::getUnqual(Ty)) 00168 return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty)); 00169 00170 // Otherwise, we just found the existing function or a prototype. 00171 return F; 00172 } 00173 00174 Constant *Module::getOrInsertFunction(const std::string &Name, 00175 const FunctionType *Ty) { 00176 AttrListPtr AttributeList = AttrListPtr::get((AttributeWithIndex *)0, 0); 00177 return getOrInsertFunction(Name, Ty, AttributeList); 00178 } 00179 00180 // getOrInsertFunction - Look up the specified function in the module symbol 00181 // table. If it does not exist, add a prototype for the function and return it. 00182 // This version of the method takes a null terminated list of function 00183 // arguments, which makes it easier for clients to use. 00184 // 00185 Constant *Module::getOrInsertFunction(const std::string &Name, 00186 AttrListPtr AttributeList, 00187 const Type *RetTy, ...) { 00188 va_list Args; 00189 va_start(Args, RetTy); 00190 00191 // Build the list of argument types... 00192 std::vector<const Type*> ArgTys; 00193 while (const Type *ArgTy = va_arg(Args, const Type*)) 00194 ArgTys.push_back(ArgTy); 00195 00196 va_end(Args); 00197 00198 // Build the function type and chain to the other getOrInsertFunction... 00199 return getOrInsertFunction(Name, FunctionType::get(RetTy, ArgTys, false), 00200 AttributeList); 00201 } 00202 00203 Constant *Module::getOrInsertFunction(const std::string &Name, 00204 const Type *RetTy, ...) { 00205 va_list Args; 00206 va_start(Args, RetTy); 00207 00208 // Build the list of argument types... 00209 std::vector<const Type*> ArgTys; 00210 while (const Type *ArgTy = va_arg(Args, const Type*)) 00211 ArgTys.push_back(ArgTy); 00212 00213 va_end(Args); 00214 00215 // Build the function type and chain to the other getOrInsertFunction... 00216 return getOrInsertFunction(Name, FunctionType::get(RetTy, ArgTys, false), 00217 AttrListPtr::get((AttributeWithIndex *)0, 0)); 00218 } 00219 00220 // getFunction - Look up the specified function in the module symbol table. 00221 // If it does not exist, return null. 00222 // 00223 Function *Module::getFunction(const std::string &Name) const { 00224 const ValueSymbolTable &SymTab = getValueSymbolTable(); 00225 return dyn_cast_or_null<Function>(SymTab.lookup(Name)); 00226 } 00227 00228 Function *Module::getFunction(const char *Name) const { 00229 const ValueSymbolTable &SymTab = getValueSymbolTable(); 00230 return dyn_cast_or_null<Function>(SymTab.lookup(Name, Name+strlen(Name))); 00231 } 00232 00233 //===----------------------------------------------------------------------===// 00234 // Methods for easy access to the global variables in the module. 00235 // 00236 00237 /// getGlobalVariable - Look up the specified global variable in the module 00238 /// symbol table. If it does not exist, return null. The type argument 00239 /// should be the underlying type of the global, i.e., it should not have 00240 /// the top-level PointerType, which represents the address of the global. 00241 /// If AllowInternal is set to true, this function will return types that 00242 /// have InternalLinkage. By default, these types are not returned. 00243 /// 00244 GlobalVariable *Module::getGlobalVariable(const std::string &Name, 00245 bool AllowInternal) const { 00246 if (Value *V = ValSymTab->lookup(Name)) { 00247 GlobalVariable *Result = dyn_cast<GlobalVariable>(V); 00248 if (Result && (AllowInternal || !Result->hasInternalLinkage())) 00249 return Result; 00250 } 00251 return 0; 00252 } 00253 00254 /// getOrInsertGlobal - Look up the specified global in the module symbol table. 00255 /// 1. If it does not exist, add a declaration of the global and return it. 00256 /// 2. Else, the global exists but has the wrong type: return the function 00257 /// with a constantexpr cast to the right type. 00258 /// 3. Finally, if the existing global is the correct delclaration, return the 00259 /// existing global. 00260 Constant *Module::getOrInsertGlobal(const std::string &Name, const Type *Ty) { 00261 ValueSymbolTable &SymTab = getValueSymbolTable(); 00262 00263 // See if we have a definition for the specified global already. 00264 GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(SymTab.lookup(Name)); 00265 if (GV == 0) { 00266 // Nope, add it 00267 GlobalVariable *New = 00268 new GlobalVariable(Ty, false, GlobalVariable::ExternalLinkage, 0, Name); 00269 GlobalList.push_back(New); 00270 return New; // Return the new declaration. 00271 } 00272 00273 // If the variable exists but has the wrong type, return a bitcast to the 00274 // right type. 00275 if (GV->getType() != PointerType::getUnqual(Ty)) 00276 return ConstantExpr::getBitCast(GV, PointerType::getUnqual(Ty)); 00277 00278 // Otherwise, we just found the existing function or a prototype. 00279 return GV; 00280 } 00281 00282 //===----------------------------------------------------------------------===// 00283 // Methods for easy access to the global variables in the module. 00284 // 00285 00286 // getNamedAlias - Look up the specified global in the module symbol table. 00287 // If it does not exist, return null. 00288 // 00289 GlobalAlias *Module::getNamedAlias(const std::string &Name) const { 00290 const ValueSymbolTable &SymTab = getValueSymbolTable(); 00291 return dyn_cast_or_null<GlobalAlias>(SymTab.lookup(Name)); 00292 } 00293 00294 //===----------------------------------------------------------------------===// 00295 // Methods for easy access to the types in the module. 00296 // 00297 00298 00299 // addTypeName - Insert an entry in the symbol table mapping Str to Type. If 00300 // there is already an entry for this name, true is returned and the symbol 00301 // table is not modified. 00302 // 00303 bool Module::addTypeName(const std::string &Name, const Type *Ty) { 00304 TypeSymbolTable &ST = getTypeSymbolTable(); 00305 00306 if (ST.lookup(Name)) return true; // Already in symtab... 00307 00308 // Not in symbol table? Set the name with the Symtab as an argument so the 00309 // type knows what to update... 00310 ST.insert(Name, Ty); 00311 00312 return false; 00313 } 00314 00315 /// getTypeByName - Return the type with the specified name in this module, or 00316 /// null if there is none by that name. 00317 const Type *Module::getTypeByName(const std::string &Name) const { 00318 const TypeSymbolTable &ST = getTypeSymbolTable(); 00319 return cast_or_null<Type>(ST.lookup(Name)); 00320 } 00321 00322 // getTypeName - If there is at least one entry in the symbol table for the 00323 // specified type, return it. 00324 // 00325 std::string Module::getTypeName(const Type *Ty) const { 00326 const TypeSymbolTable &ST = getTypeSymbolTable(); 00327 00328 TypeSymbolTable::const_iterator TI = ST.begin(); 00329 TypeSymbolTable::const_iterator TE = ST.end(); 00330 if ( TI == TE ) return ""; // No names for types 00331 00332 while (TI != TE && TI->second != Ty) 00333 ++TI; 00334 00335 if (TI != TE) // Must have found an entry! 00336 return TI->first; 00337 return ""; // Must not have found anything... 00338 } 00339 00340 //===----------------------------------------------------------------------===// 00341 // Other module related stuff. 00342 // 00343 00344 00345 // dropAllReferences() - This function causes all the subelementss to "let go" 00346 // of all references that they are maintaining. This allows one to 'delete' a 00347 // whole module at a time, even though there may be circular references... first 00348 // all references are dropped, and all use counts go to zero. Then everything 00349 // is deleted for real. Note that no operations are valid on an object that 00350 // has "dropped all references", except operator delete. 00351 // 00352 void Module::dropAllReferences() { 00353 for(Module::iterator I = begin(), E = end(); I != E; ++I) 00354 I->dropAllReferences(); 00355 00356 for(Module::global_iterator I = global_begin(), E = global_end(); I != E; ++I) 00357 I->dropAllReferences(); 00358 00359 for(Module::alias_iterator I = alias_begin(), E = alias_end(); I != E; ++I) 00360 I->dropAllReferences(); 00361 } 00362 00363 void Module::addLibrary(const std::string& Lib) { 00364 for (Module::lib_iterator I = lib_begin(), E = lib_end(); I != E; ++I) 00365 if (*I == Lib) 00366 return; 00367 LibraryList.push_back(Lib); 00368 } 00369 00370 void Module::removeLibrary(const std::string& Lib) { 00371 LibraryListType::iterator I = LibraryList.begin(); 00372 LibraryListType::iterator E = LibraryList.end(); 00373 for (;I != E; ++I) 00374 if (*I == Lib) { 00375 LibraryList.erase(I); 00376 return; 00377 } 00378 } 00379
This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.