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