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 { |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 74 | const Target &TheTarget; |
| 75 | public: |
Evan Cheng | 5928e69 | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 76 | PPCAsmBackend(const Target &T) : MCAsmBackend(), TheTarget(T) {} |
Daniel Dunbar | 0c9d9fd | 2010-12-16 03:20:06 +0000 | [diff] [blame] | 77 | |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 78 | unsigned getNumFixupKinds() const { return PPC::NumTargetFixupKinds; } |
| 79 | |
| 80 | const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const { |
| 81 | const static MCFixupKindInfo Infos[PPC::NumTargetFixupKinds] = { |
| 82 | // name offset bits flags |
| 83 | { "fixup_ppc_br24", 6, 24, MCFixupKindInfo::FKF_IsPCRel }, |
| 84 | { "fixup_ppc_brcond14", 16, 14, MCFixupKindInfo::FKF_IsPCRel }, |
Ulrich Weigand | b6a30d1 | 2013-06-24 11:03:33 +0000 | [diff] [blame] | 85 | { "fixup_ppc_br24abs", 6, 24, 0 }, |
| 86 | { "fixup_ppc_brcond14abs", 16, 14, 0 }, |
Ulrich Weigand | 6e23ac6 | 2013-05-17 12:37:21 +0000 | [diff] [blame] | 87 | { "fixup_ppc_half16", 0, 16, 0 }, |
| 88 | { "fixup_ppc_half16ds", 0, 14, 0 }, |
Bill Schmidt | 24b8dd6 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 89 | { "fixup_ppc_nofixup", 0, 0, 0 } |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 90 | }; |
Jim Grosbach | e2d2981 | 2012-01-18 18:52:20 +0000 | [diff] [blame] | 91 | |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 92 | if (Kind < FirstTargetFixupKind) |
Evan Cheng | 5928e69 | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 93 | return MCAsmBackend::getFixupKindInfo(Kind); |
Jim Grosbach | e2d2981 | 2012-01-18 18:52:20 +0000 | [diff] [blame] | 94 | |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 95 | assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() && |
| 96 | "Invalid kind!"); |
| 97 | return Infos[Kind - FirstTargetFixupKind]; |
| 98 | } |
Jim Grosbach | e2d2981 | 2012-01-18 18:52:20 +0000 | [diff] [blame] | 99 | |
Benjamin Kramer | 8abfec3 | 2012-11-24 13:18:17 +0000 | [diff] [blame] | 100 | void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize, |
| 101 | uint64_t Value) const { |
| 102 | Value = adjustFixupValue(Fixup.getKind(), Value); |
| 103 | if (!Value) return; // Doesn't change encoding. |
| 104 | |
| 105 | unsigned Offset = Fixup.getOffset(); |
Ulrich Weigand | 56f5b28 | 2013-05-15 15:01:46 +0000 | [diff] [blame] | 106 | unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind()); |
Benjamin Kramer | 8abfec3 | 2012-11-24 13:18:17 +0000 | [diff] [blame] | 107 | |
| 108 | // For each byte of the fragment that the fixup touches, mask in the bits |
| 109 | // from the fixup value. The Value has been "split up" into the appropriate |
| 110 | // bitfields above. |
Ulrich Weigand | 56f5b28 | 2013-05-15 15:01:46 +0000 | [diff] [blame] | 111 | for (unsigned i = 0; i != NumBytes; ++i) |
| 112 | Data[Offset + i] |= uint8_t((Value >> ((NumBytes - i - 1)*8)) & 0xff); |
Benjamin Kramer | 8abfec3 | 2012-11-24 13:18:17 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Jim Grosbach | aba3de9 | 2012-01-18 18:52:16 +0000 | [diff] [blame] | 115 | bool mayNeedRelaxation(const MCInst &Inst) const { |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 116 | // FIXME. |
| 117 | return false; |
| 118 | } |
Jim Grosbach | 25b63fa | 2011-12-06 00:47:03 +0000 | [diff] [blame] | 119 | |
| 120 | bool fixupNeedsRelaxation(const MCFixup &Fixup, |
| 121 | uint64_t Value, |
Eli Bendersky | 4d9ada0 | 2013-01-08 00:22:56 +0000 | [diff] [blame] | 122 | const MCRelaxableFragment *DF, |
Jim Grosbach | 25b63fa | 2011-12-06 00:47:03 +0000 | [diff] [blame] | 123 | const MCAsmLayout &Layout) const { |
| 124 | // FIXME. |
Craig Topper | e55c556 | 2012-02-07 02:50:20 +0000 | [diff] [blame] | 125 | llvm_unreachable("relaxInstruction() unimplemented"); |
Jim Grosbach | 25b63fa | 2011-12-06 00:47:03 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Jim Grosbach | e2d2981 | 2012-01-18 18:52:20 +0000 | [diff] [blame] | 128 | |
Jim Grosbach | aba3de9 | 2012-01-18 18:52:16 +0000 | [diff] [blame] | 129 | void relaxInstruction(const MCInst &Inst, MCInst &Res) const { |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 130 | // FIXME. |
Craig Topper | e55c556 | 2012-02-07 02:50:20 +0000 | [diff] [blame] | 131 | llvm_unreachable("relaxInstruction() unimplemented"); |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 132 | } |
Jim Grosbach | e2d2981 | 2012-01-18 18:52:20 +0000 | [diff] [blame] | 133 | |
Jim Grosbach | aba3de9 | 2012-01-18 18:52:16 +0000 | [diff] [blame] | 134 | bool writeNopData(uint64_t Count, MCObjectWriter *OW) const { |
Ulrich Weigand | d3ac7c0 | 2013-07-04 18:28:46 +0000 | [diff] [blame] | 135 | // Can't emit NOP with size not multiple of 32-bits |
| 136 | if (Count % 4 != 0) |
| 137 | return false; |
| 138 | |
| 139 | uint64_t NumNops = Count / 4; |
| 140 | for (uint64_t i = 0; i != NumNops; ++i) |
| 141 | OW->Write32(0x60000000); |
| 142 | |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 143 | return true; |
Jim Grosbach | e2d2981 | 2012-01-18 18:52:20 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 146 | unsigned getPointerSize() const { |
| 147 | StringRef Name = TheTarget.getName(); |
Bill Schmidt | 0a9170d | 2013-07-26 01:35:43 +0000 | [diff] [blame] | 148 | if (Name == "ppc64" || Name == "ppc64le") return 8; |
Daniel Dunbar | 7ee2181 | 2010-12-16 16:08:43 +0000 | [diff] [blame] | 149 | assert(Name == "ppc32" && "Unknown target name!"); |
| 150 | return 4; |
| 151 | } |
| 152 | }; |
Chris Lattner | aac9fa7 | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 153 | } // end anonymous namespace |
| 154 | |
| 155 | |
| 156 | // FIXME: This should be in a separate file. |
| 157 | namespace { |
| 158 | class DarwinPPCAsmBackend : public PPCAsmBackend { |
Chris Lattner | aac9fa7 | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 159 | public: |
Daniel Dunbar | d2867f1 | 2010-12-17 02:06:08 +0000 | [diff] [blame] | 160 | DarwinPPCAsmBackend(const Target &T) : PPCAsmBackend(T) { } |
Jim Grosbach | e2d2981 | 2012-01-18 18:52:20 +0000 | [diff] [blame] | 161 | |
Chris Lattner | aac9fa7 | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 162 | MCObjectWriter *createObjectWriter(raw_ostream &OS) const { |
| 163 | bool is64 = getPointerSize() == 8; |
David Fang | b88cdf6 | 2013-08-08 20:14:40 +0000 | [diff] [blame] | 164 | return createPPCMachObjectWriter( |
| 165 | OS, |
| 166 | /*Is64Bit=*/is64, |
Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 167 | (is64 ? MachO::CPU_TYPE_POWERPC64 : MachO::CPU_TYPE_POWERPC), |
| 168 | MachO::CPU_SUBTYPE_POWERPC_ALL); |
Chris Lattner | aac9fa7 | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 169 | } |
Jim Grosbach | e2d2981 | 2012-01-18 18:52:20 +0000 | [diff] [blame] | 170 | |
Chris Lattner | aac9fa7 | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 171 | virtual bool doesSectionRequireSymbols(const MCSection &Section) const { |
| 172 | return false; |
| 173 | } |
| 174 | }; |
Roman Divacky | 038c1a1 | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 175 | |
| 176 | class ELFPPCAsmBackend : public PPCAsmBackend { |
Rafael Espindola | 1ad4095 | 2011-12-21 17:00:36 +0000 | [diff] [blame] | 177 | uint8_t OSABI; |
Roman Divacky | 038c1a1 | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 178 | public: |
Rafael Espindola | 1ad4095 | 2011-12-21 17:00:36 +0000 | [diff] [blame] | 179 | ELFPPCAsmBackend(const Target &T, uint8_t OSABI) : |
| 180 | PPCAsmBackend(T), OSABI(OSABI) { } |
Jim Grosbach | e2d2981 | 2012-01-18 18:52:20 +0000 | [diff] [blame] | 181 | |
Jim Grosbach | e2d2981 | 2012-01-18 18:52:20 +0000 | [diff] [blame] | 182 | |
Roman Divacky | 038c1a1 | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 183 | MCObjectWriter *createObjectWriter(raw_ostream &OS) const { |
| 184 | bool is64 = getPointerSize() == 8; |
Rafael Espindola | 2500962 | 2011-12-22 18:38:06 +0000 | [diff] [blame] | 185 | return createPPCELFObjectWriter(OS, is64, OSABI); |
Roman Divacky | 038c1a1 | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 186 | } |
Jim Grosbach | e2d2981 | 2012-01-18 18:52:20 +0000 | [diff] [blame] | 187 | |
Roman Divacky | 038c1a1 | 2011-08-02 15:51:38 +0000 | [diff] [blame] | 188 | virtual bool doesSectionRequireSymbols(const MCSection &Section) const { |
| 189 | return false; |
| 190 | } |
| 191 | }; |
| 192 | |
Chris Lattner | aac9fa7 | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 193 | } // end anonymous namespace |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
Roman Divacky | 5dd4ccb | 2012-09-18 16:08:49 +0000 | [diff] [blame] | 198 | MCAsmBackend *llvm::createPPCAsmBackend(const Target &T, StringRef TT, StringRef CPU) { |
Daniel Dunbar | 2b9b0e3 | 2011-04-19 21:14:45 +0000 | [diff] [blame] | 199 | if (Triple(TT).isOSDarwin()) |
Chris Lattner | aac9fa7 | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 200 | return new DarwinPPCAsmBackend(T); |
Daniel Dunbar | 2b9b0e3 | 2011-04-19 21:14:45 +0000 | [diff] [blame] | 201 | |
Rafael Espindola | 1ad4095 | 2011-12-21 17:00:36 +0000 | [diff] [blame] | 202 | uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(Triple(TT).getOS()); |
| 203 | return new ELFPPCAsmBackend(T, OSABI); |
Chris Lattner | aac9fa7 | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 204 | } |