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