LLVM 19.0.0git
WasmAsmParser.cpp
Go to the documentation of this file.
1//===- WasmAsmParser.cpp - Wasm Assembly Parser -----------------------------===//
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// Note, this is for wasm, the binary format (analogous to ELF), not wasm,
10// the instruction set (analogous to x86), for which parsing code lives in
11// WebAssemblyAsmParser.
12//
13// This file contains processing for generic directives implemented using
14// MCTargetStreamer, the ones that depend on WebAssemblyTargetStreamer are in
15// WebAssemblyAsmParser.
16//
17//===----------------------------------------------------------------------===//
18
21#include "llvm/MC/MCContext.h"
27#include "llvm/MC/MCStreamer.h"
30#include <optional>
31
32using namespace llvm;
33
34namespace {
35
36class WasmAsmParser : public MCAsmParserExtension {
37 MCAsmParser *Parser = nullptr;
38 MCAsmLexer *Lexer = nullptr;
39
40 template<bool (WasmAsmParser::*HandlerMethod)(StringRef, SMLoc)>
41 void addDirectiveHandler(StringRef Directive) {
42 MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
43 this, HandleDirective<WasmAsmParser, HandlerMethod>);
44
46 }
47
48public:
49 WasmAsmParser() { BracketExpressionsSupported = true; }
50
51 void Initialize(MCAsmParser &P) override {
52 Parser = &P;
53 Lexer = &Parser->getLexer();
54 // Call the base implementation.
56
57 addDirectiveHandler<&WasmAsmParser::parseSectionDirectiveText>(".text");
58 addDirectiveHandler<&WasmAsmParser::parseSectionDirectiveData>(".data");
59 addDirectiveHandler<&WasmAsmParser::parseSectionDirective>(".section");
60 addDirectiveHandler<&WasmAsmParser::parseDirectiveSize>(".size");
61 addDirectiveHandler<&WasmAsmParser::parseDirectiveType>(".type");
62 addDirectiveHandler<&WasmAsmParser::ParseDirectiveIdent>(".ident");
63 addDirectiveHandler<
64 &WasmAsmParser::ParseDirectiveSymbolAttribute>(".weak");
65 addDirectiveHandler<
66 &WasmAsmParser::ParseDirectiveSymbolAttribute>(".local");
67 addDirectiveHandler<
68 &WasmAsmParser::ParseDirectiveSymbolAttribute>(".internal");
69 addDirectiveHandler<
70 &WasmAsmParser::ParseDirectiveSymbolAttribute>(".hidden");
71 }
72
73 bool error(const StringRef &Msg, const AsmToken &Tok) {
74 return Parser->Error(Tok.getLoc(), Msg + Tok.getString());
75 }
76
77 bool isNext(AsmToken::TokenKind Kind) {
78 auto Ok = Lexer->is(Kind);
79 if (Ok)
80 Lex();
81 return Ok;
82 }
83
84 bool expect(AsmToken::TokenKind Kind, const char *KindName) {
85 if (!isNext(Kind))
86 return error(std::string("Expected ") + KindName + ", instead got: ",
87 Lexer->getTok());
88 return false;
89 }
90
91 bool parseSectionDirectiveText(StringRef, SMLoc) {
92 // FIXME: .text currently no-op.
93 return false;
94 }
95
96 bool parseSectionDirectiveData(StringRef, SMLoc) {
99 return false;
100 }
101
102 uint32_t parseSectionFlags(StringRef FlagStr, bool &Passive, bool &Group) {
103 uint32_t flags = 0;
104 for (char C : FlagStr) {
105 switch (C) {
106 case 'p':
107 Passive = true;
108 break;
109 case 'G':
110 Group = true;
111 break;
112 case 'T':
114 break;
115 case 'S':
117 break;
118 case 'R':
120 break;
121 default:
122 return -1U;
123 }
124 }
125 return flags;
126 }
127
128 bool parseGroup(StringRef &GroupName) {
129 if (Lexer->isNot(AsmToken::Comma))
130 return TokError("expected group name");
131 Lex();
132 if (Lexer->is(AsmToken::Integer)) {
133 GroupName = getTok().getString();
134 Lex();
135 } else if (Parser->parseIdentifier(GroupName)) {
136 return TokError("invalid group name");
137 }
138 if (Lexer->is(AsmToken::Comma)) {
139 Lex();
141 if (Parser->parseIdentifier(Linkage))
142 return TokError("invalid linkage");
143 if (Linkage != "comdat")
144 return TokError("Linkage must be 'comdat'");
145 }
146 return false;
147 }
148
149 bool parseSectionDirective(StringRef, SMLoc loc) {
151 if (Parser->parseIdentifier(Name))
152 return TokError("expected identifier in directive");
153
154 if (expect(AsmToken::Comma, ","))
155 return true;
156
157 if (Lexer->isNot(AsmToken::String))
158 return error("expected string in directive, instead got: ", Lexer->getTok());
159
161 .StartsWith(".data", SectionKind::getData())
162 .StartsWith(".tdata", SectionKind::getThreadData())
163 .StartsWith(".tbss", SectionKind::getThreadBSS())
164 .StartsWith(".rodata", SectionKind::getReadOnly())
165 .StartsWith(".text", SectionKind::getText())
166 .StartsWith(".custom_section", SectionKind::getMetadata())
167 .StartsWith(".bss", SectionKind::getBSS())
168 // See use of .init_array in WasmObjectWriter and
169 // TargetLoweringObjectFileWasm
170 .StartsWith(".init_array", SectionKind::getData())
171 .StartsWith(".debug_", SectionKind::getMetadata())
173
174 // Update section flags if present in this .section directive
175 bool Passive = false;
176 bool Group = false;
178 parseSectionFlags(getTok().getStringContents(), Passive, Group);
179 if (Flags == -1U)
180 return TokError("unknown flag");
181
182 Lex();
183
184 if (expect(AsmToken::Comma, ",") || expect(AsmToken::At, "@"))
185 return true;
186
187 StringRef GroupName;
188 if (Group && parseGroup(GroupName))
189 return true;
190
191 if (expect(AsmToken::EndOfStatement, "eol"))
192 return true;
193
194 // TODO: Parse UniqueID
196 Name, *Kind, Flags, GroupName, MCContext::GenericSectionID);
197
198 if (WS->getSegmentFlags() != Flags)
199 Parser->Error(loc, "changed section flags for " + Name +
200 ", expected: 0x" +
201 utohexstr(WS->getSegmentFlags()));
202
203 if (Passive) {
204 if (!WS->isWasmData())
205 return Parser->Error(loc, "Only data sections can be passive");
206 WS->setPassive();
207 }
208
210 return false;
211 }
212
213 // TODO: This function is almost the same as ELFAsmParser::ParseDirectiveSize
214 // so maybe could be shared somehow.
215 bool parseDirectiveSize(StringRef, SMLoc Loc) {
217 if (Parser->parseIdentifier(Name))
218 return TokError("expected identifier in directive");
220 if (expect(AsmToken::Comma, ","))
221 return true;
222 const MCExpr *Expr;
223 if (Parser->parseExpression(Expr))
224 return true;
225 if (expect(AsmToken::EndOfStatement, "eol"))
226 return true;
227 auto WasmSym = cast<MCSymbolWasm>(Sym);
228 if (WasmSym->isFunction()) {
229 // Ignore .size directives for function symbols. They get their size
230 // set automatically based on their content.
231 Warning(Loc, ".size directive ignored for function symbols");
232 } else {
233 getStreamer().emitELFSize(Sym, Expr);
234 }
235 return false;
236 }
237
238 bool parseDirectiveType(StringRef, SMLoc) {
239 // This could be the start of a function, check if followed by
240 // "label,@function"
241 if (!Lexer->is(AsmToken::Identifier))
242 return error("Expected label after .type directive, got: ",
243 Lexer->getTok());
244 auto WasmSym = cast<MCSymbolWasm>(
245 getStreamer().getContext().getOrCreateSymbol(
246 Lexer->getTok().getString()));
247 Lex();
248 if (!(isNext(AsmToken::Comma) && isNext(AsmToken::At) &&
249 Lexer->is(AsmToken::Identifier)))
250 return error("Expected label,@type declaration, got: ", Lexer->getTok());
251 auto TypeName = Lexer->getTok().getString();
252 if (TypeName == "function") {
253 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
254 auto *Current =
255 cast<MCSectionWasm>(getStreamer().getCurrentSection().first);
256 if (Current->getGroup())
257 WasmSym->setComdat(true);
258 } else if (TypeName == "global")
259 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
260 else if (TypeName == "object")
261 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_DATA);
262 else
263 return error("Unknown WASM symbol type: ", Lexer->getTok());
264 Lex();
265 return expect(AsmToken::EndOfStatement, "EOL");
266 }
267
268 // FIXME: Shared with ELF.
269 /// ParseDirectiveIdent
270 /// ::= .ident string
271 bool ParseDirectiveIdent(StringRef, SMLoc) {
273 return TokError("unexpected token in '.ident' directive");
274 StringRef Data = getTok().getIdentifier();
275 Lex();
277 return TokError("unexpected token in '.ident' directive");
278 Lex();
279 getStreamer().emitIdent(Data);
280 return false;
281 }
282
283 // FIXME: Shared with ELF.
284 /// ParseDirectiveSymbolAttribute
285 /// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
286 bool ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
288 .Case(".weak", MCSA_Weak)
289 .Case(".local", MCSA_Local)
290 .Case(".hidden", MCSA_Hidden)
291 .Case(".internal", MCSA_Internal)
292 .Case(".protected", MCSA_Protected)
294 assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
296 while (true) {
298 if (getParser().parseIdentifier(Name))
299 return TokError("expected identifier in directive");
303 break;
305 return TokError("unexpected token in directive");
306 Lex();
307 }
308 }
309 Lex();
310 return false;
311 }
312};
313
314} // end anonymous namespace
315
316namespace llvm {
317
319 return new WasmAsmParser;
320}
321
322} // end namespace llvm
static bool isNot(const MachineRegisterInfo &MRI, const MachineInstr &MI)
static unsigned parseSectionFlags(const Triple &TT, StringRef flagsStr, bool *UseLastGroup)
std::string Name
Symbol * Sym
Definition: ELF_riscv.cpp:479
#define P(N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some functions that are useful when dealing with strings.
#define error(X)
Target independent representation for an assembler token.
Definition: MCAsmMacro.h:21
SMLoc getLoc() const
Definition: MCAsmLexer.cpp:26
StringRef getString() const
Get the string for the current token, this includes all characters (for example, the quotes on string...
Definition: MCAsmMacro.h:110
StringRef getIdentifier() const
Get the identifier string for the current token, which should be an identifier or a string.
Definition: MCAsmMacro.h:99
Generic assembler lexer interface, for use by target specific assembly lexers.
Definition: MCAsmLexer.h:37
bool isNot(AsmToken::TokenKind K) const
Check if the current token has kind K.
Definition: MCAsmLexer.h:144
const AsmToken & getTok() const
Get the current (last) lexed token.
Definition: MCAsmLexer.h:106
bool is(AsmToken::TokenKind K) const
Check if the current token has kind K.
Definition: MCAsmLexer.h:141
Generic interface for extending the MCAsmParser, which is implemented by target and object file assem...
virtual void Initialize(MCAsmParser &Parser)
Initialize the extension for parsing using the given Parser.
bool TokError(const Twine &Msg)
Generic assembler parser interface, for use by target specific assembly parsers.
Definition: MCAsmParser.h:123
virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc)=0
Parse an arbitrary expression.
virtual bool parseIdentifier(StringRef &Res)=0
Parse an identifier or string (as a quoted identifier) and set Res to the identifier contents.
std::pair< MCAsmParserExtension *, DirectiveHandler > ExtensionDirectiveHandler
Definition: MCAsmParser.h:127
virtual MCAsmLexer & getLexer()=0
virtual void addDirectiveHandler(StringRef Directive, ExtensionDirectiveHandler Handler)=0
bool Error(SMLoc L, const Twine &Msg, SMRange Range=std::nullopt)
Return an error at the location L, with the message Msg.
const MCObjectFileInfo * getObjectFileInfo() const
Definition: MCContext.h:450
MCSectionWasm * getWasmSection(const Twine &Section, SectionKind K, unsigned Flags=0)
Definition: MCContext.h:648
@ GenericSectionID
Pass this value as the UniqueID during section creation to get the generic section with the given nam...
Definition: MCContext.h:550
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:200
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
MCSection * getDataSection() const
This represents a section on wasm.
Definition: MCSectionWasm.h:26
void setPassive(bool V=true)
Definition: MCSectionWasm.h:85
bool isWasmData() const
Definition: MCSectionWasm.h:67
unsigned getSegmentFlags() const
Definition: MCSectionWasm.h:59
virtual bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute)=0
Add the given Attribute to Symbol.
virtual void emitELFSize(MCSymbol *Symbol, const MCExpr *Value)
Emit an ELF .size directive.
virtual void emitIdent(StringRef IdentString)
Emit the "identifiers" directive.
Definition: MCStreamer.h:911
virtual void switchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:40
Represents a location in source code.
Definition: SMLoc.h:23
static SectionKind getThreadData()
Definition: SectionKind.h:207
static SectionKind getMetadata()
Definition: SectionKind.h:188
static SectionKind getText()
Definition: SectionKind.h:190
static SectionKind getData()
Definition: SectionKind.h:213
static SectionKind getBSS()
Definition: SectionKind.h:209
static SectionKind getThreadBSS()
Definition: SectionKind.h:206
static SectionKind getReadOnly()
Definition: SectionKind.h:192
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:44
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:69
R Default(T Value)
Definition: StringSwitch.h:182
constexpr char TypeName[]
Key for Kernel::Arg::Metadata::mTypeName.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ WASM_SYMBOL_TYPE_GLOBAL
Definition: Wasm.h:210
@ WASM_SYMBOL_TYPE_DATA
Definition: Wasm.h:209
@ WASM_SYMBOL_TYPE_FUNCTION
Definition: Wasm.h:208
@ WASM_SEG_FLAG_RETAIN
Definition: Wasm.h:219
@ WASM_SEG_FLAG_TLS
Definition: Wasm.h:218
@ WASM_SEG_FLAG_STRINGS
Definition: Wasm.h:217
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MCAsmParserExtension * createWasmAsmParser()
@ Default
The result values are uniform if and only if all operands are uniform.
MCSymbolAttr
Definition: MCDirectives.h:18
@ MCSA_Local
.local (ELF)
Definition: MCDirectives.h:38
@ MCSA_Protected
.protected (ELF)
Definition: MCDirectives.h:43
@ MCSA_Internal
.internal (ELF)
Definition: MCDirectives.h:36
@ MCSA_Weak
.weak
Definition: MCDirectives.h:45
@ MCSA_Hidden
.hidden (ELF)
Definition: MCDirectives.h:33
@ MCSA_Invalid
Not a valid directive.
Definition: MCDirectives.h:19