1 //===-- ARMAsmParser.cpp - Parse ARM assembly to MCInst instructions ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "MCTargetDesc/ARMBaseInfo.h"
11 #include "MCTargetDesc/ARMAddressingModes.h"
12 #include "MCTargetDesc/ARMMCExpr.h"
13 #include "llvm/MC/MCParser/MCAsmLexer.h"
14 #include "llvm/MC/MCParser/MCAsmParser.h"
15 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
16 #include "llvm/MC/MCAsmInfo.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/MC/MCStreamer.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/MC/MCInstrDesc.h"
22 #include "llvm/MC/MCRegisterInfo.h"
23 #include "llvm/MC/MCSubtargetInfo.h"
24 #include "llvm/MC/MCTargetAsmParser.h"
25 #include "llvm/Support/MathExtras.h"
26 #include "llvm/Support/SourceMgr.h"
27 #include "llvm/Support/TargetRegistry.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/ADT/BitVector.h"
30 #include "llvm/ADT/OwningPtr.h"
31 #include "llvm/ADT/STLExtras.h"
32 #include "llvm/ADT/SmallVector.h"
33 #include "llvm/ADT/StringExtras.h"
34 #include "llvm/ADT/StringSwitch.h"
35 #include "llvm/ADT/Twine.h"
43 class ARMAsmParser : public MCTargetAsmParser {
48 ARMCC::CondCodes Cond; // Condition for IT block.
49 unsigned Mask:4; // Condition mask for instructions.
50 // Starting at first 1 (from lsb).
51 // '1' condition as indicated in IT.
52 // '0' inverse of condition (else).
53 // Count of instructions in IT block is
54 // 4 - trailingzeroes(mask)
56 bool FirstCond; // Explicit flag for when we're parsing the
57 // First instruction in the IT block. It's
58 // implied in the mask, so needs special
61 unsigned CurPosition; // Current position in parsing of IT
62 // block. In range [0,3]. Initialized
63 // according to count of instructions in block.
64 // ~0U if no active IT block.
66 bool inITBlock() { return ITState.CurPosition != ~0U;}
67 void forwardITPosition() {
68 if (!inITBlock()) return;
69 // Move to the next instruction in the IT block, if there is one. If not,
70 // mark the block as done.
71 unsigned TZ = CountTrailingZeros_32(ITState.Mask);
72 if (++ITState.CurPosition == 5 - TZ)
73 ITState.CurPosition = ~0U; // Done with the IT block after this.
77 MCAsmParser &getParser() const { return Parser; }
78 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
80 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
81 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
83 int tryParseRegister();
84 bool tryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
85 int tryParseShiftRegister(SmallVectorImpl<MCParsedAsmOperand*> &);
86 bool parseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
87 bool parseMemory(SmallVectorImpl<MCParsedAsmOperand*> &);
88 bool parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, StringRef Mnemonic);
89 bool parsePrefix(ARMMCExpr::VariantKind &RefKind);
90 bool parseMemRegOffsetShift(ARM_AM::ShiftOpc &ShiftType,
91 unsigned &ShiftAmount);
92 bool parseDirectiveWord(unsigned Size, SMLoc L);
93 bool parseDirectiveThumb(SMLoc L);
94 bool parseDirectiveThumbFunc(SMLoc L);
95 bool parseDirectiveCode(SMLoc L);
96 bool parseDirectiveSyntax(SMLoc L);
98 StringRef splitMnemonic(StringRef Mnemonic, unsigned &PredicationCode,
99 bool &CarrySetting, unsigned &ProcessorIMod,
101 void getMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
102 bool &CanAcceptPredicationCode);
104 bool isThumb() const {
105 // FIXME: Can tablegen auto-generate this?
106 return (STI.getFeatureBits() & ARM::ModeThumb) != 0;
108 bool isThumbOne() const {
109 return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2) == 0;
111 bool isThumbTwo() const {
112 return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2);
114 bool hasV6Ops() const {
115 return STI.getFeatureBits() & ARM::HasV6Ops;
117 bool hasV7Ops() const {
118 return STI.getFeatureBits() & ARM::HasV7Ops;
121 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(ARM::ModeThumb));
122 setAvailableFeatures(FB);
124 bool isMClass() const {
125 return STI.getFeatureBits() & ARM::FeatureMClass;
128 /// @name Auto-generated Match Functions
131 #define GET_ASSEMBLER_HEADER
132 #include "ARMGenAsmMatcher.inc"
136 OperandMatchResultTy parseITCondCode(SmallVectorImpl<MCParsedAsmOperand*>&);
137 OperandMatchResultTy parseCoprocNumOperand(
138 SmallVectorImpl<MCParsedAsmOperand*>&);
139 OperandMatchResultTy parseCoprocRegOperand(
140 SmallVectorImpl<MCParsedAsmOperand*>&);
141 OperandMatchResultTy parseCoprocOptionOperand(
142 SmallVectorImpl<MCParsedAsmOperand*>&);
143 OperandMatchResultTy parseMemBarrierOptOperand(
144 SmallVectorImpl<MCParsedAsmOperand*>&);
145 OperandMatchResultTy parseProcIFlagsOperand(
146 SmallVectorImpl<MCParsedAsmOperand*>&);
147 OperandMatchResultTy parseMSRMaskOperand(
148 SmallVectorImpl<MCParsedAsmOperand*>&);
149 OperandMatchResultTy parsePKHImm(SmallVectorImpl<MCParsedAsmOperand*> &O,
150 StringRef Op, int Low, int High);
151 OperandMatchResultTy parsePKHLSLImm(SmallVectorImpl<MCParsedAsmOperand*> &O) {
152 return parsePKHImm(O, "lsl", 0, 31);
154 OperandMatchResultTy parsePKHASRImm(SmallVectorImpl<MCParsedAsmOperand*> &O) {
155 return parsePKHImm(O, "asr", 1, 32);
157 OperandMatchResultTy parseSetEndImm(SmallVectorImpl<MCParsedAsmOperand*>&);
158 OperandMatchResultTy parseShifterImm(SmallVectorImpl<MCParsedAsmOperand*>&);
159 OperandMatchResultTy parseRotImm(SmallVectorImpl<MCParsedAsmOperand*>&);
160 OperandMatchResultTy parseBitfield(SmallVectorImpl<MCParsedAsmOperand*>&);
161 OperandMatchResultTy parsePostIdxReg(SmallVectorImpl<MCParsedAsmOperand*>&);
162 OperandMatchResultTy parseAM3Offset(SmallVectorImpl<MCParsedAsmOperand*>&);
163 OperandMatchResultTy parseFPImm(SmallVectorImpl<MCParsedAsmOperand*>&);
164 OperandMatchResultTy parseVectorList(SmallVectorImpl<MCParsedAsmOperand*>&);
166 // Asm Match Converter Methods
167 bool cvtT2LdrdPre(MCInst &Inst, unsigned Opcode,
168 const SmallVectorImpl<MCParsedAsmOperand*> &);
169 bool cvtT2StrdPre(MCInst &Inst, unsigned Opcode,
170 const SmallVectorImpl<MCParsedAsmOperand*> &);
171 bool cvtLdWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
172 const SmallVectorImpl<MCParsedAsmOperand*> &);
173 bool cvtStWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
174 const SmallVectorImpl<MCParsedAsmOperand*> &);
175 bool cvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
176 const SmallVectorImpl<MCParsedAsmOperand*> &);
177 bool cvtLdWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
178 const SmallVectorImpl<MCParsedAsmOperand*> &);
179 bool cvtStWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
180 const SmallVectorImpl<MCParsedAsmOperand*> &);
181 bool cvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
182 const SmallVectorImpl<MCParsedAsmOperand*> &);
183 bool cvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
184 const SmallVectorImpl<MCParsedAsmOperand*> &);
185 bool cvtLdExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
186 const SmallVectorImpl<MCParsedAsmOperand*> &);
187 bool cvtLdExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
188 const SmallVectorImpl<MCParsedAsmOperand*> &);
189 bool cvtStExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
190 const SmallVectorImpl<MCParsedAsmOperand*> &);
191 bool cvtStExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
192 const SmallVectorImpl<MCParsedAsmOperand*> &);
193 bool cvtLdrdPre(MCInst &Inst, unsigned Opcode,
194 const SmallVectorImpl<MCParsedAsmOperand*> &);
195 bool cvtStrdPre(MCInst &Inst, unsigned Opcode,
196 const SmallVectorImpl<MCParsedAsmOperand*> &);
197 bool cvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
198 const SmallVectorImpl<MCParsedAsmOperand*> &);
199 bool cvtThumbMultiply(MCInst &Inst, unsigned Opcode,
200 const SmallVectorImpl<MCParsedAsmOperand*> &);
201 bool cvtVLDwbFixed(MCInst &Inst, unsigned Opcode,
202 const SmallVectorImpl<MCParsedAsmOperand*> &);
203 bool cvtVLDwbRegister(MCInst &Inst, unsigned Opcode,
204 const SmallVectorImpl<MCParsedAsmOperand*> &);
206 bool validateInstruction(MCInst &Inst,
207 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
208 void processInstruction(MCInst &Inst,
209 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
210 bool shouldOmitCCOutOperand(StringRef Mnemonic,
211 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
214 enum ARMMatchResultTy {
215 Match_RequiresITBlock = FIRST_TARGET_MATCH_RESULT_TY,
216 Match_RequiresNotITBlock,
221 ARMAsmParser(MCSubtargetInfo &_STI, MCAsmParser &_Parser)
222 : MCTargetAsmParser(), STI(_STI), Parser(_Parser) {
223 MCAsmParserExtension::Initialize(_Parser);
225 // Initialize the set of available features.
226 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
228 // Not in an ITBlock to start with.
229 ITState.CurPosition = ~0U;
232 // Implementation of the MCTargetAsmParser interface:
233 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
234 bool ParseInstruction(StringRef Name, SMLoc NameLoc,
235 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
236 bool ParseDirective(AsmToken DirectiveID);
238 unsigned checkTargetMatchPredicate(MCInst &Inst);
240 bool MatchAndEmitInstruction(SMLoc IDLoc,
241 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
244 } // end anonymous namespace
248 /// ARMOperand - Instances of this class represent a parsed ARM machine
250 class ARMOperand : public MCParsedAsmOperand {
275 k_BitfieldDescriptor,
279 SMLoc StartLoc, EndLoc;
280 SmallVector<unsigned, 8> Registers;
284 ARMCC::CondCodes Val;
304 ARM_PROC::IFlags Val;
320 // A vector register list is a sequential list of 1 to 4 registers.
335 unsigned Val; // encoded 8-bit representation
338 /// Combined record for all forms of ARM address expressions.
341 // Offset is in OffsetReg or OffsetImm. If both are zero, no offset
343 const MCConstantExpr *OffsetImm; // Offset immediate value
344 unsigned OffsetRegNum; // Offset register num, when OffsetImm == NULL
345 ARM_AM::ShiftOpc ShiftType; // Shift type for OffsetReg
346 unsigned ShiftImm; // shift for OffsetReg.
347 unsigned Alignment; // 0 = no alignment specified
348 // n = alignment in bytes (8, 16, or 32)
349 unsigned isNegative : 1; // Negated OffsetReg? (~'U' bit)
355 ARM_AM::ShiftOpc ShiftTy;
364 ARM_AM::ShiftOpc ShiftTy;
370 ARM_AM::ShiftOpc ShiftTy;
383 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
385 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
387 StartLoc = o.StartLoc;
404 case k_DPRRegisterList:
405 case k_SPRRegisterList:
406 Registers = o.Registers;
409 VectorList = o.VectorList;
416 CoprocOption = o.CoprocOption;
424 case k_MemBarrierOpt:
430 case k_PostIndexRegister:
431 PostIdxReg = o.PostIdxReg;
439 case k_ShifterImmediate:
440 ShifterImm = o.ShifterImm;
442 case k_ShiftedRegister:
443 RegShiftedReg = o.RegShiftedReg;
445 case k_ShiftedImmediate:
446 RegShiftedImm = o.RegShiftedImm;
448 case k_RotateImmediate:
451 case k_BitfieldDescriptor:
452 Bitfield = o.Bitfield;
455 VectorIndex = o.VectorIndex;
460 /// getStartLoc - Get the location of the first token of this operand.
461 SMLoc getStartLoc() const { return StartLoc; }
462 /// getEndLoc - Get the location of the last token of this operand.
463 SMLoc getEndLoc() const { return EndLoc; }
465 ARMCC::CondCodes getCondCode() const {
466 assert(Kind == k_CondCode && "Invalid access!");
470 unsigned getCoproc() const {
471 assert((Kind == k_CoprocNum || Kind == k_CoprocReg) && "Invalid access!");
475 StringRef getToken() const {
476 assert(Kind == k_Token && "Invalid access!");
477 return StringRef(Tok.Data, Tok.Length);
480 unsigned getReg() const {
481 assert((Kind == k_Register || Kind == k_CCOut) && "Invalid access!");
485 const SmallVectorImpl<unsigned> &getRegList() const {
486 assert((Kind == k_RegisterList || Kind == k_DPRRegisterList ||
487 Kind == k_SPRRegisterList) && "Invalid access!");
491 const MCExpr *getImm() const {
492 assert(Kind == k_Immediate && "Invalid access!");
496 unsigned getFPImm() const {
497 assert(Kind == k_FPImmediate && "Invalid access!");
501 unsigned getVectorIndex() const {
502 assert(Kind == k_VectorIndex && "Invalid access!");
503 return VectorIndex.Val;
506 ARM_MB::MemBOpt getMemBarrierOpt() const {
507 assert(Kind == k_MemBarrierOpt && "Invalid access!");
511 ARM_PROC::IFlags getProcIFlags() const {
512 assert(Kind == k_ProcIFlags && "Invalid access!");
516 unsigned getMSRMask() const {
517 assert(Kind == k_MSRMask && "Invalid access!");
521 bool isCoprocNum() const { return Kind == k_CoprocNum; }
522 bool isCoprocReg() const { return Kind == k_CoprocReg; }
523 bool isCoprocOption() const { return Kind == k_CoprocOption; }
524 bool isCondCode() const { return Kind == k_CondCode; }
525 bool isCCOut() const { return Kind == k_CCOut; }
526 bool isITMask() const { return Kind == k_ITCondMask; }
527 bool isITCondCode() const { return Kind == k_CondCode; }
528 bool isImm() const { return Kind == k_Immediate; }
529 bool isFPImm() const { return Kind == k_FPImmediate; }
530 bool isImm8s4() const {
531 if (Kind != k_Immediate)
533 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
534 if (!CE) return false;
535 int64_t Value = CE->getValue();
536 return ((Value & 3) == 0) && Value >= -1020 && Value <= 1020;
538 bool isImm0_1020s4() const {
539 if (Kind != k_Immediate)
541 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
542 if (!CE) return false;
543 int64_t Value = CE->getValue();
544 return ((Value & 3) == 0) && Value >= 0 && Value <= 1020;
546 bool isImm0_508s4() const {
547 if (Kind != k_Immediate)
549 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
550 if (!CE) return false;
551 int64_t Value = CE->getValue();
552 return ((Value & 3) == 0) && Value >= 0 && Value <= 508;
554 bool isImm0_255() const {
555 if (Kind != k_Immediate)
557 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
558 if (!CE) return false;
559 int64_t Value = CE->getValue();
560 return Value >= 0 && Value < 256;
562 bool isImm0_7() const {
563 if (Kind != k_Immediate)
565 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
566 if (!CE) return false;
567 int64_t Value = CE->getValue();
568 return Value >= 0 && Value < 8;
570 bool isImm0_15() const {
571 if (Kind != k_Immediate)
573 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
574 if (!CE) return false;
575 int64_t Value = CE->getValue();
576 return Value >= 0 && Value < 16;
578 bool isImm0_31() const {
579 if (Kind != k_Immediate)
581 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
582 if (!CE) return false;
583 int64_t Value = CE->getValue();
584 return Value >= 0 && Value < 32;
586 bool isImm1_16() const {
587 if (Kind != k_Immediate)
589 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
590 if (!CE) return false;
591 int64_t Value = CE->getValue();
592 return Value > 0 && Value < 17;
594 bool isImm1_32() const {
595 if (Kind != k_Immediate)
597 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
598 if (!CE) return false;
599 int64_t Value = CE->getValue();
600 return Value > 0 && Value < 33;
602 bool isImm0_65535() const {
603 if (Kind != k_Immediate)
605 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
606 if (!CE) return false;
607 int64_t Value = CE->getValue();
608 return Value >= 0 && Value < 65536;
610 bool isImm0_65535Expr() const {
611 if (Kind != k_Immediate)
613 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
614 // If it's not a constant expression, it'll generate a fixup and be
616 if (!CE) return true;
617 int64_t Value = CE->getValue();
618 return Value >= 0 && Value < 65536;
620 bool isImm24bit() const {
621 if (Kind != k_Immediate)
623 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
624 if (!CE) return false;
625 int64_t Value = CE->getValue();
626 return Value >= 0 && Value <= 0xffffff;
628 bool isImmThumbSR() const {
629 if (Kind != k_Immediate)
631 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
632 if (!CE) return false;
633 int64_t Value = CE->getValue();
634 return Value > 0 && Value < 33;
636 bool isPKHLSLImm() const {
637 if (Kind != k_Immediate)
639 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
640 if (!CE) return false;
641 int64_t Value = CE->getValue();
642 return Value >= 0 && Value < 32;
644 bool isPKHASRImm() const {
645 if (Kind != k_Immediate)
647 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
648 if (!CE) return false;
649 int64_t Value = CE->getValue();
650 return Value > 0 && Value <= 32;
652 bool isARMSOImm() const {
653 if (Kind != k_Immediate)
655 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
656 if (!CE) return false;
657 int64_t Value = CE->getValue();
658 return ARM_AM::getSOImmVal(Value) != -1;
660 bool isT2SOImm() const {
661 if (Kind != k_Immediate)
663 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
664 if (!CE) return false;
665 int64_t Value = CE->getValue();
666 return ARM_AM::getT2SOImmVal(Value) != -1;
668 bool isT2SOImmNot() const {
669 if (Kind != k_Immediate)
671 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
672 if (!CE) return false;
673 int64_t Value = CE->getValue();
674 return ARM_AM::getT2SOImmVal(~Value) != -1;
676 bool isSetEndImm() const {
677 if (Kind != k_Immediate)
679 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
680 if (!CE) return false;
681 int64_t Value = CE->getValue();
682 return Value == 1 || Value == 0;
684 bool isReg() const { return Kind == k_Register; }
685 bool isRegList() const { return Kind == k_RegisterList; }
686 bool isDPRRegList() const { return Kind == k_DPRRegisterList; }
687 bool isSPRRegList() const { return Kind == k_SPRRegisterList; }
688 bool isToken() const { return Kind == k_Token; }
689 bool isMemBarrierOpt() const { return Kind == k_MemBarrierOpt; }
690 bool isMemory() const { return Kind == k_Memory; }
691 bool isShifterImm() const { return Kind == k_ShifterImmediate; }
692 bool isRegShiftedReg() const { return Kind == k_ShiftedRegister; }
693 bool isRegShiftedImm() const { return Kind == k_ShiftedImmediate; }
694 bool isRotImm() const { return Kind == k_RotateImmediate; }
695 bool isBitfield() const { return Kind == k_BitfieldDescriptor; }
696 bool isPostIdxRegShifted() const { return Kind == k_PostIndexRegister; }
697 bool isPostIdxReg() const {
698 return Kind == k_PostIndexRegister && PostIdxReg.ShiftTy == ARM_AM::no_shift;
700 bool isMemNoOffset(bool alignOK = false) const {
703 // No offset of any kind.
704 return Memory.OffsetRegNum == 0 && Memory.OffsetImm == 0 &&
705 (alignOK || Memory.Alignment == 0);
707 bool isAlignedMemory() const {
708 return isMemNoOffset(true);
710 bool isAddrMode2() const {
711 if (!isMemory() || Memory.Alignment != 0) return false;
712 // Check for register offset.
713 if (Memory.OffsetRegNum) return true;
714 // Immediate offset in range [-4095, 4095].
715 if (!Memory.OffsetImm) return true;
716 int64_t Val = Memory.OffsetImm->getValue();
717 return Val > -4096 && Val < 4096;
719 bool isAM2OffsetImm() const {
720 if (Kind != k_Immediate)
722 // Immediate offset in range [-4095, 4095].
723 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
724 if (!CE) return false;
725 int64_t Val = CE->getValue();
726 return Val > -4096 && Val < 4096;
728 bool isAddrMode3() const {
729 if (!isMemory() || Memory.Alignment != 0) return false;
730 // No shifts are legal for AM3.
731 if (Memory.ShiftType != ARM_AM::no_shift) return false;
732 // Check for register offset.
733 if (Memory.OffsetRegNum) return true;
734 // Immediate offset in range [-255, 255].
735 if (!Memory.OffsetImm) return true;
736 int64_t Val = Memory.OffsetImm->getValue();
737 return Val > -256 && Val < 256;
739 bool isAM3Offset() const {
740 if (Kind != k_Immediate && Kind != k_PostIndexRegister)
742 if (Kind == k_PostIndexRegister)
743 return PostIdxReg.ShiftTy == ARM_AM::no_shift;
744 // Immediate offset in range [-255, 255].
745 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
746 if (!CE) return false;
747 int64_t Val = CE->getValue();
748 // Special case, #-0 is INT32_MIN.
749 return (Val > -256 && Val < 256) || Val == INT32_MIN;
751 bool isAddrMode5() const {
752 if (!isMemory() || Memory.Alignment != 0) return false;
753 // Check for register offset.
754 if (Memory.OffsetRegNum) return false;
755 // Immediate offset in range [-1020, 1020] and a multiple of 4.
756 if (!Memory.OffsetImm) return true;
757 int64_t Val = Memory.OffsetImm->getValue();
758 return (Val >= -1020 && Val <= 1020 && ((Val & 3) == 0)) ||
761 bool isMemTBB() const {
762 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
763 Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
767 bool isMemTBH() const {
768 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
769 Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm != 1 ||
770 Memory.Alignment != 0 )
774 bool isMemRegOffset() const {
775 if (!isMemory() || !Memory.OffsetRegNum || Memory.Alignment != 0)
779 bool isT2MemRegOffset() const {
780 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
781 Memory.Alignment != 0)
783 // Only lsl #{0, 1, 2, 3} allowed.
784 if (Memory.ShiftType == ARM_AM::no_shift)
786 if (Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm > 3)
790 bool isMemThumbRR() const {
791 // Thumb reg+reg addressing is simple. Just two registers, a base and
792 // an offset. No shifts, negations or any other complicating factors.
793 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
794 Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
796 return isARMLowRegister(Memory.BaseRegNum) &&
797 (!Memory.OffsetRegNum || isARMLowRegister(Memory.OffsetRegNum));
799 bool isMemThumbRIs4() const {
800 if (!isMemory() || Memory.OffsetRegNum != 0 ||
801 !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
803 // Immediate offset, multiple of 4 in range [0, 124].
804 if (!Memory.OffsetImm) return true;
805 int64_t Val = Memory.OffsetImm->getValue();
806 return Val >= 0 && Val <= 124 && (Val % 4) == 0;
808 bool isMemThumbRIs2() const {
809 if (!isMemory() || Memory.OffsetRegNum != 0 ||
810 !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
812 // Immediate offset, multiple of 4 in range [0, 62].
813 if (!Memory.OffsetImm) return true;
814 int64_t Val = Memory.OffsetImm->getValue();
815 return Val >= 0 && Val <= 62 && (Val % 2) == 0;
817 bool isMemThumbRIs1() const {
818 if (!isMemory() || Memory.OffsetRegNum != 0 ||
819 !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
821 // Immediate offset in range [0, 31].
822 if (!Memory.OffsetImm) return true;
823 int64_t Val = Memory.OffsetImm->getValue();
824 return Val >= 0 && Val <= 31;
826 bool isMemThumbSPI() const {
827 if (!isMemory() || Memory.OffsetRegNum != 0 ||
828 Memory.BaseRegNum != ARM::SP || Memory.Alignment != 0)
830 // Immediate offset, multiple of 4 in range [0, 1020].
831 if (!Memory.OffsetImm) return true;
832 int64_t Val = Memory.OffsetImm->getValue();
833 return Val >= 0 && Val <= 1020 && (Val % 4) == 0;
835 bool isMemImm8s4Offset() const {
836 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
838 // Immediate offset a multiple of 4 in range [-1020, 1020].
839 if (!Memory.OffsetImm) return true;
840 int64_t Val = Memory.OffsetImm->getValue();
841 return Val >= -1020 && Val <= 1020 && (Val & 3) == 0;
843 bool isMemImm0_1020s4Offset() const {
844 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
846 // Immediate offset a multiple of 4 in range [0, 1020].
847 if (!Memory.OffsetImm) return true;
848 int64_t Val = Memory.OffsetImm->getValue();
849 return Val >= 0 && Val <= 1020 && (Val & 3) == 0;
851 bool isMemImm8Offset() const {
852 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
854 // Immediate offset in range [-255, 255].
855 if (!Memory.OffsetImm) return true;
856 int64_t Val = Memory.OffsetImm->getValue();
857 return (Val == INT32_MIN) || (Val > -256 && Val < 256);
859 bool isMemPosImm8Offset() const {
860 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
862 // Immediate offset in range [0, 255].
863 if (!Memory.OffsetImm) return true;
864 int64_t Val = Memory.OffsetImm->getValue();
865 return Val >= 0 && Val < 256;
867 bool isMemNegImm8Offset() const {
868 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
870 // Immediate offset in range [-255, -1].
871 if (!Memory.OffsetImm) return true;
872 int64_t Val = Memory.OffsetImm->getValue();
873 return Val > -256 && Val < 0;
875 bool isMemUImm12Offset() const {
876 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
878 // Immediate offset in range [0, 4095].
879 if (!Memory.OffsetImm) return true;
880 int64_t Val = Memory.OffsetImm->getValue();
881 return (Val >= 0 && Val < 4096);
883 bool isMemImm12Offset() const {
884 // If we have an immediate that's not a constant, treat it as a label
885 // reference needing a fixup. If it is a constant, it's something else
887 if (Kind == k_Immediate && !isa<MCConstantExpr>(getImm()))
890 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
892 // Immediate offset in range [-4095, 4095].
893 if (!Memory.OffsetImm) return true;
894 int64_t Val = Memory.OffsetImm->getValue();
895 return (Val > -4096 && Val < 4096) || (Val == INT32_MIN);
897 bool isPostIdxImm8() const {
898 if (Kind != k_Immediate)
900 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
901 if (!CE) return false;
902 int64_t Val = CE->getValue();
903 return (Val > -256 && Val < 256) || (Val == INT32_MIN);
905 bool isPostIdxImm8s4() const {
906 if (Kind != k_Immediate)
908 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
909 if (!CE) return false;
910 int64_t Val = CE->getValue();
911 return ((Val & 3) == 0 && Val >= -1020 && Val <= 1020) ||
915 bool isMSRMask() const { return Kind == k_MSRMask; }
916 bool isProcIFlags() const { return Kind == k_ProcIFlags; }
919 bool isVecListOneD() const {
920 if (Kind != k_VectorList) return false;
921 return VectorList.Count == 1;
924 bool isVecListTwoD() const {
925 if (Kind != k_VectorList) return false;
926 return VectorList.Count == 2;
929 bool isVecListThreeD() const {
930 if (Kind != k_VectorList) return false;
931 return VectorList.Count == 3;
934 bool isVecListFourD() const {
935 if (Kind != k_VectorList) return false;
936 return VectorList.Count == 4;
939 bool isVecListTwoQ() const {
940 if (Kind != k_VectorList) return false;
941 //FIXME: We haven't taught the parser to handle by-two register lists
942 // yet, so don't pretend to know one.
943 return VectorList.Count == 2 && false;
946 bool isVectorIndex8() const {
947 if (Kind != k_VectorIndex) return false;
948 return VectorIndex.Val < 8;
950 bool isVectorIndex16() const {
951 if (Kind != k_VectorIndex) return false;
952 return VectorIndex.Val < 4;
954 bool isVectorIndex32() const {
955 if (Kind != k_VectorIndex) return false;
956 return VectorIndex.Val < 2;
959 bool isNEONi8splat() const {
960 if (Kind != k_Immediate)
962 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
963 // Must be a constant.
964 if (!CE) return false;
965 int64_t Value = CE->getValue();
966 // i8 value splatted across 8 bytes. The immediate is just the 8 byte
968 return Value >= 0 && Value < 256;
971 bool isNEONi16splat() const {
972 if (Kind != k_Immediate)
974 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
975 // Must be a constant.
976 if (!CE) return false;
977 int64_t Value = CE->getValue();
978 // i16 value in the range [0,255] or [0x0100, 0xff00]
979 return (Value >= 0 && Value < 256) || (Value >= 0x0100 && Value <= 0xff00);
982 bool isNEONi32splat() const {
983 if (Kind != k_Immediate)
985 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
986 // Must be a constant.
987 if (!CE) return false;
988 int64_t Value = CE->getValue();
989 // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X.
990 return (Value >= 0 && Value < 256) ||
991 (Value >= 0x0100 && Value <= 0xff00) ||
992 (Value >= 0x010000 && Value <= 0xff0000) ||
993 (Value >= 0x01000000 && Value <= 0xff000000);
996 bool isNEONi32vmov() const {
997 if (Kind != k_Immediate)
999 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1000 // Must be a constant.
1001 if (!CE) return false;
1002 int64_t Value = CE->getValue();
1003 // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X,
1004 // for VMOV/VMVN only, 00Xf or 0Xff are also accepted.
1005 return (Value >= 0 && Value < 256) ||
1006 (Value >= 0x0100 && Value <= 0xff00) ||
1007 (Value >= 0x010000 && Value <= 0xff0000) ||
1008 (Value >= 0x01000000 && Value <= 0xff000000) ||
1009 (Value >= 0x01ff && Value <= 0xffff && (Value & 0xff) == 0xff) ||
1010 (Value >= 0x01ffff && Value <= 0xffffff && (Value & 0xffff) == 0xffff);
1013 bool isNEONi64splat() const {
1014 if (Kind != k_Immediate)
1016 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1017 // Must be a constant.
1018 if (!CE) return false;
1019 uint64_t Value = CE->getValue();
1020 // i64 value with each byte being either 0 or 0xff.
1021 for (unsigned i = 0; i < 8; ++i)
1022 if ((Value & 0xff) != 0 && (Value & 0xff) != 0xff) return false;
1026 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
1027 // Add as immediates when possible. Null MCExpr = 0.
1029 Inst.addOperand(MCOperand::CreateImm(0));
1030 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
1031 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
1033 Inst.addOperand(MCOperand::CreateExpr(Expr));
1036 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
1037 assert(N == 2 && "Invalid number of operands!");
1038 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
1039 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
1040 Inst.addOperand(MCOperand::CreateReg(RegNum));
1043 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
1044 assert(N == 1 && "Invalid number of operands!");
1045 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
1048 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
1049 assert(N == 1 && "Invalid number of operands!");
1050 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
1053 void addCoprocOptionOperands(MCInst &Inst, unsigned N) const {
1054 assert(N == 1 && "Invalid number of operands!");
1055 Inst.addOperand(MCOperand::CreateImm(CoprocOption.Val));
1058 void addITMaskOperands(MCInst &Inst, unsigned N) const {
1059 assert(N == 1 && "Invalid number of operands!");
1060 Inst.addOperand(MCOperand::CreateImm(ITMask.Mask));
1063 void addITCondCodeOperands(MCInst &Inst, unsigned N) const {
1064 assert(N == 1 && "Invalid number of operands!");
1065 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
1068 void addCCOutOperands(MCInst &Inst, unsigned N) const {
1069 assert(N == 1 && "Invalid number of operands!");
1070 Inst.addOperand(MCOperand::CreateReg(getReg()));
1073 void addRegOperands(MCInst &Inst, unsigned N) const {
1074 assert(N == 1 && "Invalid number of operands!");
1075 Inst.addOperand(MCOperand::CreateReg(getReg()));
1078 void addRegShiftedRegOperands(MCInst &Inst, unsigned N) const {
1079 assert(N == 3 && "Invalid number of operands!");
1080 assert(isRegShiftedReg() && "addRegShiftedRegOperands() on non RegShiftedReg!");
1081 Inst.addOperand(MCOperand::CreateReg(RegShiftedReg.SrcReg));
1082 Inst.addOperand(MCOperand::CreateReg(RegShiftedReg.ShiftReg));
1083 Inst.addOperand(MCOperand::CreateImm(
1084 ARM_AM::getSORegOpc(RegShiftedReg.ShiftTy, RegShiftedReg.ShiftImm)));
1087 void addRegShiftedImmOperands(MCInst &Inst, unsigned N) const {
1088 assert(N == 2 && "Invalid number of operands!");
1089 assert(isRegShiftedImm() && "addRegShiftedImmOperands() on non RegShiftedImm!");
1090 Inst.addOperand(MCOperand::CreateReg(RegShiftedImm.SrcReg));
1091 Inst.addOperand(MCOperand::CreateImm(
1092 ARM_AM::getSORegOpc(RegShiftedImm.ShiftTy, RegShiftedImm.ShiftImm)));
1095 void addShifterImmOperands(MCInst &Inst, unsigned N) const {
1096 assert(N == 1 && "Invalid number of operands!");
1097 Inst.addOperand(MCOperand::CreateImm((ShifterImm.isASR << 5) |
1101 void addRegListOperands(MCInst &Inst, unsigned N) const {
1102 assert(N == 1 && "Invalid number of operands!");
1103 const SmallVectorImpl<unsigned> &RegList = getRegList();
1104 for (SmallVectorImpl<unsigned>::const_iterator
1105 I = RegList.begin(), E = RegList.end(); I != E; ++I)
1106 Inst.addOperand(MCOperand::CreateReg(*I));
1109 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
1110 addRegListOperands(Inst, N);
1113 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
1114 addRegListOperands(Inst, N);
1117 void addRotImmOperands(MCInst &Inst, unsigned N) const {
1118 assert(N == 1 && "Invalid number of operands!");
1119 // Encoded as val>>3. The printer handles display as 8, 16, 24.
1120 Inst.addOperand(MCOperand::CreateImm(RotImm.Imm >> 3));
1123 void addBitfieldOperands(MCInst &Inst, unsigned N) const {
1124 assert(N == 1 && "Invalid number of operands!");
1125 // Munge the lsb/width into a bitfield mask.
1126 unsigned lsb = Bitfield.LSB;
1127 unsigned width = Bitfield.Width;
1128 // Make a 32-bit mask w/ the referenced bits clear and all other bits set.
1129 uint32_t Mask = ~(((uint32_t)0xffffffff >> lsb) << (32 - width) >>
1130 (32 - (lsb + width)));
1131 Inst.addOperand(MCOperand::CreateImm(Mask));
1134 void addImmOperands(MCInst &Inst, unsigned N) const {
1135 assert(N == 1 && "Invalid number of operands!");
1136 addExpr(Inst, getImm());
1139 void addFPImmOperands(MCInst &Inst, unsigned N) const {
1140 assert(N == 1 && "Invalid number of operands!");
1141 Inst.addOperand(MCOperand::CreateImm(getFPImm()));
1144 void addImm8s4Operands(MCInst &Inst, unsigned N) const {
1145 assert(N == 1 && "Invalid number of operands!");
1146 // FIXME: We really want to scale the value here, but the LDRD/STRD
1147 // instruction don't encode operands that way yet.
1148 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1149 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
1152 void addImm0_1020s4Operands(MCInst &Inst, unsigned N) const {
1153 assert(N == 1 && "Invalid number of operands!");
1154 // The immediate is scaled by four in the encoding and is stored
1155 // in the MCInst as such. Lop off the low two bits here.
1156 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1157 Inst.addOperand(MCOperand::CreateImm(CE->getValue() / 4));
1160 void addImm0_508s4Operands(MCInst &Inst, unsigned N) const {
1161 assert(N == 1 && "Invalid number of operands!");
1162 // The immediate is scaled by four in the encoding and is stored
1163 // in the MCInst as such. Lop off the low two bits here.
1164 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1165 Inst.addOperand(MCOperand::CreateImm(CE->getValue() / 4));
1168 void addImm0_255Operands(MCInst &Inst, unsigned N) const {
1169 assert(N == 1 && "Invalid number of operands!");
1170 addExpr(Inst, getImm());
1173 void addImm0_7Operands(MCInst &Inst, unsigned N) const {
1174 assert(N == 1 && "Invalid number of operands!");
1175 addExpr(Inst, getImm());
1178 void addImm0_15Operands(MCInst &Inst, unsigned N) const {
1179 assert(N == 1 && "Invalid number of operands!");
1180 addExpr(Inst, getImm());
1183 void addImm0_31Operands(MCInst &Inst, unsigned N) const {
1184 assert(N == 1 && "Invalid number of operands!");
1185 addExpr(Inst, getImm());
1188 void addImm1_16Operands(MCInst &Inst, unsigned N) const {
1189 assert(N == 1 && "Invalid number of operands!");
1190 // The constant encodes as the immediate-1, and we store in the instruction
1191 // the bits as encoded, so subtract off one here.
1192 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1193 Inst.addOperand(MCOperand::CreateImm(CE->getValue() - 1));
1196 void addImm1_32Operands(MCInst &Inst, unsigned N) const {
1197 assert(N == 1 && "Invalid number of operands!");
1198 // The constant encodes as the immediate-1, and we store in the instruction
1199 // the bits as encoded, so subtract off one here.
1200 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1201 Inst.addOperand(MCOperand::CreateImm(CE->getValue() - 1));
1204 void addImm0_65535Operands(MCInst &Inst, unsigned N) const {
1205 assert(N == 1 && "Invalid number of operands!");
1206 addExpr(Inst, getImm());
1209 void addImm0_65535ExprOperands(MCInst &Inst, unsigned N) const {
1210 assert(N == 1 && "Invalid number of operands!");
1211 addExpr(Inst, getImm());
1214 void addImm24bitOperands(MCInst &Inst, unsigned N) const {
1215 assert(N == 1 && "Invalid number of operands!");
1216 addExpr(Inst, getImm());
1219 void addImmThumbSROperands(MCInst &Inst, unsigned N) const {
1220 assert(N == 1 && "Invalid number of operands!");
1221 // The constant encodes as the immediate, except for 32, which encodes as
1223 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1224 unsigned Imm = CE->getValue();
1225 Inst.addOperand(MCOperand::CreateImm((Imm == 32 ? 0 : Imm)));
1228 void addPKHLSLImmOperands(MCInst &Inst, unsigned N) const {
1229 assert(N == 1 && "Invalid number of operands!");
1230 addExpr(Inst, getImm());
1233 void addPKHASRImmOperands(MCInst &Inst, unsigned N) const {
1234 assert(N == 1 && "Invalid number of operands!");
1235 // An ASR value of 32 encodes as 0, so that's how we want to add it to
1236 // the instruction as well.
1237 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1238 int Val = CE->getValue();
1239 Inst.addOperand(MCOperand::CreateImm(Val == 32 ? 0 : Val));
1242 void addARMSOImmOperands(MCInst &Inst, unsigned N) const {
1243 assert(N == 1 && "Invalid number of operands!");
1244 addExpr(Inst, getImm());
1247 void addT2SOImmOperands(MCInst &Inst, unsigned N) const {
1248 assert(N == 1 && "Invalid number of operands!");
1249 addExpr(Inst, getImm());
1252 void addT2SOImmNotOperands(MCInst &Inst, unsigned N) const {
1253 assert(N == 1 && "Invalid number of operands!");
1254 // The operand is actually a t2_so_imm, but we have its bitwise
1255 // negation in the assembly source, so twiddle it here.
1256 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1257 Inst.addOperand(MCOperand::CreateImm(~CE->getValue()));
1260 void addSetEndImmOperands(MCInst &Inst, unsigned N) const {
1261 assert(N == 1 && "Invalid number of operands!");
1262 addExpr(Inst, getImm());
1265 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
1266 assert(N == 1 && "Invalid number of operands!");
1267 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
1270 void addMemNoOffsetOperands(MCInst &Inst, unsigned N) const {
1271 assert(N == 1 && "Invalid number of operands!");
1272 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1275 void addAlignedMemoryOperands(MCInst &Inst, unsigned N) const {
1276 assert(N == 2 && "Invalid number of operands!");
1277 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1278 Inst.addOperand(MCOperand::CreateImm(Memory.Alignment));
1281 void addAddrMode2Operands(MCInst &Inst, unsigned N) const {
1282 assert(N == 3 && "Invalid number of operands!");
1283 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1284 if (!Memory.OffsetRegNum) {
1285 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1286 // Special case for #-0
1287 if (Val == INT32_MIN) Val = 0;
1288 if (Val < 0) Val = -Val;
1289 Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
1291 // For register offset, we encode the shift type and negation flag
1293 Val = ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
1294 Memory.ShiftImm, Memory.ShiftType);
1296 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1297 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1298 Inst.addOperand(MCOperand::CreateImm(Val));
1301 void addAM2OffsetImmOperands(MCInst &Inst, unsigned N) const {
1302 assert(N == 2 && "Invalid number of operands!");
1303 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1304 assert(CE && "non-constant AM2OffsetImm operand!");
1305 int32_t Val = CE->getValue();
1306 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1307 // Special case for #-0
1308 if (Val == INT32_MIN) Val = 0;
1309 if (Val < 0) Val = -Val;
1310 Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
1311 Inst.addOperand(MCOperand::CreateReg(0));
1312 Inst.addOperand(MCOperand::CreateImm(Val));
1315 void addAddrMode3Operands(MCInst &Inst, unsigned N) const {
1316 assert(N == 3 && "Invalid number of operands!");
1317 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1318 if (!Memory.OffsetRegNum) {
1319 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1320 // Special case for #-0
1321 if (Val == INT32_MIN) Val = 0;
1322 if (Val < 0) Val = -Val;
1323 Val = ARM_AM::getAM3Opc(AddSub, Val);
1325 // For register offset, we encode the shift type and negation flag
1327 Val = ARM_AM::getAM3Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add, 0);
1329 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1330 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1331 Inst.addOperand(MCOperand::CreateImm(Val));
1334 void addAM3OffsetOperands(MCInst &Inst, unsigned N) const {
1335 assert(N == 2 && "Invalid number of operands!");
1336 if (Kind == k_PostIndexRegister) {
1338 ARM_AM::getAM3Opc(PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub, 0);
1339 Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1340 Inst.addOperand(MCOperand::CreateImm(Val));
1345 const MCConstantExpr *CE = static_cast<const MCConstantExpr*>(getImm());
1346 int32_t Val = CE->getValue();
1347 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1348 // Special case for #-0
1349 if (Val == INT32_MIN) Val = 0;
1350 if (Val < 0) Val = -Val;
1351 Val = ARM_AM::getAM3Opc(AddSub, Val);
1352 Inst.addOperand(MCOperand::CreateReg(0));
1353 Inst.addOperand(MCOperand::CreateImm(Val));
1356 void addAddrMode5Operands(MCInst &Inst, unsigned N) const {
1357 assert(N == 2 && "Invalid number of operands!");
1358 // The lower two bits are always zero and as such are not encoded.
1359 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
1360 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1361 // Special case for #-0
1362 if (Val == INT32_MIN) Val = 0;
1363 if (Val < 0) Val = -Val;
1364 Val = ARM_AM::getAM5Opc(AddSub, Val);
1365 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1366 Inst.addOperand(MCOperand::CreateImm(Val));
1369 void addMemImm8s4OffsetOperands(MCInst &Inst, unsigned N) const {
1370 assert(N == 2 && "Invalid number of operands!");
1371 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1372 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1373 Inst.addOperand(MCOperand::CreateImm(Val));
1376 void addMemImm0_1020s4OffsetOperands(MCInst &Inst, unsigned N) const {
1377 assert(N == 2 && "Invalid number of operands!");
1378 // The lower two bits are always zero and as such are not encoded.
1379 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
1380 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1381 Inst.addOperand(MCOperand::CreateImm(Val));
1384 void addMemImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1385 assert(N == 2 && "Invalid number of operands!");
1386 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1387 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1388 Inst.addOperand(MCOperand::CreateImm(Val));
1391 void addMemPosImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1392 addMemImm8OffsetOperands(Inst, N);
1395 void addMemNegImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1396 addMemImm8OffsetOperands(Inst, N);
1399 void addMemUImm12OffsetOperands(MCInst &Inst, unsigned N) const {
1400 assert(N == 2 && "Invalid number of operands!");
1401 // If this is an immediate, it's a label reference.
1402 if (Kind == k_Immediate) {
1403 addExpr(Inst, getImm());
1404 Inst.addOperand(MCOperand::CreateImm(0));
1408 // Otherwise, it's a normal memory reg+offset.
1409 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1410 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1411 Inst.addOperand(MCOperand::CreateImm(Val));
1414 void addMemImm12OffsetOperands(MCInst &Inst, unsigned N) const {
1415 assert(N == 2 && "Invalid number of operands!");
1416 // If this is an immediate, it's a label reference.
1417 if (Kind == k_Immediate) {
1418 addExpr(Inst, getImm());
1419 Inst.addOperand(MCOperand::CreateImm(0));
1423 // Otherwise, it's a normal memory reg+offset.
1424 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1425 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1426 Inst.addOperand(MCOperand::CreateImm(Val));
1429 void addMemTBBOperands(MCInst &Inst, unsigned N) const {
1430 assert(N == 2 && "Invalid number of operands!");
1431 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1432 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1435 void addMemTBHOperands(MCInst &Inst, unsigned N) const {
1436 assert(N == 2 && "Invalid number of operands!");
1437 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1438 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1441 void addMemRegOffsetOperands(MCInst &Inst, unsigned N) const {
1442 assert(N == 3 && "Invalid number of operands!");
1443 unsigned Val = ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
1444 Memory.ShiftImm, Memory.ShiftType);
1445 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1446 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1447 Inst.addOperand(MCOperand::CreateImm(Val));
1450 void addT2MemRegOffsetOperands(MCInst &Inst, unsigned N) const {
1451 assert(N == 3 && "Invalid number of operands!");
1452 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1453 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1454 Inst.addOperand(MCOperand::CreateImm(Memory.ShiftImm));
1457 void addMemThumbRROperands(MCInst &Inst, unsigned N) const {
1458 assert(N == 2 && "Invalid number of operands!");
1459 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1460 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1463 void addMemThumbRIs4Operands(MCInst &Inst, unsigned N) const {
1464 assert(N == 2 && "Invalid number of operands!");
1465 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
1466 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1467 Inst.addOperand(MCOperand::CreateImm(Val));
1470 void addMemThumbRIs2Operands(MCInst &Inst, unsigned N) const {
1471 assert(N == 2 && "Invalid number of operands!");
1472 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 2) : 0;
1473 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1474 Inst.addOperand(MCOperand::CreateImm(Val));
1477 void addMemThumbRIs1Operands(MCInst &Inst, unsigned N) const {
1478 assert(N == 2 && "Invalid number of operands!");
1479 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue()) : 0;
1480 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1481 Inst.addOperand(MCOperand::CreateImm(Val));
1484 void addMemThumbSPIOperands(MCInst &Inst, unsigned N) const {
1485 assert(N == 2 && "Invalid number of operands!");
1486 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
1487 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1488 Inst.addOperand(MCOperand::CreateImm(Val));
1491 void addPostIdxImm8Operands(MCInst &Inst, unsigned N) const {
1492 assert(N == 1 && "Invalid number of operands!");
1493 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1494 assert(CE && "non-constant post-idx-imm8 operand!");
1495 int Imm = CE->getValue();
1496 bool isAdd = Imm >= 0;
1497 if (Imm == INT32_MIN) Imm = 0;
1498 Imm = (Imm < 0 ? -Imm : Imm) | (int)isAdd << 8;
1499 Inst.addOperand(MCOperand::CreateImm(Imm));
1502 void addPostIdxImm8s4Operands(MCInst &Inst, unsigned N) const {
1503 assert(N == 1 && "Invalid number of operands!");
1504 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1505 assert(CE && "non-constant post-idx-imm8s4 operand!");
1506 int Imm = CE->getValue();
1507 bool isAdd = Imm >= 0;
1508 if (Imm == INT32_MIN) Imm = 0;
1509 // Immediate is scaled by 4.
1510 Imm = ((Imm < 0 ? -Imm : Imm) / 4) | (int)isAdd << 8;
1511 Inst.addOperand(MCOperand::CreateImm(Imm));
1514 void addPostIdxRegOperands(MCInst &Inst, unsigned N) const {
1515 assert(N == 2 && "Invalid number of operands!");
1516 Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1517 Inst.addOperand(MCOperand::CreateImm(PostIdxReg.isAdd));
1520 void addPostIdxRegShiftedOperands(MCInst &Inst, unsigned N) const {
1521 assert(N == 2 && "Invalid number of operands!");
1522 Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1523 // The sign, shift type, and shift amount are encoded in a single operand
1524 // using the AM2 encoding helpers.
1525 ARM_AM::AddrOpc opc = PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub;
1526 unsigned Imm = ARM_AM::getAM2Opc(opc, PostIdxReg.ShiftImm,
1527 PostIdxReg.ShiftTy);
1528 Inst.addOperand(MCOperand::CreateImm(Imm));
1531 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
1532 assert(N == 1 && "Invalid number of operands!");
1533 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
1536 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
1537 assert(N == 1 && "Invalid number of operands!");
1538 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
1541 void addVecListOneDOperands(MCInst &Inst, unsigned N) const {
1542 assert(N == 1 && "Invalid number of operands!");
1543 Inst.addOperand(MCOperand::CreateReg(VectorList.RegNum));
1546 void addVecListTwoDOperands(MCInst &Inst, unsigned N) const {
1547 assert(N == 1 && "Invalid number of operands!");
1548 // Only the first register actually goes on the instruction. The rest
1549 // are implied by the opcode.
1550 Inst.addOperand(MCOperand::CreateReg(VectorList.RegNum));
1553 void addVecListThreeDOperands(MCInst &Inst, unsigned N) const {
1554 assert(N == 1 && "Invalid number of operands!");
1555 // Only the first register actually goes on the instruction. The rest
1556 // are implied by the opcode.
1557 Inst.addOperand(MCOperand::CreateReg(VectorList.RegNum));
1560 void addVecListFourDOperands(MCInst &Inst, unsigned N) const {
1561 assert(N == 1 && "Invalid number of operands!");
1562 // Only the first register actually goes on the instruction. The rest
1563 // are implied by the opcode.
1564 Inst.addOperand(MCOperand::CreateReg(VectorList.RegNum));
1567 void addVecListTwoQOperands(MCInst &Inst, unsigned N) const {
1568 assert(N == 1 && "Invalid number of operands!");
1569 // Only the first register actually goes on the instruction. The rest
1570 // are implied by the opcode.
1571 Inst.addOperand(MCOperand::CreateReg(VectorList.RegNum));
1574 void addVectorIndex8Operands(MCInst &Inst, unsigned N) const {
1575 assert(N == 1 && "Invalid number of operands!");
1576 Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1579 void addVectorIndex16Operands(MCInst &Inst, unsigned N) const {
1580 assert(N == 1 && "Invalid number of operands!");
1581 Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1584 void addVectorIndex32Operands(MCInst &Inst, unsigned N) const {
1585 assert(N == 1 && "Invalid number of operands!");
1586 Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1589 void addNEONi8splatOperands(MCInst &Inst, unsigned N) const {
1590 assert(N == 1 && "Invalid number of operands!");
1591 // The immediate encodes the type of constant as well as the value.
1592 // Mask in that this is an i8 splat.
1593 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1594 Inst.addOperand(MCOperand::CreateImm(CE->getValue() | 0xe00));
1597 void addNEONi16splatOperands(MCInst &Inst, unsigned N) const {
1598 assert(N == 1 && "Invalid number of operands!");
1599 // The immediate encodes the type of constant as well as the value.
1600 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1601 unsigned Value = CE->getValue();
1603 Value = (Value >> 8) | 0xa00;
1606 Inst.addOperand(MCOperand::CreateImm(Value));
1609 void addNEONi32splatOperands(MCInst &Inst, unsigned N) const {
1610 assert(N == 1 && "Invalid number of operands!");
1611 // The immediate encodes the type of constant as well as the value.
1612 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1613 unsigned Value = CE->getValue();
1614 if (Value >= 256 && Value <= 0xff00)
1615 Value = (Value >> 8) | 0x200;
1616 else if (Value > 0xffff && Value <= 0xff0000)
1617 Value = (Value >> 16) | 0x400;
1618 else if (Value > 0xffffff)
1619 Value = (Value >> 24) | 0x600;
1620 Inst.addOperand(MCOperand::CreateImm(Value));
1623 void addNEONi32vmovOperands(MCInst &Inst, unsigned N) const {
1624 assert(N == 1 && "Invalid number of operands!");
1625 // The immediate encodes the type of constant as well as the value.
1626 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1627 unsigned Value = CE->getValue();
1628 if (Value >= 256 && Value <= 0xffff)
1629 Value = (Value >> 8) | ((Value & 0xff) ? 0xc00 : 0x200);
1630 else if (Value > 0xffff && Value <= 0xffffff)
1631 Value = (Value >> 16) | ((Value & 0xff) ? 0xd00 : 0x400);
1632 else if (Value > 0xffffff)
1633 Value = (Value >> 24) | 0x600;
1634 Inst.addOperand(MCOperand::CreateImm(Value));
1637 void addNEONi64splatOperands(MCInst &Inst, unsigned N) const {
1638 assert(N == 1 && "Invalid number of operands!");
1639 // The immediate encodes the type of constant as well as the value.
1640 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1641 uint64_t Value = CE->getValue();
1643 for (unsigned i = 0; i < 8; ++i, Value >>= 8) {
1644 Imm |= (Value & 1) << i;
1646 Inst.addOperand(MCOperand::CreateImm(Imm | 0x1e00));
1649 virtual void print(raw_ostream &OS) const;
1651 static ARMOperand *CreateITMask(unsigned Mask, SMLoc S) {
1652 ARMOperand *Op = new ARMOperand(k_ITCondMask);
1653 Op->ITMask.Mask = Mask;
1659 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
1660 ARMOperand *Op = new ARMOperand(k_CondCode);
1667 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
1668 ARMOperand *Op = new ARMOperand(k_CoprocNum);
1669 Op->Cop.Val = CopVal;
1675 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
1676 ARMOperand *Op = new ARMOperand(k_CoprocReg);
1677 Op->Cop.Val = CopVal;
1683 static ARMOperand *CreateCoprocOption(unsigned Val, SMLoc S, SMLoc E) {
1684 ARMOperand *Op = new ARMOperand(k_CoprocOption);
1691 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
1692 ARMOperand *Op = new ARMOperand(k_CCOut);
1693 Op->Reg.RegNum = RegNum;
1699 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
1700 ARMOperand *Op = new ARMOperand(k_Token);
1701 Op->Tok.Data = Str.data();
1702 Op->Tok.Length = Str.size();
1708 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
1709 ARMOperand *Op = new ARMOperand(k_Register);
1710 Op->Reg.RegNum = RegNum;
1716 static ARMOperand *CreateShiftedRegister(ARM_AM::ShiftOpc ShTy,
1721 ARMOperand *Op = new ARMOperand(k_ShiftedRegister);
1722 Op->RegShiftedReg.ShiftTy = ShTy;
1723 Op->RegShiftedReg.SrcReg = SrcReg;
1724 Op->RegShiftedReg.ShiftReg = ShiftReg;
1725 Op->RegShiftedReg.ShiftImm = ShiftImm;
1731 static ARMOperand *CreateShiftedImmediate(ARM_AM::ShiftOpc ShTy,
1735 ARMOperand *Op = new ARMOperand(k_ShiftedImmediate);
1736 Op->RegShiftedImm.ShiftTy = ShTy;
1737 Op->RegShiftedImm.SrcReg = SrcReg;
1738 Op->RegShiftedImm.ShiftImm = ShiftImm;
1744 static ARMOperand *CreateShifterImm(bool isASR, unsigned Imm,
1746 ARMOperand *Op = new ARMOperand(k_ShifterImmediate);
1747 Op->ShifterImm.isASR = isASR;
1748 Op->ShifterImm.Imm = Imm;
1754 static ARMOperand *CreateRotImm(unsigned Imm, SMLoc S, SMLoc E) {
1755 ARMOperand *Op = new ARMOperand(k_RotateImmediate);
1756 Op->RotImm.Imm = Imm;
1762 static ARMOperand *CreateBitfield(unsigned LSB, unsigned Width,
1764 ARMOperand *Op = new ARMOperand(k_BitfieldDescriptor);
1765 Op->Bitfield.LSB = LSB;
1766 Op->Bitfield.Width = Width;
1773 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
1774 SMLoc StartLoc, SMLoc EndLoc) {
1775 KindTy Kind = k_RegisterList;
1777 if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Regs.front().first))
1778 Kind = k_DPRRegisterList;
1779 else if (ARMMCRegisterClasses[ARM::SPRRegClassID].
1780 contains(Regs.front().first))
1781 Kind = k_SPRRegisterList;
1783 ARMOperand *Op = new ARMOperand(Kind);
1784 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
1785 I = Regs.begin(), E = Regs.end(); I != E; ++I)
1786 Op->Registers.push_back(I->first);
1787 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
1788 Op->StartLoc = StartLoc;
1789 Op->EndLoc = EndLoc;
1793 static ARMOperand *CreateVectorList(unsigned RegNum, unsigned Count,
1795 ARMOperand *Op = new ARMOperand(k_VectorList);
1796 Op->VectorList.RegNum = RegNum;
1797 Op->VectorList.Count = Count;
1803 static ARMOperand *CreateVectorIndex(unsigned Idx, SMLoc S, SMLoc E,
1805 ARMOperand *Op = new ARMOperand(k_VectorIndex);
1806 Op->VectorIndex.Val = Idx;
1812 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
1813 ARMOperand *Op = new ARMOperand(k_Immediate);
1820 static ARMOperand *CreateFPImm(unsigned Val, SMLoc S, MCContext &Ctx) {
1821 ARMOperand *Op = new ARMOperand(k_FPImmediate);
1822 Op->FPImm.Val = Val;
1828 static ARMOperand *CreateMem(unsigned BaseRegNum,
1829 const MCConstantExpr *OffsetImm,
1830 unsigned OffsetRegNum,
1831 ARM_AM::ShiftOpc ShiftType,
1836 ARMOperand *Op = new ARMOperand(k_Memory);
1837 Op->Memory.BaseRegNum = BaseRegNum;
1838 Op->Memory.OffsetImm = OffsetImm;
1839 Op->Memory.OffsetRegNum = OffsetRegNum;
1840 Op->Memory.ShiftType = ShiftType;
1841 Op->Memory.ShiftImm = ShiftImm;
1842 Op->Memory.Alignment = Alignment;
1843 Op->Memory.isNegative = isNegative;
1849 static ARMOperand *CreatePostIdxReg(unsigned RegNum, bool isAdd,
1850 ARM_AM::ShiftOpc ShiftTy,
1853 ARMOperand *Op = new ARMOperand(k_PostIndexRegister);
1854 Op->PostIdxReg.RegNum = RegNum;
1855 Op->PostIdxReg.isAdd = isAdd;
1856 Op->PostIdxReg.ShiftTy = ShiftTy;
1857 Op->PostIdxReg.ShiftImm = ShiftImm;
1863 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
1864 ARMOperand *Op = new ARMOperand(k_MemBarrierOpt);
1865 Op->MBOpt.Val = Opt;
1871 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
1872 ARMOperand *Op = new ARMOperand(k_ProcIFlags);
1873 Op->IFlags.Val = IFlags;
1879 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
1880 ARMOperand *Op = new ARMOperand(k_MSRMask);
1881 Op->MMask.Val = MMask;
1888 } // end anonymous namespace.
1890 void ARMOperand::print(raw_ostream &OS) const {
1893 OS << "<fpimm " << getFPImm() << "(" << ARM_AM::getFPImmFloat(getFPImm())
1897 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
1900 OS << "<ccout " << getReg() << ">";
1902 case k_ITCondMask: {
1903 static const char *MaskStr[] = {
1904 "()", "(t)", "(e)", "(tt)", "(et)", "(te)", "(ee)", "(ttt)", "(ett)",
1905 "(tet)", "(eet)", "(tte)", "(ete)", "(tee)", "(eee)"
1907 assert((ITMask.Mask & 0xf) == ITMask.Mask);
1908 OS << "<it-mask " << MaskStr[ITMask.Mask] << ">";
1912 OS << "<coprocessor number: " << getCoproc() << ">";
1915 OS << "<coprocessor register: " << getCoproc() << ">";
1917 case k_CoprocOption:
1918 OS << "<coprocessor option: " << CoprocOption.Val << ">";
1921 OS << "<mask: " << getMSRMask() << ">";
1924 getImm()->print(OS);
1926 case k_MemBarrierOpt:
1927 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
1931 << " base:" << Memory.BaseRegNum;
1934 case k_PostIndexRegister:
1935 OS << "post-idx register " << (PostIdxReg.isAdd ? "" : "-")
1936 << PostIdxReg.RegNum;
1937 if (PostIdxReg.ShiftTy != ARM_AM::no_shift)
1938 OS << ARM_AM::getShiftOpcStr(PostIdxReg.ShiftTy) << " "
1939 << PostIdxReg.ShiftImm;
1942 case k_ProcIFlags: {
1943 OS << "<ARM_PROC::";
1944 unsigned IFlags = getProcIFlags();
1945 for (int i=2; i >= 0; --i)
1946 if (IFlags & (1 << i))
1947 OS << ARM_PROC::IFlagsToString(1 << i);
1952 OS << "<register " << getReg() << ">";
1954 case k_ShifterImmediate:
1955 OS << "<shift " << (ShifterImm.isASR ? "asr" : "lsl")
1956 << " #" << ShifterImm.Imm << ">";
1958 case k_ShiftedRegister:
1959 OS << "<so_reg_reg "
1960 << RegShiftedReg.SrcReg
1961 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(RegShiftedReg.ShiftImm))
1962 << ", " << RegShiftedReg.ShiftReg << ", "
1963 << ARM_AM::getSORegOffset(RegShiftedReg.ShiftImm)
1966 case k_ShiftedImmediate:
1967 OS << "<so_reg_imm "
1968 << RegShiftedImm.SrcReg
1969 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(RegShiftedImm.ShiftImm))
1970 << ", " << ARM_AM::getSORegOffset(RegShiftedImm.ShiftImm)
1973 case k_RotateImmediate:
1974 OS << "<ror " << " #" << (RotImm.Imm * 8) << ">";
1976 case k_BitfieldDescriptor:
1977 OS << "<bitfield " << "lsb: " << Bitfield.LSB
1978 << ", width: " << Bitfield.Width << ">";
1980 case k_RegisterList:
1981 case k_DPRRegisterList:
1982 case k_SPRRegisterList: {
1983 OS << "<register_list ";
1985 const SmallVectorImpl<unsigned> &RegList = getRegList();
1986 for (SmallVectorImpl<unsigned>::const_iterator
1987 I = RegList.begin(), E = RegList.end(); I != E; ) {
1989 if (++I < E) OS << ", ";
1996 OS << "<vector_list " << VectorList.Count << " * "
1997 << VectorList.RegNum << ">";
2000 OS << "'" << getToken() << "'";
2003 OS << "<vectorindex " << getVectorIndex() << ">";
2008 /// @name Auto-generated Match Functions
2011 static unsigned MatchRegisterName(StringRef Name);
2015 bool ARMAsmParser::ParseRegister(unsigned &RegNo,
2016 SMLoc &StartLoc, SMLoc &EndLoc) {
2017 RegNo = tryParseRegister();
2019 return (RegNo == (unsigned)-1);
2022 /// Try to parse a register name. The token must be an Identifier when called,
2023 /// and if it is a register name the token is eaten and the register number is
2024 /// returned. Otherwise return -1.
2026 int ARMAsmParser::tryParseRegister() {
2027 const AsmToken &Tok = Parser.getTok();
2028 if (Tok.isNot(AsmToken::Identifier)) return -1;
2030 // FIXME: Validate register for the current architecture; we have to do
2031 // validation later, so maybe there is no need for this here.
2032 std::string upperCase = Tok.getString().str();
2033 std::string lowerCase = LowercaseString(upperCase);
2034 unsigned RegNum = MatchRegisterName(lowerCase);
2036 RegNum = StringSwitch<unsigned>(lowerCase)
2037 .Case("r13", ARM::SP)
2038 .Case("r14", ARM::LR)
2039 .Case("r15", ARM::PC)
2040 .Case("ip", ARM::R12)
2043 if (!RegNum) return -1;
2045 Parser.Lex(); // Eat identifier token.
2050 // Try to parse a shifter (e.g., "lsl <amt>"). On success, return 0.
2051 // If a recoverable error occurs, return 1. If an irrecoverable error
2052 // occurs, return -1. An irrecoverable error is one where tokens have been
2053 // consumed in the process of trying to parse the shifter (i.e., when it is
2054 // indeed a shifter operand, but malformed).
2055 int ARMAsmParser::tryParseShiftRegister(
2056 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2057 SMLoc S = Parser.getTok().getLoc();
2058 const AsmToken &Tok = Parser.getTok();
2059 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2061 std::string upperCase = Tok.getString().str();
2062 std::string lowerCase = LowercaseString(upperCase);
2063 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
2064 .Case("lsl", ARM_AM::lsl)
2065 .Case("lsr", ARM_AM::lsr)
2066 .Case("asr", ARM_AM::asr)
2067 .Case("ror", ARM_AM::ror)
2068 .Case("rrx", ARM_AM::rrx)
2069 .Default(ARM_AM::no_shift);
2071 if (ShiftTy == ARM_AM::no_shift)
2074 Parser.Lex(); // Eat the operator.
2076 // The source register for the shift has already been added to the
2077 // operand list, so we need to pop it off and combine it into the shifted
2078 // register operand instead.
2079 OwningPtr<ARMOperand> PrevOp((ARMOperand*)Operands.pop_back_val());
2080 if (!PrevOp->isReg())
2081 return Error(PrevOp->getStartLoc(), "shift must be of a register");
2082 int SrcReg = PrevOp->getReg();
2085 if (ShiftTy == ARM_AM::rrx) {
2086 // RRX Doesn't have an explicit shift amount. The encoder expects
2087 // the shift register to be the same as the source register. Seems odd,
2091 // Figure out if this is shifted by a constant or a register (for non-RRX).
2092 if (Parser.getTok().is(AsmToken::Hash)) {
2093 Parser.Lex(); // Eat hash.
2094 SMLoc ImmLoc = Parser.getTok().getLoc();
2095 const MCExpr *ShiftExpr = 0;
2096 if (getParser().ParseExpression(ShiftExpr)) {
2097 Error(ImmLoc, "invalid immediate shift value");
2100 // The expression must be evaluatable as an immediate.
2101 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
2103 Error(ImmLoc, "invalid immediate shift value");
2106 // Range check the immediate.
2107 // lsl, ror: 0 <= imm <= 31
2108 // lsr, asr: 0 <= imm <= 32
2109 Imm = CE->getValue();
2111 ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
2112 ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
2113 Error(ImmLoc, "immediate shift value out of range");
2116 } else if (Parser.getTok().is(AsmToken::Identifier)) {
2117 ShiftReg = tryParseRegister();
2118 SMLoc L = Parser.getTok().getLoc();
2119 if (ShiftReg == -1) {
2120 Error (L, "expected immediate or register in shift operand");
2124 Error (Parser.getTok().getLoc(),
2125 "expected immediate or register in shift operand");
2130 if (ShiftReg && ShiftTy != ARM_AM::rrx)
2131 Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
2133 S, Parser.getTok().getLoc()));
2135 Operands.push_back(ARMOperand::CreateShiftedImmediate(ShiftTy, SrcReg, Imm,
2136 S, Parser.getTok().getLoc()));
2142 /// Try to parse a register name. The token must be an Identifier when called.
2143 /// If it's a register, an AsmOperand is created. Another AsmOperand is created
2144 /// if there is a "writeback". 'true' if it's not a register.
2146 /// TODO this is likely to change to allow different register types and or to
2147 /// parse for a specific register type.
2149 tryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2150 SMLoc S = Parser.getTok().getLoc();
2151 int RegNo = tryParseRegister();
2155 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
2157 const AsmToken &ExclaimTok = Parser.getTok();
2158 if (ExclaimTok.is(AsmToken::Exclaim)) {
2159 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
2160 ExclaimTok.getLoc()));
2161 Parser.Lex(); // Eat exclaim token
2165 // Also check for an index operand. This is only legal for vector registers,
2166 // but that'll get caught OK in operand matching, so we don't need to
2167 // explicitly filter everything else out here.
2168 if (Parser.getTok().is(AsmToken::LBrac)) {
2169 SMLoc SIdx = Parser.getTok().getLoc();
2170 Parser.Lex(); // Eat left bracket token.
2172 const MCExpr *ImmVal;
2173 if (getParser().ParseExpression(ImmVal))
2174 return MatchOperand_ParseFail;
2175 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(ImmVal);
2177 TokError("immediate value expected for vector index");
2178 return MatchOperand_ParseFail;
2181 SMLoc E = Parser.getTok().getLoc();
2182 if (Parser.getTok().isNot(AsmToken::RBrac)) {
2183 Error(E, "']' expected");
2184 return MatchOperand_ParseFail;
2187 Parser.Lex(); // Eat right bracket token.
2189 Operands.push_back(ARMOperand::CreateVectorIndex(MCE->getValue(),
2197 /// MatchCoprocessorOperandName - Try to parse an coprocessor related
2198 /// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
2200 static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
2201 // Use the same layout as the tablegen'erated register name matcher. Ugly,
2203 switch (Name.size()) {
2206 if (Name[0] != CoprocOp)
2223 if (Name[0] != CoprocOp || Name[1] != '1')
2227 case '0': return 10;
2228 case '1': return 11;
2229 case '2': return 12;
2230 case '3': return 13;
2231 case '4': return 14;
2232 case '5': return 15;
2240 /// parseITCondCode - Try to parse a condition code for an IT instruction.
2241 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2242 parseITCondCode(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2243 SMLoc S = Parser.getTok().getLoc();
2244 const AsmToken &Tok = Parser.getTok();
2245 if (!Tok.is(AsmToken::Identifier))
2246 return MatchOperand_NoMatch;
2247 unsigned CC = StringSwitch<unsigned>(Tok.getString())
2248 .Case("eq", ARMCC::EQ)
2249 .Case("ne", ARMCC::NE)
2250 .Case("hs", ARMCC::HS)
2251 .Case("cs", ARMCC::HS)
2252 .Case("lo", ARMCC::LO)
2253 .Case("cc", ARMCC::LO)
2254 .Case("mi", ARMCC::MI)
2255 .Case("pl", ARMCC::PL)
2256 .Case("vs", ARMCC::VS)
2257 .Case("vc", ARMCC::VC)
2258 .Case("hi", ARMCC::HI)
2259 .Case("ls", ARMCC::LS)
2260 .Case("ge", ARMCC::GE)
2261 .Case("lt", ARMCC::LT)
2262 .Case("gt", ARMCC::GT)
2263 .Case("le", ARMCC::LE)
2264 .Case("al", ARMCC::AL)
2267 return MatchOperand_NoMatch;
2268 Parser.Lex(); // Eat the token.
2270 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), S));
2272 return MatchOperand_Success;
2275 /// parseCoprocNumOperand - Try to parse an coprocessor number operand. The
2276 /// token must be an Identifier when called, and if it is a coprocessor
2277 /// number, the token is eaten and the operand is added to the operand list.
2278 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2279 parseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2280 SMLoc S = Parser.getTok().getLoc();
2281 const AsmToken &Tok = Parser.getTok();
2282 if (Tok.isNot(AsmToken::Identifier))
2283 return MatchOperand_NoMatch;
2285 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
2287 return MatchOperand_NoMatch;
2289 Parser.Lex(); // Eat identifier token.
2290 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
2291 return MatchOperand_Success;
2294 /// parseCoprocRegOperand - Try to parse an coprocessor register operand. The
2295 /// token must be an Identifier when called, and if it is a coprocessor
2296 /// number, the token is eaten and the operand is added to the operand list.
2297 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2298 parseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2299 SMLoc S = Parser.getTok().getLoc();
2300 const AsmToken &Tok = Parser.getTok();
2301 if (Tok.isNot(AsmToken::Identifier))
2302 return MatchOperand_NoMatch;
2304 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
2306 return MatchOperand_NoMatch;
2308 Parser.Lex(); // Eat identifier token.
2309 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
2310 return MatchOperand_Success;
2313 /// parseCoprocOptionOperand - Try to parse an coprocessor option operand.
2314 /// coproc_option : '{' imm0_255 '}'
2315 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2316 parseCoprocOptionOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2317 SMLoc S = Parser.getTok().getLoc();
2319 // If this isn't a '{', this isn't a coprocessor immediate operand.
2320 if (Parser.getTok().isNot(AsmToken::LCurly))
2321 return MatchOperand_NoMatch;
2322 Parser.Lex(); // Eat the '{'
2325 SMLoc Loc = Parser.getTok().getLoc();
2326 if (getParser().ParseExpression(Expr)) {
2327 Error(Loc, "illegal expression");
2328 return MatchOperand_ParseFail;
2330 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
2331 if (!CE || CE->getValue() < 0 || CE->getValue() > 255) {
2332 Error(Loc, "coprocessor option must be an immediate in range [0, 255]");
2333 return MatchOperand_ParseFail;
2335 int Val = CE->getValue();
2337 // Check for and consume the closing '}'
2338 if (Parser.getTok().isNot(AsmToken::RCurly))
2339 return MatchOperand_ParseFail;
2340 SMLoc E = Parser.getTok().getLoc();
2341 Parser.Lex(); // Eat the '}'
2343 Operands.push_back(ARMOperand::CreateCoprocOption(Val, S, E));
2344 return MatchOperand_Success;
2347 // For register list parsing, we need to map from raw GPR register numbering
2348 // to the enumeration values. The enumeration values aren't sorted by
2349 // register number due to our using "sp", "lr" and "pc" as canonical names.
2350 static unsigned getNextRegister(unsigned Reg) {
2351 // If this is a GPR, we need to do it manually, otherwise we can rely
2352 // on the sort ordering of the enumeration since the other reg-classes
2354 if (!ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
2357 default: assert(0 && "Invalid GPR number!");
2358 case ARM::R0: return ARM::R1; case ARM::R1: return ARM::R2;
2359 case ARM::R2: return ARM::R3; case ARM::R3: return ARM::R4;
2360 case ARM::R4: return ARM::R5; case ARM::R5: return ARM::R6;
2361 case ARM::R6: return ARM::R7; case ARM::R7: return ARM::R8;
2362 case ARM::R8: return ARM::R9; case ARM::R9: return ARM::R10;
2363 case ARM::R10: return ARM::R11; case ARM::R11: return ARM::R12;
2364 case ARM::R12: return ARM::SP; case ARM::SP: return ARM::LR;
2365 case ARM::LR: return ARM::PC; case ARM::PC: return ARM::R0;
2369 /// Parse a register list.
2371 parseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2372 assert(Parser.getTok().is(AsmToken::LCurly) &&
2373 "Token is not a Left Curly Brace");
2374 SMLoc S = Parser.getTok().getLoc();
2375 Parser.Lex(); // Eat '{' token.
2376 SMLoc RegLoc = Parser.getTok().getLoc();
2378 // Check the first register in the list to see what register class
2379 // this is a list of.
2380 int Reg = tryParseRegister();
2382 return Error(RegLoc, "register expected");
2384 const MCRegisterClass *RC;
2385 if (ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
2386 RC = &ARMMCRegisterClasses[ARM::GPRRegClassID];
2387 else if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg))
2388 RC = &ARMMCRegisterClasses[ARM::DPRRegClassID];
2389 else if (ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg))
2390 RC = &ARMMCRegisterClasses[ARM::SPRRegClassID];
2392 return Error(RegLoc, "invalid register in register list");
2394 // The reglist instructions have at most 16 registers, so reserve
2395 // space for that many.
2396 SmallVector<std::pair<unsigned, SMLoc>, 16> Registers;
2397 // Store the first register.
2398 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2400 // This starts immediately after the first register token in the list,
2401 // so we can see either a comma or a minus (range separator) as a legal
2403 while (Parser.getTok().is(AsmToken::Comma) ||
2404 Parser.getTok().is(AsmToken::Minus)) {
2405 if (Parser.getTok().is(AsmToken::Minus)) {
2406 Parser.Lex(); // Eat the comma.
2407 SMLoc EndLoc = Parser.getTok().getLoc();
2408 int EndReg = tryParseRegister();
2410 return Error(EndLoc, "register expected");
2411 // If the register is the same as the start reg, there's nothing
2415 // The register must be in the same register class as the first.
2416 if (!RC->contains(EndReg))
2417 return Error(EndLoc, "invalid register in register list");
2418 // Ranges must go from low to high.
2419 if (getARMRegisterNumbering(Reg) > getARMRegisterNumbering(EndReg))
2420 return Error(EndLoc, "bad range in register list");
2422 // Add all the registers in the range to the register list.
2423 while (Reg != EndReg) {
2424 Reg = getNextRegister(Reg);
2425 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2429 Parser.Lex(); // Eat the comma.
2430 RegLoc = Parser.getTok().getLoc();
2432 Reg = tryParseRegister();
2434 return Error(RegLoc, "register expected");
2435 // The register must be in the same register class as the first.
2436 if (!RC->contains(Reg))
2437 return Error(RegLoc, "invalid register in register list");
2438 // List must be monotonically increasing.
2439 if (getARMRegisterNumbering(Reg) <= getARMRegisterNumbering(OldReg))
2440 return Error(RegLoc, "register list not in ascending order");
2441 // VFP register lists must also be contiguous.
2442 // It's OK to use the enumeration values directly here rather, as the
2443 // VFP register classes have the enum sorted properly.
2444 if (RC != &ARMMCRegisterClasses[ARM::GPRRegClassID] &&
2446 return Error(RegLoc, "non-contiguous register range");
2447 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2450 SMLoc E = Parser.getTok().getLoc();
2451 if (Parser.getTok().isNot(AsmToken::RCurly))
2452 return Error(E, "'}' expected");
2453 Parser.Lex(); // Eat '}' token.
2455 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
2459 // Return the low-subreg of a given Q register.
2460 static unsigned getDRegFromQReg(unsigned QReg) {
2462 default: llvm_unreachable("expected a Q register!");
2463 case ARM::Q0: return ARM::D0;
2464 case ARM::Q1: return ARM::D2;
2465 case ARM::Q2: return ARM::D4;
2466 case ARM::Q3: return ARM::D6;
2467 case ARM::Q4: return ARM::D8;
2468 case ARM::Q5: return ARM::D10;
2469 case ARM::Q6: return ARM::D12;
2470 case ARM::Q7: return ARM::D14;
2471 case ARM::Q8: return ARM::D16;
2472 case ARM::Q9: return ARM::D19;
2473 case ARM::Q10: return ARM::D20;
2474 case ARM::Q11: return ARM::D22;
2475 case ARM::Q12: return ARM::D24;
2476 case ARM::Q13: return ARM::D26;
2477 case ARM::Q14: return ARM::D28;
2478 case ARM::Q15: return ARM::D30;
2482 // parse a vector register list
2483 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2484 parseVectorList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2485 if(Parser.getTok().isNot(AsmToken::LCurly))
2486 return MatchOperand_NoMatch;
2488 SMLoc S = Parser.getTok().getLoc();
2489 Parser.Lex(); // Eat '{' token.
2490 SMLoc RegLoc = Parser.getTok().getLoc();
2492 int Reg = tryParseRegister();
2494 Error(RegLoc, "register expected");
2495 return MatchOperand_ParseFail;
2498 unsigned FirstReg = Reg;
2499 // The list is of D registers, but we also allow Q regs and just interpret
2500 // them as the two D sub-registers.
2501 if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
2502 FirstReg = Reg = getDRegFromQReg(Reg);
2507 while (Parser.getTok().is(AsmToken::Comma)) {
2508 Parser.Lex(); // Eat the comma.
2509 RegLoc = Parser.getTok().getLoc();
2511 Reg = tryParseRegister();
2513 Error(RegLoc, "register expected");
2514 return MatchOperand_ParseFail;
2516 // vector register lists must be contiguous.
2517 // It's OK to use the enumeration values directly here rather, as the
2518 // VFP register classes have the enum sorted properly.
2520 // The list is of D registers, but we also allow Q regs and just interpret
2521 // them as the two D sub-registers.
2522 if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
2523 Reg = getDRegFromQReg(Reg);
2524 if (Reg != OldReg + 1) {
2525 Error(RegLoc, "non-contiguous register range");
2526 return MatchOperand_ParseFail;
2532 // Normal D register. Just check that it's contiguous and keep going.
2533 if (Reg != OldReg + 1) {
2534 Error(RegLoc, "non-contiguous register range");
2535 return MatchOperand_ParseFail;
2540 SMLoc E = Parser.getTok().getLoc();
2541 if (Parser.getTok().isNot(AsmToken::RCurly)) {
2542 Error(E, "'}' expected");
2543 return MatchOperand_ParseFail;
2545 Parser.Lex(); // Eat '}' token.
2547 Operands.push_back(ARMOperand::CreateVectorList(FirstReg, Count, S, E));
2548 return MatchOperand_Success;
2551 /// parseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
2552 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2553 parseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2554 SMLoc S = Parser.getTok().getLoc();
2555 const AsmToken &Tok = Parser.getTok();
2556 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2557 StringRef OptStr = Tok.getString();
2559 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
2560 .Case("sy", ARM_MB::SY)
2561 .Case("st", ARM_MB::ST)
2562 .Case("sh", ARM_MB::ISH)
2563 .Case("ish", ARM_MB::ISH)
2564 .Case("shst", ARM_MB::ISHST)
2565 .Case("ishst", ARM_MB::ISHST)
2566 .Case("nsh", ARM_MB::NSH)
2567 .Case("un", ARM_MB::NSH)
2568 .Case("nshst", ARM_MB::NSHST)
2569 .Case("unst", ARM_MB::NSHST)
2570 .Case("osh", ARM_MB::OSH)
2571 .Case("oshst", ARM_MB::OSHST)
2575 return MatchOperand_NoMatch;
2577 Parser.Lex(); // Eat identifier token.
2578 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
2579 return MatchOperand_Success;
2582 /// parseProcIFlagsOperand - Try to parse iflags from CPS instruction.
2583 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2584 parseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2585 SMLoc S = Parser.getTok().getLoc();
2586 const AsmToken &Tok = Parser.getTok();
2587 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2588 StringRef IFlagsStr = Tok.getString();
2590 // An iflags string of "none" is interpreted to mean that none of the AIF
2591 // bits are set. Not a terribly useful instruction, but a valid encoding.
2592 unsigned IFlags = 0;
2593 if (IFlagsStr != "none") {
2594 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
2595 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
2596 .Case("a", ARM_PROC::A)
2597 .Case("i", ARM_PROC::I)
2598 .Case("f", ARM_PROC::F)
2601 // If some specific iflag is already set, it means that some letter is
2602 // present more than once, this is not acceptable.
2603 if (Flag == ~0U || (IFlags & Flag))
2604 return MatchOperand_NoMatch;
2610 Parser.Lex(); // Eat identifier token.
2611 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
2612 return MatchOperand_Success;
2615 /// parseMSRMaskOperand - Try to parse mask flags from MSR instruction.
2616 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2617 parseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2618 SMLoc S = Parser.getTok().getLoc();
2619 const AsmToken &Tok = Parser.getTok();
2620 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2621 StringRef Mask = Tok.getString();
2624 // See ARMv6-M 10.1.1
2625 unsigned FlagsVal = StringSwitch<unsigned>(Mask)
2635 .Case("primask", 16)
2636 .Case("basepri", 17)
2637 .Case("basepri_max", 18)
2638 .Case("faultmask", 19)
2639 .Case("control", 20)
2642 if (FlagsVal == ~0U)
2643 return MatchOperand_NoMatch;
2645 if (!hasV7Ops() && FlagsVal >= 17 && FlagsVal <= 19)
2646 // basepri, basepri_max and faultmask only valid for V7m.
2647 return MatchOperand_NoMatch;
2649 Parser.Lex(); // Eat identifier token.
2650 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
2651 return MatchOperand_Success;
2654 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
2655 size_t Start = 0, Next = Mask.find('_');
2656 StringRef Flags = "";
2657 std::string SpecReg = LowercaseString(Mask.slice(Start, Next));
2658 if (Next != StringRef::npos)
2659 Flags = Mask.slice(Next+1, Mask.size());
2661 // FlagsVal contains the complete mask:
2663 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
2664 unsigned FlagsVal = 0;
2666 if (SpecReg == "apsr") {
2667 FlagsVal = StringSwitch<unsigned>(Flags)
2668 .Case("nzcvq", 0x8) // same as CPSR_f
2669 .Case("g", 0x4) // same as CPSR_s
2670 .Case("nzcvqg", 0xc) // same as CPSR_fs
2673 if (FlagsVal == ~0U) {
2675 return MatchOperand_NoMatch;
2677 FlagsVal = 8; // No flag
2679 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
2680 if (Flags == "all") // cpsr_all is an alias for cpsr_fc
2682 for (int i = 0, e = Flags.size(); i != e; ++i) {
2683 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
2690 // If some specific flag is already set, it means that some letter is
2691 // present more than once, this is not acceptable.
2692 if (FlagsVal == ~0U || (FlagsVal & Flag))
2693 return MatchOperand_NoMatch;
2696 } else // No match for special register.
2697 return MatchOperand_NoMatch;
2699 // Special register without flags is NOT equivalent to "fc" flags.
2700 // NOTE: This is a divergence from gas' behavior. Uncommenting the following
2701 // two lines would enable gas compatibility at the expense of breaking
2707 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
2708 if (SpecReg == "spsr")
2711 Parser.Lex(); // Eat identifier token.
2712 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
2713 return MatchOperand_Success;
2716 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2717 parsePKHImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands, StringRef Op,
2718 int Low, int High) {
2719 const AsmToken &Tok = Parser.getTok();
2720 if (Tok.isNot(AsmToken::Identifier)) {
2721 Error(Parser.getTok().getLoc(), Op + " operand expected.");
2722 return MatchOperand_ParseFail;
2724 StringRef ShiftName = Tok.getString();
2725 std::string LowerOp = LowercaseString(Op);
2726 std::string UpperOp = UppercaseString(Op);
2727 if (ShiftName != LowerOp && ShiftName != UpperOp) {
2728 Error(Parser.getTok().getLoc(), Op + " operand expected.");
2729 return MatchOperand_ParseFail;
2731 Parser.Lex(); // Eat shift type token.
2733 // There must be a '#' and a shift amount.
2734 if (Parser.getTok().isNot(AsmToken::Hash)) {
2735 Error(Parser.getTok().getLoc(), "'#' expected");
2736 return MatchOperand_ParseFail;
2738 Parser.Lex(); // Eat hash token.
2740 const MCExpr *ShiftAmount;
2741 SMLoc Loc = Parser.getTok().getLoc();
2742 if (getParser().ParseExpression(ShiftAmount)) {
2743 Error(Loc, "illegal expression");
2744 return MatchOperand_ParseFail;
2746 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
2748 Error(Loc, "constant expression expected");
2749 return MatchOperand_ParseFail;
2751 int Val = CE->getValue();
2752 if (Val < Low || Val > High) {
2753 Error(Loc, "immediate value out of range");
2754 return MatchOperand_ParseFail;
2757 Operands.push_back(ARMOperand::CreateImm(CE, Loc, Parser.getTok().getLoc()));
2759 return MatchOperand_Success;
2762 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2763 parseSetEndImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2764 const AsmToken &Tok = Parser.getTok();
2765 SMLoc S = Tok.getLoc();
2766 if (Tok.isNot(AsmToken::Identifier)) {
2767 Error(Tok.getLoc(), "'be' or 'le' operand expected");
2768 return MatchOperand_ParseFail;
2770 int Val = StringSwitch<int>(Tok.getString())
2774 Parser.Lex(); // Eat the token.
2777 Error(Tok.getLoc(), "'be' or 'le' operand expected");
2778 return MatchOperand_ParseFail;
2780 Operands.push_back(ARMOperand::CreateImm(MCConstantExpr::Create(Val,
2782 S, Parser.getTok().getLoc()));
2783 return MatchOperand_Success;
2786 /// parseShifterImm - Parse the shifter immediate operand for SSAT/USAT
2787 /// instructions. Legal values are:
2788 /// lsl #n 'n' in [0,31]
2789 /// asr #n 'n' in [1,32]
2790 /// n == 32 encoded as n == 0.
2791 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2792 parseShifterImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2793 const AsmToken &Tok = Parser.getTok();
2794 SMLoc S = Tok.getLoc();
2795 if (Tok.isNot(AsmToken::Identifier)) {
2796 Error(S, "shift operator 'asr' or 'lsl' expected");
2797 return MatchOperand_ParseFail;
2799 StringRef ShiftName = Tok.getString();
2801 if (ShiftName == "lsl" || ShiftName == "LSL")
2803 else if (ShiftName == "asr" || ShiftName == "ASR")
2806 Error(S, "shift operator 'asr' or 'lsl' expected");
2807 return MatchOperand_ParseFail;
2809 Parser.Lex(); // Eat the operator.
2811 // A '#' and a shift amount.
2812 if (Parser.getTok().isNot(AsmToken::Hash)) {
2813 Error(Parser.getTok().getLoc(), "'#' expected");
2814 return MatchOperand_ParseFail;
2816 Parser.Lex(); // Eat hash token.
2818 const MCExpr *ShiftAmount;
2819 SMLoc E = Parser.getTok().getLoc();
2820 if (getParser().ParseExpression(ShiftAmount)) {
2821 Error(E, "malformed shift expression");
2822 return MatchOperand_ParseFail;
2824 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
2826 Error(E, "shift amount must be an immediate");
2827 return MatchOperand_ParseFail;
2830 int64_t Val = CE->getValue();
2832 // Shift amount must be in [1,32]
2833 if (Val < 1 || Val > 32) {
2834 Error(E, "'asr' shift amount must be in range [1,32]");
2835 return MatchOperand_ParseFail;
2837 // asr #32 encoded as asr #0, but is not allowed in Thumb2 mode.
2838 if (isThumb() && Val == 32) {
2839 Error(E, "'asr #32' shift amount not allowed in Thumb mode");
2840 return MatchOperand_ParseFail;
2842 if (Val == 32) Val = 0;
2844 // Shift amount must be in [1,32]
2845 if (Val < 0 || Val > 31) {
2846 Error(E, "'lsr' shift amount must be in range [0,31]");
2847 return MatchOperand_ParseFail;
2851 E = Parser.getTok().getLoc();
2852 Operands.push_back(ARMOperand::CreateShifterImm(isASR, Val, S, E));
2854 return MatchOperand_Success;
2857 /// parseRotImm - Parse the shifter immediate operand for SXTB/UXTB family
2858 /// of instructions. Legal values are:
2859 /// ror #n 'n' in {0, 8, 16, 24}
2860 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2861 parseRotImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2862 const AsmToken &Tok = Parser.getTok();
2863 SMLoc S = Tok.getLoc();
2864 if (Tok.isNot(AsmToken::Identifier))
2865 return MatchOperand_NoMatch;
2866 StringRef ShiftName = Tok.getString();
2867 if (ShiftName != "ror" && ShiftName != "ROR")
2868 return MatchOperand_NoMatch;
2869 Parser.Lex(); // Eat the operator.
2871 // A '#' and a rotate amount.
2872 if (Parser.getTok().isNot(AsmToken::Hash)) {
2873 Error(Parser.getTok().getLoc(), "'#' expected");
2874 return MatchOperand_ParseFail;
2876 Parser.Lex(); // Eat hash token.
2878 const MCExpr *ShiftAmount;
2879 SMLoc E = Parser.getTok().getLoc();
2880 if (getParser().ParseExpression(ShiftAmount)) {
2881 Error(E, "malformed rotate expression");
2882 return MatchOperand_ParseFail;
2884 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
2886 Error(E, "rotate amount must be an immediate");
2887 return MatchOperand_ParseFail;
2890 int64_t Val = CE->getValue();
2891 // Shift amount must be in {0, 8, 16, 24} (0 is undocumented extension)
2892 // normally, zero is represented in asm by omitting the rotate operand
2894 if (Val != 8 && Val != 16 && Val != 24 && Val != 0) {
2895 Error(E, "'ror' rotate amount must be 8, 16, or 24");
2896 return MatchOperand_ParseFail;
2899 E = Parser.getTok().getLoc();
2900 Operands.push_back(ARMOperand::CreateRotImm(Val, S, E));
2902 return MatchOperand_Success;
2905 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2906 parseBitfield(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2907 SMLoc S = Parser.getTok().getLoc();
2908 // The bitfield descriptor is really two operands, the LSB and the width.
2909 if (Parser.getTok().isNot(AsmToken::Hash)) {
2910 Error(Parser.getTok().getLoc(), "'#' expected");
2911 return MatchOperand_ParseFail;
2913 Parser.Lex(); // Eat hash token.
2915 const MCExpr *LSBExpr;
2916 SMLoc E = Parser.getTok().getLoc();
2917 if (getParser().ParseExpression(LSBExpr)) {
2918 Error(E, "malformed immediate expression");
2919 return MatchOperand_ParseFail;
2921 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(LSBExpr);
2923 Error(E, "'lsb' operand must be an immediate");
2924 return MatchOperand_ParseFail;
2927 int64_t LSB = CE->getValue();
2928 // The LSB must be in the range [0,31]
2929 if (LSB < 0 || LSB > 31) {
2930 Error(E, "'lsb' operand must be in the range [0,31]");
2931 return MatchOperand_ParseFail;
2933 E = Parser.getTok().getLoc();
2935 // Expect another immediate operand.
2936 if (Parser.getTok().isNot(AsmToken::Comma)) {
2937 Error(Parser.getTok().getLoc(), "too few operands");
2938 return MatchOperand_ParseFail;
2940 Parser.Lex(); // Eat hash token.
2941 if (Parser.getTok().isNot(AsmToken::Hash)) {
2942 Error(Parser.getTok().getLoc(), "'#' expected");
2943 return MatchOperand_ParseFail;
2945 Parser.Lex(); // Eat hash token.
2947 const MCExpr *WidthExpr;
2948 if (getParser().ParseExpression(WidthExpr)) {
2949 Error(E, "malformed immediate expression");
2950 return MatchOperand_ParseFail;
2952 CE = dyn_cast<MCConstantExpr>(WidthExpr);
2954 Error(E, "'width' operand must be an immediate");
2955 return MatchOperand_ParseFail;
2958 int64_t Width = CE->getValue();
2959 // The LSB must be in the range [1,32-lsb]
2960 if (Width < 1 || Width > 32 - LSB) {
2961 Error(E, "'width' operand must be in the range [1,32-lsb]");
2962 return MatchOperand_ParseFail;
2964 E = Parser.getTok().getLoc();
2966 Operands.push_back(ARMOperand::CreateBitfield(LSB, Width, S, E));
2968 return MatchOperand_Success;
2971 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2972 parsePostIdxReg(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2973 // Check for a post-index addressing register operand. Specifically:
2974 // postidx_reg := '+' register {, shift}
2975 // | '-' register {, shift}
2976 // | register {, shift}
2978 // This method must return MatchOperand_NoMatch without consuming any tokens
2979 // in the case where there is no match, as other alternatives take other
2981 AsmToken Tok = Parser.getTok();
2982 SMLoc S = Tok.getLoc();
2983 bool haveEaten = false;
2986 if (Tok.is(AsmToken::Plus)) {
2987 Parser.Lex(); // Eat the '+' token.
2989 } else if (Tok.is(AsmToken::Minus)) {
2990 Parser.Lex(); // Eat the '-' token.
2994 if (Parser.getTok().is(AsmToken::Identifier))
2995 Reg = tryParseRegister();
2998 return MatchOperand_NoMatch;
2999 Error(Parser.getTok().getLoc(), "register expected");
3000 return MatchOperand_ParseFail;
3002 SMLoc E = Parser.getTok().getLoc();
3004 ARM_AM::ShiftOpc ShiftTy = ARM_AM::no_shift;
3005 unsigned ShiftImm = 0;
3006 if (Parser.getTok().is(AsmToken::Comma)) {
3007 Parser.Lex(); // Eat the ','.
3008 if (parseMemRegOffsetShift(ShiftTy, ShiftImm))
3009 return MatchOperand_ParseFail;
3012 Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ShiftTy,
3015 return MatchOperand_Success;
3018 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3019 parseAM3Offset(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3020 // Check for a post-index addressing register operand. Specifically:
3021 // am3offset := '+' register
3028 // This method must return MatchOperand_NoMatch without consuming any tokens
3029 // in the case where there is no match, as other alternatives take other
3031 AsmToken Tok = Parser.getTok();
3032 SMLoc S = Tok.getLoc();
3034 // Do immediates first, as we always parse those if we have a '#'.
3035 if (Parser.getTok().is(AsmToken::Hash)) {
3036 Parser.Lex(); // Eat the '#'.
3037 // Explicitly look for a '-', as we need to encode negative zero
3039 bool isNegative = Parser.getTok().is(AsmToken::Minus);
3040 const MCExpr *Offset;
3041 if (getParser().ParseExpression(Offset))
3042 return MatchOperand_ParseFail;
3043 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
3045 Error(S, "constant expression expected");
3046 return MatchOperand_ParseFail;
3048 SMLoc E = Tok.getLoc();
3049 // Negative zero is encoded as the flag value INT32_MIN.
3050 int32_t Val = CE->getValue();
3051 if (isNegative && Val == 0)
3055 ARMOperand::CreateImm(MCConstantExpr::Create(Val, getContext()), S, E));
3057 return MatchOperand_Success;
3061 bool haveEaten = false;
3064 if (Tok.is(AsmToken::Plus)) {
3065 Parser.Lex(); // Eat the '+' token.
3067 } else if (Tok.is(AsmToken::Minus)) {
3068 Parser.Lex(); // Eat the '-' token.
3072 if (Parser.getTok().is(AsmToken::Identifier))
3073 Reg = tryParseRegister();
3076 return MatchOperand_NoMatch;
3077 Error(Parser.getTok().getLoc(), "register expected");
3078 return MatchOperand_ParseFail;
3080 SMLoc E = Parser.getTok().getLoc();
3082 Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ARM_AM::no_shift,
3085 return MatchOperand_Success;
3088 /// cvtT2LdrdPre - Convert parsed operands to MCInst.
3089 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3090 /// when they refer multiple MIOperands inside a single one.
3092 cvtT2LdrdPre(MCInst &Inst, unsigned Opcode,
3093 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3095 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3096 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3097 // Create a writeback register dummy placeholder.
3098 Inst.addOperand(MCOperand::CreateReg(0));
3100 ((ARMOperand*)Operands[4])->addMemImm8s4OffsetOperands(Inst, 2);
3102 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3106 /// cvtT2StrdPre - Convert parsed operands to MCInst.
3107 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3108 /// when they refer multiple MIOperands inside a single one.
3110 cvtT2StrdPre(MCInst &Inst, unsigned Opcode,
3111 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3112 // Create a writeback register dummy placeholder.
3113 Inst.addOperand(MCOperand::CreateReg(0));
3115 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3116 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3118 ((ARMOperand*)Operands[4])->addMemImm8s4OffsetOperands(Inst, 2);
3120 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3124 /// cvtLdWriteBackRegT2AddrModeImm8 - Convert parsed operands to MCInst.
3125 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3126 /// when they refer multiple MIOperands inside a single one.
3128 cvtLdWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
3129 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3130 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3132 // Create a writeback register dummy placeholder.
3133 Inst.addOperand(MCOperand::CreateImm(0));
3135 ((ARMOperand*)Operands[3])->addMemImm8OffsetOperands(Inst, 2);
3136 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3140 /// cvtStWriteBackRegT2AddrModeImm8 - Convert parsed operands to MCInst.
3141 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3142 /// when they refer multiple MIOperands inside a single one.
3144 cvtStWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
3145 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3146 // Create a writeback register dummy placeholder.
3147 Inst.addOperand(MCOperand::CreateImm(0));
3148 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3149 ((ARMOperand*)Operands[3])->addMemImm8OffsetOperands(Inst, 2);
3150 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3154 /// cvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
3155 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3156 /// when they refer multiple MIOperands inside a single one.
3158 cvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
3159 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3160 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3162 // Create a writeback register dummy placeholder.
3163 Inst.addOperand(MCOperand::CreateImm(0));
3165 ((ARMOperand*)Operands[3])->addAddrMode2Operands(Inst, 3);
3166 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3170 /// cvtLdWriteBackRegAddrModeImm12 - Convert parsed operands to MCInst.
3171 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3172 /// when they refer multiple MIOperands inside a single one.
3174 cvtLdWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
3175 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3176 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3178 // Create a writeback register dummy placeholder.
3179 Inst.addOperand(MCOperand::CreateImm(0));
3181 ((ARMOperand*)Operands[3])->addMemImm12OffsetOperands(Inst, 2);
3182 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3187 /// cvtStWriteBackRegAddrModeImm12 - Convert parsed operands to MCInst.
3188 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3189 /// when they refer multiple MIOperands inside a single one.
3191 cvtStWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
3192 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3193 // Create a writeback register dummy placeholder.
3194 Inst.addOperand(MCOperand::CreateImm(0));
3195 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3196 ((ARMOperand*)Operands[3])->addMemImm12OffsetOperands(Inst, 2);
3197 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3201 /// cvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
3202 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3203 /// when they refer multiple MIOperands inside a single one.
3205 cvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
3206 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3207 // Create a writeback register dummy placeholder.
3208 Inst.addOperand(MCOperand::CreateImm(0));
3209 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3210 ((ARMOperand*)Operands[3])->addAddrMode2Operands(Inst, 3);
3211 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3215 /// cvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
3216 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3217 /// when they refer multiple MIOperands inside a single one.
3219 cvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
3220 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3221 // Create a writeback register dummy placeholder.
3222 Inst.addOperand(MCOperand::CreateImm(0));
3223 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3224 ((ARMOperand*)Operands[3])->addAddrMode3Operands(Inst, 3);
3225 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3229 /// cvtLdExtTWriteBackImm - Convert parsed operands to MCInst.
3230 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3231 /// when they refer multiple MIOperands inside a single one.
3233 cvtLdExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
3234 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3236 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3237 // Create a writeback register dummy placeholder.
3238 Inst.addOperand(MCOperand::CreateImm(0));
3240 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3242 ((ARMOperand*)Operands[4])->addPostIdxImm8Operands(Inst, 1);
3244 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3248 /// cvtLdExtTWriteBackReg - Convert parsed operands to MCInst.
3249 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3250 /// when they refer multiple MIOperands inside a single one.
3252 cvtLdExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
3253 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3255 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3256 // Create a writeback register dummy placeholder.
3257 Inst.addOperand(MCOperand::CreateImm(0));
3259 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3261 ((ARMOperand*)Operands[4])->addPostIdxRegOperands(Inst, 2);
3263 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3267 /// cvtStExtTWriteBackImm - Convert parsed operands to MCInst.
3268 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3269 /// when they refer multiple MIOperands inside a single one.
3271 cvtStExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
3272 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3273 // Create a writeback register dummy placeholder.
3274 Inst.addOperand(MCOperand::CreateImm(0));
3276 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3278 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3280 ((ARMOperand*)Operands[4])->addPostIdxImm8Operands(Inst, 1);
3282 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3286 /// cvtStExtTWriteBackReg - Convert parsed operands to MCInst.
3287 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3288 /// when they refer multiple MIOperands inside a single one.
3290 cvtStExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
3291 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3292 // Create a writeback register dummy placeholder.
3293 Inst.addOperand(MCOperand::CreateImm(0));
3295 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3297 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3299 ((ARMOperand*)Operands[4])->addPostIdxRegOperands(Inst, 2);
3301 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3305 /// cvtLdrdPre - Convert parsed operands to MCInst.
3306 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3307 /// when they refer multiple MIOperands inside a single one.
3309 cvtLdrdPre(MCInst &Inst, unsigned Opcode,
3310 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3312 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3313 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3314 // Create a writeback register dummy placeholder.
3315 Inst.addOperand(MCOperand::CreateImm(0));
3317 ((ARMOperand*)Operands[4])->addAddrMode3Operands(Inst, 3);
3319 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3323 /// cvtStrdPre - Convert parsed operands to MCInst.
3324 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3325 /// when they refer multiple MIOperands inside a single one.
3327 cvtStrdPre(MCInst &Inst, unsigned Opcode,
3328 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3329 // Create a writeback register dummy placeholder.
3330 Inst.addOperand(MCOperand::CreateImm(0));
3332 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3333 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3335 ((ARMOperand*)Operands[4])->addAddrMode3Operands(Inst, 3);
3337 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3341 /// cvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
3342 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3343 /// when they refer multiple MIOperands inside a single one.
3345 cvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
3346 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3347 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3348 // Create a writeback register dummy placeholder.
3349 Inst.addOperand(MCOperand::CreateImm(0));
3350 ((ARMOperand*)Operands[3])->addAddrMode3Operands(Inst, 3);
3351 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3355 /// cvtThumbMultiple- Convert parsed operands to MCInst.
3356 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3357 /// when they refer multiple MIOperands inside a single one.
3359 cvtThumbMultiply(MCInst &Inst, unsigned Opcode,
3360 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3361 // The second source operand must be the same register as the destination
3363 if (Operands.size() == 6 &&
3364 (((ARMOperand*)Operands[3])->getReg() !=
3365 ((ARMOperand*)Operands[5])->getReg()) &&
3366 (((ARMOperand*)Operands[3])->getReg() !=
3367 ((ARMOperand*)Operands[4])->getReg())) {
3368 Error(Operands[3]->getStartLoc(),
3369 "destination register must match source register");
3372 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3373 ((ARMOperand*)Operands[1])->addCCOutOperands(Inst, 1);
3374 ((ARMOperand*)Operands[4])->addRegOperands(Inst, 1);
3375 // If we have a three-operand form, use that, else the second source operand
3376 // is just the destination operand again.
3377 if (Operands.size() == 6)
3378 ((ARMOperand*)Operands[5])->addRegOperands(Inst, 1);
3380 Inst.addOperand(Inst.getOperand(0));
3381 ((ARMOperand*)Operands[2])->addCondCodeOperands(Inst, 2);
3387 cvtVLDwbFixed(MCInst &Inst, unsigned Opcode,
3388 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3390 ((ARMOperand*)Operands[3])->addVecListTwoDOperands(Inst, 1);
3391 // Create a writeback register dummy placeholder.
3392 Inst.addOperand(MCOperand::CreateImm(0));
3394 ((ARMOperand*)Operands[4])->addAlignedMemoryOperands(Inst, 2);
3396 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3401 cvtVLDwbRegister(MCInst &Inst, unsigned Opcode,
3402 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3404 ((ARMOperand*)Operands[3])->addVecListTwoDOperands(Inst, 1);
3405 // Create a writeback register dummy placeholder.
3406 Inst.addOperand(MCOperand::CreateImm(0));
3408 ((ARMOperand*)Operands[4])->addAlignedMemoryOperands(Inst, 2);
3410 ((ARMOperand*)Operands[5])->addRegOperands(Inst, 1);
3412 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3416 /// Parse an ARM memory expression, return false if successful else return true
3417 /// or an error. The first token must be a '[' when called.
3419 parseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3421 assert(Parser.getTok().is(AsmToken::LBrac) &&
3422 "Token is not a Left Bracket");
3423 S = Parser.getTok().getLoc();
3424 Parser.Lex(); // Eat left bracket token.
3426 const AsmToken &BaseRegTok = Parser.getTok();
3427 int BaseRegNum = tryParseRegister();
3428 if (BaseRegNum == -1)
3429 return Error(BaseRegTok.getLoc(), "register expected");
3431 // The next token must either be a comma or a closing bracket.
3432 const AsmToken &Tok = Parser.getTok();
3433 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
3434 return Error(Tok.getLoc(), "malformed memory operand");
3436 if (Tok.is(AsmToken::RBrac)) {
3438 Parser.Lex(); // Eat right bracket token.
3440 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, 0, ARM_AM::no_shift,
3441 0, 0, false, S, E));
3443 // If there's a pre-indexing writeback marker, '!', just add it as a token
3444 // operand. It's rather odd, but syntactically valid.
3445 if (Parser.getTok().is(AsmToken::Exclaim)) {
3446 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3447 Parser.Lex(); // Eat the '!'.
3453 assert(Tok.is(AsmToken::Comma) && "Lost comma in memory operand?!");
3454 Parser.Lex(); // Eat the comma.
3456 // If we have a ':', it's an alignment specifier.
3457 if (Parser.getTok().is(AsmToken::Colon)) {
3458 Parser.Lex(); // Eat the ':'.
3459 E = Parser.getTok().getLoc();
3462 if (getParser().ParseExpression(Expr))
3465 // The expression has to be a constant. Memory references with relocations
3466 // don't come through here, as they use the <label> forms of the relevant
3468 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
3470 return Error (E, "constant expression expected");
3473 switch (CE->getValue()) {
3475 return Error(E, "alignment specifier must be 64, 128, or 256 bits");
3476 case 64: Align = 8; break;
3477 case 128: Align = 16; break;
3478 case 256: Align = 32; break;
3481 // Now we should have the closing ']'
3482 E = Parser.getTok().getLoc();
3483 if (Parser.getTok().isNot(AsmToken::RBrac))
3484 return Error(E, "']' expected");
3485 Parser.Lex(); // Eat right bracket token.
3487 // Don't worry about range checking the value here. That's handled by
3488 // the is*() predicates.
3489 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, 0,
3490 ARM_AM::no_shift, 0, Align,
3493 // If there's a pre-indexing writeback marker, '!', just add it as a token
3495 if (Parser.getTok().is(AsmToken::Exclaim)) {
3496 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3497 Parser.Lex(); // Eat the '!'.
3503 // If we have a '#', it's an immediate offset, else assume it's a register
3505 if (Parser.getTok().is(AsmToken::Hash)) {
3506 Parser.Lex(); // Eat the '#'.
3507 E = Parser.getTok().getLoc();
3509 bool isNegative = getParser().getTok().is(AsmToken::Minus);
3510 const MCExpr *Offset;
3511 if (getParser().ParseExpression(Offset))
3514 // The expression has to be a constant. Memory references with relocations
3515 // don't come through here, as they use the <label> forms of the relevant
3517 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
3519 return Error (E, "constant expression expected");
3521 // If the constant was #-0, represent it as INT32_MIN.
3522 int32_t Val = CE->getValue();
3523 if (isNegative && Val == 0)
3524 CE = MCConstantExpr::Create(INT32_MIN, getContext());
3526 // Now we should have the closing ']'
3527 E = Parser.getTok().getLoc();
3528 if (Parser.getTok().isNot(AsmToken::RBrac))
3529 return Error(E, "']' expected");
3530 Parser.Lex(); // Eat right bracket token.
3532 // Don't worry about range checking the value here. That's handled by
3533 // the is*() predicates.
3534 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, CE, 0,
3535 ARM_AM::no_shift, 0, 0,
3538 // If there's a pre-indexing writeback marker, '!', just add it as a token
3540 if (Parser.getTok().is(AsmToken::Exclaim)) {
3541 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3542 Parser.Lex(); // Eat the '!'.
3548 // The register offset is optionally preceded by a '+' or '-'
3549 bool isNegative = false;
3550 if (Parser.getTok().is(AsmToken::Minus)) {
3552 Parser.Lex(); // Eat the '-'.
3553 } else if (Parser.getTok().is(AsmToken::Plus)) {
3555 Parser.Lex(); // Eat the '+'.
3558 E = Parser.getTok().getLoc();
3559 int OffsetRegNum = tryParseRegister();
3560 if (OffsetRegNum == -1)
3561 return Error(E, "register expected");
3563 // If there's a shift operator, handle it.
3564 ARM_AM::ShiftOpc ShiftType = ARM_AM::no_shift;
3565 unsigned ShiftImm = 0;
3566 if (Parser.getTok().is(AsmToken::Comma)) {
3567 Parser.Lex(); // Eat the ','.
3568 if (parseMemRegOffsetShift(ShiftType, ShiftImm))
3572 // Now we should have the closing ']'
3573 E = Parser.getTok().getLoc();
3574 if (Parser.getTok().isNot(AsmToken::RBrac))
3575 return Error(E, "']' expected");
3576 Parser.Lex(); // Eat right bracket token.
3578 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, OffsetRegNum,
3579 ShiftType, ShiftImm, 0, isNegative,
3582 // If there's a pre-indexing writeback marker, '!', just add it as a token
3584 if (Parser.getTok().is(AsmToken::Exclaim)) {
3585 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3586 Parser.Lex(); // Eat the '!'.
3592 /// parseMemRegOffsetShift - one of these two:
3593 /// ( lsl | lsr | asr | ror ) , # shift_amount
3595 /// return true if it parses a shift otherwise it returns false.
3596 bool ARMAsmParser::parseMemRegOffsetShift(ARM_AM::ShiftOpc &St,
3598 SMLoc Loc = Parser.getTok().getLoc();
3599 const AsmToken &Tok = Parser.getTok();
3600 if (Tok.isNot(AsmToken::Identifier))
3602 StringRef ShiftName = Tok.getString();
3603 if (ShiftName == "lsl" || ShiftName == "LSL")
3605 else if (ShiftName == "lsr" || ShiftName == "LSR")
3607 else if (ShiftName == "asr" || ShiftName == "ASR")
3609 else if (ShiftName == "ror" || ShiftName == "ROR")
3611 else if (ShiftName == "rrx" || ShiftName == "RRX")
3614 return Error(Loc, "illegal shift operator");
3615 Parser.Lex(); // Eat shift type token.
3617 // rrx stands alone.
3619 if (St != ARM_AM::rrx) {
3620 Loc = Parser.getTok().getLoc();
3621 // A '#' and a shift amount.
3622 const AsmToken &HashTok = Parser.getTok();
3623 if (HashTok.isNot(AsmToken::Hash))
3624 return Error(HashTok.getLoc(), "'#' expected");
3625 Parser.Lex(); // Eat hash token.
3628 if (getParser().ParseExpression(Expr))
3630 // Range check the immediate.
3631 // lsl, ror: 0 <= imm <= 31
3632 // lsr, asr: 0 <= imm <= 32
3633 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
3635 return Error(Loc, "shift amount must be an immediate");
3636 int64_t Imm = CE->getValue();
3638 ((St == ARM_AM::lsl || St == ARM_AM::ror) && Imm > 31) ||
3639 ((St == ARM_AM::lsr || St == ARM_AM::asr) && Imm > 32))
3640 return Error(Loc, "immediate shift value out of range");
3647 /// parseFPImm - A floating point immediate expression operand.
3648 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3649 parseFPImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3650 SMLoc S = Parser.getTok().getLoc();
3652 if (Parser.getTok().isNot(AsmToken::Hash))
3653 return MatchOperand_NoMatch;
3655 // Disambiguate the VMOV forms that can accept an FP immediate.
3656 // vmov.f32 <sreg>, #imm
3657 // vmov.f64 <dreg>, #imm
3658 // vmov.f32 <dreg>, #imm @ vector f32x2
3659 // vmov.f32 <qreg>, #imm @ vector f32x4
3661 // There are also the NEON VMOV instructions which expect an
3662 // integer constant. Make sure we don't try to parse an FPImm
3664 // vmov.i{8|16|32|64} <dreg|qreg>, #imm
3665 ARMOperand *TyOp = static_cast<ARMOperand*>(Operands[2]);
3666 if (!TyOp->isToken() || (TyOp->getToken() != ".f32" &&
3667 TyOp->getToken() != ".f64"))
3668 return MatchOperand_NoMatch;
3670 Parser.Lex(); // Eat the '#'.
3672 // Handle negation, as that still comes through as a separate token.
3673 bool isNegative = false;
3674 if (Parser.getTok().is(AsmToken::Minus)) {
3678 const AsmToken &Tok = Parser.getTok();
3679 if (Tok.is(AsmToken::Real)) {
3680 APFloat RealVal(APFloat::IEEEdouble, Tok.getString());
3681 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
3682 // If we had a '-' in front, toggle the sign bit.
3683 IntVal ^= (uint64_t)isNegative << 63;
3684 int Val = ARM_AM::getFP64Imm(APInt(64, IntVal));
3685 Parser.Lex(); // Eat the token.
3687 TokError("floating point value out of range");
3688 return MatchOperand_ParseFail;
3690 Operands.push_back(ARMOperand::CreateFPImm(Val, S, getContext()));
3691 return MatchOperand_Success;
3693 if (Tok.is(AsmToken::Integer)) {
3694 int64_t Val = Tok.getIntVal();
3695 Parser.Lex(); // Eat the token.
3696 if (Val > 255 || Val < 0) {
3697 TokError("encoded floating point value out of range");
3698 return MatchOperand_ParseFail;
3700 Operands.push_back(ARMOperand::CreateFPImm(Val, S, getContext()));
3701 return MatchOperand_Success;
3704 TokError("invalid floating point immediate");
3705 return MatchOperand_ParseFail;
3707 /// Parse a arm instruction operand. For now this parses the operand regardless
3708 /// of the mnemonic.
3709 bool ARMAsmParser::parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
3710 StringRef Mnemonic) {
3713 // Check if the current operand has a custom associated parser, if so, try to
3714 // custom parse the operand, or fallback to the general approach.
3715 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
3716 if (ResTy == MatchOperand_Success)
3718 // If there wasn't a custom match, try the generic matcher below. Otherwise,
3719 // there was a match, but an error occurred, in which case, just return that
3720 // the operand parsing failed.
3721 if (ResTy == MatchOperand_ParseFail)
3724 switch (getLexer().getKind()) {
3726 Error(Parser.getTok().getLoc(), "unexpected token in operand");
3728 case AsmToken::Identifier: {
3729 // If this is VMRS, check for the apsr_nzcv operand.
3730 if (!tryParseRegisterWithWriteBack(Operands))
3732 int Res = tryParseShiftRegister(Operands);
3733 if (Res == 0) // success
3735 else if (Res == -1) // irrecoverable error
3737 if (Mnemonic == "vmrs" && Parser.getTok().getString() == "apsr_nzcv") {
3738 S = Parser.getTok().getLoc();
3740 Operands.push_back(ARMOperand::CreateToken("apsr_nzcv", S));
3744 // Fall though for the Identifier case that is not a register or a
3747 case AsmToken::LParen: // parenthesized expressions like (_strcmp-4)
3748 case AsmToken::Integer: // things like 1f and 2b as a branch targets
3749 case AsmToken::Dot: { // . as a branch target
3750 // This was not a register so parse other operands that start with an
3751 // identifier (like labels) as expressions and create them as immediates.
3752 const MCExpr *IdVal;
3753 S = Parser.getTok().getLoc();
3754 if (getParser().ParseExpression(IdVal))
3756 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3757 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
3760 case AsmToken::LBrac:
3761 return parseMemory(Operands);
3762 case AsmToken::LCurly:
3763 return parseRegisterList(Operands);
3764 case AsmToken::Hash: {
3765 // #42 -> immediate.
3766 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
3767 S = Parser.getTok().getLoc();
3769 bool isNegative = Parser.getTok().is(AsmToken::Minus);
3770 const MCExpr *ImmVal;
3771 if (getParser().ParseExpression(ImmVal))
3773 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ImmVal);
3775 Error(S, "constant expression expected");
3776 return MatchOperand_ParseFail;
3778 int32_t Val = CE->getValue();
3779 if (isNegative && Val == 0)
3780 ImmVal = MCConstantExpr::Create(INT32_MIN, getContext());
3781 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3782 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
3785 case AsmToken::Colon: {
3786 // ":lower16:" and ":upper16:" expression prefixes
3787 // FIXME: Check it's an expression prefix,
3788 // e.g. (FOO - :lower16:BAR) isn't legal.
3789 ARMMCExpr::VariantKind RefKind;
3790 if (parsePrefix(RefKind))
3793 const MCExpr *SubExprVal;
3794 if (getParser().ParseExpression(SubExprVal))
3797 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
3799 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3800 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
3806 // parsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
3807 // :lower16: and :upper16:.
3808 bool ARMAsmParser::parsePrefix(ARMMCExpr::VariantKind &RefKind) {
3809 RefKind = ARMMCExpr::VK_ARM_None;
3811 // :lower16: and :upper16: modifiers
3812 assert(getLexer().is(AsmToken::Colon) && "expected a :");
3813 Parser.Lex(); // Eat ':'
3815 if (getLexer().isNot(AsmToken::Identifier)) {
3816 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
3820 StringRef IDVal = Parser.getTok().getIdentifier();
3821 if (IDVal == "lower16") {
3822 RefKind = ARMMCExpr::VK_ARM_LO16;
3823 } else if (IDVal == "upper16") {
3824 RefKind = ARMMCExpr::VK_ARM_HI16;
3826 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
3831 if (getLexer().isNot(AsmToken::Colon)) {
3832 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
3835 Parser.Lex(); // Eat the last ':'
3839 /// \brief Given a mnemonic, split out possible predication code and carry
3840 /// setting letters to form a canonical mnemonic and flags.
3842 // FIXME: Would be nice to autogen this.
3843 // FIXME: This is a bit of a maze of special cases.
3844 StringRef ARMAsmParser::splitMnemonic(StringRef Mnemonic,
3845 unsigned &PredicationCode,
3847 unsigned &ProcessorIMod,
3848 StringRef &ITMask) {
3849 PredicationCode = ARMCC::AL;
3850 CarrySetting = false;
3853 // Ignore some mnemonics we know aren't predicated forms.
3855 // FIXME: Would be nice to autogen this.
3856 if ((Mnemonic == "movs" && isThumb()) ||
3857 Mnemonic == "teq" || Mnemonic == "vceq" || Mnemonic == "svc" ||
3858 Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
3859 Mnemonic == "vmls" || Mnemonic == "vnmls" || Mnemonic == "vacge" ||
3860 Mnemonic == "vcge" || Mnemonic == "vclt" || Mnemonic == "vacgt" ||
3861 Mnemonic == "vcgt" || Mnemonic == "vcle" || Mnemonic == "smlal" ||
3862 Mnemonic == "umaal" || Mnemonic == "umlal" || Mnemonic == "vabal" ||
3863 Mnemonic == "vmlal" || Mnemonic == "vpadal" || Mnemonic == "vqdmlal")
3866 // First, split out any predication code. Ignore mnemonics we know aren't
3867 // predicated but do have a carry-set and so weren't caught above.
3868 if (Mnemonic != "adcs" && Mnemonic != "bics" && Mnemonic != "movs" &&
3869 Mnemonic != "muls" && Mnemonic != "smlals" && Mnemonic != "smulls" &&
3870 Mnemonic != "umlals" && Mnemonic != "umulls" && Mnemonic != "lsls" &&
3871 Mnemonic != "sbcs" && Mnemonic != "rscs") {
3872 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
3873 .Case("eq", ARMCC::EQ)
3874 .Case("ne", ARMCC::NE)
3875 .Case("hs", ARMCC::HS)
3876 .Case("cs", ARMCC::HS)
3877 .Case("lo", ARMCC::LO)
3878 .Case("cc", ARMCC::LO)
3879 .Case("mi", ARMCC::MI)
3880 .Case("pl", ARMCC::PL)
3881 .Case("vs", ARMCC::VS)
3882 .Case("vc", ARMCC::VC)
3883 .Case("hi", ARMCC::HI)
3884 .Case("ls", ARMCC::LS)
3885 .Case("ge", ARMCC::GE)
3886 .Case("lt", ARMCC::LT)
3887 .Case("gt", ARMCC::GT)
3888 .Case("le", ARMCC::LE)
3889 .Case("al", ARMCC::AL)
3892 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
3893 PredicationCode = CC;
3897 // Next, determine if we have a carry setting bit. We explicitly ignore all
3898 // the instructions we know end in 's'.
3899 if (Mnemonic.endswith("s") &&
3900 !(Mnemonic == "cps" || Mnemonic == "mls" ||
3901 Mnemonic == "mrs" || Mnemonic == "smmls" || Mnemonic == "vabs" ||
3902 Mnemonic == "vcls" || Mnemonic == "vmls" || Mnemonic == "vmrs" ||
3903 Mnemonic == "vnmls" || Mnemonic == "vqabs" || Mnemonic == "vrecps" ||
3904 Mnemonic == "vrsqrts" || Mnemonic == "srs" ||
3905 (Mnemonic == "movs" && isThumb()))) {
3906 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
3907 CarrySetting = true;
3910 // The "cps" instruction can have a interrupt mode operand which is glued into
3911 // the mnemonic. Check if this is the case, split it and parse the imod op
3912 if (Mnemonic.startswith("cps")) {
3913 // Split out any imod code.
3915 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
3916 .Case("ie", ARM_PROC::IE)
3917 .Case("id", ARM_PROC::ID)
3920 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
3921 ProcessorIMod = IMod;
3925 // The "it" instruction has the condition mask on the end of the mnemonic.
3926 if (Mnemonic.startswith("it")) {
3927 ITMask = Mnemonic.slice(2, Mnemonic.size());
3928 Mnemonic = Mnemonic.slice(0, 2);
3934 /// \brief Given a canonical mnemonic, determine if the instruction ever allows
3935 /// inclusion of carry set or predication code operands.
3937 // FIXME: It would be nice to autogen this.
3939 getMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
3940 bool &CanAcceptPredicationCode) {
3941 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
3942 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
3943 Mnemonic == "add" || Mnemonic == "adc" ||
3944 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
3945 Mnemonic == "orr" || Mnemonic == "mvn" ||
3946 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
3947 Mnemonic == "sbc" || Mnemonic == "eor" || Mnemonic == "neg" ||
3948 (!isThumb() && (Mnemonic == "smull" || Mnemonic == "mov" ||
3949 Mnemonic == "mla" || Mnemonic == "smlal" ||
3950 Mnemonic == "umlal" || Mnemonic == "umull"))) {
3951 CanAcceptCarrySet = true;
3953 CanAcceptCarrySet = false;
3955 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
3956 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
3957 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
3958 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
3959 Mnemonic == "dsb" || Mnemonic == "isb" || Mnemonic == "setend" ||
3960 (Mnemonic == "clrex" && !isThumb()) ||
3961 (Mnemonic == "nop" && isThumbOne()) ||
3962 ((Mnemonic == "pld" || Mnemonic == "pli" || Mnemonic == "pldw" ||
3963 Mnemonic == "ldc2" || Mnemonic == "ldc2l" ||
3964 Mnemonic == "stc2" || Mnemonic == "stc2l") && !isThumb()) ||
3965 ((Mnemonic.startswith("rfe") || Mnemonic.startswith("srs")) &&
3967 Mnemonic.startswith("cps") || (Mnemonic == "movs" && isThumbOne())) {
3968 CanAcceptPredicationCode = false;
3970 CanAcceptPredicationCode = true;
3973 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
3974 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
3975 CanAcceptPredicationCode = false;
3979 bool ARMAsmParser::shouldOmitCCOutOperand(StringRef Mnemonic,
3980 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3981 // FIXME: This is all horribly hacky. We really need a better way to deal
3982 // with optional operands like this in the matcher table.
3984 // The 'mov' mnemonic is special. One variant has a cc_out operand, while
3985 // another does not. Specifically, the MOVW instruction does not. So we
3986 // special case it here and remove the defaulted (non-setting) cc_out
3987 // operand if that's the instruction we're trying to match.
3989 // We do this as post-processing of the explicit operands rather than just
3990 // conditionally adding the cc_out in the first place because we need
3991 // to check the type of the parsed immediate operand.
3992 if (Mnemonic == "mov" && Operands.size() > 4 && !isThumb() &&
3993 !static_cast<ARMOperand*>(Operands[4])->isARMSOImm() &&
3994 static_cast<ARMOperand*>(Operands[4])->isImm0_65535Expr() &&
3995 static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
3998 // Register-register 'add' for thumb does not have a cc_out operand
3999 // when there are only two register operands.
4000 if (isThumb() && Mnemonic == "add" && Operands.size() == 5 &&
4001 static_cast<ARMOperand*>(Operands[3])->isReg() &&
4002 static_cast<ARMOperand*>(Operands[4])->isReg() &&
4003 static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
4005 // Register-register 'add' for thumb does not have a cc_out operand
4006 // when it's an ADD Rdm, SP, {Rdm|#imm0_255} instruction. We do
4007 // have to check the immediate range here since Thumb2 has a variant
4008 // that can handle a different range and has a cc_out operand.
4009 if (((isThumb() && Mnemonic == "add") ||
4010 (isThumbTwo() && Mnemonic == "sub")) &&
4011 Operands.size() == 6 &&
4012 static_cast<ARMOperand*>(Operands[3])->isReg() &&
4013 static_cast<ARMOperand*>(Operands[4])->isReg() &&
4014 static_cast<ARMOperand*>(Operands[4])->getReg() == ARM::SP &&
4015 static_cast<ARMOperand*>(Operands[1])->getReg() == 0 &&
4016 (static_cast<ARMOperand*>(Operands[5])->isReg() ||
4017 static_cast<ARMOperand*>(Operands[5])->isImm0_1020s4()))
4019 // For Thumb2, add/sub immediate does not have a cc_out operand for the
4020 // imm0_4095 variant. That's the least-preferred variant when
4021 // selecting via the generic "add" mnemonic, so to know that we
4022 // should remove the cc_out operand, we have to explicitly check that
4023 // it's not one of the other variants. Ugh.
4024 if (isThumbTwo() && (Mnemonic == "add" || Mnemonic == "sub") &&
4025 Operands.size() == 6 &&
4026 static_cast<ARMOperand*>(Operands[3])->isReg() &&
4027 static_cast<ARMOperand*>(Operands[4])->isReg() &&
4028 static_cast<ARMOperand*>(Operands[5])->isImm()) {
4029 // Nest conditions rather than one big 'if' statement for readability.
4031 // If either register is a high reg, it's either one of the SP
4032 // variants (handled above) or a 32-bit encoding, so we just
4033 // check against T3.
4034 if ((!isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) ||
4035 !isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg())) &&
4036 static_cast<ARMOperand*>(Operands[5])->isT2SOImm())
4038 // If both registers are low, we're in an IT block, and the immediate is
4039 // in range, we should use encoding T1 instead, which has a cc_out.
4041 isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) &&
4042 isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg()) &&
4043 static_cast<ARMOperand*>(Operands[5])->isImm0_7())
4046 // Otherwise, we use encoding T4, which does not have a cc_out
4051 // The thumb2 multiply instruction doesn't have a CCOut register, so
4052 // if we have a "mul" mnemonic in Thumb mode, check if we'll be able to
4053 // use the 16-bit encoding or not.
4054 if (isThumbTwo() && Mnemonic == "mul" && Operands.size() == 6 &&
4055 static_cast<ARMOperand*>(Operands[1])->getReg() == 0 &&
4056 static_cast<ARMOperand*>(Operands[3])->isReg() &&
4057 static_cast<ARMOperand*>(Operands[4])->isReg() &&
4058 static_cast<ARMOperand*>(Operands[5])->isReg() &&
4059 // If the registers aren't low regs, the destination reg isn't the
4060 // same as one of the source regs, or the cc_out operand is zero
4061 // outside of an IT block, we have to use the 32-bit encoding, so
4062 // remove the cc_out operand.
4063 (!isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) ||
4064 !isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg()) ||
4066 (static_cast<ARMOperand*>(Operands[3])->getReg() !=
4067 static_cast<ARMOperand*>(Operands[5])->getReg() &&
4068 static_cast<ARMOperand*>(Operands[3])->getReg() !=
4069 static_cast<ARMOperand*>(Operands[4])->getReg())))
4074 // Register-register 'add/sub' for thumb does not have a cc_out operand
4075 // when it's an ADD/SUB SP, #imm. Be lenient on count since there's also
4076 // the "add/sub SP, SP, #imm" version. If the follow-up operands aren't
4077 // right, this will result in better diagnostics (which operand is off)
4079 if (isThumb() && (Mnemonic == "add" || Mnemonic == "sub") &&
4080 (Operands.size() == 5 || Operands.size() == 6) &&
4081 static_cast<ARMOperand*>(Operands[3])->isReg() &&
4082 static_cast<ARMOperand*>(Operands[3])->getReg() == ARM::SP &&
4083 static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
4089 /// Parse an arm instruction mnemonic followed by its operands.
4090 bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
4091 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
4092 // Create the leading tokens for the mnemonic, split by '.' characters.
4093 size_t Start = 0, Next = Name.find('.');
4094 StringRef Mnemonic = Name.slice(Start, Next);
4096 // Split out the predication code and carry setting flag from the mnemonic.
4097 unsigned PredicationCode;
4098 unsigned ProcessorIMod;
4101 Mnemonic = splitMnemonic(Mnemonic, PredicationCode, CarrySetting,
4102 ProcessorIMod, ITMask);
4104 // In Thumb1, only the branch (B) instruction can be predicated.
4105 if (isThumbOne() && PredicationCode != ARMCC::AL && Mnemonic != "b") {
4106 Parser.EatToEndOfStatement();
4107 return Error(NameLoc, "conditional execution not supported in Thumb1");
4110 Operands.push_back(ARMOperand::CreateToken(Mnemonic, NameLoc));
4112 // Handle the IT instruction ITMask. Convert it to a bitmask. This
4113 // is the mask as it will be for the IT encoding if the conditional
4114 // encoding has a '1' as it's bit0 (i.e. 't' ==> '1'). In the case
4115 // where the conditional bit0 is zero, the instruction post-processing
4116 // will adjust the mask accordingly.
4117 if (Mnemonic == "it") {
4118 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + 2);
4119 if (ITMask.size() > 3) {
4120 Parser.EatToEndOfStatement();
4121 return Error(Loc, "too many conditions on IT instruction");
4124 for (unsigned i = ITMask.size(); i != 0; --i) {
4125 char pos = ITMask[i - 1];
4126 if (pos != 't' && pos != 'e') {
4127 Parser.EatToEndOfStatement();
4128 return Error(Loc, "illegal IT block condition mask '" + ITMask + "'");
4131 if (ITMask[i - 1] == 't')
4134 Operands.push_back(ARMOperand::CreateITMask(Mask, Loc));
4137 // FIXME: This is all a pretty gross hack. We should automatically handle
4138 // optional operands like this via tblgen.
4140 // Next, add the CCOut and ConditionCode operands, if needed.
4142 // For mnemonics which can ever incorporate a carry setting bit or predication
4143 // code, our matching model involves us always generating CCOut and
4144 // ConditionCode operands to match the mnemonic "as written" and then we let
4145 // the matcher deal with finding the right instruction or generating an
4146 // appropriate error.
4147 bool CanAcceptCarrySet, CanAcceptPredicationCode;
4148 getMnemonicAcceptInfo(Mnemonic, CanAcceptCarrySet, CanAcceptPredicationCode);
4150 // If we had a carry-set on an instruction that can't do that, issue an
4152 if (!CanAcceptCarrySet && CarrySetting) {
4153 Parser.EatToEndOfStatement();
4154 return Error(NameLoc, "instruction '" + Mnemonic +
4155 "' can not set flags, but 's' suffix specified");
4157 // If we had a predication code on an instruction that can't do that, issue an
4159 if (!CanAcceptPredicationCode && PredicationCode != ARMCC::AL) {
4160 Parser.EatToEndOfStatement();
4161 return Error(NameLoc, "instruction '" + Mnemonic +
4162 "' is not predicable, but condition code specified");
4165 // Add the carry setting operand, if necessary.
4166 if (CanAcceptCarrySet) {
4167 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size());
4168 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
4172 // Add the predication code operand, if necessary.
4173 if (CanAcceptPredicationCode) {
4174 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size() +
4176 Operands.push_back(ARMOperand::CreateCondCode(
4177 ARMCC::CondCodes(PredicationCode), Loc));
4180 // Add the processor imod operand, if necessary.
4181 if (ProcessorIMod) {
4182 Operands.push_back(ARMOperand::CreateImm(
4183 MCConstantExpr::Create(ProcessorIMod, getContext()),
4187 // Add the remaining tokens in the mnemonic.
4188 while (Next != StringRef::npos) {
4190 Next = Name.find('.', Start + 1);
4191 StringRef ExtraToken = Name.slice(Start, Next);
4193 // For now, we're only parsing Thumb1 (for the most part), so
4194 // just ignore ".n" qualifiers. We'll use them to restrict
4195 // matching when we do Thumb2.
4196 if (ExtraToken != ".n") {
4197 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Start);
4198 Operands.push_back(ARMOperand::CreateToken(ExtraToken, Loc));
4202 // Read the remaining operands.
4203 if (getLexer().isNot(AsmToken::EndOfStatement)) {
4204 // Read the first operand.
4205 if (parseOperand(Operands, Mnemonic)) {
4206 Parser.EatToEndOfStatement();
4210 while (getLexer().is(AsmToken::Comma)) {
4211 Parser.Lex(); // Eat the comma.
4213 // Parse and remember the operand.
4214 if (parseOperand(Operands, Mnemonic)) {
4215 Parser.EatToEndOfStatement();
4221 if (getLexer().isNot(AsmToken::EndOfStatement)) {
4222 SMLoc Loc = getLexer().getLoc();
4223 Parser.EatToEndOfStatement();
4224 return Error(Loc, "unexpected token in argument list");
4227 Parser.Lex(); // Consume the EndOfStatement
4229 // Some instructions, mostly Thumb, have forms for the same mnemonic that
4230 // do and don't have a cc_out optional-def operand. With some spot-checks
4231 // of the operand list, we can figure out which variant we're trying to
4232 // parse and adjust accordingly before actually matching. We shouldn't ever
4233 // try to remove a cc_out operand that was explicitly set on the the
4234 // mnemonic, of course (CarrySetting == true). Reason number #317 the
4235 // table driven matcher doesn't fit well with the ARM instruction set.
4236 if (!CarrySetting && shouldOmitCCOutOperand(Mnemonic, Operands)) {
4237 ARMOperand *Op = static_cast<ARMOperand*>(Operands[1]);
4238 Operands.erase(Operands.begin() + 1);
4242 // ARM mode 'blx' need special handling, as the register operand version
4243 // is predicable, but the label operand version is not. So, we can't rely
4244 // on the Mnemonic based checking to correctly figure out when to put
4245 // a k_CondCode operand in the list. If we're trying to match the label
4246 // version, remove the k_CondCode operand here.
4247 if (!isThumb() && Mnemonic == "blx" && Operands.size() == 3 &&
4248 static_cast<ARMOperand*>(Operands[2])->isImm()) {
4249 ARMOperand *Op = static_cast<ARMOperand*>(Operands[1]);
4250 Operands.erase(Operands.begin() + 1);
4254 // The vector-compare-to-zero instructions have a literal token "#0" at
4255 // the end that comes to here as an immediate operand. Convert it to a
4256 // token to play nicely with the matcher.
4257 if ((Mnemonic == "vceq" || Mnemonic == "vcge" || Mnemonic == "vcgt" ||
4258 Mnemonic == "vcle" || Mnemonic == "vclt") && Operands.size() == 6 &&
4259 static_cast<ARMOperand*>(Operands[5])->isImm()) {
4260 ARMOperand *Op = static_cast<ARMOperand*>(Operands[5]);
4261 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
4262 if (CE && CE->getValue() == 0) {
4263 Operands.erase(Operands.begin() + 5);
4264 Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
4268 // VCMP{E} does the same thing, but with a different operand count.
4269 if ((Mnemonic == "vcmp" || Mnemonic == "vcmpe") && Operands.size() == 5 &&
4270 static_cast<ARMOperand*>(Operands[4])->isImm()) {
4271 ARMOperand *Op = static_cast<ARMOperand*>(Operands[4]);
4272 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
4273 if (CE && CE->getValue() == 0) {
4274 Operands.erase(Operands.begin() + 4);
4275 Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
4279 // Similarly, the Thumb1 "RSB" instruction has a literal "#0" on the
4280 // end. Convert it to a token here.
4281 if (Mnemonic == "rsb" && isThumb() && Operands.size() == 6 &&
4282 static_cast<ARMOperand*>(Operands[5])->isImm()) {
4283 ARMOperand *Op = static_cast<ARMOperand*>(Operands[5]);
4284 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
4285 if (CE && CE->getValue() == 0) {
4286 Operands.erase(Operands.begin() + 5);
4287 Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
4295 // Validate context-sensitive operand constraints.
4297 // return 'true' if register list contains non-low GPR registers,
4298 // 'false' otherwise. If Reg is in the register list or is HiReg, set
4299 // 'containsReg' to true.
4300 static bool checkLowRegisterList(MCInst Inst, unsigned OpNo, unsigned Reg,
4301 unsigned HiReg, bool &containsReg) {
4302 containsReg = false;
4303 for (unsigned i = OpNo; i < Inst.getNumOperands(); ++i) {
4304 unsigned OpReg = Inst.getOperand(i).getReg();
4307 // Anything other than a low register isn't legal here.
4308 if (!isARMLowRegister(OpReg) && (!HiReg || OpReg != HiReg))
4314 // Check if the specified regisgter is in the register list of the inst,
4315 // starting at the indicated operand number.
4316 static bool listContainsReg(MCInst &Inst, unsigned OpNo, unsigned Reg) {
4317 for (unsigned i = OpNo; i < Inst.getNumOperands(); ++i) {
4318 unsigned OpReg = Inst.getOperand(i).getReg();
4325 // FIXME: We would really prefer to have MCInstrInfo (the wrapper around
4326 // the ARMInsts array) instead. Getting that here requires awkward
4327 // API changes, though. Better way?
4329 extern const MCInstrDesc ARMInsts[];
4331 static const MCInstrDesc &getInstDesc(unsigned Opcode) {
4332 return ARMInsts[Opcode];
4335 // FIXME: We would really like to be able to tablegen'erate this.
4337 validateInstruction(MCInst &Inst,
4338 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
4339 const MCInstrDesc &MCID = getInstDesc(Inst.getOpcode());
4340 SMLoc Loc = Operands[0]->getStartLoc();
4341 // Check the IT block state first.
4342 // NOTE: In Thumb mode, the BKPT instruction has the interesting property of
4343 // being allowed in IT blocks, but not being predicable. It just always
4345 if (inITBlock() && Inst.getOpcode() != ARM::tBKPT) {
4347 if (ITState.FirstCond)
4348 ITState.FirstCond = false;
4350 bit = (ITState.Mask >> (5 - ITState.CurPosition)) & 1;
4351 // The instruction must be predicable.
4352 if (!MCID.isPredicable())
4353 return Error(Loc, "instructions in IT block must be predicable");
4354 unsigned Cond = Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm();
4355 unsigned ITCond = bit ? ITState.Cond :
4356 ARMCC::getOppositeCondition(ITState.Cond);
4357 if (Cond != ITCond) {
4358 // Find the condition code Operand to get its SMLoc information.
4360 for (unsigned i = 1; i < Operands.size(); ++i)
4361 if (static_cast<ARMOperand*>(Operands[i])->isCondCode())
4362 CondLoc = Operands[i]->getStartLoc();
4363 return Error(CondLoc, "incorrect condition in IT block; got '" +
4364 StringRef(ARMCondCodeToString(ARMCC::CondCodes(Cond))) +
4365 "', but expected '" +
4366 ARMCondCodeToString(ARMCC::CondCodes(ITCond)) + "'");
4368 // Check for non-'al' condition codes outside of the IT block.
4369 } else if (isThumbTwo() && MCID.isPredicable() &&
4370 Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm() !=
4371 ARMCC::AL && Inst.getOpcode() != ARM::tB &&
4372 Inst.getOpcode() != ARM::t2B)
4373 return Error(Loc, "predicated instructions must be in IT block");
4375 switch (Inst.getOpcode()) {
4378 case ARM::LDRD_POST:
4380 // Rt2 must be Rt + 1.
4381 unsigned Rt = getARMRegisterNumbering(Inst.getOperand(0).getReg());
4382 unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(1).getReg());
4384 return Error(Operands[3]->getStartLoc(),
4385 "destination operands must be sequential");
4389 // Rt2 must be Rt + 1.
4390 unsigned Rt = getARMRegisterNumbering(Inst.getOperand(0).getReg());
4391 unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(1).getReg());
4393 return Error(Operands[3]->getStartLoc(),
4394 "source operands must be sequential");
4398 case ARM::STRD_POST:
4400 // Rt2 must be Rt + 1.
4401 unsigned Rt = getARMRegisterNumbering(Inst.getOperand(1).getReg());
4402 unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(2).getReg());
4404 return Error(Operands[3]->getStartLoc(),
4405 "source operands must be sequential");
4410 // width must be in range [1, 32-lsb]
4411 unsigned lsb = Inst.getOperand(2).getImm();
4412 unsigned widthm1 = Inst.getOperand(3).getImm();
4413 if (widthm1 >= 32 - lsb)
4414 return Error(Operands[5]->getStartLoc(),
4415 "bitfield width must be in range [1,32-lsb]");
4419 // If we're parsing Thumb2, the .w variant is available and handles
4420 // most cases that are normally illegal for a Thumb1 LDM
4421 // instruction. We'll make the transformation in processInstruction()
4424 // Thumb LDM instructions are writeback iff the base register is not
4425 // in the register list.
4426 unsigned Rn = Inst.getOperand(0).getReg();
4427 bool hasWritebackToken =
4428 (static_cast<ARMOperand*>(Operands[3])->isToken() &&
4429 static_cast<ARMOperand*>(Operands[3])->getToken() == "!");
4430 bool listContainsBase;
4431 if (checkLowRegisterList(Inst, 3, Rn, 0, listContainsBase) && !isThumbTwo())
4432 return Error(Operands[3 + hasWritebackToken]->getStartLoc(),
4433 "registers must be in range r0-r7");
4434 // If we should have writeback, then there should be a '!' token.
4435 if (!listContainsBase && !hasWritebackToken && !isThumbTwo())
4436 return Error(Operands[2]->getStartLoc(),
4437 "writeback operator '!' expected");
4438 // If we should not have writeback, there must not be a '!'. This is
4439 // true even for the 32-bit wide encodings.
4440 if (listContainsBase && hasWritebackToken)
4441 return Error(Operands[3]->getStartLoc(),
4442 "writeback operator '!' not allowed when base register "
4443 "in register list");
4447 case ARM::t2LDMIA_UPD: {
4448 if (listContainsReg(Inst, 3, Inst.getOperand(0).getReg()))
4449 return Error(Operands[4]->getStartLoc(),
4450 "writeback operator '!' not allowed when base register "
4451 "in register list");
4455 bool listContainsBase;
4456 if (checkLowRegisterList(Inst, 3, 0, ARM::PC, listContainsBase))
4457 return Error(Operands[2]->getStartLoc(),
4458 "registers must be in range r0-r7 or pc");
4462 bool listContainsBase;
4463 if (checkLowRegisterList(Inst, 3, 0, ARM::LR, listContainsBase))
4464 return Error(Operands[2]->getStartLoc(),
4465 "registers must be in range r0-r7 or lr");
4468 case ARM::tSTMIA_UPD: {
4469 bool listContainsBase;
4470 if (checkLowRegisterList(Inst, 4, 0, 0, listContainsBase) && !isThumbTwo())
4471 return Error(Operands[4]->getStartLoc(),
4472 "registers must be in range r0-r7");
4481 processInstruction(MCInst &Inst,
4482 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
4483 switch (Inst.getOpcode()) {
4484 case ARM::LDMIA_UPD:
4485 // If this is a load of a single register via a 'pop', then we should use
4486 // a post-indexed LDR instruction instead, per the ARM ARM.
4487 if (static_cast<ARMOperand*>(Operands[0])->getToken() == "pop" &&
4488 Inst.getNumOperands() == 5) {
4490 TmpInst.setOpcode(ARM::LDR_POST_IMM);
4491 TmpInst.addOperand(Inst.getOperand(4)); // Rt
4492 TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
4493 TmpInst.addOperand(Inst.getOperand(1)); // Rn
4494 TmpInst.addOperand(MCOperand::CreateReg(0)); // am2offset
4495 TmpInst.addOperand(MCOperand::CreateImm(4));
4496 TmpInst.addOperand(Inst.getOperand(2)); // CondCode
4497 TmpInst.addOperand(Inst.getOperand(3));
4501 case ARM::STMDB_UPD:
4502 // If this is a store of a single register via a 'push', then we should use
4503 // a pre-indexed STR instruction instead, per the ARM ARM.
4504 if (static_cast<ARMOperand*>(Operands[0])->getToken() == "push" &&
4505 Inst.getNumOperands() == 5) {
4507 TmpInst.setOpcode(ARM::STR_PRE_IMM);
4508 TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
4509 TmpInst.addOperand(Inst.getOperand(4)); // Rt
4510 TmpInst.addOperand(Inst.getOperand(1)); // addrmode_imm12
4511 TmpInst.addOperand(MCOperand::CreateImm(-4));
4512 TmpInst.addOperand(Inst.getOperand(2)); // CondCode
4513 TmpInst.addOperand(Inst.getOperand(3));
4518 // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
4519 // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
4520 // to encoding T2 if <Rd> is specified and encoding T2 is preferred
4521 // to encoding T1 if <Rd> is omitted."
4522 if (Inst.getOperand(3).getImm() < 8 && Operands.size() == 6)
4523 Inst.setOpcode(ARM::tADDi3);
4526 // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
4527 // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
4528 // to encoding T2 if <Rd> is specified and encoding T2 is preferred
4529 // to encoding T1 if <Rd> is omitted."
4530 if (Inst.getOperand(3).getImm() < 8 && Operands.size() == 6)
4531 Inst.setOpcode(ARM::tSUBi3);
4534 // A Thumb conditional branch outside of an IT block is a tBcc.
4535 if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock())
4536 Inst.setOpcode(ARM::tBcc);
4539 // A Thumb2 conditional branch outside of an IT block is a t2Bcc.
4540 if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock())
4541 Inst.setOpcode(ARM::t2Bcc);
4544 // If the conditional is AL or we're in an IT block, we really want t2B.
4545 if (Inst.getOperand(1).getImm() == ARMCC::AL || inITBlock())
4546 Inst.setOpcode(ARM::t2B);
4549 // If the conditional is AL, we really want tB.
4550 if (Inst.getOperand(1).getImm() == ARMCC::AL)
4551 Inst.setOpcode(ARM::tB);
4554 // If the register list contains any high registers, or if the writeback
4555 // doesn't match what tLDMIA can do, we need to use the 32-bit encoding
4556 // instead if we're in Thumb2. Otherwise, this should have generated
4557 // an error in validateInstruction().
4558 unsigned Rn = Inst.getOperand(0).getReg();
4559 bool hasWritebackToken =
4560 (static_cast<ARMOperand*>(Operands[3])->isToken() &&
4561 static_cast<ARMOperand*>(Operands[3])->getToken() == "!");
4562 bool listContainsBase;
4563 if (checkLowRegisterList(Inst, 3, Rn, 0, listContainsBase) ||
4564 (!listContainsBase && !hasWritebackToken) ||
4565 (listContainsBase && hasWritebackToken)) {
4566 // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
4567 assert (isThumbTwo());
4568 Inst.setOpcode(hasWritebackToken ? ARM::t2LDMIA_UPD : ARM::t2LDMIA);
4569 // If we're switching to the updating version, we need to insert
4570 // the writeback tied operand.
4571 if (hasWritebackToken)
4572 Inst.insert(Inst.begin(),
4573 MCOperand::CreateReg(Inst.getOperand(0).getReg()));
4577 case ARM::tSTMIA_UPD: {
4578 // If the register list contains any high registers, we need to use
4579 // the 32-bit encoding instead if we're in Thumb2. Otherwise, this
4580 // should have generated an error in validateInstruction().
4581 unsigned Rn = Inst.getOperand(0).getReg();
4582 bool listContainsBase;
4583 if (checkLowRegisterList(Inst, 4, Rn, 0, listContainsBase)) {
4584 // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
4585 assert (isThumbTwo());
4586 Inst.setOpcode(ARM::t2STMIA_UPD);
4591 // If we can use the 16-bit encoding and the user didn't explicitly
4592 // request the 32-bit variant, transform it here.
4593 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
4594 Inst.getOperand(1).getImm() <= 255 &&
4595 ((!inITBlock() && Inst.getOperand(2).getImm() == ARMCC::AL &&
4596 Inst.getOperand(4).getReg() == ARM::CPSR) ||
4597 (inITBlock() && Inst.getOperand(4).getReg() == 0)) &&
4598 (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
4599 static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
4600 // The operands aren't in the same order for tMOVi8...
4602 TmpInst.setOpcode(ARM::tMOVi8);
4603 TmpInst.addOperand(Inst.getOperand(0));
4604 TmpInst.addOperand(Inst.getOperand(4));
4605 TmpInst.addOperand(Inst.getOperand(1));
4606 TmpInst.addOperand(Inst.getOperand(2));
4607 TmpInst.addOperand(Inst.getOperand(3));
4613 // If we can use the 16-bit encoding and the user didn't explicitly
4614 // request the 32-bit variant, transform it here.
4615 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
4616 isARMLowRegister(Inst.getOperand(1).getReg()) &&
4617 Inst.getOperand(2).getImm() == ARMCC::AL &&
4618 Inst.getOperand(4).getReg() == ARM::CPSR &&
4619 (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
4620 static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
4621 // The operands aren't the same for tMOV[S]r... (no cc_out)
4623 TmpInst.setOpcode(Inst.getOperand(4).getReg() ? ARM::tMOVSr : ARM::tMOVr);
4624 TmpInst.addOperand(Inst.getOperand(0));
4625 TmpInst.addOperand(Inst.getOperand(1));
4626 TmpInst.addOperand(Inst.getOperand(2));
4627 TmpInst.addOperand(Inst.getOperand(3));
4636 // If we can use the 16-bit encoding and the user didn't explicitly
4637 // request the 32-bit variant, transform it here.
4638 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
4639 isARMLowRegister(Inst.getOperand(1).getReg()) &&
4640 Inst.getOperand(2).getImm() == 0 &&
4641 (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
4642 static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
4644 switch (Inst.getOpcode()) {
4645 default: llvm_unreachable("Illegal opcode!");
4646 case ARM::t2SXTH: NewOpc = ARM::tSXTH; break;
4647 case ARM::t2SXTB: NewOpc = ARM::tSXTB; break;
4648 case ARM::t2UXTH: NewOpc = ARM::tUXTH; break;
4649 case ARM::t2UXTB: NewOpc = ARM::tUXTB; break;
4651 // The operands aren't the same for thumb1 (no rotate operand).
4653 TmpInst.setOpcode(NewOpc);
4654 TmpInst.addOperand(Inst.getOperand(0));
4655 TmpInst.addOperand(Inst.getOperand(1));
4656 TmpInst.addOperand(Inst.getOperand(3));
4657 TmpInst.addOperand(Inst.getOperand(4));
4663 // The mask bits for all but the first condition are represented as
4664 // the low bit of the condition code value implies 't'. We currently
4665 // always have 1 implies 't', so XOR toggle the bits if the low bit
4666 // of the condition code is zero. The encoding also expects the low
4667 // bit of the condition to be encoded as bit 4 of the mask operand,
4668 // so mask that in if needed
4669 MCOperand &MO = Inst.getOperand(1);
4670 unsigned Mask = MO.getImm();
4671 unsigned OrigMask = Mask;
4672 unsigned TZ = CountTrailingZeros_32(Mask);
4673 if ((Inst.getOperand(0).getImm() & 1) == 0) {
4674 assert(Mask && TZ <= 3 && "illegal IT mask value!");
4675 for (unsigned i = 3; i != TZ; --i)
4681 // Set up the IT block state according to the IT instruction we just
4683 assert(!inITBlock() && "nested IT blocks?!");
4684 ITState.Cond = ARMCC::CondCodes(Inst.getOperand(0).getImm());
4685 ITState.Mask = OrigMask; // Use the original mask, not the updated one.
4686 ITState.CurPosition = 0;
4687 ITState.FirstCond = true;
4693 unsigned ARMAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
4694 // 16-bit thumb arithmetic instructions either require or preclude the 'S'
4695 // suffix depending on whether they're in an IT block or not.
4696 unsigned Opc = Inst.getOpcode();
4697 const MCInstrDesc &MCID = getInstDesc(Opc);
4698 if (MCID.TSFlags & ARMII::ThumbArithFlagSetting) {
4699 assert(MCID.hasOptionalDef() &&
4700 "optionally flag setting instruction missing optional def operand");
4701 assert(MCID.NumOperands == Inst.getNumOperands() &&
4702 "operand count mismatch!");
4703 // Find the optional-def operand (cc_out).
4706 !MCID.OpInfo[OpNo].isOptionalDef() && OpNo < MCID.NumOperands;
4709 // If we're parsing Thumb1, reject it completely.
4710 if (isThumbOne() && Inst.getOperand(OpNo).getReg() != ARM::CPSR)
4711 return Match_MnemonicFail;
4712 // If we're parsing Thumb2, which form is legal depends on whether we're
4714 if (isThumbTwo() && Inst.getOperand(OpNo).getReg() != ARM::CPSR &&
4716 return Match_RequiresITBlock;
4717 if (isThumbTwo() && Inst.getOperand(OpNo).getReg() == ARM::CPSR &&
4719 return Match_RequiresNotITBlock;
4721 // Some high-register supporting Thumb1 encodings only allow both registers
4722 // to be from r0-r7 when in Thumb2.
4723 else if (Opc == ARM::tADDhirr && isThumbOne() &&
4724 isARMLowRegister(Inst.getOperand(1).getReg()) &&
4725 isARMLowRegister(Inst.getOperand(2).getReg()))
4726 return Match_RequiresThumb2;
4727 // Others only require ARMv6 or later.
4728 else if (Opc == ARM::tMOVr && isThumbOne() && !hasV6Ops() &&
4729 isARMLowRegister(Inst.getOperand(0).getReg()) &&
4730 isARMLowRegister(Inst.getOperand(1).getReg()))
4731 return Match_RequiresV6;
4732 return Match_Success;
4736 MatchAndEmitInstruction(SMLoc IDLoc,
4737 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
4741 unsigned MatchResult;
4742 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
4743 switch (MatchResult) {
4746 // Context sensitive operand constraints aren't handled by the matcher,
4747 // so check them here.
4748 if (validateInstruction(Inst, Operands)) {
4749 // Still progress the IT block, otherwise one wrong condition causes
4750 // nasty cascading errors.
4751 forwardITPosition();
4755 // Some instructions need post-processing to, for example, tweak which
4756 // encoding is selected.
4757 processInstruction(Inst, Operands);
4759 // Only move forward at the very end so that everything in validate
4760 // and process gets a consistent answer about whether we're in an IT
4762 forwardITPosition();
4764 Out.EmitInstruction(Inst);
4766 case Match_MissingFeature:
4767 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
4769 case Match_InvalidOperand: {
4770 SMLoc ErrorLoc = IDLoc;
4771 if (ErrorInfo != ~0U) {
4772 if (ErrorInfo >= Operands.size())
4773 return Error(IDLoc, "too few operands for instruction");
4775 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
4776 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
4779 return Error(ErrorLoc, "invalid operand for instruction");
4781 case Match_MnemonicFail:
4782 return Error(IDLoc, "invalid instruction");
4783 case Match_ConversionFail:
4784 // The converter function will have already emited a diagnostic.
4786 case Match_RequiresNotITBlock:
4787 return Error(IDLoc, "flag setting instruction only valid outside IT block");
4788 case Match_RequiresITBlock:
4789 return Error(IDLoc, "instruction only valid inside IT block");
4790 case Match_RequiresV6:
4791 return Error(IDLoc, "instruction variant requires ARMv6 or later");
4792 case Match_RequiresThumb2:
4793 return Error(IDLoc, "instruction variant requires Thumb2");
4796 llvm_unreachable("Implement any new match types added!");
4800 /// parseDirective parses the arm specific directives
4801 bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
4802 StringRef IDVal = DirectiveID.getIdentifier();
4803 if (IDVal == ".word")
4804 return parseDirectiveWord(4, DirectiveID.getLoc());
4805 else if (IDVal == ".thumb")
4806 return parseDirectiveThumb(DirectiveID.getLoc());
4807 else if (IDVal == ".thumb_func")
4808 return parseDirectiveThumbFunc(DirectiveID.getLoc());
4809 else if (IDVal == ".code")
4810 return parseDirectiveCode(DirectiveID.getLoc());
4811 else if (IDVal == ".syntax")
4812 return parseDirectiveSyntax(DirectiveID.getLoc());
4816 /// parseDirectiveWord
4817 /// ::= .word [ expression (, expression)* ]
4818 bool ARMAsmParser::parseDirectiveWord(unsigned Size, SMLoc L) {
4819 if (getLexer().isNot(AsmToken::EndOfStatement)) {
4821 const MCExpr *Value;
4822 if (getParser().ParseExpression(Value))
4825 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
4827 if (getLexer().is(AsmToken::EndOfStatement))
4830 // FIXME: Improve diagnostic.
4831 if (getLexer().isNot(AsmToken::Comma))
4832 return Error(L, "unexpected token in directive");
4841 /// parseDirectiveThumb
4843 bool ARMAsmParser::parseDirectiveThumb(SMLoc L) {
4844 if (getLexer().isNot(AsmToken::EndOfStatement))
4845 return Error(L, "unexpected token in directive");
4848 // TODO: set thumb mode
4849 // TODO: tell the MC streamer the mode
4850 // getParser().getStreamer().Emit???();
4854 /// parseDirectiveThumbFunc
4855 /// ::= .thumbfunc symbol_name
4856 bool ARMAsmParser::parseDirectiveThumbFunc(SMLoc L) {
4857 const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
4858 bool isMachO = MAI.hasSubsectionsViaSymbols();
4861 // Darwin asm has function name after .thumb_func direction
4864 const AsmToken &Tok = Parser.getTok();
4865 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
4866 return Error(L, "unexpected token in .thumb_func directive");
4867 Name = Tok.getString();
4868 Parser.Lex(); // Consume the identifier token.
4871 if (getLexer().isNot(AsmToken::EndOfStatement))
4872 return Error(L, "unexpected token in directive");
4875 // FIXME: assuming function name will be the line following .thumb_func
4877 Name = Parser.getTok().getString();
4880 // Mark symbol as a thumb symbol.
4881 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
4882 getParser().getStreamer().EmitThumbFunc(Func);
4886 /// parseDirectiveSyntax
4887 /// ::= .syntax unified | divided
4888 bool ARMAsmParser::parseDirectiveSyntax(SMLoc L) {
4889 const AsmToken &Tok = Parser.getTok();
4890 if (Tok.isNot(AsmToken::Identifier))
4891 return Error(L, "unexpected token in .syntax directive");
4892 StringRef Mode = Tok.getString();
4893 if (Mode == "unified" || Mode == "UNIFIED")
4895 else if (Mode == "divided" || Mode == "DIVIDED")
4896 return Error(L, "'.syntax divided' arm asssembly not supported");
4898 return Error(L, "unrecognized syntax mode in .syntax directive");
4900 if (getLexer().isNot(AsmToken::EndOfStatement))
4901 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
4904 // TODO tell the MC streamer the mode
4905 // getParser().getStreamer().Emit???();
4909 /// parseDirectiveCode
4910 /// ::= .code 16 | 32
4911 bool ARMAsmParser::parseDirectiveCode(SMLoc L) {
4912 const AsmToken &Tok = Parser.getTok();
4913 if (Tok.isNot(AsmToken::Integer))
4914 return Error(L, "unexpected token in .code directive");
4915 int64_t Val = Parser.getTok().getIntVal();
4921 return Error(L, "invalid operand to .code directive");
4923 if (getLexer().isNot(AsmToken::EndOfStatement))
4924 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
4930 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
4934 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
4940 extern "C" void LLVMInitializeARMAsmLexer();
4942 /// Force static initialization.
4943 extern "C" void LLVMInitializeARMAsmParser() {
4944 RegisterMCAsmParser<ARMAsmParser> X(TheARMTarget);
4945 RegisterMCAsmParser<ARMAsmParser> Y(TheThumbTarget);
4946 LLVMInitializeARMAsmLexer();
4949 #define GET_REGISTER_MATCHER
4950 #define GET_MATCHER_IMPLEMENTATION
4951 #include "ARMGenAsmMatcher.inc"