Chris Lattner | aac9fa7 | 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 | 61d4a20 | 2011-07-25 19:53:23 +0000 | [diff] [blame] | 10 | #include "MCTargetDesc/PPCMCTargetDesc.h" |
| 11 | #include "MCTargetDesc/PPCFixupKinds.h" |
Craig Topper | b25fda9 | 2012-03-17 18:46:09 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCAsmBackend.h" |
Roman Divacky | 038c1a1 | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCELFObjectWriter.h" |
Craig Topper | 6e80c28 | 2012-03-26 06:58:25 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCFixupKindInfo.h" |
Daniel Dunbar | 73b8713 | 2010-12-16 16:08:33 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCMachObjectWriter.h" |
Chris Lattner | aac9fa7 | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCObjectWriter.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCSectionMachO.h" |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCValue.h" |
Daniel Dunbar | a5f50c1 | 2010-11-27 04:38:36 +0000 | [diff] [blame] | 19 | #include "llvm/Object/MachOFormat.h" |
Roman Divacky | 038c1a1 | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ELF.h" |
| 21 | #include "llvm/Support/ErrorHandling.h" |
Evan Cheng | 2bb4035 | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 22 | #include "llvm/Support/TargetRegistry.h" |
Chris Lattner | aac9fa7 | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
Ulrich Weigand | 56f5b28 | 2013-05-15 15:01:46 +0000 | [diff] [blame] | 25 | static uint64_t adjustFixupValue(unsigned Kind, uint64_t Value) { |
Roman Divacky | 038c1a1 | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 26 | switch (Kind) { |
| 27 | default: |
| 28 | llvm_unreachable("Unknown fixup kind!"); |
| 29 | case FK_Data_1: |
| 30 | case FK_Data_2: |
| 31 | case FK_Data_4: |
Adhemerval Zanella | f2aceda | 2012-10-25 12:27:42 +0000 | [diff] [blame] | 32 | case FK_Data_8: |
Bill Schmidt | ca4a0c9 | 2012-12-04 16:18:08 +0000 | [diff] [blame] | 33 | case PPC::fixup_ppc_tlsreg: |
Bill Schmidt | 24b8dd6 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 34 | case PPC::fixup_ppc_nofixup: |
Roman Divacky | 038c1a1 | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 35 | return Value; |
| 36 | case PPC::fixup_ppc_brcond14: |
Ulrich Weigand | b6a30d1 | 2013-06-24 11:03:33 +0000 | [diff] [blame^] | 37 | case PPC::fixup_ppc_brcond14abs: |
Adhemerval Zanella | f2aceda | 2012-10-25 12:27:42 +0000 | [diff] [blame] | 38 | return Value & 0xfffc; |
Roman Divacky | 038c1a1 | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 39 | case PPC::fixup_ppc_br24: |
Ulrich Weigand | b6a30d1 | 2013-06-24 11:03:33 +0000 | [diff] [blame^] | 40 | case PPC::fixup_ppc_br24abs: |
Roman Divacky | 038c1a1 | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 41 | return Value & 0x3fffffc; |
Ulrich Weigand | 6e23ac6 | 2013-05-17 12:37:21 +0000 | [diff] [blame] | 42 | case PPC::fixup_ppc_half16: |
Roman Divacky | 038c1a1 | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 43 | return Value & 0xffff; |
Ulrich Weigand | 6e23ac6 | 2013-05-17 12:37:21 +0000 | [diff] [blame] | 44 | case PPC::fixup_ppc_half16ds: |
Ulrich Weigand | 3e18601 | 2013-03-26 10:56:47 +0000 | [diff] [blame] | 45 | return Value & 0xfffc; |
Roman Divacky | 038c1a1 | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
Ulrich Weigand | 56f5b28 | 2013-05-15 15:01:46 +0000 | [diff] [blame] | 49 | static unsigned getFixupKindNumBytes(unsigned Kind) { |
| 50 | switch (Kind) { |
| 51 | default: |
| 52 | llvm_unreachable("Unknown fixup kind!"); |
| 53 | case FK_Data_1: |
| 54 | return 1; |
| 55 | case FK_Data_2: |
Ulrich Weigand | 6e23ac6 | 2013-05-17 12:37:21 +0000 | [diff] [blame] | 56 | case PPC::fixup_ppc_half16: |
| 57 | case PPC::fixup_ppc_half16ds: |
Ulrich Weigand | 56f5b28 | 2013-05-15 15:01:46 +0000 | [diff] [blame] | 58 | return 2; |
| 59 | case FK_Data_4: |
| 60 | case PPC::fixup_ppc_brcond14: |
Ulrich Weigand | b6a30d1 | 2013-06-24 11:03:33 +0000 | [diff] [blame^] | 61 | case PPC::fixup_ppc_brcond14abs: |
Ulrich Weigand | 56f5b28 | 2013-05-15 15:01:46 +0000 | [diff] [blame] | 62 | case PPC::fixup_ppc_br24: |
Ulrich Weigand | b6a30d1 | 2013-06-24 11:03:33 +0000 | [diff] [blame^] | 63 | case PPC::fixup_ppc_br24abs: |
Ulrich Weigand | 56f5b28 | 2013-05-15 15:01:46 +0000 | [diff] [blame] | 64 | return 4; |
| 65 | case FK_Data_8: |
| 66 | return 8; |
| 67 | case PPC::fixup_ppc_tlsreg: |
| 68 | case PPC::fixup_ppc_nofixup: |
| 69 | return 0; |
| 70 | } |
| 71 | } |
| 72 | |
Chris Lattner | aac9fa7 | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 73 | namespace { |
Daniel Dunbar | 8888a96 | 2010-12-16 16:09:19 +0000 | [diff] [blame] | 74 | class PPCMachObjectWriter : public MCMachObjectTargetWriter { |
Daniel Dunbar | 03fcccb | 2010-12-16 17:21:02 +0000 | [diff] [blame] | 75 | public: |
| 76 | PPCMachObjectWriter(bool Is64Bit, uint32_t CPUType, |
| 77 | uint32_t CPUSubtype) |
| 78 | : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype) {} |
Jim Grosbach | 28fcafb | 2011-06-24 23:44:37 +0000 | [diff] [blame] | 79 | |
| 80 | void RecordRelocation(MachObjectWriter *Writer, |
| 81 | const MCAssembler &Asm, const MCAsmLayout &Layout, |
| 82 | const MCFragment *Fragment, const MCFixup &Fixup, |
Benjamin Kramer | 1694f91 | 2012-11-24 15:23:49 +0000 | [diff] [blame] | 83 | MCValue Target, uint64_t &FixedValue) { |
| 84 | llvm_unreachable("Relocation emission for MachO/PPC unimplemented!"); |
| 85 | } |
Daniel Dunbar | 8888a96 | 2010-12-16 16:09:19 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
Evan Cheng | 5928e69 | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 88 | class PPCAsmBackend : public MCAsmBackend { |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 89 | const Target &TheTarget; |
| 90 | public: |
Evan Cheng | 5928e69 | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 91 | PPCAsmBackend(const Target &T) : MCAsmBackend(), TheTarget(T) {} |
Daniel Dunbar | 0c9d9fd | 2010-12-16 03:20:06 +0000 | [diff] [blame] | 92 | |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 93 | unsigned getNumFixupKinds() const { return PPC::NumTargetFixupKinds; } |
| 94 | |
| 95 | const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const { |
| 96 | const static MCFixupKindInfo Infos[PPC::NumTargetFixupKinds] = { |
| 97 | // name offset bits flags |
| 98 | { "fixup_ppc_br24", 6, 24, MCFixupKindInfo::FKF_IsPCRel }, |
| 99 | { "fixup_ppc_brcond14", 16, 14, MCFixupKindInfo::FKF_IsPCRel }, |
Ulrich Weigand | b6a30d1 | 2013-06-24 11:03:33 +0000 | [diff] [blame^] | 100 | { "fixup_ppc_br24abs", 6, 24, 0 }, |
| 101 | { "fixup_ppc_brcond14abs", 16, 14, 0 }, |
Ulrich Weigand | 6e23ac6 | 2013-05-17 12:37:21 +0000 | [diff] [blame] | 102 | { "fixup_ppc_half16", 0, 16, 0 }, |
| 103 | { "fixup_ppc_half16ds", 0, 14, 0 }, |
Bill Schmidt | c56f1d3 | 2012-12-11 20:30:11 +0000 | [diff] [blame] | 104 | { "fixup_ppc_tlsreg", 0, 0, 0 }, |
Bill Schmidt | 24b8dd6 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 105 | { "fixup_ppc_nofixup", 0, 0, 0 } |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 106 | }; |
Jim Grosbach | e2d2981 | 2012-01-18 18:52:20 +0000 | [diff] [blame] | 107 | |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 108 | if (Kind < FirstTargetFixupKind) |
Evan Cheng | 5928e69 | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 109 | return MCAsmBackend::getFixupKindInfo(Kind); |
Jim Grosbach | e2d2981 | 2012-01-18 18:52:20 +0000 | [diff] [blame] | 110 | |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 111 | assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() && |
| 112 | "Invalid kind!"); |
| 113 | return Infos[Kind - FirstTargetFixupKind]; |
| 114 | } |
Jim Grosbach | e2d2981 | 2012-01-18 18:52:20 +0000 | [diff] [blame] | 115 | |
Benjamin Kramer | 8abfec3 | 2012-11-24 13:18:17 +0000 | [diff] [blame] | 116 | void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize, |
| 117 | uint64_t Value) const { |
| 118 | Value = adjustFixupValue(Fixup.getKind(), Value); |
| 119 | if (!Value) return; // Doesn't change encoding. |
| 120 | |
| 121 | unsigned Offset = Fixup.getOffset(); |
Ulrich Weigand | 56f5b28 | 2013-05-15 15:01:46 +0000 | [diff] [blame] | 122 | unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind()); |
Benjamin Kramer | 8abfec3 | 2012-11-24 13:18:17 +0000 | [diff] [blame] | 123 | |
| 124 | // For each byte of the fragment that the fixup touches, mask in the bits |
| 125 | // from the fixup value. The Value has been "split up" into the appropriate |
| 126 | // bitfields above. |
Ulrich Weigand | 56f5b28 | 2013-05-15 15:01:46 +0000 | [diff] [blame] | 127 | for (unsigned i = 0; i != NumBytes; ++i) |
| 128 | Data[Offset + i] |= uint8_t((Value >> ((NumBytes - i - 1)*8)) & 0xff); |
Benjamin Kramer | 8abfec3 | 2012-11-24 13:18:17 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Jim Grosbach | aba3de9 | 2012-01-18 18:52:16 +0000 | [diff] [blame] | 131 | bool mayNeedRelaxation(const MCInst &Inst) const { |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 132 | // FIXME. |
| 133 | return false; |
| 134 | } |
Jim Grosbach | 25b63fa | 2011-12-06 00:47:03 +0000 | [diff] [blame] | 135 | |
| 136 | bool fixupNeedsRelaxation(const MCFixup &Fixup, |
| 137 | uint64_t Value, |
Eli Bendersky | 4d9ada0 | 2013-01-08 00:22:56 +0000 | [diff] [blame] | 138 | const MCRelaxableFragment *DF, |
Jim Grosbach | 25b63fa | 2011-12-06 00:47:03 +0000 | [diff] [blame] | 139 | const MCAsmLayout &Layout) const { |
| 140 | // FIXME. |
Craig Topper | e55c556 | 2012-02-07 02:50:20 +0000 | [diff] [blame] | 141 | llvm_unreachable("relaxInstruction() unimplemented"); |
Jim Grosbach | 25b63fa | 2011-12-06 00:47:03 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Jim Grosbach | e2d2981 | 2012-01-18 18:52:20 +0000 | [diff] [blame] | 144 | |
Jim Grosbach | aba3de9 | 2012-01-18 18:52:16 +0000 | [diff] [blame] | 145 | void relaxInstruction(const MCInst &Inst, MCInst &Res) const { |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 146 | // FIXME. |
Craig Topper | e55c556 | 2012-02-07 02:50:20 +0000 | [diff] [blame] | 147 | llvm_unreachable("relaxInstruction() unimplemented"); |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 148 | } |
Jim Grosbach | e2d2981 | 2012-01-18 18:52:20 +0000 | [diff] [blame] | 149 | |
Jim Grosbach | aba3de9 | 2012-01-18 18:52:16 +0000 | [diff] [blame] | 150 | bool writeNopData(uint64_t Count, MCObjectWriter *OW) const { |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 151 | // FIXME: Zero fill for now. That's not right, but at least will get the |
| 152 | // section size right. |
| 153 | for (uint64_t i = 0; i != Count; ++i) |
| 154 | OW->Write8(0); |
| 155 | return true; |
Jim Grosbach | e2d2981 | 2012-01-18 18:52:20 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 158 | unsigned getPointerSize() const { |
| 159 | StringRef Name = TheTarget.getName(); |
| 160 | if (Name == "ppc64") return 8; |
| 161 | assert(Name == "ppc32" && "Unknown target name!"); |
| 162 | return 4; |
| 163 | } |
| 164 | }; |
Chris Lattner | aac9fa7 | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 165 | } // end anonymous namespace |
| 166 | |
| 167 | |
| 168 | // FIXME: This should be in a separate file. |
| 169 | namespace { |
| 170 | class DarwinPPCAsmBackend : public PPCAsmBackend { |
Chris Lattner | aac9fa7 | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 171 | public: |
Daniel Dunbar | d2867f1 | 2010-12-17 02:06:08 +0000 | [diff] [blame] | 172 | DarwinPPCAsmBackend(const Target &T) : PPCAsmBackend(T) { } |
Jim Grosbach | e2d2981 | 2012-01-18 18:52:20 +0000 | [diff] [blame] | 173 | |
Chris Lattner | aac9fa7 | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 174 | MCObjectWriter *createObjectWriter(raw_ostream &OS) const { |
| 175 | bool is64 = getPointerSize() == 8; |
Daniel Dunbar | 03fcccb | 2010-12-16 17:21:02 +0000 | [diff] [blame] | 176 | return createMachObjectWriter(new PPCMachObjectWriter( |
| 177 | /*Is64Bit=*/is64, |
| 178 | (is64 ? object::mach::CTM_PowerPC64 : |
| 179 | object::mach::CTM_PowerPC), |
| 180 | object::mach::CSPPC_ALL), |
| 181 | OS, /*IsLittleEndian=*/false); |
Chris Lattner | aac9fa7 | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 182 | } |
Jim Grosbach | e2d2981 | 2012-01-18 18:52:20 +0000 | [diff] [blame] | 183 | |
Chris Lattner | aac9fa7 | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 184 | virtual bool doesSectionRequireSymbols(const MCSection &Section) const { |
| 185 | return false; |
| 186 | } |
| 187 | }; |
Roman Divacky | 038c1a1 | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 188 | |
| 189 | class ELFPPCAsmBackend : public PPCAsmBackend { |
Rafael Espindola | 1ad4095 | 2011-12-21 17:00:36 +0000 | [diff] [blame] | 190 | uint8_t OSABI; |
Roman Divacky | 038c1a1 | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 191 | public: |
Rafael Espindola | 1ad4095 | 2011-12-21 17:00:36 +0000 | [diff] [blame] | 192 | ELFPPCAsmBackend(const Target &T, uint8_t OSABI) : |
| 193 | PPCAsmBackend(T), OSABI(OSABI) { } |
Jim Grosbach | e2d2981 | 2012-01-18 18:52:20 +0000 | [diff] [blame] | 194 | |
Jim Grosbach | e2d2981 | 2012-01-18 18:52:20 +0000 | [diff] [blame] | 195 | |
Roman Divacky | 038c1a1 | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 196 | MCObjectWriter *createObjectWriter(raw_ostream &OS) const { |
| 197 | bool is64 = getPointerSize() == 8; |
Rafael Espindola | 2500962 | 2011-12-22 18:38:06 +0000 | [diff] [blame] | 198 | return createPPCELFObjectWriter(OS, is64, OSABI); |
Roman Divacky | 038c1a1 | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 199 | } |
Jim Grosbach | e2d2981 | 2012-01-18 18:52:20 +0000 | [diff] [blame] | 200 | |
Roman Divacky | 038c1a1 | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 201 | virtual bool doesSectionRequireSymbols(const MCSection &Section) const { |
| 202 | return false; |
| 203 | } |
| 204 | }; |
| 205 | |
Chris Lattner | aac9fa7 | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 206 | } // end anonymous namespace |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
Roman Divacky | 5dd4ccb | 2012-09-18 16:08:49 +0000 | [diff] [blame] | 211 | MCAsmBackend *llvm::createPPCAsmBackend(const Target &T, StringRef TT, StringRef CPU) { |
Daniel Dunbar | 2b9b0e3 | 2011-04-19 21:14:45 +0000 | [diff] [blame] | 212 | if (Triple(TT).isOSDarwin()) |
Chris Lattner | aac9fa7 | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 213 | return new DarwinPPCAsmBackend(T); |
Daniel Dunbar | 2b9b0e3 | 2011-04-19 21:14:45 +0000 | [diff] [blame] | 214 | |
Rafael Espindola | 1ad4095 | 2011-12-21 17:00:36 +0000 | [diff] [blame] | 215 | uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(Triple(TT).getOS()); |
| 216 | return new ELFPPCAsmBackend(T, OSABI); |
Chris Lattner | aac9fa7 | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 217 | } |