| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . I M A G E _ U -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 1992-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 |
with Ada.Numerics.Big_Numbers.Big_Integers_Ghost; |
| 33 |
use Ada.Numerics.Big_Numbers.Big_Integers_Ghost; |
| 34 |
with System.Val_Spec; |
| 35 |
|
| 36 |
package body System.Image_U is |
| 37 |
|
| 38 |
-- Ghost code, loop invariants and assertions in this unit are meant for |
| 39 |
-- analysis only, not for run-time checking, as it would be too costly |
| 40 |
-- otherwise. This is enforced by setting the assertion policy to Ignore. |
| 41 |
|
| 42 |
pragma Assertion_Policy (Ghost => Ignore, |
| 43 |
Loop_Invariant => Ignore, |
| 44 |
Assert => Ignore, |
| 45 |
Assert_And_Cut => Ignore, |
| 46 |
Subprogram_Variant => Ignore); |
| 47 |
|
| 48 |
package Unsigned_Conversion is new Unsigned_Conversions (Int => Uns); |
| 49 |
|
| 50 |
function Big (Arg : Uns) return Big_Integer renames |
| 51 |
Unsigned_Conversion.To_Big_Integer; |
| 52 |
|
| 53 |
function From_Big (Arg : Big_Integer) return Uns renames |
| 54 |
Unsigned_Conversion.From_Big_Integer; |
| 55 |
|
| 56 |
Big_10 : constant Big_Integer := Big (10) with Ghost; |
| 57 |
|
| 58 |
------------------ |
| 59 |
-- Local Lemmas -- |
| 60 |
------------------ |
| 61 |
|
| 62 |
procedure Lemma_Non_Zero (X : Uns) |
| 63 |
with |
| 64 |
Ghost, |
| 65 |
Pre => X /= 0, |
| 66 |
Post => Big (X) /= 0; |
| 67 |
|
| 68 |
procedure Lemma_Div_Commutation (X, Y : Uns) |
| 69 |
with |
| 70 |
Ghost, |
| 71 |
Pre => Y /= 0, |
| 72 |
Post => Big (X) / Big (Y) = Big (X / Y); |
| 73 |
|
| 74 |
procedure Lemma_Div_Twice (X : Big_Natural; Y, Z : Big_Positive) |
| 75 |
with |
| 76 |
Ghost, |
| 77 |
Post => X / Y / Z = X / (Y * Z); |
| 78 |
|
| 79 |
--------------------------- |
| 80 |
-- Lemma_Div_Commutation -- |
| 81 |
--------------------------- |
| 82 |
|
| 83 |
procedure Lemma_Non_Zero (X : Uns) is null; |
| 84 |
procedure Lemma_Div_Commutation (X, Y : Uns) is null; |
| 85 |
|
| 86 |
--------------------- |
| 87 |
-- Lemma_Div_Twice -- |
| 88 |
--------------------- |
| 89 |
|
| 90 |
procedure Lemma_Div_Twice (X : Big_Natural; Y, Z : Big_Positive) is |
| 91 |
XY : constant Big_Natural := X / Y; |
| 92 |
YZ : constant Big_Natural := Y * Z; |
| 93 |
XYZ : constant Big_Natural := X / Y / Z; |
| 94 |
R : constant Big_Natural := (XY rem Z) * Y + (X rem Y); |
| 95 |
begin |
| 96 |
pragma Assert (X = XY * Y + (X rem Y)); |
| 97 |
pragma Assert (XY = XY / Z * Z + (XY rem Z)); |
| 98 |
pragma Assert (X = XYZ * YZ + R); |
| 99 |
pragma Assert ((XY rem Z) * Y <= (Z - 1) * Y); |
| 100 |
pragma Assert (R <= YZ - 1); |
| 101 |
pragma Assert (X / YZ = (XYZ * YZ + R) / YZ); |
| 102 |
pragma Assert (X / YZ = XYZ + R / YZ); |
| 103 |
end Lemma_Div_Twice; |
| 104 |
|
| 105 |
-------------------- |
| 106 |
-- Image_Unsigned -- |
| 107 |
-------------------- |
| 108 |
|
| 109 |
procedure Image_Unsigned |
| 110 |
(V : Uns; |
| 111 |
S : in out String; |
| 112 |
P : out Natural) |
| 113 |
is |
| 114 |
pragma Assert (S'First = 1); |
| 115 |
|
| 116 |
procedure Prove_Value_Unsigned |
| 117 |
with |
| 118 |
Ghost, |
| 119 |
Pre => S'First = 1 |
| 120 |
and then S'Last < Integer'Last |
| 121 |
and then P in 2 .. S'Last |
| 122 |
and then S (1) = ' ' |
| 123 |
and then U_Spec.Only_Decimal_Ghost (S, From => 2, To => P) |
| 124 |
and then U_Spec.Scan_Based_Number_Ghost (S, From => 2, To => P) |
| 125 |
= U_Spec.Wrap_Option (V), |
| 126 |
Post => not System.Val_Spec.Only_Space_Ghost (S, 1, P) |
| 127 |
and then U_Spec.Is_Unsigned_Ghost (S (1 .. P)) |
| 128 |
and then U_Spec.Is_Value_Unsigned_Ghost (S (1 .. P), V); |
| 129 |
-- Ghost lemma to prove the value of Value_Unsigned from the value of |
| 130 |
-- Scan_Based_Number_Ghost on a decimal string. |
| 131 |
|
| 132 |
-------------------------- |
| 133 |
-- Prove_Value_Unsigned -- |
| 134 |
-------------------------- |
| 135 |
|
| 136 |
procedure Prove_Value_Unsigned is |
| 137 |
Str : constant String := S (1 .. P); |
| 138 |
begin |
| 139 |
pragma Assert (Str'First = 1); |
| 140 |
pragma Assert (S (2) /= ' '); |
| 141 |
pragma Assert |
| 142 |
(U_Spec.Only_Decimal_Ghost (Str, From => 2, To => P)); |
| 143 |
U_Spec.Prove_Scan_Based_Number_Ghost_Eq |
| 144 |
(S, Str, From => 2, To => P); |
| 145 |
pragma Assert |
| 146 |
(U_Spec.Scan_Based_Number_Ghost (Str, From => 2, To => P) |
| 147 |
= U_Spec.Wrap_Option (V)); |
| 148 |
U_Spec.Prove_Scan_Only_Decimal_Ghost (Str, V); |
| 149 |
end Prove_Value_Unsigned; |
| 150 |
|
| 151 |
-- Start of processing for Image_Unsigned |
| 152 |
|
| 153 |
begin |
| 154 |
S (1) := ' '; |
| 155 |
P := 1; |
| 156 |
Set_Image_Unsigned (V, S, P); |
| 157 |
|
| 158 |
Prove_Value_Unsigned; |
| 159 |
end Image_Unsigned; |
| 160 |
|
| 161 |
------------------------ |
| 162 |
-- Set_Image_Unsigned -- |
| 163 |
------------------------ |
| 164 |
|
| 165 |
procedure Set_Image_Unsigned |
| 166 |
(V : Uns; |
| 167 |
S : in out String; |
| 168 |
P : in out Natural) |
| 169 |
is |
| 170 |
Nb_Digits : Natural := 0; |
| 171 |
Value : Uns := V; |
| 172 |
|
| 173 |
-- Local ghost variables |
| 174 |
|
| 175 |
Pow : Big_Positive := 1 with Ghost; |
| 176 |
S_Init : constant String := S with Ghost; |
| 177 |
Prev_Value : Uns with Ghost; |
| 178 |
Prev_S : String := S with Ghost; |
| 179 |
|
| 180 |
-- Local ghost lemmas |
| 181 |
|
| 182 |
procedure Prove_Character_Val (R : Uns) |
| 183 |
with |
| 184 |
Ghost, |
| 185 |
Post => R rem 10 in 0 .. 9 |
| 186 |
and then Character'Val (48 + R rem 10) in '0' .. '9'; |
| 187 |
-- Ghost lemma to prove the value of a character corresponding to the |
| 188 |
-- next figure. |
| 189 |
|
| 190 |
procedure Prove_Euclidian (Val, Quot, Rest : Uns) |
| 191 |
with |
| 192 |
Ghost, |
| 193 |
Pre => Quot = Val / 10 |
| 194 |
and then Rest = Val rem 10, |
| 195 |
Post => Uns'Last - Rest >= 10 * Quot and then Val = 10 * Quot + Rest; |
| 196 |
-- Ghost lemma to prove the relation between the quotient/remainder of |
| 197 |
-- division by 10 and the initial value. |
| 198 |
|
| 199 |
procedure Prove_Hexa_To_Unsigned_Ghost (R : Uns) |
| 200 |
with |
| 201 |
Ghost, |
| 202 |
Pre => R in 0 .. 9, |
| 203 |
Post => U_Spec.Hexa_To_Unsigned_Ghost (Character'Val (48 + R)) = R; |
| 204 |
-- Ghost lemma to prove that Hexa_To_Unsigned_Ghost returns the source |
| 205 |
-- figure when applied to the corresponding character. |
| 206 |
|
| 207 |
procedure Prove_Scan_Iter |
| 208 |
(S, Prev_S : String; |
| 209 |
V, Prev_V, Res : Uns; |
| 210 |
P, Max : Natural) |
| 211 |
with |
| 212 |
Ghost, |
| 213 |
Pre => |
| 214 |
S'First = Prev_S'First and then S'Last = Prev_S'Last |
| 215 |
and then S'Last < Natural'Last and then |
| 216 |
Max in S'Range and then P in S'First .. Max and then |
| 217 |
(for all I in P + 1 .. Max => Prev_S (I) in '0' .. '9') |
| 218 |
and then (for all I in P + 1 .. Max => Prev_S (I) = S (I)) |
| 219 |
and then S (P) in '0' .. '9' |
| 220 |
and then V <= Uns'Last / 10 |
| 221 |
and then Uns'Last - U_Spec.Hexa_To_Unsigned_Ghost (S (P)) |
| 222 |
>= 10 * V |
| 223 |
and then Prev_V = |
| 224 |
V * 10 + U_Spec.Hexa_To_Unsigned_Ghost (S (P)) |
| 225 |
and then |
| 226 |
(if P = Max then Prev_V = Res |
| 227 |
else U_Spec.Scan_Based_Number_Ghost |
| 228 |
(Str => Prev_S, |
| 229 |
From => P + 1, |
| 230 |
To => Max, |
| 231 |
Base => 10, |
| 232 |
Acc => Prev_V) = U_Spec.Wrap_Option (Res)), |
| 233 |
Post => |
| 234 |
(for all I in P .. Max => S (I) in '0' .. '9') |
| 235 |
and then U_Spec.Scan_Based_Number_Ghost |
| 236 |
(Str => S, |
| 237 |
From => P, |
| 238 |
To => Max, |
| 239 |
Base => 10, |
| 240 |
Acc => V) = U_Spec.Wrap_Option (Res); |
| 241 |
-- Ghost lemma to prove that Scan_Based_Number_Ghost is preserved |
| 242 |
-- through an iteration of the loop. |
| 243 |
|
| 244 |
----------------------------- |
| 245 |
-- Local lemma null bodies -- |
| 246 |
----------------------------- |
| 247 |
|
| 248 |
procedure Prove_Character_Val (R : Uns) is null; |
| 249 |
procedure Prove_Euclidian (Val, Quot, Rest : Uns) is null; |
| 250 |
procedure Prove_Hexa_To_Unsigned_Ghost (R : Uns) is null; |
| 251 |
|
| 252 |
--------------------- |
| 253 |
-- Prove_Scan_Iter -- |
| 254 |
--------------------- |
| 255 |
|
| 256 |
procedure Prove_Scan_Iter |
| 257 |
(S, Prev_S : String; |
| 258 |
V, Prev_V, Res : Uns; |
| 259 |
P, Max : Natural) |
| 260 |
is |
| 261 |
pragma Unreferenced (Res); |
| 262 |
begin |
| 263 |
U_Spec.Lemma_Scan_Based_Number_Ghost_Step |
| 264 |
(Str => S, |
| 265 |
From => P, |
| 266 |
To => Max, |
| 267 |
Base => 10, |
| 268 |
Acc => V); |
| 269 |
if P < Max then |
| 270 |
U_Spec.Prove_Scan_Based_Number_Ghost_Eq |
| 271 |
(Prev_S, S, P + 1, Max, 10, Prev_V); |
| 272 |
else |
| 273 |
U_Spec.Lemma_Scan_Based_Number_Ghost_Base |
| 274 |
(Str => S, |
| 275 |
From => P + 1, |
| 276 |
To => Max, |
| 277 |
Base => 10, |
| 278 |
Acc => Prev_V); |
| 279 |
end if; |
| 280 |
end Prove_Scan_Iter; |
| 281 |
|
| 282 |
-- Start of processing for Set_Image_Unsigned |
| 283 |
|
| 284 |
begin |
| 285 |
pragma Assert (P >= S'First - 1 and then P < S'Last and then |
| 286 |
P < Natural'Last); |
| 287 |
-- No check is done since, as documented in the specification, the |
| 288 |
-- caller guarantees that S is long enough to hold the result. |
| 289 |
|
| 290 |
-- First we compute the number of characters needed for representing |
| 291 |
-- the number. |
| 292 |
loop |
| 293 |
Lemma_Div_Commutation (Value, 10); |
| 294 |
Lemma_Div_Twice (Big (V), Big_10 ** Nb_Digits, Big_10); |
| 295 |
|
| 296 |
Value := Value / 10; |
| 297 |
Nb_Digits := Nb_Digits + 1; |
| 298 |
Pow := Pow * 10; |
| 299 |
|
| 300 |
pragma Loop_Invariant (Nb_Digits in 1 .. Unsigned_Width_Ghost - 1); |
| 301 |
pragma Loop_Invariant (Pow = Big_10 ** Nb_Digits); |
| 302 |
pragma Loop_Invariant (Big (Value) = Big (V) / Pow); |
| 303 |
pragma Loop_Variant (Decreases => Value); |
| 304 |
|
| 305 |
exit when Value = 0; |
| 306 |
|
| 307 |
Lemma_Non_Zero (Value); |
| 308 |
pragma Assert (Pow <= Big (Uns'Last)); |
| 309 |
end loop; |
| 310 |
pragma Assert (Big (V) / (Big_10 ** Nb_Digits) = 0); |
| 311 |
|
| 312 |
Value := V; |
| 313 |
Pow := 1; |
| 314 |
|
| 315 |
pragma Assert (Value = From_Big (Big (V) / Big_10 ** 0)); |
| 316 |
|
| 317 |
-- We now populate digits from the end of the string to the beginning |
| 318 |
for J in reverse 1 .. Nb_Digits loop |
| 319 |
Lemma_Div_Commutation (Value, 10); |
| 320 |
Lemma_Div_Twice (Big (V), Big_10 ** (Nb_Digits - J), Big_10); |
| 321 |
Prove_Character_Val (Value); |
| 322 |
Prove_Hexa_To_Unsigned_Ghost (Value rem 10); |
| 323 |
|
| 324 |
Prev_Value := Value; |
| 325 |
Prev_S := S; |
| 326 |
Pow := Pow * 10; |
| 327 |
S (P + J) := Character'Val (48 + (Value rem 10)); |
| 328 |
Value := Value / 10; |
| 329 |
|
| 330 |
Prove_Euclidian |
| 331 |
(Val => Prev_Value, |
| 332 |
Quot => Value, |
| 333 |
Rest => U_Spec.Hexa_To_Unsigned_Ghost (S (P + J))); |
| 334 |
|
| 335 |
Prove_Scan_Iter |
| 336 |
(S, Prev_S, Value, Prev_Value, V, P + J, P + Nb_Digits); |
| 337 |
|
| 338 |
pragma Loop_Invariant (Value <= Uns'Last / 10); |
| 339 |
pragma Loop_Invariant |
| 340 |
(for all K in S'First .. P => S (K) = S_Init (K)); |
| 341 |
pragma Loop_Invariant |
| 342 |
(U_Spec.Only_Decimal_Ghost |
| 343 |
(S, From => P + J, To => P + Nb_Digits)); |
| 344 |
pragma Loop_Invariant (Pow = Big_10 ** (Nb_Digits - J + 1)); |
| 345 |
pragma Loop_Invariant (Big (Value) = Big (V) / Pow); |
| 346 |
pragma Loop_Invariant |
| 347 |
(U_Spec.Scan_Based_Number_Ghost |
| 348 |
(Str => S, |
| 349 |
From => P + J, |
| 350 |
To => P + Nb_Digits, |
| 351 |
Base => 10, |
| 352 |
Acc => Value) |
| 353 |
= U_Spec.Wrap_Option (V)); |
| 354 |
end loop; |
| 355 |
pragma Assert (Big (Value) = Big (V) / (Big_10 ** Nb_Digits)); |
| 356 |
pragma Assert (Value = 0); |
| 357 |
|
| 358 |
P := P + Nb_Digits; |
| 359 |
end Set_Image_Unsigned; |
| 360 |
|
| 361 |
end System.Image_U; |