LLVM  16.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"
17 #include "llvm/Support/Debug.h"
18 #include "llvm/Support/SMLoc.h"
20 #include <cassert>
21 
22 using namespace llvm;
23 
24 namespace llvm {
26  "asm-macro-max-nesting-depth", cl::init(20), cl::Hidden,
27  cl::desc("The maximum nesting depth allowed for assembly macros."));
28 }
29 
30 MCAsmParser::MCAsmParser() = default;
31 
32 MCAsmParser::~MCAsmParser() = default;
33 
35  assert(!TargetParser && "Target parser is already initialized!");
36  TargetParser = &P;
37  TargetParser->Initialize(*this);
38 }
39 
40 const AsmToken &MCAsmParser::getTok() const {
41  return getLexer().getTok();
42 }
43 
45  Loc = getTok().getLoc();
46  return false;
47 }
48 
50  if (getTok().getKind() != AsmToken::EndOfStatement)
51  return Error(getTok().getLoc(), "expected newline");
52  Lex();
53  return false;
54 }
55 
57  if (getTok().getKind() != AsmToken::EndOfStatement)
58  return Error(getTok().getLoc(), Msg);
59  Lex();
60  return false;
61 }
62 
65  return parseEOL(Msg);
66  if (getTok().getKind() != T)
67  return Error(getTok().getLoc(), Msg);
68  Lex();
69  return false;
70 }
71 
72 bool 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 
81  bool Present = (getTok().getKind() == T);
82  if (Present)
83  parseToken(T);
84  return Present;
85 }
86 
87 bool MCAsmParser::check(bool P, const Twine &Msg) {
88  return check(P, getTok().getLoc(), Msg);
89 }
90 
91 bool MCAsmParser::check(bool P, SMLoc Loc, const Twine &Msg) {
92  if (P)
93  return Error(Loc, Msg);
94  return false;
95 }
96 
97 bool MCAsmParser::TokError(const Twine &Msg, SMRange Range) {
98  return Error(getLexer().getLoc(), Msg, Range);
99 }
100 
101 bool MCAsmParser::Error(SMLoc L, const Twine &Msg, SMRange Range) {
102 
103  MCPendingError PErr;
104  PErr.Loc = L;
105  Msg.toVector(PErr.Msg);
106  PErr.Range = Range;
107  PendingErrors.push_back(PErr);
108 
109  // If we threw this parsing error after a lexing error, this should
110  // supercede the lexing error and so we remove it from the Lexer
111  // before it can propagate
112  if (getTok().is(AsmToken::Error))
113  getLexer().Lex();
114  return true;
115 }
116 
117 bool MCAsmParser::addErrorSuffix(const Twine &Suffix) {
118  // Make sure lexing errors have propagated to the parser.
119  if (getTok().is(AsmToken::Error))
120  Lex();
121  for (auto &PErr : PendingErrors)
122  Suffix.toVector(PErr.Msg);
123  return true;
124 }
125 
126 bool MCAsmParser::parseMany(function_ref<bool()> parseOne, bool hasComma) {
128  return false;
129  while (true) {
130  if (parseOne())
131  return true;
133  return false;
134  if (hasComma && parseToken(AsmToken::Comma))
135  return true;
136  }
137  return false;
138 }
139 
141  SMLoc L;
142  return parseExpression(Res, L);
143 }
144 
146  int64_t &IntegerValue) {
147  // Parse a .gnu_attribute with numerical tag and value.
148  StringRef S(L.getPointer());
149  SMLoc TagLoc;
150  TagLoc = getTok().getLoc();
151  const AsmToken &Tok = getTok();
152  if (Tok.isNot(AsmToken::Integer))
153  return false;
154  Tag = Tok.getIntVal();
155  Lex(); // Eat the Tag
156  Lex(); // Eat the comma
157  if (Tok.isNot(AsmToken::Integer))
158  return false;
159  IntegerValue = Tok.getIntVal();
160  Lex(); // Eat the IntegerValue
161  return true;
162 }
163 
165  // Cannot completely remove virtual function even in release mode.
166 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
167  dbgs() << " " << *this;
168 #endif
169 }
is
should just be implemented with a CLZ instruction Since there are other e that share this it would be best to implement this in a target independent as zero is the default value for the binary encoder e add r0 add r5 Register operands should be distinct That is
Definition: README.txt:725
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
T
StringRef.h
P
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
Definition: README-SSE.txt:411
llvm::AsmToken::EndOfStatement
@ EndOfStatement
Definition: MCAsmMacro.h:42
llvm::MCAsmParser::parseEOL
bool parseEOL()
Definition: MCAsmParser.cpp:49
llvm::AsmToken::getIntVal
int64_t getIntVal() const
Definition: MCAsmMacro.h:115
MCParsedAsmOperand.h
llvm::MCAsmLexer::getTok
const AsmToken & getTok() const
Get the current (last) lexed token.
Definition: MCAsmLexer.h:106
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:140
llvm::AsmToken::Integer
@ Integer
Definition: MCAsmMacro.h:32
llvm::MCAsmParser::parseOptionalToken
bool parseOptionalToken(AsmToken::TokenKind T)
Attempt to parse and consume token, returning true on success.
Definition: MCAsmParser.cpp:80
llvm::MCAsmParser::~MCAsmParser
virtual ~MCAsmParser()
llvm::MCAsmLexer::Lex
const AsmToken & Lex()
Consume the next token from the input stream and return it.
Definition: MCAsmLexer.h:79
llvm::dwarf::Tag
Tag
Definition: Dwarf.h:105
MCAsmParser.h
llvm::MCAsmParser::MCPendingError
Definition: MCAsmParser.h:130
MCTargetAsmParser.h
llvm::MCParsedAsmOperand::dump
virtual void dump() const
dump - Print to the debug stream.
Definition: MCAsmParser.cpp:164
llvm::AsmToken
Target independent representation for an assembler token.
Definition: MCAsmMacro.h:21
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
CommandLine.h
llvm::MCAsmParser::Lex
virtual const AsmToken & Lex()=0
Get the next AsmToken in the stream, possibly handling file inclusion first.
llvm::MCAsmParser::TokError
bool TokError(const Twine &Msg, SMRange Range=std::nullopt)
Report an error at the current lexer location.
Definition: MCAsmParser.cpp:97
llvm::MCAsmParser::parseExpression
virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc)=0
Parse an arbitrary expression.
llvm::SMLoc
Represents a location in source code.
Definition: SMLoc.h:23
Twine.h
llvm::MCAsmParser::parseIntToken
bool parseIntToken(int64_t &V, const Twine &ErrMsg)
Definition: MCAsmParser.cpp:72
llvm::MCAsmParser::PendingErrors
SmallVector< MCPendingError, 0 > PendingErrors
Definition: MCAsmParser.h:142
SMLoc.h
llvm::AsmToken::getKind
TokenKind getKind() const
Definition: MCAsmMacro.h:81
llvm::MCAsmParser::MCPendingError::Range
SMRange Range
Definition: MCAsmParser.h:133
llvm::AsmToken::Error
@ Error
Definition: MCAsmMacro.h:25
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLFunctionalExtras.h:36
llvm::cl::opt
Definition: CommandLine.h:1412
MCAsmLexer.h
llvm::MCAsmParser::addErrorSuffix
bool addErrorSuffix(const Twine &Suffix)
Definition: MCAsmParser.cpp:117
llvm::MCAsmParser::parseMany
bool parseMany(function_ref< bool()> parseOne, bool hasComma=true)
Definition: MCAsmParser.cpp:126
llvm::MCAsmParserExtension::Initialize
virtual void Initialize(MCAsmParser &Parser)
Initialize the extension for parsing using the given Parser.
Definition: MCAsmParserExtension.cpp:21
llvm::Twine::toVector
void toVector(SmallVectorImpl< char > &Out) const
Append the concatenated string into the given SmallString or SmallVector.
Definition: Twine.cpp:32
llvm::MCAsmParser::check
bool check(bool P, const Twine &Msg)
Definition: MCAsmParser.cpp:87
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:447
llvm::MCAsmParser::MCPendingError::Loc
SMLoc Loc
Definition: MCAsmParser.h:131
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::AsmToken::Comma
@ Comma
Definition: MCAsmMacro.h:49
llvm::MCAsmParser::Error
bool Error(SMLoc L, const Twine &Msg, SMRange Range=std::nullopt)
Return an error at the location L, with the message Msg.
Definition: MCAsmParser.cpp:101
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::MCAsmParser::getTok
const AsmToken & getTok() const
Get the current AsmToken from the stream.
Definition: MCAsmParser.cpp:40
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::AMDGPU::SendMsg::Msg
const CustomOperand< const MCSubtargetInfo & > Msg[]
Definition: AMDGPUAsmUtils.cpp:39
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
llvm::AsmToken::isNot
bool isNot(TokenKind K) const
Definition: MCAsmMacro.h:83
llvm::SMLoc::getPointer
const char * getPointer() const
Definition: SMLoc.h:34
llvm::MCAsmParser::MCPendingError::Msg
SmallString< 64 > Msg
Definition: MCAsmParser.h:132
llvm::MCTargetAsmParser
MCTargetAsmParser - Generic interface to target specific assembly parsers.
Definition: MCTargetAsmParser.h:314
llvm::AsmToken::TokenKind
TokenKind
Definition: MCAsmMacro.h:23
llvm::MCAsmParser::parseGNUAttribute
bool parseGNUAttribute(SMLoc L, int64_t &Tag, int64_t &IntegerValue)
Parse a .gnu_attribute.
Definition: MCAsmParser.cpp:145
llvm::MCAsmParser::getLexer
virtual MCAsmLexer & getLexer()=0
llvm::SMRange
Represents a range in source code.
Definition: SMLoc.h:48
llvm::AsmMacroMaxNestingDepth
cl::opt< unsigned > AsmMacroMaxNestingDepth
Definition: MasmParser.cpp:1086
llvm::cl::desc
Definition: CommandLine.h:413
raw_ostream.h
llvm::AsmToken::getLoc
SMLoc getLoc() const
Definition: MCAsmLexer.cpp:26
llvm::MCExpr
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
Debug.h
llvm::MCAsmParser::MCAsmParser
MCAsmParser()
llvm::MCAsmParser::parseToken
bool parseToken(AsmToken::TokenKind T, const Twine &Msg="unexpected token")
Definition: MCAsmParser.cpp:63
llvm::MCAsmParser::setTargetParser
void setTargetParser(MCTargetAsmParser &P)
Definition: MCAsmParser.cpp:34
llvm::MCAsmParser::parseTokenLoc
bool parseTokenLoc(SMLoc &Loc)
Definition: MCAsmParser.cpp:44