From 8395cf72b49f1418deccc92c50accd2464177a45 Mon Sep 17 00:00:00 2001
From: Hans-Peter Nilsson <hp@axis.com>
Date: Fri, 3 Jan 2025 18:25:36 +0100
Subject: [PATCH] MMIX: Replace format for private symbol output by output-time
 adjustment

All this started with belated MMIX regression patrol in observance of
the holidays, looking at gcc.dg/Wstringop-overflow-27.c as a
regression for target mmix.  That's because of a single message not
matched, where there is "note: destination object 'vla::22'" instead
of the expected "note: destination object 'vla'" due to
r11-5523-geafe8ee7af13c3 in which the message format and the match
changed.

That ::22 is because some identifiers that are SSA_NAME-versions and
other clones are "privatized" by ASM_FORMAT_PRIVATE_NAME and its
companion macro by default, ASM_PN_FORMAT; see the patch.  I found
that these "private names" were "unprivatized" for the purpose of
warnings and error messages *in code that only handles the default
format*, "%s.%lu".

I went ahead and wrote and tested a patch-set to hookize that
unprivatizing code, but found that it would only affect errors and
warnings; dumps still had the "target format".  While having bad
thoughts about being hit by yet another structural testism because of
the choice of outputting yet another target-specific format instead of
a canonical "versioned" format, I realized it *already was handling a
canonical format*: only the default "%s.%lu" is properly handled.

To wit, targets are better off with the default "%s.%lu" and adjusting
it (if needed, including not allowing "." or "$"), *at time of
assembly code output*.  IOW, outputs, both references and definitions,
pass a single label-output target code point: ASM_OUTPUT_LABELREF,
which is that time of output.  Some older testsuite adjustments need
to be unadjusted, but is another rabbit-hole, so I've kept that change
separate.  Other tests checking dumps, now started to pass for the
first time, some 20+.

	* config/mmix/mmix.cc (mmix_asm_output_labelref): Replace '.'
	with '::'.
	* config/mmix/mmix.h (ASM_PN_FORMAT): Define to actual default.
---
 gcc/config/mmix/mmix.cc | 29 +++++++++++++++++++++++++++++
 gcc/config/mmix/mmix.h  | 10 +++++-----
 2 files changed, 34 insertions(+), 5 deletions(-)

diff --git a/gcc/config/mmix/mmix.cc b/gcc/config/mmix/mmix.cc
index 13b6c39c7678..e167ffcb6609 100644
--- a/gcc/config/mmix/mmix.cc
+++ b/gcc/config/mmix/mmix.cc
@@ -1586,6 +1586,35 @@ mmix_asm_output_labelref (FILE *stream, const char *name)
     if (*name == '@')
       is_extern = 0;
 
+  size_t ndots = 0;
+  for (const char *s = name; *s != 0; s++)
+    if (*s == '.')
+      ndots++;
+
+  /* Replace all '.' with '::'.  We don't want a '.' as part of an identifier
+     as that'd be incompatible with mmixal.  We also don't want to do things
+     like overriding the default "%s.%lu" by '#define ASM_PN_FORMAT "%s::%lu"'
+     as that format will show up in warnings and error messages.  The default
+     won't show up in warnings and errors, as there are mechanisms in place to
+     strip that (but that only handles the default format).  FIXME: Make sure
+     no ":" is seen in the object file; we don't really want that mmixal
+     feature visible there.  */
+  if (ndots > 0)
+    {
+      char *colonized_name = XALLOCAVEC (char, strlen (name) + 1 + ndots);
+
+      char *cs = colonized_name;
+      const char *s = name;
+      for (; *s != 0; s++)
+	{
+	  if (*s == '.')
+	    *cs++ = ':';
+	  *cs++ = *s;
+	}
+      *cs = 0;
+      name = colonized_name;
+    }
+
   asm_fprintf (stream, "%s%U%s",
 	       is_extern && TARGET_TOPLEVEL_SYMBOLS ? ":" : "",
 	       name);
diff --git a/gcc/config/mmix/mmix.h b/gcc/config/mmix/mmix.h
index 47db27594d7a..44669e195b4a 100644
--- a/gcc/config/mmix/mmix.h
+++ b/gcc/config/mmix/mmix.h
@@ -656,11 +656,11 @@ typedef struct { int regs; int lib; } CUMULATIVE_ARGS;
 #define ASM_GENERATE_INTERNAL_LABEL(LABEL, PREFIX, NUM) \
  sprintf (LABEL, "*%s:%ld", PREFIX, (long)(NUM))
 
-/* Insert "::"; these are rarer than internal labels.  FIXME: Make sure no
-   ":" is seen in the object file; we don't really want that mmixal
-   feature visible there.  We don't want the default, which uses a dot;
-   that'd be incompatible with mmixal.  */
-#define ASM_PN_FORMAT "%s::%lu"
+/* Override the default, which looks at NO_DOT_IN_LABEL and NO_DOLLAR_IN_LABEL.
+   We want the real default "%s.%lu" in dumps and compiler messages, but the
+   actual assembly code format is adjusted to the effect of "%s::%lu".  See
+   mmix_asm_output_labelref.  */
+#define ASM_PN_FORMAT "%s.%lu"
 
 #define ASM_OUTPUT_DEF(STREAM, NAME, VALUE) \
  mmix_asm_output_def (STREAM, NAME, VALUE)
-- 
GitLab