* rtl.h (struct rtx_def): Update comments.
[official-gcc.git] / gcc / ada / s-valllu.adb
blob741e373c08abca2c2c084be546a2cd7422fdf886
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S Y S T E M . V A L _ L L U --
6 -- --
7 -- S p e c --
8 -- --
9 -- --
10 -- Copyright (C) 1992-1997 Free Software Foundation, Inc. --
11 -- --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
22 -- --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
29 -- --
30 -- GNAT was originally developed by the GNAT team at New York University. --
31 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
32 -- --
33 ------------------------------------------------------------------------------
35 with System.Unsigned_Types; use System.Unsigned_Types;
36 with System.Val_Util; use System.Val_Util;
38 package body System.Val_LLU is
40 -----------------------------
41 -- Scan_Long_Long_Unsigned --
42 -----------------------------
44 function Scan_Long_Long_Unsigned
45 (Str : String;
46 Ptr : access Integer;
47 Max : Integer)
48 return Long_Long_Unsigned
50 P : Integer;
51 -- Local copy of the pointer
53 Uval : Long_Long_Unsigned;
54 -- Accumulated unsigned integer result
56 Expon : Integer;
57 -- Exponent value
59 Minus : Boolean := False;
60 -- Set to True if minus sign is present, otherwise to False. Note that
61 -- a minus sign is permissible for the singular case of -0, and in any
62 -- case the pointer is left pointing past a negative integer literal.
64 Overflow : Boolean := False;
65 -- Set True if overflow is detected at any point
67 Start : Positive;
68 -- Save location of first non-blank character
70 Base_Char : Character;
71 -- Base character (# or :) in based case
73 Base : Long_Long_Unsigned := 10;
74 -- Base value (reset in based case)
76 Digit : Long_Long_Unsigned;
77 -- Digit value
79 begin
80 Scan_Sign (Str, Ptr, Max, Minus, Start);
82 if Str (Ptr.all) not in '0' .. '9' then
83 Ptr.all := Start;
84 raise Constraint_Error;
85 end if;
87 P := Ptr.all;
88 Uval := Character'Pos (Str (P)) - Character'Pos ('0');
89 P := P + 1;
91 -- Scan out digits of what is either the number or the base.
92 -- In either case, we are definitely scanning out in base 10.
94 declare
95 Umax : constant := (Long_Long_Unsigned'Last - 9) / 10;
96 -- Max value which cannot overflow on accumulating next digit
98 Umax10 : constant := Long_Long_Unsigned'Last / 10;
99 -- Numbers bigger than Umax10 overflow if multiplied by 10
101 begin
102 -- Loop through decimal digits
103 loop
104 exit when P > Max;
106 Digit := Character'Pos (Str (P)) - Character'Pos ('0');
108 -- Non-digit encountered
110 if Digit > 9 then
111 if Str (P) = '_' then
112 Scan_Underscore (Str, P, Ptr, Max, False);
113 else
114 exit;
115 end if;
117 -- Accumulate result, checking for overflow
119 else
120 if Uval <= Umax then
121 Uval := 10 * Uval + Digit;
123 elsif Uval > Umax10 then
124 Overflow := True;
126 else
127 Uval := 10 * Uval + Digit;
129 if Uval < Umax10 then
130 Overflow := True;
131 end if;
132 end if;
134 P := P + 1;
135 end if;
136 end loop;
137 end;
139 Ptr.all := P;
141 -- Deal with based case
143 if P < Max and then (Str (P) = ':' or else Str (P) = '#') then
144 Base_Char := Str (P);
145 P := P + 1;
146 Base := Uval;
147 Uval := 0;
149 -- Check base value. Overflow is set True if we find a bad base, or
150 -- a digit that is out of range of the base. That way, we scan out
151 -- the numeral that is still syntactically correct, though illegal.
152 -- We use a safe base of 16 for this scan, to avoid zero divide.
154 if Base not in 2 .. 16 then
155 Overflow := True;
156 Base := 16;
157 end if;
159 -- Scan out based integer
161 declare
162 Umax : constant Long_Long_Unsigned :=
163 (Long_Long_Unsigned'Last - Base + 1) / Base;
164 -- Max value which cannot overflow on accumulating next digit
166 UmaxB : constant Long_Long_Unsigned :=
167 Long_Long_Unsigned'Last / Base;
168 -- Numbers bigger than UmaxB overflow if multiplied by base
170 begin
171 -- Loop to scan out based integer value
173 loop
174 -- We require a digit at this stage
176 if Str (P) in '0' .. '9' then
177 Digit := Character'Pos (Str (P)) - Character'Pos ('0');
179 elsif Str (P) in 'A' .. 'F' then
180 Digit :=
181 Character'Pos (Str (P)) - (Character'Pos ('A') - 10);
183 elsif Str (P) in 'a' .. 'f' then
184 Digit :=
185 Character'Pos (Str (P)) - (Character'Pos ('a') - 10);
187 -- If we don't have a digit, then this is not a based number
188 -- after all, so we use the value we scanned out as the base
189 -- (now in Base), and the pointer to the base character was
190 -- already stored in Ptr.all.
192 else
193 Uval := Base;
194 exit;
195 end if;
197 -- If digit is too large, just signal overflow and continue.
198 -- The idea here is to keep scanning as long as the input is
199 -- syntactically valid, even if we have detected overflow
201 if Digit >= Base then
202 Overflow := True;
204 -- Here we accumulate the value, checking overflow
206 elsif Uval <= Umax then
207 Uval := Base * Uval + Digit;
209 elsif Uval > UmaxB then
210 Overflow := True;
212 else
213 Uval := Base * Uval + Digit;
215 if Uval < UmaxB then
216 Overflow := True;
217 end if;
218 end if;
220 -- If at end of string with no base char, not a based number
221 -- but we signal Constraint_Error and set the pointer past
222 -- the end of the field, since this is what the ACVC tests
223 -- seem to require, see CE3704N, line 204.
225 P := P + 1;
227 if P > Max then
228 Ptr.all := P;
229 raise Constraint_Error;
230 end if;
232 -- If terminating base character, we are done with loop
234 if Str (P) = Base_Char then
235 Ptr.all := P + 1;
236 exit;
238 -- Deal with underscore
240 elsif Str (P) = '_' then
241 Scan_Underscore (Str, P, Ptr, Max, True);
242 end if;
244 end loop;
245 end;
246 end if;
248 -- Come here with scanned unsigned value in Uval. The only remaining
249 -- required step is to deal with exponent if one is present.
251 Expon := Scan_Exponent (Str, Ptr, Max);
253 if Expon /= 0 and then Uval /= 0 then
255 -- For non-zero value, scale by exponent value. No need to do this
256 -- efficiently, since use of exponent in integer literals is rare,
257 -- and in any case the exponent cannot be very large.
259 declare
260 UmaxB : constant Long_Long_Unsigned :=
261 Long_Long_Unsigned'Last / Base;
262 -- Numbers bigger than UmaxB overflow if multiplied by base
264 begin
265 for J in 1 .. Expon loop
266 if Uval > UmaxB then
267 Overflow := True;
268 exit;
269 end if;
271 Uval := Uval * Base;
272 end loop;
273 end;
274 end if;
276 -- Return result, dealing with sign and overflow
278 if Overflow or else (Minus and then Uval /= 0) then
279 raise Constraint_Error;
280 else
281 return Uval;
282 end if;
283 end Scan_Long_Long_Unsigned;
285 ------------------------------
286 -- Value_Long_Long_Unsigned --
287 ------------------------------
289 function Value_Long_Long_Unsigned
290 (Str : String)
291 return Long_Long_Unsigned
293 V : Long_Long_Unsigned;
294 P : aliased Integer := Str'First;
296 begin
297 V := Scan_Long_Long_Unsigned (Str, P'Access, Str'Last);
298 Scan_Trailing_Blanks (Str, P);
299 return V;
301 end Value_Long_Long_Unsigned;
303 end System.Val_LLU;