Skip to content

Commit d48a36f

Browse files
authored
[ms] [llvm-ml] Allow optional parenthesized arguments for macros (#129905)
We match ML64.EXE, which allows optional parentheses around a macro's arguments.
1 parent 356bf3f commit d48a36f

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

llvm/lib/MC/MCParser/AsmLexer.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,8 +698,10 @@ size_t AsmLexer::peekTokens(MutableArrayRef<AsmToken> Buf,
698698

699699
Buf[ReadCount] = Token;
700700

701-
if (Token.is(AsmToken::Eof))
701+
if (Token.is(AsmToken::Eof)) {
702+
ReadCount++;
702703
break;
704+
}
703705
}
704706

705707
SetError(SavedErrLoc, SavedErr);

llvm/lib/MC/MCParser/MasmParser.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,7 +1985,10 @@ bool MasmParser::parseStatement(ParseStatementInfo &Info,
19851985

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

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

28312834
MCAsmMacroArguments A;
2832-
if (parseMacroArguments(M, A, ArgumentEndTok))
2835+
if (parseMacroArguments(M, A, ArgumentEndTok) || parseToken(ArgumentEndTok))
28332836
return true;
28342837

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

2916-
// Consume the right-parenthesis on the other side of the arguments.
2917-
if (parseRParen())
2918-
return true;
2919-
29202919
// Exit values may require lexing, unfortunately. We construct a new buffer to
29212920
// hold the exit value.
29222921
std::unique_ptr<MemoryBuffer> MacroValue =

llvm/test/tools/llvm-ml/macro.asm

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,25 @@ substitution_test_uppercase PROC
100100
ret
101101
substitution_test_uppercase ENDP
102102

103+
substitution_test_with_parentheses PROC
104+
; CHECK-LABEL: substitution_test_with_parentheses:
105+
106+
SubstitutionMacro(2, 8)
107+
; CHECK: mov eax, 2
108+
; CHECK-NEXT: mov eax, 2
109+
; CHECK-NEXT: mov eax, 2
110+
; CHECK-NEXT: mov eax, 2
111+
; CHECK: mov eax, dword ptr [rip + xa1]
112+
; CHECK-NEXT: mov eax, dword ptr [rip + x2]
113+
; CHECK-NEXT: mov eax, dword ptr [rip + x2]
114+
; CHECK: mov eax, 8
115+
; CHECK-NEXT: mov eax, 8
116+
; CHECK-NEXT: mov eax, 8
117+
; CHECK-NEXT: mov eax, 8
118+
119+
ret
120+
substitution_test_with_parentheses ENDP
121+
103122
AmbiguousSubstitutionMacro MACRO x, y
104123
x&y BYTE 0
105124
ENDM

0 commit comments

Comments
 (0)