Add new linkage types to support a real frontend

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@5786 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2003-04-16 20:28:45 +00:00
parent 2c72b184b8
commit 4ad02e726d
26 changed files with 334 additions and 224 deletions

View File

@ -71,8 +71,8 @@ public:
/// function is automatically inserted into the end of the function list for
/// the module.
///
Function(const FunctionType *Ty, bool isInternal, const std::string &N = "",
Module *M = 0);
Function(const FunctionType *Ty, LinkageTypes Linkage,
const std::string &N = "", Module *M = 0);
~Function();
// Specialize setName to handle symbol table majik...

View File

@ -16,12 +16,19 @@ class Module;
class GlobalValue : public User {
GlobalValue(const GlobalValue &); // do not implement
public:
enum LinkageTypes {
ExternalLinkage, // Externally visible function
LinkOnceLinkage, // Keep one copy of named function when linking (inline)
AppendingLinkage, // Special purpose, only applies to global arrays
InternalLinkage // Rename collisions when linking (static functions)
};
protected:
GlobalValue(const Type *Ty, ValueTy vty, bool hasInternalLinkage,
GlobalValue(const Type *Ty, ValueTy vty, LinkageTypes linkage,
const std::string &name = "")
: User(Ty, vty, name), HasInternalLinkage(hasInternalLinkage), Parent(0) {}
: User(Ty, vty, name), Linkage(linkage), Parent(0) {}
bool HasInternalLinkage; // Is this value accessable externally?
LinkageTypes Linkage; // The linkage of this global
Module *Parent;
public:
~GlobalValue() {}
@ -31,10 +38,12 @@ public:
return (const PointerType*)User::getType();
}
/// Internal Linkage - True if the global value is inaccessible to
bool hasInternalLinkage() const { return HasInternalLinkage; }
bool hasExternalLinkage() const { return !HasInternalLinkage; }
void setInternalLinkage(bool HIL) { HasInternalLinkage = HIL; }
bool hasExternalLinkage() const { return Linkage == ExternalLinkage; }
bool hasLinkOnceLinkage() const { return Linkage == LinkOnceLinkage; }
bool hasAppendingLinkage() const { return Linkage == AppendingLinkage; }
bool hasInternalLinkage() const { return Linkage == InternalLinkage; }
void setLinkage(LinkageTypes LT) { Linkage = LT; }
LinkageTypes getLinkage() const { return Linkage; }
/// isExternal - Return true if the primary definition of this global value is
/// outside of the current translation unit...

View File

@ -35,7 +35,7 @@ public:
/// GlobalVariable ctor - If a parent module is specified, the global is
/// automatically inserted into the end of the specified modules global list.
///
GlobalVariable(const Type *Ty, bool isConstant, bool isInternal,
GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
Constant *Initializer = 0, const std::string &Name = "",
Module *Parent = 0);

View File

@ -137,7 +137,8 @@ Cilkifier::Cilkifier(Module& M)
DummySyncFunc = new Function(FunctionType::get( Type::VoidTy,
std::vector<const Type*>(),
/*isVararg*/ false),
/*isInternal*/ false, DummySyncFuncName, &M);
GlobalValue::ExternalLinkage, DummySyncFuncName,
&M);
}
void Cilkifier::TransformFunc(Function* F,

View File

@ -155,6 +155,8 @@ global { return GLOBAL; }
constant { return CONSTANT; }
const { return CONST; }
internal { return INTERNAL; }
linkonce { return LINKONCE; }
appending { return APPENDING; }
uninitialized { return EXTERNAL; } /* Deprecated, turn into external */
external { return EXTERNAL; }
implementation { return IMPLEMENTATION; }

View File

@ -177,7 +177,7 @@ struct BBPlaceHolderHelper : public BasicBlock {
struct MethPlaceHolderHelper : public Function {
MethPlaceHolderHelper(const Type *Ty)
: Function(cast<FunctionType>(Ty), true) {}
: Function(cast<FunctionType>(Ty), InternalLinkage) {}
};
typedef PlaceholderValue<InstPlaceHolderHelper> ValuePlaceHolder;

View File

@ -522,8 +522,7 @@ static bool setValueName(Value *V, char *NameStr) {
EGV->setInitializer(GV->getInitializer());
if (GV->isConstant())
EGV->setConstant(true);
if (GV->hasInternalLinkage())
EGV->setInternalLinkage(true);
EGV->setLinkage(GV->getLinkage());
delete GV; // Destroy the duplicate!
return true; // They are equivalent!
@ -624,6 +623,7 @@ Module *RunVMAsmParser(const string &Filename, FILE *F) {
std::vector<std::pair<Constant*, BasicBlock*> > *JumpTable;
std::vector<Constant*> *ConstVector;
GlobalValue::LinkageTypes Linkage;
int64_t SInt64Val;
uint64_t UInt64Val;
int SIntVal;
@ -654,7 +654,8 @@ Module *RunVMAsmParser(const string &Filename, FILE *F) {
%type <ValueList> IndexList // For GEP derived indices
%type <TypeList> TypeListI ArgTypeListI
%type <JumpTable> JumpTable
%type <BoolVal> GlobalType OptInternal // GLOBAL or CONSTANT? Intern?
%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
%type <Linkage> OptLinkage
// ValueRef - Unresolved reference to a definition or BB
%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
@ -684,7 +685,8 @@ Module *RunVMAsmParser(const string &Filename, FILE *F) {
%token IMPLEMENTATION TRUE FALSE BEGINTOK ENDTOK DECLARE GLOBAL CONSTANT
%token TO EXCEPT DOTDOTDOT NULL_TOK CONST INTERNAL OPAQUE NOT EXTERNAL
%token TO EXCEPT DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE APPENDING
%token OPAQUE NOT EXTERNAL
// Basic Block Terminating Operators
%token <TermOpVal> RET BR SWITCH
@ -748,7 +750,10 @@ OptAssign : VAR_ID '=' {
$$ = 0;
};
OptInternal : INTERNAL { $$ = true; } | /*empty*/ { $$ = false; };
OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
APPENDING { $$ = GlobalValue::AppendingLinkage; } |
/*empty*/ { $$ = GlobalValue::ExternalLinkage; };
//===----------------------------------------------------------------------===//
// Types includes all predefined types... except void, because it can only be
@ -982,7 +987,8 @@ ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
// Create a placeholder for the global variable reference...
GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
false, true);
false,
GlobalValue::ExternalLinkage);
// Keep track of the fact that we have a forward ref to recycle it
CurModule.GlobalRefs.insert(make_pair(make_pair(PT, $2), GV));
@ -1136,7 +1142,7 @@ ConstPool : ConstPool OptAssign CONST ConstVal {
}
| ConstPool FunctionProto { // Function prototypes can be in const pool
}
| ConstPool OptAssign OptInternal GlobalType ConstVal {
| ConstPool OptAssign OptLinkage GlobalType ConstVal {
const Type *Ty = $5->getType();
// Global declarations appear in Constant Pool
Constant *Initializer = $5;
@ -1159,7 +1165,7 @@ ConstPool : ConstPool OptAssign CONST ConstVal {
| ConstPool OptAssign EXTERNAL GlobalType Types {
const Type *Ty = *$5;
// Global declarations appear in Constant Pool
GlobalVariable *GV = new GlobalVariable(Ty, $4, false);
GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
if (!setValueName(GV, $2)) { // If not redefining...
CurModule.CurrentModule->getGlobalList().push_back(GV);
int Slot = InsertValue(GV, CurModule.Values);
@ -1255,7 +1261,7 @@ FunctionHeaderH : TypesV FuncName '(' ArgList ')' {
AI->setName("");
} else { // Not already defined?
Fn = new Function(FT, false, FunctionName);
Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName);
InsertValue(Fn, CurModule.Values);
CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
}
@ -1288,13 +1294,12 @@ FunctionHeaderH : TypesV FuncName '(' ArgList ')' {
BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
FunctionHeader : OptInternal FunctionHeaderH BEGIN {
FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
$$ = CurMeth.CurrentFunction;
// Make sure that we keep track of the internal marker, even if there was
// a previous "declare".
if ($1)
$$->setInternalLinkage(true);
// Make sure that we keep track of the linkage type even if there was a
// previous "declare".
$$->setLinkage($1);
// Resolve circular types before we parse the body of the function.
ResolveTypes(CurMeth.LateResolveTypes);

View File

@ -352,7 +352,8 @@ bool BytecodeParser::parseConstantValue(const uchar *&Buf, const uchar *EndBuf,
// Create a placeholder for the global variable reference...
GlobalVariable *GVar =
new GlobalVariable(PT->getElementType(), false, true);
new GlobalVariable(PT->getElementType(), false,
GlobalValue::InternalLinkage);
// Keep track of the fact that we have a forward ref to recycle it
GlobalRefs.insert(std::make_pair(std::make_pair(PT, Slot), GVar));

View File

@ -307,7 +307,8 @@ bool BytecodeParser::ParseFunction(const uchar *&Buf, const uchar *EndBuf) {
Function *F = FunctionSignatureList.back().first;
unsigned FunctionSlot = FunctionSignatureList.back().second;
FunctionSignatureList.pop_back();
F->setInternalLinkage(isInternal != 0);
F->setLinkage(isInternal ? GlobalValue::InternalLinkage :
GlobalValue::ExternalLinkage);
const FunctionType::ParamTypes &Params =F->getFunctionType()->getParamTypes();
Function::aiterator AI = F->abegin();
@ -399,8 +400,13 @@ bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End){
const Type *ElTy = cast<PointerType>(Ty)->getElementType();
GlobalValue::LinkageTypes Linkage =
(VarType & 4) ? GlobalValue::InternalLinkage :
GlobalValue::ExternalLinkage;
// Create the global variable...
GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, VarType & 4,
GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, Linkage,
0, "", TheModule);
int DestSlot = insertValue(GV, ModuleValues);
if (DestSlot == -1) return true;
@ -435,7 +441,8 @@ bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End){
// this placeholder is replaced.
// Insert the placeholder...
Function *Func = new Function(cast<FunctionType>(Ty), false, "", TheModule);
Function *Func = new Function(cast<FunctionType>(Ty),
GlobalValue::InternalLinkage, "", TheModule);
int DestSlot = insertValue(Func, ModuleValues);
if (DestSlot == -1) return true;
ResolveReferencesToValue(Func, (unsigned)DestSlot);

View File

@ -189,42 +189,48 @@ static bool LinkGlobals(Module *Dest, const Module *Src,
//
for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
const GlobalVariable *SGV = I;
Value *V;
// If the global variable has a name, and that name is already in use in the
// Dest module, make sure that the name is a compatible global variable...
//
if (SGV->hasExternalLinkage() && SGV->hasName() &&
(V = ST->lookup(SGV->getType(), SGV->getName())) &&
cast<GlobalVariable>(V)->hasExternalLinkage()) {
// The same named thing is a global variable, because the only two things
GlobalVariable *DGV = 0;
if (SGV->hasName()) {
// A same named thing is a global variable, because the only two things
// that may be in a module level symbol table are Global Vars and
// Functions, and they both have distinct, nonoverlapping, possible types.
//
GlobalVariable *DGV = cast<GlobalVariable>(V);
DGV = cast_or_null<GlobalVariable>(ST->lookup(SGV->getType(),
SGV->getName()));
}
assert(SGV->hasInitializer() || SGV->hasExternalLinkage() &&
"Global must either be external or have an initializer!");
if (!DGV || DGV->hasInternalLinkage() || SGV->hasInternalLinkage()) {
// No linking to be performed, simply create an identical version of the
// symbol over in the dest module... the initializer will be filled in
// later by LinkGlobalInits...
//
DGV = new GlobalVariable(SGV->getType()->getElementType(),
SGV->isConstant(), SGV->getLinkage(), /*init*/0,
SGV->getName(), Dest);
// Make sure to remember this mapping...
ValueMap.insert(std::make_pair(SGV, DGV));
} else if (SGV->getLinkage() != DGV->getLinkage()) {
return Error(Err, "Global variables named '" + SGV->getName() +
"' have different linkage specifiers!");
} else if (SGV->hasExternalLinkage() || SGV->hasLinkOnceLinkage() ||
SGV->hasAppendingLinkage()) {
// If the global variable has a name, and that name is already in use in
// the Dest module, make sure that the name is a compatible global
// variable...
//
// Check to see if the two GV's have the same Const'ness...
if (SGV->isConstant() != DGV->isConstant())
return Error(Err, "Global Variable Collision on '" +
SGV->getType()->getDescription() + "':%" + SGV->getName() +
" - Global variables differ in const'ness");
// Okay, everything is cool, remember the mapping...
ValueMap.insert(std::make_pair(SGV, DGV));
} else {
// No linking to be performed, simply create an identical version of the
// symbol over in the dest module... the initializer will be filled in
// later by LinkGlobalInits...
//
GlobalVariable *DGV =
new GlobalVariable(SGV->getType()->getElementType(), SGV->isConstant(),
SGV->hasInternalLinkage(), 0, SGV->getName());
// Add the new global to the dest module
Dest->getGlobalList().push_back(DGV);
// Make sure to remember this mapping...
ValueMap.insert(std::make_pair(SGV, DGV));
assert(0 && "Unknown linkage!");
}
}
return false;
@ -245,19 +251,28 @@ static bool LinkGlobalInits(Module *Dest, const Module *Src,
if (SGV->hasInitializer()) { // Only process initialized GV's
// Figure out what the initializer looks like in the dest module...
Constant *DInit =
Constant *SInit =
cast<Constant>(RemapOperand(SGV->getInitializer(), ValueMap, 0));
GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[SGV]);
if (DGV->hasInitializer() && SGV->hasExternalLinkage() &&
DGV->hasExternalLinkage()) {
if (DGV->getInitializer() != DInit)
return Error(Err, "Global Variable Collision on '" +
SGV->getType()->getDescription() + "':%" +SGV->getName()+
" - Global variables have different initializers");
if (DGV->hasInitializer()) {
assert(SGV->getLinkage() == DGV->getLinkage());
if (SGV->hasExternalLinkage()) {
if (DGV->getInitializer() != SInit)
return Error(Err, "Global Variable Collision on '" +
SGV->getType()->getDescription() +"':%"+SGV->getName()+
" - Global variables have different initializers");
} else if (DGV->hasLinkOnceLinkage()) {
// Nothing is required, mapped values will take the new global
// automatically.
} else if (DGV->hasAppendingLinkage()) {
assert(0 && "Appending linkage unimplemented!");
} else {
assert(0 && "Unknown linkage!");
}
} else {
// Copy the initializer over now...
DGV->setInitializer(DInit);
DGV->setInitializer(SInit);
}
}
}
@ -278,39 +293,42 @@ static bool LinkFunctionProtos(Module *Dest, const Module *Src,
//
for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
const Function *SF = I; // SrcFunction
Value *V;
// If the function has a name, and that name is already in use in the Dest
// module, make sure that the name is a compatible function...
//
if (SF->hasExternalLinkage() && SF->hasName() &&
(V = ST->lookup(SF->getType(), SF->getName())) &&
cast<Function>(V)->hasExternalLinkage()) {
Function *DF = 0;
if (SF->hasName())
// The same named thing is a Function, because the only two things
// that may be in a module level symbol table are Global Vars and
// Functions, and they both have distinct, nonoverlapping, possible types.
//
Function *DF = cast<Function>(V); // DestFunction
DF = cast_or_null<Function>(ST->lookup(SF->getType(), SF->getName()));
if (!DF || SF->hasInternalLinkage() || DF->hasInternalLinkage()) {
// Function does not already exist, simply insert an external function
// signature identical to SF into the dest module...
Function *DF = new Function(SF->getFunctionType(), SF->getLinkage(),
SF->getName(), Dest);
// ... and remember this mapping...
ValueMap.insert(std::make_pair(SF, DF));
} else if (SF->getLinkage() != DF->getLinkage()) {
return Error(Err, "Functions named '" + SF->getName() +
"' have different linkage specifiers!");
} else if (SF->getLinkage() == GlobalValue::AppendingLinkage) {
return Error(Err, "Functions named '" + SF->getName() +
"' have appending linkage!");
} else if (SF->getLinkage() == GlobalValue::ExternalLinkage) {
// If the function has a name, and that name is already in use in the Dest
// module, make sure that the name is a compatible function...
//
// Check to make sure the function is not defined in both modules...
if (!SF->isExternal() && !DF->isExternal())
return Error(Err, "Function '" +
SF->getFunctionType()->getDescription() + "':\"" +
SF->getName() + "\" - Function is already defined!");
// Otherwise, just remember this mapping...
ValueMap.insert(std::make_pair(SF, DF));
} else {
// Function does not already exist, simply insert an external function
// signature identical to SF into the dest module...
Function *DF = new Function(SF->getFunctionType(),
SF->hasInternalLinkage(),
SF->getName());
// Add the function signature to the dest module...
Dest->getFunctionList().push_back(DF);
// ... and remember this mapping...
} else if (SF->getLinkage() == GlobalValue::LinkOnceLinkage) {
// Completely ignore the source function.
ValueMap.insert(std::make_pair(SF, DF));
}
}
@ -391,6 +409,7 @@ static bool LinkFunctionBodies(Module *Dest, const Module *Src,
// DF not external SF external?
if (!DF->isExternal()) {
if (DF->hasLinkOnceLinkage()) continue; // No relinkage for link-once!
if (Err)
*Err = "Function '" + (SF->hasName() ? SF->getName() :std::string(""))
+ "' body multiply defined!";

View File

@ -87,7 +87,8 @@ namespace {
GV = PI->second; // put in map
else
{
GV = new GlobalVariable(CV->getType(), true,true,CV); //put in map
GV = new GlobalVariable(CV->getType(), true, //put in map
GlobalValue::InternalLinkage, CV);
myModule->getGlobalList().push_back(GV); // GV owned by module now
}
}

View File

@ -16,13 +16,13 @@ namespace {
}
// Make sure our result is globally accessable...
Named->setInternalLinkage(false);
Named->setLinkage(GlobalValue::ExternalLinkage);
// Mark all global variables internal
for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
if (!I->isExternal()) {
I->setInitializer(0); // Make all variables external
I->setInternalLinkage(false); // Make sure it's not internal
I->setLinkage(GlobalValue::ExternalLinkage);
}
// All of the functions may be used by global variables or the named
@ -35,7 +35,9 @@ namespace {
for (Module::iterator I = M.begin(); ; ++I) {
if (&*I != Named) {
Function *New = new Function(I->getFunctionType(),false,I->getName());
Function *New = new Function(I->getFunctionType(),
GlobalValue::ExternalLinkage,
I->getName());
I->setName(""); // Remove Old name
// If it's not the named function, delete the body of the function

View File

@ -29,7 +29,7 @@ namespace {
if (&*I != MainFunc && // Leave the main function external
!I->isExternal() && // Function must be defined here
!I->hasInternalLinkage()) { // Can't already have internal linkage
I->setInternalLinkage(true);
I->setLinkage(GlobalValue::InternalLinkage);
Changed = true;
++NumFunctions;
DEBUG(std::cerr << "Internalizing func " << I->getName() << "\n");
@ -38,7 +38,7 @@ namespace {
// Mark all global variables with initializers as internal as well...
for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
if (!I->isExternal() && I->hasExternalLinkage()) {
I->setInternalLinkage(true);
I->setLinkage(GlobalValue::InternalLinkage);
Changed = true;
++NumGlobals;
DEBUG(std::cerr << "Internalizing gvar " << I->getName() << "\n");

View File

@ -250,8 +250,7 @@ void MutateStructTypes::processGlobals(Module &M) {
cast<FunctionType>(ConvertType(I->getFunctionType()));
// Create a new function to put stuff into...
Function *NewMeth = new Function(NewMTy, I->hasInternalLinkage(),
I->getName());
Function *NewMeth = new Function(NewMTy, I->getLinkage(), I->getName());
if (I->hasName())
I->setName("OLD."+I->getName());

View File

@ -137,7 +137,8 @@ Cilkifier::Cilkifier(Module& M)
DummySyncFunc = new Function(FunctionType::get( Type::VoidTy,
std::vector<const Type*>(),
/*isVararg*/ false),
/*isInternal*/ false, DummySyncFuncName, &M);
GlobalValue::ExternalLinkage, DummySyncFuncName,
&M);
}
void Cilkifier::TransformFunc(Function* F,

View File

@ -161,7 +161,8 @@ Function *PoolAllocate::MakeFunctionClone(Function &F) {
FunctionType *FuncTy = FunctionType::get(OldFuncTy->getReturnType(), ArgTys,
OldFuncTy->isVarArg());
// Create the new function...
Function *New = new Function(FuncTy, true, F.getName(), F.getParent());
Function *New = new Function(FuncTy, GlobalValue::InternalLinkage,
F.getName(), F.getParent());
// Set the rest of the new arguments names to be PDa<n> and add entries to the
// pool descriptors map

View File

@ -33,7 +33,8 @@ bool EmitFunctionTable::run(Module &M){
StructType *sttype = StructType::get(vType);
ConstantStruct *cstruct = ConstantStruct::get(sttype, vConsts);
GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true, false,
GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true,
GlobalValue::ExternalLinkage,
cstruct, "llvmFunctionTable");
M.getGlobalList().push_back(gb);
return true; // Always modifies program

View File

@ -183,14 +183,19 @@ bool ProfilePaths::runOnFunction(Function &F){
for(int xi=0; xi<numPaths; xi++)
arrayInitialize.push_back(ConstantSInt::get(Type::IntTy, 0));
Constant *initializer = ConstantArray::get(ArrayType::get(Type::IntTy, numPaths), arrayInitialize);
GlobalVariable *countVar = new GlobalVariable(ArrayType::get(Type::IntTy, numPaths), false, true, initializer, "Count", F.getParent());
const ArrayType *ATy = ArrayType::get(Type::IntTy, numPaths);
Constant *initializer = ConstantArray::get(ATy, arrayInitialize);
GlobalVariable *countVar = new GlobalVariable(ATy, false,
GlobalValue::InternalLinkage,
initializer, "Count",
F.getParent());
static GlobalVariable *threshold = NULL;
static bool insertedThreshold = false;
if(!insertedThreshold){
threshold = new GlobalVariable(Type::IntTy, false, false, 0,
"reopt_threshold");
threshold = new GlobalVariable(Type::IntTy, false,
GlobalValue::ExternalLinkage, 0,
"reopt_threshold");
F.getParent()->getGlobalList().push_back(threshold);
insertedThreshold = true;

View File

@ -154,7 +154,8 @@ static inline GlobalVariable *getStringRef(Module *M, const string &str) {
// Create the global variable and record it in the module
// The GV will be renamed to a unique name if needed.
GlobalVariable *GV = new GlobalVariable(Init->getType(), true, true, Init,
GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
GlobalValue::InternalLinkage, Init,
"trstr");
M->getGlobalList().push_back(GV);
return GV;

View File

@ -110,7 +110,7 @@ Function *CloneFunction(const Function *F,
ArgTypes, F->getFunctionType()->isVarArg());
// Create the new function...
Function *NewF = new Function(FTy, F->hasInternalLinkage(), F->getName());
Function *NewF = new Function(FTy, F->getLinkage(), F->getName());
// Loop over the arguments, copying the names of the mapped arguments over...
Function::aiterator DestI = NewF->abegin();

View File

@ -29,13 +29,14 @@ Module *CloneModule(const Module *M) {
// don't worry about attributes or initializers, they will come later.
//
for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
ValueMap[I] = new GlobalVariable(I->getType()->getElementType(),
false, false, 0, I->getName(), New);
ValueMap[I] = new GlobalVariable(I->getType()->getElementType(), false,
GlobalValue::ExternalLinkage, 0,
I->getName(), New);
// Loop over the functions in the module, making external functions as before
for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
ValueMap[I]=new Function(cast<FunctionType>(I->getType()->getElementType()),
false, I->getName(), New);
GlobalValue::ExternalLinkage, I->getName(), New);
// Now that all of the things that global variable initializer can refer to
// have been created, loop through and copy the global variable referrers
@ -46,8 +47,7 @@ Module *CloneModule(const Module *M) {
if (I->hasInitializer())
GV->setInitializer(cast<Constant>(MapValue(I->getInitializer(),
ValueMap)));
if (I->hasInternalLinkage())
GV->setInternalLinkage(true);
GV->setLinkage(I->getLinkage());
}
// Similarly, copy over function bodies now...
@ -65,8 +65,7 @@ Module *CloneModule(const Module *M) {
CloneFunctionInto(F, I, ValueMap, Returns);
}
if (I->hasInternalLinkage())
F->setInternalLinkage(true);
F->setLinkage(I->getLinkage());
}
return New;

View File

@ -189,42 +189,48 @@ static bool LinkGlobals(Module *Dest, const Module *Src,
//
for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
const GlobalVariable *SGV = I;
Value *V;
// If the global variable has a name, and that name is already in use in the
// Dest module, make sure that the name is a compatible global variable...
//
if (SGV->hasExternalLinkage() && SGV->hasName() &&
(V = ST->lookup(SGV->getType(), SGV->getName())) &&
cast<GlobalVariable>(V)->hasExternalLinkage()) {
// The same named thing is a global variable, because the only two things
GlobalVariable *DGV = 0;
if (SGV->hasName()) {
// A same named thing is a global variable, because the only two things
// that may be in a module level symbol table are Global Vars and
// Functions, and they both have distinct, nonoverlapping, possible types.
//
GlobalVariable *DGV = cast<GlobalVariable>(V);
DGV = cast_or_null<GlobalVariable>(ST->lookup(SGV->getType(),
SGV->getName()));
}
assert(SGV->hasInitializer() || SGV->hasExternalLinkage() &&
"Global must either be external or have an initializer!");
if (!DGV || DGV->hasInternalLinkage() || SGV->hasInternalLinkage()) {
// No linking to be performed, simply create an identical version of the
// symbol over in the dest module... the initializer will be filled in
// later by LinkGlobalInits...
//
DGV = new GlobalVariable(SGV->getType()->getElementType(),
SGV->isConstant(), SGV->getLinkage(), /*init*/0,
SGV->getName(), Dest);
// Make sure to remember this mapping...
ValueMap.insert(std::make_pair(SGV, DGV));
} else if (SGV->getLinkage() != DGV->getLinkage()) {
return Error(Err, "Global variables named '" + SGV->getName() +
"' have different linkage specifiers!");
} else if (SGV->hasExternalLinkage() || SGV->hasLinkOnceLinkage() ||
SGV->hasAppendingLinkage()) {
// If the global variable has a name, and that name is already in use in
// the Dest module, make sure that the name is a compatible global
// variable...
//
// Check to see if the two GV's have the same Const'ness...
if (SGV->isConstant() != DGV->isConstant())
return Error(Err, "Global Variable Collision on '" +
SGV->getType()->getDescription() + "':%" + SGV->getName() +
" - Global variables differ in const'ness");
// Okay, everything is cool, remember the mapping...
ValueMap.insert(std::make_pair(SGV, DGV));
} else {
// No linking to be performed, simply create an identical version of the
// symbol over in the dest module... the initializer will be filled in
// later by LinkGlobalInits...
//
GlobalVariable *DGV =
new GlobalVariable(SGV->getType()->getElementType(), SGV->isConstant(),
SGV->hasInternalLinkage(), 0, SGV->getName());
// Add the new global to the dest module
Dest->getGlobalList().push_back(DGV);
// Make sure to remember this mapping...
ValueMap.insert(std::make_pair(SGV, DGV));
assert(0 && "Unknown linkage!");
}
}
return false;
@ -245,19 +251,28 @@ static bool LinkGlobalInits(Module *Dest, const Module *Src,
if (SGV->hasInitializer()) { // Only process initialized GV's
// Figure out what the initializer looks like in the dest module...
Constant *DInit =
Constant *SInit =
cast<Constant>(RemapOperand(SGV->getInitializer(), ValueMap, 0));
GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[SGV]);
if (DGV->hasInitializer() && SGV->hasExternalLinkage() &&
DGV->hasExternalLinkage()) {
if (DGV->getInitializer() != DInit)
return Error(Err, "Global Variable Collision on '" +
SGV->getType()->getDescription() + "':%" +SGV->getName()+
" - Global variables have different initializers");
if (DGV->hasInitializer()) {
assert(SGV->getLinkage() == DGV->getLinkage());
if (SGV->hasExternalLinkage()) {
if (DGV->getInitializer() != SInit)
return Error(Err, "Global Variable Collision on '" +
SGV->getType()->getDescription() +"':%"+SGV->getName()+
" - Global variables have different initializers");
} else if (DGV->hasLinkOnceLinkage()) {
// Nothing is required, mapped values will take the new global
// automatically.
} else if (DGV->hasAppendingLinkage()) {
assert(0 && "Appending linkage unimplemented!");
} else {
assert(0 && "Unknown linkage!");
}
} else {
// Copy the initializer over now...
DGV->setInitializer(DInit);
DGV->setInitializer(SInit);
}
}
}
@ -278,39 +293,42 @@ static bool LinkFunctionProtos(Module *Dest, const Module *Src,
//
for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
const Function *SF = I; // SrcFunction
Value *V;
// If the function has a name, and that name is already in use in the Dest
// module, make sure that the name is a compatible function...
//
if (SF->hasExternalLinkage() && SF->hasName() &&
(V = ST->lookup(SF->getType(), SF->getName())) &&
cast<Function>(V)->hasExternalLinkage()) {
Function *DF = 0;
if (SF->hasName())
// The same named thing is a Function, because the only two things
// that may be in a module level symbol table are Global Vars and
// Functions, and they both have distinct, nonoverlapping, possible types.
//
Function *DF = cast<Function>(V); // DestFunction
DF = cast_or_null<Function>(ST->lookup(SF->getType(), SF->getName()));
if (!DF || SF->hasInternalLinkage() || DF->hasInternalLinkage()) {
// Function does not already exist, simply insert an external function
// signature identical to SF into the dest module...
Function *DF = new Function(SF->getFunctionType(), SF->getLinkage(),
SF->getName(), Dest);
// ... and remember this mapping...
ValueMap.insert(std::make_pair(SF, DF));
} else if (SF->getLinkage() != DF->getLinkage()) {
return Error(Err, "Functions named '" + SF->getName() +
"' have different linkage specifiers!");
} else if (SF->getLinkage() == GlobalValue::AppendingLinkage) {
return Error(Err, "Functions named '" + SF->getName() +
"' have appending linkage!");
} else if (SF->getLinkage() == GlobalValue::ExternalLinkage) {
// If the function has a name, and that name is already in use in the Dest
// module, make sure that the name is a compatible function...
//
// Check to make sure the function is not defined in both modules...
if (!SF->isExternal() && !DF->isExternal())
return Error(Err, "Function '" +
SF->getFunctionType()->getDescription() + "':\"" +
SF->getName() + "\" - Function is already defined!");
// Otherwise, just remember this mapping...
ValueMap.insert(std::make_pair(SF, DF));
} else {
// Function does not already exist, simply insert an external function
// signature identical to SF into the dest module...
Function *DF = new Function(SF->getFunctionType(),
SF->hasInternalLinkage(),
SF->getName());
// Add the function signature to the dest module...
Dest->getFunctionList().push_back(DF);
// ... and remember this mapping...
} else if (SF->getLinkage() == GlobalValue::LinkOnceLinkage) {
// Completely ignore the source function.
ValueMap.insert(std::make_pair(SF, DF));
}
}
@ -391,6 +409,7 @@ static bool LinkFunctionBodies(Module *Dest, const Module *Src,
// DF not external SF external?
if (!DF->isExternal()) {
if (DF->hasLinkOnceLinkage()) continue; // No relinkage for link-once!
if (Err)
*Err = "Function '" + (SF->hasName() ? SF->getName() :std::string(""))
+ "' body multiply defined!";

View File

@ -535,8 +535,15 @@ void AssemblyWriter::printModule(const Module *M) {
void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
if (GV->hasName()) Out << "%" << GV->getName() << " = ";
if (GV->hasInternalLinkage()) Out << "internal ";
if (!GV->hasInitializer()) Out << "external ";
if (!GV->hasInitializer())
Out << "external ";
else
switch (GV->getLinkage()) {
case GlobalValue::InternalLinkage: Out << "internal "; break;
case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
case GlobalValue::AppendingLinkage: Out << "appending "; break;
case GlobalValue::ExternalLinkage: break;
}
Out << (GV->isConstant() ? "constant " : "global ");
printType(GV->getType()->getElementType());
@ -594,8 +601,18 @@ void AssemblyWriter::printConstant(const Constant *CPV) {
//
void AssemblyWriter::printFunction(const Function *F) {
// Print out the return type and name...
Out << "\n" << (F->isExternal() ? "declare " : "")
<< (F->hasInternalLinkage() ? "internal " : "");
Out << "\n";
if (F->isExternal())
Out << "declare ";
else
switch (F->getLinkage()) {
case GlobalValue::InternalLinkage: Out << "internal "; break;
case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
case GlobalValue::AppendingLinkage: Out << "appending "; break;
case GlobalValue::ExternalLinkage: break;
}
printType(F->getReturnType()) << " %" << F->getName() << "(";
Table.incorporateFunction(F);

View File

@ -77,9 +77,9 @@ void Argument::setParent(Function *parent) {
// Function Implementation
//===----------------------------------------------------------------------===//
Function::Function(const FunctionType *Ty, bool isInternal,
Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
const std::string &name, Module *ParentModule)
: GlobalValue(PointerType::get(Ty), Value::FunctionVal, isInternal, name) {
: GlobalValue(PointerType::get(Ty), Value::FunctionVal, Linkage, name) {
BasicBlocks.setItemParent(this);
BasicBlocks.setParent(this);
ArgumentList.setItemParent(this);
@ -154,10 +154,10 @@ void Function::dropAllReferences() {
// GlobalVariable Implementation
//===----------------------------------------------------------------------===//
GlobalVariable::GlobalVariable(const Type *Ty, bool constant, bool isIntern,
GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
Constant *Initializer,
const std::string &Name, Module *ParentModule)
: GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, isIntern, Name),
: GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, Link, Name),
isConstantGlobal(constant) {
if (Initializer) Operands.push_back(Use((Value*)Initializer, this));

View File

@ -189,42 +189,48 @@ static bool LinkGlobals(Module *Dest, const Module *Src,
//
for (Module::const_giterator I = Src->gbegin(), E = Src->gend(); I != E; ++I){
const GlobalVariable *SGV = I;
Value *V;
// If the global variable has a name, and that name is already in use in the
// Dest module, make sure that the name is a compatible global variable...
//
if (SGV->hasExternalLinkage() && SGV->hasName() &&
(V = ST->lookup(SGV->getType(), SGV->getName())) &&
cast<GlobalVariable>(V)->hasExternalLinkage()) {
// The same named thing is a global variable, because the only two things
GlobalVariable *DGV = 0;
if (SGV->hasName()) {
// A same named thing is a global variable, because the only two things
// that may be in a module level symbol table are Global Vars and
// Functions, and they both have distinct, nonoverlapping, possible types.
//
GlobalVariable *DGV = cast<GlobalVariable>(V);
DGV = cast_or_null<GlobalVariable>(ST->lookup(SGV->getType(),
SGV->getName()));
}
assert(SGV->hasInitializer() || SGV->hasExternalLinkage() &&
"Global must either be external or have an initializer!");
if (!DGV || DGV->hasInternalLinkage() || SGV->hasInternalLinkage()) {
// No linking to be performed, simply create an identical version of the
// symbol over in the dest module... the initializer will be filled in
// later by LinkGlobalInits...
//
DGV = new GlobalVariable(SGV->getType()->getElementType(),
SGV->isConstant(), SGV->getLinkage(), /*init*/0,
SGV->getName(), Dest);
// Make sure to remember this mapping...
ValueMap.insert(std::make_pair(SGV, DGV));
} else if (SGV->getLinkage() != DGV->getLinkage()) {
return Error(Err, "Global variables named '" + SGV->getName() +
"' have different linkage specifiers!");
} else if (SGV->hasExternalLinkage() || SGV->hasLinkOnceLinkage() ||
SGV->hasAppendingLinkage()) {
// If the global variable has a name, and that name is already in use in
// the Dest module, make sure that the name is a compatible global
// variable...
//
// Check to see if the two GV's have the same Const'ness...
if (SGV->isConstant() != DGV->isConstant())
return Error(Err, "Global Variable Collision on '" +
SGV->getType()->getDescription() + "':%" + SGV->getName() +
" - Global variables differ in const'ness");
// Okay, everything is cool, remember the mapping...
ValueMap.insert(std::make_pair(SGV, DGV));
} else {
// No linking to be performed, simply create an identical version of the
// symbol over in the dest module... the initializer will be filled in
// later by LinkGlobalInits...
//
GlobalVariable *DGV =
new GlobalVariable(SGV->getType()->getElementType(), SGV->isConstant(),
SGV->hasInternalLinkage(), 0, SGV->getName());
// Add the new global to the dest module
Dest->getGlobalList().push_back(DGV);
// Make sure to remember this mapping...
ValueMap.insert(std::make_pair(SGV, DGV));
assert(0 && "Unknown linkage!");
}
}
return false;
@ -245,19 +251,28 @@ static bool LinkGlobalInits(Module *Dest, const Module *Src,
if (SGV->hasInitializer()) { // Only process initialized GV's
// Figure out what the initializer looks like in the dest module...
Constant *DInit =
Constant *SInit =
cast<Constant>(RemapOperand(SGV->getInitializer(), ValueMap, 0));
GlobalVariable *DGV = cast<GlobalVariable>(ValueMap[SGV]);
if (DGV->hasInitializer() && SGV->hasExternalLinkage() &&
DGV->hasExternalLinkage()) {
if (DGV->getInitializer() != DInit)
return Error(Err, "Global Variable Collision on '" +
SGV->getType()->getDescription() + "':%" +SGV->getName()+
" - Global variables have different initializers");
if (DGV->hasInitializer()) {
assert(SGV->getLinkage() == DGV->getLinkage());
if (SGV->hasExternalLinkage()) {
if (DGV->getInitializer() != SInit)
return Error(Err, "Global Variable Collision on '" +
SGV->getType()->getDescription() +"':%"+SGV->getName()+
" - Global variables have different initializers");
} else if (DGV->hasLinkOnceLinkage()) {
// Nothing is required, mapped values will take the new global
// automatically.
} else if (DGV->hasAppendingLinkage()) {
assert(0 && "Appending linkage unimplemented!");
} else {
assert(0 && "Unknown linkage!");
}
} else {
// Copy the initializer over now...
DGV->setInitializer(DInit);
DGV->setInitializer(SInit);
}
}
}
@ -278,39 +293,42 @@ static bool LinkFunctionProtos(Module *Dest, const Module *Src,
//
for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
const Function *SF = I; // SrcFunction
Value *V;
// If the function has a name, and that name is already in use in the Dest
// module, make sure that the name is a compatible function...
//
if (SF->hasExternalLinkage() && SF->hasName() &&
(V = ST->lookup(SF->getType(), SF->getName())) &&
cast<Function>(V)->hasExternalLinkage()) {
Function *DF = 0;
if (SF->hasName())
// The same named thing is a Function, because the only two things
// that may be in a module level symbol table are Global Vars and
// Functions, and they both have distinct, nonoverlapping, possible types.
//
Function *DF = cast<Function>(V); // DestFunction
DF = cast_or_null<Function>(ST->lookup(SF->getType(), SF->getName()));
if (!DF || SF->hasInternalLinkage() || DF->hasInternalLinkage()) {
// Function does not already exist, simply insert an external function
// signature identical to SF into the dest module...
Function *DF = new Function(SF->getFunctionType(), SF->getLinkage(),
SF->getName(), Dest);
// ... and remember this mapping...
ValueMap.insert(std::make_pair(SF, DF));
} else if (SF->getLinkage() != DF->getLinkage()) {
return Error(Err, "Functions named '" + SF->getName() +
"' have different linkage specifiers!");
} else if (SF->getLinkage() == GlobalValue::AppendingLinkage) {
return Error(Err, "Functions named '" + SF->getName() +
"' have appending linkage!");
} else if (SF->getLinkage() == GlobalValue::ExternalLinkage) {
// If the function has a name, and that name is already in use in the Dest
// module, make sure that the name is a compatible function...
//
// Check to make sure the function is not defined in both modules...
if (!SF->isExternal() && !DF->isExternal())
return Error(Err, "Function '" +
SF->getFunctionType()->getDescription() + "':\"" +
SF->getName() + "\" - Function is already defined!");
// Otherwise, just remember this mapping...
ValueMap.insert(std::make_pair(SF, DF));
} else {
// Function does not already exist, simply insert an external function
// signature identical to SF into the dest module...
Function *DF = new Function(SF->getFunctionType(),
SF->hasInternalLinkage(),
SF->getName());
// Add the function signature to the dest module...
Dest->getFunctionList().push_back(DF);
// ... and remember this mapping...
} else if (SF->getLinkage() == GlobalValue::LinkOnceLinkage) {
// Completely ignore the source function.
ValueMap.insert(std::make_pair(SF, DF));
}
}
@ -391,6 +409,7 @@ static bool LinkFunctionBodies(Module *Dest, const Module *Src,
// DF not external SF external?
if (!DF->isExternal()) {
if (DF->hasLinkOnceLinkage()) continue; // No relinkage for link-once!
if (Err)
*Err = "Function '" + (SF->hasName() ? SF->getName() :std::string(""))
+ "' body multiply defined!";

View File

@ -17,13 +17,14 @@
Function *ilist_traits<Function>::createNode() {
FunctionType *FTy =
FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false);
Function *Ret = new Function(FTy, false);
Function *Ret = new Function(FTy, GlobalValue::ExternalLinkage);
// This should not be garbage monitored.
LeakDetector::removeGarbageObject(Ret);
return Ret;
}
GlobalVariable *ilist_traits<GlobalVariable>::createNode() {
GlobalVariable *Ret = new GlobalVariable(Type::IntTy, false, false);
GlobalVariable *Ret = new GlobalVariable(Type::IntTy, false,
GlobalValue::ExternalLinkage);
// This should not be garbage monitored.
LeakDetector::removeGarbageObject(Ret);
return Ret;
@ -87,7 +88,7 @@ Function *Module::getOrInsertFunction(const std::string &Name,
if (Value *V = SymTab.lookup(PointerType::get(Ty), Name)) {
return cast<Function>(V); // Yup, got it
} else { // Nope, add one
Function *New = new Function(Ty, false, Name);
Function *New = new Function(Ty, GlobalVariable::ExternalLinkage, Name);
FunctionList.push_back(New);
return New; // Return the new prototype...
}