X86 / Cygwin asm / alignment fixes.

Patch contributed by Anton Korobeynikov!

llvm-svn: 28480
This commit is contained in:
Evan Cheng 2006-05-25 21:59:08 +00:00
parent d0754ad9e6
commit 2554e3d9ba
4 changed files with 42 additions and 30 deletions

View File

@ -26,11 +26,9 @@ using namespace llvm;
/// method to print assembly for each instruction. /// method to print assembly for each instruction.
/// ///
bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) { bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
// if (forDarwin) { // Let PassManager know we need debug information and relay
// Let PassManager know we need debug information and relay // the MachineDebugInfo address on to DwarfWriter.
// the MachineDebugInfo address on to DwarfWriter. DW.SetDebugInfo(&getAnalysis<MachineDebugInfo>());
DW.SetDebugInfo(&getAnalysis<MachineDebugInfo>());
// }
SetupMachineFunction(MF); SetupMachineFunction(MF);
O << "\n\n"; O << "\n\n";
@ -56,11 +54,17 @@ bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
break; break;
case Function::WeakLinkage: case Function::WeakLinkage:
case Function::LinkOnceLinkage: case Function::LinkOnceLinkage:
if (forDarwin) { if (Subtarget->TargetType == X86Subtarget::isDarwin) {
SwitchToTextSection( SwitchToTextSection(
".section __TEXT,__textcoal_nt,coalesced,pure_instructions", F); ".section __TEXT,__textcoal_nt,coalesced,pure_instructions", F);
O << "\t.globl\t" << CurrentFnName << "\n"; O << "\t.globl\t" << CurrentFnName << "\n";
O << "\t.weak_definition\t" << CurrentFnName << "\n"; O << "\t.weak_definition\t" << CurrentFnName << "\n";
} else if (Subtarget->TargetType == X86Subtarget::isCygwin) {
EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
O << "\t.section\t.llvm.linkonce.t." << CurrentFnName
<< ",\"ax\"\n";
SwitchToTextSection("", F);
O << "\t.weak " << CurrentFnName << "\n";
} else { } else {
EmitAlignment(4, F); // FIXME: This should be parameterized somewhere. EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
O << "\t.section\t.llvm.linkonce.t." << CurrentFnName O << "\t.section\t.llvm.linkonce.t." << CurrentFnName
@ -72,7 +76,7 @@ bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
} }
O << CurrentFnName << ":\n"; O << CurrentFnName << ":\n";
if (forDarwin) { if (Subtarget->TargetType == X86Subtarget::isDarwin) {
// Emit pre-function debug information. // Emit pre-function debug information.
DW.BeginFunction(&MF); DW.BeginFunction(&MF);
} }
@ -95,7 +99,7 @@ bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
if (HasDotTypeDotSizeDirective) if (HasDotTypeDotSizeDirective)
O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n"; O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
if (forDarwin) { if (Subtarget->TargetType == X86Subtarget::isDarwin) {
// Emit post-function debug information. // Emit post-function debug information.
DW.EndFunction(); DW.EndFunction();
} }
@ -145,7 +149,8 @@ void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
if (!isMemOp) O << '$'; if (!isMemOp) O << '$';
O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_" O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
<< MO.getConstantPoolIndex(); << MO.getConstantPoolIndex();
if (forDarwin && TM.getRelocationModel() == Reloc::PIC) if (Subtarget->TargetType == X86Subtarget::isDarwin &&
TM.getRelocationModel() == Reloc::PIC)
O << "-\"L" << getFunctionNumber() << "$pb\""; O << "-\"L" << getFunctionNumber() << "$pb\"";
int Offset = MO.getOffset(); int Offset = MO.getOffset();
if (Offset > 0) if (Offset > 0)
@ -159,7 +164,8 @@ void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
bool isMemOp = Modifier && !strcmp(Modifier, "mem"); bool isMemOp = Modifier && !strcmp(Modifier, "mem");
if (!isMemOp && !isCallOp) O << '$'; if (!isMemOp && !isCallOp) O << '$';
// Darwin block shameless ripped from PPCAsmPrinter.cpp // Darwin block shameless ripped from PPCAsmPrinter.cpp
if (forDarwin && TM.getRelocationModel() != Reloc::Static) { if (Subtarget->TargetType == X86Subtarget::isDarwin &&
TM.getRelocationModel() != Reloc::Static) {
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 // Link-once, External, or Weakly-linked global variables need
@ -190,7 +196,9 @@ 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");
if (isCallOp && forDarwin && TM.getRelocationModel() != Reloc::Static) { if (isCallOp &&
Subtarget->TargetType == X86Subtarget::isDarwin &&
TM.getRelocationModel() != Reloc::Static) {
std::string Name(GlobalPrefix); std::string Name(GlobalPrefix);
Name += MO.getSymbolName(); Name += MO.getSymbolName();
FnStubs.insert(Name); FnStubs.insert(Name);
@ -332,7 +340,7 @@ bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) { void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
++EmittedInsts; ++EmittedInsts;
// This works around some Darwin assembler bugs. // This works around some Darwin assembler bugs.
if (forDarwin) { if (Subtarget->TargetType == X86Subtarget::isDarwin) {
switch (MI->getOpcode()) { switch (MI->getOpcode()) {
case X86::REP_MOVSB: case X86::REP_MOVSB:
O << "rep/movsb (%esi),(%edi)\n"; O << "rep/movsb (%esi),(%edi)\n";

View File

@ -46,9 +46,6 @@ AsmWriterFlavor("x86-asm-syntax",
/// doInitialization /// doInitialization
bool X86SharedAsmPrinter::doInitialization(Module &M) { bool X86SharedAsmPrinter::doInitialization(Module &M) {
const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
forDarwin = false;
PrivateGlobalPrefix = ".L"; PrivateGlobalPrefix = ".L";
DefaultTextSection = ".text"; DefaultTextSection = ".text";
DefaultDataSection = ".data"; DefaultDataSection = ".data";
@ -65,7 +62,6 @@ bool X86SharedAsmPrinter::doInitialization(Module &M) {
LCOMMDirective = "\t.lcomm\t"; LCOMMDirective = "\t.lcomm\t";
COMMDirectiveTakesAlignment = false; COMMDirectiveTakesAlignment = false;
HasDotTypeDotSizeDirective = false; HasDotTypeDotSizeDirective = false;
forDarwin = true;
StaticCtorsSection = ".mod_init_func"; StaticCtorsSection = ".mod_init_func";
StaticDtorsSection = ".mod_term_func"; StaticDtorsSection = ".mod_term_func";
InlineAsmStart = "# InlineAsm Start"; InlineAsmStart = "# InlineAsm Start";
@ -75,6 +71,8 @@ bool X86SharedAsmPrinter::doInitialization(Module &M) {
GlobalPrefix = "_"; GlobalPrefix = "_";
COMMDirectiveTakesAlignment = false; COMMDirectiveTakesAlignment = false;
HasDotTypeDotSizeDirective = false; HasDotTypeDotSizeDirective = false;
StaticCtorsSection = "\t.section .ctors,\"aw\"";
StaticDtorsSection = "\t.section .dtors,\"aw\"";
break; break;
case X86Subtarget::isWindows: case X86Subtarget::isWindows:
GlobalPrefix = "_"; GlobalPrefix = "_";
@ -83,7 +81,7 @@ bool X86SharedAsmPrinter::doInitialization(Module &M) {
default: break; default: break;
} }
if (forDarwin) { if (Subtarget->TargetType == X86Subtarget::isDarwin) {
// Emit initial debug information. // Emit initial debug information.
DW.BeginModule(&M); DW.BeginModule(&M);
} }
@ -114,7 +112,8 @@ bool X86SharedAsmPrinter::doFinalization(Module &M) {
if (C->isNullValue() && /* FIXME: Verify correct */ if (C->isNullValue() && /* FIXME: Verify correct */
(I->hasInternalLinkage() || I->hasWeakLinkage() || (I->hasInternalLinkage() || I->hasWeakLinkage() ||
I->hasLinkOnceLinkage() || I->hasLinkOnceLinkage() ||
(forDarwin && I->hasExternalLinkage() && !I->hasSection()))) { (Subtarget->TargetType == X86Subtarget::isDarwin &&
I->hasExternalLinkage() && !I->hasSection()))) {
if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it. if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
if (I->hasExternalLinkage()) { if (I->hasExternalLinkage()) {
O << "\t.globl\t" << name << "\n"; O << "\t.globl\t" << name << "\n";
@ -125,13 +124,15 @@ bool X86SharedAsmPrinter::doFinalization(Module &M) {
if (LCOMMDirective != NULL) { if (LCOMMDirective != NULL) {
if (I->hasInternalLinkage()) { if (I->hasInternalLinkage()) {
O << LCOMMDirective << name << "," << Size; O << LCOMMDirective << name << "," << Size;
if (forDarwin) if (Subtarget->TargetType == X86Subtarget::isDarwin)
O << "," << (AlignmentIsInBytes ? (1 << Align) : Align); O << "," << (AlignmentIsInBytes ? (1 << Align) : Align);
} else } else
O << COMMDirective << name << "," << Size; O << COMMDirective << name << "," << Size;
} else { } else {
if (I->hasInternalLinkage()) if (Subtarget->TargetType == X86Subtarget::isCygwin) {
O << "\t.local\t" << name << "\n"; if (I->hasInternalLinkage())
O << "\t.local\t" << name << "\n";
}
O << COMMDirective << name << "," << Size; O << COMMDirective << name << "," << Size;
if (COMMDirectiveTakesAlignment) if (COMMDirectiveTakesAlignment)
O << "," << (AlignmentIsInBytes ? (1 << Align) : Align); O << "," << (AlignmentIsInBytes ? (1 << Align) : Align);
@ -142,13 +143,16 @@ bool X86SharedAsmPrinter::doFinalization(Module &M) {
switch (I->getLinkage()) { switch (I->getLinkage()) {
case GlobalValue::LinkOnceLinkage: case GlobalValue::LinkOnceLinkage:
case GlobalValue::WeakLinkage: case GlobalValue::WeakLinkage:
if (forDarwin) { if (Subtarget->TargetType == X86Subtarget::isDarwin) {
O << "\t.globl " << name << "\n" O << "\t.globl " << name << "\n"
<< "\t.weak_definition " << name << "\n"; << "\t.weak_definition " << name << "\n";
SwitchToDataSection(".section __DATA,__datacoal_nt,coalesced", I); SwitchToDataSection(".section __DATA,__datacoal_nt,coalesced", I);
} else if (Subtarget->TargetType == X86Subtarget::isCygwin) {
O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\"\n"
<< "\t.weak " << name << "\n";
} else { } else {
O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n"; O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n"
O << "\t.weak " << name << "\n"; << "\t.weak " << name << "\n";
} }
break; break;
case GlobalValue::AppendingLinkage: case GlobalValue::AppendingLinkage:
@ -176,7 +180,7 @@ bool X86SharedAsmPrinter::doFinalization(Module &M) {
} }
} }
if (forDarwin) { if (Subtarget->TargetType == X86Subtarget::isDarwin) {
SwitchToDataSection("", 0); SwitchToDataSection("", 0);
// Output stubs for dynamically-linked functions // Output stubs for dynamically-linked functions

View File

@ -57,7 +57,9 @@ struct X86SharedAsmPrinter : public AsmPrinter {
X86DwarfWriter DW; X86DwarfWriter DW;
X86SharedAsmPrinter(std::ostream &O, X86TargetMachine &TM) X86SharedAsmPrinter(std::ostream &O, X86TargetMachine &TM)
: AsmPrinter(O, TM), DW(O, this), forDarwin(false) { } : AsmPrinter(O, TM), DW(O, this) {
Subtarget = &TM.getSubtarget<X86Subtarget>();
}
bool doInitialization(Module &M); bool doInitialization(Module &M);
bool doFinalization(Module &M); bool doFinalization(Module &M);
@ -68,11 +70,9 @@ struct X86SharedAsmPrinter : public AsmPrinter {
MachineFunctionPass::getAnalysisUsage(AU); MachineFunctionPass::getAnalysisUsage(AU);
} }
bool forDarwin; // FIXME: eliminate.
const char *DefaultTextSection; // "_text" for MASM, ".text" for others. const char *DefaultTextSection; // "_text" for MASM, ".text" for others.
const char *DefaultDataSection; // "_data" for MASM, ".data" for others. const char *DefaultDataSection; // "_data" for MASM, ".data" for others.
const X86Subtarget *Subtarget;
// Necessary for Darwin to print out the apprioriate types of linker stubs // Necessary for Darwin to print out the apprioriate types of linker stubs
std::set<std::string> FnStubs, GVStubs, LinkOnceStubs; std::set<std::string> FnStubs, GVStubs, LinkOnceStubs;

View File

@ -182,6 +182,6 @@ X86Subtarget::X86Subtarget(const Module &M, const std::string &FS) {
#endif #endif
} }
if (TargetType == isDarwin) if (TargetType == isDarwin || TargetType == isCygwin)
stackAlignment = 16; stackAlignment = 16;
} }