1 //===-- MipsAsmParser.cpp - Parse Mips 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 //===----------------------------------------------------------------------===//
10 #include "MCTargetDesc/MipsMCTargetDesc.h"
11 #include "MipsRegisterInfo.h"
12 #include "llvm/ADT/StringSwitch.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCExpr.h"
15 #include "llvm/MC/MCInst.h"
16 #include "llvm/MC/MCStreamer.h"
17 #include "llvm/MC/MCSubtargetInfo.h"
18 #include "llvm/MC/MCSymbol.h"
19 #include "llvm/MC/MCParser/MCAsmLexer.h"
20 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
21 #include "llvm/MC/MCTargetAsmParser.h"
22 #include "llvm/Support/TargetRegistry.h"
27 class MipsAssemblerOptions {
29 MipsAssemblerOptions():
30 aTReg(1), reorder(true), macro(true) {
33 unsigned getATRegNum() {return aTReg;}
34 bool setATReg(unsigned Reg);
36 bool isReorder() {return reorder;}
37 void setReorder() {reorder = true;}
38 void setNoreorder() {reorder = false;}
40 bool isMacro() {return macro;}
41 void setMacro() {macro = true;}
42 void setNomacro() {macro = false;}
52 class MipsAsmParser : public MCTargetAsmParser {
64 MipsAssemblerOptions *Options;
67 #define GET_ASSEMBLER_HEADER
68 #include "MipsGenAsmMatcher.inc"
70 bool MatchAndEmitInstruction(SMLoc IDLoc,
71 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
74 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
76 bool ParseInstruction(StringRef Name, SMLoc NameLoc,
77 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
79 bool parseMathOperation(StringRef Name, SMLoc NameLoc,
80 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
82 bool ParseDirective(AsmToken DirectiveID);
84 MipsAsmParser::OperandMatchResultTy
85 parseMemOperand(SmallVectorImpl<MCParsedAsmOperand*>&);
87 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &,
90 int tryParseRegister(StringRef Mnemonic);
92 bool tryParseRegisterOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
95 bool reportParseError(StringRef ErrorMsg);
97 bool parseMemOffset(const MCExpr *&Res);
98 bool parseRelocOperand(const MCExpr *&Res);
100 bool parseDirectiveSet();
102 bool parseSetAtDirective();
103 bool parseSetNoAtDirective();
104 bool parseSetMacroDirective();
105 bool parseSetNoMacroDirective();
106 bool parseSetReorderDirective();
107 bool parseSetNoReorderDirective();
109 MCSymbolRefExpr::VariantKind getVariantKind(StringRef Symbol);
111 bool isMips64() const {
112 return (STI.getFeatureBits() & Mips::FeatureMips64) != 0;
115 bool isFP64() const {
116 return (STI.getFeatureBits() & Mips::FeatureFP64Bit) != 0;
119 int matchRegisterName(StringRef Symbol);
121 int matchRegisterByNumber(unsigned RegNum, StringRef Mnemonic);
123 void setFpFormat(FpFormatTy Format) {
127 void setDefaultFpFormat();
129 void setFpFormat(StringRef Format);
131 FpFormatTy getFpFormat() {return FpFormat;}
133 bool requestsDoubleOperand(StringRef Mnemonic);
135 unsigned getReg(int RC,int RegNo);
139 MipsAsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
140 : MCTargetAsmParser(), STI(sti), Parser(parser) {
141 // Initialize the set of available features.
142 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
143 Options = new MipsAssemblerOptions();
146 MCAsmParser &getParser() const { return Parser; }
147 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
154 /// MipsOperand - Instances of this class represent a parsed Mips machine
156 class MipsOperand : public MCParsedAsmOperand {
168 MipsOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
190 SMLoc StartLoc, EndLoc;
193 void addRegOperands(MCInst &Inst, unsigned N) const {
194 assert(N == 1 && "Invalid number of operands!");
195 Inst.addOperand(MCOperand::CreateReg(getReg()));
198 void addExpr(MCInst &Inst, const MCExpr *Expr) const{
199 // Add as immediate when possible. Null MCExpr = 0.
201 Inst.addOperand(MCOperand::CreateImm(0));
202 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
203 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
205 Inst.addOperand(MCOperand::CreateExpr(Expr));
208 void addImmOperands(MCInst &Inst, unsigned N) const {
209 assert(N == 1 && "Invalid number of operands!");
210 const MCExpr *Expr = getImm();
214 void addMemOperands(MCInst &Inst, unsigned N) const {
215 assert(N == 2 && "Invalid number of operands!");
217 Inst.addOperand(MCOperand::CreateReg(getMemBase()));
219 const MCExpr *Expr = getMemOff();
223 bool isReg() const { return Kind == k_Register; }
224 bool isImm() const { return Kind == k_Immediate; }
225 bool isToken() const { return Kind == k_Token; }
226 bool isMem() const { return Kind == k_Memory; }
228 StringRef getToken() const {
229 assert(Kind == k_Token && "Invalid access!");
230 return StringRef(Tok.Data, Tok.Length);
233 unsigned getReg() const {
234 assert((Kind == k_Register) && "Invalid access!");
238 const MCExpr *getImm() const {
239 assert((Kind == k_Immediate) && "Invalid access!");
243 unsigned getMemBase() const {
244 assert((Kind == k_Memory) && "Invalid access!");
248 const MCExpr *getMemOff() const {
249 assert((Kind == k_Memory) && "Invalid access!");
253 static MipsOperand *CreateToken(StringRef Str, SMLoc S) {
254 MipsOperand *Op = new MipsOperand(k_Token);
255 Op->Tok.Data = Str.data();
256 Op->Tok.Length = Str.size();
262 static MipsOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
263 MipsOperand *Op = new MipsOperand(k_Register);
264 Op->Reg.RegNum = RegNum;
270 static MipsOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
271 MipsOperand *Op = new MipsOperand(k_Immediate);
278 static MipsOperand *CreateMem(unsigned Base, const MCExpr *Off,
280 MipsOperand *Op = new MipsOperand(k_Memory);
288 /// getStartLoc - Get the location of the first token of this operand.
289 SMLoc getStartLoc() const { return StartLoc; }
290 /// getEndLoc - Get the location of the last token of this operand.
291 SMLoc getEndLoc() const { return EndLoc; }
293 virtual void print(raw_ostream &OS) const {
294 llvm_unreachable("unimplemented!");
300 MatchAndEmitInstruction(SMLoc IDLoc,
301 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
306 SmallVector<std::pair< unsigned, std::string >, 4> MapAndConstraints;
307 unsigned MatchResult = MatchInstructionImpl(Operands, Kind, Inst,
308 MapAndConstraints, ErrorInfo,
309 /*matchingInlineAsm*/ false);
311 switch (MatchResult) {
313 case Match_Success: {
315 Out.EmitInstruction(Inst);
318 case Match_MissingFeature:
319 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
321 case Match_InvalidOperand: {
322 SMLoc ErrorLoc = IDLoc;
323 if (ErrorInfo != ~0U) {
324 if (ErrorInfo >= Operands.size())
325 return Error(IDLoc, "too few operands for instruction");
327 ErrorLoc = ((MipsOperand*)Operands[ErrorInfo])->getStartLoc();
328 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
331 return Error(ErrorLoc, "invalid operand for instruction");
333 case Match_MnemonicFail:
334 return Error(IDLoc, "invalid instruction");
339 int MipsAsmParser::matchRegisterName(StringRef Name) {
341 int CC = StringSwitch<unsigned>(Name)
342 .Case("zero", Mips::ZERO)
343 .Case("a0", Mips::A0)
344 .Case("a1", Mips::A1)
345 .Case("a2", Mips::A2)
346 .Case("a3", Mips::A3)
347 .Case("v0", Mips::V0)
348 .Case("v1", Mips::V1)
349 .Case("s0", Mips::S0)
350 .Case("s1", Mips::S1)
351 .Case("s2", Mips::S2)
352 .Case("s3", Mips::S3)
353 .Case("s4", Mips::S4)
354 .Case("s5", Mips::S5)
355 .Case("s6", Mips::S6)
356 .Case("s7", Mips::S7)
357 .Case("k0", Mips::K0)
358 .Case("k1", Mips::K1)
359 .Case("sp", Mips::SP)
360 .Case("fp", Mips::FP)
361 .Case("gp", Mips::GP)
362 .Case("ra", Mips::RA)
363 .Case("t0", Mips::T0)
364 .Case("t1", Mips::T1)
365 .Case("t2", Mips::T2)
366 .Case("t3", Mips::T3)
367 .Case("t4", Mips::T4)
368 .Case("t5", Mips::T5)
369 .Case("t6", Mips::T6)
370 .Case("t7", Mips::T7)
371 .Case("t8", Mips::T8)
372 .Case("t9", Mips::T9)
373 .Case("at", Mips::AT)
374 .Case("fcc0", Mips::FCC0)
378 //64 bit register in Mips are following 32 bit definitions.
384 if (Name[0] == 'f') {
385 StringRef NumString = Name.substr(1);
387 if( NumString.getAsInteger(10, IntVal))
388 return -1; //not integer
392 FpFormatTy Format = getFpFormat();
394 if (Format == FP_FORMAT_S || Format == FP_FORMAT_W)
395 return getReg(Mips::FGR32RegClassID, IntVal);
396 if (Format == FP_FORMAT_D) {
398 return getReg(Mips::FGR64RegClassID, IntVal);
400 //only even numbers available as register pairs
401 if (( IntVal > 31) || (IntVal%2 != 0))
403 return getReg(Mips::AFGR64RegClassID, IntVal/2);
409 void MipsAsmParser::setDefaultFpFormat() {
411 if (isMips64() || isFP64())
412 FpFormat = FP_FORMAT_D;
414 FpFormat = FP_FORMAT_S;
417 bool MipsAsmParser::requestsDoubleOperand(StringRef Mnemonic){
419 bool IsDouble = StringSwitch<bool>(Mnemonic.lower())
428 void MipsAsmParser::setFpFormat(StringRef Format) {
430 FpFormat = StringSwitch<FpFormatTy>(Format.lower())
431 .Case(".s", FP_FORMAT_S)
432 .Case(".d", FP_FORMAT_D)
433 .Case(".l", FP_FORMAT_L)
434 .Case(".w", FP_FORMAT_W)
435 .Default(FP_FORMAT_NONE);
438 bool MipsAssemblerOptions::setATReg(unsigned Reg) {
446 unsigned MipsAsmParser::getATReg() {
447 unsigned Reg = Options->getATRegNum();
449 return getReg(Mips::CPU64RegsRegClassID,Reg);
451 return getReg(Mips::CPURegsRegClassID,Reg);
454 unsigned MipsAsmParser::getReg(int RC,int RegNo) {
455 return *(getContext().getRegisterInfo().getRegClass(RC).begin() + RegNo);
458 int MipsAsmParser::matchRegisterByNumber(unsigned RegNum, StringRef Mnemonic) {
460 if (Mnemonic.lower() == "rdhwr") {
461 //at the moment only hwreg29 is supported
470 return getReg(Mips::CPURegsRegClassID, RegNum);
473 int MipsAsmParser::tryParseRegister(StringRef Mnemonic) {
474 const AsmToken &Tok = Parser.getTok();
477 if (Tok.is(AsmToken::Identifier)) {
478 std::string lowerCase = Tok.getString().lower();
479 RegNum = matchRegisterName(lowerCase);
480 } else if (Tok.is(AsmToken::Integer))
481 RegNum = matchRegisterByNumber(static_cast<unsigned> (Tok.getIntVal()),
484 return RegNum; //error
485 //64 bit div operations require Mips::ZERO instead of MIPS::ZERO_64
486 if (isMips64() && RegNum == Mips::ZERO_64) {
487 if (Mnemonic.find("ddiv") != StringRef::npos)
494 tryParseRegisterOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
497 SMLoc S = Parser.getTok().getLoc();
500 //FIXME: we should make a more generic method for CCR
501 if ((Mnemonic == "cfc1" || Mnemonic == "ctc1")
502 && Operands.size() == 2 && Parser.getTok().is(AsmToken::Integer)){
503 RegNo = Parser.getTok().getIntVal(); //get the int value
504 //at the moment only fcc0 is supported
508 RegNo = tryParseRegister(Mnemonic);
512 Operands.push_back(MipsOperand::CreateReg(RegNo, S,
513 Parser.getTok().getLoc()));
514 Parser.Lex(); // Eat register token.
518 bool MipsAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*>&Operands,
519 StringRef Mnemonic) {
520 //Check if the current operand has a custom associated parser, if so, try to
521 //custom parse the operand, or fallback to the general approach.
522 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
523 if (ResTy == MatchOperand_Success)
525 // If there wasn't a custom match, try the generic matcher below. Otherwise,
526 // there was a match, but an error occurred, in which case, just return that
527 // the operand parsing failed.
528 if (ResTy == MatchOperand_ParseFail)
531 switch (getLexer().getKind()) {
533 Error(Parser.getTok().getLoc(), "unexpected token in operand");
535 case AsmToken::Dollar: {
537 SMLoc S = Parser.getTok().getLoc();
538 Parser.Lex(); // Eat dollar token.
539 //parse register operand
540 if (!tryParseRegisterOperand(Operands,Mnemonic)) {
541 if (getLexer().is(AsmToken::LParen)) {
542 //check if it is indexed addressing operand
543 Operands.push_back(MipsOperand::CreateToken("(", S));
544 Parser.Lex(); //eat parenthesis
545 if (getLexer().isNot(AsmToken::Dollar))
548 Parser.Lex(); //eat dollar
549 if (tryParseRegisterOperand(Operands,Mnemonic))
552 if (!getLexer().is(AsmToken::RParen))
555 S = Parser.getTok().getLoc();
556 Operands.push_back(MipsOperand::CreateToken(")", S));
561 //maybe it is a symbol reference
562 StringRef Identifier;
563 if (Parser.ParseIdentifier(Identifier))
566 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
568 MCSymbol *Sym = getContext().GetOrCreateSymbol("$" + Identifier);
570 // Otherwise create a symbol ref.
571 const MCExpr *Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
574 Operands.push_back(MipsOperand::CreateImm(Res, S, E));
577 case AsmToken::Identifier:
578 case AsmToken::LParen:
579 case AsmToken::Minus:
581 case AsmToken::Integer:
582 case AsmToken::String: {
583 // quoted label names
585 SMLoc S = Parser.getTok().getLoc();
586 if (getParser().ParseExpression(IdVal))
588 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
589 Operands.push_back(MipsOperand::CreateImm(IdVal, S, E));
592 case AsmToken::Percent: {
593 //it is a symbol reference or constant expression
595 SMLoc S = Parser.getTok().getLoc(); //start location of the operand
596 if (parseRelocOperand(IdVal))
599 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
601 Operands.push_back(MipsOperand::CreateImm(IdVal, S, E));
603 } // case AsmToken::Percent
604 } // switch(getLexer().getKind())
608 bool MipsAsmParser::parseRelocOperand(const MCExpr *&Res) {
610 Parser.Lex(); // eat % token
611 const AsmToken &Tok = Parser.getTok(); //get next token, operation
612 if (Tok.isNot(AsmToken::Identifier))
615 std::string Str = Tok.getIdentifier().str();
617 Parser.Lex(); //eat identifier
618 // now make expression from the rest of the operand
622 if (getLexer().getKind() == AsmToken::LParen) {
624 Parser.Lex(); // eat '(' token
625 if (getLexer().getKind() == AsmToken::Percent) {
626 Parser.Lex(); // eat % token
627 const AsmToken &nextTok = Parser.getTok();
628 if (nextTok.isNot(AsmToken::Identifier))
631 Str += nextTok.getIdentifier();
632 Parser.Lex(); // eat identifier
633 if (getLexer().getKind() != AsmToken::LParen)
638 if (getParser().ParseParenExpression(IdVal,EndLoc))
641 while (getLexer().getKind() == AsmToken::RParen)
642 Parser.Lex(); // eat ')' token
645 return true; // parenthesis must follow reloc operand
647 // Check the type of the expression
648 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(IdVal)) {
649 //it's a constant, evaluate lo or hi value
650 int Val = MCE->getValue();
653 } else if (Str == "hi") {
654 Val = (Val & 0xffff0000) >> 16;
656 Res = MCConstantExpr::Create(Val, getContext());
660 if (const MCSymbolRefExpr *MSRE = dyn_cast<MCSymbolRefExpr>(IdVal)) {
661 // it's a symbol, create symbolic expression from symbol
662 StringRef Symbol = MSRE->getSymbol().getName();
663 MCSymbolRefExpr::VariantKind VK = getVariantKind(Str);
664 Res = MCSymbolRefExpr::Create(Symbol,VK,getContext());
670 bool MipsAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
673 StartLoc = Parser.getTok().getLoc();
674 RegNo = tryParseRegister("");
675 EndLoc = Parser.getTok().getLoc();
676 return (RegNo == (unsigned)-1);
679 bool MipsAsmParser::parseMemOffset(const MCExpr *&Res) {
683 switch(getLexer().getKind()) {
686 case AsmToken::Integer:
687 case AsmToken::Minus:
689 return (getParser().ParseExpression(Res));
690 case AsmToken::Percent:
691 return parseRelocOperand(Res);
692 case AsmToken::LParen:
693 return false; // it's probably assuming 0
698 MipsAsmParser::OperandMatchResultTy MipsAsmParser::parseMemOperand(
699 SmallVectorImpl<MCParsedAsmOperand*>&Operands) {
701 const MCExpr *IdVal = 0;
703 // first operand is the offset
704 S = Parser.getTok().getLoc();
706 if (parseMemOffset(IdVal))
707 return MatchOperand_ParseFail;
709 const AsmToken &Tok = Parser.getTok(); // get next token
710 if (Tok.isNot(AsmToken::LParen)) {
711 Error(Parser.getTok().getLoc(), "'(' expected");
712 return MatchOperand_ParseFail;
715 Parser.Lex(); // Eat '(' token.
717 const AsmToken &Tok1 = Parser.getTok(); //get next token
718 if (Tok1.is(AsmToken::Dollar)) {
719 Parser.Lex(); // Eat '$' token.
720 if (tryParseRegisterOperand(Operands,"")) {
721 Error(Parser.getTok().getLoc(), "unexpected token in operand");
722 return MatchOperand_ParseFail;
726 Error(Parser.getTok().getLoc(), "unexpected token in operand");
727 return MatchOperand_ParseFail;
730 const AsmToken &Tok2 = Parser.getTok(); // get next token
731 if (Tok2.isNot(AsmToken::RParen)) {
732 Error(Parser.getTok().getLoc(), "')' expected");
733 return MatchOperand_ParseFail;
736 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
738 Parser.Lex(); // Eat ')' token.
741 IdVal = MCConstantExpr::Create(0, getContext());
743 // now replace register operand with the mem operand
744 MipsOperand* op = static_cast<MipsOperand*>(Operands.back());
745 int RegNo = op->getReg();
746 // remove register from operands
748 // and add memory operand
749 Operands.push_back(MipsOperand::CreateMem(RegNo, IdVal, S, E));
751 return MatchOperand_Success;
754 MCSymbolRefExpr::VariantKind MipsAsmParser::getVariantKind(StringRef Symbol) {
756 MCSymbolRefExpr::VariantKind VK
757 = StringSwitch<MCSymbolRefExpr::VariantKind>(Symbol)
758 .Case("hi", MCSymbolRefExpr::VK_Mips_ABS_HI)
759 .Case("lo", MCSymbolRefExpr::VK_Mips_ABS_LO)
760 .Case("gp_rel", MCSymbolRefExpr::VK_Mips_GPREL)
761 .Case("call16", MCSymbolRefExpr::VK_Mips_GOT_CALL)
762 .Case("got", MCSymbolRefExpr::VK_Mips_GOT)
763 .Case("tlsgd", MCSymbolRefExpr::VK_Mips_TLSGD)
764 .Case("tlsldm", MCSymbolRefExpr::VK_Mips_TLSLDM)
765 .Case("dtprel_hi", MCSymbolRefExpr::VK_Mips_DTPREL_HI)
766 .Case("dtprel_lo", MCSymbolRefExpr::VK_Mips_DTPREL_LO)
767 .Case("gottprel", MCSymbolRefExpr::VK_Mips_GOTTPREL)
768 .Case("tprel_hi", MCSymbolRefExpr::VK_Mips_TPREL_HI)
769 .Case("tprel_lo", MCSymbolRefExpr::VK_Mips_TPREL_LO)
770 .Case("got_disp", MCSymbolRefExpr::VK_Mips_GOT_DISP)
771 .Case("got_page", MCSymbolRefExpr::VK_Mips_GOT_PAGE)
772 .Case("got_ofst", MCSymbolRefExpr::VK_Mips_GOT_OFST)
773 .Case("hi(%neg(%gp_rel", MCSymbolRefExpr::VK_Mips_GPOFF_HI)
774 .Case("lo(%neg(%gp_rel", MCSymbolRefExpr::VK_Mips_GPOFF_LO)
775 .Default(MCSymbolRefExpr::VK_None);
780 static int ConvertCcString(StringRef CondString) {
781 int CC = StringSwitch<unsigned>(CondString)
804 parseMathOperation(StringRef Name, SMLoc NameLoc,
805 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
807 size_t Start = Name.find('.'), Next = Name.rfind('.');
808 StringRef Format1 = Name.slice(Start, Next);
809 // and add the first format to the operands
810 Operands.push_back(MipsOperand::CreateToken(Format1, NameLoc));
811 // now for the second format
812 StringRef Format2 = Name.slice(Next, StringRef::npos);
813 Operands.push_back(MipsOperand::CreateToken(Format2, NameLoc));
815 // set the format for the first register
816 setFpFormat(Format1);
818 // Read the remaining operands.
819 if (getLexer().isNot(AsmToken::EndOfStatement)) {
820 // Read the first operand.
821 if (ParseOperand(Operands, Name)) {
822 SMLoc Loc = getLexer().getLoc();
823 Parser.EatToEndOfStatement();
824 return Error(Loc, "unexpected token in argument list");
827 if (getLexer().isNot(AsmToken::Comma)) {
828 SMLoc Loc = getLexer().getLoc();
829 Parser.EatToEndOfStatement();
830 return Error(Loc, "unexpected token in argument list");
833 Parser.Lex(); // Eat the comma.
835 //set the format for the first register
836 setFpFormat(Format2);
838 // Parse and remember the operand.
839 if (ParseOperand(Operands, Name)) {
840 SMLoc Loc = getLexer().getLoc();
841 Parser.EatToEndOfStatement();
842 return Error(Loc, "unexpected token in argument list");
846 if (getLexer().isNot(AsmToken::EndOfStatement)) {
847 SMLoc Loc = getLexer().getLoc();
848 Parser.EatToEndOfStatement();
849 return Error(Loc, "unexpected token in argument list");
852 Parser.Lex(); // Consume the EndOfStatement
857 ParseInstruction(StringRef Name, SMLoc NameLoc,
858 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
859 // floating point instructions: should register be treated as double?
860 if (requestsDoubleOperand(Name)) {
861 setFpFormat(FP_FORMAT_D);
862 Operands.push_back(MipsOperand::CreateToken(Name, NameLoc));
865 setDefaultFpFormat();
866 // Create the leading tokens for the mnemonic, split by '.' characters.
867 size_t Start = 0, Next = Name.find('.');
868 StringRef Mnemonic = Name.slice(Start, Next);
870 Operands.push_back(MipsOperand::CreateToken(Mnemonic, NameLoc));
872 if (Next != StringRef::npos) {
873 // there is a format token in mnemonic
874 // StringRef Rest = Name.slice(Next, StringRef::npos);
875 size_t Dot = Name.find('.', Next+1);
876 StringRef Format = Name.slice(Next, Dot);
877 if (Dot == StringRef::npos) //only one '.' in a string, it's a format
878 Operands.push_back(MipsOperand::CreateToken(Format, NameLoc));
880 if (Name.startswith("c.")){
881 // floating point compare, add '.' and immediate represent for cc
882 Operands.push_back(MipsOperand::CreateToken(".", NameLoc));
883 int Cc = ConvertCcString(Format);
885 return Error(NameLoc, "Invalid conditional code");
887 SMLoc E = SMLoc::getFromPointer(
888 Parser.getTok().getLoc().getPointer() -1 );
889 Operands.push_back(MipsOperand::CreateImm(
890 MCConstantExpr::Create(Cc, getContext()), NameLoc, E));
892 // trunc, ceil, floor ...
893 return parseMathOperation(Name, NameLoc, Operands);
896 // the rest is a format
897 Format = Name.slice(Dot, StringRef::npos);
898 Operands.push_back(MipsOperand::CreateToken(Format, NameLoc));
905 // Read the remaining operands.
906 if (getLexer().isNot(AsmToken::EndOfStatement)) {
907 // Read the first operand.
908 if (ParseOperand(Operands, Name)) {
909 SMLoc Loc = getLexer().getLoc();
910 Parser.EatToEndOfStatement();
911 return Error(Loc, "unexpected token in argument list");
914 while (getLexer().is(AsmToken::Comma) ) {
915 Parser.Lex(); // Eat the comma.
917 // Parse and remember the operand.
918 if (ParseOperand(Operands, Name)) {
919 SMLoc Loc = getLexer().getLoc();
920 Parser.EatToEndOfStatement();
921 return Error(Loc, "unexpected token in argument list");
926 if (getLexer().isNot(AsmToken::EndOfStatement)) {
927 SMLoc Loc = getLexer().getLoc();
928 Parser.EatToEndOfStatement();
929 return Error(Loc, "unexpected token in argument list");
932 Parser.Lex(); // Consume the EndOfStatement
936 bool MipsAsmParser::reportParseError(StringRef ErrorMsg) {
937 SMLoc Loc = getLexer().getLoc();
938 Parser.EatToEndOfStatement();
939 return Error(Loc, ErrorMsg);
942 bool MipsAsmParser::parseSetNoAtDirective() {
943 // line should look like:
946 Options->setATReg(0);
949 // if this is not the end of the statement, report error
950 if (getLexer().isNot(AsmToken::EndOfStatement)) {
951 reportParseError("unexpected token in statement");
954 Parser.Lex(); // Consume the EndOfStatement
957 bool MipsAsmParser::parseSetAtDirective() {
959 // .set at - defaults to $1
962 if (getLexer().is(AsmToken::EndOfStatement)) {
963 Options->setATReg(1);
964 Parser.Lex(); // Consume the EndOfStatement
966 } else if (getLexer().is(AsmToken::Equal)) {
967 getParser().Lex(); //eat '='
968 if (getLexer().isNot(AsmToken::Dollar)) {
969 reportParseError("unexpected token in statement");
972 Parser.Lex(); // eat '$'
973 if (getLexer().isNot(AsmToken::Integer)) {
974 reportParseError("unexpected token in statement");
977 const AsmToken &Reg = Parser.getTok();
978 if (!Options->setATReg(Reg.getIntVal())) {
979 reportParseError("unexpected token in statement");
982 getParser().Lex(); //eat reg
984 if (getLexer().isNot(AsmToken::EndOfStatement)) {
985 reportParseError("unexpected token in statement");
988 Parser.Lex(); // Consume the EndOfStatement
991 reportParseError("unexpected token in statement");
996 bool MipsAsmParser::parseSetReorderDirective() {
998 // if this is not the end of the statement, report error
999 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1000 reportParseError("unexpected token in statement");
1003 Options->setReorder();
1004 Parser.Lex(); // Consume the EndOfStatement
1008 bool MipsAsmParser::parseSetNoReorderDirective() {
1010 // if this is not the end of the statement, report error
1011 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1012 reportParseError("unexpected token in statement");
1015 Options->setNoreorder();
1016 Parser.Lex(); // Consume the EndOfStatement
1020 bool MipsAsmParser::parseSetMacroDirective() {
1022 // if this is not the end of the statement, report error
1023 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1024 reportParseError("unexpected token in statement");
1027 Options->setMacro();
1028 Parser.Lex(); // Consume the EndOfStatement
1032 bool MipsAsmParser::parseSetNoMacroDirective() {
1034 // if this is not the end of the statement, report error
1035 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1036 reportParseError("`noreorder' must be set before `nomacro'");
1039 if (Options->isReorder()) {
1040 reportParseError("`noreorder' must be set before `nomacro'");
1043 Options->setNomacro();
1044 Parser.Lex(); // Consume the EndOfStatement
1047 bool MipsAsmParser::parseDirectiveSet() {
1050 const AsmToken &Tok = Parser.getTok();
1052 if (Tok.getString() == "noat") {
1053 return parseSetNoAtDirective();
1054 } else if (Tok.getString() == "at") {
1055 return parseSetAtDirective();
1056 } else if (Tok.getString() == "reorder") {
1057 return parseSetReorderDirective();
1058 } else if (Tok.getString() == "noreorder") {
1059 return parseSetNoReorderDirective();
1060 } else if (Tok.getString() == "macro") {
1061 return parseSetMacroDirective();
1062 } else if (Tok.getString() == "nomacro") {
1063 return parseSetNoMacroDirective();
1064 } else if (Tok.getString() == "nomips16") {
1065 // ignore this directive for now
1066 Parser.EatToEndOfStatement();
1068 } else if (Tok.getString() == "nomicromips") {
1069 // ignore this directive for now
1070 Parser.EatToEndOfStatement();
1076 bool MipsAsmParser::ParseDirective(AsmToken DirectiveID) {
1078 if (DirectiveID.getString() == ".ent") {
1079 // ignore this directive for now
1084 if (DirectiveID.getString() == ".end") {
1085 // ignore this directive for now
1090 if (DirectiveID.getString() == ".frame") {
1091 // ignore this directive for now
1092 Parser.EatToEndOfStatement();
1096 if (DirectiveID.getString() == ".set") {
1097 // ignore this directive for now
1098 //Parser.EatToEndOfStatement();
1099 return parseDirectiveSet();
1102 if (DirectiveID.getString() == ".fmask") {
1103 // ignore this directive for now
1104 Parser.EatToEndOfStatement();
1108 if (DirectiveID.getString() == ".mask") {
1109 // ignore this directive for now
1110 Parser.EatToEndOfStatement();
1114 if (DirectiveID.getString() == ".gpword") {
1115 // ignore this directive for now
1116 Parser.EatToEndOfStatement();
1123 extern "C" void LLVMInitializeMipsAsmParser() {
1124 RegisterMCAsmParser<MipsAsmParser> X(TheMipsTarget);
1125 RegisterMCAsmParser<MipsAsmParser> Y(TheMipselTarget);
1126 RegisterMCAsmParser<MipsAsmParser> A(TheMips64Target);
1127 RegisterMCAsmParser<MipsAsmParser> B(TheMips64elTarget);
1130 #define GET_REGISTER_MATCHER
1131 #define GET_MATCHER_IMPLEMENTATION
1132 #include "MipsGenAsmMatcher.inc"