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 GlobalVariable
*ilist_traits
<GlobalVariable
>::createSentinel() {
33 GlobalVariable
*Ret
= new GlobalVariable(Type::Int32Ty
, false,
34 GlobalValue::ExternalLinkage
);
35 // This should not be garbage monitored.
36 LeakDetector::removeGarbageObject(Ret
);
39 GlobalAlias
*ilist_traits
<GlobalAlias
>::createSentinel() {
40 GlobalAlias
*Ret
= new GlobalAlias(Type::Int32Ty
,
41 GlobalValue::ExternalLinkage
);
42 // This should not be garbage monitored.
43 LeakDetector::removeGarbageObject(Ret
);
47 // Explicit instantiations of SymbolTableListTraits since some of the methods
48 // are not in the public header file.
49 template class SymbolTableListTraits
<GlobalVariable
, Module
>;
50 template class SymbolTableListTraits
<Function
, Module
>;
51 template class SymbolTableListTraits
<GlobalAlias
, Module
>;
53 //===----------------------------------------------------------------------===//
54 // Primitive Module methods.
57 Module::Module(const std::string
&MID
)
58 : ModuleID(MID
), DataLayout("") {
59 ValSymTab
= new ValueSymbolTable();
60 TypeSymTab
= new TypeSymbolTable();
73 /// Target endian information...
74 Module::Endianness
Module::getEndianness() const {
75 std::string temp
= DataLayout
;
76 Module::Endianness ret
= AnyEndianness
;
78 while (!temp
.empty()) {
79 std::string token
= getToken(temp
, "-");
81 if (token
[0] == 'e') {
83 } else if (token
[0] == 'E') {
91 /// Target Pointer Size information...
92 Module::PointerSize
Module::getPointerSize() const {
93 std::string temp
= DataLayout
;
94 Module::PointerSize ret
= AnyPointerSize
;
96 while (!temp
.empty()) {
97 std::string token
= getToken(temp
, "-");
98 char signal
= getToken(token
, ":")[0];
101 int size
= atoi(getToken(token
, ":").c_str());
112 /// getNamedValue - Return the first global value in the module with
113 /// the specified name, of arbitrary type. This method returns null
114 /// if a global with the specified name is not found.
115 GlobalValue
*Module::getNamedValue(const std::string
&Name
) const {
116 return cast_or_null
<GlobalValue
>(getValueSymbolTable().lookup(Name
));
119 GlobalValue
*Module::getNamedValue(const char *Name
) const {
120 llvm::Value
*V
= getValueSymbolTable().lookup(Name
, Name
+strlen(Name
));
121 return cast_or_null
<GlobalValue
>(V
);
124 //===----------------------------------------------------------------------===//
125 // Methods for easy access to the functions in the module.
128 // getOrInsertFunction - Look up the specified function in the module symbol
129 // table. If it does not exist, add a prototype for the function and return
130 // it. This is nice because it allows most passes to get away with not handling
131 // the symbol table directly for this common task.
133 Constant
*Module::getOrInsertFunction(const std::string
&Name
,
134 const FunctionType
*Ty
,
135 AttrListPtr AttributeList
) {
136 // See if we have a definition for the specified function already.
137 GlobalValue
*F
= getNamedValue(Name
);
140 Function
*New
= Function::Create(Ty
, GlobalVariable::ExternalLinkage
, Name
);
141 if (!New
->isIntrinsic()) // Intrinsics get attrs set on construction
142 New
->setAttributes(AttributeList
);
143 FunctionList
.push_back(New
);
144 return New
; // Return the new prototype.
147 // Okay, the function exists. Does it have externally visible linkage?
148 if (F
->hasLocalLinkage()) {
149 // Clear the function's name.
151 // Retry, now there won't be a conflict.
152 Constant
*NewF
= getOrInsertFunction(Name
, Ty
);
153 F
->setName(&Name
[0], Name
.size());
157 // If the function exists but has the wrong type, return a bitcast to the
159 if (F
->getType() != PointerType::getUnqual(Ty
))
160 return ConstantExpr::getBitCast(F
, PointerType::getUnqual(Ty
));
162 // Otherwise, we just found the existing function or a prototype.
166 Constant
*Module::getOrInsertTargetIntrinsic(const std::string
&Name
,
167 const FunctionType
*Ty
,
168 AttrListPtr AttributeList
) {
169 // See if we have a definition for the specified function already.
170 GlobalValue
*F
= getNamedValue(Name
);
173 Function
*New
= Function::Create(Ty
, GlobalVariable::ExternalLinkage
, Name
);
174 New
->setAttributes(AttributeList
);
175 FunctionList
.push_back(New
);
176 return New
; // Return the new prototype.
179 // Otherwise, we just found the existing function or a prototype.
183 Constant
*Module::getOrInsertFunction(const std::string
&Name
,
184 const FunctionType
*Ty
) {
185 AttrListPtr AttributeList
= AttrListPtr::get((AttributeWithIndex
*)0, 0);
186 return getOrInsertFunction(Name
, Ty
, AttributeList
);
189 // getOrInsertFunction - Look up the specified function in the module symbol
190 // table. If it does not exist, add a prototype for the function and return it.
191 // This version of the method takes a null terminated list of function
192 // arguments, which makes it easier for clients to use.
194 Constant
*Module::getOrInsertFunction(const std::string
&Name
,
195 AttrListPtr AttributeList
,
196 const Type
*RetTy
, ...) {
198 va_start(Args
, RetTy
);
200 // Build the list of argument types...
201 std::vector
<const Type
*> ArgTys
;
202 while (const Type
*ArgTy
= va_arg(Args
, const Type
*))
203 ArgTys
.push_back(ArgTy
);
207 // Build the function type and chain to the other getOrInsertFunction...
208 return getOrInsertFunction(Name
, FunctionType::get(RetTy
, ArgTys
, false),
212 Constant
*Module::getOrInsertFunction(const std::string
&Name
,
213 const Type
*RetTy
, ...) {
215 va_start(Args
, RetTy
);
217 // Build the list of argument types...
218 std::vector
<const Type
*> ArgTys
;
219 while (const Type
*ArgTy
= va_arg(Args
, const Type
*))
220 ArgTys
.push_back(ArgTy
);
224 // Build the function type and chain to the other getOrInsertFunction...
225 return getOrInsertFunction(Name
, FunctionType::get(RetTy
, ArgTys
, false),
226 AttrListPtr::get((AttributeWithIndex
*)0, 0));
229 // getFunction - Look up the specified function in the module symbol table.
230 // If it does not exist, return null.
232 Function
*Module::getFunction(const std::string
&Name
) const {
233 return dyn_cast_or_null
<Function
>(getNamedValue(Name
));
236 Function
*Module::getFunction(const char *Name
) const {
237 return dyn_cast_or_null
<Function
>(getNamedValue(Name
));
240 //===----------------------------------------------------------------------===//
241 // Methods for easy access to the global variables in the module.
244 /// getGlobalVariable - Look up the specified global variable in the module
245 /// symbol table. If it does not exist, return null. The type argument
246 /// should be the underlying type of the global, i.e., it should not have
247 /// the top-level PointerType, which represents the address of the global.
248 /// If AllowLocal is set to true, this function will return types that
249 /// have an local. By default, these types are not returned.
251 GlobalVariable
*Module::getGlobalVariable(const std::string
&Name
,
252 bool AllowLocal
) const {
253 if (GlobalVariable
*Result
=
254 dyn_cast_or_null
<GlobalVariable
>(getNamedValue(Name
)))
255 if (AllowLocal
|| !Result
->hasLocalLinkage())
260 /// getOrInsertGlobal - Look up the specified global in the module symbol table.
261 /// 1. If it does not exist, add a declaration of the global and return it.
262 /// 2. Else, the global exists but has the wrong type: return the function
263 /// with a constantexpr cast to the right type.
264 /// 3. Finally, if the existing global is the correct delclaration, return the
266 Constant
*Module::getOrInsertGlobal(const std::string
&Name
, const Type
*Ty
) {
267 // See if we have a definition for the specified global already.
268 GlobalVariable
*GV
= dyn_cast_or_null
<GlobalVariable
>(getNamedValue(Name
));
271 GlobalVariable
*New
=
272 new GlobalVariable(Ty
, false, GlobalVariable::ExternalLinkage
, 0, Name
);
273 GlobalList
.push_back(New
);
274 return New
; // Return the new declaration.
277 // If the variable exists but has the wrong type, return a bitcast to the
279 if (GV
->getType() != PointerType::getUnqual(Ty
))
280 return ConstantExpr::getBitCast(GV
, PointerType::getUnqual(Ty
));
282 // Otherwise, we just found the existing function or a prototype.
286 //===----------------------------------------------------------------------===//
287 // Methods for easy access to the global variables in the module.
290 // getNamedAlias - Look up the specified global in the module symbol table.
291 // If it does not exist, return null.
293 GlobalAlias
*Module::getNamedAlias(const std::string
&Name
) const {
294 return dyn_cast_or_null
<GlobalAlias
>(getNamedValue(Name
));
297 //===----------------------------------------------------------------------===//
298 // Methods for easy access to the types in the module.
302 // addTypeName - Insert an entry in the symbol table mapping Str to Type. If
303 // there is already an entry for this name, true is returned and the symbol
304 // table is not modified.
306 bool Module::addTypeName(const std::string
&Name
, const Type
*Ty
) {
307 TypeSymbolTable
&ST
= getTypeSymbolTable();
309 if (ST
.lookup(Name
)) return true; // Already in symtab...
311 // Not in symbol table? Set the name with the Symtab as an argument so the
312 // type knows what to update...
318 /// getTypeByName - Return the type with the specified name in this module, or
319 /// null if there is none by that name.
320 const Type
*Module::getTypeByName(const std::string
&Name
) const {
321 const TypeSymbolTable
&ST
= getTypeSymbolTable();
322 return cast_or_null
<Type
>(ST
.lookup(Name
));
325 // getTypeName - If there is at least one entry in the symbol table for the
326 // specified type, return it.
328 std::string
Module::getTypeName(const Type
*Ty
) const {
329 const TypeSymbolTable
&ST
= getTypeSymbolTable();
331 TypeSymbolTable::const_iterator TI
= ST
.begin();
332 TypeSymbolTable::const_iterator TE
= ST
.end();
333 if ( TI
== TE
) return ""; // No names for types
335 while (TI
!= TE
&& TI
->second
!= Ty
)
338 if (TI
!= TE
) // Must have found an entry!
340 return ""; // Must not have found anything...
343 //===----------------------------------------------------------------------===//
344 // Other module related stuff.
348 // dropAllReferences() - This function causes all the subelementss to "let go"
349 // of all references that they are maintaining. This allows one to 'delete' a
350 // whole module at a time, even though there may be circular references... first
351 // all references are dropped, and all use counts go to zero. Then everything
352 // is deleted for real. Note that no operations are valid on an object that
353 // has "dropped all references", except operator delete.
355 void Module::dropAllReferences() {
356 for(Module::iterator I
= begin(), E
= end(); I
!= E
; ++I
)
357 I
->dropAllReferences();
359 for(Module::global_iterator I
= global_begin(), E
= global_end(); I
!= E
; ++I
)
360 I
->dropAllReferences();
362 for(Module::alias_iterator I
= alias_begin(), E
= alias_end(); I
!= E
; ++I
)
363 I
->dropAllReferences();
366 void Module::addLibrary(const std::string
& Lib
) {
367 for (Module::lib_iterator I
= lib_begin(), E
= lib_end(); I
!= E
; ++I
)
370 LibraryList
.push_back(Lib
);
373 void Module::removeLibrary(const std::string
& Lib
) {
374 LibraryListType::iterator I
= LibraryList
.begin();
375 LibraryListType::iterator E
= LibraryList
.end();
378 LibraryList
.erase(I
);