public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Marc Poulhiès" <poulhies@adacore.com>
To: gcc-patches@gcc.gnu.org
Cc: Eric Botcazou <ebotcazou@adacore.com>
Subject: [COMMITTED] ada: Fix resolution of mod operator of System.Storage_Elements
Date: Tue, 23 May 2023 10:08:45 +0200	[thread overview]
Message-ID: <20230523080845.1874226-1-poulhies@adacore.com> (raw)

From: Eric Botcazou <ebotcazou@adacore.com>

This operator is special because the left operand is of Address type while
the right operand and the result are of Storage_Offset type (but we raise
Constraint_Error if the right operand is not positive) and it needs to be
resolved to the type of the left operand to implement the correct semantics.

gcc/ada/

	* exp_ch4.adb (Expand_N_Op_Mod): Adjust the detection of the special
	operator of System.Storage_Elements.  Do not rewrite it into a rem.
	* sem_res.adb (Resolve_Intrinsic_Operator): Use the base type of the
	left operand for the special mod operator of System.Storage_Elements

Tested on x86_64-pc-linux-gnu, committed on master.

---
 gcc/ada/exp_ch4.adb | 11 +++++++----
 gcc/ada/sem_res.adb | 23 +++++++++++++++++++----
 2 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/gcc/ada/exp_ch4.adb b/gcc/ada/exp_ch4.adb
index c974a9e8d44..c7727904df2 100644
--- a/gcc/ada/exp_ch4.adb
+++ b/gcc/ada/exp_ch4.adb
@@ -9561,9 +9561,10 @@ package body Exp_Ch4 is
       DDC   : constant Boolean    := Do_Division_Check (N);
 
       Is_Stoele_Mod : constant Boolean :=
-        Is_RTE (First_Subtype (Typ), RE_Storage_Offset)
-          and then Nkind (Left_Opnd (N)) = N_Unchecked_Type_Conversion
-          and then Is_RTE (Etype (Expression (Left_Opnd (N))), RE_Address);
+        Is_RTE (Typ, RE_Address)
+          and then Nkind (Right_Opnd (N)) = N_Unchecked_Type_Conversion
+          and then
+            Is_RTE (Etype (Expression (Right_Opnd (N))), RE_Storage_Offset);
       --  True if this is the special mod operator of System.Storage_Elements
 
       Left  : Node_Id;
@@ -9633,6 +9634,7 @@ package body Exp_Ch4 is
         and then ((Llo >= 0 and then Rlo >= 0)
                      or else
                   (Lhi <= 0 and then Rhi <= 0))
+        and then not Is_Stoele_Mod
       then
          Rewrite (N,
            Make_Op_Rem (Sloc (N),
@@ -9683,7 +9685,8 @@ package body Exp_Ch4 is
               Make_Raise_Constraint_Error (Loc,
                 Condition =>
                   Make_Op_Le (Loc,
-                    Left_Opnd  => Duplicate_Subexpr_No_Checks (Right),
+                    Left_Opnd  =>
+                      Duplicate_Subexpr_No_Checks (Expression (Right)),
                     Right_Opnd => Make_Integer_Literal (Loc, 0)),
                 Reason => CE_Overflow_Check_Failed));
             return;
diff --git a/gcc/ada/sem_res.adb b/gcc/ada/sem_res.adb
index a99bed00118..f1d9a97452a 100644
--- a/gcc/ada/sem_res.adb
+++ b/gcc/ada/sem_res.adb
@@ -9710,10 +9710,19 @@ package body Sem_Res is
    --------------------------------
 
    procedure Resolve_Intrinsic_Operator  (N : Node_Id; Typ : Entity_Id) is
-      Btyp : constant Entity_Id := Implementation_Base_Type (Typ);
-      Op   : Entity_Id;
-      Arg1 : Node_Id;
-      Arg2 : Node_Id;
+      Is_Stoele_Mod : constant Boolean :=
+        Nkind (N) = N_Op_Mod
+          and then Is_RTE (First_Subtype (Typ), RE_Storage_Offset)
+          and then Is_RTE (Etype (Left_Opnd (N)), RE_Address);
+      --  True if this is the special mod operator of System.Storage_Elements,
+      --  which needs to be resolved to the type of the left operand in order
+      --  to implement the correct semantics.
+
+      Btyp : constant Entity_Id :=
+        (if Is_Stoele_Mod
+          then Implementation_Base_Type (Etype (Left_Opnd (N)))
+          else Implementation_Base_Type (Typ));
+      --  The base type to be used for the operator
 
       function Convert_Operand (Opnd : Node_Id) return Node_Id;
       --  If the operand is a literal, it cannot be the expression in a
@@ -9742,6 +9751,12 @@ package body Sem_Res is
          return Res;
       end Convert_Operand;
 
+      --  Local variables
+
+      Arg1 : Node_Id;
+      Arg2 : Node_Id;
+      Op   : Entity_Id;
+
    --  Start of processing for Resolve_Intrinsic_Operator
 
    begin
-- 
2.40.0


                 reply	other threads:[~2023-05-23  8:08 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230523080845.1874226-1-poulhies@adacore.com \
    --to=poulhies@adacore.com \
    --cc=ebotcazou@adacore.com \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).