1 /* Simplify intrinsic functions at compile-time.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
3 2010 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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
27 #include "intrinsic.h"
28 #include "target-memory.h"
29 #include "constructor.h"
32 gfc_expr gfc_bad_expr;
35 /* Note that 'simplification' is not just transforming expressions.
36 For functions that are not simplified at compile time, range
37 checking is done if possible.
39 The return convention is that each simplification function returns:
41 A new expression node corresponding to the simplified arguments.
42 The original arguments are destroyed by the caller, and must not
43 be a part of the new expression.
45 NULL pointer indicating that no simplification was possible and
46 the original expression should remain intact.
48 An expression pointer to gfc_bad_expr (a static placeholder)
49 indicating that some error has prevented simplification. The
50 error is generated within the function and should be propagated
53 By the time a simplification function gets control, it has been
54 decided that the function call is really supposed to be the
55 intrinsic. No type checking is strictly necessary, since only
56 valid types will be passed on. On the other hand, a simplification
57 subroutine may have to look at the type of an argument as part of
60 Array arguments are only passed to these subroutines that implement
61 the simplification of transformational intrinsics.
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 /* Range checks an expression node. If all goes well, returns the
68 node, otherwise returns &gfc_bad_expr and frees the node. */
71 range_check (gfc_expr *result, const char *name)
76 if (result->expr_type != EXPR_CONSTANT)
79 switch (gfc_range_check (result))
85 gfc_error ("Result of %s overflows its kind at %L", name,
90 gfc_error ("Result of %s underflows its kind at %L", name,
95 gfc_error ("Result of %s is NaN at %L", name, &result->where);
99 gfc_error ("Result of %s gives range error for its kind at %L", name,
104 gfc_free_expr (result);
105 return &gfc_bad_expr;
109 /* A helper function that gets an optional and possibly missing
110 kind parameter. Returns the kind, -1 if something went wrong. */
113 get_kind (bt type, gfc_expr *k, const char *name, int default_kind)
120 if (k->expr_type != EXPR_CONSTANT)
122 gfc_error ("KIND parameter of %s at %L must be an initialization "
123 "expression", name, &k->where);
127 if (gfc_extract_int (k, &kind) != NULL
128 || gfc_validate_kind (type, kind, true) < 0)
130 gfc_error ("Invalid KIND parameter of %s at %L", name, &k->where);
138 /* Converts an mpz_t signed variable into an unsigned one, assuming
139 two's complement representations and a binary width of bitsize.
140 The conversion is a no-op unless x is negative; otherwise, it can
141 be accomplished by masking out the high bits. */
144 convert_mpz_to_unsigned (mpz_t x, int bitsize)
150 /* Confirm that no bits above the signed range are unset. */
151 gcc_assert (mpz_scan0 (x, bitsize-1) == ULONG_MAX);
153 mpz_init_set_ui (mask, 1);
154 mpz_mul_2exp (mask, mask, bitsize);
155 mpz_sub_ui (mask, mask, 1);
157 mpz_and (x, x, mask);
163 /* Confirm that no bits above the signed range are set. */
164 gcc_assert (mpz_scan1 (x, bitsize-1) == ULONG_MAX);
169 /* Converts an mpz_t unsigned variable into a signed one, assuming
170 two's complement representations and a binary width of bitsize.
171 If the bitsize-1 bit is set, this is taken as a sign bit and
172 the number is converted to the corresponding negative number. */
175 convert_mpz_to_signed (mpz_t x, int bitsize)
179 /* Confirm that no bits above the unsigned range are set. */
180 gcc_assert (mpz_scan1 (x, bitsize) == ULONG_MAX);
182 if (mpz_tstbit (x, bitsize - 1) == 1)
184 mpz_init_set_ui (mask, 1);
185 mpz_mul_2exp (mask, mask, bitsize);
186 mpz_sub_ui (mask, mask, 1);
188 /* We negate the number by hand, zeroing the high bits, that is
189 make it the corresponding positive number, and then have it
190 negated by GMP, giving the correct representation of the
193 mpz_add_ui (x, x, 1);
194 mpz_and (x, x, mask);
203 /* In-place convert BOZ to REAL of the specified kind. */
206 convert_boz (gfc_expr *x, int kind)
208 if (x && x->ts.type == BT_INTEGER && x->is_boz)
215 if (!gfc_convert_boz (x, &ts))
216 return &gfc_bad_expr;
223 /* Test that the expression is an constant array. */
226 is_constant_array_expr (gfc_expr *e)
233 if (e->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (e))
236 for (c = gfc_constructor_first (e->value.constructor);
237 c; c = gfc_constructor_next (c))
238 if (c->expr->expr_type != EXPR_CONSTANT)
245 /* Initialize a transformational result expression with a given value. */
248 init_result_expr (gfc_expr *e, int init, gfc_expr *array)
250 if (e && e->expr_type == EXPR_ARRAY)
252 gfc_constructor *ctor = gfc_constructor_first (e->value.constructor);
255 init_result_expr (ctor->expr, init, array);
256 ctor = gfc_constructor_next (ctor);
259 else if (e && e->expr_type == EXPR_CONSTANT)
261 int i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
268 e->value.logical = (init ? 1 : 0);
273 mpz_set (e->value.integer, gfc_integer_kinds[i].min_int);
274 else if (init == INT_MAX)
275 mpz_set (e->value.integer, gfc_integer_kinds[i].huge);
277 mpz_set_si (e->value.integer, init);
283 mpfr_set (e->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
284 mpfr_neg (e->value.real, e->value.real, GFC_RND_MODE);
286 else if (init == INT_MAX)
287 mpfr_set (e->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
289 mpfr_set_si (e->value.real, init, GFC_RND_MODE);
293 mpc_set_si (e->value.complex, init, GFC_MPC_RND_MODE);
299 gfc_expr *len = gfc_simplify_len (array, NULL);
300 gfc_extract_int (len, &length);
301 string = gfc_get_wide_string (length + 1);
302 gfc_wide_memset (string, 0, length);
304 else if (init == INT_MAX)
306 gfc_expr *len = gfc_simplify_len (array, NULL);
307 gfc_extract_int (len, &length);
308 string = gfc_get_wide_string (length + 1);
309 gfc_wide_memset (string, 255, length);
314 string = gfc_get_wide_string (1);
317 string[length] = '\0';
318 e->value.character.length = length;
319 e->value.character.string = string;
331 /* Helper function for gfc_simplify_dot_product() and gfc_simplify_matmul. */
334 compute_dot_product (gfc_expr *matrix_a, int stride_a, int offset_a,
335 gfc_expr *matrix_b, int stride_b, int offset_b)
337 gfc_expr *result, *a, *b;
339 result = gfc_get_constant_expr (matrix_a->ts.type, matrix_a->ts.kind,
341 init_result_expr (result, 0, NULL);
343 a = gfc_constructor_lookup_expr (matrix_a->value.constructor, offset_a);
344 b = gfc_constructor_lookup_expr (matrix_b->value.constructor, offset_b);
347 /* Copying of expressions is required as operands are free'd
348 by the gfc_arith routines. */
349 switch (result->ts.type)
352 result = gfc_or (result,
353 gfc_and (gfc_copy_expr (a),
360 result = gfc_add (result,
361 gfc_multiply (gfc_copy_expr (a),
369 offset_a += stride_a;
370 a = gfc_constructor_lookup_expr (matrix_a->value.constructor, offset_a);
372 offset_b += stride_b;
373 b = gfc_constructor_lookup_expr (matrix_b->value.constructor, offset_b);
380 /* Build a result expression for transformational intrinsics,
384 transformational_result (gfc_expr *array, gfc_expr *dim, bt type,
385 int kind, locus* where)
390 if (!dim || array->rank == 1)
391 return gfc_get_constant_expr (type, kind, where);
393 result = gfc_get_array_expr (type, kind, where);
394 result->shape = gfc_copy_shape_excluding (array->shape, array->rank, dim);
395 result->rank = array->rank - 1;
397 /* gfc_array_size() would count the number of elements in the constructor,
398 we have not built those yet. */
400 for (i = 0; i < result->rank; ++i)
401 nelem *= mpz_get_ui (result->shape[i]);
403 for (i = 0; i < nelem; ++i)
405 gfc_constructor_append_expr (&result->value.constructor,
406 gfc_get_constant_expr (type, kind, where),
414 typedef gfc_expr* (*transformational_op)(gfc_expr*, gfc_expr*);
416 /* Wrapper function, implements 'op1 += 1'. Only called if MASK
417 of COUNT intrinsic is .TRUE..
419 Interface and implimentation mimics arith functions as
420 gfc_add, gfc_multiply, etc. */
422 static gfc_expr* gfc_count (gfc_expr *op1, gfc_expr *op2)
426 gcc_assert (op1->ts.type == BT_INTEGER);
427 gcc_assert (op2->ts.type == BT_LOGICAL);
428 gcc_assert (op2->value.logical);
430 result = gfc_copy_expr (op1);
431 mpz_add_ui (result->value.integer, result->value.integer, 1);
439 /* Transforms an ARRAY with operation OP, according to MASK, to a
440 scalar RESULT. E.g. called if
442 REAL, PARAMETER :: array(n, m) = ...
443 REAL, PARAMETER :: s = SUM(array)
445 where OP == gfc_add(). */
448 simplify_transformation_to_scalar (gfc_expr *result, gfc_expr *array, gfc_expr *mask,
449 transformational_op op)
452 gfc_constructor *array_ctor, *mask_ctor;
454 /* Shortcut for constant .FALSE. MASK. */
456 && mask->expr_type == EXPR_CONSTANT
457 && !mask->value.logical)
460 array_ctor = gfc_constructor_first (array->value.constructor);
462 if (mask && mask->expr_type == EXPR_ARRAY)
463 mask_ctor = gfc_constructor_first (mask->value.constructor);
467 a = array_ctor->expr;
468 array_ctor = gfc_constructor_next (array_ctor);
470 /* A constant MASK equals .TRUE. here and can be ignored. */
474 mask_ctor = gfc_constructor_next (mask_ctor);
475 if (!m->value.logical)
479 result = op (result, gfc_copy_expr (a));
485 /* Transforms an ARRAY with operation OP, according to MASK, to an
486 array RESULT. E.g. called if
488 REAL, PARAMETER :: array(n, m) = ...
489 REAL, PARAMETER :: s(n) = PROD(array, DIM=1)
491 where OP == gfc_multiply(). */
494 simplify_transformation_to_array (gfc_expr *result, gfc_expr *array, gfc_expr *dim,
495 gfc_expr *mask, transformational_op op)
498 int done, i, n, arraysize, resultsize, dim_index, dim_extent, dim_stride;
499 gfc_expr **arrayvec, **resultvec, **base, **src, **dest;
500 gfc_constructor *array_ctor, *mask_ctor, *result_ctor;
502 int count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS],
503 sstride[GFC_MAX_DIMENSIONS], dstride[GFC_MAX_DIMENSIONS],
504 tmpstride[GFC_MAX_DIMENSIONS];
506 /* Shortcut for constant .FALSE. MASK. */
508 && mask->expr_type == EXPR_CONSTANT
509 && !mask->value.logical)
512 /* Build an indexed table for array element expressions to minimize
513 linked-list traversal. Masked elements are set to NULL. */
514 gfc_array_size (array, &size);
515 arraysize = mpz_get_ui (size);
517 arrayvec = (gfc_expr**) gfc_getmem (sizeof (gfc_expr*) * arraysize);
519 array_ctor = gfc_constructor_first (array->value.constructor);
521 if (mask && mask->expr_type == EXPR_ARRAY)
522 mask_ctor = gfc_constructor_first (mask->value.constructor);
524 for (i = 0; i < arraysize; ++i)
526 arrayvec[i] = array_ctor->expr;
527 array_ctor = gfc_constructor_next (array_ctor);
531 if (!mask_ctor->expr->value.logical)
534 mask_ctor = gfc_constructor_next (mask_ctor);
538 /* Same for the result expression. */
539 gfc_array_size (result, &size);
540 resultsize = mpz_get_ui (size);
543 resultvec = (gfc_expr**) gfc_getmem (sizeof (gfc_expr*) * resultsize);
544 result_ctor = gfc_constructor_first (result->value.constructor);
545 for (i = 0; i < resultsize; ++i)
547 resultvec[i] = result_ctor->expr;
548 result_ctor = gfc_constructor_next (result_ctor);
551 gfc_extract_int (dim, &dim_index);
552 dim_index -= 1; /* zero-base index */
556 for (i = 0, n = 0; i < array->rank; ++i)
559 tmpstride[i] = (i == 0) ? 1 : tmpstride[i-1] * mpz_get_si (array->shape[i-1]);
562 dim_extent = mpz_get_si (array->shape[i]);
563 dim_stride = tmpstride[i];
567 extent[n] = mpz_get_si (array->shape[i]);
568 sstride[n] = tmpstride[i];
569 dstride[n] = (n == 0) ? 1 : dstride[n-1] * extent[n-1];
578 for (src = base, n = 0; n < dim_extent; src += dim_stride, ++n)
580 *dest = op (*dest, gfc_copy_expr (*src));
587 while (!done && count[n] == extent[n])
590 base -= sstride[n] * extent[n];
591 dest -= dstride[n] * extent[n];
594 if (n < result->rank)
605 /* Place updated expression in result constructor. */
606 result_ctor = gfc_constructor_first (result->value.constructor);
607 for (i = 0; i < resultsize; ++i)
609 result_ctor->expr = resultvec[i];
610 result_ctor = gfc_constructor_next (result_ctor);
614 gfc_free (resultvec);
620 /********************** Simplification functions *****************************/
623 gfc_simplify_abs (gfc_expr *e)
627 if (e->expr_type != EXPR_CONSTANT)
633 result = gfc_get_constant_expr (BT_INTEGER, e->ts.kind, &e->where);
634 mpz_abs (result->value.integer, e->value.integer);
635 return range_check (result, "IABS");
638 result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
639 mpfr_abs (result->value.real, e->value.real, GFC_RND_MODE);
640 return range_check (result, "ABS");
643 gfc_set_model_kind (e->ts.kind);
644 result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
645 mpc_abs (result->value.real, e->value.complex, GFC_RND_MODE);
646 return range_check (result, "CABS");
649 gfc_internal_error ("gfc_simplify_abs(): Bad type");
655 simplify_achar_char (gfc_expr *e, gfc_expr *k, const char *name, bool ascii)
659 bool too_large = false;
661 if (e->expr_type != EXPR_CONSTANT)
664 kind = get_kind (BT_CHARACTER, k, name, gfc_default_character_kind);
666 return &gfc_bad_expr;
668 if (mpz_cmp_si (e->value.integer, 0) < 0)
670 gfc_error ("Argument of %s function at %L is negative", name,
672 return &gfc_bad_expr;
675 if (ascii && gfc_option.warn_surprising
676 && mpz_cmp_si (e->value.integer, 127) > 0)
677 gfc_warning ("Argument of %s function at %L outside of range [0,127]",
680 if (kind == 1 && mpz_cmp_si (e->value.integer, 255) > 0)
685 mpz_init_set_ui (t, 2);
686 mpz_pow_ui (t, t, 32);
687 mpz_sub_ui (t, t, 1);
688 if (mpz_cmp (e->value.integer, t) > 0)
695 gfc_error ("Argument of %s function at %L is too large for the "
696 "collating sequence of kind %d", name, &e->where, kind);
697 return &gfc_bad_expr;
700 result = gfc_get_character_expr (kind, &e->where, NULL, 1);
701 result->value.character.string[0] = mpz_get_ui (e->value.integer);
708 /* We use the processor's collating sequence, because all
709 systems that gfortran currently works on are ASCII. */
712 gfc_simplify_achar (gfc_expr *e, gfc_expr *k)
714 return simplify_achar_char (e, k, "ACHAR", true);
719 gfc_simplify_acos (gfc_expr *x)
723 if (x->expr_type != EXPR_CONSTANT)
729 if (mpfr_cmp_si (x->value.real, 1) > 0
730 || mpfr_cmp_si (x->value.real, -1) < 0)
732 gfc_error ("Argument of ACOS at %L must be between -1 and 1",
734 return &gfc_bad_expr;
736 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
737 mpfr_acos (result->value.real, x->value.real, GFC_RND_MODE);
741 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
742 mpc_acos (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
746 gfc_internal_error ("in gfc_simplify_acos(): Bad type");
749 return range_check (result, "ACOS");
753 gfc_simplify_acosh (gfc_expr *x)
757 if (x->expr_type != EXPR_CONSTANT)
763 if (mpfr_cmp_si (x->value.real, 1) < 0)
765 gfc_error ("Argument of ACOSH at %L must not be less than 1",
767 return &gfc_bad_expr;
770 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
771 mpfr_acosh (result->value.real, x->value.real, GFC_RND_MODE);
775 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
776 mpc_acosh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
780 gfc_internal_error ("in gfc_simplify_acosh(): Bad type");
783 return range_check (result, "ACOSH");
787 gfc_simplify_adjustl (gfc_expr *e)
793 if (e->expr_type != EXPR_CONSTANT)
796 len = e->value.character.length;
798 for (count = 0, i = 0; i < len; ++i)
800 ch = e->value.character.string[i];
806 result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, len);
807 for (i = 0; i < len - count; ++i)
808 result->value.character.string[i] = e->value.character.string[count + i];
815 gfc_simplify_adjustr (gfc_expr *e)
821 if (e->expr_type != EXPR_CONSTANT)
824 len = e->value.character.length;
826 for (count = 0, i = len - 1; i >= 0; --i)
828 ch = e->value.character.string[i];
834 result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, len);
835 for (i = 0; i < count; ++i)
836 result->value.character.string[i] = ' ';
838 for (i = count; i < len; ++i)
839 result->value.character.string[i] = e->value.character.string[i - count];
846 gfc_simplify_aimag (gfc_expr *e)
850 if (e->expr_type != EXPR_CONSTANT)
853 result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
854 mpfr_set (result->value.real, mpc_imagref (e->value.complex), GFC_RND_MODE);
856 return range_check (result, "AIMAG");
861 gfc_simplify_aint (gfc_expr *e, gfc_expr *k)
863 gfc_expr *rtrunc, *result;
866 kind = get_kind (BT_REAL, k, "AINT", e->ts.kind);
868 return &gfc_bad_expr;
870 if (e->expr_type != EXPR_CONSTANT)
873 rtrunc = gfc_copy_expr (e);
874 mpfr_trunc (rtrunc->value.real, e->value.real);
876 result = gfc_real2real (rtrunc, kind);
878 gfc_free_expr (rtrunc);
880 return range_check (result, "AINT");
885 gfc_simplify_all (gfc_expr *mask, gfc_expr *dim)
889 if (!is_constant_array_expr (mask)
890 || !gfc_is_constant_expr (dim))
893 result = transformational_result (mask, dim, mask->ts.type,
894 mask->ts.kind, &mask->where);
895 init_result_expr (result, true, NULL);
897 return !dim || mask->rank == 1 ?
898 simplify_transformation_to_scalar (result, mask, NULL, gfc_and) :
899 simplify_transformation_to_array (result, mask, dim, NULL, gfc_and);
904 gfc_simplify_dint (gfc_expr *e)
906 gfc_expr *rtrunc, *result;
908 if (e->expr_type != EXPR_CONSTANT)
911 rtrunc = gfc_copy_expr (e);
912 mpfr_trunc (rtrunc->value.real, e->value.real);
914 result = gfc_real2real (rtrunc, gfc_default_double_kind);
916 gfc_free_expr (rtrunc);
918 return range_check (result, "DINT");
923 gfc_simplify_anint (gfc_expr *e, gfc_expr *k)
928 kind = get_kind (BT_REAL, k, "ANINT", e->ts.kind);
930 return &gfc_bad_expr;
932 if (e->expr_type != EXPR_CONSTANT)
935 result = gfc_get_constant_expr (e->ts.type, kind, &e->where);
936 mpfr_round (result->value.real, e->value.real);
938 return range_check (result, "ANINT");
943 gfc_simplify_and (gfc_expr *x, gfc_expr *y)
948 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
951 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
956 result = gfc_get_constant_expr (BT_INTEGER, kind, &x->where);
957 mpz_and (result->value.integer, x->value.integer, y->value.integer);
958 return range_check (result, "AND");
961 return gfc_get_logical_expr (kind, &x->where,
962 x->value.logical && y->value.logical);
971 gfc_simplify_any (gfc_expr *mask, gfc_expr *dim)
975 if (!is_constant_array_expr (mask)
976 || !gfc_is_constant_expr (dim))
979 result = transformational_result (mask, dim, mask->ts.type,
980 mask->ts.kind, &mask->where);
981 init_result_expr (result, false, NULL);
983 return !dim || mask->rank == 1 ?
984 simplify_transformation_to_scalar (result, mask, NULL, gfc_or) :
985 simplify_transformation_to_array (result, mask, dim, NULL, gfc_or);
990 gfc_simplify_dnint (gfc_expr *e)
994 if (e->expr_type != EXPR_CONSTANT)
997 result = gfc_get_constant_expr (BT_REAL, gfc_default_double_kind, &e->where);
998 mpfr_round (result->value.real, e->value.real);
1000 return range_check (result, "DNINT");
1005 gfc_simplify_asin (gfc_expr *x)
1009 if (x->expr_type != EXPR_CONSTANT)
1015 if (mpfr_cmp_si (x->value.real, 1) > 0
1016 || mpfr_cmp_si (x->value.real, -1) < 0)
1018 gfc_error ("Argument of ASIN at %L must be between -1 and 1",
1020 return &gfc_bad_expr;
1022 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1023 mpfr_asin (result->value.real, x->value.real, GFC_RND_MODE);
1027 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1028 mpc_asin (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1032 gfc_internal_error ("in gfc_simplify_asin(): Bad type");
1035 return range_check (result, "ASIN");
1040 gfc_simplify_asinh (gfc_expr *x)
1044 if (x->expr_type != EXPR_CONSTANT)
1047 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1052 mpfr_asinh (result->value.real, x->value.real, GFC_RND_MODE);
1056 mpc_asinh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1060 gfc_internal_error ("in gfc_simplify_asinh(): Bad type");
1063 return range_check (result, "ASINH");
1068 gfc_simplify_atan (gfc_expr *x)
1072 if (x->expr_type != EXPR_CONSTANT)
1075 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1080 mpfr_atan (result->value.real, x->value.real, GFC_RND_MODE);
1084 mpc_atan (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1088 gfc_internal_error ("in gfc_simplify_atan(): Bad type");
1091 return range_check (result, "ATAN");
1096 gfc_simplify_atanh (gfc_expr *x)
1100 if (x->expr_type != EXPR_CONSTANT)
1106 if (mpfr_cmp_si (x->value.real, 1) >= 0
1107 || mpfr_cmp_si (x->value.real, -1) <= 0)
1109 gfc_error ("Argument of ATANH at %L must be inside the range -1 "
1111 return &gfc_bad_expr;
1113 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1114 mpfr_atanh (result->value.real, x->value.real, GFC_RND_MODE);
1118 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1119 mpc_atanh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1123 gfc_internal_error ("in gfc_simplify_atanh(): Bad type");
1126 return range_check (result, "ATANH");
1131 gfc_simplify_atan2 (gfc_expr *y, gfc_expr *x)
1135 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1138 if (mpfr_sgn (y->value.real) == 0 && mpfr_sgn (x->value.real) == 0)
1140 gfc_error ("If first argument of ATAN2 %L is zero, then the "
1141 "second argument must not be zero", &x->where);
1142 return &gfc_bad_expr;
1145 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1146 mpfr_atan2 (result->value.real, y->value.real, x->value.real, GFC_RND_MODE);
1148 return range_check (result, "ATAN2");
1153 gfc_simplify_bessel_j0 (gfc_expr *x)
1157 if (x->expr_type != EXPR_CONSTANT)
1160 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1161 mpfr_j0 (result->value.real, x->value.real, GFC_RND_MODE);
1163 return range_check (result, "BESSEL_J0");
1168 gfc_simplify_bessel_j1 (gfc_expr *x)
1172 if (x->expr_type != EXPR_CONSTANT)
1175 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1176 mpfr_j1 (result->value.real, x->value.real, GFC_RND_MODE);
1178 return range_check (result, "BESSEL_J1");
1183 gfc_simplify_bessel_jn (gfc_expr *order, gfc_expr *x)
1188 if (x->expr_type != EXPR_CONSTANT || order->expr_type != EXPR_CONSTANT)
1191 n = mpz_get_si (order->value.integer);
1192 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1193 mpfr_jn (result->value.real, n, x->value.real, GFC_RND_MODE);
1195 return range_check (result, "BESSEL_JN");
1199 /* Simplify transformational form of JN and YN. */
1202 gfc_simplify_bessel_n2 (gfc_expr *order1, gfc_expr *order2, gfc_expr *x,
1209 mpfr_t x2rev, last1, last2;
1211 if (x->expr_type != EXPR_CONSTANT || order1->expr_type != EXPR_CONSTANT
1212 || order2->expr_type != EXPR_CONSTANT)
1215 n1 = mpz_get_si (order1->value.integer);
1216 n2 = mpz_get_si (order2->value.integer);
1217 result = gfc_get_array_expr (x->ts.type, x->ts.kind, &x->where);
1219 result->shape = gfc_get_shape (1);
1220 mpz_init_set_ui (result->shape[0], MAX (n2-n1+1, 0));
1225 /* Special case: x == 0; it is J0(0.0) == 1, JN(N > 0, 0.0) == 0; and
1226 YN(N, 0.0) = -Inf. */
1228 if (mpfr_cmp_ui (x->value.real, 0.0) == 0)
1230 if (!jn && gfc_option.flag_range_check)
1232 gfc_error ("Result of BESSEL_YN is -INF at %L", &result->where);
1233 gfc_free_expr (result);
1234 return &gfc_bad_expr;
1239 e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1240 mpfr_set_ui (e->value.real, 1.0, GFC_RND_MODE);
1241 gfc_constructor_append_expr (&result->value.constructor, e,
1246 for (i = n1; i <= n2; i++)
1248 e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1250 mpfr_set_ui (e->value.real, 0.0, GFC_RND_MODE);
1252 mpfr_set_inf (e->value.real, -1);
1253 gfc_constructor_append_expr (&result->value.constructor, e,
1260 /* Use the faster but more verbose recurrence algorithm. Bessel functions
1261 are stable for downward recursion and Neumann functions are stable
1262 for upward recursion. It is
1264 J(N-1, x) = x2rev * N * J(N, x) - J(N+1, x),
1265 Y(N+1, x) = x2rev * N * Y(N, x) - Y(N-1, x).
1266 Cf. http://dlmf.nist.gov/10.74#iv and http://dlmf.nist.gov/10.6#E1 */
1268 gfc_set_model_kind (x->ts.kind);
1270 /* Get first recursion anchor. */
1274 mpfr_jn (last1, n2, x->value.real, GFC_RND_MODE);
1276 mpfr_yn (last1, n1, x->value.real, GFC_RND_MODE);
1278 e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1279 mpfr_set (e->value.real, last1, GFC_RND_MODE);
1280 if (range_check (e, jn ? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr)
1284 gfc_free_expr (result);
1285 return &gfc_bad_expr;
1287 gfc_constructor_append_expr (&result->value.constructor, e, &x->where);
1295 /* Get second recursion anchor. */
1299 mpfr_jn (last2, n2-1, x->value.real, GFC_RND_MODE);
1301 mpfr_yn (last2, n1+1, x->value.real, GFC_RND_MODE);
1303 e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1304 mpfr_set (e->value.real, last2, GFC_RND_MODE);
1305 if (range_check (e, jn ? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr)
1310 gfc_free_expr (result);
1311 return &gfc_bad_expr;
1314 gfc_constructor_insert_expr (&result->value.constructor, e, &x->where, -2);
1316 gfc_constructor_append_expr (&result->value.constructor, e, &x->where);
1325 /* Start actual recursion. */
1328 mpfr_ui_div (x2rev, 2, x->value.real, GFC_RND_MODE);
1330 for (i = 2; i <= n2-n1; i++)
1332 e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1334 /* Special case: For YN, if the previous N gave -INF, set
1335 also N+1 to -INF. */
1336 if (!jn && !gfc_option.flag_range_check && mpfr_inf_p (last2))
1338 mpfr_set_inf (e->value.real, -1);
1339 gfc_constructor_append_expr (&result->value.constructor, e,
1344 mpfr_mul_si (e->value.real, x2rev, jn ? (n2-i+1) : (n1+i-1),
1346 mpfr_mul (e->value.real, e->value.real, last2, GFC_RND_MODE);
1347 mpfr_sub (e->value.real, e->value.real, last1, GFC_RND_MODE);
1349 if (range_check (e, jn ? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr)
1353 gfc_constructor_insert_expr (&result->value.constructor, e, &x->where,
1356 gfc_constructor_append_expr (&result->value.constructor, e, &x->where);
1358 mpfr_set (last1, last2, GFC_RND_MODE);
1359 mpfr_set (last2, e->value.real, GFC_RND_MODE);
1372 gfc_free_expr (result);
1373 return &gfc_bad_expr;
1378 gfc_simplify_bessel_jn2 (gfc_expr *order1, gfc_expr *order2, gfc_expr *x)
1380 return gfc_simplify_bessel_n2 (order1, order2, x, true);
1385 gfc_simplify_bessel_y0 (gfc_expr *x)
1389 if (x->expr_type != EXPR_CONSTANT)
1392 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1393 mpfr_y0 (result->value.real, x->value.real, GFC_RND_MODE);
1395 return range_check (result, "BESSEL_Y0");
1400 gfc_simplify_bessel_y1 (gfc_expr *x)
1404 if (x->expr_type != EXPR_CONSTANT)
1407 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1408 mpfr_y1 (result->value.real, x->value.real, GFC_RND_MODE);
1410 return range_check (result, "BESSEL_Y1");
1415 gfc_simplify_bessel_yn (gfc_expr *order, gfc_expr *x)
1420 if (x->expr_type != EXPR_CONSTANT || order->expr_type != EXPR_CONSTANT)
1423 n = mpz_get_si (order->value.integer);
1424 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1425 mpfr_yn (result->value.real, n, x->value.real, GFC_RND_MODE);
1427 return range_check (result, "BESSEL_YN");
1432 gfc_simplify_bessel_yn2 (gfc_expr *order1, gfc_expr *order2, gfc_expr *x)
1434 return gfc_simplify_bessel_n2 (order1, order2, x, false);
1439 gfc_simplify_bit_size (gfc_expr *e)
1441 int i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
1442 return gfc_get_int_expr (e->ts.kind, &e->where,
1443 gfc_integer_kinds[i].bit_size);
1448 gfc_simplify_btest (gfc_expr *e, gfc_expr *bit)
1452 if (e->expr_type != EXPR_CONSTANT || bit->expr_type != EXPR_CONSTANT)
1455 if (gfc_extract_int (bit, &b) != NULL || b < 0)
1456 return gfc_get_logical_expr (gfc_default_logical_kind, &e->where, false);
1458 return gfc_get_logical_expr (gfc_default_logical_kind, &e->where,
1459 mpz_tstbit (e->value.integer, b));
1464 gfc_simplify_ceiling (gfc_expr *e, gfc_expr *k)
1466 gfc_expr *ceil, *result;
1469 kind = get_kind (BT_INTEGER, k, "CEILING", gfc_default_integer_kind);
1471 return &gfc_bad_expr;
1473 if (e->expr_type != EXPR_CONSTANT)
1476 ceil = gfc_copy_expr (e);
1477 mpfr_ceil (ceil->value.real, e->value.real);
1479 result = gfc_get_constant_expr (BT_INTEGER, kind, &e->where);
1480 gfc_mpfr_to_mpz (result->value.integer, ceil->value.real, &e->where);
1482 gfc_free_expr (ceil);
1484 return range_check (result, "CEILING");
1489 gfc_simplify_char (gfc_expr *e, gfc_expr *k)
1491 return simplify_achar_char (e, k, "CHAR", false);
1495 /* Common subroutine for simplifying CMPLX, COMPLEX and DCMPLX. */
1498 simplify_cmplx (const char *name, gfc_expr *x, gfc_expr *y, int kind)
1502 if (convert_boz (x, kind) == &gfc_bad_expr)
1503 return &gfc_bad_expr;
1505 if (convert_boz (y, kind) == &gfc_bad_expr)
1506 return &gfc_bad_expr;
1508 if (x->expr_type != EXPR_CONSTANT
1509 || (y != NULL && y->expr_type != EXPR_CONSTANT))
1512 result = gfc_get_constant_expr (BT_COMPLEX, kind, &x->where);
1517 mpc_set_z (result->value.complex, x->value.integer, GFC_MPC_RND_MODE);
1521 mpc_set_fr (result->value.complex, x->value.real, GFC_RND_MODE);
1525 mpc_set (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1529 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (x)");
1533 return range_check (result, name);
1538 mpfr_set_z (mpc_imagref (result->value.complex),
1539 y->value.integer, GFC_RND_MODE);
1543 mpfr_set (mpc_imagref (result->value.complex),
1544 y->value.real, GFC_RND_MODE);
1548 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (y)");
1551 return range_check (result, name);
1556 gfc_simplify_cmplx (gfc_expr *x, gfc_expr *y, gfc_expr *k)
1560 kind = get_kind (BT_REAL, k, "CMPLX", gfc_default_complex_kind);
1562 return &gfc_bad_expr;
1564 return simplify_cmplx ("CMPLX", x, y, kind);
1569 gfc_simplify_complex (gfc_expr *x, gfc_expr *y)
1573 if (x->ts.type == BT_INTEGER && y->ts.type == BT_INTEGER)
1574 kind = gfc_default_complex_kind;
1575 else if (x->ts.type == BT_REAL || y->ts.type == BT_INTEGER)
1577 else if (x->ts.type == BT_INTEGER || y->ts.type == BT_REAL)
1579 else if (x->ts.type == BT_REAL && y->ts.type == BT_REAL)
1580 kind = (x->ts.kind > y->ts.kind) ? x->ts.kind : y->ts.kind;
1584 return simplify_cmplx ("COMPLEX", x, y, kind);
1589 gfc_simplify_conjg (gfc_expr *e)
1593 if (e->expr_type != EXPR_CONSTANT)
1596 result = gfc_copy_expr (e);
1597 mpc_conj (result->value.complex, result->value.complex, GFC_MPC_RND_MODE);
1599 return range_check (result, "CONJG");
1604 gfc_simplify_cos (gfc_expr *x)
1608 if (x->expr_type != EXPR_CONSTANT)
1611 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1616 mpfr_cos (result->value.real, x->value.real, GFC_RND_MODE);
1620 gfc_set_model_kind (x->ts.kind);
1621 mpc_cos (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1625 gfc_internal_error ("in gfc_simplify_cos(): Bad type");
1628 return range_check (result, "COS");
1633 gfc_simplify_cosh (gfc_expr *x)
1637 if (x->expr_type != EXPR_CONSTANT)
1640 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1645 mpfr_cosh (result->value.real, x->value.real, GFC_RND_MODE);
1649 mpc_cosh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1656 return range_check (result, "COSH");
1661 gfc_simplify_count (gfc_expr *mask, gfc_expr *dim, gfc_expr *kind)
1665 if (!is_constant_array_expr (mask)
1666 || !gfc_is_constant_expr (dim)
1667 || !gfc_is_constant_expr (kind))
1670 result = transformational_result (mask, dim,
1672 get_kind (BT_INTEGER, kind, "COUNT",
1673 gfc_default_integer_kind),
1676 init_result_expr (result, 0, NULL);
1678 /* Passing MASK twice, once as data array, once as mask.
1679 Whenever gfc_count is called, '1' is added to the result. */
1680 return !dim || mask->rank == 1 ?
1681 simplify_transformation_to_scalar (result, mask, mask, gfc_count) :
1682 simplify_transformation_to_array (result, mask, dim, mask, gfc_count);
1687 gfc_simplify_dcmplx (gfc_expr *x, gfc_expr *y)
1689 return simplify_cmplx ("DCMPLX", x, y, gfc_default_double_kind);
1694 gfc_simplify_dble (gfc_expr *e)
1696 gfc_expr *result = NULL;
1698 if (e->expr_type != EXPR_CONSTANT)
1701 if (convert_boz (e, gfc_default_double_kind) == &gfc_bad_expr)
1702 return &gfc_bad_expr;
1704 result = gfc_convert_constant (e, BT_REAL, gfc_default_double_kind);
1705 if (result == &gfc_bad_expr)
1706 return &gfc_bad_expr;
1708 return range_check (result, "DBLE");
1713 gfc_simplify_digits (gfc_expr *x)
1717 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
1722 digits = gfc_integer_kinds[i].digits;
1727 digits = gfc_real_kinds[i].digits;
1734 return gfc_get_int_expr (gfc_default_integer_kind, NULL, digits);
1739 gfc_simplify_dim (gfc_expr *x, gfc_expr *y)
1744 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1747 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
1748 result = gfc_get_constant_expr (x->ts.type, kind, &x->where);
1753 if (mpz_cmp (x->value.integer, y->value.integer) > 0)
1754 mpz_sub (result->value.integer, x->value.integer, y->value.integer);
1756 mpz_set_ui (result->value.integer, 0);
1761 if (mpfr_cmp (x->value.real, y->value.real) > 0)
1762 mpfr_sub (result->value.real, x->value.real, y->value.real,
1765 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
1770 gfc_internal_error ("gfc_simplify_dim(): Bad type");
1773 return range_check (result, "DIM");
1778 gfc_simplify_dot_product (gfc_expr *vector_a, gfc_expr *vector_b)
1780 if (!is_constant_array_expr (vector_a)
1781 || !is_constant_array_expr (vector_b))
1784 gcc_assert (vector_a->rank == 1);
1785 gcc_assert (vector_b->rank == 1);
1786 gcc_assert (gfc_compare_types (&vector_a->ts, &vector_b->ts));
1788 return compute_dot_product (vector_a, 1, 0, vector_b, 1, 0);
1793 gfc_simplify_dprod (gfc_expr *x, gfc_expr *y)
1795 gfc_expr *a1, *a2, *result;
1797 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1800 a1 = gfc_real2real (x, gfc_default_double_kind);
1801 a2 = gfc_real2real (y, gfc_default_double_kind);
1803 result = gfc_get_constant_expr (BT_REAL, gfc_default_double_kind, &x->where);
1804 mpfr_mul (result->value.real, a1->value.real, a2->value.real, GFC_RND_MODE);
1809 return range_check (result, "DPROD");
1814 gfc_simplify_erf (gfc_expr *x)
1818 if (x->expr_type != EXPR_CONSTANT)
1821 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1822 mpfr_erf (result->value.real, x->value.real, GFC_RND_MODE);
1824 return range_check (result, "ERF");
1829 gfc_simplify_erfc (gfc_expr *x)
1833 if (x->expr_type != EXPR_CONSTANT)
1836 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1837 mpfr_erfc (result->value.real, x->value.real, GFC_RND_MODE);
1839 return range_check (result, "ERFC");
1843 /* Helper functions to simplify ERFC_SCALED(x) = ERFC(x) * EXP(X**2). */
1845 #define MAX_ITER 200
1846 #define ARG_LIMIT 12
1848 /* Calculate ERFC_SCALED directly by its definition:
1850 ERFC_SCALED(x) = ERFC(x) * EXP(X**2)
1852 using a large precision for intermediate results. This is used for all
1853 but large values of the argument. */
1855 fullprec_erfc_scaled (mpfr_t res, mpfr_t arg)
1860 prec = mpfr_get_default_prec ();
1861 mpfr_set_default_prec (10 * prec);
1866 mpfr_set (a, arg, GFC_RND_MODE);
1867 mpfr_sqr (b, a, GFC_RND_MODE);
1868 mpfr_exp (b, b, GFC_RND_MODE);
1869 mpfr_erfc (a, a, GFC_RND_MODE);
1870 mpfr_mul (a, a, b, GFC_RND_MODE);
1872 mpfr_set (res, a, GFC_RND_MODE);
1873 mpfr_set_default_prec (prec);
1879 /* Calculate ERFC_SCALED using a power series expansion in 1/arg:
1881 ERFC_SCALED(x) = 1 / (x * sqrt(pi))
1882 * (1 + Sum_n (-1)**n * (1 * 3 * 5 * ... * (2n-1))
1885 This is used for large values of the argument. Intermediate calculations
1886 are performed with twice the precision. We don't do a fixed number of
1887 iterations of the sum, but stop when it has converged to the required
1890 asympt_erfc_scaled (mpfr_t res, mpfr_t arg)
1892 mpfr_t sum, x, u, v, w, oldsum, sumtrunc;
1897 prec = mpfr_get_default_prec ();
1898 mpfr_set_default_prec (2 * prec);
1908 mpfr_init (sumtrunc);
1909 mpfr_set_prec (oldsum, prec);
1910 mpfr_set_prec (sumtrunc, prec);
1912 mpfr_set (x, arg, GFC_RND_MODE);
1913 mpfr_set_ui (sum, 1, GFC_RND_MODE);
1914 mpz_set_ui (num, 1);
1916 mpfr_set (u, x, GFC_RND_MODE);
1917 mpfr_sqr (u, u, GFC_RND_MODE);
1918 mpfr_mul_ui (u, u, 2, GFC_RND_MODE);
1919 mpfr_pow_si (u, u, -1, GFC_RND_MODE);
1921 for (i = 1; i < MAX_ITER; i++)
1923 mpfr_set (oldsum, sum, GFC_RND_MODE);
1925 mpz_mul_ui (num, num, 2 * i - 1);
1928 mpfr_set (w, u, GFC_RND_MODE);
1929 mpfr_pow_ui (w, w, i, GFC_RND_MODE);
1931 mpfr_set_z (v, num, GFC_RND_MODE);
1932 mpfr_mul (v, v, w, GFC_RND_MODE);
1934 mpfr_add (sum, sum, v, GFC_RND_MODE);
1936 mpfr_set (sumtrunc, sum, GFC_RND_MODE);
1937 if (mpfr_cmp (sumtrunc, oldsum) == 0)
1941 /* We should have converged by now; otherwise, ARG_LIMIT is probably
1943 gcc_assert (i < MAX_ITER);
1945 /* Divide by x * sqrt(Pi). */
1946 mpfr_const_pi (u, GFC_RND_MODE);
1947 mpfr_sqrt (u, u, GFC_RND_MODE);
1948 mpfr_mul (u, u, x, GFC_RND_MODE);
1949 mpfr_div (sum, sum, u, GFC_RND_MODE);
1951 mpfr_set (res, sum, GFC_RND_MODE);
1952 mpfr_set_default_prec (prec);
1954 mpfr_clears (sum, x, u, v, w, oldsum, sumtrunc, NULL);
1960 gfc_simplify_erfc_scaled (gfc_expr *x)
1964 if (x->expr_type != EXPR_CONSTANT)
1967 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1968 if (mpfr_cmp_d (x->value.real, ARG_LIMIT) >= 0)
1969 asympt_erfc_scaled (result->value.real, x->value.real);
1971 fullprec_erfc_scaled (result->value.real, x->value.real);
1973 return range_check (result, "ERFC_SCALED");
1981 gfc_simplify_epsilon (gfc_expr *e)
1986 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
1988 result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
1989 mpfr_set (result->value.real, gfc_real_kinds[i].epsilon, GFC_RND_MODE);
1991 return range_check (result, "EPSILON");
1996 gfc_simplify_exp (gfc_expr *x)
2000 if (x->expr_type != EXPR_CONSTANT)
2003 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2008 mpfr_exp (result->value.real, x->value.real, GFC_RND_MODE);
2012 gfc_set_model_kind (x->ts.kind);
2013 mpc_exp (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
2017 gfc_internal_error ("in gfc_simplify_exp(): Bad type");
2020 return range_check (result, "EXP");
2025 gfc_simplify_exponent (gfc_expr *x)
2030 if (x->expr_type != EXPR_CONSTANT)
2033 result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
2036 gfc_set_model (x->value.real);
2038 if (mpfr_sgn (x->value.real) == 0)
2040 mpz_set_ui (result->value.integer, 0);
2044 i = (int) mpfr_get_exp (x->value.real);
2045 mpz_set_si (result->value.integer, i);
2047 return range_check (result, "EXPONENT");
2052 gfc_simplify_float (gfc_expr *a)
2056 if (a->expr_type != EXPR_CONSTANT)
2061 if (convert_boz (a, gfc_default_real_kind) == &gfc_bad_expr)
2062 return &gfc_bad_expr;
2064 result = gfc_copy_expr (a);
2067 result = gfc_int2real (a, gfc_default_real_kind);
2069 return range_check (result, "FLOAT");
2074 gfc_simplify_floor (gfc_expr *e, gfc_expr *k)
2080 kind = get_kind (BT_INTEGER, k, "FLOOR", gfc_default_integer_kind);
2082 gfc_internal_error ("gfc_simplify_floor(): Bad kind");
2084 if (e->expr_type != EXPR_CONSTANT)
2087 gfc_set_model_kind (kind);
2090 mpfr_floor (floor, e->value.real);
2092 result = gfc_get_constant_expr (BT_INTEGER, kind, &e->where);
2093 gfc_mpfr_to_mpz (result->value.integer, floor, &e->where);
2097 return range_check (result, "FLOOR");
2102 gfc_simplify_fraction (gfc_expr *x)
2105 mpfr_t absv, exp, pow2;
2107 if (x->expr_type != EXPR_CONSTANT)
2110 result = gfc_get_constant_expr (BT_REAL, x->ts.kind, &x->where);
2112 if (mpfr_sgn (x->value.real) == 0)
2114 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
2118 gfc_set_model_kind (x->ts.kind);
2123 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
2124 mpfr_log2 (exp, absv, GFC_RND_MODE);
2126 mpfr_trunc (exp, exp);
2127 mpfr_add_ui (exp, exp, 1, GFC_RND_MODE);
2129 mpfr_ui_pow (pow2, 2, exp, GFC_RND_MODE);
2131 mpfr_div (result->value.real, absv, pow2, GFC_RND_MODE);
2133 mpfr_clears (exp, absv, pow2, NULL);
2135 return range_check (result, "FRACTION");
2140 gfc_simplify_gamma (gfc_expr *x)
2144 if (x->expr_type != EXPR_CONSTANT)
2147 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2148 mpfr_gamma (result->value.real, x->value.real, GFC_RND_MODE);
2150 return range_check (result, "GAMMA");
2155 gfc_simplify_huge (gfc_expr *e)
2160 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2161 result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
2166 mpz_set (result->value.integer, gfc_integer_kinds[i].huge);
2170 mpfr_set (result->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
2182 gfc_simplify_hypot (gfc_expr *x, gfc_expr *y)
2186 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2189 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2190 mpfr_hypot (result->value.real, x->value.real, y->value.real, GFC_RND_MODE);
2191 return range_check (result, "HYPOT");
2195 /* We use the processor's collating sequence, because all
2196 systems that gfortran currently works on are ASCII. */
2199 gfc_simplify_iachar (gfc_expr *e, gfc_expr *kind)
2205 if (e->expr_type != EXPR_CONSTANT)
2208 if (e->value.character.length != 1)
2210 gfc_error ("Argument of IACHAR at %L must be of length one", &e->where);
2211 return &gfc_bad_expr;
2214 index = e->value.character.string[0];
2216 if (gfc_option.warn_surprising && index > 127)
2217 gfc_warning ("Argument of IACHAR function at %L outside of range 0..127",
2220 k = get_kind (BT_INTEGER, kind, "IACHAR", gfc_default_integer_kind);
2222 return &gfc_bad_expr;
2224 result = gfc_get_int_expr (k, &e->where, index);
2226 return range_check (result, "IACHAR");
2231 gfc_simplify_iand (gfc_expr *x, gfc_expr *y)
2235 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2238 result = gfc_get_constant_expr (BT_INTEGER, x->ts.kind, &x->where);
2239 mpz_and (result->value.integer, x->value.integer, y->value.integer);
2241 return range_check (result, "IAND");
2246 gfc_simplify_ibclr (gfc_expr *x, gfc_expr *y)
2251 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2254 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
2256 gfc_error ("Invalid second argument of IBCLR at %L", &y->where);
2257 return &gfc_bad_expr;
2260 k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
2262 if (pos >= gfc_integer_kinds[k].bit_size)
2264 gfc_error ("Second argument of IBCLR exceeds bit size at %L",
2266 return &gfc_bad_expr;
2269 result = gfc_copy_expr (x);
2271 convert_mpz_to_unsigned (result->value.integer,
2272 gfc_integer_kinds[k].bit_size);
2274 mpz_clrbit (result->value.integer, pos);
2276 convert_mpz_to_signed (result->value.integer,
2277 gfc_integer_kinds[k].bit_size);
2284 gfc_simplify_ibits (gfc_expr *x, gfc_expr *y, gfc_expr *z)
2291 if (x->expr_type != EXPR_CONSTANT
2292 || y->expr_type != EXPR_CONSTANT
2293 || z->expr_type != EXPR_CONSTANT)
2296 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
2298 gfc_error ("Invalid second argument of IBITS at %L", &y->where);
2299 return &gfc_bad_expr;
2302 if (gfc_extract_int (z, &len) != NULL || len < 0)
2304 gfc_error ("Invalid third argument of IBITS at %L", &z->where);
2305 return &gfc_bad_expr;
2308 k = gfc_validate_kind (BT_INTEGER, x->ts.kind, false);
2310 bitsize = gfc_integer_kinds[k].bit_size;
2312 if (pos + len > bitsize)
2314 gfc_error ("Sum of second and third arguments of IBITS exceeds "
2315 "bit size at %L", &y->where);
2316 return &gfc_bad_expr;
2319 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2320 convert_mpz_to_unsigned (result->value.integer,
2321 gfc_integer_kinds[k].bit_size);
2323 bits = XCNEWVEC (int, bitsize);
2325 for (i = 0; i < bitsize; i++)
2328 for (i = 0; i < len; i++)
2329 bits[i] = mpz_tstbit (x->value.integer, i + pos);
2331 for (i = 0; i < bitsize; i++)
2334 mpz_clrbit (result->value.integer, i);
2335 else if (bits[i] == 1)
2336 mpz_setbit (result->value.integer, i);
2338 gfc_internal_error ("IBITS: Bad bit");
2343 convert_mpz_to_signed (result->value.integer,
2344 gfc_integer_kinds[k].bit_size);
2351 gfc_simplify_ibset (gfc_expr *x, gfc_expr *y)
2356 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2359 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
2361 gfc_error ("Invalid second argument of IBSET at %L", &y->where);
2362 return &gfc_bad_expr;
2365 k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
2367 if (pos >= gfc_integer_kinds[k].bit_size)
2369 gfc_error ("Second argument of IBSET exceeds bit size at %L",
2371 return &gfc_bad_expr;
2374 result = gfc_copy_expr (x);
2376 convert_mpz_to_unsigned (result->value.integer,
2377 gfc_integer_kinds[k].bit_size);
2379 mpz_setbit (result->value.integer, pos);
2381 convert_mpz_to_signed (result->value.integer,
2382 gfc_integer_kinds[k].bit_size);
2389 gfc_simplify_ichar (gfc_expr *e, gfc_expr *kind)
2395 if (e->expr_type != EXPR_CONSTANT)
2398 if (e->value.character.length != 1)
2400 gfc_error ("Argument of ICHAR at %L must be of length one", &e->where);
2401 return &gfc_bad_expr;
2404 index = e->value.character.string[0];
2406 k = get_kind (BT_INTEGER, kind, "ICHAR", gfc_default_integer_kind);
2408 return &gfc_bad_expr;
2410 result = gfc_get_int_expr (k, &e->where, index);
2412 return range_check (result, "ICHAR");
2417 gfc_simplify_ieor (gfc_expr *x, gfc_expr *y)
2421 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2424 result = gfc_get_constant_expr (BT_INTEGER, x->ts.kind, &x->where);
2425 mpz_xor (result->value.integer, x->value.integer, y->value.integer);
2427 return range_check (result, "IEOR");
2432 gfc_simplify_index (gfc_expr *x, gfc_expr *y, gfc_expr *b, gfc_expr *kind)
2435 int back, len, lensub;
2436 int i, j, k, count, index = 0, start;
2438 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT
2439 || ( b != NULL && b->expr_type != EXPR_CONSTANT))
2442 if (b != NULL && b->value.logical != 0)
2447 k = get_kind (BT_INTEGER, kind, "INDEX", gfc_default_integer_kind);
2449 return &gfc_bad_expr;
2451 result = gfc_get_constant_expr (BT_INTEGER, k, &x->where);
2453 len = x->value.character.length;
2454 lensub = y->value.character.length;
2458 mpz_set_si (result->value.integer, 0);
2466 mpz_set_si (result->value.integer, 1);
2469 else if (lensub == 1)
2471 for (i = 0; i < len; i++)
2473 for (j = 0; j < lensub; j++)
2475 if (y->value.character.string[j]
2476 == x->value.character.string[i])
2486 for (i = 0; i < len; i++)
2488 for (j = 0; j < lensub; j++)
2490 if (y->value.character.string[j]
2491 == x->value.character.string[i])
2496 for (k = 0; k < lensub; k++)
2498 if (y->value.character.string[k]
2499 == x->value.character.string[k + start])
2503 if (count == lensub)
2518 mpz_set_si (result->value.integer, len + 1);
2521 else if (lensub == 1)
2523 for (i = 0; i < len; i++)
2525 for (j = 0; j < lensub; j++)
2527 if (y->value.character.string[j]
2528 == x->value.character.string[len - i])
2530 index = len - i + 1;
2538 for (i = 0; i < len; i++)
2540 for (j = 0; j < lensub; j++)
2542 if (y->value.character.string[j]
2543 == x->value.character.string[len - i])
2546 if (start <= len - lensub)
2549 for (k = 0; k < lensub; k++)
2550 if (y->value.character.string[k]
2551 == x->value.character.string[k + start])
2554 if (count == lensub)
2571 mpz_set_si (result->value.integer, index);
2572 return range_check (result, "INDEX");
2577 simplify_intconv (gfc_expr *e, int kind, const char *name)
2579 gfc_expr *result = NULL;
2581 if (e->expr_type != EXPR_CONSTANT)
2584 result = gfc_convert_constant (e, BT_INTEGER, kind);
2585 if (result == &gfc_bad_expr)
2586 return &gfc_bad_expr;
2588 return range_check (result, name);
2593 gfc_simplify_int (gfc_expr *e, gfc_expr *k)
2597 kind = get_kind (BT_INTEGER, k, "INT", gfc_default_integer_kind);
2599 return &gfc_bad_expr;
2601 return simplify_intconv (e, kind, "INT");
2605 gfc_simplify_int2 (gfc_expr *e)
2607 return simplify_intconv (e, 2, "INT2");
2612 gfc_simplify_int8 (gfc_expr *e)
2614 return simplify_intconv (e, 8, "INT8");
2619 gfc_simplify_long (gfc_expr *e)
2621 return simplify_intconv (e, 4, "LONG");
2626 gfc_simplify_ifix (gfc_expr *e)
2628 gfc_expr *rtrunc, *result;
2630 if (e->expr_type != EXPR_CONSTANT)
2633 rtrunc = gfc_copy_expr (e);
2634 mpfr_trunc (rtrunc->value.real, e->value.real);
2636 result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
2638 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real, &e->where);
2640 gfc_free_expr (rtrunc);
2642 return range_check (result, "IFIX");
2647 gfc_simplify_idint (gfc_expr *e)
2649 gfc_expr *rtrunc, *result;
2651 if (e->expr_type != EXPR_CONSTANT)
2654 rtrunc = gfc_copy_expr (e);
2655 mpfr_trunc (rtrunc->value.real, e->value.real);
2657 result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
2659 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real, &e->where);
2661 gfc_free_expr (rtrunc);
2663 return range_check (result, "IDINT");
2668 gfc_simplify_ior (gfc_expr *x, gfc_expr *y)
2672 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2675 result = gfc_get_constant_expr (BT_INTEGER, x->ts.kind, &x->where);
2676 mpz_ior (result->value.integer, x->value.integer, y->value.integer);
2678 return range_check (result, "IOR");
2683 gfc_simplify_is_iostat_end (gfc_expr *x)
2685 if (x->expr_type != EXPR_CONSTANT)
2688 return gfc_get_logical_expr (gfc_default_logical_kind, &x->where,
2689 mpz_cmp_si (x->value.integer,
2690 LIBERROR_END) == 0);
2695 gfc_simplify_is_iostat_eor (gfc_expr *x)
2697 if (x->expr_type != EXPR_CONSTANT)
2700 return gfc_get_logical_expr (gfc_default_logical_kind, &x->where,
2701 mpz_cmp_si (x->value.integer,
2702 LIBERROR_EOR) == 0);
2707 gfc_simplify_isnan (gfc_expr *x)
2709 if (x->expr_type != EXPR_CONSTANT)
2712 return gfc_get_logical_expr (gfc_default_logical_kind, &x->where,
2713 mpfr_nan_p (x->value.real));
2718 gfc_simplify_ishft (gfc_expr *e, gfc_expr *s)
2721 int shift, ashift, isize, k, *bits, i;
2723 if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
2726 if (gfc_extract_int (s, &shift) != NULL)
2728 gfc_error ("Invalid second argument of ISHFT at %L", &s->where);
2729 return &gfc_bad_expr;
2732 k = gfc_validate_kind (BT_INTEGER, e->ts.kind, false);
2734 isize = gfc_integer_kinds[k].bit_size;
2743 gfc_error ("Magnitude of second argument of ISHFT exceeds bit size "
2744 "at %L", &s->where);
2745 return &gfc_bad_expr;
2748 result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
2752 mpz_set (result->value.integer, e->value.integer);
2753 return range_check (result, "ISHFT");
2756 bits = XCNEWVEC (int, isize);
2758 for (i = 0; i < isize; i++)
2759 bits[i] = mpz_tstbit (e->value.integer, i);
2763 for (i = 0; i < shift; i++)
2764 mpz_clrbit (result->value.integer, i);
2766 for (i = 0; i < isize - shift; i++)
2769 mpz_clrbit (result->value.integer, i + shift);
2771 mpz_setbit (result->value.integer, i + shift);
2776 for (i = isize - 1; i >= isize - ashift; i--)
2777 mpz_clrbit (result->value.integer, i);
2779 for (i = isize - 1; i >= ashift; i--)
2782 mpz_clrbit (result->value.integer, i - ashift);
2784 mpz_setbit (result->value.integer, i - ashift);
2788 convert_mpz_to_signed (result->value.integer, isize);
2796 gfc_simplify_ishftc (gfc_expr *e, gfc_expr *s, gfc_expr *sz)
2799 int shift, ashift, isize, ssize, delta, k;
2802 if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
2805 if (gfc_extract_int (s, &shift) != NULL)
2807 gfc_error ("Invalid second argument of ISHFTC at %L", &s->where);
2808 return &gfc_bad_expr;
2811 k = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2812 isize = gfc_integer_kinds[k].bit_size;
2816 if (sz->expr_type != EXPR_CONSTANT)
2819 if (gfc_extract_int (sz, &ssize) != NULL || ssize <= 0)
2821 gfc_error ("Invalid third argument of ISHFTC at %L", &sz->where);
2822 return &gfc_bad_expr;
2827 gfc_error ("Magnitude of third argument of ISHFTC exceeds "
2828 "BIT_SIZE of first argument at %L", &s->where);
2829 return &gfc_bad_expr;
2843 gfc_error ("Magnitude of second argument of ISHFTC exceeds "
2844 "third argument at %L", &s->where);
2846 gfc_error ("Magnitude of second argument of ISHFTC exceeds "
2847 "BIT_SIZE of first argument at %L", &s->where);
2848 return &gfc_bad_expr;
2851 result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
2853 mpz_set (result->value.integer, e->value.integer);
2858 convert_mpz_to_unsigned (result->value.integer, isize);
2860 bits = XCNEWVEC (int, ssize);
2862 for (i = 0; i < ssize; i++)
2863 bits[i] = mpz_tstbit (e->value.integer, i);
2865 delta = ssize - ashift;
2869 for (i = 0; i < delta; i++)
2872 mpz_clrbit (result->value.integer, i + shift);
2874 mpz_setbit (result->value.integer, i + shift);
2877 for (i = delta; i < ssize; i++)
2880 mpz_clrbit (result->value.integer, i - delta);
2882 mpz_setbit (result->value.integer, i - delta);
2887 for (i = 0; i < ashift; i++)
2890 mpz_clrbit (result->value.integer, i + delta);
2892 mpz_setbit (result->value.integer, i + delta);
2895 for (i = ashift; i < ssize; i++)
2898 mpz_clrbit (result->value.integer, i + shift);
2900 mpz_setbit (result->value.integer, i + shift);
2904 convert_mpz_to_signed (result->value.integer, isize);
2912 gfc_simplify_kind (gfc_expr *e)
2914 return gfc_get_int_expr (gfc_default_integer_kind, NULL, e->ts.kind);
2919 simplify_bound_dim (gfc_expr *array, gfc_expr *kind, int d, int upper,
2920 gfc_array_spec *as, gfc_ref *ref, bool coarray)
2922 gfc_expr *l, *u, *result;
2925 k = get_kind (BT_INTEGER, kind, upper ? "UBOUND" : "LBOUND",
2926 gfc_default_integer_kind);
2928 return &gfc_bad_expr;
2930 result = gfc_get_constant_expr (BT_INTEGER, k, &array->where);
2932 /* For non-variables, LBOUND(expr, DIM=n) = 1 and
2933 UBOUND(expr, DIM=n) = SIZE(expr, DIM=n). */
2934 if (!coarray && array->expr_type != EXPR_VARIABLE)
2938 gfc_expr* dim = result;
2939 mpz_set_si (dim->value.integer, d);
2941 result = gfc_simplify_size (array, dim, kind);
2942 gfc_free_expr (dim);
2947 mpz_set_si (result->value.integer, 1);
2952 /* Otherwise, we have a variable expression. */
2953 gcc_assert (array->expr_type == EXPR_VARIABLE);
2956 /* The last dimension of an assumed-size array is special. */
2957 if ((!coarray && d == as->rank && as->type == AS_ASSUMED_SIZE && !upper)
2958 || (coarray && d == as->rank + as->corank))
2960 if (as->lower[d-1]->expr_type == EXPR_CONSTANT)
2962 gfc_free_expr (result);
2963 return gfc_copy_expr (as->lower[d-1]);
2969 result = gfc_get_constant_expr (BT_INTEGER, k, &array->where);
2971 /* Then, we need to know the extent of the given dimension. */
2972 if (coarray || ref->u.ar.type == AR_FULL)
2977 if (l->expr_type != EXPR_CONSTANT || u == NULL
2978 || u->expr_type != EXPR_CONSTANT)
2981 if (mpz_cmp (l->value.integer, u->value.integer) > 0)
2985 mpz_set_si (result->value.integer, 0);
2987 mpz_set_si (result->value.integer, 1);
2991 /* Nonzero extent. */
2993 mpz_set (result->value.integer, u->value.integer);
2995 mpz_set (result->value.integer, l->value.integer);
3002 if (gfc_ref_dimen_size (&ref->u.ar, d-1, &result->value.integer, NULL)
3007 mpz_set_si (result->value.integer, (long int) 1);
3011 return range_check (result, upper ? "UBOUND" : "LBOUND");
3014 gfc_free_expr (result);
3020 simplify_bound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind, int upper)
3026 if (array->expr_type != EXPR_VARIABLE)
3033 /* Follow any component references. */
3034 as = array->symtree->n.sym->as;
3035 for (ref = array->ref; ref; ref = ref->next)
3040 switch (ref->u.ar.type)
3047 /* We're done because 'as' has already been set in the
3048 previous iteration. */
3065 as = ref->u.c.component->as;
3077 if (as && (as->type == AS_DEFERRED || as->type == AS_ASSUMED_SHAPE))
3082 /* Multi-dimensional bounds. */
3083 gfc_expr *bounds[GFC_MAX_DIMENSIONS];
3087 /* UBOUND(ARRAY) is not valid for an assumed-size array. */
3088 if (upper && as && as->type == AS_ASSUMED_SIZE)
3090 /* An error message will be emitted in
3091 check_assumed_size_reference (resolve.c). */
3092 return &gfc_bad_expr;
3095 /* Simplify the bounds for each dimension. */
3096 for (d = 0; d < array->rank; d++)
3098 bounds[d] = simplify_bound_dim (array, kind, d + 1, upper, as, ref,
3100 if (bounds[d] == NULL || bounds[d] == &gfc_bad_expr)
3104 for (j = 0; j < d; j++)
3105 gfc_free_expr (bounds[j]);
3110 /* Allocate the result expression. */
3111 k = get_kind (BT_INTEGER, kind, upper ? "UBOUND" : "LBOUND",
3112 gfc_default_integer_kind);
3114 return &gfc_bad_expr;
3116 e = gfc_get_array_expr (BT_INTEGER, k, &array->where);
3118 /* The result is a rank 1 array; its size is the rank of the first
3119 argument to {L,U}BOUND. */
3121 e->shape = gfc_get_shape (1);
3122 mpz_init_set_ui (e->shape[0], array->rank);
3124 /* Create the constructor for this array. */
3125 for (d = 0; d < array->rank; d++)
3126 gfc_constructor_append_expr (&e->value.constructor,
3127 bounds[d], &e->where);
3133 /* A DIM argument is specified. */
3134 if (dim->expr_type != EXPR_CONSTANT)
3137 d = mpz_get_si (dim->value.integer);
3139 if (d < 1 || d > array->rank
3140 || (d == array->rank && as && as->type == AS_ASSUMED_SIZE && upper))
3142 gfc_error ("DIM argument at %L is out of bounds", &dim->where);
3143 return &gfc_bad_expr;
3146 return simplify_bound_dim (array, kind, d, upper, as, ref, false);
3152 simplify_cobound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind, int upper)
3158 if (array->expr_type != EXPR_VARIABLE)
3161 /* Follow any component references. */
3162 as = array->symtree->n.sym->as;
3163 for (ref = array->ref; ref; ref = ref->next)
3168 switch (ref->u.ar.type)
3171 if (ref->next == NULL)
3173 gcc_assert (ref->u.ar.as->corank > 0
3174 && ref->u.ar.as->rank == 0);
3182 /* We're done because 'as' has already been set in the
3183 previous iteration. */
3200 as = ref->u.c.component->as;
3212 if (as->type == AS_DEFERRED || as->type == AS_ASSUMED_SHAPE)
3217 /* Multi-dimensional cobounds. */
3218 gfc_expr *bounds[GFC_MAX_DIMENSIONS];
3222 /* Simplify the cobounds for each dimension. */
3223 for (d = 0; d < as->corank; d++)
3225 bounds[d] = simplify_bound_dim (array, kind, d + 1 + array->rank,
3226 upper, as, ref, true);
3227 if (bounds[d] == NULL || bounds[d] == &gfc_bad_expr)
3231 for (j = 0; j < d; j++)
3232 gfc_free_expr (bounds[j]);
3237 /* Allocate the result expression. */
3238 e = gfc_get_expr ();
3239 e->where = array->where;
3240 e->expr_type = EXPR_ARRAY;
3241 e->ts.type = BT_INTEGER;
3242 k = get_kind (BT_INTEGER, kind, upper ? "UCOBOUND" : "LCOBOUND",
3243 gfc_default_integer_kind);
3247 return &gfc_bad_expr;
3251 /* The result is a rank 1 array; its size is the rank of the first
3252 argument to {L,U}COBOUND. */
3254 e->shape = gfc_get_shape (1);
3255 mpz_init_set_ui (e->shape[0], as->corank);
3257 /* Create the constructor for this array. */
3258 for (d = 0; d < as->corank; d++)
3259 gfc_constructor_append_expr (&e->value.constructor,
3260 bounds[d], &e->where);
3265 /* A DIM argument is specified. */
3266 if (dim->expr_type != EXPR_CONSTANT)
3269 d = mpz_get_si (dim->value.integer);
3271 if (d < 1 || d > as->corank)
3273 gfc_error ("DIM argument at %L is out of bounds", &dim->where);
3274 return &gfc_bad_expr;
3277 return simplify_bound_dim (array, kind, d+array->rank, upper, as, ref, true);
3283 gfc_simplify_lbound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
3285 return simplify_bound (array, dim, kind, 0);
3290 gfc_simplify_lcobound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
3293 /* return simplify_cobound (array, dim, kind, 0);*/
3295 e = simplify_cobound (array, dim, kind, 0);
3299 gfc_error ("Not yet implemented: LCOBOUND for coarray with non-constant "
3300 "cobounds at %L", &array->where);
3301 return &gfc_bad_expr;
3305 gfc_simplify_leadz (gfc_expr *e)
3307 unsigned long lz, bs;
3310 if (e->expr_type != EXPR_CONSTANT)
3313 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
3314 bs = gfc_integer_kinds[i].bit_size;
3315 if (mpz_cmp_si (e->value.integer, 0) == 0)
3317 else if (mpz_cmp_si (e->value.integer, 0) < 0)
3320 lz = bs - mpz_sizeinbase (e->value.integer, 2);
3322 return gfc_get_int_expr (gfc_default_integer_kind, &e->where, lz);
3327 gfc_simplify_len (gfc_expr *e, gfc_expr *kind)
3330 int k = get_kind (BT_INTEGER, kind, "LEN", gfc_default_integer_kind);
3333 return &gfc_bad_expr;
3335 if (e->expr_type == EXPR_CONSTANT)
3337 result = gfc_get_constant_expr (BT_INTEGER, k, &e->where);
3338 mpz_set_si (result->value.integer, e->value.character.length);
3339 return range_check (result, "LEN");
3341 else if (e->ts.u.cl != NULL && e->ts.u.cl->length != NULL
3342 && e->ts.u.cl->length->expr_type == EXPR_CONSTANT
3343 && e->ts.u.cl->length->ts.type == BT_INTEGER)
3345 result = gfc_get_constant_expr (BT_INTEGER, k, &e->where);
3346 mpz_set (result->value.integer, e->ts.u.cl->length->value.integer);
3347 return range_check (result, "LEN");
3355 gfc_simplify_len_trim (gfc_expr *e, gfc_expr *kind)
3359 int k = get_kind (BT_INTEGER, kind, "LEN_TRIM", gfc_default_integer_kind);
3362 return &gfc_bad_expr;
3364 if (e->expr_type != EXPR_CONSTANT)
3367 len = e->value.character.length;
3368 for (count = 0, i = 1; i <= len; i++)
3369 if (e->value.character.string[len - i] == ' ')
3374 result = gfc_get_int_expr (k, &e->where, len - count);
3375 return range_check (result, "LEN_TRIM");
3379 gfc_simplify_lgamma (gfc_expr *x)
3384 if (x->expr_type != EXPR_CONSTANT)
3387 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
3388 mpfr_lgamma (result->value.real, &sg, x->value.real, GFC_RND_MODE);
3390 return range_check (result, "LGAMMA");
3395 gfc_simplify_lge (gfc_expr *a, gfc_expr *b)
3397 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
3400 return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
3401 gfc_compare_string (a, b) >= 0);
3406 gfc_simplify_lgt (gfc_expr *a, gfc_expr *b)
3408 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
3411 return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
3412 gfc_compare_string (a, b) > 0);
3417 gfc_simplify_lle (gfc_expr *a, gfc_expr *b)
3419 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
3422 return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
3423 gfc_compare_string (a, b) <= 0);
3428 gfc_simplify_llt (gfc_expr *a, gfc_expr *b)
3430 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
3433 return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
3434 gfc_compare_string (a, b) < 0);
3439 gfc_simplify_log (gfc_expr *x)
3443 if (x->expr_type != EXPR_CONSTANT)
3446 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
3451 if (mpfr_sgn (x->value.real) <= 0)
3453 gfc_error ("Argument of LOG at %L cannot be less than or equal "
3454 "to zero", &x->where);
3455 gfc_free_expr (result);
3456 return &gfc_bad_expr;
3459 mpfr_log (result->value.real, x->value.real, GFC_RND_MODE);
3463 if ((mpfr_sgn (mpc_realref (x->value.complex)) == 0)
3464 && (mpfr_sgn (mpc_imagref (x->value.complex)) == 0))
3466 gfc_error ("Complex argument of LOG at %L cannot be zero",
3468 gfc_free_expr (result);
3469 return &gfc_bad_expr;
3472 gfc_set_model_kind (x->ts.kind);
3473 mpc_log (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
3477 gfc_internal_error ("gfc_simplify_log: bad type");
3480 return range_check (result, "LOG");
3485 gfc_simplify_log10 (gfc_expr *x)
3489 if (x->expr_type != EXPR_CONSTANT)
3492 if (mpfr_sgn (x->value.real) <= 0)
3494 gfc_error ("Argument of LOG10 at %L cannot be less than or equal "
3495 "to zero", &x->where);
3496 return &gfc_bad_expr;
3499 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
3500 mpfr_log10 (result->value.real, x->value.real, GFC_RND_MODE);
3502 return range_check (result, "LOG10");
3507 gfc_simplify_logical (gfc_expr *e, gfc_expr *k)
3511 kind = get_kind (BT_LOGICAL, k, "LOGICAL", gfc_default_logical_kind);
3513 return &gfc_bad_expr;
3515 if (e->expr_type != EXPR_CONSTANT)
3518 return gfc_get_logical_expr (kind, &e->where, e->value.logical);
3523 gfc_simplify_matmul (gfc_expr *matrix_a, gfc_expr *matrix_b)
3526 int row, result_rows, col, result_columns;
3527 int stride_a, offset_a, stride_b, offset_b;
3529 if (!is_constant_array_expr (matrix_a)
3530 || !is_constant_array_expr (matrix_b))
3533 gcc_assert (gfc_compare_types (&matrix_a->ts, &matrix_b->ts));
3534 result = gfc_get_array_expr (matrix_a->ts.type,
3538 if (matrix_a->rank == 1 && matrix_b->rank == 2)
3541 result_columns = mpz_get_si (matrix_b->shape[0]);
3543 stride_b = mpz_get_si (matrix_b->shape[0]);
3546 result->shape = gfc_get_shape (result->rank);
3547 mpz_init_set_si (result->shape[0], result_columns);
3549 else if (matrix_a->rank == 2 && matrix_b->rank == 1)
3551 result_rows = mpz_get_si (matrix_b->shape[0]);
3553 stride_a = mpz_get_si (matrix_a->shape[0]);
3557 result->shape = gfc_get_shape (result->rank);
3558 mpz_init_set_si (result->shape[0], result_rows);
3560 else if (matrix_a->rank == 2 && matrix_b->rank == 2)
3562 result_rows = mpz_get_si (matrix_a->shape[0]);
3563 result_columns = mpz_get_si (matrix_b->shape[1]);
3564 stride_a = mpz_get_si (matrix_a->shape[1]);
3565 stride_b = mpz_get_si (matrix_b->shape[0]);
3568 result->shape = gfc_get_shape (result->rank);
3569 mpz_init_set_si (result->shape[0], result_rows);
3570 mpz_init_set_si (result->shape[1], result_columns);
3575 offset_a = offset_b = 0;
3576 for (col = 0; col < result_columns; ++col)
3580 for (row = 0; row < result_rows; ++row)
3582 gfc_expr *e = compute_dot_product (matrix_a, stride_a, offset_a,
3583 matrix_b, 1, offset_b);
3584 gfc_constructor_append_expr (&result->value.constructor,
3590 offset_b += stride_b;
3598 gfc_simplify_merge (gfc_expr *tsource, gfc_expr *fsource, gfc_expr *mask)
3600 if (tsource->expr_type != EXPR_CONSTANT
3601 || fsource->expr_type != EXPR_CONSTANT
3602 || mask->expr_type != EXPR_CONSTANT)
3605 return gfc_copy_expr (mask->value.logical ? tsource : fsource);
3609 /* Selects bewteen current value and extremum for simplify_min_max
3610 and simplify_minval_maxval. */
3612 min_max_choose (gfc_expr *arg, gfc_expr *extremum, int sign)
3614 switch (arg->ts.type)
3617 if (mpz_cmp (arg->value.integer,
3618 extremum->value.integer) * sign > 0)
3619 mpz_set (extremum->value.integer, arg->value.integer);
3623 /* We need to use mpfr_min and mpfr_max to treat NaN properly. */
3625 mpfr_max (extremum->value.real, extremum->value.real,
3626 arg->value.real, GFC_RND_MODE);
3628 mpfr_min (extremum->value.real, extremum->value.real,
3629 arg->value.real, GFC_RND_MODE);
3633 #define LENGTH(x) ((x)->value.character.length)
3634 #define STRING(x) ((x)->value.character.string)
3635 if (LENGTH(extremum) < LENGTH(arg))
3637 gfc_char_t *tmp = STRING(extremum);
3639 STRING(extremum) = gfc_get_wide_string (LENGTH(arg) + 1);
3640 memcpy (STRING(extremum), tmp,
3641 LENGTH(extremum) * sizeof (gfc_char_t));
3642 gfc_wide_memset (&STRING(extremum)[LENGTH(extremum)], ' ',
3643 LENGTH(arg) - LENGTH(extremum));
3644 STRING(extremum)[LENGTH(arg)] = '\0'; /* For debugger */
3645 LENGTH(extremum) = LENGTH(arg);
3649 if (gfc_compare_string (arg, extremum) * sign > 0)
3651 gfc_free (STRING(extremum));
3652 STRING(extremum) = gfc_get_wide_string (LENGTH(extremum) + 1);
3653 memcpy (STRING(extremum), STRING(arg),
3654 LENGTH(arg) * sizeof (gfc_char_t));
3655 gfc_wide_memset (&STRING(extremum)[LENGTH(arg)], ' ',
3656 LENGTH(extremum) - LENGTH(arg));
3657 STRING(extremum)[LENGTH(extremum)] = '\0'; /* For debugger */
3664 gfc_internal_error ("simplify_min_max(): Bad type in arglist");
3669 /* This function is special since MAX() can take any number of
3670 arguments. The simplified expression is a rewritten version of the
3671 argument list containing at most one constant element. Other
3672 constant elements are deleted. Because the argument list has
3673 already been checked, this function always succeeds. sign is 1 for
3674 MAX(), -1 for MIN(). */
3677 simplify_min_max (gfc_expr *expr, int sign)
3679 gfc_actual_arglist *arg, *last, *extremum;
3680 gfc_intrinsic_sym * specific;
3684 specific = expr->value.function.isym;
3686 arg = expr->value.function.actual;
3688 for (; arg; last = arg