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