1 //==- WebAssemblyAsmParser.cpp - Assembler for WebAssembly -*- C++ -*-==//
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 /// \file
10 /// This file is part of the WebAssembly Assembler.
11 ///
12 /// It contains code to translate a parsed .s file into MCInsts.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
17 #include "MCTargetDesc/WebAssemblyTargetStreamer.h"
18 #include "TargetInfo/WebAssemblyTargetInfo.h"
19 #include "WebAssembly.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCInst.h"
23 #include "llvm/MC/MCInstrInfo.h"
24 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
25 #include "llvm/MC/MCParser/MCTargetAsmParser.h"
26 #include "llvm/MC/MCSectionWasm.h"
27 #include "llvm/MC/MCStreamer.h"
28 #include "llvm/MC/MCSubtargetInfo.h"
29 #include "llvm/MC/MCSymbol.h"
30 #include "llvm/MC/MCSymbolWasm.h"
31 #include "llvm/Support/Endian.h"
32 #include "llvm/Support/TargetRegistry.h"
33 
34 using namespace llvm;
35 
36 #define DEBUG_TYPE "wasm-asm-parser"
37 
38 static const char *getSubtargetFeatureName(uint64_t Val);
39 
40 namespace {
41 
42 /// WebAssemblyOperand - Instances of this class represent the operands in a
43 /// parsed Wasm machine instruction.
44 struct WebAssemblyOperand : public MCParsedAsmOperand {
45   enum KindTy { Token, Integer, Float, Symbol, BrList } Kind;
46 
47   SMLoc StartLoc, EndLoc;
48 
49   struct TokOp {
50     StringRef Tok;
51   };
52 
53   struct IntOp {
54     int64_t Val;
55   };
56 
57   struct FltOp {
58     double Val;
59   };
60 
61   struct SymOp {
62     const MCExpr *Exp;
63   };
64 
65   struct BrLOp {
66     std::vector<unsigned> List;
67   };
68 
69   union {
70     struct TokOp Tok;
71     struct IntOp Int;
72     struct FltOp Flt;
73     struct SymOp Sym;
74     struct BrLOp BrL;
75   };
76 
77   WebAssemblyOperand(KindTy K, SMLoc Start, SMLoc End, TokOp T)
78       : Kind(K), StartLoc(Start), EndLoc(End), Tok(T) {}
79   WebAssemblyOperand(KindTy K, SMLoc Start, SMLoc End, IntOp I)
80       : Kind(K), StartLoc(Start), EndLoc(End), Int(I) {}
81   WebAssemblyOperand(KindTy K, SMLoc Start, SMLoc End, FltOp F)
82       : Kind(K), StartLoc(Start), EndLoc(End), Flt(F) {}
83   WebAssemblyOperand(KindTy K, SMLoc Start, SMLoc End, SymOp S)
84       : Kind(K), StartLoc(Start), EndLoc(End), Sym(S) {}
85   WebAssemblyOperand(KindTy K, SMLoc Start, SMLoc End)
86       : Kind(K), StartLoc(Start), EndLoc(End), BrL() {}
87 
88   ~WebAssemblyOperand() {
89     if (isBrList())
90       BrL.~BrLOp();
91   }
92 
93   bool isToken() const override { return Kind == Token; }
94   bool isImm() const override { return Kind == Integer || Kind == Symbol; }
95   bool isFPImm() const { return Kind == Float; }
96   bool isMem() const override { return false; }
97   bool isReg() const override { return false; }
98   bool isBrList() const { return Kind == BrList; }
99 
100   unsigned getReg() const override {
101     llvm_unreachable("Assembly inspects a register operand");
102     return 0;
103   }
104 
105   StringRef getToken() const {
106     assert(isToken());
107     return Tok.Tok;
108   }
109 
110   SMLoc getStartLoc() const override { return StartLoc; }
111   SMLoc getEndLoc() const override { return EndLoc; }
112 
113   void addRegOperands(MCInst &, unsigned) const {
114     // Required by the assembly matcher.
115     llvm_unreachable("Assembly matcher creates register operands");
116   }
117 
118   void addImmOperands(MCInst &Inst, unsigned N) const {
119     assert(N == 1 && "Invalid number of operands!");
120     if (Kind == Integer)
121       Inst.addOperand(MCOperand::createImm(Int.Val));
122     else if (Kind == Symbol)
123       Inst.addOperand(MCOperand::createExpr(Sym.Exp));
124     else
125       llvm_unreachable("Should be integer immediate or symbol!");
126   }
127 
128   void addFPImmOperands(MCInst &Inst, unsigned N) const {
129     assert(N == 1 && "Invalid number of operands!");
130     if (Kind == Float)
131       Inst.addOperand(MCOperand::createFPImm(Flt.Val));
132     else
133       llvm_unreachable("Should be float immediate!");
134   }
135 
136   void addBrListOperands(MCInst &Inst, unsigned N) const {
137     assert(N == 1 && isBrList() && "Invalid BrList!");
138     for (auto Br : BrL.List)
139       Inst.addOperand(MCOperand::createImm(Br));
140   }
141 
142   void print(raw_ostream &OS) const override {
143     switch (Kind) {
144     case Token:
145       OS << "Tok:" << Tok.Tok;
146       break;
147     case Integer:
148       OS << "Int:" << Int.Val;
149       break;
150     case Float:
151       OS << "Flt:" << Flt.Val;
152       break;
153     case Symbol:
154       OS << "Sym:" << Sym.Exp;
155       break;
156     case BrList:
157       OS << "BrList:" << BrL.List.size();
158       break;
159     }
160   }
161 };
162 
163 class WebAssemblyAsmParser final : public MCTargetAsmParser {
164   MCAsmParser &Parser;
165   MCAsmLexer &Lexer;
166 
167   // Much like WebAssemblyAsmPrinter in the backend, we have to own these.
168   std::vector<std::unique_ptr<wasm::WasmSignature>> Signatures;
169   std::vector<std::unique_ptr<std::string>> Names;
170 
171   // Order of labels, directives and instructions in a .s file have no
172   // syntactical enforcement. This class is a callback from the actual parser,
173   // and yet we have to be feeding data to the streamer in a very particular
174   // order to ensure a correct binary encoding that matches the regular backend
175   // (the streamer does not enforce this). This "state machine" enum helps
176   // guarantee that correct order.
177   enum ParserState {
178     FileStart,
179     Label,
180     FunctionStart,
181     FunctionLocals,
182     Instructions,
183     EndFunction,
184     DataSection,
185   } CurrentState = FileStart;
186 
187   // For ensuring blocks are properly nested.
188   enum NestingType {
189     Function,
190     Block,
191     Loop,
192     Try,
193     If,
194     Else,
195     Undefined,
196   };
197   std::vector<NestingType> NestingStack;
198 
199   // We track this to see if a .functype following a label is the same,
200   // as this is how we recognize the start of a function.
201   MCSymbol *LastLabel = nullptr;
202   MCSymbol *LastFunctionLabel = nullptr;
203 
204 public:
205   WebAssemblyAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser,
206                        const MCInstrInfo &MII, const MCTargetOptions &Options)
207       : MCTargetAsmParser(Options, STI, MII), Parser(Parser),
208         Lexer(Parser.getLexer()) {
209     setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
210   }
211 
212 #define GET_ASSEMBLER_HEADER
213 #include "WebAssemblyGenAsmMatcher.inc"
214 
215   // TODO: This is required to be implemented, but appears unused.
216   bool ParseRegister(unsigned & /*RegNo*/, SMLoc & /*StartLoc*/,
217                      SMLoc & /*EndLoc*/) override {
218     llvm_unreachable("ParseRegister is not implemented.");
219   }
220   OperandMatchResultTy tryParseRegister(unsigned & /*RegNo*/,
221                                         SMLoc & /*StartLoc*/,
222                                         SMLoc & /*EndLoc*/) override {
223     llvm_unreachable("tryParseRegister is not implemented.");
224   }
225 
226   bool error(const Twine &Msg, const AsmToken &Tok) {
227     return Parser.Error(Tok.getLoc(), Msg + Tok.getString());
228   }
229 
230   bool error(const Twine &Msg) {
231     return Parser.Error(Lexer.getTok().getLoc(), Msg);
232   }
233 
234   void addSignature(std::unique_ptr<wasm::WasmSignature> &&Sig) {
235     Signatures.push_back(std::move(Sig));
236   }
237 
238   StringRef storeName(StringRef Name) {
239     std::unique_ptr<std::string> N = std::make_unique<std::string>(Name);
240     Names.push_back(std::move(N));
241     return *Names.back();
242   }
243 
244   std::pair<StringRef, StringRef> nestingString(NestingType NT) {
245     switch (NT) {
246     case Function:
247       return {"function", "end_function"};
248     case Block:
249       return {"block", "end_block"};
250     case Loop:
251       return {"loop", "end_loop"};
252     case Try:
253       return {"try", "end_try"};
254     case If:
255       return {"if", "end_if"};
256     case Else:
257       return {"else", "end_if"};
258     default:
259       llvm_unreachable("unknown NestingType");
260     }
261   }
262 
263   void push(NestingType NT) { NestingStack.push_back(NT); }
264 
265   bool pop(StringRef Ins, NestingType NT1, NestingType NT2 = Undefined) {
266     if (NestingStack.empty())
267       return error(Twine("End of block construct with no start: ") + Ins);
268     auto Top = NestingStack.back();
269     if (Top != NT1 && Top != NT2)
270       return error(Twine("Block construct type mismatch, expected: ") +
271                    nestingString(Top).second + ", instead got: " + Ins);
272     NestingStack.pop_back();
273     return false;
274   }
275 
276   bool ensureEmptyNestingStack() {
277     auto Err = !NestingStack.empty();
278     while (!NestingStack.empty()) {
279       error(Twine("Unmatched block construct(s) at function end: ") +
280             nestingString(NestingStack.back()).first);
281       NestingStack.pop_back();
282     }
283     return Err;
284   }
285 
286   bool isNext(AsmToken::TokenKind Kind) {
287     auto Ok = Lexer.is(Kind);
288     if (Ok)
289       Parser.Lex();
290     return Ok;
291   }
292 
293   bool expect(AsmToken::TokenKind Kind, const char *KindName) {
294     if (!isNext(Kind))
295       return error(std::string("Expected ") + KindName + ", instead got: ",
296                    Lexer.getTok());
297     return false;
298   }
299 
300   StringRef expectIdent() {
301     if (!Lexer.is(AsmToken::Identifier)) {
302       error("Expected identifier, got: ", Lexer.getTok());
303       return StringRef();
304     }
305     auto Name = Lexer.getTok().getString();
306     Parser.Lex();
307     return Name;
308   }
309 
310   Optional<wasm::ValType> parseType(const StringRef &Type) {
311     // FIXME: can't use StringSwitch because wasm::ValType doesn't have a
312     // "invalid" value.
313     if (Type == "i32")
314       return wasm::ValType::I32;
315     if (Type == "i64")
316       return wasm::ValType::I64;
317     if (Type == "f32")
318       return wasm::ValType::F32;
319     if (Type == "f64")
320       return wasm::ValType::F64;
321     if (Type == "v128" || Type == "i8x16" || Type == "i16x8" ||
322         Type == "i32x4" || Type == "i64x2" || Type == "f32x4" ||
323         Type == "f64x2")
324       return wasm::ValType::V128;
325     if (Type == "exnref")
326       return wasm::ValType::EXNREF;
327     if (Type == "funcref")
328       return wasm::ValType::FUNCREF;
329     if (Type == "externref")
330       return wasm::ValType::EXTERNREF;
331     return Optional<wasm::ValType>();
332   }
333 
334   WebAssembly::BlockType parseBlockType(StringRef ID) {
335     // Multivalue block types are handled separately in parseSignature
336     return StringSwitch<WebAssembly::BlockType>(ID)
337         .Case("i32", WebAssembly::BlockType::I32)
338         .Case("i64", WebAssembly::BlockType::I64)
339         .Case("f32", WebAssembly::BlockType::F32)
340         .Case("f64", WebAssembly::BlockType::F64)
341         .Case("v128", WebAssembly::BlockType::V128)
342         .Case("exnref", WebAssembly::BlockType::Exnref)
343         .Case("void", WebAssembly::BlockType::Void)
344         .Default(WebAssembly::BlockType::Invalid);
345   }
346 
347   bool parseRegTypeList(SmallVectorImpl<wasm::ValType> &Types) {
348     while (Lexer.is(AsmToken::Identifier)) {
349       auto Type = parseType(Lexer.getTok().getString());
350       if (!Type)
351         return error("unknown type: ", Lexer.getTok());
352       Types.push_back(Type.getValue());
353       Parser.Lex();
354       if (!isNext(AsmToken::Comma))
355         break;
356     }
357     return false;
358   }
359 
360   void parseSingleInteger(bool IsNegative, OperandVector &Operands) {
361     auto &Int = Lexer.getTok();
362     int64_t Val = Int.getIntVal();
363     if (IsNegative)
364       Val = -Val;
365     Operands.push_back(std::make_unique<WebAssemblyOperand>(
366         WebAssemblyOperand::Integer, Int.getLoc(), Int.getEndLoc(),
367         WebAssemblyOperand::IntOp{Val}));
368     Parser.Lex();
369   }
370 
371   bool parseSingleFloat(bool IsNegative, OperandVector &Operands) {
372     auto &Flt = Lexer.getTok();
373     double Val;
374     if (Flt.getString().getAsDouble(Val, false))
375       return error("Cannot parse real: ", Flt);
376     if (IsNegative)
377       Val = -Val;
378     Operands.push_back(std::make_unique<WebAssemblyOperand>(
379         WebAssemblyOperand::Float, Flt.getLoc(), Flt.getEndLoc(),
380         WebAssemblyOperand::FltOp{Val}));
381     Parser.Lex();
382     return false;
383   }
384 
385   bool parseSpecialFloatMaybe(bool IsNegative, OperandVector &Operands) {
386     if (Lexer.isNot(AsmToken::Identifier))
387       return true;
388     auto &Flt = Lexer.getTok();
389     auto S = Flt.getString();
390     double Val;
391     if (S.compare_lower("infinity") == 0) {
392       Val = std::numeric_limits<double>::infinity();
393     } else if (S.compare_lower("nan") == 0) {
394       Val = std::numeric_limits<double>::quiet_NaN();
395     } else {
396       return true;
397     }
398     if (IsNegative)
399       Val = -Val;
400     Operands.push_back(std::make_unique<WebAssemblyOperand>(
401         WebAssemblyOperand::Float, Flt.getLoc(), Flt.getEndLoc(),
402         WebAssemblyOperand::FltOp{Val}));
403     Parser.Lex();
404     return false;
405   }
406 
407   bool checkForP2AlignIfLoadStore(OperandVector &Operands, StringRef InstName) {
408     // FIXME: there is probably a cleaner way to do this.
409     auto IsLoadStore = InstName.find(".load") != StringRef::npos ||
410                        InstName.find(".store") != StringRef::npos;
411     auto IsAtomic = InstName.find("atomic.") != StringRef::npos;
412     if (IsLoadStore || IsAtomic) {
413       // Parse load/store operands of the form: offset:p2align=align
414       if (IsLoadStore && isNext(AsmToken::Colon)) {
415         auto Id = expectIdent();
416         if (Id != "p2align")
417           return error("Expected p2align, instead got: " + Id);
418         if (expect(AsmToken::Equal, "="))
419           return true;
420         if (!Lexer.is(AsmToken::Integer))
421           return error("Expected integer constant");
422         parseSingleInteger(false, Operands);
423       } else {
424         // v128.{load,store}{8,16,32,64}_lane has both a memarg and a lane
425         // index. We need to avoid parsing an extra alignment operand for the
426         // lane index.
427         auto IsLoadStoreLane = InstName.find("_lane") != StringRef::npos;
428         if (IsLoadStoreLane && Operands.size() == 4)
429           return false;
430         // Alignment not specified (or atomics, must use default alignment).
431         // We can't just call WebAssembly::GetDefaultP2Align since we don't have
432         // an opcode until after the assembly matcher, so set a default to fix
433         // up later.
434         auto Tok = Lexer.getTok();
435         Operands.push_back(std::make_unique<WebAssemblyOperand>(
436             WebAssemblyOperand::Integer, Tok.getLoc(), Tok.getEndLoc(),
437             WebAssemblyOperand::IntOp{-1}));
438       }
439     }
440     return false;
441   }
442 
443   WebAssembly::HeapType parseHeapType(StringRef Id) {
444     return StringSwitch<WebAssembly::HeapType>(Id)
445         .Case("extern", WebAssembly::HeapType::Externref)
446         .Case("func", WebAssembly::HeapType::Funcref)
447         .Default(WebAssembly::HeapType::Invalid);
448   }
449 
450   void addBlockTypeOperand(OperandVector &Operands, SMLoc NameLoc,
451                            WebAssembly::BlockType BT) {
452     Operands.push_back(std::make_unique<WebAssemblyOperand>(
453         WebAssemblyOperand::Integer, NameLoc, NameLoc,
454         WebAssemblyOperand::IntOp{static_cast<int64_t>(BT)}));
455   }
456 
457   bool ParseInstruction(ParseInstructionInfo & /*Info*/, StringRef Name,
458                         SMLoc NameLoc, OperandVector &Operands) override {
459     // Note: Name does NOT point into the sourcecode, but to a local, so
460     // use NameLoc instead.
461     Name = StringRef(NameLoc.getPointer(), Name.size());
462 
463     // WebAssembly has instructions with / in them, which AsmLexer parses
464     // as separate tokens, so if we find such tokens immediately adjacent (no
465     // whitespace), expand the name to include them:
466     for (;;) {
467       auto &Sep = Lexer.getTok();
468       if (Sep.getLoc().getPointer() != Name.end() ||
469           Sep.getKind() != AsmToken::Slash)
470         break;
471       // Extend name with /
472       Name = StringRef(Name.begin(), Name.size() + Sep.getString().size());
473       Parser.Lex();
474       // We must now find another identifier, or error.
475       auto &Id = Lexer.getTok();
476       if (Id.getKind() != AsmToken::Identifier ||
477           Id.getLoc().getPointer() != Name.end())
478         return error("Incomplete instruction name: ", Id);
479       Name = StringRef(Name.begin(), Name.size() + Id.getString().size());
480       Parser.Lex();
481     }
482 
483     // Now construct the name as first operand.
484     Operands.push_back(std::make_unique<WebAssemblyOperand>(
485         WebAssemblyOperand::Token, NameLoc, SMLoc::getFromPointer(Name.end()),
486         WebAssemblyOperand::TokOp{Name}));
487 
488     // If this instruction is part of a control flow structure, ensure
489     // proper nesting.
490     bool ExpectBlockType = false;
491     bool ExpectFuncType = false;
492     bool ExpectHeapType = false;
493     if (Name == "block") {
494       push(Block);
495       ExpectBlockType = true;
496     } else if (Name == "loop") {
497       push(Loop);
498       ExpectBlockType = true;
499     } else if (Name == "try") {
500       push(Try);
501       ExpectBlockType = true;
502     } else if (Name == "if") {
503       push(If);
504       ExpectBlockType = true;
505     } else if (Name == "else") {
506       if (pop(Name, If))
507         return true;
508       push(Else);
509     } else if (Name == "catch") {
510       if (pop(Name, Try))
511         return true;
512       push(Try);
513     } else if (Name == "end_if") {
514       if (pop(Name, If, Else))
515         return true;
516     } else if (Name == "end_try") {
517       if (pop(Name, Try))
518         return true;
519     } else if (Name == "end_loop") {
520       if (pop(Name, Loop))
521         return true;
522     } else if (Name == "end_block") {
523       if (pop(Name, Block))
524         return true;
525     } else if (Name == "end_function") {
526       ensureLocals(getStreamer());
527       CurrentState = EndFunction;
528       if (pop(Name, Function) || ensureEmptyNestingStack())
529         return true;
530     } else if (Name == "call_indirect" || Name == "return_call_indirect") {
531       ExpectFuncType = true;
532     } else if (Name == "ref.null") {
533       ExpectHeapType = true;
534     }
535 
536     if (ExpectFuncType || (ExpectBlockType && Lexer.is(AsmToken::LParen))) {
537       // This has a special TYPEINDEX operand which in text we
538       // represent as a signature, such that we can re-build this signature,
539       // attach it to an anonymous symbol, which is what WasmObjectWriter
540       // expects to be able to recreate the actual unique-ified type indices.
541       auto Loc = Parser.getTok();
542       auto Signature = std::make_unique<wasm::WasmSignature>();
543       if (parseSignature(Signature.get()))
544         return true;
545       // Got signature as block type, don't need more
546       ExpectBlockType = false;
547       auto &Ctx = getStreamer().getContext();
548       // The "true" here will cause this to be a nameless symbol.
549       MCSymbol *Sym = Ctx.createTempSymbol("typeindex", true);
550       auto *WasmSym = cast<MCSymbolWasm>(Sym);
551       WasmSym->setSignature(Signature.get());
552       addSignature(std::move(Signature));
553       WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
554       const MCExpr *Expr = MCSymbolRefExpr::create(
555           WasmSym, MCSymbolRefExpr::VK_WASM_TYPEINDEX, Ctx);
556       Operands.push_back(std::make_unique<WebAssemblyOperand>(
557           WebAssemblyOperand::Symbol, Loc.getLoc(), Loc.getEndLoc(),
558           WebAssemblyOperand::SymOp{Expr}));
559     }
560 
561     while (Lexer.isNot(AsmToken::EndOfStatement)) {
562       auto &Tok = Lexer.getTok();
563       switch (Tok.getKind()) {
564       case AsmToken::Identifier: {
565         if (!parseSpecialFloatMaybe(false, Operands))
566           break;
567         auto &Id = Lexer.getTok();
568         if (ExpectBlockType) {
569           // Assume this identifier is a block_type.
570           auto BT = parseBlockType(Id.getString());
571           if (BT == WebAssembly::BlockType::Invalid)
572             return error("Unknown block type: ", Id);
573           addBlockTypeOperand(Operands, NameLoc, BT);
574           Parser.Lex();
575         } else if (ExpectHeapType) {
576           auto HeapType = parseHeapType(Id.getString());
577           if (HeapType == WebAssembly::HeapType::Invalid) {
578             return error("Expected a heap type: ", Id);
579           }
580           Operands.push_back(std::make_unique<WebAssemblyOperand>(
581               WebAssemblyOperand::Integer, Id.getLoc(), Id.getEndLoc(),
582               WebAssemblyOperand::IntOp{static_cast<int64_t>(HeapType)}));
583           Parser.Lex();
584         } else {
585           // Assume this identifier is a label.
586           const MCExpr *Val;
587           SMLoc End;
588           if (Parser.parseExpression(Val, End))
589             return error("Cannot parse symbol: ", Lexer.getTok());
590           Operands.push_back(std::make_unique<WebAssemblyOperand>(
591               WebAssemblyOperand::Symbol, Id.getLoc(), Id.getEndLoc(),
592               WebAssemblyOperand::SymOp{Val}));
593           if (checkForP2AlignIfLoadStore(Operands, Name))
594             return true;
595         }
596         break;
597       }
598       case AsmToken::Minus:
599         Parser.Lex();
600         if (Lexer.is(AsmToken::Integer)) {
601           parseSingleInteger(true, Operands);
602           if (checkForP2AlignIfLoadStore(Operands, Name))
603             return true;
604         } else if(Lexer.is(AsmToken::Real)) {
605           if (parseSingleFloat(true, Operands))
606             return true;
607         } else if (!parseSpecialFloatMaybe(true, Operands)) {
608         } else {
609           return error("Expected numeric constant instead got: ",
610                        Lexer.getTok());
611         }
612         break;
613       case AsmToken::Integer:
614         parseSingleInteger(false, Operands);
615         if (checkForP2AlignIfLoadStore(Operands, Name))
616           return true;
617         break;
618       case AsmToken::Real: {
619         if (parseSingleFloat(false, Operands))
620           return true;
621         break;
622       }
623       case AsmToken::LCurly: {
624         Parser.Lex();
625         auto Op = std::make_unique<WebAssemblyOperand>(
626             WebAssemblyOperand::BrList, Tok.getLoc(), Tok.getEndLoc());
627         if (!Lexer.is(AsmToken::RCurly))
628           for (;;) {
629             Op->BrL.List.push_back(Lexer.getTok().getIntVal());
630             expect(AsmToken::Integer, "integer");
631             if (!isNext(AsmToken::Comma))
632               break;
633           }
634         expect(AsmToken::RCurly, "}");
635         Operands.push_back(std::move(Op));
636         break;
637       }
638       default:
639         return error("Unexpected token in operand: ", Tok);
640       }
641       if (Lexer.isNot(AsmToken::EndOfStatement)) {
642         if (expect(AsmToken::Comma, ","))
643           return true;
644       }
645     }
646     if (ExpectBlockType && Operands.size() == 1) {
647       // Support blocks with no operands as default to void.
648       addBlockTypeOperand(Operands, NameLoc, WebAssembly::BlockType::Void);
649     }
650     Parser.Lex();
651     return false;
652   }
653 
654   void onLabelParsed(MCSymbol *Symbol) override {
655     LastLabel = Symbol;
656     CurrentState = Label;
657   }
658 
659   bool parseSignature(wasm::WasmSignature *Signature) {
660     if (expect(AsmToken::LParen, "("))
661       return true;
662     if (parseRegTypeList(Signature->Params))
663       return true;
664     if (expect(AsmToken::RParen, ")"))
665       return true;
666     if (expect(AsmToken::MinusGreater, "->"))
667       return true;
668     if (expect(AsmToken::LParen, "("))
669       return true;
670     if (parseRegTypeList(Signature->Returns))
671       return true;
672     if (expect(AsmToken::RParen, ")"))
673       return true;
674     return false;
675   }
676 
677   bool CheckDataSection() {
678     if (CurrentState != DataSection) {
679       auto WS = cast<MCSectionWasm>(getStreamer().getCurrentSection().first);
680       if (WS && WS->getKind().isText())
681         return error("data directive must occur in a data segment: ",
682                      Lexer.getTok());
683     }
684     CurrentState = DataSection;
685     return false;
686   }
687 
688   // This function processes wasm-specific directives streamed to
689   // WebAssemblyTargetStreamer, all others go to the generic parser
690   // (see WasmAsmParser).
691   bool ParseDirective(AsmToken DirectiveID) override {
692     // This function has a really weird return value behavior that is different
693     // from all the other parsing functions:
694     // - return true && no tokens consumed -> don't know this directive / let
695     //   the generic parser handle it.
696     // - return true && tokens consumed -> a parsing error occurred.
697     // - return false -> processed this directive successfully.
698     assert(DirectiveID.getKind() == AsmToken::Identifier);
699     auto &Out = getStreamer();
700     auto &TOut =
701         reinterpret_cast<WebAssemblyTargetStreamer &>(*Out.getTargetStreamer());
702     auto &Ctx = Out.getContext();
703 
704     // TODO: any time we return an error, at least one token must have been
705     // consumed, otherwise this will not signal an error to the caller.
706     if (DirectiveID.getString() == ".globaltype") {
707       auto SymName = expectIdent();
708       if (SymName.empty())
709         return true;
710       if (expect(AsmToken::Comma, ","))
711         return true;
712       auto TypeTok = Lexer.getTok();
713       auto TypeName = expectIdent();
714       if (TypeName.empty())
715         return true;
716       auto Type = parseType(TypeName);
717       if (!Type)
718         return error("Unknown type in .globaltype directive: ", TypeTok);
719       // Optional mutable modifier. Default to mutable for historical reasons.
720       // Ideally we would have gone with immutable as the default and used `mut`
721       // as the modifier to match the `.wat` format.
722       bool Mutable = true;
723       if (isNext(AsmToken::Comma)) {
724         TypeTok = Lexer.getTok();
725         auto Id = expectIdent();
726         if (Id == "immutable")
727           Mutable = false;
728         else
729           // Should we also allow `mutable` and `mut` here for clarity?
730           return error("Unknown type in .globaltype modifier: ", TypeTok);
731       }
732       // Now set this symbol with the correct type.
733       auto WasmSym = cast<MCSymbolWasm>(Ctx.getOrCreateSymbol(SymName));
734       WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
735       WasmSym->setGlobalType(
736           wasm::WasmGlobalType{uint8_t(Type.getValue()), Mutable});
737       // And emit the directive again.
738       TOut.emitGlobalType(WasmSym);
739       return expect(AsmToken::EndOfStatement, "EOL");
740     }
741 
742     if (DirectiveID.getString() == ".tabletype") {
743       auto SymName = expectIdent();
744       if (SymName.empty())
745         return true;
746       if (expect(AsmToken::Comma, ","))
747         return true;
748       auto TypeTok = Lexer.getTok();
749       auto TypeName = expectIdent();
750       if (TypeName.empty())
751         return true;
752       auto Type = parseType(TypeName);
753       if (!Type)
754         return error("Unknown type in .tabletype directive: ", TypeTok);
755 
756       // Now that we have the name and table type, we can actually create the
757       // symbol
758       auto WasmSym = cast<MCSymbolWasm>(Ctx.getOrCreateSymbol(SymName));
759       WasmSym->setType(wasm::WASM_SYMBOL_TYPE_TABLE);
760       WasmSym->setTableType(Type.getValue());
761       TOut.emitTableType(WasmSym);
762       return expect(AsmToken::EndOfStatement, "EOL");
763     }
764 
765     if (DirectiveID.getString() == ".functype") {
766       // This code has to send things to the streamer similar to
767       // WebAssemblyAsmPrinter::EmitFunctionBodyStart.
768       // TODO: would be good to factor this into a common function, but the
769       // assembler and backend really don't share any common code, and this code
770       // parses the locals separately.
771       auto SymName = expectIdent();
772       if (SymName.empty())
773         return true;
774       auto WasmSym = cast<MCSymbolWasm>(Ctx.getOrCreateSymbol(SymName));
775       if (CurrentState == Label && WasmSym == LastLabel) {
776         // This .functype indicates a start of a function.
777         if (ensureEmptyNestingStack())
778           return true;
779         CurrentState = FunctionStart;
780         LastFunctionLabel = LastLabel;
781         push(Function);
782       }
783       auto Signature = std::make_unique<wasm::WasmSignature>();
784       if (parseSignature(Signature.get()))
785         return true;
786       WasmSym->setSignature(Signature.get());
787       addSignature(std::move(Signature));
788       WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
789       TOut.emitFunctionType(WasmSym);
790       // TODO: backend also calls TOut.emitIndIdx, but that is not implemented.
791       return expect(AsmToken::EndOfStatement, "EOL");
792     }
793 
794     if (DirectiveID.getString() == ".export_name") {
795       auto SymName = expectIdent();
796       if (SymName.empty())
797         return true;
798       if (expect(AsmToken::Comma, ","))
799         return true;
800       auto ExportName = expectIdent();
801       auto WasmSym = cast<MCSymbolWasm>(Ctx.getOrCreateSymbol(SymName));
802       WasmSym->setExportName(storeName(ExportName));
803       TOut.emitExportName(WasmSym, ExportName);
804     }
805 
806     if (DirectiveID.getString() == ".import_module") {
807       auto SymName = expectIdent();
808       if (SymName.empty())
809         return true;
810       if (expect(AsmToken::Comma, ","))
811         return true;
812       auto ImportModule = expectIdent();
813       auto WasmSym = cast<MCSymbolWasm>(Ctx.getOrCreateSymbol(SymName));
814       WasmSym->setImportModule(storeName(ImportModule));
815       TOut.emitImportModule(WasmSym, ImportModule);
816     }
817 
818     if (DirectiveID.getString() == ".import_name") {
819       auto SymName = expectIdent();
820       if (SymName.empty())
821         return true;
822       if (expect(AsmToken::Comma, ","))
823         return true;
824       auto ImportName = expectIdent();
825       auto WasmSym = cast<MCSymbolWasm>(Ctx.getOrCreateSymbol(SymName));
826       WasmSym->setImportName(storeName(ImportName));
827       TOut.emitImportName(WasmSym, ImportName);
828     }
829 
830     if (DirectiveID.getString() == ".eventtype") {
831       auto SymName = expectIdent();
832       if (SymName.empty())
833         return true;
834       auto WasmSym = cast<MCSymbolWasm>(Ctx.getOrCreateSymbol(SymName));
835       auto Signature = std::make_unique<wasm::WasmSignature>();
836       if (parseRegTypeList(Signature->Params))
837         return true;
838       WasmSym->setSignature(Signature.get());
839       addSignature(std::move(Signature));
840       WasmSym->setType(wasm::WASM_SYMBOL_TYPE_EVENT);
841       TOut.emitEventType(WasmSym);
842       // TODO: backend also calls TOut.emitIndIdx, but that is not implemented.
843       return expect(AsmToken::EndOfStatement, "EOL");
844     }
845 
846     if (DirectiveID.getString() == ".local") {
847       if (CurrentState != FunctionStart)
848         return error(".local directive should follow the start of a function",
849                      Lexer.getTok());
850       SmallVector<wasm::ValType, 4> Locals;
851       if (parseRegTypeList(Locals))
852         return true;
853       TOut.emitLocal(Locals);
854       CurrentState = FunctionLocals;
855       return expect(AsmToken::EndOfStatement, "EOL");
856     }
857 
858     if (DirectiveID.getString() == ".int8" ||
859         DirectiveID.getString() == ".int16" ||
860         DirectiveID.getString() == ".int32" ||
861         DirectiveID.getString() == ".int64") {
862       if (CheckDataSection()) return true;
863       const MCExpr *Val;
864       SMLoc End;
865       if (Parser.parseExpression(Val, End))
866         return error("Cannot parse .int expression: ", Lexer.getTok());
867       size_t NumBits = 0;
868       DirectiveID.getString().drop_front(4).getAsInteger(10, NumBits);
869       Out.emitValue(Val, NumBits / 8, End);
870       return expect(AsmToken::EndOfStatement, "EOL");
871     }
872 
873     if (DirectiveID.getString() == ".asciz") {
874       if (CheckDataSection()) return true;
875       std::string S;
876       if (Parser.parseEscapedString(S))
877         return error("Cannot parse string constant: ", Lexer.getTok());
878       Out.emitBytes(StringRef(S.c_str(), S.length() + 1));
879       return expect(AsmToken::EndOfStatement, "EOL");
880     }
881 
882     return true; // We didn't process this directive.
883   }
884 
885   // Called either when the first instruction is parsed of the function ends.
886   void ensureLocals(MCStreamer &Out) {
887     if (CurrentState == FunctionStart) {
888       // We haven't seen a .local directive yet. The streamer requires locals to
889       // be encoded as a prelude to the instructions, so emit an empty list of
890       // locals here.
891       auto &TOut = reinterpret_cast<WebAssemblyTargetStreamer &>(
892           *Out.getTargetStreamer());
893       TOut.emitLocal(SmallVector<wasm::ValType, 0>());
894       CurrentState = FunctionLocals;
895     }
896   }
897 
898   bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned & /*Opcode*/,
899                                OperandVector &Operands, MCStreamer &Out,
900                                uint64_t &ErrorInfo,
901                                bool MatchingInlineAsm) override {
902     MCInst Inst;
903     Inst.setLoc(IDLoc);
904     FeatureBitset MissingFeatures;
905     unsigned MatchResult = MatchInstructionImpl(
906         Operands, Inst, ErrorInfo, MissingFeatures, MatchingInlineAsm);
907     switch (MatchResult) {
908     case Match_Success: {
909       ensureLocals(Out);
910       // Fix unknown p2align operands.
911       auto Align = WebAssembly::GetDefaultP2AlignAny(Inst.getOpcode());
912       if (Align != -1U) {
913         auto &Op0 = Inst.getOperand(0);
914         if (Op0.getImm() == -1)
915           Op0.setImm(Align);
916       }
917       if (getSTI().getTargetTriple().isArch64Bit()) {
918         // Upgrade 32-bit loads/stores to 64-bit. These mostly differ by having
919         // an offset64 arg instead of offset32, but to the assembler matcher
920         // they're both immediates so don't get selected for.
921         auto Opc64 = WebAssembly::getWasm64Opcode(
922             static_cast<uint16_t>(Inst.getOpcode()));
923         if (Opc64 >= 0) {
924           Inst.setOpcode(Opc64);
925         }
926       }
927       Out.emitInstruction(Inst, getSTI());
928       if (CurrentState == EndFunction) {
929         onEndOfFunction();
930       } else {
931         CurrentState = Instructions;
932       }
933       return false;
934     }
935     case Match_MissingFeature: {
936       assert(MissingFeatures.count() > 0 && "Expected missing features");
937       SmallString<128> Message;
938       raw_svector_ostream OS(Message);
939       OS << "instruction requires:";
940       for (unsigned i = 0, e = MissingFeatures.size(); i != e; ++i)
941         if (MissingFeatures.test(i))
942           OS << ' ' << getSubtargetFeatureName(i);
943       return Parser.Error(IDLoc, Message);
944     }
945     case Match_MnemonicFail:
946       return Parser.Error(IDLoc, "invalid instruction");
947     case Match_NearMisses:
948       return Parser.Error(IDLoc, "ambiguous instruction");
949     case Match_InvalidTiedOperand:
950     case Match_InvalidOperand: {
951       SMLoc ErrorLoc = IDLoc;
952       if (ErrorInfo != ~0ULL) {
953         if (ErrorInfo >= Operands.size())
954           return Parser.Error(IDLoc, "too few operands for instruction");
955         ErrorLoc = Operands[ErrorInfo]->getStartLoc();
956         if (ErrorLoc == SMLoc())
957           ErrorLoc = IDLoc;
958       }
959       return Parser.Error(ErrorLoc, "invalid operand for instruction");
960     }
961     }
962     llvm_unreachable("Implement any new match types added!");
963   }
964 
965   void doBeforeLabelEmit(MCSymbol *Symbol) override {
966     // Start a new section for the next function automatically, since our
967     // object writer expects each function to have its own section. This way
968     // The user can't forget this "convention".
969     auto SymName = Symbol->getName();
970     if (SymName.startswith(".L"))
971       return; // Local Symbol.
972     // Only create a new text section if we're already in one.
973     auto CWS = cast<MCSectionWasm>(getStreamer().getCurrentSection().first);
974     if (!CWS || !CWS->getKind().isText())
975       return;
976     auto SecName = ".text." + SymName;
977     auto WS = getContext().getWasmSection(SecName, SectionKind::getText());
978     getStreamer().SwitchSection(WS);
979     // Also generate DWARF for this section if requested.
980     if (getContext().getGenDwarfForAssembly())
981       getContext().addGenDwarfSection(WS);
982   }
983 
984   void onEndOfFunction() {
985     // Automatically output a .size directive, so it becomes optional for the
986     // user.
987     if (!LastFunctionLabel) return;
988     auto TempSym = getContext().createLinkerPrivateTempSymbol();
989     getStreamer().emitLabel(TempSym);
990     auto Start = MCSymbolRefExpr::create(LastFunctionLabel, getContext());
991     auto End = MCSymbolRefExpr::create(TempSym, getContext());
992     auto Expr =
993         MCBinaryExpr::create(MCBinaryExpr::Sub, End, Start, getContext());
994     getStreamer().emitELFSize(LastFunctionLabel, Expr);
995   }
996 
997   void onEndOfFile() override { ensureEmptyNestingStack(); }
998 };
999 } // end anonymous namespace
1000 
1001 // Force static initialization.
1002 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeWebAssemblyAsmParser() {
1003   RegisterMCAsmParser<WebAssemblyAsmParser> X(getTheWebAssemblyTarget32());
1004   RegisterMCAsmParser<WebAssemblyAsmParser> Y(getTheWebAssemblyTarget64());
1005 }
1006 
1007 #define GET_REGISTER_MATCHER
1008 #define GET_SUBTARGET_FEATURE_NAME
1009 #define GET_MATCHER_IMPLEMENTATION
1010 #include "WebAssemblyGenAsmMatcher.inc"
1011