33 #define DEBUG_TYPE "wasm-asm-parser" 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;
59 SMLoc StartLoc, EndLoc;
98 WebAssemblyOperand(KindTy K,
SMLoc Start,
SMLoc End, TokOp
T)
99 :
Kind(K), StartLoc(Start), EndLoc(End), Tok(T) {}
100 WebAssemblyOperand(KindTy K,
SMLoc Start,
SMLoc End, RegOp
R)
101 :
Kind(K), StartLoc(Start), EndLoc(End),
Reg(R) {}
102 WebAssemblyOperand(KindTy K,
SMLoc Start,
SMLoc End, IntOp
I)
103 :
Kind(K), StartLoc(Start), EndLoc(End),
Int(I) {}
104 WebAssemblyOperand(KindTy K,
SMLoc Start,
SMLoc End, FltOp
F)
105 :
Kind(K), StartLoc(Start), EndLoc(End), Flt(F) {}
106 WebAssemblyOperand(KindTy K,
SMLoc Start,
SMLoc End, SymOp S)
107 :
Kind(K), StartLoc(Start), EndLoc(End), Sym(S) {}
109 bool isToken()
const override {
return Kind == Token; }
110 bool isImm()
const override {
return Kind ==
Integer ||
113 bool isReg()
const override {
return Kind == Local || Kind == Stack; }
114 bool isMem()
const override {
return false; }
116 unsigned getReg()
const override {
120 return MVTToWasmReg(
Reg.Type);
128 SMLoc getStartLoc()
const override {
return StartLoc; }
129 SMLoc getEndLoc()
const override {
return EndLoc; }
131 void addRegOperands(
MCInst &Inst,
unsigned N)
const {
132 assert(N == 1 &&
"Invalid number of operands!");
136 unsigned R =
Reg.RegNo;
146 void addImmOperands(
MCInst &Inst,
unsigned N)
const {
147 assert(N == 1 &&
"Invalid number of operands!");
150 else if (Kind ==
Float)
161 OS <<
"Tok:" << Tok.Tok;
164 OS <<
"Loc:" <<
Reg.RegNo <<
":" <<
static_cast<int>(
Reg.Type);
167 OS <<
"Stk:" <<
Reg.RegNo <<
":" <<
static_cast<int>(
Reg.Type);
170 OS <<
"Int:" <<
Int.Val;
173 OS <<
"Flt:" << Flt.Val;
176 OS <<
"Sym:" << Sym.Exp;
188 std::vector<MVT::SimpleValueType> LocalTypes;
196 Lexer(Parser.
getLexer()), LastLabel(
nullptr) {
199 #define GET_ASSEMBLER_HEADER 200 #include "WebAssemblyGenAsmMatcher.inc" 203 bool ParseRegister(
unsigned &,
SMLoc &,
213 auto ok = Lexer.
is(Kind);
214 if (ok) Parser.
Lex();
220 return Error(std::string(
"Expected ") + KindName +
", instead got: ",
240 std::vector<MVT::SimpleValueType> &Types,
size_t i) {
247 auto &Local = Lexer.
getTok();
249 auto LocalNo =
static_cast<unsigned>(Local.getIntVal());
250 Operands.
push_back(make_unique<WebAssemblyOperand>(
251 WebAssemblyOperand::Local, Local.getLoc(),
253 WebAssemblyOperand::RegOp{LocalNo,
254 GetType(LocalTypes, LocalNo)}));
257 auto &StackRegTok = Lexer.
getTok();
262 auto StackOp = StackReg.
take_while([](
char c) {
return isalpha(c); });
263 auto Reg = StackReg.drop_front(StackOp.size());
264 unsigned long long ParsedRegNo = 0;
266 return Error(
"Cannot parse stack register index: ", StackRegTok);
267 unsigned RegNo =
static_cast<unsigned>(ParsedRegNo);
268 if (StackOp ==
"push") {
270 auto RegType = ParseRegType(TypePrefix);
271 GetType(StackTypes, RegNo) = RegType;
272 Operands.
push_back(make_unique<WebAssemblyOperand>(
273 WebAssemblyOperand::Stack,
274 StackRegTok.getLoc(),
275 StackRegTok.getEndLoc(),
276 WebAssemblyOperand::RegOp{RegNo, RegType}));
277 }
else if (StackOp ==
"pop") {
279 auto RegType = GetType(StackTypes, RegNo);
280 Operands.
push_back(make_unique<WebAssemblyOperand>(
281 WebAssemblyOperand::Stack,
282 StackRegTok.getLoc(),
283 StackRegTok.getEndLoc(),
284 WebAssemblyOperand::RegOp{RegNo, RegType}));
285 }
else if (StackOp ==
"drop") {
289 return Error(
"Unknown stack register prefix: ", StackRegTok);
294 "Expected identifier/integer following $, instead got: ",
301 void ParseSingleInteger(
bool IsNegative,
OperandVector &Operands) {
302 auto &Int = Lexer.
getTok();
303 int64_t Val = Int.getIntVal();
304 if (IsNegative) Val = -Val;
305 Operands.
push_back(make_unique<WebAssemblyOperand>(
307 Int.getEndLoc(), WebAssemblyOperand::IntOp{Val}));
311 bool ParseOperandStartingWithInteger(
bool IsNegative,
314 ParseSingleInteger(IsNegative, Operands);
317 auto &LParen = Lexer.
getTok();
319 make_unique<WebAssemblyOperand>(WebAssemblyOperand::Token,
322 WebAssemblyOperand::TokOp{
323 LParen.getString()}));
326 if (ParseReg(Operands, InstType))
return true;
327 auto &RParen = Lexer.
getTok();
329 make_unique<WebAssemblyOperand>(WebAssemblyOperand::Token,
332 WebAssemblyOperand::TokOp{
333 RParen.getString()}));
336 ParseSingleInteger(
false, Operands);
340 Operands.
push_back(make_unique<WebAssemblyOperand>(
342 RParen.getEndLoc(), WebAssemblyOperand::IntOp{0}));
351 make_unique<WebAssemblyOperand>(WebAssemblyOperand::Token, NameLoc,
354 WebAssemblyOperand::TokOp{
357 auto NamePair = Name.
split(
'.');
359 if (NamePair.second.empty())
std::swap(NamePair.first, NamePair.second);
361 auto &Tok = Lexer.
getTok();
362 switch (Tok.getKind()) {
365 if (ParseReg(Operands, NamePair.first))
return true;
373 return Error(
"Cannot parse symbol: ", Lexer.
getTok());
374 Operands.
push_back(make_unique<WebAssemblyOperand>(
376 Id.getEndLoc(), WebAssemblyOperand::SymOp{Val}));
382 return Error(
"Expected integer instead got: ", Lexer.
getTok());
383 if (ParseOperandStartingWithInteger(
true, Operands, NamePair.first))
387 if (ParseOperandStartingWithInteger(
false, Operands, NamePair.first))
392 if (Tok.getString().getAsDouble(Val,
false))
393 return Error(
"Cannot parse real: ", Tok);
394 Operands.
push_back(make_unique<WebAssemblyOperand>(
395 WebAssemblyOperand::Float, Tok.getLoc(),
396 Tok.getEndLoc(), WebAssemblyOperand::FltOp{Val}));
401 return Error(
"Unexpected token in operand: ", Tok);
417 auto IsIndirect = NamePair.second ==
"call_indirect";
418 if (IsIndirect || NamePair.second ==
"call") {
420 size_t CallOperands = 1;
421 if (!IsIndirect) CallOperands++;
422 if (!NamePair.first.empty()) CallOperands++;
423 if (Operands.
size() > CallOperands) {
425 for (
size_t I = CallOperands;
I < Operands.
size();
I++) {
427 reinterpret_cast<WebAssemblyOperand *
>(Operands[
I].get());
428 if (Operand->Kind != WebAssemblyOperand::Stack)
429 Parser.
Error(NameLoc,
430 "Call instruction has non-stack arguments, if this code was " 431 "generated with -disable-wasm-explicit-locals please remove it");
434 Operands.
resize(CallOperands);
441 if (NamePair.second ==
"block" || NamePair.second ==
"loop") {
442 Operands.
push_back(make_unique<WebAssemblyOperand>(
444 NameLoc, WebAssemblyOperand::IntOp{-1}));
447 if (NamePair.second ==
"get_local" || NamePair.second ==
"tee_local") {
448 if (Operands.
size() >= 3 && Operands[1]->isReg() &&
449 Operands[2]->isImm()) {
450 auto Op1 =
reinterpret_cast<WebAssemblyOperand *
>(Operands[1].get());
451 auto Op2 =
reinterpret_cast<WebAssemblyOperand *
>(Operands[2].get());
452 auto Type = GetType(LocalTypes, static_cast<size_t>(Op2->Int.Val));
453 Op1->Reg.Type =
Type;
454 GetType(StackTypes, Op1->Reg.RegNo) =
Type;
464 bool ParseDirective(
AsmToken DirectiveID)
override {
466 auto &Out = getStreamer();
468 *Out.getTargetStreamer());
472 if (DirectiveID.
getString() ==
".type") {
479 return Error(
"Expected label,@type declaration, got: ", Lexer.
getTok());
487 }
else if (DirectiveID.
getString() ==
".param" ||
492 std::vector<MVT> Params;
493 std::vector<MVT> Locals;
497 LocalTypes.push_back(RegType);
498 if (DirectiveID.
getString() ==
".param") {
499 Params.push_back(RegType);
501 Locals.push_back(RegType);
507 TOut.emitParam(LastLabel, Params);
508 TOut.emitLocal(Locals);
516 bool MatchAndEmitInstruction(
SMLoc IDLoc,
unsigned &,
519 bool MatchingInlineAsm)
override {
521 unsigned MatchResult =
522 MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
523 switch (MatchResult) {
524 case Match_Success: {
528 case Match_MissingFeature:
529 return Parser.
Error(IDLoc,
530 "instruction requires a WASM feature not currently enabled");
531 case Match_MnemonicFail:
532 return Parser.
Error(IDLoc,
"invalid instruction");
533 case Match_NearMisses:
534 return Parser.
Error(IDLoc,
"ambiguous instruction");
535 case Match_InvalidTiedOperand:
536 case Match_InvalidOperand: {
537 SMLoc ErrorLoc = IDLoc;
538 if (ErrorInfo != ~0ULL) {
539 if (ErrorInfo >= Operands.
size())
540 return Parser.
Error(IDLoc,
"too few operands for instruction");
541 ErrorLoc = Operands[ErrorInfo]->getStartLoc();
542 if (ErrorLoc ==
SMLoc())
545 return Parser.
Error(ErrorLoc,
"invalid operand for instruction");
559 #define GET_REGISTER_MATCHER 560 #define GET_MATCHER_IMPLEMENTATION 561 #include "WebAssemblyGenAsmMatcher.inc" static bool isReg(const MCInst &MI, unsigned OpNo)
void push_back(const T &Elt)
const AsmToken & getTok() const
Get the current (last) lexed token.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
StringRef getString() const
Get the string for the current token, this includes all characters (for example, the quotes on string...
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
Compute iterated dominance frontiers using a linear time algorithm.
bool is(AsmToken::TokenKind K) const
Check if the current token has kind K.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Generic assembler parser interface, for use by target specific assembly parsers.
static MCOperand createExpr(const MCExpr *Val)
MCTargetAsmParser - Generic interface to target specific assembly parsers.
bool getAsUnsignedInteger(StringRef Str, unsigned Radix, unsigned long long &Result)
Helper functions for StringRef::getAsInteger.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
virtual const AsmToken & Lex()=0
Get the next AsmToken in the stream, possibly handling file inclusion first.
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end...
virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI, bool PrintSchedInfo=false)
Emit the given Instruction into the current section.
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
static MCOperand createReg(unsigned Reg)
Generic assembler lexer interface, for use by target specific assembly lexers.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Base class for the full range of assembler expressions which are needed for parsing.
Target independent representation for an assembler token.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE R Default(T Value)
static bool isMem(const MachineInstr &MI, unsigned Op)
MCParsedAsmOperand - This abstract class represents a source-level assembly instruction operand...
std::pair< StringRef, StringRef > getToken(StringRef Source, StringRef Delimiters=" \\\)
getToken - This function extracts one token from source, ignoring any leading characters that appear ...
RegisterMCAsmParser - Helper template for registering a target specific assembly parser, for use in the target machine initialization function.
Instances of this class represent a single low-level machine instruction.
WebAssembly-specific streamer interface, to implement support WebAssembly-specific assembly directive...
const char * getPointer() const
A switch()-like statement whose cases are string literals.
Streaming machine code generation interface.
The instances of the Type class are immutable: once they are created, they are never changed...
This file provides WebAssembly-specific target descriptions.
virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc)=0
Parse a primary expression.
Interface to description of machine instruction set.
virtual MCAsmLexer & getLexer()=0
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
bool Error(SMLoc L, const Twine &Msg, SMRange Range=None)
Return an error at the location L, with the message Msg.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This file declares WebAssembly-specific target streamer classes.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef take_while(function_ref< bool(char)> F) const
Return the longest prefix of 'this' such that every character in the prefix satisfies the given predi...
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
static MCOperand createFPImm(double Val)
void LLVMInitializeWebAssemblyAsmParser()
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
static unsigned getReg(const void *D, unsigned RC, unsigned RegNo)
Base class for user error types.
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch & Case(StringLiteral S, T Value)
static SMLoc getFromPointer(const char *Ptr)
Generic base class for all target subtargets.
bool isNot(AsmToken::TokenKind K) const
Check if the current token has kind K.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class implements an extremely fast bulk output stream that can only output to a stream...
void addOperand(const MCOperand &Op)
StringRef - Represent a constant reference to a string, i.e.
Target & getTheWebAssemblyTarget32()
Represents a location in source code.
Target & getTheWebAssemblyTarget64()
static MCOperand createImm(int64_t Val)
TokenKind getKind() const