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*>&);
165 // Asm Match Converter Methods
166 bool cvtT2LdrdPre(MCInst &Inst, unsigned Opcode,
167 const SmallVectorImpl<MCParsedAsmOperand*> &);
168 bool cvtT2StrdPre(MCInst &Inst, unsigned Opcode,
169 const SmallVectorImpl<MCParsedAsmOperand*> &);
170 bool cvtLdWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
171 const SmallVectorImpl<MCParsedAsmOperand*> &);
172 bool cvtStWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
173 const SmallVectorImpl<MCParsedAsmOperand*> &);
174 bool cvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
175 const SmallVectorImpl<MCParsedAsmOperand*> &);
176 bool cvtLdWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
177 const SmallVectorImpl<MCParsedAsmOperand*> &);
178 bool cvtStWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
179 const SmallVectorImpl<MCParsedAsmOperand*> &);
180 bool cvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
181 const SmallVectorImpl<MCParsedAsmOperand*> &);
182 bool cvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
183 const SmallVectorImpl<MCParsedAsmOperand*> &);
184 bool cvtLdExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
185 const SmallVectorImpl<MCParsedAsmOperand*> &);
186 bool cvtLdExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
187 const SmallVectorImpl<MCParsedAsmOperand*> &);
188 bool cvtStExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
189 const SmallVectorImpl<MCParsedAsmOperand*> &);
190 bool cvtStExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
191 const SmallVectorImpl<MCParsedAsmOperand*> &);
192 bool cvtLdrdPre(MCInst &Inst, unsigned Opcode,
193 const SmallVectorImpl<MCParsedAsmOperand*> &);
194 bool cvtStrdPre(MCInst &Inst, unsigned Opcode,
195 const SmallVectorImpl<MCParsedAsmOperand*> &);
196 bool cvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
197 const SmallVectorImpl<MCParsedAsmOperand*> &);
198 bool cvtThumbMultiply(MCInst &Inst, unsigned Opcode,
199 const SmallVectorImpl<MCParsedAsmOperand*> &);
201 bool validateInstruction(MCInst &Inst,
202 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
203 void processInstruction(MCInst &Inst,
204 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
205 bool shouldOmitCCOutOperand(StringRef Mnemonic,
206 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
209 enum ARMMatchResultTy {
210 Match_RequiresITBlock = FIRST_TARGET_MATCH_RESULT_TY,
211 Match_RequiresNotITBlock,
216 ARMAsmParser(MCSubtargetInfo &_STI, MCAsmParser &_Parser)
217 : MCTargetAsmParser(), STI(_STI), Parser(_Parser) {
218 MCAsmParserExtension::Initialize(_Parser);
220 // Initialize the set of available features.
221 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
223 // Not in an ITBlock to start with.
224 ITState.CurPosition = ~0U;
227 // Implementation of the MCTargetAsmParser interface:
228 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
229 bool ParseInstruction(StringRef Name, SMLoc NameLoc,
230 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
231 bool ParseDirective(AsmToken DirectiveID);
233 unsigned checkTargetMatchPredicate(MCInst &Inst);
235 bool MatchAndEmitInstruction(SMLoc IDLoc,
236 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
239 } // end anonymous namespace
243 /// ARMOperand - Instances of this class represent a parsed ARM machine
245 class ARMOperand : public MCParsedAsmOperand {
269 k_BitfieldDescriptor,
273 SMLoc StartLoc, EndLoc;
274 SmallVector<unsigned, 8> Registers;
278 ARMCC::CondCodes Val;
298 ARM_PROC::IFlags Val;
323 unsigned Val; // encoded 8-bit representation
326 /// Combined record for all forms of ARM address expressions.
329 // Offset is in OffsetReg or OffsetImm. If both are zero, no offset
331 const MCConstantExpr *OffsetImm; // Offset immediate value
332 unsigned OffsetRegNum; // Offset register num, when OffsetImm == NULL
333 ARM_AM::ShiftOpc ShiftType; // Shift type for OffsetReg
334 unsigned ShiftImm; // shift for OffsetReg.
335 unsigned Alignment; // 0 = no alignment specified
336 // n = alignment in bytes (8, 16, or 32)
337 unsigned isNegative : 1; // Negated OffsetReg? (~'U' bit)
343 ARM_AM::ShiftOpc ShiftTy;
352 ARM_AM::ShiftOpc ShiftTy;
358 ARM_AM::ShiftOpc ShiftTy;
371 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
373 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
375 StartLoc = o.StartLoc;
392 case k_DPRRegisterList:
393 case k_SPRRegisterList:
394 Registers = o.Registers;
401 CoprocOption = o.CoprocOption;
409 case k_MemBarrierOpt:
415 case k_PostIndexRegister:
416 PostIdxReg = o.PostIdxReg;
424 case k_ShifterImmediate:
425 ShifterImm = o.ShifterImm;
427 case k_ShiftedRegister:
428 RegShiftedReg = o.RegShiftedReg;
430 case k_ShiftedImmediate:
431 RegShiftedImm = o.RegShiftedImm;
433 case k_RotateImmediate:
436 case k_BitfieldDescriptor:
437 Bitfield = o.Bitfield;
440 VectorIndex = o.VectorIndex;
445 /// getStartLoc - Get the location of the first token of this operand.
446 SMLoc getStartLoc() const { return StartLoc; }
447 /// getEndLoc - Get the location of the last token of this operand.
448 SMLoc getEndLoc() const { return EndLoc; }
450 ARMCC::CondCodes getCondCode() const {
451 assert(Kind == k_CondCode && "Invalid access!");
455 unsigned getCoproc() const {
456 assert((Kind == k_CoprocNum || Kind == k_CoprocReg) && "Invalid access!");
460 StringRef getToken() const {
461 assert(Kind == k_Token && "Invalid access!");
462 return StringRef(Tok.Data, Tok.Length);
465 unsigned getReg() const {
466 assert((Kind == k_Register || Kind == k_CCOut) && "Invalid access!");
470 const SmallVectorImpl<unsigned> &getRegList() const {
471 assert((Kind == k_RegisterList || Kind == k_DPRRegisterList ||
472 Kind == k_SPRRegisterList) && "Invalid access!");
476 const MCExpr *getImm() const {
477 assert(Kind == k_Immediate && "Invalid access!");
481 unsigned getFPImm() const {
482 assert(Kind == k_FPImmediate && "Invalid access!");
486 unsigned getVectorIndex() const {
487 assert(Kind == k_VectorIndex && "Invalid access!");
488 return VectorIndex.Val;
491 ARM_MB::MemBOpt getMemBarrierOpt() const {
492 assert(Kind == k_MemBarrierOpt && "Invalid access!");
496 ARM_PROC::IFlags getProcIFlags() const {
497 assert(Kind == k_ProcIFlags && "Invalid access!");
501 unsigned getMSRMask() const {
502 assert(Kind == k_MSRMask && "Invalid access!");
506 bool isCoprocNum() const { return Kind == k_CoprocNum; }
507 bool isCoprocReg() const { return Kind == k_CoprocReg; }
508 bool isCoprocOption() const { return Kind == k_CoprocOption; }
509 bool isCondCode() const { return Kind == k_CondCode; }
510 bool isCCOut() const { return Kind == k_CCOut; }
511 bool isITMask() const { return Kind == k_ITCondMask; }
512 bool isITCondCode() const { return Kind == k_CondCode; }
513 bool isImm() const { return Kind == k_Immediate; }
514 bool isFPImm() const { return Kind == k_FPImmediate; }
515 bool isImm8s4() const {
516 if (Kind != k_Immediate)
518 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
519 if (!CE) return false;
520 int64_t Value = CE->getValue();
521 return ((Value & 3) == 0) && Value >= -1020 && Value <= 1020;
523 bool isImm0_1020s4() const {
524 if (Kind != k_Immediate)
526 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
527 if (!CE) return false;
528 int64_t Value = CE->getValue();
529 return ((Value & 3) == 0) && Value >= 0 && Value <= 1020;
531 bool isImm0_508s4() const {
532 if (Kind != k_Immediate)
534 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
535 if (!CE) return false;
536 int64_t Value = CE->getValue();
537 return ((Value & 3) == 0) && Value >= 0 && Value <= 508;
539 bool isImm0_255() const {
540 if (Kind != k_Immediate)
542 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
543 if (!CE) return false;
544 int64_t Value = CE->getValue();
545 return Value >= 0 && Value < 256;
547 bool isImm0_7() const {
548 if (Kind != k_Immediate)
550 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
551 if (!CE) return false;
552 int64_t Value = CE->getValue();
553 return Value >= 0 && Value < 8;
555 bool isImm0_15() const {
556 if (Kind != k_Immediate)
558 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
559 if (!CE) return false;
560 int64_t Value = CE->getValue();
561 return Value >= 0 && Value < 16;
563 bool isImm0_31() const {
564 if (Kind != k_Immediate)
566 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
567 if (!CE) return false;
568 int64_t Value = CE->getValue();
569 return Value >= 0 && Value < 32;
571 bool isImm1_16() const {
572 if (Kind != k_Immediate)
574 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
575 if (!CE) return false;
576 int64_t Value = CE->getValue();
577 return Value > 0 && Value < 17;
579 bool isImm1_32() const {
580 if (Kind != k_Immediate)
582 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
583 if (!CE) return false;
584 int64_t Value = CE->getValue();
585 return Value > 0 && Value < 33;
587 bool isImm0_65535() const {
588 if (Kind != k_Immediate)
590 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
591 if (!CE) return false;
592 int64_t Value = CE->getValue();
593 return Value >= 0 && Value < 65536;
595 bool isImm0_65535Expr() const {
596 if (Kind != k_Immediate)
598 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
599 // If it's not a constant expression, it'll generate a fixup and be
601 if (!CE) return true;
602 int64_t Value = CE->getValue();
603 return Value >= 0 && Value < 65536;
605 bool isImm24bit() const {
606 if (Kind != k_Immediate)
608 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
609 if (!CE) return false;
610 int64_t Value = CE->getValue();
611 return Value >= 0 && Value <= 0xffffff;
613 bool isImmThumbSR() const {
614 if (Kind != k_Immediate)
616 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
617 if (!CE) return false;
618 int64_t Value = CE->getValue();
619 return Value > 0 && Value < 33;
621 bool isPKHLSLImm() const {
622 if (Kind != k_Immediate)
624 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
625 if (!CE) return false;
626 int64_t Value = CE->getValue();
627 return Value >= 0 && Value < 32;
629 bool isPKHASRImm() const {
630 if (Kind != k_Immediate)
632 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
633 if (!CE) return false;
634 int64_t Value = CE->getValue();
635 return Value > 0 && Value <= 32;
637 bool isARMSOImm() const {
638 if (Kind != k_Immediate)
640 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
641 if (!CE) return false;
642 int64_t Value = CE->getValue();
643 return ARM_AM::getSOImmVal(Value) != -1;
645 bool isT2SOImm() const {
646 if (Kind != k_Immediate)
648 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
649 if (!CE) return false;
650 int64_t Value = CE->getValue();
651 return ARM_AM::getT2SOImmVal(Value) != -1;
653 bool isSetEndImm() const {
654 if (Kind != k_Immediate)
656 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
657 if (!CE) return false;
658 int64_t Value = CE->getValue();
659 return Value == 1 || Value == 0;
661 bool isReg() const { return Kind == k_Register; }
662 bool isRegList() const { return Kind == k_RegisterList; }
663 bool isDPRRegList() const { return Kind == k_DPRRegisterList; }
664 bool isSPRRegList() const { return Kind == k_SPRRegisterList; }
665 bool isToken() const { return Kind == k_Token; }
666 bool isMemBarrierOpt() const { return Kind == k_MemBarrierOpt; }
667 bool isMemory() const { return Kind == k_Memory; }
668 bool isShifterImm() const { return Kind == k_ShifterImmediate; }
669 bool isRegShiftedReg() const { return Kind == k_ShiftedRegister; }
670 bool isRegShiftedImm() const { return Kind == k_ShiftedImmediate; }
671 bool isRotImm() const { return Kind == k_RotateImmediate; }
672 bool isBitfield() const { return Kind == k_BitfieldDescriptor; }
673 bool isPostIdxRegShifted() const { return Kind == k_PostIndexRegister; }
674 bool isPostIdxReg() const {
675 return Kind == k_PostIndexRegister && PostIdxReg.ShiftTy == ARM_AM::no_shift;
677 bool isMemNoOffset(bool alignOK = false) const {
680 // No offset of any kind.
681 return Memory.OffsetRegNum == 0 && Memory.OffsetImm == 0 &&
682 (alignOK || Memory.Alignment == 0);
684 bool isAlignedMemory() const {
685 return isMemNoOffset(true);
687 bool isAddrMode2() const {
688 if (!isMemory() || Memory.Alignment != 0) return false;
689 // Check for register offset.
690 if (Memory.OffsetRegNum) return true;
691 // Immediate offset in range [-4095, 4095].
692 if (!Memory.OffsetImm) return true;
693 int64_t Val = Memory.OffsetImm->getValue();
694 return Val > -4096 && Val < 4096;
696 bool isAM2OffsetImm() const {
697 if (Kind != k_Immediate)
699 // Immediate offset in range [-4095, 4095].
700 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
701 if (!CE) return false;
702 int64_t Val = CE->getValue();
703 return Val > -4096 && Val < 4096;
705 bool isAddrMode3() const {
706 if (!isMemory() || Memory.Alignment != 0) return false;
707 // No shifts are legal for AM3.
708 if (Memory.ShiftType != ARM_AM::no_shift) return false;
709 // Check for register offset.
710 if (Memory.OffsetRegNum) return true;
711 // Immediate offset in range [-255, 255].
712 if (!Memory.OffsetImm) return true;
713 int64_t Val = Memory.OffsetImm->getValue();
714 return Val > -256 && Val < 256;
716 bool isAM3Offset() const {
717 if (Kind != k_Immediate && Kind != k_PostIndexRegister)
719 if (Kind == k_PostIndexRegister)
720 return PostIdxReg.ShiftTy == ARM_AM::no_shift;
721 // Immediate offset in range [-255, 255].
722 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
723 if (!CE) return false;
724 int64_t Val = CE->getValue();
725 // Special case, #-0 is INT32_MIN.
726 return (Val > -256 && Val < 256) || Val == INT32_MIN;
728 bool isAddrMode5() const {
729 if (!isMemory() || Memory.Alignment != 0) return false;
730 // Check for register offset.
731 if (Memory.OffsetRegNum) return false;
732 // Immediate offset in range [-1020, 1020] and a multiple of 4.
733 if (!Memory.OffsetImm) return true;
734 int64_t Val = Memory.OffsetImm->getValue();
735 return (Val >= -1020 && Val <= 1020 && ((Val & 3) == 0)) ||
738 bool isMemTBB() const {
739 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
740 Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
744 bool isMemTBH() const {
745 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
746 Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm != 1 ||
747 Memory.Alignment != 0 )
751 bool isMemRegOffset() const {
752 if (!isMemory() || !Memory.OffsetRegNum || Memory.Alignment != 0)
756 bool isT2MemRegOffset() const {
757 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
758 Memory.Alignment != 0)
760 // Only lsl #{0, 1, 2, 3} allowed.
761 if (Memory.ShiftType == ARM_AM::no_shift)
763 if (Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm > 3)
767 bool isMemThumbRR() const {
768 // Thumb reg+reg addressing is simple. Just two registers, a base and
769 // an offset. No shifts, negations or any other complicating factors.
770 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
771 Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
773 return isARMLowRegister(Memory.BaseRegNum) &&
774 (!Memory.OffsetRegNum || isARMLowRegister(Memory.OffsetRegNum));
776 bool isMemThumbRIs4() const {
777 if (!isMemory() || Memory.OffsetRegNum != 0 ||
778 !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
780 // Immediate offset, multiple of 4 in range [0, 124].
781 if (!Memory.OffsetImm) return true;
782 int64_t Val = Memory.OffsetImm->getValue();
783 return Val >= 0 && Val <= 124 && (Val % 4) == 0;
785 bool isMemThumbRIs2() const {
786 if (!isMemory() || Memory.OffsetRegNum != 0 ||
787 !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
789 // Immediate offset, multiple of 4 in range [0, 62].
790 if (!Memory.OffsetImm) return true;
791 int64_t Val = Memory.OffsetImm->getValue();
792 return Val >= 0 && Val <= 62 && (Val % 2) == 0;
794 bool isMemThumbRIs1() const {
795 if (!isMemory() || Memory.OffsetRegNum != 0 ||
796 !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
798 // Immediate offset in range [0, 31].
799 if (!Memory.OffsetImm) return true;
800 int64_t Val = Memory.OffsetImm->getValue();
801 return Val >= 0 && Val <= 31;
803 bool isMemThumbSPI() const {
804 if (!isMemory() || Memory.OffsetRegNum != 0 ||
805 Memory.BaseRegNum != ARM::SP || Memory.Alignment != 0)
807 // Immediate offset, multiple of 4 in range [0, 1020].
808 if (!Memory.OffsetImm) return true;
809 int64_t Val = Memory.OffsetImm->getValue();
810 return Val >= 0 && Val <= 1020 && (Val % 4) == 0;
812 bool isMemImm8s4Offset() const {
813 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
815 // Immediate offset a multiple of 4 in range [-1020, 1020].
816 if (!Memory.OffsetImm) return true;
817 int64_t Val = Memory.OffsetImm->getValue();
818 return Val >= -1020 && Val <= 1020 && (Val & 3) == 0;
820 bool isMemImm0_1020s4Offset() const {
821 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
823 // Immediate offset a multiple of 4 in range [0, 1020].
824 if (!Memory.OffsetImm) return true;
825 int64_t Val = Memory.OffsetImm->getValue();
826 return Val >= 0 && Val <= 1020 && (Val & 3) == 0;
828 bool isMemImm8Offset() const {
829 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
831 // Immediate offset in range [-255, 255].
832 if (!Memory.OffsetImm) return true;
833 int64_t Val = Memory.OffsetImm->getValue();
834 return (Val == INT32_MIN) || (Val > -256 && Val < 256);
836 bool isMemPosImm8Offset() const {
837 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
839 // Immediate offset in range [0, 255].
840 if (!Memory.OffsetImm) return true;
841 int64_t Val = Memory.OffsetImm->getValue();
842 return Val >= 0 && Val < 256;
844 bool isMemNegImm8Offset() const {
845 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
847 // Immediate offset in range [-255, -1].
848 if (!Memory.OffsetImm) return true;
849 int64_t Val = Memory.OffsetImm->getValue();
850 return Val > -256 && Val < 0;
852 bool isMemUImm12Offset() const {
853 // If we have an immediate that's not a constant, treat it as a label
854 // reference needing a fixup. If it is a constant, it's something else
856 if (Kind == k_Immediate && !isa<MCConstantExpr>(getImm()))
859 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
861 // Immediate offset in range [0, 4095].
862 if (!Memory.OffsetImm) return true;
863 int64_t Val = Memory.OffsetImm->getValue();
864 return (Val >= 0 && Val < 4096);
866 bool isMemImm12Offset() const {
867 // If we have an immediate that's not a constant, treat it as a label
868 // reference needing a fixup. If it is a constant, it's something else
870 if (Kind == k_Immediate && !isa<MCConstantExpr>(getImm()))
873 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
875 // Immediate offset in range [-4095, 4095].
876 if (!Memory.OffsetImm) return true;
877 int64_t Val = Memory.OffsetImm->getValue();
878 return (Val > -4096 && Val < 4096) || (Val == INT32_MIN);
880 bool isPostIdxImm8() const {
881 if (Kind != k_Immediate)
883 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
884 if (!CE) return false;
885 int64_t Val = CE->getValue();
886 return (Val > -256 && Val < 256) || (Val == INT32_MIN);
888 bool isPostIdxImm8s4() const {
889 if (Kind != k_Immediate)
891 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
892 if (!CE) return false;
893 int64_t Val = CE->getValue();
894 return ((Val & 3) == 0 && Val >= -1020 && Val <= 1020) ||
898 bool isMSRMask() const { return Kind == k_MSRMask; }
899 bool isProcIFlags() const { return Kind == k_ProcIFlags; }
902 bool isVectorIndex8() const {
903 if (Kind != k_VectorIndex) return false;
904 return VectorIndex.Val < 8;
906 bool isVectorIndex16() const {
907 if (Kind != k_VectorIndex) return false;
908 return VectorIndex.Val < 4;
910 bool isVectorIndex32() const {
911 if (Kind != k_VectorIndex) return false;
912 return VectorIndex.Val < 2;
915 bool isNEONi8splat() const {
916 if (Kind != k_Immediate)
918 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
919 // Must be a constant.
920 if (!CE) return false;
921 int64_t Value = CE->getValue();
922 // i8 value splatted across 8 bytes. The immediate is just the 8 byte
924 return Value >= 0 && Value < 256;
927 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
928 // Add as immediates when possible. Null MCExpr = 0.
930 Inst.addOperand(MCOperand::CreateImm(0));
931 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
932 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
934 Inst.addOperand(MCOperand::CreateExpr(Expr));
937 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
938 assert(N == 2 && "Invalid number of operands!");
939 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
940 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
941 Inst.addOperand(MCOperand::CreateReg(RegNum));
944 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
945 assert(N == 1 && "Invalid number of operands!");
946 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
949 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
950 assert(N == 1 && "Invalid number of operands!");
951 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
954 void addCoprocOptionOperands(MCInst &Inst, unsigned N) const {
955 assert(N == 1 && "Invalid number of operands!");
956 Inst.addOperand(MCOperand::CreateImm(CoprocOption.Val));
959 void addITMaskOperands(MCInst &Inst, unsigned N) const {
960 assert(N == 1 && "Invalid number of operands!");
961 Inst.addOperand(MCOperand::CreateImm(ITMask.Mask));
964 void addITCondCodeOperands(MCInst &Inst, unsigned N) const {
965 assert(N == 1 && "Invalid number of operands!");
966 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
969 void addCCOutOperands(MCInst &Inst, unsigned N) const {
970 assert(N == 1 && "Invalid number of operands!");
971 Inst.addOperand(MCOperand::CreateReg(getReg()));
974 void addRegOperands(MCInst &Inst, unsigned N) const {
975 assert(N == 1 && "Invalid number of operands!");
976 Inst.addOperand(MCOperand::CreateReg(getReg()));
979 void addRegShiftedRegOperands(MCInst &Inst, unsigned N) const {
980 assert(N == 3 && "Invalid number of operands!");
981 assert(isRegShiftedReg() && "addRegShiftedRegOperands() on non RegShiftedReg!");
982 Inst.addOperand(MCOperand::CreateReg(RegShiftedReg.SrcReg));
983 Inst.addOperand(MCOperand::CreateReg(RegShiftedReg.ShiftReg));
984 Inst.addOperand(MCOperand::CreateImm(
985 ARM_AM::getSORegOpc(RegShiftedReg.ShiftTy, RegShiftedReg.ShiftImm)));
988 void addRegShiftedImmOperands(MCInst &Inst, unsigned N) const {
989 assert(N == 2 && "Invalid number of operands!");
990 assert(isRegShiftedImm() && "addRegShiftedImmOperands() on non RegShiftedImm!");
991 Inst.addOperand(MCOperand::CreateReg(RegShiftedImm.SrcReg));
992 Inst.addOperand(MCOperand::CreateImm(
993 ARM_AM::getSORegOpc(RegShiftedImm.ShiftTy, RegShiftedImm.ShiftImm)));
996 void addShifterImmOperands(MCInst &Inst, unsigned N) const {
997 assert(N == 1 && "Invalid number of operands!");
998 Inst.addOperand(MCOperand::CreateImm((ShifterImm.isASR << 5) |
1002 void addRegListOperands(MCInst &Inst, unsigned N) const {
1003 assert(N == 1 && "Invalid number of operands!");
1004 const SmallVectorImpl<unsigned> &RegList = getRegList();
1005 for (SmallVectorImpl<unsigned>::const_iterator
1006 I = RegList.begin(), E = RegList.end(); I != E; ++I)
1007 Inst.addOperand(MCOperand::CreateReg(*I));
1010 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
1011 addRegListOperands(Inst, N);
1014 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
1015 addRegListOperands(Inst, N);
1018 void addRotImmOperands(MCInst &Inst, unsigned N) const {
1019 assert(N == 1 && "Invalid number of operands!");
1020 // Encoded as val>>3. The printer handles display as 8, 16, 24.
1021 Inst.addOperand(MCOperand::CreateImm(RotImm.Imm >> 3));
1024 void addBitfieldOperands(MCInst &Inst, unsigned N) const {
1025 assert(N == 1 && "Invalid number of operands!");
1026 // Munge the lsb/width into a bitfield mask.
1027 unsigned lsb = Bitfield.LSB;
1028 unsigned width = Bitfield.Width;
1029 // Make a 32-bit mask w/ the referenced bits clear and all other bits set.
1030 uint32_t Mask = ~(((uint32_t)0xffffffff >> lsb) << (32 - width) >>
1031 (32 - (lsb + width)));
1032 Inst.addOperand(MCOperand::CreateImm(Mask));
1035 void addImmOperands(MCInst &Inst, unsigned N) const {
1036 assert(N == 1 && "Invalid number of operands!");
1037 addExpr(Inst, getImm());
1040 void addFPImmOperands(MCInst &Inst, unsigned N) const {
1041 assert(N == 1 && "Invalid number of operands!");
1042 Inst.addOperand(MCOperand::CreateImm(getFPImm()));
1045 void addImm8s4Operands(MCInst &Inst, unsigned N) const {
1046 assert(N == 1 && "Invalid number of operands!");
1047 // FIXME: We really want to scale the value here, but the LDRD/STRD
1048 // instruction don't encode operands that way yet.
1049 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1050 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
1053 void addImm0_1020s4Operands(MCInst &Inst, unsigned N) const {
1054 assert(N == 1 && "Invalid number of operands!");
1055 // The immediate is scaled by four in the encoding and is stored
1056 // in the MCInst as such. Lop off the low two bits here.
1057 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1058 Inst.addOperand(MCOperand::CreateImm(CE->getValue() / 4));
1061 void addImm0_508s4Operands(MCInst &Inst, unsigned N) const {
1062 assert(N == 1 && "Invalid number of operands!");
1063 // The immediate is scaled by four in the encoding and is stored
1064 // in the MCInst as such. Lop off the low two bits here.
1065 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1066 Inst.addOperand(MCOperand::CreateImm(CE->getValue() / 4));
1069 void addImm0_255Operands(MCInst &Inst, unsigned N) const {
1070 assert(N == 1 && "Invalid number of operands!");
1071 addExpr(Inst, getImm());
1074 void addImm0_7Operands(MCInst &Inst, unsigned N) const {
1075 assert(N == 1 && "Invalid number of operands!");
1076 addExpr(Inst, getImm());
1079 void addImm0_15Operands(MCInst &Inst, unsigned N) const {
1080 assert(N == 1 && "Invalid number of operands!");
1081 addExpr(Inst, getImm());
1084 void addImm0_31Operands(MCInst &Inst, unsigned N) const {
1085 assert(N == 1 && "Invalid number of operands!");
1086 addExpr(Inst, getImm());
1089 void addImm1_16Operands(MCInst &Inst, unsigned N) const {
1090 assert(N == 1 && "Invalid number of operands!");
1091 // The constant encodes as the immediate-1, and we store in the instruction
1092 // the bits as encoded, so subtract off one here.
1093 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1094 Inst.addOperand(MCOperand::CreateImm(CE->getValue() - 1));
1097 void addImm1_32Operands(MCInst &Inst, unsigned N) const {
1098 assert(N == 1 && "Invalid number of operands!");
1099 // The constant encodes as the immediate-1, and we store in the instruction
1100 // the bits as encoded, so subtract off one here.
1101 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1102 Inst.addOperand(MCOperand::CreateImm(CE->getValue() - 1));
1105 void addImm0_65535Operands(MCInst &Inst, unsigned N) const {
1106 assert(N == 1 && "Invalid number of operands!");
1107 addExpr(Inst, getImm());
1110 void addImm0_65535ExprOperands(MCInst &Inst, unsigned N) const {
1111 assert(N == 1 && "Invalid number of operands!");
1112 addExpr(Inst, getImm());
1115 void addImm24bitOperands(MCInst &Inst, unsigned N) const {
1116 assert(N == 1 && "Invalid number of operands!");
1117 addExpr(Inst, getImm());
1120 void addImmThumbSROperands(MCInst &Inst, unsigned N) const {
1121 assert(N == 1 && "Invalid number of operands!");
1122 // The constant encodes as the immediate, except for 32, which encodes as
1124 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1125 unsigned Imm = CE->getValue();
1126 Inst.addOperand(MCOperand::CreateImm((Imm == 32 ? 0 : Imm)));
1129 void addPKHLSLImmOperands(MCInst &Inst, unsigned N) const {
1130 assert(N == 1 && "Invalid number of operands!");
1131 addExpr(Inst, getImm());
1134 void addPKHASRImmOperands(MCInst &Inst, unsigned N) const {
1135 assert(N == 1 && "Invalid number of operands!");
1136 // An ASR value of 32 encodes as 0, so that's how we want to add it to
1137 // the instruction as well.
1138 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1139 int Val = CE->getValue();
1140 Inst.addOperand(MCOperand::CreateImm(Val == 32 ? 0 : Val));
1143 void addARMSOImmOperands(MCInst &Inst, unsigned N) const {
1144 assert(N == 1 && "Invalid number of operands!");
1145 addExpr(Inst, getImm());
1148 void addT2SOImmOperands(MCInst &Inst, unsigned N) const {
1149 assert(N == 1 && "Invalid number of operands!");
1150 addExpr(Inst, getImm());
1153 void addSetEndImmOperands(MCInst &Inst, unsigned N) const {
1154 assert(N == 1 && "Invalid number of operands!");
1155 addExpr(Inst, getImm());
1158 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
1159 assert(N == 1 && "Invalid number of operands!");
1160 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
1163 void addMemNoOffsetOperands(MCInst &Inst, unsigned N) const {
1164 assert(N == 1 && "Invalid number of operands!");
1165 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1168 void addAlignedMemoryOperands(MCInst &Inst, unsigned N) const {
1169 assert(N == 2 && "Invalid number of operands!");
1170 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1171 Inst.addOperand(MCOperand::CreateImm(Memory.Alignment));
1174 void addAddrMode2Operands(MCInst &Inst, unsigned N) const {
1175 assert(N == 3 && "Invalid number of operands!");
1176 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1177 if (!Memory.OffsetRegNum) {
1178 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1179 // Special case for #-0
1180 if (Val == INT32_MIN) Val = 0;
1181 if (Val < 0) Val = -Val;
1182 Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
1184 // For register offset, we encode the shift type and negation flag
1186 Val = ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
1187 Memory.ShiftImm, Memory.ShiftType);
1189 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1190 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1191 Inst.addOperand(MCOperand::CreateImm(Val));
1194 void addAM2OffsetImmOperands(MCInst &Inst, unsigned N) const {
1195 assert(N == 2 && "Invalid number of operands!");
1196 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1197 assert(CE && "non-constant AM2OffsetImm operand!");
1198 int32_t Val = CE->getValue();
1199 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1200 // Special case for #-0
1201 if (Val == INT32_MIN) Val = 0;
1202 if (Val < 0) Val = -Val;
1203 Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
1204 Inst.addOperand(MCOperand::CreateReg(0));
1205 Inst.addOperand(MCOperand::CreateImm(Val));
1208 void addAddrMode3Operands(MCInst &Inst, unsigned N) const {
1209 assert(N == 3 && "Invalid number of operands!");
1210 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1211 if (!Memory.OffsetRegNum) {
1212 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1213 // Special case for #-0
1214 if (Val == INT32_MIN) Val = 0;
1215 if (Val < 0) Val = -Val;
1216 Val = ARM_AM::getAM3Opc(AddSub, Val);
1218 // For register offset, we encode the shift type and negation flag
1220 Val = ARM_AM::getAM3Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add, 0);
1222 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1223 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1224 Inst.addOperand(MCOperand::CreateImm(Val));
1227 void addAM3OffsetOperands(MCInst &Inst, unsigned N) const {
1228 assert(N == 2 && "Invalid number of operands!");
1229 if (Kind == k_PostIndexRegister) {
1231 ARM_AM::getAM3Opc(PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub, 0);
1232 Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1233 Inst.addOperand(MCOperand::CreateImm(Val));
1238 const MCConstantExpr *CE = static_cast<const MCConstantExpr*>(getImm());
1239 int32_t Val = CE->getValue();
1240 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1241 // Special case for #-0
1242 if (Val == INT32_MIN) Val = 0;
1243 if (Val < 0) Val = -Val;
1244 Val = ARM_AM::getAM3Opc(AddSub, Val);
1245 Inst.addOperand(MCOperand::CreateReg(0));
1246 Inst.addOperand(MCOperand::CreateImm(Val));
1249 void addAddrMode5Operands(MCInst &Inst, unsigned N) const {
1250 assert(N == 2 && "Invalid number of operands!");
1251 // The lower two bits are always zero and as such are not encoded.
1252 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
1253 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1254 // Special case for #-0
1255 if (Val == INT32_MIN) Val = 0;
1256 if (Val < 0) Val = -Val;
1257 Val = ARM_AM::getAM5Opc(AddSub, Val);
1258 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1259 Inst.addOperand(MCOperand::CreateImm(Val));
1262 void addMemImm8s4OffsetOperands(MCInst &Inst, unsigned N) const {
1263 assert(N == 2 && "Invalid number of operands!");
1264 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1265 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1266 Inst.addOperand(MCOperand::CreateImm(Val));
1269 void addMemImm0_1020s4OffsetOperands(MCInst &Inst, unsigned N) const {
1270 assert(N == 2 && "Invalid number of operands!");
1271 // The lower two bits are always zero and as such are not encoded.
1272 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
1273 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1274 Inst.addOperand(MCOperand::CreateImm(Val));
1277 void addMemImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1278 assert(N == 2 && "Invalid number of operands!");
1279 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1280 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1281 Inst.addOperand(MCOperand::CreateImm(Val));
1284 void addMemPosImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1285 addMemImm8OffsetOperands(Inst, N);
1288 void addMemNegImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1289 addMemImm8OffsetOperands(Inst, N);
1292 void addMemUImm12OffsetOperands(MCInst &Inst, unsigned N) const {
1293 assert(N == 2 && "Invalid number of operands!");
1294 // If this is an immediate, it's a label reference.
1295 if (Kind == k_Immediate) {
1296 addExpr(Inst, getImm());
1297 Inst.addOperand(MCOperand::CreateImm(0));
1301 // Otherwise, it's a normal memory reg+offset.
1302 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1303 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1304 Inst.addOperand(MCOperand::CreateImm(Val));
1307 void addMemImm12OffsetOperands(MCInst &Inst, unsigned N) const {
1308 assert(N == 2 && "Invalid number of operands!");
1309 // If this is an immediate, it's a label reference.
1310 if (Kind == k_Immediate) {
1311 addExpr(Inst, getImm());
1312 Inst.addOperand(MCOperand::CreateImm(0));
1316 // Otherwise, it's a normal memory reg+offset.
1317 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1318 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1319 Inst.addOperand(MCOperand::CreateImm(Val));
1322 void addMemTBBOperands(MCInst &Inst, unsigned N) const {
1323 assert(N == 2 && "Invalid number of operands!");
1324 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1325 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1328 void addMemTBHOperands(MCInst &Inst, unsigned N) const {
1329 assert(N == 2 && "Invalid number of operands!");
1330 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1331 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1334 void addMemRegOffsetOperands(MCInst &Inst, unsigned N) const {
1335 assert(N == 3 && "Invalid number of operands!");
1336 unsigned Val = ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
1337 Memory.ShiftImm, Memory.ShiftType);
1338 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1339 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1340 Inst.addOperand(MCOperand::CreateImm(Val));
1343 void addT2MemRegOffsetOperands(MCInst &Inst, unsigned N) const {
1344 assert(N == 3 && "Invalid number of operands!");
1345 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1346 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1347 Inst.addOperand(MCOperand::CreateImm(Memory.ShiftImm));
1350 void addMemThumbRROperands(MCInst &Inst, unsigned N) const {
1351 assert(N == 2 && "Invalid number of operands!");
1352 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1353 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1356 void addMemThumbRIs4Operands(MCInst &Inst, unsigned N) const {
1357 assert(N == 2 && "Invalid number of operands!");
1358 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
1359 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1360 Inst.addOperand(MCOperand::CreateImm(Val));
1363 void addMemThumbRIs2Operands(MCInst &Inst, unsigned N) const {
1364 assert(N == 2 && "Invalid number of operands!");
1365 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 2) : 0;
1366 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1367 Inst.addOperand(MCOperand::CreateImm(Val));
1370 void addMemThumbRIs1Operands(MCInst &Inst, unsigned N) const {
1371 assert(N == 2 && "Invalid number of operands!");
1372 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue()) : 0;
1373 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1374 Inst.addOperand(MCOperand::CreateImm(Val));
1377 void addMemThumbSPIOperands(MCInst &Inst, unsigned N) const {
1378 assert(N == 2 && "Invalid number of operands!");
1379 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
1380 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1381 Inst.addOperand(MCOperand::CreateImm(Val));
1384 void addPostIdxImm8Operands(MCInst &Inst, unsigned N) const {
1385 assert(N == 1 && "Invalid number of operands!");
1386 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1387 assert(CE && "non-constant post-idx-imm8 operand!");
1388 int Imm = CE->getValue();
1389 bool isAdd = Imm >= 0;
1390 if (Imm == INT32_MIN) Imm = 0;
1391 Imm = (Imm < 0 ? -Imm : Imm) | (int)isAdd << 8;
1392 Inst.addOperand(MCOperand::CreateImm(Imm));
1395 void addPostIdxImm8s4Operands(MCInst &Inst, unsigned N) const {
1396 assert(N == 1 && "Invalid number of operands!");
1397 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1398 assert(CE && "non-constant post-idx-imm8s4 operand!");
1399 int Imm = CE->getValue();
1400 bool isAdd = Imm >= 0;
1401 if (Imm == INT32_MIN) Imm = 0;
1402 // Immediate is scaled by 4.
1403 Imm = ((Imm < 0 ? -Imm : Imm) / 4) | (int)isAdd << 8;
1404 Inst.addOperand(MCOperand::CreateImm(Imm));
1407 void addPostIdxRegOperands(MCInst &Inst, unsigned N) const {
1408 assert(N == 2 && "Invalid number of operands!");
1409 Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1410 Inst.addOperand(MCOperand::CreateImm(PostIdxReg.isAdd));
1413 void addPostIdxRegShiftedOperands(MCInst &Inst, unsigned N) const {
1414 assert(N == 2 && "Invalid number of operands!");
1415 Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1416 // The sign, shift type, and shift amount are encoded in a single operand
1417 // using the AM2 encoding helpers.
1418 ARM_AM::AddrOpc opc = PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub;
1419 unsigned Imm = ARM_AM::getAM2Opc(opc, PostIdxReg.ShiftImm,
1420 PostIdxReg.ShiftTy);
1421 Inst.addOperand(MCOperand::CreateImm(Imm));
1424 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
1425 assert(N == 1 && "Invalid number of operands!");
1426 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
1429 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
1430 assert(N == 1 && "Invalid number of operands!");
1431 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
1434 void addVectorIndex8Operands(MCInst &Inst, unsigned N) const {
1435 assert(N == 1 && "Invalid number of operands!");
1436 Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1439 void addVectorIndex16Operands(MCInst &Inst, unsigned N) const {
1440 assert(N == 1 && "Invalid number of operands!");
1441 Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1444 void addVectorIndex32Operands(MCInst &Inst, unsigned N) const {
1445 assert(N == 1 && "Invalid number of operands!");
1446 Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1449 void addNEONi8splatOperands(MCInst &Inst, unsigned N) const {
1450 assert(N == 1 && "Invalid number of operands!");
1451 // The immediate encodes the type of constant as well as the value.
1452 // Mask in that this is an i8 splat.
1453 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1454 Inst.addOperand(MCOperand::CreateImm(CE->getValue() | 0xe00));
1457 virtual void print(raw_ostream &OS) const;
1459 static ARMOperand *CreateITMask(unsigned Mask, SMLoc S) {
1460 ARMOperand *Op = new ARMOperand(k_ITCondMask);
1461 Op->ITMask.Mask = Mask;
1467 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
1468 ARMOperand *Op = new ARMOperand(k_CondCode);
1475 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
1476 ARMOperand *Op = new ARMOperand(k_CoprocNum);
1477 Op->Cop.Val = CopVal;
1483 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
1484 ARMOperand *Op = new ARMOperand(k_CoprocReg);
1485 Op->Cop.Val = CopVal;
1491 static ARMOperand *CreateCoprocOption(unsigned Val, SMLoc S, SMLoc E) {
1492 ARMOperand *Op = new ARMOperand(k_CoprocOption);
1499 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
1500 ARMOperand *Op = new ARMOperand(k_CCOut);
1501 Op->Reg.RegNum = RegNum;
1507 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
1508 ARMOperand *Op = new ARMOperand(k_Token);
1509 Op->Tok.Data = Str.data();
1510 Op->Tok.Length = Str.size();
1516 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
1517 ARMOperand *Op = new ARMOperand(k_Register);
1518 Op->Reg.RegNum = RegNum;
1524 static ARMOperand *CreateShiftedRegister(ARM_AM::ShiftOpc ShTy,
1529 ARMOperand *Op = new ARMOperand(k_ShiftedRegister);
1530 Op->RegShiftedReg.ShiftTy = ShTy;
1531 Op->RegShiftedReg.SrcReg = SrcReg;
1532 Op->RegShiftedReg.ShiftReg = ShiftReg;
1533 Op->RegShiftedReg.ShiftImm = ShiftImm;
1539 static ARMOperand *CreateShiftedImmediate(ARM_AM::ShiftOpc ShTy,
1543 ARMOperand *Op = new ARMOperand(k_ShiftedImmediate);
1544 Op->RegShiftedImm.ShiftTy = ShTy;
1545 Op->RegShiftedImm.SrcReg = SrcReg;
1546 Op->RegShiftedImm.ShiftImm = ShiftImm;
1552 static ARMOperand *CreateShifterImm(bool isASR, unsigned Imm,
1554 ARMOperand *Op = new ARMOperand(k_ShifterImmediate);
1555 Op->ShifterImm.isASR = isASR;
1556 Op->ShifterImm.Imm = Imm;
1562 static ARMOperand *CreateRotImm(unsigned Imm, SMLoc S, SMLoc E) {
1563 ARMOperand *Op = new ARMOperand(k_RotateImmediate);
1564 Op->RotImm.Imm = Imm;
1570 static ARMOperand *CreateBitfield(unsigned LSB, unsigned Width,
1572 ARMOperand *Op = new ARMOperand(k_BitfieldDescriptor);
1573 Op->Bitfield.LSB = LSB;
1574 Op->Bitfield.Width = Width;
1581 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
1582 SMLoc StartLoc, SMLoc EndLoc) {
1583 KindTy Kind = k_RegisterList;
1585 if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Regs.front().first))
1586 Kind = k_DPRRegisterList;
1587 else if (ARMMCRegisterClasses[ARM::SPRRegClassID].
1588 contains(Regs.front().first))
1589 Kind = k_SPRRegisterList;
1591 ARMOperand *Op = new ARMOperand(Kind);
1592 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
1593 I = Regs.begin(), E = Regs.end(); I != E; ++I)
1594 Op->Registers.push_back(I->first);
1595 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
1596 Op->StartLoc = StartLoc;
1597 Op->EndLoc = EndLoc;
1601 static ARMOperand *CreateVectorIndex(unsigned Idx, SMLoc S, SMLoc E,
1603 ARMOperand *Op = new ARMOperand(k_VectorIndex);
1604 Op->VectorIndex.Val = Idx;
1610 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
1611 ARMOperand *Op = new ARMOperand(k_Immediate);
1618 static ARMOperand *CreateFPImm(unsigned Val, SMLoc S, MCContext &Ctx) {
1619 ARMOperand *Op = new ARMOperand(k_FPImmediate);
1620 Op->FPImm.Val = Val;
1626 static ARMOperand *CreateMem(unsigned BaseRegNum,
1627 const MCConstantExpr *OffsetImm,
1628 unsigned OffsetRegNum,
1629 ARM_AM::ShiftOpc ShiftType,
1634 ARMOperand *Op = new ARMOperand(k_Memory);
1635 Op->Memory.BaseRegNum = BaseRegNum;
1636 Op->Memory.OffsetImm = OffsetImm;
1637 Op->Memory.OffsetRegNum = OffsetRegNum;
1638 Op->Memory.ShiftType = ShiftType;
1639 Op->Memory.ShiftImm = ShiftImm;
1640 Op->Memory.Alignment = Alignment;
1641 Op->Memory.isNegative = isNegative;
1647 static ARMOperand *CreatePostIdxReg(unsigned RegNum, bool isAdd,
1648 ARM_AM::ShiftOpc ShiftTy,
1651 ARMOperand *Op = new ARMOperand(k_PostIndexRegister);
1652 Op->PostIdxReg.RegNum = RegNum;
1653 Op->PostIdxReg.isAdd = isAdd;
1654 Op->PostIdxReg.ShiftTy = ShiftTy;
1655 Op->PostIdxReg.ShiftImm = ShiftImm;
1661 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
1662 ARMOperand *Op = new ARMOperand(k_MemBarrierOpt);
1663 Op->MBOpt.Val = Opt;
1669 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
1670 ARMOperand *Op = new ARMOperand(k_ProcIFlags);
1671 Op->IFlags.Val = IFlags;
1677 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
1678 ARMOperand *Op = new ARMOperand(k_MSRMask);
1679 Op->MMask.Val = MMask;
1686 } // end anonymous namespace.
1688 void ARMOperand::print(raw_ostream &OS) const {
1691 OS << "<fpimm " << getFPImm() << "(" << ARM_AM::getFPImmFloat(getFPImm())
1695 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
1698 OS << "<ccout " << getReg() << ">";
1700 case k_ITCondMask: {
1701 static char MaskStr[][6] = { "()", "(t)", "(e)", "(tt)", "(et)", "(te)",
1702 "(ee)", "(ttt)", "(ett)", "(tet)", "(eet)", "(tte)", "(ete)",
1704 assert((ITMask.Mask & 0xf) == ITMask.Mask);
1705 OS << "<it-mask " << MaskStr[ITMask.Mask] << ">";
1709 OS << "<coprocessor number: " << getCoproc() << ">";
1712 OS << "<coprocessor register: " << getCoproc() << ">";
1714 case k_CoprocOption:
1715 OS << "<coprocessor option: " << CoprocOption.Val << ">";
1718 OS << "<mask: " << getMSRMask() << ">";
1721 getImm()->print(OS);
1723 case k_MemBarrierOpt:
1724 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
1728 << " base:" << Memory.BaseRegNum;
1731 case k_PostIndexRegister:
1732 OS << "post-idx register " << (PostIdxReg.isAdd ? "" : "-")
1733 << PostIdxReg.RegNum;
1734 if (PostIdxReg.ShiftTy != ARM_AM::no_shift)
1735 OS << ARM_AM::getShiftOpcStr(PostIdxReg.ShiftTy) << " "
1736 << PostIdxReg.ShiftImm;
1739 case k_ProcIFlags: {
1740 OS << "<ARM_PROC::";
1741 unsigned IFlags = getProcIFlags();
1742 for (int i=2; i >= 0; --i)
1743 if (IFlags & (1 << i))
1744 OS << ARM_PROC::IFlagsToString(1 << i);
1749 OS << "<register " << getReg() << ">";
1751 case k_ShifterImmediate:
1752 OS << "<shift " << (ShifterImm.isASR ? "asr" : "lsl")
1753 << " #" << ShifterImm.Imm << ">";
1755 case k_ShiftedRegister:
1756 OS << "<so_reg_reg "
1757 << RegShiftedReg.SrcReg
1758 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(RegShiftedReg.ShiftImm))
1759 << ", " << RegShiftedReg.ShiftReg << ", "
1760 << ARM_AM::getSORegOffset(RegShiftedReg.ShiftImm)
1763 case k_ShiftedImmediate:
1764 OS << "<so_reg_imm "
1765 << RegShiftedImm.SrcReg
1766 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(RegShiftedImm.ShiftImm))
1767 << ", " << ARM_AM::getSORegOffset(RegShiftedImm.ShiftImm)
1770 case k_RotateImmediate:
1771 OS << "<ror " << " #" << (RotImm.Imm * 8) << ">";
1773 case k_BitfieldDescriptor:
1774 OS << "<bitfield " << "lsb: " << Bitfield.LSB
1775 << ", width: " << Bitfield.Width << ">";
1777 case k_RegisterList:
1778 case k_DPRRegisterList:
1779 case k_SPRRegisterList: {
1780 OS << "<register_list ";
1782 const SmallVectorImpl<unsigned> &RegList = getRegList();
1783 for (SmallVectorImpl<unsigned>::const_iterator
1784 I = RegList.begin(), E = RegList.end(); I != E; ) {
1786 if (++I < E) OS << ", ";
1793 OS << "'" << getToken() << "'";
1796 OS << "<vectorindex " << getVectorIndex() << ">";
1801 /// @name Auto-generated Match Functions
1804 static unsigned MatchRegisterName(StringRef Name);
1808 bool ARMAsmParser::ParseRegister(unsigned &RegNo,
1809 SMLoc &StartLoc, SMLoc &EndLoc) {
1810 RegNo = tryParseRegister();
1812 return (RegNo == (unsigned)-1);
1815 /// Try to parse a register name. The token must be an Identifier when called,
1816 /// and if it is a register name the token is eaten and the register number is
1817 /// returned. Otherwise return -1.
1819 int ARMAsmParser::tryParseRegister() {
1820 const AsmToken &Tok = Parser.getTok();
1821 if (Tok.isNot(AsmToken::Identifier)) return -1;
1823 // FIXME: Validate register for the current architecture; we have to do
1824 // validation later, so maybe there is no need for this here.
1825 std::string upperCase = Tok.getString().str();
1826 std::string lowerCase = LowercaseString(upperCase);
1827 unsigned RegNum = MatchRegisterName(lowerCase);
1829 RegNum = StringSwitch<unsigned>(lowerCase)
1830 .Case("r13", ARM::SP)
1831 .Case("r14", ARM::LR)
1832 .Case("r15", ARM::PC)
1833 .Case("ip", ARM::R12)
1836 if (!RegNum) return -1;
1838 Parser.Lex(); // Eat identifier token.
1841 // Also check for an index operand. This is only legal for vector registers,
1842 // but that'll get caught OK in operand matching, so we don't need to
1843 // explicitly filter everything else out here.
1844 if (Parser.getTok().is(AsmToken::LBrac)) {
1845 SMLoc SIdx = Parser.getTok().getLoc();
1846 Parser.Lex(); // Eat left bracket token.
1848 const MCExpr *ImmVal;
1849 if (getParser().ParseExpression(ImmVal))
1850 return MatchOperand_ParseFail;
1851 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(ImmVal);
1853 TokError("immediate value expected for vector index");
1854 return MatchOperand_ParseFail;
1857 SMLoc E = Parser.getTok().getLoc();
1858 if (Parser.getTok().isNot(AsmToken::RBrac)) {
1859 Error(E, "']' expected");
1860 return MatchOperand_ParseFail;
1863 Parser.Lex(); // Eat right bracket token.
1865 Operands.push_back(ARMOperand::CreateVectorIndex(MCE->getValue(),
1874 // Try to parse a shifter (e.g., "lsl <amt>"). On success, return 0.
1875 // If a recoverable error occurs, return 1. If an irrecoverable error
1876 // occurs, return -1. An irrecoverable error is one where tokens have been
1877 // consumed in the process of trying to parse the shifter (i.e., when it is
1878 // indeed a shifter operand, but malformed).
1879 int ARMAsmParser::tryParseShiftRegister(
1880 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1881 SMLoc S = Parser.getTok().getLoc();
1882 const AsmToken &Tok = Parser.getTok();
1883 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1885 std::string upperCase = Tok.getString().str();
1886 std::string lowerCase = LowercaseString(upperCase);
1887 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
1888 .Case("lsl", ARM_AM::lsl)
1889 .Case("lsr", ARM_AM::lsr)
1890 .Case("asr", ARM_AM::asr)
1891 .Case("ror", ARM_AM::ror)
1892 .Case("rrx", ARM_AM::rrx)
1893 .Default(ARM_AM::no_shift);
1895 if (ShiftTy == ARM_AM::no_shift)
1898 Parser.Lex(); // Eat the operator.
1900 // The source register for the shift has already been added to the
1901 // operand list, so we need to pop it off and combine it into the shifted
1902 // register operand instead.
1903 OwningPtr<ARMOperand> PrevOp((ARMOperand*)Operands.pop_back_val());
1904 if (!PrevOp->isReg())
1905 return Error(PrevOp->getStartLoc(), "shift must be of a register");
1906 int SrcReg = PrevOp->getReg();
1909 if (ShiftTy == ARM_AM::rrx) {
1910 // RRX Doesn't have an explicit shift amount. The encoder expects
1911 // the shift register to be the same as the source register. Seems odd,
1915 // Figure out if this is shifted by a constant or a register (for non-RRX).
1916 if (Parser.getTok().is(AsmToken::Hash)) {
1917 Parser.Lex(); // Eat hash.
1918 SMLoc ImmLoc = Parser.getTok().getLoc();
1919 const MCExpr *ShiftExpr = 0;
1920 if (getParser().ParseExpression(ShiftExpr)) {
1921 Error(ImmLoc, "invalid immediate shift value");
1924 // The expression must be evaluatable as an immediate.
1925 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
1927 Error(ImmLoc, "invalid immediate shift value");
1930 // Range check the immediate.
1931 // lsl, ror: 0 <= imm <= 31
1932 // lsr, asr: 0 <= imm <= 32
1933 Imm = CE->getValue();
1935 ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
1936 ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
1937 Error(ImmLoc, "immediate shift value out of range");
1940 } else if (Parser.getTok().is(AsmToken::Identifier)) {
1941 ShiftReg = tryParseRegister();
1942 SMLoc L = Parser.getTok().getLoc();
1943 if (ShiftReg == -1) {
1944 Error (L, "expected immediate or register in shift operand");
1948 Error (Parser.getTok().getLoc(),
1949 "expected immediate or register in shift operand");
1954 if (ShiftReg && ShiftTy != ARM_AM::rrx)
1955 Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
1957 S, Parser.getTok().getLoc()));
1959 Operands.push_back(ARMOperand::CreateShiftedImmediate(ShiftTy, SrcReg, Imm,
1960 S, Parser.getTok().getLoc()));
1966 /// Try to parse a register name. The token must be an Identifier when called.
1967 /// If it's a register, an AsmOperand is created. Another AsmOperand is created
1968 /// if there is a "writeback". 'true' if it's not a register.
1970 /// TODO this is likely to change to allow different register types and or to
1971 /// parse for a specific register type.
1973 tryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1974 SMLoc S = Parser.getTok().getLoc();
1975 int RegNo = tryParseRegister();
1979 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
1981 const AsmToken &ExclaimTok = Parser.getTok();
1982 if (ExclaimTok.is(AsmToken::Exclaim)) {
1983 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
1984 ExclaimTok.getLoc()));
1985 Parser.Lex(); // Eat exclaim token
1989 // Also check for an index operand. This is only legal for vector registers,
1990 // but that'll get caught OK in operand matching, so we don't need to
1991 // explicitly filter everything else out here.
1992 if (Parser.getTok().is(AsmToken::LBrac)) {
1993 SMLoc SIdx = Parser.getTok().getLoc();
1994 Parser.Lex(); // Eat left bracket token.
1996 const MCExpr *ImmVal;
1997 if (getParser().ParseExpression(ImmVal))
1998 return MatchOperand_ParseFail;
1999 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(ImmVal);
2001 TokError("immediate value expected for vector index");
2002 return MatchOperand_ParseFail;
2005 SMLoc E = Parser.getTok().getLoc();
2006 if (Parser.getTok().isNot(AsmToken::RBrac)) {
2007 Error(E, "']' expected");
2008 return MatchOperand_ParseFail;
2011 Parser.Lex(); // Eat right bracket token.
2013 Operands.push_back(ARMOperand::CreateVectorIndex(MCE->getValue(),
2021 /// MatchCoprocessorOperandName - Try to parse an coprocessor related
2022 /// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
2024 static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
2025 // Use the same layout as the tablegen'erated register name matcher. Ugly,
2027 switch (Name.size()) {
2030 if (Name[0] != CoprocOp)
2047 if (Name[0] != CoprocOp || Name[1] != '1')
2051 case '0': return 10;
2052 case '1': return 11;
2053 case '2': return 12;
2054 case '3': return 13;
2055 case '4': return 14;
2056 case '5': return 15;
2064 /// parseITCondCode - Try to parse a condition code for an IT instruction.
2065 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2066 parseITCondCode(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2067 SMLoc S = Parser.getTok().getLoc();
2068 const AsmToken &Tok = Parser.getTok();
2069 if (!Tok.is(AsmToken::Identifier))
2070 return MatchOperand_NoMatch;
2071 unsigned CC = StringSwitch<unsigned>(Tok.getString())
2072 .Case("eq", ARMCC::EQ)
2073 .Case("ne", ARMCC::NE)
2074 .Case("hs", ARMCC::HS)
2075 .Case("cs", ARMCC::HS)
2076 .Case("lo", ARMCC::LO)
2077 .Case("cc", ARMCC::LO)
2078 .Case("mi", ARMCC::MI)
2079 .Case("pl", ARMCC::PL)
2080 .Case("vs", ARMCC::VS)
2081 .Case("vc", ARMCC::VC)
2082 .Case("hi", ARMCC::HI)
2083 .Case("ls", ARMCC::LS)
2084 .Case("ge", ARMCC::GE)
2085 .Case("lt", ARMCC::LT)
2086 .Case("gt", ARMCC::GT)
2087 .Case("le", ARMCC::LE)
2088 .Case("al", ARMCC::AL)
2091 return MatchOperand_NoMatch;
2092 Parser.Lex(); // Eat the token.
2094 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), S));
2096 return MatchOperand_Success;
2099 /// parseCoprocNumOperand - Try to parse an coprocessor number operand. The
2100 /// token must be an Identifier when called, and if it is a coprocessor
2101 /// number, the token is eaten and the operand is added to the operand list.
2102 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2103 parseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2104 SMLoc S = Parser.getTok().getLoc();
2105 const AsmToken &Tok = Parser.getTok();
2106 if (Tok.isNot(AsmToken::Identifier))
2107 return MatchOperand_NoMatch;
2109 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
2111 return MatchOperand_NoMatch;
2113 Parser.Lex(); // Eat identifier token.
2114 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
2115 return MatchOperand_Success;
2118 /// parseCoprocRegOperand - Try to parse an coprocessor register operand. The
2119 /// token must be an Identifier when called, and if it is a coprocessor
2120 /// number, the token is eaten and the operand is added to the operand list.
2121 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2122 parseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2123 SMLoc S = Parser.getTok().getLoc();
2124 const AsmToken &Tok = Parser.getTok();
2125 if (Tok.isNot(AsmToken::Identifier))
2126 return MatchOperand_NoMatch;
2128 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
2130 return MatchOperand_NoMatch;
2132 Parser.Lex(); // Eat identifier token.
2133 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
2134 return MatchOperand_Success;
2137 /// parseCoprocOptionOperand - Try to parse an coprocessor option operand.
2138 /// coproc_option : '{' imm0_255 '}'
2139 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2140 parseCoprocOptionOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2141 SMLoc S = Parser.getTok().getLoc();
2143 // If this isn't a '{', this isn't a coprocessor immediate operand.
2144 if (Parser.getTok().isNot(AsmToken::LCurly))
2145 return MatchOperand_NoMatch;
2146 Parser.Lex(); // Eat the '{'
2149 SMLoc Loc = Parser.getTok().getLoc();
2150 if (getParser().ParseExpression(Expr)) {
2151 Error(Loc, "illegal expression");
2152 return MatchOperand_ParseFail;
2154 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
2155 if (!CE || CE->getValue() < 0 || CE->getValue() > 255) {
2156 Error(Loc, "coprocessor option must be an immediate in range [0, 255]");
2157 return MatchOperand_ParseFail;
2159 int Val = CE->getValue();
2161 // Check for and consume the closing '}'
2162 if (Parser.getTok().isNot(AsmToken::RCurly))
2163 return MatchOperand_ParseFail;
2164 SMLoc E = Parser.getTok().getLoc();
2165 Parser.Lex(); // Eat the '}'
2167 Operands.push_back(ARMOperand::CreateCoprocOption(Val, S, E));
2168 return MatchOperand_Success;
2171 // For register list parsing, we need to map from raw GPR register numbering
2172 // to the enumeration values. The enumeration values aren't sorted by
2173 // register number due to our using "sp", "lr" and "pc" as canonical names.
2174 static unsigned getNextRegister(unsigned Reg) {
2175 // If this is a GPR, we need to do it manually, otherwise we can rely
2176 // on the sort ordering of the enumeration since the other reg-classes
2178 if (!ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
2181 default: assert(0 && "Invalid GPR number!");
2182 case ARM::R0: return ARM::R1; case ARM::R1: return ARM::R2;
2183 case ARM::R2: return ARM::R3; case ARM::R3: return ARM::R4;
2184 case ARM::R4: return ARM::R5; case ARM::R5: return ARM::R6;
2185 case ARM::R6: return ARM::R7; case ARM::R7: return ARM::R8;
2186 case ARM::R8: return ARM::R9; case ARM::R9: return ARM::R10;
2187 case ARM::R10: return ARM::R11; case ARM::R11: return ARM::R12;
2188 case ARM::R12: return ARM::SP; case ARM::SP: return ARM::LR;
2189 case ARM::LR: return ARM::PC; case ARM::PC: return ARM::R0;
2193 /// Parse a register list.
2195 parseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2196 assert(Parser.getTok().is(AsmToken::LCurly) &&
2197 "Token is not a Left Curly Brace");
2198 SMLoc S = Parser.getTok().getLoc();
2199 Parser.Lex(); // Eat '{' token.
2200 SMLoc RegLoc = Parser.getTok().getLoc();
2202 // Check the first register in the list to see what register class
2203 // this is a list of.
2204 int Reg = tryParseRegister();
2206 return Error(RegLoc, "register expected");
2208 MCRegisterClass *RC;
2209 if (ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
2210 RC = &ARMMCRegisterClasses[ARM::GPRRegClassID];
2211 else if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg))
2212 RC = &ARMMCRegisterClasses[ARM::DPRRegClassID];
2213 else if (ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg))
2214 RC = &ARMMCRegisterClasses[ARM::SPRRegClassID];
2216 return Error(RegLoc, "invalid register in register list");
2218 // The reglist instructions have at most 16 registers, so reserve
2219 // space for that many.
2220 SmallVector<std::pair<unsigned, SMLoc>, 16> Registers;
2221 // Store the first register.
2222 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2224 // This starts immediately after the first register token in the list,
2225 // so we can see either a comma or a minus (range separator) as a legal
2227 while (Parser.getTok().is(AsmToken::Comma) ||
2228 Parser.getTok().is(AsmToken::Minus)) {
2229 if (Parser.getTok().is(AsmToken::Minus)) {
2230 Parser.Lex(); // Eat the comma.
2231 SMLoc EndLoc = Parser.getTok().getLoc();
2232 int EndReg = tryParseRegister();
2234 return Error(EndLoc, "register expected");
2235 // If the register is the same as the start reg, there's nothing
2239 // The register must be in the same register class as the first.
2240 if (!RC->contains(EndReg))
2241 return Error(EndLoc, "invalid register in register list");
2242 // Ranges must go from low to high.
2243 if (getARMRegisterNumbering(Reg) > getARMRegisterNumbering(EndReg))
2244 return Error(EndLoc, "bad range in register list");
2246 // Add all the registers in the range to the register list.
2247 while (Reg != EndReg) {
2248 Reg = getNextRegister(Reg);
2249 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2253 Parser.Lex(); // Eat the comma.
2254 RegLoc = Parser.getTok().getLoc();
2256 Reg = tryParseRegister();
2258 return Error(RegLoc, "register expected");
2259 // The register must be in the same register class as the first.
2260 if (!RC->contains(Reg))
2261 return Error(RegLoc, "invalid register in register list");
2262 // List must be monotonically increasing.
2263 if (getARMRegisterNumbering(Reg) <= getARMRegisterNumbering(OldReg))
2264 return Error(RegLoc, "register list not in ascending order");
2265 // VFP register lists must also be contiguous.
2266 // It's OK to use the enumeration values directly here rather, as the
2267 // VFP register classes have the enum sorted properly.
2268 if (RC != &ARMMCRegisterClasses[ARM::GPRRegClassID] &&
2270 return Error(RegLoc, "non-contiguous register range");
2271 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2274 SMLoc E = Parser.getTok().getLoc();
2275 if (Parser.getTok().isNot(AsmToken::RCurly))
2276 return Error(E, "'}' expected");
2277 Parser.Lex(); // Eat '}' token.
2279 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
2283 /// parseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
2284 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2285 parseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2286 SMLoc S = Parser.getTok().getLoc();
2287 const AsmToken &Tok = Parser.getTok();
2288 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2289 StringRef OptStr = Tok.getString();
2291 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
2292 .Case("sy", ARM_MB::SY)
2293 .Case("st", ARM_MB::ST)
2294 .Case("sh", ARM_MB::ISH)
2295 .Case("ish", ARM_MB::ISH)
2296 .Case("shst", ARM_MB::ISHST)
2297 .Case("ishst", ARM_MB::ISHST)
2298 .Case("nsh", ARM_MB::NSH)
2299 .Case("un", ARM_MB::NSH)
2300 .Case("nshst", ARM_MB::NSHST)
2301 .Case("unst", ARM_MB::NSHST)
2302 .Case("osh", ARM_MB::OSH)
2303 .Case("oshst", ARM_MB::OSHST)
2307 return MatchOperand_NoMatch;
2309 Parser.Lex(); // Eat identifier token.
2310 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
2311 return MatchOperand_Success;
2314 /// parseProcIFlagsOperand - Try to parse iflags from CPS instruction.
2315 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2316 parseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2317 SMLoc S = Parser.getTok().getLoc();
2318 const AsmToken &Tok = Parser.getTok();
2319 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2320 StringRef IFlagsStr = Tok.getString();
2322 // An iflags string of "none" is interpreted to mean that none of the AIF
2323 // bits are set. Not a terribly useful instruction, but a valid encoding.
2324 unsigned IFlags = 0;
2325 if (IFlagsStr != "none") {
2326 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
2327 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
2328 .Case("a", ARM_PROC::A)
2329 .Case("i", ARM_PROC::I)
2330 .Case("f", ARM_PROC::F)
2333 // If some specific iflag is already set, it means that some letter is
2334 // present more than once, this is not acceptable.
2335 if (Flag == ~0U || (IFlags & Flag))
2336 return MatchOperand_NoMatch;
2342 Parser.Lex(); // Eat identifier token.
2343 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
2344 return MatchOperand_Success;
2347 /// parseMSRMaskOperand - Try to parse mask flags from MSR instruction.
2348 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2349 parseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2350 SMLoc S = Parser.getTok().getLoc();
2351 const AsmToken &Tok = Parser.getTok();
2352 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2353 StringRef Mask = Tok.getString();
2356 // See ARMv6-M 10.1.1
2357 unsigned FlagsVal = StringSwitch<unsigned>(Mask)
2367 .Case("primask", 16)
2368 .Case("basepri", 17)
2369 .Case("basepri_max", 18)
2370 .Case("faultmask", 19)
2371 .Case("control", 20)
2374 if (FlagsVal == ~0U)
2375 return MatchOperand_NoMatch;
2377 if (!hasV7Ops() && FlagsVal >= 17 && FlagsVal <= 19)
2378 // basepri, basepri_max and faultmask only valid for V7m.
2379 return MatchOperand_NoMatch;
2381 Parser.Lex(); // Eat identifier token.
2382 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
2383 return MatchOperand_Success;
2386 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
2387 size_t Start = 0, Next = Mask.find('_');
2388 StringRef Flags = "";
2389 std::string SpecReg = LowercaseString(Mask.slice(Start, Next));
2390 if (Next != StringRef::npos)
2391 Flags = Mask.slice(Next+1, Mask.size());
2393 // FlagsVal contains the complete mask:
2395 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
2396 unsigned FlagsVal = 0;
2398 if (SpecReg == "apsr") {
2399 FlagsVal = StringSwitch<unsigned>(Flags)
2400 .Case("nzcvq", 0x8) // same as CPSR_f
2401 .Case("g", 0x4) // same as CPSR_s
2402 .Case("nzcvqg", 0xc) // same as CPSR_fs
2405 if (FlagsVal == ~0U) {
2407 return MatchOperand_NoMatch;
2409 FlagsVal = 8; // No flag
2411 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
2412 if (Flags == "all") // cpsr_all is an alias for cpsr_fc
2414 for (int i = 0, e = Flags.size(); i != e; ++i) {
2415 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
2422 // If some specific flag is already set, it means that some letter is
2423 // present more than once, this is not acceptable.
2424 if (FlagsVal == ~0U || (FlagsVal & Flag))
2425 return MatchOperand_NoMatch;
2428 } else // No match for special register.
2429 return MatchOperand_NoMatch;
2431 // Special register without flags are equivalent to "fc" flags.
2435 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
2436 if (SpecReg == "spsr")
2439 Parser.Lex(); // Eat identifier token.
2440 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
2441 return MatchOperand_Success;
2444 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2445 parsePKHImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands, StringRef Op,
2446 int Low, int High) {
2447 const AsmToken &Tok = Parser.getTok();
2448 if (Tok.isNot(AsmToken::Identifier)) {
2449 Error(Parser.getTok().getLoc(), Op + " operand expected.");
2450 return MatchOperand_ParseFail;
2452 StringRef ShiftName = Tok.getString();
2453 std::string LowerOp = LowercaseString(Op);
2454 std::string UpperOp = UppercaseString(Op);
2455 if (ShiftName != LowerOp && ShiftName != UpperOp) {
2456 Error(Parser.getTok().getLoc(), Op + " operand expected.");
2457 return MatchOperand_ParseFail;
2459 Parser.Lex(); // Eat shift type token.
2461 // There must be a '#' and a shift amount.
2462 if (Parser.getTok().isNot(AsmToken::Hash)) {
2463 Error(Parser.getTok().getLoc(), "'#' expected");
2464 return MatchOperand_ParseFail;
2466 Parser.Lex(); // Eat hash token.
2468 const MCExpr *ShiftAmount;
2469 SMLoc Loc = Parser.getTok().getLoc();
2470 if (getParser().ParseExpression(ShiftAmount)) {
2471 Error(Loc, "illegal expression");
2472 return MatchOperand_ParseFail;
2474 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
2476 Error(Loc, "constant expression expected");
2477 return MatchOperand_ParseFail;
2479 int Val = CE->getValue();
2480 if (Val < Low || Val > High) {
2481 Error(Loc, "immediate value out of range");
2482 return MatchOperand_ParseFail;
2485 Operands.push_back(ARMOperand::CreateImm(CE, Loc, Parser.getTok().getLoc()));
2487 return MatchOperand_Success;
2490 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2491 parseSetEndImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2492 const AsmToken &Tok = Parser.getTok();
2493 SMLoc S = Tok.getLoc();
2494 if (Tok.isNot(AsmToken::Identifier)) {
2495 Error(Tok.getLoc(), "'be' or 'le' operand expected");
2496 return MatchOperand_ParseFail;
2498 int Val = StringSwitch<int>(Tok.getString())
2502 Parser.Lex(); // Eat the token.
2505 Error(Tok.getLoc(), "'be' or 'le' operand expected");
2506 return MatchOperand_ParseFail;
2508 Operands.push_back(ARMOperand::CreateImm(MCConstantExpr::Create(Val,
2510 S, Parser.getTok().getLoc()));
2511 return MatchOperand_Success;
2514 /// parseShifterImm - Parse the shifter immediate operand for SSAT/USAT
2515 /// instructions. Legal values are:
2516 /// lsl #n 'n' in [0,31]
2517 /// asr #n 'n' in [1,32]
2518 /// n == 32 encoded as n == 0.
2519 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2520 parseShifterImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2521 const AsmToken &Tok = Parser.getTok();
2522 SMLoc S = Tok.getLoc();
2523 if (Tok.isNot(AsmToken::Identifier)) {
2524 Error(S, "shift operator 'asr' or 'lsl' expected");
2525 return MatchOperand_ParseFail;
2527 StringRef ShiftName = Tok.getString();
2529 if (ShiftName == "lsl" || ShiftName == "LSL")
2531 else if (ShiftName == "asr" || ShiftName == "ASR")
2534 Error(S, "shift operator 'asr' or 'lsl' expected");
2535 return MatchOperand_ParseFail;
2537 Parser.Lex(); // Eat the operator.
2539 // A '#' and a shift amount.
2540 if (Parser.getTok().isNot(AsmToken::Hash)) {
2541 Error(Parser.getTok().getLoc(), "'#' expected");
2542 return MatchOperand_ParseFail;
2544 Parser.Lex(); // Eat hash token.
2546 const MCExpr *ShiftAmount;
2547 SMLoc E = Parser.getTok().getLoc();
2548 if (getParser().ParseExpression(ShiftAmount)) {
2549 Error(E, "malformed shift expression");
2550 return MatchOperand_ParseFail;
2552 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
2554 Error(E, "shift amount must be an immediate");
2555 return MatchOperand_ParseFail;
2558 int64_t Val = CE->getValue();
2560 // Shift amount must be in [1,32]
2561 if (Val < 1 || Val > 32) {
2562 Error(E, "'asr' shift amount must be in range [1,32]");
2563 return MatchOperand_ParseFail;
2565 // asr #32 encoded as asr #0, but is not allowed in Thumb2 mode.
2566 if (isThumb() && Val == 32) {
2567 Error(E, "'asr #32' shift amount not allowed in Thumb mode");
2568 return MatchOperand_ParseFail;
2570 if (Val == 32) Val = 0;
2572 // Shift amount must be in [1,32]
2573 if (Val < 0 || Val > 31) {
2574 Error(E, "'lsr' shift amount must be in range [0,31]");
2575 return MatchOperand_ParseFail;
2579 E = Parser.getTok().getLoc();
2580 Operands.push_back(ARMOperand::CreateShifterImm(isASR, Val, S, E));
2582 return MatchOperand_Success;
2585 /// parseRotImm - Parse the shifter immediate operand for SXTB/UXTB family
2586 /// of instructions. Legal values are:
2587 /// ror #n 'n' in {0, 8, 16, 24}
2588 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2589 parseRotImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2590 const AsmToken &Tok = Parser.getTok();
2591 SMLoc S = Tok.getLoc();
2592 if (Tok.isNot(AsmToken::Identifier))
2593 return MatchOperand_NoMatch;
2594 StringRef ShiftName = Tok.getString();
2595 if (ShiftName != "ror" && ShiftName != "ROR")
2596 return MatchOperand_NoMatch;
2597 Parser.Lex(); // Eat the operator.
2599 // A '#' and a rotate amount.
2600 if (Parser.getTok().isNot(AsmToken::Hash)) {
2601 Error(Parser.getTok().getLoc(), "'#' expected");
2602 return MatchOperand_ParseFail;
2604 Parser.Lex(); // Eat hash token.
2606 const MCExpr *ShiftAmount;
2607 SMLoc E = Parser.getTok().getLoc();
2608 if (getParser().ParseExpression(ShiftAmount)) {
2609 Error(E, "malformed rotate expression");
2610 return MatchOperand_ParseFail;
2612 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
2614 Error(E, "rotate amount must be an immediate");
2615 return MatchOperand_ParseFail;
2618 int64_t Val = CE->getValue();
2619 // Shift amount must be in {0, 8, 16, 24} (0 is undocumented extension)
2620 // normally, zero is represented in asm by omitting the rotate operand
2622 if (Val != 8 && Val != 16 && Val != 24 && Val != 0) {
2623 Error(E, "'ror' rotate amount must be 8, 16, or 24");
2624 return MatchOperand_ParseFail;
2627 E = Parser.getTok().getLoc();
2628 Operands.push_back(ARMOperand::CreateRotImm(Val, S, E));
2630 return MatchOperand_Success;
2633 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2634 parseBitfield(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2635 SMLoc S = Parser.getTok().getLoc();
2636 // The bitfield descriptor is really two operands, the LSB and the width.
2637 if (Parser.getTok().isNot(AsmToken::Hash)) {
2638 Error(Parser.getTok().getLoc(), "'#' expected");
2639 return MatchOperand_ParseFail;
2641 Parser.Lex(); // Eat hash token.
2643 const MCExpr *LSBExpr;
2644 SMLoc E = Parser.getTok().getLoc();
2645 if (getParser().ParseExpression(LSBExpr)) {
2646 Error(E, "malformed immediate expression");
2647 return MatchOperand_ParseFail;
2649 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(LSBExpr);
2651 Error(E, "'lsb' operand must be an immediate");
2652 return MatchOperand_ParseFail;
2655 int64_t LSB = CE->getValue();
2656 // The LSB must be in the range [0,31]
2657 if (LSB < 0 || LSB > 31) {
2658 Error(E, "'lsb' operand must be in the range [0,31]");
2659 return MatchOperand_ParseFail;
2661 E = Parser.getTok().getLoc();
2663 // Expect another immediate operand.
2664 if (Parser.getTok().isNot(AsmToken::Comma)) {
2665 Error(Parser.getTok().getLoc(), "too few operands");
2666 return MatchOperand_ParseFail;
2668 Parser.Lex(); // Eat hash token.
2669 if (Parser.getTok().isNot(AsmToken::Hash)) {
2670 Error(Parser.getTok().getLoc(), "'#' expected");
2671 return MatchOperand_ParseFail;
2673 Parser.Lex(); // Eat hash token.
2675 const MCExpr *WidthExpr;
2676 if (getParser().ParseExpression(WidthExpr)) {
2677 Error(E, "malformed immediate expression");
2678 return MatchOperand_ParseFail;
2680 CE = dyn_cast<MCConstantExpr>(WidthExpr);
2682 Error(E, "'width' operand must be an immediate");
2683 return MatchOperand_ParseFail;
2686 int64_t Width = CE->getValue();
2687 // The LSB must be in the range [1,32-lsb]
2688 if (Width < 1 || Width > 32 - LSB) {
2689 Error(E, "'width' operand must be in the range [1,32-lsb]");
2690 return MatchOperand_ParseFail;
2692 E = Parser.getTok().getLoc();
2694 Operands.push_back(ARMOperand::CreateBitfield(LSB, Width, S, E));
2696 return MatchOperand_Success;
2699 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2700 parsePostIdxReg(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2701 // Check for a post-index addressing register operand. Specifically:
2702 // postidx_reg := '+' register {, shift}
2703 // | '-' register {, shift}
2704 // | register {, shift}
2706 // This method must return MatchOperand_NoMatch without consuming any tokens
2707 // in the case where there is no match, as other alternatives take other
2709 AsmToken Tok = Parser.getTok();
2710 SMLoc S = Tok.getLoc();
2711 bool haveEaten = false;
2714 if (Tok.is(AsmToken::Plus)) {
2715 Parser.Lex(); // Eat the '+' token.
2717 } else if (Tok.is(AsmToken::Minus)) {
2718 Parser.Lex(); // Eat the '-' token.
2722 if (Parser.getTok().is(AsmToken::Identifier))
2723 Reg = tryParseRegister();
2726 return MatchOperand_NoMatch;
2727 Error(Parser.getTok().getLoc(), "register expected");
2728 return MatchOperand_ParseFail;
2730 SMLoc E = Parser.getTok().getLoc();
2732 ARM_AM::ShiftOpc ShiftTy = ARM_AM::no_shift;
2733 unsigned ShiftImm = 0;
2734 if (Parser.getTok().is(AsmToken::Comma)) {
2735 Parser.Lex(); // Eat the ','.
2736 if (parseMemRegOffsetShift(ShiftTy, ShiftImm))
2737 return MatchOperand_ParseFail;
2740 Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ShiftTy,
2743 return MatchOperand_Success;
2746 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2747 parseAM3Offset(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2748 // Check for a post-index addressing register operand. Specifically:
2749 // am3offset := '+' register
2756 // This method must return MatchOperand_NoMatch without consuming any tokens
2757 // in the case where there is no match, as other alternatives take other
2759 AsmToken Tok = Parser.getTok();
2760 SMLoc S = Tok.getLoc();
2762 // Do immediates first, as we always parse those if we have a '#'.
2763 if (Parser.getTok().is(AsmToken::Hash)) {
2764 Parser.Lex(); // Eat the '#'.
2765 // Explicitly look for a '-', as we need to encode negative zero
2767 bool isNegative = Parser.getTok().is(AsmToken::Minus);
2768 const MCExpr *Offset;
2769 if (getParser().ParseExpression(Offset))
2770 return MatchOperand_ParseFail;
2771 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
2773 Error(S, "constant expression expected");
2774 return MatchOperand_ParseFail;
2776 SMLoc E = Tok.getLoc();
2777 // Negative zero is encoded as the flag value INT32_MIN.
2778 int32_t Val = CE->getValue();
2779 if (isNegative && Val == 0)
2783 ARMOperand::CreateImm(MCConstantExpr::Create(Val, getContext()), S, E));
2785 return MatchOperand_Success;
2789 bool haveEaten = false;
2792 if (Tok.is(AsmToken::Plus)) {
2793 Parser.Lex(); // Eat the '+' token.
2795 } else if (Tok.is(AsmToken::Minus)) {
2796 Parser.Lex(); // Eat the '-' token.
2800 if (Parser.getTok().is(AsmToken::Identifier))
2801 Reg = tryParseRegister();
2804 return MatchOperand_NoMatch;
2805 Error(Parser.getTok().getLoc(), "register expected");
2806 return MatchOperand_ParseFail;
2808 SMLoc E = Parser.getTok().getLoc();
2810 Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ARM_AM::no_shift,
2813 return MatchOperand_Success;
2816 /// cvtT2LdrdPre - Convert parsed operands to MCInst.
2817 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2818 /// when they refer multiple MIOperands inside a single one.
2820 cvtT2LdrdPre(MCInst &Inst, unsigned Opcode,
2821 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2823 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2824 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
2825 // Create a writeback register dummy placeholder.
2826 Inst.addOperand(MCOperand::CreateReg(0));
2828 ((ARMOperand*)Operands[4])->addMemImm8s4OffsetOperands(Inst, 2);
2830 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2834 /// cvtT2StrdPre - Convert parsed operands to MCInst.
2835 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2836 /// when they refer multiple MIOperands inside a single one.
2838 cvtT2StrdPre(MCInst &Inst, unsigned Opcode,
2839 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2840 // Create a writeback register dummy placeholder.
2841 Inst.addOperand(MCOperand::CreateReg(0));
2843 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2844 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
2846 ((ARMOperand*)Operands[4])->addMemImm8s4OffsetOperands(Inst, 2);
2848 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2852 /// cvtLdWriteBackRegT2AddrModeImm8 - Convert parsed operands to MCInst.
2853 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2854 /// when they refer multiple MIOperands inside a single one.
2856 cvtLdWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
2857 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2858 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2860 // Create a writeback register dummy placeholder.
2861 Inst.addOperand(MCOperand::CreateImm(0));
2863 ((ARMOperand*)Operands[3])->addMemImm8OffsetOperands(Inst, 2);
2864 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2868 /// cvtStWriteBackRegT2AddrModeImm8 - Convert parsed operands to MCInst.
2869 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2870 /// when they refer multiple MIOperands inside a single one.
2872 cvtStWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
2873 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2874 // Create a writeback register dummy placeholder.
2875 Inst.addOperand(MCOperand::CreateImm(0));
2876 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2877 ((ARMOperand*)Operands[3])->addMemImm8OffsetOperands(Inst, 2);
2878 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2882 /// cvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
2883 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2884 /// when they refer multiple MIOperands inside a single one.
2886 cvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
2887 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2888 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2890 // Create a writeback register dummy placeholder.
2891 Inst.addOperand(MCOperand::CreateImm(0));
2893 ((ARMOperand*)Operands[3])->addAddrMode2Operands(Inst, 3);
2894 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2898 /// cvtLdWriteBackRegAddrModeImm12 - Convert parsed operands to MCInst.
2899 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2900 /// when they refer multiple MIOperands inside a single one.
2902 cvtLdWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
2903 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2904 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2906 // Create a writeback register dummy placeholder.
2907 Inst.addOperand(MCOperand::CreateImm(0));
2909 ((ARMOperand*)Operands[3])->addMemImm12OffsetOperands(Inst, 2);
2910 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2915 /// cvtStWriteBackRegAddrModeImm12 - Convert parsed operands to MCInst.
2916 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2917 /// when they refer multiple MIOperands inside a single one.
2919 cvtStWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
2920 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2921 // Create a writeback register dummy placeholder.
2922 Inst.addOperand(MCOperand::CreateImm(0));
2923 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2924 ((ARMOperand*)Operands[3])->addMemImm12OffsetOperands(Inst, 2);
2925 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2929 /// cvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
2930 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2931 /// when they refer multiple MIOperands inside a single one.
2933 cvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
2934 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2935 // Create a writeback register dummy placeholder.
2936 Inst.addOperand(MCOperand::CreateImm(0));
2937 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2938 ((ARMOperand*)Operands[3])->addAddrMode2Operands(Inst, 3);
2939 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2943 /// cvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
2944 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2945 /// when they refer multiple MIOperands inside a single one.
2947 cvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
2948 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2949 // Create a writeback register dummy placeholder.
2950 Inst.addOperand(MCOperand::CreateImm(0));
2951 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2952 ((ARMOperand*)Operands[3])->addAddrMode3Operands(Inst, 3);
2953 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2957 /// cvtLdExtTWriteBackImm - Convert parsed operands to MCInst.
2958 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2959 /// when they refer multiple MIOperands inside a single one.
2961 cvtLdExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
2962 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2964 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2965 // Create a writeback register dummy placeholder.
2966 Inst.addOperand(MCOperand::CreateImm(0));
2968 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
2970 ((ARMOperand*)Operands[4])->addPostIdxImm8Operands(Inst, 1);
2972 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2976 /// cvtLdExtTWriteBackReg - Convert parsed operands to MCInst.
2977 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2978 /// when they refer multiple MIOperands inside a single one.
2980 cvtLdExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
2981 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2983 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2984 // Create a writeback register dummy placeholder.
2985 Inst.addOperand(MCOperand::CreateImm(0));
2987 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
2989 ((ARMOperand*)Operands[4])->addPostIdxRegOperands(Inst, 2);
2991 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2995 /// cvtStExtTWriteBackImm - Convert parsed operands to MCInst.
2996 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2997 /// when they refer multiple MIOperands inside a single one.
2999 cvtStExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
3000 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3001 // Create a writeback register dummy placeholder.
3002 Inst.addOperand(MCOperand::CreateImm(0));
3004 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3006 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3008 ((ARMOperand*)Operands[4])->addPostIdxImm8Operands(Inst, 1);
3010 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3014 /// cvtStExtTWriteBackReg - Convert parsed operands to MCInst.
3015 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3016 /// when they refer multiple MIOperands inside a single one.
3018 cvtStExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
3019 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3020 // Create a writeback register dummy placeholder.
3021 Inst.addOperand(MCOperand::CreateImm(0));
3023 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3025 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3027 ((ARMOperand*)Operands[4])->addPostIdxRegOperands(Inst, 2);
3029 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3033 /// cvtLdrdPre - Convert parsed operands to MCInst.
3034 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3035 /// when they refer multiple MIOperands inside a single one.
3037 cvtLdrdPre(MCInst &Inst, unsigned Opcode,
3038 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3040 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3041 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3042 // Create a writeback register dummy placeholder.
3043 Inst.addOperand(MCOperand::CreateImm(0));
3045 ((ARMOperand*)Operands[4])->addAddrMode3Operands(Inst, 3);
3047 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3051 /// cvtStrdPre - Convert parsed operands to MCInst.
3052 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3053 /// when they refer multiple MIOperands inside a single one.
3055 cvtStrdPre(MCInst &Inst, unsigned Opcode,
3056 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3057 // Create a writeback register dummy placeholder.
3058 Inst.addOperand(MCOperand::CreateImm(0));
3060 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3061 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3063 ((ARMOperand*)Operands[4])->addAddrMode3Operands(Inst, 3);
3065 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3069 /// cvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
3070 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3071 /// when they refer multiple MIOperands inside a single one.
3073 cvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
3074 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3075 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3076 // Create a writeback register dummy placeholder.
3077 Inst.addOperand(MCOperand::CreateImm(0));
3078 ((ARMOperand*)Operands[3])->addAddrMode3Operands(Inst, 3);
3079 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3083 /// cvtThumbMultiple- Convert parsed operands to MCInst.
3084 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3085 /// when they refer multiple MIOperands inside a single one.
3087 cvtThumbMultiply(MCInst &Inst, unsigned Opcode,
3088 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3089 // The second source operand must be the same register as the destination
3091 if (Operands.size() == 6 &&
3092 (((ARMOperand*)Operands[3])->getReg() !=
3093 ((ARMOperand*)Operands[5])->getReg()) &&
3094 (((ARMOperand*)Operands[3])->getReg() !=
3095 ((ARMOperand*)Operands[4])->getReg())) {
3096 Error(Operands[3]->getStartLoc(),
3097 "destination register must match source register");
3100 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3101 ((ARMOperand*)Operands[1])->addCCOutOperands(Inst, 1);
3102 ((ARMOperand*)Operands[4])->addRegOperands(Inst, 1);
3103 // If we have a three-operand form, use that, else the second source operand
3104 // is just the destination operand again.
3105 if (Operands.size() == 6)
3106 ((ARMOperand*)Operands[5])->addRegOperands(Inst, 1);
3108 Inst.addOperand(Inst.getOperand(0));
3109 ((ARMOperand*)Operands[2])->addCondCodeOperands(Inst, 2);
3114 /// Parse an ARM memory expression, return false if successful else return true
3115 /// or an error. The first token must be a '[' when called.
3117 parseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3119 assert(Parser.getTok().is(AsmToken::LBrac) &&
3120 "Token is not a Left Bracket");
3121 S = Parser.getTok().getLoc();
3122 Parser.Lex(); // Eat left bracket token.
3124 const AsmToken &BaseRegTok = Parser.getTok();
3125 int BaseRegNum = tryParseRegister();
3126 if (BaseRegNum == -1)
3127 return Error(BaseRegTok.getLoc(), "register expected");
3129 // The next token must either be a comma or a closing bracket.
3130 const AsmToken &Tok = Parser.getTok();
3131 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
3132 return Error(Tok.getLoc(), "malformed memory operand");
3134 if (Tok.is(AsmToken::RBrac)) {
3136 Parser.Lex(); // Eat right bracket token.
3138 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, 0, ARM_AM::no_shift,
3139 0, 0, false, S, E));
3141 // If there's a pre-indexing writeback marker, '!', just add it as a token
3142 // operand. It's rather odd, but syntactically valid.
3143 if (Parser.getTok().is(AsmToken::Exclaim)) {
3144 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3145 Parser.Lex(); // Eat the '!'.
3151 assert(Tok.is(AsmToken::Comma) && "Lost comma in memory operand?!");
3152 Parser.Lex(); // Eat the comma.
3154 // If we have a ':', it's an alignment specifier.
3155 if (Parser.getTok().is(AsmToken::Colon)) {
3156 Parser.Lex(); // Eat the ':'.
3157 E = Parser.getTok().getLoc();
3160 if (getParser().ParseExpression(Expr))
3163 // The expression has to be a constant. Memory references with relocations
3164 // don't come through here, as they use the <label> forms of the relevant
3166 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
3168 return Error (E, "constant expression expected");
3171 switch (CE->getValue()) {
3173 return Error(E, "alignment specifier must be 64, 128, or 256 bits");
3174 case 64: Align = 8; break;
3175 case 128: Align = 16; break;
3176 case 256: Align = 32; break;
3179 // Now we should have the closing ']'
3180 E = Parser.getTok().getLoc();
3181 if (Parser.getTok().isNot(AsmToken::RBrac))
3182 return Error(E, "']' expected");
3183 Parser.Lex(); // Eat right bracket token.
3185 // Don't worry about range checking the value here. That's handled by
3186 // the is*() predicates.
3187 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, 0,
3188 ARM_AM::no_shift, 0, Align,
3191 // If there's a pre-indexing writeback marker, '!', just add it as a token
3193 if (Parser.getTok().is(AsmToken::Exclaim)) {
3194 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3195 Parser.Lex(); // Eat the '!'.
3201 // If we have a '#', it's an immediate offset, else assume it's a register
3203 if (Parser.getTok().is(AsmToken::Hash)) {
3204 Parser.Lex(); // Eat the '#'.
3205 E = Parser.getTok().getLoc();
3207 bool isNegative = getParser().getTok().is(AsmToken::Minus);
3208 const MCExpr *Offset;
3209 if (getParser().ParseExpression(Offset))
3212 // The expression has to be a constant. Memory references with relocations
3213 // don't come through here, as they use the <label> forms of the relevant
3215 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
3217 return Error (E, "constant expression expected");
3219 // If the constant was #-0, represent it as INT32_MIN.
3220 int32_t Val = CE->getValue();
3221 if (isNegative && Val == 0)
3222 CE = MCConstantExpr::Create(INT32_MIN, getContext());
3224 // Now we should have the closing ']'
3225 E = Parser.getTok().getLoc();
3226 if (Parser.getTok().isNot(AsmToken::RBrac))
3227 return Error(E, "']' expected");
3228 Parser.Lex(); // Eat right bracket token.
3230 // Don't worry about range checking the value here. That's handled by
3231 // the is*() predicates.
3232 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, CE, 0,
3233 ARM_AM::no_shift, 0, 0,
3236 // If there's a pre-indexing writeback marker, '!', just add it as a token
3238 if (Parser.getTok().is(AsmToken::Exclaim)) {
3239 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3240 Parser.Lex(); // Eat the '!'.
3246 // The register offset is optionally preceded by a '+' or '-'
3247 bool isNegative = false;
3248 if (Parser.getTok().is(AsmToken::Minus)) {
3250 Parser.Lex(); // Eat the '-'.
3251 } else if (Parser.getTok().is(AsmToken::Plus)) {
3253 Parser.Lex(); // Eat the '+'.
3256 E = Parser.getTok().getLoc();
3257 int OffsetRegNum = tryParseRegister();
3258 if (OffsetRegNum == -1)
3259 return Error(E, "register expected");
3261 // If there's a shift operator, handle it.
3262 ARM_AM::ShiftOpc ShiftType = ARM_AM::no_shift;
3263 unsigned ShiftImm = 0;
3264 if (Parser.getTok().is(AsmToken::Comma)) {
3265 Parser.Lex(); // Eat the ','.
3266 if (parseMemRegOffsetShift(ShiftType, ShiftImm))
3270 // Now we should have the closing ']'
3271 E = Parser.getTok().getLoc();
3272 if (Parser.getTok().isNot(AsmToken::RBrac))
3273 return Error(E, "']' expected");
3274 Parser.Lex(); // Eat right bracket token.
3276 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, OffsetRegNum,
3277 ShiftType, ShiftImm, 0, isNegative,
3280 // If there's a pre-indexing writeback marker, '!', just add it as a token
3282 if (Parser.getTok().is(AsmToken::Exclaim)) {
3283 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3284 Parser.Lex(); // Eat the '!'.
3290 /// parseMemRegOffsetShift - one of these two:
3291 /// ( lsl | lsr | asr | ror ) , # shift_amount
3293 /// return true if it parses a shift otherwise it returns false.
3294 bool ARMAsmParser::parseMemRegOffsetShift(ARM_AM::ShiftOpc &St,
3296 SMLoc Loc = Parser.getTok().getLoc();
3297 const AsmToken &Tok = Parser.getTok();
3298 if (Tok.isNot(AsmToken::Identifier))
3300 StringRef ShiftName = Tok.getString();
3301 if (ShiftName == "lsl" || ShiftName == "LSL")
3303 else if (ShiftName == "lsr" || ShiftName == "LSR")
3305 else if (ShiftName == "asr" || ShiftName == "ASR")
3307 else if (ShiftName == "ror" || ShiftName == "ROR")
3309 else if (ShiftName == "rrx" || ShiftName == "RRX")
3312 return Error(Loc, "illegal shift operator");
3313 Parser.Lex(); // Eat shift type token.
3315 // rrx stands alone.
3317 if (St != ARM_AM::rrx) {
3318 Loc = Parser.getTok().getLoc();
3319 // A '#' and a shift amount.
3320 const AsmToken &HashTok = Parser.getTok();
3321 if (HashTok.isNot(AsmToken::Hash))
3322 return Error(HashTok.getLoc(), "'#' expected");
3323 Parser.Lex(); // Eat hash token.
3326 if (getParser().ParseExpression(Expr))
3328 // Range check the immediate.
3329 // lsl, ror: 0 <= imm <= 31
3330 // lsr, asr: 0 <= imm <= 32
3331 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
3333 return Error(Loc, "shift amount must be an immediate");
3334 int64_t Imm = CE->getValue();
3336 ((St == ARM_AM::lsl || St == ARM_AM::ror) && Imm > 31) ||
3337 ((St == ARM_AM::lsr || St == ARM_AM::asr) && Imm > 32))
3338 return Error(Loc, "immediate shift value out of range");
3345 /// parseFPImm - A floating point immediate expression operand.
3346 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3347 parseFPImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3348 SMLoc S = Parser.getTok().getLoc();
3350 if (Parser.getTok().isNot(AsmToken::Hash))
3351 return MatchOperand_NoMatch;
3353 // Disambiguate the VMOV forms that can accept an FP immediate.
3354 // vmov.f32 <sreg>, #imm
3355 // vmov.f64 <dreg>, #imm
3356 // vmov.f32 <dreg>, #imm @ vector f32x2
3357 // vmov.f32 <qreg>, #imm @ vector f32x4
3359 // There are also the NEON VMOV instructions which expect an
3360 // integer constant. Make sure we don't try to parse an FPImm
3362 // vmov.i{8|16|32|64} <dreg|qreg>, #imm
3363 ARMOperand *TyOp = static_cast<ARMOperand*>(Operands[2]);
3364 if (!TyOp->isToken() || (TyOp->getToken() != ".f32" &&
3365 TyOp->getToken() != ".f64"))
3366 return MatchOperand_NoMatch;
3368 Parser.Lex(); // Eat the '#'.
3370 // Handle negation, as that still comes through as a separate token.
3371 bool isNegative = false;
3372 if (Parser.getTok().is(AsmToken::Minus)) {
3376 const AsmToken &Tok = Parser.getTok();
3377 if (Tok.is(AsmToken::Real)) {
3378 APFloat RealVal(APFloat::IEEEdouble, Tok.getString());
3379 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
3380 // If we had a '-' in front, toggle the sign bit.
3381 IntVal ^= (uint64_t)isNegative << 63;
3382 int Val = ARM_AM::getFP64Imm(APInt(64, IntVal));
3383 Parser.Lex(); // Eat the token.
3385 TokError("floating point value out of range");
3386 return MatchOperand_ParseFail;
3388 Operands.push_back(ARMOperand::CreateFPImm(Val, S, getContext()));
3389 return MatchOperand_Success;
3391 if (Tok.is(AsmToken::Integer)) {
3392 int64_t Val = Tok.getIntVal();
3393 Parser.Lex(); // Eat the token.
3394 if (Val > 255 || Val < 0) {
3395 TokError("encoded floating point value out of range");
3396 return MatchOperand_ParseFail;
3398 Operands.push_back(ARMOperand::CreateFPImm(Val, S, getContext()));
3399 return MatchOperand_Success;
3402 TokError("invalid floating point immediate");
3403 return MatchOperand_ParseFail;
3405 /// Parse a arm instruction operand. For now this parses the operand regardless
3406 /// of the mnemonic.
3407 bool ARMAsmParser::parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
3408 StringRef Mnemonic) {
3411 // Check if the current operand has a custom associated parser, if so, try to
3412 // custom parse the operand, or fallback to the general approach.
3413 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
3414 if (ResTy == MatchOperand_Success)
3416 // If there wasn't a custom match, try the generic matcher below. Otherwise,
3417 // there was a match, but an error occurred, in which case, just return that
3418 // the operand parsing failed.
3419 if (ResTy == MatchOperand_ParseFail)
3422 switch (getLexer().getKind()) {
3424 Error(Parser.getTok().getLoc(), "unexpected token in operand");
3426 case AsmToken::Identifier: {
3427 // If this is VMRS, check for the apsr_nzcv operand.
3428 if (!tryParseRegisterWithWriteBack(Operands))
3430 int Res = tryParseShiftRegister(Operands);
3431 if (Res == 0) // success
3433 else if (Res == -1) // irrecoverable error
3435 if (Mnemonic == "vmrs" && Parser.getTok().getString() == "apsr_nzcv") {
3436 S = Parser.getTok().getLoc();
3438 Operands.push_back(ARMOperand::CreateToken("apsr_nzcv", S));
3442 // Fall though for the Identifier case that is not a register or a
3445 case AsmToken::Integer: // things like 1f and 2b as a branch targets
3446 case AsmToken::Dot: { // . as a branch target
3447 // This was not a register so parse other operands that start with an
3448 // identifier (like labels) as expressions and create them as immediates.
3449 const MCExpr *IdVal;
3450 S = Parser.getTok().getLoc();
3451 if (getParser().ParseExpression(IdVal))
3453 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3454 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
3457 case AsmToken::LBrac:
3458 return parseMemory(Operands);
3459 case AsmToken::LCurly:
3460 return parseRegisterList(Operands);
3461 case AsmToken::Hash: {
3462 // #42 -> immediate.
3463 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
3464 S = Parser.getTok().getLoc();
3466 bool isNegative = Parser.getTok().is(AsmToken::Minus);
3467 const MCExpr *ImmVal;
3468 if (getParser().ParseExpression(ImmVal))
3470 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ImmVal);
3472 Error(S, "constant expression expected");
3473 return MatchOperand_ParseFail;
3475 int32_t Val = CE->getValue();
3476 if (isNegative && Val == 0)
3477 ImmVal = MCConstantExpr::Create(INT32_MIN, getContext());
3478 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3479 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
3482 case AsmToken::Colon: {
3483 // ":lower16:" and ":upper16:" expression prefixes
3484 // FIXME: Check it's an expression prefix,
3485 // e.g. (FOO - :lower16:BAR) isn't legal.
3486 ARMMCExpr::VariantKind RefKind;
3487 if (parsePrefix(RefKind))
3490 const MCExpr *SubExprVal;
3491 if (getParser().ParseExpression(SubExprVal))
3494 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
3496 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3497 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
3503 // parsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
3504 // :lower16: and :upper16:.
3505 bool ARMAsmParser::parsePrefix(ARMMCExpr::VariantKind &RefKind) {
3506 RefKind = ARMMCExpr::VK_ARM_None;
3508 // :lower16: and :upper16: modifiers
3509 assert(getLexer().is(AsmToken::Colon) && "expected a :");
3510 Parser.Lex(); // Eat ':'
3512 if (getLexer().isNot(AsmToken::Identifier)) {
3513 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
3517 StringRef IDVal = Parser.getTok().getIdentifier();
3518 if (IDVal == "lower16") {
3519 RefKind = ARMMCExpr::VK_ARM_LO16;
3520 } else if (IDVal == "upper16") {
3521 RefKind = ARMMCExpr::VK_ARM_HI16;
3523 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
3528 if (getLexer().isNot(AsmToken::Colon)) {
3529 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
3532 Parser.Lex(); // Eat the last ':'
3536 /// \brief Given a mnemonic, split out possible predication code and carry
3537 /// setting letters to form a canonical mnemonic and flags.
3539 // FIXME: Would be nice to autogen this.
3540 // FIXME: This is a bit of a maze of special cases.
3541 StringRef ARMAsmParser::splitMnemonic(StringRef Mnemonic,
3542 unsigned &PredicationCode,
3544 unsigned &ProcessorIMod,
3545 StringRef &ITMask) {
3546 PredicationCode = ARMCC::AL;
3547 CarrySetting = false;
3550 // Ignore some mnemonics we know aren't predicated forms.
3552 // FIXME: Would be nice to autogen this.
3553 if ((Mnemonic == "movs" && isThumb()) ||
3554 Mnemonic == "teq" || Mnemonic == "vceq" || Mnemonic == "svc" ||
3555 Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
3556 Mnemonic == "vmls" || Mnemonic == "vnmls" || Mnemonic == "vacge" ||
3557 Mnemonic == "vcge" || Mnemonic == "vclt" || Mnemonic == "vacgt" ||
3558 Mnemonic == "vcgt" || Mnemonic == "vcle" || Mnemonic == "smlal" ||
3559 Mnemonic == "umaal" || Mnemonic == "umlal" || Mnemonic == "vabal" ||
3560 Mnemonic == "vmlal" || Mnemonic == "vpadal" || Mnemonic == "vqdmlal")
3563 // First, split out any predication code. Ignore mnemonics we know aren't
3564 // predicated but do have a carry-set and so weren't caught above.
3565 if (Mnemonic != "adcs" && Mnemonic != "bics" && Mnemonic != "movs" &&
3566 Mnemonic != "muls" && Mnemonic != "smlals" && Mnemonic != "smulls" &&
3567 Mnemonic != "umlals" && Mnemonic != "umulls" && Mnemonic != "lsls" &&
3568 Mnemonic != "sbcs" && Mnemonic != "rscs") {
3569 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
3570 .Case("eq", ARMCC::EQ)
3571 .Case("ne", ARMCC::NE)
3572 .Case("hs", ARMCC::HS)
3573 .Case("cs", ARMCC::HS)
3574 .Case("lo", ARMCC::LO)
3575 .Case("cc", ARMCC::LO)
3576 .Case("mi", ARMCC::MI)
3577 .Case("pl", ARMCC::PL)
3578 .Case("vs", ARMCC::VS)
3579 .Case("vc", ARMCC::VC)
3580 .Case("hi", ARMCC::HI)
3581 .Case("ls", ARMCC::LS)
3582 .Case("ge", ARMCC::GE)
3583 .Case("lt", ARMCC::LT)
3584 .Case("gt", ARMCC::GT)
3585 .Case("le", ARMCC::LE)
3586 .Case("al", ARMCC::AL)
3589 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
3590 PredicationCode = CC;
3594 // Next, determine if we have a carry setting bit. We explicitly ignore all
3595 // the instructions we know end in 's'.
3596 if (Mnemonic.endswith("s") &&
3597 !(Mnemonic == "cps" || Mnemonic == "mls" ||
3598 Mnemonic == "mrs" || Mnemonic == "smmls" || Mnemonic == "vabs" ||
3599 Mnemonic == "vcls" || Mnemonic == "vmls" || Mnemonic == "vmrs" ||
3600 Mnemonic == "vnmls" || Mnemonic == "vqabs" || Mnemonic == "vrecps" ||
3601 Mnemonic == "vrsqrts" || Mnemonic == "srs" ||
3602 (Mnemonic == "movs" && isThumb()))) {
3603 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
3604 CarrySetting = true;
3607 // The "cps" instruction can have a interrupt mode operand which is glued into
3608 // the mnemonic. Check if this is the case, split it and parse the imod op
3609 if (Mnemonic.startswith("cps")) {
3610 // Split out any imod code.
3612 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
3613 .Case("ie", ARM_PROC::IE)
3614 .Case("id", ARM_PROC::ID)
3617 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
3618 ProcessorIMod = IMod;
3622 // The "it" instruction has the condition mask on the end of the mnemonic.
3623 if (Mnemonic.startswith("it")) {
3624 ITMask = Mnemonic.slice(2, Mnemonic.size());
3625 Mnemonic = Mnemonic.slice(0, 2);
3631 /// \brief Given a canonical mnemonic, determine if the instruction ever allows
3632 /// inclusion of carry set or predication code operands.
3634 // FIXME: It would be nice to autogen this.
3636 getMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
3637 bool &CanAcceptPredicationCode) {
3638 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
3639 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
3640 Mnemonic == "add" || Mnemonic == "adc" ||
3641 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
3642 Mnemonic == "orr" || Mnemonic == "mvn" ||
3643 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
3644 Mnemonic == "sbc" || Mnemonic == "eor" || Mnemonic == "neg" ||
3645 (!isThumb() && (Mnemonic == "smull" || Mnemonic == "mov" ||
3646 Mnemonic == "mla" || Mnemonic == "smlal" ||
3647 Mnemonic == "umlal" || Mnemonic == "umull"))) {
3648 CanAcceptCarrySet = true;
3650 CanAcceptCarrySet = false;
3652 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
3653 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
3654 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
3655 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
3656 Mnemonic == "dsb" || Mnemonic == "isb" || Mnemonic == "setend" ||
3657 (Mnemonic == "clrex" && !isThumb()) ||
3658 (Mnemonic == "nop" && isThumbOne()) ||
3659 ((Mnemonic == "pld" || Mnemonic == "pli" || Mnemonic == "pldw" ||
3660 Mnemonic == "ldc2" || Mnemonic == "ldc2l" ||
3661 Mnemonic == "stc2" || Mnemonic == "stc2l") && !isThumb()) ||
3662 ((Mnemonic.startswith("rfe") || Mnemonic.startswith("srs")) &&
3664 Mnemonic.startswith("cps") || (Mnemonic == "movs" && isThumbOne())) {
3665 CanAcceptPredicationCode = false;
3667 CanAcceptPredicationCode = true;
3670 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
3671 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
3672 CanAcceptPredicationCode = false;
3676 bool ARMAsmParser::shouldOmitCCOutOperand(StringRef Mnemonic,
3677 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3678 // FIXME: This is all horribly hacky. We really need a better way to deal
3679 // with optional operands like this in the matcher table.
3681 // The 'mov' mnemonic is special. One variant has a cc_out operand, while
3682 // another does not. Specifically, the MOVW instruction does not. So we
3683 // special case it here and remove the defaulted (non-setting) cc_out
3684 // operand if that's the instruction we're trying to match.
3686 // We do this as post-processing of the explicit operands rather than just
3687 // conditionally adding the cc_out in the first place because we need
3688 // to check the type of the parsed immediate operand.
3689 if (Mnemonic == "mov" && Operands.size() > 4 && !isThumb() &&
3690 !static_cast<ARMOperand*>(Operands[4])->isARMSOImm() &&
3691 static_cast<ARMOperand*>(Operands[4])->isImm0_65535Expr() &&
3692 static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
3695 // Register-register 'add' for thumb does not have a cc_out operand
3696 // when there are only two register operands.
3697 if (isThumb() && Mnemonic == "add" && Operands.size() == 5 &&
3698 static_cast<ARMOperand*>(Operands[3])->isReg() &&
3699 static_cast<ARMOperand*>(Operands[4])->isReg() &&
3700 static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
3702 // Register-register 'add' for thumb does not have a cc_out operand
3703 // when it's an ADD Rdm, SP, {Rdm|#imm0_255} instruction. We do
3704 // have to check the immediate range here since Thumb2 has a variant
3705 // that can handle a different range and has a cc_out operand.
3706 if (((isThumb() && Mnemonic == "add") ||
3707 (isThumbTwo() && Mnemonic == "sub")) &&
3708 Operands.size() == 6 &&
3709 static_cast<ARMOperand*>(Operands[3])->isReg() &&
3710 static_cast<ARMOperand*>(Operands[4])->isReg() &&
3711 static_cast<ARMOperand*>(Operands[4])->getReg() == ARM::SP &&
3712 static_cast<ARMOperand*>(Operands[1])->getReg() == 0 &&
3713 (static_cast<ARMOperand*>(Operands[5])->isReg() ||
3714 static_cast<ARMOperand*>(Operands[5])->isImm0_1020s4()))
3716 // For Thumb2, add/sub immediate does not have a cc_out operand for the
3717 // imm0_4095 variant. That's the least-preferred variant when
3718 // selecting via the generic "add" mnemonic, so to know that we
3719 // should remove the cc_out operand, we have to explicitly check that
3720 // it's not one of the other variants. Ugh.
3721 if (isThumbTwo() && (Mnemonic == "add" || Mnemonic == "sub") &&
3722 Operands.size() == 6 &&
3723 static_cast<ARMOperand*>(Operands[3])->isReg() &&
3724 static_cast<ARMOperand*>(Operands[4])->isReg() &&
3725 static_cast<ARMOperand*>(Operands[5])->isImm()) {
3726 // Nest conditions rather than one big 'if' statement for readability.
3728 // If either register is a high reg, it's either one of the SP
3729 // variants (handled above) or a 32-bit encoding, so we just
3730 // check against T3.
3731 if ((!isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) ||
3732 !isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg())) &&
3733 static_cast<ARMOperand*>(Operands[5])->isT2SOImm())
3735 // If both registers are low, we're in an IT block, and the immediate is
3736 // in range, we should use encoding T1 instead, which has a cc_out.
3738 isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) &&
3739 isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg()) &&
3740 static_cast<ARMOperand*>(Operands[5])->isImm0_7())
3743 // Otherwise, we use encoding T4, which does not have a cc_out
3748 // The thumb2 multiply instruction doesn't have a CCOut register, so
3749 // if we have a "mul" mnemonic in Thumb mode, check if we'll be able to
3750 // use the 16-bit encoding or not.
3751 if (isThumbTwo() && Mnemonic == "mul" && Operands.size() == 6 &&
3752 static_cast<ARMOperand*>(Operands[1])->getReg() == 0 &&
3753 static_cast<ARMOperand*>(Operands[3])->isReg() &&
3754 static_cast<ARMOperand*>(Operands[4])->isReg() &&
3755 static_cast<ARMOperand*>(Operands[5])->isReg() &&
3756 // If the registers aren't low regs, the destination reg isn't the
3757 // same as one of the source regs, or the cc_out operand is zero
3758 // outside of an IT block, we have to use the 32-bit encoding, so
3759 // remove the cc_out operand.
3760 (!isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) ||
3761 !isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg()) ||
3763 (static_cast<ARMOperand*>(Operands[3])->getReg() !=
3764 static_cast<ARMOperand*>(Operands[5])->getReg() &&
3765 static_cast<ARMOperand*>(Operands[3])->getReg() !=
3766 static_cast<ARMOperand*>(Operands[4])->getReg())))
3771 // Register-register 'add/sub' for thumb does not have a cc_out operand
3772 // when it's an ADD/SUB SP, #imm. Be lenient on count since there's also
3773 // the "add/sub SP, SP, #imm" version. If the follow-up operands aren't
3774 // right, this will result in better diagnostics (which operand is off)
3776 if (isThumb() && (Mnemonic == "add" || Mnemonic == "sub") &&
3777 (Operands.size() == 5 || Operands.size() == 6) &&
3778 static_cast<ARMOperand*>(Operands[3])->isReg() &&
3779 static_cast<ARMOperand*>(Operands[3])->getReg() == ARM::SP &&
3780 static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
3786 /// Parse an arm instruction mnemonic followed by its operands.
3787 bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
3788 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3789 // Create the leading tokens for the mnemonic, split by '.' characters.
3790 size_t Start = 0, Next = Name.find('.');
3791 StringRef Mnemonic = Name.slice(Start, Next);
3793 // Split out the predication code and carry setting flag from the mnemonic.
3794 unsigned PredicationCode;
3795 unsigned ProcessorIMod;
3798 Mnemonic = splitMnemonic(Mnemonic, PredicationCode, CarrySetting,
3799 ProcessorIMod, ITMask);
3801 // In Thumb1, only the branch (B) instruction can be predicated.
3802 if (isThumbOne() && PredicationCode != ARMCC::AL && Mnemonic != "b") {
3803 Parser.EatToEndOfStatement();
3804 return Error(NameLoc, "conditional execution not supported in Thumb1");
3807 Operands.push_back(ARMOperand::CreateToken(Mnemonic, NameLoc));
3809 // Handle the IT instruction ITMask. Convert it to a bitmask. This
3810 // is the mask as it will be for the IT encoding if the conditional
3811 // encoding has a '1' as it's bit0 (i.e. 't' ==> '1'). In the case
3812 // where the conditional bit0 is zero, the instruction post-processing
3813 // will adjust the mask accordingly.
3814 if (Mnemonic == "it") {
3815 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + 2);
3816 if (ITMask.size() > 3) {
3817 Parser.EatToEndOfStatement();
3818 return Error(Loc, "too many conditions on IT instruction");
3821 for (unsigned i = ITMask.size(); i != 0; --i) {
3822 char pos = ITMask[i - 1];
3823 if (pos != 't' && pos != 'e') {
3824 Parser.EatToEndOfStatement();
3825 return Error(Loc, "illegal IT block condition mask '" + ITMask + "'");
3828 if (ITMask[i - 1] == 't')
3831 Operands.push_back(ARMOperand::CreateITMask(Mask, Loc));
3834 // FIXME: This is all a pretty gross hack. We should automatically handle
3835 // optional operands like this via tblgen.
3837 // Next, add the CCOut and ConditionCode operands, if needed.
3839 // For mnemonics which can ever incorporate a carry setting bit or predication
3840 // code, our matching model involves us always generating CCOut and
3841 // ConditionCode operands to match the mnemonic "as written" and then we let
3842 // the matcher deal with finding the right instruction or generating an
3843 // appropriate error.
3844 bool CanAcceptCarrySet, CanAcceptPredicationCode;
3845 getMnemonicAcceptInfo(Mnemonic, CanAcceptCarrySet, CanAcceptPredicationCode);
3847 // If we had a carry-set on an instruction that can't do that, issue an
3849 if (!CanAcceptCarrySet && CarrySetting) {
3850 Parser.EatToEndOfStatement();
3851 return Error(NameLoc, "instruction '" + Mnemonic +
3852 "' can not set flags, but 's' suffix specified");
3854 // If we had a predication code on an instruction that can't do that, issue an
3856 if (!CanAcceptPredicationCode && PredicationCode != ARMCC::AL) {
3857 Parser.EatToEndOfStatement();
3858 return Error(NameLoc, "instruction '" + Mnemonic +
3859 "' is not predicable, but condition code specified");
3862 // Add the carry setting operand, if necessary.
3863 if (CanAcceptCarrySet) {
3864 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size());
3865 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
3869 // Add the predication code operand, if necessary.
3870 if (CanAcceptPredicationCode) {
3871 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size() +
3873 Operands.push_back(ARMOperand::CreateCondCode(
3874 ARMCC::CondCodes(PredicationCode), Loc));
3877 // Add the processor imod operand, if necessary.
3878 if (ProcessorIMod) {
3879 Operands.push_back(ARMOperand::CreateImm(
3880 MCConstantExpr::Create(ProcessorIMod, getContext()),
3884 // Add the remaining tokens in the mnemonic.
3885 while (Next != StringRef::npos) {
3887 Next = Name.find('.', Start + 1);
3888 StringRef ExtraToken = Name.slice(Start, Next);
3890 // For now, we're only parsing Thumb1 (for the most part), so
3891 // just ignore ".n" qualifiers. We'll use them to restrict
3892 // matching when we do Thumb2.
3893 if (ExtraToken != ".n") {
3894 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Start);
3895 Operands.push_back(ARMOperand::CreateToken(ExtraToken, Loc));
3899 // Read the remaining operands.
3900 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3901 // Read the first operand.
3902 if (parseOperand(Operands, Mnemonic)) {
3903 Parser.EatToEndOfStatement();
3907 while (getLexer().is(AsmToken::Comma)) {
3908 Parser.Lex(); // Eat the comma.
3910 // Parse and remember the operand.
3911 if (parseOperand(Operands, Mnemonic)) {
3912 Parser.EatToEndOfStatement();
3918 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3919 SMLoc Loc = getLexer().getLoc();
3920 Parser.EatToEndOfStatement();
3921 return Error(Loc, "unexpected token in argument list");
3924 Parser.Lex(); // Consume the EndOfStatement
3926 // Some instructions, mostly Thumb, have forms for the same mnemonic that
3927 // do and don't have a cc_out optional-def operand. With some spot-checks
3928 // of the operand list, we can figure out which variant we're trying to
3929 // parse and adjust accordingly before actually matching. We shouldn't ever
3930 // try to remove a cc_out operand that was explicitly set on the the
3931 // mnemonic, of course (CarrySetting == true). Reason number #317 the
3932 // table driven matcher doesn't fit well with the ARM instruction set.
3933 if (!CarrySetting && shouldOmitCCOutOperand(Mnemonic, Operands)) {
3934 ARMOperand *Op = static_cast<ARMOperand*>(Operands[1]);
3935 Operands.erase(Operands.begin() + 1);
3939 // ARM mode 'blx' need special handling, as the register operand version
3940 // is predicable, but the label operand version is not. So, we can't rely
3941 // on the Mnemonic based checking to correctly figure out when to put
3942 // a k_CondCode operand in the list. If we're trying to match the label
3943 // version, remove the k_CondCode operand here.
3944 if (!isThumb() && Mnemonic == "blx" && Operands.size() == 3 &&
3945 static_cast<ARMOperand*>(Operands[2])->isImm()) {
3946 ARMOperand *Op = static_cast<ARMOperand*>(Operands[1]);
3947 Operands.erase(Operands.begin() + 1);
3951 // The vector-compare-to-zero instructions have a literal token "#0" at
3952 // the end that comes to here as an immediate operand. Convert it to a
3953 // token to play nicely with the matcher.
3954 if ((Mnemonic == "vceq" || Mnemonic == "vcge" || Mnemonic == "vcgt" ||
3955 Mnemonic == "vcle" || Mnemonic == "vclt") && Operands.size() == 6 &&
3956 static_cast<ARMOperand*>(Operands[5])->isImm()) {
3957 ARMOperand *Op = static_cast<ARMOperand*>(Operands[5]);
3958 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
3959 if (CE && CE->getValue() == 0) {
3960 Operands.erase(Operands.begin() + 5);
3961 Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
3965 // VCMP{E} does the same thing, but with a different operand count.
3966 if ((Mnemonic == "vcmp" || Mnemonic == "vcmpe") && Operands.size() == 5 &&
3967 static_cast<ARMOperand*>(Operands[4])->isImm()) {
3968 ARMOperand *Op = static_cast<ARMOperand*>(Operands[4]);
3969 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
3970 if (CE && CE->getValue() == 0) {
3971 Operands.erase(Operands.begin() + 4);
3972 Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
3976 // Similarly, the Thumb1 "RSB" instruction has a literal "#0" on the
3977 // end. Convert it to a token here.
3978 if (Mnemonic == "rsb" && isThumb() && Operands.size() == 6 &&
3979 static_cast<ARMOperand*>(Operands[5])->isImm()) {
3980 ARMOperand *Op = static_cast<ARMOperand*>(Operands[5]);
3981 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
3982 if (CE && CE->getValue() == 0) {
3983 Operands.erase(Operands.begin() + 5);
3984 Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
3992 // Validate context-sensitive operand constraints.
3994 // return 'true' if register list contains non-low GPR registers,
3995 // 'false' otherwise. If Reg is in the register list or is HiReg, set
3996 // 'containsReg' to true.
3997 static bool checkLowRegisterList(MCInst Inst, unsigned OpNo, unsigned Reg,
3998 unsigned HiReg, bool &containsReg) {
3999 containsReg = false;
4000 for (unsigned i = OpNo; i < Inst.getNumOperands(); ++i) {
4001 unsigned OpReg = Inst.getOperand(i).getReg();
4004 // Anything other than a low register isn't legal here.
4005 if (!isARMLowRegister(OpReg) && (!HiReg || OpReg != HiReg))
4011 // Check if the specified regisgter is in the register list of the inst,
4012 // starting at the indicated operand number.
4013 static bool listContainsReg(MCInst &Inst, unsigned OpNo, unsigned Reg) {
4014 for (unsigned i = OpNo; i < Inst.getNumOperands(); ++i) {
4015 unsigned OpReg = Inst.getOperand(i).getReg();
4022 // FIXME: We would really prefer to have MCInstrInfo (the wrapper around
4023 // the ARMInsts array) instead. Getting that here requires awkward
4024 // API changes, though. Better way?
4026 extern MCInstrDesc ARMInsts[];
4028 static MCInstrDesc &getInstDesc(unsigned Opcode) {
4029 return ARMInsts[Opcode];
4032 // FIXME: We would really like to be able to tablegen'erate this.
4034 validateInstruction(MCInst &Inst,
4035 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
4036 MCInstrDesc &MCID = getInstDesc(Inst.getOpcode());
4037 SMLoc Loc = Operands[0]->getStartLoc();
4038 // Check the IT block state first.
4039 // NOTE: In Thumb mode, the BKPT instruction has the interesting property of
4040 // being allowed in IT blocks, but not being predicable. It just always
4042 if (inITBlock() && Inst.getOpcode() != ARM::tBKPT) {
4044 if (ITState.FirstCond)
4045 ITState.FirstCond = false;
4047 bit = (ITState.Mask >> (5 - ITState.CurPosition)) & 1;
4048 // The instruction must be predicable.
4049 if (!MCID.isPredicable())
4050 return Error(Loc, "instructions in IT block must be predicable");
4051 unsigned Cond = Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm();
4052 unsigned ITCond = bit ? ITState.Cond :
4053 ARMCC::getOppositeCondition(ITState.Cond);
4054 if (Cond != ITCond) {
4055 // Find the condition code Operand to get its SMLoc information.
4057 for (unsigned i = 1; i < Operands.size(); ++i)
4058 if (static_cast<ARMOperand*>(Operands[i])->isCondCode())
4059 CondLoc = Operands[i]->getStartLoc();
4060 return Error(CondLoc, "incorrect condition in IT block; got '" +
4061 StringRef(ARMCondCodeToString(ARMCC::CondCodes(Cond))) +
4062 "', but expected '" +
4063 ARMCondCodeToString(ARMCC::CondCodes(ITCond)) + "'");
4065 // Check for non-'al' condition codes outside of the IT block.
4066 } else if (isThumbTwo() && MCID.isPredicable() &&
4067 Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm() !=
4068 ARMCC::AL && Inst.getOpcode() != ARM::tB &&
4069 Inst.getOpcode() != ARM::t2B)
4070 return Error(Loc, "predicated instructions must be in IT block");
4072 switch (Inst.getOpcode()) {
4075 case ARM::LDRD_POST:
4077 // Rt2 must be Rt + 1.
4078 unsigned Rt = getARMRegisterNumbering(Inst.getOperand(0).getReg());
4079 unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(1).getReg());
4081 return Error(Operands[3]->getStartLoc(),
4082 "destination operands must be sequential");
4086 // Rt2 must be Rt + 1.
4087 unsigned Rt = getARMRegisterNumbering(Inst.getOperand(0).getReg());
4088 unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(1).getReg());
4090 return Error(Operands[3]->getStartLoc(),
4091 "source operands must be sequential");
4095 case ARM::STRD_POST:
4097 // Rt2 must be Rt + 1.
4098 unsigned Rt = getARMRegisterNumbering(Inst.getOperand(1).getReg());
4099 unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(2).getReg());
4101 return Error(Operands[3]->getStartLoc(),
4102 "source operands must be sequential");
4107 // width must be in range [1, 32-lsb]
4108 unsigned lsb = Inst.getOperand(2).getImm();
4109 unsigned widthm1 = Inst.getOperand(3).getImm();
4110 if (widthm1 >= 32 - lsb)
4111 return Error(Operands[5]->getStartLoc(),
4112 "bitfield width must be in range [1,32-lsb]");
4116 // If we're parsing Thumb2, the .w variant is available and handles
4117 // most cases that are normally illegal for a Thumb1 LDM
4118 // instruction. We'll make the transformation in processInstruction()
4121 // Thumb LDM instructions are writeback iff the base register is not
4122 // in the register list.
4123 unsigned Rn = Inst.getOperand(0).getReg();
4124 bool hasWritebackToken =
4125 (static_cast<ARMOperand*>(Operands[3])->isToken() &&
4126 static_cast<ARMOperand*>(Operands[3])->getToken() == "!");
4127 bool listContainsBase;
4128 if (checkLowRegisterList(Inst, 3, Rn, 0, listContainsBase) && !isThumbTwo())
4129 return Error(Operands[3 + hasWritebackToken]->getStartLoc(),
4130 "registers must be in range r0-r7");
4131 // If we should have writeback, then there should be a '!' token.
4132 if (!listContainsBase && !hasWritebackToken && !isThumbTwo())
4133 return Error(Operands[2]->getStartLoc(),
4134 "writeback operator '!' expected");
4135 // If we should not have writeback, there must not be a '!'. This is
4136 // true even for the 32-bit wide encodings.
4137 if (listContainsBase && hasWritebackToken)
4138 return Error(Operands[3]->getStartLoc(),
4139 "writeback operator '!' not allowed when base register "
4140 "in register list");
4144 case ARM::t2LDMIA_UPD: {
4145 if (listContainsReg(Inst, 3, Inst.getOperand(0).getReg()))
4146 return Error(Operands[4]->getStartLoc(),
4147 "writeback operator '!' not allowed when base register "
4148 "in register list");
4152 bool listContainsBase;
4153 if (checkLowRegisterList(Inst, 3, 0, ARM::PC, listContainsBase))
4154 return Error(Operands[2]->getStartLoc(),
4155 "registers must be in range r0-r7 or pc");
4159 bool listContainsBase;
4160 if (checkLowRegisterList(Inst, 3, 0, ARM::LR, listContainsBase))
4161 return Error(Operands[2]->getStartLoc(),
4162 "registers must be in range r0-r7 or lr");
4165 case ARM::tSTMIA_UPD: {
4166 bool listContainsBase;
4167 if (checkLowRegisterList(Inst, 4, 0, 0, listContainsBase) && !isThumbTwo())
4168 return Error(Operands[4]->getStartLoc(),
4169 "registers must be in range r0-r7");
4178 processInstruction(MCInst &Inst,
4179 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
4180 switch (Inst.getOpcode()) {
4181 case ARM::LDMIA_UPD:
4182 // If this is a load of a single register via a 'pop', then we should use
4183 // a post-indexed LDR instruction instead, per the ARM ARM.
4184 if (static_cast<ARMOperand*>(Operands[0])->getToken() == "pop" &&
4185 Inst.getNumOperands() == 5) {
4187 TmpInst.setOpcode(ARM::LDR_POST_IMM);
4188 TmpInst.addOperand(Inst.getOperand(4)); // Rt
4189 TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
4190 TmpInst.addOperand(Inst.getOperand(1)); // Rn
4191 TmpInst.addOperand(MCOperand::CreateReg(0)); // am2offset
4192 TmpInst.addOperand(MCOperand::CreateImm(4));
4193 TmpInst.addOperand(Inst.getOperand(2)); // CondCode
4194 TmpInst.addOperand(Inst.getOperand(3));
4198 case ARM::STMDB_UPD:
4199 // If this is a store of a single register via a 'push', then we should use
4200 // a pre-indexed STR instruction instead, per the ARM ARM.
4201 if (static_cast<ARMOperand*>(Operands[0])->getToken() == "push" &&
4202 Inst.getNumOperands() == 5) {
4204 TmpInst.setOpcode(ARM::STR_PRE_IMM);
4205 TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
4206 TmpInst.addOperand(Inst.getOperand(4)); // Rt
4207 TmpInst.addOperand(Inst.getOperand(1)); // addrmode_imm12
4208 TmpInst.addOperand(MCOperand::CreateImm(-4));
4209 TmpInst.addOperand(Inst.getOperand(2)); // CondCode
4210 TmpInst.addOperand(Inst.getOperand(3));
4215 // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
4216 // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
4217 // to encoding T2 if <Rd> is specified and encoding T2 is preferred
4218 // to encoding T1 if <Rd> is omitted."
4219 if (Inst.getOperand(3).getImm() < 8 && Operands.size() == 6)
4220 Inst.setOpcode(ARM::tADDi3);
4223 // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
4224 // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
4225 // to encoding T2 if <Rd> is specified and encoding T2 is preferred
4226 // to encoding T1 if <Rd> is omitted."
4227 if (Inst.getOperand(3).getImm() < 8 && Operands.size() == 6)
4228 Inst.setOpcode(ARM::tSUBi3);
4231 // A Thumb conditional branch outside of an IT block is a tBcc.
4232 if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock())
4233 Inst.setOpcode(ARM::tBcc);
4236 // A Thumb2 conditional branch outside of an IT block is a t2Bcc.
4237 if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock())
4238 Inst.setOpcode(ARM::t2Bcc);
4241 // If the conditional is AL or we're in an IT block, we really want t2B.
4242 if (Inst.getOperand(1).getImm() == ARMCC::AL || inITBlock())
4243 Inst.setOpcode(ARM::t2B);
4246 // If the conditional is AL, we really want tB.
4247 if (Inst.getOperand(1).getImm() == ARMCC::AL)
4248 Inst.setOpcode(ARM::tB);
4251 // If the register list contains any high registers, or if the writeback
4252 // doesn't match what tLDMIA can do, we need to use the 32-bit encoding
4253 // instead if we're in Thumb2. Otherwise, this should have generated
4254 // an error in validateInstruction().
4255 unsigned Rn = Inst.getOperand(0).getReg();
4256 bool hasWritebackToken =
4257 (static_cast<ARMOperand*>(Operands[3])->isToken() &&
4258 static_cast<ARMOperand*>(Operands[3])->getToken() == "!");
4259 bool listContainsBase;
4260 if (checkLowRegisterList(Inst, 3, Rn, 0, listContainsBase) ||
4261 (!listContainsBase && !hasWritebackToken) ||
4262 (listContainsBase && hasWritebackToken)) {
4263 // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
4264 assert (isThumbTwo());
4265 Inst.setOpcode(hasWritebackToken ? ARM::t2LDMIA_UPD : ARM::t2LDMIA);
4266 // If we're switching to the updating version, we need to insert
4267 // the writeback tied operand.
4268 if (hasWritebackToken)
4269 Inst.insert(Inst.begin(),
4270 MCOperand::CreateReg(Inst.getOperand(0).getReg()));
4274 case ARM::tSTMIA_UPD: {
4275 // If the register list contains any high registers, we need to use
4276 // the 32-bit encoding instead if we're in Thumb2. Otherwise, this
4277 // should have generated an error in validateInstruction().
4278 unsigned Rn = Inst.getOperand(0).getReg();
4279 bool listContainsBase;
4280 if (checkLowRegisterList(Inst, 4, Rn, 0, listContainsBase)) {
4281 // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
4282 assert (isThumbTwo());
4283 Inst.setOpcode(ARM::t2STMIA_UPD);
4288 // If we can use the 16-bit encoding and the user didn't explicitly
4289 // request the 32-bit variant, transform it here.
4290 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
4291 Inst.getOperand(1).getImm() <= 255 &&
4292 ((!inITBlock() && Inst.getOperand(2).getImm() == ARMCC::AL &&
4293 Inst.getOperand(4).getReg() == ARM::CPSR) ||
4294 (inITBlock() && Inst.getOperand(4).getReg() == 0)) &&
4295 (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
4296 static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
4297 // The operands aren't in the same order for tMOVi8...
4299 TmpInst.setOpcode(ARM::tMOVi8);
4300 TmpInst.addOperand(Inst.getOperand(0));
4301 TmpInst.addOperand(Inst.getOperand(4));
4302 TmpInst.addOperand(Inst.getOperand(1));
4303 TmpInst.addOperand(Inst.getOperand(2));
4304 TmpInst.addOperand(Inst.getOperand(3));
4310 // If we can use the 16-bit encoding and the user didn't explicitly
4311 // request the 32-bit variant, transform it here.
4312 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
4313 isARMLowRegister(Inst.getOperand(1).getReg()) &&
4314 Inst.getOperand(2).getImm() == ARMCC::AL &&
4315 Inst.getOperand(4).getReg() == ARM::CPSR &&
4316 (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
4317 static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
4318 // The operands aren't the same for tMOV[S]r... (no cc_out)
4320 TmpInst.setOpcode(Inst.getOperand(4).getReg() ? ARM::tMOVSr : ARM::tMOVr);
4321 TmpInst.addOperand(Inst.getOperand(0));
4322 TmpInst.addOperand(Inst.getOperand(1));
4323 TmpInst.addOperand(Inst.getOperand(2));
4324 TmpInst.addOperand(Inst.getOperand(3));
4333 // If we can use the 16-bit encoding and the user didn't explicitly
4334 // request the 32-bit variant, transform it here.
4335 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
4336 isARMLowRegister(Inst.getOperand(1).getReg()) &&
4337 Inst.getOperand(2).getImm() == 0 &&
4338 (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
4339 static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
4341 switch (Inst.getOpcode()) {
4342 default: llvm_unreachable("Illegal opcode!");
4343 case ARM::t2SXTH: NewOpc = ARM::tSXTH; break;
4344 case ARM::t2SXTB: NewOpc = ARM::tSXTB; break;
4345 case ARM::t2UXTH: NewOpc = ARM::tUXTH; break;
4346 case ARM::t2UXTB: NewOpc = ARM::tUXTB; break;
4348 // The operands aren't the same for thumb1 (no rotate operand).
4350 TmpInst.setOpcode(NewOpc);
4351 TmpInst.addOperand(Inst.getOperand(0));
4352 TmpInst.addOperand(Inst.getOperand(1));
4353 TmpInst.addOperand(Inst.getOperand(3));
4354 TmpInst.addOperand(Inst.getOperand(4));
4360 // The mask bits for all but the first condition are represented as
4361 // the low bit of the condition code value implies 't'. We currently
4362 // always have 1 implies 't', so XOR toggle the bits if the low bit
4363 // of the condition code is zero. The encoding also expects the low
4364 // bit of the condition to be encoded as bit 4 of the mask operand,
4365 // so mask that in if needed
4366 MCOperand &MO = Inst.getOperand(1);
4367 unsigned Mask = MO.getImm();
4368 unsigned OrigMask = Mask;
4369 unsigned TZ = CountTrailingZeros_32(Mask);
4370 if ((Inst.getOperand(0).getImm() & 1) == 0) {
4371 assert(Mask && TZ <= 3 && "illegal IT mask value!");
4372 for (unsigned i = 3; i != TZ; --i)
4378 // Set up the IT block state according to the IT instruction we just
4380 assert(!inITBlock() && "nested IT blocks?!");
4381 ITState.Cond = ARMCC::CondCodes(Inst.getOperand(0).getImm());
4382 ITState.Mask = OrigMask; // Use the original mask, not the updated one.
4383 ITState.CurPosition = 0;
4384 ITState.FirstCond = true;
4390 unsigned ARMAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
4391 // 16-bit thumb arithmetic instructions either require or preclude the 'S'
4392 // suffix depending on whether they're in an IT block or not.
4393 unsigned Opc = Inst.getOpcode();
4394 MCInstrDesc &MCID = getInstDesc(Opc);
4395 if (MCID.TSFlags & ARMII::ThumbArithFlagSetting) {
4396 assert(MCID.hasOptionalDef() &&
4397 "optionally flag setting instruction missing optional def operand");
4398 assert(MCID.NumOperands == Inst.getNumOperands() &&
4399 "operand count mismatch!");
4400 // Find the optional-def operand (cc_out).
4403 !MCID.OpInfo[OpNo].isOptionalDef() && OpNo < MCID.NumOperands;
4406 // If we're parsing Thumb1, reject it completely.
4407 if (isThumbOne() && Inst.getOperand(OpNo).getReg() != ARM::CPSR)
4408 return Match_MnemonicFail;
4409 // If we're parsing Thumb2, which form is legal depends on whether we're
4411 if (isThumbTwo() && Inst.getOperand(OpNo).getReg() != ARM::CPSR &&
4413 return Match_RequiresITBlock;
4414 if (isThumbTwo() && Inst.getOperand(OpNo).getReg() == ARM::CPSR &&
4416 return Match_RequiresNotITBlock;
4418 // Some high-register supporting Thumb1 encodings only allow both registers
4419 // to be from r0-r7 when in Thumb2.
4420 else if (Opc == ARM::tADDhirr && isThumbOne() &&
4421 isARMLowRegister(Inst.getOperand(1).getReg()) &&
4422 isARMLowRegister(Inst.getOperand(2).getReg()))
4423 return Match_RequiresThumb2;
4424 // Others only require ARMv6 or later.
4425 else if (Opc == ARM::tMOVr && isThumbOne() && !hasV6Ops() &&
4426 isARMLowRegister(Inst.getOperand(0).getReg()) &&
4427 isARMLowRegister(Inst.getOperand(1).getReg()))
4428 return Match_RequiresV6;
4429 return Match_Success;
4433 MatchAndEmitInstruction(SMLoc IDLoc,
4434 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
4438 unsigned MatchResult;
4439 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
4440 switch (MatchResult) {
4443 // Context sensitive operand constraints aren't handled by the matcher,
4444 // so check them here.
4445 if (validateInstruction(Inst, Operands)) {
4446 // Still progress the IT block, otherwise one wrong condition causes
4447 // nasty cascading errors.
4448 forwardITPosition();
4452 // Some instructions need post-processing to, for example, tweak which
4453 // encoding is selected.
4454 processInstruction(Inst, Operands);
4456 // Only move forward at the very end so that everything in validate
4457 // and process gets a consistent answer about whether we're in an IT
4459 forwardITPosition();
4461 Out.EmitInstruction(Inst);
4463 case Match_MissingFeature:
4464 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
4466 case Match_InvalidOperand: {
4467 SMLoc ErrorLoc = IDLoc;
4468 if (ErrorInfo != ~0U) {
4469 if (ErrorInfo >= Operands.size())
4470 return Error(IDLoc, "too few operands for instruction");
4472 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
4473 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
4476 return Error(ErrorLoc, "invalid operand for instruction");
4478 case Match_MnemonicFail:
4479 return Error(IDLoc, "invalid instruction");
4480 case Match_ConversionFail:
4481 // The converter function will have already emited a diagnostic.
4483 case Match_RequiresNotITBlock:
4484 return Error(IDLoc, "flag setting instruction only valid outside IT block");
4485 case Match_RequiresITBlock:
4486 return Error(IDLoc, "instruction only valid inside IT block");
4487 case Match_RequiresV6:
4488 return Error(IDLoc, "instruction variant requires ARMv6 or later");
4489 case Match_RequiresThumb2:
4490 return Error(IDLoc, "instruction variant requires Thumb2");
4493 llvm_unreachable("Implement any new match types added!");
4497 /// parseDirective parses the arm specific directives
4498 bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
4499 StringRef IDVal = DirectiveID.getIdentifier();
4500 if (IDVal == ".word")
4501 return parseDirectiveWord(4, DirectiveID.getLoc());
4502 else if (IDVal == ".thumb")
4503 return parseDirectiveThumb(DirectiveID.getLoc());
4504 else if (IDVal == ".thumb_func")
4505 return parseDirectiveThumbFunc(DirectiveID.getLoc());
4506 else if (IDVal == ".code")
4507 return parseDirectiveCode(DirectiveID.getLoc());
4508 else if (IDVal == ".syntax")
4509 return parseDirectiveSyntax(DirectiveID.getLoc());
4513 /// parseDirectiveWord
4514 /// ::= .word [ expression (, expression)* ]
4515 bool ARMAsmParser::parseDirectiveWord(unsigned Size, SMLoc L) {
4516 if (getLexer().isNot(AsmToken::EndOfStatement)) {
4518 const MCExpr *Value;
4519 if (getParser().ParseExpression(Value))
4522 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
4524 if (getLexer().is(AsmToken::EndOfStatement))
4527 // FIXME: Improve diagnostic.
4528 if (getLexer().isNot(AsmToken::Comma))
4529 return Error(L, "unexpected token in directive");
4538 /// parseDirectiveThumb
4540 bool ARMAsmParser::parseDirectiveThumb(SMLoc L) {
4541 if (getLexer().isNot(AsmToken::EndOfStatement))
4542 return Error(L, "unexpected token in directive");
4545 // TODO: set thumb mode
4546 // TODO: tell the MC streamer the mode
4547 // getParser().getStreamer().Emit???();
4551 /// parseDirectiveThumbFunc
4552 /// ::= .thumbfunc symbol_name
4553 bool ARMAsmParser::parseDirectiveThumbFunc(SMLoc L) {
4554 const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
4555 bool isMachO = MAI.hasSubsectionsViaSymbols();
4558 // Darwin asm has function name after .thumb_func direction
4561 const AsmToken &Tok = Parser.getTok();
4562 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
4563 return Error(L, "unexpected token in .thumb_func directive");
4564 Name = Tok.getString();
4565 Parser.Lex(); // Consume the identifier token.
4568 if (getLexer().isNot(AsmToken::EndOfStatement))
4569 return Error(L, "unexpected token in directive");
4572 // FIXME: assuming function name will be the line following .thumb_func
4574 Name = Parser.getTok().getString();
4577 // Mark symbol as a thumb symbol.
4578 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
4579 getParser().getStreamer().EmitThumbFunc(Func);
4583 /// parseDirectiveSyntax
4584 /// ::= .syntax unified | divided
4585 bool ARMAsmParser::parseDirectiveSyntax(SMLoc L) {
4586 const AsmToken &Tok = Parser.getTok();
4587 if (Tok.isNot(AsmToken::Identifier))
4588 return Error(L, "unexpected token in .syntax directive");
4589 StringRef Mode = Tok.getString();
4590 if (Mode == "unified" || Mode == "UNIFIED")
4592 else if (Mode == "divided" || Mode == "DIVIDED")
4593 return Error(L, "'.syntax divided' arm asssembly not supported");
4595 return Error(L, "unrecognized syntax mode in .syntax directive");
4597 if (getLexer().isNot(AsmToken::EndOfStatement))
4598 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
4601 // TODO tell the MC streamer the mode
4602 // getParser().getStreamer().Emit???();
4606 /// parseDirectiveCode
4607 /// ::= .code 16 | 32
4608 bool ARMAsmParser::parseDirectiveCode(SMLoc L) {
4609 const AsmToken &Tok = Parser.getTok();
4610 if (Tok.isNot(AsmToken::Integer))
4611 return Error(L, "unexpected token in .code directive");
4612 int64_t Val = Parser.getTok().getIntVal();
4618 return Error(L, "invalid operand to .code directive");
4620 if (getLexer().isNot(AsmToken::EndOfStatement))
4621 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
4627 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
4631 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
4637 extern "C" void LLVMInitializeARMAsmLexer();
4639 /// Force static initialization.
4640 extern "C" void LLVMInitializeARMAsmParser() {
4641 RegisterMCAsmParser<ARMAsmParser> X(TheARMTarget);
4642 RegisterMCAsmParser<ARMAsmParser> Y(TheThumbTarget);
4643 LLVMInitializeARMAsmLexer();
4646 #define GET_REGISTER_MATCHER
4647 #define GET_MATCHER_IMPLEMENTATION
4648 #include "ARMGenAsmMatcher.inc"