]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
trans.c (gnat_to_gnu): Use DECL_SIZE_UNIT instead of TYPE_SIZE_UNIT for the size...
authorEric Botcazou <ebotcazou@adacore.com>
Sat, 26 Jan 2019 16:03:42 +0000 (16:03 +0000)
committerEric Botcazou <ebotcazou@gcc.gnu.org>
Sat, 26 Jan 2019 16:03:42 +0000 (16:03 +0000)
* gcc-interface/trans.c (gnat_to_gnu) <N_Assignment_Statement>: Use
DECL_SIZE_UNIT instead of TYPE_SIZE_UNIT for the size to be assigned
by a call to memset if the LHS is a DECL.

From-SVN: r268298

gcc/ada/ChangeLog
gcc/ada/gcc-interface/trans.c
gcc/testsuite/ChangeLog
gcc/testsuite/gnat.dg/array34.adb [new file with mode: 0644]

index df465b9edb840281a5bfd53e079566b60370b0aa..27719e69558278714b7f14859ea38a90c232faa4 100644 (file)
@@ -1,3 +1,9 @@
+2019-01-26  Eric Botcazou  <ebotcazou@adacore.com>
+
+       * gcc-interface/trans.c (gnat_to_gnu) <N_Assignment_Statement>: Use
+       DECL_SIZE_UNIT instead of TYPE_SIZE_UNIT for the size to be assigned
+       by a call to memset if the LHS is a DECL.
+
 2019-01-26  Eric Botcazou  <ebotcazou@adacore.com>
 
        * gcc-interface/trans.c (struct loop_info_d): Remove artificial field.
index 25bae750fbbb82452a702e802371d2165e06b218..c2c385d7030859dcf537a2dae1e2d950ee55d368 100644 (file)
@@ -6904,21 +6904,29 @@ gnat_to_gnu (Node_Id gnat_node)
          /* Or else, use memset when the conditions are met.  */
          else if (use_memset_p)
            {
-             tree value = fold_convert (integer_type_node, gnu_rhs);
-             tree to = gnu_lhs;
-             tree type = TREE_TYPE (to);
-             tree size
-               = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (type), to);
-             tree to_ptr = build_fold_addr_expr (to);
+             tree value
+               = real_zerop (gnu_rhs)
+                 ? integer_zero_node
+                 : fold_convert (integer_type_node, gnu_rhs);
+             tree dest = build_fold_addr_expr (gnu_lhs);
              tree t = builtin_decl_explicit (BUILT_IN_MEMSET);
-             if (TREE_CODE (value) == INTEGER_CST)
+             /* Be extra careful not to write too much data.  */
+             tree size;
+             if (TREE_CODE (gnu_lhs) == COMPONENT_REF)
+               size = DECL_SIZE_UNIT (TREE_OPERAND (gnu_lhs, 1));
+             else if (DECL_P (gnu_lhs))
+               size = DECL_SIZE_UNIT (gnu_lhs);
+             else
+               size = TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs));
+             size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_lhs);
+             if (TREE_CODE (value) == INTEGER_CST && !integer_zerop (value))
                {
                  tree mask
                    = build_int_cst (integer_type_node,
                                     ((HOST_WIDE_INT) 1 << BITS_PER_UNIT) - 1);
                  value = int_const_binop (BIT_AND_EXPR, value, mask);
                }
-             gnu_result = build_call_expr (t, 3, to_ptr, value, size);
+             gnu_result = build_call_expr (t, 3, dest, value, size);
            }
 
          /* Otherwise build a regular assignment.  */
index dfa212eafdeb71a0f100a9813a27983a55e9586e..acb417983cdd6ab3034feb231467429d6852fcf5 100644 (file)
@@ -1,3 +1,7 @@
+2019-01-26  Eric Botcazou  <ebotcazou@adacore.com>
+
+       * gnat.dg/array34.adb: New test.
+
 2019-01-26  Dominique d'Humieres  <dominiq@gcc.gnu.org>
 
        PR fortran/85579
diff --git a/gcc/testsuite/gnat.dg/array34.adb b/gcc/testsuite/gnat.dg/array34.adb
new file mode 100644 (file)
index 0000000..62809cd
--- /dev/null
@@ -0,0 +1,22 @@
+--  { dg-do run }
+
+procedure Array34 is
+
+  type Arr is array (1 .. 6) of Short_Short_Integer;
+  for Arr'Alignment use 4;
+
+  type Rec is record
+    A : Arr;
+    B: Short_Integer;
+  end record;
+  pragma Pack (Rec);
+
+  R : Rec;
+
+begin
+  R.B := 31415;
+  R.A := (others => 0);
+  if R.B /= 31415 then
+    raise Program_Error;
+  end if;
+end;