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