[llvm] 076ae58 - Revert "[MCParser] .altmacro: Support argument expansion not preceded by \"

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 11 21:16:41 PDT 2024


Author: Fangrui Song
Date: 2024-07-11T21:16:35-07:00
New Revision: 076ae5821671aa7911b0eba6636e844381115237

URL: https://github.com/llvm/llvm-project/commit/076ae5821671aa7911b0eba6636e844381115237
DIFF: https://github.com/llvm/llvm-project/commit/076ae5821671aa7911b0eba6636e844381115237.diff

LOG: Revert "[MCParser] .altmacro: Support argument expansion not preceded by \"

This reverts commit f8b1ca4992a22b4b65282c09dd6f07a1a2839070.
It incorrectly replaces `t` in `blt` to `h`.

```
.altmacro
.macro gen t
  blt 2f
2:
.endm

gen h
```

Fix #98558

Added: 
    

Modified: 
    llvm/lib/MC/MCParser/AsmParser.cpp

Removed: 
    llvm/test/MC/AsmParser/altmacro-arg.s


################################################################################
diff  --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp
index f3caa90eedfb1..b3d16d555deb7 100644
--- a/llvm/lib/MC/MCParser/AsmParser.cpp
+++ b/llvm/lib/MC/MCParser/AsmParser.cpp
@@ -2504,34 +2504,7 @@ bool AsmParser::expandMacro(raw_svector_ostream &OS, MCAsmMacro &Macro,
                             ArrayRef<MCAsmMacroArgument> A,
                             bool EnableAtPseudoVariable) {
   unsigned NParameters = Parameters.size();
-  auto expandArg = [&](unsigned Index) {
-    bool HasVararg = NParameters ? Parameters.back().Vararg : false;
-    bool VarargParameter = HasVararg && Index == (NParameters - 1);
-    for (const AsmToken &Token : A[Index])
-      // For altmacro mode, you can write '%expr'.
-      // The prefix '%' evaluates the expression 'expr'
-      // and uses the result as a string (e.g. replace %(1+2) with the
-      // string "3").
-      // Here, we identify the integer token which is the result of the
-      // absolute expression evaluation and replace it with its string
-      // representation.
-      if (AltMacroMode && Token.getString().front() == '%' &&
-          Token.is(AsmToken::Integer))
-        // Emit an integer value to the buffer.
-        OS << Token.getIntVal();
-      // Only Token that was validated as a string and begins with '<'
-      // is considered altMacroString!!!
-      else if (AltMacroMode && Token.getString().front() == '<' &&
-               Token.is(AsmToken::String)) {
-        OS << angleBracketString(Token.getStringContents());
-      }
-      // We expect no quotes around the string's contents when
-      // parsing for varargs.
-      else if (Token.isNot(AsmToken::String) || VarargParameter)
-        OS << Token.getString();
-      else
-        OS << Token.getStringContents();
-  };
+  bool HasVararg = NParameters ? Parameters.back().Vararg : false;
 
   // A macro without parameters is handled 
diff erently on Darwin:
   // gas accepts no arguments and does no substitutions
@@ -2565,10 +2538,36 @@ bool AsmParser::expandMacro(raw_svector_ostream &OS, MCAsmMacro &Macro,
       for (; Index < NParameters; ++Index)
         if (Parameters[Index].Name == Argument)
           break;
-      if (Index == NParameters)
+      if (Index == NParameters) {
         OS << '\\' << Argument;
-      else
-        expandArg(Index);
+      } else {
+        bool VarargParameter = HasVararg && Index == (NParameters - 1);
+        for (const AsmToken &Token : A[Index]) {
+          // For altmacro mode, you can write '%expr'.
+          // The prefix '%' evaluates the expression 'expr'
+          // and uses the result as a string (e.g. replace %(1+2) with the
+          // string "3").
+          // Here, we identify the integer token which is the result of the
+          // absolute expression evaluation and replace it with its string
+          // representation.
+          if (AltMacroMode && Token.getString().front() == '%' &&
+              Token.is(AsmToken::Integer))
+            // Emit an integer value to the buffer.
+            OS << Token.getIntVal();
+          // Only Token that was validated as a string and begins with '<'
+          // is considered altMacroString!!!
+          else if (AltMacroMode && Token.getString().front() == '<' &&
+                   Token.is(AsmToken::String)) {
+            OS << angleBracketString(Token.getStringContents());
+          }
+          // We expect no quotes around the string's contents when
+          // parsing for varargs.
+          else if (Token.isNot(AsmToken::String) || VarargParameter)
+            OS << Token.getString();
+          else
+            OS << Token.getStringContents();
+        }
+      }
       continue;
     }
 
@@ -2600,24 +2599,6 @@ bool AsmParser::expandMacro(raw_svector_ostream &OS, MCAsmMacro &Macro,
       }
     }
 
-    if (AltMacroMode && isIdentifierChar(Body[I])) {
-      size_t Len = 1;
-      while (I + Len != End && isIdentifierChar(Body[I + Len]))
-        ++Len;
-      StringRef Argument(Body.data() + I, Len);
-      unsigned Index = 0;
-      for (; Index != NParameters; ++Index)
-        if (Parameters[Index].Name == Argument)
-          break;
-      if (Index != NParameters) {
-        expandArg(Index);
-        I += Len;
-        if (I != End && Body[I] == '&')
-          ++I;
-        continue;
-      }
-    }
-
     OS << Body[I];
     ++I;
   }

diff  --git a/llvm/test/MC/AsmParser/altmacro-arg.s b/llvm/test/MC/AsmParser/altmacro-arg.s
deleted file mode 100644
index 262c5eac832e0..0000000000000
--- a/llvm/test/MC/AsmParser/altmacro-arg.s
+++ /dev/null
@@ -1,22 +0,0 @@
-## Arguments can be expanded even if they are not preceded by \
-# RUN: llvm-mc -triple=x86_64 %s | FileCheck %s
-
-# CHECK:      1 1 1a
-# CHECK-NEXT: 1 2 1a 2b
-# CHECK-NEXT: \$b \$b
-.altmacro
-.irp ._a,1
-  .print "\._a \._a& ._a&a"
-  .irp $b,2
-    .print "\._a \$b ._a&a $b&b"
-  .endr
-  .print "\$b \$b&"
-.endr
-
-# CHECK:      1 1& ._a&a
-# CHECK-NEXT: \$b \$b&
-.noaltmacro
-.irp ._a,1
-  .print "\._a \._a& ._a&a"
-  .print "\$b \$b&"
-.endr


        


More information about the llvm-commits mailing list