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 bool TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
54 bool ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
55 bool ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &);
56 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &);
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 {
115 SMLoc StartLoc, EndLoc;
116 SmallVector<unsigned, 8> Registers;
120 ARMCC::CondCodes Val;
136 // This is for all forms of ARM address expressions
139 unsigned OffsetRegNum; // used when OffsetIsReg is true
140 const MCExpr *Offset; // used when OffsetIsReg is false
141 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
142 enum ShiftType ShiftType; // used when OffsetRegShifted is true
143 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
144 unsigned Preindexed : 1;
145 unsigned Postindexed : 1;
146 unsigned OffsetIsReg : 1;
147 unsigned Negative : 1; // only used when OffsetIsReg is true
148 unsigned Writeback : 1;
152 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
154 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
156 StartLoc = o.StartLoc;
169 case DPRRegisterList:
170 case SPRRegisterList:
171 Registers = o.Registers;
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 SmallVectorImpl<unsigned> &getRegList() const {
203 assert((Kind == RegisterList || Kind == DPRRegisterList ||
204 Kind == SPRRegisterList) && "Invalid access!");
208 const MCExpr *getImm() const {
209 assert(Kind == Immediate && "Invalid access!");
213 bool isCondCode() const { return Kind == CondCode; }
214 bool isImm() const { return Kind == Immediate; }
215 bool isReg() const { return Kind == Register; }
216 bool isRegList() const { return Kind == RegisterList; }
217 bool isDPRRegList() const { return Kind == DPRRegisterList; }
218 bool isSPRRegList() const { return Kind == SPRRegisterList; }
219 bool isToken() const { return Kind == Token; }
220 bool isMemory() const { return Kind == Memory; }
221 bool isMemMode5() const {
222 if (!isMemory() || Mem.OffsetIsReg || Mem.OffsetRegShifted ||
223 Mem.Writeback || Mem.Negative)
226 // If there is an offset expression, make sure it's valid.
227 if (!Mem.Offset) return true;
229 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
230 if (!CE) return false;
232 // The offset must be a multiple of 4 in the range 0-1020.
233 int64_t Value = CE->getValue();
234 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
236 bool isMemModeThumb() const {
237 if (!isMemory() || (!Mem.OffsetIsReg && !Mem.Offset) || Mem.Writeback)
240 if (!Mem.Offset) return true;
242 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
243 if (!CE) return false;
245 // The offset must be a multiple of 4 in the range 0-124.
246 uint64_t Value = CE->getValue();
247 return ((Value & 0x3) == 0 && Value <= 124);
250 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
251 // Add as immediates when possible. Null MCExpr = 0.
253 Inst.addOperand(MCOperand::CreateImm(0));
254 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
255 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
257 Inst.addOperand(MCOperand::CreateExpr(Expr));
260 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
261 assert(N == 2 && "Invalid number of operands!");
262 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
263 // FIXME: What belongs here?
264 Inst.addOperand(MCOperand::CreateReg(0));
267 void addRegOperands(MCInst &Inst, unsigned N) const {
268 assert(N == 1 && "Invalid number of operands!");
269 Inst.addOperand(MCOperand::CreateReg(getReg()));
272 void addRegListOperands(MCInst &Inst, unsigned N) const {
273 assert(N == 1 && "Invalid number of operands!");
274 const SmallVectorImpl<unsigned> &RegList = getRegList();
275 for (SmallVectorImpl<unsigned>::const_iterator
276 I = RegList.begin(), E = RegList.end(); I != E; ++I)
277 Inst.addOperand(MCOperand::CreateReg(*I));
280 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
281 addRegListOperands(Inst, N);
284 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
285 addRegListOperands(Inst, N);
288 void addImmOperands(MCInst &Inst, unsigned N) const {
289 assert(N == 1 && "Invalid number of operands!");
290 addExpr(Inst, getImm());
293 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
294 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
296 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
297 assert(!Mem.OffsetIsReg && "Invalid mode 5 operand");
299 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
302 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
303 assert(CE && "Non-constant mode 5 offset operand!");
305 // The MCInst offset operand doesn't include the low two bits (like
306 // the instruction encoding).
307 int64_t Offset = CE->getValue() / 4;
309 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
312 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
315 Inst.addOperand(MCOperand::CreateImm(0));
319 void addMemModeThumbOperands(MCInst &Inst, unsigned N) const {
320 assert(N == 3 && isMemModeThumb() && "Invalid number of operands!");
321 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
324 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
325 assert(CE && "Non-constant mode offset operand!");
326 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
327 Inst.addOperand(MCOperand::CreateReg(0));
329 Inst.addOperand(MCOperand::CreateImm(0));
330 Inst.addOperand(MCOperand::CreateReg(Mem.OffsetRegNum));
334 virtual void dump(raw_ostream &OS) const;
336 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
337 ARMOperand *Op = new ARMOperand(CondCode);
344 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
345 ARMOperand *Op = new ARMOperand(Token);
346 Op->Tok.Data = Str.data();
347 Op->Tok.Length = Str.size();
353 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
354 ARMOperand *Op = new ARMOperand(Register);
355 Op->Reg.RegNum = RegNum;
362 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
363 SMLoc StartLoc, SMLoc EndLoc) {
364 KindTy Kind = RegisterList;
366 if (ARM::DPRRegClass.contains(Regs.front().first))
367 Kind = DPRRegisterList;
368 else if (ARM::SPRRegClass.contains(Regs.front().first))
369 Kind = SPRRegisterList;
371 ARMOperand *Op = new ARMOperand(Kind);
372 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
373 I = Regs.begin(), E = Regs.end(); I != E; ++I)
374 Op->Registers.push_back(I->first);
375 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
376 Op->StartLoc = StartLoc;
381 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
382 ARMOperand *Op = new ARMOperand(Immediate);
389 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
390 const MCExpr *Offset, unsigned OffsetRegNum,
391 bool OffsetRegShifted, enum ShiftType ShiftType,
392 const MCExpr *ShiftAmount, bool Preindexed,
393 bool Postindexed, bool Negative, bool Writeback,
395 ARMOperand *Op = new ARMOperand(Memory);
396 Op->Mem.BaseRegNum = BaseRegNum;
397 Op->Mem.OffsetIsReg = OffsetIsReg;
398 Op->Mem.Offset = Offset;
399 Op->Mem.OffsetRegNum = OffsetRegNum;
400 Op->Mem.OffsetRegShifted = OffsetRegShifted;
401 Op->Mem.ShiftType = ShiftType;
402 Op->Mem.ShiftAmount = ShiftAmount;
403 Op->Mem.Preindexed = Preindexed;
404 Op->Mem.Postindexed = Postindexed;
405 Op->Mem.Negative = Negative;
406 Op->Mem.Writeback = Writeback;
414 } // end anonymous namespace.
416 void ARMOperand::dump(raw_ostream &OS) const {
419 OS << ARMCondCodeToString(getCondCode());
428 OS << "<register " << getReg() << ">";
431 case DPRRegisterList:
432 case SPRRegisterList: {
433 OS << "<register_list ";
435 const SmallVectorImpl<unsigned> &RegList = getRegList();
436 for (SmallVectorImpl<unsigned>::const_iterator
437 I = RegList.begin(), E = RegList.end(); I != E; ) {
439 if (++I < E) OS << ", ";
446 OS << "'" << getToken() << "'";
451 /// @name Auto-generated Match Functions
454 static unsigned MatchRegisterName(StringRef Name);
458 /// Try to parse a register name. The token must be an Identifier when called,
459 /// and if it is a register name the token is eaten and the register number is
460 /// returned. Otherwise return -1.
462 int ARMAsmParser::TryParseRegister() {
463 const AsmToken &Tok = Parser.getTok();
464 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
466 // FIXME: Validate register for the current architecture; we have to do
467 // validation later, so maybe there is no need for this here.
468 unsigned RegNum = MatchRegisterName(Tok.getString());
471 Parser.Lex(); // Eat identifier token.
476 /// Try to parse a register name. The token must be an Identifier when called.
477 /// If it's a register, an AsmOperand is created. Another AsmOperand is created
478 /// if there is a "writeback". 'true' if it's not a register.
480 /// TODO this is likely to change to allow different register types and or to
481 /// parse for a specific register type.
483 TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
484 SMLoc S = Parser.getTok().getLoc();
485 int RegNo = TryParseRegister();
489 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
491 const AsmToken &ExclaimTok = Parser.getTok();
492 if (ExclaimTok.is(AsmToken::Exclaim)) {
493 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
494 ExclaimTok.getLoc()));
495 Parser.Lex(); // Eat exclaim token
501 /// Parse a register list, return it if successful else return null. The first
502 /// token must be a '{' when called.
504 ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
505 assert(Parser.getTok().is(AsmToken::LCurly) &&
506 "Token is not a Left Curly Brace");
507 SMLoc S = Parser.getTok().getLoc();
509 // Read the rest of the registers in the list.
510 unsigned PrevRegNum = 0;
511 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
514 bool IsRange = Parser.getTok().is(AsmToken::Minus);
515 Parser.Lex(); // Eat non-identifier token.
517 const AsmToken &RegTok = Parser.getTok();
518 SMLoc RegLoc = RegTok.getLoc();
519 if (RegTok.isNot(AsmToken::Identifier)) {
520 Error(RegLoc, "register expected");
524 int RegNum = TryParseRegister();
526 Error(RegLoc, "register expected");
531 int Reg = PrevRegNum;
534 Registers.push_back(std::make_pair(Reg, RegLoc));
535 } while (Reg != RegNum);
537 Registers.push_back(std::make_pair(RegNum, RegLoc));
541 } while (Parser.getTok().is(AsmToken::Comma) ||
542 Parser.getTok().is(AsmToken::Minus));
544 // Process the right curly brace of the list.
545 const AsmToken &RCurlyTok = Parser.getTok();
546 if (RCurlyTok.isNot(AsmToken::RCurly)) {
547 Error(RCurlyTok.getLoc(), "'}' expected");
551 SMLoc E = RCurlyTok.getLoc();
552 Parser.Lex(); // Eat right curly brace token.
554 // Verify the register list.
555 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
556 RI = Registers.begin(), RE = Registers.end();
558 DenseMap<unsigned, bool> RegMap;
559 RegMap[RI->first] = true;
561 unsigned HighRegNum = RI->first;
562 bool EmittedWarning = false;
564 for (++RI; RI != RE; ++RI) {
565 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
566 unsigned Reg = RegInfo.first;
569 Error(RegInfo.second, "register duplicated in register list");
573 if (!EmittedWarning && Reg < HighRegNum)
574 Warning(RegInfo.second,
575 "register not in ascending order in register list");
578 HighRegNum = std::max(Reg, HighRegNum);
581 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
585 /// Parse an ARM memory expression, return false if successful else return true
586 /// or an error. The first token must be a '[' when called.
588 /// TODO Only preindexing and postindexing addressing are started, unindexed
589 /// with option, etc are still to do.
591 ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
593 assert(Parser.getTok().is(AsmToken::LBrac) &&
594 "Token is not a Left Bracket");
595 S = Parser.getTok().getLoc();
596 Parser.Lex(); // Eat left bracket token.
598 const AsmToken &BaseRegTok = Parser.getTok();
599 if (BaseRegTok.isNot(AsmToken::Identifier)) {
600 Error(BaseRegTok.getLoc(), "register expected");
603 int BaseRegNum = TryParseRegister();
604 if (BaseRegNum == -1) {
605 Error(BaseRegTok.getLoc(), "register expected");
609 bool Preindexed = false;
610 bool Postindexed = false;
611 bool OffsetIsReg = false;
612 bool Negative = false;
613 bool Writeback = false;
615 // First look for preindexed address forms, that is after the "[Rn" we now
616 // have to see if the next token is a comma.
617 const AsmToken &Tok = Parser.getTok();
618 if (Tok.is(AsmToken::Comma)) {
620 Parser.Lex(); // Eat comma token.
622 bool OffsetRegShifted;
623 enum ShiftType ShiftType;
624 const MCExpr *ShiftAmount = 0;
625 const MCExpr *Offset = 0;
626 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
627 Offset, OffsetIsReg, OffsetRegNum, E))
629 const AsmToken &RBracTok = Parser.getTok();
630 if (RBracTok.isNot(AsmToken::RBrac)) {
631 Error(RBracTok.getLoc(), "']' expected");
634 E = RBracTok.getLoc();
635 Parser.Lex(); // Eat right bracket token.
638 const AsmToken &ExclaimTok = Parser.getTok();
639 ARMOperand *WBOp = 0;
640 if (ExclaimTok.is(AsmToken::Exclaim)) {
641 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
642 ExclaimTok.getLoc());
644 Parser.Lex(); // Eat exclaim token
647 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
648 OffsetRegNum, OffsetRegShifted,
649 ShiftType, ShiftAmount, Preindexed,
650 Postindexed, Negative, Writeback,
653 Operands.push_back(WBOp);
657 // The "[Rn" we have so far was not followed by a comma.
658 else if (Tok.is(AsmToken::RBrac)) {
659 // If there's anything other than the right brace, this is a post indexing
662 Parser.Lex(); // Eat right bracket token.
664 int OffsetRegNum = 0;
665 bool OffsetRegShifted = false;
666 enum ShiftType ShiftType = Lsl;
667 const MCExpr *ShiftAmount = 0;
668 const MCExpr *Offset = 0;
670 const AsmToken &NextTok = Parser.getTok();
672 if (NextTok.isNot(AsmToken::EndOfStatement)) {
676 if (NextTok.isNot(AsmToken::Comma)) {
677 Error(NextTok.getLoc(), "',' expected");
681 Parser.Lex(); // Eat comma token.
683 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
684 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
689 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
690 OffsetRegNum, OffsetRegShifted,
691 ShiftType, ShiftAmount, Preindexed,
692 Postindexed, Negative, Writeback,
700 /// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
701 /// we will parse the following (were +/- means that a plus or minus is
706 /// we return false on success or an error otherwise.
707 bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
708 bool &OffsetRegShifted,
709 enum ShiftType &ShiftType,
710 const MCExpr *&ShiftAmount,
711 const MCExpr *&Offset,
716 OffsetRegShifted = false;
719 const AsmToken &NextTok = Parser.getTok();
720 E = NextTok.getLoc();
721 if (NextTok.is(AsmToken::Plus))
722 Parser.Lex(); // Eat plus token.
723 else if (NextTok.is(AsmToken::Minus)) {
725 Parser.Lex(); // Eat minus token
727 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
728 const AsmToken &OffsetRegTok = Parser.getTok();
729 if (OffsetRegTok.is(AsmToken::Identifier)) {
730 SMLoc CurLoc = OffsetRegTok.getLoc();
731 OffsetRegNum = TryParseRegister();
732 if (OffsetRegNum != -1) {
738 // If we parsed a register as the offset then there can be a shift after that.
739 if (OffsetRegNum != -1) {
740 // Look for a comma then a shift
741 const AsmToken &Tok = Parser.getTok();
742 if (Tok.is(AsmToken::Comma)) {
743 Parser.Lex(); // Eat comma token.
745 const AsmToken &Tok = Parser.getTok();
746 if (ParseShift(ShiftType, ShiftAmount, E))
747 return Error(Tok.getLoc(), "shift expected");
748 OffsetRegShifted = true;
751 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
752 // Look for #offset following the "[Rn," or "[Rn],"
753 const AsmToken &HashTok = Parser.getTok();
754 if (HashTok.isNot(AsmToken::Hash))
755 return Error(HashTok.getLoc(), "'#' expected");
757 Parser.Lex(); // Eat hash token.
759 if (getParser().ParseExpression(Offset))
761 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
766 /// ParseShift as one of these two:
767 /// ( lsl | lsr | asr | ror ) , # shift_amount
769 /// and returns true if it parses a shift otherwise it returns false.
770 bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
772 const AsmToken &Tok = Parser.getTok();
773 if (Tok.isNot(AsmToken::Identifier))
775 StringRef ShiftName = Tok.getString();
776 if (ShiftName == "lsl" || ShiftName == "LSL")
778 else if (ShiftName == "lsr" || ShiftName == "LSR")
780 else if (ShiftName == "asr" || ShiftName == "ASR")
782 else if (ShiftName == "ror" || ShiftName == "ROR")
784 else if (ShiftName == "rrx" || ShiftName == "RRX")
788 Parser.Lex(); // Eat shift type token.
794 // Otherwise, there must be a '#' and a shift amount.
795 const AsmToken &HashTok = Parser.getTok();
796 if (HashTok.isNot(AsmToken::Hash))
797 return Error(HashTok.getLoc(), "'#' expected");
798 Parser.Lex(); // Eat hash token.
800 if (getParser().ParseExpression(ShiftAmount))
806 /// Parse a arm instruction operand. For now this parses the operand regardless
808 bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands){
810 switch (getLexer().getKind()) {
812 Error(Parser.getTok().getLoc(), "unexpected token in operand");
814 case AsmToken::Identifier: {
815 if (!TryParseRegisterWithWriteBack(Operands))
818 // This was not a register so parse other operands that start with an
819 // identifier (like labels) as expressions and create them as immediates.
821 S = Parser.getTok().getLoc();
822 if (getParser().ParseExpression(IdVal))
824 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
825 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
828 case AsmToken::LBrac:
829 return ParseMemory(Operands);
830 case AsmToken::LCurly:
831 return ParseRegisterList(Operands);
834 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
835 S = Parser.getTok().getLoc();
837 const MCExpr *ImmVal;
838 if (getParser().ParseExpression(ImmVal))
840 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
841 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
846 /// Parse an arm instruction mnemonic followed by its operands.
847 bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
848 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
849 // Create the leading tokens for the mnemonic, split by '.' characters.
850 size_t Start = 0, Next = Name.find('.');
851 StringRef Head = Name.slice(Start, Next);
853 // Determine the predicate, if any.
855 // FIXME: We need a way to check whether a prefix supports predication,
856 // otherwise we will end up with an ambiguity for instructions that happen to
857 // end with a predicate name.
858 // FIXME: Likewise, some arithmetic instructions have an 's' prefix which
859 // indicates to update the condition codes. Those instructions have an
860 // additional immediate operand which encodes the prefix as reg0 or CPSR.
861 // Just checking for a suffix of 's' definitely creates ambiguities; e.g,
862 // the SMMLS instruction.
863 unsigned CC = StringSwitch<unsigned>(Head.substr(Head.size()-2))
864 .Case("eq", ARMCC::EQ)
865 .Case("ne", ARMCC::NE)
866 .Case("hs", ARMCC::HS)
867 .Case("lo", ARMCC::LO)
868 .Case("mi", ARMCC::MI)
869 .Case("pl", ARMCC::PL)
870 .Case("vs", ARMCC::VS)
871 .Case("vc", ARMCC::VC)
872 .Case("hi", ARMCC::HI)
873 .Case("ls", ARMCC::LS)
874 .Case("ge", ARMCC::GE)
875 .Case("lt", ARMCC::LT)
876 .Case("gt", ARMCC::GT)
877 .Case("le", ARMCC::LE)
878 .Case("al", ARMCC::AL)
882 (CC == ARMCC::LS && (Head == "vmls" || Head == "vnmls"))) {
885 Head = Head.slice(0, Head.size() - 2);
888 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
891 // FIXME: Should only add this operand for predicated instructions
892 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC),
895 // Add the remaining tokens in the mnemonic.
896 while (Next != StringRef::npos) {
898 Next = Name.find('.', Start + 1);
899 Head = Name.slice(Start, Next);
901 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
904 // Read the remaining operands.
905 if (getLexer().isNot(AsmToken::EndOfStatement)) {
906 // Read the first operand.
907 if (ParseOperand(Operands)) {
908 Parser.EatToEndOfStatement();
912 while (getLexer().is(AsmToken::Comma)) {
913 Parser.Lex(); // Eat the comma.
915 // Parse and remember the operand.
916 if (ParseOperand(Operands)) {
917 Parser.EatToEndOfStatement();
923 if (getLexer().isNot(AsmToken::EndOfStatement)) {
924 Parser.EatToEndOfStatement();
925 return TokError("unexpected token in argument list");
928 Parser.Lex(); // Consume the EndOfStatement
933 MatchAndEmitInstruction(SMLoc IDLoc,
934 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
938 switch (MatchInstructionImpl(Operands, Inst, ErrorInfo)) {
940 Out.EmitInstruction(Inst);
942 case Match_MissingFeature:
943 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
945 case Match_InvalidOperand: {
946 SMLoc ErrorLoc = IDLoc;
947 if (ErrorInfo != ~0U) {
948 if (ErrorInfo >= Operands.size())
949 return Error(IDLoc, "too few operands for instruction");
951 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
952 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
955 return Error(ErrorLoc, "invalid operand for instruction");
957 case Match_MnemonicFail:
958 return Error(IDLoc, "unrecognized instruction mnemonic");
961 llvm_unreachable("Implement any new match types added!");
965 /// ParseDirective parses the arm specific directives
966 bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
967 StringRef IDVal = DirectiveID.getIdentifier();
968 if (IDVal == ".word")
969 return ParseDirectiveWord(4, DirectiveID.getLoc());
970 else if (IDVal == ".thumb")
971 return ParseDirectiveThumb(DirectiveID.getLoc());
972 else if (IDVal == ".thumb_func")
973 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
974 else if (IDVal == ".code")
975 return ParseDirectiveCode(DirectiveID.getLoc());
976 else if (IDVal == ".syntax")
977 return ParseDirectiveSyntax(DirectiveID.getLoc());
981 /// ParseDirectiveWord
982 /// ::= .word [ expression (, expression)* ]
983 bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
984 if (getLexer().isNot(AsmToken::EndOfStatement)) {
987 if (getParser().ParseExpression(Value))
990 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
992 if (getLexer().is(AsmToken::EndOfStatement))
995 // FIXME: Improve diagnostic.
996 if (getLexer().isNot(AsmToken::Comma))
997 return Error(L, "unexpected token in directive");
1006 /// ParseDirectiveThumb
1008 bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
1009 if (getLexer().isNot(AsmToken::EndOfStatement))
1010 return Error(L, "unexpected token in directive");
1013 // TODO: set thumb mode
1014 // TODO: tell the MC streamer the mode
1015 // getParser().getStreamer().Emit???();
1019 /// ParseDirectiveThumbFunc
1020 /// ::= .thumbfunc symbol_name
1021 bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
1022 const AsmToken &Tok = Parser.getTok();
1023 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
1024 return Error(L, "unexpected token in .thumb_func directive");
1025 StringRef Name = Tok.getString();
1026 Parser.Lex(); // Consume the identifier token.
1027 if (getLexer().isNot(AsmToken::EndOfStatement))
1028 return Error(L, "unexpected token in directive");
1031 // Mark symbol as a thumb symbol.
1032 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
1033 getParser().getStreamer().EmitThumbFunc(Func);
1037 /// ParseDirectiveSyntax
1038 /// ::= .syntax unified | divided
1039 bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
1040 const AsmToken &Tok = Parser.getTok();
1041 if (Tok.isNot(AsmToken::Identifier))
1042 return Error(L, "unexpected token in .syntax directive");
1043 StringRef Mode = Tok.getString();
1044 if (Mode == "unified" || Mode == "UNIFIED")
1046 else if (Mode == "divided" || Mode == "DIVIDED")
1049 return Error(L, "unrecognized syntax mode in .syntax directive");
1051 if (getLexer().isNot(AsmToken::EndOfStatement))
1052 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
1055 // TODO tell the MC streamer the mode
1056 // getParser().getStreamer().Emit???();
1060 /// ParseDirectiveCode
1061 /// ::= .code 16 | 32
1062 bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
1063 const AsmToken &Tok = Parser.getTok();
1064 if (Tok.isNot(AsmToken::Integer))
1065 return Error(L, "unexpected token in .code directive");
1066 int64_t Val = Parser.getTok().getIntVal();
1072 return Error(L, "invalid operand to .code directive");
1074 if (getLexer().isNot(AsmToken::EndOfStatement))
1075 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
1079 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
1081 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1086 extern "C" void LLVMInitializeARMAsmLexer();
1088 /// Force static initialization.
1089 extern "C" void LLVMInitializeARMAsmParser() {
1090 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1091 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
1092 LLVMInitializeARMAsmLexer();
1095 #define GET_REGISTER_MATCHER
1096 #define GET_MATCHER_IMPLEMENTATION
1097 #include "ARMGenAsmMatcher.inc"