https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104596

            Bug ID: 104596
           Summary: Means to add a comment in the assembly
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vries at gcc dot gnu.org
  Target Milestone: ---

I wanted to mark some insns in a way that is visible in the assembly, without
having to tinker with the .md file.

The user-level equivalent would be something like:
...
  asm ("// Start: added by x")
  ...
  asm ("// End: added by x")
...

So I wrote:
...
static rtx
gen_comment (const char *s)
{
  const char *sep = " ";
  size_t len = strlen (ASM_COMMENT_START) + strlen (sep) + strlen (s) + 1;
  char *comment = (char *) alloca (len);
  snprintf (comment, len, "%s%s%s", ASM_COMMENT_START, sep, s);
  return gen_rtx_ASM_INPUT_loc (VOIDmode, ggc_strdup (comment),
                                cfun->function_start_locus);
}
...
and used it to generate comments like this:
...
        // #APP                                                                 
// 2 "pr53465.c" 1                                                              
        // Start: Added by -minit-regs=3:                                       
        // #NO_APP                                                              
                mov.u32 %r25, 0;                                                
        // #APP                                                                 
// 2 "pr53465.c" 1                                                              
        // End: Added by -minit-regs=3:                                         
        // #NO_APP                                                              
...

This however is a bit verbose.

The APP/NO_APP is there to separate user insn from compiler insn, but in this
case, the compiler added the comments.

Furthermore, the file info is not meaningful either, we just use
cfun->function_start_locus because with UNKNOWN_LOCATION we run into a
segfault.

Both these issues are addressed by:
...
diff --git a/gcc/final.cc b/gcc/final.cc
index a9868861bd2c..5d47f3d5ba0e 100644
--- a/gcc/final.cc
+++ b/gcc/final.cc
@@ -2642,15 +2642,20 @@ final_scan_insn_1 (rtx_insn *insn, FILE *file, int
optimize_p 
ATTRIBUTE_UNUSED,
            if (string[0])
              {
                expanded_location loc;
-
-               app_enable ();
-               loc = expand_location (ASM_INPUT_SOURCE_LOCATION (body));
-               if (*loc.file && loc.line)
-                 fprintf (asm_out_file, "%s %i \"%s\" 1\n",
-                          ASM_COMMENT_START, loc.line, loc.file);
+               bool unknown_loc_p
+                 = ASM_INPUT_SOURCE_LOCATION (body) == UNKNOWN_LOCATION;
+
+               if (!unknown_loc_p)
+                 {
+                   app_enable ();
+                   loc = expand_location (ASM_INPUT_SOURCE_LOCATION (body));
+                   if (*loc.file && loc.line)
+                     fprintf (asm_out_file, "%s %i \"%s\" 1\n",
+                              ASM_COMMENT_START, loc.line, loc.file);
+                 }
                fprintf (asm_out_file, "\t%s\n", string);
 #if HAVE_AS_LINE_ZERO
-               if (*loc.file && loc.line)
+               if (!unknown_loc_p && loc.file && *loc.file && loc.line)
                  fprintf (asm_out_file, "%s 0 \"\" 2\n", ASM_COMMENT_START);
 #endif
              }
...
after which we can do in gen_comment:
...
-  return gen_rtx_ASM_INPUT_loc (VOIDmode, ggc_strdup (comment),
-                               cfun->function_start_locus);
+  return gen_rtx_ASM_INPUT (VOIDmode, ggc_strdup (comment));
...
and have the less verbose:
...
        // Start: Added by -minit-regs=3:                                       
                mov.u32 %r25, 0;                                                
        // End: Added by -minit-regs=3:                                        
              ...

Reply via email to