[Bug inline-asm/90181] Feature request: provide a way to explicitly select specific named registers in constraints

2019-04-23 Thread segher at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90181

--- Comment #7 from Segher Boessenkool  ---
(In reply to nfxjfg from comment #6)
> Yes, it's clear that that the constraint can't be _just_ the register name,
> since they'll clash with builtin constraints now or with future
> architectures (which may add arbitrary register names). The proposed
> "*registername" is pretty nice, though. Having this would be great.

Hrm, "*" already has a meaning with current GCC (it essentially is ignored
in inline asm)...  It might be better to have some new syntax that gives an
error with older GCC.

> I didn't find a RISC-V builtin for ecall (maybe I looked in the wrong
> place). That wouldbn't be sufficient anyway.

Right, you would need a builtin for every calling convention for syscalls.
The aren't too many of those though?

> Another situation where I
> wanted to specify many fixed register constraints was a piece of inline code
> that did some syscalls without touching the stack (it needed all inputs as
> registers, and in specific registers, and have some registers for free use
> by the asm code itself).

A biggish piece of asm like that might be better as actual assembler code
than as inline asm, you may want to consider that?

[Bug inline-asm/90181] Feature request: provide a way to explicitly select specific named registers in constraints

2019-04-22 Thread nfxjfg at googlemail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90181

--- Comment #6 from nfxjfg at googlemail dot com ---
Yes, it's clear that that the constraint can't be _just_ the register name,
since they'll clash with builtin constraints now or with future architectures
(which may add arbitrary register names). The proposed "*registername" is
pretty nice, though. Having this would be great.

I didn't find a RISC-V builtin for ecall (maybe I looked in the wrong place).
That wouldbn't be sufficient anyway. Another situation where I wanted to
specify many fixed register constraints was a piece of inline code that did
some syscalls without touching the stack (it needed all inputs as registers,
and in specific registers, and have some registers for free use by the asm code
itself).

Throwing macros at the problem does help to reduce the duplication, but I think
this proposed extension would solve this in a better way.

[Bug inline-asm/90181] Feature request: provide a way to explicitly select specific named registers in constraints

2019-04-20 Thread pinskia at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90181

--- Comment #5 from Andrew Pinski  ---
Actually it is not as bad if you use preprocessor tricks.

Like this code from glibc for aarch64:

