blob: 0d591c0453508f60621f589addf8196b2c965527 [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"
21#include "llvm/MC/MCParser/MCTargetAsmParser.h"
22#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
23#include "llvm/MC/MCInst.h"
24#include "llvm/MC/MCInstrInfo.h"
25#include "llvm/MC/MCSubtargetInfo.h"
26#include "llvm/MC/MCSymbol.h"
27#include "llvm/MC/MCStreamer.h"
28#include "llvm/Support/Endian.h"
29#include "llvm/Support/TargetRegistry.h"
30
31using namespace llvm;
32
33#define DEBUG_TYPE "wasm-asm-parser"
34
Derek Schuffe4825972018-03-20 20:06:35 +000035namespace {
36
Wouter van Oortmerssena7be3752018-08-13 23:12:49 +000037// We store register types as SimpleValueType to retain SIMD layout
38// information, but must also be able to supply them as the (unnamed)
39// register enum from WebAssemblyRegisterInfo.td/.inc.
40static unsigned MVTToWasmReg(MVT::SimpleValueType Type) {
41 switch(Type) {
42 case MVT::i32: return WebAssembly::I32_0;
43 case MVT::i64: return WebAssembly::I64_0;
44 case MVT::f32: return WebAssembly::F32_0;
45 case MVT::f64: return WebAssembly::F64_0;
46 case MVT::v16i8: return WebAssembly::V128_0;
47 case MVT::v8i16: return WebAssembly::V128_0;
48 case MVT::v4i32: return WebAssembly::V128_0;
Thomas Lively22442922018-08-21 21:03:18 +000049 case MVT::v2i64: return WebAssembly::V128_0;
Wouter van Oortmerssena7be3752018-08-13 23:12:49 +000050 case MVT::v4f32: return WebAssembly::V128_0;
Thomas Lively22442922018-08-21 21:03:18 +000051 case MVT::v2f64: return WebAssembly::V128_0;
Wouter van Oortmerssena7be3752018-08-13 23:12:49 +000052 default: return MVT::INVALID_SIMPLE_VALUE_TYPE;
53 }
54}
55
Derek Schuffe4825972018-03-20 20:06:35 +000056/// WebAssemblyOperand - Instances of this class represent the operands in a
57/// parsed WASM machine instruction.
58struct WebAssemblyOperand : public MCParsedAsmOperand {
Wouter van Oortmerssena7be3752018-08-13 23:12:49 +000059 enum KindTy { Token, Local, Stack, Integer, Float, Symbol } Kind;
Derek Schuffe4825972018-03-20 20:06:35 +000060
61 SMLoc StartLoc, EndLoc;
62
63 struct TokOp {
64 StringRef Tok;
65 };
66
Wouter van Oortmerssena7be3752018-08-13 23:12:49 +000067 struct RegOp {
68 // This is a (virtual) local or stack register represented as 0..
69 unsigned RegNo;
70 // In most targets, the register number also encodes the type, but for
71 // wasm we have to track that seperately since we have an unbounded
72 // number of registers.
73 // This has the unfortunate side effect that we supply a different value
74 // to the table-gen matcher at different times in the process (when it
75 // calls getReg() or addRegOperands().
76 // TODO: While this works, it feels brittle. and would be nice to clean up.
77 MVT::SimpleValueType Type;
78 };
79
Derek Schuffe4825972018-03-20 20:06:35 +000080 struct IntOp {
81 int64_t Val;
82 };
83
84 struct FltOp {
85 double Val;
86 };
87
88 struct SymOp {
89 const MCExpr *Exp;
90 };
91
92 union {
93 struct TokOp Tok;
Wouter van Oortmerssena7be3752018-08-13 23:12:49 +000094 struct RegOp Reg;
Derek Schuffe4825972018-03-20 20:06:35 +000095 struct IntOp Int;
96 struct FltOp Flt;
97 struct SymOp Sym;
98 };
99
100 WebAssemblyOperand(KindTy K, SMLoc Start, SMLoc End, TokOp T)
101 : Kind(K), StartLoc(Start), EndLoc(End), Tok(T) {}
Wouter van Oortmerssena7be3752018-08-13 23:12:49 +0000102 WebAssemblyOperand(KindTy K, SMLoc Start, SMLoc End, RegOp R)
103 : Kind(K), StartLoc(Start), EndLoc(End), Reg(R) {}
Derek Schuffe4825972018-03-20 20:06:35 +0000104 WebAssemblyOperand(KindTy K, SMLoc Start, SMLoc End, IntOp I)
105 : Kind(K), StartLoc(Start), EndLoc(End), Int(I) {}
106 WebAssemblyOperand(KindTy K, SMLoc Start, SMLoc End, FltOp F)
107 : Kind(K), StartLoc(Start), EndLoc(End), Flt(F) {}
108 WebAssemblyOperand(KindTy K, SMLoc Start, SMLoc End, SymOp S)
109 : Kind(K), StartLoc(Start), EndLoc(End), Sym(S) {}
110
111 bool isToken() const override { return Kind == Token; }
112 bool isImm() const override { return Kind == Integer ||
113 Kind == Float ||
114 Kind == Symbol; }
Wouter van Oortmerssena7be3752018-08-13 23:12:49 +0000115 bool isReg() const override { return Kind == Local || Kind == Stack; }
Derek Schuffe4825972018-03-20 20:06:35 +0000116 bool isMem() const override { return false; }
117
118 unsigned getReg() const override {
Wouter van Oortmerssena7be3752018-08-13 23:12:49 +0000119 assert(isReg());
120 // This is called from the tablegen matcher (MatchInstructionImpl)
121 // where it expects to match the type of register, see RegOp above.
122 return MVTToWasmReg(Reg.Type);
Derek Schuffe4825972018-03-20 20:06:35 +0000123 }
124
125 StringRef getToken() const {
126 assert(isToken());
127 return Tok.Tok;
128 }
129
130 SMLoc getStartLoc() const override { return StartLoc; }
131 SMLoc getEndLoc() const override { return EndLoc; }
132
Wouter van Oortmerssena7be3752018-08-13 23:12:49 +0000133 void addRegOperands(MCInst &Inst, unsigned N) const {
134 assert(N == 1 && "Invalid number of operands!");
135 assert(isReg() && "Not a register operand!");
136 // This is called from the tablegen matcher (MatchInstructionImpl)
137 // where it expects to output the actual register index, see RegOp above.
138 unsigned R = Reg.RegNo;
139 if (Kind == Stack) {
140 // A stack register is represented as a large negative number.
141 // See WebAssemblyRegNumbering::runOnMachineFunction and
142 // getWARegStackId for why this | is needed.
143 R |= INT32_MIN;
144 }
145 Inst.addOperand(MCOperand::createReg(R));
Derek Schuffe4825972018-03-20 20:06:35 +0000146 }
147
148 void addImmOperands(MCInst &Inst, unsigned N) const {
149 assert(N == 1 && "Invalid number of operands!");
150 if (Kind == Integer)
151 Inst.addOperand(MCOperand::createImm(Int.Val));
152 else if (Kind == Float)
153 Inst.addOperand(MCOperand::createFPImm(Flt.Val));
154 else if (Kind == Symbol)
155 Inst.addOperand(MCOperand::createExpr(Sym.Exp));
156 else
157 llvm_unreachable("Should be immediate or symbol!");
158 }
159
160 void print(raw_ostream &OS) const override {
161 switch (Kind) {
162 case Token:
163 OS << "Tok:" << Tok.Tok;
164 break;
Wouter van Oortmerssena7be3752018-08-13 23:12:49 +0000165 case Local:
166 OS << "Loc:" << Reg.RegNo << ":" << static_cast<int>(Reg.Type);
167 break;
168 case Stack:
169 OS << "Stk:" << Reg.RegNo << ":" << static_cast<int>(Reg.Type);
170 break;
Derek Schuffe4825972018-03-20 20:06:35 +0000171 case Integer:
172 OS << "Int:" << Int.Val;
173 break;
174 case Float:
175 OS << "Flt:" << Flt.Val;
176 break;
177 case Symbol:
178 OS << "Sym:" << Sym.Exp;
179 break;
180 }
181 }
182};
183
184class WebAssemblyAsmParser final : public MCTargetAsmParser {
185 MCAsmParser &Parser;
186 MCAsmLexer &Lexer;
Wouter van Oortmerssena7be3752018-08-13 23:12:49 +0000187 // These are for the current function being parsed:
188 // These are vectors since register assignments are so far non-sparse.
189 // Replace by map if necessary.
190 std::vector<MVT::SimpleValueType> LocalTypes;
191 std::vector<MVT::SimpleValueType> StackTypes;
Derek Schuffe4825972018-03-20 20:06:35 +0000192 MCSymbol *LastLabel;
193
194public:
195 WebAssemblyAsmParser(const MCSubtargetInfo &sti, MCAsmParser &Parser,
196 const MCInstrInfo &mii, const MCTargetOptions &Options)
197 : MCTargetAsmParser(Options, sti, mii), Parser(Parser),
198 Lexer(Parser.getLexer()), LastLabel(nullptr) {
Thomas Lively22442922018-08-21 21:03:18 +0000199 setAvailableFeatures(ComputeAvailableFeatures(sti.getFeatureBits()));
Derek Schuffe4825972018-03-20 20:06:35 +0000200 }
201
202#define GET_ASSEMBLER_HEADER
203#include "WebAssemblyGenAsmMatcher.inc"
204
205 // TODO: This is required to be implemented, but appears unused.
206 bool ParseRegister(unsigned &/*RegNo*/, SMLoc &/*StartLoc*/,
207 SMLoc &/*EndLoc*/) override {
208 llvm_unreachable("ParseRegister is not implemented.");
209 }
210
211 bool Error(const StringRef &msg, const AsmToken &tok) {
212 return Parser.Error(tok.getLoc(), msg + tok.getString());
213 }
214
215 bool IsNext(AsmToken::TokenKind Kind) {
216 auto ok = Lexer.is(Kind);
217 if (ok) Parser.Lex();
218 return ok;
219 }
220
221 bool Expect(AsmToken::TokenKind Kind, const char *KindName) {
222 if (!IsNext(Kind))
223 return Error(std::string("Expected ") + KindName + ", instead got: ",
224 Lexer.getTok());
225 return false;
226 }
227
228 MVT::SimpleValueType ParseRegType(const StringRef &RegType) {
229 // Derive type from .param .local decls, or the instruction itself.
230 return StringSwitch<MVT::SimpleValueType>(RegType)
231 .Case("i32", MVT::i32)
232 .Case("i64", MVT::i64)
233 .Case("f32", MVT::f32)
234 .Case("f64", MVT::f64)
235 .Case("i8x16", MVT::v16i8)
236 .Case("i16x8", MVT::v8i16)
237 .Case("i32x4", MVT::v4i32)
Thomas Lively22442922018-08-21 21:03:18 +0000238 .Case("i64x2", MVT::v2i64)
Derek Schuffe4825972018-03-20 20:06:35 +0000239 .Case("f32x4", MVT::v4f32)
Thomas Lively22442922018-08-21 21:03:18 +0000240 .Case("f64x2", MVT::v2f64)
241 // arbitrarily chosen vector type to associate with "v128"
242 // FIXME: should these be EVTs to avoid this arbitrary hack? Do we want
243 // to accept more specific SIMD register types?
244 .Case("v128", MVT::v16i8)
Derek Schuffe4825972018-03-20 20:06:35 +0000245 .Default(MVT::INVALID_SIMPLE_VALUE_TYPE);
246 }
247
Wouter van Oortmerssena7be3752018-08-13 23:12:49 +0000248 MVT::SimpleValueType &GetType(
249 std::vector<MVT::SimpleValueType> &Types, size_t i) {
250 Types.resize(std::max(i + 1, Types.size()), MVT::INVALID_SIMPLE_VALUE_TYPE);
251 return Types[i];
252 }
253
254 bool ParseReg(OperandVector &Operands, StringRef TypePrefix) {
255 if (Lexer.is(AsmToken::Integer)) {
256 auto &Local = Lexer.getTok();
257 // This is a reference to a local, turn it into a virtual register.
258 auto LocalNo = static_cast<unsigned>(Local.getIntVal());
259 Operands.push_back(make_unique<WebAssemblyOperand>(
260 WebAssemblyOperand::Local, Local.getLoc(),
261 Local.getEndLoc(),
262 WebAssemblyOperand::RegOp{LocalNo,
263 GetType(LocalTypes, LocalNo)}));
264 Parser.Lex();
265 } else if (Lexer.is(AsmToken::Identifier)) {
266 auto &StackRegTok = Lexer.getTok();
267 // These are push/pop/drop pseudo stack registers, which we turn
268 // into virtual registers also. The stackify pass will later turn them
269 // back into implicit stack references if possible.
270 auto StackReg = StackRegTok.getString();
271 auto StackOp = StackReg.take_while([](char c) { return isalpha(c); });
272 auto Reg = StackReg.drop_front(StackOp.size());
273 unsigned long long ParsedRegNo = 0;
274 if (!Reg.empty() && getAsUnsignedInteger(Reg, 10, ParsedRegNo))
275 return Error("Cannot parse stack register index: ", StackRegTok);
276 unsigned RegNo = static_cast<unsigned>(ParsedRegNo);
277 if (StackOp == "push") {
278 // This defines a result, record register type.
279 auto RegType = ParseRegType(TypePrefix);
280 GetType(StackTypes, RegNo) = RegType;
281 Operands.push_back(make_unique<WebAssemblyOperand>(
282 WebAssemblyOperand::Stack,
283 StackRegTok.getLoc(),
284 StackRegTok.getEndLoc(),
285 WebAssemblyOperand::RegOp{RegNo, RegType}));
286 } else if (StackOp == "pop") {
287 // This uses a previously defined stack value.
288 auto RegType = GetType(StackTypes, RegNo);
289 Operands.push_back(make_unique<WebAssemblyOperand>(
290 WebAssemblyOperand::Stack,
291 StackRegTok.getLoc(),
292 StackRegTok.getEndLoc(),
293 WebAssemblyOperand::RegOp{RegNo, RegType}));
294 } else if (StackOp == "drop") {
295 // This operand will be dropped, since it is part of an instruction
296 // whose result is void.
297 } else {
298 return Error("Unknown stack register prefix: ", StackRegTok);
299 }
300 Parser.Lex();
301 } else {
302 return Error(
303 "Expected identifier/integer following $, instead got: ",
304 Lexer.getTok());
305 }
306 IsNext(AsmToken::Equal);
307 return false;
308 }
309
Derek Schuffe4825972018-03-20 20:06:35 +0000310 void ParseSingleInteger(bool IsNegative, OperandVector &Operands) {
311 auto &Int = Lexer.getTok();
312 int64_t Val = Int.getIntVal();
313 if (IsNegative) Val = -Val;
314 Operands.push_back(make_unique<WebAssemblyOperand>(
315 WebAssemblyOperand::Integer, Int.getLoc(),
316 Int.getEndLoc(), WebAssemblyOperand::IntOp{Val}));
317 Parser.Lex();
318 }
319
320 bool ParseOperandStartingWithInteger(bool IsNegative,
321 OperandVector &Operands,
Wouter van Oortmerssena7be3752018-08-13 23:12:49 +0000322 StringRef InstType) {
Derek Schuffe4825972018-03-20 20:06:35 +0000323 ParseSingleInteger(IsNegative, Operands);
Wouter van Oortmerssena7be3752018-08-13 23:12:49 +0000324 if (Lexer.is(AsmToken::LParen)) {
325 // Parse load/store operands of the form: offset($reg)align
326 auto &LParen = Lexer.getTok();
327 Operands.push_back(
328 make_unique<WebAssemblyOperand>(WebAssemblyOperand::Token,
329 LParen.getLoc(),
330 LParen.getEndLoc(),
331 WebAssemblyOperand::TokOp{
332 LParen.getString()}));
333 Parser.Lex();
334 if (Expect(AsmToken::Dollar, "register")) return true;
335 if (ParseReg(Operands, InstType)) return true;
336 auto &RParen = Lexer.getTok();
337 Operands.push_back(
338 make_unique<WebAssemblyOperand>(WebAssemblyOperand::Token,
339 RParen.getLoc(),
340 RParen.getEndLoc(),
341 WebAssemblyOperand::TokOp{
342 RParen.getString()}));
343 if (Expect(AsmToken::RParen, ")")) return true;
344 if (Lexer.is(AsmToken::Integer)) {
Derek Schuffe4825972018-03-20 20:06:35 +0000345 ParseSingleInteger(false, Operands);
346 } else {
347 // Alignment not specified.
348 // FIXME: correctly derive a default from the instruction.
349 Operands.push_back(make_unique<WebAssemblyOperand>(
Wouter van Oortmerssena7be3752018-08-13 23:12:49 +0000350 WebAssemblyOperand::Integer, RParen.getLoc(),
351 RParen.getEndLoc(), WebAssemblyOperand::IntOp{0}));
Derek Schuffe4825972018-03-20 20:06:35 +0000352 }
353 }
354 return false;
355 }
356
357 bool ParseInstruction(ParseInstructionInfo &/*Info*/, StringRef Name,
358 SMLoc NameLoc, OperandVector &Operands) override {
359 Operands.push_back(
360 make_unique<WebAssemblyOperand>(WebAssemblyOperand::Token, NameLoc,
361 SMLoc::getFromPointer(
362 NameLoc.getPointer() + Name.size()),
363 WebAssemblyOperand::TokOp{
364 StringRef(NameLoc.getPointer(),
365 Name.size())}));
366 auto NamePair = Name.split('.');
367 // If no '.', there is no type prefix.
368 if (NamePair.second.empty()) std::swap(NamePair.first, NamePair.second);
369 while (Lexer.isNot(AsmToken::EndOfStatement)) {
370 auto &Tok = Lexer.getTok();
371 switch (Tok.getKind()) {
Wouter van Oortmerssena7be3752018-08-13 23:12:49 +0000372 case AsmToken::Dollar: {
373 Parser.Lex();
374 if (ParseReg(Operands, NamePair.first)) return true;
375 break;
376 }
Derek Schuffe4825972018-03-20 20:06:35 +0000377 case AsmToken::Identifier: {
378 auto &Id = Lexer.getTok();
379 const MCExpr *Val;
380 SMLoc End;
381 if (Parser.parsePrimaryExpr(Val, End))
382 return Error("Cannot parse symbol: ", Lexer.getTok());
383 Operands.push_back(make_unique<WebAssemblyOperand>(
384 WebAssemblyOperand::Symbol, Id.getLoc(),
385 Id.getEndLoc(), WebAssemblyOperand::SymOp{Val}));
386 break;
387 }
388 case AsmToken::Minus:
389 Parser.Lex();
390 if (Lexer.isNot(AsmToken::Integer))
391 return Error("Expected integer instead got: ", Lexer.getTok());
Wouter van Oortmerssena7be3752018-08-13 23:12:49 +0000392 if (ParseOperandStartingWithInteger(true, Operands, NamePair.first))
Derek Schuffe4825972018-03-20 20:06:35 +0000393 return true;
394 break;
395 case AsmToken::Integer:
Wouter van Oortmerssena7be3752018-08-13 23:12:49 +0000396 if (ParseOperandStartingWithInteger(false, Operands, NamePair.first))
Derek Schuffe4825972018-03-20 20:06:35 +0000397 return true;
398 break;
399 case AsmToken::Real: {
400 double Val;
401 if (Tok.getString().getAsDouble(Val, false))
402 return Error("Cannot parse real: ", Tok);
403 Operands.push_back(make_unique<WebAssemblyOperand>(
404 WebAssemblyOperand::Float, Tok.getLoc(),
405 Tok.getEndLoc(), WebAssemblyOperand::FltOp{Val}));
406 Parser.Lex();
407 break;
408 }
409 default:
410 return Error("Unexpected token in operand: ", Tok);
411 }
412 if (Lexer.isNot(AsmToken::EndOfStatement)) {
413 if (Expect(AsmToken::Comma, ",")) return true;
414 }
415 }
416 Parser.Lex();
Wouter van Oortmerssena7be3752018-08-13 23:12:49 +0000417 // Call instructions are vararg, but the tablegen matcher doesn't seem to
418 // support that, so for now we strip these extra operands.
419 // This is problematic if these arguments are not simple $pop stack
420 // registers, since e.g. a local register would get lost, so we check for
421 // this. This can be the case when using -disable-wasm-explicit-locals
422 // which currently s2wasm requires.
423 // TODO: Instead, we can move this code to MatchAndEmitInstruction below and
424 // actually generate get_local instructions on the fly.
425 // Or even better, improve the matcher to support vararg?
426 auto IsIndirect = NamePair.second == "call_indirect";
427 if (IsIndirect || NamePair.second == "call") {
428 // Figure out number of fixed operands from the instruction.
429 size_t CallOperands = 1; // The name token.
430 if (!IsIndirect) CallOperands++; // The function index.
431 if (!NamePair.first.empty()) CallOperands++; // The result register.
432 if (Operands.size() > CallOperands) {
433 // Ensure operands we drop are all $pop.
434 for (size_t I = CallOperands; I < Operands.size(); I++) {
435 auto Operand =
436 reinterpret_cast<WebAssemblyOperand *>(Operands[I].get());
437 if (Operand->Kind != WebAssemblyOperand::Stack)
438 Parser.Error(NameLoc,
439 "Call instruction has non-stack arguments, if this code was "
440 "generated with -disable-wasm-explicit-locals please remove it");
441 }
442 // Drop unneeded operands.
443 Operands.resize(CallOperands);
444 }
445 }
Derek Schuffe4825972018-03-20 20:06:35 +0000446 // Block instructions require a signature index, but these are missing in
447 // assembly, so we add a dummy one explicitly (since we have no control
448 // over signature tables here, we assume these will be regenerated when
449 // the wasm module is generated).
450 if (NamePair.second == "block" || NamePair.second == "loop") {
451 Operands.push_back(make_unique<WebAssemblyOperand>(
452 WebAssemblyOperand::Integer, NameLoc,
453 NameLoc, WebAssemblyOperand::IntOp{-1}));
454 }
Wouter van Oortmerssena7be3752018-08-13 23:12:49 +0000455 // These don't specify the type, which has to derived from the local index.
456 if (NamePair.second == "get_local" || NamePair.second == "tee_local") {
457 if (Operands.size() >= 3 && Operands[1]->isReg() &&
458 Operands[2]->isImm()) {
459 auto Op1 = reinterpret_cast<WebAssemblyOperand *>(Operands[1].get());
460 auto Op2 = reinterpret_cast<WebAssemblyOperand *>(Operands[2].get());
461 auto Type = GetType(LocalTypes, static_cast<size_t>(Op2->Int.Val));
462 Op1->Reg.Type = Type;
463 GetType(StackTypes, Op1->Reg.RegNo) = Type;
464 }
465 }
Derek Schuffe4825972018-03-20 20:06:35 +0000466 return false;
467 }
468
469 void onLabelParsed(MCSymbol *Symbol) override {
470 LastLabel = Symbol;
471 }
472
473 bool ParseDirective(AsmToken DirectiveID) override {
474 assert(DirectiveID.getKind() == AsmToken::Identifier);
475 auto &Out = getStreamer();
476 auto &TOut = reinterpret_cast<WebAssemblyTargetStreamer &>(
477 *Out.getTargetStreamer());
478 // TODO: we're just parsing the subset of directives we're interested in,
479 // and ignoring ones we don't recognise. We should ideally verify
480 // all directives here.
481 if (DirectiveID.getString() == ".type") {
482 // This could be the start of a function, check if followed by
483 // "label,@function"
484 if (!(IsNext(AsmToken::Identifier) &&
485 IsNext(AsmToken::Comma) &&
486 IsNext(AsmToken::At) &&
487 Lexer.is(AsmToken::Identifier)))
488 return Error("Expected label,@type declaration, got: ", Lexer.getTok());
Wouter van Oortmerssena7be3752018-08-13 23:12:49 +0000489 if (Lexer.getTok().getString() == "function") {
490 // Track locals from start of function.
491 LocalTypes.clear();
492 StackTypes.clear();
493 }
Derek Schuffe4825972018-03-20 20:06:35 +0000494 Parser.Lex();
495 //Out.EmitSymbolAttribute(??, MCSA_ELF_TypeFunction);
496 } else if (DirectiveID.getString() == ".param" ||
497 DirectiveID.getString() == ".local") {
498 // Track the number of locals, needed for correct virtual register
499 // assignment elsewhere.
500 // Also output a directive to the streamer.
501 std::vector<MVT> Params;
502 std::vector<MVT> Locals;
503 while (Lexer.is(AsmToken::Identifier)) {
504 auto RegType = ParseRegType(Lexer.getTok().getString());
505 if (RegType == MVT::INVALID_SIMPLE_VALUE_TYPE) return true;
Wouter van Oortmerssena7be3752018-08-13 23:12:49 +0000506 LocalTypes.push_back(RegType);
Derek Schuffe4825972018-03-20 20:06:35 +0000507 if (DirectiveID.getString() == ".param") {
508 Params.push_back(RegType);
509 } else {
510 Locals.push_back(RegType);
511 }
512 Parser.Lex();
513 if (!IsNext(AsmToken::Comma)) break;
514 }
515 assert(LastLabel);
516 TOut.emitParam(LastLabel, Params);
517 TOut.emitLocal(Locals);
518 } else {
519 // For now, ignore anydirective we don't recognize:
520 while (Lexer.isNot(AsmToken::EndOfStatement)) Parser.Lex();
521 }
522 return Expect(AsmToken::EndOfStatement, "EOL");
523 }
524
525 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &/*Opcode*/,
526 OperandVector &Operands,
527 MCStreamer &Out, uint64_t &ErrorInfo,
528 bool MatchingInlineAsm) override {
529 MCInst Inst;
530 unsigned MatchResult =
531 MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
532 switch (MatchResult) {
533 case Match_Success: {
534 Out.EmitInstruction(Inst, getSTI());
535 return false;
536 }
537 case Match_MissingFeature:
538 return Parser.Error(IDLoc,
539 "instruction requires a WASM feature not currently enabled");
540 case Match_MnemonicFail:
541 return Parser.Error(IDLoc, "invalid instruction");
542 case Match_NearMisses:
543 return Parser.Error(IDLoc, "ambiguous instruction");
544 case Match_InvalidTiedOperand:
545 case Match_InvalidOperand: {
546 SMLoc ErrorLoc = IDLoc;
547 if (ErrorInfo != ~0ULL) {
548 if (ErrorInfo >= Operands.size())
549 return Parser.Error(IDLoc, "too few operands for instruction");
550 ErrorLoc = Operands[ErrorInfo]->getStartLoc();
551 if (ErrorLoc == SMLoc())
552 ErrorLoc = IDLoc;
553 }
554 return Parser.Error(ErrorLoc, "invalid operand for instruction");
555 }
556 }
557 llvm_unreachable("Implement any new match types added!");
558 }
559};
560} // end anonymous namespace
561
562// Force static initialization.
563extern "C" void LLVMInitializeWebAssemblyAsmParser() {
564 RegisterMCAsmParser<WebAssemblyAsmParser> X(getTheWebAssemblyTarget32());
565 RegisterMCAsmParser<WebAssemblyAsmParser> Y(getTheWebAssemblyTarget64());
566}
567
568#define GET_REGISTER_MATCHER
569#define GET_MATCHER_IMPLEMENTATION
570#include "WebAssemblyGenAsmMatcher.inc"