]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/fortran/simplify.c
1 /* Simplify intrinsic functions at compile-time.
2 Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
3 Contributed by Andy Vaught & Katherine Holcomb
5 This file is part of GNU G95.
7 GNU G95 is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU G95 is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU G95; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
30 #include "intrinsic.h"
32 static mpf_t mpf_zero
, mpf_half
, mpf_one
;
33 static mpz_t mpz_zero
;
35 gfc_expr gfc_bad_expr
;
38 /* Note that 'simplification' is not just transforming expressions.
39 For functions that are not simplified at compile time, range
40 checking is done if possible.
42 The return convention is that each simplification function returns:
44 A new expression node corresponding to the simplified arguments.
45 The original arguments are destroyed by the caller, and must not
46 be a part of the new expression.
48 NULL pointer indicating that no simplification was possible and
49 the original expression should remain intact. If the
50 simplification function sets the type and/or the function name
51 via the pointer gfc_simple_expression, then this type is
54 An expression pointer to gfc_bad_expr (a static placeholder)
55 indicating that some error has prevented simplification. For
56 example, sqrt(-1.0). The error is generated within the function
57 and should be propagated upwards
59 By the time a simplification function gets control, it has been
60 decided that the function call is really supposed to be the
61 intrinsic. No type checking is strictly necessary, since only
62 valid types will be passed on. On the other hand, a simplification
63 subroutine may have to look at the type of an argument as part of
66 Array arguments are never passed to these subroutines.
68 The functions in this file don't have much comment with them, but
69 everything is reasonably straight-forward. The Standard, chapter 13
70 is the best comment you'll find for this file anyway. */
72 /* Static table for converting non-ascii character sets to ascii.
73 The xascii_table[] is the inverse table. */
75 static int ascii_table
[256] = {
76 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
77 '\b', '\t', '\n', '\v', '\0', '\r', '\0', '\0',
78 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
79 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
80 ' ', '!', '\'', '#', '$', '%', '&', '\'',
81 '(', ')', '*', '+', ',', '-', '.', '/',
82 '0', '1', '2', '3', '4', '5', '6', '7',
83 '8', '9', ':', ';', '<', '=', '>', '?',
84 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
85 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
86 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
87 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
88 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
89 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
90 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
91 'x', 'y', 'z', '{', '|', '}', '~', '\?'
94 static int xascii_table
[256];
97 /* Range checks an expression node. If all goes well, returns the
98 node, otherwise returns &gfc_bad_expr and frees the node. */
101 range_check (gfc_expr
* result
, const char *name
)
104 if (gfc_range_check (result
) == ARITH_OK
)
107 gfc_error ("Result of %s overflows its kind at %L", name
, &result
->where
);
108 gfc_free_expr (result
);
109 return &gfc_bad_expr
;
113 /* A helper function that gets an optional and possibly missing
114 kind parameter. Returns the kind, -1 if something went wrong. */
117 get_kind (bt type
, gfc_expr
* k
, const char *name
, int default_kind
)
124 if (k
->expr_type
!= EXPR_CONSTANT
)
126 gfc_error ("KIND parameter of %s at %L must be an initialization "
127 "expression", name
, &k
->where
);
132 if (gfc_extract_int (k
, &kind
) != NULL
133 || gfc_validate_kind (type
, kind
) == -1)
136 gfc_error ("Invalid KIND parameter of %s at %L", name
, &k
->where
);
144 /********************** Simplification functions *****************************/
147 gfc_simplify_abs (gfc_expr
* e
)
152 if (e
->expr_type
!= EXPR_CONSTANT
)
158 result
= gfc_constant_result (BT_INTEGER
, e
->ts
.kind
, &e
->where
);
160 mpz_abs (result
->value
.integer
, e
->value
.integer
);
162 result
= range_check (result
, "IABS");
166 result
= gfc_constant_result (BT_REAL
, e
->ts
.kind
, &e
->where
);
168 mpf_abs (result
->value
.real
, e
->value
.real
);
170 result
= range_check (result
, "ABS");
174 result
= gfc_constant_result (BT_REAL
, e
->ts
.kind
, &e
->where
);
177 mpf_mul (a
, e
->value
.complex.r
, e
->value
.complex.r
);
180 mpf_mul (b
, e
->value
.complex.i
, e
->value
.complex.i
);
183 mpf_sqrt (result
->value
.real
, a
);
188 result
= range_check (result
, "CABS");
192 gfc_internal_error ("gfc_simplify_abs(): Bad type");
200 gfc_simplify_achar (gfc_expr
* e
)
205 if (e
->expr_type
!= EXPR_CONSTANT
)
208 /* We cannot assume that the native character set is ASCII in this
210 if (gfc_extract_int (e
, &index
) != NULL
|| index
< 0 || index
> 127)
212 gfc_error ("Extended ASCII not implemented: argument of ACHAR at %L "
213 "must be between 0 and 127", &e
->where
);
214 return &gfc_bad_expr
;
217 result
= gfc_constant_result (BT_CHARACTER
, gfc_default_character_kind (),
220 result
->value
.character
.string
= gfc_getmem (2);
222 result
->value
.character
.length
= 1;
223 result
->value
.character
.string
[0] = ascii_table
[index
];
224 result
->value
.character
.string
[1] = '\0'; /* For debugger */
230 gfc_simplify_acos (gfc_expr
* x
)
233 mpf_t negative
, square
, term
;
235 if (x
->expr_type
!= EXPR_CONSTANT
)
238 if (mpf_cmp_si (x
->value
.real
, 1) > 0 || mpf_cmp_si (x
->value
.real
, -1) < 0)
240 gfc_error ("Argument of ACOS at %L must be between -1 and 1",
242 return &gfc_bad_expr
;
245 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
247 if (mpf_cmp_si (x
->value
.real
, 1) == 0)
249 mpf_set_ui (result
->value
.real
, 0);
250 return range_check (result
, "ACOS");
253 if (mpf_cmp_si (x
->value
.real
, -1) == 0)
255 mpf_set (result
->value
.real
, pi
);
256 return range_check (result
, "ACOS");
263 mpf_pow_ui (square
, x
->value
.real
, 2);
264 mpf_ui_sub (term
, 1, square
);
265 mpf_sqrt (term
, term
);
266 mpf_div (term
, x
->value
.real
, term
);
267 mpf_neg (term
, term
);
268 arctangent (&term
, &negative
);
269 mpf_add (result
->value
.real
, half_pi
, negative
);
271 mpf_clear (negative
);
275 return range_check (result
, "ACOS");
280 gfc_simplify_adjustl (gfc_expr
* e
)
286 if (e
->expr_type
!= EXPR_CONSTANT
)
289 len
= e
->value
.character
.length
;
291 result
= gfc_constant_result (BT_CHARACTER
, e
->ts
.kind
, &e
->where
);
293 result
->value
.character
.length
= len
;
294 result
->value
.character
.string
= gfc_getmem (len
+ 1);
296 for (count
= 0, i
= 0; i
< len
; ++i
)
298 ch
= e
->value
.character
.string
[i
];
304 for (i
= 0; i
< len
- count
; ++i
)
306 result
->value
.character
.string
[i
] =
307 e
->value
.character
.string
[count
+ i
];
310 for (i
= len
- count
; i
< len
; ++i
)
312 result
->value
.character
.string
[i
] = ' ';
315 result
->value
.character
.string
[len
] = '\0'; /* For debugger */
322 gfc_simplify_adjustr (gfc_expr
* e
)
328 if (e
->expr_type
!= EXPR_CONSTANT
)
331 len
= e
->value
.character
.length
;
333 result
= gfc_constant_result (BT_CHARACTER
, e
->ts
.kind
, &e
->where
);
335 result
->value
.character
.length
= len
;
336 result
->value
.character
.string
= gfc_getmem (len
+ 1);
338 for (count
= 0, i
= len
- 1; i
>= 0; --i
)
340 ch
= e
->value
.character
.string
[i
];
346 for (i
= 0; i
< count
; ++i
)
348 result
->value
.character
.string
[i
] = ' ';
351 for (i
= count
; i
< len
; ++i
)
353 result
->value
.character
.string
[i
] =
354 e
->value
.character
.string
[i
- count
];
357 result
->value
.character
.string
[len
] = '\0'; /* For debugger */
364 gfc_simplify_aimag (gfc_expr
* e
)
368 if (e
->expr_type
!= EXPR_CONSTANT
)
371 result
= gfc_constant_result (BT_REAL
, e
->ts
.kind
, &e
->where
);
372 mpf_set (result
->value
.real
, e
->value
.complex.i
);
374 return range_check (result
, "AIMAG");
379 gfc_simplify_aint (gfc_expr
* e
, gfc_expr
* k
)
381 gfc_expr
*rtrunc
, *result
;
384 kind
= get_kind (BT_REAL
, k
, "AINT", e
->ts
.kind
);
386 return &gfc_bad_expr
;
388 if (e
->expr_type
!= EXPR_CONSTANT
)
391 rtrunc
= gfc_copy_expr (e
);
393 mpf_trunc (rtrunc
->value
.real
, e
->value
.real
);
395 result
= gfc_real2real (rtrunc
, kind
);
396 gfc_free_expr (rtrunc
);
398 return range_check (result
, "AINT");
403 gfc_simplify_dint (gfc_expr
* e
)
405 gfc_expr
*rtrunc
, *result
;
407 if (e
->expr_type
!= EXPR_CONSTANT
)
410 rtrunc
= gfc_copy_expr (e
);
412 mpf_trunc (rtrunc
->value
.real
, e
->value
.real
);
414 result
= gfc_real2real (rtrunc
, gfc_default_double_kind ());
415 gfc_free_expr (rtrunc
);
417 return range_check (result
, "DINT");
423 gfc_simplify_anint (gfc_expr
* e
, gfc_expr
* k
)
425 gfc_expr
*rtrunc
, *result
;
428 kind
= get_kind (BT_REAL
, k
, "ANINT", e
->ts
.kind
);
430 return &gfc_bad_expr
;
432 if (e
->expr_type
!= EXPR_CONSTANT
)
435 result
= gfc_constant_result (e
->ts
.type
, kind
, &e
->where
);
437 rtrunc
= gfc_copy_expr (e
);
439 cmp
= mpf_cmp_ui (e
->value
.real
, 0);
443 mpf_add (rtrunc
->value
.real
, e
->value
.real
, mpf_half
);
444 mpf_trunc (result
->value
.real
, rtrunc
->value
.real
);
448 mpf_sub (rtrunc
->value
.real
, e
->value
.real
, mpf_half
);
449 mpf_trunc (result
->value
.real
, rtrunc
->value
.real
);
452 mpf_set_ui (result
->value
.real
, 0);
454 gfc_free_expr (rtrunc
);
456 return range_check (result
, "ANINT");
461 gfc_simplify_dnint (gfc_expr
* e
)
463 gfc_expr
*rtrunc
, *result
;
466 if (e
->expr_type
!= EXPR_CONSTANT
)
470 gfc_constant_result (BT_REAL
, gfc_default_double_kind (), &e
->where
);
472 rtrunc
= gfc_copy_expr (e
);
474 cmp
= mpf_cmp_ui (e
->value
.real
, 0);
478 mpf_add (rtrunc
->value
.real
, e
->value
.real
, mpf_half
);
479 mpf_trunc (result
->value
.real
, rtrunc
->value
.real
);
483 mpf_sub (rtrunc
->value
.real
, e
->value
.real
, mpf_half
);
484 mpf_trunc (result
->value
.real
, rtrunc
->value
.real
);
487 mpf_set_ui (result
->value
.real
, 0);
489 gfc_free_expr (rtrunc
);
491 return range_check (result
, "DNINT");
496 gfc_simplify_asin (gfc_expr
* x
)
499 mpf_t negative
, square
, term
;
501 if (x
->expr_type
!= EXPR_CONSTANT
)
504 if (mpf_cmp_si (x
->value
.real
, 1) > 0 || mpf_cmp_si (x
->value
.real
, -1) < 0)
506 gfc_error ("Argument of ASIN at %L must be between -1 and 1",
508 return &gfc_bad_expr
;
511 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
513 if (mpf_cmp_si (x
->value
.real
, 1) == 0)
515 mpf_set (result
->value
.real
, half_pi
);
516 return range_check (result
, "ASIN");
519 if (mpf_cmp_si (x
->value
.real
, -1) == 0)
522 mpf_neg (negative
, half_pi
);
523 mpf_set (result
->value
.real
, negative
);
524 mpf_clear (negative
);
525 return range_check (result
, "ASIN");
531 mpf_pow_ui (square
, x
->value
.real
, 2);
532 mpf_ui_sub (term
, 1, square
);
533 mpf_sqrt (term
, term
);
534 mpf_div (term
, x
->value
.real
, term
);
535 arctangent (&term
, &result
->value
.real
);
540 return range_check (result
, "ASIN");
545 gfc_simplify_atan (gfc_expr
* x
)
549 if (x
->expr_type
!= EXPR_CONSTANT
)
552 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
554 arctangent (&x
->value
.real
, &result
->value
.real
);
556 return range_check (result
, "ATAN");
562 gfc_simplify_atan2 (gfc_expr
* y
, gfc_expr
* x
)
566 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
569 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
572 if (mpf_sgn (y
->value
.real
) == 0 && mpf_sgn (x
->value
.real
) == 0)
575 ("If first argument of ATAN2 %L is zero, the second argument "
576 "must not be zero", &x
->where
);
577 gfc_free_expr (result
);
578 return &gfc_bad_expr
;
581 arctangent2 (&y
->value
.real
, &x
->value
.real
, &result
->value
.real
);
583 return range_check (result
, "ATAN2");
589 gfc_simplify_bit_size (gfc_expr
* e
)
594 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
);
596 gfc_internal_error ("In gfc_simplify_bit_size(): Bad kind");
598 result
= gfc_constant_result (BT_INTEGER
, e
->ts
.kind
, &e
->where
);
599 mpz_set_ui (result
->value
.integer
, gfc_integer_kinds
[i
].bit_size
);
606 gfc_simplify_btest (gfc_expr
* e
, gfc_expr
* bit
)
610 if (e
->expr_type
!= EXPR_CONSTANT
|| bit
->expr_type
!= EXPR_CONSTANT
)
613 if (gfc_extract_int (bit
, &b
) != NULL
|| b
< 0)
614 return gfc_logical_expr (0, &e
->where
);
616 return gfc_logical_expr (mpz_tstbit (e
->value
.integer
, b
), &e
->where
);
621 gfc_simplify_ceiling (gfc_expr
* e
, gfc_expr
* k
)
623 gfc_expr
*ceil
, *result
;
626 kind
= get_kind (BT_REAL
, k
, "CEILING", gfc_default_real_kind ());
628 return &gfc_bad_expr
;
630 if (e
->expr_type
!= EXPR_CONSTANT
)
633 result
= gfc_constant_result (BT_INTEGER
, kind
, &e
->where
);
635 ceil
= gfc_copy_expr (e
);
637 mpf_ceil (ceil
->value
.real
, e
->value
.real
);
638 mpz_set_f (result
->value
.integer
, ceil
->value
.real
);
640 gfc_free_expr (ceil
);
642 return range_check (result
, "CEILING");
647 gfc_simplify_char (gfc_expr
* e
, gfc_expr
* k
)
652 kind
= get_kind (BT_CHARACTER
, k
, "CHAR", gfc_default_character_kind ());
654 return &gfc_bad_expr
;
656 if (e
->expr_type
!= EXPR_CONSTANT
)
659 if (gfc_extract_int (e
, &c
) != NULL
|| c
< 0 || c
> 255)
661 gfc_error ("Bad character in CHAR function at %L", &e
->where
);
662 return &gfc_bad_expr
;
665 result
= gfc_constant_result (BT_CHARACTER
, kind
, &e
->where
);
667 result
->value
.character
.length
= 1;
668 result
->value
.character
.string
= gfc_getmem (2);
670 result
->value
.character
.string
[0] = c
;
671 result
->value
.character
.string
[1] = '\0'; /* For debugger */
677 /* Common subroutine for simplifying CMPLX and DCMPLX. */
680 simplify_cmplx (const char *name
, gfc_expr
* x
, gfc_expr
* y
, int kind
)
684 result
= gfc_constant_result (BT_COMPLEX
, kind
, &x
->where
);
686 mpf_set_ui (result
->value
.complex.i
, 0);
691 mpf_set_z (result
->value
.complex.r
, x
->value
.integer
);
695 mpf_set (result
->value
.complex.r
, x
->value
.real
);
699 mpf_set (result
->value
.complex.r
, x
->value
.complex.r
);
700 mpf_set (result
->value
.complex.i
, x
->value
.complex.i
);
704 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (x)");
712 mpf_set_z (result
->value
.complex.i
, y
->value
.integer
);
716 mpf_set (result
->value
.complex.i
, y
->value
.real
);
720 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (y)");
724 return range_check (result
, name
);
729 gfc_simplify_cmplx (gfc_expr
* x
, gfc_expr
* y
, gfc_expr
* k
)
733 if (x
->expr_type
!= EXPR_CONSTANT
734 || (y
!= NULL
&& y
->expr_type
!= EXPR_CONSTANT
))
737 kind
= get_kind (BT_REAL
, k
, "CMPLX", gfc_default_real_kind ());
739 return &gfc_bad_expr
;
741 return simplify_cmplx ("CMPLX", x
, y
, kind
);
746 gfc_simplify_conjg (gfc_expr
* e
)
750 if (e
->expr_type
!= EXPR_CONSTANT
)
753 result
= gfc_copy_expr (e
);
754 mpf_neg (result
->value
.complex.i
, result
->value
.complex.i
);
756 return range_check (result
, "CONJG");
761 gfc_simplify_cos (gfc_expr
* x
)
766 if (x
->expr_type
!= EXPR_CONSTANT
)
769 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
774 cosine (&x
->value
.real
, &result
->value
.real
);
780 cosine (&x
->value
.complex.r
, &xp
);
781 hypercos (&x
->value
.complex.i
, &xq
);
782 mpf_mul (result
->value
.complex.r
, xp
, xq
);
784 sine (&x
->value
.complex.r
, &xp
);
785 hypersine (&x
->value
.complex.i
, &xq
);
786 mpf_mul (xp
, xp
, xq
);
787 mpf_neg (result
->value
.complex.i
, xp
);
793 gfc_internal_error ("in gfc_simplify_cos(): Bad type");
796 return range_check (result
, "COS");
802 gfc_simplify_cosh (gfc_expr
* x
)
806 if (x
->expr_type
!= EXPR_CONSTANT
)
809 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
811 hypercos (&x
->value
.real
, &result
->value
.real
);
813 return range_check (result
, "COSH");
818 gfc_simplify_dcmplx (gfc_expr
* x
, gfc_expr
* y
)
821 if (x
->expr_type
!= EXPR_CONSTANT
822 || (y
!= NULL
&& y
->expr_type
!= EXPR_CONSTANT
))
825 return simplify_cmplx ("DCMPLX", x
, y
, gfc_default_double_kind ());
830 gfc_simplify_dble (gfc_expr
* e
)
834 if (e
->expr_type
!= EXPR_CONSTANT
)
840 result
= gfc_int2real (e
, gfc_default_double_kind ());
844 result
= gfc_real2real (e
, gfc_default_double_kind ());
848 result
= gfc_complex2real (e
, gfc_default_double_kind ());
852 gfc_internal_error ("gfc_simplify_dble(): bad type at %L", &e
->where
);
855 return range_check (result
, "DBLE");
860 gfc_simplify_digits (gfc_expr
* x
)
864 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
);
871 digits
= gfc_integer_kinds
[i
].digits
;
876 digits
= gfc_real_kinds
[i
].digits
;
881 gfc_internal_error ("gfc_simplify_digits(): Bad type");
884 return gfc_int_expr (digits
);
889 gfc_simplify_dim (gfc_expr
* x
, gfc_expr
* y
)
893 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
896 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
901 if (mpz_cmp (x
->value
.integer
, y
->value
.integer
) > 0)
902 mpz_sub (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
904 mpz_set (result
->value
.integer
, mpz_zero
);
909 if (mpf_cmp (x
->value
.real
, y
->value
.real
) > 0)
910 mpf_sub (result
->value
.real
, x
->value
.real
, y
->value
.real
);
912 mpf_set (result
->value
.real
, mpf_zero
);
917 gfc_internal_error ("gfc_simplify_dim(): Bad type");
920 return range_check (result
, "DIM");
925 gfc_simplify_dprod (gfc_expr
* x
, gfc_expr
* y
)
927 gfc_expr
*mult1
, *mult2
, *result
;
929 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
933 gfc_constant_result (BT_REAL
, gfc_default_double_kind (), &x
->where
);
935 mult1
= gfc_real2real (x
, gfc_default_double_kind ());
936 mult2
= gfc_real2real (y
, gfc_default_double_kind ());
938 mpf_mul (result
->value
.real
, mult1
->value
.real
, mult2
->value
.real
);
940 gfc_free_expr (mult1
);
941 gfc_free_expr (mult2
);
943 return range_check (result
, "DPROD");
948 gfc_simplify_epsilon (gfc_expr
* e
)
953 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
);
955 gfc_internal_error ("gfc_simplify_epsilon(): Bad kind");
957 result
= gfc_constant_result (BT_REAL
, e
->ts
.kind
, &e
->where
);
959 mpf_set (result
->value
.real
, gfc_real_kinds
[i
].epsilon
);
961 return range_check (result
, "EPSILON");
966 gfc_simplify_exp (gfc_expr
* x
)
970 double ln2
, absval
, rhuge
;
972 if (x
->expr_type
!= EXPR_CONSTANT
)
975 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
977 /* Exactitude doesn't matter here */
979 rhuge
= ln2
* mpz_get_d (gfc_integer_kinds
[0].huge
);
984 absval
= mpf_get_d (x
->value
.real
);
989 /* Underflow (set arg to zero) if x is negative and its
990 magnitude is greater than the maximum C long int times
991 ln2, because the exponential method in arith.c will fail
993 if (mpf_cmp_ui (x
->value
.real
, 0) < 0)
997 ("Argument of EXP at %L is negative and too large, "
998 "setting result to zero", &x
->where
);
999 mpf_set_ui (result
->value
.real
, 0);
1000 return range_check (result
, "EXP");
1002 /* Overflow if magnitude of x is greater than C long int
1006 gfc_error ("Argument of EXP at %L too large", &x
->where
);
1007 gfc_free_expr (result
);
1008 return &gfc_bad_expr
;
1011 exponential (&x
->value
.real
, &result
->value
.real
);
1015 /* Using Euler's formula. */
1016 absval
= mpf_get_d (x
->value
.complex.r
);
1021 if (mpf_cmp_ui (x
->value
.complex.r
, 0) < 0)
1025 ("Real part of argument of EXP at %L is negative "
1026 "and too large, setting result to zero", &x
->where
);
1028 mpf_set_ui (result
->value
.complex.r
, 0);
1029 mpf_set_ui (result
->value
.complex.i
, 0);
1030 return range_check (result
, "EXP");
1034 gfc_error ("Real part of argument of EXP at %L too large",
1036 gfc_free_expr (result
);
1037 return &gfc_bad_expr
;
1042 exponential (&x
->value
.complex.r
, &xq
);
1043 cosine (&x
->value
.complex.i
, &xp
);
1044 mpf_mul (result
->value
.complex.r
, xq
, xp
);
1045 sine (&x
->value
.complex.i
, &xp
);
1046 mpf_mul (result
->value
.complex.i
, xq
, xp
);
1052 gfc_internal_error ("in gfc_simplify_exp(): Bad type");
1055 return range_check (result
, "EXP");
1060 gfc_simplify_exponent (gfc_expr
* x
)
1062 mpf_t i2
, absv
, ln2
, lnx
;
1065 if (x
->expr_type
!= EXPR_CONSTANT
)
1068 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind (),
1071 if (mpf_cmp (x
->value
.real
, mpf_zero
) == 0)
1073 mpz_set_ui (result
->value
.integer
, 0);
1077 mpf_init_set_ui (i2
, 2);
1082 natural_logarithm (&i2
, &ln2
);
1084 mpf_abs (absv
, x
->value
.real
);
1085 natural_logarithm (&absv
, &lnx
);
1087 mpf_div (lnx
, lnx
, ln2
);
1088 mpf_trunc (lnx
, lnx
);
1089 mpf_add_ui (lnx
, lnx
, 1);
1090 mpz_set_f (result
->value
.integer
, lnx
);
1097 return range_check (result
, "EXPONENT");
1102 gfc_simplify_float (gfc_expr
* a
)
1106 if (a
->expr_type
!= EXPR_CONSTANT
)
1109 result
= gfc_int2real (a
, gfc_default_real_kind ());
1110 return range_check (result
, "FLOAT");
1115 gfc_simplify_floor (gfc_expr
* e
, gfc_expr
* k
)
1121 kind
= get_kind (BT_REAL
, k
, "FLOOR", gfc_default_real_kind ());
1123 gfc_internal_error ("gfc_simplify_floor(): Bad kind");
1125 if (e
->expr_type
!= EXPR_CONSTANT
)
1128 result
= gfc_constant_result (BT_INTEGER
, kind
, &e
->where
);
1131 mpf_floor (floor
, e
->value
.real
);
1132 mpz_set_f (result
->value
.integer
, floor
);
1135 return range_check (result
, "FLOOR");
1140 gfc_simplify_fraction (gfc_expr
* x
)
1143 mpf_t i2
, absv
, ln2
, lnx
, pow2
;
1146 if (x
->expr_type
!= EXPR_CONSTANT
)
1149 result
= gfc_constant_result (BT_REAL
, x
->ts
.kind
, &x
->where
);
1151 if (mpf_cmp (x
->value
.real
, mpf_zero
) == 0)
1153 mpf_set (result
->value
.real
, mpf_zero
);
1157 mpf_init_set_ui (i2
, 2);
1163 natural_logarithm (&i2
, &ln2
);
1165 mpf_abs (absv
, x
->value
.real
);
1166 natural_logarithm (&absv
, &lnx
);
1168 mpf_div (lnx
, lnx
, ln2
);
1169 mpf_trunc (lnx
, lnx
);
1170 mpf_add_ui (lnx
, lnx
, 1);
1172 exp2
= (unsigned long) mpf_get_d (lnx
);
1173 mpf_pow_ui (pow2
, i2
, exp2
);
1175 mpf_div (result
->value
.real
, absv
, pow2
);
1183 return range_check (result
, "FRACTION");
1188 gfc_simplify_huge (gfc_expr
* e
)
1193 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
);
1197 result
= gfc_constant_result (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
1202 mpz_set (result
->value
.integer
, gfc_integer_kinds
[i
].huge
);
1206 mpf_set (result
->value
.real
, gfc_real_kinds
[i
].huge
);
1211 gfc_internal_error ("gfc_simplify_huge(): Bad type");
1219 gfc_simplify_iachar (gfc_expr
* e
)
1224 if (e
->expr_type
!= EXPR_CONSTANT
)
1227 if (e
->value
.character
.length
!= 1)
1229 gfc_error ("Argument of IACHAR at %L must be of length one", &e
->where
);
1230 return &gfc_bad_expr
;
1233 index
= xascii_table
[(int) e
->value
.character
.string
[0] & 0xFF];
1235 result
= gfc_int_expr (index
);
1236 result
->where
= e
->where
;
1238 return range_check (result
, "IACHAR");
1243 gfc_simplify_iand (gfc_expr
* x
, gfc_expr
* y
)
1247 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1250 result
= gfc_constant_result (BT_INTEGER
, x
->ts
.kind
, &x
->where
);
1252 mpz_and (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
1254 return range_check (result
, "IAND");
1259 gfc_simplify_ibclr (gfc_expr
* x
, gfc_expr
* y
)
1264 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1267 if (gfc_extract_int (y
, &pos
) != NULL
|| pos
< 0)
1269 gfc_error ("Invalid second argument of IBCLR at %L", &y
->where
);
1270 return &gfc_bad_expr
;
1273 k
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
);
1275 gfc_internal_error ("gfc_simplify_ibclr(): Bad kind");
1277 if (pos
> gfc_integer_kinds
[k
].bit_size
)
1279 gfc_error ("Second argument of IBCLR exceeds bit size at %L",
1281 return &gfc_bad_expr
;
1284 result
= gfc_copy_expr (x
);
1286 mpz_clrbit (result
->value
.integer
, pos
);
1287 return range_check (result
, "IBCLR");
1292 gfc_simplify_ibits (gfc_expr
* x
, gfc_expr
* y
, gfc_expr
* z
)
1299 if (x
->expr_type
!= EXPR_CONSTANT
1300 || y
->expr_type
!= EXPR_CONSTANT
1301 || z
->expr_type
!= EXPR_CONSTANT
)
1304 if (gfc_extract_int (y
, &pos
) != NULL
|| pos
< 0)
1306 gfc_error ("Invalid second argument of IBITS at %L", &y
->where
);
1307 return &gfc_bad_expr
;
1310 if (gfc_extract_int (z
, &len
) != NULL
|| len
< 0)
1312 gfc_error ("Invalid third argument of IBITS at %L", &z
->where
);
1313 return &gfc_bad_expr
;
1316 k
= gfc_validate_kind (BT_INTEGER
, x
->ts
.kind
);
1318 gfc_internal_error ("gfc_simplify_ibits(): Bad kind");
1320 bitsize
= gfc_integer_kinds
[k
].bit_size
;
1322 if (pos
+ len
> bitsize
)
1325 ("Sum of second and third arguments of IBITS exceeds bit size "
1326 "at %L", &y
->where
);
1327 return &gfc_bad_expr
;
1330 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1332 bits
= gfc_getmem (bitsize
* sizeof (int));
1334 for (i
= 0; i
< bitsize
; i
++)
1337 for (i
= 0; i
< len
; i
++)
1338 bits
[i
] = mpz_tstbit (x
->value
.integer
, i
+ pos
);
1340 for (i
= 0; i
< bitsize
; i
++)
1344 mpz_clrbit (result
->value
.integer
, i
);
1346 else if (bits
[i
] == 1)
1348 mpz_setbit (result
->value
.integer
, i
);
1352 gfc_internal_error ("IBITS: Bad bit");
1358 return range_check (result
, "IBITS");
1363 gfc_simplify_ibset (gfc_expr
* x
, gfc_expr
* y
)
1368 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1371 if (gfc_extract_int (y
, &pos
) != NULL
|| pos
< 0)
1373 gfc_error ("Invalid second argument of IBSET at %L", &y
->where
);
1374 return &gfc_bad_expr
;
1377 k
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
);
1379 gfc_internal_error ("gfc_simplify_ibset(): Bad kind");
1381 if (pos
> gfc_integer_kinds
[k
].bit_size
)
1383 gfc_error ("Second argument of IBSET exceeds bit size at %L",
1385 return &gfc_bad_expr
;
1388 result
= gfc_copy_expr (x
);
1390 mpz_setbit (result
->value
.integer
, pos
);
1391 return range_check (result
, "IBSET");
1396 gfc_simplify_ichar (gfc_expr
* e
)
1401 if (e
->expr_type
!= EXPR_CONSTANT
)
1404 if (e
->value
.character
.length
!= 1)
1406 gfc_error ("Argument of ICHAR at %L must be of length one", &e
->where
);
1407 return &gfc_bad_expr
;
1410 index
= (int) e
->value
.character
.string
[0];
1412 if (index
< CHAR_MIN
|| index
> CHAR_MAX
)
1414 gfc_error ("Argument of ICHAR at %L out of range of this processor",
1416 return &gfc_bad_expr
;
1419 result
= gfc_int_expr (index
);
1420 result
->where
= e
->where
;
1421 return range_check (result
, "ICHAR");
1426 gfc_simplify_ieor (gfc_expr
* x
, gfc_expr
* y
)
1430 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1433 result
= gfc_constant_result (BT_INTEGER
, x
->ts
.kind
, &x
->where
);
1435 mpz_xor (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
1437 return range_check (result
, "IEOR");
1442 gfc_simplify_index (gfc_expr
* x
, gfc_expr
* y
, gfc_expr
* b
)
1445 int back
, len
, lensub
;
1446 int i
, j
, k
, count
, index
= 0, start
;
1448 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1451 if (b
!= NULL
&& b
->value
.logical
!= 0)
1456 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind (),
1459 len
= x
->value
.character
.length
;
1460 lensub
= y
->value
.character
.length
;
1464 mpz_set_si (result
->value
.integer
, 0);
1473 mpz_set_si (result
->value
.integer
, 1);
1476 else if (lensub
== 1)
1478 for (i
= 0; i
< len
; i
++)
1480 for (j
= 0; j
< lensub
; j
++)
1482 if (y
->value
.character
.string
[j
] ==
1483 x
->value
.character
.string
[i
])
1493 for (i
= 0; i
< len
; i
++)
1495 for (j
= 0; j
< lensub
; j
++)
1497 if (y
->value
.character
.string
[j
] ==
1498 x
->value
.character
.string
[i
])
1503 for (k
= 0; k
< lensub
; k
++)
1505 if (y
->value
.character
.string
[k
] ==
1506 x
->value
.character
.string
[k
+ start
])
1510 if (count
== lensub
)
1526 mpz_set_si (result
->value
.integer
, len
+ 1);
1529 else if (lensub
== 1)
1531 for (i
= 0; i
< len
; i
++)
1533 for (j
= 0; j
< lensub
; j
++)
1535 if (y
->value
.character
.string
[j
] ==
1536 x
->value
.character
.string
[len
- i
])
1538 index
= len
- i
+ 1;
1546 for (i
= 0; i
< len
; i
++)
1548 for (j
= 0; j
< lensub
; j
++)
1550 if (y
->value
.character
.string
[j
] ==
1551 x
->value
.character
.string
[len
- i
])
1554 if (start
<= len
- lensub
)
1557 for (k
= 0; k
< lensub
; k
++)
1558 if (y
->value
.character
.string
[k
] ==
1559 x
->value
.character
.string
[k
+ start
])
1562 if (count
== lensub
)
1579 mpz_set_si (result
->value
.integer
, index
);
1580 return range_check (result
, "INDEX");
1585 gfc_simplify_int (gfc_expr
* e
, gfc_expr
* k
)
1587 gfc_expr
*rpart
, *rtrunc
, *result
;
1590 kind
= get_kind (BT_REAL
, k
, "INT", gfc_default_real_kind ());
1592 return &gfc_bad_expr
;
1594 if (e
->expr_type
!= EXPR_CONSTANT
)
1597 result
= gfc_constant_result (BT_INTEGER
, kind
, &e
->where
);
1602 mpz_set (result
->value
.integer
, e
->value
.integer
);
1606 rtrunc
= gfc_copy_expr (e
);
1607 mpf_trunc (rtrunc
->value
.real
, e
->value
.real
);
1608 mpz_set_f (result
->value
.integer
, rtrunc
->value
.real
);
1609 gfc_free_expr (rtrunc
);
1613 rpart
= gfc_complex2real (e
, kind
);
1614 rtrunc
= gfc_copy_expr (rpart
);
1615 mpf_trunc (rtrunc
->value
.real
, rpart
->value
.real
);
1616 mpz_set_f (result
->value
.integer
, rtrunc
->value
.real
);
1617 gfc_free_expr (rpart
);
1618 gfc_free_expr (rtrunc
);
1622 gfc_error ("Argument of INT at %L is not a valid type", &e
->where
);
1623 gfc_free_expr (result
);
1624 return &gfc_bad_expr
;
1627 return range_check (result
, "INT");
1632 gfc_simplify_ifix (gfc_expr
* e
)
1634 gfc_expr
*rtrunc
, *result
;
1636 if (e
->expr_type
!= EXPR_CONSTANT
)
1639 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind (),
1642 rtrunc
= gfc_copy_expr (e
);
1644 mpf_trunc (rtrunc
->value
.real
, e
->value
.real
);
1645 mpz_set_f (result
->value
.integer
, rtrunc
->value
.real
);
1647 gfc_free_expr (rtrunc
);
1648 return range_check (result
, "IFIX");
1653 gfc_simplify_idint (gfc_expr
* e
)
1655 gfc_expr
*rtrunc
, *result
;
1657 if (e
->expr_type
!= EXPR_CONSTANT
)
1660 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind (),
1663 rtrunc
= gfc_copy_expr (e
);
1665 mpf_trunc (rtrunc
->value
.real
, e
->value
.real
);
1666 mpz_set_f (result
->value
.integer
, rtrunc
->value
.real
);
1668 gfc_free_expr (rtrunc
);
1669 return range_check (result
, "IDINT");
1674 gfc_simplify_ior (gfc_expr
* x
, gfc_expr
* y
)
1678 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1681 result
= gfc_constant_result (BT_INTEGER
, x
->ts
.kind
, &x
->where
);
1683 mpz_ior (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
1684 return range_check (result
, "IOR");
1689 gfc_simplify_ishft (gfc_expr
* e
, gfc_expr
* s
)
1692 int shift
, ashift
, isize
, k
;
1695 if (e
->expr_type
!= EXPR_CONSTANT
|| s
->expr_type
!= EXPR_CONSTANT
)
1698 if (gfc_extract_int (s
, &shift
) != NULL
)
1700 gfc_error ("Invalid second argument of ISHFT at %L", &s
->where
);
1701 return &gfc_bad_expr
;
1704 k
= gfc_validate_kind (BT_INTEGER
, e
->ts
.kind
);
1706 gfc_internal_error ("gfc_simplify_ishft(): Bad kind");
1708 isize
= gfc_integer_kinds
[k
].bit_size
;
1718 ("Magnitude of second argument of ISHFT exceeds bit size at %L",
1720 return &gfc_bad_expr
;
1723 e_int
= mpz_get_si (e
->value
.integer
);
1724 if (e_int
> INT_MAX
|| e_int
< INT_MIN
)
1725 gfc_internal_error ("ISHFT: unable to extract integer");
1727 result
= gfc_constant_result (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
1731 mpz_set (result
->value
.integer
, e
->value
.integer
);
1732 return range_check (result
, "ISHFT");
1736 mpz_set_si (result
->value
.integer
, e_int
<< shift
);
1738 mpz_set_si (result
->value
.integer
, e_int
>> ashift
);
1740 return range_check (result
, "ISHFT");
1745 gfc_simplify_ishftc (gfc_expr
* e
, gfc_expr
* s
, gfc_expr
* sz
)
1748 int shift
, ashift
, isize
, delta
, k
;
1751 if (e
->expr_type
!= EXPR_CONSTANT
|| s
->expr_type
!= EXPR_CONSTANT
)
1754 if (gfc_extract_int (s
, &shift
) != NULL
)
1756 gfc_error ("Invalid second argument of ISHFTC at %L", &s
->where
);
1757 return &gfc_bad_expr
;
1760 k
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
);
1762 gfc_internal_error ("gfc_simplify_ishftc(): Bad kind");
1766 if (gfc_extract_int (sz
, &isize
) != NULL
|| isize
< 0)
1768 gfc_error ("Invalid third argument of ISHFTC at %L", &sz
->where
);
1769 return &gfc_bad_expr
;
1773 isize
= gfc_integer_kinds
[k
].bit_size
;
1783 ("Magnitude of second argument of ISHFTC exceeds third argument "
1784 "at %L", &s
->where
);
1785 return &gfc_bad_expr
;
1788 result
= gfc_constant_result (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
1790 bits
= gfc_getmem (isize
* sizeof (int));
1792 for (i
= 0; i
< isize
; i
++)
1793 bits
[i
] = mpz_tstbit (e
->value
.integer
, i
);
1795 delta
= isize
- ashift
;
1799 mpz_set (result
->value
.integer
, e
->value
.integer
);
1801 return range_check (result
, "ISHFTC");
1806 for (i
= 0; i
< delta
; i
++)
1809 mpz_clrbit (result
->value
.integer
, i
+ shift
);
1811 mpz_setbit (result
->value
.integer
, i
+ shift
);
1814 for (i
= delta
; i
< isize
; i
++)
1817 mpz_clrbit (result
->value
.integer
, i
- delta
);
1819 mpz_setbit (result
->value
.integer
, i
- delta
);
1823 return range_check (result
, "ISHFTC");
1827 for (i
= 0; i
< ashift
; i
++)
1830 mpz_clrbit (result
->value
.integer
, i
+ delta
);
1832 mpz_setbit (result
->value
.integer
, i
+ delta
);
1835 for (i
= ashift
; i
< isize
; i
++)
1838 mpz_clrbit (result
->value
.integer
, i
+ shift
);
1840 mpz_setbit (result
->value
.integer
, i
+ shift
);
1844 return range_check (result
, "ISHFTC");
1850 gfc_simplify_kind (gfc_expr
* e
)
1853 if (e
->ts
.type
== BT_DERIVED
)
1855 gfc_error ("Argument of KIND at %L is a DERIVED type", &e
->where
);
1856 return &gfc_bad_expr
;
1859 return gfc_int_expr (e
->ts
.kind
);
1864 gfc_simplify_bound (gfc_expr
* array
, gfc_expr
* dim
, int upper
)
1870 if (array
->expr_type
!= EXPR_VARIABLE
)
1876 if (dim
->expr_type
!= EXPR_CONSTANT
)
1879 /* Follow any component references. */
1880 as
= array
->symtree
->n
.sym
->as
;
1882 while (ref
->next
!= NULL
)
1884 if (ref
->type
== REF_COMPONENT
)
1885 as
= ref
->u
.c
.sym
->as
;
1889 if (ref
->type
!= REF_ARRAY
|| ref
->u
.ar
.type
!= AR_FULL
)
1892 i
= mpz_get_si (dim
->value
.integer
);
1894 return as
->upper
[i
-1];
1896 return as
->lower
[i
-1];
1901 gfc_simplify_lbound (gfc_expr
* array
, gfc_expr
* dim
)
1903 return gfc_simplify_bound (array
, dim
, 0);
1908 gfc_simplify_len (gfc_expr
* e
)
1912 if (e
->expr_type
!= EXPR_CONSTANT
)
1915 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind (),
1918 mpz_set_si (result
->value
.integer
, e
->value
.character
.length
);
1919 return range_check (result
, "LEN");
1924 gfc_simplify_len_trim (gfc_expr
* e
)
1927 int count
, len
, lentrim
, i
;
1929 if (e
->expr_type
!= EXPR_CONSTANT
)
1932 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind (),
1935 len
= e
->value
.character
.length
;
1937 for (count
= 0, i
= 1; i
<= len
; i
++)
1938 if (e
->value
.character
.string
[len
- i
] == ' ')
1943 lentrim
= len
- count
;
1945 mpz_set_si (result
->value
.integer
, lentrim
);
1946 return range_check (result
, "LEN_TRIM");
1951 gfc_simplify_lge (gfc_expr
* a
, gfc_expr
* b
)
1954 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
1957 return gfc_logical_expr (gfc_compare_string (a
, b
, xascii_table
) >= 0,
1963 gfc_simplify_lgt (gfc_expr
* a
, gfc_expr
* b
)
1966 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
1969 return gfc_logical_expr (gfc_compare_string (a
, b
, xascii_table
) > 0,
1975 gfc_simplify_lle (gfc_expr
* a
, gfc_expr
* b
)
1978 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
1981 return gfc_logical_expr (gfc_compare_string (a
, b
, xascii_table
) <= 0,
1987 gfc_simplify_llt (gfc_expr
* a
, gfc_expr
* b
)
1990 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
1993 return gfc_logical_expr (gfc_compare_string (a
, b
, xascii_table
) < 0,
1999 gfc_simplify_log (gfc_expr
* x
)
2004 if (x
->expr_type
!= EXPR_CONSTANT
)
2007 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2012 if (mpf_cmp (x
->value
.real
, mpf_zero
) <= 0)
2015 ("Argument of LOG at %L cannot be less than or equal to zero",
2017 gfc_free_expr (result
);
2018 return &gfc_bad_expr
;
2021 natural_logarithm (&x
->value
.real
, &result
->value
.real
);
2025 if ((mpf_cmp (x
->value
.complex.r
, mpf_zero
) == 0)
2026 && (mpf_cmp (x
->value
.complex.i
, mpf_zero
) == 0))
2028 gfc_error ("Complex argument of LOG at %L cannot be zero",
2030 gfc_free_expr (result
);
2031 return &gfc_bad_expr
;
2037 mpf_div (xr
, x
->value
.complex.i
, x
->value
.complex.r
);
2038 arctangent2 (&x
->value
.complex.i
, &x
->value
.complex.r
,
2039 &result
->value
.complex.i
);
2041 mpf_mul (xr
, x
->value
.complex.r
, x
->value
.complex.r
);
2042 mpf_mul (xi
, x
->value
.complex.i
, x
->value
.complex.i
);
2043 mpf_add (xr
, xr
, xi
);
2045 natural_logarithm (&xr
, &result
->value
.complex.r
);
2053 gfc_internal_error ("gfc_simplify_log: bad type");
2056 return range_check (result
, "LOG");
2061 gfc_simplify_log10 (gfc_expr
* x
)
2065 if (x
->expr_type
!= EXPR_CONSTANT
)
2068 if (mpf_cmp (x
->value
.real
, mpf_zero
) <= 0)
2071 ("Argument of LOG10 at %L cannot be less than or equal to zero",
2073 return &gfc_bad_expr
;
2076 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2078 common_logarithm (&x
->value
.real
, &result
->value
.real
);
2080 return range_check (result
, "LOG10");
2085 gfc_simplify_logical (gfc_expr
* e
, gfc_expr
* k
)
2090 kind
= get_kind (BT_LOGICAL
, k
, "LOGICAL", gfc_default_logical_kind ());
2092 return &gfc_bad_expr
;
2094 if (e
->expr_type
!= EXPR_CONSTANT
)
2097 result
= gfc_constant_result (BT_LOGICAL
, kind
, &e
->where
);
2099 result
->value
.logical
= e
->value
.logical
;
2105 /* This function is special since MAX() can take any number of
2106 arguments. The simplified expression is a rewritten version of the
2107 argument list containing at most one constant element. Other
2108 constant elements are deleted. Because the argument list has
2109 already been checked, this function always succeeds. sign is 1 for
2110 MAX(), -1 for MIN(). */
2113 simplify_min_max (gfc_expr
* expr
, int sign
)
2115 gfc_actual_arglist
*arg
, *last
, *extremum
;
2116 gfc_intrinsic_sym
* specific
;
2120 specific
= expr
->value
.function
.isym
;
2122 arg
= expr
->value
.function
.actual
;
2124 for (; arg
; last
= arg
, arg
= arg
->next
)
2126 if (arg
->expr
->expr_type
!= EXPR_CONSTANT
)
2129 if (extremum
== NULL
)
2135 switch (arg
->expr
->ts
.type
)
2138 if (mpz_cmp (arg
->expr
->value
.integer
,
2139 extremum
->expr
->value
.integer
) * sign
> 0)
2140 mpz_set (extremum
->expr
->value
.integer
, arg
->expr
->value
.integer
);
2145 if (mpf_cmp (arg
->expr
->value
.real
, extremum
->expr
->value
.real
) *
2147 mpf_set (extremum
->expr
->value
.real
, arg
->expr
->value
.real
);
2152 gfc_internal_error ("gfc_simplify_max(): Bad type in arglist");
2155 /* Delete the extra constant argument. */
2157 expr
->value
.function
.actual
= arg
->next
;
2159 last
->next
= arg
->next
;
2162 gfc_free_actual_arglist (arg
);
2166 /* If there is one value left, replace the function call with the
2168 if (expr
->value
.function
.actual
->next
!= NULL
)
2171 /* Convert to the correct type and kind. */
2172 if (expr
->ts
.type
!= BT_UNKNOWN
)
2173 return gfc_convert_constant (expr
->value
.function
.actual
->expr
,
2174 expr
->ts
.type
, expr
->ts
.kind
);
2176 if (specific
->ts
.type
!= BT_UNKNOWN
)
2177 return gfc_convert_constant (expr
->value
.function
.actual
->expr
,
2178 specific
->ts
.type
, specific
->ts
.kind
);
2180 return gfc_copy_expr (expr
->value
.function
.actual
->expr
);
2185 gfc_simplify_min (gfc_expr
* e
)
2188 return simplify_min_max (e
, -1);
2193 gfc_simplify_max (gfc_expr
* e
)
2196 return simplify_min_max (e
, 1);
2201 gfc_simplify_maxexponent (gfc_expr
* x
)
2206 i
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
);
2208 gfc_internal_error ("gfc_simplify_maxexponent(): Bad kind");
2210 result
= gfc_int_expr (gfc_real_kinds
[i
].max_exponent
);
2211 result
->where
= x
->where
;
2218 gfc_simplify_minexponent (gfc_expr
* x
)
2223 i
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
);
2225 gfc_internal_error ("gfc_simplify_minexponent(): Bad kind");
2227 result
= gfc_int_expr (gfc_real_kinds
[i
].min_exponent
);
2228 result
->where
= x
->where
;
2235 gfc_simplify_mod (gfc_expr
* a
, gfc_expr
* p
)
2238 mpf_t quot
, iquot
, term
;
2240 if (a
->expr_type
!= EXPR_CONSTANT
|| p
->expr_type
!= EXPR_CONSTANT
)
2243 result
= gfc_constant_result (a
->ts
.type
, a
->ts
.kind
, &a
->where
);
2248 if (mpz_cmp_ui (p
->value
.integer
, 0) == 0)
2250 /* Result is processor-dependent. */
2251 gfc_error ("Second argument MOD at %L is zero", &a
->where
);
2252 gfc_free_expr (result
);
2253 return &gfc_bad_expr
;
2255 mpz_tdiv_r (result
->value
.integer
, a
->value
.integer
, p
->value
.integer
);
2259 if (mpf_cmp_ui (p
->value
.real
, 0) == 0)
2261 /* Result is processor-dependent. */
2262 gfc_error ("Second argument of MOD at %L is zero", &p
->where
);
2263 gfc_free_expr (result
);
2264 return &gfc_bad_expr
;
2271 mpf_div (quot
, a
->value
.real
, p
->value
.real
);
2272 mpf_trunc (iquot
, quot
);
2273 mpf_mul (term
, iquot
, p
->value
.real
);
2274 mpf_sub (result
->value
.real
, a
->value
.real
, term
);
2282 gfc_internal_error ("gfc_simplify_mod(): Bad arguments");
2285 return range_check (result
, "MOD");
2290 gfc_simplify_modulo (gfc_expr
* a
, gfc_expr
* p
)
2293 mpf_t quot
, iquot
, term
;
2295 if (a
->expr_type
!= EXPR_CONSTANT
|| p
->expr_type
!= EXPR_CONSTANT
)
2298 result
= gfc_constant_result (a
->ts
.type
, a
->ts
.kind
, &a
->where
);
2303 if (mpz_cmp_ui (p
->value
.integer
, 0) == 0)
2305 /* Result is processor-dependent. This processor just opts
2306 to not handle it at all. */
2307 gfc_error ("Second argument of MODULO at %L is zero", &a
->where
);
2308 gfc_free_expr (result
);
2309 return &gfc_bad_expr
;
2311 mpz_fdiv_r (result
->value
.integer
, a
->value
.integer
, p
->value
.integer
);
2316 if (mpf_cmp_ui (p
->value
.real
, 0) == 0)
2318 /* Result is processor-dependent. */
2319 gfc_error ("Second argument of MODULO at %L is zero", &p
->where
);
2320 gfc_free_expr (result
);
2321 return &gfc_bad_expr
;
2328 mpf_div (quot
, a
->value
.real
, p
->value
.real
);
2329 mpf_floor (iquot
, quot
);
2330 mpf_mul (term
, iquot
, p
->value
.real
);
2336 mpf_sub (result
->value
.real
, a
->value
.real
, term
);
2340 gfc_internal_error ("gfc_simplify_modulo(): Bad arguments");
2343 return range_check (result
, "MODULO");
2347 /* Exists for the sole purpose of consistency with other intrinsics. */
2349 gfc_simplify_mvbits (gfc_expr
* f ATTRIBUTE_UNUSED
,
2350 gfc_expr
* fp ATTRIBUTE_UNUSED
,
2351 gfc_expr
* l ATTRIBUTE_UNUSED
,
2352 gfc_expr
* to ATTRIBUTE_UNUSED
,
2353 gfc_expr
* tp ATTRIBUTE_UNUSED
)
2360 gfc_simplify_nearest (gfc_expr
* x
, gfc_expr
* s
)
2365 int p
, i
, k
, match_float
;
2367 /* FIXME: This implementation is dopey and probably not quite right,
2368 but it's a start. */
2370 if (x
->expr_type
!= EXPR_CONSTANT
)
2373 k
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
);
2375 gfc_internal_error ("gfc_simplify_precision(): Bad kind");
2377 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2379 val
= mpf_get_d (x
->value
.real
);
2380 p
= gfc_real_kinds
[k
].digits
;
2383 for (i
= 1; i
< p
; ++i
)
2388 /* TODO we should make sure that 'float' matches kind 4 */
2389 match_float
= gfc_real_kinds
[k
].kind
== 4;
2390 if (mpf_cmp_ui (s
->value
.real
, 0) > 0)
2396 mpf_set_d (result
->value
.real
, rval
);
2401 mpf_set_d (result
->value
.real
, val
);
2404 else if (mpf_cmp_ui (s
->value
.real
, 0) < 0)
2410 mpf_set_d (result
->value
.real
, rval
);
2415 mpf_set_d (result
->value
.real
, val
);
2420 gfc_error ("Invalid second argument of NEAREST at %L", &s
->where
);
2422 return &gfc_bad_expr
;
2425 return range_check (result
, "NEAREST");
2431 simplify_nint (const char *name
, gfc_expr
* e
, gfc_expr
* k
)
2433 gfc_expr
*rtrunc
, *itrunc
, *result
;
2436 kind
= get_kind (BT_INTEGER
, k
, name
, gfc_default_integer_kind ());
2438 return &gfc_bad_expr
;
2440 if (e
->expr_type
!= EXPR_CONSTANT
)
2443 result
= gfc_constant_result (BT_INTEGER
, kind
, &e
->where
);
2445 rtrunc
= gfc_copy_expr (e
);
2446 itrunc
= gfc_copy_expr (e
);
2448 cmp
= mpf_cmp_ui (e
->value
.real
, 0);
2452 mpf_add (rtrunc
->value
.real
, e
->value
.real
, mpf_half
);
2453 mpf_trunc (itrunc
->value
.real
, rtrunc
->value
.real
);
2457 mpf_sub (rtrunc
->value
.real
, e
->value
.real
, mpf_half
);
2458 mpf_trunc (itrunc
->value
.real
, rtrunc
->value
.real
);
2461 mpf_set_ui (itrunc
->value
.real
, 0);
2463 mpz_set_f (result
->value
.integer
, itrunc
->value
.real
);
2465 gfc_free_expr (itrunc
);
2466 gfc_free_expr (rtrunc
);
2468 return range_check (result
, name
);
2473 gfc_simplify_nint (gfc_expr
* e
, gfc_expr
* k
)
2476 return simplify_nint ("NINT", e
, k
);
2481 gfc_simplify_idnint (gfc_expr
* e
)
2484 return simplify_nint ("IDNINT", e
, NULL
);
2489 gfc_simplify_not (gfc_expr
* e
)
2494 if (e
->expr_type
!= EXPR_CONSTANT
)
2497 result
= gfc_constant_result (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
2499 mpz_com (result
->value
.integer
, e
->value
.integer
);
2501 /* Because of how GMP handles numbers, the result must be ANDed with
2502 the max_int mask. For radices <> 2, this will require change. */
2504 i
= gfc_validate_kind (BT_INTEGER
, e
->ts
.kind
);
2506 gfc_internal_error ("gfc_simplify_not(): Bad kind");
2508 mpz_and (result
->value
.integer
, result
->value
.integer
,
2509 gfc_integer_kinds
[i
].max_int
);
2511 return range_check (result
, "NOT");
2516 gfc_simplify_null (gfc_expr
* mold
)
2520 result
= gfc_get_expr ();
2521 result
->expr_type
= EXPR_NULL
;
2524 result
->ts
.type
= BT_UNKNOWN
;
2527 result
->ts
= mold
->ts
;
2528 result
->where
= mold
->where
;
2536 gfc_simplify_precision (gfc_expr
* e
)
2541 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
);
2543 gfc_internal_error ("gfc_simplify_precision(): Bad kind");
2545 result
= gfc_int_expr (gfc_real_kinds
[i
].precision
);
2546 result
->where
= e
->where
;
2553 gfc_simplify_radix (gfc_expr
* e
)
2558 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
);
2565 i
= gfc_integer_kinds
[i
].radix
;
2569 i
= gfc_real_kinds
[i
].radix
;
2574 gfc_internal_error ("gfc_simplify_radix(): Bad type");
2577 result
= gfc_int_expr (i
);
2578 result
->where
= e
->where
;
2585 gfc_simplify_range (gfc_expr
* e
)
2591 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
);
2598 j
= gfc_integer_kinds
[i
].range
;
2603 j
= gfc_real_kinds
[i
].range
;
2608 gfc_internal_error ("gfc_simplify_range(): Bad kind");
2611 result
= gfc_int_expr (j
);
2612 result
->where
= e
->where
;
2619 gfc_simplify_real (gfc_expr
* e
, gfc_expr
* k
)
2624 if (e
->ts
.type
== BT_COMPLEX
)
2625 kind
= get_kind (BT_REAL
, k
, "REAL", e
->ts
.kind
);
2627 kind
= get_kind (BT_REAL
, k
, "REAL", gfc_default_real_kind ());
2630 return &gfc_bad_expr
;
2632 if (e
->expr_type
!= EXPR_CONSTANT
)
2638 result
= gfc_int2real (e
, kind
);
2642 result
= gfc_real2real (e
, kind
);
2646 result
= gfc_complex2real (e
, kind
);
2650 gfc_internal_error ("bad type in REAL");
2654 return range_check (result
, "REAL");
2658 gfc_simplify_repeat (gfc_expr
* e
, gfc_expr
* n
)
2661 int i
, j
, len
, ncopies
, nlen
;
2663 if (e
->expr_type
!= EXPR_CONSTANT
|| n
->expr_type
!= EXPR_CONSTANT
)
2666 if (n
!= NULL
&& (gfc_extract_int (n
, &ncopies
) != NULL
|| ncopies
< 0))
2668 gfc_error ("Invalid second argument of REPEAT at %L", &n
->where
);
2669 return &gfc_bad_expr
;
2672 len
= e
->value
.character
.length
;
2673 nlen
= ncopies
* len
;
2675 result
= gfc_constant_result (BT_CHARACTER
, e
->ts
.kind
, &e
->where
);
2679 result
->value
.character
.string
= gfc_getmem (1);
2680 result
->value
.character
.length
= 0;
2681 result
->value
.character
.string
[0] = '\0';
2685 result
->value
.character
.length
= nlen
;
2686 result
->value
.character
.string
= gfc_getmem (nlen
+ 1);
2688 for (i
= 0; i
< ncopies
; i
++)
2689 for (j
= 0; j
< len
; j
++)
2690 result
->value
.character
.string
[j
+ i
* len
] =
2691 e
->value
.character
.string
[j
];
2693 result
->value
.character
.string
[nlen
] = '\0'; /* For debugger */
2698 /* This one is a bear, but mainly has to do with shuffling elements. */
2701 gfc_simplify_reshape (gfc_expr
* source
, gfc_expr
* shape_exp
,
2702 gfc_expr
* pad
, gfc_expr
* order_exp
)
2705 int order
[GFC_MAX_DIMENSIONS
], shape
[GFC_MAX_DIMENSIONS
];
2706 int i
, rank
, npad
, x
[GFC_MAX_DIMENSIONS
];
2707 gfc_constructor
*head
, *tail
;
2713 /* Unpack the shape array. */
2714 if (source
->expr_type
!= EXPR_ARRAY
|| !gfc_is_constant_expr (source
))
2717 if (shape_exp
->expr_type
!= EXPR_ARRAY
|| !gfc_is_constant_expr (shape_exp
))
2721 && (pad
->expr_type
!= EXPR_ARRAY
2722 || !gfc_is_constant_expr (pad
)))
2725 if (order_exp
!= NULL
2726 && (order_exp
->expr_type
!= EXPR_ARRAY
2727 || !gfc_is_constant_expr (order_exp
)))
2736 e
= gfc_get_array_element (shape_exp
, rank
);
2740 if (gfc_extract_int (e
, &shape
[rank
]) != NULL
)
2742 gfc_error ("Integer too large in shape specification at %L",
2750 if (rank
>= GFC_MAX_DIMENSIONS
)
2752 gfc_error ("Too many dimensions in shape specification for RESHAPE "
2753 "at %L", &e
->where
);
2758 if (shape
[rank
] < 0)
2760 gfc_error ("Shape specification at %L cannot be negative",
2770 gfc_error ("Shape specification at %L cannot be the null array",
2775 /* Now unpack the order array if present. */
2776 if (order_exp
== NULL
)
2778 for (i
= 0; i
< rank
; i
++)
2785 for (i
= 0; i
< rank
; i
++)
2788 for (i
= 0; i
< rank
; i
++)
2790 e
= gfc_get_array_element (order_exp
, i
);
2794 ("ORDER parameter of RESHAPE at %L is not the same size "
2795 "as SHAPE parameter", &order_exp
->where
);
2799 if (gfc_extract_int (e
, &order
[i
]) != NULL
)
2801 gfc_error ("Error in ORDER parameter of RESHAPE at %L",
2809 if (order
[i
] < 1 || order
[i
] > rank
)
2811 gfc_error ("ORDER parameter of RESHAPE at %L is out of range",
2820 gfc_error ("Invalid permutation in ORDER parameter at %L",
2829 /* Count the elements in the source and padding arrays. */
2834 gfc_array_size (pad
, &size
);
2835 npad
= mpz_get_ui (size
);
2839 gfc_array_size (source
, &size
);
2840 nsource
= mpz_get_ui (size
);
2843 /* If it weren't for that pesky permutation we could just loop
2844 through the source and round out any shortage with pad elements.
2845 But no, someone just had to have the compiler do something the
2846 user should be doing. */
2848 for (i
= 0; i
< rank
; i
++)
2853 /* Figure out which element to extract. */
2854 mpz_set_ui (index
, 0);
2856 for (i
= rank
- 1; i
>= 0; i
--)
2858 mpz_add_ui (index
, index
, x
[order
[i
]]);
2860 mpz_mul_ui (index
, index
, shape
[order
[i
- 1]]);
2863 if (mpz_cmp_ui (index
, INT_MAX
) > 0)
2864 gfc_internal_error ("Reshaped array too large at %L", &e
->where
);
2866 j
= mpz_get_ui (index
);
2869 e
= gfc_get_array_element (source
, j
);
2877 ("PAD parameter required for short SOURCE parameter at %L",
2883 e
= gfc_get_array_element (pad
, j
);
2887 head
= tail
= gfc_get_constructor ();
2890 tail
->next
= gfc_get_constructor ();
2897 tail
->where
= e
->where
;
2900 /* Calculate the next element. */
2904 if (++x
[i
] < shape
[i
])
2915 e
= gfc_get_expr ();
2916 e
->where
= source
->where
;
2917 e
->expr_type
= EXPR_ARRAY
;
2918 e
->value
.constructor
= head
;
2919 e
->shape
= gfc_get_shape (rank
);
2921 for (i
= 0; i
< rank
; i
++)
2922 mpz_init_set_ui (e
->shape
[i
], shape
[order
[i
]]);
2924 e
->ts
= head
->expr
->ts
;
2930 gfc_free_constructor (head
);
2932 return &gfc_bad_expr
;
2937 gfc_simplify_rrspacing (gfc_expr
* x
)
2940 mpf_t i2
, absv
, ln2
, lnx
, frac
, pow2
;
2944 if (x
->expr_type
!= EXPR_CONSTANT
)
2947 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
);
2949 gfc_internal_error ("gfc_simplify_rrspacing(): Bad kind");
2951 result
= gfc_constant_result (BT_REAL
, x
->ts
.kind
, &x
->where
);
2953 p
= gfc_real_kinds
[i
].digits
;
2955 if (mpf_cmp (x
->value
.real
, mpf_zero
) == 0)
2957 mpf_ui_div (result
->value
.real
, 1, gfc_real_kinds
[i
].tiny
);
2961 mpf_init_set_ui (i2
, 2);
2968 natural_logarithm (&i2
, &ln2
);
2970 mpf_abs (absv
, x
->value
.real
);
2971 natural_logarithm (&absv
, &lnx
);
2973 mpf_div (lnx
, lnx
, ln2
);
2974 mpf_trunc (lnx
, lnx
);
2975 mpf_add_ui (lnx
, lnx
, 1);
2977 exp2
= (unsigned long) mpf_get_d (lnx
);
2978 mpf_pow_ui (pow2
, i2
, exp2
);
2979 mpf_div (frac
, absv
, pow2
);
2981 exp2
= (unsigned long) p
;
2982 mpf_mul_2exp (result
->value
.real
, frac
, exp2
);
2991 return range_check (result
, "RRSPACING");
2996 gfc_simplify_scale (gfc_expr
* x
, gfc_expr
* i
)
2998 int k
, neg_flag
, power
, exp_range
;
3002 if (x
->expr_type
!= EXPR_CONSTANT
|| i
->expr_type
!= EXPR_CONSTANT
)
3005 result
= gfc_constant_result (BT_REAL
, x
->ts
.kind
, &x
->where
);
3007 if (mpf_sgn (x
->value
.real
) == 0)
3009 mpf_set_ui (result
->value
.real
, 0);
3013 k
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
);
3015 gfc_internal_error ("gfc_simplify_scale(): Bad kind");
3017 exp_range
= gfc_real_kinds
[k
].max_exponent
- gfc_real_kinds
[k
].min_exponent
;
3019 /* This check filters out values of i that would overflow an int. */
3020 if (mpz_cmp_si (i
->value
.integer
, exp_range
+ 2) > 0
3021 || mpz_cmp_si (i
->value
.integer
, -exp_range
- 2) < 0)
3023 gfc_error ("Result of SCALE overflows its kind at %L", &result
->where
);
3024 return &gfc_bad_expr
;
3027 /* Compute scale = radix ** power. */
3028 power
= mpz_get_si (i
->value
.integer
);
3038 mpf_init_set_ui (radix
, gfc_real_kinds
[k
].radix
);
3040 mpf_pow_ui (scale
, radix
, power
);
3043 mpf_div (result
->value
.real
, x
->value
.real
, scale
);
3045 mpf_mul (result
->value
.real
, x
->value
.real
, scale
);
3050 return range_check (result
, "SCALE");
3055 gfc_simplify_scan (gfc_expr
* e
, gfc_expr
* c
, gfc_expr
* b
)
3060 size_t indx
, len
, lenc
;
3062 if (e
->expr_type
!= EXPR_CONSTANT
|| c
->expr_type
!= EXPR_CONSTANT
)
3065 if (b
!= NULL
&& b
->value
.logical
!= 0)
3070 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind (),
3073 len
= e
->value
.character
.length
;
3074 lenc
= c
->value
.character
.length
;
3076 if (len
== 0 || lenc
== 0)
3085 strcspn (e
->value
.character
.string
, c
->value
.character
.string
) + 1;
3092 for (indx
= len
; indx
> 0; indx
--)
3094 for (i
= 0; i
< lenc
; i
++)
3096 if (c
->value
.character
.string
[i
]
3097 == e
->value
.character
.string
[indx
- 1])
3105 mpz_set_ui (result
->value
.integer
, indx
);
3106 return range_check (result
, "SCAN");
3111 gfc_simplify_selected_int_kind (gfc_expr
* e
)
3116 if (e
->expr_type
!= EXPR_CONSTANT
|| gfc_extract_int (e
, &range
) != NULL
)
3121 for (i
= 0; gfc_integer_kinds
[i
].kind
!= 0; i
++)
3122 if (gfc_integer_kinds
[i
].range
>= range
3123 && gfc_integer_kinds
[i
].kind
< kind
)
3124 kind
= gfc_integer_kinds
[i
].kind
;
3126 if (kind
== INT_MAX
)
3129 result
= gfc_int_expr (kind
);
3130 result
->where
= e
->where
;
3137 gfc_simplify_selected_real_kind (gfc_expr
* p
, gfc_expr
* q
)
3139 int range
, precision
, i
, kind
, found_precision
, found_range
;
3146 if (p
->expr_type
!= EXPR_CONSTANT
3147 || gfc_extract_int (p
, &precision
) != NULL
)
3155 if (q
->expr_type
!= EXPR_CONSTANT
3156 || gfc_extract_int (q
, &range
) != NULL
)
3161 found_precision
= 0;
3164 for (i
= 0; gfc_real_kinds
[i
].kind
!= 0; i
++)
3166 if (gfc_real_kinds
[i
].precision
>= precision
)
3167 found_precision
= 1;
3169 if (gfc_real_kinds
[i
].range
>= range
)
3172 if (gfc_real_kinds
[i
].precision
>= precision
3173 && gfc_real_kinds
[i
].range
>= range
&& gfc_real_kinds
[i
].kind
< kind
)
3174 kind
= gfc_real_kinds
[i
].kind
;
3177 if (kind
== INT_MAX
)
3181 if (!found_precision
)
3187 result
= gfc_int_expr (kind
);
3188 result
->where
= (p
!= NULL
) ? p
->where
: q
->where
;
3195 gfc_simplify_set_exponent (gfc_expr
* x
, gfc_expr
* i
)
3198 mpf_t i2
, ln2
, absv
, lnx
, pow2
, frac
;
3201 if (x
->expr_type
!= EXPR_CONSTANT
|| i
->expr_type
!= EXPR_CONSTANT
)
3204 result
= gfc_constant_result (BT_REAL
, x
->ts
.kind
, &x
->where
);
3206 if (mpf_cmp (x
->value
.real
, mpf_zero
) == 0)
3208 mpf_set (result
->value
.real
, mpf_zero
);
3212 mpf_init_set_ui (i2
, 2);
3219 natural_logarithm (&i2
, &ln2
);
3221 mpf_abs (absv
, x
->value
.real
);
3222 natural_logarithm (&absv
, &lnx
);
3224 mpf_div (lnx
, lnx
, ln2
);
3225 mpf_trunc (lnx
, lnx
);
3226 mpf_add_ui (lnx
, lnx
, 1);
3228 /* Old exponent value, and fraction. */
3229 exp2
= (unsigned long) mpf_get_d (lnx
);
3230 mpf_pow_ui (pow2
, i2
, exp2
);
3232 mpf_div (frac
, absv
, pow2
);
3235 exp2
= (unsigned long) mpz_get_d (i
->value
.integer
);
3236 mpf_mul_2exp (result
->value
.real
, frac
, exp2
);
3245 return range_check (result
, "SET_EXPONENT");
3250 gfc_simplify_shape (gfc_expr
* source
)
3252 mpz_t shape
[GFC_MAX_DIMENSIONS
];
3253 gfc_expr
*result
, *e
, *f
;
3258 result
= gfc_start_constructor (BT_INTEGER
, gfc_default_integer_kind (),
3261 if (source
->rank
== 0 || source
->expr_type
!= EXPR_VARIABLE
)
3264 ar
= gfc_find_array_ref (source
);
3266 t
= gfc_array_ref_shape (ar
, shape
);
3268 for (n
= 0; n
< source
->rank
; n
++)
3270 e
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind (),
3275 mpz_set (e
->value
.integer
, shape
[n
]);
3276 mpz_clear (shape
[n
]);
3280 mpz_set_ui (e
->value
.integer
, n
+ 1);
3282 f
= gfc_simplify_size (source
, e
);
3286 gfc_free_expr (result
);
3295 gfc_append_constructor (result
, e
);
3303 gfc_simplify_size (gfc_expr
* array
, gfc_expr
* dim
)
3311 if (gfc_array_size (array
, &size
) == FAILURE
)
3316 if (dim
->expr_type
!= EXPR_CONSTANT
)
3319 d
= mpz_get_ui (dim
->value
.integer
) - 1;
3320 if (gfc_array_dimen_size (array
, d
, &size
) == FAILURE
)
3324 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind (),
3327 mpz_set (result
->value
.integer
, size
);
3334 gfc_simplify_sign (gfc_expr
* x
, gfc_expr
* y
)
3338 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
3341 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3346 mpz_abs (result
->value
.integer
, x
->value
.integer
);
3347 if (mpz_sgn (y
->value
.integer
) < 0)
3348 mpz_neg (result
->value
.integer
, result
->value
.integer
);
3353 /* TODO: Handle -0.0 and +0.0 correctly on machines that support
3355 mpf_abs (result
->value
.real
, x
->value
.real
);
3356 if (mpf_sgn (y
->value
.integer
) < 0)
3357 mpf_neg (result
->value
.real
, result
->value
.real
);
3362 gfc_internal_error ("Bad type in gfc_simplify_sign");
3370 gfc_simplify_sin (gfc_expr
* x
)
3375 if (x
->expr_type
!= EXPR_CONSTANT
)
3378 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3383 sine (&x
->value
.real
, &result
->value
.real
);
3390 sine (&x
->value
.complex.r
, &xp
);
3391 hypercos (&x
->value
.complex.i
, &xq
);
3392 mpf_mul (result
->value
.complex.r
, xp
, xq
);
3394 cosine (&x
->value
.complex.r
, &xp
);
3395 hypersine (&x
->value
.complex.i
, &xq
);
3396 mpf_mul (result
->value
.complex.i
, xp
, xq
);
3403 gfc_internal_error ("in gfc_simplify_sin(): Bad type");
3406 return range_check (result
, "SIN");
3411 gfc_simplify_sinh (gfc_expr
* x
)
3415 if (x
->expr_type
!= EXPR_CONSTANT
)
3418 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3420 hypersine (&x
->value
.real
, &result
->value
.real
);
3422 return range_check (result
, "SINH");
3426 /* The argument is always a double precision real that is converted to
3427 single precision. TODO: Rounding! */
3430 gfc_simplify_sngl (gfc_expr
* a
)
3434 if (a
->expr_type
!= EXPR_CONSTANT
)
3437 result
= gfc_real2real (a
, gfc_default_real_kind ());
3438 return range_check (result
, "SNGL");
3443 gfc_simplify_spacing (gfc_expr
* x
)
3446 mpf_t i1
, i2
, ln2
, absv
, lnx
;
3451 if (x
->expr_type
!= EXPR_CONSTANT
)
3454 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
);
3456 gfc_internal_error ("gfc_simplify_spacing(): Bad kind");
3458 p
= gfc_real_kinds
[i
].digits
;
3460 result
= gfc_constant_result (BT_REAL
, x
->ts
.kind
, &x
->where
);
3462 if (mpf_cmp (x
->value
.real
, mpf_zero
) == 0)
3464 mpf_set (result
->value
.real
, gfc_real_kinds
[i
].tiny
);
3468 mpf_init_set_ui (i1
, 1);
3469 mpf_init_set_ui (i2
, 2);
3474 natural_logarithm (&i2
, &ln2
);
3476 mpf_abs (absv
, x
->value
.real
);
3477 natural_logarithm (&absv
, &lnx
);
3479 mpf_div (lnx
, lnx
, ln2
);
3480 mpf_trunc (lnx
, lnx
);
3481 mpf_add_ui (lnx
, lnx
, 1);
3483 diff
= (long) mpf_get_d (lnx
) - (long) p
;
3486 exp2
= (unsigned) diff
;
3487 mpf_mul_2exp (result
->value
.real
, i1
, exp2
);
3492 exp2
= (unsigned) diff
;
3493 mpf_div_2exp (result
->value
.real
, i1
, exp2
);
3502 if (mpf_cmp (result
->value
.real
, gfc_real_kinds
[i
].tiny
) < 0)
3503 mpf_set (result
->value
.real
, gfc_real_kinds
[i
].tiny
);
3505 return range_check (result
, "SPACING");
3510 gfc_simplify_sqrt (gfc_expr
* e
)
3513 mpf_t ac
, ad
, s
, t
, w
;
3515 if (e
->expr_type
!= EXPR_CONSTANT
)
3518 result
= gfc_constant_result (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
3523 if (mpf_cmp_si (e
->value
.real
, 0) < 0)
3525 mpf_sqrt (result
->value
.real
, e
->value
.real
);
3530 /* Formula taken from Numerical Recipes to avoid over- and
3539 if (mpf_cmp_ui (e
->value
.complex.r
, 0) == 0
3540 && mpf_cmp_ui (e
->value
.complex.i
, 0) == 0)
3543 mpf_set_ui (result
->value
.complex.r
, 0);
3544 mpf_set_ui (result
->value
.complex.i
, 0);
3548 mpf_abs (ac
, e
->value
.complex.r
);
3549 mpf_abs (ad
, e
->value
.complex.i
);
3551 if (mpf_cmp (ac
, ad
) >= 0)
3553 mpf_div (t
, e
->value
.complex.i
, e
->value
.complex.r
);
3555 mpf_add_ui (t
, t
, 1);
3557 mpf_add_ui (t
, t
, 1);
3558 mpf_div_ui (t
, t
, 2);
3565 mpf_div (s
, e
->value
.complex.r
, e
->value
.complex.i
);
3567 mpf_add_ui (t
, t
, 1);
3571 mpf_div_ui (t
, t
, 2);
3577 if (mpf_cmp_ui (w
, 0) != 0 && mpf_cmp_ui (e
->value
.complex.r
, 0) >= 0)
3579 mpf_mul_ui (t
, w
, 2);
3580 mpf_div (result
->value
.complex.i
, e
->value
.complex.i
, t
);
3581 mpf_set (result
->value
.complex.r
, w
);
3583 else if (mpf_cmp_ui (w
, 0) != 0
3584 && mpf_cmp_ui (e
->value
.complex.r
, 0) < 0
3585 && mpf_cmp_ui (e
->value
.complex.i
, 0) >= 0)
3587 mpf_mul_ui (t
, w
, 2);
3588 mpf_div (result
->value
.complex.r
, e
->value
.complex.i
, t
);
3589 mpf_set (result
->value
.complex.i
, w
);
3591 else if (mpf_cmp_ui (w
, 0) != 0
3592 && mpf_cmp_ui (e
->value
.complex.r
, 0) < 0
3593 && mpf_cmp_ui (e
->value
.complex.i
, 0) < 0)
3595 mpf_mul_ui (t
, w
, 2);
3596 mpf_div (result
->value
.complex.r
, ad
, t
);
3598 mpf_set (result
->value
.complex.i
, w
);
3601 gfc_internal_error ("invalid complex argument of SQRT at %L",
3613 gfc_internal_error ("invalid argument of SQRT at %L", &e
->where
);
3616 return range_check (result
, "SQRT");
3619 gfc_free_expr (result
);
3620 gfc_error ("Argument of SQRT at %L has a negative value", &e
->where
);
3621 return &gfc_bad_expr
;
3626 gfc_simplify_tan (gfc_expr
* x
)
3629 mpf_t mpf_sin
, mpf_cos
, mag_cos
;
3632 if (x
->expr_type
!= EXPR_CONSTANT
)
3635 i
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
);
3637 gfc_internal_error ("gfc_simplify_tan(): Bad kind");
3639 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3644 sine (&x
->value
.real
, &mpf_sin
);
3645 cosine (&x
->value
.real
, &mpf_cos
);
3646 mpf_abs (mag_cos
, mpf_cos
);
3647 if (mpf_cmp_ui (mag_cos
, 0) == 0)
3649 gfc_error ("Tangent undefined at %L", &x
->where
);
3650 mpf_clear (mpf_sin
);
3651 mpf_clear (mpf_cos
);
3652 mpf_clear (mag_cos
);
3653 gfc_free_expr (result
);
3654 return &gfc_bad_expr
;
3656 else if (mpf_cmp (mag_cos
, gfc_real_kinds
[i
].tiny
) < 0)
3658 gfc_error ("Tangent cannot be accurately evaluated at %L", &x
->where
);
3659 mpf_clear (mpf_sin
);
3660 mpf_clear (mpf_cos
);
3661 mpf_clear (mag_cos
);
3662 gfc_free_expr (result
);
3663 return &gfc_bad_expr
;
3667 mpf_div (result
->value
.real
, mpf_sin
, mpf_cos
);
3668 mpf_clear (mpf_sin
);
3669 mpf_clear (mpf_cos
);
3670 mpf_clear (mag_cos
);
3673 return range_check (result
, "TAN");
3678 gfc_simplify_tanh (gfc_expr
* x
)
3683 if (x
->expr_type
!= EXPR_CONSTANT
)
3686 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3691 hypersine (&x
->value
.real
, &xq
);
3692 hypercos (&x
->value
.real
, &xp
);
3694 mpf_div (result
->value
.real
, xq
, xp
);
3699 return range_check (result
, "TANH");
3705 gfc_simplify_tiny (gfc_expr
* e
)
3710 i
= gfc_validate_kind (BT_REAL
, e
->ts
.kind
);
3712 gfc_internal_error ("gfc_simplify_error(): Bad kind");
3714 result
= gfc_constant_result (BT_REAL
, e
->ts
.kind
, &e
->where
);
3715 mpf_set (result
->value
.real
, gfc_real_kinds
[i
].tiny
);
3722 gfc_simplify_trim (gfc_expr
* e
)
3725 int count
, i
, len
, lentrim
;
3727 if (e
->expr_type
!= EXPR_CONSTANT
)
3730 len
= e
->value
.character
.length
;
3732 result
= gfc_constant_result (BT_CHARACTER
, e
->ts
.kind
, &e
->where
);
3734 for (count
= 0, i
= 1; i
<= len
; ++i
)
3736 if (e
->value
.character
.string
[len
- i
] == ' ')
3742 lentrim
= len
- count
;
3744 result
->value
.character
.length
= lentrim
;
3745 result
->value
.character
.string
= gfc_getmem (lentrim
+ 1);
3747 for (i
= 0; i
< lentrim
; i
++)
3748 result
->value
.character
.string
[i
] = e
->value
.character
.string
[i
];
3750 result
->value
.character
.string
[lentrim
] = '\0'; /* For debugger */
3757 gfc_simplify_ubound (gfc_expr
* array
, gfc_expr
* dim
)
3759 return gfc_simplify_bound (array
, dim
, 1);
3764 gfc_simplify_verify (gfc_expr
* s
, gfc_expr
* set
, gfc_expr
* b
)
3768 size_t index
, len
, lenset
;
3771 if (s
->expr_type
!= EXPR_CONSTANT
|| set
->expr_type
!= EXPR_CONSTANT
)
3774 if (b
!= NULL
&& b
->value
.logical
!= 0)
3779 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind (),
3782 len
= s
->value
.character
.length
;
3783 lenset
= set
->value
.character
.length
;
3787 mpz_set_ui (result
->value
.integer
, 0);
3795 mpz_set_ui (result
->value
.integer
, len
);
3800 strspn (s
->value
.character
.string
, set
->value
.character
.string
) + 1;
3809 mpz_set_ui (result
->value
.integer
, 1);
3812 for (index
= len
; index
> 0; index
--)
3814 for (i
= 0; i
< lenset
; i
++)
3816 if (s
->value
.character
.string
[index
- 1]
3817 == set
->value
.character
.string
[i
])
3825 mpz_set_ui (result
->value
.integer
, index
);
3829 /****************** Constant simplification *****************/
3831 /* Master function to convert one constant to another. While this is
3832 used as a simplification function, it requires the destination type
3833 and kind information which is supplied by a special case in
3837 gfc_convert_constant (gfc_expr
* e
, bt type
, int kind
)
3839 gfc_expr
*g
, *result
, *(*f
) (gfc_expr
*, int);
3840 gfc_constructor
*head
, *c
, *tail
= NULL
;
3854 f
= gfc_int2complex
;
3871 f
= gfc_real2complex
;
3882 f
= gfc_complex2int
;
3885 f
= gfc_complex2real
;
3888 f
= gfc_complex2complex
;
3897 if (type
!= BT_LOGICAL
)
3904 gfc_internal_error ("gfc_convert_constant(): Unexpected type");
3909 switch (e
->expr_type
)
3912 result
= f (e
, kind
);
3914 return &gfc_bad_expr
;
3918 if (!gfc_is_constant_expr (e
))
3923 for (c
= e
->value
.constructor
; c
; c
= c
->next
)
3926 head
= tail
= gfc_get_constructor ();
3929 tail
->next
= gfc_get_constructor ();
3933 tail
->where
= c
->where
;
3935 if (c
->iterator
== NULL
)
3936 tail
->expr
= f (c
->expr
, kind
);
3939 g
= gfc_convert_constant (c
->expr
, type
, kind
);
3940 if (g
== &gfc_bad_expr
)
3945 if (tail
->expr
== NULL
)
3947 gfc_free_constructor (head
);
3952 result
= gfc_get_expr ();
3953 result
->ts
.type
= type
;
3954 result
->ts
.kind
= kind
;
3955 result
->expr_type
= EXPR_ARRAY
;
3956 result
->value
.constructor
= head
;
3957 result
->shape
= gfc_copy_shape (e
->shape
, e
->rank
);
3958 result
->where
= e
->where
;
3959 result
->rank
= e
->rank
;
3970 /****************** Helper functions ***********************/
3972 /* Given a collating table, create the inverse table. */
3975 invert_table (const int *table
, int *xtable
)
3979 for (i
= 0; i
< 256; i
++)
3982 for (i
= 0; i
< 256; i
++)
3983 xtable
[table
[i
]] = i
;
3988 gfc_simplify_init_1 (void)
3991 mpf_init_set_str (mpf_zero
, "0.0", 10);
3992 mpf_init_set_str (mpf_half
, "0.5", 10);
3993 mpf_init_set_str (mpf_one
, "1.0", 10);
3994 mpz_init_set_str (mpz_zero
, "0", 10);
3996 invert_table (ascii_table
, xascii_table
);
4001 gfc_simplify_done_1 (void)
4004 mpf_clear (mpf_zero
);
4005 mpf_clear (mpf_half
);
4006 mpf_clear (mpf_one
);
4007 mpz_clear (mpz_zero
);