LLVM 22.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"
29#include <optional>
30
31using namespace llvm;
32
33namespace {
34
35class WasmAsmParser : public MCAsmParserExtension {
36 MCAsmParser *Parser = nullptr;
37 AsmLexer *Lexer = nullptr;
38
39 template<bool (WasmAsmParser::*HandlerMethod)(StringRef, SMLoc)>
40 void addDirectiveHandler(StringRef Directive) {
41 MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
42 this, HandleDirective<WasmAsmParser, HandlerMethod>);
43
44 getParser().addDirectiveHandler(Directive, Handler);
45 }
46
47public:
48 WasmAsmParser() { BracketExpressionsSupported = true; }
49
50 void Initialize(MCAsmParser &P) override {
51 Parser = &P;
52 Lexer = &Parser->getLexer();
53 // Call the base implementation.
55
56 addDirectiveHandler<&WasmAsmParser::parseSectionDirectiveText>(".text");
57 addDirectiveHandler<&WasmAsmParser::parseSectionDirectiveData>(".data");
58 addDirectiveHandler<&WasmAsmParser::parseSectionDirective>(".section");
59 addDirectiveHandler<&WasmAsmParser::parseDirectiveSize>(".size");
60 addDirectiveHandler<&WasmAsmParser::parseDirectiveType>(".type");
61 addDirectiveHandler<&WasmAsmParser::ParseDirectiveIdent>(".ident");
62 addDirectiveHandler<
63 &WasmAsmParser::ParseDirectiveSymbolAttribute>(".weak");
64 addDirectiveHandler<
65 &WasmAsmParser::ParseDirectiveSymbolAttribute>(".local");
66 addDirectiveHandler<
67 &WasmAsmParser::ParseDirectiveSymbolAttribute>(".internal");
68 addDirectiveHandler<
69 &WasmAsmParser::ParseDirectiveSymbolAttribute>(".hidden");
70 }
71
72 bool error(const StringRef &Msg, const AsmToken &Tok) {
73 return Parser->Error(Tok.getLoc(), Msg + Tok.getString());
74 }
75
76 bool isNext(AsmToken::TokenKind Kind) {
77 auto Ok = Lexer->is(Kind);
78 if (Ok)
79 Lex();
80 return Ok;
81 }
82
83 bool expect(AsmToken::TokenKind Kind, const char *KindName) {
84 if (!isNext(Kind))
85 return error(std::string("Expected ") + KindName + ", instead got: ",
86 Lexer->getTok());
87 return false;
88 }
89
90 bool parseSectionDirectiveText(StringRef, SMLoc) {
91 // FIXME: .text currently no-op.
92 return false;
93 }
94
95 bool parseSectionDirectiveData(StringRef, SMLoc) {
96 auto *S = getContext().getObjectFileInfo()->getDataSection();
97 getStreamer().switchSection(S);
98 return false;
99 }
100
101 uint32_t parseSectionFlags(StringRef FlagStr, bool &Passive, bool &Group) {
102 uint32_t flags = 0;
103 for (char C : FlagStr) {
104 switch (C) {
105 case 'p':
106 Passive = true;
107 break;
108 case 'G':
109 Group = true;
110 break;
111 case 'T':
113 break;
114 case 'S':
116 break;
117 case 'R':
119 break;
120 default:
121 return -1U;
122 }
123 }
124 return flags;
125 }
126
127 bool parseGroup(StringRef &GroupName) {
128 if (Lexer->isNot(AsmToken::Comma))
129 return TokError("expected group name");
130 Lex();
131 if (Lexer->is(AsmToken::Integer)) {
132 GroupName = getTok().getString();
133 Lex();
134 } else if (Parser->parseIdentifier(GroupName)) {
135 return TokError("invalid group name");
136 }
137 if (Lexer->is(AsmToken::Comma)) {
138 Lex();
139 StringRef Linkage;
140 if (Parser->parseIdentifier(Linkage))
141 return TokError("invalid linkage");
142 if (Linkage != "comdat")
143 return TokError("Linkage must be 'comdat'");
144 }
145 return false;
146 }
147
148 bool parseSectionDirective(StringRef, SMLoc loc) {
149 StringRef Name;
150 if (Parser->parseIdentifier(Name))
151 return TokError("expected identifier in directive");
152
153 if (expect(AsmToken::Comma, ","))
154 return true;
155
156 if (Lexer->isNot(AsmToken::String))
157 return error("expected string in directive, instead got: ", Lexer->getTok());
158
159 auto Kind = StringSwitch<std::optional<SectionKind>>(Name)
160 .StartsWith(".data", SectionKind::getData())
161 .StartsWith(".tdata", SectionKind::getThreadData())
162 .StartsWith(".tbss", SectionKind::getThreadBSS())
163 .StartsWith(".rodata", SectionKind::getReadOnly())
164 .StartsWith(".text", SectionKind::getText())
165 .StartsWith(".custom_section", SectionKind::getMetadata())
166 .StartsWith(".bss", SectionKind::getBSS())
167 // See use of .init_array in WasmObjectWriter and
168 // TargetLoweringObjectFileWasm
169 .StartsWith(".init_array", SectionKind::getData())
170 .StartsWith(".debug_", SectionKind::getMetadata())
171 .Default(SectionKind::getData());
172
173 // Update section flags if present in this .section directive
174 bool Passive = false;
175 bool Group = false;
176 uint32_t Flags =
177 parseSectionFlags(getTok().getStringContents(), Passive, Group);
178 if (Flags == -1U)
179 return TokError("unknown flag");
180
181 Lex();
182
183 if (expect(AsmToken::Comma, ",") || expect(AsmToken::At, "@"))
184 return true;
185
186 StringRef GroupName;
187 if (Group && parseGroup(GroupName))
188 return true;
189
190 if (expect(AsmToken::EndOfStatement, "eol"))
191 return true;
192
193 // TODO: Parse UniqueID
194 MCSectionWasm *WS = getContext().getWasmSection(
195 Name, *Kind, Flags, GroupName, MCSection::NonUniqueID);
196
197 if (WS->getSegmentFlags() != Flags)
198 Parser->Error(loc, "changed section flags for " + Name +
199 ", expected: 0x" +
201
202 if (Passive) {
203 if (!WS->isWasmData())
204 return Parser->Error(loc, "Only data sections can be passive");
205 WS->setPassive();
206 }
207
208 getStreamer().switchSection(WS);
209 return false;
210 }
211
212 // TODO: This function is almost the same as ELFAsmParser::ParseDirectiveSize
213 // so maybe could be shared somehow.
214 bool parseDirectiveSize(StringRef, SMLoc Loc) {
215 MCSymbol *Sym;
216 if (Parser->parseSymbol(Sym))
217 return TokError("expected identifier in directive");
218 if (expect(AsmToken::Comma, ","))
219 return true;
220 const MCExpr *Expr;
221 if (Parser->parseExpression(Expr))
222 return true;
223 if (expect(AsmToken::EndOfStatement, "eol"))
224 return true;
225 auto WasmSym = static_cast<const MCSymbolWasm *>(Sym);
226 if (WasmSym->isFunction()) {
227 // Ignore .size directives for function symbols. They get their size
228 // set automatically based on their content.
229 Warning(Loc, ".size directive ignored for function symbols");
230 } else {
231 getStreamer().emitELFSize(Sym, Expr);
232 }
233 return false;
234 }
235
236 bool parseDirectiveType(StringRef, SMLoc) {
237 // This could be the start of a function, check if followed by
238 // "label,@function"
239 if (!Lexer->is(AsmToken::Identifier))
240 return error("Expected label after .type directive, got: ",
241 Lexer->getTok());
242 auto *WasmSym = static_cast<MCSymbolWasm *>(
243 getStreamer().getContext().parseSymbol(Lexer->getTok().getString()));
244 Lex();
245 if (!(isNext(AsmToken::Comma) && isNext(AsmToken::At) &&
246 Lexer->is(AsmToken::Identifier)))
247 return error("Expected label,@type declaration, got: ", Lexer->getTok());
248 auto TypeName = Lexer->getTok().getString();
249 if (TypeName == "function") {
250 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
251 auto *Current =
252 static_cast<MCSectionWasm *>(getStreamer().getCurrentSectionOnly());
253 if (Current->getGroup())
254 WasmSym->setComdat(true);
255 } else if (TypeName == "global")
256 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
257 else if (TypeName == "object")
258 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_DATA);
259 else
260 return error("Unknown WASM symbol type: ", Lexer->getTok());
261 Lex();
262 return expect(AsmToken::EndOfStatement, "EOL");
263 }
264
265 // FIXME: Shared with ELF.
266 /// ParseDirectiveIdent
267 /// ::= .ident string
268 bool ParseDirectiveIdent(StringRef, SMLoc) {
269 if (getLexer().isNot(AsmToken::String))
270 return TokError("unexpected token in '.ident' directive");
271 StringRef Data = getTok().getIdentifier();
272 Lex();
273 if (getLexer().isNot(AsmToken::EndOfStatement))
274 return TokError("unexpected token in '.ident' directive");
275 Lex();
276 getStreamer().emitIdent(Data);
277 return false;
278 }
279
280 // FIXME: Shared with ELF.
281 /// ParseDirectiveSymbolAttribute
282 /// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
283 bool ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
284 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
285 .Case(".weak", MCSA_Weak)
286 .Case(".local", MCSA_Local)
287 .Case(".hidden", MCSA_Hidden)
288 .Case(".internal", MCSA_Internal)
289 .Case(".protected", MCSA_Protected)
290 .Default(MCSA_Invalid);
291 assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
292 if (getLexer().isNot(AsmToken::EndOfStatement)) {
293 while (true) {
294 MCSymbol *Sym;
295 if (getParser().parseSymbol(Sym))
296 return TokError("expected identifier in directive");
297 getStreamer().emitSymbolAttribute(Sym, Attr);
298 if (getLexer().is(AsmToken::EndOfStatement))
299 break;
300 if (getLexer().isNot(AsmToken::Comma))
301 return TokError("unexpected token in directive");
302 Lex();
303 }
304 }
305 Lex();
306 return false;
307 }
308};
309
310} // end anonymous namespace
311
312namespace llvm {
313
315 return new WasmAsmParser;
316}
317
318} // end namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool isNot(const MachineRegisterInfo &MRI, const MachineInstr &MI)
DXIL Finalize Linkage
static unsigned parseSectionFlags(const Triple &TT, StringRef flagsStr, bool *UseLastGroup)
#define P(N)
This file contains some functions that are useful when dealing with strings.
#define error(X)
LLVM_ABI SMLoc getLoc() const
Definition AsmLexer.cpp:32
StringRef getString() const
Get the string for the current token, this includes all characters (for example, the quotes on string...
Definition MCAsmMacro.h:103
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.
std::pair< MCAsmParserExtension *, DirectiveHandler > ExtensionDirectiveHandler
void setPassive(bool V=true)
bool isWasmData() const
unsigned getSegmentFlags() const
static constexpr unsigned NonUniqueID
Definition MCSection.h:526
static SectionKind getThreadData()
static SectionKind getMetadata()
static SectionKind getText()
static SectionKind getData()
static SectionKind getBSS()
static SectionKind getThreadBSS()
static SectionKind getReadOnly()
constexpr char TypeName[]
Key for Kernel::Arg::Metadata::mTypeName.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
LLVM_ABI SimpleSymbol parseSymbol(StringRef SymName)
Get symbol classification by parsing the name of a symbol.
Definition Symbol.cpp:75
Context & getContext() const
Definition BasicBlock.h:99
@ WASM_SYMBOL_TYPE_GLOBAL
Definition Wasm.h:222
@ WASM_SYMBOL_TYPE_DATA
Definition Wasm.h:221
@ WASM_SYMBOL_TYPE_FUNCTION
Definition Wasm.h:220
@ WASM_SEG_FLAG_RETAIN
Definition Wasm.h:231
@ WASM_SEG_FLAG_TLS
Definition Wasm.h:230
@ WASM_SEG_FLAG_STRINGS
Definition Wasm.h:229
This is an optimization pass for GlobalISel generic memory operations.
std::string utohexstr(uint64_t X, bool LowerCase=false, unsigned Width=0)
MCAsmParserExtension * createWasmAsmParser()
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
@ MCSA_Local
.local (ELF)
@ MCSA_Protected
.protected (ELF)
@ MCSA_Internal
.internal (ELF)
@ MCSA_Weak
.weak
@ MCSA_Hidden
.hidden (ELF)
@ MCSA_Invalid
Not a valid directive.