Patchwork Allow printing of escaped curly braces in assembler directives with operands

login
register
mail settings
Submitter Siddhesh Poyarekar
Date March 27, 2012, 5:21 a.m.
Message ID <20120327105137.44c352dd@spoyarek>
Download mbox | patch
Permalink /patch/148859/
State New
Headers show

Comments

Siddhesh Poyarekar - March 27, 2012, 5:21 a.m.
Hi,

An assembler directive with an operand is filtered through
output_asm_insn (or asm_fprintf for gcc internal asm() directives) to
expand the operand values in the assembler as well as to choose
dialects if present. This patch is concerned primarily with the
dialects, since their syntax prevent inclusion of assembler strings
with curly braces, causing them to be interpreted as dialects.

The attached patch allows printing of curly braces in assembler by
preceding them with a \\. So to print the following code into assembler:

.pushsection ".foo"; .asciz "div { width : 50%% | height=10px }"; .long
42; .popsection

The following code needs to be used with this patch:

void f()
{
  asm ( ".pushsection \".foo\"; .asciz \"div \\{ width : 50%% |
height=10px \\} \"; .long %c0; .popsection" : : "i"(42) ); }

The other option to \\ (since it doesn't look as clean) was to use %
as an escape character, but I was not sure if that is a better looking
option or a worse looking one. I don't mind resubmitting the patch to
use %{ and %} to print curly braces if that is acceptable.

It is still possible to print curly braces in asm string literals
without operands since they do not undergo any transformation.

The patch does not introduce any regressions. I have tested this with
x86_64 and i686 and it works well with both of them.

Regards,
Siddhesh


gcc/ChangeLog:

2012-03-27  Siddhesh Poyarekar  <siddhesh@redhat.com>

	* final.c (output_asm_insn, asm_fprintf): Print curly braces if
	preceded by an escaped backslash (\\).

testsuite/ChangeLog:

2012-03-27  Siddhesh Poyarekar  <siddhesh@redhat.com>

	* gcc.dg/asm-braces.c: New test case.

Patch

diff --git a/gcc/final.c b/gcc/final.c
index 718caf1..2393c0f 100644
--- a/gcc/final.c
+++ b/gcc/final.c
@@ -3444,6 +3444,12 @@  output_asm_insn (const char *templ, rtx *operands)
 	  output_operand_lossage ("invalid %%-code");
 	break;
 
+      /* Escaped braces. Print them as is. */
+      case '\\':
+        if (*p == '{' || *p == '}')
+          c = *p++;
+        /* FALLTHROUGH */
+
       default:
 	putc (c, asm_out_file);
       }
@@ -3955,6 +3961,12 @@  asm_fprintf (FILE *file, const char *p, ...)
 	  }
 	break;
 
+      /* Escaped braces. Print them as is. */
+      case '\\':
+        if (*p == '{' || *p == '}')
+          c = *p++;
+        /* FALLTHROUGH */
+
       default:
 	putc (c, file);
       }
diff --git a/gcc/testsuite/gcc.dg/asm-braces.c b/gcc/testsuite/gcc.dg/asm-braces.c
new file mode 100644
index 0000000..4f428c8
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/asm-braces.c
@@ -0,0 +1,9 @@ 
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+void f()
+{
+  asm ( ".pushsection \".foo\"; .asciz \"div \\{ width : 50%% | height = 10px \\} \"; .long %c0; .popsection" : : "i"(42) );
+}
+
+/* { dg-final { scan-assembler "div { width : 50%% | height = 10px }" } } */
-- 
1.7.7.6