public inbox for gcc-rust@gcc.gnu.org
 help / color / mirror / Atom feed
From: Philip Herron <philip.herron@embecosm.com>
To: gcc Mailing List <gcc@gcc.gnu.org>, gcc-rust@gcc.gnu.org
Subject: qual_union_type help
Date: Wed, 23 Feb 2022 15:50:28 +0000	[thread overview]
Message-ID: <CAB2u+n2DZ1anAGJJgkWVBFjT8SLf-oC4JJiaBaAHs9bTJHYakQ@mail.gmail.com> (raw)

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

             reply	other threads:[~2022-02-23 15:50 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-23 15:50 Philip Herron [this message]
2022-02-23 17:33 ` Eric Botcazou
2022-02-23 17:44   ` Philip Herron
2022-02-23 18:19     ` Eric Botcazou

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAB2u+n2DZ1anAGJJgkWVBFjT8SLf-oC4JJiaBaAHs9bTJHYakQ@mail.gmail.com \
    --to=philip.herron@embecosm.com \
    --cc=gcc-rust@gcc.gnu.org \
    --cc=gcc@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).