Skip to content

[ms] [llvm-ml] Allow optional parenthesized arguments for macros #129905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion llvm/lib/MC/MCParser/AsmLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,8 +698,10 @@ size_t AsmLexer::peekTokens(MutableArrayRef<AsmToken> Buf,

Buf[ReadCount] = Token;

if (Token.is(AsmToken::Eof))
if (Token.is(AsmToken::Eof)) {
ReadCount++;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change related to the rest, or is it a separate drive-by fix?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... it's been a while since I wrote this, and I'm sorry to say I'm not sure. I have a vague memory that it was related in a surprising way.

break;
}
}

SetError(SavedErrLoc, SavedErr);
Expand Down
11 changes: 5 additions & 6 deletions llvm/lib/MC/MCParser/MasmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1985,7 +1985,10 @@ bool MasmParser::parseStatement(ParseStatementInfo &Info,

// If macros are enabled, check to see if this is a macro instantiation.
if (const MCAsmMacro *M = getContext().lookupMacro(IDVal.lower())) {
return handleMacroEntry(M, IDLoc);
AsmToken::TokenKind ArgumentEndTok = parseOptionalToken(AsmToken::LParen)
? AsmToken::RParen
: AsmToken::EndOfStatement;
return handleMacroEntry(M, IDLoc, ArgumentEndTok);
}

// Otherwise, we have a normal instruction or directive.
Expand Down Expand Up @@ -2829,7 +2832,7 @@ bool MasmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc,
}

MCAsmMacroArguments A;
if (parseMacroArguments(M, A, ArgumentEndTok))
if (parseMacroArguments(M, A, ArgumentEndTok) || parseToken(ArgumentEndTok))
return true;

// Macro instantiation is lexical, unfortunately. We construct a new buffer
Expand Down Expand Up @@ -2913,10 +2916,6 @@ bool MasmParser::handleMacroInvocation(const MCAsmMacro *M, SMLoc NameLoc) {
eatToEndOfStatement();
}

// Consume the right-parenthesis on the other side of the arguments.
if (parseRParen())
return true;

// Exit values may require lexing, unfortunately. We construct a new buffer to
// hold the exit value.
std::unique_ptr<MemoryBuffer> MacroValue =
Expand Down
19 changes: 19 additions & 0 deletions llvm/test/tools/llvm-ml/macro.asm
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,25 @@ substitution_test_uppercase PROC
ret
substitution_test_uppercase ENDP

substitution_test_with_parentheses PROC
; CHECK-LABEL: substitution_test_with_parentheses:

SubstitutionMacro(2, 8)
; CHECK: mov eax, 2
; CHECK-NEXT: mov eax, 2
; CHECK-NEXT: mov eax, 2
; CHECK-NEXT: mov eax, 2
; CHECK: mov eax, dword ptr [rip + xa1]
; CHECK-NEXT: mov eax, dword ptr [rip + x2]
; CHECK-NEXT: mov eax, dword ptr [rip + x2]
; CHECK: mov eax, 8
; CHECK-NEXT: mov eax, 8
; CHECK-NEXT: mov eax, 8
; CHECK-NEXT: mov eax, 8

ret
substitution_test_with_parentheses ENDP

AmbiguousSubstitutionMacro MACRO x, y
x&y BYTE 0
ENDM
Expand Down