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/StringExtras.h"
34 #include "llvm/ADT/StringSwitch.h"
35 #include "llvm/ADT/Twine.h"
43 class ARMAsmParser : public MCTargetAsmParser {
48 ARMCC::CondCodes Cond; // Condition for IT block.
49 unsigned Mask:4; // Condition mask for instructions.
50 // Starting at first 1 (from lsb).
51 // '1' condition as indicated in IT.
52 // '0' inverse of condition (else).
53 // Count of instructions in IT block is
54 // 4 - trailingzeroes(mask)
56 bool FirstCond; // Explicit flag for when we're parsing the
57 // First instruction in the IT block. It's
58 // implied in the mask, so needs special
61 unsigned CurPosition; // Current position in parsing of IT
62 // block. In range [0,3]. Initialized
63 // according to count of instructions in block.
64 // ~0U if no active IT block.
66 bool inITBlock() { return ITState.CurPosition != ~0U;}
67 void forwardITPosition() {
68 if (!inITBlock()) return;
69 // Move to the next instruction in the IT block, if there is one. If not,
70 // mark the block as done.
71 unsigned TZ = CountTrailingZeros_32(ITState.Mask);
72 if (++ITState.CurPosition == 5 - TZ)
73 ITState.CurPosition = ~0U; // Done with the IT block after this.
77 MCAsmParser &getParser() const { return Parser; }
78 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
80 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
81 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
83 int tryParseRegister();
84 bool tryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
85 int tryParseShiftRegister(SmallVectorImpl<MCParsedAsmOperand*> &);
86 bool parseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
87 bool parseMemory(SmallVectorImpl<MCParsedAsmOperand*> &);
88 bool parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, StringRef Mnemonic);
89 bool parsePrefix(ARMMCExpr::VariantKind &RefKind);
90 bool parseMemRegOffsetShift(ARM_AM::ShiftOpc &ShiftType,
91 unsigned &ShiftAmount);
92 bool parseDirectiveWord(unsigned Size, SMLoc L);
93 bool parseDirectiveThumb(SMLoc L);
94 bool parseDirectiveThumbFunc(SMLoc L);
95 bool parseDirectiveCode(SMLoc L);
96 bool parseDirectiveSyntax(SMLoc L);
98 StringRef splitMnemonic(StringRef Mnemonic, unsigned &PredicationCode,
99 bool &CarrySetting, unsigned &ProcessorIMod,
101 void getMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
102 bool &CanAcceptPredicationCode);
104 bool isThumb() const {
105 // FIXME: Can tablegen auto-generate this?
106 return (STI.getFeatureBits() & ARM::ModeThumb) != 0;
108 bool isThumbOne() const {
109 return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2) == 0;
111 bool isThumbTwo() const {
112 return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2);
114 bool hasV6Ops() const {
115 return STI.getFeatureBits() & ARM::HasV6Ops;
117 bool hasV7Ops() const {
118 return STI.getFeatureBits() & ARM::HasV7Ops;
121 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(ARM::ModeThumb));
122 setAvailableFeatures(FB);
124 bool isMClass() const {
125 return STI.getFeatureBits() & ARM::FeatureMClass;
128 /// @name Auto-generated Match Functions
131 #define GET_ASSEMBLER_HEADER
132 #include "ARMGenAsmMatcher.inc"
136 OperandMatchResultTy parseITCondCode(SmallVectorImpl<MCParsedAsmOperand*>&);
137 OperandMatchResultTy parseCoprocNumOperand(
138 SmallVectorImpl<MCParsedAsmOperand*>&);
139 OperandMatchResultTy parseCoprocRegOperand(
140 SmallVectorImpl<MCParsedAsmOperand*>&);
141 OperandMatchResultTy parseCoprocOptionOperand(
142 SmallVectorImpl<MCParsedAsmOperand*>&);
143 OperandMatchResultTy parseMemBarrierOptOperand(
144 SmallVectorImpl<MCParsedAsmOperand*>&);
145 OperandMatchResultTy parseProcIFlagsOperand(
146 SmallVectorImpl<MCParsedAsmOperand*>&);
147 OperandMatchResultTy parseMSRMaskOperand(
148 SmallVectorImpl<MCParsedAsmOperand*>&);
149 OperandMatchResultTy parsePKHImm(SmallVectorImpl<MCParsedAsmOperand*> &O,
150 StringRef Op, int Low, int High);
151 OperandMatchResultTy parsePKHLSLImm(SmallVectorImpl<MCParsedAsmOperand*> &O) {
152 return parsePKHImm(O, "lsl", 0, 31);
154 OperandMatchResultTy parsePKHASRImm(SmallVectorImpl<MCParsedAsmOperand*> &O) {
155 return parsePKHImm(O, "asr", 1, 32);
157 OperandMatchResultTy parseSetEndImm(SmallVectorImpl<MCParsedAsmOperand*>&);
158 OperandMatchResultTy parseShifterImm(SmallVectorImpl<MCParsedAsmOperand*>&);
159 OperandMatchResultTy parseRotImm(SmallVectorImpl<MCParsedAsmOperand*>&);
160 OperandMatchResultTy parseBitfield(SmallVectorImpl<MCParsedAsmOperand*>&);
161 OperandMatchResultTy parsePostIdxReg(SmallVectorImpl<MCParsedAsmOperand*>&);
162 OperandMatchResultTy parseAM3Offset(SmallVectorImpl<MCParsedAsmOperand*>&);
163 OperandMatchResultTy parseFPImm(SmallVectorImpl<MCParsedAsmOperand*>&);
164 OperandMatchResultTy parseVectorList(SmallVectorImpl<MCParsedAsmOperand*>&);
166 // Asm Match Converter Methods
167 bool cvtT2LdrdPre(MCInst &Inst, unsigned Opcode,
168 const SmallVectorImpl<MCParsedAsmOperand*> &);
169 bool cvtT2StrdPre(MCInst &Inst, unsigned Opcode,
170 const SmallVectorImpl<MCParsedAsmOperand*> &);
171 bool cvtLdWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
172 const SmallVectorImpl<MCParsedAsmOperand*> &);
173 bool cvtStWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
174 const SmallVectorImpl<MCParsedAsmOperand*> &);
175 bool cvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
176 const SmallVectorImpl<MCParsedAsmOperand*> &);
177 bool cvtLdWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
178 const SmallVectorImpl<MCParsedAsmOperand*> &);
179 bool cvtStWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
180 const SmallVectorImpl<MCParsedAsmOperand*> &);
181 bool cvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
182 const SmallVectorImpl<MCParsedAsmOperand*> &);
183 bool cvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
184 const SmallVectorImpl<MCParsedAsmOperand*> &);
185 bool cvtLdExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
186 const SmallVectorImpl<MCParsedAsmOperand*> &);
187 bool cvtLdExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
188 const SmallVectorImpl<MCParsedAsmOperand*> &);
189 bool cvtStExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
190 const SmallVectorImpl<MCParsedAsmOperand*> &);
191 bool cvtStExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
192 const SmallVectorImpl<MCParsedAsmOperand*> &);
193 bool cvtLdrdPre(MCInst &Inst, unsigned Opcode,
194 const SmallVectorImpl<MCParsedAsmOperand*> &);
195 bool cvtStrdPre(MCInst &Inst, unsigned Opcode,
196 const SmallVectorImpl<MCParsedAsmOperand*> &);
197 bool cvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
198 const SmallVectorImpl<MCParsedAsmOperand*> &);
199 bool cvtThumbMultiply(MCInst &Inst, unsigned Opcode,
200 const SmallVectorImpl<MCParsedAsmOperand*> &);
201 bool cvtVLDwbFixed(MCInst &Inst, unsigned Opcode,
202 const SmallVectorImpl<MCParsedAsmOperand*> &);
203 bool cvtVLDwbRegister(MCInst &Inst, unsigned Opcode,
204 const SmallVectorImpl<MCParsedAsmOperand*> &);
205 bool cvtVSTwbFixed(MCInst &Inst, unsigned Opcode,
206 const SmallVectorImpl<MCParsedAsmOperand*> &);
207 bool cvtVSTwbRegister(MCInst &Inst, unsigned Opcode,
208 const SmallVectorImpl<MCParsedAsmOperand*> &);
210 bool validateInstruction(MCInst &Inst,
211 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
212 void processInstruction(MCInst &Inst,
213 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
214 bool shouldOmitCCOutOperand(StringRef Mnemonic,
215 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
218 enum ARMMatchResultTy {
219 Match_RequiresITBlock = FIRST_TARGET_MATCH_RESULT_TY,
220 Match_RequiresNotITBlock,
225 ARMAsmParser(MCSubtargetInfo &_STI, MCAsmParser &_Parser)
226 : MCTargetAsmParser(), STI(_STI), Parser(_Parser) {
227 MCAsmParserExtension::Initialize(_Parser);
229 // Initialize the set of available features.
230 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
232 // Not in an ITBlock to start with.
233 ITState.CurPosition = ~0U;
236 // Implementation of the MCTargetAsmParser interface:
237 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
238 bool ParseInstruction(StringRef Name, SMLoc NameLoc,
239 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
240 bool ParseDirective(AsmToken DirectiveID);
242 unsigned checkTargetMatchPredicate(MCInst &Inst);
244 bool MatchAndEmitInstruction(SMLoc IDLoc,
245 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
248 } // end anonymous namespace
252 /// ARMOperand - Instances of this class represent a parsed ARM machine
254 class ARMOperand : public MCParsedAsmOperand {
279 k_BitfieldDescriptor,
283 SMLoc StartLoc, EndLoc;
284 SmallVector<unsigned, 8> Registers;
288 ARMCC::CondCodes Val;
308 ARM_PROC::IFlags Val;
324 // A vector register list is a sequential list of 1 to 4 registers.
339 unsigned Val; // encoded 8-bit representation
342 /// Combined record for all forms of ARM address expressions.
345 // Offset is in OffsetReg or OffsetImm. If both are zero, no offset
347 const MCConstantExpr *OffsetImm; // Offset immediate value
348 unsigned OffsetRegNum; // Offset register num, when OffsetImm == NULL
349 ARM_AM::ShiftOpc ShiftType; // Shift type for OffsetReg
350 unsigned ShiftImm; // shift for OffsetReg.
351 unsigned Alignment; // 0 = no alignment specified
352 // n = alignment in bytes (8, 16, or 32)
353 unsigned isNegative : 1; // Negated OffsetReg? (~'U' bit)
359 ARM_AM::ShiftOpc ShiftTy;
368 ARM_AM::ShiftOpc ShiftTy;
374 ARM_AM::ShiftOpc ShiftTy;
387 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
389 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
391 StartLoc = o.StartLoc;
408 case k_DPRRegisterList:
409 case k_SPRRegisterList:
410 Registers = o.Registers;
413 VectorList = o.VectorList;
420 CoprocOption = o.CoprocOption;
428 case k_MemBarrierOpt:
434 case k_PostIndexRegister:
435 PostIdxReg = o.PostIdxReg;
443 case k_ShifterImmediate:
444 ShifterImm = o.ShifterImm;
446 case k_ShiftedRegister:
447 RegShiftedReg = o.RegShiftedReg;
449 case k_ShiftedImmediate:
450 RegShiftedImm = o.RegShiftedImm;
452 case k_RotateImmediate:
455 case k_BitfieldDescriptor:
456 Bitfield = o.Bitfield;
459 VectorIndex = o.VectorIndex;
464 /// getStartLoc - Get the location of the first token of this operand.
465 SMLoc getStartLoc() const { return StartLoc; }
466 /// getEndLoc - Get the location of the last token of this operand.
467 SMLoc getEndLoc() const { return EndLoc; }
469 ARMCC::CondCodes getCondCode() const {
470 assert(Kind == k_CondCode && "Invalid access!");
474 unsigned getCoproc() const {
475 assert((Kind == k_CoprocNum || Kind == k_CoprocReg) && "Invalid access!");
479 StringRef getToken() const {
480 assert(Kind == k_Token && "Invalid access!");
481 return StringRef(Tok.Data, Tok.Length);
484 unsigned getReg() const {
485 assert((Kind == k_Register || Kind == k_CCOut) && "Invalid access!");
489 const SmallVectorImpl<unsigned> &getRegList() const {
490 assert((Kind == k_RegisterList || Kind == k_DPRRegisterList ||
491 Kind == k_SPRRegisterList) && "Invalid access!");
495 const MCExpr *getImm() const {
496 assert(Kind == k_Immediate && "Invalid access!");
500 unsigned getFPImm() const {
501 assert(Kind == k_FPImmediate && "Invalid access!");
505 unsigned getVectorIndex() const {
506 assert(Kind == k_VectorIndex && "Invalid access!");
507 return VectorIndex.Val;
510 ARM_MB::MemBOpt getMemBarrierOpt() const {
511 assert(Kind == k_MemBarrierOpt && "Invalid access!");
515 ARM_PROC::IFlags getProcIFlags() const {
516 assert(Kind == k_ProcIFlags && "Invalid access!");
520 unsigned getMSRMask() const {
521 assert(Kind == k_MSRMask && "Invalid access!");
525 bool isCoprocNum() const { return Kind == k_CoprocNum; }
526 bool isCoprocReg() const { return Kind == k_CoprocReg; }
527 bool isCoprocOption() const { return Kind == k_CoprocOption; }
528 bool isCondCode() const { return Kind == k_CondCode; }
529 bool isCCOut() const { return Kind == k_CCOut; }
530 bool isITMask() const { return Kind == k_ITCondMask; }
531 bool isITCondCode() const { return Kind == k_CondCode; }
532 bool isImm() const { return Kind == k_Immediate; }
533 bool isFPImm() const { return Kind == k_FPImmediate; }
534 bool isImm8s4() const {
535 if (Kind != k_Immediate)
537 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
538 if (!CE) return false;
539 int64_t Value = CE->getValue();
540 return ((Value & 3) == 0) && Value >= -1020 && Value <= 1020;
542 bool isImm0_1020s4() const {
543 if (Kind != k_Immediate)
545 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
546 if (!CE) return false;
547 int64_t Value = CE->getValue();
548 return ((Value & 3) == 0) && Value >= 0 && Value <= 1020;
550 bool isImm0_508s4() const {
551 if (Kind != k_Immediate)
553 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
554 if (!CE) return false;
555 int64_t Value = CE->getValue();
556 return ((Value & 3) == 0) && Value >= 0 && Value <= 508;
558 bool isImm0_255() const {
559 if (Kind != k_Immediate)
561 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
562 if (!CE) return false;
563 int64_t Value = CE->getValue();
564 return Value >= 0 && Value < 256;
566 bool isImm0_7() const {
567 if (Kind != k_Immediate)
569 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
570 if (!CE) return false;
571 int64_t Value = CE->getValue();
572 return Value >= 0 && Value < 8;
574 bool isImm0_15() const {
575 if (Kind != k_Immediate)
577 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
578 if (!CE) return false;
579 int64_t Value = CE->getValue();
580 return Value >= 0 && Value < 16;
582 bool isImm0_31() const {
583 if (Kind != k_Immediate)
585 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
586 if (!CE) return false;
587 int64_t Value = CE->getValue();
588 return Value >= 0 && Value < 32;
590 bool isImm1_16() const {
591 if (Kind != k_Immediate)
593 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
594 if (!CE) return false;
595 int64_t Value = CE->getValue();
596 return Value > 0 && Value < 17;
598 bool isImm1_32() const {
599 if (Kind != k_Immediate)
601 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
602 if (!CE) return false;
603 int64_t Value = CE->getValue();
604 return Value > 0 && Value < 33;
606 bool isImm0_65535() const {
607 if (Kind != k_Immediate)
609 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
610 if (!CE) return false;
611 int64_t Value = CE->getValue();
612 return Value >= 0 && Value < 65536;
614 bool isImm0_65535Expr() const {
615 if (Kind != k_Immediate)
617 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
618 // If it's not a constant expression, it'll generate a fixup and be
620 if (!CE) return true;
621 int64_t Value = CE->getValue();
622 return Value >= 0 && Value < 65536;
624 bool isImm24bit() const {
625 if (Kind != k_Immediate)
627 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
628 if (!CE) return false;
629 int64_t Value = CE->getValue();
630 return Value >= 0 && Value <= 0xffffff;
632 bool isImmThumbSR() const {
633 if (Kind != k_Immediate)
635 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
636 if (!CE) return false;
637 int64_t Value = CE->getValue();
638 return Value > 0 && Value < 33;
640 bool isPKHLSLImm() const {
641 if (Kind != k_Immediate)
643 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
644 if (!CE) return false;
645 int64_t Value = CE->getValue();
646 return Value >= 0 && Value < 32;
648 bool isPKHASRImm() const {
649 if (Kind != k_Immediate)
651 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
652 if (!CE) return false;
653 int64_t Value = CE->getValue();
654 return Value > 0 && Value <= 32;
656 bool isARMSOImm() const {
657 if (Kind != k_Immediate)
659 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
660 if (!CE) return false;
661 int64_t Value = CE->getValue();
662 return ARM_AM::getSOImmVal(Value) != -1;
664 bool isARMSOImmNot() const {
665 if (Kind != k_Immediate)
667 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
668 if (!CE) return false;
669 int64_t Value = CE->getValue();
670 return ARM_AM::getSOImmVal(~Value) != -1;
672 bool isT2SOImm() const {
673 if (Kind != k_Immediate)
675 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
676 if (!CE) return false;
677 int64_t Value = CE->getValue();
678 return ARM_AM::getT2SOImmVal(Value) != -1;
680 bool isT2SOImmNot() const {
681 if (Kind != k_Immediate)
683 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
684 if (!CE) return false;
685 int64_t Value = CE->getValue();
686 return ARM_AM::getT2SOImmVal(~Value) != -1;
688 bool isSetEndImm() const {
689 if (Kind != k_Immediate)
691 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
692 if (!CE) return false;
693 int64_t Value = CE->getValue();
694 return Value == 1 || Value == 0;
696 bool isReg() const { return Kind == k_Register; }
697 bool isRegList() const { return Kind == k_RegisterList; }
698 bool isDPRRegList() const { return Kind == k_DPRRegisterList; }
699 bool isSPRRegList() const { return Kind == k_SPRRegisterList; }
700 bool isToken() const { return Kind == k_Token; }
701 bool isMemBarrierOpt() const { return Kind == k_MemBarrierOpt; }
702 bool isMemory() const { return Kind == k_Memory; }
703 bool isShifterImm() const { return Kind == k_ShifterImmediate; }
704 bool isRegShiftedReg() const { return Kind == k_ShiftedRegister; }
705 bool isRegShiftedImm() const { return Kind == k_ShiftedImmediate; }
706 bool isRotImm() const { return Kind == k_RotateImmediate; }
707 bool isBitfield() const { return Kind == k_BitfieldDescriptor; }
708 bool isPostIdxRegShifted() const { return Kind == k_PostIndexRegister; }
709 bool isPostIdxReg() const {
710 return Kind == k_PostIndexRegister && PostIdxReg.ShiftTy == ARM_AM::no_shift;
712 bool isMemNoOffset(bool alignOK = false) const {
715 // No offset of any kind.
716 return Memory.OffsetRegNum == 0 && Memory.OffsetImm == 0 &&
717 (alignOK || Memory.Alignment == 0);
719 bool isAlignedMemory() const {
720 return isMemNoOffset(true);
722 bool isAddrMode2() const {
723 if (!isMemory() || Memory.Alignment != 0) return false;
724 // Check for register offset.
725 if (Memory.OffsetRegNum) return true;
726 // Immediate offset in range [-4095, 4095].
727 if (!Memory.OffsetImm) return true;
728 int64_t Val = Memory.OffsetImm->getValue();
729 return Val > -4096 && Val < 4096;
731 bool isAM2OffsetImm() const {
732 if (Kind != k_Immediate)
734 // Immediate offset in range [-4095, 4095].
735 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
736 if (!CE) return false;
737 int64_t Val = CE->getValue();
738 return Val > -4096 && Val < 4096;
740 bool isAddrMode3() const {
741 if (!isMemory() || Memory.Alignment != 0) return false;
742 // No shifts are legal for AM3.
743 if (Memory.ShiftType != ARM_AM::no_shift) return false;
744 // Check for register offset.
745 if (Memory.OffsetRegNum) return true;
746 // Immediate offset in range [-255, 255].
747 if (!Memory.OffsetImm) return true;
748 int64_t Val = Memory.OffsetImm->getValue();
749 return Val > -256 && Val < 256;
751 bool isAM3Offset() const {
752 if (Kind != k_Immediate && Kind != k_PostIndexRegister)
754 if (Kind == k_PostIndexRegister)
755 return PostIdxReg.ShiftTy == ARM_AM::no_shift;
756 // Immediate offset in range [-255, 255].
757 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
758 if (!CE) return false;
759 int64_t Val = CE->getValue();
760 // Special case, #-0 is INT32_MIN.
761 return (Val > -256 && Val < 256) || Val == INT32_MIN;
763 bool isAddrMode5() const {
764 if (!isMemory() || Memory.Alignment != 0) return false;
765 // Check for register offset.
766 if (Memory.OffsetRegNum) return false;
767 // Immediate offset in range [-1020, 1020] and a multiple of 4.
768 if (!Memory.OffsetImm) return true;
769 int64_t Val = Memory.OffsetImm->getValue();
770 return (Val >= -1020 && Val <= 1020 && ((Val & 3) == 0)) ||
773 bool isMemTBB() const {
774 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
775 Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
779 bool isMemTBH() const {
780 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
781 Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm != 1 ||
782 Memory.Alignment != 0 )
786 bool isMemRegOffset() const {
787 if (!isMemory() || !Memory.OffsetRegNum || Memory.Alignment != 0)
791 bool isT2MemRegOffset() const {
792 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
793 Memory.Alignment != 0)
795 // Only lsl #{0, 1, 2, 3} allowed.
796 if (Memory.ShiftType == ARM_AM::no_shift)
798 if (Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm > 3)
802 bool isMemThumbRR() const {
803 // Thumb reg+reg addressing is simple. Just two registers, a base and
804 // an offset. No shifts, negations or any other complicating factors.
805 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
806 Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
808 return isARMLowRegister(Memory.BaseRegNum) &&
809 (!Memory.OffsetRegNum || isARMLowRegister(Memory.OffsetRegNum));
811 bool isMemThumbRIs4() const {
812 if (!isMemory() || Memory.OffsetRegNum != 0 ||
813 !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
815 // Immediate offset, multiple of 4 in range [0, 124].
816 if (!Memory.OffsetImm) return true;
817 int64_t Val = Memory.OffsetImm->getValue();
818 return Val >= 0 && Val <= 124 && (Val % 4) == 0;
820 bool isMemThumbRIs2() const {
821 if (!isMemory() || Memory.OffsetRegNum != 0 ||
822 !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
824 // Immediate offset, multiple of 4 in range [0, 62].
825 if (!Memory.OffsetImm) return true;
826 int64_t Val = Memory.OffsetImm->getValue();
827 return Val >= 0 && Val <= 62 && (Val % 2) == 0;
829 bool isMemThumbRIs1() const {
830 if (!isMemory() || Memory.OffsetRegNum != 0 ||
831 !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
833 // Immediate offset in range [0, 31].
834 if (!Memory.OffsetImm) return true;
835 int64_t Val = Memory.OffsetImm->getValue();
836 return Val >= 0 && Val <= 31;
838 bool isMemThumbSPI() const {
839 if (!isMemory() || Memory.OffsetRegNum != 0 ||
840 Memory.BaseRegNum != ARM::SP || Memory.Alignment != 0)
842 // Immediate offset, multiple of 4 in range [0, 1020].
843 if (!Memory.OffsetImm) return true;
844 int64_t Val = Memory.OffsetImm->getValue();
845 return Val >= 0 && Val <= 1020 && (Val % 4) == 0;
847 bool isMemImm8s4Offset() const {
848 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
850 // Immediate offset a multiple of 4 in range [-1020, 1020].
851 if (!Memory.OffsetImm) return true;
852 int64_t Val = Memory.OffsetImm->getValue();
853 return Val >= -1020 && Val <= 1020 && (Val & 3) == 0;
855 bool isMemImm0_1020s4Offset() const {
856 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
858 // Immediate offset a multiple of 4 in range [0, 1020].
859 if (!Memory.OffsetImm) return true;
860 int64_t Val = Memory.OffsetImm->getValue();
861 return Val >= 0 && Val <= 1020 && (Val & 3) == 0;
863 bool isMemImm8Offset() const {
864 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
866 // Immediate offset in range [-255, 255].
867 if (!Memory.OffsetImm) return true;
868 int64_t Val = Memory.OffsetImm->getValue();
869 return (Val == INT32_MIN) || (Val > -256 && Val < 256);
871 bool isMemPosImm8Offset() const {
872 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
874 // Immediate offset in range [0, 255].
875 if (!Memory.OffsetImm) return true;
876 int64_t Val = Memory.OffsetImm->getValue();
877 return Val >= 0 && Val < 256;
879 bool isMemNegImm8Offset() const {
880 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
882 // Immediate offset in range [-255, -1].
883 if (!Memory.OffsetImm) return true;
884 int64_t Val = Memory.OffsetImm->getValue();
885 return Val > -256 && Val < 0;
887 bool isMemUImm12Offset() const {
888 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
890 // Immediate offset in range [0, 4095].
891 if (!Memory.OffsetImm) return true;
892 int64_t Val = Memory.OffsetImm->getValue();
893 return (Val >= 0 && Val < 4096);
895 bool isMemImm12Offset() const {
896 // If we have an immediate that's not a constant, treat it as a label
897 // reference needing a fixup. If it is a constant, it's something else
899 if (Kind == k_Immediate && !isa<MCConstantExpr>(getImm()))
902 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
904 // Immediate offset in range [-4095, 4095].
905 if (!Memory.OffsetImm) return true;
906 int64_t Val = Memory.OffsetImm->getValue();
907 return (Val > -4096 && Val < 4096) || (Val == INT32_MIN);
909 bool isPostIdxImm8() const {
910 if (Kind != k_Immediate)
912 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
913 if (!CE) return false;
914 int64_t Val = CE->getValue();
915 return (Val > -256 && Val < 256) || (Val == INT32_MIN);
917 bool isPostIdxImm8s4() const {
918 if (Kind != k_Immediate)
920 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
921 if (!CE) return false;
922 int64_t Val = CE->getValue();
923 return ((Val & 3) == 0 && Val >= -1020 && Val <= 1020) ||
927 bool isMSRMask() const { return Kind == k_MSRMask; }
928 bool isProcIFlags() const { return Kind == k_ProcIFlags; }
931 bool isVecListOneD() const {
932 if (Kind != k_VectorList) return false;
933 return VectorList.Count == 1;
936 bool isVecListTwoD() const {
937 if (Kind != k_VectorList) return false;
938 return VectorList.Count == 2;
941 bool isVecListThreeD() const {
942 if (Kind != k_VectorList) return false;
943 return VectorList.Count == 3;
946 bool isVecListFourD() const {
947 if (Kind != k_VectorList) return false;
948 return VectorList.Count == 4;
951 bool isVecListTwoQ() const {
952 if (Kind != k_VectorList) return false;
953 //FIXME: We haven't taught the parser to handle by-two register lists
954 // yet, so don't pretend to know one.
955 return VectorList.Count == 2 && false;
958 bool isVectorIndex8() const {
959 if (Kind != k_VectorIndex) return false;
960 return VectorIndex.Val < 8;
962 bool isVectorIndex16() const {
963 if (Kind != k_VectorIndex) return false;
964 return VectorIndex.Val < 4;
966 bool isVectorIndex32() const {
967 if (Kind != k_VectorIndex) return false;
968 return VectorIndex.Val < 2;
971 bool isNEONi8splat() const {
972 if (Kind != k_Immediate)
974 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
975 // Must be a constant.
976 if (!CE) return false;
977 int64_t Value = CE->getValue();
978 // i8 value splatted across 8 bytes. The immediate is just the 8 byte
980 return Value >= 0 && Value < 256;
983 bool isNEONi16splat() const {
984 if (Kind != k_Immediate)
986 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
987 // Must be a constant.
988 if (!CE) return false;
989 int64_t Value = CE->getValue();
990 // i16 value in the range [0,255] or [0x0100, 0xff00]
991 return (Value >= 0 && Value < 256) || (Value >= 0x0100 && Value <= 0xff00);
994 bool isNEONi32splat() const {
995 if (Kind != k_Immediate)
997 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
998 // Must be a constant.
999 if (!CE) return false;
1000 int64_t Value = CE->getValue();
1001 // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X.
1002 return (Value >= 0 && Value < 256) ||
1003 (Value >= 0x0100 && Value <= 0xff00) ||
1004 (Value >= 0x010000 && Value <= 0xff0000) ||
1005 (Value >= 0x01000000 && Value <= 0xff000000);
1008 bool isNEONi32vmov() const {
1009 if (Kind != k_Immediate)
1011 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1012 // Must be a constant.
1013 if (!CE) return false;
1014 int64_t Value = CE->getValue();
1015 // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X,
1016 // for VMOV/VMVN only, 00Xf or 0Xff are also accepted.
1017 return (Value >= 0 && Value < 256) ||
1018 (Value >= 0x0100 && Value <= 0xff00) ||
1019 (Value >= 0x010000 && Value <= 0xff0000) ||
1020 (Value >= 0x01000000 && Value <= 0xff000000) ||
1021 (Value >= 0x01ff && Value <= 0xffff && (Value & 0xff) == 0xff) ||
1022 (Value >= 0x01ffff && Value <= 0xffffff && (Value & 0xffff) == 0xffff);
1025 bool isNEONi64splat() const {
1026 if (Kind != k_Immediate)
1028 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1029 // Must be a constant.
1030 if (!CE) return false;
1031 uint64_t Value = CE->getValue();
1032 // i64 value with each byte being either 0 or 0xff.
1033 for (unsigned i = 0; i < 8; ++i)
1034 if ((Value & 0xff) != 0 && (Value & 0xff) != 0xff) return false;
1038 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
1039 // Add as immediates when possible. Null MCExpr = 0.
1041 Inst.addOperand(MCOperand::CreateImm(0));
1042 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
1043 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
1045 Inst.addOperand(MCOperand::CreateExpr(Expr));
1048 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
1049 assert(N == 2 && "Invalid number of operands!");
1050 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
1051 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
1052 Inst.addOperand(MCOperand::CreateReg(RegNum));
1055 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
1056 assert(N == 1 && "Invalid number of operands!");
1057 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
1060 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
1061 assert(N == 1 && "Invalid number of operands!");
1062 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
1065 void addCoprocOptionOperands(MCInst &Inst, unsigned N) const {
1066 assert(N == 1 && "Invalid number of operands!");
1067 Inst.addOperand(MCOperand::CreateImm(CoprocOption.Val));
1070 void addITMaskOperands(MCInst &Inst, unsigned N) const {
1071 assert(N == 1 && "Invalid number of operands!");
1072 Inst.addOperand(MCOperand::CreateImm(ITMask.Mask));
1075 void addITCondCodeOperands(MCInst &Inst, unsigned N) const {
1076 assert(N == 1 && "Invalid number of operands!");
1077 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
1080 void addCCOutOperands(MCInst &Inst, unsigned N) const {
1081 assert(N == 1 && "Invalid number of operands!");
1082 Inst.addOperand(MCOperand::CreateReg(getReg()));
1085 void addRegOperands(MCInst &Inst, unsigned N) const {
1086 assert(N == 1 && "Invalid number of operands!");
1087 Inst.addOperand(MCOperand::CreateReg(getReg()));
1090 void addRegShiftedRegOperands(MCInst &Inst, unsigned N) const {
1091 assert(N == 3 && "Invalid number of operands!");
1092 assert(isRegShiftedReg() && "addRegShiftedRegOperands() on non RegShiftedReg!");
1093 Inst.addOperand(MCOperand::CreateReg(RegShiftedReg.SrcReg));
1094 Inst.addOperand(MCOperand::CreateReg(RegShiftedReg.ShiftReg));
1095 Inst.addOperand(MCOperand::CreateImm(
1096 ARM_AM::getSORegOpc(RegShiftedReg.ShiftTy, RegShiftedReg.ShiftImm)));
1099 void addRegShiftedImmOperands(MCInst &Inst, unsigned N) const {
1100 assert(N == 2 && "Invalid number of operands!");
1101 assert(isRegShiftedImm() && "addRegShiftedImmOperands() on non RegShiftedImm!");
1102 Inst.addOperand(MCOperand::CreateReg(RegShiftedImm.SrcReg));
1103 Inst.addOperand(MCOperand::CreateImm(
1104 ARM_AM::getSORegOpc(RegShiftedImm.ShiftTy, RegShiftedImm.ShiftImm)));
1107 void addShifterImmOperands(MCInst &Inst, unsigned N) const {
1108 assert(N == 1 && "Invalid number of operands!");
1109 Inst.addOperand(MCOperand::CreateImm((ShifterImm.isASR << 5) |
1113 void addRegListOperands(MCInst &Inst, unsigned N) const {
1114 assert(N == 1 && "Invalid number of operands!");
1115 const SmallVectorImpl<unsigned> &RegList = getRegList();
1116 for (SmallVectorImpl<unsigned>::const_iterator
1117 I = RegList.begin(), E = RegList.end(); I != E; ++I)
1118 Inst.addOperand(MCOperand::CreateReg(*I));
1121 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
1122 addRegListOperands(Inst, N);
1125 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
1126 addRegListOperands(Inst, N);
1129 void addRotImmOperands(MCInst &Inst, unsigned N) const {
1130 assert(N == 1 && "Invalid number of operands!");
1131 // Encoded as val>>3. The printer handles display as 8, 16, 24.
1132 Inst.addOperand(MCOperand::CreateImm(RotImm.Imm >> 3));
1135 void addBitfieldOperands(MCInst &Inst, unsigned N) const {
1136 assert(N == 1 && "Invalid number of operands!");
1137 // Munge the lsb/width into a bitfield mask.
1138 unsigned lsb = Bitfield.LSB;
1139 unsigned width = Bitfield.Width;
1140 // Make a 32-bit mask w/ the referenced bits clear and all other bits set.
1141 uint32_t Mask = ~(((uint32_t)0xffffffff >> lsb) << (32 - width) >>
1142 (32 - (lsb + width)));
1143 Inst.addOperand(MCOperand::CreateImm(Mask));
1146 void addImmOperands(MCInst &Inst, unsigned N) const {
1147 assert(N == 1 && "Invalid number of operands!");
1148 addExpr(Inst, getImm());
1151 void addFPImmOperands(MCInst &Inst, unsigned N) const {
1152 assert(N == 1 && "Invalid number of operands!");
1153 Inst.addOperand(MCOperand::CreateImm(getFPImm()));
1156 void addImm8s4Operands(MCInst &Inst, unsigned N) const {
1157 assert(N == 1 && "Invalid number of operands!");
1158 // FIXME: We really want to scale the value here, but the LDRD/STRD
1159 // instruction don't encode operands that way yet.
1160 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1161 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
1164 void addImm0_1020s4Operands(MCInst &Inst, unsigned N) const {
1165 assert(N == 1 && "Invalid number of operands!");
1166 // The immediate is scaled by four in the encoding and is stored
1167 // in the MCInst as such. Lop off the low two bits here.
1168 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1169 Inst.addOperand(MCOperand::CreateImm(CE->getValue() / 4));
1172 void addImm0_508s4Operands(MCInst &Inst, unsigned N) const {
1173 assert(N == 1 && "Invalid number of operands!");
1174 // The immediate is scaled by four in the encoding and is stored
1175 // in the MCInst as such. Lop off the low two bits here.
1176 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1177 Inst.addOperand(MCOperand::CreateImm(CE->getValue() / 4));
1180 void addImm0_255Operands(MCInst &Inst, unsigned N) const {
1181 assert(N == 1 && "Invalid number of operands!");
1182 addExpr(Inst, getImm());
1185 void addImm0_7Operands(MCInst &Inst, unsigned N) const {
1186 assert(N == 1 && "Invalid number of operands!");
1187 addExpr(Inst, getImm());
1190 void addImm0_15Operands(MCInst &Inst, unsigned N) const {
1191 assert(N == 1 && "Invalid number of operands!");
1192 addExpr(Inst, getImm());
1195 void addImm0_31Operands(MCInst &Inst, unsigned N) const {
1196 assert(N == 1 && "Invalid number of operands!");
1197 addExpr(Inst, getImm());
1200 void addImm1_16Operands(MCInst &Inst, unsigned N) const {
1201 assert(N == 1 && "Invalid number of operands!");
1202 // The constant encodes as the immediate-1, and we store in the instruction
1203 // the bits as encoded, so subtract off one here.
1204 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1205 Inst.addOperand(MCOperand::CreateImm(CE->getValue() - 1));
1208 void addImm1_32Operands(MCInst &Inst, unsigned N) const {
1209 assert(N == 1 && "Invalid number of operands!");
1210 // The constant encodes as the immediate-1, and we store in the instruction
1211 // the bits as encoded, so subtract off one here.
1212 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1213 Inst.addOperand(MCOperand::CreateImm(CE->getValue() - 1));
1216 void addImm0_65535Operands(MCInst &Inst, unsigned N) const {
1217 assert(N == 1 && "Invalid number of operands!");
1218 addExpr(Inst, getImm());
1221 void addImm0_65535ExprOperands(MCInst &Inst, unsigned N) const {
1222 assert(N == 1 && "Invalid number of operands!");
1223 addExpr(Inst, getImm());
1226 void addImm24bitOperands(MCInst &Inst, unsigned N) const {
1227 assert(N == 1 && "Invalid number of operands!");
1228 addExpr(Inst, getImm());
1231 void addImmThumbSROperands(MCInst &Inst, unsigned N) const {
1232 assert(N == 1 && "Invalid number of operands!");
1233 // The constant encodes as the immediate, except for 32, which encodes as
1235 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1236 unsigned Imm = CE->getValue();
1237 Inst.addOperand(MCOperand::CreateImm((Imm == 32 ? 0 : Imm)));
1240 void addPKHLSLImmOperands(MCInst &Inst, unsigned N) const {
1241 assert(N == 1 && "Invalid number of operands!");
1242 addExpr(Inst, getImm());
1245 void addPKHASRImmOperands(MCInst &Inst, unsigned N) const {
1246 assert(N == 1 && "Invalid number of operands!");
1247 // An ASR value of 32 encodes as 0, so that's how we want to add it to
1248 // the instruction as well.
1249 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1250 int Val = CE->getValue();
1251 Inst.addOperand(MCOperand::CreateImm(Val == 32 ? 0 : Val));
1254 void addARMSOImmOperands(MCInst &Inst, unsigned N) const {
1255 assert(N == 1 && "Invalid number of operands!");
1256 addExpr(Inst, getImm());
1259 void addT2SOImmOperands(MCInst &Inst, unsigned N) const {
1260 assert(N == 1 && "Invalid number of operands!");
1261 addExpr(Inst, getImm());
1264 void addT2SOImmNotOperands(MCInst &Inst, unsigned N) const {
1265 assert(N == 1 && "Invalid number of operands!");
1266 // The operand is actually a t2_so_imm, but we have its bitwise
1267 // negation in the assembly source, so twiddle it here.
1268 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1269 Inst.addOperand(MCOperand::CreateImm(~CE->getValue()));
1272 void addARMSOImmNotOperands(MCInst &Inst, unsigned N) const {
1273 assert(N == 1 && "Invalid number of operands!");
1274 // The operand is actually a so_imm, but we have its bitwise
1275 // negation in the assembly source, so twiddle it here.
1276 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1277 Inst.addOperand(MCOperand::CreateImm(~CE->getValue()));
1280 void addSetEndImmOperands(MCInst &Inst, unsigned N) const {
1281 assert(N == 1 && "Invalid number of operands!");
1282 addExpr(Inst, getImm());
1285 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
1286 assert(N == 1 && "Invalid number of operands!");
1287 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
1290 void addMemNoOffsetOperands(MCInst &Inst, unsigned N) const {
1291 assert(N == 1 && "Invalid number of operands!");
1292 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1295 void addAlignedMemoryOperands(MCInst &Inst, unsigned N) const {
1296 assert(N == 2 && "Invalid number of operands!");
1297 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1298 Inst.addOperand(MCOperand::CreateImm(Memory.Alignment));
1301 void addAddrMode2Operands(MCInst &Inst, unsigned N) const {
1302 assert(N == 3 && "Invalid number of operands!");
1303 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1304 if (!Memory.OffsetRegNum) {
1305 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1306 // Special case for #-0
1307 if (Val == INT32_MIN) Val = 0;
1308 if (Val < 0) Val = -Val;
1309 Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
1311 // For register offset, we encode the shift type and negation flag
1313 Val = ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
1314 Memory.ShiftImm, Memory.ShiftType);
1316 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1317 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1318 Inst.addOperand(MCOperand::CreateImm(Val));
1321 void addAM2OffsetImmOperands(MCInst &Inst, unsigned N) const {
1322 assert(N == 2 && "Invalid number of operands!");
1323 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1324 assert(CE && "non-constant AM2OffsetImm operand!");
1325 int32_t Val = CE->getValue();
1326 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1327 // Special case for #-0
1328 if (Val == INT32_MIN) Val = 0;
1329 if (Val < 0) Val = -Val;
1330 Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
1331 Inst.addOperand(MCOperand::CreateReg(0));
1332 Inst.addOperand(MCOperand::CreateImm(Val));
1335 void addAddrMode3Operands(MCInst &Inst, unsigned N) const {
1336 assert(N == 3 && "Invalid number of operands!");
1337 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1338 if (!Memory.OffsetRegNum) {
1339 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1340 // Special case for #-0
1341 if (Val == INT32_MIN) Val = 0;
1342 if (Val < 0) Val = -Val;
1343 Val = ARM_AM::getAM3Opc(AddSub, Val);
1345 // For register offset, we encode the shift type and negation flag
1347 Val = ARM_AM::getAM3Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add, 0);
1349 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1350 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1351 Inst.addOperand(MCOperand::CreateImm(Val));
1354 void addAM3OffsetOperands(MCInst &Inst, unsigned N) const {
1355 assert(N == 2 && "Invalid number of operands!");
1356 if (Kind == k_PostIndexRegister) {
1358 ARM_AM::getAM3Opc(PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub, 0);
1359 Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1360 Inst.addOperand(MCOperand::CreateImm(Val));
1365 const MCConstantExpr *CE = static_cast<const MCConstantExpr*>(getImm());
1366 int32_t Val = CE->getValue();
1367 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1368 // Special case for #-0
1369 if (Val == INT32_MIN) Val = 0;
1370 if (Val < 0) Val = -Val;
1371 Val = ARM_AM::getAM3Opc(AddSub, Val);
1372 Inst.addOperand(MCOperand::CreateReg(0));
1373 Inst.addOperand(MCOperand::CreateImm(Val));
1376 void addAddrMode5Operands(MCInst &Inst, unsigned N) const {
1377 assert(N == 2 && "Invalid number of operands!");
1378 // The lower two bits are always zero and as such are not encoded.
1379 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
1380 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1381 // Special case for #-0
1382 if (Val == INT32_MIN) Val = 0;
1383 if (Val < 0) Val = -Val;
1384 Val = ARM_AM::getAM5Opc(AddSub, Val);
1385 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1386 Inst.addOperand(MCOperand::CreateImm(Val));
1389 void addMemImm8s4OffsetOperands(MCInst &Inst, unsigned N) const {
1390 assert(N == 2 && "Invalid number of operands!");
1391 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1392 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1393 Inst.addOperand(MCOperand::CreateImm(Val));
1396 void addMemImm0_1020s4OffsetOperands(MCInst &Inst, unsigned N) const {
1397 assert(N == 2 && "Invalid number of operands!");
1398 // The lower two bits are always zero and as such are not encoded.
1399 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
1400 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1401 Inst.addOperand(MCOperand::CreateImm(Val));
1404 void addMemImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1405 assert(N == 2 && "Invalid number of operands!");
1406 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1407 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1408 Inst.addOperand(MCOperand::CreateImm(Val));
1411 void addMemPosImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1412 addMemImm8OffsetOperands(Inst, N);
1415 void addMemNegImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1416 addMemImm8OffsetOperands(Inst, N);
1419 void addMemUImm12OffsetOperands(MCInst &Inst, unsigned N) const {
1420 assert(N == 2 && "Invalid number of operands!");
1421 // If this is an immediate, it's a label reference.
1422 if (Kind == k_Immediate) {
1423 addExpr(Inst, getImm());
1424 Inst.addOperand(MCOperand::CreateImm(0));
1428 // Otherwise, it's a normal memory reg+offset.
1429 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1430 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1431 Inst.addOperand(MCOperand::CreateImm(Val));
1434 void addMemImm12OffsetOperands(MCInst &Inst, unsigned N) const {
1435 assert(N == 2 && "Invalid number of operands!");
1436 // If this is an immediate, it's a label reference.
1437 if (Kind == k_Immediate) {
1438 addExpr(Inst, getImm());
1439 Inst.addOperand(MCOperand::CreateImm(0));
1443 // Otherwise, it's a normal memory reg+offset.
1444 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1445 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1446 Inst.addOperand(MCOperand::CreateImm(Val));
1449 void addMemTBBOperands(MCInst &Inst, unsigned N) const {
1450 assert(N == 2 && "Invalid number of operands!");
1451 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1452 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1455 void addMemTBHOperands(MCInst &Inst, unsigned N) const {
1456 assert(N == 2 && "Invalid number of operands!");
1457 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1458 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1461 void addMemRegOffsetOperands(MCInst &Inst, unsigned N) const {
1462 assert(N == 3 && "Invalid number of operands!");
1463 unsigned Val = ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
1464 Memory.ShiftImm, Memory.ShiftType);
1465 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1466 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1467 Inst.addOperand(MCOperand::CreateImm(Val));
1470 void addT2MemRegOffsetOperands(MCInst &Inst, unsigned N) const {
1471 assert(N == 3 && "Invalid number of operands!");
1472 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1473 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1474 Inst.addOperand(MCOperand::CreateImm(Memory.ShiftImm));
1477 void addMemThumbRROperands(MCInst &Inst, unsigned N) const {
1478 assert(N == 2 && "Invalid number of operands!");
1479 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1480 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1483 void addMemThumbRIs4Operands(MCInst &Inst, unsigned N) const {
1484 assert(N == 2 && "Invalid number of operands!");
1485 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
1486 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1487 Inst.addOperand(MCOperand::CreateImm(Val));
1490 void addMemThumbRIs2Operands(MCInst &Inst, unsigned N) const {
1491 assert(N == 2 && "Invalid number of operands!");
1492 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 2) : 0;
1493 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1494 Inst.addOperand(MCOperand::CreateImm(Val));
1497 void addMemThumbRIs1Operands(MCInst &Inst, unsigned N) const {
1498 assert(N == 2 && "Invalid number of operands!");
1499 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue()) : 0;
1500 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1501 Inst.addOperand(MCOperand::CreateImm(Val));
1504 void addMemThumbSPIOperands(MCInst &Inst, unsigned N) const {
1505 assert(N == 2 && "Invalid number of operands!");
1506 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
1507 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1508 Inst.addOperand(MCOperand::CreateImm(Val));
1511 void addPostIdxImm8Operands(MCInst &Inst, unsigned N) const {
1512 assert(N == 1 && "Invalid number of operands!");
1513 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1514 assert(CE && "non-constant post-idx-imm8 operand!");
1515 int Imm = CE->getValue();
1516 bool isAdd = Imm >= 0;
1517 if (Imm == INT32_MIN) Imm = 0;
1518 Imm = (Imm < 0 ? -Imm : Imm) | (int)isAdd << 8;
1519 Inst.addOperand(MCOperand::CreateImm(Imm));
1522 void addPostIdxImm8s4Operands(MCInst &Inst, unsigned N) const {
1523 assert(N == 1 && "Invalid number of operands!");
1524 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1525 assert(CE && "non-constant post-idx-imm8s4 operand!");
1526 int Imm = CE->getValue();
1527 bool isAdd = Imm >= 0;
1528 if (Imm == INT32_MIN) Imm = 0;
1529 // Immediate is scaled by 4.
1530 Imm = ((Imm < 0 ? -Imm : Imm) / 4) | (int)isAdd << 8;
1531 Inst.addOperand(MCOperand::CreateImm(Imm));
1534 void addPostIdxRegOperands(MCInst &Inst, unsigned N) const {
1535 assert(N == 2 && "Invalid number of operands!");
1536 Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1537 Inst.addOperand(MCOperand::CreateImm(PostIdxReg.isAdd));
1540 void addPostIdxRegShiftedOperands(MCInst &Inst, unsigned N) const {
1541 assert(N == 2 && "Invalid number of operands!");
1542 Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1543 // The sign, shift type, and shift amount are encoded in a single operand
1544 // using the AM2 encoding helpers.
1545 ARM_AM::AddrOpc opc = PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub;
1546 unsigned Imm = ARM_AM::getAM2Opc(opc, PostIdxReg.ShiftImm,
1547 PostIdxReg.ShiftTy);
1548 Inst.addOperand(MCOperand::CreateImm(Imm));
1551 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
1552 assert(N == 1 && "Invalid number of operands!");
1553 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
1556 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
1557 assert(N == 1 && "Invalid number of operands!");
1558 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
1561 void addVecListOneDOperands(MCInst &Inst, unsigned N) const {
1562 assert(N == 1 && "Invalid number of operands!");
1563 Inst.addOperand(MCOperand::CreateReg(VectorList.RegNum));
1566 void addVecListTwoDOperands(MCInst &Inst, unsigned N) const {
1567 assert(N == 1 && "Invalid number of operands!");
1568 // Only the first register actually goes on the instruction. The rest
1569 // are implied by the opcode.
1570 Inst.addOperand(MCOperand::CreateReg(VectorList.RegNum));
1573 void addVecListThreeDOperands(MCInst &Inst, unsigned N) const {
1574 assert(N == 1 && "Invalid number of operands!");
1575 // Only the first register actually goes on the instruction. The rest
1576 // are implied by the opcode.
1577 Inst.addOperand(MCOperand::CreateReg(VectorList.RegNum));
1580 void addVecListFourDOperands(MCInst &Inst, unsigned N) const {
1581 assert(N == 1 && "Invalid number of operands!");
1582 // Only the first register actually goes on the instruction. The rest
1583 // are implied by the opcode.
1584 Inst.addOperand(MCOperand::CreateReg(VectorList.RegNum));
1587 void addVecListTwoQOperands(MCInst &Inst, unsigned N) const {
1588 assert(N == 1 && "Invalid number of operands!");
1589 // Only the first register actually goes on the instruction. The rest
1590 // are implied by the opcode.
1591 Inst.addOperand(MCOperand::CreateReg(VectorList.RegNum));
1594 void addVectorIndex8Operands(MCInst &Inst, unsigned N) const {
1595 assert(N == 1 && "Invalid number of operands!");
1596 Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1599 void addVectorIndex16Operands(MCInst &Inst, unsigned N) const {
1600 assert(N == 1 && "Invalid number of operands!");
1601 Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1604 void addVectorIndex32Operands(MCInst &Inst, unsigned N) const {
1605 assert(N == 1 && "Invalid number of operands!");
1606 Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1609 void addNEONi8splatOperands(MCInst &Inst, unsigned N) const {
1610 assert(N == 1 && "Invalid number of operands!");
1611 // The immediate encodes the type of constant as well as the value.
1612 // Mask in that this is an i8 splat.
1613 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1614 Inst.addOperand(MCOperand::CreateImm(CE->getValue() | 0xe00));
1617 void addNEONi16splatOperands(MCInst &Inst, unsigned N) const {
1618 assert(N == 1 && "Invalid number of operands!");
1619 // The immediate encodes the type of constant as well as the value.
1620 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1621 unsigned Value = CE->getValue();
1623 Value = (Value >> 8) | 0xa00;
1626 Inst.addOperand(MCOperand::CreateImm(Value));
1629 void addNEONi32splatOperands(MCInst &Inst, unsigned N) const {
1630 assert(N == 1 && "Invalid number of operands!");
1631 // The immediate encodes the type of constant as well as the value.
1632 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1633 unsigned Value = CE->getValue();
1634 if (Value >= 256 && Value <= 0xff00)
1635 Value = (Value >> 8) | 0x200;
1636 else if (Value > 0xffff && Value <= 0xff0000)
1637 Value = (Value >> 16) | 0x400;
1638 else if (Value > 0xffffff)
1639 Value = (Value >> 24) | 0x600;
1640 Inst.addOperand(MCOperand::CreateImm(Value));
1643 void addNEONi32vmovOperands(MCInst &Inst, unsigned N) const {
1644 assert(N == 1 && "Invalid number of operands!");
1645 // The immediate encodes the type of constant as well as the value.
1646 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1647 unsigned Value = CE->getValue();
1648 if (Value >= 256 && Value <= 0xffff)
1649 Value = (Value >> 8) | ((Value & 0xff) ? 0xc00 : 0x200);
1650 else if (Value > 0xffff && Value <= 0xffffff)
1651 Value = (Value >> 16) | ((Value & 0xff) ? 0xd00 : 0x400);
1652 else if (Value > 0xffffff)
1653 Value = (Value >> 24) | 0x600;
1654 Inst.addOperand(MCOperand::CreateImm(Value));
1657 void addNEONi64splatOperands(MCInst &Inst, unsigned N) const {
1658 assert(N == 1 && "Invalid number of operands!");
1659 // The immediate encodes the type of constant as well as the value.
1660 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1661 uint64_t Value = CE->getValue();
1663 for (unsigned i = 0; i < 8; ++i, Value >>= 8) {
1664 Imm |= (Value & 1) << i;
1666 Inst.addOperand(MCOperand::CreateImm(Imm | 0x1e00));
1669 virtual void print(raw_ostream &OS) const;
1671 static ARMOperand *CreateITMask(unsigned Mask, SMLoc S) {
1672 ARMOperand *Op = new ARMOperand(k_ITCondMask);
1673 Op->ITMask.Mask = Mask;
1679 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
1680 ARMOperand *Op = new ARMOperand(k_CondCode);
1687 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
1688 ARMOperand *Op = new ARMOperand(k_CoprocNum);
1689 Op->Cop.Val = CopVal;
1695 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
1696 ARMOperand *Op = new ARMOperand(k_CoprocReg);
1697 Op->Cop.Val = CopVal;
1703 static ARMOperand *CreateCoprocOption(unsigned Val, SMLoc S, SMLoc E) {
1704 ARMOperand *Op = new ARMOperand(k_CoprocOption);
1711 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
1712 ARMOperand *Op = new ARMOperand(k_CCOut);
1713 Op->Reg.RegNum = RegNum;
1719 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
1720 ARMOperand *Op = new ARMOperand(k_Token);
1721 Op->Tok.Data = Str.data();
1722 Op->Tok.Length = Str.size();
1728 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
1729 ARMOperand *Op = new ARMOperand(k_Register);
1730 Op->Reg.RegNum = RegNum;
1736 static ARMOperand *CreateShiftedRegister(ARM_AM::ShiftOpc ShTy,
1741 ARMOperand *Op = new ARMOperand(k_ShiftedRegister);
1742 Op->RegShiftedReg.ShiftTy = ShTy;
1743 Op->RegShiftedReg.SrcReg = SrcReg;
1744 Op->RegShiftedReg.ShiftReg = ShiftReg;
1745 Op->RegShiftedReg.ShiftImm = ShiftImm;
1751 static ARMOperand *CreateShiftedImmediate(ARM_AM::ShiftOpc ShTy,
1755 ARMOperand *Op = new ARMOperand(k_ShiftedImmediate);
1756 Op->RegShiftedImm.ShiftTy = ShTy;
1757 Op->RegShiftedImm.SrcReg = SrcReg;
1758 Op->RegShiftedImm.ShiftImm = ShiftImm;
1764 static ARMOperand *CreateShifterImm(bool isASR, unsigned Imm,
1766 ARMOperand *Op = new ARMOperand(k_ShifterImmediate);
1767 Op->ShifterImm.isASR = isASR;
1768 Op->ShifterImm.Imm = Imm;
1774 static ARMOperand *CreateRotImm(unsigned Imm, SMLoc S, SMLoc E) {
1775 ARMOperand *Op = new ARMOperand(k_RotateImmediate);
1776 Op->RotImm.Imm = Imm;
1782 static ARMOperand *CreateBitfield(unsigned LSB, unsigned Width,
1784 ARMOperand *Op = new ARMOperand(k_BitfieldDescriptor);
1785 Op->Bitfield.LSB = LSB;
1786 Op->Bitfield.Width = Width;
1793 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
1794 SMLoc StartLoc, SMLoc EndLoc) {
1795 KindTy Kind = k_RegisterList;
1797 if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Regs.front().first))
1798 Kind = k_DPRRegisterList;
1799 else if (ARMMCRegisterClasses[ARM::SPRRegClassID].
1800 contains(Regs.front().first))
1801 Kind = k_SPRRegisterList;
1803 ARMOperand *Op = new ARMOperand(Kind);
1804 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
1805 I = Regs.begin(), E = Regs.end(); I != E; ++I)
1806 Op->Registers.push_back(I->first);
1807 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
1808 Op->StartLoc = StartLoc;
1809 Op->EndLoc = EndLoc;
1813 static ARMOperand *CreateVectorList(unsigned RegNum, unsigned Count,
1815 ARMOperand *Op = new ARMOperand(k_VectorList);
1816 Op->VectorList.RegNum = RegNum;
1817 Op->VectorList.Count = Count;
1823 static ARMOperand *CreateVectorIndex(unsigned Idx, SMLoc S, SMLoc E,
1825 ARMOperand *Op = new ARMOperand(k_VectorIndex);
1826 Op->VectorIndex.Val = Idx;
1832 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
1833 ARMOperand *Op = new ARMOperand(k_Immediate);
1840 static ARMOperand *CreateFPImm(unsigned Val, SMLoc S, MCContext &Ctx) {
1841 ARMOperand *Op = new ARMOperand(k_FPImmediate);
1842 Op->FPImm.Val = Val;
1848 static ARMOperand *CreateMem(unsigned BaseRegNum,
1849 const MCConstantExpr *OffsetImm,
1850 unsigned OffsetRegNum,
1851 ARM_AM::ShiftOpc ShiftType,
1856 ARMOperand *Op = new ARMOperand(k_Memory);
1857 Op->Memory.BaseRegNum = BaseRegNum;
1858 Op->Memory.OffsetImm = OffsetImm;
1859 Op->Memory.OffsetRegNum = OffsetRegNum;
1860 Op->Memory.ShiftType = ShiftType;
1861 Op->Memory.ShiftImm = ShiftImm;
1862 Op->Memory.Alignment = Alignment;
1863 Op->Memory.isNegative = isNegative;
1869 static ARMOperand *CreatePostIdxReg(unsigned RegNum, bool isAdd,
1870 ARM_AM::ShiftOpc ShiftTy,
1873 ARMOperand *Op = new ARMOperand(k_PostIndexRegister);
1874 Op->PostIdxReg.RegNum = RegNum;
1875 Op->PostIdxReg.isAdd = isAdd;
1876 Op->PostIdxReg.ShiftTy = ShiftTy;
1877 Op->PostIdxReg.ShiftImm = ShiftImm;
1883 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
1884 ARMOperand *Op = new ARMOperand(k_MemBarrierOpt);
1885 Op->MBOpt.Val = Opt;
1891 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
1892 ARMOperand *Op = new ARMOperand(k_ProcIFlags);
1893 Op->IFlags.Val = IFlags;
1899 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
1900 ARMOperand *Op = new ARMOperand(k_MSRMask);
1901 Op->MMask.Val = MMask;
1908 } // end anonymous namespace.
1910 void ARMOperand::print(raw_ostream &OS) const {
1913 OS << "<fpimm " << getFPImm() << "(" << ARM_AM::getFPImmFloat(getFPImm())
1917 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
1920 OS << "<ccout " << getReg() << ">";
1922 case k_ITCondMask: {
1923 static const char *MaskStr[] = {
1924 "()", "(t)", "(e)", "(tt)", "(et)", "(te)", "(ee)", "(ttt)", "(ett)",
1925 "(tet)", "(eet)", "(tte)", "(ete)", "(tee)", "(eee)"
1927 assert((ITMask.Mask & 0xf) == ITMask.Mask);
1928 OS << "<it-mask " << MaskStr[ITMask.Mask] << ">";
1932 OS << "<coprocessor number: " << getCoproc() << ">";
1935 OS << "<coprocessor register: " << getCoproc() << ">";
1937 case k_CoprocOption:
1938 OS << "<coprocessor option: " << CoprocOption.Val << ">";
1941 OS << "<mask: " << getMSRMask() << ">";
1944 getImm()->print(OS);
1946 case k_MemBarrierOpt:
1947 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
1951 << " base:" << Memory.BaseRegNum;
1954 case k_PostIndexRegister:
1955 OS << "post-idx register " << (PostIdxReg.isAdd ? "" : "-")
1956 << PostIdxReg.RegNum;
1957 if (PostIdxReg.ShiftTy != ARM_AM::no_shift)
1958 OS << ARM_AM::getShiftOpcStr(PostIdxReg.ShiftTy) << " "
1959 << PostIdxReg.ShiftImm;
1962 case k_ProcIFlags: {
1963 OS << "<ARM_PROC::";
1964 unsigned IFlags = getProcIFlags();
1965 for (int i=2; i >= 0; --i)
1966 if (IFlags & (1 << i))
1967 OS << ARM_PROC::IFlagsToString(1 << i);
1972 OS << "<register " << getReg() << ">";
1974 case k_ShifterImmediate:
1975 OS << "<shift " << (ShifterImm.isASR ? "asr" : "lsl")
1976 << " #" << ShifterImm.Imm << ">";
1978 case k_ShiftedRegister:
1979 OS << "<so_reg_reg "
1980 << RegShiftedReg.SrcReg
1981 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(RegShiftedReg.ShiftImm))
1982 << ", " << RegShiftedReg.ShiftReg << ", "
1983 << ARM_AM::getSORegOffset(RegShiftedReg.ShiftImm)
1986 case k_ShiftedImmediate:
1987 OS << "<so_reg_imm "
1988 << RegShiftedImm.SrcReg
1989 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(RegShiftedImm.ShiftImm))
1990 << ", " << ARM_AM::getSORegOffset(RegShiftedImm.ShiftImm)
1993 case k_RotateImmediate:
1994 OS << "<ror " << " #" << (RotImm.Imm * 8) << ">";
1996 case k_BitfieldDescriptor:
1997 OS << "<bitfield " << "lsb: " << Bitfield.LSB
1998 << ", width: " << Bitfield.Width << ">";
2000 case k_RegisterList:
2001 case k_DPRRegisterList:
2002 case k_SPRRegisterList: {
2003 OS << "<register_list ";
2005 const SmallVectorImpl<unsigned> &RegList = getRegList();
2006 for (SmallVectorImpl<unsigned>::const_iterator
2007 I = RegList.begin(), E = RegList.end(); I != E; ) {
2009 if (++I < E) OS << ", ";
2016 OS << "<vector_list " << VectorList.Count << " * "
2017 << VectorList.RegNum << ">";
2020 OS << "'" << getToken() << "'";
2023 OS << "<vectorindex " << getVectorIndex() << ">";
2028 /// @name Auto-generated Match Functions
2031 static unsigned MatchRegisterName(StringRef Name);
2035 bool ARMAsmParser::ParseRegister(unsigned &RegNo,
2036 SMLoc &StartLoc, SMLoc &EndLoc) {
2037 RegNo = tryParseRegister();
2039 return (RegNo == (unsigned)-1);
2042 /// Try to parse a register name. The token must be an Identifier when called,
2043 /// and if it is a register name the token is eaten and the register number is
2044 /// returned. Otherwise return -1.
2046 int ARMAsmParser::tryParseRegister() {
2047 const AsmToken &Tok = Parser.getTok();
2048 if (Tok.isNot(AsmToken::Identifier)) return -1;
2050 // FIXME: Validate register for the current architecture; we have to do
2051 // validation later, so maybe there is no need for this here.
2052 std::string upperCase = Tok.getString().str();
2053 std::string lowerCase = LowercaseString(upperCase);
2054 unsigned RegNum = MatchRegisterName(lowerCase);
2056 RegNum = StringSwitch<unsigned>(lowerCase)
2057 .Case("r13", ARM::SP)
2058 .Case("r14", ARM::LR)
2059 .Case("r15", ARM::PC)
2060 .Case("ip", ARM::R12)
2063 if (!RegNum) return -1;
2065 Parser.Lex(); // Eat identifier token.
2070 // Try to parse a shifter (e.g., "lsl <amt>"). On success, return 0.
2071 // If a recoverable error occurs, return 1. If an irrecoverable error
2072 // occurs, return -1. An irrecoverable error is one where tokens have been
2073 // consumed in the process of trying to parse the shifter (i.e., when it is
2074 // indeed a shifter operand, but malformed).
2075 int ARMAsmParser::tryParseShiftRegister(
2076 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2077 SMLoc S = Parser.getTok().getLoc();
2078 const AsmToken &Tok = Parser.getTok();
2079 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2081 std::string upperCase = Tok.getString().str();
2082 std::string lowerCase = LowercaseString(upperCase);
2083 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
2084 .Case("lsl", ARM_AM::lsl)
2085 .Case("lsr", ARM_AM::lsr)
2086 .Case("asr", ARM_AM::asr)
2087 .Case("ror", ARM_AM::ror)
2088 .Case("rrx", ARM_AM::rrx)
2089 .Default(ARM_AM::no_shift);
2091 if (ShiftTy == ARM_AM::no_shift)
2094 Parser.Lex(); // Eat the operator.
2096 // The source register for the shift has already been added to the
2097 // operand list, so we need to pop it off and combine it into the shifted
2098 // register operand instead.
2099 OwningPtr<ARMOperand> PrevOp((ARMOperand*)Operands.pop_back_val());
2100 if (!PrevOp->isReg())
2101 return Error(PrevOp->getStartLoc(), "shift must be of a register");
2102 int SrcReg = PrevOp->getReg();
2105 if (ShiftTy == ARM_AM::rrx) {
2106 // RRX Doesn't have an explicit shift amount. The encoder expects
2107 // the shift register to be the same as the source register. Seems odd,
2111 // Figure out if this is shifted by a constant or a register (for non-RRX).
2112 if (Parser.getTok().is(AsmToken::Hash)) {
2113 Parser.Lex(); // Eat hash.
2114 SMLoc ImmLoc = Parser.getTok().getLoc();
2115 const MCExpr *ShiftExpr = 0;
2116 if (getParser().ParseExpression(ShiftExpr)) {
2117 Error(ImmLoc, "invalid immediate shift value");
2120 // The expression must be evaluatable as an immediate.
2121 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
2123 Error(ImmLoc, "invalid immediate shift value");
2126 // Range check the immediate.
2127 // lsl, ror: 0 <= imm <= 31
2128 // lsr, asr: 0 <= imm <= 32
2129 Imm = CE->getValue();
2131 ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
2132 ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
2133 Error(ImmLoc, "immediate shift value out of range");
2136 } else if (Parser.getTok().is(AsmToken::Identifier)) {
2137 ShiftReg = tryParseRegister();
2138 SMLoc L = Parser.getTok().getLoc();
2139 if (ShiftReg == -1) {
2140 Error (L, "expected immediate or register in shift operand");
2144 Error (Parser.getTok().getLoc(),
2145 "expected immediate or register in shift operand");
2150 if (ShiftReg && ShiftTy != ARM_AM::rrx)
2151 Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
2153 S, Parser.getTok().getLoc()));
2155 Operands.push_back(ARMOperand::CreateShiftedImmediate(ShiftTy, SrcReg, Imm,
2156 S, Parser.getTok().getLoc()));
2162 /// Try to parse a register name. The token must be an Identifier when called.
2163 /// If it's a register, an AsmOperand is created. Another AsmOperand is created
2164 /// if there is a "writeback". 'true' if it's not a register.
2166 /// TODO this is likely to change to allow different register types and or to
2167 /// parse for a specific register type.
2169 tryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2170 SMLoc S = Parser.getTok().getLoc();
2171 int RegNo = tryParseRegister();
2175 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
2177 const AsmToken &ExclaimTok = Parser.getTok();
2178 if (ExclaimTok.is(AsmToken::Exclaim)) {
2179 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
2180 ExclaimTok.getLoc()));
2181 Parser.Lex(); // Eat exclaim token
2185 // Also check for an index operand. This is only legal for vector registers,
2186 // but that'll get caught OK in operand matching, so we don't need to
2187 // explicitly filter everything else out here.
2188 if (Parser.getTok().is(AsmToken::LBrac)) {
2189 SMLoc SIdx = Parser.getTok().getLoc();
2190 Parser.Lex(); // Eat left bracket token.
2192 const MCExpr *ImmVal;
2193 if (getParser().ParseExpression(ImmVal))
2194 return MatchOperand_ParseFail;
2195 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(ImmVal);
2197 TokError("immediate value expected for vector index");
2198 return MatchOperand_ParseFail;
2201 SMLoc E = Parser.getTok().getLoc();
2202 if (Parser.getTok().isNot(AsmToken::RBrac)) {
2203 Error(E, "']' expected");
2204 return MatchOperand_ParseFail;
2207 Parser.Lex(); // Eat right bracket token.
2209 Operands.push_back(ARMOperand::CreateVectorIndex(MCE->getValue(),
2217 /// MatchCoprocessorOperandName - Try to parse an coprocessor related
2218 /// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
2220 static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
2221 // Use the same layout as the tablegen'erated register name matcher. Ugly,
2223 switch (Name.size()) {
2226 if (Name[0] != CoprocOp)
2243 if (Name[0] != CoprocOp || Name[1] != '1')
2247 case '0': return 10;
2248 case '1': return 11;
2249 case '2': return 12;
2250 case '3': return 13;
2251 case '4': return 14;
2252 case '5': return 15;
2260 /// parseITCondCode - Try to parse a condition code for an IT instruction.
2261 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2262 parseITCondCode(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2263 SMLoc S = Parser.getTok().getLoc();
2264 const AsmToken &Tok = Parser.getTok();
2265 if (!Tok.is(AsmToken::Identifier))
2266 return MatchOperand_NoMatch;
2267 unsigned CC = StringSwitch<unsigned>(Tok.getString())
2268 .Case("eq", ARMCC::EQ)
2269 .Case("ne", ARMCC::NE)
2270 .Case("hs", ARMCC::HS)
2271 .Case("cs", ARMCC::HS)
2272 .Case("lo", ARMCC::LO)
2273 .Case("cc", ARMCC::LO)
2274 .Case("mi", ARMCC::MI)
2275 .Case("pl", ARMCC::PL)
2276 .Case("vs", ARMCC::VS)
2277 .Case("vc", ARMCC::VC)
2278 .Case("hi", ARMCC::HI)
2279 .Case("ls", ARMCC::LS)
2280 .Case("ge", ARMCC::GE)
2281 .Case("lt", ARMCC::LT)
2282 .Case("gt", ARMCC::GT)
2283 .Case("le", ARMCC::LE)
2284 .Case("al", ARMCC::AL)
2287 return MatchOperand_NoMatch;
2288 Parser.Lex(); // Eat the token.
2290 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), S));
2292 return MatchOperand_Success;
2295 /// parseCoprocNumOperand - Try to parse an coprocessor number operand. The
2296 /// token must be an Identifier when called, and if it is a coprocessor
2297 /// number, the token is eaten and the operand is added to the operand list.
2298 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2299 parseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2300 SMLoc S = Parser.getTok().getLoc();
2301 const AsmToken &Tok = Parser.getTok();
2302 if (Tok.isNot(AsmToken::Identifier))
2303 return MatchOperand_NoMatch;
2305 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
2307 return MatchOperand_NoMatch;
2309 Parser.Lex(); // Eat identifier token.
2310 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
2311 return MatchOperand_Success;
2314 /// parseCoprocRegOperand - Try to parse an coprocessor register operand. The
2315 /// token must be an Identifier when called, and if it is a coprocessor
2316 /// number, the token is eaten and the operand is added to the operand list.
2317 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2318 parseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2319 SMLoc S = Parser.getTok().getLoc();
2320 const AsmToken &Tok = Parser.getTok();
2321 if (Tok.isNot(AsmToken::Identifier))
2322 return MatchOperand_NoMatch;
2324 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
2326 return MatchOperand_NoMatch;
2328 Parser.Lex(); // Eat identifier token.
2329 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
2330 return MatchOperand_Success;
2333 /// parseCoprocOptionOperand - Try to parse an coprocessor option operand.
2334 /// coproc_option : '{' imm0_255 '}'
2335 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2336 parseCoprocOptionOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2337 SMLoc S = Parser.getTok().getLoc();
2339 // If this isn't a '{', this isn't a coprocessor immediate operand.
2340 if (Parser.getTok().isNot(AsmToken::LCurly))
2341 return MatchOperand_NoMatch;
2342 Parser.Lex(); // Eat the '{'
2345 SMLoc Loc = Parser.getTok().getLoc();
2346 if (getParser().ParseExpression(Expr)) {
2347 Error(Loc, "illegal expression");
2348 return MatchOperand_ParseFail;
2350 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
2351 if (!CE || CE->getValue() < 0 || CE->getValue() > 255) {
2352 Error(Loc, "coprocessor option must be an immediate in range [0, 255]");
2353 return MatchOperand_ParseFail;
2355 int Val = CE->getValue();
2357 // Check for and consume the closing '}'
2358 if (Parser.getTok().isNot(AsmToken::RCurly))
2359 return MatchOperand_ParseFail;
2360 SMLoc E = Parser.getTok().getLoc();
2361 Parser.Lex(); // Eat the '}'
2363 Operands.push_back(ARMOperand::CreateCoprocOption(Val, S, E));
2364 return MatchOperand_Success;
2367 // For register list parsing, we need to map from raw GPR register numbering
2368 // to the enumeration values. The enumeration values aren't sorted by
2369 // register number due to our using "sp", "lr" and "pc" as canonical names.
2370 static unsigned getNextRegister(unsigned Reg) {
2371 // If this is a GPR, we need to do it manually, otherwise we can rely
2372 // on the sort ordering of the enumeration since the other reg-classes
2374 if (!ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
2377 default: assert(0 && "Invalid GPR number!");
2378 case ARM::R0: return ARM::R1; case ARM::R1: return ARM::R2;
2379 case ARM::R2: return ARM::R3; case ARM::R3: return ARM::R4;
2380 case ARM::R4: return ARM::R5; case ARM::R5: return ARM::R6;
2381 case ARM::R6: return ARM::R7; case ARM::R7: return ARM::R8;
2382 case ARM::R8: return ARM::R9; case ARM::R9: return ARM::R10;
2383 case ARM::R10: return ARM::R11; case ARM::R11: return ARM::R12;
2384 case ARM::R12: return ARM::SP; case ARM::SP: return ARM::LR;
2385 case ARM::LR: return ARM::PC; case ARM::PC: return ARM::R0;
2389 /// Parse a register list.
2391 parseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2392 assert(Parser.getTok().is(AsmToken::LCurly) &&
2393 "Token is not a Left Curly Brace");
2394 SMLoc S = Parser.getTok().getLoc();
2395 Parser.Lex(); // Eat '{' token.
2396 SMLoc RegLoc = Parser.getTok().getLoc();
2398 // Check the first register in the list to see what register class
2399 // this is a list of.
2400 int Reg = tryParseRegister();
2402 return Error(RegLoc, "register expected");
2404 const MCRegisterClass *RC;
2405 if (ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
2406 RC = &ARMMCRegisterClasses[ARM::GPRRegClassID];
2407 else if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg))
2408 RC = &ARMMCRegisterClasses[ARM::DPRRegClassID];
2409 else if (ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg))
2410 RC = &ARMMCRegisterClasses[ARM::SPRRegClassID];
2412 return Error(RegLoc, "invalid register in register list");
2414 // The reglist instructions have at most 16 registers, so reserve
2415 // space for that many.
2416 SmallVector<std::pair<unsigned, SMLoc>, 16> Registers;
2417 // Store the first register.
2418 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2420 // This starts immediately after the first register token in the list,
2421 // so we can see either a comma or a minus (range separator) as a legal
2423 while (Parser.getTok().is(AsmToken::Comma) ||
2424 Parser.getTok().is(AsmToken::Minus)) {
2425 if (Parser.getTok().is(AsmToken::Minus)) {
2426 Parser.Lex(); // Eat the comma.
2427 SMLoc EndLoc = Parser.getTok().getLoc();
2428 int EndReg = tryParseRegister();
2430 return Error(EndLoc, "register expected");
2431 // If the register is the same as the start reg, there's nothing
2435 // The register must be in the same register class as the first.
2436 if (!RC->contains(EndReg))
2437 return Error(EndLoc, "invalid register in register list");
2438 // Ranges must go from low to high.
2439 if (getARMRegisterNumbering(Reg) > getARMRegisterNumbering(EndReg))
2440 return Error(EndLoc, "bad range in register list");
2442 // Add all the registers in the range to the register list.
2443 while (Reg != EndReg) {
2444 Reg = getNextRegister(Reg);
2445 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2449 Parser.Lex(); // Eat the comma.
2450 RegLoc = Parser.getTok().getLoc();
2452 Reg = tryParseRegister();
2454 return Error(RegLoc, "register expected");
2455 // The register must be in the same register class as the first.
2456 if (!RC->contains(Reg))
2457 return Error(RegLoc, "invalid register in register list");
2458 // List must be monotonically increasing.
2459 if (getARMRegisterNumbering(Reg) <= getARMRegisterNumbering(OldReg))
2460 return Error(RegLoc, "register list not in ascending order");
2461 // VFP register lists must also be contiguous.
2462 // It's OK to use the enumeration values directly here rather, as the
2463 // VFP register classes have the enum sorted properly.
2464 if (RC != &ARMMCRegisterClasses[ARM::GPRRegClassID] &&
2466 return Error(RegLoc, "non-contiguous register range");
2467 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2470 SMLoc E = Parser.getTok().getLoc();
2471 if (Parser.getTok().isNot(AsmToken::RCurly))
2472 return Error(E, "'}' expected");
2473 Parser.Lex(); // Eat '}' token.
2475 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
2479 // Return the low-subreg of a given Q register.
2480 static unsigned getDRegFromQReg(unsigned QReg) {
2482 default: llvm_unreachable("expected a Q register!");
2483 case ARM::Q0: return ARM::D0;
2484 case ARM::Q1: return ARM::D2;
2485 case ARM::Q2: return ARM::D4;
2486 case ARM::Q3: return ARM::D6;
2487 case ARM::Q4: return ARM::D8;
2488 case ARM::Q5: return ARM::D10;
2489 case ARM::Q6: return ARM::D12;
2490 case ARM::Q7: return ARM::D14;
2491 case ARM::Q8: return ARM::D16;
2492 case ARM::Q9: return ARM::D19;
2493 case ARM::Q10: return ARM::D20;
2494 case ARM::Q11: return ARM::D22;
2495 case ARM::Q12: return ARM::D24;
2496 case ARM::Q13: return ARM::D26;
2497 case ARM::Q14: return ARM::D28;
2498 case ARM::Q15: return ARM::D30;
2502 // parse a vector register list
2503 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2504 parseVectorList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2505 if(Parser.getTok().isNot(AsmToken::LCurly))
2506 return MatchOperand_NoMatch;
2508 SMLoc S = Parser.getTok().getLoc();
2509 Parser.Lex(); // Eat '{' token.
2510 SMLoc RegLoc = Parser.getTok().getLoc();
2512 int Reg = tryParseRegister();
2514 Error(RegLoc, "register expected");
2515 return MatchOperand_ParseFail;
2518 unsigned FirstReg = Reg;
2519 // The list is of D registers, but we also allow Q regs and just interpret
2520 // them as the two D sub-registers.
2521 if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
2522 FirstReg = Reg = getDRegFromQReg(Reg);
2527 while (Parser.getTok().is(AsmToken::Comma)) {
2528 Parser.Lex(); // Eat the comma.
2529 RegLoc = Parser.getTok().getLoc();
2531 Reg = tryParseRegister();
2533 Error(RegLoc, "register expected");
2534 return MatchOperand_ParseFail;
2536 // vector register lists must be contiguous.
2537 // It's OK to use the enumeration values directly here rather, as the
2538 // VFP register classes have the enum sorted properly.
2540 // The list is of D registers, but we also allow Q regs and just interpret
2541 // them as the two D sub-registers.
2542 if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
2543 Reg = getDRegFromQReg(Reg);
2544 if (Reg != OldReg + 1) {
2545 Error(RegLoc, "non-contiguous register range");
2546 return MatchOperand_ParseFail;
2552 // Normal D register. Just check that it's contiguous and keep going.
2553 if (Reg != OldReg + 1) {
2554 Error(RegLoc, "non-contiguous register range");
2555 return MatchOperand_ParseFail;
2560 SMLoc E = Parser.getTok().getLoc();
2561 if (Parser.getTok().isNot(AsmToken::RCurly)) {
2562 Error(E, "'}' expected");
2563 return MatchOperand_ParseFail;
2565 Parser.Lex(); // Eat '}' token.
2567 Operands.push_back(ARMOperand::CreateVectorList(FirstReg, Count, S, E));
2568 return MatchOperand_Success;
2571 /// parseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
2572 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2573 parseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2574 SMLoc S = Parser.getTok().getLoc();
2575 const AsmToken &Tok = Parser.getTok();
2576 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2577 StringRef OptStr = Tok.getString();
2579 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
2580 .Case("sy", ARM_MB::SY)
2581 .Case("st", ARM_MB::ST)
2582 .Case("sh", ARM_MB::ISH)
2583 .Case("ish", ARM_MB::ISH)
2584 .Case("shst", ARM_MB::ISHST)
2585 .Case("ishst", ARM_MB::ISHST)
2586 .Case("nsh", ARM_MB::NSH)
2587 .Case("un", ARM_MB::NSH)
2588 .Case("nshst", ARM_MB::NSHST)
2589 .Case("unst", ARM_MB::NSHST)
2590 .Case("osh", ARM_MB::OSH)
2591 .Case("oshst", ARM_MB::OSHST)
2595 return MatchOperand_NoMatch;
2597 Parser.Lex(); // Eat identifier token.
2598 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
2599 return MatchOperand_Success;
2602 /// parseProcIFlagsOperand - Try to parse iflags from CPS instruction.
2603 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2604 parseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2605 SMLoc S = Parser.getTok().getLoc();
2606 const AsmToken &Tok = Parser.getTok();
2607 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2608 StringRef IFlagsStr = Tok.getString();
2610 // An iflags string of "none" is interpreted to mean that none of the AIF
2611 // bits are set. Not a terribly useful instruction, but a valid encoding.
2612 unsigned IFlags = 0;
2613 if (IFlagsStr != "none") {
2614 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
2615 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
2616 .Case("a", ARM_PROC::A)
2617 .Case("i", ARM_PROC::I)
2618 .Case("f", ARM_PROC::F)
2621 // If some specific iflag is already set, it means that some letter is
2622 // present more than once, this is not acceptable.
2623 if (Flag == ~0U || (IFlags & Flag))
2624 return MatchOperand_NoMatch;
2630 Parser.Lex(); // Eat identifier token.
2631 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
2632 return MatchOperand_Success;
2635 /// parseMSRMaskOperand - Try to parse mask flags from MSR instruction.
2636 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2637 parseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2638 SMLoc S = Parser.getTok().getLoc();
2639 const AsmToken &Tok = Parser.getTok();
2640 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2641 StringRef Mask = Tok.getString();
2644 // See ARMv6-M 10.1.1
2645 unsigned FlagsVal = StringSwitch<unsigned>(Mask)
2655 .Case("primask", 16)
2656 .Case("basepri", 17)
2657 .Case("basepri_max", 18)
2658 .Case("faultmask", 19)
2659 .Case("control", 20)
2662 if (FlagsVal == ~0U)
2663 return MatchOperand_NoMatch;
2665 if (!hasV7Ops() && FlagsVal >= 17 && FlagsVal <= 19)
2666 // basepri, basepri_max and faultmask only valid for V7m.
2667 return MatchOperand_NoMatch;
2669 Parser.Lex(); // Eat identifier token.
2670 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
2671 return MatchOperand_Success;
2674 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
2675 size_t Start = 0, Next = Mask.find('_');
2676 StringRef Flags = "";
2677 std::string SpecReg = LowercaseString(Mask.slice(Start, Next));
2678 if (Next != StringRef::npos)
2679 Flags = Mask.slice(Next+1, Mask.size());
2681 // FlagsVal contains the complete mask:
2683 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
2684 unsigned FlagsVal = 0;
2686 if (SpecReg == "apsr") {
2687 FlagsVal = StringSwitch<unsigned>(Flags)
2688 .Case("nzcvq", 0x8) // same as CPSR_f
2689 .Case("g", 0x4) // same as CPSR_s
2690 .Case("nzcvqg", 0xc) // same as CPSR_fs
2693 if (FlagsVal == ~0U) {
2695 return MatchOperand_NoMatch;
2697 FlagsVal = 8; // No flag
2699 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
2700 if (Flags == "all") // cpsr_all is an alias for cpsr_fc
2702 for (int i = 0, e = Flags.size(); i != e; ++i) {
2703 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
2710 // If some specific flag is already set, it means that some letter is
2711 // present more than once, this is not acceptable.
2712 if (FlagsVal == ~0U || (FlagsVal & Flag))
2713 return MatchOperand_NoMatch;
2716 } else // No match for special register.
2717 return MatchOperand_NoMatch;
2719 // Special register without flags is NOT equivalent to "fc" flags.
2720 // NOTE: This is a divergence from gas' behavior. Uncommenting the following
2721 // two lines would enable gas compatibility at the expense of breaking
2727 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
2728 if (SpecReg == "spsr")
2731 Parser.Lex(); // Eat identifier token.
2732 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
2733 return MatchOperand_Success;
2736 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2737 parsePKHImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands, StringRef Op,
2738 int Low, int High) {
2739 const AsmToken &Tok = Parser.getTok();
2740 if (Tok.isNot(AsmToken::Identifier)) {
2741 Error(Parser.getTok().getLoc(), Op + " operand expected.");
2742 return MatchOperand_ParseFail;
2744 StringRef ShiftName = Tok.getString();
2745 std::string LowerOp = LowercaseString(Op);
2746 std::string UpperOp = UppercaseString(Op);
2747 if (ShiftName != LowerOp && ShiftName != UpperOp) {
2748 Error(Parser.getTok().getLoc(), Op + " operand expected.");
2749 return MatchOperand_ParseFail;
2751 Parser.Lex(); // Eat shift type token.
2753 // There must be a '#' and a shift amount.
2754 if (Parser.getTok().isNot(AsmToken::Hash)) {
2755 Error(Parser.getTok().getLoc(), "'#' expected");
2756 return MatchOperand_ParseFail;
2758 Parser.Lex(); // Eat hash token.
2760 const MCExpr *ShiftAmount;
2761 SMLoc Loc = Parser.getTok().getLoc();
2762 if (getParser().ParseExpression(ShiftAmount)) {
2763 Error(Loc, "illegal expression");
2764 return MatchOperand_ParseFail;
2766 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
2768 Error(Loc, "constant expression expected");
2769 return MatchOperand_ParseFail;
2771 int Val = CE->getValue();
2772 if (Val < Low || Val > High) {
2773 Error(Loc, "immediate value out of range");
2774 return MatchOperand_ParseFail;
2777 Operands.push_back(ARMOperand::CreateImm(CE, Loc, Parser.getTok().getLoc()));
2779 return MatchOperand_Success;
2782 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2783 parseSetEndImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2784 const AsmToken &Tok = Parser.getTok();
2785 SMLoc S = Tok.getLoc();
2786 if (Tok.isNot(AsmToken::Identifier)) {
2787 Error(Tok.getLoc(), "'be' or 'le' operand expected");
2788 return MatchOperand_ParseFail;
2790 int Val = StringSwitch<int>(Tok.getString())
2794 Parser.Lex(); // Eat the token.
2797 Error(Tok.getLoc(), "'be' or 'le' operand expected");
2798 return MatchOperand_ParseFail;
2800 Operands.push_back(ARMOperand::CreateImm(MCConstantExpr::Create(Val,
2802 S, Parser.getTok().getLoc()));
2803 return MatchOperand_Success;
2806 /// parseShifterImm - Parse the shifter immediate operand for SSAT/USAT
2807 /// instructions. Legal values are:
2808 /// lsl #n 'n' in [0,31]
2809 /// asr #n 'n' in [1,32]
2810 /// n == 32 encoded as n == 0.
2811 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2812 parseShifterImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2813 const AsmToken &Tok = Parser.getTok();
2814 SMLoc S = Tok.getLoc();
2815 if (Tok.isNot(AsmToken::Identifier)) {
2816 Error(S, "shift operator 'asr' or 'lsl' expected");
2817 return MatchOperand_ParseFail;
2819 StringRef ShiftName = Tok.getString();
2821 if (ShiftName == "lsl" || ShiftName == "LSL")
2823 else if (ShiftName == "asr" || ShiftName == "ASR")
2826 Error(S, "shift operator 'asr' or 'lsl' expected");
2827 return MatchOperand_ParseFail;
2829 Parser.Lex(); // Eat the operator.
2831 // A '#' and a shift amount.
2832 if (Parser.getTok().isNot(AsmToken::Hash)) {
2833 Error(Parser.getTok().getLoc(), "'#' expected");
2834 return MatchOperand_ParseFail;
2836 Parser.Lex(); // Eat hash token.
2838 const MCExpr *ShiftAmount;
2839 SMLoc E = Parser.getTok().getLoc();
2840 if (getParser().ParseExpression(ShiftAmount)) {
2841 Error(E, "malformed shift expression");
2842 return MatchOperand_ParseFail;
2844 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
2846 Error(E, "shift amount must be an immediate");
2847 return MatchOperand_ParseFail;
2850 int64_t Val = CE->getValue();
2852 // Shift amount must be in [1,32]
2853 if (Val < 1 || Val > 32) {
2854 Error(E, "'asr' shift amount must be in range [1,32]");
2855 return MatchOperand_ParseFail;
2857 // asr #32 encoded as asr #0, but is not allowed in Thumb2 mode.
2858 if (isThumb() && Val == 32) {
2859 Error(E, "'asr #32' shift amount not allowed in Thumb mode");
2860 return MatchOperand_ParseFail;
2862 if (Val == 32) Val = 0;
2864 // Shift amount must be in [1,32]
2865 if (Val < 0 || Val > 31) {
2866 Error(E, "'lsr' shift amount must be in range [0,31]");
2867 return MatchOperand_ParseFail;
2871 E = Parser.getTok().getLoc();
2872 Operands.push_back(ARMOperand::CreateShifterImm(isASR, Val, S, E));
2874 return MatchOperand_Success;
2877 /// parseRotImm - Parse the shifter immediate operand for SXTB/UXTB family
2878 /// of instructions. Legal values are:
2879 /// ror #n 'n' in {0, 8, 16, 24}
2880 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2881 parseRotImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2882 const AsmToken &Tok = Parser.getTok();
2883 SMLoc S = Tok.getLoc();
2884 if (Tok.isNot(AsmToken::Identifier))
2885 return MatchOperand_NoMatch;
2886 StringRef ShiftName = Tok.getString();
2887 if (ShiftName != "ror" && ShiftName != "ROR")
2888 return MatchOperand_NoMatch;
2889 Parser.Lex(); // Eat the operator.
2891 // A '#' and a rotate amount.
2892 if (Parser.getTok().isNot(AsmToken::Hash)) {
2893 Error(Parser.getTok().getLoc(), "'#' expected");
2894 return MatchOperand_ParseFail;
2896 Parser.Lex(); // Eat hash token.
2898 const MCExpr *ShiftAmount;
2899 SMLoc E = Parser.getTok().getLoc();
2900 if (getParser().ParseExpression(ShiftAmount)) {
2901 Error(E, "malformed rotate expression");
2902 return MatchOperand_ParseFail;
2904 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
2906 Error(E, "rotate amount must be an immediate");
2907 return MatchOperand_ParseFail;
2910 int64_t Val = CE->getValue();
2911 // Shift amount must be in {0, 8, 16, 24} (0 is undocumented extension)
2912 // normally, zero is represented in asm by omitting the rotate operand
2914 if (Val != 8 && Val != 16 && Val != 24 && Val != 0) {
2915 Error(E, "'ror' rotate amount must be 8, 16, or 24");
2916 return MatchOperand_ParseFail;
2919 E = Parser.getTok().getLoc();
2920 Operands.push_back(ARMOperand::CreateRotImm(Val, S, E));
2922 return MatchOperand_Success;
2925 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2926 parseBitfield(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2927 SMLoc S = Parser.getTok().getLoc();
2928 // The bitfield descriptor is really two operands, the LSB and the width.
2929 if (Parser.getTok().isNot(AsmToken::Hash)) {
2930 Error(Parser.getTok().getLoc(), "'#' expected");
2931 return MatchOperand_ParseFail;
2933 Parser.Lex(); // Eat hash token.
2935 const MCExpr *LSBExpr;
2936 SMLoc E = Parser.getTok().getLoc();
2937 if (getParser().ParseExpression(LSBExpr)) {
2938 Error(E, "malformed immediate expression");
2939 return MatchOperand_ParseFail;
2941 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(LSBExpr);
2943 Error(E, "'lsb' operand must be an immediate");
2944 return MatchOperand_ParseFail;
2947 int64_t LSB = CE->getValue();
2948 // The LSB must be in the range [0,31]
2949 if (LSB < 0 || LSB > 31) {
2950 Error(E, "'lsb' operand must be in the range [0,31]");
2951 return MatchOperand_ParseFail;
2953 E = Parser.getTok().getLoc();
2955 // Expect another immediate operand.
2956 if (Parser.getTok().isNot(AsmToken::Comma)) {
2957 Error(Parser.getTok().getLoc(), "too few operands");
2958 return MatchOperand_ParseFail;
2960 Parser.Lex(); // Eat hash token.
2961 if (Parser.getTok().isNot(AsmToken::Hash)) {
2962 Error(Parser.getTok().getLoc(), "'#' expected");
2963 return MatchOperand_ParseFail;
2965 Parser.Lex(); // Eat hash token.
2967 const MCExpr *WidthExpr;
2968 if (getParser().ParseExpression(WidthExpr)) {
2969 Error(E, "malformed immediate expression");
2970 return MatchOperand_ParseFail;
2972 CE = dyn_cast<MCConstantExpr>(WidthExpr);
2974 Error(E, "'width' operand must be an immediate");
2975 return MatchOperand_ParseFail;
2978 int64_t Width = CE->getValue();
2979 // The LSB must be in the range [1,32-lsb]
2980 if (Width < 1 || Width > 32 - LSB) {
2981 Error(E, "'width' operand must be in the range [1,32-lsb]");
2982 return MatchOperand_ParseFail;
2984 E = Parser.getTok().getLoc();
2986 Operands.push_back(ARMOperand::CreateBitfield(LSB, Width, S, E));
2988 return MatchOperand_Success;
2991 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2992 parsePostIdxReg(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2993 // Check for a post-index addressing register operand. Specifically:
2994 // postidx_reg := '+' register {, shift}
2995 // | '-' register {, shift}
2996 // | register {, shift}
2998 // This method must return MatchOperand_NoMatch without consuming any tokens
2999 // in the case where there is no match, as other alternatives take other
3001 AsmToken Tok = Parser.getTok();
3002 SMLoc S = Tok.getLoc();
3003 bool haveEaten = false;
3006 if (Tok.is(AsmToken::Plus)) {
3007 Parser.Lex(); // Eat the '+' token.
3009 } else if (Tok.is(AsmToken::Minus)) {
3010 Parser.Lex(); // Eat the '-' token.
3014 if (Parser.getTok().is(AsmToken::Identifier))
3015 Reg = tryParseRegister();
3018 return MatchOperand_NoMatch;
3019 Error(Parser.getTok().getLoc(), "register expected");
3020 return MatchOperand_ParseFail;
3022 SMLoc E = Parser.getTok().getLoc();
3024 ARM_AM::ShiftOpc ShiftTy = ARM_AM::no_shift;
3025 unsigned ShiftImm = 0;
3026 if (Parser.getTok().is(AsmToken::Comma)) {
3027 Parser.Lex(); // Eat the ','.
3028 if (parseMemRegOffsetShift(ShiftTy, ShiftImm))
3029 return MatchOperand_ParseFail;
3032 Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ShiftTy,
3035 return MatchOperand_Success;
3038 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3039 parseAM3Offset(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3040 // Check for a post-index addressing register operand. Specifically:
3041 // am3offset := '+' register
3048 // This method must return MatchOperand_NoMatch without consuming any tokens
3049 // in the case where there is no match, as other alternatives take other
3051 AsmToken Tok = Parser.getTok();
3052 SMLoc S = Tok.getLoc();
3054 // Do immediates first, as we always parse those if we have a '#'.
3055 if (Parser.getTok().is(AsmToken::Hash)) {
3056 Parser.Lex(); // Eat the '#'.
3057 // Explicitly look for a '-', as we need to encode negative zero
3059 bool isNegative = Parser.getTok().is(AsmToken::Minus);
3060 const MCExpr *Offset;
3061 if (getParser().ParseExpression(Offset))
3062 return MatchOperand_ParseFail;
3063 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
3065 Error(S, "constant expression expected");
3066 return MatchOperand_ParseFail;
3068 SMLoc E = Tok.getLoc();
3069 // Negative zero is encoded as the flag value INT32_MIN.
3070 int32_t Val = CE->getValue();
3071 if (isNegative && Val == 0)
3075 ARMOperand::CreateImm(MCConstantExpr::Create(Val, getContext()), S, E));
3077 return MatchOperand_Success;
3081 bool haveEaten = false;
3084 if (Tok.is(AsmToken::Plus)) {
3085 Parser.Lex(); // Eat the '+' token.
3087 } else if (Tok.is(AsmToken::Minus)) {
3088 Parser.Lex(); // Eat the '-' token.
3092 if (Parser.getTok().is(AsmToken::Identifier))
3093 Reg = tryParseRegister();
3096 return MatchOperand_NoMatch;
3097 Error(Parser.getTok().getLoc(), "register expected");
3098 return MatchOperand_ParseFail;
3100 SMLoc E = Parser.getTok().getLoc();
3102 Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ARM_AM::no_shift,
3105 return MatchOperand_Success;
3108 /// cvtT2LdrdPre - Convert parsed operands to MCInst.
3109 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3110 /// when they refer multiple MIOperands inside a single one.
3112 cvtT2LdrdPre(MCInst &Inst, unsigned Opcode,
3113 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3115 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3116 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3117 // Create a writeback register dummy placeholder.
3118 Inst.addOperand(MCOperand::CreateReg(0));
3120 ((ARMOperand*)Operands[4])->addMemImm8s4OffsetOperands(Inst, 2);
3122 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3126 /// cvtT2StrdPre - Convert parsed operands to MCInst.
3127 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3128 /// when they refer multiple MIOperands inside a single one.
3130 cvtT2StrdPre(MCInst &Inst, unsigned Opcode,
3131 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3132 // Create a writeback register dummy placeholder.
3133 Inst.addOperand(MCOperand::CreateReg(0));
3135 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3136 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3138 ((ARMOperand*)Operands[4])->addMemImm8s4OffsetOperands(Inst, 2);
3140 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3144 /// cvtLdWriteBackRegT2AddrModeImm8 - Convert parsed operands to MCInst.
3145 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3146 /// when they refer multiple MIOperands inside a single one.
3148 cvtLdWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
3149 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3150 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3152 // Create a writeback register dummy placeholder.
3153 Inst.addOperand(MCOperand::CreateImm(0));
3155 ((ARMOperand*)Operands[3])->addMemImm8OffsetOperands(Inst, 2);
3156 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3160 /// cvtStWriteBackRegT2AddrModeImm8 - Convert parsed operands to MCInst.
3161 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3162 /// when they refer multiple MIOperands inside a single one.
3164 cvtStWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
3165 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3166 // Create a writeback register dummy placeholder.
3167 Inst.addOperand(MCOperand::CreateImm(0));
3168 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3169 ((ARMOperand*)Operands[3])->addMemImm8OffsetOperands(Inst, 2);
3170 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3174 /// cvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
3175 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3176 /// when they refer multiple MIOperands inside a single one.
3178 cvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
3179 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3180 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3182 // Create a writeback register dummy placeholder.
3183 Inst.addOperand(MCOperand::CreateImm(0));
3185 ((ARMOperand*)Operands[3])->addAddrMode2Operands(Inst, 3);
3186 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3190 /// cvtLdWriteBackRegAddrModeImm12 - Convert parsed operands to MCInst.
3191 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3192 /// when they refer multiple MIOperands inside a single one.
3194 cvtLdWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
3195 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3196 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3198 // Create a writeback register dummy placeholder.
3199 Inst.addOperand(MCOperand::CreateImm(0));
3201 ((ARMOperand*)Operands[3])->addMemImm12OffsetOperands(Inst, 2);
3202 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3207 /// cvtStWriteBackRegAddrModeImm12 - Convert parsed operands to MCInst.
3208 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3209 /// when they refer multiple MIOperands inside a single one.
3211 cvtStWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
3212 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3213 // Create a writeback register dummy placeholder.
3214 Inst.addOperand(MCOperand::CreateImm(0));
3215 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3216 ((ARMOperand*)Operands[3])->addMemImm12OffsetOperands(Inst, 2);
3217 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3221 /// cvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
3222 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3223 /// when they refer multiple MIOperands inside a single one.
3225 cvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
3226 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3227 // Create a writeback register dummy placeholder.
3228 Inst.addOperand(MCOperand::CreateImm(0));
3229 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3230 ((ARMOperand*)Operands[3])->addAddrMode2Operands(Inst, 3);
3231 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3235 /// cvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
3236 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3237 /// when they refer multiple MIOperands inside a single one.
3239 cvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
3240 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3241 // Create a writeback register dummy placeholder.
3242 Inst.addOperand(MCOperand::CreateImm(0));
3243 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3244 ((ARMOperand*)Operands[3])->addAddrMode3Operands(Inst, 3);
3245 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3249 /// cvtLdExtTWriteBackImm - Convert parsed operands to MCInst.
3250 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3251 /// when they refer multiple MIOperands inside a single one.
3253 cvtLdExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
3254 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3256 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3257 // Create a writeback register dummy placeholder.
3258 Inst.addOperand(MCOperand::CreateImm(0));
3260 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3262 ((ARMOperand*)Operands[4])->addPostIdxImm8Operands(Inst, 1);
3264 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3268 /// cvtLdExtTWriteBackReg - Convert parsed operands to MCInst.
3269 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3270 /// when they refer multiple MIOperands inside a single one.
3272 cvtLdExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
3273 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3275 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3276 // Create a writeback register dummy placeholder.
3277 Inst.addOperand(MCOperand::CreateImm(0));
3279 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3281 ((ARMOperand*)Operands[4])->addPostIdxRegOperands(Inst, 2);
3283 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3287 /// cvtStExtTWriteBackImm - Convert parsed operands to MCInst.
3288 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3289 /// when they refer multiple MIOperands inside a single one.
3291 cvtStExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
3292 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3293 // Create a writeback register dummy placeholder.
3294 Inst.addOperand(MCOperand::CreateImm(0));
3296 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3298 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3300 ((ARMOperand*)Operands[4])->addPostIdxImm8Operands(Inst, 1);
3302 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3306 /// cvtStExtTWriteBackReg - Convert parsed operands to MCInst.
3307 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3308 /// when they refer multiple MIOperands inside a single one.
3310 cvtStExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
3311 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3312 // Create a writeback register dummy placeholder.
3313 Inst.addOperand(MCOperand::CreateImm(0));
3315 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3317 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3319 ((ARMOperand*)Operands[4])->addPostIdxRegOperands(Inst, 2);
3321 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3325 /// cvtLdrdPre - Convert parsed operands to MCInst.
3326 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3327 /// when they refer multiple MIOperands inside a single one.
3329 cvtLdrdPre(MCInst &Inst, unsigned Opcode,
3330 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3332 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3333 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3334 // Create a writeback register dummy placeholder.
3335 Inst.addOperand(MCOperand::CreateImm(0));
3337 ((ARMOperand*)Operands[4])->addAddrMode3Operands(Inst, 3);
3339 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3343 /// cvtStrdPre - Convert parsed operands to MCInst.
3344 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3345 /// when they refer multiple MIOperands inside a single one.
3347 cvtStrdPre(MCInst &Inst, unsigned Opcode,
3348 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3349 // Create a writeback register dummy placeholder.
3350 Inst.addOperand(MCOperand::CreateImm(0));
3352 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3353 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3355 ((ARMOperand*)Operands[4])->addAddrMode3Operands(Inst, 3);
3357 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3361 /// cvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
3362 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3363 /// when they refer multiple MIOperands inside a single one.
3365 cvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
3366 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3367 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3368 // Create a writeback register dummy placeholder.
3369 Inst.addOperand(MCOperand::CreateImm(0));
3370 ((ARMOperand*)Operands[3])->addAddrMode3Operands(Inst, 3);
3371 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3375 /// cvtThumbMultiple- Convert parsed operands to MCInst.
3376 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3377 /// when they refer multiple MIOperands inside a single one.
3379 cvtThumbMultiply(MCInst &Inst, unsigned Opcode,
3380 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3381 // The second source operand must be the same register as the destination
3383 if (Operands.size() == 6 &&
3384 (((ARMOperand*)Operands[3])->getReg() !=
3385 ((ARMOperand*)Operands[5])->getReg()) &&
3386 (((ARMOperand*)Operands[3])->getReg() !=
3387 ((ARMOperand*)Operands[4])->getReg())) {
3388 Error(Operands[3]->getStartLoc(),
3389 "destination register must match source register");
3392 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3393 ((ARMOperand*)Operands[1])->addCCOutOperands(Inst, 1);
3394 ((ARMOperand*)Operands[4])->addRegOperands(Inst, 1);
3395 // If we have a three-operand form, use that, else the second source operand
3396 // is just the destination operand again.
3397 if (Operands.size() == 6)
3398 ((ARMOperand*)Operands[5])->addRegOperands(Inst, 1);
3400 Inst.addOperand(Inst.getOperand(0));
3401 ((ARMOperand*)Operands[2])->addCondCodeOperands(Inst, 2);
3407 cvtVLDwbFixed(MCInst &Inst, unsigned Opcode,
3408 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3410 ((ARMOperand*)Operands[3])->addVecListTwoDOperands(Inst, 1);
3411 // Create a writeback register dummy placeholder.
3412 Inst.addOperand(MCOperand::CreateImm(0));
3414 ((ARMOperand*)Operands[4])->addAlignedMemoryOperands(Inst, 2);
3416 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3421 cvtVLDwbRegister(MCInst &Inst, unsigned Opcode,
3422 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3424 ((ARMOperand*)Operands[3])->addVecListTwoDOperands(Inst, 1);
3425 // Create a writeback register dummy placeholder.
3426 Inst.addOperand(MCOperand::CreateImm(0));
3428 ((ARMOperand*)Operands[4])->addAlignedMemoryOperands(Inst, 2);
3430 ((ARMOperand*)Operands[5])->addRegOperands(Inst, 1);
3432 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3437 cvtVSTwbFixed(MCInst &Inst, unsigned Opcode,
3438 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3439 // Create a writeback register dummy placeholder.
3440 Inst.addOperand(MCOperand::CreateImm(0));
3442 ((ARMOperand*)Operands[4])->addAlignedMemoryOperands(Inst, 2);
3444 ((ARMOperand*)Operands[3])->addVecListTwoDOperands(Inst, 1);
3446 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3451 cvtVSTwbRegister(MCInst &Inst, unsigned Opcode,
3452 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3453 // Create a writeback register dummy placeholder.
3454 Inst.addOperand(MCOperand::CreateImm(0));
3456 ((ARMOperand*)Operands[4])->addAlignedMemoryOperands(Inst, 2);
3458 ((ARMOperand*)Operands[5])->addRegOperands(Inst, 1);
3460 ((ARMOperand*)Operands[3])->addVecListTwoDOperands(Inst, 1);
3462 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3466 /// Parse an ARM memory expression, return false if successful else return true
3467 /// or an error. The first token must be a '[' when called.
3469 parseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3471 assert(Parser.getTok().is(AsmToken::LBrac) &&
3472 "Token is not a Left Bracket");
3473 S = Parser.getTok().getLoc();
3474 Parser.Lex(); // Eat left bracket token.
3476 const AsmToken &BaseRegTok = Parser.getTok();
3477 int BaseRegNum = tryParseRegister();
3478 if (BaseRegNum == -1)
3479 return Error(BaseRegTok.getLoc(), "register expected");
3481 // The next token must either be a comma or a closing bracket.
3482 const AsmToken &Tok = Parser.getTok();
3483 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
3484 return Error(Tok.getLoc(), "malformed memory operand");
3486 if (Tok.is(AsmToken::RBrac)) {
3488 Parser.Lex(); // Eat right bracket token.
3490 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, 0, ARM_AM::no_shift,
3491 0, 0, false, S, E));
3493 // If there's a pre-indexing writeback marker, '!', just add it as a token
3494 // operand. It's rather odd, but syntactically valid.
3495 if (Parser.getTok().is(AsmToken::Exclaim)) {
3496 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3497 Parser.Lex(); // Eat the '!'.
3503 assert(Tok.is(AsmToken::Comma) && "Lost comma in memory operand?!");
3504 Parser.Lex(); // Eat the comma.
3506 // If we have a ':', it's an alignment specifier.
3507 if (Parser.getTok().is(AsmToken::Colon)) {
3508 Parser.Lex(); // Eat the ':'.
3509 E = Parser.getTok().getLoc();
3512 if (getParser().ParseExpression(Expr))
3515 // The expression has to be a constant. Memory references with relocations
3516 // don't come through here, as they use the <label> forms of the relevant
3518 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
3520 return Error (E, "constant expression expected");
3523 switch (CE->getValue()) {
3525 return Error(E, "alignment specifier must be 64, 128, or 256 bits");
3526 case 64: Align = 8; break;
3527 case 128: Align = 16; break;
3528 case 256: Align = 32; break;
3531 // Now we should have the closing ']'
3532 E = Parser.getTok().getLoc();
3533 if (Parser.getTok().isNot(AsmToken::RBrac))
3534 return Error(E, "']' expected");
3535 Parser.Lex(); // Eat right bracket token.
3537 // Don't worry about range checking the value here. That's handled by
3538 // the is*() predicates.
3539 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, 0,
3540 ARM_AM::no_shift, 0, Align,
3543 // If there's a pre-indexing writeback marker, '!', just add it as a token
3545 if (Parser.getTok().is(AsmToken::Exclaim)) {
3546 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3547 Parser.Lex(); // Eat the '!'.
3553 // If we have a '#', it's an immediate offset, else assume it's a register
3555 if (Parser.getTok().is(AsmToken::Hash)) {
3556 Parser.Lex(); // Eat the '#'.
3557 E = Parser.getTok().getLoc();
3559 bool isNegative = getParser().getTok().is(AsmToken::Minus);
3560 const MCExpr *Offset;
3561 if (getParser().ParseExpression(Offset))
3564 // The expression has to be a constant. Memory references with relocations
3565 // don't come through here, as they use the <label> forms of the relevant
3567 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
3569 return Error (E, "constant expression expected");
3571 // If the constant was #-0, represent it as INT32_MIN.
3572 int32_t Val = CE->getValue();
3573 if (isNegative && Val == 0)
3574 CE = MCConstantExpr::Create(INT32_MIN, getContext());
3576 // Now we should have the closing ']'
3577 E = Parser.getTok().getLoc();
3578 if (Parser.getTok().isNot(AsmToken::RBrac))
3579 return Error(E, "']' expected");
3580 Parser.Lex(); // Eat right bracket token.
3582 // Don't worry about range checking the value here. That's handled by
3583 // the is*() predicates.
3584 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, CE, 0,
3585 ARM_AM::no_shift, 0, 0,
3588 // If there's a pre-indexing writeback marker, '!', just add it as a token
3590 if (Parser.getTok().is(AsmToken::Exclaim)) {
3591 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3592 Parser.Lex(); // Eat the '!'.
3598 // The register offset is optionally preceded by a '+' or '-'
3599 bool isNegative = false;
3600 if (Parser.getTok().is(AsmToken::Minus)) {
3602 Parser.Lex(); // Eat the '-'.
3603 } else if (Parser.getTok().is(AsmToken::Plus)) {
3605 Parser.Lex(); // Eat the '+'.
3608 E = Parser.getTok().getLoc();
3609 int OffsetRegNum = tryParseRegister();
3610 if (OffsetRegNum == -1)
3611 return Error(E, "register expected");
3613 // If there's a shift operator, handle it.
3614 ARM_AM::ShiftOpc ShiftType = ARM_AM::no_shift;
3615 unsigned ShiftImm = 0;
3616 if (Parser.getTok().is(AsmToken::Comma)) {
3617 Parser.Lex(); // Eat the ','.
3618 if (parseMemRegOffsetShift(ShiftType, ShiftImm))
3622 // Now we should have the closing ']'
3623 E = Parser.getTok().getLoc();
3624 if (Parser.getTok().isNot(AsmToken::RBrac))
3625 return Error(E, "']' expected");
3626 Parser.Lex(); // Eat right bracket token.
3628 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, OffsetRegNum,
3629 ShiftType, ShiftImm, 0, isNegative,
3632 // If there's a pre-indexing writeback marker, '!', just add it as a token
3634 if (Parser.getTok().is(AsmToken::Exclaim)) {
3635 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3636 Parser.Lex(); // Eat the '!'.
3642 /// parseMemRegOffsetShift - one of these two:
3643 /// ( lsl | lsr | asr | ror ) , # shift_amount
3645 /// return true if it parses a shift otherwise it returns false.
3646 bool ARMAsmParser::parseMemRegOffsetShift(ARM_AM::ShiftOpc &St,
3648 SMLoc Loc = Parser.getTok().getLoc();
3649 const AsmToken &Tok = Parser.getTok();
3650 if (Tok.isNot(AsmToken::Identifier))
3652 StringRef ShiftName = Tok.getString();
3653 if (ShiftName == "lsl" || ShiftName == "LSL")
3655 else if (ShiftName == "lsr" || ShiftName == "LSR")
3657 else if (ShiftName == "asr" || ShiftName == "ASR")
3659 else if (ShiftName == "ror" || ShiftName == "ROR")
3661 else if (ShiftName == "rrx" || ShiftName == "RRX")
3664 return Error(Loc, "illegal shift operator");
3665 Parser.Lex(); // Eat shift type token.
3667 // rrx stands alone.
3669 if (St != ARM_AM::rrx) {
3670 Loc = Parser.getTok().getLoc();
3671 // A '#' and a shift amount.
3672 const AsmToken &HashTok = Parser.getTok();
3673 if (HashTok.isNot(AsmToken::Hash))
3674 return Error(HashTok.getLoc(), "'#' expected");
3675 Parser.Lex(); // Eat hash token.
3678 if (getParser().ParseExpression(Expr))
3680 // Range check the immediate.
3681 // lsl, ror: 0 <= imm <= 31
3682 // lsr, asr: 0 <= imm <= 32
3683 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
3685 return Error(Loc, "shift amount must be an immediate");
3686 int64_t Imm = CE->getValue();
3688 ((St == ARM_AM::lsl || St == ARM_AM::ror) && Imm > 31) ||
3689 ((St == ARM_AM::lsr || St == ARM_AM::asr) && Imm > 32))
3690 return Error(Loc, "immediate shift value out of range");
3697 /// parseFPImm - A floating point immediate expression operand.
3698 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3699 parseFPImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3700 SMLoc S = Parser.getTok().getLoc();
3702 if (Parser.getTok().isNot(AsmToken::Hash))
3703 return MatchOperand_NoMatch;
3705 // Disambiguate the VMOV forms that can accept an FP immediate.
3706 // vmov.f32 <sreg>, #imm
3707 // vmov.f64 <dreg>, #imm
3708 // vmov.f32 <dreg>, #imm @ vector f32x2
3709 // vmov.f32 <qreg>, #imm @ vector f32x4
3711 // There are also the NEON VMOV instructions which expect an
3712 // integer constant. Make sure we don't try to parse an FPImm
3714 // vmov.i{8|16|32|64} <dreg|qreg>, #imm
3715 ARMOperand *TyOp = static_cast<ARMOperand*>(Operands[2]);
3716 if (!TyOp->isToken() || (TyOp->getToken() != ".f32" &&
3717 TyOp->getToken() != ".f64"))
3718 return MatchOperand_NoMatch;
3720 Parser.Lex(); // Eat the '#'.
3722 // Handle negation, as that still comes through as a separate token.
3723 bool isNegative = false;
3724 if (Parser.getTok().is(AsmToken::Minus)) {
3728 const AsmToken &Tok = Parser.getTok();
3729 if (Tok.is(AsmToken::Real)) {
3730 APFloat RealVal(APFloat::IEEEdouble, Tok.getString());
3731 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
3732 // If we had a '-' in front, toggle the sign bit.
3733 IntVal ^= (uint64_t)isNegative << 63;
3734 int Val = ARM_AM::getFP64Imm(APInt(64, IntVal));
3735 Parser.Lex(); // Eat the token.
3737 TokError("floating point value out of range");
3738 return MatchOperand_ParseFail;
3740 Operands.push_back(ARMOperand::CreateFPImm(Val, S, getContext()));
3741 return MatchOperand_Success;
3743 if (Tok.is(AsmToken::Integer)) {
3744 int64_t Val = Tok.getIntVal();
3745 Parser.Lex(); // Eat the token.
3746 if (Val > 255 || Val < 0) {
3747 TokError("encoded floating point value out of range");
3748 return MatchOperand_ParseFail;
3750 Operands.push_back(ARMOperand::CreateFPImm(Val, S, getContext()));
3751 return MatchOperand_Success;
3754 TokError("invalid floating point immediate");
3755 return MatchOperand_ParseFail;
3757 /// Parse a arm instruction operand. For now this parses the operand regardless
3758 /// of the mnemonic.
3759 bool ARMAsmParser::parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
3760 StringRef Mnemonic) {
3763 // Check if the current operand has a custom associated parser, if so, try to
3764 // custom parse the operand, or fallback to the general approach.
3765 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
3766 if (ResTy == MatchOperand_Success)
3768 // If there wasn't a custom match, try the generic matcher below. Otherwise,
3769 // there was a match, but an error occurred, in which case, just return that
3770 // the operand parsing failed.
3771 if (ResTy == MatchOperand_ParseFail)
3774 switch (getLexer().getKind()) {
3776 Error(Parser.getTok().getLoc(), "unexpected token in operand");
3778 case AsmToken::Identifier: {
3779 // If this is VMRS, check for the apsr_nzcv operand.
3780 if (!tryParseRegisterWithWriteBack(Operands))
3782 int Res = tryParseShiftRegister(Operands);
3783 if (Res == 0) // success
3785 else if (Res == -1) // irrecoverable error
3787 if (Mnemonic == "vmrs" && Parser.getTok().getString() == "apsr_nzcv") {
3788 S = Parser.getTok().getLoc();
3790 Operands.push_back(ARMOperand::CreateToken("apsr_nzcv", S));
3794 // Fall though for the Identifier case that is not a register or a
3797 case AsmToken::LParen: // parenthesized expressions like (_strcmp-4)
3798 case AsmToken::Integer: // things like 1f and 2b as a branch targets
3799 case AsmToken::Dot: { // . as a branch target
3800 // This was not a register so parse other operands that start with an
3801 // identifier (like labels) as expressions and create them as immediates.
3802 const MCExpr *IdVal;
3803 S = Parser.getTok().getLoc();
3804 if (getParser().ParseExpression(IdVal))
3806 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3807 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
3810 case AsmToken::LBrac:
3811 return parseMemory(Operands);
3812 case AsmToken::LCurly:
3813 return parseRegisterList(Operands);
3814 case AsmToken::Hash: {
3815 // #42 -> immediate.
3816 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
3817 S = Parser.getTok().getLoc();
3819 bool isNegative = Parser.getTok().is(AsmToken::Minus);
3820 const MCExpr *ImmVal;
3821 if (getParser().ParseExpression(ImmVal))
3823 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ImmVal);
3825 Error(S, "constant expression expected");
3826 return MatchOperand_ParseFail;
3828 int32_t Val = CE->getValue();
3829 if (isNegative && Val == 0)
3830 ImmVal = MCConstantExpr::Create(INT32_MIN, getContext());
3831 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3832 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
3835 case AsmToken::Colon: {
3836 // ":lower16:" and ":upper16:" expression prefixes
3837 // FIXME: Check it's an expression prefix,
3838 // e.g. (FOO - :lower16:BAR) isn't legal.
3839 ARMMCExpr::VariantKind RefKind;
3840 if (parsePrefix(RefKind))
3843 const MCExpr *SubExprVal;
3844 if (getParser().ParseExpression(SubExprVal))
3847 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
3849 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3850 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
3856 // parsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
3857 // :lower16: and :upper16:.
3858 bool ARMAsmParser::parsePrefix(ARMMCExpr::VariantKind &RefKind) {
3859 RefKind = ARMMCExpr::VK_ARM_None;
3861 // :lower16: and :upper16: modifiers
3862 assert(getLexer().is(AsmToken::Colon) && "expected a :");
3863 Parser.Lex(); // Eat ':'
3865 if (getLexer().isNot(AsmToken::Identifier)) {
3866 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
3870 StringRef IDVal = Parser.getTok().getIdentifier();
3871 if (IDVal == "lower16") {
3872 RefKind = ARMMCExpr::VK_ARM_LO16;
3873 } else if (IDVal == "upper16") {
3874 RefKind = ARMMCExpr::VK_ARM_HI16;
3876 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
3881 if (getLexer().isNot(AsmToken::Colon)) {
3882 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
3885 Parser.Lex(); // Eat the last ':'
3889 /// \brief Given a mnemonic, split out possible predication code and carry
3890 /// setting letters to form a canonical mnemonic and flags.
3892 // FIXME: Would be nice to autogen this.
3893 // FIXME: This is a bit of a maze of special cases.
3894 StringRef ARMAsmParser::splitMnemonic(StringRef Mnemonic,
3895 unsigned &PredicationCode,
3897 unsigned &ProcessorIMod,
3898 StringRef &ITMask) {
3899 PredicationCode = ARMCC::AL;
3900 CarrySetting = false;
3903 // Ignore some mnemonics we know aren't predicated forms.
3905 // FIXME: Would be nice to autogen this.
3906 if ((Mnemonic == "movs" && isThumb()) ||
3907 Mnemonic == "teq" || Mnemonic == "vceq" || Mnemonic == "svc" ||
3908 Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
3909 Mnemonic == "vmls" || Mnemonic == "vnmls" || Mnemonic == "vacge" ||
3910 Mnemonic == "vcge" || Mnemonic == "vclt" || Mnemonic == "vacgt" ||
3911 Mnemonic == "vcgt" || Mnemonic == "vcle" || Mnemonic == "smlal" ||
3912 Mnemonic == "umaal" || Mnemonic == "umlal" || Mnemonic == "vabal" ||
3913 Mnemonic == "vmlal" || Mnemonic == "vpadal" || Mnemonic == "vqdmlal")
3916 // First, split out any predication code. Ignore mnemonics we know aren't
3917 // predicated but do have a carry-set and so weren't caught above.
3918 if (Mnemonic != "adcs" && Mnemonic != "bics" && Mnemonic != "movs" &&
3919 Mnemonic != "muls" && Mnemonic != "smlals" && Mnemonic != "smulls" &&
3920 Mnemonic != "umlals" && Mnemonic != "umulls" && Mnemonic != "lsls" &&
3921 Mnemonic != "sbcs" && Mnemonic != "rscs") {
3922 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
3923 .Case("eq", ARMCC::EQ)
3924 .Case("ne", ARMCC::NE)
3925 .Case("hs", ARMCC::HS)
3926 .Case("cs", ARMCC::HS)
3927 .Case("lo", ARMCC::LO)
3928 .Case("cc", ARMCC::LO)
3929 .Case("mi", ARMCC::MI)
3930 .Case("pl", ARMCC::PL)
3931 .Case("vs", ARMCC::VS)
3932 .Case("vc", ARMCC::VC)
3933 .Case("hi", ARMCC::HI)
3934 .Case("ls", ARMCC::LS)
3935 .Case("ge", ARMCC::GE)
3936 .Case("lt", ARMCC::LT)
3937 .Case("gt", ARMCC::GT)
3938 .Case("le", ARMCC::LE)
3939 .Case("al", ARMCC::AL)
3942 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
3943 PredicationCode = CC;
3947 // Next, determine if we have a carry setting bit. We explicitly ignore all
3948 // the instructions we know end in 's'.
3949 if (Mnemonic.endswith("s") &&
3950 !(Mnemonic == "cps" || Mnemonic == "mls" ||
3951 Mnemonic == "mrs" || Mnemonic == "smmls" || Mnemonic == "vabs" ||
3952 Mnemonic == "vcls" || Mnemonic == "vmls" || Mnemonic == "vmrs" ||
3953 Mnemonic == "vnmls" || Mnemonic == "vqabs" || Mnemonic == "vrecps" ||
3954 Mnemonic == "vrsqrts" || Mnemonic == "srs" ||
3955 (Mnemonic == "movs" && isThumb()))) {
3956 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
3957 CarrySetting = true;
3960 // The "cps" instruction can have a interrupt mode operand which is glued into
3961 // the mnemonic. Check if this is the case, split it and parse the imod op
3962 if (Mnemonic.startswith("cps")) {
3963 // Split out any imod code.
3965 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
3966 .Case("ie", ARM_PROC::IE)
3967 .Case("id", ARM_PROC::ID)
3970 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
3971 ProcessorIMod = IMod;
3975 // The "it" instruction has the condition mask on the end of the mnemonic.
3976 if (Mnemonic.startswith("it")) {
3977 ITMask = Mnemonic.slice(2, Mnemonic.size());
3978 Mnemonic = Mnemonic.slice(0, 2);
3984 /// \brief Given a canonical mnemonic, determine if the instruction ever allows
3985 /// inclusion of carry set or predication code operands.
3987 // FIXME: It would be nice to autogen this.
3989 getMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
3990 bool &CanAcceptPredicationCode) {
3991 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
3992 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
3993 Mnemonic == "add" || Mnemonic == "adc" ||
3994 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
3995 Mnemonic == "orr" || Mnemonic == "mvn" ||
3996 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
3997 Mnemonic == "sbc" || Mnemonic == "eor" || Mnemonic == "neg" ||
3998 (!isThumb() && (Mnemonic == "smull" || Mnemonic == "mov" ||
3999 Mnemonic == "mla" || Mnemonic == "smlal" ||
4000 Mnemonic == "umlal" || Mnemonic == "umull"))) {
4001 CanAcceptCarrySet = true;
4003 CanAcceptCarrySet = false;
4005 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
4006 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
4007 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
4008 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
4009 Mnemonic == "dsb" || Mnemonic == "isb" || Mnemonic == "setend" ||
4010 (Mnemonic == "clrex" && !isThumb()) ||
4011 (Mnemonic == "nop" && isThumbOne()) ||
4012 ((Mnemonic == "pld" || Mnemonic == "pli" || Mnemonic == "pldw" ||
4013 Mnemonic == "ldc2" || Mnemonic == "ldc2l" ||
4014 Mnemonic == "stc2" || Mnemonic == "stc2l") && !isThumb()) ||
4015 ((Mnemonic.startswith("rfe") || Mnemonic.startswith("srs")) &&
4017 Mnemonic.startswith("cps") || (Mnemonic == "movs" && isThumbOne())) {
4018 CanAcceptPredicationCode = false;
4020 CanAcceptPredicationCode = true;
4023 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
4024 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
4025 CanAcceptPredicationCode = false;
4029 bool ARMAsmParser::shouldOmitCCOutOperand(StringRef Mnemonic,
4030 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
4031 // FIXME: This is all horribly hacky. We really need a better way to deal
4032 // with optional operands like this in the matcher table.
4034 // The 'mov' mnemonic is special. One variant has a cc_out operand, while
4035 // another does not. Specifically, the MOVW instruction does not. So we
4036 // special case it here and remove the defaulted (non-setting) cc_out
4037 // operand if that's the instruction we're trying to match.
4039 // We do this as post-processing of the explicit operands rather than just
4040 // conditionally adding the cc_out in the first place because we need
4041 // to check the type of the parsed immediate operand.
4042 if (Mnemonic == "mov" && Operands.size() > 4 && !isThumb() &&
4043 !static_cast<ARMOperand*>(Operands[4])->isARMSOImm() &&
4044 static_cast<ARMOperand*>(Operands[4])->isImm0_65535Expr() &&
4045 static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
4048 // Register-register 'add' for thumb does not have a cc_out operand
4049 // when there are only two register operands.
4050 if (isThumb() && Mnemonic == "add" && Operands.size() == 5 &&
4051 static_cast<ARMOperand*>(Operands[3])->isReg() &&
4052 static_cast<ARMOperand*>(Operands[4])->isReg() &&
4053 static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
4055 // Register-register 'add' for thumb does not have a cc_out operand
4056 // when it's an ADD Rdm, SP, {Rdm|#imm0_255} instruction. We do
4057 // have to check the immediate range here since Thumb2 has a variant
4058 // that can handle a different range and has a cc_out operand.
4059 if (((isThumb() && Mnemonic == "add") ||
4060 (isThumbTwo() && Mnemonic == "sub")) &&
4061 Operands.size() == 6 &&
4062 static_cast<ARMOperand*>(Operands[3])->isReg() &&
4063 static_cast<ARMOperand*>(Operands[4])->isReg() &&
4064 static_cast<ARMOperand*>(Operands[4])->getReg() == ARM::SP &&
4065 static_cast<ARMOperand*>(Operands[1])->getReg() == 0 &&
4066 (static_cast<ARMOperand*>(Operands[5])->isReg() ||
4067 static_cast<ARMOperand*>(Operands[5])->isImm0_1020s4()))
4069 // For Thumb2, add/sub immediate does not have a cc_out operand for the
4070 // imm0_4095 variant. That's the least-preferred variant when
4071 // selecting via the generic "add" mnemonic, so to know that we
4072 // should remove the cc_out operand, we have to explicitly check that
4073 // it's not one of the other variants. Ugh.
4074 if (isThumbTwo() && (Mnemonic == "add" || Mnemonic == "sub") &&
4075 Operands.size() == 6 &&
4076 static_cast<ARMOperand*>(Operands[3])->isReg() &&
4077 static_cast<ARMOperand*>(Operands[4])->isReg() &&
4078 static_cast<ARMOperand*>(Operands[5])->isImm()) {
4079 // Nest conditions rather than one big 'if' statement for readability.
4081 // If either register is a high reg, it's either one of the SP
4082 // variants (handled above) or a 32-bit encoding, so we just
4083 // check against T3.
4084 if ((!isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) ||
4085 !isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg())) &&
4086 static_cast<ARMOperand*>(Operands[5])->isT2SOImm())
4088 // If both registers are low, we're in an IT block, and the immediate is
4089 // in range, we should use encoding T1 instead, which has a cc_out.
4091 isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) &&
4092 isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg()) &&
4093 static_cast<ARMOperand*>(Operands[5])->isImm0_7())
4096 // Otherwise, we use encoding T4, which does not have a cc_out
4101 // The thumb2 multiply instruction doesn't have a CCOut register, so
4102 // if we have a "mul" mnemonic in Thumb mode, check if we'll be able to
4103 // use the 16-bit encoding or not.
4104 if (isThumbTwo() && Mnemonic == "mul" && Operands.size() == 6 &&
4105 static_cast<ARMOperand*>(Operands[1])->getReg() == 0 &&
4106 static_cast<ARMOperand*>(Operands[3])->isReg() &&
4107 static_cast<ARMOperand*>(Operands[4])->isReg() &&
4108 static_cast<ARMOperand*>(Operands[5])->isReg() &&
4109 // If the registers aren't low regs, the destination reg isn't the
4110 // same as one of the source regs, or the cc_out operand is zero
4111 // outside of an IT block, we have to use the 32-bit encoding, so
4112 // remove the cc_out operand.
4113 (!isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) ||
4114 !isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg()) ||
4116 (static_cast<ARMOperand*>(Operands[3])->getReg() !=
4117 static_cast<ARMOperand*>(Operands[5])->getReg() &&
4118 static_cast<ARMOperand*>(Operands[3])->getReg() !=
4119 static_cast<ARMOperand*>(Operands[4])->getReg())))
4124 // Register-register 'add/sub' for thumb does not have a cc_out operand
4125 // when it's an ADD/SUB SP, #imm. Be lenient on count since there's also
4126 // the "add/sub SP, SP, #imm" version. If the follow-up operands aren't
4127 // right, this will result in better diagnostics (which operand is off)
4129 if (isThumb() && (Mnemonic == "add" || Mnemonic == "sub") &&
4130 (Operands.size() == 5 || Operands.size() == 6) &&
4131 static_cast<ARMOperand*>(Operands[3])->isReg() &&
4132 static_cast<ARMOperand*>(Operands[3])->getReg() == ARM::SP &&
4133 static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
4139 /// Parse an arm instruction mnemonic followed by its operands.
4140 bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
4141 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
4142 // Create the leading tokens for the mnemonic, split by '.' characters.
4143 size_t Start = 0, Next = Name.find('.');
4144 StringRef Mnemonic = Name.slice(Start, Next);
4146 // Split out the predication code and carry setting flag from the mnemonic.
4147 unsigned PredicationCode;
4148 unsigned ProcessorIMod;
4151 Mnemonic = splitMnemonic(Mnemonic, PredicationCode, CarrySetting,
4152 ProcessorIMod, ITMask);
4154 // In Thumb1, only the branch (B) instruction can be predicated.
4155 if (isThumbOne() && PredicationCode != ARMCC::AL && Mnemonic != "b") {
4156 Parser.EatToEndOfStatement();
4157 return Error(NameLoc, "conditional execution not supported in Thumb1");
4160 Operands.push_back(ARMOperand::CreateToken(Mnemonic, NameLoc));
4162 // Handle the IT instruction ITMask. Convert it to a bitmask. This
4163 // is the mask as it will be for the IT encoding if the conditional
4164 // encoding has a '1' as it's bit0 (i.e. 't' ==> '1'). In the case
4165 // where the conditional bit0 is zero, the instruction post-processing
4166 // will adjust the mask accordingly.
4167 if (Mnemonic == "it") {
4168 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + 2);
4169 if (ITMask.size() > 3) {
4170 Parser.EatToEndOfStatement();
4171 return Error(Loc, "too many conditions on IT instruction");
4174 for (unsigned i = ITMask.size(); i != 0; --i) {
4175 char pos = ITMask[i - 1];
4176 if (pos != 't' && pos != 'e') {
4177 Parser.EatToEndOfStatement();
4178 return Error(Loc, "illegal IT block condition mask '" + ITMask + "'");
4181 if (ITMask[i - 1] == 't')
4184 Operands.push_back(ARMOperand::CreateITMask(Mask, Loc));
4187 // FIXME: This is all a pretty gross hack. We should automatically handle
4188 // optional operands like this via tblgen.
4190 // Next, add the CCOut and ConditionCode operands, if needed.
4192 // For mnemonics which can ever incorporate a carry setting bit or predication
4193 // code, our matching model involves us always generating CCOut and
4194 // ConditionCode operands to match the mnemonic "as written" and then we let
4195 // the matcher deal with finding the right instruction or generating an
4196 // appropriate error.
4197 bool CanAcceptCarrySet, CanAcceptPredicationCode;
4198 getMnemonicAcceptInfo(Mnemonic, CanAcceptCarrySet, CanAcceptPredicationCode);
4200 // If we had a carry-set on an instruction that can't do that, issue an
4202 if (!CanAcceptCarrySet && CarrySetting) {
4203 Parser.EatToEndOfStatement();
4204 return Error(NameLoc, "instruction '" + Mnemonic +
4205 "' can not set flags, but 's' suffix specified");
4207 // If we had a predication code on an instruction that can't do that, issue an
4209 if (!CanAcceptPredicationCode && PredicationCode != ARMCC::AL) {
4210 Parser.EatToEndOfStatement();
4211 return Error(NameLoc, "instruction '" + Mnemonic +
4212 "' is not predicable, but condition code specified");
4215 // Add the carry setting operand, if necessary.
4216 if (CanAcceptCarrySet) {
4217 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size());
4218 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
4222 // Add the predication code operand, if necessary.
4223 if (CanAcceptPredicationCode) {
4224 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size() +
4226 Operands.push_back(ARMOperand::CreateCondCode(
4227 ARMCC::CondCodes(PredicationCode), Loc));
4230 // Add the processor imod operand, if necessary.
4231 if (ProcessorIMod) {
4232 Operands.push_back(ARMOperand::CreateImm(
4233 MCConstantExpr::Create(ProcessorIMod, getContext()),
4237 // Add the remaining tokens in the mnemonic.
4238 while (Next != StringRef::npos) {
4240 Next = Name.find('.', Start + 1);
4241 StringRef ExtraToken = Name.slice(Start, Next);
4243 // For now, we're only parsing Thumb1 (for the most part), so
4244 // just ignore ".n" qualifiers. We'll use them to restrict
4245 // matching when we do Thumb2.
4246 if (ExtraToken != ".n") {
4247 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Start);
4248 Operands.push_back(ARMOperand::CreateToken(ExtraToken, Loc));
4252 // Read the remaining operands.
4253 if (getLexer().isNot(AsmToken::EndOfStatement)) {
4254 // Read the first operand.
4255 if (parseOperand(Operands, Mnemonic)) {
4256 Parser.EatToEndOfStatement();
4260 while (getLexer().is(AsmToken::Comma)) {
4261 Parser.Lex(); // Eat the comma.
4263 // Parse and remember the operand.
4264 if (parseOperand(Operands, Mnemonic)) {
4265 Parser.EatToEndOfStatement();
4271 if (getLexer().isNot(AsmToken::EndOfStatement)) {
4272 SMLoc Loc = getLexer().getLoc();
4273 Parser.EatToEndOfStatement();
4274 return Error(Loc, "unexpected token in argument list");
4277 Parser.Lex(); // Consume the EndOfStatement
4279 // Some instructions, mostly Thumb, have forms for the same mnemonic that
4280 // do and don't have a cc_out optional-def operand. With some spot-checks
4281 // of the operand list, we can figure out which variant we're trying to
4282 // parse and adjust accordingly before actually matching. We shouldn't ever
4283 // try to remove a cc_out operand that was explicitly set on the the
4284 // mnemonic, of course (CarrySetting == true). Reason number #317 the
4285 // table driven matcher doesn't fit well with the ARM instruction set.
4286 if (!CarrySetting && shouldOmitCCOutOperand(Mnemonic, Operands)) {
4287 ARMOperand *Op = static_cast<ARMOperand*>(Operands[1]);
4288 Operands.erase(Operands.begin() + 1);
4292 // ARM mode 'blx' need special handling, as the register operand version
4293 // is predicable, but the label operand version is not. So, we can't rely
4294 // on the Mnemonic based checking to correctly figure out when to put
4295 // a k_CondCode operand in the list. If we're trying to match the label
4296 // version, remove the k_CondCode operand here.
4297 if (!isThumb() && Mnemonic == "blx" && Operands.size() == 3 &&
4298 static_cast<ARMOperand*>(Operands[2])->isImm()) {
4299 ARMOperand *Op = static_cast<ARMOperand*>(Operands[1]);
4300 Operands.erase(Operands.begin() + 1);
4304 // The vector-compare-to-zero instructions have a literal token "#0" at
4305 // the end that comes to here as an immediate operand. Convert it to a
4306 // token to play nicely with the matcher.
4307 if ((Mnemonic == "vceq" || Mnemonic == "vcge" || Mnemonic == "vcgt" ||
4308 Mnemonic == "vcle" || Mnemonic == "vclt") && Operands.size() == 6 &&
4309 static_cast<ARMOperand*>(Operands[5])->isImm()) {
4310 ARMOperand *Op = static_cast<ARMOperand*>(Operands[5]);
4311 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
4312 if (CE && CE->getValue() == 0) {
4313 Operands.erase(Operands.begin() + 5);
4314 Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
4318 // VCMP{E} does the same thing, but with a different operand count.
4319 if ((Mnemonic == "vcmp" || Mnemonic == "vcmpe") && Operands.size() == 5 &&
4320 static_cast<ARMOperand*>(Operands[4])->isImm()) {
4321 ARMOperand *Op = static_cast<ARMOperand*>(Operands[4]);
4322 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
4323 if (CE && CE->getValue() == 0) {
4324 Operands.erase(Operands.begin() + 4);
4325 Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
4329 // Similarly, the Thumb1 "RSB" instruction has a literal "#0" on the
4330 // end. Convert it to a token here.
4331 if (Mnemonic == "rsb" && isThumb() && Operands.size() == 6 &&
4332 static_cast<ARMOperand*>(Operands[5])->isImm()) {
4333 ARMOperand *Op = static_cast<ARMOperand*>(Operands[5]);
4334 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
4335 if (CE && CE->getValue() == 0) {
4336 Operands.erase(Operands.begin() + 5);
4337 Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
4345 // Validate context-sensitive operand constraints.
4347 // return 'true' if register list contains non-low GPR registers,
4348 // 'false' otherwise. If Reg is in the register list or is HiReg, set
4349 // 'containsReg' to true.
4350 static bool checkLowRegisterList(MCInst Inst, unsigned OpNo, unsigned Reg,
4351 unsigned HiReg, bool &containsReg) {
4352 containsReg = false;
4353 for (unsigned i = OpNo; i < Inst.getNumOperands(); ++i) {
4354 unsigned OpReg = Inst.getOperand(i).getReg();
4357 // Anything other than a low register isn't legal here.
4358 if (!isARMLowRegister(OpReg) && (!HiReg || OpReg != HiReg))
4364 // Check if the specified regisgter is in the register list of the inst,
4365 // starting at the indicated operand number.
4366 static bool listContainsReg(MCInst &Inst, unsigned OpNo, unsigned Reg) {
4367 for (unsigned i = OpNo; i < Inst.getNumOperands(); ++i) {
4368 unsigned OpReg = Inst.getOperand(i).getReg();
4375 // FIXME: We would really prefer to have MCInstrInfo (the wrapper around
4376 // the ARMInsts array) instead. Getting that here requires awkward
4377 // API changes, though. Better way?
4379 extern const MCInstrDesc ARMInsts[];
4381 static const MCInstrDesc &getInstDesc(unsigned Opcode) {
4382 return ARMInsts[Opcode];
4385 // FIXME: We would really like to be able to tablegen'erate this.
4387 validateInstruction(MCInst &Inst,
4388 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
4389 const MCInstrDesc &MCID = getInstDesc(Inst.getOpcode());
4390 SMLoc Loc = Operands[0]->getStartLoc();
4391 // Check the IT block state first.
4392 // NOTE: In Thumb mode, the BKPT instruction has the interesting property of
4393 // being allowed in IT blocks, but not being predicable. It just always
4395 if (inITBlock() && Inst.getOpcode() != ARM::tBKPT) {
4397 if (ITState.FirstCond)
4398 ITState.FirstCond = false;
4400 bit = (ITState.Mask >> (5 - ITState.CurPosition)) & 1;
4401 // The instruction must be predicable.
4402 if (!MCID.isPredicable())
4403 return Error(Loc, "instructions in IT block must be predicable");
4404 unsigned Cond = Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm();
4405 unsigned ITCond = bit ? ITState.Cond :
4406 ARMCC::getOppositeCondition(ITState.Cond);
4407 if (Cond != ITCond) {
4408 // Find the condition code Operand to get its SMLoc information.
4410 for (unsigned i = 1; i < Operands.size(); ++i)
4411 if (static_cast<ARMOperand*>(Operands[i])->isCondCode())
4412 CondLoc = Operands[i]->getStartLoc();
4413 return Error(CondLoc, "incorrect condition in IT block; got '" +
4414 StringRef(ARMCondCodeToString(ARMCC::CondCodes(Cond))) +
4415 "', but expected '" +
4416 ARMCondCodeToString(ARMCC::CondCodes(ITCond)) + "'");
4418 // Check for non-'al' condition codes outside of the IT block.
4419 } else if (isThumbTwo() && MCID.isPredicable() &&
4420 Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm() !=
4421 ARMCC::AL && Inst.getOpcode() != ARM::tB &&
4422 Inst.getOpcode() != ARM::t2B)
4423 return Error(Loc, "predicated instructions must be in IT block");
4425 switch (Inst.getOpcode()) {
4428 case ARM::LDRD_POST:
4430 // Rt2 must be Rt + 1.
4431 unsigned Rt = getARMRegisterNumbering(Inst.getOperand(0).getReg());
4432 unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(1).getReg());
4434 return Error(Operands[3]->getStartLoc(),
4435 "destination operands must be sequential");
4439 // Rt2 must be Rt + 1.
4440 unsigned Rt = getARMRegisterNumbering(Inst.getOperand(0).getReg());
4441 unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(1).getReg());
4443 return Error(Operands[3]->getStartLoc(),
4444 "source operands must be sequential");
4448 case ARM::STRD_POST:
4450 // Rt2 must be Rt + 1.
4451 unsigned Rt = getARMRegisterNumbering(Inst.getOperand(1).getReg());
4452 unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(2).getReg());
4454 return Error(Operands[3]->getStartLoc(),
4455 "source operands must be sequential");
4460 // width must be in range [1, 32-lsb]
4461 unsigned lsb = Inst.getOperand(2).getImm();
4462 unsigned widthm1 = Inst.getOperand(3).getImm();
4463 if (widthm1 >= 32 - lsb)
4464 return Error(Operands[5]->getStartLoc(),
4465 "bitfield width must be in range [1,32-lsb]");
4469 // If we're parsing Thumb2, the .w variant is available and handles
4470 // most cases that are normally illegal for a Thumb1 LDM
4471 // instruction. We'll make the transformation in processInstruction()
4474 // Thumb LDM instructions are writeback iff the base register is not
4475 // in the register list.
4476 unsigned Rn = Inst.getOperand(0).getReg();
4477 bool hasWritebackToken =
4478 (static_cast<ARMOperand*>(Operands[3])->isToken() &&
4479 static_cast<ARMOperand*>(Operands[3])->getToken() == "!");
4480 bool listContainsBase;
4481 if (checkLowRegisterList(Inst, 3, Rn, 0, listContainsBase) && !isThumbTwo())
4482 return Error(Operands[3 + hasWritebackToken]->getStartLoc(),
4483 "registers must be in range r0-r7");
4484 // If we should have writeback, then there should be a '!' token.
4485 if (!listContainsBase && !hasWritebackToken && !isThumbTwo())
4486 return Error(Operands[2]->getStartLoc(),
4487 "writeback operator '!' expected");
4488 // If we should not have writeback, there must not be a '!'. This is
4489 // true even for the 32-bit wide encodings.
4490 if (listContainsBase && hasWritebackToken)
4491 return Error(Operands[3]->getStartLoc(),
4492 "writeback operator '!' not allowed when base register "
4493 "in register list");
4497 case ARM::t2LDMIA_UPD: {
4498 if (listContainsReg(Inst, 3, Inst.getOperand(0).getReg()))
4499 return Error(Operands[4]->getStartLoc(),
4500 "writeback operator '!' not allowed when base register "
4501 "in register list");
4505 bool listContainsBase;
4506 if (checkLowRegisterList(Inst, 3, 0, ARM::PC, listContainsBase))
4507 return Error(Operands[2]->getStartLoc(),
4508 "registers must be in range r0-r7 or pc");
4512 bool listContainsBase;
4513 if (checkLowRegisterList(Inst, 3, 0, ARM::LR, listContainsBase))
4514 return Error(Operands[2]->getStartLoc(),
4515 "registers must be in range r0-r7 or lr");
4518 case ARM::tSTMIA_UPD: {
4519 bool listContainsBase;
4520 if (checkLowRegisterList(Inst, 4, 0, 0, listContainsBase) && !isThumbTwo())
4521 return Error(Operands[4]->getStartLoc(),
4522 "registers must be in range r0-r7");
4531 processInstruction(MCInst &Inst,
4532 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
4533 switch (Inst.getOpcode()) {
4534 case ARM::LDMIA_UPD:
4535 // If this is a load of a single register via a 'pop', then we should use
4536 // a post-indexed LDR instruction instead, per the ARM ARM.
4537 if (static_cast<ARMOperand*>(Operands[0])->getToken() == "pop" &&
4538 Inst.getNumOperands() == 5) {
4540 TmpInst.setOpcode(ARM::LDR_POST_IMM);
4541 TmpInst.addOperand(Inst.getOperand(4)); // Rt
4542 TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
4543 TmpInst.addOperand(Inst.getOperand(1)); // Rn
4544 TmpInst.addOperand(MCOperand::CreateReg(0)); // am2offset
4545 TmpInst.addOperand(MCOperand::CreateImm(4));
4546 TmpInst.addOperand(Inst.getOperand(2)); // CondCode
4547 TmpInst.addOperand(Inst.getOperand(3));
4551 case ARM::STMDB_UPD:
4552 // If this is a store of a single register via a 'push', then we should use
4553 // a pre-indexed STR instruction instead, per the ARM ARM.
4554 if (static_cast<ARMOperand*>(Operands[0])->getToken() == "push" &&
4555 Inst.getNumOperands() == 5) {
4557 TmpInst.setOpcode(ARM::STR_PRE_IMM);
4558 TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
4559 TmpInst.addOperand(Inst.getOperand(4)); // Rt
4560 TmpInst.addOperand(Inst.getOperand(1)); // addrmode_imm12
4561 TmpInst.addOperand(MCOperand::CreateImm(-4));
4562 TmpInst.addOperand(Inst.getOperand(2)); // CondCode
4563 TmpInst.addOperand(Inst.getOperand(3));
4568 // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
4569 // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
4570 // to encoding T2 if <Rd> is specified and encoding T2 is preferred
4571 // to encoding T1 if <Rd> is omitted."
4572 if (Inst.getOperand(3).getImm() < 8 && Operands.size() == 6)
4573 Inst.setOpcode(ARM::tADDi3);
4576 // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
4577 // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
4578 // to encoding T2 if <Rd> is specified and encoding T2 is preferred
4579 // to encoding T1 if <Rd> is omitted."
4580 if (Inst.getOperand(3).getImm() < 8 && Operands.size() == 6)
4581 Inst.setOpcode(ARM::tSUBi3);
4584 // A Thumb conditional branch outside of an IT block is a tBcc.
4585 if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock())
4586 Inst.setOpcode(ARM::tBcc);
4589 // A Thumb2 conditional branch outside of an IT block is a t2Bcc.
4590 if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock())
4591 Inst.setOpcode(ARM::t2Bcc);
4594 // If the conditional is AL or we're in an IT block, we really want t2B.
4595 if (Inst.getOperand(1).getImm() == ARMCC::AL || inITBlock())
4596 Inst.setOpcode(ARM::t2B);
4599 // If the conditional is AL, we really want tB.
4600 if (Inst.getOperand(1).getImm() == ARMCC::AL)
4601 Inst.setOpcode(ARM::tB);
4604 // If the register list contains any high registers, or if the writeback
4605 // doesn't match what tLDMIA can do, we need to use the 32-bit encoding
4606 // instead if we're in Thumb2. Otherwise, this should have generated
4607 // an error in validateInstruction().
4608 unsigned Rn = Inst.getOperand(0).getReg();
4609 bool hasWritebackToken =
4610 (static_cast<ARMOperand*>(Operands[3])->isToken() &&
4611 static_cast<ARMOperand*>(Operands[3])->getToken() == "!");
4612 bool listContainsBase;
4613 if (checkLowRegisterList(Inst, 3, Rn, 0, listContainsBase) ||
4614 (!listContainsBase && !hasWritebackToken) ||
4615 (listContainsBase && hasWritebackToken)) {
4616 // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
4617 assert (isThumbTwo());
4618 Inst.setOpcode(hasWritebackToken ? ARM::t2LDMIA_UPD : ARM::t2LDMIA);
4619 // If we're switching to the updating version, we need to insert
4620 // the writeback tied operand.
4621 if (hasWritebackToken)
4622 Inst.insert(Inst.begin(),
4623 MCOperand::CreateReg(Inst.getOperand(0).getReg()));
4627 case ARM::tSTMIA_UPD: {
4628 // If the register list contains any high registers, we need to use
4629 // the 32-bit encoding instead if we're in Thumb2. Otherwise, this
4630 // should have generated an error in validateInstruction().
4631 unsigned Rn = Inst.getOperand(0).getReg();
4632 bool listContainsBase;
4633 if (checkLowRegisterList(Inst, 4, Rn, 0, listContainsBase)) {
4634 // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
4635 assert (isThumbTwo());
4636 Inst.setOpcode(ARM::t2STMIA_UPD);
4641 // If we can use the 16-bit encoding and the user didn't explicitly
4642 // request the 32-bit variant, transform it here.
4643 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
4644 Inst.getOperand(1).getImm() <= 255 &&
4645 ((!inITBlock() && Inst.getOperand(2).getImm() == ARMCC::AL &&
4646 Inst.getOperand(4).getReg() == ARM::CPSR) ||
4647 (inITBlock() && Inst.getOperand(4).getReg() == 0)) &&
4648 (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
4649 static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
4650 // The operands aren't in the same order for tMOVi8...
4652 TmpInst.setOpcode(ARM::tMOVi8);
4653 TmpInst.addOperand(Inst.getOperand(0));
4654 TmpInst.addOperand(Inst.getOperand(4));
4655 TmpInst.addOperand(Inst.getOperand(1));
4656 TmpInst.addOperand(Inst.getOperand(2));
4657 TmpInst.addOperand(Inst.getOperand(3));
4663 // If we can use the 16-bit encoding and the user didn't explicitly
4664 // request the 32-bit variant, transform it here.
4665 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
4666 isARMLowRegister(Inst.getOperand(1).getReg()) &&
4667 Inst.getOperand(2).getImm() == ARMCC::AL &&
4668 Inst.getOperand(4).getReg() == ARM::CPSR &&
4669 (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
4670 static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
4671 // The operands aren't the same for tMOV[S]r... (no cc_out)
4673 TmpInst.setOpcode(Inst.getOperand(4).getReg() ? ARM::tMOVSr : ARM::tMOVr);
4674 TmpInst.addOperand(Inst.getOperand(0));
4675 TmpInst.addOperand(Inst.getOperand(1));
4676 TmpInst.addOperand(Inst.getOperand(2));
4677 TmpInst.addOperand(Inst.getOperand(3));
4686 // If we can use the 16-bit encoding and the user didn't explicitly
4687 // request the 32-bit variant, transform it here.
4688 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
4689 isARMLowRegister(Inst.getOperand(1).getReg()) &&
4690 Inst.getOperand(2).getImm() == 0 &&
4691 (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
4692 static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
4694 switch (Inst.getOpcode()) {
4695 default: llvm_unreachable("Illegal opcode!");
4696 case ARM::t2SXTH: NewOpc = ARM::tSXTH; break;
4697 case ARM::t2SXTB: NewOpc = ARM::tSXTB; break;
4698 case ARM::t2UXTH: NewOpc = ARM::tUXTH; break;
4699 case ARM::t2UXTB: NewOpc = ARM::tUXTB; break;
4701 // The operands aren't the same for thumb1 (no rotate operand).
4703 TmpInst.setOpcode(NewOpc);
4704 TmpInst.addOperand(Inst.getOperand(0));
4705 TmpInst.addOperand(Inst.getOperand(1));
4706 TmpInst.addOperand(Inst.getOperand(3));
4707 TmpInst.addOperand(Inst.getOperand(4));
4713 // The mask bits for all but the first condition are represented as
4714 // the low bit of the condition code value implies 't'. We currently
4715 // always have 1 implies 't', so XOR toggle the bits if the low bit
4716 // of the condition code is zero. The encoding also expects the low
4717 // bit of the condition to be encoded as bit 4 of the mask operand,
4718 // so mask that in if needed
4719 MCOperand &MO = Inst.getOperand(1);
4720 unsigned Mask = MO.getImm();
4721 unsigned OrigMask = Mask;
4722 unsigned TZ = CountTrailingZeros_32(Mask);
4723 if ((Inst.getOperand(0).getImm() & 1) == 0) {
4724 assert(Mask && TZ <= 3 && "illegal IT mask value!");
4725 for (unsigned i = 3; i != TZ; --i)
4731 // Set up the IT block state according to the IT instruction we just
4733 assert(!inITBlock() && "nested IT blocks?!");
4734 ITState.Cond = ARMCC::CondCodes(Inst.getOperand(0).getImm());
4735 ITState.Mask = OrigMask; // Use the original mask, not the updated one.
4736 ITState.CurPosition = 0;
4737 ITState.FirstCond = true;
4743 unsigned ARMAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
4744 // 16-bit thumb arithmetic instructions either require or preclude the 'S'
4745 // suffix depending on whether they're in an IT block or not.
4746 unsigned Opc = Inst.getOpcode();
4747 const MCInstrDesc &MCID = getInstDesc(Opc);
4748 if (MCID.TSFlags & ARMII::ThumbArithFlagSetting) {
4749 assert(MCID.hasOptionalDef() &&
4750 "optionally flag setting instruction missing optional def operand");
4751 assert(MCID.NumOperands == Inst.getNumOperands() &&
4752 "operand count mismatch!");
4753 // Find the optional-def operand (cc_out).
4756 !MCID.OpInfo[OpNo].isOptionalDef() && OpNo < MCID.NumOperands;
4759 // If we're parsing Thumb1, reject it completely.
4760 if (isThumbOne() && Inst.getOperand(OpNo).getReg() != ARM::CPSR)
4761 return Match_MnemonicFail;
4762 // If we're parsing Thumb2, which form is legal depends on whether we're
4764 if (isThumbTwo() && Inst.getOperand(OpNo).getReg() != ARM::CPSR &&
4766 return Match_RequiresITBlock;
4767 if (isThumbTwo() && Inst.getOperand(OpNo).getReg() == ARM::CPSR &&
4769 return Match_RequiresNotITBlock;
4771 // Some high-register supporting Thumb1 encodings only allow both registers
4772 // to be from r0-r7 when in Thumb2.
4773 else if (Opc == ARM::tADDhirr && isThumbOne() &&
4774 isARMLowRegister(Inst.getOperand(1).getReg()) &&
4775 isARMLowRegister(Inst.getOperand(2).getReg()))
4776 return Match_RequiresThumb2;
4777 // Others only require ARMv6 or later.
4778 else if (Opc == ARM::tMOVr && isThumbOne() && !hasV6Ops() &&
4779 isARMLowRegister(Inst.getOperand(0).getReg()) &&
4780 isARMLowRegister(Inst.getOperand(1).getReg()))
4781 return Match_RequiresV6;
4782 return Match_Success;
4786 MatchAndEmitInstruction(SMLoc IDLoc,
4787 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
4791 unsigned MatchResult;
4792 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
4793 switch (MatchResult) {
4796 // Context sensitive operand constraints aren't handled by the matcher,
4797 // so check them here.
4798 if (validateInstruction(Inst, Operands)) {
4799 // Still progress the IT block, otherwise one wrong condition causes
4800 // nasty cascading errors.
4801 forwardITPosition();
4805 // Some instructions need post-processing to, for example, tweak which
4806 // encoding is selected.
4807 processInstruction(Inst, Operands);
4809 // Only move forward at the very end so that everything in validate
4810 // and process gets a consistent answer about whether we're in an IT
4812 forwardITPosition();
4814 Out.EmitInstruction(Inst);
4816 case Match_MissingFeature:
4817 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
4819 case Match_InvalidOperand: {
4820 SMLoc ErrorLoc = IDLoc;
4821 if (ErrorInfo != ~0U) {
4822 if (ErrorInfo >= Operands.size())
4823 return Error(IDLoc, "too few operands for instruction");
4825 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
4826 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
4829 return Error(ErrorLoc, "invalid operand for instruction");
4831 case Match_MnemonicFail:
4832 return Error(IDLoc, "invalid instruction");
4833 case Match_ConversionFail:
4834 // The converter function will have already emited a diagnostic.
4836 case Match_RequiresNotITBlock:
4837 return Error(IDLoc, "flag setting instruction only valid outside IT block");
4838 case Match_RequiresITBlock:
4839 return Error(IDLoc, "instruction only valid inside IT block");
4840 case Match_RequiresV6:
4841 return Error(IDLoc, "instruction variant requires ARMv6 or later");
4842 case Match_RequiresThumb2:
4843 return Error(IDLoc, "instruction variant requires Thumb2");
4846 llvm_unreachable("Implement any new match types added!");
4850 /// parseDirective parses the arm specific directives
4851 bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
4852 StringRef IDVal = DirectiveID.getIdentifier();
4853 if (IDVal == ".word")
4854 return parseDirectiveWord(4, DirectiveID.getLoc());
4855 else if (IDVal == ".thumb")
4856 return parseDirectiveThumb(DirectiveID.getLoc());
4857 else if (IDVal == ".thumb_func")
4858 return parseDirectiveThumbFunc(DirectiveID.getLoc());
4859 else if (IDVal == ".code")
4860 return parseDirectiveCode(DirectiveID.getLoc());
4861 else if (IDVal == ".syntax")
4862 return parseDirectiveSyntax(DirectiveID.getLoc());
4866 /// parseDirectiveWord
4867 /// ::= .word [ expression (, expression)* ]
4868 bool ARMAsmParser::parseDirectiveWord(unsigned Size, SMLoc L) {
4869 if (getLexer().isNot(AsmToken::EndOfStatement)) {
4871 const MCExpr *Value;
4872 if (getParser().ParseExpression(Value))
4875 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
4877 if (getLexer().is(AsmToken::EndOfStatement))
4880 // FIXME: Improve diagnostic.
4881 if (getLexer().isNot(AsmToken::Comma))
4882 return Error(L, "unexpected token in directive");
4891 /// parseDirectiveThumb
4893 bool ARMAsmParser::parseDirectiveThumb(SMLoc L) {
4894 if (getLexer().isNot(AsmToken::EndOfStatement))
4895 return Error(L, "unexpected token in directive");
4898 // TODO: set thumb mode
4899 // TODO: tell the MC streamer the mode
4900 // getParser().getStreamer().Emit???();
4904 /// parseDirectiveThumbFunc
4905 /// ::= .thumbfunc symbol_name
4906 bool ARMAsmParser::parseDirectiveThumbFunc(SMLoc L) {
4907 const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
4908 bool isMachO = MAI.hasSubsectionsViaSymbols();
4911 // Darwin asm has function name after .thumb_func direction
4914 const AsmToken &Tok = Parser.getTok();
4915 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
4916 return Error(L, "unexpected token in .thumb_func directive");
4917 Name = Tok.getString();
4918 Parser.Lex(); // Consume the identifier token.
4921 if (getLexer().isNot(AsmToken::EndOfStatement))
4922 return Error(L, "unexpected token in directive");
4925 // FIXME: assuming function name will be the line following .thumb_func
4927 Name = Parser.getTok().getString();
4930 // Mark symbol as a thumb symbol.
4931 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
4932 getParser().getStreamer().EmitThumbFunc(Func);
4936 /// parseDirectiveSyntax
4937 /// ::= .syntax unified | divided
4938 bool ARMAsmParser::parseDirectiveSyntax(SMLoc L) {
4939 const AsmToken &Tok = Parser.getTok();
4940 if (Tok.isNot(AsmToken::Identifier))
4941 return Error(L, "unexpected token in .syntax directive");
4942 StringRef Mode = Tok.getString();
4943 if (Mode == "unified" || Mode == "UNIFIED")
4945 else if (Mode == "divided" || Mode == "DIVIDED")
4946 return Error(L, "'.syntax divided' arm asssembly not supported");
4948 return Error(L, "unrecognized syntax mode in .syntax directive");
4950 if (getLexer().isNot(AsmToken::EndOfStatement))
4951 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
4954 // TODO tell the MC streamer the mode
4955 // getParser().getStreamer().Emit???();
4959 /// parseDirectiveCode
4960 /// ::= .code 16 | 32
4961 bool ARMAsmParser::parseDirectiveCode(SMLoc L) {
4962 const AsmToken &Tok = Parser.getTok();
4963 if (Tok.isNot(AsmToken::Integer))
4964 return Error(L, "unexpected token in .code directive");
4965 int64_t Val = Parser.getTok().getIntVal();
4971 return Error(L, "invalid operand to .code directive");
4973 if (getLexer().isNot(AsmToken::EndOfStatement))
4974 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
4980 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
4984 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
4990 extern "C" void LLVMInitializeARMAsmLexer();
4992 /// Force static initialization.
4993 extern "C" void LLVMInitializeARMAsmParser() {
4994 RegisterMCAsmParser<ARMAsmParser> X(TheARMTarget);
4995 RegisterMCAsmParser<ARMAsmParser> Y(TheThumbTarget);
4996 LLVMInitializeARMAsmLexer();
4999 #define GET_REGISTER_MATCHER
5000 #define GET_MATCHER_IMPLEMENTATION
5001 #include "ARMGenAsmMatcher.inc"