# undef INTERNAL_SYSCALL_RAW
# define INTERNAL_SYSCALL_RAW(name, err, nr, args...)   \
  ({ long _sys_result;  \
 {  \
   LOAD_ARGS_##nr (args)\
   register long _x8 asm ("x8") = (name);   \
   asm volatile ("svc   0   // syscall " # name \
 : "=r" (_x0) : "r"(_x8) ASM_ARGS_##nr : "memory"); \
   _sys_result = _x0;   \
 }  \
 _sys_result; })

# define LOAD_ARGS_0()  \
  register long _x0 asm ("x0");
# define LOAD_ARGS_1(x0)\
  long _x0tmp = (long) (x0);\
  LOAD_ARGS_0 ()\
  _x0 = _x0tmp;
# define LOAD_ARGS_2(x0, x1)\
  long _x1tmp = (long) (x1);\
  LOAD_ARGS_1 (x0)  \
  register long _x1 asm ("x1") = _x1tmp;
# define LOAD_ARGS_3(x0, x1, x2)\
  long _x2tmp = (long) (x2);\
  LOAD_ARGS_2 (x0, x1)  \
  register long _x2 asm ("x2") = _x2tmp;
# define LOAD_ARGS_4(x0, x1, x2, x3)\
  long _x3tmp = (long) (x3);\
  LOAD_ARGS_3 (x0, x1, x2)  \
  register long _x3 asm ("x3") = _x3tmp;
# define LOAD_ARGS_5(x0, x1, x2, x3, x4)\
  long _x4tmp = (long) (x4);\
  LOAD_ARGS_4 (x0, x1, x2, x3)  \
  register long _x4 asm ("x4") = _x4tmp;
# define LOAD_ARGS_6(x0, x1, x2, x3, x4, x5)\
  long _x5tmp = (long) (x5);\
  LOAD_ARGS_5 (x0, x1, x2, x3, x4)  \
  register long _x5 asm ("x5") = _x5tmp;
# define LOAD_ARGS_7(x0, x1, x2, x3, x4, x5, x6)\
  long _x6tmp = (long) (x6);\
  LOAD_ARGS_6 (x0, x1, x2, x3, x4, x5)  \
  register long _x6 asm ("x6") = _x6tmp;

# define ASM_ARGS_0
# define ASM_ARGS_1 , "r" (_x0)
# define ASM_ARGS_2 ASM_ARGS_1, "r" (_x1)
# define ASM_ARGS_3 ASM_ARGS_2, "r" (_x2)
# define ASM_ARGS_4 ASM_ARGS_3, "r" (_x3)
# define ASM_ARGS_5 ASM_ARGS_4, "r" (_x4)
# define ASM_ARGS_6 ASM_ARGS_5, "r" (_x5)
# define ASM_ARGS_7 ASM_ARGS_6, "r" (_x6)

[Bug inline-asm/90181] Feature request: provide a way to explicitly select specific named registers in constraints

2019-04-20 Thread segher at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90181

Segher Boessenkool  changed:

   What|Removed |Added

 Status|RESOLVED|REOPENED
   Last reconfirmed||2019-04-20
 CC||segher at gcc dot gnu.org
 Resolution|WONTFIX |---
 Ever confirmed|0   |1

--- Comment #4 from Segher Boessenkool  ---
(In reply to nfxjfg from comment #0)
> Currently, inline assembler constraints have no way to select an explicitly
> named register. Apparently you're supposed to use register variables. There
> is even text that register variables exist only for this use case.

That is not what it says.  Originally a local register variable lived in
the specified register always.  This quickly was found out to not really
work.  After many years it was finally documented as just not supported
for anything but assembler operands.

Hopefully it will actually do *only* this in the not too far future.  We
should be able to make (almost) all gotchas here magically disappear.

> For example, suppose you want to pass something through the register a7 on
> the RISC-V platform. You need to do:
> 
>   void call_ecall(size_t num)
>   {
> register size_t r_a7 __asm("a7") = num;
> __asm volatile("ecall" : : "r" (r_a7) : "memory");
>   }
> 
> This gets awkward fast. It adds a lot of extra noise if you have many
> registers to pass (the ecall instruction provides an example where this may
> be needed).

Does the riscv port not have a builtin for this?

> The semantics are also not entirely clear: will r_a7 occupy the a7 register
> for the entire function

It does not matter: you are only allowed to pass it to the asm, and nothing
else is defined behaviour.

>   void call_ecall(size_t num)
>   {
> __asm volatile("ecall" : : "a7" (num) : "memory");
>   }

Because a7 is not a constraint.  It also cannot *be* one, in general;
for example, many archs have a register "r0" but the constraint "r0"
already means something else.

So we need some new syntax for this.  I suggested "*a7" before.

Confirmed.  It's a reasonable request, and it is a feature that would make
GCC better, and isn't too hard to define or implement.  Reopening.

[Bug inline-asm/90181] Feature request: provide a way to explicitly select specific named registers in constraints

2019-04-19 Thread nfxjfg at googlemail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90181

--- Comment #3 from nfxjfg at googlemail dot com ---
Yes, it's documented this way, but it makes it appear all kinds of fragile. For
one, I normally expect that the compiler will reorder and interleave any
statements in my code (because that's what compilers always tend to do from a
user's POV), so it seems weird that it works at all, since there is nothing
that strictly associates the register declaration and the asm block. Yes, the
gcc docs (the part you quoted  in particular) sort of make it clear that it's
simply the lexical order of operations that matters here. In the gcc example
for example, the compiler can't just move the initialization of t1 below the p1
initialization, although with normal code it can and will do that.

Anyway, that is not the central point of this feature request. I only brought
it up as an argument that register variables are confusing and roundabout.

The central point of this feature request is that I'm asking for a way to
specify a named register in a contraint directly, instead of having to use this
detour over register variables. Can I ask why this feature request has been
rejected?

[Bug inline-asm/90181] Feature request: provide a way to explicitly select specific named registers in constraints

2019-04-19 Thread pinskia at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90181

Andrew Pinski  changed:

   What|Removed |Added

 Status|UNCONFIRMED |RESOLVED
 Resolution|--- |WONTFIX

--- Comment #2 from Andrew Pinski  ---
I think the manual is clear (when it comes to the following questions:
will r_a7 occupy the a7 register for the entire function (suppose there is more
C code around it)? What if call_ecall gets inlined into a larger function?
)
https://gcc.gnu.org/onlinedocs/gcc/Local-Register-Variables.html#Local-Register-Variables

Warning: In the above example, be aware that a register (for example r0) can be
call-clobbered by subsequent code, including function calls and library calls
for arithmetic operators on other variables (for example the initialization of
p2).

[Bug inline-asm/90181] Feature request: provide a way to explicitly select specific named registers in constraints

2019-04-19 Thread sch...@linux-m68k.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90181

--- Comment #1 from Andreas Schwab  ---
x86 doesn't support this either.  It just happens to have a few register
classes that consist of a single register, but only because of ISA constraints.