1 /* Simplify intrinsic functions at compile-time.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
3 2010, 2011 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/>. */
24 #include "coretypes.h"
28 #include "intrinsic.h"
29 #include "target-memory.h"
30 #include "constructor.h"
31 #include "version.h" /* For version_string. */
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.
50 An expression pointer to gfc_bad_expr (a static placeholder)
51 indicating that some error has prevented simplification. The
52 error is generated within the function and should be propagated
55 By the time a simplification function gets control, it has been
56 decided that the function call is really supposed to be the
57 intrinsic. No type checking is strictly necessary, since only
58 valid types will be passed on. On the other hand, a simplification
59 subroutine may have to look at the type of an argument as part of
62 Array arguments are only passed to these subroutines that implement
63 the simplification of transformational intrinsics.
65 The functions in this file don't have much comment with them, but
66 everything is reasonably straight-forward. The Standard, chapter 13
67 is the best comment you'll find for this file anyway. */
69 /* Range checks an expression node. If all goes well, returns the
70 node, otherwise returns &gfc_bad_expr and frees the node. */
73 range_check (gfc_expr *result, const char *name)
78 if (result->expr_type != EXPR_CONSTANT)
81 switch (gfc_range_check (result))
87 gfc_error ("Result of %s overflows its kind at %L", name,
92 gfc_error ("Result of %s underflows its kind at %L", name,
97 gfc_error ("Result of %s is NaN at %L", name, &result->where);
101 gfc_error ("Result of %s gives range error for its kind at %L", name,
106 gfc_free_expr (result);
107 return &gfc_bad_expr;
111 /* A helper function that gets an optional and possibly missing
112 kind parameter. Returns the kind, -1 if something went wrong. */
115 get_kind (bt type, gfc_expr *k, const char *name, int default_kind)
122 if (k->expr_type != EXPR_CONSTANT)
124 gfc_error ("KIND parameter of %s at %L must be an initialization "
125 "expression", name, &k->where);
129 if (gfc_extract_int (k, &kind) != NULL
130 || gfc_validate_kind (type, kind, true) < 0)
132 gfc_error ("Invalid KIND parameter of %s at %L", name, &k->where);
140 /* Converts an mpz_t signed variable into an unsigned one, assuming
141 two's complement representations and a binary width of bitsize.
142 The conversion is a no-op unless x is negative; otherwise, it can
143 be accomplished by masking out the high bits. */
146 convert_mpz_to_unsigned (mpz_t x, int bitsize)
152 /* Confirm that no bits above the signed range are unset. */
153 gcc_assert (mpz_scan0 (x, bitsize-1) == ULONG_MAX);
155 mpz_init_set_ui (mask, 1);
156 mpz_mul_2exp (mask, mask, bitsize);
157 mpz_sub_ui (mask, mask, 1);
159 mpz_and (x, x, mask);
165 /* Confirm that no bits above the signed range are set. */
166 gcc_assert (mpz_scan1 (x, bitsize-1) == ULONG_MAX);
171 /* Converts an mpz_t unsigned variable into a signed one, assuming
172 two's complement representations and a binary width of bitsize.
173 If the bitsize-1 bit is set, this is taken as a sign bit and
174 the number is converted to the corresponding negative number. */
177 convert_mpz_to_signed (mpz_t x, int bitsize)
181 /* Confirm that no bits above the unsigned range are set. */
182 gcc_assert (mpz_scan1 (x, bitsize) == ULONG_MAX);
184 if (mpz_tstbit (x, bitsize - 1) == 1)
186 mpz_init_set_ui (mask, 1);
187 mpz_mul_2exp (mask, mask, bitsize);
188 mpz_sub_ui (mask, mask, 1);
190 /* We negate the number by hand, zeroing the high bits, that is
191 make it the corresponding positive number, and then have it
192 negated by GMP, giving the correct representation of the
195 mpz_add_ui (x, x, 1);
196 mpz_and (x, x, mask);
205 /* In-place convert BOZ to REAL of the specified kind. */
208 convert_boz (gfc_expr *x, int kind)
210 if (x && x->ts.type == BT_INTEGER && x->is_boz)
217 if (!gfc_convert_boz (x, &ts))
218 return &gfc_bad_expr;
225 /* Test that the expression is an constant array. */
228 is_constant_array_expr (gfc_expr *e)
235 if (e->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (e))
238 for (c = gfc_constructor_first (e->value.constructor);
239 c; c = gfc_constructor_next (c))
240 if (c->expr->expr_type != EXPR_CONSTANT
241 && c->expr->expr_type != EXPR_STRUCTURE)
248 /* Initialize a transformational result expression with a given value. */
251 init_result_expr (gfc_expr *e, int init, gfc_expr *array)
253 if (e && e->expr_type == EXPR_ARRAY)
255 gfc_constructor *ctor = gfc_constructor_first (e->value.constructor);
258 init_result_expr (ctor->expr, init, array);
259 ctor = gfc_constructor_next (ctor);
262 else if (e && e->expr_type == EXPR_CONSTANT)
264 int i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
271 e->value.logical = (init ? 1 : 0);
276 mpz_set (e->value.integer, gfc_integer_kinds[i].min_int);
277 else if (init == INT_MAX)
278 mpz_set (e->value.integer, gfc_integer_kinds[i].huge);
280 mpz_set_si (e->value.integer, init);
286 mpfr_set (e->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
287 mpfr_neg (e->value.real, e->value.real, GFC_RND_MODE);
289 else if (init == INT_MAX)
290 mpfr_set (e->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
292 mpfr_set_si (e->value.real, init, GFC_RND_MODE);
296 mpc_set_si (e->value.complex, init, GFC_MPC_RND_MODE);
302 gfc_expr *len = gfc_simplify_len (array, NULL);
303 gfc_extract_int (len, &length);
304 string = gfc_get_wide_string (length + 1);
305 gfc_wide_memset (string, 0, length);
307 else if (init == INT_MAX)
309 gfc_expr *len = gfc_simplify_len (array, NULL);
310 gfc_extract_int (len, &length);
311 string = gfc_get_wide_string (length + 1);
312 gfc_wide_memset (string, 255, length);
317 string = gfc_get_wide_string (1);
320 string[length] = '\0';
321 e->value.character.length = length;
322 e->value.character.string = string;
334 /* Helper function for gfc_simplify_dot_product() and gfc_simplify_matmul. */
337 compute_dot_product (gfc_expr *matrix_a, int stride_a, int offset_a,
338 gfc_expr *matrix_b, int stride_b, int offset_b)
340 gfc_expr *result, *a, *b;
342 result = gfc_get_constant_expr (matrix_a->ts.type, matrix_a->ts.kind,
344 init_result_expr (result, 0, NULL);
346 a = gfc_constructor_lookup_expr (matrix_a->value.constructor, offset_a);
347 b = gfc_constructor_lookup_expr (matrix_b->value.constructor, offset_b);
350 /* Copying of expressions is required as operands are free'd
351 by the gfc_arith routines. */
352 switch (result->ts.type)
355 result = gfc_or (result,
356 gfc_and (gfc_copy_expr (a),
363 result = gfc_add (result,
364 gfc_multiply (gfc_copy_expr (a),
372 offset_a += stride_a;
373 a = gfc_constructor_lookup_expr (matrix_a->value.constructor, offset_a);
375 offset_b += stride_b;
376 b = gfc_constructor_lookup_expr (matrix_b->value.constructor, offset_b);
383 /* Build a result expression for transformational intrinsics,
387 transformational_result (gfc_expr *array, gfc_expr *dim, bt type,
388 int kind, locus* where)
393 if (!dim || array->rank == 1)
394 return gfc_get_constant_expr (type, kind, where);
396 result = gfc_get_array_expr (type, kind, where);
397 result->shape = gfc_copy_shape_excluding (array->shape, array->rank, dim);
398 result->rank = array->rank - 1;
400 /* gfc_array_size() would count the number of elements in the constructor,
401 we have not built those yet. */
403 for (i = 0; i < result->rank; ++i)
404 nelem *= mpz_get_ui (result->shape[i]);
406 for (i = 0; i < nelem; ++i)
408 gfc_constructor_append_expr (&result->value.constructor,
409 gfc_get_constant_expr (type, kind, where),
417 typedef gfc_expr* (*transformational_op)(gfc_expr*, gfc_expr*);
419 /* Wrapper function, implements 'op1 += 1'. Only called if MASK
420 of COUNT intrinsic is .TRUE..
422 Interface and implementation mimics arith functions as
423 gfc_add, gfc_multiply, etc. */
425 static gfc_expr* gfc_count (gfc_expr *op1, gfc_expr *op2)
429 gcc_assert (op1->ts.type == BT_INTEGER);
430 gcc_assert (op2->ts.type == BT_LOGICAL);
431 gcc_assert (op2->value.logical);
433 result = gfc_copy_expr (op1);
434 mpz_add_ui (result->value.integer, result->value.integer, 1);
442 /* Transforms an ARRAY with operation OP, according to MASK, to a
443 scalar RESULT. E.g. called if
445 REAL, PARAMETER :: array(n, m) = ...
446 REAL, PARAMETER :: s = SUM(array)
448 where OP == gfc_add(). */
451 simplify_transformation_to_scalar (gfc_expr *result, gfc_expr *array, gfc_expr *mask,
452 transformational_op op)
455 gfc_constructor *array_ctor, *mask_ctor;
457 /* Shortcut for constant .FALSE. MASK. */
459 && mask->expr_type == EXPR_CONSTANT
460 && !mask->value.logical)
463 array_ctor = gfc_constructor_first (array->value.constructor);
465 if (mask && mask->expr_type == EXPR_ARRAY)
466 mask_ctor = gfc_constructor_first (mask->value.constructor);
470 a = array_ctor->expr;
471 array_ctor = gfc_constructor_next (array_ctor);
473 /* A constant MASK equals .TRUE. here and can be ignored. */
477 mask_ctor = gfc_constructor_next (mask_ctor);
478 if (!m->value.logical)
482 result = op (result, gfc_copy_expr (a));
488 /* Transforms an ARRAY with operation OP, according to MASK, to an
489 array RESULT. E.g. called if
491 REAL, PARAMETER :: array(n, m) = ...
492 REAL, PARAMETER :: s(n) = PROD(array, DIM=1)
494 where OP == gfc_multiply(). The result might be post processed using post_op. */
497 simplify_transformation_to_array (gfc_expr *result, gfc_expr *array, gfc_expr *dim,
498 gfc_expr *mask, transformational_op op,
499 transformational_op post_op)
502 int done, i, n, arraysize, resultsize, dim_index, dim_extent, dim_stride;
503 gfc_expr **arrayvec, **resultvec, **base, **src, **dest;
504 gfc_constructor *array_ctor, *mask_ctor, *result_ctor;
506 int count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS],
507 sstride[GFC_MAX_DIMENSIONS], dstride[GFC_MAX_DIMENSIONS],
508 tmpstride[GFC_MAX_DIMENSIONS];
510 /* Shortcut for constant .FALSE. MASK. */
512 && mask->expr_type == EXPR_CONSTANT
513 && !mask->value.logical)
516 /* Build an indexed table for array element expressions to minimize
517 linked-list traversal. Masked elements are set to NULL. */
518 gfc_array_size (array, &size);
519 arraysize = mpz_get_ui (size);
522 arrayvec = XCNEWVEC (gfc_expr*, arraysize);
524 array_ctor = gfc_constructor_first (array->value.constructor);
526 if (mask && mask->expr_type == EXPR_ARRAY)
527 mask_ctor = gfc_constructor_first (mask->value.constructor);
529 for (i = 0; i < arraysize; ++i)
531 arrayvec[i] = array_ctor->expr;
532 array_ctor = gfc_constructor_next (array_ctor);
536 if (!mask_ctor->expr->value.logical)
539 mask_ctor = gfc_constructor_next (mask_ctor);
543 /* Same for the result expression. */
544 gfc_array_size (result, &size);
545 resultsize = mpz_get_ui (size);
548 resultvec = XCNEWVEC (gfc_expr*, resultsize);
549 result_ctor = gfc_constructor_first (result->value.constructor);
550 for (i = 0; i < resultsize; ++i)
552 resultvec[i] = result_ctor->expr;
553 result_ctor = gfc_constructor_next (result_ctor);
556 gfc_extract_int (dim, &dim_index);
557 dim_index -= 1; /* zero-base index */
561 for (i = 0, n = 0; i < array->rank; ++i)
564 tmpstride[i] = (i == 0) ? 1 : tmpstride[i-1] * mpz_get_si (array->shape[i-1]);
567 dim_extent = mpz_get_si (array->shape[i]);
568 dim_stride = tmpstride[i];
572 extent[n] = mpz_get_si (array->shape[i]);
573 sstride[n] = tmpstride[i];
574 dstride[n] = (n == 0) ? 1 : dstride[n-1] * extent[n-1];
583 for (src = base, n = 0; n < dim_extent; src += dim_stride, ++n)
585 *dest = op (*dest, gfc_copy_expr (*src));
592 while (!done && count[n] == extent[n])
595 base -= sstride[n] * extent[n];
596 dest -= dstride[n] * extent[n];
599 if (n < result->rank)
610 /* Place updated expression in result constructor. */
611 result_ctor = gfc_constructor_first (result->value.constructor);
612 for (i = 0; i < resultsize; ++i)
615 result_ctor->expr = post_op (result_ctor->expr, resultvec[i]);
617 result_ctor->expr = resultvec[i];
618 result_ctor = gfc_constructor_next (result_ctor);
628 simplify_transformation (gfc_expr *array, gfc_expr *dim, gfc_expr *mask,
629 int init_val, transformational_op op)
633 if (!is_constant_array_expr (array)
634 || !gfc_is_constant_expr (dim))
638 && !is_constant_array_expr (mask)
639 && mask->expr_type != EXPR_CONSTANT)
642 result = transformational_result (array, dim, array->ts.type,
643 array->ts.kind, &array->where);
644 init_result_expr (result, init_val, NULL);
646 return !dim || array->rank == 1 ?
647 simplify_transformation_to_scalar (result, array, mask, op) :
648 simplify_transformation_to_array (result, array, dim, mask, op, NULL);
652 /********************** Simplification functions *****************************/
655 gfc_simplify_abs (gfc_expr *e)
659 if (e->expr_type != EXPR_CONSTANT)
665 result = gfc_get_constant_expr (BT_INTEGER, e->ts.kind, &e->where);
666 mpz_abs (result->value.integer, e->value.integer);
667 return range_check (result, "IABS");
670 result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
671 mpfr_abs (result->value.real, e->value.real, GFC_RND_MODE);
672 return range_check (result, "ABS");
675 gfc_set_model_kind (e->ts.kind);
676 result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
677 mpc_abs (result->value.real, e->value.complex, GFC_RND_MODE);
678 return range_check (result, "CABS");
681 gfc_internal_error ("gfc_simplify_abs(): Bad type");
687 simplify_achar_char (gfc_expr *e, gfc_expr *k, const char *name, bool ascii)
691 bool too_large = false;
693 if (e->expr_type != EXPR_CONSTANT)
696 kind = get_kind (BT_CHARACTER, k, name, gfc_default_character_kind);
698 return &gfc_bad_expr;
700 if (mpz_cmp_si (e->value.integer, 0) < 0)
702 gfc_error ("Argument of %s function at %L is negative", name,
704 return &gfc_bad_expr;
707 if (ascii && gfc_option.warn_surprising
708 && mpz_cmp_si (e->value.integer, 127) > 0)
709 gfc_warning ("Argument of %s function at %L outside of range [0,127]",
712 if (kind == 1 && mpz_cmp_si (e->value.integer, 255) > 0)
717 mpz_init_set_ui (t, 2);
718 mpz_pow_ui (t, t, 32);
719 mpz_sub_ui (t, t, 1);
720 if (mpz_cmp (e->value.integer, t) > 0)
727 gfc_error ("Argument of %s function at %L is too large for the "
728 "collating sequence of kind %d", name, &e->where, kind);
729 return &gfc_bad_expr;
732 result = gfc_get_character_expr (kind, &e->where, NULL, 1);
733 result->value.character.string[0] = mpz_get_ui (e->value.integer);
740 /* We use the processor's collating sequence, because all
741 systems that gfortran currently works on are ASCII. */
744 gfc_simplify_achar (gfc_expr *e, gfc_expr *k)
746 return simplify_achar_char (e, k, "ACHAR", true);
751 gfc_simplify_acos (gfc_expr *x)
755 if (x->expr_type != EXPR_CONSTANT)
761 if (mpfr_cmp_si (x->value.real, 1) > 0
762 || mpfr_cmp_si (x->value.real, -1) < 0)
764 gfc_error ("Argument of ACOS at %L must be between -1 and 1",
766 return &gfc_bad_expr;
768 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
769 mpfr_acos (result->value.real, x->value.real, GFC_RND_MODE);
773 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
774 mpc_acos (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
778 gfc_internal_error ("in gfc_simplify_acos(): Bad type");
781 return range_check (result, "ACOS");
785 gfc_simplify_acosh (gfc_expr *x)
789 if (x->expr_type != EXPR_CONSTANT)
795 if (mpfr_cmp_si (x->value.real, 1) < 0)
797 gfc_error ("Argument of ACOSH at %L must not be less than 1",
799 return &gfc_bad_expr;
802 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
803 mpfr_acosh (result->value.real, x->value.real, GFC_RND_MODE);
807 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
808 mpc_acosh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
812 gfc_internal_error ("in gfc_simplify_acosh(): Bad type");
815 return range_check (result, "ACOSH");
819 gfc_simplify_adjustl (gfc_expr *e)
825 if (e->expr_type != EXPR_CONSTANT)
828 len = e->value.character.length;
830 for (count = 0, i = 0; i < len; ++i)
832 ch = e->value.character.string[i];
838 result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, len);
839 for (i = 0; i < len - count; ++i)
840 result->value.character.string[i] = e->value.character.string[count + i];
847 gfc_simplify_adjustr (gfc_expr *e)
853 if (e->expr_type != EXPR_CONSTANT)
856 len = e->value.character.length;
858 for (count = 0, i = len - 1; i >= 0; --i)
860 ch = e->value.character.string[i];
866 result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, len);
867 for (i = 0; i < count; ++i)
868 result->value.character.string[i] = ' ';
870 for (i = count; i < len; ++i)
871 result->value.character.string[i] = e->value.character.string[i - count];
878 gfc_simplify_aimag (gfc_expr *e)
882 if (e->expr_type != EXPR_CONSTANT)
885 result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
886 mpfr_set (result->value.real, mpc_imagref (e->value.complex), GFC_RND_MODE);
888 return range_check (result, "AIMAG");
893 gfc_simplify_aint (gfc_expr *e, gfc_expr *k)
895 gfc_expr *rtrunc, *result;
898 kind = get_kind (BT_REAL, k, "AINT", e->ts.kind);
900 return &gfc_bad_expr;
902 if (e->expr_type != EXPR_CONSTANT)
905 rtrunc = gfc_copy_expr (e);
906 mpfr_trunc (rtrunc->value.real, e->value.real);
908 result = gfc_real2real (rtrunc, kind);
910 gfc_free_expr (rtrunc);
912 return range_check (result, "AINT");
917 gfc_simplify_all (gfc_expr *mask, gfc_expr *dim)
919 return simplify_transformation (mask, dim, NULL, true, gfc_and);
924 gfc_simplify_dint (gfc_expr *e)
926 gfc_expr *rtrunc, *result;
928 if (e->expr_type != EXPR_CONSTANT)
931 rtrunc = gfc_copy_expr (e);
932 mpfr_trunc (rtrunc->value.real, e->value.real);
934 result = gfc_real2real (rtrunc, gfc_default_double_kind);
936 gfc_free_expr (rtrunc);
938 return range_check (result, "DINT");
943 gfc_simplify_dreal (gfc_expr *e)
945 gfc_expr *result = NULL;
947 if (e->expr_type != EXPR_CONSTANT)
950 result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
951 mpc_real (result->value.real, e->value.complex, GFC_RND_MODE);
953 return range_check (result, "DREAL");
958 gfc_simplify_anint (gfc_expr *e, gfc_expr *k)
963 kind = get_kind (BT_REAL, k, "ANINT", e->ts.kind);
965 return &gfc_bad_expr;
967 if (e->expr_type != EXPR_CONSTANT)
970 result = gfc_get_constant_expr (e->ts.type, kind, &e->where);
971 mpfr_round (result->value.real, e->value.real);
973 return range_check (result, "ANINT");
978 gfc_simplify_and (gfc_expr *x, gfc_expr *y)
983 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
986 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
991 result = gfc_get_constant_expr (BT_INTEGER, kind, &x->where);
992 mpz_and (result->value.integer, x->value.integer, y->value.integer);
993 return range_check (result, "AND");
996 return gfc_get_logical_expr (kind, &x->where,
997 x->value.logical && y->value.logical);
1006 gfc_simplify_any (gfc_expr *mask, gfc_expr *dim)
1008 return simplify_transformation (mask, dim, NULL, false, gfc_or);
1013 gfc_simplify_dnint (gfc_expr *e)
1017 if (e->expr_type != EXPR_CONSTANT)
1020 result = gfc_get_constant_expr (BT_REAL, gfc_default_double_kind, &e->where);
1021 mpfr_round (result->value.real, e->value.real);
1023 return range_check (result, "DNINT");
1028 gfc_simplify_asin (gfc_expr *x)
1032 if (x->expr_type != EXPR_CONSTANT)
1038 if (mpfr_cmp_si (x->value.real, 1) > 0
1039 || mpfr_cmp_si (x->value.real, -1) < 0)
1041 gfc_error ("Argument of ASIN at %L must be between -1 and 1",
1043 return &gfc_bad_expr;
1045 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1046 mpfr_asin (result->value.real, x->value.real, GFC_RND_MODE);
1050 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1051 mpc_asin (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1055 gfc_internal_error ("in gfc_simplify_asin(): Bad type");
1058 return range_check (result, "ASIN");
1063 gfc_simplify_asinh (gfc_expr *x)
1067 if (x->expr_type != EXPR_CONSTANT)
1070 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1075 mpfr_asinh (result->value.real, x->value.real, GFC_RND_MODE);
1079 mpc_asinh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1083 gfc_internal_error ("in gfc_simplify_asinh(): Bad type");
1086 return range_check (result, "ASINH");
1091 gfc_simplify_atan (gfc_expr *x)
1095 if (x->expr_type != EXPR_CONSTANT)
1098 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1103 mpfr_atan (result->value.real, x->value.real, GFC_RND_MODE);
1107 mpc_atan (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1111 gfc_internal_error ("in gfc_simplify_atan(): Bad type");
1114 return range_check (result, "ATAN");
1119 gfc_simplify_atanh (gfc_expr *x)
1123 if (x->expr_type != EXPR_CONSTANT)
1129 if (mpfr_cmp_si (x->value.real, 1) >= 0
1130 || mpfr_cmp_si (x->value.real, -1) <= 0)
1132 gfc_error ("Argument of ATANH at %L must be inside the range -1 "
1134 return &gfc_bad_expr;
1136 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1137 mpfr_atanh (result->value.real, x->value.real, GFC_RND_MODE);
1141 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1142 mpc_atanh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1146 gfc_internal_error ("in gfc_simplify_atanh(): Bad type");
1149 return range_check (result, "ATANH");
1154 gfc_simplify_atan2 (gfc_expr *y, gfc_expr *x)
1158 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1161 if (mpfr_sgn (y->value.real) == 0 && mpfr_sgn (x->value.real) == 0)
1163 gfc_error ("If first argument of ATAN2 %L is zero, then the "
1164 "second argument must not be zero", &x->where);
1165 return &gfc_bad_expr;
1168 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1169 mpfr_atan2 (result->value.real, y->value.real, x->value.real, GFC_RND_MODE);
1171 return range_check (result, "ATAN2");
1176 gfc_simplify_bessel_j0 (gfc_expr *x)
1180 if (x->expr_type != EXPR_CONSTANT)
1183 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1184 mpfr_j0 (result->value.real, x->value.real, GFC_RND_MODE);
1186 return range_check (result, "BESSEL_J0");
1191 gfc_simplify_bessel_j1 (gfc_expr *x)
1195 if (x->expr_type != EXPR_CONSTANT)
1198 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1199 mpfr_j1 (result->value.real, x->value.real, GFC_RND_MODE);
1201 return range_check (result, "BESSEL_J1");
1206 gfc_simplify_bessel_jn (gfc_expr *order, gfc_expr *x)
1211 if (x->expr_type != EXPR_CONSTANT || order->expr_type != EXPR_CONSTANT)
1214 n = mpz_get_si (order->value.integer);
1215 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1216 mpfr_jn (result->value.real, n, x->value.real, GFC_RND_MODE);
1218 return range_check (result, "BESSEL_JN");
1222 /* Simplify transformational form of JN and YN. */
1225 gfc_simplify_bessel_n2 (gfc_expr *order1, gfc_expr *order2, gfc_expr *x,
1232 mpfr_t x2rev, last1, last2;
1234 if (x->expr_type != EXPR_CONSTANT || order1->expr_type != EXPR_CONSTANT
1235 || order2->expr_type != EXPR_CONSTANT)
1238 n1 = mpz_get_si (order1->value.integer);
1239 n2 = mpz_get_si (order2->value.integer);
1240 result = gfc_get_array_expr (x->ts.type, x->ts.kind, &x->where);
1242 result->shape = gfc_get_shape (1);
1243 mpz_init_set_ui (result->shape[0], MAX (n2-n1+1, 0));
1248 /* Special case: x == 0; it is J0(0.0) == 1, JN(N > 0, 0.0) == 0; and
1249 YN(N, 0.0) = -Inf. */
1251 if (mpfr_cmp_ui (x->value.real, 0.0) == 0)
1253 if (!jn && gfc_option.flag_range_check)
1255 gfc_error ("Result of BESSEL_YN is -INF at %L", &result->where);
1256 gfc_free_expr (result);
1257 return &gfc_bad_expr;
1262 e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1263 mpfr_set_ui (e->value.real, 1, GFC_RND_MODE);
1264 gfc_constructor_append_expr (&result->value.constructor, e,
1269 for (i = n1; i <= n2; i++)
1271 e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1273 mpfr_set_ui (e->value.real, 0, GFC_RND_MODE);
1275 mpfr_set_inf (e->value.real, -1);
1276 gfc_constructor_append_expr (&result->value.constructor, e,
1283 /* Use the faster but more verbose recurrence algorithm. Bessel functions
1284 are stable for downward recursion and Neumann functions are stable
1285 for upward recursion. It is
1287 J(N-1, x) = x2rev * N * J(N, x) - J(N+1, x),
1288 Y(N+1, x) = x2rev * N * Y(N, x) - Y(N-1, x).
1289 Cf. http://dlmf.nist.gov/10.74#iv and http://dlmf.nist.gov/10.6#E1 */
1291 gfc_set_model_kind (x->ts.kind);
1293 /* Get first recursion anchor. */
1297 mpfr_jn (last1, n2, x->value.real, GFC_RND_MODE);
1299 mpfr_yn (last1, n1, x->value.real, GFC_RND_MODE);
1301 e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1302 mpfr_set (e->value.real, last1, GFC_RND_MODE);
1303 if (range_check (e, jn ? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr)
1307 gfc_free_expr (result);
1308 return &gfc_bad_expr;
1310 gfc_constructor_append_expr (&result->value.constructor, e, &x->where);
1318 /* Get second recursion anchor. */
1322 mpfr_jn (last2, n2-1, x->value.real, GFC_RND_MODE);
1324 mpfr_yn (last2, n1+1, x->value.real, GFC_RND_MODE);
1326 e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1327 mpfr_set (e->value.real, last2, GFC_RND_MODE);
1328 if (range_check (e, jn ? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr)
1333 gfc_free_expr (result);
1334 return &gfc_bad_expr;
1337 gfc_constructor_insert_expr (&result->value.constructor, e, &x->where, -2);
1339 gfc_constructor_append_expr (&result->value.constructor, e, &x->where);
1348 /* Start actual recursion. */
1351 mpfr_ui_div (x2rev, 2, x->value.real, GFC_RND_MODE);
1353 for (i = 2; i <= n2-n1; i++)
1355 e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1357 /* Special case: For YN, if the previous N gave -INF, set
1358 also N+1 to -INF. */
1359 if (!jn && !gfc_option.flag_range_check && mpfr_inf_p (last2))
1361 mpfr_set_inf (e->value.real, -1);
1362 gfc_constructor_append_expr (&result->value.constructor, e,
1367 mpfr_mul_si (e->value.real, x2rev, jn ? (n2-i+1) : (n1+i-1),
1369 mpfr_mul (e->value.real, e->value.real, last2, GFC_RND_MODE);
1370 mpfr_sub (e->value.real, e->value.real, last1, GFC_RND_MODE);
1372 if (range_check (e, jn ? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr)
1374 /* Range_check frees "e" in that case. */
1380 gfc_constructor_insert_expr (&result->value.constructor, e, &x->where,
1383 gfc_constructor_append_expr (&result->value.constructor, e, &x->where);
1385 mpfr_set (last1, last2, GFC_RND_MODE);
1386 mpfr_set (last2, e->value.real, GFC_RND_MODE);
1399 gfc_free_expr (result);
1400 return &gfc_bad_expr;
1405 gfc_simplify_bessel_jn2 (gfc_expr *order1, gfc_expr *order2, gfc_expr *x)
1407 return gfc_simplify_bessel_n2 (order1, order2, x, true);
1412 gfc_simplify_bessel_y0 (gfc_expr *x)
1416 if (x->expr_type != EXPR_CONSTANT)
1419 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1420 mpfr_y0 (result->value.real, x->value.real, GFC_RND_MODE);
1422 return range_check (result, "BESSEL_Y0");
1427 gfc_simplify_bessel_y1 (gfc_expr *x)
1431 if (x->expr_type != EXPR_CONSTANT)
1434 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1435 mpfr_y1 (result->value.real, x->value.real, GFC_RND_MODE);
1437 return range_check (result, "BESSEL_Y1");
1442 gfc_simplify_bessel_yn (gfc_expr *order, gfc_expr *x)
1447 if (x->expr_type != EXPR_CONSTANT || order->expr_type != EXPR_CONSTANT)
1450 n = mpz_get_si (order->value.integer);
1451 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1452 mpfr_yn (result->value.real, n, x->value.real, GFC_RND_MODE);
1454 return range_check (result, "BESSEL_YN");
1459 gfc_simplify_bessel_yn2 (gfc_expr *order1, gfc_expr *order2, gfc_expr *x)
1461 return gfc_simplify_bessel_n2 (order1, order2, x, false);
1466 gfc_simplify_bit_size (gfc_expr *e)
1468 int i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
1469 return gfc_get_int_expr (e->ts.kind, &e->where,
1470 gfc_integer_kinds[i].bit_size);
1475 gfc_simplify_btest (gfc_expr *e, gfc_expr *bit)
1479 if (e->expr_type != EXPR_CONSTANT || bit->expr_type != EXPR_CONSTANT)
1482 if (gfc_extract_int (bit, &b) != NULL || b < 0)
1483 return gfc_get_logical_expr (gfc_default_logical_kind, &e->where, false);
1485 return gfc_get_logical_expr (gfc_default_logical_kind, &e->where,
1486 mpz_tstbit (e->value.integer, b));
1491 compare_bitwise (gfc_expr *i, gfc_expr *j)
1496 gcc_assert (i->ts.type == BT_INTEGER);
1497 gcc_assert (j->ts.type == BT_INTEGER);
1499 mpz_init_set (x, i->value.integer);
1500 k = gfc_validate_kind (i->ts.type, i->ts.kind, false);
1501 convert_mpz_to_unsigned (x, gfc_integer_kinds[k].bit_size);
1503 mpz_init_set (y, j->value.integer);
1504 k = gfc_validate_kind (j->ts.type, j->ts.kind, false);
1505 convert_mpz_to_unsigned (y, gfc_integer_kinds[k].bit_size);
1507 res = mpz_cmp (x, y);
1515 gfc_simplify_bge (gfc_expr *i, gfc_expr *j)
1517 if (i->expr_type != EXPR_CONSTANT || j->expr_type != EXPR_CONSTANT)
1520 return gfc_get_logical_expr (gfc_default_logical_kind, &i->where,
1521 compare_bitwise (i, j) >= 0);
1526 gfc_simplify_bgt (gfc_expr *i, gfc_expr *j)
1528 if (i->expr_type != EXPR_CONSTANT || j->expr_type != EXPR_CONSTANT)
1531 return gfc_get_logical_expr (gfc_default_logical_kind, &i->where,
1532 compare_bitwise (i, j) > 0);
1537 gfc_simplify_ble (gfc_expr *i, gfc_expr *j)
1539 if (i->expr_type != EXPR_CONSTANT || j->expr_type != EXPR_CONSTANT)
1542 return gfc_get_logical_expr (gfc_default_logical_kind, &i->where,
1543 compare_bitwise (i, j) <= 0);
1548 gfc_simplify_blt (gfc_expr *i, gfc_expr *j)
1550 if (i->expr_type != EXPR_CONSTANT || j->expr_type != EXPR_CONSTANT)
1553 return gfc_get_logical_expr (gfc_default_logical_kind, &i->where,
1554 compare_bitwise (i, j) < 0);
1559 gfc_simplify_ceiling (gfc_expr *e, gfc_expr *k)
1561 gfc_expr *ceil, *result;
1564 kind = get_kind (BT_INTEGER, k, "CEILING", gfc_default_integer_kind);
1566 return &gfc_bad_expr;
1568 if (e->expr_type != EXPR_CONSTANT)
1571 ceil = gfc_copy_expr (e);
1572 mpfr_ceil (ceil->value.real, e->value.real);
1574 result = gfc_get_constant_expr (BT_INTEGER, kind, &e->where);
1575 gfc_mpfr_to_mpz (result->value.integer, ceil->value.real, &e->where);
1577 gfc_free_expr (ceil);
1579 return range_check (result, "CEILING");
1584 gfc_simplify_char (gfc_expr *e, gfc_expr *k)
1586 return simplify_achar_char (e, k, "CHAR", false);
1590 /* Common subroutine for simplifying CMPLX, COMPLEX and DCMPLX. */
1593 simplify_cmplx (const char *name, gfc_expr *x, gfc_expr *y, int kind)
1597 if (convert_boz (x, kind) == &gfc_bad_expr)
1598 return &gfc_bad_expr;
1600 if (convert_boz (y, kind) == &gfc_bad_expr)
1601 return &gfc_bad_expr;
1603 if (x->expr_type != EXPR_CONSTANT
1604 || (y != NULL && y->expr_type != EXPR_CONSTANT))
1607 result = gfc_get_constant_expr (BT_COMPLEX, kind, &x->where);
1612 mpc_set_z (result->value.complex, x->value.integer, GFC_MPC_RND_MODE);
1616 mpc_set_fr (result->value.complex, x->value.real, GFC_RND_MODE);
1620 mpc_set (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1624 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (x)");
1628 return range_check (result, name);
1633 mpfr_set_z (mpc_imagref (result->value.complex),
1634 y->value.integer, GFC_RND_MODE);
1638 mpfr_set (mpc_imagref (result->value.complex),
1639 y->value.real, GFC_RND_MODE);
1643 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (y)");
1646 return range_check (result, name);
1651 gfc_simplify_cmplx (gfc_expr *x, gfc_expr *y, gfc_expr *k)
1655 kind = get_kind (BT_REAL, k, "CMPLX", gfc_default_complex_kind);
1657 return &gfc_bad_expr;
1659 return simplify_cmplx ("CMPLX", x, y, kind);
1664 gfc_simplify_complex (gfc_expr *x, gfc_expr *y)
1668 if (x->ts.type == BT_INTEGER && y->ts.type == BT_INTEGER)
1669 kind = gfc_default_complex_kind;
1670 else if (x->ts.type == BT_REAL || y->ts.type == BT_INTEGER)
1672 else if (x->ts.type == BT_INTEGER || y->ts.type == BT_REAL)
1674 else if (x->ts.type == BT_REAL && y->ts.type == BT_REAL)
1675 kind = (x->ts.kind > y->ts.kind) ? x->ts.kind : y->ts.kind;
1679 return simplify_cmplx ("COMPLEX", x, y, kind);
1684 gfc_simplify_conjg (gfc_expr *e)
1688 if (e->expr_type != EXPR_CONSTANT)
1691 result = gfc_copy_expr (e);
1692 mpc_conj (result->value.complex, result->value.complex, GFC_MPC_RND_MODE);
1694 return range_check (result, "CONJG");
1699 gfc_simplify_cos (gfc_expr *x)
1703 if (x->expr_type != EXPR_CONSTANT)
1706 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1711 mpfr_cos (result->value.real, x->value.real, GFC_RND_MODE);
1715 gfc_set_model_kind (x->ts.kind);
1716 mpc_cos (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1720 gfc_internal_error ("in gfc_simplify_cos(): Bad type");
1723 return range_check (result, "COS");
1728 gfc_simplify_cosh (gfc_expr *x)
1732 if (x->expr_type != EXPR_CONSTANT)
1735 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1740 mpfr_cosh (result->value.real, x->value.real, GFC_RND_MODE);
1744 mpc_cosh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1751 return range_check (result, "COSH");
1756 gfc_simplify_count (gfc_expr *mask, gfc_expr *dim, gfc_expr *kind)
1760 if (!is_constant_array_expr (mask)
1761 || !gfc_is_constant_expr (dim)
1762 || !gfc_is_constant_expr (kind))
1765 result = transformational_result (mask, dim,
1767 get_kind (BT_INTEGER, kind, "COUNT",
1768 gfc_default_integer_kind),
1771 init_result_expr (result, 0, NULL);
1773 /* Passing MASK twice, once as data array, once as mask.
1774 Whenever gfc_count is called, '1' is added to the result. */
1775 return !dim || mask->rank == 1 ?
1776 simplify_transformation_to_scalar (result, mask, mask, gfc_count) :
1777 simplify_transformation_to_array (result, mask, dim, mask, gfc_count, NULL);
1782 gfc_simplify_dcmplx (gfc_expr *x, gfc_expr *y)
1784 return simplify_cmplx ("DCMPLX", x, y, gfc_default_double_kind);
1789 gfc_simplify_dble (gfc_expr *e)
1791 gfc_expr *result = NULL;
1793 if (e->expr_type != EXPR_CONSTANT)
1796 if (convert_boz (e, gfc_default_double_kind) == &gfc_bad_expr)
1797 return &gfc_bad_expr;
1799 result = gfc_convert_constant (e, BT_REAL, gfc_default_double_kind);
1800 if (result == &gfc_bad_expr)
1801 return &gfc_bad_expr;
1803 return range_check (result, "DBLE");
1808 gfc_simplify_digits (gfc_expr *x)
1812 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
1817 digits = gfc_integer_kinds[i].digits;
1822 digits = gfc_real_kinds[i].digits;
1829 return gfc_get_int_expr (gfc_default_integer_kind, NULL, digits);
1834 gfc_simplify_dim (gfc_expr *x, gfc_expr *y)
1839 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1842 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
1843 result = gfc_get_constant_expr (x->ts.type, kind, &x->where);
1848 if (mpz_cmp (x->value.integer, y->value.integer) > 0)
1849 mpz_sub (result->value.integer, x->value.integer, y->value.integer);
1851 mpz_set_ui (result->value.integer, 0);
1856 if (mpfr_cmp (x->value.real, y->value.real) > 0)
1857 mpfr_sub (result->value.real, x->value.real, y->value.real,
1860 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
1865 gfc_internal_error ("gfc_simplify_dim(): Bad type");
1868 return range_check (result, "DIM");
1873 gfc_simplify_dot_product (gfc_expr *vector_a, gfc_expr *vector_b)
1875 if (!is_constant_array_expr (vector_a)
1876 || !is_constant_array_expr (vector_b))
1879 gcc_assert (vector_a->rank == 1);
1880 gcc_assert (vector_b->rank == 1);
1881 gcc_assert (gfc_compare_types (&vector_a->ts, &vector_b->ts));
1883 return compute_dot_product (vector_a, 1, 0, vector_b, 1, 0);
1888 gfc_simplify_dprod (gfc_expr *x, gfc_expr *y)
1890 gfc_expr *a1, *a2, *result;
1892 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1895 a1 = gfc_real2real (x, gfc_default_double_kind);
1896 a2 = gfc_real2real (y, gfc_default_double_kind);
1898 result = gfc_get_constant_expr (BT_REAL, gfc_default_double_kind, &x->where);
1899 mpfr_mul (result->value.real, a1->value.real, a2->value.real, GFC_RND_MODE);
1904 return range_check (result, "DPROD");
1909 simplify_dshift (gfc_expr *arg1, gfc_expr *arg2, gfc_expr *shiftarg,
1913 int i, k, size, shift;
1915 if (arg1->expr_type != EXPR_CONSTANT || arg2->expr_type != EXPR_CONSTANT
1916 || shiftarg->expr_type != EXPR_CONSTANT)
1919 k = gfc_validate_kind (BT_INTEGER, arg1->ts.kind, false);
1920 size = gfc_integer_kinds[k].bit_size;
1922 gfc_extract_int (shiftarg, &shift);
1924 /* DSHIFTR(I,J,SHIFT) = DSHIFTL(I,J,SIZE-SHIFT). */
1926 shift = size - shift;
1928 result = gfc_get_constant_expr (BT_INTEGER, arg1->ts.kind, &arg1->where);
1929 mpz_set_ui (result->value.integer, 0);
1931 for (i = 0; i < shift; i++)
1932 if (mpz_tstbit (arg2->value.integer, size - shift + i))
1933 mpz_setbit (result->value.integer, i);
1935 for (i = 0; i < size - shift; i++)
1936 if (mpz_tstbit (arg1->value.integer, i))
1937 mpz_setbit (result->value.integer, shift + i);
1939 /* Convert to a signed value. */
1940 convert_mpz_to_signed (result->value.integer, size);
1947 gfc_simplify_dshiftr (gfc_expr *arg1, gfc_expr *arg2, gfc_expr *shiftarg)
1949 return simplify_dshift (arg1, arg2, shiftarg, true);
1954 gfc_simplify_dshiftl (gfc_expr *arg1, gfc_expr *arg2, gfc_expr *shiftarg)
1956 return simplify_dshift (arg1, arg2, shiftarg, false);
1961 gfc_simplify_erf (gfc_expr *x)
1965 if (x->expr_type != EXPR_CONSTANT)
1968 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1969 mpfr_erf (result->value.real, x->value.real, GFC_RND_MODE);
1971 return range_check (result, "ERF");
1976 gfc_simplify_erfc (gfc_expr *x)
1980 if (x->expr_type != EXPR_CONSTANT)
1983 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1984 mpfr_erfc (result->value.real, x->value.real, GFC_RND_MODE);
1986 return range_check (result, "ERFC");
1990 /* Helper functions to simplify ERFC_SCALED(x) = ERFC(x) * EXP(X**2). */
1992 #define MAX_ITER 200
1993 #define ARG_LIMIT 12
1995 /* Calculate ERFC_SCALED directly by its definition:
1997 ERFC_SCALED(x) = ERFC(x) * EXP(X**2)
1999 using a large precision for intermediate results. This is used for all
2000 but large values of the argument. */
2002 fullprec_erfc_scaled (mpfr_t res, mpfr_t arg)
2007 prec = mpfr_get_default_prec ();
2008 mpfr_set_default_prec (10 * prec);
2013 mpfr_set (a, arg, GFC_RND_MODE);
2014 mpfr_sqr (b, a, GFC_RND_MODE);
2015 mpfr_exp (b, b, GFC_RND_MODE);
2016 mpfr_erfc (a, a, GFC_RND_MODE);
2017 mpfr_mul (a, a, b, GFC_RND_MODE);
2019 mpfr_set (res, a, GFC_RND_MODE);
2020 mpfr_set_default_prec (prec);
2026 /* Calculate ERFC_SCALED using a power series expansion in 1/arg:
2028 ERFC_SCALED(x) = 1 / (x * sqrt(pi))
2029 * (1 + Sum_n (-1)**n * (1 * 3 * 5 * ... * (2n-1))
2032 This is used for large values of the argument. Intermediate calculations
2033 are performed with twice the precision. We don't do a fixed number of
2034 iterations of the sum, but stop when it has converged to the required
2037 asympt_erfc_scaled (mpfr_t res, mpfr_t arg)
2039 mpfr_t sum, x, u, v, w, oldsum, sumtrunc;
2044 prec = mpfr_get_default_prec ();
2045 mpfr_set_default_prec (2 * prec);
2055 mpfr_init (sumtrunc);
2056 mpfr_set_prec (oldsum, prec);
2057 mpfr_set_prec (sumtrunc, prec);
2059 mpfr_set (x, arg, GFC_RND_MODE);
2060 mpfr_set_ui (sum, 1, GFC_RND_MODE);
2061 mpz_set_ui (num, 1);
2063 mpfr_set (u, x, GFC_RND_MODE);
2064 mpfr_sqr (u, u, GFC_RND_MODE);
2065 mpfr_mul_ui (u, u, 2, GFC_RND_MODE);
2066 mpfr_pow_si (u, u, -1, GFC_RND_MODE);
2068 for (i = 1; i < MAX_ITER; i++)
2070 mpfr_set (oldsum, sum, GFC_RND_MODE);
2072 mpz_mul_ui (num, num, 2 * i - 1);
2075 mpfr_set (w, u, GFC_RND_MODE);
2076 mpfr_pow_ui (w, w, i, GFC_RND_MODE);
2078 mpfr_set_z (v, num, GFC_RND_MODE);
2079 mpfr_mul (v, v, w, GFC_RND_MODE);
2081 mpfr_add (sum, sum, v, GFC_RND_MODE);
2083 mpfr_set (sumtrunc, sum, GFC_RND_MODE);
2084 if (mpfr_cmp (sumtrunc, oldsum) == 0)
2088 /* We should have converged by now; otherwise, ARG_LIMIT is probably
2090 gcc_assert (i < MAX_ITER);
2092 /* Divide by x * sqrt(Pi). */
2093 mpfr_const_pi (u, GFC_RND_MODE);
2094 mpfr_sqrt (u, u, GFC_RND_MODE);
2095 mpfr_mul (u, u, x, GFC_RND_MODE);
2096 mpfr_div (sum, sum, u, GFC_RND_MODE);
2098 mpfr_set (res, sum, GFC_RND_MODE);
2099 mpfr_set_default_prec (prec);
2101 mpfr_clears (sum, x, u, v, w, oldsum, sumtrunc, NULL);
2107 gfc_simplify_erfc_scaled (gfc_expr *x)
2111 if (x->expr_type != EXPR_CONSTANT)
2114 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2115 if (mpfr_cmp_d (x->value.real, ARG_LIMIT) >= 0)
2116 asympt_erfc_scaled (result->value.real, x->value.real);
2118 fullprec_erfc_scaled (result->value.real, x->value.real);
2120 return range_check (result, "ERFC_SCALED");
2128 gfc_simplify_epsilon (gfc_expr *e)
2133 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2135 result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
2136 mpfr_set (result->value.real, gfc_real_kinds[i].epsilon, GFC_RND_MODE);
2138 return range_check (result, "EPSILON");
2143 gfc_simplify_exp (gfc_expr *x)
2147 if (x->expr_type != EXPR_CONSTANT)
2150 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2155 mpfr_exp (result->value.real, x->value.real, GFC_RND_MODE);
2159 gfc_set_model_kind (x->ts.kind);
2160 mpc_exp (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
2164 gfc_internal_error ("in gfc_simplify_exp(): Bad type");
2167 return range_check (result, "EXP");
2172 gfc_simplify_exponent (gfc_expr *x)
2177 if (x->expr_type != EXPR_CONSTANT)
2180 result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
2183 gfc_set_model (x->value.real);
2185 if (mpfr_sgn (x->value.real) == 0)
2187 mpz_set_ui (result->value.integer, 0);
2191 i = (int) mpfr_get_exp (x->value.real);
2192 mpz_set_si (result->value.integer, i);
2194 return range_check (result, "EXPONENT");
2199 gfc_simplify_float (gfc_expr *a)
2203 if (a->expr_type != EXPR_CONSTANT)
2208 if (convert_boz (a, gfc_default_real_kind) == &gfc_bad_expr)
2209 return &gfc_bad_expr;
2211 result = gfc_copy_expr (a);
2214 result = gfc_int2real (a, gfc_default_real_kind);
2216 return range_check (result, "FLOAT");
2221 is_last_ref_vtab (gfc_expr *e)
2224 gfc_component *comp = NULL;
2226 if (e->expr_type != EXPR_VARIABLE)
2229 for (ref = e->ref; ref; ref = ref->next)
2230 if (ref->type == REF_COMPONENT)
2231 comp = ref->u.c.component;
2233 if (!e->ref || !comp)
2234 return e->symtree->n.sym->attr.vtab;
2236 if (comp->name[0] == '_' && strcmp (comp->name, "_vptr") == 0)
2244 gfc_simplify_extends_type_of (gfc_expr *a, gfc_expr *mold)
2246 /* Avoid simplification of resolved symbols. */
2247 if (is_last_ref_vtab (a) || is_last_ref_vtab (mold))
2250 if (a->ts.type == BT_DERIVED && mold->ts.type == BT_DERIVED)
2251 return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
2252 gfc_type_is_extension_of (mold->ts.u.derived,
2254 /* Return .false. if the dynamic type can never be the same. */
2255 if ((a->ts.type == BT_CLASS && mold->ts.type == BT_CLASS
2256 && !gfc_type_is_extension_of
2257 (mold->ts.u.derived->components->ts.u.derived,
2258 a->ts.u.derived->components->ts.u.derived)
2259 && !gfc_type_is_extension_of
2260 (a->ts.u.derived->components->ts.u.derived,
2261 mold->ts.u.derived->components->ts.u.derived))
2262 || (a->ts.type == BT_DERIVED && mold->ts.type == BT_CLASS
2263 && !gfc_type_is_extension_of
2265 mold->ts.u.derived->components->ts.u.derived)
2266 && !gfc_type_is_extension_of
2267 (mold->ts.u.derived->components->ts.u.derived,
2269 || (a->ts.type == BT_CLASS && mold->ts.type == BT_DERIVED
2270 && !gfc_type_is_extension_of
2271 (mold->ts.u.derived,
2272 a->ts.u.derived->components->ts.u.derived)))
2273 return gfc_get_logical_expr (gfc_default_logical_kind, &a->where, false);
2275 if (mold->ts.type == BT_DERIVED
2276 && gfc_type_is_extension_of (mold->ts.u.derived,
2277 a->ts.u.derived->components->ts.u.derived))
2278 return gfc_get_logical_expr (gfc_default_logical_kind, &a->where, true);
2285 gfc_simplify_same_type_as (gfc_expr *a, gfc_expr *b)
2287 /* Avoid simplification of resolved symbols. */
2288 if (is_last_ref_vtab (a) || is_last_ref_vtab (b))
2291 /* Return .false. if the dynamic type can never be the
2293 if ((a->ts.type == BT_CLASS || b->ts.type == BT_CLASS)
2294 && !gfc_type_compatible (&a->ts, &b->ts)
2295 && !gfc_type_compatible (&b->ts, &a->ts))
2296 return gfc_get_logical_expr (gfc_default_logical_kind, &a->where, false);
2298 if (a->ts.type != BT_DERIVED || b->ts.type != BT_DERIVED)
2301 return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
2302 gfc_compare_derived_types (a->ts.u.derived,
2308 gfc_simplify_floor (gfc_expr *e, gfc_expr *k)
2314 kind = get_kind (BT_INTEGER, k, "FLOOR", gfc_default_integer_kind);
2316 gfc_internal_error ("gfc_simplify_floor(): Bad kind");
2318 if (e->expr_type != EXPR_CONSTANT)
2321 gfc_set_model_kind (kind);
2324 mpfr_floor (floor, e->value.real);
2326 result = gfc_get_constant_expr (BT_INTEGER, kind, &e->where);
2327 gfc_mpfr_to_mpz (result->value.integer, floor, &e->where);
2331 return range_check (result, "FLOOR");
2336 gfc_simplify_fraction (gfc_expr *x)
2339 mpfr_t absv, exp, pow2;
2341 if (x->expr_type != EXPR_CONSTANT)
2344 result = gfc_get_constant_expr (BT_REAL, x->ts.kind, &x->where);
2346 if (mpfr_sgn (x->value.real) == 0)
2348 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
2352 gfc_set_model_kind (x->ts.kind);
2357 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
2358 mpfr_log2 (exp, absv, GFC_RND_MODE);
2360 mpfr_trunc (exp, exp);
2361 mpfr_add_ui (exp, exp, 1, GFC_RND_MODE);
2363 mpfr_ui_pow (pow2, 2, exp, GFC_RND_MODE);
2365 mpfr_div (result->value.real, absv, pow2, GFC_RND_MODE);
2367 mpfr_clears (exp, absv, pow2, NULL);
2369 return range_check (result, "FRACTION");
2374 gfc_simplify_gamma (gfc_expr *x)
2378 if (x->expr_type != EXPR_CONSTANT)
2381 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2382 mpfr_gamma (result->value.real, x->value.real, GFC_RND_MODE);
2384 return range_check (result, "GAMMA");
2389 gfc_simplify_huge (gfc_expr *e)
2394 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2395 result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
2400 mpz_set (result->value.integer, gfc_integer_kinds[i].huge);
2404 mpfr_set (result->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
2416 gfc_simplify_hypot (gfc_expr *x, gfc_expr *y)
2420 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2423 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2424 mpfr_hypot (result->value.real, x->value.real, y->value.real, GFC_RND_MODE);
2425 return range_check (result, "HYPOT");
2429 /* We use the processor's collating sequence, because all
2430 systems that gfortran currently works on are ASCII. */
2433 gfc_simplify_iachar (gfc_expr *e, gfc_expr *kind)
2439 if (e->expr_type != EXPR_CONSTANT)
2442 if (e->value.character.length != 1)
2444 gfc_error ("Argument of IACHAR at %L must be of length one", &e->where);
2445 return &gfc_bad_expr;
2448 index = e->value.character.string[0];
2450 if (gfc_option.warn_surprising && index > 127)
2451 gfc_warning ("Argument of IACHAR function at %L outside of range 0..127",
2454 k = get_kind (BT_INTEGER, kind, "IACHAR", gfc_default_integer_kind);
2456 return &gfc_bad_expr;
2458 result = gfc_get_int_expr (k, &e->where, index);
2460 return range_check (result, "IACHAR");
2465 do_bit_and (gfc_expr *result, gfc_expr *e)
2467 gcc_assert (e->ts.type == BT_INTEGER && e->expr_type == EXPR_CONSTANT);
2468 gcc_assert (result->ts.type == BT_INTEGER
2469 && result->expr_type == EXPR_CONSTANT);
2471 mpz_and (result->value.integer, result->value.integer, e->value.integer);
2477 gfc_simplify_iall (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
2479 return simplify_transformation (array, dim, mask, -1, do_bit_and);
2484 do_bit_ior (gfc_expr *result, gfc_expr *e)
2486 gcc_assert (e->ts.type == BT_INTEGER && e->expr_type == EXPR_CONSTANT);
2487 gcc_assert (result->ts.type == BT_INTEGER
2488 && result->expr_type == EXPR_CONSTANT);
2490 mpz_ior (result->value.integer, result->value.integer, e->value.integer);
2496 gfc_simplify_iany (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
2498 return simplify_transformation (array, dim, mask, 0, do_bit_ior);
2503 gfc_simplify_iand (gfc_expr *x, gfc_expr *y)
2507 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2510 result = gfc_get_constant_expr (BT_INTEGER, x->ts.kind, &x->where);
2511 mpz_and (result->value.integer, x->value.integer, y->value.integer);
2513 return range_check (result, "IAND");
2518 gfc_simplify_ibclr (gfc_expr *x, gfc_expr *y)
2523 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2526 gfc_extract_int (y, &pos);
2528 k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
2530 result = gfc_copy_expr (x);
2532 convert_mpz_to_unsigned (result->value.integer,
2533 gfc_integer_kinds[k].bit_size);
2535 mpz_clrbit (result->value.integer, pos);
2537 convert_mpz_to_signed (result->value.integer,
2538 gfc_integer_kinds[k].bit_size);
2545 gfc_simplify_ibits (gfc_expr *x, gfc_expr *y, gfc_expr *z)
2552 if (x->expr_type != EXPR_CONSTANT
2553 || y->expr_type != EXPR_CONSTANT
2554 || z->expr_type != EXPR_CONSTANT)
2557 gfc_extract_int (y, &pos);
2558 gfc_extract_int (z, &len);
2560 k = gfc_validate_kind (BT_INTEGER, x->ts.kind, false);
2562 bitsize = gfc_integer_kinds[k].bit_size;
2564 if (pos + len > bitsize)
2566 gfc_error ("Sum of second and third arguments of IBITS exceeds "
2567 "bit size at %L", &y->where);
2568 return &gfc_bad_expr;
2571 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2572 convert_mpz_to_unsigned (result->value.integer,
2573 gfc_integer_kinds[k].bit_size);
2575 bits = XCNEWVEC (int, bitsize);
2577 for (i = 0; i < bitsize; i++)
2580 for (i = 0; i < len; i++)
2581 bits[i] = mpz_tstbit (x->value.integer, i + pos);
2583 for (i = 0; i < bitsize; i++)
2586 mpz_clrbit (result->value.integer, i);
2587 else if (bits[i] == 1)
2588 mpz_setbit (result->value.integer, i);
2590 gfc_internal_error ("IBITS: Bad bit");
2595 convert_mpz_to_signed (result->value.integer,
2596 gfc_integer_kinds[k].bit_size);
2603 gfc_simplify_ibset (gfc_expr *x, gfc_expr *y)
2608 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2611 gfc_extract_int (y, &pos);
2613 k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
2615 result = gfc_copy_expr (x);
2617 convert_mpz_to_unsigned (result->value.integer,
2618 gfc_integer_kinds[k].bit_size);
2620 mpz_setbit (result->value.integer, pos);
2622 convert_mpz_to_signed (result->value.integer,
2623 gfc_integer_kinds[k].bit_size);
2630 gfc_simplify_ichar (gfc_expr *e, gfc_expr *kind)
2636 if (e->expr_type != EXPR_CONSTANT)
2639 if (e->value.character.length != 1)
2641 gfc_error ("Argument of ICHAR at %L must be of length one", &e->where);
2642 return &gfc_bad_expr;
2645 index = e->value.character.string[0];
2647 k = get_kind (BT_INTEGER, kind, "ICHAR", gfc_default_integer_kind);
2649 return &gfc_bad_expr;
2651 result = gfc_get_int_expr (k, &e->where, index);
2653 return range_check (result, "ICHAR");
2658 gfc_simplify_ieor (gfc_expr *x, gfc_expr *y)
2662 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2665 result = gfc_get_constant_expr (BT_INTEGER, x->ts.kind, &x->where);
2666 mpz_xor (result->value.integer, x->value.integer, y->value.integer);
2668 return range_check (result, "IEOR");
2673 gfc_simplify_index (gfc_expr *x, gfc_expr *y, gfc_expr *b, gfc_expr *kind)
2676 int back, len, lensub;
2677 int i, j, k, count, index = 0, start;
2679 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT
2680 || ( b != NULL && b->expr_type != EXPR_CONSTANT))
2683 if (b != NULL && b->value.logical != 0)
2688 k = get_kind (BT_INTEGER, kind, "INDEX", gfc_default_integer_kind);
2690 return &gfc_bad_expr;
2692 result = gfc_get_constant_expr (BT_INTEGER, k, &x->where);
2694 len = x->value.character.length;
2695 lensub = y->value.character.length;
2699 mpz_set_si (result->value.integer, 0);
2707 mpz_set_si (result->value.integer, 1);
2710 else if (lensub == 1)
2712 for (i = 0; i < len; i++)
2714 for (j = 0; j < lensub; j++)
2716 if (y->value.character.string[j]
2717 == x->value.character.string[i])
2727 for (i = 0; i < len; i++)
2729 for (j = 0; j < lensub; j++)
2731 if (y->value.character.string[j]
2732 == x->value.character.string[i])
2737 for (k = 0; k < lensub; k++)
2739 if (y->value.character.string[k]
2740 == x->value.character.string[k + start])
2744 if (count == lensub)
2759 mpz_set_si (result->value.integer, len + 1);
2762 else if (lensub == 1)
2764 for (i = 0; i < len; i++)
2766 for (j = 0; j < lensub; j++)
2768 if (y->value.character.string[j]
2769 == x->value.character.string[len - i])
2771 index = len - i + 1;
2779 for (i = 0; i < len; i++)
2781 for (j = 0; j < lensub; j++)
2783 if (y->value.character.string[j]
2784 == x->value.character.string[len - i])
2787 if (start <= len - lensub)
2790 for (k = 0; k < lensub; k++)
2791 if (y->value.character.string[k]
2792 == x->value.character.string[k + start])
2795 if (count == lensub)
2812 mpz_set_si (result->value.integer, index);
2813 return range_check (result, "INDEX");
2818 simplify_intconv (gfc_expr *e, int kind, const char *name)
2820 gfc_expr *result = NULL;
2822 if (e->expr_type != EXPR_CONSTANT)
2825 result = gfc_convert_constant (e, BT_INTEGER, kind);
2826 if (result == &gfc_bad_expr)
2827 return &gfc_bad_expr;
2829 return range_check (result, name);
2834 gfc_simplify_int (gfc_expr *e, gfc_expr *k)
2838 kind = get_kind (BT_INTEGER, k, "INT", gfc_default_integer_kind);
2840 return &gfc_bad_expr;
2842 return simplify_intconv (e, kind, "INT");
2846 gfc_simplify_int2 (gfc_expr *e)
2848 return simplify_intconv (e, 2, "INT2");
2853 gfc_simplify_int8 (gfc_expr *e)
2855 return simplify_intconv (e, 8, "INT8");
2860 gfc_simplify_long (gfc_expr *e)
2862 return simplify_intconv (e, 4, "LONG");
2867 gfc_simplify_ifix (gfc_expr *e)
2869 gfc_expr *rtrunc, *result;
2871 if (e->expr_type != EXPR_CONSTANT)
2874 rtrunc = gfc_copy_expr (e);
2875 mpfr_trunc (rtrunc->value.real, e->value.real);
2877 result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
2879 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real, &e->where);
2881 gfc_free_expr (rtrunc);
2883 return range_check (result, "IFIX");
2888 gfc_simplify_idint (gfc_expr *e)
2890 gfc_expr *rtrunc, *result;
2892 if (e->expr_type != EXPR_CONSTANT)
2895 rtrunc = gfc_copy_expr (e);
2896 mpfr_trunc (rtrunc->value.real, e->value.real);
2898 result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
2900 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real, &e->where);
2902 gfc_free_expr (rtrunc);
2904 return range_check (result, "IDINT");
2909 gfc_simplify_ior (gfc_expr *x, gfc_expr *y)
2913 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2916 result = gfc_get_constant_expr (BT_INTEGER, x->ts.kind, &x->where);
2917 mpz_ior (result->value.integer, x->value.integer, y->value.integer);
2919 return range_check (result, "IOR");
2924 do_bit_xor (gfc_expr *result, gfc_expr *e)
2926 gcc_assert (e->ts.type == BT_INTEGER && e->expr_type == EXPR_CONSTANT);
2927 gcc_assert (result->ts.type == BT_INTEGER
2928 && result->expr_type == EXPR_CONSTANT);
2930 mpz_xor (result->value.integer, result->value.integer, e->value.integer);
2936 gfc_simplify_iparity (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
2938 return simplify_transformation (array, dim, mask, 0, do_bit_xor);
2943 gfc_simplify_is_iostat_end (gfc_expr *x)
2945 if (x->expr_type != EXPR_CONSTANT)
2948 return gfc_get_logical_expr (gfc_default_logical_kind, &x->where,
2949 mpz_cmp_si (x->value.integer,
2950 LIBERROR_END) == 0);
2955 gfc_simplify_is_iostat_eor (gfc_expr *x)
2957 if (x->expr_type != EXPR_CONSTANT)
2960 return gfc_get_logical_expr (gfc_default_logical_kind, &x->where,
2961 mpz_cmp_si (x->value.integer,
2962 LIBERROR_EOR) == 0);
2967 gfc_simplify_isnan (gfc_expr *x)
2969 if (x->expr_type != EXPR_CONSTANT)
2972 return gfc_get_logical_expr (gfc_default_logical_kind, &x->where,
2973 mpfr_nan_p (x->value.real));
2977 /* Performs a shift on its first argument. Depending on the last
2978 argument, the shift can be arithmetic, i.e. with filling from the
2979 left like in the SHIFTA intrinsic. */
2981 simplify_shift (gfc_expr *e, gfc_expr *s, const char *name,
2982 bool arithmetic, int direction)
2985 int ashift, *bits, i, k, bitsize, shift;
2987 if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
2990 gfc_extract_int (s, &shift);
2992 k = gfc_validate_kind (BT_INTEGER, e->ts.kind, false);
2993 bitsize = gfc_integer_kinds[k].bit_size;
2995 result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
2999 mpz_set (result->value.integer, e->value.integer);
3003 if (direction > 0 && shift < 0)
3005 /* Left shift, as in SHIFTL. */
3006 gfc_error ("Second argument of %s is negative at %L", name, &e->where);
3007 return &gfc_bad_expr;
3009 else if (direction < 0)
3011 /* Right shift, as in SHIFTR or SHIFTA. */
3014 gfc_error ("Second argument of %s is negative at %L",
3016 return &gfc_bad_expr;
3022 ashift = (shift >= 0 ? shift : -shift);
3024 if (ashift > bitsize)
3026 gfc_error ("Magnitude of second argument of %s exceeds bit size "
3027 "at %L", name, &e->where);
3028 return &gfc_bad_expr;
3031 bits = XCNEWVEC (int, bitsize);
3033 for (i = 0; i < bitsize; i++)
3034 bits[i] = mpz_tstbit (e->value.integer, i);
3039 for (i = 0; i < shift; i++)
3040 mpz_clrbit (result->value.integer, i);
3042 for (i = 0; i < bitsize - shift; i++)
3045 mpz_clrbit (result->value.integer, i + shift);
3047 mpz_setbit (result->value.integer, i + shift);
3053 if (arithmetic && bits[bitsize - 1])
3054 for (i = bitsize - 1; i >= bitsize - ashift; i--)
3055 mpz_setbit (result->value.integer, i);
3057 for (i = bitsize - 1; i >= bitsize - ashift; i--)
3058 mpz_clrbit (result->value.integer, i);
3060 for (i = bitsize - 1; i >= ashift; i--)
3063 mpz_clrbit (result->value.integer, i - ashift);
3065 mpz_setbit (result->value.integer, i - ashift);
3069 convert_mpz_to_signed (result->value.integer, bitsize);
3077 gfc_simplify_ishft (gfc_expr *e, gfc_expr *s)
3079 return simplify_shift (e, s, "ISHFT", false, 0);
3084 gfc_simplify_lshift (gfc_expr *e, gfc_expr *s)
3086 return simplify_shift (e, s, "LSHIFT", false, 1);
3091 gfc_simplify_rshift (gfc_expr *e, gfc_expr *s)
3093 return simplify_shift (e, s, "RSHIFT", true, -1);
3098 gfc_simplify_shifta (gfc_expr *e, gfc_expr *s)
3100 return simplify_shift (e, s, "SHIFTA", true, -1);
3105 gfc_simplify_shiftl (gfc_expr *e, gfc_expr *s)
3107 return simplify_shift (e, s, "SHIFTL", false, 1);
3112 gfc_simplify_shiftr (gfc_expr *e, gfc_expr *s)
3114 return simplify_shift (e, s, "SHIFTR", false, -1);
3119 gfc_simplify_ishftc (gfc_expr *e, gfc_expr *s, gfc_expr *sz)
3122 int shift, ashift, isize, ssize, delta, k;
3125 if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
3128 gfc_extract_int (s, &shift);
3130 k = gfc_validate_kind (e->ts.type, e->ts.kind, false);
3131 isize = gfc_integer_kinds[k].bit_size;
3135 if (sz->expr_type != EXPR_CONSTANT)
3138 gfc_extract_int (sz, &ssize);
3152 gfc_error ("Magnitude of second argument of ISHFTC exceeds "
3153 "BIT_SIZE of first argument at %L", &s->where);
3154 return &gfc_bad_expr;
3157 result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
3159 mpz_set (result->value.integer, e->value.integer);
3164 convert_mpz_to_unsigned (result->value.integer, isize);
3166 bits = XCNEWVEC (int, ssize);
3168 for (i = 0; i < ssize; i++)
3169 bits[i] = mpz_tstbit (e->value.integer, i);
3171 delta = ssize - ashift;
3175 for (i = 0; i < delta; i++)
3178 mpz_clrbit (result->value.integer, i + shift);
3180 mpz_setbit (result->value.integer, i + shift);
3183 for (i = delta; i < ssize; i++)
3186 mpz_clrbit (result->value.integer, i - delta);
3188 mpz_setbit (result->value.integer, i - delta);
3193 for (i = 0; i < ashift; i++)
3196 mpz_clrbit (result->value.integer, i + delta);
3198 mpz_setbit (result->value.integer, i + delta);
3201 for (i = ashift; i < ssize; i++)
3204 mpz_clrbit (result->value.integer, i + shift);
3206 mpz_setbit (result->value.integer, i + shift);
3210 convert_mpz_to_signed (result->value.integer, isize);
3218 gfc_simplify_kind (gfc_expr *e)
3220 return gfc_get_int_expr (gfc_default_integer_kind, NULL, e->ts.kind);
3225 simplify_bound_dim (gfc_expr *array, gfc_expr *kind, int d, int upper,
3226 gfc_array_spec *as, gfc_ref *ref, bool coarray)
3228 gfc_expr *l, *u, *result;
3231 k = get_kind (BT_INTEGER, kind, upper ? "UBOUND" : "LBOUND",
3232 gfc_default_integer_kind);
3234 return &gfc_bad_expr;
3236 result = gfc_get_constant_expr (BT_INTEGER, k, &array->where);
3238 /* For non-variables, LBOUND(expr, DIM=n) = 1 and
3239 UBOUND(expr, DIM=n) = SIZE(expr, DIM=n). */
3240 if (!coarray && array->expr_type != EXPR_VARIABLE)
3244 gfc_expr* dim = result;
3245 mpz_set_si (dim->value.integer, d);
3247 result = gfc_simplify_size (array, dim, kind);
3248 gfc_free_expr (dim);
3253 mpz_set_si (result->value.integer, 1);
3258 /* Otherwise, we have a variable expression. */
3259 gcc_assert (array->expr_type == EXPR_VARIABLE);
3262 if (gfc_resolve_array_spec (as, 0) == FAILURE)
3265 /* The last dimension of an assumed-size array is special. */
3266 if ((!coarray && d == as->rank && as->type == AS_ASSUMED_SIZE && !upper)
3267 || (coarray && d == as->rank + as->corank
3268 && (!upper || gfc_option.coarray == GFC_FCOARRAY_SINGLE)))
3270 if (as->lower[d-1]->expr_type == EXPR_CONSTANT)
3272 gfc_free_expr (result);
3273 return gfc_copy_expr (as->lower[d-1]);
3279 result = gfc_get_constant_expr (BT_INTEGER, k, &array->where);
3281 /* Then, we need to know the extent of the given dimension. */
3282 if (coarray || ref->u.ar.type == AR_FULL)
3287 if (l->expr_type != EXPR_CONSTANT || u == NULL
3288 || u->expr_type != EXPR_CONSTANT)
3291 if (mpz_cmp (l->value.integer, u->value.integer) > 0)
3295 mpz_set_si (result->value.integer, 0);
3297 mpz_set_si (result->value.integer, 1);
3301 /* Nonzero extent. */
3303 mpz_set (result->value.integer, u->value.integer);
3305 mpz_set (result->value.integer, l->value.integer);
3312 if (gfc_ref_dimen_size (&ref->u.ar, d-1, &result->value.integer, NULL)
3317 mpz_set_si (result->value.integer, (long int) 1);
3321 return range_check (result, upper ? "UBOUND" : "LBOUND");
3324 gfc_free_expr (result);
3330 simplify_bound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind, int upper)
3336 if (array->ts.type == BT_CLASS)
3339 if (array->expr_type != EXPR_VARIABLE)
3346 /* Follow any component references. */
3347 as = array->symtree->n.sym->as;
3348 for (ref = array->ref; ref; ref = ref->next)
3353 switch (ref->u.ar.type)
3360 /* We're done because 'as' has already been set in the
3361 previous iteration. */
3378 as = ref->u.c.component->as;
3390 if (as && (as->type == AS_DEFERRED || as->type == AS_ASSUMED_SHAPE
3391 || as->type == AS_ASSUMED_RANK))
3396 /* Multi-dimensional bounds. */
3397 gfc_expr *bounds[GFC_MAX_DIMENSIONS];
3401 /* UBOUND(ARRAY) is not valid for an assumed-size array. */
3402 if (upper && as && as->type == AS_ASSUMED_SIZE)
3404 /* An error message will be emitted in
3405 check_assumed_size_reference (resolve.c). */
3406 return &gfc_bad_expr;
3409 /* Simplify the bounds for each dimension. */
3410 for (d = 0; d < array->rank; d++)
3412 bounds[d] = simplify_bound_dim (array, kind, d + 1, upper, as, ref,
3414 if (bounds[d] == NULL || bounds[d] == &gfc_bad_expr)
3418 for (j = 0; j < d; j++)
3419 gfc_free_expr (bounds[j]);
3424 /* Allocate the result expression. */
3425 k = get_kind (BT_INTEGER, kind, upper ? "UBOUND" : "LBOUND",
3426 gfc_default_integer_kind);
3428 return &gfc_bad_expr;
3430 e = gfc_get_array_expr (BT_INTEGER, k, &array->where);
3432 /* The result is a rank 1 array; its size is the rank of the first
3433 argument to {L,U}BOUND. */
3435 e->shape = gfc_get_shape (1);
3436 mpz_init_set_ui (e->shape[0], array->rank);
3438 /* Create the constructor for this array. */
3439 for (d = 0; d < array->rank; d++)
3440 gfc_constructor_append_expr (&e->value.constructor,
3441 bounds[d], &e->where);
3447 /* A DIM argument is specified. */
3448 if (dim->expr_type != EXPR_CONSTANT)
3451 d = mpz_get_si (dim->value.integer);
3453 if ((d < 1 || d > array->rank)
3454 || (d == array->rank && as && as->type == AS_ASSUMED_SIZE && upper))
3456 gfc_error ("DIM argument at %L is out of bounds", &dim->where);
3457 return &gfc_bad_expr;
3460 if (as && as->type == AS_ASSUMED_RANK)
3463 return simplify_bound_dim (array, kind, d, upper, as, ref, false);
3469 simplify_cobound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind, int upper)
3475 if (array->expr_type != EXPR_VARIABLE)
3478 /* Follow any component references. */
3479 as = (array->ts.type == BT_CLASS && array->ts.u.derived->components)
3480 ? array->ts.u.derived->components->as
3481 : array->symtree->n.sym->as;
3482 for (ref = array->ref; ref; ref = ref->next)
3487 switch (ref->u.ar.type)
3490 if (ref->u.ar.as->corank > 0)
3492 gcc_assert (as == ref->u.ar.as);
3499 /* We're done because 'as' has already been set in the
3500 previous iteration. */
3517 as = ref->u.c.component->as;
3530 if (as->cotype == AS_DEFERRED || as->cotype == AS_ASSUMED_SHAPE)
3535 /* Multi-dimensional cobounds. */
3536 gfc_expr *bounds[GFC_MAX_DIMENSIONS];
3540 /* Simplify the cobounds for each dimension. */
3541 for (d = 0; d < as->corank; d++)
3543 bounds[d] = simplify_bound_dim (array, kind, d + 1 + as->rank,
3544 upper, as, ref, true);
3545 if (bounds[d] == NULL || bounds[d] == &gfc_bad_expr)
3549 for (j = 0; j < d; j++)
3550 gfc_free_expr (bounds[j]);
3555 /* Allocate the result expression. */
3556 e = gfc_get_expr ();
3557 e->where = array->where;
3558 e->expr_type = EXPR_ARRAY;
3559 e->ts.type = BT_INTEGER;
3560 k = get_kind (BT_INTEGER, kind, upper ? "UCOBOUND" : "LCOBOUND",
3561 gfc_default_integer_kind);
3565 return &gfc_bad_expr;
3569 /* The result is a rank 1 array; its size is the rank of the first
3570 argument to {L,U}COBOUND. */
3572 e->shape = gfc_get_shape (1);
3573 mpz_init_set_ui (e->shape[0], as->corank);
3575 /* Create the constructor for this array. */
3576 for (d = 0; d < as->corank; d++)
3577 gfc_constructor_append_expr (&e->value.constructor,
3578 bounds[d], &e->where);
3583 /* A DIM argument is specified. */
3584 if (dim->expr_type != EXPR_CONSTANT)
3587 d = mpz_get_si (dim->value.integer);
3589 if (d < 1 || d > as->corank)
3591 gfc_error ("DIM argument at %L is out of bounds", &dim->where);
3592 return &gfc_bad_expr;
3595 return simplify_bound_dim (array, kind, d+as->rank, upper, as, ref, true);
3601 gfc_simplify_lbound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
3603 return simplify_bound (array, dim, kind, 0);
3608 gfc_simplify_lcobound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
3610 return simplify_cobound (array, dim, kind, 0);
3614 gfc_simplify_leadz (gfc_expr *e)
3616 unsigned long lz, bs;
3619 if (e->expr_type != EXPR_CONSTANT)
3622 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
3623 bs = gfc_integer_kinds[i].bit_size;
3624 if (mpz_cmp_si (e->value.integer, 0) == 0)
3626 else if (mpz_cmp_si (e->value.integer, 0) < 0)
3629 lz = bs - mpz_sizeinbase (e->value.integer, 2);
3631 return gfc_get_int_expr (gfc_default_integer_kind, &e->where, lz);
3636 gfc_simplify_len (gfc_expr *e, gfc_expr *kind)
3639 int k = get_kind (BT_INTEGER, kind, "LEN", gfc_default_integer_kind);
3642 return &gfc_bad_expr;
3644 if (e->expr_type == EXPR_CONSTANT)
3646 result = gfc_get_constant_expr (BT_INTEGER, k, &e->where);
3647 mpz_set_si (result->value.integer, e->value.character.length);
3648 return range_check (result, "LEN");
3650 else if (e->ts.u.cl != NULL && e->ts.u.cl->length != NULL
3651 && e->ts.u.cl->length->expr_type == EXPR_CONSTANT
3652 && e->ts.u.cl->length->ts.type == BT_INTEGER)
3654 result = gfc_get_constant_expr (BT_INTEGER, k, &e->where);
3655 mpz_set (result->value.integer, e->ts.u.cl->length->value.integer);
3656 return range_check (result, "LEN");
3664 gfc_simplify_len_trim (gfc_expr *e, gfc_expr *kind)
3668 int k = get_kind (BT_INTEGER, kind, "LEN_TRIM", gfc_default_integer_kind);
3671 return &gfc_bad_expr;
3673 if (e->expr_type != EXPR_CONSTANT)
3676 len = e->value.character.length;
3677 for (count = 0, i = 1; i <= len; i++)
3678 if (e->value.character.string[len - i] == ' ')
3683 result = gfc_get_int_expr (k, &e->where, len - count);
3684 return range_check (result, "LEN_TRIM");
3688 gfc_simplify_lgamma (gfc_expr *x)
3693 if (x->expr_type != EXPR_CONSTANT)
3696 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
3697 mpfr_lgamma (result->value.real, &sg, x->value.real, GFC_RND_MODE);
3699 return range_check (result, "LGAMMA");
3704 gfc_simplify_lge (gfc_expr *a, gfc_expr *b)
3706 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
3709 return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
3710 gfc_compare_string (a, b) >= 0);
3715 gfc_simplify_lgt (gfc_expr *a, gfc_expr *b)
3717 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
3720 return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
3721 gfc_compare_string (a, b) > 0);
3726 gfc_simplify_lle (gfc_expr *a, gfc_expr *b)
3728 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
3731 return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
3732 gfc_compare_string (a, b) <= 0);
3737 gfc_simplify_llt (gfc_expr *a, gfc_expr *b)
3739 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
3742 return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
3743 gfc_compare_string (a, b) < 0);
3748 gfc_simplify_log (gfc_expr *x)
3752 if (x->expr_type != EXPR_CONSTANT)
3755 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
3760 if (mpfr_sgn (x->value.real) <= 0)
3762 gfc_error ("Argument of LOG at %L cannot be less than or equal "
3763 "to zero", &x->where);
3764 gfc_free_expr (result);
3765 return &gfc_bad_expr;
3768 mpfr_log (result->value.real, x->value.real, GFC_RND_MODE);
3772 if ((mpfr_sgn (mpc_realref (x->value.complex)) == 0)
3773 && (mpfr_sgn (mpc_imagref (x->value.complex)) == 0))
3775 gfc_error ("Complex argument of LOG at %L cannot be zero",
3777 gfc_free_expr (result);
3778 return &gfc_bad_expr;
3781 gfc_set_model_kind (x->ts.kind);
3782 mpc_log (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
3786 gfc_internal_error ("gfc_simplify_log: bad type");
3789 return range_check (result, "LOG");
3794 gfc_simplify_log10 (gfc_expr *x)
3798 if (x->expr_type != EXPR_CONSTANT)
3801 if (mpfr_sgn (x->value.real) <= 0)
3803 gfc_error ("Argument of LOG10 at %L cannot be less than or equal "
3804 "to zero", &x->where);
3805 return &gfc_bad_expr;
3808 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
3809 mpfr_log10 (result->value.real, x->value.real, GFC_RND_MODE);
3811 return range_check (result, "LOG10");
3816 gfc_simplify_logical (gfc_expr *e, gfc_expr *k)
3820 kind = get_kind (BT_LOGICAL, k, "LOGICAL", gfc_default_logical_kind);
3822 return &gfc_bad_expr;
3824 if (e->expr_type != EXPR_CONSTANT)
3827 return gfc_get_logical_expr (kind, &e->where, e->value.logical);
3832 gfc_simplify_matmul (gfc_expr *matrix_a, gfc_expr *matrix_b)
3835 int row, result_rows, col, result_columns;
3836 int stride_a, offset_a, stride_b, offset_b;
3838 if (!is_constant_array_expr (matrix_a)
3839 || !is_constant_array_expr (matrix_b))
3842 gcc_assert (gfc_compare_types (&matrix_a->ts, &matrix_b->ts));
3843 result = gfc_get_array_expr (matrix_a->ts.type,
3847 if (matrix_a->rank == 1 && matrix_b->rank == 2)
3850 result_columns = mpz_get_si (matrix_b->shape[0]);
3852 stride_b = mpz_get_si (matrix_b->shape[0]);
3855 result->shape = gfc_get_shape (result->rank);
3856 mpz_init_set_si (result->shape[0], result_columns);
3858 else if (matrix_a->rank == 2 && matrix_b->rank == 1)
3860 result_rows = mpz_get_si (matrix_b->shape[0]);
3862 stride_a = mpz_get_si (matrix_a->shape[0]);
3866 result->shape = gfc_get_shape (result->rank);
3867 mpz_init_set_si (result->shape[0], result_rows);
3869 else if (matrix_a->rank == 2 && matrix_b->rank == 2)
3871 result_rows = mpz_get_si (matrix_a->shape[0]);
3872 result_columns = mpz_get_si (matrix_b->shape[1]);
3873 stride_a = mpz_get_si (matrix_a->shape[1]);
3874 stride_b = mpz_get_si (matrix_b->shape[0]);
3877 result->shape = gfc_get_shape (result->rank);
3878 mpz_init_set_si (result->shape[0], result_rows);
3879 mpz_init_set_si (result->shape[1], result_columns);
3884 offset_a = offset_b = 0;
3885 for (col = 0; col < result_columns; ++col)
3889 for (row = 0; row < result_rows; ++row)
3891 gfc_expr *e = compute_dot_product (matrix_a, stride_a, offset_a,
3892 matrix_b, 1, offset_b);
3893 gfc_constructor_append_expr (&result->value.constructor,
3899 offset_b += stride_b;
3907 gfc_simplify_maskr (gfc_expr *i, gfc_expr *kind_arg)
3913 if (i->expr_type != EXPR_CONSTANT)
3916 kind = get_kind (BT_INTEGER, kind_arg, "MASKR", gfc_default_integer_kind);
3918 return &gfc_bad_expr;
3919 k = gfc_validate_kind (BT_INTEGER, kind, false);
3921 s = gfc_extract_int (i, &arg);
3924 result = gfc_get_constant_expr (BT_INTEGER, kind, &i->where);
3926 /* MASKR(n) = 2^n - 1 */
3927 mpz_set_ui (result->value.integer, 1);
3928 mpz_mul_2exp (result->value.integer, result->value.integer, arg);
3929 mpz_sub_ui (result->value.integer, result->value.integer, 1);
3931 convert_mpz_to_signed (result->value.integer, gfc_integer_kinds[k].bit_size);
3938 gfc_simplify_maskl (gfc_expr *i, gfc_expr *kind_arg)
3945 if (i->expr_type != EXPR_CONSTANT)
3948 kind = get_kind (BT_INTEGER, kind_arg, "MASKL", gfc_default_integer_kind);
3950 return &gfc_bad_expr;
3951 k = gfc_validate_kind (BT_INTEGER, kind, false);
3953 s = gfc_extract_int (i, &arg);
3956 result = gfc_get_constant_expr (BT_INTEGER, kind, &i->where);
3958 /* MASKL(n) = 2^bit_size - 2^(bit_size - n) */
3959 mpz_init_set_ui (z, 1);
3960 mpz_mul_2exp (z, z, gfc_integer_kinds[k].bit_size);
3961 mpz_set_ui (result->value.integer, 1);
3962 mpz_mul_2exp (result->value.integer, result->value.integer,
3963 gfc_integer_kinds[k].bit_size - arg);
3964 mpz_sub (result->value.integer, z, result->value.integer);
3967 convert_mpz_to_signed (result->value.integer, gfc_integer_kinds[k].bit_size);
3974 gfc_simplify_merge (gfc_expr *tsource, gfc_expr *fsource, gfc_expr *mask)
3976 if (tsource->expr_type != EXPR_CONSTANT
3977 || fsource->expr_type != EXPR_CONSTANT
3978 || mask->expr_type != EXPR_CONSTANT)
3981 return gfc_copy_expr (mask->value.logical ? tsource : fsource);
3986 gfc_simplify_merge_bits (gfc_expr *i, gfc_expr *j, gfc_expr *mask_expr)
3988 mpz_t arg1, arg2, mask;
3991 if (i->expr_type != EXPR_CONSTANT || j->expr_type != EXPR_CONSTANT
3992 || mask_expr->expr_type != EXPR_CONSTANT)
3995 result = gfc_get_constant_expr (BT_INTEGER, i->ts.kind, &i->where);
3997 /* Convert all argument to unsigned. */
3998 mpz_init_set (arg1, i->value.integer);
3999 mpz_init_set (arg2, j->value.integer);
4000 mpz_init_set (mask, mask_expr->value.integer);
4002 /* MERGE_BITS(I,J,MASK) = IOR (IAND (I, MASK), IAND (J, NOT (MASK))). */
4003 mpz_and (arg1, arg1, mask);
4004 mpz_com (mask, mask);
4005 mpz_and (arg2, arg2, mask);
4006 mpz_ior (result->value.integer, arg1, arg2);
4016 /* Selects between current value and extremum for simplify_min_max
4017 and simplify_minval_maxval. */
4019 min_max_choose (gfc_expr *arg, gfc_expr *extremum, int sign)
4021 switch (arg->ts.type)
4024 if (mpz_cmp (arg->value.integer,
4025 extremum->value.integer) * sign > 0)
4026 mpz_set (extremum->value.integer, arg->value.integer);
4030 /* We need to use mpfr_min and mpfr_max to treat NaN properly. */
4032 mpfr_max (extremum->value.real, extremum->value.real,
4033 arg->value.real, GFC_RND_MODE);
4035 mpfr_min (extremum->value.real, extremum->value.real,
4036 arg->value.real, GFC_RND_MODE);
4040 #define LENGTH(x) ((x)->value.character.length)
4041 #define STRING(x) ((x)->value.character.string)
4042 if (LENGTH(extremum) < LENGTH(arg))
4044 gfc_char_t *tmp = STRING(extremum);
4046 STRING(extremum) = gfc_get_wide_string (LENGTH(arg) + 1);
4047 memcpy (STRING(extremum), tmp,
4048 LENGTH(extremum) * sizeof (gfc_char_t));
4049 gfc_wide_memset (&STRING(extremum)[LENGTH(extremum)], ' ',
4050 LENGTH(arg) - LENGTH(extremum));
4051 STRING(extremum)[LENGTH(arg)] = '\0'; /* For debugger */
4052 LENGTH(extremum) = LENGTH(arg);
4056 if (gfc_compare_string (arg, extremum) * sign > 0)
4058 free (STRING(extremum));
4059 STRING(extremum) = gfc_get_wide_string (LENGTH(extremum) + 1);
4060 memcpy (STRING(extremum), STRING(arg),
4061 LENGTH(arg) * sizeof (gfc_char_t));
4062 gfc_wide_memset (&STRING(extremum)[LENGTH(arg)], ' ',
4063 LENGTH(extremum) - LENGTH(arg));
4064 STRING(extremum)[LENGTH(extremum)] = '\0'; /* For debugger */
4071 gfc_internal_error ("simplify_min_max(): Bad type in arglist");
4076 /* This function is special since MAX() can take any number of
4077 arguments. The simplified expression is a rewritten version of the
4078 argument list containing at most one constant element. Other
4079 constant elements are deleted. Because the argument list has
4080 already been checked, this function always succeeds. sign is 1 for
4081 MAX(), -1 for MIN(). */
4084 simplify_min_max (gfc_expr *expr, int sign)
4086 gfc_actual_arglist *arg, *last, *extremum;
4087 gfc_intrinsic_sym * specific;
4091 specific = expr->value.function.isym;
4093 arg = expr->value.function.actual;
4095 for (; arg; last = arg, arg = arg->next)
4097 if (arg->expr->expr_type != EXPR_CONSTANT)
4100 if (extremum == NULL)
4106 min_max_choose (arg->expr, extremum->expr, sign);
4108 /* Delete the extra constant argument. */
4110 expr->value.function.actual = arg->next;
4112 last->next = arg->next;
4115 gfc_free_actual_arglist (arg);
4119 /* If there is one value left, replace the function call with the
4121 if (expr->value.function.actual->next != NULL)
4124 /* Convert to the correct type and kind. */
4125 if (expr->ts.type != BT_UNKNOWN)
4126 return gfc_convert_constant (expr->value.function.actual->expr,
4127 expr->ts.type, expr->ts.kind);
4129 if (specific->ts.type != BT_UNKNOWN)
4130 return gfc_convert_constant (expr->value.function.actual->expr,
4131 specific->ts.type, specific->ts.kind);
4133 return gfc_copy_expr (expr->value.function.actual->expr);
4138 gfc_simplify_min (gfc_expr *e)
4140 return simplify_min_max (e, -1);
4145 gfc_simplify_max (gfc_expr *e)
4147 return simplify_min_max (e, 1);
4151 /* This is a simplified version of simplify_min_max to provide
4152 simplification of minval and maxval for a vector. */
4155 simplify_minval_maxval (gfc_expr *expr, int sign)
4157 gfc_constructor *c, *extremum;
4158 gfc_intrinsic_sym * specific;
4161 specific = expr->value.function.isym;
4163 for (c = gfc_constructor_first (expr->value.constructor);
4164 c; c = gfc_constructor_next (c))
4166 if (c->expr->expr_type != EXPR_CONSTANT)
4169 if (extremum == NULL)
4175 min_max_choose (c->expr, extremum->expr, sign);
4178 if (extremum == NULL)
4181 /* Convert to the correct type and kind. */
4182 if (expr->ts.type != BT_UNKNOWN)
4183 return gfc_convert_constant (extremum->expr,
4184 expr->ts.type, expr->ts.kind);
4186 if (specific->ts.type != BT_UNKNOWN)
4187 return gfc_convert_constant (extremum->expr,
4188 specific->ts.type, specific->ts.kind);
4190 return gfc_copy_expr (extremum->expr);
4195 gfc_simplify_minval (gfc_expr *array, gfc_expr* dim, gfc_expr *mask)
4197 if (array->expr_type != EXPR_ARRAY || array->rank != 1 || dim || mask)
4200 return simplify_minval_maxval (array, -1);
4205 gfc_simplify_maxval (gfc_expr *array, gfc_expr* dim, gfc_expr *mask)
4207 if (array->expr_type != EXPR_ARRAY || array->rank != 1 || dim || mask)
4210 return simplify_minval_maxval (array, 1);
4215 gfc_simplify_maxexponent (gfc_expr *x)
4217 int i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
4218 return gfc_get_int_expr (gfc_default_integer_kind, &x->where,
4219 gfc_real_kinds[i].max_exponent);
4224 gfc_simplify_minexponent (gfc_expr *x)
4226 int i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
4227 return gfc_get_int_expr (gfc_default_integer_kind, &x->where,
4228 gfc_real_kinds[i].min_exponent);
4233 gfc_simplify_mod (gfc_expr *a, gfc_expr *p)
4238 if (a->expr_type != EXPR_CONSTANT || p->expr_type != EXPR_CONSTANT)
4241 kind = a->ts.kind > p->ts.kind ? a->ts.kind : p->ts.kind;
4242 result = gfc_get_constant_expr (a->ts.type, kind, &a->where);
4247 if (mpz_cmp_ui (p->value.integer, 0) == 0)
4249 /* Result is processor-dependent. */
4250 gfc_error ("Second argument MOD at %L is zero", &a->where);
4251 gfc_free_expr (result);
4252 return &gfc_bad_expr;
4254 mpz_tdiv_r (result->value.integer, a->value.integer, p->value.integer);
4258 if (mpfr_cmp_ui (p->value.real, 0) == 0)
4260 /* Result is processor-dependent. */
4261 gfc_error ("Second argument of MOD at %L is zero", &p->where);
4262 gfc_free_expr (result);
4263 return &gfc_bad_expr;
4266 gfc_set_model_kind (kind);
4267 mpfr_fmod (result->value.real, a->value.real, p->value.real,
4272 gfc_internal_error ("gfc_simplify_mod(): Bad arguments");
4275 return range_check (result, "MOD");
4280 gfc_simplify_modulo (gfc_expr *a, gfc_expr *p)
4285 if (a->expr_type != EXPR_CONSTANT || p->expr_type != EXPR_CONSTANT)
4288 kind = a->ts.kind > p->ts.kind ? a->ts.kind : p->ts.kind;
4289 result = gfc_get_constant_expr (a->ts.type, kind, &a->where);
4294 if (mpz_cmp_ui (p->value.integer, 0) == 0)
4296 /* Result is processor-dependent. This processor just opts
4297 to not handle it at all. */
4298 gfc_error ("Second argument of MODULO at %L is zero", &a->where);
4299 gfc_free_expr (result);
4300 return &gfc_bad_expr;
4302 mpz_fdiv_r (result->value.integer, a->value.integer, p->value.integer);
4307 if (mpfr_cmp_ui (p->value.real, 0) == 0)
4309 /* Result is processor-dependent. */
4310 gfc_error ("Second argument of MODULO at %L is zero", &p->where);
4311 gfc_free_expr (result);
4312 return &gfc_bad_expr;
4315 gfc_set_model_kind (kind);
4316 mpfr_fmod (result->value.real, a->value.real, p->value.real,
4318 if (mpfr_cmp_ui (result->value.real, 0) != 0)
4320 if (mpfr_signbit (a->value.real) != mpfr_signbit (p->value.real))
4321 mpfr_add (result->value.real, result->value.real, p->value.real,
4325 mpfr_copysign (result->value.real, result->value.real,
4326 p->value.real, GFC_RND_MODE);
4330 gfc_internal_error ("gfc_simplify_modulo(): Bad arguments");
4333 return range_check (result, "MODULO");
4337 /* Exists for the sole purpose of consistency with other intrinsics. */
4339 gfc_simplify_mvbits (gfc_expr *f ATTRIBUTE_UNUSED,
4340 gfc_expr *fp ATTRIBUTE_UNUSED,
4341 gfc_expr *l ATTRIBUTE_UNUSED,
4342 gfc_expr *to ATTRIBUTE_UNUSED,
4343 gfc_expr *tp ATTRIBUTE_UNUSED)
4350 gfc_simplify_nearest (gfc_expr *x, gfc_expr *s)
4353 mp_exp_t emin, emax;
4356 if (x->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
4359 result = gfc_copy_expr (x);
4361 /* Save current values of emin and emax. */
4362 emin = mpfr_get_emin ();
4363 emax = mpfr_get_emax ();
4365 /* Set emin and emax for the current model number. */
4366 kind = gfc_validate_kind (BT_REAL, x->ts.kind, 0);
4367 mpfr_set_emin ((mp_exp_t) gfc_real_kinds[kind].min_exponent -
4368 mpfr_get_prec(result->value.real) + 1);
4369 mpfr_set_emax ((mp_exp_t) gfc_real_kinds[kind].max_exponent - 1);
4370 mpfr_check_range (result->value.real, 0, GMP_RNDU);
4372 if (mpfr_sgn (s->value.real) > 0)
4374 mpfr_nextabove (result->value.real);
4375 mpfr_subnormalize (result->value.real, 0, GMP_RNDU);
4379 mpfr_nextbelow (result->value.real);
4380 mpfr_subnormalize (result->value.real, 0, GMP_RNDD);
4383 mpfr_set_emin (emin);
4384 mpfr_set_emax (emax);
4386 /* Only NaN can occur. Do not use range check as it gives an
4387 error for denormal numbers. */
4388 if (mpfr_nan_p (result->value.real) && gfc_option.flag_range_check)
4390 gfc_error ("Result of NEAREST is NaN at %L", &result->where);
4391 gfc_free_expr (result);
4392 return &gfc_bad_expr;
4400 simplify_nint (const char *name, gfc_expr *e, gfc_expr *k)
4402 gfc_expr *itrunc, *result;
4405 kind = get_kind (BT_INTEGER, k, name, gfc_default_integer_kind);
4407 return &gfc_bad_expr;
4409 if (e->expr_type != EXPR_CONSTANT)
4412 itrunc = gfc_copy_expr (e);
4413 mpfr_round (itrunc->value.real, e->value.real);
4415 result = gfc_get_constant_expr (BT_INTEGER, kind, &e->where);
4416 gfc_mpfr_to_mpz (result->value.integer, itrunc->value.real, &e->where);
4418 gfc_free_expr (itrunc);
4420 return range_check (result, name);
4425 gfc_simplify_new_line (gfc_expr *e)
4429 result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, 1);
4430 result->value.character.string[0] = '\n';
4437 gfc_simplify_nint (gfc_expr *e, gfc_expr *k)
4439 return simplify_nint ("NINT", e, k);
4444 gfc_simplify_idnint (gfc_expr *e)
4446 return simplify_nint ("IDNINT", e, NULL);
4451 add_squared (gfc_expr *result, gfc_expr *e)
4455 gcc_assert (e->ts.type == BT_REAL && e->expr_type == EXPR_CONSTANT);
4456 gcc_assert (result->ts.type == BT_REAL
4457 && result->expr_type == EXPR_CONSTANT);
4459 gfc_set_model_kind (result->ts.kind);
4461 mpfr_pow_ui (tmp, e->value.real, 2, GFC_RND_MODE);
4462 mpfr_add (result->value.real, result->value.real, tmp,
4471 do_sqrt (gfc_expr *result, gfc_expr *e)
4473 gcc_assert (e->ts.type == BT_REAL && e->expr_type == EXPR_CONSTANT);
4474 gcc_assert (result->ts.type == BT_REAL
4475 && result->expr_type == EXPR_CONSTANT);
4477 mpfr_set (result->value.real, e->value.real, GFC_RND_MODE);
4478 mpfr_sqrt (result->value.real, result->value.real, GFC_RND_MODE);
4484 gfc_simplify_norm2 (gfc_expr *e, gfc_expr *dim)
4488 if (!is_constant_array_expr (e)
4489 || (dim != NULL && !gfc_is_constant_expr (dim)))
4492 result = transformational_result (e, dim, e->ts.type, e->ts.kind, &e->where);
4493 init_result_expr (result, 0, NULL);
4495 if (!dim || e->rank == 1)
4497 result = simplify_transformation_to_scalar (result, e, NULL,
4499 mpfr_sqrt (result->value.real, result->value.real, GFC_RND_MODE);
4502 result = simplify_transformation_to_array (result, e, dim, NULL,
4503 add_squared, &do_sqrt);
4510 gfc_simplify_not (gfc_expr *e)
4514 if (e->expr_type != EXPR_CONSTANT)
4517 result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
4518 mpz_com (result->value.integer, e->value.integer);
4520 return range_check (result, "NOT");
4525 gfc_simplify_null (gfc_expr *mold)
4531 result = gfc_copy_expr (mold);
4532 result->expr_type = EXPR_NULL;
4535 result = gfc_get_null_expr (NULL);
4542 gfc_simplify_num_images (void)
4546 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
4548 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
4549 return &gfc_bad_expr;
4552 if (gfc_option.coarray != GFC_FCOARRAY_SINGLE)
4555 /* FIXME: gfc_current_locus is wrong. */
4556 result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
4557 &gfc_current_locus);
4558 mpz_set_si (result->value.integer, 1);
4564 gfc_simplify_or (gfc_expr *x, gfc_expr *y)
4569 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
4572 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
4577 result = gfc_get_constant_expr (BT_INTEGER, kind, &x->where);
4578 mpz_ior (result->value.integer, x->value.integer, y->value.integer);
4579 return range_check (result, "OR");
4582 return gfc_get_logical_expr (kind, &x->where,
4583 x->value.logical || y->value.logical);
4591 gfc_simplify_pack (gfc_expr *array, gfc_expr *mask, gfc_expr *vector)
4594 gfc_constructor *array_ctor, *mask_ctor, *vector_ctor;
4596 if (!is_constant_array_expr(array)
4597 || !is_constant_array_expr(vector)
4598 || (!gfc_is_constant_expr (mask)
4599 && !is_constant_array_expr(mask)))
4602 result = gfc_get_array_expr (array->ts.type, array->ts.kind, &array->where);
4603 if (array->ts.type == BT_DERIVED)
4604 result->ts.u.derived = array->ts.u.derived;
4606 array_ctor = gfc_constructor_first (array->value.constructor);
4607 vector_ctor = vector
4608 ? gfc_constructor_first (vector->value.constructor)
4611 if (mask->expr_type == EXPR_CONSTANT
4612 && mask->value.logical)
4614 /* Copy all elements of ARRAY to RESULT. */
4617 gfc_constructor_append_expr (&result->value.constructor,
4618 gfc_copy_expr (array_ctor->expr),
4621 array_ctor = gfc_constructor_next (array_ctor);
4622 vector_ctor = gfc_constructor_next (vector_ctor);
4625 else if (mask->expr_type == EXPR_ARRAY)
4627 /* Copy only those elements of ARRAY to RESULT whose
4628 MASK equals .TRUE.. */
4629 mask_ctor = gfc_constructor_first (mask->value.constructor);
4632 if (mask_ctor->expr->value.logical)
4634 gfc_constructor_append_expr (&result->value.constructor,
4635 gfc_copy_expr (array_ctor->expr),
4637 vector_ctor = gfc_constructor_next (vector_ctor);
4640 array_ctor = gfc_constructor_next (array_ctor);
4641 mask_ctor = gfc_constructor_next (mask_ctor);
4645 /* Append any left-over elements from VECTOR to RESULT. */
4648 gfc_constructor_append_expr (&result->value.constructor,
4649 gfc_copy_expr (vector_ctor->expr),
4651 vector_ctor = gfc_constructor_next (vector_ctor);
4654 result->shape = gfc_get_shape (1);
4655 gfc_array_size (result, &result->shape[0]);
4657 if (array->ts.type == BT_CHARACTER)
4658 result->ts.u.cl = array->ts.u.cl;
4665 do_xor (gfc_expr *result, gfc_expr *e)
4667 gcc_assert (e->ts.type == BT_LOGICAL && e->expr_type == EXPR_CONSTANT);
4668 gcc_assert (result->ts.type == BT_LOGICAL
4669 && result->expr_type == EXPR_CONSTANT);
4671 result->value.logical = result->value.logical != e->value.logical;
4678 gfc_simplify_parity (gfc_expr *e, gfc_expr *dim)
4680 return simplify_transformation (e, dim, NULL, 0, do_xor);
4685 gfc_simplify_popcnt (gfc_expr *e)
4690 if (e->expr_type != EXPR_CONSTANT)
4693 k = gfc_validate_kind (e->ts.type, e->ts.kind, false);
4695 /* Convert argument to unsigned, then count the '1' bits. */
4696 mpz_init_set (x, e->value.integer);
4697 convert_mpz_to_unsigned (x, gfc_integer_kinds[k].bit_size);
4698 res = mpz_popcount (x);
4701 return gfc_get_int_expr (gfc_default_integer_kind, &e->where, res);
4706 gfc_simplify_poppar (gfc_expr *e)
4712 if (e->expr_type != EXPR_CONSTANT)
4715 popcnt = gfc_simplify_popcnt (e);
4716 gcc_assert (popcnt);
4718 s = gfc_extract_int (popcnt, &i);
4721 return gfc_get_int_expr (gfc_default_integer_kind, &e->where, i % 2);
4726 gfc_simplify_precision (gfc_expr *e)
4728 int i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
4729 return gfc_get_int_expr (gfc_default_integer_kind, &e->where,
4730 gfc_real_kinds[i].precision);
4735 gfc_simplify_product (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
4737 return simplify_transformation (array, dim, mask, 1, gfc_multiply);
4742 gfc_simplify_radix (gfc_expr *e)
4745 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
4750 i = gfc_integer_kinds[i].radix;
4754 i = gfc_real_kinds[i].radix;
4761 return gfc_get_int_expr (gfc_default_integer_kind, &e->where, i);
4766 gfc_simplify_range (gfc_expr *e)
4769 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
4774 i = gfc_integer_kinds[i].range;
4779 i = gfc_real_kinds[i].range;
4786 return gfc_get_int_expr (gfc_default_integer_kind, &e->where, i);
4791 gfc_simplify_rank (gfc_expr *e)
4797 return gfc_get_int_expr (gfc_default_integer_kind, &e->where, e->rank);
4802 gfc_simplify_real (gfc_expr *e, gfc_expr *k)
4804 gfc_expr *result = NULL;
4807 if (e->ts.type == BT_COMPLEX)
4808 kind = get_kind (BT_REAL, k, "REAL", e->ts.kind);
4810 kind = get_kind (BT_REAL, k, "REAL", gfc_default_real_kind);
4813 return &gfc_bad_expr;
4815 if (e->expr_type != EXPR_CONSTANT)
4818 if (convert_boz (e, kind) == &gfc_bad_expr)
4819 return &gfc_bad_expr;
4821 result = gfc_convert_constant (e, BT_REAL, kind);
4822 if (result == &gfc_bad_expr)
4823 return &gfc_bad_expr;
4825 return range_check (result, "REAL");
4830 gfc_simplify_realpart (gfc_expr *e)
4834 if (e->expr_type != EXPR_CONSTANT)
4837 result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
4838 mpc_real (result->value.real, e->value.complex, GFC_RND_MODE);
4840 return range_check (result, "REALPART");
4844 gfc_simplify_repeat (gfc_expr *e, gfc_expr *n)
4847 int i, j, len, ncop, nlen;
4849 bool have_length = false;
4851 /* If NCOPIES isn't a constant, there's nothing we can do. */
4852 if (n->expr_type != EXPR_CONSTANT)
4855 /* If NCOPIES is negative, it's an error. */
4856 if (mpz_sgn (n->value.integer) < 0)
4858 gfc_error ("Argument NCOPIES of REPEAT intrinsic is negative at %L",
4860 return &gfc_bad_expr;
4863 /* If we don't know the character length, we can do no more. */
4864 if (e->ts.u.cl && e->ts.u.cl->length
4865 && e->ts.u.cl->length->expr_type == EXPR_CONSTANT)
4867 len = mpz_get_si (e->ts.u.cl->length->value.integer);
4870 else if (e->expr_type == EXPR_CONSTANT
4871 && (e->ts.u.cl == NULL || e->ts.u.cl->length == NULL))
4873 len = e->value.character.length;
4878 /* If the source length is 0, any value of NCOPIES is valid
4879 and everything behaves as if NCOPIES == 0. */
4882 mpz_set_ui (ncopies, 0);
4884 mpz_set (ncopies, n->value.integer);
4886 /* Check that NCOPIES isn't too large. */
4892 /* Compute the maximum value allowed for NCOPIES: huge(cl) / len. */
4894 i = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4898 mpz_tdiv_q (max, gfc_integer_kinds[i].huge,
4899 e->ts.u.cl->length->value.integer);
4903 mpz_init_set_si (mlen, len);
4904 mpz_tdiv_q (max, gfc_integer_kinds[i].huge, mlen);
4908 /* The check itself. */
4909 if (mpz_cmp (ncopies, max) > 0)
4912 mpz_clear (ncopies);
4913 gfc_error ("Argument NCOPIES of REPEAT intrinsic is too large at %L",
4915 return &gfc_bad_expr;
4920 mpz_clear (ncopies);
4922 /* For further simplification, we need the character string to be
4924 if (e->expr_type != EXPR_CONSTANT)
4928 (e->ts.u.cl->length &&
4929 mpz_sgn (e->ts.u.cl->length->value.integer)) != 0)
4931 const char *res = gfc_extract_int (n, &ncop);
4932 gcc_assert (res == NULL);
4938 return gfc_get_character_expr (e->ts.kind, &e->where, NULL, 0);
4940 len = e->value.character.length;
4943 result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, nlen);
4944 for (i = 0; i < ncop; i++)
4945 for (j = 0; j < len; j++)
4946 result->value.character.string[j+i*len]= e->value.character.string[j];
4948 result->value.character.string[nlen] = '\0'; /* For debugger */
4953 /* This one is a bear, but mainly has to do with shuffling elements. */
4956 gfc_simplify_reshape (gfc_expr *source, gfc_expr *shape_exp,
4957 gfc_expr *pad, gfc_expr *order_exp)
4959 int order[GFC_MAX_DIMENSIONS], shape[GFC_MAX_DIMENSIONS];
4960 int i, rank, npad, x[GFC_MAX_DIMENSIONS];
4964 gfc_expr *e, *result;
4966 /* Check that argument expression types are OK. */
4967 if (!is_constant_array_expr (source)
4968 || !is_constant_array_expr (shape_exp)
4969 || !is_constant_array_expr (pad)
4970 || !is_constant_array_expr (order_exp))
4973 /* Proceed with simplification, unpacking the array. */
4980 e = gfc_constructor_lookup_expr (shape_exp->value.constructor, rank);
4984 gfc_extract_int (e, &shape[rank]);
4986 gcc_assert (rank >= 0 && rank < GFC_MAX_DIMENSIONS);
4987 gcc_assert (shape[rank] >= 0);
4992 gcc_assert (rank > 0);
4994 /* Now unpack the order array if present. */
4995 if (order_exp == NULL)
4997 for (i = 0; i < rank; i++)
5002 for (i = 0; i < rank; i++)
5005 for (i = 0; i < rank; i++)
5007 e = gfc_constructor_lookup_expr (order_exp->value.constructor, i);
5010 gfc_extract_int (e, &order[i]);
5012 gcc_assert (order[i] >= 1 && order[i] <= rank);
5014 gcc_assert (x[order[i]] == 0);
5019 /* Count the elements in the source and padding arrays. */
5024 gfc_array_size (pad, &size);
5025 npad = mpz_get_ui (size);
5029 gfc_array_size (source, &size);
5030 nsource = mpz_get_ui (size);
5033 /* If it weren't for that pesky permutation we could just loop
5034 through the source and round out any shortage with pad elements.
5035 But no, someone just had to have the compiler do something the
5036 user should be doing. */
5038 for (i = 0; i < rank; i++)
5041 result = gfc_get_array_expr (source->ts.type, source->ts.kind,
5043 if (source->ts.type == BT_DERIVED)
5044 result->ts.u.derived = source->ts.u.derived;
5045 result->rank = rank;
5046 result->shape = gfc_get_shape (rank);
5047 for (i = 0; i < rank; i++)
5048 mpz_init_set_ui (result->shape[i], shape[i]);
5050 while (nsource > 0 || npad > 0)
5052 /* Figure out which element to extract. */
5053 mpz_set_ui (index, 0);
5055 for (i = rank - 1; i >= 0; i--)
5057 mpz_add_ui (index, index, x[order[i]]);
5059 mpz_mul_ui (index, index, shape[order[i - 1]]);
5062 if (mpz_cmp_ui (index, INT_MAX) > 0)
5063 gfc_internal_error ("Reshaped array too large at %C");
5065 j = mpz_get_ui (index);
5068 e = gfc_constructor_lookup_expr (source->value.constructor, j);
5071 gcc_assert (npad > 0);
5075 e = gfc_constructor_lookup_expr (pad->value.constructor, j);
5079 gfc_constructor_append_expr (&result->value.constructor,
5080 gfc_copy_expr (e), &e->where);
5082 /* Calculate the next element. */
5086 if (++x[i] < shape[i])
5102 gfc_simplify_rrspacing (gfc_expr *x)
5108 if (x->expr_type != EXPR_CONSTANT)
5111 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
5113 result = gfc_get_constant_expr (BT_REAL, x->ts.kind, &x->where);
5114 mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE);
5116 /* Special case x = -0 and 0. */
5117 if (mpfr_sgn (result->value.real) == 0)
5119 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
5123 /* | x * 2**(-e) | * 2**p. */
5124 e = - (long int) mpfr_get_exp (x->value.real);
5125 mpfr_mul_2si (result->value.real, result->value.real, e, GFC_RND_MODE);
5127 p = (long int) gfc_real_kinds[i].digits;
5128 mpfr_mul_2si (result->value.real, result->value.real, p, GFC_RND_MODE);
5130 return range_check (result, "RRSPACING");
5135 gfc_simplify_scale (gfc_expr *x, gfc_expr *i)
5137 int k, neg_flag, power, exp_range;
5138 mpfr_t scale, radix;
5141 if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
5144 result = gfc_get_constant_expr (BT_REAL, x->ts.kind, &x->where);
5146 if (mpfr_sgn (x->value.real) == 0)
5148 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
5152 k = gfc_validate_kind (BT_REAL, x->ts.kind, false);
5154 exp_range = gfc_real_kinds[k].max_exponent - gfc_real_kinds[k].min_exponent;
5156 /* This check filters out values of i that would overflow an int. */
5157 if (mpz_cmp_si (i->value.integer, exp_range + 2) > 0
5158 || mpz_cmp_si (i->value.integer, -exp_range - 2) < 0)
5160 gfc_error ("Result of SCALE overflows its kind at %L", &result->where);
5161 gfc_free_expr (result);
5162 return &gfc_bad_expr;
5165 /* Compute scale = radix ** power. */
5166 power = mpz_get_si (i->value.integer);
5176 gfc_set_model_kind (x->ts.kind);
5179 mpfr_set_ui (radix, gfc_real_kinds[k].radix, GFC_RND_MODE);
5180 mpfr_pow_ui (scale, radix, power, GFC_RND_MODE);
5183 mpfr_div (result->value.real, x->value.real, scale, GFC_RND_MODE);
5185 mpfr_mul (result->value.real, x->value.real, scale, GFC_RND_MODE);
5187 mpfr_clears (scale, radix, NULL);
5189 return range_check (result, "SCALE");
5193 /* Variants of strspn and strcspn that operate on wide characters. */
5196 wide_strspn (const gfc_char_t *s1, const gfc_char_t *s2)
5199 const gfc_char_t *c;
5203 for (c = s2; *c; c++)
5217 wide_strcspn (const gfc_char_t *s1, const gfc_char_t *s2)
5220 const gfc_char_t *c;
5224 for (c = s2; *c; c++)
5239 gfc_simplify_scan (gfc_expr *e, gfc_expr *c, gfc_expr *b, gfc_expr *kind)
5244 size_t indx, len, lenc;
5245 int k = get_kind (BT_INTEGER, kind, "SCAN", gfc_default_integer_kind);
5248 return &gfc_bad_expr;
5250 if (e->expr_type != EXPR_CONSTANT || c->expr_type != EXPR_CONSTANT
5251 || ( b != NULL && b->expr_type != EXPR_CONSTANT))
5254 if (b != NULL && b->value.logical != 0)
5259 len = e->value.character.length;
5260 lenc = c->value.character.length;
5262 if (len == 0 || lenc == 0)
5270 indx = wide_strcspn (e->value.character.string,
5271 c->value.character.string) + 1;
5278 for (indx = len; indx > 0; indx--)
5280 for (i = 0; i < lenc; i++)
5282 if (c->value.character.string[i]
5283 == e->value.character.string[indx - 1])
5292 result = gfc_get_int_expr (k, &e->where, indx);
5293 return range_check (result, "SCAN");
5298 gfc_simplify_selected_char_kind (gfc_expr *e)
5302 if (e->expr_type != EXPR_CONSTANT)
5305 if (gfc_compare_with_Cstring (e, "ascii", false) == 0
5306 || gfc_compare_with_Cstring (e, "default", false) == 0)
5308 else if (gfc_compare_with_Cstring (e, "iso_10646", false) == 0)
5313 return gfc_get_int_expr (gfc_default_integer_kind, &e->where, kind);
5318 gfc_simplify_selected_int_kind (gfc_expr *e)
5322 if (e->expr_type != EXPR_CONSTANT || gfc_extract_int (e, &range) != NULL)
5327 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
5328 if (gfc_integer_kinds[i].range >= range
5329 && gfc_integer_kinds[i].kind < kind)
5330 kind = gfc_integer_kinds[i].kind;
5332 if (kind == INT_MAX)
5335 return gfc_get_int_expr (gfc_default_integer_kind, &e->where, kind);
5340 gfc_simplify_selected_real_kind (gfc_expr *p, gfc_expr *q, gfc_expr *rdx)
5342 int range, precision, radix, i, kind, found_precision, found_range,
5344 locus *loc = &gfc_current_locus;
5350 if (p->expr_type != EXPR_CONSTANT
5351 || gfc_extract_int (p, &precision) != NULL)
5360 if (q->expr_type != EXPR_CONSTANT
5361 || gfc_extract_int (q, &range) != NULL)
5372 if (rdx->expr_type != EXPR_CONSTANT
5373 || gfc_extract_int (rdx, &radix) != NULL)
5381 found_precision = 0;
5385 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
5387 if (gfc_real_kinds[i].precision >= precision)
5388 found_precision = 1;
5390 if (gfc_real_kinds[i].range >= range)
5393 if (gfc_real_kinds[i].radix >= radix)
5396 if (gfc_real_kinds[i].precision >= precision
5397 && gfc_real_kinds[i].range >= range
5398 && gfc_real_kinds[i].radix >= radix && gfc_real_kinds[i].kind < kind)
5399 kind = gfc_real_kinds[i].kind;
5402 if (kind == INT_MAX)
5404 if (found_radix && found_range && !found_precision)
5406 else if (found_radix && found_precision && !found_range)
5408 else if (found_radix && !found_precision && !found_range)
5410 else if (found_radix)
5416 return gfc_get_int_expr (gfc_default_integer_kind, loc, kind);
5421 gfc_simplify_set_exponent (gfc_expr *x, gfc_expr *i)
5424 mpfr_t exp, absv, log2, pow2, frac;
5427 if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
5430 result = gfc_get_constant_expr (BT_REAL, x->ts.kind, &x->where);
5432 if (mpfr_sgn (x->value.real) == 0)
5434 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
5438 gfc_set_model_kind (x->ts.kind);
5445 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
5446 mpfr_log2 (log2, absv, GFC_RND_MODE);
5448 mpfr_trunc (log2, log2);
5449 mpfr_add_ui (exp, log2, 1, GFC_RND_MODE);
5451 /* Old exponent value, and fraction. */
5452 mpfr_ui_pow (pow2, 2, exp, GFC_RND_MODE);
5454 mpfr_div (frac, absv, pow2, GFC_RND_MODE);
5457 exp2 = (unsigned long) mpz_get_d (i->value.integer);
5458 mpfr_mul_2exp (result->value.real, frac, exp2, GFC_RND_MODE);
5460 mpfr_clears (absv, log2, pow2, frac, NULL);
5462 return range_check (result, "SET_EXPONENT");
5467 gfc_simplify_shape (gfc_expr *source, gfc_expr *kind)
5469 mpz_t shape[GFC_MAX_DIMENSIONS];
5470 gfc_expr *result, *e, *f;
5474 int k = get_kind (BT_INTEGER, kind, "SHAPE", gfc_default_integer_kind);
5476 if (source->rank == -1)
5479 result = gfc_get_array_expr (BT_INTEGER, k, &source->where);
5481 if (source->rank == 0)
5484 if (source->expr_type == EXPR_VARIABLE)
5486 ar = gfc_find_array_ref (source);
5487 t = gfc_array_ref_shape (ar, shape);
5489 else if (source->shape)
5492 for (n = 0; n < source->rank; n++)
5494 mpz_init (shape[n]);
5495 mpz_set (shape[n], source->shape[n]);
5501 for (n = 0; n < source->rank; n++)
5503 e = gfc_get_constant_expr (BT_INTEGER, k, &source->where);
5507 mpz_set (e->value.integer, shape[n]);
5508 mpz_clear (shape[n]);
5512 mpz_set_ui (e->value.integer, n + 1);
5514 f = gfc_simplify_size (source, e, NULL);
5518 gfc_free_expr (result);
5525 gfc_constructor_append_expr (&result->value.constructor, e, NULL);
5533 gfc_simplify_size (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
5536 gfc_expr *return_value;
5538 int k = get_kind (BT_INTEGER, kind, "SIZE", gfc_default_integer_kind);
5541 return &gfc_bad_expr;
5543 /* For unary operations, the size of the result is given by the size
5544 of the operand. For binary ones, it's the size of the first operand
5545 unless it is scalar, then it is the size of the second. */
5546 if (array->expr_type == EXPR_OP && !array->value.op.uop)
5548 gfc_expr* replacement;
5549 gfc_expr* simplified;
5551 switch (array->value.op.op)
5553 /* Unary operations. */
5555 case INTRINSIC_UPLUS:
5556 case INTRINSIC_UMINUS:
5557 case INTRINSIC_PARENTHESES:
5558 replacement = array->value.op.op1;
5561 /* Binary operations. If any one of the operands is scalar, take
5562 the other one's size. If both of them are arrays, it does not
5563 matter -- try to find one with known shape, if possible. */
5565 if (array->value.op.op1->rank == 0)
5566 replacement = array->value.op.op2;
5567 else if (array->value.op.op2->rank == 0)
5568 replacement = array->value.op.op1;
5571 simplified = gfc_simplify_size (array->value.op.op1, dim, kind);
5575 replacement = array->value.op.op2;
5580 /* Try to reduce it directly if possible. */
5581 simplified = gfc_simplify_size (replacement, dim, kind);
5583 /* Otherwise, we build a new SIZE call. This is hopefully at least
5584 simpler than the original one. */
5586 simplified = gfc_build_intrinsic_call ("size", array->where, 3,
5587 gfc_copy_expr (replacement),
5588 gfc_copy_expr (dim),
5589 gfc_copy_expr (kind));
5596 if (gfc_array_size (array, &size) == FAILURE)
5601 if (dim->expr_type != EXPR_CONSTANT)
5604 d = mpz_get_ui (dim->value.integer) - 1;
5605 if (gfc_array_dimen_size (array, d, &size) == FAILURE)
5609 return_value = gfc_get_int_expr (k, &array->where, mpz_get_si (size));
5611 return return_value;
5616 gfc_simplify_sign (gfc_expr *x, gfc_expr *y)
5620 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
5623 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
5628 mpz_abs (result->value.integer, x->value.integer);
5629 if (mpz_sgn (y->value.integer) < 0)
5630 mpz_neg (result->value.integer, result->value.integer);
5634 if (gfc_option.flag_sign_zero)
5635 mpfr_copysign (result->value.real, x->value.real, y->value.real,
5638 mpfr_setsign (result->value.real, x->value.real,
5639 mpfr_sgn (y->value.real) < 0 ? 1 : 0, GFC_RND_MODE);
5643 gfc_internal_error ("Bad type in gfc_simplify_sign");
5651 gfc_simplify_sin (gfc_expr *x)
5655 if (x->expr_type != EXPR_CONSTANT)
5658 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
5663 mpfr_sin (result->value.real, x->value.real, GFC_RND_MODE);
5667 gfc_set_model (x->value.real);
5668 mpc_sin (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
5672 gfc_internal_error ("in gfc_simplify_sin(): Bad type");
5675 return range_check (result, "SIN");
5680 gfc_simplify_sinh (gfc_expr *x)
5684 if (x->expr_type != EXPR_CONSTANT)
5687 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
5692 mpfr_sinh (result->value.real, x->value.real, GFC_RND_MODE);
5696 mpc_sinh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
5703 return range_check (result, "SINH");
5707 /* The argument is always a double precision real that is converted to
5708 single precision. TODO: Rounding! */
5711 gfc_simplify_sngl (gfc_expr *a)
5715 if (a->expr_type != EXPR_CONSTANT)
5718 result = gfc_real2real (a, gfc_default_real_kind);
5719 return range_check (result, "SNGL");
5724 gfc_simplify_spacing (gfc_expr *x)
5730 if (x->expr_type != EXPR_CONSTANT)
5733 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
5735 result = gfc_get_constant_expr (BT_REAL, x->ts.kind, &x->where);
5737 /* Special case x = 0 and -0. */
5738 mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE);
5739 if (mpfr_sgn (result->value.real) == 0)
5741 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
5745 /* In the Fortran 95 standard, the result is b**(e - p) where b, e, and p
5746 are the radix, exponent of x, and precision. This excludes the
5747 possibility of subnormal numbers. Fortran 2003 states the result is
5748 b**max(e - p, emin - 1). */
5750 ep = (long int) mpfr_get_exp (x->value.real) - gfc_real_kinds[i].digits;
5751 en = (long int) gfc_real_kinds[i].min_exponent - 1;
5752 en = en > ep ? en : ep;
5754 mpfr_set_ui (result->value.real, 1, GFC_RND_MODE);
5755 mpfr_mul_2si (result->value.real, result->value.real, en, GFC_RND_MODE);
5757 return range_check (result, "SPACING");
5762 gfc_simplify_spread (gfc_expr *source, gfc_expr *dim_expr, gfc_expr *ncopies_expr)
5764 gfc_expr *result = 0L;
5765 int i, j, dim, ncopies;
5768 if ((!gfc_is_constant_expr (source)
5769 && !is_constant_array_expr (source))
5770 || !gfc_is_constant_expr (dim_expr)
5771 || !gfc_is_constant_expr (ncopies_expr))
5774 gcc_assert (dim_expr->ts.type == BT_INTEGER);
5775 gfc_extract_int (dim_expr, &dim);
5776 dim -= 1; /* zero-base DIM */
5778 gcc_assert (ncopies_expr->ts.type == BT_INTEGER);
5779 gfc_extract_int (ncopies_expr, &ncopies);
5780 ncopies = MAX (ncopies, 0);
5782 /* Do not allow the array size to exceed the limit for an array
5784 if (source->expr_type == EXPR_ARRAY)
5786 if (gfc_array_size (source, &size) == FAILURE)
5787 gfc_internal_error ("Failure getting length of a constant array.");
5790 mpz_init_set_ui (size, 1);
5792 if (mpz_get_si (size)*ncopies > gfc_option.flag_max_array_constructor)
5795 if (source->expr_type == EXPR_CONSTANT)
5797 gcc_assert (dim == 0);
5799 result = gfc_get_array_expr (source->ts.type, source->ts.kind,
5801 if (source->ts.type == BT_DERIVED)
5802 result->ts.u.derived = source->ts.u.derived;
5804 result->shape = gfc_get_shape (result->rank);
5805 mpz_init_set_si (result->shape[0], ncopies);
5807 for (i = 0; i < ncopies; ++i)
5808 gfc_constructor_append_expr (&result->value.constructor,
5809 gfc_copy_expr (source), NULL);
5811 else if (source->expr_type == EXPR_ARRAY)
5813 int offset, rstride[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS];
5814 gfc_constructor *source_ctor;
5816 gcc_assert (source->rank < GFC_MAX_DIMENSIONS);
5817 gcc_assert (dim >= 0 && dim <= source->rank);
5819 result = gfc_get_array_expr (source->ts.type, source->ts.kind,
5821 if (source->ts.type == BT_DERIVED)
5822 result->ts.u.derived = source->ts.u.derived;
5823 result->rank = source->rank + 1;
5824 result->shape = gfc_get_shape (result->rank);
5826 for (i = 0, j = 0; i < result->rank; ++i)
5829 mpz_init_set (result->shape[i], source->shape[j++]);
5831 mpz_init_set_si (result->shape[i], ncopies);
5833 extent[i] = mpz_get_si (result->shape[i]);
5834 rstride[i] = (i == 0) ? 1 : rstride[i-1] * extent[i-1];
5838 for (source_ctor = gfc_constructor_first (source->value.constructor);
5839 source_ctor; source_ctor = gfc_constructor_next (source_ctor))
5841 for (i = 0; i < ncopies; ++i)
5842 gfc_constructor_insert_expr (&result->value.constructor,
5843 gfc_copy_expr (source_ctor->expr),
5844 NULL, offset + i * rstride[dim]);
5846 offset += (dim == 0 ? ncopies : 1);
5850 /* FIXME: Returning here avoids a regression in array_simplify_1.f90.
5851 Replace NULL with gcc_unreachable() after implementing
5852 gfc_simplify_cshift(). */
5855 if (source->ts.type == BT_CHARACTER)
5856 result->ts.u.cl = source->ts.u.cl;
5863 gfc_simplify_sqrt (gfc_expr *e)
5865 gfc_expr *result = NULL;
5867 if (e->expr_type != EXPR_CONSTANT)
5873 if (mpfr_cmp_si (e->value.real, 0) < 0)
5875 gfc_error ("Argument of SQRT at %L has a negative value",
5877 return &gfc_bad_expr;
5879 result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
5880 mpfr_sqrt (result->value.real, e->value.real, GFC_RND_MODE);
5884 gfc_set_model (e->value.real);
5886 result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
5887 mpc_sqrt (result->value.complex, e->value.complex, GFC_MPC_RND_MODE);
5891 gfc_internal_error ("invalid argument of SQRT at %L", &e->where);
5894 return range_check (result, "SQRT");
5899 gfc_simplify_sum (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
5901 return simplify_transformation (array, dim, mask, 0, gfc_add);
5906 gfc_simplify_tan (gfc_expr *x)
5910 if (x->expr_type != EXPR_CONSTANT)
5913 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
5918 mpfr_tan (result->value.real, x->value.real, GFC_RND_MODE);
5922 mpc_tan (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
5929 return range_check (result, "TAN");
5934 gfc_simplify_tanh (gfc_expr *x)
5938 if (x->expr_type != EXPR_CONSTANT)
5941 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
5946 mpfr_tanh (result->value.real, x->value.real, GFC_RND_MODE);
5950 mpc_tanh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
5957 return range_check (result, "TANH");
5962 gfc_simplify_tiny (gfc_expr *e)
5967 i = gfc_validate_kind (BT_REAL, e->ts.kind, false);
5969 result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
5970 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
5977 gfc_simplify_trailz (gfc_expr *e)
5979 unsigned long tz, bs;
5982 if (e->expr_type != EXPR_CONSTANT)
5985 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
5986 bs = gfc_integer_kinds[i].bit_size;
5987 tz = mpz_scan1 (e->value.integer, 0);
5989 return gfc_get_int_expr (gfc_default_integer_kind,
5990 &e->where, MIN (tz, bs));
5995 gfc_simplify_transfer (gfc_expr *source, gfc_expr *mold, gfc_expr *size)
5998 gfc_expr *mold_element;
6003 unsigned char *buffer;
6004 size_t result_length;
6007 if (!gfc_is_constant_expr (source)
6008 || (gfc_init_expr_flag && !gfc_is_constant_expr (mold))
6009 || !gfc_is_constant_expr (size))
6012 if (gfc_calculate_transfer_sizes (source, mold, size, &source_size,
6013 &result_size, &result_length) == FAILURE)
6016 /* Calculate the size of the source. */
6017 if (source->expr_type == EXPR_ARRAY
6018 && gfc_array_size (source, &tmp) == FAILURE)
6019 gfc_internal_error ("Failure getting length of a constant array.");
6021 /* Create an empty new expression with the appropriate characteristics. */
6022 result = gfc_get_constant_expr (mold->ts.type, mold->ts.kind,
6024 result->ts = mold->ts;
6026 mold_element = mold->expr_type == EXPR_ARRAY
6027 ? gfc_constructor_first (mold->value.constructor)->expr
6030 /* Set result character length, if needed. Note that this needs to be
6031 set even for array expressions, in order to pass this information into
6032 gfc_target_interpret_expr. */
6033 if (result->ts.type == BT_CHARACTER && gfc_is_constant_expr (mold_element))
6034 result->value.character.length = mold_element->value.character.length;
6036 /* Set the number of elements in the result, and determine its size. */
6038 if (mold->expr_type == EXPR_ARRAY || mold->rank || size)
6040 result->expr_type = EXPR_ARRAY;
6042 result->shape = gfc_get_shape (1);
6043 mpz_init_set_ui (result->shape[0], result_length);
6048 /* Allocate the buffer to store the binary version of the source. */
6049 buffer_size = MAX (source_size, result_size);
6050 buffer = (unsigned char*)alloca (buffer_size);
6051 memset (buffer, 0, buffer_size);
6053 /* Now write source to the buffer. */
6054 gfc_target_encode_expr (source, buffer, buffer_size);
6056 /* And read the buffer back into the new expression. */
6057 gfc_target_interpret_expr (buffer, buffer_size, result, false);
6064 gfc_simplify_transpose (gfc_expr *matrix)
6066 int row, matrix_rows, col, matrix_cols;
6069 if (!is_constant_array_expr (matrix))
6072 gcc_assert (matrix->rank == 2);
6074 result = gfc_get_array_expr (matrix->ts.type, matrix->ts.kind,
6077 result->shape = gfc_get_shape (result->rank);
6078 mpz_set (result->shape[0], matrix->shape[1]);
6079 mpz_set (result->shape[1], matrix->shape[0]);
6081 if (matrix->ts.type == BT_CHARACTER)
6082 result->ts.u.cl = matrix->ts.u.cl;
6083 else if (matrix->ts.type == BT_DERIVED)
6084 result->ts.u.derived = matrix->ts.u.derived;
6086 matrix_rows = mpz_get_si (matrix->shape[0]);
6087 matrix_cols = mpz_get_si (matrix->shape[1]);
6088 for (row = 0; row < matrix_rows; ++row)
6089 for (col = 0; col < matrix_cols; ++col)
6091 gfc_expr *e = gfc_constructor_lookup_expr (matrix->value.constructor,
6092 col * matrix_rows + row);
6093 gfc_constructor_insert_expr (&result->value.constructor,
6094 gfc_copy_expr (e), &matrix->where,
6095 row * matrix_cols + col);
6103 gfc_simplify_trim (gfc_expr *e)
6106 int count, i, len, lentrim;
6108 if (e->expr_type != EXPR_CONSTANT)
6111 len = e->value.character.length;
6112 for (count = 0, i = 1; i <= len; ++i)
6114 if (e->value.character.string[len - i] == ' ')
6120 lentrim = len - count;
6122 result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, lentrim);
6123 for (i = 0; i < lentrim; i++)
6124 result->value.character.string[i] = e->value.character.string[i];
6131 gfc_simplify_image_index (gfc_expr *coarray, gfc_expr *sub)
6136 gfc_constructor *sub_cons;
6140 if (!is_constant_array_expr (sub))
6143 /* Follow any component references. */
6144 as = coarray->symtree->n.sym->as;
6145 for (ref = coarray->ref; ref; ref = ref->next)
6146 if (ref->type == REF_COMPONENT)
6149 if (as->type == AS_DEFERRED)
6152 /* "valid sequence of cosubscripts" are required; thus, return 0 unless
6153 the cosubscript addresses the first image. */
6155 sub_cons = gfc_constructor_first (sub->value.constructor);
6158 for (d = 1; d <= as->corank; d++)
6163 gcc_assert (sub_cons != NULL);
6165 ca_bound = simplify_bound_dim (coarray, NULL, d + as->rank, 0, as,
6167 if (ca_bound == NULL)
6170 if (ca_bound == &gfc_bad_expr)
6173 cmp = mpz_cmp (ca_bound->value.integer, sub_cons->expr->value.integer);
6177 gfc_free_expr (ca_bound);
6178 sub_cons = gfc_constructor_next (sub_cons);
6182 first_image = false;
6186 gfc_error ("Out of bounds in IMAGE_INDEX at %L for dimension %d, "
6187 "SUB has %ld and COARRAY lower bound is %ld)",
6189 mpz_get_si (sub_cons->expr->value.integer),
6190 mpz_get_si (ca_bound->value.integer));
6191 gfc_free_expr (ca_bound);
6192 return &gfc_bad_expr;
6195 gfc_free_expr (ca_bound);
6197 /* Check whether upperbound is valid for the multi-images case. */
6200 ca_bound = simplify_bound_dim (coarray, NULL, d + as->rank, 1, as,
6202 if (ca_bound == &gfc_bad_expr)
6205 if (ca_bound && ca_bound->expr_type == EXPR_CONSTANT
6206 && mpz_cmp (ca_bound->value.integer,
6207 sub_cons->expr->value.integer) < 0)
6209 gfc_error ("Out of bounds in IMAGE_INDEX at %L for dimension %d, "
6210 "SUB has %ld and COARRAY upper bound is %ld)",
6212 mpz_get_si (sub_cons->expr->value.integer),
6213 mpz_get_si (ca_bound->value.integer));
6214 gfc_free_expr (ca_bound);
6215 return &gfc_bad_expr;
6219 gfc_free_expr (ca_bound);
6222 sub_cons = gfc_constructor_next (sub_cons);
6225 gcc_assert (sub_cons == NULL);
6227 if (gfc_option.coarray != GFC_FCOARRAY_SINGLE && !first_image)
6230 result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
6231 &gfc_current_locus);
6233 mpz_set_si (result->value.integer, 1);
6235 mpz_set_si (result->value.integer, 0);
6242 gfc_simplify_this_image (gfc_expr *coarray, gfc_expr *dim)
6244 if (gfc_option.coarray != GFC_FCOARRAY_SINGLE)
6247 if (coarray == NULL)
6250 /* FIXME: gfc_current_locus is wrong. */
6251 result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
6252 &gfc_current_locus);
6253 mpz_set_si (result->value.integer, 1);
6257 /* For -fcoarray=single, this_image(A) is the same as lcobound(A). */
6258 return simplify_cobound (coarray, dim, NULL, 0);
6263 gfc_simplify_ubound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
6265 return simplify_bound (array, dim, kind, 1);
6269 gfc_simplify_ucobound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
6271 return simplify_cobound (array, dim, kind, 1);
6276 gfc_simplify_unpack (gfc_expr *vector, gfc_expr *mask, gfc_expr *field)
6278 gfc_expr *result, *e;
6279 gfc_constructor *vector_ctor, *mask_ctor, *field_ctor;
6281 if (!is_constant_array_expr (vector)
6282 || !is_constant_array_expr (mask)
6283 || (!gfc_is_constant_expr (field)
6284 && !is_constant_array_expr(field)))
6287 result = gfc_get_array_expr (vector->ts.type, vector->ts.kind,
6289 if (vector->ts.type == BT_DERIVED)
6290 result->ts.u.derived = vector->ts.u.derived;
6291 result->rank = mask->rank;
6292 result->shape = gfc_copy_shape (mask->shape, mask->rank);
6294 if (vector->ts.type == BT_CHARACTER)
6295 result->ts.u.cl = vector->ts.u.cl;
6297 vector_ctor = gfc_constructor_first (vector->value.constructor);
6298 mask_ctor = gfc_constructor_first (mask->value.constructor);
6300 = field->expr_type == EXPR_ARRAY
6301 ? gfc_constructor_first (field->value.constructor)
6306 if (mask_ctor->expr->value.logical)
6308 gcc_assert (vector_ctor);
6309 e = gfc_copy_expr (vector_ctor->expr);
6310 vector_ctor = gfc_constructor_next (vector_ctor);
6312 else if (field->expr_type == EXPR_ARRAY)
6313 e = gfc_copy_expr (field_ctor->expr);
6315 e = gfc_copy_expr (field);
6317 gfc_constructor_append_expr (&result->value.constructor, e, NULL);
6319 mask_ctor = gfc_constructor_next (mask_ctor);
6320 field_ctor = gfc_constructor_next (field_ctor);
6328 gfc_simplify_verify (gfc_expr *s, gfc_expr *set, gfc_expr *b, gfc_expr *kind)
6332 size_t index, len, lenset;
6334 int k = get_kind (BT_INTEGER, kind, "VERIFY", gfc_default_integer_kind);
6337 return &gfc_bad_expr;
6339 if (s->expr_type != EXPR_CONSTANT || set->expr_type != EXPR_CONSTANT
6340 || ( b != NULL && b->expr_type != EXPR_CONSTANT))
6343 if (b != NULL && b->value.logical != 0)
6348 result = gfc_get_constant_expr (BT_INTEGER, k, &s->where);
6350 len = s->value.character.length;
6351 lenset = set->value.character.length;
6355 mpz_set_ui (result->value.integer, 0);
6363 mpz_set_ui (result->value.integer, 1);
6367 index = wide_strspn (s->value.character.string,
6368 set->value.character.string) + 1;
6377 mpz_set_ui (result->value.integer, len);
6380 for (index = len; index > 0; index --)
6382 for (i = 0; i < lenset; i++)
6384 if (s->value.character.string[index - 1]
6385 == set->value.character.string[i])
6393 mpz_set_ui (result->value.integer, index);
6399 gfc_simplify_xor (gfc_expr *x, gfc_expr *y)
6404 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
6407 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
6412 result = gfc_get_constant_expr (BT_INTEGER, kind, &x->where);
6413 mpz_xor (result->value.integer, x->value.integer, y->value.integer);
6414 return range_check (result, "XOR");
6417 return gfc_get_logical_expr (kind, &x->where,
6418 (x->value.logical && !y->value.logical)
6419 || (!x->value.logical && y->value.logical));
6427 /****************** Constant simplification *****************/
6429 /* Master function to convert one constant to another. While this is
6430 used as a simplification function, it requires the destination type
6431 and kind information which is supplied by a special case in
6435 gfc_convert_constant (gfc_expr *e, bt type, int kind)
6437 gfc_expr *g, *result, *(*f) (gfc_expr *, int);
6452 f = gfc_int2complex;
6472 f = gfc_real2complex;
6483 f = gfc_complex2int;
6486 f = gfc_complex2real;
6489 f = gfc_complex2complex;
6515 f = gfc_hollerith2int;
6519 f = gfc_hollerith2real;
6523 f = gfc_hollerith2complex;
6527 f = gfc_hollerith2character;
6531 f = gfc_hollerith2logical;
6541 gfc_internal_error ("gfc_convert_constant(): Unexpected type");
6546 switch (e->expr_type)
6549 result = f (e, kind);
6551 return &gfc_bad_expr;
6555 if (!gfc_is_constant_expr (e))
6558 result = gfc_get_array_expr (type, kind, &e->where);
6559 result->shape = gfc_copy_shape (e->shape, e->rank);
6560 result->rank = e->rank;
6562 for (c = gfc_constructor_first (e->value.constructor);
6563 c; c = gfc_constructor_next (c))
6566 if (c->iterator == NULL)
6567 tmp = f (c->expr, kind);
6570 g = gfc_convert_constant (c->expr, type, kind);
6571 if (g == &gfc_bad_expr)
6573 gfc_free_expr (result);
6581 gfc_free_expr (result);
6585 gfc_constructor_append_expr (&result->value.constructor,
6599 /* Function for converting character constants. */
6601 gfc_convert_char_constant (gfc_expr *e, bt type ATTRIBUTE_UNUSED, int kind)
6606 if (!gfc_is_constant_expr (e))
6609 if (e->expr_type == EXPR_CONSTANT)
6611 /* Simple case of a scalar. */
6612 result = gfc_get_constant_expr (BT_CHARACTER, kind, &e->where);
6614 return &gfc_bad_expr;
6616 result->value.character.length = e->value.character.length;
6617 result->value.character.string
6618 = gfc_get_wide_string (e->value.character.length + 1);
6619 memcpy (result->value.character.string, e->value.character.string,
6620 (e->value.character.length + 1) * sizeof (gfc_char_t));
6622 /* Check we only have values representable in the destination kind. */
6623 for (i = 0; i < result->value.character.length; i++)
6624 if (!gfc_check_character_range (result->value.character.string[i],
6627 gfc_error ("Character '%s' in string at %L cannot be converted "
6628 "into character kind %d",
6629 gfc_print_wide_char (result->value.character.string[i]),
6631 return &gfc_bad_expr;
6636 else if (e->expr_type == EXPR_ARRAY)
6638 /* For an array constructor, we convert each constructor element. */
6641 result = gfc_get_array_expr (type, kind, &e->where);
6642 result->shape = gfc_copy_shape (e->shape, e->rank);
6643 result->rank = e->rank;
6644 result->ts.u.cl = e->ts.u.cl;
6646 for (c = gfc_constructor_first (e->value.constructor);
6647 c; c = gfc_constructor_next (c))
6649 gfc_expr *tmp = gfc_convert_char_constant (c->expr, type, kind);
6650 if (tmp == &gfc_bad_expr)
6652 gfc_free_expr (result);
6653 return &gfc_bad_expr;
6658 gfc_free_expr (result);
6662 gfc_constructor_append_expr (&result->value.constructor,
6674 gfc_simplify_compiler_options (void)
6679 str = gfc_get_option_string ();
6680 result = gfc_get_character_expr (gfc_default_character_kind,
6681 &gfc_current_locus, str, strlen (str));
6688 gfc_simplify_compiler_version (void)
6693 len = strlen ("GCC version ") + strlen (version_string);
6694 buffer = XALLOCAVEC (char, len + 1);
6695 snprintf (buffer, len + 1, "GCC version %s", version_string);
6696 return gfc_get_character_expr (gfc_default_character_kind,
6697 &gfc_current_locus, buffer, len);