1 //===- WasmAsmParser.cpp - Wasm Assembly Parser -----------------------------===//
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
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.
13 // This file contains processing for generic directives implemented using
14 // MCTargetStreamer, the ones that depend on WebAssemblyTargetStreamer are in
15 // WebAssemblyAsmParser.
17 //===----------------------------------------------------------------------===//
19 #include "llvm/BinaryFormat/Wasm.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCParser/MCAsmLexer.h"
22 #include "llvm/MC/MCParser/MCAsmParser.h"
23 #include "llvm/MC/MCParser/MCAsmParserExtension.h"
24 #include "llvm/MC/MCSectionWasm.h"
25 #include "llvm/MC/MCStreamer.h"
26 #include "llvm/MC/MCSymbol.h"
27 #include "llvm/MC/MCSymbolWasm.h"
28 #include "llvm/Support/MachineValueType.h"
34 class WasmAsmParser
: public MCAsmParserExtension
{
35 MCAsmParser
*Parser
= nullptr;
36 MCAsmLexer
*Lexer
= nullptr;
38 template<bool (WasmAsmParser::*HandlerMethod
)(StringRef
, SMLoc
)>
39 void addDirectiveHandler(StringRef Directive
) {
40 MCAsmParser::ExtensionDirectiveHandler Handler
= std::make_pair(
41 this, HandleDirective
<WasmAsmParser
, HandlerMethod
>);
43 getParser().addDirectiveHandler(Directive
, Handler
);
47 WasmAsmParser() { BracketExpressionsSupported
= true; }
49 void Initialize(MCAsmParser
&P
) override
{
51 Lexer
= &Parser
->getLexer();
52 // Call the base implementation.
53 this->MCAsmParserExtension::Initialize(*Parser
);
55 addDirectiveHandler
<&WasmAsmParser::parseSectionDirectiveText
>(".text");
56 addDirectiveHandler
<&WasmAsmParser::parseSectionDirective
>(".section");
57 addDirectiveHandler
<&WasmAsmParser::parseDirectiveSize
>(".size");
58 addDirectiveHandler
<&WasmAsmParser::parseDirectiveType
>(".type");
59 addDirectiveHandler
<&WasmAsmParser::ParseDirectiveIdent
>(".ident");
61 &WasmAsmParser::ParseDirectiveSymbolAttribute
>(".weak");
63 &WasmAsmParser::ParseDirectiveSymbolAttribute
>(".local");
65 &WasmAsmParser::ParseDirectiveSymbolAttribute
>(".internal");
67 &WasmAsmParser::ParseDirectiveSymbolAttribute
>(".hidden");
70 bool error(const StringRef
&Msg
, const AsmToken
&Tok
) {
71 return Parser
->Error(Tok
.getLoc(), Msg
+ Tok
.getString());
74 bool isNext(AsmToken::TokenKind Kind
) {
75 auto Ok
= Lexer
->is(Kind
);
81 bool expect(AsmToken::TokenKind Kind
, const char *KindName
) {
83 return error(std::string("Expected ") + KindName
+ ", instead got: ",
88 bool parseSectionDirectiveText(StringRef
, SMLoc
) {
89 // FIXME: .text currently no-op.
93 bool parseSectionFlags(StringRef FlagStr
, bool &Passive
) {
94 SmallVector
<StringRef
, 2> Flags
;
95 // If there are no flags, keep Flags empty
96 FlagStr
.split(Flags
, ",", -1, false);
97 for (auto &Flag
: Flags
) {
98 if (Flag
== "passive")
101 return error("Expected section flags, instead got: ", Lexer
->getTok());
106 bool parseSectionDirective(StringRef
, SMLoc
) {
108 if (Parser
->parseIdentifier(Name
))
109 return TokError("expected identifier in directive");
111 if (expect(AsmToken::Comma
, ","))
114 if (Lexer
->isNot(AsmToken::String
))
115 return error("expected string in directive, instead got: ", Lexer
->getTok());
117 auto Kind
= StringSwitch
<Optional
<SectionKind
>>(Name
)
118 .StartsWith(".data", SectionKind::getData())
119 .StartsWith(".rodata", SectionKind::getReadOnly())
120 .StartsWith(".text", SectionKind::getText())
121 .StartsWith(".custom_section", SectionKind::getMetadata())
122 .StartsWith(".bss", SectionKind::getBSS())
123 // See use of .init_array in WasmObjectWriter and
124 // TargetLoweringObjectFileWasm
125 .StartsWith(".init_array", SectionKind::getData())
126 .Default(Optional
<SectionKind
>());
127 if (!Kind
.hasValue())
128 return Parser
->Error(Lexer
->getLoc(), "unknown section kind: " + Name
);
130 MCSectionWasm
*Section
= getContext().getWasmSection(Name
, Kind
.getValue());
132 // Update section flags if present in this .section directive
133 bool Passive
= false;
134 if (parseSectionFlags(getTok().getStringContents(), Passive
))
138 if (!Section
->isWasmData())
139 return Parser
->Error(getTok().getLoc(),
140 "Only data sections can be passive");
141 Section
->setPassive();
146 if (expect(AsmToken::Comma
, ",") || expect(AsmToken::At
, "@") ||
147 expect(AsmToken::EndOfStatement
, "eol"))
150 auto WS
= getContext().getWasmSection(Name
, Kind
.getValue());
151 getStreamer().SwitchSection(WS
);
155 // TODO: This function is almost the same as ELFAsmParser::ParseDirectiveSize
156 // so maybe could be shared somehow.
157 bool parseDirectiveSize(StringRef
, SMLoc
) {
159 if (Parser
->parseIdentifier(Name
))
160 return TokError("expected identifier in directive");
161 auto Sym
= getContext().getOrCreateSymbol(Name
);
162 if (expect(AsmToken::Comma
, ","))
165 if (Parser
->parseExpression(Expr
))
167 if (expect(AsmToken::EndOfStatement
, "eol"))
169 // This is done automatically by the assembler for functions currently,
170 // so this is only currently needed for data sections:
171 getStreamer().emitELFSize(Sym
, Expr
);
175 bool parseDirectiveType(StringRef
, SMLoc
) {
176 // This could be the start of a function, check if followed by
178 if (!Lexer
->is(AsmToken::Identifier
))
179 return error("Expected label after .type directive, got: ",
181 auto WasmSym
= cast
<MCSymbolWasm
>(
182 getStreamer().getContext().getOrCreateSymbol(
183 Lexer
->getTok().getString()));
185 if (!(isNext(AsmToken::Comma
) && isNext(AsmToken::At
) &&
186 Lexer
->is(AsmToken::Identifier
)))
187 return error("Expected label,@type declaration, got: ", Lexer
->getTok());
188 auto TypeName
= Lexer
->getTok().getString();
189 if (TypeName
== "function")
190 WasmSym
->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION
);
191 else if (TypeName
== "global")
192 WasmSym
->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL
);
193 else if (TypeName
== "object")
194 WasmSym
->setType(wasm::WASM_SYMBOL_TYPE_DATA
);
196 return error("Unknown WASM symbol type: ", Lexer
->getTok());
198 return expect(AsmToken::EndOfStatement
, "EOL");
201 // FIXME: Shared with ELF.
202 /// ParseDirectiveIdent
203 /// ::= .ident string
204 bool ParseDirectiveIdent(StringRef
, SMLoc
) {
205 if (getLexer().isNot(AsmToken::String
))
206 return TokError("unexpected token in '.ident' directive");
207 StringRef Data
= getTok().getIdentifier();
209 if (getLexer().isNot(AsmToken::EndOfStatement
))
210 return TokError("unexpected token in '.ident' directive");
212 getStreamer().EmitIdent(Data
);
216 // FIXME: Shared with ELF.
217 /// ParseDirectiveSymbolAttribute
218 /// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
219 bool ParseDirectiveSymbolAttribute(StringRef Directive
, SMLoc
) {
220 MCSymbolAttr Attr
= StringSwitch
<MCSymbolAttr
>(Directive
)
221 .Case(".weak", MCSA_Weak
)
222 .Case(".local", MCSA_Local
)
223 .Case(".hidden", MCSA_Hidden
)
224 .Case(".internal", MCSA_Internal
)
225 .Case(".protected", MCSA_Protected
)
226 .Default(MCSA_Invalid
);
227 assert(Attr
!= MCSA_Invalid
&& "unexpected symbol attribute directive!");
228 if (getLexer().isNot(AsmToken::EndOfStatement
)) {
231 if (getParser().parseIdentifier(Name
))
232 return TokError("expected identifier in directive");
233 MCSymbol
*Sym
= getContext().getOrCreateSymbol(Name
);
234 getStreamer().EmitSymbolAttribute(Sym
, Attr
);
235 if (getLexer().is(AsmToken::EndOfStatement
))
237 if (getLexer().isNot(AsmToken::Comma
))
238 return TokError("unexpected token in directive");
247 } // end anonymous namespace
251 MCAsmParserExtension
*createWasmAsmParser() {
252 return new WasmAsmParser
;
255 } // end namespace llvm