1 /* Simplify intrinsic functions at compile-time.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
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"
29 #include "target-memory.h"
31 gfc_expr gfc_bad_expr;
34 /* Note that 'simplification' is not just transforming expressions.
35 For functions that are not simplified at compile time, range
36 checking is done if possible.
38 The return convention is that each simplification function returns:
40 A new expression node corresponding to the simplified arguments.
41 The original arguments are destroyed by the caller, and must not
42 be a part of the new expression.
44 NULL pointer indicating that no simplification was possible and
45 the original expression should remain intact. If the
46 simplification function sets the type and/or the function name
47 via the pointer gfc_simple_expression, then this type is
50 An expression pointer to gfc_bad_expr (a static placeholder)
51 indicating that some error has prevented simplification. For
52 example, sqrt(-1.0). The error is generated within the function
53 and should be propagated upwards
55 By the time a simplification function gets control, it has been
56 decided that the function call is really supposed to be the
57 intrinsic. No type checking is strictly necessary, since only
58 valid types will be passed on. On the other hand, a simplification
59 subroutine may have to look at the type of an argument as part of
62 Array arguments are never passed to these subroutines.
64 The functions in this file don't have much comment with them, but
65 everything is reasonably straight-forward. The Standard, chapter 13
66 is the best comment you'll find for this file anyway. */
68 /* Range checks an expression node. If all goes well, returns the
69 node, otherwise returns &gfc_bad_expr and frees the node. */
72 range_check (gfc_expr *result, const char *name)
74 switch (gfc_range_check (result))
80 gfc_error ("Result of %s overflows its kind at %L", name,
85 gfc_error ("Result of %s underflows its kind at %L", name,
90 gfc_error ("Result of %s is NaN at %L", name, &result->where);
94 gfc_error ("Result of %s gives range error for its kind at %L", name,
99 gfc_free_expr (result);
100 return &gfc_bad_expr;
104 /* A helper function that gets an optional and possibly missing
105 kind parameter. Returns the kind, -1 if something went wrong. */
108 get_kind (bt type, gfc_expr *k, const char *name, int default_kind)
115 if (k->expr_type != EXPR_CONSTANT)
117 gfc_error ("KIND parameter of %s at %L must be an initialization "
118 "expression", name, &k->where);
123 if (gfc_extract_int (k, &kind) != NULL
124 || gfc_validate_kind (type, kind, true) < 0)
127 gfc_error ("Invalid KIND parameter of %s at %L", name, &k->where);
135 /* Converts an mpz_t signed variable into an unsigned one, assuming
136 two's complement representations and a binary width of bitsize.
137 The conversion is a no-op unless x is negative; otherwise, it can
138 be accomplished by masking out the high bits. */
141 convert_mpz_to_unsigned (mpz_t x, int bitsize)
147 /* Confirm that no bits above the signed range are unset. */
148 gcc_assert (mpz_scan0 (x, bitsize-1) == ULONG_MAX);
150 mpz_init_set_ui (mask, 1);
151 mpz_mul_2exp (mask, mask, bitsize);
152 mpz_sub_ui (mask, mask, 1);
154 mpz_and (x, x, mask);
160 /* Confirm that no bits above the signed range are set. */
161 gcc_assert (mpz_scan1 (x, bitsize-1) == ULONG_MAX);
166 /* Converts an mpz_t unsigned variable into a signed one, assuming
167 two's complement representations and a binary width of bitsize.
168 If the bitsize-1 bit is set, this is taken as a sign bit and
169 the number is converted to the corresponding negative number. */
172 convert_mpz_to_signed (mpz_t x, int bitsize)
176 /* Confirm that no bits above the unsigned range are set. */
177 gcc_assert (mpz_scan1 (x, bitsize) == ULONG_MAX);
179 if (mpz_tstbit (x, bitsize - 1) == 1)
181 mpz_init_set_ui (mask, 1);
182 mpz_mul_2exp (mask, mask, bitsize);
183 mpz_sub_ui (mask, mask, 1);
185 /* We negate the number by hand, zeroing the high bits, that is
186 make it the corresponding positive number, and then have it
187 negated by GMP, giving the correct representation of the
190 mpz_add_ui (x, x, 1);
191 mpz_and (x, x, mask);
200 /********************** Simplification functions *****************************/
203 gfc_simplify_abs (gfc_expr *e)
207 if (e->expr_type != EXPR_CONSTANT)
213 result = gfc_constant_result (BT_INTEGER, e->ts.kind, &e->where);
215 mpz_abs (result->value.integer, e->value.integer);
217 result = range_check (result, "IABS");
221 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
223 mpfr_abs (result->value.real, e->value.real, GFC_RND_MODE);
225 result = range_check (result, "ABS");
229 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
231 gfc_set_model_kind (e->ts.kind);
233 mpfr_hypot (result->value.real, e->value.complex.r,
234 e->value.complex.i, GFC_RND_MODE);
235 result = range_check (result, "CABS");
239 gfc_internal_error ("gfc_simplify_abs(): Bad type");
245 /* We use the processor's collating sequence, because all
246 systems that gfortran currently works on are ASCII. */
249 gfc_simplify_achar (gfc_expr *e)
255 if (e->expr_type != EXPR_CONSTANT)
258 ch = gfc_extract_int (e, &c);
261 gfc_internal_error ("gfc_simplify_achar: %s", ch);
263 if (gfc_option.warn_surprising && (c < 0 || c > 127))
264 gfc_warning ("Argument of ACHAR function at %L outside of range [0,127]",
267 result = gfc_constant_result (BT_CHARACTER, gfc_default_character_kind,
270 result->value.character.string = gfc_getmem (2);
272 result->value.character.length = 1;
273 result->value.character.string[0] = c;
274 result->value.character.string[1] = '\0'; /* For debugger */
280 gfc_simplify_acos (gfc_expr *x)
284 if (x->expr_type != EXPR_CONSTANT)
287 if (mpfr_cmp_si (x->value.real, 1) > 0
288 || mpfr_cmp_si (x->value.real, -1) < 0)
290 gfc_error ("Argument of ACOS at %L must be between -1 and 1",
292 return &gfc_bad_expr;
295 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
297 mpfr_acos (result->value.real, x->value.real, GFC_RND_MODE);
299 return range_check (result, "ACOS");
303 gfc_simplify_acosh (gfc_expr *x)
307 if (x->expr_type != EXPR_CONSTANT)
310 if (mpfr_cmp_si (x->value.real, 1) < 0)
312 gfc_error ("Argument of ACOSH at %L must not be less than 1",
314 return &gfc_bad_expr;
317 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
319 mpfr_acosh (result->value.real, x->value.real, GFC_RND_MODE);
321 return range_check (result, "ACOSH");
325 gfc_simplify_adjustl (gfc_expr *e)
331 if (e->expr_type != EXPR_CONSTANT)
334 len = e->value.character.length;
336 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
338 result->value.character.length = len;
339 result->value.character.string = gfc_getmem (len + 1);
341 for (count = 0, i = 0; i < len; ++i)
343 ch = e->value.character.string[i];
349 for (i = 0; i < len - count; ++i)
350 result->value.character.string[i] = e->value.character.string[count + i];
352 for (i = len - count; i < len; ++i)
353 result->value.character.string[i] = ' ';
355 result->value.character.string[len] = '\0'; /* For debugger */
362 gfc_simplify_adjustr (gfc_expr *e)
368 if (e->expr_type != EXPR_CONSTANT)
371 len = e->value.character.length;
373 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
375 result->value.character.length = len;
376 result->value.character.string = gfc_getmem (len + 1);
378 for (count = 0, i = len - 1; i >= 0; --i)
380 ch = e->value.character.string[i];
386 for (i = 0; i < count; ++i)
387 result->value.character.string[i] = ' ';
389 for (i = count; i < len; ++i)
390 result->value.character.string[i] = e->value.character.string[i - count];
392 result->value.character.string[len] = '\0'; /* For debugger */
399 gfc_simplify_aimag (gfc_expr *e)
403 if (e->expr_type != EXPR_CONSTANT)
406 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
407 mpfr_set (result->value.real, e->value.complex.i, GFC_RND_MODE);
409 return range_check (result, "AIMAG");
414 gfc_simplify_aint (gfc_expr *e, gfc_expr *k)
416 gfc_expr *rtrunc, *result;
419 kind = get_kind (BT_REAL, k, "AINT", e->ts.kind);
421 return &gfc_bad_expr;
423 if (e->expr_type != EXPR_CONSTANT)
426 rtrunc = gfc_copy_expr (e);
428 mpfr_trunc (rtrunc->value.real, e->value.real);
430 result = gfc_real2real (rtrunc, kind);
431 gfc_free_expr (rtrunc);
433 return range_check (result, "AINT");
438 gfc_simplify_dint (gfc_expr *e)
440 gfc_expr *rtrunc, *result;
442 if (e->expr_type != EXPR_CONSTANT)
445 rtrunc = gfc_copy_expr (e);
447 mpfr_trunc (rtrunc->value.real, e->value.real);
449 result = gfc_real2real (rtrunc, gfc_default_double_kind);
450 gfc_free_expr (rtrunc);
452 return range_check (result, "DINT");
457 gfc_simplify_anint (gfc_expr *e, gfc_expr *k)
462 kind = get_kind (BT_REAL, k, "ANINT", e->ts.kind);
464 return &gfc_bad_expr;
466 if (e->expr_type != EXPR_CONSTANT)
469 result = gfc_constant_result (e->ts.type, kind, &e->where);
471 mpfr_round (result->value.real, e->value.real);
473 return range_check (result, "ANINT");
478 gfc_simplify_and (gfc_expr *x, gfc_expr *y)
483 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
486 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
487 if (x->ts.type == BT_INTEGER)
489 result = gfc_constant_result (BT_INTEGER, kind, &x->where);
490 mpz_and (result->value.integer, x->value.integer, y->value.integer);
492 else /* BT_LOGICAL */
494 result = gfc_constant_result (BT_LOGICAL, kind, &x->where);
495 result->value.logical = x->value.logical && y->value.logical;
498 return range_check (result, "AND");
503 gfc_simplify_dnint (gfc_expr *e)
507 if (e->expr_type != EXPR_CONSTANT)
510 result = gfc_constant_result (BT_REAL, gfc_default_double_kind, &e->where);
512 mpfr_round (result->value.real, e->value.real);
514 return range_check (result, "DNINT");
519 gfc_simplify_asin (gfc_expr *x)
523 if (x->expr_type != EXPR_CONSTANT)
526 if (mpfr_cmp_si (x->value.real, 1) > 0
527 || mpfr_cmp_si (x->value.real, -1) < 0)
529 gfc_error ("Argument of ASIN at %L must be between -1 and 1",
531 return &gfc_bad_expr;
534 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
536 mpfr_asin (result->value.real, x->value.real, GFC_RND_MODE);
538 return range_check (result, "ASIN");
543 gfc_simplify_asinh (gfc_expr *x)
547 if (x->expr_type != EXPR_CONSTANT)
550 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
552 mpfr_asinh (result->value.real, x->value.real, GFC_RND_MODE);
554 return range_check (result, "ASINH");
559 gfc_simplify_atan (gfc_expr *x)
563 if (x->expr_type != EXPR_CONSTANT)
566 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
568 mpfr_atan (result->value.real, x->value.real, GFC_RND_MODE);
570 return range_check (result, "ATAN");
575 gfc_simplify_atanh (gfc_expr *x)
579 if (x->expr_type != EXPR_CONSTANT)
582 if (mpfr_cmp_si (x->value.real, 1) >= 0
583 || mpfr_cmp_si (x->value.real, -1) <= 0)
585 gfc_error ("Argument of ATANH at %L must be inside the range -1 to 1",
587 return &gfc_bad_expr;
590 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
592 mpfr_atanh (result->value.real, x->value.real, GFC_RND_MODE);
594 return range_check (result, "ATANH");
599 gfc_simplify_atan2 (gfc_expr *y, gfc_expr *x)
603 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
606 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
608 if (mpfr_sgn (y->value.real) == 0 && mpfr_sgn (x->value.real) == 0)
610 gfc_error ("If first argument of ATAN2 %L is zero, then the "
611 "second argument must not be zero", &x->where);
612 gfc_free_expr (result);
613 return &gfc_bad_expr;
616 mpfr_atan2 (result->value.real, y->value.real, x->value.real, GFC_RND_MODE);
618 return range_check (result, "ATAN2");
623 gfc_simplify_bit_size (gfc_expr *e)
628 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
629 result = gfc_constant_result (BT_INTEGER, e->ts.kind, &e->where);
630 mpz_set_ui (result->value.integer, gfc_integer_kinds[i].bit_size);
637 gfc_simplify_btest (gfc_expr *e, gfc_expr *bit)
641 if (e->expr_type != EXPR_CONSTANT || bit->expr_type != EXPR_CONSTANT)
644 if (gfc_extract_int (bit, &b) != NULL || b < 0)
645 return gfc_logical_expr (0, &e->where);
647 return gfc_logical_expr (mpz_tstbit (e->value.integer, b), &e->where);
652 gfc_simplify_ceiling (gfc_expr *e, gfc_expr *k)
654 gfc_expr *ceil, *result;
657 kind = get_kind (BT_INTEGER, k, "CEILING", gfc_default_integer_kind);
659 return &gfc_bad_expr;
661 if (e->expr_type != EXPR_CONSTANT)
664 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
666 ceil = gfc_copy_expr (e);
668 mpfr_ceil (ceil->value.real, e->value.real);
669 gfc_mpfr_to_mpz (result->value.integer, ceil->value.real);
671 gfc_free_expr (ceil);
673 return range_check (result, "CEILING");
678 gfc_simplify_char (gfc_expr *e, gfc_expr *k)
684 kind = get_kind (BT_CHARACTER, k, "CHAR", gfc_default_character_kind);
686 return &gfc_bad_expr;
688 if (e->expr_type != EXPR_CONSTANT)
691 ch = gfc_extract_int (e, &c);
694 gfc_internal_error ("gfc_simplify_char: %s", ch);
696 if (c < 0 || c > UCHAR_MAX)
697 gfc_error ("Argument of CHAR function at %L outside of range [0,255]",
700 result = gfc_constant_result (BT_CHARACTER, kind, &e->where);
702 result->value.character.length = 1;
703 result->value.character.string = gfc_getmem (2);
705 result->value.character.string[0] = c;
706 result->value.character.string[1] = '\0'; /* For debugger */
712 /* Common subroutine for simplifying CMPLX and DCMPLX. */
715 simplify_cmplx (const char *name, gfc_expr *x, gfc_expr *y, int kind)
719 result = gfc_constant_result (BT_COMPLEX, kind, &x->where);
721 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
726 mpfr_set_z (result->value.complex.r, x->value.integer, GFC_RND_MODE);
730 mpfr_set (result->value.complex.r, x->value.real, GFC_RND_MODE);
734 mpfr_set (result->value.complex.r, x->value.complex.r, GFC_RND_MODE);
735 mpfr_set (result->value.complex.i, x->value.complex.i, GFC_RND_MODE);
739 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (x)");
747 mpfr_set_z (result->value.complex.i, y->value.integer, GFC_RND_MODE);
751 mpfr_set (result->value.complex.i, y->value.real, GFC_RND_MODE);
755 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (y)");
759 return range_check (result, name);
764 gfc_simplify_cmplx (gfc_expr *x, gfc_expr *y, gfc_expr *k)
768 if (x->expr_type != EXPR_CONSTANT
769 || (y != NULL && y->expr_type != EXPR_CONSTANT))
772 kind = get_kind (BT_REAL, k, "CMPLX", gfc_default_real_kind);
774 return &gfc_bad_expr;
776 return simplify_cmplx ("CMPLX", x, y, kind);
781 gfc_simplify_complex (gfc_expr *x, gfc_expr *y)
785 if (x->expr_type != EXPR_CONSTANT
786 || (y != NULL && y->expr_type != EXPR_CONSTANT))
789 if (x->ts.type == BT_INTEGER)
791 if (y->ts.type == BT_INTEGER)
792 kind = gfc_default_real_kind;
798 if (y->ts.type == BT_REAL)
799 kind = (x->ts.kind > y->ts.kind) ? x->ts.kind : y->ts.kind;
804 return simplify_cmplx ("COMPLEX", x, y, kind);
809 gfc_simplify_conjg (gfc_expr *e)
813 if (e->expr_type != EXPR_CONSTANT)
816 result = gfc_copy_expr (e);
817 mpfr_neg (result->value.complex.i, result->value.complex.i, GFC_RND_MODE);
819 return range_check (result, "CONJG");
824 gfc_simplify_cos (gfc_expr *x)
829 if (x->expr_type != EXPR_CONSTANT)
832 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
837 mpfr_cos (result->value.real, x->value.real, GFC_RND_MODE);
840 gfc_set_model_kind (x->ts.kind);
844 mpfr_cos (xp, x->value.complex.r, GFC_RND_MODE);
845 mpfr_cosh (xq, x->value.complex.i, GFC_RND_MODE);
846 mpfr_mul (result->value.complex.r, xp, xq, GFC_RND_MODE);
848 mpfr_sin (xp, x->value.complex.r, GFC_RND_MODE);
849 mpfr_sinh (xq, x->value.complex.i, GFC_RND_MODE);
850 mpfr_mul (xp, xp, xq, GFC_RND_MODE);
851 mpfr_neg (result->value.complex.i, xp, GFC_RND_MODE );
857 gfc_internal_error ("in gfc_simplify_cos(): Bad type");
860 return range_check (result, "COS");
866 gfc_simplify_cosh (gfc_expr *x)
870 if (x->expr_type != EXPR_CONSTANT)
873 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
875 mpfr_cosh (result->value.real, x->value.real, GFC_RND_MODE);
877 return range_check (result, "COSH");
882 gfc_simplify_dcmplx (gfc_expr *x, gfc_expr *y)
885 if (x->expr_type != EXPR_CONSTANT
886 || (y != NULL && y->expr_type != EXPR_CONSTANT))
889 return simplify_cmplx ("DCMPLX", x, y, gfc_default_double_kind);
894 gfc_simplify_dble (gfc_expr *e)
898 if (e->expr_type != EXPR_CONSTANT)
904 result = gfc_int2real (e, gfc_default_double_kind);
908 result = gfc_real2real (e, gfc_default_double_kind);
912 result = gfc_complex2real (e, gfc_default_double_kind);
916 gfc_internal_error ("gfc_simplify_dble(): bad type at %L", &e->where);
919 return range_check (result, "DBLE");
924 gfc_simplify_digits (gfc_expr *x)
928 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
932 digits = gfc_integer_kinds[i].digits;
937 digits = gfc_real_kinds[i].digits;
944 return gfc_int_expr (digits);
949 gfc_simplify_dim (gfc_expr *x, gfc_expr *y)
954 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
957 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
958 result = gfc_constant_result (x->ts.type, kind, &x->where);
963 if (mpz_cmp (x->value.integer, y->value.integer) > 0)
964 mpz_sub (result->value.integer, x->value.integer, y->value.integer);
966 mpz_set_ui (result->value.integer, 0);
971 if (mpfr_cmp (x->value.real, y->value.real) > 0)
972 mpfr_sub (result->value.real, x->value.real, y->value.real,
975 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
980 gfc_internal_error ("gfc_simplify_dim(): Bad type");
983 return range_check (result, "DIM");
988 gfc_simplify_dprod (gfc_expr *x, gfc_expr *y)
990 gfc_expr *a1, *a2, *result;
992 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
995 result = gfc_constant_result (BT_REAL, gfc_default_double_kind, &x->where);
997 a1 = gfc_real2real (x, gfc_default_double_kind);
998 a2 = gfc_real2real (y, gfc_default_double_kind);
1000 mpfr_mul (result->value.real, a1->value.real, a2->value.real, GFC_RND_MODE);
1005 return range_check (result, "DPROD");
1010 gfc_simplify_epsilon (gfc_expr *e)
1015 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
1017 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
1019 mpfr_set (result->value.real, gfc_real_kinds[i].epsilon, GFC_RND_MODE);
1021 return range_check (result, "EPSILON");
1026 gfc_simplify_exp (gfc_expr *x)
1031 if (x->expr_type != EXPR_CONSTANT)
1034 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1039 mpfr_exp (result->value.real, x->value.real, GFC_RND_MODE);
1043 gfc_set_model_kind (x->ts.kind);
1046 mpfr_exp (xq, x->value.complex.r, GFC_RND_MODE);
1047 mpfr_cos (xp, x->value.complex.i, GFC_RND_MODE);
1048 mpfr_mul (result->value.complex.r, xq, xp, GFC_RND_MODE);
1049 mpfr_sin (xp, x->value.complex.i, GFC_RND_MODE);
1050 mpfr_mul (result->value.complex.i, xq, xp, GFC_RND_MODE);
1056 gfc_internal_error ("in gfc_simplify_exp(): Bad type");
1059 return range_check (result, "EXP");
1063 gfc_simplify_exponent (gfc_expr *x)
1068 if (x->expr_type != EXPR_CONSTANT)
1071 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
1074 gfc_set_model (x->value.real);
1076 if (mpfr_sgn (x->value.real) == 0)
1078 mpz_set_ui (result->value.integer, 0);
1082 i = (int) mpfr_get_exp (x->value.real);
1083 mpz_set_si (result->value.integer, i);
1085 return range_check (result, "EXPONENT");
1090 gfc_simplify_float (gfc_expr *a)
1094 if (a->expr_type != EXPR_CONSTANT)
1097 result = gfc_int2real (a, gfc_default_real_kind);
1098 return range_check (result, "FLOAT");
1103 gfc_simplify_floor (gfc_expr *e, gfc_expr *k)
1109 kind = get_kind (BT_INTEGER, k, "FLOOR", gfc_default_integer_kind);
1111 gfc_internal_error ("gfc_simplify_floor(): Bad kind");
1113 if (e->expr_type != EXPR_CONSTANT)
1116 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
1118 gfc_set_model_kind (kind);
1120 mpfr_floor (floor, e->value.real);
1122 gfc_mpfr_to_mpz (result->value.integer, floor);
1126 return range_check (result, "FLOOR");
1131 gfc_simplify_fraction (gfc_expr *x)
1134 mpfr_t absv, exp, pow2;
1136 if (x->expr_type != EXPR_CONSTANT)
1139 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
1141 gfc_set_model_kind (x->ts.kind);
1143 if (mpfr_sgn (x->value.real) == 0)
1145 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
1153 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
1154 mpfr_log2 (exp, absv, GFC_RND_MODE);
1156 mpfr_trunc (exp, exp);
1157 mpfr_add_ui (exp, exp, 1, GFC_RND_MODE);
1159 mpfr_ui_pow (pow2, 2, exp, GFC_RND_MODE);
1161 mpfr_div (result->value.real, absv, pow2, GFC_RND_MODE);
1167 return range_check (result, "FRACTION");
1172 gfc_simplify_huge (gfc_expr *e)
1177 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
1179 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
1184 mpz_set (result->value.integer, gfc_integer_kinds[i].huge);
1188 mpfr_set (result->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
1198 /* We use the processor's collating sequence, because all
1199 systems that gfortran currently works on are ASCII. */
1202 gfc_simplify_iachar (gfc_expr *e)
1207 if (e->expr_type != EXPR_CONSTANT)
1210 if (e->value.character.length != 1)
1212 gfc_error ("Argument of IACHAR at %L must be of length one", &e->where);
1213 return &gfc_bad_expr;
1216 index = (unsigned char) e->value.character.string[0];
1218 if (gfc_option.warn_surprising && index > 127)
1219 gfc_warning ("Argument of IACHAR function at %L outside of range 0..127",
1222 result = gfc_int_expr (index);
1223 result->where = e->where;
1225 return range_check (result, "IACHAR");
1230 gfc_simplify_iand (gfc_expr *x, gfc_expr *y)
1234 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1237 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
1239 mpz_and (result->value.integer, x->value.integer, y->value.integer);
1241 return range_check (result, "IAND");
1246 gfc_simplify_ibclr (gfc_expr *x, gfc_expr *y)
1251 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1254 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
1256 gfc_error ("Invalid second argument of IBCLR at %L", &y->where);
1257 return &gfc_bad_expr;
1260 k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
1262 if (pos >= gfc_integer_kinds[k].bit_size)
1264 gfc_error ("Second argument of IBCLR exceeds bit size at %L",
1266 return &gfc_bad_expr;
1269 result = gfc_copy_expr (x);
1271 convert_mpz_to_unsigned (result->value.integer,
1272 gfc_integer_kinds[k].bit_size);
1274 mpz_clrbit (result->value.integer, pos);
1276 convert_mpz_to_signed (result->value.integer,
1277 gfc_integer_kinds[k].bit_size);
1279 return range_check (result, "IBCLR");
1284 gfc_simplify_ibits (gfc_expr *x, gfc_expr *y, gfc_expr *z)
1291 if (x->expr_type != EXPR_CONSTANT
1292 || y->expr_type != EXPR_CONSTANT
1293 || z->expr_type != EXPR_CONSTANT)
1296 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
1298 gfc_error ("Invalid second argument of IBITS at %L", &y->where);
1299 return &gfc_bad_expr;
1302 if (gfc_extract_int (z, &len) != NULL || len < 0)
1304 gfc_error ("Invalid third argument of IBITS at %L", &z->where);
1305 return &gfc_bad_expr;
1308 k = gfc_validate_kind (BT_INTEGER, x->ts.kind, false);
1310 bitsize = gfc_integer_kinds[k].bit_size;
1312 if (pos + len > bitsize)
1314 gfc_error ("Sum of second and third arguments of IBITS exceeds "
1315 "bit size at %L", &y->where);
1316 return &gfc_bad_expr;
1319 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1321 bits = gfc_getmem (bitsize * sizeof (int));
1323 for (i = 0; i < bitsize; i++)
1326 for (i = 0; i < len; i++)
1327 bits[i] = mpz_tstbit (x->value.integer, i + pos);
1329 for (i = 0; i < bitsize; i++)
1332 mpz_clrbit (result->value.integer, i);
1333 else if (bits[i] == 1)
1334 mpz_setbit (result->value.integer, i);
1336 gfc_internal_error ("IBITS: Bad bit");
1341 return range_check (result, "IBITS");
1346 gfc_simplify_ibset (gfc_expr *x, gfc_expr *y)
1351 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1354 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
1356 gfc_error ("Invalid second argument of IBSET at %L", &y->where);
1357 return &gfc_bad_expr;
1360 k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
1362 if (pos >= gfc_integer_kinds[k].bit_size)
1364 gfc_error ("Second argument of IBSET exceeds bit size at %L",
1366 return &gfc_bad_expr;
1369 result = gfc_copy_expr (x);
1371 convert_mpz_to_unsigned (result->value.integer,
1372 gfc_integer_kinds[k].bit_size);
1374 mpz_setbit (result->value.integer, pos);
1376 convert_mpz_to_signed (result->value.integer,
1377 gfc_integer_kinds[k].bit_size);
1379 return range_check (result, "IBSET");
1384 gfc_simplify_ichar (gfc_expr *e)
1389 if (e->expr_type != EXPR_CONSTANT)
1392 if (e->value.character.length != 1)
1394 gfc_error ("Argument of ICHAR at %L must be of length one", &e->where);
1395 return &gfc_bad_expr;
1398 index = (unsigned char) e->value.character.string[0];
1400 if (index < 0 || index > UCHAR_MAX)
1401 gfc_internal_error("Argument of ICHAR at %L out of range", &e->where);
1403 result = gfc_int_expr (index);
1404 result->where = e->where;
1405 return range_check (result, "ICHAR");
1410 gfc_simplify_ieor (gfc_expr *x, gfc_expr *y)
1414 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1417 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
1419 mpz_xor (result->value.integer, x->value.integer, y->value.integer);
1421 return range_check (result, "IEOR");
1426 gfc_simplify_index (gfc_expr *x, gfc_expr *y, gfc_expr *b)
1429 int back, len, lensub;
1430 int i, j, k, count, index = 0, start;
1432 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1435 if (b != NULL && b->value.logical != 0)
1440 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
1443 len = x->value.character.length;
1444 lensub = y->value.character.length;
1448 mpz_set_si (result->value.integer, 0);
1456 mpz_set_si (result->value.integer, 1);
1459 else if (lensub == 1)
1461 for (i = 0; i < len; i++)
1463 for (j = 0; j < lensub; j++)
1465 if (y->value.character.string[j]
1466 == x->value.character.string[i])
1476 for (i = 0; i < len; i++)
1478 for (j = 0; j < lensub; j++)
1480 if (y->value.character.string[j]
1481 == x->value.character.string[i])
1486 for (k = 0; k < lensub; k++)
1488 if (y->value.character.string[k]
1489 == x->value.character.string[k + start])
1493 if (count == lensub)
1508 mpz_set_si (result->value.integer, len + 1);
1511 else if (lensub == 1)
1513 for (i = 0; i < len; i++)
1515 for (j = 0; j < lensub; j++)
1517 if (y->value.character.string[j]
1518 == x->value.character.string[len - i])
1520 index = len - i + 1;
1528 for (i = 0; i < len; i++)
1530 for (j = 0; j < lensub; j++)
1532 if (y->value.character.string[j]
1533 == x->value.character.string[len - i])
1536 if (start <= len - lensub)
1539 for (k = 0; k < lensub; k++)
1540 if (y->value.character.string[k]
1541 == x->value.character.string[k + start])
1544 if (count == lensub)
1561 mpz_set_si (result->value.integer, index);
1562 return range_check (result, "INDEX");
1567 gfc_simplify_int (gfc_expr *e, gfc_expr *k)
1569 gfc_expr *rpart, *rtrunc, *result;
1572 kind = get_kind (BT_INTEGER, k, "INT", gfc_default_integer_kind);
1574 return &gfc_bad_expr;
1576 if (e->expr_type != EXPR_CONSTANT)
1579 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
1584 mpz_set (result->value.integer, e->value.integer);
1588 rtrunc = gfc_copy_expr (e);
1589 mpfr_trunc (rtrunc->value.real, e->value.real);
1590 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1591 gfc_free_expr (rtrunc);
1595 rpart = gfc_complex2real (e, kind);
1596 rtrunc = gfc_copy_expr (rpart);
1597 mpfr_trunc (rtrunc->value.real, rpart->value.real);
1598 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1599 gfc_free_expr (rpart);
1600 gfc_free_expr (rtrunc);
1604 gfc_error ("Argument of INT at %L is not a valid type", &e->where);
1605 gfc_free_expr (result);
1606 return &gfc_bad_expr;
1609 return range_check (result, "INT");
1614 gfc_simplify_intconv (gfc_expr *e, int kind, const char *name)
1616 gfc_expr *rpart, *rtrunc, *result;
1618 if (e->expr_type != EXPR_CONSTANT)
1621 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
1626 mpz_set (result->value.integer, e->value.integer);
1630 rtrunc = gfc_copy_expr (e);
1631 mpfr_trunc (rtrunc->value.real, e->value.real);
1632 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1633 gfc_free_expr (rtrunc);
1637 rpart = gfc_complex2real (e, kind);
1638 rtrunc = gfc_copy_expr (rpart);
1639 mpfr_trunc (rtrunc->value.real, rpart->value.real);
1640 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1641 gfc_free_expr (rpart);
1642 gfc_free_expr (rtrunc);
1646 gfc_error ("Argument of %s at %L is not a valid type", name, &e->where);
1647 gfc_free_expr (result);
1648 return &gfc_bad_expr;
1651 return range_check (result, name);
1656 gfc_simplify_int2 (gfc_expr *e)
1658 return gfc_simplify_intconv (e, 2, "INT2");
1663 gfc_simplify_int8 (gfc_expr *e)
1665 return gfc_simplify_intconv (e, 8, "INT8");
1670 gfc_simplify_long (gfc_expr *e)
1672 return gfc_simplify_intconv (e, 4, "LONG");
1677 gfc_simplify_ifix (gfc_expr *e)
1679 gfc_expr *rtrunc, *result;
1681 if (e->expr_type != EXPR_CONSTANT)
1684 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
1687 rtrunc = gfc_copy_expr (e);
1689 mpfr_trunc (rtrunc->value.real, e->value.real);
1690 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1692 gfc_free_expr (rtrunc);
1693 return range_check (result, "IFIX");
1698 gfc_simplify_idint (gfc_expr *e)
1700 gfc_expr *rtrunc, *result;
1702 if (e->expr_type != EXPR_CONSTANT)
1705 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
1708 rtrunc = gfc_copy_expr (e);
1710 mpfr_trunc (rtrunc->value.real, e->value.real);
1711 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1713 gfc_free_expr (rtrunc);
1714 return range_check (result, "IDINT");
1719 gfc_simplify_ior (gfc_expr *x, gfc_expr *y)
1723 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1726 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
1728 mpz_ior (result->value.integer, x->value.integer, y->value.integer);
1729 return range_check (result, "IOR");
1734 gfc_simplify_ishft (gfc_expr *e, gfc_expr *s)
1737 int shift, ashift, isize, k, *bits, i;
1739 if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
1742 if (gfc_extract_int (s, &shift) != NULL)
1744 gfc_error ("Invalid second argument of ISHFT at %L", &s->where);
1745 return &gfc_bad_expr;
1748 k = gfc_validate_kind (BT_INTEGER, e->ts.kind, false);
1750 isize = gfc_integer_kinds[k].bit_size;
1759 gfc_error ("Magnitude of second argument of ISHFT exceeds bit size "
1760 "at %L", &s->where);
1761 return &gfc_bad_expr;
1764 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
1768 mpz_set (result->value.integer, e->value.integer);
1769 return range_check (result, "ISHFT");
1772 bits = gfc_getmem (isize * sizeof (int));
1774 for (i = 0; i < isize; i++)
1775 bits[i] = mpz_tstbit (e->value.integer, i);
1779 for (i = 0; i < shift; i++)
1780 mpz_clrbit (result->value.integer, i);
1782 for (i = 0; i < isize - shift; i++)
1785 mpz_clrbit (result->value.integer, i + shift);
1787 mpz_setbit (result->value.integer, i + shift);
1792 for (i = isize - 1; i >= isize - ashift; i--)
1793 mpz_clrbit (result->value.integer, i);
1795 for (i = isize - 1; i >= ashift; i--)
1798 mpz_clrbit (result->value.integer, i - ashift);
1800 mpz_setbit (result->value.integer, i - ashift);
1804 convert_mpz_to_signed (result->value.integer, isize);
1812 gfc_simplify_ishftc (gfc_expr *e, gfc_expr *s, gfc_expr *sz)
1815 int shift, ashift, isize, ssize, delta, k;
1818 if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
1821 if (gfc_extract_int (s, &shift) != NULL)
1823 gfc_error ("Invalid second argument of ISHFTC at %L", &s->where);
1824 return &gfc_bad_expr;
1827 k = gfc_validate_kind (e->ts.type, e->ts.kind, false);
1828 isize = gfc_integer_kinds[k].bit_size;
1832 if (sz->expr_type != EXPR_CONSTANT)
1835 if (gfc_extract_int (sz, &ssize) != NULL || ssize <= 0)
1837 gfc_error ("Invalid third argument of ISHFTC at %L", &sz->where);
1838 return &gfc_bad_expr;
1843 gfc_error ("Magnitude of third argument of ISHFTC exceeds "
1844 "BIT_SIZE of first argument at %L", &s->where);
1845 return &gfc_bad_expr;
1859 gfc_error ("Magnitude of second argument of ISHFTC exceeds "
1860 "third argument at %L", &s->where);
1862 gfc_error ("Magnitude of second argument of ISHFTC exceeds "
1863 "BIT_SIZE of first argument at %L", &s->where);
1864 return &gfc_bad_expr;
1867 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
1869 mpz_set (result->value.integer, e->value.integer);
1874 convert_mpz_to_unsigned (result->value.integer, isize);
1876 bits = gfc_getmem (ssize * sizeof (int));
1878 for (i = 0; i < ssize; i++)
1879 bits[i] = mpz_tstbit (e->value.integer, i);
1881 delta = ssize - ashift;
1885 for (i = 0; i < delta; i++)
1888 mpz_clrbit (result->value.integer, i + shift);
1890 mpz_setbit (result->value.integer, i + shift);
1893 for (i = delta; i < ssize; i++)
1896 mpz_clrbit (result->value.integer, i - delta);
1898 mpz_setbit (result->value.integer, i - delta);
1903 for (i = 0; i < ashift; i++)
1906 mpz_clrbit (result->value.integer, i + delta);
1908 mpz_setbit (result->value.integer, i + delta);
1911 for (i = ashift; i < ssize; i++)
1914 mpz_clrbit (result->value.integer, i + shift);
1916 mpz_setbit (result->value.integer, i + shift);
1920 convert_mpz_to_signed (result->value.integer, isize);
1928 gfc_simplify_kind (gfc_expr *e)
1931 if (e->ts.type == BT_DERIVED)
1933 gfc_error ("Argument of KIND at %L is a DERIVED type", &e->where);
1934 return &gfc_bad_expr;
1937 return gfc_int_expr (e->ts.kind);
1942 simplify_bound_dim (gfc_expr *array, int d, int upper, gfc_array_spec *as)
1944 gfc_expr *l, *u, *result;
1946 /* The last dimension of an assumed-size array is special. */
1947 if (d == as->rank && as->type == AS_ASSUMED_SIZE && !upper)
1949 if (as->lower[d-1]->expr_type == EXPR_CONSTANT)
1950 return gfc_copy_expr (as->lower[d-1]);
1955 /* Then, we need to know the extent of the given dimension. */
1959 if (l->expr_type != EXPR_CONSTANT || u->expr_type != EXPR_CONSTANT)
1962 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
1965 if (mpz_cmp (l->value.integer, u->value.integer) > 0)
1969 mpz_set_si (result->value.integer, 0);
1971 mpz_set_si (result->value.integer, 1);
1975 /* Nonzero extent. */
1977 mpz_set (result->value.integer, u->value.integer);
1979 mpz_set (result->value.integer, l->value.integer);
1982 return range_check (result, upper ? "UBOUND" : "LBOUND");
1987 simplify_bound (gfc_expr *array, gfc_expr *dim, int upper)
1993 if (array->expr_type != EXPR_VARIABLE)
1996 /* Follow any component references. */
1997 as = array->symtree->n.sym->as;
1998 for (ref = array->ref; ref; ref = ref->next)
2003 switch (ref->u.ar.type)
2010 /* We're done because 'as' has already been set in the
2011 previous iteration. */
2022 as = ref->u.c.component->as;
2034 if (as->type == AS_DEFERRED || as->type == AS_ASSUMED_SHAPE)
2039 /* Multi-dimensional bounds. */
2040 gfc_expr *bounds[GFC_MAX_DIMENSIONS];
2042 gfc_constructor *head, *tail;
2044 /* UBOUND(ARRAY) is not valid for an assumed-size array. */
2045 if (upper && as->type == AS_ASSUMED_SIZE)
2047 /* An error message will be emitted in
2048 check_assumed_size_reference (resolve.c). */
2049 return &gfc_bad_expr;
2052 /* Simplify the bounds for each dimension. */
2053 for (d = 0; d < array->rank; d++)
2055 bounds[d] = simplify_bound_dim (array, d + 1, upper, as);
2056 if (bounds[d] == NULL || bounds[d] == &gfc_bad_expr)
2060 for (j = 0; j < d; j++)
2061 gfc_free_expr (bounds[j]);
2066 /* Allocate the result expression. */
2067 e = gfc_get_expr ();
2068 e->where = array->where;
2069 e->expr_type = EXPR_ARRAY;
2070 e->ts.type = BT_INTEGER;
2071 e->ts.kind = gfc_default_integer_kind;
2073 /* The result is a rank 1 array; its size is the rank of the first
2074 argument to {L,U}BOUND. */
2076 e->shape = gfc_get_shape (1);
2077 mpz_init_set_ui (e->shape[0], array->rank);
2079 /* Create the constructor for this array. */
2081 for (d = 0; d < array->rank; d++)
2083 /* Get a new constructor element. */
2085 head = tail = gfc_get_constructor ();
2088 tail->next = gfc_get_constructor ();
2092 tail->where = e->where;
2093 tail->expr = bounds[d];
2095 e->value.constructor = head;
2101 /* A DIM argument is specified. */
2102 if (dim->expr_type != EXPR_CONSTANT)
2105 d = mpz_get_si (dim->value.integer);
2107 if (d < 1 || d > as->rank
2108 || (d == as->rank && as->type == AS_ASSUMED_SIZE && upper))
2110 gfc_error ("DIM argument at %L is out of bounds", &dim->where);
2111 return &gfc_bad_expr;
2114 return simplify_bound_dim (array, d, upper, as);
2120 gfc_simplify_lbound (gfc_expr *array, gfc_expr *dim)
2122 return simplify_bound (array, dim, 0);
2127 gfc_simplify_len (gfc_expr *e)
2131 if (e->expr_type == EXPR_CONSTANT)
2133 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
2135 mpz_set_si (result->value.integer, e->value.character.length);
2136 return range_check (result, "LEN");
2139 if (e->ts.cl != NULL && e->ts.cl->length != NULL
2140 && e->ts.cl->length->expr_type == EXPR_CONSTANT
2141 && e->ts.cl->length->ts.type == BT_INTEGER)
2143 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
2145 mpz_set (result->value.integer, e->ts.cl->length->value.integer);
2146 return range_check (result, "LEN");
2154 gfc_simplify_len_trim (gfc_expr *e)
2157 int count, len, lentrim, i;
2159 if (e->expr_type != EXPR_CONSTANT)
2162 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
2165 len = e->value.character.length;
2167 for (count = 0, i = 1; i <= len; i++)
2168 if (e->value.character.string[len - i] == ' ')
2173 lentrim = len - count;
2175 mpz_set_si (result->value.integer, lentrim);
2176 return range_check (result, "LEN_TRIM");
2181 gfc_simplify_lge (gfc_expr *a, gfc_expr *b)
2183 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
2186 return gfc_logical_expr (gfc_compare_string (a, b) >= 0, &a->where);
2191 gfc_simplify_lgt (gfc_expr *a, gfc_expr *b)
2193 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
2196 return gfc_logical_expr (gfc_compare_string (a, b) > 0,
2202 gfc_simplify_lle (gfc_expr *a, gfc_expr *b)
2204 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
2207 return gfc_logical_expr (gfc_compare_string (a, b) <= 0, &a->where);
2212 gfc_simplify_llt (gfc_expr *a, gfc_expr *b)
2214 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
2217 return gfc_logical_expr (gfc_compare_string (a, b) < 0, &a->where);
2222 gfc_simplify_log (gfc_expr *x)
2227 if (x->expr_type != EXPR_CONSTANT)
2230 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
2232 gfc_set_model_kind (x->ts.kind);
2237 if (mpfr_sgn (x->value.real) <= 0)
2239 gfc_error ("Argument of LOG at %L cannot be less than or equal "
2240 "to zero", &x->where);
2241 gfc_free_expr (result);
2242 return &gfc_bad_expr;
2245 mpfr_log (result->value.real, x->value.real, GFC_RND_MODE);
2249 if ((mpfr_sgn (x->value.complex.r) == 0)
2250 && (mpfr_sgn (x->value.complex.i) == 0))
2252 gfc_error ("Complex argument of LOG at %L cannot be zero",
2254 gfc_free_expr (result);
2255 return &gfc_bad_expr;
2261 mpfr_atan2 (result->value.complex.i, x->value.complex.i,
2262 x->value.complex.r, GFC_RND_MODE);
2264 mpfr_mul (xr, x->value.complex.r, x->value.complex.r, GFC_RND_MODE);
2265 mpfr_mul (xi, x->value.complex.i, x->value.complex.i, GFC_RND_MODE);
2266 mpfr_add (xr, xr, xi, GFC_RND_MODE);
2267 mpfr_sqrt (xr, xr, GFC_RND_MODE);
2268 mpfr_log (result->value.complex.r, xr, GFC_RND_MODE);
2276 gfc_internal_error ("gfc_simplify_log: bad type");
2279 return range_check (result, "LOG");
2284 gfc_simplify_log10 (gfc_expr *x)
2288 if (x->expr_type != EXPR_CONSTANT)
2291 gfc_set_model_kind (x->ts.kind);
2293 if (mpfr_sgn (x->value.real) <= 0)
2295 gfc_error ("Argument of LOG10 at %L cannot be less than or equal "
2296 "to zero", &x->where);
2297 return &gfc_bad_expr;
2300 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
2302 mpfr_log10 (result->value.real, x->value.real, GFC_RND_MODE);
2304 return range_check (result, "LOG10");
2309 gfc_simplify_logical (gfc_expr *e, gfc_expr *k)
2314 kind = get_kind (BT_LOGICAL, k, "LOGICAL", gfc_default_logical_kind);
2316 return &gfc_bad_expr;
2318 if (e->expr_type != EXPR_CONSTANT)
2321 result = gfc_constant_result (BT_LOGICAL, kind, &e->where);
2323 result->value.logical = e->value.logical;
2329 /* This function is special since MAX() can take any number of
2330 arguments. The simplified expression is a rewritten version of the
2331 argument list containing at most one constant element. Other
2332 constant elements are deleted. Because the argument list has
2333 already been checked, this function always succeeds. sign is 1 for
2334 MAX(), -1 for MIN(). */
2337 simplify_min_max (gfc_expr *expr, int sign)
2339 gfc_actual_arglist *arg, *last, *extremum;
2340 gfc_intrinsic_sym * specific;
2344 specific = expr->value.function.isym;
2346 arg = expr->value.function.actual;
2348 for (; arg; last = arg, arg = arg->next)
2350 if (arg->expr->expr_type != EXPR_CONSTANT)
2353 if (extremum == NULL)
2359 switch (arg->expr->ts.type)
2362 if (mpz_cmp (arg->expr->value.integer,
2363 extremum->expr->value.integer) * sign > 0)
2364 mpz_set (extremum->expr->value.integer, arg->expr->value.integer);
2369 if (mpfr_cmp (arg->expr->value.real, extremum->expr->value.real)
2371 mpfr_set (extremum->expr->value.real, arg->expr->value.real,
2377 gfc_internal_error ("gfc_simplify_max(): Bad type in arglist");
2380 /* Delete the extra constant argument. */
2382 expr->value.function.actual = arg->next;
2384 last->next = arg->next;
2387 gfc_free_actual_arglist (arg);
2391 /* If there is one value left, replace the function call with the
2393 if (expr->value.function.actual->next != NULL)
2396 /* Convert to the correct type and kind. */
2397 if (expr->ts.type != BT_UNKNOWN)
2398 return gfc_convert_constant (expr->value.function.actual->expr,
2399 expr->ts.type, expr->ts.kind);
2401 if (specific->ts.type != BT_UNKNOWN)
2402 return gfc_convert_constant (expr->value.function.actual->expr,
2403 specific->ts.type, specific->ts.kind);
2405 return gfc_copy_expr (expr->value.function.actual->expr);
2410 gfc_simplify_min (gfc_expr *e)
2412 return simplify_min_max (e, -1);
2417 gfc_simplify_max (gfc_expr *e)
2419 return simplify_min_max (e, 1);
2424 gfc_simplify_maxexponent (gfc_expr *x)
2429 i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
2431 result = gfc_int_expr (gfc_real_kinds[i].max_exponent);
2432 result->where = x->where;
2439 gfc_simplify_minexponent (gfc_expr *x)
2444 i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
2446 result = gfc_int_expr (gfc_real_kinds[i].min_exponent);
2447 result->where = x->where;
2454 gfc_simplify_mod (gfc_expr *a, gfc_expr *p)
2457 mpfr_t quot, iquot, term;
2460 if (a->expr_type != EXPR_CONSTANT || p->expr_type != EXPR_CONSTANT)
2463 kind = a->ts.kind > p->ts.kind ? a->ts.kind : p->ts.kind;
2464 result = gfc_constant_result (a->ts.type, kind, &a->where);
2469 if (mpz_cmp_ui (p->value.integer, 0) == 0)
2471 /* Result is processor-dependent. */
2472 gfc_error ("Second argument MOD at %L is zero", &a->where);
2473 gfc_free_expr (result);
2474 return &gfc_bad_expr;
2476 mpz_tdiv_r (result->value.integer, a->value.integer, p->value.integer);
2480 if (mpfr_cmp_ui (p->value.real, 0) == 0)
2482 /* Result is processor-dependent. */
2483 gfc_error ("Second argument of MOD at %L is zero", &p->where);
2484 gfc_free_expr (result);
2485 return &gfc_bad_expr;
2488 gfc_set_model_kind (kind);
2493 mpfr_div (quot, a->value.real, p->value.real, GFC_RND_MODE);
2494 mpfr_trunc (iquot, quot);
2495 mpfr_mul (term, iquot, p->value.real, GFC_RND_MODE);
2496 mpfr_sub (result->value.real, a->value.real, term, GFC_RND_MODE);
2504 gfc_internal_error ("gfc_simplify_mod(): Bad arguments");
2507 return range_check (result, "MOD");
2512 gfc_simplify_modulo (gfc_expr *a, gfc_expr *p)
2515 mpfr_t quot, iquot, term;
2518 if (a->expr_type != EXPR_CONSTANT || p->expr_type != EXPR_CONSTANT)
2521 kind = a->ts.kind > p->ts.kind ? a->ts.kind : p->ts.kind;
2522 result = gfc_constant_result (a->ts.type, kind, &a->where);
2527 if (mpz_cmp_ui (p->value.integer, 0) == 0)
2529 /* Result is processor-dependent. This processor just opts
2530 to not handle it at all. */
2531 gfc_error ("Second argument of MODULO at %L is zero", &a->where);
2532 gfc_free_expr (result);
2533 return &gfc_bad_expr;
2535 mpz_fdiv_r (result->value.integer, a->value.integer, p->value.integer);
2540 if (mpfr_cmp_ui (p->value.real, 0) == 0)
2542 /* Result is processor-dependent. */
2543 gfc_error ("Second argument of MODULO at %L is zero", &p->where);
2544 gfc_free_expr (result);
2545 return &gfc_bad_expr;
2548 gfc_set_model_kind (kind);
2553 mpfr_div (quot, a->value.real, p->value.real, GFC_RND_MODE);
2554 mpfr_floor (iquot, quot);
2555 mpfr_mul (term, iquot, p->value.real, GFC_RND_MODE);
2556 mpfr_sub (result->value.real, a->value.real, term, GFC_RND_MODE);
2564 gfc_internal_error ("gfc_simplify_modulo(): Bad arguments");
2567 return range_check (result, "MODULO");
2571 /* Exists for the sole purpose of consistency with other intrinsics. */
2573 gfc_simplify_mvbits (gfc_expr *f ATTRIBUTE_UNUSED,
2574 gfc_expr *fp ATTRIBUTE_UNUSED,
2575 gfc_expr *l ATTRIBUTE_UNUSED,
2576 gfc_expr *to ATTRIBUTE_UNUSED,
2577 gfc_expr *tp ATTRIBUTE_UNUSED)
2584 gfc_simplify_nearest (gfc_expr *x, gfc_expr *s)
2590 if (x->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
2593 if (mpfr_sgn (s->value.real) == 0)
2595 gfc_error ("Second argument of NEAREST at %L shall not be zero",
2597 return &gfc_bad_expr;
2600 gfc_set_model_kind (x->ts.kind);
2601 result = gfc_copy_expr (x);
2603 sgn = mpfr_sgn (s->value.real);
2605 mpfr_set_inf (tmp, sgn);
2606 mpfr_nexttoward (result->value.real, tmp);
2609 return range_check (result, "NEAREST");
2614 simplify_nint (const char *name, gfc_expr *e, gfc_expr *k)
2616 gfc_expr *itrunc, *result;
2619 kind = get_kind (BT_INTEGER, k, name, gfc_default_integer_kind);
2621 return &gfc_bad_expr;
2623 if (e->expr_type != EXPR_CONSTANT)
2626 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
2628 itrunc = gfc_copy_expr (e);
2630 mpfr_round (itrunc->value.real, e->value.real);
2632 gfc_mpfr_to_mpz (result->value.integer, itrunc->value.real);
2634 gfc_free_expr (itrunc);
2636 return range_check (result, name);
2641 gfc_simplify_new_line (gfc_expr *e)
2645 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
2646 result->value.character.string = gfc_getmem (2);
2647 result->value.character.length = 1;
2648 result->value.character.string[0] = '\n';
2649 result->value.character.string[1] = '\0'; /* For debugger */
2655 gfc_simplify_nint (gfc_expr *e, gfc_expr *k)
2657 return simplify_nint ("NINT", e, k);
2662 gfc_simplify_idnint (gfc_expr *e)
2664 return simplify_nint ("IDNINT", e, NULL);
2669 gfc_simplify_not (gfc_expr *e)
2673 if (e->expr_type != EXPR_CONSTANT)
2676 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
2678 mpz_com (result->value.integer, e->value.integer);
2680 return range_check (result, "NOT");
2685 gfc_simplify_null (gfc_expr *mold)
2691 result = gfc_get_expr ();
2692 result->ts.type = BT_UNKNOWN;
2695 result = gfc_copy_expr (mold);
2696 result->expr_type = EXPR_NULL;
2703 gfc_simplify_or (gfc_expr *x, gfc_expr *y)
2708 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2711 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
2712 if (x->ts.type == BT_INTEGER)
2714 result = gfc_constant_result (BT_INTEGER, kind, &x->where);
2715 mpz_ior (result->value.integer, x->value.integer, y->value.integer);
2717 else /* BT_LOGICAL */
2719 result = gfc_constant_result (BT_LOGICAL, kind, &x->where);
2720 result->value.logical = x->value.logical || y->value.logical;
2723 return range_check (result, "OR");
2728 gfc_simplify_precision (gfc_expr *e)
2733 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2735 result = gfc_int_expr (gfc_real_kinds[i].precision);
2736 result->where = e->where;
2743 gfc_simplify_radix (gfc_expr *e)
2748 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2752 i = gfc_integer_kinds[i].radix;
2756 i = gfc_real_kinds[i].radix;
2763 result = gfc_int_expr (i);
2764 result->where = e->where;
2771 gfc_simplify_range (gfc_expr *e)
2777 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2782 j = gfc_integer_kinds[i].range;
2787 j = gfc_real_kinds[i].range;
2794 result = gfc_int_expr (j);
2795 result->where = e->where;
2802 gfc_simplify_real (gfc_expr *e, gfc_expr *k)
2807 if (e->ts.type == BT_COMPLEX)
2808 kind = get_kind (BT_REAL, k, "REAL", e->ts.kind);
2810 kind = get_kind (BT_REAL, k, "REAL", gfc_default_real_kind);
2813 return &gfc_bad_expr;
2815 if (e->expr_type != EXPR_CONSTANT)
2821 result = gfc_int2real (e, kind);
2825 result = gfc_real2real (e, kind);
2829 result = gfc_complex2real (e, kind);
2833 gfc_internal_error ("bad type in REAL");
2837 return range_check (result, "REAL");
2842 gfc_simplify_realpart (gfc_expr *e)
2846 if (e->expr_type != EXPR_CONSTANT)
2849 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
2850 mpfr_set (result->value.real, e->value.complex.r, GFC_RND_MODE);
2852 return range_check (result, "REALPART");
2856 gfc_simplify_repeat (gfc_expr *e, gfc_expr *n)
2859 int i, j, len, ncop, nlen;
2861 bool have_length = false;
2863 /* If NCOPIES isn't a constant, there's nothing we can do. */
2864 if (n->expr_type != EXPR_CONSTANT)
2867 /* If NCOPIES is negative, it's an error. */
2868 if (mpz_sgn (n->value.integer) < 0)
2870 gfc_error ("Argument NCOPIES of REPEAT intrinsic is negative at %L",
2872 return &gfc_bad_expr;
2875 /* If we don't know the character length, we can do no more. */
2876 if (e->ts.cl && e->ts.cl->length
2877 && e->ts.cl->length->expr_type == EXPR_CONSTANT)
2879 len = mpz_get_si (e->ts.cl->length->value.integer);
2882 else if (e->expr_type == EXPR_CONSTANT
2883 && (e->ts.cl == NULL || e->ts.cl->length == NULL))
2885 len = e->value.character.length;
2890 /* If the source length is 0, any value of NCOPIES is valid
2891 and everything behaves as if NCOPIES == 0. */
2894 mpz_set_ui (ncopies, 0);
2896 mpz_set (ncopies, n->value.integer);
2898 /* Check that NCOPIES isn't too large. */
2904 /* Compute the maximum value allowed for NCOPIES: huge(cl) / len. */
2906 i = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
2910 mpz_tdiv_q (max, gfc_integer_kinds[i].huge,
2911 e->ts.cl->length->value.integer);
2915 mpz_init_set_si (mlen, len);
2916 mpz_tdiv_q (max, gfc_integer_kinds[i].huge, mlen);
2920 /* The check itself. */
2921 if (mpz_cmp (ncopies, max) > 0)
2924 mpz_clear (ncopies);
2925 gfc_error ("Argument NCOPIES of REPEAT intrinsic is too large at %L",
2927 return &gfc_bad_expr;
2932 mpz_clear (ncopies);
2934 /* For further simplification, we need the character string to be
2936 if (e->expr_type != EXPR_CONSTANT)
2939 if (len || mpz_sgn (e->ts.cl->length->value.integer) != 0)
2941 const char *res = gfc_extract_int (n, &ncop);
2942 gcc_assert (res == NULL);
2947 len = e->value.character.length;
2950 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
2954 result->value.character.string = gfc_getmem (1);
2955 result->value.character.length = 0;
2956 result->value.character.string[0] = '\0';
2960 result->value.character.length = nlen;
2961 result->value.character.string = gfc_getmem (nlen + 1);
2963 for (i = 0; i < ncop; i++)
2964 for (j = 0; j < len; j++)
2965 result->value.character.string[j + i * len]
2966 = e->value.character.string[j];
2968 result->value.character.string[nlen] = '\0'; /* For debugger */
2973 /* This one is a bear, but mainly has to do with shuffling elements. */
2976 gfc_simplify_reshape (gfc_expr *source, gfc_expr *shape_exp,
2977 gfc_expr *pad, gfc_expr *order_exp)
2979 int order[GFC_MAX_DIMENSIONS], shape[GFC_MAX_DIMENSIONS];
2980 int i, rank, npad, x[GFC_MAX_DIMENSIONS];
2981 gfc_constructor *head, *tail;
2987 /* Unpack the shape array. */
2988 if (source->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (source))
2991 if (shape_exp->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (shape_exp))
2995 && (pad->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (pad)))
2998 if (order_exp != NULL
2999 && (order_exp->expr_type != EXPR_ARRAY
3000 || !gfc_is_constant_expr (order_exp)))
3009 e = gfc_get_array_element (shape_exp, rank);
3013 if (gfc_extract_int (e, &shape[rank]) != NULL)
3015 gfc_error ("Integer too large in shape specification at %L",
3023 if (rank >= GFC_MAX_DIMENSIONS)
3025 gfc_error ("Too many dimensions in shape specification for RESHAPE "
3026 "at %L", &e->where);
3031 if (shape[rank] < 0)
3033 gfc_error ("Shape specification at %L cannot be negative",
3043 gfc_error ("Shape specification at %L cannot be the null array",
3048 /* Now unpack the order array if present. */
3049 if (order_exp == NULL)
3051 for (i = 0; i < rank; i++)
3056 for (i = 0; i < rank; i++)
3059 for (i = 0; i < rank; i++)
3061 e = gfc_get_array_element (order_exp, i);
3064 gfc_error ("ORDER parameter of RESHAPE at %L is not the same "
3065 "size as SHAPE parameter", &order_exp->where);
3069 if (gfc_extract_int (e, &order[i]) != NULL)
3071 gfc_error ("Error in ORDER parameter of RESHAPE at %L",
3079 if (order[i] < 1 || order[i] > rank)
3081 gfc_error ("ORDER parameter of RESHAPE at %L is out of range",
3090 gfc_error ("Invalid permutation in ORDER parameter at %L",
3099 /* Count the elements in the source and padding arrays. */
3104 gfc_array_size (pad, &size);
3105 npad = mpz_get_ui (size);
3109 gfc_array_size (source, &size);
3110 nsource = mpz_get_ui (size);
3113 /* If it weren't for that pesky permutation we could just loop
3114 through the source and round out any shortage with pad elements.
3115 But no, someone just had to have the compiler do something the
3116 user should be doing. */
3118 for (i = 0; i < rank; i++)
3123 /* Figure out which element to extract. */
3124 mpz_set_ui (index, 0);
3126 for (i = rank - 1; i >= 0; i--)
3128 mpz_add_ui (index, index, x[order[i]]);
3130 mpz_mul_ui (index, index, shape[order[i - 1]]);
3133 if (mpz_cmp_ui (index, INT_MAX) > 0)
3134 gfc_internal_error ("Reshaped array too large at %L", &e->where);
3136 j = mpz_get_ui (index);
3139 e = gfc_get_array_element (source, j);
3146 gfc_error ("PAD parameter required for short SOURCE parameter "
3147 "at %L", &source->where);
3152 e = gfc_get_array_element (pad, j);
3156 head = tail = gfc_get_constructor ();
3159 tail->next = gfc_get_constructor ();
3166 tail->where = e->where;
3169 /* Calculate the next element. */
3173 if (++x[i] < shape[i])
3184 e = gfc_get_expr ();
3185 e->where = source->where;
3186 e->expr_type = EXPR_ARRAY;
3187 e->value.constructor = head;
3188 e->shape = gfc_get_shape (rank);
3190 for (i = 0; i < rank; i++)
3191 mpz_init_set_ui (e->shape[i], shape[i]);
3199 gfc_free_constructor (head);
3201 return &gfc_bad_expr;
3206 gfc_simplify_rrspacing (gfc_expr *x)
3212 if (x->expr_type != EXPR_CONSTANT)
3215 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
3217 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
3219 mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE);
3221 /* Special case x = -0 and 0. */
3222 if (mpfr_sgn (result->value.real) == 0)
3224 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
3228 /* | x * 2**(-e) | * 2**p. */
3229 e = - (long int) mpfr_get_exp (x->value.real);
3230 mpfr_mul_2si (result->value.real, result->value.real, e, GFC_RND_MODE);
3232 p = (long int) gfc_real_kinds[i].digits;
3233 mpfr_mul_2si (result->value.real, result->value.real, p, GFC_RND_MODE);
3235 return range_check (result, "RRSPACING");
3240 gfc_simplify_scale (gfc_expr *x, gfc_expr *i)
3242 int k, neg_flag, power, exp_range;
3243 mpfr_t scale, radix;
3246 if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
3249 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
3251 if (mpfr_sgn (x->value.real) == 0)
3253 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
3257 k = gfc_validate_kind (BT_REAL, x->ts.kind, false);
3259 exp_range = gfc_real_kinds[k].max_exponent - gfc_real_kinds[k].min_exponent;
3261 /* This check filters out values of i that would overflow an int. */
3262 if (mpz_cmp_si (i->value.integer, exp_range + 2) > 0
3263 || mpz_cmp_si (i->value.integer, -exp_range - 2) < 0)
3265 gfc_error ("Result of SCALE overflows its kind at %L", &result->where);
3266 return &gfc_bad_expr;
3269 /* Compute scale = radix ** power. */
3270 power = mpz_get_si (i->value.integer);
3280 gfc_set_model_kind (x->ts.kind);
3283 mpfr_set_ui (radix, gfc_real_kinds[k].radix, GFC_RND_MODE);
3284 mpfr_pow_ui (scale, radix, power, GFC_RND_MODE);
3287 mpfr_div (result->value.real, x->value.real, scale, GFC_RND_MODE);
3289 mpfr_mul (result->value.real, x->value.real, scale, GFC_RND_MODE);
3294 return range_check (result, "SCALE");
3299 gfc_simplify_scan (gfc_expr *e, gfc_expr *c, gfc_expr *b)
3304 size_t indx, len, lenc;
3306 if (e->expr_type != EXPR_CONSTANT || c->expr_type != EXPR_CONSTANT)
3309 if (b != NULL && b->value.logical != 0)
3314 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
3317 len = e->value.character.length;
3318 lenc = c->value.character.length;
3320 if (len == 0 || lenc == 0)
3328 indx = strcspn (e->value.character.string, c->value.character.string)
3336 for (indx = len; indx > 0; indx--)
3338 for (i = 0; i < lenc; i++)
3340 if (c->value.character.string[i]
3341 == e->value.character.string[indx - 1])
3349 mpz_set_ui (result->value.integer, indx);
3350 return range_check (result, "SCAN");
3355 gfc_simplify_selected_int_kind (gfc_expr *e)
3360 if (e->expr_type != EXPR_CONSTANT || gfc_extract_int (e, &range) != NULL)
3365 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
3366 if (gfc_integer_kinds[i].range >= range
3367 && gfc_integer_kinds[i].kind < kind)
3368 kind = gfc_integer_kinds[i].kind;
3370 if (kind == INT_MAX)
3373 result = gfc_int_expr (kind);
3374 result->where = e->where;
3381 gfc_simplify_selected_real_kind (gfc_expr *p, gfc_expr *q)
3383 int range, precision, i, kind, found_precision, found_range;
3390 if (p->expr_type != EXPR_CONSTANT
3391 || gfc_extract_int (p, &precision) != NULL)
3399 if (q->expr_type != EXPR_CONSTANT
3400 || gfc_extract_int (q, &range) != NULL)
3405 found_precision = 0;
3408 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
3410 if (gfc_real_kinds[i].precision >= precision)
3411 found_precision = 1;
3413 if (gfc_real_kinds[i].range >= range)
3416 if (gfc_real_kinds[i].precision >= precision
3417 && gfc_real_kinds[i].range >= range && gfc_real_kinds[i].kind < kind)
3418 kind = gfc_real_kinds[i].kind;
3421 if (kind == INT_MAX)
3425 if (!found_precision)
3431 result = gfc_int_expr (kind);
3432 result->where = (p != NULL) ? p->where : q->where;
3439 gfc_simplify_set_exponent (gfc_expr *x, gfc_expr *i)
3442 mpfr_t exp, absv, log2, pow2, frac;
3445 if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
3448 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
3450 gfc_set_model_kind (x->ts.kind);
3452 if (mpfr_sgn (x->value.real) == 0)
3454 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
3464 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
3465 mpfr_log2 (log2, absv, GFC_RND_MODE);
3467 mpfr_trunc (log2, log2);
3468 mpfr_add_ui (exp, log2, 1, GFC_RND_MODE);
3470 /* Old exponent value, and fraction. */
3471 mpfr_ui_pow (pow2, 2, exp, GFC_RND_MODE);
3473 mpfr_div (frac, absv, pow2, GFC_RND_MODE);
3476 exp2 = (unsigned long) mpz_get_d (i->value.integer);
3477 mpfr_mul_2exp (result->value.real, frac, exp2, GFC_RND_MODE);
3484 return range_check (result, "SET_EXPONENT");
3489 gfc_simplify_shape (gfc_expr *source)
3491 mpz_t shape[GFC_MAX_DIMENSIONS];
3492 gfc_expr *result, *e, *f;
3497 if (source->rank == 0 || source->expr_type != EXPR_VARIABLE)
3500 result = gfc_start_constructor (BT_INTEGER, gfc_default_integer_kind,
3503 ar = gfc_find_array_ref (source);
3505 t = gfc_array_ref_shape (ar, shape);
3507 for (n = 0; n < source->rank; n++)
3509 e = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
3514 mpz_set (e->value.integer, shape[n]);
3515 mpz_clear (shape[n]);
3519 mpz_set_ui (e->value.integer, n + 1);
3521 f = gfc_simplify_size (source, e);
3525 gfc_free_expr (result);
3534 gfc_append_constructor (result, e);
3542 gfc_simplify_size (gfc_expr *array, gfc_expr *dim)
3550 if (gfc_array_size (array, &size) == FAILURE)
3555 if (dim->expr_type != EXPR_CONSTANT)
3558 d = mpz_get_ui (dim->value.integer) - 1;
3559 if (gfc_array_dimen_size (array, d, &size) == FAILURE)
3563 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
3566 mpz_set (result->value.integer, size);
3573 gfc_simplify_sign (gfc_expr *x, gfc_expr *y)
3577 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
3580 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3585 mpz_abs (result->value.integer, x->value.integer);
3586 if (mpz_sgn (y->value.integer) < 0)
3587 mpz_neg (result->value.integer, result->value.integer);
3592 /* TODO: Handle -0.0 and +0.0 correctly on machines that support
3594 mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE);
3595 if (mpfr_sgn (y->value.real) < 0)
3596 mpfr_neg (result->value.real, result->value.real, GFC_RND_MODE);
3601 gfc_internal_error ("Bad type in gfc_simplify_sign");
3609 gfc_simplify_sin (gfc_expr *x)
3614 if (x->expr_type != EXPR_CONSTANT)
3617 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3622 mpfr_sin (result->value.real, x->value.real, GFC_RND_MODE);
3626 gfc_set_model (x->value.real);
3630 mpfr_sin (xp, x->value.complex.r, GFC_RND_MODE);
3631 mpfr_cosh (xq, x->value.complex.i, GFC_RND_MODE);
3632 mpfr_mul (result->value.complex.r, xp, xq, GFC_RND_MODE);
3634 mpfr_cos (xp, x->value.complex.r, GFC_RND_MODE);
3635 mpfr_sinh (xq, x->value.complex.i, GFC_RND_MODE);
3636 mpfr_mul (result->value.complex.i, xp, xq, GFC_RND_MODE);
3643 gfc_internal_error ("in gfc_simplify_sin(): Bad type");
3646 return range_check (result, "SIN");
3651 gfc_simplify_sinh (gfc_expr *x)
3655 if (x->expr_type != EXPR_CONSTANT)
3658 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3660 mpfr_sinh (result->value.real, x->value.real, GFC_RND_MODE);
3662 return range_check (result, "SINH");
3666 /* The argument is always a double precision real that is converted to
3667 single precision. TODO: Rounding! */
3670 gfc_simplify_sngl (gfc_expr *a)
3674 if (a->expr_type != EXPR_CONSTANT)
3677 result = gfc_real2real (a, gfc_default_real_kind);
3678 return range_check (result, "SNGL");
3683 gfc_simplify_spacing (gfc_expr *x)
3689 if (x->expr_type != EXPR_CONSTANT)
3692 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
3694 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
3696 /* Special case x = 0 and -0. */
3697 mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE);
3698 if (mpfr_sgn (result->value.real) == 0)
3700 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
3704 /* In the Fortran 95 standard, the result is b**(e - p) where b, e, and p
3705 are the radix, exponent of x, and precision. This excludes the
3706 possibility of subnormal numbers. Fortran 2003 states the result is
3707 b**max(e - p, emin - 1). */
3709 ep = (long int) mpfr_get_exp (x->value.real) - gfc_real_kinds[i].digits;
3710 en = (long int) gfc_real_kinds[i].min_exponent - 1;
3711 en = en > ep ? en : ep;
3713 mpfr_set_ui (result->value.real, 1, GFC_RND_MODE);
3714 mpfr_mul_2si (result->value.real, result->value.real, en, GFC_RND_MODE);
3716 return range_check (result, "SPACING");
3721 gfc_simplify_sqrt (gfc_expr *e)
3724 mpfr_t ac, ad, s, t, w;
3726 if (e->expr_type != EXPR_CONSTANT)
3729 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
3734 if (mpfr_cmp_si (e->value.real, 0) < 0)
3736 mpfr_sqrt (result->value.real, e->value.real, GFC_RND_MODE);
3741 /* Formula taken from Numerical Recipes to avoid over- and
3744 gfc_set_model (e->value.real);
3751 if (mpfr_cmp_ui (e->value.complex.r, 0) == 0
3752 && mpfr_cmp_ui (e->value.complex.i, 0) == 0)
3754 mpfr_set_ui (result->value.complex.r, 0, GFC_RND_MODE);
3755 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
3759 mpfr_abs (ac, e->value.complex.r, GFC_RND_MODE);
3760 mpfr_abs (ad, e->value.complex.i, GFC_RND_MODE);
3762 if (mpfr_cmp (ac, ad) >= 0)
3764 mpfr_div (t, e->value.complex.i, e->value.complex.r, GFC_RND_MODE);
3765 mpfr_mul (t, t, t, GFC_RND_MODE);
3766 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
3767 mpfr_sqrt (t, t, GFC_RND_MODE);
3768 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
3769 mpfr_div_ui (t, t, 2, GFC_RND_MODE);
3770 mpfr_sqrt (t, t, GFC_RND_MODE);
3771 mpfr_sqrt (s, ac, GFC_RND_MODE);
3772 mpfr_mul (w, s, t, GFC_RND_MODE);
3776 mpfr_div (s, e->value.complex.r, e->value.complex.i, GFC_RND_MODE);
3777 mpfr_mul (t, s, s, GFC_RND_MODE);
3778 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
3779 mpfr_sqrt (t, t, GFC_RND_MODE);
3780 mpfr_abs (s, s, GFC_RND_MODE);
3781 mpfr_add (t, t, s, GFC_RND_MODE);
3782 mpfr_div_ui (t, t, 2, GFC_RND_MODE);
3783 mpfr_sqrt (t, t, GFC_RND_MODE);
3784 mpfr_sqrt (s, ad, GFC_RND_MODE);
3785 mpfr_mul (w, s, t, GFC_RND_MODE);
3788 if (mpfr_cmp_ui (w, 0) != 0 && mpfr_cmp_ui (e->value.complex.r, 0) >= 0)
3790 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
3791 mpfr_div (result->value.complex.i, e->value.complex.i, t, GFC_RND_MODE);
3792 mpfr_set (result->value.complex.r, w, GFC_RND_MODE);
3794 else if (mpfr_cmp_ui (w, 0) != 0
3795 && mpfr_cmp_ui (e->value.complex.r, 0) < 0
3796 && mpfr_cmp_ui (e->value.complex.i, 0) >= 0)
3798 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
3799 mpfr_div (result->value.complex.r, e->value.complex.i, t, GFC_RND_MODE);
3800 mpfr_set (result->value.complex.i, w, GFC_RND_MODE);
3802 else if (mpfr_cmp_ui (w, 0) != 0
3803 && mpfr_cmp_ui (e->value.complex.r, 0) < 0
3804 && mpfr_cmp_ui (e->value.complex.i, 0) < 0)
3806 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
3807 mpfr_div (result->value.complex.r, ad, t, GFC_RND_MODE);
3808 mpfr_neg (w, w, GFC_RND_MODE);
3809 mpfr_set (result->value.complex.i, w, GFC_RND_MODE);
3812 gfc_internal_error ("invalid complex argument of SQRT at %L",
3824 gfc_internal_error ("invalid argument of SQRT at %L", &e->where);
3827 return range_check (result, "SQRT");
3830 gfc_free_expr (result);
3831 gfc_error ("Argument of SQRT at %L has a negative value", &e->where);
3832 return &gfc_bad_expr;
3837 gfc_simplify_tan (gfc_expr *x)
3842 if (x->expr_type != EXPR_CONSTANT)
3845 i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
3847 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3849 mpfr_tan (result->value.real, x->value.real, GFC_RND_MODE);
3851 return range_check (result, "TAN");
3856 gfc_simplify_tanh (gfc_expr *x)
3860 if (x->expr_type != EXPR_CONSTANT)
3863 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3865 mpfr_tanh (result->value.real, x->value.real, GFC_RND_MODE);
3867 return range_check (result, "TANH");
3873 gfc_simplify_tiny (gfc_expr *e)
3878 i = gfc_validate_kind (BT_REAL, e->ts.kind, false);
3880 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
3881 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
3888 gfc_simplify_transfer (gfc_expr *source, gfc_expr *mold, gfc_expr *size)
3891 gfc_expr *mold_element;
3894 size_t result_elt_size;
3897 unsigned char *buffer;
3899 if (!gfc_is_constant_expr (source)
3900 || !gfc_is_constant_expr (size))
3903 /* Calculate the size of the source. */
3904 if (source->expr_type == EXPR_ARRAY
3905 && gfc_array_size (source, &tmp) == FAILURE)
3906 gfc_internal_error ("Failure getting length of a constant array.");
3908 source_size = gfc_target_expr_size (source);
3910 /* Create an empty new expression with the appropriate characteristics. */
3911 result = gfc_constant_result (mold->ts.type, mold->ts.kind,
3913 result->ts = mold->ts;
3915 mold_element = mold->expr_type == EXPR_ARRAY
3916 ? mold->value.constructor->expr
3919 /* Set result character length, if needed. Note that this needs to be
3920 set even for array expressions, in order to pass this information into
3921 gfc_target_interpret_expr. */
3922 if (result->ts.type == BT_CHARACTER)
3923 result->value.character.length = mold_element->value.character.length;
3925 /* Set the number of elements in the result, and determine its size. */
3926 result_elt_size = gfc_target_expr_size (mold_element);
3927 if (mold->expr_type == EXPR_ARRAY || mold->rank || size)
3931 result->expr_type = EXPR_ARRAY;
3935 result_length = (size_t)mpz_get_ui (size->value.integer);
3938 result_length = source_size / result_elt_size;
3939 if (result_length * result_elt_size < source_size)
3943 result->shape = gfc_get_shape (1);
3944 mpz_init_set_ui (result->shape[0], result_length);
3946 result_size = result_length * result_elt_size;
3951 result_size = result_elt_size;
3954 /* Allocate the buffer to store the binary version of the source. */
3955 buffer_size = MAX (source_size, result_size);
3956 buffer = (unsigned char*)alloca (buffer_size);
3958 /* Now write source to the buffer. */
3959 gfc_target_encode_expr (source, buffer, buffer_size);
3961 /* And read the buffer back into the new expression. */
3962 gfc_target_interpret_expr (buffer, buffer_size, result);
3969 gfc_simplify_trim (gfc_expr *e)
3972 int count, i, len, lentrim;
3974 if (e->expr_type != EXPR_CONSTANT)
3977 len = e->value.character.length;
3979 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
3981 for (count = 0, i = 1; i <= len; ++i)
3983 if (e->value.character.string[len - i] == ' ')
3989 lentrim = len - count;
3991 result->value.character.length = lentrim;
3992 result->value.character.string = gfc_getmem (lentrim + 1);
3994 for (i = 0; i < lentrim; i++)
3995 result->value.character.string[i] = e->value.character.string[i];
3997 result->value.character.string[lentrim] = '\0'; /* For debugger */
4004 gfc_simplify_ubound (gfc_expr *array, gfc_expr *dim)
4006 return simplify_bound (array, dim, 1);
4011 gfc_simplify_verify (gfc_expr *s, gfc_expr *set, gfc_expr *b)
4015 size_t index, len, lenset;
4018 if (s->expr_type != EXPR_CONSTANT || set->expr_type != EXPR_CONSTANT)
4021 if (b != NULL && b->value.logical != 0)
4026 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
4029 len = s->value.character.length;
4030 lenset = set->value.character.length;
4034 mpz_set_ui (result->value.integer, 0);
4042 mpz_set_ui (result->value.integer, 1);
4046 index = strspn (s->value.character.string, set->value.character.string)
4056 mpz_set_ui (result->value.integer, len);
4059 for (index = len; index > 0; index --)
4061 for (i = 0; i < lenset; i++)
4063 if (s->value.character.string[index - 1]
4064 == set->value.character.string[i])
4072 mpz_set_ui (result->value.integer, index);
4078 gfc_simplify_xor (gfc_expr *x, gfc_expr *y)
4083 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
4086 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
4087 if (x->ts.type == BT_INTEGER)
4089 result = gfc_constant_result (BT_INTEGER, kind, &x->where);
4090 mpz_xor (result->value.integer, x->value.integer, y->value.integer);
4092 else /* BT_LOGICAL */
4094 result = gfc_constant_result (BT_LOGICAL, kind, &x->where);
4095 result->value.logical = (x->value.logical && !y->value.logical)
4096 || (!x->value.logical && y->value.logical);
4099 return range_check (result, "XOR");
4103 /****************** Constant simplification *****************/
4105 /* Master function to convert one constant to another. While this is
4106 used as a simplification function, it requires the destination type
4107 and kind information which is supplied by a special case in
4111 gfc_convert_constant (gfc_expr *e, bt type, int kind)
4113 gfc_expr *g, *result, *(*f) (gfc_expr *, int);
4114 gfc_constructor *head, *c, *tail = NULL;
4128 f = gfc_int2complex;
4148 f = gfc_real2complex;
4159 f = gfc_complex2int;
4162 f = gfc_complex2real;
4165 f = gfc_complex2complex;
4191 f = gfc_hollerith2int;
4195 f = gfc_hollerith2real;
4199 f = gfc_hollerith2complex;
4203 f = gfc_hollerith2character;
4207 f = gfc_hollerith2logical;
4217 gfc_internal_error ("gfc_convert_constant(): Unexpected type");
4222 switch (e->expr_type)
4225 result = f (e, kind);
4227 return &gfc_bad_expr;
4231 if (!gfc_is_constant_expr (e))
4236 for (c = e->value.constructor; c; c = c->next)
4239 head = tail = gfc_get_constructor ();
4242 tail->next = gfc_get_constructor ();
4246 tail->where = c->where;
4248 if (c->iterator == NULL)
4249 tail->expr = f (c->expr, kind);
4252 g = gfc_convert_constant (c->expr, type, kind);
4253 if (g == &gfc_bad_expr)
4258 if (tail->expr == NULL)
4260 gfc_free_constructor (head);
4265 result = gfc_get_expr ();
4266 result->ts.type = type;
4267 result->ts.kind = kind;
4268 result->expr_type = EXPR_ARRAY;
4269 result->value.constructor = head;
4270 result->shape = gfc_copy_shape (e->shape, e->rank);
4271 result->where = e->where;
4272 result->rank = e->rank;