public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Fix internal error on small array with negative lower bound
@ 2023-05-18  9:28 Eric Botcazou
  2023-05-18 12:02 ` Richard Biener
  0 siblings, 1 reply; 4+ messages in thread
From: Eric Botcazou @ 2023-05-18  9:28 UTC (permalink / raw)
  To: gcc-patches

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

Hi,

Ada supports arrays with negative indices, although the internal index type is
sizetype like in other languages, which is unsigned.  This means that negative
values are represented by very large numbers, which works with a bit of care.
The attached test exposes a small loophole in output_constructor_bitfield.

Tested on x86-64/Linux, OK for the mainline?


2023-05-18  Eric Botcazou <ebotcazou@adacore.com>

	* varasm.cc (output_constructor_bitfield): Call tree_to_uhwi instead
	of tree_to_shwi on array indices.  Minor tweaks.


2023-05-18  Eric Botcazou <ebotcazou@adacore.com>

	* gnat.dg/specs/array6.ads: New test.

-- 
Eric Botcazou

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

diff --git a/gcc/varasm.cc b/gcc/varasm.cc
index 2256194d934..478cbfe6736 100644
--- a/gcc/varasm.cc
+++ b/gcc/varasm.cc
@@ -5585,19 +5585,18 @@ output_constructor_bitfield (oc_local_state *local, unsigned int bit_offset)
 
   /* Relative index of this element if this is an array component.  */
   HOST_WIDE_INT relative_index
-    = (!local->field
-       ? (local->index
-	  ? (tree_to_shwi (local->index)
-	     - tree_to_shwi (local->min_index))
-	  : local->last_relative_index + 1)
-       : 0);
+    = (local->field
+       ? 0
+       : (local->index
+	  ? tree_to_uhwi (local->index) - tree_to_uhwi (local->min_index)
+	  : local->last_relative_index + 1));
 
   /* Bit position of this element from the start of the containing
      constructor.  */
   HOST_WIDE_INT constructor_relative_ebitpos
-      = (local->field
-	 ? int_bit_position (local->field)
-	 : ebitsize * relative_index);
+    = (local->field
+       ? int_bit_position (local->field)
+       : ebitsize * relative_index);
 
   /* Bit position of this element from the start of a possibly ongoing
      outer byte buffer.  */

[-- Attachment #3: array6.ads --]
[-- Type: text/x-adasrc, Size: 288 bytes --]

-- { dg-do compile }

package Array6 is 

  type Range_Type is range -10 ..  10;
  type Array_Type is array (Range_Type range <> ) of Short_Short_Integer;

  type Record_Type is record 
    A : Array_Type(-2..4);
  end record ;

  Rec : Record_Type := (A => (others => -1));

end Array6;

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] Fix internal error on small array with negative lower bound
  2023-05-18  9:28 [PATCH] Fix internal error on small array with negative lower bound Eric Botcazou
@ 2023-05-18 12:02 ` Richard Biener
  2023-05-18 17:44   ` Eric Botcazou
  0 siblings, 1 reply; 4+ messages in thread
From: Richard Biener @ 2023-05-18 12:02 UTC (permalink / raw)
  To: Eric Botcazou; +Cc: gcc-patches

On Thu, May 18, 2023 at 11:51 AM Eric Botcazou via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> Hi,
>
> Ada supports arrays with negative indices, although the internal index type is
> sizetype like in other languages, which is unsigned.  This means that negative
> values are represented by very large numbers, which works with a bit of care.
> The attached test exposes a small loophole in output_constructor_bitfield.
>
> Tested on x86-64/Linux, OK for the mainline?

Would it be better to use

  wi::to_uhwi (wi::to_wide (local->index) - wi::to_wide (local->min_index))

to honor the actual sign of the indices?  I think nothing forbids frontends to
use a signed TYPE_DOMAIN here?  But the difference should be always
representable in an unsigned value of course.

>
> 2023-05-18  Eric Botcazou <ebotcazou@adacore.com>
>
>         * varasm.cc (output_constructor_bitfield): Call tree_to_uhwi instead
>         of tree_to_shwi on array indices.  Minor tweaks.
>
>
> 2023-05-18  Eric Botcazou <ebotcazou@adacore.com>
>
>         * gnat.dg/specs/array6.ads: New test.
>
> --
> Eric Botcazou

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] Fix internal error on small array with negative lower bound
  2023-05-18 12:02 ` Richard Biener
@ 2023-05-18 17:44   ` Eric Botcazou
  2023-05-18 18:24     ` Richard Biener
  0 siblings, 1 reply; 4+ messages in thread
From: Eric Botcazou @ 2023-05-18 17:44 UTC (permalink / raw)
  To: Richard Biener; +Cc: gcc-patches

> Would it be better to use
> 
>   wi::to_uhwi (wi::to_wide (local->index) - wi::to_wide (local->min_index))
> 
> to honor the actual sign of the indices?  I think nothing forbids frontends
> to use a signed TYPE_DOMAIN here?  But the difference should be always
> representable in an unsigned value of course.

We use tree_to_uhwi everywhere else though, see categorize_ctor_elements_1:

	  if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
	    mult = (tree_to_uhwi (hi_index)
		    - tree_to_uhwi (lo_index) + 1);

or store_constructor

		    this_node_count = (tree_to_uhwi (hi_index)
			       - tree_to_uhwi (lo_index) + 1);

so the proposed form looks better for the sake of consistency.

-- 
Eric Botcazou



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] Fix internal error on small array with negative lower bound
  2023-05-18 17:44   ` Eric Botcazou
@ 2023-05-18 18:24     ` Richard Biener
  0 siblings, 0 replies; 4+ messages in thread
From: Richard Biener @ 2023-05-18 18:24 UTC (permalink / raw)
  To: Eric Botcazou; +Cc: gcc-patches



> Am 18.05.2023 um 19:44 schrieb Eric Botcazou <botcazou@adacore.com>:
> 
> 
>> 
>> Would it be better to use
>> 
>>  wi::to_uhwi (wi::to_wide (local->index) - wi::to_wide (local->min_index))
>> 
>> to honor the actual sign of the indices?  I think nothing forbids frontends
>> to use a signed TYPE_DOMAIN here?  But the difference should be always
>> representable in an unsigned value of course.
> 
> We use tree_to_uhwi everywhere else though, see categorize_ctor_elements_1:
> 
>      if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
>        mult = (tree_to_uhwi (hi_index)
>            - tree_to_uhwi (lo_index) + 1);
> 
> or store_constructor
> 
>            this_node_count = (tree_to_uhwi (hi_index)
>                   - tree_to_uhwi (lo_index) + 1);
> 
> so the proposed form looks better for the sake of consistency.

Ok, thanks for checking.

Richard 

> -- 
> Eric Botcazou
> 
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-05-18 18:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-18  9:28 [PATCH] Fix internal error on small array with negative lower bound Eric Botcazou
2023-05-18 12:02 ` Richard Biener
2023-05-18 17:44   ` Eric Botcazou
2023-05-18 18:24     ` Richard Biener

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