1 //===-- ARMAsmParser.cpp - Parse ARM assembly to MCInst instructions ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "MCTargetDesc/ARMBaseInfo.h"
11 #include "MCTargetDesc/ARMAddressingModes.h"
12 #include "MCTargetDesc/ARMMCExpr.h"
13 #include "llvm/MC/MCParser/MCAsmLexer.h"
14 #include "llvm/MC/MCParser/MCAsmParser.h"
15 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
16 #include "llvm/MC/MCAsmInfo.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/MC/MCStreamer.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/MC/MCInstrDesc.h"
22 #include "llvm/MC/MCRegisterInfo.h"
23 #include "llvm/MC/MCSubtargetInfo.h"
24 #include "llvm/MC/MCTargetAsmParser.h"
25 #include "llvm/Support/MathExtras.h"
26 #include "llvm/Support/SourceMgr.h"
27 #include "llvm/Support/TargetRegistry.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/ADT/BitVector.h"
30 #include "llvm/ADT/OwningPtr.h"
31 #include "llvm/ADT/STLExtras.h"
32 #include "llvm/ADT/SmallVector.h"
33 #include "llvm/ADT/StringSwitch.h"
34 #include "llvm/ADT/Twine.h"
42 enum VectorLaneTy { NoLanes, AllLanes, IndexedLane };
44 class ARMAsmParser : public MCTargetAsmParser {
48 // Map of register aliases registers via the .req directive.
49 StringMap<unsigned> RegisterReqs;
52 ARMCC::CondCodes Cond; // Condition for IT block.
53 unsigned Mask:4; // Condition mask for instructions.
54 // Starting at first 1 (from lsb).
55 // '1' condition as indicated in IT.
56 // '0' inverse of condition (else).
57 // Count of instructions in IT block is
58 // 4 - trailingzeroes(mask)
60 bool FirstCond; // Explicit flag for when we're parsing the
61 // First instruction in the IT block. It's
62 // implied in the mask, so needs special
65 unsigned CurPosition; // Current position in parsing of IT
66 // block. In range [0,3]. Initialized
67 // according to count of instructions in block.
68 // ~0U if no active IT block.
70 bool inITBlock() { return ITState.CurPosition != ~0U;}
71 void forwardITPosition() {
72 if (!inITBlock()) return;
73 // Move to the next instruction in the IT block, if there is one. If not,
74 // mark the block as done.
75 unsigned TZ = CountTrailingZeros_32(ITState.Mask);
76 if (++ITState.CurPosition == 5 - TZ)
77 ITState.CurPosition = ~0U; // Done with the IT block after this.
81 MCAsmParser &getParser() const { return Parser; }
82 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
84 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
85 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
87 int tryParseRegister();
88 bool tryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
89 int tryParseShiftRegister(SmallVectorImpl<MCParsedAsmOperand*> &);
90 bool parseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
91 bool parseMemory(SmallVectorImpl<MCParsedAsmOperand*> &);
92 bool parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, StringRef Mnemonic);
93 bool parsePrefix(ARMMCExpr::VariantKind &RefKind);
94 bool parseMemRegOffsetShift(ARM_AM::ShiftOpc &ShiftType,
95 unsigned &ShiftAmount);
96 bool parseDirectiveWord(unsigned Size, SMLoc L);
97 bool parseDirectiveThumb(SMLoc L);
98 bool parseDirectiveARM(SMLoc L);
99 bool parseDirectiveThumbFunc(SMLoc L);
100 bool parseDirectiveCode(SMLoc L);
101 bool parseDirectiveSyntax(SMLoc L);
102 bool parseDirectiveReq(StringRef Name, SMLoc L);
103 bool parseDirectiveUnreq(SMLoc L);
104 bool parseDirectiveArch(SMLoc L);
105 bool parseDirectiveEabiAttr(SMLoc L);
107 StringRef splitMnemonic(StringRef Mnemonic, unsigned &PredicationCode,
108 bool &CarrySetting, unsigned &ProcessorIMod,
110 void getMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
111 bool &CanAcceptPredicationCode);
113 bool isThumb() const {
114 // FIXME: Can tablegen auto-generate this?
115 return (STI.getFeatureBits() & ARM::ModeThumb) != 0;
117 bool isThumbOne() const {
118 return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2) == 0;
120 bool isThumbTwo() const {
121 return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2);
123 bool hasV6Ops() const {
124 return STI.getFeatureBits() & ARM::HasV6Ops;
126 bool hasV7Ops() const {
127 return STI.getFeatureBits() & ARM::HasV7Ops;
130 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(ARM::ModeThumb));
131 setAvailableFeatures(FB);
133 bool isMClass() const {
134 return STI.getFeatureBits() & ARM::FeatureMClass;
137 /// @name Auto-generated Match Functions
140 #define GET_ASSEMBLER_HEADER
141 #include "ARMGenAsmMatcher.inc"
145 OperandMatchResultTy parseITCondCode(SmallVectorImpl<MCParsedAsmOperand*>&);
146 OperandMatchResultTy parseCoprocNumOperand(
147 SmallVectorImpl<MCParsedAsmOperand*>&);
148 OperandMatchResultTy parseCoprocRegOperand(
149 SmallVectorImpl<MCParsedAsmOperand*>&);
150 OperandMatchResultTy parseCoprocOptionOperand(
151 SmallVectorImpl<MCParsedAsmOperand*>&);
152 OperandMatchResultTy parseMemBarrierOptOperand(
153 SmallVectorImpl<MCParsedAsmOperand*>&);
154 OperandMatchResultTy parseProcIFlagsOperand(
155 SmallVectorImpl<MCParsedAsmOperand*>&);
156 OperandMatchResultTy parseMSRMaskOperand(
157 SmallVectorImpl<MCParsedAsmOperand*>&);
158 OperandMatchResultTy parsePKHImm(SmallVectorImpl<MCParsedAsmOperand*> &O,
159 StringRef Op, int Low, int High);
160 OperandMatchResultTy parsePKHLSLImm(SmallVectorImpl<MCParsedAsmOperand*> &O) {
161 return parsePKHImm(O, "lsl", 0, 31);
163 OperandMatchResultTy parsePKHASRImm(SmallVectorImpl<MCParsedAsmOperand*> &O) {
164 return parsePKHImm(O, "asr", 1, 32);
166 OperandMatchResultTy parseSetEndImm(SmallVectorImpl<MCParsedAsmOperand*>&);
167 OperandMatchResultTy parseShifterImm(SmallVectorImpl<MCParsedAsmOperand*>&);
168 OperandMatchResultTy parseRotImm(SmallVectorImpl<MCParsedAsmOperand*>&);
169 OperandMatchResultTy parseBitfield(SmallVectorImpl<MCParsedAsmOperand*>&);
170 OperandMatchResultTy parsePostIdxReg(SmallVectorImpl<MCParsedAsmOperand*>&);
171 OperandMatchResultTy parseAM3Offset(SmallVectorImpl<MCParsedAsmOperand*>&);
172 OperandMatchResultTy parseFPImm(SmallVectorImpl<MCParsedAsmOperand*>&);
173 OperandMatchResultTy parseVectorList(SmallVectorImpl<MCParsedAsmOperand*>&);
174 OperandMatchResultTy parseVectorLane(VectorLaneTy &LaneKind, unsigned &Index);
176 // Asm Match Converter Methods
177 bool cvtT2LdrdPre(MCInst &Inst, unsigned Opcode,
178 const SmallVectorImpl<MCParsedAsmOperand*> &);
179 bool cvtT2StrdPre(MCInst &Inst, unsigned Opcode,
180 const SmallVectorImpl<MCParsedAsmOperand*> &);
181 bool cvtLdWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
182 const SmallVectorImpl<MCParsedAsmOperand*> &);
183 bool cvtStWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
184 const SmallVectorImpl<MCParsedAsmOperand*> &);
185 bool cvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
186 const SmallVectorImpl<MCParsedAsmOperand*> &);
187 bool cvtLdWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
188 const SmallVectorImpl<MCParsedAsmOperand*> &);
189 bool cvtStWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
190 const SmallVectorImpl<MCParsedAsmOperand*> &);
191 bool cvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
192 const SmallVectorImpl<MCParsedAsmOperand*> &);
193 bool cvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
194 const SmallVectorImpl<MCParsedAsmOperand*> &);
195 bool cvtLdExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
196 const SmallVectorImpl<MCParsedAsmOperand*> &);
197 bool cvtLdExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
198 const SmallVectorImpl<MCParsedAsmOperand*> &);
199 bool cvtStExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
200 const SmallVectorImpl<MCParsedAsmOperand*> &);
201 bool cvtStExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
202 const SmallVectorImpl<MCParsedAsmOperand*> &);
203 bool cvtLdrdPre(MCInst &Inst, unsigned Opcode,
204 const SmallVectorImpl<MCParsedAsmOperand*> &);
205 bool cvtStrdPre(MCInst &Inst, unsigned Opcode,
206 const SmallVectorImpl<MCParsedAsmOperand*> &);
207 bool cvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
208 const SmallVectorImpl<MCParsedAsmOperand*> &);
209 bool cvtThumbMultiply(MCInst &Inst, unsigned Opcode,
210 const SmallVectorImpl<MCParsedAsmOperand*> &);
211 bool cvtVLDwbFixed(MCInst &Inst, unsigned Opcode,
212 const SmallVectorImpl<MCParsedAsmOperand*> &);
213 bool cvtVLDwbRegister(MCInst &Inst, unsigned Opcode,
214 const SmallVectorImpl<MCParsedAsmOperand*> &);
215 bool cvtVSTwbFixed(MCInst &Inst, unsigned Opcode,
216 const SmallVectorImpl<MCParsedAsmOperand*> &);
217 bool cvtVSTwbRegister(MCInst &Inst, unsigned Opcode,
218 const SmallVectorImpl<MCParsedAsmOperand*> &);
220 bool validateInstruction(MCInst &Inst,
221 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
222 bool processInstruction(MCInst &Inst,
223 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
224 bool shouldOmitCCOutOperand(StringRef Mnemonic,
225 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
228 enum ARMMatchResultTy {
229 Match_RequiresITBlock = FIRST_TARGET_MATCH_RESULT_TY,
230 Match_RequiresNotITBlock,
235 ARMAsmParser(MCSubtargetInfo &_STI, MCAsmParser &_Parser)
236 : MCTargetAsmParser(), STI(_STI), Parser(_Parser) {
237 MCAsmParserExtension::Initialize(_Parser);
239 // Initialize the set of available features.
240 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
242 // Not in an ITBlock to start with.
243 ITState.CurPosition = ~0U;
246 // Implementation of the MCTargetAsmParser interface:
247 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
248 bool ParseInstruction(StringRef Name, SMLoc NameLoc,
249 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
250 bool ParseDirective(AsmToken DirectiveID);
252 unsigned checkTargetMatchPredicate(MCInst &Inst);
254 bool MatchAndEmitInstruction(SMLoc IDLoc,
255 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
258 } // end anonymous namespace
262 /// ARMOperand - Instances of this class represent a parsed ARM machine
264 class ARMOperand : public MCParsedAsmOperand {
284 k_VectorListAllLanes,
290 k_BitfieldDescriptor,
294 SMLoc StartLoc, EndLoc;
295 SmallVector<unsigned, 8> Registers;
299 ARMCC::CondCodes Val;
319 ARM_PROC::IFlags Val;
335 // A vector register list is a sequential list of 1 to 4 registers.
351 /// Combined record for all forms of ARM address expressions.
354 // Offset is in OffsetReg or OffsetImm. If both are zero, no offset
356 const MCConstantExpr *OffsetImm; // Offset immediate value
357 unsigned OffsetRegNum; // Offset register num, when OffsetImm == NULL
358 ARM_AM::ShiftOpc ShiftType; // Shift type for OffsetReg
359 unsigned ShiftImm; // shift for OffsetReg.
360 unsigned Alignment; // 0 = no alignment specified
361 // n = alignment in bytes (2, 4, 8, 16, or 32)
362 unsigned isNegative : 1; // Negated OffsetReg? (~'U' bit)
368 ARM_AM::ShiftOpc ShiftTy;
377 ARM_AM::ShiftOpc ShiftTy;
383 ARM_AM::ShiftOpc ShiftTy;
396 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
398 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
400 StartLoc = o.StartLoc;
417 case k_DPRRegisterList:
418 case k_SPRRegisterList:
419 Registers = o.Registers;
422 case k_VectorListAllLanes:
423 case k_VectorListIndexed:
424 VectorList = o.VectorList;
431 CoprocOption = o.CoprocOption;
436 case k_MemBarrierOpt:
442 case k_PostIndexRegister:
443 PostIdxReg = o.PostIdxReg;
451 case k_ShifterImmediate:
452 ShifterImm = o.ShifterImm;
454 case k_ShiftedRegister:
455 RegShiftedReg = o.RegShiftedReg;
457 case k_ShiftedImmediate:
458 RegShiftedImm = o.RegShiftedImm;
460 case k_RotateImmediate:
463 case k_BitfieldDescriptor:
464 Bitfield = o.Bitfield;
467 VectorIndex = o.VectorIndex;
472 /// getStartLoc - Get the location of the first token of this operand.
473 SMLoc getStartLoc() const { return StartLoc; }
474 /// getEndLoc - Get the location of the last token of this operand.
475 SMLoc getEndLoc() const { return EndLoc; }
477 ARMCC::CondCodes getCondCode() const {
478 assert(Kind == k_CondCode && "Invalid access!");
482 unsigned getCoproc() const {
483 assert((Kind == k_CoprocNum || Kind == k_CoprocReg) && "Invalid access!");
487 StringRef getToken() const {
488 assert(Kind == k_Token && "Invalid access!");
489 return StringRef(Tok.Data, Tok.Length);
492 unsigned getReg() const {
493 assert((Kind == k_Register || Kind == k_CCOut) && "Invalid access!");
497 const SmallVectorImpl<unsigned> &getRegList() const {
498 assert((Kind == k_RegisterList || Kind == k_DPRRegisterList ||
499 Kind == k_SPRRegisterList) && "Invalid access!");
503 const MCExpr *getImm() const {
504 assert(isImm() && "Invalid access!");
508 unsigned getVectorIndex() const {
509 assert(Kind == k_VectorIndex && "Invalid access!");
510 return VectorIndex.Val;
513 ARM_MB::MemBOpt getMemBarrierOpt() const {
514 assert(Kind == k_MemBarrierOpt && "Invalid access!");
518 ARM_PROC::IFlags getProcIFlags() const {
519 assert(Kind == k_ProcIFlags && "Invalid access!");
523 unsigned getMSRMask() const {
524 assert(Kind == k_MSRMask && "Invalid access!");
528 bool isCoprocNum() const { return Kind == k_CoprocNum; }
529 bool isCoprocReg() const { return Kind == k_CoprocReg; }
530 bool isCoprocOption() const { return Kind == k_CoprocOption; }
531 bool isCondCode() const { return Kind == k_CondCode; }
532 bool isCCOut() const { return Kind == k_CCOut; }
533 bool isITMask() const { return Kind == k_ITCondMask; }
534 bool isITCondCode() const { return Kind == k_CondCode; }
535 bool isImm() const { return Kind == k_Immediate; }
536 bool isFPImm() const {
537 if (!isImm()) return false;
538 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
539 if (!CE) return false;
540 int Val = ARM_AM::getFP32Imm(APInt(32, CE->getValue()));
543 bool isFBits16() const {
544 if (!isImm()) return false;
545 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
546 if (!CE) return false;
547 int64_t Value = CE->getValue();
548 return Value >= 0 && Value <= 16;
550 bool isFBits32() const {
551 if (!isImm()) return false;
552 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
553 if (!CE) return false;
554 int64_t Value = CE->getValue();
555 return Value >= 1 && Value <= 32;
557 bool isImm8s4() const {
558 if (!isImm()) return false;
559 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
560 if (!CE) return false;
561 int64_t Value = CE->getValue();
562 return ((Value & 3) == 0) && Value >= -1020 && Value <= 1020;
564 bool isImm0_1020s4() const {
565 if (!isImm()) return false;
566 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
567 if (!CE) return false;
568 int64_t Value = CE->getValue();
569 return ((Value & 3) == 0) && Value >= 0 && Value <= 1020;
571 bool isImm0_508s4() const {
572 if (!isImm()) return false;
573 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
574 if (!CE) return false;
575 int64_t Value = CE->getValue();
576 return ((Value & 3) == 0) && Value >= 0 && Value <= 508;
578 bool isImm0_255() const {
579 if (!isImm()) return false;
580 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
581 if (!CE) return false;
582 int64_t Value = CE->getValue();
583 return Value >= 0 && Value < 256;
585 bool isImm0_1() const {
586 if (!isImm()) return false;
587 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
588 if (!CE) return false;
589 int64_t Value = CE->getValue();
590 return Value >= 0 && Value < 2;
592 bool isImm0_3() const {
593 if (!isImm()) return false;
594 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
595 if (!CE) return false;
596 int64_t Value = CE->getValue();
597 return Value >= 0 && Value < 4;
599 bool isImm0_7() const {
600 if (!isImm()) return false;
601 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
602 if (!CE) return false;
603 int64_t Value = CE->getValue();
604 return Value >= 0 && Value < 8;
606 bool isImm0_15() const {
607 if (!isImm()) return false;
608 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
609 if (!CE) return false;
610 int64_t Value = CE->getValue();
611 return Value >= 0 && Value < 16;
613 bool isImm0_31() const {
614 if (!isImm()) return false;
615 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
616 if (!CE) return false;
617 int64_t Value = CE->getValue();
618 return Value >= 0 && Value < 32;
620 bool isImm0_63() const {
621 if (!isImm()) return false;
622 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
623 if (!CE) return false;
624 int64_t Value = CE->getValue();
625 return Value >= 0 && Value < 64;
627 bool isImm8() const {
628 if (!isImm()) return false;
629 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
630 if (!CE) return false;
631 int64_t Value = CE->getValue();
634 bool isImm16() const {
635 if (!isImm()) return false;
636 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
637 if (!CE) return false;
638 int64_t Value = CE->getValue();
641 bool isImm32() const {
642 if (!isImm()) return false;
643 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
644 if (!CE) return false;
645 int64_t Value = CE->getValue();
648 bool isShrImm8() const {
649 if (!isImm()) return false;
650 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
651 if (!CE) return false;
652 int64_t Value = CE->getValue();
653 return Value > 0 && Value <= 8;
655 bool isShrImm16() const {
656 if (!isImm()) return false;
657 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
658 if (!CE) return false;
659 int64_t Value = CE->getValue();
660 return Value > 0 && Value <= 16;
662 bool isShrImm32() const {
663 if (!isImm()) return false;
664 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
665 if (!CE) return false;
666 int64_t Value = CE->getValue();
667 return Value > 0 && Value <= 32;
669 bool isShrImm64() const {
670 if (!isImm()) return false;
671 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
672 if (!CE) return false;
673 int64_t Value = CE->getValue();
674 return Value > 0 && Value <= 64;
676 bool isImm1_7() const {
677 if (!isImm()) return false;
678 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
679 if (!CE) return false;
680 int64_t Value = CE->getValue();
681 return Value > 0 && Value < 8;
683 bool isImm1_15() const {
684 if (!isImm()) return false;
685 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
686 if (!CE) return false;
687 int64_t Value = CE->getValue();
688 return Value > 0 && Value < 16;
690 bool isImm1_31() const {
691 if (!isImm()) return false;
692 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
693 if (!CE) return false;
694 int64_t Value = CE->getValue();
695 return Value > 0 && Value < 32;
697 bool isImm1_16() const {
698 if (!isImm()) return false;
699 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
700 if (!CE) return false;
701 int64_t Value = CE->getValue();
702 return Value > 0 && Value < 17;
704 bool isImm1_32() const {
705 if (!isImm()) return false;
706 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
707 if (!CE) return false;
708 int64_t Value = CE->getValue();
709 return Value > 0 && Value < 33;
711 bool isImm0_32() const {
712 if (!isImm()) return false;
713 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
714 if (!CE) return false;
715 int64_t Value = CE->getValue();
716 return Value >= 0 && Value < 33;
718 bool isImm0_65535() const {
719 if (!isImm()) return false;
720 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
721 if (!CE) return false;
722 int64_t Value = CE->getValue();
723 return Value >= 0 && Value < 65536;
725 bool isImm0_65535Expr() const {
726 if (!isImm()) return false;
727 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
728 // If it's not a constant expression, it'll generate a fixup and be
730 if (!CE) return true;
731 int64_t Value = CE->getValue();
732 return Value >= 0 && Value < 65536;
734 bool isImm24bit() const {
735 if (!isImm()) return false;
736 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
737 if (!CE) return false;
738 int64_t Value = CE->getValue();
739 return Value >= 0 && Value <= 0xffffff;
741 bool isImmThumbSR() const {
742 if (!isImm()) return false;
743 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
744 if (!CE) return false;
745 int64_t Value = CE->getValue();
746 return Value > 0 && Value < 33;
748 bool isPKHLSLImm() const {
749 if (!isImm()) return false;
750 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
751 if (!CE) return false;
752 int64_t Value = CE->getValue();
753 return Value >= 0 && Value < 32;
755 bool isPKHASRImm() const {
756 if (!isImm()) return false;
757 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
758 if (!CE) return false;
759 int64_t Value = CE->getValue();
760 return Value > 0 && Value <= 32;
762 bool isARMSOImm() const {
763 if (!isImm()) return false;
764 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
765 if (!CE) return false;
766 int64_t Value = CE->getValue();
767 return ARM_AM::getSOImmVal(Value) != -1;
769 bool isARMSOImmNot() const {
770 if (!isImm()) return false;
771 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
772 if (!CE) return false;
773 int64_t Value = CE->getValue();
774 return ARM_AM::getSOImmVal(~Value) != -1;
776 bool isARMSOImmNeg() const {
777 if (!isImm()) return false;
778 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
779 if (!CE) return false;
780 int64_t Value = CE->getValue();
781 return ARM_AM::getSOImmVal(-Value) != -1;
783 bool isT2SOImm() const {
784 if (!isImm()) return false;
785 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
786 if (!CE) return false;
787 int64_t Value = CE->getValue();
788 return ARM_AM::getT2SOImmVal(Value) != -1;
790 bool isT2SOImmNot() const {
791 if (!isImm()) return false;
792 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
793 if (!CE) return false;
794 int64_t Value = CE->getValue();
795 return ARM_AM::getT2SOImmVal(~Value) != -1;
797 bool isT2SOImmNeg() const {
798 if (!isImm()) return false;
799 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
800 if (!CE) return false;
801 int64_t Value = CE->getValue();
802 return ARM_AM::getT2SOImmVal(-Value) != -1;
804 bool isSetEndImm() const {
805 if (!isImm()) return false;
806 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
807 if (!CE) return false;
808 int64_t Value = CE->getValue();
809 return Value == 1 || Value == 0;
811 bool isReg() const { return Kind == k_Register; }
812 bool isRegList() const { return Kind == k_RegisterList; }
813 bool isDPRRegList() const { return Kind == k_DPRRegisterList; }
814 bool isSPRRegList() const { return Kind == k_SPRRegisterList; }
815 bool isToken() const { return Kind == k_Token; }
816 bool isMemBarrierOpt() const { return Kind == k_MemBarrierOpt; }
817 bool isMemory() const { return Kind == k_Memory; }
818 bool isShifterImm() const { return Kind == k_ShifterImmediate; }
819 bool isRegShiftedReg() const { return Kind == k_ShiftedRegister; }
820 bool isRegShiftedImm() const { return Kind == k_ShiftedImmediate; }
821 bool isRotImm() const { return Kind == k_RotateImmediate; }
822 bool isBitfield() const { return Kind == k_BitfieldDescriptor; }
823 bool isPostIdxRegShifted() const { return Kind == k_PostIndexRegister; }
824 bool isPostIdxReg() const {
825 return Kind == k_PostIndexRegister && PostIdxReg.ShiftTy ==ARM_AM::no_shift;
827 bool isMemNoOffset(bool alignOK = false) const {
830 // No offset of any kind.
831 return Memory.OffsetRegNum == 0 && Memory.OffsetImm == 0 &&
832 (alignOK || Memory.Alignment == 0);
834 bool isMemPCRelImm12() const {
835 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
837 // Base register must be PC.
838 if (Memory.BaseRegNum != ARM::PC)
840 // Immediate offset in range [-4095, 4095].
841 if (!Memory.OffsetImm) return true;
842 int64_t Val = Memory.OffsetImm->getValue();
843 return (Val > -4096 && Val < 4096) || (Val == INT32_MIN);
845 bool isAlignedMemory() const {
846 return isMemNoOffset(true);
848 bool isAddrMode2() const {
849 if (!isMemory() || Memory.Alignment != 0) return false;
850 // Check for register offset.
851 if (Memory.OffsetRegNum) return true;
852 // Immediate offset in range [-4095, 4095].
853 if (!Memory.OffsetImm) return true;
854 int64_t Val = Memory.OffsetImm->getValue();
855 return Val > -4096 && Val < 4096;
857 bool isAM2OffsetImm() const {
858 if (!isImm()) return false;
859 // Immediate offset in range [-4095, 4095].
860 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
861 if (!CE) return false;
862 int64_t Val = CE->getValue();
863 return Val > -4096 && Val < 4096;
865 bool isAddrMode3() const {
866 // If we have an immediate that's not a constant, treat it as a label
867 // reference needing a fixup. If it is a constant, it's something else
869 if (isImm() && !isa<MCConstantExpr>(getImm()))
871 if (!isMemory() || Memory.Alignment != 0) return false;
872 // No shifts are legal for AM3.
873 if (Memory.ShiftType != ARM_AM::no_shift) return false;
874 // Check for register offset.
875 if (Memory.OffsetRegNum) return true;
876 // Immediate offset in range [-255, 255].
877 if (!Memory.OffsetImm) return true;
878 int64_t Val = Memory.OffsetImm->getValue();
879 return Val > -256 && Val < 256;
881 bool isAM3Offset() const {
882 if (Kind != k_Immediate && Kind != k_PostIndexRegister)
884 if (Kind == k_PostIndexRegister)
885 return PostIdxReg.ShiftTy == ARM_AM::no_shift;
886 // Immediate offset in range [-255, 255].
887 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
888 if (!CE) return false;
889 int64_t Val = CE->getValue();
890 // Special case, #-0 is INT32_MIN.
891 return (Val > -256 && Val < 256) || Val == INT32_MIN;
893 bool isAddrMode5() const {
894 // If we have an immediate that's not a constant, treat it as a label
895 // reference needing a fixup. If it is a constant, it's something else
897 if (isImm() && !isa<MCConstantExpr>(getImm()))
899 if (!isMemory() || Memory.Alignment != 0) return false;
900 // Check for register offset.
901 if (Memory.OffsetRegNum) return false;
902 // Immediate offset in range [-1020, 1020] and a multiple of 4.
903 if (!Memory.OffsetImm) return true;
904 int64_t Val = Memory.OffsetImm->getValue();
905 return (Val >= -1020 && Val <= 1020 && ((Val & 3) == 0)) ||
908 bool isMemTBB() const {
909 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
910 Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
914 bool isMemTBH() const {
915 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
916 Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm != 1 ||
917 Memory.Alignment != 0 )
921 bool isMemRegOffset() const {
922 if (!isMemory() || !Memory.OffsetRegNum || Memory.Alignment != 0)
926 bool isT2MemRegOffset() const {
927 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
928 Memory.Alignment != 0)
930 // Only lsl #{0, 1, 2, 3} allowed.
931 if (Memory.ShiftType == ARM_AM::no_shift)
933 if (Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm > 3)
937 bool isMemThumbRR() const {
938 // Thumb reg+reg addressing is simple. Just two registers, a base and
939 // an offset. No shifts, negations or any other complicating factors.
940 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
941 Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
943 return isARMLowRegister(Memory.BaseRegNum) &&
944 (!Memory.OffsetRegNum || isARMLowRegister(Memory.OffsetRegNum));
946 bool isMemThumbRIs4() const {
947 if (!isMemory() || Memory.OffsetRegNum != 0 ||
948 !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
950 // Immediate offset, multiple of 4 in range [0, 124].
951 if (!Memory.OffsetImm) return true;
952 int64_t Val = Memory.OffsetImm->getValue();
953 return Val >= 0 && Val <= 124 && (Val % 4) == 0;
955 bool isMemThumbRIs2() const {
956 if (!isMemory() || Memory.OffsetRegNum != 0 ||
957 !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
959 // Immediate offset, multiple of 4 in range [0, 62].
960 if (!Memory.OffsetImm) return true;
961 int64_t Val = Memory.OffsetImm->getValue();
962 return Val >= 0 && Val <= 62 && (Val % 2) == 0;
964 bool isMemThumbRIs1() const {
965 if (!isMemory() || Memory.OffsetRegNum != 0 ||
966 !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
968 // Immediate offset in range [0, 31].
969 if (!Memory.OffsetImm) return true;
970 int64_t Val = Memory.OffsetImm->getValue();
971 return Val >= 0 && Val <= 31;
973 bool isMemThumbSPI() const {
974 if (!isMemory() || Memory.OffsetRegNum != 0 ||
975 Memory.BaseRegNum != ARM::SP || Memory.Alignment != 0)
977 // Immediate offset, multiple of 4 in range [0, 1020].
978 if (!Memory.OffsetImm) return true;
979 int64_t Val = Memory.OffsetImm->getValue();
980 return Val >= 0 && Val <= 1020 && (Val % 4) == 0;
982 bool isMemImm8s4Offset() const {
983 // If we have an immediate that's not a constant, treat it as a label
984 // reference needing a fixup. If it is a constant, it's something else
986 if (isImm() && !isa<MCConstantExpr>(getImm()))
988 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
990 // Immediate offset a multiple of 4 in range [-1020, 1020].
991 if (!Memory.OffsetImm) return true;
992 int64_t Val = Memory.OffsetImm->getValue();
993 return Val >= -1020 && Val <= 1020 && (Val & 3) == 0;
995 bool isMemImm0_1020s4Offset() const {
996 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
998 // Immediate offset a multiple of 4 in range [0, 1020].
999 if (!Memory.OffsetImm) return true;
1000 int64_t Val = Memory.OffsetImm->getValue();
1001 return Val >= 0 && Val <= 1020 && (Val & 3) == 0;
1003 bool isMemImm8Offset() const {
1004 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1006 // Base reg of PC isn't allowed for these encodings.
1007 if (Memory.BaseRegNum == ARM::PC) return false;
1008 // Immediate offset in range [-255, 255].
1009 if (!Memory.OffsetImm) return true;
1010 int64_t Val = Memory.OffsetImm->getValue();
1011 return (Val == INT32_MIN) || (Val > -256 && Val < 256);
1013 bool isMemPosImm8Offset() const {
1014 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1016 // Immediate offset in range [0, 255].
1017 if (!Memory.OffsetImm) return true;
1018 int64_t Val = Memory.OffsetImm->getValue();
1019 return Val >= 0 && Val < 256;
1021 bool isMemNegImm8Offset() const {
1022 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1024 // Base reg of PC isn't allowed for these encodings.
1025 if (Memory.BaseRegNum == ARM::PC) return false;
1026 // Immediate offset in range [-255, -1].
1027 if (!Memory.OffsetImm) return false;
1028 int64_t Val = Memory.OffsetImm->getValue();
1029 return (Val == INT32_MIN) || (Val > -256 && Val < 0);
1031 bool isMemUImm12Offset() const {
1032 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1034 // Immediate offset in range [0, 4095].
1035 if (!Memory.OffsetImm) return true;
1036 int64_t Val = Memory.OffsetImm->getValue();
1037 return (Val >= 0 && Val < 4096);
1039 bool isMemImm12Offset() const {
1040 // If we have an immediate that's not a constant, treat it as a label
1041 // reference needing a fixup. If it is a constant, it's something else
1042 // and we reject it.
1043 if (isImm() && !isa<MCConstantExpr>(getImm()))
1046 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1048 // Immediate offset in range [-4095, 4095].
1049 if (!Memory.OffsetImm) return true;
1050 int64_t Val = Memory.OffsetImm->getValue();
1051 return (Val > -4096 && Val < 4096) || (Val == INT32_MIN);
1053 bool isPostIdxImm8() const {
1054 if (!isImm()) return false;
1055 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1056 if (!CE) return false;
1057 int64_t Val = CE->getValue();
1058 return (Val > -256 && Val < 256) || (Val == INT32_MIN);
1060 bool isPostIdxImm8s4() const {
1061 if (!isImm()) return false;
1062 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1063 if (!CE) return false;
1064 int64_t Val = CE->getValue();
1065 return ((Val & 3) == 0 && Val >= -1020 && Val <= 1020) ||
1069 bool isMSRMask() const { return Kind == k_MSRMask; }
1070 bool isProcIFlags() const { return Kind == k_ProcIFlags; }
1073 bool isSingleSpacedVectorList() const {
1074 return Kind == k_VectorList && !VectorList.isDoubleSpaced;
1076 bool isDoubleSpacedVectorList() const {
1077 return Kind == k_VectorList && VectorList.isDoubleSpaced;
1079 bool isVecListOneD() const {
1080 if (!isSingleSpacedVectorList()) return false;
1081 return VectorList.Count == 1;
1084 bool isVecListTwoD() const {
1085 if (!isSingleSpacedVectorList()) return false;
1086 return VectorList.Count == 2;
1089 bool isVecListThreeD() const {
1090 if (!isSingleSpacedVectorList()) return false;
1091 return VectorList.Count == 3;
1094 bool isVecListFourD() const {
1095 if (!isSingleSpacedVectorList()) return false;
1096 return VectorList.Count == 4;
1099 bool isVecListTwoQ() const {
1100 if (!isDoubleSpacedVectorList()) return false;
1101 return VectorList.Count == 2;
1104 bool isSingleSpacedVectorAllLanes() const {
1105 return Kind == k_VectorListAllLanes && !VectorList.isDoubleSpaced;
1107 bool isDoubleSpacedVectorAllLanes() const {
1108 return Kind == k_VectorListAllLanes && VectorList.isDoubleSpaced;
1110 bool isVecListOneDAllLanes() const {
1111 if (!isSingleSpacedVectorAllLanes()) return false;
1112 return VectorList.Count == 1;
1115 bool isVecListTwoDAllLanes() const {
1116 if (!isSingleSpacedVectorAllLanes()) return false;
1117 return VectorList.Count == 2;
1120 bool isVecListTwoQAllLanes() const {
1121 if (!isDoubleSpacedVectorAllLanes()) return false;
1122 return VectorList.Count == 2;
1125 bool isSingleSpacedVectorIndexed() const {
1126 return Kind == k_VectorListIndexed && !VectorList.isDoubleSpaced;
1128 bool isDoubleSpacedVectorIndexed() const {
1129 return Kind == k_VectorListIndexed && VectorList.isDoubleSpaced;
1131 bool isVecListOneDByteIndexed() const {
1132 if (!isSingleSpacedVectorIndexed()) return false;
1133 return VectorList.Count == 1 && VectorList.LaneIndex <= 7;
1136 bool isVecListOneDHWordIndexed() const {
1137 if (!isSingleSpacedVectorIndexed()) return false;
1138 return VectorList.Count == 1 && VectorList.LaneIndex <= 3;
1141 bool isVecListOneDWordIndexed() const {
1142 if (!isSingleSpacedVectorIndexed()) return false;
1143 return VectorList.Count == 1 && VectorList.LaneIndex <= 1;
1146 bool isVecListTwoDByteIndexed() const {
1147 if (!isSingleSpacedVectorIndexed()) return false;
1148 return VectorList.Count == 2 && VectorList.LaneIndex <= 7;
1151 bool isVecListTwoDHWordIndexed() const {
1152 if (!isSingleSpacedVectorIndexed()) return false;
1153 return VectorList.Count == 2 && VectorList.LaneIndex <= 3;
1156 bool isVecListTwoQWordIndexed() const {
1157 if (!isDoubleSpacedVectorIndexed()) return false;
1158 return VectorList.Count == 2 && VectorList.LaneIndex <= 1;
1161 bool isVecListTwoQHWordIndexed() const {
1162 if (!isDoubleSpacedVectorIndexed()) return false;
1163 return VectorList.Count == 2 && VectorList.LaneIndex <= 3;
1166 bool isVecListTwoDWordIndexed() const {
1167 if (!isSingleSpacedVectorIndexed()) return false;
1168 return VectorList.Count == 2 && VectorList.LaneIndex <= 1;
1171 bool isVectorIndex8() const {
1172 if (Kind != k_VectorIndex) return false;
1173 return VectorIndex.Val < 8;
1175 bool isVectorIndex16() const {
1176 if (Kind != k_VectorIndex) return false;
1177 return VectorIndex.Val < 4;
1179 bool isVectorIndex32() const {
1180 if (Kind != k_VectorIndex) return false;
1181 return VectorIndex.Val < 2;
1184 bool isNEONi8splat() const {
1185 if (!isImm()) return false;
1186 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1187 // Must be a constant.
1188 if (!CE) return false;
1189 int64_t Value = CE->getValue();
1190 // i8 value splatted across 8 bytes. The immediate is just the 8 byte
1192 return Value >= 0 && Value < 256;
1195 bool isNEONi16splat() const {
1196 if (!isImm()) return false;
1197 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1198 // Must be a constant.
1199 if (!CE) return false;
1200 int64_t Value = CE->getValue();
1201 // i16 value in the range [0,255] or [0x0100, 0xff00]
1202 return (Value >= 0 && Value < 256) || (Value >= 0x0100 && Value <= 0xff00);
1205 bool isNEONi32splat() const {
1206 if (!isImm()) return false;
1207 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1208 // Must be a constant.
1209 if (!CE) return false;
1210 int64_t Value = CE->getValue();
1211 // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X.
1212 return (Value >= 0 && Value < 256) ||
1213 (Value >= 0x0100 && Value <= 0xff00) ||
1214 (Value >= 0x010000 && Value <= 0xff0000) ||
1215 (Value >= 0x01000000 && Value <= 0xff000000);
1218 bool isNEONi32vmov() const {
1219 if (!isImm()) return false;
1220 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1221 // Must be a constant.
1222 if (!CE) return false;
1223 int64_t Value = CE->getValue();
1224 // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X,
1225 // for VMOV/VMVN only, 00Xf or 0Xff are also accepted.
1226 return (Value >= 0 && Value < 256) ||
1227 (Value >= 0x0100 && Value <= 0xff00) ||
1228 (Value >= 0x010000 && Value <= 0xff0000) ||
1229 (Value >= 0x01000000 && Value <= 0xff000000) ||
1230 (Value >= 0x01ff && Value <= 0xffff && (Value & 0xff) == 0xff) ||
1231 (Value >= 0x01ffff && Value <= 0xffffff && (Value & 0xffff) == 0xffff);
1233 bool isNEONi32vmovNeg() const {
1234 if (!isImm()) return false;
1235 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1236 // Must be a constant.
1237 if (!CE) return false;
1238 int64_t Value = ~CE->getValue();
1239 // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X,
1240 // for VMOV/VMVN only, 00Xf or 0Xff are also accepted.
1241 return (Value >= 0 && Value < 256) ||
1242 (Value >= 0x0100 && Value <= 0xff00) ||
1243 (Value >= 0x010000 && Value <= 0xff0000) ||
1244 (Value >= 0x01000000 && Value <= 0xff000000) ||
1245 (Value >= 0x01ff && Value <= 0xffff && (Value & 0xff) == 0xff) ||
1246 (Value >= 0x01ffff && Value <= 0xffffff && (Value & 0xffff) == 0xffff);
1249 bool isNEONi64splat() const {
1250 if (!isImm()) return false;
1251 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1252 // Must be a constant.
1253 if (!CE) return false;
1254 uint64_t Value = CE->getValue();
1255 // i64 value with each byte being either 0 or 0xff.
1256 for (unsigned i = 0; i < 8; ++i)
1257 if ((Value & 0xff) != 0 && (Value & 0xff) != 0xff) return false;
1261 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
1262 // Add as immediates when possible. Null MCExpr = 0.
1264 Inst.addOperand(MCOperand::CreateImm(0));
1265 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
1266 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
1268 Inst.addOperand(MCOperand::CreateExpr(Expr));
1271 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
1272 assert(N == 2 && "Invalid number of operands!");
1273 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
1274 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
1275 Inst.addOperand(MCOperand::CreateReg(RegNum));
1278 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
1279 assert(N == 1 && "Invalid number of operands!");
1280 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
1283 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
1284 assert(N == 1 && "Invalid number of operands!");
1285 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
1288 void addCoprocOptionOperands(MCInst &Inst, unsigned N) const {
1289 assert(N == 1 && "Invalid number of operands!");
1290 Inst.addOperand(MCOperand::CreateImm(CoprocOption.Val));
1293 void addITMaskOperands(MCInst &Inst, unsigned N) const {
1294 assert(N == 1 && "Invalid number of operands!");
1295 Inst.addOperand(MCOperand::CreateImm(ITMask.Mask));
1298 void addITCondCodeOperands(MCInst &Inst, unsigned N) const {
1299 assert(N == 1 && "Invalid number of operands!");
1300 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
1303 void addCCOutOperands(MCInst &Inst, unsigned N) const {
1304 assert(N == 1 && "Invalid number of operands!");
1305 Inst.addOperand(MCOperand::CreateReg(getReg()));
1308 void addRegOperands(MCInst &Inst, unsigned N) const {
1309 assert(N == 1 && "Invalid number of operands!");
1310 Inst.addOperand(MCOperand::CreateReg(getReg()));
1313 void addRegShiftedRegOperands(MCInst &Inst, unsigned N) const {
1314 assert(N == 3 && "Invalid number of operands!");
1315 assert(isRegShiftedReg() &&
1316 "addRegShiftedRegOperands() on non RegShiftedReg!");
1317 Inst.addOperand(MCOperand::CreateReg(RegShiftedReg.SrcReg));
1318 Inst.addOperand(MCOperand::CreateReg(RegShiftedReg.ShiftReg));
1319 Inst.addOperand(MCOperand::CreateImm(
1320 ARM_AM::getSORegOpc(RegShiftedReg.ShiftTy, RegShiftedReg.ShiftImm)));
1323 void addRegShiftedImmOperands(MCInst &Inst, unsigned N) const {
1324 assert(N == 2 && "Invalid number of operands!");
1325 assert(isRegShiftedImm() &&
1326 "addRegShiftedImmOperands() on non RegShiftedImm!");
1327 Inst.addOperand(MCOperand::CreateReg(RegShiftedImm.SrcReg));
1328 Inst.addOperand(MCOperand::CreateImm(
1329 ARM_AM::getSORegOpc(RegShiftedImm.ShiftTy, RegShiftedImm.ShiftImm)));
1332 void addShifterImmOperands(MCInst &Inst, unsigned N) const {
1333 assert(N == 1 && "Invalid number of operands!");
1334 Inst.addOperand(MCOperand::CreateImm((ShifterImm.isASR << 5) |
1338 void addRegListOperands(MCInst &Inst, unsigned N) const {
1339 assert(N == 1 && "Invalid number of operands!");
1340 const SmallVectorImpl<unsigned> &RegList = getRegList();
1341 for (SmallVectorImpl<unsigned>::const_iterator
1342 I = RegList.begin(), E = RegList.end(); I != E; ++I)
1343 Inst.addOperand(MCOperand::CreateReg(*I));
1346 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
1347 addRegListOperands(Inst, N);
1350 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
1351 addRegListOperands(Inst, N);
1354 void addRotImmOperands(MCInst &Inst, unsigned N) const {
1355 assert(N == 1 && "Invalid number of operands!");
1356 // Encoded as val>>3. The printer handles display as 8, 16, 24.
1357 Inst.addOperand(MCOperand::CreateImm(RotImm.Imm >> 3));
1360 void addBitfieldOperands(MCInst &Inst, unsigned N) const {
1361 assert(N == 1 && "Invalid number of operands!");
1362 // Munge the lsb/width into a bitfield mask.
1363 unsigned lsb = Bitfield.LSB;
1364 unsigned width = Bitfield.Width;
1365 // Make a 32-bit mask w/ the referenced bits clear and all other bits set.
1366 uint32_t Mask = ~(((uint32_t)0xffffffff >> lsb) << (32 - width) >>
1367 (32 - (lsb + width)));
1368 Inst.addOperand(MCOperand::CreateImm(Mask));
1371 void addImmOperands(MCInst &Inst, unsigned N) const {
1372 assert(N == 1 && "Invalid number of operands!");
1373 addExpr(Inst, getImm());
1376 void addFBits16Operands(MCInst &Inst, unsigned N) const {
1377 assert(N == 1 && "Invalid number of operands!");
1378 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1379 Inst.addOperand(MCOperand::CreateImm(16 - CE->getValue()));
1382 void addFBits32Operands(MCInst &Inst, unsigned N) const {
1383 assert(N == 1 && "Invalid number of operands!");
1384 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1385 Inst.addOperand(MCOperand::CreateImm(32 - CE->getValue()));
1388 void addFPImmOperands(MCInst &Inst, unsigned N) const {
1389 assert(N == 1 && "Invalid number of operands!");
1390 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1391 int Val = ARM_AM::getFP32Imm(APInt(32, CE->getValue()));
1392 Inst.addOperand(MCOperand::CreateImm(Val));
1395 void addImm8s4Operands(MCInst &Inst, unsigned N) const {
1396 assert(N == 1 && "Invalid number of operands!");
1397 // FIXME: We really want to scale the value here, but the LDRD/STRD
1398 // instruction don't encode operands that way yet.
1399 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1400 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
1403 void addImm0_1020s4Operands(MCInst &Inst, unsigned N) const {
1404 assert(N == 1 && "Invalid number of operands!");
1405 // The immediate is scaled by four in the encoding and is stored
1406 // in the MCInst as such. Lop off the low two bits here.
1407 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1408 Inst.addOperand(MCOperand::CreateImm(CE->getValue() / 4));
1411 void addImm0_508s4Operands(MCInst &Inst, unsigned N) const {
1412 assert(N == 1 && "Invalid number of operands!");
1413 // The immediate is scaled by four in the encoding and is stored
1414 // in the MCInst as such. Lop off the low two bits here.
1415 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1416 Inst.addOperand(MCOperand::CreateImm(CE->getValue() / 4));
1419 void addImm1_16Operands(MCInst &Inst, unsigned N) const {
1420 assert(N == 1 && "Invalid number of operands!");
1421 // The constant encodes as the immediate-1, and we store in the instruction
1422 // the bits as encoded, so subtract off one here.
1423 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1424 Inst.addOperand(MCOperand::CreateImm(CE->getValue() - 1));
1427 void addImm1_32Operands(MCInst &Inst, unsigned N) const {
1428 assert(N == 1 && "Invalid number of operands!");
1429 // The constant encodes as the immediate-1, and we store in the instruction
1430 // the bits as encoded, so subtract off one here.
1431 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1432 Inst.addOperand(MCOperand::CreateImm(CE->getValue() - 1));
1435 void addImmThumbSROperands(MCInst &Inst, unsigned N) const {
1436 assert(N == 1 && "Invalid number of operands!");
1437 // The constant encodes as the immediate, except for 32, which encodes as
1439 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1440 unsigned Imm = CE->getValue();
1441 Inst.addOperand(MCOperand::CreateImm((Imm == 32 ? 0 : Imm)));
1444 void addPKHASRImmOperands(MCInst &Inst, unsigned N) const {
1445 assert(N == 1 && "Invalid number of operands!");
1446 // An ASR value of 32 encodes as 0, so that's how we want to add it to
1447 // the instruction as well.
1448 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1449 int Val = CE->getValue();
1450 Inst.addOperand(MCOperand::CreateImm(Val == 32 ? 0 : Val));
1453 void addT2SOImmNotOperands(MCInst &Inst, unsigned N) const {
1454 assert(N == 1 && "Invalid number of operands!");
1455 // The operand is actually a t2_so_imm, but we have its bitwise
1456 // negation in the assembly source, so twiddle it here.
1457 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1458 Inst.addOperand(MCOperand::CreateImm(~CE->getValue()));
1461 void addT2SOImmNegOperands(MCInst &Inst, unsigned N) const {
1462 assert(N == 1 && "Invalid number of operands!");
1463 // The operand is actually a t2_so_imm, but we have its
1464 // negation in the assembly source, so twiddle it here.
1465 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1466 Inst.addOperand(MCOperand::CreateImm(-CE->getValue()));
1469 void addARMSOImmNotOperands(MCInst &Inst, unsigned N) const {
1470 assert(N == 1 && "Invalid number of operands!");
1471 // The operand is actually a so_imm, but we have its bitwise
1472 // negation in the assembly source, so twiddle it here.
1473 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1474 Inst.addOperand(MCOperand::CreateImm(~CE->getValue()));
1477 void addARMSOImmNegOperands(MCInst &Inst, unsigned N) const {
1478 assert(N == 1 && "Invalid number of operands!");
1479 // The operand is actually a so_imm, but we have its
1480 // negation in the assembly source, so twiddle it here.
1481 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1482 Inst.addOperand(MCOperand::CreateImm(-CE->getValue()));
1485 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
1486 assert(N == 1 && "Invalid number of operands!");
1487 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
1490 void addMemNoOffsetOperands(MCInst &Inst, unsigned N) const {
1491 assert(N == 1 && "Invalid number of operands!");
1492 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1495 void addMemPCRelImm12Operands(MCInst &Inst, unsigned N) const {
1496 assert(N == 1 && "Invalid number of operands!");
1497 int32_t Imm = Memory.OffsetImm->getValue();
1498 // FIXME: Handle #-0
1499 if (Imm == INT32_MIN) Imm = 0;
1500 Inst.addOperand(MCOperand::CreateImm(Imm));
1503 void addAlignedMemoryOperands(MCInst &Inst, unsigned N) const {
1504 assert(N == 2 && "Invalid number of operands!");
1505 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1506 Inst.addOperand(MCOperand::CreateImm(Memory.Alignment));
1509 void addAddrMode2Operands(MCInst &Inst, unsigned N) const {
1510 assert(N == 3 && "Invalid number of operands!");
1511 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1512 if (!Memory.OffsetRegNum) {
1513 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1514 // Special case for #-0
1515 if (Val == INT32_MIN) Val = 0;
1516 if (Val < 0) Val = -Val;
1517 Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
1519 // For register offset, we encode the shift type and negation flag
1521 Val = ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
1522 Memory.ShiftImm, Memory.ShiftType);
1524 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1525 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1526 Inst.addOperand(MCOperand::CreateImm(Val));
1529 void addAM2OffsetImmOperands(MCInst &Inst, unsigned N) const {
1530 assert(N == 2 && "Invalid number of operands!");
1531 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1532 assert(CE && "non-constant AM2OffsetImm operand!");
1533 int32_t Val = CE->getValue();
1534 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1535 // Special case for #-0
1536 if (Val == INT32_MIN) Val = 0;
1537 if (Val < 0) Val = -Val;
1538 Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
1539 Inst.addOperand(MCOperand::CreateReg(0));
1540 Inst.addOperand(MCOperand::CreateImm(Val));
1543 void addAddrMode3Operands(MCInst &Inst, unsigned N) const {
1544 assert(N == 3 && "Invalid number of operands!");
1545 // If we have an immediate that's not a constant, treat it as a label
1546 // reference needing a fixup. If it is a constant, it's something else
1547 // and we reject it.
1549 Inst.addOperand(MCOperand::CreateExpr(getImm()));
1550 Inst.addOperand(MCOperand::CreateReg(0));
1551 Inst.addOperand(MCOperand::CreateImm(0));
1555 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1556 if (!Memory.OffsetRegNum) {
1557 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1558 // Special case for #-0
1559 if (Val == INT32_MIN) Val = 0;
1560 if (Val < 0) Val = -Val;
1561 Val = ARM_AM::getAM3Opc(AddSub, Val);
1563 // For register offset, we encode the shift type and negation flag
1565 Val = ARM_AM::getAM3Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add, 0);
1567 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1568 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1569 Inst.addOperand(MCOperand::CreateImm(Val));
1572 void addAM3OffsetOperands(MCInst &Inst, unsigned N) const {
1573 assert(N == 2 && "Invalid number of operands!");
1574 if (Kind == k_PostIndexRegister) {
1576 ARM_AM::getAM3Opc(PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub, 0);
1577 Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1578 Inst.addOperand(MCOperand::CreateImm(Val));
1583 const MCConstantExpr *CE = static_cast<const MCConstantExpr*>(getImm());
1584 int32_t Val = CE->getValue();
1585 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1586 // Special case for #-0
1587 if (Val == INT32_MIN) Val = 0;
1588 if (Val < 0) Val = -Val;
1589 Val = ARM_AM::getAM3Opc(AddSub, Val);
1590 Inst.addOperand(MCOperand::CreateReg(0));
1591 Inst.addOperand(MCOperand::CreateImm(Val));
1594 void addAddrMode5Operands(MCInst &Inst, unsigned N) const {
1595 assert(N == 2 && "Invalid number of operands!");
1596 // If we have an immediate that's not a constant, treat it as a label
1597 // reference needing a fixup. If it is a constant, it's something else
1598 // and we reject it.
1600 Inst.addOperand(MCOperand::CreateExpr(getImm()));
1601 Inst.addOperand(MCOperand::CreateImm(0));
1605 // The lower two bits are always zero and as such are not encoded.
1606 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
1607 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1608 // Special case for #-0
1609 if (Val == INT32_MIN) Val = 0;
1610 if (Val < 0) Val = -Val;
1611 Val = ARM_AM::getAM5Opc(AddSub, Val);
1612 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1613 Inst.addOperand(MCOperand::CreateImm(Val));
1616 void addMemImm8s4OffsetOperands(MCInst &Inst, unsigned N) const {
1617 assert(N == 2 && "Invalid number of operands!");
1618 // If we have an immediate that's not a constant, treat it as a label
1619 // reference needing a fixup. If it is a constant, it's something else
1620 // and we reject it.
1622 Inst.addOperand(MCOperand::CreateExpr(getImm()));
1623 Inst.addOperand(MCOperand::CreateImm(0));
1627 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1628 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1629 Inst.addOperand(MCOperand::CreateImm(Val));
1632 void addMemImm0_1020s4OffsetOperands(MCInst &Inst, unsigned N) const {
1633 assert(N == 2 && "Invalid number of operands!");
1634 // The lower two bits are always zero and as such are not encoded.
1635 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
1636 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1637 Inst.addOperand(MCOperand::CreateImm(Val));
1640 void addMemImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1641 assert(N == 2 && "Invalid number of operands!");
1642 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1643 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1644 Inst.addOperand(MCOperand::CreateImm(Val));
1647 void addMemPosImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1648 addMemImm8OffsetOperands(Inst, N);
1651 void addMemNegImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1652 addMemImm8OffsetOperands(Inst, N);
1655 void addMemUImm12OffsetOperands(MCInst &Inst, unsigned N) const {
1656 assert(N == 2 && "Invalid number of operands!");
1657 // If this is an immediate, it's a label reference.
1659 addExpr(Inst, getImm());
1660 Inst.addOperand(MCOperand::CreateImm(0));
1664 // Otherwise, it's a normal memory reg+offset.
1665 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1666 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1667 Inst.addOperand(MCOperand::CreateImm(Val));
1670 void addMemImm12OffsetOperands(MCInst &Inst, unsigned N) const {
1671 assert(N == 2 && "Invalid number of operands!");
1672 // If this is an immediate, it's a label reference.
1674 addExpr(Inst, getImm());
1675 Inst.addOperand(MCOperand::CreateImm(0));
1679 // Otherwise, it's a normal memory reg+offset.
1680 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1681 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1682 Inst.addOperand(MCOperand::CreateImm(Val));
1685 void addMemTBBOperands(MCInst &Inst, unsigned N) const {
1686 assert(N == 2 && "Invalid number of operands!");
1687 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1688 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1691 void addMemTBHOperands(MCInst &Inst, unsigned N) const {
1692 assert(N == 2 && "Invalid number of operands!");
1693 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1694 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1697 void addMemRegOffsetOperands(MCInst &Inst, unsigned N) const {
1698 assert(N == 3 && "Invalid number of operands!");
1700 ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
1701 Memory.ShiftImm, Memory.ShiftType);
1702 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1703 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1704 Inst.addOperand(MCOperand::CreateImm(Val));
1707 void addT2MemRegOffsetOperands(MCInst &Inst, unsigned N) const {
1708 assert(N == 3 && "Invalid number of operands!");
1709 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1710 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1711 Inst.addOperand(MCOperand::CreateImm(Memory.ShiftImm));
1714 void addMemThumbRROperands(MCInst &Inst, unsigned N) const {
1715 assert(N == 2 && "Invalid number of operands!");
1716 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1717 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1720 void addMemThumbRIs4Operands(MCInst &Inst, unsigned N) const {
1721 assert(N == 2 && "Invalid number of operands!");
1722 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
1723 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1724 Inst.addOperand(MCOperand::CreateImm(Val));
1727 void addMemThumbRIs2Operands(MCInst &Inst, unsigned N) const {
1728 assert(N == 2 && "Invalid number of operands!");
1729 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 2) : 0;
1730 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1731 Inst.addOperand(MCOperand::CreateImm(Val));
1734 void addMemThumbRIs1Operands(MCInst &Inst, unsigned N) const {
1735 assert(N == 2 && "Invalid number of operands!");
1736 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue()) : 0;
1737 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1738 Inst.addOperand(MCOperand::CreateImm(Val));
1741 void addMemThumbSPIOperands(MCInst &Inst, unsigned N) const {
1742 assert(N == 2 && "Invalid number of operands!");
1743 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
1744 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1745 Inst.addOperand(MCOperand::CreateImm(Val));
1748 void addPostIdxImm8Operands(MCInst &Inst, unsigned N) const {
1749 assert(N == 1 && "Invalid number of operands!");
1750 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1751 assert(CE && "non-constant post-idx-imm8 operand!");
1752 int Imm = CE->getValue();
1753 bool isAdd = Imm >= 0;
1754 if (Imm == INT32_MIN) Imm = 0;
1755 Imm = (Imm < 0 ? -Imm : Imm) | (int)isAdd << 8;
1756 Inst.addOperand(MCOperand::CreateImm(Imm));
1759 void addPostIdxImm8s4Operands(MCInst &Inst, unsigned N) const {
1760 assert(N == 1 && "Invalid number of operands!");
1761 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1762 assert(CE && "non-constant post-idx-imm8s4 operand!");
1763 int Imm = CE->getValue();
1764 bool isAdd = Imm >= 0;
1765 if (Imm == INT32_MIN) Imm = 0;
1766 // Immediate is scaled by 4.
1767 Imm = ((Imm < 0 ? -Imm : Imm) / 4) | (int)isAdd << 8;
1768 Inst.addOperand(MCOperand::CreateImm(Imm));
1771 void addPostIdxRegOperands(MCInst &Inst, unsigned N) const {
1772 assert(N == 2 && "Invalid number of operands!");
1773 Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1774 Inst.addOperand(MCOperand::CreateImm(PostIdxReg.isAdd));
1777 void addPostIdxRegShiftedOperands(MCInst &Inst, unsigned N) const {
1778 assert(N == 2 && "Invalid number of operands!");
1779 Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1780 // The sign, shift type, and shift amount are encoded in a single operand
1781 // using the AM2 encoding helpers.
1782 ARM_AM::AddrOpc opc = PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub;
1783 unsigned Imm = ARM_AM::getAM2Opc(opc, PostIdxReg.ShiftImm,
1784 PostIdxReg.ShiftTy);
1785 Inst.addOperand(MCOperand::CreateImm(Imm));
1788 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
1789 assert(N == 1 && "Invalid number of operands!");
1790 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
1793 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
1794 assert(N == 1 && "Invalid number of operands!");
1795 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
1798 void addVecListOperands(MCInst &Inst, unsigned N) const {
1799 assert(N == 1 && "Invalid number of operands!");
1800 Inst.addOperand(MCOperand::CreateReg(VectorList.RegNum));
1803 void addVecListIndexedOperands(MCInst &Inst, unsigned N) const {
1804 assert(N == 2 && "Invalid number of operands!");
1805 Inst.addOperand(MCOperand::CreateReg(VectorList.RegNum));
1806 Inst.addOperand(MCOperand::CreateImm(VectorList.LaneIndex));
1809 void addVectorIndex8Operands(MCInst &Inst, unsigned N) const {
1810 assert(N == 1 && "Invalid number of operands!");
1811 Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1814 void addVectorIndex16Operands(MCInst &Inst, unsigned N) const {
1815 assert(N == 1 && "Invalid number of operands!");
1816 Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1819 void addVectorIndex32Operands(MCInst &Inst, unsigned N) const {
1820 assert(N == 1 && "Invalid number of operands!");
1821 Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1824 void addNEONi8splatOperands(MCInst &Inst, unsigned N) const {
1825 assert(N == 1 && "Invalid number of operands!");
1826 // The immediate encodes the type of constant as well as the value.
1827 // Mask in that this is an i8 splat.
1828 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1829 Inst.addOperand(MCOperand::CreateImm(CE->getValue() | 0xe00));
1832 void addNEONi16splatOperands(MCInst &Inst, unsigned N) const {
1833 assert(N == 1 && "Invalid number of operands!");
1834 // The immediate encodes the type of constant as well as the value.
1835 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1836 unsigned Value = CE->getValue();
1838 Value = (Value >> 8) | 0xa00;
1841 Inst.addOperand(MCOperand::CreateImm(Value));
1844 void addNEONi32splatOperands(MCInst &Inst, unsigned N) const {
1845 assert(N == 1 && "Invalid number of operands!");
1846 // The immediate encodes the type of constant as well as the value.
1847 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1848 unsigned Value = CE->getValue();
1849 if (Value >= 256 && Value <= 0xff00)
1850 Value = (Value >> 8) | 0x200;
1851 else if (Value > 0xffff && Value <= 0xff0000)
1852 Value = (Value >> 16) | 0x400;
1853 else if (Value > 0xffffff)
1854 Value = (Value >> 24) | 0x600;
1855 Inst.addOperand(MCOperand::CreateImm(Value));
1858 void addNEONi32vmovOperands(MCInst &Inst, unsigned N) const {
1859 assert(N == 1 && "Invalid number of operands!");
1860 // The immediate encodes the type of constant as well as the value.
1861 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1862 unsigned Value = CE->getValue();
1863 if (Value >= 256 && Value <= 0xffff)
1864 Value = (Value >> 8) | ((Value & 0xff) ? 0xc00 : 0x200);
1865 else if (Value > 0xffff && Value <= 0xffffff)
1866 Value = (Value >> 16) | ((Value & 0xff) ? 0xd00 : 0x400);
1867 else if (Value > 0xffffff)
1868 Value = (Value >> 24) | 0x600;
1869 Inst.addOperand(MCOperand::CreateImm(Value));
1872 void addNEONi32vmovNegOperands(MCInst &Inst, unsigned N) const {
1873 assert(N == 1 && "Invalid number of operands!");
1874 // The immediate encodes the type of constant as well as the value.
1875 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1876 unsigned Value = ~CE->getValue();
1877 if (Value >= 256 && Value <= 0xffff)
1878 Value = (Value >> 8) | ((Value & 0xff) ? 0xc00 : 0x200);
1879 else if (Value > 0xffff && Value <= 0xffffff)
1880 Value = (Value >> 16) | ((Value & 0xff) ? 0xd00 : 0x400);
1881 else if (Value > 0xffffff)
1882 Value = (Value >> 24) | 0x600;
1883 Inst.addOperand(MCOperand::CreateImm(Value));
1886 void addNEONi64splatOperands(MCInst &Inst, unsigned N) const {
1887 assert(N == 1 && "Invalid number of operands!");
1888 // The immediate encodes the type of constant as well as the value.
1889 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1890 uint64_t Value = CE->getValue();
1892 for (unsigned i = 0; i < 8; ++i, Value >>= 8) {
1893 Imm |= (Value & 1) << i;
1895 Inst.addOperand(MCOperand::CreateImm(Imm | 0x1e00));
1898 virtual void print(raw_ostream &OS) const;
1900 static ARMOperand *CreateITMask(unsigned Mask, SMLoc S) {
1901 ARMOperand *Op = new ARMOperand(k_ITCondMask);
1902 Op->ITMask.Mask = Mask;
1908 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
1909 ARMOperand *Op = new ARMOperand(k_CondCode);
1916 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
1917 ARMOperand *Op = new ARMOperand(k_CoprocNum);
1918 Op->Cop.Val = CopVal;
1924 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
1925 ARMOperand *Op = new ARMOperand(k_CoprocReg);
1926 Op->Cop.Val = CopVal;
1932 static ARMOperand *CreateCoprocOption(unsigned Val, SMLoc S, SMLoc E) {
1933 ARMOperand *Op = new ARMOperand(k_CoprocOption);
1940 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
1941 ARMOperand *Op = new ARMOperand(k_CCOut);
1942 Op->Reg.RegNum = RegNum;
1948 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
1949 ARMOperand *Op = new ARMOperand(k_Token);
1950 Op->Tok.Data = Str.data();
1951 Op->Tok.Length = Str.size();
1957 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
1958 ARMOperand *Op = new ARMOperand(k_Register);
1959 Op->Reg.RegNum = RegNum;
1965 static ARMOperand *CreateShiftedRegister(ARM_AM::ShiftOpc ShTy,
1970 ARMOperand *Op = new ARMOperand(k_ShiftedRegister);
1971 Op->RegShiftedReg.ShiftTy = ShTy;
1972 Op->RegShiftedReg.SrcReg = SrcReg;
1973 Op->RegShiftedReg.ShiftReg = ShiftReg;
1974 Op->RegShiftedReg.ShiftImm = ShiftImm;
1980 static ARMOperand *CreateShiftedImmediate(ARM_AM::ShiftOpc ShTy,
1984 ARMOperand *Op = new ARMOperand(k_ShiftedImmediate);
1985 Op->RegShiftedImm.ShiftTy = ShTy;
1986 Op->RegShiftedImm.SrcReg = SrcReg;
1987 Op->RegShiftedImm.ShiftImm = ShiftImm;
1993 static ARMOperand *CreateShifterImm(bool isASR, unsigned Imm,
1995 ARMOperand *Op = new ARMOperand(k_ShifterImmediate);
1996 Op->ShifterImm.isASR = isASR;
1997 Op->ShifterImm.Imm = Imm;
2003 static ARMOperand *CreateRotImm(unsigned Imm, SMLoc S, SMLoc E) {
2004 ARMOperand *Op = new ARMOperand(k_RotateImmediate);
2005 Op->RotImm.Imm = Imm;
2011 static ARMOperand *CreateBitfield(unsigned LSB, unsigned Width,
2013 ARMOperand *Op = new ARMOperand(k_BitfieldDescriptor);
2014 Op->Bitfield.LSB = LSB;
2015 Op->Bitfield.Width = Width;
2022 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
2023 SMLoc StartLoc, SMLoc EndLoc) {
2024 KindTy Kind = k_RegisterList;
2026 if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Regs.front().first))
2027 Kind = k_DPRRegisterList;
2028 else if (ARMMCRegisterClasses[ARM::SPRRegClassID].
2029 contains(Regs.front().first))
2030 Kind = k_SPRRegisterList;
2032 ARMOperand *Op = new ARMOperand(Kind);
2033 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
2034 I = Regs.begin(), E = Regs.end(); I != E; ++I)
2035 Op->Registers.push_back(I->first);
2036 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
2037 Op->StartLoc = StartLoc;
2038 Op->EndLoc = EndLoc;
2042 static ARMOperand *CreateVectorList(unsigned RegNum, unsigned Count,
2043 bool isDoubleSpaced, SMLoc S, SMLoc E) {
2044 ARMOperand *Op = new ARMOperand(k_VectorList);
2045 Op->VectorList.RegNum = RegNum;
2046 Op->VectorList.Count = Count;
2047 Op->VectorList.isDoubleSpaced = isDoubleSpaced;
2053 static ARMOperand *CreateVectorListAllLanes(unsigned RegNum, unsigned Count,
2054 bool isDoubleSpaced,
2056 ARMOperand *Op = new ARMOperand(k_VectorListAllLanes);
2057 Op->VectorList.RegNum = RegNum;
2058 Op->VectorList.Count = Count;
2059 Op->VectorList.isDoubleSpaced = isDoubleSpaced;
2065 static ARMOperand *CreateVectorListIndexed(unsigned RegNum, unsigned Count,
2067 bool isDoubleSpaced,
2069 ARMOperand *Op = new ARMOperand(k_VectorListIndexed);
2070 Op->VectorList.RegNum = RegNum;
2071 Op->VectorList.Count = Count;
2072 Op->VectorList.LaneIndex = Index;
2073 Op->VectorList.isDoubleSpaced = isDoubleSpaced;
2079 static ARMOperand *CreateVectorIndex(unsigned Idx, SMLoc S, SMLoc E,
2081 ARMOperand *Op = new ARMOperand(k_VectorIndex);
2082 Op->VectorIndex.Val = Idx;
2088 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
2089 ARMOperand *Op = new ARMOperand(k_Immediate);
2096 static ARMOperand *CreateMem(unsigned BaseRegNum,
2097 const MCConstantExpr *OffsetImm,
2098 unsigned OffsetRegNum,
2099 ARM_AM::ShiftOpc ShiftType,
2104 ARMOperand *Op = new ARMOperand(k_Memory);
2105 Op->Memory.BaseRegNum = BaseRegNum;
2106 Op->Memory.OffsetImm = OffsetImm;
2107 Op->Memory.OffsetRegNum = OffsetRegNum;
2108 Op->Memory.ShiftType = ShiftType;
2109 Op->Memory.ShiftImm = ShiftImm;
2110 Op->Memory.Alignment = Alignment;
2111 Op->Memory.isNegative = isNegative;
2117 static ARMOperand *CreatePostIdxReg(unsigned RegNum, bool isAdd,
2118 ARM_AM::ShiftOpc ShiftTy,
2121 ARMOperand *Op = new ARMOperand(k_PostIndexRegister);
2122 Op->PostIdxReg.RegNum = RegNum;
2123 Op->PostIdxReg.isAdd = isAdd;
2124 Op->PostIdxReg.ShiftTy = ShiftTy;
2125 Op->PostIdxReg.ShiftImm = ShiftImm;
2131 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
2132 ARMOperand *Op = new ARMOperand(k_MemBarrierOpt);
2133 Op->MBOpt.Val = Opt;
2139 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
2140 ARMOperand *Op = new ARMOperand(k_ProcIFlags);
2141 Op->IFlags.Val = IFlags;
2147 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
2148 ARMOperand *Op = new ARMOperand(k_MSRMask);
2149 Op->MMask.Val = MMask;
2156 } // end anonymous namespace.
2158 void ARMOperand::print(raw_ostream &OS) const {
2161 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
2164 OS << "<ccout " << getReg() << ">";
2166 case k_ITCondMask: {
2167 static const char *MaskStr[] = {
2168 "()", "(t)", "(e)", "(tt)", "(et)", "(te)", "(ee)", "(ttt)", "(ett)",
2169 "(tet)", "(eet)", "(tte)", "(ete)", "(tee)", "(eee)"
2171 assert((ITMask.Mask & 0xf) == ITMask.Mask);
2172 OS << "<it-mask " << MaskStr[ITMask.Mask] << ">";
2176 OS << "<coprocessor number: " << getCoproc() << ">";
2179 OS << "<coprocessor register: " << getCoproc() << ">";
2181 case k_CoprocOption:
2182 OS << "<coprocessor option: " << CoprocOption.Val << ">";
2185 OS << "<mask: " << getMSRMask() << ">";
2188 getImm()->print(OS);
2190 case k_MemBarrierOpt:
2191 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
2195 << " base:" << Memory.BaseRegNum;
2198 case k_PostIndexRegister:
2199 OS << "post-idx register " << (PostIdxReg.isAdd ? "" : "-")
2200 << PostIdxReg.RegNum;
2201 if (PostIdxReg.ShiftTy != ARM_AM::no_shift)
2202 OS << ARM_AM::getShiftOpcStr(PostIdxReg.ShiftTy) << " "
2203 << PostIdxReg.ShiftImm;
2206 case k_ProcIFlags: {
2207 OS << "<ARM_PROC::";
2208 unsigned IFlags = getProcIFlags();
2209 for (int i=2; i >= 0; --i)
2210 if (IFlags & (1 << i))
2211 OS << ARM_PROC::IFlagsToString(1 << i);
2216 OS << "<register " << getReg() << ">";
2218 case k_ShifterImmediate:
2219 OS << "<shift " << (ShifterImm.isASR ? "asr" : "lsl")
2220 << " #" << ShifterImm.Imm << ">";
2222 case k_ShiftedRegister:
2223 OS << "<so_reg_reg "
2224 << RegShiftedReg.SrcReg << " "
2225 << ARM_AM::getShiftOpcStr(RegShiftedReg.ShiftTy)
2226 << " " << RegShiftedReg.ShiftReg << ">";
2228 case k_ShiftedImmediate:
2229 OS << "<so_reg_imm "
2230 << RegShiftedImm.SrcReg << " "
2231 << ARM_AM::getShiftOpcStr(RegShiftedImm.ShiftTy)
2232 << " #" << RegShiftedImm.ShiftImm << ">";
2234 case k_RotateImmediate:
2235 OS << "<ror " << " #" << (RotImm.Imm * 8) << ">";
2237 case k_BitfieldDescriptor:
2238 OS << "<bitfield " << "lsb: " << Bitfield.LSB
2239 << ", width: " << Bitfield.Width << ">";
2241 case k_RegisterList:
2242 case k_DPRRegisterList:
2243 case k_SPRRegisterList: {
2244 OS << "<register_list ";
2246 const SmallVectorImpl<unsigned> &RegList = getRegList();
2247 for (SmallVectorImpl<unsigned>::const_iterator
2248 I = RegList.begin(), E = RegList.end(); I != E; ) {
2250 if (++I < E) OS << ", ";
2257 OS << "<vector_list " << VectorList.Count << " * "
2258 << VectorList.RegNum << ">";
2260 case k_VectorListAllLanes:
2261 OS << "<vector_list(all lanes) " << VectorList.Count << " * "
2262 << VectorList.RegNum << ">";
2264 case k_VectorListIndexed:
2265 OS << "<vector_list(lane " << VectorList.LaneIndex << ") "
2266 << VectorList.Count << " * " << VectorList.RegNum << ">";
2269 OS << "'" << getToken() << "'";
2272 OS << "<vectorindex " << getVectorIndex() << ">";
2277 /// @name Auto-generated Match Functions
2280 static unsigned MatchRegisterName(StringRef Name);
2284 bool ARMAsmParser::ParseRegister(unsigned &RegNo,
2285 SMLoc &StartLoc, SMLoc &EndLoc) {
2286 StartLoc = Parser.getTok().getLoc();
2287 RegNo = tryParseRegister();
2288 EndLoc = Parser.getTok().getLoc();
2290 return (RegNo == (unsigned)-1);
2293 /// Try to parse a register name. The token must be an Identifier when called,
2294 /// and if it is a register name the token is eaten and the register number is
2295 /// returned. Otherwise return -1.
2297 int ARMAsmParser::tryParseRegister() {
2298 const AsmToken &Tok = Parser.getTok();
2299 if (Tok.isNot(AsmToken::Identifier)) return -1;
2301 std::string lowerCase = Tok.getString().lower();
2302 unsigned RegNum = MatchRegisterName(lowerCase);
2304 RegNum = StringSwitch<unsigned>(lowerCase)
2305 .Case("r13", ARM::SP)
2306 .Case("r14", ARM::LR)
2307 .Case("r15", ARM::PC)
2308 .Case("ip", ARM::R12)
2309 // Additional register name aliases for 'gas' compatibility.
2310 .Case("a1", ARM::R0)
2311 .Case("a2", ARM::R1)
2312 .Case("a3", ARM::R2)
2313 .Case("a4", ARM::R3)
2314 .Case("v1", ARM::R4)
2315 .Case("v2", ARM::R5)
2316 .Case("v3", ARM::R6)
2317 .Case("v4", ARM::R7)
2318 .Case("v5", ARM::R8)
2319 .Case("v6", ARM::R9)
2320 .Case("v7", ARM::R10)
2321 .Case("v8", ARM::R11)
2322 .Case("sb", ARM::R9)
2323 .Case("sl", ARM::R10)
2324 .Case("fp", ARM::R11)
2328 // Check for aliases registered via .req. Canonicalize to lower case.
2329 // That's more consistent since register names are case insensitive, and
2330 // it's how the original entry was passed in from MC/MCParser/AsmParser.
2331 StringMap<unsigned>::const_iterator Entry = RegisterReqs.find(lowerCase);
2332 // If no match, return failure.
2333 if (Entry == RegisterReqs.end())
2335 Parser.Lex(); // Eat identifier token.
2336 return Entry->getValue();
2339 Parser.Lex(); // Eat identifier token.
2344 // Try to parse a shifter (e.g., "lsl <amt>"). On success, return 0.
2345 // If a recoverable error occurs, return 1. If an irrecoverable error
2346 // occurs, return -1. An irrecoverable error is one where tokens have been
2347 // consumed in the process of trying to parse the shifter (i.e., when it is
2348 // indeed a shifter operand, but malformed).
2349 int ARMAsmParser::tryParseShiftRegister(
2350 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2351 SMLoc S = Parser.getTok().getLoc();
2352 const AsmToken &Tok = Parser.getTok();
2353 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2355 std::string lowerCase = Tok.getString().lower();
2356 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
2357 .Case("asl", ARM_AM::lsl)
2358 .Case("lsl", ARM_AM::lsl)
2359 .Case("lsr", ARM_AM::lsr)
2360 .Case("asr", ARM_AM::asr)
2361 .Case("ror", ARM_AM::ror)
2362 .Case("rrx", ARM_AM::rrx)
2363 .Default(ARM_AM::no_shift);
2365 if (ShiftTy == ARM_AM::no_shift)
2368 Parser.Lex(); // Eat the operator.
2370 // The source register for the shift has already been added to the
2371 // operand list, so we need to pop it off and combine it into the shifted
2372 // register operand instead.
2373 OwningPtr<ARMOperand> PrevOp((ARMOperand*)Operands.pop_back_val());
2374 if (!PrevOp->isReg())
2375 return Error(PrevOp->getStartLoc(), "shift must be of a register");
2376 int SrcReg = PrevOp->getReg();
2379 if (ShiftTy == ARM_AM::rrx) {
2380 // RRX Doesn't have an explicit shift amount. The encoder expects
2381 // the shift register to be the same as the source register. Seems odd,
2385 // Figure out if this is shifted by a constant or a register (for non-RRX).
2386 if (Parser.getTok().is(AsmToken::Hash) ||
2387 Parser.getTok().is(AsmToken::Dollar)) {
2388 Parser.Lex(); // Eat hash.
2389 SMLoc ImmLoc = Parser.getTok().getLoc();
2390 const MCExpr *ShiftExpr = 0;
2391 if (getParser().ParseExpression(ShiftExpr)) {
2392 Error(ImmLoc, "invalid immediate shift value");
2395 // The expression must be evaluatable as an immediate.
2396 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
2398 Error(ImmLoc, "invalid immediate shift value");
2401 // Range check the immediate.
2402 // lsl, ror: 0 <= imm <= 31
2403 // lsr, asr: 0 <= imm <= 32
2404 Imm = CE->getValue();
2406 ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
2407 ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
2408 Error(ImmLoc, "immediate shift value out of range");
2411 // shift by zero is a nop. Always send it through as lsl.
2412 // ('as' compatibility)
2414 ShiftTy = ARM_AM::lsl;
2415 } else if (Parser.getTok().is(AsmToken::Identifier)) {
2416 ShiftReg = tryParseRegister();
2417 SMLoc L = Parser.getTok().getLoc();
2418 if (ShiftReg == -1) {
2419 Error (L, "expected immediate or register in shift operand");
2423 Error (Parser.getTok().getLoc(),
2424 "expected immediate or register in shift operand");
2429 if (ShiftReg && ShiftTy != ARM_AM::rrx)
2430 Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
2432 S, Parser.getTok().getLoc()));
2434 Operands.push_back(ARMOperand::CreateShiftedImmediate(ShiftTy, SrcReg, Imm,
2435 S, Parser.getTok().getLoc()));
2441 /// Try to parse a register name. The token must be an Identifier when called.
2442 /// If it's a register, an AsmOperand is created. Another AsmOperand is created
2443 /// if there is a "writeback". 'true' if it's not a register.
2445 /// TODO this is likely to change to allow different register types and or to
2446 /// parse for a specific register type.
2448 tryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2449 SMLoc S = Parser.getTok().getLoc();
2450 int RegNo = tryParseRegister();
2454 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
2456 const AsmToken &ExclaimTok = Parser.getTok();
2457 if (ExclaimTok.is(AsmToken::Exclaim)) {
2458 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
2459 ExclaimTok.getLoc()));
2460 Parser.Lex(); // Eat exclaim token
2464 // Also check for an index operand. This is only legal for vector registers,
2465 // but that'll get caught OK in operand matching, so we don't need to
2466 // explicitly filter everything else out here.
2467 if (Parser.getTok().is(AsmToken::LBrac)) {
2468 SMLoc SIdx = Parser.getTok().getLoc();
2469 Parser.Lex(); // Eat left bracket token.
2471 const MCExpr *ImmVal;
2472 if (getParser().ParseExpression(ImmVal))
2473 return MatchOperand_ParseFail;
2474 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(ImmVal);
2476 TokError("immediate value expected for vector index");
2477 return MatchOperand_ParseFail;
2480 SMLoc E = Parser.getTok().getLoc();
2481 if (Parser.getTok().isNot(AsmToken::RBrac)) {
2482 Error(E, "']' expected");
2483 return MatchOperand_ParseFail;
2486 Parser.Lex(); // Eat right bracket token.
2488 Operands.push_back(ARMOperand::CreateVectorIndex(MCE->getValue(),
2496 /// MatchCoprocessorOperandName - Try to parse an coprocessor related
2497 /// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
2499 static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
2500 // Use the same layout as the tablegen'erated register name matcher. Ugly,
2502 switch (Name.size()) {
2505 if (Name[0] != CoprocOp)
2521 if (Name[0] != CoprocOp || Name[1] != '1')
2525 case '0': return 10;
2526 case '1': return 11;
2527 case '2': return 12;
2528 case '3': return 13;
2529 case '4': return 14;
2530 case '5': return 15;
2535 /// parseITCondCode - Try to parse a condition code for an IT instruction.
2536 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2537 parseITCondCode(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2538 SMLoc S = Parser.getTok().getLoc();
2539 const AsmToken &Tok = Parser.getTok();
2540 if (!Tok.is(AsmToken::Identifier))
2541 return MatchOperand_NoMatch;
2542 unsigned CC = StringSwitch<unsigned>(Tok.getString())
2543 .Case("eq", ARMCC::EQ)
2544 .Case("ne", ARMCC::NE)
2545 .Case("hs", ARMCC::HS)
2546 .Case("cs", ARMCC::HS)
2547 .Case("lo", ARMCC::LO)
2548 .Case("cc", ARMCC::LO)
2549 .Case("mi", ARMCC::MI)
2550 .Case("pl", ARMCC::PL)
2551 .Case("vs", ARMCC::VS)
2552 .Case("vc", ARMCC::VC)
2553 .Case("hi", ARMCC::HI)
2554 .Case("ls", ARMCC::LS)
2555 .Case("ge", ARMCC::GE)
2556 .Case("lt", ARMCC::LT)
2557 .Case("gt", ARMCC::GT)
2558 .Case("le", ARMCC::LE)
2559 .Case("al", ARMCC::AL)
2562 return MatchOperand_NoMatch;
2563 Parser.Lex(); // Eat the token.
2565 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), S));
2567 return MatchOperand_Success;
2570 /// parseCoprocNumOperand - Try to parse an coprocessor number operand. The
2571 /// token must be an Identifier when called, and if it is a coprocessor
2572 /// number, the token is eaten and the operand is added to the operand list.
2573 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2574 parseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2575 SMLoc S = Parser.getTok().getLoc();
2576 const AsmToken &Tok = Parser.getTok();
2577 if (Tok.isNot(AsmToken::Identifier))
2578 return MatchOperand_NoMatch;
2580 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
2582 return MatchOperand_NoMatch;
2584 Parser.Lex(); // Eat identifier token.
2585 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
2586 return MatchOperand_Success;
2589 /// parseCoprocRegOperand - Try to parse an coprocessor register operand. The
2590 /// token must be an Identifier when called, and if it is a coprocessor
2591 /// number, the token is eaten and the operand is added to the operand list.
2592 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2593 parseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2594 SMLoc S = Parser.getTok().getLoc();
2595 const AsmToken &Tok = Parser.getTok();
2596 if (Tok.isNot(AsmToken::Identifier))
2597 return MatchOperand_NoMatch;
2599 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
2601 return MatchOperand_NoMatch;
2603 Parser.Lex(); // Eat identifier token.
2604 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
2605 return MatchOperand_Success;
2608 /// parseCoprocOptionOperand - Try to parse an coprocessor option operand.
2609 /// coproc_option : '{' imm0_255 '}'
2610 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2611 parseCoprocOptionOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2612 SMLoc S = Parser.getTok().getLoc();
2614 // If this isn't a '{', this isn't a coprocessor immediate operand.
2615 if (Parser.getTok().isNot(AsmToken::LCurly))
2616 return MatchOperand_NoMatch;
2617 Parser.Lex(); // Eat the '{'
2620 SMLoc Loc = Parser.getTok().getLoc();
2621 if (getParser().ParseExpression(Expr)) {
2622 Error(Loc, "illegal expression");
2623 return MatchOperand_ParseFail;
2625 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
2626 if (!CE || CE->getValue() < 0 || CE->getValue() > 255) {
2627 Error(Loc, "coprocessor option must be an immediate in range [0, 255]");
2628 return MatchOperand_ParseFail;
2630 int Val = CE->getValue();
2632 // Check for and consume the closing '}'
2633 if (Parser.getTok().isNot(AsmToken::RCurly))
2634 return MatchOperand_ParseFail;
2635 SMLoc E = Parser.getTok().getLoc();
2636 Parser.Lex(); // Eat the '}'
2638 Operands.push_back(ARMOperand::CreateCoprocOption(Val, S, E));
2639 return MatchOperand_Success;
2642 // For register list parsing, we need to map from raw GPR register numbering
2643 // to the enumeration values. The enumeration values aren't sorted by
2644 // register number due to our using "sp", "lr" and "pc" as canonical names.
2645 static unsigned getNextRegister(unsigned Reg) {
2646 // If this is a GPR, we need to do it manually, otherwise we can rely
2647 // on the sort ordering of the enumeration since the other reg-classes
2649 if (!ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
2652 default: assert(0 && "Invalid GPR number!");
2653 case ARM::R0: return ARM::R1; case ARM::R1: return ARM::R2;
2654 case ARM::R2: return ARM::R3; case ARM::R3: return ARM::R4;
2655 case ARM::R4: return ARM::R5; case ARM::R5: return ARM::R6;
2656 case ARM::R6: return ARM::R7; case ARM::R7: return ARM::R8;
2657 case ARM::R8: return ARM::R9; case ARM::R9: return ARM::R10;
2658 case ARM::R10: return ARM::R11; case ARM::R11: return ARM::R12;
2659 case ARM::R12: return ARM::SP; case ARM::SP: return ARM::LR;
2660 case ARM::LR: return ARM::PC; case ARM::PC: return ARM::R0;
2664 // Return the low-subreg of a given Q register.
2665 static unsigned getDRegFromQReg(unsigned QReg) {
2667 default: llvm_unreachable("expected a Q register!");
2668 case ARM::Q0: return ARM::D0;
2669 case ARM::Q1: return ARM::D2;
2670 case ARM::Q2: return ARM::D4;
2671 case ARM::Q3: return ARM::D6;
2672 case ARM::Q4: return ARM::D8;
2673 case ARM::Q5: return ARM::D10;
2674 case ARM::Q6: return ARM::D12;
2675 case ARM::Q7: return ARM::D14;
2676 case ARM::Q8: return ARM::D16;
2677 case ARM::Q9: return ARM::D18;
2678 case ARM::Q10: return ARM::D20;
2679 case ARM::Q11: return ARM::D22;
2680 case ARM::Q12: return ARM::D24;
2681 case ARM::Q13: return ARM::D26;
2682 case ARM::Q14: return ARM::D28;
2683 case ARM::Q15: return ARM::D30;
2687 /// Parse a register list.
2689 parseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2690 assert(Parser.getTok().is(AsmToken::LCurly) &&
2691 "Token is not a Left Curly Brace");
2692 SMLoc S = Parser.getTok().getLoc();
2693 Parser.Lex(); // Eat '{' token.
2694 SMLoc RegLoc = Parser.getTok().getLoc();
2696 // Check the first register in the list to see what register class
2697 // this is a list of.
2698 int Reg = tryParseRegister();
2700 return Error(RegLoc, "register expected");
2702 // The reglist instructions have at most 16 registers, so reserve
2703 // space for that many.
2704 SmallVector<std::pair<unsigned, SMLoc>, 16> Registers;
2706 // Allow Q regs and just interpret them as the two D sub-registers.
2707 if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
2708 Reg = getDRegFromQReg(Reg);
2709 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2712 const MCRegisterClass *RC;
2713 if (ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
2714 RC = &ARMMCRegisterClasses[ARM::GPRRegClassID];
2715 else if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg))
2716 RC = &ARMMCRegisterClasses[ARM::DPRRegClassID];
2717 else if (ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg))
2718 RC = &ARMMCRegisterClasses[ARM::SPRRegClassID];
2720 return Error(RegLoc, "invalid register in register list");
2722 // Store the register.
2723 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2725 // This starts immediately after the first register token in the list,
2726 // so we can see either a comma or a minus (range separator) as a legal
2728 while (Parser.getTok().is(AsmToken::Comma) ||
2729 Parser.getTok().is(AsmToken::Minus)) {
2730 if (Parser.getTok().is(AsmToken::Minus)) {
2731 Parser.Lex(); // Eat the minus.
2732 SMLoc EndLoc = Parser.getTok().getLoc();
2733 int EndReg = tryParseRegister();
2735 return Error(EndLoc, "register expected");
2736 // Allow Q regs and just interpret them as the two D sub-registers.
2737 if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(EndReg))
2738 EndReg = getDRegFromQReg(EndReg) + 1;
2739 // If the register is the same as the start reg, there's nothing
2743 // The register must be in the same register class as the first.
2744 if (!RC->contains(EndReg))
2745 return Error(EndLoc, "invalid register in register list");
2746 // Ranges must go from low to high.
2747 if (getARMRegisterNumbering(Reg) > getARMRegisterNumbering(EndReg))
2748 return Error(EndLoc, "bad range in register list");
2750 // Add all the registers in the range to the register list.
2751 while (Reg != EndReg) {
2752 Reg = getNextRegister(Reg);
2753 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2757 Parser.Lex(); // Eat the comma.
2758 RegLoc = Parser.getTok().getLoc();
2760 const AsmToken RegTok = Parser.getTok();
2761 Reg = tryParseRegister();
2763 return Error(RegLoc, "register expected");
2764 // Allow Q regs and just interpret them as the two D sub-registers.
2765 bool isQReg = false;
2766 if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
2767 Reg = getDRegFromQReg(Reg);
2770 // The register must be in the same register class as the first.
2771 if (!RC->contains(Reg))
2772 return Error(RegLoc, "invalid register in register list");
2773 // List must be monotonically increasing.
2774 if (getARMRegisterNumbering(Reg) < getARMRegisterNumbering(OldReg))
2775 return Error(RegLoc, "register list not in ascending order");
2776 if (getARMRegisterNumbering(Reg) == getARMRegisterNumbering(OldReg)) {
2777 Warning(RegLoc, "duplicated register (" + RegTok.getString() +
2778 ") in register list");
2781 // VFP register lists must also be contiguous.
2782 // It's OK to use the enumeration values directly here rather, as the
2783 // VFP register classes have the enum sorted properly.
2784 if (RC != &ARMMCRegisterClasses[ARM::GPRRegClassID] &&
2786 return Error(RegLoc, "non-contiguous register range");
2787 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2789 Registers.push_back(std::pair<unsigned, SMLoc>(++Reg, RegLoc));
2792 SMLoc E = Parser.getTok().getLoc();
2793 if (Parser.getTok().isNot(AsmToken::RCurly))
2794 return Error(E, "'}' expected");
2795 Parser.Lex(); // Eat '}' token.
2797 // Push the register list operand.
2798 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
2800 // The ARM system instruction variants for LDM/STM have a '^' token here.
2801 if (Parser.getTok().is(AsmToken::Caret)) {
2802 Operands.push_back(ARMOperand::CreateToken("^",Parser.getTok().getLoc()));
2803 Parser.Lex(); // Eat '^' token.
2809 // Helper function to parse the lane index for vector lists.
2810 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2811 parseVectorLane(VectorLaneTy &LaneKind, unsigned &Index) {
2812 Index = 0; // Always return a defined index value.
2813 if (Parser.getTok().is(AsmToken::LBrac)) {
2814 Parser.Lex(); // Eat the '['.
2815 if (Parser.getTok().is(AsmToken::RBrac)) {
2816 // "Dn[]" is the 'all lanes' syntax.
2817 LaneKind = AllLanes;
2818 Parser.Lex(); // Eat the ']'.
2819 return MatchOperand_Success;
2821 const MCExpr *LaneIndex;
2822 SMLoc Loc = Parser.getTok().getLoc();
2823 if (getParser().ParseExpression(LaneIndex)) {
2824 Error(Loc, "illegal expression");
2825 return MatchOperand_ParseFail;
2827 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(LaneIndex);
2829 Error(Loc, "lane index must be empty or an integer");
2830 return MatchOperand_ParseFail;
2832 if (Parser.getTok().isNot(AsmToken::RBrac)) {
2833 Error(Parser.getTok().getLoc(), "']' expected");
2834 return MatchOperand_ParseFail;
2836 Parser.Lex(); // Eat the ']'.
2837 int64_t Val = CE->getValue();
2839 // FIXME: Make this range check context sensitive for .8, .16, .32.
2840 if (Val < 0 || Val > 7) {
2841 Error(Parser.getTok().getLoc(), "lane index out of range");
2842 return MatchOperand_ParseFail;
2845 LaneKind = IndexedLane;
2846 return MatchOperand_Success;
2849 return MatchOperand_Success;
2852 // parse a vector register list
2853 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2854 parseVectorList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2855 VectorLaneTy LaneKind;
2857 SMLoc S = Parser.getTok().getLoc();
2858 // As an extension (to match gas), support a plain D register or Q register
2859 // (without encosing curly braces) as a single or double entry list,
2861 if (Parser.getTok().is(AsmToken::Identifier)) {
2862 int Reg = tryParseRegister();
2864 return MatchOperand_NoMatch;
2865 SMLoc E = Parser.getTok().getLoc();
2866 if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg)) {
2867 OperandMatchResultTy Res = parseVectorLane(LaneKind, LaneIndex);
2868 if (Res != MatchOperand_Success)
2872 E = Parser.getTok().getLoc();
2873 Operands.push_back(ARMOperand::CreateVectorList(Reg, 1, false, S, E));
2876 E = Parser.getTok().getLoc();
2877 Operands.push_back(ARMOperand::CreateVectorListAllLanes(Reg, 1, false,
2881 Operands.push_back(ARMOperand::CreateVectorListIndexed(Reg, 1,
2886 return MatchOperand_Success;
2888 if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
2889 Reg = getDRegFromQReg(Reg);
2890 OperandMatchResultTy Res = parseVectorLane(LaneKind, LaneIndex);
2891 if (Res != MatchOperand_Success)
2895 E = Parser.getTok().getLoc();
2896 Operands.push_back(ARMOperand::CreateVectorList(Reg, 2, false, S, E));
2899 E = Parser.getTok().getLoc();
2900 Operands.push_back(ARMOperand::CreateVectorListAllLanes(Reg, 2, false,
2904 Operands.push_back(ARMOperand::CreateVectorListIndexed(Reg, 2,
2909 return MatchOperand_Success;
2911 Error(S, "vector register expected");
2912 return MatchOperand_ParseFail;
2915 if (Parser.getTok().isNot(AsmToken::LCurly))
2916 return MatchOperand_NoMatch;
2918 Parser.Lex(); // Eat '{' token.
2919 SMLoc RegLoc = Parser.getTok().getLoc();
2921 int Reg = tryParseRegister();
2923 Error(RegLoc, "register expected");
2924 return MatchOperand_ParseFail;
2928 unsigned FirstReg = Reg;
2929 // The list is of D registers, but we also allow Q regs and just interpret
2930 // them as the two D sub-registers.
2931 if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
2932 FirstReg = Reg = getDRegFromQReg(Reg);
2933 Spacing = 1; // double-spacing requires explicit D registers, otherwise
2934 // it's ambiguous with four-register single spaced.
2938 if (parseVectorLane(LaneKind, LaneIndex) != MatchOperand_Success)
2939 return MatchOperand_ParseFail;
2941 while (Parser.getTok().is(AsmToken::Comma) ||
2942 Parser.getTok().is(AsmToken::Minus)) {
2943 if (Parser.getTok().is(AsmToken::Minus)) {
2945 Spacing = 1; // Register range implies a single spaced list.
2946 else if (Spacing == 2) {
2947 Error(Parser.getTok().getLoc(),
2948 "sequential registers in double spaced list");
2949 return MatchOperand_ParseFail;
2951 Parser.Lex(); // Eat the minus.
2952 SMLoc EndLoc = Parser.getTok().getLoc();
2953 int EndReg = tryParseRegister();
2955 Error(EndLoc, "register expected");
2956 return MatchOperand_ParseFail;
2958 // Allow Q regs and just interpret them as the two D sub-registers.
2959 if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(EndReg))
2960 EndReg = getDRegFromQReg(EndReg) + 1;
2961 // If the register is the same as the start reg, there's nothing
2965 // The register must be in the same register class as the first.
2966 if (!ARMMCRegisterClasses[ARM::DPRRegClassID].contains(EndReg)) {
2967 Error(EndLoc, "invalid register in register list");
2968 return MatchOperand_ParseFail;
2970 // Ranges must go from low to high.
2972 Error(EndLoc, "bad range in register list");
2973 return MatchOperand_ParseFail;
2975 // Parse the lane specifier if present.
2976 VectorLaneTy NextLaneKind;
2977 unsigned NextLaneIndex;
2978 if (parseVectorLane(NextLaneKind, NextLaneIndex) != MatchOperand_Success)
2979 return MatchOperand_ParseFail;
2980 if (NextLaneKind != LaneKind || LaneIndex != NextLaneIndex) {
2981 Error(EndLoc, "mismatched lane index in register list");
2982 return MatchOperand_ParseFail;
2984 EndLoc = Parser.getTok().getLoc();
2986 // Add all the registers in the range to the register list.
2987 Count += EndReg - Reg;
2991 Parser.Lex(); // Eat the comma.
2992 RegLoc = Parser.getTok().getLoc();
2994 Reg = tryParseRegister();
2996 Error(RegLoc, "register expected");
2997 return MatchOperand_ParseFail;
2999 // vector register lists must be contiguous.
3000 // It's OK to use the enumeration values directly here rather, as the
3001 // VFP register classes have the enum sorted properly.
3003 // The list is of D registers, but we also allow Q regs and just interpret
3004 // them as the two D sub-registers.
3005 if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
3007 Spacing = 1; // Register range implies a single spaced list.
3008 else if (Spacing == 2) {
3010 "invalid register in double-spaced list (must be 'D' register')");
3011 return MatchOperand_ParseFail;
3013 Reg = getDRegFromQReg(Reg);
3014 if (Reg != OldReg + 1) {
3015 Error(RegLoc, "non-contiguous register range");
3016 return MatchOperand_ParseFail;
3020 // Parse the lane specifier if present.
3021 VectorLaneTy NextLaneKind;
3022 unsigned NextLaneIndex;
3023 SMLoc EndLoc = Parser.getTok().getLoc();
3024 if (parseVectorLane(NextLaneKind, NextLaneIndex) != MatchOperand_Success)
3025 return MatchOperand_ParseFail;
3026 if (NextLaneKind != LaneKind || LaneIndex != NextLaneIndex) {
3027 Error(EndLoc, "mismatched lane index in register list");
3028 return MatchOperand_ParseFail;
3032 // Normal D register.
3033 // Figure out the register spacing (single or double) of the list if
3034 // we don't know it already.
3036 Spacing = 1 + (Reg == OldReg + 2);
3038 // Just check that it's contiguous and keep going.
3039 if (Reg != OldReg + Spacing) {
3040 Error(RegLoc, "non-contiguous register range");
3041 return MatchOperand_ParseFail;
3044 // Parse the lane specifier if present.
3045 VectorLaneTy NextLaneKind;
3046 unsigned NextLaneIndex;
3047 SMLoc EndLoc = Parser.getTok().getLoc();
3048 if (parseVectorLane(NextLaneKind, NextLaneIndex) != MatchOperand_Success)
3049 return MatchOperand_ParseFail;
3050 if (NextLaneKind != LaneKind || LaneIndex != NextLaneIndex) {
3051 Error(EndLoc, "mismatched lane index in register list");
3052 return MatchOperand_ParseFail;
3056 SMLoc E = Parser.getTok().getLoc();
3057 if (Parser.getTok().isNot(AsmToken::RCurly)) {
3058 Error(E, "'}' expected");
3059 return MatchOperand_ParseFail;
3061 Parser.Lex(); // Eat '}' token.
3065 Operands.push_back(ARMOperand::CreateVectorList(FirstReg, Count,
3066 (Spacing == 2), S, E));
3069 Operands.push_back(ARMOperand::CreateVectorListAllLanes(FirstReg, Count,
3074 Operands.push_back(ARMOperand::CreateVectorListIndexed(FirstReg, Count,
3080 return MatchOperand_Success;
3083 /// parseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
3084 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3085 parseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3086 SMLoc S = Parser.getTok().getLoc();
3087 const AsmToken &Tok = Parser.getTok();
3088 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
3089 StringRef OptStr = Tok.getString();
3091 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
3092 .Case("sy", ARM_MB::SY)
3093 .Case("st", ARM_MB::ST)
3094 .Case("sh", ARM_MB::ISH)
3095 .Case("ish", ARM_MB::ISH)
3096 .Case("shst", ARM_MB::ISHST)
3097 .Case("ishst", ARM_MB::ISHST)
3098 .Case("nsh", ARM_MB::NSH)
3099 .Case("un", ARM_MB::NSH)
3100 .Case("nshst", ARM_MB::NSHST)
3101 .Case("unst", ARM_MB::NSHST)
3102 .Case("osh", ARM_MB::OSH)
3103 .Case("oshst", ARM_MB::OSHST)
3107 return MatchOperand_NoMatch;
3109 Parser.Lex(); // Eat identifier token.
3110 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
3111 return MatchOperand_Success;
3114 /// parseProcIFlagsOperand - Try to parse iflags from CPS instruction.
3115 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3116 parseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3117 SMLoc S = Parser.getTok().getLoc();
3118 const AsmToken &Tok = Parser.getTok();
3119 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
3120 StringRef IFlagsStr = Tok.getString();
3122 // An iflags string of "none" is interpreted to mean that none of the AIF
3123 // bits are set. Not a terribly useful instruction, but a valid encoding.
3124 unsigned IFlags = 0;
3125 if (IFlagsStr != "none") {
3126 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
3127 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
3128 .Case("a", ARM_PROC::A)
3129 .Case("i", ARM_PROC::I)
3130 .Case("f", ARM_PROC::F)
3133 // If some specific iflag is already set, it means that some letter is
3134 // present more than once, this is not acceptable.
3135 if (Flag == ~0U || (IFlags & Flag))
3136 return MatchOperand_NoMatch;
3142 Parser.Lex(); // Eat identifier token.
3143 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
3144 return MatchOperand_Success;
3147 /// parseMSRMaskOperand - Try to parse mask flags from MSR instruction.
3148 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3149 parseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3150 SMLoc S = Parser.getTok().getLoc();
3151 const AsmToken &Tok = Parser.getTok();
3152 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
3153 StringRef Mask = Tok.getString();
3156 // See ARMv6-M 10.1.1
3157 unsigned FlagsVal = StringSwitch<unsigned>(Mask)
3167 .Case("primask", 16)
3168 .Case("basepri", 17)
3169 .Case("basepri_max", 18)
3170 .Case("faultmask", 19)
3171 .Case("control", 20)
3174 if (FlagsVal == ~0U)
3175 return MatchOperand_NoMatch;
3177 if (!hasV7Ops() && FlagsVal >= 17 && FlagsVal <= 19)
3178 // basepri, basepri_max and faultmask only valid for V7m.
3179 return MatchOperand_NoMatch;
3181 Parser.Lex(); // Eat identifier token.
3182 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
3183 return MatchOperand_Success;
3186 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
3187 size_t Start = 0, Next = Mask.find('_');
3188 StringRef Flags = "";
3189 std::string SpecReg = Mask.slice(Start, Next).lower();
3190 if (Next != StringRef::npos)
3191 Flags = Mask.slice(Next+1, Mask.size());
3193 // FlagsVal contains the complete mask:
3195 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
3196 unsigned FlagsVal = 0;
3198 if (SpecReg == "apsr") {
3199 FlagsVal = StringSwitch<unsigned>(Flags)
3200 .Case("nzcvq", 0x8) // same as CPSR_f
3201 .Case("g", 0x4) // same as CPSR_s
3202 .Case("nzcvqg", 0xc) // same as CPSR_fs
3205 if (FlagsVal == ~0U) {
3207 return MatchOperand_NoMatch;
3209 FlagsVal = 8; // No flag
3211 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
3212 if (Flags == "all") // cpsr_all is an alias for cpsr_fc
3214 for (int i = 0, e = Flags.size(); i != e; ++i) {
3215 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
3222 // If some specific flag is already set, it means that some letter is
3223 // present more than once, this is not acceptable.
3224 if (FlagsVal == ~0U || (FlagsVal & Flag))
3225 return MatchOperand_NoMatch;
3228 } else // No match for special register.
3229 return MatchOperand_NoMatch;
3231 // Special register without flags is NOT equivalent to "fc" flags.
3232 // NOTE: This is a divergence from gas' behavior. Uncommenting the following
3233 // two lines would enable gas compatibility at the expense of breaking
3239 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
3240 if (SpecReg == "spsr")
3243 Parser.Lex(); // Eat identifier token.
3244 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
3245 return MatchOperand_Success;
3248 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3249 parsePKHImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands, StringRef Op,
3250 int Low, int High) {
3251 const AsmToken &Tok = Parser.getTok();
3252 if (Tok.isNot(AsmToken::Identifier)) {
3253 Error(Parser.getTok().getLoc(), Op + " operand expected.");
3254 return MatchOperand_ParseFail;
3256 StringRef ShiftName = Tok.getString();
3257 std::string LowerOp = Op.lower();
3258 std::string UpperOp = Op.upper();
3259 if (ShiftName != LowerOp && ShiftName != UpperOp) {
3260 Error(Parser.getTok().getLoc(), Op + " operand expected.");
3261 return MatchOperand_ParseFail;
3263 Parser.Lex(); // Eat shift type token.
3265 // There must be a '#' and a shift amount.
3266 if (Parser.getTok().isNot(AsmToken::Hash) &&
3267 Parser.getTok().isNot(AsmToken::Dollar)) {
3268 Error(Parser.getTok().getLoc(), "'#' expected");
3269 return MatchOperand_ParseFail;
3271 Parser.Lex(); // Eat hash token.
3273 const MCExpr *ShiftAmount;
3274 SMLoc Loc = Parser.getTok().getLoc();
3275 if (getParser().ParseExpression(ShiftAmount)) {
3276 Error(Loc, "illegal expression");
3277 return MatchOperand_ParseFail;
3279 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
3281 Error(Loc, "constant expression expected");
3282 return MatchOperand_ParseFail;
3284 int Val = CE->getValue();
3285 if (Val < Low || Val > High) {
3286 Error(Loc, "immediate value out of range");
3287 return MatchOperand_ParseFail;
3290 Operands.push_back(ARMOperand::CreateImm(CE, Loc, Parser.getTok().getLoc()));
3292 return MatchOperand_Success;
3295 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3296 parseSetEndImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3297 const AsmToken &Tok = Parser.getTok();
3298 SMLoc S = Tok.getLoc();
3299 if (Tok.isNot(AsmToken::Identifier)) {
3300 Error(Tok.getLoc(), "'be' or 'le' operand expected");
3301 return MatchOperand_ParseFail;
3303 int Val = StringSwitch<int>(Tok.getString())
3307 Parser.Lex(); // Eat the token.
3310 Error(Tok.getLoc(), "'be' or 'le' operand expected");
3311 return MatchOperand_ParseFail;
3313 Operands.push_back(ARMOperand::CreateImm(MCConstantExpr::Create(Val,
3315 S, Parser.getTok().getLoc()));
3316 return MatchOperand_Success;
3319 /// parseShifterImm - Parse the shifter immediate operand for SSAT/USAT
3320 /// instructions. Legal values are:
3321 /// lsl #n 'n' in [0,31]
3322 /// asr #n 'n' in [1,32]
3323 /// n == 32 encoded as n == 0.
3324 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3325 parseShifterImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3326 const AsmToken &Tok = Parser.getTok();
3327 SMLoc S = Tok.getLoc();
3328 if (Tok.isNot(AsmToken::Identifier)) {
3329 Error(S, "shift operator 'asr' or 'lsl' expected");
3330 return MatchOperand_ParseFail;
3332 StringRef ShiftName = Tok.getString();
3334 if (ShiftName == "lsl" || ShiftName == "LSL")
3336 else if (ShiftName == "asr" || ShiftName == "ASR")
3339 Error(S, "shift operator 'asr' or 'lsl' expected");
3340 return MatchOperand_ParseFail;
3342 Parser.Lex(); // Eat the operator.
3344 // A '#' and a shift amount.
3345 if (Parser.getTok().isNot(AsmToken::Hash) &&
3346 Parser.getTok().isNot(AsmToken::Dollar)) {
3347 Error(Parser.getTok().getLoc(), "'#' expected");
3348 return MatchOperand_ParseFail;
3350 Parser.Lex(); // Eat hash token.
3352 const MCExpr *ShiftAmount;
3353 SMLoc E = Parser.getTok().getLoc();
3354 if (getParser().ParseExpression(ShiftAmount)) {
3355 Error(E, "malformed shift expression");
3356 return MatchOperand_ParseFail;
3358 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
3360 Error(E, "shift amount must be an immediate");
3361 return MatchOperand_ParseFail;
3364 int64_t Val = CE->getValue();
3366 // Shift amount must be in [1,32]
3367 if (Val < 1 || Val > 32) {
3368 Error(E, "'asr' shift amount must be in range [1,32]");
3369 return MatchOperand_ParseFail;
3371 // asr #32 encoded as asr #0, but is not allowed in Thumb2 mode.
3372 if (isThumb() && Val == 32) {
3373 Error(E, "'asr #32' shift amount not allowed in Thumb mode");
3374 return MatchOperand_ParseFail;
3376 if (Val == 32) Val = 0;
3378 // Shift amount must be in [1,32]
3379 if (Val < 0 || Val > 31) {
3380 Error(E, "'lsr' shift amount must be in range [0,31]");
3381 return MatchOperand_ParseFail;
3385 E = Parser.getTok().getLoc();
3386 Operands.push_back(ARMOperand::CreateShifterImm(isASR, Val, S, E));
3388 return MatchOperand_Success;
3391 /// parseRotImm - Parse the shifter immediate operand for SXTB/UXTB family
3392 /// of instructions. Legal values are:
3393 /// ror #n 'n' in {0, 8, 16, 24}
3394 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3395 parseRotImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3396 const AsmToken &Tok = Parser.getTok();
3397 SMLoc S = Tok.getLoc();
3398 if (Tok.isNot(AsmToken::Identifier))
3399 return MatchOperand_NoMatch;
3400 StringRef ShiftName = Tok.getString();
3401 if (ShiftName != "ror" && ShiftName != "ROR")
3402 return MatchOperand_NoMatch;
3403 Parser.Lex(); // Eat the operator.
3405 // A '#' and a rotate amount.
3406 if (Parser.getTok().isNot(AsmToken::Hash) &&
3407 Parser.getTok().isNot(AsmToken::Dollar)) {
3408 Error(Parser.getTok().getLoc(), "'#' expected");
3409 return MatchOperand_ParseFail;
3411 Parser.Lex(); // Eat hash token.
3413 const MCExpr *ShiftAmount;
3414 SMLoc E = Parser.getTok().getLoc();
3415 if (getParser().ParseExpression(ShiftAmount)) {
3416 Error(E, "malformed rotate expression");
3417 return MatchOperand_ParseFail;
3419 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
3421 Error(E, "rotate amount must be an immediate");
3422 return MatchOperand_ParseFail;
3425 int64_t Val = CE->getValue();
3426 // Shift amount must be in {0, 8, 16, 24} (0 is undocumented extension)
3427 // normally, zero is represented in asm by omitting the rotate operand
3429 if (Val != 8 && Val != 16 && Val != 24 && Val != 0) {
3430 Error(E, "'ror' rotate amount must be 8, 16, or 24");
3431 return MatchOperand_ParseFail;
3434 E = Parser.getTok().getLoc();
3435 Operands.push_back(ARMOperand::CreateRotImm(Val, S, E));
3437 return MatchOperand_Success;
3440 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3441 parseBitfield(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3442 SMLoc S = Parser.getTok().getLoc();
3443 // The bitfield descriptor is really two operands, the LSB and the width.
3444 if (Parser.getTok().isNot(AsmToken::Hash) &&
3445 Parser.getTok().isNot(AsmToken::Dollar)) {
3446 Error(Parser.getTok().getLoc(), "'#' expected");
3447 return MatchOperand_ParseFail;
3449 Parser.Lex(); // Eat hash token.
3451 const MCExpr *LSBExpr;
3452 SMLoc E = Parser.getTok().getLoc();
3453 if (getParser().ParseExpression(LSBExpr)) {
3454 Error(E, "malformed immediate expression");
3455 return MatchOperand_ParseFail;
3457 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(LSBExpr);
3459 Error(E, "'lsb' operand must be an immediate");
3460 return MatchOperand_ParseFail;
3463 int64_t LSB = CE->getValue();
3464 // The LSB must be in the range [0,31]
3465 if (LSB < 0 || LSB > 31) {
3466 Error(E, "'lsb' operand must be in the range [0,31]");
3467 return MatchOperand_ParseFail;
3469 E = Parser.getTok().getLoc();
3471 // Expect another immediate operand.
3472 if (Parser.getTok().isNot(AsmToken::Comma)) {
3473 Error(Parser.getTok().getLoc(), "too few operands");
3474 return MatchOperand_ParseFail;
3476 Parser.Lex(); // Eat hash token.
3477 if (Parser.getTok().isNot(AsmToken::Hash) &&
3478 Parser.getTok().isNot(AsmToken::Dollar)) {
3479 Error(Parser.getTok().getLoc(), "'#' expected");
3480 return MatchOperand_ParseFail;
3482 Parser.Lex(); // Eat hash token.
3484 const MCExpr *WidthExpr;
3485 if (getParser().ParseExpression(WidthExpr)) {
3486 Error(E, "malformed immediate expression");
3487 return MatchOperand_ParseFail;
3489 CE = dyn_cast<MCConstantExpr>(WidthExpr);
3491 Error(E, "'width' operand must be an immediate");
3492 return MatchOperand_ParseFail;
3495 int64_t Width = CE->getValue();
3496 // The LSB must be in the range [1,32-lsb]
3497 if (Width < 1 || Width > 32 - LSB) {
3498 Error(E, "'width' operand must be in the range [1,32-lsb]");
3499 return MatchOperand_ParseFail;
3501 E = Parser.getTok().getLoc();
3503 Operands.push_back(ARMOperand::CreateBitfield(LSB, Width, S, E));
3505 return MatchOperand_Success;
3508 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3509 parsePostIdxReg(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3510 // Check for a post-index addressing register operand. Specifically:
3511 // postidx_reg := '+' register {, shift}
3512 // | '-' register {, shift}
3513 // | register {, shift}
3515 // This method must return MatchOperand_NoMatch without consuming any tokens
3516 // in the case where there is no match, as other alternatives take other
3518 AsmToken Tok = Parser.getTok();
3519 SMLoc S = Tok.getLoc();
3520 bool haveEaten = false;
3523 if (Tok.is(AsmToken::Plus)) {
3524 Parser.Lex(); // Eat the '+' token.
3526 } else if (Tok.is(AsmToken::Minus)) {
3527 Parser.Lex(); // Eat the '-' token.
3531 if (Parser.getTok().is(AsmToken::Identifier))
3532 Reg = tryParseRegister();
3535 return MatchOperand_NoMatch;
3536 Error(Parser.getTok().getLoc(), "register expected");
3537 return MatchOperand_ParseFail;
3539 SMLoc E = Parser.getTok().getLoc();
3541 ARM_AM::ShiftOpc ShiftTy = ARM_AM::no_shift;
3542 unsigned ShiftImm = 0;
3543 if (Parser.getTok().is(AsmToken::Comma)) {
3544 Parser.Lex(); // Eat the ','.
3545 if (parseMemRegOffsetShift(ShiftTy, ShiftImm))
3546 return MatchOperand_ParseFail;
3549 Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ShiftTy,
3552 return MatchOperand_Success;
3555 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3556 parseAM3Offset(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3557 // Check for a post-index addressing register operand. Specifically:
3558 // am3offset := '+' register
3565 // This method must return MatchOperand_NoMatch without consuming any tokens
3566 // in the case where there is no match, as other alternatives take other
3568 AsmToken Tok = Parser.getTok();
3569 SMLoc S = Tok.getLoc();
3571 // Do immediates first, as we always parse those if we have a '#'.
3572 if (Parser.getTok().is(AsmToken::Hash) ||
3573 Parser.getTok().is(AsmToken::Dollar)) {
3574 Parser.Lex(); // Eat the '#'.
3575 // Explicitly look for a '-', as we need to encode negative zero
3577 bool isNegative = Parser.getTok().is(AsmToken::Minus);
3578 const MCExpr *Offset;
3579 if (getParser().ParseExpression(Offset))
3580 return MatchOperand_ParseFail;
3581 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
3583 Error(S, "constant expression expected");
3584 return MatchOperand_ParseFail;
3586 SMLoc E = Tok.getLoc();
3587 // Negative zero is encoded as the flag value INT32_MIN.
3588 int32_t Val = CE->getValue();
3589 if (isNegative && Val == 0)
3593 ARMOperand::CreateImm(MCConstantExpr::Create(Val, getContext()), S, E));
3595 return MatchOperand_Success;
3599 bool haveEaten = false;
3602 if (Tok.is(AsmToken::Plus)) {
3603 Parser.Lex(); // Eat the '+' token.
3605 } else if (Tok.is(AsmToken::Minus)) {
3606 Parser.Lex(); // Eat the '-' token.
3610 if (Parser.getTok().is(AsmToken::Identifier))
3611 Reg = tryParseRegister();
3614 return MatchOperand_NoMatch;
3615 Error(Parser.getTok().getLoc(), "register expected");
3616 return MatchOperand_ParseFail;
3618 SMLoc E = Parser.getTok().getLoc();
3620 Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ARM_AM::no_shift,
3623 return MatchOperand_Success;
3626 /// cvtT2LdrdPre - Convert parsed operands to MCInst.
3627 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3628 /// when they refer multiple MIOperands inside a single one.
3630 cvtT2LdrdPre(MCInst &Inst, unsigned Opcode,
3631 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3633 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3634 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3635 // Create a writeback register dummy placeholder.
3636 Inst.addOperand(MCOperand::CreateReg(0));
3638 ((ARMOperand*)Operands[4])->addMemImm8s4OffsetOperands(Inst, 2);
3640 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3644 /// cvtT2StrdPre - Convert parsed operands to MCInst.
3645 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3646 /// when they refer multiple MIOperands inside a single one.
3648 cvtT2StrdPre(MCInst &Inst, unsigned Opcode,
3649 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3650 // Create a writeback register dummy placeholder.
3651 Inst.addOperand(MCOperand::CreateReg(0));
3653 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3654 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3656 ((ARMOperand*)Operands[4])->addMemImm8s4OffsetOperands(Inst, 2);
3658 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3662 /// cvtLdWriteBackRegT2AddrModeImm8 - Convert parsed operands to MCInst.
3663 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3664 /// when they refer multiple MIOperands inside a single one.
3666 cvtLdWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
3667 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3668 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3670 // Create a writeback register dummy placeholder.
3671 Inst.addOperand(MCOperand::CreateImm(0));
3673 ((ARMOperand*)Operands[3])->addMemImm8OffsetOperands(Inst, 2);
3674 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3678 /// cvtStWriteBackRegT2AddrModeImm8 - Convert parsed operands to MCInst.
3679 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3680 /// when they refer multiple MIOperands inside a single one.
3682 cvtStWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
3683 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3684 // Create a writeback register dummy placeholder.
3685 Inst.addOperand(MCOperand::CreateImm(0));
3686 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3687 ((ARMOperand*)Operands[3])->addMemImm8OffsetOperands(Inst, 2);
3688 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3692 /// cvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
3693 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3694 /// when they refer multiple MIOperands inside a single one.
3696 cvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
3697 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3698 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3700 // Create a writeback register dummy placeholder.
3701 Inst.addOperand(MCOperand::CreateImm(0));
3703 ((ARMOperand*)Operands[3])->addAddrMode2Operands(Inst, 3);
3704 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3708 /// cvtLdWriteBackRegAddrModeImm12 - Convert parsed operands to MCInst.
3709 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3710 /// when they refer multiple MIOperands inside a single one.
3712 cvtLdWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
3713 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3714 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3716 // Create a writeback register dummy placeholder.
3717 Inst.addOperand(MCOperand::CreateImm(0));
3719 ((ARMOperand*)Operands[3])->addMemImm12OffsetOperands(Inst, 2);
3720 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3725 /// cvtStWriteBackRegAddrModeImm12 - Convert parsed operands to MCInst.
3726 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3727 /// when they refer multiple MIOperands inside a single one.
3729 cvtStWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
3730 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3731 // Create a writeback register dummy placeholder.
3732 Inst.addOperand(MCOperand::CreateImm(0));
3733 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3734 ((ARMOperand*)Operands[3])->addMemImm12OffsetOperands(Inst, 2);
3735 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3739 /// cvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
3740 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3741 /// when they refer multiple MIOperands inside a single one.
3743 cvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
3744 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3745 // Create a writeback register dummy placeholder.
3746 Inst.addOperand(MCOperand::CreateImm(0));
3747 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3748 ((ARMOperand*)Operands[3])->addAddrMode2Operands(Inst, 3);
3749 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3753 /// cvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
3754 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3755 /// when they refer multiple MIOperands inside a single one.
3757 cvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
3758 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3759 // Create a writeback register dummy placeholder.
3760 Inst.addOperand(MCOperand::CreateImm(0));
3761 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3762 ((ARMOperand*)Operands[3])->addAddrMode3Operands(Inst, 3);
3763 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3767 /// cvtLdExtTWriteBackImm - Convert parsed operands to MCInst.
3768 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3769 /// when they refer multiple MIOperands inside a single one.
3771 cvtLdExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
3772 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3774 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3775 // Create a writeback register dummy placeholder.
3776 Inst.addOperand(MCOperand::CreateImm(0));
3778 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3780 ((ARMOperand*)Operands[4])->addPostIdxImm8Operands(Inst, 1);
3782 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3786 /// cvtLdExtTWriteBackReg - Convert parsed operands to MCInst.
3787 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3788 /// when they refer multiple MIOperands inside a single one.
3790 cvtLdExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
3791 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3793 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3794 // Create a writeback register dummy placeholder.
3795 Inst.addOperand(MCOperand::CreateImm(0));
3797 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3799 ((ARMOperand*)Operands[4])->addPostIdxRegOperands(Inst, 2);
3801 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3805 /// cvtStExtTWriteBackImm - Convert parsed operands to MCInst.
3806 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3807 /// when they refer multiple MIOperands inside a single one.
3809 cvtStExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
3810 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3811 // Create a writeback register dummy placeholder.
3812 Inst.addOperand(MCOperand::CreateImm(0));
3814 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3816 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3818 ((ARMOperand*)Operands[4])->addPostIdxImm8Operands(Inst, 1);
3820 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3824 /// cvtStExtTWriteBackReg - Convert parsed operands to MCInst.
3825 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3826 /// when they refer multiple MIOperands inside a single one.
3828 cvtStExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
3829 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3830 // Create a writeback register dummy placeholder.
3831 Inst.addOperand(MCOperand::CreateImm(0));
3833 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3835 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3837 ((ARMOperand*)Operands[4])->addPostIdxRegOperands(Inst, 2);
3839 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3843 /// cvtLdrdPre - Convert parsed operands to MCInst.
3844 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3845 /// when they refer multiple MIOperands inside a single one.
3847 cvtLdrdPre(MCInst &Inst, unsigned Opcode,
3848 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3850 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3851 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3852 // Create a writeback register dummy placeholder.
3853 Inst.addOperand(MCOperand::CreateImm(0));
3855 ((ARMOperand*)Operands[4])->addAddrMode3Operands(Inst, 3);
3857 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3861 /// cvtStrdPre - Convert parsed operands to MCInst.
3862 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3863 /// when they refer multiple MIOperands inside a single one.
3865 cvtStrdPre(MCInst &Inst, unsigned Opcode,
3866 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3867 // Create a writeback register dummy placeholder.
3868 Inst.addOperand(MCOperand::CreateImm(0));
3870 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3871 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3873 ((ARMOperand*)Operands[4])->addAddrMode3Operands(Inst, 3);
3875 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3879 /// cvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
3880 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3881 /// when they refer multiple MIOperands inside a single one.
3883 cvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
3884 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3885 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3886 // Create a writeback register dummy placeholder.
3887 Inst.addOperand(MCOperand::CreateImm(0));
3888 ((ARMOperand*)Operands[3])->addAddrMode3Operands(Inst, 3);
3889 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3893 /// cvtThumbMultiple- Convert parsed operands to MCInst.
3894 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3895 /// when they refer multiple MIOperands inside a single one.
3897 cvtThumbMultiply(MCInst &Inst, unsigned Opcode,
3898 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3899 // The second source operand must be the same register as the destination
3901 if (Operands.size() == 6 &&
3902 (((ARMOperand*)Operands[3])->getReg() !=
3903 ((ARMOperand*)Operands[5])->getReg()) &&
3904 (((ARMOperand*)Operands[3])->getReg() !=
3905 ((ARMOperand*)Operands[4])->getReg())) {
3906 Error(Operands[3]->getStartLoc(),
3907 "destination register must match source register");
3910 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3911 ((ARMOperand*)Operands[1])->addCCOutOperands(Inst, 1);
3912 // If we have a three-operand form, make sure to set Rn to be the operand
3913 // that isn't the same as Rd.
3915 if (Operands.size() == 6 &&
3916 ((ARMOperand*)Operands[4])->getReg() ==
3917 ((ARMOperand*)Operands[3])->getReg())
3919 ((ARMOperand*)Operands[RegOp])->addRegOperands(Inst, 1);
3920 Inst.addOperand(Inst.getOperand(0));
3921 ((ARMOperand*)Operands[2])->addCondCodeOperands(Inst, 2);
3927 cvtVLDwbFixed(MCInst &Inst, unsigned Opcode,
3928 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3930 ((ARMOperand*)Operands[3])->addVecListOperands(Inst, 1);
3931 // Create a writeback register dummy placeholder.
3932 Inst.addOperand(MCOperand::CreateImm(0));
3934 ((ARMOperand*)Operands[4])->addAlignedMemoryOperands(Inst, 2);
3936 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3941 cvtVLDwbRegister(MCInst &Inst, unsigned Opcode,
3942 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3944 ((ARMOperand*)Operands[3])->addVecListOperands(Inst, 1);
3945 // Create a writeback register dummy placeholder.
3946 Inst.addOperand(MCOperand::CreateImm(0));
3948 ((ARMOperand*)Operands[4])->addAlignedMemoryOperands(Inst, 2);
3950 ((ARMOperand*)Operands[5])->addRegOperands(Inst, 1);
3952 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3957 cvtVSTwbFixed(MCInst &Inst, unsigned Opcode,
3958 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3959 // Create a writeback register dummy placeholder.
3960 Inst.addOperand(MCOperand::CreateImm(0));
3962 ((ARMOperand*)Operands[4])->addAlignedMemoryOperands(Inst, 2);
3964 ((ARMOperand*)Operands[3])->addVecListOperands(Inst, 1);
3966 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3971 cvtVSTwbRegister(MCInst &Inst, unsigned Opcode,
3972 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3973 // Create a writeback register dummy placeholder.
3974 Inst.addOperand(MCOperand::CreateImm(0));
3976 ((ARMOperand*)Operands[4])->addAlignedMemoryOperands(Inst, 2);
3978 ((ARMOperand*)Operands[5])->addRegOperands(Inst, 1);
3980 ((ARMOperand*)Operands[3])->addVecListOperands(Inst, 1);
3982 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3986 /// Parse an ARM memory expression, return false if successful else return true
3987 /// or an error. The first token must be a '[' when called.
3989 parseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3991 assert(Parser.getTok().is(AsmToken::LBrac) &&
3992 "Token is not a Left Bracket");
3993 S = Parser.getTok().getLoc();
3994 Parser.Lex(); // Eat left bracket token.
3996 const AsmToken &BaseRegTok = Parser.getTok();
3997 int BaseRegNum = tryParseRegister();
3998 if (BaseRegNum == -1)
3999 return Error(BaseRegTok.getLoc(), "register expected");
4001 // The next token must either be a comma or a closing bracket.
4002 const AsmToken &Tok = Parser.getTok();
4003 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
4004 return Error(Tok.getLoc(), "malformed memory operand");
4006 if (Tok.is(AsmToken::RBrac)) {
4008 Parser.Lex(); // Eat right bracket token.
4010 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, 0, ARM_AM::no_shift,
4011 0, 0, false, S, E));
4013 // If there's a pre-indexing writeback marker, '!', just add it as a token
4014 // operand. It's rather odd, but syntactically valid.
4015 if (Parser.getTok().is(AsmToken::Exclaim)) {
4016 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
4017 Parser.Lex(); // Eat the '!'.
4023 assert(Tok.is(AsmToken::Comma) && "Lost comma in memory operand?!");
4024 Parser.Lex(); // Eat the comma.
4026 // If we have a ':', it's an alignment specifier.
4027 if (Parser.getTok().is(AsmToken::Colon)) {
4028 Parser.Lex(); // Eat the ':'.
4029 E = Parser.getTok().getLoc();
4032 if (getParser().ParseExpression(Expr))
4035 // The expression has to be a constant. Memory references with relocations
4036 // don't come through here, as they use the <label> forms of the relevant
4038 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
4040 return Error (E, "constant expression expected");
4043 switch (CE->getValue()) {
4046 "alignment specifier must be 16, 32, 64, 128, or 256 bits");
4047 case 16: Align = 2; break;
4048 case 32: Align = 4; break;
4049 case 64: Align = 8; break;
4050 case 128: Align = 16; break;
4051 case 256: Align = 32; break;
4054 // Now we should have the closing ']'
4055 E = Parser.getTok().getLoc();
4056 if (Parser.getTok().isNot(AsmToken::RBrac))
4057 return Error(E, "']' expected");
4058 Parser.Lex(); // Eat right bracket token.
4060 // Don't worry about range checking the value here. That's handled by
4061 // the is*() predicates.
4062 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, 0,
4063 ARM_AM::no_shift, 0, Align,
4066 // If there's a pre-indexing writeback marker, '!', just add it as a token
4068 if (Parser.getTok().is(AsmToken::Exclaim)) {
4069 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
4070 Parser.Lex(); // Eat the '!'.
4076 // If we have a '#', it's an immediate offset, else assume it's a register
4077 // offset. Be friendly and also accept a plain integer (without a leading
4078 // hash) for gas compatibility.
4079 if (Parser.getTok().is(AsmToken::Hash) ||
4080 Parser.getTok().is(AsmToken::Dollar) ||
4081 Parser.getTok().is(AsmToken::Integer)) {
4082 if (Parser.getTok().isNot(AsmToken::Integer))
4083 Parser.Lex(); // Eat the '#'.
4084 E = Parser.getTok().getLoc();
4086 bool isNegative = getParser().getTok().is(AsmToken::Minus);
4087 const MCExpr *Offset;
4088 if (getParser().ParseExpression(Offset))
4091 // The expression has to be a constant. Memory references with relocations
4092 // don't come through here, as they use the <label> forms of the relevant
4094 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
4096 return Error (E, "constant expression expected");
4098 // If the constant was #-0, represent it as INT32_MIN.
4099 int32_t Val = CE->getValue();
4100 if (isNegative && Val == 0)
4101 CE = MCConstantExpr::Create(INT32_MIN, getContext());
4103 // Now we should have the closing ']'
4104 E = Parser.getTok().getLoc();
4105 if (Parser.getTok().isNot(AsmToken::RBrac))
4106 return Error(E, "']' expected");
4107 Parser.Lex(); // Eat right bracket token.
4109 // Don't worry about range checking the value here. That's handled by
4110 // the is*() predicates.
4111 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, CE, 0,
4112 ARM_AM::no_shift, 0, 0,
4115 // If there's a pre-indexing writeback marker, '!', just add it as a token
4117 if (Parser.getTok().is(AsmToken::Exclaim)) {
4118 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
4119 Parser.Lex(); // Eat the '!'.
4125 // The register offset is optionally preceded by a '+' or '-'
4126 bool isNegative = false;
4127 if (Parser.getTok().is(AsmToken::Minus)) {
4129 Parser.Lex(); // Eat the '-'.
4130 } else if (Parser.getTok().is(AsmToken::Plus)) {
4132 Parser.Lex(); // Eat the '+'.
4135 E = Parser.getTok().getLoc();
4136 int OffsetRegNum = tryParseRegister();
4137 if (OffsetRegNum == -1)
4138 return Error(E, "register expected");
4140 // If there's a shift operator, handle it.
4141 ARM_AM::ShiftOpc ShiftType = ARM_AM::no_shift;
4142 unsigned ShiftImm = 0;
4143 if (Parser.getTok().is(AsmToken::Comma)) {
4144 Parser.Lex(); // Eat the ','.
4145 if (parseMemRegOffsetShift(ShiftType, ShiftImm))
4149 // Now we should have the closing ']'
4150 E = Parser.getTok().getLoc();
4151 if (Parser.getTok().isNot(AsmToken::RBrac))
4152 return Error(E, "']' expected");
4153 Parser.Lex(); // Eat right bracket token.
4155 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, OffsetRegNum,
4156 ShiftType, ShiftImm, 0, isNegative,
4159 // If there's a pre-indexing writeback marker, '!', just add it as a token
4161 if (Parser.getTok().is(AsmToken::Exclaim)) {
4162 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
4163 Parser.Lex(); // Eat the '!'.
4169 /// parseMemRegOffsetShift - one of these two:
4170 /// ( lsl | lsr | asr | ror ) , # shift_amount
4172 /// return true if it parses a shift otherwise it returns false.
4173 bool ARMAsmParser::parseMemRegOffsetShift(ARM_AM::ShiftOpc &St,
4175 SMLoc Loc = Parser.getTok().getLoc();
4176 const AsmToken &Tok = Parser.getTok();
4177 if (Tok.isNot(AsmToken::Identifier))
4179 StringRef ShiftName = Tok.getString();
4180 if (ShiftName == "lsl" || ShiftName == "LSL" ||
4181 ShiftName == "asl" || ShiftName == "ASL")
4183 else if (ShiftName == "lsr" || ShiftName == "LSR")
4185 else if (ShiftName == "asr" || ShiftName == "ASR")
4187 else if (ShiftName == "ror" || ShiftName == "ROR")
4189 else if (ShiftName == "rrx" || ShiftName == "RRX")
4192 return Error(Loc, "illegal shift operator");
4193 Parser.Lex(); // Eat shift type token.
4195 // rrx stands alone.
4197 if (St != ARM_AM::rrx) {
4198 Loc = Parser.getTok().getLoc();
4199 // A '#' and a shift amount.
4200 const AsmToken &HashTok = Parser.getTok();
4201 if (HashTok.isNot(AsmToken::Hash) &&
4202 HashTok.isNot(AsmToken::Dollar))
4203 return Error(HashTok.getLoc(), "'#' expected");
4204 Parser.Lex(); // Eat hash token.
4207 if (getParser().ParseExpression(Expr))
4209 // Range check the immediate.
4210 // lsl, ror: 0 <= imm <= 31
4211 // lsr, asr: 0 <= imm <= 32
4212 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
4214 return Error(Loc, "shift amount must be an immediate");
4215 int64_t Imm = CE->getValue();
4217 ((St == ARM_AM::lsl || St == ARM_AM::ror) && Imm > 31) ||
4218 ((St == ARM_AM::lsr || St == ARM_AM::asr) && Imm > 32))
4219 return Error(Loc, "immediate shift value out of range");
4226 /// parseFPImm - A floating point immediate expression operand.
4227 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
4228 parseFPImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
4229 // Anything that can accept a floating point constant as an operand
4230 // needs to go through here, as the regular ParseExpression is
4233 // This routine still creates a generic Immediate operand, containing
4234 // a bitcast of the 64-bit floating point value. The various operands
4235 // that accept floats can check whether the value is valid for them
4236 // via the standard is*() predicates.
4238 SMLoc S = Parser.getTok().getLoc();
4240 if (Parser.getTok().isNot(AsmToken::Hash) &&
4241 Parser.getTok().isNot(AsmToken::Dollar))
4242 return MatchOperand_NoMatch;
4244 // Disambiguate the VMOV forms that can accept an FP immediate.
4245 // vmov.f32 <sreg>, #imm
4246 // vmov.f64 <dreg>, #imm
4247 // vmov.f32 <dreg>, #imm @ vector f32x2
4248 // vmov.f32 <qreg>, #imm @ vector f32x4
4250 // There are also the NEON VMOV instructions which expect an
4251 // integer constant. Make sure we don't try to parse an FPImm
4253 // vmov.i{8|16|32|64} <dreg|qreg>, #imm
4254 ARMOperand *TyOp = static_cast<ARMOperand*>(Operands[2]);
4255 if (!TyOp->isToken() || (TyOp->getToken() != ".f32" &&
4256 TyOp->getToken() != ".f64"))
4257 return MatchOperand_NoMatch;
4259 Parser.Lex(); // Eat the '#'.
4261 // Handle negation, as that still comes through as a separate token.
4262 bool isNegative = false;
4263 if (Parser.getTok().is(AsmToken::Minus)) {
4267 const AsmToken &Tok = Parser.getTok();
4268 SMLoc Loc = Tok.getLoc();
4269 if (Tok.is(AsmToken::Real)) {
4270 APFloat RealVal(APFloat::IEEEsingle, Tok.getString());
4271 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
4272 // If we had a '-' in front, toggle the sign bit.
4273 IntVal ^= (uint64_t)isNegative << 31;
4274 Parser.Lex(); // Eat the token.
4275 Operands.push_back(ARMOperand::CreateImm(
4276 MCConstantExpr::Create(IntVal, getContext()),
4277 S, Parser.getTok().getLoc()));
4278 return MatchOperand_Success;
4280 // Also handle plain integers. Instructions which allow floating point
4281 // immediates also allow a raw encoded 8-bit value.
4282 if (Tok.is(AsmToken::Integer)) {
4283 int64_t Val = Tok.getIntVal();
4284 Parser.Lex(); // Eat the token.
4285 if (Val > 255 || Val < 0) {
4286 Error(Loc, "encoded floating point value out of range");
4287 return MatchOperand_ParseFail;
4289 double RealVal = ARM_AM::getFPImmFloat(Val);
4290 Val = APFloat(APFloat::IEEEdouble, RealVal).bitcastToAPInt().getZExtValue();
4291 Operands.push_back(ARMOperand::CreateImm(
4292 MCConstantExpr::Create(Val, getContext()), S,
4293 Parser.getTok().getLoc()));
4294 return MatchOperand_Success;
4297 Error(Loc, "invalid floating point immediate");
4298 return MatchOperand_ParseFail;
4301 /// Parse a arm instruction operand. For now this parses the operand regardless
4302 /// of the mnemonic.
4303 bool ARMAsmParser::parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
4304 StringRef Mnemonic) {
4307 // Check if the current operand has a custom associated parser, if so, try to
4308 // custom parse the operand, or fallback to the general approach.
4309 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
4310 if (ResTy == MatchOperand_Success)
4312 // If there wasn't a custom match, try the generic matcher below. Otherwise,
4313 // there was a match, but an error occurred, in which case, just return that
4314 // the operand parsing failed.
4315 if (ResTy == MatchOperand_ParseFail)
4318 switch (getLexer().getKind()) {
4320 Error(Parser.getTok().getLoc(), "unexpected token in operand");
4322 case AsmToken::Identifier: {
4323 if (!tryParseRegisterWithWriteBack(Operands))
4325 int Res = tryParseShiftRegister(Operands);
4326 if (Res == 0) // success
4328 else if (Res == -1) // irrecoverable error
4330 // If this is VMRS, check for the apsr_nzcv operand.
4331 if (Mnemonic == "vmrs" && Parser.getTok().getString() == "apsr_nzcv") {
4332 S = Parser.getTok().getLoc();
4334 Operands.push_back(ARMOperand::CreateToken("apsr_nzcv", S));
4338 // Fall though for the Identifier case that is not a register or a
4341 case AsmToken::LParen: // parenthesized expressions like (_strcmp-4)
4342 case AsmToken::Integer: // things like 1f and 2b as a branch targets
4343 case AsmToken::String: // quoted label names.
4344 case AsmToken::Dot: { // . as a branch target
4345 // This was not a register so parse other operands that start with an
4346 // identifier (like labels) as expressions and create them as immediates.
4347 const MCExpr *IdVal;
4348 S = Parser.getTok().getLoc();
4349 if (getParser().ParseExpression(IdVal))
4351 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
4352 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
4355 case AsmToken::LBrac:
4356 return parseMemory(Operands);
4357 case AsmToken::LCurly:
4358 return parseRegisterList(Operands);
4359 case AsmToken::Dollar:
4360 case AsmToken::Hash: {
4361 // #42 -> immediate.
4362 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
4363 S = Parser.getTok().getLoc();
4365 bool isNegative = Parser.getTok().is(AsmToken::Minus);
4366 const MCExpr *ImmVal;
4367 if (getParser().ParseExpression(ImmVal))
4369 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ImmVal);
4371 int32_t Val = CE->getValue();
4372 if (isNegative && Val == 0)
4373 ImmVal = MCConstantExpr::Create(INT32_MIN, getContext());
4375 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
4376 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
4379 case AsmToken::Colon: {
4380 // ":lower16:" and ":upper16:" expression prefixes
4381 // FIXME: Check it's an expression prefix,
4382 // e.g. (FOO - :lower16:BAR) isn't legal.
4383 ARMMCExpr::VariantKind RefKind;
4384 if (parsePrefix(RefKind))
4387 const MCExpr *SubExprVal;
4388 if (getParser().ParseExpression(SubExprVal))
4391 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
4393 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
4394 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
4400 // parsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
4401 // :lower16: and :upper16:.
4402 bool ARMAsmParser::parsePrefix(ARMMCExpr::VariantKind &RefKind) {
4403 RefKind = ARMMCExpr::VK_ARM_None;
4405 // :lower16: and :upper16: modifiers
4406 assert(getLexer().is(AsmToken::Colon) && "expected a :");
4407 Parser.Lex(); // Eat ':'
4409 if (getLexer().isNot(AsmToken::Identifier)) {
4410 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
4414 StringRef IDVal = Parser.getTok().getIdentifier();
4415 if (IDVal == "lower16") {
4416 RefKind = ARMMCExpr::VK_ARM_LO16;
4417 } else if (IDVal == "upper16") {
4418 RefKind = ARMMCExpr::VK_ARM_HI16;
4420 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
4425 if (getLexer().isNot(AsmToken::Colon)) {
4426 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
4429 Parser.Lex(); // Eat the last ':'
4433 /// \brief Given a mnemonic, split out possible predication code and carry
4434 /// setting letters to form a canonical mnemonic and flags.
4436 // FIXME: Would be nice to autogen this.
4437 // FIXME: This is a bit of a maze of special cases.
4438 StringRef ARMAsmParser::splitMnemonic(StringRef Mnemonic,
4439 unsigned &PredicationCode,
4441 unsigned &ProcessorIMod,
4442 StringRef &ITMask) {
4443 PredicationCode = ARMCC::AL;
4444 CarrySetting = false;
4447 // Ignore some mnemonics we know aren't predicated forms.
4449 // FIXME: Would be nice to autogen this.
4450 if ((Mnemonic == "movs" && isThumb()) ||
4451 Mnemonic == "teq" || Mnemonic == "vceq" || Mnemonic == "svc" ||
4452 Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
4453 Mnemonic == "vmls" || Mnemonic == "vnmls" || Mnemonic == "vacge" ||
4454 Mnemonic == "vcge" || Mnemonic == "vclt" || Mnemonic == "vacgt" ||
4455 Mnemonic == "vcgt" || Mnemonic == "vcle" || Mnemonic == "smlal" ||
4456 Mnemonic == "umaal" || Mnemonic == "umlal" || Mnemonic == "vabal" ||
4457 Mnemonic == "vmlal" || Mnemonic == "vpadal" || Mnemonic == "vqdmlal" ||
4458 Mnemonic == "fmuls")
4461 // First, split out any predication code. Ignore mnemonics we know aren't
4462 // predicated but do have a carry-set and so weren't caught above.
4463 if (Mnemonic != "adcs" && Mnemonic != "bics" && Mnemonic != "movs" &&
4464 Mnemonic != "muls" && Mnemonic != "smlals" && Mnemonic != "smulls" &&
4465 Mnemonic != "umlals" && Mnemonic != "umulls" && Mnemonic != "lsls" &&
4466 Mnemonic != "sbcs" && Mnemonic != "rscs") {
4467 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
4468 .Case("eq", ARMCC::EQ)
4469 .Case("ne", ARMCC::NE)
4470 .Case("hs", ARMCC::HS)
4471 .Case("cs", ARMCC::HS)
4472 .Case("lo", ARMCC::LO)
4473 .Case("cc", ARMCC::LO)
4474 .Case("mi", ARMCC::MI)
4475 .Case("pl", ARMCC::PL)
4476 .Case("vs", ARMCC::VS)
4477 .Case("vc", ARMCC::VC)
4478 .Case("hi", ARMCC::HI)
4479 .Case("ls", ARMCC::LS)
4480 .Case("ge", ARMCC::GE)
4481 .Case("lt", ARMCC::LT)
4482 .Case("gt", ARMCC::GT)
4483 .Case("le", ARMCC::LE)
4484 .Case("al", ARMCC::AL)
4487 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
4488 PredicationCode = CC;
4492 // Next, determine if we have a carry setting bit. We explicitly ignore all
4493 // the instructions we know end in 's'.
4494 if (Mnemonic.endswith("s") &&
4495 !(Mnemonic == "cps" || Mnemonic == "mls" ||
4496 Mnemonic == "mrs" || Mnemonic == "smmls" || Mnemonic == "vabs" ||
4497 Mnemonic == "vcls" || Mnemonic == "vmls" || Mnemonic == "vmrs" ||
4498 Mnemonic == "vnmls" || Mnemonic == "vqabs" || Mnemonic == "vrecps" ||
4499 Mnemonic == "vrsqrts" || Mnemonic == "srs" || Mnemonic == "flds" ||
4500 Mnemonic == "fmrs" || Mnemonic == "fsqrts" || Mnemonic == "fsubs" ||
4501 Mnemonic == "fsts" || Mnemonic == "fcpys" || Mnemonic == "fdivs" ||
4502 Mnemonic == "fmuls" || Mnemonic == "fcmps" ||
4503 (Mnemonic == "movs" && isThumb()))) {
4504 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
4505 CarrySetting = true;
4508 // The "cps" instruction can have a interrupt mode operand which is glued into
4509 // the mnemonic. Check if this is the case, split it and parse the imod op
4510 if (Mnemonic.startswith("cps")) {
4511 // Split out any imod code.
4513 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
4514 .Case("ie", ARM_PROC::IE)
4515 .Case("id", ARM_PROC::ID)
4518 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
4519 ProcessorIMod = IMod;
4523 // The "it" instruction has the condition mask on the end of the mnemonic.
4524 if (Mnemonic.startswith("it")) {
4525 ITMask = Mnemonic.slice(2, Mnemonic.size());
4526 Mnemonic = Mnemonic.slice(0, 2);
4532 /// \brief Given a canonical mnemonic, determine if the instruction ever allows
4533 /// inclusion of carry set or predication code operands.
4535 // FIXME: It would be nice to autogen this.
4537 getMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
4538 bool &CanAcceptPredicationCode) {
4539 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
4540 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
4541 Mnemonic == "add" || Mnemonic == "adc" ||
4542 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
4543 Mnemonic == "orr" || Mnemonic == "mvn" ||
4544 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
4545 Mnemonic == "sbc" || Mnemonic == "eor" || Mnemonic == "neg" ||
4546 (!isThumb() && (Mnemonic == "smull" || Mnemonic == "mov" ||
4547 Mnemonic == "mla" || Mnemonic == "smlal" ||
4548 Mnemonic == "umlal" || Mnemonic == "umull"))) {
4549 CanAcceptCarrySet = true;
4551 CanAcceptCarrySet = false;
4553 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
4554 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
4555 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
4556 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
4557 Mnemonic == "dsb" || Mnemonic == "isb" || Mnemonic == "setend" ||
4558 (Mnemonic == "clrex" && !isThumb()) ||
4559 (Mnemonic == "nop" && isThumbOne()) ||
4560 ((Mnemonic == "pld" || Mnemonic == "pli" || Mnemonic == "pldw" ||
4561 Mnemonic == "ldc2" || Mnemonic == "ldc2l" ||
4562 Mnemonic == "stc2" || Mnemonic == "stc2l") && !isThumb()) ||
4563 ((Mnemonic.startswith("rfe") || Mnemonic.startswith("srs")) &&
4565 Mnemonic.startswith("cps") || (Mnemonic == "movs" && isThumbOne())) {
4566 CanAcceptPredicationCode = false;
4568 CanAcceptPredicationCode = true;
4571 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
4572 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
4573 CanAcceptPredicationCode = false;
4577 bool ARMAsmParser::shouldOmitCCOutOperand(StringRef Mnemonic,
4578 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
4579 // FIXME: This is all horribly hacky. We really need a better way to deal
4580 // with optional operands like this in the matcher table.
4582 // The 'mov' mnemonic is special. One variant has a cc_out operand, while
4583 // another does not. Specifically, the MOVW instruction does not. So we
4584 // special case it here and remove the defaulted (non-setting) cc_out
4585 // operand if that's the instruction we're trying to match.
4587 // We do this as post-processing of the explicit operands rather than just
4588 // conditionally adding the cc_out in the first place because we need
4589 // to check the type of the parsed immediate operand.
4590 if (Mnemonic == "mov" && Operands.size() > 4 && !isThumb() &&
4591 !static_cast<ARMOperand*>(Operands[4])->isARMSOImm() &&
4592 static_cast<ARMOperand*>(Operands[4])->isImm0_65535Expr() &&
4593 static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
4596 // Register-register 'add' for thumb does not have a cc_out operand
4597 // when there are only two register operands.
4598 if (isThumb() && Mnemonic == "add" && Operands.size() == 5 &&
4599 static_cast<ARMOperand*>(Operands[3])->isReg() &&
4600 static_cast<ARMOperand*>(Operands[4])->isReg() &&
4601 static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
4603 // Register-register 'add' for thumb does not have a cc_out operand
4604 // when it's an ADD Rdm, SP, {Rdm|#imm0_255} instruction. We do
4605 // have to check the immediate range here since Thumb2 has a variant
4606 // that can handle a different range and has a cc_out operand.
4607 if (((isThumb() && Mnemonic == "add") ||
4608 (isThumbTwo() && Mnemonic == "sub")) &&
4609 Operands.size() == 6 &&
4610 static_cast<ARMOperand*>(Operands[3])->isReg() &&
4611 static_cast<ARMOperand*>(Operands[4])->isReg() &&
4612 static_cast<ARMOperand*>(Operands[4])->getReg() == ARM::SP &&
4613 static_cast<ARMOperand*>(Operands[1])->getReg() == 0 &&
4614 (static_cast<ARMOperand*>(Operands[5])->isReg() ||
4615 static_cast<ARMOperand*>(Operands[5])->isImm0_1020s4()))
4617 // For Thumb2, add/sub immediate does not have a cc_out operand for the
4618 // imm0_4095 variant. That's the least-preferred variant when
4619 // selecting via the generic "add" mnemonic, so to know that we
4620 // should remove the cc_out operand, we have to explicitly check that
4621 // it's not one of the other variants. Ugh.
4622 if (isThumbTwo() && (Mnemonic == "add" || Mnemonic == "sub") &&
4623 Operands.size() == 6 &&
4624 static_cast<ARMOperand*>(Operands[3])->isReg() &&
4625 static_cast<ARMOperand*>(Operands[4])->isReg() &&
4626 static_cast<ARMOperand*>(Operands[5])->isImm()) {
4627 // Nest conditions rather than one big 'if' statement for readability.
4629 // If either register is a high reg, it's either one of the SP
4630 // variants (handled above) or a 32-bit encoding, so we just
4631 // check against T3. If the second register is the PC, this is an
4632 // alternate form of ADR, which uses encoding T4, so check for that too.
4633 if ((!isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) ||
4634 !isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg())) &&
4635 static_cast<ARMOperand*>(Operands[4])->getReg() != ARM::PC &&
4636 static_cast<ARMOperand*>(Operands[5])->isT2SOImm())
4638 // If both registers are low, we're in an IT block, and the immediate is
4639 // in range, we should use encoding T1 instead, which has a cc_out.
4641 isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) &&
4642 isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg()) &&
4643 static_cast<ARMOperand*>(Operands[5])->isImm0_7())
4646 // Otherwise, we use encoding T4, which does not have a cc_out
4651 // The thumb2 multiply instruction doesn't have a CCOut register, so
4652 // if we have a "mul" mnemonic in Thumb mode, check if we'll be able to
4653 // use the 16-bit encoding or not.
4654 if (isThumbTwo() && Mnemonic == "mul" && Operands.size() == 6 &&
4655 static_cast<ARMOperand*>(Operands[1])->getReg() == 0 &&
4656 static_cast<ARMOperand*>(Operands[3])->isReg() &&
4657 static_cast<ARMOperand*>(Operands[4])->isReg() &&
4658 static_cast<ARMOperand*>(Operands[5])->isReg() &&
4659 // If the registers aren't low regs, the destination reg isn't the
4660 // same as one of the source regs, or the cc_out operand is zero
4661 // outside of an IT block, we have to use the 32-bit encoding, so
4662 // remove the cc_out operand.
4663 (!isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) ||
4664 !isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg()) ||
4665 !isARMLowRegister(static_cast<ARMOperand*>(Operands[5])->getReg()) ||
4667 (static_cast<ARMOperand*>(Operands[3])->getReg() !=
4668 static_cast<ARMOperand*>(Operands[5])->getReg() &&
4669 static_cast<ARMOperand*>(Operands[3])->getReg() !=
4670 static_cast<ARMOperand*>(Operands[4])->getReg())))
4673 // Also check the 'mul' syntax variant that doesn't specify an explicit
4674 // destination register.
4675 if (isThumbTwo() && Mnemonic == "mul" && Operands.size() == 5 &&
4676 static_cast<ARMOperand*>(Operands[1])->getReg() == 0 &&
4677 static_cast<ARMOperand*>(Operands[3])->isReg() &&
4678 static_cast<ARMOperand*>(Operands[4])->isReg() &&
4679 // If the registers aren't low regs or the cc_out operand is zero
4680 // outside of an IT block, we have to use the 32-bit encoding, so
4681 // remove the cc_out operand.
4682 (!isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) ||
4683 !isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg()) ||
4689 // Register-register 'add/sub' for thumb does not have a cc_out operand
4690 // when it's an ADD/SUB SP, #imm. Be lenient on count since there's also
4691 // the "add/sub SP, SP, #imm" version. If the follow-up operands aren't
4692 // right, this will result in better diagnostics (which operand is off)
4694 if (isThumb() && (Mnemonic == "add" || Mnemonic == "sub") &&
4695 (Operands.size() == 5 || Operands.size() == 6) &&
4696 static_cast<ARMOperand*>(Operands[3])->isReg() &&
4697 static_cast<ARMOperand*>(Operands[3])->getReg() == ARM::SP &&
4698 static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
4704 static bool isDataTypeToken(StringRef Tok) {
4705 return Tok == ".8" || Tok == ".16" || Tok == ".32" || Tok == ".64" ||
4706 Tok == ".i8" || Tok == ".i16" || Tok == ".i32" || Tok == ".i64" ||
4707 Tok == ".u8" || Tok == ".u16" || Tok == ".u32" || Tok == ".u64" ||
4708 Tok == ".s8" || Tok == ".s16" || Tok == ".s32" || Tok == ".s64" ||
4709 Tok == ".p8" || Tok == ".p16" || Tok == ".f32" || Tok == ".f64" ||
4710 Tok == ".f" || Tok == ".d";
4713 // FIXME: This bit should probably be handled via an explicit match class
4714 // in the .td files that matches the suffix instead of having it be
4715 // a literal string token the way it is now.
4716 static bool doesIgnoreDataTypeSuffix(StringRef Mnemonic, StringRef DT) {
4717 return Mnemonic.startswith("vldm") || Mnemonic.startswith("vstm");
4720 static void applyMnemonicAliases(StringRef &Mnemonic, unsigned Features);
4721 /// Parse an arm instruction mnemonic followed by its operands.
4722 bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
4723 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
4724 // Apply mnemonic aliases before doing anything else, as the destination
4725 // mnemnonic may include suffices and we want to handle them normally.
4726 // The generic tblgen'erated code does this later, at the start of
4727 // MatchInstructionImpl(), but that's too late for aliases that include
4728 // any sort of suffix.
4729 unsigned AvailableFeatures = getAvailableFeatures();
4730 applyMnemonicAliases(Name, AvailableFeatures);
4732 // First check for the ARM-specific .req directive.
4733 if (Parser.getTok().is(AsmToken::Identifier) &&
4734 Parser.getTok().getIdentifier() == ".req") {
4735 parseDirectiveReq(Name, NameLoc);
4736 // We always return 'error' for this, as we're done with this
4737 // statement and don't need to match the 'instruction."
4741 // Create the leading tokens for the mnemonic, split by '.' characters.
4742 size_t Start = 0, Next = Name.find('.');
4743 StringRef Mnemonic = Name.slice(Start, Next);
4745 // Split out the predication code and carry setting flag from the mnemonic.
4746 unsigned PredicationCode;
4747 unsigned ProcessorIMod;
4750 Mnemonic = splitMnemonic(Mnemonic, PredicationCode, CarrySetting,
4751 ProcessorIMod, ITMask);
4753 // In Thumb1, only the branch (B) instruction can be predicated.
4754 if (isThumbOne() && PredicationCode != ARMCC::AL && Mnemonic != "b") {
4755 Parser.EatToEndOfStatement();
4756 return Error(NameLoc, "conditional execution not supported in Thumb1");
4759 Operands.push_back(ARMOperand::CreateToken(Mnemonic, NameLoc));
4761 // Handle the IT instruction ITMask. Convert it to a bitmask. This
4762 // is the mask as it will be for the IT encoding if the conditional
4763 // encoding has a '1' as it's bit0 (i.e. 't' ==> '1'). In the case
4764 // where the conditional bit0 is zero, the instruction post-processing
4765 // will adjust the mask accordingly.
4766 if (Mnemonic == "it") {
4767 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + 2);
4768 if (ITMask.size() > 3) {
4769 Parser.EatToEndOfStatement();
4770 return Error(Loc, "too many conditions on IT instruction");
4773 for (unsigned i = ITMask.size(); i != 0; --i) {
4774 char pos = ITMask[i - 1];
4775 if (pos != 't' && pos != 'e') {
4776 Parser.EatToEndOfStatement();
4777 return Error(Loc, "illegal IT block condition mask '" + ITMask + "'");
4780 if (ITMask[i - 1] == 't')
4783 Operands.push_back(ARMOperand::CreateITMask(Mask, Loc));
4786 // FIXME: This is all a pretty gross hack. We should automatically handle
4787 // optional operands like this via tblgen.
4789 // Next, add the CCOut and ConditionCode operands, if needed.
4791 // For mnemonics which can ever incorporate a carry setting bit or predication
4792 // code, our matching model involves us always generating CCOut and
4793 // ConditionCode operands to match the mnemonic "as written" and then we let
4794 // the matcher deal with finding the right instruction or generating an
4795 // appropriate error.
4796 bool CanAcceptCarrySet, CanAcceptPredicationCode;
4797 getMnemonicAcceptInfo(Mnemonic, CanAcceptCarrySet, CanAcceptPredicationCode);
4799 // If we had a carry-set on an instruction that can't do that, issue an
4801 if (!CanAcceptCarrySet && CarrySetting) {
4802 Parser.EatToEndOfStatement();
4803 return Error(NameLoc, "instruction '" + Mnemonic +
4804 "' can not set flags, but 's' suffix specified");
4806 // If we had a predication code on an instruction that can't do that, issue an
4808 if (!CanAcceptPredicationCode && PredicationCode != ARMCC::AL) {
4809 Parser.EatToEndOfStatement();
4810 return Error(NameLoc, "instruction '" + Mnemonic +
4811 "' is not predicable, but condition code specified");
4814 // Add the carry setting operand, if necessary.
4815 if (CanAcceptCarrySet) {
4816 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size());
4817 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
4821 // Add the predication code operand, if necessary.
4822 if (CanAcceptPredicationCode) {
4823 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size() +
4825 Operands.push_back(ARMOperand::CreateCondCode(
4826 ARMCC::CondCodes(PredicationCode), Loc));
4829 // Add the processor imod operand, if necessary.
4830 if (ProcessorIMod) {
4831 Operands.push_back(ARMOperand::CreateImm(
4832 MCConstantExpr::Create(ProcessorIMod, getContext()),
4836 // Add the remaining tokens in the mnemonic.
4837 while (Next != StringRef::npos) {
4839 Next = Name.find('.', Start + 1);
4840 StringRef ExtraToken = Name.slice(Start, Next);
4842 // Some NEON instructions have an optional datatype suffix that is
4843 // completely ignored. Check for that.
4844 if (isDataTypeToken(ExtraToken) &&
4845 doesIgnoreDataTypeSuffix(Mnemonic, ExtraToken))
4848 if (ExtraToken != ".n") {
4849 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Start);
4850 Operands.push_back(ARMOperand::CreateToken(ExtraToken, Loc));
4854 // Read the remaining operands.
4855 if (getLexer().isNot(AsmToken::EndOfStatement)) {
4856 // Read the first operand.
4857 if (parseOperand(Operands, Mnemonic)) {
4858 Parser.EatToEndOfStatement();
4862 while (getLexer().is(AsmToken::Comma)) {
4863 Parser.Lex(); // Eat the comma.
4865 // Parse and remember the operand.
4866 if (parseOperand(Operands, Mnemonic)) {
4867 Parser.EatToEndOfStatement();
4873 if (getLexer().isNot(AsmToken::EndOfStatement)) {
4874 SMLoc Loc = getLexer().getLoc();
4875 Parser.EatToEndOfStatement();
4876 return Error(Loc, "unexpected token in argument list");
4879 Parser.Lex(); // Consume the EndOfStatement
4881 // Some instructions, mostly Thumb, have forms for the same mnemonic that
4882 // do and don't have a cc_out optional-def operand. With some spot-checks
4883 // of the operand list, we can figure out which variant we're trying to
4884 // parse and adjust accordingly before actually matching. We shouldn't ever
4885 // try to remove a cc_out operand that was explicitly set on the the
4886 // mnemonic, of course (CarrySetting == true). Reason number #317 the
4887 // table driven matcher doesn't fit well with the ARM instruction set.
4888 if (!CarrySetting && shouldOmitCCOutOperand(Mnemonic, Operands)) {
4889 ARMOperand *Op = static_cast<ARMOperand*>(Operands[1]);
4890 Operands.erase(Operands.begin() + 1);
4894 // ARM mode 'blx' need special handling, as the register operand version
4895 // is predicable, but the label operand version is not. So, we can't rely
4896 // on the Mnemonic based checking to correctly figure out when to put
4897 // a k_CondCode operand in the list. If we're trying to match the label
4898 // version, remove the k_CondCode operand here.
4899 if (!isThumb() && Mnemonic == "blx" && Operands.size() == 3 &&
4900 static_cast<ARMOperand*>(Operands[2])->isImm()) {
4901 ARMOperand *Op = static_cast<ARMOperand*>(Operands[1]);
4902 Operands.erase(Operands.begin() + 1);
4906 // The vector-compare-to-zero instructions have a literal token "#0" at
4907 // the end that comes to here as an immediate operand. Convert it to a
4908 // token to play nicely with the matcher.
4909 if ((Mnemonic == "vceq" || Mnemonic == "vcge" || Mnemonic == "vcgt" ||
4910 Mnemonic == "vcle" || Mnemonic == "vclt") && Operands.size() == 6 &&
4911 static_cast<ARMOperand*>(Operands[5])->isImm()) {
4912 ARMOperand *Op = static_cast<ARMOperand*>(Operands[5]);
4913 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
4914 if (CE && CE->getValue() == 0) {
4915 Operands.erase(Operands.begin() + 5);
4916 Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
4920 // VCMP{E} does the same thing, but with a different operand count.
4921 if ((Mnemonic == "vcmp" || Mnemonic == "vcmpe") && Operands.size() == 5 &&
4922 static_cast<ARMOperand*>(Operands[4])->isImm()) {
4923 ARMOperand *Op = static_cast<ARMOperand*>(Operands[4]);
4924 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
4925 if (CE && CE->getValue() == 0) {
4926 Operands.erase(Operands.begin() + 4);
4927 Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
4931 // Similarly, the Thumb1 "RSB" instruction has a literal "#0" on the
4932 // end. Convert it to a token here. Take care not to convert those
4933 // that should hit the Thumb2 encoding.
4934 if (Mnemonic == "rsb" && isThumb() && Operands.size() == 6 &&
4935 static_cast<ARMOperand*>(Operands[3])->isReg() &&
4936 static_cast<ARMOperand*>(Operands[4])->isReg() &&
4937 static_cast<ARMOperand*>(Operands[5])->isImm()) {
4938 ARMOperand *Op = static_cast<ARMOperand*>(Operands[5]);
4939 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
4940 if (CE && CE->getValue() == 0 &&
4942 // The cc_out operand matches the IT block.
4943 ((inITBlock() != CarrySetting) &&
4944 // Neither register operand is a high register.
4945 (isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) &&
4946 isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg()))))){
4947 Operands.erase(Operands.begin() + 5);
4948 Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
4956 // Validate context-sensitive operand constraints.
4958 // return 'true' if register list contains non-low GPR registers,
4959 // 'false' otherwise. If Reg is in the register list or is HiReg, set
4960 // 'containsReg' to true.
4961 static bool checkLowRegisterList(MCInst Inst, unsigned OpNo, unsigned Reg,
4962 unsigned HiReg, bool &containsReg) {
4963 containsReg = false;
4964 for (unsigned i = OpNo; i < Inst.getNumOperands(); ++i) {
4965 unsigned OpReg = Inst.getOperand(i).getReg();
4968 // Anything other than a low register isn't legal here.
4969 if (!isARMLowRegister(OpReg) && (!HiReg || OpReg != HiReg))
4975 // Check if the specified regisgter is in the register list of the inst,
4976 // starting at the indicated operand number.
4977 static bool listContainsReg(MCInst &Inst, unsigned OpNo, unsigned Reg) {
4978 for (unsigned i = OpNo; i < Inst.getNumOperands(); ++i) {
4979 unsigned OpReg = Inst.getOperand(i).getReg();
4986 // FIXME: We would really prefer to have MCInstrInfo (the wrapper around
4987 // the ARMInsts array) instead. Getting that here requires awkward
4988 // API changes, though. Better way?
4990 extern const MCInstrDesc ARMInsts[];
4992 static const MCInstrDesc &getInstDesc(unsigned Opcode) {
4993 return ARMInsts[Opcode];
4996 // FIXME: We would really like to be able to tablegen'erate this.
4998 validateInstruction(MCInst &Inst,
4999 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
5000 const MCInstrDesc &MCID = getInstDesc(Inst.getOpcode());
5001 SMLoc Loc = Operands[0]->getStartLoc();
5002 // Check the IT block state first.
5003 // NOTE: In Thumb mode, the BKPT instruction has the interesting property of
5004 // being allowed in IT blocks, but not being predicable. It just always
5006 if (inITBlock() && Inst.getOpcode() != ARM::tBKPT) {
5008 if (ITState.FirstCond)
5009 ITState.FirstCond = false;
5011 bit = (ITState.Mask >> (5 - ITState.CurPosition)) & 1;
5012 // The instruction must be predicable.
5013 if (!MCID.isPredicable())
5014 return Error(Loc, "instructions in IT block must be predicable");
5015 unsigned Cond = Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm();
5016 unsigned ITCond = bit ? ITState.Cond :
5017 ARMCC::getOppositeCondition(ITState.Cond);
5018 if (Cond != ITCond) {
5019 // Find the condition code Operand to get its SMLoc information.
5021 for (unsigned i = 1; i < Operands.size(); ++i)
5022 if (static_cast<ARMOperand*>(Operands[i])->isCondCode())
5023 CondLoc = Operands[i]->getStartLoc();
5024 return Error(CondLoc, "incorrect condition in IT block; got '" +
5025 StringRef(ARMCondCodeToString(ARMCC::CondCodes(Cond))) +
5026 "', but expected '" +
5027 ARMCondCodeToString(ARMCC::CondCodes(ITCond)) + "'");
5029 // Check for non-'al' condition codes outside of the IT block.
5030 } else if (isThumbTwo() && MCID.isPredicable() &&
5031 Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm() !=
5032 ARMCC::AL && Inst.getOpcode() != ARM::tB &&
5033 Inst.getOpcode() != ARM::t2B)
5034 return Error(Loc, "predicated instructions must be in IT block");
5036 switch (Inst.getOpcode()) {
5039 case ARM::LDRD_POST:
5041 // Rt2 must be Rt + 1.
5042 unsigned Rt = getARMRegisterNumbering(Inst.getOperand(0).getReg());
5043 unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(1).getReg());
5045 return Error(Operands[3]->getStartLoc(),
5046 "destination operands must be sequential");
5050 // Rt2 must be Rt + 1.
5051 unsigned Rt = getARMRegisterNumbering(Inst.getOperand(0).getReg());
5052 unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(1).getReg());
5054 return Error(Operands[3]->getStartLoc(),
5055 "source operands must be sequential");
5059 case ARM::STRD_POST:
5061 // Rt2 must be Rt + 1.
5062 unsigned Rt = getARMRegisterNumbering(Inst.getOperand(1).getReg());
5063 unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(2).getReg());
5065 return Error(Operands[3]->getStartLoc(),
5066 "source operands must be sequential");
5071 // width must be in range [1, 32-lsb]
5072 unsigned lsb = Inst.getOperand(2).getImm();
5073 unsigned widthm1 = Inst.getOperand(3).getImm();
5074 if (widthm1 >= 32 - lsb)
5075 return Error(Operands[5]->getStartLoc(),
5076 "bitfield width must be in range [1,32-lsb]");
5080 // If we're parsing Thumb2, the .w variant is available and handles
5081 // most cases that are normally illegal for a Thumb1 LDM
5082 // instruction. We'll make the transformation in processInstruction()
5085 // Thumb LDM instructions are writeback iff the base register is not
5086 // in the register list.
5087 unsigned Rn = Inst.getOperand(0).getReg();
5088 bool hasWritebackToken =
5089 (static_cast<ARMOperand*>(Operands[3])->isToken() &&
5090 static_cast<ARMOperand*>(Operands[3])->getToken() == "!");
5091 bool listContainsBase;
5092 if (checkLowRegisterList(Inst, 3, Rn, 0, listContainsBase) && !isThumbTwo())
5093 return Error(Operands[3 + hasWritebackToken]->getStartLoc(),
5094 "registers must be in range r0-r7");
5095 // If we should have writeback, then there should be a '!' token.
5096 if (!listContainsBase && !hasWritebackToken && !isThumbTwo())
5097 return Error(Operands[2]->getStartLoc(),
5098 "writeback operator '!' expected");
5099 // If we should not have writeback, there must not be a '!'. This is
5100 // true even for the 32-bit wide encodings.
5101 if (listContainsBase && hasWritebackToken)
5102 return Error(Operands[3]->getStartLoc(),
5103 "writeback operator '!' not allowed when base register "
5104 "in register list");
5108 case ARM::t2LDMIA_UPD: {
5109 if (listContainsReg(Inst, 3, Inst.getOperand(0).getReg()))
5110 return Error(Operands[4]->getStartLoc(),
5111 "writeback operator '!' not allowed when base register "
5112 "in register list");
5115 // Like for ldm/stm, push and pop have hi-reg handling version in Thumb2,
5116 // so only issue a diagnostic for thumb1. The instructions will be
5117 // switched to the t2 encodings in processInstruction() if necessary.
5119 bool listContainsBase;
5120 if (checkLowRegisterList(Inst, 2, 0, ARM::PC, listContainsBase) &&
5122 return Error(Operands[2]->getStartLoc(),
5123 "registers must be in range r0-r7 or pc");
5127 bool listContainsBase;
5128 if (checkLowRegisterList(Inst, 2, 0, ARM::LR, listContainsBase) &&
5130 return Error(Operands[2]->getStartLoc(),
5131 "registers must be in range r0-r7 or lr");
5134 case ARM::tSTMIA_UPD: {
5135 bool listContainsBase;
5136 if (checkLowRegisterList(Inst, 4, 0, 0, listContainsBase) && !isThumbTwo())
5137 return Error(Operands[4]->getStartLoc(),
5138 "registers must be in range r0-r7");
5146 static unsigned getRealVSTLNOpcode(unsigned Opc, unsigned &Spacing) {
5148 default: assert(0 && "unexpected opcode!");
5150 case ARM::VST1LNdWB_fixed_Asm_8:
5152 return ARM::VST1LNd8_UPD;
5153 case ARM::VST1LNdWB_fixed_Asm_16:
5155 return ARM::VST1LNd16_UPD;
5156 case ARM::VST1LNdWB_fixed_Asm_32:
5158 return ARM::VST1LNd32_UPD;
5159 case ARM::VST1LNdWB_register_Asm_8:
5161 return ARM::VST1LNd8_UPD;
5162 case ARM::VST1LNdWB_register_Asm_16:
5164 return ARM::VST1LNd16_UPD;
5165 case ARM::VST1LNdWB_register_Asm_32:
5167 return ARM::VST1LNd32_UPD;
5168 case ARM::VST1LNdAsm_8:
5170 return ARM::VST1LNd8;
5171 case ARM::VST1LNdAsm_16:
5173 return ARM::VST1LNd16;
5174 case ARM::VST1LNdAsm_32:
5176 return ARM::VST1LNd32;
5179 case ARM::VST2LNdWB_fixed_Asm_8:
5181 return ARM::VST2LNd8_UPD;
5182 case ARM::VST2LNdWB_fixed_Asm_16:
5184 return ARM::VST2LNd16_UPD;
5185 case ARM::VST2LNdWB_fixed_Asm_32:
5187 return ARM::VST2LNd32_UPD;
5188 case ARM::VST2LNqWB_fixed_Asm_16:
5190 return ARM::VST2LNq16_UPD;
5191 case ARM::VST2LNqWB_fixed_Asm_32:
5193 return ARM::VST2LNq32_UPD;
5195 case ARM::VST2LNdWB_register_Asm_8:
5197 return ARM::VST2LNd8_UPD;
5198 case ARM::VST2LNdWB_register_Asm_16:
5200 return ARM::VST2LNd16_UPD;
5201 case ARM::VST2LNdWB_register_Asm_32:
5203 return ARM::VST2LNd32_UPD;
5204 case ARM::VST2LNqWB_register_Asm_16:
5206 return ARM::VST2LNq16_UPD;
5207 case ARM::VST2LNqWB_register_Asm_32:
5209 return ARM::VST2LNq32_UPD;
5211 case ARM::VST2LNdAsm_8:
5213 return ARM::VST2LNd8;
5214 case ARM::VST2LNdAsm_16:
5216 return ARM::VST2LNd16;
5217 case ARM::VST2LNdAsm_32:
5219 return ARM::VST2LNd32;
5220 case ARM::VST2LNqAsm_16:
5222 return ARM::VST2LNq16;
5223 case ARM::VST2LNqAsm_32:
5225 return ARM::VST2LNq32;
5229 static unsigned getRealVLDLNOpcode(unsigned Opc, unsigned &Spacing) {
5231 default: assert(0 && "unexpected opcode!");
5233 case ARM::VLD1LNdWB_fixed_Asm_8:
5235 return ARM::VLD1LNd8_UPD;
5236 case ARM::VLD1LNdWB_fixed_Asm_16:
5238 return ARM::VLD1LNd16_UPD;
5239 case ARM::VLD1LNdWB_fixed_Asm_32:
5241 return ARM::VLD1LNd32_UPD;
5242 case ARM::VLD1LNdWB_register_Asm_8:
5244 return ARM::VLD1LNd8_UPD;
5245 case ARM::VLD1LNdWB_register_Asm_16:
5247 return ARM::VLD1LNd16_UPD;
5248 case ARM::VLD1LNdWB_register_Asm_32:
5250 return ARM::VLD1LNd32_UPD;
5251 case ARM::VLD1LNdAsm_8:
5253 return ARM::VLD1LNd8;
5254 case ARM::VLD1LNdAsm_16:
5256 return ARM::VLD1LNd16;
5257 case ARM::VLD1LNdAsm_32:
5259 return ARM::VLD1LNd32;
5262 case ARM::VLD2LNdWB_fixed_Asm_8:
5264 return ARM::VLD2LNd8_UPD;
5265 case ARM::VLD2LNdWB_fixed_Asm_16:
5267 return ARM::VLD2LNd16_UPD;
5268 case ARM::VLD2LNdWB_fixed_Asm_32:
5270 return ARM::VLD2LNd32_UPD;
5271 case ARM::VLD2LNqWB_fixed_Asm_16:
5273 return ARM::VLD2LNq16_UPD;
5274 case ARM::VLD2LNqWB_fixed_Asm_32:
5276 return ARM::VLD2LNq32_UPD;
5277 case ARM::VLD2LNdWB_register_Asm_8:
5279 return ARM::VLD2LNd8_UPD;
5280 case ARM::VLD2LNdWB_register_Asm_16:
5282 return ARM::VLD2LNd16_UPD;
5283 case ARM::VLD2LNdWB_register_Asm_32:
5285 return ARM::VLD2LNd32_UPD;
5286 case ARM::VLD2LNqWB_register_Asm_16:
5288 return ARM::VLD2LNq16_UPD;
5289 case ARM::VLD2LNqWB_register_Asm_32:
5291 return ARM::VLD2LNq32_UPD;
5292 case ARM::VLD2LNdAsm_8:
5294 return ARM::VLD2LNd8;
5295 case ARM::VLD2LNdAsm_16:
5297 return ARM::VLD2LNd16;
5298 case ARM::VLD2LNdAsm_32:
5300 return ARM::VLD2LNd32;
5301 case ARM::VLD2LNqAsm_16:
5303 return ARM::VLD2LNq16;
5304 case ARM::VLD2LNqAsm_32:
5306 return ARM::VLD2LNq32;
5311 processInstruction(MCInst &Inst,
5312 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
5313 switch (Inst.getOpcode()) {
5314 // Aliases for alternate PC+imm syntax of LDR instructions.
5315 case ARM::t2LDRpcrel:
5316 Inst.setOpcode(ARM::t2LDRpci);
5318 case ARM::t2LDRBpcrel:
5319 Inst.setOpcode(ARM::t2LDRBpci);
5321 case ARM::t2LDRHpcrel:
5322 Inst.setOpcode(ARM::t2LDRHpci);
5324 case ARM::t2LDRSBpcrel:
5325 Inst.setOpcode(ARM::t2LDRSBpci);
5327 case ARM::t2LDRSHpcrel:
5328 Inst.setOpcode(ARM::t2LDRSHpci);
5330 // Handle NEON VST complex aliases.
5331 case ARM::VST1LNdWB_register_Asm_8:
5332 case ARM::VST1LNdWB_register_Asm_16:
5333 case ARM::VST1LNdWB_register_Asm_32: {
5335 // Shuffle the operands around so the lane index operand is in the
5338 TmpInst.setOpcode(getRealVSTLNOpcode(Inst.getOpcode(), Spacing));
5339 TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
5340 TmpInst.addOperand(Inst.getOperand(2)); // Rn
5341 TmpInst.addOperand(Inst.getOperand(3)); // alignment
5342 TmpInst.addOperand(Inst.getOperand(4)); // Rm
5343 TmpInst.addOperand(Inst.getOperand(0)); // Vd
5344 TmpInst.addOperand(Inst.getOperand(1)); // lane
5345 TmpInst.addOperand(Inst.getOperand(5)); // CondCode
5346 TmpInst.addOperand(Inst.getOperand(6));
5351 case ARM::VST2LNdWB_register_Asm_8:
5352 case ARM::VST2LNdWB_register_Asm_16:
5353 case ARM::VST2LNdWB_register_Asm_32:
5354 case ARM::VST2LNqWB_register_Asm_16:
5355 case ARM::VST2LNqWB_register_Asm_32: {
5357 // Shuffle the operands around so the lane index operand is in the
5360 TmpInst.setOpcode(getRealVSTLNOpcode(Inst.getOpcode(), Spacing));
5361 TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
5362 TmpInst.addOperand(Inst.getOperand(2)); // Rn
5363 TmpInst.addOperand(Inst.getOperand(3)); // alignment
5364 TmpInst.addOperand(Inst.getOperand(4)); // Rm
5365 TmpInst.addOperand(Inst.getOperand(0)); // Vd
5366 TmpInst.addOperand(MCOperand::CreateReg(Inst.getOperand(0).getReg() +
5368 TmpInst.addOperand(Inst.getOperand(1)); // lane
5369 TmpInst.addOperand(Inst.getOperand(5)); // CondCode
5370 TmpInst.addOperand(Inst.getOperand(6));
5374 case ARM::VST1LNdWB_fixed_Asm_8:
5375 case ARM::VST1LNdWB_fixed_Asm_16:
5376 case ARM::VST1LNdWB_fixed_Asm_32: {
5378 // Shuffle the operands around so the lane index operand is in the
5381 TmpInst.setOpcode(getRealVSTLNOpcode(Inst.getOpcode(), Spacing));
5382 TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
5383 TmpInst.addOperand(Inst.getOperand(2)); // Rn
5384 TmpInst.addOperand(Inst.getOperand(3)); // alignment
5385 TmpInst.addOperand(MCOperand::CreateReg(0)); // Rm
5386 TmpInst.addOperand(Inst.getOperand(0)); // Vd
5387 TmpInst.addOperand(Inst.getOperand(1)); // lane
5388 TmpInst.addOperand(Inst.getOperand(4)); // CondCode
5389 TmpInst.addOperand(Inst.getOperand(5));
5394 case ARM::VST2LNdWB_fixed_Asm_8:
5395 case ARM::VST2LNdWB_fixed_Asm_16:
5396 case ARM::VST2LNdWB_fixed_Asm_32:
5397 case ARM::VST2LNqWB_fixed_Asm_16:
5398 case ARM::VST2LNqWB_fixed_Asm_32: {
5400 // Shuffle the operands around so the lane index operand is in the
5403 TmpInst.setOpcode(getRealVSTLNOpcode(Inst.getOpcode(), Spacing));
5404 TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
5405 TmpInst.addOperand(Inst.getOperand(2)); // Rn
5406 TmpInst.addOperand(Inst.getOperand(3)); // alignment
5407 TmpInst.addOperand(MCOperand::CreateReg(0)); // Rm
5408 TmpInst.addOperand(Inst.getOperand(0)); // Vd
5409 TmpInst.addOperand(MCOperand::CreateReg(Inst.getOperand(0).getReg() +
5411 TmpInst.addOperand(Inst.getOperand(1)); // lane
5412 TmpInst.addOperand(Inst.getOperand(4)); // CondCode
5413 TmpInst.addOperand(Inst.getOperand(5));
5417 case ARM::VST1LNdAsm_8:
5418 case ARM::VST1LNdAsm_16:
5419 case ARM::VST1LNdAsm_32: {
5421 // Shuffle the operands around so the lane index operand is in the
5424 TmpInst.setOpcode(getRealVSTLNOpcode(Inst.getOpcode(), Spacing));
5425 TmpInst.addOperand(Inst.getOperand(2)); // Rn
5426 TmpInst.addOperand(Inst.getOperand(3)); // alignment
5427 TmpInst.addOperand(Inst.getOperand(0)); // Vd
5428 TmpInst.addOperand(Inst.getOperand(1)); // lane
5429 TmpInst.addOperand(Inst.getOperand(4)); // CondCode
5430 TmpInst.addOperand(Inst.getOperand(5));
5435 case ARM::VST2LNdAsm_8:
5436 case ARM::VST2LNdAsm_16:
5437 case ARM::VST2LNdAsm_32:
5438 case ARM::VST2LNqAsm_16:
5439 case ARM::VST2LNqAsm_32: {
5441 // Shuffle the operands around so the lane index operand is in the
5444 TmpInst.setOpcode(getRealVSTLNOpcode(Inst.getOpcode(), Spacing));
5445 TmpInst.addOperand(Inst.getOperand(2)); // Rn
5446 TmpInst.addOperand(Inst.getOperand(3)); // alignment
5447 TmpInst.addOperand(Inst.getOperand(0)); // Vd
5448 TmpInst.addOperand(MCOperand::CreateReg(Inst.getOperand(0).getReg() +
5450 TmpInst.addOperand(Inst.getOperand(1)); // lane
5451 TmpInst.addOperand(Inst.getOperand(4)); // CondCode
5452 TmpInst.addOperand(Inst.getOperand(5));
5456 // Handle NEON VLD complex aliases.
5457 case ARM::VLD1LNdWB_register_Asm_8:
5458 case ARM::VLD1LNdWB_register_Asm_16:
5459 case ARM::VLD1LNdWB_register_Asm_32: {
5461 // Shuffle the operands around so the lane index operand is in the
5464 TmpInst.setOpcode(getRealVLDLNOpcode(Inst.getOpcode(), Spacing));
5465 TmpInst.addOperand(Inst.getOperand(0)); // Vd
5466 TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
5467 TmpInst.addOperand(Inst.getOperand(2)); // Rn
5468 TmpInst.addOperand(Inst.getOperand(3)); // alignment
5469 TmpInst.addOperand(Inst.getOperand(4)); // Rm
5470 TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
5471 TmpInst.addOperand(Inst.getOperand(1)); // lane
5472 TmpInst.addOperand(Inst.getOperand(5)); // CondCode
5473 TmpInst.addOperand(Inst.getOperand(6));
5478 case ARM::VLD2LNdWB_register_Asm_8:
5479 case ARM::VLD2LNdWB_register_Asm_16:
5480 case ARM::VLD2LNdWB_register_Asm_32:
5481 case ARM::VLD2LNqWB_register_Asm_16:
5482 case ARM::VLD2LNqWB_register_Asm_32: {
5484 // Shuffle the operands around so the lane index operand is in the
5487 TmpInst.setOpcode(getRealVLDLNOpcode(Inst.getOpcode(), Spacing));
5488 TmpInst.addOperand(Inst.getOperand(0)); // Vd
5489 TmpInst.addOperand(MCOperand::CreateReg(Inst.getOperand(0).getReg() +
5491 TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
5492 TmpInst.addOperand(Inst.getOperand(2)); // Rn
5493 TmpInst.addOperand(Inst.getOperand(3)); // alignment
5494 TmpInst.addOperand(Inst.getOperand(4)); // Rm
5495 TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
5496 TmpInst.addOperand(MCOperand::CreateReg(Inst.getOperand(0).getReg() +
5498 TmpInst.addOperand(Inst.getOperand(1)); // lane
5499 TmpInst.addOperand(Inst.getOperand(5)); // CondCode
5500 TmpInst.addOperand(Inst.getOperand(6));
5505 case ARM::VLD1LNdWB_fixed_Asm_8:
5506 case ARM::VLD1LNdWB_fixed_Asm_16:
5507 case ARM::VLD1LNdWB_fixed_Asm_32: {
5509 // Shuffle the operands around so the lane index operand is in the
5512 TmpInst.setOpcode(getRealVLDLNOpcode(Inst.getOpcode(), Spacing));
5513 TmpInst.addOperand(Inst.getOperand(0)); // Vd
5514 TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
5515 TmpInst.addOperand(Inst.getOperand(2)); // Rn
5516 TmpInst.addOperand(Inst.getOperand(3)); // alignment
5517 TmpInst.addOperand(MCOperand::CreateReg(0)); // Rm
5518 TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
5519 TmpInst.addOperand(Inst.getOperand(1)); // lane
5520 TmpInst.addOperand(Inst.getOperand(4)); // CondCode
5521 TmpInst.addOperand(Inst.getOperand(5));
5526 case ARM::VLD2LNdWB_fixed_Asm_8:
5527 case ARM::VLD2LNdWB_fixed_Asm_16:
5528 case ARM::VLD2LNdWB_fixed_Asm_32:
5529 case ARM::VLD2LNqWB_fixed_Asm_16:
5530 case ARM::VLD2LNqWB_fixed_Asm_32: {
5532 // Shuffle the operands around so the lane index operand is in the
5535 TmpInst.setOpcode(getRealVLDLNOpcode(Inst.getOpcode(), Spacing));
5536 TmpInst.addOperand(Inst.getOperand(0)); // Vd
5537 TmpInst.addOperand(MCOperand::CreateReg(Inst.getOperand(0).getReg() +
5539 TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
5540 TmpInst.addOperand(Inst.getOperand(2)); // Rn
5541 TmpInst.addOperand(Inst.getOperand(3)); // alignment
5542 TmpInst.addOperand(MCOperand::CreateReg(0)); // Rm
5543 TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
5544 TmpInst.addOperand(MCOperand::CreateReg(Inst.getOperand(0).getReg() +
5546 TmpInst.addOperand(Inst.getOperand(1)); // lane
5547 TmpInst.addOperand(Inst.getOperand(4)); // CondCode
5548 TmpInst.addOperand(Inst.getOperand(5));
5553 case ARM::VLD1LNdAsm_8:
5554 case ARM::VLD1LNdAsm_16:
5555 case ARM::VLD1LNdAsm_32: {
5557 // Shuffle the operands around so the lane index operand is in the
5560 TmpInst.setOpcode(getRealVLDLNOpcode(Inst.getOpcode(), Spacing));
5561 TmpInst.addOperand(Inst.getOperand(0)); // Vd
5562 TmpInst.addOperand(Inst.getOperand(2)); // Rn
5563 TmpInst.addOperand(Inst.getOperand(3)); // alignment
5564 TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
5565 TmpInst.addOperand(Inst.getOperand(1)); // lane
5566 TmpInst.addOperand(Inst.getOperand(4)); // CondCode
5567 TmpInst.addOperand(Inst.getOperand(5));
5572 case ARM::VLD2LNdAsm_8:
5573 case ARM::VLD2LNdAsm_16:
5574 case ARM::VLD2LNdAsm_32:
5575 case ARM::VLD2LNqAsm_16:
5576 case ARM::VLD2LNqAsm_32: {
5578 // Shuffle the operands around so the lane index operand is in the
5581 TmpInst.setOpcode(getRealVLDLNOpcode(Inst.getOpcode(), Spacing));
5582 TmpInst.addOperand(Inst.getOperand(0)); // Vd
5583 TmpInst.addOperand(MCOperand::CreateReg(Inst.getOperand(0).getReg() +
5585 TmpInst.addOperand(Inst.getOperand(2)); // Rn
5586 TmpInst.addOperand(Inst.getOperand(3)); // alignment
5587 TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
5588 TmpInst.addOperand(MCOperand::CreateReg(Inst.getOperand(0).getReg() +
5590 TmpInst.addOperand(Inst.getOperand(1)); // lane
5591 TmpInst.addOperand(Inst.getOperand(4)); // CondCode
5592 TmpInst.addOperand(Inst.getOperand(5));
5596 // Handle the Thumb2 mode MOV complex aliases.
5598 case ARM::t2MOVSsr: {
5599 // Which instruction to expand to depends on the CCOut operand and
5600 // whether we're in an IT block if the register operands are low
5602 bool isNarrow = false;
5603 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
5604 isARMLowRegister(Inst.getOperand(1).getReg()) &&
5605 isARMLowRegister(Inst.getOperand(2).getReg()) &&
5606 Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg() &&
5607 inITBlock() == (Inst.getOpcode() == ARM::t2MOVsr))
5611 switch(ARM_AM::getSORegShOp(Inst.getOperand(3).getImm())) {
5612 default: llvm_unreachable("unexpected opcode!");
5613 case ARM_AM::asr: newOpc = isNarrow ? ARM::tASRrr : ARM::t2ASRrr; break;
5614 case ARM_AM::lsr: newOpc = isNarrow ? ARM::tLSRrr : ARM::t2LSRrr; break;
5615 case ARM_AM::lsl: newOpc = isNarrow ? ARM::tLSLrr : ARM::t2LSLrr; break;
5616 case ARM_AM::ror: newOpc = isNarrow ? ARM::tROR : ARM::t2RORrr; break;
5618 TmpInst.setOpcode(newOpc);
5619 TmpInst.addOperand(Inst.getOperand(0)); // Rd
5621 TmpInst.addOperand(MCOperand::CreateReg(
5622 Inst.getOpcode() == ARM::t2MOVSsr ? ARM::CPSR : 0));
5623 TmpInst.addOperand(Inst.getOperand(1)); // Rn
5624 TmpInst.addOperand(Inst.getOperand(2)); // Rm
5625 TmpInst.addOperand(Inst.getOperand(4)); // CondCode
5626 TmpInst.addOperand(Inst.getOperand(5));
5628 TmpInst.addOperand(MCOperand::CreateReg(
5629 Inst.getOpcode() == ARM::t2MOVSsr ? ARM::CPSR : 0));
5634 case ARM::t2MOVSsi: {
5635 // Which instruction to expand to depends on the CCOut operand and
5636 // whether we're in an IT block if the register operands are low
5638 bool isNarrow = false;
5639 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
5640 isARMLowRegister(Inst.getOperand(1).getReg()) &&
5641 inITBlock() == (Inst.getOpcode() == ARM::t2MOVsi))
5645 switch(ARM_AM::getSORegShOp(Inst.getOperand(2).getImm())) {
5646 default: llvm_unreachable("unexpected opcode!");
5647 case ARM_AM::asr: newOpc = isNarrow ? ARM::tASRri : ARM::t2ASRri; break;
5648 case ARM_AM::lsr: newOpc = isNarrow ? ARM::tLSRri : ARM::t2LSRri; break;
5649 case ARM_AM::lsl: newOpc = isNarrow ? ARM::tLSLri : ARM::t2LSLri; break;
5650 case ARM_AM::ror: newOpc = ARM::t2RORri; isNarrow = false; break;
5651 case ARM_AM::rrx: isNarrow = false; newOpc = ARM::t2RRX; break;
5653 unsigned Ammount = ARM_AM::getSORegOffset(Inst.getOperand(2).getImm());
5654 if (Ammount == 32) Ammount = 0;
5655 TmpInst.setOpcode(newOpc);
5656 TmpInst.addOperand(Inst.getOperand(0)); // Rd
5658 TmpInst.addOperand(MCOperand::CreateReg(
5659 Inst.getOpcode() == ARM::t2MOVSsi ? ARM::CPSR : 0));
5660 TmpInst.addOperand(Inst.getOperand(1)); // Rn
5661 if (newOpc != ARM::t2RRX)
5662 TmpInst.addOperand(MCOperand::CreateImm(Ammount));
5663 TmpInst.addOperand(Inst.getOperand(3)); // CondCode
5664 TmpInst.addOperand(Inst.getOperand(4));
5666 TmpInst.addOperand(MCOperand::CreateReg(
5667 Inst.getOpcode() == ARM::t2MOVSsi ? ARM::CPSR : 0));
5671 // Handle the ARM mode MOV complex aliases.
5676 ARM_AM::ShiftOpc ShiftTy;
5677 switch(Inst.getOpcode()) {
5678 default: llvm_unreachable("unexpected opcode!");
5679 case ARM::ASRr: ShiftTy = ARM_AM::asr; break;
5680 case ARM::LSRr: ShiftTy = ARM_AM::lsr; break;
5681 case ARM::LSLr: ShiftTy = ARM_AM::lsl; break;
5682 case ARM::RORr: ShiftTy = ARM_AM::ror; break;
5684 unsigned Shifter = ARM_AM::getSORegOpc(ShiftTy, 0);
5686 TmpInst.setOpcode(ARM::MOVsr);
5687 TmpInst.addOperand(Inst.getOperand(0)); // Rd
5688 TmpInst.addOperand(Inst.getOperand(1)); // Rn
5689 TmpInst.addOperand(Inst.getOperand(2)); // Rm
5690 TmpInst.addOperand(MCOperand::CreateImm(Shifter)); // Shift value and ty
5691 TmpInst.addOperand(Inst.getOperand(3)); // CondCode
5692 TmpInst.addOperand(Inst.getOperand(4));
5693 TmpInst.addOperand(Inst.getOperand(5)); // cc_out
5701 ARM_AM::ShiftOpc ShiftTy;
5702 switch(Inst.getOpcode()) {
5703 default: llvm_unreachable("unexpected opcode!");
5704 case ARM::ASRi: ShiftTy = ARM_AM::asr; break;
5705 case ARM::LSRi: ShiftTy = ARM_AM::lsr; break;
5706 case ARM::LSLi: ShiftTy = ARM_AM::lsl; break;
5707 case ARM::RORi: ShiftTy = ARM_AM::ror; break;
5709 // A shift by zero is a plain MOVr, not a MOVsi.
5710 unsigned Amt = Inst.getOperand(2).getImm();
5711 unsigned Opc = Amt == 0 ? ARM::MOVr : ARM::MOVsi;
5712 unsigned Shifter = ARM_AM::getSORegOpc(ShiftTy, Amt);
5714 TmpInst.setOpcode(Opc);
5715 TmpInst.addOperand(Inst.getOperand(0)); // Rd
5716 TmpInst.addOperand(Inst.getOperand(1)); // Rn
5717 if (Opc == ARM::MOVsi)
5718 TmpInst.addOperand(MCOperand::CreateImm(Shifter)); // Shift value and ty
5719 TmpInst.addOperand(Inst.getOperand(3)); // CondCode
5720 TmpInst.addOperand(Inst.getOperand(4));
5721 TmpInst.addOperand(Inst.getOperand(5)); // cc_out
5726 unsigned Shifter = ARM_AM::getSORegOpc(ARM_AM::rrx, 0);
5728 TmpInst.setOpcode(ARM::MOVsi);
5729 TmpInst.addOperand(Inst.getOperand(0)); // Rd
5730 TmpInst.addOperand(Inst.getOperand(1)); // Rn
5731 TmpInst.addOperand(MCOperand::CreateImm(Shifter)); // Shift value and ty
5732 TmpInst.addOperand(Inst.getOperand(2)); // CondCode
5733 TmpInst.addOperand(Inst.getOperand(3));
5734 TmpInst.addOperand(Inst.getOperand(4)); // cc_out
5738 case ARM::t2LDMIA_UPD: {
5739 // If this is a load of a single register, then we should use
5740 // a post-indexed LDR instruction instead, per the ARM ARM.
5741 if (Inst.getNumOperands() != 5)
5744 TmpInst.setOpcode(ARM::t2LDR_POST);
5745 TmpInst.addOperand(Inst.getOperand(4)); // Rt
5746 TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
5747 TmpInst.addOperand(Inst.getOperand(1)); // Rn
5748 TmpInst.addOperand(MCOperand::CreateImm(4));
5749 TmpInst.addOperand(Inst.getOperand(2)); // CondCode
5750 TmpInst.addOperand(Inst.getOperand(3));
5754 case ARM::t2STMDB_UPD: {
5755 // If this is a store of a single register, then we should use
5756 // a pre-indexed STR instruction instead, per the ARM ARM.
5757 if (Inst.getNumOperands() != 5)
5760 TmpInst.setOpcode(ARM::t2STR_PRE);
5761 TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
5762 TmpInst.addOperand(Inst.getOperand(4)); // Rt
5763 TmpInst.addOperand(Inst.getOperand(1)); // Rn
5764 TmpInst.addOperand(MCOperand::CreateImm(-4));
5765 TmpInst.addOperand(Inst.getOperand(2)); // CondCode
5766 TmpInst.addOperand(Inst.getOperand(3));
5770 case ARM::LDMIA_UPD:
5771 // If this is a load of a single register via a 'pop', then we should use
5772 // a post-indexed LDR instruction instead, per the ARM ARM.
5773 if (static_cast<ARMOperand*>(Operands[0])->getToken() == "pop" &&
5774 Inst.getNumOperands() == 5) {
5776 TmpInst.setOpcode(ARM::LDR_POST_IMM);
5777 TmpInst.addOperand(Inst.getOperand(4)); // Rt
5778 TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
5779 TmpInst.addOperand(Inst.getOperand(1)); // Rn
5780 TmpInst.addOperand(MCOperand::CreateReg(0)); // am2offset
5781 TmpInst.addOperand(MCOperand::CreateImm(4));
5782 TmpInst.addOperand(Inst.getOperand(2)); // CondCode
5783 TmpInst.addOperand(Inst.getOperand(3));
5788 case ARM::STMDB_UPD:
5789 // If this is a store of a single register via a 'push', then we should use
5790 // a pre-indexed STR instruction instead, per the ARM ARM.
5791 if (static_cast<ARMOperand*>(Operands[0])->getToken() == "push" &&
5792 Inst.getNumOperands() == 5) {
5794 TmpInst.setOpcode(ARM::STR_PRE_IMM);
5795 TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
5796 TmpInst.addOperand(Inst.getOperand(4)); // Rt
5797 TmpInst.addOperand(Inst.getOperand(1)); // addrmode_imm12
5798 TmpInst.addOperand(MCOperand::CreateImm(-4));
5799 TmpInst.addOperand(Inst.getOperand(2)); // CondCode
5800 TmpInst.addOperand(Inst.getOperand(3));
5804 case ARM::t2ADDri12:
5805 // If the immediate fits for encoding T3 (t2ADDri) and the generic "add"
5806 // mnemonic was used (not "addw"), encoding T3 is preferred.
5807 if (static_cast<ARMOperand*>(Operands[0])->getToken() != "add" ||
5808 ARM_AM::getT2SOImmVal(Inst.getOperand(2).getImm()) == -1)
5810 Inst.setOpcode(ARM::t2ADDri);
5811 Inst.addOperand(MCOperand::CreateReg(0)); // cc_out
5813 case ARM::t2SUBri12:
5814 // If the immediate fits for encoding T3 (t2SUBri) and the generic "sub"
5815 // mnemonic was used (not "subw"), encoding T3 is preferred.
5816 if (static_cast<ARMOperand*>(Operands[0])->getToken() != "sub" ||
5817 ARM_AM::getT2SOImmVal(Inst.getOperand(2).getImm()) == -1)
5819 Inst.setOpcode(ARM::t2SUBri);
5820 Inst.addOperand(MCOperand::CreateReg(0)); // cc_out
5823 // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
5824 // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
5825 // to encoding T2 if <Rd> is specified and encoding T2 is preferred
5826 // to encoding T1 if <Rd> is omitted."
5827 if (Inst.getOperand(3).getImm() < 8 && Operands.size() == 6) {
5828 Inst.setOpcode(ARM::tADDi3);
5833 // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
5834 // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
5835 // to encoding T2 if <Rd> is specified and encoding T2 is preferred
5836 // to encoding T1 if <Rd> is omitted."
5837 if (Inst.getOperand(3).getImm() < 8 && Operands.size() == 6) {
5838 Inst.setOpcode(ARM::tSUBi3);
5842 case ARM::t2ADDrr: {
5843 // If the destination and first source operand are the same, and
5844 // there's no setting of the flags, use encoding T2 instead of T3.
5845 // Note that this is only for ADD, not SUB. This mirrors the system
5846 // 'as' behaviour. Make sure the wide encoding wasn't explicit.
5847 if (Inst.getOperand(0).getReg() != Inst.getOperand(1).getReg() ||
5848 Inst.getOperand(5).getReg() != 0 ||
5849 (static_cast<ARMOperand*>(Operands[3])->isToken() &&
5850 static_cast<ARMOperand*>(Operands[3])->getToken() == ".w"))
5853 TmpInst.setOpcode(ARM::tADDhirr);
5854 TmpInst.addOperand(Inst.getOperand(0));
5855 TmpInst.addOperand(Inst.getOperand(0));
5856 TmpInst.addOperand(Inst.getOperand(2));
5857 TmpInst.addOperand(Inst.getOperand(3));
5858 TmpInst.addOperand(Inst.getOperand(4));
5863 // A Thumb conditional branch outside of an IT block is a tBcc.
5864 if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock()) {
5865 Inst.setOpcode(ARM::tBcc);
5870 // A Thumb2 conditional branch outside of an IT block is a t2Bcc.
5871 if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock()){
5872 Inst.setOpcode(ARM::t2Bcc);
5877 // If the conditional is AL or we're in an IT block, we really want t2B.
5878 if (Inst.getOperand(1).getImm() == ARMCC::AL || inITBlock()) {
5879 Inst.setOpcode(ARM::t2B);
5884 // If the conditional is AL, we really want tB.
5885 if (Inst.getOperand(1).getImm() == ARMCC::AL) {
5886 Inst.setOpcode(ARM::tB);
5891 // If the register list contains any high registers, or if the writeback
5892 // doesn't match what tLDMIA can do, we need to use the 32-bit encoding
5893 // instead if we're in Thumb2. Otherwise, this should have generated
5894 // an error in validateInstruction().
5895 unsigned Rn = Inst.getOperand(0).getReg();
5896 bool hasWritebackToken =
5897 (static_cast<ARMOperand*>(Operands[3])->isToken() &&
5898 static_cast<ARMOperand*>(Operands[3])->getToken() == "!");
5899 bool listContainsBase;
5900 if (checkLowRegisterList(Inst, 3, Rn, 0, listContainsBase) ||
5901 (!listContainsBase && !hasWritebackToken) ||
5902 (listContainsBase && hasWritebackToken)) {
5903 // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
5904 assert (isThumbTwo());
5905 Inst.setOpcode(hasWritebackToken ? ARM::t2LDMIA_UPD : ARM::t2LDMIA);
5906 // If we're switching to the updating version, we need to insert
5907 // the writeback tied operand.
5908 if (hasWritebackToken)
5909 Inst.insert(Inst.begin(),
5910 MCOperand::CreateReg(Inst.getOperand(0).getReg()));
5915 case ARM::tSTMIA_UPD: {
5916 // If the register list contains any high registers, we need to use
5917 // the 32-bit encoding instead if we're in Thumb2. Otherwise, this
5918 // should have generated an error in validateInstruction().
5919 unsigned Rn = Inst.getOperand(0).getReg();
5920 bool listContainsBase;
5921 if (checkLowRegisterList(Inst, 4, Rn, 0, listContainsBase)) {
5922 // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
5923 assert (isThumbTwo());
5924 Inst.setOpcode(ARM::t2STMIA_UPD);
5930 bool listContainsBase;
5931 // If the register list contains any high registers, we need to use
5932 // the 32-bit encoding instead if we're in Thumb2. Otherwise, this
5933 // should have generated an error in validateInstruction().
5934 if (!checkLowRegisterList(Inst, 2, 0, ARM::PC, listContainsBase))
5936 assert (isThumbTwo());
5937 Inst.setOpcode(ARM::t2LDMIA_UPD);
5938 // Add the base register and writeback operands.
5939 Inst.insert(Inst.begin(), MCOperand::CreateReg(ARM::SP));
5940 Inst.insert(Inst.begin(), MCOperand::CreateReg(ARM::SP));
5944 bool listContainsBase;
5945 if (!checkLowRegisterList(Inst, 2, 0, ARM::LR, listContainsBase))
5947 assert (isThumbTwo());
5948 Inst.setOpcode(ARM::t2STMDB_UPD);
5949 // Add the base register and writeback operands.
5950 Inst.insert(Inst.begin(), MCOperand::CreateReg(ARM::SP));
5951 Inst.insert(Inst.begin(), MCOperand::CreateReg(ARM::SP));
5955 // If we can use the 16-bit encoding and the user didn't explicitly
5956 // request the 32-bit variant, transform it here.
5957 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
5958 Inst.getOperand(1).getImm() <= 255 &&
5959 ((!inITBlock() && Inst.getOperand(2).getImm() == ARMCC::AL &&
5960 Inst.getOperand(4).getReg() == ARM::CPSR) ||
5961 (inITBlock() && Inst.getOperand(4).getReg() == 0)) &&
5962 (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
5963 static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
5964 // The operands aren't in the same order for tMOVi8...
5966 TmpInst.setOpcode(ARM::tMOVi8);
5967 TmpInst.addOperand(Inst.getOperand(0));
5968 TmpInst.addOperand(Inst.getOperand(4));
5969 TmpInst.addOperand(Inst.getOperand(1));
5970 TmpInst.addOperand(Inst.getOperand(2));
5971 TmpInst.addOperand(Inst.getOperand(3));
5978 // If we can use the 16-bit encoding and the user didn't explicitly
5979 // request the 32-bit variant, transform it here.
5980 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
5981 isARMLowRegister(Inst.getOperand(1).getReg()) &&
5982 Inst.getOperand(2).getImm() == ARMCC::AL &&
5983 Inst.getOperand(4).getReg() == ARM::CPSR &&
5984 (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
5985 static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
5986 // The operands aren't the same for tMOV[S]r... (no cc_out)
5988 TmpInst.setOpcode(Inst.getOperand(4).getReg() ? ARM::tMOVSr : ARM::tMOVr);
5989 TmpInst.addOperand(Inst.getOperand(0));
5990 TmpInst.addOperand(Inst.getOperand(1));
5991 TmpInst.addOperand(Inst.getOperand(2));
5992 TmpInst.addOperand(Inst.getOperand(3));
6002 // If we can use the 16-bit encoding and the user didn't explicitly
6003 // request the 32-bit variant, transform it here.
6004 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
6005 isARMLowRegister(Inst.getOperand(1).getReg()) &&
6006 Inst.getOperand(2).getImm() == 0 &&
6007 (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
6008 static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
6010 switch (Inst.getOpcode()) {
6011 default: llvm_unreachable("Illegal opcode!");
6012 case ARM::t2SXTH: NewOpc = ARM::tSXTH; break;
6013 case ARM::t2SXTB: NewOpc = ARM::tSXTB; break;
6014 case ARM::t2UXTH: NewOpc = ARM::tUXTH; break;
6015 case ARM::t2UXTB: NewOpc = ARM::tUXTB; break;
6017 // The operands aren't the same for thumb1 (no rotate operand).
6019 TmpInst.setOpcode(NewOpc);
6020 TmpInst.addOperand(Inst.getOperand(0));
6021 TmpInst.addOperand(Inst.getOperand(1));
6022 TmpInst.addOperand(Inst.getOperand(3));
6023 TmpInst.addOperand(Inst.getOperand(4));
6030 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(Inst.getOperand(2).getImm());
6031 if (SOpc == ARM_AM::rrx) return false;
6032 if (ARM_AM::getSORegOffset(Inst.getOperand(2).getImm()) == 0) {
6033 // Shifting by zero is accepted as a vanilla 'MOVr'
6035 TmpInst.setOpcode(ARM::MOVr);
6036 TmpInst.addOperand(Inst.getOperand(0));
6037 TmpInst.addOperand(Inst.getOperand(1));
6038 TmpInst.addOperand(Inst.getOperand(3));
6039 TmpInst.addOperand(Inst.getOperand(4));
6040 TmpInst.addOperand(Inst.getOperand(5));
6053 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(Inst.getOperand(3).getImm());
6054 if (SOpc == ARM_AM::rrx) return false;
6055 switch (Inst.getOpcode()) {
6056 default: assert(0 && "unexpected opcode!");
6057 case ARM::ANDrsi: newOpc = ARM::ANDrr; break;
6058 case ARM::ORRrsi: newOpc = ARM::ORRrr; break;
6059 case ARM::EORrsi: newOpc = ARM::EORrr; break;
6060 case ARM::BICrsi: newOpc = ARM::BICrr; break;
6061 case ARM::SUBrsi: newOpc = ARM::SUBrr; break;
6062 case ARM::ADDrsi: newOpc = ARM::ADDrr; break;
6064 // If the shift is by zero, use the non-shifted instruction definition.
6065 if (ARM_AM::getSORegOffset(Inst.getOperand(3).getImm()) == 0) {
6067 TmpInst.setOpcode(newOpc);
6068 TmpInst.addOperand(Inst.getOperand(0));
6069 TmpInst.addOperand(Inst.getOperand(1));
6070 TmpInst.addOperand(Inst.getOperand(2));
6071 TmpInst.addOperand(Inst.getOperand(4));
6072 TmpInst.addOperand(Inst.getOperand(5));
6073 TmpInst.addOperand(Inst.getOperand(6));
6080 // The mask bits for all but the first condition are represented as
6081 // the low bit of the condition code value implies 't'. We currently
6082 // always have 1 implies 't', so XOR toggle the bits if the low bit
6083 // of the condition code is zero. The encoding also expects the low
6084 // bit of the condition to be encoded as bit 4 of the mask operand,
6085 // so mask that in if needed
6086 MCOperand &MO = Inst.getOperand(1);
6087 unsigned Mask = MO.getImm();
6088 unsigned OrigMask = Mask;
6089 unsigned TZ = CountTrailingZeros_32(Mask);
6090 if ((Inst.getOperand(0).getImm() & 1) == 0) {
6091 assert(Mask && TZ <= 3 && "illegal IT mask value!");
6092 for (unsigned i = 3; i != TZ; --i)
6098 // Set up the IT block state according to the IT instruction we just
6100 assert(!inITBlock() && "nested IT blocks?!");
6101 ITState.Cond = ARMCC::CondCodes(Inst.getOperand(0).getImm());
6102 ITState.Mask = OrigMask; // Use the original mask, not the updated one.
6103 ITState.CurPosition = 0;
6104 ITState.FirstCond = true;
6111 unsigned ARMAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
6112 // 16-bit thumb arithmetic instructions either require or preclude the 'S'
6113 // suffix depending on whether they're in an IT block or not.
6114 unsigned Opc = Inst.getOpcode();
6115 const MCInstrDesc &MCID = getInstDesc(Opc);
6116 if (MCID.TSFlags & ARMII::ThumbArithFlagSetting) {
6117 assert(MCID.hasOptionalDef() &&
6118 "optionally flag setting instruction missing optional def operand");
6119 assert(MCID.NumOperands == Inst.getNumOperands() &&
6120 "operand count mismatch!");
6121 // Find the optional-def operand (cc_out).
6124 !MCID.OpInfo[OpNo].isOptionalDef() && OpNo < MCID.NumOperands;
6127 // If we're parsing Thumb1, reject it completely.
6128 if (isThumbOne() && Inst.getOperand(OpNo).getReg() != ARM::CPSR)
6129 return Match_MnemonicFail;
6130 // If we're parsing Thumb2, which form is legal depends on whether we're
6132 if (isThumbTwo() && Inst.getOperand(OpNo).getReg() != ARM::CPSR &&
6134 return Match_RequiresITBlock;
6135 if (isThumbTwo() && Inst.getOperand(OpNo).getReg() == ARM::CPSR &&
6137 return Match_RequiresNotITBlock;
6139 // Some high-register supporting Thumb1 encodings only allow both registers
6140 // to be from r0-r7 when in Thumb2.
6141 else if (Opc == ARM::tADDhirr && isThumbOne() &&
6142 isARMLowRegister(Inst.getOperand(1).getReg()) &&
6143 isARMLowRegister(Inst.getOperand(2).getReg()))
6144 return Match_RequiresThumb2;
6145 // Others only require ARMv6 or later.
6146 else if (Opc == ARM::tMOVr && isThumbOne() && !hasV6Ops() &&
6147 isARMLowRegister(Inst.getOperand(0).getReg()) &&
6148 isARMLowRegister(Inst.getOperand(1).getReg()))
6149 return Match_RequiresV6;
6150 return Match_Success;
6154 MatchAndEmitInstruction(SMLoc IDLoc,
6155 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
6159 unsigned MatchResult;
6160 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
6161 switch (MatchResult) {
6164 // Context sensitive operand constraints aren't handled by the matcher,
6165 // so check them here.
6166 if (validateInstruction(Inst, Operands)) {
6167 // Still progress the IT block, otherwise one wrong condition causes
6168 // nasty cascading errors.
6169 forwardITPosition();
6173 // Some instructions need post-processing to, for example, tweak which
6174 // encoding is selected. Loop on it while changes happen so the
6175 // individual transformations can chain off each other. E.g.,
6176 // tPOP(r8)->t2LDMIA_UPD(sp,r8)->t2STR_POST(sp,r8)
6177 while (processInstruction(Inst, Operands))
6180 // Only move forward at the very end so that everything in validate
6181 // and process gets a consistent answer about whether we're in an IT
6183 forwardITPosition();
6185 Out.EmitInstruction(Inst);
6187 case Match_MissingFeature:
6188 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
6190 case Match_InvalidOperand: {
6191 SMLoc ErrorLoc = IDLoc;
6192 if (ErrorInfo != ~0U) {
6193 if (ErrorInfo >= Operands.size())
6194 return Error(IDLoc, "too few operands for instruction");
6196 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
6197 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
6200 return Error(ErrorLoc, "invalid operand for instruction");
6202 case Match_MnemonicFail:
6203 return Error(IDLoc, "invalid instruction");
6204 case Match_ConversionFail:
6205 // The converter function will have already emited a diagnostic.
6207 case Match_RequiresNotITBlock:
6208 return Error(IDLoc, "flag setting instruction only valid outside IT block");
6209 case Match_RequiresITBlock:
6210 return Error(IDLoc, "instruction only valid inside IT block");
6211 case Match_RequiresV6:
6212 return Error(IDLoc, "instruction variant requires ARMv6 or later");
6213 case Match_RequiresThumb2:
6214 return Error(IDLoc, "instruction variant requires Thumb2");
6217 llvm_unreachable("Implement any new match types added!");
6220 /// parseDirective parses the arm specific directives
6221 bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
6222 StringRef IDVal = DirectiveID.getIdentifier();
6223 if (IDVal == ".word")
6224 return parseDirectiveWord(4, DirectiveID.getLoc());
6225 else if (IDVal == ".thumb")
6226 return parseDirectiveThumb(DirectiveID.getLoc());
6227 else if (IDVal == ".arm")
6228 return parseDirectiveARM(DirectiveID.getLoc());
6229 else if (IDVal == ".thumb_func")
6230 return parseDirectiveThumbFunc(DirectiveID.getLoc());
6231 else if (IDVal == ".code")
6232 return parseDirectiveCode(DirectiveID.getLoc());
6233 else if (IDVal == ".syntax")
6234 return parseDirectiveSyntax(DirectiveID.getLoc());
6235 else if (IDVal == ".unreq")
6236 return parseDirectiveUnreq(DirectiveID.getLoc());
6237 else if (IDVal == ".arch")
6238 return parseDirectiveArch(DirectiveID.getLoc());
6239 else if (IDVal == ".eabi_attribute")
6240 return parseDirectiveEabiAttr(DirectiveID.getLoc());
6244 /// parseDirectiveWord
6245 /// ::= .word [ expression (, expression)* ]
6246 bool ARMAsmParser::parseDirectiveWord(unsigned Size, SMLoc L) {
6247 if (getLexer().isNot(AsmToken::EndOfStatement)) {
6249 const MCExpr *Value;
6250 if (getParser().ParseExpression(Value))
6253 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
6255 if (getLexer().is(AsmToken::EndOfStatement))
6258 // FIXME: Improve diagnostic.
6259 if (getLexer().isNot(AsmToken::Comma))
6260 return Error(L, "unexpected token in directive");
6269 /// parseDirectiveThumb
6271 bool ARMAsmParser::parseDirectiveThumb(SMLoc L) {
6272 if (getLexer().isNot(AsmToken::EndOfStatement))
6273 return Error(L, "unexpected token in directive");
6278 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
6282 /// parseDirectiveARM
6284 bool ARMAsmParser::parseDirectiveARM(SMLoc L) {
6285 if (getLexer().isNot(AsmToken::EndOfStatement))
6286 return Error(L, "unexpected token in directive");
6291 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
6295 /// parseDirectiveThumbFunc
6296 /// ::= .thumbfunc symbol_name
6297 bool ARMAsmParser::parseDirectiveThumbFunc(SMLoc L) {
6298 const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
6299 bool isMachO = MAI.hasSubsectionsViaSymbols();
6301 bool needFuncName = true;
6303 // Darwin asm has (optionally) function name after .thumb_func direction
6306 const AsmToken &Tok = Parser.getTok();
6307 if (Tok.isNot(AsmToken::EndOfStatement)) {
6308 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
6309 return Error(L, "unexpected token in .thumb_func directive");
6310 Name = Tok.getIdentifier();
6311 Parser.Lex(); // Consume the identifier token.
6312 needFuncName = false;
6316 if (getLexer().isNot(AsmToken::EndOfStatement))
6317 return Error(L, "unexpected token in directive");
6319 // Eat the end of statement and any blank lines that follow.
6320 while (getLexer().is(AsmToken::EndOfStatement))
6323 // FIXME: assuming function name will be the line following .thumb_func
6324 // We really should be checking the next symbol definition even if there's
6325 // stuff in between.
6327 Name = Parser.getTok().getIdentifier();
6330 // Mark symbol as a thumb symbol.
6331 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
6332 getParser().getStreamer().EmitThumbFunc(Func);
6336 /// parseDirectiveSyntax
6337 /// ::= .syntax unified | divided
6338 bool ARMAsmParser::parseDirectiveSyntax(SMLoc L) {
6339 const AsmToken &Tok = Parser.getTok();
6340 if (Tok.isNot(AsmToken::Identifier))
6341 return Error(L, "unexpected token in .syntax directive");
6342 StringRef Mode = Tok.getString();
6343 if (Mode == "unified" || Mode == "UNIFIED")
6345 else if (Mode == "divided" || Mode == "DIVIDED")
6346 return Error(L, "'.syntax divided' arm asssembly not supported");
6348 return Error(L, "unrecognized syntax mode in .syntax directive");
6350 if (getLexer().isNot(AsmToken::EndOfStatement))
6351 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
6354 // TODO tell the MC streamer the mode
6355 // getParser().getStreamer().Emit???();
6359 /// parseDirectiveCode
6360 /// ::= .code 16 | 32
6361 bool ARMAsmParser::parseDirectiveCode(SMLoc L) {
6362 const AsmToken &Tok = Parser.getTok();
6363 if (Tok.isNot(AsmToken::Integer))
6364 return Error(L, "unexpected token in .code directive");
6365 int64_t Val = Parser.getTok().getIntVal();
6371 return Error(L, "invalid operand to .code directive");
6373 if (getLexer().isNot(AsmToken::EndOfStatement))
6374 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
6380 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
6384 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
6390 /// parseDirectiveReq
6391 /// ::= name .req registername
6392 bool ARMAsmParser::parseDirectiveReq(StringRef Name, SMLoc L) {
6393 Parser.Lex(); // Eat the '.req' token.
6395 SMLoc SRegLoc, ERegLoc;
6396 if (ParseRegister(Reg, SRegLoc, ERegLoc)) {
6397 Parser.EatToEndOfStatement();
6398 return Error(SRegLoc, "register name expected");
6401 // Shouldn't be anything else.
6402 if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
6403 Parser.EatToEndOfStatement();
6404 return Error(Parser.getTok().getLoc(),
6405 "unexpected input in .req directive.");
6408 Parser.Lex(); // Consume the EndOfStatement
6410 if (RegisterReqs.GetOrCreateValue(Name, Reg).getValue() != Reg)
6411 return Error(SRegLoc, "redefinition of '" + Name +
6412 "' does not match original.");
6417 /// parseDirectiveUneq
6418 /// ::= .unreq registername
6419 bool ARMAsmParser::parseDirectiveUnreq(SMLoc L) {
6420 if (Parser.getTok().isNot(AsmToken::Identifier)) {
6421 Parser.EatToEndOfStatement();
6422 return Error(L, "unexpected input in .unreq directive.");
6424 RegisterReqs.erase(Parser.getTok().getIdentifier());
6425 Parser.Lex(); // Eat the identifier.
6429 /// parseDirectiveArch
6431 bool ARMAsmParser::parseDirectiveArch(SMLoc L) {
6435 /// parseDirectiveEabiAttr
6436 /// ::= .eabi_attribute int, int
6437 bool ARMAsmParser::parseDirectiveEabiAttr(SMLoc L) {
6441 extern "C" void LLVMInitializeARMAsmLexer();
6443 /// Force static initialization.
6444 extern "C" void LLVMInitializeARMAsmParser() {
6445 RegisterMCAsmParser<ARMAsmParser> X(TheARMTarget);
6446 RegisterMCAsmParser<ARMAsmParser> Y(TheThumbTarget);
6447 LLVMInitializeARMAsmLexer();
6450 #define GET_REGISTER_MATCHER
6451 #define GET_MATCHER_IMPLEMENTATION
6452 #include "ARMGenAsmMatcher.inc"