- Target PIC style is no longer affected by relocation model.

- In x86-64 mode, symbols with external linkage (not just symbols which are
  defined externally) requires GOT indirect reference.
- Stylistic code clean up.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33345 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Evan Cheng 2007-01-18 22:27:12 +00:00
parent 93d9eb8263
commit ae19abc2cc
2 changed files with 49 additions and 49 deletions

View File

@ -189,6 +189,14 @@ bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
return false; return false;
} }
static inline bool printGOT(TargetMachine &TM, const X86Subtarget* ST) {
return ST->isPICStyleGOT() && TM.getRelocationModel() == Reloc::PIC_;
}
static inline bool printStub(TargetMachine &TM, const X86Subtarget* ST) {
return ST->isPICStyleStub() && TM.getRelocationModel() != Reloc::Static;
}
void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo, void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
const char *Modifier, bool NotRIPRel) { const char *Modifier, bool NotRIPRel) {
const MachineOperand &MO = MI->getOperand(OpNo); const MachineOperand &MO = MI->getOperand(OpNo);
@ -232,7 +240,7 @@ void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
O << "@GOTOFF"; O << "@GOTOFF";
} }
if (isMemOp && Subtarget->is64Bit() && !NotRIPRel) if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
O << "(%rip)"; O << "(%rip)";
return; return;
} }
@ -246,7 +254,7 @@ void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
if (Subtarget->isPICStyleStub()) if (Subtarget->isPICStyleStub())
O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber() O << "-\"" << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
<< "$pb\""; << "$pb\"";
if (Subtarget->isPICStyleGOT()) else if (Subtarget->isPICStyleGOT())
O << "@GOTOFF"; O << "@GOTOFF";
} }
@ -256,7 +264,7 @@ void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
else if (Offset < 0) else if (Offset < 0)
O << Offset; O << Offset;
if (isMemOp && Subtarget->is64Bit() && !NotRIPRel) if (isMemOp && Subtarget->isPICStyleRIPRel() && !NotRIPRel)
O << "(%rip)"; O << "(%rip)";
return; return;
} }
@ -267,17 +275,14 @@ void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
GlobalValue *GV = MO.getGlobal(); GlobalValue *GV = MO.getGlobal();
std::string Name = Mang->getValueName(GV); std::string Name = Mang->getValueName(GV);
bool isExt = (GV->isExternal() || GV->hasWeakLinkage() ||
GV->hasLinkOnceLinkage());
bool isHidden = GV->hasHiddenVisibility();
X86SharedAsmPrinter::decorateName(Name, GV); X86SharedAsmPrinter::decorateName(Name, GV);
if (Subtarget->isPICStyleStub()) { if (printStub(TM, Subtarget)) {
// Link-once, External, or Weakly-linked global variables need // Link-once, External, or Weakly-linked global variables need
// non-lazily-resolved stubs // non-lazily-resolved stubs
if (isExt) { if (GV->isExternal() ||
GV->hasWeakLinkage() ||
GV->hasLinkOnceLinkage()) {
// Dynamically-resolved functions need a stub for the function. // Dynamically-resolved functions need a stub for the function.
if (isCallOp && isa<Function>(GV)) { if (isCallOp && isa<Function>(GV)) {
FnStubs.insert(Name); FnStubs.insert(Name);
@ -287,9 +292,8 @@ void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
O << TAI->getPrivateGlobalPrefix() << Name << "$non_lazy_ptr"; O << TAI->getPrivateGlobalPrefix() << Name << "$non_lazy_ptr";
} }
} else { } else {
if (GV->hasDLLImportLinkage()) { if (GV->hasDLLImportLinkage())
O << "__imp_"; O << "__imp_";
}
O << Name; O << Name;
} }
@ -303,17 +307,16 @@ void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
O << Name; O << Name;
if (isCallOp && isa<Function>(GV)) { if (isCallOp && isa<Function>(GV)) {
if (Subtarget->isPICStyleGOT()) { if (printGOT(TM, Subtarget)) {
// Assemble call via PLT for non-local symbols // Assemble call via PLT for non-local symbols
if (!isHidden || GV->isExternal()) if (!GV->hasHiddenVisibility() || GV->isExternal())
O << "@PLT"; O << "@PLT";
} }
if (Subtarget->isTargetCygMing() && GV->isExternal()) { if (Subtarget->isTargetCygMing() && GV->isExternal())
// Save function name for later type emission // Save function name for later type emission
FnStubs.insert(Name); FnStubs.insert(Name);
} }
} }
}
if (GV->hasExternalWeakLinkage()) if (GV->hasExternalWeakLinkage())
ExtWeakSymbols.insert(GV); ExtWeakSymbols.insert(GV);
@ -325,20 +328,23 @@ void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
O << Offset; O << Offset;
if (isMemOp) { if (isMemOp) {
if (Subtarget->isPICStyleGOT()) { if (printGOT(TM, Subtarget)) {
if (Subtarget->GVRequiresExtraLoad(GV, TM, false)) if (Subtarget->GVRequiresExtraLoad(GV, TM, false))
O << "@GOT"; O << "@GOT";
else else
O << "@GOTOFF"; O << "@GOTOFF";
} else } else if (Subtarget->isPICStyleRIPRel() && !NotRIPRel) {
if (isExt && Subtarget->isPICStyleRIPRel()) if ((GV->hasExternalLinkage() ||
O << "@GOTPCREL(%rip)"; GV->hasWeakLinkage() ||
else if (Subtarget->is64Bit() && !NotRIPRel) GV->hasLinkOnceLinkage()) &&
TM.getRelocationModel() != Reloc::Static)
O << "@GOTPCREL";
// Use rip when possible to reduce code size, except when // Use rip when possible to reduce code size, except when
// index or base register are also part of the address. e.g. // index or base register are also part of the address. e.g.
// foo(%rip)(%rcx,%rax,4) is not legal // foo(%rip)(%rcx,%rax,4) is not legal
O << "(%rip)"; O << "(%rip)";
} }
}
return; return;
} }
@ -346,7 +352,7 @@ void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
bool isCallOp = Modifier && !strcmp(Modifier, "call"); bool isCallOp = Modifier && !strcmp(Modifier, "call");
std::string Name(TAI->getGlobalPrefix()); std::string Name(TAI->getGlobalPrefix());
Name += MO.getSymbolName(); Name += MO.getSymbolName();
if (isCallOp && Subtarget->isPICStyleStub()) { if (isCallOp && printStub(TM, Subtarget)) {
FnStubs.insert(Name); FnStubs.insert(Name);
O << TAI->getPrivateGlobalPrefix() << Name << "$stub"; O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
return; return;
@ -354,25 +360,25 @@ void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
if (!isCallOp) O << '$'; if (!isCallOp) O << '$';
O << Name; O << Name;
if (Subtarget->isPICStyleGOT()) { if (printGOT(TM, Subtarget)) {
std::string GOTName(TAI->getGlobalPrefix()); std::string GOTName(TAI->getGlobalPrefix());
GOTName+="_GLOBAL_OFFSET_TABLE_"; GOTName+="_GLOBAL_OFFSET_TABLE_";
if (Name == GOTName) if (Name == GOTName)
// Really hack! Emit extra offset to PC during printing GOT offset to // HACK! Emit extra offset to PC during printing GOT offset to
// compensate size of popl instruction. The resulting code should look // compensate for the size of popl instruction. The resulting code
// like: // should look like:
// call .piclabel // call .piclabel
// piclabel: // piclabel:
// popl %some_register // popl %some_register
// addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register // addl $_GLOBAL_ADDRESS_TABLE_ + [.-piclabel], %some_register
O << " + [.-" O << " + [.-"
<< computePICLabel(getFunctionNumber(), TAI, Subtarget) << "]"; << computePICLabel(getFunctionNumber(), TAI, Subtarget) << "]";
if (isCallOp)
O << "@PLT";
} }
if (isCallOp && Subtarget->isPICStyleGOT()) if (!isCallOp && Subtarget->isPICStyleRIPRel())
O << "@PLT";
if (!isCallOp && Subtarget->is64Bit())
O << "(%rip)"; O << "(%rip)";
return; return;

View File

@ -128,24 +128,18 @@ X86TargetMachine::X86TargetMachine(const Module &M, const std::string &FS, bool
setCodeModel(CodeModel::Small); setCodeModel(CodeModel::Small);
} }
if (getRelocationModel() == Reloc::PIC_) { if (Subtarget.isTargetCygMing())
if (Subtarget.isTargetDarwin()) { Subtarget.setPICStyle(PICStyle::WinPIC);
else if (Subtarget.isTargetDarwin())
if (Subtarget.is64Bit()) if (Subtarget.is64Bit())
Subtarget.setPICStyle(PICStyle::RIPRel); Subtarget.setPICStyle(PICStyle::RIPRel);
else else
Subtarget.setPICStyle(PICStyle::Stub); Subtarget.setPICStyle(PICStyle::Stub);
} else if (Subtarget.isTargetELF()) else if (Subtarget.isTargetELF())
if (Subtarget.is64Bit())
Subtarget.setPICStyle(PICStyle::RIPRel);
else
Subtarget.setPICStyle(PICStyle::GOT); Subtarget.setPICStyle(PICStyle::GOT);
else
assert(0 && "Don't know how to generate PIC code for this target!");
} else if (getRelocationModel() == Reloc::DynamicNoPIC) {
if (Subtarget.isTargetDarwin())
Subtarget.setPICStyle(PICStyle::Stub);
else if (Subtarget.isTargetCygMing())
Subtarget.setPICStyle(PICStyle::WinPIC);
else
assert(0 && "Don't know how to generate PIC code for this target!");
}
} }
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//