Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 1 | //===-- ARMAsmBackend.cpp - ARM 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 | |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 10 | #include "ARM.h" |
Jim Grosbach | dff84b0 | 2010-12-02 00:28:45 +0000 | [diff] [blame] | 11 | #include "ARMAddressingModes.h" |
Jim Grosbach | 679cbd3 | 2010-11-09 01:37:15 +0000 | [diff] [blame] | 12 | #include "ARMFixupKinds.h" |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/Twine.h" |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCAssembler.h" |
| 15 | #include "llvm/MC/MCExpr.h" |
Rafael Espindola | f230df9 | 2010-10-16 18:23:53 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCObjectFormat.h" |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCObjectWriter.h" |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCSectionELF.h" |
| 19 | #include "llvm/MC/MCSectionMachO.h" |
Daniel Dunbar | 36d76a8 | 2010-11-27 04:38:36 +0000 | [diff] [blame] | 20 | #include "llvm/Object/MachOFormat.h" |
Wesley Peck | eecb858 | 2010-10-22 15:52:49 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ELF.h" |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
| 23 | #include "llvm/Support/raw_ostream.h" |
Jim Grosbach | dff84b0 | 2010-12-02 00:28:45 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetAsmBackend.h" |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetRegistry.h" |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
| 28 | namespace { |
| 29 | class ARMAsmBackend : public TargetAsmBackend { |
| 30 | public: |
Rafael Espindola | fd46797 | 2010-11-26 04:24:21 +0000 | [diff] [blame] | 31 | ARMAsmBackend(const Target &T) : TargetAsmBackend() {} |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 32 | |
| 33 | bool MayNeedRelaxation(const MCInst &Inst) const; |
| 34 | |
| 35 | void RelaxInstruction(const MCInst &Inst, MCInst &Res) const; |
| 36 | |
| 37 | bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const; |
Jim Grosbach | 3787a40 | 2010-09-30 17:45:51 +0000 | [diff] [blame] | 38 | |
| 39 | unsigned getPointerSize() const { |
| 40 | return 4; |
| 41 | } |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 42 | }; |
Chris Lattner | b75c651 | 2010-11-17 05:41:32 +0000 | [diff] [blame] | 43 | } // end anonymous namespace |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 44 | |
| 45 | bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const { |
| 46 | // FIXME: Thumb targets, different move constant targets.. |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const { |
| 51 | assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented"); |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const { |
Jim Grosbach | e50e6bc | 2010-11-11 23:41:09 +0000 | [diff] [blame] | 56 | // FIXME: Zero fill for now. That's not right, but at least will get the |
| 57 | // section size right. |
| 58 | for (uint64_t i = 0; i != Count; ++i) |
| 59 | OW->Write8(0); |
Rafael Espindola | cecbc3d | 2010-10-25 17:50:35 +0000 | [diff] [blame] | 60 | return true; |
Jim Grosbach | 87dc3aa | 2010-09-30 03:20:34 +0000 | [diff] [blame] | 61 | } |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 62 | |
Jason W Kim | 0c628c2 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 63 | static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) { |
| 64 | switch (Kind) { |
| 65 | default: |
| 66 | llvm_unreachable("Unknown fixup kind!"); |
| 67 | case FK_Data_4: |
Jason W Kim | 0c628c2 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 68 | return Value; |
Jason W Kim | 2ccf148 | 2010-12-03 19:40:23 +0000 | [diff] [blame] | 69 | case ARM::fixup_arm_movt_hi16: |
| 70 | case ARM::fixup_arm_movw_lo16: { |
| 71 | unsigned Hi4 = (Value & 0xF000) >> 12; |
| 72 | unsigned Lo12 = Value & 0x0FFF; |
| 73 | // inst{19-16} = Hi4; |
| 74 | // inst{11-0} = Lo12; |
| 75 | Value = (Hi4 << 16) | (Lo12); |
| 76 | return Value; |
| 77 | } |
Jim Grosbach | dff84b0 | 2010-12-02 00:28:45 +0000 | [diff] [blame] | 78 | case ARM::fixup_arm_ldst_pcrel_12: { |
Jason W Kim | 0c628c2 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 79 | bool isAdd = true; |
| 80 | // ARM PC-relative values are offset by 8. |
| 81 | Value -= 8; |
| 82 | if ((int64_t)Value < 0) { |
| 83 | Value = -Value; |
| 84 | isAdd = false; |
| 85 | } |
| 86 | assert ((Value < 4096) && "Out of range pc-relative fixup value!"); |
| 87 | Value |= isAdd << 23; |
| 88 | return Value; |
| 89 | } |
Jim Grosbach | dff84b0 | 2010-12-02 00:28:45 +0000 | [diff] [blame] | 90 | case ARM::fixup_arm_adr_pcrel_12: { |
| 91 | // ARM PC-relative values are offset by 8. |
| 92 | Value -= 8; |
| 93 | unsigned opc = 4; // bits {24-21}. Default to add: 0b0100 |
| 94 | if ((int64_t)Value < 0) { |
| 95 | Value = -Value; |
| 96 | opc = 2; // 0b0010 |
| 97 | } |
| 98 | assert(ARM_AM::getSOImmVal(Value) != -1 && |
| 99 | "Out of range pc-relative fixup value!"); |
| 100 | // Encode the immediate and shift the opcode into place. |
| 101 | return ARM_AM::getSOImmVal(Value) | (opc << 21); |
| 102 | } |
Jason W Kim | 0c628c2 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 103 | case ARM::fixup_arm_branch: |
| 104 | // These values don't encode the low two bits since they're always zero. |
| 105 | // Offset by 8 just as above. |
Jim Grosbach | 662a816 | 2010-12-06 23:57:07 +0000 | [diff] [blame] | 106 | return 0xffffff & ((Value - 8) >> 2); |
| 107 | case ARM::fixup_arm_thumb_bl: { |
| 108 | // The value doesn't encode the low bit (always zero) and is offset by |
| 109 | // four. The value is encoded into disjoint bit positions in the destination |
| 110 | // opcode. x = unchanged, I = immediate value bit, S = sign extension bit |
| 111 | // xxxxxSIIIIIIIIII xxxxxIIIIIIIIIII |
| 112 | // Note that the halfwords are stored high first, low second; so we need |
| 113 | // to transpose the fixup value here to map properly. |
| 114 | uint32_t Binary = 0x3fffff & ((Value - 4) >> 1); |
| 115 | Binary = ((Binary & 0x7ff) << 16) | (Binary >> 11); |
| 116 | return Binary; |
| 117 | } |
Jason W Kim | 0c628c2 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 118 | case ARM::fixup_arm_pcrel_10: { |
| 119 | // Offset by 8 just as above. |
| 120 | Value = Value - 8; |
| 121 | bool isAdd = true; |
| 122 | if ((int64_t)Value < 0) { |
| 123 | Value = -Value; |
| 124 | isAdd = false; |
| 125 | } |
| 126 | // These values don't encode the low two bits since they're always zero. |
| 127 | Value >>= 2; |
| 128 | assert ((Value < 256) && "Out of range pc-relative fixup value!"); |
| 129 | Value |= isAdd << 23; |
| 130 | return Value; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 135 | namespace { |
Bill Wendling | 52e635e | 2010-12-07 23:05:20 +0000 | [diff] [blame^] | 136 | |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 137 | // FIXME: This should be in a separate file. |
| 138 | // ELF is an ELF of course... |
| 139 | class ELFARMAsmBackend : public ARMAsmBackend { |
Rafael Espindola | f230df9 | 2010-10-16 18:23:53 +0000 | [diff] [blame] | 140 | MCELFObjectFormat Format; |
| 141 | |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 142 | public: |
| 143 | Triple::OSType OSType; |
| 144 | ELFARMAsmBackend(const Target &T, Triple::OSType _OSType) |
| 145 | : ARMAsmBackend(T), OSType(_OSType) { |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 146 | HasScatteredSymbols = true; |
| 147 | } |
| 148 | |
Rafael Espindola | f230df9 | 2010-10-16 18:23:53 +0000 | [diff] [blame] | 149 | virtual const MCObjectFormat &getObjectFormat() const { |
| 150 | return Format; |
| 151 | } |
| 152 | |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 153 | void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize, |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 154 | uint64_t Value) const; |
| 155 | |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 156 | MCObjectWriter *createObjectWriter(raw_ostream &OS) const { |
Daniel Dunbar | 115a3dd | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 157 | return createELFObjectWriter(OS, /*Is64Bit=*/false, |
| 158 | OSType, ELF::EM_ARM, |
| 159 | /*IsLittleEndian=*/true, |
| 160 | /*HasRelocationAddend=*/false); |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 161 | } |
| 162 | }; |
| 163 | |
Bill Wendling | 52e635e | 2010-12-07 23:05:20 +0000 | [diff] [blame^] | 164 | // FIXME: Raise this to share code between Darwin and ELF. |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 165 | void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data, |
| 166 | unsigned DataSize, uint64_t Value) const { |
Bill Wendling | 52e635e | 2010-12-07 23:05:20 +0000 | [diff] [blame^] | 167 | unsigned NumBytes = 4; // FIXME: 2 for Thumb |
Jason W Kim | 85fed5e | 2010-12-01 02:40:06 +0000 | [diff] [blame] | 168 | |
Bill Wendling | 52e635e | 2010-12-07 23:05:20 +0000 | [diff] [blame^] | 169 | Value = adjustFixupValue(Fixup.getKind(), Value); |
| 170 | if (!Value) return; // No need to encode nothing. |
| 171 | |
| 172 | unsigned Offset = Fixup.getOffset(); |
| 173 | assert(Offset % NumBytes == 0 && "Offset mod NumBytes is nonzero!"); |
| 174 | |
| 175 | // For each byte of the fragment that the fixup touches, mask in the bits from |
| 176 | // the fixup value. The Value has been "split up" into the appropriate |
| 177 | // bitfields above. |
| 178 | for (unsigned i = 0; i != NumBytes; ++i) |
| 179 | Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff); |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | // FIXME: This should be in a separate file. |
| 183 | class DarwinARMAsmBackend : public ARMAsmBackend { |
Rafael Espindola | f230df9 | 2010-10-16 18:23:53 +0000 | [diff] [blame] | 184 | MCMachOObjectFormat Format; |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 185 | public: |
Chris Lattner | b75c651 | 2010-11-17 05:41:32 +0000 | [diff] [blame] | 186 | DarwinARMAsmBackend(const Target &T) : ARMAsmBackend(T) { |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 187 | HasScatteredSymbols = true; |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Rafael Espindola | f230df9 | 2010-10-16 18:23:53 +0000 | [diff] [blame] | 190 | virtual const MCObjectFormat &getObjectFormat() const { |
| 191 | return Format; |
| 192 | } |
| 193 | |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 194 | void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize, |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 195 | uint64_t Value) const; |
| 196 | |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 197 | MCObjectWriter *createObjectWriter(raw_ostream &OS) const { |
Jim Grosbach | c9d1439 | 2010-11-05 18:48:58 +0000 | [diff] [blame] | 198 | // FIXME: Subtarget info should be derived. Force v7 for now. |
Daniel Dunbar | 36d76a8 | 2010-11-27 04:38:36 +0000 | [diff] [blame] | 199 | return createMachObjectWriter(OS, /*Is64Bit=*/false, |
| 200 | object::mach::CTM_ARM, |
| 201 | object::mach::CSARM_V7, |
Daniel Dunbar | 115a3dd | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 202 | /*IsLittleEndian=*/true); |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | virtual bool doesSectionRequireSymbols(const MCSection &Section) const { |
| 206 | return false; |
| 207 | } |
| 208 | }; |
| 209 | |
Jim Grosbach | c466b93 | 2010-11-11 18:04:49 +0000 | [diff] [blame] | 210 | static unsigned getFixupKindNumBytes(unsigned Kind) { |
Jim Grosbach | 679cbd3 | 2010-11-09 01:37:15 +0000 | [diff] [blame] | 211 | switch (Kind) { |
Jim Grosbach | 662a816 | 2010-12-06 23:57:07 +0000 | [diff] [blame] | 212 | default: |
| 213 | llvm_unreachable("Unknown fixup kind!"); |
| 214 | case FK_Data_4: |
| 215 | return 4; |
| 216 | case ARM::fixup_arm_ldst_pcrel_12: |
| 217 | case ARM::fixup_arm_pcrel_10: |
| 218 | case ARM::fixup_arm_adr_pcrel_12: |
| 219 | case ARM::fixup_arm_branch: |
| 220 | return 3; |
| 221 | case ARM::fixup_arm_thumb_bl: |
| 222 | return 4; |
Jim Grosbach | 679cbd3 | 2010-11-09 01:37:15 +0000 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 226 | void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data, |
| 227 | unsigned DataSize, uint64_t Value) const { |
Jim Grosbach | c466b93 | 2010-11-11 18:04:49 +0000 | [diff] [blame] | 228 | unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind()); |
Jim Grosbach | 679cbd3 | 2010-11-09 01:37:15 +0000 | [diff] [blame] | 229 | Value = adjustFixupValue(Fixup.getKind(), Value); |
| 230 | |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 231 | assert(Fixup.getOffset() + NumBytes <= DataSize && |
Jim Grosbach | 679cbd3 | 2010-11-09 01:37:15 +0000 | [diff] [blame] | 232 | "Invalid fixup offset!"); |
| 233 | // For each byte of the fragment that the fixup touches, mask in the |
| 234 | // bits from the fixup value. |
| 235 | for (unsigned i = 0; i != NumBytes; ++i) |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 236 | Data[Fixup.getOffset() + i] |= uint8_t(Value >> (i * 8)); |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 237 | } |
Bill Wendling | 52e635e | 2010-12-07 23:05:20 +0000 | [diff] [blame^] | 238 | |
Jim Grosbach | f73fd72 | 2010-09-30 03:21:00 +0000 | [diff] [blame] | 239 | } // end anonymous namespace |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 240 | |
| 241 | TargetAsmBackend *llvm::createARMAsmBackend(const Target &T, |
| 242 | const std::string &TT) { |
| 243 | switch (Triple(TT).getOS()) { |
| 244 | case Triple::Darwin: |
| 245 | return new DarwinARMAsmBackend(T); |
| 246 | case Triple::MinGW32: |
| 247 | case Triple::Cygwin: |
| 248 | case Triple::Win32: |
| 249 | assert(0 && "Windows not supported on ARM"); |
| 250 | default: |
| 251 | return new ELFARMAsmBackend(T, Triple(TT).getOS()); |
| 252 | } |
| 253 | } |