| 1 | //===-- MCAsmParser.cpp - Abstract Asm Parser Interface -------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/MC/MCParser/MCAsmParser.h" |
| 10 | #include "llvm/ADT/StringRef.h" |
| 11 | #include "llvm/ADT/Twine.h" |
| 12 | #include "llvm/Config/llvm-config.h" |
| 13 | #include "llvm/MC/MCAsmInfo.h" |
| 14 | #include "llvm/MC/MCParser/AsmLexer.h" |
| 15 | #include "llvm/MC/MCParser/MCParsedAsmOperand.h" |
| 16 | #include "llvm/MC/MCParser/MCTargetAsmParser.h" |
| 17 | #include "llvm/Support/CommandLine.h" |
| 18 | #include "llvm/Support/Debug.h" |
| 19 | #include "llvm/Support/SMLoc.h" |
| 20 | #include "llvm/Support/raw_ostream.h" |
| 21 | #include <cassert> |
| 22 | |
| 23 | using namespace llvm; |
| 24 | |
| 25 | namespace llvm { |
| 26 | cl::opt<unsigned> AsmMacroMaxNestingDepth( |
| 27 | "asm-macro-max-nesting-depth" , cl::init(Val: 20), cl::Hidden, |
| 28 | cl::desc("The maximum nesting depth allowed for assembly macros." )); |
| 29 | } |
| 30 | |
| 31 | MCAsmParser::MCAsmParser(MCContext &Ctx, MCStreamer &Out, SourceMgr &SM, |
| 32 | const MCAsmInfo &MAI) |
| 33 | : Ctx(Ctx), Out(Out), SrcMgr(SM), MAI(MAI), Lexer(MAI) {} |
| 34 | |
| 35 | MCAsmParser::~MCAsmParser() = default; |
| 36 | |
| 37 | void MCAsmParser::setTargetParser(MCTargetAsmParser &P) { |
| 38 | assert(!TargetParser && "Target parser is already initialized!" ); |
| 39 | TargetParser = &P; |
| 40 | TargetParser->Initialize(Parser&: *this); |
| 41 | } |
| 42 | |
| 43 | const AsmToken &MCAsmParser::getTok() const { |
| 44 | return getLexer().getTok(); |
| 45 | } |
| 46 | |
| 47 | bool MCAsmParser::parseTokenLoc(SMLoc &Loc) { |
| 48 | Loc = getTok().getLoc(); |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | bool MCAsmParser::parseEOL() { |
| 53 | if (getTok().getKind() != AsmToken::EndOfStatement) |
| 54 | return Error(L: getTok().getLoc(), Msg: "expected newline" ); |
| 55 | Lex(); |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | bool MCAsmParser::parseEOL(const Twine &Msg) { |
| 60 | if (getTok().getKind() != AsmToken::EndOfStatement) |
| 61 | return Error(L: getTok().getLoc(), Msg); |
| 62 | Lex(); |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | bool MCAsmParser::parseToken(AsmToken::TokenKind T, const Twine &Msg) { |
| 67 | if (T == AsmToken::EndOfStatement) |
| 68 | return parseEOL(Msg); |
| 69 | if (getTok().getKind() != T) |
| 70 | return Error(L: getTok().getLoc(), Msg); |
| 71 | Lex(); |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | bool MCAsmParser::parseIntToken(int64_t &V, const Twine &Msg) { |
| 76 | if (getTok().getKind() != AsmToken::Integer) |
| 77 | return TokError(Msg); |
| 78 | V = getTok().getIntVal(); |
| 79 | Lex(); |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | bool MCAsmParser::parseOptionalToken(AsmToken::TokenKind T) { |
| 84 | bool Present = (getTok().getKind() == T); |
| 85 | if (Present) |
| 86 | parseToken(T); |
| 87 | return Present; |
| 88 | } |
| 89 | |
| 90 | bool MCAsmParser::check(bool P, const Twine &Msg) { |
| 91 | return check(P, Loc: getTok().getLoc(), Msg); |
| 92 | } |
| 93 | |
| 94 | bool MCAsmParser::check(bool P, SMLoc Loc, const Twine &Msg) { |
| 95 | if (P) |
| 96 | return Error(L: Loc, Msg); |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | bool MCAsmParser::TokError(const Twine &Msg, SMRange Range) { |
| 101 | return Error(L: getLexer().getLoc(), Msg, Range); |
| 102 | } |
| 103 | |
| 104 | bool MCAsmParser::Error(SMLoc L, const Twine &Msg, SMRange Range) { |
| 105 | MCPendingError PErr; |
| 106 | PErr.Loc = L; |
| 107 | Msg.toVector(Out&: PErr.Msg); |
| 108 | PErr.Range = Range; |
| 109 | PendingErrors.push_back(Elt: PErr); |
| 110 | |
| 111 | // If we threw this parsing error after a lexing error, this should |
| 112 | // supercede the lexing error and so we remove it from the Lexer |
| 113 | // before it can propagate |
| 114 | if (getTok().is(K: AsmToken::Error)) |
| 115 | getLexer().Lex(); |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | bool MCAsmParser::addErrorSuffix(const Twine &Suffix) { |
| 120 | // Make sure lexing errors have propagated to the parser. |
| 121 | if (getTok().is(K: AsmToken::Error)) |
| 122 | Lex(); |
| 123 | for (auto &PErr : PendingErrors) |
| 124 | Suffix.toVector(Out&: PErr.Msg); |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | bool MCAsmParser::parseMany(function_ref<bool()> parseOne, bool hasComma) { |
| 129 | if (parseOptionalToken(T: AsmToken::EndOfStatement)) |
| 130 | return false; |
| 131 | while (true) { |
| 132 | if (parseOne()) |
| 133 | return true; |
| 134 | if (parseOptionalToken(T: AsmToken::EndOfStatement)) |
| 135 | return false; |
| 136 | if (hasComma && parseToken(T: AsmToken::Comma)) |
| 137 | return true; |
| 138 | } |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | bool MCAsmParser::parseExpression(const MCExpr *&Res) { |
| 143 | SMLoc L; |
| 144 | return parseExpression(Res, EndLoc&: L); |
| 145 | } |
| 146 | |
| 147 | bool MCAsmParser::parseGNUAttribute(SMLoc L, int64_t &Tag, |
| 148 | int64_t &IntegerValue) { |
| 149 | // Parse a .gnu_attribute with numerical tag and value. |
| 150 | StringRef S(L.getPointer()); |
| 151 | SMLoc TagLoc; |
| 152 | TagLoc = getTok().getLoc(); |
| 153 | const AsmToken &Tok = getTok(); |
| 154 | if (Tok.isNot(K: AsmToken::Integer)) |
| 155 | return false; |
| 156 | Tag = Tok.getIntVal(); |
| 157 | Lex(); // Eat the Tag |
| 158 | Lex(); // Eat the comma |
| 159 | if (Tok.isNot(K: AsmToken::Integer)) |
| 160 | return false; |
| 161 | IntegerValue = Tok.getIntVal(); |
| 162 | Lex(); // Eat the IntegerValue |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | void MCParsedAsmOperand::dump() const { |
| 167 | // Cannot completely remove virtual function even in release mode. |
| 168 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 169 | dbgs() << " " ; |
| 170 | print(dbgs(), MCAsmInfo()); |
| 171 | #endif |
| 172 | } |
| 173 | |