public inbox for gcc-rust@gcc.gnu.org
 help / color / mirror / Atom feed
* qual_union_type help
@ 2022-02-23 15:50 Philip Herron
  2022-02-23 17:33 ` Eric Botcazou
  0 siblings, 1 reply; 4+ messages in thread
From: Philip Herron @ 2022-02-23 15:50 UTC (permalink / raw)
  To: gcc Mailing List, gcc-rust

Hi everyone

I am seeking some help on using the QUAL_UNION_TYPE it seems ideally
placed to be used for enums in Rust. My initial implementation was
achieved by simply using a union such as:

union my_enum {
  variant_a { enum discriminant; ...data } ;
  variant_b { enum discriminant; ...data} ;
}

Having each variant contain an enum for the discriminant meant that
dataless handling variants were quite simple. Upon trying to use the
QUAL_UNION_TYPE this all seems reasonably trivial but I can't figure
out what to set the DECL_QUALIFIER to be, initially from reading the
documentation on https://gcc.gnu.org/onlinedocs/gccint/Types.html it
seemed that it wants a slightly different layout by using a
placeholder_expr to reference an outer discriminant field such that
the layout becomes:

struct my_enum {
  enum discriminant;
  union variants {
    variant_a { data };
    variant_b { data };
  }
}

So I have been creating my DECL_QUALIFIER as follows:

```
tree field_offset = NULL_TREE;
tree place_holder = build0 (PLACEHOLDER_EXPR, sizetype);
tree place_holder_expr
  = build3 (COMPONENT_REF, TREE_TYPE (qual_field),
        place_holder, qual_field, field_offset);

DECL_QUALIFIER (field)
    = build2 (EQ_EXPR, boolean_type_node,
                        place_holder_expr, discrim);
```

So this seems to generate some interesting code and then finally hit
an ICE inside:

Breakpoint 5, gimplify_expr (expr_p=0x7ffff6ff1f48,
pre_p=0x7fffffffd2d8, post_p=0x7fffffffbb58, gimple_test_f=0x15aa98f
<is_gimple_min_lval(tree_node*)>, fallback=3) at
../../gccrs/gcc/gimplify.cc:15755
15755                 gcc_unreachable ();
(gdb) call debug_tree(*expr_p)
 <placeholder_expr 0x7ffff70116a8
    type <integer_type 0x7ffff700a000 sizetype public unsigned DI
        size <integer_cst 0x7ffff6ff3c00 constant 64>
        unit-size <integer_cst 0x7ffff6ff3c18 constant 8>
        align:64 warn_if_not_align:0 symtab:0 alias-set -1
canonical-type 0x7ffff700a000 precision:64 min <integer_cst
0x7ffff6ff3c30 0> max <integer_cst 0x7ffff6ff44a0
18446744073709551615>>
   >

This makes sense this we can't gimplify a placeholder expression. So
this seems that when i make a constructor for the QUAL_UNION_TYPE i
must iterate the fields and replace the placeholder_expr to have a
reference to the discriminant via another COMPONENT_REF. Though does
this mean for the constructor i will need to create a temporary to
hold onto it to create another component_ref?

Or am I doing this completely wrong? I haven't had much success in
following the QUAL_UNION_TYPE code in the Ada front-end, but any
pointers would be greatly appreciated.

Thanks.

--Phil

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: qual_union_type help
  2022-02-23 15:50 qual_union_type help Philip Herron
@ 2022-02-23 17:33 ` Eric Botcazou
  2022-02-23 17:44   ` Philip Herron
  0 siblings, 1 reply; 4+ messages in thread
From: Eric Botcazou @ 2022-02-23 17:33 UTC (permalink / raw)
  To: Philip Herron; +Cc: gcc Mailing List, gcc-rust

> This makes sense this we can't gimplify a placeholder expression. So
> this seems that when i make a constructor for the QUAL_UNION_TYPE i
> must iterate the fields and replace the placeholder_expr to have a
> reference to the discriminant via another COMPONENT_REF. Though does
> this mean for the constructor i will need to create a temporary to
> hold onto it to create another component_ref?

The Ada compiler gets rid of all PLACEHOLDER_EXPRs in CONSTRUCTORs because the 
subtype of these CONSTRUCTORs is always constrained in Ada parlance, i.e. you 
know the value of the discriminant since it is assigned in the CONSTRUCTOR, so 
the gimplifier is indeed presumably not wired to eliminate them on its own.

-- 
Eric Botcazou



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: qual_union_type help
  2022-02-23 17:33 ` Eric Botcazou
@ 2022-02-23 17:44   ` Philip Herron
  2022-02-23 18:19     ` Eric Botcazou
  0 siblings, 1 reply; 4+ messages in thread
From: Philip Herron @ 2022-02-23 17:44 UTC (permalink / raw)
  To: Eric Botcazou; +Cc: gcc Mailing List, gcc-rust

Hi Eric,

That makes sense during construction we also know what the value of
the discriminant is. What does the Ada front-end replace the
placeholder_exprs with? Can it simply be the value of the discriminant
at constructor? I haven't tried that.

Thanks

--Phil

On Wed, 23 Feb 2022 at 17:33, Eric Botcazou <botcazou@adacore.com> wrote:
>
> > This makes sense this we can't gimplify a placeholder expression. So
> > this seems that when i make a constructor for the QUAL_UNION_TYPE i
> > must iterate the fields and replace the placeholder_expr to have a
> > reference to the discriminant via another COMPONENT_REF. Though does
> > this mean for the constructor i will need to create a temporary to
> > hold onto it to create another component_ref?
>
> The Ada compiler gets rid of all PLACEHOLDER_EXPRs in CONSTRUCTORs because the
> subtype of these CONSTRUCTORs is always constrained in Ada parlance, i.e. you
> know the value of the discriminant since it is assigned in the CONSTRUCTOR, so
> the gimplifier is indeed presumably not wired to eliminate them on its own.
>
> --
> Eric Botcazou
>
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: qual_union_type help
  2022-02-23 17:44   ` Philip Herron
@ 2022-02-23 18:19     ` Eric Botcazou
  0 siblings, 0 replies; 4+ messages in thread
From: Eric Botcazou @ 2022-02-23 18:19 UTC (permalink / raw)
  To: Philip Herron; +Cc: gcc Mailing List, gcc-rust

> That makes sense during construction we also know what the value of
> the discriminant is. What does the Ada front-end replace the
> placeholder_exprs with? Can it simply be the value of the discriminant
> at constructor? I haven't tried that.

Ultimately all PLACEHOLDER_EXPRs need to be replaced by something in the code, 
i.e. they can only survive in (abstract) types.  There is an entire machinery 
in tree.c for that, called both from the front-end and middle-end in Ada.  You 
can replace it with an explicit value (SUBSTITUTE_IN_EXPR) or you can search 
for it in an object (SUBSTITUTE_PLACEHOLDER_IN_EXPR).  You can presumably do 
it through the gimplification hook.

-- 
Eric Botcazou



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-02-23 18:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-23 15:50 qual_union_type help Philip Herron
2022-02-23 17:33 ` Eric Botcazou
2022-02-23 17:44   ` Philip Herron
2022-02-23 18:19     ` Eric Botcazou

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).