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 parseMemBarrierOptOperand(
142 SmallVectorImpl<MCParsedAsmOperand*>&);
143 OperandMatchResultTy parseProcIFlagsOperand(
144 SmallVectorImpl<MCParsedAsmOperand*>&);
145 OperandMatchResultTy parseMSRMaskOperand(
146 SmallVectorImpl<MCParsedAsmOperand*>&);
147 OperandMatchResultTy parsePKHImm(SmallVectorImpl<MCParsedAsmOperand*> &O,
148 StringRef Op, int Low, int High);
149 OperandMatchResultTy parsePKHLSLImm(SmallVectorImpl<MCParsedAsmOperand*> &O) {
150 return parsePKHImm(O, "lsl", 0, 31);
152 OperandMatchResultTy parsePKHASRImm(SmallVectorImpl<MCParsedAsmOperand*> &O) {
153 return parsePKHImm(O, "asr", 1, 32);
155 OperandMatchResultTy parseSetEndImm(SmallVectorImpl<MCParsedAsmOperand*>&);
156 OperandMatchResultTy parseShifterImm(SmallVectorImpl<MCParsedAsmOperand*>&);
157 OperandMatchResultTy parseRotImm(SmallVectorImpl<MCParsedAsmOperand*>&);
158 OperandMatchResultTy parseBitfield(SmallVectorImpl<MCParsedAsmOperand*>&);
159 OperandMatchResultTy parsePostIdxReg(SmallVectorImpl<MCParsedAsmOperand*>&);
160 OperandMatchResultTy parseAM3Offset(SmallVectorImpl<MCParsedAsmOperand*>&);
161 OperandMatchResultTy parseFPImm(SmallVectorImpl<MCParsedAsmOperand*>&);
163 // Asm Match Converter Methods
164 bool cvtT2LdrdPre(MCInst &Inst, unsigned Opcode,
165 const SmallVectorImpl<MCParsedAsmOperand*> &);
166 bool cvtT2StrdPre(MCInst &Inst, unsigned Opcode,
167 const SmallVectorImpl<MCParsedAsmOperand*> &);
168 bool cvtLdWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
169 const SmallVectorImpl<MCParsedAsmOperand*> &);
170 bool cvtStWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
171 const SmallVectorImpl<MCParsedAsmOperand*> &);
172 bool cvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
173 const SmallVectorImpl<MCParsedAsmOperand*> &);
174 bool cvtLdWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
175 const SmallVectorImpl<MCParsedAsmOperand*> &);
176 bool cvtStWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
177 const SmallVectorImpl<MCParsedAsmOperand*> &);
178 bool cvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
179 const SmallVectorImpl<MCParsedAsmOperand*> &);
180 bool cvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
181 const SmallVectorImpl<MCParsedAsmOperand*> &);
182 bool cvtLdExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
183 const SmallVectorImpl<MCParsedAsmOperand*> &);
184 bool cvtLdExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
185 const SmallVectorImpl<MCParsedAsmOperand*> &);
186 bool cvtStExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
187 const SmallVectorImpl<MCParsedAsmOperand*> &);
188 bool cvtStExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
189 const SmallVectorImpl<MCParsedAsmOperand*> &);
190 bool cvtLdrdPre(MCInst &Inst, unsigned Opcode,
191 const SmallVectorImpl<MCParsedAsmOperand*> &);
192 bool cvtStrdPre(MCInst &Inst, unsigned Opcode,
193 const SmallVectorImpl<MCParsedAsmOperand*> &);
194 bool cvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
195 const SmallVectorImpl<MCParsedAsmOperand*> &);
196 bool cvtThumbMultiply(MCInst &Inst, unsigned Opcode,
197 const SmallVectorImpl<MCParsedAsmOperand*> &);
199 bool validateInstruction(MCInst &Inst,
200 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
201 void processInstruction(MCInst &Inst,
202 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
203 bool shouldOmitCCOutOperand(StringRef Mnemonic,
204 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
207 enum ARMMatchResultTy {
208 Match_RequiresITBlock = FIRST_TARGET_MATCH_RESULT_TY,
209 Match_RequiresNotITBlock,
214 ARMAsmParser(MCSubtargetInfo &_STI, MCAsmParser &_Parser)
215 : MCTargetAsmParser(), STI(_STI), Parser(_Parser) {
216 MCAsmParserExtension::Initialize(_Parser);
218 // Initialize the set of available features.
219 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
221 // Not in an ITBlock to start with.
222 ITState.CurPosition = ~0U;
225 // Implementation of the MCTargetAsmParser interface:
226 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
227 bool ParseInstruction(StringRef Name, SMLoc NameLoc,
228 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
229 bool ParseDirective(AsmToken DirectiveID);
231 unsigned checkTargetMatchPredicate(MCInst &Inst);
233 bool MatchAndEmitInstruction(SMLoc IDLoc,
234 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
237 } // end anonymous namespace
241 /// ARMOperand - Instances of this class represent a parsed ARM machine
243 class ARMOperand : public MCParsedAsmOperand {
266 k_BitfieldDescriptor,
270 SMLoc StartLoc, EndLoc;
271 SmallVector<unsigned, 8> Registers;
275 ARMCC::CondCodes Val;
291 ARM_PROC::IFlags Val;
316 unsigned Val; // encoded 8-bit representation
319 /// Combined record for all forms of ARM address expressions.
322 // Offset is in OffsetReg or OffsetImm. If both are zero, no offset
324 const MCConstantExpr *OffsetImm; // Offset immediate value
325 unsigned OffsetRegNum; // Offset register num, when OffsetImm == NULL
326 ARM_AM::ShiftOpc ShiftType; // Shift type for OffsetReg
327 unsigned ShiftImm; // shift for OffsetReg.
328 unsigned Alignment; // 0 = no alignment specified
329 // n = alignment in bytes (8, 16, or 32)
330 unsigned isNegative : 1; // Negated OffsetReg? (~'U' bit)
336 ARM_AM::ShiftOpc ShiftTy;
345 ARM_AM::ShiftOpc ShiftTy;
351 ARM_AM::ShiftOpc ShiftTy;
364 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
366 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
368 StartLoc = o.StartLoc;
385 case k_DPRRegisterList:
386 case k_SPRRegisterList:
387 Registers = o.Registers;
399 case k_MemBarrierOpt:
405 case k_PostIndexRegister:
406 PostIdxReg = o.PostIdxReg;
414 case k_ShifterImmediate:
415 ShifterImm = o.ShifterImm;
417 case k_ShiftedRegister:
418 RegShiftedReg = o.RegShiftedReg;
420 case k_ShiftedImmediate:
421 RegShiftedImm = o.RegShiftedImm;
423 case k_RotateImmediate:
426 case k_BitfieldDescriptor:
427 Bitfield = o.Bitfield;
430 VectorIndex = o.VectorIndex;
435 /// getStartLoc - Get the location of the first token of this operand.
436 SMLoc getStartLoc() const { return StartLoc; }
437 /// getEndLoc - Get the location of the last token of this operand.
438 SMLoc getEndLoc() const { return EndLoc; }
440 ARMCC::CondCodes getCondCode() const {
441 assert(Kind == k_CondCode && "Invalid access!");
445 unsigned getCoproc() const {
446 assert((Kind == k_CoprocNum || Kind == k_CoprocReg) && "Invalid access!");
450 StringRef getToken() const {
451 assert(Kind == k_Token && "Invalid access!");
452 return StringRef(Tok.Data, Tok.Length);
455 unsigned getReg() const {
456 assert((Kind == k_Register || Kind == k_CCOut) && "Invalid access!");
460 const SmallVectorImpl<unsigned> &getRegList() const {
461 assert((Kind == k_RegisterList || Kind == k_DPRRegisterList ||
462 Kind == k_SPRRegisterList) && "Invalid access!");
466 const MCExpr *getImm() const {
467 assert(Kind == k_Immediate && "Invalid access!");
471 unsigned getFPImm() const {
472 assert(Kind == k_FPImmediate && "Invalid access!");
476 unsigned getVectorIndex() const {
477 assert(Kind == k_VectorIndex && "Invalid access!");
478 return VectorIndex.Val;
481 ARM_MB::MemBOpt getMemBarrierOpt() const {
482 assert(Kind == k_MemBarrierOpt && "Invalid access!");
486 ARM_PROC::IFlags getProcIFlags() const {
487 assert(Kind == k_ProcIFlags && "Invalid access!");
491 unsigned getMSRMask() const {
492 assert(Kind == k_MSRMask && "Invalid access!");
496 bool isCoprocNum() const { return Kind == k_CoprocNum; }
497 bool isCoprocReg() const { return Kind == k_CoprocReg; }
498 bool isCondCode() const { return Kind == k_CondCode; }
499 bool isCCOut() const { return Kind == k_CCOut; }
500 bool isITMask() const { return Kind == k_ITCondMask; }
501 bool isITCondCode() const { return Kind == k_CondCode; }
502 bool isImm() const { return Kind == k_Immediate; }
503 bool isFPImm() const { return Kind == k_FPImmediate; }
504 bool isImm8s4() const {
505 if (Kind != k_Immediate)
507 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
508 if (!CE) return false;
509 int64_t Value = CE->getValue();
510 return ((Value & 3) == 0) && Value >= -1020 && Value <= 1020;
512 bool isImm0_1020s4() const {
513 if (Kind != k_Immediate)
515 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
516 if (!CE) return false;
517 int64_t Value = CE->getValue();
518 return ((Value & 3) == 0) && Value >= 0 && Value <= 1020;
520 bool isImm0_508s4() const {
521 if (Kind != k_Immediate)
523 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
524 if (!CE) return false;
525 int64_t Value = CE->getValue();
526 return ((Value & 3) == 0) && Value >= 0 && Value <= 508;
528 bool isImm0_255() const {
529 if (Kind != k_Immediate)
531 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
532 if (!CE) return false;
533 int64_t Value = CE->getValue();
534 return Value >= 0 && Value < 256;
536 bool isImm0_7() const {
537 if (Kind != k_Immediate)
539 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
540 if (!CE) return false;
541 int64_t Value = CE->getValue();
542 return Value >= 0 && Value < 8;
544 bool isImm0_15() const {
545 if (Kind != k_Immediate)
547 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
548 if (!CE) return false;
549 int64_t Value = CE->getValue();
550 return Value >= 0 && Value < 16;
552 bool isImm0_31() const {
553 if (Kind != k_Immediate)
555 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
556 if (!CE) return false;
557 int64_t Value = CE->getValue();
558 return Value >= 0 && Value < 32;
560 bool isImm1_16() const {
561 if (Kind != k_Immediate)
563 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
564 if (!CE) return false;
565 int64_t Value = CE->getValue();
566 return Value > 0 && Value < 17;
568 bool isImm1_32() const {
569 if (Kind != k_Immediate)
571 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
572 if (!CE) return false;
573 int64_t Value = CE->getValue();
574 return Value > 0 && Value < 33;
576 bool isImm0_65535() const {
577 if (Kind != k_Immediate)
579 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
580 if (!CE) return false;
581 int64_t Value = CE->getValue();
582 return Value >= 0 && Value < 65536;
584 bool isImm0_65535Expr() const {
585 if (Kind != k_Immediate)
587 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
588 // If it's not a constant expression, it'll generate a fixup and be
590 if (!CE) return true;
591 int64_t Value = CE->getValue();
592 return Value >= 0 && Value < 65536;
594 bool isImm24bit() 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 <= 0xffffff;
602 bool isImmThumbSR() 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 < 33;
610 bool isPKHLSLImm() const {
611 if (Kind != k_Immediate)
613 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
614 if (!CE) return false;
615 int64_t Value = CE->getValue();
616 return Value >= 0 && Value < 32;
618 bool isPKHASRImm() const {
619 if (Kind != k_Immediate)
621 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
622 if (!CE) return false;
623 int64_t Value = CE->getValue();
624 return Value > 0 && Value <= 32;
626 bool isARMSOImm() const {
627 if (Kind != k_Immediate)
629 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
630 if (!CE) return false;
631 int64_t Value = CE->getValue();
632 return ARM_AM::getSOImmVal(Value) != -1;
634 bool isT2SOImm() const {
635 if (Kind != k_Immediate)
637 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
638 if (!CE) return false;
639 int64_t Value = CE->getValue();
640 return ARM_AM::getT2SOImmVal(Value) != -1;
642 bool isSetEndImm() const {
643 if (Kind != k_Immediate)
645 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
646 if (!CE) return false;
647 int64_t Value = CE->getValue();
648 return Value == 1 || Value == 0;
650 bool isReg() const { return Kind == k_Register; }
651 bool isRegList() const { return Kind == k_RegisterList; }
652 bool isDPRRegList() const { return Kind == k_DPRRegisterList; }
653 bool isSPRRegList() const { return Kind == k_SPRRegisterList; }
654 bool isToken() const { return Kind == k_Token; }
655 bool isMemBarrierOpt() const { return Kind == k_MemBarrierOpt; }
656 bool isMemory() const { return Kind == k_Memory; }
657 bool isShifterImm() const { return Kind == k_ShifterImmediate; }
658 bool isRegShiftedReg() const { return Kind == k_ShiftedRegister; }
659 bool isRegShiftedImm() const { return Kind == k_ShiftedImmediate; }
660 bool isRotImm() const { return Kind == k_RotateImmediate; }
661 bool isBitfield() const { return Kind == k_BitfieldDescriptor; }
662 bool isPostIdxRegShifted() const { return Kind == k_PostIndexRegister; }
663 bool isPostIdxReg() const {
664 return Kind == k_PostIndexRegister && PostIdxReg.ShiftTy == ARM_AM::no_shift;
666 bool isMemNoOffset(bool alignOK = false) const {
669 // No offset of any kind.
670 return Memory.OffsetRegNum == 0 && Memory.OffsetImm == 0 &&
671 (alignOK || Memory.Alignment == 0);
673 bool isAlignedMemory() const {
674 return isMemNoOffset(true);
676 bool isAddrMode2() const {
677 if (!isMemory() || Memory.Alignment != 0) return false;
678 // Check for register offset.
679 if (Memory.OffsetRegNum) return true;
680 // Immediate offset in range [-4095, 4095].
681 if (!Memory.OffsetImm) return true;
682 int64_t Val = Memory.OffsetImm->getValue();
683 return Val > -4096 && Val < 4096;
685 bool isAM2OffsetImm() const {
686 if (Kind != k_Immediate)
688 // Immediate offset in range [-4095, 4095].
689 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
690 if (!CE) return false;
691 int64_t Val = CE->getValue();
692 return Val > -4096 && Val < 4096;
694 bool isAddrMode3() const {
695 if (!isMemory() || Memory.Alignment != 0) return false;
696 // No shifts are legal for AM3.
697 if (Memory.ShiftType != ARM_AM::no_shift) return false;
698 // Check for register offset.
699 if (Memory.OffsetRegNum) return true;
700 // Immediate offset in range [-255, 255].
701 if (!Memory.OffsetImm) return true;
702 int64_t Val = Memory.OffsetImm->getValue();
703 return Val > -256 && Val < 256;
705 bool isAM3Offset() const {
706 if (Kind != k_Immediate && Kind != k_PostIndexRegister)
708 if (Kind == k_PostIndexRegister)
709 return PostIdxReg.ShiftTy == ARM_AM::no_shift;
710 // Immediate offset in range [-255, 255].
711 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
712 if (!CE) return false;
713 int64_t Val = CE->getValue();
714 // Special case, #-0 is INT32_MIN.
715 return (Val > -256 && Val < 256) || Val == INT32_MIN;
717 bool isAddrMode5() const {
718 if (!isMemory() || Memory.Alignment != 0) return false;
719 // Check for register offset.
720 if (Memory.OffsetRegNum) return false;
721 // Immediate offset in range [-1020, 1020] and a multiple of 4.
722 if (!Memory.OffsetImm) return true;
723 int64_t Val = Memory.OffsetImm->getValue();
724 return (Val >= -1020 && Val <= 1020 && ((Val & 3) == 0)) ||
727 bool isMemTBB() const {
728 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
729 Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
733 bool isMemTBH() const {
734 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
735 Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm != 1 ||
736 Memory.Alignment != 0 )
740 bool isMemRegOffset() const {
741 if (!isMemory() || !Memory.OffsetRegNum || Memory.Alignment != 0)
745 bool isT2MemRegOffset() const {
746 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
747 Memory.Alignment != 0)
749 // Only lsl #{0, 1, 2, 3} allowed.
750 if (Memory.ShiftType == ARM_AM::no_shift)
752 if (Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm > 3)
756 bool isMemThumbRR() const {
757 // Thumb reg+reg addressing is simple. Just two registers, a base and
758 // an offset. No shifts, negations or any other complicating factors.
759 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
760 Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
762 return isARMLowRegister(Memory.BaseRegNum) &&
763 (!Memory.OffsetRegNum || isARMLowRegister(Memory.OffsetRegNum));
765 bool isMemThumbRIs4() const {
766 if (!isMemory() || Memory.OffsetRegNum != 0 ||
767 !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
769 // Immediate offset, multiple of 4 in range [0, 124].
770 if (!Memory.OffsetImm) return true;
771 int64_t Val = Memory.OffsetImm->getValue();
772 return Val >= 0 && Val <= 124 && (Val % 4) == 0;
774 bool isMemThumbRIs2() const {
775 if (!isMemory() || Memory.OffsetRegNum != 0 ||
776 !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
778 // Immediate offset, multiple of 4 in range [0, 62].
779 if (!Memory.OffsetImm) return true;
780 int64_t Val = Memory.OffsetImm->getValue();
781 return Val >= 0 && Val <= 62 && (Val % 2) == 0;
783 bool isMemThumbRIs1() const {
784 if (!isMemory() || Memory.OffsetRegNum != 0 ||
785 !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
787 // Immediate offset in range [0, 31].
788 if (!Memory.OffsetImm) return true;
789 int64_t Val = Memory.OffsetImm->getValue();
790 return Val >= 0 && Val <= 31;
792 bool isMemThumbSPI() const {
793 if (!isMemory() || Memory.OffsetRegNum != 0 ||
794 Memory.BaseRegNum != ARM::SP || Memory.Alignment != 0)
796 // Immediate offset, multiple of 4 in range [0, 1020].
797 if (!Memory.OffsetImm) return true;
798 int64_t Val = Memory.OffsetImm->getValue();
799 return Val >= 0 && Val <= 1020 && (Val % 4) == 0;
801 bool isMemImm8s4Offset() const {
802 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
804 // Immediate offset a multiple of 4 in range [-1020, 1020].
805 if (!Memory.OffsetImm) return true;
806 int64_t Val = Memory.OffsetImm->getValue();
807 return Val >= -1020 && Val <= 1020 && (Val & 3) == 0;
809 bool isMemImm0_1020s4Offset() const {
810 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
812 // Immediate offset a multiple of 4 in range [0, 1020].
813 if (!Memory.OffsetImm) return true;
814 int64_t Val = Memory.OffsetImm->getValue();
815 return Val >= 0 && Val <= 1020 && (Val & 3) == 0;
817 bool isMemImm8Offset() const {
818 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
820 // Immediate offset in range [-255, 255].
821 if (!Memory.OffsetImm) return true;
822 int64_t Val = Memory.OffsetImm->getValue();
823 return (Val == INT32_MIN) || (Val > -256 && Val < 256);
825 bool isMemPosImm8Offset() const {
826 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
828 // Immediate offset in range [0, 255].
829 if (!Memory.OffsetImm) return true;
830 int64_t Val = Memory.OffsetImm->getValue();
831 return Val >= 0 && Val < 256;
833 bool isMemNegImm8Offset() const {
834 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
836 // Immediate offset in range [-255, -1].
837 if (!Memory.OffsetImm) return true;
838 int64_t Val = Memory.OffsetImm->getValue();
839 return Val > -256 && Val < 0;
841 bool isMemUImm12Offset() const {
842 // If we have an immediate that's not a constant, treat it as a label
843 // reference needing a fixup. If it is a constant, it's something else
845 if (Kind == k_Immediate && !isa<MCConstantExpr>(getImm()))
848 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
850 // Immediate offset in range [0, 4095].
851 if (!Memory.OffsetImm) return true;
852 int64_t Val = Memory.OffsetImm->getValue();
853 return (Val >= 0 && Val < 4096);
855 bool isMemImm12Offset() const {
856 // If we have an immediate that's not a constant, treat it as a label
857 // reference needing a fixup. If it is a constant, it's something else
859 if (Kind == k_Immediate && !isa<MCConstantExpr>(getImm()))
862 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
864 // Immediate offset in range [-4095, 4095].
865 if (!Memory.OffsetImm) return true;
866 int64_t Val = Memory.OffsetImm->getValue();
867 return (Val > -4096 && Val < 4096) || (Val == INT32_MIN);
869 bool isPostIdxImm8() const {
870 if (Kind != k_Immediate)
872 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
873 if (!CE) return false;
874 int64_t Val = CE->getValue();
875 return (Val > -256 && Val < 256) || (Val == INT32_MIN);
878 bool isMSRMask() const { return Kind == k_MSRMask; }
879 bool isProcIFlags() const { return Kind == k_ProcIFlags; }
881 bool isVectorIndex8() const {
882 if (Kind != k_VectorIndex) return false;
883 return VectorIndex.Val < 8;
885 bool isVectorIndex16() const {
886 if (Kind != k_VectorIndex) return false;
887 return VectorIndex.Val < 4;
889 bool isVectorIndex32() const {
890 if (Kind != k_VectorIndex) return false;
891 return VectorIndex.Val < 2;
896 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
897 // Add as immediates when possible. Null MCExpr = 0.
899 Inst.addOperand(MCOperand::CreateImm(0));
900 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
901 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
903 Inst.addOperand(MCOperand::CreateExpr(Expr));
906 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
907 assert(N == 2 && "Invalid number of operands!");
908 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
909 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
910 Inst.addOperand(MCOperand::CreateReg(RegNum));
913 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
914 assert(N == 1 && "Invalid number of operands!");
915 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
918 void addITMaskOperands(MCInst &Inst, unsigned N) const {
919 assert(N == 1 && "Invalid number of operands!");
920 Inst.addOperand(MCOperand::CreateImm(ITMask.Mask));
923 void addITCondCodeOperands(MCInst &Inst, unsigned N) const {
924 assert(N == 1 && "Invalid number of operands!");
925 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
928 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
929 assert(N == 1 && "Invalid number of operands!");
930 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
933 void addCCOutOperands(MCInst &Inst, unsigned N) const {
934 assert(N == 1 && "Invalid number of operands!");
935 Inst.addOperand(MCOperand::CreateReg(getReg()));
938 void addRegOperands(MCInst &Inst, unsigned N) const {
939 assert(N == 1 && "Invalid number of operands!");
940 Inst.addOperand(MCOperand::CreateReg(getReg()));
943 void addRegShiftedRegOperands(MCInst &Inst, unsigned N) const {
944 assert(N == 3 && "Invalid number of operands!");
945 assert(isRegShiftedReg() && "addRegShiftedRegOperands() on non RegShiftedReg!");
946 Inst.addOperand(MCOperand::CreateReg(RegShiftedReg.SrcReg));
947 Inst.addOperand(MCOperand::CreateReg(RegShiftedReg.ShiftReg));
948 Inst.addOperand(MCOperand::CreateImm(
949 ARM_AM::getSORegOpc(RegShiftedReg.ShiftTy, RegShiftedReg.ShiftImm)));
952 void addRegShiftedImmOperands(MCInst &Inst, unsigned N) const {
953 assert(N == 2 && "Invalid number of operands!");
954 assert(isRegShiftedImm() && "addRegShiftedImmOperands() on non RegShiftedImm!");
955 Inst.addOperand(MCOperand::CreateReg(RegShiftedImm.SrcReg));
956 Inst.addOperand(MCOperand::CreateImm(
957 ARM_AM::getSORegOpc(RegShiftedImm.ShiftTy, RegShiftedImm.ShiftImm)));
960 void addShifterImmOperands(MCInst &Inst, unsigned N) const {
961 assert(N == 1 && "Invalid number of operands!");
962 Inst.addOperand(MCOperand::CreateImm((ShifterImm.isASR << 5) |
966 void addRegListOperands(MCInst &Inst, unsigned N) const {
967 assert(N == 1 && "Invalid number of operands!");
968 const SmallVectorImpl<unsigned> &RegList = getRegList();
969 for (SmallVectorImpl<unsigned>::const_iterator
970 I = RegList.begin(), E = RegList.end(); I != E; ++I)
971 Inst.addOperand(MCOperand::CreateReg(*I));
974 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
975 addRegListOperands(Inst, N);
978 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
979 addRegListOperands(Inst, N);
982 void addRotImmOperands(MCInst &Inst, unsigned N) const {
983 assert(N == 1 && "Invalid number of operands!");
984 // Encoded as val>>3. The printer handles display as 8, 16, 24.
985 Inst.addOperand(MCOperand::CreateImm(RotImm.Imm >> 3));
988 void addBitfieldOperands(MCInst &Inst, unsigned N) const {
989 assert(N == 1 && "Invalid number of operands!");
990 // Munge the lsb/width into a bitfield mask.
991 unsigned lsb = Bitfield.LSB;
992 unsigned width = Bitfield.Width;
993 // Make a 32-bit mask w/ the referenced bits clear and all other bits set.
994 uint32_t Mask = ~(((uint32_t)0xffffffff >> lsb) << (32 - width) >>
995 (32 - (lsb + width)));
996 Inst.addOperand(MCOperand::CreateImm(Mask));
999 void addImmOperands(MCInst &Inst, unsigned N) const {
1000 assert(N == 1 && "Invalid number of operands!");
1001 addExpr(Inst, getImm());
1004 void addFPImmOperands(MCInst &Inst, unsigned N) const {
1005 assert(N == 1 && "Invalid number of operands!");
1006 Inst.addOperand(MCOperand::CreateImm(getFPImm()));
1009 void addImm8s4Operands(MCInst &Inst, unsigned N) const {
1010 assert(N == 1 && "Invalid number of operands!");
1011 // FIXME: We really want to scale the value here, but the LDRD/STRD
1012 // instruction don't encode operands that way yet.
1013 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1014 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
1017 void addImm0_1020s4Operands(MCInst &Inst, unsigned N) const {
1018 assert(N == 1 && "Invalid number of operands!");
1019 // The immediate is scaled by four in the encoding and is stored
1020 // in the MCInst as such. Lop off the low two bits here.
1021 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1022 Inst.addOperand(MCOperand::CreateImm(CE->getValue() / 4));
1025 void addImm0_508s4Operands(MCInst &Inst, unsigned N) const {
1026 assert(N == 1 && "Invalid number of operands!");
1027 // The immediate is scaled by four in the encoding and is stored
1028 // in the MCInst as such. Lop off the low two bits here.
1029 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1030 Inst.addOperand(MCOperand::CreateImm(CE->getValue() / 4));
1033 void addImm0_255Operands(MCInst &Inst, unsigned N) const {
1034 assert(N == 1 && "Invalid number of operands!");
1035 addExpr(Inst, getImm());
1038 void addImm0_7Operands(MCInst &Inst, unsigned N) const {
1039 assert(N == 1 && "Invalid number of operands!");
1040 addExpr(Inst, getImm());
1043 void addImm0_15Operands(MCInst &Inst, unsigned N) const {
1044 assert(N == 1 && "Invalid number of operands!");
1045 addExpr(Inst, getImm());
1048 void addImm0_31Operands(MCInst &Inst, unsigned N) const {
1049 assert(N == 1 && "Invalid number of operands!");
1050 addExpr(Inst, getImm());
1053 void addImm1_16Operands(MCInst &Inst, unsigned N) const {
1054 assert(N == 1 && "Invalid number of operands!");
1055 // The constant encodes as the immediate-1, and we store in the instruction
1056 // the bits as encoded, so subtract off one here.
1057 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1058 Inst.addOperand(MCOperand::CreateImm(CE->getValue() - 1));
1061 void addImm1_32Operands(MCInst &Inst, unsigned N) const {
1062 assert(N == 1 && "Invalid number of operands!");
1063 // The constant encodes as the immediate-1, and we store in the instruction
1064 // the bits as encoded, so subtract off one here.
1065 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1066 Inst.addOperand(MCOperand::CreateImm(CE->getValue() - 1));
1069 void addImm0_65535Operands(MCInst &Inst, unsigned N) const {
1070 assert(N == 1 && "Invalid number of operands!");
1071 addExpr(Inst, getImm());
1074 void addImm0_65535ExprOperands(MCInst &Inst, unsigned N) const {
1075 assert(N == 1 && "Invalid number of operands!");
1076 addExpr(Inst, getImm());
1079 void addImm24bitOperands(MCInst &Inst, unsigned N) const {
1080 assert(N == 1 && "Invalid number of operands!");
1081 addExpr(Inst, getImm());
1084 void addImmThumbSROperands(MCInst &Inst, unsigned N) const {
1085 assert(N == 1 && "Invalid number of operands!");
1086 // The constant encodes as the immediate, except for 32, which encodes as
1088 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1089 unsigned Imm = CE->getValue();
1090 Inst.addOperand(MCOperand::CreateImm((Imm == 32 ? 0 : Imm)));
1093 void addPKHLSLImmOperands(MCInst &Inst, unsigned N) const {
1094 assert(N == 1 && "Invalid number of operands!");
1095 addExpr(Inst, getImm());
1098 void addPKHASRImmOperands(MCInst &Inst, unsigned N) const {
1099 assert(N == 1 && "Invalid number of operands!");
1100 // An ASR value of 32 encodes as 0, so that's how we want to add it to
1101 // the instruction as well.
1102 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1103 int Val = CE->getValue();
1104 Inst.addOperand(MCOperand::CreateImm(Val == 32 ? 0 : Val));
1107 void addARMSOImmOperands(MCInst &Inst, unsigned N) const {
1108 assert(N == 1 && "Invalid number of operands!");
1109 addExpr(Inst, getImm());
1112 void addT2SOImmOperands(MCInst &Inst, unsigned N) const {
1113 assert(N == 1 && "Invalid number of operands!");
1114 addExpr(Inst, getImm());
1117 void addSetEndImmOperands(MCInst &Inst, unsigned N) const {
1118 assert(N == 1 && "Invalid number of operands!");
1119 addExpr(Inst, getImm());
1122 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
1123 assert(N == 1 && "Invalid number of operands!");
1124 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
1127 void addMemNoOffsetOperands(MCInst &Inst, unsigned N) const {
1128 assert(N == 1 && "Invalid number of operands!");
1129 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1132 void addAlignedMemoryOperands(MCInst &Inst, unsigned N) const {
1133 assert(N == 2 && "Invalid number of operands!");
1134 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1135 Inst.addOperand(MCOperand::CreateImm(Memory.Alignment));
1138 void addAddrMode2Operands(MCInst &Inst, unsigned N) const {
1139 assert(N == 3 && "Invalid number of operands!");
1140 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1141 if (!Memory.OffsetRegNum) {
1142 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1143 // Special case for #-0
1144 if (Val == INT32_MIN) Val = 0;
1145 if (Val < 0) Val = -Val;
1146 Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
1148 // For register offset, we encode the shift type and negation flag
1150 Val = ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
1151 Memory.ShiftImm, Memory.ShiftType);
1153 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1154 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1155 Inst.addOperand(MCOperand::CreateImm(Val));
1158 void addAM2OffsetImmOperands(MCInst &Inst, unsigned N) const {
1159 assert(N == 2 && "Invalid number of operands!");
1160 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1161 assert(CE && "non-constant AM2OffsetImm operand!");
1162 int32_t Val = CE->getValue();
1163 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1164 // Special case for #-0
1165 if (Val == INT32_MIN) Val = 0;
1166 if (Val < 0) Val = -Val;
1167 Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
1168 Inst.addOperand(MCOperand::CreateReg(0));
1169 Inst.addOperand(MCOperand::CreateImm(Val));
1172 void addAddrMode3Operands(MCInst &Inst, unsigned N) const {
1173 assert(N == 3 && "Invalid number of operands!");
1174 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1175 if (!Memory.OffsetRegNum) {
1176 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1177 // Special case for #-0
1178 if (Val == INT32_MIN) Val = 0;
1179 if (Val < 0) Val = -Val;
1180 Val = ARM_AM::getAM3Opc(AddSub, Val);
1182 // For register offset, we encode the shift type and negation flag
1184 Val = ARM_AM::getAM3Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add, 0);
1186 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1187 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1188 Inst.addOperand(MCOperand::CreateImm(Val));
1191 void addAM3OffsetOperands(MCInst &Inst, unsigned N) const {
1192 assert(N == 2 && "Invalid number of operands!");
1193 if (Kind == k_PostIndexRegister) {
1195 ARM_AM::getAM3Opc(PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub, 0);
1196 Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1197 Inst.addOperand(MCOperand::CreateImm(Val));
1202 const MCConstantExpr *CE = static_cast<const MCConstantExpr*>(getImm());
1203 int32_t Val = CE->getValue();
1204 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1205 // Special case for #-0
1206 if (Val == INT32_MIN) Val = 0;
1207 if (Val < 0) Val = -Val;
1208 Val = ARM_AM::getAM3Opc(AddSub, Val);
1209 Inst.addOperand(MCOperand::CreateReg(0));
1210 Inst.addOperand(MCOperand::CreateImm(Val));
1213 void addAddrMode5Operands(MCInst &Inst, unsigned N) const {
1214 assert(N == 2 && "Invalid number of operands!");
1215 // The lower two bits are always zero and as such are not encoded.
1216 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
1217 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1218 // Special case for #-0
1219 if (Val == INT32_MIN) Val = 0;
1220 if (Val < 0) Val = -Val;
1221 Val = ARM_AM::getAM5Opc(AddSub, Val);
1222 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1223 Inst.addOperand(MCOperand::CreateImm(Val));
1226 void addMemImm8s4OffsetOperands(MCInst &Inst, unsigned N) const {
1227 assert(N == 2 && "Invalid number of operands!");
1228 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1229 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1230 Inst.addOperand(MCOperand::CreateImm(Val));
1233 void addMemImm0_1020s4OffsetOperands(MCInst &Inst, unsigned N) const {
1234 assert(N == 2 && "Invalid number of operands!");
1235 // The lower two bits are always zero and as such are not encoded.
1236 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
1237 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1238 Inst.addOperand(MCOperand::CreateImm(Val));
1241 void addMemImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1242 assert(N == 2 && "Invalid number of operands!");
1243 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1244 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1245 Inst.addOperand(MCOperand::CreateImm(Val));
1248 void addMemPosImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1249 addMemImm8OffsetOperands(Inst, N);
1252 void addMemNegImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1253 addMemImm8OffsetOperands(Inst, N);
1256 void addMemUImm12OffsetOperands(MCInst &Inst, unsigned N) const {
1257 assert(N == 2 && "Invalid number of operands!");
1258 // If this is an immediate, it's a label reference.
1259 if (Kind == k_Immediate) {
1260 addExpr(Inst, getImm());
1261 Inst.addOperand(MCOperand::CreateImm(0));
1265 // Otherwise, it's a normal memory reg+offset.
1266 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1267 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1268 Inst.addOperand(MCOperand::CreateImm(Val));
1271 void addMemImm12OffsetOperands(MCInst &Inst, unsigned N) const {
1272 assert(N == 2 && "Invalid number of operands!");
1273 // If this is an immediate, it's a label reference.
1274 if (Kind == k_Immediate) {
1275 addExpr(Inst, getImm());
1276 Inst.addOperand(MCOperand::CreateImm(0));
1280 // Otherwise, it's a normal memory reg+offset.
1281 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1282 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1283 Inst.addOperand(MCOperand::CreateImm(Val));
1286 void addMemTBBOperands(MCInst &Inst, unsigned N) const {
1287 assert(N == 2 && "Invalid number of operands!");
1288 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1289 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1292 void addMemTBHOperands(MCInst &Inst, unsigned N) const {
1293 assert(N == 2 && "Invalid number of operands!");
1294 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1295 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1298 void addMemRegOffsetOperands(MCInst &Inst, unsigned N) const {
1299 assert(N == 3 && "Invalid number of operands!");
1300 unsigned Val = ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
1301 Memory.ShiftImm, Memory.ShiftType);
1302 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1303 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1304 Inst.addOperand(MCOperand::CreateImm(Val));
1307 void addT2MemRegOffsetOperands(MCInst &Inst, unsigned N) const {
1308 assert(N == 3 && "Invalid number of operands!");
1309 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1310 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1311 Inst.addOperand(MCOperand::CreateImm(Memory.ShiftImm));
1314 void addMemThumbRROperands(MCInst &Inst, unsigned N) const {
1315 assert(N == 2 && "Invalid number of operands!");
1316 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1317 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1320 void addMemThumbRIs4Operands(MCInst &Inst, unsigned N) const {
1321 assert(N == 2 && "Invalid number of operands!");
1322 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
1323 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1324 Inst.addOperand(MCOperand::CreateImm(Val));
1327 void addMemThumbRIs2Operands(MCInst &Inst, unsigned N) const {
1328 assert(N == 2 && "Invalid number of operands!");
1329 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 2) : 0;
1330 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1331 Inst.addOperand(MCOperand::CreateImm(Val));
1334 void addMemThumbRIs1Operands(MCInst &Inst, unsigned N) const {
1335 assert(N == 2 && "Invalid number of operands!");
1336 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue()) : 0;
1337 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1338 Inst.addOperand(MCOperand::CreateImm(Val));
1341 void addMemThumbSPIOperands(MCInst &Inst, unsigned N) const {
1342 assert(N == 2 && "Invalid number of operands!");
1343 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
1344 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1345 Inst.addOperand(MCOperand::CreateImm(Val));
1348 void addPostIdxImm8Operands(MCInst &Inst, unsigned N) const {
1349 assert(N == 1 && "Invalid number of operands!");
1350 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1351 assert(CE && "non-constant post-idx-imm8 operand!");
1352 int Imm = CE->getValue();
1353 bool isAdd = Imm >= 0;
1354 if (Imm == INT32_MIN) Imm = 0;
1355 Imm = (Imm < 0 ? -Imm : Imm) | (int)isAdd << 8;
1356 Inst.addOperand(MCOperand::CreateImm(Imm));
1359 void addPostIdxRegOperands(MCInst &Inst, unsigned N) const {
1360 assert(N == 2 && "Invalid number of operands!");
1361 Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1362 Inst.addOperand(MCOperand::CreateImm(PostIdxReg.isAdd));
1365 void addPostIdxRegShiftedOperands(MCInst &Inst, unsigned N) const {
1366 assert(N == 2 && "Invalid number of operands!");
1367 Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1368 // The sign, shift type, and shift amount are encoded in a single operand
1369 // using the AM2 encoding helpers.
1370 ARM_AM::AddrOpc opc = PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub;
1371 unsigned Imm = ARM_AM::getAM2Opc(opc, PostIdxReg.ShiftImm,
1372 PostIdxReg.ShiftTy);
1373 Inst.addOperand(MCOperand::CreateImm(Imm));
1376 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
1377 assert(N == 1 && "Invalid number of operands!");
1378 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
1381 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
1382 assert(N == 1 && "Invalid number of operands!");
1383 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
1386 void addVectorIndex8Operands(MCInst &Inst, unsigned N) const {
1387 assert(N == 1 && "Invalid number of operands!");
1388 Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1391 void addVectorIndex16Operands(MCInst &Inst, unsigned N) const {
1392 assert(N == 1 && "Invalid number of operands!");
1393 Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1396 void addVectorIndex32Operands(MCInst &Inst, unsigned N) const {
1397 assert(N == 1 && "Invalid number of operands!");
1398 Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1401 virtual void print(raw_ostream &OS) const;
1403 static ARMOperand *CreateITMask(unsigned Mask, SMLoc S) {
1404 ARMOperand *Op = new ARMOperand(k_ITCondMask);
1405 Op->ITMask.Mask = Mask;
1411 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
1412 ARMOperand *Op = new ARMOperand(k_CondCode);
1419 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
1420 ARMOperand *Op = new ARMOperand(k_CoprocNum);
1421 Op->Cop.Val = CopVal;
1427 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
1428 ARMOperand *Op = new ARMOperand(k_CoprocReg);
1429 Op->Cop.Val = CopVal;
1435 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
1436 ARMOperand *Op = new ARMOperand(k_CCOut);
1437 Op->Reg.RegNum = RegNum;
1443 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
1444 ARMOperand *Op = new ARMOperand(k_Token);
1445 Op->Tok.Data = Str.data();
1446 Op->Tok.Length = Str.size();
1452 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
1453 ARMOperand *Op = new ARMOperand(k_Register);
1454 Op->Reg.RegNum = RegNum;
1460 static ARMOperand *CreateShiftedRegister(ARM_AM::ShiftOpc ShTy,
1465 ARMOperand *Op = new ARMOperand(k_ShiftedRegister);
1466 Op->RegShiftedReg.ShiftTy = ShTy;
1467 Op->RegShiftedReg.SrcReg = SrcReg;
1468 Op->RegShiftedReg.ShiftReg = ShiftReg;
1469 Op->RegShiftedReg.ShiftImm = ShiftImm;
1475 static ARMOperand *CreateShiftedImmediate(ARM_AM::ShiftOpc ShTy,
1479 ARMOperand *Op = new ARMOperand(k_ShiftedImmediate);
1480 Op->RegShiftedImm.ShiftTy = ShTy;
1481 Op->RegShiftedImm.SrcReg = SrcReg;
1482 Op->RegShiftedImm.ShiftImm = ShiftImm;
1488 static ARMOperand *CreateShifterImm(bool isASR, unsigned Imm,
1490 ARMOperand *Op = new ARMOperand(k_ShifterImmediate);
1491 Op->ShifterImm.isASR = isASR;
1492 Op->ShifterImm.Imm = Imm;
1498 static ARMOperand *CreateRotImm(unsigned Imm, SMLoc S, SMLoc E) {
1499 ARMOperand *Op = new ARMOperand(k_RotateImmediate);
1500 Op->RotImm.Imm = Imm;
1506 static ARMOperand *CreateBitfield(unsigned LSB, unsigned Width,
1508 ARMOperand *Op = new ARMOperand(k_BitfieldDescriptor);
1509 Op->Bitfield.LSB = LSB;
1510 Op->Bitfield.Width = Width;
1517 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
1518 SMLoc StartLoc, SMLoc EndLoc) {
1519 KindTy Kind = k_RegisterList;
1521 if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Regs.front().first))
1522 Kind = k_DPRRegisterList;
1523 else if (ARMMCRegisterClasses[ARM::SPRRegClassID].
1524 contains(Regs.front().first))
1525 Kind = k_SPRRegisterList;
1527 ARMOperand *Op = new ARMOperand(Kind);
1528 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
1529 I = Regs.begin(), E = Regs.end(); I != E; ++I)
1530 Op->Registers.push_back(I->first);
1531 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
1532 Op->StartLoc = StartLoc;
1533 Op->EndLoc = EndLoc;
1537 static ARMOperand *CreateVectorIndex(unsigned Idx, SMLoc S, SMLoc E,
1539 ARMOperand *Op = new ARMOperand(k_VectorIndex);
1540 Op->VectorIndex.Val = Idx;
1546 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
1547 ARMOperand *Op = new ARMOperand(k_Immediate);
1554 static ARMOperand *CreateFPImm(unsigned Val, SMLoc S, MCContext &Ctx) {
1555 ARMOperand *Op = new ARMOperand(k_FPImmediate);
1556 Op->FPImm.Val = Val;
1562 static ARMOperand *CreateMem(unsigned BaseRegNum,
1563 const MCConstantExpr *OffsetImm,
1564 unsigned OffsetRegNum,
1565 ARM_AM::ShiftOpc ShiftType,
1570 ARMOperand *Op = new ARMOperand(k_Memory);
1571 Op->Memory.BaseRegNum = BaseRegNum;
1572 Op->Memory.OffsetImm = OffsetImm;
1573 Op->Memory.OffsetRegNum = OffsetRegNum;
1574 Op->Memory.ShiftType = ShiftType;
1575 Op->Memory.ShiftImm = ShiftImm;
1576 Op->Memory.Alignment = Alignment;
1577 Op->Memory.isNegative = isNegative;
1583 static ARMOperand *CreatePostIdxReg(unsigned RegNum, bool isAdd,
1584 ARM_AM::ShiftOpc ShiftTy,
1587 ARMOperand *Op = new ARMOperand(k_PostIndexRegister);
1588 Op->PostIdxReg.RegNum = RegNum;
1589 Op->PostIdxReg.isAdd = isAdd;
1590 Op->PostIdxReg.ShiftTy = ShiftTy;
1591 Op->PostIdxReg.ShiftImm = ShiftImm;
1597 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
1598 ARMOperand *Op = new ARMOperand(k_MemBarrierOpt);
1599 Op->MBOpt.Val = Opt;
1605 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
1606 ARMOperand *Op = new ARMOperand(k_ProcIFlags);
1607 Op->IFlags.Val = IFlags;
1613 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
1614 ARMOperand *Op = new ARMOperand(k_MSRMask);
1615 Op->MMask.Val = MMask;
1622 } // end anonymous namespace.
1624 void ARMOperand::print(raw_ostream &OS) const {
1627 OS << "<fpimm " << getFPImm() << "(" << ARM_AM::getFPImmFloat(getFPImm())
1631 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
1634 OS << "<ccout " << getReg() << ">";
1636 case k_ITCondMask: {
1637 static char MaskStr[][6] = { "()", "(t)", "(e)", "(tt)", "(et)", "(te)",
1638 "(ee)", "(ttt)", "(ett)", "(tet)", "(eet)", "(tte)", "(ete)",
1640 assert((ITMask.Mask & 0xf) == ITMask.Mask);
1641 OS << "<it-mask " << MaskStr[ITMask.Mask] << ">";
1645 OS << "<coprocessor number: " << getCoproc() << ">";
1648 OS << "<coprocessor register: " << getCoproc() << ">";
1651 OS << "<mask: " << getMSRMask() << ">";
1654 getImm()->print(OS);
1656 case k_MemBarrierOpt:
1657 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
1661 << " base:" << Memory.BaseRegNum;
1664 case k_PostIndexRegister:
1665 OS << "post-idx register " << (PostIdxReg.isAdd ? "" : "-")
1666 << PostIdxReg.RegNum;
1667 if (PostIdxReg.ShiftTy != ARM_AM::no_shift)
1668 OS << ARM_AM::getShiftOpcStr(PostIdxReg.ShiftTy) << " "
1669 << PostIdxReg.ShiftImm;
1672 case k_ProcIFlags: {
1673 OS << "<ARM_PROC::";
1674 unsigned IFlags = getProcIFlags();
1675 for (int i=2; i >= 0; --i)
1676 if (IFlags & (1 << i))
1677 OS << ARM_PROC::IFlagsToString(1 << i);
1682 OS << "<register " << getReg() << ">";
1684 case k_ShifterImmediate:
1685 OS << "<shift " << (ShifterImm.isASR ? "asr" : "lsl")
1686 << " #" << ShifterImm.Imm << ">";
1688 case k_ShiftedRegister:
1689 OS << "<so_reg_reg "
1690 << RegShiftedReg.SrcReg
1691 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(RegShiftedReg.ShiftImm))
1692 << ", " << RegShiftedReg.ShiftReg << ", "
1693 << ARM_AM::getSORegOffset(RegShiftedReg.ShiftImm)
1696 case k_ShiftedImmediate:
1697 OS << "<so_reg_imm "
1698 << RegShiftedImm.SrcReg
1699 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(RegShiftedImm.ShiftImm))
1700 << ", " << ARM_AM::getSORegOffset(RegShiftedImm.ShiftImm)
1703 case k_RotateImmediate:
1704 OS << "<ror " << " #" << (RotImm.Imm * 8) << ">";
1706 case k_BitfieldDescriptor:
1707 OS << "<bitfield " << "lsb: " << Bitfield.LSB
1708 << ", width: " << Bitfield.Width << ">";
1710 case k_RegisterList:
1711 case k_DPRRegisterList:
1712 case k_SPRRegisterList: {
1713 OS << "<register_list ";
1715 const SmallVectorImpl<unsigned> &RegList = getRegList();
1716 for (SmallVectorImpl<unsigned>::const_iterator
1717 I = RegList.begin(), E = RegList.end(); I != E; ) {
1719 if (++I < E) OS << ", ";
1726 OS << "'" << getToken() << "'";
1729 OS << "<vectorindex " << getVectorIndex() << ">";
1734 /// @name Auto-generated Match Functions
1737 static unsigned MatchRegisterName(StringRef Name);
1741 bool ARMAsmParser::ParseRegister(unsigned &RegNo,
1742 SMLoc &StartLoc, SMLoc &EndLoc) {
1743 RegNo = tryParseRegister();
1745 return (RegNo == (unsigned)-1);
1748 /// Try to parse a register name. The token must be an Identifier when called,
1749 /// and if it is a register name the token is eaten and the register number is
1750 /// returned. Otherwise return -1.
1752 int ARMAsmParser::tryParseRegister() {
1753 const AsmToken &Tok = Parser.getTok();
1754 if (Tok.isNot(AsmToken::Identifier)) return -1;
1756 // FIXME: Validate register for the current architecture; we have to do
1757 // validation later, so maybe there is no need for this here.
1758 std::string upperCase = Tok.getString().str();
1759 std::string lowerCase = LowercaseString(upperCase);
1760 unsigned RegNum = MatchRegisterName(lowerCase);
1762 RegNum = StringSwitch<unsigned>(lowerCase)
1763 .Case("r13", ARM::SP)
1764 .Case("r14", ARM::LR)
1765 .Case("r15", ARM::PC)
1766 .Case("ip", ARM::R12)
1769 if (!RegNum) return -1;
1771 Parser.Lex(); // Eat identifier token.
1774 // Also check for an index operand. This is only legal for vector registers,
1775 // but that'll get caught OK in operand matching, so we don't need to
1776 // explicitly filter everything else out here.
1777 if (Parser.getTok().is(AsmToken::LBrac)) {
1778 SMLoc SIdx = Parser.getTok().getLoc();
1779 Parser.Lex(); // Eat left bracket token.
1781 const MCExpr *ImmVal;
1782 SMLoc ExprLoc = Parser.getTok().getLoc();
1783 if (getParser().ParseExpression(ImmVal))
1784 return MatchOperand_ParseFail;
1785 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(ImmVal);
1787 TokError("immediate value expected for vector index");
1788 return MatchOperand_ParseFail;
1791 SMLoc E = Parser.getTok().getLoc();
1792 if (Parser.getTok().isNot(AsmToken::RBrac)) {
1793 Error(E, "']' expected");
1794 return MatchOperand_ParseFail;
1797 Parser.Lex(); // Eat right bracket token.
1799 Operands.push_back(ARMOperand::CreateVectorIndex(MCE->getValue(),
1808 // Try to parse a shifter (e.g., "lsl <amt>"). On success, return 0.
1809 // If a recoverable error occurs, return 1. If an irrecoverable error
1810 // occurs, return -1. An irrecoverable error is one where tokens have been
1811 // consumed in the process of trying to parse the shifter (i.e., when it is
1812 // indeed a shifter operand, but malformed).
1813 int ARMAsmParser::tryParseShiftRegister(
1814 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1815 SMLoc S = Parser.getTok().getLoc();
1816 const AsmToken &Tok = Parser.getTok();
1817 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1819 std::string upperCase = Tok.getString().str();
1820 std::string lowerCase = LowercaseString(upperCase);
1821 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
1822 .Case("lsl", ARM_AM::lsl)
1823 .Case("lsr", ARM_AM::lsr)
1824 .Case("asr", ARM_AM::asr)
1825 .Case("ror", ARM_AM::ror)
1826 .Case("rrx", ARM_AM::rrx)
1827 .Default(ARM_AM::no_shift);
1829 if (ShiftTy == ARM_AM::no_shift)
1832 Parser.Lex(); // Eat the operator.
1834 // The source register for the shift has already been added to the
1835 // operand list, so we need to pop it off and combine it into the shifted
1836 // register operand instead.
1837 OwningPtr<ARMOperand> PrevOp((ARMOperand*)Operands.pop_back_val());
1838 if (!PrevOp->isReg())
1839 return Error(PrevOp->getStartLoc(), "shift must be of a register");
1840 int SrcReg = PrevOp->getReg();
1843 if (ShiftTy == ARM_AM::rrx) {
1844 // RRX Doesn't have an explicit shift amount. The encoder expects
1845 // the shift register to be the same as the source register. Seems odd,
1849 // Figure out if this is shifted by a constant or a register (for non-RRX).
1850 if (Parser.getTok().is(AsmToken::Hash)) {
1851 Parser.Lex(); // Eat hash.
1852 SMLoc ImmLoc = Parser.getTok().getLoc();
1853 const MCExpr *ShiftExpr = 0;
1854 if (getParser().ParseExpression(ShiftExpr)) {
1855 Error(ImmLoc, "invalid immediate shift value");
1858 // The expression must be evaluatable as an immediate.
1859 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
1861 Error(ImmLoc, "invalid immediate shift value");
1864 // Range check the immediate.
1865 // lsl, ror: 0 <= imm <= 31
1866 // lsr, asr: 0 <= imm <= 32
1867 Imm = CE->getValue();
1869 ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
1870 ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
1871 Error(ImmLoc, "immediate shift value out of range");
1874 } else if (Parser.getTok().is(AsmToken::Identifier)) {
1875 ShiftReg = tryParseRegister();
1876 SMLoc L = Parser.getTok().getLoc();
1877 if (ShiftReg == -1) {
1878 Error (L, "expected immediate or register in shift operand");
1882 Error (Parser.getTok().getLoc(),
1883 "expected immediate or register in shift operand");
1888 if (ShiftReg && ShiftTy != ARM_AM::rrx)
1889 Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
1891 S, Parser.getTok().getLoc()));
1893 Operands.push_back(ARMOperand::CreateShiftedImmediate(ShiftTy, SrcReg, Imm,
1894 S, Parser.getTok().getLoc()));
1900 /// Try to parse a register name. The token must be an Identifier when called.
1901 /// If it's a register, an AsmOperand is created. Another AsmOperand is created
1902 /// if there is a "writeback". 'true' if it's not a register.
1904 /// TODO this is likely to change to allow different register types and or to
1905 /// parse for a specific register type.
1907 tryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1908 SMLoc S = Parser.getTok().getLoc();
1909 int RegNo = tryParseRegister();
1913 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
1915 const AsmToken &ExclaimTok = Parser.getTok();
1916 if (ExclaimTok.is(AsmToken::Exclaim)) {
1917 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
1918 ExclaimTok.getLoc()));
1919 Parser.Lex(); // Eat exclaim token
1923 // Also check for an index operand. This is only legal for vector registers,
1924 // but that'll get caught OK in operand matching, so we don't need to
1925 // explicitly filter everything else out here.
1926 if (Parser.getTok().is(AsmToken::LBrac)) {
1927 SMLoc SIdx = Parser.getTok().getLoc();
1928 Parser.Lex(); // Eat left bracket token.
1930 const MCExpr *ImmVal;
1931 SMLoc ExprLoc = Parser.getTok().getLoc();
1932 if (getParser().ParseExpression(ImmVal))
1933 return MatchOperand_ParseFail;
1934 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(ImmVal);
1936 TokError("immediate value expected for vector index");
1937 return MatchOperand_ParseFail;
1940 SMLoc E = Parser.getTok().getLoc();
1941 if (Parser.getTok().isNot(AsmToken::RBrac)) {
1942 Error(E, "']' expected");
1943 return MatchOperand_ParseFail;
1946 Parser.Lex(); // Eat right bracket token.
1948 Operands.push_back(ARMOperand::CreateVectorIndex(MCE->getValue(),
1956 /// MatchCoprocessorOperandName - Try to parse an coprocessor related
1957 /// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
1959 static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
1960 // Use the same layout as the tablegen'erated register name matcher. Ugly,
1962 switch (Name.size()) {
1965 if (Name[0] != CoprocOp)
1982 if (Name[0] != CoprocOp || Name[1] != '1')
1986 case '0': return 10;
1987 case '1': return 11;
1988 case '2': return 12;
1989 case '3': return 13;
1990 case '4': return 14;
1991 case '5': return 15;
1999 /// parseITCondCode - Try to parse a condition code for an IT instruction.
2000 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2001 parseITCondCode(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2002 SMLoc S = Parser.getTok().getLoc();
2003 const AsmToken &Tok = Parser.getTok();
2004 if (!Tok.is(AsmToken::Identifier))
2005 return MatchOperand_NoMatch;
2006 unsigned CC = StringSwitch<unsigned>(Tok.getString())
2007 .Case("eq", ARMCC::EQ)
2008 .Case("ne", ARMCC::NE)
2009 .Case("hs", ARMCC::HS)
2010 .Case("cs", ARMCC::HS)
2011 .Case("lo", ARMCC::LO)
2012 .Case("cc", ARMCC::LO)
2013 .Case("mi", ARMCC::MI)
2014 .Case("pl", ARMCC::PL)
2015 .Case("vs", ARMCC::VS)
2016 .Case("vc", ARMCC::VC)
2017 .Case("hi", ARMCC::HI)
2018 .Case("ls", ARMCC::LS)
2019 .Case("ge", ARMCC::GE)
2020 .Case("lt", ARMCC::LT)
2021 .Case("gt", ARMCC::GT)
2022 .Case("le", ARMCC::LE)
2023 .Case("al", ARMCC::AL)
2026 return MatchOperand_NoMatch;
2027 Parser.Lex(); // Eat the token.
2029 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), S));
2031 return MatchOperand_Success;
2034 /// parseCoprocNumOperand - Try to parse an coprocessor number operand. The
2035 /// token must be an Identifier when called, and if it is a coprocessor
2036 /// number, the token is eaten and the operand is added to the operand list.
2037 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2038 parseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2039 SMLoc S = Parser.getTok().getLoc();
2040 const AsmToken &Tok = Parser.getTok();
2041 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2043 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
2045 return MatchOperand_NoMatch;
2047 Parser.Lex(); // Eat identifier token.
2048 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
2049 return MatchOperand_Success;
2052 /// parseCoprocRegOperand - Try to parse an coprocessor register operand. The
2053 /// token must be an Identifier when called, and if it is a coprocessor
2054 /// number, the token is eaten and the operand is added to the operand list.
2055 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2056 parseCoprocRegOperand(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 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
2063 return MatchOperand_NoMatch;
2065 Parser.Lex(); // Eat identifier token.
2066 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
2067 return MatchOperand_Success;
2070 // For register list parsing, we need to map from raw GPR register numbering
2071 // to the enumeration values. The enumeration values aren't sorted by
2072 // register number due to our using "sp", "lr" and "pc" as canonical names.
2073 static unsigned getNextRegister(unsigned Reg) {
2074 // If this is a GPR, we need to do it manually, otherwise we can rely
2075 // on the sort ordering of the enumeration since the other reg-classes
2077 if (!ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
2080 default: assert(0 && "Invalid GPR number!");
2081 case ARM::R0: return ARM::R1; case ARM::R1: return ARM::R2;
2082 case ARM::R2: return ARM::R3; case ARM::R3: return ARM::R4;
2083 case ARM::R4: return ARM::R5; case ARM::R5: return ARM::R6;
2084 case ARM::R6: return ARM::R7; case ARM::R7: return ARM::R8;
2085 case ARM::R8: return ARM::R9; case ARM::R9: return ARM::R10;
2086 case ARM::R10: return ARM::R11; case ARM::R11: return ARM::R12;
2087 case ARM::R12: return ARM::SP; case ARM::SP: return ARM::LR;
2088 case ARM::LR: return ARM::PC; case ARM::PC: return ARM::R0;
2092 /// Parse a register list.
2094 parseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2095 assert(Parser.getTok().is(AsmToken::LCurly) &&
2096 "Token is not a Left Curly Brace");
2097 SMLoc S = Parser.getTok().getLoc();
2098 Parser.Lex(); // Eat '{' token.
2099 SMLoc RegLoc = Parser.getTok().getLoc();
2101 // Check the first register in the list to see what register class
2102 // this is a list of.
2103 int Reg = tryParseRegister();
2105 return Error(RegLoc, "register expected");
2107 MCRegisterClass *RC;
2108 if (ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
2109 RC = &ARMMCRegisterClasses[ARM::GPRRegClassID];
2110 else if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg))
2111 RC = &ARMMCRegisterClasses[ARM::DPRRegClassID];
2112 else if (ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg))
2113 RC = &ARMMCRegisterClasses[ARM::SPRRegClassID];
2115 return Error(RegLoc, "invalid register in register list");
2117 // The reglist instructions have at most 16 registers, so reserve
2118 // space for that many.
2119 SmallVector<std::pair<unsigned, SMLoc>, 16> Registers;
2120 // Store the first register.
2121 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2123 // This starts immediately after the first register token in the list,
2124 // so we can see either a comma or a minus (range separator) as a legal
2126 while (Parser.getTok().is(AsmToken::Comma) ||
2127 Parser.getTok().is(AsmToken::Minus)) {
2128 if (Parser.getTok().is(AsmToken::Minus)) {
2129 Parser.Lex(); // Eat the comma.
2130 SMLoc EndLoc = Parser.getTok().getLoc();
2131 int EndReg = tryParseRegister();
2133 return Error(EndLoc, "register expected");
2134 // If the register is the same as the start reg, there's nothing
2138 // The register must be in the same register class as the first.
2139 if (!RC->contains(EndReg))
2140 return Error(EndLoc, "invalid register in register list");
2141 // Ranges must go from low to high.
2142 if (getARMRegisterNumbering(Reg) > getARMRegisterNumbering(EndReg))
2143 return Error(EndLoc, "bad range in register list");
2145 // Add all the registers in the range to the register list.
2146 while (Reg != EndReg) {
2147 Reg = getNextRegister(Reg);
2148 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2152 Parser.Lex(); // Eat the comma.
2153 RegLoc = Parser.getTok().getLoc();
2155 Reg = tryParseRegister();
2157 return Error(RegLoc, "register expected");
2158 // The register must be in the same register class as the first.
2159 if (!RC->contains(Reg))
2160 return Error(RegLoc, "invalid register in register list");
2161 // List must be monotonically increasing.
2162 if (getARMRegisterNumbering(Reg) <= getARMRegisterNumbering(OldReg))
2163 return Error(RegLoc, "register list not in ascending order");
2164 // VFP register lists must also be contiguous.
2165 // It's OK to use the enumeration values directly here rather, as the
2166 // VFP register classes have the enum sorted properly.
2167 if (RC != &ARMMCRegisterClasses[ARM::GPRRegClassID] &&
2169 return Error(RegLoc, "non-contiguous register range");
2170 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2173 SMLoc E = Parser.getTok().getLoc();
2174 if (Parser.getTok().isNot(AsmToken::RCurly))
2175 return Error(E, "'}' expected");
2176 Parser.Lex(); // Eat '}' token.
2178 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
2182 /// parseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
2183 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2184 parseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2185 SMLoc S = Parser.getTok().getLoc();
2186 const AsmToken &Tok = Parser.getTok();
2187 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2188 StringRef OptStr = Tok.getString();
2190 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
2191 .Case("sy", ARM_MB::SY)
2192 .Case("st", ARM_MB::ST)
2193 .Case("sh", ARM_MB::ISH)
2194 .Case("ish", ARM_MB::ISH)
2195 .Case("shst", ARM_MB::ISHST)
2196 .Case("ishst", ARM_MB::ISHST)
2197 .Case("nsh", ARM_MB::NSH)
2198 .Case("un", ARM_MB::NSH)
2199 .Case("nshst", ARM_MB::NSHST)
2200 .Case("unst", ARM_MB::NSHST)
2201 .Case("osh", ARM_MB::OSH)
2202 .Case("oshst", ARM_MB::OSHST)
2206 return MatchOperand_NoMatch;
2208 Parser.Lex(); // Eat identifier token.
2209 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
2210 return MatchOperand_Success;
2213 /// parseProcIFlagsOperand - Try to parse iflags from CPS instruction.
2214 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2215 parseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2216 SMLoc S = Parser.getTok().getLoc();
2217 const AsmToken &Tok = Parser.getTok();
2218 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2219 StringRef IFlagsStr = Tok.getString();
2221 // An iflags string of "none" is interpreted to mean that none of the AIF
2222 // bits are set. Not a terribly useful instruction, but a valid encoding.
2223 unsigned IFlags = 0;
2224 if (IFlagsStr != "none") {
2225 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
2226 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
2227 .Case("a", ARM_PROC::A)
2228 .Case("i", ARM_PROC::I)
2229 .Case("f", ARM_PROC::F)
2232 // If some specific iflag is already set, it means that some letter is
2233 // present more than once, this is not acceptable.
2234 if (Flag == ~0U || (IFlags & Flag))
2235 return MatchOperand_NoMatch;
2241 Parser.Lex(); // Eat identifier token.
2242 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
2243 return MatchOperand_Success;
2246 /// parseMSRMaskOperand - Try to parse mask flags from MSR instruction.
2247 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2248 parseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2249 SMLoc S = Parser.getTok().getLoc();
2250 const AsmToken &Tok = Parser.getTok();
2251 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2252 StringRef Mask = Tok.getString();
2255 // See ARMv6-M 10.1.1
2256 unsigned FlagsVal = StringSwitch<unsigned>(Mask)
2266 .Case("primask", 16)
2267 .Case("basepri", 17)
2268 .Case("basepri_max", 18)
2269 .Case("faultmask", 19)
2270 .Case("control", 20)
2273 if (FlagsVal == ~0U)
2274 return MatchOperand_NoMatch;
2276 if (!hasV7Ops() && FlagsVal >= 17 && FlagsVal <= 19)
2277 // basepri, basepri_max and faultmask only valid for V7m.
2278 return MatchOperand_NoMatch;
2280 Parser.Lex(); // Eat identifier token.
2281 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
2282 return MatchOperand_Success;
2285 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
2286 size_t Start = 0, Next = Mask.find('_');
2287 StringRef Flags = "";
2288 std::string SpecReg = LowercaseString(Mask.slice(Start, Next));
2289 if (Next != StringRef::npos)
2290 Flags = Mask.slice(Next+1, Mask.size());
2292 // FlagsVal contains the complete mask:
2294 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
2295 unsigned FlagsVal = 0;
2297 if (SpecReg == "apsr") {
2298 FlagsVal = StringSwitch<unsigned>(Flags)
2299 .Case("nzcvq", 0x8) // same as CPSR_f
2300 .Case("g", 0x4) // same as CPSR_s
2301 .Case("nzcvqg", 0xc) // same as CPSR_fs
2304 if (FlagsVal == ~0U) {
2306 return MatchOperand_NoMatch;
2308 FlagsVal = 8; // No flag
2310 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
2311 if (Flags == "all") // cpsr_all is an alias for cpsr_fc
2313 for (int i = 0, e = Flags.size(); i != e; ++i) {
2314 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
2321 // If some specific flag is already set, it means that some letter is
2322 // present more than once, this is not acceptable.
2323 if (FlagsVal == ~0U || (FlagsVal & Flag))
2324 return MatchOperand_NoMatch;
2327 } else // No match for special register.
2328 return MatchOperand_NoMatch;
2330 // Special register without flags are equivalent to "fc" flags.
2334 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
2335 if (SpecReg == "spsr")
2338 Parser.Lex(); // Eat identifier token.
2339 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
2340 return MatchOperand_Success;
2343 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2344 parsePKHImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands, StringRef Op,
2345 int Low, int High) {
2346 const AsmToken &Tok = Parser.getTok();
2347 if (Tok.isNot(AsmToken::Identifier)) {
2348 Error(Parser.getTok().getLoc(), Op + " operand expected.");
2349 return MatchOperand_ParseFail;
2351 StringRef ShiftName = Tok.getString();
2352 std::string LowerOp = LowercaseString(Op);
2353 std::string UpperOp = UppercaseString(Op);
2354 if (ShiftName != LowerOp && ShiftName != UpperOp) {
2355 Error(Parser.getTok().getLoc(), Op + " operand expected.");
2356 return MatchOperand_ParseFail;
2358 Parser.Lex(); // Eat shift type token.
2360 // There must be a '#' and a shift amount.
2361 if (Parser.getTok().isNot(AsmToken::Hash)) {
2362 Error(Parser.getTok().getLoc(), "'#' expected");
2363 return MatchOperand_ParseFail;
2365 Parser.Lex(); // Eat hash token.
2367 const MCExpr *ShiftAmount;
2368 SMLoc Loc = Parser.getTok().getLoc();
2369 if (getParser().ParseExpression(ShiftAmount)) {
2370 Error(Loc, "illegal expression");
2371 return MatchOperand_ParseFail;
2373 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
2375 Error(Loc, "constant expression expected");
2376 return MatchOperand_ParseFail;
2378 int Val = CE->getValue();
2379 if (Val < Low || Val > High) {
2380 Error(Loc, "immediate value out of range");
2381 return MatchOperand_ParseFail;
2384 Operands.push_back(ARMOperand::CreateImm(CE, Loc, Parser.getTok().getLoc()));
2386 return MatchOperand_Success;
2389 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2390 parseSetEndImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2391 const AsmToken &Tok = Parser.getTok();
2392 SMLoc S = Tok.getLoc();
2393 if (Tok.isNot(AsmToken::Identifier)) {
2394 Error(Tok.getLoc(), "'be' or 'le' operand expected");
2395 return MatchOperand_ParseFail;
2397 int Val = StringSwitch<int>(Tok.getString())
2401 Parser.Lex(); // Eat the token.
2404 Error(Tok.getLoc(), "'be' or 'le' operand expected");
2405 return MatchOperand_ParseFail;
2407 Operands.push_back(ARMOperand::CreateImm(MCConstantExpr::Create(Val,
2409 S, Parser.getTok().getLoc()));
2410 return MatchOperand_Success;
2413 /// parseShifterImm - Parse the shifter immediate operand for SSAT/USAT
2414 /// instructions. Legal values are:
2415 /// lsl #n 'n' in [0,31]
2416 /// asr #n 'n' in [1,32]
2417 /// n == 32 encoded as n == 0.
2418 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2419 parseShifterImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2420 const AsmToken &Tok = Parser.getTok();
2421 SMLoc S = Tok.getLoc();
2422 if (Tok.isNot(AsmToken::Identifier)) {
2423 Error(S, "shift operator 'asr' or 'lsl' expected");
2424 return MatchOperand_ParseFail;
2426 StringRef ShiftName = Tok.getString();
2428 if (ShiftName == "lsl" || ShiftName == "LSL")
2430 else if (ShiftName == "asr" || ShiftName == "ASR")
2433 Error(S, "shift operator 'asr' or 'lsl' expected");
2434 return MatchOperand_ParseFail;
2436 Parser.Lex(); // Eat the operator.
2438 // A '#' and a shift amount.
2439 if (Parser.getTok().isNot(AsmToken::Hash)) {
2440 Error(Parser.getTok().getLoc(), "'#' expected");
2441 return MatchOperand_ParseFail;
2443 Parser.Lex(); // Eat hash token.
2445 const MCExpr *ShiftAmount;
2446 SMLoc E = Parser.getTok().getLoc();
2447 if (getParser().ParseExpression(ShiftAmount)) {
2448 Error(E, "malformed shift expression");
2449 return MatchOperand_ParseFail;
2451 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
2453 Error(E, "shift amount must be an immediate");
2454 return MatchOperand_ParseFail;
2457 int64_t Val = CE->getValue();
2459 // Shift amount must be in [1,32]
2460 if (Val < 1 || Val > 32) {
2461 Error(E, "'asr' shift amount must be in range [1,32]");
2462 return MatchOperand_ParseFail;
2464 // asr #32 encoded as asr #0, but is not allowed in Thumb2 mode.
2465 if (isThumb() && Val == 32) {
2466 Error(E, "'asr #32' shift amount not allowed in Thumb mode");
2467 return MatchOperand_ParseFail;
2469 if (Val == 32) Val = 0;
2471 // Shift amount must be in [1,32]
2472 if (Val < 0 || Val > 31) {
2473 Error(E, "'lsr' shift amount must be in range [0,31]");
2474 return MatchOperand_ParseFail;
2478 E = Parser.getTok().getLoc();
2479 Operands.push_back(ARMOperand::CreateShifterImm(isASR, Val, S, E));
2481 return MatchOperand_Success;
2484 /// parseRotImm - Parse the shifter immediate operand for SXTB/UXTB family
2485 /// of instructions. Legal values are:
2486 /// ror #n 'n' in {0, 8, 16, 24}
2487 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2488 parseRotImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2489 const AsmToken &Tok = Parser.getTok();
2490 SMLoc S = Tok.getLoc();
2491 if (Tok.isNot(AsmToken::Identifier))
2492 return MatchOperand_NoMatch;
2493 StringRef ShiftName = Tok.getString();
2494 if (ShiftName != "ror" && ShiftName != "ROR")
2495 return MatchOperand_NoMatch;
2496 Parser.Lex(); // Eat the operator.
2498 // A '#' and a rotate amount.
2499 if (Parser.getTok().isNot(AsmToken::Hash)) {
2500 Error(Parser.getTok().getLoc(), "'#' expected");
2501 return MatchOperand_ParseFail;
2503 Parser.Lex(); // Eat hash token.
2505 const MCExpr *ShiftAmount;
2506 SMLoc E = Parser.getTok().getLoc();
2507 if (getParser().ParseExpression(ShiftAmount)) {
2508 Error(E, "malformed rotate expression");
2509 return MatchOperand_ParseFail;
2511 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
2513 Error(E, "rotate amount must be an immediate");
2514 return MatchOperand_ParseFail;
2517 int64_t Val = CE->getValue();
2518 // Shift amount must be in {0, 8, 16, 24} (0 is undocumented extension)
2519 // normally, zero is represented in asm by omitting the rotate operand
2521 if (Val != 8 && Val != 16 && Val != 24 && Val != 0) {
2522 Error(E, "'ror' rotate amount must be 8, 16, or 24");
2523 return MatchOperand_ParseFail;
2526 E = Parser.getTok().getLoc();
2527 Operands.push_back(ARMOperand::CreateRotImm(Val, S, E));
2529 return MatchOperand_Success;
2532 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2533 parseBitfield(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2534 SMLoc S = Parser.getTok().getLoc();
2535 // The bitfield descriptor is really two operands, the LSB and the width.
2536 if (Parser.getTok().isNot(AsmToken::Hash)) {
2537 Error(Parser.getTok().getLoc(), "'#' expected");
2538 return MatchOperand_ParseFail;
2540 Parser.Lex(); // Eat hash token.
2542 const MCExpr *LSBExpr;
2543 SMLoc E = Parser.getTok().getLoc();
2544 if (getParser().ParseExpression(LSBExpr)) {
2545 Error(E, "malformed immediate expression");
2546 return MatchOperand_ParseFail;
2548 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(LSBExpr);
2550 Error(E, "'lsb' operand must be an immediate");
2551 return MatchOperand_ParseFail;
2554 int64_t LSB = CE->getValue();
2555 // The LSB must be in the range [0,31]
2556 if (LSB < 0 || LSB > 31) {
2557 Error(E, "'lsb' operand must be in the range [0,31]");
2558 return MatchOperand_ParseFail;
2560 E = Parser.getTok().getLoc();
2562 // Expect another immediate operand.
2563 if (Parser.getTok().isNot(AsmToken::Comma)) {
2564 Error(Parser.getTok().getLoc(), "too few operands");
2565 return MatchOperand_ParseFail;
2567 Parser.Lex(); // Eat hash token.
2568 if (Parser.getTok().isNot(AsmToken::Hash)) {
2569 Error(Parser.getTok().getLoc(), "'#' expected");
2570 return MatchOperand_ParseFail;
2572 Parser.Lex(); // Eat hash token.
2574 const MCExpr *WidthExpr;
2575 if (getParser().ParseExpression(WidthExpr)) {
2576 Error(E, "malformed immediate expression");
2577 return MatchOperand_ParseFail;
2579 CE = dyn_cast<MCConstantExpr>(WidthExpr);
2581 Error(E, "'width' operand must be an immediate");
2582 return MatchOperand_ParseFail;
2585 int64_t Width = CE->getValue();
2586 // The LSB must be in the range [1,32-lsb]
2587 if (Width < 1 || Width > 32 - LSB) {
2588 Error(E, "'width' operand must be in the range [1,32-lsb]");
2589 return MatchOperand_ParseFail;
2591 E = Parser.getTok().getLoc();
2593 Operands.push_back(ARMOperand::CreateBitfield(LSB, Width, S, E));
2595 return MatchOperand_Success;
2598 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2599 parsePostIdxReg(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2600 // Check for a post-index addressing register operand. Specifically:
2601 // postidx_reg := '+' register {, shift}
2602 // | '-' register {, shift}
2603 // | register {, shift}
2605 // This method must return MatchOperand_NoMatch without consuming any tokens
2606 // in the case where there is no match, as other alternatives take other
2608 AsmToken Tok = Parser.getTok();
2609 SMLoc S = Tok.getLoc();
2610 bool haveEaten = false;
2613 if (Tok.is(AsmToken::Plus)) {
2614 Parser.Lex(); // Eat the '+' token.
2616 } else if (Tok.is(AsmToken::Minus)) {
2617 Parser.Lex(); // Eat the '-' token.
2621 if (Parser.getTok().is(AsmToken::Identifier))
2622 Reg = tryParseRegister();
2625 return MatchOperand_NoMatch;
2626 Error(Parser.getTok().getLoc(), "register expected");
2627 return MatchOperand_ParseFail;
2629 SMLoc E = Parser.getTok().getLoc();
2631 ARM_AM::ShiftOpc ShiftTy = ARM_AM::no_shift;
2632 unsigned ShiftImm = 0;
2633 if (Parser.getTok().is(AsmToken::Comma)) {
2634 Parser.Lex(); // Eat the ','.
2635 if (parseMemRegOffsetShift(ShiftTy, ShiftImm))
2636 return MatchOperand_ParseFail;
2639 Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ShiftTy,
2642 return MatchOperand_Success;
2645 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2646 parseAM3Offset(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2647 // Check for a post-index addressing register operand. Specifically:
2648 // am3offset := '+' register
2655 // This method must return MatchOperand_NoMatch without consuming any tokens
2656 // in the case where there is no match, as other alternatives take other
2658 AsmToken Tok = Parser.getTok();
2659 SMLoc S = Tok.getLoc();
2661 // Do immediates first, as we always parse those if we have a '#'.
2662 if (Parser.getTok().is(AsmToken::Hash)) {
2663 Parser.Lex(); // Eat the '#'.
2664 // Explicitly look for a '-', as we need to encode negative zero
2666 bool isNegative = Parser.getTok().is(AsmToken::Minus);
2667 const MCExpr *Offset;
2668 if (getParser().ParseExpression(Offset))
2669 return MatchOperand_ParseFail;
2670 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
2672 Error(S, "constant expression expected");
2673 return MatchOperand_ParseFail;
2675 SMLoc E = Tok.getLoc();
2676 // Negative zero is encoded as the flag value INT32_MIN.
2677 int32_t Val = CE->getValue();
2678 if (isNegative && Val == 0)
2682 ARMOperand::CreateImm(MCConstantExpr::Create(Val, getContext()), S, E));
2684 return MatchOperand_Success;
2688 bool haveEaten = false;
2691 if (Tok.is(AsmToken::Plus)) {
2692 Parser.Lex(); // Eat the '+' token.
2694 } else if (Tok.is(AsmToken::Minus)) {
2695 Parser.Lex(); // Eat the '-' token.
2699 if (Parser.getTok().is(AsmToken::Identifier))
2700 Reg = tryParseRegister();
2703 return MatchOperand_NoMatch;
2704 Error(Parser.getTok().getLoc(), "register expected");
2705 return MatchOperand_ParseFail;
2707 SMLoc E = Parser.getTok().getLoc();
2709 Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ARM_AM::no_shift,
2712 return MatchOperand_Success;
2715 /// cvtT2LdrdPre - Convert parsed operands to MCInst.
2716 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2717 /// when they refer multiple MIOperands inside a single one.
2719 cvtT2LdrdPre(MCInst &Inst, unsigned Opcode,
2720 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2722 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2723 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
2724 // Create a writeback register dummy placeholder.
2725 Inst.addOperand(MCOperand::CreateReg(0));
2727 ((ARMOperand*)Operands[4])->addMemImm8s4OffsetOperands(Inst, 2);
2729 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2733 /// cvtT2StrdPre - Convert parsed operands to MCInst.
2734 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2735 /// when they refer multiple MIOperands inside a single one.
2737 cvtT2StrdPre(MCInst &Inst, unsigned Opcode,
2738 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2739 // Create a writeback register dummy placeholder.
2740 Inst.addOperand(MCOperand::CreateReg(0));
2742 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2743 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
2745 ((ARMOperand*)Operands[4])->addMemImm8s4OffsetOperands(Inst, 2);
2747 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2751 /// cvtLdWriteBackRegT2AddrModeImm8 - Convert parsed operands to MCInst.
2752 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2753 /// when they refer multiple MIOperands inside a single one.
2755 cvtLdWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
2756 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2757 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2759 // Create a writeback register dummy placeholder.
2760 Inst.addOperand(MCOperand::CreateImm(0));
2762 ((ARMOperand*)Operands[3])->addMemImm8OffsetOperands(Inst, 2);
2763 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2767 /// cvtStWriteBackRegT2AddrModeImm8 - Convert parsed operands to MCInst.
2768 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2769 /// when they refer multiple MIOperands inside a single one.
2771 cvtStWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
2772 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2773 // Create a writeback register dummy placeholder.
2774 Inst.addOperand(MCOperand::CreateImm(0));
2775 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2776 ((ARMOperand*)Operands[3])->addMemImm8OffsetOperands(Inst, 2);
2777 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2781 /// cvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
2782 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2783 /// when they refer multiple MIOperands inside a single one.
2785 cvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
2786 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2787 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2789 // Create a writeback register dummy placeholder.
2790 Inst.addOperand(MCOperand::CreateImm(0));
2792 ((ARMOperand*)Operands[3])->addAddrMode2Operands(Inst, 3);
2793 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2797 /// cvtLdWriteBackRegAddrModeImm12 - Convert parsed operands to MCInst.
2798 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2799 /// when they refer multiple MIOperands inside a single one.
2801 cvtLdWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
2802 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2803 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2805 // Create a writeback register dummy placeholder.
2806 Inst.addOperand(MCOperand::CreateImm(0));
2808 ((ARMOperand*)Operands[3])->addMemImm12OffsetOperands(Inst, 2);
2809 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2814 /// cvtStWriteBackRegAddrModeImm12 - Convert parsed operands to MCInst.
2815 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2816 /// when they refer multiple MIOperands inside a single one.
2818 cvtStWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
2819 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2820 // Create a writeback register dummy placeholder.
2821 Inst.addOperand(MCOperand::CreateImm(0));
2822 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2823 ((ARMOperand*)Operands[3])->addMemImm12OffsetOperands(Inst, 2);
2824 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2828 /// cvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
2829 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2830 /// when they refer multiple MIOperands inside a single one.
2832 cvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
2833 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2834 // Create a writeback register dummy placeholder.
2835 Inst.addOperand(MCOperand::CreateImm(0));
2836 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2837 ((ARMOperand*)Operands[3])->addAddrMode2Operands(Inst, 3);
2838 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2842 /// cvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
2843 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2844 /// when they refer multiple MIOperands inside a single one.
2846 cvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
2847 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2848 // Create a writeback register dummy placeholder.
2849 Inst.addOperand(MCOperand::CreateImm(0));
2850 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2851 ((ARMOperand*)Operands[3])->addAddrMode3Operands(Inst, 3);
2852 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2856 /// cvtLdExtTWriteBackImm - Convert parsed operands to MCInst.
2857 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2858 /// when they refer multiple MIOperands inside a single one.
2860 cvtLdExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
2861 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2863 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2864 // Create a writeback register dummy placeholder.
2865 Inst.addOperand(MCOperand::CreateImm(0));
2867 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
2869 ((ARMOperand*)Operands[4])->addPostIdxImm8Operands(Inst, 1);
2871 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2875 /// cvtLdExtTWriteBackReg - Convert parsed operands to MCInst.
2876 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2877 /// when they refer multiple MIOperands inside a single one.
2879 cvtLdExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
2880 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2882 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2883 // Create a writeback register dummy placeholder.
2884 Inst.addOperand(MCOperand::CreateImm(0));
2886 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
2888 ((ARMOperand*)Operands[4])->addPostIdxRegOperands(Inst, 2);
2890 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2894 /// cvtStExtTWriteBackImm - Convert parsed operands to MCInst.
2895 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2896 /// when they refer multiple MIOperands inside a single one.
2898 cvtStExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
2899 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2900 // Create a writeback register dummy placeholder.
2901 Inst.addOperand(MCOperand::CreateImm(0));
2903 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2905 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
2907 ((ARMOperand*)Operands[4])->addPostIdxImm8Operands(Inst, 1);
2909 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2913 /// cvtStExtTWriteBackReg - Convert parsed operands to MCInst.
2914 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2915 /// when they refer multiple MIOperands inside a single one.
2917 cvtStExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
2918 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2919 // Create a writeback register dummy placeholder.
2920 Inst.addOperand(MCOperand::CreateImm(0));
2922 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2924 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
2926 ((ARMOperand*)Operands[4])->addPostIdxRegOperands(Inst, 2);
2928 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2932 /// cvtLdrdPre - Convert parsed operands to MCInst.
2933 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2934 /// when they refer multiple MIOperands inside a single one.
2936 cvtLdrdPre(MCInst &Inst, unsigned Opcode,
2937 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2939 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2940 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
2941 // Create a writeback register dummy placeholder.
2942 Inst.addOperand(MCOperand::CreateImm(0));
2944 ((ARMOperand*)Operands[4])->addAddrMode3Operands(Inst, 3);
2946 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2950 /// cvtStrdPre - Convert parsed operands to MCInst.
2951 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2952 /// when they refer multiple MIOperands inside a single one.
2954 cvtStrdPre(MCInst &Inst, unsigned Opcode,
2955 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2956 // Create a writeback register dummy placeholder.
2957 Inst.addOperand(MCOperand::CreateImm(0));
2959 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2960 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
2962 ((ARMOperand*)Operands[4])->addAddrMode3Operands(Inst, 3);
2964 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2968 /// cvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
2969 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2970 /// when they refer multiple MIOperands inside a single one.
2972 cvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
2973 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2974 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2975 // Create a writeback register dummy placeholder.
2976 Inst.addOperand(MCOperand::CreateImm(0));
2977 ((ARMOperand*)Operands[3])->addAddrMode3Operands(Inst, 3);
2978 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2982 /// cvtThumbMultiple- Convert parsed operands to MCInst.
2983 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2984 /// when they refer multiple MIOperands inside a single one.
2986 cvtThumbMultiply(MCInst &Inst, unsigned Opcode,
2987 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2988 // The second source operand must be the same register as the destination
2990 if (Operands.size() == 6 &&
2991 (((ARMOperand*)Operands[3])->getReg() !=
2992 ((ARMOperand*)Operands[5])->getReg()) &&
2993 (((ARMOperand*)Operands[3])->getReg() !=
2994 ((ARMOperand*)Operands[4])->getReg())) {
2995 Error(Operands[3]->getStartLoc(),
2996 "destination register must match source register");
2999 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3000 ((ARMOperand*)Operands[1])->addCCOutOperands(Inst, 1);
3001 ((ARMOperand*)Operands[4])->addRegOperands(Inst, 1);
3002 // If we have a three-operand form, use that, else the second source operand
3003 // is just the destination operand again.
3004 if (Operands.size() == 6)
3005 ((ARMOperand*)Operands[5])->addRegOperands(Inst, 1);
3007 Inst.addOperand(Inst.getOperand(0));
3008 ((ARMOperand*)Operands[2])->addCondCodeOperands(Inst, 2);
3013 /// Parse an ARM memory expression, return false if successful else return true
3014 /// or an error. The first token must be a '[' when called.
3016 parseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3018 assert(Parser.getTok().is(AsmToken::LBrac) &&
3019 "Token is not a Left Bracket");
3020 S = Parser.getTok().getLoc();
3021 Parser.Lex(); // Eat left bracket token.
3023 const AsmToken &BaseRegTok = Parser.getTok();
3024 int BaseRegNum = tryParseRegister();
3025 if (BaseRegNum == -1)
3026 return Error(BaseRegTok.getLoc(), "register expected");
3028 // The next token must either be a comma or a closing bracket.
3029 const AsmToken &Tok = Parser.getTok();
3030 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
3031 return Error(Tok.getLoc(), "malformed memory operand");
3033 if (Tok.is(AsmToken::RBrac)) {
3035 Parser.Lex(); // Eat right bracket token.
3037 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, 0, ARM_AM::no_shift,
3038 0, 0, false, S, E));
3040 // If there's a pre-indexing writeback marker, '!', just add it as a token
3041 // operand. It's rather odd, but syntactically valid.
3042 if (Parser.getTok().is(AsmToken::Exclaim)) {
3043 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3044 Parser.Lex(); // Eat the '!'.
3050 assert(Tok.is(AsmToken::Comma) && "Lost comma in memory operand?!");
3051 Parser.Lex(); // Eat the comma.
3053 // If we have a ':', it's an alignment specifier.
3054 if (Parser.getTok().is(AsmToken::Colon)) {
3055 Parser.Lex(); // Eat the ':'.
3056 E = Parser.getTok().getLoc();
3059 if (getParser().ParseExpression(Expr))
3062 // The expression has to be a constant. Memory references with relocations
3063 // don't come through here, as they use the <label> forms of the relevant
3065 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
3067 return Error (E, "constant expression expected");
3070 switch (CE->getValue()) {
3072 return Error(E, "alignment specifier must be 64, 128, or 256 bits");
3073 case 64: Align = 8; break;
3074 case 128: Align = 16; break;
3075 case 256: Align = 32; break;
3078 // Now we should have the closing ']'
3079 E = Parser.getTok().getLoc();
3080 if (Parser.getTok().isNot(AsmToken::RBrac))
3081 return Error(E, "']' expected");
3082 Parser.Lex(); // Eat right bracket token.
3084 // Don't worry about range checking the value here. That's handled by
3085 // the is*() predicates.
3086 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, 0,
3087 ARM_AM::no_shift, 0, Align,
3090 // If there's a pre-indexing writeback marker, '!', just add it as a token
3092 if (Parser.getTok().is(AsmToken::Exclaim)) {
3093 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3094 Parser.Lex(); // Eat the '!'.
3100 // If we have a '#', it's an immediate offset, else assume it's a register
3102 if (Parser.getTok().is(AsmToken::Hash)) {
3103 Parser.Lex(); // Eat the '#'.
3104 E = Parser.getTok().getLoc();
3106 bool isNegative = getParser().getTok().is(AsmToken::Minus);
3107 const MCExpr *Offset;
3108 if (getParser().ParseExpression(Offset))
3111 // The expression has to be a constant. Memory references with relocations
3112 // don't come through here, as they use the <label> forms of the relevant
3114 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
3116 return Error (E, "constant expression expected");
3118 // If the constant was #-0, represent it as INT32_MIN.
3119 int32_t Val = CE->getValue();
3120 if (isNegative && Val == 0)
3121 CE = MCConstantExpr::Create(INT32_MIN, getContext());
3123 // Now we should have the closing ']'
3124 E = Parser.getTok().getLoc();
3125 if (Parser.getTok().isNot(AsmToken::RBrac))
3126 return Error(E, "']' expected");
3127 Parser.Lex(); // Eat right bracket token.
3129 // Don't worry about range checking the value here. That's handled by
3130 // the is*() predicates.
3131 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, CE, 0,
3132 ARM_AM::no_shift, 0, 0,
3135 // If there's a pre-indexing writeback marker, '!', just add it as a token
3137 if (Parser.getTok().is(AsmToken::Exclaim)) {
3138 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3139 Parser.Lex(); // Eat the '!'.
3145 // The register offset is optionally preceded by a '+' or '-'
3146 bool isNegative = false;
3147 if (Parser.getTok().is(AsmToken::Minus)) {
3149 Parser.Lex(); // Eat the '-'.
3150 } else if (Parser.getTok().is(AsmToken::Plus)) {
3152 Parser.Lex(); // Eat the '+'.
3155 E = Parser.getTok().getLoc();
3156 int OffsetRegNum = tryParseRegister();
3157 if (OffsetRegNum == -1)
3158 return Error(E, "register expected");
3160 // If there's a shift operator, handle it.
3161 ARM_AM::ShiftOpc ShiftType = ARM_AM::no_shift;
3162 unsigned ShiftImm = 0;
3163 if (Parser.getTok().is(AsmToken::Comma)) {
3164 Parser.Lex(); // Eat the ','.
3165 if (parseMemRegOffsetShift(ShiftType, ShiftImm))
3169 // Now we should have the closing ']'
3170 E = Parser.getTok().getLoc();
3171 if (Parser.getTok().isNot(AsmToken::RBrac))
3172 return Error(E, "']' expected");
3173 Parser.Lex(); // Eat right bracket token.
3175 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, OffsetRegNum,
3176 ShiftType, ShiftImm, 0, isNegative,
3179 // If there's a pre-indexing writeback marker, '!', just add it as a token
3181 if (Parser.getTok().is(AsmToken::Exclaim)) {
3182 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3183 Parser.Lex(); // Eat the '!'.
3189 /// parseMemRegOffsetShift - one of these two:
3190 /// ( lsl | lsr | asr | ror ) , # shift_amount
3192 /// return true if it parses a shift otherwise it returns false.
3193 bool ARMAsmParser::parseMemRegOffsetShift(ARM_AM::ShiftOpc &St,
3195 SMLoc Loc = Parser.getTok().getLoc();
3196 const AsmToken &Tok = Parser.getTok();
3197 if (Tok.isNot(AsmToken::Identifier))
3199 StringRef ShiftName = Tok.getString();
3200 if (ShiftName == "lsl" || ShiftName == "LSL")
3202 else if (ShiftName == "lsr" || ShiftName == "LSR")
3204 else if (ShiftName == "asr" || ShiftName == "ASR")
3206 else if (ShiftName == "ror" || ShiftName == "ROR")
3208 else if (ShiftName == "rrx" || ShiftName == "RRX")
3211 return Error(Loc, "illegal shift operator");
3212 Parser.Lex(); // Eat shift type token.
3214 // rrx stands alone.
3216 if (St != ARM_AM::rrx) {
3217 Loc = Parser.getTok().getLoc();
3218 // A '#' and a shift amount.
3219 const AsmToken &HashTok = Parser.getTok();
3220 if (HashTok.isNot(AsmToken::Hash))
3221 return Error(HashTok.getLoc(), "'#' expected");
3222 Parser.Lex(); // Eat hash token.
3225 if (getParser().ParseExpression(Expr))
3227 // Range check the immediate.
3228 // lsl, ror: 0 <= imm <= 31
3229 // lsr, asr: 0 <= imm <= 32
3230 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
3232 return Error(Loc, "shift amount must be an immediate");
3233 int64_t Imm = CE->getValue();
3235 ((St == ARM_AM::lsl || St == ARM_AM::ror) && Imm > 31) ||
3236 ((St == ARM_AM::lsr || St == ARM_AM::asr) && Imm > 32))
3237 return Error(Loc, "immediate shift value out of range");
3244 /// parseFPImm - A floating point immediate expression operand.
3245 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3246 parseFPImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3247 SMLoc S = Parser.getTok().getLoc();
3249 if (Parser.getTok().isNot(AsmToken::Hash))
3250 return MatchOperand_NoMatch;
3251 Parser.Lex(); // Eat the '#'.
3253 // Handle negation, as that still comes through as a separate token.
3254 bool isNegative = false;
3255 if (Parser.getTok().is(AsmToken::Minus)) {
3259 const AsmToken &Tok = Parser.getTok();
3260 if (Tok.is(AsmToken::Real)) {
3261 APFloat RealVal(APFloat::IEEEdouble, Tok.getString());
3262 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
3263 // If we had a '-' in front, toggle the sign bit.
3264 IntVal ^= (uint64_t)isNegative << 63;
3265 int Val = ARM_AM::getFP64Imm(APInt(64, IntVal));
3266 Parser.Lex(); // Eat the token.
3268 TokError("floating point value out of range");
3269 return MatchOperand_ParseFail;
3271 Operands.push_back(ARMOperand::CreateFPImm(Val, S, getContext()));
3272 return MatchOperand_Success;
3274 if (Tok.is(AsmToken::Integer)) {
3275 int64_t Val = Tok.getIntVal();
3276 Parser.Lex(); // Eat the token.
3277 if (Val > 255 || Val < 0) {
3278 TokError("encoded floating point value out of range");
3279 return MatchOperand_ParseFail;
3281 Operands.push_back(ARMOperand::CreateFPImm(Val, S, getContext()));
3282 return MatchOperand_Success;
3285 TokError("invalid floating point immediate");
3286 return MatchOperand_ParseFail;
3288 /// Parse a arm instruction operand. For now this parses the operand regardless
3289 /// of the mnemonic.
3290 bool ARMAsmParser::parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
3291 StringRef Mnemonic) {
3294 // Check if the current operand has a custom associated parser, if so, try to
3295 // custom parse the operand, or fallback to the general approach.
3296 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
3297 if (ResTy == MatchOperand_Success)
3299 // If there wasn't a custom match, try the generic matcher below. Otherwise,
3300 // there was a match, but an error occurred, in which case, just return that
3301 // the operand parsing failed.
3302 if (ResTy == MatchOperand_ParseFail)
3305 switch (getLexer().getKind()) {
3307 Error(Parser.getTok().getLoc(), "unexpected token in operand");
3309 case AsmToken::Identifier: {
3310 // If this is VMRS, check for the apsr_nzcv operand.
3311 if (!tryParseRegisterWithWriteBack(Operands))
3313 int Res = tryParseShiftRegister(Operands);
3314 if (Res == 0) // success
3316 else if (Res == -1) // irrecoverable error
3318 if (Mnemonic == "vmrs" && Parser.getTok().getString() == "apsr_nzcv") {
3319 S = Parser.getTok().getLoc();
3321 Operands.push_back(ARMOperand::CreateToken("apsr_nzcv", S));
3325 // Fall though for the Identifier case that is not a register or a
3328 case AsmToken::Integer: // things like 1f and 2b as a branch targets
3329 case AsmToken::Dot: { // . as a branch target
3330 // This was not a register so parse other operands that start with an
3331 // identifier (like labels) as expressions and create them as immediates.
3332 const MCExpr *IdVal;
3333 S = Parser.getTok().getLoc();
3334 if (getParser().ParseExpression(IdVal))
3336 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3337 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
3340 case AsmToken::LBrac:
3341 return parseMemory(Operands);
3342 case AsmToken::LCurly:
3343 return parseRegisterList(Operands);
3344 case AsmToken::Hash: {
3345 // #42 -> immediate.
3346 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
3347 S = Parser.getTok().getLoc();
3349 bool isNegative = Parser.getTok().is(AsmToken::Minus);
3350 const MCExpr *ImmVal;
3351 if (getParser().ParseExpression(ImmVal))
3353 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ImmVal);
3355 Error(S, "constant expression expected");
3356 return MatchOperand_ParseFail;
3358 int32_t Val = CE->getValue();
3359 if (isNegative && Val == 0)
3360 ImmVal = MCConstantExpr::Create(INT32_MIN, getContext());
3361 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3362 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
3365 case AsmToken::Colon: {
3366 // ":lower16:" and ":upper16:" expression prefixes
3367 // FIXME: Check it's an expression prefix,
3368 // e.g. (FOO - :lower16:BAR) isn't legal.
3369 ARMMCExpr::VariantKind RefKind;
3370 if (parsePrefix(RefKind))
3373 const MCExpr *SubExprVal;
3374 if (getParser().ParseExpression(SubExprVal))
3377 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
3379 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3380 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
3386 // parsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
3387 // :lower16: and :upper16:.
3388 bool ARMAsmParser::parsePrefix(ARMMCExpr::VariantKind &RefKind) {
3389 RefKind = ARMMCExpr::VK_ARM_None;
3391 // :lower16: and :upper16: modifiers
3392 assert(getLexer().is(AsmToken::Colon) && "expected a :");
3393 Parser.Lex(); // Eat ':'
3395 if (getLexer().isNot(AsmToken::Identifier)) {
3396 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
3400 StringRef IDVal = Parser.getTok().getIdentifier();
3401 if (IDVal == "lower16") {
3402 RefKind = ARMMCExpr::VK_ARM_LO16;
3403 } else if (IDVal == "upper16") {
3404 RefKind = ARMMCExpr::VK_ARM_HI16;
3406 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
3411 if (getLexer().isNot(AsmToken::Colon)) {
3412 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
3415 Parser.Lex(); // Eat the last ':'
3419 /// \brief Given a mnemonic, split out possible predication code and carry
3420 /// setting letters to form a canonical mnemonic and flags.
3422 // FIXME: Would be nice to autogen this.
3423 // FIXME: This is a bit of a maze of special cases.
3424 StringRef ARMAsmParser::splitMnemonic(StringRef Mnemonic,
3425 unsigned &PredicationCode,
3427 unsigned &ProcessorIMod,
3428 StringRef &ITMask) {
3429 PredicationCode = ARMCC::AL;
3430 CarrySetting = false;
3433 // Ignore some mnemonics we know aren't predicated forms.
3435 // FIXME: Would be nice to autogen this.
3436 if ((Mnemonic == "movs" && isThumb()) ||
3437 Mnemonic == "teq" || Mnemonic == "vceq" || Mnemonic == "svc" ||
3438 Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
3439 Mnemonic == "vmls" || Mnemonic == "vnmls" || Mnemonic == "vacge" ||
3440 Mnemonic == "vcge" || Mnemonic == "vclt" || Mnemonic == "vacgt" ||
3441 Mnemonic == "vcgt" || Mnemonic == "vcle" || Mnemonic == "smlal" ||
3442 Mnemonic == "umaal" || Mnemonic == "umlal" || Mnemonic == "vabal" ||
3443 Mnemonic == "vmlal" || Mnemonic == "vpadal" || Mnemonic == "vqdmlal")
3446 // First, split out any predication code. Ignore mnemonics we know aren't
3447 // predicated but do have a carry-set and so weren't caught above.
3448 if (Mnemonic != "adcs" && Mnemonic != "bics" && Mnemonic != "movs" &&
3449 Mnemonic != "muls" && Mnemonic != "smlals" && Mnemonic != "smulls" &&
3450 Mnemonic != "umlals" && Mnemonic != "umulls" && Mnemonic != "lsls" &&
3451 Mnemonic != "sbcs" && Mnemonic != "rscs") {
3452 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
3453 .Case("eq", ARMCC::EQ)
3454 .Case("ne", ARMCC::NE)
3455 .Case("hs", ARMCC::HS)
3456 .Case("cs", ARMCC::HS)
3457 .Case("lo", ARMCC::LO)
3458 .Case("cc", ARMCC::LO)
3459 .Case("mi", ARMCC::MI)
3460 .Case("pl", ARMCC::PL)
3461 .Case("vs", ARMCC::VS)
3462 .Case("vc", ARMCC::VC)
3463 .Case("hi", ARMCC::HI)
3464 .Case("ls", ARMCC::LS)
3465 .Case("ge", ARMCC::GE)
3466 .Case("lt", ARMCC::LT)
3467 .Case("gt", ARMCC::GT)
3468 .Case("le", ARMCC::LE)
3469 .Case("al", ARMCC::AL)
3472 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
3473 PredicationCode = CC;
3477 // Next, determine if we have a carry setting bit. We explicitly ignore all
3478 // the instructions we know end in 's'.
3479 if (Mnemonic.endswith("s") &&
3480 !(Mnemonic == "cps" || Mnemonic == "mls" ||
3481 Mnemonic == "mrs" || Mnemonic == "smmls" || Mnemonic == "vabs" ||
3482 Mnemonic == "vcls" || Mnemonic == "vmls" || Mnemonic == "vmrs" ||
3483 Mnemonic == "vnmls" || Mnemonic == "vqabs" || Mnemonic == "vrecps" ||
3484 Mnemonic == "vrsqrts" || Mnemonic == "srs" ||
3485 (Mnemonic == "movs" && isThumb()))) {
3486 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
3487 CarrySetting = true;
3490 // The "cps" instruction can have a interrupt mode operand which is glued into
3491 // the mnemonic. Check if this is the case, split it and parse the imod op
3492 if (Mnemonic.startswith("cps")) {
3493 // Split out any imod code.
3495 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
3496 .Case("ie", ARM_PROC::IE)
3497 .Case("id", ARM_PROC::ID)
3500 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
3501 ProcessorIMod = IMod;
3505 // The "it" instruction has the condition mask on the end of the mnemonic.
3506 if (Mnemonic.startswith("it")) {
3507 ITMask = Mnemonic.slice(2, Mnemonic.size());
3508 Mnemonic = Mnemonic.slice(0, 2);
3514 /// \brief Given a canonical mnemonic, determine if the instruction ever allows
3515 /// inclusion of carry set or predication code operands.
3517 // FIXME: It would be nice to autogen this.
3519 getMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
3520 bool &CanAcceptPredicationCode) {
3521 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
3522 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
3523 Mnemonic == "add" || Mnemonic == "adc" ||
3524 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
3525 Mnemonic == "orr" || Mnemonic == "mvn" ||
3526 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
3527 Mnemonic == "sbc" || Mnemonic == "eor" || Mnemonic == "neg" ||
3528 (!isThumb() && (Mnemonic == "smull" || Mnemonic == "mov" ||
3529 Mnemonic == "mla" || Mnemonic == "smlal" ||
3530 Mnemonic == "umlal" || Mnemonic == "umull"))) {
3531 CanAcceptCarrySet = true;
3533 CanAcceptCarrySet = false;
3535 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
3536 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
3537 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
3538 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
3539 Mnemonic == "dsb" || Mnemonic == "isb" || Mnemonic == "setend" ||
3540 (Mnemonic == "clrex" && !isThumb()) ||
3541 (Mnemonic == "nop" && isThumbOne()) ||
3542 ((Mnemonic == "pld" || Mnemonic == "pli" || Mnemonic == "pldw") &&
3544 ((Mnemonic.startswith("rfe") || Mnemonic.startswith("srs")) &&
3546 Mnemonic.startswith("cps") || (Mnemonic == "movs" && isThumbOne())) {
3547 CanAcceptPredicationCode = false;
3549 CanAcceptPredicationCode = true;
3552 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
3553 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
3554 CanAcceptPredicationCode = false;
3558 bool ARMAsmParser::shouldOmitCCOutOperand(StringRef Mnemonic,
3559 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3560 // FIXME: This is all horribly hacky. We really need a better way to deal
3561 // with optional operands like this in the matcher table.
3563 // The 'mov' mnemonic is special. One variant has a cc_out operand, while
3564 // another does not. Specifically, the MOVW instruction does not. So we
3565 // special case it here and remove the defaulted (non-setting) cc_out
3566 // operand if that's the instruction we're trying to match.
3568 // We do this as post-processing of the explicit operands rather than just
3569 // conditionally adding the cc_out in the first place because we need
3570 // to check the type of the parsed immediate operand.
3571 if (Mnemonic == "mov" && Operands.size() > 4 && !isThumb() &&
3572 !static_cast<ARMOperand*>(Operands[4])->isARMSOImm() &&
3573 static_cast<ARMOperand*>(Operands[4])->isImm0_65535Expr() &&
3574 static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
3577 // Register-register 'add' for thumb does not have a cc_out operand
3578 // when there are only two register operands.
3579 if (isThumb() && Mnemonic == "add" && Operands.size() == 5 &&
3580 static_cast<ARMOperand*>(Operands[3])->isReg() &&
3581 static_cast<ARMOperand*>(Operands[4])->isReg() &&
3582 static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
3584 // Register-register 'add' for thumb does not have a cc_out operand
3585 // when it's an ADD Rdm, SP, {Rdm|#imm0_255} instruction. We do
3586 // have to check the immediate range here since Thumb2 has a variant
3587 // that can handle a different range and has a cc_out operand.
3588 if (((isThumb() && Mnemonic == "add") ||
3589 (isThumbTwo() && Mnemonic == "sub")) &&
3590 Operands.size() == 6 &&
3591 static_cast<ARMOperand*>(Operands[3])->isReg() &&
3592 static_cast<ARMOperand*>(Operands[4])->isReg() &&
3593 static_cast<ARMOperand*>(Operands[4])->getReg() == ARM::SP &&
3594 static_cast<ARMOperand*>(Operands[1])->getReg() == 0 &&
3595 (static_cast<ARMOperand*>(Operands[5])->isReg() ||
3596 static_cast<ARMOperand*>(Operands[5])->isImm0_1020s4()))
3598 // For Thumb2, add/sub immediate does not have a cc_out operand for the
3599 // imm0_4095 variant. That's the least-preferred variant when
3600 // selecting via the generic "add" mnemonic, so to know that we
3601 // should remove the cc_out operand, we have to explicitly check that
3602 // it's not one of the other variants. Ugh.
3603 if (isThumbTwo() && (Mnemonic == "add" || Mnemonic == "sub") &&
3604 Operands.size() == 6 &&
3605 static_cast<ARMOperand*>(Operands[3])->isReg() &&
3606 static_cast<ARMOperand*>(Operands[4])->isReg() &&
3607 static_cast<ARMOperand*>(Operands[5])->isImm()) {
3608 // Nest conditions rather than one big 'if' statement for readability.
3610 // If either register is a high reg, it's either one of the SP
3611 // variants (handled above) or a 32-bit encoding, so we just
3612 // check against T3.
3613 if ((!isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) ||
3614 !isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg())) &&
3615 static_cast<ARMOperand*>(Operands[5])->isT2SOImm())
3617 // If both registers are low, we're in an IT block, and the immediate is
3618 // in range, we should use encoding T1 instead, which has a cc_out.
3620 isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) &&
3621 isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg()) &&
3622 static_cast<ARMOperand*>(Operands[5])->isImm0_7())
3625 // Otherwise, we use encoding T4, which does not have a cc_out
3630 // The thumb2 multiply instruction doesn't have a CCOut register, so
3631 // if we have a "mul" mnemonic in Thumb mode, check if we'll be able to
3632 // use the 16-bit encoding or not.
3633 if (isThumbTwo() && Mnemonic == "mul" && Operands.size() == 6 &&
3634 static_cast<ARMOperand*>(Operands[1])->getReg() == 0 &&
3635 static_cast<ARMOperand*>(Operands[3])->isReg() &&
3636 static_cast<ARMOperand*>(Operands[4])->isReg() &&
3637 static_cast<ARMOperand*>(Operands[5])->isReg() &&
3638 // If the registers aren't low regs, the destination reg isn't the
3639 // same as one of the source regs, or the cc_out operand is zero
3640 // outside of an IT block, we have to use the 32-bit encoding, so
3641 // remove the cc_out operand.
3642 (!isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) ||
3643 !isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg()) ||
3645 (static_cast<ARMOperand*>(Operands[3])->getReg() !=
3646 static_cast<ARMOperand*>(Operands[5])->getReg() &&
3647 static_cast<ARMOperand*>(Operands[3])->getReg() !=
3648 static_cast<ARMOperand*>(Operands[4])->getReg())))
3653 // Register-register 'add/sub' for thumb does not have a cc_out operand
3654 // when it's an ADD/SUB SP, #imm. Be lenient on count since there's also
3655 // the "add/sub SP, SP, #imm" version. If the follow-up operands aren't
3656 // right, this will result in better diagnostics (which operand is off)
3658 if (isThumb() && (Mnemonic == "add" || Mnemonic == "sub") &&
3659 (Operands.size() == 5 || Operands.size() == 6) &&
3660 static_cast<ARMOperand*>(Operands[3])->isReg() &&
3661 static_cast<ARMOperand*>(Operands[3])->getReg() == ARM::SP &&
3662 static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
3668 /// Parse an arm instruction mnemonic followed by its operands.
3669 bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
3670 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3671 // Create the leading tokens for the mnemonic, split by '.' characters.
3672 size_t Start = 0, Next = Name.find('.');
3673 StringRef Mnemonic = Name.slice(Start, Next);
3675 // Split out the predication code and carry setting flag from the mnemonic.
3676 unsigned PredicationCode;
3677 unsigned ProcessorIMod;
3680 Mnemonic = splitMnemonic(Mnemonic, PredicationCode, CarrySetting,
3681 ProcessorIMod, ITMask);
3683 // In Thumb1, only the branch (B) instruction can be predicated.
3684 if (isThumbOne() && PredicationCode != ARMCC::AL && Mnemonic != "b") {
3685 Parser.EatToEndOfStatement();
3686 return Error(NameLoc, "conditional execution not supported in Thumb1");
3689 Operands.push_back(ARMOperand::CreateToken(Mnemonic, NameLoc));
3691 // Handle the IT instruction ITMask. Convert it to a bitmask. This
3692 // is the mask as it will be for the IT encoding if the conditional
3693 // encoding has a '1' as it's bit0 (i.e. 't' ==> '1'). In the case
3694 // where the conditional bit0 is zero, the instruction post-processing
3695 // will adjust the mask accordingly.
3696 if (Mnemonic == "it") {
3697 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + 2);
3698 if (ITMask.size() > 3) {
3699 Parser.EatToEndOfStatement();
3700 return Error(Loc, "too many conditions on IT instruction");
3703 for (unsigned i = ITMask.size(); i != 0; --i) {
3704 char pos = ITMask[i - 1];
3705 if (pos != 't' && pos != 'e') {
3706 Parser.EatToEndOfStatement();
3707 return Error(Loc, "illegal IT block condition mask '" + ITMask + "'");
3710 if (ITMask[i - 1] == 't')
3713 Operands.push_back(ARMOperand::CreateITMask(Mask, Loc));
3716 // FIXME: This is all a pretty gross hack. We should automatically handle
3717 // optional operands like this via tblgen.
3719 // Next, add the CCOut and ConditionCode operands, if needed.
3721 // For mnemonics which can ever incorporate a carry setting bit or predication
3722 // code, our matching model involves us always generating CCOut and
3723 // ConditionCode operands to match the mnemonic "as written" and then we let
3724 // the matcher deal with finding the right instruction or generating an
3725 // appropriate error.
3726 bool CanAcceptCarrySet, CanAcceptPredicationCode;
3727 getMnemonicAcceptInfo(Mnemonic, CanAcceptCarrySet, CanAcceptPredicationCode);
3729 // If we had a carry-set on an instruction that can't do that, issue an
3731 if (!CanAcceptCarrySet && CarrySetting) {
3732 Parser.EatToEndOfStatement();
3733 return Error(NameLoc, "instruction '" + Mnemonic +
3734 "' can not set flags, but 's' suffix specified");
3736 // If we had a predication code on an instruction that can't do that, issue an
3738 if (!CanAcceptPredicationCode && PredicationCode != ARMCC::AL) {
3739 Parser.EatToEndOfStatement();
3740 return Error(NameLoc, "instruction '" + Mnemonic +
3741 "' is not predicable, but condition code specified");
3744 // Add the carry setting operand, if necessary.
3745 if (CanAcceptCarrySet) {
3746 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size());
3747 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
3751 // Add the predication code operand, if necessary.
3752 if (CanAcceptPredicationCode) {
3753 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size() +
3755 Operands.push_back(ARMOperand::CreateCondCode(
3756 ARMCC::CondCodes(PredicationCode), Loc));
3759 // Add the processor imod operand, if necessary.
3760 if (ProcessorIMod) {
3761 Operands.push_back(ARMOperand::CreateImm(
3762 MCConstantExpr::Create(ProcessorIMod, getContext()),
3766 // Add the remaining tokens in the mnemonic.
3767 while (Next != StringRef::npos) {
3769 Next = Name.find('.', Start + 1);
3770 StringRef ExtraToken = Name.slice(Start, Next);
3772 // For now, we're only parsing Thumb1 (for the most part), so
3773 // just ignore ".n" qualifiers. We'll use them to restrict
3774 // matching when we do Thumb2.
3775 if (ExtraToken != ".n") {
3776 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Start);
3777 Operands.push_back(ARMOperand::CreateToken(ExtraToken, Loc));
3781 // Read the remaining operands.
3782 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3783 // Read the first operand.
3784 if (parseOperand(Operands, Mnemonic)) {
3785 Parser.EatToEndOfStatement();
3789 while (getLexer().is(AsmToken::Comma)) {
3790 Parser.Lex(); // Eat the comma.
3792 // Parse and remember the operand.
3793 if (parseOperand(Operands, Mnemonic)) {
3794 Parser.EatToEndOfStatement();
3800 if (getLexer().isNot(AsmToken::EndOfStatement)) {
3801 SMLoc Loc = getLexer().getLoc();
3802 Parser.EatToEndOfStatement();
3803 return Error(Loc, "unexpected token in argument list");
3806 Parser.Lex(); // Consume the EndOfStatement
3808 // Some instructions, mostly Thumb, have forms for the same mnemonic that
3809 // do and don't have a cc_out optional-def operand. With some spot-checks
3810 // of the operand list, we can figure out which variant we're trying to
3811 // parse and adjust accordingly before actually matching. We shouldn't ever
3812 // try to remove a cc_out operand that was explicitly set on the the
3813 // mnemonic, of course (CarrySetting == true). Reason number #317 the
3814 // table driven matcher doesn't fit well with the ARM instruction set.
3815 if (!CarrySetting && shouldOmitCCOutOperand(Mnemonic, Operands)) {
3816 ARMOperand *Op = static_cast<ARMOperand*>(Operands[1]);
3817 Operands.erase(Operands.begin() + 1);
3821 // ARM mode 'blx' need special handling, as the register operand version
3822 // is predicable, but the label operand version is not. So, we can't rely
3823 // on the Mnemonic based checking to correctly figure out when to put
3824 // a k_CondCode operand in the list. If we're trying to match the label
3825 // version, remove the k_CondCode operand here.
3826 if (!isThumb() && Mnemonic == "blx" && Operands.size() == 3 &&
3827 static_cast<ARMOperand*>(Operands[2])->isImm()) {
3828 ARMOperand *Op = static_cast<ARMOperand*>(Operands[1]);
3829 Operands.erase(Operands.begin() + 1);
3833 // The vector-compare-to-zero instructions have a literal token "#0" at
3834 // the end that comes to here as an immediate operand. Convert it to a
3835 // token to play nicely with the matcher.
3836 if ((Mnemonic == "vceq" || Mnemonic == "vcge" || Mnemonic == "vcgt" ||
3837 Mnemonic == "vcle" || Mnemonic == "vclt") && Operands.size() == 6 &&
3838 static_cast<ARMOperand*>(Operands[5])->isImm()) {
3839 ARMOperand *Op = static_cast<ARMOperand*>(Operands[5]);
3840 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
3841 if (CE && CE->getValue() == 0) {
3842 Operands.erase(Operands.begin() + 5);
3843 Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
3847 // VCMP{E} does the same thing, but with a different operand count.
3848 if ((Mnemonic == "vcmp" || Mnemonic == "vcmpe") && Operands.size() == 5 &&
3849 static_cast<ARMOperand*>(Operands[4])->isImm()) {
3850 ARMOperand *Op = static_cast<ARMOperand*>(Operands[4]);
3851 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
3852 if (CE && CE->getValue() == 0) {
3853 Operands.erase(Operands.begin() + 4);
3854 Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
3858 // Similarly, the Thumb1 "RSB" instruction has a literal "#0" on the
3859 // end. Convert it to a token here.
3860 if (Mnemonic == "rsb" && isThumb() && Operands.size() == 6 &&
3861 static_cast<ARMOperand*>(Operands[5])->isImm()) {
3862 ARMOperand *Op = static_cast<ARMOperand*>(Operands[5]);
3863 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
3864 if (CE && CE->getValue() == 0) {
3865 Operands.erase(Operands.begin() + 5);
3866 Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
3874 // Validate context-sensitive operand constraints.
3876 // return 'true' if register list contains non-low GPR registers,
3877 // 'false' otherwise. If Reg is in the register list or is HiReg, set
3878 // 'containsReg' to true.
3879 static bool checkLowRegisterList(MCInst Inst, unsigned OpNo, unsigned Reg,
3880 unsigned HiReg, bool &containsReg) {
3881 containsReg = false;
3882 for (unsigned i = OpNo; i < Inst.getNumOperands(); ++i) {
3883 unsigned OpReg = Inst.getOperand(i).getReg();
3886 // Anything other than a low register isn't legal here.
3887 if (!isARMLowRegister(OpReg) && (!HiReg || OpReg != HiReg))
3893 // Check if the specified regisgter is in the register list of the inst,
3894 // starting at the indicated operand number.
3895 static bool listContainsReg(MCInst &Inst, unsigned OpNo, unsigned Reg) {
3896 for (unsigned i = OpNo; i < Inst.getNumOperands(); ++i) {
3897 unsigned OpReg = Inst.getOperand(i).getReg();
3904 // FIXME: We would really prefer to have MCInstrInfo (the wrapper around
3905 // the ARMInsts array) instead. Getting that here requires awkward
3906 // API changes, though. Better way?
3908 extern MCInstrDesc ARMInsts[];
3910 static MCInstrDesc &getInstDesc(unsigned Opcode) {
3911 return ARMInsts[Opcode];
3914 // FIXME: We would really like to be able to tablegen'erate this.
3916 validateInstruction(MCInst &Inst,
3917 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3918 MCInstrDesc &MCID = getInstDesc(Inst.getOpcode());
3919 SMLoc Loc = Operands[0]->getStartLoc();
3920 // Check the IT block state first.
3921 // NOTE: In Thumb mode, the BKPT instruction has the interesting property of
3922 // being allowed in IT blocks, but not being predicable. It just always
3924 if (inITBlock() && Inst.getOpcode() != ARM::tBKPT) {
3926 if (ITState.FirstCond)
3927 ITState.FirstCond = false;
3929 bit = (ITState.Mask >> (5 - ITState.CurPosition)) & 1;
3930 // The instruction must be predicable.
3931 if (!MCID.isPredicable())
3932 return Error(Loc, "instructions in IT block must be predicable");
3933 unsigned Cond = Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm();
3934 unsigned ITCond = bit ? ITState.Cond :
3935 ARMCC::getOppositeCondition(ITState.Cond);
3936 if (Cond != ITCond) {
3937 // Find the condition code Operand to get its SMLoc information.
3939 for (unsigned i = 1; i < Operands.size(); ++i)
3940 if (static_cast<ARMOperand*>(Operands[i])->isCondCode())
3941 CondLoc = Operands[i]->getStartLoc();
3942 return Error(CondLoc, "incorrect condition in IT block; got '" +
3943 StringRef(ARMCondCodeToString(ARMCC::CondCodes(Cond))) +
3944 "', but expected '" +
3945 ARMCondCodeToString(ARMCC::CondCodes(ITCond)) + "'");
3947 // Check for non-'al' condition codes outside of the IT block.
3948 } else if (isThumbTwo() && MCID.isPredicable() &&
3949 Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm() !=
3950 ARMCC::AL && Inst.getOpcode() != ARM::tB &&
3951 Inst.getOpcode() != ARM::t2B)
3952 return Error(Loc, "predicated instructions must be in IT block");
3954 switch (Inst.getOpcode()) {
3957 case ARM::LDRD_POST:
3959 // Rt2 must be Rt + 1.
3960 unsigned Rt = getARMRegisterNumbering(Inst.getOperand(0).getReg());
3961 unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(1).getReg());
3963 return Error(Operands[3]->getStartLoc(),
3964 "destination operands must be sequential");
3968 // Rt2 must be Rt + 1.
3969 unsigned Rt = getARMRegisterNumbering(Inst.getOperand(0).getReg());
3970 unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(1).getReg());
3972 return Error(Operands[3]->getStartLoc(),
3973 "source operands must be sequential");
3977 case ARM::STRD_POST:
3979 // Rt2 must be Rt + 1.
3980 unsigned Rt = getARMRegisterNumbering(Inst.getOperand(1).getReg());
3981 unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(2).getReg());
3983 return Error(Operands[3]->getStartLoc(),
3984 "source operands must be sequential");
3989 // width must be in range [1, 32-lsb]
3990 unsigned lsb = Inst.getOperand(2).getImm();
3991 unsigned widthm1 = Inst.getOperand(3).getImm();
3992 if (widthm1 >= 32 - lsb)
3993 return Error(Operands[5]->getStartLoc(),
3994 "bitfield width must be in range [1,32-lsb]");
3998 // If we're parsing Thumb2, the .w variant is available and handles
3999 // most cases that are normally illegal for a Thumb1 LDM
4000 // instruction. We'll make the transformation in processInstruction()
4003 // Thumb LDM instructions are writeback iff the base register is not
4004 // in the register list.
4005 unsigned Rn = Inst.getOperand(0).getReg();
4006 bool hasWritebackToken =
4007 (static_cast<ARMOperand*>(Operands[3])->isToken() &&
4008 static_cast<ARMOperand*>(Operands[3])->getToken() == "!");
4009 bool listContainsBase;
4010 if (checkLowRegisterList(Inst, 3, Rn, 0, listContainsBase) && !isThumbTwo())
4011 return Error(Operands[3 + hasWritebackToken]->getStartLoc(),
4012 "registers must be in range r0-r7");
4013 // If we should have writeback, then there should be a '!' token.
4014 if (!listContainsBase && !hasWritebackToken && !isThumbTwo())
4015 return Error(Operands[2]->getStartLoc(),
4016 "writeback operator '!' expected");
4017 // If we should not have writeback, there must not be a '!'. This is
4018 // true even for the 32-bit wide encodings.
4019 if (listContainsBase && hasWritebackToken)
4020 return Error(Operands[3]->getStartLoc(),
4021 "writeback operator '!' not allowed when base register "
4022 "in register list");
4026 case ARM::t2LDMIA_UPD: {
4027 if (listContainsReg(Inst, 3, Inst.getOperand(0).getReg()))
4028 return Error(Operands[4]->getStartLoc(),
4029 "writeback operator '!' not allowed when base register "
4030 "in register list");
4034 bool listContainsBase;
4035 if (checkLowRegisterList(Inst, 3, 0, ARM::PC, listContainsBase))
4036 return Error(Operands[2]->getStartLoc(),
4037 "registers must be in range r0-r7 or pc");
4041 bool listContainsBase;
4042 if (checkLowRegisterList(Inst, 3, 0, ARM::LR, listContainsBase))
4043 return Error(Operands[2]->getStartLoc(),
4044 "registers must be in range r0-r7 or lr");
4047 case ARM::tSTMIA_UPD: {
4048 bool listContainsBase;
4049 if (checkLowRegisterList(Inst, 4, 0, 0, listContainsBase) && !isThumbTwo())
4050 return Error(Operands[4]->getStartLoc(),
4051 "registers must be in range r0-r7");
4060 processInstruction(MCInst &Inst,
4061 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
4062 switch (Inst.getOpcode()) {
4063 case ARM::LDMIA_UPD:
4064 // If this is a load of a single register via a 'pop', then we should use
4065 // a post-indexed LDR instruction instead, per the ARM ARM.
4066 if (static_cast<ARMOperand*>(Operands[0])->getToken() == "pop" &&
4067 Inst.getNumOperands() == 5) {
4069 TmpInst.setOpcode(ARM::LDR_POST_IMM);
4070 TmpInst.addOperand(Inst.getOperand(4)); // Rt
4071 TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
4072 TmpInst.addOperand(Inst.getOperand(1)); // Rn
4073 TmpInst.addOperand(MCOperand::CreateReg(0)); // am2offset
4074 TmpInst.addOperand(MCOperand::CreateImm(4));
4075 TmpInst.addOperand(Inst.getOperand(2)); // CondCode
4076 TmpInst.addOperand(Inst.getOperand(3));
4080 case ARM::STMDB_UPD:
4081 // If this is a store of a single register via a 'push', then we should use
4082 // a pre-indexed STR instruction instead, per the ARM ARM.
4083 if (static_cast<ARMOperand*>(Operands[0])->getToken() == "push" &&
4084 Inst.getNumOperands() == 5) {
4086 TmpInst.setOpcode(ARM::STR_PRE_IMM);
4087 TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
4088 TmpInst.addOperand(Inst.getOperand(4)); // Rt
4089 TmpInst.addOperand(Inst.getOperand(1)); // addrmode_imm12
4090 TmpInst.addOperand(MCOperand::CreateImm(-4));
4091 TmpInst.addOperand(Inst.getOperand(2)); // CondCode
4092 TmpInst.addOperand(Inst.getOperand(3));
4097 // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
4098 // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
4099 // to encoding T2 if <Rd> is specified and encoding T2 is preferred
4100 // to encoding T1 if <Rd> is omitted."
4101 if (Inst.getOperand(3).getImm() < 8 && Operands.size() == 6)
4102 Inst.setOpcode(ARM::tADDi3);
4105 // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
4106 // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
4107 // to encoding T2 if <Rd> is specified and encoding T2 is preferred
4108 // to encoding T1 if <Rd> is omitted."
4109 if (Inst.getOperand(3).getImm() < 8 && Operands.size() == 6)
4110 Inst.setOpcode(ARM::tSUBi3);
4113 // A Thumb conditional branch outside of an IT block is a tBcc.
4114 if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock())
4115 Inst.setOpcode(ARM::tBcc);
4118 // A Thumb2 conditional branch outside of an IT block is a t2Bcc.
4119 if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock())
4120 Inst.setOpcode(ARM::t2Bcc);
4123 // If the conditional is AL or we're in an IT block, we really want t2B.
4124 if (Inst.getOperand(1).getImm() == ARMCC::AL || inITBlock())
4125 Inst.setOpcode(ARM::t2B);
4128 // If the conditional is AL, we really want tB.
4129 if (Inst.getOperand(1).getImm() == ARMCC::AL)
4130 Inst.setOpcode(ARM::tB);
4133 // If the register list contains any high registers, or if the writeback
4134 // doesn't match what tLDMIA can do, we need to use the 32-bit encoding
4135 // instead if we're in Thumb2. Otherwise, this should have generated
4136 // an error in validateInstruction().
4137 unsigned Rn = Inst.getOperand(0).getReg();
4138 bool hasWritebackToken =
4139 (static_cast<ARMOperand*>(Operands[3])->isToken() &&
4140 static_cast<ARMOperand*>(Operands[3])->getToken() == "!");
4141 bool listContainsBase;
4142 if (checkLowRegisterList(Inst, 3, Rn, 0, listContainsBase) ||
4143 (!listContainsBase && !hasWritebackToken) ||
4144 (listContainsBase && hasWritebackToken)) {
4145 // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
4146 assert (isThumbTwo());
4147 Inst.setOpcode(hasWritebackToken ? ARM::t2LDMIA_UPD : ARM::t2LDMIA);
4148 // If we're switching to the updating version, we need to insert
4149 // the writeback tied operand.
4150 if (hasWritebackToken)
4151 Inst.insert(Inst.begin(),
4152 MCOperand::CreateReg(Inst.getOperand(0).getReg()));
4156 case ARM::tSTMIA_UPD: {
4157 // If the register list contains any high registers, we need to use
4158 // the 32-bit encoding instead if we're in Thumb2. Otherwise, this
4159 // should have generated an error in validateInstruction().
4160 unsigned Rn = Inst.getOperand(0).getReg();
4161 bool listContainsBase;
4162 if (checkLowRegisterList(Inst, 4, Rn, 0, listContainsBase)) {
4163 // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
4164 assert (isThumbTwo());
4165 Inst.setOpcode(ARM::t2STMIA_UPD);
4170 // If we can use the 16-bit encoding and the user didn't explicitly
4171 // request the 32-bit variant, transform it here.
4172 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
4173 Inst.getOperand(1).getImm() <= 255 &&
4174 ((!inITBlock() && Inst.getOperand(2).getImm() == ARMCC::AL &&
4175 Inst.getOperand(4).getReg() == ARM::CPSR) ||
4176 (inITBlock() && Inst.getOperand(4).getReg() == 0)) &&
4177 (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
4178 static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
4179 // The operands aren't in the same order for tMOVi8...
4181 TmpInst.setOpcode(ARM::tMOVi8);
4182 TmpInst.addOperand(Inst.getOperand(0));
4183 TmpInst.addOperand(Inst.getOperand(4));
4184 TmpInst.addOperand(Inst.getOperand(1));
4185 TmpInst.addOperand(Inst.getOperand(2));
4186 TmpInst.addOperand(Inst.getOperand(3));
4192 // If we can use the 16-bit encoding and the user didn't explicitly
4193 // request the 32-bit variant, transform it here.
4194 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
4195 isARMLowRegister(Inst.getOperand(1).getReg()) &&
4196 Inst.getOperand(2).getImm() == ARMCC::AL &&
4197 Inst.getOperand(4).getReg() == ARM::CPSR &&
4198 (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
4199 static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
4200 // The operands aren't the same for tMOV[S]r... (no cc_out)
4202 TmpInst.setOpcode(Inst.getOperand(4).getReg() ? ARM::tMOVSr : ARM::tMOVr);
4203 TmpInst.addOperand(Inst.getOperand(0));
4204 TmpInst.addOperand(Inst.getOperand(1));
4205 TmpInst.addOperand(Inst.getOperand(2));
4206 TmpInst.addOperand(Inst.getOperand(3));
4215 // If we can use the 16-bit encoding and the user didn't explicitly
4216 // request the 32-bit variant, transform it here.
4217 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
4218 isARMLowRegister(Inst.getOperand(1).getReg()) &&
4219 Inst.getOperand(2).getImm() == 0 &&
4220 (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
4221 static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
4223 switch (Inst.getOpcode()) {
4224 default: llvm_unreachable("Illegal opcode!");
4225 case ARM::t2SXTH: NewOpc = ARM::tSXTH; break;
4226 case ARM::t2SXTB: NewOpc = ARM::tSXTB; break;
4227 case ARM::t2UXTH: NewOpc = ARM::tUXTH; break;
4228 case ARM::t2UXTB: NewOpc = ARM::tUXTB; break;
4230 // The operands aren't the same for thumb1 (no rotate operand).
4232 TmpInst.setOpcode(NewOpc);
4233 TmpInst.addOperand(Inst.getOperand(0));
4234 TmpInst.addOperand(Inst.getOperand(1));
4235 TmpInst.addOperand(Inst.getOperand(3));
4236 TmpInst.addOperand(Inst.getOperand(4));
4242 // The mask bits for all but the first condition are represented as
4243 // the low bit of the condition code value implies 't'. We currently
4244 // always have 1 implies 't', so XOR toggle the bits if the low bit
4245 // of the condition code is zero. The encoding also expects the low
4246 // bit of the condition to be encoded as bit 4 of the mask operand,
4247 // so mask that in if needed
4248 MCOperand &MO = Inst.getOperand(1);
4249 unsigned Mask = MO.getImm();
4250 unsigned OrigMask = Mask;
4251 unsigned TZ = CountTrailingZeros_32(Mask);
4252 if ((Inst.getOperand(0).getImm() & 1) == 0) {
4253 assert(Mask && TZ <= 3 && "illegal IT mask value!");
4254 for (unsigned i = 3; i != TZ; --i)
4260 // Set up the IT block state according to the IT instruction we just
4262 assert(!inITBlock() && "nested IT blocks?!");
4263 ITState.Cond = ARMCC::CondCodes(Inst.getOperand(0).getImm());
4264 ITState.Mask = OrigMask; // Use the original mask, not the updated one.
4265 ITState.CurPosition = 0;
4266 ITState.FirstCond = true;
4272 unsigned ARMAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
4273 // 16-bit thumb arithmetic instructions either require or preclude the 'S'
4274 // suffix depending on whether they're in an IT block or not.
4275 unsigned Opc = Inst.getOpcode();
4276 MCInstrDesc &MCID = getInstDesc(Opc);
4277 if (MCID.TSFlags & ARMII::ThumbArithFlagSetting) {
4278 assert(MCID.hasOptionalDef() &&
4279 "optionally flag setting instruction missing optional def operand");
4280 assert(MCID.NumOperands == Inst.getNumOperands() &&
4281 "operand count mismatch!");
4282 // Find the optional-def operand (cc_out).
4285 !MCID.OpInfo[OpNo].isOptionalDef() && OpNo < MCID.NumOperands;
4288 // If we're parsing Thumb1, reject it completely.
4289 if (isThumbOne() && Inst.getOperand(OpNo).getReg() != ARM::CPSR)
4290 return Match_MnemonicFail;
4291 // If we're parsing Thumb2, which form is legal depends on whether we're
4293 if (isThumbTwo() && Inst.getOperand(OpNo).getReg() != ARM::CPSR &&
4295 return Match_RequiresITBlock;
4296 if (isThumbTwo() && Inst.getOperand(OpNo).getReg() == ARM::CPSR &&
4298 return Match_RequiresNotITBlock;
4300 // Some high-register supporting Thumb1 encodings only allow both registers
4301 // to be from r0-r7 when in Thumb2.
4302 else if (Opc == ARM::tADDhirr && isThumbOne() &&
4303 isARMLowRegister(Inst.getOperand(1).getReg()) &&
4304 isARMLowRegister(Inst.getOperand(2).getReg()))
4305 return Match_RequiresThumb2;
4306 // Others only require ARMv6 or later.
4307 else if (Opc == ARM::tMOVr && isThumbOne() && !hasV6Ops() &&
4308 isARMLowRegister(Inst.getOperand(0).getReg()) &&
4309 isARMLowRegister(Inst.getOperand(1).getReg()))
4310 return Match_RequiresV6;
4311 return Match_Success;
4315 MatchAndEmitInstruction(SMLoc IDLoc,
4316 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
4320 unsigned MatchResult;
4321 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
4322 switch (MatchResult) {
4325 // Context sensitive operand constraints aren't handled by the matcher,
4326 // so check them here.
4327 if (validateInstruction(Inst, Operands)) {
4328 // Still progress the IT block, otherwise one wrong condition causes
4329 // nasty cascading errors.
4330 forwardITPosition();
4334 // Some instructions need post-processing to, for example, tweak which
4335 // encoding is selected.
4336 processInstruction(Inst, Operands);
4338 // Only move forward at the very end so that everything in validate
4339 // and process gets a consistent answer about whether we're in an IT
4341 forwardITPosition();
4343 Out.EmitInstruction(Inst);
4345 case Match_MissingFeature:
4346 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
4348 case Match_InvalidOperand: {
4349 SMLoc ErrorLoc = IDLoc;
4350 if (ErrorInfo != ~0U) {
4351 if (ErrorInfo >= Operands.size())
4352 return Error(IDLoc, "too few operands for instruction");
4354 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
4355 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
4358 return Error(ErrorLoc, "invalid operand for instruction");
4360 case Match_MnemonicFail:
4361 return Error(IDLoc, "invalid instruction");
4362 case Match_ConversionFail:
4363 // The converter function will have already emited a diagnostic.
4365 case Match_RequiresNotITBlock:
4366 return Error(IDLoc, "flag setting instruction only valid outside IT block");
4367 case Match_RequiresITBlock:
4368 return Error(IDLoc, "instruction only valid inside IT block");
4369 case Match_RequiresV6:
4370 return Error(IDLoc, "instruction variant requires ARMv6 or later");
4371 case Match_RequiresThumb2:
4372 return Error(IDLoc, "instruction variant requires Thumb2");
4375 llvm_unreachable("Implement any new match types added!");
4379 /// parseDirective parses the arm specific directives
4380 bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
4381 StringRef IDVal = DirectiveID.getIdentifier();
4382 if (IDVal == ".word")
4383 return parseDirectiveWord(4, DirectiveID.getLoc());
4384 else if (IDVal == ".thumb")
4385 return parseDirectiveThumb(DirectiveID.getLoc());
4386 else if (IDVal == ".thumb_func")
4387 return parseDirectiveThumbFunc(DirectiveID.getLoc());
4388 else if (IDVal == ".code")
4389 return parseDirectiveCode(DirectiveID.getLoc());
4390 else if (IDVal == ".syntax")
4391 return parseDirectiveSyntax(DirectiveID.getLoc());
4395 /// parseDirectiveWord
4396 /// ::= .word [ expression (, expression)* ]
4397 bool ARMAsmParser::parseDirectiveWord(unsigned Size, SMLoc L) {
4398 if (getLexer().isNot(AsmToken::EndOfStatement)) {
4400 const MCExpr *Value;
4401 if (getParser().ParseExpression(Value))
4404 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
4406 if (getLexer().is(AsmToken::EndOfStatement))
4409 // FIXME: Improve diagnostic.
4410 if (getLexer().isNot(AsmToken::Comma))
4411 return Error(L, "unexpected token in directive");
4420 /// parseDirectiveThumb
4422 bool ARMAsmParser::parseDirectiveThumb(SMLoc L) {
4423 if (getLexer().isNot(AsmToken::EndOfStatement))
4424 return Error(L, "unexpected token in directive");
4427 // TODO: set thumb mode
4428 // TODO: tell the MC streamer the mode
4429 // getParser().getStreamer().Emit???();
4433 /// parseDirectiveThumbFunc
4434 /// ::= .thumbfunc symbol_name
4435 bool ARMAsmParser::parseDirectiveThumbFunc(SMLoc L) {
4436 const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
4437 bool isMachO = MAI.hasSubsectionsViaSymbols();
4440 // Darwin asm has function name after .thumb_func direction
4443 const AsmToken &Tok = Parser.getTok();
4444 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
4445 return Error(L, "unexpected token in .thumb_func directive");
4446 Name = Tok.getString();
4447 Parser.Lex(); // Consume the identifier token.
4450 if (getLexer().isNot(AsmToken::EndOfStatement))
4451 return Error(L, "unexpected token in directive");
4454 // FIXME: assuming function name will be the line following .thumb_func
4456 Name = Parser.getTok().getString();
4459 // Mark symbol as a thumb symbol.
4460 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
4461 getParser().getStreamer().EmitThumbFunc(Func);
4465 /// parseDirectiveSyntax
4466 /// ::= .syntax unified | divided
4467 bool ARMAsmParser::parseDirectiveSyntax(SMLoc L) {
4468 const AsmToken &Tok = Parser.getTok();
4469 if (Tok.isNot(AsmToken::Identifier))
4470 return Error(L, "unexpected token in .syntax directive");
4471 StringRef Mode = Tok.getString();
4472 if (Mode == "unified" || Mode == "UNIFIED")
4474 else if (Mode == "divided" || Mode == "DIVIDED")
4475 return Error(L, "'.syntax divided' arm asssembly not supported");
4477 return Error(L, "unrecognized syntax mode in .syntax directive");
4479 if (getLexer().isNot(AsmToken::EndOfStatement))
4480 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
4483 // TODO tell the MC streamer the mode
4484 // getParser().getStreamer().Emit???();
4488 /// parseDirectiveCode
4489 /// ::= .code 16 | 32
4490 bool ARMAsmParser::parseDirectiveCode(SMLoc L) {
4491 const AsmToken &Tok = Parser.getTok();
4492 if (Tok.isNot(AsmToken::Integer))
4493 return Error(L, "unexpected token in .code directive");
4494 int64_t Val = Parser.getTok().getIntVal();
4500 return Error(L, "invalid operand to .code directive");
4502 if (getLexer().isNot(AsmToken::EndOfStatement))
4503 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
4509 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
4513 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
4519 extern "C" void LLVMInitializeARMAsmLexer();
4521 /// Force static initialization.
4522 extern "C" void LLVMInitializeARMAsmParser() {
4523 RegisterMCAsmParser<ARMAsmParser> X(TheARMTarget);
4524 RegisterMCAsmParser<ARMAsmParser> Y(TheThumbTarget);
4525 LLVMInitializeARMAsmLexer();
4528 #define GET_REGISTER_MATCHER
4529 #define GET_MATCHER_IMPLEMENTATION
4530 #include "ARMGenAsmMatcher.inc"