Skip to content

Commit a5eb83d

Browse files
committed
[ms] [llvm-ml] Allow optional parenthesized arguments for macros
We match ML64.EXE, which allows optional parentheses around a macro's arguments.
1 parent 04d4314 commit a5eb83d

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
@@ -1986,7 +1986,10 @@ bool MasmParser::parseStatement(ParseStatementInfo &Info,
19861986

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

19921995
// Otherwise, we have a normal instruction or directive.
@@ -2830,7 +2833,7 @@ bool MasmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc,
28302833
}
28312834

28322835
MCAsmMacroArguments A;
2833-
if (parseMacroArguments(M, A, ArgumentEndTok))
2836+
if (parseMacroArguments(M, A, ArgumentEndTok) || parseToken(ArgumentEndTok))
28342837
return true;
28352838

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

2917-
// Consume the right-parenthesis on the other side of the arguments.
2918-
if (parseRParen())
2919-
return true;
2920-
29212920
// Exit values may require lexing, unfortunately. We construct a new buffer to
29222921
// hold the exit value.
29232922
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)