1 //===-- Module.cpp - Implement the Module class ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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 "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/Support/LeakDetector.h"
21 #include "SymbolTableListTraitsImpl.h"
22 #include "llvm/TypeSymbolTable.h"
28 //===----------------------------------------------------------------------===//
29 // Methods to implement the globals and functions lists.
32 Function *ilist_traits<Function>::createSentinel() {
34 FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false);
35 Function *Ret = Function::Create(FTy, GlobalValue::ExternalLinkage);
36 // This should not be garbage monitored.
37 LeakDetector::removeGarbageObject(Ret);
40 GlobalVariable *ilist_traits<GlobalVariable>::createSentinel() {
41 GlobalVariable *Ret = new GlobalVariable(Type::Int32Ty, false,
42 GlobalValue::ExternalLinkage);
43 // This should not be garbage monitored.
44 LeakDetector::removeGarbageObject(Ret);
47 GlobalAlias *ilist_traits<GlobalAlias>::createSentinel() {
48 GlobalAlias *Ret = new GlobalAlias(Type::Int32Ty,
49 GlobalValue::ExternalLinkage);
50 // This should not be garbage monitored.
51 LeakDetector::removeGarbageObject(Ret);
55 iplist<Function> &ilist_traits<Function>::getList(Module *M) {
56 return M->getFunctionList();
58 iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) {
59 return M->getGlobalList();
61 iplist<GlobalAlias> &ilist_traits<GlobalAlias>::getList(Module *M) {
62 return M->getAliasList();
65 // Explicit instantiations of SymbolTableListTraits since some of the methods
66 // are not in the public header file.
67 template class SymbolTableListTraits<GlobalVariable, Module>;
68 template class SymbolTableListTraits<Function, Module>;
69 template class SymbolTableListTraits<GlobalAlias, Module>;
71 //===----------------------------------------------------------------------===//
72 // Primitive Module methods.
75 Module::Module(const std::string &MID)
76 : ModuleID(MID), DataLayout("") {
77 ValSymTab = new ValueSymbolTable();
78 TypeSymTab = new TypeSymbolTable();
91 // Module::dump() - Allow printing from debugger
92 void Module::dump() const {
93 print(*cerr.stream());
96 /// Target endian information...
97 Module::Endianness Module::getEndianness() const {
98 std::string temp = DataLayout;
99 Module::Endianness ret = AnyEndianness;
101 while (!temp.empty()) {
102 std::string token = getToken(temp, "-");
104 if (token[0] == 'e') {
106 } else if (token[0] == 'E') {
114 /// Target Pointer Size information...
115 Module::PointerSize Module::getPointerSize() const {
116 std::string temp = DataLayout;
117 Module::PointerSize ret = AnyPointerSize;
119 while (!temp.empty()) {
120 std::string token = getToken(temp, "-");
121 char signal = getToken(token, ":")[0];
124 int size = atoi(getToken(token, ":").c_str());
135 //===----------------------------------------------------------------------===//
136 // Methods for easy access to the functions in the module.
139 // getOrInsertFunction - Look up the specified function in the module symbol
140 // table. If it does not exist, add a prototype for the function and return
141 // it. This is nice because it allows most passes to get away with not handling
142 // the symbol table directly for this common task.
144 Constant *Module::getOrInsertFunction(const std::string &Name,
145 const FunctionType *Ty) {
146 ValueSymbolTable &SymTab = getValueSymbolTable();
148 // See if we have a definition for the specified function already.
149 GlobalValue *F = dyn_cast_or_null<GlobalValue>(SymTab.lookup(Name));
152 Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name);
153 FunctionList.push_back(New);
154 return New; // Return the new prototype.
157 // Okay, the function exists. Does it have externally visible linkage?
158 if (F->hasInternalLinkage()) {
159 // Clear the function's name.
161 // Retry, now there won't be a conflict.
162 Constant *NewF = getOrInsertFunction(Name, Ty);
163 F->setName(&Name[0], Name.size());
167 // If the function exists but has the wrong type, return a bitcast to the
169 if (F->getType() != PointerType::getUnqual(Ty))
170 return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty));
172 // Otherwise, we just found the existing function or a prototype.
176 // getOrInsertFunction - Look up the specified function in the module symbol
177 // table. If it does not exist, add a prototype for the function and return it.
178 // This version of the method takes a null terminated list of function
179 // arguments, which makes it easier for clients to use.
181 Constant *Module::getOrInsertFunction(const std::string &Name,
182 const Type *RetTy, ...) {
184 va_start(Args, RetTy);
186 // Build the list of argument types...
187 std::vector<const Type*> ArgTys;
188 while (const Type *ArgTy = va_arg(Args, const Type*))
189 ArgTys.push_back(ArgTy);
193 // Build the function type and chain to the other getOrInsertFunction...
194 return getOrInsertFunction(Name, FunctionType::get(RetTy, ArgTys, false));
198 // getFunction - Look up the specified function in the module symbol table.
199 // If it does not exist, return null.
201 Function *Module::getFunction(const std::string &Name) const {
202 const ValueSymbolTable &SymTab = getValueSymbolTable();
203 return dyn_cast_or_null<Function>(SymTab.lookup(Name));
206 Function *Module::getFunction(const char *Name) const {
207 const ValueSymbolTable &SymTab = getValueSymbolTable();
208 return dyn_cast_or_null<Function>(SymTab.lookup(Name, Name+strlen(Name)));
211 //===----------------------------------------------------------------------===//
212 // Methods for easy access to the global variables in the module.
215 /// getGlobalVariable - Look up the specified global variable in the module
216 /// symbol table. If it does not exist, return null. The type argument
217 /// should be the underlying type of the global, i.e., it should not have
218 /// the top-level PointerType, which represents the address of the global.
219 /// If AllowInternal is set to true, this function will return types that
220 /// have InternalLinkage. By default, these types are not returned.
222 GlobalVariable *Module::getGlobalVariable(const std::string &Name,
223 bool AllowInternal) const {
224 if (Value *V = ValSymTab->lookup(Name)) {
225 GlobalVariable *Result = dyn_cast<GlobalVariable>(V);
226 if (Result && (AllowInternal || !Result->hasInternalLinkage()))
232 //===----------------------------------------------------------------------===//
233 // Methods for easy access to the global variables in the module.
236 // getNamedAlias - Look up the specified global in the module symbol table.
237 // If it does not exist, return null.
239 GlobalAlias *Module::getNamedAlias(const std::string &Name) const {
240 const ValueSymbolTable &SymTab = getValueSymbolTable();
241 return dyn_cast_or_null<GlobalAlias>(SymTab.lookup(Name));
244 //===----------------------------------------------------------------------===//
245 // Methods for easy access to the types in the module.
249 // addTypeName - Insert an entry in the symbol table mapping Str to Type. If
250 // there is already an entry for this name, true is returned and the symbol
251 // table is not modified.
253 bool Module::addTypeName(const std::string &Name, const Type *Ty) {
254 TypeSymbolTable &ST = getTypeSymbolTable();
256 if (ST.lookup(Name)) return true; // Already in symtab...
258 // Not in symbol table? Set the name with the Symtab as an argument so the
259 // type knows what to update...
265 /// getTypeByName - Return the type with the specified name in this module, or
266 /// null if there is none by that name.
267 const Type *Module::getTypeByName(const std::string &Name) const {
268 const TypeSymbolTable &ST = getTypeSymbolTable();
269 return cast_or_null<Type>(ST.lookup(Name));
272 // getTypeName - If there is at least one entry in the symbol table for the
273 // specified type, return it.
275 std::string Module::getTypeName(const Type *Ty) const {
276 const TypeSymbolTable &ST = getTypeSymbolTable();
278 TypeSymbolTable::const_iterator TI = ST.begin();
279 TypeSymbolTable::const_iterator TE = ST.end();
280 if ( TI == TE ) return ""; // No names for types
282 while (TI != TE && TI->second != Ty)
285 if (TI != TE) // Must have found an entry!
287 return ""; // Must not have found anything...
290 //===----------------------------------------------------------------------===//
291 // Other module related stuff.
295 // dropAllReferences() - This function causes all the subelementss to "let go"
296 // of all references that they are maintaining. This allows one to 'delete' a
297 // whole module at a time, even though there may be circular references... first
298 // all references are dropped, and all use counts go to zero. Then everything
299 // is deleted for real. Note that no operations are valid on an object that
300 // has "dropped all references", except operator delete.
302 void Module::dropAllReferences() {
303 for(Module::iterator I = begin(), E = end(); I != E; ++I)
304 I->dropAllReferences();
306 for(Module::global_iterator I = global_begin(), E = global_end(); I != E; ++I)
307 I->dropAllReferences();
309 for(Module::alias_iterator I = alias_begin(), E = alias_end(); I != E; ++I)
310 I->dropAllReferences();
313 void Module::addLibrary(const std::string& Lib) {
314 for (Module::lib_iterator I = lib_begin(), E = lib_end(); I != E; ++I)
317 LibraryList.push_back(Lib);
320 void Module::removeLibrary(const std::string& Lib) {
321 LibraryListType::iterator I = LibraryList.begin();
322 LibraryListType::iterator E = LibraryList.end();
325 LibraryList.erase(I);