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