2003-10-13 03:32:08 +00:00
|
|
|
//===-- Module.cpp - Implement the Module class ---------------------------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
// This file implements the Module class for the VMCore library.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Module.h"
|
2001-06-30 04:35:40 +00:00
|
|
|
#include "llvm/InstrTypes.h"
|
2002-04-28 19:55:58 +00:00
|
|
|
#include "llvm/Constants.h"
|
2002-03-29 03:44:18 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2006-05-18 02:10:31 +00:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/LeakDetector.h"
|
2002-06-25 16:13:24 +00:00
|
|
|
#include "SymbolTableListTraitsImpl.h"
|
|
|
|
#include <algorithm>
|
2003-08-31 00:19:28 +00:00
|
|
|
#include <cstdarg>
|
2006-05-18 02:10:31 +00:00
|
|
|
#include <cstdlib>
|
2004-07-04 11:55:37 +00:00
|
|
|
#include <iostream>
|
2001-10-13 06:58:40 +00:00
|
|
|
#include <map>
|
2003-11-21 20:23:48 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2003-12-31 08:43:01 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2004-04-21 18:27:56 +00:00
|
|
|
// Methods to implement the globals and functions lists.
|
2003-12-31 08:43:01 +00:00
|
|
|
//
|
|
|
|
|
2005-01-30 00:09:23 +00:00
|
|
|
Function *ilist_traits<Function>::createSentinel() {
|
2002-09-08 18:59:35 +00:00
|
|
|
FunctionType *FTy =
|
|
|
|
FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false);
|
2003-04-16 20:28:45 +00:00
|
|
|
Function *Ret = new Function(FTy, GlobalValue::ExternalLinkage);
|
2002-09-08 18:59:35 +00:00
|
|
|
// This should not be garbage monitored.
|
|
|
|
LeakDetector::removeGarbageObject(Ret);
|
|
|
|
return Ret;
|
2002-06-25 16:13:24 +00:00
|
|
|
}
|
2005-01-30 00:09:23 +00:00
|
|
|
GlobalVariable *ilist_traits<GlobalVariable>::createSentinel() {
|
2003-04-16 20:28:45 +00:00
|
|
|
GlobalVariable *Ret = new GlobalVariable(Type::IntTy, false,
|
|
|
|
GlobalValue::ExternalLinkage);
|
2002-09-08 18:59:35 +00:00
|
|
|
// This should not be garbage monitored.
|
|
|
|
LeakDetector::removeGarbageObject(Ret);
|
|
|
|
return Ret;
|
2002-06-25 16:13:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
iplist<Function> &ilist_traits<Function>::getList(Module *M) {
|
|
|
|
return M->getFunctionList();
|
|
|
|
}
|
|
|
|
iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) {
|
|
|
|
return M->getGlobalList();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Explicit instantiations of SymbolTableListTraits since some of the methods
|
2006-01-24 04:13:11 +00:00
|
|
|
// are not in the public header file.
|
2003-11-05 05:15:42 +00:00
|
|
|
template class SymbolTableListTraits<GlobalVariable, Module, Module>;
|
|
|
|
template class SymbolTableListTraits<Function, Module, Module>;
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-12-31 08:43:01 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Primitive Module methods.
|
|
|
|
//
|
2001-10-13 06:58:40 +00:00
|
|
|
|
2003-04-22 18:02:04 +00:00
|
|
|
Module::Module(const std::string &MID)
|
2006-05-18 02:10:31 +00:00
|
|
|
: ModuleID(MID), DataLayout("") {
|
2002-06-25 16:13:24 +00:00
|
|
|
FunctionList.setItemParent(this);
|
|
|
|
FunctionList.setParent(this);
|
|
|
|
GlobalList.setItemParent(this);
|
|
|
|
GlobalList.setParent(this);
|
2002-11-20 18:36:02 +00:00
|
|
|
SymTab = new SymbolTable();
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Module::~Module() {
|
|
|
|
dropAllReferences();
|
2002-06-25 16:13:24 +00:00
|
|
|
GlobalList.clear();
|
2001-09-10 07:58:01 +00:00
|
|
|
GlobalList.setParent(0);
|
2002-06-25 16:13:24 +00:00
|
|
|
FunctionList.clear();
|
2002-03-26 18:01:55 +00:00
|
|
|
FunctionList.setParent(0);
|
2004-07-25 18:08:57 +00:00
|
|
|
LibraryList.clear();
|
2002-04-28 04:51:51 +00:00
|
|
|
delete SymTab;
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2002-08-17 23:32:47 +00:00
|
|
|
// Module::dump() - Allow printing from debugger
|
|
|
|
void Module::dump() const {
|
|
|
|
print(std::cerr);
|
|
|
|
}
|
|
|
|
|
2006-05-18 02:10:31 +00:00
|
|
|
/// Target endian information...
|
|
|
|
Module::Endianness Module::getEndianness() const {
|
|
|
|
std::string temp = DataLayout;
|
2006-05-18 05:46:08 +00:00
|
|
|
Module::Endianness ret = AnyEndianness;
|
2006-05-18 02:10:31 +00:00
|
|
|
|
2006-05-18 05:46:08 +00:00
|
|
|
while (!temp.empty()) {
|
2006-05-18 02:10:31 +00:00
|
|
|
std::string token = getToken(temp, "-");
|
|
|
|
|
|
|
|
if (token[0] == 'e') {
|
2006-05-18 05:46:08 +00:00
|
|
|
ret = LittleEndian;
|
2006-05-18 02:10:31 +00:00
|
|
|
} else if (token[0] == 'E') {
|
2006-05-18 05:46:08 +00:00
|
|
|
ret = BigEndian;
|
2006-05-18 02:10:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-18 05:46:08 +00:00
|
|
|
return ret;
|
2006-05-18 02:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Module::setEndianness(Endianness E) {
|
2006-05-18 05:46:08 +00:00
|
|
|
if (!DataLayout.empty() && E != AnyEndianness)
|
|
|
|
DataLayout += "-";
|
2006-05-18 02:10:31 +00:00
|
|
|
|
|
|
|
if (E == LittleEndian)
|
2006-05-18 05:46:08 +00:00
|
|
|
DataLayout += "e";
|
2006-05-18 02:10:31 +00:00
|
|
|
else if (E == BigEndian)
|
2006-05-18 05:46:08 +00:00
|
|
|
DataLayout += "E";
|
2006-05-18 02:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Target Pointer Size information...
|
|
|
|
Module::PointerSize Module::getPointerSize() const {
|
|
|
|
std::string temp = DataLayout;
|
2006-05-18 05:46:08 +00:00
|
|
|
Module::PointerSize ret = AnyPointerSize;
|
2006-05-18 02:10:31 +00:00
|
|
|
|
2006-05-18 05:46:08 +00:00
|
|
|
while (!temp.empty()) {
|
2006-05-18 02:10:31 +00:00
|
|
|
std::string token = getToken(temp, "-");
|
|
|
|
char signal = getToken(token, ":")[0];
|
|
|
|
|
|
|
|
if (signal == 'p') {
|
|
|
|
int size = atoi(getToken(token, ":").c_str());
|
|
|
|
if (size == 32)
|
2006-05-18 05:46:08 +00:00
|
|
|
ret = Pointer32;
|
2006-05-18 02:10:31 +00:00
|
|
|
else if (size == 64)
|
2006-05-18 05:46:08 +00:00
|
|
|
ret = Pointer64;
|
2006-05-18 02:10:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-18 05:46:08 +00:00
|
|
|
return ret;
|
2006-05-18 02:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Module::setPointerSize(PointerSize PS) {
|
2006-05-18 05:46:08 +00:00
|
|
|
if (!DataLayout.empty() && PS != AnyPointerSize)
|
|
|
|
DataLayout += "-";
|
2006-05-18 02:10:31 +00:00
|
|
|
|
|
|
|
if (PS == Pointer32)
|
2006-05-18 05:46:08 +00:00
|
|
|
DataLayout += "p:32:32";
|
2006-05-18 02:10:31 +00:00
|
|
|
else if (PS == Pointer64)
|
2006-05-18 05:46:08 +00:00
|
|
|
DataLayout += "p:64:64";
|
2006-05-18 02:10:31 +00:00
|
|
|
}
|
|
|
|
|
2003-12-31 08:43:01 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Methods for easy access to the functions in the module.
|
|
|
|
//
|
|
|
|
|
2002-03-29 03:44:18 +00:00
|
|
|
// getOrInsertFunction - Look up the specified function in the module symbol
|
|
|
|
// table. If it does not exist, add a prototype for the function and return
|
|
|
|
// it. This is nice because it allows most passes to get away with not handling
|
|
|
|
// the symbol table directly for this common task.
|
|
|
|
//
|
|
|
|
Function *Module::getOrInsertFunction(const std::string &Name,
|
|
|
|
const FunctionType *Ty) {
|
2002-11-20 18:36:02 +00:00
|
|
|
SymbolTable &SymTab = getSymbolTable();
|
2002-03-29 03:44:18 +00:00
|
|
|
|
|
|
|
// See if we have a definitions for the specified function already...
|
2002-11-20 18:36:02 +00:00
|
|
|
if (Value *V = SymTab.lookup(PointerType::get(Ty), Name)) {
|
2002-03-29 03:44:18 +00:00
|
|
|
return cast<Function>(V); // Yup, got it
|
|
|
|
} else { // Nope, add one
|
2003-04-16 20:28:45 +00:00
|
|
|
Function *New = new Function(Ty, GlobalVariable::ExternalLinkage, Name);
|
2002-03-29 03:44:18 +00:00
|
|
|
FunctionList.push_back(New);
|
|
|
|
return New; // Return the new prototype...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-31 00:19:28 +00:00
|
|
|
// getOrInsertFunction - Look up the specified function in the module symbol
|
|
|
|
// table. If it does not exist, add a prototype for the function and return it.
|
|
|
|
// This version of the method takes a null terminated list of function
|
|
|
|
// arguments, which makes it easier for clients to use.
|
|
|
|
//
|
|
|
|
Function *Module::getOrInsertFunction(const std::string &Name,
|
|
|
|
const Type *RetTy, ...) {
|
|
|
|
va_list Args;
|
|
|
|
va_start(Args, RetTy);
|
|
|
|
|
|
|
|
// Build the list of argument types...
|
|
|
|
std::vector<const Type*> ArgTys;
|
|
|
|
while (const Type *ArgTy = va_arg(Args, const Type*))
|
|
|
|
ArgTys.push_back(ArgTy);
|
|
|
|
|
|
|
|
va_end(Args);
|
|
|
|
|
|
|
|
// Build the function type and chain to the other getOrInsertFunction...
|
|
|
|
return getOrInsertFunction(Name, FunctionType::get(RetTy, ArgTys, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-29 03:44:18 +00:00
|
|
|
// getFunction - Look up the specified function in the module symbol table.
|
|
|
|
// If it does not exist, return null.
|
|
|
|
//
|
|
|
|
Function *Module::getFunction(const std::string &Name, const FunctionType *Ty) {
|
2002-11-20 18:36:02 +00:00
|
|
|
SymbolTable &SymTab = getSymbolTable();
|
|
|
|
return cast_or_null<Function>(SymTab.lookup(PointerType::get(Ty), Name));
|
2002-03-29 03:44:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-11-08 20:34:02 +00:00
|
|
|
/// getMainFunction - This function looks up main efficiently. This is such a
|
|
|
|
/// common case, that it is a method in Module. If main cannot be found, a
|
|
|
|
/// null pointer is returned.
|
|
|
|
///
|
|
|
|
Function *Module::getMainFunction() {
|
|
|
|
std::vector<const Type*> Params;
|
|
|
|
|
|
|
|
// int main(void)...
|
|
|
|
if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
|
|
|
|
Params, false)))
|
|
|
|
return F;
|
|
|
|
|
|
|
|
// void main(void)...
|
|
|
|
if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
|
|
|
|
Params, false)))
|
|
|
|
return F;
|
|
|
|
|
|
|
|
Params.push_back(Type::IntTy);
|
|
|
|
|
|
|
|
// int main(int argc)...
|
|
|
|
if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
|
|
|
|
Params, false)))
|
|
|
|
return F;
|
|
|
|
|
|
|
|
// void main(int argc)...
|
|
|
|
if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
|
|
|
|
Params, false)))
|
|
|
|
return F;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i != 2; ++i) { // Check argv and envp
|
|
|
|
Params.push_back(PointerType::get(PointerType::get(Type::SByteTy)));
|
|
|
|
|
|
|
|
// int main(int argc, char **argv)...
|
|
|
|
if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
|
|
|
|
Params, false)))
|
|
|
|
return F;
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2002-11-08 20:34:02 +00:00
|
|
|
// void main(int argc, char **argv)...
|
|
|
|
if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
|
|
|
|
Params, false)))
|
|
|
|
return F;
|
|
|
|
}
|
|
|
|
|
2002-11-19 18:41:44 +00:00
|
|
|
// Ok, try to find main the hard way...
|
|
|
|
return getNamedFunction("main");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getNamedFunction - Return the first function in the module with the
|
|
|
|
/// specified name, of arbitrary type. This method returns null if a function
|
|
|
|
/// with the specified name is not found.
|
|
|
|
///
|
2006-05-31 16:40:28 +00:00
|
|
|
Function *Module::getNamedFunction(const std::string &Name) const {
|
2002-11-19 18:41:44 +00:00
|
|
|
// Loop over all of the functions, looking for the function desired
|
2006-05-31 16:40:28 +00:00
|
|
|
const Function *Found = 0;
|
|
|
|
for (const_iterator I = begin(), E = end(); I != E; ++I)
|
2002-11-19 18:41:44 +00:00
|
|
|
if (I->getName() == Name)
|
2003-07-23 20:21:30 +00:00
|
|
|
if (I->isExternal())
|
|
|
|
Found = I;
|
|
|
|
else
|
2006-05-31 16:40:28 +00:00
|
|
|
return const_cast<Function*>(&(*I));
|
|
|
|
return const_cast<Function*>(Found); // Non-external function not found...
|
2002-11-08 20:34:02 +00:00
|
|
|
}
|
|
|
|
|
2003-12-31 08:43:01 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Methods for easy access to the global variables in the module.
|
|
|
|
//
|
|
|
|
|
|
|
|
/// getGlobalVariable - Look up the specified global variable in the module
|
2005-12-05 05:30:21 +00:00
|
|
|
/// symbol table. If it does not exist, return null. The type argument
|
|
|
|
/// should be the underlying type of the global, i.e., it should not have
|
|
|
|
/// the top-level PointerType, which represents the address of the global.
|
|
|
|
/// If AllowInternal is set to true, this function will return types that
|
|
|
|
/// have InternalLinkage. By default, these types are not returned.
|
2003-12-31 08:43:01 +00:00
|
|
|
///
|
2005-04-21 23:48:37 +00:00
|
|
|
GlobalVariable *Module::getGlobalVariable(const std::string &Name,
|
2005-12-05 05:30:21 +00:00
|
|
|
const Type *Ty, bool AllowInternal) {
|
2003-12-31 08:43:01 +00:00
|
|
|
if (Value *V = getSymbolTable().lookup(PointerType::get(Ty), Name)) {
|
|
|
|
GlobalVariable *Result = cast<GlobalVariable>(V);
|
2005-12-05 05:30:21 +00:00
|
|
|
if (AllowInternal || !Result->hasInternalLinkage())
|
2003-12-31 08:43:01 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-03-08 18:39:13 +00:00
|
|
|
/// getNamedGlobal - Return the first global variable in the module with the
|
|
|
|
/// specified name, of arbitrary type. This method returns null if a global
|
|
|
|
/// with the specified name is not found.
|
|
|
|
///
|
2006-05-31 16:40:28 +00:00
|
|
|
GlobalVariable *Module::getNamedGlobal(const std::string &Name) const {
|
2006-03-08 18:39:13 +00:00
|
|
|
// FIXME: This would be much faster with a symbol table that doesn't
|
|
|
|
// discriminate based on type!
|
2006-05-31 16:40:28 +00:00
|
|
|
for (const_global_iterator I = global_begin(), E = global_end();
|
2006-03-08 18:39:13 +00:00
|
|
|
I != E; ++I)
|
|
|
|
if (I->getName() == Name)
|
2006-05-31 16:40:28 +00:00
|
|
|
return const_cast<GlobalVariable*>(&(*I));
|
2006-03-08 18:39:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-12-31 08:43:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Methods for easy access to the types in the module.
|
|
|
|
//
|
|
|
|
|
2002-11-08 20:34:02 +00:00
|
|
|
|
2003-12-31 07:09:33 +00:00
|
|
|
// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
|
|
|
|
// there is already an entry for this name, true is returned and the symbol
|
|
|
|
// table is not modified.
|
|
|
|
//
|
|
|
|
bool Module::addTypeName(const std::string &Name, const Type *Ty) {
|
|
|
|
SymbolTable &ST = getSymbolTable();
|
|
|
|
|
2004-05-25 08:52:20 +00:00
|
|
|
if (ST.lookupType(Name)) return true; // Already in symtab...
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2003-12-31 07:09:33 +00:00
|
|
|
// Not in symbol table? Set the name with the Symtab as an argument so the
|
|
|
|
// type knows what to update...
|
2004-07-10 16:37:42 +00:00
|
|
|
ST.insert(Name, Ty);
|
2003-12-31 07:09:33 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getTypeByName - Return the type with the specified name in this module, or
|
|
|
|
/// null if there is none by that name.
|
|
|
|
const Type *Module::getTypeByName(const std::string &Name) const {
|
|
|
|
const SymbolTable &ST = getSymbolTable();
|
2004-05-25 08:52:20 +00:00
|
|
|
return cast_or_null<Type>(ST.lookupType(Name));
|
2003-12-31 07:09:33 +00:00
|
|
|
}
|
2002-11-08 20:34:02 +00:00
|
|
|
|
2002-04-13 18:58:33 +00:00
|
|
|
// getTypeName - If there is at least one entry in the symbol table for the
|
|
|
|
// specified type, return it.
|
|
|
|
//
|
2003-12-31 07:09:33 +00:00
|
|
|
std::string Module::getTypeName(const Type *Ty) const {
|
2002-11-20 18:36:02 +00:00
|
|
|
const SymbolTable &ST = getSymbolTable();
|
2002-04-13 18:58:33 +00:00
|
|
|
|
2004-05-25 08:52:20 +00:00
|
|
|
SymbolTable::type_const_iterator TI = ST.type_begin();
|
|
|
|
SymbolTable::type_const_iterator TE = ST.type_end();
|
|
|
|
if ( TI == TE ) return ""; // No names for types
|
2002-04-13 18:58:33 +00:00
|
|
|
|
2004-05-25 08:52:20 +00:00
|
|
|
while (TI != TE && TI->second != Ty)
|
2002-04-13 18:58:33 +00:00
|
|
|
++TI;
|
|
|
|
|
|
|
|
if (TI != TE) // Must have found an entry!
|
|
|
|
return TI->first;
|
|
|
|
return ""; // Must not have found anything...
|
|
|
|
}
|
|
|
|
|
2003-12-31 08:43:01 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Other module related stuff.
|
|
|
|
//
|
|
|
|
|
|
|
|
|
2002-08-17 23:32:47 +00:00
|
|
|
// dropAllReferences() - This function causes all the subelementss to "let go"
|
|
|
|
// of all references that they are maintaining. This allows one to 'delete' a
|
|
|
|
// whole module at a time, even though there may be circular references... first
|
|
|
|
// all references are dropped, and all use counts go to zero. Then everything
|
2003-10-10 17:54:14 +00:00
|
|
|
// is deleted for real. Note that no operations are valid on an object that
|
2002-08-17 23:32:47 +00:00
|
|
|
// has "dropped all references", except operator delete.
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
void Module::dropAllReferences() {
|
2002-06-25 16:13:24 +00:00
|
|
|
for(Module::iterator I = begin(), E = end(); I != E; ++I)
|
|
|
|
I->dropAllReferences();
|
2001-10-13 06:58:40 +00:00
|
|
|
|
2005-03-15 04:54:21 +00:00
|
|
|
for(Module::global_iterator I = global_begin(), E = global_end(); I != E; ++I)
|
2002-06-25 16:13:24 +00:00
|
|
|
I->dropAllReferences();
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
2001-06-30 04:35:40 +00:00
|
|
|
|