Add support for 'weak' linkage.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@9171 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner
2003-10-16 18:29:00 +00:00
parent 6b25242a4b
commit 72ac148d49
9 changed files with 117 additions and 22 deletions

View File

@ -763,7 +763,7 @@ OptAssign : Name '=' {
OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } | OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
WEAK { $$ = GlobalValue::LinkOnceLinkage; /* FIXME */ } | WEAK { $$ = GlobalValue::WeakLinkage; } |
APPENDING { $$ = GlobalValue::AppendingLinkage; } | APPENDING { $$ = GlobalValue::AppendingLinkage; } |
/*empty*/ { $$ = GlobalValue::ExternalLinkage; }; /*empty*/ { $$ = GlobalValue::ExternalLinkage; };

View File

@ -433,6 +433,24 @@ static bool LinkGlobals(Module *Dest, const Module *Src,
} else if (DGV->isExternal()) { // If DGV is external but SGV is not... } else if (DGV->isExternal()) { // If DGV is external but SGV is not...
ValueMap.insert(std::make_pair(SGV, DGV)); ValueMap.insert(std::make_pair(SGV, DGV));
DGV->setLinkage(SGV->getLinkage()); // Inherit linkage! DGV->setLinkage(SGV->getLinkage()); // Inherit linkage!
} else if (SGV->hasWeakLinkage()) {
// At this point we know that DGV has LinkOnce, Appending, Weak, or
// External linkage. If DGV is Appending, this is an error.
if (DGV->hasAppendingLinkage())
return Error(Err, "Linking globals named '" + SGV->getName() +
" ' with 'weak' and 'appending' linkage is not allowed!");
// Otherwise, just perform the link.
ValueMap.insert(std::make_pair(SGV, DGV));
} else if (DGV->hasWeakLinkage()) {
// At this point we know that SGV has LinkOnce, Appending, or External
// linkage. If SGV is Appending, this is an error.
if (SGV->hasAppendingLinkage())
return Error(Err, "Linking globals named '" + SGV->getName() +
" ' with 'weak' and 'appending' linkage is not allowed!");
if (!SGV->hasLinkOnceLinkage())
DGV->setLinkage(SGV->getLinkage()); // Inherit linkage!
ValueMap.insert(std::make_pair(SGV, DGV));
} else if (SGV->getLinkage() != DGV->getLinkage()) { } else if (SGV->getLinkage() != DGV->getLinkage()) {
return Error(Err, "Global variables named '" + SGV->getName() + return Error(Err, "Global variables named '" + SGV->getName() +
"' have different linkage specifiers!"); "' have different linkage specifiers!");
@ -505,7 +523,7 @@ static bool LinkGlobalInits(Module *Dest, const Module *Src,
return Error(Err, "Global Variable Collision on '" + return Error(Err, "Global Variable Collision on '" +
SGV->getType()->getDescription() +"':%"+SGV->getName()+ SGV->getType()->getDescription() +"':%"+SGV->getName()+
" - Global variables have different initializers"); " - Global variables have different initializers");
} else if (DGV->hasLinkOnceLinkage()) { } else if (DGV->hasLinkOnceLinkage() || DGV->hasWeakLinkage()) {
// Nothing is required, mapped values will take the new global // Nothing is required, mapped values will take the new global
// automatically. // automatically.
} else if (DGV->hasAppendingLinkage()) { } else if (DGV->hasAppendingLinkage()) {
@ -574,6 +592,16 @@ static bool LinkFunctionProtos(Module *Dest, const Module *Src,
ValueMap.insert(std::make_pair(SF, DF)); ValueMap.insert(std::make_pair(SF, DF));
DF->setLinkage(SF->getLinkage()); DF->setLinkage(SF->getLinkage());
} else if (SF->hasWeakLinkage()) {
// At this point we know that DF has LinkOnce, Weak, or External linkage.
ValueMap.insert(std::make_pair(SF, DF));
} else if (DF->hasWeakLinkage()) {
// At this point we know that SF has LinkOnce or External linkage.
ValueMap.insert(std::make_pair(SF, DF));
if (!SF->hasLinkOnceLinkage()) // Don't inherit linkonce linkage
DF->setLinkage(SF->getLinkage());
} else if (SF->getLinkage() != DF->getLinkage()) { } else if (SF->getLinkage() != DF->getLinkage()) {
return Error(Err, "Functions named '" + SF->getName() + return Error(Err, "Functions named '" + SF->getName() +
"' have different linkage specifiers!"); "' have different linkage specifiers!");
@ -667,10 +695,9 @@ static bool LinkFunctionBodies(Module *Dest, const Module *Src,
// DF not external SF external? // DF not external SF external?
if (!DF->isExternal()) { if (!DF->isExternal()) {
if (DF->hasLinkOnceLinkage()) continue; // No relinkage for link-once! if (DF->hasLinkOnceLinkage()) continue; // No relinkage for link-once!
if (Err) if (SF->hasWeakLinkage()) continue;
*Err = "Function '" + (SF->hasName() ? SF->getName() :std::string("")) return Error(Err, "Function '" + SF->getName() +
+ "' body multiply defined!"; "' body multiply defined!");
return true;
} }
if (LinkFunctionBody(DF, SF, ValueMap, Err)) return true; if (LinkFunctionBody(DF, SF, ValueMap, Err)) return true;

View File

@ -684,6 +684,8 @@ void CWriter::printModule(Module *M) {
printType(Out, I->getType()->getElementType(), Mang->getValueName(I)); printType(Out, I->getType()->getElementType(), Mang->getValueName(I));
if (I->hasLinkOnceLinkage()) if (I->hasLinkOnceLinkage())
Out << " __attribute__((common))"; Out << " __attribute__((common))";
else if (I->hasWeakLinkage())
Out << " __attribute__((weak))";
if (!I->getInitializer()->isNullValue()) { if (!I->getInitializer()->isNullValue()) {
Out << " = " ; Out << " = " ;
writeOperand(I->getInitializer()); writeOperand(I->getInitializer());
@ -893,6 +895,8 @@ void CWriter::printFunctionSignature(const Function *F, bool Prototype) {
FunctionInnards << ")"; FunctionInnards << ")";
// Print out the return type and the entire signature for that matter // Print out the return type and the entire signature for that matter
printType(Out, F->getReturnType(), FunctionInnards.str()); printType(Out, F->getReturnType(), FunctionInnards.str());
if (F->hasWeakLinkage()) Out << " __attribute((weak))";
} }
void CWriter::printFunction(Function *F) { void CWriter::printFunction(Function *F) {

View File

@ -684,6 +684,8 @@ void CWriter::printModule(Module *M) {
printType(Out, I->getType()->getElementType(), Mang->getValueName(I)); printType(Out, I->getType()->getElementType(), Mang->getValueName(I));
if (I->hasLinkOnceLinkage()) if (I->hasLinkOnceLinkage())
Out << " __attribute__((common))"; Out << " __attribute__((common))";
else if (I->hasWeakLinkage())
Out << " __attribute__((weak))";
if (!I->getInitializer()->isNullValue()) { if (!I->getInitializer()->isNullValue()) {
Out << " = " ; Out << " = " ;
writeOperand(I->getInitializer()); writeOperand(I->getInitializer());
@ -893,6 +895,8 @@ void CWriter::printFunctionSignature(const Function *F, bool Prototype) {
FunctionInnards << ")"; FunctionInnards << ")";
// Print out the return type and the entire signature for that matter // Print out the return type and the entire signature for that matter
printType(Out, F->getReturnType(), FunctionInnards.str()); printType(Out, F->getReturnType(), FunctionInnards.str());
if (F->hasWeakLinkage()) Out << " __attribute((weak))";
} }
void CWriter::printFunction(Function *F) { void CWriter::printFunction(Function *F) {

View File

@ -948,7 +948,8 @@ bool Printer::doFinalization(Module &M) {
unsigned Align = TD.getTypeAlignment(C->getType()); unsigned Align = TD.getTypeAlignment(C->getType());
if (C->isNullValue() && if (C->isNullValue() &&
(I->hasLinkOnceLinkage() || I->hasInternalLinkage())) { (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
I->hasWeakLinkage() /* FIXME: Verify correct */)) {
SwitchSection(O, CurSection, ".data"); SwitchSection(O, CurSection, ".data");
if (I->hasInternalLinkage()) if (I->hasInternalLinkage())
O << "\t.local " << name << "\n"; O << "\t.local " << name << "\n";
@ -961,6 +962,7 @@ bool Printer::doFinalization(Module &M) {
} else { } else {
switch (I->getLinkage()) { switch (I->getLinkage()) {
case GlobalValue::LinkOnceLinkage: case GlobalValue::LinkOnceLinkage:
case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
// Nonnull linkonce -> weak // Nonnull linkonce -> weak
O << "\t.weak " << name << "\n"; O << "\t.weak " << name << "\n";
SwitchSection(O, CurSection, ""); SwitchSection(O, CurSection, "");

View File

@ -948,7 +948,8 @@ bool Printer::doFinalization(Module &M) {
unsigned Align = TD.getTypeAlignment(C->getType()); unsigned Align = TD.getTypeAlignment(C->getType());
if (C->isNullValue() && if (C->isNullValue() &&
(I->hasLinkOnceLinkage() || I->hasInternalLinkage())) { (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
I->hasWeakLinkage() /* FIXME: Verify correct */)) {
SwitchSection(O, CurSection, ".data"); SwitchSection(O, CurSection, ".data");
if (I->hasInternalLinkage()) if (I->hasInternalLinkage())
O << "\t.local " << name << "\n"; O << "\t.local " << name << "\n";
@ -961,6 +962,7 @@ bool Printer::doFinalization(Module &M) {
} else { } else {
switch (I->getLinkage()) { switch (I->getLinkage()) {
case GlobalValue::LinkOnceLinkage: case GlobalValue::LinkOnceLinkage:
case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
// Nonnull linkonce -> weak // Nonnull linkonce -> weak
O << "\t.weak " << name << "\n"; O << "\t.weak " << name << "\n";
SwitchSection(O, CurSection, ""); SwitchSection(O, CurSection, "");

View File

@ -433,6 +433,24 @@ static bool LinkGlobals(Module *Dest, const Module *Src,
} else if (DGV->isExternal()) { // If DGV is external but SGV is not... } else if (DGV->isExternal()) { // If DGV is external but SGV is not...
ValueMap.insert(std::make_pair(SGV, DGV)); ValueMap.insert(std::make_pair(SGV, DGV));
DGV->setLinkage(SGV->getLinkage()); // Inherit linkage! DGV->setLinkage(SGV->getLinkage()); // Inherit linkage!
} else if (SGV->hasWeakLinkage()) {
// At this point we know that DGV has LinkOnce, Appending, Weak, or
// External linkage. If DGV is Appending, this is an error.
if (DGV->hasAppendingLinkage())
return Error(Err, "Linking globals named '" + SGV->getName() +
" ' with 'weak' and 'appending' linkage is not allowed!");
// Otherwise, just perform the link.
ValueMap.insert(std::make_pair(SGV, DGV));
} else if (DGV->hasWeakLinkage()) {
// At this point we know that SGV has LinkOnce, Appending, or External
// linkage. If SGV is Appending, this is an error.
if (SGV->hasAppendingLinkage())
return Error(Err, "Linking globals named '" + SGV->getName() +
" ' with 'weak' and 'appending' linkage is not allowed!");
if (!SGV->hasLinkOnceLinkage())
DGV->setLinkage(SGV->getLinkage()); // Inherit linkage!
ValueMap.insert(std::make_pair(SGV, DGV));
} else if (SGV->getLinkage() != DGV->getLinkage()) { } else if (SGV->getLinkage() != DGV->getLinkage()) {
return Error(Err, "Global variables named '" + SGV->getName() + return Error(Err, "Global variables named '" + SGV->getName() +
"' have different linkage specifiers!"); "' have different linkage specifiers!");
@ -505,7 +523,7 @@ static bool LinkGlobalInits(Module *Dest, const Module *Src,
return Error(Err, "Global Variable Collision on '" + return Error(Err, "Global Variable Collision on '" +
SGV->getType()->getDescription() +"':%"+SGV->getName()+ SGV->getType()->getDescription() +"':%"+SGV->getName()+
" - Global variables have different initializers"); " - Global variables have different initializers");
} else if (DGV->hasLinkOnceLinkage()) { } else if (DGV->hasLinkOnceLinkage() || DGV->hasWeakLinkage()) {
// Nothing is required, mapped values will take the new global // Nothing is required, mapped values will take the new global
// automatically. // automatically.
} else if (DGV->hasAppendingLinkage()) { } else if (DGV->hasAppendingLinkage()) {
@ -574,6 +592,16 @@ static bool LinkFunctionProtos(Module *Dest, const Module *Src,
ValueMap.insert(std::make_pair(SF, DF)); ValueMap.insert(std::make_pair(SF, DF));
DF->setLinkage(SF->getLinkage()); DF->setLinkage(SF->getLinkage());
} else if (SF->hasWeakLinkage()) {
// At this point we know that DF has LinkOnce, Weak, or External linkage.
ValueMap.insert(std::make_pair(SF, DF));
} else if (DF->hasWeakLinkage()) {
// At this point we know that SF has LinkOnce or External linkage.
ValueMap.insert(std::make_pair(SF, DF));
if (!SF->hasLinkOnceLinkage()) // Don't inherit linkonce linkage
DF->setLinkage(SF->getLinkage());
} else if (SF->getLinkage() != DF->getLinkage()) { } else if (SF->getLinkage() != DF->getLinkage()) {
return Error(Err, "Functions named '" + SF->getName() + return Error(Err, "Functions named '" + SF->getName() +
"' have different linkage specifiers!"); "' have different linkage specifiers!");
@ -667,10 +695,9 @@ static bool LinkFunctionBodies(Module *Dest, const Module *Src,
// DF not external SF external? // DF not external SF external?
if (!DF->isExternal()) { if (!DF->isExternal()) {
if (DF->hasLinkOnceLinkage()) continue; // No relinkage for link-once! if (DF->hasLinkOnceLinkage()) continue; // No relinkage for link-once!
if (Err) if (SF->hasWeakLinkage()) continue;
*Err = "Function '" + (SF->hasName() ? SF->getName() :std::string("")) return Error(Err, "Function '" + SF->getName() +
+ "' body multiply defined!"; "' body multiply defined!");
return true;
} }
if (LinkFunctionBody(DF, SF, ValueMap, Err)) return true; if (LinkFunctionBody(DF, SF, ValueMap, Err)) return true;

View File

@ -586,6 +586,7 @@ void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
switch (GV->getLinkage()) { switch (GV->getLinkage()) {
case GlobalValue::InternalLinkage: Out << "internal "; break; case GlobalValue::InternalLinkage: Out << "internal "; break;
case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break; case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
case GlobalValue::WeakLinkage: Out << "weak "; break;
case GlobalValue::AppendingLinkage: Out << "appending "; break; case GlobalValue::AppendingLinkage: Out << "appending "; break;
case GlobalValue::ExternalLinkage: break; case GlobalValue::ExternalLinkage: break;
} }
@ -654,6 +655,7 @@ void AssemblyWriter::printFunction(const Function *F) {
switch (F->getLinkage()) { switch (F->getLinkage()) {
case GlobalValue::InternalLinkage: Out << "internal "; break; case GlobalValue::InternalLinkage: Out << "internal "; break;
case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break; case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
case GlobalValue::WeakLinkage: Out << "weak "; break;
case GlobalValue::AppendingLinkage: Out << "appending "; break; case GlobalValue::AppendingLinkage: Out << "appending "; break;
case GlobalValue::ExternalLinkage: break; case GlobalValue::ExternalLinkage: break;
} }

View File

@ -433,6 +433,24 @@ static bool LinkGlobals(Module *Dest, const Module *Src,
} else if (DGV->isExternal()) { // If DGV is external but SGV is not... } else if (DGV->isExternal()) { // If DGV is external but SGV is not...
ValueMap.insert(std::make_pair(SGV, DGV)); ValueMap.insert(std::make_pair(SGV, DGV));
DGV->setLinkage(SGV->getLinkage()); // Inherit linkage! DGV->setLinkage(SGV->getLinkage()); // Inherit linkage!
} else if (SGV->hasWeakLinkage()) {
// At this point we know that DGV has LinkOnce, Appending, Weak, or
// External linkage. If DGV is Appending, this is an error.
if (DGV->hasAppendingLinkage())
return Error(Err, "Linking globals named '" + SGV->getName() +
" ' with 'weak' and 'appending' linkage is not allowed!");
// Otherwise, just perform the link.
ValueMap.insert(std::make_pair(SGV, DGV));
} else if (DGV->hasWeakLinkage()) {
// At this point we know that SGV has LinkOnce, Appending, or External
// linkage. If SGV is Appending, this is an error.
if (SGV->hasAppendingLinkage())
return Error(Err, "Linking globals named '" + SGV->getName() +
" ' with 'weak' and 'appending' linkage is not allowed!");
if (!SGV->hasLinkOnceLinkage())
DGV->setLinkage(SGV->getLinkage()); // Inherit linkage!
ValueMap.insert(std::make_pair(SGV, DGV));
} else if (SGV->getLinkage() != DGV->getLinkage()) { } else if (SGV->getLinkage() != DGV->getLinkage()) {
return Error(Err, "Global variables named '" + SGV->getName() + return Error(Err, "Global variables named '" + SGV->getName() +
"' have different linkage specifiers!"); "' have different linkage specifiers!");
@ -505,7 +523,7 @@ static bool LinkGlobalInits(Module *Dest, const Module *Src,
return Error(Err, "Global Variable Collision on '" + return Error(Err, "Global Variable Collision on '" +
SGV->getType()->getDescription() +"':%"+SGV->getName()+ SGV->getType()->getDescription() +"':%"+SGV->getName()+
" - Global variables have different initializers"); " - Global variables have different initializers");
} else if (DGV->hasLinkOnceLinkage()) { } else if (DGV->hasLinkOnceLinkage() || DGV->hasWeakLinkage()) {
// Nothing is required, mapped values will take the new global // Nothing is required, mapped values will take the new global
// automatically. // automatically.
} else if (DGV->hasAppendingLinkage()) { } else if (DGV->hasAppendingLinkage()) {
@ -574,6 +592,16 @@ static bool LinkFunctionProtos(Module *Dest, const Module *Src,
ValueMap.insert(std::make_pair(SF, DF)); ValueMap.insert(std::make_pair(SF, DF));
DF->setLinkage(SF->getLinkage()); DF->setLinkage(SF->getLinkage());
} else if (SF->hasWeakLinkage()) {
// At this point we know that DF has LinkOnce, Weak, or External linkage.
ValueMap.insert(std::make_pair(SF, DF));
} else if (DF->hasWeakLinkage()) {
// At this point we know that SF has LinkOnce or External linkage.
ValueMap.insert(std::make_pair(SF, DF));
if (!SF->hasLinkOnceLinkage()) // Don't inherit linkonce linkage
DF->setLinkage(SF->getLinkage());
} else if (SF->getLinkage() != DF->getLinkage()) { } else if (SF->getLinkage() != DF->getLinkage()) {
return Error(Err, "Functions named '" + SF->getName() + return Error(Err, "Functions named '" + SF->getName() +
"' have different linkage specifiers!"); "' have different linkage specifiers!");
@ -667,10 +695,9 @@ static bool LinkFunctionBodies(Module *Dest, const Module *Src,
// DF not external SF external? // DF not external SF external?
if (!DF->isExternal()) { if (!DF->isExternal()) {
if (DF->hasLinkOnceLinkage()) continue; // No relinkage for link-once! if (DF->hasLinkOnceLinkage()) continue; // No relinkage for link-once!
if (Err) if (SF->hasWeakLinkage()) continue;
*Err = "Function '" + (SF->hasName() ? SF->getName() :std::string("")) return Error(Err, "Function '" + SF->getName() +
+ "' body multiply defined!"; "' body multiply defined!");
return true;
} }
if (LinkFunctionBody(DF, SF, ValueMap, Err)) return true; if (LinkFunctionBody(DF, SF, ValueMap, Err)) return true;