blob: 8d924deb851356cefbcb6b511e6ae301629aef81 [file] [log] [blame]
Chris Lattner44d2c352003-10-13 03:32:08 +00001//===-- Module.cpp - Implement the Module class ---------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
10// This file implements the Module class for the VMCore library.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner2f7c9632001-06-06 20:29:01 +000014#include "llvm/Module.h"
Chris Lattner31cf9842001-06-30 04:35:40 +000015#include "llvm/InstrTypes.h"
Chris Lattnerca142372002-04-28 19:55:58 +000016#include "llvm/Constants.h"
Chris Lattnera483b062002-03-29 03:44:18 +000017#include "llvm/DerivedTypes.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000018#include "llvm/ADT/STLExtras.h"
Owen Anderson9eb1a262006-05-18 02:10:31 +000019#include "llvm/ADT/StringExtras.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000020#include "llvm/Support/LeakDetector.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000021#include "SymbolTableListTraitsImpl.h"
22#include <algorithm>
Chris Lattnerbd717d82003-08-31 00:19:28 +000023#include <cstdarg>
Owen Anderson9eb1a262006-05-18 02:10:31 +000024#include <cstdlib>
Chris Lattner446ad502001-10-13 06:58:40 +000025#include <map>
Chris Lattner189d19f2003-11-21 20:23:48 +000026using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000027
Chris Lattner09bd1a02003-12-31 08:43:01 +000028//===----------------------------------------------------------------------===//
Misha Brukman3bcead72004-04-21 18:27:56 +000029// Methods to implement the globals and functions lists.
Chris Lattner09bd1a02003-12-31 08:43:01 +000030//
31
Chris Lattnerf6c93e32005-01-30 00:09:23 +000032Function *ilist_traits<Function>::createSentinel() {
Chris Lattner184b2982002-09-08 18:59:35 +000033 FunctionType *FTy =
34 FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false);
Chris Lattner379a8d22003-04-16 20:28:45 +000035 Function *Ret = new Function(FTy, GlobalValue::ExternalLinkage);
Chris Lattner184b2982002-09-08 18:59:35 +000036 // This should not be garbage monitored.
37 LeakDetector::removeGarbageObject(Ret);
38 return Ret;
Chris Lattner113f4f42002-06-25 16:13:24 +000039}
Chris Lattnerf6c93e32005-01-30 00:09:23 +000040GlobalVariable *ilist_traits<GlobalVariable>::createSentinel() {
Chris Lattner379a8d22003-04-16 20:28:45 +000041 GlobalVariable *Ret = new GlobalVariable(Type::IntTy, false,
42 GlobalValue::ExternalLinkage);
Chris Lattner184b2982002-09-08 18:59:35 +000043 // This should not be garbage monitored.
44 LeakDetector::removeGarbageObject(Ret);
45 return Ret;
Chris Lattner113f4f42002-06-25 16:13:24 +000046}
47
48iplist<Function> &ilist_traits<Function>::getList(Module *M) {
49 return M->getFunctionList();
50}
51iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) {
52 return M->getGlobalList();
53}
54
55// Explicit instantiations of SymbolTableListTraits since some of the methods
Chris Lattnereef2fe72006-01-24 04:13:11 +000056// are not in the public header file.
Chris Lattner41baa982003-11-05 05:15:42 +000057template class SymbolTableListTraits<GlobalVariable, Module, Module>;
58template class SymbolTableListTraits<Function, Module, Module>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000059
Chris Lattner09bd1a02003-12-31 08:43:01 +000060//===----------------------------------------------------------------------===//
61// Primitive Module methods.
62//
Chris Lattner446ad502001-10-13 06:58:40 +000063
Chris Lattnerc3f6e002003-04-22 18:02:04 +000064Module::Module(const std::string &MID)
Owen Anderson9eb1a262006-05-18 02:10:31 +000065 : ModuleID(MID), DataLayout("") {
Chris Lattner113f4f42002-06-25 16:13:24 +000066 FunctionList.setItemParent(this);
67 FunctionList.setParent(this);
68 GlobalList.setItemParent(this);
69 GlobalList.setParent(this);
Chris Lattner98cf1f52002-11-20 18:36:02 +000070 SymTab = new SymbolTable();
Chris Lattner2f7c9632001-06-06 20:29:01 +000071}
72
73Module::~Module() {
74 dropAllReferences();
Chris Lattner113f4f42002-06-25 16:13:24 +000075 GlobalList.clear();
Chris Lattnerda975502001-09-10 07:58:01 +000076 GlobalList.setParent(0);
Chris Lattner113f4f42002-06-25 16:13:24 +000077 FunctionList.clear();
Chris Lattner57698e22002-03-26 18:01:55 +000078 FunctionList.setParent(0);
Reid Spencera0b05b32004-07-25 18:08:57 +000079 LibraryList.clear();
Chris Lattner2c8ff632002-04-28 04:51:51 +000080 delete SymTab;
Chris Lattner2f7c9632001-06-06 20:29:01 +000081}
82
Chris Lattnere0f6af9b2002-08-17 23:32:47 +000083// Module::dump() - Allow printing from debugger
84void Module::dump() const {
Bill Wendling22e978a2006-12-07 20:04:42 +000085 print(*cerr.stream());
Chris Lattnere0f6af9b2002-08-17 23:32:47 +000086}
87
Owen Anderson9eb1a262006-05-18 02:10:31 +000088/// Target endian information...
89Module::Endianness Module::getEndianness() const {
90 std::string temp = DataLayout;
Owen Anderson08aecf52006-05-18 05:46:08 +000091 Module::Endianness ret = AnyEndianness;
Owen Anderson9eb1a262006-05-18 02:10:31 +000092
Owen Anderson08aecf52006-05-18 05:46:08 +000093 while (!temp.empty()) {
Owen Anderson9eb1a262006-05-18 02:10:31 +000094 std::string token = getToken(temp, "-");
95
96 if (token[0] == 'e') {
Owen Anderson08aecf52006-05-18 05:46:08 +000097 ret = LittleEndian;
Owen Anderson9eb1a262006-05-18 02:10:31 +000098 } else if (token[0] == 'E') {
Owen Anderson08aecf52006-05-18 05:46:08 +000099 ret = BigEndian;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000100 }
101 }
102
Owen Anderson08aecf52006-05-18 05:46:08 +0000103 return ret;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000104}
105
106void Module::setEndianness(Endianness E) {
Owen Anderson08aecf52006-05-18 05:46:08 +0000107 if (!DataLayout.empty() && E != AnyEndianness)
108 DataLayout += "-";
Owen Anderson9eb1a262006-05-18 02:10:31 +0000109
110 if (E == LittleEndian)
Owen Anderson08aecf52006-05-18 05:46:08 +0000111 DataLayout += "e";
Owen Anderson9eb1a262006-05-18 02:10:31 +0000112 else if (E == BigEndian)
Owen Anderson08aecf52006-05-18 05:46:08 +0000113 DataLayout += "E";
Owen Anderson9eb1a262006-05-18 02:10:31 +0000114}
115
116/// Target Pointer Size information...
117Module::PointerSize Module::getPointerSize() const {
118 std::string temp = DataLayout;
Owen Anderson08aecf52006-05-18 05:46:08 +0000119 Module::PointerSize ret = AnyPointerSize;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000120
Owen Anderson08aecf52006-05-18 05:46:08 +0000121 while (!temp.empty()) {
Owen Anderson9eb1a262006-05-18 02:10:31 +0000122 std::string token = getToken(temp, "-");
123 char signal = getToken(token, ":")[0];
124
125 if (signal == 'p') {
126 int size = atoi(getToken(token, ":").c_str());
127 if (size == 32)
Owen Anderson08aecf52006-05-18 05:46:08 +0000128 ret = Pointer32;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000129 else if (size == 64)
Owen Anderson08aecf52006-05-18 05:46:08 +0000130 ret = Pointer64;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000131 }
132 }
133
Owen Anderson08aecf52006-05-18 05:46:08 +0000134 return ret;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000135}
136
137void Module::setPointerSize(PointerSize PS) {
Owen Anderson08aecf52006-05-18 05:46:08 +0000138 if (!DataLayout.empty() && PS != AnyPointerSize)
139 DataLayout += "-";
Owen Anderson9eb1a262006-05-18 02:10:31 +0000140
141 if (PS == Pointer32)
Owen Anderson08aecf52006-05-18 05:46:08 +0000142 DataLayout += "p:32:32";
Owen Anderson9eb1a262006-05-18 02:10:31 +0000143 else if (PS == Pointer64)
Owen Anderson08aecf52006-05-18 05:46:08 +0000144 DataLayout += "p:64:64";
Owen Anderson9eb1a262006-05-18 02:10:31 +0000145}
146
Chris Lattner09bd1a02003-12-31 08:43:01 +0000147//===----------------------------------------------------------------------===//
148// Methods for easy access to the functions in the module.
149//
150
Chris Lattnera483b062002-03-29 03:44:18 +0000151// getOrInsertFunction - Look up the specified function in the module symbol
152// table. If it does not exist, add a prototype for the function and return
153// it. This is nice because it allows most passes to get away with not handling
154// the symbol table directly for this common task.
155//
156Function *Module::getOrInsertFunction(const std::string &Name,
157 const FunctionType *Ty) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000158 SymbolTable &SymTab = getSymbolTable();
Chris Lattnera483b062002-03-29 03:44:18 +0000159
160 // See if we have a definitions for the specified function already...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000161 if (Value *V = SymTab.lookup(PointerType::get(Ty), Name)) {
Chris Lattnera483b062002-03-29 03:44:18 +0000162 return cast<Function>(V); // Yup, got it
163 } else { // Nope, add one
Chris Lattner379a8d22003-04-16 20:28:45 +0000164 Function *New = new Function(Ty, GlobalVariable::ExternalLinkage, Name);
Chris Lattnera483b062002-03-29 03:44:18 +0000165 FunctionList.push_back(New);
166 return New; // Return the new prototype...
167 }
168}
169
Chris Lattnerbd717d82003-08-31 00:19:28 +0000170// getOrInsertFunction - Look up the specified function in the module symbol
171// table. If it does not exist, add a prototype for the function and return it.
172// This version of the method takes a null terminated list of function
173// arguments, which makes it easier for clients to use.
174//
175Function *Module::getOrInsertFunction(const std::string &Name,
176 const Type *RetTy, ...) {
177 va_list Args;
178 va_start(Args, RetTy);
179
180 // Build the list of argument types...
181 std::vector<const Type*> ArgTys;
182 while (const Type *ArgTy = va_arg(Args, const Type*))
183 ArgTys.push_back(ArgTy);
184
185 va_end(Args);
186
187 // Build the function type and chain to the other getOrInsertFunction...
188 return getOrInsertFunction(Name, FunctionType::get(RetTy, ArgTys, false));
189}
190
191
Chris Lattnera483b062002-03-29 03:44:18 +0000192// getFunction - Look up the specified function in the module symbol table.
193// If it does not exist, return null.
194//
195Function *Module::getFunction(const std::string &Name, const FunctionType *Ty) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000196 SymbolTable &SymTab = getSymbolTable();
197 return cast_or_null<Function>(SymTab.lookup(PointerType::get(Ty), Name));
Chris Lattnera483b062002-03-29 03:44:18 +0000198}
199
200
Chris Lattner1f985e02002-11-08 20:34:02 +0000201/// getMainFunction - This function looks up main efficiently. This is such a
202/// common case, that it is a method in Module. If main cannot be found, a
203/// null pointer is returned.
204///
205Function *Module::getMainFunction() {
206 std::vector<const Type*> Params;
207
208 // int main(void)...
209 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
210 Params, false)))
211 return F;
212
213 // void main(void)...
214 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
215 Params, false)))
216 return F;
217
218 Params.push_back(Type::IntTy);
219
220 // int main(int argc)...
221 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
222 Params, false)))
223 return F;
224
225 // void main(int argc)...
226 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
227 Params, false)))
228 return F;
229
230 for (unsigned i = 0; i != 2; ++i) { // Check argv and envp
231 Params.push_back(PointerType::get(PointerType::get(Type::SByteTy)));
232
233 // int main(int argc, char **argv)...
234 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
235 Params, false)))
236 return F;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000237
Chris Lattner1f985e02002-11-08 20:34:02 +0000238 // void main(int argc, char **argv)...
239 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
240 Params, false)))
241 return F;
242 }
243
Chris Lattnerb2e46c02002-11-19 18:41:44 +0000244 // Ok, try to find main the hard way...
245 return getNamedFunction("main");
246}
247
248/// getNamedFunction - Return the first function in the module with the
249/// specified name, of arbitrary type. This method returns null if a function
250/// with the specified name is not found.
251///
Reid Spencer9fef1632006-05-31 16:40:28 +0000252Function *Module::getNamedFunction(const std::string &Name) const {
Chris Lattnerb2e46c02002-11-19 18:41:44 +0000253 // Loop over all of the functions, looking for the function desired
Reid Spencer9fef1632006-05-31 16:40:28 +0000254 const Function *Found = 0;
255 for (const_iterator I = begin(), E = end(); I != E; ++I)
Chris Lattnerb2e46c02002-11-19 18:41:44 +0000256 if (I->getName() == Name)
Chris Lattner4b4dacd2003-07-23 20:21:30 +0000257 if (I->isExternal())
258 Found = I;
259 else
Reid Spencer9fef1632006-05-31 16:40:28 +0000260 return const_cast<Function*>(&(*I));
261 return const_cast<Function*>(Found); // Non-external function not found...
Chris Lattner1f985e02002-11-08 20:34:02 +0000262}
263
Chris Lattner09bd1a02003-12-31 08:43:01 +0000264//===----------------------------------------------------------------------===//
265// Methods for easy access to the global variables in the module.
266//
267
268/// getGlobalVariable - Look up the specified global variable in the module
Chris Lattner7d4d93c2005-12-05 05:30:21 +0000269/// symbol table. If it does not exist, return null. The type argument
270/// should be the underlying type of the global, i.e., it should not have
271/// the top-level PointerType, which represents the address of the global.
272/// If AllowInternal is set to true, this function will return types that
273/// have InternalLinkage. By default, these types are not returned.
Chris Lattner09bd1a02003-12-31 08:43:01 +0000274///
Misha Brukmanb1c93172005-04-21 23:48:37 +0000275GlobalVariable *Module::getGlobalVariable(const std::string &Name,
Chris Lattner7d4d93c2005-12-05 05:30:21 +0000276 const Type *Ty, bool AllowInternal) {
Chris Lattner09bd1a02003-12-31 08:43:01 +0000277 if (Value *V = getSymbolTable().lookup(PointerType::get(Ty), Name)) {
278 GlobalVariable *Result = cast<GlobalVariable>(V);
Chris Lattner7d4d93c2005-12-05 05:30:21 +0000279 if (AllowInternal || !Result->hasInternalLinkage())
Chris Lattner09bd1a02003-12-31 08:43:01 +0000280 return Result;
281 }
282 return 0;
283}
284
Chris Lattner48a8e092006-03-08 18:39:13 +0000285/// getNamedGlobal - Return the first global variable in the module with the
286/// specified name, of arbitrary type. This method returns null if a global
287/// with the specified name is not found.
288///
Reid Spencer9fef1632006-05-31 16:40:28 +0000289GlobalVariable *Module::getNamedGlobal(const std::string &Name) const {
Chris Lattner48a8e092006-03-08 18:39:13 +0000290 // FIXME: This would be much faster with a symbol table that doesn't
291 // discriminate based on type!
Reid Spencer9fef1632006-05-31 16:40:28 +0000292 for (const_global_iterator I = global_begin(), E = global_end();
Chris Lattner48a8e092006-03-08 18:39:13 +0000293 I != E; ++I)
294 if (I->getName() == Name)
Reid Spencer9fef1632006-05-31 16:40:28 +0000295 return const_cast<GlobalVariable*>(&(*I));
Chris Lattner48a8e092006-03-08 18:39:13 +0000296 return 0;
297}
298
Chris Lattner09bd1a02003-12-31 08:43:01 +0000299
300
301//===----------------------------------------------------------------------===//
302// Methods for easy access to the types in the module.
303//
304
Chris Lattner1f985e02002-11-08 20:34:02 +0000305
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000306// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
307// there is already an entry for this name, true is returned and the symbol
308// table is not modified.
309//
310bool Module::addTypeName(const std::string &Name, const Type *Ty) {
311 SymbolTable &ST = getSymbolTable();
312
Reid Spencerabb6f002004-05-25 08:52:20 +0000313 if (ST.lookupType(Name)) return true; // Already in symtab...
Misha Brukmanb1c93172005-04-21 23:48:37 +0000314
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000315 // Not in symbol table? Set the name with the Symtab as an argument so the
316 // type knows what to update...
Reid Spencerf9776c32004-07-10 16:37:42 +0000317 ST.insert(Name, Ty);
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000318
319 return false;
320}
321
322/// getTypeByName - Return the type with the specified name in this module, or
323/// null if there is none by that name.
324const Type *Module::getTypeByName(const std::string &Name) const {
325 const SymbolTable &ST = getSymbolTable();
Reid Spencerabb6f002004-05-25 08:52:20 +0000326 return cast_or_null<Type>(ST.lookupType(Name));
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000327}
Chris Lattner1f985e02002-11-08 20:34:02 +0000328
Chris Lattner10b7cb52002-04-13 18:58:33 +0000329// getTypeName - If there is at least one entry in the symbol table for the
330// specified type, return it.
331//
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000332std::string Module::getTypeName(const Type *Ty) const {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000333 const SymbolTable &ST = getSymbolTable();
Chris Lattner10b7cb52002-04-13 18:58:33 +0000334
Reid Spencerabb6f002004-05-25 08:52:20 +0000335 SymbolTable::type_const_iterator TI = ST.type_begin();
336 SymbolTable::type_const_iterator TE = ST.type_end();
337 if ( TI == TE ) return ""; // No names for types
Chris Lattner10b7cb52002-04-13 18:58:33 +0000338
Reid Spencerabb6f002004-05-25 08:52:20 +0000339 while (TI != TE && TI->second != Ty)
Chris Lattner10b7cb52002-04-13 18:58:33 +0000340 ++TI;
341
342 if (TI != TE) // Must have found an entry!
343 return TI->first;
344 return ""; // Must not have found anything...
345}
346
Chris Lattner09bd1a02003-12-31 08:43:01 +0000347//===----------------------------------------------------------------------===//
348// Other module related stuff.
349//
350
351
Chris Lattnere0f6af9b2002-08-17 23:32:47 +0000352// dropAllReferences() - This function causes all the subelementss to "let go"
353// of all references that they are maintaining. This allows one to 'delete' a
354// whole module at a time, even though there may be circular references... first
355// all references are dropped, and all use counts go to zero. Then everything
Misha Brukmanfa100532003-10-10 17:54:14 +0000356// is deleted for real. Note that no operations are valid on an object that
Chris Lattnere0f6af9b2002-08-17 23:32:47 +0000357// has "dropped all references", except operator delete.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000358//
359void Module::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000360 for(Module::iterator I = begin(), E = end(); I != E; ++I)
361 I->dropAllReferences();
Chris Lattner446ad502001-10-13 06:58:40 +0000362
Chris Lattner531f9e92005-03-15 04:54:21 +0000363 for(Module::global_iterator I = global_begin(), E = global_end(); I != E; ++I)
Chris Lattner113f4f42002-06-25 16:13:24 +0000364 I->dropAllReferences();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000365}
Chris Lattner31cf9842001-06-30 04:35:40 +0000366