blob: 98faff1d3325113191ab935bd466cba11667954a [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"
Reid Spencer32af9e82007-01-06 07:24:44 +000022#include "llvm/TypeSymbolTable.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000023#include <algorithm>
Chris Lattnerbd717d82003-08-31 00:19:28 +000024#include <cstdarg>
Owen Anderson9eb1a262006-05-18 02:10:31 +000025#include <cstdlib>
Chris Lattner446ad502001-10-13 06:58:40 +000026#include <map>
Chris Lattner189d19f2003-11-21 20:23:48 +000027using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000028
Chris Lattner09bd1a02003-12-31 08:43:01 +000029//===----------------------------------------------------------------------===//
Misha Brukman3bcead72004-04-21 18:27:56 +000030// Methods to implement the globals and functions lists.
Chris Lattner09bd1a02003-12-31 08:43:01 +000031//
32
Chris Lattnerf6c93e32005-01-30 00:09:23 +000033Function *ilist_traits<Function>::createSentinel() {
Chris Lattner184b2982002-09-08 18:59:35 +000034 FunctionType *FTy =
Reid Spencer8d9336d2006-12-31 05:26:44 +000035 FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false,
36 std::vector<FunctionType::ParameterAttributes>() );
Chris Lattner379a8d22003-04-16 20:28:45 +000037 Function *Ret = new Function(FTy, GlobalValue::ExternalLinkage);
Chris Lattner184b2982002-09-08 18:59:35 +000038 // This should not be garbage monitored.
39 LeakDetector::removeGarbageObject(Ret);
40 return Ret;
Chris Lattner113f4f42002-06-25 16:13:24 +000041}
Chris Lattnerf6c93e32005-01-30 00:09:23 +000042GlobalVariable *ilist_traits<GlobalVariable>::createSentinel() {
Reid Spencer8d9336d2006-12-31 05:26:44 +000043 GlobalVariable *Ret = new GlobalVariable(Type::Int32Ty, false,
Chris Lattner379a8d22003-04-16 20:28:45 +000044 GlobalValue::ExternalLinkage);
Chris Lattner184b2982002-09-08 18:59:35 +000045 // This should not be garbage monitored.
46 LeakDetector::removeGarbageObject(Ret);
47 return Ret;
Chris Lattner113f4f42002-06-25 16:13:24 +000048}
49
50iplist<Function> &ilist_traits<Function>::getList(Module *M) {
51 return M->getFunctionList();
52}
53iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) {
54 return M->getGlobalList();
55}
56
57// Explicit instantiations of SymbolTableListTraits since some of the methods
Chris Lattnereef2fe72006-01-24 04:13:11 +000058// are not in the public header file.
Chris Lattner41baa982003-11-05 05:15:42 +000059template class SymbolTableListTraits<GlobalVariable, Module, Module>;
60template class SymbolTableListTraits<Function, Module, Module>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000061
Chris Lattner09bd1a02003-12-31 08:43:01 +000062//===----------------------------------------------------------------------===//
63// Primitive Module methods.
64//
Chris Lattner446ad502001-10-13 06:58:40 +000065
Chris Lattnerc3f6e002003-04-22 18:02:04 +000066Module::Module(const std::string &MID)
Owen Anderson9eb1a262006-05-18 02:10:31 +000067 : ModuleID(MID), DataLayout("") {
Chris Lattner113f4f42002-06-25 16:13:24 +000068 FunctionList.setItemParent(this);
69 FunctionList.setParent(this);
70 GlobalList.setItemParent(this);
71 GlobalList.setParent(this);
Reid Spencer32af9e82007-01-06 07:24:44 +000072 ValSymTab = new SymbolTable();
73 TypeSymTab = new TypeSymbolTable();
Chris Lattner2f7c9632001-06-06 20:29:01 +000074}
75
76Module::~Module() {
77 dropAllReferences();
Chris Lattner113f4f42002-06-25 16:13:24 +000078 GlobalList.clear();
Chris Lattnerda975502001-09-10 07:58:01 +000079 GlobalList.setParent(0);
Chris Lattner113f4f42002-06-25 16:13:24 +000080 FunctionList.clear();
Chris Lattner57698e22002-03-26 18:01:55 +000081 FunctionList.setParent(0);
Reid Spencera0b05b32004-07-25 18:08:57 +000082 LibraryList.clear();
Reid Spencer32af9e82007-01-06 07:24:44 +000083 delete ValSymTab;
84 delete TypeSymTab;
Chris Lattner2f7c9632001-06-06 20:29:01 +000085}
86
Chris Lattnere0f6af9b2002-08-17 23:32:47 +000087// Module::dump() - Allow printing from debugger
88void Module::dump() const {
Bill Wendling22e978a2006-12-07 20:04:42 +000089 print(*cerr.stream());
Chris Lattnere0f6af9b2002-08-17 23:32:47 +000090}
91
Owen Anderson9eb1a262006-05-18 02:10:31 +000092/// Target endian information...
93Module::Endianness Module::getEndianness() const {
94 std::string temp = DataLayout;
Owen Anderson08aecf52006-05-18 05:46:08 +000095 Module::Endianness ret = AnyEndianness;
Owen Anderson9eb1a262006-05-18 02:10:31 +000096
Owen Anderson08aecf52006-05-18 05:46:08 +000097 while (!temp.empty()) {
Owen Anderson9eb1a262006-05-18 02:10:31 +000098 std::string token = getToken(temp, "-");
99
100 if (token[0] == 'e') {
Owen Anderson08aecf52006-05-18 05:46:08 +0000101 ret = LittleEndian;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000102 } else if (token[0] == 'E') {
Owen Anderson08aecf52006-05-18 05:46:08 +0000103 ret = BigEndian;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000104 }
105 }
106
Owen Anderson08aecf52006-05-18 05:46:08 +0000107 return ret;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000108}
109
110void Module::setEndianness(Endianness E) {
Owen Anderson08aecf52006-05-18 05:46:08 +0000111 if (!DataLayout.empty() && E != AnyEndianness)
112 DataLayout += "-";
Owen Anderson9eb1a262006-05-18 02:10:31 +0000113
114 if (E == LittleEndian)
Owen Anderson08aecf52006-05-18 05:46:08 +0000115 DataLayout += "e";
Owen Anderson9eb1a262006-05-18 02:10:31 +0000116 else if (E == BigEndian)
Owen Anderson08aecf52006-05-18 05:46:08 +0000117 DataLayout += "E";
Owen Anderson9eb1a262006-05-18 02:10:31 +0000118}
119
120/// Target Pointer Size information...
121Module::PointerSize Module::getPointerSize() const {
122 std::string temp = DataLayout;
Owen Anderson08aecf52006-05-18 05:46:08 +0000123 Module::PointerSize ret = AnyPointerSize;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000124
Owen Anderson08aecf52006-05-18 05:46:08 +0000125 while (!temp.empty()) {
Owen Anderson9eb1a262006-05-18 02:10:31 +0000126 std::string token = getToken(temp, "-");
127 char signal = getToken(token, ":")[0];
128
129 if (signal == 'p') {
130 int size = atoi(getToken(token, ":").c_str());
131 if (size == 32)
Owen Anderson08aecf52006-05-18 05:46:08 +0000132 ret = Pointer32;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000133 else if (size == 64)
Owen Anderson08aecf52006-05-18 05:46:08 +0000134 ret = Pointer64;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000135 }
136 }
137
Owen Anderson08aecf52006-05-18 05:46:08 +0000138 return ret;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000139}
140
141void Module::setPointerSize(PointerSize PS) {
Owen Anderson08aecf52006-05-18 05:46:08 +0000142 if (!DataLayout.empty() && PS != AnyPointerSize)
143 DataLayout += "-";
Owen Anderson9eb1a262006-05-18 02:10:31 +0000144
145 if (PS == Pointer32)
Owen Anderson08aecf52006-05-18 05:46:08 +0000146 DataLayout += "p:32:32";
Owen Anderson9eb1a262006-05-18 02:10:31 +0000147 else if (PS == Pointer64)
Owen Anderson08aecf52006-05-18 05:46:08 +0000148 DataLayout += "p:64:64";
Owen Anderson9eb1a262006-05-18 02:10:31 +0000149}
150
Chris Lattner09bd1a02003-12-31 08:43:01 +0000151//===----------------------------------------------------------------------===//
152// Methods for easy access to the functions in the module.
153//
154
Chris Lattnera483b062002-03-29 03:44:18 +0000155// getOrInsertFunction - Look up the specified function in the module symbol
156// table. If it does not exist, add a prototype for the function and return
157// it. This is nice because it allows most passes to get away with not handling
158// the symbol table directly for this common task.
159//
160Function *Module::getOrInsertFunction(const std::string &Name,
161 const FunctionType *Ty) {
Reid Spencer32af9e82007-01-06 07:24:44 +0000162 SymbolTable &SymTab = getValueSymbolTable();
Chris Lattnera483b062002-03-29 03:44:18 +0000163
164 // See if we have a definitions for the specified function already...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000165 if (Value *V = SymTab.lookup(PointerType::get(Ty), Name)) {
Chris Lattnera483b062002-03-29 03:44:18 +0000166 return cast<Function>(V); // Yup, got it
167 } else { // Nope, add one
Chris Lattner379a8d22003-04-16 20:28:45 +0000168 Function *New = new Function(Ty, GlobalVariable::ExternalLinkage, Name);
Chris Lattnera483b062002-03-29 03:44:18 +0000169 FunctionList.push_back(New);
170 return New; // Return the new prototype...
171 }
172}
173
Chris Lattnerbd717d82003-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//
179Function *Module::getOrInsertFunction(const std::string &Name,
180 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 Lattnera483b062002-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//
199Function *Module::getFunction(const std::string &Name, const FunctionType *Ty) {
Reid Spencer32af9e82007-01-06 07:24:44 +0000200 SymbolTable &SymTab = getValueSymbolTable();
Chris Lattner98cf1f52002-11-20 18:36:02 +0000201 return cast_or_null<Function>(SymTab.lookup(PointerType::get(Ty), Name));
Chris Lattnera483b062002-03-29 03:44:18 +0000202}
203
204
Chris Lattner1f985e02002-11-08 20:34:02 +0000205/// getMainFunction - This function looks up main efficiently. This is such a
206/// common case, that it is a method in Module. If main cannot be found, a
207/// null pointer is returned.
208///
209Function *Module::getMainFunction() {
210 std::vector<const Type*> Params;
211
212 // int main(void)...
Reid Spencer8d9336d2006-12-31 05:26:44 +0000213 if (Function *F = getFunction("main", FunctionType::get(Type::Int32Ty,
Chris Lattner1f985e02002-11-08 20:34:02 +0000214 Params, false)))
215 return F;
216
217 // void main(void)...
218 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
219 Params, false)))
220 return F;
221
Reid Spencer8d9336d2006-12-31 05:26:44 +0000222 Params.push_back(Type::Int32Ty);
Chris Lattner1f985e02002-11-08 20:34:02 +0000223
224 // int main(int argc)...
Reid Spencer8d9336d2006-12-31 05:26:44 +0000225 if (Function *F = getFunction("main", FunctionType::get(Type::Int32Ty,
Chris Lattner1f985e02002-11-08 20:34:02 +0000226 Params, false)))
227 return F;
228
229 // void main(int argc)...
230 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
231 Params, false)))
232 return F;
233
234 for (unsigned i = 0; i != 2; ++i) { // Check argv and envp
Reid Spencer8d9336d2006-12-31 05:26:44 +0000235 Params.push_back(PointerType::get(PointerType::get(Type::Int8Ty)));
Chris Lattner1f985e02002-11-08 20:34:02 +0000236
237 // int main(int argc, char **argv)...
Reid Spencer8d9336d2006-12-31 05:26:44 +0000238 if (Function *F = getFunction("main", FunctionType::get(Type::Int32Ty,
Chris Lattner1f985e02002-11-08 20:34:02 +0000239 Params, false)))
240 return F;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000241
Chris Lattner1f985e02002-11-08 20:34:02 +0000242 // void main(int argc, char **argv)...
243 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
244 Params, false)))
245 return F;
246 }
247
Chris Lattnerb2e46c02002-11-19 18:41:44 +0000248 // Ok, try to find main the hard way...
249 return getNamedFunction("main");
250}
251
252/// getNamedFunction - Return the first function in the module with the
253/// specified name, of arbitrary type. This method returns null if a function
254/// with the specified name is not found.
255///
Reid Spencer9fef1632006-05-31 16:40:28 +0000256Function *Module::getNamedFunction(const std::string &Name) const {
Chris Lattnerb2e46c02002-11-19 18:41:44 +0000257 // Loop over all of the functions, looking for the function desired
Reid Spencer9fef1632006-05-31 16:40:28 +0000258 const Function *Found = 0;
259 for (const_iterator I = begin(), E = end(); I != E; ++I)
Chris Lattnerb2e46c02002-11-19 18:41:44 +0000260 if (I->getName() == Name)
Chris Lattner4b4dacd2003-07-23 20:21:30 +0000261 if (I->isExternal())
262 Found = I;
263 else
Reid Spencer9fef1632006-05-31 16:40:28 +0000264 return const_cast<Function*>(&(*I));
265 return const_cast<Function*>(Found); // Non-external function not found...
Chris Lattner1f985e02002-11-08 20:34:02 +0000266}
267
Chris Lattner09bd1a02003-12-31 08:43:01 +0000268//===----------------------------------------------------------------------===//
269// Methods for easy access to the global variables in the module.
270//
271
272/// getGlobalVariable - Look up the specified global variable in the module
Chris Lattner7d4d93c2005-12-05 05:30:21 +0000273/// symbol table. If it does not exist, return null. The type argument
274/// should be the underlying type of the global, i.e., it should not have
275/// the top-level PointerType, which represents the address of the global.
276/// If AllowInternal is set to true, this function will return types that
277/// have InternalLinkage. By default, these types are not returned.
Chris Lattner09bd1a02003-12-31 08:43:01 +0000278///
Misha Brukmanb1c93172005-04-21 23:48:37 +0000279GlobalVariable *Module::getGlobalVariable(const std::string &Name,
Chris Lattner7d4d93c2005-12-05 05:30:21 +0000280 const Type *Ty, bool AllowInternal) {
Reid Spencer32af9e82007-01-06 07:24:44 +0000281 if (Value *V = getValueSymbolTable().lookup(PointerType::get(Ty), Name)) {
Chris Lattner09bd1a02003-12-31 08:43:01 +0000282 GlobalVariable *Result = cast<GlobalVariable>(V);
Chris Lattner7d4d93c2005-12-05 05:30:21 +0000283 if (AllowInternal || !Result->hasInternalLinkage())
Chris Lattner09bd1a02003-12-31 08:43:01 +0000284 return Result;
285 }
286 return 0;
287}
288
Chris Lattner48a8e092006-03-08 18:39:13 +0000289/// getNamedGlobal - Return the first global variable in the module with the
290/// specified name, of arbitrary type. This method returns null if a global
291/// with the specified name is not found.
292///
Reid Spencer9fef1632006-05-31 16:40:28 +0000293GlobalVariable *Module::getNamedGlobal(const std::string &Name) const {
Chris Lattner48a8e092006-03-08 18:39:13 +0000294 // FIXME: This would be much faster with a symbol table that doesn't
295 // discriminate based on type!
Reid Spencer9fef1632006-05-31 16:40:28 +0000296 for (const_global_iterator I = global_begin(), E = global_end();
Chris Lattner48a8e092006-03-08 18:39:13 +0000297 I != E; ++I)
298 if (I->getName() == Name)
Reid Spencer9fef1632006-05-31 16:40:28 +0000299 return const_cast<GlobalVariable*>(&(*I));
Chris Lattner48a8e092006-03-08 18:39:13 +0000300 return 0;
301}
302
Chris Lattner09bd1a02003-12-31 08:43:01 +0000303
304
305//===----------------------------------------------------------------------===//
306// Methods for easy access to the types in the module.
307//
308
Chris Lattner1f985e02002-11-08 20:34:02 +0000309
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000310// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
311// there is already an entry for this name, true is returned and the symbol
312// table is not modified.
313//
314bool Module::addTypeName(const std::string &Name, const Type *Ty) {
Reid Spencer32af9e82007-01-06 07:24:44 +0000315 TypeSymbolTable &ST = getTypeSymbolTable();
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000316
Reid Spencer32af9e82007-01-06 07:24:44 +0000317 if (ST.lookup(Name)) return true; // Already in symtab...
Misha Brukmanb1c93172005-04-21 23:48:37 +0000318
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000319 // Not in symbol table? Set the name with the Symtab as an argument so the
320 // type knows what to update...
Reid Spencerf9776c32004-07-10 16:37:42 +0000321 ST.insert(Name, Ty);
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000322
323 return false;
324}
325
326/// getTypeByName - Return the type with the specified name in this module, or
327/// null if there is none by that name.
328const Type *Module::getTypeByName(const std::string &Name) const {
Reid Spencer32af9e82007-01-06 07:24:44 +0000329 const TypeSymbolTable &ST = getTypeSymbolTable();
330 return cast_or_null<Type>(ST.lookup(Name));
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000331}
Chris Lattner1f985e02002-11-08 20:34:02 +0000332
Chris Lattner10b7cb52002-04-13 18:58:33 +0000333// getTypeName - If there is at least one entry in the symbol table for the
334// specified type, return it.
335//
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000336std::string Module::getTypeName(const Type *Ty) const {
Reid Spencer32af9e82007-01-06 07:24:44 +0000337 const TypeSymbolTable &ST = getTypeSymbolTable();
Chris Lattner10b7cb52002-04-13 18:58:33 +0000338
Reid Spencer32af9e82007-01-06 07:24:44 +0000339 TypeSymbolTable::const_iterator TI = ST.begin();
340 TypeSymbolTable::const_iterator TE = ST.end();
Reid Spencerabb6f002004-05-25 08:52:20 +0000341 if ( TI == TE ) return ""; // No names for types
Chris Lattner10b7cb52002-04-13 18:58:33 +0000342
Reid Spencerabb6f002004-05-25 08:52:20 +0000343 while (TI != TE && TI->second != Ty)
Chris Lattner10b7cb52002-04-13 18:58:33 +0000344 ++TI;
345
346 if (TI != TE) // Must have found an entry!
347 return TI->first;
348 return ""; // Must not have found anything...
349}
350
Chris Lattner09bd1a02003-12-31 08:43:01 +0000351//===----------------------------------------------------------------------===//
352// Other module related stuff.
353//
354
355
Chris Lattnere0f6af9b2002-08-17 23:32:47 +0000356// dropAllReferences() - This function causes all the subelementss to "let go"
357// of all references that they are maintaining. This allows one to 'delete' a
358// whole module at a time, even though there may be circular references... first
359// all references are dropped, and all use counts go to zero. Then everything
Misha Brukmanfa100532003-10-10 17:54:14 +0000360// is deleted for real. Note that no operations are valid on an object that
Chris Lattnere0f6af9b2002-08-17 23:32:47 +0000361// has "dropped all references", except operator delete.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000362//
363void Module::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000364 for(Module::iterator I = begin(), E = end(); I != E; ++I)
365 I->dropAllReferences();
Chris Lattner446ad502001-10-13 06:58:40 +0000366
Chris Lattner531f9e92005-03-15 04:54:21 +0000367 for(Module::global_iterator I = global_begin(), E = global_end(); I != E; ++I)
Chris Lattner113f4f42002-06-25 16:13:24 +0000368 I->dropAllReferences();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000369}
Chris Lattner31cf9842001-06-30 04:35:40 +0000370