1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-26 12:43:36 +01:00

Darwin ABI issues: weak, linkonce, etc. dynamic-no-pic support is complete.

Also fixed a function stub bug. Added weak and linkonce support for
x86 Linux.

llvm-svn: 26038
This commit is contained in:
Evan Cheng 2006-02-07 08:38:37 +00:00
parent a7d31e53e8
commit 15369d0b89
4 changed files with 111 additions and 111 deletions

View File

@ -33,12 +33,32 @@ bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
EmitConstantPool(MF.getConstantPool()); EmitConstantPool(MF.getConstantPool());
// Print out labels for the function. // Print out labels for the function.
SwitchSection("\t.text\n", MF.getFunction()); const Function *F = MF.getFunction();
EmitAlignment(4); // FIXME: This should be parameterized somewhere. switch (F->getLinkage()) {
if (!MF.getFunction()->hasInternalLinkage()) default: assert(0 && "Unknown linkage type!");
case Function::InternalLinkage: // Symbols default to internal.
SwitchSection(".text", F);
EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
break;
case Function::ExternalLinkage:
SwitchSection(".text", F);
EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
O << "\t.globl\t" << CurrentFnName << "\n"; O << "\t.globl\t" << CurrentFnName << "\n";
if (HasDotTypeDotSizeDirective) break;
O << "\t.type\t" << CurrentFnName << ", @function\n"; case Function::WeakLinkage:
case Function::LinkOnceLinkage:
if (forDarwin) {
SwitchSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions",
F);
O << "\t.weak_definition\t" << CurrentFnName << "\n";
} else {
EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
O << "\t.section\t.llvm.linkonce.t." << CurrentFnName
<< ",\"ax\",@progbits\n";
O << "\t.weak " << CurrentFnName << "\n";
}
break;
}
O << CurrentFnName << ":\n"; O << CurrentFnName << ":\n";
// Print out code for the function. // Print out code for the function.
@ -95,27 +115,24 @@ void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
return; return;
case MachineOperand::MO_GlobalAddress: { case MachineOperand::MO_GlobalAddress: {
bool isCallOp = Modifier && !strcmp(Modifier, "call"); bool isCallOp = Modifier && !strcmp(Modifier, "call");
// Darwin block shameless ripped from PowerPCAsmPrinter.cpp bool isMemOp = Modifier && !strcmp(Modifier, "mem");
// Darwin block shameless ripped from PPCAsmPrinter.cpp
if (forDarwin) { if (forDarwin) {
if (!isCallOp) O << '$'; if (!isMemOp && !isCallOp) O << '$';
GlobalValue *GV = MO.getGlobal(); GlobalValue *GV = MO.getGlobal();
std::string Name = Mang->getValueName(GV); std::string Name = Mang->getValueName(GV);
// Link-once, External, or Weakly-linked global variables need
// Dynamically-resolved functions need a stub for the function. Be // non-lazily-resolved stubs
// wary however not to output $stub for external functions whose addresses if (GV->isExternal() || GV->hasWeakLinkage() ||
// are taken. Those should be emitted as $non_lazy_ptr below. GV->hasLinkOnceLinkage()) {
Function *F = dyn_cast<Function>(GV); // Dynamically-resolved functions need a stub for the function.
if (F && isCallOp && F->isExternal()) { if (isCallOp && isa<Function>(GV) && cast<Function>(GV)->isExternal()) {
FnStubs.insert(Name); FnStubs.insert(Name);
O << "L" << Name << "$stub"; O << "L" << Name << "$stub";
} else if (GV->hasLinkOnceLinkage()) { } else {
// Link-once, External, or Weakly-linked global variables need GVStubs.insert(Name);
// non-lazily-resolved stubs O << "L" << Name << "$non_lazy_ptr";
LinkOnceStubs.insert(Name); }
O << "L" << Name << "$non_lazy_ptr";
} else if (GV->isExternal() || GV->hasWeakLinkage()) {
GVStubs.insert(Name);
O << "L" << Name << "$non_lazy_ptr";
} else { } else {
O << Mang->getValueName(GV); O << Mang->getValueName(GV);
} }
@ -126,7 +143,7 @@ void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
O << Offset; O << Offset;
return; return;
} }
if (!isCallOp) O << '$'; if (!isMemOp && !isCallOp) O << '$';
O << Mang->getValueName(MO.getGlobal()); O << Mang->getValueName(MO.getGlobal());
int Offset = MO.getOffset(); int Offset = MO.getOffset();
if (Offset > 0) if (Offset > 0)
@ -137,13 +154,14 @@ void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
} }
case MachineOperand::MO_ExternalSymbol: { case MachineOperand::MO_ExternalSymbol: {
bool isCallOp = Modifier && !strcmp(Modifier, "call"); bool isCallOp = Modifier && !strcmp(Modifier, "call");
bool isMemOp = Modifier && !strcmp(Modifier, "mem");
if (isCallOp && forDarwin) { if (isCallOp && forDarwin) {
std::string Name(GlobalPrefix); Name += MO.getSymbolName(); std::string Name(GlobalPrefix); Name += MO.getSymbolName();
FnStubs.insert(Name); FnStubs.insert(Name);
O << "L" << Name << "$stub"; O << "L" << Name << "$stub";
return; return;
} }
if (!isCallOp) O << '$'; if (!isMemOp && !isCallOp) O << '$';
O << GlobalPrefix << MO.getSymbolName(); O << GlobalPrefix << MO.getSymbolName();
return; return;
} }
@ -197,7 +215,7 @@ void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
} }
if (DispSpec.isGlobalAddress()) { if (DispSpec.isGlobalAddress()) {
printOperand(MI, Op+3, "call"); printOperand(MI, Op+3, "mem");
} else { } else {
int DispVal = DispSpec.getImmedValue(); int DispVal = DispSpec.getImmedValue();
if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))

