1 //===-- Module.cpp - Implement the Module class ---------------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Module class for the VMCore library.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Module.h"
15 #include "llvm/InstrTypes.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/Support/LeakDetector.h"
21 #include "SymbolTableListTraitsImpl.h"
22 #include "llvm/TypeSymbolTable.h"
29 //===----------------------------------------------------------------------===//
30 // Methods to implement the globals and functions lists.
33 Function *ilist_traits<Function>::createSentinel() {
35 FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false,
36 std::vector<FunctionType::ParameterAttributes>() );
37 Function *Ret = new Function(FTy, GlobalValue::ExternalLinkage);
38 // This should not be garbage monitored.
39 LeakDetector::removeGarbageObject(Ret);
42 GlobalVariable *ilist_traits<GlobalVariable>::createSentinel() {
43 GlobalVariable *Ret = new GlobalVariable(Type::Int32Ty, false,
44 GlobalValue::ExternalLinkage);
45 // This should not be garbage monitored.
46 LeakDetector::removeGarbageObject(Ret);
50 iplist<Function> &ilist_traits<Function>::getList(Module *M) {
51 return M->getFunctionList();
53 iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) {
54 return M->getGlobalList();
57 // Explicit instantiations of SymbolTableListTraits since some of the methods
58 // are not in the public header file.
59 template class SymbolTableListTraits<GlobalVariable, Module, Module>;
60 template class SymbolTableListTraits<Function, Module, Module>;
62 //===----------------------------------------------------------------------===//
63 // Primitive Module methods.
66 Module::Module(const std::string &MID)
67 : ModuleID(MID), DataLayout("") {
68 FunctionList.setItemParent(this);
69 FunctionList.setParent(this);
70 GlobalList.setItemParent(this);
71 GlobalList.setParent(this);
72 ValSymTab = new SymbolTable();
73 TypeSymTab = new TypeSymbolTable();
79 GlobalList.setParent(0);
81 FunctionList.setParent(0);
87 // Module::dump() - Allow printing from debugger
88 void Module::dump() const {
89 print(*cerr.stream());
92 /// Target endian information...
93 Module::Endianness Module::getEndianness() const {
94 std::string temp = DataLayout;
95 Module::Endianness ret = AnyEndianness;
97 while (!temp.empty()) {
98 std::string token = getToken(temp, "-");
100 if (token[0] == 'e') {
102 } else if (token[0] == 'E') {
110 void Module::setEndianness(Endianness E) {
111 if (!DataLayout.empty() && E != AnyEndianness)
114 if (E == LittleEndian)
116 else if (E == BigEndian)
120 /// Target Pointer Size information...
121 Module::PointerSize Module::getPointerSize() const {
122 std::string temp = DataLayout;
123 Module::PointerSize ret = AnyPointerSize;
125 while (!temp.empty()) {
126 std::string token = getToken(temp, "-");
127 char signal = getToken(token, ":")[0];
130 int size = atoi(getToken(token, ":").c_str());
141 void Module::setPointerSize(PointerSize PS) {
142 if (!DataLayout.empty() && PS != AnyPointerSize)
146 DataLayout += "p:32:32";
147 else if (PS == Pointer64)
148 DataLayout += "p:64:64";
151 //===----------------------------------------------------------------------===//
152 // Methods for easy access to the functions in the module.
155 // 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.
160 Function *Module::getOrInsertFunction(const std::string &Name,
161 const FunctionType *Ty) {
162 SymbolTable &SymTab = getValueSymbolTable();
164 // See if we have a definitions for the specified function already...
165 if (Value *V = SymTab.lookup(PointerType::get(Ty), Name)) {
166 return cast<Function>(V); // Yup, got it
167 } else { // Nope, add one
168 Function *New = new Function(Ty, GlobalVariable::ExternalLinkage, Name);
169 FunctionList.push_back(New);
170 return New; // Return the new prototype...
174 // 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.
179 Function *Module::getOrInsertFunction(const std::string &Name,
180 const Type *RetTy, ...) {
182 va_start(Args, RetTy);
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);
191 // Build the function type and chain to the other getOrInsertFunction...
192 return getOrInsertFunction(Name, FunctionType::get(RetTy, ArgTys, false));
196 // getFunction - Look up the specified function in the module symbol table.
197 // If it does not exist, return null.
199 Function *Module::getFunction(const std::string &Name, const FunctionType *Ty) {
200 SymbolTable &SymTab = getValueSymbolTable();
201 return cast_or_null<Function>(SymTab.lookup(PointerType::get(Ty), Name));
205 /// 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.
209 Function *Module::getMainFunction() {
210 std::vector<const Type*> Params;
213 if (Function *F = getFunction("main", FunctionType::get(Type::Int32Ty,
217 // void main(void)...
218 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
222 Params.push_back(Type::Int32Ty);
224 // int main(int argc)...
225 if (Function *F = getFunction("main", FunctionType::get(Type::Int32Ty,
229 // void main(int argc)...
230 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
234 for (unsigned i = 0; i != 2; ++i) { // Check argv and envp
235 Params.push_back(PointerType::get(PointerType::get(Type::Int8Ty)));
237 // int main(int argc, char **argv)...
238 if (Function *F = getFunction("main", FunctionType::get(Type::Int32Ty,
242 // void main(int argc, char **argv)...
243 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
248 // Ok, try to find main the hard way...
249 return getNamedFunction("main");
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.
256 Function *Module::getNamedFunction(const std::string &Name) const {
257 // Loop over all of the functions, looking for the function desired
258 const Function *Found = 0;
259 for (const_iterator I = begin(), E = end(); I != E; ++I)
260 if (I->getName() == Name)
264 return const_cast<Function*>(&(*I));
265 return const_cast<Function*>(Found); // Non-external function not found...
268 //===----------------------------------------------------------------------===//
269 // Methods for easy access to the global variables in the module.
272 /// getGlobalVariable - Look up the specified global variable in the module
273 /// 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.
279 GlobalVariable *Module::getGlobalVariable(const std::string &Name,
280 const Type *Ty, bool AllowInternal) {
281 if (Value *V = getValueSymbolTable().lookup(PointerType::get(Ty), Name)) {
282 GlobalVariable *Result = cast<GlobalVariable>(V);
283 if (AllowInternal || !Result->hasInternalLinkage())
289 /// 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.
293 GlobalVariable *Module::getNamedGlobal(const std::string &Name) const {
294 // FIXME: This would be much faster with a symbol table that doesn't
295 // discriminate based on type!
296 for (const_global_iterator I = global_begin(), E = global_end();
298 if (I->getName() == Name)
299 return const_cast<GlobalVariable*>(&(*I));
305 //===----------------------------------------------------------------------===//
306 // Methods for easy access to the types in the module.
310 // 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.
314 bool Module::addTypeName(const std::string &Name, const Type *Ty) {
315 TypeSymbolTable &ST = getTypeSymbolTable();
317 if (ST.lookup(Name)) return true; // Already in symtab...
319 // Not in symbol table? Set the name with the Symtab as an argument so the
320 // type knows what to update...
326 /// getTypeByName - Return the type with the specified name in this module, or
327 /// null if there is none by that name.
328 const Type *Module::getTypeByName(const std::string &Name) const {
329 const TypeSymbolTable &ST = getTypeSymbolTable();
330 return cast_or_null<Type>(ST.lookup(Name));
333 // getTypeName - If there is at least one entry in the symbol table for the
334 // specified type, return it.
336 std::string Module::getTypeName(const Type *Ty) const {
337 const TypeSymbolTable &ST = getTypeSymbolTable();
339 TypeSymbolTable::const_iterator TI = ST.begin();
340 TypeSymbolTable::const_iterator TE = ST.end();
341 if ( TI == TE ) return ""; // No names for types
343 while (TI != TE && TI->second != Ty)
346 if (TI != TE) // Must have found an entry!
348 return ""; // Must not have found anything...
351 //===----------------------------------------------------------------------===//
352 // Other module related stuff.
356 // 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
360 // is deleted for real. Note that no operations are valid on an object that
361 // has "dropped all references", except operator delete.
363 void Module::dropAllReferences() {
364 for(Module::iterator I = begin(), E = end(); I != E; ++I)
365 I->dropAllReferences();
367 for(Module::global_iterator I = global_begin(), E = global_end(); I != E; ++I)
368 I->dropAllReferences();