Index: llvm/include/llvm/MC/MCParser/AsmLexer.h =================================================================== --- llvm/include/llvm/MC/MCParser/AsmLexer.h +++ llvm/include/llvm/MC/MCParser/AsmLexer.h @@ -30,7 +30,6 @@ StringRef CurBuf; bool IsAtStartOfLine = true; bool IsAtStartOfStatement = true; - bool IsParsingMSInlineAsm = false; bool IsPeeking = false; protected: @@ -44,7 +43,6 @@ ~AsmLexer() override; void setBuffer(StringRef Buf, const char *ptr = nullptr); - void setParsingMSInlineAsm(bool V) { IsParsingMSInlineAsm = V; } StringRef LexUntilEndOfStatement() override; Index: llvm/include/llvm/MC/MCParser/MCAsmLexer.h =================================================================== --- llvm/include/llvm/MC/MCParser/MCAsmLexer.h +++ llvm/include/llvm/MC/MCParser/MCAsmLexer.h @@ -50,6 +50,7 @@ bool SkipSpace = true; bool AllowAtInIdentifier; bool IsAtStartOfStatement = true; + bool LexMasmIntegers = false; AsmCommentConsumer *CommentConsumer = nullptr; MCAsmLexer(); @@ -146,6 +147,10 @@ void setCommentConsumer(AsmCommentConsumer *CommentConsumer) { this->CommentConsumer = CommentConsumer; } + + /// Set whether to lex masm-style binary and hex literals. They look like + /// 0b1101 and 0ABCh respectively. + void setLexMasmIntegers(bool V) { LexMasmIntegers = V; } }; } // end namespace llvm Index: llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp =================================================================== --- llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp +++ llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp @@ -156,9 +156,10 @@ Parser->setAssemblerDialect(Dialect); Parser->setTargetParser(*TAP.get()); Parser->setEnablePrintSchedInfo(EnablePrintSchedInfo); + // Enable lexing Masm binary and hex integer literals in intel inline + // assembly. if (Dialect == InlineAsm::AD_Intel) - // We need this flag to be able to parse numbers like "0bH" - Parser->setParsingInlineAsm(true); + Parser->getLexer().setLexMasmIntegers(true); if (MF) { const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); TAP->SetFrameRegister(TRI->getFrameRegister(*MF)); Index: llvm/lib/MC/MCParser/AsmLexer.cpp =================================================================== --- llvm/lib/MC/MCParser/AsmLexer.cpp +++ llvm/lib/MC/MCParser/AsmLexer.cpp @@ -281,7 +281,7 @@ AsmToken AsmLexer::LexDigit() { // MASM-flavor binary integer: [01]+[bB] // MASM-flavor hexadecimal integer: [0-9][0-9a-fA-F]*[hH] - if (IsParsingMSInlineAsm && isdigit(CurPtr[-1])) { + if (LexMasmIntegers && isdigit(CurPtr[-1])) { const char *FirstNonBinary = (CurPtr[-1] != '0' && CurPtr[-1] != '1') ? CurPtr - 1 : nullptr; const char *OldCurPtr = CurPtr; @@ -346,7 +346,7 @@ return intToken(Result, Value); } - if (!IsParsingMSInlineAsm && ((*CurPtr == 'b') || (*CurPtr == 'B'))) { + if (!LexMasmIntegers && ((*CurPtr == 'b') || (*CurPtr == 'B'))) { ++CurPtr; // See if we actually have "0b" as part of something like "jmp 0b\n" if (!isDigit(CurPtr[0])) { @@ -395,7 +395,7 @@ return ReturnError(TokStart, "invalid hexadecimal number"); // Consume the optional [hH]. - if (!IsParsingMSInlineAsm && (*CurPtr == 'h' || *CurPtr == 'H')) + if (!LexMasmIntegers && (*CurPtr == 'h' || *CurPtr == 'H')) ++CurPtr; // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL Index: llvm/lib/MC/MCParser/AsmParser.cpp =================================================================== --- llvm/lib/MC/MCParser/AsmParser.cpp +++ llvm/lib/MC/MCParser/AsmParser.cpp @@ -229,7 +229,9 @@ void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; - Lexer.setParsingMSInlineAsm(V); + // When parsing MS inline asm, we must lex 0b1101 and 0ABCH as binary and + // hex integer literals. + Lexer.setLexMasmIntegers(V); } bool isParsingInlineAsm() override { return ParsingInlineAsm; } Index: llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp =================================================================== --- llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp +++ llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp @@ -3283,7 +3283,6 @@ if (IDVal.startswith(".code")) return ParseDirectiveCode(IDVal, DirectiveID.getLoc()); else if (IDVal.startswith(".att_syntax")) { - getParser().setParsingInlineAsm(false); if (getLexer().isNot(AsmToken::EndOfStatement)) { if (Parser.getTok().getString() == "prefix") Parser.Lex(); @@ -3296,7 +3295,6 @@ return false; } else if (IDVal.startswith(".intel_syntax")) { getParser().setAssemblerDialect(1); - getParser().setParsingInlineAsm(true); if (getLexer().isNot(AsmToken::EndOfStatement)) { if (Parser.getTok().getString() == "noprefix") Parser.Lex(); Index: llvm/test/MC/X86/pr27884.s =================================================================== --- llvm/test/MC/X86/pr27884.s +++ llvm/test/MC/X86/pr27884.s @@ -1,4 +1,4 @@ -// RUN: llvm-mc -triple x86_64-unknown-unknown %s +// RUN: llvm-mc -triple x86_64-unknown-unknown %s -masm-integers=1 .intel_syntax add rbx, 0B0h Index: llvm/tools/llvm-mc/llvm-mc.cpp =================================================================== --- llvm/tools/llvm-mc/llvm-mc.cpp +++ llvm/tools/llvm-mc/llvm-mc.cpp @@ -164,6 +164,10 @@ static cl::opt SaveTempLabels("save-temp-labels", cl::desc("Don't discard temporary labels")); +static cl::opt LexMasmIntegers( + "masm-integers", + cl::desc("Enable binary and hex masm integers (0b110 and 0ABCh)")); + static cl::opt NoExecStack("no-exec-stack", cl::desc("File doesn't need an exec stack")); @@ -293,6 +297,7 @@ return SymbolResult; Parser->setShowParsedOperands(ShowInstOperands); Parser->setTargetParser(*TAP); + Parser->getLexer().setLexMasmIntegers(LexMasmIntegers); int Res = Parser->Run(NoInitialTextSection);