| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . V A L U E _ U _ S P E C -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2022-2025, Free Software Foundation, Inc. -- |
| 10 |
-- -- |
| 11 |
-- GNAT is free software; you can redistribute it and/or modify it under -- |
| 12 |
-- terms of the GNU General Public License as published by the Free Soft- -- |
| 13 |
-- ware Foundation; either version 3, or (at your option) any later ver- -- |
| 14 |
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
| 15 |
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- |
| 16 |
-- or FITNESS FOR A PARTICULAR PURPOSE. -- |
| 17 |
-- -- |
| 18 |
-- As a special exception under Section 7 of GPL version 3, you are granted -- |
| 19 |
-- additional permissions described in the GCC Runtime Library Exception, -- |
| 20 |
-- version 3.1, as published by the Free Software Foundation. -- |
| 21 |
-- -- |
| 22 |
-- You should have received a copy of the GNU General Public License and -- |
| 23 |
-- a copy of the GCC Runtime Library Exception along with this program; -- |
| 24 |
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see -- |
| 25 |
-- <http://www.gnu.org/licenses/>. -- |
| 26 |
-- -- |
| 27 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 28 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 29 |
-- -- |
| 30 |
------------------------------------------------------------------------------ |
| 31 |
|
| 32 |
pragma Assertion_Policy (Pre => Ignore, |
| 33 |
Post => Ignore, |
| 34 |
Contract_Cases => Ignore, |
| 35 |
Ghost => Ignore, |
| 36 |
Subprogram_Variant => Ignore); |
| 37 |
|
| 38 |
package body System.Value_U_Spec with SPARK_Mode is |
| 39 |
|
| 40 |
----------------------------- |
| 41 |
-- Exponent_Unsigned_Ghost -- |
| 42 |
----------------------------- |
| 43 |
|
| 44 |
function Exponent_Unsigned_Ghost |
| 45 |
(Value : Uns; |
| 46 |
Exp : Natural; |
| 47 |
Base : Uns := 10) return Uns_Option |
| 48 |
is |
| 49 |
(if Exp = 0 or Value = 0 then (Overflow => False, Value => Value) |
| 50 |
elsif Scan_Overflows_Ghost (0, Base, Value) then (Overflow => True) |
| 51 |
else Exponent_Unsigned_Ghost (Value * Base, Exp - 1, Base)); |
| 52 |
|
| 53 |
--------------------- |
| 54 |
-- Last_Hexa_Ghost -- |
| 55 |
--------------------- |
| 56 |
|
| 57 |
function Last_Hexa_Ghost (Str : String) return Positive is |
| 58 |
begin |
| 59 |
pragma Annotate (Gnatcheck, Exempt_On, "Improper_Returns", |
| 60 |
"occurs in ghost code, not executable"); |
| 61 |
|
| 62 |
for J in Str'Range loop |
| 63 |
if Str (J) not in '0' .. '9' | 'a' .. 'f' | 'A' .. 'F' | '_' then |
| 64 |
return J - 1; |
| 65 |
end if; |
| 66 |
|
| 67 |
pragma Loop_Invariant |
| 68 |
(for all K in Str'First .. J => |
| 69 |
Str (K) in '0' .. '9' | 'a' .. 'f' | 'A' .. 'F' | '_'); |
| 70 |
end loop; |
| 71 |
|
| 72 |
return Str'Last; |
| 73 |
|
| 74 |
pragma Annotate (Gnatcheck, Exempt_Off, "Improper_Returns"); |
| 75 |
end Last_Hexa_Ghost; |
| 76 |
|
| 77 |
----------------------------- |
| 78 |
-- Lemmas with null bodies -- |
| 79 |
----------------------------- |
| 80 |
|
| 81 |
procedure Lemma_Scan_Based_Number_Ghost_Base |
| 82 |
(Str : String; |
| 83 |
From, To : Integer; |
| 84 |
Base : Uns := 10; |
| 85 |
Acc : Uns := 0) |
| 86 |
is null; |
| 87 |
|
| 88 |
procedure Lemma_Scan_Based_Number_Ghost_Underscore |
| 89 |
(Str : String; |
| 90 |
From, To : Integer; |
| 91 |
Base : Uns := 10; |
| 92 |
Acc : Uns := 0) |
| 93 |
is null; |
| 94 |
|
| 95 |
procedure Lemma_Scan_Based_Number_Ghost_Overflow |
| 96 |
(Str : String; |
| 97 |
From, To : Integer; |
| 98 |
Base : Uns := 10; |
| 99 |
Acc : Uns := 0) |
| 100 |
is null; |
| 101 |
|
| 102 |
procedure Lemma_Scan_Based_Number_Ghost_Step |
| 103 |
(Str : String; |
| 104 |
From, To : Integer; |
| 105 |
Base : Uns := 10; |
| 106 |
Acc : Uns := 0) |
| 107 |
is null; |
| 108 |
|
| 109 |
procedure Lemma_Exponent_Unsigned_Ghost_Base |
| 110 |
(Value : Uns; |
| 111 |
Exp : Natural; |
| 112 |
Base : Uns := 10) |
| 113 |
is null; |
| 114 |
|
| 115 |
procedure Lemma_Exponent_Unsigned_Ghost_Overflow |
| 116 |
(Value : Uns; |
| 117 |
Exp : Natural; |
| 118 |
Base : Uns := 10) |
| 119 |
is null; |
| 120 |
|
| 121 |
procedure Lemma_Exponent_Unsigned_Ghost_Step |
| 122 |
(Value : Uns; |
| 123 |
Exp : Natural; |
| 124 |
Base : Uns := 10) |
| 125 |
is null; |
| 126 |
|
| 127 |
-------------------------------------- |
| 128 |
-- Prove_Scan_Based_Number_Ghost_Eq -- |
| 129 |
-------------------------------------- |
| 130 |
|
| 131 |
procedure Prove_Scan_Based_Number_Ghost_Eq |
| 132 |
(Str1, Str2 : String; |
| 133 |
From, To : Integer; |
| 134 |
Base : Uns := 10; |
| 135 |
Acc : Uns := 0) |
| 136 |
is |
| 137 |
begin |
| 138 |
if From > To then |
| 139 |
null; |
| 140 |
elsif Str1 (From) = '_' then |
| 141 |
Prove_Scan_Based_Number_Ghost_Eq |
| 142 |
(Str1, Str2, From + 1, To, Base, Acc); |
| 143 |
elsif Scan_Overflows_Ghost |
| 144 |
(Hexa_To_Unsigned_Ghost (Str1 (From)), Base, Acc) |
| 145 |
then |
| 146 |
null; |
| 147 |
else |
| 148 |
Prove_Scan_Based_Number_Ghost_Eq |
| 149 |
(Str1, Str2, From + 1, To, Base, |
| 150 |
Base * Acc + Hexa_To_Unsigned_Ghost (Str1 (From))); |
| 151 |
end if; |
| 152 |
end Prove_Scan_Based_Number_Ghost_Eq; |
| 153 |
|
| 154 |
----------------------------------- |
| 155 |
-- Prove_Scan_Only_Decimal_Ghost -- |
| 156 |
----------------------------------- |
| 157 |
|
| 158 |
procedure Prove_Scan_Only_Decimal_Ghost |
| 159 |
(Str : String; |
| 160 |
Val : Uns) |
| 161 |
is |
| 162 |
pragma Assert (Str (Str'First + 1) /= ' '); |
| 163 |
Non_Blank : constant Positive := First_Non_Space_Ghost |
| 164 |
(Str, Str'First, Str'Last); |
| 165 |
pragma Assert (Non_Blank = Str'First + 1); |
| 166 |
Fst_Num : constant Positive := |
| 167 |
(if Str (Non_Blank) = '+' then Non_Blank + 1 else Non_Blank); |
| 168 |
pragma Assert (Fst_Num = Str'First + 1); |
| 169 |
begin |
| 170 |
pragma Assert |
| 171 |
(Is_Raw_Unsigned_Format_Ghost (Str (Fst_Num .. Str'Last))); |
| 172 |
pragma Assert |
| 173 |
(Scan_Split_No_Overflow_Ghost (Str, Str'First + 1, Str'Last)); |
| 174 |
pragma Assert |
| 175 |
((Val, 10, 0) = Scan_Split_Value_Ghost (Str, Str'First + 1, Str'Last)); |
| 176 |
pragma Assert |
| 177 |
(Raw_Unsigned_No_Overflow_Ghost (Str, Fst_Num, Str'Last)); |
| 178 |
pragma Assert (Val = Exponent_Unsigned_Ghost (Val, 0, 10).Value); |
| 179 |
pragma Assert (Is_Unsigned_Ghost (Str)); |
| 180 |
pragma Assert (Is_Value_Unsigned_Ghost (Str, Val)); |
| 181 |
end Prove_Scan_Only_Decimal_Ghost; |
| 182 |
|
| 183 |
----------------------------- |
| 184 |
-- Scan_Based_Number_Ghost -- |
| 185 |
----------------------------- |
| 186 |
|
| 187 |
function Scan_Based_Number_Ghost |
| 188 |
(Str : String; |
| 189 |
From, To : Integer; |
| 190 |
Base : Uns := 10; |
| 191 |
Acc : Uns := 0) return Uns_Option |
| 192 |
is |
| 193 |
(if From > To then (Overflow => False, Value => Acc) |
| 194 |
elsif Str (From) = '_' |
| 195 |
then Scan_Based_Number_Ghost (Str, From + 1, To, Base, Acc) |
| 196 |
elsif Scan_Overflows_Ghost |
| 197 |
(Hexa_To_Unsigned_Ghost (Str (From)), Base, Acc) |
| 198 |
then (Overflow => True) |
| 199 |
else Scan_Based_Number_Ghost |
| 200 |
(Str, From + 1, To, Base, |
| 201 |
Base * Acc + Hexa_To_Unsigned_Ghost (Str (From)))); |
| 202 |
|
| 203 |
end System.Value_U_Spec; |