2003-10-13 05:32:08 +02:00
|
|
|
//===-- Module.cpp - Implement the Module class ---------------------------===//
|
2003-10-20 21:43:21 +02: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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 22:29:01 +02:00
|
|
|
//
|
|
|
|
// This file implements the Module class for the VMCore library.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Module.h"
|
2001-06-30 06:35:40 +02:00
|
|
|
#include "llvm/InstrTypes.h"
|
2002-04-28 21:55:58 +02:00
|
|
|
#include "llvm/Constants.h"
|
2002-03-29 04:44:18 +01:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2001-11-27 01:03:19 +01:00
|
|
|
#include "Support/STLExtras.h"
|
2002-09-08 20:59:35 +02:00
|
|
|
#include "Support/LeakDetector.h"
|
2002-06-25 18:13:24 +02:00
|
|
|
#include "SymbolTableListTraitsImpl.h"
|
|
|
|
#include <algorithm>
|
2003-08-31 02:19:28 +02:00
|
|
|
#include <cstdarg>
|
2001-10-13 08:58:40 +02:00
|
|
|
#include <map>
|
2003-11-21 21:23:48 +01:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2002-06-25 18:13:24 +02:00
|
|
|
Function *ilist_traits<Function>::createNode() {
|
2002-09-08 20:59:35 +02:00
|
|
|
FunctionType *FTy =
|
|
|
|
FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false);
|
2003-04-16 22:28:45 +02:00
|
|
|
Function *Ret = new Function(FTy, GlobalValue::ExternalLinkage);
|
2002-09-08 20:59:35 +02:00
|
|
|
// This should not be garbage monitored.
|
|
|
|
LeakDetector::removeGarbageObject(Ret);
|
|
|
|
return Ret;
|
2002-06-25 18:13:24 +02:00
|
|
|
}
|
|
|
|
GlobalVariable *ilist_traits<GlobalVariable>::createNode() {
|
2003-04-16 22:28:45 +02:00
|
|
|
GlobalVariable *Ret = new GlobalVariable(Type::IntTy, false,
|
|
|
|
GlobalValue::ExternalLinkage);
|
2002-09-08 20:59:35 +02:00
|
|
|
// This should not be garbage monitored.
|
|
|
|
LeakDetector::removeGarbageObject(Ret);
|
|
|
|
return Ret;
|
2002-06-25 18:13:24 +02: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
|
|
|
|
// are not in the public header file...
|
2003-11-05 06:15:42 +01:00
|
|
|
template class SymbolTableListTraits<GlobalVariable, Module, Module>;
|
|
|
|
template class SymbolTableListTraits<Function, Module, Module>;
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2001-10-13 08:58:40 +02:00
|
|
|
// Define the GlobalValueRefMap as a struct that wraps a map so that we don't
|
|
|
|
// have Module.h depend on <map>
|
|
|
|
//
|
2003-11-21 21:23:48 +01:00
|
|
|
namespace llvm {
|
|
|
|
struct GlobalValueRefMap {
|
|
|
|
typedef std::map<GlobalValue*, ConstantPointerRef*> MapTy;
|
|
|
|
typedef MapTy::iterator iterator;
|
|
|
|
std::map<GlobalValue*, ConstantPointerRef*> Map;
|
|
|
|
};
|
|
|
|
}
|
2001-10-13 08:58:40 +02:00
|
|
|
|
|
|
|
|
2003-04-22 20:02:04 +02:00
|
|
|
Module::Module(const std::string &MID)
|
2003-08-24 15:48:48 +02:00
|
|
|
: ModuleID(MID), Endian(AnyEndianness), PtrSize(AnyPointerSize) {
|
2002-06-25 18:13:24 +02:00
|
|
|
FunctionList.setItemParent(this);
|
|
|
|
FunctionList.setParent(this);
|
|
|
|
GlobalList.setItemParent(this);
|
|
|
|
GlobalList.setParent(this);
|
2002-04-28 06:51:51 +02:00
|
|
|
GVRefMap = 0;
|
2002-11-20 19:36:02 +01:00
|
|
|
SymTab = new SymbolTable();
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Module::~Module() {
|
|
|
|
dropAllReferences();
|
2002-06-25 18:13:24 +02:00
|
|
|
GlobalList.clear();
|
2001-09-10 09:58:01 +02:00
|
|
|
GlobalList.setParent(0);
|
2002-06-25 18:13:24 +02:00
|
|
|
FunctionList.clear();
|
2002-03-26 19:01:55 +01:00
|
|
|
FunctionList.setParent(0);
|
2002-04-28 06:51:51 +02:00
|
|
|
delete SymTab;
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
2002-08-18 01:32:47 +02:00
|
|
|
// Module::dump() - Allow printing from debugger
|
|
|
|
void Module::dump() const {
|
|
|
|
print(std::cerr);
|
|
|
|
}
|
|
|
|
|
2002-03-29 04:44:18 +01: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 19:36:02 +01:00
|
|
|
SymbolTable &SymTab = getSymbolTable();
|
2002-03-29 04:44:18 +01:00
|
|
|
|
|
|
|
// See if we have a definitions for the specified function already...
|
2002-11-20 19:36:02 +01:00
|
|
|
if (Value *V = SymTab.lookup(PointerType::get(Ty), Name)) {
|
2002-03-29 04:44:18 +01:00
|
|
|
return cast<Function>(V); // Yup, got it
|
|
|
|
} else { // Nope, add one
|
2003-04-16 22:28:45 +02:00
|
|
|
Function *New = new Function(Ty, GlobalVariable::ExternalLinkage, Name);
|
2002-03-29 04:44:18 +01:00
|
|
|
FunctionList.push_back(New);
|
|
|
|
return New; // Return the new prototype...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-31 02:19:28 +02: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 04:44:18 +01: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 19:36:02 +01:00
|
|
|
SymbolTable &SymTab = getSymbolTable();
|
|
|
|
return cast_or_null<Function>(SymTab.lookup(PointerType::get(Ty), Name));
|
2002-03-29 04:44:18 +01:00
|
|
|
}
|
|
|
|
|
2002-03-29 05:48:40 +01: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) {
|
2002-11-20 19:36:02 +01:00
|
|
|
SymbolTable &ST = getSymbolTable();
|
2002-03-29 05:48:40 +01:00
|
|
|
|
2002-11-20 19:36:02 +01:00
|
|
|
if (ST.lookup(Type::TypeTy, Name)) return true; // Already in symtab...
|
2002-03-29 05:48:40 +01:00
|
|
|
|
|
|
|
// Not in symbol table? Set the name with the Symtab as an argument so the
|
|
|
|
// type knows what to update...
|
2002-11-20 19:36:02 +01:00
|
|
|
((Value*)Ty)->setName(Name, &ST);
|
2002-03-29 05:48:40 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2002-03-29 04:44:18 +01:00
|
|
|
|
2002-11-08 21:34:02 +01: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;
|
|
|
|
|
|
|
|
// void main(int argc, char **argv)...
|
|
|
|
if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
|
|
|
|
Params, false)))
|
|
|
|
return F;
|
|
|
|
}
|
|
|
|
|
2002-11-19 19:41:44 +01: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.
|
|
|
|
///
|
|
|
|
Function *Module::getNamedFunction(const std::string &Name) {
|
|
|
|
// Loop over all of the functions, looking for the function desired
|
2003-07-23 22:21:30 +02:00
|
|
|
Function *Found = 0;
|
2002-11-08 21:34:02 +01:00
|
|
|
for (iterator I = begin(), E = end(); I != E; ++I)
|
2002-11-19 19:41:44 +01:00
|
|
|
if (I->getName() == Name)
|
2003-07-23 22:21:30 +02:00
|
|
|
if (I->isExternal())
|
|
|
|
Found = I;
|
|
|
|
else
|
|
|
|
return I;
|
|
|
|
return Found; // Non-external function not found...
|
2002-11-08 21:34:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-04-13 20:58:33 +02:00
|
|
|
// getTypeName - If there is at least one entry in the symbol table for the
|
|
|
|
// specified type, return it.
|
|
|
|
//
|
|
|
|
std::string Module::getTypeName(const Type *Ty) {
|
2002-11-20 19:36:02 +01:00
|
|
|
const SymbolTable &ST = getSymbolTable();
|
|
|
|
if (ST.find(Type::TypeTy) == ST.end())
|
2002-04-13 20:58:33 +02:00
|
|
|
return ""; // No names for types...
|
|
|
|
|
2002-11-20 19:36:02 +01:00
|
|
|
SymbolTable::type_const_iterator TI = ST.type_begin(Type::TypeTy);
|
|
|
|
SymbolTable::type_const_iterator TE = ST.type_end(Type::TypeTy);
|
2002-04-13 20:58:33 +02:00
|
|
|
|
|
|
|
while (TI != TE && TI->second != (const Value*)Ty)
|
|
|
|
++TI;
|
|
|
|
|
|
|
|
if (TI != TE) // Must have found an entry!
|
|
|
|
return TI->first;
|
|
|
|
return ""; // Must not have found anything...
|
|
|
|
}
|
|
|
|
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2002-08-18 01:32:47 +02: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 19:54:14 +02:00
|
|
|
// is deleted for real. Note that no operations are valid on an object that
|
2002-08-18 01:32:47 +02:00
|
|
|
// has "dropped all references", except operator delete.
|
2001-06-06 22:29:01 +02:00
|
|
|
//
|
|
|
|
void Module::dropAllReferences() {
|
2002-06-25 18:13:24 +02:00
|
|
|
for(Module::iterator I = begin(), E = end(); I != E; ++I)
|
|
|
|
I->dropAllReferences();
|
2001-10-13 08:58:40 +02:00
|
|
|
|
2002-06-25 18:13:24 +02:00
|
|
|
for(Module::giterator I = gbegin(), E = gend(); I != E; ++I)
|
|
|
|
I->dropAllReferences();
|
2001-10-13 08:58:40 +02:00
|
|
|
|
|
|
|
// If there are any GlobalVariable references still out there, nuke them now.
|
|
|
|
// Since all references are hereby dropped, nothing could possibly reference
|
2002-08-18 02:40:04 +02:00
|
|
|
// them still. Note that destroying all of the constant pointer refs will
|
|
|
|
// eventually cause the GVRefMap field to be set to null (by
|
|
|
|
// destroyConstantPointerRef, below).
|
|
|
|
//
|
|
|
|
while (GVRefMap)
|
|
|
|
// Delete the ConstantPointerRef node...
|
|
|
|
GVRefMap->Map.begin()->second->destroyConstant();
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
2001-06-30 06:35:40 +02:00
|
|
|
|
2001-10-13 08:58:40 +02:00
|
|
|
// Accessor for the underlying GlobalValRefMap...
|
2001-12-03 23:26:30 +01:00
|
|
|
ConstantPointerRef *Module::getConstantPointerRef(GlobalValue *V){
|
2001-10-13 08:58:40 +02:00
|
|
|
// Create ref map lazily on demand...
|
|
|
|
if (GVRefMap == 0) GVRefMap = new GlobalValueRefMap();
|
|
|
|
|
2002-08-12 22:23:29 +02:00
|
|
|
GlobalValueRefMap::iterator I = GVRefMap->Map.find(V);
|
|
|
|
if (I != GVRefMap->Map.end()) return I->second;
|
2001-10-13 08:58:40 +02:00
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
ConstantPointerRef *Ref = new ConstantPointerRef(V);
|
2002-08-18 02:40:04 +02:00
|
|
|
GVRefMap->Map[V] = Ref;
|
2001-10-13 08:58:40 +02:00
|
|
|
return Ref;
|
|
|
|
}
|
|
|
|
|
2002-08-18 02:40:04 +02:00
|
|
|
void Module::destroyConstantPointerRef(ConstantPointerRef *CPR) {
|
|
|
|
assert(GVRefMap && "No map allocated, but we have a CPR?");
|
|
|
|
if (!GVRefMap->Map.erase(CPR->getValue())) // Remove it from the map...
|
|
|
|
assert(0 && "ConstantPointerRef not found in module CPR map!");
|
|
|
|
|
|
|
|
if (GVRefMap->Map.empty()) { // If the map is empty, delete it.
|
|
|
|
delete GVRefMap;
|
|
|
|
GVRefMap = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-03 23:26:30 +01:00
|
|
|
void Module::mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV) {
|
2003-05-15 21:37:21 +02:00
|
|
|
assert(OldGV != NewGV && "Cannot mutate to the same global!");
|
2002-08-12 22:23:29 +02:00
|
|
|
GlobalValueRefMap::iterator I = GVRefMap->Map.find(OldGV);
|
|
|
|
assert(I != GVRefMap->Map.end() &&
|
2001-12-03 23:26:30 +01:00
|
|
|
"mutateConstantPointerRef; OldGV not in table!");
|
|
|
|
ConstantPointerRef *Ref = I->second;
|
2001-10-13 08:58:40 +02:00
|
|
|
|
|
|
|
// Remove the old entry...
|
2002-08-12 22:23:29 +02:00
|
|
|
GVRefMap->Map.erase(I);
|
2001-10-13 08:58:40 +02:00
|
|
|
|
2003-05-15 21:37:21 +02:00
|
|
|
// Check to see if a CPR already exists for NewGV
|
|
|
|
I = GVRefMap->Map.lower_bound(NewGV);
|
|
|
|
|
|
|
|
if (I == GVRefMap->Map.end() || I->first != NewGV) {
|
|
|
|
// Insert the new entry...
|
|
|
|
GVRefMap->Map.insert(I, std::make_pair(NewGV, Ref));
|
|
|
|
} else {
|
|
|
|
// Otherwise, an entry already exists for the current global value.
|
|
|
|
// Completely replace the old CPR with the existing one...
|
|
|
|
Ref->replaceAllUsesWith(I->second);
|
|
|
|
delete Ref;
|
|
|
|
}
|
2001-10-13 08:58:40 +02:00
|
|
|
}
|
2003-11-11 23:41:34 +01:00
|
|
|
|