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*> &);
202 bool validateInstruction(MCInst &Inst,
203 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
204 void processInstruction(MCInst &Inst,
205 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
206 bool shouldOmitCCOutOperand(StringRef Mnemonic,
207 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
210 enum ARMMatchResultTy {
211 Match_RequiresITBlock = FIRST_TARGET_MATCH_RESULT_TY,
212 Match_RequiresNotITBlock,
217 ARMAsmParser(MCSubtargetInfo &_STI, MCAsmParser &_Parser)
218 : MCTargetAsmParser(), STI(_STI), Parser(_Parser) {
219 MCAsmParserExtension::Initialize(_Parser);
221 // Initialize the set of available features.
222 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
224 // Not in an ITBlock to start with.
225 ITState.CurPosition = ~0U;
228 // Implementation of the MCTargetAsmParser interface:
229 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
230 bool ParseInstruction(StringRef Name, SMLoc NameLoc,
231 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
232 bool ParseDirective(AsmToken DirectiveID);
234 unsigned checkTargetMatchPredicate(MCInst &Inst);
236 bool MatchAndEmitInstruction(SMLoc IDLoc,
237 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
240 } // end anonymous namespace
244 /// ARMOperand - Instances of this class represent a parsed ARM machine
246 class ARMOperand : public MCParsedAsmOperand {
271 k_BitfieldDescriptor,
275 SMLoc StartLoc, EndLoc;
276 SmallVector<unsigned, 8> Registers;
280 ARMCC::CondCodes Val;
300 ARM_PROC::IFlags Val;
316 // A vector register list is a sequential list of 1 to 4 registers.
331 unsigned Val; // encoded 8-bit representation
334 /// Combined record for all forms of ARM address expressions.
337 // Offset is in OffsetReg or OffsetImm. If both are zero, no offset
339 const MCConstantExpr *OffsetImm; // Offset immediate value
340 unsigned OffsetRegNum; // Offset register num, when OffsetImm == NULL
341 ARM_AM::ShiftOpc ShiftType; // Shift type for OffsetReg
342 unsigned ShiftImm; // shift for OffsetReg.
343 unsigned Alignment; // 0 = no alignment specified
344 // n = alignment in bytes (8, 16, or 32)
345 unsigned isNegative : 1; // Negated OffsetReg? (~'U' bit)
351 ARM_AM::ShiftOpc ShiftTy;
360 ARM_AM::ShiftOpc ShiftTy;
366 ARM_AM::ShiftOpc ShiftTy;
379 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
381 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
383 StartLoc = o.StartLoc;
400 case k_DPRRegisterList:
401 case k_SPRRegisterList:
402 Registers = o.Registers;
405 VectorList = o.VectorList;
412 CoprocOption = o.CoprocOption;
420 case k_MemBarrierOpt:
426 case k_PostIndexRegister:
427 PostIdxReg = o.PostIdxReg;
435 case k_ShifterImmediate:
436 ShifterImm = o.ShifterImm;
438 case k_ShiftedRegister:
439 RegShiftedReg = o.RegShiftedReg;
441 case k_ShiftedImmediate:
442 RegShiftedImm = o.RegShiftedImm;
444 case k_RotateImmediate:
447 case k_BitfieldDescriptor:
448 Bitfield = o.Bitfield;
451 VectorIndex = o.VectorIndex;
456 /// getStartLoc - Get the location of the first token of this operand.
457 SMLoc getStartLoc() const { return StartLoc; }
458 /// getEndLoc - Get the location of the last token of this operand.
459 SMLoc getEndLoc() const { return EndLoc; }
461 ARMCC::CondCodes getCondCode() const {
462 assert(Kind == k_CondCode && "Invalid access!");
466 unsigned getCoproc() const {
467 assert((Kind == k_CoprocNum || Kind == k_CoprocReg) && "Invalid access!");
471 StringRef getToken() const {
472 assert(Kind == k_Token && "Invalid access!");
473 return StringRef(Tok.Data, Tok.Length);
476 unsigned getReg() const {
477 assert((Kind == k_Register || Kind == k_CCOut) && "Invalid access!");
481 const SmallVectorImpl<unsigned> &getRegList() const {
482 assert((Kind == k_RegisterList || Kind == k_DPRRegisterList ||
483 Kind == k_SPRRegisterList) && "Invalid access!");
487 const MCExpr *getImm() const {
488 assert(Kind == k_Immediate && "Invalid access!");
492 unsigned getFPImm() const {
493 assert(Kind == k_FPImmediate && "Invalid access!");
497 unsigned getVectorIndex() const {
498 assert(Kind == k_VectorIndex && "Invalid access!");
499 return VectorIndex.Val;
502 ARM_MB::MemBOpt getMemBarrierOpt() const {
503 assert(Kind == k_MemBarrierOpt && "Invalid access!");
507 ARM_PROC::IFlags getProcIFlags() const {
508 assert(Kind == k_ProcIFlags && "Invalid access!");
512 unsigned getMSRMask() const {
513 assert(Kind == k_MSRMask && "Invalid access!");
517 bool isCoprocNum() const { return Kind == k_CoprocNum; }
518 bool isCoprocReg() const { return Kind == k_CoprocReg; }
519 bool isCoprocOption() const { return Kind == k_CoprocOption; }
520 bool isCondCode() const { return Kind == k_CondCode; }
521 bool isCCOut() const { return Kind == k_CCOut; }
522 bool isITMask() const { return Kind == k_ITCondMask; }
523 bool isITCondCode() const { return Kind == k_CondCode; }
524 bool isImm() const { return Kind == k_Immediate; }
525 bool isFPImm() const { return Kind == k_FPImmediate; }
526 bool isImm8s4() const {
527 if (Kind != k_Immediate)
529 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
530 if (!CE) return false;
531 int64_t Value = CE->getValue();
532 return ((Value & 3) == 0) && Value >= -1020 && Value <= 1020;
534 bool isImm0_1020s4() 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 >= 0 && Value <= 1020;
542 bool isImm0_508s4() 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 <= 508;
550 bool isImm0_255() 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 >= 0 && Value < 256;
558 bool isImm0_7() 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 < 8;
566 bool isImm0_15() 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 < 16;
574 bool isImm0_31() 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 < 32;
582 bool isImm1_16() 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 < 17;
590 bool isImm1_32() 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 < 33;
598 bool isImm0_65535() 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 < 65536;
606 bool isImm0_65535Expr() const {
607 if (Kind != k_Immediate)
609 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
610 // If it's not a constant expression, it'll generate a fixup and be
612 if (!CE) return true;
613 int64_t Value = CE->getValue();
614 return Value >= 0 && Value < 65536;
616 bool isImm24bit() const {
617 if (Kind != k_Immediate)
619 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
620 if (!CE) return false;
621 int64_t Value = CE->getValue();
622 return Value >= 0 && Value <= 0xffffff;
624 bool isImmThumbSR() 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 < 33;
632 bool isPKHLSLImm() 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 < 32;
640 bool isPKHASRImm() 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 isARMSOImm() 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 ARM_AM::getSOImmVal(Value) != -1;
656 bool isT2SOImm() 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::getT2SOImmVal(Value) != -1;
664 bool isSetEndImm() 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 Value == 1 || Value == 0;
672 bool isReg() const { return Kind == k_Register; }
673 bool isRegList() const { return Kind == k_RegisterList; }
674 bool isDPRRegList() const { return Kind == k_DPRRegisterList; }
675 bool isSPRRegList() const { return Kind == k_SPRRegisterList; }
676 bool isToken() const { return Kind == k_Token; }
677 bool isMemBarrierOpt() const { return Kind == k_MemBarrierOpt; }
678 bool isMemory() const { return Kind == k_Memory; }
679 bool isShifterImm() const { return Kind == k_ShifterImmediate; }
680 bool isRegShiftedReg() const { return Kind == k_ShiftedRegister; }
681 bool isRegShiftedImm() const { return Kind == k_ShiftedImmediate; }
682 bool isRotImm() const { return Kind == k_RotateImmediate; }
683 bool isBitfield() const { return Kind == k_BitfieldDescriptor; }
684 bool isPostIdxRegShifted() const { return Kind == k_PostIndexRegister; }
685 bool isPostIdxReg() const {
686 return Kind == k_PostIndexRegister && PostIdxReg.ShiftTy == ARM_AM::no_shift;
688 bool isMemNoOffset(bool alignOK = false) const {
691 // No offset of any kind.
692 return Memory.OffsetRegNum == 0 && Memory.OffsetImm == 0 &&
693 (alignOK || Memory.Alignment == 0);
695 bool isAlignedMemory() const {
696 return isMemNoOffset(true);
698 bool isAddrMode2() const {
699 if (!isMemory() || Memory.Alignment != 0) return false;
700 // Check for register offset.
701 if (Memory.OffsetRegNum) return true;
702 // Immediate offset in range [-4095, 4095].
703 if (!Memory.OffsetImm) return true;
704 int64_t Val = Memory.OffsetImm->getValue();
705 return Val > -4096 && Val < 4096;
707 bool isAM2OffsetImm() const {
708 if (Kind != k_Immediate)
710 // Immediate offset in range [-4095, 4095].
711 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
712 if (!CE) return false;
713 int64_t Val = CE->getValue();
714 return Val > -4096 && Val < 4096;
716 bool isAddrMode3() const {
717 if (!isMemory() || Memory.Alignment != 0) return false;
718 // No shifts are legal for AM3.
719 if (Memory.ShiftType != ARM_AM::no_shift) return false;
720 // Check for register offset.
721 if (Memory.OffsetRegNum) return true;
722 // Immediate offset in range [-255, 255].
723 if (!Memory.OffsetImm) return true;
724 int64_t Val = Memory.OffsetImm->getValue();
725 return Val > -256 && Val < 256;
727 bool isAM3Offset() const {
728 if (Kind != k_Immediate && Kind != k_PostIndexRegister)
730 if (Kind == k_PostIndexRegister)
731 return PostIdxReg.ShiftTy == ARM_AM::no_shift;
732 // Immediate offset in range [-255, 255].
733 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
734 if (!CE) return false;
735 int64_t Val = CE->getValue();
736 // Special case, #-0 is INT32_MIN.
737 return (Val > -256 && Val < 256) || Val == INT32_MIN;
739 bool isAddrMode5() const {
740 if (!isMemory() || Memory.Alignment != 0) return false;
741 // Check for register offset.
742 if (Memory.OffsetRegNum) return false;
743 // Immediate offset in range [-1020, 1020] and a multiple of 4.
744 if (!Memory.OffsetImm) return true;
745 int64_t Val = Memory.OffsetImm->getValue();
746 return (Val >= -1020 && Val <= 1020 && ((Val & 3) == 0)) ||
749 bool isMemTBB() const {
750 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
751 Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
755 bool isMemTBH() const {
756 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
757 Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm != 1 ||
758 Memory.Alignment != 0 )
762 bool isMemRegOffset() const {
763 if (!isMemory() || !Memory.OffsetRegNum || Memory.Alignment != 0)
767 bool isT2MemRegOffset() const {
768 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
769 Memory.Alignment != 0)
771 // Only lsl #{0, 1, 2, 3} allowed.
772 if (Memory.ShiftType == ARM_AM::no_shift)
774 if (Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm > 3)
778 bool isMemThumbRR() const {
779 // Thumb reg+reg addressing is simple. Just two registers, a base and
780 // an offset. No shifts, negations or any other complicating factors.
781 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
782 Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
784 return isARMLowRegister(Memory.BaseRegNum) &&
785 (!Memory.OffsetRegNum || isARMLowRegister(Memory.OffsetRegNum));
787 bool isMemThumbRIs4() const {
788 if (!isMemory() || Memory.OffsetRegNum != 0 ||
789 !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
791 // Immediate offset, multiple of 4 in range [0, 124].
792 if (!Memory.OffsetImm) return true;
793 int64_t Val = Memory.OffsetImm->getValue();
794 return Val >= 0 && Val <= 124 && (Val % 4) == 0;
796 bool isMemThumbRIs2() const {
797 if (!isMemory() || Memory.OffsetRegNum != 0 ||
798 !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
800 // Immediate offset, multiple of 4 in range [0, 62].
801 if (!Memory.OffsetImm) return true;
802 int64_t Val = Memory.OffsetImm->getValue();
803 return Val >= 0 && Val <= 62 && (Val % 2) == 0;
805 bool isMemThumbRIs1() const {
806 if (!isMemory() || Memory.OffsetRegNum != 0 ||
807 !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
809 // Immediate offset in range [0, 31].
810 if (!Memory.OffsetImm) return true;
811 int64_t Val = Memory.OffsetImm->getValue();
812 return Val >= 0 && Val <= 31;
814 bool isMemThumbSPI() const {
815 if (!isMemory() || Memory.OffsetRegNum != 0 ||
816 Memory.BaseRegNum != ARM::SP || Memory.Alignment != 0)
818 // Immediate offset, multiple of 4 in range [0, 1020].
819 if (!Memory.OffsetImm) return true;
820 int64_t Val = Memory.OffsetImm->getValue();
821 return Val >= 0 && Val <= 1020 && (Val % 4) == 0;
823 bool isMemImm8s4Offset() const {
824 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
826 // Immediate offset a multiple of 4 in range [-1020, 1020].
827 if (!Memory.OffsetImm) return true;
828 int64_t Val = Memory.OffsetImm->getValue();
829 return Val >= -1020 && Val <= 1020 && (Val & 3) == 0;
831 bool isMemImm0_1020s4Offset() const {
832 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
834 // Immediate offset a multiple of 4 in range [0, 1020].
835 if (!Memory.OffsetImm) return true;
836 int64_t Val = Memory.OffsetImm->getValue();
837 return Val >= 0 && Val <= 1020 && (Val & 3) == 0;
839 bool isMemImm8Offset() const {
840 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
842 // Immediate offset in range [-255, 255].
843 if (!Memory.OffsetImm) return true;
844 int64_t Val = Memory.OffsetImm->getValue();
845 return (Val == INT32_MIN) || (Val > -256 && Val < 256);
847 bool isMemPosImm8Offset() const {
848 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
850 // Immediate offset in range [0, 255].
851 if (!Memory.OffsetImm) return true;
852 int64_t Val = Memory.OffsetImm->getValue();
853 return Val >= 0 && Val < 256;
855 bool isMemNegImm8Offset() const {
856 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
858 // Immediate offset in range [-255, -1].
859 if (!Memory.OffsetImm) return true;
860 int64_t Val = Memory.OffsetImm->getValue();
861 return Val > -256 && Val < 0;
863 bool isMemUImm12Offset() const {
864 // If we have an immediate that's not a constant, treat it as a label
865 // reference needing a fixup. If it is a constant, it's something else
867 if (Kind == k_Immediate && !isa<MCConstantExpr>(getImm()))
870 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
872 // Immediate offset in range [0, 4095].
873 if (!Memory.OffsetImm) return true;
874 int64_t Val = Memory.OffsetImm->getValue();
875 return (Val >= 0 && Val < 4096);
877 bool isMemImm12Offset() const {
878 // If we have an immediate that's not a constant, treat it as a label
879 // reference needing a fixup. If it is a constant, it's something else
881 if (Kind == k_Immediate && !isa<MCConstantExpr>(getImm()))
884 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
886 // Immediate offset in range [-4095, 4095].
887 if (!Memory.OffsetImm) return true;
888 int64_t Val = Memory.OffsetImm->getValue();
889 return (Val > -4096 && Val < 4096) || (Val == INT32_MIN);
891 bool isPostIdxImm8() const {
892 if (Kind != k_Immediate)
894 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
895 if (!CE) return false;
896 int64_t Val = CE->getValue();
897 return (Val > -256 && Val < 256) || (Val == INT32_MIN);
899 bool isPostIdxImm8s4() const {
900 if (Kind != k_Immediate)
902 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
903 if (!CE) return false;
904 int64_t Val = CE->getValue();
905 return ((Val & 3) == 0 && Val >= -1020 && Val <= 1020) ||
909 bool isMSRMask() const { return Kind == k_MSRMask; }
910 bool isProcIFlags() const { return Kind == k_ProcIFlags; }
913 bool isVecListOneD() const {
914 if (Kind != k_VectorList) return false;
915 return VectorList.Count == 1;
918 bool isVectorIndex8() const {
919 if (Kind != k_VectorIndex) return false;
920 return VectorIndex.Val < 8;
922 bool isVectorIndex16() const {
923 if (Kind != k_VectorIndex) return false;
924 return VectorIndex.Val < 4;
926 bool isVectorIndex32() const {
927 if (Kind != k_VectorIndex) return false;
928 return VectorIndex.Val < 2;
931 bool isNEONi8splat() const {
932 if (Kind != k_Immediate)
934 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
935 // Must be a constant.
936 if (!CE) return false;
937 int64_t Value = CE->getValue();
938 // i8 value splatted across 8 bytes. The immediate is just the 8 byte
940 return Value >= 0 && Value < 256;
943 bool isNEONi16splat() const {
944 if (Kind != k_Immediate)
946 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
947 // Must be a constant.
948 if (!CE) return false;
949 int64_t Value = CE->getValue();
950 // i16 value in the range [0,255] or [0x0100, 0xff00]
951 return (Value >= 0 && Value < 256) || (Value >= 0x0100 && Value <= 0xff00);
954 bool isNEONi32splat() const {
955 if (Kind != k_Immediate)
957 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
958 // Must be a constant.
959 if (!CE) return false;
960 int64_t Value = CE->getValue();
961 // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X.
962 return (Value >= 0 && Value < 256) ||
963 (Value >= 0x0100 && Value <= 0xff00) ||
964 (Value >= 0x010000 && Value <= 0xff0000) ||
965 (Value >= 0x01000000 && Value <= 0xff000000);
968 bool isNEONi32vmov() const {
969 if (Kind != k_Immediate)
971 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
972 // Must be a constant.
973 if (!CE) return false;
974 int64_t Value = CE->getValue();
975 // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X,
976 // for VMOV/VMVN only, 00Xf or 0Xff are also accepted.
977 return (Value >= 0 && Value < 256) ||
978 (Value >= 0x0100 && Value <= 0xff00) ||
979 (Value >= 0x010000 && Value <= 0xff0000) ||
980 (Value >= 0x01000000 && Value <= 0xff000000) ||
981 (Value >= 0x01ff && Value <= 0xffff && (Value & 0xff) == 0xff) ||
982 (Value >= 0x01ffff && Value <= 0xffffff && (Value & 0xffff) == 0xffff);
985 bool isNEONi64splat() const {
986 if (Kind != k_Immediate)
988 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
989 // Must be a constant.
990 if (!CE) return false;
991 uint64_t Value = CE->getValue();
992 // i64 value with each byte being either 0 or 0xff.
993 for (unsigned i = 0; i < 8; ++i)
994 if ((Value & 0xff) != 0 && (Value & 0xff) != 0xff) return false;
998 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
999 // Add as immediates when possible. Null MCExpr = 0.
1001 Inst.addOperand(MCOperand::CreateImm(0));
1002 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
1003 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
1005 Inst.addOperand(MCOperand::CreateExpr(Expr));
1008 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
1009 assert(N == 2 && "Invalid number of operands!");
1010 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
1011 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
1012 Inst.addOperand(MCOperand::CreateReg(RegNum));
1015 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
1016 assert(N == 1 && "Invalid number of operands!");
1017 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
1020 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
1021 assert(N == 1 && "Invalid number of operands!");
1022 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
1025 void addCoprocOptionOperands(MCInst &Inst, unsigned N) const {
1026 assert(N == 1 && "Invalid number of operands!");
1027 Inst.addOperand(MCOperand::CreateImm(CoprocOption.Val));
1030 void addITMaskOperands(MCInst &Inst, unsigned N) const {
1031 assert(N == 1 && "Invalid number of operands!");
1032 Inst.addOperand(MCOperand::CreateImm(ITMask.Mask));
1035 void addITCondCodeOperands(MCInst &Inst, unsigned N) const {
1036 assert(N == 1 && "Invalid number of operands!");
1037 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
1040 void addCCOutOperands(MCInst &Inst, unsigned N) const {
1041 assert(N == 1 && "Invalid number of operands!");
1042 Inst.addOperand(MCOperand::CreateReg(getReg()));
1045 void addRegOperands(MCInst &Inst, unsigned N) const {
1046 assert(N == 1 && "Invalid number of operands!");
1047 Inst.addOperand(MCOperand::CreateReg(getReg()));
1050 void addRegShiftedRegOperands(MCInst &Inst, unsigned N) const {
1051 assert(N == 3 && "Invalid number of operands!");
1052 assert(isRegShiftedReg() && "addRegShiftedRegOperands() on non RegShiftedReg!");
1053 Inst.addOperand(MCOperand::CreateReg(RegShiftedReg.SrcReg));
1054 Inst.addOperand(MCOperand::CreateReg(RegShiftedReg.ShiftReg));
1055 Inst.addOperand(MCOperand::CreateImm(
1056 ARM_AM::getSORegOpc(RegShiftedReg.ShiftTy, RegShiftedReg.ShiftImm)));
1059 void addRegShiftedImmOperands(MCInst &Inst, unsigned N) const {
1060 assert(N == 2 && "Invalid number of operands!");
1061 assert(isRegShiftedImm() && "addRegShiftedImmOperands() on non RegShiftedImm!");
1062 Inst.addOperand(MCOperand::CreateReg(RegShiftedImm.SrcReg));
1063 Inst.addOperand(MCOperand::CreateImm(
1064 ARM_AM::getSORegOpc(RegShiftedImm.ShiftTy, RegShiftedImm.ShiftImm)));
1067 void addShifterImmOperands(MCInst &Inst, unsigned N) const {
1068 assert(N == 1 && "Invalid number of operands!");
1069 Inst.addOperand(MCOperand::CreateImm((ShifterImm.isASR << 5) |
1073 void addRegListOperands(MCInst &Inst, unsigned N) const {
1074 assert(N == 1 && "Invalid number of operands!");
1075 const SmallVectorImpl<unsigned> &RegList = getRegList();
1076 for (SmallVectorImpl<unsigned>::const_iterator
1077 I = RegList.begin(), E = RegList.end(); I != E; ++I)
1078 Inst.addOperand(MCOperand::CreateReg(*I));
1081 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
1082 addRegListOperands(Inst, N);
1085 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
1086 addRegListOperands(Inst, N);
1089 void addRotImmOperands(MCInst &Inst, unsigned N) const {
1090 assert(N == 1 && "Invalid number of operands!");
1091 // Encoded as val>>3. The printer handles display as 8, 16, 24.
1092 Inst.addOperand(MCOperand::CreateImm(RotImm.Imm >> 3));
1095 void addBitfieldOperands(MCInst &Inst, unsigned N) const {
1096 assert(N == 1 && "Invalid number of operands!");
1097 // Munge the lsb/width into a bitfield mask.
1098 unsigned lsb = Bitfield.LSB;
1099 unsigned width = Bitfield.Width;
1100 // Make a 32-bit mask w/ the referenced bits clear and all other bits set.
1101 uint32_t Mask = ~(((uint32_t)0xffffffff >> lsb) << (32 - width) >>
1102 (32 - (lsb + width)));
1103 Inst.addOperand(MCOperand::CreateImm(Mask));
1106 void addImmOperands(MCInst &Inst, unsigned N) const {
1107 assert(N == 1 && "Invalid number of operands!");
1108 addExpr(Inst, getImm());
1111 void addFPImmOperands(MCInst &Inst, unsigned N) const {
1112 assert(N == 1 && "Invalid number of operands!");
1113 Inst.addOperand(MCOperand::CreateImm(getFPImm()));
1116 void addImm8s4Operands(MCInst &Inst, unsigned N) const {
1117 assert(N == 1 && "Invalid number of operands!");
1118 // FIXME: We really want to scale the value here, but the LDRD/STRD
1119 // instruction don't encode operands that way yet.
1120 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1121 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
1124 void addImm0_1020s4Operands(MCInst &Inst, unsigned N) const {
1125 assert(N == 1 && "Invalid number of operands!");
1126 // The immediate is scaled by four in the encoding and is stored
1127 // in the MCInst as such. Lop off the low two bits here.
1128 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1129 Inst.addOperand(MCOperand::CreateImm(CE->getValue() / 4));
1132 void addImm0_508s4Operands(MCInst &Inst, unsigned N) const {
1133 assert(N == 1 && "Invalid number of operands!");
1134 // The immediate is scaled by four in the encoding and is stored
1135 // in the MCInst as such. Lop off the low two bits here.
1136 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1137 Inst.addOperand(MCOperand::CreateImm(CE->getValue() / 4));
1140 void addImm0_255Operands(MCInst &Inst, unsigned N) const {
1141 assert(N == 1 && "Invalid number of operands!");
1142 addExpr(Inst, getImm());
1145 void addImm0_7Operands(MCInst &Inst, unsigned N) const {
1146 assert(N == 1 && "Invalid number of operands!");
1147 addExpr(Inst, getImm());
1150 void addImm0_15Operands(MCInst &Inst, unsigned N) const {
1151 assert(N == 1 && "Invalid number of operands!");
1152 addExpr(Inst, getImm());
1155 void addImm0_31Operands(MCInst &Inst, unsigned N) const {
1156 assert(N == 1 && "Invalid number of operands!");
1157 addExpr(Inst, getImm());
1160 void addImm1_16Operands(MCInst &Inst, unsigned N) const {
1161 assert(N == 1 && "Invalid number of operands!");
1162 // The constant encodes as the immediate-1, and we store in the instruction
1163 // the bits as encoded, so subtract off one here.
1164 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1165 Inst.addOperand(MCOperand::CreateImm(CE->getValue() - 1));
1168 void addImm1_32Operands(MCInst &Inst, unsigned N) const {
1169 assert(N == 1 && "Invalid number of operands!");
1170 // The constant encodes as the immediate-1, and we store in the instruction
1171 // the bits as encoded, so subtract off one here.
1172 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1173 Inst.addOperand(MCOperand::CreateImm(CE->getValue() - 1));
1176 void addImm0_65535Operands(MCInst &Inst, unsigned N) const {
1177 assert(N == 1 && "Invalid number of operands!");
1178 addExpr(Inst, getImm());
1181 void addImm0_65535ExprOperands(MCInst &Inst, unsigned N) const {
1182 assert(N == 1 && "Invalid number of operands!");
1183 addExpr(Inst, getImm());
1186 void addImm24bitOperands(MCInst &Inst, unsigned N) const {
1187 assert(N == 1 && "Invalid number of operands!");
1188 addExpr(Inst, getImm());
1191 void addImmThumbSROperands(MCInst &Inst, unsigned N) const {
1192 assert(N == 1 && "Invalid number of operands!");
1193 // The constant encodes as the immediate, except for 32, which encodes as
1195 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1196 unsigned Imm = CE->getValue();
1197 Inst.addOperand(MCOperand::CreateImm((Imm == 32 ? 0 : Imm)));
1200 void addPKHLSLImmOperands(MCInst &Inst, unsigned N) const {
1201 assert(N == 1 && "Invalid number of operands!");
1202 addExpr(Inst, getImm());
1205 void addPKHASRImmOperands(MCInst &Inst, unsigned N) const {
1206 assert(N == 1 && "Invalid number of operands!");
1207 // An ASR value of 32 encodes as 0, so that's how we want to add it to
1208 // the instruction as well.
1209 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1210 int Val = CE->getValue();
1211 Inst.addOperand(MCOperand::CreateImm(Val == 32 ? 0 : Val));
1214 void addARMSOImmOperands(MCInst &Inst, unsigned N) const {
1215 assert(N == 1 && "Invalid number of operands!");
1216 addExpr(Inst, getImm());
1219 void addT2SOImmOperands(MCInst &Inst, unsigned N) const {
1220 assert(N == 1 && "Invalid number of operands!");
1221 addExpr(Inst, getImm());
1224 void addSetEndImmOperands(MCInst &Inst, unsigned N) const {
1225 assert(N == 1 && "Invalid number of operands!");
1226 addExpr(Inst, getImm());
1229 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
1230 assert(N == 1 && "Invalid number of operands!");
1231 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
1234 void addMemNoOffsetOperands(MCInst &Inst, unsigned N) const {
1235 assert(N == 1 && "Invalid number of operands!");
1236 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1239 void addAlignedMemoryOperands(MCInst &Inst, unsigned N) const {
1240 assert(N == 2 && "Invalid number of operands!");
1241 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1242 Inst.addOperand(MCOperand::CreateImm(Memory.Alignment));
1245 void addAddrMode2Operands(MCInst &Inst, unsigned N) const {
1246 assert(N == 3 && "Invalid number of operands!");
1247 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1248 if (!Memory.OffsetRegNum) {
1249 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1250 // Special case for #-0
1251 if (Val == INT32_MIN) Val = 0;
1252 if (Val < 0) Val = -Val;
1253 Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
1255 // For register offset, we encode the shift type and negation flag
1257 Val = ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
1258 Memory.ShiftImm, Memory.ShiftType);
1260 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1261 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1262 Inst.addOperand(MCOperand::CreateImm(Val));
1265 void addAM2OffsetImmOperands(MCInst &Inst, unsigned N) const {
1266 assert(N == 2 && "Invalid number of operands!");
1267 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1268 assert(CE && "non-constant AM2OffsetImm operand!");
1269 int32_t Val = CE->getValue();
1270 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1271 // Special case for #-0
1272 if (Val == INT32_MIN) Val = 0;
1273 if (Val < 0) Val = -Val;
1274 Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
1275 Inst.addOperand(MCOperand::CreateReg(0));
1276 Inst.addOperand(MCOperand::CreateImm(Val));
1279 void addAddrMode3Operands(MCInst &Inst, unsigned N) const {
1280 assert(N == 3 && "Invalid number of operands!");
1281 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1282 if (!Memory.OffsetRegNum) {
1283 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1284 // Special case for #-0
1285 if (Val == INT32_MIN) Val = 0;
1286 if (Val < 0) Val = -Val;
1287 Val = ARM_AM::getAM3Opc(AddSub, Val);
1289 // For register offset, we encode the shift type and negation flag
1291 Val = ARM_AM::getAM3Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add, 0);
1293 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1294 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1295 Inst.addOperand(MCOperand::CreateImm(Val));
1298 void addAM3OffsetOperands(MCInst &Inst, unsigned N) const {
1299 assert(N == 2 && "Invalid number of operands!");
1300 if (Kind == k_PostIndexRegister) {
1302 ARM_AM::getAM3Opc(PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub, 0);
1303 Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1304 Inst.addOperand(MCOperand::CreateImm(Val));
1309 const MCConstantExpr *CE = static_cast<const MCConstantExpr*>(getImm());
1310 int32_t Val = CE->getValue();
1311 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1312 // Special case for #-0
1313 if (Val == INT32_MIN) Val = 0;
1314 if (Val < 0) Val = -Val;
1315 Val = ARM_AM::getAM3Opc(AddSub, Val);
1316 Inst.addOperand(MCOperand::CreateReg(0));
1317 Inst.addOperand(MCOperand::CreateImm(Val));
1320 void addAddrMode5Operands(MCInst &Inst, unsigned N) const {
1321 assert(N == 2 && "Invalid number of operands!");
1322 // The lower two bits are always zero and as such are not encoded.
1323 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
1324 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1325 // Special case for #-0
1326 if (Val == INT32_MIN) Val = 0;
1327 if (Val < 0) Val = -Val;
1328 Val = ARM_AM::getAM5Opc(AddSub, Val);
1329 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1330 Inst.addOperand(MCOperand::CreateImm(Val));
1333 void addMemImm8s4OffsetOperands(MCInst &Inst, unsigned N) const {
1334 assert(N == 2 && "Invalid number of operands!");
1335 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1336 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1337 Inst.addOperand(MCOperand::CreateImm(Val));
1340 void addMemImm0_1020s4OffsetOperands(MCInst &Inst, unsigned N) const {
1341 assert(N == 2 && "Invalid number of operands!");
1342 // The lower two bits are always zero and as such are not encoded.
1343 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
1344 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1345 Inst.addOperand(MCOperand::CreateImm(Val));
1348 void addMemImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1349 assert(N == 2 && "Invalid number of operands!");
1350 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1351 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1352 Inst.addOperand(MCOperand::CreateImm(Val));
1355 void addMemPosImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1356 addMemImm8OffsetOperands(Inst, N);
1359 void addMemNegImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1360 addMemImm8OffsetOperands(Inst, N);
1363 void addMemUImm12OffsetOperands(MCInst &Inst, unsigned N) const {
1364 assert(N == 2 && "Invalid number of operands!");
1365 // If this is an immediate, it's a label reference.
1366 if (Kind == k_Immediate) {
1367 addExpr(Inst, getImm());
1368 Inst.addOperand(MCOperand::CreateImm(0));
1372 // Otherwise, it's a normal memory reg+offset.
1373 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1374 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1375 Inst.addOperand(MCOperand::CreateImm(Val));
1378 void addMemImm12OffsetOperands(MCInst &Inst, unsigned N) const {
1379 assert(N == 2 && "Invalid number of operands!");
1380 // If this is an immediate, it's a label reference.
1381 if (Kind == k_Immediate) {
1382 addExpr(Inst, getImm());
1383 Inst.addOperand(MCOperand::CreateImm(0));
1387 // Otherwise, it's a normal memory reg+offset.
1388 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1389 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1390 Inst.addOperand(MCOperand::CreateImm(Val));
1393 void addMemTBBOperands(MCInst &Inst, unsigned N) const {
1394 assert(N == 2 && "Invalid number of operands!");
1395 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1396 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1399 void addMemTBHOperands(MCInst &Inst, unsigned N) const {
1400 assert(N == 2 && "Invalid number of operands!");
1401 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1402 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1405 void addMemRegOffsetOperands(MCInst &Inst, unsigned N) const {
1406 assert(N == 3 && "Invalid number of operands!");
1407 unsigned Val = ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
1408 Memory.ShiftImm, Memory.ShiftType);
1409 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1410 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1411 Inst.addOperand(MCOperand::CreateImm(Val));
1414 void addT2MemRegOffsetOperands(MCInst &Inst, unsigned N) const {
1415 assert(N == 3 && "Invalid number of operands!");
1416 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1417 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1418 Inst.addOperand(MCOperand::CreateImm(Memory.ShiftImm));
1421 void addMemThumbRROperands(MCInst &Inst, unsigned N) const {
1422 assert(N == 2 && "Invalid number of operands!");
1423 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1424 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1427 void addMemThumbRIs4Operands(MCInst &Inst, unsigned N) const {
1428 assert(N == 2 && "Invalid number of operands!");
1429 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
1430 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1431 Inst.addOperand(MCOperand::CreateImm(Val));
1434 void addMemThumbRIs2Operands(MCInst &Inst, unsigned N) const {
1435 assert(N == 2 && "Invalid number of operands!");
1436 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 2) : 0;
1437 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1438 Inst.addOperand(MCOperand::CreateImm(Val));
1441 void addMemThumbRIs1Operands(MCInst &Inst, unsigned N) const {
1442 assert(N == 2 && "Invalid number of operands!");
1443 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue()) : 0;
1444 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1445 Inst.addOperand(MCOperand::CreateImm(Val));
1448 void addMemThumbSPIOperands(MCInst &Inst, unsigned N) const {
1449 assert(N == 2 && "Invalid number of operands!");
1450 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
1451 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1452 Inst.addOperand(MCOperand::CreateImm(Val));
1455 void addPostIdxImm8Operands(MCInst &Inst, unsigned N) const {
1456 assert(N == 1 && "Invalid number of operands!");
1457 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1458 assert(CE && "non-constant post-idx-imm8 operand!");
1459 int Imm = CE->getValue();
1460 bool isAdd = Imm >= 0;
1461 if (Imm == INT32_MIN) Imm = 0;
1462 Imm = (Imm < 0 ? -Imm : Imm) | (int)isAdd << 8;
1463 Inst.addOperand(MCOperand::CreateImm(Imm));
1466 void addPostIdxImm8s4Operands(MCInst &Inst, unsigned N) const {
1467 assert(N == 1 && "Invalid number of operands!");
1468 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1469 assert(CE && "non-constant post-idx-imm8s4 operand!");
1470 int Imm = CE->getValue();
1471 bool isAdd = Imm >= 0;
1472 if (Imm == INT32_MIN) Imm = 0;
1473 // Immediate is scaled by 4.
1474 Imm = ((Imm < 0 ? -Imm : Imm) / 4) | (int)isAdd << 8;
1475 Inst.addOperand(MCOperand::CreateImm(Imm));
1478 void addPostIdxRegOperands(MCInst &Inst, unsigned N) const {
1479 assert(N == 2 && "Invalid number of operands!");
1480 Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1481 Inst.addOperand(MCOperand::CreateImm(PostIdxReg.isAdd));
1484 void addPostIdxRegShiftedOperands(MCInst &Inst, unsigned N) const {
1485 assert(N == 2 && "Invalid number of operands!");
1486 Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1487 // The sign, shift type, and shift amount are encoded in a single operand
1488 // using the AM2 encoding helpers.
1489 ARM_AM::AddrOpc opc = PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub;
1490 unsigned Imm = ARM_AM::getAM2Opc(opc, PostIdxReg.ShiftImm,
1491 PostIdxReg.ShiftTy);
1492 Inst.addOperand(MCOperand::CreateImm(Imm));
1495 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
1496 assert(N == 1 && "Invalid number of operands!");
1497 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
1500 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
1501 assert(N == 1 && "Invalid number of operands!");
1502 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
1505 void addVecListOneDOperands(MCInst &Inst, unsigned N) const {
1506 assert(N == 1 && "Invalid number of operands!");
1507 Inst.addOperand(MCOperand::CreateReg(VectorList.RegNum));
1510 void addVectorIndex8Operands(MCInst &Inst, unsigned N) const {
1511 assert(N == 1 && "Invalid number of operands!");
1512 Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1515 void addVectorIndex16Operands(MCInst &Inst, unsigned N) const {
1516 assert(N == 1 && "Invalid number of operands!");
1517 Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1520 void addVectorIndex32Operands(MCInst &Inst, unsigned N) const {
1521 assert(N == 1 && "Invalid number of operands!");
1522 Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1525 void addNEONi8splatOperands(MCInst &Inst, unsigned N) const {
1526 assert(N == 1 && "Invalid number of operands!");
1527 // The immediate encodes the type of constant as well as the value.
1528 // Mask in that this is an i8 splat.
1529 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1530 Inst.addOperand(MCOperand::CreateImm(CE->getValue() | 0xe00));
1533 void addNEONi16splatOperands(MCInst &Inst, unsigned N) const {
1534 assert(N == 1 && "Invalid number of operands!");
1535 // The immediate encodes the type of constant as well as the value.
1536 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1537 unsigned Value = CE->getValue();
1539 Value = (Value >> 8) | 0xa00;
1542 Inst.addOperand(MCOperand::CreateImm(Value));
1545 void addNEONi32splatOperands(MCInst &Inst, unsigned N) const {
1546 assert(N == 1 && "Invalid number of operands!");
1547 // The immediate encodes the type of constant as well as the value.
1548 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1549 unsigned Value = CE->getValue();
1550 if (Value >= 256 && Value <= 0xff00)
1551 Value = (Value >> 8) | 0x200;
1552 else if (Value > 0xffff && Value <= 0xff0000)
1553 Value = (Value >> 16) | 0x400;
1554 else if (Value > 0xffffff)
1555 Value = (Value >> 24) | 0x600;
1556 Inst.addOperand(MCOperand::CreateImm(Value));
1559 void addNEONi32vmovOperands(MCInst &Inst, unsigned N) const {
1560 assert(N == 1 && "Invalid number of operands!");
1561 // The immediate encodes the type of constant as well as the value.
1562 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1563 unsigned Value = CE->getValue();
1564 if (Value >= 256 && Value <= 0xffff)
1565 Value = (Value >> 8) | ((Value & 0xff) ? 0xc00 : 0x200);
1566 else if (Value > 0xffff && Value <= 0xffffff)
1567 Value = (Value >> 16) | ((Value & 0xff) ? 0xd00 : 0x400);
1568 else if (Value > 0xffffff)
1569 Value = (Value >> 24) | 0x600;
1570 Inst.addOperand(MCOperand::CreateImm(Value));
1573 void addNEONi64splatOperands(MCInst &Inst, unsigned N) const {
1574 assert(N == 1 && "Invalid number of operands!");
1575 // The immediate encodes the type of constant as well as the value.
1576 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1577 uint64_t Value = CE->getValue();
1579 for (unsigned i = 0; i < 8; ++i, Value >>= 8) {
1580 Imm |= (Value & 1) << i;
1582 Inst.addOperand(MCOperand::CreateImm(Imm | 0x1e00));
1585 virtual void print(raw_ostream &OS) const;
1587 static ARMOperand *CreateITMask(unsigned Mask, SMLoc S) {
1588 ARMOperand *Op = new ARMOperand(k_ITCondMask);
1589 Op->ITMask.Mask = Mask;
1595 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
1596 ARMOperand *Op = new ARMOperand(k_CondCode);
1603 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
1604 ARMOperand *Op = new ARMOperand(k_CoprocNum);
1605 Op->Cop.Val = CopVal;
1611 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
1612 ARMOperand *Op = new ARMOperand(k_CoprocReg);
1613 Op->Cop.Val = CopVal;
1619 static ARMOperand *CreateCoprocOption(unsigned Val, SMLoc S, SMLoc E) {
1620 ARMOperand *Op = new ARMOperand(k_CoprocOption);
1627 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
1628 ARMOperand *Op = new ARMOperand(k_CCOut);
1629 Op->Reg.RegNum = RegNum;
1635 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
1636 ARMOperand *Op = new ARMOperand(k_Token);
1637 Op->Tok.Data = Str.data();
1638 Op->Tok.Length = Str.size();
1644 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
1645 ARMOperand *Op = new ARMOperand(k_Register);
1646 Op->Reg.RegNum = RegNum;
1652 static ARMOperand *CreateShiftedRegister(ARM_AM::ShiftOpc ShTy,
1657 ARMOperand *Op = new ARMOperand(k_ShiftedRegister);
1658 Op->RegShiftedReg.ShiftTy = ShTy;
1659 Op->RegShiftedReg.SrcReg = SrcReg;
1660 Op->RegShiftedReg.ShiftReg = ShiftReg;
1661 Op->RegShiftedReg.ShiftImm = ShiftImm;
1667 static ARMOperand *CreateShiftedImmediate(ARM_AM::ShiftOpc ShTy,
1671 ARMOperand *Op = new ARMOperand(k_ShiftedImmediate);
1672 Op->RegShiftedImm.ShiftTy = ShTy;
1673 Op->RegShiftedImm.SrcReg = SrcReg;
1674 Op->RegShiftedImm.ShiftImm = ShiftImm;
1680 static ARMOperand *CreateShifterImm(bool isASR, unsigned Imm,
1682 ARMOperand *Op = new ARMOperand(k_ShifterImmediate);
1683 Op->ShifterImm.isASR = isASR;
1684 Op->ShifterImm.Imm = Imm;
1690 static ARMOperand *CreateRotImm(unsigned Imm, SMLoc S, SMLoc E) {
1691 ARMOperand *Op = new ARMOperand(k_RotateImmediate);
1692 Op->RotImm.Imm = Imm;
1698 static ARMOperand *CreateBitfield(unsigned LSB, unsigned Width,
1700 ARMOperand *Op = new ARMOperand(k_BitfieldDescriptor);
1701 Op->Bitfield.LSB = LSB;
1702 Op->Bitfield.Width = Width;
1709 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
1710 SMLoc StartLoc, SMLoc EndLoc) {
1711 KindTy Kind = k_RegisterList;
1713 if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Regs.front().first))
1714 Kind = k_DPRRegisterList;
1715 else if (ARMMCRegisterClasses[ARM::SPRRegClassID].
1716 contains(Regs.front().first))
1717 Kind = k_SPRRegisterList;
1719 ARMOperand *Op = new ARMOperand(Kind);
1720 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
1721 I = Regs.begin(), E = Regs.end(); I != E; ++I)
1722 Op->Registers.push_back(I->first);
1723 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
1724 Op->StartLoc = StartLoc;
1725 Op->EndLoc = EndLoc;
1729 static ARMOperand *CreateVectorList(unsigned RegNum, unsigned Count,
1731 ARMOperand *Op = new ARMOperand(k_VectorList);
1732 Op->VectorList.RegNum = RegNum;
1733 Op->VectorList.Count = Count;
1739 static ARMOperand *CreateVectorIndex(unsigned Idx, SMLoc S, SMLoc E,
1741 ARMOperand *Op = new ARMOperand(k_VectorIndex);
1742 Op->VectorIndex.Val = Idx;
1748 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
1749 ARMOperand *Op = new ARMOperand(k_Immediate);
1756 static ARMOperand *CreateFPImm(unsigned Val, SMLoc S, MCContext &Ctx) {
1757 ARMOperand *Op = new ARMOperand(k_FPImmediate);
1758 Op->FPImm.Val = Val;
1764 static ARMOperand *CreateMem(unsigned BaseRegNum,
1765 const MCConstantExpr *OffsetImm,
1766 unsigned OffsetRegNum,
1767 ARM_AM::ShiftOpc ShiftType,
1772 ARMOperand *Op = new ARMOperand(k_Memory);
1773 Op->Memory.BaseRegNum = BaseRegNum;
1774 Op->Memory.OffsetImm = OffsetImm;
1775 Op->Memory.OffsetRegNum = OffsetRegNum;
1776 Op->Memory.ShiftType = ShiftType;
1777 Op->Memory.ShiftImm = ShiftImm;
1778 Op->Memory.Alignment = Alignment;
1779 Op->Memory.isNegative = isNegative;
1785 static ARMOperand *CreatePostIdxReg(unsigned RegNum, bool isAdd,
1786 ARM_AM::ShiftOpc ShiftTy,
1789 ARMOperand *Op = new ARMOperand(k_PostIndexRegister);
1790 Op->PostIdxReg.RegNum = RegNum;
1791 Op->PostIdxReg.isAdd = isAdd;
1792 Op->PostIdxReg.ShiftTy = ShiftTy;
1793 Op->PostIdxReg.ShiftImm = ShiftImm;
1799 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
1800 ARMOperand *Op = new ARMOperand(k_MemBarrierOpt);
1801 Op->MBOpt.Val = Opt;
1807 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
1808 ARMOperand *Op = new ARMOperand(k_ProcIFlags);
1809 Op->IFlags.Val = IFlags;
1815 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
1816 ARMOperand *Op = new ARMOperand(k_MSRMask);
1817 Op->MMask.Val = MMask;
1824 } // end anonymous namespace.
1826 void ARMOperand::print(raw_ostream &OS) const {
1829 OS << "<fpimm " << getFPImm() << "(" << ARM_AM::getFPImmFloat(getFPImm())
1833 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
1836 OS << "<ccout " << getReg() << ">";
1838 case k_ITCondMask: {
1839 static char MaskStr[][6] = { "()", "(t)", "(e)", "(tt)", "(et)", "(te)",
1840 "(ee)", "(ttt)", "(ett)", "(tet)", "(eet)", "(tte)", "(ete)",
1842 assert((ITMask.Mask & 0xf) == ITMask.Mask);
1843 OS << "<it-mask " << MaskStr[ITMask.Mask] << ">";
1847 OS << "<coprocessor number: " << getCoproc() << ">";
1850 OS << "<coprocessor register: " << getCoproc() << ">";
1852 case k_CoprocOption:
1853 OS << "<coprocessor option: " << CoprocOption.Val << ">";
1856 OS << "<mask: " << getMSRMask() << ">";
1859 getImm()->print(OS);
1861 case k_MemBarrierOpt:
1862 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
1866 << " base:" << Memory.BaseRegNum;
1869 case k_PostIndexRegister:
1870 OS << "post-idx register " << (PostIdxReg.isAdd ? "" : "-")
1871 << PostIdxReg.RegNum;
1872 if (PostIdxReg.ShiftTy != ARM_AM::no_shift)
1873 OS << ARM_AM::getShiftOpcStr(PostIdxReg.ShiftTy) << " "
1874 << PostIdxReg.ShiftImm;
1877 case k_ProcIFlags: {
1878 OS << "<ARM_PROC::";
1879 unsigned IFlags = getProcIFlags();
1880 for (int i=2; i >= 0; --i)
1881 if (IFlags & (1 << i))
1882 OS << ARM_PROC::IFlagsToString(1 << i);
1887 OS << "<register " << getReg() << ">";
1889 case k_ShifterImmediate:
1890 OS << "<shift " << (ShifterImm.isASR ? "asr" : "lsl")
1891 << " #" << ShifterImm.Imm << ">";
1893 case k_ShiftedRegister:
1894 OS << "<so_reg_reg "
1895 << RegShiftedReg.SrcReg
1896 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(RegShiftedReg.ShiftImm))
1897 << ", " << RegShiftedReg.ShiftReg << ", "
1898 << ARM_AM::getSORegOffset(RegShiftedReg.ShiftImm)
1901 case k_ShiftedImmediate:
1902 OS << "<so_reg_imm "
1903 << RegShiftedImm.SrcReg
1904 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(RegShiftedImm.ShiftImm))
1905 << ", " << ARM_AM::getSORegOffset(RegShiftedImm.ShiftImm)
1908 case k_RotateImmediate:
1909 OS << "<ror " << " #" << (RotImm.Imm * 8) << ">";
1911 case k_BitfieldDescriptor:
1912 OS << "<bitfield " << "lsb: " << Bitfield.LSB
1913 << ", width: " << Bitfield.Width << ">";
1915 case k_RegisterList:
1916 case k_DPRRegisterList:
1917 case k_SPRRegisterList: {
1918 OS << "<register_list ";
1920 const SmallVectorImpl<unsigned> &RegList = getRegList();
1921 for (SmallVectorImpl<unsigned>::const_iterator
1922 I = RegList.begin(), E = RegList.end(); I != E; ) {
1924 if (++I < E) OS << ", ";
1931 OS << "<vector_list " << VectorList.Count << " * "
1932 << VectorList.RegNum << ">";
1935 OS << "'" << getToken() << "'";
1938 OS << "<vectorindex " << getVectorIndex() << ">";
1943 /// @name Auto-generated Match Functions
1946 static unsigned MatchRegisterName(StringRef Name);
1950 bool ARMAsmParser::ParseRegister(unsigned &RegNo,
1951 SMLoc &StartLoc, SMLoc &EndLoc) {
1952 RegNo = tryParseRegister();
1954 return (RegNo == (unsigned)-1);
1957 /// Try to parse a register name. The token must be an Identifier when called,
1958 /// and if it is a register name the token is eaten and the register number is
1959 /// returned. Otherwise return -1.
1961 int ARMAsmParser::tryParseRegister() {
1962 const AsmToken &Tok = Parser.getTok();
1963 if (Tok.isNot(AsmToken::Identifier)) return -1;
1965 // FIXME: Validate register for the current architecture; we have to do
1966 // validation later, so maybe there is no need for this here.
1967 std::string upperCase = Tok.getString().str();
1968 std::string lowerCase = LowercaseString(upperCase);
1969 unsigned RegNum = MatchRegisterName(lowerCase);
1971 RegNum = StringSwitch<unsigned>(lowerCase)
1972 .Case("r13", ARM::SP)
1973 .Case("r14", ARM::LR)
1974 .Case("r15", ARM::PC)
1975 .Case("ip", ARM::R12)
1978 if (!RegNum) return -1;
1980 Parser.Lex(); // Eat identifier token.
1985 // Try to parse a shifter (e.g., "lsl <amt>"). On success, return 0.
1986 // If a recoverable error occurs, return 1. If an irrecoverable error
1987 // occurs, return -1. An irrecoverable error is one where tokens have been
1988 // consumed in the process of trying to parse the shifter (i.e., when it is
1989 // indeed a shifter operand, but malformed).
1990 int ARMAsmParser::tryParseShiftRegister(
1991 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1992 SMLoc S = Parser.getTok().getLoc();
1993 const AsmToken &Tok = Parser.getTok();
1994 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1996 std::string upperCase = Tok.getString().str();
1997 std::string lowerCase = LowercaseString(upperCase);
1998 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
1999 .Case("lsl", ARM_AM::lsl)
2000 .Case("lsr", ARM_AM::lsr)
2001 .Case("asr", ARM_AM::asr)
2002 .Case("ror", ARM_AM::ror)
2003 .Case("rrx", ARM_AM::rrx)
2004 .Default(ARM_AM::no_shift);
2006 if (ShiftTy == ARM_AM::no_shift)
2009 Parser.Lex(); // Eat the operator.
2011 // The source register for the shift has already been added to the
2012 // operand list, so we need to pop it off and combine it into the shifted
2013 // register operand instead.
2014 OwningPtr<ARMOperand> PrevOp((ARMOperand*)Operands.pop_back_val());
2015 if (!PrevOp->isReg())
2016 return Error(PrevOp->getStartLoc(), "shift must be of a register");
2017 int SrcReg = PrevOp->getReg();
2020 if (ShiftTy == ARM_AM::rrx) {
2021 // RRX Doesn't have an explicit shift amount. The encoder expects
2022 // the shift register to be the same as the source register. Seems odd,
2026 // Figure out if this is shifted by a constant or a register (for non-RRX).
2027 if (Parser.getTok().is(AsmToken::Hash)) {
2028 Parser.Lex(); // Eat hash.
2029 SMLoc ImmLoc = Parser.getTok().getLoc();
2030 const MCExpr *ShiftExpr = 0;
2031 if (getParser().ParseExpression(ShiftExpr)) {
2032 Error(ImmLoc, "invalid immediate shift value");
2035 // The expression must be evaluatable as an immediate.
2036 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
2038 Error(ImmLoc, "invalid immediate shift value");
2041 // Range check the immediate.
2042 // lsl, ror: 0 <= imm <= 31
2043 // lsr, asr: 0 <= imm <= 32
2044 Imm = CE->getValue();
2046 ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
2047 ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
2048 Error(ImmLoc, "immediate shift value out of range");
2051 } else if (Parser.getTok().is(AsmToken::Identifier)) {
2052 ShiftReg = tryParseRegister();
2053 SMLoc L = Parser.getTok().getLoc();
2054 if (ShiftReg == -1) {
2055 Error (L, "expected immediate or register in shift operand");
2059 Error (Parser.getTok().getLoc(),
2060 "expected immediate or register in shift operand");
2065 if (ShiftReg && ShiftTy != ARM_AM::rrx)
2066 Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
2068 S, Parser.getTok().getLoc()));
2070 Operands.push_back(ARMOperand::CreateShiftedImmediate(ShiftTy, SrcReg, Imm,
2071 S, Parser.getTok().getLoc()));
2077 /// Try to parse a register name. The token must be an Identifier when called.
2078 /// If it's a register, an AsmOperand is created. Another AsmOperand is created
2079 /// if there is a "writeback". 'true' if it's not a register.
2081 /// TODO this is likely to change to allow different register types and or to
2082 /// parse for a specific register type.
2084 tryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2085 SMLoc S = Parser.getTok().getLoc();
2086 int RegNo = tryParseRegister();
2090 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
2092 const AsmToken &ExclaimTok = Parser.getTok();
2093 if (ExclaimTok.is(AsmToken::Exclaim)) {
2094 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
2095 ExclaimTok.getLoc()));
2096 Parser.Lex(); // Eat exclaim token
2100 // Also check for an index operand. This is only legal for vector registers,
2101 // but that'll get caught OK in operand matching, so we don't need to
2102 // explicitly filter everything else out here.
2103 if (Parser.getTok().is(AsmToken::LBrac)) {
2104 SMLoc SIdx = Parser.getTok().getLoc();
2105 Parser.Lex(); // Eat left bracket token.
2107 const MCExpr *ImmVal;
2108 if (getParser().ParseExpression(ImmVal))
2109 return MatchOperand_ParseFail;
2110 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(ImmVal);
2112 TokError("immediate value expected for vector index");
2113 return MatchOperand_ParseFail;
2116 SMLoc E = Parser.getTok().getLoc();
2117 if (Parser.getTok().isNot(AsmToken::RBrac)) {
2118 Error(E, "']' expected");
2119 return MatchOperand_ParseFail;
2122 Parser.Lex(); // Eat right bracket token.
2124 Operands.push_back(ARMOperand::CreateVectorIndex(MCE->getValue(),
2132 /// MatchCoprocessorOperandName - Try to parse an coprocessor related
2133 /// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
2135 static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
2136 // Use the same layout as the tablegen'erated register name matcher. Ugly,
2138 switch (Name.size()) {
2141 if (Name[0] != CoprocOp)
2158 if (Name[0] != CoprocOp || Name[1] != '1')
2162 case '0': return 10;
2163 case '1': return 11;
2164 case '2': return 12;
2165 case '3': return 13;
2166 case '4': return 14;
2167 case '5': return 15;
2175 /// parseITCondCode - Try to parse a condition code for an IT instruction.
2176 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2177 parseITCondCode(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2178 SMLoc S = Parser.getTok().getLoc();
2179 const AsmToken &Tok = Parser.getTok();
2180 if (!Tok.is(AsmToken::Identifier))
2181 return MatchOperand_NoMatch;
2182 unsigned CC = StringSwitch<unsigned>(Tok.getString())
2183 .Case("eq", ARMCC::EQ)
2184 .Case("ne", ARMCC::NE)
2185 .Case("hs", ARMCC::HS)
2186 .Case("cs", ARMCC::HS)
2187 .Case("lo", ARMCC::LO)
2188 .Case("cc", ARMCC::LO)
2189 .Case("mi", ARMCC::MI)
2190 .Case("pl", ARMCC::PL)
2191 .Case("vs", ARMCC::VS)
2192 .Case("vc", ARMCC::VC)
2193 .Case("hi", ARMCC::HI)
2194 .Case("ls", ARMCC::LS)
2195 .Case("ge", ARMCC::GE)
2196 .Case("lt", ARMCC::LT)
2197 .Case("gt", ARMCC::GT)
2198 .Case("le", ARMCC::LE)
2199 .Case("al", ARMCC::AL)
2202 return MatchOperand_NoMatch;
2203 Parser.Lex(); // Eat the token.
2205 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), S));
2207 return MatchOperand_Success;
2210 /// parseCoprocNumOperand - Try to parse an coprocessor number operand. The
2211 /// token must be an Identifier when called, and if it is a coprocessor
2212 /// number, the token is eaten and the operand is added to the operand list.
2213 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2214 parseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2215 SMLoc S = Parser.getTok().getLoc();
2216 const AsmToken &Tok = Parser.getTok();
2217 if (Tok.isNot(AsmToken::Identifier))
2218 return MatchOperand_NoMatch;
2220 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
2222 return MatchOperand_NoMatch;
2224 Parser.Lex(); // Eat identifier token.
2225 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
2226 return MatchOperand_Success;
2229 /// parseCoprocRegOperand - Try to parse an coprocessor register operand. The
2230 /// token must be an Identifier when called, and if it is a coprocessor
2231 /// number, the token is eaten and the operand is added to the operand list.
2232 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2233 parseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2234 SMLoc S = Parser.getTok().getLoc();
2235 const AsmToken &Tok = Parser.getTok();
2236 if (Tok.isNot(AsmToken::Identifier))
2237 return MatchOperand_NoMatch;
2239 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
2241 return MatchOperand_NoMatch;
2243 Parser.Lex(); // Eat identifier token.
2244 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
2245 return MatchOperand_Success;
2248 /// parseCoprocOptionOperand - Try to parse an coprocessor option operand.
2249 /// coproc_option : '{' imm0_255 '}'
2250 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2251 parseCoprocOptionOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2252 SMLoc S = Parser.getTok().getLoc();
2254 // If this isn't a '{', this isn't a coprocessor immediate operand.
2255 if (Parser.getTok().isNot(AsmToken::LCurly))
2256 return MatchOperand_NoMatch;
2257 Parser.Lex(); // Eat the '{'
2260 SMLoc Loc = Parser.getTok().getLoc();
2261 if (getParser().ParseExpression(Expr)) {
2262 Error(Loc, "illegal expression");
2263 return MatchOperand_ParseFail;
2265 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
2266 if (!CE || CE->getValue() < 0 || CE->getValue() > 255) {
2267 Error(Loc, "coprocessor option must be an immediate in range [0, 255]");
2268 return MatchOperand_ParseFail;
2270 int Val = CE->getValue();
2272 // Check for and consume the closing '}'
2273 if (Parser.getTok().isNot(AsmToken::RCurly))
2274 return MatchOperand_ParseFail;
2275 SMLoc E = Parser.getTok().getLoc();
2276 Parser.Lex(); // Eat the '}'
2278 Operands.push_back(ARMOperand::CreateCoprocOption(Val, S, E));
2279 return MatchOperand_Success;
2282 // For register list parsing, we need to map from raw GPR register numbering
2283 // to the enumeration values. The enumeration values aren't sorted by
2284 // register number due to our using "sp", "lr" and "pc" as canonical names.
2285 static unsigned getNextRegister(unsigned Reg) {
2286 // If this is a GPR, we need to do it manually, otherwise we can rely
2287 // on the sort ordering of the enumeration since the other reg-classes
2289 if (!ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
2292 default: assert(0 && "Invalid GPR number!");
2293 case ARM::R0: return ARM::R1; case ARM::R1: return ARM::R2;
2294 case ARM::R2: return ARM::R3; case ARM::R3: return ARM::R4;
2295 case ARM::R4: return ARM::R5; case ARM::R5: return ARM::R6;
2296 case ARM::R6: return ARM::R7; case ARM::R7: return ARM::R8;
2297 case ARM::R8: return ARM::R9; case ARM::R9: return ARM::R10;
2298 case ARM::R10: return ARM::R11; case ARM::R11: return ARM::R12;
2299 case ARM::R12: return ARM::SP; case ARM::SP: return ARM::LR;
2300 case ARM::LR: return ARM::PC; case ARM::PC: return ARM::R0;
2304 /// Parse a register list.
2306 parseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2307 assert(Parser.getTok().is(AsmToken::LCurly) &&
2308 "Token is not a Left Curly Brace");
2309 SMLoc S = Parser.getTok().getLoc();
2310 Parser.Lex(); // Eat '{' token.
2311 SMLoc RegLoc = Parser.getTok().getLoc();
2313 // Check the first register in the list to see what register class
2314 // this is a list of.
2315 int Reg = tryParseRegister();
2317 return Error(RegLoc, "register expected");
2319 MCRegisterClass *RC;
2320 if (ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
2321 RC = &ARMMCRegisterClasses[ARM::GPRRegClassID];
2322 else if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg))
2323 RC = &ARMMCRegisterClasses[ARM::DPRRegClassID];
2324 else if (ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg))
2325 RC = &ARMMCRegisterClasses[ARM::SPRRegClassID];
2327 return Error(RegLoc, "invalid register in register list");
2329 // The reglist instructions have at most 16 registers, so reserve
2330 // space for that many.
2331 SmallVector<std::pair<unsigned, SMLoc>, 16> Registers;
2332 // Store the first register.
2333 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2335 // This starts immediately after the first register token in the list,
2336 // so we can see either a comma or a minus (range separator) as a legal
2338 while (Parser.getTok().is(AsmToken::Comma) ||
2339 Parser.getTok().is(AsmToken::Minus)) {
2340 if (Parser.getTok().is(AsmToken::Minus)) {
2341 Parser.Lex(); // Eat the comma.
2342 SMLoc EndLoc = Parser.getTok().getLoc();
2343 int EndReg = tryParseRegister();
2345 return Error(EndLoc, "register expected");
2346 // If the register is the same as the start reg, there's nothing
2350 // The register must be in the same register class as the first.
2351 if (!RC->contains(EndReg))
2352 return Error(EndLoc, "invalid register in register list");
2353 // Ranges must go from low to high.
2354 if (getARMRegisterNumbering(Reg) > getARMRegisterNumbering(EndReg))
2355 return Error(EndLoc, "bad range in register list");
2357 // Add all the registers in the range to the register list.
2358 while (Reg != EndReg) {
2359 Reg = getNextRegister(Reg);
2360 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2364 Parser.Lex(); // Eat the comma.
2365 RegLoc = Parser.getTok().getLoc();
2367 Reg = tryParseRegister();
2369 return Error(RegLoc, "register expected");
2370 // The register must be in the same register class as the first.
2371 if (!RC->contains(Reg))
2372 return Error(RegLoc, "invalid register in register list");
2373 // List must be monotonically increasing.
2374 if (getARMRegisterNumbering(Reg) <= getARMRegisterNumbering(OldReg))
2375 return Error(RegLoc, "register list not in ascending order");
2376 // VFP register lists must also be contiguous.
2377 // It's OK to use the enumeration values directly here rather, as the
2378 // VFP register classes have the enum sorted properly.
2379 if (RC != &ARMMCRegisterClasses[ARM::GPRRegClassID] &&
2381 return Error(RegLoc, "non-contiguous register range");
2382 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2385 SMLoc E = Parser.getTok().getLoc();
2386 if (Parser.getTok().isNot(AsmToken::RCurly))
2387 return Error(E, "'}' expected");
2388 Parser.Lex(); // Eat '}' token.
2390 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
2394 // parse a vector register list
2395 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2396 parseVectorList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2397 if(Parser.getTok().isNot(AsmToken::LCurly))
2398 return MatchOperand_NoMatch;
2400 SMLoc S = Parser.getTok().getLoc();
2401 Parser.Lex(); // Eat '{' token.
2402 SMLoc RegLoc = Parser.getTok().getLoc();
2404 int Reg = tryParseRegister();
2406 Error(RegLoc, "register expected");
2407 return MatchOperand_ParseFail;
2410 unsigned FirstReg = Reg;
2412 while (Parser.getTok().is(AsmToken::Comma)) {
2413 Parser.Lex(); // Eat the comma.
2414 RegLoc = Parser.getTok().getLoc();
2416 Reg = tryParseRegister();
2418 Error(RegLoc, "register expected");
2419 return MatchOperand_ParseFail;
2421 // vector register lists must also be contiguous.
2422 // It's OK to use the enumeration values directly here rather, as the
2423 // VFP register classes have the enum sorted properly.
2424 if (Reg != OldReg + 1) {
2425 Error(RegLoc, "non-contiguous register range");
2426 return MatchOperand_ParseFail;
2432 SMLoc E = Parser.getTok().getLoc();
2433 if (Parser.getTok().isNot(AsmToken::RCurly)) {
2434 Error(E, "'}' expected");
2435 return MatchOperand_ParseFail;
2437 Parser.Lex(); // Eat '}' token.
2439 Operands.push_back(ARMOperand::CreateVectorList(FirstReg, Count, S, E));
2440 return MatchOperand_Success;
2443 /// parseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
2444 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2445 parseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2446 SMLoc S = Parser.getTok().getLoc();
2447 const AsmToken &Tok = Parser.getTok();
2448 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2449 StringRef OptStr = Tok.getString();
2451 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
2452 .Case("sy", ARM_MB::SY)
2453 .Case("st", ARM_MB::ST)
2454 .Case("sh", ARM_MB::ISH)
2455 .Case("ish", ARM_MB::ISH)
2456 .Case("shst", ARM_MB::ISHST)
2457 .Case("ishst", ARM_MB::ISHST)
2458 .Case("nsh", ARM_MB::NSH)
2459 .Case("un", ARM_MB::NSH)
2460 .Case("nshst", ARM_MB::NSHST)
2461 .Case("unst", ARM_MB::NSHST)
2462 .Case("osh", ARM_MB::OSH)
2463 .Case("oshst", ARM_MB::OSHST)
2467 return MatchOperand_NoMatch;
2469 Parser.Lex(); // Eat identifier token.
2470 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
2471 return MatchOperand_Success;
2474 /// parseProcIFlagsOperand - Try to parse iflags from CPS instruction.
2475 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2476 parseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2477 SMLoc S = Parser.getTok().getLoc();
2478 const AsmToken &Tok = Parser.getTok();
2479 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2480 StringRef IFlagsStr = Tok.getString();
2482 // An iflags string of "none" is interpreted to mean that none of the AIF
2483 // bits are set. Not a terribly useful instruction, but a valid encoding.
2484 unsigned IFlags = 0;
2485 if (IFlagsStr != "none") {
2486 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
2487 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
2488 .Case("a", ARM_PROC::A)
2489 .Case("i", ARM_PROC::I)
2490 .Case("f", ARM_PROC::F)
2493 // If some specific iflag is already set, it means that some letter is
2494 // present more than once, this is not acceptable.
2495 if (Flag == ~0U || (IFlags & Flag))
2496 return MatchOperand_NoMatch;
2502 Parser.Lex(); // Eat identifier token.
2503 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
2504 return MatchOperand_Success;
2507 /// parseMSRMaskOperand - Try to parse mask flags from MSR instruction.
2508 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2509 parseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2510 SMLoc S = Parser.getTok().getLoc();
2511 const AsmToken &Tok = Parser.getTok();
2512 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2513 StringRef Mask = Tok.getString();
2516 // See ARMv6-M 10.1.1
2517 unsigned FlagsVal = StringSwitch<unsigned>(Mask)
2527 .Case("primask", 16)
2528 .Case("basepri", 17)
2529 .Case("basepri_max", 18)
2530 .Case("faultmask", 19)
2531 .Case("control", 20)
2534 if (FlagsVal == ~0U)
2535 return MatchOperand_NoMatch;
2537 if (!hasV7Ops() && FlagsVal >= 17 && FlagsVal <= 19)
2538 // basepri, basepri_max and faultmask only valid for V7m.
2539 return MatchOperand_NoMatch;
2541 Parser.Lex(); // Eat identifier token.
2542 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
2543 return MatchOperand_Success;
2546 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
2547 size_t Start = 0, Next = Mask.find('_');
2548 StringRef Flags = "";
2549 std::string SpecReg = LowercaseString(Mask.slice(Start, Next));
2550 if (Next != StringRef::npos)
2551 Flags = Mask.slice(Next+1, Mask.size());
2553 // FlagsVal contains the complete mask:
2555 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
2556 unsigned FlagsVal = 0;
2558 if (SpecReg == "apsr") {
2559 FlagsVal = StringSwitch<unsigned>(Flags)
2560 .Case("nzcvq", 0x8) // same as CPSR_f
2561 .Case("g", 0x4) // same as CPSR_s
2562 .Case("nzcvqg", 0xc) // same as CPSR_fs
2565 if (FlagsVal == ~0U) {
2567 return MatchOperand_NoMatch;
2569 FlagsVal = 8; // No flag
2571 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
2572 if (Flags == "all") // cpsr_all is an alias for cpsr_fc
2574 for (int i = 0, e = Flags.size(); i != e; ++i) {
2575 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
2582 // If some specific flag is already set, it means that some letter is
2583 // present more than once, this is not acceptable.
2584 if (FlagsVal == ~0U || (FlagsVal & Flag))
2585 return MatchOperand_NoMatch;
2588 } else // No match for special register.
2589 return MatchOperand_NoMatch;
2591 // Special register without flags are equivalent to "fc" flags.
2595 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
2596 if (SpecReg == "spsr")
2599 Parser.Lex(); // Eat identifier token.
2600 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
2601 return MatchOperand_Success;
2604 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2605 parsePKHImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands, StringRef Op,
2606 int Low, int High) {
2607 const AsmToken &Tok = Parser.getTok();
2608 if (Tok.isNot(AsmToken::Identifier)) {
2609 Error(Parser.getTok().getLoc(), Op + " operand expected.");
2610 return MatchOperand_ParseFail;
2612 StringRef ShiftName = Tok.getString();
2613 std::string LowerOp = LowercaseString(Op);
2614 std::string UpperOp = UppercaseString(Op);
2615 if (ShiftName != LowerOp && ShiftName != UpperOp) {
2616 Error(Parser.getTok().getLoc(), Op + " operand expected.");
2617 return MatchOperand_ParseFail;
2619 Parser.Lex(); // Eat shift type token.
2621 // There must be a '#' and a shift amount.
2622 if (Parser.getTok().isNot(AsmToken::Hash)) {
2623 Error(Parser.getTok().getLoc(), "'#' expected");
2624 return MatchOperand_ParseFail;
2626 Parser.Lex(); // Eat hash token.
2628 const MCExpr *ShiftAmount;
2629 SMLoc Loc = Parser.getTok().getLoc();
2630 if (getParser().ParseExpression(ShiftAmount)) {
2631 Error(Loc, "illegal expression");
2632 return MatchOperand_ParseFail;
2634 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
2636 Error(Loc, "constant expression expected");
2637 return MatchOperand_ParseFail;
2639 int Val = CE->getValue();
2640 if (Val < Low || Val > High) {
2641 Error(Loc, "immediate value out of range");
2642 return MatchOperand_ParseFail;
2645 Operands.push_back(ARMOperand::CreateImm(CE, Loc, Parser.getTok().getLoc()));
2647 return MatchOperand_Success;
2650 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2651 parseSetEndImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2652 const AsmToken &Tok = Parser.getTok();
2653 SMLoc S = Tok.getLoc();
2654 if (Tok.isNot(AsmToken::Identifier)) {
2655 Error(Tok.getLoc(), "'be' or 'le' operand expected");
2656 return MatchOperand_ParseFail;
2658 int Val = StringSwitch<int>(Tok.getString())
2662 Parser.Lex(); // Eat the token.
2665 Error(Tok.getLoc(), "'be' or 'le' operand expected");
2666 return MatchOperand_ParseFail;
2668 Operands.push_back(ARMOperand::CreateImm(MCConstantExpr::Create(Val,
2670 S, Parser.getTok().getLoc()));
2671 return MatchOperand_Success;
2674 /// parseShifterImm - Parse the shifter immediate operand for SSAT/USAT
2675 /// instructions. Legal values are:
2676 /// lsl #n 'n' in [0,31]
2677 /// asr #n 'n' in [1,32]
2678 /// n == 32 encoded as n == 0.
2679 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2680 parseShifterImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2681 const AsmToken &Tok = Parser.getTok();
2682 SMLoc S = Tok.getLoc();
2683 if (Tok.isNot(AsmToken::Identifier)) {
2684 Error(S, "shift operator 'asr' or 'lsl' expected");
2685 return MatchOperand_ParseFail;
2687 StringRef ShiftName = Tok.getString();
2689 if (ShiftName == "lsl" || ShiftName == "LSL")
2691 else if (ShiftName == "asr" || ShiftName == "ASR")
2694 Error(S, "shift operator 'asr' or 'lsl' expected");
2695 return MatchOperand_ParseFail;
2697 Parser.Lex(); // Eat the operator.
2699 // A '#' and a shift amount.
2700 if (Parser.getTok().isNot(AsmToken::Hash)) {
2701 Error(Parser.getTok().getLoc(), "'#' expected");
2702 return MatchOperand_ParseFail;
2704 Parser.Lex(); // Eat hash token.
2706 const MCExpr *ShiftAmount;
2707 SMLoc E = Parser.getTok().getLoc();
2708 if (getParser().ParseExpression(ShiftAmount)) {
2709 Error(E, "malformed shift expression");
2710 return MatchOperand_ParseFail;
2712 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
2714 Error(E, "shift amount must be an immediate");
2715 return MatchOperand_ParseFail;
2718 int64_t Val = CE->getValue();
2720 // Shift amount must be in [1,32]
2721 if (Val < 1 || Val > 32) {
2722 Error(E, "'asr' shift amount must be in range [1,32]");
2723 return MatchOperand_ParseFail;
2725 // asr #32 encoded as asr #0, but is not allowed in Thumb2 mode.
2726 if (isThumb() && Val == 32) {
2727 Error(E, "'asr #32' shift amount not allowed in Thumb mode");
2728 return MatchOperand_ParseFail;
2730 if (Val == 32) Val = 0;
2732 // Shift amount must be in [1,32]
2733 if (Val < 0 || Val > 31) {
2734 Error(E, "'lsr' shift amount must be in range [0,31]");
2735 return MatchOperand_ParseFail;
2739 E = Parser.getTok().getLoc();
2740 Operands.push_back(ARMOperand::CreateShifterImm(isASR, Val, S, E));
2742 return MatchOperand_Success;
2745 /// parseRotImm - Parse the shifter immediate operand for SXTB/UXTB family
2746 /// of instructions. Legal values are:
2747 /// ror #n 'n' in {0, 8, 16, 24}
2748 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2749 parseRotImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2750 const AsmToken &Tok = Parser.getTok();
2751 SMLoc S = Tok.getLoc();
2752 if (Tok.isNot(AsmToken::Identifier))
2753 return MatchOperand_NoMatch;
2754 StringRef ShiftName = Tok.getString();
2755 if (ShiftName != "ror" && ShiftName != "ROR")
2756 return MatchOperand_NoMatch;
2757 Parser.Lex(); // Eat the operator.
2759 // A '#' and a rotate amount.
2760 if (Parser.getTok().isNot(AsmToken::Hash)) {
2761 Error(Parser.getTok().getLoc(), "'#' expected");
2762 return MatchOperand_ParseFail;
2764 Parser.Lex(); // Eat hash token.
2766 const MCExpr *ShiftAmount;
2767 SMLoc E = Parser.getTok().getLoc();
2768 if (getParser().ParseExpression(ShiftAmount)) {
2769 Error(E, "malformed rotate expression");
2770 return MatchOperand_ParseFail;
2772 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
2774 Error(E, "rotate amount must be an immediate");
2775 return MatchOperand_ParseFail;
2778 int64_t Val = CE->getValue();
2779 // Shift amount must be in {0, 8, 16, 24} (0 is undocumented extension)
2780 // normally, zero is represented in asm by omitting the rotate operand
2782 if (Val != 8 && Val != 16 && Val != 24 && Val != 0) {
2783 Error(E, "'ror' rotate amount must be 8, 16, or 24");
2784 return MatchOperand_ParseFail;
2787 E = Parser.getTok().getLoc();
2788 Operands.push_back(ARMOperand::CreateRotImm(Val, S, E));
2790 return MatchOperand_Success;
2793 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2794 parseBitfield(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2795 SMLoc S = Parser.getTok().getLoc();
2796 // The bitfield descriptor is really two operands, the LSB and the width.
2797 if (Parser.getTok().isNot(AsmToken::Hash)) {
2798 Error(Parser.getTok().getLoc(), "'#' expected");
2799 return MatchOperand_ParseFail;
2801 Parser.Lex(); // Eat hash token.
2803 const MCExpr *LSBExpr;
2804 SMLoc E = Parser.getTok().getLoc();
2805 if (getParser().ParseExpression(LSBExpr)) {
2806 Error(E, "malformed immediate expression");
2807 return MatchOperand_ParseFail;
2809 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(LSBExpr);
2811 Error(E, "'lsb' operand must be an immediate");
2812 return MatchOperand_ParseFail;
2815 int64_t LSB = CE->getValue();
2816 // The LSB must be in the range [0,31]
2817 if (LSB < 0 || LSB > 31) {
2818 Error(E, "'lsb' operand must be in the range [0,31]");
2819 return MatchOperand_ParseFail;
2821 E = Parser.getTok().getLoc();
2823 // Expect another immediate operand.
2824 if (Parser.getTok().isNot(AsmToken::Comma)) {
2825 Error(Parser.getTok().getLoc(), "too few operands");
2826 return MatchOperand_ParseFail;
2828 Parser.Lex(); // Eat hash token.
2829 if (Parser.getTok().isNot(AsmToken::Hash)) {
2830 Error(Parser.getTok().getLoc(), "'#' expected");
2831 return MatchOperand_ParseFail;
2833 Parser.Lex(); // Eat hash token.
2835 const MCExpr *WidthExpr;
2836 if (getParser().ParseExpression(WidthExpr)) {
2837 Error(E, "malformed immediate expression");
2838 return MatchOperand_ParseFail;
2840 CE = dyn_cast<MCConstantExpr>(WidthExpr);
2842 Error(E, "'width' operand must be an immediate");
2843 return MatchOperand_ParseFail;
2846 int64_t Width = CE->getValue();
2847 // The LSB must be in the range [1,32-lsb]
2848 if (Width < 1 || Width > 32 - LSB) {
2849 Error(E, "'width' operand must be in the range [1,32-lsb]");
2850 return MatchOperand_ParseFail;
2852 E = Parser.getTok().getLoc();
2854 Operands.push_back(ARMOperand::CreateBitfield(LSB, Width, S, E));
2856 return MatchOperand_Success;
2859 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2860 parsePostIdxReg(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2861 // Check for a post-index addressing register operand. Specifically:
2862 // postidx_reg := '+' register {, shift}
2863 // | '-' register {, shift}
2864 // | register {, shift}
2866 // This method must return MatchOperand_NoMatch without consuming any tokens
2867 // in the case where there is no match, as other alternatives take other
2869 AsmToken Tok = Parser.getTok();
2870 SMLoc S = Tok.getLoc();
2871 bool haveEaten = false;
2874 if (Tok.is(AsmToken::Plus)) {
2875 Parser.Lex(); // Eat the '+' token.
2877 } else if (Tok.is(AsmToken::Minus)) {
2878 Parser.Lex(); // Eat the '-' token.
2882 if (Parser.getTok().is(AsmToken::Identifier))
2883 Reg = tryParseRegister();
2886 return MatchOperand_NoMatch;
2887 Error(Parser.getTok().getLoc(), "register expected");
2888 return MatchOperand_ParseFail;
2890 SMLoc E = Parser.getTok().getLoc();
2892 ARM_AM::ShiftOpc ShiftTy = ARM_AM::no_shift;
2893 unsigned ShiftImm = 0;
2894 if (Parser.getTok().is(AsmToken::Comma)) {
2895 Parser.Lex(); // Eat the ','.
2896 if (parseMemRegOffsetShift(ShiftTy, ShiftImm))
2897 return MatchOperand_ParseFail;
2900 Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ShiftTy,
2903 return MatchOperand_Success;
2906 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2907 parseAM3Offset(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2908 // Check for a post-index addressing register operand. Specifically:
2909 // am3offset := '+' register
2916 // This method must return MatchOperand_NoMatch without consuming any tokens
2917 // in the case where there is no match, as other alternatives take other
2919 AsmToken Tok = Parser.getTok();
2920 SMLoc S = Tok.getLoc();
2922 // Do immediates first, as we always parse those if we have a '#'.
2923 if (Parser.getTok().is(AsmToken::Hash)) {
2924 Parser.Lex(); // Eat the '#'.
2925 // Explicitly look for a '-', as we need to encode negative zero
2927 bool isNegative = Parser.getTok().is(AsmToken::Minus);
2928 const MCExpr *Offset;
2929 if (getParser().ParseExpression(Offset))
2930 return MatchOperand_ParseFail;
2931 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
2933 Error(S, "constant expression expected");
2934 return MatchOperand_ParseFail;
2936 SMLoc E = Tok.getLoc();
2937 // Negative zero is encoded as the flag value INT32_MIN.
2938 int32_t Val = CE->getValue();
2939 if (isNegative && Val == 0)
2943 ARMOperand::CreateImm(MCConstantExpr::Create(Val, getContext()), S, E));
2945 return MatchOperand_Success;
2949 bool haveEaten = false;
2952 if (Tok.is(AsmToken::Plus)) {
2953 Parser.Lex(); // Eat the '+' token.
2955 } else if (Tok.is(AsmToken::Minus)) {
2956 Parser.Lex(); // Eat the '-' token.
2960 if (Parser.getTok().is(AsmToken::Identifier))
2961 Reg = tryParseRegister();
2964 return MatchOperand_NoMatch;
2965 Error(Parser.getTok().getLoc(), "register expected");
2966 return MatchOperand_ParseFail;
2968 SMLoc E = Parser.getTok().getLoc();
2970 Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ARM_AM::no_shift,
2973 return MatchOperand_Success;
2976 /// cvtT2LdrdPre - Convert parsed operands to MCInst.
2977 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2978 /// when they refer multiple MIOperands inside a single one.
2980 cvtT2LdrdPre(MCInst &Inst, unsigned Opcode,
2981 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2983 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2984 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
2985 // Create a writeback register dummy placeholder.
2986 Inst.addOperand(MCOperand::CreateReg(0));
2988 ((ARMOperand*)Operands[4])->addMemImm8s4OffsetOperands(Inst, 2);
2990 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2994 /// cvtT2StrdPre - Convert parsed operands to MCInst.
2995 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2996 /// when they refer multiple MIOperands inside a single one.
2998 cvtT2StrdPre(MCInst &Inst, unsigned Opcode,
2999 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3000 // Create a writeback register dummy placeholder.
3001 Inst.addOperand(MCOperand::CreateReg(0));
3003 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3004 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3006 ((ARMOperand*)Operands[4])->addMemImm8s4OffsetOperands(Inst, 2);
3008 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3012 /// cvtLdWriteBackRegT2AddrModeImm8 - Convert parsed operands to MCInst.
3013 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3014 /// when they refer multiple MIOperands inside a single one.
3016 cvtLdWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
3017 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3018 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3020 // Create a writeback register dummy placeholder.
3021 Inst.addOperand(MCOperand::CreateImm(0));
3023 ((ARMOperand*)Operands[3])->addMemImm8OffsetOperands(Inst, 2);
3024 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3028 /// cvtStWriteBackRegT2AddrModeImm8 - Convert parsed operands to MCInst.
3029 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3030 /// when they refer multiple MIOperands inside a single one.
3032 cvtStWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
3033 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3034 // Create a writeback register dummy placeholder.
3035 Inst.addOperand(MCOperand::CreateImm(0));
3036 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3037 ((ARMOperand*)Operands[3])->addMemImm8OffsetOperands(Inst, 2);
3038 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3042 /// cvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
3043 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3044 /// when they refer multiple MIOperands inside a single one.
3046 cvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
3047 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3048 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3050 // Create a writeback register dummy placeholder.
3051 Inst.addOperand(MCOperand::CreateImm(0));
3053 ((ARMOperand*)Operands[3])->addAddrMode2Operands(Inst, 3);
3054 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3058 /// cvtLdWriteBackRegAddrModeImm12 - Convert parsed operands to MCInst.
3059 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3060 /// when they refer multiple MIOperands inside a single one.
3062 cvtLdWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
3063 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3064 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3066 // Create a writeback register dummy placeholder.
3067 Inst.addOperand(MCOperand::CreateImm(0));
3069 ((ARMOperand*)Operands[3])->addMemImm12OffsetOperands(Inst, 2);
3070 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3075 /// cvtStWriteBackRegAddrModeImm12 - Convert parsed operands to MCInst.
3076 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3077 /// when they refer multiple MIOperands inside a single one.
3079 cvtStWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
3080 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3081 // Create a writeback register dummy placeholder.
3082 Inst.addOperand(MCOperand::CreateImm(0));
3083 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3084 ((ARMOperand*)Operands[3])->addMemImm12OffsetOperands(Inst, 2);
3085 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3089 /// cvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
3090 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3091 /// when they refer multiple MIOperands inside a single one.
3093 cvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
3094 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3095 // Create a writeback register dummy placeholder.
3096 Inst.addOperand(MCOperand::CreateImm(0));
3097 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3098 ((ARMOperand*)Operands[3])->addAddrMode2Operands(Inst, 3);
3099 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3103 /// cvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
3104 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3105 /// when they refer multiple MIOperands inside a single one.
3107 cvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
3108 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3109 // Create a writeback register dummy placeholder.
3110 Inst.addOperand(MCOperand::CreateImm(0));
3111 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3112 ((ARMOperand*)Operands[3])->addAddrMode3Operands(Inst, 3);
3113 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3117 /// cvtLdExtTWriteBackImm - Convert parsed operands to MCInst.
3118 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3119 /// when they refer multiple MIOperands inside a single one.
3121 cvtLdExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
3122 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3124 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3125 // Create a writeback register dummy placeholder.
3126 Inst.addOperand(MCOperand::CreateImm(0));
3128 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3130 ((ARMOperand*)Operands[4])->addPostIdxImm8Operands(Inst, 1);
3132 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3136 /// cvtLdExtTWriteBackReg - Convert parsed operands to MCInst.
3137 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3138 /// when they refer multiple MIOperands inside a single one.
3140 cvtLdExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
3141 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3143 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3144 // Create a writeback register dummy placeholder.
3145 Inst.addOperand(MCOperand::CreateImm(0));
3147 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3149 ((ARMOperand*)Operands[4])->addPostIdxRegOperands(Inst, 2);
3151 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3155 /// cvtStExtTWriteBackImm - Convert parsed operands to MCInst.
3156 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3157 /// when they refer multiple MIOperands inside a single one.
3159 cvtStExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
3160 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3161 // Create a writeback register dummy placeholder.
3162 Inst.addOperand(MCOperand::CreateImm(0));
3164 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3166 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3168 ((ARMOperand*)Operands[4])->addPostIdxImm8Operands(Inst, 1);
3170 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3174 /// cvtStExtTWriteBackReg - 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 cvtStExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
3179 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3180 // Create a writeback register dummy placeholder.
3181 Inst.addOperand(MCOperand::CreateImm(0));
3183 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3185 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3187 ((ARMOperand*)Operands[4])->addPostIdxRegOperands(Inst, 2);
3189 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3193 /// cvtLdrdPre - Convert parsed operands to MCInst.
3194 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3195 /// when they refer multiple MIOperands inside a single one.
3197 cvtLdrdPre(MCInst &Inst, unsigned Opcode,
3198 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3200 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3201 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3202 // Create a writeback register dummy placeholder.
3203 Inst.addOperand(MCOperand::CreateImm(0));
3205 ((ARMOperand*)Operands[4])->addAddrMode3Operands(Inst, 3);
3207 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3211 /// cvtStrdPre - Convert parsed operands to MCInst.
3212 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3213 /// when they refer multiple MIOperands inside a single one.
3215 cvtStrdPre(MCInst &Inst, unsigned Opcode,
3216 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3217 // Create a writeback register dummy placeholder.
3218 Inst.addOperand(MCOperand::CreateImm(0));
3220 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3221 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3223 ((ARMOperand*)Operands[4])->addAddrMode3Operands(Inst, 3);
3225 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3229 /// cvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
3230 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3231 /// when they refer multiple MIOperands inside a single one.
3233 cvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
3234 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3235 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3236 // Create a writeback register dummy placeholder.
3237 Inst.addOperand(MCOperand::CreateImm(0));
3238 ((ARMOperand*)Operands[3])->addAddrMode3Operands(Inst, 3);
3239 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3243 /// cvtThumbMultiple- Convert parsed operands to MCInst.
3244 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3245 /// when they refer multiple MIOperands inside a single one.
3247 cvtThumbMultiply(MCInst &Inst, unsigned Opcode,
3248 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3249 // The second source operand must be the same register as the destination
3251 if (Operands.size() == 6 &&
3252 (((ARMOperand*)Operands[3])->getReg() !=
3253 ((ARMOperand*)Operands[5])->getReg()) &&
3254 (((ARMOperand*)Operands[3])->getReg() !=
3255 ((ARMOperand*)Operands[4])->getReg())) {
3256 Error(Operands[3]->getStartLoc(),
3257 "destination register must match source register");
3260 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3261 ((ARMOperand*)Operands[1])->addCCOutOperands(Inst, 1);
3262 ((ARMOperand*)Operands[4])->addRegOperands(Inst, 1);
3263 // If we have a three-operand form, use that, else the second source operand
3264 // is just the destination operand again.
3265 if (Operands.size() == 6)
3266 ((ARMOperand*)Operands[5])->addRegOperands(Inst, 1);
3268 Inst.addOperand(Inst.getOperand(0));
3269 ((ARMOperand*)Operands[2])->addCondCodeOperands(Inst, 2);
3274 /// Parse an ARM memory expression, return false if successful else return true
3275 /// or an error. The first token must be a '[' when called.
3277 parseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3279 assert(Parser.getTok().is(AsmToken::LBrac) &&
3280 "Token is not a Left Bracket");
3281 S = Parser.getTok().getLoc();
3282 Parser.Lex(); // Eat left bracket token.
3284 const AsmToken &BaseRegTok = Parser.getTok();
3285 int BaseRegNum = tryParseRegister();
3286 if (BaseRegNum == -1)
3287 return Error(BaseRegTok.getLoc(), "register expected");
3289 // The next token must either be a comma or a closing bracket.
3290 const AsmToken &Tok = Parser.getTok();
3291 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
3292 return Error(Tok.getLoc(), "malformed memory operand");
3294 if (Tok.is(AsmToken::RBrac)) {
3296 Parser.Lex(); // Eat right bracket token.
3298 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, 0, ARM_AM::no_shift,
3299 0, 0, false, S, E));
3301 // If there's a pre-indexing writeback marker, '!', just add it as a token
3302 // operand. It's rather odd, but syntactically valid.
3303 if (Parser.getTok().is(AsmToken::Exclaim)) {
3304 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3305 Parser.Lex(); // Eat the '!'.
3311 assert(Tok.is(AsmToken::Comma) && "Lost comma in memory operand?!");
3312 Parser.Lex(); // Eat the comma.
3314 // If we have a ':', it's an alignment specifier.
3315 if (Parser.getTok().is(AsmToken::Colon)) {
3316 Parser.Lex(); // Eat the ':'.
3317 E = Parser.getTok().getLoc();
3320 if (getParser().ParseExpression(Expr))
3323 // The expression has to be a constant. Memory references with relocations
3324 // don't come through here, as they use the <label> forms of the relevant
3326 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
3328 return Error (E, "constant expression expected");
3331 switch (CE->getValue()) {
3333 return Error(E, "alignment specifier must be 64, 128, or 256 bits");
3334 case 64: Align = 8; break;
3335 case 128: Align = 16; break;
3336 case 256: Align = 32; break;
3339 // Now we should have the closing ']'
3340 E = Parser.getTok().getLoc();
3341 if (Parser.getTok().isNot(AsmToken::RBrac))
3342 return Error(E, "']' expected");
3343 Parser.Lex(); // Eat right bracket token.
3345 // Don't worry about range checking the value here. That's handled by
3346 // the is*() predicates.
3347 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, 0,
3348 ARM_AM::no_shift, 0, Align,
3351 // If there's a pre-indexing writeback marker, '!', just add it as a token
3353 if (Parser.getTok().is(AsmToken::Exclaim)) {
3354 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3355 Parser.Lex(); // Eat the '!'.
3361 // If we have a '#', it's an immediate offset, else assume it's a register
3363 if (Parser.getTok().is(AsmToken::Hash)) {
3364 Parser.Lex(); // Eat the '#'.
3365 E = Parser.getTok().getLoc();
3367 bool isNegative = getParser().getTok().is(AsmToken::Minus);
3368 const MCExpr *Offset;
3369 if (getParser().ParseExpression(Offset))
3372 // The expression has to be a constant. Memory references with relocations
3373 // don't come through here, as they use the <label> forms of the relevant
3375 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
3377 return Error (E, "constant expression expected");
3379 // If the constant was #-0, represent it as INT32_MIN.
3380 int32_t Val = CE->getValue();
3381 if (isNegative && Val == 0)
3382 CE = MCConstantExpr::Create(INT32_MIN, getContext());
3384 // Now we should have the closing ']'
3385 E = Parser.getTok().getLoc();
3386 if (Parser.getTok().isNot(AsmToken::RBrac))
3387 return Error(E, "']' expected");
3388 Parser.Lex(); // Eat right bracket token.
3390 // Don't worry about range checking the value here. That's handled by
3391 // the is*() predicates.
3392 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, CE, 0,
3393 ARM_AM::no_shift, 0, 0,
3396 // If there's a pre-indexing writeback marker, '!', just add it as a token
3398 if (Parser.getTok().is(AsmToken::Exclaim)) {
3399 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3400 Parser.Lex(); // Eat the '!'.
3406 // The register offset is optionally preceded by a '+' or '-'
3407 bool isNegative = false;
3408 if (Parser.getTok().is(AsmToken::Minus)) {
3410 Parser.Lex(); // Eat the '-'.
3411 } else if (Parser.getTok().is(AsmToken::Plus)) {
3413 Parser.Lex(); // Eat the '+'.
3416 E = Parser.getTok().getLoc();
3417 int OffsetRegNum = tryParseRegister();
3418 if (OffsetRegNum == -1)
3419 return Error(E, "register expected");
3421 // If there's a shift operator, handle it.
3422 ARM_AM::ShiftOpc ShiftType = ARM_AM::no_shift;
3423 unsigned ShiftImm = 0;
3424 if (Parser.getTok().is(AsmToken::Comma)) {
3425 Parser.Lex(); // Eat the ','.
3426 if (parseMemRegOffsetShift(ShiftType, ShiftImm))
3430 // Now we should have the closing ']'
3431 E = Parser.getTok().getLoc();
3432 if (Parser.getTok().isNot(AsmToken::RBrac))
3433 return Error(E, "']' expected");
3434 Parser.Lex(); // Eat right bracket token.
3436 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, OffsetRegNum,
3437 ShiftType, ShiftImm, 0, isNegative,
3440 // If there's a pre-indexing writeback marker, '!', just add it as a token
3442 if (Parser.getTok().is(AsmToken::Exclaim)) {
3443 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3444 Parser.Lex(); // Eat the '!'.
3450 /// parseMemRegOffsetShift - one of these two:
3451 /// ( lsl | lsr | asr | ror ) , # shift_amount
3453 /// return true if it parses a shift otherwise it returns false.
3454 bool ARMAsmParser::parseMemRegOffsetShift(ARM_AM::ShiftOpc &St,
3456 SMLoc Loc = Parser.getTok().getLoc();
3457 const AsmToken &Tok = Parser.getTok();
3458 if (Tok.isNot(AsmToken::Identifier))
3460 StringRef ShiftName = Tok.getString();
3461 if (ShiftName == "lsl" || ShiftName == "LSL")
3463 else if (ShiftName == "lsr" || ShiftName == "LSR")
3465 else if (ShiftName == "asr" || ShiftName == "ASR")
3467 else if (ShiftName == "ror" || ShiftName == "ROR")
3469 else if (ShiftName == "rrx" || ShiftName == "RRX")
3472 return Error(Loc, "illegal shift operator");
3473 Parser.Lex(); // Eat shift type token.
3475 // rrx stands alone.
3477 if (St != ARM_AM::rrx) {
3478 Loc = Parser.getTok().getLoc();
3479 // A '#' and a shift amount.
3480 const AsmToken &HashTok = Parser.getTok();
3481 if (HashTok.isNot(AsmToken::Hash))
3482 return Error(HashTok.getLoc(), "'#' expected");
3483 Parser.Lex(); // Eat hash token.
3486 if (getParser().ParseExpression(Expr))
3488 // Range check the immediate.
3489 // lsl, ror: 0 <= imm <= 31
3490 // lsr, asr: 0 <= imm <= 32
3491 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
3493 return Error(Loc, "shift amount must be an immediate");
3494 int64_t Imm = CE->getValue();
3496 ((St == ARM_AM::lsl || St == ARM_AM::ror) && Imm > 31) ||
3497 ((St == ARM_AM::lsr || St == ARM_AM::asr) && Imm > 32))
3498 return Error(Loc, "immediate shift value out of range");
3505 /// parseFPImm - A floating point immediate expression operand.
3506 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3507 parseFPImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3508 SMLoc S = Parser.getTok().getLoc();
3510 if (Parser.getTok().isNot(AsmToken::Hash))
3511 return MatchOperand_NoMatch;
3513 // Disambiguate the VMOV forms that can accept an FP immediate.
3514 // vmov.f32 <sreg>, #imm
3515 // vmov.f64 <dreg>, #imm
3516 // vmov.f32 <dreg>, #imm @ vector f32x2
3517 // vmov.f32 <qreg>, #imm @ vector f32x4
3519 // There are also the NEON VMOV instructions which expect an
3520 // integer constant. Make sure we don't try to parse an FPImm
3522 // vmov.i{8|16|32|64} <dreg|qreg>, #imm
3523 ARMOperand *TyOp = static_cast<ARMOperand*>(Operands[2]);
3524 if (!TyOp->isToken() || (TyOp->getToken() != ".f32" &&
3525 TyOp->getToken() != ".f64"))
3526 return MatchOperand_NoMatch;
3528 Parser.Lex(); // Eat the '#'.
3530 // Handle negation, as that still comes through as a separate token.
3531 bool isNegative = false;
3532 if (Parser.getTok().is(AsmToken::Minus)) {
3536 const AsmToken &Tok = Parser.getTok();
3537 if (Tok.is(AsmToken::Real)) {
3538 APFloat RealVal(APFloat::IEEEdouble, Tok.getString());
3539 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
3540 // If we had a '-' in front, toggle the sign bit.
3541 IntVal ^= (uint64_t)isNegative << 63;
3542 int Val = ARM_AM::getFP64Imm(APInt(64, IntVal));
3543 Parser.Lex(); // Eat the token.
3545 TokError("floating point value out of range");
3546 return MatchOperand_ParseFail;
3548 Operands.push_back(ARMOperand::CreateFPImm(Val, S, getContext()));
3549 return MatchOperand_Success;
3551 if (Tok.is(AsmToken::Integer)) {
3552 int64_t Val = Tok.getIntVal();
3553 Parser.Lex(); // Eat the token.
3554 if (Val > 255 || Val < 0) {
3555 TokError("encoded floating point value out of range");
3556 return MatchOperand_ParseFail;
3558 Operands.push_back(ARMOperand::CreateFPImm(Val, S, getContext()));
3559 return MatchOperand_Success;
3562 TokError("invalid floating point immediate");
3563 return MatchOperand_ParseFail;
3565 /// Parse a arm instruction operand. For now this parses the operand regardless
3566 /// of the mnemonic.
3567 bool ARMAsmParser::parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
3568 StringRef Mnemonic) {
3571 // Check if the current operand has a custom associated parser, if so, try to
3572 // custom parse the operand, or fallback to the general approach.
3573 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
3574 if (ResTy == MatchOperand_Success)
3576 // If there wasn't a custom match, try the generic matcher below. Otherwise,
3577 // there was a match, but an error occurred, in which case, just return that
3578 // the operand parsing failed.
3579 if (ResTy == MatchOperand_ParseFail)
3582 switch (getLexer().getKind()) {
3584 Error(Parser.getTok().getLoc(), "unexpected token in operand");
3586 case AsmToken::Identifier: {
3587 // If this is VMRS, check for the apsr_nzcv operand.
3588 if (!tryParseRegisterWithWriteBack(Operands))
3590 int Res = tryParseShiftRegister(Operands);
3591 if (Res == 0) // success
3593 else if (Res == -1) // irrecoverable error
3595 if (Mnemonic == "vmrs" && Parser.getTok().getString() == "apsr_nzcv") {
3596 S = Parser.getTok().getLoc();
3598 Operands.push_back(ARMOperand::CreateToken("apsr_nzcv", S));
3602 // Fall though for the Identifier case that is not a register or a
3605 case AsmToken::Integer: // things like 1f and 2b as a branch targets
3606 case AsmToken::Dot: { // . as a branch target
3607 // This was not a register so parse other operands that start with an
3608 // identifier (like labels) as expressions and create them as immediates.
3609 const MCExpr *IdVal;
3610 S = Parser.getTok().getLoc();
3611 if (getParser().ParseExpression(IdVal))
3613 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3614 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
3617 case AsmToken::LBrac:
3618 return parseMemory(Operands);
3619 case AsmToken::LCurly:
3620 return parseRegisterList(Operands);
3621 case AsmToken::Hash: {
3622 // #42 -> immediate.
3623 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
3624 S = Parser.getTok().getLoc();
3626 bool isNegative = Parser.getTok().is(AsmToken::Minus);
3627 const MCExpr *ImmVal;
3628 if (getParser().ParseExpression(ImmVal))
3630 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ImmVal);
3632 Error(S, "constant expression expected");
3633 return MatchOperand_ParseFail;
3635 int32_t Val = CE->getValue();
3636 if (isNegative && Val == 0)
3637 ImmVal = MCConstantExpr::Create(INT32_MIN, getContext());
3638 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3639 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
3642 case AsmToken::Colon: {
3643 // ":lower16:" and ":upper16:" expression prefixes
3644 // FIXME: Check it's an expression prefix,
3645 // e.g. (FOO - :lower16:BAR) isn't legal.
3646 ARMMCExpr::VariantKind RefKind;
3647 if (parsePrefix(RefKind))
3650 const MCExpr *SubExprVal;
3651 if (getParser().ParseExpression(SubExprVal))
3654 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
3656 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3657 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
3663 // parsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
3664 // :lower16: and :upper16:.
3665 bool ARMAsmParser::parsePrefix(ARMMCExpr::VariantKind &RefKind) {
3666 RefKind = ARMMCExpr::VK_ARM_None;
3668 // :lower16: and :upper16: modifiers
3669 assert(getLexer().is(AsmToken::Colon) && "expected a :");
3670 Parser.Lex(); // Eat ':'
3672 if (getLexer().isNot(AsmToken::Identifier)) {
3673 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
3677 StringRef IDVal = Parser.getTok().getIdentifier();
3678 if (IDVal == "lower16") {
3679 RefKind = ARMMCExpr::VK_ARM_LO16;
3680 } else if (IDVal == "upper16") {
3681 RefKind = ARMMCExpr::VK_ARM_HI16;
3683 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
3688 if (getLexer().isNot(AsmToken::Colon)) {
3689 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
3692 Parser.Lex(); // Eat the last ':'
3696 /// \brief Given a mnemonic, split out possible predication code and carry
3697 /// setting letters to form a canonical mnemonic and flags.
3699 // FIXME: Would be nice to autogen this.
3700 // FIXME: This is a bit of a maze of special cases.
3701 StringRef ARMAsmParser::splitMnemonic(StringRef Mnemonic,
3702 unsigned &PredicationCode,
3704 unsigned &ProcessorIMod,
3705 StringRef &ITMask) {
3706 PredicationCode = ARMCC::AL;
3707 CarrySetting = false;
3710 // Ignore some mnemonics we know aren't predicated forms.
3712 // FIXME: Would be nice to autogen this.
3713 if ((Mnemonic == "movs" && isThumb()) ||
3714 Mnemonic == "teq" || Mnemonic == "vceq" || Mnemonic == "svc" ||
3715 Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
3716 Mnemonic == "vmls" || Mnemonic == "vnmls" || Mnemonic == "vacge" ||
3717 Mnemonic == "vcge" || Mnemonic == "vclt" || Mnemonic == "vacgt" ||
3718 Mnemonic == "vcgt" || Mnemonic == "vcle" || Mnemonic == "smlal" ||
3719 Mnemonic == "umaal" || Mnemonic == "umlal" || Mnemonic == "vabal" ||
3720 Mnemonic == "vmlal" || Mnemonic == "vpadal" || Mnemonic == "vqdmlal")
3723 // First, split out any predication code. Ignore mnemonics we know aren't
3724 // predicated but do have a carry-set and so weren't caught above.
3725 if (Mnemonic != "adcs" && Mnemonic != "bics" && Mnemonic != "movs" &&
3726 Mnemonic != "muls" && Mnemonic != "smlals" && Mnemonic != "smulls" &&
3727 Mnemonic != "umlals" && Mnemonic != "umulls" && Mnemonic != "lsls" &&
3728 Mnemonic != "sbcs" && Mnemonic != "rscs") {
3729 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
3730 .Case("eq", ARMCC::EQ)
3731 .Case("ne", ARMCC::NE)
3732 .Case("hs", ARMCC::HS)
3733 .Case("cs", ARMCC::HS)
3734 .Case("lo", ARMCC::LO)
3735 .Case("cc", ARMCC::LO)
3736 .Case("mi", ARMCC::MI)
3737 .Case("pl", ARMCC::PL)
3738 .Case("vs", ARMCC::VS)
3739 .Case("vc", ARMCC::VC)
3740 .Case("hi", ARMCC::HI)
3741 .Case("ls", ARMCC::LS)
3742 .Case("ge", ARMCC::GE)
3743 .Case("lt", ARMCC::LT)
3744 .Case("gt", ARMCC::GT)
3745 .Case("le", ARMCC::LE)
3746 .Case("al", ARMCC::AL)
3749 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
3750 PredicationCode = CC;
3754 // Next, determine if we have a carry setting bit. We explicitly ignore all
3755 // the instructions we know end in 's'.
3756 if (Mnemonic.endswith("s") &&
3757 !(Mnemonic == "cps" || Mnemonic == "mls" ||
3758 Mnemonic == "mrs" || Mnemonic == "smmls" || Mnemonic == "vabs" ||
3759 Mnemonic == "vcls" || Mnemonic == "vmls" || Mnemonic == "vmrs" ||
3760 Mnemonic == "vnmls" || Mnemonic == "vqabs" || Mnemonic == "vrecps" ||
3761 Mnemonic == "vrsqrts" || Mnemonic == "srs" ||
3762 (Mnemonic == "movs" && isThumb()))) {
3763 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
3764 CarrySetting = true;
3767 // The "cps" instruction can have a interrupt mode operand which is glued into
3768 // the mnemonic. Check if this is the case, split it and parse the imod op
3769 if (Mnemonic.startswith("cps")) {
3770 // Split out any imod code.
3772 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
3773 .Case("ie", ARM_PROC::IE)
3774 .Case("id", ARM_PROC::ID)
3777 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
3778 ProcessorIMod = IMod;
3782 // The "it" instruction has the condition mask on the end of the mnemonic.
3783 if (Mnemonic.startswith("it")) {
3784 ITMask = Mnemonic.slice(2, Mnemonic.size());
3785 Mnemonic = Mnemonic.slice(0, 2);
3791 /// \brief Given a canonical mnemonic, determine if the instruction ever allows
3792 /// inclusion of carry set or predication code operands.
3794 // FIXME: It would be nice to autogen this.
3796 getMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
3797 bool &CanAcceptPredicationCode) {
3798 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
3799 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
3800 Mnemonic == "add" || Mnemonic == "adc" ||
3801 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
3802 Mnemonic == "orr" || Mnemonic == "mvn" ||
3803 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
3804 Mnemonic == "sbc" || Mnemonic == "eor" || Mnemonic == "neg" ||
3805 (!isThumb() && (Mnemonic == "smull" || Mnemonic == "mov" ||
3806 Mnemonic == "mla" || Mnemonic == "smlal" ||
3807 Mnemonic == "umlal" || Mnemonic == "umull"))) {
3808 CanAcceptCarrySet = true;
3810 CanAcceptCarrySet = false;
3812 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
3813 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
3814 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
3815 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
3816 Mnemonic == "dsb" || Mnemonic == "isb" || Mnemonic == "setend" ||
3817 (Mnemonic == "clrex" && !isThumb()) ||
3818 (Mnemonic == "nop" && isThumbOne()) ||
3819 ((Mnemonic == "pld" || Mnemonic == "pli" || Mnemonic == "pldw" ||
3820 Mnemonic == "ldc2" || Mnemonic == "ldc2l" ||
3821 Mnemonic == "stc2" || Mnemonic == "stc2l") && !isThumb()) ||
3822 ((Mnemonic.startswith("rfe") || Mnemonic.startswith("srs")) &&
3824 Mnemonic.startswith("cps") || (Mnemonic == "movs" && isThumbOne())) {
3825 CanAcceptPredicationCode = false;
3827 CanAcceptPredicationCode = true;
3830 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
3831 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
3832 CanAcceptPredicationCode = false;
3836 bool ARMAsmParser::shouldOmitCCOutOperand(StringRef Mnemonic,
3837 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3838 // FIXME: This is all horribly hacky. We really need a better way to deal
3839 // with optional operands like this in the matcher table.
3841 // The 'mov' mnemonic is special. One variant has a cc_out operand, while
3842 // another does not. Specifically, the MOVW instruction does not. So we
3843 // special case it here and remove the defaulted (non-setting) cc_out
3844 // operand if that's the instruction we're trying to match.
3846 // We do this as post-processing of the explicit operands rather than just
3847 // conditionally adding the cc_out in the first place because we need
3848 // to check the type of the parsed immediate operand.
3849 if (Mnemonic == "mov" && Operands.size() > 4 && !isThumb() &&
3850 !static_cast<ARMOperand*>(Operands[4])->isARMSOImm() &&
3851 static_cast<ARMOperand*>(Operands[4])->isImm0_65535Expr() &&
3852 static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
3855 // Register-register 'add' for thumb does not have a cc_out operand
3856 // when there are only two register operands.
3857 if (isThumb() && Mnemonic == "add" && Operands.size() == 5 &&
3858 static_cast<ARMOperand*>(Operands[3])->isReg() &&
3859 static_cast<ARMOperand*>(Operands[4])->isReg() &&
3860 static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
3862 // Register-register 'add' for thumb does not have a cc_out operand
3863 // when it's an ADD Rdm, SP, {Rdm|#imm0_255} instruction. We do
3864 // have to check the immediate range here since Thumb2 has a variant
3865 // that can handle a different range and has a cc_out operand.
3866 if (((isThumb() && Mnemonic == "add") ||
3867 (isThumbTwo() && Mnemonic == "sub")) &&
3868 Operands.size() == 6 &&
3869 static_cast<ARMOperand*>(Operands[3])->isReg() &&
3870 static_cast<ARMOperand*>(Operands[4])->isReg() &&
3871 static_cast<ARMOperand*>(Operands[4])->getReg() == ARM::SP &&
3872 static_cast<ARMOperand*>(Operands[1])->getReg() == 0 &&
3873 (static_cast<ARMOperand*>(Operands[5])->isReg() ||
3874 static_cast<ARMOperand*>(Operands[5])->isImm0_1020s4()))
3876 // For Thumb2, add/sub immediate does not have a cc_out operand for the
3877 // imm0_4095 variant. That's the least-preferred variant when
3878 // selecting via the generic "add" mnemonic, so to know that we
3879 // should remove the cc_out operand, we have to explicitly check that
3880 // it's not one of the other variants. Ugh.
3881 if (isThumbTwo() && (Mnemonic == "add" || Mnemonic == "sub") &&
3882 Operands.size() == 6 &&
3883 static_cast<ARMOperand*>(Operands[3])->isReg() &&
3884 static_cast<ARMOperand*>(Operands[4])->isReg() &&
3885 static_cast<ARMOperand*>(Operands[5])->isImm()) {
3886 // Nest conditions rather than one big 'if' statement for readability.
3888 // If either register is a high reg, it's either one of the SP
3889 // variants (handled above) or a 32-bit encoding, so we just
3890 // check against T3.
3891 if ((!isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) ||
3892 !isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg())) &&
3893 static_cast<ARMOperand*>(Operands[5])->isT2SOImm())
3895 // If both registers are low, we're in an IT block, and the immediate is
3896 // in range, we should use encoding T1 instead, which has a cc_out.
3898 isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) &&
3899 isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg()) &&
3900 static_cast<ARMOperand*>(Operands[5])->isImm0_7())
3903 // Otherwise, we use encoding T4, which does not have a cc_out
3908 // The thumb2 multiply instruction doesn't have a CCOut register, so
3909 // if we have a "mul" mnemonic in Thumb mode, check if we'll be able to
3910 // use the 16-bit encoding or not.
3911 if (isThumbTwo() && Mnemonic == "mul" && Operands.size() == 6 &&
3912 static_cast<ARMOperand*>(Operands[1])->getReg() == 0 &&
3913 static_cast<ARMOperand*>(Operands[3])->isReg() &&
3914 static_cast<ARMOperand*>(Operands[4])->isReg() &&
3915 static_cast<ARMOperand*>(Operands[5])->isReg() &&
3916 // If the registers aren't low regs, the destination reg isn't the
3917 // same as one of the source regs, or the cc_out operand is zero
3918 // outside of an IT block, we have to use the 32-bit encoding, so
3919 // remove the cc_out operand.
3920 (!isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) ||
3921 !isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg()) ||
3923 (static_cast<ARMOperand*>(Operands[3])->getReg() !=
3924 static_cast<ARMOperand*>(Operands[5])->getReg() &&
3925 static_cast<ARMOperand*>(Operands[3])->getReg() !=
3926 static_cast<ARMOperand*>(Operands[4])->getReg())))
3931 // Register-register 'add/sub' for thumb does not have a cc_out operand
3932 // when it's an ADD/SUB SP, #imm. Be lenient on count since there's also
3933 // the "add/sub SP, SP, #imm" version. If the follow-up operands aren't
3934 // right, this will result in better diagnostics (which operand is off)
3936 if (isThumb() && (Mnemonic == "add" || Mnemonic == "sub") &&
3937 (Operands.size() == 5 || Operands.size() == 6) &&
3938 static_cast<ARMOperand*>(Operands[3])->isReg() &&
3939 static_cast<ARMOperand*>(Operands[3])->getReg() == ARM::SP &&
3940 static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
3946 /// Parse an arm instruction mnemonic followed by its operands.
3947 bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
3948 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3949 // Create the leading tokens for the mnemonic, split by '.' characters.
3950 size_t Start = 0, Next = Name.find('.');
3951 StringRef Mnemonic = Name.slice(Start, Next);
3953 // Split out the predication code and carry setting flag from the mnemonic.
3954 unsigned PredicationCode;
3955 unsigned ProcessorIMod;
3958 Mnemonic = splitMnemonic(Mnemonic, PredicationCode, CarrySetting,
3959 ProcessorIMod, ITMask);
3961 // In Thumb1, only the branch (B) instruction can be predicated.
3962 if (isThumbOne() && PredicationCode != ARMCC::AL && Mnemonic != "b") {
3963 Parser.EatToEndOfStatement();
3964 return Error(NameLoc, "conditional execution not supported in Thumb1");
3967 Operands.push_back(ARMOperand::CreateToken(Mnemonic, NameLoc));
3969 // Handle the IT instruction ITMask. Convert it to a bitmask. This
3970 // is the mask as it will be for the IT encoding if the conditional
3971 // encoding has a '1' as it's bit0 (i.e. 't' ==> '1'). In the case
3972 // where the conditional bit0 is zero, the instruction post-processing
3973 // will adjust the mask accordingly.
3974 if (Mnemonic == "it") {
3975 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + 2);
3976 if (ITMask.size() > 3) {
3977 Parser.EatToEndOfStatement();
3978 return Error(Loc, "too many conditions on IT instruction");
3981 for (unsigned i = ITMask.size(); i != 0; --i) {
3982 char pos = ITMask[i - 1];
3983 if (pos != 't' && pos != 'e') {
3984 Parser.EatToEndOfStatement();
3985 return Error(Loc, "illegal IT block condition mask '" + ITMask + "'");
3988 if (ITMask[i - 1] == 't')
3991 Operands.push_back(ARMOperand::CreateITMask(Mask, Loc));
3994 // FIXME: This is all a pretty gross hack. We should automatically handle
3995 // optional operands like this via tblgen.
3997 // Next, add the CCOut and ConditionCode operands, if needed.
3999 // For mnemonics which can ever incorporate a carry setting bit or predication
4000 // code, our matching model involves us always generating CCOut and
4001 // ConditionCode operands to match the mnemonic "as written" and then we let
4002 // the matcher deal with finding the right instruction or generating an
4003 // appropriate error.
4004 bool CanAcceptCarrySet, CanAcceptPredicationCode;
4005 getMnemonicAcceptInfo(Mnemonic, CanAcceptCarrySet, CanAcceptPredicationCode);
4007 // If we had a carry-set on an instruction that can't do that, issue an
4009 if (!CanAcceptCarrySet && CarrySetting) {
4010 Parser.EatToEndOfStatement();
4011 return Error(NameLoc, "instruction '" + Mnemonic +
4012 "' can not set flags, but 's' suffix specified");
4014 // If we had a predication code on an instruction that can't do that, issue an
4016 if (!CanAcceptPredicationCode && PredicationCode != ARMCC::AL) {
4017 Parser.EatToEndOfStatement();
4018 return Error(NameLoc, "instruction '" + Mnemonic +
4019 "' is not predicable, but condition code specified");
4022 // Add the carry setting operand, if necessary.
4023 if (CanAcceptCarrySet) {
4024 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size());
4025 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
4029 // Add the predication code operand, if necessary.
4030 if (CanAcceptPredicationCode) {
4031 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size() +
4033 Operands.push_back(ARMOperand::CreateCondCode(
4034 ARMCC::CondCodes(PredicationCode), Loc));
4037 // Add the processor imod operand, if necessary.
4038 if (ProcessorIMod) {
4039 Operands.push_back(ARMOperand::CreateImm(
4040 MCConstantExpr::Create(ProcessorIMod, getContext()),
4044 // Add the remaining tokens in the mnemonic.
4045 while (Next != StringRef::npos) {
4047 Next = Name.find('.', Start + 1);
4048 StringRef ExtraToken = Name.slice(Start, Next);
4050 // For now, we're only parsing Thumb1 (for the most part), so
4051 // just ignore ".n" qualifiers. We'll use them to restrict
4052 // matching when we do Thumb2.
4053 if (ExtraToken != ".n") {
4054 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Start);
4055 Operands.push_back(ARMOperand::CreateToken(ExtraToken, Loc));
4059 // Read the remaining operands.
4060 if (getLexer().isNot(AsmToken::EndOfStatement)) {
4061 // Read the first operand.
4062 if (parseOperand(Operands, Mnemonic)) {
4063 Parser.EatToEndOfStatement();
4067 while (getLexer().is(AsmToken::Comma)) {
4068 Parser.Lex(); // Eat the comma.
4070 // Parse and remember the operand.
4071 if (parseOperand(Operands, Mnemonic)) {
4072 Parser.EatToEndOfStatement();
4078 if (getLexer().isNot(AsmToken::EndOfStatement)) {
4079 SMLoc Loc = getLexer().getLoc();
4080 Parser.EatToEndOfStatement();
4081 return Error(Loc, "unexpected token in argument list");
4084 Parser.Lex(); // Consume the EndOfStatement
4086 // Some instructions, mostly Thumb, have forms for the same mnemonic that
4087 // do and don't have a cc_out optional-def operand. With some spot-checks
4088 // of the operand list, we can figure out which variant we're trying to
4089 // parse and adjust accordingly before actually matching. We shouldn't ever
4090 // try to remove a cc_out operand that was explicitly set on the the
4091 // mnemonic, of course (CarrySetting == true). Reason number #317 the
4092 // table driven matcher doesn't fit well with the ARM instruction set.
4093 if (!CarrySetting && shouldOmitCCOutOperand(Mnemonic, Operands)) {
4094 ARMOperand *Op = static_cast<ARMOperand*>(Operands[1]);
4095 Operands.erase(Operands.begin() + 1);
4099 // ARM mode 'blx' need special handling, as the register operand version
4100 // is predicable, but the label operand version is not. So, we can't rely
4101 // on the Mnemonic based checking to correctly figure out when to put
4102 // a k_CondCode operand in the list. If we're trying to match the label
4103 // version, remove the k_CondCode operand here.
4104 if (!isThumb() && Mnemonic == "blx" && Operands.size() == 3 &&
4105 static_cast<ARMOperand*>(Operands[2])->isImm()) {
4106 ARMOperand *Op = static_cast<ARMOperand*>(Operands[1]);
4107 Operands.erase(Operands.begin() + 1);
4111 // The vector-compare-to-zero instructions have a literal token "#0" at
4112 // the end that comes to here as an immediate operand. Convert it to a
4113 // token to play nicely with the matcher.
4114 if ((Mnemonic == "vceq" || Mnemonic == "vcge" || Mnemonic == "vcgt" ||
4115 Mnemonic == "vcle" || Mnemonic == "vclt") && Operands.size() == 6 &&
4116 static_cast<ARMOperand*>(Operands[5])->isImm()) {
4117 ARMOperand *Op = static_cast<ARMOperand*>(Operands[5]);
4118 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
4119 if (CE && CE->getValue() == 0) {
4120 Operands.erase(Operands.begin() + 5);
4121 Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
4125 // VCMP{E} does the same thing, but with a different operand count.
4126 if ((Mnemonic == "vcmp" || Mnemonic == "vcmpe") && Operands.size() == 5 &&
4127 static_cast<ARMOperand*>(Operands[4])->isImm()) {
4128 ARMOperand *Op = static_cast<ARMOperand*>(Operands[4]);
4129 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
4130 if (CE && CE->getValue() == 0) {
4131 Operands.erase(Operands.begin() + 4);
4132 Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
4136 // Similarly, the Thumb1 "RSB" instruction has a literal "#0" on the
4137 // end. Convert it to a token here.
4138 if (Mnemonic == "rsb" && isThumb() && Operands.size() == 6 &&
4139 static_cast<ARMOperand*>(Operands[5])->isImm()) {
4140 ARMOperand *Op = static_cast<ARMOperand*>(Operands[5]);
4141 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
4142 if (CE && CE->getValue() == 0) {
4143 Operands.erase(Operands.begin() + 5);
4144 Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
4152 // Validate context-sensitive operand constraints.
4154 // return 'true' if register list contains non-low GPR registers,
4155 // 'false' otherwise. If Reg is in the register list or is HiReg, set
4156 // 'containsReg' to true.
4157 static bool checkLowRegisterList(MCInst Inst, unsigned OpNo, unsigned Reg,
4158 unsigned HiReg, bool &containsReg) {
4159 containsReg = false;
4160 for (unsigned i = OpNo; i < Inst.getNumOperands(); ++i) {
4161 unsigned OpReg = Inst.getOperand(i).getReg();
4164 // Anything other than a low register isn't legal here.
4165 if (!isARMLowRegister(OpReg) && (!HiReg || OpReg != HiReg))
4171 // Check if the specified regisgter is in the register list of the inst,
4172 // starting at the indicated operand number.
4173 static bool listContainsReg(MCInst &Inst, unsigned OpNo, unsigned Reg) {
4174 for (unsigned i = OpNo; i < Inst.getNumOperands(); ++i) {
4175 unsigned OpReg = Inst.getOperand(i).getReg();
4182 // FIXME: We would really prefer to have MCInstrInfo (the wrapper around
4183 // the ARMInsts array) instead. Getting that here requires awkward
4184 // API changes, though. Better way?
4186 extern MCInstrDesc ARMInsts[];
4188 static MCInstrDesc &getInstDesc(unsigned Opcode) {
4189 return ARMInsts[Opcode];
4192 // FIXME: We would really like to be able to tablegen'erate this.
4194 validateInstruction(MCInst &Inst,
4195 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
4196 MCInstrDesc &MCID = getInstDesc(Inst.getOpcode());
4197 SMLoc Loc = Operands[0]->getStartLoc();
4198 // Check the IT block state first.
4199 // NOTE: In Thumb mode, the BKPT instruction has the interesting property of
4200 // being allowed in IT blocks, but not being predicable. It just always
4202 if (inITBlock() && Inst.getOpcode() != ARM::tBKPT) {
4204 if (ITState.FirstCond)
4205 ITState.FirstCond = false;
4207 bit = (ITState.Mask >> (5 - ITState.CurPosition)) & 1;
4208 // The instruction must be predicable.
4209 if (!MCID.isPredicable())
4210 return Error(Loc, "instructions in IT block must be predicable");
4211 unsigned Cond = Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm();
4212 unsigned ITCond = bit ? ITState.Cond :
4213 ARMCC::getOppositeCondition(ITState.Cond);
4214 if (Cond != ITCond) {
4215 // Find the condition code Operand to get its SMLoc information.
4217 for (unsigned i = 1; i < Operands.size(); ++i)
4218 if (static_cast<ARMOperand*>(Operands[i])->isCondCode())
4219 CondLoc = Operands[i]->getStartLoc();
4220 return Error(CondLoc, "incorrect condition in IT block; got '" +
4221 StringRef(ARMCondCodeToString(ARMCC::CondCodes(Cond))) +
4222 "', but expected '" +
4223 ARMCondCodeToString(ARMCC::CondCodes(ITCond)) + "'");
4225 // Check for non-'al' condition codes outside of the IT block.
4226 } else if (isThumbTwo() && MCID.isPredicable() &&
4227 Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm() !=
4228 ARMCC::AL && Inst.getOpcode() != ARM::tB &&
4229 Inst.getOpcode() != ARM::t2B)
4230 return Error(Loc, "predicated instructions must be in IT block");
4232 switch (Inst.getOpcode()) {
4235 case ARM::LDRD_POST:
4237 // Rt2 must be Rt + 1.
4238 unsigned Rt = getARMRegisterNumbering(Inst.getOperand(0).getReg());
4239 unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(1).getReg());
4241 return Error(Operands[3]->getStartLoc(),
4242 "destination operands must be sequential");
4246 // Rt2 must be Rt + 1.
4247 unsigned Rt = getARMRegisterNumbering(Inst.getOperand(0).getReg());
4248 unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(1).getReg());
4250 return Error(Operands[3]->getStartLoc(),
4251 "source operands must be sequential");
4255 case ARM::STRD_POST:
4257 // Rt2 must be Rt + 1.
4258 unsigned Rt = getARMRegisterNumbering(Inst.getOperand(1).getReg());
4259 unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(2).getReg());
4261 return Error(Operands[3]->getStartLoc(),
4262 "source operands must be sequential");
4267 // width must be in range [1, 32-lsb]
4268 unsigned lsb = Inst.getOperand(2).getImm();
4269 unsigned widthm1 = Inst.getOperand(3).getImm();
4270 if (widthm1 >= 32 - lsb)
4271 return Error(Operands[5]->getStartLoc(),
4272 "bitfield width must be in range [1,32-lsb]");
4276 // If we're parsing Thumb2, the .w variant is available and handles
4277 // most cases that are normally illegal for a Thumb1 LDM
4278 // instruction. We'll make the transformation in processInstruction()
4281 // Thumb LDM instructions are writeback iff the base register is not
4282 // in the register list.
4283 unsigned Rn = Inst.getOperand(0).getReg();
4284 bool hasWritebackToken =
4285 (static_cast<ARMOperand*>(Operands[3])->isToken() &&
4286 static_cast<ARMOperand*>(Operands[3])->getToken() == "!");
4287 bool listContainsBase;
4288 if (checkLowRegisterList(Inst, 3, Rn, 0, listContainsBase) && !isThumbTwo())
4289 return Error(Operands[3 + hasWritebackToken]->getStartLoc(),
4290 "registers must be in range r0-r7");
4291 // If we should have writeback, then there should be a '!' token.
4292 if (!listContainsBase && !hasWritebackToken && !isThumbTwo())
4293 return Error(Operands[2]->getStartLoc(),
4294 "writeback operator '!' expected");
4295 // If we should not have writeback, there must not be a '!'. This is
4296 // true even for the 32-bit wide encodings.
4297 if (listContainsBase && hasWritebackToken)
4298 return Error(Operands[3]->getStartLoc(),
4299 "writeback operator '!' not allowed when base register "
4300 "in register list");
4304 case ARM::t2LDMIA_UPD: {
4305 if (listContainsReg(Inst, 3, Inst.getOperand(0).getReg()))
4306 return Error(Operands[4]->getStartLoc(),
4307 "writeback operator '!' not allowed when base register "
4308 "in register list");
4312 bool listContainsBase;
4313 if (checkLowRegisterList(Inst, 3, 0, ARM::PC, listContainsBase))
4314 return Error(Operands[2]->getStartLoc(),
4315 "registers must be in range r0-r7 or pc");
4319 bool listContainsBase;
4320 if (checkLowRegisterList(Inst, 3, 0, ARM::LR, listContainsBase))
4321 return Error(Operands[2]->getStartLoc(),
4322 "registers must be in range r0-r7 or lr");
4325 case ARM::tSTMIA_UPD: {
4326 bool listContainsBase;
4327 if (checkLowRegisterList(Inst, 4, 0, 0, listContainsBase) && !isThumbTwo())
4328 return Error(Operands[4]->getStartLoc(),
4329 "registers must be in range r0-r7");
4338 processInstruction(MCInst &Inst,
4339 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
4340 switch (Inst.getOpcode()) {
4341 case ARM::LDMIA_UPD:
4342 // If this is a load of a single register via a 'pop', then we should use
4343 // a post-indexed LDR instruction instead, per the ARM ARM.
4344 if (static_cast<ARMOperand*>(Operands[0])->getToken() == "pop" &&
4345 Inst.getNumOperands() == 5) {
4347 TmpInst.setOpcode(ARM::LDR_POST_IMM);
4348 TmpInst.addOperand(Inst.getOperand(4)); // Rt
4349 TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
4350 TmpInst.addOperand(Inst.getOperand(1)); // Rn
4351 TmpInst.addOperand(MCOperand::CreateReg(0)); // am2offset
4352 TmpInst.addOperand(MCOperand::CreateImm(4));
4353 TmpInst.addOperand(Inst.getOperand(2)); // CondCode
4354 TmpInst.addOperand(Inst.getOperand(3));
4358 case ARM::STMDB_UPD:
4359 // If this is a store of a single register via a 'push', then we should use
4360 // a pre-indexed STR instruction instead, per the ARM ARM.
4361 if (static_cast<ARMOperand*>(Operands[0])->getToken() == "push" &&
4362 Inst.getNumOperands() == 5) {
4364 TmpInst.setOpcode(ARM::STR_PRE_IMM);
4365 TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
4366 TmpInst.addOperand(Inst.getOperand(4)); // Rt
4367 TmpInst.addOperand(Inst.getOperand(1)); // addrmode_imm12
4368 TmpInst.addOperand(MCOperand::CreateImm(-4));
4369 TmpInst.addOperand(Inst.getOperand(2)); // CondCode
4370 TmpInst.addOperand(Inst.getOperand(3));
4375 // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
4376 // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
4377 // to encoding T2 if <Rd> is specified and encoding T2 is preferred
4378 // to encoding T1 if <Rd> is omitted."
4379 if (Inst.getOperand(3).getImm() < 8 && Operands.size() == 6)
4380 Inst.setOpcode(ARM::tADDi3);
4383 // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
4384 // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
4385 // to encoding T2 if <Rd> is specified and encoding T2 is preferred
4386 // to encoding T1 if <Rd> is omitted."
4387 if (Inst.getOperand(3).getImm() < 8 && Operands.size() == 6)
4388 Inst.setOpcode(ARM::tSUBi3);
4391 // A Thumb conditional branch outside of an IT block is a tBcc.
4392 if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock())
4393 Inst.setOpcode(ARM::tBcc);
4396 // A Thumb2 conditional branch outside of an IT block is a t2Bcc.
4397 if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock())
4398 Inst.setOpcode(ARM::t2Bcc);
4401 // If the conditional is AL or we're in an IT block, we really want t2B.
4402 if (Inst.getOperand(1).getImm() == ARMCC::AL || inITBlock())
4403 Inst.setOpcode(ARM::t2B);
4406 // If the conditional is AL, we really want tB.
4407 if (Inst.getOperand(1).getImm() == ARMCC::AL)
4408 Inst.setOpcode(ARM::tB);
4411 // If the register list contains any high registers, or if the writeback
4412 // doesn't match what tLDMIA can do, we need to use the 32-bit encoding
4413 // instead if we're in Thumb2. Otherwise, this should have generated
4414 // an error in validateInstruction().
4415 unsigned Rn = Inst.getOperand(0).getReg();
4416 bool hasWritebackToken =
4417 (static_cast<ARMOperand*>(Operands[3])->isToken() &&
4418 static_cast<ARMOperand*>(Operands[3])->getToken() == "!");
4419 bool listContainsBase;
4420 if (checkLowRegisterList(Inst, 3, Rn, 0, listContainsBase) ||
4421 (!listContainsBase && !hasWritebackToken) ||
4422 (listContainsBase && hasWritebackToken)) {
4423 // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
4424 assert (isThumbTwo());
4425 Inst.setOpcode(hasWritebackToken ? ARM::t2LDMIA_UPD : ARM::t2LDMIA);
4426 // If we're switching to the updating version, we need to insert
4427 // the writeback tied operand.
4428 if (hasWritebackToken)
4429 Inst.insert(Inst.begin(),
4430 MCOperand::CreateReg(Inst.getOperand(0).getReg()));
4434 case ARM::tSTMIA_UPD: {
4435 // If the register list contains any high registers, we need to use
4436 // the 32-bit encoding instead if we're in Thumb2. Otherwise, this
4437 // should have generated an error in validateInstruction().
4438 unsigned Rn = Inst.getOperand(0).getReg();
4439 bool listContainsBase;
4440 if (checkLowRegisterList(Inst, 4, Rn, 0, listContainsBase)) {
4441 // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
4442 assert (isThumbTwo());
4443 Inst.setOpcode(ARM::t2STMIA_UPD);
4448 // If we can use the 16-bit encoding and the user didn't explicitly
4449 // request the 32-bit variant, transform it here.
4450 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
4451 Inst.getOperand(1).getImm() <= 255 &&
4452 ((!inITBlock() && Inst.getOperand(2).getImm() == ARMCC::AL &&
4453 Inst.getOperand(4).getReg() == ARM::CPSR) ||
4454 (inITBlock() && Inst.getOperand(4).getReg() == 0)) &&
4455 (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
4456 static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
4457 // The operands aren't in the same order for tMOVi8...
4459 TmpInst.setOpcode(ARM::tMOVi8);
4460 TmpInst.addOperand(Inst.getOperand(0));
4461 TmpInst.addOperand(Inst.getOperand(4));
4462 TmpInst.addOperand(Inst.getOperand(1));
4463 TmpInst.addOperand(Inst.getOperand(2));
4464 TmpInst.addOperand(Inst.getOperand(3));
4470 // If we can use the 16-bit encoding and the user didn't explicitly
4471 // request the 32-bit variant, transform it here.
4472 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
4473 isARMLowRegister(Inst.getOperand(1).getReg()) &&
4474 Inst.getOperand(2).getImm() == ARMCC::AL &&
4475 Inst.getOperand(4).getReg() == ARM::CPSR &&
4476 (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
4477 static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
4478 // The operands aren't the same for tMOV[S]r... (no cc_out)
4480 TmpInst.setOpcode(Inst.getOperand(4).getReg() ? ARM::tMOVSr : ARM::tMOVr);
4481 TmpInst.addOperand(Inst.getOperand(0));
4482 TmpInst.addOperand(Inst.getOperand(1));
4483 TmpInst.addOperand(Inst.getOperand(2));
4484 TmpInst.addOperand(Inst.getOperand(3));
4493 // If we can use the 16-bit encoding and the user didn't explicitly
4494 // request the 32-bit variant, transform it here.
4495 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
4496 isARMLowRegister(Inst.getOperand(1).getReg()) &&
4497 Inst.getOperand(2).getImm() == 0 &&
4498 (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
4499 static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
4501 switch (Inst.getOpcode()) {
4502 default: llvm_unreachable("Illegal opcode!");
4503 case ARM::t2SXTH: NewOpc = ARM::tSXTH; break;
4504 case ARM::t2SXTB: NewOpc = ARM::tSXTB; break;
4505 case ARM::t2UXTH: NewOpc = ARM::tUXTH; break;
4506 case ARM::t2UXTB: NewOpc = ARM::tUXTB; break;
4508 // The operands aren't the same for thumb1 (no rotate operand).
4510 TmpInst.setOpcode(NewOpc);
4511 TmpInst.addOperand(Inst.getOperand(0));
4512 TmpInst.addOperand(Inst.getOperand(1));
4513 TmpInst.addOperand(Inst.getOperand(3));
4514 TmpInst.addOperand(Inst.getOperand(4));
4520 // The mask bits for all but the first condition are represented as
4521 // the low bit of the condition code value implies 't'. We currently
4522 // always have 1 implies 't', so XOR toggle the bits if the low bit
4523 // of the condition code is zero. The encoding also expects the low
4524 // bit of the condition to be encoded as bit 4 of the mask operand,
4525 // so mask that in if needed
4526 MCOperand &MO = Inst.getOperand(1);
4527 unsigned Mask = MO.getImm();
4528 unsigned OrigMask = Mask;
4529 unsigned TZ = CountTrailingZeros_32(Mask);
4530 if ((Inst.getOperand(0).getImm() & 1) == 0) {
4531 assert(Mask && TZ <= 3 && "illegal IT mask value!");
4532 for (unsigned i = 3; i != TZ; --i)
4538 // Set up the IT block state according to the IT instruction we just
4540 assert(!inITBlock() && "nested IT blocks?!");
4541 ITState.Cond = ARMCC::CondCodes(Inst.getOperand(0).getImm());
4542 ITState.Mask = OrigMask; // Use the original mask, not the updated one.
4543 ITState.CurPosition = 0;
4544 ITState.FirstCond = true;
4550 unsigned ARMAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
4551 // 16-bit thumb arithmetic instructions either require or preclude the 'S'
4552 // suffix depending on whether they're in an IT block or not.
4553 unsigned Opc = Inst.getOpcode();
4554 MCInstrDesc &MCID = getInstDesc(Opc);
4555 if (MCID.TSFlags & ARMII::ThumbArithFlagSetting) {
4556 assert(MCID.hasOptionalDef() &&
4557 "optionally flag setting instruction missing optional def operand");
4558 assert(MCID.NumOperands == Inst.getNumOperands() &&
4559 "operand count mismatch!");
4560 // Find the optional-def operand (cc_out).
4563 !MCID.OpInfo[OpNo].isOptionalDef() && OpNo < MCID.NumOperands;
4566 // If we're parsing Thumb1, reject it completely.
4567 if (isThumbOne() && Inst.getOperand(OpNo).getReg() != ARM::CPSR)
4568 return Match_MnemonicFail;
4569 // If we're parsing Thumb2, which form is legal depends on whether we're
4571 if (isThumbTwo() && Inst.getOperand(OpNo).getReg() != ARM::CPSR &&
4573 return Match_RequiresITBlock;
4574 if (isThumbTwo() && Inst.getOperand(OpNo).getReg() == ARM::CPSR &&
4576 return Match_RequiresNotITBlock;
4578 // Some high-register supporting Thumb1 encodings only allow both registers
4579 // to be from r0-r7 when in Thumb2.
4580 else if (Opc == ARM::tADDhirr && isThumbOne() &&
4581 isARMLowRegister(Inst.getOperand(1).getReg()) &&
4582 isARMLowRegister(Inst.getOperand(2).getReg()))
4583 return Match_RequiresThumb2;
4584 // Others only require ARMv6 or later.
4585 else if (Opc == ARM::tMOVr && isThumbOne() && !hasV6Ops() &&
4586 isARMLowRegister(Inst.getOperand(0).getReg()) &&
4587 isARMLowRegister(Inst.getOperand(1).getReg()))
4588 return Match_RequiresV6;
4589 return Match_Success;
4593 MatchAndEmitInstruction(SMLoc IDLoc,
4594 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
4598 unsigned MatchResult;
4599 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
4600 switch (MatchResult) {
4603 // Context sensitive operand constraints aren't handled by the matcher,
4604 // so check them here.
4605 if (validateInstruction(Inst, Operands)) {
4606 // Still progress the IT block, otherwise one wrong condition causes
4607 // nasty cascading errors.
4608 forwardITPosition();
4612 // Some instructions need post-processing to, for example, tweak which
4613 // encoding is selected.
4614 processInstruction(Inst, Operands);
4616 // Only move forward at the very end so that everything in validate
4617 // and process gets a consistent answer about whether we're in an IT
4619 forwardITPosition();
4621 Out.EmitInstruction(Inst);
4623 case Match_MissingFeature:
4624 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
4626 case Match_InvalidOperand: {
4627 SMLoc ErrorLoc = IDLoc;
4628 if (ErrorInfo != ~0U) {
4629 if (ErrorInfo >= Operands.size())
4630 return Error(IDLoc, "too few operands for instruction");
4632 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
4633 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
4636 return Error(ErrorLoc, "invalid operand for instruction");
4638 case Match_MnemonicFail:
4639 return Error(IDLoc, "invalid instruction");
4640 case Match_ConversionFail:
4641 // The converter function will have already emited a diagnostic.
4643 case Match_RequiresNotITBlock:
4644 return Error(IDLoc, "flag setting instruction only valid outside IT block");
4645 case Match_RequiresITBlock:
4646 return Error(IDLoc, "instruction only valid inside IT block");
4647 case Match_RequiresV6:
4648 return Error(IDLoc, "instruction variant requires ARMv6 or later");
4649 case Match_RequiresThumb2:
4650 return Error(IDLoc, "instruction variant requires Thumb2");
4653 llvm_unreachable("Implement any new match types added!");
4657 /// parseDirective parses the arm specific directives
4658 bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
4659 StringRef IDVal = DirectiveID.getIdentifier();
4660 if (IDVal == ".word")
4661 return parseDirectiveWord(4, DirectiveID.getLoc());
4662 else if (IDVal == ".thumb")
4663 return parseDirectiveThumb(DirectiveID.getLoc());
4664 else if (IDVal == ".thumb_func")
4665 return parseDirectiveThumbFunc(DirectiveID.getLoc());
4666 else if (IDVal == ".code")
4667 return parseDirectiveCode(DirectiveID.getLoc());
4668 else if (IDVal == ".syntax")
4669 return parseDirectiveSyntax(DirectiveID.getLoc());
4673 /// parseDirectiveWord
4674 /// ::= .word [ expression (, expression)* ]
4675 bool ARMAsmParser::parseDirectiveWord(unsigned Size, SMLoc L) {
4676 if (getLexer().isNot(AsmToken::EndOfStatement)) {
4678 const MCExpr *Value;
4679 if (getParser().ParseExpression(Value))
4682 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
4684 if (getLexer().is(AsmToken::EndOfStatement))
4687 // FIXME: Improve diagnostic.
4688 if (getLexer().isNot(AsmToken::Comma))
4689 return Error(L, "unexpected token in directive");
4698 /// parseDirectiveThumb
4700 bool ARMAsmParser::parseDirectiveThumb(SMLoc L) {
4701 if (getLexer().isNot(AsmToken::EndOfStatement))
4702 return Error(L, "unexpected token in directive");
4705 // TODO: set thumb mode
4706 // TODO: tell the MC streamer the mode
4707 // getParser().getStreamer().Emit???();
4711 /// parseDirectiveThumbFunc
4712 /// ::= .thumbfunc symbol_name
4713 bool ARMAsmParser::parseDirectiveThumbFunc(SMLoc L) {
4714 const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
4715 bool isMachO = MAI.hasSubsectionsViaSymbols();
4718 // Darwin asm has function name after .thumb_func direction
4721 const AsmToken &Tok = Parser.getTok();
4722 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
4723 return Error(L, "unexpected token in .thumb_func directive");
4724 Name = Tok.getString();
4725 Parser.Lex(); // Consume the identifier token.
4728 if (getLexer().isNot(AsmToken::EndOfStatement))
4729 return Error(L, "unexpected token in directive");
4732 // FIXME: assuming function name will be the line following .thumb_func
4734 Name = Parser.getTok().getString();
4737 // Mark symbol as a thumb symbol.
4738 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
4739 getParser().getStreamer().EmitThumbFunc(Func);
4743 /// parseDirectiveSyntax
4744 /// ::= .syntax unified | divided
4745 bool ARMAsmParser::parseDirectiveSyntax(SMLoc L) {
4746 const AsmToken &Tok = Parser.getTok();
4747 if (Tok.isNot(AsmToken::Identifier))
4748 return Error(L, "unexpected token in .syntax directive");
4749 StringRef Mode = Tok.getString();
4750 if (Mode == "unified" || Mode == "UNIFIED")
4752 else if (Mode == "divided" || Mode == "DIVIDED")
4753 return Error(L, "'.syntax divided' arm asssembly not supported");
4755 return Error(L, "unrecognized syntax mode in .syntax directive");
4757 if (getLexer().isNot(AsmToken::EndOfStatement))
4758 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
4761 // TODO tell the MC streamer the mode
4762 // getParser().getStreamer().Emit???();
4766 /// parseDirectiveCode
4767 /// ::= .code 16 | 32
4768 bool ARMAsmParser::parseDirectiveCode(SMLoc L) {
4769 const AsmToken &Tok = Parser.getTok();
4770 if (Tok.isNot(AsmToken::Integer))
4771 return Error(L, "unexpected token in .code directive");
4772 int64_t Val = Parser.getTok().getIntVal();
4778 return Error(L, "invalid operand to .code directive");
4780 if (getLexer().isNot(AsmToken::EndOfStatement))
4781 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
4787 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
4791 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
4797 extern "C" void LLVMInitializeARMAsmLexer();
4799 /// Force static initialization.
4800 extern "C" void LLVMInitializeARMAsmParser() {
4801 RegisterMCAsmParser<ARMAsmParser> X(TheARMTarget);
4802 RegisterMCAsmParser<ARMAsmParser> Y(TheThumbTarget);
4803 LLVMInitializeARMAsmLexer();
4806 #define GET_REGISTER_MATCHER
4807 #define GET_MATCHER_IMPLEMENTATION
4808 #include "ARMGenAsmMatcher.inc"