xref: /freebsd/contrib/llvm-project/llvm/lib/MC/MCParser/MCAsmParser.cpp (revision da477bcdc0c335171bb0ed3813f570026de6df85)
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 
22 using namespace llvm;
23 
24 cl::opt<unsigned> AsmMacroMaxNestingDepth(
25     "asm-macro-max-nesting-depth", cl::init(20), cl::Hidden,
26     cl::desc("The maximum nesting depth allowed for assembly macros."));
27 
28 MCAsmParser::MCAsmParser() {}
29 
30 MCAsmParser::~MCAsmParser() = default;
31 
32 void MCAsmParser::setTargetParser(MCTargetAsmParser &P) {
33   assert(!TargetParser && "Target parser is already initialized!");
34   TargetParser = &P;
35   TargetParser->Initialize(*this);
36 }
37 
38 const AsmToken &MCAsmParser::getTok() const {
39   return getLexer().getTok();
40 }
41 
42 bool MCAsmParser::parseTokenLoc(SMLoc &Loc) {
43   Loc = getTok().getLoc();
44   return false;
45 }
46 
47 bool MCAsmParser::parseEOL(const Twine &Msg) {
48   if (getTok().getKind() != AsmToken::EndOfStatement)
49     return Error(getTok().getLoc(), Msg);
50   Lex();
51   return false;
52 }
53 
54 bool MCAsmParser::parseToken(AsmToken::TokenKind T, const Twine &Msg) {
55   if (T == AsmToken::EndOfStatement)
56     return parseEOL(Msg);
57   if (getTok().getKind() != T)
58     return Error(getTok().getLoc(), Msg);
59   Lex();
60   return false;
61 }
62 
63 bool MCAsmParser::parseIntToken(int64_t &V, const Twine &Msg) {
64   if (getTok().getKind() != AsmToken::Integer)
65     return TokError(Msg);
66   V = getTok().getIntVal();
67   Lex();
68   return false;
69 }
70 
71 bool MCAsmParser::parseOptionalToken(AsmToken::TokenKind T) {
72   bool Present = (getTok().getKind() == T);
73   if (Present)
74     parseToken(T);
75   return Present;
76 }
77 
78 bool MCAsmParser::check(bool P, const Twine &Msg) {
79   return check(P, getTok().getLoc(), Msg);
80 }
81 
82 bool MCAsmParser::check(bool P, SMLoc Loc, const Twine &Msg) {
83   if (P)
84     return Error(Loc, Msg);
85   return false;
86 }
87 
88 bool MCAsmParser::TokError(const Twine &Msg, SMRange Range) {
89   return Error(getLexer().getLoc(), Msg, Range);
90 }
91 
92 bool MCAsmParser::Error(SMLoc L, const Twine &Msg, SMRange Range) {
93 
94   MCPendingError PErr;
95   PErr.Loc = L;
96   Msg.toVector(PErr.Msg);
97   PErr.Range = Range;
98   PendingErrors.push_back(PErr);
99 
100   // If we threw this parsing error after a lexing error, this should
101   // supercede the lexing error and so we remove it from the Lexer
102   // before it can propagate
103   if (getTok().is(AsmToken::Error))
104     getLexer().Lex();
105   return true;
106 }
107 
108 bool MCAsmParser::addErrorSuffix(const Twine &Suffix) {
109   // Make sure lexing errors have propagated to the parser.
110   if (getTok().is(AsmToken::Error))
111     Lex();
112   for (auto &PErr : PendingErrors)
113     Suffix.toVector(PErr.Msg);
114   return true;
115 }
116 
117 bool MCAsmParser::parseMany(function_ref<bool()> parseOne, bool hasComma) {
118   if (parseOptionalToken(AsmToken::EndOfStatement))
119     return false;
120   while (true) {
121     if (parseOne())
122       return true;
123     if (parseOptionalToken(AsmToken::EndOfStatement))
124       return false;
125     if (hasComma && parseToken(AsmToken::Comma))
126       return true;
127   }
128   return false;
129 }
130 
131 bool MCAsmParser::parseExpression(const MCExpr *&Res) {
132   SMLoc L;
133   return parseExpression(Res, L);
134 }
135 
136 void MCParsedAsmOperand::dump() const {
137   // Cannot completely remove virtual function even in release mode.
138 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
139   dbgs() << "  " << *this;
140 #endif
141 }
142