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