LLVM 18.0.0git
ARMAsmParser.cpp
Go to the documentation of this file.
1//===- ARMAsmParser.cpp - Parse ARM assembly to MCInst instructions -------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "ARMBaseInstrInfo.h"
10#include "ARMFeatures.h"
17#include "Utils/ARMBaseInfo.h"
18#include "llvm/ADT/APFloat.h"
19#include "llvm/ADT/APInt.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/SmallSet.h"
23#include "llvm/ADT/StringMap.h"
24#include "llvm/ADT/StringRef.h"
25#include "llvm/ADT/StringSet.h"
27#include "llvm/ADT/Twine.h"
28#include "llvm/MC/MCContext.h"
29#include "llvm/MC/MCExpr.h"
30#include "llvm/MC/MCInst.h"
31#include "llvm/MC/MCInstrDesc.h"
32#include "llvm/MC/MCInstrInfo.h"
40#include "llvm/MC/MCSection.h"
41#include "llvm/MC/MCStreamer.h"
43#include "llvm/MC/MCSymbol.h"
52#include "llvm/Support/SMLoc.h"
57#include <algorithm>
58#include <cassert>
59#include <cstddef>
60#include <cstdint>
61#include <iterator>
62#include <limits>
63#include <memory>
64#include <string>
65#include <utility>
66#include <vector>
67
68#define DEBUG_TYPE "asm-parser"
69
70using namespace llvm;
71
72namespace llvm {
77};
78extern const ARMInstrTable ARMDescs;
79} // end namespace llvm
80
81namespace {
82
83enum class ImplicitItModeTy { Always, Never, ARMOnly, ThumbOnly };
84
85static cl::opt<ImplicitItModeTy> ImplicitItMode(
86 "arm-implicit-it", cl::init(ImplicitItModeTy::ARMOnly),
87 cl::desc("Allow conditional instructions outdside of an IT block"),
88 cl::values(clEnumValN(ImplicitItModeTy::Always, "always",
89 "Accept in both ISAs, emit implicit ITs in Thumb"),
90 clEnumValN(ImplicitItModeTy::Never, "never",
91 "Warn in ARM, reject in Thumb"),
92 clEnumValN(ImplicitItModeTy::ARMOnly, "arm",
93 "Accept in ARM, reject in Thumb"),
94 clEnumValN(ImplicitItModeTy::ThumbOnly, "thumb",
95 "Warn in ARM, emit implicit ITs in Thumb")));
96
97static cl::opt<bool> AddBuildAttributes("arm-add-build-attributes",
98 cl::init(false));
99
100enum VectorLaneTy { NoLanes, AllLanes, IndexedLane };
101
102static inline unsigned extractITMaskBit(unsigned Mask, unsigned Position) {
103 // Position==0 means we're not in an IT block at all. Position==1
104 // means we want the first state bit, which is always 0 (Then).
105 // Position==2 means we want the second state bit, stored at bit 3
106 // of Mask, and so on downwards. So (5 - Position) will shift the
107 // right bit down to bit 0, including the always-0 bit at bit 4 for
108 // the mandatory initial Then.
109 return (Mask >> (5 - Position) & 1);
110}
111
112class UnwindContext {
113 using Locs = SmallVector<SMLoc, 4>;
114
115 MCAsmParser &Parser;
116 Locs FnStartLocs;
117 Locs CantUnwindLocs;
118 Locs PersonalityLocs;
119 Locs PersonalityIndexLocs;
120 Locs HandlerDataLocs;
121 int FPReg;
122
123public:
124 UnwindContext(MCAsmParser &P) : Parser(P), FPReg(ARM::SP) {}
125
126 bool hasFnStart() const { return !FnStartLocs.empty(); }
127 bool cantUnwind() const { return !CantUnwindLocs.empty(); }
128 bool hasHandlerData() const { return !HandlerDataLocs.empty(); }
129
130 bool hasPersonality() const {
131 return !(PersonalityLocs.empty() && PersonalityIndexLocs.empty());
132 }
133
134 void recordFnStart(SMLoc L) { FnStartLocs.push_back(L); }
135 void recordCantUnwind(SMLoc L) { CantUnwindLocs.push_back(L); }
136 void recordPersonality(SMLoc L) { PersonalityLocs.push_back(L); }
137 void recordHandlerData(SMLoc L) { HandlerDataLocs.push_back(L); }
138 void recordPersonalityIndex(SMLoc L) { PersonalityIndexLocs.push_back(L); }
139
140 void saveFPReg(int Reg) { FPReg = Reg; }
141 int getFPReg() const { return FPReg; }
142
143 void emitFnStartLocNotes() const {
144 for (const SMLoc &Loc : FnStartLocs)
145 Parser.Note(Loc, ".fnstart was specified here");
146 }
147
148 void emitCantUnwindLocNotes() const {
149 for (const SMLoc &Loc : CantUnwindLocs)
150 Parser.Note(Loc, ".cantunwind was specified here");
151 }
152
153 void emitHandlerDataLocNotes() const {
154 for (const SMLoc &Loc : HandlerDataLocs)
155 Parser.Note(Loc, ".handlerdata was specified here");
156 }
157
158 void emitPersonalityLocNotes() const {
159 for (Locs::const_iterator PI = PersonalityLocs.begin(),
160 PE = PersonalityLocs.end(),
161 PII = PersonalityIndexLocs.begin(),
162 PIE = PersonalityIndexLocs.end();
163 PI != PE || PII != PIE;) {
164 if (PI != PE && (PII == PIE || PI->getPointer() < PII->getPointer()))
165 Parser.Note(*PI++, ".personality was specified here");
166 else if (PII != PIE && (PI == PE || PII->getPointer() < PI->getPointer()))
167 Parser.Note(*PII++, ".personalityindex was specified here");
168 else
169 llvm_unreachable(".personality and .personalityindex cannot be "
170 "at the same location");
171 }
172 }
173
174 void reset() {
175 FnStartLocs = Locs();
176 CantUnwindLocs = Locs();
177 PersonalityLocs = Locs();
178 HandlerDataLocs = Locs();
179 PersonalityIndexLocs = Locs();
180 FPReg = ARM::SP;
181 }
182};
183
184// Various sets of ARM instruction mnemonics which are used by the asm parser
185class ARMMnemonicSets {
186 StringSet<> CDE;
187 StringSet<> CDEWithVPTSuffix;
188public:
189 ARMMnemonicSets(const MCSubtargetInfo &STI);
190
191 /// Returns true iff a given mnemonic is a CDE instruction
192 bool isCDEInstr(StringRef Mnemonic) {
193 // Quick check before searching the set
194 if (!Mnemonic.startswith("cx") && !Mnemonic.startswith("vcx"))
195 return false;
196 return CDE.count(Mnemonic);
197 }
198
199 /// Returns true iff a given mnemonic is a VPT-predicable CDE instruction
200 /// (possibly with a predication suffix "e" or "t")
201 bool isVPTPredicableCDEInstr(StringRef Mnemonic) {
202 if (!Mnemonic.startswith("vcx"))
203 return false;
204 return CDEWithVPTSuffix.count(Mnemonic);
205 }
206
207 /// Returns true iff a given mnemonic is an IT-predicable CDE instruction
208 /// (possibly with a condition suffix)
209 bool isITPredicableCDEInstr(StringRef Mnemonic) {
210 if (!Mnemonic.startswith("cx"))
211 return false;
212 return Mnemonic.startswith("cx1a") || Mnemonic.startswith("cx1da") ||
213 Mnemonic.startswith("cx2a") || Mnemonic.startswith("cx2da") ||
214 Mnemonic.startswith("cx3a") || Mnemonic.startswith("cx3da");
215 }
216
217 /// Return true iff a given mnemonic is an integer CDE instruction with
218 /// dual-register destination
219 bool isCDEDualRegInstr(StringRef Mnemonic) {
220 if (!Mnemonic.startswith("cx"))
221 return false;
222 return Mnemonic == "cx1d" || Mnemonic == "cx1da" ||
223 Mnemonic == "cx2d" || Mnemonic == "cx2da" ||
224 Mnemonic == "cx3d" || Mnemonic == "cx3da";
225 }
226};
227
228ARMMnemonicSets::ARMMnemonicSets(const MCSubtargetInfo &STI) {
229 for (StringRef Mnemonic: { "cx1", "cx1a", "cx1d", "cx1da",
230 "cx2", "cx2a", "cx2d", "cx2da",
231 "cx3", "cx3a", "cx3d", "cx3da", })
232 CDE.insert(Mnemonic);
233 for (StringRef Mnemonic :
234 {"vcx1", "vcx1a", "vcx2", "vcx2a", "vcx3", "vcx3a"}) {
235 CDE.insert(Mnemonic);
236 CDEWithVPTSuffix.insert(Mnemonic);
237 CDEWithVPTSuffix.insert(std::string(Mnemonic) + "t");
238 CDEWithVPTSuffix.insert(std::string(Mnemonic) + "e");
239 }
240}
241
242class ARMAsmParser : public MCTargetAsmParser {
243 const MCRegisterInfo *MRI;
244 UnwindContext UC;
245 ARMMnemonicSets MS;
246
247 ARMTargetStreamer &getTargetStreamer() {
248 assert(getParser().getStreamer().getTargetStreamer() &&
249 "do not have a target streamer");
251 return static_cast<ARMTargetStreamer &>(TS);
252 }
253
254 // Map of register aliases registers via the .req directive.
255 StringMap<unsigned> RegisterReqs;
256
257 bool NextSymbolIsThumb;
258
259 bool useImplicitITThumb() const {
260 return ImplicitItMode == ImplicitItModeTy::Always ||
261 ImplicitItMode == ImplicitItModeTy::ThumbOnly;
262 }
263
264 bool useImplicitITARM() const {
265 return ImplicitItMode == ImplicitItModeTy::Always ||
266 ImplicitItMode == ImplicitItModeTy::ARMOnly;
267 }
268
269 struct {
270 ARMCC::CondCodes Cond; // Condition for IT block.
271 unsigned Mask:4; // Condition mask for instructions.
272 // Starting at first 1 (from lsb).
273 // '1' condition as indicated in IT.
274 // '0' inverse of condition (else).
275 // Count of instructions in IT block is
276 // 4 - trailingzeroes(mask)
277 // Note that this does not have the same encoding
278 // as in the IT instruction, which also depends
279 // on the low bit of the condition code.
280
281 unsigned CurPosition; // Current position in parsing of IT
282 // block. In range [0,4], with 0 being the IT
283 // instruction itself. Initialized according to
284 // count of instructions in block. ~0U if no
285 // active IT block.
286
287 bool IsExplicit; // true - The IT instruction was present in the
288 // input, we should not modify it.
289 // false - The IT instruction was added
290 // implicitly, we can extend it if that
291 // would be legal.
292 } ITState;
293
294 SmallVector<MCInst, 4> PendingConditionalInsts;
295
296 void flushPendingInstructions(MCStreamer &Out) override {
297 if (!inImplicitITBlock()) {
298 assert(PendingConditionalInsts.size() == 0);
299 return;
300 }
301
302 // Emit the IT instruction
303 MCInst ITInst;
304 ITInst.setOpcode(ARM::t2IT);
305 ITInst.addOperand(MCOperand::createImm(ITState.Cond));
306 ITInst.addOperand(MCOperand::createImm(ITState.Mask));
307 Out.emitInstruction(ITInst, getSTI());
308
309 // Emit the conditional instructions
310 assert(PendingConditionalInsts.size() <= 4);
311 for (const MCInst &Inst : PendingConditionalInsts) {
312 Out.emitInstruction(Inst, getSTI());
313 }
314 PendingConditionalInsts.clear();
315
316 // Clear the IT state
317 ITState.Mask = 0;
318 ITState.CurPosition = ~0U;
319 }
320
321 bool inITBlock() { return ITState.CurPosition != ~0U; }
322 bool inExplicitITBlock() { return inITBlock() && ITState.IsExplicit; }
323 bool inImplicitITBlock() { return inITBlock() && !ITState.IsExplicit; }
324
325 bool lastInITBlock() {
326 return ITState.CurPosition == 4 - (unsigned)llvm::countr_zero(ITState.Mask);
327 }
328
329 void forwardITPosition() {
330 if (!inITBlock()) return;
331 // Move to the next instruction in the IT block, if there is one. If not,
332 // mark the block as done, except for implicit IT blocks, which we leave
333 // open until we find an instruction that can't be added to it.
334 unsigned TZ = llvm::countr_zero(ITState.Mask);
335 if (++ITState.CurPosition == 5 - TZ && ITState.IsExplicit)
336 ITState.CurPosition = ~0U; // Done with the IT block after this.
337 }
338
339 // Rewind the state of the current IT block, removing the last slot from it.
340 void rewindImplicitITPosition() {
341 assert(inImplicitITBlock());
342 assert(ITState.CurPosition > 1);
343 ITState.CurPosition--;
344 unsigned TZ = llvm::countr_zero(ITState.Mask);
345 unsigned NewMask = 0;
346 NewMask |= ITState.Mask & (0xC << TZ);
347 NewMask |= 0x2 << TZ;
348 ITState.Mask = NewMask;
349 }
350
351 // Rewind the state of the current IT block, removing the last slot from it.
352 // If we were at the first slot, this closes the IT block.
353 void discardImplicitITBlock() {
354 assert(inImplicitITBlock());
355 assert(ITState.CurPosition == 1);
356 ITState.CurPosition = ~0U;
357 }
358
359 // Return the low-subreg of a given Q register.
360 unsigned getDRegFromQReg(unsigned QReg) const {
361 return MRI->getSubReg(QReg, ARM::dsub_0);
362 }
363
364 // Get the condition code corresponding to the current IT block slot.
365 ARMCC::CondCodes currentITCond() {
366 unsigned MaskBit = extractITMaskBit(ITState.Mask, ITState.CurPosition);
367 return MaskBit ? ARMCC::getOppositeCondition(ITState.Cond) : ITState.Cond;
368 }
369
370 // Invert the condition of the current IT block slot without changing any
371 // other slots in the same block.
372 void invertCurrentITCondition() {
373 if (ITState.CurPosition == 1) {
374 ITState.Cond = ARMCC::getOppositeCondition(ITState.Cond);
375 } else {
376 ITState.Mask ^= 1 << (5 - ITState.CurPosition);
377 }
378 }
379
380 // Returns true if the current IT block is full (all 4 slots used).
381 bool isITBlockFull() {
382 return inITBlock() && (ITState.Mask & 1);
383 }
384
385 // Extend the current implicit IT block to have one more slot with the given
386 // condition code.
387 void extendImplicitITBlock(ARMCC::CondCodes Cond) {
388 assert(inImplicitITBlock());
389 assert(!isITBlockFull());
390 assert(Cond == ITState.Cond ||
391 Cond == ARMCC::getOppositeCondition(ITState.Cond));
392 unsigned TZ = llvm::countr_zero(ITState.Mask);
393 unsigned NewMask = 0;
394 // Keep any existing condition bits.
395 NewMask |= ITState.Mask & (0xE << TZ);
396 // Insert the new condition bit.
397 NewMask |= (Cond != ITState.Cond) << TZ;
398 // Move the trailing 1 down one bit.
399 NewMask |= 1 << (TZ - 1);
400 ITState.Mask = NewMask;
401 }
402
403 // Create a new implicit IT block with a dummy condition code.
404 void startImplicitITBlock() {
405 assert(!inITBlock());
406 ITState.Cond = ARMCC::AL;
407 ITState.Mask = 8;
408 ITState.CurPosition = 1;
409 ITState.IsExplicit = false;
410 }
411
412 // Create a new explicit IT block with the given condition and mask.
413 // The mask should be in the format used in ARMOperand and
414 // MCOperand, with a 1 implying 'e', regardless of the low bit of
415 // the condition.
416 void startExplicitITBlock(ARMCC::CondCodes Cond, unsigned Mask) {
417 assert(!inITBlock());
418 ITState.Cond = Cond;
419 ITState.Mask = Mask;
420 ITState.CurPosition = 0;
421 ITState.IsExplicit = true;
422 }
423
424 struct {
425 unsigned Mask : 4;
426 unsigned CurPosition;
427 } VPTState;
428 bool inVPTBlock() { return VPTState.CurPosition != ~0U; }
429 void forwardVPTPosition() {
430 if (!inVPTBlock()) return;
431 unsigned TZ = llvm::countr_zero(VPTState.Mask);
432 if (++VPTState.CurPosition == 5 - TZ)
433 VPTState.CurPosition = ~0U;
434 }
435
436 void Note(SMLoc L, const Twine &Msg, SMRange Range = std::nullopt) {
437 return getParser().Note(L, Msg, Range);
438 }
439
440 bool Warning(SMLoc L, const Twine &Msg, SMRange Range = std::nullopt) {
441 return getParser().Warning(L, Msg, Range);
442 }
443
444 bool Error(SMLoc L, const Twine &Msg, SMRange Range = std::nullopt) {
445 return getParser().Error(L, Msg, Range);
446 }
447
448 bool validatetLDMRegList(const MCInst &Inst, const OperandVector &Operands,
449 unsigned ListNo, bool IsARPop = false);
450 bool validatetSTMRegList(const MCInst &Inst, const OperandVector &Operands,
451 unsigned ListNo);
452
453 int tryParseRegister();
454 bool tryParseRegisterWithWriteBack(OperandVector &);
455 int tryParseShiftRegister(OperandVector &);
456 bool parseRegisterList(OperandVector &, bool EnforceOrder = true,
457 bool AllowRAAC = false);
458 bool parseMemory(OperandVector &);
459 bool parseOperand(OperandVector &, StringRef Mnemonic);
460 bool parseImmExpr(int64_t &Out);
461 bool parsePrefix(ARMMCExpr::VariantKind &RefKind);
462 bool parseMemRegOffsetShift(ARM_AM::ShiftOpc &ShiftType,
463 unsigned &ShiftAmount);
464 bool parseLiteralValues(unsigned Size, SMLoc L);
465 bool parseDirectiveThumb(SMLoc L);
466 bool parseDirectiveARM(SMLoc L);
467 bool parseDirectiveThumbFunc(SMLoc L);
468 bool parseDirectiveCode(SMLoc L);
469 bool parseDirectiveSyntax(SMLoc L);
470 bool parseDirectiveReq(StringRef Name, SMLoc L);
471 bool parseDirectiveUnreq(SMLoc L);
472 bool parseDirectiveArch(SMLoc L);
473 bool parseDirectiveEabiAttr(SMLoc L);
474 bool parseDirectiveCPU(SMLoc L);
475 bool parseDirectiveFPU(SMLoc L);
476 bool parseDirectiveFnStart(SMLoc L);
477 bool parseDirectiveFnEnd(SMLoc L);
478 bool parseDirectiveCantUnwind(SMLoc L);
479 bool parseDirectivePersonality(SMLoc L);
480 bool parseDirectiveHandlerData(SMLoc L);
481 bool parseDirectiveSetFP(SMLoc L);
482 bool parseDirectivePad(SMLoc L);
483 bool parseDirectiveRegSave(SMLoc L, bool IsVector);
484 bool parseDirectiveInst(SMLoc L, char Suffix = '\0');
485 bool parseDirectiveLtorg(SMLoc L);
486 bool parseDirectiveEven(SMLoc L);
487 bool parseDirectivePersonalityIndex(SMLoc L);
488 bool parseDirectiveUnwindRaw(SMLoc L);
489 bool parseDirectiveTLSDescSeq(SMLoc L);
490 bool parseDirectiveMovSP(SMLoc L);
491 bool parseDirectiveObjectArch(SMLoc L);
492 bool parseDirectiveArchExtension(SMLoc L);
493 bool parseDirectiveAlign(SMLoc L);
494 bool parseDirectiveThumbSet(SMLoc L);
495
496 bool parseDirectiveSEHAllocStack(SMLoc L, bool Wide);
497 bool parseDirectiveSEHSaveRegs(SMLoc L, bool Wide);
498 bool parseDirectiveSEHSaveSP(SMLoc L);
499 bool parseDirectiveSEHSaveFRegs(SMLoc L);
500 bool parseDirectiveSEHSaveLR(SMLoc L);
501 bool parseDirectiveSEHPrologEnd(SMLoc L, bool Fragment);
502 bool parseDirectiveSEHNop(SMLoc L, bool Wide);
503 bool parseDirectiveSEHEpilogStart(SMLoc L, bool Condition);
504 bool parseDirectiveSEHEpilogEnd(SMLoc L);
505 bool parseDirectiveSEHCustom(SMLoc L);
506
507 bool isMnemonicVPTPredicable(StringRef Mnemonic, StringRef ExtraToken);
508 StringRef splitMnemonic(StringRef Mnemonic, StringRef ExtraToken,
509 unsigned &PredicationCode,
510 unsigned &VPTPredicationCode, bool &CarrySetting,
511 unsigned &ProcessorIMod, StringRef &ITMask);
512 void getMnemonicAcceptInfo(StringRef Mnemonic, StringRef ExtraToken,
513 StringRef FullInst, bool &CanAcceptCarrySet,
514 bool &CanAcceptPredicationCode,
515 bool &CanAcceptVPTPredicationCode);
516 bool enableArchExtFeature(StringRef Name, SMLoc &ExtLoc);
517
518 void tryConvertingToTwoOperandForm(StringRef Mnemonic, bool CarrySetting,
520 bool CDEConvertDualRegOperand(StringRef Mnemonic, OperandVector &Operands);
521
522 bool isThumb() const {
523 // FIXME: Can tablegen auto-generate this?
524 return getSTI().hasFeature(ARM::ModeThumb);
525 }
526
527 bool isThumbOne() const {
528 return isThumb() && !getSTI().hasFeature(ARM::FeatureThumb2);
529 }
530
531 bool isThumbTwo() const {
532 return isThumb() && getSTI().hasFeature(ARM::FeatureThumb2);
533 }
534
535 bool hasThumb() const {
536 return getSTI().hasFeature(ARM::HasV4TOps);
537 }
538
539 bool hasThumb2() const {
540 return getSTI().hasFeature(ARM::FeatureThumb2);
541 }
542
543 bool hasV6Ops() const {
544 return getSTI().hasFeature(ARM::HasV6Ops);
545 }
546
547 bool hasV6T2Ops() const {
548 return getSTI().hasFeature(ARM::HasV6T2Ops);
549 }
550
551 bool hasV6MOps() const {
552 return getSTI().hasFeature(ARM::HasV6MOps);
553 }
554
555 bool hasV7Ops() const {
556 return getSTI().hasFeature(ARM::HasV7Ops);
557 }
558
559 bool hasV8Ops() const {
560 return getSTI().hasFeature(ARM::HasV8Ops);
561 }
562
563 bool hasV8MBaseline() const {
564 return getSTI().hasFeature(ARM::HasV8MBaselineOps);
565 }
566
567 bool hasV8MMainline() const {
568 return getSTI().hasFeature(ARM::HasV8MMainlineOps);
569 }
570 bool hasV8_1MMainline() const {
571 return getSTI().hasFeature(ARM::HasV8_1MMainlineOps);
572 }
573 bool hasMVE() const {
574 return getSTI().hasFeature(ARM::HasMVEIntegerOps);
575 }
576 bool hasMVEFloat() const {
577 return getSTI().hasFeature(ARM::HasMVEFloatOps);
578 }
579 bool hasCDE() const {
580 return getSTI().hasFeature(ARM::HasCDEOps);
581 }
582 bool has8MSecExt() const {
583 return getSTI().hasFeature(ARM::Feature8MSecExt);
584 }
585
586 bool hasARM() const {
587 return !getSTI().hasFeature(ARM::FeatureNoARM);
588 }
589
590 bool hasDSP() const {
591 return getSTI().hasFeature(ARM::FeatureDSP);
592 }
593
594 bool hasD32() const {
595 return getSTI().hasFeature(ARM::FeatureD32);
596 }
597
598 bool hasV8_1aOps() const {
599 return getSTI().hasFeature(ARM::HasV8_1aOps);
600 }
601
602 bool hasRAS() const {
603 return getSTI().hasFeature(ARM::FeatureRAS);
604 }
605
606 void SwitchMode() {
607 MCSubtargetInfo &STI = copySTI();
608 auto FB = ComputeAvailableFeatures(STI.ToggleFeature(ARM::ModeThumb));
610 }
611
612 void FixModeAfterArchChange(bool WasThumb, SMLoc Loc);
613
614 bool isMClass() const {
615 return getSTI().hasFeature(ARM::FeatureMClass);
616 }
617
618 /// @name Auto-generated Match Functions
619 /// {
620
621#define GET_ASSEMBLER_HEADER
622#include "ARMGenAsmMatcher.inc"
623
624 /// }
625
626 ParseStatus parseITCondCode(OperandVector &);
627 ParseStatus parseCoprocNumOperand(OperandVector &);
628 ParseStatus parseCoprocRegOperand(OperandVector &);
629 ParseStatus parseCoprocOptionOperand(OperandVector &);
630 ParseStatus parseMemBarrierOptOperand(OperandVector &);
631 ParseStatus parseTraceSyncBarrierOptOperand(OperandVector &);
632 ParseStatus parseInstSyncBarrierOptOperand(OperandVector &);
633 ParseStatus parseProcIFlagsOperand(OperandVector &);
634 ParseStatus parseMSRMaskOperand(OperandVector &);
635 ParseStatus parseBankedRegOperand(OperandVector &);
636 ParseStatus parsePKHImm(OperandVector &O, StringRef Op, int Low, int High);
637 ParseStatus parsePKHLSLImm(OperandVector &O) {
638 return parsePKHImm(O, "lsl", 0, 31);
639 }
640 ParseStatus parsePKHASRImm(OperandVector &O) {
641 return parsePKHImm(O, "asr", 1, 32);
642 }
643 ParseStatus parseSetEndImm(OperandVector &);
644 ParseStatus parseShifterImm(OperandVector &);
645 ParseStatus parseRotImm(OperandVector &);
646 ParseStatus parseModImm(OperandVector &);
647 ParseStatus parseBitfield(OperandVector &);
648 ParseStatus parsePostIdxReg(OperandVector &);
649 ParseStatus parseAM3Offset(OperandVector &);
650 ParseStatus parseFPImm(OperandVector &);
651 ParseStatus parseVectorList(OperandVector &);
652 ParseStatus parseVectorLane(VectorLaneTy &LaneKind, unsigned &Index,
653 SMLoc &EndLoc);
654
655 // Asm Match Converter Methods
656 void cvtThumbMultiply(MCInst &Inst, const OperandVector &);
657 void cvtThumbBranches(MCInst &Inst, const OperandVector &);
658 void cvtMVEVMOVQtoDReg(MCInst &Inst, const OperandVector &);
659
660 bool validateInstruction(MCInst &Inst, const OperandVector &Ops);
661 bool processInstruction(MCInst &Inst, const OperandVector &Ops, MCStreamer &Out);
662 bool shouldOmitCCOutOperand(StringRef Mnemonic, OperandVector &Operands);
663 bool shouldOmitPredicateOperand(StringRef Mnemonic, OperandVector &Operands);
664 bool shouldOmitVectorPredicateOperand(StringRef Mnemonic, OperandVector &Operands);
665 bool isITBlockTerminator(MCInst &Inst) const;
666 void fixupGNULDRDAlias(StringRef Mnemonic, OperandVector &Operands);
667 bool validateLDRDSTRD(MCInst &Inst, const OperandVector &Operands,
668 bool Load, bool ARMMode, bool Writeback);
669
670public:
671 enum ARMMatchResultTy {
672 Match_RequiresITBlock = FIRST_TARGET_MATCH_RESULT_TY,
673 Match_RequiresNotITBlock,
674 Match_RequiresV6,
675 Match_RequiresThumb2,
676 Match_RequiresV8,
677 Match_RequiresFlagSetting,
678#define GET_OPERAND_DIAGNOSTIC_TYPES
679#include "ARMGenAsmMatcher.inc"
680
681 };
682
683 ARMAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser,
684 const MCInstrInfo &MII, const MCTargetOptions &Options)
685 : MCTargetAsmParser(Options, STI, MII), UC(Parser), MS(STI) {
687
688 // Cache the MCRegisterInfo.
690
691 // Initialize the set of available features.
692 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
693
694 // Add build attributes based on the selected target.
696 getTargetStreamer().emitTargetAttributes(STI);
697
698 // Not in an ITBlock to start with.
699 ITState.CurPosition = ~0U;
700
701 VPTState.CurPosition = ~0U;
702
703 NextSymbolIsThumb = false;
704 }
705
706 // Implementation of the MCTargetAsmParser interface:
707 bool parseRegister(MCRegister &Reg, SMLoc &StartLoc, SMLoc &EndLoc) override;
709 SMLoc &EndLoc) override;
711 SMLoc NameLoc, OperandVector &Operands) override;
712 bool ParseDirective(AsmToken DirectiveID) override;
713
715 unsigned Kind) override;
716 unsigned checkTargetMatchPredicate(MCInst &Inst) override;
717
718 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
721 bool MatchingInlineAsm) override;
722 unsigned MatchInstruction(OperandVector &Operands, MCInst &Inst,
724 bool MatchingInlineAsm, bool &EmitInITBlock,
725 MCStreamer &Out);
726
727 struct NearMissMessage {
728 SMLoc Loc;
729 SmallString<128> Message;
730 };
731
732 const char *getCustomOperandDiag(ARMMatchResultTy MatchError);
733
734 void FilterNearMisses(SmallVectorImpl<NearMissInfo> &NearMissesIn,
737 void ReportNearMisses(SmallVectorImpl<NearMissInfo> &NearMisses, SMLoc IDLoc,
739
740 void doBeforeLabelEmit(MCSymbol *Symbol, SMLoc IDLoc) override;
741
742 void onLabelParsed(MCSymbol *Symbol) override;
743};
744
745/// ARMOperand - Instances of this class represent a parsed ARM machine
746/// operand.
747class ARMOperand : public MCParsedAsmOperand {
748 enum KindTy {
749 k_CondCode,
750 k_VPTPred,
751 k_CCOut,
752 k_ITCondMask,
753 k_CoprocNum,
754 k_CoprocReg,
755 k_CoprocOption,
756 k_Immediate,
757 k_MemBarrierOpt,
758 k_InstSyncBarrierOpt,
759 k_TraceSyncBarrierOpt,
760 k_Memory,
761 k_PostIndexRegister,
762 k_MSRMask,
763 k_BankedReg,
764 k_ProcIFlags,
765 k_VectorIndex,
766 k_Register,
767 k_RegisterList,
768 k_RegisterListWithAPSR,
769 k_DPRRegisterList,
770 k_SPRRegisterList,
771 k_FPSRegisterListWithVPR,
772 k_FPDRegisterListWithVPR,
773 k_VectorList,
774 k_VectorListAllLanes,
775 k_VectorListIndexed,
776 k_ShiftedRegister,
777 k_ShiftedImmediate,
778 k_ShifterImmediate,
779 k_RotateImmediate,
780 k_ModifiedImmediate,
781 k_ConstantPoolImmediate,
782 k_BitfieldDescriptor,
783 k_Token,
784 } Kind;
785
786 SMLoc StartLoc, EndLoc, AlignmentLoc;
788
789 struct CCOp {
791 };
792
793 struct VCCOp {
795 };
796
797 struct CopOp {
798 unsigned Val;
799 };
800
801 struct CoprocOptionOp {
802 unsigned Val;
803 };
804
805 struct ITMaskOp {
806 unsigned Mask:4;
807 };
808
809 struct MBOptOp {
810 ARM_MB::MemBOpt Val;
811 };
812
813 struct ISBOptOp {
815 };
816
817 struct TSBOptOp {
819 };
820
821 struct IFlagsOp {
823 };
824
825 struct MMaskOp {
826 unsigned Val;
827 };
828
829 struct BankedRegOp {
830 unsigned Val;
831 };
832
833 struct TokOp {
834 const char *Data;
835 unsigned Length;
836 };
837
838 struct RegOp {
839 unsigned RegNum;
840 };
841
842 // A vector register list is a sequential list of 1 to 4 registers.
843 struct VectorListOp {
844 unsigned RegNum;
845 unsigned Count;
846 unsigned LaneIndex;
847 bool isDoubleSpaced;
848 };
849
850 struct VectorIndexOp {
851 unsigned Val;
852 };
853
854 struct ImmOp {
855 const MCExpr *Val;
856 };
857
858 /// Combined record for all forms of ARM address expressions.
859 struct MemoryOp {
860 unsigned BaseRegNum;
861 // Offset is in OffsetReg or OffsetImm. If both are zero, no offset
862 // was specified.
863 const MCExpr *OffsetImm; // Offset immediate value
864 unsigned OffsetRegNum; // Offset register num, when OffsetImm == NULL
865 ARM_AM::ShiftOpc ShiftType; // Shift type for OffsetReg
866 unsigned ShiftImm; // shift for OffsetReg.
867 unsigned Alignment; // 0 = no alignment specified
868 // n = alignment in bytes (2, 4, 8, 16, or 32)
869 unsigned isNegative : 1; // Negated OffsetReg? (~'U' bit)
870 };
871
872 struct PostIdxRegOp {
873 unsigned RegNum;
874 bool isAdd;
875 ARM_AM::ShiftOpc ShiftTy;
876 unsigned ShiftImm;
877 };
878
879 struct ShifterImmOp {
880 bool isASR;
881 unsigned Imm;
882 };
883
884 struct RegShiftedRegOp {
885 ARM_AM::ShiftOpc ShiftTy;
886 unsigned SrcReg;
887 unsigned ShiftReg;
888 unsigned ShiftImm;
889 };
890
891 struct RegShiftedImmOp {
892 ARM_AM::ShiftOpc ShiftTy;
893 unsigned SrcReg;
894 unsigned ShiftImm;
895 };
896
897 struct RotImmOp {
898 unsigned Imm;
899 };
900
901 struct ModImmOp {
902 unsigned Bits;
903 unsigned Rot;
904 };
905
906 struct BitfieldOp {
907 unsigned LSB;
908 unsigned Width;
909 };
910
911 union {
912 struct CCOp CC;
913 struct VCCOp VCC;
914 struct CopOp Cop;
915 struct CoprocOptionOp CoprocOption;
916 struct MBOptOp MBOpt;
917 struct ISBOptOp ISBOpt;
918 struct TSBOptOp TSBOpt;
919 struct ITMaskOp ITMask;
920 struct IFlagsOp IFlags;
921 struct MMaskOp MMask;
922 struct BankedRegOp BankedReg;
923 struct TokOp Tok;
924 struct RegOp Reg;
925 struct VectorListOp VectorList;
926 struct VectorIndexOp VectorIndex;
927 struct ImmOp Imm;
928 struct MemoryOp Memory;
929 struct PostIdxRegOp PostIdxReg;
930 struct ShifterImmOp ShifterImm;
931 struct RegShiftedRegOp RegShiftedReg;
932 struct RegShiftedImmOp RegShiftedImm;
933 struct RotImmOp RotImm;
934 struct ModImmOp ModImm;
935 struct BitfieldOp Bitfield;
936 };
937
938public:
939 ARMOperand(KindTy K) : Kind(K) {}
940
941 /// getStartLoc - Get the location of the first token of this operand.
942 SMLoc getStartLoc() const override { return StartLoc; }
943
944 /// getEndLoc - Get the location of the last token of this operand.
945 SMLoc getEndLoc() const override { return EndLoc; }
946
947 /// getLocRange - Get the range between the first and last token of this
948 /// operand.
949 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
950
951 /// getAlignmentLoc - Get the location of the Alignment token of this operand.
952 SMLoc getAlignmentLoc() const {
953 assert(Kind == k_Memory && "Invalid access!");
954 return AlignmentLoc;
955 }
956
958 assert(Kind == k_CondCode && "Invalid access!");
959 return CC.Val;
960 }
961
962 ARMVCC::VPTCodes getVPTPred() const {
963 assert(isVPTPred() && "Invalid access!");
964 return VCC.Val;
965 }
966
967 unsigned getCoproc() const {
968 assert((Kind == k_CoprocNum || Kind == k_CoprocReg) && "Invalid access!");
969 return Cop.Val;
970 }
971
972 StringRef getToken() const {
973 assert(Kind == k_Token && "Invalid access!");
974 return StringRef(Tok.Data, Tok.Length);
975 }
976
977 unsigned getReg() const override {
978 assert((Kind == k_Register || Kind == k_CCOut) && "Invalid access!");
979 return Reg.RegNum;
980 }
981
982 const SmallVectorImpl<unsigned> &getRegList() const {
983 assert((Kind == k_RegisterList || Kind == k_RegisterListWithAPSR ||
984 Kind == k_DPRRegisterList || Kind == k_SPRRegisterList ||
985 Kind == k_FPSRegisterListWithVPR ||
986 Kind == k_FPDRegisterListWithVPR) &&
987 "Invalid access!");
988 return Registers;
989 }
990
991 const MCExpr *getImm() const {
992 assert(isImm() && "Invalid access!");
993 return Imm.Val;
994 }
995
996 const MCExpr *getConstantPoolImm() const {
997 assert(isConstantPoolImm() && "Invalid access!");
998 return Imm.Val;
999 }
1000
1001 unsigned getVectorIndex() const {
1002 assert(Kind == k_VectorIndex && "Invalid access!");
1003 return VectorIndex.Val;
1004 }
1005
1006 ARM_MB::MemBOpt getMemBarrierOpt() const {
1007 assert(Kind == k_MemBarrierOpt && "Invalid access!");
1008 return MBOpt.Val;
1009 }
1010
1011 ARM_ISB::InstSyncBOpt getInstSyncBarrierOpt() const {
1012 assert(Kind == k_InstSyncBarrierOpt && "Invalid access!");
1013 return ISBOpt.Val;
1014 }
1015
1016 ARM_TSB::TraceSyncBOpt getTraceSyncBarrierOpt() const {
1017 assert(Kind == k_TraceSyncBarrierOpt && "Invalid access!");
1018 return TSBOpt.Val;
1019 }
1020
1021 ARM_PROC::IFlags getProcIFlags() const {
1022 assert(Kind == k_ProcIFlags && "Invalid access!");
1023 return IFlags.Val;
1024 }
1025
1026 unsigned getMSRMask() const {
1027 assert(Kind == k_MSRMask && "Invalid access!");
1028 return MMask.Val;
1029 }
1030
1031 unsigned getBankedReg() const {
1032 assert(Kind == k_BankedReg && "Invalid access!");
1033 return BankedReg.Val;
1034 }
1035
1036 bool isCoprocNum() const { return Kind == k_CoprocNum; }
1037 bool isCoprocReg() const { return Kind == k_CoprocReg; }
1038 bool isCoprocOption() const { return Kind == k_CoprocOption; }
1039 bool isCondCode() const { return Kind == k_CondCode; }
1040 bool isVPTPred() const { return Kind == k_VPTPred; }
1041 bool isCCOut() const { return Kind == k_CCOut; }
1042 bool isITMask() const { return Kind == k_ITCondMask; }
1043 bool isITCondCode() const { return Kind == k_CondCode; }
1044 bool isImm() const override {
1045 return Kind == k_Immediate;
1046 }
1047
1048 bool isARMBranchTarget() const {
1049 if (!isImm()) return false;
1050
1051 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm()))
1052 return CE->getValue() % 4 == 0;
1053 return true;
1054 }
1055
1056
1057 bool isThumbBranchTarget() const {
1058 if (!isImm()) return false;
1059
1060 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm()))
1061 return CE->getValue() % 2 == 0;
1062 return true;
1063 }
1064
1065 // checks whether this operand is an unsigned offset which fits is a field
1066 // of specified width and scaled by a specific number of bits
1067 template<unsigned width, unsigned scale>
1068 bool isUnsignedOffset() const {
1069 if (!isImm()) return false;
1070 if (isa<MCSymbolRefExpr>(Imm.Val)) return true;
1071 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val)) {
1072 int64_t Val = CE->getValue();
1073 int64_t Align = 1LL << scale;
1074 int64_t Max = Align * ((1LL << width) - 1);
1075 return ((Val % Align) == 0) && (Val >= 0) && (Val <= Max);
1076 }
1077 return false;
1078 }
1079
1080 // checks whether this operand is an signed offset which fits is a field
1081 // of specified width and scaled by a specific number of bits
1082 template<unsigned width, unsigned scale>
1083 bool isSignedOffset() const {
1084 if (!isImm()) return false;
1085 if (isa<MCSymbolRefExpr>(Imm.Val)) return true;
1086 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val)) {
1087 int64_t Val = CE->getValue();
1088 int64_t Align = 1LL << scale;
1089 int64_t Max = Align * ((1LL << (width-1)) - 1);
1090 int64_t Min = -Align * (1LL << (width-1));
1091 return ((Val % Align) == 0) && (Val >= Min) && (Val <= Max);
1092 }
1093 return false;
1094 }
1095
1096 // checks whether this operand is an offset suitable for the LE /
1097 // LETP instructions in Arm v8.1M
1098 bool isLEOffset() const {
1099 if (!isImm()) return false;
1100 if (isa<MCSymbolRefExpr>(Imm.Val)) return true;
1101 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val)) {
1102 int64_t Val = CE->getValue();
1103 return Val < 0 && Val >= -4094 && (Val & 1) == 0;
1104 }
1105 return false;
1106 }
1107
1108 // checks whether this operand is a memory operand computed as an offset
1109 // applied to PC. the offset may have 8 bits of magnitude and is represented
1110 // with two bits of shift. textually it may be either [pc, #imm], #imm or
1111 // relocable expression...
1112 bool isThumbMemPC() const {
1113 int64_t Val = 0;
1114 if (isImm()) {
1115 if (isa<MCSymbolRefExpr>(Imm.Val)) return true;
1116 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val);
1117 if (!CE) return false;
1118 Val = CE->getValue();
1119 }
1120 else if (isGPRMem()) {
1121 if(!Memory.OffsetImm || Memory.OffsetRegNum) return false;
1122 if(Memory.BaseRegNum != ARM::PC) return false;
1123 if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm))
1124 Val = CE->getValue();
1125 else
1126 return false;
1127 }
1128 else return false;
1129 return ((Val % 4) == 0) && (Val >= 0) && (Val <= 1020);
1130 }
1131
1132 bool isFPImm() const {
1133 if (!isImm()) return false;
1134 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1135 if (!CE) return false;
1136 int Val = ARM_AM::getFP32Imm(APInt(32, CE->getValue()));
1137 return Val != -1;
1138 }
1139
1140 template<int64_t N, int64_t M>
1141 bool isImmediate() const {
1142 if (!isImm()) return false;
1143 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1144 if (!CE) return false;
1145 int64_t Value = CE->getValue();
1146 return Value >= N && Value <= M;
1147 }
1148
1149 template<int64_t N, int64_t M>
1150 bool isImmediateS4() const {
1151 if (!isImm()) return false;
1152 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1153 if (!CE) return false;
1154 int64_t Value = CE->getValue();
1155 return ((Value & 3) == 0) && Value >= N && Value <= M;
1156 }
1157 template<int64_t N, int64_t M>
1158 bool isImmediateS2() const {
1159 if (!isImm()) return false;
1160 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1161 if (!CE) return false;
1162 int64_t Value = CE->getValue();
1163 return ((Value & 1) == 0) && Value >= N && Value <= M;
1164 }
1165 bool isFBits16() const {
1166 return isImmediate<0, 17>();
1167 }
1168 bool isFBits32() const {
1169 return isImmediate<1, 33>();
1170 }
1171 bool isImm8s4() const {
1172 return isImmediateS4<-1020, 1020>();
1173 }
1174 bool isImm7s4() const {
1175 return isImmediateS4<-508, 508>();
1176 }
1177 bool isImm7Shift0() const {
1178 return isImmediate<-127, 127>();
1179 }
1180 bool isImm7Shift1() const {
1181 return isImmediateS2<-255, 255>();
1182 }
1183 bool isImm7Shift2() const {
1184 return isImmediateS4<-511, 511>();
1185 }
1186 bool isImm7() const {
1187 return isImmediate<-127, 127>();
1188 }
1189 bool isImm0_1020s4() const {
1190 return isImmediateS4<0, 1020>();
1191 }
1192 bool isImm0_508s4() const {
1193 return isImmediateS4<0, 508>();
1194 }
1195 bool isImm0_508s4Neg() const {
1196 if (!isImm()) return false;
1197 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1198 if (!CE) return false;
1199 int64_t Value = -CE->getValue();
1200 // explicitly exclude zero. we want that to use the normal 0_508 version.
1201 return ((Value & 3) == 0) && Value > 0 && Value <= 508;
1202 }
1203
1204 bool isImm0_4095Neg() const {
1205 if (!isImm()) return false;
1206 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1207 if (!CE) return false;
1208 // isImm0_4095Neg is used with 32-bit immediates only.
1209 // 32-bit immediates are zero extended to 64-bit when parsed,
1210 // thus simple -CE->getValue() results in a big negative number,
1211 // not a small positive number as intended
1212 if ((CE->getValue() >> 32) > 0) return false;
1213 uint32_t Value = -static_cast<uint32_t>(CE->getValue());
1214 return Value > 0 && Value < 4096;
1215 }
1216
1217 bool isImm0_7() const {
1218 return isImmediate<0, 7>();
1219 }
1220
1221 bool isImm1_16() const {
1222 return isImmediate<1, 16>();
1223 }
1224
1225 bool isImm1_32() const {
1226 return isImmediate<1, 32>();
1227 }
1228
1229 bool isImm8_255() const {
1230 return isImmediate<8, 255>();
1231 }
1232
1233 bool isImm0_255Expr() const {
1234 if (!isImm())
1235 return false;
1236 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1237 // If it's not a constant expression, it'll generate a fixup and be
1238 // handled later.
1239 if (!CE)
1240 return true;
1241 int64_t Value = CE->getValue();
1242 return isUInt<8>(Value);
1243 }
1244
1245 bool isImm256_65535Expr() const {
1246 if (!isImm()) return false;
1247 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1248 // If it's not a constant expression, it'll generate a fixup and be
1249 // handled later.
1250 if (!CE) return true;
1251 int64_t Value = CE->getValue();
1252 return Value >= 256 && Value < 65536;
1253 }
1254
1255 bool isImm0_65535Expr() const {
1256 if (!isImm()) return false;
1257 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1258 // If it's not a constant expression, it'll generate a fixup and be
1259 // handled later.
1260 if (!CE) return true;
1261 int64_t Value = CE->getValue();
1262 return Value >= 0 && Value < 65536;
1263 }
1264
1265 bool isImm24bit() const {
1266 return isImmediate<0, 0xffffff + 1>();
1267 }
1268
1269 bool isImmThumbSR() const {
1270 return isImmediate<1, 33>();
1271 }
1272
1273 bool isPKHLSLImm() const {
1274 return isImmediate<0, 32>();
1275 }
1276
1277 bool isPKHASRImm() const {
1278 return isImmediate<0, 33>();
1279 }
1280
1281 bool isAdrLabel() const {
1282 // If we have an immediate that's not a constant, treat it as a label
1283 // reference needing a fixup.
1284 if (isImm() && !isa<MCConstantExpr>(getImm()))
1285 return true;
1286
1287 // If it is a constant, it must fit into a modified immediate encoding.
1288 if (!isImm()) return false;
1289 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1290 if (!CE) return false;
1291 int64_t Value = CE->getValue();
1292 return (ARM_AM::getSOImmVal(Value) != -1 ||
1293 ARM_AM::getSOImmVal(-Value) != -1);
1294 }
1295
1296 bool isT2SOImm() const {
1297 // If we have an immediate that's not a constant, treat it as an expression
1298 // needing a fixup.
1299 if (isImm() && !isa<MCConstantExpr>(getImm())) {
1300 // We want to avoid matching :upper16: and :lower16: as we want these
1301 // expressions to match in isImm0_65535Expr()
1302 const ARMMCExpr *ARM16Expr = dyn_cast<ARMMCExpr>(getImm());
1303 return (!ARM16Expr || (ARM16Expr->getKind() != ARMMCExpr::VK_ARM_HI16 &&
1304 ARM16Expr->getKind() != ARMMCExpr::VK_ARM_LO16));
1305 }
1306 if (!isImm()) return false;
1307 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1308 if (!CE) return false;
1309 int64_t Value = CE->getValue();
1310 return ARM_AM::getT2SOImmVal(Value) != -1;
1311 }
1312
1313 bool isT2SOImmNot() const {
1314 if (!isImm()) return false;
1315 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1316 if (!CE) return false;
1317 int64_t Value = CE->getValue();
1318 return ARM_AM::getT2SOImmVal(Value) == -1 &&
1320 }
1321
1322 bool isT2SOImmNeg() const {
1323 if (!isImm()) return false;
1324 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1325 if (!CE) return false;
1326 int64_t Value = CE->getValue();
1327 // Only use this when not representable as a plain so_imm.
1328 return ARM_AM::getT2SOImmVal(Value) == -1 &&
1330 }
1331
1332 bool isSetEndImm() const {
1333 if (!isImm()) return false;
1334 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1335 if (!CE) return false;
1336 int64_t Value = CE->getValue();
1337 return Value == 1 || Value == 0;
1338 }
1339
1340 bool isReg() const override { return Kind == k_Register; }
1341 bool isRegList() const { return Kind == k_RegisterList; }
1342 bool isRegListWithAPSR() const {
1343 return Kind == k_RegisterListWithAPSR || Kind == k_RegisterList;
1344 }
1345 bool isDPRRegList() const { return Kind == k_DPRRegisterList; }
1346 bool isSPRRegList() const { return Kind == k_SPRRegisterList; }
1347 bool isFPSRegListWithVPR() const { return Kind == k_FPSRegisterListWithVPR; }
1348 bool isFPDRegListWithVPR() const { return Kind == k_FPDRegisterListWithVPR; }
1349 bool isToken() const override { return Kind == k_Token; }
1350 bool isMemBarrierOpt() const { return Kind == k_MemBarrierOpt; }
1351 bool isInstSyncBarrierOpt() const { return Kind == k_InstSyncBarrierOpt; }
1352 bool isTraceSyncBarrierOpt() const { return Kind == k_TraceSyncBarrierOpt; }
1353 bool isMem() const override {
1354 return isGPRMem() || isMVEMem();
1355 }
1356 bool isMVEMem() const {
1357 if (Kind != k_Memory)
1358 return false;
1359 if (Memory.BaseRegNum &&
1360 !ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Memory.BaseRegNum) &&
1361 !ARMMCRegisterClasses[ARM::MQPRRegClassID].contains(Memory.BaseRegNum))
1362 return false;
1363 if (Memory.OffsetRegNum &&
1364 !ARMMCRegisterClasses[ARM::MQPRRegClassID].contains(
1365 Memory.OffsetRegNum))
1366 return false;
1367 return true;
1368 }
1369 bool isGPRMem() const {
1370 if (Kind != k_Memory)
1371 return false;
1372 if (Memory.BaseRegNum &&
1373 !ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Memory.BaseRegNum))
1374 return false;
1375 if (Memory.OffsetRegNum &&
1376 !ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Memory.OffsetRegNum))
1377 return false;
1378 return true;
1379 }
1380 bool isShifterImm() const { return Kind == k_ShifterImmediate; }
1381 bool isRegShiftedReg() const {
1382 return Kind == k_ShiftedRegister &&
1383 ARMMCRegisterClasses[ARM::GPRRegClassID].contains(
1384 RegShiftedReg.SrcReg) &&
1385 ARMMCRegisterClasses[ARM::GPRRegClassID].contains(
1386 RegShiftedReg.ShiftReg);
1387 }
1388 bool isRegShiftedImm() const {
1389 return Kind == k_ShiftedImmediate &&
1390 ARMMCRegisterClasses[ARM::GPRRegClassID].contains(
1391 RegShiftedImm.SrcReg);
1392 }
1393 bool isRotImm() const { return Kind == k_RotateImmediate; }
1394
1395 template<unsigned Min, unsigned Max>
1396 bool isPowerTwoInRange() const {
1397 if (!isImm()) return false;
1398 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1399 if (!CE) return false;
1400 int64_t Value = CE->getValue();
1401 return Value > 0 && llvm::popcount((uint64_t)Value) == 1 && Value >= Min &&
1402 Value <= Max;
1403 }
1404 bool isModImm() const { return Kind == k_ModifiedImmediate; }
1405
1406 bool isModImmNot() const {
1407 if (!isImm()) return false;
1408 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1409 if (!CE) return false;
1410 int64_t Value = CE->getValue();
1411 return ARM_AM::getSOImmVal(~Value) != -1;
1412 }
1413
1414 bool isModImmNeg() const {
1415 if (!isImm()) return false;
1416 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1417 if (!CE) return false;
1418 int64_t Value = CE->getValue();
1419 return ARM_AM::getSOImmVal(Value) == -1 &&
1420 ARM_AM::getSOImmVal(-Value) != -1;
1421 }
1422
1423 bool isThumbModImmNeg1_7() const {
1424 if (!isImm()) return false;
1425 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1426 if (!CE) return false;
1427 int32_t Value = -(int32_t)CE->getValue();
1428 return 0 < Value && Value < 8;
1429 }
1430
1431 bool isThumbModImmNeg8_255() const {
1432 if (!isImm()) return false;
1433 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1434 if (!CE) return false;
1435 int32_t Value = -(int32_t)CE->getValue();
1436 return 7 < Value && Value < 256;
1437 }
1438
1439 bool isConstantPoolImm() const { return Kind == k_ConstantPoolImmediate; }
1440 bool isBitfield() const { return Kind == k_BitfieldDescriptor; }
1441 bool isPostIdxRegShifted() const {
1442 return Kind == k_PostIndexRegister &&
1443 ARMMCRegisterClasses[ARM::GPRRegClassID].contains(PostIdxReg.RegNum);
1444 }
1445 bool isPostIdxReg() const {
1446 return isPostIdxRegShifted() && PostIdxReg.ShiftTy == ARM_AM::no_shift;
1447 }
1448 bool isMemNoOffset(bool alignOK = false, unsigned Alignment = 0) const {
1449 if (!isGPRMem())
1450 return false;
1451 // No offset of any kind.
1452 return Memory.OffsetRegNum == 0 && Memory.OffsetImm == nullptr &&
1453 (alignOK || Memory.Alignment == Alignment);
1454 }
1455 bool isMemNoOffsetT2(bool alignOK = false, unsigned Alignment = 0) const {
1456 if (!isGPRMem())
1457 return false;
1458
1459 if (!ARMMCRegisterClasses[ARM::GPRnopcRegClassID].contains(
1460 Memory.BaseRegNum))
1461 return false;
1462
1463 // No offset of any kind.
1464 return Memory.OffsetRegNum == 0 && Memory.OffsetImm == nullptr &&
1465 (alignOK || Memory.Alignment == Alignment);
1466 }
1467 bool isMemNoOffsetT2NoSp(bool alignOK = false, unsigned Alignment = 0) const {
1468 if (!isGPRMem())
1469 return false;
1470
1471 if (!ARMMCRegisterClasses[ARM::rGPRRegClassID].contains(
1472 Memory.BaseRegNum))
1473 return false;
1474
1475 // No offset of any kind.
1476 return Memory.OffsetRegNum == 0 && Memory.OffsetImm == nullptr &&
1477 (alignOK || Memory.Alignment == Alignment);
1478 }
1479 bool isMemNoOffsetT(bool alignOK = false, unsigned Alignment = 0) const {
1480 if (!isGPRMem())
1481 return false;
1482
1483 if (!ARMMCRegisterClasses[ARM::tGPRRegClassID].contains(
1484 Memory.BaseRegNum))
1485 return false;
1486
1487 // No offset of any kind.
1488 return Memory.OffsetRegNum == 0 && Memory.OffsetImm == nullptr &&
1489 (alignOK || Memory.Alignment == Alignment);
1490 }
1491 bool isMemPCRelImm12() const {
1492 if (!isGPRMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1493 return false;
1494 // Base register must be PC.
1495 if (Memory.BaseRegNum != ARM::PC)
1496 return false;
1497 // Immediate offset in range [-4095, 4095].
1498 if (!Memory.OffsetImm) return true;
1499 if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1500 int64_t Val = CE->getValue();
1501 return (Val > -4096 && Val < 4096) ||
1502 (Val == std::numeric_limits<int32_t>::min());
1503 }
1504 return false;
1505 }
1506
1507 bool isAlignedMemory() const {
1508 return isMemNoOffset(true);
1509 }
1510
1511 bool isAlignedMemoryNone() const {
1512 return isMemNoOffset(false, 0);
1513 }
1514
1515 bool isDupAlignedMemoryNone() const {
1516 return isMemNoOffset(false, 0);
1517 }
1518
1519 bool isAlignedMemory16() const {
1520 if (isMemNoOffset(false, 2)) // alignment in bytes for 16-bits is 2.
1521 return true;
1522 return isMemNoOffset(false, 0);
1523 }
1524
1525 bool isDupAlignedMemory16() const {
1526 if (isMemNoOffset(false, 2)) // alignment in bytes for 16-bits is 2.
1527 return true;
1528 return isMemNoOffset(false, 0);
1529 }
1530
1531 bool isAlignedMemory32() const {
1532 if (isMemNoOffset(false, 4)) // alignment in bytes for 32-bits is 4.
1533 return true;
1534 return isMemNoOffset(false, 0);
1535 }
1536
1537 bool isDupAlignedMemory32() const {
1538 if (isMemNoOffset(false, 4)) // alignment in bytes for 32-bits is 4.
1539 return true;
1540 return isMemNoOffset(false, 0);
1541 }
1542
1543 bool isAlignedMemory64() const {
1544 if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1545 return true;
1546 return isMemNoOffset(false, 0);
1547 }
1548
1549 bool isDupAlignedMemory64() const {
1550 if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1551 return true;
1552 return isMemNoOffset(false, 0);
1553 }
1554
1555 bool isAlignedMemory64or128() const {
1556 if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1557 return true;
1558 if (isMemNoOffset(false, 16)) // alignment in bytes for 128-bits is 16.
1559 return true;
1560 return isMemNoOffset(false, 0);
1561 }
1562
1563 bool isDupAlignedMemory64or128() const {
1564 if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1565 return true;
1566 if (isMemNoOffset(false, 16)) // alignment in bytes for 128-bits is 16.
1567 return true;
1568 return isMemNoOffset(false, 0);
1569 }
1570
1571 bool isAlignedMemory64or128or256() const {
1572 if (isMemNoOffset(false, 8)) // alignment in bytes for 64-bits is 8.
1573 return true;
1574 if (isMemNoOffset(false, 16)) // alignment in bytes for 128-bits is 16.
1575 return true;
1576 if (isMemNoOffset(false, 32)) // alignment in bytes for 256-bits is 32.
1577 return true;
1578 return isMemNoOffset(false, 0);
1579 }
1580
1581 bool isAddrMode2() const {
1582 if (!isGPRMem() || Memory.Alignment != 0) return false;
1583 // Check for register offset.
1584 if (Memory.OffsetRegNum) return true;
1585 // Immediate offset in range [-4095, 4095].
1586 if (!Memory.OffsetImm) return true;
1587 if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1588 int64_t Val = CE->getValue();
1589 return Val > -4096 && Val < 4096;
1590 }
1591 return false;
1592 }
1593
1594 bool isAM2OffsetImm() const {
1595 if (!isImm()) return false;
1596 // Immediate offset in range [-4095, 4095].
1597 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1598 if (!CE) return false;
1599 int64_t Val = CE->getValue();
1600 return (Val == std::numeric_limits<int32_t>::min()) ||
1601 (Val > -4096 && Val < 4096);
1602 }
1603
1604 bool isAddrMode3() const {
1605 // If we have an immediate that's not a constant, treat it as a label
1606 // reference needing a fixup. If it is a constant, it's something else
1607 // and we reject it.
1608 if (isImm() && !isa<MCConstantExpr>(getImm()))
1609 return true;
1610 if (!isGPRMem() || Memory.Alignment != 0) return false;
1611 // No shifts are legal for AM3.
1612 if (Memory.ShiftType != ARM_AM::no_shift) return false;
1613 // Check for register offset.
1614 if (Memory.OffsetRegNum) return true;
1615 // Immediate offset in range [-255, 255].
1616 if (!Memory.OffsetImm) return true;
1617 if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1618 int64_t Val = CE->getValue();
1619 // The #-0 offset is encoded as std::numeric_limits<int32_t>::min(), and
1620 // we have to check for this too.
1621 return (Val > -256 && Val < 256) ||
1622 Val == std::numeric_limits<int32_t>::min();
1623 }
1624 return false;
1625 }
1626
1627 bool isAM3Offset() const {
1628 if (isPostIdxReg())
1629 return true;
1630 if (!isImm())
1631 return false;
1632 // Immediate offset in range [-255, 255].
1633 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1634 if (!CE) return false;
1635 int64_t Val = CE->getValue();
1636 // Special case, #-0 is std::numeric_limits<int32_t>::min().
1637 return (Val > -256 && Val < 256) ||
1638 Val == std::numeric_limits<int32_t>::min();
1639 }
1640
1641 bool isAddrMode5() const {
1642 // If we have an immediate that's not a constant, treat it as a label
1643 // reference needing a fixup. If it is a constant, it's something else
1644 // and we reject it.
1645 if (isImm() && !isa<MCConstantExpr>(getImm()))
1646 return true;
1647 if (!isGPRMem() || Memory.Alignment != 0) return false;
1648 // Check for register offset.
1649 if (Memory.OffsetRegNum) return false;
1650 // Immediate offset in range [-1020, 1020] and a multiple of 4.
1651 if (!Memory.OffsetImm) return true;
1652 if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1653 int64_t Val = CE->getValue();
1654 return (Val >= -1020 && Val <= 1020 && ((Val & 3) == 0)) ||
1655 Val == std::numeric_limits<int32_t>::min();
1656 }
1657 return false;
1658 }
1659
1660 bool isAddrMode5FP16() const {
1661 // If we have an immediate that's not a constant, treat it as a label
1662 // reference needing a fixup. If it is a constant, it's something else
1663 // and we reject it.
1664 if (isImm() && !isa<MCConstantExpr>(getImm()))
1665 return true;
1666 if (!isGPRMem() || Memory.Alignment != 0) return false;
1667 // Check for register offset.
1668 if (Memory.OffsetRegNum) return false;
1669 // Immediate offset in range [-510, 510] and a multiple of 2.
1670 if (!Memory.OffsetImm) return true;
1671 if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1672 int64_t Val = CE->getValue();
1673 return (Val >= -510 && Val <= 510 && ((Val & 1) == 0)) ||
1674 Val == std::numeric_limits<int32_t>::min();
1675 }
1676 return false;
1677 }
1678
1679 bool isMemTBB() const {
1680 if (!isGPRMem() || !Memory.OffsetRegNum || Memory.isNegative ||
1681 Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
1682 return false;
1683 return true;
1684 }
1685
1686 bool isMemTBH() const {
1687 if (!isGPRMem() || !Memory.OffsetRegNum || Memory.isNegative ||
1688 Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm != 1 ||
1689 Memory.Alignment != 0 )
1690 return false;
1691 return true;
1692 }
1693
1694 bool isMemRegOffset() const {
1695 if (!isGPRMem() || !Memory.OffsetRegNum || Memory.Alignment != 0)
1696 return false;
1697 return true;
1698 }
1699
1700 bool isT2MemRegOffset() const {
1701 if (!isGPRMem() || !Memory.OffsetRegNum || Memory.isNegative ||
1702 Memory.Alignment != 0 || Memory.BaseRegNum == ARM::PC)
1703 return false;
1704 // Only lsl #{0, 1, 2, 3} allowed.
1705 if (Memory.ShiftType == ARM_AM::no_shift)
1706 return true;
1707 if (Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm > 3)
1708 return false;
1709 return true;
1710 }
1711
1712 bool isMemThumbRR() const {
1713 // Thumb reg+reg addressing is simple. Just two registers, a base and
1714 // an offset. No shifts, negations or any other complicating factors.
1715 if (!isGPRMem() || !Memory.OffsetRegNum || Memory.isNegative ||
1716 Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
1717 return false;
1718 return isARMLowRegister(Memory.BaseRegNum) &&
1719 (!Memory.OffsetRegNum || isARMLowRegister(Memory.OffsetRegNum));
1720 }
1721
1722 bool isMemThumbRIs4() const {
1723 if (!isGPRMem() || Memory.OffsetRegNum != 0 ||
1724 !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
1725 return false;
1726 // Immediate offset, multiple of 4 in range [0, 124].
1727 if (!Memory.OffsetImm) return true;
1728 if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1729 int64_t Val = CE->getValue();
1730 return Val >= 0 && Val <= 124 && (Val % 4) == 0;
1731 }
1732 return false;
1733 }
1734
1735 bool isMemThumbRIs2() const {
1736 if (!isGPRMem() || Memory.OffsetRegNum != 0 ||
1737 !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
1738 return false;
1739 // Immediate offset, multiple of 4 in range [0, 62].
1740 if (!Memory.OffsetImm) return true;
1741 if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1742 int64_t Val = CE->getValue();
1743 return Val >= 0 && Val <= 62 && (Val % 2) == 0;
1744 }
1745 return false;
1746 }
1747
1748 bool isMemThumbRIs1() const {
1749 if (!isGPRMem() || Memory.OffsetRegNum != 0 ||
1750 !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
1751 return false;
1752 // Immediate offset in range [0, 31].
1753 if (!Memory.OffsetImm) return true;
1754 if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1755 int64_t Val = CE->getValue();
1756 return Val >= 0 && Val <= 31;
1757 }
1758 return false;
1759 }
1760
1761 bool isMemThumbSPI() const {
1762 if (!isGPRMem() || Memory.OffsetRegNum != 0 ||
1763 Memory.BaseRegNum != ARM::SP || Memory.Alignment != 0)
1764 return false;
1765 // Immediate offset, multiple of 4 in range [0, 1020].
1766 if (!Memory.OffsetImm) return true;
1767 if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1768 int64_t Val = CE->getValue();
1769 return Val >= 0 && Val <= 1020 && (Val % 4) == 0;
1770 }
1771 return false;
1772 }
1773
1774 bool isMemImm8s4Offset() const {
1775 // If we have an immediate that's not a constant, treat it as a label
1776 // reference needing a fixup. If it is a constant, it's something else
1777 // and we reject it.
1778 if (isImm() && !isa<MCConstantExpr>(getImm()))
1779 return true;
1780 if (!isGPRMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1781 return false;
1782 // Immediate offset a multiple of 4 in range [-1020, 1020].
1783 if (!Memory.OffsetImm) return true;
1784 if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1785 int64_t Val = CE->getValue();
1786 // Special case, #-0 is std::numeric_limits<int32_t>::min().
1787 return (Val >= -1020 && Val <= 1020 && (Val & 3) == 0) ||
1788 Val == std::numeric_limits<int32_t>::min();
1789 }
1790 return false;
1791 }
1792
1793 bool isMemImm7s4Offset() const {
1794 // If we have an immediate that's not a constant, treat it as a label
1795 // reference needing a fixup. If it is a constant, it's something else
1796 // and we reject it.
1797 if (isImm() && !isa<MCConstantExpr>(getImm()))
1798 return true;
1799 if (!isGPRMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0 ||
1800 !ARMMCRegisterClasses[ARM::GPRnopcRegClassID].contains(
1801 Memory.BaseRegNum))
1802 return false;
1803 // Immediate offset a multiple of 4 in range [-508, 508].
1804 if (!Memory.OffsetImm) return true;
1805 if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1806 int64_t Val = CE->getValue();
1807 // Special case, #-0 is INT32_MIN.
1808 return (Val >= -508 && Val <= 508 && (Val & 3) == 0) || Val == INT32_MIN;
1809 }
1810 return false;
1811 }
1812
1813 bool isMemImm0_1020s4Offset() const {
1814 if (!isGPRMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1815 return false;
1816 // Immediate offset a multiple of 4 in range [0, 1020].
1817 if (!Memory.OffsetImm) return true;
1818 if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1819 int64_t Val = CE->getValue();
1820 return Val >= 0 && Val <= 1020 && (Val & 3) == 0;
1821 }
1822 return false;
1823 }
1824
1825 bool isMemImm8Offset() const {
1826 if (!isGPRMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1827 return false;
1828 // Base reg of PC isn't allowed for these encodings.
1829 if (Memory.BaseRegNum == ARM::PC) return false;
1830 // Immediate offset in range [-255, 255].
1831 if (!Memory.OffsetImm) return true;
1832 if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1833 int64_t Val = CE->getValue();
1834 return (Val == std::numeric_limits<int32_t>::min()) ||
1835 (Val > -256 && Val < 256);
1836 }
1837 return false;
1838 }
1839
1840 template<unsigned Bits, unsigned RegClassID>
1841 bool isMemImm7ShiftedOffset() const {
1842 if (!isGPRMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0 ||
1843 !ARMMCRegisterClasses[RegClassID].contains(Memory.BaseRegNum))
1844 return false;
1845
1846 // Expect an immediate offset equal to an element of the range
1847 // [-127, 127], shifted left by Bits.
1848
1849 if (!Memory.OffsetImm) return true;
1850 if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1851 int64_t Val = CE->getValue();
1852
1853 // INT32_MIN is a special-case value (indicating the encoding with
1854 // zero offset and the subtract bit set)
1855 if (Val == INT32_MIN)
1856 return true;
1857
1858 unsigned Divisor = 1U << Bits;
1859
1860 // Check that the low bits are zero
1861 if (Val % Divisor != 0)
1862 return false;
1863
1864 // Check that the remaining offset is within range.
1865 Val /= Divisor;
1866 return (Val >= -127 && Val <= 127);
1867 }
1868 return false;
1869 }
1870
1871 template <int shift> bool isMemRegRQOffset() const {
1872 if (!isMVEMem() || Memory.OffsetImm != nullptr || Memory.Alignment != 0)
1873 return false;
1874
1875 if (!ARMMCRegisterClasses[ARM::GPRnopcRegClassID].contains(
1876 Memory.BaseRegNum))
1877 return false;
1878 if (!ARMMCRegisterClasses[ARM::MQPRRegClassID].contains(
1879 Memory.OffsetRegNum))
1880 return false;
1881
1882 if (shift == 0 && Memory.ShiftType != ARM_AM::no_shift)
1883 return false;
1884
1885 if (shift > 0 &&
1886 (Memory.ShiftType != ARM_AM::uxtw || Memory.ShiftImm != shift))
1887 return false;
1888
1889 return true;
1890 }
1891
1892 template <int shift> bool isMemRegQOffset() const {
1893 if (!isMVEMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1894 return false;
1895
1896 if (!ARMMCRegisterClasses[ARM::MQPRRegClassID].contains(
1897 Memory.BaseRegNum))
1898 return false;
1899
1900 if (!Memory.OffsetImm)
1901 return true;
1902 static_assert(shift < 56,
1903 "Such that we dont shift by a value higher than 62");
1904 if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1905 int64_t Val = CE->getValue();
1906
1907 // The value must be a multiple of (1 << shift)
1908 if ((Val & ((1U << shift) - 1)) != 0)
1909 return false;
1910
1911 // And be in the right range, depending on the amount that it is shifted
1912 // by. Shift 0, is equal to 7 unsigned bits, the sign bit is set
1913 // separately.
1914 int64_t Range = (1U << (7 + shift)) - 1;
1915 return (Val == INT32_MIN) || (Val > -Range && Val < Range);
1916 }
1917 return false;
1918 }
1919
1920 bool isMemPosImm8Offset() const {
1921 if (!isGPRMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1922 return false;
1923 // Immediate offset in range [0, 255].
1924 if (!Memory.OffsetImm) return true;
1925 if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1926 int64_t Val = CE->getValue();
1927 return Val >= 0 && Val < 256;
1928 }
1929 return false;
1930 }
1931
1932 bool isMemNegImm8Offset() const {
1933 if (!isGPRMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1934 return false;
1935 // Base reg of PC isn't allowed for these encodings.
1936 if (Memory.BaseRegNum == ARM::PC) return false;
1937 // Immediate offset in range [-255, -1].
1938 if (!Memory.OffsetImm) return false;
1939 if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1940 int64_t Val = CE->getValue();
1941 return (Val == std::numeric_limits<int32_t>::min()) ||
1942 (Val > -256 && Val < 0);
1943 }
1944 return false;
1945 }
1946
1947 bool isMemUImm12Offset() const {
1948 if (!isGPRMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1949 return false;
1950 // Immediate offset in range [0, 4095].
1951 if (!Memory.OffsetImm) return true;
1952 if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1953 int64_t Val = CE->getValue();
1954 return (Val >= 0 && Val < 4096);
1955 }
1956 return false;
1957 }
1958
1959 bool isMemImm12Offset() const {
1960 // If we have an immediate that's not a constant, treat it as a label
1961 // reference needing a fixup. If it is a constant, it's something else
1962 // and we reject it.
1963
1964 if (isImm() && !isa<MCConstantExpr>(getImm()))
1965 return true;
1966
1967 if (!isGPRMem() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1968 return false;
1969 // Immediate offset in range [-4095, 4095].
1970 if (!Memory.OffsetImm) return true;
1971 if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
1972 int64_t Val = CE->getValue();
1973 return (Val > -4096 && Val < 4096) ||
1974 (Val == std::numeric_limits<int32_t>::min());
1975 }
1976 // If we have an immediate that's not a constant, treat it as a
1977 // symbolic expression needing a fixup.
1978 return true;
1979 }
1980
1981 bool isConstPoolAsmImm() const {
1982 // Delay processing of Constant Pool Immediate, this will turn into
1983 // a constant. Match no other operand
1984 return (isConstantPoolImm());
1985 }
1986
1987 bool isPostIdxImm8() const {
1988 if (!isImm()) return false;
1989 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1990 if (!CE) return false;
1991 int64_t Val = CE->getValue();
1992 return (Val > -256 && Val < 256) ||
1993 (Val == std::numeric_limits<int32_t>::min());
1994 }
1995
1996 bool isPostIdxImm8s4() const {
1997 if (!isImm()) return false;
1998 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1999 if (!CE) return false;
2000 int64_t Val = CE->getValue();
2001 return ((Val & 3) == 0 && Val >= -1020 && Val <= 1020) ||
2002 (Val == std::numeric_limits<int32_t>::min());
2003 }
2004
2005 bool isMSRMask() const { return Kind == k_MSRMask; }
2006 bool isBankedReg() const { return Kind == k_BankedReg; }
2007 bool isProcIFlags() const { return Kind == k_ProcIFlags; }
2008
2009 // NEON operands.
2010 bool isSingleSpacedVectorList() const {
2011 return Kind == k_VectorList && !VectorList.isDoubleSpaced;
2012 }
2013
2014 bool isDoubleSpacedVectorList() const {
2015 return Kind == k_VectorList && VectorList.isDoubleSpaced;
2016 }
2017
2018 bool isVecListOneD() const {
2019 if (!isSingleSpacedVectorList()) return false;
2020 return VectorList.Count == 1;
2021 }
2022
2023 bool isVecListTwoMQ() const {
2024 return isSingleSpacedVectorList() && VectorList.Count == 2 &&
2025 ARMMCRegisterClasses[ARM::MQPRRegClassID].contains(
2026 VectorList.RegNum);
2027 }
2028
2029 bool isVecListDPair() const {
2030 if (!isSingleSpacedVectorList()) return false;
2031 return (ARMMCRegisterClasses[ARM::DPairRegClassID]
2032 .contains(VectorList.RegNum));
2033 }
2034
2035 bool isVecListThreeD() const {
2036 if (!isSingleSpacedVectorList()) return false;
2037 return VectorList.Count == 3;
2038 }
2039
2040 bool isVecListFourD() const {
2041 if (!isSingleSpacedVectorList()) return false;
2042 return VectorList.Count == 4;
2043 }
2044
2045 bool isVecListDPairSpaced() const {
2046 if (Kind != k_VectorList) return false;
2047 if (isSingleSpacedVectorList()) return false;
2048 return (ARMMCRegisterClasses[ARM::DPairSpcRegClassID]
2049 .contains(VectorList.RegNum));
2050 }
2051
2052 bool isVecListThreeQ() const {
2053 if (!isDoubleSpacedVectorList()) return false;
2054 return VectorList.Count == 3;
2055 }
2056
2057 bool isVecListFourQ() const {
2058 if (!isDoubleSpacedVectorList()) return false;
2059 return VectorList.Count == 4;
2060 }
2061
2062 bool isVecListFourMQ() const {
2063 return isSingleSpacedVectorList() && VectorList.Count == 4 &&
2064 ARMMCRegisterClasses[ARM::MQPRRegClassID].contains(
2065 VectorList.RegNum);
2066 }
2067
2068 bool isSingleSpacedVectorAllLanes() const {
2069 return Kind == k_VectorListAllLanes && !VectorList.isDoubleSpaced;
2070 }
2071
2072 bool isDoubleSpacedVectorAllLanes() const {
2073 return Kind == k_VectorListAllLanes && VectorList.isDoubleSpaced;
2074 }
2075
2076 bool isVecListOneDAllLanes() const {
2077 if (!isSingleSpacedVectorAllLanes()) return false;
2078 return VectorList.Count == 1;
2079 }
2080
2081 bool isVecListDPairAllLanes() const {
2082 if (!isSingleSpacedVectorAllLanes()) return false;
2083 return (ARMMCRegisterClasses[ARM::DPairRegClassID]
2084 .contains(VectorList.RegNum));
2085 }
2086
2087 bool isVecListDPairSpacedAllLanes() const {
2088 if (!isDoubleSpacedVectorAllLanes()) return false;
2089 return VectorList.Count == 2;
2090 }
2091
2092 bool isVecListThreeDAllLanes() const {
2093 if (!isSingleSpacedVectorAllLanes()) return false;
2094 return VectorList.Count == 3;
2095 }
2096
2097 bool isVecListThreeQAllLanes() const {
2098 if (!isDoubleSpacedVectorAllLanes()) return false;
2099 return VectorList.Count == 3;
2100 }
2101
2102 bool isVecListFourDAllLanes() const {
2103 if (!isSingleSpacedVectorAllLanes()) return false;
2104 return VectorList.Count == 4;
2105 }
2106
2107 bool isVecListFourQAllLanes() const {
2108 if (!isDoubleSpacedVectorAllLanes()) return false;
2109 return VectorList.Count == 4;
2110 }
2111
2112 bool isSingleSpacedVectorIndexed() const {
2113 return Kind == k_VectorListIndexed && !VectorList.isDoubleSpaced;
2114 }
2115
2116 bool isDoubleSpacedVectorIndexed() const {
2117 return Kind == k_VectorListIndexed && VectorList.isDoubleSpaced;
2118 }
2119
2120 bool isVecListOneDByteIndexed() const {
2121 if (!isSingleSpacedVectorIndexed()) return false;
2122 return VectorList.Count == 1 && VectorList.LaneIndex <= 7;
2123 }
2124
2125 bool isVecListOneDHWordIndexed() const {
2126 if (!isSingleSpacedVectorIndexed()) return false;
2127 return VectorList.Count == 1 && VectorList.LaneIndex <= 3;
2128 }
2129
2130 bool isVecListOneDWordIndexed() const {
2131 if (!isSingleSpacedVectorIndexed()) return false;
2132 return VectorList.Count == 1 && VectorList.LaneIndex <= 1;
2133 }
2134
2135 bool isVecListTwoDByteIndexed() const {
2136 if (!isSingleSpacedVectorIndexed()) return false;
2137 return VectorList.Count == 2 && VectorList.LaneIndex <= 7;
2138 }
2139
2140 bool isVecListTwoDHWordIndexed() const {
2141 if (!isSingleSpacedVectorIndexed()) return false;
2142 return VectorList.Count == 2 && VectorList.LaneIndex <= 3;
2143 }
2144
2145 bool isVecListTwoQWordIndexed() const {
2146 if (!isDoubleSpacedVectorIndexed()) return false;
2147 return VectorList.Count == 2 && VectorList.LaneIndex <= 1;
2148 }
2149
2150 bool isVecListTwoQHWordIndexed() const {
2151 if (!isDoubleSpacedVectorIndexed()) return false;
2152 return VectorList.Count == 2 && VectorList.LaneIndex <= 3;
2153 }
2154
2155 bool isVecListTwoDWordIndexed() const {
2156 if (!isSingleSpacedVectorIndexed()) return false;
2157 return VectorList.Count == 2 && VectorList.LaneIndex <= 1;
2158 }
2159
2160 bool isVecListThreeDByteIndexed() const {
2161 if (!isSingleSpacedVectorIndexed()) return false;
2162 return VectorList.Count == 3 && VectorList.LaneIndex <= 7;
2163 }
2164
2165 bool isVecListThreeDHWordIndexed() const {
2166 if (!isSingleSpacedVectorIndexed()) return false;
2167 return VectorList.Count == 3 && VectorList.LaneIndex <= 3;
2168 }
2169
2170 bool isVecListThreeQWordIndexed() const {
2171 if (!isDoubleSpacedVectorIndexed()) return false;
2172 return VectorList.Count == 3 && VectorList.LaneIndex <= 1;
2173 }
2174
2175 bool isVecListThreeQHWordIndexed() const {
2176 if (!isDoubleSpacedVectorIndexed()) return false;
2177 return VectorList.Count == 3 && VectorList.LaneIndex <= 3;
2178 }
2179
2180 bool isVecListThreeDWordIndexed() const {
2181 if (!isSingleSpacedVectorIndexed()) return false;
2182 return VectorList.Count == 3 && VectorList.LaneIndex <= 1;
2183 }
2184
2185 bool isVecListFourDByteIndexed() const {
2186 if (!isSingleSpacedVectorIndexed()) return false;
2187 return VectorList.Count == 4 && VectorList.LaneIndex <= 7;
2188 }
2189
2190 bool isVecListFourDHWordIndexed() const {
2191 if (!isSingleSpacedVectorIndexed()) return false;
2192 return VectorList.Count == 4 && VectorList.LaneIndex <= 3;
2193 }
2194
2195 bool isVecListFourQWordIndexed() const {
2196 if (!isDoubleSpacedVectorIndexed()) return false;
2197 return VectorList.Count == 4 && VectorList.LaneIndex <= 1;
2198 }
2199
2200 bool isVecListFourQHWordIndexed() const {
2201 if (!isDoubleSpacedVectorIndexed()) return false;
2202 return VectorList.Count == 4 && VectorList.LaneIndex <= 3;
2203 }
2204
2205 bool isVecListFourDWordIndexed() const {
2206 if (!isSingleSpacedVectorIndexed()) return false;
2207 return VectorList.Count == 4 && VectorList.LaneIndex <= 1;
2208 }
2209
2210 bool isVectorIndex() const { return Kind == k_VectorIndex; }
2211
2212 template <unsigned NumLanes>
2213 bool isVectorIndexInRange() const {
2214 if (Kind != k_VectorIndex) return false;
2215 return VectorIndex.Val < NumLanes;
2216 }
2217
2218 bool isVectorIndex8() const { return isVectorIndexInRange<8>(); }
2219 bool isVectorIndex16() const { return isVectorIndexInRange<4>(); }
2220 bool isVectorIndex32() const { return isVectorIndexInRange<2>(); }
2221 bool isVectorIndex64() const { return isVectorIndexInRange<1>(); }
2222
2223 template<int PermittedValue, int OtherPermittedValue>
2224 bool isMVEPairVectorIndex() const {
2225 if (Kind != k_VectorIndex) return false;
2226 return VectorIndex.Val == PermittedValue ||
2227 VectorIndex.Val == OtherPermittedValue;
2228 }
2229
2230 bool isNEONi8splat() const {
2231 if (!isImm()) return false;
2232 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2233 // Must be a constant.
2234 if (!CE) return false;
2235 int64_t Value = CE->getValue();
2236 // i8 value splatted across 8 bytes. The immediate is just the 8 byte
2237 // value.
2238 return Value >= 0 && Value < 256;
2239 }
2240
2241 bool isNEONi16splat() const {
2242 if (isNEONByteReplicate(2))
2243 return false; // Leave that for bytes replication and forbid by default.
2244 if (!isImm())
2245 return false;
2246 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2247 // Must be a constant.
2248 if (!CE) return false;
2249 unsigned Value = CE->getValue();
2251 }
2252
2253 bool isNEONi16splatNot() const {
2254 if (!isImm())
2255 return false;
2256 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2257 // Must be a constant.
2258 if (!CE) return false;
2259 unsigned Value = CE->getValue();
2260 return ARM_AM::isNEONi16splat(~Value & 0xffff);
2261 }
2262
2263 bool isNEONi32splat() const {
2264 if (isNEONByteReplicate(4))
2265 return false; // Leave that for bytes replication and forbid by default.
2266 if (!isImm())
2267 return false;
2268 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2269 // Must be a constant.
2270 if (!CE) return false;
2271 unsigned Value = CE->getValue();
2273 }
2274
2275 bool isNEONi32splatNot() const {
2276 if (!isImm())
2277 return false;
2278 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2279 // Must be a constant.
2280 if (!CE) return false;
2281 unsigned Value = CE->getValue();
2283 }
2284
2285 static bool isValidNEONi32vmovImm(int64_t Value) {
2286 // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X,
2287 // for VMOV/VMVN only, 00Xf or 0Xff are also accepted.
2288 return ((Value & 0xffffffffffffff00) == 0) ||
2289 ((Value & 0xffffffffffff00ff) == 0) ||
2290 ((Value & 0xffffffffff00ffff) == 0) ||
2291 ((Value & 0xffffffff00ffffff) == 0) ||
2292 ((Value & 0xffffffffffff00ff) == 0xff) ||
2293 ((Value & 0xffffffffff00ffff) == 0xffff);
2294 }
2295
2296 bool isNEONReplicate(unsigned Width, unsigned NumElems, bool Inv) const {
2297 assert((Width == 8 || Width == 16 || Width == 32) &&
2298 "Invalid element width");
2299 assert(NumElems * Width <= 64 && "Invalid result width");
2300
2301 if (!isImm())
2302 return false;
2303 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2304 // Must be a constant.
2305 if (!CE)
2306 return false;
2307 int64_t Value = CE->getValue();
2308 if (!Value)
2309 return false; // Don't bother with zero.
2310 if (Inv)
2311 Value = ~Value;
2312
2313 uint64_t Mask = (1ull << Width) - 1;
2314 uint64_t Elem = Value & Mask;
2315 if (Width == 16 && (Elem & 0x00ff) != 0 && (Elem & 0xff00) != 0)
2316 return false;
2317 if (Width == 32 && !isValidNEONi32vmovImm(Elem))
2318 return false;
2319
2320 for (unsigned i = 1; i < NumElems; ++i) {
2321 Value >>= Width;
2322 if ((Value & Mask) != Elem)
2323 return false;
2324 }
2325 return true;
2326 }
2327
2328 bool isNEONByteReplicate(unsigned NumBytes) const {
2329 return isNEONReplicate(8, NumBytes, false);
2330 }
2331
2332 static void checkNeonReplicateArgs(unsigned FromW, unsigned ToW) {
2333 assert((FromW == 8 || FromW == 16 || FromW == 32) &&
2334 "Invalid source width");
2335 assert((ToW == 16 || ToW == 32 || ToW == 64) &&
2336 "Invalid destination width");
2337 assert(FromW < ToW && "ToW is not less than FromW");
2338 }
2339
2340 template<unsigned FromW, unsigned ToW>
2341 bool isNEONmovReplicate() const {
2342 checkNeonReplicateArgs(FromW, ToW);
2343 if (ToW == 64 && isNEONi64splat())
2344 return false;
2345 return isNEONReplicate(FromW, ToW / FromW, false);
2346 }
2347
2348 template<unsigned FromW, unsigned ToW>
2349 bool isNEONinvReplicate() const {
2350 checkNeonReplicateArgs(FromW, ToW);
2351 return isNEONReplicate(FromW, ToW / FromW, true);
2352 }
2353
2354 bool isNEONi32vmov() const {
2355 if (isNEONByteReplicate(4))
2356 return false; // Let it to be classified as byte-replicate case.
2357 if (!isImm())
2358 return false;
2359 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2360 // Must be a constant.
2361 if (!CE)
2362 return false;
2363 return isValidNEONi32vmovImm(CE->getValue());
2364 }
2365
2366 bool isNEONi32vmovNeg() const {
2367 if (!isImm()) return false;
2368 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2369 // Must be a constant.
2370 if (!CE) return false;
2371 return isValidNEONi32vmovImm(~CE->getValue());
2372 }
2373
2374 bool isNEONi64splat() const {
2375 if (!isImm()) return false;
2376 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2377 // Must be a constant.
2378 if (!CE) return false;
2379 uint64_t Value = CE->getValue();
2380 // i64 value with each byte being either 0 or 0xff.
2381 for (unsigned i = 0; i < 8; ++i, Value >>= 8)
2382 if ((Value & 0xff) != 0 && (Value & 0xff) != 0xff) return false;
2383 return true;
2384 }
2385
2386 template<int64_t Angle, int64_t Remainder>
2387 bool isComplexRotation() const {
2388 if (!isImm()) return false;
2389
2390 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2391 if (!CE) return false;
2392 uint64_t Value = CE->getValue();
2393
2394 return (Value % Angle == Remainder && Value <= 270);
2395 }
2396
2397 bool isMVELongShift() const {
2398 if (!isImm()) return false;
2399 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2400 // Must be a constant.
2401 if (!CE) return false;
2402 uint64_t Value = CE->getValue();
2403 return Value >= 1 && Value <= 32;
2404 }
2405
2406 bool isMveSaturateOp() const {
2407 if (!isImm()) return false;
2408 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2409 if (!CE) return false;
2410 uint64_t Value = CE->getValue();
2411 return Value == 48 || Value == 64;
2412 }
2413
2414 bool isITCondCodeNoAL() const {
2415 if (!isITCondCode()) return false;
2417 return CC != ARMCC::AL;
2418 }
2419
2420 bool isITCondCodeRestrictedI() const {
2421 if (!isITCondCode())
2422 return false;
2424 return CC == ARMCC::EQ || CC == ARMCC::NE;
2425 }
2426
2427 bool isITCondCodeRestrictedS() const {
2428 if (!isITCondCode())
2429 return false;
2431 return CC == ARMCC::LT || CC == ARMCC::GT || CC == ARMCC::LE ||
2432 CC == ARMCC::GE;
2433 }
2434
2435 bool isITCondCodeRestrictedU() const {
2436 if (!isITCondCode())
2437 return false;
2439 return CC == ARMCC::HS || CC == ARMCC::HI;
2440 }
2441
2442 bool isITCondCodeRestrictedFP() const {
2443 if (!isITCondCode())
2444 return false;
2446 return CC == ARMCC::EQ || CC == ARMCC::NE || CC == ARMCC::LT ||
2447 CC == ARMCC::GT || CC == ARMCC::LE || CC == ARMCC::GE;
2448 }
2449
2450 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
2451 // Add as immediates when possible. Null MCExpr = 0.
2452 if (!Expr)
2454 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
2455 Inst.addOperand(MCOperand::createImm(CE->getValue()));
2456 else
2458 }
2459
2460 void addARMBranchTargetOperands(MCInst &Inst, unsigned N) const {
2461 assert(N == 1 && "Invalid number of operands!");
2462 addExpr(Inst, getImm());
2463 }
2464
2465 void addThumbBranchTargetOperands(MCInst &Inst, unsigned N) const {
2466 assert(N == 1 && "Invalid number of operands!");
2467 addExpr(Inst, getImm());
2468 }
2469
2470 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
2471 assert(N == 2 && "Invalid number of operands!");
2472 Inst.addOperand(MCOperand::createImm(unsigned(getCondCode())));
2473 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
2474 Inst.addOperand(MCOperand::createReg(RegNum));
2475 }
2476
2477 void addVPTPredNOperands(MCInst &Inst, unsigned N) const {
2478 assert(N == 3 && "Invalid number of operands!");
2479 Inst.addOperand(MCOperand::createImm(unsigned(getVPTPred())));
2480 unsigned RegNum = getVPTPred() == ARMVCC::None ? 0: ARM::P0;
2481 Inst.addOperand(MCOperand::createReg(RegNum));
2483 }
2484
2485 void addVPTPredROperands(MCInst &Inst, unsigned N) const {
2486 assert(N == 4 && "Invalid number of operands!");
2487 addVPTPredNOperands(Inst, N-1);
2488 unsigned RegNum;
2489 if (getVPTPred() == ARMVCC::None) {
2490 RegNum = 0;
2491 } else {
2492 unsigned NextOpIndex = Inst.getNumOperands();
2493 const MCInstrDesc &MCID =
2494 ARMDescs.Insts[ARM::INSTRUCTION_LIST_END - 1 - Inst.getOpcode()];
2495 int TiedOp = MCID.getOperandConstraint(NextOpIndex, MCOI::TIED_TO);
2496 assert(TiedOp >= 0 &&
2497 "Inactive register in vpred_r is not tied to an output!");
2498 RegNum = Inst.getOperand(TiedOp).getReg();
2499 }
2500 Inst.addOperand(MCOperand::createReg(RegNum));
2501 }
2502
2503 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
2504 assert(N == 1 && "Invalid number of operands!");
2505 Inst.addOperand(MCOperand::createImm(getCoproc()));
2506 }
2507
2508 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
2509 assert(N == 1 && "Invalid number of operands!");
2510 Inst.addOperand(MCOperand::createImm(getCoproc()));
2511 }
2512
2513 void addCoprocOptionOperands(MCInst &Inst, unsigned N) const {
2514 assert(N == 1 && "Invalid number of operands!");
2515 Inst.addOperand(MCOperand::createImm(CoprocOption.Val));
2516 }
2517
2518 void addITMaskOperands(MCInst &Inst, unsigned N) const {
2519 assert(N == 1 && "Invalid number of operands!");
2520 Inst.addOperand(MCOperand::createImm(ITMask.Mask));
2521 }
2522
2523 void addITCondCodeOperands(MCInst &Inst, unsigned N) const {
2524 assert(N == 1 && "Invalid number of operands!");
2525 Inst.addOperand(MCOperand::createImm(unsigned(getCondCode())));
2526 }
2527
2528 void addITCondCodeInvOperands(MCInst &Inst, unsigned N) const {
2529 assert(N == 1 && "Invalid number of operands!");
2531 }
2532
2533 void addCCOutOperands(MCInst &Inst, unsigned N) const {
2534 assert(N == 1 && "Invalid number of operands!");
2536 }
2537
2538 void addRegOperands(MCInst &Inst, unsigned N) const {
2539 assert(N == 1 && "Invalid number of operands!");
2541 }
2542
2543 void addRegShiftedRegOperands(MCInst &Inst, unsigned N) const {
2544 assert(N == 3 && "Invalid number of operands!");
2545 assert(isRegShiftedReg() &&
2546 "addRegShiftedRegOperands() on non-RegShiftedReg!");
2547 Inst.addOperand(MCOperand::createReg(RegShiftedReg.SrcReg));
2548 Inst.addOperand(MCOperand::createReg(RegShiftedReg.ShiftReg));
2550 ARM_AM::getSORegOpc(RegShiftedReg.ShiftTy, RegShiftedReg.ShiftImm)));
2551 }
2552
2553 void addRegShiftedImmOperands(MCInst &Inst, unsigned N) const {
2554 assert(N == 2 && "Invalid number of operands!");
2555 assert(isRegShiftedImm() &&
2556 "addRegShiftedImmOperands() on non-RegShiftedImm!");
2557 Inst.addOperand(MCOperand::createReg(RegShiftedImm.SrcReg));
2558 // Shift of #32 is encoded as 0 where permitted
2559 unsigned Imm = (RegShiftedImm.ShiftImm == 32 ? 0 : RegShiftedImm.ShiftImm);
2561 ARM_AM::getSORegOpc(RegShiftedImm.ShiftTy, Imm)));
2562 }
2563
2564 void addShifterImmOperands(MCInst &Inst, unsigned N) const {
2565 assert(N == 1 && "Invalid number of operands!");
2566 Inst.addOperand(MCOperand::createImm((ShifterImm.isASR << 5) |
2567 ShifterImm.Imm));
2568 }
2569
2570 void addRegListOperands(MCInst &Inst, unsigned N) const {
2571 assert(N == 1 && "Invalid number of operands!");
2572 const SmallVectorImpl<unsigned> &RegList = getRegList();
2573 for (unsigned Reg : RegList)
2575 }
2576
2577 void addRegListWithAPSROperands(MCInst &Inst, unsigned N) const {
2578 assert(N == 1 && "Invalid number of operands!");
2579 const SmallVectorImpl<unsigned> &RegList = getRegList();
2580 for (unsigned Reg : RegList)
2582 }
2583
2584 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
2585 addRegListOperands(Inst, N);
2586 }
2587
2588 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
2589 addRegListOperands(Inst, N);
2590 }
2591
2592 void addFPSRegListWithVPROperands(MCInst &Inst, unsigned N) const {
2593 addRegListOperands(Inst, N);
2594 }
2595
2596 void addFPDRegListWithVPROperands(MCInst &Inst, unsigned N) const {
2597 addRegListOperands(Inst, N);
2598 }
2599
2600 void addRotImmOperands(MCInst &Inst, unsigned N) const {
2601 assert(N == 1 && "Invalid number of operands!");
2602 // Encoded as val>>3. The printer handles display as 8, 16, 24.
2603 Inst.addOperand(MCOperand::createImm(RotImm.Imm >> 3));
2604 }
2605
2606 void addModImmOperands(MCInst &Inst, unsigned N) const {
2607 assert(N == 1 && "Invalid number of operands!");
2608
2609 // Support for fixups (MCFixup)
2610 if (isImm())
2611 return addImmOperands(Inst, N);
2612
2613 Inst.addOperand(MCOperand::createImm(ModImm.Bits | (ModImm.Rot << 7)));
2614 }
2615
2616 void addModImmNotOperands(MCInst &Inst, unsigned N) const {
2617 assert(N == 1 && "Invalid number of operands!");
2618 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2619 uint32_t Enc = ARM_AM::getSOImmVal(~CE->getValue());
2621 }
2622
2623 void addModImmNegOperands(MCInst &Inst, unsigned N) const {
2624 assert(N == 1 && "Invalid number of operands!");
2625 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2626 uint32_t Enc = ARM_AM::getSOImmVal(-CE->getValue());
2628 }
2629
2630 void addThumbModImmNeg8_255Operands(MCInst &Inst, unsigned N) const {
2631 assert(N == 1 && "Invalid number of operands!");
2632 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2633 uint32_t Val = -CE->getValue();
2635 }
2636
2637 void addThumbModImmNeg1_7Operands(MCInst &Inst, unsigned N) const {
2638 assert(N == 1 && "Invalid number of operands!");
2639 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2640 uint32_t Val = -CE->getValue();
2642 }
2643
2644 void addBitfieldOperands(MCInst &Inst, unsigned N) const {
2645 assert(N == 1 && "Invalid number of operands!");
2646 // Munge the lsb/width into a bitfield mask.
2647 unsigned lsb = Bitfield.LSB;
2648 unsigned width = Bitfield.Width;
2649 // Make a 32-bit mask w/ the referenced bits clear and all other bits set.
2650 uint32_t Mask = ~(((uint32_t)0xffffffff >> lsb) << (32 - width) >>
2651 (32 - (lsb + width)));
2652 Inst.addOperand(MCOperand::createImm(Mask));
2653 }
2654
2655 void addImmOperands(MCInst &Inst, unsigned N) const {
2656 assert(N == 1 && "Invalid number of operands!");
2657 addExpr(Inst, getImm());
2658 }
2659
2660 void addFBits16Operands(MCInst &Inst, unsigned N) const {
2661 assert(N == 1 && "Invalid number of operands!");
2662 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2663 Inst.addOperand(MCOperand::createImm(16 - CE->getValue()));
2664 }
2665
2666 void addFBits32Operands(MCInst &Inst, unsigned N) const {
2667 assert(N == 1 && "Invalid number of operands!");
2668 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2669 Inst.addOperand(MCOperand::createImm(32 - CE->getValue()));
2670 }
2671
2672 void addFPImmOperands(MCInst &Inst, unsigned N) const {
2673 assert(N == 1 && "Invalid number of operands!");
2674 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2675 int Val = ARM_AM::getFP32Imm(APInt(32, CE->getValue()));
2677 }
2678
2679 void addImm8s4Operands(MCInst &Inst, unsigned N) const {
2680 assert(N == 1 && "Invalid number of operands!");
2681 // FIXME: We really want to scale the value here, but the LDRD/STRD
2682 // instruction don't encode operands that way yet.
2683 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2684 Inst.addOperand(MCOperand::createImm(CE->getValue()));
2685 }
2686
2687 void addImm7s4Operands(MCInst &Inst, unsigned N) const {
2688 assert(N == 1 && "Invalid number of operands!");
2689 // FIXME: We really want to scale the value here, but the VSTR/VLDR_VSYSR
2690 // instruction don't encode operands that way yet.
2691 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2692 Inst.addOperand(MCOperand::createImm(CE->getValue()));
2693 }
2694
2695 void addImm7Shift0Operands(MCInst &Inst, unsigned N) const {
2696 assert(N == 1 && "Invalid number of operands!");
2697 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2698 Inst.addOperand(MCOperand::createImm(CE->getValue()));
2699 }
2700
2701 void addImm7Shift1Operands(MCInst &Inst, unsigned N) const {
2702 assert(N == 1 && "Invalid number of operands!");
2703 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2704 Inst.addOperand(MCOperand::createImm(CE->getValue()));
2705 }
2706
2707 void addImm7Shift2Operands(MCInst &Inst, unsigned N) const {
2708 assert(N == 1 && "Invalid number of operands!");
2709 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2710 Inst.addOperand(MCOperand::createImm(CE->getValue()));
2711 }
2712
2713 void addImm7Operands(MCInst &Inst, unsigned N) const {
2714 assert(N == 1 && "Invalid number of operands!");
2715 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2716 Inst.addOperand(MCOperand::createImm(CE->getValue()));
2717 }
2718
2719 void addImm0_1020s4Operands(MCInst &Inst, unsigned N) const {
2720 assert(N == 1 && "Invalid number of operands!");
2721 // The immediate is scaled by four in the encoding and is stored
2722 // in the MCInst as such. Lop off the low two bits here.
2723 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2724 Inst.addOperand(MCOperand::createImm(CE->getValue() / 4));
2725 }
2726
2727 void addImm0_508s4NegOperands(MCInst &Inst, unsigned N) const {
2728 assert(N == 1 && "Invalid number of operands!");
2729 // The immediate is scaled by four in the encoding and is stored
2730 // in the MCInst as such. Lop off the low two bits here.
2731 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2732 Inst.addOperand(MCOperand::createImm(-(CE->getValue() / 4)));
2733 }
2734
2735 void addImm0_508s4Operands(MCInst &Inst, unsigned N) const {
2736 assert(N == 1 && "Invalid number of operands!");
2737 // The immediate is scaled by four in the encoding and is stored
2738 // in the MCInst as such. Lop off the low two bits here.
2739 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2740 Inst.addOperand(MCOperand::createImm(CE->getValue() / 4));
2741 }
2742
2743 void addImm1_16Operands(MCInst &Inst, unsigned N) const {
2744 assert(N == 1 && "Invalid number of operands!");
2745 // The constant encodes as the immediate-1, and we store in the instruction
2746 // the bits as encoded, so subtract off one here.
2747 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2748 Inst.addOperand(MCOperand::createImm(CE->getValue() - 1));
2749 }
2750
2751 void addImm1_32Operands(MCInst &Inst, unsigned N) const {
2752 assert(N == 1 && "Invalid number of operands!");
2753 // The constant encodes as the immediate-1, and we store in the instruction
2754 // the bits as encoded, so subtract off one here.
2755 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2756 Inst.addOperand(MCOperand::createImm(CE->getValue() - 1));
2757 }
2758
2759 void addImmThumbSROperands(MCInst &Inst, unsigned N) const {
2760 assert(N == 1 && "Invalid number of operands!");
2761 // The constant encodes as the immediate, except for 32, which encodes as
2762 // zero.
2763 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2764 unsigned Imm = CE->getValue();
2765 Inst.addOperand(MCOperand::createImm((Imm == 32 ? 0 : Imm)));
2766 }
2767
2768 void addPKHASRImmOperands(MCInst &Inst, unsigned N) const {
2769 assert(N == 1 && "Invalid number of operands!");
2770 // An ASR value of 32 encodes as 0, so that's how we want to add it to
2771 // the instruction as well.
2772 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2773 int Val = CE->getValue();
2774 Inst.addOperand(MCOperand::createImm(Val == 32 ? 0 : Val));
2775 }
2776
2777 void addT2SOImmNotOperands(MCInst &Inst, unsigned N) const {
2778 assert(N == 1 && "Invalid number of operands!");
2779 // The operand is actually a t2_so_imm, but we have its bitwise
2780 // negation in the assembly source, so twiddle it here.
2781 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2782 Inst.addOperand(MCOperand::createImm(~(uint32_t)CE->getValue()));
2783 }
2784
2785 void addT2SOImmNegOperands(MCInst &Inst, unsigned N) const {
2786 assert(N == 1 && "Invalid number of operands!");
2787 // The operand is actually a t2_so_imm, but we have its
2788 // negation in the assembly source, so twiddle it here.
2789 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2790 Inst.addOperand(MCOperand::createImm(-(uint32_t)CE->getValue()));
2791 }
2792
2793 void addImm0_4095NegOperands(MCInst &Inst, unsigned N) const {
2794 assert(N == 1 && "Invalid number of operands!");
2795 // The operand is actually an imm0_4095, but we have its
2796 // negation in the assembly source, so twiddle it here.
2797 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2798 Inst.addOperand(MCOperand::createImm(-(uint32_t)CE->getValue()));
2799 }
2800
2801 void addUnsignedOffset_b8s2Operands(MCInst &Inst, unsigned N) const {
2802 if(const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm())) {
2803 Inst.addOperand(MCOperand::createImm(CE->getValue() >> 2));
2804 return;
2805 }
2806 const MCSymbolRefExpr *SR = cast<MCSymbolRefExpr>(Imm.Val);
2808 }
2809
2810 void addThumbMemPCOperands(MCInst &Inst, unsigned N) const {
2811 assert(N == 1 && "Invalid number of operands!");
2812 if (isImm()) {
2813 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2814 if (CE) {
2815 Inst.addOperand(MCOperand::createImm(CE->getValue()));
2816 return;
2817 }
2818 const MCSymbolRefExpr *SR = cast<MCSymbolRefExpr>(Imm.Val);
2820 return;
2821 }
2822
2823 assert(isGPRMem() && "Unknown value type!");
2824 assert(isa<MCConstantExpr>(Memory.OffsetImm) && "Unknown value type!");
2825 if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm))
2826 Inst.addOperand(MCOperand::createImm(CE->getValue()));
2827 else
2828 Inst.addOperand(MCOperand::createExpr(Memory.OffsetImm));
2829 }
2830
2831 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
2832 assert(N == 1 && "Invalid number of operands!");
2833 Inst.addOperand(MCOperand::createImm(unsigned(getMemBarrierOpt())));
2834 }
2835
2836 void addInstSyncBarrierOptOperands(MCInst &Inst, unsigned N) const {
2837 assert(N == 1 && "Invalid number of operands!");
2838 Inst.addOperand(MCOperand::createImm(unsigned(getInstSyncBarrierOpt())));
2839 }
2840
2841 void addTraceSyncBarrierOptOperands(MCInst &Inst, unsigned N) const {
2842 assert(N == 1 && "Invalid number of operands!");
2843 Inst.addOperand(MCOperand::createImm(unsigned(getTraceSyncBarrierOpt())));
2844 }
2845
2846 void addMemNoOffsetOperands(MCInst &Inst, unsigned N) const {
2847 assert(N == 1 && "Invalid number of operands!");
2848 Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2849 }
2850
2851 void addMemNoOffsetT2Operands(MCInst &Inst, unsigned N) const {
2852 assert(N == 1 && "Invalid number of operands!");
2853 Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2854 }
2855
2856 void addMemNoOffsetT2NoSpOperands(MCInst &Inst, unsigned N) const {
2857 assert(N == 1 && "Invalid number of operands!");
2858 Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2859 }
2860
2861 void addMemNoOffsetTOperands(MCInst &Inst, unsigned N) const {
2862 assert(N == 1 && "Invalid number of operands!");
2863 Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2864 }
2865
2866 void addMemPCRelImm12Operands(MCInst &Inst, unsigned N) const {
2867 assert(N == 1 && "Invalid number of operands!");
2868 if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm))
2869 Inst.addOperand(MCOperand::createImm(CE->getValue()));
2870 else
2871 Inst.addOperand(MCOperand::createExpr(Memory.OffsetImm));
2872 }
2873
2874 void addAdrLabelOperands(MCInst &Inst, unsigned N) const {
2875 assert(N == 1 && "Invalid number of operands!");
2876 assert(isImm() && "Not an immediate!");
2877
2878 // If we have an immediate that's not a constant, treat it as a label
2879 // reference needing a fixup.
2880 if (!isa<MCConstantExpr>(getImm())) {
2881 Inst.addOperand(MCOperand::createExpr(getImm()));
2882 return;
2883 }
2884
2885 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
2886 int Val = CE->getValue();
2888 }
2889
2890 void addAlignedMemoryOperands(MCInst &Inst, unsigned N) const {
2891 assert(N == 2 && "Invalid number of operands!");
2892 Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2893 Inst.addOperand(MCOperand::createImm(Memory.Alignment));
2894 }
2895
2896 void addDupAlignedMemoryNoneOperands(MCInst &Inst, unsigned N) const {
2897 addAlignedMemoryOperands(Inst, N);
2898 }
2899
2900 void addAlignedMemoryNoneOperands(MCInst &Inst, unsigned N) const {
2901 addAlignedMemoryOperands(Inst, N);
2902 }
2903
2904 void addAlignedMemory16Operands(MCInst &Inst, unsigned N) const {
2905 addAlignedMemoryOperands(Inst, N);
2906 }
2907
2908 void addDupAlignedMemory16Operands(MCInst &Inst, unsigned N) const {
2909 addAlignedMemoryOperands(Inst, N);
2910 }
2911
2912 void addAlignedMemory32Operands(MCInst &Inst, unsigned N) const {
2913 addAlignedMemoryOperands(Inst, N);
2914 }
2915
2916 void addDupAlignedMemory32Operands(MCInst &Inst, unsigned N) const {
2917 addAlignedMemoryOperands(Inst, N);
2918 }
2919
2920 void addAlignedMemory64Operands(MCInst &Inst, unsigned N) const {
2921 addAlignedMemoryOperands(Inst, N);
2922 }
2923
2924 void addDupAlignedMemory64Operands(MCInst &Inst, unsigned N) const {
2925 addAlignedMemoryOperands(Inst, N);
2926 }
2927
2928 void addAlignedMemory64or128Operands(MCInst &Inst, unsigned N) const {
2929 addAlignedMemoryOperands(Inst, N);
2930 }
2931
2932 void addDupAlignedMemory64or128Operands(MCInst &Inst, unsigned N) const {
2933 addAlignedMemoryOperands(Inst, N);
2934 }
2935
2936 void addAlignedMemory64or128or256Operands(MCInst &Inst, unsigned N) const {
2937 addAlignedMemoryOperands(Inst, N);
2938 }
2939
2940 void addAddrMode2Operands(MCInst &Inst, unsigned N) const {
2941 assert(N == 3 && "Invalid number of operands!");
2942 Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2943 Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2944 if (!Memory.OffsetRegNum) {
2945 if (!Memory.OffsetImm)
2947 else if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
2948 int32_t Val = CE->getValue();
2950 // Special case for #-0
2951 if (Val == std::numeric_limits<int32_t>::min())
2952 Val = 0;
2953 if (Val < 0)
2954 Val = -Val;
2955 Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
2957 } else
2958 Inst.addOperand(MCOperand::createExpr(Memory.OffsetImm));
2959 } else {
2960 // For register offset, we encode the shift type and negation flag
2961 // here.
2962 int32_t Val =
2964 Memory.ShiftImm, Memory.ShiftType);
2966 }
2967 }
2968
2969 void addAM2OffsetImmOperands(MCInst &Inst, unsigned N) const {
2970 assert(N == 2 && "Invalid number of operands!");
2971 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
2972 assert(CE && "non-constant AM2OffsetImm operand!");
2973 int32_t Val = CE->getValue();
2975 // Special case for #-0
2976 if (Val == std::numeric_limits<int32_t>::min()) Val = 0;
2977 if (Val < 0) Val = -Val;
2978 Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
2981 }
2982
2983 void addAddrMode3Operands(MCInst &Inst, unsigned N) const {
2984 assert(N == 3 && "Invalid number of operands!");
2985 // If we have an immediate that's not a constant, treat it as a label
2986 // reference needing a fixup. If it is a constant, it's something else
2987 // and we reject it.
2988 if (isImm()) {
2989 Inst.addOperand(MCOperand::createExpr(getImm()));
2992 return;
2993 }
2994
2995 Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
2996 Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
2997 if (!Memory.OffsetRegNum) {
2998 if (!Memory.OffsetImm)
3000 else if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
3001 int32_t Val = CE->getValue();
3003 // Special case for #-0
3004 if (Val == std::numeric_limits<int32_t>::min())
3005 Val = 0;
3006 if (Val < 0)
3007 Val = -Val;
3008 Val = ARM_AM::getAM3Opc(AddSub, Val);
3010 } else
3011 Inst.addOperand(MCOperand::createExpr(Memory.OffsetImm));
3012 } else {
3013 // For register offset, we encode the shift type and negation flag
3014 // here.
3015 int32_t Val =
3018 }
3019 }
3020
3021 void addAM3OffsetOperands(MCInst &Inst, unsigned N) const {
3022 assert(N == 2 && "Invalid number of operands!");
3023 if (Kind == k_PostIndexRegister) {
3024 int32_t Val =
3025 ARM_AM::getAM3Opc(PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub, 0);
3026 Inst.addOperand(MCOperand::createReg(PostIdxReg.RegNum));
3028 return;
3029 }
3030
3031 // Constant offset.
3032 const MCConstantExpr *CE = static_cast<const MCConstantExpr*>(getImm());
3033 int32_t Val = CE->getValue();
3035 // Special case for #-0
3036 if (Val == std::numeric_limits<int32_t>::min()) Val = 0;
3037 if (Val < 0) Val = -Val;
3038 Val = ARM_AM::getAM3Opc(AddSub, Val);
3041 }
3042
3043 void addAddrMode5Operands(MCInst &Inst, unsigned N) const {
3044 assert(N == 2 && "Invalid number of operands!");
3045 // If we have an immediate that's not a constant, treat it as a label
3046 // reference needing a fixup. If it is a constant, it's something else
3047 // and we reject it.
3048 if (isImm()) {
3049 Inst.addOperand(MCOperand::createExpr(getImm()));
3051 return;
3052 }
3053
3054 Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3055 if (!Memory.OffsetImm)
3057 else if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
3058 // The lower two bits are always zero and as such are not encoded.
3059 int32_t Val = CE->getValue() / 4;
3061 // Special case for #-0
3062 if (Val == std::numeric_limits<int32_t>::min())
3063 Val = 0;
3064 if (Val < 0)
3065 Val = -Val;
3066 Val = ARM_AM::getAM5Opc(AddSub, Val);
3068 } else
3069 Inst.addOperand(MCOperand::createExpr(Memory.OffsetImm));
3070 }
3071
3072 void addAddrMode5FP16Operands(MCInst &Inst, unsigned N) const {
3073 assert(N == 2 && "Invalid number of operands!");
3074 // If we have an immediate that's not a constant, treat it as a label
3075 // reference needing a fixup. If it is a constant, it's something else
3076 // and we reject it.
3077 if (isImm()) {
3078 Inst.addOperand(MCOperand::createExpr(getImm()));
3080 return;
3081 }
3082
3083 Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3084 // The lower bit is always zero and as such is not encoded.
3085 if (!Memory.OffsetImm)
3087 else if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm)) {
3088 int32_t Val = CE->getValue() / 2;
3090 // Special case for #-0
3091 if (Val == std::numeric_limits<int32_t>::min())
3092 Val = 0;
3093 if (Val < 0)
3094 Val = -Val;
3095 Val = ARM_AM::getAM5FP16Opc(AddSub, Val);
3097 } else
3098 Inst.addOperand(MCOperand::createExpr(Memory.OffsetImm));
3099 }
3100
3101 void addMemImm8s4OffsetOperands(MCInst &Inst, unsigned N) const {
3102 assert(N == 2 && "Invalid number of operands!");
3103 // If we have an immediate that's not a constant, treat it as a label
3104 // reference needing a fixup. If it is a constant, it's something else
3105 // and we reject it.
3106 if (isImm()) {
3107 Inst.addOperand(MCOperand::createExpr(getImm()));
3109 return;
3110 }
3111
3112 Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3113 addExpr(Inst, Memory.OffsetImm);
3114 }
3115
3116 void addMemImm7s4OffsetOperands(MCInst &Inst, unsigned N) const {
3117 assert(N == 2 && "Invalid number of operands!");
3118 // If we have an immediate that's not a constant, treat it as a label
3119 // reference needing a fixup. If it is a constant, it's something else
3120 // and we reject it.
3121 if (isImm()) {
3122 Inst.addOperand(MCOperand::createExpr(getImm()));
3124 return;
3125 }
3126
3127 Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3128 addExpr(Inst, Memory.OffsetImm);
3129 }
3130
3131 void addMemImm0_1020s4OffsetOperands(MCInst &Inst, unsigned N) const {
3132 assert(N == 2 && "Invalid number of operands!");
3133 Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3134 if (!Memory.OffsetImm)
3136 else if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm))
3137 // The lower two bits are always zero and as such are not encoded.
3138 Inst.addOperand(MCOperand::createImm(CE->getValue() / 4));
3139 else
3140 Inst.addOperand(MCOperand::createExpr(Memory.OffsetImm));
3141 }
3142
3143 void addMemImmOffsetOperands(MCInst &Inst, unsigned N) const {
3144 assert(N == 2 && "Invalid number of operands!");
3145 Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3146 addExpr(Inst, Memory.OffsetImm);
3147 }
3148
3149 void addMemRegRQOffsetOperands(MCInst &Inst, unsigned N) const {
3150 assert(N == 2 && "Invalid number of operands!");
3151 Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3152 Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
3153 }
3154
3155 void addMemUImm12OffsetOperands(MCInst &Inst, unsigned N) const {
3156 assert(N == 2 && "Invalid number of operands!");
3157 // If this is an immediate, it's a label reference.
3158 if (isImm()) {
3159 addExpr(Inst, getImm());
3161 return;
3162 }
3163
3164 // Otherwise, it's a normal memory reg+offset.
3165 Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3166 addExpr(Inst, Memory.OffsetImm);
3167 }
3168
3169 void addMemImm12OffsetOperands(MCInst &Inst, unsigned N) const {
3170 assert(N == 2 && "Invalid number of operands!");
3171 // If this is an immediate, it's a label reference.
3172 if (isImm()) {
3173 addExpr(Inst, getImm());
3175 return;
3176 }
3177
3178 // Otherwise, it's a normal memory reg+offset.
3179 Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3180 addExpr(Inst, Memory.OffsetImm);
3181 }
3182
3183 void addConstPoolAsmImmOperands(MCInst &Inst, unsigned N) const {
3184 assert(N == 1 && "Invalid number of operands!");
3185 // This is container for the immediate that we will create the constant
3186 // pool from
3187 addExpr(Inst, getConstantPoolImm());
3188 }
3189
3190 void addMemTBBOperands(MCInst &Inst, unsigned N) const {
3191 assert(N == 2 && "Invalid number of operands!");
3192 Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3193 Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
3194 }
3195
3196 void addMemTBHOperands(MCInst &Inst, unsigned N) const {
3197 assert(N == 2 && "Invalid number of operands!");
3198 Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3199 Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
3200 }
3201
3202 void addMemRegOffsetOperands(MCInst &Inst, unsigned N) const {
3203 assert(N == 3 && "Invalid number of operands!");
3204 unsigned Val =
3206 Memory.ShiftImm, Memory.ShiftType);
3207 Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3208 Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
3210 }
3211
3212 void addT2MemRegOffsetOperands(MCInst &Inst, unsigned N) const {
3213 assert(N == 3 && "Invalid number of operands!");
3214 Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3215 Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
3216 Inst.addOperand(MCOperand::createImm(Memory.ShiftImm));
3217 }
3218
3219 void addMemThumbRROperands(MCInst &Inst, unsigned N) const {
3220 assert(N == 2 && "Invalid number of operands!");
3221 Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3222 Inst.addOperand(MCOperand::createReg(Memory.OffsetRegNum));
3223 }
3224
3225 void addMemThumbRIs4Operands(MCInst &Inst, unsigned N) const {
3226 assert(N == 2 && "Invalid number of operands!");
3227 Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3228 if (!Memory.OffsetImm)
3230 else if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm))
3231 // The lower two bits are always zero and as such are not encoded.
3232 Inst.addOperand(MCOperand::createImm(CE->getValue() / 4));
3233 else
3234 Inst.addOperand(MCOperand::createExpr(Memory.OffsetImm));
3235 }
3236
3237 void addMemThumbRIs2Operands(MCInst &Inst, unsigned N) const {
3238 assert(N == 2 && "Invalid number of operands!");
3239 Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3240 if (!Memory.OffsetImm)
3242 else if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm))
3243 Inst.addOperand(MCOperand::createImm(CE->getValue() / 2));
3244 else
3245 Inst.addOperand(MCOperand::createExpr(Memory.OffsetImm));
3246 }
3247
3248 void addMemThumbRIs1Operands(MCInst &Inst, unsigned N) const {
3249 assert(N == 2 && "Invalid number of operands!");
3250 Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3251 addExpr(Inst, Memory.OffsetImm);
3252 }
3253
3254 void addMemThumbSPIOperands(MCInst &Inst, unsigned N) const {
3255 assert(N == 2 && "Invalid number of operands!");
3256 Inst.addOperand(MCOperand::createReg(Memory.BaseRegNum));
3257 if (!Memory.OffsetImm)
3259 else if (const auto *CE = dyn_cast<MCConstantExpr>(Memory.OffsetImm))
3260 // The lower two bits are always zero and as such are not encoded.
3261 Inst.addOperand(MCOperand::createImm(CE->getValue() / 4));
3262 else
3263 Inst.addOperand(MCOperand::createExpr(Memory.OffsetImm));
3264 }
3265
3266 void addPostIdxImm8Operands(MCInst &Inst, unsigned N) const {
3267 assert(N == 1 && "Invalid number of operands!");
3268 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
3269 assert(CE && "non-constant post-idx-imm8 operand!");
3270 int Imm = CE->getValue();
3271 bool isAdd = Imm >= 0;
3272 if (Imm == std::numeric_limits<int32_t>::min()) Imm = 0;
3273 Imm = (Imm < 0 ? -Imm : Imm) | (int)isAdd << 8;
3275 }
3276
3277 void addPostIdxImm8s4Operands(MCInst &Inst, unsigned N) const {
3278 assert(N == 1 && "Invalid number of operands!");
3279 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
3280 assert(CE && "non-constant post-idx-imm8s4 operand!");
3281 int Imm = CE->getValue();
3282 bool isAdd = Imm >= 0;
3283 if (Imm == std::numeric_limits<int32_t>::min()) Imm = 0;
3284 // Immediate is scaled by 4.
3285 Imm = ((Imm < 0 ? -Imm : Imm) / 4) | (int)isAdd << 8;
3287 }
3288
3289 void addPostIdxRegOperands(MCInst &Inst, unsigned N) const {
3290 assert(N == 2 && "Invalid number of operands!");
3291 Inst.addOperand(MCOperand::createReg(PostIdxReg.RegNum));
3292 Inst.addOperand(MCOperand::createImm(PostIdxReg.isAdd));
3293 }
3294
3295 void addPostIdxRegShiftedOperands(MCInst &Inst, unsigned N) const {
3296 assert(N == 2 && "Invalid number of operands!");
3297 Inst.addOperand(MCOperand::createReg(PostIdxReg.RegNum));
3298 // The sign, shift type, and shift amount are encoded in a single operand
3299 // using the AM2 encoding helpers.
3300 ARM_AM::AddrOpc opc = PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub;
3301 unsigned Imm = ARM_AM::getAM2Opc(opc, PostIdxReg.ShiftImm,
3302 PostIdxReg.ShiftTy);
3304 }
3305
3306 void addPowerTwoOperands(MCInst &Inst, unsigned N) const {
3307 assert(N == 1 && "Invalid number of operands!");
3308 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
3309 Inst.addOperand(MCOperand::createImm(CE->getValue()));
3310 }
3311
3312 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
3313 assert(N == 1 && "Invalid number of operands!");
3314 Inst.addOperand(MCOperand::createImm(unsigned(getMSRMask())));
3315 }
3316
3317 void addBankedRegOperands(MCInst &Inst, unsigned N) const {
3318 assert(N == 1 && "Invalid number of operands!");
3319 Inst.addOperand(MCOperand::createImm(unsigned(getBankedReg())));
3320 }
3321
3322 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
3323 assert(N == 1 && "Invalid number of operands!");
3324 Inst.addOperand(MCOperand::createImm(unsigned(getProcIFlags())));
3325 }
3326
3327 void addVecListOperands(MCInst &Inst, unsigned N) const {
3328 assert(N == 1 && "Invalid number of operands!");
3329 Inst.addOperand(MCOperand::createReg(VectorList.RegNum));
3330 }
3331
3332 void addMVEVecListOperands(MCInst &Inst, unsigned N) const {
3333 assert(N == 1 && "Invalid number of operands!");
3334
3335 // When we come here, the VectorList field will identify a range
3336 // of q-registers by its base register and length, and it will
3337 // have already been error-checked to be the expected length of
3338 // range and contain only q-regs in the range q0-q7. So we can
3339 // count on the base register being in the range q0-q6 (for 2
3340 // regs) or q0-q4 (for 4)
3341 //
3342 // The MVE instructions taking a register range of this kind will
3343 // need an operand in the MQQPR or MQQQQPR class, representing the
3344 // entire range as a unit. So we must translate into that class,
3345 // by finding the index of the base register in the MQPR reg
3346 // class, and returning the super-register at the corresponding
3347 // index in the target class.
3348
3349 const MCRegisterClass *RC_in = &ARMMCRegisterClasses[ARM::MQPRRegClassID];
3350 const MCRegisterClass *RC_out =
3351 (VectorList.Count == 2) ? &ARMMCRegisterClasses[ARM::MQQPRRegClassID]
3352 : &ARMMCRegisterClasses[ARM::MQQQQPRRegClassID];
3353
3354 unsigned I, E = RC_out->getNumRegs();
3355 for (I = 0; I < E; I++)
3356 if (RC_in->getRegister(I) == VectorList.RegNum)
3357 break;
3358 assert(I < E && "Invalid vector list start register!");
3359
3361 }
3362
3363 void addVecListIndexedOperands(MCInst &Inst, unsigned N) const {
3364 assert(N == 2 && "Invalid number of operands!");
3365 Inst.addOperand(MCOperand::createReg(VectorList.RegNum));
3366 Inst.addOperand(MCOperand::createImm(VectorList.LaneIndex));
3367 }
3368
3369 void addVectorIndex8Operands(MCInst &Inst, unsigned N) const {
3370 assert(N == 1 && "Invalid number of operands!");
3371 Inst.addOperand(MCOperand::createImm(getVectorIndex()));
3372 }
3373
3374 void addVectorIndex16Operands(MCInst &Inst, unsigned N) const {
3375 assert(N == 1 && "Invalid number of operands!");
3376 Inst.addOperand(MCOperand::createImm(getVectorIndex()));
3377 }
3378
3379 void addVectorIndex32Operands(MCInst &Inst, unsigned N) const {
3380 assert(N == 1 && "Invalid number of operands!");
3381 Inst.addOperand(MCOperand::createImm(getVectorIndex()));
3382 }
3383
3384 void addVectorIndex64Operands(MCInst &Inst, unsigned N) const {
3385 assert(N == 1 && "Invalid number of operands!");
3386 Inst.addOperand(MCOperand::createImm(getVectorIndex()));
3387 }
3388
3389 void addMVEVectorIndexOperands(MCInst &Inst, unsigned N) const {
3390 assert(N == 1 && "Invalid number of operands!");
3391 Inst.addOperand(MCOperand::createImm(getVectorIndex()));
3392 }
3393
3394 void addMVEPairVectorIndexOperands(MCInst &Inst, unsigned N) const {
3395 assert(N == 1 && "Invalid number of operands!");
3396 Inst.addOperand(MCOperand::createImm(getVectorIndex()));
3397 }
3398
3399 void addNEONi8splatOperands(MCInst &Inst, unsigned N) const {
3400 assert(N == 1 && "Invalid number of operands!");
3401 // The immediate encodes the type of constant as well as the value.
3402 // Mask in that this is an i8 splat.
3403 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
3404 Inst.addOperand(MCOperand::createImm(CE->getValue() | 0xe00));
3405 }
3406
3407 void addNEONi16splatOperands(MCInst &Inst, unsigned N) const {
3408 assert(N == 1 && "Invalid number of operands!");
3409 // The immediate encodes the type of constant as well as the value.
3410 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
3411 unsigned Value = CE->getValue();
3414 }
3415
3416 void addNEONi16splatNotOperands(MCInst &Inst, unsigned N) const {
3417 assert(N == 1 && "Invalid number of operands!");
3418 // The immediate encodes the type of constant as well as the value.
3419 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
3420 unsigned Value = CE->getValue();
3423 }
3424
3425 void addNEONi32splatOperands(MCInst &Inst, unsigned N) const {
3426 assert(N == 1 && "Invalid number of operands!");
3427 // The immediate encodes the type of constant as well as the value.
3428 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
3429 unsigned Value = CE->getValue();
3432 }
3433
3434 void addNEONi32splatNotOperands(MCInst &Inst, unsigned N) const {
3435 assert(N == 1 && "Invalid number of operands!");
3436 // The immediate encodes the type of constant as well as the value.
3437 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
3438 unsigned Value = CE->getValue();
3441 }
3442
3443 void addNEONi8ReplicateOperands(MCInst &Inst, bool Inv) const {
3444 // The immediate encodes the type of constant as well as the value.
3445 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
3446 assert((Inst.getOpcode() == ARM::VMOVv8i8 ||
3447 Inst.getOpcode() == ARM::VMOVv16i8) &&
3448 "All instructions that wants to replicate non-zero byte "
3449 "always must be replaced with VMOVv8i8 or VMOVv16i8.");
3450 unsigned Value = CE->getValue();
3451 if (Inv)
3452 Value = ~Value;
3453 unsigned B = Value & 0xff;
3454 B |= 0xe00; // cmode = 0b1110
3456 }
3457
3458 void addNEONinvi8ReplicateOperands(MCInst &Inst, unsigned N) const {
3459 assert(N == 1 && "Invalid number of operands!");
3460 addNEONi8ReplicateOperands(Inst, true);
3461 }
3462
3463 static unsigned encodeNeonVMOVImmediate(unsigned Value) {
3464 if (Value >= 256 && Value <= 0xffff)
3465 Value = (Value >> 8) | ((Value & 0xff) ? 0xc00 : 0x200);
3466 else if (Value > 0xffff && Value <= 0xffffff)
3467 Value = (Value >> 16) | ((Value & 0xff) ? 0xd00 : 0x400);
3468 else if (Value > 0xffffff)
3469 Value = (Value >> 24) | 0x600;
3470 return Value;
3471 }
3472
3473 void addNEONi32vmovOperands(MCInst &Inst, unsigned N) const {
3474 assert(N == 1 && "Invalid number of operands!");
3475 // The immediate encodes the type of constant as well as the value.
3476 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
3477 unsigned Value = encodeNeonVMOVImmediate(CE->getValue());
3479 }
3480
3481 void addNEONvmovi8ReplicateOperands(MCInst &Inst, unsigned N) const {
3482 assert(N == 1 && "Invalid number of operands!");
3483 addNEONi8ReplicateOperands(Inst, false);
3484 }
3485
3486 void addNEONvmovi16ReplicateOperands(MCInst &Inst, unsigned N) const {
3487 assert(N == 1 && "Invalid number of operands!");
3488 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
3489 assert((Inst.getOpcode() == ARM::VMOVv4i16 ||
3490 Inst.getOpcode() == ARM::VMOVv8i16 ||
3491 Inst.getOpcode() == ARM::VMVNv4i16 ||
3492 Inst.getOpcode() == ARM::VMVNv8i16) &&
3493 "All instructions that want to replicate non-zero half-word "
3494 "always must be replaced with V{MOV,MVN}v{4,8}i16.");
3495 uint64_t Value = CE->getValue();
3496 unsigned Elem = Value & 0xffff;
3497 if (Elem >= 256)
3498 Elem = (Elem >> 8) | 0x200;
3499 Inst.addOperand(MCOperand::createImm(Elem));
3500 }
3501
3502 void addNEONi32vmovNegOperands(MCInst &Inst, unsigned N) const {
3503 assert(N == 1 && "Invalid number of operands!");
3504 // The immediate encodes the type of constant as well as the value.
3505 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
3506 unsigned Value = encodeNeonVMOVImmediate(~CE->getValue());
3508 }
3509
3510 void addNEONvmovi32ReplicateOperands(MCInst &Inst, unsigned N) const {
3511 assert(N == 1 && "Invalid number of operands!");
3512 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
3513 assert((Inst.getOpcode() == ARM::VMOVv2i32 ||
3514 Inst.getOpcode() == ARM::VMOVv4i32 ||
3515 Inst.getOpcode() == ARM::VMVNv2i32 ||
3516 Inst.getOpcode() == ARM::VMVNv4i32) &&
3517 "All instructions that want to replicate non-zero word "
3518 "always must be replaced with V{MOV,MVN}v{2,4}i32.");
3519 uint64_t Value = CE->getValue();
3520 unsigned Elem = encodeNeonVMOVImmediate(Value & 0xffffffff);
3521 Inst.addOperand(MCOperand::createImm(Elem));
3522 }
3523
3524 void addNEONi64splatOperands(MCInst &Inst, unsigned N) const {
3525 assert(N == 1 && "Invalid number of operands!");
3526 // The immediate encodes the type of constant as well as the value.
3527 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
3528 uint64_t Value = CE->getValue();
3529 unsigned Imm = 0;
3530 for (unsigned i = 0; i < 8; ++i, Value >>= 8) {
3531 Imm |= (Value & 1) << i;
3532 }
3533 Inst.addOperand(MCOperand::createImm(Imm | 0x1e00));
3534 }
3535
3536 void addComplexRotationEvenOperands(MCInst &Inst, unsigned N) const {
3537 assert(N == 1 && "Invalid number of operands!");
3538 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
3539 Inst.addOperand(MCOperand::createImm(CE->getValue() / 90));
3540 }
3541
3542 void addComplexRotationOddOperands(MCInst &Inst, unsigned N) const {
3543 assert(N == 1 && "Invalid number of operands!");
3544 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
3545 Inst.addOperand(MCOperand::createImm((CE->getValue() - 90) / 180));
3546 }
3547
3548 void addMveSaturateOperands(MCInst &Inst, unsigned N) const {
3549 assert(N == 1 && "Invalid number of operands!");
3550 const MCConstantExpr *CE = cast<MCConstantExpr>(getImm());
3551 unsigned Imm = CE->getValue();
3552 assert((Imm == 48 || Imm == 64) && "Invalid saturate operand");
3553 Inst.addOperand(MCOperand::createImm(Imm == 48 ? 1 : 0));
3554 }
3555
3556 void print(raw_ostream &OS) const override;
3557
3558 static std::unique_ptr<ARMOperand> CreateITMask(unsigned Mask, SMLoc S) {
3559 auto Op = std::make_unique<ARMOperand>(k_ITCondMask);
3560 Op->ITMask.Mask = Mask;
3561 Op->StartLoc = S;
3562 Op->EndLoc = S;
3563 return Op;
3564 }
3565
3566 static std::unique_ptr<ARMOperand> CreateCondCode(ARMCC::CondCodes CC,
3567 SMLoc S) {
3568 auto Op = std::make_unique<ARMOperand>(k_CondCode);
3569 Op->CC.Val = CC;
3570 Op->StartLoc = S;
3571 Op->EndLoc = S;
3572 return Op;
3573 }
3574
3575 static std::unique_ptr<ARMOperand> CreateVPTPred(ARMVCC::VPTCodes CC,
3576 SMLoc S) {
3577 auto Op = std::make_unique<ARMOperand>(k_VPTPred);
3578 Op->VCC.Val = CC;
3579 Op->StartLoc = S;
3580 Op->EndLoc = S;
3581 return Op;
3582 }
3583
3584 static std::unique_ptr<ARMOperand> CreateCoprocNum(unsigned CopVal, SMLoc S) {
3585 auto Op = std::make_unique<ARMOperand>(k_CoprocNum);
3586 Op->Cop.Val = CopVal;
3587 Op->StartLoc = S;
3588 Op->EndLoc = S;
3589 return Op;
3590 }
3591
3592 static std::unique_ptr<ARMOperand> CreateCoprocReg(unsigned CopVal, SMLoc S) {
3593 auto Op = std::make_unique<ARMOperand>(k_CoprocReg);
3594 Op->Cop.Val = CopVal;
3595 Op->StartLoc = S;
3596 Op->EndLoc = S;
3597 return Op;
3598 }
3599
3600 static std::unique_ptr<ARMOperand> CreateCoprocOption(unsigned Val, SMLoc S,
3601 SMLoc E) {
3602 auto Op = std::make_unique<ARMOperand>(k_CoprocOption);
3603 Op->Cop.Val = Val;
3604 Op->StartLoc = S;
3605 Op->EndLoc = E;
3606 return Op;
3607 }
3608
3609 static std::unique_ptr<ARMOperand> CreateCCOut(unsigned RegNum, SMLoc S) {
3610 auto Op = std::make_unique<ARMOperand>(k_CCOut);
3611 Op->Reg.RegNum = RegNum;
3612 Op->StartLoc = S;
3613 Op->EndLoc = S;
3614 return Op;
3615 }
3616
3617 static std::unique_ptr<ARMOperand> CreateToken(StringRef Str, SMLoc S) {
3618 auto Op = std::make_unique<ARMOperand>(k_Token);
3619 Op->Tok.Data = Str.data();
3620 Op->Tok.Length = Str.size();
3621 Op->StartLoc = S;
3622 Op->EndLoc = S;
3623 return Op;
3624 }
3625
3626 static std::unique_ptr<ARMOperand> CreateReg(unsigned RegNum, SMLoc S,
3627 SMLoc E) {
3628 auto Op = std::make_unique<ARMOperand>(k_Register);
3629 Op->Reg.RegNum = RegNum;
3630 Op->StartLoc = S;
3631 Op->EndLoc = E;
3632 return Op;
3633 }
3634
3635 static std::unique_ptr<ARMOperand>
3636 CreateShiftedRegister(ARM_AM::ShiftOpc ShTy, unsigned SrcReg,
3637 unsigned ShiftReg, unsigned ShiftImm, SMLoc S,
3638 SMLoc E) {
3639 auto Op = std::make_unique<ARMOperand>(k_ShiftedRegister);
3640 Op->RegShiftedReg.ShiftTy = ShTy;
3641 Op->RegShiftedReg.SrcReg = SrcReg;
3642 Op->RegShiftedReg.ShiftReg = ShiftReg;
3643 Op->RegShiftedReg.ShiftImm = ShiftImm;
3644 Op->StartLoc = S;
3645 Op->EndLoc = E;
3646 return Op;
3647 }
3648
3649 static std::unique_ptr<ARMOperand>
3650 CreateShiftedImmediate(ARM_AM::ShiftOpc ShTy, unsigned SrcReg,
3651 unsigned ShiftImm, SMLoc S, SMLoc E) {
3652 auto Op = std::make_unique<ARMOperand>(k_ShiftedImmediate);
3653 Op->RegShiftedImm.ShiftTy = ShTy;
3654 Op->RegShiftedImm.SrcReg = SrcReg;
3655 Op->RegShiftedImm.ShiftImm = ShiftImm;
3656 Op->StartLoc = S;
3657 Op->EndLoc = E;
3658 return Op;
3659 }
3660
3661 static std::unique_ptr<ARMOperand> CreateShifterImm(bool isASR, unsigned Imm,
3662 SMLoc S, SMLoc E) {
3663 auto Op = std::make_unique<ARMOperand>(k_ShifterImmediate);
3664 Op->ShifterImm.isASR = isASR;
3665 Op->ShifterImm.Imm = Imm;
3666 Op->StartLoc = S;
3667 Op->EndLoc = E;
3668 return Op;
3669 }
3670
3671 static std::unique_ptr<ARMOperand> CreateRotImm(unsigned Imm, SMLoc S,
3672 SMLoc E) {
3673 auto Op = std::make_unique<ARMOperand>(k_RotateImmediate);
3674 Op->RotImm.Imm = Imm;
3675 Op->StartLoc = S;
3676 Op->EndLoc = E;
3677 return Op;
3678 }
3679
3680 static std::unique_ptr<ARMOperand> CreateModImm(unsigned Bits, unsigned Rot,
3681 SMLoc S, SMLoc E) {
3682 auto Op = std::make_unique<ARMOperand>(k_ModifiedImmediate);
3683 Op->ModImm.Bits = Bits;
3684 Op->ModImm.Rot = Rot;
3685 Op->StartLoc = S;
3686 Op->EndLoc = E;
3687 return Op;
3688 }
3689
3690 static std::unique_ptr<ARMOperand>
3691 CreateConstantPoolImm(const MCExpr *Val, SMLoc S, SMLoc E) {
3692 auto Op = std::make_unique<ARMOperand>(k_ConstantPoolImmediate);
3693 Op->Imm.Val = Val;
3694 Op->StartLoc = S;
3695 Op->EndLoc = E;
3696 return Op;
3697 }
3698
3699 static std::unique_ptr<ARMOperand>
3700 CreateBitfield(unsigned LSB, unsigned Width, SMLoc S, SMLoc E) {
3701 auto Op = std::make_unique<ARMOperand>(k_BitfieldDescriptor);
3702 Op->Bitfield.LSB = LSB;
3703 Op->Bitfield.Width = Width;
3704 Op->StartLoc = S;
3705 Op->EndLoc = E;
3706 return Op;
3707 }
3708
3709 static std::unique_ptr<ARMOperand>
3710 CreateRegList(SmallVectorImpl<std::pair<unsigned, unsigned>> &Regs,
3711 SMLoc StartLoc, SMLoc EndLoc) {
3712 assert(Regs.size() > 0 && "RegList contains no registers?");
3713 KindTy Kind = k_RegisterList;
3714
3715 if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(
3716 Regs.front().second)) {
3717 if (Regs.back().second == ARM::VPR)
3718 Kind = k_FPDRegisterListWithVPR;
3719 else
3720 Kind = k_DPRRegisterList;
3721 } else if (ARMMCRegisterClasses[ARM::SPRRegClassID].contains(
3722 Regs.front().second)) {
3723 if (Regs.back().second == ARM::VPR)
3724 Kind = k_FPSRegisterListWithVPR;
3725 else
3726 Kind = k_SPRRegisterList;
3727 }
3728
3729 if (Kind == k_RegisterList && Regs.back().second == ARM::APSR)
3730 Kind = k_RegisterListWithAPSR;
3731
3732 assert(llvm::is_sorted(Regs) && "Register list must be sorted by encoding");
3733
3734 auto Op = std::make_unique<ARMOperand>(Kind);
3735 for (const auto &P : Regs)
3736 Op->Registers.push_back(P.second);
3737
3738 Op->StartLoc = StartLoc;
3739 Op->EndLoc = EndLoc;
3740 return Op;
3741 }
3742
3743 static std::unique_ptr<ARMOperand> CreateVectorList(unsigned RegNum,
3744 unsigned Count,
3745 bool isDoubleSpaced,
3746 SMLoc S, SMLoc E) {
3747 auto Op = std::make_unique<ARMOperand>(k_VectorList);
3748 Op->VectorList.RegNum = RegNum;
3749 Op->VectorList.Count = Count;
3750 Op->VectorList.isDoubleSpaced = isDoubleSpaced;
3751 Op->StartLoc = S;
3752 Op->EndLoc = E;
3753 return Op;
3754 }
3755
3756 static std::unique_ptr<ARMOperand>
3757 CreateVectorListAllLanes(unsigned RegNum, unsigned Count, bool isDoubleSpaced,
3758 SMLoc S, SMLoc E) {
3759 auto Op = std::make_unique<ARMOperand>(k_VectorListAllLanes);
3760 Op->VectorList.RegNum = RegNum;
3761 Op->VectorList.Count = Count;
3762 Op->VectorList.isDoubleSpaced = isDoubleSpaced;
3763 Op->StartLoc = S;
3764 Op->EndLoc = E;
3765 return Op;
3766 }
3767
3768 static std::unique_ptr<ARMOperand>
3769 CreateVectorListIndexed(unsigned RegNum, unsigned Count, unsigned Index,
3770 bool isDoubleSpaced, SMLoc S, SMLoc E) {
3771 auto Op = std::make_unique<ARMOperand>(k_VectorListIndexed);
3772 Op->VectorList.RegNum = RegNum;
3773 Op->VectorList.Count = Count;
3774 Op->VectorList.LaneIndex = Index;
3775 Op->VectorList.isDoubleSpaced = isDoubleSpaced;
3776 Op->StartLoc = S;
3777 Op->EndLoc = E;
3778 return Op;
3779 }
3780
3781 static std::unique_ptr<ARMOperand>
3782 CreateVectorIndex(unsigned Idx, SMLoc S, SMLoc E, MCContext &Ctx) {
3783 auto Op = std::make_unique<ARMOperand>(k_VectorIndex);
3784 Op->VectorIndex.Val = Idx;
3785 Op->StartLoc = S;
3786 Op->EndLoc = E;
3787 return Op;
3788 }
3789
3790 static std::unique_ptr<ARMOperand> CreateImm(const MCExpr *Val, SMLoc S,
3791 SMLoc E) {
3792 auto Op = std::make_unique<ARMOperand>(k_Immediate);
3793 Op->Imm.Val = Val;
3794 Op->StartLoc = S;
3795 Op->EndLoc = E;
3796 return Op;
3797 }
3798
3799 static std::unique_ptr<ARMOperand>
3800 CreateMem(unsigned BaseRegNum, const MCExpr *OffsetImm, unsigned OffsetRegNum,
3801 ARM_AM::ShiftOpc ShiftType, unsigned ShiftImm, unsigned Alignment,
3802 bool isNegative, SMLoc S, SMLoc E, SMLoc AlignmentLoc = SMLoc()) {
3803 auto Op = std::make_unique<ARMOperand>(k_Memory);
3804 Op->Memory.BaseRegNum = BaseRegNum;
3805 Op->Memory.OffsetImm = OffsetImm;
3806 Op->Memory.OffsetRegNum = OffsetRegNum;
3807 Op->Memory.ShiftType = ShiftType;
3808 Op->Memory.ShiftImm = ShiftImm;
3809 Op->Memory.Alignment = Alignment;
3810 Op->Memory.isNegative = isNegative;
3811 Op->StartLoc = S;
3812 Op->EndLoc = E;
3813 Op->AlignmentLoc = AlignmentLoc;
3814 return Op;
3815 }
3816
3817 static std::unique_ptr<ARMOperand>
3818 CreatePostIdxReg(unsigned RegNum, bool isAdd, ARM_AM::ShiftOpc ShiftTy,
3819 unsigned ShiftImm, SMLoc S, SMLoc E) {
3820 auto Op = std::make_unique<ARMOperand>(k_PostIndexRegister);
3821 Op->PostIdxReg.RegNum = RegNum;
3822 Op->PostIdxReg.isAdd = isAdd;
3823 Op->PostIdxReg.ShiftTy = ShiftTy;
3824 Op->PostIdxReg.ShiftImm = ShiftImm;
3825 Op->StartLoc = S;
3826 Op->EndLoc = E;
3827 return Op;
3828 }
3829
3830 static std::unique_ptr<ARMOperand> CreateMemBarrierOpt(ARM_MB::MemBOpt Opt,
3831 SMLoc S) {
3832 auto Op = std::make_unique<ARMOperand>(k_MemBarrierOpt);
3833 Op->MBOpt.Val = Opt;
3834 Op->StartLoc = S;
3835 Op->EndLoc = S;
3836 return Op;
3837 }
3838
3839 static std::unique_ptr<ARMOperand>
3840 CreateInstSyncBarrierOpt(ARM_ISB::InstSyncBOpt Opt, SMLoc S) {
3841 auto Op = std::make_unique<ARMOperand>(k_InstSyncBarrierOpt);
3842 Op->ISBOpt.Val = Opt;
3843 Op->StartLoc = S;
3844 Op->EndLoc = S;
3845 return Op;
3846 }
3847
3848 static std::unique_ptr<ARMOperand>
3849 CreateTraceSyncBarrierOpt(ARM_TSB::TraceSyncBOpt Opt, SMLoc S) {
3850 auto Op = std::make_unique<ARMOperand>(k_TraceSyncBarrierOpt);
3851 Op->TSBOpt.Val = Opt;
3852 Op->StartLoc = S;
3853 Op->EndLoc = S;
3854 return Op;
3855 }
3856
3857 static std::unique_ptr<ARMOperand> CreateProcIFlags(ARM_PROC::IFlags IFlags,
3858 SMLoc S) {
3859 auto Op = std::make_unique<ARMOperand>(k_ProcIFlags);
3860 Op->IFlags.Val = IFlags;
3861 Op->StartLoc = S;
3862 Op->EndLoc = S;
3863 return Op;
3864 }
3865
3866 static std::unique_ptr<ARMOperand> CreateMSRMask(unsigned MMask, SMLoc S) {
3867 auto Op = std::make_unique<ARMOperand>(k_MSRMask);
3868 Op->MMask.Val = MMask;
3869 Op->StartLoc = S;
3870 Op->EndLoc = S;
3871 return Op;
3872 }
3873
3874 static std::unique_ptr<ARMOperand> CreateBankedReg(unsigned Reg, SMLoc S) {
3875 auto Op = std::make_unique<ARMOperand>(k_BankedReg);
3876 Op->BankedReg.Val = Reg;
3877 Op->StartLoc = S;
3878 Op->EndLoc = S;
3879 return Op;
3880 }
3881};
3882
3883} // end anonymous namespace.
3884
3885void ARMOperand::print(raw_ostream &OS) const {
3886 auto RegName = [](MCRegister Reg) {
3887 if (Reg)
3889 else
3890 return "noreg";
3891 };
3892
3893 switch (Kind) {
3894 case k_CondCode:
3895 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
3896 break;
3897 case k_VPTPred:
3898 OS << "<ARMVCC::" << ARMVPTPredToString(getVPTPred()) << ">";
3899 break;
3900 case k_CCOut:
3901 OS << "<ccout " << RegName(getReg()) << ">";
3902 break;
3903 case k_ITCondMask: {
3904 static const char *const MaskStr[] = {
3905 "(invalid)", "(tttt)", "(ttt)", "(ttte)",
3906 "(tt)", "(ttet)", "(tte)", "(ttee)",
3907 "(t)", "(tett)", "(tet)", "(tete)",
3908 "(te)", "(teet)", "(tee)", "(teee)",
3909 };
3910 assert((ITMask.Mask & 0xf) == ITMask.Mask);
3911 OS << "<it-mask " << MaskStr[ITMask.Mask] << ">";
3912 break;
3913 }
3914 case k_CoprocNum:
3915 OS << "<coprocessor number: " << getCoproc() << ">";
3916 break;
3917 case k_CoprocReg:
3918 OS << "<coprocessor register: " << getCoproc() << ">";
3919 break;
3920 case k_CoprocOption:
3921 OS << "<coprocessor option: " << CoprocOption.Val << ">";
3922 break;
3923 case k_MSRMask:
3924 OS << "<mask: " << getMSRMask() << ">";
3925 break;
3926 case k_BankedReg:
3927 OS << "<banked reg: " << getBankedReg() << ">";
3928 break;
3929 case k_Immediate:
3930 OS << *getImm();
3931 break;
3932 case k_MemBarrierOpt:
3933 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt(), false) << ">";
3934 break;
3935 case k_InstSyncBarrierOpt:
3936 OS << "<ARM_ISB::" << InstSyncBOptToString(getInstSyncBarrierOpt()) << ">";
3937 break;
3938 case k_TraceSyncBarrierOpt:
3939 OS << "<ARM_TSB::" << TraceSyncBOptToString(getTraceSyncBarrierOpt()) << ">";
3940 break;
3941 case k_Memory:
3942 OS << "<memory";
3943 if (Memory.BaseRegNum)
3944 OS << " base:" << RegName(Memory.BaseRegNum);
3945 if (Memory.OffsetImm)
3946 OS << " offset-imm:" << *Memory.OffsetImm;
3947 if (Memory.OffsetRegNum)
3948 OS << " offset-reg:" << (Memory.isNegative ? "-" : "")
3949 << RegName(Memory.OffsetRegNum);
3950 if (Memory.ShiftType != ARM_AM::no_shift) {
3951 OS << " shift-type:" << ARM_AM::getShiftOpcStr(Memory.ShiftType);
3952 OS << " shift-imm:" << Memory.ShiftImm;
3953 }
3954 if (Memory.Alignment)
3955 OS << " alignment:" << Memory.Alignment;
3956 OS << ">";
3957 break;
3958 case k_PostIndexRegister:
3959 OS << "post-idx register " << (PostIdxReg.isAdd ? "" : "-")
3960 << RegName(PostIdxReg.RegNum);
3961 if (PostIdxReg.ShiftTy != ARM_AM::no_shift)
3962 OS << ARM_AM::getShiftOpcStr(PostIdxReg.ShiftTy) << " "
3963 << PostIdxReg.ShiftImm;
3964 OS << ">";
3965 break;
3966 case k_ProcIFlags: {
3967 OS << "<ARM_PROC::";
3968 unsigned IFlags = getProcIFlags();
3969 for (int i=2; i >= 0; --i)
3970 if (IFlags & (1 << i))
3971 OS << ARM_PROC::IFlagsToString(1 << i);
3972 OS << ">";
3973 break;
3974 }
3975 case k_Register:
3976 OS << "<register " << RegName(getReg()) << ">";
3977 break;
3978 case k_ShifterImmediate:
3979 OS << "<shift " << (ShifterImm.isASR ? "asr" : "lsl")
3980 << " #" << ShifterImm.Imm << ">";
3981 break;
3982 case k_ShiftedRegister:
3983 OS << "<so_reg_reg " << RegName(RegShiftedReg.SrcReg) << " "
3984 << ARM_AM::getShiftOpcStr(RegShiftedReg.ShiftTy) << " "
3985 << RegName(RegShiftedReg.ShiftReg) << ">";
3986 break;
3987 case k_ShiftedImmediate:
3988 OS << "<so_reg_imm " << RegName(RegShiftedImm.SrcReg) << " "
3989 << ARM_AM::getShiftOpcStr(RegShiftedImm.ShiftTy) << " #"
3990 << RegShiftedImm.ShiftImm << ">";
3991 break;
3992 case k_RotateImmediate:
3993 OS << "<ror " << " #" << (RotImm.Imm * 8) << ">";
3994 break;
3995 case k_ModifiedImmediate:
3996 OS << "<mod_imm #" << ModImm.Bits << ", #"
3997 << ModImm.Rot << ")>";
3998 break;
3999 case k_ConstantPoolImmediate:
4000 OS << "<constant_pool_imm #" << *getConstantPoolImm();
4001 break;
4002 case k_BitfieldDescriptor:
4003 OS << "<bitfield " << "lsb: " << Bitfield.LSB
4004 << ", width: " << Bitfield.Width << ">";
4005 break;
4006 case k_RegisterList:
4007 case k_RegisterListWithAPSR:
4008 case k_DPRRegisterList:
4009 case k_SPRRegisterList:
4010 case k_FPSRegisterListWithVPR:
4011 case k_FPDRegisterListWithVPR: {
4012 OS << "<register_list ";
4013
4014 const SmallVectorImpl<unsigned> &RegList = getRegList();
4016 I = RegList.begin(), E = RegList.end(); I != E; ) {
4017 OS << RegName(*I);
4018 if (++I < E) OS << ", ";
4019 }
4020
4021 OS << ">";
4022 break;
4023 }
4024 case k_VectorList:
4025 OS << "<vector_list " << VectorList.Count << " * "
4026 << RegName(VectorList.RegNum) << ">";
4027 break;
4028 case k_VectorListAllLanes:
4029 OS << "<vector_list(all lanes) " << VectorList.Count << " * "
4030 << RegName(VectorList.RegNum) << ">";
4031 break;
4032 case k_VectorListIndexed:
4033 OS << "<vector_list(lane " << VectorList.LaneIndex << ") "
4034 << VectorList.Count << " * " << RegName(VectorList.RegNum) << ">";
4035 break;
4036 case k_Token:
4037 OS << "'" << getToken() << "'";
4038 break;
4039 case k_VectorIndex:
4040 OS << "<vectorindex " << getVectorIndex() << ">";
4041 break;
4042 }
4043}
4044
4045/// @name Auto-generated Match Functions
4046/// {
4047
4049
4050/// }
4051
4052bool ARMAsmParser::parseRegister(MCRegister &Reg, SMLoc &StartLoc,
4053 SMLoc &EndLoc) {
4054 const AsmToken &Tok = getParser().getTok();
4055 StartLoc = Tok.getLoc();
4056 EndLoc = Tok.getEndLoc();
4057 Reg = tryParseRegister();
4058
4059 return Reg == (unsigned)-1;
4060}
4061
4062ParseStatus ARMAsmParser::tryParseRegister(MCRegister &Reg, SMLoc &StartLoc,
4063 SMLoc &EndLoc) {
4064 if (parseRegister(Reg, StartLoc, EndLoc))
4065 return ParseStatus::NoMatch;
4066 return ParseStatus::Success;
4067}
4068
4069/// Try to parse a register name. The token must be an Identifier when called,
4070/// and if it is a register name the token is eaten and the register number is
4071/// returned. Otherwise return -1.
4072int ARMAsmParser::tryParseRegister() {
4073 MCAsmParser &Parser = getParser();
4074 const AsmToken &Tok = Parser.getTok();
4075 if (Tok.isNot(AsmToken::Identifier)) return -1;
4076
4077 std::string lowerCase = Tok.getString().lower();
4078 unsigned RegNum = MatchRegisterName(lowerCase);
4079 if (!RegNum) {
4080 RegNum = StringSwitch<unsigned>(lowerCase)
4081 .Case("r13", ARM::SP)
4082 .Case("r14", ARM::LR)
4083 .Case("r15", ARM::PC)
4084 .Case("ip", ARM::R12)
4085 // Additional register name aliases for 'gas' compatibility.
4086 .Case("a1", ARM::R0)
4087 .Case("a2", ARM::R1)
4088 .Case("a3", ARM::R2)
4089 .Case("a4", ARM::R3)
4090 .Case("v1", ARM::R4)
4091 .Case("v2", ARM::R5)
4092 .Case("v3", ARM::R6)
4093 .Case("v4", ARM::R7)
4094 .Case("v5", ARM::R8)
4095 .Case("v6", ARM::R9)
4096 .Case("v7", ARM::R10)
4097 .Case("v8", ARM::R11)
4098 .Case("sb", ARM::R9)
4099 .Case("sl", ARM::R10)
4100 .Case("fp", ARM::R11)
4101 .Default(0);
4102 }
4103 if (!RegNum) {
4104 // Check for aliases registered via .req. Canonicalize to lower case.
4105 // That's more consistent since register names are case insensitive, and
4106 // it's how the original entry was passed in from MC/MCParser/AsmParser.
4107 StringMap<unsigned>::const_iterator Entry = RegisterReqs.find(lowerCase);
4108 // If no match, return failure.
4109 if (Entry == RegisterReqs.end())
4110 return -1;
4111 Parser.Lex(); // Eat identifier token.
4112 return Entry->getValue();
4113 }
4114
4115 // Some FPUs only have 16 D registers, so D16-D31 are invalid
4116 if (!hasD32() && RegNum >= ARM::D16 && RegNum <= ARM::D31)
4117 return -1;
4118
4119 Parser.Lex(); // Eat identifier token.
4120
4121 return RegNum;
4122}
4123
4124// Try to parse a shifter (e.g., "lsl <amt>"). On success, return 0.
4125// If a recoverable error occurs, return 1. If an irrecoverable error
4126// occurs, return -1. An irrecoverable error is one where tokens have been
4127// consumed in the process of trying to parse the shifter (i.e., when it is
4128// indeed a shifter operand, but malformed).
4129int ARMAsmParser::tryParseShiftRegister(OperandVector &Operands) {
4130 MCAsmParser &Parser = getParser();
4131 SMLoc S = Parser.getTok().getLoc();
4132 const AsmToken &Tok = Parser.getTok();
4133 if (Tok.isNot(AsmToken::Identifier))
4134 return -1;
4135
4136 std::string lowerCase = Tok.getString().lower();
4138 .Case("asl", ARM_AM::lsl)
4139 .Case("lsl", ARM_AM::lsl)
4140 .Case("lsr", ARM_AM::lsr)
4141 .Case("asr", ARM_AM::asr)
4142 .Case("ror", ARM_AM::ror)
4143 .Case("rrx", ARM_AM::rrx)
4145
4146 if (ShiftTy == ARM_AM::no_shift)
4147 return 1;
4148
4149 Parser.Lex(); // Eat the operator.
4150
4151 // The source register for the shift has already been added to the
4152 // operand list, so we need to pop it off and combine it into the shifted
4153 // register operand instead.
4154 std::unique_ptr<ARMOperand> PrevOp(
4155 (ARMOperand *)Operands.pop_back_val().release());
4156 if (!PrevOp->isReg())
4157 return Error(PrevOp->getStartLoc(), "shift must be of a register");
4158 int SrcReg = PrevOp->getReg();
4159
4160 SMLoc EndLoc;
4161 int64_t Imm = 0;
4162 int ShiftReg = 0;
4163 if (ShiftTy == ARM_AM::rrx) {
4164 // RRX Doesn't have an explicit shift amount. The encoder expects
4165 // the shift register to be the same as the source register. Seems odd,
4166 // but OK.
4167 ShiftReg = SrcReg;
4168 } else {
4169 // Figure out if this is shifted by a constant or a register (for non-RRX).
4170 if (Parser.getTok().is(AsmToken::Hash) ||
4171 Parser.getTok().is(AsmToken::Dollar)) {
4172 Parser.Lex(); // Eat hash.
4173 SMLoc ImmLoc = Parser.getTok().getLoc();
4174 const MCExpr *ShiftExpr = nullptr;
4175 if (getParser().parseExpression(ShiftExpr, EndLoc)) {
4176 Error(ImmLoc, "invalid immediate shift value");
4177 return -1;
4178 }
4179 // The expression must be evaluatable as an immediate.
4180 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
4181 if (!CE) {
4182 Error(ImmLoc, "invalid immediate shift value");
4183 return -1;
4184 }
4185 // Range check the immediate.
4186 // lsl, ror: 0 <= imm <= 31
4187 // lsr, asr: 0 <= imm <= 32
4188 Imm = CE->getValue();
4189 if (Imm < 0 ||
4190 ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
4191 ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
4192 Error(ImmLoc, "immediate shift value out of range");
4193 return -1;
4194 }
4195 // shift by zero is a nop. Always send it through as lsl.
4196 // ('as' compatibility)
4197 if (Imm == 0)
4198 ShiftTy = ARM_AM::lsl;
4199 } else if (Parser.getTok().is(AsmToken::Identifier)) {
4200 SMLoc L = Parser.getTok().getLoc();
4201 EndLoc = Parser.getTok().getEndLoc();
4202 ShiftReg = tryParseRegister();
4203 if (ShiftReg == -1) {
4204 Error(L, "expected immediate or register in shift operand");
4205 return -1;
4206 }
4207 } else {
4208 Error(Parser.getTok().getLoc(),
4209 "expected immediate or register in shift operand");
4210 return -1;
4211 }
4212 }
4213
4214 if (ShiftReg && ShiftTy != ARM_AM::rrx)
4215 Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
4216 ShiftReg, Imm,
4217 S, EndLoc));
4218 else
4219 Operands.push_back(ARMOperand::CreateShiftedImmediate(ShiftTy, SrcReg, Imm,
4220 S, EndLoc));
4221
4222 return 0;
4223}
4224
4225/// Try to parse a register name. The token must be an Identifier when called.
4226/// If it's a register, an AsmOperand is created. Another AsmOperand is created
4227/// if there is a "writeback". 'true' if it's not a register.
4228///
4229/// TODO this is likely to change to allow different register types and or to
4230/// parse for a specific register type.
4231bool ARMAsmParser::tryParseRegisterWithWriteBack(OperandVector &Operands) {
4232 MCAsmParser &Parser = getParser();
4233 SMLoc RegStartLoc = Parser.getTok().getLoc();
4234 SMLoc RegEndLoc = Parser.getTok().getEndLoc();
4235 int RegNo = tryParseRegister();
4236 if (RegNo == -1)
4237 return true;
4238
4239 Operands.push_back(ARMOperand::CreateReg(RegNo, RegStartLoc, RegEndLoc));
4240
4241 const AsmToken &ExclaimTok = Parser.getTok();
4242 if (ExclaimTok.is(AsmToken::Exclaim)) {
4243 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
4244 ExclaimTok.getLoc()));
4245 Parser.Lex(); // Eat exclaim token
4246 return false;
4247 }
4248
4249 // Also check for an index operand. This is only legal for vector registers,
4250 // but that'll get caught OK in operand matching, so we don't need to
4251 // explicitly filter everything else out here.
4252 if (Parser.getTok().is(AsmToken::LBrac)) {
4253 SMLoc SIdx = Parser.getTok().getLoc();
4254 Parser.Lex(); // Eat left bracket token.
4255
4256 const MCExpr *ImmVal;
4257 if (getParser().parseExpression(ImmVal))
4258 return true;
4259 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(ImmVal);
4260 if (!MCE)
4261 return TokError("immediate value expected for vector index");
4262
4263 if (Parser.getTok().isNot(AsmToken::RBrac))
4264 return Error(Parser.getTok().getLoc(), "']' expected");
4265
4266 SMLoc E = Parser.getTok().getEndLoc();
4267 Parser.Lex(); // Eat right bracket token.
4268
4269 Operands.push_back(ARMOperand::CreateVectorIndex(MCE->getValue(),
4270 SIdx, E,
4271 getContext()));
4272 }
4273
4274 return false;
4275}
4276
4277/// MatchCoprocessorOperandName - Try to parse an coprocessor related
4278/// instruction with a symbolic operand name.
4279/// We accept "crN" syntax for GAS compatibility.
4280/// <operand-name> ::= <prefix><number>
4281/// If CoprocOp is 'c', then:
4282/// <prefix> ::= c | cr
4283/// If CoprocOp is 'p', then :
4284/// <prefix> ::= p
4285/// <number> ::= integer in range [0, 15]
4286static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
4287 // Use the same layout as the tablegen'erated register name matcher. Ugly,
4288 // but efficient.
4289 if (Name.size() < 2 || Name[0] != CoprocOp)
4290 return -1;
4291 Name = (Name[1] == 'r') ? Name.drop_front(2) : Name.drop_front();
4292
4293 switch (Name.size()) {
4294 default: return -1;
4295 case 1:
4296 switch (Name[0]) {
4297 default: return -1;
4298 case '0': return 0;
4299 case '1': return 1;
4300 case '2': return 2;
4301 case '3': return 3;
4302 case '4': return 4;
4303 case '5': return 5;
4304 case '6': return 6;
4305 case '7': return 7;
4306 case '8': return 8;
4307 case '9': return 9;
4308 }
4309 case 2:
4310 if (Name[0] != '1')
4311 return -1;
4312 switch (Name[1]) {
4313 default: return -1;
4314 // CP10 and CP11 are VFP/NEON and so vector instructions should be used.
4315 // However, old cores (v5/v6) did use them in that way.
4316 case '0': return 10;
4317 case '1': return 11;
4318 case '2': return 12;
4319 case '3': return 13;
4320 case '4': return 14;
4321 case '5': return 15;
4322 }
4323 }
4324}
4325
4326/// parseITCondCode - Try to parse a condition code for an IT instruction.
4327ParseStatus ARMAsmParser::parseITCondCode(OperandVector &Operands) {
4328 MCAsmParser &Parser = getParser();
4329 SMLoc S = Parser.getTok().getLoc();
4330 const AsmToken &Tok = Parser.getTok();
4331 if (!Tok.is(AsmToken::Identifier))
4332 return ParseStatus::NoMatch;
4333 unsigned CC = ARMCondCodeFromString(Tok.getString());
4334 if (CC == ~0U)
4335 return ParseStatus::NoMatch;
4336 Parser.Lex(); // Eat the token.
4337
4338 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), S));
4339
4340 return ParseStatus::Success;
4341}
4342
4343/// parseCoprocNumOperand - Try to parse an coprocessor number operand. The
4344/// token must be an Identifier when called, and if it is a coprocessor
4345/// number, the token is eaten and the operand is added to the operand list.
4346ParseStatus ARMAsmParser::parseCoprocNumOperand(OperandVector &Operands) {
4347 MCAsmParser &Parser = getParser();
4348 SMLoc S = Parser.getTok().getLoc();
4349 const AsmToken &Tok = Parser.getTok();
4350 if (Tok.isNot(AsmToken::Identifier))
4351 return ParseStatus::NoMatch;
4352
4353 int Num = MatchCoprocessorOperandName(Tok.getString().lower(), 'p');
4354 if (Num == -1)
4355 return ParseStatus::NoMatch;
4356 if (!isValidCoprocessorNumber(Num, getSTI().getFeatureBits()))
4357 return ParseStatus::NoMatch;
4358
4359 Parser.Lex(); // Eat identifier token.
4360 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
4361 return ParseStatus::Success;
4362}
4363
4364/// parseCoprocRegOperand - Try to parse an coprocessor register operand. The
4365/// token must be an Identifier when called, and if it is a coprocessor
4366/// number, the token is eaten and the operand is added to the operand list.
4367ParseStatus ARMAsmParser::parseCoprocRegOperand(OperandVector &Operands) {
4368 MCAsmParser &Parser = getParser();
4369 SMLoc S = Parser.getTok().getLoc();
4370 const AsmToken &Tok = Parser.getTok();
4371 if (Tok.isNot(AsmToken::Identifier))
4372 return ParseStatus::NoMatch;
4373
4374 int Reg = MatchCoprocessorOperandName(Tok.getString().lower(), 'c');
4375 if (Reg == -1)
4376 return ParseStatus::NoMatch;
4377
4378 Parser.Lex(); // Eat identifier token.
4379 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
4380 return ParseStatus::Success;
4381}
4382
4383/// parseCoprocOptionOperand - Try to parse an coprocessor option operand.
4384/// coproc_option : '{' imm0_255 '}'
4385ParseStatus ARMAsmParser::parseCoprocOptionOperand(OperandVector &Operands) {
4386 MCAsmParser &Parser = getParser();
4387 SMLoc S = Parser.getTok().getLoc();
4388
4389 // If this isn't a '{', this isn't a coprocessor immediate operand.
4390 if (Parser.getTok().isNot(AsmToken::LCurly))
4391 return ParseStatus::NoMatch;
4392 Parser.Lex(); // Eat the '{'
4393
4394 const MCExpr *Expr;
4395 SMLoc Loc = Parser.getTok().getLoc();
4396 if (getParser().parseExpression(Expr))
4397 return Error(Loc, "illegal expression");
4398 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
4399 if (!CE || CE->getValue() < 0 || CE->getValue() > 255)
4400 return Error(Loc,
4401 "coprocessor option must be an immediate in range [0, 255]");
4402 int Val = CE->getValue();
4403
4404 // Check for and consume the closing '}'
4405 if (Parser.getTok().isNot(AsmToken::RCurly))
4406 return ParseStatus::Failure;
4407 SMLoc E = Parser.getTok().getEndLoc();
4408 Parser.Lex(); // Eat the '}'
4409
4410 Operands.push_back(ARMOperand::CreateCoprocOption(Val, S, E));
4411 return ParseStatus::Success;
4412}
4413
4414// For register list parsing, we need to map from raw GPR register numbering
4415// to the enumeration values. The enumeration values aren't sorted by
4416// register number due to our using "sp", "lr" and "pc" as canonical names.
4417static unsigned getNextRegister(unsigned Reg) {
4418 // If this is a GPR, we need to do it manually, otherwise we can rely
4419 // on the sort ordering of the enumeration since the other reg-classes
4420 // are sane.
4421 if (!ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
4422 return Reg + 1;
4423 switch(Reg) {
4424 default: llvm_unreachable("Invalid GPR number!");
4425 case ARM::R0: return ARM::R1; case ARM::R1: return ARM::R2;
4426 case ARM::R2: return ARM::R3; case ARM::R3: return ARM::R4;
4427 case ARM::R4: return ARM::R5; case ARM::R5: return ARM::R6;
4428 case ARM::R6: return ARM::R7; case ARM::R7: return ARM::R8;
4429 case ARM::R8: return ARM::R9; case ARM::R9: return ARM::R10;
4430 case ARM::R10: return ARM::R11; case ARM::R11: return ARM::R12;
4431 case ARM::R12: return ARM::SP; case ARM::SP: return ARM::LR;
4432 case ARM::LR: return ARM::PC; case ARM::PC: return ARM::R0;
4433 }
4434}
4435
4436// Insert an <Encoding, Register> pair in an ordered vector. Return true on
4437// success, or false, if duplicate encoding found.
4438static bool
4439insertNoDuplicates(SmallVectorImpl<std::pair<unsigned, unsigned>> &Regs,
4440 unsigned Enc, unsigned Reg) {
4441 Regs.emplace_back(Enc, Reg);
4442 for (auto I = Regs.rbegin(), J = I + 1, E = Regs.rend(); J != E; ++I, ++J) {
4443 if (J->first == Enc) {
4444 Regs.erase(J.base());
4445 return false;
4446 }
4447 if (J->first < Enc)
4448 break;
4449 std::swap(*I, *J);
4450 }
4451 return true;
4452}
4453
4454/// Parse a register list.
4455bool ARMAsmParser::parseRegisterList(OperandVector &Operands, bool EnforceOrder,
4456 bool AllowRAAC) {
4457 MCAsmParser &Parser = getParser();
4458 if (Parser.getTok().isNot(AsmToken::LCurly))
4459 return TokError("Token is not a Left Curly Brace");
4460 SMLoc S = Parser.getTok().getLoc();
4461 Parser.Lex(); // Eat '{' token.
4462 SMLoc RegLoc = Parser.getTok().getLoc();
4463
4464 // Check the first register in the list to see what register class
4465 // this is a list of.
4466 int Reg = tryParseRegister();
4467 if (Reg == -1)
4468 return Error(RegLoc, "register expected");
4469 if (!AllowRAAC && Reg == ARM::RA_AUTH_CODE)
4470 return Error(RegLoc, "pseudo-register not allowed");
4471 // The reglist instructions have at most 16 registers, so reserve
4472 // space for that many.
4473 int EReg = 0;
4475
4476 // Allow Q regs and just interpret them as the two D sub-registers.
4477 if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
4478 Reg = getDRegFromQReg(Reg);
4479 EReg = MRI->getEncodingValue(Reg);
4480 Registers.emplace_back(EReg, Reg);
4481 ++Reg;
4482 }
4483 const MCRegisterClass *RC;
4484 if (Reg == ARM::RA_AUTH_CODE ||
4485 ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
4486 RC = &ARMMCRegisterClasses[ARM::GPRRegClassID];
4487 else if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg))
4488 RC = &ARMMCRegisterClasses[ARM::DPRRegClassID];
4489 else if (ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg))
4490 RC = &ARMMCRegisterClasses[ARM::SPRRegClassID];
4491 else if (ARMMCRegisterClasses[ARM::GPRwithAPSRnospRegClassID].contains(Reg))
4492 RC = &ARMMCRegisterClasses[ARM::GPRwithAPSRnospRegClassID];
4493 else
4494 return Error(RegLoc, "invalid register in register list");
4495
4496 // Store the register.
4497 EReg = MRI->getEncodingValue(Reg);
4498 Registers.emplace_back(EReg, Reg);
4499
4500 // This starts immediately after the first register token in the list,
4501 // so we can see either a comma or a minus (range separator) as a legal
4502 // next token.
4503 while (Parser.getTok().is(AsmToken::Comma) ||
4504 Parser.getTok().is(AsmToken::Minus)) {
4505 if (Parser.getTok().is(AsmToken::Minus)) {
4506 if (Reg == ARM::RA_AUTH_CODE)
4507 return Error(RegLoc, "pseudo-register not allowed");
4508 Parser.Lex(); // Eat the minus.
4509 SMLoc AfterMinusLoc = Parser.getTok().getLoc();
4510 int EndReg = tryParseRegister();
4511 if (EndReg == -1)
4512 return Error(AfterMinusLoc, "register expected");
4513 if (EndReg == ARM::RA_AUTH_CODE)
4514 return Error(AfterMinusLoc, "pseudo-register not allowed");
4515 // Allow Q regs and just interpret them as the two D sub-registers.
4516 if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(EndReg))
4517 EndReg = getDRegFromQReg(EndReg) + 1;
4518 // If the register is the same as the start reg, there's nothing
4519 // more to do.
4520 if (Reg == EndReg)
4521 continue;
4522 // The register must be in the same register class as the first.
4523 if (!RC->contains(Reg))
4524 return Error(AfterMinusLoc, "invalid register in register list");
4525 // Ranges must go from low to high.
4526 if (MRI->getEncodingValue(Reg) > MRI->getEncodingValue(EndReg))
4527 return Error(AfterMinusLoc, "bad range in register list");
4528
4529 // Add all the registers in the range to the register list.
4530 while (Reg != EndReg) {
4532 EReg = MRI->getEncodingValue(Reg);
4533 if (!insertNoDuplicates(Registers, EReg, Reg)) {
4534 Warning(AfterMinusLoc, StringRef("duplicated register (") +
4536 ") in register list");
4537 }
4538 }
4539 continue;
4540 }
4541 Parser.Lex(); // Eat the comma.
4542 RegLoc = Parser.getTok().getLoc();
4543 int OldReg = Reg;
4544 const AsmToken RegTok = Parser.getTok();
4545 Reg = tryParseRegister();
4546 if (Reg == -1)
4547 return Error(RegLoc, "register expected");
4548 if (!AllowRAAC && Reg == ARM::RA_AUTH_CODE)
4549 return Error(RegLoc, "pseudo-register not allowed");
4550 // Allow Q regs and just interpret them as the two D sub-registers.
4551 bool isQReg = false;
4552 if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
4553 Reg = getDRegFromQReg(Reg);
4554 isQReg = true;
4555 }
4556 if (Reg != ARM::RA_AUTH_CODE && !RC->contains(Reg) &&
4557 RC->getID() == ARMMCRegisterClasses[ARM::GPRRegClassID].getID() &&
4558 ARMMCRegisterClasses[ARM::GPRwithAPSRnospRegClassID].contains(Reg)) {
4559 // switch the register classes, as GPRwithAPSRnospRegClassID is a partial
4560 // subset of GPRRegClassId except it contains APSR as well.
4561 RC = &ARMMCRegisterClasses[ARM::GPRwithAPSRnospRegClassID];
4562 }
4563 if (Reg == ARM::VPR &&
4564 (RC == &ARMMCRegisterClasses[ARM::SPRRegClassID] ||
4565 RC == &ARMMCRegisterClasses[ARM::DPRRegClassID] ||
4566 RC == &ARMMCRegisterClasses[ARM::FPWithVPRRegClassID])) {
4567 RC = &ARMMCRegisterClasses[ARM::FPWithVPRRegClassID];
4568 EReg = MRI->getEncodingValue(Reg);
4569 if (!insertNoDuplicates(Registers, EReg, Reg)) {
4570 Warning(RegLoc, "duplicated register (" + RegTok.getString() +
4571 ") in register list");
4572 }
4573 continue;
4574 }
4575 // The register must be in the same register class as the first.
4576 if ((Reg == ARM::RA_AUTH_CODE &&
4577 RC != &ARMMCRegisterClasses[ARM::GPRRegClassID]) ||
4578 (Reg != ARM::RA_AUTH_CODE && !RC->contains(Reg)))
4579 return Error(RegLoc, "invalid register in register list");
4580 // In most cases, the list must be monotonically increasing. An
4581 // exception is CLRM, which is order-independent anyway, so
4582 // there's no potential for confusion if you write clrm {r2,r1}
4583 // instead of clrm {r1,r2}.
4584 if (EnforceOrder &&
4585 MRI->getEncodingValue(Reg) < MRI->getEncodingValue(OldReg)) {
4586 if (ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
4587 Warning(RegLoc, "register list not in ascending order");
4588 else if (!ARMMCRegisterClasses[ARM::GPRwithAPSRnospRegClassID].contains(Reg))
4589 return Error(RegLoc, "register list not in ascending order");
4590 }
4591 // VFP register lists must also be contiguous.
4592 if (RC != &ARMMCRegisterClasses[ARM::GPRRegClassID] &&
4593 RC != &ARMMCRegisterClasses[ARM::GPRwithAPSRnospRegClassID] &&
4594 Reg != OldReg + 1)
4595 return Error(RegLoc, "non-contiguous register range");
4596 EReg = MRI->getEncodingValue(Reg);
4597 if (!insertNoDuplicates(Registers, EReg, Reg)) {
4598 Warning(RegLoc, "duplicated register (" + RegTok.getString() +
4599 ") in register list");
4600 }
4601 if (isQReg) {
4602 EReg = MRI->getEncodingValue(++Reg);
4603 Registers.emplace_back(EReg, Reg);
4604 }
4605 }
4606
4607 if (Parser.getTok().isNot(AsmToken::RCurly))
4608 return Error(Parser.getTok().getLoc(), "'}' expected");
4609 SMLoc E = Parser.getTok().getEndLoc();
4610 Parser.Lex(); // Eat '}' token.
4611
4612 // Push the register list operand.
4613 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
4614
4615 // The ARM system instruction variants for LDM/STM have a '^' token here.
4616 if (Parser.getTok().is(AsmToken::Caret)) {
4617 Operands.push_back(ARMOperand::CreateToken("^",Parser.getTok().getLoc()));
4618 Parser.Lex(); // Eat '^' token.
4619 }
4620
4621 return false;
4622}
4623
4624// Helper function to parse the lane index for vector lists.
4625ParseStatus ARMAsmParser::parseVectorLane(VectorLaneTy &LaneKind,
4626 unsigned &Index, SMLoc &EndLoc) {
4627 MCAsmParser &Parser = getParser();
4628 Index = 0; // Always return a defined index value.
4629 if (Parser.getTok().is(AsmToken::LBrac)) {
4630 Parser.Lex(); // Eat the '['.
4631 if (Parser.getTok().is(AsmToken::RBrac)) {
4632 // "Dn[]" is the 'all lanes' syntax.
4633 LaneKind = AllLanes;
4634 EndLoc = Parser.getTok().getEndLoc();
4635 Parser.Lex(); // Eat the ']'.
4636 return ParseStatus::Success;
4637 }
4638
4639 // There's an optional '#' token here. Normally there wouldn't be, but
4640 // inline assemble puts one in, and it's friendly to accept that.
4641 if (Parser.getTok().is(AsmToken::Hash))
4642 Parser.Lex(); // Eat '#' or '$'.
4643
4644 const MCExpr *LaneIndex;
4645 SMLoc Loc = Parser.getTok().getLoc();
4646 if (getParser().parseExpression(LaneIndex))
4647 return Error(Loc, "illegal expression");
4648 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(LaneIndex);
4649 if (!CE)
4650 return Error(Loc, "lane index must be empty or an integer");
4651 if (Parser.getTok().isNot(AsmToken::RBrac))
4652 return Error(Parser.getTok().getLoc(), "']' expected");
4653 EndLoc = Parser.getTok().getEndLoc();
4654 Parser.Lex(); // Eat the ']'.
4655 int64_t Val = CE->getValue();
4656
4657 // FIXME: Make this range check context sensitive for .8, .16, .32.
4658 if (Val < 0 || Val > 7)
4659 return Error(Parser.getTok().getLoc(), "lane index out of range");
4660 Index = Val;
4661 LaneKind = IndexedLane;
4662 return ParseStatus::Success;
4663 }
4664 LaneKind = NoLanes;
4665 return ParseStatus::Success;
4666}
4667
4668// parse a vector register list
4669ParseStatus ARMAsmParser::parseVectorList(OperandVector &Operands) {
4670 MCAsmParser &Parser = getParser();
4671 VectorLaneTy LaneKind;
4672 unsigned LaneIndex;
4673 SMLoc S = Parser.getTok().getLoc();
4674 // As an extension (to match gas), support a plain D register or Q register
4675 // (without encosing curly braces) as a single or double entry list,
4676 // respectively.
4677 if (!hasMVE() && Parser.getTok().is(AsmToken::Identifier)) {
4678 SMLoc E = Parser.getTok().getEndLoc();
4679 int Reg = tryParseRegister();
4680 if (Reg == -1)
4681 return ParseStatus::NoMatch;
4682 if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg)) {
4683 ParseStatus Res = parseVectorLane(LaneKind, LaneIndex, E);
4684 if (!Res.isSuccess())
4685 return Res;
4686 switch (LaneKind) {
4687 case NoLanes:
4688 Operands.push_back(ARMOperand::CreateVectorList(Reg, 1, false, S, E));
4689 break;
4690 case AllLanes:
4691 Operands.push_back(ARMOperand::CreateVectorListAllLanes(Reg, 1, false,
4692 S, E));
4693 break;
4694 case IndexedLane:
4695 Operands.push_back(ARMOperand::CreateVectorListIndexed(Reg, 1,
4696 LaneIndex,
4697 false, S, E));