]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
inline-asm: Add support for cc operand modifier
authorJakub Jelinek <jakub@redhat.com>
Wed, 18 Dec 2024 10:49:11 +0000 (11:49 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Wed, 18 Dec 2024 10:49:11 +0000 (11:49 +0100)
As mentioned in the "inline asm: Add new constraint for symbol definitions"
patch description, while the c operand modifier is documented to:
Require a constant operand and print the constant expression with no punctuation.
it actually doesn't do that with -fpic at least on some targets and
has been behaving that way for at least 3 decades.
It prints the operand using output_addr_const if CONSTANT_ADDRESS_P is true,
but CONSTANT_ADDRESS_P can do all sorts of target specific checks.
And if it is false, it falls back to output_operand (operands[opnum], 'c');
which will on various targets just result in an error that it is invalid
modifier letter (weird because it is documented), on others like x86 or
alpha will handle the operand in some weird way if it is a comparison
and otherwise complain the argument isn't a comparison, on others like
arm perhaps do what the user wanted.

As I wrote, we are pretty much out of modifier letters because some targets
use a lot of them, and almost out of % punctuation chars (I think ` is left)
but right now punctuation chars aren't normally followed by operand number
anyway.

So, the following patch takes one of the generic letters (c) and adds an
extra modifier char after it, I chose cc, which behaves like c but just
always uses output_addr_const instead of falling back to the machine
dependent code.

2024-12-18  Jakub Jelinek  <jakub@redhat.com>

* final.cc (output_asm_insn): Add support for cc operand modifier.
* doc/extend.texi (Generic Operand Modifiers): Document cc operand
modifier.
* doc/md.texi (@samp{:} in constraint): Mention the cc operand
modifier and add small example.

* c-c++-common/toplevel-asm-4.c: Don't use -fno-pie option.
Use cc modifier instead of c.
(v, w): Add extern keyword.
* c-c++-common/toplevel-asm-6.c: New test.

gcc/doc/extend.texi
gcc/doc/md.texi
gcc/final.cc
gcc/testsuite/c-c++-common/toplevel-asm-4.c
gcc/testsuite/c-c++-common/toplevel-asm-6.c [new file with mode: 0644]

index f045159963edef343cd51a0221d0a52be4f880f9..773e487b8246bb4930632e0a0c5774a1d3e3f294 100644 (file)
@@ -12141,6 +12141,11 @@ The following table shows the modifiers supported by all targets and their effec
 @item @code{c}
 @tab Require a constant operand and print the constant expression with no punctuation.
 @tab @code{%c0}
+@item @code{cc}
+@tab Like @samp{%c} except try harder to print it with no punctuation.
+@samp{%c} can e.g.@: fail to print constant addresses in position independent code on
+some architectures.
+@tab @code{%cc0}
 @item @code{n}
 @tab Like @samp{%c} except that the value of the constant is negated before printing.
 @tab @code{%n0}
index 4a65091168405a4b6f27968abee85146511bd63c..0ed1bc121438f367b3b90520bf2c674c184588cc 100644 (file)
@@ -1510,6 +1510,13 @@ This constraint, allowed only in input operands, says the inline @code{asm}
 pattern defines specific function or variable symbol.  The constraint
 shouldn't be mixed with other constraints on the same operand and
 the operand should be address of a function or non-automatic variable.
+Best used with the @samp{cc} modifier when printing the operand, so that
+even in position independent code it prints as a label.
+
+@smallexample
+void foo (void);
+asm (".globl %cc0; %cc0: ret" : : ":" (foo));
+@end smallexample
 
 @cindex other register constraints
 @cindex extensible constraints
index 184b71ccee66daa5163433660e2e5f6995177942..ec7644c2535a83a15101c2e52ef2a590b63361ec 100644 (file)
@@ -3493,7 +3493,10 @@ output_asm_insn (const char *templ, rtx *operands)
            int letter = *p++;
            unsigned long opnum;
            char *endptr;
+           int letter2 = 0;
 
+           if (letter == 'c' && *p == 'c')
+             letter2 = *p++;
            opnum = strtoul (p, &endptr, 10);
 
            if (endptr == p)
@@ -3507,7 +3510,7 @@ output_asm_insn (const char *templ, rtx *operands)
              output_address (VOIDmode, operands[opnum]);
            else if (letter == 'c')
              {
-               if (CONSTANT_ADDRESS_P (operands[opnum]))
+               if (letter2 == 'c' || CONSTANT_ADDRESS_P (operands[opnum]))
                  output_addr_const (asm_out_file, operands[opnum]);
                else
                  output_operand (operands[opnum], 'c');
index c9a2089fba5320a4172821ee5210dadd01211ad5..2f100a65f4269c052a151b4cb50c5adcac6b127a 100644 (file)
@@ -1,9 +1,8 @@
 /* PR c/41045 */
 /* { dg-do compile } */
 /* { dg-options "-O0" } */
-/* { dg-additional-options "-fno-pie" { target pie } } */
 
-int v[42], w;
+extern int v[42], w;
 void foo (void);
 
-asm ("# %c0: %c1:" :: ":" (foo), ":" (v), ":" (&w));
+asm ("# %cc0: %cc1:" :: ":" (foo), ":" (v), ":" (&w));
diff --git a/gcc/testsuite/c-c++-common/toplevel-asm-6.c b/gcc/testsuite/c-c++-common/toplevel-asm-6.c
new file mode 100644 (file)
index 0000000..0b77ce5
--- /dev/null
@@ -0,0 +1,9 @@
+/* PR c/41045 */
+/* { dg-do compile } */
+/* { dg-options "-O0" } */
+/* { dg-additional-options "-fPIC" { target fpic } } */
+
+extern int v[42], w;
+void foo (void);
+
+asm ("# %cc0: %cc1:" :: ":" (foo), ":" (v), ":" (&w));