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/MCContext.h"
19 #include "llvm/MC/MCStreamer.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/MC/MCInst.h"
22 #include "llvm/Target/TargetRegistry.h"
23 #include "llvm/Target/TargetAsmParser.h"
24 #include "llvm/Support/SourceMgr.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/ADT/StringExtras.h"
28 #include "llvm/ADT/StringSwitch.h"
29 #include "llvm/ADT/Twine.h"
32 /// Shift types used for register controlled shifts in ARM memory addressing.
45 class ARMAsmParser : public TargetAsmParser {
49 MCAsmParser &getParser() const { return Parser; }
50 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
52 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
53 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
55 int TryParseRegister();
56 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
57 bool TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
58 bool ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
59 bool ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &);
60 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, StringRef Mnemonic);
61 bool ParsePrefix(ARMMCExpr::VariantKind &RefKind);
62 const MCExpr *ApplyPrefixToExpr(const MCExpr *E,
63 MCSymbolRefExpr::VariantKind Variant);
66 bool ParseMemoryOffsetReg(bool &Negative,
67 bool &OffsetRegShifted,
68 enum ShiftType &ShiftType,
69 const MCExpr *&ShiftAmount,
70 const MCExpr *&Offset,
74 bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount, SMLoc &E);
75 bool ParseDirectiveWord(unsigned Size, SMLoc L);
76 bool ParseDirectiveThumb(SMLoc L);
77 bool ParseDirectiveThumbFunc(SMLoc L);
78 bool ParseDirectiveCode(SMLoc L);
79 bool ParseDirectiveSyntax(SMLoc L);
81 bool MatchAndEmitInstruction(SMLoc IDLoc,
82 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
84 void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
85 bool &CanAcceptPredicationCode);
87 /// @name Auto-generated Match Functions
90 #define GET_ASSEMBLER_HEADER
91 #include "ARMGenAsmMatcher.inc"
95 OperandMatchResultTy tryParseCoprocNumOperand(
96 SmallVectorImpl<MCParsedAsmOperand*>&);
97 OperandMatchResultTy tryParseCoprocRegOperand(
98 SmallVectorImpl<MCParsedAsmOperand*>&);
99 OperandMatchResultTy tryParseMemBarrierOptOperand(
100 SmallVectorImpl<MCParsedAsmOperand*> &);
101 OperandMatchResultTy tryParseProcIFlagsOperand(
102 SmallVectorImpl<MCParsedAsmOperand*> &);
105 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
106 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {
107 // Initialize the set of available features.
108 setAvailableFeatures(ComputeAvailableFeatures(
109 &TM.getSubtarget<ARMSubtarget>()));
112 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
113 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
114 virtual bool ParseDirective(AsmToken DirectiveID);
116 } // end anonymous namespace
120 /// ARMOperand - Instances of this class represent a parsed ARM machine
122 class ARMOperand : public MCParsedAsmOperand {
139 SMLoc StartLoc, EndLoc;
140 SmallVector<unsigned, 8> Registers;
144 ARMCC::CondCodes Val;
156 ARM_PROC::IFlags Val;
172 /// Combined record for all forms of ARM address expressions.
176 unsigned RegNum; ///< Offset register num, when OffsetIsReg.
177 const MCExpr *Value; ///< Offset value, when !OffsetIsReg.
179 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
180 enum ShiftType ShiftType; // used when OffsetRegShifted is true
181 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
182 unsigned Preindexed : 1;
183 unsigned Postindexed : 1;
184 unsigned OffsetIsReg : 1;
185 unsigned Negative : 1; // only used when OffsetIsReg is true
186 unsigned Writeback : 1;
190 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
192 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
194 StartLoc = o.StartLoc;
208 case DPRRegisterList:
209 case SPRRegisterList:
210 Registers = o.Registers;
230 /// getStartLoc - Get the location of the first token of this operand.
231 SMLoc getStartLoc() const { return StartLoc; }
232 /// getEndLoc - Get the location of the last token of this operand.
233 SMLoc getEndLoc() const { return EndLoc; }
235 ARMCC::CondCodes getCondCode() const {
236 assert(Kind == CondCode && "Invalid access!");
240 unsigned getCoproc() const {
241 assert((Kind == CoprocNum || Kind == CoprocReg) && "Invalid access!");
245 StringRef getToken() const {
246 assert(Kind == Token && "Invalid access!");
247 return StringRef(Tok.Data, Tok.Length);
250 unsigned getReg() const {
251 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
255 const SmallVectorImpl<unsigned> &getRegList() const {
256 assert((Kind == RegisterList || Kind == DPRRegisterList ||
257 Kind == SPRRegisterList) && "Invalid access!");
261 const MCExpr *getImm() const {
262 assert(Kind == Immediate && "Invalid access!");
266 ARM_MB::MemBOpt getMemBarrierOpt() const {
267 assert(Kind == MemBarrierOpt && "Invalid access!");
271 ARM_PROC::IFlags getProcIFlags() const {
272 assert(Kind == ProcIFlags && "Invalid access!");
276 /// @name Memory Operand Accessors
279 unsigned getMemBaseRegNum() const {
280 return Mem.BaseRegNum;
282 unsigned getMemOffsetRegNum() const {
283 assert(Mem.OffsetIsReg && "Invalid access!");
284 return Mem.Offset.RegNum;
286 const MCExpr *getMemOffset() const {
287 assert(!Mem.OffsetIsReg && "Invalid access!");
288 return Mem.Offset.Value;
290 unsigned getMemOffsetRegShifted() const {
291 assert(Mem.OffsetIsReg && "Invalid access!");
292 return Mem.OffsetRegShifted;
294 const MCExpr *getMemShiftAmount() const {
295 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
296 return Mem.ShiftAmount;
298 enum ShiftType getMemShiftType() const {
299 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
300 return Mem.ShiftType;
302 bool getMemPreindexed() const { return Mem.Preindexed; }
303 bool getMemPostindexed() const { return Mem.Postindexed; }
304 bool getMemOffsetIsReg() const { return Mem.OffsetIsReg; }
305 bool getMemNegative() const { return Mem.Negative; }
306 bool getMemWriteback() const { return Mem.Writeback; }
310 bool isCoprocNum() const { return Kind == CoprocNum; }
311 bool isCoprocReg() const { return Kind == CoprocReg; }
312 bool isCondCode() const { return Kind == CondCode; }
313 bool isCCOut() const { return Kind == CCOut; }
314 bool isImm() const { return Kind == Immediate; }
315 bool isReg() const { return Kind == Register; }
316 bool isRegList() const { return Kind == RegisterList; }
317 bool isDPRRegList() const { return Kind == DPRRegisterList; }
318 bool isSPRRegList() const { return Kind == SPRRegisterList; }
319 bool isToken() const { return Kind == Token; }
320 bool isMemBarrierOpt() const { return Kind == MemBarrierOpt; }
321 bool isMemory() const { return Kind == Memory; }
322 bool isMemMode5() const {
323 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
327 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
328 if (!CE) return false;
330 // The offset must be a multiple of 4 in the range 0-1020.
331 int64_t Value = CE->getValue();
332 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
334 bool isMemModeRegThumb() const {
335 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
339 bool isMemModeImmThumb() const {
340 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
343 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
344 if (!CE) return false;
346 // The offset must be a multiple of 4 in the range 0-124.
347 uint64_t Value = CE->getValue();
348 return ((Value & 0x3) == 0 && Value <= 124);
350 bool isProcIFlags() const { return Kind == ProcIFlags; }
352 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
353 // Add as immediates when possible. Null MCExpr = 0.
355 Inst.addOperand(MCOperand::CreateImm(0));
356 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
357 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
359 Inst.addOperand(MCOperand::CreateExpr(Expr));
362 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
363 assert(N == 2 && "Invalid number of operands!");
364 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
365 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
366 Inst.addOperand(MCOperand::CreateReg(RegNum));
369 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
370 assert(N == 1 && "Invalid number of operands!");
371 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
374 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
375 assert(N == 1 && "Invalid number of operands!");
376 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
379 void addCCOutOperands(MCInst &Inst, unsigned N) const {
380 assert(N == 1 && "Invalid number of operands!");
381 Inst.addOperand(MCOperand::CreateReg(getReg()));
384 void addRegOperands(MCInst &Inst, unsigned N) const {
385 assert(N == 1 && "Invalid number of operands!");
386 Inst.addOperand(MCOperand::CreateReg(getReg()));
389 void addRegListOperands(MCInst &Inst, unsigned N) const {
390 assert(N == 1 && "Invalid number of operands!");
391 const SmallVectorImpl<unsigned> &RegList = getRegList();
392 for (SmallVectorImpl<unsigned>::const_iterator
393 I = RegList.begin(), E = RegList.end(); I != E; ++I)
394 Inst.addOperand(MCOperand::CreateReg(*I));
397 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
398 addRegListOperands(Inst, N);
401 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
402 addRegListOperands(Inst, N);
405 void addImmOperands(MCInst &Inst, unsigned N) const {
406 assert(N == 1 && "Invalid number of operands!");
407 addExpr(Inst, getImm());
410 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
411 assert(N == 1 && "Invalid number of operands!");
412 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
415 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
416 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
418 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
419 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
421 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
423 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
424 assert(CE && "Non-constant mode 5 offset operand!");
426 // The MCInst offset operand doesn't include the low two bits (like
427 // the instruction encoding).
428 int64_t Offset = CE->getValue() / 4;
430 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
433 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
437 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
438 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
439 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
440 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
443 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
444 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
445 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
446 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
447 assert(CE && "Non-constant mode offset operand!");
448 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
451 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
452 assert(N == 1 && "Invalid number of operands!");
453 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
456 virtual void dump(raw_ostream &OS) const;
458 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
459 ARMOperand *Op = new ARMOperand(CondCode);
466 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
467 ARMOperand *Op = new ARMOperand(CoprocNum);
468 Op->Cop.Val = CopVal;
474 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
475 ARMOperand *Op = new ARMOperand(CoprocReg);
476 Op->Cop.Val = CopVal;
482 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
483 ARMOperand *Op = new ARMOperand(CCOut);
484 Op->Reg.RegNum = RegNum;
490 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
491 ARMOperand *Op = new ARMOperand(Token);
492 Op->Tok.Data = Str.data();
493 Op->Tok.Length = Str.size();
499 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
500 ARMOperand *Op = new ARMOperand(Register);
501 Op->Reg.RegNum = RegNum;
508 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
509 SMLoc StartLoc, SMLoc EndLoc) {
510 KindTy Kind = RegisterList;
512 if (ARM::DPRRegClass.contains(Regs.front().first))
513 Kind = DPRRegisterList;
514 else if (ARM::SPRRegClass.contains(Regs.front().first))
515 Kind = SPRRegisterList;
517 ARMOperand *Op = new ARMOperand(Kind);
518 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
519 I = Regs.begin(), E = Regs.end(); I != E; ++I)
520 Op->Registers.push_back(I->first);
521 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
522 Op->StartLoc = StartLoc;
527 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
528 ARMOperand *Op = new ARMOperand(Immediate);
535 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
536 const MCExpr *Offset, int OffsetRegNum,
537 bool OffsetRegShifted, enum ShiftType ShiftType,
538 const MCExpr *ShiftAmount, bool Preindexed,
539 bool Postindexed, bool Negative, bool Writeback,
541 assert((OffsetRegNum == -1 || OffsetIsReg) &&
542 "OffsetRegNum must imply OffsetIsReg!");
543 assert((!OffsetRegShifted || OffsetIsReg) &&
544 "OffsetRegShifted must imply OffsetIsReg!");
545 assert((Offset || OffsetIsReg) &&
546 "Offset must exists unless register offset is used!");
547 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
548 "Cannot have shift amount without shifted register offset!");
549 assert((!Offset || !OffsetIsReg) &&
550 "Cannot have expression offset and register offset!");
552 ARMOperand *Op = new ARMOperand(Memory);
553 Op->Mem.BaseRegNum = BaseRegNum;
554 Op->Mem.OffsetIsReg = OffsetIsReg;
556 Op->Mem.Offset.RegNum = OffsetRegNum;
558 Op->Mem.Offset.Value = Offset;
559 Op->Mem.OffsetRegShifted = OffsetRegShifted;
560 Op->Mem.ShiftType = ShiftType;
561 Op->Mem.ShiftAmount = ShiftAmount;
562 Op->Mem.Preindexed = Preindexed;
563 Op->Mem.Postindexed = Postindexed;
564 Op->Mem.Negative = Negative;
565 Op->Mem.Writeback = Writeback;
572 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
573 ARMOperand *Op = new ARMOperand(MemBarrierOpt);
580 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
581 ARMOperand *Op = new ARMOperand(ProcIFlags);
582 Op->IFlags.Val = IFlags;
589 } // end anonymous namespace.
591 void ARMOperand::dump(raw_ostream &OS) const {
594 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
597 OS << "<ccout " << getReg() << ">";
600 OS << "<coprocessor number: " << getCoproc() << ">";
603 OS << "<coprocessor register: " << getCoproc() << ">";
609 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
613 << "base:" << getMemBaseRegNum();
614 if (getMemOffsetIsReg()) {
615 OS << " offset:<register " << getMemOffsetRegNum();
616 if (getMemOffsetRegShifted()) {
617 OS << " offset-shift-type:" << getMemShiftType();
618 OS << " offset-shift-amount:" << *getMemShiftAmount();
621 OS << " offset:" << *getMemOffset();
623 if (getMemOffsetIsReg())
624 OS << " (offset-is-reg)";
625 if (getMemPreindexed())
626 OS << " (pre-indexed)";
627 if (getMemPostindexed())
628 OS << " (post-indexed)";
629 if (getMemNegative())
631 if (getMemWriteback())
632 OS << " (writeback)";
637 unsigned IFlags = getProcIFlags();
638 for (int i=2; i >= 0; --i)
639 if (IFlags & (1 << i))
640 OS << ARM_PROC::IFlagsToString(1 << i);
645 OS << "<register " << getReg() << ">";
648 case DPRRegisterList:
649 case SPRRegisterList: {
650 OS << "<register_list ";
652 const SmallVectorImpl<unsigned> &RegList = getRegList();
653 for (SmallVectorImpl<unsigned>::const_iterator
654 I = RegList.begin(), E = RegList.end(); I != E; ) {
656 if (++I < E) OS << ", ";
663 OS << "'" << getToken() << "'";
668 /// @name Auto-generated Match Functions
671 static unsigned MatchRegisterName(StringRef Name);
675 bool ARMAsmParser::ParseRegister(unsigned &RegNo,
676 SMLoc &StartLoc, SMLoc &EndLoc) {
677 RegNo = TryParseRegister();
679 return (RegNo == (unsigned)-1);
682 /// Try to parse a register name. The token must be an Identifier when called,
683 /// and if it is a register name the token is eaten and the register number is
684 /// returned. Otherwise return -1.
686 int ARMAsmParser::TryParseRegister() {
687 const AsmToken &Tok = Parser.getTok();
688 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
690 // FIXME: Validate register for the current architecture; we have to do
691 // validation later, so maybe there is no need for this here.
692 std::string upperCase = Tok.getString().str();
693 std::string lowerCase = LowercaseString(upperCase);
694 unsigned RegNum = MatchRegisterName(lowerCase);
696 RegNum = StringSwitch<unsigned>(lowerCase)
697 .Case("r13", ARM::SP)
698 .Case("r14", ARM::LR)
699 .Case("r15", ARM::PC)
700 .Case("ip", ARM::R12)
703 if (!RegNum) return -1;
705 Parser.Lex(); // Eat identifier token.
709 /// Try to parse a register name. The token must be an Identifier when called.
710 /// If it's a register, an AsmOperand is created. Another AsmOperand is created
711 /// if there is a "writeback". 'true' if it's not a register.
713 /// TODO this is likely to change to allow different register types and or to
714 /// parse for a specific register type.
716 TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
717 SMLoc S = Parser.getTok().getLoc();
718 int RegNo = TryParseRegister();
722 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
724 const AsmToken &ExclaimTok = Parser.getTok();
725 if (ExclaimTok.is(AsmToken::Exclaim)) {
726 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
727 ExclaimTok.getLoc()));
728 Parser.Lex(); // Eat exclaim token
734 /// MatchCoprocessorOperandName - Try to parse an coprocessor related
735 /// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
737 static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
738 // Use the same layout as the tablegen'erated register name matcher. Ugly,
740 switch (Name.size()) {
743 if (Name[0] != CoprocOp)
760 if (Name[0] != CoprocOp || Name[1] != '1')
774 llvm_unreachable("Unhandled coprocessor operand string!");
778 /// tryParseCoprocNumOperand - Try to parse an coprocessor number operand. The
779 /// token must be an Identifier when called, and if it is a coprocessor
780 /// number, the token is eaten and the operand is added to the operand list.
781 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
782 tryParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
783 SMLoc S = Parser.getTok().getLoc();
784 const AsmToken &Tok = Parser.getTok();
785 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
787 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
789 return MatchOperand_NoMatch;
791 Parser.Lex(); // Eat identifier token.
792 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
793 return MatchOperand_Success;
796 /// tryParseCoprocRegOperand - Try to parse an coprocessor register operand. The
797 /// token must be an Identifier when called, and if it is a coprocessor
798 /// number, the token is eaten and the operand is added to the operand list.
799 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
800 tryParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
801 SMLoc S = Parser.getTok().getLoc();
802 const AsmToken &Tok = Parser.getTok();
803 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
805 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
807 return MatchOperand_NoMatch;
809 Parser.Lex(); // Eat identifier token.
810 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
811 return MatchOperand_Success;
814 /// Parse a register list, return it if successful else return null. The first
815 /// token must be a '{' when called.
817 ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
818 assert(Parser.getTok().is(AsmToken::LCurly) &&
819 "Token is not a Left Curly Brace");
820 SMLoc S = Parser.getTok().getLoc();
822 // Read the rest of the registers in the list.
823 unsigned PrevRegNum = 0;
824 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
827 bool IsRange = Parser.getTok().is(AsmToken::Minus);
828 Parser.Lex(); // Eat non-identifier token.
830 const AsmToken &RegTok = Parser.getTok();
831 SMLoc RegLoc = RegTok.getLoc();
832 if (RegTok.isNot(AsmToken::Identifier)) {
833 Error(RegLoc, "register expected");
837 int RegNum = TryParseRegister();
839 Error(RegLoc, "register expected");
844 int Reg = PrevRegNum;
847 Registers.push_back(std::make_pair(Reg, RegLoc));
848 } while (Reg != RegNum);
850 Registers.push_back(std::make_pair(RegNum, RegLoc));
854 } while (Parser.getTok().is(AsmToken::Comma) ||
855 Parser.getTok().is(AsmToken::Minus));
857 // Process the right curly brace of the list.
858 const AsmToken &RCurlyTok = Parser.getTok();
859 if (RCurlyTok.isNot(AsmToken::RCurly)) {
860 Error(RCurlyTok.getLoc(), "'}' expected");
864 SMLoc E = RCurlyTok.getLoc();
865 Parser.Lex(); // Eat right curly brace token.
867 // Verify the register list.
868 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
869 RI = Registers.begin(), RE = Registers.end();
871 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
872 bool EmittedWarning = false;
874 DenseMap<unsigned, bool> RegMap;
875 RegMap[HighRegNum] = true;
877 for (++RI; RI != RE; ++RI) {
878 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
879 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
882 Error(RegInfo.second, "register duplicated in register list");
886 if (!EmittedWarning && Reg < HighRegNum)
887 Warning(RegInfo.second,
888 "register not in ascending order in register list");
891 HighRegNum = std::max(Reg, HighRegNum);
894 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
898 /// tryParseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
899 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
900 tryParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
901 SMLoc S = Parser.getTok().getLoc();
902 const AsmToken &Tok = Parser.getTok();
903 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
904 StringRef OptStr = Tok.getString();
906 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
907 .Case("sy", ARM_MB::SY)
908 .Case("st", ARM_MB::ST)
909 .Case("ish", ARM_MB::ISH)
910 .Case("ishst", ARM_MB::ISHST)
911 .Case("nsh", ARM_MB::NSH)
912 .Case("nshst", ARM_MB::NSHST)
913 .Case("osh", ARM_MB::OSH)
914 .Case("oshst", ARM_MB::OSHST)
918 return MatchOperand_NoMatch;
920 Parser.Lex(); // Eat identifier token.
921 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
922 return MatchOperand_Success;
925 /// ParseProcIFlagsOperand - Try to parse iflags from CPS instruction.
926 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
927 tryParseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
928 SMLoc S = Parser.getTok().getLoc();
929 const AsmToken &Tok = Parser.getTok();
930 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
931 StringRef IFlagsStr = Tok.getString();
934 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
935 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
936 .Case("a", ARM_PROC::A)
937 .Case("i", ARM_PROC::I)
938 .Case("f", ARM_PROC::F)
941 // If some specific iflag is already set, it means that some letter is
942 // present more than once, this is not acceptable.
943 if (Flag == ~0U || (IFlags & Flag))
944 return MatchOperand_NoMatch;
949 Parser.Lex(); // Eat identifier token.
950 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
951 return MatchOperand_Success;
954 /// Parse an ARM memory expression, return false if successful else return true
955 /// or an error. The first token must be a '[' when called.
957 /// TODO Only preindexing and postindexing addressing are started, unindexed
958 /// with option, etc are still to do.
960 ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
962 assert(Parser.getTok().is(AsmToken::LBrac) &&
963 "Token is not a Left Bracket");
964 S = Parser.getTok().getLoc();
965 Parser.Lex(); // Eat left bracket token.
967 const AsmToken &BaseRegTok = Parser.getTok();
968 if (BaseRegTok.isNot(AsmToken::Identifier)) {
969 Error(BaseRegTok.getLoc(), "register expected");
972 int BaseRegNum = TryParseRegister();
973 if (BaseRegNum == -1) {
974 Error(BaseRegTok.getLoc(), "register expected");
978 // The next token must either be a comma or a closing bracket.
979 const AsmToken &Tok = Parser.getTok();
980 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
983 bool Preindexed = false;
984 bool Postindexed = false;
985 bool OffsetIsReg = false;
986 bool Negative = false;
987 bool Writeback = false;
988 ARMOperand *WBOp = 0;
989 int OffsetRegNum = -1;
990 bool OffsetRegShifted = false;
991 enum ShiftType ShiftType = Lsl;
992 const MCExpr *ShiftAmount = 0;
993 const MCExpr *Offset = 0;
995 // First look for preindexed address forms, that is after the "[Rn" we now
996 // have to see if the next token is a comma.
997 if (Tok.is(AsmToken::Comma)) {
999 Parser.Lex(); // Eat comma token.
1001 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
1002 Offset, OffsetIsReg, OffsetRegNum, E))
1004 const AsmToken &RBracTok = Parser.getTok();
1005 if (RBracTok.isNot(AsmToken::RBrac)) {
1006 Error(RBracTok.getLoc(), "']' expected");
1009 E = RBracTok.getLoc();
1010 Parser.Lex(); // Eat right bracket token.
1012 const AsmToken &ExclaimTok = Parser.getTok();
1013 if (ExclaimTok.is(AsmToken::Exclaim)) {
1014 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
1015 ExclaimTok.getLoc());
1017 Parser.Lex(); // Eat exclaim token
1020 // The "[Rn" we have so far was not followed by a comma.
1022 // If there's anything other than the right brace, this is a post indexing
1025 Parser.Lex(); // Eat right bracket token.
1027 const AsmToken &NextTok = Parser.getTok();
1029 if (NextTok.isNot(AsmToken::EndOfStatement)) {
1033 if (NextTok.isNot(AsmToken::Comma)) {
1034 Error(NextTok.getLoc(), "',' expected");
1038 Parser.Lex(); // Eat comma token.
1040 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
1041 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
1047 // Force Offset to exist if used.
1050 Offset = MCConstantExpr::Create(0, getContext());
1053 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
1054 OffsetRegNum, OffsetRegShifted,
1055 ShiftType, ShiftAmount, Preindexed,
1056 Postindexed, Negative, Writeback,
1059 Operands.push_back(WBOp);
1064 /// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
1065 /// we will parse the following (were +/- means that a plus or minus is
1070 /// we return false on success or an error otherwise.
1071 bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
1072 bool &OffsetRegShifted,
1073 enum ShiftType &ShiftType,
1074 const MCExpr *&ShiftAmount,
1075 const MCExpr *&Offset,
1080 OffsetRegShifted = false;
1081 OffsetIsReg = false;
1083 const AsmToken &NextTok = Parser.getTok();
1084 E = NextTok.getLoc();
1085 if (NextTok.is(AsmToken::Plus))
1086 Parser.Lex(); // Eat plus token.
1087 else if (NextTok.is(AsmToken::Minus)) {
1089 Parser.Lex(); // Eat minus token
1091 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
1092 const AsmToken &OffsetRegTok = Parser.getTok();
1093 if (OffsetRegTok.is(AsmToken::Identifier)) {
1094 SMLoc CurLoc = OffsetRegTok.getLoc();
1095 OffsetRegNum = TryParseRegister();
1096 if (OffsetRegNum != -1) {
1102 // If we parsed a register as the offset then there can be a shift after that.
1103 if (OffsetRegNum != -1) {
1104 // Look for a comma then a shift
1105 const AsmToken &Tok = Parser.getTok();
1106 if (Tok.is(AsmToken::Comma)) {
1107 Parser.Lex(); // Eat comma token.
1109 const AsmToken &Tok = Parser.getTok();
1110 if (ParseShift(ShiftType, ShiftAmount, E))
1111 return Error(Tok.getLoc(), "shift expected");
1112 OffsetRegShifted = true;
1115 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1116 // Look for #offset following the "[Rn," or "[Rn],"
1117 const AsmToken &HashTok = Parser.getTok();
1118 if (HashTok.isNot(AsmToken::Hash))
1119 return Error(HashTok.getLoc(), "'#' expected");
1121 Parser.Lex(); // Eat hash token.
1123 if (getParser().ParseExpression(Offset))
1125 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1130 /// ParseShift as one of these two:
1131 /// ( lsl | lsr | asr | ror ) , # shift_amount
1133 /// and returns true if it parses a shift otherwise it returns false.
1134 bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
1136 const AsmToken &Tok = Parser.getTok();
1137 if (Tok.isNot(AsmToken::Identifier))
1139 StringRef ShiftName = Tok.getString();
1140 if (ShiftName == "lsl" || ShiftName == "LSL")
1142 else if (ShiftName == "lsr" || ShiftName == "LSR")
1144 else if (ShiftName == "asr" || ShiftName == "ASR")
1146 else if (ShiftName == "ror" || ShiftName == "ROR")
1148 else if (ShiftName == "rrx" || ShiftName == "RRX")
1152 Parser.Lex(); // Eat shift type token.
1154 // Rrx stands alone.
1158 // Otherwise, there must be a '#' and a shift amount.
1159 const AsmToken &HashTok = Parser.getTok();
1160 if (HashTok.isNot(AsmToken::Hash))
1161 return Error(HashTok.getLoc(), "'#' expected");
1162 Parser.Lex(); // Eat hash token.
1164 if (getParser().ParseExpression(ShiftAmount))
1170 /// Parse a arm instruction operand. For now this parses the operand regardless
1171 /// of the mnemonic.
1172 bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1173 StringRef Mnemonic) {
1176 // Check if the current operand has a custom associated parser, if so, try to
1177 // custom parse the operand, or fallback to the general approach.
1178 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1179 if (ResTy == MatchOperand_Success)
1181 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1182 // there was a match, but an error occurred, in which case, just return that
1183 // the operand parsing failed.
1184 if (ResTy == MatchOperand_ParseFail)
1187 switch (getLexer().getKind()) {
1189 Error(Parser.getTok().getLoc(), "unexpected token in operand");
1191 case AsmToken::Identifier:
1192 if (!TryParseRegisterWithWriteBack(Operands))
1195 // Fall though for the Identifier case that is not a register or a
1197 case AsmToken::Integer: // things like 1f and 2b as a branch targets
1198 case AsmToken::Dot: { // . as a branch target
1199 // This was not a register so parse other operands that start with an
1200 // identifier (like labels) as expressions and create them as immediates.
1201 const MCExpr *IdVal;
1202 S = Parser.getTok().getLoc();
1203 if (getParser().ParseExpression(IdVal))
1205 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1206 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1209 case AsmToken::LBrac:
1210 return ParseMemory(Operands);
1211 case AsmToken::LCurly:
1212 return ParseRegisterList(Operands);
1213 case AsmToken::Hash:
1214 // #42 -> immediate.
1215 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
1216 S = Parser.getTok().getLoc();
1218 const MCExpr *ImmVal;
1219 if (getParser().ParseExpression(ImmVal))
1221 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1222 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1224 case AsmToken::Colon: {
1225 // ":lower16:" and ":upper16:" expression prefixes
1226 // FIXME: Check it's an expression prefix,
1227 // e.g. (FOO - :lower16:BAR) isn't legal.
1228 ARMMCExpr::VariantKind RefKind;
1229 if (ParsePrefix(RefKind))
1232 const MCExpr *SubExprVal;
1233 if (getParser().ParseExpression(SubExprVal))
1236 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1238 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1239 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
1245 // ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1246 // :lower16: and :upper16:.
1247 bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1248 RefKind = ARMMCExpr::VK_ARM_None;
1250 // :lower16: and :upper16: modifiers
1251 assert(getLexer().is(AsmToken::Colon) && "expected a :");
1252 Parser.Lex(); // Eat ':'
1254 if (getLexer().isNot(AsmToken::Identifier)) {
1255 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1259 StringRef IDVal = Parser.getTok().getIdentifier();
1260 if (IDVal == "lower16") {
1261 RefKind = ARMMCExpr::VK_ARM_LO16;
1262 } else if (IDVal == "upper16") {
1263 RefKind = ARMMCExpr::VK_ARM_HI16;
1265 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1270 if (getLexer().isNot(AsmToken::Colon)) {
1271 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1274 Parser.Lex(); // Eat the last ':'
1279 ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1280 MCSymbolRefExpr::VariantKind Variant) {
1281 // Recurse over the given expression, rebuilding it to apply the given variant
1282 // to the leftmost symbol.
1283 if (Variant == MCSymbolRefExpr::VK_None)
1286 switch (E->getKind()) {
1287 case MCExpr::Target:
1288 llvm_unreachable("Can't handle target expr yet");
1289 case MCExpr::Constant:
1290 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1292 case MCExpr::SymbolRef: {
1293 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1295 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1298 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1302 llvm_unreachable("Can't handle unary expressions yet");
1304 case MCExpr::Binary: {
1305 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1306 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1307 const MCExpr *RHS = BE->getRHS();
1311 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1315 assert(0 && "Invalid expression kind!");
1319 /// \brief Given a mnemonic, split out possible predication code and carry
1320 /// setting letters to form a canonical mnemonic and flags.
1322 // FIXME: Would be nice to autogen this.
1323 static StringRef SplitMnemonic(StringRef Mnemonic,
1324 unsigned &PredicationCode,
1326 unsigned &ProcessorIMod) {
1327 PredicationCode = ARMCC::AL;
1328 CarrySetting = false;
1331 // Ignore some mnemonics we know aren't predicated forms.
1333 // FIXME: Would be nice to autogen this.
1334 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1335 Mnemonic == "movs" ||
1336 Mnemonic == "svc" ||
1337 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1338 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1339 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1340 Mnemonic == "vclt" ||
1341 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1342 Mnemonic == "vcle" ||
1343 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1344 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
1345 Mnemonic == "vqdmlal"))
1348 // First, split out any predication code.
1349 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
1350 .Case("eq", ARMCC::EQ)
1351 .Case("ne", ARMCC::NE)
1352 .Case("hs", ARMCC::HS)
1353 .Case("lo", ARMCC::LO)
1354 .Case("mi", ARMCC::MI)
1355 .Case("pl", ARMCC::PL)
1356 .Case("vs", ARMCC::VS)
1357 .Case("vc", ARMCC::VC)
1358 .Case("hi", ARMCC::HI)
1359 .Case("ls", ARMCC::LS)
1360 .Case("ge", ARMCC::GE)
1361 .Case("lt", ARMCC::LT)
1362 .Case("gt", ARMCC::GT)
1363 .Case("le", ARMCC::LE)
1364 .Case("al", ARMCC::AL)
1367 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
1368 PredicationCode = CC;
1371 // Next, determine if we have a carry setting bit. We explicitly ignore all
1372 // the instructions we know end in 's'.
1373 if (Mnemonic.endswith("s") &&
1374 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1375 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1376 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1377 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1378 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1379 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1380 CarrySetting = true;
1383 // The "cps" instruction can have a interrupt mode operand which is glued into
1384 // the mnemonic. Check if this is the case, split it and parse the imod op
1385 if (Mnemonic.startswith("cps")) {
1386 // Split out any imod code.
1388 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
1389 .Case("ie", ARM_PROC::IE)
1390 .Case("id", ARM_PROC::ID)
1393 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
1394 ProcessorIMod = IMod;
1401 /// \brief Given a canonical mnemonic, determine if the instruction ever allows
1402 /// inclusion of carry set or predication code operands.
1404 // FIXME: It would be nice to autogen this.
1406 GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
1407 bool &CanAcceptPredicationCode) {
1408 bool isThumb = TM.getSubtarget<ARMSubtarget>().isThumb();
1410 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
1411 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
1412 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
1413 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
1414 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mov" ||
1415 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
1416 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
1417 Mnemonic == "eor" || Mnemonic == "smlal" || Mnemonic == "mvn") {
1418 CanAcceptCarrySet = true;
1420 CanAcceptCarrySet = false;
1423 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
1424 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
1425 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
1426 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
1427 Mnemonic == "dsb" || Mnemonic == "movs" || Mnemonic == "isb" ||
1428 Mnemonic == "clrex" || Mnemonic.startswith("cps")) {
1429 CanAcceptPredicationCode = false;
1431 CanAcceptPredicationCode = true;
1435 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
1436 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
1437 CanAcceptPredicationCode = false;
1440 /// Parse an arm instruction mnemonic followed by its operands.
1441 bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
1442 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1443 // Create the leading tokens for the mnemonic, split by '.' characters.
1444 size_t Start = 0, Next = Name.find('.');
1445 StringRef Head = Name.slice(Start, Next);
1447 // Split out the predication code and carry setting flag from the mnemonic.
1448 unsigned PredicationCode;
1449 unsigned ProcessorIMod;
1451 Head = SplitMnemonic(Head, PredicationCode, CarrySetting,
1454 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
1456 // Next, add the CCOut and ConditionCode operands, if needed.
1458 // For mnemonics which can ever incorporate a carry setting bit or predication
1459 // code, our matching model involves us always generating CCOut and
1460 // ConditionCode operands to match the mnemonic "as written" and then we let
1461 // the matcher deal with finding the right instruction or generating an
1462 // appropriate error.
1463 bool CanAcceptCarrySet, CanAcceptPredicationCode;
1464 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
1466 // Add the carry setting operand, if necessary.
1468 // FIXME: It would be awesome if we could somehow invent a location such that
1469 // match errors on this operand would print a nice diagnostic about how the
1470 // 's' character in the mnemonic resulted in a CCOut operand.
1471 if (CanAcceptCarrySet) {
1472 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
1475 // This mnemonic can't ever accept a carry set, but the user wrote one (or
1476 // misspelled another mnemonic).
1478 // FIXME: Issue a nice error.
1481 // Add the predication code operand, if necessary.
1482 if (CanAcceptPredicationCode) {
1483 Operands.push_back(ARMOperand::CreateCondCode(
1484 ARMCC::CondCodes(PredicationCode), NameLoc));
1486 // This mnemonic can't ever accept a predication code, but the user wrote
1487 // one (or misspelled another mnemonic).
1489 // FIXME: Issue a nice error.
1492 // Add the processor imod operand, if necessary.
1493 if (ProcessorIMod) {
1494 Operands.push_back(ARMOperand::CreateImm(
1495 MCConstantExpr::Create(ProcessorIMod, getContext()),
1498 // This mnemonic can't ever accept a imod, but the user wrote
1499 // one (or misspelled another mnemonic).
1501 // FIXME: Issue a nice error.
1504 // Add the remaining tokens in the mnemonic.
1505 while (Next != StringRef::npos) {
1507 Next = Name.find('.', Start + 1);
1508 StringRef ExtraToken = Name.slice(Start, Next);
1510 Operands.push_back(ARMOperand::CreateToken(ExtraToken, NameLoc));
1513 // Read the remaining operands.
1514 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1515 // Read the first operand.
1516 if (ParseOperand(Operands, Head)) {
1517 Parser.EatToEndOfStatement();
1521 while (getLexer().is(AsmToken::Comma)) {
1522 Parser.Lex(); // Eat the comma.
1524 // Parse and remember the operand.
1525 if (ParseOperand(Operands, Head)) {
1526 Parser.EatToEndOfStatement();
1532 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1533 Parser.EatToEndOfStatement();
1534 return TokError("unexpected token in argument list");
1537 Parser.Lex(); // Consume the EndOfStatement
1542 MatchAndEmitInstruction(SMLoc IDLoc,
1543 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1547 MatchResultTy MatchResult, MatchResult2;
1548 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1549 if (MatchResult != Match_Success) {
1550 // If we get a Match_InvalidOperand it might be some arithmetic instruction
1551 // that does not update the condition codes. So try adding a CCOut operand
1552 // with a value of reg0.
1553 if (MatchResult == Match_InvalidOperand) {
1554 Operands.insert(Operands.begin() + 1,
1555 ARMOperand::CreateCCOut(0,
1556 ((ARMOperand*)Operands[0])->getStartLoc()));
1557 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1558 if (MatchResult2 == Match_Success)
1559 MatchResult = Match_Success;
1561 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
1562 Operands.erase(Operands.begin() + 1);
1566 // If we get a Match_MnemonicFail it might be some arithmetic instruction
1567 // that updates the condition codes if it ends in 's'. So see if the
1568 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
1569 // operand with a value of CPSR.
1570 else if(MatchResult == Match_MnemonicFail) {
1571 // Get the instruction mnemonic, which is the first token.
1572 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
1573 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
1574 // removed the 's' from the mnemonic for matching.
1575 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
1576 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
1577 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1578 Operands.erase(Operands.begin());
1580 Operands.insert(Operands.begin(),
1581 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
1582 Operands.insert(Operands.begin() + 1,
1583 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
1584 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1585 if (MatchResult2 == Match_Success)
1586 MatchResult = Match_Success;
1588 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1589 Operands.erase(Operands.begin());
1591 Operands.insert(Operands.begin(),
1592 ARMOperand::CreateToken(Mnemonic, NameLoc));
1593 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
1594 Operands.erase(Operands.begin() + 1);
1600 switch (MatchResult) {
1602 Out.EmitInstruction(Inst);
1604 case Match_MissingFeature:
1605 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1607 case Match_InvalidOperand: {
1608 SMLoc ErrorLoc = IDLoc;
1609 if (ErrorInfo != ~0U) {
1610 if (ErrorInfo >= Operands.size())
1611 return Error(IDLoc, "too few operands for instruction");
1613 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
1614 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1617 return Error(ErrorLoc, "invalid operand for instruction");
1619 case Match_MnemonicFail:
1620 return Error(IDLoc, "unrecognized instruction mnemonic");
1621 case Match_ConversionFail:
1622 return Error(IDLoc, "unable to convert operands to instruction");
1625 llvm_unreachable("Implement any new match types added!");
1629 /// ParseDirective parses the arm specific directives
1630 bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
1631 StringRef IDVal = DirectiveID.getIdentifier();
1632 if (IDVal == ".word")
1633 return ParseDirectiveWord(4, DirectiveID.getLoc());
1634 else if (IDVal == ".thumb")
1635 return ParseDirectiveThumb(DirectiveID.getLoc());
1636 else if (IDVal == ".thumb_func")
1637 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
1638 else if (IDVal == ".code")
1639 return ParseDirectiveCode(DirectiveID.getLoc());
1640 else if (IDVal == ".syntax")
1641 return ParseDirectiveSyntax(DirectiveID.getLoc());
1645 /// ParseDirectiveWord
1646 /// ::= .word [ expression (, expression)* ]
1647 bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1648 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1650 const MCExpr *Value;
1651 if (getParser().ParseExpression(Value))
1654 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
1656 if (getLexer().is(AsmToken::EndOfStatement))
1659 // FIXME: Improve diagnostic.
1660 if (getLexer().isNot(AsmToken::Comma))
1661 return Error(L, "unexpected token in directive");
1670 /// ParseDirectiveThumb
1672 bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
1673 if (getLexer().isNot(AsmToken::EndOfStatement))
1674 return Error(L, "unexpected token in directive");
1677 // TODO: set thumb mode
1678 // TODO: tell the MC streamer the mode
1679 // getParser().getStreamer().Emit???();
1683 /// ParseDirectiveThumbFunc
1684 /// ::= .thumbfunc symbol_name
1685 bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
1686 const AsmToken &Tok = Parser.getTok();
1687 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
1688 return Error(L, "unexpected token in .thumb_func directive");
1689 StringRef Name = Tok.getString();
1690 Parser.Lex(); // Consume the identifier token.
1691 if (getLexer().isNot(AsmToken::EndOfStatement))
1692 return Error(L, "unexpected token in directive");
1695 // Mark symbol as a thumb symbol.
1696 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
1697 getParser().getStreamer().EmitThumbFunc(Func);
1701 /// ParseDirectiveSyntax
1702 /// ::= .syntax unified | divided
1703 bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
1704 const AsmToken &Tok = Parser.getTok();
1705 if (Tok.isNot(AsmToken::Identifier))
1706 return Error(L, "unexpected token in .syntax directive");
1707 StringRef Mode = Tok.getString();
1708 if (Mode == "unified" || Mode == "UNIFIED")
1710 else if (Mode == "divided" || Mode == "DIVIDED")
1711 return Error(L, "'.syntax divided' arm asssembly not supported");
1713 return Error(L, "unrecognized syntax mode in .syntax directive");
1715 if (getLexer().isNot(AsmToken::EndOfStatement))
1716 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
1719 // TODO tell the MC streamer the mode
1720 // getParser().getStreamer().Emit???();
1724 /// ParseDirectiveCode
1725 /// ::= .code 16 | 32
1726 bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
1727 const AsmToken &Tok = Parser.getTok();
1728 if (Tok.isNot(AsmToken::Integer))
1729 return Error(L, "unexpected token in .code directive");
1730 int64_t Val = Parser.getTok().getIntVal();
1736 return Error(L, "invalid operand to .code directive");
1738 if (getLexer().isNot(AsmToken::EndOfStatement))
1739 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
1742 // FIXME: We need to be able switch subtargets at this point so that
1743 // MatchInstructionImpl() will work when it gets the AvailableFeatures which
1744 // includes Feature_IsThumb or not to match the right instructions. This is
1745 // blocked on the FIXME in llvm-mc.cpp when creating the TargetMachine.
1747 assert(TM.getSubtarget<ARMSubtarget>().isThumb() &&
1748 "switching between arm/thumb not yet suppported via .code 16)");
1749 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
1752 assert(!TM.getSubtarget<ARMSubtarget>().isThumb() &&
1753 "switching between thumb/arm not yet suppported via .code 32)");
1754 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1760 extern "C" void LLVMInitializeARMAsmLexer();
1762 /// Force static initialization.
1763 extern "C" void LLVMInitializeARMAsmParser() {
1764 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1765 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
1766 LLVMInitializeARMAsmLexer();
1769 #define GET_REGISTER_MATCHER
1770 #define GET_MATCHER_IMPLEMENTATION
1771 #include "ARMGenAsmMatcher.inc"