From ce71989188b8aa289b7c1907b7810a36508d2be2 Mon Sep 17 00:00:00 2001 From: Nirav Dave Date: Tue, 20 Mar 2018 19:12:41 +0000 Subject: [PATCH] [MC,X86] Cleanup some X86 parser functions to use MCParser helpers. NFCI. llvm-svn: 328019 --- llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp | 67 +++++++++----------------- 1 file changed, 22 insertions(+), 45 deletions(-) diff --git a/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp b/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp index dd48602..36aecb8 100644 --- a/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp +++ b/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp @@ -2109,12 +2109,8 @@ std::unique_ptr X86AsmParser::ParseMemOperand(unsigned SegReg, if (getLexer().isNot(AsmToken::RParen)) { // Parse the scale amount: // ::= ',' [scale-expression] - if (getLexer().isNot(AsmToken::Comma)) { - Error(Parser.getTok().getLoc(), - "expected comma in scale expression"); + if (parseToken(AsmToken::Comma, "expected comma in scale expression")) return nullptr; - } - Parser.Lex(); // Eat the comma. if (getLexer().isNot(AsmToken::RParen)) { SMLoc Loc = Parser.getTok().getLoc(); @@ -2155,12 +2151,9 @@ std::unique_ptr X86AsmParser::ParseMemOperand(unsigned SegReg, } // Ok, we've eaten the memory operand, verify we have a ')' and eat it too. - if (getLexer().isNot(AsmToken::RParen)) { - Error(Parser.getTok().getLoc(), "unexpected token in memory operand"); - return nullptr; - } SMLoc MemEnd = Parser.getTok().getEndLoc(); - Parser.Lex(); // Eat the ')'. + if (parseToken(AsmToken::RParen, "unexpected token in memory operand")) + return nullptr; // Check for use of invalid 16-bit registers. Only BX/BP/SI/DI are allowed, // and then only in non-64-bit modes. Except for DX, which is a special case @@ -3198,10 +3191,9 @@ bool X86AsmParser::ParseDirective(AsmToken DirectiveID) { /// parseDirectiveEven /// ::= .even bool X86AsmParser::parseDirectiveEven(SMLoc L) { - if (getLexer().isNot(AsmToken::EndOfStatement)) { - TokError("unexpected token in directive"); - return false; - } + if (parseToken(AsmToken::EndOfStatement, "unexpected token in directive")) + return false; + const MCSection *Section = getStreamer().getCurrentSectionOnly(); if (!Section) { getStreamer().InitSections(false); @@ -3216,37 +3208,22 @@ bool X86AsmParser::parseDirectiveEven(SMLoc L) { /// ParseDirectiveWord /// ::= .word [ expression (, expression)* ] bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) { - MCAsmParser &Parser = getParser(); - if (getLexer().isNot(AsmToken::EndOfStatement)) { - for (;;) { - const MCExpr *Value; - SMLoc ExprLoc = getLexer().getLoc(); - if (getParser().parseExpression(Value)) - return false; - - if (const auto *MCE = dyn_cast(Value)) { - assert(Size <= 8 && "Invalid size"); - uint64_t IntValue = MCE->getValue(); - if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue)) - return Error(ExprLoc, "literal value out of range for directive"); - getStreamer().EmitIntValue(IntValue, Size); - } else { - getStreamer().EmitValue(Value, Size, ExprLoc); - } - - if (getLexer().is(AsmToken::EndOfStatement)) - break; - - // FIXME: Improve diagnostic. - if (getLexer().isNot(AsmToken::Comma)) { - Error(L, "unexpected token in directive"); - return false; - } - Parser.Lex(); - } - } - - Parser.Lex(); + auto parseOp = [&]() -> bool { + const MCExpr *Value; + SMLoc ExprLoc = getLexer().getLoc(); + if (getParser().parseExpression(Value)) + return true; + if (const auto *MCE = dyn_cast(Value)) { + assert(Size <= 8 && "Invalid size"); + uint64_t IntValue = MCE->getValue(); + if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue)) + return Error(ExprLoc, "literal value out of range for directive"); + getStreamer().EmitIntValue(IntValue, Size); + } else + getStreamer().EmitValue(Value, Size, ExprLoc); + return false; + }; + parseMany(parseOp); return false; } -- 2.7.4