LLVM 24.0.0git
MCAsmParser.cpp
Go to the documentation of this file.
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
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"
17#include "llvm/Support/Debug.h"
18#include "llvm/Support/SMLoc.h"
20#include <cassert>
21
22using namespace llvm;
23
27
29
31 assert(!TargetParser && "Target parser is already initialized!");
32 TargetParser = &P;
33 TargetParser->Initialize(*this);
34}
35
37 return getLexer().getTok();
38}
39
41 Loc = getTok().getLoc();
42 return false;
43}
44
46 if (getTok().getKind() != AsmToken::EndOfStatement)
47 return Error(getTok().getLoc(), "expected newline");
48 Lex();
49 return false;
50}
51
53 if (getTok().getKind() != AsmToken::EndOfStatement)
54 return Error(getTok().getLoc(), Msg);
55 Lex();
56 return false;
57}
58
61 return parseEOL(Msg);
62 if (getTok().getKind() != T)
63 return Error(getTok().getLoc(), Msg);
64 Lex();
65 return false;
66}
67
68bool MCAsmParser::parseIntToken(int64_t &V, const Twine &Msg) {
69 if (getTok().getKind() != AsmToken::Integer)
70 return TokError(Msg);
71 V = getTok().getIntVal();
72 Lex();
73 return false;
74}
75
77 bool Present = (getTok().getKind() == T);
78 if (Present)
80 return Present;
81}
82
83bool MCAsmParser::check(bool P, const Twine &Msg) {
84 return check(P, getTok().getLoc(), Msg);
85}
86
87bool MCAsmParser::check(bool P, SMLoc Loc, const Twine &Msg) {
88 if (P)
89 return Error(Loc, Msg);
90 return false;
91}
92
94 return Error(getLexer().getLoc(), Msg, Range);
95}
96
98 MCPendingError PErr;
99 PErr.Loc = L;
100 Msg.toVector(PErr.Msg);
101 PErr.Range = Range;
102 PendingErrors.push_back(PErr);
103
104 // If we threw this parsing error after a lexing error, this should
105 // supercede the lexing error and so we remove it from the Lexer
106 // before it can propagate
107 if (getTok().is(AsmToken::Error))
108 getLexer().Lex();
109 return true;
110}
111
113 // Make sure lexing errors have propagated to the parser.
114 if (getTok().is(AsmToken::Error))
115 Lex();
116 for (auto &PErr : PendingErrors)
117 Suffix.toVector(PErr.Msg);
118 return true;
119}
120
121bool MCAsmParser::parseMany(function_ref<bool()> parseOne, bool hasComma) {
123 return false;
124 while (true) {
125 if (parseOne())
126 return true;
128 return false;
129 if (hasComma && parseToken(AsmToken::Comma))
130 return true;
131 }
132 return false;
133}
134
136 SMLoc L;
137 return parseExpression(Res, L);
138}
139
141 int64_t &IntegerValue) {
142 // Parse a .gnu_attribute with numerical tag and value.
143 StringRef S(L.getPointer());
144 SMLoc TagLoc;
145 TagLoc = getTok().getLoc();
146 const AsmToken &Tok = getTok();
147 if (Tok.isNot(AsmToken::Integer))
148 return false;
149 Tag = Tok.getIntVal();
150 Lex(); // Eat the Tag
151 Lex(); // Eat the comma
152 if (Tok.isNot(AsmToken::Integer))
153 return false;
154 IntegerValue = Tok.getIntVal();
155 Lex(); // Eat the IntegerValue
156 return true;
157}
158
160 StringRef Name;
161 if (parseIdentifier(Name))
162 return true;
163
164 Res = getContext().parseSymbol(Name);
165 return false;
166}
167
169 // Cannot completely remove virtual function even in release mode.
170#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
171 MCTargetOptions Opts;
172 dbgs() << " ";
173 print(dbgs(), MCAsmInfo(Opts));
174#endif
175}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define T
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
#define P(N)
const char * Msg
const AsmToken & getTok() const
Get the current (last) lexed token.
Definition AsmLexer.h:119
const AsmToken & Lex()
Consume the next token from the input stream and return it.
Definition AsmLexer.h:93
Target independent representation for an assembler token.
Definition MCAsmMacro.h:22
LLVM_ABI SMLoc getLoc() const
Definition AsmLexer.cpp:31
int64_t getIntVal() const
Definition MCAsmMacro.h:108
bool isNot(TokenKind K) const
Definition MCAsmMacro.h:76
TokenKind getKind() const
Definition MCAsmMacro.h:74
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition MCAsmInfo.h:67
bool parseMany(function_ref< bool()> parseOne, bool hasComma=true)
bool Error(SMLoc L, const Twine &Msg, SMRange Range={})
Return an error at the location L, with the message Msg.
bool parseToken(AsmToken::TokenKind T, const Twine &Msg="unexpected token")
bool addErrorSuffix(const Twine &Suffix)
bool check(bool P, const Twine &Msg)
bool parseIntToken(int64_t &V, const Twine &ErrMsg="expected integer")
MCContext & getContext()
virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc)=0
Parse an arbitrary expression.
AsmLexer & getLexer()
const AsmToken & getTok() const
Get the current AsmToken from the stream.
virtual bool parseIdentifier(StringRef &Res)=0
Parse an identifier or string (as a quoted identifier) and set Res to the identifier contents.
bool parseSymbol(MCSymbol *&Res)
Parse identifier and get or create symbol for it.
SmallVector< MCPendingError, 0 > PendingErrors
MCAsmParser(MCContext &, MCStreamer &, SourceMgr &, const MCAsmInfo &)
bool parseOptionalToken(AsmToken::TokenKind T)
Attempt to parse and consume token, returning true on success.
MCStreamer & Out
virtual const AsmToken & Lex()=0
Get the next AsmToken in the stream, possibly handling file inclusion first.
SourceMgr & SrcMgr
bool TokError(const Twine &Msg, SMRange Range={})
Report an error at the current lexer location.
MCContext & Ctx
const MCAsmInfo & MAI
virtual ~MCAsmParser()
bool parseTokenLoc(SMLoc &Loc)
bool parseGNUAttribute(SMLoc L, int64_t &Tag, int64_t &IntegerValue)
Parse a .gnu_attribute.
void setTargetParser(MCTargetAsmParser &P)
Context object for machine code objects.
Definition MCContext.h:83
LLVM_ABI MCSymbol * parseSymbol(const Twine &Name)
Variant of getOrCreateSymbol that handles backslash-escaped symbols.
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:34
virtual void dump() const
dump - Print to the debug stream.
virtual void print(raw_ostream &, const MCAsmInfo &) const =0
print - Print a debug representation of the operand to the given stream.
Streaming machine code generation interface.
Definition MCStreamer.h:222
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
MCTargetAsmParser - Generic interface to target specific assembly parsers.
Represents a location in source code.
Definition SMLoc.h:22
Represents a range in source code.
Definition SMLoc.h:47
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition SourceMgr.h:34
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM_ABI void toVector(SmallVectorImpl< char > &Out) const
Append the concatenated string into the given SmallString or SmallVector.
Definition Twine.cpp:32
An efficient, type-erasing, non-owning reference to a callable.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209