1 /* Simplify intrinsic functions at compile-time.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
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 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"
30 /* Savely advance an array constructor by 'n' elements.
31 Mainly used by simplifiers of transformational intrinsics. */
32 #define ADVANCE(ctor, n) do { int i; for (i = 0; i < n && ctor; ++i) ctor = ctor->next; } while (0)
34 gfc_expr gfc_bad_expr;
37 /* Note that 'simplification' is not just transforming expressions.
38 For functions that are not simplified at compile time, range
39 checking is done if possible.
41 The return convention is that each simplification function returns:
43 A new expression node corresponding to the simplified arguments.
44 The original arguments are destroyed by the caller, and must not
45 be a part of the new expression.
47 NULL pointer indicating that no simplification was possible and
48 the original expression should remain intact. If the
49 simplification function sets the type and/or the function name
50 via the pointer gfc_simple_expression, then this type is
53 An expression pointer to gfc_bad_expr (a static placeholder)
54 indicating that some error has prevented simplification. For
55 example, sqrt(-1.0). The error is generated within the function
56 and should be propagated upwards
58 By the time a simplification function gets control, it has been
59 decided that the function call is really supposed to be the
60 intrinsic. No type checking is strictly necessary, since only
61 valid types will be passed on. On the other hand, a simplification
62 subroutine may have to look at the type of an argument as part of
65 Array arguments are never passed to these subroutines.
67 The functions in this file don't have much comment with them, but
68 everything is reasonably straight-forward. The Standard, chapter 13
69 is the best comment you'll find for this file anyway. */
71 /* Range checks an expression node. If all goes well, returns the
72 node, otherwise returns &gfc_bad_expr and frees the node. */
75 range_check (gfc_expr *result, const char *name)
80 switch (gfc_range_check (result))
86 gfc_error ("Result of %s overflows its kind at %L", name,
91 gfc_error ("Result of %s underflows its kind at %L", name,
96 gfc_error ("Result of %s is NaN at %L", name, &result->where);
100 gfc_error ("Result of %s gives range error for its kind at %L", name,
105 gfc_free_expr (result);
106 return &gfc_bad_expr;
110 /* A helper function that gets an optional and possibly missing
111 kind parameter. Returns the kind, -1 if something went wrong. */
114 get_kind (bt type, gfc_expr *k, const char *name, int default_kind)
121 if (k->expr_type != EXPR_CONSTANT)
123 gfc_error ("KIND parameter of %s at %L must be an initialization "
124 "expression", name, &k->where);
128 if (gfc_extract_int (k, &kind) != NULL
129 || gfc_validate_kind (type, kind, true) < 0)
131 gfc_error ("Invalid KIND parameter of %s at %L", name, &k->where);
139 /* Helper function to get an integer constant with a kind number given
140 by an integer constant expression. */
142 int_expr_with_kind (int i, gfc_expr *kind, const char *name)
144 gfc_expr *res = gfc_int_expr (i);
145 res->ts.kind = get_kind (BT_INTEGER, kind, name, gfc_default_integer_kind);
146 if (res->ts.kind == -1)
153 /* Converts an mpz_t signed variable into an unsigned one, assuming
154 two's complement representations and a binary width of bitsize.
155 The conversion is a no-op unless x is negative; otherwise, it can
156 be accomplished by masking out the high bits. */
159 convert_mpz_to_unsigned (mpz_t x, int bitsize)
165 /* Confirm that no bits above the signed range are unset. */
166 gcc_assert (mpz_scan0 (x, bitsize-1) == ULONG_MAX);
168 mpz_init_set_ui (mask, 1);
169 mpz_mul_2exp (mask, mask, bitsize);
170 mpz_sub_ui (mask, mask, 1);
172 mpz_and (x, x, mask);
178 /* Confirm that no bits above the signed range are set. */
179 gcc_assert (mpz_scan1 (x, bitsize-1) == ULONG_MAX);
184 /* Converts an mpz_t unsigned variable into a signed one, assuming
185 two's complement representations and a binary width of bitsize.
186 If the bitsize-1 bit is set, this is taken as a sign bit and
187 the number is converted to the corresponding negative number. */
190 convert_mpz_to_signed (mpz_t x, int bitsize)
194 /* Confirm that no bits above the unsigned range are set. */
195 gcc_assert (mpz_scan1 (x, bitsize) == ULONG_MAX);
197 if (mpz_tstbit (x, bitsize - 1) == 1)
199 mpz_init_set_ui (mask, 1);
200 mpz_mul_2exp (mask, mask, bitsize);
201 mpz_sub_ui (mask, mask, 1);
203 /* We negate the number by hand, zeroing the high bits, that is
204 make it the corresponding positive number, and then have it
205 negated by GMP, giving the correct representation of the
208 mpz_add_ui (x, x, 1);
209 mpz_and (x, x, mask);
217 /* Test that the expression is an constant array. */
220 is_constant_array_expr (gfc_expr *e)
227 if (e->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (e))
230 for (c = e->value.constructor; c; c = c->next)
231 if (c->expr->expr_type != EXPR_CONSTANT)
238 /* Initialize a transformational result expression with a given value. */
241 init_result_expr (gfc_expr *e, int init, gfc_expr *array)
243 if (e && e->expr_type == EXPR_ARRAY)
245 gfc_constructor *ctor = e->value.constructor;
248 init_result_expr (ctor->expr, init, array);
252 else if (e && e->expr_type == EXPR_CONSTANT)
254 int i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
261 e->value.logical = (init ? 1 : 0);
266 mpz_set (e->value.integer, gfc_integer_kinds[i].min_int);
267 else if (init == INT_MAX)
268 mpz_set (e->value.integer, gfc_integer_kinds[i].huge);
270 mpz_set_si (e->value.integer, init);
276 mpfr_set (e->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
277 mpfr_neg (e->value.real, e->value.real, GFC_RND_MODE);
279 else if (init == INT_MAX)
280 mpfr_set (e->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
282 mpfr_set_si (e->value.real, init, GFC_RND_MODE);
287 mpc_set_si (e->value.complex, init, GFC_MPC_RND_MODE);
289 mpfr_set_si (e->value.complex.r, init, GFC_RND_MODE);
290 mpfr_set_si (e->value.complex.i, 0, GFC_RND_MODE);
297 gfc_expr *len = gfc_simplify_len (array, NULL);
298 gfc_extract_int (len, &length);
299 string = gfc_get_wide_string (length + 1);
300 gfc_wide_memset (string, 0, length);
302 else if (init == INT_MAX)
304 gfc_expr *len = gfc_simplify_len (array, NULL);
305 gfc_extract_int (len, &length);
306 string = gfc_get_wide_string (length + 1);
307 gfc_wide_memset (string, 255, length);
312 string = gfc_get_wide_string (1);
315 string[length] = '\0';
316 e->value.character.length = length;
317 e->value.character.string = string;
329 /* Helper function for gfc_simplify_dot_product() and gfc_simplify_matmul. */
332 compute_dot_product (gfc_constructor *ctor_a, int stride_a,
333 gfc_constructor *ctor_b, int stride_b)
336 gfc_expr *a = ctor_a->expr, *b = ctor_b->expr;
338 gcc_assert (gfc_compare_types (&a->ts, &b->ts));
340 result = gfc_constant_result (a->ts.type, a->ts.kind, &a->where);
341 init_result_expr (result, 0, NULL);
343 while (ctor_a && ctor_b)
345 /* Copying of expressions is required as operands are free'd
346 by the gfc_arith routines. */
347 switch (result->ts.type)
350 result = gfc_or (result,
351 gfc_and (gfc_copy_expr (ctor_a->expr),
352 gfc_copy_expr (ctor_b->expr)));
358 result = gfc_add (result,
359 gfc_multiply (gfc_copy_expr (ctor_a->expr),
360 gfc_copy_expr (ctor_b->expr)));
367 ADVANCE (ctor_a, stride_a);
368 ADVANCE (ctor_b, stride_b);
375 /* Build a result expression for transformational intrinsics,
379 transformational_result (gfc_expr *array, gfc_expr *dim, bt type,
380 int kind, locus* where)
385 if (!dim || array->rank == 1)
386 return gfc_constant_result (type, kind, where);
388 result = gfc_start_constructor (type, kind, where);
389 result->shape = gfc_copy_shape_excluding (array->shape, array->rank, dim);
390 result->rank = array->rank - 1;
392 /* gfc_array_size() would count the number of elements in the constructor,
393 we have not built those yet. */
395 for (i = 0; i < result->rank; ++i)
396 nelem *= mpz_get_ui (result->shape[i]);
398 for (i = 0; i < nelem; ++i)
400 gfc_expr *e = gfc_constant_result (type, kind, where);
401 gfc_append_constructor (result, e);
408 typedef gfc_expr* (*transformational_op)(gfc_expr*, gfc_expr*);
410 /* Wrapper function, implements 'op1 += 1'. Only called if MASK
411 of COUNT intrinsic is .TRUE..
413 Interface and implimentation mimics arith functions as
414 gfc_add, gfc_multiply, etc. */
416 static gfc_expr* gfc_count (gfc_expr *op1, gfc_expr *op2)
420 gcc_assert (op1->ts.type == BT_INTEGER);
421 gcc_assert (op2->ts.type == BT_LOGICAL);
422 gcc_assert (op2->value.logical);
424 result = gfc_copy_expr (op1);
425 mpz_add_ui (result->value.integer, result->value.integer, 1);
433 /* Transforms an ARRAY with operation OP, according to MASK, to a
434 scalar RESULT. E.g. called if
436 REAL, PARAMETER :: array(n, m) = ...
437 REAL, PARAMETER :: s = SUM(array)
439 where OP == gfc_add(). */
442 simplify_transformation_to_scalar (gfc_expr *result, gfc_expr *array, gfc_expr *mask,
443 transformational_op op)
446 gfc_constructor *array_ctor, *mask_ctor;
448 /* Shortcut for constant .FALSE. MASK. */
450 && mask->expr_type == EXPR_CONSTANT
451 && !mask->value.logical)
454 array_ctor = array->value.constructor;
456 if (mask && mask->expr_type == EXPR_ARRAY)
457 mask_ctor = mask->value.constructor;
461 a = array_ctor->expr;
462 array_ctor = array_ctor->next;
464 /* A constant MASK equals .TRUE. here and can be ignored. */
468 mask_ctor = mask_ctor->next;
469 if (!m->value.logical)
473 result = op (result, gfc_copy_expr (a));
479 /* Transforms an ARRAY with operation OP, according to MASK, to an
480 array RESULT. E.g. called if
482 REAL, PARAMETER :: array(n, m) = ...
483 REAL, PARAMETER :: s(n) = PROD(array, DIM=1)
485 where OP == gfc_multiply(). */
488 simplify_transformation_to_array (gfc_expr *result, gfc_expr *array, gfc_expr *dim,
489 gfc_expr *mask, transformational_op op)
492 int done, i, n, arraysize, resultsize, dim_index, dim_extent, dim_stride;
493 gfc_expr **arrayvec, **resultvec, **base, **src, **dest;
494 gfc_constructor *array_ctor, *mask_ctor, *result_ctor;
496 int count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS],
497 sstride[GFC_MAX_DIMENSIONS], dstride[GFC_MAX_DIMENSIONS],
498 tmpstride[GFC_MAX_DIMENSIONS];
500 /* Shortcut for constant .FALSE. MASK. */
502 && mask->expr_type == EXPR_CONSTANT
503 && !mask->value.logical)
506 /* Build an indexed table for array element expressions to minimize
507 linked-list traversal. Masked elements are set to NULL. */
508 gfc_array_size (array, &size);
509 arraysize = mpz_get_ui (size);
511 arrayvec = (gfc_expr**) gfc_getmem (sizeof (gfc_expr*) * arraysize);
513 array_ctor = array->value.constructor;
515 if (mask && mask->expr_type == EXPR_ARRAY)
516 mask_ctor = mask->value.constructor;
518 for (i = 0; i < arraysize; ++i)
520 arrayvec[i] = array_ctor->expr;
521 array_ctor = array_ctor->next;
525 if (!mask_ctor->expr->value.logical)
528 mask_ctor = mask_ctor->next;
532 /* Same for the result expression. */
533 gfc_array_size (result, &size);
534 resultsize = mpz_get_ui (size);
537 resultvec = (gfc_expr**) gfc_getmem (sizeof (gfc_expr*) * resultsize);
538 result_ctor = result->value.constructor;
539 for (i = 0; i < resultsize; ++i)
541 resultvec[i] = result_ctor->expr;
542 result_ctor = result_ctor->next;
545 gfc_extract_int (dim, &dim_index);
546 dim_index -= 1; /* zero-base index */
550 for (i = 0, n = 0; i < array->rank; ++i)
553 tmpstride[i] = (i == 0) ? 1 : tmpstride[i-1] * mpz_get_si (array->shape[i-1]);
556 dim_extent = mpz_get_si (array->shape[i]);
557 dim_stride = tmpstride[i];
561 extent[n] = mpz_get_si (array->shape[i]);
562 sstride[n] = tmpstride[i];
563 dstride[n] = (n == 0) ? 1 : dstride[n-1] * extent[n-1];
572 for (src = base, n = 0; n < dim_extent; src += dim_stride, ++n)
574 *dest = op (*dest, gfc_copy_expr (*src));
581 while (!done && count[n] == extent[n])
584 base -= sstride[n] * extent[n];
585 dest -= dstride[n] * extent[n];
588 if (n < result->rank)
599 /* Place updated expression in result constructor. */
600 result_ctor = result->value.constructor;
601 for (i = 0; i < resultsize; ++i)
603 result_ctor->expr = resultvec[i];
604 result_ctor = result_ctor->next;
608 gfc_free (resultvec);
614 /********************** Simplification functions *****************************/
617 gfc_simplify_abs (gfc_expr *e)
621 if (e->expr_type != EXPR_CONSTANT)
627 result = gfc_constant_result (BT_INTEGER, e->ts.kind, &e->where);
629 mpz_abs (result->value.integer, e->value.integer);
631 result = range_check (result, "IABS");
635 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
637 mpfr_abs (result->value.real, e->value.real, GFC_RND_MODE);
639 result = range_check (result, "ABS");
643 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
645 gfc_set_model_kind (e->ts.kind);
648 mpc_abs (result->value.real, e->value.complex, GFC_RND_MODE);
650 mpfr_hypot (result->value.real, e->value.complex.r,
651 e->value.complex.i, GFC_RND_MODE);
653 result = range_check (result, "CABS");
657 gfc_internal_error ("gfc_simplify_abs(): Bad type");
665 simplify_achar_char (gfc_expr *e, gfc_expr *k, const char *name, bool ascii)
669 bool too_large = false;
671 if (e->expr_type != EXPR_CONSTANT)
674 kind = get_kind (BT_CHARACTER, k, name, gfc_default_character_kind);
676 return &gfc_bad_expr;
678 if (mpz_cmp_si (e->value.integer, 0) < 0)
680 gfc_error ("Argument of %s function at %L is negative", name,
682 return &gfc_bad_expr;
685 if (ascii && gfc_option.warn_surprising
686 && mpz_cmp_si (e->value.integer, 127) > 0)
687 gfc_warning ("Argument of %s function at %L outside of range [0,127]",
690 if (kind == 1 && mpz_cmp_si (e->value.integer, 255) > 0)
695 mpz_init_set_ui (t, 2);
696 mpz_pow_ui (t, t, 32);
697 mpz_sub_ui (t, t, 1);
698 if (mpz_cmp (e->value.integer, t) > 0)
705 gfc_error ("Argument of %s function at %L is too large for the "
706 "collating sequence of kind %d", name, &e->where, kind);
707 return &gfc_bad_expr;
710 result = gfc_constant_result (BT_CHARACTER, kind, &e->where);
711 result->value.character.string = gfc_get_wide_string (2);
712 result->value.character.length = 1;
713 result->value.character.string[0] = mpz_get_ui (e->value.integer);
714 result->value.character.string[1] = '\0'; /* For debugger */
720 /* We use the processor's collating sequence, because all
721 systems that gfortran currently works on are ASCII. */
724 gfc_simplify_achar (gfc_expr *e, gfc_expr *k)
726 return simplify_achar_char (e, k, "ACHAR", true);
731 gfc_simplify_acos (gfc_expr *x)
735 if (x->expr_type != EXPR_CONSTANT)
738 if (mpfr_cmp_si (x->value.real, 1) > 0
739 || mpfr_cmp_si (x->value.real, -1) < 0)
741 gfc_error ("Argument of ACOS at %L must be between -1 and 1",
743 return &gfc_bad_expr;
746 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
748 mpfr_acos (result->value.real, x->value.real, GFC_RND_MODE);
750 return range_check (result, "ACOS");
754 gfc_simplify_acosh (gfc_expr *x)
758 if (x->expr_type != EXPR_CONSTANT)
761 if (mpfr_cmp_si (x->value.real, 1) < 0)
763 gfc_error ("Argument of ACOSH at %L must not be less than 1",
765 return &gfc_bad_expr;
768 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
770 mpfr_acosh (result->value.real, x->value.real, GFC_RND_MODE);
772 return range_check (result, "ACOSH");
776 gfc_simplify_adjustl (gfc_expr *e)
782 if (e->expr_type != EXPR_CONSTANT)
785 len = e->value.character.length;
787 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
789 result->value.character.length = len;
790 result->value.character.string = gfc_get_wide_string (len + 1);
792 for (count = 0, i = 0; i < len; ++i)
794 ch = e->value.character.string[i];
800 for (i = 0; i < len - count; ++i)
801 result->value.character.string[i] = e->value.character.string[count + i];
803 for (i = len - count; i < len; ++i)
804 result->value.character.string[i] = ' ';
806 result->value.character.string[len] = '\0'; /* For debugger */
813 gfc_simplify_adjustr (gfc_expr *e)
819 if (e->expr_type != EXPR_CONSTANT)
822 len = e->value.character.length;
824 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
826 result->value.character.length = len;
827 result->value.character.string = gfc_get_wide_string (len + 1);
829 for (count = 0, i = len - 1; i >= 0; --i)
831 ch = e->value.character.string[i];
837 for (i = 0; i < count; ++i)
838 result->value.character.string[i] = ' ';
840 for (i = count; i < len; ++i)
841 result->value.character.string[i] = e->value.character.string[i - count];
843 result->value.character.string[len] = '\0'; /* For debugger */
850 gfc_simplify_aimag (gfc_expr *e)
854 if (e->expr_type != EXPR_CONSTANT)
857 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
858 mpfr_set (result->value.real, mpc_imagref (e->value.complex), GFC_RND_MODE);
860 return range_check (result, "AIMAG");
865 gfc_simplify_aint (gfc_expr *e, gfc_expr *k)
867 gfc_expr *rtrunc, *result;
870 kind = get_kind (BT_REAL, k, "AINT", e->ts.kind);
872 return &gfc_bad_expr;
874 if (e->expr_type != EXPR_CONSTANT)
877 rtrunc = gfc_copy_expr (e);
879 mpfr_trunc (rtrunc->value.real, e->value.real);
881 result = gfc_real2real (rtrunc, kind);
882 gfc_free_expr (rtrunc);
884 return range_check (result, "AINT");
889 gfc_simplify_all (gfc_expr *mask, gfc_expr *dim)
893 if (!is_constant_array_expr (mask)
894 || !gfc_is_constant_expr (dim))
897 result = transformational_result (mask, dim, mask->ts.type,
898 mask->ts.kind, &mask->where);
899 init_result_expr (result, true, NULL);
901 return !dim || mask->rank == 1 ?
902 simplify_transformation_to_scalar (result, mask, NULL, gfc_and) :
903 simplify_transformation_to_array (result, mask, dim, NULL, gfc_and);
908 gfc_simplify_dint (gfc_expr *e)
910 gfc_expr *rtrunc, *result;
912 if (e->expr_type != EXPR_CONSTANT)
915 rtrunc = gfc_copy_expr (e);
917 mpfr_trunc (rtrunc->value.real, e->value.real);
919 result = gfc_real2real (rtrunc, gfc_default_double_kind);
920 gfc_free_expr (rtrunc);
922 return range_check (result, "DINT");
927 gfc_simplify_anint (gfc_expr *e, gfc_expr *k)
932 kind = get_kind (BT_REAL, k, "ANINT", e->ts.kind);
934 return &gfc_bad_expr;
936 if (e->expr_type != EXPR_CONSTANT)
939 result = gfc_constant_result (e->ts.type, kind, &e->where);
941 mpfr_round (result->value.real, e->value.real);
943 return range_check (result, "ANINT");
948 gfc_simplify_and (gfc_expr *x, gfc_expr *y)
953 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
956 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
957 if (x->ts.type == BT_INTEGER)
959 result = gfc_constant_result (BT_INTEGER, kind, &x->where);
960 mpz_and (result->value.integer, x->value.integer, y->value.integer);
961 return range_check (result, "AND");
963 else /* BT_LOGICAL */
965 result = gfc_constant_result (BT_LOGICAL, kind, &x->where);
966 result->value.logical = x->value.logical && y->value.logical;
973 gfc_simplify_any (gfc_expr *mask, gfc_expr *dim)
977 if (!is_constant_array_expr (mask)
978 || !gfc_is_constant_expr (dim))
981 result = transformational_result (mask, dim, mask->ts.type,
982 mask->ts.kind, &mask->where);
983 init_result_expr (result, false, NULL);
985 return !dim || mask->rank == 1 ?
986 simplify_transformation_to_scalar (result, mask, NULL, gfc_or) :
987 simplify_transformation_to_array (result, mask, dim, NULL, gfc_or);
992 gfc_simplify_dnint (gfc_expr *e)
996 if (e->expr_type != EXPR_CONSTANT)
999 result = gfc_constant_result (BT_REAL, gfc_default_double_kind, &e->where);
1001 mpfr_round (result->value.real, e->value.real);
1003 return range_check (result, "DNINT");
1008 gfc_simplify_asin (gfc_expr *x)
1012 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;
1023 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1025 mpfr_asin (result->value.real, x->value.real, GFC_RND_MODE);
1027 return range_check (result, "ASIN");
1032 gfc_simplify_asinh (gfc_expr *x)
1036 if (x->expr_type != EXPR_CONSTANT)
1039 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1041 mpfr_asinh (result->value.real, x->value.real, GFC_RND_MODE);
1043 return range_check (result, "ASINH");
1048 gfc_simplify_atan (gfc_expr *x)
1052 if (x->expr_type != EXPR_CONSTANT)
1055 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1057 mpfr_atan (result->value.real, x->value.real, GFC_RND_MODE);
1059 return range_check (result, "ATAN");
1064 gfc_simplify_atanh (gfc_expr *x)
1068 if (x->expr_type != EXPR_CONSTANT)
1071 if (mpfr_cmp_si (x->value.real, 1) >= 0
1072 || mpfr_cmp_si (x->value.real, -1) <= 0)
1074 gfc_error ("Argument of ATANH at %L must be inside the range -1 to 1",
1076 return &gfc_bad_expr;
1079 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1081 mpfr_atanh (result->value.real, x->value.real, GFC_RND_MODE);
1083 return range_check (result, "ATANH");
1088 gfc_simplify_atan2 (gfc_expr *y, gfc_expr *x)
1092 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1095 if (mpfr_sgn (y->value.real) == 0 && mpfr_sgn (x->value.real) == 0)
1097 gfc_error ("If first argument of ATAN2 %L is zero, then the "
1098 "second argument must not be zero", &x->where);
1099 return &gfc_bad_expr;
1102 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1104 mpfr_atan2 (result->value.real, y->value.real, x->value.real, GFC_RND_MODE);
1106 return range_check (result, "ATAN2");
1111 gfc_simplify_bessel_j0 (gfc_expr *x ATTRIBUTE_UNUSED)
1115 if (x->expr_type != EXPR_CONSTANT)
1118 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1119 mpfr_j0 (result->value.real, x->value.real, GFC_RND_MODE);
1121 return range_check (result, "BESSEL_J0");
1126 gfc_simplify_bessel_j1 (gfc_expr *x ATTRIBUTE_UNUSED)
1130 if (x->expr_type != EXPR_CONSTANT)
1133 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1134 mpfr_j1 (result->value.real, x->value.real, GFC_RND_MODE);
1136 return range_check (result, "BESSEL_J1");
1141 gfc_simplify_bessel_jn (gfc_expr *order ATTRIBUTE_UNUSED,
1142 gfc_expr *x ATTRIBUTE_UNUSED)
1147 if (x->expr_type != EXPR_CONSTANT || order->expr_type != EXPR_CONSTANT)
1150 n = mpz_get_si (order->value.integer);
1151 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1152 mpfr_jn (result->value.real, n, x->value.real, GFC_RND_MODE);
1154 return range_check (result, "BESSEL_JN");
1159 gfc_simplify_bessel_y0 (gfc_expr *x ATTRIBUTE_UNUSED)
1163 if (x->expr_type != EXPR_CONSTANT)
1166 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1167 mpfr_y0 (result->value.real, x->value.real, GFC_RND_MODE);
1169 return range_check (result, "BESSEL_Y0");
1174 gfc_simplify_bessel_y1 (gfc_expr *x ATTRIBUTE_UNUSED)
1178 if (x->expr_type != EXPR_CONSTANT)
1181 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1182 mpfr_y1 (result->value.real, x->value.real, GFC_RND_MODE);
1184 return range_check (result, "BESSEL_Y1");
1189 gfc_simplify_bessel_yn (gfc_expr *order ATTRIBUTE_UNUSED,
1190 gfc_expr *x ATTRIBUTE_UNUSED)
1195 if (x->expr_type != EXPR_CONSTANT || order->expr_type != EXPR_CONSTANT)
1198 n = mpz_get_si (order->value.integer);
1199 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1200 mpfr_yn (result->value.real, n, x->value.real, GFC_RND_MODE);
1202 return range_check (result, "BESSEL_YN");
1207 gfc_simplify_bit_size (gfc_expr *e)
1212 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
1213 result = gfc_constant_result (BT_INTEGER, e->ts.kind, &e->where);
1214 mpz_set_ui (result->value.integer, gfc_integer_kinds[i].bit_size);
1221 gfc_simplify_btest (gfc_expr *e, gfc_expr *bit)
1225 if (e->expr_type != EXPR_CONSTANT || bit->expr_type != EXPR_CONSTANT)
1228 if (gfc_extract_int (bit, &b) != NULL || b < 0)
1229 return gfc_logical_expr (0, &e->where);
1231 return gfc_logical_expr (mpz_tstbit (e->value.integer, b), &e->where);
1236 gfc_simplify_ceiling (gfc_expr *e, gfc_expr *k)
1238 gfc_expr *ceil, *result;
1241 kind = get_kind (BT_INTEGER, k, "CEILING", gfc_default_integer_kind);
1243 return &gfc_bad_expr;
1245 if (e->expr_type != EXPR_CONSTANT)
1248 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
1250 ceil = gfc_copy_expr (e);
1252 mpfr_ceil (ceil->value.real, e->value.real);
1253 gfc_mpfr_to_mpz (result->value.integer, ceil->value.real, &e->where);
1255 gfc_free_expr (ceil);
1257 return range_check (result, "CEILING");
1262 gfc_simplify_char (gfc_expr *e, gfc_expr *k)
1264 return simplify_achar_char (e, k, "CHAR", false);
1268 /* Common subroutine for simplifying CMPLX and DCMPLX. */
1271 simplify_cmplx (const char *name, gfc_expr *x, gfc_expr *y, int kind)
1275 result = gfc_constant_result (BT_COMPLEX, kind, &x->where);
1278 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
1286 mpc_set_z (result->value.complex, x->value.integer, GFC_MPC_RND_MODE);
1288 mpfr_set_z (result->value.complex.r, x->value.integer, GFC_RND_MODE);
1294 mpc_set_fr (result->value.complex, x->value.real, GFC_RND_MODE);
1296 mpfr_set (result->value.complex.r, x->value.real, GFC_RND_MODE);
1302 mpc_set (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1304 mpfr_set (result->value.complex.r, x->value.complex.r, GFC_RND_MODE);
1305 mpfr_set (result->value.complex.i, x->value.complex.i, GFC_RND_MODE);
1310 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (x)");
1319 mpfr_set_z (mpc_imagref (result->value.complex),
1320 y->value.integer, GFC_RND_MODE);
1324 mpfr_set (mpc_imagref (result->value.complex),
1325 y->value.real, GFC_RND_MODE);
1329 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (y)");
1338 ts.kind = result->ts.kind;
1340 if (!gfc_convert_boz (x, &ts))
1341 return &gfc_bad_expr;
1342 mpfr_set (mpc_realref (result->value.complex),
1343 x->value.real, GFC_RND_MODE);
1350 ts.kind = result->ts.kind;
1352 if (!gfc_convert_boz (y, &ts))
1353 return &gfc_bad_expr;
1354 mpfr_set (mpc_imagref (result->value.complex),
1355 y->value.real, GFC_RND_MODE);
1358 return range_check (result, name);
1362 /* Function called when we won't simplify an expression like CMPLX (or
1363 COMPLEX or DCMPLX) but still want to convert BOZ arguments. */
1366 only_convert_cmplx_boz (gfc_expr *x, gfc_expr *y, int kind)
1373 if (x->is_boz && !gfc_convert_boz (x, &ts))
1374 return &gfc_bad_expr;
1376 if (y && y->is_boz && !gfc_convert_boz (y, &ts))
1377 return &gfc_bad_expr;
1384 gfc_simplify_cmplx (gfc_expr *x, gfc_expr *y, gfc_expr *k)
1388 kind = get_kind (BT_REAL, k, "CMPLX", gfc_default_real_kind);
1390 return &gfc_bad_expr;
1392 if (x->expr_type != EXPR_CONSTANT
1393 || (y != NULL && y->expr_type != EXPR_CONSTANT))
1394 return only_convert_cmplx_boz (x, y, kind);
1396 return simplify_cmplx ("CMPLX", x, y, kind);
1401 gfc_simplify_complex (gfc_expr *x, gfc_expr *y)
1405 if (x->ts.type == BT_INTEGER)
1407 if (y->ts.type == BT_INTEGER)
1408 kind = gfc_default_real_kind;
1414 if (y->ts.type == BT_REAL)
1415 kind = (x->ts.kind > y->ts.kind) ? x->ts.kind : y->ts.kind;
1420 if (x->expr_type != EXPR_CONSTANT
1421 || (y != NULL && y->expr_type != EXPR_CONSTANT))
1422 return only_convert_cmplx_boz (x, y, kind);
1424 return simplify_cmplx ("COMPLEX", x, y, kind);
1429 gfc_simplify_conjg (gfc_expr *e)
1433 if (e->expr_type != EXPR_CONSTANT)
1436 result = gfc_copy_expr (e);
1438 mpc_conj (result->value.complex, result->value.complex, GFC_MPC_RND_MODE);
1440 mpfr_neg (result->value.complex.i, result->value.complex.i, GFC_RND_MODE);
1443 return range_check (result, "CONJG");
1448 gfc_simplify_cos (gfc_expr *x)
1452 if (x->expr_type != EXPR_CONSTANT)
1455 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1460 mpfr_cos (result->value.real, x->value.real, GFC_RND_MODE);
1463 gfc_set_model_kind (x->ts.kind);
1465 mpc_cos (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1472 mpfr_cos (xp, x->value.complex.r, GFC_RND_MODE);
1473 mpfr_cosh (xq, x->value.complex.i, GFC_RND_MODE);
1474 mpfr_mul (result->value.complex.r, xp, xq, GFC_RND_MODE);
1476 mpfr_sin (xp, x->value.complex.r, GFC_RND_MODE);
1477 mpfr_sinh (xq, x->value.complex.i, GFC_RND_MODE);
1478 mpfr_mul (xp, xp, xq, GFC_RND_MODE);
1479 mpfr_neg (result->value.complex.i, xp, GFC_RND_MODE );
1481 mpfr_clears (xp, xq, NULL);
1486 gfc_internal_error ("in gfc_simplify_cos(): Bad type");
1489 return range_check (result, "COS");
1495 gfc_simplify_cosh (gfc_expr *x)
1499 if (x->expr_type != EXPR_CONSTANT)
1502 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1504 mpfr_cosh (result->value.real, x->value.real, GFC_RND_MODE);
1506 return range_check (result, "COSH");
1511 gfc_simplify_count (gfc_expr *mask, gfc_expr *dim, gfc_expr *kind)
1515 if (!is_constant_array_expr (mask)
1516 || !gfc_is_constant_expr (dim)
1517 || !gfc_is_constant_expr (kind))
1520 result = transformational_result (mask, dim,
1522 get_kind (BT_INTEGER, kind, "COUNT",
1523 gfc_default_integer_kind),
1526 init_result_expr (result, 0, NULL);
1528 /* Passing MASK twice, once as data array, once as mask.
1529 Whenever gfc_count is called, '1' is added to the result. */
1530 return !dim || mask->rank == 1 ?
1531 simplify_transformation_to_scalar (result, mask, mask, gfc_count) :
1532 simplify_transformation_to_array (result, mask, dim, mask, gfc_count);
1537 gfc_simplify_dcmplx (gfc_expr *x, gfc_expr *y)
1540 if (x->expr_type != EXPR_CONSTANT
1541 || (y != NULL && y->expr_type != EXPR_CONSTANT))
1542 return only_convert_cmplx_boz (x, y, gfc_default_double_kind);
1544 return simplify_cmplx ("DCMPLX", x, y, gfc_default_double_kind);
1549 gfc_simplify_dble (gfc_expr *e)
1551 gfc_expr *result = NULL;
1553 if (e->expr_type != EXPR_CONSTANT)
1560 result = gfc_int2real (e, gfc_default_double_kind);
1564 result = gfc_real2real (e, gfc_default_double_kind);
1568 result = gfc_complex2real (e, gfc_default_double_kind);
1572 gfc_internal_error ("gfc_simplify_dble(): bad type at %L", &e->where);
1575 if (e->ts.type == BT_INTEGER && e->is_boz)
1580 ts.kind = gfc_default_double_kind;
1581 result = gfc_copy_expr (e);
1582 if (!gfc_convert_boz (result, &ts))
1584 gfc_free_expr (result);
1585 return &gfc_bad_expr;
1589 return range_check (result, "DBLE");
1594 gfc_simplify_digits (gfc_expr *x)
1598 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
1602 digits = gfc_integer_kinds[i].digits;
1607 digits = gfc_real_kinds[i].digits;
1614 return gfc_int_expr (digits);
1619 gfc_simplify_dim (gfc_expr *x, gfc_expr *y)
1624 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1627 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
1628 result = gfc_constant_result (x->ts.type, kind, &x->where);
1633 if (mpz_cmp (x->value.integer, y->value.integer) > 0)
1634 mpz_sub (result->value.integer, x->value.integer, y->value.integer);
1636 mpz_set_ui (result->value.integer, 0);
1641 if (mpfr_cmp (x->value.real, y->value.real) > 0)
1642 mpfr_sub (result->value.real, x->value.real, y->value.real,
1645 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
1650 gfc_internal_error ("gfc_simplify_dim(): Bad type");
1653 return range_check (result, "DIM");
1658 gfc_simplify_dot_product (gfc_expr *vector_a, gfc_expr *vector_b)
1662 if (!is_constant_array_expr (vector_a)
1663 || !is_constant_array_expr (vector_b))
1666 gcc_assert (vector_a->rank == 1);
1667 gcc_assert (vector_b->rank == 1);
1668 gcc_assert (gfc_compare_types (&vector_a->ts, &vector_b->ts));
1670 if (vector_a->value.constructor && vector_b->value.constructor)
1671 return compute_dot_product (vector_a->value.constructor, 1,
1672 vector_b->value.constructor, 1);
1674 /* Zero sized array ... */
1675 result = gfc_constant_result (vector_a->ts.type,
1678 init_result_expr (result, 0, NULL);
1684 gfc_simplify_dprod (gfc_expr *x, gfc_expr *y)
1686 gfc_expr *a1, *a2, *result;
1688 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1691 result = gfc_constant_result (BT_REAL, gfc_default_double_kind, &x->where);
1693 a1 = gfc_real2real (x, gfc_default_double_kind);
1694 a2 = gfc_real2real (y, gfc_default_double_kind);
1696 mpfr_mul (result->value.real, a1->value.real, a2->value.real, GFC_RND_MODE);
1701 return range_check (result, "DPROD");
1706 gfc_simplify_erf (gfc_expr *x)
1710 if (x->expr_type != EXPR_CONSTANT)
1713 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1715 mpfr_erf (result->value.real, x->value.real, GFC_RND_MODE);
1717 return range_check (result, "ERF");
1722 gfc_simplify_erfc (gfc_expr *x)
1726 if (x->expr_type != EXPR_CONSTANT)
1729 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1731 mpfr_erfc (result->value.real, x->value.real, GFC_RND_MODE);
1733 return range_check (result, "ERFC");
1737 /* Helper functions to simplify ERFC_SCALED(x) = ERFC(x) * EXP(X**2). */
1739 #define MAX_ITER 200
1740 #define ARG_LIMIT 12
1742 /* Calculate ERFC_SCALED directly by its definition:
1744 ERFC_SCALED(x) = ERFC(x) * EXP(X**2)
1746 using a large precision for intermediate results. This is used for all
1747 but large values of the argument. */
1749 fullprec_erfc_scaled (mpfr_t res, mpfr_t arg)
1754 prec = mpfr_get_default_prec ();
1755 mpfr_set_default_prec (10 * prec);
1760 mpfr_set (a, arg, GFC_RND_MODE);
1761 mpfr_sqr (b, a, GFC_RND_MODE);
1762 mpfr_exp (b, b, GFC_RND_MODE);
1763 mpfr_erfc (a, a, GFC_RND_MODE);
1764 mpfr_mul (a, a, b, GFC_RND_MODE);
1766 mpfr_set (res, a, GFC_RND_MODE);
1767 mpfr_set_default_prec (prec);
1773 /* Calculate ERFC_SCALED using a power series expansion in 1/arg:
1775 ERFC_SCALED(x) = 1 / (x * sqrt(pi))
1776 * (1 + Sum_n (-1)**n * (1 * 3 * 5 * ... * (2n-1))
1779 This is used for large values of the argument. Intermediate calculations
1780 are performed with twice the precision. We don't do a fixed number of
1781 iterations of the sum, but stop when it has converged to the required
1784 asympt_erfc_scaled (mpfr_t res, mpfr_t arg)
1786 mpfr_t sum, x, u, v, w, oldsum, sumtrunc;
1791 prec = mpfr_get_default_prec ();
1792 mpfr_set_default_prec (2 * prec);
1802 mpfr_init (sumtrunc);
1803 mpfr_set_prec (oldsum, prec);
1804 mpfr_set_prec (sumtrunc, prec);
1806 mpfr_set (x, arg, GFC_RND_MODE);
1807 mpfr_set_ui (sum, 1, GFC_RND_MODE);
1808 mpz_set_ui (num, 1);
1810 mpfr_set (u, x, GFC_RND_MODE);
1811 mpfr_sqr (u, u, GFC_RND_MODE);
1812 mpfr_mul_ui (u, u, 2, GFC_RND_MODE);
1813 mpfr_pow_si (u, u, -1, GFC_RND_MODE);
1815 for (i = 1; i < MAX_ITER; i++)
1817 mpfr_set (oldsum, sum, GFC_RND_MODE);
1819 mpz_mul_ui (num, num, 2 * i - 1);
1822 mpfr_set (w, u, GFC_RND_MODE);
1823 mpfr_pow_ui (w, w, i, GFC_RND_MODE);
1825 mpfr_set_z (v, num, GFC_RND_MODE);
1826 mpfr_mul (v, v, w, GFC_RND_MODE);
1828 mpfr_add (sum, sum, v, GFC_RND_MODE);
1830 mpfr_set (sumtrunc, sum, GFC_RND_MODE);
1831 if (mpfr_cmp (sumtrunc, oldsum) == 0)
1835 /* We should have converged by now; otherwise, ARG_LIMIT is probably
1837 gcc_assert (i < MAX_ITER);
1839 /* Divide by x * sqrt(Pi). */
1840 mpfr_const_pi (u, GFC_RND_MODE);
1841 mpfr_sqrt (u, u, GFC_RND_MODE);
1842 mpfr_mul (u, u, x, GFC_RND_MODE);
1843 mpfr_div (sum, sum, u, GFC_RND_MODE);
1845 mpfr_set (res, sum, GFC_RND_MODE);
1846 mpfr_set_default_prec (prec);
1848 mpfr_clears (sum, x, u, v, w, oldsum, sumtrunc, NULL);
1854 gfc_simplify_erfc_scaled (gfc_expr *x)
1858 if (x->expr_type != EXPR_CONSTANT)
1861 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1862 if (mpfr_cmp_d (x->value.real, ARG_LIMIT) >= 0)
1863 asympt_erfc_scaled (result->value.real, x->value.real);
1865 fullprec_erfc_scaled (result->value.real, x->value.real);
1867 return range_check (result, "ERFC_SCALED");
1875 gfc_simplify_epsilon (gfc_expr *e)
1880 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
1882 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
1884 mpfr_set (result->value.real, gfc_real_kinds[i].epsilon, GFC_RND_MODE);
1886 return range_check (result, "EPSILON");
1891 gfc_simplify_exp (gfc_expr *x)
1895 if (x->expr_type != EXPR_CONSTANT)
1898 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1903 mpfr_exp (result->value.real, x->value.real, GFC_RND_MODE);
1907 gfc_set_model_kind (x->ts.kind);
1909 mpc_exp (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1915 mpfr_exp (xq, x->value.complex.r, GFC_RND_MODE);
1916 mpfr_cos (xp, x->value.complex.i, GFC_RND_MODE);
1917 mpfr_mul (result->value.complex.r, xq, xp, GFC_RND_MODE);
1918 mpfr_sin (xp, x->value.complex.i, GFC_RND_MODE);
1919 mpfr_mul (result->value.complex.i, xq, xp, GFC_RND_MODE);
1920 mpfr_clears (xp, xq, NULL);
1926 gfc_internal_error ("in gfc_simplify_exp(): Bad type");
1929 return range_check (result, "EXP");
1933 gfc_simplify_exponent (gfc_expr *x)
1938 if (x->expr_type != EXPR_CONSTANT)
1941 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
1944 gfc_set_model (x->value.real);
1946 if (mpfr_sgn (x->value.real) == 0)
1948 mpz_set_ui (result->value.integer, 0);
1952 i = (int) mpfr_get_exp (x->value.real);
1953 mpz_set_si (result->value.integer, i);
1955 return range_check (result, "EXPONENT");
1960 gfc_simplify_float (gfc_expr *a)
1964 if (a->expr_type != EXPR_CONSTANT)
1973 ts.kind = gfc_default_real_kind;
1975 result = gfc_copy_expr (a);
1976 if (!gfc_convert_boz (result, &ts))
1978 gfc_free_expr (result);
1979 return &gfc_bad_expr;
1983 result = gfc_int2real (a, gfc_default_real_kind);
1984 return range_check (result, "FLOAT");
1989 gfc_simplify_floor (gfc_expr *e, gfc_expr *k)
1995 kind = get_kind (BT_INTEGER, k, "FLOOR", gfc_default_integer_kind);
1997 gfc_internal_error ("gfc_simplify_floor(): Bad kind");
1999 if (e->expr_type != EXPR_CONSTANT)
2002 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
2004 gfc_set_model_kind (kind);
2006 mpfr_floor (floor, e->value.real);
2008 gfc_mpfr_to_mpz (result->value.integer, floor, &e->where);
2012 return range_check (result, "FLOOR");
2017 gfc_simplify_fraction (gfc_expr *x)
2020 mpfr_t absv, exp, pow2;
2022 if (x->expr_type != EXPR_CONSTANT)
2025 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
2027 if (mpfr_sgn (x->value.real) == 0)
2029 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
2033 gfc_set_model_kind (x->ts.kind);
2038 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
2039 mpfr_log2 (exp, absv, GFC_RND_MODE);
2041 mpfr_trunc (exp, exp);
2042 mpfr_add_ui (exp, exp, 1, GFC_RND_MODE);
2044 mpfr_ui_pow (pow2, 2, exp, GFC_RND_MODE);
2046 mpfr_div (result->value.real, absv, pow2, GFC_RND_MODE);
2048 mpfr_clears (exp, absv, pow2, NULL);
2050 return range_check (result, "FRACTION");
2055 gfc_simplify_gamma (gfc_expr *x)
2059 if (x->expr_type != EXPR_CONSTANT)
2062 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
2064 mpfr_gamma (result->value.real, x->value.real, GFC_RND_MODE);
2066 return range_check (result, "GAMMA");
2071 gfc_simplify_huge (gfc_expr *e)
2076 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2078 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
2083 mpz_set (result->value.integer, gfc_integer_kinds[i].huge);
2087 mpfr_set (result->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
2099 gfc_simplify_hypot (gfc_expr *x, gfc_expr *y)
2103 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2106 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
2107 mpfr_hypot (result->value.real, x->value.real, y->value.real, GFC_RND_MODE);
2108 return range_check (result, "HYPOT");
2112 /* We use the processor's collating sequence, because all
2113 systems that gfortran currently works on are ASCII. */
2116 gfc_simplify_iachar (gfc_expr *e, gfc_expr *kind)
2121 if (e->expr_type != EXPR_CONSTANT)
2124 if (e->value.character.length != 1)
2126 gfc_error ("Argument of IACHAR at %L must be of length one", &e->where);
2127 return &gfc_bad_expr;
2130 index = e->value.character.string[0];
2132 if (gfc_option.warn_surprising && index > 127)
2133 gfc_warning ("Argument of IACHAR function at %L outside of range 0..127",
2136 if ((result = int_expr_with_kind (index, kind, "IACHAR")) == NULL)
2137 return &gfc_bad_expr;
2139 result->where = e->where;
2141 return range_check (result, "IACHAR");
2146 gfc_simplify_iand (gfc_expr *x, gfc_expr *y)
2150 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2153 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
2155 mpz_and (result->value.integer, x->value.integer, y->value.integer);
2157 return range_check (result, "IAND");
2162 gfc_simplify_ibclr (gfc_expr *x, gfc_expr *y)
2167 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2170 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
2172 gfc_error ("Invalid second argument of IBCLR at %L", &y->where);
2173 return &gfc_bad_expr;
2176 k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
2178 if (pos >= gfc_integer_kinds[k].bit_size)
2180 gfc_error ("Second argument of IBCLR exceeds bit size at %L",
2182 return &gfc_bad_expr;
2185 result = gfc_copy_expr (x);
2187 convert_mpz_to_unsigned (result->value.integer,
2188 gfc_integer_kinds[k].bit_size);
2190 mpz_clrbit (result->value.integer, pos);
2192 convert_mpz_to_signed (result->value.integer,
2193 gfc_integer_kinds[k].bit_size);
2200 gfc_simplify_ibits (gfc_expr *x, gfc_expr *y, gfc_expr *z)
2207 if (x->expr_type != EXPR_CONSTANT
2208 || y->expr_type != EXPR_CONSTANT
2209 || z->expr_type != EXPR_CONSTANT)
2212 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
2214 gfc_error ("Invalid second argument of IBITS at %L", &y->where);
2215 return &gfc_bad_expr;
2218 if (gfc_extract_int (z, &len) != NULL || len < 0)
2220 gfc_error ("Invalid third argument of IBITS at %L", &z->where);
2221 return &gfc_bad_expr;
2224 k = gfc_validate_kind (BT_INTEGER, x->ts.kind, false);
2226 bitsize = gfc_integer_kinds[k].bit_size;
2228 if (pos + len > bitsize)
2230 gfc_error ("Sum of second and third arguments of IBITS exceeds "
2231 "bit size at %L", &y->where);
2232 return &gfc_bad_expr;
2235 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
2236 convert_mpz_to_unsigned (result->value.integer,
2237 gfc_integer_kinds[k].bit_size);
2239 bits = XCNEWVEC (int, bitsize);
2241 for (i = 0; i < bitsize; i++)
2244 for (i = 0; i < len; i++)
2245 bits[i] = mpz_tstbit (x->value.integer, i + pos);
2247 for (i = 0; i < bitsize; i++)
2250 mpz_clrbit (result->value.integer, i);
2251 else if (bits[i] == 1)
2252 mpz_setbit (result->value.integer, i);
2254 gfc_internal_error ("IBITS: Bad bit");
2259 convert_mpz_to_signed (result->value.integer,
2260 gfc_integer_kinds[k].bit_size);
2267 gfc_simplify_ibset (gfc_expr *x, gfc_expr *y)
2272 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2275 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
2277 gfc_error ("Invalid second argument of IBSET at %L", &y->where);
2278 return &gfc_bad_expr;
2281 k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
2283 if (pos >= gfc_integer_kinds[k].bit_size)
2285 gfc_error ("Second argument of IBSET exceeds bit size at %L",
2287 return &gfc_bad_expr;
2290 result = gfc_copy_expr (x);
2292 convert_mpz_to_unsigned (result->value.integer,
2293 gfc_integer_kinds[k].bit_size);
2295 mpz_setbit (result->value.integer, pos);
2297 convert_mpz_to_signed (result->value.integer,
2298 gfc_integer_kinds[k].bit_size);
2305 gfc_simplify_ichar (gfc_expr *e, gfc_expr *kind)
2310 if (e->expr_type != EXPR_CONSTANT)
2313 if (e->value.character.length != 1)
2315 gfc_error ("Argument of ICHAR at %L must be of length one", &e->where);
2316 return &gfc_bad_expr;
2319 index = e->value.character.string[0];
2321 if ((result = int_expr_with_kind (index, kind, "ICHAR")) == NULL)
2322 return &gfc_bad_expr;
2324 result->where = e->where;
2325 return range_check (result, "ICHAR");
2330 gfc_simplify_ieor (gfc_expr *x, gfc_expr *y)
2334 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2337 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
2339 mpz_xor (result->value.integer, x->value.integer, y->value.integer);
2341 return range_check (result, "IEOR");
2346 gfc_simplify_index (gfc_expr *x, gfc_expr *y, gfc_expr *b, gfc_expr *kind)
2349 int back, len, lensub;
2350 int i, j, k, count, index = 0, start;
2352 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT
2353 || ( b != NULL && b->expr_type != EXPR_CONSTANT))
2356 if (b != NULL && b->value.logical != 0)
2361 k = get_kind (BT_INTEGER, kind, "INDEX", gfc_default_integer_kind);
2363 return &gfc_bad_expr;
2365 result = gfc_constant_result (BT_INTEGER, k, &x->where);
2367 len = x->value.character.length;
2368 lensub = y->value.character.length;
2372 mpz_set_si (result->value.integer, 0);
2380 mpz_set_si (result->value.integer, 1);
2383 else if (lensub == 1)
2385 for (i = 0; i < len; i++)
2387 for (j = 0; j < lensub; j++)
2389 if (y->value.character.string[j]
2390 == x->value.character.string[i])
2400 for (i = 0; i < len; i++)
2402 for (j = 0; j < lensub; j++)
2404 if (y->value.character.string[j]
2405 == x->value.character.string[i])
2410 for (k = 0; k < lensub; k++)
2412 if (y->value.character.string[k]
2413 == x->value.character.string[k + start])
2417 if (count == lensub)
2432 mpz_set_si (result->value.integer, len + 1);
2435 else if (lensub == 1)
2437 for (i = 0; i < len; i++)
2439 for (j = 0; j < lensub; j++)
2441 if (y->value.character.string[j]
2442 == x->value.character.string[len - i])
2444 index = len - i + 1;
2452 for (i = 0; i < len; i++)
2454 for (j = 0; j < lensub; j++)
2456 if (y->value.character.string[j]
2457 == x->value.character.string[len - i])
2460 if (start <= len - lensub)
2463 for (k = 0; k < lensub; k++)
2464 if (y->value.character.string[k]
2465 == x->value.character.string[k + start])
2468 if (count == lensub)
2485 mpz_set_si (result->value.integer, index);
2486 return range_check (result, "INDEX");
2491 gfc_simplify_int (gfc_expr *e, gfc_expr *k)
2493 gfc_expr *result = NULL;
2496 kind = get_kind (BT_INTEGER, k, "INT", gfc_default_integer_kind);
2498 return &gfc_bad_expr;
2500 if (e->expr_type != EXPR_CONSTANT)
2506 result = gfc_int2int (e, kind);
2510 result = gfc_real2int (e, kind);
2514 result = gfc_complex2int (e, kind);
2518 gfc_error ("Argument of INT at %L is not a valid type", &e->where);
2519 return &gfc_bad_expr;
2522 return range_check (result, "INT");
2527 simplify_intconv (gfc_expr *e, int kind, const char *name)
2529 gfc_expr *result = NULL;
2531 if (e->expr_type != EXPR_CONSTANT)
2537 result = gfc_int2int (e, kind);
2541 result = gfc_real2int (e, kind);
2545 result = gfc_complex2int (e, kind);
2549 gfc_error ("Argument of %s at %L is not a valid type", name, &e->where);
2550 return &gfc_bad_expr;
2553 return range_check (result, name);
2558 gfc_simplify_int2 (gfc_expr *e)
2560 return simplify_intconv (e, 2, "INT2");
2565 gfc_simplify_int8 (gfc_expr *e)
2567 return simplify_intconv (e, 8, "INT8");
2572 gfc_simplify_long (gfc_expr *e)
2574 return simplify_intconv (e, 4, "LONG");
2579 gfc_simplify_ifix (gfc_expr *e)
2581 gfc_expr *rtrunc, *result;
2583 if (e->expr_type != EXPR_CONSTANT)
2586 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
2589 rtrunc = gfc_copy_expr (e);
2591 mpfr_trunc (rtrunc->value.real, e->value.real);
2592 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real, &e->where);
2594 gfc_free_expr (rtrunc);
2595 return range_check (result, "IFIX");
2600 gfc_simplify_idint (gfc_expr *e)
2602 gfc_expr *rtrunc, *result;
2604 if (e->expr_type != EXPR_CONSTANT)
2607 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
2610 rtrunc = gfc_copy_expr (e);
2612 mpfr_trunc (rtrunc->value.real, e->value.real);
2613 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real, &e->where);
2615 gfc_free_expr (rtrunc);
2616 return range_check (result, "IDINT");
2621 gfc_simplify_ior (gfc_expr *x, gfc_expr *y)
2625 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2628 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
2630 mpz_ior (result->value.integer, x->value.integer, y->value.integer);
2631 return range_check (result, "IOR");
2636 gfc_simplify_is_iostat_end (gfc_expr *x)
2640 if (x->expr_type != EXPR_CONSTANT)
2643 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
2645 result->value.logical = (mpz_cmp_si (x->value.integer, LIBERROR_END) == 0);
2652 gfc_simplify_is_iostat_eor (gfc_expr *x)
2656 if (x->expr_type != EXPR_CONSTANT)
2659 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
2661 result->value.logical = (mpz_cmp_si (x->value.integer, LIBERROR_EOR) == 0);
2668 gfc_simplify_isnan (gfc_expr *x)
2672 if (x->expr_type != EXPR_CONSTANT)
2675 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
2677 result->value.logical = mpfr_nan_p (x->value.real);
2684 gfc_simplify_ishft (gfc_expr *e, gfc_expr *s)
2687 int shift, ashift, isize, k, *bits, i;
2689 if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
2692 if (gfc_extract_int (s, &shift) != NULL)
2694 gfc_error ("Invalid second argument of ISHFT at %L", &s->where);
2695 return &gfc_bad_expr;
2698 k = gfc_validate_kind (BT_INTEGER, e->ts.kind, false);
2700 isize = gfc_integer_kinds[k].bit_size;
2709 gfc_error ("Magnitude of second argument of ISHFT exceeds bit size "
2710 "at %L", &s->where);
2711 return &gfc_bad_expr;
2714 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
2718 mpz_set (result->value.integer, e->value.integer);
2719 return range_check (result, "ISHFT");
2722 bits = XCNEWVEC (int, isize);
2724 for (i = 0; i < isize; i++)
2725 bits[i] = mpz_tstbit (e->value.integer, i);
2729 for (i = 0; i < shift; i++)
2730 mpz_clrbit (result->value.integer, i);
2732 for (i = 0; i < isize - shift; i++)
2735 mpz_clrbit (result->value.integer, i + shift);
2737 mpz_setbit (result->value.integer, i + shift);
2742 for (i = isize - 1; i >= isize - ashift; i--)
2743 mpz_clrbit (result->value.integer, i);
2745 for (i = isize - 1; i >= ashift; i--)
2748 mpz_clrbit (result->value.integer, i - ashift);
2750 mpz_setbit (result->value.integer, i - ashift);
2754 convert_mpz_to_signed (result->value.integer, isize);
2762 gfc_simplify_ishftc (gfc_expr *e, gfc_expr *s, gfc_expr *sz)
2765 int shift, ashift, isize, ssize, delta, k;
2768 if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
2771 if (gfc_extract_int (s, &shift) != NULL)
2773 gfc_error ("Invalid second argument of ISHFTC at %L", &s->where);
2774 return &gfc_bad_expr;
2777 k = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2778 isize = gfc_integer_kinds[k].bit_size;
2782 if (sz->expr_type != EXPR_CONSTANT)
2785 if (gfc_extract_int (sz, &ssize) != NULL || ssize <= 0)
2787 gfc_error ("Invalid third argument of ISHFTC at %L", &sz->where);
2788 return &gfc_bad_expr;
2793 gfc_error ("Magnitude of third argument of ISHFTC exceeds "
2794 "BIT_SIZE of first argument at %L", &s->where);
2795 return &gfc_bad_expr;
2809 gfc_error ("Magnitude of second argument of ISHFTC exceeds "
2810 "third argument at %L", &s->where);
2812 gfc_error ("Magnitude of second argument of ISHFTC exceeds "
2813 "BIT_SIZE of first argument at %L", &s->where);
2814 return &gfc_bad_expr;
2817 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
2819 mpz_set (result->value.integer, e->value.integer);
2824 convert_mpz_to_unsigned (result->value.integer, isize);
2826 bits = XCNEWVEC (int, ssize);
2828 for (i = 0; i < ssize; i++)
2829 bits[i] = mpz_tstbit (e->value.integer, i);
2831 delta = ssize - ashift;
2835 for (i = 0; i < delta; i++)
2838 mpz_clrbit (result->value.integer, i + shift);
2840 mpz_setbit (result->value.integer, i + shift);
2843 for (i = delta; i < ssize; i++)
2846 mpz_clrbit (result->value.integer, i - delta);
2848 mpz_setbit (result->value.integer, i - delta);
2853 for (i = 0; i < ashift; i++)
2856 mpz_clrbit (result->value.integer, i + delta);
2858 mpz_setbit (result->value.integer, i + delta);
2861 for (i = ashift; i < ssize; i++)
2864 mpz_clrbit (result->value.integer, i + shift);
2866 mpz_setbit (result->value.integer, i + shift);
2870 convert_mpz_to_signed (result->value.integer, isize);
2878 gfc_simplify_kind (gfc_expr *e)
2881 if (e->ts.type == BT_DERIVED)
2883 gfc_error ("Argument of KIND at %L is a DERIVED type", &e->where);
2884 return &gfc_bad_expr;
2887 return gfc_int_expr (e->ts.kind);
2892 simplify_bound_dim (gfc_expr *array, gfc_expr *kind, int d, int upper,
2893 gfc_array_spec *as, gfc_ref *ref)
2895 gfc_expr *l, *u, *result;
2898 /* The last dimension of an assumed-size array is special. */
2899 if (d == as->rank && as->type == AS_ASSUMED_SIZE && !upper)
2901 if (as->lower[d-1]->expr_type == EXPR_CONSTANT)
2902 return gfc_copy_expr (as->lower[d-1]);
2907 k = get_kind (BT_INTEGER, kind, upper ? "UBOUND" : "LBOUND",
2908 gfc_default_integer_kind);
2910 return &gfc_bad_expr;
2912 result = gfc_constant_result (BT_INTEGER, k, &array->where);
2915 /* Then, we need to know the extent of the given dimension. */
2916 if (ref->u.ar.type == AR_FULL)
2921 if (l->expr_type != EXPR_CONSTANT || u->expr_type != EXPR_CONSTANT)
2924 if (mpz_cmp (l->value.integer, u->value.integer) > 0)
2928 mpz_set_si (result->value.integer, 0);
2930 mpz_set_si (result->value.integer, 1);
2934 /* Nonzero extent. */
2936 mpz_set (result->value.integer, u->value.integer);
2938 mpz_set (result->value.integer, l->value.integer);
2945 if (gfc_ref_dimen_size (&ref->u.ar, d-1, &result->value.integer)
2950 mpz_set_si (result->value.integer, (long int) 1);
2953 return range_check (result, upper ? "UBOUND" : "LBOUND");
2958 simplify_bound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind, int upper)
2964 if (array->expr_type != EXPR_VARIABLE)
2967 /* Follow any component references. */
2968 as = array->symtree->n.sym->as;
2969 for (ref = array->ref; ref; ref = ref->next)
2974 switch (ref->u.ar.type)
2981 /* We're done because 'as' has already been set in the
2982 previous iteration. */
2999 as = ref->u.c.component->as;
3011 if (as->type == AS_DEFERRED || as->type == AS_ASSUMED_SHAPE)
3016 /* Multi-dimensional bounds. */
3017 gfc_expr *bounds[GFC_MAX_DIMENSIONS];
3019 gfc_constructor *head, *tail;
3022 /* UBOUND(ARRAY) is not valid for an assumed-size array. */
3023 if (upper && as->type == AS_ASSUMED_SIZE)
3025 /* An error message will be emitted in
3026 check_assumed_size_reference (resolve.c). */
3027 return &gfc_bad_expr;
3030 /* Simplify the bounds for each dimension. */
3031 for (d = 0; d < array->rank; d++)
3033 bounds[d] = simplify_bound_dim (array, kind, d + 1, upper, as, ref);
3034 if (bounds[d] == NULL || bounds[d] == &gfc_bad_expr)
3038 for (j = 0; j < d; j++)
3039 gfc_free_expr (bounds[j]);
3044 /* Allocate the result expression. */
3045 e = gfc_get_expr ();
3046 e->where = array->where;
3047 e->expr_type = EXPR_ARRAY;
3048 e->ts.type = BT_INTEGER;
3049 k = get_kind (BT_INTEGER, kind, upper ? "UBOUND" : "LBOUND",
3050 gfc_default_integer_kind);
3054 return &gfc_bad_expr;
3058 /* The result is a rank 1 array; its size is the rank of the first
3059 argument to {L,U}BOUND. */
3061 e->shape = gfc_get_shape (1);
3062 mpz_init_set_ui (e->shape[0], array->rank);
3064 /* Create the constructor for this array. */
3066 for (d = 0; d < array->rank; d++)
3068 /* Get a new constructor element. */
3070 head = tail = gfc_get_constructor ();
3073 tail->next = gfc_get_constructor ();
3077 tail->where = e->where;
3078 tail->expr = bounds[d];
3080 e->value.constructor = head;
3086 /* A DIM argument is specified. */
3087 if (dim->expr_type != EXPR_CONSTANT)
3090 d = mpz_get_si (dim->value.integer);
3092 if (d < 1 || d > as->rank
3093 || (d == as->rank && as->type == AS_ASSUMED_SIZE && upper))
3095 gfc_error ("DIM argument at %L is out of bounds", &dim->where);
3096 return &gfc_bad_expr;
3099 return simplify_bound_dim (array, kind, d, upper, as, ref);
3105 gfc_simplify_lbound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
3107 return simplify_bound (array, dim, kind, 0);
3112 gfc_simplify_leadz (gfc_expr *e)
3115 unsigned long lz, bs;
3118 if (e->expr_type != EXPR_CONSTANT)
3121 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
3122 bs = gfc_integer_kinds[i].bit_size;
3123 if (mpz_cmp_si (e->value.integer, 0) == 0)
3125 else if (mpz_cmp_si (e->value.integer, 0) < 0)
3128 lz = bs - mpz_sizeinbase (e->value.integer, 2);
3130 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
3132 mpz_set_ui (result->value.integer, lz);
3139 gfc_simplify_len (gfc_expr *e, gfc_expr *kind)
3142 int k = get_kind (BT_INTEGER, kind, "LEN", gfc_default_integer_kind);
3145 return &gfc_bad_expr;
3147 if (e->expr_type == EXPR_CONSTANT)
3149 result = gfc_constant_result (BT_INTEGER, k, &e->where);
3150 mpz_set_si (result->value.integer, e->value.character.length);
3151 if (gfc_range_check (result) == ARITH_OK)
3155 gfc_free_expr (result);
3160 if (e->ts.cl != NULL && e->ts.cl->length != NULL
3161 && e->ts.cl->length->expr_type == EXPR_CONSTANT
3162 && e->ts.cl->length->ts.type == BT_INTEGER)
3164 result = gfc_constant_result (BT_INTEGER, k, &e->where);
3165 mpz_set (result->value.integer, e->ts.cl->length->value.integer);
3166 if (gfc_range_check (result) == ARITH_OK)
3170 gfc_free_expr (result);
3180 gfc_simplify_len_trim (gfc_expr *e, gfc_expr *kind)
3183 int count, len, lentrim, i;
3184 int k = get_kind (BT_INTEGER, kind, "LEN_TRIM", gfc_default_integer_kind);
3187 return &gfc_bad_expr;
3189 if (e->expr_type != EXPR_CONSTANT)
3192 result = gfc_constant_result (BT_INTEGER, k, &e->where);
3193 len = e->value.character.length;
3195 for (count = 0, i = 1; i <= len; i++)
3196 if (e->value.character.string[len - i] == ' ')
3201 lentrim = len - count;
3203 mpz_set_si (result->value.integer, lentrim);
3204 return range_check (result, "LEN_TRIM");
3208 gfc_simplify_lgamma (gfc_expr *x ATTRIBUTE_UNUSED)
3213 if (x->expr_type != EXPR_CONSTANT)
3216 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3218 mpfr_lgamma (result->value.real, &sg, x->value.real, GFC_RND_MODE);
3220 return range_check (result, "LGAMMA");
3225 gfc_simplify_lge (gfc_expr *a, gfc_expr *b)
3227 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
3230 return gfc_logical_expr (gfc_compare_string (a, b) >= 0, &a->where);
3235 gfc_simplify_lgt (gfc_expr *a, gfc_expr *b)
3237 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
3240 return gfc_logical_expr (gfc_compare_string (a, b) > 0,
3246 gfc_simplify_lle (gfc_expr *a, gfc_expr *b)
3248 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
3251 return gfc_logical_expr (gfc_compare_string (a, b) <= 0, &a->where);
3256 gfc_simplify_llt (gfc_expr *a, gfc_expr *b)
3258 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
3261 return gfc_logical_expr (gfc_compare_string (a, b) < 0, &a->where);
3266 gfc_simplify_log (gfc_expr *x)
3270 if (x->expr_type != EXPR_CONSTANT)
3273 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3279 if (mpfr_sgn (x->value.real) <= 0)
3281 gfc_error ("Argument of LOG at %L cannot be less than or equal "
3282 "to zero", &x->where);
3283 gfc_free_expr (result);
3284 return &gfc_bad_expr;
3287 mpfr_log (result->value.real, x->value.real, GFC_RND_MODE);
3291 if ((mpfr_sgn (mpc_realref (x->value.complex)) == 0)
3292 && (mpfr_sgn (mpc_imagref (x->value.complex)) == 0))
3294 gfc_error ("Complex argument of LOG at %L cannot be zero",
3296 gfc_free_expr (result);
3297 return &gfc_bad_expr;
3300 gfc_set_model_kind (x->ts.kind);
3302 mpc_log (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
3309 mpfr_atan2 (result->value.complex.i, x->value.complex.i,
3310 x->value.complex.r, GFC_RND_MODE);
3312 mpfr_mul (xr, x->value.complex.r, x->value.complex.r, GFC_RND_MODE);
3313 mpfr_mul (xi, x->value.complex.i, x->value.complex.i, GFC_RND_MODE);
3314 mpfr_add (xr, xr, xi, GFC_RND_MODE);
3315 mpfr_sqrt (xr, xr, GFC_RND_MODE);
3316 mpfr_log (result->value.complex.r, xr, GFC_RND_MODE);
3318 mpfr_clears (xr, xi, NULL);
3324 gfc_internal_error ("gfc_simplify_log: bad type");
3327 return range_check (result, "LOG");
3332 gfc_simplify_log10 (gfc_expr *x)
3336 if (x->expr_type != EXPR_CONSTANT)
3339 if (mpfr_sgn (x->value.real) <= 0)
3341 gfc_error ("Argument of LOG10 at %L cannot be less than or equal "
3342 "to zero", &x->where);
3343 return &gfc_bad_expr;
3346 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3348 mpfr_log10 (result->value.real, x->value.real, GFC_RND_MODE);
3350 return range_check (result, "LOG10");
3355 gfc_simplify_logical (gfc_expr *e, gfc_expr *k)
3360 kind = get_kind (BT_LOGICAL, k, "LOGICAL", gfc_default_logical_kind);
3362 return &gfc_bad_expr;
3364 if (e->expr_type != EXPR_CONSTANT)
3367 result = gfc_constant_result (BT_LOGICAL, kind, &e->where);
3369 result->value.logical = e->value.logical;
3376 gfc_simplify_matmul (gfc_expr *matrix_a, gfc_expr *matrix_b)
3379 gfc_constructor *ma_ctor, *mb_ctor;
3380 int row, result_rows, col, result_columns, stride_a, stride_b;
3382 if (!is_constant_array_expr (matrix_a)
3383 || !is_constant_array_expr (matrix_b))
3386 gcc_assert (gfc_compare_types (&matrix_a->ts, &matrix_b->ts));
3387 result = gfc_start_constructor (matrix_a->ts.type,
3391 if (matrix_a->rank == 1 && matrix_b->rank == 2)
3394 result_columns = mpz_get_si (matrix_b->shape[0]);
3396 stride_b = mpz_get_si (matrix_b->shape[0]);
3399 result->shape = gfc_get_shape (result->rank);
3400 mpz_init_set_si (result->shape[0], result_columns);
3402 else if (matrix_a->rank == 2 && matrix_b->rank == 1)
3404 result_rows = mpz_get_si (matrix_b->shape[0]);
3406 stride_a = mpz_get_si (matrix_a->shape[0]);
3410 result->shape = gfc_get_shape (result->rank);
3411 mpz_init_set_si (result->shape[0], result_rows);
3413 else if (matrix_a->rank == 2 && matrix_b->rank == 2)
3415 result_rows = mpz_get_si (matrix_a->shape[0]);
3416 result_columns = mpz_get_si (matrix_b->shape[1]);
3417 stride_a = mpz_get_si (matrix_a->shape[1]);
3418 stride_b = mpz_get_si (matrix_b->shape[0]);
3421 result->shape = gfc_get_shape (result->rank);
3422 mpz_init_set_si (result->shape[0], result_rows);
3423 mpz_init_set_si (result->shape[1], result_columns);
3428 ma_ctor = matrix_a->value.constructor;
3429 mb_ctor = matrix_b->value.constructor;
3431 for (col = 0; col < result_columns; ++col)
3433 ma_ctor = matrix_a->value.constructor;
3435 for (row = 0; row < result_rows; ++row)
3438 e = compute_dot_product (ma_ctor, stride_a,
3441 gfc_append_constructor (result, e);
3443 ADVANCE (ma_ctor, 1);
3446 ADVANCE (mb_ctor, stride_b);
3454 gfc_simplify_merge (gfc_expr *tsource, gfc_expr *fsource, gfc_expr *mask)
3456 if (tsource->expr_type != EXPR_CONSTANT
3457 || fsource->expr_type != EXPR_CONSTANT
3458 || mask->expr_type != EXPR_CONSTANT)
3461 return gfc_copy_expr (mask->value.logical ? tsource : fsource);
3465 /* Selects bewteen current value and extremum for simplify_min_max
3466 and simplify_minval_maxval. */
3468 min_max_choose (gfc_expr *arg, gfc_expr *extremum, int sign)
3470 switch (arg->ts.type)
3473 if (mpz_cmp (arg->value.integer,
3474 extremum->value.integer) * sign > 0)
3475 mpz_set (extremum->value.integer, arg->value.integer);
3479 /* We need to use mpfr_min and mpfr_max to treat NaN properly. */
3481 mpfr_max (extremum->value.real, extremum->value.real,
3482 arg->value.real, GFC_RND_MODE);
3484 mpfr_min (extremum->value.real, extremum->value.real,
3485 arg->value.real, GFC_RND_MODE);
3489 #define LENGTH(x) ((x)->value.character.length)
3490 #define STRING(x) ((x)->value.character.string)
3491 if (LENGTH(extremum) < LENGTH(arg))
3493 gfc_char_t *tmp = STRING(extremum);
3495 STRING(extremum) = gfc_get_wide_string (LENGTH(arg) + 1);
3496 memcpy (STRING(extremum), tmp,
3497 LENGTH(extremum) * sizeof (gfc_char_t));
3498 gfc_wide_memset (&STRING(extremum)[LENGTH(extremum)], ' ',
3499 LENGTH(arg) - LENGTH(extremum));
3500 STRING(extremum)[LENGTH(arg)] = '\0'; /* For debugger */
3501 LENGTH(extremum) = LENGTH(arg);
3505 if (gfc_compare_string (arg, extremum) * sign > 0)
3507 gfc_free (STRING(extremum));
3508 STRING(extremum) = gfc_get_wide_string (LENGTH(extremum) + 1);
3509 memcpy (STRING(extremum), STRING(arg),
3510 LENGTH(arg) * sizeof (gfc_char_t));
3511 gfc_wide_memset (&STRING(extremum)[LENGTH(arg)], ' ',
3512 LENGTH(extremum) - LENGTH(arg));
3513 STRING(extremum)[LENGTH(extremum)] = '\0'; /* For debugger */
3520 gfc_internal_error ("simplify_min_max(): Bad type in arglist");
3525 /* This function is special since MAX() can take any number of
3526 arguments. The simplified expression is a rewritten version of the
3527 argument list containing at most one constant element. Other
3528 constant elements are deleted. Because the argument list has
3529 already been checked, this function always succeeds. sign is 1 for
3530 MAX(), -1 for MIN(). */
3533 simplify_min_max (gfc_expr *expr, int sign)
3535 gfc_actual_arglist *arg, *last, *extremum;
3536 gfc_intrinsic_sym * specific;
3540 specific = expr->value.function.isym;
3542 arg = expr->value.function.actual;
3544 for (; arg; last = arg, arg = arg->next)
3546 if (arg->expr->expr_type != EXPR_CONSTANT)
3549 if (extremum == NULL)
3555 min_max_choose (arg->expr, extremum->expr, sign);
3557 /* Delete the extra constant argument. */
3559 expr->value.function.actual = arg->next;
3561 last->next = arg->next;
3564 gfc_free_actual_arglist (arg);
3568 /* If there is one value left, replace the function call with the
3570 if (expr->value.function.actual->next != NULL)
3573 /* Convert to the correct type and kind. */
3574 if (expr->ts.type != BT_UNKNOWN)
3575 return gfc_convert_constant (expr->value.function.actual->expr,
3576 expr->ts.type, expr->ts.kind);
3578 if (specific->ts.type != BT_UNKNOWN)
3579 return gfc_convert_constant (expr->value.function.actual->expr,
3580 specific->ts.type, specific->ts.kind);
3582 return gfc_copy_expr (expr->value.function.actual->expr);
3587 gfc_simplify_min (gfc_expr *e)
3589 return simplify_min_max (e, -1);
3594 gfc_simplify_max (gfc_expr *e)
3596 return simplify_min_max (e, 1);
3600 /* This is a simplified version of simplify_min_max to provide
3601 simplification of minval and maxval for a vector. */
3604 simplify_minval_maxval (gfc_expr *expr, int sign)
3606 gfc_constructor *ctr, *extremum;
3607 gfc_intrinsic_sym * specific;
3610 specific = expr->value.function.isym;
3612 ctr = expr->value.constructor;
3614 for (; ctr; ctr = ctr->next)
3616 if (ctr->expr->expr_type != EXPR_CONSTANT)
3619 if (extremum == NULL)
3625 min_max_choose (ctr->expr, extremum->expr, sign);
3628 if (extremum == NULL)
3631 /* Convert to the correct type and kind. */
3632 if (expr->ts.type != BT_UNKNOWN)
3633 return gfc_convert_constant (extremum->expr,
3634 expr->ts.type, expr->ts.kind);
3636 if (specific->ts.type != BT_UNKNOWN)
3637 return gfc_convert_constant (extremum->expr,
3638 specific->ts.type, specific->ts.kind);
3640 return gfc_copy_expr (extremum->expr);
3645 gfc_simplify_minval (gfc_expr *array, gfc_expr* dim, gfc_expr *mask)
3647 if (array->expr_type != EXPR_ARRAY || array->rank != 1 || dim || mask)
3650 return simplify_minval_maxval (array, -1);
3655 gfc_simplify_maxval (gfc_expr *array, gfc_expr* dim, gfc_expr *mask)
3657 if (array->expr_type != EXPR_ARRAY || array->rank != 1 || dim || mask)
3659 return simplify_minval_maxval (array, 1);
3664 gfc_simplify_maxexponent (gfc_expr *x)
3669 i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
3671 result = gfc_int_expr (gfc_real_kinds[i].max_exponent);
3672 result->where = x->where;
3679 gfc_simplify_minexponent (gfc_expr *x)
3684 i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
3686 result = gfc_int_expr (gfc_real_kinds[i].min_exponent);
3687 result->where = x->where;
3694 gfc_simplify_mod (gfc_expr *a, gfc_expr *p)
3700 if (a->expr_type != EXPR_CONSTANT || p->expr_type != EXPR_CONSTANT)
3703 kind = a->ts.kind > p->ts.kind ? a->ts.kind : p->ts.kind;
3704 result = gfc_constant_result (a->ts.type, kind, &a->where);
3709 if (mpz_cmp_ui (p->value.integer, 0) == 0)
3711 /* Result is processor-dependent. */
3712 gfc_error ("Second argument MOD at %L is zero", &a->where);
3713 gfc_free_expr (result);
3714 return &gfc_bad_expr;
3716 mpz_tdiv_r (result->value.integer, a->value.integer, p->value.integer);
3720 if (mpfr_cmp_ui (p->value.real, 0) == 0)
3722 /* Result is processor-dependent. */
3723 gfc_error ("Second argument of MOD at %L is zero", &p->where);
3724 gfc_free_expr (result);
3725 return &gfc_bad_expr;
3728 gfc_set_model_kind (kind);
3730 mpfr_div (tmp, a->value.real, p->value.real, GFC_RND_MODE);
3731 mpfr_trunc (tmp, tmp);
3732 mpfr_mul (tmp, tmp, p->value.real, GFC_RND_MODE);
3733 mpfr_sub (result->value.real, a->value.real, tmp, GFC_RND_MODE);
3738 gfc_internal_error ("gfc_simplify_mod(): Bad arguments");
3741 return range_check (result, "MOD");
3746 gfc_simplify_modulo (gfc_expr *a, gfc_expr *p)
3752 if (a->expr_type != EXPR_CONSTANT || p->expr_type != EXPR_CONSTANT)
3755 kind = a->ts.kind > p->ts.kind ? a->ts.kind : p->ts.kind;
3756 result = gfc_constant_result (a->ts.type, kind, &a->where);
3761 if (mpz_cmp_ui (p->value.integer, 0) == 0)
3763 /* Result is processor-dependent. This processor just opts
3764 to not handle it at all. */
3765 gfc_error ("Second argument of MODULO at %L is zero", &a->where);
3766 gfc_free_expr (result);
3767 return &gfc_bad_expr;
3769 mpz_fdiv_r (result->value.integer, a->value.integer, p->value.integer);
3774 if (mpfr_cmp_ui (p->value.real, 0) == 0)
3776 /* Result is processor-dependent. */
3777 gfc_error ("Second argument of MODULO at %L is zero", &p->where);
3778 gfc_free_expr (result);
3779 return &gfc_bad_expr;
3782 gfc_set_model_kind (kind);
3784 mpfr_div (tmp, a->value.real, p->value.real, GFC_RND_MODE);
3785 mpfr_floor (tmp, tmp);
3786 mpfr_mul (tmp, tmp, p->value.real, GFC_RND_MODE);
3787 mpfr_sub (result->value.real, a->value.real, tmp, GFC_RND_MODE);
3792 gfc_internal_error ("gfc_simplify_modulo(): Bad arguments");
3795 return range_check (result, "MODULO");
3799 /* Exists for the sole purpose of consistency with other intrinsics. */
3801 gfc_simplify_mvbits (gfc_expr *f ATTRIBUTE_UNUSED,
3802 gfc_expr *fp ATTRIBUTE_UNUSED,
3803 gfc_expr *l ATTRIBUTE_UNUSED,
3804 gfc_expr *to ATTRIBUTE_UNUSED,
3805 gfc_expr *tp ATTRIBUTE_UNUSED)
3812 gfc_simplify_nearest (gfc_expr *x, gfc_expr *s)
3815 mp_exp_t emin, emax;
3818 if (x->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
3821 if (mpfr_sgn (s->value.real) == 0)
3823 gfc_error ("Second argument of NEAREST at %L shall not be zero",
3825 return &gfc_bad_expr;
3828 result = gfc_copy_expr (x);
3830 /* Save current values of emin and emax. */
3831 emin = mpfr_get_emin ();
3832 emax = mpfr_get_emax ();
3834 /* Set emin and emax for the current model number. */
3835 kind = gfc_validate_kind (BT_REAL, x->ts.kind, 0);
3836 mpfr_set_emin ((mp_exp_t) gfc_real_kinds[kind].min_exponent -
3837 mpfr_get_prec(result->value.real) + 1);
3838 mpfr_set_emax ((mp_exp_t) gfc_real_kinds[kind].max_exponent - 1);
3839 mpfr_check_range (result->value.real, 0, GMP_RNDU);
3841 if (mpfr_sgn (s->value.real) > 0)
3843 mpfr_nextabove (result->value.real);
3844 mpfr_subnormalize (result->value.real, 0, GMP_RNDU);
3848 mpfr_nextbelow (result->value.real);
3849 mpfr_subnormalize (result->value.real, 0, GMP_RNDD);
3852 mpfr_set_emin (emin);
3853 mpfr_set_emax (emax);
3855 /* Only NaN can occur. Do not use range check as it gives an
3856 error for denormal numbers. */
3857 if (mpfr_nan_p (result->value.real) && gfc_option.flag_range_check)
3859 gfc_error ("Result of NEAREST is NaN at %L", &result->where);
3860 gfc_free_expr (result);
3861 return &gfc_bad_expr;
3869 simplify_nint (const char *name, gfc_expr *e, gfc_expr *k)
3871 gfc_expr *itrunc, *result;
3874 kind = get_kind (BT_INTEGER, k, name, gfc_default_integer_kind);
3876 return &gfc_bad_expr;
3878 if (e->expr_type != EXPR_CONSTANT)
3881 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
3883 itrunc = gfc_copy_expr (e);
3885 mpfr_round (itrunc->value.real, e->value.real);
3887 gfc_mpfr_to_mpz (result->value.integer, itrunc->value.real, &e->where);
3889 gfc_free_expr (itrunc);
3891 return range_check (result, name);
3896 gfc_simplify_new_line (gfc_expr *e)
3900 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
3901 result->value.character.string = gfc_get_wide_string (2);
3902 result->value.character.length = 1;
3903 result->value.character.string[0] = '\n';
3904 result->value.character.string[1] = '\0'; /* For debugger */
3910 gfc_simplify_nint (gfc_expr *e, gfc_expr *k)
3912 return simplify_nint ("NINT", e, k);
3917 gfc_simplify_idnint (gfc_expr *e)
3919 return simplify_nint ("IDNINT", e, NULL);
3924 gfc_simplify_not (gfc_expr *e)
3928 if (e->expr_type != EXPR_CONSTANT)
3931 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
3933 mpz_com (result->value.integer, e->value.integer);
3935 return range_check (result, "NOT");
3940 gfc_simplify_null (gfc_expr *mold)
3946 result = gfc_get_expr ();
3947 result->ts.type = BT_UNKNOWN;
3950 result = gfc_copy_expr (mold);
3951 result->expr_type = EXPR_NULL;
3958 gfc_simplify_or (gfc_expr *x, gfc_expr *y)
3963 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
3966 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
3967 if (x->ts.type == BT_INTEGER)
3969 result = gfc_constant_result (BT_INTEGER, kind, &x->where);
3970 mpz_ior (result->value.integer, x->value.integer, y->value.integer);
3971 return range_check (result, "OR");
3973 else /* BT_LOGICAL */
3975 result = gfc_constant_result (BT_LOGICAL, kind, &x->where);
3976 result->value.logical = x->value.logical || y->value.logical;
3983 gfc_simplify_pack (gfc_expr *array, gfc_expr *mask, gfc_expr *vector)
3986 gfc_constructor *array_ctor, *mask_ctor, *vector_ctor;
3988 if (!is_constant_array_expr(array)
3989 || !is_constant_array_expr(vector)
3990 || (!gfc_is_constant_expr (mask)
3991 && !is_constant_array_expr(mask)))
3994 result = gfc_start_constructor (array->ts.type,
3998 array_ctor = array->value.constructor;
3999 vector_ctor = vector ? vector->value.constructor : NULL;
4001 if (mask->expr_type == EXPR_CONSTANT
4002 && mask->value.logical)
4004 /* Copy all elements of ARRAY to RESULT. */
4007 gfc_append_constructor (result,
4008 gfc_copy_expr (array_ctor->expr));
4010 ADVANCE (array_ctor, 1);
4011 ADVANCE (vector_ctor, 1);
4014 else if (mask->expr_type == EXPR_ARRAY)
4016 /* Copy only those elements of ARRAY to RESULT whose
4017 MASK equals .TRUE.. */
4018 mask_ctor = mask->value.constructor;
4021 if (mask_ctor->expr->value.logical)
4023 gfc_append_constructor (result,
4024 gfc_copy_expr (array_ctor->expr));
4025 ADVANCE (vector_ctor, 1);
4028 ADVANCE (array_ctor, 1);
4029 ADVANCE (mask_ctor, 1);
4033 /* Append any left-over elements from VECTOR to RESULT. */
4036 gfc_append_constructor (result,
4037 gfc_copy_expr (vector_ctor->expr));
4038 ADVANCE (vector_ctor, 1);
4041 result->shape = gfc_get_shape (1);
4042 gfc_array_size (result, &result->shape[0]);
4044 if (array->ts.type == BT_CHARACTER)
4045 result->ts.cl = array->ts.cl;
4052 gfc_simplify_precision (gfc_expr *e)
4057 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
4059 result = gfc_int_expr (gfc_real_kinds[i].precision);
4060 result->where = e->where;
4067 gfc_simplify_product (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
4071 if (!is_constant_array_expr (array)
4072 || !gfc_is_constant_expr (dim))
4076 && !is_constant_array_expr (mask)
4077 && mask->expr_type != EXPR_CONSTANT)
4080 result = transformational_result (array, dim, array->ts.type,
4081 array->ts.kind, &array->where);
4082 init_result_expr (result, 1, NULL);
4084 return !dim || array->rank == 1 ?
4085 simplify_transformation_to_scalar (result, array, mask, gfc_multiply) :
4086 simplify_transformation_to_array (result, array, dim, mask, gfc_multiply);
4091 gfc_simplify_radix (gfc_expr *e)
4096 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
4100 i = gfc_integer_kinds[i].radix;
4104 i = gfc_real_kinds[i].radix;
4111 result = gfc_int_expr (i);
4112 result->where = e->where;
4119 gfc_simplify_range (gfc_expr *e)
4125 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
4130 j = gfc_integer_kinds[i].range;
4135 j = gfc_real_kinds[i].range;
4142 result = gfc_int_expr (j);
4143 result->where = e->where;
4150 gfc_simplify_real (gfc_expr *e, gfc_expr *k)
4152 gfc_expr *result = NULL;
4155 if (e->ts.type == BT_COMPLEX)
4156 kind = get_kind (BT_REAL, k, "REAL", e->ts.kind);
4158 kind = get_kind (BT_REAL, k, "REAL", gfc_default_real_kind);
4161 return &gfc_bad_expr;
4163 if (e->expr_type != EXPR_CONSTANT)
4170 result = gfc_int2real (e, kind);
4174 result = gfc_real2real (e, kind);
4178 result = gfc_complex2real (e, kind);
4182 gfc_internal_error ("bad type in REAL");
4186 if (e->ts.type == BT_INTEGER && e->is_boz)
4192 result = gfc_copy_expr (e);
4193 if (!gfc_convert_boz (result, &ts))
4195 gfc_free_expr (result);
4196 return &gfc_bad_expr;
4200 return range_check (result, "REAL");
4205 gfc_simplify_realpart (gfc_expr *e)
4209 if (e->expr_type != EXPR_CONSTANT)
4212 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
4214 mpc_real (result->value.real, e->value.complex, GFC_RND_MODE);
4216 mpfr_set (result->value.real, e->value.complex.r, GFC_RND_MODE);
4219 return range_check (result, "REALPART");
4223 gfc_simplify_repeat (gfc_expr *e, gfc_expr *n)
4226 int i, j, len, ncop, nlen;
4228 bool have_length = false;
4230 /* If NCOPIES isn't a constant, there's nothing we can do. */
4231 if (n->expr_type != EXPR_CONSTANT)
4234 /* If NCOPIES is negative, it's an error. */
4235 if (mpz_sgn (n->value.integer) < 0)
4237 gfc_error ("Argument NCOPIES of REPEAT intrinsic is negative at %L",
4239 return &gfc_bad_expr;
4242 /* If we don't know the character length, we can do no more. */
4243 if (e->ts.cl && e->ts.cl->length
4244 && e->ts.cl->length->expr_type == EXPR_CONSTANT)
4246 len = mpz_get_si (e->ts.cl->length->value.integer);
4249 else if (e->expr_type == EXPR_CONSTANT
4250 && (e->ts.cl == NULL || e->ts.cl->length == NULL))
4252 len = e->value.character.length;
4257 /* If the source length is 0, any value of NCOPIES is valid
4258 and everything behaves as if NCOPIES == 0. */
4261 mpz_set_ui (ncopies, 0);
4263 mpz_set (ncopies, n->value.integer);
4265 /* Check that NCOPIES isn't too large. */
4271 /* Compute the maximum value allowed for NCOPIES: huge(cl) / len. */
4273 i = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4277 mpz_tdiv_q (max, gfc_integer_kinds[i].huge,
4278 e->ts.cl->length->value.integer);
4282 mpz_init_set_si (mlen, len);
4283 mpz_tdiv_q (max, gfc_integer_kinds[i].huge, mlen);
4287 /* The check itself. */
4288 if (mpz_cmp (ncopies, max) > 0)
4291 mpz_clear (ncopies);
4292 gfc_error ("Argument NCOPIES of REPEAT intrinsic is too large at %L",
4294 return &gfc_bad_expr;
4299 mpz_clear (ncopies);
4301 /* For further simplification, we need the character string to be
4303 if (e->expr_type != EXPR_CONSTANT)
4307 (e->ts.cl->length &&
4308 mpz_sgn (e->ts.cl->length->value.integer)) != 0)
4310 const char *res = gfc_extract_int (n, &ncop);
4311 gcc_assert (res == NULL);
4316 len = e->value.character.length;
4319 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
4323 result->value.character.string = gfc_get_wide_string (1);
4324 result->value.character.length = 0;
4325 result->value.character.string[0] = '\0';
4329 result->value.character.length = nlen;
4330 result->value.character.string = gfc_get_wide_string (nlen + 1);
4332 for (i = 0; i < ncop; i++)
4333 for (j = 0; j < len; j++)
4334 result->value.character.string[j+i*len]= e->value.character.string[j];
4336 result->value.character.string[nlen] = '\0'; /* For debugger */
4341 /* This one is a bear, but mainly has to do with shuffling elements. */
4344 gfc_simplify_reshape (gfc_expr *source, gfc_expr *shape_exp,
4345 gfc_expr *pad, gfc_expr *order_exp)
4347 int order[GFC_MAX_DIMENSIONS], shape[GFC_MAX_DIMENSIONS];
4348 int i, rank, npad, x[GFC_MAX_DIMENSIONS];
4349 gfc_constructor *head, *tail;
4355 /* Check that argument expression types are OK. */
4356 if (!is_constant_array_expr (source)
4357 || !is_constant_array_expr (shape_exp)
4358 || !is_constant_array_expr (pad)
4359 || !is_constant_array_expr (order_exp))
4362 /* Proceed with simplification, unpacking the array. */
4370 e = gfc_get_array_element (shape_exp, rank);
4374 gfc_extract_int (e, &shape[rank]);
4376 gcc_assert (rank >= 0 && rank < GFC_MAX_DIMENSIONS);
4377 gcc_assert (shape[rank] >= 0);
4383 gcc_assert (rank > 0);
4385 /* Now unpack the order array if present. */
4386 if (order_exp == NULL)
4388 for (i = 0; i < rank; i++)
4393 for (i = 0; i < rank; i++)
4396 for (i = 0; i < rank; i++)
4398 e = gfc_get_array_element (order_exp, i);
4401 gfc_extract_int (e, &order[i]);
4404 gcc_assert (order[i] >= 1 && order[i] <= rank);
4406 gcc_assert (x[order[i]] == 0);
4411 /* Count the elements in the source and padding arrays. */
4416 gfc_array_size (pad, &size);
4417 npad = mpz_get_ui (size);
4421 gfc_array_size (source, &size);
4422 nsource = mpz_get_ui (size);
4425 /* If it weren't for that pesky permutation we could just loop
4426 through the source and round out any shortage with pad elements.
4427 But no, someone just had to have the compiler do something the
4428 user should be doing. */
4430 for (i = 0; i < rank; i++)
4433 while (nsource > 0 || npad > 0)
4435 /* Figure out which element to extract. */
4436 mpz_set_ui (index, 0);
4438 for (i = rank - 1; i >= 0; i--)
4440 mpz_add_ui (index, index, x[order[i]]);
4442 mpz_mul_ui (index, index, shape[order[i - 1]]);
4445 if (mpz_cmp_ui (index, INT_MAX) > 0)
4446 gfc_internal_error ("Reshaped array too large at %C");
4448 j = mpz_get_ui (index);
4451 e = gfc_get_array_element (source, j);
4454 gcc_assert (npad > 0);
4458 e = gfc_get_array_element (pad, j);
4463 head = tail = gfc_get_constructor ();
4466 tail->next = gfc_get_constructor ();
4470 tail->where = e->where;
4473 /* Calculate the next element. */
4477 if (++x[i] < shape[i])
4488 e = gfc_get_expr ();
4489 e->where = source->where;
4490 e->expr_type = EXPR_ARRAY;
4491 e->value.constructor = head;
4492 e->shape = gfc_get_shape (rank);
4494 for (i = 0; i < rank; i++)
4495 mpz_init_set_ui (e->shape[i], shape[i]);
4505 gfc_simplify_rrspacing (gfc_expr *x)
4511 if (x->expr_type != EXPR_CONSTANT)
4514 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
4516 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
4518 mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE);
4520 /* Special case x = -0 and 0. */
4521 if (mpfr_sgn (result->value.real) == 0)
4523 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
4527 /* | x * 2**(-e) | * 2**p. */
4528 e = - (long int) mpfr_get_exp (x->value.real);
4529 mpfr_mul_2si (result->value.real, result->value.real, e, GFC_RND_MODE);
4531 p = (long int) gfc_real_kinds[i].digits;
4532 mpfr_mul_2si (result->value.real, result->value.real, p, GFC_RND_MODE);
4534 return range_check (result, "RRSPACING");
4539 gfc_simplify_scale (gfc_expr *x, gfc_expr *i)
4541 int k, neg_flag, power, exp_range;
4542 mpfr_t scale, radix;
4545 if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
4548 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
4550 if (mpfr_sgn (x->value.real) == 0)
4552 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
4556 k = gfc_validate_kind (BT_REAL, x->ts.kind, false);
4558 exp_range = gfc_real_kinds[k].max_exponent - gfc_real_kinds[k].min_exponent;
4560 /* This check filters out values of i that would overflow an int. */
4561 if (mpz_cmp_si (i->value.integer, exp_range + 2) > 0
4562 || mpz_cmp_si (i->value.integer, -exp_range - 2) < 0)
4564 gfc_error ("Result of SCALE overflows its kind at %L", &result->where);
4565 gfc_free_expr (result);
4566 return &gfc_bad_expr;
4569 /* Compute scale = radix ** power. */
4570 power = mpz_get_si (i->value.integer);
4580 gfc_set_model_kind (x->ts.kind);
4583 mpfr_set_ui (radix, gfc_real_kinds[k].radix, GFC_RND_MODE);
4584 mpfr_pow_ui (scale, radix, power, GFC_RND_MODE);
4587 mpfr_div (result->value.real, x->value.real, scale, GFC_RND_MODE);
4589 mpfr_mul (result->value.real, x->value.real, scale, GFC_RND_MODE);
4591 mpfr_clears (scale, radix, NULL);
4593 return range_check (result, "SCALE");
4597 /* Variants of strspn and strcspn that operate on wide characters. */
4600 wide_strspn (const gfc_char_t *s1, const gfc_char_t *s2)
4603 const gfc_char_t *c;
4607 for (c = s2; *c; c++)
4621 wide_strcspn (const gfc_char_t *s1, const gfc_char_t *s2)
4624 const gfc_char_t *c;
4628 for (c = s2; *c; c++)
4643 gfc_simplify_scan (gfc_expr *e, gfc_expr *c, gfc_expr *b, gfc_expr *kind)
4648 size_t indx, len, lenc;
4649 int k = get_kind (BT_INTEGER, kind, "SCAN", gfc_default_integer_kind);
4652 return &gfc_bad_expr;
4654 if (e->expr_type != EXPR_CONSTANT || c->expr_type != EXPR_CONSTANT)
4657 if (b != NULL && b->value.logical != 0)
4662 result = gfc_constant_result (BT_INTEGER, k, &e->where);
4664 len = e->value.character.length;
4665 lenc = c->value.character.length;
4667 if (len == 0 || lenc == 0)
4675 indx = wide_strcspn (e->value.character.string,
4676 c->value.character.string) + 1;
4683 for (indx = len; indx > 0; indx--)
4685 for (i = 0; i < lenc; i++)
4687 if (c->value.character.string[i]
4688 == e->value.character.string[indx - 1])
4696 mpz_set_ui (result->value.integer, indx);
4697 return range_check (result, "SCAN");
4702 gfc_simplify_selected_char_kind (gfc_expr *e)
4707 if (e->expr_type != EXPR_CONSTANT)
4710 if (gfc_compare_with_Cstring (e, "ascii", false) == 0
4711 || gfc_compare_with_Cstring (e, "default", false) == 0)
4713 else if (gfc_compare_with_Cstring (e, "iso_10646", false) == 0)
4718 result = gfc_int_expr (kind);
4719 result->where = e->where;
4726 gfc_simplify_selected_int_kind (gfc_expr *e)
4731 if (e->expr_type != EXPR_CONSTANT || gfc_extract_int (e, &range) != NULL)
4736 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
4737 if (gfc_integer_kinds[i].range >= range
4738 && gfc_integer_kinds[i].kind < kind)
4739 kind = gfc_integer_kinds[i].kind;
4741 if (kind == INT_MAX)
4744 result = gfc_int_expr (kind);
4745 result->where = e->where;
4752 gfc_simplify_selected_real_kind (gfc_expr *p, gfc_expr *q)
4754 int range, precision, i, kind, found_precision, found_range;
4761 if (p->expr_type != EXPR_CONSTANT
4762 || gfc_extract_int (p, &precision) != NULL)
4770 if (q->expr_type != EXPR_CONSTANT
4771 || gfc_extract_int (q, &range) != NULL)
4776 found_precision = 0;
4779 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
4781 if (gfc_real_kinds[i].precision >= precision)
4782 found_precision = 1;
4784 if (gfc_real_kinds[i].range >= range)
4787 if (gfc_real_kinds[i].precision >= precision
4788 && gfc_real_kinds[i].range >= range && gfc_real_kinds[i].kind < kind)
4789 kind = gfc_real_kinds[i].kind;
4792 if (kind == INT_MAX)
4796 if (!found_precision)
4802 result = gfc_int_expr (kind);
4803 result->where = (p != NULL) ? p->where : q->where;
4810 gfc_simplify_set_exponent (gfc_expr *x, gfc_expr *i)
4813 mpfr_t exp, absv, log2, pow2, frac;
4816 if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
4819 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
4821 if (mpfr_sgn (x->value.real) == 0)
4823 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
4827 gfc_set_model_kind (x->ts.kind);
4834 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
4835 mpfr_log2 (log2, absv, GFC_RND_MODE);
4837 mpfr_trunc (log2, log2);
4838 mpfr_add_ui (exp, log2, 1, GFC_RND_MODE);
4840 /* Old exponent value, and fraction. */
4841 mpfr_ui_pow (pow2, 2, exp, GFC_RND_MODE);
4843 mpfr_div (frac, absv, pow2, GFC_RND_MODE);
4846 exp2 = (unsigned long) mpz_get_d (i->value.integer);
4847 mpfr_mul_2exp (result->value.real, frac, exp2, GFC_RND_MODE);
4849 mpfr_clears (absv, log2, pow2, frac, NULL);
4851 return range_check (result, "SET_EXPONENT");
4856 gfc_simplify_shape (gfc_expr *source)
4858 mpz_t shape[GFC_MAX_DIMENSIONS];
4859 gfc_expr *result, *e, *f;
4864 if (source->rank == 0)
4865 return gfc_start_constructor (BT_INTEGER, gfc_default_integer_kind,
4868 if (source->expr_type != EXPR_VARIABLE)
4871 result = gfc_start_constructor (BT_INTEGER, gfc_default_integer_kind,
4874 ar = gfc_find_array_ref (source);
4876 t = gfc_array_ref_shape (ar, shape);
4878 for (n = 0; n < source->rank; n++)
4880 e = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
4885 mpz_set (e->value.integer, shape[n]);
4886 mpz_clear (shape[n]);
4890 mpz_set_ui (e->value.integer, n + 1);
4892 f = gfc_simplify_size (source, e, NULL);
4896 gfc_free_expr (result);
4905 gfc_append_constructor (result, e);
4913 gfc_simplify_size (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
4918 int k = get_kind (BT_INTEGER, kind, "SIZE", gfc_default_integer_kind);
4921 return &gfc_bad_expr;
4925 if (gfc_array_size (array, &size) == FAILURE)
4930 if (dim->expr_type != EXPR_CONSTANT)
4933 d = mpz_get_ui (dim->value.integer) - 1;
4934 if (gfc_array_dimen_size (array, d, &size) == FAILURE)
4938 result = gfc_constant_result (BT_INTEGER, k, &array->where);
4939 mpz_set (result->value.integer, size);
4945 gfc_simplify_sign (gfc_expr *x, gfc_expr *y)
4949 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
4952 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
4957 mpz_abs (result->value.integer, x->value.integer);
4958 if (mpz_sgn (y->value.integer) < 0)
4959 mpz_neg (result->value.integer, result->value.integer);
4964 /* TODO: Handle -0.0 and +0.0 correctly on machines that support
4966 mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE);
4967 if (mpfr_sgn (y->value.real) < 0)
4968 mpfr_neg (result->value.real, result->value.real, GFC_RND_MODE);
4973 gfc_internal_error ("Bad type in gfc_simplify_sign");
4981 gfc_simplify_sin (gfc_expr *x)
4985 if (x->expr_type != EXPR_CONSTANT)
4988 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
4993 mpfr_sin (result->value.real, x->value.real, GFC_RND_MODE);
4997 gfc_set_model (x->value.real);
4999 mpc_sin (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
5006 mpfr_sin (xp, x->value.complex.r, GFC_RND_MODE);
5007 mpfr_cosh (xq, x->value.complex.i, GFC_RND_MODE);
5008 mpfr_mul (result->value.complex.r, xp, xq, GFC_RND_MODE);
5010 mpfr_cos (xp, x->value.complex.r, GFC_RND_MODE);
5011 mpfr_sinh (xq, x->value.complex.i, GFC_RND_MODE);
5012 mpfr_mul (result->value.complex.i, xp, xq, GFC_RND_MODE);
5014 mpfr_clears (xp, xq, NULL);
5020 gfc_internal_error ("in gfc_simplify_sin(): Bad type");
5023 return range_check (result, "SIN");
5028 gfc_simplify_sinh (gfc_expr *x)
5032 if (x->expr_type != EXPR_CONSTANT)
5035 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
5037 mpfr_sinh (result->value.real, x->value.real, GFC_RND_MODE);
5039 return range_check (result, "SINH");
5043 /* The argument is always a double precision real that is converted to
5044 single precision. TODO: Rounding! */
5047 gfc_simplify_sngl (gfc_expr *a)
5051 if (a->expr_type != EXPR_CONSTANT)
5054 result = gfc_real2real (a, gfc_default_real_kind);
5055 return range_check (result, "SNGL");
5060 gfc_simplify_spacing (gfc_expr *x)
5066 if (x->expr_type != EXPR_CONSTANT)
5069 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
5071 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
5073 /* Special case x = 0 and -0. */
5074 mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE);
5075 if (mpfr_sgn (result->value.real) == 0)
5077 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
5081 /* In the Fortran 95 standard, the result is b**(e - p) where b, e, and p
5082 are the radix, exponent of x, and precision. This excludes the
5083 possibility of subnormal numbers. Fortran 2003 states the result is
5084 b**max(e - p, emin - 1). */
5086 ep = (long int) mpfr_get_exp (x->value.real) - gfc_real_kinds[i].digits;
5087 en = (long int) gfc_real_kinds[i].min_exponent - 1;
5088 en = en > ep ? en : ep;
5090 mpfr_set_ui (result->value.real, 1, GFC_RND_MODE);
5091 mpfr_mul_2si (result->value.real, result->value.real, en, GFC_RND_MODE);
5093 return range_check (result, "SPACING");
5098 gfc_simplify_spread (gfc_expr *source, gfc_expr *dim_expr, gfc_expr *ncopies_expr)
5100 gfc_expr *result = 0L;
5101 int i, j, dim, ncopies;
5104 if ((!gfc_is_constant_expr (source)
5105 && !is_constant_array_expr (source))
5106 || !gfc_is_constant_expr (dim_expr)
5107 || !gfc_is_constant_expr (ncopies_expr))
5110 gcc_assert (dim_expr->ts.type == BT_INTEGER);
5111 gfc_extract_int (dim_expr, &dim);
5112 dim -= 1; /* zero-base DIM */
5114 gcc_assert (ncopies_expr->ts.type == BT_INTEGER);
5115 gfc_extract_int (ncopies_expr, &ncopies);
5116 ncopies = MAX (ncopies, 0);
5118 /* Do not allow the array size to exceed the limit for an array
5120 if (source->expr_type == EXPR_ARRAY)
5122 if (gfc_array_size (source, &size) == FAILURE)
5123 gfc_internal_error ("Failure getting length of a constant array.");
5126 mpz_init_set_ui (size, 1);
5128 if (mpz_get_si (size)*ncopies > gfc_option.flag_max_array_constructor)
5131 if (source->expr_type == EXPR_CONSTANT)
5133 gcc_assert (dim == 0);
5135 result = gfc_start_constructor (source->ts.type,
5139 result->shape = gfc_get_shape (result->rank);
5140 mpz_init_set_si (result->shape[0], ncopies);
5142 for (i = 0; i < ncopies; ++i)
5143 gfc_append_constructor (result, gfc_copy_expr (source));
5145 else if (source->expr_type == EXPR_ARRAY)
5147 int result_size, rstride[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS];
5148 gfc_constructor *ctor, *source_ctor, *result_ctor;
5150 gcc_assert (source->rank < GFC_MAX_DIMENSIONS);
5151 gcc_assert (dim >= 0 && dim <= source->rank);
5153 result = gfc_start_constructor (source->ts.type,
5156 result->rank = source->rank + 1;
5157 result->shape = gfc_get_shape (result->rank);
5160 for (i = 0, j = 0; i < result->rank; ++i)
5163 mpz_init_set (result->shape[i], source->shape[j++]);
5165 mpz_init_set_si (result->shape[i], ncopies);
5167 extent[i] = mpz_get_si (result->shape[i]);
5168 rstride[i] = (i == 0) ? 1 : rstride[i-1] * extent[i-1];
5169 result_size *= extent[i];
5172 for (i = 0; i < result_size; ++i)
5173 gfc_append_constructor (result, NULL);
5175 source_ctor = source->value.constructor;
5176 result_ctor = result->value.constructor;
5181 for (i = 0; i < ncopies; ++i)
5183 ctor->expr = gfc_copy_expr (source_ctor->expr);
5184 ADVANCE (ctor, rstride[dim]);
5187 ADVANCE (result_ctor, (dim == 0 ? ncopies : 1));
5188 ADVANCE (source_ctor, 1);
5192 /* FIXME: Returning here avoids a regression in array_simplify_1.f90.
5193 Replace NULL with gcc_unreachable() after implementing
5194 gfc_simplify_cshift(). */
5197 if (source->ts.type == BT_CHARACTER)
5198 result->ts.cl = source->ts.cl;
5205 gfc_simplify_sqrt (gfc_expr *e)
5209 if (e->expr_type != EXPR_CONSTANT)
5212 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
5217 if (mpfr_cmp_si (e->value.real, 0) < 0)
5219 mpfr_sqrt (result->value.real, e->value.real, GFC_RND_MODE);
5224 gfc_set_model (e->value.real);
5226 mpc_sqrt (result->value.complex, e->value.complex, GFC_MPC_RND_MODE);
5229 /* Formula taken from Numerical Recipes to avoid over- and
5232 mpfr_t ac, ad, s, t, w;
5239 if (mpfr_cmp_ui (e->value.complex.r, 0) == 0
5240 && mpfr_cmp_ui (e->value.complex.i, 0) == 0)
5242 mpfr_set_ui (result->value.complex.r, 0, GFC_RND_MODE);
5243 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
5247 mpfr_abs (ac, e->value.complex.r, GFC_RND_MODE);
5248 mpfr_abs (ad, e->value.complex.i, GFC_RND_MODE);
5250 if (mpfr_cmp (ac, ad) >= 0)
5252 mpfr_div (t, e->value.complex.i, e->value.complex.r, GFC_RND_MODE);
5253 mpfr_mul (t, t, t, GFC_RND_MODE);
5254 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
5255 mpfr_sqrt (t, t, GFC_RND_MODE);
5256 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
5257 mpfr_div_ui (t, t, 2, GFC_RND_MODE);
5258 mpfr_sqrt (t, t, GFC_RND_MODE);
5259 mpfr_sqrt (s, ac, GFC_RND_MODE);
5260 mpfr_mul (w, s, t, GFC_RND_MODE);
5264 mpfr_div (s, e->value.complex.r, e->value.complex.i, GFC_RND_MODE);
5265 mpfr_mul (t, s, s, GFC_RND_MODE);
5266 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
5267 mpfr_sqrt (t, t, GFC_RND_MODE);
5268 mpfr_abs (s, s, GFC_RND_MODE);
5269 mpfr_add (t, t, s, GFC_RND_MODE);
5270 mpfr_div_ui (t, t, 2, GFC_RND_MODE);
5271 mpfr_sqrt (t, t, GFC_RND_MODE);
5272 mpfr_sqrt (s, ad, GFC_RND_MODE);
5273 mpfr_mul (w, s, t, GFC_RND_MODE);
5276 if (mpfr_cmp_ui (w, 0) != 0 && mpfr_cmp_ui (e->value.complex.r, 0) >= 0)
5278 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
5279 mpfr_div (result->value.complex.i, e->value.complex.i, t, GFC_RND_MODE);
5280 mpfr_set (result->value.complex.r, w, GFC_RND_MODE);
5282 else if (mpfr_cmp_ui (w, 0) != 0
5283 && mpfr_cmp_ui (e->value.complex.r, 0) < 0
5284 && mpfr_cmp_ui (e->value.complex.i, 0) >= 0)
5286 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
5287 mpfr_div (result->value.complex.r, e->value.complex.i, t, GFC_RND_MODE);
5288 mpfr_set (result->value.complex.i, w, GFC_RND_MODE);
5290 else if (mpfr_cmp_ui (w, 0) != 0
5291 && mpfr_cmp_ui (e->value.complex.r, 0) < 0
5292 && mpfr_cmp_ui (e->value.complex.i, 0) < 0)
5294 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
5295 mpfr_div (result->value.complex.r, ad, t, GFC_RND_MODE);
5296 mpfr_neg (w, w, GFC_RND_MODE);
5297 mpfr_set (result->value.complex.i, w, GFC_RND_MODE);
5300 gfc_internal_error ("invalid complex argument of SQRT at %L",
5303 mpfr_clears (s, t, ac, ad, w, NULL);
5309 gfc_internal_error ("invalid argument of SQRT at %L", &e->where);
5312 return range_check (result, "SQRT");
5315 gfc_free_expr (result);
5316 gfc_error ("Argument of SQRT at %L has a negative value", &e->where);
5317 return &gfc_bad_expr;
5322 gfc_simplify_sum (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
5326 if (!is_constant_array_expr (array)
5327 || !gfc_is_constant_expr (dim))
5331 && !is_constant_array_expr (mask)
5332 && mask->expr_type != EXPR_CONSTANT)
5335 result = transformational_result (array, dim, array->ts.type,
5336 array->ts.kind, &array->where);
5337 init_result_expr (result, 0, NULL);
5339 return !dim || array->rank == 1 ?
5340 simplify_transformation_to_scalar (result, array, mask, gfc_add) :
5341 simplify_transformation_to_array (result, array, dim, mask, gfc_add);
5346 gfc_simplify_tan (gfc_expr *x)
5351 if (x->expr_type != EXPR_CONSTANT)
5354 i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
5356 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
5358 mpfr_tan (result->value.real, x->value.real, GFC_RND_MODE);
5360 return range_check (result, "TAN");
5365 gfc_simplify_tanh (gfc_expr *x)
5369 if (x->expr_type != EXPR_CONSTANT)
5372 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
5374 mpfr_tanh (result->value.real, x->value.real, GFC_RND_MODE);
5376 return range_check (result, "TANH");
5382 gfc_simplify_tiny (gfc_expr *e)
5387 i = gfc_validate_kind (BT_REAL, e->ts.kind, false);
5389 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
5390 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
5397 gfc_simplify_trailz (gfc_expr *e)
5400 unsigned long tz, bs;
5403 if (e->expr_type != EXPR_CONSTANT)
5406 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
5407 bs = gfc_integer_kinds[i].bit_size;
5408 tz = mpz_scan1 (e->value.integer, 0);
5410 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind, &e->where);
5411 mpz_set_ui (result->value.integer, MIN (tz, bs));
5418 gfc_simplify_transfer (gfc_expr *source, gfc_expr *mold, gfc_expr *size)
5421 gfc_expr *mold_element;
5424 size_t result_elt_size;
5427 unsigned char *buffer;
5429 if (!gfc_is_constant_expr (source)
5430 || (gfc_init_expr && !gfc_is_constant_expr (mold))
5431 || !gfc_is_constant_expr (size))
5434 if (source->expr_type == EXPR_FUNCTION)
5437 /* Calculate the size of the source. */
5438 if (source->expr_type == EXPR_ARRAY
5439 && gfc_array_size (source, &tmp) == FAILURE)
5440 gfc_internal_error ("Failure getting length of a constant array.");
5442 source_size = gfc_target_expr_size (source);
5444 /* Create an empty new expression with the appropriate characteristics. */
5445 result = gfc_constant_result (mold->ts.type, mold->ts.kind,
5447 result->ts = mold->ts;
5449 mold_element = mold->expr_type == EXPR_ARRAY
5450 ? mold->value.constructor->expr
5453 /* Set result character length, if needed. Note that this needs to be
5454 set even for array expressions, in order to pass this information into
5455 gfc_target_interpret_expr. */
5456 if (result->ts.type == BT_CHARACTER && gfc_is_constant_expr (mold_element))
5457 result->value.character.length = mold_element->value.character.length;
5459 /* Set the number of elements in the result, and determine its size. */
5460 result_elt_size = gfc_target_expr_size (mold_element);
5461 if (result_elt_size == 0)
5463 gfc_free_expr (result);
5467 if (mold->expr_type == EXPR_ARRAY || mold->rank || size)
5471 result->expr_type = EXPR_ARRAY;
5475 result_length = (size_t)mpz_get_ui (size->value.integer);
5478 result_length = source_size / result_elt_size;
5479 if (result_length * result_elt_size < source_size)
5483 result->shape = gfc_get_shape (1);
5484 mpz_init_set_ui (result->shape[0], result_length);
5486 result_size = result_length * result_elt_size;
5491 result_size = result_elt_size;
5494 if (gfc_option.warn_surprising && source_size < result_size)
5495 gfc_warning("Intrinsic TRANSFER at %L has partly undefined result: "
5496 "source size %ld < result size %ld", &source->where,
5497 (long) source_size, (long) result_size);
5499 /* Allocate the buffer to store the binary version of the source. */
5500 buffer_size = MAX (source_size, result_size);
5501 buffer = (unsigned char*)alloca (buffer_size);
5502 memset (buffer, 0, buffer_size);
5504 /* Now write source to the buffer. */
5505 gfc_target_encode_expr (source, buffer, buffer_size);
5507 /* And read the buffer back into the new expression. */
5508 gfc_target_interpret_expr (buffer, buffer_size, result);
5515 gfc_simplify_transpose (gfc_expr *matrix)
5519 gfc_constructor *matrix_ctor;
5521 if (!is_constant_array_expr (matrix))
5524 gcc_assert (matrix->rank == 2);
5526 result = gfc_start_constructor (matrix->ts.type, matrix->ts.kind, &matrix->where);
5528 result->shape = gfc_get_shape (result->rank);
5529 mpz_set (result->shape[0], matrix->shape[1]);
5530 mpz_set (result->shape[1], matrix->shape[0]);
5532 if (matrix->ts.type == BT_CHARACTER)
5533 result->ts.cl = matrix->ts.cl;
5535 matrix_rows = mpz_get_si (matrix->shape[0]);
5536 matrix_ctor = matrix->value.constructor;
5537 for (i = 0; i < matrix_rows; ++i)
5539 gfc_constructor *column_ctor = matrix_ctor;
5542 gfc_append_constructor (result,
5543 gfc_copy_expr (column_ctor->expr));
5545 ADVANCE (column_ctor, matrix_rows);
5548 ADVANCE (matrix_ctor, 1);
5556 gfc_simplify_trim (gfc_expr *e)
5559 int count, i, len, lentrim;
5561 if (e->expr_type != EXPR_CONSTANT)
5564 len = e->value.character.length;
5566 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
5568 for (count = 0, i = 1; i <= len; ++i)
5570 if (e->value.character.string[len - i] == ' ')
5576 lentrim = len - count;
5578 result->value.character.length = lentrim;
5579 result->value.character.string = gfc_get_wide_string (lentrim + 1);
5581 for (i = 0; i < lentrim; i++)
5582 result->value.character.string[i] = e->value.character.string[i];
5584 result->value.character.string[lentrim] = '\0'; /* For debugger */
5591 gfc_simplify_ubound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
5593 return simplify_bound (array, dim, kind, 1);
5598 gfc_simplify_unpack (gfc_expr *vector, gfc_expr *mask, gfc_expr *field)
5600 gfc_expr *result, *e;
5601 gfc_constructor *vector_ctor, *mask_ctor, *field_ctor;
5603 if (!is_constant_array_expr (vector)
5604 || !is_constant_array_expr (mask)
5605 || (!gfc_is_constant_expr (field)
5606 && !is_constant_array_expr(field)))
5609 result = gfc_start_constructor (vector->ts.type,
5612 result->rank = mask->rank;
5613 result->shape = gfc_copy_shape (mask->shape, mask->rank);
5615 if (vector->ts.type == BT_CHARACTER)
5616 result->ts.cl = vector->ts.cl;
5618 vector_ctor = vector->value.constructor;
5619 mask_ctor = mask->value.constructor;
5620 field_ctor = field->expr_type == EXPR_ARRAY ? field->value.constructor : NULL;
5624 if (mask_ctor->expr->value.logical)
5626 gcc_assert (vector_ctor);
5627 e = gfc_copy_expr (vector_ctor->expr);
5628 ADVANCE (vector_ctor, 1);
5630 else if (field->expr_type == EXPR_ARRAY)
5631 e = gfc_copy_expr (field_ctor->expr);
5633 e = gfc_copy_expr (field);
5635 gfc_append_constructor (result, e);
5637 ADVANCE (mask_ctor, 1);
5638 ADVANCE (field_ctor, 1);
5646 gfc_simplify_verify (gfc_expr *s, gfc_expr *set, gfc_expr *b, gfc_expr *kind)
5650 size_t index, len, lenset;
5652 int k = get_kind (BT_INTEGER, kind, "VERIFY", gfc_default_integer_kind);
5655 return &gfc_bad_expr;
5657 if (s->expr_type != EXPR_CONSTANT || set->expr_type != EXPR_CONSTANT)
5660 if (b != NULL && b->value.logical != 0)
5665 result = gfc_constant_result (BT_INTEGER, k, &s->where);
5667 len = s->value.character.length;
5668 lenset = set->value.character.length;
5672 mpz_set_ui (result->value.integer, 0);
5680 mpz_set_ui (result->value.integer, 1);
5684 index = wide_strspn (s->value.character.string,
5685 set->value.character.string) + 1;
5694 mpz_set_ui (result->value.integer, len);
5697 for (index = len; index > 0; index --)
5699 for (i = 0; i < lenset; i++)
5701 if (s->value.character.string[index - 1]
5702 == set->value.character.string[i])
5710 mpz_set_ui (result->value.integer, index);
5716 gfc_simplify_xor (gfc_expr *x, gfc_expr *y)
5721 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
5724 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
5725 if (x->ts.type == BT_INTEGER)
5727 result = gfc_constant_result (BT_INTEGER, kind, &x->where);
5728 mpz_xor (result->value.integer, x->value.integer, y->value.integer);
5729 return range_check (result, "XOR");
5731 else /* BT_LOGICAL */
5733 result = gfc_constant_result (BT_LOGICAL, kind, &x->where);
5734 result->value.logical = (x->value.logical && !y->value.logical)
5735 || (!x->value.logical && y->value.logical);
5742 /****************** Constant simplification *****************/
5744 /* Master function to convert one constant to another. While this is
5745 used as a simplification function, it requires the destination type
5746 and kind information which is supplied by a special case in
5750 gfc_convert_constant (gfc_expr *e, bt type, int kind)
5752 gfc_expr *g, *result, *(*f) (gfc_expr *, int);
5753 gfc_constructor *head, *c, *tail = NULL;
5767 f = gfc_int2complex;
5787 f = gfc_real2complex;
5798 f = gfc_complex2int;
5801 f = gfc_complex2real;
5804 f = gfc_complex2complex;
5830 f = gfc_hollerith2int;
5834 f = gfc_hollerith2real;
5838 f = gfc_hollerith2complex;
5842 f = gfc_hollerith2character;
5846 f = gfc_hollerith2logical;
5856 gfc_internal_error ("gfc_convert_constant(): Unexpected type");
5861 switch (e->expr_type)
5864 result = f (e, kind);
5866 return &gfc_bad_expr;
5870 if (!gfc_is_constant_expr (e))
5875 for (c = e->value.constructor; c; c = c->next)
5878 head = tail = gfc_get_constructor ();
5881 tail->next = gfc_get_constructor ();
5885 tail->where = c->where;
5887 if (c->iterator == NULL)
5888 tail->expr = f (c->expr, kind);
5891 g = gfc_convert_constant (c->expr, type, kind);
5892 if (g == &gfc_bad_expr)
5897 if (tail->expr == NULL)
5899 gfc_free_constructor (head);
5904 result = gfc_get_expr ();
5905 result->ts.type = type;
5906 result->ts.kind = kind;
5907 result->expr_type = EXPR_ARRAY;
5908 result->value.constructor = head;
5909 result->shape = gfc_copy_shape (e->shape, e->rank);
5910 result->where = e->where;
5911 result->rank = e->rank;
5922 /* Function for converting character constants. */
5924 gfc_convert_char_constant (gfc_expr *e, bt type ATTRIBUTE_UNUSED, int kind)
5929 if (!gfc_is_constant_expr (e))
5932 if (e->expr_type == EXPR_CONSTANT)
5934 /* Simple case of a scalar. */
5935 result = gfc_constant_result (BT_CHARACTER, kind, &e->where);
5937 return &gfc_bad_expr;
5939 result->value.character.length = e->value.character.length;
5940 result->value.character.string
5941 = gfc_get_wide_string (e->value.character.length + 1);
5942 memcpy (result->value.character.string, e->value.character.string,
5943 (e->value.character.length + 1) * sizeof (gfc_char_t));
5945 /* Check we only have values representable in the destination kind. */
5946 for (i = 0; i < result->value.character.length; i++)
5947 if (!gfc_check_character_range (result->value.character.string[i],
5950 gfc_error ("Character '%s' in string at %L cannot be converted "
5951 "into character kind %d",
5952 gfc_print_wide_char (result->value.character.string[i]),
5954 return &gfc_bad_expr;
5959 else if (e->expr_type == EXPR_ARRAY)
5961 /* For an array constructor, we convert each constructor element. */
5962 gfc_constructor *head = NULL, *tail = NULL, *c;
5964 for (c = e->value.constructor; c; c = c->next)
5967 head = tail = gfc_get_constructor ();
5970 tail->next = gfc_get_constructor ();
5974 tail->where = c->where;
5975 tail->expr = gfc_convert_char_constant (c->expr, type, kind);
5976 if (tail->expr == &gfc_bad_expr)
5979 return &gfc_bad_expr;
5982 if (tail->expr == NULL)
5984 gfc_free_constructor (head);
5989 result = gfc_get_expr ();
5990 result->ts.type = type;
5991 result->ts.kind = kind;
5992 result->expr_type = EXPR_ARRAY;
5993 result->value.constructor = head;
5994 result->shape = gfc_copy_shape (e->shape, e->rank);
5995 result->where = e->where;
5996 result->rank = e->rank;
5997 result->ts.cl = e->ts.cl;