blob: c93b21575353ec1b05aef13544d04c589b3b41e7 [file] [log] [blame]
Derek Schuffe4825972018-03-20 20:06:35 +00001//==- WebAssemblyAsmParser.cpp - Assembler for WebAssembly -*- C++ -*-==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000011/// This file is part of the WebAssembly Assembler.
Derek Schuffe4825972018-03-20 20:06:35 +000012///
13/// It contains code to translate a parsed .s file into MCInsts.
14///
15//===----------------------------------------------------------------------===//
16
17#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
18#include "MCTargetDesc/WebAssemblyTargetStreamer.h"
19#include "WebAssembly.h"
20#include "llvm/MC/MCContext.h"
Derek Schuffe4825972018-03-20 20:06:35 +000021#include "llvm/MC/MCInst.h"
22#include "llvm/MC/MCInstrInfo.h"
Heejin Ahnf208f632018-09-05 01:27:38 +000023#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
24#include "llvm/MC/MCParser/MCTargetAsmParser.h"
25#include "llvm/MC/MCStreamer.h"
Derek Schuffe4825972018-03-20 20:06:35 +000026#include "llvm/MC/MCSubtargetInfo.h"
27#include "llvm/MC/MCSymbol.h"
Wouter van Oortmerssende28b5d2018-11-02 22:04:33 +000028#include "llvm/MC/MCSymbolWasm.h"
Derek Schuffe4825972018-03-20 20:06:35 +000029#include "llvm/Support/Endian.h"
30#include "llvm/Support/TargetRegistry.h"
31
32using namespace llvm;
33
34#define DEBUG_TYPE "wasm-asm-parser"
35
Derek Schuffe4825972018-03-20 20:06:35 +000036namespace {
37
Derek Schuffe4825972018-03-20 20:06:35 +000038/// WebAssemblyOperand - Instances of this class represent the operands in a
39/// parsed WASM machine instruction.
40struct WebAssemblyOperand : public MCParsedAsmOperand {
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +000041 enum KindTy { Token, Integer, Float, Symbol } Kind;
Derek Schuffe4825972018-03-20 20:06:35 +000042
43 SMLoc StartLoc, EndLoc;
44
45 struct TokOp {
46 StringRef Tok;
47 };
48
Derek Schuffe4825972018-03-20 20:06:35 +000049 struct IntOp {
50 int64_t Val;
51 };
52
53 struct FltOp {
54 double Val;
55 };
56
57 struct SymOp {
58 const MCExpr *Exp;
59 };
60
61 union {
62 struct TokOp Tok;
Derek Schuffe4825972018-03-20 20:06:35 +000063 struct IntOp Int;
64 struct FltOp Flt;
65 struct SymOp Sym;
66 };
67
68 WebAssemblyOperand(KindTy K, SMLoc Start, SMLoc End, TokOp T)
Heejin Ahnf208f632018-09-05 01:27:38 +000069 : Kind(K), StartLoc(Start), EndLoc(End), Tok(T) {}
Derek Schuffe4825972018-03-20 20:06:35 +000070 WebAssemblyOperand(KindTy K, SMLoc Start, SMLoc End, IntOp I)
Heejin Ahnf208f632018-09-05 01:27:38 +000071 : Kind(K), StartLoc(Start), EndLoc(End), Int(I) {}
Derek Schuffe4825972018-03-20 20:06:35 +000072 WebAssemblyOperand(KindTy K, SMLoc Start, SMLoc End, FltOp F)
Heejin Ahnf208f632018-09-05 01:27:38 +000073 : Kind(K), StartLoc(Start), EndLoc(End), Flt(F) {}
Derek Schuffe4825972018-03-20 20:06:35 +000074 WebAssemblyOperand(KindTy K, SMLoc Start, SMLoc End, SymOp S)
Heejin Ahnf208f632018-09-05 01:27:38 +000075 : Kind(K), StartLoc(Start), EndLoc(End), Sym(S) {}
Derek Schuffe4825972018-03-20 20:06:35 +000076
77 bool isToken() const override { return Kind == Token; }
Heejin Ahnf208f632018-09-05 01:27:38 +000078 bool isImm() const override {
79 return Kind == Integer || Kind == Float || Kind == Symbol;
80 }
Derek Schuffe4825972018-03-20 20:06:35 +000081 bool isMem() const override { return false; }
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +000082 bool isReg() const override { return false; }
Derek Schuffe4825972018-03-20 20:06:35 +000083
84 unsigned getReg() const override {
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +000085 llvm_unreachable("Assembly inspects a register operand");
86 return 0;
Derek Schuffe4825972018-03-20 20:06:35 +000087 }
88
89 StringRef getToken() const {
90 assert(isToken());
91 return Tok.Tok;
92 }
93
94 SMLoc getStartLoc() const override { return StartLoc; }
95 SMLoc getEndLoc() const override { return EndLoc; }
96
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +000097 void addRegOperands(MCInst &, unsigned) const {
98 // Required by the assembly matcher.
99 llvm_unreachable("Assembly matcher creates register operands");
Derek Schuffe4825972018-03-20 20:06:35 +0000100 }
101
102 void addImmOperands(MCInst &Inst, unsigned N) const {
103 assert(N == 1 && "Invalid number of operands!");
104 if (Kind == Integer)
105 Inst.addOperand(MCOperand::createImm(Int.Val));
106 else if (Kind == Float)
107 Inst.addOperand(MCOperand::createFPImm(Flt.Val));
108 else if (Kind == Symbol)
109 Inst.addOperand(MCOperand::createExpr(Sym.Exp));
110 else
111 llvm_unreachable("Should be immediate or symbol!");
112 }
113
114 void print(raw_ostream &OS) const override {
115 switch (Kind) {
116 case Token:
117 OS << "Tok:" << Tok.Tok;
118 break;
Derek Schuffe4825972018-03-20 20:06:35 +0000119 case Integer:
120 OS << "Int:" << Int.Val;
121 break;
122 case Float:
123 OS << "Flt:" << Flt.Val;
124 break;
125 case Symbol:
126 OS << "Sym:" << Sym.Exp;
127 break;
128 }
129 }
130};
131
132class WebAssemblyAsmParser final : public MCTargetAsmParser {
133 MCAsmParser &Parser;
134 MCAsmLexer &Lexer;
Derek Schuffe4825972018-03-20 20:06:35 +0000135
Wouter van Oortmerssen49482f82018-11-19 17:10:36 +0000136 // Much like WebAssemblyAsmPrinter in the backend, we have to own these.
137 std::vector<std::unique_ptr<wasm::WasmSignature>> Signatures;
138
Wouter van Oortmerssenc7b89f02018-12-03 20:30:28 +0000139 // Order of labels, directives and instructions in a .s file have no
140 // syntactical enforcement. This class is a callback from the actual parser,
141 // and yet we have to be feeding data to the streamer in a very particular
142 // order to ensure a correct binary encoding that matches the regular backend
143 // (the streamer does not enforce this). This "state machine" enum helps
144 // guarantee that correct order.
145 enum ParserState {
146 FileStart,
147 Label,
148 FunctionStart,
149 FunctionLocals,
150 Instructions,
151 } CurrentState = FileStart;
152
153 // We track this to see if a .functype following a label is the same,
154 // as this is how we recognize the start of a function.
155 MCSymbol *LastLabel = nullptr;
156
Derek Schuffe4825972018-03-20 20:06:35 +0000157public:
Wouter van Oortmerssende28b5d2018-11-02 22:04:33 +0000158 WebAssemblyAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser,
159 const MCInstrInfo &MII, const MCTargetOptions &Options)
160 : MCTargetAsmParser(Options, STI, MII), Parser(Parser),
Wouter van Oortmerssencc75e772018-11-12 20:15:01 +0000161 Lexer(Parser.getLexer()) {
Wouter van Oortmerssende28b5d2018-11-02 22:04:33 +0000162 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Derek Schuffe4825972018-03-20 20:06:35 +0000163 }
164
Wouter van Oortmerssen49482f82018-11-19 17:10:36 +0000165 void addSignature(std::unique_ptr<wasm::WasmSignature> &&Sig) {
166 Signatures.push_back(std::move(Sig));
167 }
168
Derek Schuffe4825972018-03-20 20:06:35 +0000169#define GET_ASSEMBLER_HEADER
170#include "WebAssemblyGenAsmMatcher.inc"
171
172 // TODO: This is required to be implemented, but appears unused.
Heejin Ahnf208f632018-09-05 01:27:38 +0000173 bool ParseRegister(unsigned & /*RegNo*/, SMLoc & /*StartLoc*/,
174 SMLoc & /*EndLoc*/) override {
Derek Schuffe4825972018-03-20 20:06:35 +0000175 llvm_unreachable("ParseRegister is not implemented.");
176 }
177
178 bool Error(const StringRef &msg, const AsmToken &tok) {
179 return Parser.Error(tok.getLoc(), msg + tok.getString());
180 }
181
182 bool IsNext(AsmToken::TokenKind Kind) {
183 auto ok = Lexer.is(Kind);
Heejin Ahnf208f632018-09-05 01:27:38 +0000184 if (ok)
185 Parser.Lex();
Derek Schuffe4825972018-03-20 20:06:35 +0000186 return ok;
187 }
188
189 bool Expect(AsmToken::TokenKind Kind, const char *KindName) {
190 if (!IsNext(Kind))
191 return Error(std::string("Expected ") + KindName + ", instead got: ",
192 Lexer.getTok());
193 return false;
194 }
195
Wouter van Oortmerssen49482f82018-11-19 17:10:36 +0000196 StringRef ExpectIdent() {
197 if (!Lexer.is(AsmToken::Identifier)) {
198 Error("Expected identifier, got: ", Lexer.getTok());
199 return StringRef();
200 }
201 auto Name = Lexer.getTok().getString();
202 Parser.Lex();
203 return Name;
Derek Schuffe4825972018-03-20 20:06:35 +0000204 }
205
Wouter van Oortmerssen49482f82018-11-19 17:10:36 +0000206 Optional<wasm::ValType> ParseType(const StringRef &Type) {
207 // FIXME: can't use StringSwitch because wasm::ValType doesn't have a
208 // "invalid" value.
209 if (Type == "i32") return wasm::ValType::I32;
210 if (Type == "i64") return wasm::ValType::I64;
211 if (Type == "f32") return wasm::ValType::F32;
212 if (Type == "f64") return wasm::ValType::F64;
213 if (Type == "v128" || Type == "i8x16" || Type == "i16x8" ||
214 Type == "i32x4" || Type == "i64x2" || Type == "f32x4" ||
215 Type == "f64x2") return wasm::ValType::V128;
216 return Optional<wasm::ValType>();
217 }
218
219 bool ParseRegTypeList(SmallVectorImpl<wasm::ValType> &Types) {
Wouter van Oortmerssencc75e772018-11-12 20:15:01 +0000220 while (Lexer.is(AsmToken::Identifier)) {
Wouter van Oortmerssen49482f82018-11-19 17:10:36 +0000221 auto Type = ParseType(Lexer.getTok().getString());
222 if (!Type)
Wouter van Oortmerssencc75e772018-11-12 20:15:01 +0000223 return true;
Wouter van Oortmerssen49482f82018-11-19 17:10:36 +0000224 Types.push_back(Type.getValue());
Wouter van Oortmerssencc75e772018-11-12 20:15:01 +0000225 Parser.Lex();
226 if (!IsNext(AsmToken::Comma))
227 break;
228 }
Wouter van Oortmerssen49482f82018-11-19 17:10:36 +0000229 return false;
Wouter van Oortmerssencc75e772018-11-12 20:15:01 +0000230 }
231
Derek Schuffe4825972018-03-20 20:06:35 +0000232 void ParseSingleInteger(bool IsNegative, OperandVector &Operands) {
233 auto &Int = Lexer.getTok();
234 int64_t Val = Int.getIntVal();
Heejin Ahnf208f632018-09-05 01:27:38 +0000235 if (IsNegative)
236 Val = -Val;
Derek Schuffe4825972018-03-20 20:06:35 +0000237 Operands.push_back(make_unique<WebAssemblyOperand>(
Heejin Ahnf208f632018-09-05 01:27:38 +0000238 WebAssemblyOperand::Integer, Int.getLoc(), Int.getEndLoc(),
239 WebAssemblyOperand::IntOp{Val}));
Derek Schuffe4825972018-03-20 20:06:35 +0000240 Parser.Lex();
241 }
242
Heejin Ahnf208f632018-09-05 01:27:38 +0000243 bool ParseOperandStartingWithInteger(bool IsNegative, OperandVector &Operands,
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +0000244 StringRef InstName) {
Derek Schuffe4825972018-03-20 20:06:35 +0000245 ParseSingleInteger(IsNegative, Operands);
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +0000246 // FIXME: there is probably a cleaner way to do this.
247 auto IsLoadStore = InstName.startswith("load") ||
248 InstName.startswith("store") ||
249 InstName.startswith("atomic_load") ||
250 InstName.startswith("atomic_store");
251 if (IsLoadStore) {
252 // Parse load/store operands of the form: offset align
253 auto &Offset = Lexer.getTok();
254 if (Offset.is(AsmToken::Integer)) {
Derek Schuffe4825972018-03-20 20:06:35 +0000255 ParseSingleInteger(false, Operands);
256 } else {
257 // Alignment not specified.
258 // FIXME: correctly derive a default from the instruction.
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +0000259 // We can't just call WebAssembly::GetDefaultP2Align since we don't have
260 // an opcode until after the assembly matcher.
Derek Schuffe4825972018-03-20 20:06:35 +0000261 Operands.push_back(make_unique<WebAssemblyOperand>(
Heejin Ahnf208f632018-09-05 01:27:38 +0000262 WebAssemblyOperand::Integer, Offset.getLoc(), Offset.getEndLoc(),
263 WebAssemblyOperand::IntOp{0}));
Derek Schuffe4825972018-03-20 20:06:35 +0000264 }
265 }
266 return false;
267 }
268
Heejin Ahnf208f632018-09-05 01:27:38 +0000269 bool ParseInstruction(ParseInstructionInfo & /*Info*/, StringRef Name,
Derek Schuffe4825972018-03-20 20:06:35 +0000270 SMLoc NameLoc, OperandVector &Operands) override {
Wouter van Oortmerssen0c83c3f2018-10-01 17:20:31 +0000271 // Note: Name does NOT point into the sourcecode, but to a local, so
272 // use NameLoc instead.
273 Name = StringRef(NameLoc.getPointer(), Name.size());
274 // WebAssembly has instructions with / in them, which AsmLexer parses
275 // as seperate tokens, so if we find such tokens immediately adjacent (no
276 // whitespace), expand the name to include them:
277 for (;;) {
278 auto &Sep = Lexer.getTok();
279 if (Sep.getLoc().getPointer() != Name.end() ||
280 Sep.getKind() != AsmToken::Slash) break;
281 // Extend name with /
282 Name = StringRef(Name.begin(), Name.size() + Sep.getString().size());
283 Parser.Lex();
284 // We must now find another identifier, or error.
285 auto &Id = Lexer.getTok();
286 if (Id.getKind() != AsmToken::Identifier ||
287 Id.getLoc().getPointer() != Name.end())
288 return Error("Incomplete instruction name: ", Id);
289 Name = StringRef(Name.begin(), Name.size() + Id.getString().size());
290 Parser.Lex();
291 }
292 // Now construct the name as first operand.
Heejin Ahnf208f632018-09-05 01:27:38 +0000293 Operands.push_back(make_unique<WebAssemblyOperand>(
Wouter van Oortmerssen0c83c3f2018-10-01 17:20:31 +0000294 WebAssemblyOperand::Token, NameLoc, SMLoc::getFromPointer(Name.end()),
295 WebAssemblyOperand::TokOp{Name}));
Derek Schuffe4825972018-03-20 20:06:35 +0000296 auto NamePair = Name.split('.');
297 // If no '.', there is no type prefix.
Wouter van Oortmerssen0c83c3f2018-10-01 17:20:31 +0000298 auto BaseName = NamePair.second.empty() ? NamePair.first : NamePair.second;
Derek Schuffe4825972018-03-20 20:06:35 +0000299 while (Lexer.isNot(AsmToken::EndOfStatement)) {
300 auto &Tok = Lexer.getTok();
301 switch (Tok.getKind()) {
Derek Schuffe4825972018-03-20 20:06:35 +0000302 case AsmToken::Identifier: {
303 auto &Id = Lexer.getTok();
304 const MCExpr *Val;
305 SMLoc End;
306 if (Parser.parsePrimaryExpr(Val, End))
307 return Error("Cannot parse symbol: ", Lexer.getTok());
308 Operands.push_back(make_unique<WebAssemblyOperand>(
Heejin Ahnf208f632018-09-05 01:27:38 +0000309 WebAssemblyOperand::Symbol, Id.getLoc(), Id.getEndLoc(),
310 WebAssemblyOperand::SymOp{Val}));
Derek Schuffe4825972018-03-20 20:06:35 +0000311 break;
312 }
313 case AsmToken::Minus:
314 Parser.Lex();
315 if (Lexer.isNot(AsmToken::Integer))
316 return Error("Expected integer instead got: ", Lexer.getTok());
Wouter van Oortmerssen0c83c3f2018-10-01 17:20:31 +0000317 if (ParseOperandStartingWithInteger(true, Operands, BaseName))
Derek Schuffe4825972018-03-20 20:06:35 +0000318 return true;
319 break;
320 case AsmToken::Integer:
Wouter van Oortmerssen0c83c3f2018-10-01 17:20:31 +0000321 if (ParseOperandStartingWithInteger(false, Operands, BaseName))
Derek Schuffe4825972018-03-20 20:06:35 +0000322 return true;
323 break;
324 case AsmToken::Real: {
325 double Val;
326 if (Tok.getString().getAsDouble(Val, false))
327 return Error("Cannot parse real: ", Tok);
328 Operands.push_back(make_unique<WebAssemblyOperand>(
Heejin Ahnf208f632018-09-05 01:27:38 +0000329 WebAssemblyOperand::Float, Tok.getLoc(), Tok.getEndLoc(),
330 WebAssemblyOperand::FltOp{Val}));
Derek Schuffe4825972018-03-20 20:06:35 +0000331 Parser.Lex();
332 break;
333 }
334 default:
335 return Error("Unexpected token in operand: ", Tok);
336 }
337 if (Lexer.isNot(AsmToken::EndOfStatement)) {
Heejin Ahnf208f632018-09-05 01:27:38 +0000338 if (Expect(AsmToken::Comma, ","))
339 return true;
Derek Schuffe4825972018-03-20 20:06:35 +0000340 }
341 }
342 Parser.Lex();
Derek Schuffe4825972018-03-20 20:06:35 +0000343 // Block instructions require a signature index, but these are missing in
344 // assembly, so we add a dummy one explicitly (since we have no control
345 // over signature tables here, we assume these will be regenerated when
346 // the wasm module is generated).
Heejin Ahn2e398972018-11-01 20:32:15 +0000347 if (BaseName == "block" || BaseName == "loop" || BaseName == "try") {
Derek Schuffe4825972018-03-20 20:06:35 +0000348 Operands.push_back(make_unique<WebAssemblyOperand>(
Heejin Ahnf208f632018-09-05 01:27:38 +0000349 WebAssemblyOperand::Integer, NameLoc, NameLoc,
350 WebAssemblyOperand::IntOp{-1}));
Derek Schuffe4825972018-03-20 20:06:35 +0000351 }
Derek Schuffe4825972018-03-20 20:06:35 +0000352 return false;
353 }
354
Wouter van Oortmerssenc7b89f02018-12-03 20:30:28 +0000355 void onLabelParsed(MCSymbol *Symbol) override {
356 LastLabel = Symbol;
357 CurrentState = Label;
358 }
359
Wouter van Oortmerssencc75e772018-11-12 20:15:01 +0000360 // This function processes wasm-specific directives streamed to
361 // WebAssemblyTargetStreamer, all others go to the generic parser
362 // (see WasmAsmParser).
Derek Schuffe4825972018-03-20 20:06:35 +0000363 bool ParseDirective(AsmToken DirectiveID) override {
Wouter van Oortmerssende28b5d2018-11-02 22:04:33 +0000364 // This function has a really weird return value behavior that is different
365 // from all the other parsing functions:
366 // - return true && no tokens consumed -> don't know this directive / let
367 // the generic parser handle it.
368 // - return true && tokens consumed -> a parsing error occurred.
369 // - return false -> processed this directive successfully.
Derek Schuffe4825972018-03-20 20:06:35 +0000370 assert(DirectiveID.getKind() == AsmToken::Identifier);
371 auto &Out = getStreamer();
Heejin Ahnf208f632018-09-05 01:27:38 +0000372 auto &TOut =
373 reinterpret_cast<WebAssemblyTargetStreamer &>(*Out.getTargetStreamer());
Wouter van Oortmerssende28b5d2018-11-02 22:04:33 +0000374 // TODO: any time we return an error, at least one token must have been
375 // consumed, otherwise this will not signal an error to the caller.
Wouter van Oortmerssencc75e772018-11-12 20:15:01 +0000376 if (DirectiveID.getString() == ".globaltype") {
Wouter van Oortmerssen49482f82018-11-19 17:10:36 +0000377 auto SymName = ExpectIdent();
378 if (SymName.empty()) return true;
379 if (Expect(AsmToken::Comma, ",")) return true;
380 auto TypeTok = Lexer.getTok();
381 auto TypeName = ExpectIdent();
382 if (TypeName.empty()) return true;
383 auto Type = ParseType(TypeName);
384 if (!Type)
385 return Error("Unknown type in .globaltype directive: ", TypeTok);
Wouter van Oortmerssende28b5d2018-11-02 22:04:33 +0000386 // Now set this symbol with the correct type.
387 auto WasmSym = cast<MCSymbolWasm>(
Wouter van Oortmerssen49482f82018-11-19 17:10:36 +0000388 TOut.getStreamer().getContext().getOrCreateSymbol(SymName));
Wouter van Oortmerssende28b5d2018-11-02 22:04:33 +0000389 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
Wouter van Oortmerssen49482f82018-11-19 17:10:36 +0000390 WasmSym->setGlobalType(
391 wasm::WasmGlobalType{uint8_t(Type.getValue()), true});
Wouter van Oortmerssende28b5d2018-11-02 22:04:33 +0000392 // And emit the directive again.
393 TOut.emitGlobalType(WasmSym);
394 return Expect(AsmToken::EndOfStatement, "EOL");
Wouter van Oortmerssen49482f82018-11-19 17:10:36 +0000395 } else if (DirectiveID.getString() == ".functype") {
Wouter van Oortmerssenc7b89f02018-12-03 20:30:28 +0000396 // This code has to send things to the streamer similar to
397 // WebAssemblyAsmPrinter::EmitFunctionBodyStart.
398 // TODO: would be good to factor this into a common function, but the
399 // assembler and backend really don't share any common code, and this code
400 // parses the locals seperately.
Wouter van Oortmerssen49482f82018-11-19 17:10:36 +0000401 auto SymName = ExpectIdent();
402 if (SymName.empty()) return true;
403 auto WasmSym = cast<MCSymbolWasm>(
404 TOut.getStreamer().getContext().getOrCreateSymbol(SymName));
Wouter van Oortmerssenc7b89f02018-12-03 20:30:28 +0000405 if (CurrentState == Label && WasmSym == LastLabel) {
406 // This .functype indicates a start of a function.
407 CurrentState = FunctionStart;
408 }
Wouter van Oortmerssen49482f82018-11-19 17:10:36 +0000409 auto Signature = make_unique<wasm::WasmSignature>();
410 if (Expect(AsmToken::LParen, "(")) return true;
411 if (ParseRegTypeList(Signature->Params)) return true;
412 if (Expect(AsmToken::RParen, ")")) return true;
413 if (Expect(AsmToken::MinusGreater, "->")) return true;
414 if (Expect(AsmToken::LParen, "(")) return true;
415 if (ParseRegTypeList(Signature->Returns)) return true;
416 if (Expect(AsmToken::RParen, ")")) return true;
417 WasmSym->setSignature(Signature.get());
418 addSignature(std::move(Signature));
419 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
420 TOut.emitFunctionType(WasmSym);
Wouter van Oortmerssenc7b89f02018-12-03 20:30:28 +0000421 // TODO: backend also calls TOut.emitIndIdx, but that is not implemented.
Wouter van Oortmerssen49482f82018-11-19 17:10:36 +0000422 return Expect(AsmToken::EndOfStatement, "EOL");
Wouter van Oortmerssencc75e772018-11-12 20:15:01 +0000423 } else if (DirectiveID.getString() == ".local") {
Wouter van Oortmerssenc7b89f02018-12-03 20:30:28 +0000424 if (CurrentState != FunctionStart)
425 return Error(".local directive should follow the start of a function",
426 Lexer.getTok());
Wouter van Oortmerssen49482f82018-11-19 17:10:36 +0000427 SmallVector<wasm::ValType, 4> Locals;
Wouter van Oortmerssencc75e772018-11-12 20:15:01 +0000428 if (ParseRegTypeList(Locals)) return true;
Derek Schuffe4825972018-03-20 20:06:35 +0000429 TOut.emitLocal(Locals);
Wouter van Oortmerssenc7b89f02018-12-03 20:30:28 +0000430 CurrentState = FunctionLocals;
Wouter van Oortmerssen49482f82018-11-19 17:10:36 +0000431 return Expect(AsmToken::EndOfStatement, "EOL");
Derek Schuffe4825972018-03-20 20:06:35 +0000432 }
Wouter van Oortmerssencc75e772018-11-12 20:15:01 +0000433 return true; // We didn't process this directive.
Derek Schuffe4825972018-03-20 20:06:35 +0000434 }
435
Heejin Ahnf208f632018-09-05 01:27:38 +0000436 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned & /*Opcode*/,
437 OperandVector &Operands, MCStreamer &Out,
438 uint64_t &ErrorInfo,
Derek Schuffe4825972018-03-20 20:06:35 +0000439 bool MatchingInlineAsm) override {
440 MCInst Inst;
441 unsigned MatchResult =
442 MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
443 switch (MatchResult) {
444 case Match_Success: {
Wouter van Oortmerssenc7b89f02018-12-03 20:30:28 +0000445 if (CurrentState == FunctionStart) {
446 // This is the first instruction in a function, but we haven't seen
447 // a .local directive yet. The streamer requires locals to be encoded
448 // as a prelude to the instructions, so emit an empty list of locals
449 // here.
450 auto &TOut = reinterpret_cast<WebAssemblyTargetStreamer &>(
451 *Out.getTargetStreamer());
452 TOut.emitLocal(SmallVector<wasm::ValType, 0>());
453 }
454 CurrentState = Instructions;
Derek Schuffe4825972018-03-20 20:06:35 +0000455 Out.EmitInstruction(Inst, getSTI());
456 return false;
457 }
458 case Match_MissingFeature:
Heejin Ahnf208f632018-09-05 01:27:38 +0000459 return Parser.Error(
460 IDLoc, "instruction requires a WASM feature not currently enabled");
Derek Schuffe4825972018-03-20 20:06:35 +0000461 case Match_MnemonicFail:
462 return Parser.Error(IDLoc, "invalid instruction");
463 case Match_NearMisses:
464 return Parser.Error(IDLoc, "ambiguous instruction");
465 case Match_InvalidTiedOperand:
466 case Match_InvalidOperand: {
467 SMLoc ErrorLoc = IDLoc;
468 if (ErrorInfo != ~0ULL) {
469 if (ErrorInfo >= Operands.size())
470 return Parser.Error(IDLoc, "too few operands for instruction");
471 ErrorLoc = Operands[ErrorInfo]->getStartLoc();
472 if (ErrorLoc == SMLoc())
473 ErrorLoc = IDLoc;
474 }
475 return Parser.Error(ErrorLoc, "invalid operand for instruction");
476 }
477 }
478 llvm_unreachable("Implement any new match types added!");
479 }
480};
481} // end anonymous namespace
482
483// Force static initialization.
484extern "C" void LLVMInitializeWebAssemblyAsmParser() {
485 RegisterMCAsmParser<WebAssemblyAsmParser> X(getTheWebAssemblyTarget32());
486 RegisterMCAsmParser<WebAssemblyAsmParser> Y(getTheWebAssemblyTarget64());
487}
488
489#define GET_REGISTER_MATCHER
490#define GET_MATCHER_IMPLEMENTATION
491#include "WebAssemblyGenAsmMatcher.inc"