public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [Ada] Fix wrong assignment of overaligned array component
@ 2019-01-26 16:09 Eric Botcazou
  0 siblings, 0 replies; only message in thread
From: Eric Botcazou @ 2019-01-26 16:09 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 731 bytes --]

This is a regression present on all active branches: under a specific set of 
circumstances, the compiler generate wrong code for the assignment of an array 
component of a record type, when the nominal subtype of the component is an
array type with an alignment clause that specifies a larger alignment than the 
natural one.

Tested on x86_64-suse-linux, applied on all active branches.


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>

	* gnat.dg/array34.adb: New test.

-- 
Eric Botcazou

[-- Attachment #2: p.diff --]
[-- Type: text/x-patch, Size: 1483 bytes --]

Index: gcc-interface/trans.c
===================================================================
--- gcc-interface/trans.c	(revision 335507)
+++ gcc-interface/trans.c	(revision 335508)
@@ -7754,12 +7754,17 @@ gnat_to_gnu (Node_Id gnat_node)
 		= real_zerop (gnu_rhs)
 		  ? integer_zero_node
 		  : 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 dest = build_fold_addr_expr (gnu_lhs);
 	      tree t = builtin_decl_explicit (BUILT_IN_MEMSET);
+	      /* 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
@@ -7767,7 +7772,7 @@ gnat_to_gnu (Node_Id gnat_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.  */

[-- Attachment #3: array34.adb --]
[-- Type: text/x-adasrc, Size: 335 bytes --]

--  { 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;

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

only message in thread, other threads:[~2019-01-26 16:04 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-26 16:09 [Ada] Fix wrong assignment of overaligned array component 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).