[llvm] r260521 - [MC] Fixed parsing of macro arguments where expressions with spaces are present.

Scott Egerton via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 11 05:48:49 PST 2016


Author: s.egerton
Date: Thu Feb 11 07:48:49 2016
New Revision: 260521

URL: http://llvm.org/viewvc/llvm-project?rev=260521&view=rev
Log:
[MC] Fixed parsing of macro arguments where expressions with spaces are present.

Summary:
Fixed an issue for mips with an instruction such as 'sdc1 $f1, 272 +8(a0)' which has a space between '272' and '+'. The parser would then parse '272' and '+8' as two arguments instead of a single expression resulting in one too many arguments in the pseudo instruction.
The reason that the test case has been changed is so that the expected
output matches the output of the GNU assembler.

Reviewers: vkalintiris, dsanders

Subscribers: dsanders, llvm-commits

Differential Revision: http://reviews.llvm.org/D13592

Added:
    llvm/trunk/test/MC/Mips/user-macro-argument-separation.s
Modified:
    llvm/trunk/lib/MC/MCParser/AsmParser.cpp
    llvm/trunk/test/MC/AsmParser/macros-gas.s

Modified: llvm/trunk/lib/MC/MCParser/AsmParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCParser/AsmParser.cpp?rev=260521&r1=260520&r2=260521&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCParser/AsmParser.cpp (original)
+++ llvm/trunk/lib/MC/MCParser/AsmParser.cpp Thu Feb 11 07:48:49 2016
@@ -2067,7 +2067,6 @@ static bool isOperator(AsmToken::TokenKi
   case AsmToken::AmpAmp:
   case AsmToken::Exclaim:
   case AsmToken::ExclaimEqual:
-  case AsmToken::Percent:
   case AsmToken::Less:
   case AsmToken::LessEqual:
   case AsmToken::LessLess:
@@ -2106,37 +2105,44 @@ bool AsmParser::parseMacroArgument(MCAsm
   }
 
   unsigned ParenLevel = 0;
-  unsigned AddTokens = 0;
 
   // Darwin doesn't use spaces to delmit arguments.
   AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin);
 
