* [Ada] Fix biased integer arithmetic
@ 2020-11-11 14:13 Eric Botcazou
0 siblings, 0 replies; only message in thread
From: Eric Botcazou @ 2020-11-11 14:13 UTC (permalink / raw)
To: gcc-patches
[-- Attachment #1: Type: text/plain, Size: 498 bytes --]
The Ada compiler uses a biased representation when a size clause reserves
fewer bits than normal either for the lower or for the upper bound.
Tested on x86-64/Linux, applied on the mainline, 10 and 9 branches.
2020-11-11 Eric Botcazou <ebotcazou@adacore.com>
* gcc-interface/trans.c (build_binary_op_trapv): Convert operands
to the result type before doing generic overflow checking.
2020-11-11 Eric Botcazou <ebotcazou@adacore.com>
* gnat.dg/bias2.adb: New test.
--
Eric Botcazou
[-- Attachment #2: bias2.adb --]
[-- Type: text/x-adasrc, Size: 486 bytes --]
-- { dg-do run }
procedure Bias2 is
type Biased_T is range 1 .. 2 ** 6;
for Biased_T'Size use 6; -- { dg-warning "biased representation" }
X, Y : Biased_T;
begin
X := 1;
Y := 1;
if X + Y /= 2 then
raise Program_Error;
end if;
X := 2;
Y := 1;
if X - Y /= 1 then
raise Program_Error;
end if;
X := 2;
Y := 3;
if X * Y /= 6 then
raise Program_Error;
end if;
X := 24;
Y := 3;
if X / Y /= 8 then
raise Program_Error;
end if;
end;
[-- Attachment #3: p.diff --]
[-- Type: text/x-patch, Size: 809 bytes --]
diff --git a/gcc/ada/gcc-interface/trans.c b/gcc/ada/gcc-interface/trans.c
index 065fcd2f956..7be8463d32b 100644
--- a/gcc/ada/gcc-interface/trans.c
+++ b/gcc/ada/gcc-interface/trans.c
@@ -9361,6 +9361,11 @@ build_binary_op_trapv (enum tree_code code, tree gnu_type, tree left,
/* If no operand is a constant, we use the generic implementation. */
if (TREE_CODE (lhs) != INTEGER_CST && TREE_CODE (rhs) != INTEGER_CST)
{
+ /* First convert the operands to the result type like build_binary_op.
+ This is where the bias is made explicit for biased types. */
+ lhs = convert (gnu_type, lhs);
+ rhs = convert (gnu_type, rhs);
+
/* Never inline a 64-bit mult for a 32-bit target, it's way too long. */
if (code == MULT_EXPR && precision == 64 && BITS_PER_WORD < 64)
{
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2020-11-11 14:13 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-11 14:13 [Ada] Fix biased integer arithmetic 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).