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/LLVMContext.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/Support/LeakDetector.h"
22 #include "SymbolTableListTraitsImpl.h"
23 #include "llvm/TypeSymbolTable.h"
29 //===----------------------------------------------------------------------===//
30 // Methods to implement the globals and functions lists.
33 GlobalVariable
*ilist_traits
<GlobalVariable
>::createSentinel() {
34 GlobalVariable
*Ret
= new GlobalVariable(getGlobalContext(), Type::Int32Ty
,
35 false, GlobalValue::ExternalLinkage
);
36 // This should not be garbage monitored.
37 LeakDetector::removeGarbageObject(Ret
);
40 GlobalAlias
*ilist_traits
<GlobalAlias
>::createSentinel() {
41 GlobalAlias
*Ret
= new GlobalAlias(Type::Int32Ty
,
42 GlobalValue::ExternalLinkage
);
43 // This should not be garbage monitored.
44 LeakDetector::removeGarbageObject(Ret
);
48 // Explicit instantiations of SymbolTableListTraits since some of the methods
49 // are not in the public header file.
50 template class SymbolTableListTraits
<GlobalVariable
, Module
>;
51 template class SymbolTableListTraits
<Function
, Module
>;
52 template class SymbolTableListTraits
<GlobalAlias
, Module
>;
54 //===----------------------------------------------------------------------===//
55 // Primitive Module methods.
58 Module::Module(const StringRef
&MID
, LLVMContext
& C
)
59 : Context(C
), ModuleID(MID
), DataLayout("") {
60 ValSymTab
= new ValueSymbolTable();
61 TypeSymTab
= new TypeSymbolTable();
75 /// Target endian information...
76 Module::Endianness
Module::getEndianness() const {
77 std::string temp
= DataLayout
;
78 Module::Endianness ret
= AnyEndianness
;
80 while (!temp
.empty()) {
81 std::string token
= getToken(temp
, "-");
83 if (token
[0] == 'e') {
85 } else if (token
[0] == 'E') {
93 /// Target Pointer Size information...
94 Module::PointerSize
Module::getPointerSize() const {
95 std::string temp
= DataLayout
;
96 Module::PointerSize ret
= AnyPointerSize
;
98 while (!temp
.empty()) {
99 std::string token
= getToken(temp
, "-");
100 char signal
= getToken(token
, ":")[0];
103 int size
= atoi(getToken(token
, ":").c_str());
114 /// getNamedValue - Return the first global value in the module with
115 /// the specified name, of arbitrary type. This method returns null
116 /// if a global with the specified name is not found.
117 GlobalValue
*Module::getNamedValue(const StringRef
&Name
) const {
118 return cast_or_null
<GlobalValue
>(getValueSymbolTable().lookup(Name
));
121 //===----------------------------------------------------------------------===//
122 // Methods for easy access to the functions in the module.
125 // getOrInsertFunction - Look up the specified function in the module symbol
126 // table. If it does not exist, add a prototype for the function and return
127 // it. This is nice because it allows most passes to get away with not handling
128 // the symbol table directly for this common task.
130 Constant
*Module::getOrInsertFunction(const StringRef
&Name
,
131 const FunctionType
*Ty
,
132 AttrListPtr AttributeList
) {
133 // See if we have a definition for the specified function already.
134 GlobalValue
*F
= getNamedValue(Name
);
137 Function
*New
= Function::Create(Ty
, GlobalVariable::ExternalLinkage
, Name
);
138 if (!New
->isIntrinsic()) // Intrinsics get attrs set on construction
139 New
->setAttributes(AttributeList
);
140 FunctionList
.push_back(New
);
141 return New
; // Return the new prototype.
144 // Okay, the function exists. Does it have externally visible linkage?
145 if (F
->hasLocalLinkage()) {
146 // Clear the function's name.
148 // Retry, now there won't be a conflict.
149 Constant
*NewF
= getOrInsertFunction(Name
, Ty
);
154 // If the function exists but has the wrong type, return a bitcast to the
156 if (F
->getType() != PointerType::getUnqual(Ty
))
157 return ConstantExpr::getBitCast(F
, PointerType::getUnqual(Ty
));
159 // Otherwise, we just found the existing function or a prototype.
163 Constant
*Module::getOrInsertTargetIntrinsic(const StringRef
&Name
,
164 const FunctionType
*Ty
,
165 AttrListPtr AttributeList
) {
166 // See if we have a definition for the specified function already.
167 GlobalValue
*F
= getNamedValue(Name
);
170 Function
*New
= Function::Create(Ty
, GlobalVariable::ExternalLinkage
, Name
);
171 New
->setAttributes(AttributeList
);
172 FunctionList
.push_back(New
);
173 return New
; // Return the new prototype.
176 // Otherwise, we just found the existing function or a prototype.
180 Constant
*Module::getOrInsertFunction(const StringRef
&Name
,
181 const FunctionType
*Ty
) {
182 AttrListPtr AttributeList
= AttrListPtr::get((AttributeWithIndex
*)0, 0);
183 return getOrInsertFunction(Name
, Ty
, AttributeList
);
186 // getOrInsertFunction - Look up the specified function in the module symbol
187 // table. If it does not exist, add a prototype for the function and return it.
188 // This version of the method takes a null terminated list of function
189 // arguments, which makes it easier for clients to use.
191 Constant
*Module::getOrInsertFunction(const StringRef
&Name
,
192 AttrListPtr AttributeList
,
193 const Type
*RetTy
, ...) {
195 va_start(Args
, RetTy
);
197 // Build the list of argument types...
198 std::vector
<const Type
*> ArgTys
;
199 while (const Type
*ArgTy
= va_arg(Args
, const Type
*))
200 ArgTys
.push_back(ArgTy
);
204 // Build the function type and chain to the other getOrInsertFunction...
205 return getOrInsertFunction(Name
,
206 FunctionType::get(RetTy
, ArgTys
, false),
210 Constant
*Module::getOrInsertFunction(const StringRef
&Name
,
211 const Type
*RetTy
, ...) {
213 va_start(Args
, RetTy
);
215 // Build the list of argument types...
216 std::vector
<const Type
*> ArgTys
;
217 while (const Type
*ArgTy
= va_arg(Args
, const Type
*))
218 ArgTys
.push_back(ArgTy
);
222 // Build the function type and chain to the other getOrInsertFunction...
223 return getOrInsertFunction(Name
,
224 FunctionType::get(RetTy
, ArgTys
, false),
225 AttrListPtr::get((AttributeWithIndex
*)0, 0));
228 // getFunction - Look up the specified function in the module symbol table.
229 // If it does not exist, return null.
231 Function
*Module::getFunction(const StringRef
&Name
) const {
232 return dyn_cast_or_null
<Function
>(getNamedValue(Name
));
235 //===----------------------------------------------------------------------===//
236 // Methods for easy access to the global variables in the module.
239 /// getGlobalVariable - Look up the specified global variable in the module
240 /// symbol table. If it does not exist, return null. The type argument
241 /// should be the underlying type of the global, i.e., it should not have
242 /// the top-level PointerType, which represents the address of the global.
243 /// If AllowLocal is set to true, this function will return types that
244 /// have an local. By default, these types are not returned.
246 GlobalVariable
*Module::getGlobalVariable(const StringRef
&Name
,
247 bool AllowLocal
) const {
248 if (GlobalVariable
*Result
=
249 dyn_cast_or_null
<GlobalVariable
>(getNamedValue(Name
)))
250 if (AllowLocal
|| !Result
->hasLocalLinkage())
255 /// getOrInsertGlobal - Look up the specified global in the module symbol table.
256 /// 1. If it does not exist, add a declaration of the global and return it.
257 /// 2. Else, the global exists but has the wrong type: return the function
258 /// with a constantexpr cast to the right type.
259 /// 3. Finally, if the existing global is the correct delclaration, return the
261 Constant
*Module::getOrInsertGlobal(const StringRef
&Name
, const Type
*Ty
) {
262 // See if we have a definition for the specified global already.
263 GlobalVariable
*GV
= dyn_cast_or_null
<GlobalVariable
>(getNamedValue(Name
));
266 GlobalVariable
*New
=
267 new GlobalVariable(*this, Ty
, false, GlobalVariable::ExternalLinkage
,
269 return New
; // Return the new declaration.
272 // If the variable exists but has the wrong type, return a bitcast to the
274 if (GV
->getType() != PointerType::getUnqual(Ty
))
275 return ConstantExpr::getBitCast(GV
, PointerType::getUnqual(Ty
));
277 // Otherwise, we just found the existing function or a prototype.
281 //===----------------------------------------------------------------------===//
282 // Methods for easy access to the global variables in the module.
285 // getNamedAlias - Look up the specified global in the module symbol table.
286 // If it does not exist, return null.
288 GlobalAlias
*Module::getNamedAlias(const StringRef
&Name
) const {
289 return dyn_cast_or_null
<GlobalAlias
>(getNamedValue(Name
));
292 /// getNamedMetadata - Return the first NamedMDNode in the module with the
293 /// specified name. This method returns null if a NamedMDNode with the
294 //// specified name is not found.
295 NamedMDNode
*Module::getNamedMetadata(const StringRef
&Name
) const {
296 return dyn_cast_or_null
<NamedMDNode
>(getValueSymbolTable().lookup(Name
));
299 /// getOrInsertNamedMetadata - Return the first named MDNode in the module
300 /// with the specified name. This method returns a new NamedMDNode if a
301 /// NamedMDNode with the specified name is not found.
302 NamedMDNode
*Module::getOrInsertNamedMetadata(const StringRef
&Name
) {
304 dyn_cast_or_null
<NamedMDNode
>(getValueSymbolTable().lookup(Name
));
306 NMD
= NamedMDNode::Create(Name
, NULL
, 0, this);
310 //===----------------------------------------------------------------------===//
311 // Methods for easy access to the types in the module.
315 // addTypeName - Insert an entry in the symbol table mapping Str to Type. If
316 // there is already an entry for this name, true is returned and the symbol
317 // table is not modified.
319 bool Module::addTypeName(const StringRef
&Name
, const Type
*Ty
) {
320 TypeSymbolTable
&ST
= getTypeSymbolTable();
322 if (ST
.lookup(Name
)) return true; // Already in symtab...
324 // Not in symbol table? Set the name with the Symtab as an argument so the
325 // type knows what to update...
331 /// getTypeByName - Return the type with the specified name in this module, or
332 /// null if there is none by that name.
333 const Type
*Module::getTypeByName(const StringRef
&Name
) const {
334 const TypeSymbolTable
&ST
= getTypeSymbolTable();
335 return cast_or_null
<Type
>(ST
.lookup(Name
));
338 // getTypeName - If there is at least one entry in the symbol table for the
339 // specified type, return it.
341 std::string
Module::getTypeName(const Type
*Ty
) const {
342 const TypeSymbolTable
&ST
= getTypeSymbolTable();
344 TypeSymbolTable::const_iterator TI
= ST
.begin();
345 TypeSymbolTable::const_iterator TE
= ST
.end();
346 if ( TI
== TE
) return ""; // No names for types
348 while (TI
!= TE
&& TI
->second
!= Ty
)
351 if (TI
!= TE
) // Must have found an entry!
353 return ""; // Must not have found anything...
356 //===----------------------------------------------------------------------===//
357 // Other module related stuff.
361 // dropAllReferences() - This function causes all the subelementss to "let go"
362 // of all references that they are maintaining. This allows one to 'delete' a
363 // whole module at a time, even though there may be circular references... first
364 // all references are dropped, and all use counts go to zero. Then everything
365 // is deleted for real. Note that no operations are valid on an object that
366 // has "dropped all references", except operator delete.
368 void Module::dropAllReferences() {
369 for(Module::iterator I
= begin(), E
= end(); I
!= E
; ++I
)
370 I
->dropAllReferences();
372 for(Module::global_iterator I
= global_begin(), E
= global_end(); I
!= E
; ++I
)
373 I
->dropAllReferences();
375 for(Module::alias_iterator I
= alias_begin(), E
= alias_end(); I
!= E
; ++I
)
376 I
->dropAllReferences();
379 void Module::addLibrary(const StringRef
& Lib
) {
380 for (Module::lib_iterator I
= lib_begin(), E
= lib_end(); I
!= E
; ++I
)
383 LibraryList
.push_back(Lib
);
386 void Module::removeLibrary(const StringRef
& Lib
) {
387 LibraryListType::iterator I
= LibraryList
.begin();
388 LibraryListType::iterator E
= LibraryList
.end();
391 LibraryList
.erase(I
);