Hi!

x86_function_profiler emits assembly directly into file and only emits
AT&T syntax.  The following patch adjusts it to emit MASM syntax
if -masm=intel.
As it doesn't use asm_fprintf, I can't use {|} syntax for the dialects.

I've tested using
for i in -mcmodel=large "-mcmodel=large -fpic" "" -fpic "-m32 -fpic" "-m32"; do
./xgcc -B ./ -c -O2 -fprofile $i -masm=att pr113122.c -o pr113122.o1;
./xgcc -B ./ -c -O2 -fprofile $i -masm=intel pr113122.c -o pr113122.o2;
objdump -dr pr113122.o1 > /tmp/1; objdump -dr pr113122.o2 > /tmp/2;
diff -up /tmp/1 /tmp/2; done
that the emitted sequences are identical after assembly.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2024-01-18  Jakub Jelinek  <ja...@redhat.com>

        PR target/113122
        * config/i386/i386.cc (x86_function_profiler): Add -masm=intel
        support.  Add missing space after , in emitted assembly in some
        cases.  Formatting fixes.

        * gcc.target/i386/pr113122-1.c: New test.
        * gcc.target/i386/pr113122-2.c: New test.
        * gcc.target/i386/pr113122-3.c: New test.
        * gcc.target/i386/pr113122-4.c: New test.

