typeof and operands in named address spaces

Uros Bizjak ubizjak@gmail.com
Thu Nov 5 11:03:10 GMT 2020


On Thu, Nov 5, 2020 at 10:36 AM Alexander Monakov <amonakov@ispras.ru> wrote:
>
> On Thu, 5 Nov 2020, Uros Bizjak via Gcc wrote:
>
> > > Looks like writing
> > >
> > >     typeof((typeof(_var))0) tmp__;
> > >
> > > makes it work.  Assumes there's a literal zero for the type of course.
> >
> > This is very limiting assumption, which already breaks for the following test:
>
> To elaborate Richard's idea, you need a way to decay lvalue to rvalue inside
> the typeof to strip the address space; if you need the macro to work for
> more types than just scalar types, the following expression may be useful:
>
>   typeof(0?(_var):(_var))

Great, this works well for various operand types.

> (though there's a bug: +(_var) should also suffice for scalar types, but
>  somehow GCC keeps the address space on the resulting rvalue)
>
> But I wonder if you actually need this at all:

The posted example is a bit naive, because assignment and basic
operations can be implemented directly, e.g.:

__seg_fs int x;

void
test (void)
{
  x &= 1;
}

compiles to:

       andl    $1, %fs:x(%rip)

without any macro usage at all. However, several operations, such as
xadd and cmpxchg are implemented using assembly templates (see e.g.
arch/x86/include/asm/percpu.h), where local instances are needed.

Uros.


More information about the Gcc mailing list