Index: lib/MC/MCParser/AsmParser.cpp =================================================================== --- lib/MC/MCParser/AsmParser.cpp +++ lib/MC/MCParser/AsmParser.cpp @@ -675,6 +675,7 @@ extern MCAsmParserExtension *createDarwinAsmParser(); extern MCAsmParserExtension *createELFAsmParser(); extern MCAsmParserExtension *createCOFFAsmParser(); +extern MCAsmParserExtension *createWasmAsmParser(); } // end namespace llvm @@ -705,10 +706,7 @@ PlatformParser.reset(createELFAsmParser()); break; case MCObjectFileInfo::IsWasm: - // TODO: WASM will need its own MCAsmParserExtension implementation, but - // for now we can re-use the ELF one, since the directives can be the - // same for now. - PlatformParser.reset(createELFAsmParser()); + PlatformParser.reset(createWasmAsmParser()); break; } @@ -3921,7 +3919,7 @@ parseToken(AsmToken::EndOfStatement)) return addErrorSuffix(" in '.cfi_startproc' directive"); } - + // TODO(kristina): Deal with a corner case of incorrect diagnostic context // being produced if this directive is emitted as part of preprocessor macro // expansion which can *ONLY* happen if Clang's cc1as is the API consumer. Index: lib/MC/MCParser/CMakeLists.txt =================================================================== --- lib/MC/MCParser/CMakeLists.txt +++ lib/MC/MCParser/CMakeLists.txt @@ -8,6 +8,7 @@ MCAsmParser.cpp MCAsmParserExtension.cpp MCTargetAsmParser.cpp + WasmAsmParser.cpp ADDITIONAL_HEADER_DIRS ${LLVM_MAIN_INCLUDE_DIR}/llvm/MC/MCParser Index: lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp =================================================================== --- lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp +++ lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp @@ -132,13 +132,12 @@ class WebAssemblyAsmParser final : public MCTargetAsmParser { MCAsmParser &Parser; MCAsmLexer &Lexer; - MCSymbolWasm *LastSymbol; public: WebAssemblyAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser, const MCInstrInfo &MII, const MCTargetOptions &Options) : MCTargetAsmParser(Options, STI, MII), Parser(Parser), - Lexer(Parser.getLexer()), LastSymbol(nullptr) { + Lexer(Parser.getLexer()) { setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits())); } @@ -191,6 +190,19 @@ .Default({MVT::INVALID_SIMPLE_VALUE_TYPE, wasm::WASM_TYPE_NORESULT}); } + bool ParseRegTypeList(std::vector &Types) { + while (Lexer.is(AsmToken::Identifier)) { + auto RegType = ParseRegType(Lexer.getTok().getString()).first; + if (RegType == MVT::INVALID_SIMPLE_VALUE_TYPE) + return true; + Types.push_back(RegType); + Parser.Lex(); + if (!IsNext(AsmToken::Comma)) + break; + } + return Expect(AsmToken::EndOfStatement, "EOL"); + } + void ParseSingleInteger(bool IsNegative, OperandVector &Operands) { auto &Int = Lexer.getTok(); int64_t Val = Int.getIntVal(); @@ -315,9 +327,11 @@ } void onLabelParsed(MCSymbol *Symbol) override { - LastSymbol = cast(Symbol); } + // This function processes wasm-specific directives streamed to + // WebAssemblyTargetStreamer, all others go to the generic parser + // (see WasmAsmParser). bool ParseDirective(AsmToken DirectiveID) override { // This function has a really weird return value behavior that is different // from all the other parsing functions: @@ -331,44 +345,7 @@ reinterpret_cast(*Out.getTargetStreamer()); // TODO: any time we return an error, at least one token must have been // consumed, otherwise this will not signal an error to the caller. - if (DirectiveID.getString() == ".type") { - // This could be the start of a function, check if followed by - // "label,@function" - if (!Lexer.is(AsmToken::Identifier)) - return Error("Expected label after .type directive, got: ", - Lexer.getTok()); - auto WasmSym = cast( - TOut.getStreamer().getContext().getOrCreateSymbol( - Lexer.getTok().getString())); - Parser.Lex(); - if (!(IsNext(AsmToken::Comma) && IsNext(AsmToken::At) && - Lexer.is(AsmToken::Identifier))) - return Error("Expected label,@type declaration, got: ", Lexer.getTok()); - auto TypeName = Lexer.getTok().getString(); - if (TypeName == "function") - WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); - else if (TypeName == "global") - WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL); - else - return Error("Unknown WASM symbol type: ", Lexer.getTok()); - Parser.Lex(); - return Expect(AsmToken::EndOfStatement, "EOL"); - } else if (DirectiveID.getString() == ".size") { - if (!Lexer.is(AsmToken::Identifier)) - return Error("Expected label after .size directive, got: ", - Lexer.getTok()); - auto WasmSym = cast( - TOut.getStreamer().getContext().getOrCreateSymbol( - Lexer.getTok().getString())); - Parser.Lex(); - if (!IsNext(AsmToken::Comma)) - return Error("Expected `,`, got: ", Lexer.getTok()); - const MCExpr *Exp; - if (Parser.parseExpression(Exp)) - return Error("Cannot parse .size expression: ", Lexer.getTok()); - WasmSym->setSize(Exp); - return Expect(AsmToken::EndOfStatement, "EOL"); - } else if (DirectiveID.getString() == ".globaltype") { + if (DirectiveID.getString() == ".globaltype") { if (!Lexer.is(AsmToken::Identifier)) return Error("Expected symbol name after .globaltype directive, got: ", Lexer.getTok()); @@ -392,40 +369,23 @@ // And emit the directive again. TOut.emitGlobalType(WasmSym); return Expect(AsmToken::EndOfStatement, "EOL"); - } else if (DirectiveID.getString() == ".param" || - DirectiveID.getString() == ".local") { - // Track the number of locals, needed for correct virtual register - // assignment elsewhere. - // Also output a directive to the streamer. + } else if (DirectiveID.getString() == ".param") { std::vector Params; + if (ParseRegTypeList(Params)) return true; + TOut.emitParam(nullptr /* unused */, Params); + return false; + } else if (DirectiveID.getString() == ".result") { + std::vector Results; + if (ParseRegTypeList(Results)) return true; + TOut.emitResult(nullptr /* unused */, Results); + return false; + } else if (DirectiveID.getString() == ".local") { std::vector Locals; - while (Lexer.is(AsmToken::Identifier)) { - auto RegType = ParseRegType(Lexer.getTok().getString()).first; - if (RegType == MVT::INVALID_SIMPLE_VALUE_TYPE) - return true; - if (DirectiveID.getString() == ".param") { - Params.push_back(RegType); - } else { - Locals.push_back(RegType); - } - Parser.Lex(); - if (!IsNext(AsmToken::Comma)) - break; - } - assert(LastSymbol); - // TODO: LastSymbol isn't even used by emitParam, so could be removed. - TOut.emitParam(LastSymbol, Params); + if (ParseRegTypeList(Locals)) return true; TOut.emitLocal(Locals); - return Expect(AsmToken::EndOfStatement, "EOL"); - } else { - // TODO: remove. - while (Lexer.isNot(AsmToken::EndOfStatement)) - Parser.Lex(); - return Expect(AsmToken::EndOfStatement, "EOL"); + return false; } - // TODO: current ELF directive parsing is broken, fix this is a followup. - //return true; // We didn't process this directive. - return false; + return true; // We didn't process this directive. } bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned & /*Opcode*/, Index: test/CodeGen/WebAssembly/inline-asm-roundtrip.ll =================================================================== --- test/CodeGen/WebAssembly/inline-asm-roundtrip.ll +++ test/CodeGen/WebAssembly/inline-asm-roundtrip.ll @@ -22,6 +22,7 @@ ; CHECK-LABEL: main: ; CHECK-NEXT: .param i32, i32 +; CHECK-NEXT: .result i32 ; CHECK-NEXT: .local i32 ; CHECK-NEXT: i32.const 1 ; CHECK-NEXT: set_local [[SRC:[0-9]+]] Index: test/MC/WebAssembly/basic-assembly.s =================================================================== --- test/MC/WebAssembly/basic-assembly.s +++ test/MC/WebAssembly/basic-assembly.s @@ -3,10 +3,12 @@ # RUN: llvm-mc -triple=wasm32-unknown-unknown -filetype=obj -mattr=+simd128,+nontrapping-fptoint,+exception-handling < %s .text + .section .text.main,"",@ .type test0,@function test0: # Test all types: .param i32, i64 + .result i32 .local f32, f64, v128, v128 # Explicit getlocal/setlocal: get_local 2 @@ -65,6 +67,7 @@ # CHECK: .text # CHECK-LABEL: test0: # CHECK-NEXT: .param i32, i64 +# CHECK-NEXT: .result i32 # CHECK-NEXT: .local f32, f64 # CHECK-NEXT: get_local 2 # CHECK-NEXT: set_local 2