[PATCH] D76962: [MC] Parse directives with arguments as macro arguments

Jian Cai via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 27 17:39:51 PDT 2020


jcai19 created this revision.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

Currently when expanding an macro that takes directives as arguments,
the integrated assembler treats the name of a directive and its
arguments separated by space as separate arguments to the macro instead
of one argument. For example, the following sample code will fail to
assemble since .section and .foo are treated as two arguments to the
alternative_inst macro.

.macro alternative_insn insn
\insn
+.endm

+alternative_insn .section .foo

This change fixes such issue by identifying directives used as macro
arguments and include their arguments as part of the macro argument.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D76962

Files:
  llvm/lib/MC/MCParser/AsmParser.cpp
  llvm/test/MC/X86/expand-macro-directive-arguments.s


Index: llvm/test/MC/X86/expand-macro-directive-arguments.s
===================================================================
--- /dev/null
+++ llvm/test/MC/X86/expand-macro-directive-arguments.s
@@ -0,0 +1,10 @@
+# RUN: llvm-mc -filetype=obj -triple=i386 %s -o - | llvm-readelf --sections | FileCheck %s
+
+.macro alternative_insn insn1, insn2
+# CHECK: .foo              PROGBITS        00000000 000034 000000 00      0   0  1
+\insn1
+# CHECK: .bar PROGBITS        00000000 000034 000000 00      0   0  1
+\insn2
+.endm
+
+alternative_insn .section .foo, .section .bar
Index: llvm/lib/MC/MCParser/AsmParser.cpp
===================================================================
--- llvm/lib/MC/MCParser/AsmParser.cpp
+++ llvm/lib/MC/MCParser/AsmParser.cpp
@@ -2611,6 +2611,7 @@
           continue;
         }
       }
+
       if (SpaceEaten)
         break;
     }
@@ -2629,6 +2630,15 @@
     // Append the token to the current argument list.
     MA.push_back(getTok());
     Lexer.Lex();
+
+    // If the token is a directive, append all the following arguments if there is any
+    AsmToken &Tok = MA.back();
+    if (Tok.is(AsmToken::Identifier) && Tok.getString().startswith(".")) {
+      while (!Lexer.is(AsmToken::Comma) && !Lexer.is(AsmToken::EndOfStatement)) {
+        MA.push_back(getTok());
+        Lexer.Lex();
+      }
+    }
   }
 
   if (ParenLevel != 0)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D76962.253261.patch
Type: text/x-patch
Size: 1380 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200328/cfc4fc48/attachment.bin>


More information about the llvm-commits mailing list