Skip to content

Commit 9d94a4e

Browse files
committed
AsmParser: Parse (and ignore) nested .macro definitions.
This enables a slightly odd feature of gas. The macro is defined when the outermost macro is instantiated. PR18599 llvm-svn: 201045
1 parent 15b2669 commit 9d94a4e

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

llvm/lib/MC/MCParser/AsmParser.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3183,6 +3183,7 @@ bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
31833183
Lex();
31843184

31853185
AsmToken EndToken, StartToken = getTok();
3186+
unsigned MacroDepth = 0;
31863187

31873188
// Lex the macro definition.
31883189
for (;;) {
@@ -3191,15 +3192,25 @@ bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) {
31913192
return Error(DirectiveLoc, "no matching '.endmacro' in definition");
31923193

31933194
// Otherwise, check whether we have reach the .endmacro.
3194-
if (getLexer().is(AsmToken::Identifier) &&
3195-
(getTok().getIdentifier() == ".endm" ||
3196-
getTok().getIdentifier() == ".endmacro")) {
3197-
EndToken = getTok();
3198-
Lex();
3199-
if (getLexer().isNot(AsmToken::EndOfStatement))
3200-
return TokError("unexpected token in '" + EndToken.getIdentifier() +
3201-
"' directive");
3202-
break;
3195+
if (getLexer().is(AsmToken::Identifier)) {
3196+
if (getTok().getIdentifier() == ".endm" ||
3197+
getTok().getIdentifier() == ".endmacro") {
3198+
if (MacroDepth == 0) { // Outermost macro.
3199+
EndToken = getTok();
3200+
Lex();
3201+
if (getLexer().isNot(AsmToken::EndOfStatement))
3202+
return TokError("unexpected token in '" + EndToken.getIdentifier() +
3203+
"' directive");
3204+
break;
3205+
} else {
3206+
// Otherwise we just found the end of an inner macro.
3207+
--MacroDepth;
3208+
}
3209+
} else if (getTok().getIdentifier() == ".macro") {
3210+
// We allow nested macros. Those aren't instantiated until the outermost
3211+
// macro is expanded so just ignore them for now.
3212+
++MacroDepth;
3213+
}
32033214
}
32043215

32053216
// Otherwise, scan til the end of the statement.

llvm/test/MC/AsmParser/macro-def-in-instantiation.s

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,23 @@ $4
1111
.data
1212
// CHECK: .byte 10
1313
.mybyte 10
14+
15+
// PR18599
16+
.macro macro_a
17+
18+
.macro macro_b
19+
.byte 10
20+
.macro macro_c
21+
.endm
22+
23+
macro_c
24+
.purgem macro_c
25+
.endm
26+
27+
macro_b
28+
.endm
29+
30+
macro_a
31+
macro_b
32+
// CHECK: .byte 10
33+
// CHECK: .byte 10

0 commit comments

Comments
 (0)