]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
varasm: Fix output_constructor where a RANGE_EXPR index needs to skip some elts ...
authorJakub Jelinek <jakub@redhat.com>
Tue, 7 Apr 2020 18:59:37 +0000 (20:59 +0200)
committerJakub Jelinek <jakub@redhat.com>
Tue, 7 Apr 2020 18:59:37 +0000 (20:59 +0200)
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.

gcc/ChangeLog
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/torture/pr94303.C [new file with mode: 0644]
gcc/varasm.c

index e52fcf6707b3bf39b516cb3d82d7ef32d49476ae..4e84e4a69fad608587fad2519c65d5b7470e1575 100644 (file)
@@ -1,14 +1,20 @@
 2020-04-07  Jakub Jelinek  <jakub@redhat.com>
 
        Backported from mainline
+       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.
+
        2020-03-25  Richard Biener  <rguenther@suse.de>
                    Jakub Jelinek  <jakub@redhat.com>
 
        PR debug/94283
        * tree-if-conv.c (ifcvt_local_dce): Delete dead statements backwards.
 
-2020-04-07  Jakub Jelinek  <jakub@redhat.com>
-
        2020-03-24  Jakub Jelinek  <jakub@redhat.com>
 
        PR debug/94283
index a36c7d85fae5cfc264a306e35bd7d2b4b6a521ef..d0c4d51028089fa51ac79a4613563196be0d8570 100644 (file)
@@ -3,6 +3,9 @@
        Backported from mainline
        2020-03-25  Jakub Jelinek  <jakub@redhat.com>
 
+       PR middle-end/94303
+       * g++.dg/torture/pr94303.C: New test.
+
        PR debug/94283
        * gcc.dg/pr94283.c: New test.
 
diff --git a/gcc/testsuite/g++.dg/torture/pr94303.C b/gcc/testsuite/g++.dg/torture/pr94303.C
new file mode 100644 (file)
index 0000000..45b90a2
--- /dev/null
@@ -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 ();
+}
index 8f006dcff671ab0c75758e675a072dcb5c3f1307..8d87e28cc7c515a7ba0d8341385eadc69aebec33 100644 (file)
@@ -5136,6 +5136,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));