public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc(refs/vendors/redhat/heads/gcc-8-branch)] varasm: Fix output_constructor where a RANGE_EXPR index needs to skip some elts [PR94303]
@ 2020-09-17 17:20 Jakub Jelinek
  0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2020-09-17 17:20 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:042d9c31cb79d7706b49039e8ba92b85a398629d

commit 042d9c31cb79d7706b49039e8ba92b85a398629d
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Tue Apr 7 20:59:37 2020 +0200

    varasm: Fix output_constructor where a RANGE_EXPR index needs to skip some elts [PR94303]
    
    The following testcase is miscompiled, because output_constructor doesn't
    output the initializer correctly.  The FE creates {[1...2] = 9} in this
    case, and we emit .long 9; long 9; .zero 8 instead of the expected
    .zero 8; .long 9; .long 9.  If the CONSTRUCTOR is {[1] = 9, [2] = 9},
    output_constructor_regular_field has code to notice that the current
    location (local->total_bytes) is smaller than the location we want to write
    to (1*sizeof(elt)) and will call assemble_zeros to skip those.  But
    RANGE_EXPRs are handled by a different function which didn't do this,
    so for RANGE_EXPRs we emitted them properly only if local->total_bytes
    was always equal to the location where the RANGE_EXPR needs to start.
    
    2020-03-25  Jakub Jelinek  <jakub@redhat.com>
    
            PR middle-end/94303
            * varasm.c (output_constructor_array_range): If local->index
            RANGE_EXPR doesn't start at the current location in the constructor,
            skip needed number of bytes using assemble_zeros or assert we don't
            go backwards.
    
            PR middle-end/94303
            * g++.dg/torture/pr94303.C: New test.
    
    (cherry picked from commit 56407bab53a514ffcd6ac011965cebdc5eb3ef54)

Diff:
---
 gcc/testsuite/g++.dg/torture/pr94303.C | 17 +++++++++++++++++
 gcc/varasm.c                           | 20 ++++++++++++++++++++
 2 files changed, 37 insertions(+)

diff --git a/gcc/testsuite/g++.dg/torture/pr94303.C b/gcc/testsuite/g++.dg/torture/pr94303.C
new file mode 100644
index 00000000000..45b90a2249d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/torture/pr94303.C
@@ -0,0 +1,17 @@
+// PR middle-end/94303
+// { dg-do run }
+
+struct A {
+  int d = 9;
+  A () = default;
+  A (int x) : d(x) {}
+  void foo () { if (d < 1) __builtin_abort (); }
+};
+
+A a[3] = { 1 };
+
+int
+main ()
+{
+  a[2].foo ();
+}
diff --git a/gcc/varasm.c b/gcc/varasm.c
index adf279152b4..f5ca6c2d9af 100644
--- a/gcc/varasm.c
+++ b/gcc/varasm.c
@@ -5069,6 +5069,26 @@ struct oc_local_state {
 static void
 output_constructor_array_range (oc_local_state *local)
 {
+  /* Perform the index calculation in modulo arithmetic but
+     sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
+     but we are using an unsigned sizetype.  */
+  unsigned prec = TYPE_PRECISION (sizetype);
+  offset_int idx = wi::sext (wi::to_offset (TREE_OPERAND (local->index, 0))
+			     - wi::to_offset (local->min_index), prec);
+  tree valtype = TREE_TYPE (local->val);
+  HOST_WIDE_INT fieldpos
+    = (idx * wi::to_offset (TYPE_SIZE_UNIT (valtype))).to_short_addr ();
+
+  /* Advance to offset of this element.  */
+  if (fieldpos > local->total_bytes)
+    {
+      assemble_zeros (fieldpos - local->total_bytes);
+      local->total_bytes = fieldpos;
+    }
+  else
+    /* Must not go backwards.  */
+    gcc_assert (fieldpos == local->total_bytes);
+
   unsigned HOST_WIDE_INT fieldsize
     = int_size_in_bytes (TREE_TYPE (local->type));


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-09-17 17:20 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-17 17:20 [gcc(refs/vendors/redhat/heads/gcc-8-branch)] varasm: Fix output_constructor where a RANGE_EXPR index needs to skip some elts [PR94303] Jakub Jelinek

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).