blob: a58d5b34131b3c4a4948663cca0fa055d05ed07c [file] [log] [blame]
Jason W Kimb3212452010-09-30 02:17:26 +00001//===-- 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
Jim Grosbach45e50d82011-08-16 17:06:20 +000010#include "MCTargetDesc/ARMMCTargetDesc.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000011#include "MCTargetDesc/ARMAddressingModes.h"
Joe Abbey8e72eb72014-09-16 09:18:23 +000012#include "MCTargetDesc/ARMAsmBackend.h"
13#include "MCTargetDesc/ARMAsmBackendDarwin.h"
14#include "MCTargetDesc/ARMAsmBackendELF.h"
15#include "MCTargetDesc/ARMAsmBackendWinCOFF.h"
Evan Chengad5f4852011-07-23 00:00:19 +000016#include "MCTargetDesc/ARMBaseInfo.h"
17#include "MCTargetDesc/ARMFixupKinds.h"
Quentin Colombet77ca8b82013-01-14 21:34:09 +000018#include "llvm/ADT/StringSwitch.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/MC/MCAsmBackend.h"
Jason W Kimb3212452010-09-30 02:17:26 +000020#include "llvm/MC/MCAssembler.h"
Jim Grosbache78031a2012-04-30 22:30:43 +000021#include "llvm/MC/MCContext.h"
Jim Grosbach87055ed2010-12-08 01:16:55 +000022#include "llvm/MC/MCDirectives.h"
Rafael Espindolaf0e24d42010-12-17 16:59:53 +000023#include "llvm/MC/MCELFObjectWriter.h"
Jason W Kimb3212452010-09-30 02:17:26 +000024#include "llvm/MC/MCExpr.h"
Craig Topper6e80c282012-03-26 06:58:25 +000025#include "llvm/MC/MCFixupKindInfo.h"
Daniel Dunbar73b87132010-12-16 16:08:33 +000026#include "llvm/MC/MCMachObjectWriter.h"
Jason W Kimb3212452010-09-30 02:17:26 +000027#include "llvm/MC/MCObjectWriter.h"
Tim Northoverf8e47e42015-10-28 22:56:36 +000028#include "llvm/MC/MCRegisterInfo.h"
Jason W Kimb3212452010-09-30 02:17:26 +000029#include "llvm/MC/MCSectionELF.h"
30#include "llvm/MC/MCSectionMachO.h"
Jim Grosbach45e50d82011-08-16 17:06:20 +000031#include "llvm/MC/MCSubtargetInfo.h"
Jim Grosbach3b50c9e2012-01-18 00:23:57 +000032#include "llvm/MC/MCValue.h"
Tim Northoverf8e47e42015-10-28 22:56:36 +000033#include "llvm/Support/Debug.h"
Wesley Peck18510902010-10-22 15:52:49 +000034#include "llvm/Support/ELF.h"
Jason W Kimb3212452010-09-30 02:17:26 +000035#include "llvm/Support/ErrorHandling.h"
Tim Northoverf8e47e42015-10-28 22:56:36 +000036#include "llvm/Support/Format.h"
Charles Davis8bdfafd2013-09-01 04:28:48 +000037#include "llvm/Support/MachO.h"
Vedant Kumar366dd9fd2015-08-21 21:52:48 +000038#include "llvm/Support/TargetParser.h"
Jason W Kimb3212452010-09-30 02:17:26 +000039#include "llvm/Support/raw_ostream.h"
Jason W Kimb3212452010-09-30 02:17:26 +000040using namespace llvm;
41
42namespace {
Rafael Espindola6b5e56c2010-12-17 17:45:22 +000043class ARMELFObjectWriter : public MCELFObjectTargetWriter {
44public:
Rafael Espindola1ad40952011-12-21 17:00:36 +000045 ARMELFObjectWriter(uint8_t OSABI)
Joe Abbey8e72eb72014-09-16 09:18:23 +000046 : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI, ELF::EM_ARM,
47 /*HasRelocationAddend*/ false) {}
Rafael Espindola6b5e56c2010-12-17 17:45:22 +000048};
Benjamin Kramerb32a5042016-01-27 19:29:42 +000049} // end anonymous namespace
Rafael Espindola6b5e56c2010-12-17 17:45:22 +000050
Joe Abbey8e72eb72014-09-16 09:18:23 +000051const MCFixupKindInfo &ARMAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
52 const static MCFixupKindInfo InfosLE[ARM::NumTargetFixupKinds] = {
53 // This table *must* be in the order that the fixup_* kinds are defined in
54 // ARMFixupKinds.h.
55 //
56 // Name Offset (bits) Size (bits) Flags
57 {"fixup_arm_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
58 {"fixup_t2_ldst_pcrel_12", 0, 32,
59 MCFixupKindInfo::FKF_IsPCRel |
60 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
61 {"fixup_arm_pcrel_10_unscaled", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
62 {"fixup_arm_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
63 {"fixup_t2_pcrel_10", 0, 32,
64 MCFixupKindInfo::FKF_IsPCRel |
65 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
Oliver Stannard65b85382016-01-25 10:26:26 +000066 {"fixup_arm_pcrel_9", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
67 {"fixup_t2_pcrel_9", 0, 32,
68 MCFixupKindInfo::FKF_IsPCRel |
69 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
Joe Abbey8e72eb72014-09-16 09:18:23 +000070 {"fixup_thumb_adr_pcrel_10", 0, 8,
71 MCFixupKindInfo::FKF_IsPCRel |
72 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
73 {"fixup_arm_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
74 {"fixup_t2_adr_pcrel_12", 0, 32,
75 MCFixupKindInfo::FKF_IsPCRel |
76 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
77 {"fixup_arm_condbranch", 0, 24, MCFixupKindInfo::FKF_IsPCRel},
78 {"fixup_arm_uncondbranch", 0, 24, MCFixupKindInfo::FKF_IsPCRel},
79 {"fixup_t2_condbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
80 {"fixup_t2_uncondbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
81 {"fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
82 {"fixup_arm_uncondbl", 0, 24, MCFixupKindInfo::FKF_IsPCRel},
83 {"fixup_arm_condbl", 0, 24, MCFixupKindInfo::FKF_IsPCRel},
84 {"fixup_arm_blx", 0, 24, MCFixupKindInfo::FKF_IsPCRel},
85 {"fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
Tim Northover56048d52016-05-10 21:48:48 +000086 {"fixup_arm_thumb_blx", 0, 32,
87 MCFixupKindInfo::FKF_IsPCRel |
88 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
Joe Abbey8e72eb72014-09-16 09:18:23 +000089 {"fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
90 {"fixup_arm_thumb_cp", 0, 8,
91 MCFixupKindInfo::FKF_IsPCRel |
92 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
93 {"fixup_arm_thumb_bcc", 0, 8, MCFixupKindInfo::FKF_IsPCRel},
94 // movw / movt: 16-bits immediate but scattered into two chunks 0 - 12, 16
95 // - 19.
96 {"fixup_arm_movt_hi16", 0, 20, 0},
97 {"fixup_arm_movw_lo16", 0, 20, 0},
98 {"fixup_t2_movt_hi16", 0, 20, 0},
99 {"fixup_t2_movw_lo16", 0, 20, 0},
James Molloyb876c722016-04-01 09:40:47 +0000100 {"fixup_arm_mod_imm", 0, 12, 0},
Joe Abbey8e72eb72014-09-16 09:18:23 +0000101 };
102 const static MCFixupKindInfo InfosBE[ARM::NumTargetFixupKinds] = {
103 // This table *must* be in the order that the fixup_* kinds are defined in
104 // ARMFixupKinds.h.
105 //
106 // Name Offset (bits) Size (bits) Flags
107 {"fixup_arm_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
108 {"fixup_t2_ldst_pcrel_12", 0, 32,
109 MCFixupKindInfo::FKF_IsPCRel |
110 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
111 {"fixup_arm_pcrel_10_unscaled", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
112 {"fixup_arm_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
113 {"fixup_t2_pcrel_10", 0, 32,
114 MCFixupKindInfo::FKF_IsPCRel |
115 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
Oliver Stannard65b85382016-01-25 10:26:26 +0000116 {"fixup_arm_pcrel_9", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
117 {"fixup_t2_pcrel_9", 0, 32,
118 MCFixupKindInfo::FKF_IsPCRel |
119 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
Joe Abbey8e72eb72014-09-16 09:18:23 +0000120 {"fixup_thumb_adr_pcrel_10", 8, 8,
121 MCFixupKindInfo::FKF_IsPCRel |
122 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
123 {"fixup_arm_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
124 {"fixup_t2_adr_pcrel_12", 0, 32,
125 MCFixupKindInfo::FKF_IsPCRel |
126 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
127 {"fixup_arm_condbranch", 8, 24, MCFixupKindInfo::FKF_IsPCRel},
128 {"fixup_arm_uncondbranch", 8, 24, MCFixupKindInfo::FKF_IsPCRel},
129 {"fixup_t2_condbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
130 {"fixup_t2_uncondbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
131 {"fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
132 {"fixup_arm_uncondbl", 8, 24, MCFixupKindInfo::FKF_IsPCRel},
133 {"fixup_arm_condbl", 8, 24, MCFixupKindInfo::FKF_IsPCRel},
134 {"fixup_arm_blx", 8, 24, MCFixupKindInfo::FKF_IsPCRel},
135 {"fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
Tim Northover56048d52016-05-10 21:48:48 +0000136 {"fixup_arm_thumb_blx", 0, 32,
137 MCFixupKindInfo::FKF_IsPCRel |
138 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
Joe Abbey8e72eb72014-09-16 09:18:23 +0000139 {"fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
140 {"fixup_arm_thumb_cp", 8, 8,
141 MCFixupKindInfo::FKF_IsPCRel |
142 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
143 {"fixup_arm_thumb_bcc", 8, 8, MCFixupKindInfo::FKF_IsPCRel},
144 // movw / movt: 16-bits immediate but scattered into two chunks 0 - 12, 16
145 // - 19.
146 {"fixup_arm_movt_hi16", 12, 20, 0},
147 {"fixup_arm_movw_lo16", 12, 20, 0},
148 {"fixup_t2_movt_hi16", 12, 20, 0},
149 {"fixup_t2_movw_lo16", 12, 20, 0},
James Molloyb876c722016-04-01 09:40:47 +0000150 {"fixup_arm_mod_imm", 20, 12, 0},
Joe Abbey8e72eb72014-09-16 09:18:23 +0000151 };
Jim Grosbach45e50d82011-08-16 17:06:20 +0000152
Joe Abbey8e72eb72014-09-16 09:18:23 +0000153 if (Kind < FirstTargetFixupKind)
154 return MCAsmBackend::getFixupKindInfo(Kind);
155
156 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
157 "Invalid kind!");
158 return (IsLittleEndian ? InfosLE : InfosBE)[Kind - FirstTargetFixupKind];
159}
160
161void ARMAsmBackend::handleAssemblerFlag(MCAssemblerFlag Flag) {
162 switch (Flag) {
163 default:
164 break;
165 case MCAF_Code16:
166 setIsThumb(true);
167 break;
168 case MCAF_Code32:
169 setIsThumb(false);
170 break;
Jim Grosbach45e50d82011-08-16 17:06:20 +0000171 }
Joe Abbey8e72eb72014-09-16 09:18:23 +0000172}
Jason W Kimb3212452010-09-30 02:17:26 +0000173
Tim Northover42335572015-04-06 18:44:42 +0000174unsigned ARMAsmBackend::getRelaxedOpcode(unsigned Op) const {
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000175 bool HasThumb2 = STI->getFeatureBits()[ARM::FeatureThumb2];
Bradley Smitha1189102016-01-15 10:26:17 +0000176 bool HasV8MBaselineOps = STI->getFeatureBits()[ARM::HasV8MBaselineOps];
Tim Northover42335572015-04-06 18:44:42 +0000177
Jim Grosbach34a7c6d2011-12-05 23:45:46 +0000178 switch (Op) {
Joe Abbey8e72eb72014-09-16 09:18:23 +0000179 default:
180 return Op;
181 case ARM::tBcc:
Aaron Ballmanac336242015-04-07 13:28:37 +0000182 return HasThumb2 ? (unsigned)ARM::t2Bcc : Op;
Joe Abbey8e72eb72014-09-16 09:18:23 +0000183 case ARM::tLDRpci:
Aaron Ballmanac336242015-04-07 13:28:37 +0000184 return HasThumb2 ? (unsigned)ARM::t2LDRpci : Op;
Joe Abbey8e72eb72014-09-16 09:18:23 +0000185 case ARM::tADR:
Aaron Ballmanac336242015-04-07 13:28:37 +0000186 return HasThumb2 ? (unsigned)ARM::t2ADR : Op;
Joe Abbey8e72eb72014-09-16 09:18:23 +0000187 case ARM::tB:
Bradley Smitha1189102016-01-15 10:26:17 +0000188 return HasV8MBaselineOps ? (unsigned)ARM::t2B : Op;
Joe Abbey8e72eb72014-09-16 09:18:23 +0000189 case ARM::tCBZ:
190 return ARM::tHINT;
191 case ARM::tCBNZ:
192 return ARM::tHINT;
Jim Grosbach34a7c6d2011-12-05 23:45:46 +0000193 }
194}
195
Jim Grosbachaba3de92012-01-18 18:52:16 +0000196bool ARMAsmBackend::mayNeedRelaxation(const MCInst &Inst) const {
Jim Grosbach34a7c6d2011-12-05 23:45:46 +0000197 if (getRelaxedOpcode(Inst.getOpcode()) != Inst.getOpcode())
198 return true;
Jason W Kimb3212452010-09-30 02:17:26 +0000199 return false;
200}
201
Tim Northover8d67b8e2015-10-02 18:07:18 +0000202const char *ARMAsmBackend::reasonForFixupRelaxation(const MCFixup &Fixup,
203 uint64_t Value) const {
Benjamin Kramer116e99a2012-01-19 21:11:13 +0000204 switch ((unsigned)Fixup.getKind()) {
Jim Grosbachc4aa60f2012-03-19 21:32:32 +0000205 case ARM::fixup_arm_thumb_br: {
206 // Relaxing tB to t2B. tB has a signed 12-bit displacement with the
207 // low bit being an implied zero. There's an implied +4 offset for the
208 // branch, so we adjust the other way here to determine what's
209 // encodable.
210 //
211 // Relax if the value is too big for a (signed) i8.
212 int64_t Offset = int64_t(Value) - 4;
Tim Northover8d67b8e2015-10-02 18:07:18 +0000213 if (Offset > 2046 || Offset < -2048)
214 return "out of range pc-relative fixup value";
215 break;
Jim Grosbachc4aa60f2012-03-19 21:32:32 +0000216 }
Jim Grosbachcb80eb22012-01-18 21:54:16 +0000217 case ARM::fixup_arm_thumb_bcc: {
218 // Relaxing tBcc to t2Bcc. tBcc has a signed 9-bit displacement with the
219 // low bit being an implied zero. There's an implied +4 offset for the
220 // branch, so we adjust the other way here to determine what's
221 // encodable.
222 //
223 // Relax if the value is too big for a (signed) i8.
224 int64_t Offset = int64_t(Value) - 4;
Tim Northover8d67b8e2015-10-02 18:07:18 +0000225 if (Offset > 254 || Offset < -256)
226 return "out of range pc-relative fixup value";
227 break;
Jim Grosbachcb80eb22012-01-18 21:54:16 +0000228 }
Jim Grosbach44e5c392012-01-19 02:09:38 +0000229 case ARM::fixup_thumb_adr_pcrel_10:
Jim Grosbachcb80eb22012-01-18 21:54:16 +0000230 case ARM::fixup_arm_thumb_cp: {
Jim Grosbachb008df42012-01-19 01:50:30 +0000231 // If the immediate is negative, greater than 1020, or not a multiple
232 // of four, the wide version of the instruction must be used.
Jim Grosbachcb80eb22012-01-18 21:54:16 +0000233 int64_t Offset = int64_t(Value) - 4;
Tim Northover8d67b8e2015-10-02 18:07:18 +0000234 if (Offset & 3)
235 return "misaligned pc-relative fixup value";
236 else if (Offset > 1020 || Offset < 0)
237 return "out of range pc-relative fixup value";
238 break;
Jim Grosbachcb80eb22012-01-18 21:54:16 +0000239 }
Tim Northover8d67b8e2015-10-02 18:07:18 +0000240 case ARM::fixup_arm_thumb_cb: {
Kevin Enderby9bd296a2014-01-10 00:43:32 +0000241 // If we have a Thumb CBZ or CBNZ instruction and its target is the next
242 // instruction it is is actually out of range for the instruction.
243 // It will be changed to a NOP.
244 int64_t Offset = (Value & ~1);
Tim Northover8d67b8e2015-10-02 18:07:18 +0000245 if (Offset == 2)
246 return "will be converted to nop";
247 break;
Jim Grosbachcb80eb22012-01-18 21:54:16 +0000248 }
Tim Northover8d67b8e2015-10-02 18:07:18 +0000249 default:
250 llvm_unreachable("Unexpected fixup kind in reasonForFixupRelaxation()!");
251 }
252 return nullptr;
253}
254
255bool ARMAsmBackend::fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
256 const MCRelaxableFragment *DF,
257 const MCAsmLayout &Layout) const {
258 return reasonForFixupRelaxation(Fixup, Value);
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000259}
260
Nirav Dave86030622016-07-11 14:23:53 +0000261void ARMAsmBackend::relaxInstruction(const MCInst &Inst,
262 const MCSubtargetInfo &STI,
263 MCInst &Res) const {
Jim Grosbach34a7c6d2011-12-05 23:45:46 +0000264 unsigned RelaxedOp = getRelaxedOpcode(Inst.getOpcode());
265
266 // Sanity check w/ diagnostic if we get here w/ a bogus instruction.
267 if (RelaxedOp == Inst.getOpcode()) {
268 SmallString<256> Tmp;
269 raw_svector_ostream OS(Tmp);
270 Inst.dump_pretty(OS);
271 OS << "\n";
272 report_fatal_error("unexpected instruction to relax: " + OS.str());
273 }
274
Kevin Enderby9bd296a2014-01-10 00:43:32 +0000275 // If we are changing Thumb CBZ or CBNZ instruction to a NOP, aka tHINT, we
276 // have to change the operands too.
277 if ((Inst.getOpcode() == ARM::tCBZ || Inst.getOpcode() == ARM::tCBNZ) &&
278 RelaxedOp == ARM::tHINT) {
279 Res.setOpcode(RelaxedOp);
Jim Grosbache9119e42015-05-13 18:37:00 +0000280 Res.addOperand(MCOperand::createImm(0));
281 Res.addOperand(MCOperand::createImm(14));
282 Res.addOperand(MCOperand::createReg(0));
Kevin Enderby9bd296a2014-01-10 00:43:32 +0000283 return;
Joe Abbey8e72eb72014-09-16 09:18:23 +0000284 }
Kevin Enderby9bd296a2014-01-10 00:43:32 +0000285
286 // The rest of instructions we're relaxing have the same operands.
Jim Grosbach34a7c6d2011-12-05 23:45:46 +0000287 // We just need to update to the proper opcode.
288 Res = Inst;
289 Res.setOpcode(RelaxedOp);
Jason W Kimb3212452010-09-30 02:17:26 +0000290}
291
Jim Grosbachaba3de92012-01-18 18:52:16 +0000292bool ARMAsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const {
Jim Grosbach45e50d82011-08-16 17:06:20 +0000293 const uint16_t Thumb1_16bitNopEncoding = 0x46c0; // using MOV r8,r8
294 const uint16_t Thumb2_16bitNopEncoding = 0xbf00; // NOP
Joe Abbey8e72eb72014-09-16 09:18:23 +0000295 const uint32_t ARMv4_NopEncoding = 0xe1a00000; // using MOV r0,r0
Jim Grosbach7ccdb7c2011-11-16 22:40:25 +0000296 const uint32_t ARMv6T2_NopEncoding = 0xe320f000; // NOP
Jim Grosbach87055ed2010-12-08 01:16:55 +0000297 if (isThumb()) {
Joe Abbey8e72eb72014-09-16 09:18:23 +0000298 const uint16_t nopEncoding =
299 hasNOP() ? Thumb2_16bitNopEncoding : Thumb1_16bitNopEncoding;
Jim Grosbach97f1de72010-12-17 19:03:02 +0000300 uint64_t NumNops = Count / 2;
301 for (uint64_t i = 0; i != NumNops; ++i)
Jim Grosbach36e60e92015-06-04 22:24:41 +0000302 OW->write16(nopEncoding);
Jim Grosbach97f1de72010-12-17 19:03:02 +0000303 if (Count & 1)
Jim Grosbach36e60e92015-06-04 22:24:41 +0000304 OW->write8(0);
Jim Grosbach87055ed2010-12-08 01:16:55 +0000305 return true;
306 }
307 // ARM mode
Joe Abbey8e72eb72014-09-16 09:18:23 +0000308 const uint32_t nopEncoding =
309 hasNOP() ? ARMv6T2_NopEncoding : ARMv4_NopEncoding;
Jim Grosbach97f1de72010-12-17 19:03:02 +0000310 uint64_t NumNops = Count / 4;
311 for (uint64_t i = 0; i != NumNops; ++i)
Jim Grosbach36e60e92015-06-04 22:24:41 +0000312 OW->write32(nopEncoding);
Jim Grosbach45e50d82011-08-16 17:06:20 +0000313 // FIXME: should this function return false when unable to write exactly
314 // 'Count' bytes with NOP encodings?
Jim Grosbach97f1de72010-12-17 19:03:02 +0000315 switch (Count % 4) {
Joe Abbey8e72eb72014-09-16 09:18:23 +0000316 default:
317 break; // No leftover bytes to write
318 case 1:
Jim Grosbach36e60e92015-06-04 22:24:41 +0000319 OW->write8(0);
Joe Abbey8e72eb72014-09-16 09:18:23 +0000320 break;
321 case 2:
Jim Grosbach36e60e92015-06-04 22:24:41 +0000322 OW->write16(0);
Joe Abbey8e72eb72014-09-16 09:18:23 +0000323 break;
324 case 3:
Jim Grosbach36e60e92015-06-04 22:24:41 +0000325 OW->write16(0);
326 OW->write8(0xa0);
Joe Abbey8e72eb72014-09-16 09:18:23 +0000327 break;
Jim Grosbach97f1de72010-12-17 19:03:02 +0000328 }
329
Rafael Espindola0ed15432010-10-25 17:50:35 +0000330 return true;
Jim Grosbach58bce992010-09-30 03:20:34 +0000331}
Jason W Kimb3212452010-09-30 02:17:26 +0000332
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000333static uint32_t swapHalfWords(uint32_t Value, bool IsLittleEndian) {
334 if (IsLittleEndian) {
335 // Note that the halfwords are stored high first and low second in thumb;
336 // so we need to swap the fixup value here to map properly.
337 uint32_t Swapped = (Value & 0xFFFF0000) >> 16;
338 Swapped |= (Value & 0x0000FFFF) << 16;
339 return Swapped;
Joe Abbey8e72eb72014-09-16 09:18:23 +0000340 } else
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000341 return Value;
342}
343
344static uint32_t joinHalfWords(uint32_t FirstHalf, uint32_t SecondHalf,
345 bool IsLittleEndian) {
346 uint32_t Value;
347
348 if (IsLittleEndian) {
349 Value = (SecondHalf & 0xFFFF) << 16;
350 Value |= (FirstHalf & 0xFFFF);
351 } else {
352 Value = (SecondHalf & 0xFFFF);
353 Value |= (FirstHalf & 0xFFFF) << 16;
354 }
355
356 return Value;
357}
358
Tim Northover8d67b8e2015-10-02 18:07:18 +0000359unsigned ARMAsmBackend::adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
360 bool IsPCRel, MCContext *Ctx,
361 bool IsLittleEndian,
362 bool IsResolved) const {
Jim Grosbache78031a2012-04-30 22:30:43 +0000363 unsigned Kind = Fixup.getKind();
Jason W Kimfc5c5222010-12-01 22:46:50 +0000364 switch (Kind) {
365 default:
366 llvm_unreachable("Unknown fixup kind!");
Jim Grosbach4416dfa2010-12-17 18:39:10 +0000367 case FK_Data_1:
368 case FK_Data_2:
Jason W Kimfc5c5222010-12-01 22:46:50 +0000369 case FK_Data_4:
Jason W Kimfc5c5222010-12-01 22:46:50 +0000370 return Value;
Saleem Abdulrasoolfc6b85b2014-05-08 01:35:57 +0000371 case FK_SecRel_2:
372 return Value;
Saleem Abdulrasool729c7a02014-05-04 23:13:15 +0000373 case FK_SecRel_4:
374 return Value;
Jason W Kimd5e6e542010-12-03 19:40:23 +0000375 case ARM::fixup_arm_movt_hi16:
Rafael Espindola5904e122014-03-29 06:26:49 +0000376 if (!IsPCRel)
377 Value >>= 16;
Justin Bognerb03fd122016-08-17 05:10:15 +0000378 LLVM_FALLTHROUGH;
Rafael Espindola5904e122014-03-29 06:26:49 +0000379 case ARM::fixup_arm_movw_lo16: {
Jason W Kimd5e6e542010-12-03 19:40:23 +0000380 unsigned Hi4 = (Value & 0xF000) >> 12;
381 unsigned Lo12 = Value & 0x0FFF;
382 // inst{19-16} = Hi4;
383 // inst{11-0} = Lo12;
384 Value = (Hi4 << 16) | (Lo12);
385 return Value;
386 }
Evan Chengd4a5c052011-01-14 02:38:49 +0000387 case ARM::fixup_t2_movt_hi16:
Rafael Espindola5904e122014-03-29 06:26:49 +0000388 if (!IsPCRel)
389 Value >>= 16;
Justin Bognerb03fd122016-08-17 05:10:15 +0000390 LLVM_FALLTHROUGH;
Rafael Espindola5904e122014-03-29 06:26:49 +0000391 case ARM::fixup_t2_movw_lo16: {
Evan Chengd4a5c052011-01-14 02:38:49 +0000392 unsigned Hi4 = (Value & 0xF000) >> 12;
393 unsigned i = (Value & 0x800) >> 11;
394 unsigned Mid3 = (Value & 0x700) >> 8;
395 unsigned Lo8 = Value & 0x0FF;
396 // inst{19-16} = Hi4;
397 // inst{26} = i;
398 // inst{14-12} = Mid3;
399 // inst{7-0} = Lo8;
Jim Grosbachd76f43e2011-09-30 22:02:45 +0000400 Value = (Hi4 << 16) | (i << 26) | (Mid3 << 12) | (Lo8);
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000401 return swapHalfWords(Value, IsLittleEndian);
Evan Chengd4a5c052011-01-14 02:38:49 +0000402 }
Owen Anderson3e6ee1d2010-12-09 01:51:07 +0000403 case ARM::fixup_arm_ldst_pcrel_12:
Jason W Kimfc5c5222010-12-01 22:46:50 +0000404 // ARM PC-relative values are offset by 8.
Owen Anderson3ef19d92010-12-09 20:27:52 +0000405 Value -= 4;
Justin Bognerb03fd122016-08-17 05:10:15 +0000406 LLVM_FALLTHROUGH;
Owen Anderson3e6ee1d2010-12-09 01:51:07 +0000407 case ARM::fixup_t2_ldst_pcrel_12: {
408 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Anderson3ef19d92010-12-09 20:27:52 +0000409 Value -= 4;
Owen Anderson3e6ee1d2010-12-09 01:51:07 +0000410 bool isAdd = true;
Jason W Kimfc5c5222010-12-01 22:46:50 +0000411 if ((int64_t)Value < 0) {
412 Value = -Value;
413 isAdd = false;
414 }
Oliver Stannard9be59af2015-11-17 10:00:43 +0000415 if (Ctx && Value >= 4096) {
416 Ctx->reportError(Fixup.getLoc(), "out of range pc-relative fixup value");
417 return 0;
418 }
Jason W Kimfc5c5222010-12-01 22:46:50 +0000419 Value |= isAdd << 23;
Jim Grosbach3aeb8672010-12-13 19:18:13 +0000420
Owen Anderson3e6ee1d2010-12-09 01:51:07 +0000421 // Same addressing mode as fixup_arm_pcrel_10,
422 // but with 16-bit halfwords swapped.
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000423 if (Kind == ARM::fixup_t2_ldst_pcrel_12)
424 return swapHalfWords(Value, IsLittleEndian);
Jim Grosbach3aeb8672010-12-13 19:18:13 +0000425
Jason W Kimfc5c5222010-12-01 22:46:50 +0000426 return Value;
427 }
Jim Grosbachce2bd8d2010-12-02 00:28:45 +0000428 case ARM::fixup_arm_adr_pcrel_12: {
429 // ARM PC-relative values are offset by 8.
430 Value -= 8;
431 unsigned opc = 4; // bits {24-21}. Default to add: 0b0100
432 if ((int64_t)Value < 0) {
433 Value = -Value;
434 opc = 2; // 0b0010
435 }
Oliver Stannard9be59af2015-11-17 10:00:43 +0000436 if (Ctx && ARM_AM::getSOImmVal(Value) == -1) {
437 Ctx->reportError(Fixup.getLoc(), "out of range pc-relative fixup value");
438 return 0;
439 }
Jim Grosbachce2bd8d2010-12-02 00:28:45 +0000440 // Encode the immediate and shift the opcode into place.
441 return ARM_AM::getSOImmVal(Value) | (opc << 21);
442 }
Jim Grosbache34793e2010-12-14 16:25:15 +0000443
Owen Anderson6d375e52010-12-14 00:36:49 +0000444 case ARM::fixup_t2_adr_pcrel_12: {
445 Value -= 4;
446 unsigned opc = 0;
447 if ((int64_t)Value < 0) {
448 Value = -Value;
449 opc = 5;
450 }
451
452 uint32_t out = (opc << 21);
Owen Anderson8543d4f2011-03-23 22:03:44 +0000453 out |= (Value & 0x800) << 15;
Owen Anderson6d375e52010-12-14 00:36:49 +0000454 out |= (Value & 0x700) << 4;
455 out |= (Value & 0x0FF);
Jim Grosbache34793e2010-12-14 16:25:15 +0000456
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000457 return swapHalfWords(out, IsLittleEndian);
Owen Anderson6d375e52010-12-14 00:36:49 +0000458 }
Jim Grosbache34793e2010-12-14 16:25:15 +0000459
Jason W Kimd2e2f562011-02-04 19:47:15 +0000460 case ARM::fixup_arm_condbranch:
461 case ARM::fixup_arm_uncondbranch:
James Molloyfb5cd602012-03-30 09:15:32 +0000462 case ARM::fixup_arm_uncondbl:
463 case ARM::fixup_arm_condbl:
Jim Grosbach7b811d32012-02-27 21:36:23 +0000464 case ARM::fixup_arm_blx:
Jason W Kimfc5c5222010-12-01 22:46:50 +0000465 // These values don't encode the low two bits since they're always zero.
466 // Offset by 8 just as above.
Joe Abbey8e72eb72014-09-16 09:18:23 +0000467 if (const MCSymbolRefExpr *SRE =
468 dyn_cast<MCSymbolRefExpr>(Fixup.getValue()))
Davide Italiano249c45d2016-03-15 00:25:22 +0000469 if (SRE->getKind() == MCSymbolRefExpr::VK_TLSCALL)
Saleem Abdulrasool6e00ca82014-01-30 04:02:31 +0000470 return 0;
Jim Grosbach9e199462010-12-06 23:57:07 +0000471 return 0xffffff & ((Value - 8) >> 2);
Owen Anderson578074b2010-12-13 19:31:11 +0000472 case ARM::fixup_t2_uncondbranch: {
Owen Anderson235c2762010-12-10 23:02:28 +0000473 Value = Value - 4;
Owen Anderson302d5fd2010-12-09 00:27:41 +0000474 Value >>= 1; // Low bit is not encoded.
Jim Grosbach3aeb8672010-12-13 19:18:13 +0000475
Jim Grosbachf588c512010-12-13 19:25:46 +0000476 uint32_t out = 0;
Joe Abbey8e72eb72014-09-16 09:18:23 +0000477 bool I = Value & 0x800000;
Owen Anderson578074b2010-12-13 19:31:11 +0000478 bool J1 = Value & 0x400000;
479 bool J2 = Value & 0x200000;
480 J1 ^= I;
481 J2 ^= I;
Jim Grosbache34793e2010-12-14 16:25:15 +0000482
Joe Abbey8e72eb72014-09-16 09:18:23 +0000483 out |= I << 26; // S bit
484 out |= !J1 << 13; // J1 bit
485 out |= !J2 << 11; // J2 bit
486 out |= (Value & 0x1FF800) << 5; // imm6 field
487 out |= (Value & 0x0007FF); // imm11 field
Jim Grosbache34793e2010-12-14 16:25:15 +0000488
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000489 return swapHalfWords(out, IsLittleEndian);
Owen Anderson578074b2010-12-13 19:31:11 +0000490 }
491 case ARM::fixup_t2_condbranch: {
492 Value = Value - 4;
493 Value >>= 1; // Low bit is not encoded.
Jim Grosbache34793e2010-12-14 16:25:15 +0000494
Owen Anderson578074b2010-12-13 19:31:11 +0000495 uint64_t out = 0;
Owen Anderson14e41272010-12-09 01:02:09 +0000496 out |= (Value & 0x80000) << 7; // S bit
497 out |= (Value & 0x40000) >> 7; // J2 bit
498 out |= (Value & 0x20000) >> 4; // J1 bit
499 out |= (Value & 0x1F800) << 5; // imm6 field
500 out |= (Value & 0x007FF); // imm11 field
Jim Grosbach3aeb8672010-12-13 19:18:13 +0000501
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000502 return swapHalfWords(out, IsLittleEndian);
Owen Anderson302d5fd2010-12-09 00:27:41 +0000503 }
Jim Grosbach9e199462010-12-06 23:57:07 +0000504 case ARM::fixup_arm_thumb_bl: {
Saleem Abdulrasool077fd252014-01-26 22:29:36 +0000505 // The value doesn't encode the low bit (always zero) and is offset by
506 // four. The 32-bit immediate value is encoded as
507 // imm32 = SignExtend(S:I1:I2:imm10:imm11:0)
508 // where I1 = NOT(J1 ^ S) and I2 = NOT(J2 ^ S).
509 // The value is encoded into disjoint bit positions in the destination
510 // opcode. x = unchanged, I = immediate value bit, S = sign extension bit,
511 // J = either J1 or J2 bit
512 //
513 // BL: xxxxxSIIIIIIIIII xxJxJIIIIIIIIIII
514 //
515 // Note that the halfwords are stored high first, low second; so we need
516 // to transpose the fixup value here to map properly.
517 uint32_t offset = (Value - 4) >> 1;
518 uint32_t signBit = (offset & 0x800000) >> 23;
519 uint32_t I1Bit = (offset & 0x400000) >> 22;
520 uint32_t J1Bit = (I1Bit ^ 0x1) ^ signBit;
521 uint32_t I2Bit = (offset & 0x200000) >> 21;
522 uint32_t J2Bit = (I2Bit ^ 0x1) ^ signBit;
523 uint32_t imm10Bits = (offset & 0x1FF800) >> 11;
524 uint32_t imm11Bits = (offset & 0x000007FF);
NAKAMURA Takumi8018a292013-06-11 06:52:36 +0000525
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000526 uint32_t FirstHalf = (((uint16_t)signBit << 10) | (uint16_t)imm10Bits);
527 uint32_t SecondHalf = (((uint16_t)J1Bit << 13) | ((uint16_t)J2Bit << 11) |
Joe Abbey8e72eb72014-09-16 09:18:23 +0000528 (uint16_t)imm11Bits);
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000529 return joinHalfWords(FirstHalf, SecondHalf, IsLittleEndian);
Bill Wendling3392bfc2010-12-09 00:39:08 +0000530 }
531 case ARM::fixup_arm_thumb_blx: {
Saleem Abdulrasool077fd252014-01-26 22:29:36 +0000532 // The value doesn't encode the low two bits (always zero) and is offset by
533 // four (see fixup_arm_thumb_cp). The 32-bit immediate value is encoded as
534 // imm32 = SignExtend(S:I1:I2:imm10H:imm10L:00)
535 // where I1 = NOT(J1 ^ S) and I2 = NOT(J2 ^ S).
536 // The value is encoded into disjoint bit positions in the destination
537 // opcode. x = unchanged, I = immediate value bit, S = sign extension bit,
538 // J = either J1 or J2 bit, 0 = zero.
539 //
540 // BLX: xxxxxSIIIIIIIIII xxJxJIIIIIIIIII0
541 //
542 // Note that the halfwords are stored high first, low second; so we need
543 // to transpose the fixup value here to map properly.
Tim Northover34956472016-08-25 20:41:30 +0000544 if (Ctx && Value % 4 != 0) {
Tim Northover56048d52016-05-10 21:48:48 +0000545 Ctx->reportError(Fixup.getLoc(), "misaligned ARM call destination");
546 return 0;
547 }
548
549 uint32_t offset = (Value - 4) >> 2;
Joe Abbey8e72eb72014-09-16 09:18:23 +0000550 if (const MCSymbolRefExpr *SRE =
551 dyn_cast<MCSymbolRefExpr>(Fixup.getValue()))
Davide Italiano249c45d2016-03-15 00:25:22 +0000552 if (SRE->getKind() == MCSymbolRefExpr::VK_TLSCALL)
Saleem Abdulrasool6e00ca82014-01-30 04:02:31 +0000553 offset = 0;
Saleem Abdulrasool077fd252014-01-26 22:29:36 +0000554 uint32_t signBit = (offset & 0x400000) >> 22;
555 uint32_t I1Bit = (offset & 0x200000) >> 21;
556 uint32_t J1Bit = (I1Bit ^ 0x1) ^ signBit;
557 uint32_t I2Bit = (offset & 0x100000) >> 20;
558 uint32_t J2Bit = (I2Bit ^ 0x1) ^ signBit;
559 uint32_t imm10HBits = (offset & 0xFFC00) >> 10;
560 uint32_t imm10LBits = (offset & 0x3FF);
NAKAMURA Takumi8018a292013-06-11 06:52:36 +0000561
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000562 uint32_t FirstHalf = (((uint16_t)signBit << 10) | (uint16_t)imm10HBits);
563 uint32_t SecondHalf = (((uint16_t)J1Bit << 13) | ((uint16_t)J2Bit << 11) |
Joe Abbey8e72eb72014-09-16 09:18:23 +0000564 ((uint16_t)imm10LBits) << 1);
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000565 return joinHalfWords(FirstHalf, SecondHalf, IsLittleEndian);
Jim Grosbach9e199462010-12-06 23:57:07 +0000566 }
Tim Northover8d67b8e2015-10-02 18:07:18 +0000567 case ARM::fixup_thumb_adr_pcrel_10:
Bill Wendling8a6449c2010-12-08 01:57:09 +0000568 case ARM::fixup_arm_thumb_cp:
Tim Northover8d67b8e2015-10-02 18:07:18 +0000569 // On CPUs supporting Thumb2, this will be relaxed to an ldr.w, otherwise we
570 // could have an error on our hands.
571 if (Ctx && !STI->getFeatureBits()[ARM::FeatureThumb2] && IsResolved) {
572 const char *FixupDiagnostic = reasonForFixupRelaxation(Fixup, Value);
Oliver Stannard9be59af2015-11-17 10:00:43 +0000573 if (FixupDiagnostic) {
574 Ctx->reportError(Fixup.getLoc(), FixupDiagnostic);
575 return 0;
576 }
Tim Northover8d67b8e2015-10-02 18:07:18 +0000577 }
578 // Offset by 4, and don't encode the low two bits.
579 return ((Value - 4) >> 2) & 0xff;
Jim Grosbach68b27eb2010-12-09 19:50:12 +0000580 case ARM::fixup_arm_thumb_cb: {
Prakhar Bahugunaa27c4a02016-08-16 10:41:56 +0000581 // CB instructions can only branch to offsets in [4, 126] in multiples of 2
582 // so ensure that the raw value LSB is zero and it lies in [2, 130].
583 // An offset of 2 will be relaxed to a NOP.
584 if (Ctx && ((int64_t)Value < 2 || Value > 0x82 || Value & 1)) {
Prakhar Bahuguna15ed7ec2016-08-16 10:41:52 +0000585 Ctx->reportError(Fixup.getLoc(), "out of range pc-relative fixup value");
586 return 0;
587 }
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000588 // Offset by 4 and don't encode the lower bit, which is always 0.
Tim Northover8d67b8e2015-10-02 18:07:18 +0000589 // FIXME: diagnose if no Thumb2
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000590 uint32_t Binary = (Value - 4) >> 1;
Owen Andersonf636a642010-12-14 19:42:53 +0000591 return ((Binary & 0x20) << 4) | ((Binary & 0x1f) << 3);
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000592 }
Jim Grosbache119da12010-12-10 18:21:33 +0000593 case ARM::fixup_arm_thumb_br:
594 // Offset by 4 and don't encode the lower bit, which is always 0.
Bradley Smitha1189102016-01-15 10:26:17 +0000595 if (Ctx && !STI->getFeatureBits()[ARM::FeatureThumb2] &&
596 !STI->getFeatureBits()[ARM::HasV8MBaselineOps]) {
Tim Northover8d67b8e2015-10-02 18:07:18 +0000597 const char *FixupDiagnostic = reasonForFixupRelaxation(Fixup, Value);
Oliver Stannard9be59af2015-11-17 10:00:43 +0000598 if (FixupDiagnostic) {
599 Ctx->reportError(Fixup.getLoc(), FixupDiagnostic);
600 return 0;
601 }
Tim Northover8d67b8e2015-10-02 18:07:18 +0000602 }
Jim Grosbache119da12010-12-10 18:21:33 +0000603 return ((Value - 4) >> 1) & 0x7ff;
Jim Grosbach78485ad2010-12-10 17:13:40 +0000604 case ARM::fixup_arm_thumb_bcc:
605 // Offset by 4 and don't encode the lower bit, which is always 0.
Tim Northover8d67b8e2015-10-02 18:07:18 +0000606 if (Ctx && !STI->getFeatureBits()[ARM::FeatureThumb2]) {
607 const char *FixupDiagnostic = reasonForFixupRelaxation(Fixup, Value);
Oliver Stannard9be59af2015-11-17 10:00:43 +0000608 if (FixupDiagnostic) {
609 Ctx->reportError(Fixup.getLoc(), FixupDiagnostic);
610 return 0;
611 }
Tim Northover8d67b8e2015-10-02 18:07:18 +0000612 }
Jim Grosbach78485ad2010-12-10 17:13:40 +0000613 return ((Value - 4) >> 1) & 0xff;
Jim Grosbach8648c102011-12-19 23:06:24 +0000614 case ARM::fixup_arm_pcrel_10_unscaled: {
615 Value = Value - 8; // ARM fixups offset by an additional word and don't
616 // need to adjust for the half-word ordering.
617 bool isAdd = true;
618 if ((int64_t)Value < 0) {
619 Value = -Value;
620 isAdd = false;
621 }
Jim Grosbach913cc302012-03-30 21:54:22 +0000622 // The value has the low 4 bits encoded in [3:0] and the high 4 in [11:8].
Oliver Stannard9be59af2015-11-17 10:00:43 +0000623 if (Ctx && Value >= 256) {
624 Ctx->reportError(Fixup.getLoc(), "out of range pc-relative fixup value");
625 return 0;
626 }
Jim Grosbach913cc302012-03-30 21:54:22 +0000627 Value = (Value & 0xf) | ((Value & 0xf0) << 4);
Jim Grosbach8648c102011-12-19 23:06:24 +0000628 return Value | (isAdd << 23);
629 }
Jim Grosbach3c685612010-12-08 20:32:07 +0000630 case ARM::fixup_arm_pcrel_10:
Owen Anderson4743d752010-12-10 22:46:47 +0000631 Value = Value - 4; // ARM fixups offset by an additional word and don't
Jim Grosbach3c685612010-12-08 20:32:07 +0000632 // need to adjust for the half-word ordering.
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000633 LLVM_FALLTHROUGH;
Jim Grosbach3c685612010-12-08 20:32:07 +0000634 case ARM::fixup_t2_pcrel_10: {
635 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Anderson4743d752010-12-10 22:46:47 +0000636 Value = Value - 4;
Jason W Kimfc5c5222010-12-01 22:46:50 +0000637 bool isAdd = true;
638 if ((int64_t)Value < 0) {
639 Value = -Value;
640 isAdd = false;
641 }
642 // These values don't encode the low two bits since they're always zero.
643 Value >>= 2;
Oliver Stannard9be59af2015-11-17 10:00:43 +0000644 if (Ctx && Value >= 256) {
645 Ctx->reportError(Fixup.getLoc(), "out of range pc-relative fixup value");
646 return 0;
647 }
Jason W Kimfc5c5222010-12-01 22:46:50 +0000648 Value |= isAdd << 23;
Jim Grosbach3c685612010-12-08 20:32:07 +0000649
Jim Grosbach8648c102011-12-19 23:06:24 +0000650 // Same addressing mode as fixup_arm_pcrel_10, but with 16-bit halfwords
651 // swapped.
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000652 if (Kind == ARM::fixup_t2_pcrel_10)
653 return swapHalfWords(Value, IsLittleEndian);
Jim Grosbach3c685612010-12-08 20:32:07 +0000654
Jason W Kimfc5c5222010-12-01 22:46:50 +0000655 return Value;
656 }
Oliver Stannard65b85382016-01-25 10:26:26 +0000657 case ARM::fixup_arm_pcrel_9:
658 Value = Value - 4; // ARM fixups offset by an additional word and don't
659 // need to adjust for the half-word ordering.
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000660 LLVM_FALLTHROUGH;
Oliver Stannard65b85382016-01-25 10:26:26 +0000661 case ARM::fixup_t2_pcrel_9: {
662 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
663 Value = Value - 4;
664 bool isAdd = true;
665 if ((int64_t)Value < 0) {
666 Value = -Value;
667 isAdd = false;
668 }
669 // These values don't encode the low bit since it's always zero.
670 if (Ctx && (Value & 1)) {
671 Ctx->reportError(Fixup.getLoc(), "invalid value for this fixup");
672 return 0;
673 }
674 Value >>= 1;
675 if (Ctx && Value >= 256) {
676 Ctx->reportError(Fixup.getLoc(), "out of range pc-relative fixup value");
677 return 0;
678 }
679 Value |= isAdd << 23;
680
681 // Same addressing mode as fixup_arm_pcrel_9, but with 16-bit halfwords
682 // swapped.
683 if (Kind == ARM::fixup_t2_pcrel_9)
684 return swapHalfWords(Value, IsLittleEndian);
685
686 return Value;
687 }
James Molloyb876c722016-04-01 09:40:47 +0000688 case ARM::fixup_arm_mod_imm:
689 Value = ARM_AM::getSOImmVal(Value);
690 if (Ctx && Value >> 12) {
691 Ctx->reportError(Fixup.getLoc(), "out of range immediate fixup value");
692 return 0;
693 }
694 return Value;
Jason W Kimfc5c5222010-12-01 22:46:50 +0000695 }
696}
697
Jim Grosbache78031a2012-04-30 22:30:43 +0000698void ARMAsmBackend::processFixupValue(const MCAssembler &Asm,
699 const MCAsmLayout &Layout,
700 const MCFixup &Fixup,
701 const MCFragment *DF,
Rafael Espindola3e3de5e2014-03-28 16:06:09 +0000702 const MCValue &Target, uint64_t &Value,
Jim Grosbache78031a2012-04-30 22:30:43 +0000703 bool &IsResolved) {
704 const MCSymbolRefExpr *A = Target.getSymA();
Rafael Espindola49b85482015-11-04 23:00:39 +0000705 const MCSymbol *Sym = A ? &A->getSymbol() : nullptr;
Tim Northover34956472016-08-25 20:41:30 +0000706 // MachO (the only user of "Value") tries to make .o files that look vaguely
707 // pre-linked, so for MOVW/MOVT and .word relocations they put the Thumb bit
708 // into the addend if possible. Other relocation types don't want this bit
709 // though (branches couldn't encode it if it *was* present, and no other
710 // relocations exist) and it can interfere with checking valid expressions.
711 if ((unsigned)Fixup.getKind() == FK_Data_4 ||
712 (unsigned)Fixup.getKind() == ARM::fixup_arm_movw_lo16 ||
713 (unsigned)Fixup.getKind() == ARM::fixup_arm_movt_hi16 ||
714 (unsigned)Fixup.getKind() == ARM::fixup_t2_movw_lo16 ||
715 (unsigned)Fixup.getKind() == ARM::fixup_t2_movt_hi16) {
Rafael Espindola49b85482015-11-04 23:00:39 +0000716 if (Sym) {
717 if (Asm.isThumbFunc(Sym))
Jim Grosbache78031a2012-04-30 22:30:43 +0000718 Value |= 1;
719 }
720 }
Rafael Espindola49b85482015-11-04 23:00:39 +0000721 if (IsResolved && (unsigned)Fixup.getKind() == ARM::fixup_arm_thumb_bl) {
722 assert(Sym && "How did we resolve this?");
723
724 // If the symbol is external the linker will handle it.
725 // FIXME: Should we handle it as an optimization?
Rafael Espindolae61a9022015-11-05 01:10:15 +0000726
727 // If the symbol is out of range, produce a relocation and hope the
728 // linker can handle it. GNU AS produces an error in this case.
729 if (Sym->isExternal() || Value >= 0x400004)
Rafael Espindola49b85482015-11-04 23:00:39 +0000730 IsResolved = false;
Logan Chiend5c48aa2014-02-05 14:15:16 +0000731 }
Jim Grosbache78031a2012-04-30 22:30:43 +0000732 // We must always generate a relocation for BL/BLX instructions if we have
733 // a symbol to reference, as the linker relies on knowing the destination
734 // symbol's thumb-ness to get interworking right.
735 if (A && ((unsigned)Fixup.getKind() == ARM::fixup_arm_thumb_blx ||
Jim Grosbache78031a2012-04-30 22:30:43 +0000736 (unsigned)Fixup.getKind() == ARM::fixup_arm_blx ||
737 (unsigned)Fixup.getKind() == ARM::fixup_arm_uncondbl ||
738 (unsigned)Fixup.getKind() == ARM::fixup_arm_condbl))
739 IsResolved = false;
740
741 // Try to get the encoded value for the fixup as-if we're mapping it into
742 // the instruction. This allows adjustFixupValue() to issue a diagnostic
743 // if the value aren't invalid.
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000744 (void)adjustFixupValue(Fixup, Value, false, &Asm.getContext(),
Tim Northover8d67b8e2015-10-02 18:07:18 +0000745 IsLittleEndian, IsResolved);
Jim Grosbache78031a2012-04-30 22:30:43 +0000746}
747
Bill Wendlingf09c44c2010-12-07 23:11:00 +0000748/// getFixupKindNumBytes - The number of bytes the fixup may change.
Jim Grosbach9d6d77a2010-11-11 18:04:49 +0000749static unsigned getFixupKindNumBytes(unsigned Kind) {
Jim Grosbach90987142010-11-09 01:37:15 +0000750 switch (Kind) {
Jim Grosbach9e199462010-12-06 23:57:07 +0000751 default:
752 llvm_unreachable("Unknown fixup kind!");
Bill Wendling8a6449c2010-12-08 01:57:09 +0000753
Jim Grosbach4416dfa2010-12-17 18:39:10 +0000754 case FK_Data_1:
Jim Grosbach78485ad2010-12-10 17:13:40 +0000755 case ARM::fixup_arm_thumb_bcc:
Bill Wendling8a6449c2010-12-08 01:57:09 +0000756 case ARM::fixup_arm_thumb_cp:
Jim Grosbach509dc2a2010-12-14 22:28:03 +0000757 case ARM::fixup_thumb_adr_pcrel_10:
Bill Wendling8a6449c2010-12-08 01:57:09 +0000758 return 1;
759
Jim Grosbach4416dfa2010-12-17 18:39:10 +0000760 case FK_Data_2:
Jim Grosbache119da12010-12-10 18:21:33 +0000761 case ARM::fixup_arm_thumb_br:
Jim Grosbach68b27eb2010-12-09 19:50:12 +0000762 case ARM::fixup_arm_thumb_cb:
James Molloyb876c722016-04-01 09:40:47 +0000763 case ARM::fixup_arm_mod_imm:
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000764 return 2;
765
Jim Grosbach8648c102011-12-19 23:06:24 +0000766 case ARM::fixup_arm_pcrel_10_unscaled:
Jim Grosbach9e199462010-12-06 23:57:07 +0000767 case ARM::fixup_arm_ldst_pcrel_12:
768 case ARM::fixup_arm_pcrel_10:
Oliver Stannard65b85382016-01-25 10:26:26 +0000769 case ARM::fixup_arm_pcrel_9:
Jim Grosbach9e199462010-12-06 23:57:07 +0000770 case ARM::fixup_arm_adr_pcrel_12:
James Molloyfb5cd602012-03-30 09:15:32 +0000771 case ARM::fixup_arm_uncondbl:
772 case ARM::fixup_arm_condbl:
Jim Grosbach7b811d32012-02-27 21:36:23 +0000773 case ARM::fixup_arm_blx:
Jason W Kimd2e2f562011-02-04 19:47:15 +0000774 case ARM::fixup_arm_condbranch:
775 case ARM::fixup_arm_uncondbranch:
Jim Grosbach9e199462010-12-06 23:57:07 +0000776 return 3;
Bill Wendling8a6449c2010-12-08 01:57:09 +0000777
778 case FK_Data_4:
Owen Anderson3e6ee1d2010-12-09 01:51:07 +0000779 case ARM::fixup_t2_ldst_pcrel_12:
Owen Anderson578074b2010-12-13 19:31:11 +0000780 case ARM::fixup_t2_condbranch:
781 case ARM::fixup_t2_uncondbranch:
Owen Anderson0f7142d2010-12-08 00:18:36 +0000782 case ARM::fixup_t2_pcrel_10:
Oliver Stannard65b85382016-01-25 10:26:26 +0000783 case ARM::fixup_t2_pcrel_9:
Owen Anderson6d375e52010-12-14 00:36:49 +0000784 case ARM::fixup_t2_adr_pcrel_12:
Jim Grosbach9e199462010-12-06 23:57:07 +0000785 case ARM::fixup_arm_thumb_bl:
Bill Wendling3392bfc2010-12-09 00:39:08 +0000786 case ARM::fixup_arm_thumb_blx:
Evan Chengd4a5c052011-01-14 02:38:49 +0000787 case ARM::fixup_arm_movt_hi16:
788 case ARM::fixup_arm_movw_lo16:
Evan Chengd4a5c052011-01-14 02:38:49 +0000789 case ARM::fixup_t2_movt_hi16:
790 case ARM::fixup_t2_movw_lo16:
Jim Grosbach9e199462010-12-06 23:57:07 +0000791 return 4;
Saleem Abdulrasool729c7a02014-05-04 23:13:15 +0000792
Saleem Abdulrasoolfc6b85b2014-05-08 01:35:57 +0000793 case FK_SecRel_2:
794 return 2;
Saleem Abdulrasool729c7a02014-05-04 23:13:15 +0000795 case FK_SecRel_4:
796 return 4;
Jim Grosbach90987142010-11-09 01:37:15 +0000797 }
798}
799
Christian Pirker2a111602014-03-28 14:35:30 +0000800/// getFixupKindContainerSizeBytes - The number of bytes of the
801/// container involved in big endian.
802static unsigned getFixupKindContainerSizeBytes(unsigned Kind) {
803 switch (Kind) {
804 default:
805 llvm_unreachable("Unknown fixup kind!");
806
807 case FK_Data_1:
808 return 1;
809 case FK_Data_2:
810 return 2;
811 case FK_Data_4:
812 return 4;
813
814 case ARM::fixup_arm_thumb_bcc:
815 case ARM::fixup_arm_thumb_cp:
816 case ARM::fixup_thumb_adr_pcrel_10:
817 case ARM::fixup_arm_thumb_br:
818 case ARM::fixup_arm_thumb_cb:
819 // Instruction size is 2 bytes.
820 return 2;
821
822 case ARM::fixup_arm_pcrel_10_unscaled:
823 case ARM::fixup_arm_ldst_pcrel_12:
824 case ARM::fixup_arm_pcrel_10:
825 case ARM::fixup_arm_adr_pcrel_12:
826 case ARM::fixup_arm_uncondbl:
827 case ARM::fixup_arm_condbl:
828 case ARM::fixup_arm_blx:
829 case ARM::fixup_arm_condbranch:
830 case ARM::fixup_arm_uncondbranch:
831 case ARM::fixup_t2_ldst_pcrel_12:
832 case ARM::fixup_t2_condbranch:
833 case ARM::fixup_t2_uncondbranch:
834 case ARM::fixup_t2_pcrel_10:
835 case ARM::fixup_t2_adr_pcrel_12:
836 case ARM::fixup_arm_thumb_bl:
837 case ARM::fixup_arm_thumb_blx:
838 case ARM::fixup_arm_movt_hi16:
839 case ARM::fixup_arm_movw_lo16:
Christian Pirker2a111602014-03-28 14:35:30 +0000840 case ARM::fixup_t2_movt_hi16:
841 case ARM::fixup_t2_movw_lo16:
James Molloyb876c722016-04-01 09:40:47 +0000842 case ARM::fixup_arm_mod_imm:
Christian Pirker2a111602014-03-28 14:35:30 +0000843 // Instruction size is 4 bytes.
844 return 4;
845 }
846}
847
Benjamin Kramer07ea85a2012-11-24 14:36:43 +0000848void ARMAsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
Rafael Espindola5904e122014-03-29 06:26:49 +0000849 unsigned DataSize, uint64_t Value,
850 bool IsPCRel) const {
Jim Grosbach9d6d77a2010-11-11 18:04:49 +0000851 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
Tim Northover8d67b8e2015-10-02 18:07:18 +0000852 Value =
853 adjustFixupValue(Fixup, Value, IsPCRel, nullptr, IsLittleEndian, true);
Joe Abbey8e72eb72014-09-16 09:18:23 +0000854 if (!Value)
855 return; // Doesn't change encoding.
Jim Grosbach90987142010-11-09 01:37:15 +0000856
Bill Wendlingf09c44c2010-12-07 23:11:00 +0000857 unsigned Offset = Fixup.getOffset();
858 assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
859
Christian Pirker2a111602014-03-28 14:35:30 +0000860 // Used to point to big endian bytes.
861 unsigned FullSizeBytes;
Christian Pirker875629f2014-05-20 09:24:37 +0000862 if (!IsLittleEndian) {
Christian Pirker2a111602014-03-28 14:35:30 +0000863 FullSizeBytes = getFixupKindContainerSizeBytes(Fixup.getKind());
Christian Pirker875629f2014-05-20 09:24:37 +0000864 assert((Offset + FullSizeBytes) <= DataSize && "Invalid fixup size!");
865 assert(NumBytes <= FullSizeBytes && "Invalid fixup size!");
866 }
Christian Pirker2a111602014-03-28 14:35:30 +0000867
Benjamin Kramer07ea85a2012-11-24 14:36:43 +0000868 // For each byte of the fragment that the fixup touches, mask in the bits from
869 // the fixup value. The Value has been "split up" into the appropriate
870 // bitfields above.
Christian Pirker2a111602014-03-28 14:35:30 +0000871 for (unsigned i = 0; i != NumBytes; ++i) {
872 unsigned Idx = IsLittleEndian ? i : (FullSizeBytes - 1 - i);
873 Data[Offset + Idx] |= uint8_t((Value >> (i * 8)) & 0xff);
874 }
Jason W Kimb3212452010-09-30 02:17:26 +0000875}
Bill Wendling721724e2010-12-07 23:05:20 +0000876
Tim Northoverf8e47e42015-10-28 22:56:36 +0000877namespace CU {
878
879/// \brief Compact unwind encoding values.
880enum CompactUnwindEncodings {
881 UNWIND_ARM_MODE_MASK = 0x0F000000,
882 UNWIND_ARM_MODE_FRAME = 0x01000000,
883 UNWIND_ARM_MODE_FRAME_D = 0x02000000,
884 UNWIND_ARM_MODE_DWARF = 0x04000000,
885
886 UNWIND_ARM_FRAME_STACK_ADJUST_MASK = 0x00C00000,
887
888 UNWIND_ARM_FRAME_FIRST_PUSH_R4 = 0x00000001,
889 UNWIND_ARM_FRAME_FIRST_PUSH_R5 = 0x00000002,
890 UNWIND_ARM_FRAME_FIRST_PUSH_R6 = 0x00000004,
891
892 UNWIND_ARM_FRAME_SECOND_PUSH_R8 = 0x00000008,
893 UNWIND_ARM_FRAME_SECOND_PUSH_R9 = 0x00000010,
894 UNWIND_ARM_FRAME_SECOND_PUSH_R10 = 0x00000020,
895 UNWIND_ARM_FRAME_SECOND_PUSH_R11 = 0x00000040,
896 UNWIND_ARM_FRAME_SECOND_PUSH_R12 = 0x00000080,
897
898 UNWIND_ARM_FRAME_D_REG_COUNT_MASK = 0x00000F00,
899
900 UNWIND_ARM_DWARF_SECTION_OFFSET = 0x00FFFFFF
901};
902
903} // end CU namespace
904
905/// Generate compact unwind encoding for the function based on the CFI
906/// instructions. If the CFI instructions describe a frame that cannot be
907/// encoded in compact unwind, the method returns UNWIND_ARM_MODE_DWARF which
908/// tells the runtime to fallback and unwind using dwarf.
909uint32_t ARMAsmBackendDarwin::generateCompactUnwindEncoding(
910 ArrayRef<MCCFIInstruction> Instrs) const {
911 DEBUG_WITH_TYPE("compact-unwind", llvm::dbgs() << "generateCU()\n");
912 // Only armv7k uses CFI based unwinding.
913 if (Subtype != MachO::CPU_SUBTYPE_ARM_V7K)
914 return 0;
915 // No .cfi directives means no frame.
916 if (Instrs.empty())
917 return 0;
918 // Start off assuming CFA is at SP+0.
919 int CFARegister = ARM::SP;
920 int CFARegisterOffset = 0;
921 // Mark savable registers as initially unsaved
922 DenseMap<unsigned, int> RegOffsets;
923 int FloatRegCount = 0;
924 // Process each .cfi directive and build up compact unwind info.
925 for (size_t i = 0, e = Instrs.size(); i != e; ++i) {
926 int Reg;
927 const MCCFIInstruction &Inst = Instrs[i];
928 switch (Inst.getOperation()) {
929 case MCCFIInstruction::OpDefCfa: // DW_CFA_def_cfa
930 CFARegisterOffset = -Inst.getOffset();
931 CFARegister = MRI.getLLVMRegNum(Inst.getRegister(), true);
932 break;
933 case MCCFIInstruction::OpDefCfaOffset: // DW_CFA_def_cfa_offset
934 CFARegisterOffset = -Inst.getOffset();
935 break;
936 case MCCFIInstruction::OpDefCfaRegister: // DW_CFA_def_cfa_register
937 CFARegister = MRI.getLLVMRegNum(Inst.getRegister(), true);
938 break;
939 case MCCFIInstruction::OpOffset: // DW_CFA_offset
940 Reg = MRI.getLLVMRegNum(Inst.getRegister(), true);
941 if (ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
942 RegOffsets[Reg] = Inst.getOffset();
943 else if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg)) {
944 RegOffsets[Reg] = Inst.getOffset();
945 ++FloatRegCount;
946 } else {
947 DEBUG_WITH_TYPE("compact-unwind",
948 llvm::dbgs() << ".cfi_offset on unknown register="
949 << Inst.getRegister() << "\n");
950 return CU::UNWIND_ARM_MODE_DWARF;
951 }
952 break;
953 case MCCFIInstruction::OpRelOffset: // DW_CFA_advance_loc
954 // Ignore
955 break;
956 default:
957 // Directive not convertable to compact unwind, bail out.
958 DEBUG_WITH_TYPE("compact-unwind",
959 llvm::dbgs()
960 << "CFI directive not compatiable with comact "
961 "unwind encoding, opcode=" << Inst.getOperation()
962 << "\n");
963 return CU::UNWIND_ARM_MODE_DWARF;
964 break;
965 }
966 }
967
968 // If no frame set up, return no unwind info.
969 if ((CFARegister == ARM::SP) && (CFARegisterOffset == 0))
970 return 0;
971
972 // Verify standard frame (lr/r7) was used.
973 if (CFARegister != ARM::R7) {
974 DEBUG_WITH_TYPE("compact-unwind", llvm::dbgs() << "frame register is "
975 << CFARegister
976 << " instead of r7\n");
977 return CU::UNWIND_ARM_MODE_DWARF;
978 }
979 int StackAdjust = CFARegisterOffset - 8;
980 if (RegOffsets.lookup(ARM::LR) != (-4 - StackAdjust)) {
981 DEBUG_WITH_TYPE("compact-unwind",
982 llvm::dbgs()
983 << "LR not saved as standard frame, StackAdjust="
984 << StackAdjust
985 << ", CFARegisterOffset=" << CFARegisterOffset
986 << ", lr save at offset=" << RegOffsets[14] << "\n");
987 return CU::UNWIND_ARM_MODE_DWARF;
988 }
989 if (RegOffsets.lookup(ARM::R7) != (-8 - StackAdjust)) {
990 DEBUG_WITH_TYPE("compact-unwind",
991 llvm::dbgs() << "r7 not saved as standard frame\n");
992 return CU::UNWIND_ARM_MODE_DWARF;
993 }
994 uint32_t CompactUnwindEncoding = CU::UNWIND_ARM_MODE_FRAME;
995
996 // If var-args are used, there may be a stack adjust required.
997 switch (StackAdjust) {
998 case 0:
999 break;
1000 case 4:
1001 CompactUnwindEncoding |= 0x00400000;
1002 break;
1003 case 8:
1004 CompactUnwindEncoding |= 0x00800000;
1005 break;
1006 case 12:
1007 CompactUnwindEncoding |= 0x00C00000;
1008 break;
1009 default:
1010 DEBUG_WITH_TYPE("compact-unwind", llvm::dbgs()
1011 << ".cfi_def_cfa stack adjust ("
1012 << StackAdjust << ") out of range\n");
1013 return CU::UNWIND_ARM_MODE_DWARF;
1014 }
1015
1016 // If r6 is saved, it must be right below r7.
1017 static struct {
1018 unsigned Reg;
1019 unsigned Encoding;
1020 } GPRCSRegs[] = {{ARM::R6, CU::UNWIND_ARM_FRAME_FIRST_PUSH_R6},
1021 {ARM::R5, CU::UNWIND_ARM_FRAME_FIRST_PUSH_R5},
1022 {ARM::R4, CU::UNWIND_ARM_FRAME_FIRST_PUSH_R4},
1023 {ARM::R12, CU::UNWIND_ARM_FRAME_SECOND_PUSH_R12},
1024 {ARM::R11, CU::UNWIND_ARM_FRAME_SECOND_PUSH_R11},
1025 {ARM::R10, CU::UNWIND_ARM_FRAME_SECOND_PUSH_R10},
1026 {ARM::R9, CU::UNWIND_ARM_FRAME_SECOND_PUSH_R9},
1027 {ARM::R8, CU::UNWIND_ARM_FRAME_SECOND_PUSH_R8}};
1028
1029 int CurOffset = -8 - StackAdjust;
1030 for (auto CSReg : GPRCSRegs) {
1031 auto Offset = RegOffsets.find(CSReg.Reg);
1032 if (Offset == RegOffsets.end())
1033 continue;
1034
1035 int RegOffset = Offset->second;
1036 if (RegOffset != CurOffset - 4) {
1037 DEBUG_WITH_TYPE("compact-unwind",
1038 llvm::dbgs() << MRI.getName(CSReg.Reg) << " saved at "
1039 << RegOffset << " but only supported at "
1040 << CurOffset << "\n");
1041 return CU::UNWIND_ARM_MODE_DWARF;
1042 }
1043 CompactUnwindEncoding |= CSReg.Encoding;
1044 CurOffset -= 4;
1045 }
1046
1047 // If no floats saved, we are done.
1048 if (FloatRegCount == 0)
1049 return CompactUnwindEncoding;
1050
1051 // Switch mode to include D register saving.
1052 CompactUnwindEncoding &= ~CU::UNWIND_ARM_MODE_MASK;
1053 CompactUnwindEncoding |= CU::UNWIND_ARM_MODE_FRAME_D;
1054
1055 // FIXME: supporting more than 4 saved D-registers compactly would be trivial,
1056 // but needs coordination with the linker and libunwind.
1057 if (FloatRegCount > 4) {
1058 DEBUG_WITH_TYPE("compact-unwind",
1059 llvm::dbgs() << "unsupported number of D registers saved ("
1060 << FloatRegCount << ")\n");
1061 return CU::UNWIND_ARM_MODE_DWARF;
1062 }
1063
1064 // Floating point registers must either be saved sequentially, or we defer to
1065 // DWARF. No gaps allowed here so check that each saved d-register is
1066 // precisely where it should be.
1067 static unsigned FPRCSRegs[] = { ARM::D8, ARM::D10, ARM::D12, ARM::D14 };
1068 for (int Idx = FloatRegCount - 1; Idx >= 0; --Idx) {
1069 auto Offset = RegOffsets.find(FPRCSRegs[Idx]);
1070 if (Offset == RegOffsets.end()) {
1071 DEBUG_WITH_TYPE("compact-unwind",
1072 llvm::dbgs() << FloatRegCount << " D-regs saved, but "
1073 << MRI.getName(FPRCSRegs[Idx])
1074 << " not saved\n");
1075 return CU::UNWIND_ARM_MODE_DWARF;
1076 } else if (Offset->second != CurOffset - 8) {
1077 DEBUG_WITH_TYPE("compact-unwind",
1078 llvm::dbgs() << FloatRegCount << " D-regs saved, but "
1079 << MRI.getName(FPRCSRegs[Idx])
1080 << " saved at " << Offset->second
1081 << ", expected at " << CurOffset - 8
1082 << "\n");
1083 return CU::UNWIND_ARM_MODE_DWARF;
1084 }
1085 CurOffset -= 8;
1086 }
1087
1088 return CompactUnwindEncoding | ((FloatRegCount - 1) << 8);
1089}
1090
Vedant Kumar366dd9fd2015-08-21 21:52:48 +00001091static MachO::CPUSubTypeARM getMachOSubTypeFromArch(StringRef Arch) {
Chandler Carruthbb47b9a2015-08-30 02:09:48 +00001092 unsigned AK = ARM::parseArch(Arch);
Vedant Kumar366dd9fd2015-08-21 21:52:48 +00001093 switch (AK) {
1094 default:
1095 return MachO::CPU_SUBTYPE_ARM_V7;
1096 case ARM::AK_ARMV4T:
1097 return MachO::CPU_SUBTYPE_ARM_V4T;
Artyom Skrobov2c2f3782015-11-12 15:51:41 +00001098 case ARM::AK_ARMV5T:
1099 case ARM::AK_ARMV5TE:
1100 case ARM::AK_ARMV5TEJ:
1101 return MachO::CPU_SUBTYPE_ARM_V5;
Vedant Kumar366dd9fd2015-08-21 21:52:48 +00001102 case ARM::AK_ARMV6:
1103 case ARM::AK_ARMV6K:
1104 return MachO::CPU_SUBTYPE_ARM_V6;
Artyom Skrobov2c2f3782015-11-12 15:51:41 +00001105 case ARM::AK_ARMV7A:
Vedant Kumar366dd9fd2015-08-21 21:52:48 +00001106 return MachO::CPU_SUBTYPE_ARM_V7;
1107 case ARM::AK_ARMV7S:
1108 return MachO::CPU_SUBTYPE_ARM_V7S;
1109 case ARM::AK_ARMV7K:
1110 return MachO::CPU_SUBTYPE_ARM_V7K;
1111 case ARM::AK_ARMV6M:
Vedant Kumar366dd9fd2015-08-21 21:52:48 +00001112 return MachO::CPU_SUBTYPE_ARM_V6M;
1113 case ARM::AK_ARMV7M:
1114 return MachO::CPU_SUBTYPE_ARM_V7M;
1115 case ARM::AK_ARMV7EM:
1116 return MachO::CPU_SUBTYPE_ARM_V7EM;
1117 }
1118}
1119
Bill Wendling58e2d3d2013-09-09 02:37:14 +00001120MCAsmBackend *llvm::createARMAsmBackend(const Target &T,
Daniel Sanders418caf52015-06-10 10:35:34 +00001121 const MCRegisterInfo &MRI,
Daniel Sanders50f17232015-09-15 16:17:27 +00001122 const Triple &TheTriple, StringRef CPU,
Joel Jones373d7d32016-07-25 17:18:28 +00001123 const MCTargetOptions &Options,
Daniel Sanders418caf52015-06-10 10:35:34 +00001124 bool isLittle) {
Daniel Sanders50f17232015-09-15 16:17:27 +00001125 switch (TheTriple.getObjectFormat()) {
Joe Abbey8e72eb72014-09-16 09:18:23 +00001126 default:
1127 llvm_unreachable("unsupported object format");
Daniel Sanders50f17232015-09-15 16:17:27 +00001128 case Triple::MachO: {
1129 MachO::CPUSubTypeARM CS = getMachOSubTypeFromArch(TheTriple.getArchName());
Tim Northoverf8e47e42015-10-28 22:56:36 +00001130 return new ARMAsmBackendDarwin(T, TheTriple, MRI, CS);
Owen Anderson975ddf82011-04-01 21:07:39 +00001131 }
Daniel Sanders50f17232015-09-15 16:17:27 +00001132 case Triple::COFF:
1133 assert(TheTriple.isOSWindows() && "non-Windows ARM COFF is not supported");
1134 return new ARMAsmBackendWinCOFF(T, TheTriple);
1135 case Triple::ELF:
1136 assert(TheTriple.isOSBinFormatELF() && "using ELF for non-ELF target");
1137 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TheTriple.getOS());
1138 return new ARMAsmBackendELF(T, TheTriple, OSABI, isLittle);
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +00001139 }
Jason W Kimb3212452010-09-30 02:17:26 +00001140}
Christian Pirker2a111602014-03-28 14:35:30 +00001141
Christian Pirkerdc9ff752014-04-01 15:19:30 +00001142MCAsmBackend *llvm::createARMLEAsmBackend(const Target &T,
Christian Pirker2a111602014-03-28 14:35:30 +00001143 const MCRegisterInfo &MRI,
Joel Jones373d7d32016-07-25 17:18:28 +00001144 const Triple &TT, StringRef CPU,
1145 const MCTargetOptions &Options) {
1146 return createARMAsmBackend(T, MRI, TT, CPU, Options, true);
Christian Pirker2a111602014-03-28 14:35:30 +00001147}
1148
Christian Pirkerdc9ff752014-04-01 15:19:30 +00001149MCAsmBackend *llvm::createARMBEAsmBackend(const Target &T,
Christian Pirker2a111602014-03-28 14:35:30 +00001150 const MCRegisterInfo &MRI,
Joel Jones373d7d32016-07-25 17:18:28 +00001151 const Triple &TT, StringRef CPU,
1152 const MCTargetOptions &Options) {
1153 return createARMAsmBackend(T, MRI, TT, CPU, Options, false);
Christian Pirker2a111602014-03-28 14:35:30 +00001154}
1155
Christian Pirkerdc9ff752014-04-01 15:19:30 +00001156MCAsmBackend *llvm::createThumbLEAsmBackend(const Target &T,
Joe Abbey8e72eb72014-09-16 09:18:23 +00001157 const MCRegisterInfo &MRI,
Joel Jones373d7d32016-07-25 17:18:28 +00001158 const Triple &TT, StringRef CPU,
1159 const MCTargetOptions &Options) {
1160 return createARMAsmBackend(T, MRI, TT, CPU, Options, true);
Christian Pirker2a111602014-03-28 14:35:30 +00001161}
1162
Christian Pirkerdc9ff752014-04-01 15:19:30 +00001163MCAsmBackend *llvm::createThumbBEAsmBackend(const Target &T,
Joe Abbey8e72eb72014-09-16 09:18:23 +00001164 const MCRegisterInfo &MRI,
Joel Jones373d7d32016-07-25 17:18:28 +00001165 const Triple &TT, StringRef CPU,
1166 const MCTargetOptions &Options) {
1167 return createARMAsmBackend(T, MRI, TT, CPU, Options, false);
Christian Pirker2a111602014-03-28 14:35:30 +00001168}