[PATCH PR gas/33030 v2] gas: accept leading zeros on dollar local labels in z80 sdcc compat mode

Aaron Griffith aargri@gmail.com
Sun Jul 6 21:48:14 GMT 2025


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
---
Apart from the requested changes, I've also added a comment to the top
of the SDCC test case explaining the purpose of this test. A comment
like this might have prevented the original break, and it's cheap
enough to add.
---
Changes in v2:
- More careful compliance with GNU code style.
- Add comments noting assumptions about SDCC assembly format.
- Add a comment to the SDCC test explaining the purpose of the test.
- Link to v1: https://inbox.sourceware.org/20250703-z80-sdcc-fix-v1-1-89f46103852a@gmail.com
---
 gas/config/tc-z80.c          | 27 +++++++++++++++++++++++++++
 gas/testsuite/gas/z80/sdcc.s | 40 ++++++++++++++++++++++------------------
 2 files changed, 49 insertions(+), 18 deletions(-)

diff --git a/gas/config/tc-z80.c b/gas/config/tc-z80.c
index 3abc0268699593ac01ac060e82fbf18a4d4986a9..a1792dcc2bcd3180a9440d6ccb9b0a52cb800800 100644
--- a/gas/config/tc-z80.c
+++ b/gas/config/tc-z80.c
@@ -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))
     {
diff --git a/gas/testsuite/gas/z80/sdcc.s b/gas/testsuite/gas/z80/sdcc.s
index 98994276ced372bddc31580300ee6a5ede2a99f7..de2e0aa14980a67283d5a50d79c6d4406fe378cc 100644
--- a/gas/testsuite/gas/z80/sdcc.s
+++ b/gas/testsuite/gas/z80/sdcc.s
@@ -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

---
base-commit: dbd830f14f791fa8d27afa08b258347b95608e57
change-id: 20250703-z80-sdcc-fix-3d44fed1a788

Best regards,
-- 
Aaron Griffith <aargri@gmail.com>



More information about the Binutils mailing list