[llvm] 0ba553d - [MC] Allowing the use of $-prefixed integer as asm identifiers

Lucas Prates via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 6 08:27:58 PST 2020


Author: Lucas Prates
Date: 2020-03-06T16:27:51Z
New Revision: 0ba553d153e65b283c7934c3f061ad0487f03ba6

URL: https://github.com/llvm/llvm-project/commit/0ba553d153e65b283c7934c3f061ad0487f03ba6
DIFF: https://github.com/llvm/llvm-project/commit/0ba553d153e65b283c7934c3f061ad0487f03ba6.diff

LOG: [MC] Allowing the use of $-prefixed integer as asm identifiers

Summary:
Dollar signed prefixed integers were not allowed by the AsmParser to be
used as Identifiers, differing from the GNU assembler behavior.

This patch updates the parsing of Identifiers to consider such cases as
valid, where the identifier string includes the $ prefix itself. As the
Lexer currently splits these occurrences into separate tokens, those
need to be combined by the AsmParser itself.

Reviewers: efriedma, chill

Reviewed By: efriedma

Subscribers: sdardis, hiraditya, jrtc27, atanasyan, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D75111

Added: 
    llvm/test/MC/MachO/dollar-identifier.s

Modified: 
    llvm/lib/MC/MCParser/AsmParser.cpp
    llvm/test/MC/ARM/arm-branches.s
    llvm/test/MC/Mips/cpsetup-bad.s
    llvm/test/MC/Mips/invalid-instructions-spellcheck.s

Removed: 
    llvm/test/MC/MachO/bad-dollar.s


################################################################################
diff  --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp
index a3d2d9f3d3ba..c5c44a7861f0 100644
--- a/llvm/lib/MC/MCParser/AsmParser.cpp
+++ b/llvm/lib/MC/MCParser/AsmParser.cpp
@@ -2854,18 +2854,18 @@ bool AsmParser::parseIdentifier(StringRef &Res) {
     AsmToken Buf[1];
     Lexer.peekTokens(Buf, false);
 
-    if (Buf[0].isNot(AsmToken::Identifier))
+    if (Buf[0].isNot(AsmToken::Identifier) && Buf[0].isNot(AsmToken::Integer))
       return true;
 
-    // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
+    // We have a '$' or '@' followed by an identifier or integer token, make
+    // sure they are adjacent.
     if (PrefixLoc.getPointer() + 1 != Buf[0].getLoc().getPointer())
       return true;
 
     // eat $ or @
     Lexer.Lex(); // Lexer's Lex guarantees consecutive token.
     // Construct the joined identifier and consume the token.
-    Res =
-        StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
+    Res = StringRef(PrefixLoc.getPointer(), getTok().getString().size() + 1);
     Lex(); // Parser Lex to maintain invariants.
     return false;
   }

diff  --git a/llvm/test/MC/ARM/arm-branches.s b/llvm/test/MC/ARM/arm-branches.s
index da719a6bcd2e..e18fa5de1258 100644
--- a/llvm/test/MC/ARM/arm-branches.s
+++ b/llvm/test/MC/ARM/arm-branches.s
@@ -19,17 +19,22 @@
 @------------------------------------------------------------------------------
 
         .global $foo
+        .global $4
         b $foo
         bl $foo
         beq $foo
         blx $foo
         b $foo + 4
+        bl $4
+        beq $4 + 4
 
 @ CHECK: b      ($foo)                      @ encoding: [A,A,A,0xea]
 @ CHECK: bl     ($foo)                      @ encoding: [A,A,A,0xeb]
 @ CHECK: beq    ($foo)                      @ encoding: [A,A,A,0x0a]
 @ CHECK: blx    ($foo)                      @ encoding: [A,A,A,0xfa]
 @ CHECK: b      #($foo)+4                   @ encoding: [A,A,A,0xea]
+@ CHECK: bl     ($4)                        @ encoding: [A,A,A,0xeb]
+@ CHECK: beq    #($4)+4                     @ encoding: [A,A,A,0x0a]
 
 @------------------------------------------------------------------------------
 @ Leading '$' should be allowed to introduce an expression

diff  --git a/llvm/test/MC/MachO/bad-dollar.s b/llvm/test/MC/MachO/bad-dollar.s
deleted file mode 100644
index fd72ed0230db..000000000000
--- a/llvm/test/MC/MachO/bad-dollar.s
+++ /dev/null
@@ -1,5 +0,0 @@
-// RUN: not llvm-mc -triple x86_64-apple-darwin10 %s 2> %t.err > %t
-// RUN: FileCheck --check-prefix=CHECK-ERROR < %t.err %s
-
-.long $1
-// CHECK-ERROR: 4:7: error: invalid token in expression

diff  --git a/llvm/test/MC/MachO/dollar-identifier.s b/llvm/test/MC/MachO/dollar-identifier.s
new file mode 100644
index 000000000000..ca6993f7f404
--- /dev/null
+++ b/llvm/test/MC/MachO/dollar-identifier.s
@@ -0,0 +1,4 @@
+// RUN: llvm-mc -triple x86_64-apple-darwin10 %s | FileCheck %s
+
+.long $1
+// CHECK: .long ($1)

diff  --git a/llvm/test/MC/Mips/cpsetup-bad.s b/llvm/test/MC/Mips/cpsetup-bad.s
index 4b2148603bd0..14e89095d121 100644
--- a/llvm/test/MC/Mips/cpsetup-bad.s
+++ b/llvm/test/MC/Mips/cpsetup-bad.s
@@ -12,8 +12,6 @@ t1:
 # ASM: :[[@LINE-1]]:23: error: expected save register or stack offset
         .cpsetup $31, $32, __cerror
 # ASM: :[[@LINE-1]]:23: error: invalid register
-        .cpsetup $25, $2, $3
-# ASM: :[[@LINE-1]]:27: error: expected expression
         .cpsetup $25, $2, 4
 # ASM: :[[@LINE-1]]:28: error: expected symbol
         .cpsetup $25, $2, 4+65

diff  --git a/llvm/test/MC/Mips/invalid-instructions-spellcheck.s b/llvm/test/MC/Mips/invalid-instructions-spellcheck.s
index 459e71c8912a..9f1fb2f6f1be 100644
--- a/llvm/test/MC/Mips/invalid-instructions-spellcheck.s
+++ b/llvm/test/MC/Mips/invalid-instructions-spellcheck.s
@@ -13,7 +13,7 @@
 
 $2, $1, $25
 
-# ALL:      error: unexpected token at start of statement
+# ALL:      error: unknown instruction
 # ALL-NEXT: $2, $1, $25
 # ALL-NEXT:  ^
 


        


More information about the llvm-commits mailing list