--- gcc/config/i386/i386.cc.jj  2024-01-05 15:22:21.810685516 +0100
+++ gcc/config/i386/i386.cc     2024-01-17 16:52:48.026177278 +0100
@@ -22746,7 +22746,10 @@ x86_function_profiler (FILE *file, int l
   if (TARGET_64BIT)
     {
 #ifndef NO_PROFILE_COUNTERS
-      fprintf (file, "\tleaq\t%sP%d(%%rip),%%r11\n", LPREFIX, labelno);
+      if (ASSEMBLER_DIALECT == ASM_INTEL)
+       fprintf (file, "\tlea\tr11, %sP%d[rip]\n", LPREFIX, labelno);
+      else
+       fprintf (file, "\tleaq\t%sP%d(%%rip), %%r11\n", LPREFIX, labelno);
 #endif
 
       if (!TARGET_PECOFF)
@@ -22757,12 +22760,29 @@ x86_function_profiler (FILE *file, int l
              /* NB: R10 is caller-saved.  Although it can be used as a
                 static chain register, it is preserved when calling
                 mcount for nested functions.  */
-             fprintf (file, "1:\tmovabsq\t$%s, %%r10\n\tcall\t*%%r10\n",
-                      mcount_name);
+             if (ASSEMBLER_DIALECT == ASM_INTEL)
+               fprintf (file, "1:\tmovabs\tr10, OFFSET FLAT:%s\n"
+                              "\tcall\tr10\n", mcount_name);
+             else
+               fprintf (file, "1:\tmovabsq\t$%s, %%r10\n\tcall\t*%%r10\n",
+                        mcount_name);
              break;
            case CM_LARGE_PIC:
 #ifdef NO_PROFILE_COUNTERS
-             fprintf (file, "1:\tmovabsq\t$_GLOBAL_OFFSET_TABLE_-1b, %%r11\n");
+             if (ASSEMBLER_DIALECT == ASM_INTEL)
+               {
+                 fprintf (file, "1:movabs\tr11, "
+                                "OFFSET FLAT:_GLOBAL_OFFSET_TABLE_-1b\n");
+                 fprintf (file, "\tlea\tr10, 1b[rip]\n");
+                 fprintf (file, "\tadd\tr10, r11\n");
+                 fprintf (file, "\tmovabs\tr11, OFFSET FLAT:%s@PLTOFF\n",
+                          mcount_name);
+                 fprintf (file, "\tadd\tr10, r11\n");
+                 fprintf (file, "\tcall\tr10\n");
+                 break;
+               }
+             fprintf (file,
+                      "1:\tmovabsq\t$_GLOBAL_OFFSET_TABLE_-1b, %%r11\n");
              fprintf (file, "\tleaq\t1b(%%rip), %%r10\n");
              fprintf (file, "\taddq\t%%r11, %%r10\n");
              fprintf (file, "\tmovabsq\t$%s@PLTOFF, %%r11\n", mcount_name);
@@ -22776,7 +22796,12 @@ x86_function_profiler (FILE *file, int l
            case CM_MEDIUM_PIC:
              if (!ix86_direct_extern_access)
                {
-                 fprintf (file, "1:\tcall\t*%s@GOTPCREL(%%rip)\n", 
mcount_name);
+                 if (ASSEMBLER_DIALECT == ASM_INTEL)
+                   fprintf (file, "1:\tcall\t[QWORD PTR %s@GOTPCREL[rip]]",
+                            mcount_name);
+                 else
+                   fprintf (file, "1:\tcall\t*%s@GOTPCREL(%%rip)\n",
+                            mcount_name);
                  break;
                }
              /* fall through */
@@ -22791,23 +22816,37 @@ x86_function_profiler (FILE *file, int l
   else if (flag_pic)
     {
 #ifndef NO_PROFILE_COUNTERS
-      fprintf (file, "\tleal\t%sP%d@GOTOFF(%%ebx),%%" PROFILE_COUNT_REGISTER 
"\n",
-              LPREFIX, labelno);
+      if (ASSEMBLER_DIALECT == ASM_INTEL)
+       fprintf (file,
+                "\tlea\t" PROFILE_COUNT_REGISTER ", %sP%d@GOTOFF[ebx]\n",
+                LPREFIX, labelno);
+      else
+       fprintf (file,
+                "\tleal\t%sP%d@GOTOFF(%%ebx), %%" PROFILE_COUNT_REGISTER "\n",
+                LPREFIX, labelno);
 #endif
-      fprintf (file, "1:\tcall\t*%s@GOT(%%ebx)\n", mcount_name);
+      if (ASSEMBLER_DIALECT == ASM_INTEL)
+       fprintf (file, "1:\tcall\t[DWORD PTR %s@GOT[ebx]]\n", mcount_name);
+      else
+       fprintf (file, "1:\tcall\t*%s@GOT(%%ebx)\n", mcount_name);
     }
   else
     {
 #ifndef NO_PROFILE_COUNTERS
-      fprintf (file, "\tmovl\t$%sP%d,%%" PROFILE_COUNT_REGISTER "\n",
-              LPREFIX, labelno);
+      if (ASSEMBLER_DIALECT == ASM_INTEL)
+       fprintf (file,
+                "\tmov\t" PROFILE_COUNT_REGISTER ", OFFSET FLAT:%sP%d\n",
+                LPREFIX, labelno);
+      else
+       fprintf (file, "\tmovl\t$%sP%d, %%" PROFILE_COUNT_REGISTER "\n",
+                LPREFIX, labelno);
 #endif
       x86_print_call_or_nop (file, mcount_name);
     }
 
   if (flag_record_mcount
-       || lookup_attribute ("fentry_section",
-                                DECL_ATTRIBUTES (current_function_decl)))
+      || lookup_attribute ("fentry_section",
+                          DECL_ATTRIBUTES (current_function_decl)))
     {
       const char *sname = "__mcount_loc";
 
--- gcc/testsuite/gcc.target/i386/pr113122-1.c.jj       2024-01-17 
17:04:48.572481159 +0100
+++ gcc/testsuite/gcc.target/i386/pr113122-1.c  2024-01-17 17:05:08.444205080 
+0100
@@ -0,0 +1,10 @@
+/* PR target/113122 */
+/* { dg-do assemble { target { *-*-linux* && lp64 } } } */
+/* { dg-require-effective-target mfentry } */
+/* { dg-require-effective-target masm_intel } */
+/* { dg-options "-fprofile -mfentry -O2 -mcmodel=large -masm=intel" } */
+
+void
+func (void)
+{
+}
--- gcc/testsuite/gcc.target/i386/pr113122-2.c.jj       2024-01-17 
17:05:19.368053314 +0100
+++ gcc/testsuite/gcc.target/i386/pr113122-2.c  2024-01-17 17:05:30.426899673 
+0100
@@ -0,0 +1,11 @@
+/* PR target/113122 */
+/* { dg-do assemble { target { *-*-linux* && lp64 } } } */
+/* { dg-require-effective-target mfentry } */
+/* { dg-require-effective-target masm_intel } */
+/* { dg-require-effective-target fpic } */
+/* { dg-options "-fpic -fprofile -mfentry -O2 -mcmodel=large -masm=intel" } */
+
+void
+func (void)
+{
+}
--- gcc/testsuite/gcc.target/i386/pr113122-3.c.jj       2024-01-17 
17:06:12.932320651 +0100
+++ gcc/testsuite/gcc.target/i386/pr113122-3.c  2024-01-17 17:06:46.199870472 
+0100
@@ -0,0 +1,9 @@
+/* PR target/113122 */
+/* { dg-do assemble { target *-*-linux* } } */
+/* { dg-require-effective-target masm_intel } */
+/* { dg-options "-fprofile -O2 -masm=intel" } */
+
+void
+func (void)
+{
+}
--- gcc/testsuite/gcc.target/i386/pr113122-4.c.jj       2024-01-17 
17:06:56.327733421 +0100
+++ gcc/testsuite/gcc.target/i386/pr113122-4.c  2024-01-17 17:07:10.229545301 
+0100
@@ -0,0 +1,10 @@
+/* PR target/113122 */
+/* { dg-do assemble { target *-*-linux* } } */
+/* { dg-require-effective-target masm_intel } */
+/* { dg-require-effective-target fpic } */
+/* { dg-options "-fpic -fprofile -O2 -masm=intel" } */
+
+void
+func (void)
+{
+}

        Jakub

Reply via email to