]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gas: accept leading zeros on dollar local labels in z80 sdcc compat mode
authorAaron Griffith <aargri@gmail.com>
Mon, 14 Jul 2025 08:38:52 +0000 (10:38 +0200)
committerJan Beulich <jbeulich@suse.com>
Mon, 14 Jul 2025 08:38:52 +0000 (10:38 +0200)
SDCC assembly output uses 5-digit numeric dollar sign labels, padded
with zeros. Commit 226749d made these invalid, and broke the Z80 SDCC
compatibility mode in GAS.

https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=226749d5a6ff0d5c607d6428d6c81e1e7e7a994b

This restores SDCC compatibility by replacing the leading zeros with
spaces when inside dollar local labels and when SDCC compatibility is
enabled. It also restores the SDCC test case to represent actual
syntax emitted by SDCC, and adds a note explaining the purpose of
the test.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=33030

gas/config/tc-z80.c
gas/testsuite/gas/z80/sdcc.s

index e7f014cb84daa4057c5d74a6e475b16da903564b..add28a39119ccdfaff2d744da73836bae01116ec 100644 (file)
@@ -633,6 +633,33 @@ z80_start_line_hook (void)
          break;
        }
     }
+  /* Remove leading zeros from dollar local labels if SDCC compat enabled.  */
+  if (sdcc_compat && *input_line_pointer == '0')
+    {
+      char *dollar;
+
+      /* SDCC emits at most one label definition per line, so it is
+        enough to look at only the first label.  Hand-written asm
+        might use more, but then it is unlikely to use leading zeros
+        on dollar local labels.  */
+
+      /* Place p at the first character after [0-9]+.  */
+      for (p = input_line_pointer; *p >= '0' && *p <= '9'; ++p)
+       ;
+
+      /* Is this a dollar sign label?
+        GAS allows spaces between $ and :, but SDCC does not.  */
+      if (p[0] == '$' && p[1] == ':')
+       {
+         dollar = p;
+         /* Replace zeros with spaces until the first non-zero,
+            but leave the last character before $ intact (for e.g. 0$:).  */
+         for (p = input_line_pointer; *p == '0' && p < dollar - 1; ++p)
+           {
+             *p = ' ';
+           }
+       }
+    }
   /* Check for <label>[:] =|([.](EQU|DEFL)) <value>.  */
   if (is_name_beginner (*input_line_pointer))
     {
index 98994276ced372bddc31580300ee6a5ede2a99f7..de2e0aa14980a67283d5a50d79c6d4406fe378cc 100644 (file)
@@ -1,3 +1,7 @@
+;; This file uses syntax emitted by the Small Device C Compiler.  It
+;; is used to test the -sdcc flag to the z80 assembler.  Modifications
+;; to this file should take care to keep SDCC syntax.
+
         .module longpolls
         .optsdcc -mz80
 
@@ -13,7 +17,7 @@ valueadr = 0x1234
 _start::
 ;comment
        ld      hl, #4+0
-0$:
+00000$:
        adc     a, a
        adc     a, b
        adc     a, c
@@ -29,7 +33,7 @@ _start::
        adc     a, (hl)
        adc     a, 5 (ix)
        adc     a, -2 (iy)
-100$:
+00100$:
        add     a, a
        add     a, b
        add     a, c
@@ -45,7 +49,7 @@ _start::
        add     a, (hl)
        add     a, 5 (ix)
        add     a, -2 (iy)
-200$:
+00200$:
        and     a, a
        and     a, b
        and     a, c
@@ -61,7 +65,7 @@ _start::
        and     a, (hl)
        and     a, 5 (ix)
        and     a, -2 (iy)
-300$:
+00300$:
        cp      a, a
        cp      a, b
        cp      a, c
@@ -77,7 +81,7 @@ _start::
        cp      a, (hl)
        cp      a, 5 (ix)
        cp      a, -2 (iy)
-400$:
+00400$:
        or      a, a
        or      a, b
        or      a, c
@@ -93,7 +97,7 @@ _start::
        or      a, (hl)
        or      a, 5 (ix)
        or      a, -2 (iy)
-500$:
+00500$:
        sbc     a, a
        sbc     a, b
        sbc     a, c
@@ -109,7 +113,7 @@ _start::
        sbc     a, (hl)
        sbc     a, 5 (ix)
        sbc     a, -2 (iy)
-600$:
+00600$:
        sub     a, a
        sub     a, b
        sub     a, c
@@ -125,7 +129,7 @@ _start::
        sub     a, (hl)
        sub     a, 5 (ix)
        sub     a, -2 (iy)
-700$:
+00700$:
        xor     a, a
        xor     a, b
        xor     a, c
@@ -142,21 +146,21 @@ _start::
        xor     a, 5 (ix)
        xor     a, -2 (iy)
 
-       jp      0$
-       jp      100$
-       jp      200$
-       jp      300$
-       jp      500$
-       jp      600$
-       jp      700$
+       jp      00000$
+       jp      00100$
+       jp      00200$
+       jp      00300$
+       jp      00500$
+       jp      00600$
+       jp      00700$
 _func:
        ld      hl,0
        ld      (hl),#<function
-100$:
+00100$:
        inc     hl
        ld      (hl),#>function
-600$:
-       jr      100$
+00600$:
+       jr      00100$
 _finish::
        ld      a, 2 (iy)
        ld      -1 (ix), a