[PATCH] D73176: [ARM] Fix dropped dollar sign from symbols in branch targets

Lucas Prates via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 22 05:11:13 PST 2020


pratlucas created this revision.
Herald added subscribers: llvm-commits, hiraditya, kristof.beyls.
Herald added a project: LLVM.
pratlucas added a reviewer: efriedma.

ARMAsmParser was incorrectly dropping a leading dollar sign character
from symbol names in targets of branch instructions. This was caused by
an incorrect assumption that the contents following the dollar sign
token should be handled as a constant immediate, similarly to the #
token.

This patch avoids the operand parsing from consuming the dollar sign
token when it is followed by an identifier, making sure it is properly
parsed as part of the expression.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D73176

Files:
  llvm/lib/Target/ARM/AsmParser/ARMAsmParser.cpp
  llvm/test/MC/ARM/arm-branches.s


Index: llvm/test/MC/ARM/arm-branches.s
===================================================================
--- llvm/test/MC/ARM/arm-branches.s
+++ llvm/test/MC/ARM/arm-branches.s
@@ -13,3 +13,32 @@
 @ CHECK: bl	#4                      @ encoding: [0x01,0x00,0x00,0xeb]
 @ CHECK: beq	#4                      @ encoding: [0x01,0x00,0x00,0x0a]
 @ CHECK: blx	#2                      @ encoding: [0x00,0x00,0x00,0xfb]
+
+ at ------------------------------------------------------------------------------
+@ Immediate constant should be allowed to be prefixed with '$' instead of '#'
+ at ------------------------------------------------------------------------------
+
+        b $4
+        bl $4
+        beq $4
+        blx $2
+
+@ CHECK: b      #4                      @ encoding: [0x01,0x00,0x00,0xea]
+@ CHECK: bl     #4                      @ encoding: [0x01,0x00,0x00,0xeb]
+@ CHECK: beq    #4                      @ encoding: [0x01,0x00,0x00,0x0a]
+@ CHECK: blx    #2                      @ encoding: [0x00,0x00,0x00,0xfb]
+
+ at ------------------------------------------------------------------------------
+@ Leading '$' on branch targets must not be dropped if part of symbol names
+ at ------------------------------------------------------------------------------
+
+        .global $foo
+        b $foo
+        bl $foo
+        beq $foo
+        blx $foo
+
+@ 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]
Index: llvm/lib/Target/ARM/AsmParser/ARMAsmParser.cpp
===================================================================
--- llvm/lib/Target/ARM/AsmParser/ARMAsmParser.cpp
+++ llvm/lib/Target/ARM/AsmParser/ARMAsmParser.cpp
@@ -6046,9 +6046,16 @@
     return parseRegisterList(Operands, !Mnemonic.startswith("clr"));
   case AsmToken::Dollar:
   case AsmToken::Hash:
-    // #42 -> immediate.
+    // #42 -> immediate
+    // $42 -> immediate
+    // $foo -> symbol name
     S = Parser.getTok().getLoc();
-    Parser.Lex();
+
+    // Operand can be a symbol starting with the $ character, in which case we
+    // should not drop the current token before parsing the expression
+    if (Parser.getTok().isNot(AsmToken::Dollar)
+        || getLexer().peekTok().isNot(AsmToken::Identifier))
+      Parser.Lex();
 
     if (Parser.getTok().isNot(AsmToken::Colon)) {
       bool isNegative = Parser.getTok().is(AsmToken::Minus);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D73176.239549.patch
Type: text/x-patch
Size: 2563 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200122/7fc4d379/attachment.bin>


More information about the llvm-commits mailing list