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