blob: 429cf1a4c69508066e17a9f8569a22ce7a21871e [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- Module.cpp - Implement the Module class ---------------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the Module class for the VMCore library.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner00950542001-06-06 20:29:01 +000014#include "llvm/Module.h"
Chris Lattner28bf86a2001-06-30 04:35:40 +000015#include "llvm/InstrTypes.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000016#include "llvm/Constants.h"
Chris Lattner6056c492002-03-29 03:44:18 +000017#include "llvm/DerivedTypes.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000018#include "llvm/ADT/STLExtras.h"
Owen Anderson1d8b8532006-05-18 02:10:31 +000019#include "llvm/ADT/StringExtras.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000020#include "llvm/Support/LeakDetector.h"
Chris Lattner7e708292002-06-25 16:13:24 +000021#include "SymbolTableListTraitsImpl.h"
Reid Spencer78d033e2007-01-06 07:24:44 +000022#include "llvm/TypeSymbolTable.h"
Chris Lattner7e708292002-06-25 16:13:24 +000023#include <algorithm>
Chris Lattner0ae8e872003-08-31 00:19:28 +000024#include <cstdarg>
Owen Anderson1d8b8532006-05-18 02:10:31 +000025#include <cstdlib>
Chris Lattner31f84992003-11-21 20:23:48 +000026using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Chris Lattner60837822003-12-31 08:43:01 +000028//===----------------------------------------------------------------------===//
Misha Brukmanaf76cfb2004-04-21 18:27:56 +000029// Methods to implement the globals and functions lists.
Chris Lattner60837822003-12-31 08:43:01 +000030//
31
Chris Lattnerbca81442005-01-30 00:09:23 +000032Function *ilist_traits<Function>::createSentinel() {
Chris Lattnerd1e693f2002-09-08 18:59:35 +000033 FunctionType *FTy =
Reid Spencer8129a392007-04-09 06:12:07 +000034 FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false);
Gabor Greif051a9502008-04-06 20:25:17 +000035 Function *Ret = Function::Create(FTy, GlobalValue::ExternalLinkage);
Chris Lattnerd1e693f2002-09-08 18:59:35 +000036 // This should not be garbage monitored.
37 LeakDetector::removeGarbageObject(Ret);
38 return Ret;
Chris Lattner7e708292002-06-25 16:13:24 +000039}
Chris Lattnerbca81442005-01-30 00:09:23 +000040GlobalVariable *ilist_traits<GlobalVariable>::createSentinel() {
Reid Spencer79e21d32006-12-31 05:26:44 +000041 GlobalVariable *Ret = new GlobalVariable(Type::Int32Ty, false,
Chris Lattner4ad02e72003-04-16 20:28:45 +000042 GlobalValue::ExternalLinkage);
Chris Lattnerd1e693f2002-09-08 18:59:35 +000043 // This should not be garbage monitored.
44 LeakDetector::removeGarbageObject(Ret);
45 return Ret;
Chris Lattner7e708292002-06-25 16:13:24 +000046}
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +000047GlobalAlias *ilist_traits<GlobalAlias>::createSentinel() {
Dan Gohman97cf759b2008-01-29 12:09:55 +000048 GlobalAlias *Ret = new GlobalAlias(Type::Int32Ty,
49 GlobalValue::ExternalLinkage);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +000050 // This should not be garbage monitored.
51 LeakDetector::removeGarbageObject(Ret);
52 return Ret;
53}
Chris Lattner7e708292002-06-25 16:13:24 +000054
55iplist<Function> &ilist_traits<Function>::getList(Module *M) {
56 return M->getFunctionList();
57}
58iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) {
59 return M->getGlobalList();
60}
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +000061iplist<GlobalAlias> &ilist_traits<GlobalAlias>::getList(Module *M) {
62 return M->getAliasList();
63}
Chris Lattner7e708292002-06-25 16:13:24 +000064
65// Explicit instantiations of SymbolTableListTraits since some of the methods
Chris Lattnercc041ba2006-01-24 04:13:11 +000066// are not in the public header file.
Chris Lattner17fcdd52007-04-17 03:26:42 +000067template class SymbolTableListTraits<GlobalVariable, Module>;
68template class SymbolTableListTraits<Function, Module>;
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +000069template class SymbolTableListTraits<GlobalAlias, Module>;
Chris Lattner00950542001-06-06 20:29:01 +000070
Chris Lattner60837822003-12-31 08:43:01 +000071//===----------------------------------------------------------------------===//
72// Primitive Module methods.
73//
Chris Lattnerdd6dfbc2001-10-13 06:58:40 +000074
Chris Lattner678c6a02003-04-22 18:02:04 +000075Module::Module(const std::string &MID)
Owen Anderson1d8b8532006-05-18 02:10:31 +000076 : ModuleID(MID), DataLayout("") {
Reid Spenceref9b9a72007-02-05 20:47:22 +000077 ValSymTab = new ValueSymbolTable();
Reid Spencer78d033e2007-01-06 07:24:44 +000078 TypeSymTab = new TypeSymbolTable();
Chris Lattner00950542001-06-06 20:29:01 +000079}
80
81Module::~Module() {
82 dropAllReferences();
Chris Lattner7e708292002-06-25 16:13:24 +000083 GlobalList.clear();
Chris Lattner7e708292002-06-25 16:13:24 +000084 FunctionList.clear();
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +000085 AliasList.clear();
Reid Spencer801db472004-07-25 18:08:57 +000086 LibraryList.clear();
Reid Spencer78d033e2007-01-06 07:24:44 +000087 delete ValSymTab;
88 delete TypeSymTab;
Chris Lattner00950542001-06-06 20:29:01 +000089}
90
Chris Lattner9da07152002-08-17 23:32:47 +000091// Module::dump() - Allow printing from debugger
92void Module::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +000093 print(*cerr.stream());
Chris Lattner9da07152002-08-17 23:32:47 +000094}
95
Owen Anderson1d8b8532006-05-18 02:10:31 +000096/// Target endian information...
97Module::Endianness Module::getEndianness() const {
98 std::string temp = DataLayout;
Owen Andersonac4c75b2006-05-18 05:46:08 +000099 Module::Endianness ret = AnyEndianness;
Owen Anderson1d8b8532006-05-18 02:10:31 +0000100
Owen Andersonac4c75b2006-05-18 05:46:08 +0000101 while (!temp.empty()) {
Owen Anderson1d8b8532006-05-18 02:10:31 +0000102 std::string token = getToken(temp, "-");
103
104 if (token[0] == 'e') {
Owen Andersonac4c75b2006-05-18 05:46:08 +0000105 ret = LittleEndian;
Owen Anderson1d8b8532006-05-18 02:10:31 +0000106 } else if (token[0] == 'E') {
Owen Andersonac4c75b2006-05-18 05:46:08 +0000107 ret = BigEndian;
Owen Anderson1d8b8532006-05-18 02:10:31 +0000108 }
109 }
110
Owen Andersonac4c75b2006-05-18 05:46:08 +0000111 return ret;
Owen Anderson1d8b8532006-05-18 02:10:31 +0000112}
113
Owen Anderson1d8b8532006-05-18 02:10:31 +0000114/// Target Pointer Size information...
115Module::PointerSize Module::getPointerSize() const {
116 std::string temp = DataLayout;
Owen Andersonac4c75b2006-05-18 05:46:08 +0000117 Module::PointerSize ret = AnyPointerSize;
Owen Anderson1d8b8532006-05-18 02:10:31 +0000118
Owen Andersonac4c75b2006-05-18 05:46:08 +0000119 while (!temp.empty()) {
Owen Anderson1d8b8532006-05-18 02:10:31 +0000120 std::string token = getToken(temp, "-");
121 char signal = getToken(token, ":")[0];
122
123 if (signal == 'p') {
124 int size = atoi(getToken(token, ":").c_str());
125 if (size == 32)
Owen Andersonac4c75b2006-05-18 05:46:08 +0000126 ret = Pointer32;
Owen Anderson1d8b8532006-05-18 02:10:31 +0000127 else if (size == 64)
Owen Andersonac4c75b2006-05-18 05:46:08 +0000128 ret = Pointer64;
Owen Anderson1d8b8532006-05-18 02:10:31 +0000129 }
130 }
131
Owen Andersonac4c75b2006-05-18 05:46:08 +0000132 return ret;
Owen Anderson1d8b8532006-05-18 02:10:31 +0000133}
134
Chris Lattner60837822003-12-31 08:43:01 +0000135//===----------------------------------------------------------------------===//
136// Methods for easy access to the functions in the module.
137//
138
Reid Spenceref9b9a72007-02-05 20:47:22 +0000139// 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.
143//
Chris Lattner70d13052007-01-07 08:09:25 +0000144Constant *Module::getOrInsertFunction(const std::string &Name,
Chris Lattner6056c492002-03-29 03:44:18 +0000145 const FunctionType *Ty) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000146 ValueSymbolTable &SymTab = getValueSymbolTable();
Chris Lattner6056c492002-03-29 03:44:18 +0000147
Reid Spenceref9b9a72007-02-05 20:47:22 +0000148 // See if we have a definition for the specified function already.
Reid Spencer688b0492007-02-05 21:19:13 +0000149 GlobalValue *F = dyn_cast_or_null<GlobalValue>(SymTab.lookup(Name));
Chris Lattner70d13052007-01-07 08:09:25 +0000150 if (F == 0) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000151 // Nope, add it
Gabor Greif051a9502008-04-06 20:25:17 +0000152 Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name);
Chris Lattner6056c492002-03-29 03:44:18 +0000153 FunctionList.push_back(New);
Chris Lattner70d13052007-01-07 08:09:25 +0000154 return New; // Return the new prototype.
Chris Lattner6056c492002-03-29 03:44:18 +0000155 }
Chris Lattner70d13052007-01-07 08:09:25 +0000156
157 // Okay, the function exists. Does it have externally visible linkage?
158 if (F->hasInternalLinkage()) {
159 // Rename the function.
Reid Spenceref9b9a72007-02-05 20:47:22 +0000160 F->setName(SymTab.getUniqueName(F->getName()));
Chris Lattner70d13052007-01-07 08:09:25 +0000161 // Retry, now there won't be a conflict.
162 return getOrInsertFunction(Name, Ty);
163 }
164
165 // If the function exists but has the wrong type, return a bitcast to the
166 // right type.
Christopher Lamb43ad6b32007-12-17 01:12:55 +0000167 if (F->getType() != PointerType::getUnqual(Ty))
168 return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty));
Chris Lattner70d13052007-01-07 08:09:25 +0000169
170 // Otherwise, we just found the existing function or a prototype.
171 return F;
Chris Lattner6056c492002-03-29 03:44:18 +0000172}
173
Chris Lattner0ae8e872003-08-31 00:19:28 +0000174// getOrInsertFunction - Look up the specified function in the module symbol
175// table. If it does not exist, add a prototype for the function and return it.
176// This version of the method takes a null terminated list of function
177// arguments, which makes it easier for clients to use.
178//
Chris Lattner70d13052007-01-07 08:09:25 +0000179Constant *Module::getOrInsertFunction(const std::string &Name,
Chris Lattner0ae8e872003-08-31 00:19:28 +0000180 const Type *RetTy, ...) {
181 va_list Args;
182 va_start(Args, RetTy);
183
184 // Build the list of argument types...
185 std::vector<const Type*> ArgTys;
186 while (const Type *ArgTy = va_arg(Args, const Type*))
187 ArgTys.push_back(ArgTy);
188
189 va_end(Args);
190
191 // Build the function type and chain to the other getOrInsertFunction...
192 return getOrInsertFunction(Name, FunctionType::get(RetTy, ArgTys, false));
193}
194
195
Chris Lattner6056c492002-03-29 03:44:18 +0000196// getFunction - Look up the specified function in the module symbol table.
197// If it does not exist, return null.
198//
Reid Spenceref9b9a72007-02-05 20:47:22 +0000199Function *Module::getFunction(const std::string &Name) const {
200 const ValueSymbolTable &SymTab = getValueSymbolTable();
201 return dyn_cast_or_null<Function>(SymTab.lookup(Name));
Chris Lattnera9a93f32002-11-08 20:34:02 +0000202}
203
Chris Lattner60837822003-12-31 08:43:01 +0000204//===----------------------------------------------------------------------===//
205// Methods for easy access to the global variables in the module.
206//
207
208/// getGlobalVariable - Look up the specified global variable in the module
Chris Lattner30614672005-12-05 05:30:21 +0000209/// symbol table. If it does not exist, return null. The type argument
210/// should be the underlying type of the global, i.e., it should not have
211/// the top-level PointerType, which represents the address of the global.
212/// If AllowInternal is set to true, this function will return types that
213/// have InternalLinkage. By default, these types are not returned.
Chris Lattner60837822003-12-31 08:43:01 +0000214///
Misha Brukmanfd939082005-04-21 23:48:37 +0000215GlobalVariable *Module::getGlobalVariable(const std::string &Name,
Reid Spenceref9b9a72007-02-05 20:47:22 +0000216 bool AllowInternal) const {
217 if (Value *V = ValSymTab->lookup(Name)) {
218 GlobalVariable *Result = dyn_cast<GlobalVariable>(V);
219 if (Result && (AllowInternal || !Result->hasInternalLinkage()))
Chris Lattner60837822003-12-31 08:43:01 +0000220 return Result;
221 }
222 return 0;
223}
224
Chris Lattner60837822003-12-31 08:43:01 +0000225//===----------------------------------------------------------------------===//
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000226// Methods for easy access to the global variables in the module.
227//
228
229// getNamedAlias - Look up the specified global in the module symbol table.
230// If it does not exist, return null.
231//
232GlobalAlias *Module::getNamedAlias(const std::string &Name) const {
233 const ValueSymbolTable &SymTab = getValueSymbolTable();
234 return dyn_cast_or_null<GlobalAlias>(SymTab.lookup(Name));
235}
236
237//===----------------------------------------------------------------------===//
Chris Lattner60837822003-12-31 08:43:01 +0000238// Methods for easy access to the types in the module.
239//
240
Chris Lattnera9a93f32002-11-08 20:34:02 +0000241
Chris Lattner6d28a262003-12-31 07:09:33 +0000242// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
243// there is already an entry for this name, true is returned and the symbol
244// table is not modified.
245//
246bool Module::addTypeName(const std::string &Name, const Type *Ty) {
Reid Spencer78d033e2007-01-06 07:24:44 +0000247 TypeSymbolTable &ST = getTypeSymbolTable();
Chris Lattner6d28a262003-12-31 07:09:33 +0000248
Reid Spencer78d033e2007-01-06 07:24:44 +0000249 if (ST.lookup(Name)) return true; // Already in symtab...
Misha Brukmanfd939082005-04-21 23:48:37 +0000250
Chris Lattner6d28a262003-12-31 07:09:33 +0000251 // Not in symbol table? Set the name with the Symtab as an argument so the
252 // type knows what to update...
Reid Spencer3ba415a2004-07-10 16:37:42 +0000253 ST.insert(Name, Ty);
Chris Lattner6d28a262003-12-31 07:09:33 +0000254
255 return false;
256}
257
258/// getTypeByName - Return the type with the specified name in this module, or
259/// null if there is none by that name.
260const Type *Module::getTypeByName(const std::string &Name) const {
Reid Spencer78d033e2007-01-06 07:24:44 +0000261 const TypeSymbolTable &ST = getTypeSymbolTable();
262 return cast_or_null<Type>(ST.lookup(Name));
Chris Lattner6d28a262003-12-31 07:09:33 +0000263}
Chris Lattnera9a93f32002-11-08 20:34:02 +0000264
Chris Lattnerf33fa6f2002-04-13 18:58:33 +0000265// getTypeName - If there is at least one entry in the symbol table for the
266// specified type, return it.
267//
Chris Lattner6d28a262003-12-31 07:09:33 +0000268std::string Module::getTypeName(const Type *Ty) const {
Reid Spencer78d033e2007-01-06 07:24:44 +0000269 const TypeSymbolTable &ST = getTypeSymbolTable();
Chris Lattnerf33fa6f2002-04-13 18:58:33 +0000270
Reid Spencer78d033e2007-01-06 07:24:44 +0000271 TypeSymbolTable::const_iterator TI = ST.begin();
272 TypeSymbolTable::const_iterator TE = ST.end();
Reid Spencer567bc2c2004-05-25 08:52:20 +0000273 if ( TI == TE ) return ""; // No names for types
Chris Lattnerf33fa6f2002-04-13 18:58:33 +0000274
Reid Spencer567bc2c2004-05-25 08:52:20 +0000275 while (TI != TE && TI->second != Ty)
Chris Lattnerf33fa6f2002-04-13 18:58:33 +0000276 ++TI;
277
278 if (TI != TE) // Must have found an entry!
279 return TI->first;
280 return ""; // Must not have found anything...
281}
282
Chris Lattner60837822003-12-31 08:43:01 +0000283//===----------------------------------------------------------------------===//
284// Other module related stuff.
285//
286
287
Chris Lattner9da07152002-08-17 23:32:47 +0000288// dropAllReferences() - This function causes all the subelementss to "let go"
289// of all references that they are maintaining. This allows one to 'delete' a
290// whole module at a time, even though there may be circular references... first
291// all references are dropped, and all use counts go to zero. Then everything
Misha Brukman6b634522003-10-10 17:54:14 +0000292// is deleted for real. Note that no operations are valid on an object that
Chris Lattner9da07152002-08-17 23:32:47 +0000293// has "dropped all references", except operator delete.
Chris Lattner00950542001-06-06 20:29:01 +0000294//
295void Module::dropAllReferences() {
Chris Lattner7e708292002-06-25 16:13:24 +0000296 for(Module::iterator I = begin(), E = end(); I != E; ++I)
297 I->dropAllReferences();
Chris Lattnerdd6dfbc2001-10-13 06:58:40 +0000298
Chris Lattnere4d5c442005-03-15 04:54:21 +0000299 for(Module::global_iterator I = global_begin(), E = global_end(); I != E; ++I)
Chris Lattner7e708292002-06-25 16:13:24 +0000300 I->dropAllReferences();
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000301
302 for(Module::alias_iterator I = alias_begin(), E = alias_end(); I != E; ++I)
303 I->dropAllReferences();
Chris Lattner00950542001-06-06 20:29:01 +0000304}
Chris Lattner28bf86a2001-06-30 04:35:40 +0000305
Reid Spencer6734b572007-02-04 00:40:42 +0000306void Module::addLibrary(const std::string& Lib) {
307 for (Module::lib_iterator I = lib_begin(), E = lib_end(); I != E; ++I)
308 if (*I == Lib)
309 return;
310 LibraryList.push_back(Lib);
311}
312
313void Module::removeLibrary(const std::string& Lib) {
314 LibraryListType::iterator I = LibraryList.begin();
315 LibraryListType::iterator E = LibraryList.end();
316 for (;I != E; ++I)
317 if (*I == Lib) {
318 LibraryList.erase(I);
319 return;
320 }
321}
322