1 //===-- Module.cpp - Implement the Module class ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Module class for the VMCore library.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Module.h"
15 #include "llvm/InstrTypes.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "Support/STLExtras.h"
19 #include "Support/LeakDetector.h"
20 #include "SymbolTableListTraitsImpl.h"
27 //===----------------------------------------------------------------------===//
28 // Methods to implement the globals and functions lists.
31 Function *ilist_traits<Function>::createNode() {
33 FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false);
34 Function *Ret = new Function(FTy, GlobalValue::ExternalLinkage);
35 // This should not be garbage monitored.
36 LeakDetector::removeGarbageObject(Ret);
39 GlobalVariable *ilist_traits<GlobalVariable>::createNode() {
40 GlobalVariable *Ret = new GlobalVariable(Type::IntTy, false,
41 GlobalValue::ExternalLinkage);
42 // This should not be garbage monitored.
43 LeakDetector::removeGarbageObject(Ret);
47 iplist<Function> &ilist_traits<Function>::getList(Module *M) {
48 return M->getFunctionList();
50 iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) {
51 return M->getGlobalList();
54 // Explicit instantiations of SymbolTableListTraits since some of the methods
55 // are not in the public header file...
56 template class SymbolTableListTraits<GlobalVariable, Module, Module>;
57 template class SymbolTableListTraits<Function, Module, Module>;
59 // Define the GlobalValueRefMap as a struct that wraps a map so that we don't
60 // have Module.h depend on <map>
63 struct GlobalValueRefMap {
64 typedef std::map<GlobalValue*, ConstantPointerRef*> MapTy;
65 typedef MapTy::iterator iterator;
66 std::map<GlobalValue*, ConstantPointerRef*> Map;
70 //===----------------------------------------------------------------------===//
71 // Primitive Module methods.
74 Module::Module(const std::string &MID)
75 : ModuleID(MID), Endian(AnyEndianness), PtrSize(AnyPointerSize) {
76 FunctionList.setItemParent(this);
77 FunctionList.setParent(this);
78 GlobalList.setItemParent(this);
79 GlobalList.setParent(this);
81 SymTab = new SymbolTable();
87 GlobalList.setParent(0);
89 FunctionList.setParent(0);
93 // Module::dump() - Allow printing from debugger
94 void Module::dump() const {
98 //===----------------------------------------------------------------------===//
99 // Methods for easy access to the functions in the module.
102 // getOrInsertFunction - Look up the specified function in the module symbol
103 // table. If it does not exist, add a prototype for the function and return
104 // it. This is nice because it allows most passes to get away with not handling
105 // the symbol table directly for this common task.
107 Function *Module::getOrInsertFunction(const std::string &Name,
108 const FunctionType *Ty) {
109 SymbolTable &SymTab = getSymbolTable();
111 // See if we have a definitions for the specified function already...
112 if (Value *V = SymTab.lookup(PointerType::get(Ty), Name)) {
113 return cast<Function>(V); // Yup, got it
114 } else { // Nope, add one
115 Function *New = new Function(Ty, GlobalVariable::ExternalLinkage, Name);
116 FunctionList.push_back(New);
117 return New; // Return the new prototype...
121 // getOrInsertFunction - Look up the specified function in the module symbol
122 // table. If it does not exist, add a prototype for the function and return it.
123 // This version of the method takes a null terminated list of function
124 // arguments, which makes it easier for clients to use.
126 Function *Module::getOrInsertFunction(const std::string &Name,
127 const Type *RetTy, ...) {
129 va_start(Args, RetTy);
131 // Build the list of argument types...
132 std::vector<const Type*> ArgTys;
133 while (const Type *ArgTy = va_arg(Args, const Type*))
134 ArgTys.push_back(ArgTy);
138 // Build the function type and chain to the other getOrInsertFunction...
139 return getOrInsertFunction(Name, FunctionType::get(RetTy, ArgTys, false));
143 // getFunction - Look up the specified function in the module symbol table.
144 // If it does not exist, return null.
146 Function *Module::getFunction(const std::string &Name, const FunctionType *Ty) {
147 SymbolTable &SymTab = getSymbolTable();
148 return cast_or_null<Function>(SymTab.lookup(PointerType::get(Ty), Name));
152 /// getMainFunction - This function looks up main efficiently. This is such a
153 /// common case, that it is a method in Module. If main cannot be found, a
154 /// null pointer is returned.
156 Function *Module::getMainFunction() {
157 std::vector<const Type*> Params;
160 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
164 // void main(void)...
165 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
169 Params.push_back(Type::IntTy);
171 // int main(int argc)...
172 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
176 // void main(int argc)...
177 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
181 for (unsigned i = 0; i != 2; ++i) { // Check argv and envp
182 Params.push_back(PointerType::get(PointerType::get(Type::SByteTy)));
184 // int main(int argc, char **argv)...
185 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
189 // void main(int argc, char **argv)...
190 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
195 // Ok, try to find main the hard way...
196 return getNamedFunction("main");
199 /// getNamedFunction - Return the first function in the module with the
200 /// specified name, of arbitrary type. This method returns null if a function
201 /// with the specified name is not found.
203 Function *Module::getNamedFunction(const std::string &Name) {
204 // Loop over all of the functions, looking for the function desired
206 for (iterator I = begin(), E = end(); I != E; ++I)
207 if (I->getName() == Name)
212 return Found; // Non-external function not found...
215 //===----------------------------------------------------------------------===//
216 // Methods for easy access to the global variables in the module.
219 /// getGlobalVariable - Look up the specified global variable in the module
220 /// symbol table. If it does not exist, return null. Note that this only
221 /// returns a global variable if it does not have internal linkage. The type
222 /// argument should be the underlying type of the global, ie, it should not
223 /// have the top-level PointerType, which represents the address of the
226 GlobalVariable *Module::getGlobalVariable(const std::string &Name,
228 if (Value *V = getSymbolTable().lookup(PointerType::get(Ty), Name)) {
229 GlobalVariable *Result = cast<GlobalVariable>(V);
230 if (!Result->hasInternalLinkage())
238 //===----------------------------------------------------------------------===//
239 // Methods for easy access to the types in the module.
243 // addTypeName - Insert an entry in the symbol table mapping Str to Type. If
244 // there is already an entry for this name, true is returned and the symbol
245 // table is not modified.
247 bool Module::addTypeName(const std::string &Name, const Type *Ty) {
248 SymbolTable &ST = getSymbolTable();
250 if (ST.lookupType(Name)) return true; // Already in symtab...
252 // Not in symbol table? Set the name with the Symtab as an argument so the
253 // type knows what to update...
259 /// getTypeByName - Return the type with the specified name in this module, or
260 /// null if there is none by that name.
261 const Type *Module::getTypeByName(const std::string &Name) const {
262 const SymbolTable &ST = getSymbolTable();
263 return cast_or_null<Type>(ST.lookupType(Name));
266 // getTypeName - If there is at least one entry in the symbol table for the
267 // specified type, return it.
269 std::string Module::getTypeName(const Type *Ty) const {
270 const SymbolTable &ST = getSymbolTable();
272 SymbolTable::type_const_iterator TI = ST.type_begin();
273 SymbolTable::type_const_iterator TE = ST.type_end();
274 if ( TI == TE ) return ""; // No names for types
276 while (TI != TE && TI->second != Ty)
279 if (TI != TE) // Must have found an entry!
281 return ""; // Must not have found anything...
285 //===----------------------------------------------------------------------===//
286 // Other module related stuff.
290 // dropAllReferences() - This function causes all the subelementss to "let go"
291 // of all references that they are maintaining. This allows one to 'delete' a
292 // whole module at a time, even though there may be circular references... first
293 // all references are dropped, and all use counts go to zero. Then everything
294 // is deleted for real. Note that no operations are valid on an object that
295 // has "dropped all references", except operator delete.
297 void Module::dropAllReferences() {
298 for(Module::iterator I = begin(), E = end(); I != E; ++I)
299 I->dropAllReferences();
301 for(Module::giterator I = gbegin(), E = gend(); I != E; ++I)
302 I->dropAllReferences();
304 // If there are any GlobalVariable references still out there, nuke them now.
305 // Since all references are hereby dropped, nothing could possibly reference
306 // them still. Note that destroying all of the constant pointer refs will
307 // eventually cause the GVRefMap field to be set to null (by
308 // destroyConstantPointerRef, below).
311 // Delete the ConstantPointerRef node...
312 GVRefMap->Map.begin()->second->destroyConstant();
315 // Accessor for the underlying GlobalValRefMap...
316 ConstantPointerRef *Module::getConstantPointerRef(GlobalValue *V){
317 // Create ref map lazily on demand...
318 if (GVRefMap == 0) GVRefMap = new GlobalValueRefMap();
320 GlobalValueRefMap::iterator I = GVRefMap->Map.find(V);
321 if (I != GVRefMap->Map.end()) return I->second;
323 ConstantPointerRef *Ref = new ConstantPointerRef(V);
324 GVRefMap->Map[V] = Ref;
328 void Module::destroyConstantPointerRef(ConstantPointerRef *CPR) {
329 assert(GVRefMap && "No map allocated, but we have a CPR?");
330 if (!GVRefMap->Map.erase(CPR->getValue())) // Remove it from the map...
331 assert(0 && "ConstantPointerRef not found in module CPR map!");
333 if (GVRefMap->Map.empty()) { // If the map is empty, delete it.