From 0a6040063fd9419041a81ada8f75b389381eb6b9 Mon Sep 17 00:00:00 2001 From: Bruno Cardoso Lopes Date: Tue, 9 Oct 2007 03:01:19 +0000 Subject: [PATCH] Position Independent Code (PIC) support [2] - Added a function to hold the stack location where GP must be stored during LowerCALL - AsmPrinter now emits directives based on relocation type - PIC_ set to default relocation type (same as GCC) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@42779 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Target/Mips/MipsAsmPrinter.cpp | 28 ++++++++++++++++++--------- lib/Target/Mips/MipsMachineFunction.h | 8 ++++++++ lib/Target/Mips/MipsTargetMachine.cpp | 6 +++++- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/lib/Target/Mips/MipsAsmPrinter.cpp b/lib/Target/Mips/MipsAsmPrinter.cpp index 254cc563c9c..cd5c1a3d657 100644 --- a/lib/Target/Mips/MipsAsmPrinter.cpp +++ b/lib/Target/Mips/MipsAsmPrinter.cpp @@ -67,7 +67,7 @@ namespace { void printHex32(unsigned int Value); void emitFunctionStart(MachineFunction &MF); - void emitFunctionEnd(); + void emitFunctionEnd(MachineFunction &MF); void emitFrameDirective(MachineFunction &MF); void emitMaskDirective(MachineFunction &MF); void emitFMaskDirective(MachineFunction &MF); @@ -209,10 +209,12 @@ getSavedRegsBitmask(bool isFloat, MachineFunction &MF) Bitmask |= (1 << MipsRegisterInfo::getRegisterNumbering(CSI[i].getReg())); if (RI.hasFP(MF)) - Bitmask |= (1 << MipsRegisterInfo::getRegisterNumbering(RI.getFrameRegister(MF))); + Bitmask |= (1 << MipsRegisterInfo:: + getRegisterNumbering(RI.getFrameRegister(MF))); if (MF.getFrameInfo()->hasCalls()) - Bitmask |= (1 << MipsRegisterInfo::getRegisterNumbering(RI.getRARegister())); + Bitmask |= (1 << MipsRegisterInfo:: + getRegisterNumbering(RI.getRARegister())); return Bitmask; } @@ -247,16 +249,24 @@ emitFunctionStart(MachineFunction &MF) emitFrameDirective(MF); emitMaskDirective(MF); emitFMaskDirective(MF); - emitSetDirective(NOREORDER); - emitSetDirective(NOMACRO); + + if (MF.getTarget().getRelocationModel() == Reloc::Static) { + emitSetDirective(NOREORDER); + emitSetDirective(NOMACRO); + } + O << "\n"; } /// Emit the directives used by GAS on the end of functions void MipsAsmPrinter:: -emitFunctionEnd() { - emitSetDirective(MACRO); - emitSetDirective(REORDER); +emitFunctionEnd(MachineFunction &MF) +{ + if (MF.getTarget().getRelocationModel() == Reloc::Static) { + emitSetDirective(MACRO); + emitSetDirective(REORDER); + } + O << "\t.end\t" << CurrentFnName << "\n"; } @@ -298,7 +308,7 @@ runOnMachineFunction(MachineFunction &MF) } // Emit function end directives - emitFunctionEnd(); + emitFunctionEnd(MF); // We didn't modify anything. return false; diff --git a/lib/Target/Mips/MipsMachineFunction.h b/lib/Target/Mips/MipsMachineFunction.h index 0f9a1e0e6a3..2515aa0438a 100644 --- a/lib/Target/Mips/MipsMachineFunction.h +++ b/lib/Target/Mips/MipsMachineFunction.h @@ -33,6 +33,11 @@ private: /// the Return Address must be saved int RAStackOffset; + /// When PIC is used the GP must be saved on the stack + /// on the function prologue, so a reference to its stack + /// location must be kept. + int GPStackOffset; + /// MipsFIHolder - Holds a FrameIndex and it's Stack Pointer Offset struct MipsFIHolder { @@ -69,6 +74,9 @@ public: int getRAStackOffset() const { return RAStackOffset; } void setRAStackOffset(int Off) { RAStackOffset = Off; } + int getGPStackOffset() const { return GPStackOffset; } + void setGPStackOffset(int Off) { GPStackOffset = Off; } + int getTopSavedRegOffset() const { return (RAStackOffset > FPStackOffset) ? (RAStackOffset) : (FPStackOffset); diff --git a/lib/Target/Mips/MipsTargetMachine.cpp b/lib/Target/Mips/MipsTargetMachine.cpp index 372e267d462..64516594f66 100644 --- a/lib/Target/Mips/MipsTargetMachine.cpp +++ b/lib/Target/Mips/MipsTargetMachine.cpp @@ -39,7 +39,11 @@ MipsTargetMachine:: MipsTargetMachine(const Module &M, const std::string &FS): Subtarget(*this, M, FS), DataLayout("E-p:32:32:32"), InstrInfo(*this), FrameInfo(TargetFrameInfo::StackGrowsUp, 8, 0), - TLInfo(*this) {} + TLInfo(*this) +{ + if (getRelocationModel() != Reloc::Static) + setRelocationModel(Reloc::PIC_); +} // return 0 and must specify -march to gen MIPS code. unsigned MipsTargetMachine::