diff options
author | Sergei Barannikov <barannikov88@gmail.com> | 2023-06-29 17:22:41 +0300 |
---|---|---|
committer | Sergei Barannikov <barannikov88@gmail.com> | 2023-07-01 04:33:28 +0300 |
commit | af20c1c1298d15f36470cd9d5b2cccb3b9b59c30 (patch) | |
tree | c53cfe4d72aa04582b2f5759240df170a6535bb6 /llvm/lib/Target/AVR | |
parent | 624813a4f41c5945dc8f8d998173960ad75db731 (diff) | |
download | llvm-af20c1c1298d15f36470cd9d5b2cccb3b9b59c30.zip llvm-af20c1c1298d15f36470cd9d5b2cccb3b9b59c30.tar.gz llvm-af20c1c1298d15f36470cd9d5b2cccb3b9b59c30.tar.bz2 |
[MC] Add three-state parseDirective as a replacement for ParseDirective
Conventionally, parsing methods return false on success and true on
error. However, directive parsing methods need a third state: the
directive is not target specific. AsmParser::parseStatement detected
this case by using a fragile heuristic: if the target parser did not
consume any tokens, the directive is assumed to be not target-specific.
Some targets fail to follow the convention: they return success after
emitting an error or do not consume the entire line and return failure
on successful parsing. This was partially worked around by checking for
pending errors in parseStatement.
This patch tries to improve the situation by introducing parseDirective
method that returns ParseStatus -- three-state class. The new method
should eventually replace the old one returning bool.
ParseStatus is intentionally implicitly constructible from bool to allow
uses like `return Error(Loc, "message")`. It also has a potential to
replace OperandMatchResulTy as it is more convenient to use due to the
implicit construction from bool and more type safe.
Reviewed By: MaskRay
Differential Revision: https://reviews.llvm.org/D154101
Diffstat (limited to 'llvm/lib/Target/AVR')
-rw-r--r-- | llvm/lib/Target/AVR/AsmParser/AVRAsmParser.cpp | 30 |
1 files changed, 16 insertions, 14 deletions
diff --git a/llvm/lib/Target/AVR/AsmParser/AVRAsmParser.cpp b/llvm/lib/Target/AVR/AsmParser/AVRAsmParser.cpp index 8d30b78..6c328ff 100644 --- a/llvm/lib/Target/AVR/AsmParser/AVRAsmParser.cpp +++ b/llvm/lib/Target/AVR/AsmParser/AVRAsmParser.cpp @@ -64,7 +64,7 @@ class AVRAsmParser : public MCTargetAsmParser { bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc, OperandVector &Operands) override; - bool ParseDirective(AsmToken DirectiveID) override; + ParseStatus parseDirective(AsmToken DirectiveID) override; OperandMatchResultTy parseMemriOperand(OperandVector &Operands); @@ -90,7 +90,7 @@ class AVRAsmParser : public MCTargetAsmParser { uint64_t const &ErrorInfo); bool missingFeature(SMLoc const &Loc, uint64_t const &ErrorInfo); - bool parseLiteralValues(unsigned SizeInBytes, SMLoc L); + ParseStatus parseLiteralValues(unsigned SizeInBytes, SMLoc L); public: AVRAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser, @@ -674,19 +674,18 @@ bool AVRAsmParser::ParseInstruction(ParseInstructionInfo &Info, return false; } -bool AVRAsmParser::ParseDirective(llvm::AsmToken DirectiveID) { +ParseStatus AVRAsmParser::parseDirective(llvm::AsmToken DirectiveID) { StringRef IDVal = DirectiveID.getIdentifier(); - if (IDVal.lower() == ".long") { - parseLiteralValues(SIZE_LONG, DirectiveID.getLoc()); - } else if (IDVal.lower() == ".word" || IDVal.lower() == ".short") { - parseLiteralValues(SIZE_WORD, DirectiveID.getLoc()); - } else if (IDVal.lower() == ".byte") { - parseLiteralValues(1, DirectiveID.getLoc()); - } - return true; + if (IDVal.lower() == ".long") + return parseLiteralValues(SIZE_LONG, DirectiveID.getLoc()); + if (IDVal.lower() == ".word" || IDVal.lower() == ".short") + return parseLiteralValues(SIZE_WORD, DirectiveID.getLoc()); + if (IDVal.lower() == ".byte") + return parseLiteralValues(1, DirectiveID.getLoc()); + return ParseStatus::NoMatch; } -bool AVRAsmParser::parseLiteralValues(unsigned SizeInBytes, SMLoc L) { +ParseStatus AVRAsmParser::parseLiteralValues(unsigned SizeInBytes, SMLoc L) { MCAsmParser &Parser = getParser(); AVRMCELFStreamer &AVRStreamer = static_cast<AVRMCELFStreamer &>(Parser.getStreamer()); @@ -698,7 +697,7 @@ bool AVRAsmParser::parseLiteralValues(unsigned SizeInBytes, SMLoc L) { MCSymbol *Symbol = getContext().getOrCreateSymbol(".text"); AVRStreamer.emitValueForModiferKind(Symbol, SizeInBytes, L, AVRMCExpr::VK_AVR_None); - return false; + return ParseStatus::NoMatch; } if (Parser.getTok().getKind() == AsmToken::Identifier && @@ -715,7 +714,10 @@ bool AVRAsmParser::parseLiteralValues(unsigned SizeInBytes, SMLoc L) { MCSymbol *Symbol = getContext().getOrCreateSymbol(Parser.getTok().getString()); AVRStreamer.emitValueForModiferKind(Symbol, SizeInBytes, L, ModifierKind); - return false; + Lex(); // Eat the symbol name. + if (parseToken(AsmToken::RParen)) + return ParseStatus::Failure; + return parseEOL(); } auto parseOne = [&]() -> bool { |