]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
[Ada] Crash on right shift operator for signed integers
authorJustin Squirek <squirek@adacore.com>
Mon, 12 Oct 2020 16:06:08 +0000 (12:06 -0400)
committerPierre-Marie de Rodat <derodat@adacore.com>
Wed, 25 Nov 2020 13:22:31 +0000 (08:22 -0500)
gcc/ada/

* doc/gnat_rm/intrinsic_subprograms.rst (Shifts and Rotates):
Document behavior on negative numbers
* gnat_rm.texi: Regenerate.
* sem_eval.adb (Fold_Shift): Set modulus to be based on the RM
size for non-modular integer types.

gcc/ada/doc/gnat_rm/intrinsic_subprograms.rst
gcc/ada/gnat_rm.texi
gcc/ada/sem_eval.adb

index bf9f0b9e6d9817c659a1c99c02fbcd32a9734b75..e448816572fdbeb09b57ac4045e4d527185363b8 100644 (file)
@@ -217,7 +217,9 @@ The formal parameter names can be anything.
 
 A more convenient way of providing these shift operators is to use
 the Provide_Shift_Operators pragma, which provides the function declarations
-and corresponding pragma Import's for all five shift functions.
+and corresponding pragma Import's for all five shift functions. Note that in
+using these provided shift operations, shifts performed on negative numbers
+will result in modification of the sign bit.
 
 .. _Source_Location:
 
index 24afe79993d8b5c4193f45debf6164bfc87a577f..decca9ef63f59ea9eca21e557d69b598a2cdb3d9 100644 (file)
@@ -17862,7 +17862,9 @@ The formal parameter names can be anything.
 
 A more convenient way of providing these shift operators is to use
 the Provide_Shift_Operators pragma, which provides the function declarations
-and corresponding pragma Import's for all five shift functions.
+and corresponding pragma Import's for all five shift functions. Note that in
+using these provided shift operations, shifts performed on negative numbers
+will result in modification of the sign bit.
 
 @node Source_Location,,Shifts and Rotates,Intrinsic Subprograms
 @anchor{gnat_rm/intrinsic_subprograms source-location}@anchor{271}@anchor{gnat_rm/intrinsic_subprograms id13}@anchor{272}
index a04c5fa513c92fc0c13c551e4284e0e419122aa6..52aa8c111dad630c3fea73450a29c432ff4fd836 100644 (file)
@@ -4815,13 +4815,23 @@ package body Sem_Eval is
          if Op = N_Op_Shift_Left then
             Check_Elab_Call;
 
-            --  Fold Shift_Left (X, Y) by computing (X * 2**Y) rem modulus
+            declare
+               Modulus : Uint;
+            begin
+               if Is_Modular_Integer_Type (Typ) then
+                  Modulus := Einfo.Modulus (Typ);
+               else
+                  Modulus := Uint_2 ** RM_Size (Typ);
+               end if;
 
-            Fold_Uint
-              (N,
-               (Expr_Value (Left) * (Uint_2 ** Expr_Value (Right)))
-                 rem Modulus (Typ),
-               Static => Static);
+               --  Fold Shift_Left (X, Y) by computing (X * 2**Y) rem modulus
+
+               Fold_Uint
+                 (N,
+                  (Expr_Value (Left) * (Uint_2 ** Expr_Value (Right)))
+                    rem Modulus,
+                  Static => Static);
+            end;
 
          elsif Op = N_Op_Shift_Right then
             Check_Elab_Call;