1 /* Simplify intrinsic functions at compile-time.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation,
4 Contributed by Andy Vaught & Katherine Holcomb
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
28 #include "intrinsic.h"
30 gfc_expr gfc_bad_expr
;
33 /* Note that 'simplification' is not just transforming expressions.
34 For functions that are not simplified at compile time, range
35 checking is done if possible.
37 The return convention is that each simplification function returns:
39 A new expression node corresponding to the simplified arguments.
40 The original arguments are destroyed by the caller, and must not
41 be a part of the new expression.
43 NULL pointer indicating that no simplification was possible and
44 the original expression should remain intact. If the
45 simplification function sets the type and/or the function name
46 via the pointer gfc_simple_expression, then this type is
49 An expression pointer to gfc_bad_expr (a static placeholder)
50 indicating that some error has prevented simplification. For
51 example, sqrt(-1.0). The error is generated within the function
52 and should be propagated upwards
54 By the time a simplification function gets control, it has been
55 decided that the function call is really supposed to be the
56 intrinsic. No type checking is strictly necessary, since only
57 valid types will be passed on. On the other hand, a simplification
58 subroutine may have to look at the type of an argument as part of
61 Array arguments are never passed to these subroutines.
63 The functions in this file don't have much comment with them, but
64 everything is reasonably straight-forward. The Standard, chapter 13
65 is the best comment you'll find for this file anyway. */
67 /* Static table for converting non-ascii character sets to ascii.
68 The xascii_table[] is the inverse table. */
70 static int ascii_table
[256] = {
71 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
72 '\b', '\t', '\n', '\v', '\0', '\r', '\0', '\0',
73 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
74 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
75 ' ', '!', '\'', '#', '$', '%', '&', '\'',
76 '(', ')', '*', '+', ',', '-', '.', '/',
77 '0', '1', '2', '3', '4', '5', '6', '7',
78 '8', '9', ':', ';', '<', '=', '>', '?',
79 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
80 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
81 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
82 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
83 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
84 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
85 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
86 'x', 'y', 'z', '{', '|', '}', '~', '\?'
89 static int xascii_table
[256];
92 /* Range checks an expression node. If all goes well, returns the
93 node, otherwise returns &gfc_bad_expr and frees the node. */
96 range_check (gfc_expr
* result
, const char *name
)
98 if (gfc_range_check (result
) == ARITH_OK
)
101 gfc_error ("Result of %s overflows its kind at %L", name
, &result
->where
);
102 gfc_free_expr (result
);
103 return &gfc_bad_expr
;
107 /* A helper function that gets an optional and possibly missing
108 kind parameter. Returns the kind, -1 if something went wrong. */
111 get_kind (bt type
, gfc_expr
* k
, const char *name
, int default_kind
)
118 if (k
->expr_type
!= EXPR_CONSTANT
)
120 gfc_error ("KIND parameter of %s at %L must be an initialization "
121 "expression", name
, &k
->where
);
126 if (gfc_extract_int (k
, &kind
) != NULL
127 || gfc_validate_kind (type
, kind
, true) < 0)
130 gfc_error ("Invalid KIND parameter of %s at %L", name
, &k
->where
);
138 /* Checks if X, which is assumed to represent a two's complement
139 integer of binary width BITSIZE, has the signbit set. If so, makes
140 X the corresponding negative number. */
143 twos_complement (mpz_t x
, int bitsize
)
147 if (mpz_tstbit (x
, bitsize
- 1) == 1)
149 mpz_init_set_ui(mask
, 1);
150 mpz_mul_2exp(mask
, mask
, bitsize
);
151 mpz_sub_ui(mask
, mask
, 1);
153 /* We negate the number by hand, zeroing the high bits, that is
154 make it the corresponding positive number, and then have it
155 negated by GMP, giving the correct representation of the
158 mpz_add_ui (x
, x
, 1);
159 mpz_and (x
, x
, mask
);
168 /********************** Simplification functions *****************************/
171 gfc_simplify_abs (gfc_expr
* e
)
175 if (e
->expr_type
!= EXPR_CONSTANT
)
181 result
= gfc_constant_result (BT_INTEGER
, e
->ts
.kind
, &e
->where
);
183 mpz_abs (result
->value
.integer
, e
->value
.integer
);
185 result
= range_check (result
, "IABS");
189 result
= gfc_constant_result (BT_REAL
, e
->ts
.kind
, &e
->where
);
191 mpfr_abs (result
->value
.real
, e
->value
.real
, GFC_RND_MODE
);
193 result
= range_check (result
, "ABS");
197 result
= gfc_constant_result (BT_REAL
, e
->ts
.kind
, &e
->where
);
199 gfc_set_model_kind (e
->ts
.kind
);
201 mpfr_hypot (result
->value
.real
, e
->value
.complex.r
,
202 e
->value
.complex.i
, GFC_RND_MODE
);
203 result
= range_check (result
, "CABS");
207 gfc_internal_error ("gfc_simplify_abs(): Bad type");
215 gfc_simplify_achar (gfc_expr
* e
)
220 if (e
->expr_type
!= EXPR_CONSTANT
)
223 /* We cannot assume that the native character set is ASCII in this
225 if (gfc_extract_int (e
, &index
) != NULL
|| index
< 0 || index
> 127)
227 gfc_error ("Extended ASCII not implemented: argument of ACHAR at %L "
228 "must be between 0 and 127", &e
->where
);
229 return &gfc_bad_expr
;
232 result
= gfc_constant_result (BT_CHARACTER
, gfc_default_character_kind
,
235 result
->value
.character
.string
= gfc_getmem (2);
237 result
->value
.character
.length
= 1;
238 result
->value
.character
.string
[0] = ascii_table
[index
];
239 result
->value
.character
.string
[1] = '\0'; /* For debugger */
245 gfc_simplify_acos (gfc_expr
* x
)
249 if (x
->expr_type
!= EXPR_CONSTANT
)
252 if (mpfr_cmp_si (x
->value
.real
, 1) > 0 || mpfr_cmp_si (x
->value
.real
, -1) < 0)
254 gfc_error ("Argument of ACOS at %L must be between -1 and 1",
256 return &gfc_bad_expr
;
259 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
261 mpfr_acos (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
263 return range_check (result
, "ACOS");
267 gfc_simplify_acosh (gfc_expr
* x
)
271 if (x
->expr_type
!= EXPR_CONSTANT
)
274 if (mpfr_cmp_si (x
->value
.real
, 1) < 0)
276 gfc_error ("Argument of ACOSH at %L must not be less than 1",
278 return &gfc_bad_expr
;
281 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
283 mpfr_acosh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
285 return range_check (result
, "ACOSH");
289 gfc_simplify_adjustl (gfc_expr
* e
)
295 if (e
->expr_type
!= EXPR_CONSTANT
)
298 len
= e
->value
.character
.length
;
300 result
= gfc_constant_result (BT_CHARACTER
, e
->ts
.kind
, &e
->where
);
302 result
->value
.character
.length
= len
;
303 result
->value
.character
.string
= gfc_getmem (len
+ 1);
305 for (count
= 0, i
= 0; i
< len
; ++i
)
307 ch
= e
->value
.character
.string
[i
];
313 for (i
= 0; i
< len
- count
; ++i
)
315 result
->value
.character
.string
[i
] =
316 e
->value
.character
.string
[count
+ i
];
319 for (i
= len
- count
; i
< len
; ++i
)
321 result
->value
.character
.string
[i
] = ' ';
324 result
->value
.character
.string
[len
] = '\0'; /* For debugger */
331 gfc_simplify_adjustr (gfc_expr
* e
)
337 if (e
->expr_type
!= EXPR_CONSTANT
)
340 len
= e
->value
.character
.length
;
342 result
= gfc_constant_result (BT_CHARACTER
, e
->ts
.kind
, &e
->where
);
344 result
->value
.character
.length
= len
;
345 result
->value
.character
.string
= gfc_getmem (len
+ 1);
347 for (count
= 0, i
= len
- 1; i
>= 0; --i
)
349 ch
= e
->value
.character
.string
[i
];
355 for (i
= 0; i
< count
; ++i
)
357 result
->value
.character
.string
[i
] = ' ';
360 for (i
= count
; i
< len
; ++i
)
362 result
->value
.character
.string
[i
] =
363 e
->value
.character
.string
[i
- count
];
366 result
->value
.character
.string
[len
] = '\0'; /* For debugger */
373 gfc_simplify_aimag (gfc_expr
* e
)
378 if (e
->expr_type
!= EXPR_CONSTANT
)
381 result
= gfc_constant_result (BT_REAL
, e
->ts
.kind
, &e
->where
);
382 mpfr_set (result
->value
.real
, e
->value
.complex.i
, GFC_RND_MODE
);
384 return range_check (result
, "AIMAG");
389 gfc_simplify_aint (gfc_expr
* e
, gfc_expr
* k
)
391 gfc_expr
*rtrunc
, *result
;
394 kind
= get_kind (BT_REAL
, k
, "AINT", e
->ts
.kind
);
396 return &gfc_bad_expr
;
398 if (e
->expr_type
!= EXPR_CONSTANT
)
401 rtrunc
= gfc_copy_expr (e
);
403 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
405 result
= gfc_real2real (rtrunc
, kind
);
406 gfc_free_expr (rtrunc
);
408 return range_check (result
, "AINT");
413 gfc_simplify_dint (gfc_expr
* e
)
415 gfc_expr
*rtrunc
, *result
;
417 if (e
->expr_type
!= EXPR_CONSTANT
)
420 rtrunc
= gfc_copy_expr (e
);
422 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
424 result
= gfc_real2real (rtrunc
, gfc_default_double_kind
);
425 gfc_free_expr (rtrunc
);
427 return range_check (result
, "DINT");
432 gfc_simplify_anint (gfc_expr
* e
, gfc_expr
* k
)
437 kind
= get_kind (BT_REAL
, k
, "ANINT", e
->ts
.kind
);
439 return &gfc_bad_expr
;
441 if (e
->expr_type
!= EXPR_CONSTANT
)
444 result
= gfc_constant_result (e
->ts
.type
, kind
, &e
->where
);
446 mpfr_round (result
->value
.real
, e
->value
.real
);
448 return range_check (result
, "ANINT");
453 gfc_simplify_dnint (gfc_expr
* e
)
457 if (e
->expr_type
!= EXPR_CONSTANT
)
460 result
= gfc_constant_result (BT_REAL
, gfc_default_double_kind
, &e
->where
);
462 mpfr_round (result
->value
.real
, e
->value
.real
);
464 return range_check (result
, "DNINT");
469 gfc_simplify_asin (gfc_expr
* x
)
473 if (x
->expr_type
!= EXPR_CONSTANT
)
476 if (mpfr_cmp_si (x
->value
.real
, 1) > 0 || mpfr_cmp_si (x
->value
.real
, -1) < 0)
478 gfc_error ("Argument of ASIN at %L must be between -1 and 1",
480 return &gfc_bad_expr
;
483 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
485 mpfr_asin(result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
487 return range_check (result
, "ASIN");
492 gfc_simplify_asinh (gfc_expr
* x
)
496 if (x
->expr_type
!= EXPR_CONSTANT
)
499 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
501 mpfr_asinh(result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
503 return range_check (result
, "ASINH");
508 gfc_simplify_atan (gfc_expr
* x
)
512 if (x
->expr_type
!= EXPR_CONSTANT
)
515 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
517 mpfr_atan(result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
519 return range_check (result
, "ATAN");
524 gfc_simplify_atanh (gfc_expr
* x
)
528 if (x
->expr_type
!= EXPR_CONSTANT
)
531 if (mpfr_cmp_si (x
->value
.real
, 1) >= 0 ||
532 mpfr_cmp_si (x
->value
.real
, -1) <= 0)
534 gfc_error ("Argument of ATANH at %L must be inside the range -1 to 1",
536 return &gfc_bad_expr
;
539 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
541 mpfr_atanh(result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
543 return range_check (result
, "ATANH");
548 gfc_simplify_atan2 (gfc_expr
* y
, gfc_expr
* x
)
552 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
555 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
557 if (mpfr_sgn (y
->value
.real
) == 0 && mpfr_sgn (x
->value
.real
) == 0)
560 ("If first argument of ATAN2 %L is zero, then the second argument "
561 "must not be zero", &x
->where
);
562 gfc_free_expr (result
);
563 return &gfc_bad_expr
;
566 arctangent2 (y
->value
.real
, x
->value
.real
, result
->value
.real
);
568 return range_check (result
, "ATAN2");
573 gfc_simplify_bit_size (gfc_expr
* e
)
578 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
579 result
= gfc_constant_result (BT_INTEGER
, e
->ts
.kind
, &e
->where
);
580 mpz_set_ui (result
->value
.integer
, gfc_integer_kinds
[i
].bit_size
);
587 gfc_simplify_btest (gfc_expr
* e
, gfc_expr
* bit
)
591 if (e
->expr_type
!= EXPR_CONSTANT
|| bit
->expr_type
!= EXPR_CONSTANT
)
594 if (gfc_extract_int (bit
, &b
) != NULL
|| b
< 0)
595 return gfc_logical_expr (0, &e
->where
);
597 return gfc_logical_expr (mpz_tstbit (e
->value
.integer
, b
), &e
->where
);
602 gfc_simplify_ceiling (gfc_expr
* e
, gfc_expr
* k
)
604 gfc_expr
*ceil
, *result
;
607 kind
= get_kind (BT_INTEGER
, k
, "CEILING", gfc_default_integer_kind
);
609 return &gfc_bad_expr
;
611 if (e
->expr_type
!= EXPR_CONSTANT
)
614 result
= gfc_constant_result (BT_INTEGER
, kind
, &e
->where
);
616 ceil
= gfc_copy_expr (e
);
618 mpfr_ceil (ceil
->value
.real
, e
->value
.real
);
619 gfc_mpfr_to_mpz(result
->value
.integer
, ceil
->value
.real
);
621 gfc_free_expr (ceil
);
623 return range_check (result
, "CEILING");
628 gfc_simplify_char (gfc_expr
* e
, gfc_expr
* k
)
633 kind
= get_kind (BT_CHARACTER
, k
, "CHAR", gfc_default_character_kind
);
635 return &gfc_bad_expr
;
637 if (e
->expr_type
!= EXPR_CONSTANT
)
640 if (gfc_extract_int (e
, &c
) != NULL
|| c
< 0 || c
> 255)
642 gfc_error ("Bad character in CHAR function at %L", &e
->where
);
643 return &gfc_bad_expr
;
646 result
= gfc_constant_result (BT_CHARACTER
, kind
, &e
->where
);
648 result
->value
.character
.length
= 1;
649 result
->value
.character
.string
= gfc_getmem (2);
651 result
->value
.character
.string
[0] = c
;
652 result
->value
.character
.string
[1] = '\0'; /* For debugger */
658 /* Common subroutine for simplifying CMPLX and DCMPLX. */
661 simplify_cmplx (const char *name
, gfc_expr
* x
, gfc_expr
* y
, int kind
)
665 result
= gfc_constant_result (BT_COMPLEX
, kind
, &x
->where
);
667 mpfr_set_ui (result
->value
.complex.i
, 0, GFC_RND_MODE
);
672 mpfr_set_z (result
->value
.complex.r
, x
->value
.integer
, GFC_RND_MODE
);
676 mpfr_set (result
->value
.complex.r
, x
->value
.real
, GFC_RND_MODE
);
680 mpfr_set (result
->value
.complex.r
, x
->value
.complex.r
, GFC_RND_MODE
);
681 mpfr_set (result
->value
.complex.i
, x
->value
.complex.i
, GFC_RND_MODE
);
685 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (x)");
693 mpfr_set_z (result
->value
.complex.i
, y
->value
.integer
, GFC_RND_MODE
);
697 mpfr_set (result
->value
.complex.i
, y
->value
.real
, GFC_RND_MODE
);
701 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (y)");
705 return range_check (result
, name
);
710 gfc_simplify_cmplx (gfc_expr
* x
, gfc_expr
* y
, gfc_expr
* k
)
714 if (x
->expr_type
!= EXPR_CONSTANT
715 || (y
!= NULL
&& y
->expr_type
!= EXPR_CONSTANT
))
718 kind
= get_kind (BT_REAL
, k
, "CMPLX", gfc_default_real_kind
);
720 return &gfc_bad_expr
;
722 return simplify_cmplx ("CMPLX", x
, y
, kind
);
727 gfc_simplify_conjg (gfc_expr
* e
)
731 if (e
->expr_type
!= EXPR_CONSTANT
)
734 result
= gfc_copy_expr (e
);
735 mpfr_neg (result
->value
.complex.i
, result
->value
.complex.i
, GFC_RND_MODE
);
737 return range_check (result
, "CONJG");
742 gfc_simplify_cos (gfc_expr
* x
)
747 if (x
->expr_type
!= EXPR_CONSTANT
)
750 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
755 mpfr_cos (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
758 gfc_set_model_kind (x
->ts
.kind
);
762 mpfr_cos (xp
, x
->value
.complex.r
, GFC_RND_MODE
);
763 mpfr_cosh (xq
, x
->value
.complex.i
, GFC_RND_MODE
);
764 mpfr_mul(result
->value
.complex.r
, xp
, xq
, GFC_RND_MODE
);
766 mpfr_sin (xp
, x
->value
.complex.r
, GFC_RND_MODE
);
767 mpfr_sinh (xq
, x
->value
.complex.i
, GFC_RND_MODE
);
768 mpfr_mul (xp
, xp
, xq
, GFC_RND_MODE
);
769 mpfr_neg (result
->value
.complex.i
, xp
, GFC_RND_MODE
);
775 gfc_internal_error ("in gfc_simplify_cos(): Bad type");
778 return range_check (result
, "COS");
784 gfc_simplify_cosh (gfc_expr
* x
)
788 if (x
->expr_type
!= EXPR_CONSTANT
)
791 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
793 mpfr_cosh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
795 return range_check (result
, "COSH");
800 gfc_simplify_dcmplx (gfc_expr
* x
, gfc_expr
* y
)
803 if (x
->expr_type
!= EXPR_CONSTANT
804 || (y
!= NULL
&& y
->expr_type
!= EXPR_CONSTANT
))
807 return simplify_cmplx ("DCMPLX", x
, y
, gfc_default_double_kind
);
812 gfc_simplify_dble (gfc_expr
* e
)
816 if (e
->expr_type
!= EXPR_CONSTANT
)
822 result
= gfc_int2real (e
, gfc_default_double_kind
);
826 result
= gfc_real2real (e
, gfc_default_double_kind
);
830 result
= gfc_complex2real (e
, gfc_default_double_kind
);
834 gfc_internal_error ("gfc_simplify_dble(): bad type at %L", &e
->where
);
837 return range_check (result
, "DBLE");
842 gfc_simplify_digits (gfc_expr
* x
)
846 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
850 digits
= gfc_integer_kinds
[i
].digits
;
855 digits
= gfc_real_kinds
[i
].digits
;
862 return gfc_int_expr (digits
);
867 gfc_simplify_dim (gfc_expr
* x
, gfc_expr
* y
)
871 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
874 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
879 if (mpz_cmp (x
->value
.integer
, y
->value
.integer
) > 0)
880 mpz_sub (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
882 mpz_set_ui (result
->value
.integer
, 0);
887 if (mpfr_cmp (x
->value
.real
, y
->value
.real
) > 0)
888 mpfr_sub (result
->value
.real
, x
->value
.real
, y
->value
.real
, GFC_RND_MODE
);
890 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
895 gfc_internal_error ("gfc_simplify_dim(): Bad type");
898 return range_check (result
, "DIM");
903 gfc_simplify_dprod (gfc_expr
* x
, gfc_expr
* y
)
905 gfc_expr
*a1
, *a2
, *result
;
907 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
911 gfc_constant_result (BT_REAL
, gfc_default_double_kind
, &x
->where
);
913 a1
= gfc_real2real (x
, gfc_default_double_kind
);
914 a2
= gfc_real2real (y
, gfc_default_double_kind
);
916 mpfr_mul (result
->value
.real
, a1
->value
.real
, a2
->value
.real
, GFC_RND_MODE
);
921 return range_check (result
, "DPROD");
926 gfc_simplify_epsilon (gfc_expr
* e
)
931 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
933 result
= gfc_constant_result (BT_REAL
, e
->ts
.kind
, &e
->where
);
935 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].epsilon
, GFC_RND_MODE
);
937 return range_check (result
, "EPSILON");
942 gfc_simplify_exp (gfc_expr
* x
)
947 if (x
->expr_type
!= EXPR_CONSTANT
)
950 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
955 mpfr_exp(result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
959 gfc_set_model_kind (x
->ts
.kind
);
962 mpfr_exp (xq
, x
->value
.complex.r
, GFC_RND_MODE
);
963 mpfr_cos (xp
, x
->value
.complex.i
, GFC_RND_MODE
);
964 mpfr_mul (result
->value
.complex.r
, xq
, xp
, GFC_RND_MODE
);
965 mpfr_sin (xp
, x
->value
.complex.i
, GFC_RND_MODE
);
966 mpfr_mul (result
->value
.complex.i
, xq
, xp
, GFC_RND_MODE
);
972 gfc_internal_error ("in gfc_simplify_exp(): Bad type");
975 return range_check (result
, "EXP");
978 /* FIXME: MPFR should be able to do this better */
980 gfc_simplify_exponent (gfc_expr
* x
)
986 if (x
->expr_type
!= EXPR_CONSTANT
)
989 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
992 gfc_set_model (x
->value
.real
);
994 if (mpfr_sgn (x
->value
.real
) == 0)
996 mpz_set_ui (result
->value
.integer
, 0);
1002 mpfr_abs (tmp
, x
->value
.real
, GFC_RND_MODE
);
1003 mpfr_log2 (tmp
, tmp
, GFC_RND_MODE
);
1005 gfc_mpfr_to_mpz (result
->value
.integer
, tmp
);
1007 /* The model number for tiny(x) is b**(emin - 1) where b is the base and emin
1008 is the smallest exponent value. So, we need to add 1 if x is tiny(x). */
1009 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
1010 if (mpfr_cmp (x
->value
.real
, gfc_real_kinds
[i
].tiny
) == 0)
1011 mpz_add_ui (result
->value
.integer
,result
->value
.integer
, 1);
1015 return range_check (result
, "EXPONENT");
1020 gfc_simplify_float (gfc_expr
* a
)
1024 if (a
->expr_type
!= EXPR_CONSTANT
)
1027 result
= gfc_int2real (a
, gfc_default_real_kind
);
1028 return range_check (result
, "FLOAT");
1033 gfc_simplify_floor (gfc_expr
* e
, gfc_expr
* k
)
1039 kind
= get_kind (BT_INTEGER
, k
, "FLOOR", gfc_default_integer_kind
);
1041 gfc_internal_error ("gfc_simplify_floor(): Bad kind");
1043 if (e
->expr_type
!= EXPR_CONSTANT
)
1046 result
= gfc_constant_result (BT_INTEGER
, kind
, &e
->where
);
1048 gfc_set_model_kind (kind
);
1050 mpfr_floor (floor
, e
->value
.real
);
1052 gfc_mpfr_to_mpz (result
->value
.integer
, floor
);
1056 return range_check (result
, "FLOOR");
1061 gfc_simplify_fraction (gfc_expr
* x
)
1064 mpfr_t absv
, exp
, pow2
;
1066 if (x
->expr_type
!= EXPR_CONSTANT
)
1069 result
= gfc_constant_result (BT_REAL
, x
->ts
.kind
, &x
->where
);
1071 gfc_set_model_kind (x
->ts
.kind
);
1073 if (mpfr_sgn (x
->value
.real
) == 0)
1075 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
1083 mpfr_abs (absv
, x
->value
.real
, GFC_RND_MODE
);
1084 mpfr_log2 (exp
, absv
, GFC_RND_MODE
);
1086 mpfr_trunc (exp
, exp
);
1087 mpfr_add_ui (exp
, exp
, 1, GFC_RND_MODE
);
1089 mpfr_ui_pow (pow2
, 2, exp
, GFC_RND_MODE
);
1091 mpfr_div (result
->value
.real
, absv
, pow2
, GFC_RND_MODE
);
1097 return range_check (result
, "FRACTION");
1102 gfc_simplify_huge (gfc_expr
* e
)
1107 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
1109 result
= gfc_constant_result (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
1114 mpz_set (result
->value
.integer
, gfc_integer_kinds
[i
].huge
);
1118 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].huge
, GFC_RND_MODE
);
1130 gfc_simplify_iachar (gfc_expr
* e
)
1135 if (e
->expr_type
!= EXPR_CONSTANT
)
1138 if (e
->value
.character
.length
!= 1)
1140 gfc_error ("Argument of IACHAR at %L must be of length one", &e
->where
);
1141 return &gfc_bad_expr
;
1144 index
= xascii_table
[(int) e
->value
.character
.string
[0] & 0xFF];
1146 result
= gfc_int_expr (index
);
1147 result
->where
= e
->where
;
1149 return range_check (result
, "IACHAR");
1154 gfc_simplify_iand (gfc_expr
* x
, gfc_expr
* y
)
1158 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1161 result
= gfc_constant_result (BT_INTEGER
, x
->ts
.kind
, &x
->where
);
1163 mpz_and (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
1165 return range_check (result
, "IAND");
1170 gfc_simplify_ibclr (gfc_expr
* x
, gfc_expr
* y
)
1175 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1178 if (gfc_extract_int (y
, &pos
) != NULL
|| pos
< 0)
1180 gfc_error ("Invalid second argument of IBCLR at %L", &y
->where
);
1181 return &gfc_bad_expr
;
1184 k
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
1186 if (pos
> gfc_integer_kinds
[k
].bit_size
)
1188 gfc_error ("Second argument of IBCLR exceeds bit size at %L",
1190 return &gfc_bad_expr
;
1193 result
= gfc_copy_expr (x
);
1195 mpz_clrbit (result
->value
.integer
, pos
);
1196 return range_check (result
, "IBCLR");
1201 gfc_simplify_ibits (gfc_expr
* x
, gfc_expr
* y
, gfc_expr
* z
)
1208 if (x
->expr_type
!= EXPR_CONSTANT
1209 || y
->expr_type
!= EXPR_CONSTANT
1210 || z
->expr_type
!= EXPR_CONSTANT
)
1213 if (gfc_extract_int (y
, &pos
) != NULL
|| pos
< 0)
1215 gfc_error ("Invalid second argument of IBITS at %L", &y
->where
);
1216 return &gfc_bad_expr
;
1219 if (gfc_extract_int (z
, &len
) != NULL
|| len
< 0)
1221 gfc_error ("Invalid third argument of IBITS at %L", &z
->where
);
1222 return &gfc_bad_expr
;
1225 k
= gfc_validate_kind (BT_INTEGER
, x
->ts
.kind
, false);
1227 bitsize
= gfc_integer_kinds
[k
].bit_size
;
1229 if (pos
+ len
> bitsize
)
1232 ("Sum of second and third arguments of IBITS exceeds bit size "
1233 "at %L", &y
->where
);
1234 return &gfc_bad_expr
;
1237 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1239 bits
= gfc_getmem (bitsize
* sizeof (int));
1241 for (i
= 0; i
< bitsize
; i
++)
1244 for (i
= 0; i
< len
; i
++)
1245 bits
[i
] = mpz_tstbit (x
->value
.integer
, i
+ pos
);
1247 for (i
= 0; i
< bitsize
; i
++)
1251 mpz_clrbit (result
->value
.integer
, i
);
1253 else if (bits
[i
] == 1)
1255 mpz_setbit (result
->value
.integer
, i
);
1259 gfc_internal_error ("IBITS: Bad bit");
1265 return range_check (result
, "IBITS");
1270 gfc_simplify_ibset (gfc_expr
* x
, gfc_expr
* y
)
1275 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1278 if (gfc_extract_int (y
, &pos
) != NULL
|| pos
< 0)
1280 gfc_error ("Invalid second argument of IBSET at %L", &y
->where
);
1281 return &gfc_bad_expr
;
1284 k
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
1286 if (pos
> gfc_integer_kinds
[k
].bit_size
)
1288 gfc_error ("Second argument of IBSET exceeds bit size at %L",
1290 return &gfc_bad_expr
;
1293 result
= gfc_copy_expr (x
);
1295 mpz_setbit (result
->value
.integer
, pos
);
1296 return range_check (result
, "IBSET");
1301 gfc_simplify_ichar (gfc_expr
* e
)
1306 if (e
->expr_type
!= EXPR_CONSTANT
)
1309 if (e
->value
.character
.length
!= 1)
1311 gfc_error ("Argument of ICHAR at %L must be of length one", &e
->where
);
1312 return &gfc_bad_expr
;
1315 index
= (int) e
->value
.character
.string
[0];
1317 if (index
< CHAR_MIN
|| index
> CHAR_MAX
)
1319 gfc_error ("Argument of ICHAR at %L out of range of this processor",
1321 return &gfc_bad_expr
;
1324 result
= gfc_int_expr (index
);
1325 result
->where
= e
->where
;
1326 return range_check (result
, "ICHAR");
1331 gfc_simplify_ieor (gfc_expr
* x
, gfc_expr
* y
)
1335 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1338 result
= gfc_constant_result (BT_INTEGER
, x
->ts
.kind
, &x
->where
);
1340 mpz_xor (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
1342 return range_check (result
, "IEOR");
1347 gfc_simplify_index (gfc_expr
* x
, gfc_expr
* y
, gfc_expr
* b
)
1350 int back
, len
, lensub
;
1351 int i
, j
, k
, count
, index
= 0, start
;
1353 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1356 if (b
!= NULL
&& b
->value
.logical
!= 0)
1361 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
1364 len
= x
->value
.character
.length
;
1365 lensub
= y
->value
.character
.length
;
1369 mpz_set_si (result
->value
.integer
, 0);
1378 mpz_set_si (result
->value
.integer
, 1);
1381 else if (lensub
== 1)
1383 for (i
= 0; i
< len
; i
++)
1385 for (j
= 0; j
< lensub
; j
++)
1387 if (y
->value
.character
.string
[j
] ==
1388 x
->value
.character
.string
[i
])
1398 for (i
= 0; i
< len
; i
++)
1400 for (j
= 0; j
< lensub
; j
++)
1402 if (y
->value
.character
.string
[j
] ==
1403 x
->value
.character
.string
[i
])
1408 for (k
= 0; k
< lensub
; k
++)
1410 if (y
->value
.character
.string
[k
] ==
1411 x
->value
.character
.string
[k
+ start
])
1415 if (count
== lensub
)
1431 mpz_set_si (result
->value
.integer
, len
+ 1);
1434 else if (lensub
== 1)
1436 for (i
= 0; i
< len
; i
++)
1438 for (j
= 0; j
< lensub
; j
++)
1440 if (y
->value
.character
.string
[j
] ==
1441 x
->value
.character
.string
[len
- i
])
1443 index
= len
- i
+ 1;
1451 for (i
= 0; i
< len
; i
++)
1453 for (j
= 0; j
< lensub
; j
++)
1455 if (y
->value
.character
.string
[j
] ==
1456 x
->value
.character
.string
[len
- i
])
1459 if (start
<= len
- lensub
)
1462 for (k
= 0; k
< lensub
; k
++)
1463 if (y
->value
.character
.string
[k
] ==
1464 x
->value
.character
.string
[k
+ start
])
1467 if (count
== lensub
)
1484 mpz_set_si (result
->value
.integer
, index
);
1485 return range_check (result
, "INDEX");
1490 gfc_simplify_int (gfc_expr
* e
, gfc_expr
* k
)
1492 gfc_expr
*rpart
, *rtrunc
, *result
;
1495 kind
= get_kind (BT_INTEGER
, k
, "INT", gfc_default_integer_kind
);
1497 return &gfc_bad_expr
;
1499 if (e
->expr_type
!= EXPR_CONSTANT
)
1502 result
= gfc_constant_result (BT_INTEGER
, kind
, &e
->where
);
1507 mpz_set (result
->value
.integer
, e
->value
.integer
);
1511 rtrunc
= gfc_copy_expr (e
);
1512 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
1513 gfc_mpfr_to_mpz (result
->value
.integer
, rtrunc
->value
.real
);
1514 gfc_free_expr (rtrunc
);
1518 rpart
= gfc_complex2real (e
, kind
);
1519 rtrunc
= gfc_copy_expr (rpart
);
1520 mpfr_trunc (rtrunc
->value
.real
, rpart
->value
.real
);
1521 gfc_mpfr_to_mpz (result
->value
.integer
, rtrunc
->value
.real
);
1522 gfc_free_expr (rpart
);
1523 gfc_free_expr (rtrunc
);
1527 gfc_error ("Argument of INT at %L is not a valid type", &e
->where
);
1528 gfc_free_expr (result
);
1529 return &gfc_bad_expr
;
1532 return range_check (result
, "INT");
1537 gfc_simplify_ifix (gfc_expr
* e
)
1539 gfc_expr
*rtrunc
, *result
;
1541 if (e
->expr_type
!= EXPR_CONSTANT
)
1544 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
1547 rtrunc
= gfc_copy_expr (e
);
1549 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
1550 gfc_mpfr_to_mpz (result
->value
.integer
, rtrunc
->value
.real
);
1552 gfc_free_expr (rtrunc
);
1553 return range_check (result
, "IFIX");
1558 gfc_simplify_idint (gfc_expr
* e
)
1560 gfc_expr
*rtrunc
, *result
;
1562 if (e
->expr_type
!= EXPR_CONSTANT
)
1565 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
1568 rtrunc
= gfc_copy_expr (e
);
1570 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
1571 gfc_mpfr_to_mpz (result
->value
.integer
, rtrunc
->value
.real
);
1573 gfc_free_expr (rtrunc
);
1574 return range_check (result
, "IDINT");
1579 gfc_simplify_ior (gfc_expr
* x
, gfc_expr
* y
)
1583 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1586 result
= gfc_constant_result (BT_INTEGER
, x
->ts
.kind
, &x
->where
);
1588 mpz_ior (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
1589 return range_check (result
, "IOR");
1594 gfc_simplify_ishft (gfc_expr
* e
, gfc_expr
* s
)
1597 int shift
, ashift
, isize
, k
, *bits
, i
;
1599 if (e
->expr_type
!= EXPR_CONSTANT
|| s
->expr_type
!= EXPR_CONSTANT
)
1602 if (gfc_extract_int (s
, &shift
) != NULL
)
1604 gfc_error ("Invalid second argument of ISHFT at %L", &s
->where
);
1605 return &gfc_bad_expr
;
1608 k
= gfc_validate_kind (BT_INTEGER
, e
->ts
.kind
, false);
1610 isize
= gfc_integer_kinds
[k
].bit_size
;
1620 ("Magnitude of second argument of ISHFT exceeds bit size at %L",
1622 return &gfc_bad_expr
;
1625 result
= gfc_constant_result (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
1629 mpz_set (result
->value
.integer
, e
->value
.integer
);
1630 return range_check (result
, "ISHFT");
1633 bits
= gfc_getmem (isize
* sizeof (int));
1635 for (i
= 0; i
< isize
; i
++)
1636 bits
[i
] = mpz_tstbit (e
->value
.integer
, i
);
1640 for (i
= 0; i
< shift
; i
++)
1641 mpz_clrbit (result
->value
.integer
, i
);
1643 for (i
= 0; i
< isize
- shift
; i
++)
1646 mpz_clrbit (result
->value
.integer
, i
+ shift
);
1648 mpz_setbit (result
->value
.integer
, i
+ shift
);
1653 for (i
= isize
- 1; i
>= isize
- ashift
; i
--)
1654 mpz_clrbit (result
->value
.integer
, i
);
1656 for (i
= isize
- 1; i
>= ashift
; i
--)
1659 mpz_clrbit (result
->value
.integer
, i
- ashift
);
1661 mpz_setbit (result
->value
.integer
, i
- ashift
);
1665 twos_complement (result
->value
.integer
, isize
);
1673 gfc_simplify_ishftc (gfc_expr
* e
, gfc_expr
* s
, gfc_expr
* sz
)
1676 int shift
, ashift
, isize
, delta
, k
;
1679 if (e
->expr_type
!= EXPR_CONSTANT
|| s
->expr_type
!= EXPR_CONSTANT
)
1682 if (gfc_extract_int (s
, &shift
) != NULL
)
1684 gfc_error ("Invalid second argument of ISHFTC at %L", &s
->where
);
1685 return &gfc_bad_expr
;
1688 k
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
1692 if (gfc_extract_int (sz
, &isize
) != NULL
|| isize
< 0)
1694 gfc_error ("Invalid third argument of ISHFTC at %L", &sz
->where
);
1695 return &gfc_bad_expr
;
1699 isize
= gfc_integer_kinds
[k
].bit_size
;
1709 ("Magnitude of second argument of ISHFTC exceeds third argument "
1710 "at %L", &s
->where
);
1711 return &gfc_bad_expr
;
1714 result
= gfc_constant_result (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
1718 mpz_set (result
->value
.integer
, e
->value
.integer
);
1722 bits
= gfc_getmem (isize
* sizeof (int));
1724 for (i
= 0; i
< isize
; i
++)
1725 bits
[i
] = mpz_tstbit (e
->value
.integer
, i
);
1727 delta
= isize
- ashift
;
1731 for (i
= 0; i
< delta
; i
++)
1734 mpz_clrbit (result
->value
.integer
, i
+ shift
);
1736 mpz_setbit (result
->value
.integer
, i
+ shift
);
1739 for (i
= delta
; i
< isize
; i
++)
1742 mpz_clrbit (result
->value
.integer
, i
- delta
);
1744 mpz_setbit (result
->value
.integer
, i
- delta
);
1749 for (i
= 0; i
< ashift
; i
++)
1752 mpz_clrbit (result
->value
.integer
, i
+ delta
);
1754 mpz_setbit (result
->value
.integer
, i
+ delta
);
1757 for (i
= ashift
; i
< isize
; i
++)
1760 mpz_clrbit (result
->value
.integer
, i
+ shift
);
1762 mpz_setbit (result
->value
.integer
, i
+ shift
);
1766 twos_complement (result
->value
.integer
, isize
);
1774 gfc_simplify_kind (gfc_expr
* e
)
1777 if (e
->ts
.type
== BT_DERIVED
)
1779 gfc_error ("Argument of KIND at %L is a DERIVED type", &e
->where
);
1780 return &gfc_bad_expr
;
1783 return gfc_int_expr (e
->ts
.kind
);
1788 simplify_bound (gfc_expr
* array
, gfc_expr
* dim
, int upper
)
1795 if (array
->expr_type
!= EXPR_VARIABLE
)
1799 /* TODO: Simplify constant multi-dimensional bounds. */
1802 if (dim
->expr_type
!= EXPR_CONSTANT
)
1805 /* Follow any component references. */
1806 as
= array
->symtree
->n
.sym
->as
;
1807 for (ref
= array
->ref
; ref
; ref
= ref
->next
)
1812 switch (ref
->u
.ar
.type
)
1819 /* We're done because 'as' has already been set in the
1820 previous iteration. */
1831 as
= ref
->u
.c
.component
->as
;
1842 if (as
->type
== AS_DEFERRED
|| as
->type
== AS_ASSUMED_SHAPE
)
1845 d
= mpz_get_si (dim
->value
.integer
);
1847 if (d
< 1 || d
> as
->rank
1848 || (d
== as
->rank
&& as
->type
== AS_ASSUMED_SIZE
&& upper
))
1850 gfc_error ("DIM argument at %L is out of bounds", &dim
->where
);
1851 return &gfc_bad_expr
;
1854 e
= upper
? as
->upper
[d
-1] : as
->lower
[d
-1];
1856 if (e
->expr_type
!= EXPR_CONSTANT
)
1859 return gfc_copy_expr (e
);
1864 gfc_simplify_lbound (gfc_expr
* array
, gfc_expr
* dim
)
1866 return simplify_bound (array
, dim
, 0);
1871 gfc_simplify_len (gfc_expr
* e
)
1875 if (e
->expr_type
!= EXPR_CONSTANT
)
1878 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
1881 mpz_set_si (result
->value
.integer
, e
->value
.character
.length
);
1882 return range_check (result
, "LEN");
1887 gfc_simplify_len_trim (gfc_expr
* e
)
1890 int count
, len
, lentrim
, i
;
1892 if (e
->expr_type
!= EXPR_CONSTANT
)
1895 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
1898 len
= e
->value
.character
.length
;
1900 for (count
= 0, i
= 1; i
<= len
; i
++)
1901 if (e
->value
.character
.string
[len
- i
] == ' ')
1906 lentrim
= len
- count
;
1908 mpz_set_si (result
->value
.integer
, lentrim
);
1909 return range_check (result
, "LEN_TRIM");
1914 gfc_simplify_lge (gfc_expr
* a
, gfc_expr
* b
)
1917 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
1920 return gfc_logical_expr (gfc_compare_string (a
, b
, xascii_table
) >= 0,
1926 gfc_simplify_lgt (gfc_expr
* a
, gfc_expr
* b
)
1929 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
1932 return gfc_logical_expr (gfc_compare_string (a
, b
, xascii_table
) > 0,
1938 gfc_simplify_lle (gfc_expr
* a
, gfc_expr
* b
)
1941 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
1944 return gfc_logical_expr (gfc_compare_string (a
, b
, xascii_table
) <= 0,
1950 gfc_simplify_llt (gfc_expr
* a
, gfc_expr
* b
)
1953 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
1956 return gfc_logical_expr (gfc_compare_string (a
, b
, xascii_table
) < 0,
1962 gfc_simplify_log (gfc_expr
* x
)
1967 if (x
->expr_type
!= EXPR_CONSTANT
)
1970 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1972 gfc_set_model_kind (x
->ts
.kind
);
1977 if (mpfr_sgn (x
->value
.real
) <= 0)
1980 ("Argument of LOG at %L cannot be less than or equal to zero",
1982 gfc_free_expr (result
);
1983 return &gfc_bad_expr
;
1986 mpfr_log(result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1990 if ((mpfr_sgn (x
->value
.complex.r
) == 0)
1991 && (mpfr_sgn (x
->value
.complex.i
) == 0))
1993 gfc_error ("Complex argument of LOG at %L cannot be zero",
1995 gfc_free_expr (result
);
1996 return &gfc_bad_expr
;
2002 arctangent2 (x
->value
.complex.i
, x
->value
.complex.r
,
2003 result
->value
.complex.i
);
2005 mpfr_mul (xr
, x
->value
.complex.r
, x
->value
.complex.r
, GFC_RND_MODE
);
2006 mpfr_mul (xi
, x
->value
.complex.i
, x
->value
.complex.i
, GFC_RND_MODE
);
2007 mpfr_add (xr
, xr
, xi
, GFC_RND_MODE
);
2008 mpfr_sqrt (xr
, xr
, GFC_RND_MODE
);
2009 mpfr_log (result
->value
.complex.r
, xr
, GFC_RND_MODE
);
2017 gfc_internal_error ("gfc_simplify_log: bad type");
2020 return range_check (result
, "LOG");
2025 gfc_simplify_log10 (gfc_expr
* x
)
2029 if (x
->expr_type
!= EXPR_CONSTANT
)
2032 gfc_set_model_kind (x
->ts
.kind
);
2034 if (mpfr_sgn (x
->value
.real
) <= 0)
2037 ("Argument of LOG10 at %L cannot be less than or equal to zero",
2039 return &gfc_bad_expr
;
2042 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2044 mpfr_log10 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
2046 return range_check (result
, "LOG10");
2051 gfc_simplify_logical (gfc_expr
* e
, gfc_expr
* k
)
2056 kind
= get_kind (BT_LOGICAL
, k
, "LOGICAL", gfc_default_logical_kind
);
2058 return &gfc_bad_expr
;
2060 if (e
->expr_type
!= EXPR_CONSTANT
)
2063 result
= gfc_constant_result (BT_LOGICAL
, kind
, &e
->where
);
2065 result
->value
.logical
= e
->value
.logical
;
2071 /* This function is special since MAX() can take any number of
2072 arguments. The simplified expression is a rewritten version of the
2073 argument list containing at most one constant element. Other
2074 constant elements are deleted. Because the argument list has
2075 already been checked, this function always succeeds. sign is 1 for
2076 MAX(), -1 for MIN(). */
2079 simplify_min_max (gfc_expr
* expr
, int sign
)
2081 gfc_actual_arglist
*arg
, *last
, *extremum
;
2082 gfc_intrinsic_sym
* specific
;
2086 specific
= expr
->value
.function
.isym
;
2088 arg
= expr
->value
.function
.actual
;
2090 for (; arg
; last
= arg
, arg
= arg
->next
)
2092 if (arg
->expr
->expr_type
!= EXPR_CONSTANT
)
2095 if (extremum
== NULL
)
2101 switch (arg
->expr
->ts
.type
)
2104 if (mpz_cmp (arg
->expr
->value
.integer
,
2105 extremum
->expr
->value
.integer
) * sign
> 0)
2106 mpz_set (extremum
->expr
->value
.integer
, arg
->expr
->value
.integer
);
2111 if (mpfr_cmp (arg
->expr
->value
.real
, extremum
->expr
->value
.real
) *
2113 mpfr_set (extremum
->expr
->value
.real
, arg
->expr
->value
.real
,
2119 gfc_internal_error ("gfc_simplify_max(): Bad type in arglist");
2122 /* Delete the extra constant argument. */
2124 expr
->value
.function
.actual
= arg
->next
;
2126 last
->next
= arg
->next
;
2129 gfc_free_actual_arglist (arg
);
2133 /* If there is one value left, replace the function call with the
2135 if (expr
->value
.function
.actual
->next
!= NULL
)
2138 /* Convert to the correct type and kind. */
2139 if (expr
->ts
.type
!= BT_UNKNOWN
)
2140 return gfc_convert_constant (expr
->value
.function
.actual
->expr
,
2141 expr
->ts
.type
, expr
->ts
.kind
);
2143 if (specific
->ts
.type
!= BT_UNKNOWN
)
2144 return gfc_convert_constant (expr
->value
.function
.actual
->expr
,
2145 specific
->ts
.type
, specific
->ts
.kind
);
2147 return gfc_copy_expr (expr
->value
.function
.actual
->expr
);
2152 gfc_simplify_min (gfc_expr
* e
)
2154 return simplify_min_max (e
, -1);
2159 gfc_simplify_max (gfc_expr
* e
)
2161 return simplify_min_max (e
, 1);
2166 gfc_simplify_maxexponent (gfc_expr
* x
)
2171 i
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, false);
2173 result
= gfc_int_expr (gfc_real_kinds
[i
].max_exponent
);
2174 result
->where
= x
->where
;
2181 gfc_simplify_minexponent (gfc_expr
* x
)
2186 i
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, false);
2188 result
= gfc_int_expr (gfc_real_kinds
[i
].min_exponent
);
2189 result
->where
= x
->where
;
2196 gfc_simplify_mod (gfc_expr
* a
, gfc_expr
* p
)
2199 mpfr_t quot
, iquot
, term
;
2201 if (a
->expr_type
!= EXPR_CONSTANT
|| p
->expr_type
!= EXPR_CONSTANT
)
2204 result
= gfc_constant_result (a
->ts
.type
, a
->ts
.kind
, &a
->where
);
2209 if (mpz_cmp_ui (p
->value
.integer
, 0) == 0)
2211 /* Result is processor-dependent. */
2212 gfc_error ("Second argument MOD at %L is zero", &a
->where
);
2213 gfc_free_expr (result
);
2214 return &gfc_bad_expr
;
2216 mpz_tdiv_r (result
->value
.integer
, a
->value
.integer
, p
->value
.integer
);
2220 if (mpfr_cmp_ui (p
->value
.real
, 0) == 0)
2222 /* Result is processor-dependent. */
2223 gfc_error ("Second argument of MOD at %L is zero", &p
->where
);
2224 gfc_free_expr (result
);
2225 return &gfc_bad_expr
;
2228 gfc_set_model_kind (a
->ts
.kind
);
2233 mpfr_div (quot
, a
->value
.real
, p
->value
.real
, GFC_RND_MODE
);
2234 mpfr_trunc (iquot
, quot
);
2235 mpfr_mul (term
, iquot
, p
->value
.real
, GFC_RND_MODE
);
2236 mpfr_sub (result
->value
.real
, a
->value
.real
, term
, GFC_RND_MODE
);
2244 gfc_internal_error ("gfc_simplify_mod(): Bad arguments");
2247 return range_check (result
, "MOD");
2252 gfc_simplify_modulo (gfc_expr
* a
, gfc_expr
* p
)
2255 mpfr_t quot
, iquot
, term
;
2257 if (a
->expr_type
!= EXPR_CONSTANT
|| p
->expr_type
!= EXPR_CONSTANT
)
2260 result
= gfc_constant_result (a
->ts
.type
, a
->ts
.kind
, &a
->where
);
2265 if (mpz_cmp_ui (p
->value
.integer
, 0) == 0)
2267 /* Result is processor-dependent. This processor just opts
2268 to not handle it at all. */
2269 gfc_error ("Second argument of MODULO at %L is zero", &a
->where
);
2270 gfc_free_expr (result
);
2271 return &gfc_bad_expr
;
2273 mpz_fdiv_r (result
->value
.integer
, a
->value
.integer
, p
->value
.integer
);
2278 if (mpfr_cmp_ui (p
->value
.real
, 0) == 0)
2280 /* Result is processor-dependent. */
2281 gfc_error ("Second argument of MODULO at %L is zero", &p
->where
);
2282 gfc_free_expr (result
);
2283 return &gfc_bad_expr
;
2286 gfc_set_model_kind (a
->ts
.kind
);
2291 mpfr_div (quot
, a
->value
.real
, p
->value
.real
, GFC_RND_MODE
);
2292 mpfr_floor (iquot
, quot
);
2293 mpfr_mul (term
, iquot
, p
->value
.real
, GFC_RND_MODE
);
2294 mpfr_sub (result
->value
.real
, a
->value
.real
, term
, GFC_RND_MODE
);
2302 gfc_internal_error ("gfc_simplify_modulo(): Bad arguments");
2305 return range_check (result
, "MODULO");
2309 /* Exists for the sole purpose of consistency with other intrinsics. */
2311 gfc_simplify_mvbits (gfc_expr
* f ATTRIBUTE_UNUSED
,
2312 gfc_expr
* fp ATTRIBUTE_UNUSED
,
2313 gfc_expr
* l ATTRIBUTE_UNUSED
,
2314 gfc_expr
* to ATTRIBUTE_UNUSED
,
2315 gfc_expr
* tp ATTRIBUTE_UNUSED
)
2322 gfc_simplify_nearest (gfc_expr
* x
, gfc_expr
* s
)
2328 if (x
->expr_type
!= EXPR_CONSTANT
|| s
->expr_type
!= EXPR_CONSTANT
)
2331 gfc_set_model_kind (x
->ts
.kind
);
2332 result
= gfc_copy_expr (x
);
2334 direction
= mpfr_sgn (s
->value
.real
);
2338 gfc_error ("Second argument of NEAREST at %L may not be zero",
2341 return &gfc_bad_expr
;
2344 /* TODO: Use mpfr_nextabove and mpfr_nextbelow once we move to a
2345 newer version of mpfr. */
2347 sgn
= mpfr_sgn (x
->value
.real
);
2351 int k
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, 0);
2354 mpfr_add (result
->value
.real
,
2355 x
->value
.real
, gfc_real_kinds
[k
].subnormal
, GFC_RND_MODE
);
2357 mpfr_sub (result
->value
.real
,
2358 x
->value
.real
, gfc_real_kinds
[k
].subnormal
, GFC_RND_MODE
);
2364 direction
= -direction
;
2365 mpfr_neg (result
->value
.real
, result
->value
.real
, GFC_RND_MODE
);
2369 mpfr_add_one_ulp (result
->value
.real
, GFC_RND_MODE
);
2372 /* In this case the exponent can shrink, which makes us skip
2373 over one number because we subtract one ulp with the
2374 larger exponent. Thus we need to compensate for this. */
2375 mpfr_init_set (tmp
, result
->value
.real
, GFC_RND_MODE
);
2377 mpfr_sub_one_ulp (result
->value
.real
, GFC_RND_MODE
);
2378 mpfr_add_one_ulp (result
->value
.real
, GFC_RND_MODE
);
2380 /* If we're back to where we started, the spacing is one
2381 ulp, and we get the correct result by subtracting. */
2382 if (mpfr_cmp (tmp
, result
->value
.real
) == 0)
2383 mpfr_sub_one_ulp (result
->value
.real
, GFC_RND_MODE
);
2389 mpfr_neg (result
->value
.real
, result
->value
.real
, GFC_RND_MODE
);
2392 return range_check (result
, "NEAREST");
2397 simplify_nint (const char *name
, gfc_expr
* e
, gfc_expr
* k
)
2399 gfc_expr
*itrunc
, *result
;
2402 kind
= get_kind (BT_INTEGER
, k
, name
, gfc_default_integer_kind
);
2404 return &gfc_bad_expr
;
2406 if (e
->expr_type
!= EXPR_CONSTANT
)
2409 result
= gfc_constant_result (BT_INTEGER
, kind
, &e
->where
);
2411 itrunc
= gfc_copy_expr (e
);
2413 mpfr_round(itrunc
->value
.real
, e
->value
.real
);
2415 gfc_mpfr_to_mpz (result
->value
.integer
, itrunc
->value
.real
);
2417 gfc_free_expr (itrunc
);
2419 return range_check (result
, name
);
2424 gfc_simplify_nint (gfc_expr
* e
, gfc_expr
* k
)
2426 return simplify_nint ("NINT", e
, k
);
2431 gfc_simplify_idnint (gfc_expr
* e
)
2433 return simplify_nint ("IDNINT", e
, NULL
);
2438 gfc_simplify_not (gfc_expr
* e
)
2443 if (e
->expr_type
!= EXPR_CONSTANT
)
2446 result
= gfc_constant_result (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
2448 mpz_com (result
->value
.integer
, e
->value
.integer
);
2450 /* Because of how GMP handles numbers, the result must be ANDed with
2451 the max_int mask. For radices <> 2, this will require change. */
2453 i
= gfc_validate_kind (BT_INTEGER
, e
->ts
.kind
, false);
2455 mpz_and (result
->value
.integer
, result
->value
.integer
,
2456 gfc_integer_kinds
[i
].max_int
);
2458 return range_check (result
, "NOT");
2463 gfc_simplify_null (gfc_expr
* mold
)
2467 result
= gfc_get_expr ();
2468 result
->expr_type
= EXPR_NULL
;
2471 result
->ts
.type
= BT_UNKNOWN
;
2474 result
->ts
= mold
->ts
;
2475 result
->where
= mold
->where
;
2483 gfc_simplify_precision (gfc_expr
* e
)
2488 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
2490 result
= gfc_int_expr (gfc_real_kinds
[i
].precision
);
2491 result
->where
= e
->where
;
2498 gfc_simplify_radix (gfc_expr
* e
)
2503 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
2507 i
= gfc_integer_kinds
[i
].radix
;
2511 i
= gfc_real_kinds
[i
].radix
;
2518 result
= gfc_int_expr (i
);
2519 result
->where
= e
->where
;
2526 gfc_simplify_range (gfc_expr
* e
)
2532 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
2537 j
= gfc_integer_kinds
[i
].range
;
2542 j
= gfc_real_kinds
[i
].range
;
2549 result
= gfc_int_expr (j
);
2550 result
->where
= e
->where
;
2557 gfc_simplify_real (gfc_expr
* e
, gfc_expr
* k
)
2562 if (e
->ts
.type
== BT_COMPLEX
)
2563 kind
= get_kind (BT_REAL
, k
, "REAL", e
->ts
.kind
);
2565 kind
= get_kind (BT_REAL
, k
, "REAL", gfc_default_real_kind
);
2568 return &gfc_bad_expr
;
2570 if (e
->expr_type
!= EXPR_CONSTANT
)
2576 result
= gfc_int2real (e
, kind
);
2580 result
= gfc_real2real (e
, kind
);
2584 result
= gfc_complex2real (e
, kind
);
2588 gfc_internal_error ("bad type in REAL");
2592 return range_check (result
, "REAL");
2597 gfc_simplify_realpart (gfc_expr
* e
)
2601 if (e
->expr_type
!= EXPR_CONSTANT
)
2604 result
= gfc_constant_result (BT_REAL
, e
->ts
.kind
, &e
->where
);
2605 mpfr_set (result
->value
.real
, e
->value
.complex.r
, GFC_RND_MODE
);
2607 return range_check (result
, "REALPART");
2611 gfc_simplify_repeat (gfc_expr
* e
, gfc_expr
* n
)
2614 int i
, j
, len
, ncopies
, nlen
;
2616 if (e
->expr_type
!= EXPR_CONSTANT
|| n
->expr_type
!= EXPR_CONSTANT
)
2619 if (n
!= NULL
&& (gfc_extract_int (n
, &ncopies
) != NULL
|| ncopies
< 0))
2621 gfc_error ("Invalid second argument of REPEAT at %L", &n
->where
);
2622 return &gfc_bad_expr
;
2625 len
= e
->value
.character
.length
;
2626 nlen
= ncopies
* len
;
2628 result
= gfc_constant_result (BT_CHARACTER
, e
->ts
.kind
, &e
->where
);
2632 result
->value
.character
.string
= gfc_getmem (1);
2633 result
->value
.character
.length
= 0;
2634 result
->value
.character
.string
[0] = '\0';
2638 result
->value
.character
.length
= nlen
;
2639 result
->value
.character
.string
= gfc_getmem (nlen
+ 1);
2641 for (i
= 0; i
< ncopies
; i
++)
2642 for (j
= 0; j
< len
; j
++)
2643 result
->value
.character
.string
[j
+ i
* len
] =
2644 e
->value
.character
.string
[j
];
2646 result
->value
.character
.string
[nlen
] = '\0'; /* For debugger */
2651 /* This one is a bear, but mainly has to do with shuffling elements. */
2654 gfc_simplify_reshape (gfc_expr
* source
, gfc_expr
* shape_exp
,
2655 gfc_expr
* pad
, gfc_expr
* order_exp
)
2658 int order
[GFC_MAX_DIMENSIONS
], shape
[GFC_MAX_DIMENSIONS
];
2659 int i
, rank
, npad
, x
[GFC_MAX_DIMENSIONS
];
2660 gfc_constructor
*head
, *tail
;
2666 /* Unpack the shape array. */
2667 if (source
->expr_type
!= EXPR_ARRAY
|| !gfc_is_constant_expr (source
))
2670 if (shape_exp
->expr_type
!= EXPR_ARRAY
|| !gfc_is_constant_expr (shape_exp
))
2674 && (pad
->expr_type
!= EXPR_ARRAY
2675 || !gfc_is_constant_expr (pad
)))
2678 if (order_exp
!= NULL
2679 && (order_exp
->expr_type
!= EXPR_ARRAY
2680 || !gfc_is_constant_expr (order_exp
)))
2689 e
= gfc_get_array_element (shape_exp
, rank
);
2693 if (gfc_extract_int (e
, &shape
[rank
]) != NULL
)
2695 gfc_error ("Integer too large in shape specification at %L",
2703 if (rank
>= GFC_MAX_DIMENSIONS
)
2705 gfc_error ("Too many dimensions in shape specification for RESHAPE "
2706 "at %L", &e
->where
);
2711 if (shape
[rank
] < 0)
2713 gfc_error ("Shape specification at %L cannot be negative",
2723 gfc_error ("Shape specification at %L cannot be the null array",
2728 /* Now unpack the order array if present. */
2729 if (order_exp
== NULL
)
2731 for (i
= 0; i
< rank
; i
++)
2738 for (i
= 0; i
< rank
; i
++)
2741 for (i
= 0; i
< rank
; i
++)
2743 e
= gfc_get_array_element (order_exp
, i
);
2747 ("ORDER parameter of RESHAPE at %L is not the same size "
2748 "as SHAPE parameter", &order_exp
->where
);
2752 if (gfc_extract_int (e
, &order
[i
]) != NULL
)
2754 gfc_error ("Error in ORDER parameter of RESHAPE at %L",
2762 if (order
[i
] < 1 || order
[i
] > rank
)
2764 gfc_error ("ORDER parameter of RESHAPE at %L is out of range",
2773 gfc_error ("Invalid permutation in ORDER parameter at %L",
2782 /* Count the elements in the source and padding arrays. */
2787 gfc_array_size (pad
, &size
);
2788 npad
= mpz_get_ui (size
);
2792 gfc_array_size (source
, &size
);
2793 nsource
= mpz_get_ui (size
);
2796 /* If it weren't for that pesky permutation we could just loop
2797 through the source and round out any shortage with pad elements.
2798 But no, someone just had to have the compiler do something the
2799 user should be doing. */
2801 for (i
= 0; i
< rank
; i
++)
2806 /* Figure out which element to extract. */
2807 mpz_set_ui (index
, 0);
2809 for (i
= rank
- 1; i
>= 0; i
--)
2811 mpz_add_ui (index
, index
, x
[order
[i
]]);
2813 mpz_mul_ui (index
, index
, shape
[order
[i
- 1]]);
2816 if (mpz_cmp_ui (index
, INT_MAX
) > 0)
2817 gfc_internal_error ("Reshaped array too large at %L", &e
->where
);
2819 j
= mpz_get_ui (index
);
2822 e
= gfc_get_array_element (source
, j
);
2830 ("PAD parameter required for short SOURCE parameter at %L",
2836 e
= gfc_get_array_element (pad
, j
);
2840 head
= tail
= gfc_get_constructor ();
2843 tail
->next
= gfc_get_constructor ();
2850 tail
->where
= e
->where
;
2853 /* Calculate the next element. */
2857 if (++x
[i
] < shape
[i
])
2868 e
= gfc_get_expr ();
2869 e
->where
= source
->where
;
2870 e
->expr_type
= EXPR_ARRAY
;
2871 e
->value
.constructor
= head
;
2872 e
->shape
= gfc_get_shape (rank
);
2874 for (i
= 0; i
< rank
; i
++)
2875 mpz_init_set_ui (e
->shape
[i
], shape
[i
]);
2883 gfc_free_constructor (head
);
2885 return &gfc_bad_expr
;
2890 gfc_simplify_rrspacing (gfc_expr
* x
)
2893 mpfr_t absv
, log2
, exp
, frac
, pow2
;
2896 if (x
->expr_type
!= EXPR_CONSTANT
)
2899 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
2901 result
= gfc_constant_result (BT_REAL
, x
->ts
.kind
, &x
->where
);
2903 p
= gfc_real_kinds
[i
].digits
;
2905 gfc_set_model_kind (x
->ts
.kind
);
2907 if (mpfr_sgn (x
->value
.real
) == 0)
2909 mpfr_ui_div (result
->value
.real
, 1, gfc_real_kinds
[i
].tiny
, GFC_RND_MODE
);
2918 mpfr_abs (absv
, x
->value
.real
, GFC_RND_MODE
);
2919 mpfr_log2 (log2
, absv
, GFC_RND_MODE
);
2921 mpfr_trunc (log2
, log2
);
2922 mpfr_add_ui (exp
, log2
, 1, GFC_RND_MODE
);
2924 mpfr_ui_pow (pow2
, 2, exp
, GFC_RND_MODE
);
2925 mpfr_div (frac
, absv
, pow2
, GFC_RND_MODE
);
2927 mpfr_mul_2exp (result
->value
.real
, frac
, (unsigned long)p
, GFC_RND_MODE
);
2934 return range_check (result
, "RRSPACING");
2939 gfc_simplify_scale (gfc_expr
* x
, gfc_expr
* i
)
2941 int k
, neg_flag
, power
, exp_range
;
2942 mpfr_t scale
, radix
;
2945 if (x
->expr_type
!= EXPR_CONSTANT
|| i
->expr_type
!= EXPR_CONSTANT
)
2948 result
= gfc_constant_result (BT_REAL
, x
->ts
.kind
, &x
->where
);
2950 if (mpfr_sgn (x
->value
.real
) == 0)
2952 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
2956 k
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, false);
2958 exp_range
= gfc_real_kinds
[k
].max_exponent
- gfc_real_kinds
[k
].min_exponent
;
2960 /* This check filters out values of i that would overflow an int. */
2961 if (mpz_cmp_si (i
->value
.integer
, exp_range
+ 2) > 0
2962 || mpz_cmp_si (i
->value
.integer
, -exp_range
- 2) < 0)
2964 gfc_error ("Result of SCALE overflows its kind at %L", &result
->where
);
2965 return &gfc_bad_expr
;
2968 /* Compute scale = radix ** power. */
2969 power
= mpz_get_si (i
->value
.integer
);
2979 gfc_set_model_kind (x
->ts
.kind
);
2982 mpfr_set_ui (radix
, gfc_real_kinds
[k
].radix
, GFC_RND_MODE
);
2983 mpfr_pow_ui (scale
, radix
, power
, GFC_RND_MODE
);
2986 mpfr_div (result
->value
.real
, x
->value
.real
, scale
, GFC_RND_MODE
);
2988 mpfr_mul (result
->value
.real
, x
->value
.real
, scale
, GFC_RND_MODE
);
2993 return range_check (result
, "SCALE");
2998 gfc_simplify_scan (gfc_expr
* e
, gfc_expr
* c
, gfc_expr
* b
)
3003 size_t indx
, len
, lenc
;
3005 if (e
->expr_type
!= EXPR_CONSTANT
|| c
->expr_type
!= EXPR_CONSTANT
)
3008 if (b
!= NULL
&& b
->value
.logical
!= 0)
3013 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
3016 len
= e
->value
.character
.length
;
3017 lenc
= c
->value
.character
.length
;
3019 if (len
== 0 || lenc
== 0)
3028 strcspn (e
->value
.character
.string
, c
->value
.character
.string
) + 1;
3035 for (indx
= len
; indx
> 0; indx
--)
3037 for (i
= 0; i
< lenc
; i
++)
3039 if (c
->value
.character
.string
[i
]
3040 == e
->value
.character
.string
[indx
- 1])
3048 mpz_set_ui (result
->value
.integer
, indx
);
3049 return range_check (result
, "SCAN");
3054 gfc_simplify_selected_int_kind (gfc_expr
* e
)
3059 if (e
->expr_type
!= EXPR_CONSTANT
|| gfc_extract_int (e
, &range
) != NULL
)
3064 for (i
= 0; gfc_integer_kinds
[i
].kind
!= 0; i
++)
3065 if (gfc_integer_kinds
[i
].range
>= range
3066 && gfc_integer_kinds
[i
].kind
< kind
)
3067 kind
= gfc_integer_kinds
[i
].kind
;
3069 if (kind
== INT_MAX
)
3072 result
= gfc_int_expr (kind
);
3073 result
->where
= e
->where
;
3080 gfc_simplify_selected_real_kind (gfc_expr
* p
, gfc_expr
* q
)
3082 int range
, precision
, i
, kind
, found_precision
, found_range
;
3089 if (p
->expr_type
!= EXPR_CONSTANT
3090 || gfc_extract_int (p
, &precision
) != NULL
)
3098 if (q
->expr_type
!= EXPR_CONSTANT
3099 || gfc_extract_int (q
, &range
) != NULL
)
3104 found_precision
= 0;
3107 for (i
= 0; gfc_real_kinds
[i
].kind
!= 0; i
++)
3109 if (gfc_real_kinds
[i
].precision
>= precision
)
3110 found_precision
= 1;
3112 if (gfc_real_kinds
[i
].range
>= range
)
3115 if (gfc_real_kinds
[i
].precision
>= precision
3116 && gfc_real_kinds
[i
].range
>= range
&& gfc_real_kinds
[i
].kind
< kind
)
3117 kind
= gfc_real_kinds
[i
].kind
;
3120 if (kind
== INT_MAX
)
3124 if (!found_precision
)
3130 result
= gfc_int_expr (kind
);
3131 result
->where
= (p
!= NULL
) ? p
->where
: q
->where
;
3138 gfc_simplify_set_exponent (gfc_expr
* x
, gfc_expr
* i
)
3141 mpfr_t exp
, absv
, log2
, pow2
, frac
;
3144 if (x
->expr_type
!= EXPR_CONSTANT
|| i
->expr_type
!= EXPR_CONSTANT
)
3147 result
= gfc_constant_result (BT_REAL
, x
->ts
.kind
, &x
->where
);
3149 gfc_set_model_kind (x
->ts
.kind
);
3151 if (mpfr_sgn (x
->value
.real
) == 0)
3153 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
3163 mpfr_abs (absv
, x
->value
.real
, GFC_RND_MODE
);
3164 mpfr_log2 (log2
, absv
, GFC_RND_MODE
);
3166 mpfr_trunc (log2
, log2
);
3167 mpfr_add_ui (exp
, log2
, 1, GFC_RND_MODE
);
3169 /* Old exponent value, and fraction. */
3170 mpfr_ui_pow (pow2
, 2, exp
, GFC_RND_MODE
);
3172 mpfr_div (frac
, absv
, pow2
, GFC_RND_MODE
);
3175 exp2
= (unsigned long) mpz_get_d (i
->value
.integer
);
3176 mpfr_mul_2exp (result
->value
.real
, frac
, exp2
, GFC_RND_MODE
);
3183 return range_check (result
, "SET_EXPONENT");
3188 gfc_simplify_shape (gfc_expr
* source
)
3190 mpz_t shape
[GFC_MAX_DIMENSIONS
];
3191 gfc_expr
*result
, *e
, *f
;
3196 if (source
->rank
== 0 || source
->expr_type
!= EXPR_VARIABLE
)
3199 result
= gfc_start_constructor (BT_INTEGER
, gfc_default_integer_kind
,
3202 ar
= gfc_find_array_ref (source
);
3204 t
= gfc_array_ref_shape (ar
, shape
);
3206 for (n
= 0; n
< source
->rank
; n
++)
3208 e
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
3213 mpz_set (e
->value
.integer
, shape
[n
]);
3214 mpz_clear (shape
[n
]);
3218 mpz_set_ui (e
->value
.integer
, n
+ 1);
3220 f
= gfc_simplify_size (source
, e
);
3224 gfc_free_expr (result
);
3233 gfc_append_constructor (result
, e
);
3241 gfc_simplify_size (gfc_expr
* array
, gfc_expr
* dim
)
3249 if (gfc_array_size (array
, &size
) == FAILURE
)
3254 if (dim
->expr_type
!= EXPR_CONSTANT
)
3257 d
= mpz_get_ui (dim
->value
.integer
) - 1;
3258 if (gfc_array_dimen_size (array
, d
, &size
) == FAILURE
)
3262 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
3265 mpz_set (result
->value
.integer
, size
);
3272 gfc_simplify_sign (gfc_expr
* x
, gfc_expr
* y
)
3276 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
3279 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3284 mpz_abs (result
->value
.integer
, x
->value
.integer
);
3285 if (mpz_sgn (y
->value
.integer
) < 0)
3286 mpz_neg (result
->value
.integer
, result
->value
.integer
);
3291 /* TODO: Handle -0.0 and +0.0 correctly on machines that support
3293 mpfr_abs (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
3294 if (mpfr_sgn (y
->value
.real
) < 0)
3295 mpfr_neg (result
->value
.real
, result
->value
.real
, GFC_RND_MODE
);
3300 gfc_internal_error ("Bad type in gfc_simplify_sign");
3308 gfc_simplify_sin (gfc_expr
* x
)
3313 if (x
->expr_type
!= EXPR_CONSTANT
)
3316 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3321 mpfr_sin (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
3325 gfc_set_model (x
->value
.real
);
3329 mpfr_sin (xp
, x
->value
.complex.r
, GFC_RND_MODE
);
3330 mpfr_cosh (xq
, x
->value
.complex.i
, GFC_RND_MODE
);
3331 mpfr_mul (result
->value
.complex.r
, xp
, xq
, GFC_RND_MODE
);
3333 mpfr_cos (xp
, x
->value
.complex.r
, GFC_RND_MODE
);
3334 mpfr_sinh (xq
, x
->value
.complex.i
, GFC_RND_MODE
);
3335 mpfr_mul (result
->value
.complex.i
, xp
, xq
, GFC_RND_MODE
);
3342 gfc_internal_error ("in gfc_simplify_sin(): Bad type");
3345 return range_check (result
, "SIN");
3350 gfc_simplify_sinh (gfc_expr
* x
)
3354 if (x
->expr_type
!= EXPR_CONSTANT
)
3357 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3359 mpfr_sinh(result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
3361 return range_check (result
, "SINH");
3365 /* The argument is always a double precision real that is converted to
3366 single precision. TODO: Rounding! */
3369 gfc_simplify_sngl (gfc_expr
* a
)
3373 if (a
->expr_type
!= EXPR_CONSTANT
)
3376 result
= gfc_real2real (a
, gfc_default_real_kind
);
3377 return range_check (result
, "SNGL");
3382 gfc_simplify_spacing (gfc_expr
* x
)
3389 if (x
->expr_type
!= EXPR_CONSTANT
)
3392 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
3394 p
= gfc_real_kinds
[i
].digits
;
3396 result
= gfc_constant_result (BT_REAL
, x
->ts
.kind
, &x
->where
);
3398 gfc_set_model_kind (x
->ts
.kind
);
3400 if (mpfr_sgn (x
->value
.real
) == 0)
3402 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].tiny
, GFC_RND_MODE
);
3409 mpfr_abs (absv
, x
->value
.real
, GFC_RND_MODE
);
3410 mpfr_log2 (log2
, absv
, GFC_RND_MODE
);
3411 mpfr_trunc (log2
, log2
);
3413 mpfr_add_ui (log2
, log2
, 1, GFC_RND_MODE
);
3415 /* FIXME: We should be using mpfr_get_si here, but this function is
3416 not available with the version of mpfr distributed with gmp (as of
3417 2004-09-17). Replace once mpfr has been imported into the gcc cvs
3419 diff
= (long)mpfr_get_d (log2
, GFC_RND_MODE
) - (long)p
;
3420 mpfr_set_ui (result
->value
.real
, 1, GFC_RND_MODE
);
3421 mpfr_mul_2si (result
->value
.real
, result
->value
.real
, diff
, GFC_RND_MODE
);
3426 if (mpfr_cmp (result
->value
.real
, gfc_real_kinds
[i
].tiny
) < 0)
3427 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].tiny
, GFC_RND_MODE
);
3429 return range_check (result
, "SPACING");
3434 gfc_simplify_sqrt (gfc_expr
* e
)
3437 mpfr_t ac
, ad
, s
, t
, w
;
3439 if (e
->expr_type
!= EXPR_CONSTANT
)
3442 result
= gfc_constant_result (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
3447 if (mpfr_cmp_si (e
->value
.real
, 0) < 0)
3449 mpfr_sqrt (result
->value
.real
, e
->value
.real
, GFC_RND_MODE
);
3454 /* Formula taken from Numerical Recipes to avoid over- and
3457 gfc_set_model (e
->value
.real
);
3464 if (mpfr_cmp_ui (e
->value
.complex.r
, 0) == 0
3465 && mpfr_cmp_ui (e
->value
.complex.i
, 0) == 0)
3468 mpfr_set_ui (result
->value
.complex.r
, 0, GFC_RND_MODE
);
3469 mpfr_set_ui (result
->value
.complex.i
, 0, GFC_RND_MODE
);
3473 mpfr_abs (ac
, e
->value
.complex.r
, GFC_RND_MODE
);
3474 mpfr_abs (ad
, e
->value
.complex.i
, GFC_RND_MODE
);
3476 if (mpfr_cmp (ac
, ad
) >= 0)
3478 mpfr_div (t
, e
->value
.complex.i
, e
->value
.complex.r
, GFC_RND_MODE
);
3479 mpfr_mul (t
, t
, t
, GFC_RND_MODE
);
3480 mpfr_add_ui (t
, t
, 1, GFC_RND_MODE
);
3481 mpfr_sqrt (t
, t
, GFC_RND_MODE
);
3482 mpfr_add_ui (t
, t
, 1, GFC_RND_MODE
);
3483 mpfr_div_ui (t
, t
, 2, GFC_RND_MODE
);
3484 mpfr_sqrt (t
, t
, GFC_RND_MODE
);
3485 mpfr_sqrt (s
, ac
, GFC_RND_MODE
);
3486 mpfr_mul (w
, s
, t
, GFC_RND_MODE
);
3490 mpfr_div (s
, e
->value
.complex.r
, e
->value
.complex.i
, GFC_RND_MODE
);
3491 mpfr_mul (t
, s
, s
, GFC_RND_MODE
);
3492 mpfr_add_ui (t
, t
, 1, GFC_RND_MODE
);
3493 mpfr_sqrt (t
, t
, GFC_RND_MODE
);
3494 mpfr_abs (s
, s
, GFC_RND_MODE
);
3495 mpfr_add (t
, t
, s
, GFC_RND_MODE
);
3496 mpfr_div_ui (t
, t
, 2, GFC_RND_MODE
);
3497 mpfr_sqrt (t
, t
, GFC_RND_MODE
);
3498 mpfr_sqrt (s
, ad
, GFC_RND_MODE
);
3499 mpfr_mul (w
, s
, t
, GFC_RND_MODE
);
3502 if (mpfr_cmp_ui (w
, 0) != 0 && mpfr_cmp_ui (e
->value
.complex.r
, 0) >= 0)
3504 mpfr_mul_ui (t
, w
, 2, GFC_RND_MODE
);
3505 mpfr_div (result
->value
.complex.i
, e
->value
.complex.i
, t
, GFC_RND_MODE
);
3506 mpfr_set (result
->value
.complex.r
, w
, GFC_RND_MODE
);
3508 else if (mpfr_cmp_ui (w
, 0) != 0
3509 && mpfr_cmp_ui (e
->value
.complex.r
, 0) < 0
3510 && mpfr_cmp_ui (e
->value
.complex.i
, 0) >= 0)
3512 mpfr_mul_ui (t
, w
, 2, GFC_RND_MODE
);
3513 mpfr_div (result
->value
.complex.r
, e
->value
.complex.i
, t
, GFC_RND_MODE
);
3514 mpfr_set (result
->value
.complex.i
, w
, GFC_RND_MODE
);
3516 else if (mpfr_cmp_ui (w
, 0) != 0
3517 && mpfr_cmp_ui (e
->value
.complex.r
, 0) < 0
3518 && mpfr_cmp_ui (e
->value
.complex.i
, 0) < 0)
3520 mpfr_mul_ui (t
, w
, 2, GFC_RND_MODE
);
3521 mpfr_div (result
->value
.complex.r
, ad
, t
, GFC_RND_MODE
);
3522 mpfr_neg (w
, w
, GFC_RND_MODE
);
3523 mpfr_set (result
->value
.complex.i
, w
, GFC_RND_MODE
);
3526 gfc_internal_error ("invalid complex argument of SQRT at %L",
3538 gfc_internal_error ("invalid argument of SQRT at %L", &e
->where
);
3541 return range_check (result
, "SQRT");
3544 gfc_free_expr (result
);
3545 gfc_error ("Argument of SQRT at %L has a negative value", &e
->where
);
3546 return &gfc_bad_expr
;
3551 gfc_simplify_tan (gfc_expr
* x
)
3556 if (x
->expr_type
!= EXPR_CONSTANT
)
3559 i
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, false);
3561 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3563 mpfr_tan (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
3565 return range_check (result
, "TAN");
3570 gfc_simplify_tanh (gfc_expr
* x
)
3574 if (x
->expr_type
!= EXPR_CONSTANT
)
3577 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3579 mpfr_tanh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
3581 return range_check (result
, "TANH");
3587 gfc_simplify_tiny (gfc_expr
* e
)
3592 i
= gfc_validate_kind (BT_REAL
, e
->ts
.kind
, false);
3594 result
= gfc_constant_result (BT_REAL
, e
->ts
.kind
, &e
->where
);
3595 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].tiny
, GFC_RND_MODE
);
3602 gfc_simplify_trim (gfc_expr
* e
)
3605 int count
, i
, len
, lentrim
;
3607 if (e
->expr_type
!= EXPR_CONSTANT
)
3610 len
= e
->value
.character
.length
;
3612 result
= gfc_constant_result (BT_CHARACTER
, e
->ts
.kind
, &e
->where
);
3614 for (count
= 0, i
= 1; i
<= len
; ++i
)
3616 if (e
->value
.character
.string
[len
- i
] == ' ')
3622 lentrim
= len
- count
;
3624 result
->value
.character
.length
= lentrim
;
3625 result
->value
.character
.string
= gfc_getmem (lentrim
+ 1);
3627 for (i
= 0; i
< lentrim
; i
++)
3628 result
->value
.character
.string
[i
] = e
->value
.character
.string
[i
];
3630 result
->value
.character
.string
[lentrim
] = '\0'; /* For debugger */
3637 gfc_simplify_ubound (gfc_expr
* array
, gfc_expr
* dim
)
3639 return simplify_bound (array
, dim
, 1);
3644 gfc_simplify_verify (gfc_expr
* s
, gfc_expr
* set
, gfc_expr
* b
)
3648 size_t index
, len
, lenset
;
3651 if (s
->expr_type
!= EXPR_CONSTANT
|| set
->expr_type
!= EXPR_CONSTANT
)
3654 if (b
!= NULL
&& b
->value
.logical
!= 0)
3659 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
3662 len
= s
->value
.character
.length
;
3663 lenset
= set
->value
.character
.length
;
3667 mpz_set_ui (result
->value
.integer
, 0);
3675 mpz_set_ui (result
->value
.integer
, len
);
3680 strspn (s
->value
.character
.string
, set
->value
.character
.string
) + 1;
3689 mpz_set_ui (result
->value
.integer
, 1);
3692 for (index
= len
; index
> 0; index
--)
3694 for (i
= 0; i
< lenset
; i
++)
3696 if (s
->value
.character
.string
[index
- 1]
3697 == set
->value
.character
.string
[i
])
3705 mpz_set_ui (result
->value
.integer
, index
);
3709 /****************** Constant simplification *****************/
3711 /* Master function to convert one constant to another. While this is
3712 used as a simplification function, it requires the destination type
3713 and kind information which is supplied by a special case in
3717 gfc_convert_constant (gfc_expr
* e
, bt type
, int kind
)
3719 gfc_expr
*g
, *result
, *(*f
) (gfc_expr
*, int);
3720 gfc_constructor
*head
, *c
, *tail
= NULL
;
3734 f
= gfc_int2complex
;
3754 f
= gfc_real2complex
;
3765 f
= gfc_complex2int
;
3768 f
= gfc_complex2real
;
3771 f
= gfc_complex2complex
;
3797 f
= gfc_hollerith2int
;
3801 f
= gfc_hollerith2real
;
3805 f
= gfc_hollerith2complex
;
3809 f
= gfc_hollerith2character
;
3813 f
= gfc_hollerith2logical
;
3823 gfc_internal_error ("gfc_convert_constant(): Unexpected type");
3828 switch (e
->expr_type
)
3831 result
= f (e
, kind
);
3833 return &gfc_bad_expr
;
3837 if (!gfc_is_constant_expr (e
))
3842 for (c
= e
->value
.constructor
; c
; c
= c
->next
)
3845 head
= tail
= gfc_get_constructor ();
3848 tail
->next
= gfc_get_constructor ();
3852 tail
->where
= c
->where
;
3854 if (c
->iterator
== NULL
)
3855 tail
->expr
= f (c
->expr
, kind
);
3858 g
= gfc_convert_constant (c
->expr
, type
, kind
);
3859 if (g
== &gfc_bad_expr
)
3864 if (tail
->expr
== NULL
)
3866 gfc_free_constructor (head
);
3871 result
= gfc_get_expr ();
3872 result
->ts
.type
= type
;
3873 result
->ts
.kind
= kind
;
3874 result
->expr_type
= EXPR_ARRAY
;
3875 result
->value
.constructor
= head
;
3876 result
->shape
= gfc_copy_shape (e
->shape
, e
->rank
);
3877 result
->where
= e
->where
;
3878 result
->rank
= e
->rank
;
3889 /****************** Helper functions ***********************/
3891 /* Given a collating table, create the inverse table. */
3894 invert_table (const int *table
, int *xtable
)
3898 for (i
= 0; i
< 256; i
++)
3901 for (i
= 0; i
< 256; i
++)
3902 xtable
[table
[i
]] = i
;
3907 gfc_simplify_init_1 (void)
3910 invert_table (ascii_table
, xascii_table
);