aboutsummaryrefslogtreecommitdiff
path: root/gcc/ada/libgnat/s-vauspe.adb
blob: dac452a239e9a4ed0c0c5a6a37d68a1124a9997f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
------------------------------------------------------------------------------
--                                                                          --
--                         GNAT COMPILER COMPONENTS                         --
--                                                                          --
--                   S Y S T E M . V A L U E _ U _ S P E C                  --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--          Copyright (C) 2022-2024, Free Software Foundation, Inc.         --
--                                                                          --
-- GNAT is free software;  you can  redistribute it  and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
--                                                                          --
-- GNAT was originally developed  by the GNAT team at  New York University. --
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
--                                                                          --
------------------------------------------------------------------------------

pragma Assertion_Policy (Pre                => Ignore,
                         Post               => Ignore,
                         Contract_Cases     => Ignore,
                         Ghost              => Ignore,
                         Subprogram_Variant => Ignore);

package body System.Value_U_Spec with SPARK_Mode is

   -----------------------------
   -- Exponent_Unsigned_Ghost --
   -----------------------------

   function Exponent_Unsigned_Ghost
     (Value : Uns;
      Exp   : Natural;
      Base  : Uns := 10) return Uns_Option
   is
      (if Exp = 0 or Value = 0 then (Overflow => False, Value => Value)
       elsif Scan_Overflows_Ghost (0, Base, Value) then (Overflow => True)
       else Exponent_Unsigned_Ghost (Value * Base, Exp - 1, Base));

   ---------------------
   -- Last_Hexa_Ghost --
   ---------------------

   function Last_Hexa_Ghost (Str : String) return Positive is
   begin
      pragma Annotate (Gnatcheck, Exempt_On, "Improper_Returns",
                       "occurs in ghost code, not executable");

      for J in Str'Range loop
         if Str (J) not in '0' .. '9' | 'a' .. 'f' | 'A' .. 'F' | '_' then
            return J - 1;
         end if;

         pragma Loop_Invariant
           (for all K in Str'First .. J =>
              Str (K) in '0' .. '9' | 'a' .. 'f' | 'A' .. 'F' | '_');
      end loop;

      return Str'Last;

      pragma Annotate (Gnatcheck, Exempt_Off, "Improper_Returns");
   end Last_Hexa_Ghost;

   -----------------------------
   -- Lemmas with null bodies --
   -----------------------------

   procedure Lemma_Scan_Based_Number_Ghost_Base
     (Str      : String;
      From, To : Integer;
      Base     : Uns := 10;
      Acc      : Uns := 0)
   is null;

   procedure Lemma_Scan_Based_Number_Ghost_Underscore
     (Str      : String;
      From, To : Integer;
      Base     : Uns := 10;
      Acc      : Uns := 0)
   is null;

   procedure Lemma_Scan_Based_Number_Ghost_Overflow
     (Str      : String;
      From, To : Integer;
      Base     : Uns := 10;
      Acc      : Uns := 0)
   is null;

   procedure Lemma_Scan_Based_Number_Ghost_Step
     (Str      : String;
      From, To : Integer;
      Base     : Uns := 10;
      Acc      : Uns := 0)
   is null;

   procedure Lemma_Exponent_Unsigned_Ghost_Base
     (Value : Uns;
      Exp   : Natural;
      Base  : Uns := 10)
   is null;

   procedure Lemma_Exponent_Unsigned_Ghost_Overflow
     (Value : Uns;
      Exp   : Natural;
      Base  : Uns := 10)
   is null;

   procedure Lemma_Exponent_Unsigned_Ghost_Step
     (Value : Uns;
      Exp   : Natural;
      Base  : Uns := 10)
   is null;

   --------------------------------------
   -- Prove_Scan_Based_Number_Ghost_Eq --
   --------------------------------------

   procedure Prove_Scan_Based_Number_Ghost_Eq
     (Str1, Str2 : String;
      From, To : Integer;
      Base     : Uns := 10;
      Acc      : Uns := 0)
   is
   begin
      if From > To then
         null;
      elsif Str1 (From) = '_' then
         Prove_Scan_Based_Number_Ghost_Eq
           (Str1, Str2, From + 1, To, Base, Acc);
      elsif Scan_Overflows_Ghost
        (Hexa_To_Unsigned_Ghost (Str1 (From)), Base, Acc)
      then
         null;
      else
         Prove_Scan_Based_Number_Ghost_Eq
           (Str1, Str2, From + 1, To, Base,
            Base * Acc + Hexa_To_Unsigned_Ghost (Str1 (From)));
      end if;
   end Prove_Scan_Based_Number_Ghost_Eq;

   -----------------------------------
   -- Prove_Scan_Only_Decimal_Ghost --
   -----------------------------------

   procedure Prove_Scan_Only_Decimal_Ghost
     (Str : String;
      Val : Uns)
   is
      pragma Assert (Str (Str'First + 1) /= ' ');
      Non_Blank : constant Positive := First_Non_Space_Ghost
        (Str, Str'First, Str'Last);
      pragma Assert (Non_Blank = Str'First + 1);
      Fst_Num   : constant Positive :=
        (if Str (Non_Blank) = '+' then Non_Blank + 1 else Non_Blank);
      pragma Assert (Fst_Num = Str'First + 1);
   begin
      pragma Assert
        (Is_Raw_Unsigned_Format_Ghost (Str (Fst_Num .. Str'Last)));
      pragma Assert
        (Scan_Split_No_Overflow_Ghost (Str, Str'First + 1, Str'Last));
      pragma Assert
        ((Val, 10, 0) = Scan_Split_Value_Ghost (Str, Str'First + 1, Str'Last));
      pragma Assert
        (Raw_Unsigned_No_Overflow_Ghost (Str, Fst_Num, Str'Last));
      pragma Assert (Val = Exponent_Unsigned_Ghost (Val, 0, 10).Value);
      pragma Assert (Is_Unsigned_Ghost (Str));
      pragma Assert (Is_Value_Unsigned_Ghost (Str, Val));
   end Prove_Scan_Only_Decimal_Ghost;

   -----------------------------
   -- Scan_Based_Number_Ghost --
   -----------------------------

   function Scan_Based_Number_Ghost
     (Str      : String;
      From, To : Integer;
      Base     : Uns := 10;
      Acc      : Uns := 0) return Uns_Option
   is
      (if From > To then (Overflow => False, Value => Acc)
       elsif Str (From) = '_'
       then Scan_Based_Number_Ghost (Str, From + 1, To, Base, Acc)
       elsif Scan_Overflows_Ghost
         (Hexa_To_Unsigned_Ghost (Str (From)), Base, Acc)
       then (Overflow => True)
       else Scan_Based_Number_Ghost
         (Str, From + 1, To, Base,
          Base * Acc + Hexa_To_Unsigned_Ghost (Str (From))));

end System.Value_U_Spec;