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" |
Roman Divacky | 038c1a1 | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ELF.h" |
| 20 | #include "llvm/Support/ErrorHandling.h" |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MachO.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 | 24b8dd6 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 33 | case PPC::fixup_ppc_nofixup: |
Roman Divacky | 038c1a1 | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 34 | return Value; |
| 35 | case PPC::fixup_ppc_brcond14: |
Ulrich Weigand | b6a30d1 | 2013-06-24 11:03:33 +0000 | [diff] [blame] | 36 | case PPC::fixup_ppc_brcond14abs: |
Adhemerval Zanella | f2aceda | 2012-10-25 12:27:42 +0000 | [diff] [blame] | 37 | return Value & 0xfffc; |
Roman Divacky | 038c1a1 | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 38 | case PPC::fixup_ppc_br24: |
Ulrich Weigand | b6a30d1 | 2013-06-24 11:03:33 +0000 | [diff] [blame] | 39 | case PPC::fixup_ppc_br24abs: |
Roman Divacky | 038c1a1 | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 40 | return Value & 0x3fffffc; |
Ulrich Weigand | 6e23ac6 | 2013-05-17 12:37:21 +0000 | [diff] [blame] | 41 | case PPC::fixup_ppc_half16: |
Roman Divacky | 038c1a1 | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 42 | return Value & 0xffff; |
Ulrich Weigand | 6e23ac6 | 2013-05-17 12:37:21 +0000 | [diff] [blame] | 43 | case PPC::fixup_ppc_half16ds: |
Ulrich Weigand | 3e18601 | 2013-03-26 10:56:47 +0000 | [diff] [blame] | 44 | return Value & 0xfffc; |
Roman Divacky | 038c1a1 | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
Ulrich Weigand | 56f5b28 | 2013-05-15 15:01:46 +0000 | [diff] [blame] | 48 | static unsigned getFixupKindNumBytes(unsigned Kind) { |
| 49 | switch (Kind) { |
| 50 | default: |
| 51 | llvm_unreachable("Unknown fixup kind!"); |
| 52 | case FK_Data_1: |
| 53 | return 1; |
| 54 | case FK_Data_2: |
Ulrich Weigand | 6e23ac6 | 2013-05-17 12:37:21 +0000 | [diff] [blame] | 55 | case PPC::fixup_ppc_half16: |
| 56 | case PPC::fixup_ppc_half16ds: |
Ulrich Weigand | 56f5b28 | 2013-05-15 15:01:46 +0000 | [diff] [blame] | 57 | return 2; |
| 58 | case FK_Data_4: |
| 59 | case PPC::fixup_ppc_brcond14: |
Ulrich Weigand | b6a30d1 | 2013-06-24 11:03:33 +0000 | [diff] [blame] | 60 | case PPC::fixup_ppc_brcond14abs: |
Ulrich Weigand | 56f5b28 | 2013-05-15 15:01:46 +0000 | [diff] [blame] | 61 | case PPC::fixup_ppc_br24: |
Ulrich Weigand | b6a30d1 | 2013-06-24 11:03:33 +0000 | [diff] [blame] | 62 | case PPC::fixup_ppc_br24abs: |
Ulrich Weigand | 56f5b28 | 2013-05-15 15:01:46 +0000 | [diff] [blame] | 63 | return 4; |
| 64 | case FK_Data_8: |
| 65 | return 8; |
Ulrich Weigand | 56f5b28 | 2013-05-15 15:01:46 +0000 | [diff] [blame] | 66 | case PPC::fixup_ppc_nofixup: |
| 67 | return 0; |
| 68 | } |
| 69 | } |
| 70 | |
Chris Lattner | aac9fa7 | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 71 | namespace { |
Daniel Dunbar | 8888a96 | 2010-12-16 16:09:19 +0000 | [diff] [blame] | 72 | |
Evan Cheng | 5928e69 | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 73 | class PPCAsmBackend : public MCAsmBackend { |
Ulrich Weigand | cae3a17 | 2014-03-24 18:16:09 +0000 | [diff] [blame] | 74 | const Target &TheTarget; |
| 75 | bool IsLittleEndian; |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 76 | public: |
Ulrich Weigand | cae3a17 | 2014-03-24 18:16:09 +0000 | [diff] [blame] | 77 | PPCAsmBackend(const Target &T, bool isLittle) : MCAsmBackend(), TheTarget(T), |
| 78 | IsLittleEndian(isLittle) {} |
Daniel Dunbar | 0c9d9fd | 2010-12-16 03:20:06 +0000 | [diff] [blame] | 79 | |
Craig Topper | 0d3fa92 | 2014-04-29 07:57:37 +0000 | [diff] [blame^] | 80 | unsigned getNumFixupKinds() const override { |
| 81 | return PPC::NumTargetFixupKinds; |
| 82 | } |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 83 | |
Craig Topper | 0d3fa92 | 2014-04-29 07:57:37 +0000 | [diff] [blame^] | 84 | const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override { |
Ulrich Weigand | cae3a17 | 2014-03-24 18:16:09 +0000 | [diff] [blame] | 85 | const static MCFixupKindInfo InfosBE[PPC::NumTargetFixupKinds] = { |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 86 | // name offset bits flags |
| 87 | { "fixup_ppc_br24", 6, 24, MCFixupKindInfo::FKF_IsPCRel }, |
| 88 | { "fixup_ppc_brcond14", 16, 14, MCFixupKindInfo::FKF_IsPCRel }, |
Ulrich Weigand | b6a30d1 | 2013-06-24 11:03:33 +0000 | [diff] [blame] | 89 | { "fixup_ppc_br24abs", 6, 24, 0 }, |
| 90 | { "fixup_ppc_brcond14abs", 16, 14, 0 }, |
Ulrich Weigand | 6e23ac6 | 2013-05-17 12:37:21 +0000 | [diff] [blame] | 91 | { "fixup_ppc_half16", 0, 16, 0 }, |
| 92 | { "fixup_ppc_half16ds", 0, 14, 0 }, |
Bill Schmidt | 24b8dd6 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 93 | { "fixup_ppc_nofixup", 0, 0, 0 } |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 94 | }; |
Ulrich Weigand | cae3a17 | 2014-03-24 18:16:09 +0000 | [diff] [blame] | 95 | const static MCFixupKindInfo InfosLE[PPC::NumTargetFixupKinds] = { |
| 96 | // name offset bits flags |
| 97 | { "fixup_ppc_br24", 2, 24, MCFixupKindInfo::FKF_IsPCRel }, |
| 98 | { "fixup_ppc_brcond14", 2, 14, MCFixupKindInfo::FKF_IsPCRel }, |
| 99 | { "fixup_ppc_br24abs", 2, 24, 0 }, |
| 100 | { "fixup_ppc_brcond14abs", 2, 14, 0 }, |
| 101 | { "fixup_ppc_half16", 0, 16, 0 }, |
| 102 | { "fixup_ppc_half16ds", 2, 14, 0 }, |
| 103 | { "fixup_ppc_nofixup", 0, 0, 0 } |
| 104 | }; |
Jim Grosbach | e2d2981 | 2012-01-18 18:52:20 +0000 | [diff] [blame] | 105 | |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 106 | if (Kind < FirstTargetFixupKind) |
Evan Cheng | 5928e69 | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 107 | return MCAsmBackend::getFixupKindInfo(Kind); |
Jim Grosbach | e2d2981 | 2012-01-18 18:52:20 +0000 | [diff] [blame] | 108 | |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 109 | assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() && |
| 110 | "Invalid kind!"); |
Ulrich Weigand | cae3a17 | 2014-03-24 18:16:09 +0000 | [diff] [blame] | 111 | return (IsLittleEndian? InfosLE : InfosBE)[Kind - FirstTargetFixupKind]; |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 112 | } |
Jim Grosbach | e2d2981 | 2012-01-18 18:52:20 +0000 | [diff] [blame] | 113 | |
Benjamin Kramer | 8abfec3 | 2012-11-24 13:18:17 +0000 | [diff] [blame] | 114 | void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize, |
Craig Topper | 0d3fa92 | 2014-04-29 07:57:37 +0000 | [diff] [blame^] | 115 | uint64_t Value, bool IsPCRel) const override { |
Benjamin Kramer | 8abfec3 | 2012-11-24 13:18:17 +0000 | [diff] [blame] | 116 | Value = adjustFixupValue(Fixup.getKind(), Value); |
| 117 | if (!Value) return; // Doesn't change encoding. |
| 118 | |
| 119 | unsigned Offset = Fixup.getOffset(); |
Ulrich Weigand | 56f5b28 | 2013-05-15 15:01:46 +0000 | [diff] [blame] | 120 | unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind()); |
Benjamin Kramer | 8abfec3 | 2012-11-24 13:18:17 +0000 | [diff] [blame] | 121 | |
| 122 | // For each byte of the fragment that the fixup touches, mask in the bits |
| 123 | // from the fixup value. The Value has been "split up" into the appropriate |
| 124 | // bitfields above. |
Ulrich Weigand | cae3a17 | 2014-03-24 18:16:09 +0000 | [diff] [blame] | 125 | for (unsigned i = 0; i != NumBytes; ++i) { |
| 126 | unsigned Idx = IsLittleEndian ? i : (NumBytes - 1 - i); |
| 127 | Data[Offset + i] |= uint8_t((Value >> (Idx * 8)) & 0xff); |
| 128 | } |
Benjamin Kramer | 8abfec3 | 2012-11-24 13:18:17 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Craig Topper | 0d3fa92 | 2014-04-29 07:57:37 +0000 | [diff] [blame^] | 131 | bool mayNeedRelaxation(const MCInst &Inst) const override { |
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, |
Craig Topper | 0d3fa92 | 2014-04-29 07:57:37 +0000 | [diff] [blame^] | 139 | const MCAsmLayout &Layout) const override { |
Jim Grosbach | 25b63fa | 2011-12-06 00:47:03 +0000 | [diff] [blame] | 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 | |
Craig Topper | 0d3fa92 | 2014-04-29 07:57:37 +0000 | [diff] [blame^] | 145 | void relaxInstruction(const MCInst &Inst, MCInst &Res) const override { |
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 | |
Craig Topper | 0d3fa92 | 2014-04-29 07:57:37 +0000 | [diff] [blame^] | 150 | bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override { |
Ulrich Weigand | d3ac7c0 | 2013-07-04 18:28:46 +0000 | [diff] [blame] | 151 | uint64_t NumNops = Count / 4; |
| 152 | for (uint64_t i = 0; i != NumNops; ++i) |
| 153 | OW->Write32(0x60000000); |
| 154 | |
David Majnemer | 7137420 | 2013-09-26 09:18:48 +0000 | [diff] [blame] | 155 | switch (Count % 4) { |
| 156 | default: break; // No leftover bytes to write |
| 157 | case 1: OW->Write8(0); break; |
| 158 | case 2: OW->Write16(0); break; |
| 159 | case 3: OW->Write16(0); OW->Write8(0); break; |
| 160 | } |
| 161 | |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 162 | return true; |
Jim Grosbach | e2d2981 | 2012-01-18 18:52:20 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 165 | unsigned getPointerSize() const { |
| 166 | StringRef Name = TheTarget.getName(); |
Bill Schmidt | 0a9170d | 2013-07-26 01:35:43 +0000 | [diff] [blame] | 167 | if (Name == "ppc64" || Name == "ppc64le") return 8; |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 168 | assert(Name == "ppc32" && "Unknown target name!"); |
| 169 | return 4; |
| 170 | } |
Ulrich Weigand | cae3a17 | 2014-03-24 18:16:09 +0000 | [diff] [blame] | 171 | |
| 172 | bool isLittleEndian() const { |
| 173 | return IsLittleEndian; |
| 174 | } |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 175 | }; |
Chris Lattner | aac9fa7 | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 176 | } // end anonymous namespace |
| 177 | |
| 178 | |
| 179 | // FIXME: This should be in a separate file. |
| 180 | namespace { |
| 181 | class DarwinPPCAsmBackend : public PPCAsmBackend { |
Chris Lattner | aac9fa7 | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 182 | public: |
Ulrich Weigand | cae3a17 | 2014-03-24 18:16:09 +0000 | [diff] [blame] | 183 | DarwinPPCAsmBackend(const Target &T) : PPCAsmBackend(T, false) { } |
Jim Grosbach | e2d2981 | 2012-01-18 18:52:20 +0000 | [diff] [blame] | 184 | |
Craig Topper | 0d3fa92 | 2014-04-29 07:57:37 +0000 | [diff] [blame^] | 185 | MCObjectWriter *createObjectWriter(raw_ostream &OS) const override { |
Chris Lattner | aac9fa7 | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 186 | bool is64 = getPointerSize() == 8; |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 187 | return createPPCMachObjectWriter( |
| 188 | OS, |
| 189 | /*Is64Bit=*/is64, |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 190 | (is64 ? MachO::CPU_TYPE_POWERPC64 : MachO::CPU_TYPE_POWERPC), |
| 191 | MachO::CPU_SUBTYPE_POWERPC_ALL); |
Chris Lattner | aac9fa7 | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 192 | } |
Chris Lattner | aac9fa7 | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 193 | }; |
Roman Divacky | 038c1a1 | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 194 | |
| 195 | class ELFPPCAsmBackend : public PPCAsmBackend { |
Rafael Espindola | 1ad4095 | 2011-12-21 17:00:36 +0000 | [diff] [blame] | 196 | uint8_t OSABI; |
Roman Divacky | 038c1a1 | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 197 | public: |
Ulrich Weigand | cae3a17 | 2014-03-24 18:16:09 +0000 | [diff] [blame] | 198 | ELFPPCAsmBackend(const Target &T, bool IsLittleEndian, uint8_t OSABI) : |
| 199 | PPCAsmBackend(T, IsLittleEndian), OSABI(OSABI) { } |
Jim Grosbach | e2d2981 | 2012-01-18 18:52:20 +0000 | [diff] [blame] | 200 | |
Jim Grosbach | e2d2981 | 2012-01-18 18:52:20 +0000 | [diff] [blame] | 201 | |
Craig Topper | 0d3fa92 | 2014-04-29 07:57:37 +0000 | [diff] [blame^] | 202 | MCObjectWriter *createObjectWriter(raw_ostream &OS) const override { |
Roman Divacky | 038c1a1 | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 203 | bool is64 = getPointerSize() == 8; |
Ulrich Weigand | cae3a17 | 2014-03-24 18:16:09 +0000 | [diff] [blame] | 204 | return createPPCELFObjectWriter(OS, is64, isLittleEndian(), OSABI); |
Roman Divacky | 038c1a1 | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 205 | } |
Roman Divacky | 038c1a1 | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 206 | }; |
| 207 | |
Chris Lattner | aac9fa7 | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 208 | } // end anonymous namespace |
| 209 | |
Bill Wendling | 58e2d3d | 2013-09-09 02:37:14 +0000 | [diff] [blame] | 210 | MCAsmBackend *llvm::createPPCAsmBackend(const Target &T, |
| 211 | const MCRegisterInfo &MRI, |
| 212 | StringRef TT, StringRef CPU) { |
Daniel Dunbar | 2b9b0e3 | 2011-04-19 21:14:45 +0000 | [diff] [blame] | 213 | if (Triple(TT).isOSDarwin()) |
Chris Lattner | aac9fa7 | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 214 | return new DarwinPPCAsmBackend(T); |
Daniel Dunbar | 2b9b0e3 | 2011-04-19 21:14:45 +0000 | [diff] [blame] | 215 | |
Rafael Espindola | 1ad4095 | 2011-12-21 17:00:36 +0000 | [diff] [blame] | 216 | uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(Triple(TT).getOS()); |
Ulrich Weigand | cae3a17 | 2014-03-24 18:16:09 +0000 | [diff] [blame] | 217 | bool IsLittleEndian = Triple(TT).getArch() == Triple::ppc64le; |
| 218 | return new ELFPPCAsmBackend(T, IsLittleEndian, OSABI); |
Chris Lattner | aac9fa7 | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 219 | } |