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 isVecListTwoD() const {
919 if (Kind != k_VectorList) return false;
920 return VectorList.Count == 2;
923 bool isVectorIndex8() const {
924 if (Kind != k_VectorIndex) return false;
925 return VectorIndex.Val < 8;
927 bool isVectorIndex16() const {
928 if (Kind != k_VectorIndex) return false;
929 return VectorIndex.Val < 4;
931 bool isVectorIndex32() const {
932 if (Kind != k_VectorIndex) return false;
933 return VectorIndex.Val < 2;
936 bool isNEONi8splat() const {
937 if (Kind != k_Immediate)
939 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
940 // Must be a constant.
941 if (!CE) return false;
942 int64_t Value = CE->getValue();
943 // i8 value splatted across 8 bytes. The immediate is just the 8 byte
945 return Value >= 0 && Value < 256;
948 bool isNEONi16splat() const {
949 if (Kind != k_Immediate)
951 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
952 // Must be a constant.
953 if (!CE) return false;
954 int64_t Value = CE->getValue();
955 // i16 value in the range [0,255] or [0x0100, 0xff00]
956 return (Value >= 0 && Value < 256) || (Value >= 0x0100 && Value <= 0xff00);
959 bool isNEONi32splat() const {
960 if (Kind != k_Immediate)
962 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
963 // Must be a constant.
964 if (!CE) return false;
965 int64_t Value = CE->getValue();
966 // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X.
967 return (Value >= 0 && Value < 256) ||
968 (Value >= 0x0100 && Value <= 0xff00) ||
969 (Value >= 0x010000 && Value <= 0xff0000) ||
970 (Value >= 0x01000000 && Value <= 0xff000000);
973 bool isNEONi32vmov() const {
974 if (Kind != k_Immediate)
976 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
977 // Must be a constant.
978 if (!CE) return false;
979 int64_t Value = CE->getValue();
980 // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X,
981 // for VMOV/VMVN only, 00Xf or 0Xff are also accepted.
982 return (Value >= 0 && Value < 256) ||
983 (Value >= 0x0100 && Value <= 0xff00) ||
984 (Value >= 0x010000 && Value <= 0xff0000) ||
985 (Value >= 0x01000000 && Value <= 0xff000000) ||
986 (Value >= 0x01ff && Value <= 0xffff && (Value & 0xff) == 0xff) ||
987 (Value >= 0x01ffff && Value <= 0xffffff && (Value & 0xffff) == 0xffff);
990 bool isNEONi64splat() const {
991 if (Kind != k_Immediate)
993 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
994 // Must be a constant.
995 if (!CE) return false;
996 uint64_t Value = CE->getValue();
997 // i64 value with each byte being either 0 or 0xff.
998 for (unsigned i = 0; i < 8; ++i)
999 if ((Value & 0xff) != 0 && (Value & 0xff) != 0xff) return false;
1003 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
1004 // Add as immediates when possible. Null MCExpr = 0.
1006 Inst.addOperand(MCOperand::CreateImm(0));
1007 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
1008 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
1010 Inst.addOperand(MCOperand::CreateExpr(Expr));
1013 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
1014 assert(N == 2 && "Invalid number of operands!");
1015 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
1016 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
1017 Inst.addOperand(MCOperand::CreateReg(RegNum));
1020 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
1021 assert(N == 1 && "Invalid number of operands!");
1022 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
1025 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
1026 assert(N == 1 && "Invalid number of operands!");
1027 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
1030 void addCoprocOptionOperands(MCInst &Inst, unsigned N) const {
1031 assert(N == 1 && "Invalid number of operands!");
1032 Inst.addOperand(MCOperand::CreateImm(CoprocOption.Val));
1035 void addITMaskOperands(MCInst &Inst, unsigned N) const {
1036 assert(N == 1 && "Invalid number of operands!");
1037 Inst.addOperand(MCOperand::CreateImm(ITMask.Mask));
1040 void addITCondCodeOperands(MCInst &Inst, unsigned N) const {
1041 assert(N == 1 && "Invalid number of operands!");
1042 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
1045 void addCCOutOperands(MCInst &Inst, unsigned N) const {
1046 assert(N == 1 && "Invalid number of operands!");
1047 Inst.addOperand(MCOperand::CreateReg(getReg()));
1050 void addRegOperands(MCInst &Inst, unsigned N) const {
1051 assert(N == 1 && "Invalid number of operands!");
1052 Inst.addOperand(MCOperand::CreateReg(getReg()));
1055 void addRegShiftedRegOperands(MCInst &Inst, unsigned N) const {
1056 assert(N == 3 && "Invalid number of operands!");
1057 assert(isRegShiftedReg() && "addRegShiftedRegOperands() on non RegShiftedReg!");
1058 Inst.addOperand(MCOperand::CreateReg(RegShiftedReg.SrcReg));
1059 Inst.addOperand(MCOperand::CreateReg(RegShiftedReg.ShiftReg));
1060 Inst.addOperand(MCOperand::CreateImm(
1061 ARM_AM::getSORegOpc(RegShiftedReg.ShiftTy, RegShiftedReg.ShiftImm)));
1064 void addRegShiftedImmOperands(MCInst &Inst, unsigned N) const {
1065 assert(N == 2 && "Invalid number of operands!");
1066 assert(isRegShiftedImm() && "addRegShiftedImmOperands() on non RegShiftedImm!");
1067 Inst.addOperand(MCOperand::CreateReg(RegShiftedImm.SrcReg));
1068 Inst.addOperand(MCOperand::CreateImm(
1069 ARM_AM::getSORegOpc(RegShiftedImm.ShiftTy, RegShiftedImm.ShiftImm)));
1072 void addShifterImmOperands(MCInst &Inst, unsigned N) const {
1073 assert(N == 1 && "Invalid number of operands!");
1074 Inst.addOperand(MCOperand::CreateImm((ShifterImm.isASR << 5) |
1078 void addRegListOperands(MCInst &Inst, unsigned N) const {
1079 assert(N == 1 && "Invalid number of operands!");
1080 const SmallVectorImpl<unsigned> &RegList = getRegList();
1081 for (SmallVectorImpl<unsigned>::const_iterator
1082 I = RegList.begin(), E = RegList.end(); I != E; ++I)
1083 Inst.addOperand(MCOperand::CreateReg(*I));
1086 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
1087 addRegListOperands(Inst, N);
1090 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
1091 addRegListOperands(Inst, N);
1094 void addRotImmOperands(MCInst &Inst, unsigned N) const {
1095 assert(N == 1 && "Invalid number of operands!");
1096 // Encoded as val>>3. The printer handles display as 8, 16, 24.
1097 Inst.addOperand(MCOperand::CreateImm(RotImm.Imm >> 3));
1100 void addBitfieldOperands(MCInst &Inst, unsigned N) const {
1101 assert(N == 1 && "Invalid number of operands!");
1102 // Munge the lsb/width into a bitfield mask.
1103 unsigned lsb = Bitfield.LSB;
1104 unsigned width = Bitfield.Width;
1105 // Make a 32-bit mask w/ the referenced bits clear and all other bits set.
1106 uint32_t Mask = ~(((uint32_t)0xffffffff >> lsb) << (32 - width) >>
1107 (32 - (lsb + width)));
1108 Inst.addOperand(MCOperand::CreateImm(Mask));
1111 void addImmOperands(MCInst &Inst, unsigned N) const {
1112 assert(N == 1 && "Invalid number of operands!");
1113 addExpr(Inst, getImm());
1116 void addFPImmOperands(MCInst &Inst, unsigned N) const {
1117 assert(N == 1 && "Invalid number of operands!");
1118 Inst.addOperand(MCOperand::CreateImm(getFPImm()));
1121 void addImm8s4Operands(MCInst &Inst, unsigned N) const {
1122 assert(N == 1 && "Invalid number of operands!");
1123 // FIXME: We really want to scale the value here, but the LDRD/STRD
1124 // instruction don't encode operands that way yet.
1125 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1126 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
1129 void addImm0_1020s4Operands(MCInst &Inst, unsigned N) const {
1130 assert(N == 1 && "Invalid number of operands!");
1131 // The immediate is scaled by four in the encoding and is stored
1132 // in the MCInst as such. Lop off the low two bits here.
1133 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1134 Inst.addOperand(MCOperand::CreateImm(CE->getValue() / 4));
1137 void addImm0_508s4Operands(MCInst &Inst, unsigned N) const {
1138 assert(N == 1 && "Invalid number of operands!");
1139 // The immediate is scaled by four in the encoding and is stored
1140 // in the MCInst as such. Lop off the low two bits here.
1141 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1142 Inst.addOperand(MCOperand::CreateImm(CE->getValue() / 4));
1145 void addImm0_255Operands(MCInst &Inst, unsigned N) const {
1146 assert(N == 1 && "Invalid number of operands!");
1147 addExpr(Inst, getImm());
1150 void addImm0_7Operands(MCInst &Inst, unsigned N) const {
1151 assert(N == 1 && "Invalid number of operands!");
1152 addExpr(Inst, getImm());
1155 void addImm0_15Operands(MCInst &Inst, unsigned N) const {
1156 assert(N == 1 && "Invalid number of operands!");
1157 addExpr(Inst, getImm());
1160 void addImm0_31Operands(MCInst &Inst, unsigned N) const {
1161 assert(N == 1 && "Invalid number of operands!");
1162 addExpr(Inst, getImm());
1165 void addImm1_16Operands(MCInst &Inst, unsigned N) const {
1166 assert(N == 1 && "Invalid number of operands!");
1167 // The constant encodes as the immediate-1, and we store in the instruction
1168 // the bits as encoded, so subtract off one here.
1169 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1170 Inst.addOperand(MCOperand::CreateImm(CE->getValue() - 1));
1173 void addImm1_32Operands(MCInst &Inst, unsigned N) const {
1174 assert(N == 1 && "Invalid number of operands!");
1175 // The constant encodes as the immediate-1, and we store in the instruction
1176 // the bits as encoded, so subtract off one here.
1177 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1178 Inst.addOperand(MCOperand::CreateImm(CE->getValue() - 1));
1181 void addImm0_65535Operands(MCInst &Inst, unsigned N) const {
1182 assert(N == 1 && "Invalid number of operands!");
1183 addExpr(Inst, getImm());
1186 void addImm0_65535ExprOperands(MCInst &Inst, unsigned N) const {
1187 assert(N == 1 && "Invalid number of operands!");
1188 addExpr(Inst, getImm());
1191 void addImm24bitOperands(MCInst &Inst, unsigned N) const {
1192 assert(N == 1 && "Invalid number of operands!");
1193 addExpr(Inst, getImm());
1196 void addImmThumbSROperands(MCInst &Inst, unsigned N) const {
1197 assert(N == 1 && "Invalid number of operands!");
1198 // The constant encodes as the immediate, except for 32, which encodes as
1200 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1201 unsigned Imm = CE->getValue();
1202 Inst.addOperand(MCOperand::CreateImm((Imm == 32 ? 0 : Imm)));
1205 void addPKHLSLImmOperands(MCInst &Inst, unsigned N) const {
1206 assert(N == 1 && "Invalid number of operands!");
1207 addExpr(Inst, getImm());
1210 void addPKHASRImmOperands(MCInst &Inst, unsigned N) const {
1211 assert(N == 1 && "Invalid number of operands!");
1212 // An ASR value of 32 encodes as 0, so that's how we want to add it to
1213 // the instruction as well.
1214 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1215 int Val = CE->getValue();
1216 Inst.addOperand(MCOperand::CreateImm(Val == 32 ? 0 : Val));
1219 void addARMSOImmOperands(MCInst &Inst, unsigned N) const {
1220 assert(N == 1 && "Invalid number of operands!");
1221 addExpr(Inst, getImm());
1224 void addT2SOImmOperands(MCInst &Inst, unsigned N) const {
1225 assert(N == 1 && "Invalid number of operands!");
1226 addExpr(Inst, getImm());
1229 void addSetEndImmOperands(MCInst &Inst, unsigned N) const {
1230 assert(N == 1 && "Invalid number of operands!");
1231 addExpr(Inst, getImm());
1234 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
1235 assert(N == 1 && "Invalid number of operands!");
1236 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
1239 void addMemNoOffsetOperands(MCInst &Inst, unsigned N) const {
1240 assert(N == 1 && "Invalid number of operands!");
1241 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1244 void addAlignedMemoryOperands(MCInst &Inst, unsigned N) const {
1245 assert(N == 2 && "Invalid number of operands!");
1246 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1247 Inst.addOperand(MCOperand::CreateImm(Memory.Alignment));
1250 void addAddrMode2Operands(MCInst &Inst, unsigned N) const {
1251 assert(N == 3 && "Invalid number of operands!");
1252 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1253 if (!Memory.OffsetRegNum) {
1254 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1255 // Special case for #-0
1256 if (Val == INT32_MIN) Val = 0;
1257 if (Val < 0) Val = -Val;
1258 Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
1260 // For register offset, we encode the shift type and negation flag
1262 Val = ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
1263 Memory.ShiftImm, Memory.ShiftType);
1265 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1266 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1267 Inst.addOperand(MCOperand::CreateImm(Val));
1270 void addAM2OffsetImmOperands(MCInst &Inst, unsigned N) const {
1271 assert(N == 2 && "Invalid number of operands!");
1272 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1273 assert(CE && "non-constant AM2OffsetImm operand!");
1274 int32_t Val = CE->getValue();
1275 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1276 // Special case for #-0
1277 if (Val == INT32_MIN) Val = 0;
1278 if (Val < 0) Val = -Val;
1279 Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
1280 Inst.addOperand(MCOperand::CreateReg(0));
1281 Inst.addOperand(MCOperand::CreateImm(Val));
1284 void addAddrMode3Operands(MCInst &Inst, unsigned N) const {
1285 assert(N == 3 && "Invalid number of operands!");
1286 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1287 if (!Memory.OffsetRegNum) {
1288 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1289 // Special case for #-0
1290 if (Val == INT32_MIN) Val = 0;
1291 if (Val < 0) Val = -Val;
1292 Val = ARM_AM::getAM3Opc(AddSub, Val);
1294 // For register offset, we encode the shift type and negation flag
1296 Val = ARM_AM::getAM3Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add, 0);
1298 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1299 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1300 Inst.addOperand(MCOperand::CreateImm(Val));
1303 void addAM3OffsetOperands(MCInst &Inst, unsigned N) const {
1304 assert(N == 2 && "Invalid number of operands!");
1305 if (Kind == k_PostIndexRegister) {
1307 ARM_AM::getAM3Opc(PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub, 0);
1308 Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1309 Inst.addOperand(MCOperand::CreateImm(Val));
1314 const MCConstantExpr *CE = static_cast<const MCConstantExpr*>(getImm());
1315 int32_t Val = CE->getValue();
1316 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1317 // Special case for #-0
1318 if (Val == INT32_MIN) Val = 0;
1319 if (Val < 0) Val = -Val;
1320 Val = ARM_AM::getAM3Opc(AddSub, Val);
1321 Inst.addOperand(MCOperand::CreateReg(0));
1322 Inst.addOperand(MCOperand::CreateImm(Val));
1325 void addAddrMode5Operands(MCInst &Inst, unsigned N) const {
1326 assert(N == 2 && "Invalid number of operands!");
1327 // The lower two bits are always zero and as such are not encoded.
1328 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
1329 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1330 // Special case for #-0
1331 if (Val == INT32_MIN) Val = 0;
1332 if (Val < 0) Val = -Val;
1333 Val = ARM_AM::getAM5Opc(AddSub, Val);
1334 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1335 Inst.addOperand(MCOperand::CreateImm(Val));
1338 void addMemImm8s4OffsetOperands(MCInst &Inst, unsigned N) const {
1339 assert(N == 2 && "Invalid number of operands!");
1340 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1341 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1342 Inst.addOperand(MCOperand::CreateImm(Val));
1345 void addMemImm0_1020s4OffsetOperands(MCInst &Inst, unsigned N) const {
1346 assert(N == 2 && "Invalid number of operands!");
1347 // The lower two bits are always zero and as such are not encoded.
1348 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
1349 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1350 Inst.addOperand(MCOperand::CreateImm(Val));
1353 void addMemImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1354 assert(N == 2 && "Invalid number of operands!");
1355 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1356 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1357 Inst.addOperand(MCOperand::CreateImm(Val));
1360 void addMemPosImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1361 addMemImm8OffsetOperands(Inst, N);
1364 void addMemNegImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1365 addMemImm8OffsetOperands(Inst, N);
1368 void addMemUImm12OffsetOperands(MCInst &Inst, unsigned N) const {
1369 assert(N == 2 && "Invalid number of operands!");
1370 // If this is an immediate, it's a label reference.
1371 if (Kind == k_Immediate) {
1372 addExpr(Inst, getImm());
1373 Inst.addOperand(MCOperand::CreateImm(0));
1377 // Otherwise, it's a normal memory reg+offset.
1378 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1379 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1380 Inst.addOperand(MCOperand::CreateImm(Val));
1383 void addMemImm12OffsetOperands(MCInst &Inst, unsigned N) const {
1384 assert(N == 2 && "Invalid number of operands!");
1385 // If this is an immediate, it's a label reference.
1386 if (Kind == k_Immediate) {
1387 addExpr(Inst, getImm());
1388 Inst.addOperand(MCOperand::CreateImm(0));
1392 // Otherwise, it's a normal memory reg+offset.
1393 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1394 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1395 Inst.addOperand(MCOperand::CreateImm(Val));
1398 void addMemTBBOperands(MCInst &Inst, unsigned N) const {
1399 assert(N == 2 && "Invalid number of operands!");
1400 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1401 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1404 void addMemTBHOperands(MCInst &Inst, unsigned N) const {
1405 assert(N == 2 && "Invalid number of operands!");
1406 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1407 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1410 void addMemRegOffsetOperands(MCInst &Inst, unsigned N) const {
1411 assert(N == 3 && "Invalid number of operands!");
1412 unsigned Val = ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
1413 Memory.ShiftImm, Memory.ShiftType);
1414 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1415 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1416 Inst.addOperand(MCOperand::CreateImm(Val));
1419 void addT2MemRegOffsetOperands(MCInst &Inst, unsigned N) const {
1420 assert(N == 3 && "Invalid number of operands!");
1421 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1422 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1423 Inst.addOperand(MCOperand::CreateImm(Memory.ShiftImm));
1426 void addMemThumbRROperands(MCInst &Inst, unsigned N) const {
1427 assert(N == 2 && "Invalid number of operands!");
1428 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1429 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1432 void addMemThumbRIs4Operands(MCInst &Inst, unsigned N) const {
1433 assert(N == 2 && "Invalid number of operands!");
1434 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
1435 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1436 Inst.addOperand(MCOperand::CreateImm(Val));
1439 void addMemThumbRIs2Operands(MCInst &Inst, unsigned N) const {
1440 assert(N == 2 && "Invalid number of operands!");
1441 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 2) : 0;
1442 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1443 Inst.addOperand(MCOperand::CreateImm(Val));
1446 void addMemThumbRIs1Operands(MCInst &Inst, unsigned N) const {
1447 assert(N == 2 && "Invalid number of operands!");
1448 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue()) : 0;
1449 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1450 Inst.addOperand(MCOperand::CreateImm(Val));
1453 void addMemThumbSPIOperands(MCInst &Inst, unsigned N) const {
1454 assert(N == 2 && "Invalid number of operands!");
1455 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
1456 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1457 Inst.addOperand(MCOperand::CreateImm(Val));
1460 void addPostIdxImm8Operands(MCInst &Inst, unsigned N) const {
1461 assert(N == 1 && "Invalid number of operands!");
1462 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1463 assert(CE && "non-constant post-idx-imm8 operand!");
1464 int Imm = CE->getValue();
1465 bool isAdd = Imm >= 0;
1466 if (Imm == INT32_MIN) Imm = 0;
1467 Imm = (Imm < 0 ? -Imm : Imm) | (int)isAdd << 8;
1468 Inst.addOperand(MCOperand::CreateImm(Imm));
1471 void addPostIdxImm8s4Operands(MCInst &Inst, unsigned N) const {
1472 assert(N == 1 && "Invalid number of operands!");
1473 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1474 assert(CE && "non-constant post-idx-imm8s4 operand!");
1475 int Imm = CE->getValue();
1476 bool isAdd = Imm >= 0;
1477 if (Imm == INT32_MIN) Imm = 0;
1478 // Immediate is scaled by 4.
1479 Imm = ((Imm < 0 ? -Imm : Imm) / 4) | (int)isAdd << 8;
1480 Inst.addOperand(MCOperand::CreateImm(Imm));
1483 void addPostIdxRegOperands(MCInst &Inst, unsigned N) const {
1484 assert(N == 2 && "Invalid number of operands!");
1485 Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1486 Inst.addOperand(MCOperand::CreateImm(PostIdxReg.isAdd));
1489 void addPostIdxRegShiftedOperands(MCInst &Inst, unsigned N) const {
1490 assert(N == 2 && "Invalid number of operands!");
1491 Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1492 // The sign, shift type, and shift amount are encoded in a single operand
1493 // using the AM2 encoding helpers.
1494 ARM_AM::AddrOpc opc = PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub;
1495 unsigned Imm = ARM_AM::getAM2Opc(opc, PostIdxReg.ShiftImm,
1496 PostIdxReg.ShiftTy);
1497 Inst.addOperand(MCOperand::CreateImm(Imm));
1500 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
1501 assert(N == 1 && "Invalid number of operands!");
1502 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
1505 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
1506 assert(N == 1 && "Invalid number of operands!");
1507 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
1510 void addVecListOneDOperands(MCInst &Inst, unsigned N) const {
1511 assert(N == 1 && "Invalid number of operands!");
1512 Inst.addOperand(MCOperand::CreateReg(VectorList.RegNum));
1515 void addVecListTwoDOperands(MCInst &Inst, unsigned N) const {
1516 assert(N == 1 && "Invalid number of operands!");
1517 // Only the first register actually goes on the instruction. The rest
1518 // are implied by the opcode.
1519 Inst.addOperand(MCOperand::CreateReg(VectorList.RegNum));
1522 void addVectorIndex8Operands(MCInst &Inst, unsigned N) const {
1523 assert(N == 1 && "Invalid number of operands!");
1524 Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1527 void addVectorIndex16Operands(MCInst &Inst, unsigned N) const {
1528 assert(N == 1 && "Invalid number of operands!");
1529 Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1532 void addVectorIndex32Operands(MCInst &Inst, unsigned N) const {
1533 assert(N == 1 && "Invalid number of operands!");
1534 Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1537 void addNEONi8splatOperands(MCInst &Inst, unsigned N) const {
1538 assert(N == 1 && "Invalid number of operands!");
1539 // The immediate encodes the type of constant as well as the value.
1540 // Mask in that this is an i8 splat.
1541 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1542 Inst.addOperand(MCOperand::CreateImm(CE->getValue() | 0xe00));
1545 void addNEONi16splatOperands(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();
1551 Value = (Value >> 8) | 0xa00;
1554 Inst.addOperand(MCOperand::CreateImm(Value));
1557 void addNEONi32splatOperands(MCInst &Inst, unsigned N) const {
1558 assert(N == 1 && "Invalid number of operands!");
1559 // The immediate encodes the type of constant as well as the value.
1560 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1561 unsigned Value = CE->getValue();
1562 if (Value >= 256 && Value <= 0xff00)
1563 Value = (Value >> 8) | 0x200;
1564 else if (Value > 0xffff && Value <= 0xff0000)
1565 Value = (Value >> 16) | 0x400;
1566 else if (Value > 0xffffff)
1567 Value = (Value >> 24) | 0x600;
1568 Inst.addOperand(MCOperand::CreateImm(Value));
1571 void addNEONi32vmovOperands(MCInst &Inst, unsigned N) const {
1572 assert(N == 1 && "Invalid number of operands!");
1573 // The immediate encodes the type of constant as well as the value.
1574 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1575 unsigned Value = CE->getValue();
1576 if (Value >= 256 && Value <= 0xffff)
1577 Value = (Value >> 8) | ((Value & 0xff) ? 0xc00 : 0x200);
1578 else if (Value > 0xffff && Value <= 0xffffff)
1579 Value = (Value >> 16) | ((Value & 0xff) ? 0xd00 : 0x400);
1580 else if (Value > 0xffffff)
1581 Value = (Value >> 24) | 0x600;
1582 Inst.addOperand(MCOperand::CreateImm(Value));
1585 void addNEONi64splatOperands(MCInst &Inst, unsigned N) const {
1586 assert(N == 1 && "Invalid number of operands!");
1587 // The immediate encodes the type of constant as well as the value.
1588 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1589 uint64_t Value = CE->getValue();
1591 for (unsigned i = 0; i < 8; ++i, Value >>= 8) {
1592 Imm |= (Value & 1) << i;
1594 Inst.addOperand(MCOperand::CreateImm(Imm | 0x1e00));
1597 virtual void print(raw_ostream &OS) const;
1599 static ARMOperand *CreateITMask(unsigned Mask, SMLoc S) {
1600 ARMOperand *Op = new ARMOperand(k_ITCondMask);
1601 Op->ITMask.Mask = Mask;
1607 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
1608 ARMOperand *Op = new ARMOperand(k_CondCode);
1615 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
1616 ARMOperand *Op = new ARMOperand(k_CoprocNum);
1617 Op->Cop.Val = CopVal;
1623 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
1624 ARMOperand *Op = new ARMOperand(k_CoprocReg);
1625 Op->Cop.Val = CopVal;
1631 static ARMOperand *CreateCoprocOption(unsigned Val, SMLoc S, SMLoc E) {
1632 ARMOperand *Op = new ARMOperand(k_CoprocOption);
1639 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
1640 ARMOperand *Op = new ARMOperand(k_CCOut);
1641 Op->Reg.RegNum = RegNum;
1647 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
1648 ARMOperand *Op = new ARMOperand(k_Token);
1649 Op->Tok.Data = Str.data();
1650 Op->Tok.Length = Str.size();
1656 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
1657 ARMOperand *Op = new ARMOperand(k_Register);
1658 Op->Reg.RegNum = RegNum;
1664 static ARMOperand *CreateShiftedRegister(ARM_AM::ShiftOpc ShTy,
1669 ARMOperand *Op = new ARMOperand(k_ShiftedRegister);
1670 Op->RegShiftedReg.ShiftTy = ShTy;
1671 Op->RegShiftedReg.SrcReg = SrcReg;
1672 Op->RegShiftedReg.ShiftReg = ShiftReg;
1673 Op->RegShiftedReg.ShiftImm = ShiftImm;
1679 static ARMOperand *CreateShiftedImmediate(ARM_AM::ShiftOpc ShTy,
1683 ARMOperand *Op = new ARMOperand(k_ShiftedImmediate);
1684 Op->RegShiftedImm.ShiftTy = ShTy;
1685 Op->RegShiftedImm.SrcReg = SrcReg;
1686 Op->RegShiftedImm.ShiftImm = ShiftImm;
1692 static ARMOperand *CreateShifterImm(bool isASR, unsigned Imm,
1694 ARMOperand *Op = new ARMOperand(k_ShifterImmediate);
1695 Op->ShifterImm.isASR = isASR;
1696 Op->ShifterImm.Imm = Imm;
1702 static ARMOperand *CreateRotImm(unsigned Imm, SMLoc S, SMLoc E) {
1703 ARMOperand *Op = new ARMOperand(k_RotateImmediate);
1704 Op->RotImm.Imm = Imm;
1710 static ARMOperand *CreateBitfield(unsigned LSB, unsigned Width,
1712 ARMOperand *Op = new ARMOperand(k_BitfieldDescriptor);
1713 Op->Bitfield.LSB = LSB;
1714 Op->Bitfield.Width = Width;
1721 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
1722 SMLoc StartLoc, SMLoc EndLoc) {
1723 KindTy Kind = k_RegisterList;
1725 if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Regs.front().first))
1726 Kind = k_DPRRegisterList;
1727 else if (ARMMCRegisterClasses[ARM::SPRRegClassID].
1728 contains(Regs.front().first))
1729 Kind = k_SPRRegisterList;
1731 ARMOperand *Op = new ARMOperand(Kind);
1732 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
1733 I = Regs.begin(), E = Regs.end(); I != E; ++I)
1734 Op->Registers.push_back(I->first);
1735 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
1736 Op->StartLoc = StartLoc;
1737 Op->EndLoc = EndLoc;
1741 static ARMOperand *CreateVectorList(unsigned RegNum, unsigned Count,
1743 ARMOperand *Op = new ARMOperand(k_VectorList);
1744 Op->VectorList.RegNum = RegNum;
1745 Op->VectorList.Count = Count;
1751 static ARMOperand *CreateVectorIndex(unsigned Idx, SMLoc S, SMLoc E,
1753 ARMOperand *Op = new ARMOperand(k_VectorIndex);
1754 Op->VectorIndex.Val = Idx;
1760 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
1761 ARMOperand *Op = new ARMOperand(k_Immediate);
1768 static ARMOperand *CreateFPImm(unsigned Val, SMLoc S, MCContext &Ctx) {
1769 ARMOperand *Op = new ARMOperand(k_FPImmediate);
1770 Op->FPImm.Val = Val;
1776 static ARMOperand *CreateMem(unsigned BaseRegNum,
1777 const MCConstantExpr *OffsetImm,
1778 unsigned OffsetRegNum,
1779 ARM_AM::ShiftOpc ShiftType,
1784 ARMOperand *Op = new ARMOperand(k_Memory);
1785 Op->Memory.BaseRegNum = BaseRegNum;
1786 Op->Memory.OffsetImm = OffsetImm;
1787 Op->Memory.OffsetRegNum = OffsetRegNum;
1788 Op->Memory.ShiftType = ShiftType;
1789 Op->Memory.ShiftImm = ShiftImm;
1790 Op->Memory.Alignment = Alignment;
1791 Op->Memory.isNegative = isNegative;
1797 static ARMOperand *CreatePostIdxReg(unsigned RegNum, bool isAdd,
1798 ARM_AM::ShiftOpc ShiftTy,
1801 ARMOperand *Op = new ARMOperand(k_PostIndexRegister);
1802 Op->PostIdxReg.RegNum = RegNum;
1803 Op->PostIdxReg.isAdd = isAdd;
1804 Op->PostIdxReg.ShiftTy = ShiftTy;
1805 Op->PostIdxReg.ShiftImm = ShiftImm;
1811 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
1812 ARMOperand *Op = new ARMOperand(k_MemBarrierOpt);
1813 Op->MBOpt.Val = Opt;
1819 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
1820 ARMOperand *Op = new ARMOperand(k_ProcIFlags);
1821 Op->IFlags.Val = IFlags;
1827 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
1828 ARMOperand *Op = new ARMOperand(k_MSRMask);
1829 Op->MMask.Val = MMask;
1836 } // end anonymous namespace.
1838 void ARMOperand::print(raw_ostream &OS) const {
1841 OS << "<fpimm " << getFPImm() << "(" << ARM_AM::getFPImmFloat(getFPImm())
1845 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
1848 OS << "<ccout " << getReg() << ">";
1850 case k_ITCondMask: {
1851 static char MaskStr[][6] = { "()", "(t)", "(e)", "(tt)", "(et)", "(te)",
1852 "(ee)", "(ttt)", "(ett)", "(tet)", "(eet)", "(tte)", "(ete)",
1854 assert((ITMask.Mask & 0xf) == ITMask.Mask);
1855 OS << "<it-mask " << MaskStr[ITMask.Mask] << ">";
1859 OS << "<coprocessor number: " << getCoproc() << ">";
1862 OS << "<coprocessor register: " << getCoproc() << ">";
1864 case k_CoprocOption:
1865 OS << "<coprocessor option: " << CoprocOption.Val << ">";
1868 OS << "<mask: " << getMSRMask() << ">";
1871 getImm()->print(OS);
1873 case k_MemBarrierOpt:
1874 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
1878 << " base:" << Memory.BaseRegNum;
1881 case k_PostIndexRegister:
1882 OS << "post-idx register " << (PostIdxReg.isAdd ? "" : "-")
1883 << PostIdxReg.RegNum;
1884 if (PostIdxReg.ShiftTy != ARM_AM::no_shift)
1885 OS << ARM_AM::getShiftOpcStr(PostIdxReg.ShiftTy) << " "
1886 << PostIdxReg.ShiftImm;
1889 case k_ProcIFlags: {
1890 OS << "<ARM_PROC::";
1891 unsigned IFlags = getProcIFlags();
1892 for (int i=2; i >= 0; --i)
1893 if (IFlags & (1 << i))
1894 OS << ARM_PROC::IFlagsToString(1 << i);
1899 OS << "<register " << getReg() << ">";
1901 case k_ShifterImmediate:
1902 OS << "<shift " << (ShifterImm.isASR ? "asr" : "lsl")
1903 << " #" << ShifterImm.Imm << ">";
1905 case k_ShiftedRegister:
1906 OS << "<so_reg_reg "
1907 << RegShiftedReg.SrcReg
1908 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(RegShiftedReg.ShiftImm))
1909 << ", " << RegShiftedReg.ShiftReg << ", "
1910 << ARM_AM::getSORegOffset(RegShiftedReg.ShiftImm)
1913 case k_ShiftedImmediate:
1914 OS << "<so_reg_imm "
1915 << RegShiftedImm.SrcReg
1916 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(RegShiftedImm.ShiftImm))
1917 << ", " << ARM_AM::getSORegOffset(RegShiftedImm.ShiftImm)
1920 case k_RotateImmediate:
1921 OS << "<ror " << " #" << (RotImm.Imm * 8) << ">";
1923 case k_BitfieldDescriptor:
1924 OS << "<bitfield " << "lsb: " << Bitfield.LSB
1925 << ", width: " << Bitfield.Width << ">";
1927 case k_RegisterList:
1928 case k_DPRRegisterList:
1929 case k_SPRRegisterList: {
1930 OS << "<register_list ";
1932 const SmallVectorImpl<unsigned> &RegList = getRegList();
1933 for (SmallVectorImpl<unsigned>::const_iterator
1934 I = RegList.begin(), E = RegList.end(); I != E; ) {
1936 if (++I < E) OS << ", ";
1943 OS << "<vector_list " << VectorList.Count << " * "
1944 << VectorList.RegNum << ">";
1947 OS << "'" << getToken() << "'";
1950 OS << "<vectorindex " << getVectorIndex() << ">";
1955 /// @name Auto-generated Match Functions
1958 static unsigned MatchRegisterName(StringRef Name);
1962 bool ARMAsmParser::ParseRegister(unsigned &RegNo,
1963 SMLoc &StartLoc, SMLoc &EndLoc) {
1964 RegNo = tryParseRegister();
1966 return (RegNo == (unsigned)-1);
1969 /// Try to parse a register name. The token must be an Identifier when called,
1970 /// and if it is a register name the token is eaten and the register number is
1971 /// returned. Otherwise return -1.
1973 int ARMAsmParser::tryParseRegister() {
1974 const AsmToken &Tok = Parser.getTok();
1975 if (Tok.isNot(AsmToken::Identifier)) return -1;
1977 // FIXME: Validate register for the current architecture; we have to do
1978 // validation later, so maybe there is no need for this here.
1979 std::string upperCase = Tok.getString().str();
1980 std::string lowerCase = LowercaseString(upperCase);
1981 unsigned RegNum = MatchRegisterName(lowerCase);
1983 RegNum = StringSwitch<unsigned>(lowerCase)
1984 .Case("r13", ARM::SP)
1985 .Case("r14", ARM::LR)
1986 .Case("r15", ARM::PC)
1987 .Case("ip", ARM::R12)
1990 if (!RegNum) return -1;
1992 Parser.Lex(); // Eat identifier token.
1997 // Try to parse a shifter (e.g., "lsl <amt>"). On success, return 0.
1998 // If a recoverable error occurs, return 1. If an irrecoverable error
1999 // occurs, return -1. An irrecoverable error is one where tokens have been
2000 // consumed in the process of trying to parse the shifter (i.e., when it is
2001 // indeed a shifter operand, but malformed).
2002 int ARMAsmParser::tryParseShiftRegister(
2003 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2004 SMLoc S = Parser.getTok().getLoc();
2005 const AsmToken &Tok = Parser.getTok();
2006 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2008 std::string upperCase = Tok.getString().str();
2009 std::string lowerCase = LowercaseString(upperCase);
2010 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
2011 .Case("lsl", ARM_AM::lsl)
2012 .Case("lsr", ARM_AM::lsr)
2013 .Case("asr", ARM_AM::asr)
2014 .Case("ror", ARM_AM::ror)
2015 .Case("rrx", ARM_AM::rrx)
2016 .Default(ARM_AM::no_shift);
2018 if (ShiftTy == ARM_AM::no_shift)
2021 Parser.Lex(); // Eat the operator.
2023 // The source register for the shift has already been added to the
2024 // operand list, so we need to pop it off and combine it into the shifted
2025 // register operand instead.
2026 OwningPtr<ARMOperand> PrevOp((ARMOperand*)Operands.pop_back_val());
2027 if (!PrevOp->isReg())
2028 return Error(PrevOp->getStartLoc(), "shift must be of a register");
2029 int SrcReg = PrevOp->getReg();
2032 if (ShiftTy == ARM_AM::rrx) {
2033 // RRX Doesn't have an explicit shift amount. The encoder expects
2034 // the shift register to be the same as the source register. Seems odd,
2038 // Figure out if this is shifted by a constant or a register (for non-RRX).
2039 if (Parser.getTok().is(AsmToken::Hash)) {
2040 Parser.Lex(); // Eat hash.
2041 SMLoc ImmLoc = Parser.getTok().getLoc();
2042 const MCExpr *ShiftExpr = 0;
2043 if (getParser().ParseExpression(ShiftExpr)) {
2044 Error(ImmLoc, "invalid immediate shift value");
2047 // The expression must be evaluatable as an immediate.
2048 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
2050 Error(ImmLoc, "invalid immediate shift value");
2053 // Range check the immediate.
2054 // lsl, ror: 0 <= imm <= 31
2055 // lsr, asr: 0 <= imm <= 32
2056 Imm = CE->getValue();
2058 ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
2059 ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
2060 Error(ImmLoc, "immediate shift value out of range");
2063 } else if (Parser.getTok().is(AsmToken::Identifier)) {
2064 ShiftReg = tryParseRegister();
2065 SMLoc L = Parser.getTok().getLoc();
2066 if (ShiftReg == -1) {
2067 Error (L, "expected immediate or register in shift operand");
2071 Error (Parser.getTok().getLoc(),
2072 "expected immediate or register in shift operand");
2077 if (ShiftReg && ShiftTy != ARM_AM::rrx)
2078 Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
2080 S, Parser.getTok().getLoc()));
2082 Operands.push_back(ARMOperand::CreateShiftedImmediate(ShiftTy, SrcReg, Imm,
2083 S, Parser.getTok().getLoc()));
2089 /// Try to parse a register name. The token must be an Identifier when called.
2090 /// If it's a register, an AsmOperand is created. Another AsmOperand is created
2091 /// if there is a "writeback". 'true' if it's not a register.
2093 /// TODO this is likely to change to allow different register types and or to
2094 /// parse for a specific register type.
2096 tryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2097 SMLoc S = Parser.getTok().getLoc();
2098 int RegNo = tryParseRegister();
2102 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
2104 const AsmToken &ExclaimTok = Parser.getTok();
2105 if (ExclaimTok.is(AsmToken::Exclaim)) {
2106 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
2107 ExclaimTok.getLoc()));
2108 Parser.Lex(); // Eat exclaim token
2112 // Also check for an index operand. This is only legal for vector registers,
2113 // but that'll get caught OK in operand matching, so we don't need to
2114 // explicitly filter everything else out here.
2115 if (Parser.getTok().is(AsmToken::LBrac)) {
2116 SMLoc SIdx = Parser.getTok().getLoc();
2117 Parser.Lex(); // Eat left bracket token.
2119 const MCExpr *ImmVal;
2120 if (getParser().ParseExpression(ImmVal))
2121 return MatchOperand_ParseFail;
2122 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(ImmVal);
2124 TokError("immediate value expected for vector index");
2125 return MatchOperand_ParseFail;
2128 SMLoc E = Parser.getTok().getLoc();
2129 if (Parser.getTok().isNot(AsmToken::RBrac)) {
2130 Error(E, "']' expected");
2131 return MatchOperand_ParseFail;
2134 Parser.Lex(); // Eat right bracket token.
2136 Operands.push_back(ARMOperand::CreateVectorIndex(MCE->getValue(),
2144 /// MatchCoprocessorOperandName - Try to parse an coprocessor related
2145 /// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
2147 static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
2148 // Use the same layout as the tablegen'erated register name matcher. Ugly,
2150 switch (Name.size()) {
2153 if (Name[0] != CoprocOp)
2170 if (Name[0] != CoprocOp || Name[1] != '1')
2174 case '0': return 10;
2175 case '1': return 11;
2176 case '2': return 12;
2177 case '3': return 13;
2178 case '4': return 14;
2179 case '5': return 15;
2187 /// parseITCondCode - Try to parse a condition code for an IT instruction.
2188 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2189 parseITCondCode(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2190 SMLoc S = Parser.getTok().getLoc();
2191 const AsmToken &Tok = Parser.getTok();
2192 if (!Tok.is(AsmToken::Identifier))
2193 return MatchOperand_NoMatch;
2194 unsigned CC = StringSwitch<unsigned>(Tok.getString())
2195 .Case("eq", ARMCC::EQ)
2196 .Case("ne", ARMCC::NE)
2197 .Case("hs", ARMCC::HS)
2198 .Case("cs", ARMCC::HS)
2199 .Case("lo", ARMCC::LO)
2200 .Case("cc", ARMCC::LO)
2201 .Case("mi", ARMCC::MI)
2202 .Case("pl", ARMCC::PL)
2203 .Case("vs", ARMCC::VS)
2204 .Case("vc", ARMCC::VC)
2205 .Case("hi", ARMCC::HI)
2206 .Case("ls", ARMCC::LS)
2207 .Case("ge", ARMCC::GE)
2208 .Case("lt", ARMCC::LT)
2209 .Case("gt", ARMCC::GT)
2210 .Case("le", ARMCC::LE)
2211 .Case("al", ARMCC::AL)
2214 return MatchOperand_NoMatch;
2215 Parser.Lex(); // Eat the token.
2217 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), S));
2219 return MatchOperand_Success;
2222 /// parseCoprocNumOperand - Try to parse an coprocessor number operand. The
2223 /// token must be an Identifier when called, and if it is a coprocessor
2224 /// number, the token is eaten and the operand is added to the operand list.
2225 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2226 parseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2227 SMLoc S = Parser.getTok().getLoc();
2228 const AsmToken &Tok = Parser.getTok();
2229 if (Tok.isNot(AsmToken::Identifier))
2230 return MatchOperand_NoMatch;
2232 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
2234 return MatchOperand_NoMatch;
2236 Parser.Lex(); // Eat identifier token.
2237 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
2238 return MatchOperand_Success;
2241 /// parseCoprocRegOperand - Try to parse an coprocessor register operand. The
2242 /// token must be an Identifier when called, and if it is a coprocessor
2243 /// number, the token is eaten and the operand is added to the operand list.
2244 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2245 parseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2246 SMLoc S = Parser.getTok().getLoc();
2247 const AsmToken &Tok = Parser.getTok();
2248 if (Tok.isNot(AsmToken::Identifier))
2249 return MatchOperand_NoMatch;
2251 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
2253 return MatchOperand_NoMatch;
2255 Parser.Lex(); // Eat identifier token.
2256 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
2257 return MatchOperand_Success;
2260 /// parseCoprocOptionOperand - Try to parse an coprocessor option operand.
2261 /// coproc_option : '{' imm0_255 '}'
2262 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2263 parseCoprocOptionOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2264 SMLoc S = Parser.getTok().getLoc();
2266 // If this isn't a '{', this isn't a coprocessor immediate operand.
2267 if (Parser.getTok().isNot(AsmToken::LCurly))
2268 return MatchOperand_NoMatch;
2269 Parser.Lex(); // Eat the '{'
2272 SMLoc Loc = Parser.getTok().getLoc();
2273 if (getParser().ParseExpression(Expr)) {
2274 Error(Loc, "illegal expression");
2275 return MatchOperand_ParseFail;
2277 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
2278 if (!CE || CE->getValue() < 0 || CE->getValue() > 255) {
2279 Error(Loc, "coprocessor option must be an immediate in range [0, 255]");
2280 return MatchOperand_ParseFail;
2282 int Val = CE->getValue();
2284 // Check for and consume the closing '}'
2285 if (Parser.getTok().isNot(AsmToken::RCurly))
2286 return MatchOperand_ParseFail;
2287 SMLoc E = Parser.getTok().getLoc();
2288 Parser.Lex(); // Eat the '}'
2290 Operands.push_back(ARMOperand::CreateCoprocOption(Val, S, E));
2291 return MatchOperand_Success;
2294 // For register list parsing, we need to map from raw GPR register numbering
2295 // to the enumeration values. The enumeration values aren't sorted by
2296 // register number due to our using "sp", "lr" and "pc" as canonical names.
2297 static unsigned getNextRegister(unsigned Reg) {
2298 // If this is a GPR, we need to do it manually, otherwise we can rely
2299 // on the sort ordering of the enumeration since the other reg-classes
2301 if (!ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
2304 default: assert(0 && "Invalid GPR number!");
2305 case ARM::R0: return ARM::R1; case ARM::R1: return ARM::R2;
2306 case ARM::R2: return ARM::R3; case ARM::R3: return ARM::R4;
2307 case ARM::R4: return ARM::R5; case ARM::R5: return ARM::R6;
2308 case ARM::R6: return ARM::R7; case ARM::R7: return ARM::R8;
2309 case ARM::R8: return ARM::R9; case ARM::R9: return ARM::R10;
2310 case ARM::R10: return ARM::R11; case ARM::R11: return ARM::R12;
2311 case ARM::R12: return ARM::SP; case ARM::SP: return ARM::LR;
2312 case ARM::LR: return ARM::PC; case ARM::PC: return ARM::R0;
2316 /// Parse a register list.
2318 parseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2319 assert(Parser.getTok().is(AsmToken::LCurly) &&
2320 "Token is not a Left Curly Brace");
2321 SMLoc S = Parser.getTok().getLoc();
2322 Parser.Lex(); // Eat '{' token.
2323 SMLoc RegLoc = Parser.getTok().getLoc();
2325 // Check the first register in the list to see what register class
2326 // this is a list of.
2327 int Reg = tryParseRegister();
2329 return Error(RegLoc, "register expected");
2331 MCRegisterClass *RC;
2332 if (ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
2333 RC = &ARMMCRegisterClasses[ARM::GPRRegClassID];
2334 else if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg))
2335 RC = &ARMMCRegisterClasses[ARM::DPRRegClassID];
2336 else if (ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg))
2337 RC = &ARMMCRegisterClasses[ARM::SPRRegClassID];
2339 return Error(RegLoc, "invalid register in register list");
2341 // The reglist instructions have at most 16 registers, so reserve
2342 // space for that many.
2343 SmallVector<std::pair<unsigned, SMLoc>, 16> Registers;
2344 // Store the first register.
2345 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2347 // This starts immediately after the first register token in the list,
2348 // so we can see either a comma or a minus (range separator) as a legal
2350 while (Parser.getTok().is(AsmToken::Comma) ||
2351 Parser.getTok().is(AsmToken::Minus)) {
2352 if (Parser.getTok().is(AsmToken::Minus)) {
2353 Parser.Lex(); // Eat the comma.
2354 SMLoc EndLoc = Parser.getTok().getLoc();
2355 int EndReg = tryParseRegister();
2357 return Error(EndLoc, "register expected");
2358 // If the register is the same as the start reg, there's nothing
2362 // The register must be in the same register class as the first.
2363 if (!RC->contains(EndReg))
2364 return Error(EndLoc, "invalid register in register list");
2365 // Ranges must go from low to high.
2366 if (getARMRegisterNumbering(Reg) > getARMRegisterNumbering(EndReg))
2367 return Error(EndLoc, "bad range in register list");
2369 // Add all the registers in the range to the register list.
2370 while (Reg != EndReg) {
2371 Reg = getNextRegister(Reg);
2372 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2376 Parser.Lex(); // Eat the comma.
2377 RegLoc = Parser.getTok().getLoc();
2379 Reg = tryParseRegister();
2381 return Error(RegLoc, "register expected");
2382 // The register must be in the same register class as the first.
2383 if (!RC->contains(Reg))
2384 return Error(RegLoc, "invalid register in register list");
2385 // List must be monotonically increasing.
2386 if (getARMRegisterNumbering(Reg) <= getARMRegisterNumbering(OldReg))
2387 return Error(RegLoc, "register list not in ascending order");
2388 // VFP register lists must also be contiguous.
2389 // It's OK to use the enumeration values directly here rather, as the
2390 // VFP register classes have the enum sorted properly.
2391 if (RC != &ARMMCRegisterClasses[ARM::GPRRegClassID] &&
2393 return Error(RegLoc, "non-contiguous register range");
2394 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2397 SMLoc E = Parser.getTok().getLoc();
2398 if (Parser.getTok().isNot(AsmToken::RCurly))
2399 return Error(E, "'}' expected");
2400 Parser.Lex(); // Eat '}' token.
2402 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
2406 // parse a vector register list
2407 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2408 parseVectorList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2409 if(Parser.getTok().isNot(AsmToken::LCurly))
2410 return MatchOperand_NoMatch;
2412 SMLoc S = Parser.getTok().getLoc();
2413 Parser.Lex(); // Eat '{' token.
2414 SMLoc RegLoc = Parser.getTok().getLoc();
2416 int Reg = tryParseRegister();
2418 Error(RegLoc, "register expected");
2419 return MatchOperand_ParseFail;
2422 unsigned FirstReg = Reg;
2424 while (Parser.getTok().is(AsmToken::Comma)) {
2425 Parser.Lex(); // Eat the comma.
2426 RegLoc = Parser.getTok().getLoc();
2428 Reg = tryParseRegister();
2430 Error(RegLoc, "register expected");
2431 return MatchOperand_ParseFail;
2433 // vector register lists must also be contiguous.
2434 // It's OK to use the enumeration values directly here rather, as the
2435 // VFP register classes have the enum sorted properly.
2436 if (Reg != OldReg + 1) {
2437 Error(RegLoc, "non-contiguous register range");
2438 return MatchOperand_ParseFail;
2444 SMLoc E = Parser.getTok().getLoc();
2445 if (Parser.getTok().isNot(AsmToken::RCurly)) {
2446 Error(E, "'}' expected");
2447 return MatchOperand_ParseFail;
2449 Parser.Lex(); // Eat '}' token.
2451 Operands.push_back(ARMOperand::CreateVectorList(FirstReg, Count, S, E));
2452 return MatchOperand_Success;
2455 /// parseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
2456 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2457 parseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2458 SMLoc S = Parser.getTok().getLoc();
2459 const AsmToken &Tok = Parser.getTok();
2460 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2461 StringRef OptStr = Tok.getString();
2463 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
2464 .Case("sy", ARM_MB::SY)
2465 .Case("st", ARM_MB::ST)
2466 .Case("sh", ARM_MB::ISH)
2467 .Case("ish", ARM_MB::ISH)
2468 .Case("shst", ARM_MB::ISHST)
2469 .Case("ishst", ARM_MB::ISHST)
2470 .Case("nsh", ARM_MB::NSH)
2471 .Case("un", ARM_MB::NSH)
2472 .Case("nshst", ARM_MB::NSHST)
2473 .Case("unst", ARM_MB::NSHST)
2474 .Case("osh", ARM_MB::OSH)
2475 .Case("oshst", ARM_MB::OSHST)
2479 return MatchOperand_NoMatch;
2481 Parser.Lex(); // Eat identifier token.
2482 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
2483 return MatchOperand_Success;
2486 /// parseProcIFlagsOperand - Try to parse iflags from CPS instruction.
2487 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2488 parseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2489 SMLoc S = Parser.getTok().getLoc();
2490 const AsmToken &Tok = Parser.getTok();
2491 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2492 StringRef IFlagsStr = Tok.getString();
2494 // An iflags string of "none" is interpreted to mean that none of the AIF
2495 // bits are set. Not a terribly useful instruction, but a valid encoding.
2496 unsigned IFlags = 0;
2497 if (IFlagsStr != "none") {
2498 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
2499 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
2500 .Case("a", ARM_PROC::A)
2501 .Case("i", ARM_PROC::I)
2502 .Case("f", ARM_PROC::F)
2505 // If some specific iflag is already set, it means that some letter is
2506 // present more than once, this is not acceptable.
2507 if (Flag == ~0U || (IFlags & Flag))
2508 return MatchOperand_NoMatch;
2514 Parser.Lex(); // Eat identifier token.
2515 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
2516 return MatchOperand_Success;
2519 /// parseMSRMaskOperand - Try to parse mask flags from MSR instruction.
2520 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2521 parseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2522 SMLoc S = Parser.getTok().getLoc();
2523 const AsmToken &Tok = Parser.getTok();
2524 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2525 StringRef Mask = Tok.getString();
2528 // See ARMv6-M 10.1.1
2529 unsigned FlagsVal = StringSwitch<unsigned>(Mask)
2539 .Case("primask", 16)
2540 .Case("basepri", 17)
2541 .Case("basepri_max", 18)
2542 .Case("faultmask", 19)
2543 .Case("control", 20)
2546 if (FlagsVal == ~0U)
2547 return MatchOperand_NoMatch;
2549 if (!hasV7Ops() && FlagsVal >= 17 && FlagsVal <= 19)
2550 // basepri, basepri_max and faultmask only valid for V7m.
2551 return MatchOperand_NoMatch;
2553 Parser.Lex(); // Eat identifier token.
2554 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
2555 return MatchOperand_Success;
2558 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
2559 size_t Start = 0, Next = Mask.find('_');
2560 StringRef Flags = "";
2561 std::string SpecReg = LowercaseString(Mask.slice(Start, Next));
2562 if (Next != StringRef::npos)
2563 Flags = Mask.slice(Next+1, Mask.size());
2565 // FlagsVal contains the complete mask:
2567 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
2568 unsigned FlagsVal = 0;
2570 if (SpecReg == "apsr") {
2571 FlagsVal = StringSwitch<unsigned>(Flags)
2572 .Case("nzcvq", 0x8) // same as CPSR_f
2573 .Case("g", 0x4) // same as CPSR_s
2574 .Case("nzcvqg", 0xc) // same as CPSR_fs
2577 if (FlagsVal == ~0U) {
2579 return MatchOperand_NoMatch;
2581 FlagsVal = 8; // No flag
2583 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
2584 if (Flags == "all") // cpsr_all is an alias for cpsr_fc
2586 for (int i = 0, e = Flags.size(); i != e; ++i) {
2587 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
2594 // If some specific flag is already set, it means that some letter is
2595 // present more than once, this is not acceptable.
2596 if (FlagsVal == ~0U || (FlagsVal & Flag))
2597 return MatchOperand_NoMatch;
2600 } else // No match for special register.
2601 return MatchOperand_NoMatch;
2603 // Special register without flags is NOT equivalent to "fc" flags.
2604 // NOTE: This is a divergence from gas' behavior. Uncommenting the following
2605 // two lines would enable gas compatibility at the expense of breaking
2611 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
2612 if (SpecReg == "spsr")
2615 Parser.Lex(); // Eat identifier token.
2616 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
2617 return MatchOperand_Success;
2620 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2621 parsePKHImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands, StringRef Op,
2622 int Low, int High) {
2623 const AsmToken &Tok = Parser.getTok();
2624 if (Tok.isNot(AsmToken::Identifier)) {
2625 Error(Parser.getTok().getLoc(), Op + " operand expected.");
2626 return MatchOperand_ParseFail;
2628 StringRef ShiftName = Tok.getString();
2629 std::string LowerOp = LowercaseString(Op);
2630 std::string UpperOp = UppercaseString(Op);
2631 if (ShiftName != LowerOp && ShiftName != UpperOp) {
2632 Error(Parser.getTok().getLoc(), Op + " operand expected.");
2633 return MatchOperand_ParseFail;
2635 Parser.Lex(); // Eat shift type token.
2637 // There must be a '#' and a shift amount.
2638 if (Parser.getTok().isNot(AsmToken::Hash)) {
2639 Error(Parser.getTok().getLoc(), "'#' expected");
2640 return MatchOperand_ParseFail;
2642 Parser.Lex(); // Eat hash token.
2644 const MCExpr *ShiftAmount;
2645 SMLoc Loc = Parser.getTok().getLoc();
2646 if (getParser().ParseExpression(ShiftAmount)) {
2647 Error(Loc, "illegal expression");
2648 return MatchOperand_ParseFail;
2650 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
2652 Error(Loc, "constant expression expected");
2653 return MatchOperand_ParseFail;
2655 int Val = CE->getValue();
2656 if (Val < Low || Val > High) {
2657 Error(Loc, "immediate value out of range");
2658 return MatchOperand_ParseFail;
2661 Operands.push_back(ARMOperand::CreateImm(CE, Loc, Parser.getTok().getLoc()));
2663 return MatchOperand_Success;
2666 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2667 parseSetEndImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2668 const AsmToken &Tok = Parser.getTok();
2669 SMLoc S = Tok.getLoc();
2670 if (Tok.isNot(AsmToken::Identifier)) {
2671 Error(Tok.getLoc(), "'be' or 'le' operand expected");
2672 return MatchOperand_ParseFail;
2674 int Val = StringSwitch<int>(Tok.getString())
2678 Parser.Lex(); // Eat the token.
2681 Error(Tok.getLoc(), "'be' or 'le' operand expected");
2682 return MatchOperand_ParseFail;
2684 Operands.push_back(ARMOperand::CreateImm(MCConstantExpr::Create(Val,
2686 S, Parser.getTok().getLoc()));
2687 return MatchOperand_Success;
2690 /// parseShifterImm - Parse the shifter immediate operand for SSAT/USAT
2691 /// instructions. Legal values are:
2692 /// lsl #n 'n' in [0,31]
2693 /// asr #n 'n' in [1,32]
2694 /// n == 32 encoded as n == 0.
2695 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2696 parseShifterImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2697 const AsmToken &Tok = Parser.getTok();
2698 SMLoc S = Tok.getLoc();
2699 if (Tok.isNot(AsmToken::Identifier)) {
2700 Error(S, "shift operator 'asr' or 'lsl' expected");
2701 return MatchOperand_ParseFail;
2703 StringRef ShiftName = Tok.getString();
2705 if (ShiftName == "lsl" || ShiftName == "LSL")
2707 else if (ShiftName == "asr" || ShiftName == "ASR")
2710 Error(S, "shift operator 'asr' or 'lsl' expected");
2711 return MatchOperand_ParseFail;
2713 Parser.Lex(); // Eat the operator.
2715 // A '#' and a shift amount.
2716 if (Parser.getTok().isNot(AsmToken::Hash)) {
2717 Error(Parser.getTok().getLoc(), "'#' expected");
2718 return MatchOperand_ParseFail;
2720 Parser.Lex(); // Eat hash token.
2722 const MCExpr *ShiftAmount;
2723 SMLoc E = Parser.getTok().getLoc();
2724 if (getParser().ParseExpression(ShiftAmount)) {
2725 Error(E, "malformed shift expression");
2726 return MatchOperand_ParseFail;
2728 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
2730 Error(E, "shift amount must be an immediate");
2731 return MatchOperand_ParseFail;
2734 int64_t Val = CE->getValue();
2736 // Shift amount must be in [1,32]
2737 if (Val < 1 || Val > 32) {
2738 Error(E, "'asr' shift amount must be in range [1,32]");
2739 return MatchOperand_ParseFail;
2741 // asr #32 encoded as asr #0, but is not allowed in Thumb2 mode.
2742 if (isThumb() && Val == 32) {
2743 Error(E, "'asr #32' shift amount not allowed in Thumb mode");
2744 return MatchOperand_ParseFail;
2746 if (Val == 32) Val = 0;
2748 // Shift amount must be in [1,32]
2749 if (Val < 0 || Val > 31) {
2750 Error(E, "'lsr' shift amount must be in range [0,31]");
2751 return MatchOperand_ParseFail;
2755 E = Parser.getTok().getLoc();
2756 Operands.push_back(ARMOperand::CreateShifterImm(isASR, Val, S, E));
2758 return MatchOperand_Success;
2761 /// parseRotImm - Parse the shifter immediate operand for SXTB/UXTB family
2762 /// of instructions. Legal values are:
2763 /// ror #n 'n' in {0, 8, 16, 24}
2764 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2765 parseRotImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2766 const AsmToken &Tok = Parser.getTok();
2767 SMLoc S = Tok.getLoc();
2768 if (Tok.isNot(AsmToken::Identifier))
2769 return MatchOperand_NoMatch;
2770 StringRef ShiftName = Tok.getString();
2771 if (ShiftName != "ror" && ShiftName != "ROR")
2772 return MatchOperand_NoMatch;
2773 Parser.Lex(); // Eat the operator.
2775 // A '#' and a rotate amount.
2776 if (Parser.getTok().isNot(AsmToken::Hash)) {
2777 Error(Parser.getTok().getLoc(), "'#' expected");
2778 return MatchOperand_ParseFail;
2780 Parser.Lex(); // Eat hash token.
2782 const MCExpr *ShiftAmount;
2783 SMLoc E = Parser.getTok().getLoc();
2784 if (getParser().ParseExpression(ShiftAmount)) {
2785 Error(E, "malformed rotate expression");
2786 return MatchOperand_ParseFail;
2788 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
2790 Error(E, "rotate amount must be an immediate");
2791 return MatchOperand_ParseFail;
2794 int64_t Val = CE->getValue();
2795 // Shift amount must be in {0, 8, 16, 24} (0 is undocumented extension)
2796 // normally, zero is represented in asm by omitting the rotate operand
2798 if (Val != 8 && Val != 16 && Val != 24 && Val != 0) {
2799 Error(E, "'ror' rotate amount must be 8, 16, or 24");
2800 return MatchOperand_ParseFail;
2803 E = Parser.getTok().getLoc();
2804 Operands.push_back(ARMOperand::CreateRotImm(Val, S, E));
2806 return MatchOperand_Success;
2809 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2810 parseBitfield(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2811 SMLoc S = Parser.getTok().getLoc();
2812 // The bitfield descriptor is really two operands, the LSB and the width.
2813 if (Parser.getTok().isNot(AsmToken::Hash)) {
2814 Error(Parser.getTok().getLoc(), "'#' expected");
2815 return MatchOperand_ParseFail;
2817 Parser.Lex(); // Eat hash token.
2819 const MCExpr *LSBExpr;
2820 SMLoc E = Parser.getTok().getLoc();
2821 if (getParser().ParseExpression(LSBExpr)) {
2822 Error(E, "malformed immediate expression");
2823 return MatchOperand_ParseFail;
2825 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(LSBExpr);
2827 Error(E, "'lsb' operand must be an immediate");
2828 return MatchOperand_ParseFail;
2831 int64_t LSB = CE->getValue();
2832 // The LSB must be in the range [0,31]
2833 if (LSB < 0 || LSB > 31) {
2834 Error(E, "'lsb' operand must be in the range [0,31]");
2835 return MatchOperand_ParseFail;
2837 E = Parser.getTok().getLoc();
2839 // Expect another immediate operand.
2840 if (Parser.getTok().isNot(AsmToken::Comma)) {
2841 Error(Parser.getTok().getLoc(), "too few operands");
2842 return MatchOperand_ParseFail;
2844 Parser.Lex(); // Eat hash token.
2845 if (Parser.getTok().isNot(AsmToken::Hash)) {
2846 Error(Parser.getTok().getLoc(), "'#' expected");
2847 return MatchOperand_ParseFail;
2849 Parser.Lex(); // Eat hash token.
2851 const MCExpr *WidthExpr;
2852 if (getParser().ParseExpression(WidthExpr)) {
2853 Error(E, "malformed immediate expression");
2854 return MatchOperand_ParseFail;
2856 CE = dyn_cast<MCConstantExpr>(WidthExpr);
2858 Error(E, "'width' operand must be an immediate");
2859 return MatchOperand_ParseFail;
2862 int64_t Width = CE->getValue();
2863 // The LSB must be in the range [1,32-lsb]
2864 if (Width < 1 || Width > 32 - LSB) {
2865 Error(E, "'width' operand must be in the range [1,32-lsb]");
2866 return MatchOperand_ParseFail;
2868 E = Parser.getTok().getLoc();
2870 Operands.push_back(ARMOperand::CreateBitfield(LSB, Width, S, E));
2872 return MatchOperand_Success;
2875 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2876 parsePostIdxReg(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2877 // Check for a post-index addressing register operand. Specifically:
2878 // postidx_reg := '+' register {, shift}
2879 // | '-' register {, shift}
2880 // | register {, shift}
2882 // This method must return MatchOperand_NoMatch without consuming any tokens
2883 // in the case where there is no match, as other alternatives take other
2885 AsmToken Tok = Parser.getTok();
2886 SMLoc S = Tok.getLoc();
2887 bool haveEaten = false;
2890 if (Tok.is(AsmToken::Plus)) {
2891 Parser.Lex(); // Eat the '+' token.
2893 } else if (Tok.is(AsmToken::Minus)) {
2894 Parser.Lex(); // Eat the '-' token.
2898 if (Parser.getTok().is(AsmToken::Identifier))
2899 Reg = tryParseRegister();
2902 return MatchOperand_NoMatch;
2903 Error(Parser.getTok().getLoc(), "register expected");
2904 return MatchOperand_ParseFail;
2906 SMLoc E = Parser.getTok().getLoc();
2908 ARM_AM::ShiftOpc ShiftTy = ARM_AM::no_shift;
2909 unsigned ShiftImm = 0;
2910 if (Parser.getTok().is(AsmToken::Comma)) {
2911 Parser.Lex(); // Eat the ','.
2912 if (parseMemRegOffsetShift(ShiftTy, ShiftImm))
2913 return MatchOperand_ParseFail;
2916 Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ShiftTy,
2919 return MatchOperand_Success;
2922 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2923 parseAM3Offset(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2924 // Check for a post-index addressing register operand. Specifically:
2925 // am3offset := '+' register
2932 // This method must return MatchOperand_NoMatch without consuming any tokens
2933 // in the case where there is no match, as other alternatives take other
2935 AsmToken Tok = Parser.getTok();
2936 SMLoc S = Tok.getLoc();
2938 // Do immediates first, as we always parse those if we have a '#'.
2939 if (Parser.getTok().is(AsmToken::Hash)) {
2940 Parser.Lex(); // Eat the '#'.
2941 // Explicitly look for a '-', as we need to encode negative zero
2943 bool isNegative = Parser.getTok().is(AsmToken::Minus);
2944 const MCExpr *Offset;
2945 if (getParser().ParseExpression(Offset))
2946 return MatchOperand_ParseFail;
2947 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
2949 Error(S, "constant expression expected");
2950 return MatchOperand_ParseFail;
2952 SMLoc E = Tok.getLoc();
2953 // Negative zero is encoded as the flag value INT32_MIN.
2954 int32_t Val = CE->getValue();
2955 if (isNegative && Val == 0)
2959 ARMOperand::CreateImm(MCConstantExpr::Create(Val, getContext()), S, E));
2961 return MatchOperand_Success;
2965 bool haveEaten = false;
2968 if (Tok.is(AsmToken::Plus)) {
2969 Parser.Lex(); // Eat the '+' token.
2971 } else if (Tok.is(AsmToken::Minus)) {
2972 Parser.Lex(); // Eat the '-' token.
2976 if (Parser.getTok().is(AsmToken::Identifier))
2977 Reg = tryParseRegister();
2980 return MatchOperand_NoMatch;
2981 Error(Parser.getTok().getLoc(), "register expected");
2982 return MatchOperand_ParseFail;
2984 SMLoc E = Parser.getTok().getLoc();
2986 Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ARM_AM::no_shift,
2989 return MatchOperand_Success;
2992 /// cvtT2LdrdPre - Convert parsed operands to MCInst.
2993 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2994 /// when they refer multiple MIOperands inside a single one.
2996 cvtT2LdrdPre(MCInst &Inst, unsigned Opcode,
2997 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2999 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3000 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3001 // Create a writeback register dummy placeholder.
3002 Inst.addOperand(MCOperand::CreateReg(0));
3004 ((ARMOperand*)Operands[4])->addMemImm8s4OffsetOperands(Inst, 2);
3006 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3010 /// cvtT2StrdPre - Convert parsed operands to MCInst.
3011 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3012 /// when they refer multiple MIOperands inside a single one.
3014 cvtT2StrdPre(MCInst &Inst, unsigned Opcode,
3015 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3016 // Create a writeback register dummy placeholder.
3017 Inst.addOperand(MCOperand::CreateReg(0));
3019 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3020 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3022 ((ARMOperand*)Operands[4])->addMemImm8s4OffsetOperands(Inst, 2);
3024 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3028 /// cvtLdWriteBackRegT2AddrModeImm8 - 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 cvtLdWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
3033 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3034 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3036 // Create a writeback register dummy placeholder.
3037 Inst.addOperand(MCOperand::CreateImm(0));
3039 ((ARMOperand*)Operands[3])->addMemImm8OffsetOperands(Inst, 2);
3040 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3044 /// cvtStWriteBackRegT2AddrModeImm8 - Convert parsed operands to MCInst.
3045 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3046 /// when they refer multiple MIOperands inside a single one.
3048 cvtStWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
3049 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3050 // Create a writeback register dummy placeholder.
3051 Inst.addOperand(MCOperand::CreateImm(0));
3052 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3053 ((ARMOperand*)Operands[3])->addMemImm8OffsetOperands(Inst, 2);
3054 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3058 /// cvtLdWriteBackRegAddrMode2 - 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 cvtLdWriteBackRegAddrMode2(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])->addAddrMode2Operands(Inst, 3);
3070 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3074 /// cvtLdWriteBackRegAddrModeImm12 - Convert parsed operands to MCInst.
3075 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3076 /// when they refer multiple MIOperands inside a single one.
3078 cvtLdWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
3079 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3080 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3082 // Create a writeback register dummy placeholder.
3083 Inst.addOperand(MCOperand::CreateImm(0));
3085 ((ARMOperand*)Operands[3])->addMemImm12OffsetOperands(Inst, 2);
3086 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3091 /// cvtStWriteBackRegAddrModeImm12 - Convert parsed operands to MCInst.
3092 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3093 /// when they refer multiple MIOperands inside a single one.
3095 cvtStWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
3096 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3097 // Create a writeback register dummy placeholder.
3098 Inst.addOperand(MCOperand::CreateImm(0));
3099 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3100 ((ARMOperand*)Operands[3])->addMemImm12OffsetOperands(Inst, 2);
3101 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3105 /// cvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
3106 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3107 /// when they refer multiple MIOperands inside a single one.
3109 cvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
3110 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3111 // Create a writeback register dummy placeholder.
3112 Inst.addOperand(MCOperand::CreateImm(0));
3113 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3114 ((ARMOperand*)Operands[3])->addAddrMode2Operands(Inst, 3);
3115 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3119 /// cvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
3120 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3121 /// when they refer multiple MIOperands inside a single one.
3123 cvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
3124 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3125 // Create a writeback register dummy placeholder.
3126 Inst.addOperand(MCOperand::CreateImm(0));
3127 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3128 ((ARMOperand*)Operands[3])->addAddrMode3Operands(Inst, 3);
3129 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3133 /// cvtLdExtTWriteBackImm - Convert parsed operands to MCInst.
3134 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3135 /// when they refer multiple MIOperands inside a single one.
3137 cvtLdExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
3138 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3140 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3141 // Create a writeback register dummy placeholder.
3142 Inst.addOperand(MCOperand::CreateImm(0));
3144 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3146 ((ARMOperand*)Operands[4])->addPostIdxImm8Operands(Inst, 1);
3148 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3152 /// cvtLdExtTWriteBackReg - Convert parsed operands to MCInst.
3153 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3154 /// when they refer multiple MIOperands inside a single one.
3156 cvtLdExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
3157 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3159 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3160 // Create a writeback register dummy placeholder.
3161 Inst.addOperand(MCOperand::CreateImm(0));
3163 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3165 ((ARMOperand*)Operands[4])->addPostIdxRegOperands(Inst, 2);
3167 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3171 /// cvtStExtTWriteBackImm - Convert parsed operands to MCInst.
3172 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3173 /// when they refer multiple MIOperands inside a single one.
3175 cvtStExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
3176 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3177 // Create a writeback register dummy placeholder.
3178 Inst.addOperand(MCOperand::CreateImm(0));
3180 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3182 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3184 ((ARMOperand*)Operands[4])->addPostIdxImm8Operands(Inst, 1);
3186 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3190 /// cvtStExtTWriteBackReg - Convert parsed operands to MCInst.
3191 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3192 /// when they refer multiple MIOperands inside a single one.
3194 cvtStExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
3195 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3196 // Create a writeback register dummy placeholder.
3197 Inst.addOperand(MCOperand::CreateImm(0));
3199 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3201 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3203 ((ARMOperand*)Operands[4])->addPostIdxRegOperands(Inst, 2);
3205 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3209 /// cvtLdrdPre - Convert parsed operands to MCInst.
3210 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3211 /// when they refer multiple MIOperands inside a single one.
3213 cvtLdrdPre(MCInst &Inst, unsigned Opcode,
3214 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3216 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3217 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3218 // Create a writeback register dummy placeholder.
3219 Inst.addOperand(MCOperand::CreateImm(0));
3221 ((ARMOperand*)Operands[4])->addAddrMode3Operands(Inst, 3);
3223 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3227 /// cvtStrdPre - Convert parsed operands to MCInst.
3228 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3229 /// when they refer multiple MIOperands inside a single one.
3231 cvtStrdPre(MCInst &Inst, unsigned Opcode,
3232 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3233 // Create a writeback register dummy placeholder.
3234 Inst.addOperand(MCOperand::CreateImm(0));
3236 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3237 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3239 ((ARMOperand*)Operands[4])->addAddrMode3Operands(Inst, 3);
3241 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3245 /// cvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
3246 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3247 /// when they refer multiple MIOperands inside a single one.
3249 cvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
3250 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3251 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3252 // Create a writeback register dummy placeholder.
3253 Inst.addOperand(MCOperand::CreateImm(0));
3254 ((ARMOperand*)Operands[3])->addAddrMode3Operands(Inst, 3);
3255 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3259 /// cvtThumbMultiple- Convert parsed operands to MCInst.
3260 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3261 /// when they refer multiple MIOperands inside a single one.
3263 cvtThumbMultiply(MCInst &Inst, unsigned Opcode,
3264 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3265 // The second source operand must be the same register as the destination
3267 if (Operands.size() == 6 &&
3268 (((ARMOperand*)Operands[3])->getReg() !=
3269 ((ARMOperand*)Operands[5])->getReg()) &&
3270 (((ARMOperand*)Operands[3])->getReg() !=
3271 ((ARMOperand*)Operands[4])->getReg())) {
3272 Error(Operands[3]->getStartLoc(),
3273 "destination register must match source register");
3276 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3277 ((ARMOperand*)Operands[1])->addCCOutOperands(Inst, 1);
3278 ((ARMOperand*)Operands[4])->addRegOperands(Inst, 1);
3279 // If we have a three-operand form, use that, else the second source operand
3280 // is just the destination operand again.
3281 if (Operands.size() == 6)
3282 ((ARMOperand*)Operands[5])->addRegOperands(Inst, 1);
3284 Inst.addOperand(Inst.getOperand(0));
3285 ((ARMOperand*)Operands[2])->addCondCodeOperands(Inst, 2);
3290 /// Parse an ARM memory expression, return false if successful else return true
3291 /// or an error. The first token must be a '[' when called.
3293 parseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3295 assert(Parser.getTok().is(AsmToken::LBrac) &&
3296 "Token is not a Left Bracket");
3297 S = Parser.getTok().getLoc();
3298 Parser.Lex(); // Eat left bracket token.
3300 const AsmToken &BaseRegTok = Parser.getTok();
3301 int BaseRegNum = tryParseRegister();
3302 if (BaseRegNum == -1)
3303 return Error(BaseRegTok.getLoc(), "register expected");
3305 // The next token must either be a comma or a closing bracket.
3306 const AsmToken &Tok = Parser.getTok();
3307 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
3308 return Error(Tok.getLoc(), "malformed memory operand");
3310 if (Tok.is(AsmToken::RBrac)) {
3312 Parser.Lex(); // Eat right bracket token.
3314 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, 0, ARM_AM::no_shift,
3315 0, 0, false, S, E));
3317 // If there's a pre-indexing writeback marker, '!', just add it as a token
3318 // operand. It's rather odd, but syntactically valid.
3319 if (Parser.getTok().is(AsmToken::Exclaim)) {
3320 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3321 Parser.Lex(); // Eat the '!'.
3327 assert(Tok.is(AsmToken::Comma) && "Lost comma in memory operand?!");
3328 Parser.Lex(); // Eat the comma.
3330 // If we have a ':', it's an alignment specifier.
3331 if (Parser.getTok().is(AsmToken::Colon)) {
3332 Parser.Lex(); // Eat the ':'.
3333 E = Parser.getTok().getLoc();
3336 if (getParser().ParseExpression(Expr))
3339 // The expression has to be a constant. Memory references with relocations
3340 // don't come through here, as they use the <label> forms of the relevant
3342 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
3344 return Error (E, "constant expression expected");
3347 switch (CE->getValue()) {
3349 return Error(E, "alignment specifier must be 64, 128, or 256 bits");
3350 case 64: Align = 8; break;
3351 case 128: Align = 16; break;
3352 case 256: Align = 32; break;
3355 // Now we should have the closing ']'
3356 E = Parser.getTok().getLoc();
3357 if (Parser.getTok().isNot(AsmToken::RBrac))
3358 return Error(E, "']' expected");
3359 Parser.Lex(); // Eat right bracket token.
3361 // Don't worry about range checking the value here. That's handled by
3362 // the is*() predicates.
3363 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, 0,
3364 ARM_AM::no_shift, 0, Align,
3367 // If there's a pre-indexing writeback marker, '!', just add it as a token
3369 if (Parser.getTok().is(AsmToken::Exclaim)) {
3370 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3371 Parser.Lex(); // Eat the '!'.
3377 // If we have a '#', it's an immediate offset, else assume it's a register
3379 if (Parser.getTok().is(AsmToken::Hash)) {
3380 Parser.Lex(); // Eat the '#'.
3381 E = Parser.getTok().getLoc();
3383 bool isNegative = getParser().getTok().is(AsmToken::Minus);
3384 const MCExpr *Offset;
3385 if (getParser().ParseExpression(Offset))
3388 // The expression has to be a constant. Memory references with relocations
3389 // don't come through here, as they use the <label> forms of the relevant
3391 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
3393 return Error (E, "constant expression expected");
3395 // If the constant was #-0, represent it as INT32_MIN.
3396 int32_t Val = CE->getValue();
3397 if (isNegative && Val == 0)
3398 CE = MCConstantExpr::Create(INT32_MIN, getContext());
3400 // Now we should have the closing ']'
3401 E = Parser.getTok().getLoc();
3402 if (Parser.getTok().isNot(AsmToken::RBrac))
3403 return Error(E, "']' expected");
3404 Parser.Lex(); // Eat right bracket token.
3406 // Don't worry about range checking the value here. That's handled by
3407 // the is*() predicates.
3408 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, CE, 0,
3409 ARM_AM::no_shift, 0, 0,
3412 // If there's a pre-indexing writeback marker, '!', just add it as a token
3414 if (Parser.getTok().is(AsmToken::Exclaim)) {
3415 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3416 Parser.Lex(); // Eat the '!'.
3422 // The register offset is optionally preceded by a '+' or '-'
3423 bool isNegative = false;
3424 if (Parser.getTok().is(AsmToken::Minus)) {
3426 Parser.Lex(); // Eat the '-'.
3427 } else if (Parser.getTok().is(AsmToken::Plus)) {
3429 Parser.Lex(); // Eat the '+'.
3432 E = Parser.getTok().getLoc();
3433 int OffsetRegNum = tryParseRegister();
3434 if (OffsetRegNum == -1)
3435 return Error(E, "register expected");
3437 // If there's a shift operator, handle it.
3438 ARM_AM::ShiftOpc ShiftType = ARM_AM::no_shift;
3439 unsigned ShiftImm = 0;
3440 if (Parser.getTok().is(AsmToken::Comma)) {
3441 Parser.Lex(); // Eat the ','.
3442 if (parseMemRegOffsetShift(ShiftType, ShiftImm))
3446 // Now we should have the closing ']'
3447 E = Parser.getTok().getLoc();
3448 if (Parser.getTok().isNot(AsmToken::RBrac))
3449 return Error(E, "']' expected");
3450 Parser.Lex(); // Eat right bracket token.
3452 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, OffsetRegNum,
3453 ShiftType, ShiftImm, 0, isNegative,
3456 // If there's a pre-indexing writeback marker, '!', just add it as a token
3458 if (Parser.getTok().is(AsmToken::Exclaim)) {
3459 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3460 Parser.Lex(); // Eat the '!'.
3466 /// parseMemRegOffsetShift - one of these two:
3467 /// ( lsl | lsr | asr | ror ) , # shift_amount
3469 /// return true if it parses a shift otherwise it returns false.
3470 bool ARMAsmParser::parseMemRegOffsetShift(ARM_AM::ShiftOpc &St,
3472 SMLoc Loc = Parser.getTok().getLoc();
3473 const AsmToken &Tok = Parser.getTok();
3474 if (Tok.isNot(AsmToken::Identifier))
3476 StringRef ShiftName = Tok.getString();
3477 if (ShiftName == "lsl" || ShiftName == "LSL")
3479 else if (ShiftName == "lsr" || ShiftName == "LSR")
3481 else if (ShiftName == "asr" || ShiftName == "ASR")
3483 else if (ShiftName == "ror" || ShiftName == "ROR")
3485 else if (ShiftName == "rrx" || ShiftName == "RRX")
3488 return Error(Loc, "illegal shift operator");
3489 Parser.Lex(); // Eat shift type token.
3491 // rrx stands alone.
3493 if (St != ARM_AM::rrx) {
3494 Loc = Parser.getTok().getLoc();
3495 // A '#' and a shift amount.
3496 const AsmToken &HashTok = Parser.getTok();
3497 if (HashTok.isNot(AsmToken::Hash))
3498 return Error(HashTok.getLoc(), "'#' expected");
3499 Parser.Lex(); // Eat hash token.
3502 if (getParser().ParseExpression(Expr))
3504 // Range check the immediate.
3505 // lsl, ror: 0 <= imm <= 31
3506 // lsr, asr: 0 <= imm <= 32
3507 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
3509 return Error(Loc, "shift amount must be an immediate");
3510 int64_t Imm = CE->getValue();
3512 ((St == ARM_AM::lsl || St == ARM_AM::ror) && Imm > 31) ||
3513 ((St == ARM_AM::lsr || St == ARM_AM::asr) && Imm > 32))
3514 return Error(Loc, "immediate shift value out of range");
3521 /// parseFPImm - A floating point immediate expression operand.
3522 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3523 parseFPImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3524 SMLoc S = Parser.getTok().getLoc();
3526 if (Parser.getTok().isNot(AsmToken::Hash))
3527 return MatchOperand_NoMatch;
3529 // Disambiguate the VMOV forms that can accept an FP immediate.
3530 // vmov.f32 <sreg>, #imm
3531 // vmov.f64 <dreg>, #imm
3532 // vmov.f32 <dreg>, #imm @ vector f32x2
3533 // vmov.f32 <qreg>, #imm @ vector f32x4
3535 // There are also the NEON VMOV instructions which expect an
3536 // integer constant. Make sure we don't try to parse an FPImm
3538 // vmov.i{8|16|32|64} <dreg|qreg>, #imm
3539 ARMOperand *TyOp = static_cast<ARMOperand*>(Operands[2]);
3540 if (!TyOp->isToken() || (TyOp->getToken() != ".f32" &&
3541 TyOp->getToken() != ".f64"))
3542 return MatchOperand_NoMatch;
3544 Parser.Lex(); // Eat the '#'.
3546 // Handle negation, as that still comes through as a separate token.
3547 bool isNegative = false;
3548 if (Parser.getTok().is(AsmToken::Minus)) {
3552 const AsmToken &Tok = Parser.getTok();
3553 if (Tok.is(AsmToken::Real)) {
3554 APFloat RealVal(APFloat::IEEEdouble, Tok.getString());
3555 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
3556 // If we had a '-' in front, toggle the sign bit.
3557 IntVal ^= (uint64_t)isNegative << 63;
3558 int Val = ARM_AM::getFP64Imm(APInt(64, IntVal));
3559 Parser.Lex(); // Eat the token.
3561 TokError("floating point value out of range");
3562 return MatchOperand_ParseFail;
3564 Operands.push_back(ARMOperand::CreateFPImm(Val, S, getContext()));
3565 return MatchOperand_Success;
3567 if (Tok.is(AsmToken::Integer)) {
3568 int64_t Val = Tok.getIntVal();
3569 Parser.Lex(); // Eat the token.
3570 if (Val > 255 || Val < 0) {
3571 TokError("encoded floating point value out of range");
3572 return MatchOperand_ParseFail;
3574 Operands.push_back(ARMOperand::CreateFPImm(Val, S, getContext()));
3575 return MatchOperand_Success;
3578 TokError("invalid floating point immediate");
3579 return MatchOperand_ParseFail;
3581 /// Parse a arm instruction operand. For now this parses the operand regardless
3582 /// of the mnemonic.
3583 bool ARMAsmParser::parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
3584 StringRef Mnemonic) {
3587 // Check if the current operand has a custom associated parser, if so, try to
3588 // custom parse the operand, or fallback to the general approach.
3589 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
3590 if (ResTy == MatchOperand_Success)
3592 // If there wasn't a custom match, try the generic matcher below. Otherwise,
3593 // there was a match, but an error occurred, in which case, just return that
3594 // the operand parsing failed.
3595 if (ResTy == MatchOperand_ParseFail)
3598 switch (getLexer().getKind()) {
3600 Error(Parser.getTok().getLoc(), "unexpected token in operand");
3602 case AsmToken::Identifier: {
3603 // If this is VMRS, check for the apsr_nzcv operand.
3604 if (!tryParseRegisterWithWriteBack(Operands))
3606 int Res = tryParseShiftRegister(Operands);
3607 if (Res == 0) // success
3609 else if (Res == -1) // irrecoverable error
3611 if (Mnemonic == "vmrs" && Parser.getTok().getString() == "apsr_nzcv") {
3612 S = Parser.getTok().getLoc();
3614 Operands.push_back(ARMOperand::CreateToken("apsr_nzcv", S));
3618 // Fall though for the Identifier case that is not a register or a
3621 case AsmToken::Integer: // things like 1f and 2b as a branch targets
3622 case AsmToken::Dot: { // . as a branch target
3623 // This was not a register so parse other operands that start with an
3624 // identifier (like labels) as expressions and create them as immediates.
3625 const MCExpr *IdVal;
3626 S = Parser.getTok().getLoc();
3627 if (getParser().ParseExpression(IdVal))
3629 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3630 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
3633 case AsmToken::LBrac:
3634 return parseMemory(Operands);
3635 case AsmToken::LCurly:
3636 return parseRegisterList(Operands);
3637 case AsmToken::Hash: {
3638 // #42 -> immediate.
3639 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
3640 S = Parser.getTok().getLoc();
3642 bool isNegative = Parser.getTok().is(AsmToken::Minus);
3643 const MCExpr *ImmVal;
3644 if (getParser().ParseExpression(ImmVal))
3646 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ImmVal);
3648 Error(S, "constant expression expected");
3649 return MatchOperand_ParseFail;
3651 int32_t Val = CE->getValue();
3652 if (isNegative && Val == 0)
3653 ImmVal = MCConstantExpr::Create(INT32_MIN, getContext());
3654 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3655 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
3658 case AsmToken::Colon: {
3659 // ":lower16:" and ":upper16:" expression prefixes
3660 // FIXME: Check it's an expression prefix,
3661 // e.g. (FOO - :lower16:BAR) isn't legal.
3662 ARMMCExpr::VariantKind RefKind;
3663 if (parsePrefix(RefKind))
3666 const MCExpr *SubExprVal;
3667 if (getParser().ParseExpression(SubExprVal))
3670 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
3672 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3673 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
3679 // parsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
3680 // :lower16: and :upper16:.
3681 bool ARMAsmParser::parsePrefix(ARMMCExpr::VariantKind &RefKind) {
3682 RefKind = ARMMCExpr::VK_ARM_None;
3684 // :lower16: and :upper16: modifiers
3685 assert(getLexer().is(AsmToken::Colon) && "expected a :");
3686 Parser.Lex(); // Eat ':'
3688 if (getLexer().isNot(AsmToken::Identifier)) {
3689 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
3693 StringRef IDVal = Parser.getTok().getIdentifier();
3694 if (IDVal == "lower16") {
3695 RefKind = ARMMCExpr::VK_ARM_LO16;
3696 } else if (IDVal == "upper16") {
3697 RefKind = ARMMCExpr::VK_ARM_HI16;
3699 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
3704 if (getLexer().isNot(AsmToken::Colon)) {
3705 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
3708 Parser.Lex(); // Eat the last ':'
3712 /// \brief Given a mnemonic, split out possible predication code and carry
3713 /// setting letters to form a canonical mnemonic and flags.
3715 // FIXME: Would be nice to autogen this.
3716 // FIXME: This is a bit of a maze of special cases.
3717 StringRef ARMAsmParser::splitMnemonic(StringRef Mnemonic,
3718 unsigned &PredicationCode,
3720 unsigned &ProcessorIMod,
3721 StringRef &ITMask) {
3722 PredicationCode = ARMCC::AL;
3723 CarrySetting = false;
3726 // Ignore some mnemonics we know aren't predicated forms.
3728 // FIXME: Would be nice to autogen this.
3729 if ((Mnemonic == "movs" && isThumb()) ||
3730 Mnemonic == "teq" || Mnemonic == "vceq" || Mnemonic == "svc" ||
3731 Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
3732 Mnemonic == "vmls" || Mnemonic == "vnmls" || Mnemonic == "vacge" ||
3733 Mnemonic == "vcge" || Mnemonic == "vclt" || Mnemonic == "vacgt" ||
3734 Mnemonic == "vcgt" || Mnemonic == "vcle" || Mnemonic == "smlal" ||
3735 Mnemonic == "umaal" || Mnemonic == "umlal" || Mnemonic == "vabal" ||
3736 Mnemonic == "vmlal" || Mnemonic == "vpadal" || Mnemonic == "vqdmlal")
3739 // First, split out any predication code. Ignore mnemonics we know aren't
3740 // predicated but do have a carry-set and so weren't caught above.
3741 if (Mnemonic != "adcs" && Mnemonic != "bics" && Mnemonic != "movs" &&
3742 Mnemonic != "muls" && Mnemonic != "smlals" && Mnemonic != "smulls" &&
3743 Mnemonic != "umlals" && Mnemonic != "umulls" && Mnemonic != "lsls" &&
3744 Mnemonic != "sbcs" && Mnemonic != "rscs") {
3745 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
3746 .Case("eq", ARMCC::EQ)
3747 .Case("ne", ARMCC::NE)
3748 .Case("hs", ARMCC::HS)
3749 .Case("cs", ARMCC::HS)
3750 .Case("lo", ARMCC::LO)
3751 .Case("cc", ARMCC::LO)
3752 .Case("mi", ARMCC::MI)
3753 .Case("pl", ARMCC::PL)
3754 .Case("vs", ARMCC::VS)
3755 .Case("vc", ARMCC::VC)
3756 .Case("hi", ARMCC::HI)
3757 .Case("ls", ARMCC::LS)
3758 .Case("ge", ARMCC::GE)
3759 .Case("lt", ARMCC::LT)
3760 .Case("gt", ARMCC::GT)
3761 .Case("le", ARMCC::LE)
3762 .Case("al", ARMCC::AL)
3765 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
3766 PredicationCode = CC;
3770 // Next, determine if we have a carry setting bit. We explicitly ignore all
3771 // the instructions we know end in 's'.
3772 if (Mnemonic.endswith("s") &&
3773 !(Mnemonic == "cps" || Mnemonic == "mls" ||
3774 Mnemonic == "mrs" || Mnemonic == "smmls" || Mnemonic == "vabs" ||
3775 Mnemonic == "vcls" || Mnemonic == "vmls" || Mnemonic == "vmrs" ||
3776 Mnemonic == "vnmls" || Mnemonic == "vqabs" || Mnemonic == "vrecps" ||
3777 Mnemonic == "vrsqrts" || Mnemonic == "srs" ||
3778 (Mnemonic == "movs" && isThumb()))) {
3779 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
3780 CarrySetting = true;
3783 // The "cps" instruction can have a interrupt mode operand which is glued into
3784 // the mnemonic. Check if this is the case, split it and parse the imod op
3785 if (Mnemonic.startswith("cps")) {
3786 // Split out any imod code.
3788 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
3789 .Case("ie", ARM_PROC::IE)
3790 .Case("id", ARM_PROC::ID)
3793 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
3794 ProcessorIMod = IMod;
3798 // The "it" instruction has the condition mask on the end of the mnemonic.
3799 if (Mnemonic.startswith("it")) {
3800 ITMask = Mnemonic.slice(2, Mnemonic.size());
3801 Mnemonic = Mnemonic.slice(0, 2);
3807 /// \brief Given a canonical mnemonic, determine if the instruction ever allows
3808 /// inclusion of carry set or predication code operands.
3810 // FIXME: It would be nice to autogen this.
3812 getMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
3813 bool &CanAcceptPredicationCode) {
3814 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
3815 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
3816 Mnemonic == "add" || Mnemonic == "adc" ||
3817 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
3818 Mnemonic == "orr" || Mnemonic == "mvn" ||
3819 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
3820 Mnemonic == "sbc" || Mnemonic == "eor" || Mnemonic == "neg" ||
3821 (!isThumb() && (Mnemonic == "smull" || Mnemonic == "mov" ||
3822 Mnemonic == "mla" || Mnemonic == "smlal" ||
3823 Mnemonic == "umlal" || Mnemonic == "umull"))) {
3824 CanAcceptCarrySet = true;
3826 CanAcceptCarrySet = false;
3828 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
3829 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
3830 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
3831 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
3832 Mnemonic == "dsb" || Mnemonic == "isb" || Mnemonic == "setend" ||
3833 (Mnemonic == "clrex" && !isThumb()) ||
3834 (Mnemonic == "nop" && isThumbOne()) ||
3835 ((Mnemonic == "pld" || Mnemonic == "pli" || Mnemonic == "pldw" ||
3836 Mnemonic == "ldc2" || Mnemonic == "ldc2l" ||
3837 Mnemonic == "stc2" || Mnemonic == "stc2l") && !isThumb()) ||
3838 ((Mnemonic.startswith("rfe") || Mnemonic.startswith("srs")) &&
3840 Mnemonic.startswith("cps") || (Mnemonic == "movs" && isThumbOne())) {
3841 CanAcceptPredicationCode = false;
3843 CanAcceptPredicationCode = true;
3846 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
3847 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
3848 CanAcceptPredicationCode = false;
3852 bool ARMAsmParser::shouldOmitCCOutOperand(StringRef Mnemonic,
3853 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3854 // FIXME: This is all horribly hacky. We really need a better way to deal
3855 // with optional operands like this in the matcher table.
3857 // The 'mov' mnemonic is special. One variant has a cc_out operand, while
3858 // another does not. Specifically, the MOVW instruction does not. So we
3859 // special case it here and remove the defaulted (non-setting) cc_out
3860 // operand if that's the instruction we're trying to match.
3862 // We do this as post-processing of the explicit operands rather than just
3863 // conditionally adding the cc_out in the first place because we need
3864 // to check the type of the parsed immediate operand.
3865 if (Mnemonic == "mov" && Operands.size() > 4 && !isThumb() &&
3866 !static_cast<ARMOperand*>(Operands[4])->isARMSOImm() &&
3867 static_cast<ARMOperand*>(Operands[4])->isImm0_65535Expr() &&
3868 static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
3871 // Register-register 'add' for thumb does not have a cc_out operand
3872 // when there are only two register operands.
3873 if (isThumb() && Mnemonic == "add" && Operands.size() == 5 &&
3874 static_cast<ARMOperand*>(Operands[3])->isReg() &&
3875 static_cast<ARMOperand*>(Operands[4])->isReg() &&
3876 static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
3878 // Register-register 'add' for thumb does not have a cc_out operand
3879 // when it's an ADD Rdm, SP, {Rdm|#imm0_255} instruction. We do
3880 // have to check the immediate range here since Thumb2 has a variant
3881 // that can handle a different range and has a cc_out operand.
3882 if (((isThumb() && Mnemonic == "add") ||
3883 (isThumbTwo() && Mnemonic == "sub")) &&
3884 Operands.size() == 6 &&
3885 static_cast<ARMOperand*>(Operands[3])->isReg() &&
3886 static_cast<ARMOperand*>(Operands[4])->isReg() &&
3887 static_cast<ARMOperand*>(Operands[4])->getReg() == ARM::SP &&
3888 static_cast<ARMOperand*>(Operands[1])->getReg() == 0 &&
3889 (static_cast<ARMOperand*>(Operands[5])->isReg() ||
3890 static_cast<ARMOperand*>(Operands[5])->isImm0_1020s4()))
3892 // For Thumb2, add/sub immediate does not have a cc_out operand for the
3893 // imm0_4095 variant. That's the least-preferred variant when
3894 // selecting via the generic "add" mnemonic, so to know that we
3895 // should remove the cc_out operand, we have to explicitly check that
3896 // it's not one of the other variants. Ugh.
3897 if (isThumbTwo() && (Mnemonic == "add" || Mnemonic == "sub") &&
3898 Operands.size() == 6 &&
3899 static_cast<ARMOperand*>(Operands[3])->isReg() &&
3900 static_cast<ARMOperand*>(Operands[4])->isReg() &&
3901 static_cast<ARMOperand*>(Operands[5])->isImm()) {
3902 // Nest conditions rather than one big 'if' statement for readability.
3904 // If either register is a high reg, it's either one of the SP
3905 // variants (handled above) or a 32-bit encoding, so we just
3906 // check against T3.
3907 if ((!isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) ||
3908 !isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg())) &&
3909 static_cast<ARMOperand*>(Operands[5])->isT2SOImm())
3911 // If both registers are low, we're in an IT block, and the immediate is
3912 // in range, we should use encoding T1 instead, which has a cc_out.
3914 isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) &&
3915 isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg()) &&
3916 static_cast<ARMOperand*>(Operands[5])->isImm0_7())
3919 // Otherwise, we use encoding T4, which does not have a cc_out
3924 // The thumb2 multiply instruction doesn't have a CCOut register, so
3925 // if we have a "mul" mnemonic in Thumb mode, check if we'll be able to
3926 // use the 16-bit encoding or not.
3927 if (isThumbTwo() && Mnemonic == "mul" && Operands.size() == 6 &&
3928 static_cast<ARMOperand*>(Operands[1])->getReg() == 0 &&
3929 static_cast<ARMOperand*>(Operands[3])->isReg() &&
3930 static_cast<ARMOperand*>(Operands[4])->isReg() &&
3931 static_cast<ARMOperand*>(Operands[5])->isReg() &&
3932 // If the registers aren't low regs, the destination reg isn't the
3933 // same as one of the source regs, or the cc_out operand is zero
3934 // outside of an IT block, we have to use the 32-bit encoding, so
3935 // remove the cc_out operand.
3936 (!isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) ||
3937 !isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg()) ||
3939 (static_cast<ARMOperand*>(Operands[3])->getReg() !=
3940 static_cast<ARMOperand*>(Operands[5])->getReg() &&
3941 static_cast<ARMOperand*>(Operands[3])->getReg() !=
3942 static_cast<ARMOperand*>(Operands[4])->getReg())))
3947 // Register-register 'add/sub' for thumb does not have a cc_out operand
3948 // when it's an ADD/SUB SP, #imm. Be lenient on count since there's also
3949 // the "add/sub SP, SP, #imm" version. If the follow-up operands aren't
3950 // right, this will result in better diagnostics (which operand is off)
3952 if (isThumb() && (Mnemonic == "add" || Mnemonic == "sub") &&
3953 (Operands.size() == 5 || Operands.size() == 6) &&
3954 static_cast<ARMOperand*>(Operands[3])->isReg() &&
3955 static_cast<ARMOperand*>(Operands[3])->getReg() == ARM::SP &&
3956 static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
3962 /// Parse an arm instruction mnemonic followed by its operands.
3963 bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
3964 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3965 // Create the leading tokens for the mnemonic, split by '.' characters.
3966 size_t Start = 0, Next = Name.find('.');
3967 StringRef Mnemonic = Name.slice(Start, Next);
3969 // Split out the predication code and carry setting flag from the mnemonic.
3970 unsigned PredicationCode;
3971 unsigned ProcessorIMod;
3974 Mnemonic = splitMnemonic(Mnemonic, PredicationCode, CarrySetting,
3975 ProcessorIMod, ITMask);
3977 // In Thumb1, only the branch (B) instruction can be predicated.
3978 if (isThumbOne() && PredicationCode != ARMCC::AL && Mnemonic != "b") {
3979 Parser.EatToEndOfStatement();
3980 return Error(NameLoc, "conditional execution not supported in Thumb1");
3983 Operands.push_back(ARMOperand::CreateToken(Mnemonic, NameLoc));
3985 // Handle the IT instruction ITMask. Convert it to a bitmask. This
3986 // is the mask as it will be for the IT encoding if the conditional
3987 // encoding has a '1' as it's bit0 (i.e. 't' ==> '1'). In the case
3988 // where the conditional bit0 is zero, the instruction post-processing
3989 // will adjust the mask accordingly.
3990 if (Mnemonic == "it") {
3991 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + 2);
3992 if (ITMask.size() > 3) {
3993 Parser.EatToEndOfStatement();
3994 return Error(Loc, "too many conditions on IT instruction");
3997 for (unsigned i = ITMask.size(); i != 0; --i) {
3998 char pos = ITMask[i - 1];
3999 if (pos != 't' && pos != 'e') {
4000 Parser.EatToEndOfStatement();
4001 return Error(Loc, "illegal IT block condition mask '" + ITMask + "'");
4004 if (ITMask[i - 1] == 't')
4007 Operands.push_back(ARMOperand::CreateITMask(Mask, Loc));
4010 // FIXME: This is all a pretty gross hack. We should automatically handle
4011 // optional operands like this via tblgen.
4013 // Next, add the CCOut and ConditionCode operands, if needed.
4015 // For mnemonics which can ever incorporate a carry setting bit or predication
4016 // code, our matching model involves us always generating CCOut and
4017 // ConditionCode operands to match the mnemonic "as written" and then we let
4018 // the matcher deal with finding the right instruction or generating an
4019 // appropriate error.
4020 bool CanAcceptCarrySet, CanAcceptPredicationCode;
4021 getMnemonicAcceptInfo(Mnemonic, CanAcceptCarrySet, CanAcceptPredicationCode);
4023 // If we had a carry-set on an instruction that can't do that, issue an
4025 if (!CanAcceptCarrySet && CarrySetting) {
4026 Parser.EatToEndOfStatement();
4027 return Error(NameLoc, "instruction '" + Mnemonic +
4028 "' can not set flags, but 's' suffix specified");
4030 // If we had a predication code on an instruction that can't do that, issue an
4032 if (!CanAcceptPredicationCode && PredicationCode != ARMCC::AL) {
4033 Parser.EatToEndOfStatement();
4034 return Error(NameLoc, "instruction '" + Mnemonic +
4035 "' is not predicable, but condition code specified");
4038 // Add the carry setting operand, if necessary.
4039 if (CanAcceptCarrySet) {
4040 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size());
4041 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
4045 // Add the predication code operand, if necessary.
4046 if (CanAcceptPredicationCode) {
4047 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size() +
4049 Operands.push_back(ARMOperand::CreateCondCode(
4050 ARMCC::CondCodes(PredicationCode), Loc));
4053 // Add the processor imod operand, if necessary.
4054 if (ProcessorIMod) {
4055 Operands.push_back(ARMOperand::CreateImm(
4056 MCConstantExpr::Create(ProcessorIMod, getContext()),
4060 // Add the remaining tokens in the mnemonic.
4061 while (Next != StringRef::npos) {
4063 Next = Name.find('.', Start + 1);
4064 StringRef ExtraToken = Name.slice(Start, Next);
4066 // For now, we're only parsing Thumb1 (for the most part), so
4067 // just ignore ".n" qualifiers. We'll use them to restrict
4068 // matching when we do Thumb2.
4069 if (ExtraToken != ".n") {
4070 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Start);
4071 Operands.push_back(ARMOperand::CreateToken(ExtraToken, Loc));
4075 // Read the remaining operands.
4076 if (getLexer().isNot(AsmToken::EndOfStatement)) {
4077 // Read the first operand.
4078 if (parseOperand(Operands, Mnemonic)) {
4079 Parser.EatToEndOfStatement();
4083 while (getLexer().is(AsmToken::Comma)) {
4084 Parser.Lex(); // Eat the comma.
4086 // Parse and remember the operand.
4087 if (parseOperand(Operands, Mnemonic)) {
4088 Parser.EatToEndOfStatement();
4094 if (getLexer().isNot(AsmToken::EndOfStatement)) {
4095 SMLoc Loc = getLexer().getLoc();
4096 Parser.EatToEndOfStatement();
4097 return Error(Loc, "unexpected token in argument list");
4100 Parser.Lex(); // Consume the EndOfStatement
4102 // Some instructions, mostly Thumb, have forms for the same mnemonic that
4103 // do and don't have a cc_out optional-def operand. With some spot-checks
4104 // of the operand list, we can figure out which variant we're trying to
4105 // parse and adjust accordingly before actually matching. We shouldn't ever
4106 // try to remove a cc_out operand that was explicitly set on the the
4107 // mnemonic, of course (CarrySetting == true). Reason number #317 the
4108 // table driven matcher doesn't fit well with the ARM instruction set.
4109 if (!CarrySetting && shouldOmitCCOutOperand(Mnemonic, Operands)) {
4110 ARMOperand *Op = static_cast<ARMOperand*>(Operands[1]);
4111 Operands.erase(Operands.begin() + 1);
4115 // ARM mode 'blx' need special handling, as the register operand version
4116 // is predicable, but the label operand version is not. So, we can't rely
4117 // on the Mnemonic based checking to correctly figure out when to put
4118 // a k_CondCode operand in the list. If we're trying to match the label
4119 // version, remove the k_CondCode operand here.
4120 if (!isThumb() && Mnemonic == "blx" && Operands.size() == 3 &&
4121 static_cast<ARMOperand*>(Operands[2])->isImm()) {
4122 ARMOperand *Op = static_cast<ARMOperand*>(Operands[1]);
4123 Operands.erase(Operands.begin() + 1);
4127 // The vector-compare-to-zero instructions have a literal token "#0" at
4128 // the end that comes to here as an immediate operand. Convert it to a
4129 // token to play nicely with the matcher.
4130 if ((Mnemonic == "vceq" || Mnemonic == "vcge" || Mnemonic == "vcgt" ||
4131 Mnemonic == "vcle" || Mnemonic == "vclt") && Operands.size() == 6 &&
4132 static_cast<ARMOperand*>(Operands[5])->isImm()) {
4133 ARMOperand *Op = static_cast<ARMOperand*>(Operands[5]);
4134 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
4135 if (CE && CE->getValue() == 0) {
4136 Operands.erase(Operands.begin() + 5);
4137 Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
4141 // VCMP{E} does the same thing, but with a different operand count.
4142 if ((Mnemonic == "vcmp" || Mnemonic == "vcmpe") && Operands.size() == 5 &&
4143 static_cast<ARMOperand*>(Operands[4])->isImm()) {
4144 ARMOperand *Op = static_cast<ARMOperand*>(Operands[4]);
4145 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
4146 if (CE && CE->getValue() == 0) {
4147 Operands.erase(Operands.begin() + 4);
4148 Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
4152 // Similarly, the Thumb1 "RSB" instruction has a literal "#0" on the
4153 // end. Convert it to a token here.
4154 if (Mnemonic == "rsb" && isThumb() && Operands.size() == 6 &&
4155 static_cast<ARMOperand*>(Operands[5])->isImm()) {
4156 ARMOperand *Op = static_cast<ARMOperand*>(Operands[5]);
4157 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
4158 if (CE && CE->getValue() == 0) {
4159 Operands.erase(Operands.begin() + 5);
4160 Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
4168 // Validate context-sensitive operand constraints.
4170 // return 'true' if register list contains non-low GPR registers,
4171 // 'false' otherwise. If Reg is in the register list or is HiReg, set
4172 // 'containsReg' to true.
4173 static bool checkLowRegisterList(MCInst Inst, unsigned OpNo, unsigned Reg,
4174 unsigned HiReg, bool &containsReg) {
4175 containsReg = false;
4176 for (unsigned i = OpNo; i < Inst.getNumOperands(); ++i) {
4177 unsigned OpReg = Inst.getOperand(i).getReg();
4180 // Anything other than a low register isn't legal here.
4181 if (!isARMLowRegister(OpReg) && (!HiReg || OpReg != HiReg))
4187 // Check if the specified regisgter is in the register list of the inst,
4188 // starting at the indicated operand number.
4189 static bool listContainsReg(MCInst &Inst, unsigned OpNo, unsigned Reg) {
4190 for (unsigned i = OpNo; i < Inst.getNumOperands(); ++i) {
4191 unsigned OpReg = Inst.getOperand(i).getReg();
4198 // FIXME: We would really prefer to have MCInstrInfo (the wrapper around
4199 // the ARMInsts array) instead. Getting that here requires awkward
4200 // API changes, though. Better way?
4202 extern MCInstrDesc ARMInsts[];
4204 static MCInstrDesc &getInstDesc(unsigned Opcode) {
4205 return ARMInsts[Opcode];
4208 // FIXME: We would really like to be able to tablegen'erate this.
4210 validateInstruction(MCInst &Inst,
4211 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
4212 MCInstrDesc &MCID = getInstDesc(Inst.getOpcode());
4213 SMLoc Loc = Operands[0]->getStartLoc();
4214 // Check the IT block state first.
4215 // NOTE: In Thumb mode, the BKPT instruction has the interesting property of
4216 // being allowed in IT blocks, but not being predicable. It just always
4218 if (inITBlock() && Inst.getOpcode() != ARM::tBKPT) {
4220 if (ITState.FirstCond)
4221 ITState.FirstCond = false;
4223 bit = (ITState.Mask >> (5 - ITState.CurPosition)) & 1;
4224 // The instruction must be predicable.
4225 if (!MCID.isPredicable())
4226 return Error(Loc, "instructions in IT block must be predicable");
4227 unsigned Cond = Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm();
4228 unsigned ITCond = bit ? ITState.Cond :
4229 ARMCC::getOppositeCondition(ITState.Cond);
4230 if (Cond != ITCond) {
4231 // Find the condition code Operand to get its SMLoc information.
4233 for (unsigned i = 1; i < Operands.size(); ++i)
4234 if (static_cast<ARMOperand*>(Operands[i])->isCondCode())
4235 CondLoc = Operands[i]->getStartLoc();
4236 return Error(CondLoc, "incorrect condition in IT block; got '" +
4237 StringRef(ARMCondCodeToString(ARMCC::CondCodes(Cond))) +
4238 "', but expected '" +
4239 ARMCondCodeToString(ARMCC::CondCodes(ITCond)) + "'");
4241 // Check for non-'al' condition codes outside of the IT block.
4242 } else if (isThumbTwo() && MCID.isPredicable() &&
4243 Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm() !=
4244 ARMCC::AL && Inst.getOpcode() != ARM::tB &&
4245 Inst.getOpcode() != ARM::t2B)
4246 return Error(Loc, "predicated instructions must be in IT block");
4248 switch (Inst.getOpcode()) {
4251 case ARM::LDRD_POST:
4253 // Rt2 must be Rt + 1.
4254 unsigned Rt = getARMRegisterNumbering(Inst.getOperand(0).getReg());
4255 unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(1).getReg());
4257 return Error(Operands[3]->getStartLoc(),
4258 "destination operands must be sequential");
4262 // Rt2 must be Rt + 1.
4263 unsigned Rt = getARMRegisterNumbering(Inst.getOperand(0).getReg());
4264 unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(1).getReg());
4266 return Error(Operands[3]->getStartLoc(),
4267 "source operands must be sequential");
4271 case ARM::STRD_POST:
4273 // Rt2 must be Rt + 1.
4274 unsigned Rt = getARMRegisterNumbering(Inst.getOperand(1).getReg());
4275 unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(2).getReg());
4277 return Error(Operands[3]->getStartLoc(),
4278 "source operands must be sequential");
4283 // width must be in range [1, 32-lsb]
4284 unsigned lsb = Inst.getOperand(2).getImm();
4285 unsigned widthm1 = Inst.getOperand(3).getImm();
4286 if (widthm1 >= 32 - lsb)
4287 return Error(Operands[5]->getStartLoc(),
4288 "bitfield width must be in range [1,32-lsb]");
4292 // If we're parsing Thumb2, the .w variant is available and handles
4293 // most cases that are normally illegal for a Thumb1 LDM
4294 // instruction. We'll make the transformation in processInstruction()
4297 // Thumb LDM instructions are writeback iff the base register is not
4298 // in the register list.
4299 unsigned Rn = Inst.getOperand(0).getReg();
4300 bool hasWritebackToken =
4301 (static_cast<ARMOperand*>(Operands[3])->isToken() &&
4302 static_cast<ARMOperand*>(Operands[3])->getToken() == "!");
4303 bool listContainsBase;
4304 if (checkLowRegisterList(Inst, 3, Rn, 0, listContainsBase) && !isThumbTwo())
4305 return Error(Operands[3 + hasWritebackToken]->getStartLoc(),
4306 "registers must be in range r0-r7");
4307 // If we should have writeback, then there should be a '!' token.
4308 if (!listContainsBase && !hasWritebackToken && !isThumbTwo())
4309 return Error(Operands[2]->getStartLoc(),
4310 "writeback operator '!' expected");
4311 // If we should not have writeback, there must not be a '!'. This is
4312 // true even for the 32-bit wide encodings.
4313 if (listContainsBase && hasWritebackToken)
4314 return Error(Operands[3]->getStartLoc(),
4315 "writeback operator '!' not allowed when base register "
4316 "in register list");
4320 case ARM::t2LDMIA_UPD: {
4321 if (listContainsReg(Inst, 3, Inst.getOperand(0).getReg()))
4322 return Error(Operands[4]->getStartLoc(),
4323 "writeback operator '!' not allowed when base register "
4324 "in register list");
4328 bool listContainsBase;
4329 if (checkLowRegisterList(Inst, 3, 0, ARM::PC, listContainsBase))
4330 return Error(Operands[2]->getStartLoc(),
4331 "registers must be in range r0-r7 or pc");
4335 bool listContainsBase;
4336 if (checkLowRegisterList(Inst, 3, 0, ARM::LR, listContainsBase))
4337 return Error(Operands[2]->getStartLoc(),
4338 "registers must be in range r0-r7 or lr");
4341 case ARM::tSTMIA_UPD: {
4342 bool listContainsBase;
4343 if (checkLowRegisterList(Inst, 4, 0, 0, listContainsBase) && !isThumbTwo())
4344 return Error(Operands[4]->getStartLoc(),
4345 "registers must be in range r0-r7");
4354 processInstruction(MCInst &Inst,
4355 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
4356 switch (Inst.getOpcode()) {
4357 case ARM::LDMIA_UPD:
4358 // If this is a load of a single register via a 'pop', then we should use
4359 // a post-indexed LDR instruction instead, per the ARM ARM.
4360 if (static_cast<ARMOperand*>(Operands[0])->getToken() == "pop" &&
4361 Inst.getNumOperands() == 5) {
4363 TmpInst.setOpcode(ARM::LDR_POST_IMM);
4364 TmpInst.addOperand(Inst.getOperand(4)); // Rt
4365 TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
4366 TmpInst.addOperand(Inst.getOperand(1)); // Rn
4367 TmpInst.addOperand(MCOperand::CreateReg(0)); // am2offset
4368 TmpInst.addOperand(MCOperand::CreateImm(4));
4369 TmpInst.addOperand(Inst.getOperand(2)); // CondCode
4370 TmpInst.addOperand(Inst.getOperand(3));
4374 case ARM::STMDB_UPD:
4375 // If this is a store of a single register via a 'push', then we should use
4376 // a pre-indexed STR instruction instead, per the ARM ARM.
4377 if (static_cast<ARMOperand*>(Operands[0])->getToken() == "push" &&
4378 Inst.getNumOperands() == 5) {
4380 TmpInst.setOpcode(ARM::STR_PRE_IMM);
4381 TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
4382 TmpInst.addOperand(Inst.getOperand(4)); // Rt
4383 TmpInst.addOperand(Inst.getOperand(1)); // addrmode_imm12
4384 TmpInst.addOperand(MCOperand::CreateImm(-4));
4385 TmpInst.addOperand(Inst.getOperand(2)); // CondCode
4386 TmpInst.addOperand(Inst.getOperand(3));
4391 // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
4392 // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
4393 // to encoding T2 if <Rd> is specified and encoding T2 is preferred
4394 // to encoding T1 if <Rd> is omitted."
4395 if (Inst.getOperand(3).getImm() < 8 && Operands.size() == 6)
4396 Inst.setOpcode(ARM::tADDi3);
4399 // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
4400 // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
4401 // to encoding T2 if <Rd> is specified and encoding T2 is preferred
4402 // to encoding T1 if <Rd> is omitted."
4403 if (Inst.getOperand(3).getImm() < 8 && Operands.size() == 6)
4404 Inst.setOpcode(ARM::tSUBi3);
4407 // A Thumb conditional branch outside of an IT block is a tBcc.
4408 if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock())
4409 Inst.setOpcode(ARM::tBcc);
4412 // A Thumb2 conditional branch outside of an IT block is a t2Bcc.
4413 if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock())
4414 Inst.setOpcode(ARM::t2Bcc);
4417 // If the conditional is AL or we're in an IT block, we really want t2B.
4418 if (Inst.getOperand(1).getImm() == ARMCC::AL || inITBlock())
4419 Inst.setOpcode(ARM::t2B);
4422 // If the conditional is AL, we really want tB.
4423 if (Inst.getOperand(1).getImm() == ARMCC::AL)
4424 Inst.setOpcode(ARM::tB);
4427 // If the register list contains any high registers, or if the writeback
4428 // doesn't match what tLDMIA can do, we need to use the 32-bit encoding
4429 // instead if we're in Thumb2. Otherwise, this should have generated
4430 // an error in validateInstruction().
4431 unsigned Rn = Inst.getOperand(0).getReg();
4432 bool hasWritebackToken =
4433 (static_cast<ARMOperand*>(Operands[3])->isToken() &&
4434 static_cast<ARMOperand*>(Operands[3])->getToken() == "!");
4435 bool listContainsBase;
4436 if (checkLowRegisterList(Inst, 3, Rn, 0, listContainsBase) ||
4437 (!listContainsBase && !hasWritebackToken) ||
4438 (listContainsBase && hasWritebackToken)) {
4439 // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
4440 assert (isThumbTwo());
4441 Inst.setOpcode(hasWritebackToken ? ARM::t2LDMIA_UPD : ARM::t2LDMIA);
4442 // If we're switching to the updating version, we need to insert
4443 // the writeback tied operand.
4444 if (hasWritebackToken)
4445 Inst.insert(Inst.begin(),
4446 MCOperand::CreateReg(Inst.getOperand(0).getReg()));
4450 case ARM::tSTMIA_UPD: {
4451 // If the register list contains any high registers, we need to use
4452 // the 32-bit encoding instead if we're in Thumb2. Otherwise, this
4453 // should have generated an error in validateInstruction().
4454 unsigned Rn = Inst.getOperand(0).getReg();
4455 bool listContainsBase;
4456 if (checkLowRegisterList(Inst, 4, Rn, 0, listContainsBase)) {
4457 // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
4458 assert (isThumbTwo());
4459 Inst.setOpcode(ARM::t2STMIA_UPD);
4464 // If we can use the 16-bit encoding and the user didn't explicitly
4465 // request the 32-bit variant, transform it here.
4466 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
4467 Inst.getOperand(1).getImm() <= 255 &&
4468 ((!inITBlock() && Inst.getOperand(2).getImm() == ARMCC::AL &&
4469 Inst.getOperand(4).getReg() == ARM::CPSR) ||
4470 (inITBlock() && Inst.getOperand(4).getReg() == 0)) &&
4471 (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
4472 static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
4473 // The operands aren't in the same order for tMOVi8...
4475 TmpInst.setOpcode(ARM::tMOVi8);
4476 TmpInst.addOperand(Inst.getOperand(0));
4477 TmpInst.addOperand(Inst.getOperand(4));
4478 TmpInst.addOperand(Inst.getOperand(1));
4479 TmpInst.addOperand(Inst.getOperand(2));
4480 TmpInst.addOperand(Inst.getOperand(3));
4486 // If we can use the 16-bit encoding and the user didn't explicitly
4487 // request the 32-bit variant, transform it here.
4488 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
4489 isARMLowRegister(Inst.getOperand(1).getReg()) &&
4490 Inst.getOperand(2).getImm() == ARMCC::AL &&
4491 Inst.getOperand(4).getReg() == ARM::CPSR &&
4492 (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
4493 static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
4494 // The operands aren't the same for tMOV[S]r... (no cc_out)
4496 TmpInst.setOpcode(Inst.getOperand(4).getReg() ? ARM::tMOVSr : ARM::tMOVr);
4497 TmpInst.addOperand(Inst.getOperand(0));
4498 TmpInst.addOperand(Inst.getOperand(1));
4499 TmpInst.addOperand(Inst.getOperand(2));
4500 TmpInst.addOperand(Inst.getOperand(3));
4509 // If we can use the 16-bit encoding and the user didn't explicitly
4510 // request the 32-bit variant, transform it here.
4511 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
4512 isARMLowRegister(Inst.getOperand(1).getReg()) &&
4513 Inst.getOperand(2).getImm() == 0 &&
4514 (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
4515 static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
4517 switch (Inst.getOpcode()) {
4518 default: llvm_unreachable("Illegal opcode!");
4519 case ARM::t2SXTH: NewOpc = ARM::tSXTH; break;
4520 case ARM::t2SXTB: NewOpc = ARM::tSXTB; break;
4521 case ARM::t2UXTH: NewOpc = ARM::tUXTH; break;
4522 case ARM::t2UXTB: NewOpc = ARM::tUXTB; break;
4524 // The operands aren't the same for thumb1 (no rotate operand).
4526 TmpInst.setOpcode(NewOpc);
4527 TmpInst.addOperand(Inst.getOperand(0));
4528 TmpInst.addOperand(Inst.getOperand(1));
4529 TmpInst.addOperand(Inst.getOperand(3));
4530 TmpInst.addOperand(Inst.getOperand(4));
4536 // The mask bits for all but the first condition are represented as
4537 // the low bit of the condition code value implies 't'. We currently
4538 // always have 1 implies 't', so XOR toggle the bits if the low bit
4539 // of the condition code is zero. The encoding also expects the low
4540 // bit of the condition to be encoded as bit 4 of the mask operand,
4541 // so mask that in if needed
4542 MCOperand &MO = Inst.getOperand(1);
4543 unsigned Mask = MO.getImm();
4544 unsigned OrigMask = Mask;
4545 unsigned TZ = CountTrailingZeros_32(Mask);
4546 if ((Inst.getOperand(0).getImm() & 1) == 0) {
4547 assert(Mask && TZ <= 3 && "illegal IT mask value!");
4548 for (unsigned i = 3; i != TZ; --i)
4554 // Set up the IT block state according to the IT instruction we just
4556 assert(!inITBlock() && "nested IT blocks?!");
4557 ITState.Cond = ARMCC::CondCodes(Inst.getOperand(0).getImm());
4558 ITState.Mask = OrigMask; // Use the original mask, not the updated one.
4559 ITState.CurPosition = 0;
4560 ITState.FirstCond = true;
4566 unsigned ARMAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
4567 // 16-bit thumb arithmetic instructions either require or preclude the 'S'
4568 // suffix depending on whether they're in an IT block or not.
4569 unsigned Opc = Inst.getOpcode();
4570 MCInstrDesc &MCID = getInstDesc(Opc);
4571 if (MCID.TSFlags & ARMII::ThumbArithFlagSetting) {
4572 assert(MCID.hasOptionalDef() &&
4573 "optionally flag setting instruction missing optional def operand");
4574 assert(MCID.NumOperands == Inst.getNumOperands() &&
4575 "operand count mismatch!");
4576 // Find the optional-def operand (cc_out).
4579 !MCID.OpInfo[OpNo].isOptionalDef() && OpNo < MCID.NumOperands;
4582 // If we're parsing Thumb1, reject it completely.
4583 if (isThumbOne() && Inst.getOperand(OpNo).getReg() != ARM::CPSR)
4584 return Match_MnemonicFail;
4585 // If we're parsing Thumb2, which form is legal depends on whether we're
4587 if (isThumbTwo() && Inst.getOperand(OpNo).getReg() != ARM::CPSR &&
4589 return Match_RequiresITBlock;
4590 if (isThumbTwo() && Inst.getOperand(OpNo).getReg() == ARM::CPSR &&
4592 return Match_RequiresNotITBlock;
4594 // Some high-register supporting Thumb1 encodings only allow both registers
4595 // to be from r0-r7 when in Thumb2.
4596 else if (Opc == ARM::tADDhirr && isThumbOne() &&
4597 isARMLowRegister(Inst.getOperand(1).getReg()) &&
4598 isARMLowRegister(Inst.getOperand(2).getReg()))
4599 return Match_RequiresThumb2;
4600 // Others only require ARMv6 or later.
4601 else if (Opc == ARM::tMOVr && isThumbOne() && !hasV6Ops() &&
4602 isARMLowRegister(Inst.getOperand(0).getReg()) &&
4603 isARMLowRegister(Inst.getOperand(1).getReg()))
4604 return Match_RequiresV6;
4605 return Match_Success;
4609 MatchAndEmitInstruction(SMLoc IDLoc,
4610 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
4614 unsigned MatchResult;
4615 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
4616 switch (MatchResult) {
4619 // Context sensitive operand constraints aren't handled by the matcher,
4620 // so check them here.
4621 if (validateInstruction(Inst, Operands)) {
4622 // Still progress the IT block, otherwise one wrong condition causes
4623 // nasty cascading errors.
4624 forwardITPosition();
4628 // Some instructions need post-processing to, for example, tweak which
4629 // encoding is selected.
4630 processInstruction(Inst, Operands);
4632 // Only move forward at the very end so that everything in validate
4633 // and process gets a consistent answer about whether we're in an IT
4635 forwardITPosition();
4637 Out.EmitInstruction(Inst);
4639 case Match_MissingFeature:
4640 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
4642 case Match_InvalidOperand: {
4643 SMLoc ErrorLoc = IDLoc;
4644 if (ErrorInfo != ~0U) {
4645 if (ErrorInfo >= Operands.size())
4646 return Error(IDLoc, "too few operands for instruction");
4648 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
4649 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
4652 return Error(ErrorLoc, "invalid operand for instruction");
4654 case Match_MnemonicFail:
4655 return Error(IDLoc, "invalid instruction");
4656 case Match_ConversionFail:
4657 // The converter function will have already emited a diagnostic.
4659 case Match_RequiresNotITBlock:
4660 return Error(IDLoc, "flag setting instruction only valid outside IT block");
4661 case Match_RequiresITBlock:
4662 return Error(IDLoc, "instruction only valid inside IT block");
4663 case Match_RequiresV6:
4664 return Error(IDLoc, "instruction variant requires ARMv6 or later");
4665 case Match_RequiresThumb2:
4666 return Error(IDLoc, "instruction variant requires Thumb2");
4669 llvm_unreachable("Implement any new match types added!");
4673 /// parseDirective parses the arm specific directives
4674 bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
4675 StringRef IDVal = DirectiveID.getIdentifier();
4676 if (IDVal == ".word")
4677 return parseDirectiveWord(4, DirectiveID.getLoc());
4678 else if (IDVal == ".thumb")
4679 return parseDirectiveThumb(DirectiveID.getLoc());
4680 else if (IDVal == ".thumb_func")
4681 return parseDirectiveThumbFunc(DirectiveID.getLoc());
4682 else if (IDVal == ".code")
4683 return parseDirectiveCode(DirectiveID.getLoc());
4684 else if (IDVal == ".syntax")
4685 return parseDirectiveSyntax(DirectiveID.getLoc());
4689 /// parseDirectiveWord
4690 /// ::= .word [ expression (, expression)* ]
4691 bool ARMAsmParser::parseDirectiveWord(unsigned Size, SMLoc L) {
4692 if (getLexer().isNot(AsmToken::EndOfStatement)) {
4694 const MCExpr *Value;
4695 if (getParser().ParseExpression(Value))
4698 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
4700 if (getLexer().is(AsmToken::EndOfStatement))
4703 // FIXME: Improve diagnostic.
4704 if (getLexer().isNot(AsmToken::Comma))
4705 return Error(L, "unexpected token in directive");
4714 /// parseDirectiveThumb
4716 bool ARMAsmParser::parseDirectiveThumb(SMLoc L) {
4717 if (getLexer().isNot(AsmToken::EndOfStatement))
4718 return Error(L, "unexpected token in directive");
4721 // TODO: set thumb mode
4722 // TODO: tell the MC streamer the mode
4723 // getParser().getStreamer().Emit???();
4727 /// parseDirectiveThumbFunc
4728 /// ::= .thumbfunc symbol_name
4729 bool ARMAsmParser::parseDirectiveThumbFunc(SMLoc L) {
4730 const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
4731 bool isMachO = MAI.hasSubsectionsViaSymbols();
4734 // Darwin asm has function name after .thumb_func direction
4737 const AsmToken &Tok = Parser.getTok();
4738 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
4739 return Error(L, "unexpected token in .thumb_func directive");
4740 Name = Tok.getString();
4741 Parser.Lex(); // Consume the identifier token.
4744 if (getLexer().isNot(AsmToken::EndOfStatement))
4745 return Error(L, "unexpected token in directive");
4748 // FIXME: assuming function name will be the line following .thumb_func
4750 Name = Parser.getTok().getString();
4753 // Mark symbol as a thumb symbol.
4754 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
4755 getParser().getStreamer().EmitThumbFunc(Func);
4759 /// parseDirectiveSyntax
4760 /// ::= .syntax unified | divided
4761 bool ARMAsmParser::parseDirectiveSyntax(SMLoc L) {
4762 const AsmToken &Tok = Parser.getTok();
4763 if (Tok.isNot(AsmToken::Identifier))
4764 return Error(L, "unexpected token in .syntax directive");
4765 StringRef Mode = Tok.getString();
4766 if (Mode == "unified" || Mode == "UNIFIED")
4768 else if (Mode == "divided" || Mode == "DIVIDED")
4769 return Error(L, "'.syntax divided' arm asssembly not supported");
4771 return Error(L, "unrecognized syntax mode in .syntax directive");
4773 if (getLexer().isNot(AsmToken::EndOfStatement))
4774 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
4777 // TODO tell the MC streamer the mode
4778 // getParser().getStreamer().Emit???();
4782 /// parseDirectiveCode
4783 /// ::= .code 16 | 32
4784 bool ARMAsmParser::parseDirectiveCode(SMLoc L) {
4785 const AsmToken &Tok = Parser.getTok();
4786 if (Tok.isNot(AsmToken::Integer))
4787 return Error(L, "unexpected token in .code directive");
4788 int64_t Val = Parser.getTok().getIntVal();
4794 return Error(L, "invalid operand to .code directive");
4796 if (getLexer().isNot(AsmToken::EndOfStatement))
4797 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
4803 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
4807 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
4813 extern "C" void LLVMInitializeARMAsmLexer();
4815 /// Force static initialization.
4816 extern "C" void LLVMInitializeARMAsmParser() {
4817 RegisterMCAsmParser<ARMAsmParser> X(TheARMTarget);
4818 RegisterMCAsmParser<ARMAsmParser> Y(TheThumbTarget);
4819 LLVMInitializeARMAsmLexer();
4822 #define GET_REGISTER_MATCHER
4823 #define GET_MATCHER_IMPLEMENTATION
4824 #include "ARMGenAsmMatcher.inc"