View File

@ -80,113 +80,93 @@ bool X86SharedAsmPrinter::doFinalization(Module &M) {
const TargetData &TD = TM.getTargetData(); const TargetData &TD = TM.getTargetData();
// Print out module-level global variables here. // Print out module-level global variables here.
for (Module::const_global_iterator I = M.global_begin(), for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
E = M.global_end(); I != E; ++I) { I != E; ++I) {
if (!I->hasInitializer()) continue; // External global require no code if (!I->hasInitializer()) continue; // External global require no code
// Check to see if this is a special global used by LLVM, if so, emit it. // Check to see if this is a special global used by LLVM, if so, emit it.
if (I->hasAppendingLinkage() && EmitSpecialLLVMGlobal(I)) if (I->hasAppendingLinkage() && EmitSpecialLLVMGlobal(I))
continue; continue;
O << "\n\n";
std::string name = Mang->getValueName(I); std::string name = Mang->getValueName(I);
Constant *C = I->getInitializer(); Constant *C = I->getInitializer();
unsigned Size = TD.getTypeSize(C->getType()); unsigned Size = TD.getTypeSize(C->getType());
unsigned Align = getPreferredAlignmentLog(I); unsigned Align = getPreferredAlignmentLog(I);
switch (I->getLinkage()) { if (C->isNullValue() && /* FIXME: Verify correct */
default: assert(0 && "Unknown linkage type!"); (I->hasInternalLinkage() || I->hasWeakLinkage() ||
case GlobalValue::LinkOnceLinkage: I->hasLinkOnceLinkage())) {
case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak. if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
if (C->isNullValue()) { if (forDarwin) {
O << COMMDirective << name << "," << Size; SwitchSection(".data", I);
if (COMMDirectiveTakesAlignment) if (I->hasInternalLinkage())
O << "," << (1 << Align);
O << "\t\t" << CommentString << " " << I->getName() << "\n";
continue;
}
// Nonnull linkonce -> weak
O << "\t.weak " << name << "\n";
O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
SwitchSection("", I);
break;
case GlobalValue::InternalLinkage:
if (C->isNullValue()) {
if (LCOMMDirective) {
O << LCOMMDirective << name << "," << Size << "," << Align; O << LCOMMDirective << name << "," << Size << "," << Align;
continue; else
} else { O << COMMDirective << name << "," << Size;
SwitchSection(".bss", I); } else {
O << "\t.local " << name << "\n"; SwitchSection(".local", I);
O << COMMDirective << name << "," << Size; O << COMMDirective << name << "," << Size << "," << Align;
if (COMMDirectiveTakesAlignment) }
O << "," << (1 << Align); O << "\t\t" << CommentString << " '" << I->getName() << "'\n";
O << "\t\t# "; } else {
WriteAsOperand(O, I, true, true, &M); switch (I->getLinkage()) {
O << "\n"; case GlobalValue::LinkOnceLinkage:
continue; case GlobalValue::WeakLinkage:
} if (forDarwin) {
O << "\t.globl " << name << '\n'
<< "\t.weak_definition " << name << '\n';
SwitchSection(".section __DATA,__datacoal_nt,coalesced", I);
} else {
O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
O << "\t.weak " << name << "\n";
}
break;
case GlobalValue::AppendingLinkage:
// FIXME: appending linkage variables should go into a section of
// their name or something. For now, just emit them as external.
case GlobalValue::ExternalLinkage:
// If external or appending, declare as a global symbol
O << "\t.globl " << name << "\n";
// FALL THROUGH
case GlobalValue::InternalLinkage:
SwitchSection(".data", I);
break;
default:
assert(0 && "Unknown linkage type!");
} }
SwitchSection(".data", I);
break;
case GlobalValue::AppendingLinkage:
// FIXME: appending linkage variables should go into a section of
// their name or something. For now, just emit them as external.
case GlobalValue::ExternalLinkage:
SwitchSection(C->isNullValue() ? ".bss" : ".data", I);
// If external or appending, declare as a global symbol
O << "\t.globl " << name << "\n";
break;
}
EmitAlignment(Align); EmitAlignment(Align, I);
if (HasDotTypeDotSizeDirective) { O << name << ":\t\t\t\t" << CommentString << " '" << I->getName()
O << "\t.type " << name << ",@object\n"; << "'\n";
O << "\t.size " << name << "," << Size << "\n"; EmitGlobalConstant(C);
O << '\n';
} }
O << name << ":\t\t\t" << CommentString << ' ' << I->getName() << '\n';
EmitGlobalConstant(C);
} }
if (forDarwin) { if (forDarwin) {
SwitchSection("", 0); SwitchSection("", 0);
// Output stubs for external global variables
if (GVStubs.begin() != GVStubs.end())
O << "\t.non_lazy_symbol_pointer\n";
for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
i != e; ++i) {
O << "L" << *i << "$non_lazy_ptr:\n";
O << "\t.indirect_symbol " << *i << "\n";
O << "\t.long\t0\n";
}
// Output stubs for dynamically-linked functions // Output stubs for dynamically-linked functions
unsigned j = 1; unsigned j = 1;
for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end(); for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
i != e; ++i, ++j) { i != e; ++i, ++j) {
O << "\t.symbol_stub\n"; SwitchSection(".section __IMPORT,__jump_table,symbol_stubs,"
"self_modifying_code+pure_instructions,5", 0);
O << "L" << *i << "$stub:\n"; O << "L" << *i << "$stub:\n";
O << "\t.indirect_symbol " << *i << "\n"; O << "\t.indirect_symbol " << *i << "\n";
O << "\tjmp\t*L" << j << "$lz\n"; O << "\thlt ; hlt ; hlt ; hlt ; hlt\n";
O << "L" << *i << "$stub_binder:\n";
O << "\tpushl\t$L" << j << "$lz\n";
O << "\tjmp\tdyld_stub_binding_helper\n";
O << "\t.section __DATA, __la_sym_ptr3,lazy_symbol_pointers\n";
O << "L" << j << "$lz:\n";
O << "\t.indirect_symbol " << *i << "\n";
O << "\t.long\tL" << *i << "$stub_binder\n";
} }
O << "\n"; O << "\n";
// Output stubs for link-once variables // Output stubs for external and common global variables.
if (LinkOnceStubs.begin() != LinkOnceStubs.end()) if (GVStubs.begin() != GVStubs.end())
O << ".data\n.align 2\n"; SwitchSection(".section __IMPORT,__pointers,non_lazy_symbol_pointers", 0);
for (std::set<std::string>::iterator i = LinkOnceStubs.begin(), for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
e = LinkOnceStubs.end(); i != e; ++i) { i != e; ++i) {
O << "L" << *i << "$non_lazy_ptr:\n" O << "L" << *i << "$non_lazy_ptr:\n";
<< "\t.long\t" << *i << '\n'; O << "\t.indirect_symbol " << *i << "\n";
O << "\t.long\t0\n";
} }
} }

