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");
61 bool error(const StringRef
&Msg
, const AsmToken
&Tok
) {
62 return Parser
->Error(Tok
.getLoc(), Msg
+ Tok
.getString());
65 bool isNext(AsmToken::TokenKind Kind
) {
66 auto Ok
= Lexer
->is(Kind
);
72 bool expect(AsmToken::TokenKind Kind
, const char *KindName
) {
74 return error(std::string("Expected ") + KindName
+ ", instead got: ",
79 bool parseSectionDirectiveText(StringRef
, SMLoc
) {
80 // FIXME: .text currently no-op.
84 bool parseSectionFlags(StringRef FlagStr
, bool &Passive
) {
85 SmallVector
<StringRef
, 2> Flags
;
86 // If there are no flags, keep Flags empty
87 FlagStr
.split(Flags
, ",", -1, false);
88 for (auto &Flag
: Flags
) {
89 if (Flag
== "passive")
92 return error("Expected section flags, instead got: ", Lexer
->getTok());
97 bool parseSectionDirective(StringRef
, SMLoc
) {
99 if (Parser
->parseIdentifier(Name
))
100 return TokError("expected identifier in directive");
102 if (expect(AsmToken::Comma
, ","))
105 if (Lexer
->isNot(AsmToken::String
))
106 return error("expected string in directive, instead got: ", Lexer
->getTok());
108 SectionKind Kind
= StringSwitch
<SectionKind
>(Name
)
109 .StartsWith(".data", SectionKind::getData())
110 .StartsWith(".rodata", SectionKind::getReadOnly())
111 .StartsWith(".text", SectionKind::getText())
112 .StartsWith(".custom_section", SectionKind::getMetadata());
114 MCSectionWasm
* Section
= getContext().getWasmSection(Name
, Kind
);
116 // Update section flags if present in this .section directive
117 bool Passive
= false;
118 if (parseSectionFlags(getTok().getStringContents(), Passive
))
122 if (!Section
->isWasmData())
123 return Parser
->Error(getTok().getLoc(),
124 "Only data sections can be passive");
125 Section
->setPassive();
130 if (expect(AsmToken::Comma
, ",") || expect(AsmToken::At
, "@") ||
131 expect(AsmToken::EndOfStatement
, "eol"))
137 static SectionType SectionTypes
[] = {
138 {".text", SectionKind::getText()},
139 {".rodata", SectionKind::getReadOnly()},
140 {".data", SectionKind::getData()},
141 {".custom_section", SectionKind::getMetadata()},
142 // TODO: add more types.
144 for (size_t I
= 0; I
< sizeof(SectionTypes
) / sizeof(SectionType
); I
++) {
145 if (Name
.startswith(SectionTypes
[I
].Name
)) {
146 auto WS
= getContext().getWasmSection(Name
, SectionTypes
[I
].Kind
);
147 getStreamer().SwitchSection(WS
);
151 // Not found, just ignore this section.
152 // For code in a text section WebAssemblyAsmParser automatically adds
153 // one section per function, so they're optional to be specified with
158 // TODO: This function is almost the same as ELFAsmParser::ParseDirectiveSize
159 // so maybe could be shared somehow.
160 bool parseDirectiveSize(StringRef
, SMLoc
) {
162 if (Parser
->parseIdentifier(Name
))
163 return TokError("expected identifier in directive");
164 auto Sym
= getContext().getOrCreateSymbol(Name
);
165 if (expect(AsmToken::Comma
, ","))
168 if (Parser
->parseExpression(Expr
))
170 if (expect(AsmToken::EndOfStatement
, "eol"))
172 // This is done automatically by the assembler for functions currently,
173 // so this is only currently needed for data sections:
174 getStreamer().emitELFSize(Sym
, Expr
);
178 bool parseDirectiveType(StringRef
, SMLoc
) {
179 // This could be the start of a function, check if followed by
181 if (!Lexer
->is(AsmToken::Identifier
))
182 return error("Expected label after .type directive, got: ",
184 auto WasmSym
= cast
<MCSymbolWasm
>(
185 getStreamer().getContext().getOrCreateSymbol(
186 Lexer
->getTok().getString()));
188 if (!(isNext(AsmToken::Comma
) && isNext(AsmToken::At
) &&
189 Lexer
->is(AsmToken::Identifier
)))
190 return error("Expected label,@type declaration, got: ", Lexer
->getTok());
191 auto TypeName
= Lexer
->getTok().getString();
192 if (TypeName
== "function")
193 WasmSym
->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION
);
194 else if (TypeName
== "global")
195 WasmSym
->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL
);
197 return error("Unknown WASM symbol type: ", Lexer
->getTok());
199 return expect(AsmToken::EndOfStatement
, "EOL");
203 } // end anonymous namespace
207 MCAsmParserExtension
*createWasmAsmParser() {
208 return new WasmAsmParser
;
211 } // end namespace llvm