1 //===-- ARMAsmBackend.cpp - ARM Assembler Backend -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "ARMAddressingModes.h"
12 #include "ARMFixupKinds.h"
13 #include "llvm/ADT/Twine.h"
14 #include "llvm/MC/MCAssembler.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCObjectFormat.h"
17 #include "llvm/MC/MCObjectWriter.h"
18 #include "llvm/MC/MCSectionELF.h"
19 #include "llvm/MC/MCSectionMachO.h"
20 #include "llvm/Object/MachOFormat.h"
21 #include "llvm/Support/ELF.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/Target/TargetAsmBackend.h"
25 #include "llvm/Target/TargetRegistry.h"
29 class ARMAsmBackend : public TargetAsmBackend {
31 ARMAsmBackend(const Target &T) : TargetAsmBackend() {}
33 bool MayNeedRelaxation(const MCInst &Inst) const;
35 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
37 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
39 unsigned getPointerSize() const {
43 } // end anonymous namespace
45 bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
46 // FIXME: Thumb targets, different move constant targets..
50 void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
51 assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented");
55 bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
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)
63 static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
66 llvm_unreachable("Unknown fixup kind!");
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;
75 Value = (Hi4 << 16) | (Lo12);
78 case ARM::fixup_arm_ldst_pcrel_12: {
80 // ARM PC-relative values are offset by 8.
82 if ((int64_t)Value < 0) {
86 assert ((Value < 4096) && "Out of range pc-relative fixup value!");
90 case ARM::fixup_arm_adr_pcrel_12: {
91 // ARM PC-relative values are offset by 8.
93 unsigned opc = 4; // bits {24-21}. Default to add: 0b0100
94 if ((int64_t)Value < 0) {
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);
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.
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);
118 case ARM::fixup_arm_pcrel_10: {
119 // Offset by 8 just as above.
122 if ((int64_t)Value < 0) {
126 // These values don't encode the low two bits since they're always zero.
128 assert ((Value < 256) && "Out of range pc-relative fixup value!");
129 Value |= isAdd << 23;
137 // FIXME: This should be in a separate file.
138 // ELF is an ELF of course...
139 class ELFARMAsmBackend : public ARMAsmBackend {
140 MCELFObjectFormat Format;
143 Triple::OSType OSType;
144 ELFARMAsmBackend(const Target &T, Triple::OSType _OSType)
145 : ARMAsmBackend(T), OSType(_OSType) {
146 HasScatteredSymbols = true;
149 virtual const MCObjectFormat &getObjectFormat() const {
153 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
154 uint64_t Value) const;
156 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
157 return createELFObjectWriter(OS, /*Is64Bit=*/false,
159 /*IsLittleEndian=*/true,
160 /*HasRelocationAddend=*/false);
164 // FIXME: Raise this to share code between Darwin and ELF.
165 void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
166 unsigned DataSize, uint64_t Value) const {
167 unsigned NumBytes = 4; // FIXME: 2 for Thumb
169 Value = adjustFixupValue(Fixup.getKind(), Value);
170 if (!Value) return; // No need to encode nothing.
172 unsigned Offset = Fixup.getOffset();
173 assert(Offset % NumBytes == 0 && "Offset mod NumBytes is nonzero!");
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
178 for (unsigned i = 0; i != NumBytes; ++i)
179 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
182 // FIXME: This should be in a separate file.
183 class DarwinARMAsmBackend : public ARMAsmBackend {
184 MCMachOObjectFormat Format;
186 DarwinARMAsmBackend(const Target &T) : ARMAsmBackend(T) {
187 HasScatteredSymbols = true;
190 virtual const MCObjectFormat &getObjectFormat() const {
194 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
195 uint64_t Value) const;
197 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
198 // FIXME: Subtarget info should be derived. Force v7 for now.
199 return createMachObjectWriter(OS, /*Is64Bit=*/false,
200 object::mach::CTM_ARM,
201 object::mach::CSARM_V7,
202 /*IsLittleEndian=*/true);
205 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
210 static unsigned getFixupKindNumBytes(unsigned Kind) {
213 llvm_unreachable("Unknown fixup kind!");
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:
221 case ARM::fixup_arm_thumb_bl:
226 void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
227 unsigned DataSize, uint64_t Value) const {
228 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
229 Value = adjustFixupValue(Fixup.getKind(), Value);
231 assert(Fixup.getOffset() + NumBytes <= DataSize &&
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)
236 Data[Fixup.getOffset() + i] |= uint8_t(Value >> (i * 8));
239 } // end anonymous namespace
241 TargetAsmBackend *llvm::createARMAsmBackend(const Target &T,
242 const std::string &TT) {
243 switch (Triple(TT).getOS()) {
245 return new DarwinARMAsmBackend(T);
246 case Triple::MinGW32:
249 assert(0 && "Windows not supported on ARM");
251 return new ELFARMAsmBackend(T, Triple(TT).getOS());