LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Module.cpp

Go to the documentation of this file.
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>::createNode() {
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>::createNode() {
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.  Note that this only
00210 /// returns a global variable if it does not have internal linkage.  The type
00211 /// argument should be the underlying type of the global, ie, it should not
00212 /// have the top-level PointerType, which represents the address of the
00213 /// global.
00214 ///
00215 GlobalVariable *Module::getGlobalVariable(const std::string &Name, 
00216                                           const Type *Ty) {
00217   if (Value *V = getSymbolTable().lookup(PointerType::get(Ty), Name)) {
00218     GlobalVariable *Result = cast<GlobalVariable>(V);
00219     if (!Result->hasInternalLinkage())
00220       return Result;
00221   }
00222   return 0;
00223 }
00224 
00225 
00226 
00227 //===----------------------------------------------------------------------===//
00228 // Methods for easy access to the types in the module.
00229 //
00230 
00231 
00232 // addTypeName - Insert an entry in the symbol table mapping Str to Type.  If
00233 // there is already an entry for this name, true is returned and the symbol
00234 // table is not modified.
00235 //
00236 bool Module::addTypeName(const std::string &Name, const Type *Ty) {
00237   SymbolTable &ST = getSymbolTable();
00238 
00239   if (ST.lookupType(Name)) return true;  // Already in symtab...
00240   
00241   // Not in symbol table?  Set the name with the Symtab as an argument so the
00242   // type knows what to update...
00243   ST.insert(Name, Ty);
00244 
00245   return false;
00246 }
00247 
00248 /// getTypeByName - Return the type with the specified name in this module, or
00249 /// null if there is none by that name.
00250 const Type *Module::getTypeByName(const std::string &Name) const {
00251   const SymbolTable &ST = getSymbolTable();
00252   return cast_or_null<Type>(ST.lookupType(Name));
00253 }
00254 
00255 // getTypeName - If there is at least one entry in the symbol table for the
00256 // specified type, return it.
00257 //
00258 std::string Module::getTypeName(const Type *Ty) const {
00259   const SymbolTable &ST = getSymbolTable();
00260 
00261   SymbolTable::type_const_iterator TI = ST.type_begin();
00262   SymbolTable::type_const_iterator TE = ST.type_end();
00263   if ( TI == TE ) return ""; // No names for types
00264 
00265   while (TI != TE && TI->second != Ty)
00266     ++TI;
00267 
00268   if (TI != TE)  // Must have found an entry!
00269     return TI->first;
00270   return "";     // Must not have found anything...
00271 }
00272 
00273 //===----------------------------------------------------------------------===//
00274 // Other module related stuff.
00275 //
00276 
00277 
00278 // dropAllReferences() - This function causes all the subelementss to "let go"
00279 // of all references that they are maintaining.  This allows one to 'delete' a
00280 // whole module at a time, even though there may be circular references... first
00281 // all references are dropped, and all use counts go to zero.  Then everything
00282 // is deleted for real.  Note that no operations are valid on an object that
00283 // has "dropped all references", except operator delete.
00284 //
00285 void Module::dropAllReferences() {
00286   for(Module::iterator I = begin(), E = end(); I != E; ++I)
00287     I->dropAllReferences();
00288 
00289   for(Module::giterator I = gbegin(), E = gend(); I != E; ++I)
00290     I->dropAllReferences();
00291 }
00292