blob: 7dcd44ca2e5c526ef59c82b895d4b9041d173310 [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>
Reid Spencer8baf8e22004-07-04 11:55:37 +000025#include <iostream>
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 =
35 FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false);
Chris Lattner379a8d22003-04-16 20:28:45 +000036 Function *Ret = new Function(FTy, GlobalValue::ExternalLinkage);
Chris Lattner184b2982002-09-08 18:59:35 +000037 // This should not be garbage monitored.
38 LeakDetector::removeGarbageObject(Ret);
39 return Ret;
Chris Lattner113f4f42002-06-25 16:13:24 +000040}
Chris Lattnerf6c93e32005-01-30 00:09:23 +000041GlobalVariable *ilist_traits<GlobalVariable>::createSentinel() {
Chris Lattner379a8d22003-04-16 20:28:45 +000042 GlobalVariable *Ret = new GlobalVariable(Type::IntTy, false,
43 GlobalValue::ExternalLinkage);
Chris Lattner184b2982002-09-08 18:59:35 +000044 // This should not be garbage monitored.
45 LeakDetector::removeGarbageObject(Ret);
46 return Ret;
Chris Lattner113f4f42002-06-25 16:13:24 +000047}
48
49iplist<Function> &ilist_traits<Function>::getList(Module *M) {
50 return M->getFunctionList();
51}
52iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) {
53 return M->getGlobalList();
54}
55
56// Explicit instantiations of SymbolTableListTraits since some of the methods
Chris Lattnereef2fe72006-01-24 04:13:11 +000057// are not in the public header file.
Chris Lattner41baa982003-11-05 05:15:42 +000058template class SymbolTableListTraits<GlobalVariable, Module, Module>;
59template class SymbolTableListTraits<Function, Module, Module>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000060
Chris Lattner09bd1a02003-12-31 08:43:01 +000061//===----------------------------------------------------------------------===//
62// Primitive Module methods.
63//
Chris Lattner446ad502001-10-13 06:58:40 +000064
Chris Lattnerc3f6e002003-04-22 18:02:04 +000065Module::Module(const std::string &MID)
Owen Anderson9eb1a262006-05-18 02:10:31 +000066 : ModuleID(MID), DataLayout("") {
Chris Lattner113f4f42002-06-25 16:13:24 +000067 FunctionList.setItemParent(this);
68 FunctionList.setParent(this);
69 GlobalList.setItemParent(this);
70 GlobalList.setParent(this);
Chris Lattner98cf1f52002-11-20 18:36:02 +000071 SymTab = new SymbolTable();
Chris Lattner2f7c9632001-06-06 20:29:01 +000072}
73
74Module::~Module() {
75 dropAllReferences();
Chris Lattner113f4f42002-06-25 16:13:24 +000076 GlobalList.clear();
Chris Lattnerda975502001-09-10 07:58:01 +000077 GlobalList.setParent(0);
Chris Lattner113f4f42002-06-25 16:13:24 +000078 FunctionList.clear();
Chris Lattner57698e22002-03-26 18:01:55 +000079 FunctionList.setParent(0);
Reid Spencera0b05b32004-07-25 18:08:57 +000080 LibraryList.clear();
Chris Lattner2c8ff632002-04-28 04:51:51 +000081 delete SymTab;
Chris Lattner2f7c9632001-06-06 20:29:01 +000082}
83
Chris Lattnere0f6af9b2002-08-17 23:32:47 +000084// Module::dump() - Allow printing from debugger
85void Module::dump() const {
86 print(std::cerr);
87}
88
Owen Anderson9eb1a262006-05-18 02:10:31 +000089/// Target endian information...
90Module::Endianness Module::getEndianness() const {
91 std::string temp = DataLayout;
Owen Anderson08aecf52006-05-18 05:46:08 +000092 Module::Endianness ret = AnyEndianness;
Owen Anderson9eb1a262006-05-18 02:10:31 +000093
Owen Anderson08aecf52006-05-18 05:46:08 +000094 while (!temp.empty()) {
Owen Anderson9eb1a262006-05-18 02:10:31 +000095 std::string token = getToken(temp, "-");
96
97 if (token[0] == 'e') {
Owen Anderson08aecf52006-05-18 05:46:08 +000098 ret = LittleEndian;
Owen Anderson9eb1a262006-05-18 02:10:31 +000099 } else if (token[0] == 'E') {
Owen Anderson08aecf52006-05-18 05:46:08 +0000100 ret = BigEndian;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000101 }
102 }
103
Owen Anderson08aecf52006-05-18 05:46:08 +0000104 return ret;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000105}
106
107void Module::setEndianness(Endianness E) {
Owen Anderson08aecf52006-05-18 05:46:08 +0000108 if (!DataLayout.empty() && E != AnyEndianness)
109 DataLayout += "-";
Owen Anderson9eb1a262006-05-18 02:10:31 +0000110
111 if (E == LittleEndian)
Owen Anderson08aecf52006-05-18 05:46:08 +0000112 DataLayout += "e";
Owen Anderson9eb1a262006-05-18 02:10:31 +0000113 else if (E == BigEndian)
Owen Anderson08aecf52006-05-18 05:46:08 +0000114 DataLayout += "E";
Owen Anderson9eb1a262006-05-18 02:10:31 +0000115}
116
117/// Target Pointer Size information...
118Module::PointerSize Module::getPointerSize() const {
119 std::string temp = DataLayout;
Owen Anderson08aecf52006-05-18 05:46:08 +0000120 Module::PointerSize ret = AnyPointerSize;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000121
Owen Anderson08aecf52006-05-18 05:46:08 +0000122 while (!temp.empty()) {
Owen Anderson9eb1a262006-05-18 02:10:31 +0000123 std::string token = getToken(temp, "-");
124 char signal = getToken(token, ":")[0];
125
126 if (signal == 'p') {
127 int size = atoi(getToken(token, ":").c_str());
128 if (size == 32)
Owen Anderson08aecf52006-05-18 05:46:08 +0000129 ret = Pointer32;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000130 else if (size == 64)
Owen Anderson08aecf52006-05-18 05:46:08 +0000131 ret = Pointer64;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000132 }
133 }
134
Owen Anderson08aecf52006-05-18 05:46:08 +0000135 return ret;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000136}
137
138void Module::setPointerSize(PointerSize PS) {
Owen Anderson08aecf52006-05-18 05:46:08 +0000139 if (!DataLayout.empty() && PS != AnyPointerSize)
140 DataLayout += "-";
Owen Anderson9eb1a262006-05-18 02:10:31 +0000141
142 if (PS == Pointer32)
Owen Anderson08aecf52006-05-18 05:46:08 +0000143 DataLayout += "p:32:32";
Owen Anderson9eb1a262006-05-18 02:10:31 +0000144 else if (PS == Pointer64)
Owen Anderson08aecf52006-05-18 05:46:08 +0000145 DataLayout += "p:64:64";
Owen Anderson9eb1a262006-05-18 02:10:31 +0000146}
147
Chris Lattner09bd1a02003-12-31 08:43:01 +0000148//===----------------------------------------------------------------------===//
149// Methods for easy access to the functions in the module.
150//
151
Chris Lattnera483b062002-03-29 03:44:18 +0000152// getOrInsertFunction - Look up the specified function in the module symbol
153// table. If it does not exist, add a prototype for the function and return
154// it. This is nice because it allows most passes to get away with not handling
155// the symbol table directly for this common task.
156//
157Function *Module::getOrInsertFunction(const std::string &Name,
158 const FunctionType *Ty) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000159 SymbolTable &SymTab = getSymbolTable();
Chris Lattnera483b062002-03-29 03:44:18 +0000160
161 // See if we have a definitions for the specified function already...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000162 if (Value *V = SymTab.lookup(PointerType::get(Ty), Name)) {
Chris Lattnera483b062002-03-29 03:44:18 +0000163 return cast<Function>(V); // Yup, got it
164 } else { // Nope, add one
Chris Lattner379a8d22003-04-16 20:28:45 +0000165 Function *New = new Function(Ty, GlobalVariable::ExternalLinkage, Name);
Chris Lattnera483b062002-03-29 03:44:18 +0000166 FunctionList.push_back(New);
167 return New; // Return the new prototype...
168 }
169}
170
Chris Lattnerbd717d82003-08-31 00:19:28 +0000171// getOrInsertFunction - Look up the specified function in the module symbol
172// table. If it does not exist, add a prototype for the function and return it.
173// This version of the method takes a null terminated list of function
174// arguments, which makes it easier for clients to use.
175//
176Function *Module::getOrInsertFunction(const std::string &Name,
177 const Type *RetTy, ...) {
178 va_list Args;
179 va_start(Args, RetTy);
180
181 // Build the list of argument types...
182 std::vector<const Type*> ArgTys;
183 while (const Type *ArgTy = va_arg(Args, const Type*))
184 ArgTys.push_back(ArgTy);
185
186 va_end(Args);
187
188 // Build the function type and chain to the other getOrInsertFunction...
189 return getOrInsertFunction(Name, FunctionType::get(RetTy, ArgTys, false));
190}
191
192
Chris Lattnera483b062002-03-29 03:44:18 +0000193// getFunction - Look up the specified function in the module symbol table.
194// If it does not exist, return null.
195//
196Function *Module::getFunction(const std::string &Name, const FunctionType *Ty) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000197 SymbolTable &SymTab = getSymbolTable();
198 return cast_or_null<Function>(SymTab.lookup(PointerType::get(Ty), Name));
Chris Lattnera483b062002-03-29 03:44:18 +0000199}
200
201
Chris Lattner1f985e02002-11-08 20:34:02 +0000202/// getMainFunction - This function looks up main efficiently. This is such a
203/// common case, that it is a method in Module. If main cannot be found, a
204/// null pointer is returned.
205///
206Function *Module::getMainFunction() {
207 std::vector<const Type*> Params;
208
209 // int main(void)...
210 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
211 Params, false)))
212 return F;
213
214 // void main(void)...
215 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
216 Params, false)))
217 return F;
218
219 Params.push_back(Type::IntTy);
220
221 // int main(int argc)...
222 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
223 Params, false)))
224 return F;
225
226 // void main(int argc)...
227 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
228 Params, false)))
229 return F;
230
231 for (unsigned i = 0; i != 2; ++i) { // Check argv and envp
232 Params.push_back(PointerType::get(PointerType::get(Type::SByteTy)));
233
234 // int main(int argc, char **argv)...
235 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
236 Params, false)))
237 return F;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000238
Chris Lattner1f985e02002-11-08 20:34:02 +0000239 // void main(int argc, char **argv)...
240 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
241 Params, false)))
242 return F;
243 }
244
Chris Lattnerb2e46c02002-11-19 18:41:44 +0000245 // Ok, try to find main the hard way...
246 return getNamedFunction("main");
247}
248
249/// getNamedFunction - Return the first function in the module with the
250/// specified name, of arbitrary type. This method returns null if a function
251/// with the specified name is not found.
252///
Reid Spencer9fef1632006-05-31 16:40:28 +0000253Function *Module::getNamedFunction(const std::string &Name) const {
Chris Lattnerb2e46c02002-11-19 18:41:44 +0000254 // Loop over all of the functions, looking for the function desired
Reid Spencer9fef1632006-05-31 16:40:28 +0000255 const Function *Found = 0;
256 for (const_iterator I = begin(), E = end(); I != E; ++I)
Chris Lattnerb2e46c02002-11-19 18:41:44 +0000257 if (I->getName() == Name)
Chris Lattner4b4dacd2003-07-23 20:21:30 +0000258 if (I->isExternal())
259 Found = I;
260 else
Reid Spencer9fef1632006-05-31 16:40:28 +0000261 return const_cast<Function*>(&(*I));
262 return const_cast<Function*>(Found); // Non-external function not found...
Chris Lattner1f985e02002-11-08 20:34:02 +0000263}
264
Chris Lattner09bd1a02003-12-31 08:43:01 +0000265//===----------------------------------------------------------------------===//
266// Methods for easy access to the global variables in the module.
267//
268
269/// getGlobalVariable - Look up the specified global variable in the module
Chris Lattner7d4d93c2005-12-05 05:30:21 +0000270/// symbol table. If it does not exist, return null. The type argument
271/// should be the underlying type of the global, i.e., it should not have
272/// the top-level PointerType, which represents the address of the global.
273/// If AllowInternal is set to true, this function will return types that
274/// have InternalLinkage. By default, these types are not returned.
Chris Lattner09bd1a02003-12-31 08:43:01 +0000275///
Misha Brukmanb1c93172005-04-21 23:48:37 +0000276GlobalVariable *Module::getGlobalVariable(const std::string &Name,
Chris Lattner7d4d93c2005-12-05 05:30:21 +0000277 const Type *Ty, bool AllowInternal) {
Chris Lattner09bd1a02003-12-31 08:43:01 +0000278 if (Value *V = getSymbolTable().lookup(PointerType::get(Ty), Name)) {
279 GlobalVariable *Result = cast<GlobalVariable>(V);
Chris Lattner7d4d93c2005-12-05 05:30:21 +0000280 if (AllowInternal || !Result->hasInternalLinkage())
Chris Lattner09bd1a02003-12-31 08:43:01 +0000281 return Result;
282 }
283 return 0;
284}
285
Chris Lattner48a8e092006-03-08 18:39:13 +0000286/// getNamedGlobal - Return the first global variable in the module with the
287/// specified name, of arbitrary type. This method returns null if a global
288/// with the specified name is not found.
289///
Reid Spencer9fef1632006-05-31 16:40:28 +0000290GlobalVariable *Module::getNamedGlobal(const std::string &Name) const {
Chris Lattner48a8e092006-03-08 18:39:13 +0000291 // FIXME: This would be much faster with a symbol table that doesn't
292 // discriminate based on type!
Reid Spencer9fef1632006-05-31 16:40:28 +0000293 for (const_global_iterator I = global_begin(), E = global_end();
Chris Lattner48a8e092006-03-08 18:39:13 +0000294 I != E; ++I)
295 if (I->getName() == Name)
Reid Spencer9fef1632006-05-31 16:40:28 +0000296 return const_cast<GlobalVariable*>(&(*I));
Chris Lattner48a8e092006-03-08 18:39:13 +0000297 return 0;
298}
299
Chris Lattner09bd1a02003-12-31 08:43:01 +0000300
301
302//===----------------------------------------------------------------------===//
303// Methods for easy access to the types in the module.
304//
305
Chris Lattner1f985e02002-11-08 20:34:02 +0000306
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000307// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
308// there is already an entry for this name, true is returned and the symbol
309// table is not modified.
310//
311bool Module::addTypeName(const std::string &Name, const Type *Ty) {
312 SymbolTable &ST = getSymbolTable();
313
Reid Spencerabb6f002004-05-25 08:52:20 +0000314 if (ST.lookupType(Name)) return true; // Already in symtab...
Misha Brukmanb1c93172005-04-21 23:48:37 +0000315
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000316 // Not in symbol table? Set the name with the Symtab as an argument so the
317 // type knows what to update...
Reid Spencerf9776c32004-07-10 16:37:42 +0000318 ST.insert(Name, Ty);
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000319
320 return false;
321}
322
323/// getTypeByName - Return the type with the specified name in this module, or
324/// null if there is none by that name.
325const Type *Module::getTypeByName(const std::string &Name) const {
326 const SymbolTable &ST = getSymbolTable();
Reid Spencerabb6f002004-05-25 08:52:20 +0000327 return cast_or_null<Type>(ST.lookupType(Name));
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000328}
Chris Lattner1f985e02002-11-08 20:34:02 +0000329
Chris Lattner10b7cb52002-04-13 18:58:33 +0000330// getTypeName - If there is at least one entry in the symbol table for the
331// specified type, return it.
332//
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000333std::string Module::getTypeName(const Type *Ty) const {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000334 const SymbolTable &ST = getSymbolTable();
Chris Lattner10b7cb52002-04-13 18:58:33 +0000335
Reid Spencerabb6f002004-05-25 08:52:20 +0000336 SymbolTable::type_const_iterator TI = ST.type_begin();
337 SymbolTable::type_const_iterator TE = ST.type_end();
338 if ( TI == TE ) return ""; // No names for types
Chris Lattner10b7cb52002-04-13 18:58:33 +0000339
Reid Spencerabb6f002004-05-25 08:52:20 +0000340 while (TI != TE && TI->second != Ty)
Chris Lattner10b7cb52002-04-13 18:58:33 +0000341 ++TI;
342
343 if (TI != TE) // Must have found an entry!
344 return TI->first;
345 return ""; // Must not have found anything...
346}
347
Chris Lattner09bd1a02003-12-31 08:43:01 +0000348//===----------------------------------------------------------------------===//
349// Other module related stuff.
350//
351
352
Chris Lattnere0f6af9b2002-08-17 23:32:47 +0000353// dropAllReferences() - This function causes all the subelementss to "let go"
354// of all references that they are maintaining. This allows one to 'delete' a
355// whole module at a time, even though there may be circular references... first
356// all references are dropped, and all use counts go to zero. Then everything
Misha Brukmanfa100532003-10-10 17:54:14 +0000357// is deleted for real. Note that no operations are valid on an object that
Chris Lattnere0f6af9b2002-08-17 23:32:47 +0000358// has "dropped all references", except operator delete.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000359//
360void Module::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000361 for(Module::iterator I = begin(), E = end(); I != E; ++I)
362 I->dropAllReferences();
Chris Lattner446ad502001-10-13 06:58:40 +0000363
Chris Lattner531f9e92005-03-15 04:54:21 +0000364 for(Module::global_iterator I = global_begin(), E = global_end(); I != E; ++I)
Chris Lattner113f4f42002-06-25 16:13:24 +0000365 I->dropAllReferences();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000366}
Chris Lattner31cf9842001-06-30 04:35:40 +0000367