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 /// Target endian information...
92 Module::Endianness Module::getEndianness() const {
93 std::string temp = DataLayout;
94 Module::Endianness ret = AnyEndianness;
96 while (!temp.empty()) {
97 std::string token = getToken(temp, "-");
99 if (token[0] == 'e') {
101 } else if (token[0] == 'E') {
109 /// Target Pointer Size information...
110 Module::PointerSize Module::getPointerSize() const {
111 std::string temp = DataLayout;
112 Module::PointerSize ret = AnyPointerSize;
114 while (!temp.empty()) {
115 std::string token = getToken(temp, "-");
116 char signal = getToken(token, ":")[0];
119 int size = atoi(getToken(token, ":").c_str());
130 //===----------------------------------------------------------------------===//
131 // Methods for easy access to the functions in the module.
134 // getOrInsertFunction - Look up the specified function in the module symbol
135 // table. If it does not exist, add a prototype for the function and return
136 // it. This is nice because it allows most passes to get away with not handling
137 // the symbol table directly for this common task.
139 Constant *Module::getOrInsertFunction(const std::string &Name,
140 const FunctionType *Ty,
141 AttrListPtr AttributeList) {
142 ValueSymbolTable &SymTab = getValueSymbolTable();
144 // See if we have a definition for the specified function already.
145 GlobalValue *F = dyn_cast_or_null<GlobalValue>(SymTab.lookup(Name));
148 Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name);
149 if (!New->isIntrinsic()) // Intrinsics get attrs set on construction
150 New->setAttributes(AttributeList);
151 FunctionList.push_back(New);
152 return New; // Return the new prototype.
155 // Okay, the function exists. Does it have externally visible linkage?
156 if (F->hasLocalLinkage()) {
157 // Clear the function's name.
159 // Retry, now there won't be a conflict.
160 Constant *NewF = getOrInsertFunction(Name, Ty);
161 F->setName(&Name[0], Name.size());
165 // If the function exists but has the wrong type, return a bitcast to the
167 if (F->getType() != PointerType::getUnqual(Ty))
168 return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty));
170 // Otherwise, we just found the existing function or a prototype.
174 Constant *Module::getOrInsertTargetIntrinsic(const std::string &Name,
175 const FunctionType *Ty,
176 AttrListPtr AttributeList) {
177 ValueSymbolTable &SymTab = getValueSymbolTable();
179 // See if we have a definition for the specified function already.
180 GlobalValue *F = dyn_cast_or_null<GlobalValue>(SymTab.lookup(Name));
183 Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name);
184 New->setAttributes(AttributeList);
185 FunctionList.push_back(New);
186 return New; // Return the new prototype.
189 // Otherwise, we just found the existing function or a prototype.
193 Constant *Module::getOrInsertFunction(const std::string &Name,
194 const FunctionType *Ty) {
195 AttrListPtr AttributeList = AttrListPtr::get((AttributeWithIndex *)0, 0);
196 return getOrInsertFunction(Name, Ty, AttributeList);
199 // getOrInsertFunction - Look up the specified function in the module symbol
200 // table. If it does not exist, add a prototype for the function and return it.
201 // This version of the method takes a null terminated list of function
202 // arguments, which makes it easier for clients to use.
204 Constant *Module::getOrInsertFunction(const std::string &Name,
205 AttrListPtr AttributeList,
206 const Type *RetTy, ...) {
208 va_start(Args, RetTy);
210 // Build the list of argument types...
211 std::vector<const Type*> ArgTys;
212 while (const Type *ArgTy = va_arg(Args, const Type*))
213 ArgTys.push_back(ArgTy);
217 // Build the function type and chain to the other getOrInsertFunction...
218 return getOrInsertFunction(Name, FunctionType::get(RetTy, ArgTys, false),
222 Constant *Module::getOrInsertFunction(const std::string &Name,
223 const Type *RetTy, ...) {
225 va_start(Args, RetTy);
227 // Build the list of argument types...
228 std::vector<const Type*> ArgTys;
229 while (const Type *ArgTy = va_arg(Args, const Type*))
230 ArgTys.push_back(ArgTy);
234 // Build the function type and chain to the other getOrInsertFunction...
235 return getOrInsertFunction(Name, FunctionType::get(RetTy, ArgTys, false),
236 AttrListPtr::get((AttributeWithIndex *)0, 0));
239 // getFunction - Look up the specified function in the module symbol table.
240 // If it does not exist, return null.
242 Function *Module::getFunction(const std::string &Name) const {
243 const ValueSymbolTable &SymTab = getValueSymbolTable();
244 return dyn_cast_or_null<Function>(SymTab.lookup(Name));
247 Function *Module::getFunction(const char *Name) const {
248 const ValueSymbolTable &SymTab = getValueSymbolTable();
249 return dyn_cast_or_null<Function>(SymTab.lookup(Name, Name+strlen(Name)));
252 //===----------------------------------------------------------------------===//
253 // Methods for easy access to the global variables in the module.
256 /// getGlobalVariable - Look up the specified global variable in the module
257 /// symbol table. If it does not exist, return null. The type argument
258 /// should be the underlying type of the global, i.e., it should not have
259 /// the top-level PointerType, which represents the address of the global.
260 /// If AllowLocal is set to true, this function will return types that
261 /// have an local. By default, these types are not returned.
263 GlobalVariable *Module::getGlobalVariable(const std::string &Name,
264 bool AllowLocal) const {
265 if (Value *V = ValSymTab->lookup(Name)) {
266 GlobalVariable *Result = dyn_cast<GlobalVariable>(V);
267 if (Result && (AllowLocal || !Result->hasLocalLinkage()))
273 /// getOrInsertGlobal - Look up the specified global in the module symbol table.
274 /// 1. If it does not exist, add a declaration of the global and return it.
275 /// 2. Else, the global exists but has the wrong type: return the function
276 /// with a constantexpr cast to the right type.
277 /// 3. Finally, if the existing global is the correct delclaration, return the
279 Constant *Module::getOrInsertGlobal(const std::string &Name, const Type *Ty) {
280 ValueSymbolTable &SymTab = getValueSymbolTable();
282 // See if we have a definition for the specified global already.
283 GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(SymTab.lookup(Name));
286 GlobalVariable *New =
287 new GlobalVariable(Ty, false, GlobalVariable::ExternalLinkage, 0, Name);
288 GlobalList.push_back(New);
289 return New; // Return the new declaration.
292 // If the variable exists but has the wrong type, return a bitcast to the
294 if (GV->getType() != PointerType::getUnqual(Ty))
295 return ConstantExpr::getBitCast(GV, PointerType::getUnqual(Ty));
297 // Otherwise, we just found the existing function or a prototype.
301 //===----------------------------------------------------------------------===//
302 // Methods for easy access to the global variables in the module.
305 // getNamedAlias - Look up the specified global in the module symbol table.
306 // If it does not exist, return null.
308 GlobalAlias *Module::getNamedAlias(const std::string &Name) const {
309 const ValueSymbolTable &SymTab = getValueSymbolTable();
310 return dyn_cast_or_null<GlobalAlias>(SymTab.lookup(Name));
313 //===----------------------------------------------------------------------===//
314 // Methods for easy access to the types in the module.
318 // addTypeName - Insert an entry in the symbol table mapping Str to Type. If
319 // there is already an entry for this name, true is returned and the symbol
320 // table is not modified.
322 bool Module::addTypeName(const std::string &Name, const Type *Ty) {
323 TypeSymbolTable &ST = getTypeSymbolTable();
325 if (ST.lookup(Name)) return true; // Already in symtab...
327 // Not in symbol table? Set the name with the Symtab as an argument so the
328 // type knows what to update...
334 /// getTypeByName - Return the type with the specified name in this module, or
335 /// null if there is none by that name.
336 const Type *Module::getTypeByName(const std::string &Name) const {
337 const TypeSymbolTable &ST = getTypeSymbolTable();
338 return cast_or_null<Type>(ST.lookup(Name));
341 // getTypeName - If there is at least one entry in the symbol table for the
342 // specified type, return it.
344 std::string Module::getTypeName(const Type *Ty) const {
345 const TypeSymbolTable &ST = getTypeSymbolTable();
347 TypeSymbolTable::const_iterator TI = ST.begin();
348 TypeSymbolTable::const_iterator TE = ST.end();
349 if ( TI == TE ) return ""; // No names for types
351 while (TI != TE && TI->second != Ty)
354 if (TI != TE) // Must have found an entry!
356 return ""; // Must not have found anything...
359 //===----------------------------------------------------------------------===//
360 // Other module related stuff.
364 // dropAllReferences() - This function causes all the subelementss to "let go"
365 // of all references that they are maintaining. This allows one to 'delete' a
366 // whole module at a time, even though there may be circular references... first
367 // all references are dropped, and all use counts go to zero. Then everything
368 // is deleted for real. Note that no operations are valid on an object that
369 // has "dropped all references", except operator delete.
371 void Module::dropAllReferences() {
372 for(Module::iterator I = begin(), E = end(); I != E; ++I)
373 I->dropAllReferences();
375 for(Module::global_iterator I = global_begin(), E = global_end(); I != E; ++I)
376 I->dropAllReferences();
378 for(Module::alias_iterator I = alias_begin(), E = alias_end(); I != E; ++I)
379 I->dropAllReferences();
382 void Module::addLibrary(const std::string& Lib) {
383 for (Module::lib_iterator I = lib_begin(), E = lib_end(); I != E; ++I)
386 LibraryList.push_back(Lib);
389 void Module::removeLibrary(const std::string& Lib) {
390 LibraryListType::iterator I = LibraryList.begin();
391 LibraryListType::iterator E = LibraryList.end();
394 LibraryList.erase(I);