[gcc r9-9039] Fix biased integer arithmetic

Eric Botcazou ebotcazou@gcc.gnu.org
Wed Nov 11 14:12:56 GMT 2020


https://gcc.gnu.org/g:d8157e053f397327a45851fd584f3e906770c629

commit r9-9039-gd8157e053f397327a45851fd584f3e906770c629
Author: Eric Botcazou <ebotcazou@adacore.com>
Date:   Wed Nov 11 15:08:16 2020 +0100

    Fix biased integer arithmetic
    
    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.
    
    gcc/ada/ChangeLog:
            * gcc-interface/trans.c (build_binary_op_trapv): Convert operands
            to the result type before doing generic overflow checking.
            * gcc-interface/utils.c (can_materialize_object_renaming_p): Add
            pair of missing parentheses.
    
    gcc/testsuite/ChangeLog:
            * gnat.dg/bias2.adb: New test.

Diff:
---
 gcc/ada/gcc-interface/trans.c   |  5 +++++
 gcc/ada/gcc-interface/utils.c   |  2 +-
 gcc/testsuite/gnat.dg/bias2.adb | 33 +++++++++++++++++++++++++++++++++
 3 files changed, 39 insertions(+), 1 deletion(-)

diff --git a/gcc/ada/gcc-interface/trans.c b/gcc/ada/gcc-interface/trans.c
index 4e589e4335b..0ba3ad97ba6 100644
--- a/gcc/ada/gcc-interface/trans.c
+++ b/gcc/ada/gcc-interface/trans.c
@@ -9801,6 +9801,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)
 	{
diff --git a/gcc/ada/gcc-interface/utils.c b/gcc/ada/gcc-interface/utils.c
index 2290f0413d5..98542fc1001 100644
--- a/gcc/ada/gcc-interface/utils.c
+++ b/gcc/ada/gcc-interface/utils.c
@@ -5624,7 +5624,7 @@ can_materialize_object_renaming_p (Node_Id expr)
     {
       expr = Original_Node (expr);
 
-      switch Nkind (expr)
+      switch (Nkind (expr))
 	{
 	case N_Identifier:
 	case N_Expanded_Name:
diff --git a/gcc/testsuite/gnat.dg/bias2.adb b/gcc/testsuite/gnat.dg/bias2.adb
new file mode 100644
index 00000000000..a32e9a337ad
--- /dev/null
+++ b/gcc/testsuite/gnat.dg/bias2.adb
@@ -0,0 +1,33 @@
+-- { 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;


More information about the Gcc-cvs mailing list