Chris Lattner | b46443a | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 1 | //===-- PPCAsmBackend.cpp - PPC Assembler Backend -------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Evan Cheng | 78c10ee | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 10 | #include "llvm/MC/MCAsmBackend.h" |
Evan Cheng | 966aeb5 | 2011-07-25 19:53:23 +0000 | [diff] [blame] | 11 | #include "MCTargetDesc/PPCMCTargetDesc.h" |
| 12 | #include "MCTargetDesc/PPCFixupKinds.h" |
Roman Divacky | 2c0d69f | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCELFObjectWriter.h" |
Daniel Dunbar | aa4b7dd | 2010-12-16 16:08:33 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCMachObjectWriter.h" |
Chris Lattner | b46443a | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCSectionMachO.h" |
Chris Lattner | b46443a | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCObjectWriter.h" |
Jim Grosbach | ba8297e | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCValue.h" |
Daniel Dunbar | 36d76a8 | 2010-11-27 04:38:36 +0000 | [diff] [blame] | 18 | #include "llvm/Object/MachOFormat.h" |
Roman Divacky | 2c0d69f | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ELF.h" |
| 20 | #include "llvm/Support/ErrorHandling.h" |
Evan Cheng | 3e74d6f | 2011-08-24 18:08:43 +0000 | [diff] [blame^] | 21 | #include "llvm/Support/TargetRegistry.h" |
Chris Lattner | b46443a | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
Roman Divacky | 2c0d69f | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 24 | static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) { |
| 25 | switch (Kind) { |
| 26 | default: |
| 27 | llvm_unreachable("Unknown fixup kind!"); |
| 28 | case FK_Data_1: |
| 29 | case FK_Data_2: |
| 30 | case FK_Data_4: |
| 31 | return Value; |
| 32 | case PPC::fixup_ppc_brcond14: |
| 33 | return Value & 0x3ffc; |
| 34 | case PPC::fixup_ppc_br24: |
| 35 | return Value & 0x3fffffc; |
| 36 | #if 0 |
| 37 | case PPC::fixup_ppc_hi16: |
| 38 | return (Value >> 16) & 0xffff; |
| 39 | #endif |
| 40 | case PPC::fixup_ppc_ha16: |
| 41 | return ((Value >> 16) + ((Value & 0x8000) ? 1 : 0)) & 0xffff; |
| 42 | case PPC::fixup_ppc_lo16: |
| 43 | return Value & 0xffff; |
| 44 | } |
| 45 | } |
| 46 | |
Chris Lattner | b46443a | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 47 | namespace { |
Daniel Dunbar | ae5abd5 | 2010-12-16 16:09:19 +0000 | [diff] [blame] | 48 | class PPCMachObjectWriter : public MCMachObjectTargetWriter { |
Daniel Dunbar | 5d05d97 | 2010-12-16 17:21:02 +0000 | [diff] [blame] | 49 | public: |
| 50 | PPCMachObjectWriter(bool Is64Bit, uint32_t CPUType, |
| 51 | uint32_t CPUSubtype) |
| 52 | : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype) {} |
Jim Grosbach | ba8297e | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 53 | |
| 54 | void RecordRelocation(MachObjectWriter *Writer, |
| 55 | const MCAssembler &Asm, const MCAsmLayout &Layout, |
| 56 | const MCFragment *Fragment, const MCFixup &Fixup, |
| 57 | MCValue Target, uint64_t &FixedValue) {} |
Daniel Dunbar | ae5abd5 | 2010-12-16 16:09:19 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
Roman Divacky | 2c0d69f | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 60 | class PPCELFObjectWriter : public MCELFObjectTargetWriter { |
| 61 | public: |
| 62 | PPCELFObjectWriter(bool Is64Bit, Triple::OSType OSType, uint16_t EMachine, |
| 63 | bool HasRelocationAddend, bool isLittleEndian) |
| 64 | : MCELFObjectTargetWriter(Is64Bit, OSType, EMachine, HasRelocationAddend) {} |
| 65 | }; |
| 66 | |
Evan Cheng | 78c10ee | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 67 | class PPCAsmBackend : public MCAsmBackend { |
Daniel Dunbar | 297ed28 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 68 | const Target &TheTarget; |
| 69 | public: |
Evan Cheng | 78c10ee | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 70 | PPCAsmBackend(const Target &T) : MCAsmBackend(), TheTarget(T) {} |
Daniel Dunbar | 2761fc4 | 2010-12-16 03:20:06 +0000 | [diff] [blame] | 71 | |
Daniel Dunbar | 297ed28 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 72 | unsigned getNumFixupKinds() const { return PPC::NumTargetFixupKinds; } |
| 73 | |
| 74 | const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const { |
| 75 | const static MCFixupKindInfo Infos[PPC::NumTargetFixupKinds] = { |
| 76 | // name offset bits flags |
| 77 | { "fixup_ppc_br24", 6, 24, MCFixupKindInfo::FKF_IsPCRel }, |
| 78 | { "fixup_ppc_brcond14", 16, 14, MCFixupKindInfo::FKF_IsPCRel }, |
| 79 | { "fixup_ppc_lo16", 16, 16, 0 }, |
| 80 | { "fixup_ppc_ha16", 16, 16, 0 }, |
| 81 | { "fixup_ppc_lo14", 16, 14, 0 } |
| 82 | }; |
| 83 | |
| 84 | if (Kind < FirstTargetFixupKind) |
Evan Cheng | 78c10ee | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 85 | return MCAsmBackend::getFixupKindInfo(Kind); |
Daniel Dunbar | 297ed28 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 86 | |
| 87 | assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() && |
| 88 | "Invalid kind!"); |
| 89 | return Infos[Kind - FirstTargetFixupKind]; |
| 90 | } |
| 91 | |
| 92 | bool MayNeedRelaxation(const MCInst &Inst) const { |
| 93 | // FIXME. |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | void RelaxInstruction(const MCInst &Inst, MCInst &Res) const { |
| 98 | // FIXME. |
| 99 | assert(0 && "RelaxInstruction() unimplemented"); |
| 100 | } |
| 101 | |
| 102 | bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const { |
| 103 | // FIXME: Zero fill for now. That's not right, but at least will get the |
| 104 | // section size right. |
| 105 | for (uint64_t i = 0; i != Count; ++i) |
| 106 | OW->Write8(0); |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | unsigned getPointerSize() const { |
| 111 | StringRef Name = TheTarget.getName(); |
| 112 | if (Name == "ppc64") return 8; |
| 113 | assert(Name == "ppc32" && "Unknown target name!"); |
| 114 | return 4; |
| 115 | } |
| 116 | }; |
Chris Lattner | b46443a | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 117 | } // end anonymous namespace |
| 118 | |
| 119 | |
| 120 | // FIXME: This should be in a separate file. |
| 121 | namespace { |
| 122 | class DarwinPPCAsmBackend : public PPCAsmBackend { |
Chris Lattner | b46443a | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 123 | public: |
Daniel Dunbar | 7b62afa | 2010-12-17 02:06:08 +0000 | [diff] [blame] | 124 | DarwinPPCAsmBackend(const Target &T) : PPCAsmBackend(T) { } |
Chris Lattner | b46443a | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 125 | |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 126 | void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize, |
Chris Lattner | b46443a | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 127 | uint64_t Value) const { |
| 128 | assert(0 && "UNIMP"); |
| 129 | } |
| 130 | |
Chris Lattner | b46443a | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 131 | MCObjectWriter *createObjectWriter(raw_ostream &OS) const { |
| 132 | bool is64 = getPointerSize() == 8; |
Daniel Dunbar | 5d05d97 | 2010-12-16 17:21:02 +0000 | [diff] [blame] | 133 | return createMachObjectWriter(new PPCMachObjectWriter( |
| 134 | /*Is64Bit=*/is64, |
| 135 | (is64 ? object::mach::CTM_PowerPC64 : |
| 136 | object::mach::CTM_PowerPC), |
| 137 | object::mach::CSPPC_ALL), |
| 138 | OS, /*IsLittleEndian=*/false); |
Chris Lattner | b46443a | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | virtual bool doesSectionRequireSymbols(const MCSection &Section) const { |
| 142 | return false; |
| 143 | } |
| 144 | }; |
Roman Divacky | 2c0d69f | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 145 | |
| 146 | class ELFPPCAsmBackend : public PPCAsmBackend { |
| 147 | Triple::OSType OSType; |
| 148 | public: |
| 149 | ELFPPCAsmBackend(const Target &T, Triple::OSType OSType) : |
| 150 | PPCAsmBackend(T), OSType(OSType) { } |
| 151 | |
| 152 | void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize, |
| 153 | uint64_t Value) const { |
| 154 | Value = adjustFixupValue(Fixup.getKind(), Value); |
| 155 | if (!Value) return; // Doesn't change encoding. |
| 156 | |
| 157 | unsigned Offset = Fixup.getOffset(); |
| 158 | |
| 159 | // For each byte of the fragment that the fixup touches, mask in the bits from |
| 160 | // the fixup value. The Value has been "split up" into the appropriate |
| 161 | // bitfields above. |
| 162 | for (unsigned i = 0; i != 4; ++i) |
| 163 | Data[Offset + i] |= uint8_t((Value >> ((4 - i - 1)*8)) & 0xff); |
| 164 | } |
| 165 | |
| 166 | MCObjectWriter *createObjectWriter(raw_ostream &OS) const { |
| 167 | bool is64 = getPointerSize() == 8; |
| 168 | return createELFObjectWriter(new PPCELFObjectWriter( |
| 169 | /*Is64Bit=*/is64, |
| 170 | OSType, |
| 171 | is64 ? ELF::EM_PPC64 : ELF::EM_PPC, |
| 172 | /*addend*/ true, /*isLittleEndian*/ false), |
| 173 | OS, /*IsLittleEndian=*/false); |
| 174 | } |
| 175 | |
| 176 | virtual bool doesSectionRequireSymbols(const MCSection &Section) const { |
| 177 | return false; |
| 178 | } |
| 179 | }; |
| 180 | |
Chris Lattner | b46443a | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 181 | } // end anonymous namespace |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
Evan Cheng | 78c10ee | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 186 | MCAsmBackend *llvm::createPPCAsmBackend(const Target &T, StringRef TT) { |
Daniel Dunbar | 912225e | 2011-04-19 21:14:45 +0000 | [diff] [blame] | 187 | if (Triple(TT).isOSDarwin()) |
Chris Lattner | b46443a | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 188 | return new DarwinPPCAsmBackend(T); |
Daniel Dunbar | 912225e | 2011-04-19 21:14:45 +0000 | [diff] [blame] | 189 | |
Roman Divacky | 2c0d69f | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 190 | return new ELFPPCAsmBackend(T, Triple(TT).getOS()); |
Chris Lattner | b46443a | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 191 | } |