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