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 | |
| 10 | #include "llvm/Target/TargetAsmBackend.h" |
| 11 | #include "ARM.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" |
| 14 | #include "llvm/MC/ELFObjectWriter.h" |
| 15 | #include "llvm/MC/MCAssembler.h" |
| 16 | #include "llvm/MC/MCExpr.h" |
Rafael Espindola | f230df9 | 2010-10-16 18:23:53 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCObjectFormat.h" |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCObjectWriter.h" |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCSectionELF.h" |
| 20 | #include "llvm/MC/MCSectionMachO.h" |
| 21 | #include "llvm/MC/MachObjectWriter.h" |
Wesley Peck | eecb858 | 2010-10-22 15:52:49 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ELF.h" |
Jim Grosbach | c9d1439 | 2010-11-05 18:48:58 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MachO.h" |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ErrorHandling.h" |
| 25 | #include "llvm/Support/raw_ostream.h" |
| 26 | #include "llvm/Target/TargetRegistry.h" |
| 27 | #include "llvm/Target/TargetAsmBackend.h" |
| 28 | using namespace llvm; |
| 29 | |
| 30 | namespace { |
| 31 | class ARMAsmBackend : public TargetAsmBackend { |
| 32 | public: |
| 33 | ARMAsmBackend(const Target &T) |
Jim Grosbach | f73fd72 | 2010-09-30 03:21:00 +0000 | [diff] [blame] | 34 | : TargetAsmBackend(T) { |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | bool MayNeedRelaxation(const MCInst &Inst) const; |
| 38 | |
| 39 | void RelaxInstruction(const MCInst &Inst, MCInst &Res) const; |
| 40 | |
| 41 | bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const; |
Jim Grosbach | 3787a40 | 2010-09-30 17:45:51 +0000 | [diff] [blame] | 42 | |
| 43 | unsigned getPointerSize() const { |
| 44 | return 4; |
| 45 | } |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const { |
| 49 | // FIXME: Thumb targets, different move constant targets.. |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const { |
| 54 | assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented"); |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const { |
Jim Grosbach | e50e6bc | 2010-11-11 23:41:09 +0000 | [diff] [blame^] | 59 | // if ((Count % 4) != 0) { |
| 60 | // // Fixme: % 2 for Thumb? |
| 61 | // return false; |
| 62 | // } |
| 63 | // FIXME: Zero fill for now. That's not right, but at least will get the |
| 64 | // section size right. |
| 65 | for (uint64_t i = 0; i != Count; ++i) |
| 66 | OW->Write8(0); |
Rafael Espindola | cecbc3d | 2010-10-25 17:50:35 +0000 | [diff] [blame] | 67 | return true; |
Jim Grosbach | 87dc3aa | 2010-09-30 03:20:34 +0000 | [diff] [blame] | 68 | } |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 69 | } // end anonymous namespace |
| 70 | |
| 71 | namespace { |
| 72 | // FIXME: This should be in a separate file. |
| 73 | // ELF is an ELF of course... |
| 74 | class ELFARMAsmBackend : public ARMAsmBackend { |
Rafael Espindola | f230df9 | 2010-10-16 18:23:53 +0000 | [diff] [blame] | 75 | MCELFObjectFormat Format; |
| 76 | |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 77 | public: |
| 78 | Triple::OSType OSType; |
| 79 | ELFARMAsmBackend(const Target &T, Triple::OSType _OSType) |
| 80 | : ARMAsmBackend(T), OSType(_OSType) { |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 81 | HasScatteredSymbols = true; |
| 82 | } |
| 83 | |
Rafael Espindola | f230df9 | 2010-10-16 18:23:53 +0000 | [diff] [blame] | 84 | virtual const MCObjectFormat &getObjectFormat() const { |
| 85 | return Format; |
| 86 | } |
| 87 | |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 88 | void ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF, |
| 89 | uint64_t Value) const; |
| 90 | |
| 91 | bool isVirtualSection(const MCSection &Section) const { |
| 92 | const MCSectionELF &SE = static_cast<const MCSectionELF&>(Section); |
| 93 | return SE.getType() == MCSectionELF::SHT_NOBITS; |
| 94 | } |
| 95 | |
| 96 | MCObjectWriter *createObjectWriter(raw_ostream &OS) const { |
| 97 | return new ELFObjectWriter(OS, /*Is64Bit=*/false, |
Wesley Peck | eecb858 | 2010-10-22 15:52:49 +0000 | [diff] [blame] | 98 | OSType, ELF::EM_ARM, |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 99 | /*IsLittleEndian=*/true, |
| 100 | /*HasRelocationAddend=*/false); |
| 101 | } |
| 102 | }; |
| 103 | |
Jason W Kim | a4c2724 | 2010-09-30 14:58:19 +0000 | [diff] [blame] | 104 | // Fixme: can we raise this to share code between Darwin and ELF? |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 105 | void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF, |
| 106 | uint64_t Value) const { |
| 107 | assert(0 && "ELFARMAsmBackend::ApplyFixup() unimplemented"); |
| 108 | } |
| 109 | |
| 110 | // FIXME: This should be in a separate file. |
| 111 | class DarwinARMAsmBackend : public ARMAsmBackend { |
Rafael Espindola | f230df9 | 2010-10-16 18:23:53 +0000 | [diff] [blame] | 112 | MCMachOObjectFormat Format; |
| 113 | |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 114 | public: |
| 115 | DarwinARMAsmBackend(const Target &T) |
| 116 | : ARMAsmBackend(T) { |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 117 | HasScatteredSymbols = true; |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Rafael Espindola | f230df9 | 2010-10-16 18:23:53 +0000 | [diff] [blame] | 120 | virtual const MCObjectFormat &getObjectFormat() const { |
| 121 | return Format; |
| 122 | } |
| 123 | |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 124 | void ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF, |
| 125 | uint64_t Value) const; |
| 126 | |
| 127 | bool isVirtualSection(const MCSection &Section) const { |
| 128 | const MCSectionMachO &SMO = static_cast<const MCSectionMachO&>(Section); |
| 129 | return (SMO.getType() == MCSectionMachO::S_ZEROFILL || |
| 130 | SMO.getType() == MCSectionMachO::S_GB_ZEROFILL || |
| 131 | SMO.getType() == MCSectionMachO::S_THREAD_LOCAL_ZEROFILL); |
| 132 | } |
| 133 | |
| 134 | MCObjectWriter *createObjectWriter(raw_ostream &OS) const { |
Jim Grosbach | c9d1439 | 2010-11-05 18:48:58 +0000 | [diff] [blame] | 135 | // FIXME: Subtarget info should be derived. Force v7 for now. |
| 136 | return new MachObjectWriter(OS, /*Is64Bit=*/false, MachO::CPUTypeARM, |
| 137 | MachO::CPUSubType_ARM_V7); |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | virtual bool doesSectionRequireSymbols(const MCSection &Section) const { |
| 141 | return false; |
| 142 | } |
| 143 | }; |
| 144 | |
Jim Grosbach | c466b93 | 2010-11-11 18:04:49 +0000 | [diff] [blame] | 145 | static unsigned getFixupKindNumBytes(unsigned Kind) { |
Jim Grosbach | 679cbd3 | 2010-11-09 01:37:15 +0000 | [diff] [blame] | 146 | switch (Kind) { |
| 147 | default: llvm_unreachable("Unknown fixup kind!"); |
Jim Grosbach | c466b93 | 2010-11-11 18:04:49 +0000 | [diff] [blame] | 148 | case FK_Data_4: return 4; |
Jim Grosbach | 679cbd3 | 2010-11-09 01:37:15 +0000 | [diff] [blame] | 149 | case ARM::fixup_arm_pcrel_12: return 2; |
| 150 | case ARM::fixup_arm_vfp_pcrel_12: return 1; |
Jim Grosbach | c466b93 | 2010-11-11 18:04:49 +0000 | [diff] [blame] | 151 | case ARM::fixup_arm_branch: return 3; |
Jim Grosbach | 679cbd3 | 2010-11-09 01:37:15 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) { |
| 156 | switch (Kind) { |
| 157 | default: |
| 158 | llvm_unreachable("Unknown fixup kind!"); |
| 159 | case FK_Data_4: |
Jim Grosbach | a9a0dde | 2010-11-09 17:36:59 +0000 | [diff] [blame] | 160 | return Value; |
Jim Grosbach | 679cbd3 | 2010-11-09 01:37:15 +0000 | [diff] [blame] | 161 | case ARM::fixup_arm_pcrel_12: |
| 162 | // ARM PC-relative values are offset by 8. |
| 163 | return Value - 8; |
Jim Grosbach | c466b93 | 2010-11-11 18:04:49 +0000 | [diff] [blame] | 164 | case ARM::fixup_arm_branch: |
Jim Grosbach | 679cbd3 | 2010-11-09 01:37:15 +0000 | [diff] [blame] | 165 | case ARM::fixup_arm_vfp_pcrel_12: |
Jim Grosbach | c466b93 | 2010-11-11 18:04:49 +0000 | [diff] [blame] | 166 | // These values don't encode the low two bits since they're always zero. |
| 167 | // Offset by 8 just as above. |
Jim Grosbach | 679cbd3 | 2010-11-09 01:37:15 +0000 | [diff] [blame] | 168 | return (Value - 8) >> 2; |
| 169 | } |
| 170 | } |
| 171 | |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 172 | void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF, |
Jim Grosbach | c466b93 | 2010-11-11 18:04:49 +0000 | [diff] [blame] | 173 | uint64_t Value) const { |
| 174 | unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind()); |
Jim Grosbach | 679cbd3 | 2010-11-09 01:37:15 +0000 | [diff] [blame] | 175 | Value = adjustFixupValue(Fixup.getKind(), Value); |
| 176 | |
| 177 | assert(Fixup.getOffset() + NumBytes <= DF.getContents().size() && |
| 178 | "Invalid fixup offset!"); |
| 179 | // For each byte of the fragment that the fixup touches, mask in the |
| 180 | // bits from the fixup value. |
| 181 | for (unsigned i = 0; i != NumBytes; ++i) |
| 182 | DF.getContents()[Fixup.getOffset() + i] |= uint8_t(Value >> (i * 8)); |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 183 | } |
Jim Grosbach | f73fd72 | 2010-09-30 03:21:00 +0000 | [diff] [blame] | 184 | } // end anonymous namespace |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 185 | |
| 186 | TargetAsmBackend *llvm::createARMAsmBackend(const Target &T, |
| 187 | const std::string &TT) { |
| 188 | switch (Triple(TT).getOS()) { |
| 189 | case Triple::Darwin: |
| 190 | return new DarwinARMAsmBackend(T); |
| 191 | case Triple::MinGW32: |
| 192 | case Triple::Cygwin: |
| 193 | case Triple::Win32: |
| 194 | assert(0 && "Windows not supported on ARM"); |
| 195 | default: |
| 196 | return new ELFARMAsmBackend(T, Triple(TT).getOS()); |
| 197 | } |
| 198 | } |