[AArch64] Default to SEH exception handling on MinGW
[llvm-complete.git] / lib / MC / MCParser / WasmAsmParser.cpp
blob1054b8710529e78c1a47608cf0fdfb11baeb5b6a
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.
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"
30 using namespace llvm;
32 namespace {
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);
46 public:
47 WasmAsmParser() { BracketExpressionsSupported = true; }
49 void Initialize(MCAsmParser &P) override {
50 Parser = &P;
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);
67 if (Ok)
68 Lex();
69 return Ok;
72 bool expect(AsmToken::TokenKind Kind, const char *KindName) {
73 if (!isNext(Kind))
74 return error(std::string("Expected ") + KindName + ", instead got: ",
75 Lexer->getTok());
76 return false;
79 bool parseSectionDirectiveText(StringRef, SMLoc) {
80 // FIXME: .text currently no-op.
81 return false;
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")
90 Passive = true;
91 else
92 return error("Expected section flags, instead got: ", Lexer->getTok());
94 return false;
97 bool parseSectionDirective(StringRef, SMLoc) {
98 StringRef Name;
99 if (Parser->parseIdentifier(Name))
100 return TokError("expected identifier in directive");
102 if (expect(AsmToken::Comma, ","))
103 return true;
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))
119 return true;
121 if (Passive) {
122 if (!Section->isWasmData())
123 return Parser->Error(getTok().getLoc(),
124 "Only data sections can be passive");
125 Section->setPassive();
128 Lex();
130 if (expect(AsmToken::Comma, ",") || expect(AsmToken::At, "@") ||
131 expect(AsmToken::EndOfStatement, "eol"))
132 return true;
133 struct SectionType {
134 const char *Name;
135 SectionKind Kind;
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);
148 return false;
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
154 // this directive.
155 return false;
158 // TODO: This function is almost the same as ELFAsmParser::ParseDirectiveSize
159 // so maybe could be shared somehow.
160 bool parseDirectiveSize(StringRef, SMLoc) {
161 StringRef Name;
162 if (Parser->parseIdentifier(Name))
163 return TokError("expected identifier in directive");
164 auto Sym = getContext().getOrCreateSymbol(Name);
165 if (expect(AsmToken::Comma, ","))
166 return true;
167 const MCExpr *Expr;
168 if (Parser->parseExpression(Expr))
169 return true;
170 if (expect(AsmToken::EndOfStatement, "eol"))
171 return true;
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);
175 return false;
178 bool parseDirectiveType(StringRef, SMLoc) {
179 // This could be the start of a function, check if followed by
180 // "label,@function"
181 if (!Lexer->is(AsmToken::Identifier))
182 return error("Expected label after .type directive, got: ",
183 Lexer->getTok());
184 auto WasmSym = cast<MCSymbolWasm>(
185 getStreamer().getContext().getOrCreateSymbol(
186 Lexer->getTok().getString()));
187 Lex();
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);
196 else
197 return error("Unknown WASM symbol type: ", Lexer->getTok());
198 Lex();
199 return expect(AsmToken::EndOfStatement, "EOL");
203 } // end anonymous namespace
205 namespace llvm {
207 MCAsmParserExtension *createWasmAsmParser() {
208 return new WasmAsmParser;
211 } // end namespace llvm