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 "ARMBaseRegisterInfo.h"
13 #include "ARMSubtarget.h"
14 #include "llvm/MC/MCParser/MCAsmLexer.h"
15 #include "llvm/MC/MCParser/MCAsmParser.h"
16 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/MC/MCStreamer.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/Target/TargetRegistry.h"
22 #include "llvm/Target/TargetAsmParser.h"
23 #include "llvm/Support/SourceMgr.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/ADT/StringSwitch.h"
27 #include "llvm/ADT/Twine.h"
30 /// Shift types used for register controlled shifts in ARM memory addressing.
43 class ARMAsmParser : public TargetAsmParser {
47 MCAsmParser &getParser() const { return Parser; }
48 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
50 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
51 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
53 int TryParseRegister();
54 bool TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
55 bool ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
56 bool ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &);
57 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &);
59 bool ParseMemoryOffsetReg(bool &Negative,
60 bool &OffsetRegShifted,
61 enum ShiftType &ShiftType,
62 const MCExpr *&ShiftAmount,
63 const MCExpr *&Offset,
67 bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount, SMLoc &E);
68 bool ParseDirectiveWord(unsigned Size, SMLoc L);
69 bool ParseDirectiveThumb(SMLoc L);
70 bool ParseDirectiveThumbFunc(SMLoc L);
71 bool ParseDirectiveCode(SMLoc L);
72 bool ParseDirectiveSyntax(SMLoc L);
74 bool MatchAndEmitInstruction(SMLoc IDLoc,
75 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
78 /// @name Auto-generated Match Functions
81 #define GET_ASSEMBLER_HEADER
82 #include "ARMGenAsmMatcher.inc"
87 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
88 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {
89 // Initialize the set of available features.
90 setAvailableFeatures(ComputeAvailableFeatures(
91 &TM.getSubtarget<ARMSubtarget>()));
94 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
95 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
96 virtual bool ParseDirective(AsmToken DirectiveID);
98 } // end anonymous namespace
102 /// ARMOperand - Instances of this class represent a parsed ARM machine
104 class ARMOperand : public MCParsedAsmOperand {
117 SMLoc StartLoc, EndLoc;
118 SmallVector<unsigned, 8> Registers;
122 ARMCC::CondCodes Val;
138 /// Combined record for all forms of ARM address expressions.
141 unsigned OffsetRegNum; // used when OffsetIsReg is true
142 const MCExpr *Offset; // used when OffsetIsReg is false
143 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
144 enum ShiftType ShiftType; // used when OffsetRegShifted is true
145 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
146 unsigned Preindexed : 1;
147 unsigned Postindexed : 1;
148 unsigned OffsetIsReg : 1;
149 unsigned Negative : 1; // only used when OffsetIsReg is true
150 unsigned Writeback : 1;
154 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
156 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
158 StartLoc = o.StartLoc;
172 case DPRRegisterList:
173 case SPRRegisterList:
174 Registers = o.Registers;
185 /// getStartLoc - Get the location of the first token of this operand.
186 SMLoc getStartLoc() const { return StartLoc; }
187 /// getEndLoc - Get the location of the last token of this operand.
188 SMLoc getEndLoc() const { return EndLoc; }
190 ARMCC::CondCodes getCondCode() const {
191 assert(Kind == CondCode && "Invalid access!");
195 StringRef getToken() const {
196 assert(Kind == Token && "Invalid access!");
197 return StringRef(Tok.Data, Tok.Length);
200 unsigned getReg() const {
201 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
205 const SmallVectorImpl<unsigned> &getRegList() const {
206 assert((Kind == RegisterList || Kind == DPRRegisterList ||
207 Kind == SPRRegisterList) && "Invalid access!");
211 const MCExpr *getImm() const {
212 assert(Kind == Immediate && "Invalid access!");
216 bool isCondCode() const { return Kind == CondCode; }
217 bool isCCOut() const { return Kind == CCOut; }
218 bool isImm() const { return Kind == Immediate; }
219 bool isReg() const { return Kind == Register; }
220 bool isRegList() const { return Kind == RegisterList; }
221 bool isDPRRegList() const { return Kind == DPRRegisterList; }
222 bool isSPRRegList() const { return Kind == SPRRegisterList; }
223 bool isToken() const { return Kind == Token; }
224 bool isMemory() const { return Kind == Memory; }
225 bool isMemMode5() const {
226 if (!isMemory() || Mem.OffsetIsReg || Mem.OffsetRegShifted ||
227 Mem.Writeback || Mem.Negative)
230 // If there is an offset expression, make sure it's valid.
231 if (!Mem.Offset) return true;
233 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
234 if (!CE) return false;
236 // The offset must be a multiple of 4 in the range 0-1020.
237 int64_t Value = CE->getValue();
238 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
240 bool isMemModeRegThumb() const {
241 if (!isMemory() || (!Mem.OffsetIsReg && !Mem.Offset) || Mem.Writeback)
243 return !Mem.Offset || !isa<MCConstantExpr>(Mem.Offset);
245 bool isMemModeImmThumb() const {
246 if (!isMemory() || (!Mem.OffsetIsReg && !Mem.Offset) || Mem.Writeback)
249 if (!Mem.Offset) return false;
251 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
252 if (!CE) return false;
254 // The offset must be a multiple of 4 in the range 0-124.
255 uint64_t Value = CE->getValue();
256 return ((Value & 0x3) == 0 && Value <= 124);
259 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
260 // Add as immediates when possible. Null MCExpr = 0.
262 Inst.addOperand(MCOperand::CreateImm(0));
263 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
264 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
266 Inst.addOperand(MCOperand::CreateExpr(Expr));
269 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
270 assert(N == 2 && "Invalid number of operands!");
271 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
272 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
273 Inst.addOperand(MCOperand::CreateReg(RegNum));
276 void addCCOutOperands(MCInst &Inst, unsigned N) const {
277 assert(N == 1 && "Invalid number of operands!");
278 Inst.addOperand(MCOperand::CreateReg(getReg()));
281 void addRegOperands(MCInst &Inst, unsigned N) const {
282 assert(N == 1 && "Invalid number of operands!");
283 Inst.addOperand(MCOperand::CreateReg(getReg()));
286 void addRegListOperands(MCInst &Inst, unsigned N) const {
287 assert(N == 1 && "Invalid number of operands!");
288 const SmallVectorImpl<unsigned> &RegList = getRegList();
289 for (SmallVectorImpl<unsigned>::const_iterator
290 I = RegList.begin(), E = RegList.end(); I != E; ++I)
291 Inst.addOperand(MCOperand::CreateReg(*I));
294 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
295 addRegListOperands(Inst, N);
298 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
299 addRegListOperands(Inst, N);
302 void addImmOperands(MCInst &Inst, unsigned N) const {
303 assert(N == 1 && "Invalid number of operands!");
304 addExpr(Inst, getImm());
307 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
308 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
310 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
311 assert(!Mem.OffsetIsReg && "Invalid mode 5 operand");
313 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
316 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
317 assert(CE && "Non-constant mode 5 offset operand!");
319 // The MCInst offset operand doesn't include the low two bits (like
320 // the instruction encoding).
321 int64_t Offset = CE->getValue() / 4;
323 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
326 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
329 Inst.addOperand(MCOperand::CreateImm(0));
333 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
334 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
335 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
336 Inst.addOperand(MCOperand::CreateReg(Mem.OffsetRegNum));
339 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
340 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
341 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
342 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
343 assert(CE && "Non-constant mode offset operand!");
344 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
347 virtual void dump(raw_ostream &OS) const;
349 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
350 ARMOperand *Op = new ARMOperand(CondCode);
357 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
358 ARMOperand *Op = new ARMOperand(CCOut);
359 Op->Reg.RegNum = RegNum;
365 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
366 ARMOperand *Op = new ARMOperand(Token);
367 Op->Tok.Data = Str.data();
368 Op->Tok.Length = Str.size();
374 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
375 ARMOperand *Op = new ARMOperand(Register);
376 Op->Reg.RegNum = RegNum;
383 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
384 SMLoc StartLoc, SMLoc EndLoc) {
385 KindTy Kind = RegisterList;
387 if (ARM::DPRRegClass.contains(Regs.front().first))
388 Kind = DPRRegisterList;
389 else if (ARM::SPRRegClass.contains(Regs.front().first))
390 Kind = SPRRegisterList;
392 ARMOperand *Op = new ARMOperand(Kind);
393 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
394 I = Regs.begin(), E = Regs.end(); I != E; ++I)
395 Op->Registers.push_back(I->first);
396 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
397 Op->StartLoc = StartLoc;
402 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
403 ARMOperand *Op = new ARMOperand(Immediate);
410 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
411 const MCExpr *Offset, unsigned OffsetRegNum,
412 bool OffsetRegShifted, enum ShiftType ShiftType,
413 const MCExpr *ShiftAmount, bool Preindexed,
414 bool Postindexed, bool Negative, bool Writeback,
416 ARMOperand *Op = new ARMOperand(Memory);
417 Op->Mem.BaseRegNum = BaseRegNum;
418 Op->Mem.OffsetIsReg = OffsetIsReg;
419 Op->Mem.Offset = Offset;
420 Op->Mem.OffsetRegNum = OffsetRegNum;
421 Op->Mem.OffsetRegShifted = OffsetRegShifted;
422 Op->Mem.ShiftType = ShiftType;
423 Op->Mem.ShiftAmount = ShiftAmount;
424 Op->Mem.Preindexed = Preindexed;
425 Op->Mem.Postindexed = Postindexed;
426 Op->Mem.Negative = Negative;
427 Op->Mem.Writeback = Writeback;
435 } // end anonymous namespace.
437 void ARMOperand::dump(raw_ostream &OS) const {
440 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
443 OS << "<ccout " << getReg() << ">";
452 OS << "<register " << getReg() << ">";
455 case DPRRegisterList:
456 case SPRRegisterList: {
457 OS << "<register_list ";
459 const SmallVectorImpl<unsigned> &RegList = getRegList();
460 for (SmallVectorImpl<unsigned>::const_iterator
461 I = RegList.begin(), E = RegList.end(); I != E; ) {
463 if (++I < E) OS << ", ";
470 OS << "'" << getToken() << "'";
475 /// @name Auto-generated Match Functions
478 static unsigned MatchRegisterName(StringRef Name);
482 /// Try to parse a register name. The token must be an Identifier when called,
483 /// and if it is a register name the token is eaten and the register number is
484 /// returned. Otherwise return -1.
486 int ARMAsmParser::TryParseRegister() {
487 const AsmToken &Tok = Parser.getTok();
488 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
490 // FIXME: Validate register for the current architecture; we have to do
491 // validation later, so maybe there is no need for this here.
492 unsigned RegNum = MatchRegisterName(Tok.getString());
495 Parser.Lex(); // Eat identifier token.
500 /// Try to parse a register name. The token must be an Identifier when called.
501 /// If it's a register, an AsmOperand is created. Another AsmOperand is created
502 /// if there is a "writeback". 'true' if it's not a register.
504 /// TODO this is likely to change to allow different register types and or to
505 /// parse for a specific register type.
507 TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
508 SMLoc S = Parser.getTok().getLoc();
509 int RegNo = TryParseRegister();
513 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
515 const AsmToken &ExclaimTok = Parser.getTok();
516 if (ExclaimTok.is(AsmToken::Exclaim)) {
517 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
518 ExclaimTok.getLoc()));
519 Parser.Lex(); // Eat exclaim token
525 /// Parse a register list, return it if successful else return null. The first
526 /// token must be a '{' when called.
528 ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
529 assert(Parser.getTok().is(AsmToken::LCurly) &&
530 "Token is not a Left Curly Brace");
531 SMLoc S = Parser.getTok().getLoc();
533 // Read the rest of the registers in the list.
534 unsigned PrevRegNum = 0;
535 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
538 bool IsRange = Parser.getTok().is(AsmToken::Minus);
539 Parser.Lex(); // Eat non-identifier token.
541 const AsmToken &RegTok = Parser.getTok();
542 SMLoc RegLoc = RegTok.getLoc();
543 if (RegTok.isNot(AsmToken::Identifier)) {
544 Error(RegLoc, "register expected");
548 int RegNum = TryParseRegister();
550 Error(RegLoc, "register expected");
555 int Reg = PrevRegNum;
558 Registers.push_back(std::make_pair(Reg, RegLoc));
559 } while (Reg != RegNum);
561 Registers.push_back(std::make_pair(RegNum, RegLoc));
565 } while (Parser.getTok().is(AsmToken::Comma) ||
566 Parser.getTok().is(AsmToken::Minus));
568 // Process the right curly brace of the list.
569 const AsmToken &RCurlyTok = Parser.getTok();
570 if (RCurlyTok.isNot(AsmToken::RCurly)) {
571 Error(RCurlyTok.getLoc(), "'}' expected");
575 SMLoc E = RCurlyTok.getLoc();
576 Parser.Lex(); // Eat right curly brace token.
578 // Verify the register list.
579 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
580 RI = Registers.begin(), RE = Registers.end();
582 DenseMap<unsigned, bool> RegMap;
583 RegMap[RI->first] = true;
585 unsigned HighRegNum = RI->first;
586 bool EmittedWarning = false;
588 for (++RI; RI != RE; ++RI) {
589 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
590 unsigned Reg = RegInfo.first;
593 Error(RegInfo.second, "register duplicated in register list");
597 if (!EmittedWarning && Reg < HighRegNum)
598 Warning(RegInfo.second,
599 "register not in ascending order in register list");
602 HighRegNum = std::max(Reg, HighRegNum);
605 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
609 /// Parse an ARM memory expression, return false if successful else return true
610 /// or an error. The first token must be a '[' when called.
612 /// TODO Only preindexing and postindexing addressing are started, unindexed
613 /// with option, etc are still to do.
615 ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
617 assert(Parser.getTok().is(AsmToken::LBrac) &&
618 "Token is not a Left Bracket");
619 S = Parser.getTok().getLoc();
620 Parser.Lex(); // Eat left bracket token.
622 const AsmToken &BaseRegTok = Parser.getTok();
623 if (BaseRegTok.isNot(AsmToken::Identifier)) {
624 Error(BaseRegTok.getLoc(), "register expected");
627 int BaseRegNum = TryParseRegister();
628 if (BaseRegNum == -1) {
629 Error(BaseRegTok.getLoc(), "register expected");
633 bool Preindexed = false;
634 bool Postindexed = false;
635 bool OffsetIsReg = false;
636 bool Negative = false;
637 bool Writeback = false;
639 // First look for preindexed address forms, that is after the "[Rn" we now
640 // have to see if the next token is a comma.
641 const AsmToken &Tok = Parser.getTok();
642 if (Tok.is(AsmToken::Comma)) {
644 Parser.Lex(); // Eat comma token.
646 bool OffsetRegShifted;
647 enum ShiftType ShiftType;
648 const MCExpr *ShiftAmount = 0;
649 const MCExpr *Offset = 0;
650 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
651 Offset, OffsetIsReg, OffsetRegNum, E))
653 const AsmToken &RBracTok = Parser.getTok();
654 if (RBracTok.isNot(AsmToken::RBrac)) {
655 Error(RBracTok.getLoc(), "']' expected");
658 E = RBracTok.getLoc();
659 Parser.Lex(); // Eat right bracket token.
662 const AsmToken &ExclaimTok = Parser.getTok();
663 ARMOperand *WBOp = 0;
664 if (ExclaimTok.is(AsmToken::Exclaim)) {
665 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
666 ExclaimTok.getLoc());
668 Parser.Lex(); // Eat exclaim token
671 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
672 OffsetRegNum, OffsetRegShifted,
673 ShiftType, ShiftAmount, Preindexed,
674 Postindexed, Negative, Writeback,
677 Operands.push_back(WBOp);
681 // The "[Rn" we have so far was not followed by a comma.
682 else if (Tok.is(AsmToken::RBrac)) {
683 // If there's anything other than the right brace, this is a post indexing
686 Parser.Lex(); // Eat right bracket token.
688 int OffsetRegNum = 0;
689 bool OffsetRegShifted = false;
690 enum ShiftType ShiftType = Lsl;
691 const MCExpr *ShiftAmount = 0;
692 const MCExpr *Offset = 0;
694 const AsmToken &NextTok = Parser.getTok();
696 if (NextTok.isNot(AsmToken::EndOfStatement)) {
700 if (NextTok.isNot(AsmToken::Comma)) {
701 Error(NextTok.getLoc(), "',' expected");
705 Parser.Lex(); // Eat comma token.
707 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
708 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
713 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
714 OffsetRegNum, OffsetRegShifted,
715 ShiftType, ShiftAmount, Preindexed,
716 Postindexed, Negative, Writeback,
724 /// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
725 /// we will parse the following (were +/- means that a plus or minus is
730 /// we return false on success or an error otherwise.
731 bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
732 bool &OffsetRegShifted,
733 enum ShiftType &ShiftType,
734 const MCExpr *&ShiftAmount,
735 const MCExpr *&Offset,
740 OffsetRegShifted = false;
743 const AsmToken &NextTok = Parser.getTok();
744 E = NextTok.getLoc();
745 if (NextTok.is(AsmToken::Plus))
746 Parser.Lex(); // Eat plus token.
747 else if (NextTok.is(AsmToken::Minus)) {
749 Parser.Lex(); // Eat minus token
751 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
752 const AsmToken &OffsetRegTok = Parser.getTok();
753 if (OffsetRegTok.is(AsmToken::Identifier)) {
754 SMLoc CurLoc = OffsetRegTok.getLoc();
755 OffsetRegNum = TryParseRegister();
756 if (OffsetRegNum != -1) {
762 // If we parsed a register as the offset then there can be a shift after that.
763 if (OffsetRegNum != -1) {
764 // Look for a comma then a shift
765 const AsmToken &Tok = Parser.getTok();
766 if (Tok.is(AsmToken::Comma)) {
767 Parser.Lex(); // Eat comma token.
769 const AsmToken &Tok = Parser.getTok();
770 if (ParseShift(ShiftType, ShiftAmount, E))
771 return Error(Tok.getLoc(), "shift expected");
772 OffsetRegShifted = true;
775 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
776 // Look for #offset following the "[Rn," or "[Rn],"
777 const AsmToken &HashTok = Parser.getTok();
778 if (HashTok.isNot(AsmToken::Hash))
779 return Error(HashTok.getLoc(), "'#' expected");
781 Parser.Lex(); // Eat hash token.
783 if (getParser().ParseExpression(Offset))
785 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
790 /// ParseShift as one of these two:
791 /// ( lsl | lsr | asr | ror ) , # shift_amount
793 /// and returns true if it parses a shift otherwise it returns false.
794 bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
796 const AsmToken &Tok = Parser.getTok();
797 if (Tok.isNot(AsmToken::Identifier))
799 StringRef ShiftName = Tok.getString();
800 if (ShiftName == "lsl" || ShiftName == "LSL")
802 else if (ShiftName == "lsr" || ShiftName == "LSR")
804 else if (ShiftName == "asr" || ShiftName == "ASR")
806 else if (ShiftName == "ror" || ShiftName == "ROR")
808 else if (ShiftName == "rrx" || ShiftName == "RRX")
812 Parser.Lex(); // Eat shift type token.
818 // Otherwise, there must be a '#' and a shift amount.
819 const AsmToken &HashTok = Parser.getTok();
820 if (HashTok.isNot(AsmToken::Hash))
821 return Error(HashTok.getLoc(), "'#' expected");
822 Parser.Lex(); // Eat hash token.
824 if (getParser().ParseExpression(ShiftAmount))
830 /// Parse a arm instruction operand. For now this parses the operand regardless
832 bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands){
834 switch (getLexer().getKind()) {
836 Error(Parser.getTok().getLoc(), "unexpected token in operand");
838 case AsmToken::Identifier: {
839 if (!TryParseRegisterWithWriteBack(Operands))
842 // This was not a register so parse other operands that start with an
843 // identifier (like labels) as expressions and create them as immediates.
845 S = Parser.getTok().getLoc();
846 if (getParser().ParseExpression(IdVal))
848 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
849 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
852 case AsmToken::LBrac:
853 return ParseMemory(Operands);
854 case AsmToken::LCurly:
855 return ParseRegisterList(Operands);
858 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
859 S = Parser.getTok().getLoc();
861 const MCExpr *ImmVal;
862 if (getParser().ParseExpression(ImmVal))
864 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
865 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
870 /// \brief Given a mnemonic, split out possible predication code and carry
871 /// setting letters to form a canonical mnemonic and flags.
873 // FIXME: Would be nice to autogen this.
874 static StringRef SplitMnemonicAndCC(StringRef Mnemonic,
875 unsigned &PredicationCode,
876 bool &CarrySetting) {
877 PredicationCode = ARMCC::AL;
878 CarrySetting = false;
880 // Ignore some mnemonics we know aren't predicated forms.
882 // FIXME: Would be nice to autogen this.
883 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
884 Mnemonic == "movs" ||
886 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
887 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
888 Mnemonic == "vacge" || Mnemonic == "vcge" ||
889 Mnemonic == "vclt" ||
890 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
891 Mnemonic == "vcle" ||
892 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
893 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
894 Mnemonic == "vqdmlal"))
897 // First, split out any predication code.
898 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
899 .Case("eq", ARMCC::EQ)
900 .Case("ne", ARMCC::NE)
901 .Case("hs", ARMCC::HS)
902 .Case("lo", ARMCC::LO)
903 .Case("mi", ARMCC::MI)
904 .Case("pl", ARMCC::PL)
905 .Case("vs", ARMCC::VS)
906 .Case("vc", ARMCC::VC)
907 .Case("hi", ARMCC::HI)
908 .Case("ls", ARMCC::LS)
909 .Case("ge", ARMCC::GE)
910 .Case("lt", ARMCC::LT)
911 .Case("gt", ARMCC::GT)
912 .Case("le", ARMCC::LE)
913 .Case("al", ARMCC::AL)
916 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
917 PredicationCode = CC;
920 // Next, determine if we have a carry setting bit. We explicitly ignore all
921 // the instructions we know end in 's'.
922 if (Mnemonic.endswith("s") &&
923 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
924 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
925 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
926 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
927 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
928 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
935 /// \brief Given a canonical mnemonic, determine if the instruction ever allows
936 /// inclusion of carry set or predication code operands.
938 // FIXME: It would be nice to autogen this.
939 static void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
940 bool &CanAcceptPredicationCode) {
941 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
942 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
943 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
944 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
945 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mov" ||
946 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
947 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
948 Mnemonic == "eor" || Mnemonic == "smlal" || Mnemonic == "mvn") {
949 CanAcceptCarrySet = true;
951 CanAcceptCarrySet = false;
954 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
955 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
956 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
957 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
958 Mnemonic == "dsb" || Mnemonic == "movs") {
959 CanAcceptPredicationCode = false;
961 CanAcceptPredicationCode = true;
965 /// Parse an arm instruction mnemonic followed by its operands.
966 bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
967 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
968 // Create the leading tokens for the mnemonic, split by '.' characters.
969 size_t Start = 0, Next = Name.find('.');
970 StringRef Head = Name.slice(Start, Next);
972 // Split out the predication code and carry setting flag from the mnemonic.
973 unsigned PredicationCode;
975 Head = SplitMnemonicAndCC(Head, PredicationCode, CarrySetting);
977 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
979 // Next, add the CCOut and ConditionCode operands, if needed.
981 // For mnemonics which can ever incorporate a carry setting bit or predication
982 // code, our matching model involves us always generating CCOut and
983 // ConditionCode operands to match the mnemonic "as written" and then we let
984 // the matcher deal with finding the right instruction or generating an
985 // appropriate error.
986 bool CanAcceptCarrySet, CanAcceptPredicationCode;
987 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
989 // Add the carry setting operand, if necessary.
991 // FIXME: It would be awesome if we could somehow invent a location such that
992 // match errors on this operand would print a nice diagnostic about how the
993 // 's' character in the mnemonic resulted in a CCOut operand.
994 if (CanAcceptCarrySet) {
995 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
998 // This mnemonic can't ever accept a carry set, but the user wrote one (or
999 // misspelled another mnemonic).
1001 // FIXME: Issue a nice error.
1004 // Add the predication code operand, if necessary.
1005 if (CanAcceptPredicationCode) {
1006 Operands.push_back(ARMOperand::CreateCondCode(
1007 ARMCC::CondCodes(PredicationCode), NameLoc));
1009 // This mnemonic can't ever accept a predication code, but the user wrote
1010 // one (or misspelled another mnemonic).
1012 // FIXME: Issue a nice error.
1015 // Add the remaining tokens in the mnemonic.
1016 while (Next != StringRef::npos) {
1018 Next = Name.find('.', Start + 1);
1019 Head = Name.slice(Start, Next);
1021 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
1024 // Read the remaining operands.
1025 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1026 // Read the first operand.
1027 if (ParseOperand(Operands)) {
1028 Parser.EatToEndOfStatement();
1032 while (getLexer().is(AsmToken::Comma)) {
1033 Parser.Lex(); // Eat the comma.
1035 // Parse and remember the operand.
1036 if (ParseOperand(Operands)) {
1037 Parser.EatToEndOfStatement();
1043 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1044 Parser.EatToEndOfStatement();
1045 return TokError("unexpected token in argument list");
1048 Parser.Lex(); // Consume the EndOfStatement
1053 MatchAndEmitInstruction(SMLoc IDLoc,
1054 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1058 MatchResultTy MatchResult, MatchResult2;
1059 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1060 if (MatchResult != Match_Success) {
1061 // If we get a Match_InvalidOperand it might be some arithmetic instruction
1062 // that does not update the condition codes. So try adding a CCOut operand
1063 // with a value of reg0.
1064 if (MatchResult == Match_InvalidOperand) {
1065 Operands.insert(Operands.begin() + 1,
1066 ARMOperand::CreateCCOut(0,
1067 ((ARMOperand*)Operands[0])->getStartLoc()));
1068 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1069 if (MatchResult2 == Match_Success)
1070 MatchResult = Match_Success;
1072 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
1073 Operands.erase(Operands.begin() + 1);
1077 // If we get a Match_MnemonicFail it might be some arithmetic instruction
1078 // that updates the condition codes if it ends in 's'. So see if the
1079 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
1080 // operand with a value of CPSR.
1081 else if(MatchResult == Match_MnemonicFail) {
1082 // Get the instruction mnemonic, which is the first token.
1083 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
1084 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
1085 // removed the 's' from the mnemonic for matching.
1086 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
1087 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
1088 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1089 Operands.erase(Operands.begin());
1091 Operands.insert(Operands.begin(),
1092 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
1093 Operands.insert(Operands.begin() + 1,
1094 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
1095 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1096 if (MatchResult2 == Match_Success)
1097 MatchResult = Match_Success;
1099 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1100 Operands.erase(Operands.begin());
1102 Operands.insert(Operands.begin(),
1103 ARMOperand::CreateToken(Mnemonic, NameLoc));
1104 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
1105 Operands.erase(Operands.begin() + 1);
1111 switch (MatchResult) {
1113 Out.EmitInstruction(Inst);
1115 case Match_MissingFeature:
1116 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1118 case Match_InvalidOperand: {
1119 SMLoc ErrorLoc = IDLoc;
1120 if (ErrorInfo != ~0U) {
1121 if (ErrorInfo >= Operands.size())
1122 return Error(IDLoc, "too few operands for instruction");
1124 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
1125 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1128 return Error(ErrorLoc, "invalid operand for instruction");
1130 case Match_MnemonicFail:
1131 return Error(IDLoc, "unrecognized instruction mnemonic");
1134 llvm_unreachable("Implement any new match types added!");
1138 /// ParseDirective parses the arm specific directives
1139 bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
1140 StringRef IDVal = DirectiveID.getIdentifier();
1141 if (IDVal == ".word")
1142 return ParseDirectiveWord(4, DirectiveID.getLoc());
1143 else if (IDVal == ".thumb")
1144 return ParseDirectiveThumb(DirectiveID.getLoc());
1145 else if (IDVal == ".thumb_func")
1146 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
1147 else if (IDVal == ".code")
1148 return ParseDirectiveCode(DirectiveID.getLoc());
1149 else if (IDVal == ".syntax")
1150 return ParseDirectiveSyntax(DirectiveID.getLoc());
1154 /// ParseDirectiveWord
1155 /// ::= .word [ expression (, expression)* ]
1156 bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1157 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1159 const MCExpr *Value;
1160 if (getParser().ParseExpression(Value))
1163 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
1165 if (getLexer().is(AsmToken::EndOfStatement))
1168 // FIXME: Improve diagnostic.
1169 if (getLexer().isNot(AsmToken::Comma))
1170 return Error(L, "unexpected token in directive");
1179 /// ParseDirectiveThumb
1181 bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
1182 if (getLexer().isNot(AsmToken::EndOfStatement))
1183 return Error(L, "unexpected token in directive");
1186 // TODO: set thumb mode
1187 // TODO: tell the MC streamer the mode
1188 // getParser().getStreamer().Emit???();
1192 /// ParseDirectiveThumbFunc
1193 /// ::= .thumbfunc symbol_name
1194 bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
1195 const AsmToken &Tok = Parser.getTok();
1196 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
1197 return Error(L, "unexpected token in .thumb_func directive");
1198 StringRef Name = Tok.getString();
1199 Parser.Lex(); // Consume the identifier token.
1200 if (getLexer().isNot(AsmToken::EndOfStatement))
1201 return Error(L, "unexpected token in directive");
1204 // Mark symbol as a thumb symbol.
1205 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
1206 getParser().getStreamer().EmitThumbFunc(Func);
1210 /// ParseDirectiveSyntax
1211 /// ::= .syntax unified | divided
1212 bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
1213 const AsmToken &Tok = Parser.getTok();
1214 if (Tok.isNot(AsmToken::Identifier))
1215 return Error(L, "unexpected token in .syntax directive");
1216 StringRef Mode = Tok.getString();
1217 if (Mode == "unified" || Mode == "UNIFIED")
1219 else if (Mode == "divided" || Mode == "DIVIDED")
1222 return Error(L, "unrecognized syntax mode in .syntax directive");
1224 if (getLexer().isNot(AsmToken::EndOfStatement))
1225 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
1228 // TODO tell the MC streamer the mode
1229 // getParser().getStreamer().Emit???();
1233 /// ParseDirectiveCode
1234 /// ::= .code 16 | 32
1235 bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
1236 const AsmToken &Tok = Parser.getTok();
1237 if (Tok.isNot(AsmToken::Integer))
1238 return Error(L, "unexpected token in .code directive");
1239 int64_t Val = Parser.getTok().getIntVal();
1245 return Error(L, "invalid operand to .code directive");
1247 if (getLexer().isNot(AsmToken::EndOfStatement))
1248 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
1252 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
1254 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1259 extern "C" void LLVMInitializeARMAsmLexer();
1261 /// Force static initialization.
1262 extern "C" void LLVMInitializeARMAsmParser() {
1263 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1264 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
1265 LLVMInitializeARMAsmLexer();
1268 #define GET_REGISTER_MATCHER
1269 #define GET_MATCHER_IMPLEMENTATION
1270 #include "ARMGenAsmMatcher.inc"