* tree-cfg.c (tree_find_edge_insert_loc): Handle naked RETURN_EXPR.
[official-gcc.git] / gcc / ada / s-valrea.adb
blob3a6ca8c0ba2d225494fda96701692e6d49cbcb2f
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S Y S T E M . V A L _ R E A L --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2005 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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
28 -- --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 with System.Powten_Table; use System.Powten_Table;
35 with System.Val_Util; use System.Val_Util;
37 package body System.Val_Real is
39 ---------------
40 -- Scan_Real --
41 ---------------
43 function Scan_Real
44 (Str : String;
45 Ptr : access Integer;
46 Max : Integer) return Long_Long_Float
48 procedure Reset;
49 pragma Import (C, Reset, "__gnat_init_float");
50 -- We import the floating-point processor reset routine so that we can
51 -- be sure the floating-point processor is properly set for conversion
52 -- calls (see description of Reset in GNAT.Float_Control (g-flocon.ads).
53 -- This is notably need on Windows, where calls to the operating system
54 -- randomly reset the processor into 64-bit mode.
56 P : Integer;
57 -- Local copy of string pointer
59 Base : Long_Long_Float;
60 -- Base value
62 Uval : Long_Long_Float;
63 -- Accumulated float result
65 subtype Digs is Character range '0' .. '9';
66 -- Used to check for decimal digit
68 Scale : Integer := 0;
69 -- Power of Base to multiply result by
71 Start : Positive;
72 -- Position of starting non-blank character
74 Minus : Boolean;
75 -- Set to True if minus sign is present, otherwise to False
77 Bad_Base : Boolean := False;
78 -- Set True if Base out of range or if out of range digit
80 After_Point : Natural := 0;
81 -- Set to 1 after the point
83 Num_Saved_Zeroes : Natural := 0;
84 -- This counts zeroes after the decimal point. A non-zero value means
85 -- that this number of previously scanned digits are zero. if the end
86 -- of the number is reached, these zeroes are simply discarded, which
87 -- ensures that trailing zeroes after the point never affect the value
88 -- (which might otherwise happen as a result of rounding). With this
89 -- processing in place, we can ensure that, for example, we get the
90 -- same exact result from 1.0E+49 and 1.0000000E+49. This is not
91 -- necessarily required in a case like this where the result is not
92 -- a machine number, but it is certainly a desirable behavior.
94 procedure Scanf;
95 -- Scans integer literal value starting at current character position.
96 -- For each digit encountered, Uval is multiplied by 10.0, and the new
97 -- digit value is incremented. In addition Scale is decremented for each
98 -- digit encountered if we are after the point (After_Point = 1). The
99 -- longest possible syntactically valid numeral is scanned out, and on
100 -- return P points past the last character. On entry, the current
101 -- character is known to be a digit, so a numeral is definitely present.
103 procedure Scanf is
104 Digit : Natural;
106 begin
107 loop
108 Digit := Character'Pos (Str (P)) - Character'Pos ('0');
109 P := P + 1;
111 -- Save up trailing zeroes after the decimal point
113 if Digit = 0 and After_Point = 1 then
114 Num_Saved_Zeroes := Num_Saved_Zeroes + 1;
116 -- Here for a non-zero digit
118 else
119 -- First deal with any previously saved zeroes
121 if Num_Saved_Zeroes /= 0 then
122 while Num_Saved_Zeroes > Maxpow loop
123 Uval := Uval * Powten (Maxpow);
124 Num_Saved_Zeroes := Num_Saved_Zeroes - Maxpow;
125 Scale := Scale - Maxpow;
126 end loop;
128 Uval := Uval * Powten (Num_Saved_Zeroes);
129 Scale := Scale - Num_Saved_Zeroes;
131 Num_Saved_Zeroes := 0;
132 end if;
134 -- Accumulate new digit
136 Uval := Uval * 10.0 + Long_Long_Float (Digit);
137 Scale := Scale - After_Point;
138 end if;
140 -- Done if end of input field
142 if P > Max then
143 return;
145 -- Check next character
147 elsif Str (P) not in Digs then
148 if Str (P) = '_' then
149 Scan_Underscore (Str, P, Ptr, Max, False);
150 else
151 return;
152 end if;
153 end if;
154 end loop;
155 end Scanf;
157 -- Start of processing for System.Scan_Real
159 begin
160 Reset;
161 Scan_Sign (Str, Ptr, Max, Minus, Start);
162 P := Ptr.all;
163 Ptr.all := Start;
165 -- If digit, scan numeral before point
167 if Str (P) in Digs then
168 Uval := 0.0;
169 Scanf;
171 -- Initial point, allowed only if followed by digit (RM 3.5(47))
173 elsif Str (P) = '.'
174 and then P < Max
175 and then Str (P + 1) in Digs
176 then
177 Uval := 0.0;
179 -- Any other initial character is an error
181 else
182 raise Constraint_Error;
183 end if;
185 -- Deal with based case
187 if P < Max and then (Str (P) = ':' or else Str (P) = '#') then
188 declare
189 Base_Char : constant Character := Str (P);
190 Digit : Natural;
191 Fdigit : Long_Long_Float;
193 begin
194 -- Set bad base if out of range, and use safe base of 16.0,
195 -- to guard against division by zero in the loop below.
197 if Uval < 2.0 or else Uval > 16.0 then
198 Bad_Base := True;
199 Uval := 16.0;
200 end if;
202 Base := Uval;
203 Uval := 0.0;
204 P := P + 1;
206 -- Special check to allow initial point (RM 3.5(49))
208 if Str (P) = '.' then
209 After_Point := 1;
210 P := P + 1;
211 end if;
213 -- Loop to scan digits of based number. On entry to the loop we
214 -- must have a valid digit. If we don't, then we have an illegal
215 -- floating-point value, and we raise Constraint_Error, note that
216 -- Ptr at this stage was reset to the proper (Start) value.
218 loop
219 if P > Max then
220 raise Constraint_Error;
222 elsif Str (P) in Digs then
223 Digit := Character'Pos (Str (P)) - Character'Pos ('0');
225 elsif Str (P) in 'A' .. 'F' then
226 Digit :=
227 Character'Pos (Str (P)) - (Character'Pos ('A') - 10);
229 elsif Str (P) in 'a' .. 'f' then
230 Digit :=
231 Character'Pos (Str (P)) - (Character'Pos ('a') - 10);
233 else
234 raise Constraint_Error;
235 end if;
237 -- Save up trailing zeroes after the decimal point
239 if Digit = 0 and After_Point = 1 then
240 Num_Saved_Zeroes := Num_Saved_Zeroes + 1;
242 -- Here for a non-zero digit
244 else
245 -- First deal with any previously saved zeroes
247 if Num_Saved_Zeroes /= 0 then
248 Uval := Uval * Base ** Num_Saved_Zeroes;
249 Scale := Scale - Num_Saved_Zeroes;
250 Num_Saved_Zeroes := 0;
251 end if;
253 -- Now accumulate the new digit
255 Fdigit := Long_Long_Float (Digit);
257 if Fdigit >= Base then
258 Bad_Base := True;
259 else
260 Scale := Scale - After_Point;
261 Uval := Uval * Base + Fdigit;
262 end if;
263 end if;
265 P := P + 1;
267 if P > Max then
268 raise Constraint_Error;
270 elsif Str (P) = '_' then
271 Scan_Underscore (Str, P, Ptr, Max, True);
273 else
274 -- Skip past period after digit. Note that the processing
275 -- here will permit either a digit after the period, or the
276 -- terminating base character, as allowed in (RM 3.5(48))
278 if Str (P) = '.' and then After_Point = 0 then
279 P := P + 1;
280 After_Point := 1;
282 if P > Max then
283 raise Constraint_Error;
284 end if;
285 end if;
287 exit when Str (P) = Base_Char;
288 end if;
289 end loop;
291 -- Based number successfully scanned out (point was found)
293 Ptr.all := P + 1;
294 end;
296 -- Non-based case, check for being at decimal point now. Note that
297 -- in Ada 95, we do not insist on a decimal point being present
299 else
300 Base := 10.0;
301 After_Point := 1;
303 if P <= Max and then Str (P) = '.' then
304 P := P + 1;
306 -- Scan digits after point if any are present (RM 3.5(46))
308 if P <= Max and then Str (P) in Digs then
309 Scanf;
310 end if;
311 end if;
313 Ptr.all := P;
314 end if;
316 -- At this point, we have Uval containing the digits of the value as
317 -- an integer, and Scale indicates the negative of the number of digits
318 -- after the point. Base contains the base value (an integral value in
319 -- the range 2.0 .. 16.0). Test for exponent, must be at least one
320 -- character after the E for the exponent to be valid.
322 Scale := Scale + Scan_Exponent (Str, Ptr, Max, Real => True);
324 -- At this point the exponent has been scanned if one is present and
325 -- Scale is adjusted to include the exponent value. Uval contains the
326 -- the integral value which is to be multiplied by Base ** Scale.
328 -- If base is not 10, use exponentiation for scaling
330 if Base /= 10.0 then
331 Uval := Uval * Base ** Scale;
333 -- For base 10, use power of ten table, repeatedly if necessary.
335 elsif Scale > 0 then
336 while Scale > Maxpow loop
337 Uval := Uval * Powten (Maxpow);
338 Scale := Scale - Maxpow;
339 end loop;
341 if Scale > 0 then
342 Uval := Uval * Powten (Scale);
343 end if;
345 elsif Scale < 0 then
346 while (-Scale) > Maxpow loop
347 Uval := Uval / Powten (Maxpow);
348 Scale := Scale + Maxpow;
349 end loop;
351 if Scale < 0 then
352 Uval := Uval / Powten (-Scale);
353 end if;
354 end if;
356 -- Here is where we check for a bad based number
358 if Bad_Base then
359 raise Constraint_Error;
361 -- If OK, then deal with initial minus sign, note that this processing
362 -- is done even if Uval is zero, so that -0.0 is correctly interpreted.
364 else
365 if Minus then
366 return -Uval;
367 else
368 return Uval;
369 end if;
370 end if;
371 end Scan_Real;
373 ----------------
374 -- Value_Real --
375 ----------------
377 function Value_Real (Str : String) return Long_Long_Float is
378 V : Long_Long_Float;
379 P : aliased Integer := Str'First;
381 begin
382 V := Scan_Real (Str, P'Access, Str'Last);
383 Scan_Trailing_Blanks (Str, P);
384 return V;
385 end Value_Real;
387 end System.Val_Real;