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/MCDirectives.h"
16 #include "llvm/MC/MCExpr.h"
17 #include "llvm/MC/MCObjectFormat.h"
18 #include "llvm/MC/MCObjectWriter.h"
19 #include "llvm/MC/MCSectionELF.h"
20 #include "llvm/MC/MCSectionMachO.h"
21 #include "llvm/Object/MachOFormat.h"
22 #include "llvm/Support/ELF.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Target/TargetAsmBackend.h"
26 #include "llvm/Target/TargetRegistry.h"
30 class ARMAsmBackend : public TargetAsmBackend {
31 bool isThumbMode; // Currently emitting Thumb code.
33 ARMAsmBackend(const Target &T) : TargetAsmBackend(), isThumbMode(false) {}
35 bool MayNeedRelaxation(const MCInst &Inst) const;
37 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
39 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
41 void HandleAssemblerFlag(MCAssemblerFlag Flag) {
53 unsigned getPointerSize() const { return 4; }
54 bool isThumb() const { return isThumbMode; }
55 void setIsThumb(bool it) { isThumbMode = it; }
57 } // end anonymous namespace
59 bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
60 // FIXME: Thumb targets, different move constant targets..
64 void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
65 assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented");
69 bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
71 assert (((Count & 1) == 0) && "Unaligned Nop data fragment!");
72 // FIXME: 0xbf00 is the ARMv7 value. For v6 and before, we'll need to
73 // use 0x46c0 (which is a 'mov r8, r8' insn).
75 for (uint64_t i = 0; i != Count; ++i)
81 for (uint64_t i = 0; i != Count; ++i)
82 OW->Write32(0xe1a00000);
86 static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
89 llvm_unreachable("Unknown fixup kind!");
92 case ARM::fixup_arm_movt_hi16:
93 case ARM::fixup_arm_movw_lo16: {
94 unsigned Hi4 = (Value & 0xF000) >> 12;
95 unsigned Lo12 = Value & 0x0FFF;
98 Value = (Hi4 << 16) | (Lo12);
101 case ARM::fixup_arm_ldst_pcrel_12:
102 // ARM PC-relative values are offset by 8.
104 case ARM::fixup_t2_ldst_pcrel_12: {
105 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
108 if ((int64_t)Value < 0) {
112 assert ((Value < 4096) && "Out of range pc-relative fixup value!");
113 Value |= isAdd << 23;
115 // Same addressing mode as fixup_arm_pcrel_10,
116 // but with 16-bit halfwords swapped.
117 if (Kind == ARM::fixup_t2_ldst_pcrel_12) {
118 uint64_t swapped = (Value & 0xFFFF0000) >> 16;
119 swapped |= (Value & 0x0000FFFF) << 16;
125 case ARM::fixup_arm_adr_pcrel_12: {
126 // ARM PC-relative values are offset by 8.
128 unsigned opc = 4; // bits {24-21}. Default to add: 0b0100
129 if ((int64_t)Value < 0) {
133 assert(ARM_AM::getSOImmVal(Value) != -1 &&
134 "Out of range pc-relative fixup value!");
135 // Encode the immediate and shift the opcode into place.
136 return ARM_AM::getSOImmVal(Value) | (opc << 21);
138 case ARM::fixup_arm_branch:
139 // These values don't encode the low two bits since they're always zero.
140 // Offset by 8 just as above.
141 return 0xffffff & ((Value - 8) >> 2);
142 case ARM::fixup_t2_branch: {
144 Value >>= 1; // Low bit is not encoded.
147 out |= (Value & 0x80000) << 7; // S bit
148 out |= (Value & 0x40000) >> 7; // J2 bit
149 out |= (Value & 0x20000) >> 4; // J1 bit
150 out |= (Value & 0x1F800) << 5; // imm6 field
151 out |= (Value & 0x007FF); // imm11 field
153 uint64_t swapped = (out & 0xFFFF0000) >> 16;
154 swapped |= (out & 0x0000FFFF) << 16;
157 case ARM::fixup_arm_thumb_bl: {
158 // The value doesn't encode the low bit (always zero) and is offset by
159 // four. The value is encoded into disjoint bit positions in the destination
160 // opcode. x = unchanged, I = immediate value bit, S = sign extension bit
162 // BL: xxxxxSIIIIIIIIII xxxxxIIIIIIIIIII
164 // Note that the halfwords are stored high first, low second; so we need
165 // to transpose the fixup value here to map properly.
166 unsigned isNeg = (int64_t(Value) < 0) ? 1 : 0;
168 Value = 0x3fffff & ((Value - 4) >> 1);
169 Binary = (Value & 0x7ff) << 16; // Low imm11 value.
170 Binary |= (Value & 0x1ffc00) >> 11; // High imm10 value.
171 Binary |= isNeg << 10; // Sign bit.
174 case ARM::fixup_arm_thumb_blx: {
175 // The value doesn't encode the low two bits (always zero) and is offset by
176 // four (see fixup_arm_thumb_cp). The value is encoded into disjoint bit
177 // positions in the destination opcode. x = unchanged, I = immediate value
178 // bit, S = sign extension bit, 0 = zero.
180 // BLX: xxxxxSIIIIIIIIII xxxxxIIIIIIIIII0
182 // Note that the halfwords are stored high first, low second; so we need
183 // to transpose the fixup value here to map properly.
184 unsigned isNeg = (int64_t(Value) < 0) ? 1 : 0;
186 Value = 0xfffff & ((Value - 2) >> 2);
187 Binary = (Value & 0x3ff) << 17; // Low imm10L value.
188 Binary |= (Value & 0xffc00) >> 10; // High imm10H value.
189 Binary |= isNeg << 10; // Sign bit.
192 case ARM::fixup_arm_thumb_cp:
193 // Offset by 4, and don't encode the low two bits. Two bytes of that
194 // 'off by 4' is implicitly handled by the half-word ordering of the
195 // Thumb encoding, so we only need to adjust by 2 here.
196 return ((Value - 2) >> 2) & 0xff;
197 case ARM::fixup_arm_thumb_cb: {
198 // Offset by 4 and don't encode the lower bit, which is always 0.
199 uint32_t Binary = (Value - 4) >> 1;
200 return ((Binary & 0x20) << 9) | ((Binary & 0x1f) << 3);
202 case ARM::fixup_arm_pcrel_10:
203 Value = Value - 6; // ARM fixups offset by an additional word and don't
204 // need to adjust for the half-word ordering.
206 case ARM::fixup_t2_pcrel_10: {
207 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
210 if ((int64_t)Value < 0) {
214 // These values don't encode the low two bits since they're always zero.
216 assert ((Value < 256) && "Out of range pc-relative fixup value!");
217 Value |= isAdd << 23;
219 // Same addressing mode as fixup_arm_pcrel_10,
220 // but with 16-bit halfwords swapped.
221 if (Kind == ARM::fixup_t2_pcrel_10) {
222 uint64_t swapped = (Value & 0xFFFF0000) >> 16;
223 swapped |= (Value & 0x0000FFFF) << 16;
234 // FIXME: This should be in a separate file.
235 // ELF is an ELF of course...
236 class ELFARMAsmBackend : public ARMAsmBackend {
237 MCELFObjectFormat Format;
240 Triple::OSType OSType;
241 ELFARMAsmBackend(const Target &T, Triple::OSType _OSType)
242 : ARMAsmBackend(T), OSType(_OSType) {
243 HasScatteredSymbols = true;
246 virtual const MCObjectFormat &getObjectFormat() const {
250 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
251 uint64_t Value) const;
253 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
254 return createELFObjectWriter(OS, /*Is64Bit=*/false,
256 /*IsLittleEndian=*/true,
257 /*HasRelocationAddend=*/false);
261 // FIXME: Raise this to share code between Darwin and ELF.
262 void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
263 unsigned DataSize, uint64_t Value) const {
264 unsigned NumBytes = 4; // FIXME: 2 for Thumb
265 Value = adjustFixupValue(Fixup.getKind(), Value);
266 if (!Value) return; // Doesn't change encoding.
268 unsigned Offset = Fixup.getOffset();
269 assert(Offset % NumBytes == 0 && "Offset mod NumBytes is nonzero!");
271 // For each byte of the fragment that the fixup touches, mask in the bits from
272 // the fixup value. The Value has been "split up" into the appropriate
274 for (unsigned i = 0; i != NumBytes; ++i)
275 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
278 // FIXME: This should be in a separate file.
279 class DarwinARMAsmBackend : public ARMAsmBackend {
280 MCMachOObjectFormat Format;
282 DarwinARMAsmBackend(const Target &T) : ARMAsmBackend(T) {
283 HasScatteredSymbols = true;
286 virtual const MCObjectFormat &getObjectFormat() const {
290 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
291 uint64_t Value) const;
293 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
294 // FIXME: Subtarget info should be derived. Force v7 for now.
295 return createMachObjectWriter(OS, /*Is64Bit=*/false,
296 object::mach::CTM_ARM,
297 object::mach::CSARM_V7,
298 /*IsLittleEndian=*/true);
301 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
306 /// getFixupKindNumBytes - The number of bytes the fixup may change.
307 static unsigned getFixupKindNumBytes(unsigned Kind) {
310 llvm_unreachable("Unknown fixup kind!");
312 case ARM::fixup_arm_thumb_cp:
315 case ARM::fixup_arm_thumb_cb:
318 case ARM::fixup_arm_ldst_pcrel_12:
319 case ARM::fixup_arm_pcrel_10:
320 case ARM::fixup_arm_adr_pcrel_12:
321 case ARM::fixup_arm_branch:
325 case ARM::fixup_t2_ldst_pcrel_12:
326 case ARM::fixup_t2_branch:
327 case ARM::fixup_t2_pcrel_10:
328 case ARM::fixup_arm_thumb_bl:
329 case ARM::fixup_arm_thumb_blx:
334 void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
335 unsigned DataSize, uint64_t Value) const {
336 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
337 Value = adjustFixupValue(Fixup.getKind(), Value);
338 if (!Value) return; // Doesn't change encoding.
340 unsigned Offset = Fixup.getOffset();
341 assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
343 // For each byte of the fragment that the fixup touches, mask in the
344 // bits from the fixup value.
345 for (unsigned i = 0; i != NumBytes; ++i)
346 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
349 } // end anonymous namespace
351 TargetAsmBackend *llvm::createARMAsmBackend(const Target &T,
352 const std::string &TT) {
353 switch (Triple(TT).getOS()) {
355 return new DarwinARMAsmBackend(T);
356 case Triple::MinGW32:
359 assert(0 && "Windows not supported on ARM");
361 return new ELFARMAsmBackend(T, Triple(TT).getOS());