LLVM API Documentation
00001 //===-- Module.cpp - Implement the Module class ---------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source 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/Support/LeakDetector.h" 00020 #include "SymbolTableListTraitsImpl.h" 00021 #include <algorithm> 00022 #include <cstdarg> 00023 #include <iostream> 00024 #include <map> 00025 using namespace llvm; 00026 00027 //===----------------------------------------------------------------------===// 00028 // Methods to implement the globals and functions lists. 00029 // 00030 00031 Function *ilist_traits<Function>::createSentinel() { 00032 FunctionType *FTy = 00033 FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false); 00034 Function *Ret = new Function(FTy, GlobalValue::ExternalLinkage); 00035 // This should not be garbage monitored. 00036 LeakDetector::removeGarbageObject(Ret); 00037 return Ret; 00038 } 00039 GlobalVariable *ilist_traits<GlobalVariable>::createSentinel() { 00040 GlobalVariable *Ret = new GlobalVariable(Type::IntTy, false, 00041 GlobalValue::ExternalLinkage); 00042 // This should not be garbage monitored. 00043 LeakDetector::removeGarbageObject(Ret); 00044 return Ret; 00045 } 00046 00047 iplist<Function> &ilist_traits<Function>::getList(Module *M) { 00048 return M->getFunctionList(); 00049 } 00050 iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) { 00051 return M->getGlobalList(); 00052 } 00053 00054 // Explicit instantiations of SymbolTableListTraits since some of the methods 00055 // are not in the public header file. 00056 template class SymbolTableListTraits<GlobalVariable, Module, Module>; 00057 template class SymbolTableListTraits<Function, Module, Module>; 00058 00059 //===----------------------------------------------------------------------===// 00060 // Primitive Module methods. 00061 // 00062 00063 Module::Module(const std::string &MID) 00064 : ModuleID(MID), Endian(AnyEndianness), PtrSize(AnyPointerSize) { 00065 FunctionList.setItemParent(this); 00066 FunctionList.setParent(this); 00067 GlobalList.setItemParent(this); 00068 GlobalList.setParent(this); 00069 SymTab = new SymbolTable(); 00070 } 00071 00072 Module::~Module() { 00073 dropAllReferences(); 00074 GlobalList.clear(); 00075 GlobalList.setParent(0); 00076 FunctionList.clear(); 00077 FunctionList.setParent(0); 00078 LibraryList.clear(); 00079 delete SymTab; 00080 } 00081 00082 // Module::dump() - Allow printing from debugger 00083 void Module::dump() const { 00084 print(std::cerr); 00085 } 00086 00087 //===----------------------------------------------------------------------===// 00088 // Methods for easy access to the functions in the module. 00089 // 00090 00091 // getOrInsertFunction - Look up the specified function in the module symbol 00092 // table. If it does not exist, add a prototype for the function and return 00093 // it. This is nice because it allows most passes to get away with not handling 00094 // the symbol table directly for this common task. 00095 // 00096 Function *Module::getOrInsertFunction(const std::string &Name, 00097 const FunctionType *Ty) { 00098 SymbolTable &SymTab = getSymbolTable(); 00099 00100 // See if we have a definitions for the specified function already... 00101 if (Value *V = SymTab.lookup(PointerType::get(Ty), Name)) { 00102 return cast<Function>(V); // Yup, got it 00103 } else { // Nope, add one 00104 Function *New = new Function(Ty, GlobalVariable::ExternalLinkage, Name); 00105 FunctionList.push_back(New); 00106 return New; // Return the new prototype... 00107 } 00108 } 00109 00110 // getOrInsertFunction - Look up the specified function in the module symbol 00111 // table. If it does not exist, add a prototype for the function and return it. 00112 // This version of the method takes a null terminated list of function 00113 // arguments, which makes it easier for clients to use. 00114 // 00115 Function *Module::getOrInsertFunction(const std::string &Name, 00116 const Type *RetTy, ...) { 00117 va_list Args; 00118 va_start(Args, RetTy); 00119 00120 // Build the list of argument types... 00121 std::vector<const Type*> ArgTys; 00122 while (const Type *ArgTy = va_arg(Args, const Type*)) 00123 ArgTys.push_back(ArgTy); 00124 00125 va_end(Args); 00126 00127 // Build the function type and chain to the other getOrInsertFunction... 00128 return getOrInsertFunction(Name, FunctionType::get(RetTy, ArgTys, false)); 00129 } 00130 00131 00132 // getFunction - Look up the specified function in the module symbol table. 00133 // If it does not exist, return null. 00134 // 00135 Function *Module::getFunction(const std::string &Name, const FunctionType *Ty) { 00136 SymbolTable &SymTab = getSymbolTable(); 00137 return cast_or_null<Function>(SymTab.lookup(PointerType::get(Ty), Name)); 00138 } 00139 00140 00141 /// getMainFunction - This function looks up main efficiently. This is such a 00142 /// common case, that it is a method in Module. If main cannot be found, a 00143 /// null pointer is returned. 00144 /// 00145 Function *Module::getMainFunction() { 00146 std::vector<const Type*> Params; 00147 00148 // int main(void)... 00149 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy, 00150 Params, false))) 00151 return F; 00152 00153 // void main(void)... 00154 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy, 00155 Params, false))) 00156 return F; 00157 00158 Params.push_back(Type::IntTy); 00159 00160 // int main(int argc)... 00161 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy, 00162 Params, false))) 00163 return F; 00164 00165 // void main(int argc)... 00166 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy, 00167 Params, false))) 00168 return F; 00169 00170 for (unsigned i = 0; i != 2; ++i) { // Check argv and envp 00171 Params.push_back(PointerType::get(PointerType::get(Type::SByteTy))); 00172 00173 // int main(int argc, char **argv)... 00174 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy, 00175 Params, false))) 00176 return F; 00177 00178 // void main(int argc, char **argv)... 00179 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy, 00180 Params, false))) 00181 return F; 00182 } 00183 00184 // Ok, try to find main the hard way... 00185 return getNamedFunction("main"); 00186 } 00187 00188 /// getNamedFunction - Return the first function in the module with the 00189 /// specified name, of arbitrary type. This method returns null if a function 00190 /// with the specified name is not found. 00191 /// 00192 Function *Module::getNamedFunction(const std::string &Name) { 00193 // Loop over all of the functions, looking for the function desired 00194 Function *Found = 0; 00195 for (iterator I = begin(), E = end(); I != E; ++I) 00196 if (I->getName() == Name) 00197 if (I->isExternal()) 00198 Found = I; 00199 else 00200 return I; 00201 return Found; // Non-external function not found... 00202 } 00203 00204 //===----------------------------------------------------------------------===// 00205 // Methods for easy access to the global variables in the module. 00206 // 00207 00208 /// getGlobalVariable - Look up the specified global variable in the module 00209 /// symbol table. If it does not exist, return null. The type argument 00210 /// should be the underlying type of the global, i.e., it should not have 00211 /// the top-level PointerType, which represents the address of the global. 00212 /// If AllowInternal is set to true, this function will return types that 00213 /// have InternalLinkage. By default, these types are not returned. 00214 /// 00215 GlobalVariable *Module::getGlobalVariable(const std::string &Name, 00216 const Type *Ty, bool AllowInternal) { 00217 if (Value *V = getSymbolTable().lookup(PointerType::get(Ty), Name)) { 00218 GlobalVariable *Result = cast<GlobalVariable>(V); 00219 if (AllowInternal || !Result->hasInternalLinkage()) 00220 return Result; 00221 } 00222 return 0; 00223 } 00224 00225 /// getNamedGlobal - Return the first global variable in the module with the 00226 /// specified name, of arbitrary type. This method returns null if a global 00227 /// with the specified name is not found. 00228 /// 00229 GlobalVariable *Module::getNamedGlobal(const std::string &Name) { 00230 // FIXME: This would be much faster with a symbol table that doesn't 00231 // discriminate based on type! 00232 for (global_iterator I = global_begin(), E = global_end(); 00233 I != E; ++I) 00234 if (I->getName() == Name) 00235 return I; 00236 return 0; 00237 } 00238 00239 00240 00241 //===----------------------------------------------------------------------===// 00242 // Methods for easy access to the types in the module. 00243 // 00244 00245 00246 // addTypeName - Insert an entry in the symbol table mapping Str to Type. If 00247 // there is already an entry for this name, true is returned and the symbol 00248 // table is not modified. 00249 // 00250 bool Module::addTypeName(const std::string &Name, const Type *Ty) { 00251 SymbolTable &ST = getSymbolTable(); 00252 00253 if (ST.lookupType(Name)) return true; // Already in symtab... 00254 00255 // Not in symbol table? Set the name with the Symtab as an argument so the 00256 // type knows what to update... 00257 ST.insert(Name, Ty); 00258 00259 return false; 00260 } 00261 00262 /// getTypeByName - Return the type with the specified name in this module, or 00263 /// null if there is none by that name. 00264 const Type *Module::getTypeByName(const std::string &Name) const { 00265 const SymbolTable &ST = getSymbolTable(); 00266 return cast_or_null<Type>(ST.lookupType(Name)); 00267 } 00268 00269 // getTypeName - If there is at least one entry in the symbol table for the 00270 // specified type, return it. 00271 // 00272 std::string Module::getTypeName(const Type *Ty) const { 00273 const SymbolTable &ST = getSymbolTable(); 00274 00275 SymbolTable::type_const_iterator TI = ST.type_begin(); 00276 SymbolTable::type_const_iterator TE = ST.type_end(); 00277 if ( TI == TE ) return ""; // No names for types 00278 00279 while (TI != TE && TI->second != Ty) 00280 ++TI; 00281 00282 if (TI != TE) // Must have found an entry! 00283 return TI->first; 00284 return ""; // Must not have found anything... 00285 } 00286 00287 //===----------------------------------------------------------------------===// 00288 // Other module related stuff. 00289 // 00290 00291 00292 // dropAllReferences() - This function causes all the subelementss to "let go" 00293 // of all references that they are maintaining. This allows one to 'delete' a 00294 // whole module at a time, even though there may be circular references... first 00295 // all references are dropped, and all use counts go to zero. Then everything 00296 // is deleted for real. Note that no operations are valid on an object that 00297 // has "dropped all references", except operator delete. 00298 // 00299 void Module::dropAllReferences() { 00300 for(Module::iterator I = begin(), E = end(); I != E; ++I) 00301 I->dropAllReferences(); 00302 00303 for(Module::global_iterator I = global_begin(), E = global_end(); I != E; ++I) 00304 I->dropAllReferences(); 00305 } 00306