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 //===----------------------------------------------------------------------===//
11 #include "ARMAddressingModes.h"
12 #include "ARMMCExpr.h"
13 #include "ARMBaseRegisterInfo.h"
14 #include "ARMSubtarget.h"
15 #include "llvm/MC/MCParser/MCAsmLexer.h"
16 #include "llvm/MC/MCParser/MCAsmParser.h"
17 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
18 #include "llvm/MC/MCAsmInfo.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/MC/MCStreamer.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCInst.h"
23 #include "llvm/MC/MCSubtargetInfo.h"
24 #include "llvm/Target/TargetRegistry.h"
25 #include "llvm/Target/TargetAsmParser.h"
26 #include "llvm/Support/SourceMgr.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/ADT/OwningPtr.h"
29 #include "llvm/ADT/SmallVector.h"
30 #include "llvm/ADT/StringExtras.h"
31 #include "llvm/ADT/StringSwitch.h"
32 #include "llvm/ADT/Twine.h"
40 class ARMAsmParser : public TargetAsmParser {
44 MCAsmParser &getParser() const { return Parser; }
45 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
47 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
48 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
50 int TryParseRegister();
51 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
52 bool TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
53 int TryParseShiftRegister(SmallVectorImpl<MCParsedAsmOperand*> &);
54 bool ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
55 bool ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &,
56 ARMII::AddrMode AddrMode);
57 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, StringRef Mnemonic);
58 bool ParsePrefix(ARMMCExpr::VariantKind &RefKind);
59 const MCExpr *ApplyPrefixToExpr(const MCExpr *E,
60 MCSymbolRefExpr::VariantKind Variant);
63 bool ParseMemoryOffsetReg(bool &Negative,
64 bool &OffsetRegShifted,
65 enum ARM_AM::ShiftOpc &ShiftType,
66 const MCExpr *&ShiftAmount,
67 const MCExpr *&Offset,
71 bool ParseShift(enum ARM_AM::ShiftOpc &St,
72 const MCExpr *&ShiftAmount, SMLoc &E);
73 bool ParseDirectiveWord(unsigned Size, SMLoc L);
74 bool ParseDirectiveThumb(SMLoc L);
75 bool ParseDirectiveThumbFunc(SMLoc L);
76 bool ParseDirectiveCode(SMLoc L);
77 bool ParseDirectiveSyntax(SMLoc L);
79 bool MatchAndEmitInstruction(SMLoc IDLoc,
80 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
82 void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
83 bool &CanAcceptPredicationCode);
85 bool isThumb() const {
86 // FIXME: Can tablegen auto-generate this?
87 return (STI.getFeatureBits() & ARM::ModeThumb) != 0;
89 bool isThumbOne() const {
90 return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2) == 0;
93 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(ARM::ModeThumb));
94 setAvailableFeatures(FB);
97 /// @name Auto-generated Match Functions
100 #define GET_ASSEMBLER_HEADER
101 #include "ARMGenAsmMatcher.inc"
105 OperandMatchResultTy tryParseCoprocNumOperand(
106 SmallVectorImpl<MCParsedAsmOperand*>&);
107 OperandMatchResultTy tryParseCoprocRegOperand(
108 SmallVectorImpl<MCParsedAsmOperand*>&);
109 OperandMatchResultTy tryParseMemBarrierOptOperand(
110 SmallVectorImpl<MCParsedAsmOperand*>&);
111 OperandMatchResultTy tryParseProcIFlagsOperand(
112 SmallVectorImpl<MCParsedAsmOperand*>&);
113 OperandMatchResultTy tryParseMSRMaskOperand(
114 SmallVectorImpl<MCParsedAsmOperand*>&);
115 OperandMatchResultTy tryParseMemMode2Operand(
116 SmallVectorImpl<MCParsedAsmOperand*>&);
117 OperandMatchResultTy tryParseMemMode3Operand(
118 SmallVectorImpl<MCParsedAsmOperand*>&);
120 // Asm Match Converter Methods
121 bool CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
122 const SmallVectorImpl<MCParsedAsmOperand*> &);
123 bool CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
124 const SmallVectorImpl<MCParsedAsmOperand*> &);
125 bool CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
126 const SmallVectorImpl<MCParsedAsmOperand*> &);
127 bool CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
128 const SmallVectorImpl<MCParsedAsmOperand*> &);
131 ARMAsmParser(MCSubtargetInfo &_STI, MCAsmParser &_Parser)
132 : TargetAsmParser(), STI(_STI), Parser(_Parser) {
133 MCAsmParserExtension::Initialize(_Parser);
135 // Initialize the set of available features.
136 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
139 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
140 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
141 virtual bool ParseDirective(AsmToken DirectiveID);
143 } // end anonymous namespace
147 /// ARMOperand - Instances of this class represent a parsed ARM machine
149 class ARMOperand : public MCParsedAsmOperand {
169 SMLoc StartLoc, EndLoc;
170 SmallVector<unsigned, 8> Registers;
174 ARMCC::CondCodes Val;
186 ARM_PROC::IFlags Val;
206 /// Combined record for all forms of ARM address expressions.
208 ARMII::AddrMode AddrMode;
211 unsigned RegNum; ///< Offset register num, when OffsetIsReg.
212 const MCExpr *Value; ///< Offset value, when !OffsetIsReg.
214 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
215 enum ARM_AM::ShiftOpc ShiftType; // used when OffsetRegShifted is true
216 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
217 unsigned Preindexed : 1;
218 unsigned Postindexed : 1;
219 unsigned OffsetIsReg : 1;
220 unsigned Negative : 1; // only used when OffsetIsReg is true
221 unsigned Writeback : 1;
225 ARM_AM::ShiftOpc ShiftTy;
229 ARM_AM::ShiftOpc ShiftTy;
236 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
238 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
240 StartLoc = o.StartLoc;
254 case DPRRegisterList:
255 case SPRRegisterList:
256 Registers = o.Registers;
280 case ShiftedRegister:
281 ShiftedReg = o.ShiftedReg;
286 /// getStartLoc - Get the location of the first token of this operand.
287 SMLoc getStartLoc() const { return StartLoc; }
288 /// getEndLoc - Get the location of the last token of this operand.
289 SMLoc getEndLoc() const { return EndLoc; }
291 ARMCC::CondCodes getCondCode() const {
292 assert(Kind == CondCode && "Invalid access!");
296 unsigned getCoproc() const {
297 assert((Kind == CoprocNum || Kind == CoprocReg) && "Invalid access!");
301 StringRef getToken() const {
302 assert(Kind == Token && "Invalid access!");
303 return StringRef(Tok.Data, Tok.Length);
306 unsigned getReg() const {
307 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
311 const SmallVectorImpl<unsigned> &getRegList() const {
312 assert((Kind == RegisterList || Kind == DPRRegisterList ||
313 Kind == SPRRegisterList) && "Invalid access!");
317 const MCExpr *getImm() const {
318 assert(Kind == Immediate && "Invalid access!");
322 ARM_MB::MemBOpt getMemBarrierOpt() const {
323 assert(Kind == MemBarrierOpt && "Invalid access!");
327 ARM_PROC::IFlags getProcIFlags() const {
328 assert(Kind == ProcIFlags && "Invalid access!");
332 unsigned getMSRMask() const {
333 assert(Kind == MSRMask && "Invalid access!");
337 /// @name Memory Operand Accessors
339 ARMII::AddrMode getMemAddrMode() const {
342 unsigned getMemBaseRegNum() const {
343 return Mem.BaseRegNum;
345 unsigned getMemOffsetRegNum() const {
346 assert(Mem.OffsetIsReg && "Invalid access!");
347 return Mem.Offset.RegNum;
349 const MCExpr *getMemOffset() const {
350 assert(!Mem.OffsetIsReg && "Invalid access!");
351 return Mem.Offset.Value;
353 unsigned getMemOffsetRegShifted() const {
354 assert(Mem.OffsetIsReg && "Invalid access!");
355 return Mem.OffsetRegShifted;
357 const MCExpr *getMemShiftAmount() const {
358 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
359 return Mem.ShiftAmount;
361 enum ARM_AM::ShiftOpc getMemShiftType() const {
362 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
363 return Mem.ShiftType;
365 bool getMemPreindexed() const { return Mem.Preindexed; }
366 bool getMemPostindexed() const { return Mem.Postindexed; }
367 bool getMemOffsetIsReg() const { return Mem.OffsetIsReg; }
368 bool getMemNegative() const { return Mem.Negative; }
369 bool getMemWriteback() const { return Mem.Writeback; }
373 bool isCoprocNum() const { return Kind == CoprocNum; }
374 bool isCoprocReg() const { return Kind == CoprocReg; }
375 bool isCondCode() const { return Kind == CondCode; }
376 bool isCCOut() const { return Kind == CCOut; }
377 bool isImm() const { return Kind == Immediate; }
378 bool isImm0_255() const {
379 if (Kind != Immediate)
381 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
382 if (!CE) return false;
383 int64_t Value = CE->getValue();
384 return Value >= 0 && Value < 256;
386 bool isImm0_7() const {
387 if (Kind != Immediate)
389 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
390 if (!CE) return false;
391 int64_t Value = CE->getValue();
392 return Value >= 0 && Value < 8;
394 bool isImm0_15() const {
395 if (Kind != Immediate)
397 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
398 if (!CE) return false;
399 int64_t Value = CE->getValue();
400 return Value >= 0 && Value < 16;
402 bool isImm0_65535() const {
403 if (Kind != Immediate)
405 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
406 if (!CE) return false;
407 int64_t Value = CE->getValue();
408 return Value >= 0 && Value < 65536;
410 bool isT2SOImm() const {
411 if (Kind != Immediate)
413 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
414 if (!CE) return false;
415 int64_t Value = CE->getValue();
416 return ARM_AM::getT2SOImmVal(Value) != -1;
418 bool isReg() const { return Kind == Register; }
419 bool isRegList() const { return Kind == RegisterList; }
420 bool isDPRRegList() const { return Kind == DPRRegisterList; }
421 bool isSPRRegList() const { return Kind == SPRRegisterList; }
422 bool isToken() const { return Kind == Token; }
423 bool isMemBarrierOpt() const { return Kind == MemBarrierOpt; }
424 bool isMemory() const { return Kind == Memory; }
425 bool isShifter() const { return Kind == Shifter; }
426 bool isShiftedReg() const { return Kind == ShiftedRegister; }
427 bool isMemMode2() const {
428 if (getMemAddrMode() != ARMII::AddrMode2)
431 if (getMemOffsetIsReg())
434 if (getMemNegative() &&
435 !(getMemPostindexed() || getMemPreindexed()))
438 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
439 if (!CE) return false;
440 int64_t Value = CE->getValue();
442 // The offset must be in the range 0-4095 (imm12).
443 if (Value > 4095 || Value < -4095)
448 bool isMemMode3() const {
449 if (getMemAddrMode() != ARMII::AddrMode3)
452 if (getMemOffsetIsReg()) {
453 if (getMemOffsetRegShifted())
454 return false; // No shift with offset reg allowed
458 if (getMemNegative() &&
459 !(getMemPostindexed() || getMemPreindexed()))
462 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
463 if (!CE) return false;
464 int64_t Value = CE->getValue();
466 // The offset must be in the range 0-255 (imm8).
467 if (Value > 255 || Value < -255)
472 bool isMemMode5() const {
473 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
477 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
478 if (!CE) return false;
480 // The offset must be a multiple of 4 in the range 0-1020.
481 int64_t Value = CE->getValue();
482 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
484 bool isMemMode7() const {
486 getMemPreindexed() ||
487 getMemPostindexed() ||
488 getMemOffsetIsReg() ||
493 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
494 if (!CE) return false;
501 bool isMemModeRegThumb() const {
502 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
506 bool isMemModeImmThumb() const {
507 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
510 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
511 if (!CE) return false;
513 // The offset must be a multiple of 4 in the range 0-124.
514 uint64_t Value = CE->getValue();
515 return ((Value & 0x3) == 0 && Value <= 124);
517 bool isMSRMask() const { return Kind == MSRMask; }
518 bool isProcIFlags() const { return Kind == ProcIFlags; }
520 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
521 // Add as immediates when possible. Null MCExpr = 0.
523 Inst.addOperand(MCOperand::CreateImm(0));
524 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
525 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
527 Inst.addOperand(MCOperand::CreateExpr(Expr));
530 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
531 assert(N == 2 && "Invalid number of operands!");
532 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
533 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
534 Inst.addOperand(MCOperand::CreateReg(RegNum));
537 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
538 assert(N == 1 && "Invalid number of operands!");
539 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
542 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
543 assert(N == 1 && "Invalid number of operands!");
544 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
547 void addCCOutOperands(MCInst &Inst, unsigned N) const {
548 assert(N == 1 && "Invalid number of operands!");
549 Inst.addOperand(MCOperand::CreateReg(getReg()));
552 void addRegOperands(MCInst &Inst, unsigned N) const {
553 assert(N == 1 && "Invalid number of operands!");
554 Inst.addOperand(MCOperand::CreateReg(getReg()));
557 void addShiftedRegOperands(MCInst &Inst, unsigned N) const {
558 assert(N == 3 && "Invalid number of operands!");
559 assert(isShiftedReg() && "addShiftedRegOperands() on non ShiftedReg!");
560 assert((ShiftedReg.ShiftReg == 0 ||
561 ARM_AM::getSORegOffset(ShiftedReg.ShiftImm) == 0) &&
562 "Invalid shifted register operand!");
563 Inst.addOperand(MCOperand::CreateReg(ShiftedReg.SrcReg));
564 Inst.addOperand(MCOperand::CreateReg(ShiftedReg.ShiftReg));
565 Inst.addOperand(MCOperand::CreateImm(
566 ARM_AM::getSORegOpc(ShiftedReg.ShiftTy, ShiftedReg.ShiftImm)));
569 void addShifterOperands(MCInst &Inst, unsigned N) const {
570 assert(N == 1 && "Invalid number of operands!");
571 Inst.addOperand(MCOperand::CreateImm(
572 ARM_AM::getSORegOpc(Shift.ShiftTy, 0)));
575 void addRegListOperands(MCInst &Inst, unsigned N) const {
576 assert(N == 1 && "Invalid number of operands!");
577 const SmallVectorImpl<unsigned> &RegList = getRegList();
578 for (SmallVectorImpl<unsigned>::const_iterator
579 I = RegList.begin(), E = RegList.end(); I != E; ++I)
580 Inst.addOperand(MCOperand::CreateReg(*I));
583 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
584 addRegListOperands(Inst, N);
587 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
588 addRegListOperands(Inst, N);
591 void addImmOperands(MCInst &Inst, unsigned N) const {
592 assert(N == 1 && "Invalid number of operands!");
593 addExpr(Inst, getImm());
596 void addImm0_255Operands(MCInst &Inst, unsigned N) const {
597 assert(N == 1 && "Invalid number of operands!");
598 addExpr(Inst, getImm());
601 void addImm0_7Operands(MCInst &Inst, unsigned N) const {
602 assert(N == 1 && "Invalid number of operands!");
603 addExpr(Inst, getImm());
606 void addImm0_15Operands(MCInst &Inst, unsigned N) const {
607 assert(N == 1 && "Invalid number of operands!");
608 addExpr(Inst, getImm());
611 void addImm0_65535Operands(MCInst &Inst, unsigned N) const {
612 assert(N == 1 && "Invalid number of operands!");
613 addExpr(Inst, getImm());
616 void addT2SOImmOperands(MCInst &Inst, unsigned N) const {
617 assert(N == 1 && "Invalid number of operands!");
618 addExpr(Inst, getImm());
621 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
622 assert(N == 1 && "Invalid number of operands!");
623 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
626 void addMemMode7Operands(MCInst &Inst, unsigned N) const {
627 assert(N == 1 && isMemMode7() && "Invalid number of operands!");
628 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
630 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
632 assert((CE || CE->getValue() == 0) &&
633 "No offset operand support in mode 7");
636 void addMemMode2Operands(MCInst &Inst, unsigned N) const {
637 assert(isMemMode2() && "Invalid mode or number of operands!");
638 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
639 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
641 if (getMemOffsetIsReg()) {
642 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
644 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
645 ARM_AM::ShiftOpc ShOpc = ARM_AM::no_shift;
646 int64_t ShiftAmount = 0;
648 if (getMemOffsetRegShifted()) {
649 ShOpc = getMemShiftType();
650 const MCConstantExpr *CE =
651 dyn_cast<MCConstantExpr>(getMemShiftAmount());
652 ShiftAmount = CE->getValue();
655 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(AMOpc, ShiftAmount,
660 // Create a operand placeholder to always yield the same number of operands.
661 Inst.addOperand(MCOperand::CreateReg(0));
663 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
665 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
666 assert(CE && "Non-constant mode 2 offset operand!");
667 int64_t Offset = CE->getValue();
670 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::add,
671 Offset, ARM_AM::no_shift, IdxMode)));
673 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::sub,
674 -Offset, ARM_AM::no_shift, IdxMode)));
677 void addMemMode3Operands(MCInst &Inst, unsigned N) const {
678 assert(isMemMode3() && "Invalid mode or number of operands!");
679 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
680 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
682 if (getMemOffsetIsReg()) {
683 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
685 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
686 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(AMOpc, 0,
691 // Create a operand placeholder to always yield the same number of operands.
692 Inst.addOperand(MCOperand::CreateReg(0));
694 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
696 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
697 assert(CE && "Non-constant mode 3 offset operand!");
698 int64_t Offset = CE->getValue();
701 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::add,
704 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::sub,
708 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
709 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
711 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
712 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
714 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
716 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
717 assert(CE && "Non-constant mode 5 offset operand!");
719 // The MCInst offset operand doesn't include the low two bits (like
720 // the instruction encoding).
721 int64_t Offset = CE->getValue() / 4;
723 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
726 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
730 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
731 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
732 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
733 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
736 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
737 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
738 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
739 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
740 assert(CE && "Non-constant mode offset operand!");
741 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
744 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
745 assert(N == 1 && "Invalid number of operands!");
746 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
749 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
750 assert(N == 1 && "Invalid number of operands!");
751 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
754 virtual void print(raw_ostream &OS) const;
756 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
757 ARMOperand *Op = new ARMOperand(CondCode);
764 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
765 ARMOperand *Op = new ARMOperand(CoprocNum);
766 Op->Cop.Val = CopVal;
772 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
773 ARMOperand *Op = new ARMOperand(CoprocReg);
774 Op->Cop.Val = CopVal;
780 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
781 ARMOperand *Op = new ARMOperand(CCOut);
782 Op->Reg.RegNum = RegNum;
788 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
789 ARMOperand *Op = new ARMOperand(Token);
790 Op->Tok.Data = Str.data();
791 Op->Tok.Length = Str.size();
797 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
798 ARMOperand *Op = new ARMOperand(Register);
799 Op->Reg.RegNum = RegNum;
805 static ARMOperand *CreateShiftedRegister(ARM_AM::ShiftOpc ShTy,
810 ARMOperand *Op = new ARMOperand(ShiftedRegister);
811 Op->ShiftedReg.ShiftTy = ShTy;
812 Op->ShiftedReg.SrcReg = SrcReg;
813 Op->ShiftedReg.ShiftReg = ShiftReg;
814 Op->ShiftedReg.ShiftImm = ShiftImm;
820 static ARMOperand *CreateShifter(ARM_AM::ShiftOpc ShTy,
822 ARMOperand *Op = new ARMOperand(Shifter);
823 Op->Shift.ShiftTy = ShTy;
830 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
831 SMLoc StartLoc, SMLoc EndLoc) {
832 KindTy Kind = RegisterList;
834 if (ARM::DPRRegClass.contains(Regs.front().first))
835 Kind = DPRRegisterList;
836 else if (ARM::SPRRegClass.contains(Regs.front().first))
837 Kind = SPRRegisterList;
839 ARMOperand *Op = new ARMOperand(Kind);
840 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
841 I = Regs.begin(), E = Regs.end(); I != E; ++I)
842 Op->Registers.push_back(I->first);
843 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
844 Op->StartLoc = StartLoc;
849 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
850 ARMOperand *Op = new ARMOperand(Immediate);
857 static ARMOperand *CreateMem(ARMII::AddrMode AddrMode, unsigned BaseRegNum,
858 bool OffsetIsReg, const MCExpr *Offset,
859 int OffsetRegNum, bool OffsetRegShifted,
860 enum ARM_AM::ShiftOpc ShiftType,
861 const MCExpr *ShiftAmount, bool Preindexed,
862 bool Postindexed, bool Negative, bool Writeback,
864 assert((OffsetRegNum == -1 || OffsetIsReg) &&
865 "OffsetRegNum must imply OffsetIsReg!");
866 assert((!OffsetRegShifted || OffsetIsReg) &&
867 "OffsetRegShifted must imply OffsetIsReg!");
868 assert((Offset || OffsetIsReg) &&
869 "Offset must exists unless register offset is used!");
870 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
871 "Cannot have shift amount without shifted register offset!");
872 assert((!Offset || !OffsetIsReg) &&
873 "Cannot have expression offset and register offset!");
875 ARMOperand *Op = new ARMOperand(Memory);
876 Op->Mem.AddrMode = AddrMode;
877 Op->Mem.BaseRegNum = BaseRegNum;
878 Op->Mem.OffsetIsReg = OffsetIsReg;
880 Op->Mem.Offset.RegNum = OffsetRegNum;
882 Op->Mem.Offset.Value = Offset;
883 Op->Mem.OffsetRegShifted = OffsetRegShifted;
884 Op->Mem.ShiftType = ShiftType;
885 Op->Mem.ShiftAmount = ShiftAmount;
886 Op->Mem.Preindexed = Preindexed;
887 Op->Mem.Postindexed = Postindexed;
888 Op->Mem.Negative = Negative;
889 Op->Mem.Writeback = Writeback;
896 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
897 ARMOperand *Op = new ARMOperand(MemBarrierOpt);
904 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
905 ARMOperand *Op = new ARMOperand(ProcIFlags);
906 Op->IFlags.Val = IFlags;
912 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
913 ARMOperand *Op = new ARMOperand(MSRMask);
914 Op->MMask.Val = MMask;
921 } // end anonymous namespace.
923 void ARMOperand::print(raw_ostream &OS) const {
926 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
929 OS << "<ccout " << getReg() << ">";
932 OS << "<coprocessor number: " << getCoproc() << ">";
935 OS << "<coprocessor register: " << getCoproc() << ">";
938 OS << "<mask: " << getMSRMask() << ">";
944 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
948 << "am:" << ARMII::AddrModeToString(getMemAddrMode())
949 << " base:" << getMemBaseRegNum();
950 if (getMemOffsetIsReg()) {
951 OS << " offset:<register " << getMemOffsetRegNum();
952 if (getMemOffsetRegShifted()) {
953 OS << " offset-shift-type:" << getMemShiftType();
954 OS << " offset-shift-amount:" << *getMemShiftAmount();
957 OS << " offset:" << *getMemOffset();
959 if (getMemOffsetIsReg())
960 OS << " (offset-is-reg)";
961 if (getMemPreindexed())
962 OS << " (pre-indexed)";
963 if (getMemPostindexed())
964 OS << " (post-indexed)";
965 if (getMemNegative())
967 if (getMemWriteback())
968 OS << " (writeback)";
973 unsigned IFlags = getProcIFlags();
974 for (int i=2; i >= 0; --i)
975 if (IFlags & (1 << i))
976 OS << ARM_PROC::IFlagsToString(1 << i);
981 OS << "<register " << getReg() << ">";
984 OS << "<shifter " << ARM_AM::getShiftOpcStr(Shift.ShiftTy) << ">";
986 case ShiftedRegister:
989 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(ShiftedReg.ShiftImm))
990 << ", " << ShiftedReg.ShiftReg << ", "
991 << ARM_AM::getSORegOffset(ShiftedReg.ShiftImm)
995 case DPRRegisterList:
996 case SPRRegisterList: {
997 OS << "<register_list ";
999 const SmallVectorImpl<unsigned> &RegList = getRegList();
1000 for (SmallVectorImpl<unsigned>::const_iterator
1001 I = RegList.begin(), E = RegList.end(); I != E; ) {
1003 if (++I < E) OS << ", ";
1010 OS << "'" << getToken() << "'";
1015 /// @name Auto-generated Match Functions
1018 static unsigned MatchRegisterName(StringRef Name);
1022 bool ARMAsmParser::ParseRegister(unsigned &RegNo,
1023 SMLoc &StartLoc, SMLoc &EndLoc) {
1024 RegNo = TryParseRegister();
1026 return (RegNo == (unsigned)-1);
1029 /// Try to parse a register name. The token must be an Identifier when called,
1030 /// and if it is a register name the token is eaten and the register number is
1031 /// returned. Otherwise return -1.
1033 int ARMAsmParser::TryParseRegister() {
1034 const AsmToken &Tok = Parser.getTok();
1035 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1037 // FIXME: Validate register for the current architecture; we have to do
1038 // validation later, so maybe there is no need for this here.
1039 std::string upperCase = Tok.getString().str();
1040 std::string lowerCase = LowercaseString(upperCase);
1041 unsigned RegNum = MatchRegisterName(lowerCase);
1043 RegNum = StringSwitch<unsigned>(lowerCase)
1044 .Case("r13", ARM::SP)
1045 .Case("r14", ARM::LR)
1046 .Case("r15", ARM::PC)
1047 .Case("ip", ARM::R12)
1050 if (!RegNum) return -1;
1052 Parser.Lex(); // Eat identifier token.
1056 // Try to parse a shifter (e.g., "lsl <amt>"). On success, return 0.
1057 // If a recoverable error occurs, return 1. If an irrecoverable error
1058 // occurs, return -1. An irrecoverable error is one where tokens have been
1059 // consumed in the process of trying to parse the shifter (i.e., when it is
1060 // indeed a shifter operand, but malformed).
1061 int ARMAsmParser::TryParseShiftRegister(
1062 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1063 SMLoc S = Parser.getTok().getLoc();
1064 const AsmToken &Tok = Parser.getTok();
1065 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1067 std::string upperCase = Tok.getString().str();
1068 std::string lowerCase = LowercaseString(upperCase);
1069 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
1070 .Case("lsl", ARM_AM::lsl)
1071 .Case("lsr", ARM_AM::lsr)
1072 .Case("asr", ARM_AM::asr)
1073 .Case("ror", ARM_AM::ror)
1074 .Case("rrx", ARM_AM::rrx)
1075 .Default(ARM_AM::no_shift);
1077 if (ShiftTy == ARM_AM::no_shift)
1080 Parser.Lex(); // Eat the operator.
1082 // The source register for the shift has already been added to the
1083 // operand list, so we need to pop it off and combine it into the shifted
1084 // register operand instead.
1085 OwningPtr<ARMOperand> PrevOp((ARMOperand*)Operands.pop_back_val());
1086 if (!PrevOp->isReg())
1087 return Error(PrevOp->getStartLoc(), "shift must be of a register");
1088 int SrcReg = PrevOp->getReg();
1091 if (ShiftTy == ARM_AM::rrx) {
1092 // RRX Doesn't have an explicit shift amount. The encoder expects
1093 // the shift register to be the same as the source register. Seems odd,
1097 // Figure out if this is shifted by a constant or a register (for non-RRX).
1098 if (Parser.getTok().is(AsmToken::Hash)) {
1099 Parser.Lex(); // Eat hash.
1100 SMLoc ImmLoc = Parser.getTok().getLoc();
1101 const MCExpr *ShiftExpr = 0;
1102 if (getParser().ParseExpression(ShiftExpr)) {
1103 Error(ImmLoc, "invalid immediate shift value");
1106 // The expression must be evaluatable as an immediate.
1107 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
1109 Error(ImmLoc, "invalid immediate shift value");
1112 // Range check the immediate.
1113 // lsl, ror: 0 <= imm <= 31
1114 // lsr, asr: 0 <= imm <= 32
1115 Imm = CE->getValue();
1117 ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
1118 ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
1119 Error(ImmLoc, "immediate shift value out of range");
1122 } else if (Parser.getTok().is(AsmToken::Identifier)) {
1123 ShiftReg = TryParseRegister();
1124 SMLoc L = Parser.getTok().getLoc();
1125 if (ShiftReg == -1) {
1126 Error (L, "expected immediate or register in shift operand");
1130 Error (Parser.getTok().getLoc(),
1131 "expected immediate or register in shift operand");
1136 Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
1138 S, Parser.getTok().getLoc()));
1144 /// Try to parse a register name. The token must be an Identifier when called.
1145 /// If it's a register, an AsmOperand is created. Another AsmOperand is created
1146 /// if there is a "writeback". 'true' if it's not a register.
1148 /// TODO this is likely to change to allow different register types and or to
1149 /// parse for a specific register type.
1151 TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1152 SMLoc S = Parser.getTok().getLoc();
1153 int RegNo = TryParseRegister();
1157 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
1159 const AsmToken &ExclaimTok = Parser.getTok();
1160 if (ExclaimTok.is(AsmToken::Exclaim)) {
1161 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
1162 ExclaimTok.getLoc()));
1163 Parser.Lex(); // Eat exclaim token
1169 /// MatchCoprocessorOperandName - Try to parse an coprocessor related
1170 /// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
1172 static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
1173 // Use the same layout as the tablegen'erated register name matcher. Ugly,
1175 switch (Name.size()) {
1178 if (Name[0] != CoprocOp)
1195 if (Name[0] != CoprocOp || Name[1] != '1')
1199 case '0': return 10;
1200 case '1': return 11;
1201 case '2': return 12;
1202 case '3': return 13;
1203 case '4': return 14;
1204 case '5': return 15;
1212 /// tryParseCoprocNumOperand - Try to parse an coprocessor number operand. The
1213 /// token must be an Identifier when called, and if it is a coprocessor
1214 /// number, the token is eaten and the operand is added to the operand list.
1215 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1216 tryParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1217 SMLoc S = Parser.getTok().getLoc();
1218 const AsmToken &Tok = Parser.getTok();
1219 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1221 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
1223 return MatchOperand_NoMatch;
1225 Parser.Lex(); // Eat identifier token.
1226 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
1227 return MatchOperand_Success;
1230 /// tryParseCoprocRegOperand - Try to parse an coprocessor register operand. The
1231 /// token must be an Identifier when called, and if it is a coprocessor
1232 /// number, the token is eaten and the operand is added to the operand list.
1233 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1234 tryParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1235 SMLoc S = Parser.getTok().getLoc();
1236 const AsmToken &Tok = Parser.getTok();
1237 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1239 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
1241 return MatchOperand_NoMatch;
1243 Parser.Lex(); // Eat identifier token.
1244 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
1245 return MatchOperand_Success;
1248 /// Parse a register list, return it if successful else return null. The first
1249 /// token must be a '{' when called.
1251 ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1252 assert(Parser.getTok().is(AsmToken::LCurly) &&
1253 "Token is not a Left Curly Brace");
1254 SMLoc S = Parser.getTok().getLoc();
1256 // Read the rest of the registers in the list.
1257 unsigned PrevRegNum = 0;
1258 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
1261 bool IsRange = Parser.getTok().is(AsmToken::Minus);
1262 Parser.Lex(); // Eat non-identifier token.
1264 const AsmToken &RegTok = Parser.getTok();
1265 SMLoc RegLoc = RegTok.getLoc();
1266 if (RegTok.isNot(AsmToken::Identifier)) {
1267 Error(RegLoc, "register expected");
1271 int RegNum = TryParseRegister();
1273 Error(RegLoc, "register expected");
1278 int Reg = PrevRegNum;
1281 Registers.push_back(std::make_pair(Reg, RegLoc));
1282 } while (Reg != RegNum);
1284 Registers.push_back(std::make_pair(RegNum, RegLoc));
1287 PrevRegNum = RegNum;
1288 } while (Parser.getTok().is(AsmToken::Comma) ||
1289 Parser.getTok().is(AsmToken::Minus));
1291 // Process the right curly brace of the list.
1292 const AsmToken &RCurlyTok = Parser.getTok();
1293 if (RCurlyTok.isNot(AsmToken::RCurly)) {
1294 Error(RCurlyTok.getLoc(), "'}' expected");
1298 SMLoc E = RCurlyTok.getLoc();
1299 Parser.Lex(); // Eat right curly brace token.
1301 // Verify the register list.
1302 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
1303 RI = Registers.begin(), RE = Registers.end();
1305 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
1306 bool EmittedWarning = false;
1308 DenseMap<unsigned, bool> RegMap;
1309 RegMap[HighRegNum] = true;
1311 for (++RI; RI != RE; ++RI) {
1312 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
1313 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
1316 Error(RegInfo.second, "register duplicated in register list");
1320 if (!EmittedWarning && Reg < HighRegNum)
1321 Warning(RegInfo.second,
1322 "register not in ascending order in register list");
1325 HighRegNum = std::max(Reg, HighRegNum);
1328 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
1332 /// tryParseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
1333 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1334 tryParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1335 SMLoc S = Parser.getTok().getLoc();
1336 const AsmToken &Tok = Parser.getTok();
1337 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1338 StringRef OptStr = Tok.getString();
1340 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
1341 .Case("sy", ARM_MB::SY)
1342 .Case("st", ARM_MB::ST)
1343 .Case("sh", ARM_MB::ISH)
1344 .Case("ish", ARM_MB::ISH)
1345 .Case("shst", ARM_MB::ISHST)
1346 .Case("ishst", ARM_MB::ISHST)
1347 .Case("nsh", ARM_MB::NSH)
1348 .Case("un", ARM_MB::NSH)
1349 .Case("nshst", ARM_MB::NSHST)
1350 .Case("unst", ARM_MB::NSHST)
1351 .Case("osh", ARM_MB::OSH)
1352 .Case("oshst", ARM_MB::OSHST)
1356 return MatchOperand_NoMatch;
1358 Parser.Lex(); // Eat identifier token.
1359 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
1360 return MatchOperand_Success;
1363 /// tryParseProcIFlagsOperand - Try to parse iflags from CPS instruction.
1364 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1365 tryParseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1366 SMLoc S = Parser.getTok().getLoc();
1367 const AsmToken &Tok = Parser.getTok();
1368 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1369 StringRef IFlagsStr = Tok.getString();
1371 unsigned IFlags = 0;
1372 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
1373 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
1374 .Case("a", ARM_PROC::A)
1375 .Case("i", ARM_PROC::I)
1376 .Case("f", ARM_PROC::F)
1379 // If some specific iflag is already set, it means that some letter is
1380 // present more than once, this is not acceptable.
1381 if (Flag == ~0U || (IFlags & Flag))
1382 return MatchOperand_NoMatch;
1387 Parser.Lex(); // Eat identifier token.
1388 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
1389 return MatchOperand_Success;
1392 /// tryParseMSRMaskOperand - Try to parse mask flags from MSR instruction.
1393 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1394 tryParseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1395 SMLoc S = Parser.getTok().getLoc();
1396 const AsmToken &Tok = Parser.getTok();
1397 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1398 StringRef Mask = Tok.getString();
1400 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
1401 size_t Start = 0, Next = Mask.find('_');
1402 StringRef Flags = "";
1403 StringRef SpecReg = Mask.slice(Start, Next);
1404 if (Next != StringRef::npos)
1405 Flags = Mask.slice(Next+1, Mask.size());
1407 // FlagsVal contains the complete mask:
1409 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1410 unsigned FlagsVal = 0;
1412 if (SpecReg == "apsr") {
1413 FlagsVal = StringSwitch<unsigned>(Flags)
1414 .Case("nzcvq", 0x8) // same as CPSR_c
1415 .Case("g", 0x4) // same as CPSR_s
1416 .Case("nzcvqg", 0xc) // same as CPSR_fs
1419 if (FlagsVal == ~0U) {
1421 return MatchOperand_NoMatch;
1423 FlagsVal = 0; // No flag
1425 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
1426 if (Flags == "all") // cpsr_all is an alias for cpsr_fc
1428 for (int i = 0, e = Flags.size(); i != e; ++i) {
1429 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
1436 // If some specific flag is already set, it means that some letter is
1437 // present more than once, this is not acceptable.
1438 if (FlagsVal == ~0U || (FlagsVal & Flag))
1439 return MatchOperand_NoMatch;
1442 } else // No match for special register.
1443 return MatchOperand_NoMatch;
1445 // Special register without flags are equivalent to "fc" flags.
1449 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1450 if (SpecReg == "spsr")
1453 Parser.Lex(); // Eat identifier token.
1454 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
1455 return MatchOperand_Success;
1458 /// tryParseMemMode2Operand - Try to parse memory addressing mode 2 operand.
1459 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1460 tryParseMemMode2Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1461 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
1463 if (ParseMemory(Operands, ARMII::AddrMode2))
1464 return MatchOperand_NoMatch;
1466 return MatchOperand_Success;
1469 /// tryParseMemMode3Operand - Try to parse memory addressing mode 3 operand.
1470 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1471 tryParseMemMode3Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1472 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
1474 if (ParseMemory(Operands, ARMII::AddrMode3))
1475 return MatchOperand_NoMatch;
1477 return MatchOperand_Success;
1480 /// CvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1481 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
1482 /// when they refer multiple MIOperands inside a single one.
1484 CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1485 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1486 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1488 // Create a writeback register dummy placeholder.
1489 Inst.addOperand(MCOperand::CreateImm(0));
1491 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1492 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1496 /// CvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1497 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
1498 /// when they refer multiple MIOperands inside a single one.
1500 CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1501 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1502 // Create a writeback register dummy placeholder.
1503 Inst.addOperand(MCOperand::CreateImm(0));
1504 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1505 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1506 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1510 /// CvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1511 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
1512 /// when they refer multiple MIOperands inside a single one.
1514 CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1515 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1516 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1518 // Create a writeback register dummy placeholder.
1519 Inst.addOperand(MCOperand::CreateImm(0));
1521 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1522 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1526 /// CvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1527 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
1528 /// when they refer multiple MIOperands inside a single one.
1530 CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1531 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1532 // Create a writeback register dummy placeholder.
1533 Inst.addOperand(MCOperand::CreateImm(0));
1534 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1535 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1536 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1540 /// Parse an ARM memory expression, return false if successful else return true
1541 /// or an error. The first token must be a '[' when called.
1543 /// TODO Only preindexing and postindexing addressing are started, unindexed
1544 /// with option, etc are still to do.
1546 ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1547 ARMII::AddrMode AddrMode = ARMII::AddrModeNone) {
1549 assert(Parser.getTok().is(AsmToken::LBrac) &&
1550 "Token is not a Left Bracket");
1551 S = Parser.getTok().getLoc();
1552 Parser.Lex(); // Eat left bracket token.
1554 const AsmToken &BaseRegTok = Parser.getTok();
1555 if (BaseRegTok.isNot(AsmToken::Identifier)) {
1556 Error(BaseRegTok.getLoc(), "register expected");
1559 int BaseRegNum = TryParseRegister();
1560 if (BaseRegNum == -1) {
1561 Error(BaseRegTok.getLoc(), "register expected");
1565 // The next token must either be a comma or a closing bracket.
1566 const AsmToken &Tok = Parser.getTok();
1567 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
1570 bool Preindexed = false;
1571 bool Postindexed = false;
1572 bool OffsetIsReg = false;
1573 bool Negative = false;
1574 bool Writeback = false;
1575 ARMOperand *WBOp = 0;
1576 int OffsetRegNum = -1;
1577 bool OffsetRegShifted = false;
1578 enum ARM_AM::ShiftOpc ShiftType = ARM_AM::lsl;
1579 const MCExpr *ShiftAmount = 0;
1580 const MCExpr *Offset = 0;
1582 // First look for preindexed address forms, that is after the "[Rn" we now
1583 // have to see if the next token is a comma.
1584 if (Tok.is(AsmToken::Comma)) {
1586 Parser.Lex(); // Eat comma token.
1588 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
1589 Offset, OffsetIsReg, OffsetRegNum, E))
1591 const AsmToken &RBracTok = Parser.getTok();
1592 if (RBracTok.isNot(AsmToken::RBrac)) {
1593 Error(RBracTok.getLoc(), "']' expected");
1596 E = RBracTok.getLoc();
1597 Parser.Lex(); // Eat right bracket token.
1599 const AsmToken &ExclaimTok = Parser.getTok();
1600 if (ExclaimTok.is(AsmToken::Exclaim)) {
1601 // None of addrmode3 instruction uses "!"
1602 if (AddrMode == ARMII::AddrMode3)
1605 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
1606 ExclaimTok.getLoc());
1608 Parser.Lex(); // Eat exclaim token
1609 } else { // In addressing mode 2, pre-indexed mode always end with "!"
1610 if (AddrMode == ARMII::AddrMode2)
1614 // The "[Rn" we have so far was not followed by a comma.
1616 // If there's anything other than the right brace, this is a post indexing
1619 Parser.Lex(); // Eat right bracket token.
1621 const AsmToken &NextTok = Parser.getTok();
1623 if (NextTok.isNot(AsmToken::EndOfStatement)) {
1627 if (NextTok.isNot(AsmToken::Comma)) {
1628 Error(NextTok.getLoc(), "',' expected");
1632 Parser.Lex(); // Eat comma token.
1634 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
1635 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
1641 // Force Offset to exist if used.
1644 Offset = MCConstantExpr::Create(0, getContext());
1646 if (AddrMode == ARMII::AddrMode3 && OffsetRegShifted) {
1647 Error(E, "shift amount not supported");
1652 Operands.push_back(ARMOperand::CreateMem(AddrMode, BaseRegNum, OffsetIsReg,
1653 Offset, OffsetRegNum, OffsetRegShifted,
1654 ShiftType, ShiftAmount, Preindexed,
1655 Postindexed, Negative, Writeback, S, E));
1657 Operands.push_back(WBOp);
1662 /// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
1663 /// we will parse the following (were +/- means that a plus or minus is
1668 /// we return false on success or an error otherwise.
1669 bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
1670 bool &OffsetRegShifted,
1671 enum ARM_AM::ShiftOpc &ShiftType,
1672 const MCExpr *&ShiftAmount,
1673 const MCExpr *&Offset,
1678 OffsetRegShifted = false;
1679 OffsetIsReg = false;
1681 const AsmToken &NextTok = Parser.getTok();
1682 E = NextTok.getLoc();
1683 if (NextTok.is(AsmToken::Plus))
1684 Parser.Lex(); // Eat plus token.
1685 else if (NextTok.is(AsmToken::Minus)) {
1687 Parser.Lex(); // Eat minus token
1689 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
1690 const AsmToken &OffsetRegTok = Parser.getTok();
1691 if (OffsetRegTok.is(AsmToken::Identifier)) {
1692 SMLoc CurLoc = OffsetRegTok.getLoc();
1693 OffsetRegNum = TryParseRegister();
1694 if (OffsetRegNum != -1) {
1700 // If we parsed a register as the offset then there can be a shift after that.
1701 if (OffsetRegNum != -1) {
1702 // Look for a comma then a shift
1703 const AsmToken &Tok = Parser.getTok();
1704 if (Tok.is(AsmToken::Comma)) {
1705 Parser.Lex(); // Eat comma token.
1707 const AsmToken &Tok = Parser.getTok();
1708 if (ParseShift(ShiftType, ShiftAmount, E))
1709 return Error(Tok.getLoc(), "shift expected");
1710 OffsetRegShifted = true;
1713 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1714 // Look for #offset following the "[Rn," or "[Rn],"
1715 const AsmToken &HashTok = Parser.getTok();
1716 if (HashTok.isNot(AsmToken::Hash))
1717 return Error(HashTok.getLoc(), "'#' expected");
1719 Parser.Lex(); // Eat hash token.
1721 if (getParser().ParseExpression(Offset))
1723 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1728 /// ParseShift as one of these two:
1729 /// ( lsl | lsr | asr | ror ) , # shift_amount
1731 /// and returns true if it parses a shift otherwise it returns false.
1732 bool ARMAsmParser::ParseShift(ARM_AM::ShiftOpc &St,
1733 const MCExpr *&ShiftAmount, SMLoc &E) {
1734 const AsmToken &Tok = Parser.getTok();
1735 if (Tok.isNot(AsmToken::Identifier))
1737 StringRef ShiftName = Tok.getString();
1738 if (ShiftName == "lsl" || ShiftName == "LSL")
1740 else if (ShiftName == "lsr" || ShiftName == "LSR")
1742 else if (ShiftName == "asr" || ShiftName == "ASR")
1744 else if (ShiftName == "ror" || ShiftName == "ROR")
1746 else if (ShiftName == "rrx" || ShiftName == "RRX")
1750 Parser.Lex(); // Eat shift type token.
1752 // Rrx stands alone.
1753 if (St == ARM_AM::rrx)
1756 // Otherwise, there must be a '#' and a shift amount.
1757 const AsmToken &HashTok = Parser.getTok();
1758 if (HashTok.isNot(AsmToken::Hash))
1759 return Error(HashTok.getLoc(), "'#' expected");
1760 Parser.Lex(); // Eat hash token.
1762 if (getParser().ParseExpression(ShiftAmount))
1768 /// Parse a arm instruction operand. For now this parses the operand regardless
1769 /// of the mnemonic.
1770 bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1771 StringRef Mnemonic) {
1774 // Check if the current operand has a custom associated parser, if so, try to
1775 // custom parse the operand, or fallback to the general approach.
1776 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1777 if (ResTy == MatchOperand_Success)
1779 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1780 // there was a match, but an error occurred, in which case, just return that
1781 // the operand parsing failed.
1782 if (ResTy == MatchOperand_ParseFail)
1785 switch (getLexer().getKind()) {
1787 Error(Parser.getTok().getLoc(), "unexpected token in operand");
1789 case AsmToken::Identifier: {
1790 if (!TryParseRegisterWithWriteBack(Operands))
1792 int Res = TryParseShiftRegister(Operands);
1793 if (Res == 0) // success
1795 else if (Res == -1) // irrecoverable error
1798 // Fall though for the Identifier case that is not a register or a
1801 case AsmToken::Integer: // things like 1f and 2b as a branch targets
1802 case AsmToken::Dot: { // . as a branch target
1803 // This was not a register so parse other operands that start with an
1804 // identifier (like labels) as expressions and create them as immediates.
1805 const MCExpr *IdVal;
1806 S = Parser.getTok().getLoc();
1807 if (getParser().ParseExpression(IdVal))
1809 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1810 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1813 case AsmToken::LBrac:
1814 return ParseMemory(Operands);
1815 case AsmToken::LCurly:
1816 return ParseRegisterList(Operands);
1817 case AsmToken::Hash:
1818 // #42 -> immediate.
1819 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
1820 S = Parser.getTok().getLoc();
1822 const MCExpr *ImmVal;
1823 if (getParser().ParseExpression(ImmVal))
1825 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1826 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1828 case AsmToken::Colon: {
1829 // ":lower16:" and ":upper16:" expression prefixes
1830 // FIXME: Check it's an expression prefix,
1831 // e.g. (FOO - :lower16:BAR) isn't legal.
1832 ARMMCExpr::VariantKind RefKind;
1833 if (ParsePrefix(RefKind))
1836 const MCExpr *SubExprVal;
1837 if (getParser().ParseExpression(SubExprVal))
1840 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1842 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1843 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
1849 // ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1850 // :lower16: and :upper16:.
1851 bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1852 RefKind = ARMMCExpr::VK_ARM_None;
1854 // :lower16: and :upper16: modifiers
1855 assert(getLexer().is(AsmToken::Colon) && "expected a :");
1856 Parser.Lex(); // Eat ':'
1858 if (getLexer().isNot(AsmToken::Identifier)) {
1859 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1863 StringRef IDVal = Parser.getTok().getIdentifier();
1864 if (IDVal == "lower16") {
1865 RefKind = ARMMCExpr::VK_ARM_LO16;
1866 } else if (IDVal == "upper16") {
1867 RefKind = ARMMCExpr::VK_ARM_HI16;
1869 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1874 if (getLexer().isNot(AsmToken::Colon)) {
1875 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1878 Parser.Lex(); // Eat the last ':'
1883 ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1884 MCSymbolRefExpr::VariantKind Variant) {
1885 // Recurse over the given expression, rebuilding it to apply the given variant
1886 // to the leftmost symbol.
1887 if (Variant == MCSymbolRefExpr::VK_None)
1890 switch (E->getKind()) {
1891 case MCExpr::Target:
1892 llvm_unreachable("Can't handle target expr yet");
1893 case MCExpr::Constant:
1894 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1896 case MCExpr::SymbolRef: {
1897 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1899 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1902 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1906 llvm_unreachable("Can't handle unary expressions yet");
1908 case MCExpr::Binary: {
1909 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1910 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1911 const MCExpr *RHS = BE->getRHS();
1915 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1919 assert(0 && "Invalid expression kind!");
1923 /// \brief Given a mnemonic, split out possible predication code and carry
1924 /// setting letters to form a canonical mnemonic and flags.
1926 // FIXME: Would be nice to autogen this.
1927 static StringRef SplitMnemonic(StringRef Mnemonic,
1928 unsigned &PredicationCode,
1930 unsigned &ProcessorIMod) {
1931 PredicationCode = ARMCC::AL;
1932 CarrySetting = false;
1935 // Ignore some mnemonics we know aren't predicated forms.
1937 // FIXME: Would be nice to autogen this.
1938 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1939 Mnemonic == "movs" ||
1940 Mnemonic == "svc" ||
1941 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1942 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1943 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1944 Mnemonic == "vclt" ||
1945 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1946 Mnemonic == "vcle" ||
1947 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1948 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
1949 Mnemonic == "vqdmlal" || Mnemonic == "bics"))
1952 // First, split out any predication code. Ignore mnemonics we know aren't
1953 // predicated but do have a carry-set and so weren't caught above.
1954 if (Mnemonic != "adcs") {
1955 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
1956 .Case("eq", ARMCC::EQ)
1957 .Case("ne", ARMCC::NE)
1958 .Case("hs", ARMCC::HS)
1959 .Case("cs", ARMCC::HS)
1960 .Case("lo", ARMCC::LO)
1961 .Case("cc", ARMCC::LO)
1962 .Case("mi", ARMCC::MI)
1963 .Case("pl", ARMCC::PL)
1964 .Case("vs", ARMCC::VS)
1965 .Case("vc", ARMCC::VC)
1966 .Case("hi", ARMCC::HI)
1967 .Case("ls", ARMCC::LS)
1968 .Case("ge", ARMCC::GE)
1969 .Case("lt", ARMCC::LT)
1970 .Case("gt", ARMCC::GT)
1971 .Case("le", ARMCC::LE)
1972 .Case("al", ARMCC::AL)
1975 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
1976 PredicationCode = CC;
1980 // Next, determine if we have a carry setting bit. We explicitly ignore all
1981 // the instructions we know end in 's'.
1982 if (Mnemonic.endswith("s") &&
1983 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1984 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1985 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1986 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1987 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1988 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1989 CarrySetting = true;
1992 // The "cps" instruction can have a interrupt mode operand which is glued into
1993 // the mnemonic. Check if this is the case, split it and parse the imod op
1994 if (Mnemonic.startswith("cps")) {
1995 // Split out any imod code.
1997 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
1998 .Case("ie", ARM_PROC::IE)
1999 .Case("id", ARM_PROC::ID)
2002 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
2003 ProcessorIMod = IMod;
2010 /// \brief Given a canonical mnemonic, determine if the instruction ever allows
2011 /// inclusion of carry set or predication code operands.
2013 // FIXME: It would be nice to autogen this.
2015 GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
2016 bool &CanAcceptPredicationCode) {
2017 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
2018 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
2019 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
2020 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
2021 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mvn" ||
2022 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
2023 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
2024 Mnemonic == "eor" || Mnemonic == "smlal" ||
2025 (Mnemonic == "mov" && !isThumbOne())) {
2026 CanAcceptCarrySet = true;
2028 CanAcceptCarrySet = false;
2031 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
2032 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
2033 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
2034 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
2035 Mnemonic == "dsb" || Mnemonic == "movs" || Mnemonic == "isb" ||
2036 Mnemonic == "clrex" || Mnemonic.startswith("cps")) {
2037 CanAcceptPredicationCode = false;
2039 CanAcceptPredicationCode = true;
2043 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
2044 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
2045 CanAcceptPredicationCode = false;
2048 /// Parse an arm instruction mnemonic followed by its operands.
2049 bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
2050 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2051 // Create the leading tokens for the mnemonic, split by '.' characters.
2052 size_t Start = 0, Next = Name.find('.');
2053 StringRef Head = Name.slice(Start, Next);
2055 // Split out the predication code and carry setting flag from the mnemonic.
2056 unsigned PredicationCode;
2057 unsigned ProcessorIMod;
2059 Head = SplitMnemonic(Head, PredicationCode, CarrySetting,
2062 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
2064 // Next, add the CCOut and ConditionCode operands, if needed.
2066 // For mnemonics which can ever incorporate a carry setting bit or predication
2067 // code, our matching model involves us always generating CCOut and
2068 // ConditionCode operands to match the mnemonic "as written" and then we let
2069 // the matcher deal with finding the right instruction or generating an
2070 // appropriate error.
2071 bool CanAcceptCarrySet, CanAcceptPredicationCode;
2072 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
2074 // If we had a carry-set on an instruction that can't do that, issue an
2076 if (!CanAcceptCarrySet && CarrySetting) {
2077 Parser.EatToEndOfStatement();
2078 return Error(NameLoc, "instruction '" + Head +
2079 "' can not set flags, but 's' suffix specified");
2082 // Add the carry setting operand, if necessary.
2084 // FIXME: It would be awesome if we could somehow invent a location such that
2085 // match errors on this operand would print a nice diagnostic about how the
2086 // 's' character in the mnemonic resulted in a CCOut operand.
2087 if (CanAcceptCarrySet)
2088 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
2091 // Add the predication code operand, if necessary.
2092 if (CanAcceptPredicationCode) {
2093 Operands.push_back(ARMOperand::CreateCondCode(
2094 ARMCC::CondCodes(PredicationCode), NameLoc));
2096 // This mnemonic can't ever accept a predication code, but the user wrote
2097 // one (or misspelled another mnemonic).
2099 // FIXME: Issue a nice error.
2102 // Add the processor imod operand, if necessary.
2103 if (ProcessorIMod) {
2104 Operands.push_back(ARMOperand::CreateImm(
2105 MCConstantExpr::Create(ProcessorIMod, getContext()),
2108 // This mnemonic can't ever accept a imod, but the user wrote
2109 // one (or misspelled another mnemonic).
2111 // FIXME: Issue a nice error.
2114 // Add the remaining tokens in the mnemonic.
2115 while (Next != StringRef::npos) {
2117 Next = Name.find('.', Start + 1);
2118 StringRef ExtraToken = Name.slice(Start, Next);
2120 Operands.push_back(ARMOperand::CreateToken(ExtraToken, NameLoc));
2123 // Read the remaining operands.
2124 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2125 // Read the first operand.
2126 if (ParseOperand(Operands, Head)) {
2127 Parser.EatToEndOfStatement();
2131 while (getLexer().is(AsmToken::Comma)) {
2132 Parser.Lex(); // Eat the comma.
2134 // Parse and remember the operand.
2135 if (ParseOperand(Operands, Head)) {
2136 Parser.EatToEndOfStatement();
2142 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2143 Parser.EatToEndOfStatement();
2144 return TokError("unexpected token in argument list");
2147 Parser.Lex(); // Consume the EndOfStatement
2152 MatchAndEmitInstruction(SMLoc IDLoc,
2153 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
2157 MatchResultTy MatchResult, MatchResult2;
2158 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2159 if (MatchResult != Match_Success) {
2160 // If we get a Match_InvalidOperand it might be some arithmetic instruction
2161 // that does not update the condition codes. So try adding a CCOut operand
2162 // with a value of reg0.
2163 if (MatchResult == Match_InvalidOperand) {
2164 Operands.insert(Operands.begin() + 1,
2165 ARMOperand::CreateCCOut(0,
2166 ((ARMOperand*)Operands[0])->getStartLoc()));
2167 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2168 if (MatchResult2 == Match_Success)
2169 MatchResult = Match_Success;
2171 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
2172 Operands.erase(Operands.begin() + 1);
2176 // If we get a Match_MnemonicFail it might be some arithmetic instruction
2177 // that updates the condition codes if it ends in 's'. So see if the
2178 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
2179 // operand with a value of CPSR.
2180 else if (MatchResult == Match_MnemonicFail) {
2181 // Get the instruction mnemonic, which is the first token.
2182 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
2183 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
2184 // removed the 's' from the mnemonic for matching.
2185 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
2186 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
2187 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
2188 Operands.erase(Operands.begin());
2190 Operands.insert(Operands.begin(),
2191 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
2192 Operands.insert(Operands.begin() + 1,
2193 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
2194 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2195 if (MatchResult2 == Match_Success)
2196 MatchResult = Match_Success;
2198 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
2199 Operands.erase(Operands.begin());
2201 Operands.insert(Operands.begin(),
2202 ARMOperand::CreateToken(Mnemonic, NameLoc));
2203 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
2204 Operands.erase(Operands.begin() + 1);
2210 switch (MatchResult) {
2212 Out.EmitInstruction(Inst);
2214 case Match_MissingFeature:
2215 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
2217 case Match_InvalidOperand: {
2218 SMLoc ErrorLoc = IDLoc;
2219 if (ErrorInfo != ~0U) {
2220 if (ErrorInfo >= Operands.size())
2221 return Error(IDLoc, "too few operands for instruction");
2223 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
2224 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
2227 return Error(ErrorLoc, "invalid operand for instruction");
2229 case Match_MnemonicFail:
2230 return Error(IDLoc, "unrecognized instruction mnemonic");
2231 case Match_ConversionFail:
2232 return Error(IDLoc, "unable to convert operands to instruction");
2235 llvm_unreachable("Implement any new match types added!");
2239 /// ParseDirective parses the arm specific directives
2240 bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
2241 StringRef IDVal = DirectiveID.getIdentifier();
2242 if (IDVal == ".word")
2243 return ParseDirectiveWord(4, DirectiveID.getLoc());
2244 else if (IDVal == ".thumb")
2245 return ParseDirectiveThumb(DirectiveID.getLoc());
2246 else if (IDVal == ".thumb_func")
2247 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
2248 else if (IDVal == ".code")
2249 return ParseDirectiveCode(DirectiveID.getLoc());
2250 else if (IDVal == ".syntax")
2251 return ParseDirectiveSyntax(DirectiveID.getLoc());
2255 /// ParseDirectiveWord
2256 /// ::= .word [ expression (, expression)* ]
2257 bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
2258 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2260 const MCExpr *Value;
2261 if (getParser().ParseExpression(Value))
2264 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
2266 if (getLexer().is(AsmToken::EndOfStatement))
2269 // FIXME: Improve diagnostic.
2270 if (getLexer().isNot(AsmToken::Comma))
2271 return Error(L, "unexpected token in directive");
2280 /// ParseDirectiveThumb
2282 bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
2283 if (getLexer().isNot(AsmToken::EndOfStatement))
2284 return Error(L, "unexpected token in directive");
2287 // TODO: set thumb mode
2288 // TODO: tell the MC streamer the mode
2289 // getParser().getStreamer().Emit???();
2293 /// ParseDirectiveThumbFunc
2294 /// ::= .thumbfunc symbol_name
2295 bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
2296 const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
2297 bool isMachO = MAI.hasSubsectionsViaSymbols();
2300 // Darwin asm has function name after .thumb_func direction
2303 const AsmToken &Tok = Parser.getTok();
2304 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
2305 return Error(L, "unexpected token in .thumb_func directive");
2306 Name = Tok.getString();
2307 Parser.Lex(); // Consume the identifier token.
2310 if (getLexer().isNot(AsmToken::EndOfStatement))
2311 return Error(L, "unexpected token in directive");
2314 // FIXME: assuming function name will be the line following .thumb_func
2316 Name = Parser.getTok().getString();
2319 // Mark symbol as a thumb symbol.
2320 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
2321 getParser().getStreamer().EmitThumbFunc(Func);
2325 /// ParseDirectiveSyntax
2326 /// ::= .syntax unified | divided
2327 bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
2328 const AsmToken &Tok = Parser.getTok();
2329 if (Tok.isNot(AsmToken::Identifier))
2330 return Error(L, "unexpected token in .syntax directive");
2331 StringRef Mode = Tok.getString();
2332 if (Mode == "unified" || Mode == "UNIFIED")
2334 else if (Mode == "divided" || Mode == "DIVIDED")
2335 return Error(L, "'.syntax divided' arm asssembly not supported");
2337 return Error(L, "unrecognized syntax mode in .syntax directive");
2339 if (getLexer().isNot(AsmToken::EndOfStatement))
2340 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
2343 // TODO tell the MC streamer the mode
2344 // getParser().getStreamer().Emit???();
2348 /// ParseDirectiveCode
2349 /// ::= .code 16 | 32
2350 bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
2351 const AsmToken &Tok = Parser.getTok();
2352 if (Tok.isNot(AsmToken::Integer))
2353 return Error(L, "unexpected token in .code directive");
2354 int64_t Val = Parser.getTok().getIntVal();
2360 return Error(L, "invalid operand to .code directive");
2362 if (getLexer().isNot(AsmToken::EndOfStatement))
2363 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
2369 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
2373 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
2379 extern "C" void LLVMInitializeARMAsmLexer();
2381 /// Force static initialization.
2382 extern "C" void LLVMInitializeARMAsmParser() {
2383 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
2384 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
2385 LLVMInitializeARMAsmLexer();
2388 #define GET_REGISTER_MATCHER
2389 #define GET_MATCHER_IMPLEMENTATION
2390 #include "ARMGenAsmMatcher.inc"