+  bool SpaceEaten;
+
   for (;;) {
+    SpaceEaten = false;
     if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
       return TokError("unexpected token in macro instantiation");
 
-    if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
-      break;
+    if (ParenLevel == 0) {
+
+      if (Lexer.is(AsmToken::Comma))
+        break;
 
-    if (Lexer.is(AsmToken::Space)) {
-      Lex(); // Eat spaces
+      if (Lexer.is(AsmToken::Space)) {
+        SpaceEaten = true;
+        Lex(); // Eat spaces
+      }
 
       // Spaces can delimit parameters, but could also be part an expression.
       // If the token after a space is an operator, add the token and the next
       // one into this argument
       if (!IsDarwin) {
         if (isOperator(Lexer.getKind())) {
-          // Check to see whether the token is used as an operator,
-          // or part of an identifier
-          const char *NextChar = getTok().getEndLoc().getPointer();
-          if (*NextChar == ' ')
-            AddTokens = 2;
-        }
+          MA.push_back(getTok());
+          Lex();
+
+          // Whitespace after an operator can be ignored.
+          if (Lexer.is(AsmToken::Space))
+            Lex();
 
-        if (!AddTokens && ParenLevel == 0) {
-          break;
+          continue;
         }
       }
+      if (SpaceEaten)
+        break;
     }
 
     // handleMacroEntry relies on not advancing the lexer here
@@ -2152,8 +2158,6 @@ bool AsmParser::parseMacroArgument(MCAsm
 
     // Append the token to the current argument list.
     MA.push_back(getTok());
-    if (AddTokens)
-      AddTokens--;
     Lex();
   }
 

Modified: llvm/trunk/test/MC/AsmParser/macros-gas.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/AsmParser/macros-gas.s?rev=260521&r1=260520&r2=260521&view=diff
==============================================================================
--- llvm/trunk/test/MC/AsmParser/macros-gas.s (original)
+++ llvm/trunk/test/MC/AsmParser/macros-gas.s Thu Feb 11 07:48:49 2016
@@ -39,10 +39,10 @@ test3 1, 2 3
 .ascii "\_a \_b \_c"
 .endm
 
-// CHECK: .ascii "1 (23) "
+// CHECK: .ascii "1 (2 3) "
 test3_prime 1, (2 3)
 
-// CHECK: .ascii "1 (23) "
+// CHECK: .ascii "1 (2 3) "
 test3_prime 1 (2 3)
 
 // CHECK: .ascii "1 2 "

Added: llvm/trunk/test/MC/Mips/user-macro-argument-separation.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/Mips/user-macro-argument-separation.s?rev=260521&view=auto
==============================================================================
--- llvm/trunk/test/MC/Mips/user-macro-argument-separation.s (added)
+++ llvm/trunk/test/MC/Mips/user-macro-argument-separation.s Thu Feb 11 07:48:49 2016
@@ -0,0 +1,40 @@
+# RUN: llvm-mc %s -triple=mipsel-unknown-linux -show-encoding -mcpu=mips32r2 | \
+# RUN:   FileCheck %s
+# RUN: llvm-mc %s -triple=mipsel-unknown-linux -show-encoding -mcpu=mips32r2 | \
+# RUN:   FileCheck %s
+# RUN: llvm-mc %s -triple=mips-unknown-linux -show-encoding -mcpu=mips32r2 | \
+# RUN:   FileCheck %s
+
+# Check that the IAS expands macro instructions in the same way as GAS
+
+.extern sym
+# imm and rs are deliberately swapped to test whitespace separated arguments.
+.macro EX2 insn, rd, imm, rs
+.ex\@: \insn \rd, \rs, \imm
+.endm
+
+.option pic0
+
+EX2 addiu $2, 1 $3           # CHECK: addiu    $2, $3, 1
+EX2 addiu $2, ~1 $3          # CHECK: addiu    $2, $3, -2
+EX2 addiu $2, ~ 1 $3         # CHECK: addiu    $2, $3, -2
+EX2 addiu $2, 1+1 $3         # CHECK: addiu    $2, $3, 2
+EX2 addiu $2, 1+ 1 $3        # CHECK: addiu    $2, $3, 2
+EX2 addiu $2, 1 +1 $3        # CHECK: addiu    $2, $3, 2
+EX2 addiu $2, 1 + 1 $3       # CHECK: addiu    $2, $3, 2
+EX2 addiu $2, 1+~1 $3        # CHECK: addiu    $2, $3, -1
+EX2 addiu $2, 1+~ 1 $3       # CHECK: addiu    $2, $3, -1
+EX2 addiu $2, 1+ ~1 $3       # CHECK: addiu    $2, $3, -1
+EX2 addiu $2, 1 +~1 $3       # CHECK: addiu    $2, $3, -1
+EX2 addiu $2, 1 +~ 1 $3      # CHECK: addiu    $2, $3, -1
+EX2 addiu $2, 1 + ~1 $3      # CHECK: addiu    $2, $3, -1
+EX2 addiu $2, 1 + ~ 1 $3     # CHECK: addiu    $2, $3, -1
+EX2 addiu $2, 1+(1) $3       # CHECK: addiu    $2, $3, 2
+EX2 addiu $2, 1 +(1) $3      # CHECK: addiu    $2, $3, 2
+EX2 addiu $2, 1+ (1) $3      # CHECK: addiu    $2, $3, 2
+EX2 addiu $2, 1 + (1) $3     # CHECK: addiu    $2, $3, 2
+EX2 addiu $2, 1+(1)+1 $3     # CHECK: addiu    $2, $3, 3
+EX2 addiu $2, 1 +(1)+1 $3    # CHECK: addiu    $2, $3, 3
+EX2 addiu $2, 1+ (1)+1 $3    # CHECK: addiu    $2, $3, 3
+EX2 addiu $2, 1 + (1)+1 $3   # CHECK: addiu    $2, $3, 3
+nop                          # CHECK: nop




More information about the llvm-commits mailing list