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 "ARMSubtarget.h"
13 #include "llvm/MC/MCParser/MCAsmLexer.h"
14 #include "llvm/MC/MCParser/MCAsmParser.h"
15 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCStreamer.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/Target/TargetRegistry.h"
21 #include "llvm/Target/TargetAsmParser.h"
22 #include "llvm/Support/SourceMgr.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/ADT/StringSwitch.h"
26 #include "llvm/ADT/Twine.h"
29 // The shift types for register controlled shifts in arm memory addressing
42 class ARMAsmParser : public TargetAsmParser {
46 MCAsmParser &getParser() const { return Parser; }
47 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
49 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
50 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
52 int TryParseRegister();
53 ARMOperand *TryParseRegisterWithWriteBack();
54 ARMOperand *ParseRegisterList();
55 ARMOperand *ParseMemory();
56 ARMOperand *ParseOperand();
58 bool ParseMemoryOffsetReg(bool &Negative,
59 bool &OffsetRegShifted,
60 enum ShiftType &ShiftType,
61 const MCExpr *&ShiftAmount,
62 const MCExpr *&Offset,
66 bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount, SMLoc &E);
67 bool ParseDirectiveWord(unsigned Size, SMLoc L);
68 bool ParseDirectiveThumb(SMLoc L);
69 bool ParseDirectiveThumbFunc(SMLoc L);
70 bool ParseDirectiveCode(SMLoc L);
71 bool ParseDirectiveSyntax(SMLoc L);
73 bool MatchAndEmitInstruction(SMLoc IDLoc,
74 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
77 /// @name Auto-generated Match Functions
80 #define GET_ASSEMBLER_HEADER
81 #include "ARMGenAsmMatcher.inc"
86 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
87 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {
88 // Initialize the set of available features.
89 setAvailableFeatures(ComputeAvailableFeatures(
90 &TM.getSubtarget<ARMSubtarget>()));
93 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
94 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
95 virtual bool ParseDirective(AsmToken DirectiveID);
97 } // end anonymous namespace
101 /// ARMOperand - Instances of this class represent a parsed ARM machine
103 class ARMOperand : public MCParsedAsmOperand {
113 SMLoc StartLoc, EndLoc;
117 ARMCC::CondCodes Val;
131 std::vector<unsigned> *Registers;
138 // This is 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;
182 /// getStartLoc - Get the location of the first token of this operand.
183 SMLoc getStartLoc() const { return StartLoc; }
184 /// getEndLoc - Get the location of the last token of this operand.
185 SMLoc getEndLoc() const { return EndLoc; }
187 ARMCC::CondCodes getCondCode() const {
188 assert(Kind == CondCode && "Invalid access!");
192 StringRef getToken() const {
193 assert(Kind == Token && "Invalid access!");
194 return StringRef(Tok.Data, Tok.Length);
197 unsigned getReg() const {
198 assert(Kind == Register && "Invalid access!");
202 const std::vector<unsigned> &getRegList() const {
203 assert(Kind == RegisterList && "Invalid access!");
204 return *RegList.Registers;
207 const MCExpr *getImm() const {
208 assert(Kind == Immediate && "Invalid access!");
212 bool isCondCode() const { return Kind == CondCode; }
213 bool isImm() const { return Kind == Immediate; }
214 bool isReg() const { return Kind == Register; }
215 bool isRegList() const { return Kind == RegisterList; }
216 bool isToken() const { return Kind == Token; }
217 bool isMemory() const { return Kind == Memory; }
218 bool isMemMode5() const {
219 if (!isMemory() || Mem.OffsetIsReg || Mem.OffsetRegShifted ||
220 Mem.Writeback || Mem.Negative)
222 // If there is an offset expression, make sure it's valid.
225 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
228 // The offset must be a multiple of 4 in the range 0-1020.
229 int64_t Value = CE->getValue();
230 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
233 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
234 // Add as immediates when possible. Null MCExpr = 0.
236 Inst.addOperand(MCOperand::CreateImm(0));
237 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
238 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
240 Inst.addOperand(MCOperand::CreateExpr(Expr));
243 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
244 assert(N == 2 && "Invalid number of operands!");
245 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
246 // FIXME: What belongs here?
247 Inst.addOperand(MCOperand::CreateReg(0));
250 void addRegOperands(MCInst &Inst, unsigned N) const {
251 assert(N == 1 && "Invalid number of operands!");
252 Inst.addOperand(MCOperand::CreateReg(getReg()));
255 void addRegListOperands(MCInst &Inst, unsigned N) const {
256 assert(N == 1 && "Invalid number of operands!");
257 const std::vector<unsigned> &RegList = getRegList();
258 for (std::vector<unsigned>::const_iterator
259 I = RegList.begin(), E = RegList.end(); I != E; ++I)
260 Inst.addOperand(MCOperand::CreateReg(*I));
263 void addImmOperands(MCInst &Inst, unsigned N) const {
264 assert(N == 1 && "Invalid number of operands!");
265 addExpr(Inst, getImm());
268 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
269 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
271 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
272 assert(!Mem.OffsetIsReg && "Invalid mode 5 operand");
274 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
277 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
278 assert(CE && "Non-constant mode 5 offset operand!");
280 // The MCInst offset operand doesn't include the low two bits (like
281 // the instruction encoding).
282 int64_t Offset = CE->getValue() / 4;
284 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
287 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
290 Inst.addOperand(MCOperand::CreateImm(0));
294 virtual void dump(raw_ostream &OS) const;
296 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
297 ARMOperand *Op = new ARMOperand(CondCode);
304 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
305 ARMOperand *Op = new ARMOperand(Token);
306 Op->Tok.Data = Str.data();
307 Op->Tok.Length = Str.size();
313 static ARMOperand *CreateReg(unsigned RegNum, bool Writeback, SMLoc S,
315 ARMOperand *Op = new ARMOperand(Register);
316 Op->Reg.RegNum = RegNum;
317 Op->Reg.Writeback = Writeback;
324 CreateRegList(std::vector<std::pair<unsigned, SMLoc> > &Regs,
326 ARMOperand *Op = new ARMOperand(RegisterList);
327 Op->RegList.Registers = new std::vector<unsigned>();
328 for (std::vector<std::pair<unsigned, SMLoc> >::iterator
329 I = Regs.begin(), E = Regs.end(); I != E; ++I)
330 Op->RegList.Registers->push_back(I->first);
331 std::sort(Op->RegList.Registers->begin(), Op->RegList.Registers->end());
337 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
338 ARMOperand *Op = new ARMOperand(Immediate);
345 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
346 const MCExpr *Offset, unsigned OffsetRegNum,
347 bool OffsetRegShifted, enum ShiftType ShiftType,
348 const MCExpr *ShiftAmount, bool Preindexed,
349 bool Postindexed, bool Negative, bool Writeback,
351 ARMOperand *Op = new ARMOperand(Memory);
352 Op->Mem.BaseRegNum = BaseRegNum;
353 Op->Mem.OffsetIsReg = OffsetIsReg;
354 Op->Mem.Offset = Offset;
355 Op->Mem.OffsetRegNum = OffsetRegNum;
356 Op->Mem.OffsetRegShifted = OffsetRegShifted;
357 Op->Mem.ShiftType = ShiftType;
358 Op->Mem.ShiftAmount = ShiftAmount;
359 Op->Mem.Preindexed = Preindexed;
360 Op->Mem.Postindexed = Postindexed;
361 Op->Mem.Negative = Negative;
362 Op->Mem.Writeback = Writeback;
370 } // end anonymous namespace.
372 void ARMOperand::dump(raw_ostream &OS) const {
375 OS << ARMCondCodeToString(getCondCode());
384 OS << "<register " << getReg() << ">";
387 OS << "<register_list ";
389 const std::vector<unsigned> &RegList = getRegList();
390 for (std::vector<unsigned>::const_iterator
391 I = RegList.begin(), E = RegList.end(); I != E; ) {
393 if (++I < E) OS << ", ";
400 OS << "'" << getToken() << "'";
405 /// @name Auto-generated Match Functions
408 static unsigned MatchRegisterName(StringRef Name);
412 /// Try to parse a register name. The token must be an Identifier when called,
413 /// and if it is a register name the token is eaten and the register number is
414 /// returned. Otherwise return -1.
416 int ARMAsmParser::TryParseRegister() {
417 const AsmToken &Tok = Parser.getTok();
418 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
420 // FIXME: Validate register for the current architecture; we have to do
421 // validation later, so maybe there is no need for this here.
422 unsigned RegNum = MatchRegisterName(Tok.getString());
425 Parser.Lex(); // Eat identifier token.
430 /// Try to parse a register name. The token must be an Identifier when called,
431 /// and if it is a register name the token is eaten and the register number is
432 /// returned. Otherwise return -1.
434 /// TODO this is likely to change to allow different register types and or to
435 /// parse for a specific register type.
436 ARMOperand *ARMAsmParser::TryParseRegisterWithWriteBack() {
437 SMLoc S = Parser.getTok().getLoc();
438 int RegNo = TryParseRegister();
442 SMLoc E = Parser.getTok().getLoc();
444 bool Writeback = false;
445 const AsmToken &ExclaimTok = Parser.getTok();
446 if (ExclaimTok.is(AsmToken::Exclaim)) {
447 E = ExclaimTok.getLoc();
449 Parser.Lex(); // Eat exclaim token
452 return ARMOperand::CreateReg(RegNo, Writeback, S, E);
455 /// Parse a register list, return it if successful else return null. The first
456 /// token must be a '{' when called.
457 ARMOperand *ARMAsmParser::ParseRegisterList() {
458 assert(Parser.getTok().is(AsmToken::LCurly) &&
459 "Token is not a Left Curly Brace");
460 SMLoc S = Parser.getTok().getLoc();
462 // Read the rest of the registers in the list.
463 unsigned PrevRegNum = 0;
464 std::vector<std::pair<unsigned, SMLoc> > Registers;
465 Registers.reserve(32);
468 bool IsRange = Parser.getTok().is(AsmToken::Minus);
469 Parser.Lex(); // Eat non-identifier token.
471 const AsmToken &RegTok = Parser.getTok();
472 SMLoc RegLoc = RegTok.getLoc();
473 if (RegTok.isNot(AsmToken::Identifier)) {
474 Error(RegLoc, "register expected");
478 int RegNum = TryParseRegister();
480 Error(RegLoc, "register expected");
485 int Reg = PrevRegNum;
488 Registers.push_back(std::make_pair(Reg, RegLoc));
489 } while (Reg != RegNum);
491 Registers.push_back(std::make_pair(RegNum, RegLoc));
495 } while (Parser.getTok().is(AsmToken::Comma) ||
496 Parser.getTok().is(AsmToken::Minus));
498 // Process the right curly brace of the list.
499 const AsmToken &RCurlyTok = Parser.getTok();
500 if (RCurlyTok.isNot(AsmToken::RCurly)) {
501 Error(RCurlyTok.getLoc(), "'}' expected");
505 SMLoc E = RCurlyTok.getLoc();
506 Parser.Lex(); // Eat right curly brace token.
508 // Verify the register list.
509 std::vector<std::pair<unsigned, SMLoc> >::const_iterator
510 RI = Registers.begin(), RE = Registers.end();
512 unsigned HighRegNum = RI->first;
513 DenseMap<unsigned, bool> RegMap;
514 RegMap[RI->first] = true;
516 for (++RI; RI != RE; ++RI) {
517 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
519 if (RegMap[RegInfo.first]) {
520 Error(RegInfo.second, "register duplicated in register list");
524 if (RegInfo.first < HighRegNum)
525 Warning(RegInfo.second,
526 "register not in ascending order in register list");
528 RegMap[RegInfo.first] = true;
529 HighRegNum = std::max(RegInfo.first, HighRegNum);
532 return ARMOperand::CreateRegList(Registers, S, E);
535 /// Parse an ARM memory expression, return false if successful else return true
536 /// or an error. The first token must be a '[' when called.
537 /// TODO Only preindexing and postindexing addressing are started, unindexed
538 /// with option, etc are still to do.
539 ARMOperand *ARMAsmParser::ParseMemory() {
541 assert(Parser.getTok().is(AsmToken::LBrac) &&
542 "Token is not a Left Bracket");
543 S = Parser.getTok().getLoc();
544 Parser.Lex(); // Eat left bracket token.
546 const AsmToken &BaseRegTok = Parser.getTok();
547 if (BaseRegTok.isNot(AsmToken::Identifier)) {
548 Error(BaseRegTok.getLoc(), "register expected");
551 int BaseRegNum = TryParseRegister();
552 if (BaseRegNum == -1) {
553 Error(BaseRegTok.getLoc(), "register expected");
557 bool Preindexed = false;
558 bool Postindexed = false;
559 bool OffsetIsReg = false;
560 bool Negative = false;
561 bool Writeback = false;
563 // First look for preindexed address forms, that is after the "[Rn" we now
564 // have to see if the next token is a comma.
565 const AsmToken &Tok = Parser.getTok();
566 if (Tok.is(AsmToken::Comma)) {
568 Parser.Lex(); // Eat comma token.
570 bool OffsetRegShifted;
571 enum ShiftType ShiftType;
572 const MCExpr *ShiftAmount;
573 const MCExpr *Offset;
574 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
575 Offset, OffsetIsReg, OffsetRegNum, E))
577 const AsmToken &RBracTok = Parser.getTok();
578 if (RBracTok.isNot(AsmToken::RBrac)) {
579 Error(RBracTok.getLoc(), "']' expected");
582 E = RBracTok.getLoc();
583 Parser.Lex(); // Eat right bracket token.
585 const AsmToken &ExclaimTok = Parser.getTok();
586 if (ExclaimTok.is(AsmToken::Exclaim)) {
587 E = ExclaimTok.getLoc();
589 Parser.Lex(); // Eat exclaim token
591 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
592 OffsetRegShifted, ShiftType, ShiftAmount,
593 Preindexed, Postindexed, Negative, Writeback,
596 // The "[Rn" we have so far was not followed by a comma.
597 else if (Tok.is(AsmToken::RBrac)) {
598 // If there's anything other than the right brace, this is a post indexing
601 Parser.Lex(); // Eat right bracket token.
603 int OffsetRegNum = 0;
604 bool OffsetRegShifted = false;
605 enum ShiftType ShiftType;
606 const MCExpr *ShiftAmount;
607 const MCExpr *Offset = 0;
609 const AsmToken &NextTok = Parser.getTok();
610 if (NextTok.isNot(AsmToken::EndOfStatement)) {
613 if (NextTok.isNot(AsmToken::Comma)) {
614 Error(NextTok.getLoc(), "',' expected");
617 Parser.Lex(); // Eat comma token.
618 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
619 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
624 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
625 OffsetRegShifted, ShiftType, ShiftAmount,
626 Preindexed, Postindexed, Negative, Writeback,
633 /// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
634 /// we will parse the following (were +/- means that a plus or minus is
639 /// we return false on success or an error otherwise.
640 bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
641 bool &OffsetRegShifted,
642 enum ShiftType &ShiftType,
643 const MCExpr *&ShiftAmount,
644 const MCExpr *&Offset,
649 OffsetRegShifted = false;
652 const AsmToken &NextTok = Parser.getTok();
653 E = NextTok.getLoc();
654 if (NextTok.is(AsmToken::Plus))
655 Parser.Lex(); // Eat plus token.
656 else if (NextTok.is(AsmToken::Minus)) {
658 Parser.Lex(); // Eat minus token
660 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
661 const AsmToken &OffsetRegTok = Parser.getTok();
662 if (OffsetRegTok.is(AsmToken::Identifier)) {
663 SMLoc CurLoc = OffsetRegTok.getLoc();
664 OffsetRegNum = TryParseRegister();
665 if (OffsetRegNum != -1) {
671 // If we parsed a register as the offset then there can be a shift after that.
672 if (OffsetRegNum != -1) {
673 // Look for a comma then a shift
674 const AsmToken &Tok = Parser.getTok();
675 if (Tok.is(AsmToken::Comma)) {
676 Parser.Lex(); // Eat comma token.
678 const AsmToken &Tok = Parser.getTok();
679 if (ParseShift(ShiftType, ShiftAmount, E))
680 return Error(Tok.getLoc(), "shift expected");
681 OffsetRegShifted = true;
684 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
685 // Look for #offset following the "[Rn," or "[Rn],"
686 const AsmToken &HashTok = Parser.getTok();
687 if (HashTok.isNot(AsmToken::Hash))
688 return Error(HashTok.getLoc(), "'#' expected");
690 Parser.Lex(); // Eat hash token.
692 if (getParser().ParseExpression(Offset))
694 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
699 /// ParseShift as one of these two:
700 /// ( lsl | lsr | asr | ror ) , # shift_amount
702 /// and returns true if it parses a shift otherwise it returns false.
703 bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
705 const AsmToken &Tok = Parser.getTok();
706 if (Tok.isNot(AsmToken::Identifier))
708 StringRef ShiftName = Tok.getString();
709 if (ShiftName == "lsl" || ShiftName == "LSL")
711 else if (ShiftName == "lsr" || ShiftName == "LSR")
713 else if (ShiftName == "asr" || ShiftName == "ASR")
715 else if (ShiftName == "ror" || ShiftName == "ROR")
717 else if (ShiftName == "rrx" || ShiftName == "RRX")
721 Parser.Lex(); // Eat shift type token.
727 // Otherwise, there must be a '#' and a shift amount.
728 const AsmToken &HashTok = Parser.getTok();
729 if (HashTok.isNot(AsmToken::Hash))
730 return Error(HashTok.getLoc(), "'#' expected");
731 Parser.Lex(); // Eat hash token.
733 if (getParser().ParseExpression(ShiftAmount))
739 /// Parse a arm instruction operand. For now this parses the operand regardless
741 ARMOperand *ARMAsmParser::ParseOperand() {
743 switch (getLexer().getKind()) {
745 Error(Parser.getTok().getLoc(), "unexpected token in operand");
747 case AsmToken::Identifier:
748 if (ARMOperand *Op = TryParseRegisterWithWriteBack())
751 // This was not a register so parse other operands that start with an
752 // identifier (like labels) as expressions and create them as immediates.
754 S = Parser.getTok().getLoc();
755 if (getParser().ParseExpression(IdVal))
757 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
758 return ARMOperand::CreateImm(IdVal, S, E);
759 case AsmToken::LBrac:
760 return ParseMemory();
761 case AsmToken::LCurly:
762 return ParseRegisterList();
765 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
766 S = Parser.getTok().getLoc();
768 const MCExpr *ImmVal;
769 if (getParser().ParseExpression(ImmVal))
771 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
772 return ARMOperand::CreateImm(ImmVal, S, E);
776 /// Parse an arm instruction mnemonic followed by its operands.
777 bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
778 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
779 // Create the leading tokens for the mnemonic, split by '.' characters.
780 size_t Start = 0, Next = Name.find('.');
781 StringRef Head = Name.slice(Start, Next);
783 // Determine the predicate, if any.
785 // FIXME: We need a way to check whether a prefix supports predication,
786 // otherwise we will end up with an ambiguity for instructions that happen to
787 // end with a predicate name.
788 // FIXME: Likewise, some arithmetic instructions have an 's' prefix which
789 // indicates to update the condition codes. Those instructions have an
790 // additional immediate operand which encodes the prefix as reg0 or CPSR.
791 // Just checking for a suffix of 's' definitely creates ambiguities; e.g,
792 // the SMMLS instruction.
793 unsigned CC = StringSwitch<unsigned>(Head.substr(Head.size()-2))
794 .Case("eq", ARMCC::EQ)
795 .Case("ne", ARMCC::NE)
796 .Case("hs", ARMCC::HS)
797 .Case("lo", ARMCC::LO)
798 .Case("mi", ARMCC::MI)
799 .Case("pl", ARMCC::PL)
800 .Case("vs", ARMCC::VS)
801 .Case("vc", ARMCC::VC)
802 .Case("hi", ARMCC::HI)
803 .Case("ls", ARMCC::LS)
804 .Case("ge", ARMCC::GE)
805 .Case("lt", ARMCC::LT)
806 .Case("gt", ARMCC::GT)
807 .Case("le", ARMCC::LE)
808 .Case("al", ARMCC::AL)
812 (CC == ARMCC::LS && (Head == "vmls" || Head == "vnmls"))) {
815 Head = Head.slice(0, Head.size() - 2);
818 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
819 // FIXME: Should only add this operand for predicated instructions
820 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), NameLoc));
822 // Add the remaining tokens in the mnemonic.
823 while (Next != StringRef::npos) {
825 Next = Name.find('.', Start + 1);
826 Head = Name.slice(Start, Next);
828 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
831 // Read the remaining operands.
832 if (getLexer().isNot(AsmToken::EndOfStatement)) {
833 // Read the first operand.
834 if (ARMOperand *Op = ParseOperand())
835 Operands.push_back(Op);
837 Parser.EatToEndOfStatement();
841 while (getLexer().is(AsmToken::Comma)) {
842 Parser.Lex(); // Eat the comma.
844 // Parse and remember the operand.
845 if (ARMOperand *Op = ParseOperand())
846 Operands.push_back(Op);
848 Parser.EatToEndOfStatement();
854 if (getLexer().isNot(AsmToken::EndOfStatement)) {
855 Parser.EatToEndOfStatement();
856 return TokError("unexpected token in argument list");
859 Parser.Lex(); // Consume the EndOfStatement
864 MatchAndEmitInstruction(SMLoc IDLoc,
865 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
869 switch (MatchInstructionImpl(Operands, Inst, ErrorInfo)) {
871 Out.EmitInstruction(Inst);
873 case Match_MissingFeature:
874 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
876 case Match_InvalidOperand: {
877 SMLoc ErrorLoc = IDLoc;
878 if (ErrorInfo != ~0U) {
879 if (ErrorInfo >= Operands.size())
880 return Error(IDLoc, "too few operands for instruction");
882 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
883 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
886 return Error(ErrorLoc, "invalid operand for instruction");
888 case Match_MnemonicFail:
889 return Error(IDLoc, "unrecognized instruction mnemonic");
892 llvm_unreachable("Implement any new match types added!");
896 /// ParseDirective parses the arm specific directives
897 bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
898 StringRef IDVal = DirectiveID.getIdentifier();
899 if (IDVal == ".word")
900 return ParseDirectiveWord(4, DirectiveID.getLoc());
901 else if (IDVal == ".thumb")
902 return ParseDirectiveThumb(DirectiveID.getLoc());
903 else if (IDVal == ".thumb_func")
904 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
905 else if (IDVal == ".code")
906 return ParseDirectiveCode(DirectiveID.getLoc());
907 else if (IDVal == ".syntax")
908 return ParseDirectiveSyntax(DirectiveID.getLoc());
912 /// ParseDirectiveWord
913 /// ::= .word [ expression (, expression)* ]
914 bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
915 if (getLexer().isNot(AsmToken::EndOfStatement)) {
918 if (getParser().ParseExpression(Value))
921 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
923 if (getLexer().is(AsmToken::EndOfStatement))
926 // FIXME: Improve diagnostic.
927 if (getLexer().isNot(AsmToken::Comma))
928 return Error(L, "unexpected token in directive");
937 /// ParseDirectiveThumb
939 bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
940 if (getLexer().isNot(AsmToken::EndOfStatement))
941 return Error(L, "unexpected token in directive");
944 // TODO: set thumb mode
945 // TODO: tell the MC streamer the mode
946 // getParser().getStreamer().Emit???();
950 /// ParseDirectiveThumbFunc
951 /// ::= .thumbfunc symbol_name
952 bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
953 const AsmToken &Tok = Parser.getTok();
954 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
955 return Error(L, "unexpected token in .thumb_func directive");
956 StringRef Name = Tok.getString();
957 Parser.Lex(); // Consume the identifier token.
958 if (getLexer().isNot(AsmToken::EndOfStatement))
959 return Error(L, "unexpected token in directive");
962 // Mark symbol as a thumb symbol.
963 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
964 getParser().getStreamer().EmitThumbFunc(Func);
968 /// ParseDirectiveSyntax
969 /// ::= .syntax unified | divided
970 bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
971 const AsmToken &Tok = Parser.getTok();
972 if (Tok.isNot(AsmToken::Identifier))
973 return Error(L, "unexpected token in .syntax directive");
974 StringRef Mode = Tok.getString();
975 if (Mode == "unified" || Mode == "UNIFIED")
977 else if (Mode == "divided" || Mode == "DIVIDED")
980 return Error(L, "unrecognized syntax mode in .syntax directive");
982 if (getLexer().isNot(AsmToken::EndOfStatement))
983 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
986 // TODO tell the MC streamer the mode
987 // getParser().getStreamer().Emit???();
991 /// ParseDirectiveCode
992 /// ::= .code 16 | 32
993 bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
994 const AsmToken &Tok = Parser.getTok();
995 if (Tok.isNot(AsmToken::Integer))
996 return Error(L, "unexpected token in .code directive");
997 int64_t Val = Parser.getTok().getIntVal();
1003 return Error(L, "invalid operand to .code directive");
1005 if (getLexer().isNot(AsmToken::EndOfStatement))
1006 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
1010 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
1012 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1017 extern "C" void LLVMInitializeARMAsmLexer();
1019 /// Force static initialization.
1020 extern "C" void LLVMInitializeARMAsmParser() {
1021 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1022 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
1023 LLVMInitializeARMAsmLexer();
1026 #define GET_REGISTER_MATCHER
1027 #define GET_MATCHER_IMPLEMENTATION
1028 #include "ARMGenAsmMatcher.inc"