1 //==- 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 11 /// This file is part of the WebAssembly Assembler. 12 /// 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/MCInst.h" 22 #include "llvm/MC/MCInstrInfo.h" 23 #include "llvm/MC/MCParser/MCParsedAsmOperand.h" 24 #include "llvm/MC/MCParser/MCTargetAsmParser.h" 25 #include "llvm/MC/MCStreamer.h" 26 #include "llvm/MC/MCSubtargetInfo.h" 27 #include "llvm/MC/MCSymbol.h" 28 #include "llvm/MC/MCSymbolWasm.h" 29 #include "llvm/Support/Endian.h" 30 #include "llvm/Support/TargetRegistry.h" 31 32 using namespace llvm; 33 34 #define DEBUG_TYPE "wasm-asm-parser" 35 36 namespace { 37 38 /// WebAssemblyOperand - Instances of this class represent the operands in a 39 /// parsed WASM machine instruction. 40 struct WebAssemblyOperand : public MCParsedAsmOperand { 41 enum KindTy { Token, Integer, Float, Symbol } Kind; 42 43 SMLoc StartLoc, EndLoc; 44 45 struct TokOp { 46 StringRef Tok; 47 }; 48 49 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; 63 struct IntOp Int; 64 struct FltOp Flt; 65 struct SymOp Sym; 66 }; 67 68 WebAssemblyOperand(KindTy K, SMLoc Start, SMLoc End, TokOp T) 69 : Kind(K), StartLoc(Start), EndLoc(End), Tok(T) {} 70 WebAssemblyOperand(KindTy K, SMLoc Start, SMLoc End, IntOp I) 71 : Kind(K), StartLoc(Start), EndLoc(End), Int(I) {} 72 WebAssemblyOperand(KindTy K, SMLoc Start, SMLoc End, FltOp F) 73 : Kind(K), StartLoc(Start), EndLoc(End), Flt(F) {} 74 WebAssemblyOperand(KindTy K, SMLoc Start, SMLoc End, SymOp S) 75 : Kind(K), StartLoc(Start), EndLoc(End), Sym(S) {} 76 77 bool isToken() const override { return Kind == Token; } 78 bool isImm() const override { 79 return Kind == Integer || Kind == Float || Kind == Symbol; 80 } 81 bool isMem() const override { return false; } 82 bool isReg() const override { return false; } 83 84 unsigned getReg() const override { 85 llvm_unreachable("Assembly inspects a register operand"); 86 return 0; 87 } 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 97 void addRegOperands(MCInst &, unsigned) const { 98 // Required by the assembly matcher. 99 llvm_unreachable("Assembly matcher creates register operands"); 100 } 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; 119 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 132 class WebAssemblyAsmParser final : public MCTargetAsmParser { 133 MCAsmParser &Parser; 134 MCAsmLexer &Lexer; 135 136 // Much like WebAssemblyAsmPrinter in the backend, we have to own these. 137 std::vector<std::unique_ptr<wasm::WasmSignature>> Signatures; 138 139 // 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 157 public: 158 WebAssemblyAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser, 159 const MCInstrInfo &MII, const MCTargetOptions &Options) 160 : MCTargetAsmParser(Options, STI, MII), Parser(Parser), 161 Lexer(Parser.getLexer()) { 162 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits())); 163 } 164 165 void addSignature(std::unique_ptr<wasm::WasmSignature> &&Sig) { 166 Signatures.push_back(std::move(Sig)); 167 } 168 169 #define GET_ASSEMBLER_HEADER 170 #include "WebAssemblyGenAsmMatcher.inc" 171 172 // TODO: This is required to be implemented, but appears unused. 173 bool ParseRegister(unsigned & /*RegNo*/, SMLoc & /*StartLoc*/, 174 SMLoc & /*EndLoc*/) override { 175 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); 184 if (ok) 185 Parser.Lex(); 186 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 196 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; 204 } 205 206 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) { 220 while (Lexer.is(AsmToken::Identifier)) { 221 auto Type = ParseType(Lexer.getTok().getString()); 222 if (!Type) 223 return true; 224 Types.push_back(Type.getValue()); 225 Parser.Lex(); 226 if (!IsNext(AsmToken::Comma)) 227 break; 228 } 229 return false; 230 } 231 232 void ParseSingleInteger(bool IsNegative, OperandVector &Operands) { 233 auto &Int = Lexer.getTok(); 234 int64_t Val = Int.getIntVal(); 235 if (IsNegative) 236 Val = -Val; 237 Operands.push_back(make_unique<WebAssemblyOperand>( 238 WebAssemblyOperand::Integer, Int.getLoc(), Int.getEndLoc(), 239 WebAssemblyOperand::IntOp{Val})); 240 Parser.Lex(); 241 } 242 243 bool ParseOperandStartingWithInteger(bool IsNegative, OperandVector &Operands, 244 StringRef InstName) { 245 ParseSingleInteger(IsNegative, Operands); 246 // 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)) { 255 ParseSingleInteger(false, Operands); 256 } else { 257 // Alignment not specified. 258 // FIXME: correctly derive a default from the instruction. 259 // We can't just call WebAssembly::GetDefaultP2Align since we don't have 260 // an opcode until after the assembly matcher. 261 Operands.push_back(make_unique<WebAssemblyOperand>( 262 WebAssemblyOperand::Integer, Offset.getLoc(), Offset.getEndLoc(), 263 WebAssemblyOperand::IntOp{0})); 264 } 265 } 266 return false; 267 } 268 269 bool ParseInstruction(ParseInstructionInfo & /*Info*/, StringRef Name, 270 SMLoc NameLoc, OperandVector &Operands) override { 271 // 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. 293 Operands.push_back(make_unique<WebAssemblyOperand>( 294 WebAssemblyOperand::Token, NameLoc, SMLoc::getFromPointer(Name.end()), 295 WebAssemblyOperand::TokOp{Name})); 296 auto NamePair = Name.split('.'); 297 // If no '.', there is no type prefix. 298 auto BaseName = NamePair.second.empty() ? NamePair.first : NamePair.second; 299 while (Lexer.isNot(AsmToken::EndOfStatement)) { 300 auto &Tok = Lexer.getTok(); 301 switch (Tok.getKind()) { 302 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>( 309 WebAssemblyOperand::Symbol, Id.getLoc(), Id.getEndLoc(), 310 WebAssemblyOperand::SymOp{Val})); 311 break; 312 } 313 case AsmToken::Minus: 314 Parser.Lex(); 315 if (Lexer.isNot(AsmToken::Integer)) 316 return Error("Expected integer instead got: ", Lexer.getTok()); 317 if (ParseOperandStartingWithInteger(true, Operands, BaseName)) 318 return true; 319 break; 320 case AsmToken::Integer: 321 if (ParseOperandStartingWithInteger(false, Operands, BaseName)) 322 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>( 329 WebAssemblyOperand::Float, Tok.getLoc(), Tok.getEndLoc(), 330 WebAssemblyOperand::FltOp{Val})); 331 Parser.Lex(); 332 break; 333 } 334 default: 335 return Error("Unexpected token in operand: ", Tok); 336 } 337 if (Lexer.isNot(AsmToken::EndOfStatement)) { 338 if (Expect(AsmToken::Comma, ",")) 339 return true; 340 } 341 } 342 Parser.Lex(); 343 // 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). 347 if (BaseName == "block" || BaseName == "loop" || BaseName == "try") { 348 Operands.push_back(make_unique<WebAssemblyOperand>( 349 WebAssemblyOperand::Integer, NameLoc, NameLoc, 350 WebAssemblyOperand::IntOp{-1})); 351 } 352 return false; 353 } 354 355 void onLabelParsed(MCSymbol *Symbol) override { 356 LastLabel = Symbol; 357 CurrentState = Label; 358 } 359 360 // This function processes wasm-specific directives streamed to 361 // WebAssemblyTargetStreamer, all others go to the generic parser 362 // (see WasmAsmParser). 363 bool ParseDirective(AsmToken DirectiveID) override { 364 // 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. 370 assert(DirectiveID.getKind() == AsmToken::Identifier); 371 auto &Out = getStreamer(); 372 auto &TOut = 373 reinterpret_cast<WebAssemblyTargetStreamer &>(*Out.getTargetStreamer()); 374 // 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. 376 if (DirectiveID.getString() == ".globaltype") { 377 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); 386 // Now set this symbol with the correct type. 387 auto WasmSym = cast<MCSymbolWasm>( 388 TOut.getStreamer().getContext().getOrCreateSymbol(SymName)); 389 WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL); 390 WasmSym->setGlobalType( 391 wasm::WasmGlobalType{uint8_t(Type.getValue()), true}); 392 // And emit the directive again. 393 TOut.emitGlobalType(WasmSym); 394 return Expect(AsmToken::EndOfStatement, "EOL"); 395 } else if (DirectiveID.getString() == ".functype") { 396 // 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. 401 auto SymName = ExpectIdent(); 402 if (SymName.empty()) return true; 403 auto WasmSym = cast<MCSymbolWasm>( 404 TOut.getStreamer().getContext().getOrCreateSymbol(SymName)); 405 if (CurrentState == Label && WasmSym == LastLabel) { 406 // This .functype indicates a start of a function. 407 CurrentState = FunctionStart; 408 } 409 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); 421 // TODO: backend also calls TOut.emitIndIdx, but that is not implemented. 422 return Expect(AsmToken::EndOfStatement, "EOL"); 423 } else if (DirectiveID.getString() == ".local") { 424 if (CurrentState != FunctionStart) 425 return Error(".local directive should follow the start of a function", 426 Lexer.getTok()); 427 SmallVector<wasm::ValType, 4> Locals; 428 if (ParseRegTypeList(Locals)) return true; 429 TOut.emitLocal(Locals); 430 CurrentState = FunctionLocals; 431 return Expect(AsmToken::EndOfStatement, "EOL"); 432 } 433 return true; // We didn't process this directive. 434 } 435 436 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned & /*Opcode*/, 437 OperandVector &Operands, MCStreamer &Out, 438 uint64_t &ErrorInfo, 439 bool MatchingInlineAsm) override { 440 MCInst Inst; 441 unsigned MatchResult = 442 MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm); 443 switch (MatchResult) { 444 case Match_Success: { 445 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; 455 Out.EmitInstruction(Inst, getSTI()); 456 return false; 457 } 458 case Match_MissingFeature: 459 return Parser.Error( 460 IDLoc, "instruction requires a WASM feature not currently enabled"); 461 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. 484 extern "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" 492