* config/fpu-387.h (set_fpu): Add "=m" for stmxcsr.
[official-gcc.git] / libgfortran / io / read.c
bloba3a221ae14666684f135348556760d363c9b4594
1 /* Copyright (C) 2002-2003 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
4 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6 Libgfortran is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 In addition to the permissions in the GNU General Public License, the
12 Free Software Foundation gives you unlimited permission to link the
13 compiled version of this file into combinations with other programs,
14 and to distribute those combinations without any restriction coming
15 from the use of this file. (The General Public License restrictions
16 do apply in other respects; for example, they cover modification of
17 the file, and distribution when not linked into a combine
18 executable.)
20 Libgfortran is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with Libgfortran; see the file COPYING. If not, write to
27 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
28 Boston, MA 02110-1301, USA. */
31 #include "config.h"
32 #include <string.h>
33 #include <errno.h>
34 #include <ctype.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include "libgfortran.h"
38 #include "io.h"
40 /* read.c -- Deal with formatted reads */
42 /* set_integer()-- All of the integer assignments come here to
43 * actually place the value into memory. */
45 void
46 set_integer (void *dest, GFC_INTEGER_LARGEST value, int length)
48 switch (length)
50 #ifdef HAVE_GFC_INTEGER_16
51 case 16:
53 GFC_INTEGER_16 tmp = value;
54 memcpy (dest, (void *) &tmp, length);
56 break;
57 #endif
58 case 8:
60 GFC_INTEGER_8 tmp = value;
61 memcpy (dest, (void *) &tmp, length);
63 break;
64 case 4:
66 GFC_INTEGER_4 tmp = value;
67 memcpy (dest, (void *) &tmp, length);
69 break;
70 case 2:
72 GFC_INTEGER_2 tmp = value;
73 memcpy (dest, (void *) &tmp, length);
75 break;
76 case 1:
78 GFC_INTEGER_1 tmp = value;
79 memcpy (dest, (void *) &tmp, length);
81 break;
82 default:
83 internal_error ("Bad integer kind");
88 /* max_value()-- Given a length (kind), return the maximum signed or
89 * unsigned value */
91 GFC_UINTEGER_LARGEST
92 max_value (int length, int signed_flag)
94 GFC_UINTEGER_LARGEST value;
95 int n;
97 switch (length)
99 #if defined HAVE_GFC_REAL_16 || defined HAVE_GFC_REAL_10
100 case 16:
101 case 10:
102 value = 1;
103 for (n = 1; n < 4 * length; n++)
104 value = (value << 2) + 3;
105 if (! signed_flag)
106 value = 2*value+1;
107 break;
108 #endif
109 case 8:
110 value = signed_flag ? 0x7fffffffffffffff : 0xffffffffffffffff;
111 break;
112 case 4:
113 value = signed_flag ? 0x7fffffff : 0xffffffff;
114 break;
115 case 2:
116 value = signed_flag ? 0x7fff : 0xffff;
117 break;
118 case 1:
119 value = signed_flag ? 0x7f : 0xff;
120 break;
121 default:
122 internal_error ("Bad integer kind");
125 return value;
129 /* convert_real()-- Convert a character representation of a floating
130 * point number to the machine number. Returns nonzero if there is a
131 * range problem during conversion. TODO: handle not-a-numbers and
132 * infinities. */
135 convert_real (void *dest, const char *buffer, int length)
137 errno = 0;
139 switch (length)
141 case 4:
143 GFC_REAL_4 tmp =
144 #if defined(HAVE_STRTOF)
145 strtof (buffer, NULL);
146 #else
147 (GFC_REAL_4) strtod (buffer, NULL);
148 #endif
149 memcpy (dest, (void *) &tmp, length);
151 break;
152 case 8:
154 GFC_REAL_8 tmp = strtod (buffer, NULL);
155 memcpy (dest, (void *) &tmp, length);
157 break;
158 #if defined(HAVE_GFC_REAL_10) && defined (HAVE_STRTOLD)
159 case 10:
161 GFC_REAL_10 tmp = strtold (buffer, NULL);
162 memcpy (dest, (void *) &tmp, length);
164 break;
165 #endif
166 #if defined(HAVE_GFC_REAL_16) && defined (HAVE_STRTOLD)
167 case 16:
169 GFC_REAL_16 tmp = strtold (buffer, NULL);
170 memcpy (dest, (void *) &tmp, length);
172 break;
173 #endif
174 default:
175 internal_error ("Unsupported real kind during IO");
178 if (errno != 0 && errno != EINVAL)
180 generate_error (ERROR_READ_VALUE,
181 "Range error during floating point read");
182 return 1;
185 return 0;
189 /* read_l()-- Read a logical value */
191 void
192 read_l (fnode * f, char *dest, int length)
194 char *p;
195 int w;
197 w = f->u.w;
198 p = read_block (&w);
199 if (p == NULL)
200 return;
202 while (*p == ' ')
204 if (--w == 0)
205 goto bad;
206 p++;
209 if (*p == '.')
211 if (--w == 0)
212 goto bad;
213 p++;
216 switch (*p)
218 case 't':
219 case 'T':
220 set_integer (dest, (GFC_INTEGER_LARGEST) 1, length);
221 break;
222 case 'f':
223 case 'F':
224 set_integer (dest, (GFC_INTEGER_LARGEST) 0, length);
225 break;
226 default:
227 bad:
228 generate_error (ERROR_READ_VALUE, "Bad value on logical read");
229 break;
234 /* read_a()-- Read a character record. This one is pretty easy. */
236 void
237 read_a (fnode * f, char *p, int length)
239 char *source;
240 int w, m, n;
242 w = f->u.w;
243 if (w == -1) /* '(A)' edit descriptor */
244 w = length;
246 source = read_block (&w);
247 if (source == NULL)
248 return;
249 if (w > length)
250 source += (w - length);
252 m = (w > length) ? length : w;
253 memcpy (p, source, m);
255 n = length - w;
256 if (n > 0)
257 memset (p + m, ' ', n);
261 /* eat_leading_spaces()-- Given a character pointer and a width,
262 * ignore the leading spaces. */
264 static char *
265 eat_leading_spaces (int *width, char *p)
267 for (;;)
269 if (*width == 0 || *p != ' ')
270 break;
272 (*width)--;
273 p++;
276 return p;
280 static char
281 next_char (char **p, int *w)
283 char c, *q;
285 if (*w == 0)
286 return '\0';
288 q = *p;
289 c = *q++;
290 *p = q;
292 (*w)--;
294 if (c != ' ')
295 return c;
296 if (g.blank_status != BLANK_UNSPECIFIED)
297 return ' '; /* return a blank to signal a null */
299 /* At this point, the rest of the field has to be trailing blanks */
301 while (*w > 0)
303 if (*q++ != ' ')
304 return '?';
305 (*w)--;
308 *p = q;
309 return '\0';
313 /* read_decimal()-- Read a decimal integer value. The values here are
314 * signed values. */
316 void
317 read_decimal (fnode * f, char *dest, int length)
319 GFC_UINTEGER_LARGEST value, maxv, maxv_10;
320 GFC_INTEGER_LARGEST v;
321 int w, negative;
322 char c, *p;
324 w = f->u.w;
325 p = read_block (&w);
326 if (p == NULL)
327 return;
329 p = eat_leading_spaces (&w, p);
330 if (w == 0)
332 set_integer (dest, (GFC_INTEGER_LARGEST) 0, length);
333 return;
336 maxv = max_value (length, 1);
337 maxv_10 = maxv / 10;
339 negative = 0;
340 value = 0;
342 switch (*p)
344 case '-':
345 negative = 1;
346 /* Fall through */
348 case '+':
349 p++;
350 if (--w == 0)
351 goto bad;
352 /* Fall through */
354 default:
355 break;
358 /* At this point we have a digit-string */
359 value = 0;
361 for (;;)
363 c = next_char (&p, &w);
364 if (c == '\0')
365 break;
367 if (c == ' ')
369 if (g.blank_status == BLANK_NULL) continue;
370 if (g.blank_status == BLANK_ZERO) c = '0';
373 if (c < '0' || c > '9')
374 goto bad;
376 if (value > maxv_10)
377 goto overflow;
379 c -= '0';
380 value = 10 * value;
382 if (value > maxv - c)
383 goto overflow;
384 value += c;
387 v = value;
388 if (negative)
389 v = -v;
391 set_integer (dest, v, length);
392 return;
394 bad:
395 generate_error (ERROR_READ_VALUE, "Bad value during integer read");
396 return;
398 overflow:
399 generate_error (ERROR_READ_OVERFLOW,
400 "Value overflowed during integer read");
401 return;
405 /* read_radix()-- This function reads values for non-decimal radixes.
406 * The difference here is that we treat the values here as unsigned
407 * values for the purposes of overflow. If minus sign is present and
408 * the top bit is set, the value will be incorrect. */
410 void
411 read_radix (fnode * f, char *dest, int length, int radix)
413 GFC_UINTEGER_LARGEST value, maxv, maxv_r;
414 GFC_INTEGER_LARGEST v;
415 int w, negative;
416 char c, *p;
418 w = f->u.w;
419 p = read_block (&w);
420 if (p == NULL)
421 return;
423 p = eat_leading_spaces (&w, p);
424 if (w == 0)
426 set_integer (dest, (GFC_INTEGER_LARGEST) 0, length);
427 return;
430 maxv = max_value (length, 0);
431 maxv_r = maxv / radix;
433 negative = 0;
434 value = 0;
436 switch (*p)
438 case '-':
439 negative = 1;
440 /* Fall through */
442 case '+':
443 p++;
444 if (--w == 0)
445 goto bad;
446 /* Fall through */
448 default:
449 break;
452 /* At this point we have a digit-string */
453 value = 0;
455 for (;;)
457 c = next_char (&p, &w);
458 if (c == '\0')
459 break;
460 if (c == ' ')
462 if (g.blank_status == BLANK_NULL) continue;
463 if (g.blank_status == BLANK_ZERO) c = '0';
466 switch (radix)
468 case 2:
469 if (c < '0' || c > '1')
470 goto bad;
471 break;
473 case 8:
474 if (c < '0' || c > '7')
475 goto bad;
476 break;
478 case 16:
479 switch (c)
481 case '0':
482 case '1':
483 case '2':
484 case '3':
485 case '4':
486 case '5':
487 case '6':
488 case '7':
489 case '8':
490 case '9':
491 break;
493 case 'a':
494 case 'b':
495 case 'c':
496 case 'd':
497 case 'e':
498 case 'f':
499 c = c - 'a' + '9' + 1;
500 break;
502 case 'A':
503 case 'B':
504 case 'C':
505 case 'D':
506 case 'E':
507 case 'F':
508 c = c - 'A' + '9' + 1;
509 break;
511 default:
512 goto bad;
515 break;
518 if (value > maxv_r)
519 goto overflow;
521 c -= '0';
522 value = radix * value;
524 if (maxv - c < value)
525 goto overflow;
526 value += c;
529 v = value;
530 if (negative)
531 v = -v;
533 set_integer (dest, v, length);
534 return;
536 bad:
537 generate_error (ERROR_READ_VALUE, "Bad value during integer read");
538 return;
540 overflow:
541 generate_error (ERROR_READ_OVERFLOW,
542 "Value overflowed during integer read");
543 return;
547 /* read_f()-- Read a floating point number with F-style editing, which
548 is what all of the other floating point descriptors behave as. The
549 tricky part is that optional spaces are allowed after an E or D,
550 and the implicit decimal point if a decimal point is not present in
551 the input. */
553 void
554 read_f (fnode * f, char *dest, int length)
556 int w, seen_dp, exponent;
557 int exponent_sign, val_sign;
558 int ndigits;
559 int edigits;
560 int i;
561 char *p, *buffer;
562 char *digits;
564 val_sign = 1;
565 seen_dp = 0;
566 w = f->u.w;
567 p = read_block (&w);
568 if (p == NULL)
569 return;
571 p = eat_leading_spaces (&w, p);
572 if (w == 0)
573 goto zero;
575 /* Optional sign */
577 if (*p == '-' || *p == '+')
579 if (*p == '-')
580 val_sign = -1;
581 p++;
582 w--;
585 exponent_sign = 1;
586 p = eat_leading_spaces (&w, p);
587 if (w == 0)
588 goto zero;
590 /* A digit, a '.' or a exponent character ('e', 'E', 'd' or 'D')
591 is required at this point */
593 if (!isdigit (*p) && *p != '.' && *p != 'd' && *p != 'D'
594 && *p != 'e' && *p != 'E')
595 goto bad_float;
597 /* Remember the position of the first digit. */
598 digits = p;
599 ndigits = 0;
601 /* Scan through the string to find the exponent. */
602 while (w > 0)
604 switch (*p)
606 case '.':
607 if (seen_dp)
608 goto bad_float;
609 seen_dp = 1;
610 /* Fall through */
612 case '0':
613 case '1':
614 case '2':
615 case '3':
616 case '4':
617 case '5':
618 case '6':
619 case '7':
620 case '8':
621 case '9':
622 case ' ':
623 ndigits++;
624 p++;
625 w--;
626 break;
628 case '-':
629 exponent_sign = -1;
630 /* Fall through */
632 case '+':
633 p++;
634 w--;
635 goto exp2;
637 case 'd':
638 case 'e':
639 case 'D':
640 case 'E':
641 p++;
642 w--;
643 goto exp1;
645 default:
646 goto bad_float;
650 /* No exponent has been seen, so we use the current scale factor */
651 exponent = -g.scale_factor;
652 goto done;
654 bad_float:
655 generate_error (ERROR_READ_VALUE, "Bad value during floating point read");
656 return;
658 /* The value read is zero */
659 zero:
660 switch (length)
662 case 4:
663 *((GFC_REAL_4 *) dest) = 0;
664 break;
666 case 8:
667 *((GFC_REAL_8 *) dest) = 0;
668 break;
670 #ifdef HAVE_GFC_REAL_10
671 case 10:
672 *((GFC_REAL_10 *) dest) = 0;
673 break;
674 #endif
676 #ifdef HAVE_GFC_REAL_16
677 case 16:
678 *((GFC_REAL_16 *) dest) = 0;
679 break;
680 #endif
682 default:
683 internal_error ("Unsupported real kind during IO");
685 return;
687 /* At this point the start of an exponent has been found */
688 exp1:
689 while (w > 0 && *p == ' ')
691 w--;
692 p++;
695 switch (*p)
697 case '-':
698 exponent_sign = -1;
699 /* Fall through */
701 case '+':
702 p++;
703 w--;
704 break;
707 if (w == 0)
708 goto bad_float;
710 /* At this point a digit string is required. We calculate the value
711 of the exponent in order to take account of the scale factor and
712 the d parameter before explict conversion takes place. */
713 exp2:
714 if (!isdigit (*p))
715 goto bad_float;
717 exponent = *p - '0';
718 p++;
719 w--;
721 if (g.blank_status == BLANK_UNSPECIFIED) /* Normal processing of exponent */
723 while (w > 0 && isdigit (*p))
725 exponent = 10 * exponent + *p - '0';
726 p++;
727 w--;
730 /* Only allow trailing blanks */
732 while (w > 0)
734 if (*p != ' ')
735 goto bad_float;
736 p++;
737 w--;
740 else /* BZ or BN status is enabled */
742 while (w > 0)
744 if (*p == ' ')
746 if (g.blank_status == BLANK_ZERO) *p = '0';
747 if (g.blank_status == BLANK_NULL)
749 p++;
750 w--;
751 continue;
754 else if (!isdigit (*p))
755 goto bad_float;
757 exponent = 10 * exponent + *p - '0';
758 p++;
759 w--;
763 exponent = exponent * exponent_sign;
765 done:
766 /* Use the precision specified in the format if no decimal point has been
767 seen. */
768 if (!seen_dp)
769 exponent -= f->u.real.d;
771 if (exponent > 0)
773 edigits = 2;
774 i = exponent;
776 else
778 edigits = 3;
779 i = -exponent;
782 while (i >= 10)
784 i /= 10;
785 edigits++;
788 i = ndigits + edigits + 1;
789 if (val_sign < 0)
790 i++;
792 if (i < SCRATCH_SIZE)
793 buffer = scratch;
794 else
795 buffer = get_mem (i);
797 /* Reformat the string into a temporary buffer. As we're using atof it's
798 easiest to just leave the decimal point in place. */
799 p = buffer;
800 if (val_sign < 0)
801 *(p++) = '-';
802 for (; ndigits > 0; ndigits--)
804 if (*digits == ' ')
806 if (g.blank_status == BLANK_ZERO) *digits = '0';
807 if (g.blank_status == BLANK_NULL)
809 digits++;
810 continue;
813 *p = *digits;
814 p++;
815 digits++;
817 *(p++) = 'e';
818 sprintf (p, "%d", exponent);
820 /* Do the actual conversion. */
821 convert_real (dest, buffer, length);
823 if (buffer != scratch)
824 free_mem (buffer);
826 return;
830 /* read_x()-- Deal with the X/TR descriptor. We just read some data
831 * and never look at it. */
833 void
834 read_x (int n)
836 if ((current_unit->flags.pad == PAD_NO || is_internal_unit ())
837 && current_unit->bytes_left < n)
838 n = current_unit->bytes_left;
840 if (n > 0)
841 read_block (&n);