View File

@ -1876,11 +1876,13 @@ SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
// the GlobalAddress must be in the base or index register of the address, // the GlobalAddress must be in the base or index register of the address,
// not the GV offset field. // not the GV offset field.
if (getTargetMachine(). if (getTargetMachine().
getSubtarget<X86Subtarget>().getIndirectExternAndWeakGlobals() && getSubtarget<X86Subtarget>().getIndirectExternAndWeakGlobals()) {
(GV->hasWeakLinkage() || GV->isExternal())) if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()))
DAG.getTargetGlobalAddress(GV, getPointerTy()), Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
DAG.getSrcValue(NULL)); DAG.getTargetGlobalAddress(GV, getPointerTy()),
DAG.getSrcValue(NULL));
}
return Result; return Result;
} }
case ISD::VASTART: { case ISD::VASTART: {

View File

@ -109,7 +109,7 @@ void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
abort (); abort ();
return; return;
case MachineOperand::MO_GlobalAddress: { case MachineOperand::MO_GlobalAddress: {
if (!Modifier || strcmp(Modifier, "call")) if (!Modifier || strcmp(Modifier, "call") || strcmp(Modifier, "mem"))
O << "OFFSET "; O << "OFFSET ";
O << Mang->getValueName(MO.getGlobal()); O << Mang->getValueName(MO.getGlobal());
int Offset = MO.getOffset(); int Offset = MO.getOffset();
@ -161,7 +161,7 @@ void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
O << "["; O << "[";
bool NeedPlus = false; bool NeedPlus = false;
if (BaseReg.getReg()) { if (BaseReg.getReg()) {
printOp(BaseReg, "call"); printOp(BaseReg, "mem");
NeedPlus = true; NeedPlus = true;
} }
@ -176,7 +176,7 @@ void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
if (DispSpec.isGlobalAddress()) { if (DispSpec.isGlobalAddress()) {
if (NeedPlus) if (NeedPlus)
O << " + "; O << " + ";
printOp(DispSpec, "call"); printOp(DispSpec, "mem");
} else { } else {
int DispVal = DispSpec.getImmedValue(); int DispVal = DispSpec.getImmedValue();
if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) { if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {