blob: 13493cac3bf5d8e31573ae49c2d0c1ec6825402b [file] [log] [blame]
Chris Lattner44d2c352003-10-13 03:32:08 +00001//===-- Module.cpp - Implement the Module class ---------------------------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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"
Chris Lattner5de22042001-11-27 00:03:19 +000018#include "Support/STLExtras.h"
Chris Lattner184b2982002-09-08 18:59:35 +000019#include "Support/LeakDetector.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000020#include "SymbolTableListTraitsImpl.h"
21#include <algorithm>
Chris Lattnerbd717d82003-08-31 00:19:28 +000022#include <cstdarg>
Reid Spencer8baf8e22004-07-04 11:55:37 +000023#include <iostream>
Chris Lattner446ad502001-10-13 06:58:40 +000024#include <map>
Chris Lattner189d19f2003-11-21 20:23:48 +000025using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000026
Chris Lattner09bd1a02003-12-31 08:43:01 +000027//===----------------------------------------------------------------------===//
Misha Brukman3bcead72004-04-21 18:27:56 +000028// Methods to implement the globals and functions lists.
Chris Lattner09bd1a02003-12-31 08:43:01 +000029//
30
Chris Lattner113f4f42002-06-25 16:13:24 +000031Function *ilist_traits<Function>::createNode() {
Chris Lattner184b2982002-09-08 18:59:35 +000032 FunctionType *FTy =
33 FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false);
Chris Lattner379a8d22003-04-16 20:28:45 +000034 Function *Ret = new Function(FTy, GlobalValue::ExternalLinkage);
Chris Lattner184b2982002-09-08 18:59:35 +000035 // This should not be garbage monitored.
36 LeakDetector::removeGarbageObject(Ret);
37 return Ret;
Chris Lattner113f4f42002-06-25 16:13:24 +000038}
39GlobalVariable *ilist_traits<GlobalVariable>::createNode() {
Chris Lattner379a8d22003-04-16 20:28:45 +000040 GlobalVariable *Ret = new GlobalVariable(Type::IntTy, false,
41 GlobalValue::ExternalLinkage);
Chris Lattner184b2982002-09-08 18:59:35 +000042 // This should not be garbage monitored.
43 LeakDetector::removeGarbageObject(Ret);
44 return Ret;
Chris Lattner113f4f42002-06-25 16:13:24 +000045}
46
47iplist<Function> &ilist_traits<Function>::getList(Module *M) {
48 return M->getFunctionList();
49}
50iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) {
51 return M->getGlobalList();
52}
53
54// Explicit instantiations of SymbolTableListTraits since some of the methods
55// are not in the public header file...
Chris Lattner41baa982003-11-05 05:15:42 +000056template class SymbolTableListTraits<GlobalVariable, Module, Module>;
57template class SymbolTableListTraits<Function, Module, Module>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000058
Chris Lattner09bd1a02003-12-31 08:43:01 +000059//===----------------------------------------------------------------------===//
60// Primitive Module methods.
61//
Chris Lattner446ad502001-10-13 06:58:40 +000062
Chris Lattnerc3f6e002003-04-22 18:02:04 +000063Module::Module(const std::string &MID)
Chris Lattner8068e0c2003-08-24 13:48:48 +000064 : ModuleID(MID), Endian(AnyEndianness), PtrSize(AnyPointerSize) {
Chris Lattner113f4f42002-06-25 16:13:24 +000065 FunctionList.setItemParent(this);
66 FunctionList.setParent(this);
67 GlobalList.setItemParent(this);
68 GlobalList.setParent(this);
Chris Lattner98cf1f52002-11-20 18:36:02 +000069 SymTab = new SymbolTable();
Chris Lattner2f7c9632001-06-06 20:29:01 +000070}
71
72Module::~Module() {
73 dropAllReferences();
Chris Lattner113f4f42002-06-25 16:13:24 +000074 GlobalList.clear();
Chris Lattnerda975502001-09-10 07:58:01 +000075 GlobalList.setParent(0);
Chris Lattner113f4f42002-06-25 16:13:24 +000076 FunctionList.clear();
Chris Lattner57698e22002-03-26 18:01:55 +000077 FunctionList.setParent(0);
Chris Lattner2c8ff632002-04-28 04:51:51 +000078 delete SymTab;
Chris Lattner2f7c9632001-06-06 20:29:01 +000079}
80
Chris Lattnere0f6af9b2002-08-17 23:32:47 +000081// Module::dump() - Allow printing from debugger
82void Module::dump() const {
83 print(std::cerr);
84}
85
Chris Lattner09bd1a02003-12-31 08:43:01 +000086//===----------------------------------------------------------------------===//
87// Methods for easy access to the functions in the module.
88//
89
Chris Lattnera483b062002-03-29 03:44:18 +000090// getOrInsertFunction - Look up the specified function in the module symbol
91// table. If it does not exist, add a prototype for the function and return
92// it. This is nice because it allows most passes to get away with not handling
93// the symbol table directly for this common task.
94//
95Function *Module::getOrInsertFunction(const std::string &Name,
96 const FunctionType *Ty) {
Chris Lattner98cf1f52002-11-20 18:36:02 +000097 SymbolTable &SymTab = getSymbolTable();
Chris Lattnera483b062002-03-29 03:44:18 +000098
99 // See if we have a definitions for the specified function already...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000100 if (Value *V = SymTab.lookup(PointerType::get(Ty), Name)) {
Chris Lattnera483b062002-03-29 03:44:18 +0000101 return cast<Function>(V); // Yup, got it
102 } else { // Nope, add one
Chris Lattner379a8d22003-04-16 20:28:45 +0000103 Function *New = new Function(Ty, GlobalVariable::ExternalLinkage, Name);
Chris Lattnera483b062002-03-29 03:44:18 +0000104 FunctionList.push_back(New);
105 return New; // Return the new prototype...
106 }
107}
108
Chris Lattnerbd717d82003-08-31 00:19:28 +0000109// getOrInsertFunction - Look up the specified function in the module symbol
110// table. If it does not exist, add a prototype for the function and return it.
111// This version of the method takes a null terminated list of function
112// arguments, which makes it easier for clients to use.
113//
114Function *Module::getOrInsertFunction(const std::string &Name,
115 const Type *RetTy, ...) {
116 va_list Args;
117 va_start(Args, RetTy);
118
119 // Build the list of argument types...
120 std::vector<const Type*> ArgTys;
121 while (const Type *ArgTy = va_arg(Args, const Type*))
122 ArgTys.push_back(ArgTy);
123
124 va_end(Args);
125
126 // Build the function type and chain to the other getOrInsertFunction...
127 return getOrInsertFunction(Name, FunctionType::get(RetTy, ArgTys, false));
128}
129
130
Chris Lattnera483b062002-03-29 03:44:18 +0000131// getFunction - Look up the specified function in the module symbol table.
132// If it does not exist, return null.
133//
134Function *Module::getFunction(const std::string &Name, const FunctionType *Ty) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000135 SymbolTable &SymTab = getSymbolTable();
136 return cast_or_null<Function>(SymTab.lookup(PointerType::get(Ty), Name));
Chris Lattnera483b062002-03-29 03:44:18 +0000137}
138
139
Chris Lattner1f985e02002-11-08 20:34:02 +0000140/// getMainFunction - This function looks up main efficiently. This is such a
141/// common case, that it is a method in Module. If main cannot be found, a
142/// null pointer is returned.
143///
144Function *Module::getMainFunction() {
145 std::vector<const Type*> Params;
146
147 // int main(void)...
148 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
149 Params, false)))
150 return F;
151
152 // void main(void)...
153 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
154 Params, false)))
155 return F;
156
157 Params.push_back(Type::IntTy);
158
159 // int main(int argc)...
160 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
161 Params, false)))
162 return F;
163
164 // void main(int argc)...
165 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
166 Params, false)))
167 return F;
168
169 for (unsigned i = 0; i != 2; ++i) { // Check argv and envp
170 Params.push_back(PointerType::get(PointerType::get(Type::SByteTy)));
171
172 // int main(int argc, char **argv)...
173 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
174 Params, false)))
175 return F;
176
177 // void main(int argc, char **argv)...
178 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
179 Params, false)))
180 return F;
181 }
182
Chris Lattnerb2e46c02002-11-19 18:41:44 +0000183 // Ok, try to find main the hard way...
184 return getNamedFunction("main");
185}
186
187/// getNamedFunction - Return the first function in the module with the
188/// specified name, of arbitrary type. This method returns null if a function
189/// with the specified name is not found.
190///
191Function *Module::getNamedFunction(const std::string &Name) {
192 // Loop over all of the functions, looking for the function desired
Chris Lattner4b4dacd2003-07-23 20:21:30 +0000193 Function *Found = 0;
Chris Lattner1f985e02002-11-08 20:34:02 +0000194 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattnerb2e46c02002-11-19 18:41:44 +0000195 if (I->getName() == Name)
Chris Lattner4b4dacd2003-07-23 20:21:30 +0000196 if (I->isExternal())
197 Found = I;
198 else
199 return I;
200 return Found; // Non-external function not found...
Chris Lattner1f985e02002-11-08 20:34:02 +0000201}
202
Chris Lattner09bd1a02003-12-31 08:43:01 +0000203//===----------------------------------------------------------------------===//
204// Methods for easy access to the global variables in the module.
205//
206
207/// getGlobalVariable - Look up the specified global variable in the module
208/// symbol table. If it does not exist, return null. Note that this only
209/// returns a global variable if it does not have internal linkage. The type
210/// argument should be the underlying type of the global, ie, it should not
211/// have the top-level PointerType, which represents the address of the
212/// global.
213///
214GlobalVariable *Module::getGlobalVariable(const std::string &Name,
215 const Type *Ty) {
216 if (Value *V = getSymbolTable().lookup(PointerType::get(Ty), Name)) {
217 GlobalVariable *Result = cast<GlobalVariable>(V);
218 if (!Result->hasInternalLinkage())
219 return Result;
220 }
221 return 0;
222}
223
224
225
226//===----------------------------------------------------------------------===//
227// Methods for easy access to the types in the module.
228//
229
Chris Lattner1f985e02002-11-08 20:34:02 +0000230
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000231// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
232// there is already an entry for this name, true is returned and the symbol
233// table is not modified.
234//
235bool Module::addTypeName(const std::string &Name, const Type *Ty) {
236 SymbolTable &ST = getSymbolTable();
237
Reid Spencerabb6f002004-05-25 08:52:20 +0000238 if (ST.lookupType(Name)) return true; // Already in symtab...
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000239
240 // Not in symbol table? Set the name with the Symtab as an argument so the
241 // type knows what to update...
Reid Spencerf9776c32004-07-10 16:37:42 +0000242 ST.insert(Name, Ty);
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000243
244 return false;
245}
246
247/// getTypeByName - Return the type with the specified name in this module, or
248/// null if there is none by that name.
249const Type *Module::getTypeByName(const std::string &Name) const {
250 const SymbolTable &ST = getSymbolTable();
Reid Spencerabb6f002004-05-25 08:52:20 +0000251 return cast_or_null<Type>(ST.lookupType(Name));
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000252}
Chris Lattner1f985e02002-11-08 20:34:02 +0000253
Chris Lattner10b7cb52002-04-13 18:58:33 +0000254// getTypeName - If there is at least one entry in the symbol table for the
255// specified type, return it.
256//
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000257std::string Module::getTypeName(const Type *Ty) const {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000258 const SymbolTable &ST = getSymbolTable();
Chris Lattner10b7cb52002-04-13 18:58:33 +0000259
Reid Spencerabb6f002004-05-25 08:52:20 +0000260 SymbolTable::type_const_iterator TI = ST.type_begin();
261 SymbolTable::type_const_iterator TE = ST.type_end();
262 if ( TI == TE ) return ""; // No names for types
Chris Lattner10b7cb52002-04-13 18:58:33 +0000263
Reid Spencerabb6f002004-05-25 08:52:20 +0000264 while (TI != TE && TI->second != Ty)
Chris Lattner10b7cb52002-04-13 18:58:33 +0000265 ++TI;
266
267 if (TI != TE) // Must have found an entry!
268 return TI->first;
269 return ""; // Must not have found anything...
270}
271
Chris Lattner2f7c9632001-06-06 20:29:01 +0000272
Chris Lattner09bd1a02003-12-31 08:43:01 +0000273//===----------------------------------------------------------------------===//
274// Other module related stuff.
275//
276
277
Chris Lattnere0f6af9b2002-08-17 23:32:47 +0000278// dropAllReferences() - This function causes all the subelementss to "let go"
279// of all references that they are maintaining. This allows one to 'delete' a
280// whole module at a time, even though there may be circular references... first
281// all references are dropped, and all use counts go to zero. Then everything
Misha Brukmanfa100532003-10-10 17:54:14 +0000282// is deleted for real. Note that no operations are valid on an object that
Chris Lattnere0f6af9b2002-08-17 23:32:47 +0000283// has "dropped all references", except operator delete.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000284//
285void Module::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000286 for(Module::iterator I = begin(), E = end(); I != E; ++I)
287 I->dropAllReferences();
Chris Lattner446ad502001-10-13 06:58:40 +0000288
Chris Lattner113f4f42002-06-25 16:13:24 +0000289 for(Module::giterator I = gbegin(), E = gend(); I != E; ++I)
290 I->dropAllReferences();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000291}
Chris Lattner31cf9842001-06-30 04:35:40 +0000292