Daily bump.
[official-gcc.git] / gcc / fortran / simplify.c
blob90067b6bbe6ad358ba82fada9a971be1eebea5e3
1 /* Simplify intrinsic functions at compile-time.
2 Copyright (C) 2000-2021 Free Software Foundation, Inc.
3 Contributed by Andy Vaught & Katherine Holcomb
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h" /* For BITS_PER_UNIT. */
25 #include "gfortran.h"
26 #include "arith.h"
27 #include "intrinsic.h"
28 #include "match.h"
29 #include "target-memory.h"
30 #include "constructor.h"
31 #include "version.h" /* For version_string. */
33 /* Prototypes. */
35 static int min_max_choose (gfc_expr *, gfc_expr *, int, bool back_val = false);
37 gfc_expr gfc_bad_expr;
39 static gfc_expr *simplify_size (gfc_expr *, gfc_expr *, int);
42 /* Note that 'simplification' is not just transforming expressions.
43 For functions that are not simplified at compile time, range
44 checking is done if possible.
46 The return convention is that each simplification function returns:
48 A new expression node corresponding to the simplified arguments.
49 The original arguments are destroyed by the caller, and must not
50 be a part of the new expression.
52 NULL pointer indicating that no simplification was possible and
53 the original expression should remain intact.
55 An expression pointer to gfc_bad_expr (a static placeholder)
56 indicating that some error has prevented simplification. The
57 error is generated within the function and should be propagated
58 upwards
60 By the time a simplification function gets control, it has been
61 decided that the function call is really supposed to be the
62 intrinsic. No type checking is strictly necessary, since only
63 valid types will be passed on. On the other hand, a simplification
64 subroutine may have to look at the type of an argument as part of
65 its processing.
67 Array arguments are only passed to these subroutines that implement
68 the simplification of transformational intrinsics.
70 The functions in this file don't have much comment with them, but
71 everything is reasonably straight-forward. The Standard, chapter 13
72 is the best comment you'll find for this file anyway. */
74 /* Range checks an expression node. If all goes well, returns the
75 node, otherwise returns &gfc_bad_expr and frees the node. */
77 static gfc_expr *
78 range_check (gfc_expr *result, const char *name)
80 if (result == NULL)
81 return &gfc_bad_expr;
83 if (result->expr_type != EXPR_CONSTANT)
84 return result;
86 switch (gfc_range_check (result))
88 case ARITH_OK:
89 return result;
91 case ARITH_OVERFLOW:
92 gfc_error ("Result of %s overflows its kind at %L", name,
93 &result->where);
94 break;
96 case ARITH_UNDERFLOW:
97 gfc_error ("Result of %s underflows its kind at %L", name,
98 &result->where);
99 break;
101 case ARITH_NAN:
102 gfc_error ("Result of %s is NaN at %L", name, &result->where);
103 break;
105 default:
106 gfc_error ("Result of %s gives range error for its kind at %L", name,
107 &result->where);
108 break;
111 gfc_free_expr (result);
112 return &gfc_bad_expr;
116 /* A helper function that gets an optional and possibly missing
117 kind parameter. Returns the kind, -1 if something went wrong. */
119 static int
120 get_kind (bt type, gfc_expr *k, const char *name, int default_kind)
122 int kind;
124 if (k == NULL)
125 return default_kind;
127 if (k->expr_type != EXPR_CONSTANT)
129 gfc_error ("KIND parameter of %s at %L must be an initialization "
130 "expression", name, &k->where);
131 return -1;
134 if (gfc_extract_int (k, &kind)
135 || gfc_validate_kind (type, kind, true) < 0)
137 gfc_error ("Invalid KIND parameter of %s at %L", name, &k->where);
138 return -1;
141 return kind;
145 /* Converts an mpz_t signed variable into an unsigned one, assuming
146 two's complement representations and a binary width of bitsize.
147 The conversion is a no-op unless x is negative; otherwise, it can
148 be accomplished by masking out the high bits. */
150 static void
151 convert_mpz_to_unsigned (mpz_t x, int bitsize)
153 mpz_t mask;
155 if (mpz_sgn (x) < 0)
157 /* Confirm that no bits above the signed range are unset if we
158 are doing range checking. */
159 if (flag_range_check != 0)
160 gcc_assert (mpz_scan0 (x, bitsize-1) == ULONG_MAX);
162 mpz_init_set_ui (mask, 1);
163 mpz_mul_2exp (mask, mask, bitsize);
164 mpz_sub_ui (mask, mask, 1);
166 mpz_and (x, x, mask);
168 mpz_clear (mask);
170 else
172 /* Confirm that no bits above the signed range are set if we
173 are doing range checking. */
174 if (flag_range_check != 0)
175 gcc_assert (mpz_scan1 (x, bitsize-1) == ULONG_MAX);
180 /* Converts an mpz_t unsigned variable into a signed one, assuming
181 two's complement representations and a binary width of bitsize.
182 If the bitsize-1 bit is set, this is taken as a sign bit and
183 the number is converted to the corresponding negative number. */
185 void
186 gfc_convert_mpz_to_signed (mpz_t x, int bitsize)
188 mpz_t mask;
190 /* Confirm that no bits above the unsigned range are set if we are
191 doing range checking. */
192 if (flag_range_check != 0)
193 gcc_assert (mpz_scan1 (x, bitsize) == ULONG_MAX);
195 if (mpz_tstbit (x, bitsize - 1) == 1)
197 mpz_init_set_ui (mask, 1);
198 mpz_mul_2exp (mask, mask, bitsize);
199 mpz_sub_ui (mask, mask, 1);
201 /* We negate the number by hand, zeroing the high bits, that is
202 make it the corresponding positive number, and then have it
203 negated by GMP, giving the correct representation of the
204 negative number. */
205 mpz_com (x, x);
206 mpz_add_ui (x, x, 1);
207 mpz_and (x, x, mask);
209 mpz_neg (x, x);
211 mpz_clear (mask);
216 /* Test that the expression is a constant array, simplifying if
217 we are dealing with a parameter array. */
219 static bool
220 is_constant_array_expr (gfc_expr *e)
222 gfc_constructor *c;
223 bool array_OK = true;
224 mpz_t size;
226 if (e == NULL)
227 return true;
229 if (e->expr_type == EXPR_VARIABLE && e->rank > 0
230 && e->symtree->n.sym->attr.flavor == FL_PARAMETER)
231 gfc_simplify_expr (e, 1);
233 if (e->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (e))
234 return false;
236 for (c = gfc_constructor_first (e->value.constructor);
237 c; c = gfc_constructor_next (c))
238 if (c->expr->expr_type != EXPR_CONSTANT
239 && c->expr->expr_type != EXPR_STRUCTURE)
241 array_OK = false;
242 break;
245 /* Check and expand the constructor. */
246 if (!array_OK && gfc_init_expr_flag && e->rank == 1)
248 array_OK = gfc_reduce_init_expr (e);
249 /* gfc_reduce_init_expr resets the flag. */
250 gfc_init_expr_flag = true;
252 else
253 return array_OK;
255 /* Recheck to make sure that any EXPR_ARRAYs have gone. */
256 for (c = gfc_constructor_first (e->value.constructor);
257 c; c = gfc_constructor_next (c))
258 if (c->expr->expr_type != EXPR_CONSTANT
259 && c->expr->expr_type != EXPR_STRUCTURE)
260 return false;
262 /* Make sure that the array has a valid shape. */
263 if (e->shape == NULL && e->rank == 1)
265 if (!gfc_array_size(e, &size))
266 return false;
267 e->shape = gfc_get_shape (1);
268 mpz_init_set (e->shape[0], size);
269 mpz_clear (size);
272 return array_OK;
275 /* Test for a size zero array. */
276 bool
277 gfc_is_size_zero_array (gfc_expr *array)
280 if (array->rank == 0)
281 return false;
283 if (array->expr_type == EXPR_VARIABLE && array->rank > 0
284 && array->symtree->n.sym->attr.flavor == FL_PARAMETER
285 && array->shape != NULL)
287 for (int i = 0; i < array->rank; i++)
288 if (mpz_cmp_si (array->shape[i], 0) <= 0)
289 return true;
291 return false;
294 if (array->expr_type == EXPR_ARRAY)
295 return array->value.constructor == NULL;
297 return false;
301 /* Initialize a transformational result expression with a given value. */
303 static void
304 init_result_expr (gfc_expr *e, int init, gfc_expr *array)
306 if (e && e->expr_type == EXPR_ARRAY)
308 gfc_constructor *ctor = gfc_constructor_first (e->value.constructor);
309 while (ctor)
311 init_result_expr (ctor->expr, init, array);
312 ctor = gfc_constructor_next (ctor);
315 else if (e && e->expr_type == EXPR_CONSTANT)
317 int i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
318 HOST_WIDE_INT length;
319 gfc_char_t *string;
321 switch (e->ts.type)
323 case BT_LOGICAL:
324 e->value.logical = (init ? 1 : 0);
325 break;
327 case BT_INTEGER:
328 if (init == INT_MIN)
329 mpz_set (e->value.integer, gfc_integer_kinds[i].min_int);
330 else if (init == INT_MAX)
331 mpz_set (e->value.integer, gfc_integer_kinds[i].huge);
332 else
333 mpz_set_si (e->value.integer, init);
334 break;
336 case BT_REAL:
337 if (init == INT_MIN)
339 mpfr_set (e->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
340 mpfr_neg (e->value.real, e->value.real, GFC_RND_MODE);
342 else if (init == INT_MAX)
343 mpfr_set (e->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
344 else
345 mpfr_set_si (e->value.real, init, GFC_RND_MODE);
346 break;
348 case BT_COMPLEX:
349 mpc_set_si (e->value.complex, init, GFC_MPC_RND_MODE);
350 break;
352 case BT_CHARACTER:
353 if (init == INT_MIN)
355 gfc_expr *len = gfc_simplify_len (array, NULL);
356 gfc_extract_hwi (len, &length);
357 string = gfc_get_wide_string (length + 1);
358 gfc_wide_memset (string, 0, length);
360 else if (init == INT_MAX)
362 gfc_expr *len = gfc_simplify_len (array, NULL);
363 gfc_extract_hwi (len, &length);
364 string = gfc_get_wide_string (length + 1);
365 gfc_wide_memset (string, 255, length);
367 else
369 length = 0;
370 string = gfc_get_wide_string (1);
373 string[length] = '\0';
374 e->value.character.length = length;
375 e->value.character.string = string;
376 break;
378 default:
379 gcc_unreachable();
382 else
383 gcc_unreachable();
387 /* Helper function for gfc_simplify_dot_product() and gfc_simplify_matmul;
388 if conj_a is true, the matrix_a is complex conjugated. */
390 static gfc_expr *
391 compute_dot_product (gfc_expr *matrix_a, int stride_a, int offset_a,
392 gfc_expr *matrix_b, int stride_b, int offset_b,
393 bool conj_a)
395 gfc_expr *result, *a, *b, *c;
397 /* Set result to an INTEGER(1) 0 for numeric types and .false. for
398 LOGICAL. Mixed-mode math in the loop will promote result to the
399 correct type and kind. */
400 if (matrix_a->ts.type == BT_LOGICAL)
401 result = gfc_get_logical_expr (gfc_default_logical_kind, NULL, false);
402 else
403 result = gfc_get_int_expr (1, NULL, 0);
404 result->where = matrix_a->where;
406 a = gfc_constructor_lookup_expr (matrix_a->value.constructor, offset_a);
407 b = gfc_constructor_lookup_expr (matrix_b->value.constructor, offset_b);
408 while (a && b)
410 /* Copying of expressions is required as operands are free'd
411 by the gfc_arith routines. */
412 switch (result->ts.type)
414 case BT_LOGICAL:
415 result = gfc_or (result,
416 gfc_and (gfc_copy_expr (a),
417 gfc_copy_expr (b)));
418 break;
420 case BT_INTEGER:
421 case BT_REAL:
422 case BT_COMPLEX:
423 if (conj_a && a->ts.type == BT_COMPLEX)
424 c = gfc_simplify_conjg (a);
425 else
426 c = gfc_copy_expr (a);
427 result = gfc_add (result, gfc_multiply (c, gfc_copy_expr (b)));
428 break;
430 default:
431 gcc_unreachable();
434 offset_a += stride_a;
435 a = gfc_constructor_lookup_expr (matrix_a->value.constructor, offset_a);
437 offset_b += stride_b;
438 b = gfc_constructor_lookup_expr (matrix_b->value.constructor, offset_b);
441 return result;
445 /* Build a result expression for transformational intrinsics,
446 depending on DIM. */
448 static gfc_expr *
449 transformational_result (gfc_expr *array, gfc_expr *dim, bt type,
450 int kind, locus* where)
452 gfc_expr *result;
453 int i, nelem;
455 if (!dim || array->rank == 1)
456 return gfc_get_constant_expr (type, kind, where);
458 result = gfc_get_array_expr (type, kind, where);
459 result->shape = gfc_copy_shape_excluding (array->shape, array->rank, dim);
460 result->rank = array->rank - 1;
462 /* gfc_array_size() would count the number of elements in the constructor,
463 we have not built those yet. */
464 nelem = 1;
465 for (i = 0; i < result->rank; ++i)
466 nelem *= mpz_get_ui (result->shape[i]);
468 for (i = 0; i < nelem; ++i)
470 gfc_constructor_append_expr (&result->value.constructor,
471 gfc_get_constant_expr (type, kind, where),
472 NULL);
475 return result;
479 typedef gfc_expr* (*transformational_op)(gfc_expr*, gfc_expr*);
481 /* Wrapper function, implements 'op1 += 1'. Only called if MASK
482 of COUNT intrinsic is .TRUE..
484 Interface and implementation mimics arith functions as
485 gfc_add, gfc_multiply, etc. */
487 static gfc_expr *
488 gfc_count (gfc_expr *op1, gfc_expr *op2)
490 gfc_expr *result;
492 gcc_assert (op1->ts.type == BT_INTEGER);
493 gcc_assert (op2->ts.type == BT_LOGICAL);
494 gcc_assert (op2->value.logical);
496 result = gfc_copy_expr (op1);
497 mpz_add_ui (result->value.integer, result->value.integer, 1);
499 gfc_free_expr (op1);
500 gfc_free_expr (op2);
501 return result;
505 /* Transforms an ARRAY with operation OP, according to MASK, to a
506 scalar RESULT. E.g. called if
508 REAL, PARAMETER :: array(n, m) = ...
509 REAL, PARAMETER :: s = SUM(array)
511 where OP == gfc_add(). */
513 static gfc_expr *
514 simplify_transformation_to_scalar (gfc_expr *result, gfc_expr *array, gfc_expr *mask,
515 transformational_op op)
517 gfc_expr *a, *m;
518 gfc_constructor *array_ctor, *mask_ctor;
520 /* Shortcut for constant .FALSE. MASK. */
521 if (mask
522 && mask->expr_type == EXPR_CONSTANT
523 && !mask->value.logical)
524 return result;
526 array_ctor = gfc_constructor_first (array->value.constructor);
527 mask_ctor = NULL;
528 if (mask && mask->expr_type == EXPR_ARRAY)
529 mask_ctor = gfc_constructor_first (mask->value.constructor);
531 while (array_ctor)
533 a = array_ctor->expr;
534 array_ctor = gfc_constructor_next (array_ctor);
536 /* A constant MASK equals .TRUE. here and can be ignored. */
537 if (mask_ctor)
539 m = mask_ctor->expr;
540 mask_ctor = gfc_constructor_next (mask_ctor);
541 if (!m->value.logical)
542 continue;
545 result = op (result, gfc_copy_expr (a));
546 if (!result)
547 return result;
550 return result;
553 /* Transforms an ARRAY with operation OP, according to MASK, to an
554 array RESULT. E.g. called if
556 REAL, PARAMETER :: array(n, m) = ...
557 REAL, PARAMETER :: s(n) = PROD(array, DIM=1)
559 where OP == gfc_multiply().
560 The result might be post processed using post_op. */
562 static gfc_expr *
563 simplify_transformation_to_array (gfc_expr *result, gfc_expr *array, gfc_expr *dim,
564 gfc_expr *mask, transformational_op op,
565 transformational_op post_op)
567 mpz_t size;
568 int done, i, n, arraysize, resultsize, dim_index, dim_extent, dim_stride;
569 gfc_expr **arrayvec, **resultvec, **base, **src, **dest;
570 gfc_constructor *array_ctor, *mask_ctor, *result_ctor;
572 int count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS],
573 sstride[GFC_MAX_DIMENSIONS], dstride[GFC_MAX_DIMENSIONS],
574 tmpstride[GFC_MAX_DIMENSIONS];
576 /* Shortcut for constant .FALSE. MASK. */
577 if (mask
578 && mask->expr_type == EXPR_CONSTANT
579 && !mask->value.logical)
580 return result;
582 /* Build an indexed table for array element expressions to minimize
583 linked-list traversal. Masked elements are set to NULL. */
584 gfc_array_size (array, &size);
585 arraysize = mpz_get_ui (size);
586 mpz_clear (size);
588 arrayvec = XCNEWVEC (gfc_expr*, arraysize);
590 array_ctor = gfc_constructor_first (array->value.constructor);
591 mask_ctor = NULL;
592 if (mask && mask->expr_type == EXPR_ARRAY)
593 mask_ctor = gfc_constructor_first (mask->value.constructor);
595 for (i = 0; i < arraysize; ++i)
597 arrayvec[i] = array_ctor->expr;
598 array_ctor = gfc_constructor_next (array_ctor);
600 if (mask_ctor)
602 if (!mask_ctor->expr->value.logical)
603 arrayvec[i] = NULL;
605 mask_ctor = gfc_constructor_next (mask_ctor);
609 /* Same for the result expression. */
610 gfc_array_size (result, &size);
611 resultsize = mpz_get_ui (size);
612 mpz_clear (size);
614 resultvec = XCNEWVEC (gfc_expr*, resultsize);
615 result_ctor = gfc_constructor_first (result->value.constructor);
616 for (i = 0; i < resultsize; ++i)
618 resultvec[i] = result_ctor->expr;
619 result_ctor = gfc_constructor_next (result_ctor);
622 gfc_extract_int (dim, &dim_index);
623 dim_index -= 1; /* zero-base index */
624 dim_extent = 0;
625 dim_stride = 0;
627 for (i = 0, n = 0; i < array->rank; ++i)
629 count[i] = 0;
630 tmpstride[i] = (i == 0) ? 1 : tmpstride[i-1] * mpz_get_si (array->shape[i-1]);
631 if (i == dim_index)
633 dim_extent = mpz_get_si (array->shape[i]);
634 dim_stride = tmpstride[i];
635 continue;
638 extent[n] = mpz_get_si (array->shape[i]);
639 sstride[n] = tmpstride[i];
640 dstride[n] = (n == 0) ? 1 : dstride[n-1] * extent[n-1];
641 n += 1;
644 done = resultsize <= 0;
645 base = arrayvec;
646 dest = resultvec;
647 while (!done)
649 for (src = base, n = 0; n < dim_extent; src += dim_stride, ++n)
650 if (*src)
651 *dest = op (*dest, gfc_copy_expr (*src));
653 if (post_op)
654 *dest = post_op (*dest, *dest);
656 count[0]++;
657 base += sstride[0];
658 dest += dstride[0];
660 n = 0;
661 while (!done && count[n] == extent[n])
663 count[n] = 0;
664 base -= sstride[n] * extent[n];
665 dest -= dstride[n] * extent[n];
667 n++;
668 if (n < result->rank)
670 /* If the nested loop is unrolled GFC_MAX_DIMENSIONS
671 times, we'd warn for the last iteration, because the
672 array index will have already been incremented to the
673 array sizes, and we can't tell that this must make
674 the test against result->rank false, because ranks
675 must not exceed GFC_MAX_DIMENSIONS. */
676 GCC_DIAGNOSTIC_PUSH_IGNORED (-Warray-bounds)
677 count[n]++;
678 base += sstride[n];
679 dest += dstride[n];
680 GCC_DIAGNOSTIC_POP
682 else
683 done = true;
687 /* Place updated expression in result constructor. */
688 result_ctor = gfc_constructor_first (result->value.constructor);
689 for (i = 0; i < resultsize; ++i)
691 result_ctor->expr = resultvec[i];
692 result_ctor = gfc_constructor_next (result_ctor);
695 free (arrayvec);
696 free (resultvec);
697 return result;
701 static gfc_expr *
702 simplify_transformation (gfc_expr *array, gfc_expr *dim, gfc_expr *mask,
703 int init_val, transformational_op op)
705 gfc_expr *result;
706 bool size_zero;
708 size_zero = gfc_is_size_zero_array (array);
710 if (!(is_constant_array_expr (array) || size_zero)
711 || !gfc_is_constant_expr (dim))
712 return NULL;
714 if (mask
715 && !is_constant_array_expr (mask)
716 && mask->expr_type != EXPR_CONSTANT)
717 return NULL;
719 result = transformational_result (array, dim, array->ts.type,
720 array->ts.kind, &array->where);
721 init_result_expr (result, init_val, array);
723 if (size_zero)
724 return result;
726 return !dim || array->rank == 1 ?
727 simplify_transformation_to_scalar (result, array, mask, op) :
728 simplify_transformation_to_array (result, array, dim, mask, op, NULL);
732 /********************** Simplification functions *****************************/
734 gfc_expr *
735 gfc_simplify_abs (gfc_expr *e)
737 gfc_expr *result;
739 if (e->expr_type != EXPR_CONSTANT)
740 return NULL;
742 switch (e->ts.type)
744 case BT_INTEGER:
745 result = gfc_get_constant_expr (BT_INTEGER, e->ts.kind, &e->where);
746 mpz_abs (result->value.integer, e->value.integer);
747 return range_check (result, "IABS");
749 case BT_REAL:
750 result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
751 mpfr_abs (result->value.real, e->value.real, GFC_RND_MODE);
752 return range_check (result, "ABS");
754 case BT_COMPLEX:
755 gfc_set_model_kind (e->ts.kind);
756 result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
757 mpc_abs (result->value.real, e->value.complex, GFC_RND_MODE);
758 return range_check (result, "CABS");
760 default:
761 gfc_internal_error ("gfc_simplify_abs(): Bad type");
766 static gfc_expr *
767 simplify_achar_char (gfc_expr *e, gfc_expr *k, const char *name, bool ascii)
769 gfc_expr *result;
770 int kind;
771 bool too_large = false;
773 if (e->expr_type != EXPR_CONSTANT)
774 return NULL;
776 kind = get_kind (BT_CHARACTER, k, name, gfc_default_character_kind);
777 if (kind == -1)
778 return &gfc_bad_expr;
780 if (mpz_cmp_si (e->value.integer, 0) < 0)
782 gfc_error ("Argument of %s function at %L is negative", name,
783 &e->where);
784 return &gfc_bad_expr;
787 if (ascii && warn_surprising && mpz_cmp_si (e->value.integer, 127) > 0)
788 gfc_warning (OPT_Wsurprising,
789 "Argument of %s function at %L outside of range [0,127]",
790 name, &e->where);
792 if (kind == 1 && mpz_cmp_si (e->value.integer, 255) > 0)
793 too_large = true;
794 else if (kind == 4)
796 mpz_t t;
797 mpz_init_set_ui (t, 2);
798 mpz_pow_ui (t, t, 32);
799 mpz_sub_ui (t, t, 1);
800 if (mpz_cmp (e->value.integer, t) > 0)
801 too_large = true;
802 mpz_clear (t);
805 if (too_large)
807 gfc_error ("Argument of %s function at %L is too large for the "
808 "collating sequence of kind %d", name, &e->where, kind);
809 return &gfc_bad_expr;
812 result = gfc_get_character_expr (kind, &e->where, NULL, 1);
813 result->value.character.string[0] = mpz_get_ui (e->value.integer);
815 return result;
820 /* We use the processor's collating sequence, because all
821 systems that gfortran currently works on are ASCII. */
823 gfc_expr *
824 gfc_simplify_achar (gfc_expr *e, gfc_expr *k)
826 return simplify_achar_char (e, k, "ACHAR", true);
830 gfc_expr *
831 gfc_simplify_acos (gfc_expr *x)
833 gfc_expr *result;
835 if (x->expr_type != EXPR_CONSTANT)
836 return NULL;
838 switch (x->ts.type)
840 case BT_REAL:
841 if (mpfr_cmp_si (x->value.real, 1) > 0
842 || mpfr_cmp_si (x->value.real, -1) < 0)
844 gfc_error ("Argument of ACOS at %L must be between -1 and 1",
845 &x->where);
846 return &gfc_bad_expr;
848 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
849 mpfr_acos (result->value.real, x->value.real, GFC_RND_MODE);
850 break;
852 case BT_COMPLEX:
853 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
854 mpc_acos (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
855 break;
857 default:
858 gfc_internal_error ("in gfc_simplify_acos(): Bad type");
861 return range_check (result, "ACOS");
864 gfc_expr *
865 gfc_simplify_acosh (gfc_expr *x)
867 gfc_expr *result;
869 if (x->expr_type != EXPR_CONSTANT)
870 return NULL;
872 switch (x->ts.type)
874 case BT_REAL:
875 if (mpfr_cmp_si (x->value.real, 1) < 0)
877 gfc_error ("Argument of ACOSH at %L must not be less than 1",
878 &x->where);
879 return &gfc_bad_expr;
882 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
883 mpfr_acosh (result->value.real, x->value.real, GFC_RND_MODE);
884 break;
886 case BT_COMPLEX:
887 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
888 mpc_acosh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
889 break;
891 default:
892 gfc_internal_error ("in gfc_simplify_acosh(): Bad type");
895 return range_check (result, "ACOSH");
898 gfc_expr *
899 gfc_simplify_adjustl (gfc_expr *e)
901 gfc_expr *result;
902 int count, i, len;
903 gfc_char_t ch;
905 if (e->expr_type != EXPR_CONSTANT)
906 return NULL;
908 len = e->value.character.length;
910 for (count = 0, i = 0; i < len; ++i)
912 ch = e->value.character.string[i];
913 if (ch != ' ')
914 break;
915 ++count;
918 result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, len);
919 for (i = 0; i < len - count; ++i)
920 result->value.character.string[i] = e->value.character.string[count + i];
922 return result;
926 gfc_expr *
927 gfc_simplify_adjustr (gfc_expr *e)
929 gfc_expr *result;
930 int count, i, len;
931 gfc_char_t ch;
933 if (e->expr_type != EXPR_CONSTANT)
934 return NULL;
936 len = e->value.character.length;
938 for (count = 0, i = len - 1; i >= 0; --i)
940 ch = e->value.character.string[i];
941 if (ch != ' ')
942 break;
943 ++count;
946 result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, len);
947 for (i = 0; i < count; ++i)
948 result->value.character.string[i] = ' ';
950 for (i = count; i < len; ++i)
951 result->value.character.string[i] = e->value.character.string[i - count];
953 return result;
957 gfc_expr *
958 gfc_simplify_aimag (gfc_expr *e)
960 gfc_expr *result;
962 if (e->expr_type != EXPR_CONSTANT)
963 return NULL;
965 result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
966 mpfr_set (result->value.real, mpc_imagref (e->value.complex), GFC_RND_MODE);
968 return range_check (result, "AIMAG");
972 gfc_expr *
973 gfc_simplify_aint (gfc_expr *e, gfc_expr *k)
975 gfc_expr *rtrunc, *result;
976 int kind;
978 kind = get_kind (BT_REAL, k, "AINT", e->ts.kind);
979 if (kind == -1)
980 return &gfc_bad_expr;
982 if (e->expr_type != EXPR_CONSTANT)
983 return NULL;
985 rtrunc = gfc_copy_expr (e);
986 mpfr_trunc (rtrunc->value.real, e->value.real);
988 result = gfc_real2real (rtrunc, kind);
990 gfc_free_expr (rtrunc);
992 return range_check (result, "AINT");
996 gfc_expr *
997 gfc_simplify_all (gfc_expr *mask, gfc_expr *dim)
999 return simplify_transformation (mask, dim, NULL, true, gfc_and);
1003 gfc_expr *
1004 gfc_simplify_dint (gfc_expr *e)
1006 gfc_expr *rtrunc, *result;
1008 if (e->expr_type != EXPR_CONSTANT)
1009 return NULL;
1011 rtrunc = gfc_copy_expr (e);
1012 mpfr_trunc (rtrunc->value.real, e->value.real);
1014 result = gfc_real2real (rtrunc, gfc_default_double_kind);
1016 gfc_free_expr (rtrunc);
1018 return range_check (result, "DINT");
1022 gfc_expr *
1023 gfc_simplify_dreal (gfc_expr *e)
1025 gfc_expr *result = NULL;
1027 if (e->expr_type != EXPR_CONSTANT)
1028 return NULL;
1030 result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
1031 mpc_real (result->value.real, e->value.complex, GFC_RND_MODE);
1033 return range_check (result, "DREAL");
1037 gfc_expr *
1038 gfc_simplify_anint (gfc_expr *e, gfc_expr *k)
1040 gfc_expr *result;
1041 int kind;
1043 kind = get_kind (BT_REAL, k, "ANINT", e->ts.kind);
1044 if (kind == -1)
1045 return &gfc_bad_expr;
1047 if (e->expr_type != EXPR_CONSTANT)
1048 return NULL;
1050 result = gfc_get_constant_expr (e->ts.type, kind, &e->where);
1051 mpfr_round (result->value.real, e->value.real);
1053 return range_check (result, "ANINT");
1057 gfc_expr *
1058 gfc_simplify_and (gfc_expr *x, gfc_expr *y)
1060 gfc_expr *result;
1061 int kind;
1063 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1064 return NULL;
1066 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
1068 switch (x->ts.type)
1070 case BT_INTEGER:
1071 result = gfc_get_constant_expr (BT_INTEGER, kind, &x->where);
1072 mpz_and (result->value.integer, x->value.integer, y->value.integer);
1073 return range_check (result, "AND");
1075 case BT_LOGICAL:
1076 return gfc_get_logical_expr (kind, &x->where,
1077 x->value.logical && y->value.logical);
1079 default:
1080 gcc_unreachable ();
1085 gfc_expr *
1086 gfc_simplify_any (gfc_expr *mask, gfc_expr *dim)
1088 return simplify_transformation (mask, dim, NULL, false, gfc_or);
1092 gfc_expr *
1093 gfc_simplify_dnint (gfc_expr *e)
1095 gfc_expr *result;
1097 if (e->expr_type != EXPR_CONSTANT)
1098 return NULL;
1100 result = gfc_get_constant_expr (BT_REAL, gfc_default_double_kind, &e->where);
1101 mpfr_round (result->value.real, e->value.real);
1103 return range_check (result, "DNINT");
1107 gfc_expr *
1108 gfc_simplify_asin (gfc_expr *x)
1110 gfc_expr *result;
1112 if (x->expr_type != EXPR_CONSTANT)
1113 return NULL;
1115 switch (x->ts.type)
1117 case BT_REAL:
1118 if (mpfr_cmp_si (x->value.real, 1) > 0
1119 || mpfr_cmp_si (x->value.real, -1) < 0)
1121 gfc_error ("Argument of ASIN at %L must be between -1 and 1",
1122 &x->where);
1123 return &gfc_bad_expr;
1125 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1126 mpfr_asin (result->value.real, x->value.real, GFC_RND_MODE);
1127 break;
1129 case BT_COMPLEX:
1130 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1131 mpc_asin (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1132 break;
1134 default:
1135 gfc_internal_error ("in gfc_simplify_asin(): Bad type");
1138 return range_check (result, "ASIN");
1142 /* Convert radians to degrees, i.e., x * 180 / pi. */
1144 static void
1145 rad2deg (mpfr_t x)
1147 mpfr_t tmp;
1149 mpfr_init (tmp);
1150 mpfr_const_pi (tmp, GFC_RND_MODE);
1151 mpfr_mul_ui (x, x, 180, GFC_RND_MODE);
1152 mpfr_div (x, x, tmp, GFC_RND_MODE);
1153 mpfr_clear (tmp);
1157 /* Simplify ACOSD(X) where the returned value has units of degree. */
1159 gfc_expr *
1160 gfc_simplify_acosd (gfc_expr *x)
1162 gfc_expr *result;
1164 if (x->expr_type != EXPR_CONSTANT)
1165 return NULL;
1167 if (mpfr_cmp_si (x->value.real, 1) > 0
1168 || mpfr_cmp_si (x->value.real, -1) < 0)
1170 gfc_error ("Argument of ACOSD at %L must be between -1 and 1",
1171 &x->where);
1172 return &gfc_bad_expr;
1175 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1176 mpfr_acos (result->value.real, x->value.real, GFC_RND_MODE);
1177 rad2deg (result->value.real);
1179 return range_check (result, "ACOSD");
1183 /* Simplify asind (x) where the returned value has units of degree. */
1185 gfc_expr *
1186 gfc_simplify_asind (gfc_expr *x)
1188 gfc_expr *result;
1190 if (x->expr_type != EXPR_CONSTANT)
1191 return NULL;
1193 if (mpfr_cmp_si (x->value.real, 1) > 0
1194 || mpfr_cmp_si (x->value.real, -1) < 0)
1196 gfc_error ("Argument of ASIND at %L must be between -1 and 1",
1197 &x->where);
1198 return &gfc_bad_expr;
1201 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1202 mpfr_asin (result->value.real, x->value.real, GFC_RND_MODE);
1203 rad2deg (result->value.real);
1205 return range_check (result, "ASIND");
1209 /* Simplify atand (x) where the returned value has units of degree. */
1211 gfc_expr *
1212 gfc_simplify_atand (gfc_expr *x)
1214 gfc_expr *result;
1216 if (x->expr_type != EXPR_CONSTANT)
1217 return NULL;
1219 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1220 mpfr_atan (result->value.real, x->value.real, GFC_RND_MODE);
1221 rad2deg (result->value.real);
1223 return range_check (result, "ATAND");
1227 gfc_expr *
1228 gfc_simplify_asinh (gfc_expr *x)
1230 gfc_expr *result;
1232 if (x->expr_type != EXPR_CONSTANT)
1233 return NULL;
1235 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1237 switch (x->ts.type)
1239 case BT_REAL:
1240 mpfr_asinh (result->value.real, x->value.real, GFC_RND_MODE);
1241 break;
1243 case BT_COMPLEX:
1244 mpc_asinh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1245 break;
1247 default:
1248 gfc_internal_error ("in gfc_simplify_asinh(): Bad type");
1251 return range_check (result, "ASINH");
1255 gfc_expr *
1256 gfc_simplify_atan (gfc_expr *x)
1258 gfc_expr *result;
1260 if (x->expr_type != EXPR_CONSTANT)
1261 return NULL;
1263 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1265 switch (x->ts.type)
1267 case BT_REAL:
1268 mpfr_atan (result->value.real, x->value.real, GFC_RND_MODE);
1269 break;
1271 case BT_COMPLEX:
1272 mpc_atan (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1273 break;
1275 default:
1276 gfc_internal_error ("in gfc_simplify_atan(): Bad type");
1279 return range_check (result, "ATAN");
1283 gfc_expr *
1284 gfc_simplify_atanh (gfc_expr *x)
1286 gfc_expr *result;
1288 if (x->expr_type != EXPR_CONSTANT)
1289 return NULL;
1291 switch (x->ts.type)
1293 case BT_REAL:
1294 if (mpfr_cmp_si (x->value.real, 1) >= 0
1295 || mpfr_cmp_si (x->value.real, -1) <= 0)
1297 gfc_error ("Argument of ATANH at %L must be inside the range -1 "
1298 "to 1", &x->where);
1299 return &gfc_bad_expr;
1301 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1302 mpfr_atanh (result->value.real, x->value.real, GFC_RND_MODE);
1303 break;
1305 case BT_COMPLEX:
1306 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1307 mpc_atanh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1308 break;
1310 default:
1311 gfc_internal_error ("in gfc_simplify_atanh(): Bad type");
1314 return range_check (result, "ATANH");
1318 gfc_expr *
1319 gfc_simplify_atan2 (gfc_expr *y, gfc_expr *x)
1321 gfc_expr *result;
1323 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1324 return NULL;
1326 if (mpfr_zero_p (y->value.real) && mpfr_zero_p (x->value.real))
1328 gfc_error ("If first argument of ATAN2 at %L is zero, then the "
1329 "second argument must not be zero", &y->where);
1330 return &gfc_bad_expr;
1333 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1334 mpfr_atan2 (result->value.real, y->value.real, x->value.real, GFC_RND_MODE);
1336 return range_check (result, "ATAN2");
1340 gfc_expr *
1341 gfc_simplify_bessel_j0 (gfc_expr *x)
1343 gfc_expr *result;
1345 if (x->expr_type != EXPR_CONSTANT)
1346 return NULL;
1348 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1349 mpfr_j0 (result->value.real, x->value.real, GFC_RND_MODE);
1351 return range_check (result, "BESSEL_J0");
1355 gfc_expr *
1356 gfc_simplify_bessel_j1 (gfc_expr *x)
1358 gfc_expr *result;
1360 if (x->expr_type != EXPR_CONSTANT)
1361 return NULL;
1363 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1364 mpfr_j1 (result->value.real, x->value.real, GFC_RND_MODE);
1366 return range_check (result, "BESSEL_J1");
1370 gfc_expr *
1371 gfc_simplify_bessel_jn (gfc_expr *order, gfc_expr *x)
1373 gfc_expr *result;
1374 long n;
1376 if (x->expr_type != EXPR_CONSTANT || order->expr_type != EXPR_CONSTANT)
1377 return NULL;
1379 n = mpz_get_si (order->value.integer);
1380 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1381 mpfr_jn (result->value.real, n, x->value.real, GFC_RND_MODE);
1383 return range_check (result, "BESSEL_JN");
1387 /* Simplify transformational form of JN and YN. */
1389 static gfc_expr *
1390 gfc_simplify_bessel_n2 (gfc_expr *order1, gfc_expr *order2, gfc_expr *x,
1391 bool jn)
1393 gfc_expr *result;
1394 gfc_expr *e;
1395 long n1, n2;
1396 int i;
1397 mpfr_t x2rev, last1, last2;
1399 if (x->expr_type != EXPR_CONSTANT || order1->expr_type != EXPR_CONSTANT
1400 || order2->expr_type != EXPR_CONSTANT)
1401 return NULL;
1403 n1 = mpz_get_si (order1->value.integer);
1404 n2 = mpz_get_si (order2->value.integer);
1405 result = gfc_get_array_expr (x->ts.type, x->ts.kind, &x->where);
1406 result->rank = 1;
1407 result->shape = gfc_get_shape (1);
1408 mpz_init_set_ui (result->shape[0], MAX (n2-n1+1, 0));
1410 if (n2 < n1)
1411 return result;
1413 /* Special case: x == 0; it is J0(0.0) == 1, JN(N > 0, 0.0) == 0; and
1414 YN(N, 0.0) = -Inf. */
1416 if (mpfr_cmp_ui (x->value.real, 0.0) == 0)
1418 if (!jn && flag_range_check)
1420 gfc_error ("Result of BESSEL_YN is -INF at %L", &result->where);
1421 gfc_free_expr (result);
1422 return &gfc_bad_expr;
1425 if (jn && n1 == 0)
1427 e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1428 mpfr_set_ui (e->value.real, 1, GFC_RND_MODE);
1429 gfc_constructor_append_expr (&result->value.constructor, e,
1430 &x->where);
1431 n1++;
1434 for (i = n1; i <= n2; i++)
1436 e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1437 if (jn)
1438 mpfr_set_ui (e->value.real, 0, GFC_RND_MODE);
1439 else
1440 mpfr_set_inf (e->value.real, -1);
1441 gfc_constructor_append_expr (&result->value.constructor, e,
1442 &x->where);
1445 return result;
1448 /* Use the faster but more verbose recurrence algorithm. Bessel functions
1449 are stable for downward recursion and Neumann functions are stable
1450 for upward recursion. It is
1451 x2rev = 2.0/x,
1452 J(N-1, x) = x2rev * N * J(N, x) - J(N+1, x),
1453 Y(N+1, x) = x2rev * N * Y(N, x) - Y(N-1, x).
1454 Cf. http://dlmf.nist.gov/10.74#iv and http://dlmf.nist.gov/10.6#E1 */
1456 gfc_set_model_kind (x->ts.kind);
1458 /* Get first recursion anchor. */
1460 mpfr_init (last1);
1461 if (jn)
1462 mpfr_jn (last1, n2, x->value.real, GFC_RND_MODE);
1463 else
1464 mpfr_yn (last1, n1, x->value.real, GFC_RND_MODE);
1466 e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1467 mpfr_set (e->value.real, last1, GFC_RND_MODE);
1468 if (range_check (e, jn ? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr)
1470 mpfr_clear (last1);
1471 gfc_free_expr (e);
1472 gfc_free_expr (result);
1473 return &gfc_bad_expr;
1475 gfc_constructor_append_expr (&result->value.constructor, e, &x->where);
1477 if (n1 == n2)
1479 mpfr_clear (last1);
1480 return result;
1483 /* Get second recursion anchor. */
1485 mpfr_init (last2);
1486 if (jn)
1487 mpfr_jn (last2, n2-1, x->value.real, GFC_RND_MODE);
1488 else
1489 mpfr_yn (last2, n1+1, x->value.real, GFC_RND_MODE);
1491 e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1492 mpfr_set (e->value.real, last2, GFC_RND_MODE);
1493 if (range_check (e, jn ? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr)
1495 mpfr_clear (last1);
1496 mpfr_clear (last2);
1497 gfc_free_expr (e);
1498 gfc_free_expr (result);
1499 return &gfc_bad_expr;
1501 if (jn)
1502 gfc_constructor_insert_expr (&result->value.constructor, e, &x->where, -2);
1503 else
1504 gfc_constructor_append_expr (&result->value.constructor, e, &x->where);
1506 if (n1 + 1 == n2)
1508 mpfr_clear (last1);
1509 mpfr_clear (last2);
1510 return result;
1513 /* Start actual recursion. */
1515 mpfr_init (x2rev);
1516 mpfr_ui_div (x2rev, 2, x->value.real, GFC_RND_MODE);
1518 for (i = 2; i <= n2-n1; i++)
1520 e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1522 /* Special case: For YN, if the previous N gave -INF, set
1523 also N+1 to -INF. */
1524 if (!jn && !flag_range_check && mpfr_inf_p (last2))
1526 mpfr_set_inf (e->value.real, -1);
1527 gfc_constructor_append_expr (&result->value.constructor, e,
1528 &x->where);
1529 continue;
1532 mpfr_mul_si (e->value.real, x2rev, jn ? (n2-i+1) : (n1+i-1),
1533 GFC_RND_MODE);
1534 mpfr_mul (e->value.real, e->value.real, last2, GFC_RND_MODE);
1535 mpfr_sub (e->value.real, e->value.real, last1, GFC_RND_MODE);
1537 if (range_check (e, jn ? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr)
1539 /* Range_check frees "e" in that case. */
1540 e = NULL;
1541 goto error;
1544 if (jn)
1545 gfc_constructor_insert_expr (&result->value.constructor, e, &x->where,
1546 -i-1);
1547 else
1548 gfc_constructor_append_expr (&result->value.constructor, e, &x->where);
1550 mpfr_set (last1, last2, GFC_RND_MODE);
1551 mpfr_set (last2, e->value.real, GFC_RND_MODE);
1554 mpfr_clear (last1);
1555 mpfr_clear (last2);
1556 mpfr_clear (x2rev);
1557 return result;
1559 error:
1560 mpfr_clear (last1);
1561 mpfr_clear (last2);
1562 mpfr_clear (x2rev);
1563 gfc_free_expr (e);
1564 gfc_free_expr (result);
1565 return &gfc_bad_expr;
1569 gfc_expr *
1570 gfc_simplify_bessel_jn2 (gfc_expr *order1, gfc_expr *order2, gfc_expr *x)
1572 return gfc_simplify_bessel_n2 (order1, order2, x, true);
1576 gfc_expr *
1577 gfc_simplify_bessel_y0 (gfc_expr *x)
1579 gfc_expr *result;
1581 if (x->expr_type != EXPR_CONSTANT)
1582 return NULL;
1584 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1585 mpfr_y0 (result->value.real, x->value.real, GFC_RND_MODE);
1587 return range_check (result, "BESSEL_Y0");
1591 gfc_expr *
1592 gfc_simplify_bessel_y1 (gfc_expr *x)
1594 gfc_expr *result;
1596 if (x->expr_type != EXPR_CONSTANT)
1597 return NULL;
1599 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1600 mpfr_y1 (result->value.real, x->value.real, GFC_RND_MODE);
1602 return range_check (result, "BESSEL_Y1");
1606 gfc_expr *
1607 gfc_simplify_bessel_yn (gfc_expr *order, gfc_expr *x)
1609 gfc_expr *result;
1610 long n;
1612 if (x->expr_type != EXPR_CONSTANT || order->expr_type != EXPR_CONSTANT)
1613 return NULL;
1615 n = mpz_get_si (order->value.integer);
1616 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1617 mpfr_yn (result->value.real, n, x->value.real, GFC_RND_MODE);
1619 return range_check (result, "BESSEL_YN");
1623 gfc_expr *
1624 gfc_simplify_bessel_yn2 (gfc_expr *order1, gfc_expr *order2, gfc_expr *x)
1626 return gfc_simplify_bessel_n2 (order1, order2, x, false);
1630 gfc_expr *
1631 gfc_simplify_bit_size (gfc_expr *e)
1633 int i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
1634 return gfc_get_int_expr (e->ts.kind, &e->where,
1635 gfc_integer_kinds[i].bit_size);
1639 gfc_expr *
1640 gfc_simplify_btest (gfc_expr *e, gfc_expr *bit)
1642 int b;
1644 if (e->expr_type != EXPR_CONSTANT || bit->expr_type != EXPR_CONSTANT)
1645 return NULL;
1647 if (gfc_extract_int (bit, &b) || b < 0)
1648 return gfc_get_logical_expr (gfc_default_logical_kind, &e->where, false);
1650 return gfc_get_logical_expr (gfc_default_logical_kind, &e->where,
1651 mpz_tstbit (e->value.integer, b));
1655 static int
1656 compare_bitwise (gfc_expr *i, gfc_expr *j)
1658 mpz_t x, y;
1659 int k, res;
1661 gcc_assert (i->ts.type == BT_INTEGER);
1662 gcc_assert (j->ts.type == BT_INTEGER);
1664 mpz_init_set (x, i->value.integer);
1665 k = gfc_validate_kind (i->ts.type, i->ts.kind, false);
1666 convert_mpz_to_unsigned (x, gfc_integer_kinds[k].bit_size);
1668 mpz_init_set (y, j->value.integer);
1669 k = gfc_validate_kind (j->ts.type, j->ts.kind, false);
1670 convert_mpz_to_unsigned (y, gfc_integer_kinds[k].bit_size);
1672 res = mpz_cmp (x, y);
1673 mpz_clear (x);
1674 mpz_clear (y);
1675 return res;
1679 gfc_expr *
1680 gfc_simplify_bge (gfc_expr *i, gfc_expr *j)
1682 if (i->expr_type != EXPR_CONSTANT || j->expr_type != EXPR_CONSTANT)
1683 return NULL;
1685 return gfc_get_logical_expr (gfc_default_logical_kind, &i->where,
1686 compare_bitwise (i, j) >= 0);
1690 gfc_expr *
1691 gfc_simplify_bgt (gfc_expr *i, gfc_expr *j)
1693 if (i->expr_type != EXPR_CONSTANT || j->expr_type != EXPR_CONSTANT)
1694 return NULL;
1696 return gfc_get_logical_expr (gfc_default_logical_kind, &i->where,
1697 compare_bitwise (i, j) > 0);
1701 gfc_expr *
1702 gfc_simplify_ble (gfc_expr *i, gfc_expr *j)
1704 if (i->expr_type != EXPR_CONSTANT || j->expr_type != EXPR_CONSTANT)
1705 return NULL;
1707 return gfc_get_logical_expr (gfc_default_logical_kind, &i->where,
1708 compare_bitwise (i, j) <= 0);
1712 gfc_expr *
1713 gfc_simplify_blt (gfc_expr *i, gfc_expr *j)
1715 if (i->expr_type != EXPR_CONSTANT || j->expr_type != EXPR_CONSTANT)
1716 return NULL;
1718 return gfc_get_logical_expr (gfc_default_logical_kind, &i->where,
1719 compare_bitwise (i, j) < 0);
1723 gfc_expr *
1724 gfc_simplify_ceiling (gfc_expr *e, gfc_expr *k)
1726 gfc_expr *ceil, *result;
1727 int kind;
1729 kind = get_kind (BT_INTEGER, k, "CEILING", gfc_default_integer_kind);
1730 if (kind == -1)
1731 return &gfc_bad_expr;
1733 if (e->expr_type != EXPR_CONSTANT)
1734 return NULL;
1736 ceil = gfc_copy_expr (e);
1737 mpfr_ceil (ceil->value.real, e->value.real);
1739 result = gfc_get_constant_expr (BT_INTEGER, kind, &e->where);
1740 gfc_mpfr_to_mpz (result->value.integer, ceil->value.real, &e->where);
1742 gfc_free_expr (ceil);
1744 return range_check (result, "CEILING");
1748 gfc_expr *
1749 gfc_simplify_char (gfc_expr *e, gfc_expr *k)
1751 return simplify_achar_char (e, k, "CHAR", false);
1755 /* Common subroutine for simplifying CMPLX, COMPLEX and DCMPLX. */
1757 static gfc_expr *
1758 simplify_cmplx (const char *name, gfc_expr *x, gfc_expr *y, int kind)
1760 gfc_expr *result;
1762 if (x->expr_type != EXPR_CONSTANT
1763 || (y != NULL && y->expr_type != EXPR_CONSTANT))
1764 return NULL;
1766 result = gfc_get_constant_expr (BT_COMPLEX, kind, &x->where);
1768 switch (x->ts.type)
1770 case BT_INTEGER:
1771 mpc_set_z (result->value.complex, x->value.integer, GFC_MPC_RND_MODE);
1772 break;
1774 case BT_REAL:
1775 mpc_set_fr (result->value.complex, x->value.real, GFC_RND_MODE);
1776 break;
1778 case BT_COMPLEX:
1779 mpc_set (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1780 break;
1782 default:
1783 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (x)");
1786 if (!y)
1787 return range_check (result, name);
1789 switch (y->ts.type)
1791 case BT_INTEGER:
1792 mpfr_set_z (mpc_imagref (result->value.complex),
1793 y->value.integer, GFC_RND_MODE);
1794 break;
1796 case BT_REAL:
1797 mpfr_set (mpc_imagref (result->value.complex),
1798 y->value.real, GFC_RND_MODE);
1799 break;
1801 default:
1802 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (y)");
1805 return range_check (result, name);
1809 gfc_expr *
1810 gfc_simplify_cmplx (gfc_expr *x, gfc_expr *y, gfc_expr *k)
1812 int kind;
1814 kind = get_kind (BT_REAL, k, "CMPLX", gfc_default_complex_kind);
1815 if (kind == -1)
1816 return &gfc_bad_expr;
1818 return simplify_cmplx ("CMPLX", x, y, kind);
1822 gfc_expr *
1823 gfc_simplify_complex (gfc_expr *x, gfc_expr *y)
1825 int kind;
1827 if (x->ts.type == BT_INTEGER && y->ts.type == BT_INTEGER)
1828 kind = gfc_default_complex_kind;
1829 else if (x->ts.type == BT_REAL || y->ts.type == BT_INTEGER)
1830 kind = x->ts.kind;
1831 else if (x->ts.type == BT_INTEGER || y->ts.type == BT_REAL)
1832 kind = y->ts.kind;
1833 else if (x->ts.type == BT_REAL && y->ts.type == BT_REAL)
1834 kind = (x->ts.kind > y->ts.kind) ? x->ts.kind : y->ts.kind;
1835 else
1836 gcc_unreachable ();
1838 return simplify_cmplx ("COMPLEX", x, y, kind);
1842 gfc_expr *
1843 gfc_simplify_conjg (gfc_expr *e)
1845 gfc_expr *result;
1847 if (e->expr_type != EXPR_CONSTANT)
1848 return NULL;
1850 result = gfc_copy_expr (e);
1851 mpc_conj (result->value.complex, result->value.complex, GFC_MPC_RND_MODE);
1853 return range_check (result, "CONJG");
1857 /* Simplify atan2d (x) where the unit is degree. */
1859 gfc_expr *
1860 gfc_simplify_atan2d (gfc_expr *y, gfc_expr *x)
1862 gfc_expr *result;
1864 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1865 return NULL;
1867 if (mpfr_zero_p (y->value.real) && mpfr_zero_p (x->value.real))
1869 gfc_error ("If first argument of ATAN2D at %L is zero, then the "
1870 "second argument must not be zero", &y->where);
1871 return &gfc_bad_expr;
1874 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1875 mpfr_atan2 (result->value.real, y->value.real, x->value.real, GFC_RND_MODE);
1876 rad2deg (result->value.real);
1878 return range_check (result, "ATAN2D");
1882 gfc_expr *
1883 gfc_simplify_cos (gfc_expr *x)
1885 gfc_expr *result;
1887 if (x->expr_type != EXPR_CONSTANT)
1888 return NULL;
1890 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1892 switch (x->ts.type)
1894 case BT_REAL:
1895 mpfr_cos (result->value.real, x->value.real, GFC_RND_MODE);
1896 break;
1898 case BT_COMPLEX:
1899 gfc_set_model_kind (x->ts.kind);
1900 mpc_cos (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1901 break;
1903 default:
1904 gfc_internal_error ("in gfc_simplify_cos(): Bad type");
1907 return range_check (result, "COS");
1911 static void
1912 deg2rad (mpfr_t x)
1914 mpfr_t d2r;
1916 mpfr_init (d2r);
1917 mpfr_const_pi (d2r, GFC_RND_MODE);
1918 mpfr_div_ui (d2r, d2r, 180, GFC_RND_MODE);
1919 mpfr_mul (x, x, d2r, GFC_RND_MODE);
1920 mpfr_clear (d2r);
1924 /* Simplification routines for SIND, COSD, TAND. */
1925 #include "trigd_fe.inc"
1928 /* Simplify COSD(X) where X has the unit of degree. */
1930 gfc_expr *
1931 gfc_simplify_cosd (gfc_expr *x)
1933 gfc_expr *result;
1935 if (x->expr_type != EXPR_CONSTANT)
1936 return NULL;
1938 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1939 mpfr_set (result->value.real, x->value.real, GFC_RND_MODE);
1940 simplify_cosd (result->value.real);
1942 return range_check (result, "COSD");
1946 /* Simplify SIND(X) where X has the unit of degree. */
1948 gfc_expr *
1949 gfc_simplify_sind (gfc_expr *x)
1951 gfc_expr *result;
1953 if (x->expr_type != EXPR_CONSTANT)
1954 return NULL;
1956 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1957 mpfr_set (result->value.real, x->value.real, GFC_RND_MODE);
1958 simplify_sind (result->value.real);
1960 return range_check (result, "SIND");
1964 /* Simplify TAND(X) where X has the unit of degree. */
1966 gfc_expr *
1967 gfc_simplify_tand (gfc_expr *x)
1969 gfc_expr *result;
1971 if (x->expr_type != EXPR_CONSTANT)
1972 return NULL;
1974 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1975 mpfr_set (result->value.real, x->value.real, GFC_RND_MODE);
1976 simplify_tand (result->value.real);
1978 return range_check (result, "TAND");
1982 /* Simplify COTAND(X) where X has the unit of degree. */
1984 gfc_expr *
1985 gfc_simplify_cotand (gfc_expr *x)
1987 gfc_expr *result;
1989 if (x->expr_type != EXPR_CONSTANT)
1990 return NULL;
1992 /* Implement COTAND = -TAND(x+90).
1993 TAND offers correct exact values for multiples of 30 degrees.
1994 This implementation is also compatible with the behavior of some legacy
1995 compilers. Keep this consistent with gfc_conv_intrinsic_cotand. */
1996 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1997 mpfr_set (result->value.real, x->value.real, GFC_RND_MODE);
1998 mpfr_add_ui (result->value.real, result->value.real, 90, GFC_RND_MODE);
1999 simplify_tand (result->value.real);
2000 mpfr_neg (result->value.real, result->value.real, GFC_RND_MODE);
2002 return range_check (result, "COTAND");
2006 gfc_expr *
2007 gfc_simplify_cosh (gfc_expr *x)
2009 gfc_expr *result;
2011 if (x->expr_type != EXPR_CONSTANT)
2012 return NULL;
2014 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2016 switch (x->ts.type)
2018 case BT_REAL:
2019 mpfr_cosh (result->value.real, x->value.real, GFC_RND_MODE);
2020 break;
2022 case BT_COMPLEX:
2023 mpc_cosh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
2024 break;
2026 default:
2027 gcc_unreachable ();
2030 return range_check (result, "COSH");
2034 gfc_expr *
2035 gfc_simplify_count (gfc_expr *mask, gfc_expr *dim, gfc_expr *kind)
2037 gfc_expr *result;
2038 bool size_zero;
2040 size_zero = gfc_is_size_zero_array (mask);
2042 if (!(is_constant_array_expr (mask) || size_zero)
2043 || !gfc_is_constant_expr (dim)
2044 || !gfc_is_constant_expr (kind))
2045 return NULL;
2047 result = transformational_result (mask, dim,
2048 BT_INTEGER,
2049 get_kind (BT_INTEGER, kind, "COUNT",
2050 gfc_default_integer_kind),
2051 &mask->where);
2053 init_result_expr (result, 0, NULL);
2055 if (size_zero)
2056 return result;
2058 /* Passing MASK twice, once as data array, once as mask.
2059 Whenever gfc_count is called, '1' is added to the result. */
2060 return !dim || mask->rank == 1 ?
2061 simplify_transformation_to_scalar (result, mask, mask, gfc_count) :
2062 simplify_transformation_to_array (result, mask, dim, mask, gfc_count, NULL);
2065 /* Simplification routine for cshift. This works by copying the array
2066 expressions into a one-dimensional array, shuffling the values into another
2067 one-dimensional array and creating the new array expression from this. The
2068 shuffling part is basically taken from the library routine. */
2070 gfc_expr *
2071 gfc_simplify_cshift (gfc_expr *array, gfc_expr *shift, gfc_expr *dim)
2073 gfc_expr *result;
2074 int which;
2075 gfc_expr **arrayvec, **resultvec;
2076 gfc_expr **rptr, **sptr;
2077 mpz_t size;
2078 size_t arraysize, shiftsize, i;
2079 gfc_constructor *array_ctor, *shift_ctor;
2080 ssize_t *shiftvec, *hptr;
2081 ssize_t shift_val, len;
2082 ssize_t count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS],
2083 hs_ex[GFC_MAX_DIMENSIONS + 1],
2084 hstride[GFC_MAX_DIMENSIONS], sstride[GFC_MAX_DIMENSIONS],
2085 a_extent[GFC_MAX_DIMENSIONS], a_stride[GFC_MAX_DIMENSIONS],
2086 h_extent[GFC_MAX_DIMENSIONS],
2087 ss_ex[GFC_MAX_DIMENSIONS + 1];
2088 ssize_t rsoffset;
2089 int d, n;
2090 bool continue_loop;
2091 gfc_expr **src, **dest;
2093 if (!is_constant_array_expr (array))
2094 return NULL;
2096 if (shift->rank > 0)
2097 gfc_simplify_expr (shift, 1);
2099 if (!gfc_is_constant_expr (shift))
2100 return NULL;
2102 /* Make dim zero-based. */
2103 if (dim)
2105 if (!gfc_is_constant_expr (dim))
2106 return NULL;
2107 which = mpz_get_si (dim->value.integer) - 1;
2109 else
2110 which = 0;
2112 if (array->shape == NULL)
2113 return NULL;
2115 gfc_array_size (array, &size);
2116 arraysize = mpz_get_ui (size);
2117 mpz_clear (size);
2119 result = gfc_get_array_expr (array->ts.type, array->ts.kind, &array->where);
2120 result->shape = gfc_copy_shape (array->shape, array->rank);
2121 result->rank = array->rank;
2122 result->ts.u.derived = array->ts.u.derived;
2124 if (arraysize == 0)
2125 return result;
2127 arrayvec = XCNEWVEC (gfc_expr *, arraysize);
2128 array_ctor = gfc_constructor_first (array->value.constructor);
2129 for (i = 0; i < arraysize; i++)
2131 arrayvec[i] = array_ctor->expr;
2132 array_ctor = gfc_constructor_next (array_ctor);
2135 resultvec = XCNEWVEC (gfc_expr *, arraysize);
2137 extent[0] = 1;
2138 count[0] = 0;
2140 for (d=0; d < array->rank; d++)
2142 a_extent[d] = mpz_get_si (array->shape[d]);
2143 a_stride[d] = d == 0 ? 1 : a_stride[d-1] * a_extent[d-1];
2146 if (shift->rank > 0)
2148 gfc_array_size (shift, &size);
2149 shiftsize = mpz_get_ui (size);
2150 mpz_clear (size);
2151 shiftvec = XCNEWVEC (ssize_t, shiftsize);
2152 shift_ctor = gfc_constructor_first (shift->value.constructor);
2153 for (d = 0; d < shift->rank; d++)
2155 h_extent[d] = mpz_get_si (shift->shape[d]);
2156 hstride[d] = d == 0 ? 1 : hstride[d-1] * h_extent[d-1];
2159 else
2160 shiftvec = NULL;
2162 /* Shut up compiler */
2163 len = 1;
2164 rsoffset = 1;
2166 n = 0;
2167 for (d=0; d < array->rank; d++)
2169 if (d == which)
2171 rsoffset = a_stride[d];
2172 len = a_extent[d];
2174 else
2176 count[n] = 0;
2177 extent[n] = a_extent[d];
2178 sstride[n] = a_stride[d];
2179 ss_ex[n] = sstride[n] * extent[n];
2180 if (shiftvec)
2181 hs_ex[n] = hstride[n] * extent[n];
2182 n++;
2185 ss_ex[n] = 0;
2186 hs_ex[n] = 0;
2188 if (shiftvec)
2190 for (i = 0; i < shiftsize; i++)
2192 ssize_t val;
2193 val = mpz_get_si (shift_ctor->expr->value.integer);
2194 val = val % len;
2195 if (val < 0)
2196 val += len;
2197 shiftvec[i] = val;
2198 shift_ctor = gfc_constructor_next (shift_ctor);
2200 shift_val = 0;
2202 else
2204 shift_val = mpz_get_si (shift->value.integer);
2205 shift_val = shift_val % len;
2206 if (shift_val < 0)
2207 shift_val += len;
2210 continue_loop = true;
2211 d = array->rank;
2212 rptr = resultvec;
2213 sptr = arrayvec;
2214 hptr = shiftvec;
2216 while (continue_loop)
2218 ssize_t sh;
2219 if (shiftvec)
2220 sh = *hptr;
2221 else
2222 sh = shift_val;
2224 src = &sptr[sh * rsoffset];
2225 dest = rptr;
2226 for (n = 0; n < len - sh; n++)
2228 *dest = *src;
2229 dest += rsoffset;
2230 src += rsoffset;
2232 src = sptr;
2233 for ( n = 0; n < sh; n++)
2235 *dest = *src;
2236 dest += rsoffset;
2237 src += rsoffset;
2239 rptr += sstride[0];
2240 sptr += sstride[0];
2241 if (shiftvec)
2242 hptr += hstride[0];
2243 count[0]++;
2244 n = 0;
2245 while (count[n] == extent[n])
2247 count[n] = 0;
2248 rptr -= ss_ex[n];
2249 sptr -= ss_ex[n];
2250 if (shiftvec)
2251 hptr -= hs_ex[n];
2252 n++;
2253 if (n >= d - 1)
2255 continue_loop = false;
2256 break;
2258 else
2260 count[n]++;
2261 rptr += sstride[n];
2262 sptr += sstride[n];
2263 if (shiftvec)
2264 hptr += hstride[n];
2269 for (i = 0; i < arraysize; i++)
2271 gfc_constructor_append_expr (&result->value.constructor,
2272 gfc_copy_expr (resultvec[i]),
2273 NULL);
2275 return result;
2279 gfc_expr *
2280 gfc_simplify_dcmplx (gfc_expr *x, gfc_expr *y)
2282 return simplify_cmplx ("DCMPLX", x, y, gfc_default_double_kind);
2286 gfc_expr *
2287 gfc_simplify_dble (gfc_expr *e)
2289 gfc_expr *result = NULL;
2290 int tmp1, tmp2;
2292 if (e->expr_type != EXPR_CONSTANT)
2293 return NULL;
2295 /* For explicit conversion, turn off -Wconversion and -Wconversion-extra
2296 warnings. */
2297 tmp1 = warn_conversion;
2298 tmp2 = warn_conversion_extra;
2299 warn_conversion = warn_conversion_extra = 0;
2301 result = gfc_convert_constant (e, BT_REAL, gfc_default_double_kind);
2303 warn_conversion = tmp1;
2304 warn_conversion_extra = tmp2;
2306 if (result == &gfc_bad_expr)
2307 return &gfc_bad_expr;
2309 return range_check (result, "DBLE");
2313 gfc_expr *
2314 gfc_simplify_digits (gfc_expr *x)
2316 int i, digits;
2318 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
2320 switch (x->ts.type)
2322 case BT_INTEGER:
2323 digits = gfc_integer_kinds[i].digits;
2324 break;
2326 case BT_REAL:
2327 case BT_COMPLEX:
2328 digits = gfc_real_kinds[i].digits;
2329 break;
2331 default:
2332 gcc_unreachable ();
2335 return gfc_get_int_expr (gfc_default_integer_kind, NULL, digits);
2339 gfc_expr *
2340 gfc_simplify_dim (gfc_expr *x, gfc_expr *y)
2342 gfc_expr *result;
2343 int kind;
2345 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2346 return NULL;
2348 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
2349 result = gfc_get_constant_expr (x->ts.type, kind, &x->where);
2351 switch (x->ts.type)
2353 case BT_INTEGER:
2354 if (mpz_cmp (x->value.integer, y->value.integer) > 0)
2355 mpz_sub (result->value.integer, x->value.integer, y->value.integer);
2356 else
2357 mpz_set_ui (result->value.integer, 0);
2359 break;
2361 case BT_REAL:
2362 if (mpfr_cmp (x->value.real, y->value.real) > 0)
2363 mpfr_sub (result->value.real, x->value.real, y->value.real,
2364 GFC_RND_MODE);
2365 else
2366 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
2368 break;
2370 default:
2371 gfc_internal_error ("gfc_simplify_dim(): Bad type");
2374 return range_check (result, "DIM");
2378 gfc_expr*
2379 gfc_simplify_dot_product (gfc_expr *vector_a, gfc_expr *vector_b)
2381 /* If vector_a is a zero-sized array, the result is 0 for INTEGER,
2382 REAL, and COMPLEX types and .false. for LOGICAL. */
2383 if (vector_a->shape && mpz_get_si (vector_a->shape[0]) == 0)
2385 if (vector_a->ts.type == BT_LOGICAL)
2386 return gfc_get_logical_expr (gfc_default_logical_kind, NULL, false);
2387 else
2388 return gfc_get_int_expr (gfc_default_integer_kind, NULL, 0);
2391 if (!is_constant_array_expr (vector_a)
2392 || !is_constant_array_expr (vector_b))
2393 return NULL;
2395 return compute_dot_product (vector_a, 1, 0, vector_b, 1, 0, true);
2399 gfc_expr *
2400 gfc_simplify_dprod (gfc_expr *x, gfc_expr *y)
2402 gfc_expr *a1, *a2, *result;
2404 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2405 return NULL;
2407 a1 = gfc_real2real (x, gfc_default_double_kind);
2408 a2 = gfc_real2real (y, gfc_default_double_kind);
2410 result = gfc_get_constant_expr (BT_REAL, gfc_default_double_kind, &x->where);
2411 mpfr_mul (result->value.real, a1->value.real, a2->value.real, GFC_RND_MODE);
2413 gfc_free_expr (a2);
2414 gfc_free_expr (a1);
2416 return range_check (result, "DPROD");
2420 static gfc_expr *
2421 simplify_dshift (gfc_expr *arg1, gfc_expr *arg2, gfc_expr *shiftarg,
2422 bool right)
2424 gfc_expr *result;
2425 int i, k, size, shift;
2427 if (arg1->expr_type != EXPR_CONSTANT || arg2->expr_type != EXPR_CONSTANT
2428 || shiftarg->expr_type != EXPR_CONSTANT)
2429 return NULL;
2431 k = gfc_validate_kind (BT_INTEGER, arg1->ts.kind, false);
2432 size = gfc_integer_kinds[k].bit_size;
2434 gfc_extract_int (shiftarg, &shift);
2436 /* DSHIFTR(I,J,SHIFT) = DSHIFTL(I,J,SIZE-SHIFT). */
2437 if (right)
2438 shift = size - shift;
2440 result = gfc_get_constant_expr (BT_INTEGER, arg1->ts.kind, &arg1->where);
2441 mpz_set_ui (result->value.integer, 0);
2443 for (i = 0; i < shift; i++)
2444 if (mpz_tstbit (arg2->value.integer, size - shift + i))
2445 mpz_setbit (result->value.integer, i);
2447 for (i = 0; i < size - shift; i++)
2448 if (mpz_tstbit (arg1->value.integer, i))
2449 mpz_setbit (result->value.integer, shift + i);
2451 /* Convert to a signed value. */
2452 gfc_convert_mpz_to_signed (result->value.integer, size);
2454 return result;
2458 gfc_expr *
2459 gfc_simplify_dshiftr (gfc_expr *arg1, gfc_expr *arg2, gfc_expr *shiftarg)
2461 return simplify_dshift (arg1, arg2, shiftarg, true);
2465 gfc_expr *
2466 gfc_simplify_dshiftl (gfc_expr *arg1, gfc_expr *arg2, gfc_expr *shiftarg)
2468 return simplify_dshift (arg1, arg2, shiftarg, false);
2472 gfc_expr *
2473 gfc_simplify_eoshift (gfc_expr *array, gfc_expr *shift, gfc_expr *boundary,
2474 gfc_expr *dim)
2476 bool temp_boundary;
2477 gfc_expr *bnd;
2478 gfc_expr *result;
2479 int which;
2480 gfc_expr **arrayvec, **resultvec;
2481 gfc_expr **rptr, **sptr;
2482 mpz_t size;
2483 size_t arraysize, i;
2484 gfc_constructor *array_ctor, *shift_ctor, *bnd_ctor;
2485 ssize_t shift_val, len;
2486 ssize_t count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS],
2487 sstride[GFC_MAX_DIMENSIONS], a_extent[GFC_MAX_DIMENSIONS],
2488 a_stride[GFC_MAX_DIMENSIONS], ss_ex[GFC_MAX_DIMENSIONS + 1];
2489 ssize_t rsoffset;
2490 int d, n;
2491 bool continue_loop;
2492 gfc_expr **src, **dest;
2493 size_t s_len;
2495 if (!is_constant_array_expr (array))
2496 return NULL;
2498 if (shift->rank > 0)
2499 gfc_simplify_expr (shift, 1);
2501 if (!gfc_is_constant_expr (shift))
2502 return NULL;
2504 if (boundary)
2506 if (boundary->rank > 0)
2507 gfc_simplify_expr (boundary, 1);
2509 if (!gfc_is_constant_expr (boundary))
2510 return NULL;
2513 if (dim)
2515 if (!gfc_is_constant_expr (dim))
2516 return NULL;
2517 which = mpz_get_si (dim->value.integer) - 1;
2519 else
2520 which = 0;
2522 s_len = 0;
2523 if (boundary == NULL)
2525 temp_boundary = true;
2526 switch (array->ts.type)
2529 case BT_INTEGER:
2530 bnd = gfc_get_int_expr (array->ts.kind, NULL, 0);
2531 break;
2533 case BT_LOGICAL:
2534 bnd = gfc_get_logical_expr (array->ts.kind, NULL, 0);
2535 break;
2537 case BT_REAL:
2538 bnd = gfc_get_constant_expr (array->ts.type, array->ts.kind, &gfc_current_locus);
2539 mpfr_set_ui (bnd->value.real, 0, GFC_RND_MODE);
2540 break;
2542 case BT_COMPLEX:
2543 bnd = gfc_get_constant_expr (array->ts.type, array->ts.kind, &gfc_current_locus);
2544 mpc_set_ui (bnd->value.complex, 0, GFC_RND_MODE);
2545 break;
2547 case BT_CHARACTER:
2548 s_len = mpz_get_ui (array->ts.u.cl->length->value.integer);
2549 bnd = gfc_get_character_expr (array->ts.kind, &gfc_current_locus, NULL, s_len);
2550 break;
2552 default:
2553 gcc_unreachable();
2557 else
2559 temp_boundary = false;
2560 bnd = boundary;
2563 gfc_array_size (array, &size);
2564 arraysize = mpz_get_ui (size);
2565 mpz_clear (size);
2567 result = gfc_get_array_expr (array->ts.type, array->ts.kind, &array->where);
2568 result->shape = gfc_copy_shape (array->shape, array->rank);
2569 result->rank = array->rank;
2570 result->ts = array->ts;
2572 if (arraysize == 0)
2573 goto final;
2575 arrayvec = XCNEWVEC (gfc_expr *, arraysize);
2576 array_ctor = gfc_constructor_first (array->value.constructor);
2577 for (i = 0; i < arraysize; i++)
2579 arrayvec[i] = array_ctor->expr;
2580 array_ctor = gfc_constructor_next (array_ctor);
2583 resultvec = XCNEWVEC (gfc_expr *, arraysize);
2585 extent[0] = 1;
2586 count[0] = 0;
2588 for (d=0; d < array->rank; d++)
2590 a_extent[d] = mpz_get_si (array->shape[d]);
2591 a_stride[d] = d == 0 ? 1 : a_stride[d-1] * a_extent[d-1];
2594 if (shift->rank > 0)
2596 shift_ctor = gfc_constructor_first (shift->value.constructor);
2597 shift_val = 0;
2599 else
2601 shift_ctor = NULL;
2602 shift_val = mpz_get_si (shift->value.integer);
2605 if (bnd->rank > 0)
2606 bnd_ctor = gfc_constructor_first (bnd->value.constructor);
2607 else
2608 bnd_ctor = NULL;
2610 /* Shut up compiler */
2611 len = 1;
2612 rsoffset = 1;
2614 n = 0;
2615 for (d=0; d < array->rank; d++)
2617 if (d == which)
2619 rsoffset = a_stride[d];
2620 len = a_extent[d];
2622 else
2624 count[n] = 0;
2625 extent[n] = a_extent[d];
2626 sstride[n] = a_stride[d];
2627 ss_ex[n] = sstride[n] * extent[n];
2628 n++;
2631 ss_ex[n] = 0;
2633 continue_loop = true;
2634 d = array->rank;
2635 rptr = resultvec;
2636 sptr = arrayvec;
2638 while (continue_loop)
2640 ssize_t sh, delta;
2642 if (shift_ctor)
2643 sh = mpz_get_si (shift_ctor->expr->value.integer);
2644 else
2645 sh = shift_val;
2647 if (( sh >= 0 ? sh : -sh ) > len)
2649 delta = len;
2650 sh = len;
2652 else
2653 delta = (sh >= 0) ? sh: -sh;
2655 if (sh > 0)
2657 src = &sptr[delta * rsoffset];
2658 dest = rptr;
2660 else
2662 src = sptr;
2663 dest = &rptr[delta * rsoffset];
2666 for (n = 0; n < len - delta; n++)
2668 *dest = *src;
2669 dest += rsoffset;
2670 src += rsoffset;
2673 if (sh < 0)
2674 dest = rptr;
2676 n = delta;
2678 if (bnd_ctor)
2680 while (n--)
2682 *dest = gfc_copy_expr (bnd_ctor->expr);
2683 dest += rsoffset;
2686 else
2688 while (n--)
2690 *dest = gfc_copy_expr (bnd);
2691 dest += rsoffset;
2694 rptr += sstride[0];
2695 sptr += sstride[0];
2696 if (shift_ctor)
2697 shift_ctor = gfc_constructor_next (shift_ctor);
2699 if (bnd_ctor)
2700 bnd_ctor = gfc_constructor_next (bnd_ctor);
2702 count[0]++;
2703 n = 0;
2704 while (count[n] == extent[n])
2706 count[n] = 0;
2707 rptr -= ss_ex[n];
2708 sptr -= ss_ex[n];
2709 n++;
2710 if (n >= d - 1)
2712 continue_loop = false;
2713 break;
2715 else
2717 count[n]++;
2718 rptr += sstride[n];
2719 sptr += sstride[n];
2724 for (i = 0; i < arraysize; i++)
2726 gfc_constructor_append_expr (&result->value.constructor,
2727 gfc_copy_expr (resultvec[i]),
2728 NULL);
2731 final:
2732 if (temp_boundary)
2733 gfc_free_expr (bnd);
2735 return result;
2738 gfc_expr *
2739 gfc_simplify_erf (gfc_expr *x)
2741 gfc_expr *result;
2743 if (x->expr_type != EXPR_CONSTANT)
2744 return NULL;
2746 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2747 mpfr_erf (result->value.real, x->value.real, GFC_RND_MODE);
2749 return range_check (result, "ERF");
2753 gfc_expr *
2754 gfc_simplify_erfc (gfc_expr *x)
2756 gfc_expr *result;
2758 if (x->expr_type != EXPR_CONSTANT)
2759 return NULL;
2761 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2762 mpfr_erfc (result->value.real, x->value.real, GFC_RND_MODE);
2764 return range_check (result, "ERFC");
2768 /* Helper functions to simplify ERFC_SCALED(x) = ERFC(x) * EXP(X**2). */
2770 #define MAX_ITER 200
2771 #define ARG_LIMIT 12
2773 /* Calculate ERFC_SCALED directly by its definition:
2775 ERFC_SCALED(x) = ERFC(x) * EXP(X**2)
2777 using a large precision for intermediate results. This is used for all
2778 but large values of the argument. */
2779 static void
2780 fullprec_erfc_scaled (mpfr_t res, mpfr_t arg)
2782 mpfr_prec_t prec;
2783 mpfr_t a, b;
2785 prec = mpfr_get_default_prec ();
2786 mpfr_set_default_prec (10 * prec);
2788 mpfr_init (a);
2789 mpfr_init (b);
2791 mpfr_set (a, arg, GFC_RND_MODE);
2792 mpfr_sqr (b, a, GFC_RND_MODE);
2793 mpfr_exp (b, b, GFC_RND_MODE);
2794 mpfr_erfc (a, a, GFC_RND_MODE);
2795 mpfr_mul (a, a, b, GFC_RND_MODE);
2797 mpfr_set (res, a, GFC_RND_MODE);
2798 mpfr_set_default_prec (prec);
2800 mpfr_clear (a);
2801 mpfr_clear (b);
2804 /* Calculate ERFC_SCALED using a power series expansion in 1/arg:
2806 ERFC_SCALED(x) = 1 / (x * sqrt(pi))
2807 * (1 + Sum_n (-1)**n * (1 * 3 * 5 * ... * (2n-1))
2808 / (2 * x**2)**n)
2810 This is used for large values of the argument. Intermediate calculations
2811 are performed with twice the precision. We don't do a fixed number of
2812 iterations of the sum, but stop when it has converged to the required
2813 precision. */
2814 static void
2815 asympt_erfc_scaled (mpfr_t res, mpfr_t arg)
2817 mpfr_t sum, x, u, v, w, oldsum, sumtrunc;
2818 mpz_t num;
2819 mpfr_prec_t prec;
2820 unsigned i;
2822 prec = mpfr_get_default_prec ();
2823 mpfr_set_default_prec (2 * prec);
2825 mpfr_init (sum);
2826 mpfr_init (x);
2827 mpfr_init (u);
2828 mpfr_init (v);
2829 mpfr_init (w);
2830 mpz_init (num);
2832 mpfr_init (oldsum);
2833 mpfr_init (sumtrunc);
2834 mpfr_set_prec (oldsum, prec);
2835 mpfr_set_prec (sumtrunc, prec);
2837 mpfr_set (x, arg, GFC_RND_MODE);
2838 mpfr_set_ui (sum, 1, GFC_RND_MODE);
2839 mpz_set_ui (num, 1);
2841 mpfr_set (u, x, GFC_RND_MODE);
2842 mpfr_sqr (u, u, GFC_RND_MODE);
2843 mpfr_mul_ui (u, u, 2, GFC_RND_MODE);
2844 mpfr_pow_si (u, u, -1, GFC_RND_MODE);
2846 for (i = 1; i < MAX_ITER; i++)
2848 mpfr_set (oldsum, sum, GFC_RND_MODE);
2850 mpz_mul_ui (num, num, 2 * i - 1);
2851 mpz_neg (num, num);
2853 mpfr_set (w, u, GFC_RND_MODE);
2854 mpfr_pow_ui (w, w, i, GFC_RND_MODE);
2856 mpfr_set_z (v, num, GFC_RND_MODE);
2857 mpfr_mul (v, v, w, GFC_RND_MODE);
2859 mpfr_add (sum, sum, v, GFC_RND_MODE);
2861 mpfr_set (sumtrunc, sum, GFC_RND_MODE);
2862 if (mpfr_cmp (sumtrunc, oldsum) == 0)
2863 break;
2866 /* We should have converged by now; otherwise, ARG_LIMIT is probably
2867 set too low. */
2868 gcc_assert (i < MAX_ITER);
2870 /* Divide by x * sqrt(Pi). */
2871 mpfr_const_pi (u, GFC_RND_MODE);
2872 mpfr_sqrt (u, u, GFC_RND_MODE);
2873 mpfr_mul (u, u, x, GFC_RND_MODE);
2874 mpfr_div (sum, sum, u, GFC_RND_MODE);
2876 mpfr_set (res, sum, GFC_RND_MODE);
2877 mpfr_set_default_prec (prec);
2879 mpfr_clears (sum, x, u, v, w, oldsum, sumtrunc, NULL);
2880 mpz_clear (num);
2884 gfc_expr *
2885 gfc_simplify_erfc_scaled (gfc_expr *x)
2887 gfc_expr *result;
2889 if (x->expr_type != EXPR_CONSTANT)
2890 return NULL;
2892 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2893 if (mpfr_cmp_d (x->value.real, ARG_LIMIT) >= 0)
2894 asympt_erfc_scaled (result->value.real, x->value.real);
2895 else
2896 fullprec_erfc_scaled (result->value.real, x->value.real);
2898 return range_check (result, "ERFC_SCALED");
2901 #undef MAX_ITER
2902 #undef ARG_LIMIT
2905 gfc_expr *
2906 gfc_simplify_epsilon (gfc_expr *e)
2908 gfc_expr *result;
2909 int i;
2911 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2913 result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
2914 mpfr_set (result->value.real, gfc_real_kinds[i].epsilon, GFC_RND_MODE);
2916 return range_check (result, "EPSILON");
2920 gfc_expr *
2921 gfc_simplify_exp (gfc_expr *x)
2923 gfc_expr *result;
2925 if (x->expr_type != EXPR_CONSTANT)
2926 return NULL;
2928 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2930 switch (x->ts.type)
2932 case BT_REAL:
2933 mpfr_exp (result->value.real, x->value.real, GFC_RND_MODE);
2934 break;
2936 case BT_COMPLEX:
2937 gfc_set_model_kind (x->ts.kind);
2938 mpc_exp (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
2939 break;
2941 default:
2942 gfc_internal_error ("in gfc_simplify_exp(): Bad type");
2945 return range_check (result, "EXP");
2949 gfc_expr *
2950 gfc_simplify_exponent (gfc_expr *x)
2952 long int val;
2953 gfc_expr *result;
2955 if (x->expr_type != EXPR_CONSTANT)
2956 return NULL;
2958 result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
2959 &x->where);
2961 /* EXPONENT(inf) = EXPONENT(nan) = HUGE(0) */
2962 if (mpfr_inf_p (x->value.real) || mpfr_nan_p (x->value.real))
2964 int i = gfc_validate_kind (BT_INTEGER, gfc_default_integer_kind, false);
2965 mpz_set (result->value.integer, gfc_integer_kinds[i].huge);
2966 return result;
2969 /* EXPONENT(+/- 0.0) = 0 */
2970 if (mpfr_zero_p (x->value.real))
2972 mpz_set_ui (result->value.integer, 0);
2973 return result;
2976 gfc_set_model (x->value.real);
2978 val = (long int) mpfr_get_exp (x->value.real);
2979 mpz_set_si (result->value.integer, val);
2981 return range_check (result, "EXPONENT");
2985 gfc_expr *
2986 gfc_simplify_failed_or_stopped_images (gfc_expr *team ATTRIBUTE_UNUSED,
2987 gfc_expr *kind)
2989 if (flag_coarray == GFC_FCOARRAY_NONE)
2991 gfc_current_locus = *gfc_current_intrinsic_where;
2992 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
2993 return &gfc_bad_expr;
2996 if (flag_coarray == GFC_FCOARRAY_SINGLE)
2998 gfc_expr *result;
2999 int actual_kind;
3000 if (kind)
3001 gfc_extract_int (kind, &actual_kind);
3002 else
3003 actual_kind = gfc_default_integer_kind;
3005 result = gfc_get_array_expr (BT_INTEGER, actual_kind, &gfc_current_locus);
3006 result->rank = 1;
3007 return result;
3010 /* For fcoarray = lib no simplification is possible, because it is not known
3011 what images failed or are stopped at compile time. */
3012 return NULL;
3016 gfc_expr *
3017 gfc_simplify_get_team (gfc_expr *level ATTRIBUTE_UNUSED)
3019 if (flag_coarray == GFC_FCOARRAY_NONE)
3021 gfc_current_locus = *gfc_current_intrinsic_where;
3022 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3023 return &gfc_bad_expr;
3026 if (flag_coarray == GFC_FCOARRAY_SINGLE)
3028 gfc_expr *result;
3029 result = gfc_get_array_expr (BT_INTEGER, gfc_default_integer_kind, &gfc_current_locus);
3030 result->rank = 0;
3031 return result;
3034 /* For fcoarray = lib no simplification is possible, because it is not known
3035 what images failed or are stopped at compile time. */
3036 return NULL;
3040 gfc_expr *
3041 gfc_simplify_float (gfc_expr *a)
3043 gfc_expr *result;
3045 if (a->expr_type != EXPR_CONSTANT)
3046 return NULL;
3048 result = gfc_int2real (a, gfc_default_real_kind);
3050 return range_check (result, "FLOAT");
3054 static bool
3055 is_last_ref_vtab (gfc_expr *e)
3057 gfc_ref *ref;
3058 gfc_component *comp = NULL;
3060 if (e->expr_type != EXPR_VARIABLE)
3061 return false;
3063 for (ref = e->ref; ref; ref = ref->next)
3064 if (ref->type == REF_COMPONENT)
3065 comp = ref->u.c.component;
3067 if (!e->ref || !comp)
3068 return e->symtree->n.sym->attr.vtab;
3070 if (comp->name[0] == '_' && strcmp (comp->name, "_vptr") == 0)
3071 return true;
3073 return false;
3077 gfc_expr *
3078 gfc_simplify_extends_type_of (gfc_expr *a, gfc_expr *mold)
3080 /* Avoid simplification of resolved symbols. */
3081 if (is_last_ref_vtab (a) || is_last_ref_vtab (mold))
3082 return NULL;
3084 if (a->ts.type == BT_DERIVED && mold->ts.type == BT_DERIVED)
3085 return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
3086 gfc_type_is_extension_of (mold->ts.u.derived,
3087 a->ts.u.derived));
3089 if (UNLIMITED_POLY (a) || UNLIMITED_POLY (mold))
3090 return NULL;
3092 /* Return .false. if the dynamic type can never be an extension. */
3093 if ((a->ts.type == BT_CLASS && mold->ts.type == BT_CLASS
3094 && !gfc_type_is_extension_of
3095 (mold->ts.u.derived->components->ts.u.derived,
3096 a->ts.u.derived->components->ts.u.derived)
3097 && !gfc_type_is_extension_of
3098 (a->ts.u.derived->components->ts.u.derived,
3099 mold->ts.u.derived->components->ts.u.derived))
3100 || (a->ts.type == BT_DERIVED && mold->ts.type == BT_CLASS
3101 && !gfc_type_is_extension_of
3102 (mold->ts.u.derived->components->ts.u.derived,
3103 a->ts.u.derived))
3104 || (a->ts.type == BT_CLASS && mold->ts.type == BT_DERIVED
3105 && !gfc_type_is_extension_of
3106 (mold->ts.u.derived,
3107 a->ts.u.derived->components->ts.u.derived)
3108 && !gfc_type_is_extension_of
3109 (a->ts.u.derived->components->ts.u.derived,
3110 mold->ts.u.derived)))
3111 return gfc_get_logical_expr (gfc_default_logical_kind, &a->where, false);
3113 /* Return .true. if the dynamic type is guaranteed to be an extension. */
3114 if (a->ts.type == BT_CLASS && mold->ts.type == BT_DERIVED
3115 && gfc_type_is_extension_of (mold->ts.u.derived,
3116 a->ts.u.derived->components->ts.u.derived))
3117 return gfc_get_logical_expr (gfc_default_logical_kind, &a->where, true);
3119 return NULL;
3123 gfc_expr *
3124 gfc_simplify_same_type_as (gfc_expr *a, gfc_expr *b)
3126 /* Avoid simplification of resolved symbols. */
3127 if (is_last_ref_vtab (a) || is_last_ref_vtab (b))
3128 return NULL;
3130 /* Return .false. if the dynamic type can never be the
3131 same. */
3132 if (((a->ts.type == BT_CLASS && gfc_expr_attr (a).class_ok)
3133 || (b->ts.type == BT_CLASS && gfc_expr_attr (b).class_ok))
3134 && !gfc_type_compatible (&a->ts, &b->ts)
3135 && !gfc_type_compatible (&b->ts, &a->ts))
3136 return gfc_get_logical_expr (gfc_default_logical_kind, &a->where, false);
3138 if (a->ts.type != BT_DERIVED || b->ts.type != BT_DERIVED)
3139 return NULL;
3141 return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
3142 gfc_compare_derived_types (a->ts.u.derived,
3143 b->ts.u.derived));
3147 gfc_expr *
3148 gfc_simplify_floor (gfc_expr *e, gfc_expr *k)
3150 gfc_expr *result;
3151 mpfr_t floor;
3152 int kind;
3154 kind = get_kind (BT_INTEGER, k, "FLOOR", gfc_default_integer_kind);
3155 if (kind == -1)
3156 gfc_internal_error ("gfc_simplify_floor(): Bad kind");
3158 if (e->expr_type != EXPR_CONSTANT)
3159 return NULL;
3161 mpfr_init2 (floor, mpfr_get_prec (e->value.real));
3162 mpfr_floor (floor, e->value.real);
3164 result = gfc_get_constant_expr (BT_INTEGER, kind, &e->where);
3165 gfc_mpfr_to_mpz (result->value.integer, floor, &e->where);
3167 mpfr_clear (floor);
3169 return range_check (result, "FLOOR");
3173 gfc_expr *
3174 gfc_simplify_fraction (gfc_expr *x)
3176 gfc_expr *result;
3177 mpfr_exp_t e;
3179 if (x->expr_type != EXPR_CONSTANT)
3180 return NULL;
3182 result = gfc_get_constant_expr (BT_REAL, x->ts.kind, &x->where);
3184 /* FRACTION(inf) = NaN. */
3185 if (mpfr_inf_p (x->value.real))
3187 mpfr_set_nan (result->value.real);
3188 return result;
3191 /* mpfr_frexp() correctly handles zeros and NaNs. */
3192 mpfr_frexp (&e, result->value.real, x->value.real, GFC_RND_MODE);
3194 return range_check (result, "FRACTION");
3198 gfc_expr *
3199 gfc_simplify_gamma (gfc_expr *x)
3201 gfc_expr *result;
3203 if (x->expr_type != EXPR_CONSTANT)
3204 return NULL;
3206 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
3207 mpfr_gamma (result->value.real, x->value.real, GFC_RND_MODE);
3209 return range_check (result, "GAMMA");
3213 gfc_expr *
3214 gfc_simplify_huge (gfc_expr *e)
3216 gfc_expr *result;
3217 int i;
3219 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
3220 result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
3222 switch (e->ts.type)
3224 case BT_INTEGER:
3225 mpz_set (result->value.integer, gfc_integer_kinds[i].huge);
3226 break;
3228 case BT_REAL:
3229 mpfr_set (result->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
3230 break;
3232 default:
3233 gcc_unreachable ();
3236 return result;
3240 gfc_expr *
3241 gfc_simplify_hypot (gfc_expr *x, gfc_expr *y)
3243 gfc_expr *result;
3245 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
3246 return NULL;
3248 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
3249 mpfr_hypot (result->value.real, x->value.real, y->value.real, GFC_RND_MODE);
3250 return range_check (result, "HYPOT");
3254 /* We use the processor's collating sequence, because all
3255 systems that gfortran currently works on are ASCII. */
3257 gfc_expr *
3258 gfc_simplify_iachar (gfc_expr *e, gfc_expr *kind)
3260 gfc_expr *result;
3261 gfc_char_t index;
3262 int k;
3264 if (e->expr_type != EXPR_CONSTANT)
3265 return NULL;
3267 if (e->value.character.length != 1)
3269 gfc_error ("Argument of IACHAR at %L must be of length one", &e->where);
3270 return &gfc_bad_expr;
3273 index = e->value.character.string[0];
3275 if (warn_surprising && index > 127)
3276 gfc_warning (OPT_Wsurprising,
3277 "Argument of IACHAR function at %L outside of range 0..127",
3278 &e->where);
3280 k = get_kind (BT_INTEGER, kind, "IACHAR", gfc_default_integer_kind);
3281 if (k == -1)
3282 return &gfc_bad_expr;
3284 result = gfc_get_int_expr (k, &e->where, index);
3286 return range_check (result, "IACHAR");
3290 static gfc_expr *
3291 do_bit_and (gfc_expr *result, gfc_expr *e)
3293 gcc_assert (e->ts.type == BT_INTEGER && e->expr_type == EXPR_CONSTANT);
3294 gcc_assert (result->ts.type == BT_INTEGER
3295 && result->expr_type == EXPR_CONSTANT);
3297 mpz_and (result->value.integer, result->value.integer, e->value.integer);
3298 return result;
3302 gfc_expr *
3303 gfc_simplify_iall (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
3305 return simplify_transformation (array, dim, mask, -1, do_bit_and);
3309 static gfc_expr *
3310 do_bit_ior (gfc_expr *result, gfc_expr *e)
3312 gcc_assert (e->ts.type == BT_INTEGER && e->expr_type == EXPR_CONSTANT);
3313 gcc_assert (result->ts.type == BT_INTEGER
3314 && result->expr_type == EXPR_CONSTANT);
3316 mpz_ior (result->value.integer, result->value.integer, e->value.integer);
3317 return result;
3321 gfc_expr *
3322 gfc_simplify_iany (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
3324 return simplify_transformation (array, dim, mask, 0, do_bit_ior);
3328 gfc_expr *
3329 gfc_simplify_iand (gfc_expr *x, gfc_expr *y)
3331 gfc_expr *result;
3333 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
3334 return NULL;
3336 result = gfc_get_constant_expr (BT_INTEGER, x->ts.kind, &x->where);
3337 mpz_and (result->value.integer, x->value.integer, y->value.integer);
3339 return range_check (result, "IAND");
3343 gfc_expr *
3344 gfc_simplify_ibclr (gfc_expr *x, gfc_expr *y)
3346 gfc_expr *result;
3347 int k, pos;
3349 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
3350 return NULL;
3352 gfc_extract_int (y, &pos);
3354 k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
3356 result = gfc_copy_expr (x);
3358 convert_mpz_to_unsigned (result->value.integer,
3359 gfc_integer_kinds[k].bit_size);
3361 mpz_clrbit (result->value.integer, pos);
3363 gfc_convert_mpz_to_signed (result->value.integer,
3364 gfc_integer_kinds[k].bit_size);
3366 return result;
3370 gfc_expr *
3371 gfc_simplify_ibits (gfc_expr *x, gfc_expr *y, gfc_expr *z)
3373 gfc_expr *result;
3374 int pos, len;
3375 int i, k, bitsize;
3376 int *bits;
3378 if (x->expr_type != EXPR_CONSTANT
3379 || y->expr_type != EXPR_CONSTANT
3380 || z->expr_type != EXPR_CONSTANT)
3381 return NULL;
3383 gfc_extract_int (y, &pos);
3384 gfc_extract_int (z, &len);
3386 k = gfc_validate_kind (BT_INTEGER, x->ts.kind, false);
3388 bitsize = gfc_integer_kinds[k].bit_size;
3390 if (pos + len > bitsize)
3392 gfc_error ("Sum of second and third arguments of IBITS exceeds "
3393 "bit size at %L", &y->where);
3394 return &gfc_bad_expr;
3397 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
3398 convert_mpz_to_unsigned (result->value.integer,
3399 gfc_integer_kinds[k].bit_size);
3401 bits = XCNEWVEC (int, bitsize);
3403 for (i = 0; i < bitsize; i++)
3404 bits[i] = 0;
3406 for (i = 0; i < len; i++)
3407 bits[i] = mpz_tstbit (x->value.integer, i + pos);
3409 for (i = 0; i < bitsize; i++)
3411 if (bits[i] == 0)
3412 mpz_clrbit (result->value.integer, i);
3413 else if (bits[i] == 1)
3414 mpz_setbit (result->value.integer, i);
3415 else
3416 gfc_internal_error ("IBITS: Bad bit");
3419 free (bits);
3421 gfc_convert_mpz_to_signed (result->value.integer,
3422 gfc_integer_kinds[k].bit_size);
3424 return result;
3428 gfc_expr *
3429 gfc_simplify_ibset (gfc_expr *x, gfc_expr *y)
3431 gfc_expr *result;
3432 int k, pos;
3434 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
3435 return NULL;
3437 gfc_extract_int (y, &pos);
3439 k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
3441 result = gfc_copy_expr (x);
3443 convert_mpz_to_unsigned (result->value.integer,
3444 gfc_integer_kinds[k].bit_size);
3446 mpz_setbit (result->value.integer, pos);
3448 gfc_convert_mpz_to_signed (result->value.integer,
3449 gfc_integer_kinds[k].bit_size);
3451 return result;
3455 gfc_expr *
3456 gfc_simplify_ichar (gfc_expr *e, gfc_expr *kind)
3458 gfc_expr *result;
3459 gfc_char_t index;
3460 int k;
3462 if (e->expr_type != EXPR_CONSTANT)
3463 return NULL;
3465 if (e->value.character.length != 1)
3467 gfc_error ("Argument of ICHAR at %L must be of length one", &e->where);
3468 return &gfc_bad_expr;
3471 index = e->value.character.string[0];
3473 k = get_kind (BT_INTEGER, kind, "ICHAR", gfc_default_integer_kind);
3474 if (k == -1)
3475 return &gfc_bad_expr;
3477 result = gfc_get_int_expr (k, &e->where, index);
3479 return range_check (result, "ICHAR");
3483 gfc_expr *
3484 gfc_simplify_ieor (gfc_expr *x, gfc_expr *y)
3486 gfc_expr *result;
3488 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
3489 return NULL;
3491 result = gfc_get_constant_expr (BT_INTEGER, x->ts.kind, &x->where);
3492 mpz_xor (result->value.integer, x->value.integer, y->value.integer);
3494 return range_check (result, "IEOR");
3498 gfc_expr *
3499 gfc_simplify_index (gfc_expr *x, gfc_expr *y, gfc_expr *b, gfc_expr *kind)
3501 gfc_expr *result;
3502 int back, len, lensub;
3503 int i, j, k, count, index = 0, start;
3505 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT
3506 || ( b != NULL && b->expr_type != EXPR_CONSTANT))
3507 return NULL;
3509 if (b != NULL && b->value.logical != 0)
3510 back = 1;
3511 else
3512 back = 0;
3514 k = get_kind (BT_INTEGER, kind, "INDEX", gfc_default_integer_kind);
3515 if (k == -1)
3516 return &gfc_bad_expr;
3518 result = gfc_get_constant_expr (BT_INTEGER, k, &x->where);
3520 len = x->value.character.length;
3521 lensub = y->value.character.length;
3523 if (len < lensub)
3525 mpz_set_si (result->value.integer, 0);
3526 return result;
3529 if (back == 0)
3531 if (lensub == 0)
3533 mpz_set_si (result->value.integer, 1);
3534 return result;
3536 else if (lensub == 1)
3538 for (i = 0; i < len; i++)
3540 for (j = 0; j < lensub; j++)
3542 if (y->value.character.string[j]
3543 == x->value.character.string[i])
3545 index = i + 1;
3546 goto done;
3551 else
3553 for (i = 0; i < len; i++)
3555 for (j = 0; j < lensub; j++)
3557 if (y->value.character.string[j]
3558 == x->value.character.string[i])
3560 start = i;
3561 count = 0;
3563 for (k = 0; k < lensub; k++)
3565 if (y->value.character.string[k]
3566 == x->value.character.string[k + start])
3567 count++;
3570 if (count == lensub)
3572 index = start + 1;
3573 goto done;
3581 else
3583 if (lensub == 0)
3585 mpz_set_si (result->value.integer, len + 1);
3586 return result;
3588 else if (lensub == 1)
3590 for (i = 0; i < len; i++)
3592 for (j = 0; j < lensub; j++)
3594 if (y->value.character.string[j]
3595 == x->value.character.string[len - i])
3597 index = len - i + 1;
3598 goto done;
3603 else
3605 for (i = 0; i < len; i++)
3607 for (j = 0; j < lensub; j++)
3609 if (y->value.character.string[j]
3610 == x->value.character.string[len - i])
3612 start = len - i;
3613 if (start <= len - lensub)
3615 count = 0;
3616 for (k = 0; k < lensub; k++)
3617 if (y->value.character.string[k]
3618 == x->value.character.string[k + start])
3619 count++;
3621 if (count == lensub)
3623 index = start + 1;
3624 goto done;
3627 else
3629 continue;
3637 done:
3638 mpz_set_si (result->value.integer, index);
3639 return range_check (result, "INDEX");
3643 static gfc_expr *
3644 simplify_intconv (gfc_expr *e, int kind, const char *name)
3646 gfc_expr *result = NULL;
3647 int tmp1, tmp2;
3649 /* Convert BOZ to integer, and return without range checking. */
3650 if (e->ts.type == BT_BOZ)
3652 if (!gfc_boz2int (e, kind))
3653 return NULL;
3654 result = gfc_copy_expr (e);
3655 return result;
3658 if (e->expr_type != EXPR_CONSTANT)
3659 return NULL;
3661 /* For explicit conversion, turn off -Wconversion and -Wconversion-extra
3662 warnings. */
3663 tmp1 = warn_conversion;
3664 tmp2 = warn_conversion_extra;
3665 warn_conversion = warn_conversion_extra = 0;
3667 result = gfc_convert_constant (e, BT_INTEGER, kind);
3669 warn_conversion = tmp1;
3670 warn_conversion_extra = tmp2;
3672 if (result == &gfc_bad_expr)
3673 return &gfc_bad_expr;
3675 return range_check (result, name);
3679 gfc_expr *
3680 gfc_simplify_int (gfc_expr *e, gfc_expr *k)
3682 int kind;
3684 kind = get_kind (BT_INTEGER, k, "INT", gfc_default_integer_kind);
3685 if (kind == -1)
3686 return &gfc_bad_expr;
3688 return simplify_intconv (e, kind, "INT");
3691 gfc_expr *
3692 gfc_simplify_int2 (gfc_expr *e)
3694 return simplify_intconv (e, 2, "INT2");
3698 gfc_expr *
3699 gfc_simplify_int8 (gfc_expr *e)
3701 return simplify_intconv (e, 8, "INT8");
3705 gfc_expr *
3706 gfc_simplify_long (gfc_expr *e)
3708 return simplify_intconv (e, 4, "LONG");
3712 gfc_expr *
3713 gfc_simplify_ifix (gfc_expr *e)
3715 gfc_expr *rtrunc, *result;
3717 if (e->expr_type != EXPR_CONSTANT)
3718 return NULL;
3720 rtrunc = gfc_copy_expr (e);
3721 mpfr_trunc (rtrunc->value.real, e->value.real);
3723 result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
3724 &e->where);
3725 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real, &e->where);
3727 gfc_free_expr (rtrunc);
3729 return range_check (result, "IFIX");
3733 gfc_expr *
3734 gfc_simplify_idint (gfc_expr *e)
3736 gfc_expr *rtrunc, *result;
3738 if (e->expr_type != EXPR_CONSTANT)
3739 return NULL;
3741 rtrunc = gfc_copy_expr (e);
3742 mpfr_trunc (rtrunc->value.real, e->value.real);
3744 result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
3745 &e->where);
3746 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real, &e->where);
3748 gfc_free_expr (rtrunc);
3750 return range_check (result, "IDINT");
3754 gfc_expr *
3755 gfc_simplify_ior (gfc_expr *x, gfc_expr *y)
3757 gfc_expr *result;
3759 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
3760 return NULL;
3762 result = gfc_get_constant_expr (BT_INTEGER, x->ts.kind, &x->where);
3763 mpz_ior (result->value.integer, x->value.integer, y->value.integer);
3765 return range_check (result, "IOR");
3769 static gfc_expr *
3770 do_bit_xor (gfc_expr *result, gfc_expr *e)
3772 gcc_assert (e->ts.type == BT_INTEGER && e->expr_type == EXPR_CONSTANT);
3773 gcc_assert (result->ts.type == BT_INTEGER
3774 && result->expr_type == EXPR_CONSTANT);
3776 mpz_xor (result->value.integer, result->value.integer, e->value.integer);
3777 return result;
3781 gfc_expr *
3782 gfc_simplify_iparity (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
3784 return simplify_transformation (array, dim, mask, 0, do_bit_xor);
3788 gfc_expr *
3789 gfc_simplify_is_iostat_end (gfc_expr *x)
3791 if (x->expr_type != EXPR_CONSTANT)
3792 return NULL;
3794 return gfc_get_logical_expr (gfc_default_logical_kind, &x->where,
3795 mpz_cmp_si (x->value.integer,
3796 LIBERROR_END) == 0);
3800 gfc_expr *
3801 gfc_simplify_is_iostat_eor (gfc_expr *x)
3803 if (x->expr_type != EXPR_CONSTANT)
3804 return NULL;
3806 return gfc_get_logical_expr (gfc_default_logical_kind, &x->where,
3807 mpz_cmp_si (x->value.integer,
3808 LIBERROR_EOR) == 0);
3812 gfc_expr *
3813 gfc_simplify_isnan (gfc_expr *x)
3815 if (x->expr_type != EXPR_CONSTANT)
3816 return NULL;
3818 return gfc_get_logical_expr (gfc_default_logical_kind, &x->where,
3819 mpfr_nan_p (x->value.real));
3823 /* Performs a shift on its first argument. Depending on the last
3824 argument, the shift can be arithmetic, i.e. with filling from the
3825 left like in the SHIFTA intrinsic. */
3826 static gfc_expr *
3827 simplify_shift (gfc_expr *e, gfc_expr *s, const char *name,
3828 bool arithmetic, int direction)
3830 gfc_expr *result;
3831 int ashift, *bits, i, k, bitsize, shift;
3833 if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
3834 return NULL;
3836 gfc_extract_int (s, &shift);
3838 k = gfc_validate_kind (BT_INTEGER, e->ts.kind, false);
3839 bitsize = gfc_integer_kinds[k].bit_size;
3841 result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
3843 if (shift == 0)
3845 mpz_set (result->value.integer, e->value.integer);
3846 return result;
3849 if (direction > 0 && shift < 0)
3851 /* Left shift, as in SHIFTL. */
3852 gfc_error ("Second argument of %s is negative at %L", name, &e->where);
3853 return &gfc_bad_expr;
3855 else if (direction < 0)
3857 /* Right shift, as in SHIFTR or SHIFTA. */
3858 if (shift < 0)
3860 gfc_error ("Second argument of %s is negative at %L",
3861 name, &e->where);
3862 return &gfc_bad_expr;
3865 shift = -shift;
3868 ashift = (shift >= 0 ? shift : -shift);
3870 if (ashift > bitsize)
3872 gfc_error ("Magnitude of second argument of %s exceeds bit size "
3873 "at %L", name, &e->where);
3874 return &gfc_bad_expr;
3877 bits = XCNEWVEC (int, bitsize);
3879 for (i = 0; i < bitsize; i++)
3880 bits[i] = mpz_tstbit (e->value.integer, i);
3882 if (shift > 0)
3884 /* Left shift. */
3885 for (i = 0; i < shift; i++)
3886 mpz_clrbit (result->value.integer, i);
3888 for (i = 0; i < bitsize - shift; i++)
3890 if (bits[i] == 0)
3891 mpz_clrbit (result->value.integer, i + shift);
3892 else
3893 mpz_setbit (result->value.integer, i + shift);
3896 else
3898 /* Right shift. */
3899 if (arithmetic && bits[bitsize - 1])
3900 for (i = bitsize - 1; i >= bitsize - ashift; i--)
3901 mpz_setbit (result->value.integer, i);
3902 else
3903 for (i = bitsize - 1; i >= bitsize - ashift; i--)
3904 mpz_clrbit (result->value.integer, i);
3906 for (i = bitsize - 1; i >= ashift; i--)
3908 if (bits[i] == 0)
3909 mpz_clrbit (result->value.integer, i - ashift);
3910 else
3911 mpz_setbit (result->value.integer, i - ashift);
3915 gfc_convert_mpz_to_signed (result->value.integer, bitsize);
3916 free (bits);
3918 return result;
3922 gfc_expr *
3923 gfc_simplify_ishft (gfc_expr *e, gfc_expr *s)
3925 return simplify_shift (e, s, "ISHFT", false, 0);
3929 gfc_expr *
3930 gfc_simplify_lshift (gfc_expr *e, gfc_expr *s)
3932 return simplify_shift (e, s, "LSHIFT", false, 1);
3936 gfc_expr *
3937 gfc_simplify_rshift (gfc_expr *e, gfc_expr *s)
3939 return simplify_shift (e, s, "RSHIFT", true, -1);
3943 gfc_expr *
3944 gfc_simplify_shifta (gfc_expr *e, gfc_expr *s)
3946 return simplify_shift (e, s, "SHIFTA", true, -1);
3950 gfc_expr *
3951 gfc_simplify_shiftl (gfc_expr *e, gfc_expr *s)
3953 return simplify_shift (e, s, "SHIFTL", false, 1);
3957 gfc_expr *
3958 gfc_simplify_shiftr (gfc_expr *e, gfc_expr *s)
3960 return simplify_shift (e, s, "SHIFTR", false, -1);
3964 gfc_expr *
3965 gfc_simplify_ishftc (gfc_expr *e, gfc_expr *s, gfc_expr *sz)
3967 gfc_expr *result;
3968 int shift, ashift, isize, ssize, delta, k;
3969 int i, *bits;
3971 if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
3972 return NULL;
3974 gfc_extract_int (s, &shift);
3976 k = gfc_validate_kind (e->ts.type, e->ts.kind, false);
3977 isize = gfc_integer_kinds[k].bit_size;
3979 if (sz != NULL)
3981 if (sz->expr_type != EXPR_CONSTANT)
3982 return NULL;
3984 gfc_extract_int (sz, &ssize);
3986 else
3987 ssize = isize;
3989 if (shift >= 0)
3990 ashift = shift;
3991 else
3992 ashift = -shift;
3994 if (ashift > ssize)
3996 if (sz == NULL)
3997 gfc_error ("Magnitude of second argument of ISHFTC exceeds "
3998 "BIT_SIZE of first argument at %C");
3999 else
4000 gfc_error ("Absolute value of SHIFT shall be less than or equal "
4001 "to SIZE at %C");
4002 return &gfc_bad_expr;
4005 result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
4007 mpz_set (result->value.integer, e->value.integer);
4009 if (shift == 0)
4010 return result;
4012 convert_mpz_to_unsigned (result->value.integer, isize);
4014 bits = XCNEWVEC (int, ssize);
4016 for (i = 0; i < ssize; i++)
4017 bits[i] = mpz_tstbit (e->value.integer, i);
4019 delta = ssize - ashift;
4021 if (shift > 0)
4023 for (i = 0; i < delta; i++)
4025 if (bits[i] == 0)
4026 mpz_clrbit (result->value.integer, i + shift);
4027 else
4028 mpz_setbit (result->value.integer, i + shift);
4031 for (i = delta; i < ssize; i++)
4033 if (bits[i] == 0)
4034 mpz_clrbit (result->value.integer, i - delta);
4035 else
4036 mpz_setbit (result->value.integer, i - delta);
4039 else
4041 for (i = 0; i < ashift; i++)
4043 if (bits[i] == 0)
4044 mpz_clrbit (result->value.integer, i + delta);
4045 else
4046 mpz_setbit (result->value.integer, i + delta);
4049 for (i = ashift; i < ssize; i++)
4051 if (bits[i] == 0)
4052 mpz_clrbit (result->value.integer, i + shift);
4053 else
4054 mpz_setbit (result->value.integer, i + shift);
4058 gfc_convert_mpz_to_signed (result->value.integer, isize);
4060 free (bits);
4061 return result;
4065 gfc_expr *
4066 gfc_simplify_kind (gfc_expr *e)
4068 return gfc_get_int_expr (gfc_default_integer_kind, NULL, e->ts.kind);
4072 static gfc_expr *
4073 simplify_bound_dim (gfc_expr *array, gfc_expr *kind, int d, int upper,
4074 gfc_array_spec *as, gfc_ref *ref, bool coarray)
4076 gfc_expr *l, *u, *result;
4077 int k;
4079 k = get_kind (BT_INTEGER, kind, upper ? "UBOUND" : "LBOUND",
4080 gfc_default_integer_kind);
4081 if (k == -1)
4082 return &gfc_bad_expr;
4084 result = gfc_get_constant_expr (BT_INTEGER, k, &array->where);
4086 /* For non-variables, LBOUND(expr, DIM=n) = 1 and
4087 UBOUND(expr, DIM=n) = SIZE(expr, DIM=n). */
4088 if (!coarray && array->expr_type != EXPR_VARIABLE)
4090 if (upper)
4092 gfc_expr* dim = result;
4093 mpz_set_si (dim->value.integer, d);
4095 result = simplify_size (array, dim, k);
4096 gfc_free_expr (dim);
4097 if (!result)
4098 goto returnNull;
4100 else
4101 mpz_set_si (result->value.integer, 1);
4103 goto done;
4106 /* Otherwise, we have a variable expression. */
4107 gcc_assert (array->expr_type == EXPR_VARIABLE);
4108 gcc_assert (as);
4110 if (!gfc_resolve_array_spec (as, 0))
4111 return NULL;
4113 /* The last dimension of an assumed-size array is special. */
4114 if ((!coarray && d == as->rank && as->type == AS_ASSUMED_SIZE && !upper)
4115 || (coarray && d == as->rank + as->corank
4116 && (!upper || flag_coarray == GFC_FCOARRAY_SINGLE)))
4118 if (as->lower[d-1] && as->lower[d-1]->expr_type == EXPR_CONSTANT)
4120 gfc_free_expr (result);
4121 return gfc_copy_expr (as->lower[d-1]);
4124 goto returnNull;
4127 result = gfc_get_constant_expr (BT_INTEGER, k, &array->where);
4129 /* Then, we need to know the extent of the given dimension. */
4130 if (coarray || (ref->u.ar.type == AR_FULL && !ref->next))
4132 gfc_expr *declared_bound;
4133 int empty_bound;
4134 bool constant_lbound, constant_ubound;
4136 l = as->lower[d-1];
4137 u = as->upper[d-1];
4139 gcc_assert (l != NULL);
4141 constant_lbound = l->expr_type == EXPR_CONSTANT;
4142 constant_ubound = u && u->expr_type == EXPR_CONSTANT;
4144 empty_bound = upper ? 0 : 1;
4145 declared_bound = upper ? u : l;
4147 if ((!upper && !constant_lbound)
4148 || (upper && !constant_ubound))
4149 goto returnNull;
4151 if (!coarray)
4153 /* For {L,U}BOUND, the value depends on whether the array
4154 is empty. We can nevertheless simplify if the declared bound
4155 has the same value as that of an empty array, in which case
4156 the result isn't dependent on the array emptyness. */
4157 if (mpz_cmp_si (declared_bound->value.integer, empty_bound) == 0)
4158 mpz_set_si (result->value.integer, empty_bound);
4159 else if (!constant_lbound || !constant_ubound)
4160 /* Array emptyness can't be determined, we can't simplify. */
4161 goto returnNull;
4162 else if (mpz_cmp (l->value.integer, u->value.integer) > 0)
4163 mpz_set_si (result->value.integer, empty_bound);
4164 else
4165 mpz_set (result->value.integer, declared_bound->value.integer);
4167 else
4168 mpz_set (result->value.integer, declared_bound->value.integer);
4170 else
4172 if (upper)
4174 int d2 = 0, cnt = 0;
4175 for (int idx = 0; idx < ref->u.ar.dimen; ++idx)
4177 if (ref->u.ar.dimen_type[idx] == DIMEN_ELEMENT)
4178 d2++;
4179 else if (cnt < d - 1)
4180 cnt++;
4181 else
4182 break;
4184 if (!gfc_ref_dimen_size (&ref->u.ar, d2 + d - 1, &result->value.integer, NULL))
4185 goto returnNull;
4187 else
4188 mpz_set_si (result->value.integer, (long int) 1);
4191 done:
4192 return range_check (result, upper ? "UBOUND" : "LBOUND");
4194 returnNull:
4195 gfc_free_expr (result);
4196 return NULL;
4200 static gfc_expr *
4201 simplify_bound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind, int upper)
4203 gfc_ref *ref;
4204 gfc_array_spec *as;
4205 ar_type type = AR_UNKNOWN;
4206 int d;
4208 if (array->ts.type == BT_CLASS)
4209 return NULL;
4211 if (array->expr_type != EXPR_VARIABLE)
4213 as = NULL;
4214 ref = NULL;
4215 goto done;
4218 /* Do not attempt to resolve if error has already been issued. */
4219 if (array->symtree->n.sym->error)
4220 return NULL;
4222 /* Follow any component references. */
4223 as = array->symtree->n.sym->as;
4224 for (ref = array->ref; ref; ref = ref->next)
4226 switch (ref->type)
4228 case REF_ARRAY:
4229 type = ref->u.ar.type;
4230 switch (ref->u.ar.type)
4232 case AR_ELEMENT:
4233 as = NULL;
4234 continue;
4236 case AR_FULL:
4237 /* We're done because 'as' has already been set in the
4238 previous iteration. */
4239 goto done;
4241 case AR_UNKNOWN:
4242 return NULL;
4244 case AR_SECTION:
4245 as = ref->u.ar.as;
4246 goto done;
4249 gcc_unreachable ();
4251 case REF_COMPONENT:
4252 as = ref->u.c.component->as;
4253 continue;
4255 case REF_SUBSTRING:
4256 case REF_INQUIRY:
4257 continue;
4261 gcc_unreachable ();
4263 done:
4265 if (as && (as->type == AS_DEFERRED || as->type == AS_ASSUMED_RANK
4266 || (as->type == AS_ASSUMED_SHAPE && upper)))
4267 return NULL;
4269 /* 'array' shall not be an unallocated allocatable variable or a pointer that
4270 is not associated. */
4271 if (array->expr_type == EXPR_VARIABLE
4272 && (gfc_expr_attr (array).allocatable || gfc_expr_attr (array).pointer))
4273 return NULL;
4275 gcc_assert (!as
4276 || (as->type != AS_DEFERRED
4277 && array->expr_type == EXPR_VARIABLE
4278 && !gfc_expr_attr (array).allocatable
4279 && !gfc_expr_attr (array).pointer));
4281 if (dim == NULL)
4283 /* Multi-dimensional bounds. */
4284 gfc_expr *bounds[GFC_MAX_DIMENSIONS];
4285 gfc_expr *e;
4286 int k;
4288 /* UBOUND(ARRAY) is not valid for an assumed-size array. */
4289 if (upper && type == AR_FULL && as && as->type == AS_ASSUMED_SIZE)
4291 /* An error message will be emitted in
4292 check_assumed_size_reference (resolve.c). */
4293 return &gfc_bad_expr;
4296 /* Simplify the bounds for each dimension. */
4297 for (d = 0; d < array->rank; d++)
4299 bounds[d] = simplify_bound_dim (array, kind, d + 1, upper, as, ref,
4300 false);
4301 if (bounds[d] == NULL || bounds[d] == &gfc_bad_expr)
4303 int j;
4305 for (j = 0; j < d; j++)
4306 gfc_free_expr (bounds[j]);
4308 if (gfc_seen_div0)
4309 return &gfc_bad_expr;
4310 else
4311 return bounds[d];
4315 /* Allocate the result expression. */
4316 k = get_kind (BT_INTEGER, kind, upper ? "UBOUND" : "LBOUND",
4317 gfc_default_integer_kind);
4318 if (k == -1)
4319 return &gfc_bad_expr;
4321 e = gfc_get_array_expr (BT_INTEGER, k, &array->where);
4323 /* The result is a rank 1 array; its size is the rank of the first
4324 argument to {L,U}BOUND. */
4325 e->rank = 1;
4326 e->shape = gfc_get_shape (1);
4327 mpz_init_set_ui (e->shape[0], array->rank);
4329 /* Create the constructor for this array. */
4330 for (d = 0; d < array->rank; d++)
4331 gfc_constructor_append_expr (&e->value.constructor,
4332 bounds[d], &e->where);
4334 return e;
4336 else
4338 /* A DIM argument is specified. */
4339 if (dim->expr_type != EXPR_CONSTANT)
4340 return NULL;
4342 d = mpz_get_si (dim->value.integer);
4344 if ((d < 1 || d > array->rank)
4345 || (d == array->rank && as && as->type == AS_ASSUMED_SIZE && upper))
4347 gfc_error ("DIM argument at %L is out of bounds", &dim->where);
4348 return &gfc_bad_expr;
4351 if (as && as->type == AS_ASSUMED_RANK)
4352 return NULL;
4354 return simplify_bound_dim (array, kind, d, upper, as, ref, false);
4359 static gfc_expr *
4360 simplify_cobound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind, int upper)
4362 gfc_ref *ref;
4363 gfc_array_spec *as;
4364 int d;
4366 if (array->expr_type != EXPR_VARIABLE)
4367 return NULL;
4369 /* Follow any component references. */
4370 as = (array->ts.type == BT_CLASS && array->ts.u.derived->components)
4371 ? array->ts.u.derived->components->as
4372 : array->symtree->n.sym->as;
4373 for (ref = array->ref; ref; ref = ref->next)
4375 switch (ref->type)
4377 case REF_ARRAY:
4378 switch (ref->u.ar.type)
4380 case AR_ELEMENT:
4381 if (ref->u.ar.as->corank > 0)
4383 gcc_assert (as == ref->u.ar.as);
4384 goto done;
4386 as = NULL;
4387 continue;
4389 case AR_FULL:
4390 /* We're done because 'as' has already been set in the
4391 previous iteration. */
4392 goto done;
4394 case AR_UNKNOWN:
4395 return NULL;
4397 case AR_SECTION:
4398 as = ref->u.ar.as;
4399 goto done;
4402 gcc_unreachable ();
4404 case REF_COMPONENT:
4405 as = ref->u.c.component->as;
4406 continue;
4408 case REF_SUBSTRING:
4409 case REF_INQUIRY:
4410 continue;
4414 if (!as)
4415 gcc_unreachable ();
4417 done:
4419 if (as->cotype == AS_DEFERRED || as->cotype == AS_ASSUMED_SHAPE)
4420 return NULL;
4422 if (dim == NULL)
4424 /* Multi-dimensional cobounds. */
4425 gfc_expr *bounds[GFC_MAX_DIMENSIONS];
4426 gfc_expr *e;
4427 int k;
4429 /* Simplify the cobounds for each dimension. */
4430 for (d = 0; d < as->corank; d++)
4432 bounds[d] = simplify_bound_dim (array, kind, d + 1 + as->rank,
4433 upper, as, ref, true);
4434 if (bounds[d] == NULL || bounds[d] == &gfc_bad_expr)
4436 int j;
4438 for (j = 0; j < d; j++)
4439 gfc_free_expr (bounds[j]);
4440 return bounds[d];
4444 /* Allocate the result expression. */
4445 e = gfc_get_expr ();
4446 e->where = array->where;
4447 e->expr_type = EXPR_ARRAY;
4448 e->ts.type = BT_INTEGER;
4449 k = get_kind (BT_INTEGER, kind, upper ? "UCOBOUND" : "LCOBOUND",
4450 gfc_default_integer_kind);
4451 if (k == -1)
4453 gfc_free_expr (e);
4454 return &gfc_bad_expr;
4456 e->ts.kind = k;
4458 /* The result is a rank 1 array; its size is the rank of the first
4459 argument to {L,U}COBOUND. */
4460 e->rank = 1;
4461 e->shape = gfc_get_shape (1);
4462 mpz_init_set_ui (e->shape[0], as->corank);
4464 /* Create the constructor for this array. */
4465 for (d = 0; d < as->corank; d++)
4466 gfc_constructor_append_expr (&e->value.constructor,
4467 bounds[d], &e->where);
4468 return e;
4470 else
4472 /* A DIM argument is specified. */
4473 if (dim->expr_type != EXPR_CONSTANT)
4474 return NULL;
4476 d = mpz_get_si (dim->value.integer);
4478 if (d < 1 || d > as->corank)
4480 gfc_error ("DIM argument at %L is out of bounds", &dim->where);
4481 return &gfc_bad_expr;
4484 return simplify_bound_dim (array, kind, d+as->rank, upper, as, ref, true);
4489 gfc_expr *
4490 gfc_simplify_lbound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
4492 return simplify_bound (array, dim, kind, 0);
4496 gfc_expr *
4497 gfc_simplify_lcobound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
4499 return simplify_cobound (array, dim, kind, 0);
4502 gfc_expr *
4503 gfc_simplify_leadz (gfc_expr *e)
4505 unsigned long lz, bs;
4506 int i;
4508 if (e->expr_type != EXPR_CONSTANT)
4509 return NULL;
4511 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
4512 bs = gfc_integer_kinds[i].bit_size;
4513 if (mpz_cmp_si (e->value.integer, 0) == 0)
4514 lz = bs;
4515 else if (mpz_cmp_si (e->value.integer, 0) < 0)
4516 lz = 0;
4517 else
4518 lz = bs - mpz_sizeinbase (e->value.integer, 2);
4520 return gfc_get_int_expr (gfc_default_integer_kind, &e->where, lz);
4524 /* Check for constant length of a substring. */
4526 static bool
4527 substring_has_constant_len (gfc_expr *e)
4529 gfc_ref *ref;
4530 HOST_WIDE_INT istart, iend, length;
4531 bool equal_length = false;
4533 if (e->ts.type != BT_CHARACTER)
4534 return false;
4536 for (ref = e->ref; ref; ref = ref->next)
4537 if (ref->type != REF_COMPONENT && ref->type != REF_ARRAY)
4538 break;
4540 if (!ref
4541 || ref->type != REF_SUBSTRING
4542 || !ref->u.ss.start
4543 || ref->u.ss.start->expr_type != EXPR_CONSTANT
4544 || !ref->u.ss.end
4545 || ref->u.ss.end->expr_type != EXPR_CONSTANT)
4546 return false;
4548 /* Basic checks on substring starting and ending indices. */
4549 if (!gfc_resolve_substring (ref, &equal_length))
4550 return false;
4552 istart = gfc_mpz_get_hwi (ref->u.ss.start->value.integer);
4553 iend = gfc_mpz_get_hwi (ref->u.ss.end->value.integer);
4555 if (istart <= iend)
4556 length = iend - istart + 1;
4557 else
4558 length = 0;
4560 /* Fix substring length. */
4561 e->value.character.length = length;
4563 return true;
4567 gfc_expr *
4568 gfc_simplify_len (gfc_expr *e, gfc_expr *kind)
4570 gfc_expr *result;
4571 int k = get_kind (BT_INTEGER, kind, "LEN", gfc_default_integer_kind);
4573 if (k == -1)
4574 return &gfc_bad_expr;
4576 if (e->expr_type == EXPR_CONSTANT
4577 || substring_has_constant_len (e))
4579 result = gfc_get_constant_expr (BT_INTEGER, k, &e->where);
4580 mpz_set_si (result->value.integer, e->value.character.length);
4581 return range_check (result, "LEN");
4583 else if (e->ts.u.cl != NULL && e->ts.u.cl->length != NULL
4584 && e->ts.u.cl->length->expr_type == EXPR_CONSTANT
4585 && e->ts.u.cl->length->ts.type == BT_INTEGER)
4587 result = gfc_get_constant_expr (BT_INTEGER, k, &e->where);
4588 mpz_set (result->value.integer, e->ts.u.cl->length->value.integer);
4589 return range_check (result, "LEN");
4591 else if (e->expr_type == EXPR_VARIABLE && e->ts.type == BT_CHARACTER
4592 && e->symtree->n.sym
4593 && e->symtree->n.sym->ts.type != BT_DERIVED
4594 && e->symtree->n.sym->assoc && e->symtree->n.sym->assoc->target
4595 && e->symtree->n.sym->assoc->target->ts.type == BT_DERIVED
4596 && e->symtree->n.sym->assoc->target->symtree->n.sym
4597 && UNLIMITED_POLY (e->symtree->n.sym->assoc->target->symtree->n.sym))
4599 /* The expression in assoc->target points to a ref to the _data component
4600 of the unlimited polymorphic entity. To get the _len component the last
4601 _data ref needs to be stripped and a ref to the _len component added. */
4602 return gfc_get_len_component (e->symtree->n.sym->assoc->target, k);
4603 else
4604 return NULL;
4608 gfc_expr *
4609 gfc_simplify_len_trim (gfc_expr *e, gfc_expr *kind)
4611 gfc_expr *result;
4612 size_t count, len, i;
4613 int k = get_kind (BT_INTEGER, kind, "LEN_TRIM", gfc_default_integer_kind);
4615 if (k == -1)
4616 return &gfc_bad_expr;
4618 if (e->expr_type != EXPR_CONSTANT)
4619 return NULL;
4621 len = e->value.character.length;
4622 for (count = 0, i = 1; i <= len; i++)
4623 if (e->value.character.string[len - i] == ' ')
4624 count++;
4625 else
4626 break;
4628 result = gfc_get_int_expr (k, &e->where, len - count);
4629 return range_check (result, "LEN_TRIM");
4632 gfc_expr *
4633 gfc_simplify_lgamma (gfc_expr *x)
4635 gfc_expr *result;
4636 int sg;
4638 if (x->expr_type != EXPR_CONSTANT)
4639 return NULL;
4641 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
4642 mpfr_lgamma (result->value.real, &sg, x->value.real, GFC_RND_MODE);
4644 return range_check (result, "LGAMMA");
4648 gfc_expr *
4649 gfc_simplify_lge (gfc_expr *a, gfc_expr *b)
4651 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
4652 return NULL;
4654 return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
4655 gfc_compare_string (a, b) >= 0);
4659 gfc_expr *
4660 gfc_simplify_lgt (gfc_expr *a, gfc_expr *b)
4662 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
4663 return NULL;
4665 return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
4666 gfc_compare_string (a, b) > 0);
4670 gfc_expr *
4671 gfc_simplify_lle (gfc_expr *a, gfc_expr *b)
4673 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
4674 return NULL;
4676 return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
4677 gfc_compare_string (a, b) <= 0);
4681 gfc_expr *
4682 gfc_simplify_llt (gfc_expr *a, gfc_expr *b)
4684 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
4685 return NULL;
4687 return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
4688 gfc_compare_string (a, b) < 0);
4692 gfc_expr *
4693 gfc_simplify_log (gfc_expr *x)
4695 gfc_expr *result;
4697 if (x->expr_type != EXPR_CONSTANT)
4698 return NULL;
4700 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
4702 switch (x->ts.type)
4704 case BT_REAL:
4705 if (mpfr_sgn (x->value.real) <= 0)
4707 gfc_error ("Argument of LOG at %L cannot be less than or equal "
4708 "to zero", &x->where);
4709 gfc_free_expr (result);
4710 return &gfc_bad_expr;
4713 mpfr_log (result->value.real, x->value.real, GFC_RND_MODE);
4714 break;
4716 case BT_COMPLEX:
4717 if (mpfr_zero_p (mpc_realref (x->value.complex))
4718 && mpfr_zero_p (mpc_imagref (x->value.complex)))
4720 gfc_error ("Complex argument of LOG at %L cannot be zero",
4721 &x->where);
4722 gfc_free_expr (result);
4723 return &gfc_bad_expr;
4726 gfc_set_model_kind (x->ts.kind);
4727 mpc_log (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
4728 break;
4730 default:
4731 gfc_internal_error ("gfc_simplify_log: bad type");
4734 return range_check (result, "LOG");
4738 gfc_expr *
4739 gfc_simplify_log10 (gfc_expr *x)
4741 gfc_expr *result;
4743 if (x->expr_type != EXPR_CONSTANT)
4744 return NULL;
4746 if (mpfr_sgn (x->value.real) <= 0)
4748 gfc_error ("Argument of LOG10 at %L cannot be less than or equal "
4749 "to zero", &x->where);
4750 return &gfc_bad_expr;
4753 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
4754 mpfr_log10 (result->value.real, x->value.real, GFC_RND_MODE);
4756 return range_check (result, "LOG10");
4760 gfc_expr *
4761 gfc_simplify_logical (gfc_expr *e, gfc_expr *k)
4763 int kind;
4765 kind = get_kind (BT_LOGICAL, k, "LOGICAL", gfc_default_logical_kind);
4766 if (kind < 0)
4767 return &gfc_bad_expr;
4769 if (e->expr_type != EXPR_CONSTANT)
4770 return NULL;
4772 return gfc_get_logical_expr (kind, &e->where, e->value.logical);
4776 gfc_expr*
4777 gfc_simplify_matmul (gfc_expr *matrix_a, gfc_expr *matrix_b)
4779 gfc_expr *result;
4780 int row, result_rows, col, result_columns;
4781 int stride_a, offset_a, stride_b, offset_b;
4783 if (!is_constant_array_expr (matrix_a)
4784 || !is_constant_array_expr (matrix_b))
4785 return NULL;
4787 /* MATMUL should do mixed-mode arithmetic. Set the result type. */
4788 if (matrix_a->ts.type != matrix_b->ts.type)
4790 gfc_expr e;
4791 e.expr_type = EXPR_OP;
4792 gfc_clear_ts (&e.ts);
4793 e.value.op.op = INTRINSIC_NONE;
4794 e.value.op.op1 = matrix_a;
4795 e.value.op.op2 = matrix_b;
4796 gfc_type_convert_binary (&e, 1);
4797 result = gfc_get_array_expr (e.ts.type, e.ts.kind, &matrix_a->where);
4799 else
4801 result = gfc_get_array_expr (matrix_a->ts.type, matrix_a->ts.kind,
4802 &matrix_a->where);
4805 if (matrix_a->rank == 1 && matrix_b->rank == 2)
4807 result_rows = 1;
4808 result_columns = mpz_get_si (matrix_b->shape[1]);
4809 stride_a = 1;
4810 stride_b = mpz_get_si (matrix_b->shape[0]);
4812 result->rank = 1;
4813 result->shape = gfc_get_shape (result->rank);
4814 mpz_init_set_si (result->shape[0], result_columns);
4816 else if (matrix_a->rank == 2 && matrix_b->rank == 1)
4818 result_rows = mpz_get_si (matrix_a->shape[0]);
4819 result_columns = 1;
4820 stride_a = mpz_get_si (matrix_a->shape[0]);
4821 stride_b = 1;
4823 result->rank = 1;
4824 result->shape = gfc_get_shape (result->rank);
4825 mpz_init_set_si (result->shape[0], result_rows);
4827 else if (matrix_a->rank == 2 && matrix_b->rank == 2)
4829 result_rows = mpz_get_si (matrix_a->shape[0]);
4830 result_columns = mpz_get_si (matrix_b->shape[1]);
4831 stride_a = mpz_get_si (matrix_a->shape[0]);
4832 stride_b = mpz_get_si (matrix_b->shape[0]);
4834 result->rank = 2;
4835 result->shape = gfc_get_shape (result->rank);
4836 mpz_init_set_si (result->shape[0], result_rows);
4837 mpz_init_set_si (result->shape[1], result_columns);
4839 else
4840 gcc_unreachable();
4842 offset_b = 0;
4843 for (col = 0; col < result_columns; ++col)
4845 offset_a = 0;
4847 for (row = 0; row < result_rows; ++row)
4849 gfc_expr *e = compute_dot_product (matrix_a, stride_a, offset_a,
4850 matrix_b, 1, offset_b, false);
4851 gfc_constructor_append_expr (&result->value.constructor,
4852 e, NULL);
4854 offset_a += 1;
4857 offset_b += stride_b;
4860 return result;
4864 gfc_expr *
4865 gfc_simplify_maskr (gfc_expr *i, gfc_expr *kind_arg)
4867 gfc_expr *result;
4868 int kind, arg, k;
4870 if (i->expr_type != EXPR_CONSTANT)
4871 return NULL;
4873 kind = get_kind (BT_INTEGER, kind_arg, "MASKR", gfc_default_integer_kind);
4874 if (kind == -1)
4875 return &gfc_bad_expr;
4876 k = gfc_validate_kind (BT_INTEGER, kind, false);
4878 bool fail = gfc_extract_int (i, &arg);
4879 gcc_assert (!fail);
4881 result = gfc_get_constant_expr (BT_INTEGER, kind, &i->where);
4883 /* MASKR(n) = 2^n - 1 */
4884 mpz_set_ui (result->value.integer, 1);
4885 mpz_mul_2exp (result->value.integer, result->value.integer, arg);
4886 mpz_sub_ui (result->value.integer, result->value.integer, 1);
4888 gfc_convert_mpz_to_signed (result->value.integer, gfc_integer_kinds[k].bit_size);
4890 return result;
4894 gfc_expr *
4895 gfc_simplify_maskl (gfc_expr *i, gfc_expr *kind_arg)
4897 gfc_expr *result;
4898 int kind, arg, k;
4899 mpz_t z;
4901 if (i->expr_type != EXPR_CONSTANT)
4902 return NULL;
4904 kind = get_kind (BT_INTEGER, kind_arg, "MASKL", gfc_default_integer_kind);
4905 if (kind == -1)
4906 return &gfc_bad_expr;
4907 k = gfc_validate_kind (BT_INTEGER, kind, false);
4909 bool fail = gfc_extract_int (i, &arg);
4910 gcc_assert (!fail);
4912 result = gfc_get_constant_expr (BT_INTEGER, kind, &i->where);
4914 /* MASKL(n) = 2^bit_size - 2^(bit_size - n) */
4915 mpz_init_set_ui (z, 1);
4916 mpz_mul_2exp (z, z, gfc_integer_kinds[k].bit_size);
4917 mpz_set_ui (result->value.integer, 1);
4918 mpz_mul_2exp (result->value.integer, result->value.integer,
4919 gfc_integer_kinds[k].bit_size - arg);
4920 mpz_sub (result->value.integer, z, result->value.integer);
4921 mpz_clear (z);
4923 gfc_convert_mpz_to_signed (result->value.integer, gfc_integer_kinds[k].bit_size);
4925 return result;
4929 gfc_expr *
4930 gfc_simplify_merge (gfc_expr *tsource, gfc_expr *fsource, gfc_expr *mask)
4932 gfc_expr * result;
4933 gfc_constructor *tsource_ctor, *fsource_ctor, *mask_ctor;
4935 if (mask->expr_type == EXPR_CONSTANT)
4937 result = gfc_copy_expr (mask->value.logical ? tsource : fsource);
4938 /* Parenthesis is needed to get lower bounds of 1. */
4939 result = gfc_get_parentheses (result);
4940 gfc_simplify_expr (result, 1);
4941 return result;
4944 if (!mask->rank || !is_constant_array_expr (mask)
4945 || !is_constant_array_expr (tsource) || !is_constant_array_expr (fsource))
4946 return NULL;
4948 result = gfc_get_array_expr (tsource->ts.type, tsource->ts.kind,
4949 &tsource->where);
4950 if (tsource->ts.type == BT_DERIVED)
4951 result->ts.u.derived = tsource->ts.u.derived;
4952 else if (tsource->ts.type == BT_CHARACTER)
4953 result->ts.u.cl = tsource->ts.u.cl;
4955 tsource_ctor = gfc_constructor_first (tsource->value.constructor);
4956 fsource_ctor = gfc_constructor_first (fsource->value.constructor);
4957 mask_ctor = gfc_constructor_first (mask->value.constructor);
4959 while (mask_ctor)
4961 if (mask_ctor->expr->value.logical)
4962 gfc_constructor_append_expr (&result->value.constructor,
4963 gfc_copy_expr (tsource_ctor->expr),
4964 NULL);
4965 else
4966 gfc_constructor_append_expr (&result->value.constructor,
4967 gfc_copy_expr (fsource_ctor->expr),
4968 NULL);
4969 tsource_ctor = gfc_constructor_next (tsource_ctor);
4970 fsource_ctor = gfc_constructor_next (fsource_ctor);
4971 mask_ctor = gfc_constructor_next (mask_ctor);
4974 result->shape = gfc_get_shape (1);
4975 gfc_array_size (result, &result->shape[0]);
4977 return result;
4981 gfc_expr *
4982 gfc_simplify_merge_bits (gfc_expr *i, gfc_expr *j, gfc_expr *mask_expr)
4984 mpz_t arg1, arg2, mask;
4985 gfc_expr *result;
4987 if (i->expr_type != EXPR_CONSTANT || j->expr_type != EXPR_CONSTANT
4988 || mask_expr->expr_type != EXPR_CONSTANT)
4989 return NULL;
4991 result = gfc_get_constant_expr (BT_INTEGER, i->ts.kind, &i->where);
4993 /* Convert all argument to unsigned. */
4994 mpz_init_set (arg1, i->value.integer);
4995 mpz_init_set (arg2, j->value.integer);
4996 mpz_init_set (mask, mask_expr->value.integer);
4998 /* MERGE_BITS(I,J,MASK) = IOR (IAND (I, MASK), IAND (J, NOT (MASK))). */
4999 mpz_and (arg1, arg1, mask);
5000 mpz_com (mask, mask);
5001 mpz_and (arg2, arg2, mask);
5002 mpz_ior (result->value.integer, arg1, arg2);
5004 mpz_clear (arg1);
5005 mpz_clear (arg2);
5006 mpz_clear (mask);
5008 return result;
5012 /* Selects between current value and extremum for simplify_min_max
5013 and simplify_minval_maxval. */
5014 static int
5015 min_max_choose (gfc_expr *arg, gfc_expr *extremum, int sign, bool back_val)
5017 int ret;
5019 switch (arg->ts.type)
5021 case BT_INTEGER:
5022 if (extremum->ts.kind < arg->ts.kind)
5023 extremum->ts.kind = arg->ts.kind;
5024 ret = mpz_cmp (arg->value.integer,
5025 extremum->value.integer) * sign;
5026 if (ret > 0)
5027 mpz_set (extremum->value.integer, arg->value.integer);
5028 break;
5030 case BT_REAL:
5031 if (extremum->ts.kind < arg->ts.kind)
5032 extremum->ts.kind = arg->ts.kind;
5033 if (mpfr_nan_p (extremum->value.real))
5035 ret = 1;
5036 mpfr_set (extremum->value.real, arg->value.real, GFC_RND_MODE);
5038 else if (mpfr_nan_p (arg->value.real))
5039 ret = -1;
5040 else
5042 ret = mpfr_cmp (arg->value.real, extremum->value.real) * sign;
5043 if (ret > 0)
5044 mpfr_set (extremum->value.real, arg->value.real, GFC_RND_MODE);
5046 break;
5048 case BT_CHARACTER:
5049 #define LENGTH(x) ((x)->value.character.length)
5050 #define STRING(x) ((x)->value.character.string)
5051 if (LENGTH (extremum) < LENGTH(arg))
5053 gfc_char_t *tmp = STRING(extremum);
5055 STRING(extremum) = gfc_get_wide_string (LENGTH(arg) + 1);
5056 memcpy (STRING(extremum), tmp,
5057 LENGTH(extremum) * sizeof (gfc_char_t));
5058 gfc_wide_memset (&STRING(extremum)[LENGTH(extremum)], ' ',
5059 LENGTH(arg) - LENGTH(extremum));
5060 STRING(extremum)[LENGTH(arg)] = '\0'; /* For debugger */
5061 LENGTH(extremum) = LENGTH(arg);
5062 free (tmp);
5064 ret = gfc_compare_string (arg, extremum) * sign;
5065 if (ret > 0)
5067 free (STRING(extremum));
5068 STRING(extremum) = gfc_get_wide_string (LENGTH(extremum) + 1);
5069 memcpy (STRING(extremum), STRING(arg),
5070 LENGTH(arg) * sizeof (gfc_char_t));
5071 gfc_wide_memset (&STRING(extremum)[LENGTH(arg)], ' ',
5072 LENGTH(extremum) - LENGTH(arg));
5073 STRING(extremum)[LENGTH(extremum)] = '\0'; /* For debugger */
5075 #undef LENGTH
5076 #undef STRING
5077 break;
5079 default:
5080 gfc_internal_error ("simplify_min_max(): Bad type in arglist");
5082 if (back_val && ret == 0)
5083 ret = 1;
5085 return ret;
5089 /* This function is special since MAX() can take any number of
5090 arguments. The simplified expression is a rewritten version of the
5091 argument list containing at most one constant element. Other
5092 constant elements are deleted. Because the argument list has
5093 already been checked, this function always succeeds. sign is 1 for
5094 MAX(), -1 for MIN(). */
5096 static gfc_expr *
5097 simplify_min_max (gfc_expr *expr, int sign)
5099 int tmp1, tmp2;
5100 gfc_actual_arglist *arg, *last, *extremum;
5101 gfc_expr *tmp, *ret;
5102 const char *fname;
5104 last = NULL;
5105 extremum = NULL;
5107 arg = expr->value.function.actual;
5109 for (; arg; last = arg, arg = arg->next)
5111 if (arg->expr->expr_type != EXPR_CONSTANT)
5112 continue;
5114 if (extremum == NULL)
5116 extremum = arg;
5117 continue;
5120 min_max_choose (arg->expr, extremum->expr, sign);
5122 /* Delete the extra constant argument. */
5123 last->next = arg->next;
5125 arg->next = NULL;
5126 gfc_free_actual_arglist (arg);
5127 arg = last;
5130 /* If there is one value left, replace the function call with the
5131 expression. */
5132 if (expr->value.function.actual->next != NULL)
5133 return NULL;
5135 /* Handle special cases of specific functions (min|max)1 and
5136 a(min|max)0. */
5138 tmp = expr->value.function.actual->expr;
5139 fname = expr->value.function.isym->name;
5141 if ((tmp->ts.type != BT_INTEGER || tmp->ts.kind != gfc_integer_4_kind)
5142 && (strcmp (fname, "min1") == 0 || strcmp (fname, "max1") == 0))
5144 /* Explicit conversion, turn off -Wconversion and -Wconversion-extra
5145 warnings. */
5146 tmp1 = warn_conversion;
5147 tmp2 = warn_conversion_extra;
5148 warn_conversion = warn_conversion_extra = 0;
5150 ret = gfc_convert_constant (tmp, BT_INTEGER, gfc_integer_4_kind);
5152 warn_conversion = tmp1;
5153 warn_conversion_extra = tmp2;
5155 else if ((tmp->ts.type != BT_REAL || tmp->ts.kind != gfc_real_4_kind)
5156 && (strcmp (fname, "amin0") == 0 || strcmp (fname, "amax0") == 0))
5158 ret = gfc_convert_constant (tmp, BT_REAL, gfc_real_4_kind);
5160 else
5161 ret = gfc_copy_expr (tmp);
5163 return ret;
5168 gfc_expr *
5169 gfc_simplify_min (gfc_expr *e)
5171 return simplify_min_max (e, -1);
5175 gfc_expr *
5176 gfc_simplify_max (gfc_expr *e)
5178 return simplify_min_max (e, 1);
5181 /* Helper function for gfc_simplify_minval. */
5183 static gfc_expr *
5184 gfc_min (gfc_expr *op1, gfc_expr *op2)
5186 min_max_choose (op1, op2, -1);
5187 gfc_free_expr (op1);
5188 return op2;
5191 /* Simplify minval for constant arrays. */
5193 gfc_expr *
5194 gfc_simplify_minval (gfc_expr *array, gfc_expr* dim, gfc_expr *mask)
5196 return simplify_transformation (array, dim, mask, INT_MAX, gfc_min);
5199 /* Helper function for gfc_simplify_maxval. */
5201 static gfc_expr *
5202 gfc_max (gfc_expr *op1, gfc_expr *op2)
5204 min_max_choose (op1, op2, 1);
5205 gfc_free_expr (op1);
5206 return op2;
5210 /* Simplify maxval for constant arrays. */
5212 gfc_expr *
5213 gfc_simplify_maxval (gfc_expr *array, gfc_expr* dim, gfc_expr *mask)
5215 return simplify_transformation (array, dim, mask, INT_MIN, gfc_max);
5219 /* Transform minloc or maxloc of an array, according to MASK,
5220 to the scalar result. This code is mostly identical to
5221 simplify_transformation_to_scalar. */
5223 static gfc_expr *
5224 simplify_minmaxloc_to_scalar (gfc_expr *result, gfc_expr *array, gfc_expr *mask,
5225 gfc_expr *extremum, int sign, bool back_val)
5227 gfc_expr *a, *m;
5228 gfc_constructor *array_ctor, *mask_ctor;
5229 mpz_t count;
5231 mpz_set_si (result->value.integer, 0);
5234 /* Shortcut for constant .FALSE. MASK. */
5235 if (mask
5236 && mask->expr_type == EXPR_CONSTANT
5237 && !mask->value.logical)
5238 return result;
5240 array_ctor = gfc_constructor_first (array->value.constructor);
5241 if (mask && mask->expr_type == EXPR_ARRAY)
5242 mask_ctor = gfc_constructor_first (mask->value.constructor);
5243 else
5244 mask_ctor = NULL;
5246 mpz_init_set_si (count, 0);
5247 while (array_ctor)
5249 mpz_add_ui (count, count, 1);
5250 a = array_ctor->expr;
5251 array_ctor = gfc_constructor_next (array_ctor);
5252 /* A constant MASK equals .TRUE. here and can be ignored. */
5253 if (mask_ctor)
5255 m = mask_ctor->expr;
5256 mask_ctor = gfc_constructor_next (mask_ctor);
5257 if (!m->value.logical)
5258 continue;
5260 if (min_max_choose (a, extremum, sign, back_val) > 0)
5261 mpz_set (result->value.integer, count);
5263 mpz_clear (count);
5264 gfc_free_expr (extremum);
5265 return result;
5268 /* Simplify minloc / maxloc in the absence of a dim argument. */
5270 static gfc_expr *
5271 simplify_minmaxloc_nodim (gfc_expr *result, gfc_expr *extremum,
5272 gfc_expr *array, gfc_expr *mask, int sign,
5273 bool back_val)
5275 ssize_t res[GFC_MAX_DIMENSIONS];
5276 int i, n;
5277 gfc_constructor *result_ctor, *array_ctor, *mask_ctor;
5278 ssize_t count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS],
5279 sstride[GFC_MAX_DIMENSIONS];
5280 gfc_expr *a, *m;
5281 bool continue_loop;
5282 bool ma;
5284 for (i = 0; i<array->rank; i++)
5285 res[i] = -1;
5287 /* Shortcut for constant .FALSE. MASK. */
5288 if (mask
5289 && mask->expr_type == EXPR_CONSTANT
5290 && !mask->value.logical)
5291 goto finish;
5293 if (array->shape == NULL)
5294 goto finish;
5296 for (i = 0; i < array->rank; i++)
5298 count[i] = 0;
5299 sstride[i] = (i == 0) ? 1 : sstride[i-1] * mpz_get_si (array->shape[i-1]);
5300 extent[i] = mpz_get_si (array->shape[i]);
5301 if (extent[i] <= 0)
5302 goto finish;
5305 continue_loop = true;
5306 array_ctor = gfc_constructor_first (array->value.constructor);
5307 if (mask && mask->rank > 0)
5308 mask_ctor = gfc_constructor_first (mask->value.constructor);
5309 else
5310 mask_ctor = NULL;
5312 /* Loop over the array elements (and mask), keeping track of
5313 the indices to return. */
5314 while (continue_loop)
5318 a = array_ctor->expr;
5319 if (mask_ctor)
5321 m = mask_ctor->expr;
5322 ma = m->value.logical;
5323 mask_ctor = gfc_constructor_next (mask_ctor);
5325 else
5326 ma = true;
5328 if (ma && min_max_choose (a, extremum, sign, back_val) > 0)
5330 for (i = 0; i<array->rank; i++)
5331 res[i] = count[i];
5333 array_ctor = gfc_constructor_next (array_ctor);
5334 count[0] ++;
5335 } while (count[0] != extent[0]);
5336 n = 0;
5339 /* When we get to the end of a dimension, reset it and increment
5340 the next dimension. */
5341 count[n] = 0;
5342 n++;
5343 if (n >= array->rank)
5345 continue_loop = false;
5346 break;
5348 else
5349 count[n] ++;
5350 } while (count[n] == extent[n]);
5353 finish:
5354 gfc_free_expr (extremum);
5355 result_ctor = gfc_constructor_first (result->value.constructor);
5356 for (i = 0; i<array->rank; i++)
5358 gfc_expr *r_expr;
5359 r_expr = result_ctor->expr;
5360 mpz_set_si (r_expr->value.integer, res[i] + 1);
5361 result_ctor = gfc_constructor_next (result_ctor);
5363 return result;
5366 /* Helper function for gfc_simplify_minmaxloc - build an array
5367 expression with n elements. */
5369 static gfc_expr *
5370 new_array (bt type, int kind, int n, locus *where)
5372 gfc_expr *result;
5373 int i;
5375 result = gfc_get_array_expr (type, kind, where);
5376 result->rank = 1;
5377 result->shape = gfc_get_shape(1);
5378 mpz_init_set_si (result->shape[0], n);
5379 for (i = 0; i < n; i++)
5381 gfc_constructor_append_expr (&result->value.constructor,
5382 gfc_get_constant_expr (type, kind, where),
5383 NULL);
5386 return result;
5389 /* Simplify minloc and maxloc. This code is mostly identical to
5390 simplify_transformation_to_array. */
5392 static gfc_expr *
5393 simplify_minmaxloc_to_array (gfc_expr *result, gfc_expr *array,
5394 gfc_expr *dim, gfc_expr *mask,
5395 gfc_expr *extremum, int sign, bool back_val)
5397 mpz_t size;
5398 int done, i, n, arraysize, resultsize, dim_index, dim_extent, dim_stride;
5399 gfc_expr **arrayvec, **resultvec, **base, **src, **dest;
5400 gfc_constructor *array_ctor, *mask_ctor, *result_ctor;
5402 int count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS],
5403 sstride[GFC_MAX_DIMENSIONS], dstride[GFC_MAX_DIMENSIONS],
5404 tmpstride[GFC_MAX_DIMENSIONS];
5406 /* Shortcut for constant .FALSE. MASK. */
5407 if (mask
5408 && mask->expr_type == EXPR_CONSTANT
5409 && !mask->value.logical)
5410 return result;
5412 /* Build an indexed table for array element expressions to minimize
5413 linked-list traversal. Masked elements are set to NULL. */
5414 gfc_array_size (array, &size);
5415 arraysize = mpz_get_ui (size);
5416 mpz_clear (size);
5418 arrayvec = XCNEWVEC (gfc_expr*, arraysize);
5420 array_ctor = gfc_constructor_first (array->value.constructor);
5421 mask_ctor = NULL;
5422 if (mask && mask->expr_type == EXPR_ARRAY)
5423 mask_ctor = gfc_constructor_first (mask->value.constructor);
5425 for (i = 0; i < arraysize; ++i)
5427 arrayvec[i] = array_ctor->expr;
5428 array_ctor = gfc_constructor_next (array_ctor);
5430 if (mask_ctor)
5432 if (!mask_ctor->expr->value.logical)
5433 arrayvec[i] = NULL;
5435 mask_ctor = gfc_constructor_next (mask_ctor);
5439 /* Same for the result expression. */
5440 gfc_array_size (result, &size);
5441 resultsize = mpz_get_ui (size);
5442 mpz_clear (size);
5444 resultvec = XCNEWVEC (gfc_expr*, resultsize);
5445 result_ctor = gfc_constructor_first (result->value.constructor);
5446 for (i = 0; i < resultsize; ++i)
5448 resultvec[i] = result_ctor->expr;
5449 result_ctor = gfc_constructor_next (result_ctor);
5452 gfc_extract_int (dim, &dim_index);
5453 dim_index -= 1; /* zero-base index */
5454 dim_extent = 0;
5455 dim_stride = 0;
5457 for (i = 0, n = 0; i < array->rank; ++i)
5459 count[i] = 0;
5460 tmpstride[i] = (i == 0) ? 1 : tmpstride[i-1] * mpz_get_si (array->shape[i-1]);
5461 if (i == dim_index)
5463 dim_extent = mpz_get_si (array->shape[i]);
5464 dim_stride = tmpstride[i];
5465 continue;
5468 extent[n] = mpz_get_si (array->shape[i]);
5469 sstride[n] = tmpstride[i];
5470 dstride[n] = (n == 0) ? 1 : dstride[n-1] * extent[n-1];
5471 n += 1;
5474 done = resultsize <= 0;
5475 base = arrayvec;
5476 dest = resultvec;
5477 while (!done)
5479 gfc_expr *ex;
5480 ex = gfc_copy_expr (extremum);
5481 for (src = base, n = 0; n < dim_extent; src += dim_stride, ++n)
5483 if (*src && min_max_choose (*src, ex, sign, back_val) > 0)
5484 mpz_set_si ((*dest)->value.integer, n + 1);
5487 count[0]++;
5488 base += sstride[0];
5489 dest += dstride[0];
5490 gfc_free_expr (ex);
5492 n = 0;
5493 while (!done && count[n] == extent[n])
5495 count[n] = 0;
5496 base -= sstride[n] * extent[n];
5497 dest -= dstride[n] * extent[n];
5499 n++;
5500 if (n < result->rank)
5502 /* If the nested loop is unrolled GFC_MAX_DIMENSIONS
5503 times, we'd warn for the last iteration, because the
5504 array index will have already been incremented to the
5505 array sizes, and we can't tell that this must make
5506 the test against result->rank false, because ranks
5507 must not exceed GFC_MAX_DIMENSIONS. */
5508 GCC_DIAGNOSTIC_PUSH_IGNORED (-Warray-bounds)
5509 count[n]++;
5510 base += sstride[n];
5511 dest += dstride[n];
5512 GCC_DIAGNOSTIC_POP
5514 else
5515 done = true;
5519 /* Place updated expression in result constructor. */
5520 result_ctor = gfc_constructor_first (result->value.constructor);
5521 for (i = 0; i < resultsize; ++i)
5523 result_ctor->expr = resultvec[i];
5524 result_ctor = gfc_constructor_next (result_ctor);
5527 free (arrayvec);
5528 free (resultvec);
5529 free (extremum);
5530 return result;
5533 /* Simplify minloc and maxloc for constant arrays. */
5535 static gfc_expr *
5536 gfc_simplify_minmaxloc (gfc_expr *array, gfc_expr *dim, gfc_expr *mask,
5537 gfc_expr *kind, gfc_expr *back, int sign)
5539 gfc_expr *result;
5540 gfc_expr *extremum;
5541 int ikind;
5542 int init_val;
5543 bool back_val = false;
5545 if (!is_constant_array_expr (array)
5546 || !gfc_is_constant_expr (dim))
5547 return NULL;
5549 if (mask
5550 && !is_constant_array_expr (mask)
5551 && mask->expr_type != EXPR_CONSTANT)
5552 return NULL;
5554 if (kind)
5556 if (gfc_extract_int (kind, &ikind, -1))
5557 return NULL;
5559 else
5560 ikind = gfc_default_integer_kind;
5562 if (back)
5564 if (back->expr_type != EXPR_CONSTANT)
5565 return NULL;
5567 back_val = back->value.logical;
5570 if (sign < 0)
5571 init_val = INT_MAX;
5572 else if (sign > 0)
5573 init_val = INT_MIN;
5574 else
5575 gcc_unreachable();
5577 extremum = gfc_get_constant_expr (array->ts.type, array->ts.kind, &array->where);
5578 init_result_expr (extremum, init_val, array);
5580 if (dim)
5582 result = transformational_result (array, dim, BT_INTEGER,
5583 ikind, &array->where);
5584 init_result_expr (result, 0, array);
5586 if (array->rank == 1)
5587 return simplify_minmaxloc_to_scalar (result, array, mask, extremum,
5588 sign, back_val);
5589 else
5590 return simplify_minmaxloc_to_array (result, array, dim, mask, extremum,
5591 sign, back_val);
5593 else
5595 result = new_array (BT_INTEGER, ikind, array->rank, &array->where);
5596 return simplify_minmaxloc_nodim (result, extremum, array, mask,
5597 sign, back_val);
5601 gfc_expr *
5602 gfc_simplify_minloc (gfc_expr *array, gfc_expr *dim, gfc_expr *mask, gfc_expr *kind,
5603 gfc_expr *back)
5605 return gfc_simplify_minmaxloc (array, dim, mask, kind, back, -1);
5608 gfc_expr *
5609 gfc_simplify_maxloc (gfc_expr *array, gfc_expr *dim, gfc_expr *mask, gfc_expr *kind,
5610 gfc_expr *back)
5612 return gfc_simplify_minmaxloc (array, dim, mask, kind, back, 1);
5615 /* Simplify findloc to scalar. Similar to
5616 simplify_minmaxloc_to_scalar. */
5618 static gfc_expr *
5619 simplify_findloc_to_scalar (gfc_expr *result, gfc_expr *array, gfc_expr *value,
5620 gfc_expr *mask, int back_val)
5622 gfc_expr *a, *m;
5623 gfc_constructor *array_ctor, *mask_ctor;
5624 mpz_t count;
5626 mpz_set_si (result->value.integer, 0);
5628 /* Shortcut for constant .FALSE. MASK. */
5629 if (mask
5630 && mask->expr_type == EXPR_CONSTANT
5631 && !mask->value.logical)
5632 return result;
5634 array_ctor = gfc_constructor_first (array->value.constructor);
5635 if (mask && mask->expr_type == EXPR_ARRAY)
5636 mask_ctor = gfc_constructor_first (mask->value.constructor);
5637 else
5638 mask_ctor = NULL;
5640 mpz_init_set_si (count, 0);
5641 while (array_ctor)
5643 mpz_add_ui (count, count, 1);
5644 a = array_ctor->expr;
5645 array_ctor = gfc_constructor_next (array_ctor);
5646 /* A constant MASK equals .TRUE. here and can be ignored. */
5647 if (mask_ctor)
5649 m = mask_ctor->expr;
5650 mask_ctor = gfc_constructor_next (mask_ctor);
5651 if (!m->value.logical)
5652 continue;
5654 if (gfc_compare_expr (a, value, INTRINSIC_EQ) == 0)
5656 /* We have a match. If BACK is true, continue so we find
5657 the last one. */
5658 mpz_set (result->value.integer, count);
5659 if (!back_val)
5660 break;
5663 mpz_clear (count);
5664 return result;
5667 /* Simplify findloc in the absence of a dim argument. Similar to
5668 simplify_minmaxloc_nodim. */
5670 static gfc_expr *
5671 simplify_findloc_nodim (gfc_expr *result, gfc_expr *value, gfc_expr *array,
5672 gfc_expr *mask, bool back_val)
5674 ssize_t res[GFC_MAX_DIMENSIONS];
5675 int i, n;
5676 gfc_constructor *result_ctor, *array_ctor, *mask_ctor;
5677 ssize_t count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS],
5678 sstride[GFC_MAX_DIMENSIONS];
5679 gfc_expr *a, *m;
5680 bool continue_loop;
5681 bool ma;
5683 for (i = 0; i < array->rank; i++)
5684 res[i] = -1;
5686 /* Shortcut for constant .FALSE. MASK. */
5687 if (mask
5688 && mask->expr_type == EXPR_CONSTANT
5689 && !mask->value.logical)
5690 goto finish;
5692 for (i = 0; i < array->rank; i++)
5694 count[i] = 0;
5695 sstride[i] = (i == 0) ? 1 : sstride[i-1] * mpz_get_si (array->shape[i-1]);
5696 extent[i] = mpz_get_si (array->shape[i]);
5697 if (extent[i] <= 0)
5698 goto finish;
5701 continue_loop = true;
5702 array_ctor = gfc_constructor_first (array->value.constructor);
5703 if (mask && mask->rank > 0)
5704 mask_ctor = gfc_constructor_first (mask->value.constructor);
5705 else
5706 mask_ctor = NULL;
5708 /* Loop over the array elements (and mask), keeping track of
5709 the indices to return. */
5710 while (continue_loop)
5714 a = array_ctor->expr;
5715 if (mask_ctor)
5717 m = mask_ctor->expr;
5718 ma = m->value.logical;
5719 mask_ctor = gfc_constructor_next (mask_ctor);
5721 else
5722 ma = true;
5724 if (ma && gfc_compare_expr (a, value, INTRINSIC_EQ) == 0)
5726 for (i = 0; i < array->rank; i++)
5727 res[i] = count[i];
5728 if (!back_val)
5729 goto finish;
5731 array_ctor = gfc_constructor_next (array_ctor);
5732 count[0] ++;
5733 } while (count[0] != extent[0]);
5734 n = 0;
5737 /* When we get to the end of a dimension, reset it and increment
5738 the next dimension. */
5739 count[n] = 0;
5740 n++;
5741 if (n >= array->rank)
5743 continue_loop = false;
5744 break;
5746 else
5747 count[n] ++;
5748 } while (count[n] == extent[n]);
5751 finish:
5752 result_ctor = gfc_constructor_first (result->value.constructor);
5753 for (i = 0; i < array->rank; i++)
5755 gfc_expr *r_expr;
5756 r_expr = result_ctor->expr;
5757 mpz_set_si (r_expr->value.integer, res[i] + 1);
5758 result_ctor = gfc_constructor_next (result_ctor);
5760 return result;
5764 /* Simplify findloc to an array. Similar to
5765 simplify_minmaxloc_to_array. */
5767 static gfc_expr *
5768 simplify_findloc_to_array (gfc_expr *result, gfc_expr *array, gfc_expr *value,
5769 gfc_expr *dim, gfc_expr *mask, bool back_val)
5771 mpz_t size;
5772 int done, i, n, arraysize, resultsize, dim_index, dim_extent, dim_stride;
5773 gfc_expr **arrayvec, **resultvec, **base, **src, **dest;
5774 gfc_constructor *array_ctor, *mask_ctor, *result_ctor;
5776 int count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS],
5777 sstride[GFC_MAX_DIMENSIONS], dstride[GFC_MAX_DIMENSIONS],
5778 tmpstride[GFC_MAX_DIMENSIONS];
5780 /* Shortcut for constant .FALSE. MASK. */
5781 if (mask
5782 && mask->expr_type == EXPR_CONSTANT
5783 && !mask->value.logical)
5784 return result;
5786 /* Build an indexed table for array element expressions to minimize
5787 linked-list traversal. Masked elements are set to NULL. */
5788 gfc_array_size (array, &size);
5789 arraysize = mpz_get_ui (size);
5790 mpz_clear (size);
5792 arrayvec = XCNEWVEC (gfc_expr*, arraysize);
5794 array_ctor = gfc_constructor_first (array->value.constructor);
5795 mask_ctor = NULL;
5796 if (mask && mask->expr_type == EXPR_ARRAY)
5797 mask_ctor = gfc_constructor_first (mask->value.constructor);
5799 for (i = 0; i < arraysize; ++i)
5801 arrayvec[i] = array_ctor->expr;
5802 array_ctor = gfc_constructor_next (array_ctor);
5804 if (mask_ctor)
5806 if (!mask_ctor->expr->value.logical)
5807 arrayvec[i] = NULL;
5809 mask_ctor = gfc_constructor_next (mask_ctor);
5813 /* Same for the result expression. */
5814 gfc_array_size (result, &size);
5815 resultsize = mpz_get_ui (size);
5816 mpz_clear (size);
5818 resultvec = XCNEWVEC (gfc_expr*, resultsize);
5819 result_ctor = gfc_constructor_first (result->value.constructor);
5820 for (i = 0; i < resultsize; ++i)
5822 resultvec[i] = result_ctor->expr;
5823 result_ctor = gfc_constructor_next (result_ctor);
5826 gfc_extract_int (dim, &dim_index);
5828 dim_index -= 1; /* Zero-base index. */
5829 dim_extent = 0;
5830 dim_stride = 0;
5832 for (i = 0, n = 0; i < array->rank; ++i)
5834 count[i] = 0;
5835 tmpstride[i] = (i == 0) ? 1 : tmpstride[i-1] * mpz_get_si (array->shape[i-1]);
5836 if (i == dim_index)
5838 dim_extent = mpz_get_si (array->shape[i]);
5839 dim_stride = tmpstride[i];
5840 continue;
5843 extent[n] = mpz_get_si (array->shape[i]);
5844 sstride[n] = tmpstride[i];
5845 dstride[n] = (n == 0) ? 1 : dstride[n-1] * extent[n-1];
5846 n += 1;
5849 done = resultsize <= 0;
5850 base = arrayvec;
5851 dest = resultvec;
5852 while (!done)
5854 for (src = base, n = 0; n < dim_extent; src += dim_stride, ++n)
5856 if (*src && gfc_compare_expr (*src, value, INTRINSIC_EQ) == 0)
5858 mpz_set_si ((*dest)->value.integer, n + 1);
5859 if (!back_val)
5860 break;
5864 count[0]++;
5865 base += sstride[0];
5866 dest += dstride[0];
5868 n = 0;
5869 while (!done && count[n] == extent[n])
5871 count[n] = 0;
5872 base -= sstride[n] * extent[n];
5873 dest -= dstride[n] * extent[n];
5875 n++;
5876 if (n < result->rank)
5878 /* If the nested loop is unrolled GFC_MAX_DIMENSIONS
5879 times, we'd warn for the last iteration, because the
5880 array index will have already been incremented to the
5881 array sizes, and we can't tell that this must make
5882 the test against result->rank false, because ranks
5883 must not exceed GFC_MAX_DIMENSIONS. */
5884 GCC_DIAGNOSTIC_PUSH_IGNORED (-Warray-bounds)
5885 count[n]++;
5886 base += sstride[n];
5887 dest += dstride[n];
5888 GCC_DIAGNOSTIC_POP
5890 else
5891 done = true;
5895 /* Place updated expression in result constructor. */
5896 result_ctor = gfc_constructor_first (result->value.constructor);
5897 for (i = 0; i < resultsize; ++i)
5899 result_ctor->expr = resultvec[i];
5900 result_ctor = gfc_constructor_next (result_ctor);
5903 free (arrayvec);
5904 free (resultvec);
5905 return result;
5908 /* Simplify findloc. */
5910 gfc_expr *
5911 gfc_simplify_findloc (gfc_expr *array, gfc_expr *value, gfc_expr *dim,
5912 gfc_expr *mask, gfc_expr *kind, gfc_expr *back)
5914 gfc_expr *result;
5915 int ikind;
5916 bool back_val = false;
5918 if (!is_constant_array_expr (array)
5919 || !gfc_is_constant_expr (dim))
5920 return NULL;
5922 if (! gfc_is_constant_expr (value))
5923 return 0;
5925 if (mask
5926 && !is_constant_array_expr (mask)
5927 && mask->expr_type != EXPR_CONSTANT)
5928 return NULL;
5930 if (kind)
5932 if (gfc_extract_int (kind, &ikind, -1))
5933 return NULL;
5935 else
5936 ikind = gfc_default_integer_kind;
5938 if (back)
5940 if (back->expr_type != EXPR_CONSTANT)
5941 return NULL;
5943 back_val = back->value.logical;
5946 if (dim)
5948 result = transformational_result (array, dim, BT_INTEGER,
5949 ikind, &array->where);
5950 init_result_expr (result, 0, array);
5952 if (array->rank == 1)
5953 return simplify_findloc_to_scalar (result, array, value, mask,
5954 back_val);
5955 else
5956 return simplify_findloc_to_array (result, array, value, dim, mask,
5957 back_val);
5959 else
5961 result = new_array (BT_INTEGER, ikind, array->rank, &array->where);
5962 return simplify_findloc_nodim (result, value, array, mask, back_val);
5964 return NULL;
5967 gfc_expr *
5968 gfc_simplify_maxexponent (gfc_expr *x)
5970 int i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
5971 return gfc_get_int_expr (gfc_default_integer_kind, &x->where,
5972 gfc_real_kinds[i].max_exponent);
5976 gfc_expr *
5977 gfc_simplify_minexponent (gfc_expr *x)
5979 int i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
5980 return gfc_get_int_expr (gfc_default_integer_kind, &x->where,
5981 gfc_real_kinds[i].min_exponent);
5985 gfc_expr *
5986 gfc_simplify_mod (gfc_expr *a, gfc_expr *p)
5988 gfc_expr *result;
5989 int kind;
5991 /* First check p. */
5992 if (p->expr_type != EXPR_CONSTANT)
5993 return NULL;
5995 /* p shall not be 0. */
5996 switch (p->ts.type)
5998 case BT_INTEGER:
5999 if (mpz_cmp_ui (p->value.integer, 0) == 0)
6001 gfc_error ("Argument %qs of MOD at %L shall not be zero",
6002 "P", &p->where);
6003 return &gfc_bad_expr;
6005 break;
6006 case BT_REAL:
6007 if (mpfr_cmp_ui (p->value.real, 0) == 0)
6009 gfc_error ("Argument %qs of MOD at %L shall not be zero",
6010 "P", &p->where);
6011 return &gfc_bad_expr;
6013 break;
6014 default:
6015 gfc_internal_error ("gfc_simplify_mod(): Bad arguments");
6018 if (a->expr_type != EXPR_CONSTANT)
6019 return NULL;
6021 kind = a->ts.kind > p->ts.kind ? a->ts.kind : p->ts.kind;
6022 result = gfc_get_constant_expr (a->ts.type, kind, &a->where);
6024 if (a->ts.type == BT_INTEGER)
6025 mpz_tdiv_r (result->value.integer, a->value.integer, p->value.integer);
6026 else
6028 gfc_set_model_kind (kind);
6029 mpfr_fmod (result->value.real, a->value.real, p->value.real,
6030 GFC_RND_MODE);
6033 return range_check (result, "MOD");
6037 gfc_expr *
6038 gfc_simplify_modulo (gfc_expr *a, gfc_expr *p)
6040 gfc_expr *result;
6041 int kind;
6043 /* First check p. */
6044 if (p->expr_type != EXPR_CONSTANT)
6045 return NULL;
6047 /* p shall not be 0. */
6048 switch (p->ts.type)
6050 case BT_INTEGER:
6051 if (mpz_cmp_ui (p->value.integer, 0) == 0)
6053 gfc_error ("Argument %qs of MODULO at %L shall not be zero",
6054 "P", &p->where);
6055 return &gfc_bad_expr;
6057 break;
6058 case BT_REAL:
6059 if (mpfr_cmp_ui (p->value.real, 0) == 0)
6061 gfc_error ("Argument %qs of MODULO at %L shall not be zero",
6062 "P", &p->where);
6063 return &gfc_bad_expr;
6065 break;
6066 default:
6067 gfc_internal_error ("gfc_simplify_modulo(): Bad arguments");
6070 if (a->expr_type != EXPR_CONSTANT)
6071 return NULL;
6073 kind = a->ts.kind > p->ts.kind ? a->ts.kind : p->ts.kind;
6074 result = gfc_get_constant_expr (a->ts.type, kind, &a->where);
6076 if (a->ts.type == BT_INTEGER)
6077 mpz_fdiv_r (result->value.integer, a->value.integer, p->value.integer);
6078 else
6080 gfc_set_model_kind (kind);
6081 mpfr_fmod (result->value.real, a->value.real, p->value.real,
6082 GFC_RND_MODE);
6083 if (mpfr_cmp_ui (result->value.real, 0) != 0)
6085 if (mpfr_signbit (a->value.real) != mpfr_signbit (p->value.real))
6086 mpfr_add (result->value.real, result->value.real, p->value.real,
6087 GFC_RND_MODE);
6089 else
6090 mpfr_copysign (result->value.real, result->value.real,
6091 p->value.real, GFC_RND_MODE);
6094 return range_check (result, "MODULO");
6098 gfc_expr *
6099 gfc_simplify_nearest (gfc_expr *x, gfc_expr *s)
6101 gfc_expr *result;
6102 mpfr_exp_t emin, emax;
6103 int kind;
6105 if (x->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
6106 return NULL;
6108 result = gfc_copy_expr (x);
6110 /* Save current values of emin and emax. */
6111 emin = mpfr_get_emin ();
6112 emax = mpfr_get_emax ();
6114 /* Set emin and emax for the current model number. */
6115 kind = gfc_validate_kind (BT_REAL, x->ts.kind, 0);
6116 mpfr_set_emin ((mpfr_exp_t) gfc_real_kinds[kind].min_exponent -
6117 mpfr_get_prec(result->value.real) + 1);
6118 mpfr_set_emax ((mpfr_exp_t) gfc_real_kinds[kind].max_exponent - 1);
6119 mpfr_check_range (result->value.real, 0, MPFR_RNDU);
6121 if (mpfr_sgn (s->value.real) > 0)
6123 mpfr_nextabove (result->value.real);
6124 mpfr_subnormalize (result->value.real, 0, MPFR_RNDU);
6126 else
6128 mpfr_nextbelow (result->value.real);
6129 mpfr_subnormalize (result->value.real, 0, MPFR_RNDD);
6132 mpfr_set_emin (emin);
6133 mpfr_set_emax (emax);
6135 /* Only NaN can occur. Do not use range check as it gives an
6136 error for denormal numbers. */
6137 if (mpfr_nan_p (result->value.real) && flag_range_check)
6139 gfc_error ("Result of NEAREST is NaN at %L", &result->where);
6140 gfc_free_expr (result);
6141 return &gfc_bad_expr;
6144 return result;
6148 static gfc_expr *
6149 simplify_nint (const char *name, gfc_expr *e, gfc_expr *k)
6151 gfc_expr *itrunc, *result;
6152 int kind;
6154 kind = get_kind (BT_INTEGER, k, name, gfc_default_integer_kind);
6155 if (kind == -1)
6156 return &gfc_bad_expr;
6158 if (e->expr_type != EXPR_CONSTANT)
6159 return NULL;
6161 itrunc = gfc_copy_expr (e);
6162 mpfr_round (itrunc->value.real, e->value.real);
6164 result = gfc_get_constant_expr (BT_INTEGER, kind, &e->where);
6165 gfc_mpfr_to_mpz (result->value.integer, itrunc->value.real, &e->where);
6167 gfc_free_expr (itrunc);
6169 return range_check (result, name);
6173 gfc_expr *
6174 gfc_simplify_new_line (gfc_expr *e)
6176 gfc_expr *result;
6178 result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, 1);
6179 result->value.character.string[0] = '\n';
6181 return result;
6185 gfc_expr *
6186 gfc_simplify_nint (gfc_expr *e, gfc_expr *k)
6188 return simplify_nint ("NINT", e, k);
6192 gfc_expr *
6193 gfc_simplify_idnint (gfc_expr *e)
6195 return simplify_nint ("IDNINT", e, NULL);
6198 static int norm2_scale;
6200 static gfc_expr *
6201 norm2_add_squared (gfc_expr *result, gfc_expr *e)
6203 mpfr_t tmp;
6205 gcc_assert (e->ts.type == BT_REAL && e->expr_type == EXPR_CONSTANT);
6206 gcc_assert (result->ts.type == BT_REAL
6207 && result->expr_type == EXPR_CONSTANT);
6209 gfc_set_model_kind (result->ts.kind);
6210 int index = gfc_validate_kind (BT_REAL, result->ts.kind, false);
6211 mpfr_exp_t exp;
6212 if (mpfr_regular_p (result->value.real))
6214 exp = mpfr_get_exp (result->value.real);
6215 /* If result is getting close to overflowing, scale down. */
6216 if (exp >= gfc_real_kinds[index].max_exponent - 4
6217 && norm2_scale <= gfc_real_kinds[index].max_exponent - 2)
6219 norm2_scale += 2;
6220 mpfr_div_ui (result->value.real, result->value.real, 16,
6221 GFC_RND_MODE);
6225 mpfr_init (tmp);
6226 if (mpfr_regular_p (e->value.real))
6228 exp = mpfr_get_exp (e->value.real);
6229 /* If e**2 would overflow or close to overflowing, scale down. */
6230 if (exp - norm2_scale >= gfc_real_kinds[index].max_exponent / 2 - 2)
6232 int new_scale = gfc_real_kinds[index].max_exponent / 2 + 4;
6233 mpfr_set_ui (tmp, 1, GFC_RND_MODE);
6234 mpfr_set_exp (tmp, new_scale - norm2_scale);
6235 mpfr_div (result->value.real, result->value.real, tmp, GFC_RND_MODE);
6236 mpfr_div (result->value.real, result->value.real, tmp, GFC_RND_MODE);
6237 norm2_scale = new_scale;
6240 if (norm2_scale)
6242 mpfr_set_ui (tmp, 1, GFC_RND_MODE);
6243 mpfr_set_exp (tmp, norm2_scale);
6244 mpfr_div (tmp, e->value.real, tmp, GFC_RND_MODE);
6246 else
6247 mpfr_set (tmp, e->value.real, GFC_RND_MODE);
6248 mpfr_pow_ui (tmp, tmp, 2, GFC_RND_MODE);
6249 mpfr_add (result->value.real, result->value.real, tmp,
6250 GFC_RND_MODE);
6251 mpfr_clear (tmp);
6253 return result;
6257 static gfc_expr *
6258 norm2_do_sqrt (gfc_expr *result, gfc_expr *e)
6260 gcc_assert (e->ts.type == BT_REAL && e->expr_type == EXPR_CONSTANT);
6261 gcc_assert (result->ts.type == BT_REAL
6262 && result->expr_type == EXPR_CONSTANT);
6264 if (result != e)
6265 mpfr_set (result->value.real, e->value.real, GFC_RND_MODE);
6266 mpfr_sqrt (result->value.real, result->value.real, GFC_RND_MODE);
6267 if (norm2_scale && mpfr_regular_p (result->value.real))
6269 mpfr_t tmp;
6270 mpfr_init (tmp);
6271 mpfr_set_ui (tmp, 1, GFC_RND_MODE);
6272 mpfr_set_exp (tmp, norm2_scale);
6273 mpfr_mul (result->value.real, result->value.real, tmp, GFC_RND_MODE);
6274 mpfr_clear (tmp);
6276 norm2_scale = 0;
6278 return result;
6282 gfc_expr *
6283 gfc_simplify_norm2 (gfc_expr *e, gfc_expr *dim)
6285 gfc_expr *result;
6286 bool size_zero;
6288 size_zero = gfc_is_size_zero_array (e);
6290 if (!(is_constant_array_expr (e) || size_zero)
6291 || (dim != NULL && !gfc_is_constant_expr (dim)))
6292 return NULL;
6294 result = transformational_result (e, dim, e->ts.type, e->ts.kind, &e->where);
6295 init_result_expr (result, 0, NULL);
6297 if (size_zero)
6298 return result;
6300 norm2_scale = 0;
6301 if (!dim || e->rank == 1)
6303 result = simplify_transformation_to_scalar (result, e, NULL,
6304 norm2_add_squared);
6305 mpfr_sqrt (result->value.real, result->value.real, GFC_RND_MODE);
6306 if (norm2_scale && mpfr_regular_p (result->value.real))
6308 mpfr_t tmp;
6309 mpfr_init (tmp);
6310 mpfr_set_ui (tmp, 1, GFC_RND_MODE);
6311 mpfr_set_exp (tmp, norm2_scale);
6312 mpfr_mul (result->value.real, result->value.real, tmp, GFC_RND_MODE);
6313 mpfr_clear (tmp);
6315 norm2_scale = 0;
6317 else
6318 result = simplify_transformation_to_array (result, e, dim, NULL,
6319 norm2_add_squared,
6320 norm2_do_sqrt);
6322 return result;
6326 gfc_expr *
6327 gfc_simplify_not (gfc_expr *e)
6329 gfc_expr *result;
6331 if (e->expr_type != EXPR_CONSTANT)
6332 return NULL;
6334 result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
6335 mpz_com (result->value.integer, e->value.integer);
6337 return range_check (result, "NOT");
6341 gfc_expr *
6342 gfc_simplify_null (gfc_expr *mold)
6344 gfc_expr *result;
6346 if (mold)
6348 result = gfc_copy_expr (mold);
6349 result->expr_type = EXPR_NULL;
6351 else
6352 result = gfc_get_null_expr (NULL);
6354 return result;
6358 gfc_expr *
6359 gfc_simplify_num_images (gfc_expr *distance ATTRIBUTE_UNUSED, gfc_expr *failed)
6361 gfc_expr *result;
6363 if (flag_coarray == GFC_FCOARRAY_NONE)
6365 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
6366 return &gfc_bad_expr;
6369 if (flag_coarray != GFC_FCOARRAY_SINGLE)
6370 return NULL;
6372 if (failed && failed->expr_type != EXPR_CONSTANT)
6373 return NULL;
6375 /* FIXME: gfc_current_locus is wrong. */
6376 result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
6377 &gfc_current_locus);
6379 if (failed && failed->value.logical != 0)
6380 mpz_set_si (result->value.integer, 0);
6381 else
6382 mpz_set_si (result->value.integer, 1);
6384 return result;
6388 gfc_expr *
6389 gfc_simplify_or (gfc_expr *x, gfc_expr *y)
6391 gfc_expr *result;
6392 int kind;
6394 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
6395 return NULL;
6397 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
6399 switch (x->ts.type)
6401 case BT_INTEGER:
6402 result = gfc_get_constant_expr (BT_INTEGER, kind, &x->where);
6403 mpz_ior (result->value.integer, x->value.integer, y->value.integer);
6404 return range_check (result, "OR");
6406 case BT_LOGICAL:
6407 return gfc_get_logical_expr (kind, &x->where,
6408 x->value.logical || y->value.logical);
6409 default:
6410 gcc_unreachable();
6415 gfc_expr *
6416 gfc_simplify_pack (gfc_expr *array, gfc_expr *mask, gfc_expr *vector)
6418 gfc_expr *result;
6419 gfc_constructor *array_ctor, *mask_ctor, *vector_ctor;
6421 if (!is_constant_array_expr (array)
6422 || !is_constant_array_expr (vector)
6423 || (!gfc_is_constant_expr (mask)
6424 && !is_constant_array_expr (mask)))
6425 return NULL;
6427 result = gfc_get_array_expr (array->ts.type, array->ts.kind, &array->where);
6428 if (array->ts.type == BT_DERIVED)
6429 result->ts.u.derived = array->ts.u.derived;
6431 array_ctor = gfc_constructor_first (array->value.constructor);
6432 vector_ctor = vector
6433 ? gfc_constructor_first (vector->value.constructor)
6434 : NULL;
6436 if (mask->expr_type == EXPR_CONSTANT
6437 && mask->value.logical)
6439 /* Copy all elements of ARRAY to RESULT. */
6440 while (array_ctor)
6442 gfc_constructor_append_expr (&result->value.constructor,
6443 gfc_copy_expr (array_ctor->expr),
6444 NULL);
6446 array_ctor = gfc_constructor_next (array_ctor);
6447 vector_ctor = gfc_constructor_next (vector_ctor);
6450 else if (mask->expr_type == EXPR_ARRAY)
6452 /* Copy only those elements of ARRAY to RESULT whose
6453 MASK equals .TRUE.. */
6454 mask_ctor = gfc_constructor_first (mask->value.constructor);
6455 while (mask_ctor)
6457 if (mask_ctor->expr->value.logical)
6459 gfc_constructor_append_expr (&result->value.constructor,
6460 gfc_copy_expr (array_ctor->expr),
6461 NULL);
6462 vector_ctor = gfc_constructor_next (vector_ctor);
6465 array_ctor = gfc_constructor_next (array_ctor);
6466 mask_ctor = gfc_constructor_next (mask_ctor);
6470 /* Append any left-over elements from VECTOR to RESULT. */
6471 while (vector_ctor)
6473 gfc_constructor_append_expr (&result->value.constructor,
6474 gfc_copy_expr (vector_ctor->expr),
6475 NULL);
6476 vector_ctor = gfc_constructor_next (vector_ctor);
6479 result->shape = gfc_get_shape (1);
6480 gfc_array_size (result, &result->shape[0]);
6482 if (array->ts.type == BT_CHARACTER)
6483 result->ts.u.cl = array->ts.u.cl;
6485 return result;
6489 static gfc_expr *
6490 do_xor (gfc_expr *result, gfc_expr *e)
6492 gcc_assert (e->ts.type == BT_LOGICAL && e->expr_type == EXPR_CONSTANT);
6493 gcc_assert (result->ts.type == BT_LOGICAL
6494 && result->expr_type == EXPR_CONSTANT);
6496 result->value.logical = result->value.logical != e->value.logical;
6497 return result;
6501 gfc_expr *
6502 gfc_simplify_is_contiguous (gfc_expr *array)
6504 if (gfc_is_simply_contiguous (array, false, true))
6505 return gfc_get_logical_expr (gfc_default_logical_kind, &array->where, 1);
6507 if (gfc_is_not_contiguous (array))
6508 return gfc_get_logical_expr (gfc_default_logical_kind, &array->where, 0);
6510 return NULL;
6514 gfc_expr *
6515 gfc_simplify_parity (gfc_expr *e, gfc_expr *dim)
6517 return simplify_transformation (e, dim, NULL, 0, do_xor);
6521 gfc_expr *
6522 gfc_simplify_popcnt (gfc_expr *e)
6524 int res, k;
6525 mpz_t x;
6527 if (e->expr_type != EXPR_CONSTANT)
6528 return NULL;
6530 k = gfc_validate_kind (e->ts.type, e->ts.kind, false);
6532 /* Convert argument to unsigned, then count the '1' bits. */
6533 mpz_init_set (x, e->value.integer);
6534 convert_mpz_to_unsigned (x, gfc_integer_kinds[k].bit_size);
6535 res = mpz_popcount (x);
6536 mpz_clear (x);
6538 return gfc_get_int_expr (gfc_default_integer_kind, &e->where, res);
6542 gfc_expr *
6543 gfc_simplify_poppar (gfc_expr *e)
6545 gfc_expr *popcnt;
6546 int i;
6548 if (e->expr_type != EXPR_CONSTANT)
6549 return NULL;
6551 popcnt = gfc_simplify_popcnt (e);
6552 gcc_assert (popcnt);
6554 bool fail = gfc_extract_int (popcnt, &i);
6555 gcc_assert (!fail);
6557 return gfc_get_int_expr (gfc_default_integer_kind, &e->where, i % 2);
6561 gfc_expr *
6562 gfc_simplify_precision (gfc_expr *e)
6564 int i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
6565 return gfc_get_int_expr (gfc_default_integer_kind, &e->where,
6566 gfc_real_kinds[i].precision);
6570 gfc_expr *
6571 gfc_simplify_product (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
6573 return simplify_transformation (array, dim, mask, 1, gfc_multiply);
6577 gfc_expr *
6578 gfc_simplify_radix (gfc_expr *e)
6580 int i;
6581 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
6583 switch (e->ts.type)
6585 case BT_INTEGER:
6586 i = gfc_integer_kinds[i].radix;
6587 break;
6589 case BT_REAL:
6590 i = gfc_real_kinds[i].radix;
6591 break;
6593 default:
6594 gcc_unreachable ();
6597 return gfc_get_int_expr (gfc_default_integer_kind, &e->where, i);
6601 gfc_expr *
6602 gfc_simplify_range (gfc_expr *e)
6604 int i;
6605 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
6607 switch (e->ts.type)
6609 case BT_INTEGER:
6610 i = gfc_integer_kinds[i].range;
6611 break;
6613 case BT_REAL:
6614 case BT_COMPLEX:
6615 i = gfc_real_kinds[i].range;
6616 break;
6618 default:
6619 gcc_unreachable ();
6622 return gfc_get_int_expr (gfc_default_integer_kind, &e->where, i);
6626 gfc_expr *
6627 gfc_simplify_rank (gfc_expr *e)
6629 /* Assumed rank. */
6630 if (e->rank == -1)
6631 return NULL;
6633 return gfc_get_int_expr (gfc_default_integer_kind, &e->where, e->rank);
6637 gfc_expr *
6638 gfc_simplify_real (gfc_expr *e, gfc_expr *k)
6640 gfc_expr *result = NULL;
6641 int kind, tmp1, tmp2;
6643 /* Convert BOZ to real, and return without range checking. */
6644 if (e->ts.type == BT_BOZ)
6646 /* Determine kind for conversion of the BOZ. */
6647 if (k)
6648 gfc_extract_int (k, &kind);
6649 else
6650 kind = gfc_default_real_kind;
6652 if (!gfc_boz2real (e, kind))
6653 return NULL;
6654 result = gfc_copy_expr (e);
6655 return result;
6658 if (e->ts.type == BT_COMPLEX)
6659 kind = get_kind (BT_REAL, k, "REAL", e->ts.kind);
6660 else
6661 kind = get_kind (BT_REAL, k, "REAL", gfc_default_real_kind);
6663 if (kind == -1)
6664 return &gfc_bad_expr;
6666 if (e->expr_type != EXPR_CONSTANT)
6667 return NULL;
6669 /* For explicit conversion, turn off -Wconversion and -Wconversion-extra
6670 warnings. */
6671 tmp1 = warn_conversion;
6672 tmp2 = warn_conversion_extra;
6673 warn_conversion = warn_conversion_extra = 0;
6675 result = gfc_convert_constant (e, BT_REAL, kind);
6677 warn_conversion = tmp1;
6678 warn_conversion_extra = tmp2;
6680 if (result == &gfc_bad_expr)
6681 return &gfc_bad_expr;
6683 return range_check (result, "REAL");
6687 gfc_expr *
6688 gfc_simplify_realpart (gfc_expr *e)
6690 gfc_expr *result;
6692 if (e->expr_type != EXPR_CONSTANT)
6693 return NULL;
6695 result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
6696 mpc_real (result->value.real, e->value.complex, GFC_RND_MODE);
6698 return range_check (result, "REALPART");
6701 gfc_expr *
6702 gfc_simplify_repeat (gfc_expr *e, gfc_expr *n)
6704 gfc_expr *result;
6705 gfc_charlen_t len;
6706 mpz_t ncopies;
6707 bool have_length = false;
6709 /* If NCOPIES isn't a constant, there's nothing we can do. */
6710 if (n->expr_type != EXPR_CONSTANT)
6711 return NULL;
6713 /* If NCOPIES is negative, it's an error. */
6714 if (mpz_sgn (n->value.integer) < 0)
6716 gfc_error ("Argument NCOPIES of REPEAT intrinsic is negative at %L",
6717 &n->where);
6718 return &gfc_bad_expr;
6721 /* If we don't know the character length, we can do no more. */
6722 if (e->ts.u.cl && e->ts.u.cl->length
6723 && e->ts.u.cl->length->expr_type == EXPR_CONSTANT)
6725 len = gfc_mpz_get_hwi (e->ts.u.cl->length->value.integer);
6726 have_length = true;
6728 else if (e->expr_type == EXPR_CONSTANT
6729 && (e->ts.u.cl == NULL || e->ts.u.cl->length == NULL))
6731 len = e->value.character.length;
6733 else
6734 return NULL;
6736 /* If the source length is 0, any value of NCOPIES is valid
6737 and everything behaves as if NCOPIES == 0. */
6738 mpz_init (ncopies);
6739 if (len == 0)
6740 mpz_set_ui (ncopies, 0);
6741 else
6742 mpz_set (ncopies, n->value.integer);
6744 /* Check that NCOPIES isn't too large. */
6745 if (len)
6747 mpz_t max, mlen;
6748 int i;
6750 /* Compute the maximum value allowed for NCOPIES: huge(cl) / len. */
6751 mpz_init (max);
6752 i = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
6754 if (have_length)
6756 mpz_tdiv_q (max, gfc_integer_kinds[i].huge,
6757 e->ts.u.cl->length->value.integer);
6759 else
6761 mpz_init (mlen);
6762 gfc_mpz_set_hwi (mlen, len);
6763 mpz_tdiv_q (max, gfc_integer_kinds[i].huge, mlen);
6764 mpz_clear (mlen);
6767 /* The check itself. */
6768 if (mpz_cmp (ncopies, max) > 0)
6770 mpz_clear (max);
6771 mpz_clear (ncopies);
6772 gfc_error ("Argument NCOPIES of REPEAT intrinsic is too large at %L",
6773 &n->where);
6774 return &gfc_bad_expr;
6777 mpz_clear (max);
6779 mpz_clear (ncopies);
6781 /* For further simplification, we need the character string to be
6782 constant. */
6783 if (e->expr_type != EXPR_CONSTANT)
6784 return NULL;
6786 HOST_WIDE_INT ncop;
6787 if (len ||
6788 (e->ts.u.cl->length &&
6789 mpz_sgn (e->ts.u.cl->length->value.integer) != 0))
6791 bool fail = gfc_extract_hwi (n, &ncop);
6792 gcc_assert (!fail);
6794 else
6795 ncop = 0;
6797 if (ncop == 0)
6798 return gfc_get_character_expr (e->ts.kind, &e->where, NULL, 0);
6800 len = e->value.character.length;
6801 gfc_charlen_t nlen = ncop * len;
6803 /* Here's a semi-arbitrary limit. If the string is longer than 1 GB
6804 (2**28 elements * 4 bytes (wide chars) per element) defer to
6805 runtime instead of consuming (unbounded) memory and CPU at
6806 compile time. */
6807 if (nlen > 268435456)
6809 gfc_warning_now (0, "Evaluation of string longer than 2**28 at %L"
6810 " deferred to runtime, expect bugs", &e->where);
6811 return NULL;
6814 result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, nlen);
6815 for (size_t i = 0; i < (size_t) ncop; i++)
6816 for (size_t j = 0; j < (size_t) len; j++)
6817 result->value.character.string[j+i*len]= e->value.character.string[j];
6819 result->value.character.string[nlen] = '\0'; /* For debugger */
6820 return result;
6824 /* This one is a bear, but mainly has to do with shuffling elements. */
6826 gfc_expr *
6827 gfc_simplify_reshape (gfc_expr *source, gfc_expr *shape_exp,
6828 gfc_expr *pad, gfc_expr *order_exp)
6830 int order[GFC_MAX_DIMENSIONS], shape[GFC_MAX_DIMENSIONS];
6831 int i, rank, npad, x[GFC_MAX_DIMENSIONS];
6832 mpz_t index, size;
6833 unsigned long j;
6834 size_t nsource;
6835 gfc_expr *e, *result;
6836 bool zerosize = false;
6838 /* Check that argument expression types are OK. */
6839 if (!is_constant_array_expr (source)
6840 || !is_constant_array_expr (shape_exp)
6841 || !is_constant_array_expr (pad)
6842 || !is_constant_array_expr (order_exp))
6843 return NULL;
6845 if (source->shape == NULL)
6846 return NULL;
6848 /* Proceed with simplification, unpacking the array. */
6850 mpz_init (index);
6851 rank = 0;
6853 for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
6854 x[i] = 0;
6856 for (;;)
6858 e = gfc_constructor_lookup_expr (shape_exp->value.constructor, rank);
6859 if (e == NULL)
6860 break;
6862 gfc_extract_int (e, &shape[rank]);
6864 gcc_assert (rank >= 0 && rank < GFC_MAX_DIMENSIONS);
6865 if (shape[rank] < 0)
6867 gfc_error ("The SHAPE array for the RESHAPE intrinsic at %L has a "
6868 "negative value %d for dimension %d",
6869 &shape_exp->where, shape[rank], rank+1);
6870 return &gfc_bad_expr;
6873 rank++;
6876 gcc_assert (rank > 0);
6878 /* Now unpack the order array if present. */
6879 if (order_exp == NULL)
6881 for (i = 0; i < rank; i++)
6882 order[i] = i;
6884 else
6886 mpz_t size;
6887 int order_size, shape_size;
6889 if (order_exp->rank != shape_exp->rank)
6891 gfc_error ("Shapes of ORDER at %L and SHAPE at %L are different",
6892 &order_exp->where, &shape_exp->where);
6893 return &gfc_bad_expr;
6896 gfc_array_size (shape_exp, &size);
6897 shape_size = mpz_get_ui (size);
6898 mpz_clear (size);
6899 gfc_array_size (order_exp, &size);
6900 order_size = mpz_get_ui (size);
6901 mpz_clear (size);
6902 if (order_size != shape_size)
6904 gfc_error ("Sizes of ORDER at %L and SHAPE at %L are different",
6905 &order_exp->where, &shape_exp->where);
6906 return &gfc_bad_expr;
6909 for (i = 0; i < rank; i++)
6911 e = gfc_constructor_lookup_expr (order_exp->value.constructor, i);
6912 gcc_assert (e);
6914 gfc_extract_int (e, &order[i]);
6916 if (order[i] < 1 || order[i] > rank)
6918 gfc_error ("Element with a value of %d in ORDER at %L must be "
6919 "in the range [1, ..., %d] for the RESHAPE intrinsic "
6920 "near %L", order[i], &order_exp->where, rank,
6921 &shape_exp->where);
6922 return &gfc_bad_expr;
6925 order[i]--;
6926 if (x[order[i]] != 0)
6928 gfc_error ("ORDER at %L is not a permutation of the size of "
6929 "SHAPE at %L", &order_exp->where, &shape_exp->where);
6930 return &gfc_bad_expr;
6932 x[order[i]] = 1;
6936 /* Count the elements in the source and padding arrays. */
6938 npad = 0;
6939 if (pad != NULL)
6941 gfc_array_size (pad, &size);
6942 npad = mpz_get_ui (size);
6943 mpz_clear (size);
6946 gfc_array_size (source, &size);
6947 nsource = mpz_get_ui (size);
6948 mpz_clear (size);
6950 /* If it weren't for that pesky permutation we could just loop
6951 through the source and round out any shortage with pad elements.
6952 But no, someone just had to have the compiler do something the
6953 user should be doing. */
6955 for (i = 0; i < rank; i++)
6956 x[i] = 0;
6958 result = gfc_get_array_expr (source->ts.type, source->ts.kind,
6959 &source->where);
6960 if (source->ts.type == BT_DERIVED)
6961 result->ts.u.derived = source->ts.u.derived;
6962 if (source->ts.type == BT_CHARACTER && result->ts.u.cl == NULL)
6963 result->ts = source->ts;
6964 result->rank = rank;
6965 result->shape = gfc_get_shape (rank);
6966 for (i = 0; i < rank; i++)
6968 mpz_init_set_ui (result->shape[i], shape[i]);
6969 if (shape[i] == 0)
6970 zerosize = true;
6973 if (zerosize)
6974 goto sizezero;
6976 while (nsource > 0 || npad > 0)
6978 /* Figure out which element to extract. */
6979 mpz_set_ui (index, 0);
6981 for (i = rank - 1; i >= 0; i--)
6983 mpz_add_ui (index, index, x[order[i]]);
6984 if (i != 0)
6985 mpz_mul_ui (index, index, shape[order[i - 1]]);
6988 if (mpz_cmp_ui (index, INT_MAX) > 0)
6989 gfc_internal_error ("Reshaped array too large at %C");
6991 j = mpz_get_ui (index);
6993 if (j < nsource)
6994 e = gfc_constructor_lookup_expr (source->value.constructor, j);
6995 else
6997 if (npad <= 0)
6999 mpz_clear (index);
7000 return NULL;
7002 j = j - nsource;
7003 j = j % npad;
7004 e = gfc_constructor_lookup_expr (pad->value.constructor, j);
7006 gcc_assert (e);
7008 gfc_constructor_append_expr (&result->value.constructor,
7009 gfc_copy_expr (e), &e->where);
7011 /* Calculate the next element. */
7012 i = 0;
7014 inc:
7015 if (++x[i] < shape[i])
7016 continue;
7017 x[i++] = 0;
7018 if (i < rank)
7019 goto inc;
7021 break;
7024 sizezero:
7026 mpz_clear (index);
7028 return result;
7032 gfc_expr *
7033 gfc_simplify_rrspacing (gfc_expr *x)
7035 gfc_expr *result;
7036 int i;
7037 long int e, p;
7039 if (x->expr_type != EXPR_CONSTANT)
7040 return NULL;
7042 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
7044 result = gfc_get_constant_expr (BT_REAL, x->ts.kind, &x->where);
7046 /* RRSPACING(+/- 0.0) = 0.0 */
7047 if (mpfr_zero_p (x->value.real))
7049 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
7050 return result;
7053 /* RRSPACING(inf) = NaN */
7054 if (mpfr_inf_p (x->value.real))
7056 mpfr_set_nan (result->value.real);
7057 return result;
7060 /* RRSPACING(NaN) = same NaN */
7061 if (mpfr_nan_p (x->value.real))
7063 mpfr_set (result->value.real, x->value.real, GFC_RND_MODE);
7064 return result;
7067 /* | x * 2**(-e) | * 2**p. */
7068 mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE);
7069 e = - (long int) mpfr_get_exp (x->value.real);
7070 mpfr_mul_2si (result->value.real, result->value.real, e, GFC_RND_MODE);
7072 p = (long int) gfc_real_kinds[i].digits;
7073 mpfr_mul_2si (result->value.real, result->value.real, p, GFC_RND_MODE);
7075 return range_check (result, "RRSPACING");
7079 gfc_expr *
7080 gfc_simplify_scale (gfc_expr *x, gfc_expr *i)
7082 int k, neg_flag, power, exp_range;
7083 mpfr_t scale, radix;
7084 gfc_expr *result;
7086 if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
7087 return NULL;
7089 result = gfc_get_constant_expr (BT_REAL, x->ts.kind, &x->where);
7091 if (mpfr_zero_p (x->value.real))
7093 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
7094 return result;
7097 k = gfc_validate_kind (BT_REAL, x->ts.kind, false);
7099 exp_range = gfc_real_kinds[k].max_exponent - gfc_real_kinds[k].min_exponent;
7101 /* This check filters out values of i that would overflow an int. */
7102 if (mpz_cmp_si (i->value.integer, exp_range + 2) > 0
7103 || mpz_cmp_si (i->value.integer, -exp_range - 2) < 0)
7105 gfc_error ("Result of SCALE overflows its kind at %L", &result->where);
7106 gfc_free_expr (result);
7107 return &gfc_bad_expr;
7110 /* Compute scale = radix ** power. */
7111 power = mpz_get_si (i->value.integer);
7113 if (power >= 0)
7114 neg_flag = 0;
7115 else
7117 neg_flag = 1;
7118 power = -power;
7121 gfc_set_model_kind (x->ts.kind);
7122 mpfr_init (scale);
7123 mpfr_init (radix);
7124 mpfr_set_ui (radix, gfc_real_kinds[k].radix, GFC_RND_MODE);
7125 mpfr_pow_ui (scale, radix, power, GFC_RND_MODE);
7127 if (neg_flag)
7128 mpfr_div (result->value.real, x->value.real, scale, GFC_RND_MODE);
7129 else
7130 mpfr_mul (result->value.real, x->value.real, scale, GFC_RND_MODE);
7132 mpfr_clears (scale, radix, NULL);
7134 return range_check (result, "SCALE");
7138 /* Variants of strspn and strcspn that operate on wide characters. */
7140 static size_t
7141 wide_strspn (const gfc_char_t *s1, const gfc_char_t *s2)
7143 size_t i = 0;
7144 const gfc_char_t *c;
7146 while (s1[i])
7148 for (c = s2; *c; c++)
7150 if (s1[i] == *c)
7151 break;
7153 if (*c == '\0')
7154 break;
7155 i++;
7158 return i;
7161 static size_t
7162 wide_strcspn (const gfc_char_t *s1, const gfc_char_t *s2)
7164 size_t i = 0;
7165 const gfc_char_t *c;
7167 while (s1[i])
7169 for (c = s2; *c; c++)
7171 if (s1[i] == *c)
7172 break;
7174 if (*c)
7175 break;
7176 i++;
7179 return i;
7183 gfc_expr *
7184 gfc_simplify_scan (gfc_expr *e, gfc_expr *c, gfc_expr *b, gfc_expr *kind)
7186 gfc_expr *result;
7187 int back;
7188 size_t i;
7189 size_t indx, len, lenc;
7190 int k = get_kind (BT_INTEGER, kind, "SCAN", gfc_default_integer_kind);
7192 if (k == -1)
7193 return &gfc_bad_expr;
7195 if (e->expr_type != EXPR_CONSTANT || c->expr_type != EXPR_CONSTANT
7196 || ( b != NULL && b->expr_type != EXPR_CONSTANT))
7197 return NULL;
7199 if (b != NULL && b->value.logical != 0)
7200 back = 1;
7201 else
7202 back = 0;
7204 len = e->value.character.length;
7205 lenc = c->value.character.length;
7207 if (len == 0 || lenc == 0)
7209 indx = 0;
7211 else
7213 if (back == 0)
7215 indx = wide_strcspn (e->value.character.string,
7216 c->value.character.string) + 1;
7217 if (indx > len)
7218 indx = 0;
7220 else
7221 for (indx = len; indx > 0; indx--)
7223 for (i = 0; i < lenc; i++)
7225 if (c->value.character.string[i]
7226 == e->value.character.string[indx - 1])
7227 break;
7229 if (i < lenc)
7230 break;
7234 result = gfc_get_int_expr (k, &e->where, indx);
7235 return range_check (result, "SCAN");
7239 gfc_expr *
7240 gfc_simplify_selected_char_kind (gfc_expr *e)
7242 int kind;
7244 if (e->expr_type != EXPR_CONSTANT)
7245 return NULL;
7247 if (gfc_compare_with_Cstring (e, "ascii", false) == 0
7248 || gfc_compare_with_Cstring (e, "default", false) == 0)
7249 kind = 1;
7250 else if (gfc_compare_with_Cstring (e, "iso_10646", false) == 0)
7251 kind = 4;
7252 else
7253 kind = -1;
7255 return gfc_get_int_expr (gfc_default_integer_kind, &e->where, kind);
7259 gfc_expr *
7260 gfc_simplify_selected_int_kind (gfc_expr *e)
7262 int i, kind, range;
7264 if (e->expr_type != EXPR_CONSTANT || gfc_extract_int (e, &range))
7265 return NULL;
7267 kind = INT_MAX;
7269 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
7270 if (gfc_integer_kinds[i].range >= range
7271 && gfc_integer_kinds[i].kind < kind)
7272 kind = gfc_integer_kinds[i].kind;
7274 if (kind == INT_MAX)
7275 kind = -1;
7277 return gfc_get_int_expr (gfc_default_integer_kind, &e->where, kind);
7281 gfc_expr *
7282 gfc_simplify_selected_real_kind (gfc_expr *p, gfc_expr *q, gfc_expr *rdx)
7284 int range, precision, radix, i, kind, found_precision, found_range,
7285 found_radix;
7286 locus *loc = &gfc_current_locus;
7288 if (p == NULL)
7289 precision = 0;
7290 else
7292 if (p->expr_type != EXPR_CONSTANT
7293 || gfc_extract_int (p, &precision))
7294 return NULL;
7295 loc = &p->where;
7298 if (q == NULL)
7299 range = 0;
7300 else
7302 if (q->expr_type != EXPR_CONSTANT
7303 || gfc_extract_int (q, &range))
7304 return NULL;
7306 if (!loc)
7307 loc = &q->where;
7310 if (rdx == NULL)
7311 radix = 0;
7312 else
7314 if (rdx->expr_type != EXPR_CONSTANT
7315 || gfc_extract_int (rdx, &radix))
7316 return NULL;
7318 if (!loc)
7319 loc = &rdx->where;
7322 kind = INT_MAX;
7323 found_precision = 0;
7324 found_range = 0;
7325 found_radix = 0;
7327 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
7329 if (gfc_real_kinds[i].precision >= precision)
7330 found_precision = 1;
7332 if (gfc_real_kinds[i].range >= range)
7333 found_range = 1;
7335 if (radix == 0 || gfc_real_kinds[i].radix == radix)
7336 found_radix = 1;
7338 if (gfc_real_kinds[i].precision >= precision
7339 && gfc_real_kinds[i].range >= range
7340 && (radix == 0 || gfc_real_kinds[i].radix == radix)
7341 && gfc_real_kinds[i].kind < kind)
7342 kind = gfc_real_kinds[i].kind;
7345 if (kind == INT_MAX)
7347 if (found_radix && found_range && !found_precision)
7348 kind = -1;
7349 else if (found_radix && found_precision && !found_range)
7350 kind = -2;
7351 else if (found_radix && !found_precision && !found_range)
7352 kind = -3;
7353 else if (found_radix)
7354 kind = -4;
7355 else
7356 kind = -5;
7359 return gfc_get_int_expr (gfc_default_integer_kind, loc, kind);
7363 gfc_expr *
7364 gfc_simplify_set_exponent (gfc_expr *x, gfc_expr *i)
7366 gfc_expr *result;
7367 mpfr_t exp, absv, log2, pow2, frac;
7368 unsigned long exp2;
7370 if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
7371 return NULL;
7373 result = gfc_get_constant_expr (BT_REAL, x->ts.kind, &x->where);
7375 /* SET_EXPONENT (+/-0.0, I) = +/- 0.0
7376 SET_EXPONENT (NaN) = same NaN */
7377 if (mpfr_zero_p (x->value.real) || mpfr_nan_p (x->value.real))
7379 mpfr_set (result->value.real, x->value.real, GFC_RND_MODE);
7380 return result;
7383 /* SET_EXPONENT (inf) = NaN */
7384 if (mpfr_inf_p (x->value.real))
7386 mpfr_set_nan (result->value.real);
7387 return result;
7390 gfc_set_model_kind (x->ts.kind);
7391 mpfr_init (absv);
7392 mpfr_init (log2);
7393 mpfr_init (exp);
7394 mpfr_init (pow2);
7395 mpfr_init (frac);
7397 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
7398 mpfr_log2 (log2, absv, GFC_RND_MODE);
7400 mpfr_trunc (log2, log2);
7401 mpfr_add_ui (exp, log2, 1, GFC_RND_MODE);
7403 /* Old exponent value, and fraction. */
7404 mpfr_ui_pow (pow2, 2, exp, GFC_RND_MODE);
7406 mpfr_div (frac, absv, pow2, GFC_RND_MODE);
7408 /* New exponent. */
7409 exp2 = (unsigned long) mpz_get_d (i->value.integer);
7410 mpfr_mul_2exp (result->value.real, frac, exp2, GFC_RND_MODE);
7412 mpfr_clears (absv, log2, pow2, frac, NULL);
7414 return range_check (result, "SET_EXPONENT");
7418 gfc_expr *
7419 gfc_simplify_shape (gfc_expr *source, gfc_expr *kind)
7421 mpz_t shape[GFC_MAX_DIMENSIONS];
7422 gfc_expr *result, *e, *f;
7423 gfc_array_ref *ar;
7424 int n;
7425 bool t;
7426 int k = get_kind (BT_INTEGER, kind, "SHAPE", gfc_default_integer_kind);
7428 if (source->rank == -1)
7429 return NULL;
7431 result = gfc_get_array_expr (BT_INTEGER, k, &source->where);
7432 result->shape = gfc_get_shape (1);
7433 mpz_init (result->shape[0]);
7435 if (source->rank == 0)
7436 return result;
7438 if (source->expr_type == EXPR_VARIABLE)
7440 ar = gfc_find_array_ref (source);
7441 t = gfc_array_ref_shape (ar, shape);
7443 else if (source->shape)
7445 t = true;
7446 for (n = 0; n < source->rank; n++)
7448 mpz_init (shape[n]);
7449 mpz_set (shape[n], source->shape[n]);
7452 else
7453 t = false;
7455 for (n = 0; n < source->rank; n++)
7457 e = gfc_get_constant_expr (BT_INTEGER, k, &source->where);
7459 if (t)
7460 mpz_set (e->value.integer, shape[n]);
7461 else
7463 mpz_set_ui (e->value.integer, n + 1);
7465 f = simplify_size (source, e, k);
7466 gfc_free_expr (e);
7467 if (f == NULL)
7469 gfc_free_expr (result);
7470 return NULL;
7472 else
7473 e = f;
7476 if (e == &gfc_bad_expr || range_check (e, "SHAPE") == &gfc_bad_expr)
7478 gfc_free_expr (result);
7479 if (t)
7480 gfc_clear_shape (shape, source->rank);
7481 return &gfc_bad_expr;
7484 gfc_constructor_append_expr (&result->value.constructor, e, NULL);
7487 if (t)
7488 gfc_clear_shape (shape, source->rank);
7490 mpz_set_si (result->shape[0], source->rank);
7492 return result;
7496 static gfc_expr *
7497 simplify_size (gfc_expr *array, gfc_expr *dim, int k)
7499 mpz_t size;
7500 gfc_expr *return_value;
7501 int d;
7502 gfc_ref *ref;
7504 /* For unary operations, the size of the result is given by the size
7505 of the operand. For binary ones, it's the size of the first operand
7506 unless it is scalar, then it is the size of the second. */
7507 if (array->expr_type == EXPR_OP && !array->value.op.uop)
7509 gfc_expr* replacement;
7510 gfc_expr* simplified;
7512 switch (array->value.op.op)
7514 /* Unary operations. */
7515 case INTRINSIC_NOT:
7516 case INTRINSIC_UPLUS:
7517 case INTRINSIC_UMINUS:
7518 case INTRINSIC_PARENTHESES:
7519 replacement = array->value.op.op1;
7520 break;
7522 /* Binary operations. If any one of the operands is scalar, take
7523 the other one's size. If both of them are arrays, it does not
7524 matter -- try to find one with known shape, if possible. */
7525 default:
7526 if (array->value.op.op1->rank == 0)
7527 replacement = array->value.op.op2;
7528 else if (array->value.op.op2->rank == 0)
7529 replacement = array->value.op.op1;
7530 else
7532 simplified = simplify_size (array->value.op.op1, dim, k);
7533 if (simplified)
7534 return simplified;
7536 replacement = array->value.op.op2;
7538 break;
7541 /* Try to reduce it directly if possible. */
7542 simplified = simplify_size (replacement, dim, k);
7544 /* Otherwise, we build a new SIZE call. This is hopefully at least
7545 simpler than the original one. */
7546 if (!simplified)
7548 gfc_expr *kind = gfc_get_int_expr (gfc_default_integer_kind, NULL, k);
7549 simplified = gfc_build_intrinsic_call (gfc_current_ns,
7550 GFC_ISYM_SIZE, "size",
7551 array->where, 3,
7552 gfc_copy_expr (replacement),
7553 gfc_copy_expr (dim),
7554 kind);
7556 return simplified;
7559 for (ref = array->ref; ref; ref = ref->next)
7560 if (ref->type == REF_ARRAY && ref->u.ar.as)
7561 gfc_resolve_array_spec (ref->u.ar.as, 0);
7563 if (dim == NULL)
7565 if (!gfc_array_size (array, &size))
7566 return NULL;
7568 else
7570 if (dim->expr_type != EXPR_CONSTANT)
7571 return NULL;
7573 d = mpz_get_ui (dim->value.integer) - 1;
7574 if (!gfc_array_dimen_size (array, d, &size))
7575 return NULL;
7578 return_value = gfc_get_constant_expr (BT_INTEGER, k, &array->where);
7579 mpz_set (return_value->value.integer, size);
7580 mpz_clear (size);
7582 return return_value;
7586 gfc_expr *
7587 gfc_simplify_size (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
7589 gfc_expr *result;
7590 int k = get_kind (BT_INTEGER, kind, "SIZE", gfc_default_integer_kind);
7592 if (k == -1)
7593 return &gfc_bad_expr;
7595 result = simplify_size (array, dim, k);
7596 if (result == NULL || result == &gfc_bad_expr)
7597 return result;
7599 return range_check (result, "SIZE");
7603 /* SIZEOF and C_SIZEOF return the size in bytes of an array element
7604 multiplied by the array size. */
7606 gfc_expr *
7607 gfc_simplify_sizeof (gfc_expr *x)
7609 gfc_expr *result = NULL;
7610 mpz_t array_size;
7611 size_t res_size;
7613 if (x->ts.type == BT_CLASS || x->ts.deferred)
7614 return NULL;
7616 if (x->ts.type == BT_CHARACTER
7617 && (!x->ts.u.cl || !x->ts.u.cl->length
7618 || x->ts.u.cl->length->expr_type != EXPR_CONSTANT))
7619 return NULL;
7621 if (x->rank && x->expr_type != EXPR_ARRAY
7622 && !gfc_array_size (x, &array_size))
7623 return NULL;
7625 result = gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
7626 &x->where);
7627 gfc_target_expr_size (x, &res_size);
7628 mpz_set_si (result->value.integer, res_size);
7630 return result;
7634 /* STORAGE_SIZE returns the size in bits of a single array element. */
7636 gfc_expr *
7637 gfc_simplify_storage_size (gfc_expr *x,
7638 gfc_expr *kind)
7640 gfc_expr *result = NULL;
7641 int k;
7642 size_t siz;
7644 if (x->ts.type == BT_CLASS || x->ts.deferred)
7645 return NULL;
7647 if (x->ts.type == BT_CHARACTER && x->expr_type != EXPR_CONSTANT
7648 && (!x->ts.u.cl || !x->ts.u.cl->length
7649 || x->ts.u.cl->length->expr_type != EXPR_CONSTANT))
7650 return NULL;
7652 k = get_kind (BT_INTEGER, kind, "STORAGE_SIZE", gfc_default_integer_kind);
7653 if (k == -1)
7654 return &gfc_bad_expr;
7656 result = gfc_get_constant_expr (BT_INTEGER, k, &x->where);
7658 gfc_element_size (x, &siz);
7659 mpz_set_si (result->value.integer, siz);
7660 mpz_mul_ui (result->value.integer, result->value.integer, BITS_PER_UNIT);
7662 return range_check (result, "STORAGE_SIZE");
7666 gfc_expr *
7667 gfc_simplify_sign (gfc_expr *x, gfc_expr *y)
7669 gfc_expr *result;
7671 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
7672 return NULL;
7674 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
7676 switch (x->ts.type)
7678 case BT_INTEGER:
7679 mpz_abs (result->value.integer, x->value.integer);
7680 if (mpz_sgn (y->value.integer) < 0)
7681 mpz_neg (result->value.integer, result->value.integer);
7682 break;
7684 case BT_REAL:
7685 if (flag_sign_zero)
7686 mpfr_copysign (result->value.real, x->value.real, y->value.real,
7687 GFC_RND_MODE);
7688 else
7689 mpfr_setsign (result->value.real, x->value.real,
7690 mpfr_sgn (y->value.real) < 0 ? 1 : 0, GFC_RND_MODE);
7691 break;
7693 default:
7694 gfc_internal_error ("Bad type in gfc_simplify_sign");
7697 return result;
7701 gfc_expr *
7702 gfc_simplify_sin (gfc_expr *x)
7704 gfc_expr *result;
7706 if (x->expr_type != EXPR_CONSTANT)
7707 return NULL;
7709 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
7711 switch (x->ts.type)
7713 case BT_REAL:
7714 mpfr_sin (result->value.real, x->value.real, GFC_RND_MODE);
7715 break;
7717 case BT_COMPLEX:
7718 gfc_set_model (x->value.real);
7719 mpc_sin (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
7720 break;
7722 default:
7723 gfc_internal_error ("in gfc_simplify_sin(): Bad type");
7726 return range_check (result, "SIN");
7730 gfc_expr *
7731 gfc_simplify_sinh (gfc_expr *x)
7733 gfc_expr *result;
7735 if (x->expr_type != EXPR_CONSTANT)
7736 return NULL;
7738 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
7740 switch (x->ts.type)
7742 case BT_REAL:
7743 mpfr_sinh (result->value.real, x->value.real, GFC_RND_MODE);
7744 break;
7746 case BT_COMPLEX:
7747 mpc_sinh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
7748 break;
7750 default:
7751 gcc_unreachable ();
7754 return range_check (result, "SINH");
7758 /* The argument is always a double precision real that is converted to
7759 single precision. TODO: Rounding! */
7761 gfc_expr *
7762 gfc_simplify_sngl (gfc_expr *a)
7764 gfc_expr *result;
7765 int tmp1, tmp2;
7767 if (a->expr_type != EXPR_CONSTANT)
7768 return NULL;
7770 /* For explicit conversion, turn off -Wconversion and -Wconversion-extra
7771 warnings. */
7772 tmp1 = warn_conversion;
7773 tmp2 = warn_conversion_extra;
7774 warn_conversion = warn_conversion_extra = 0;
7776 result = gfc_real2real (a, gfc_default_real_kind);
7778 warn_conversion = tmp1;
7779 warn_conversion_extra = tmp2;
7781 return range_check (result, "SNGL");
7785 gfc_expr *
7786 gfc_simplify_spacing (gfc_expr *x)
7788 gfc_expr *result;
7789 int i;
7790 long int en, ep;
7792 if (x->expr_type != EXPR_CONSTANT)
7793 return NULL;
7795 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
7796 result = gfc_get_constant_expr (BT_REAL, x->ts.kind, &x->where);
7798 /* SPACING(+/- 0.0) = SPACING(TINY(0.0)) = TINY(0.0) */
7799 if (mpfr_zero_p (x->value.real))
7801 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
7802 return result;
7805 /* SPACING(inf) = NaN */
7806 if (mpfr_inf_p (x->value.real))
7808 mpfr_set_nan (result->value.real);
7809 return result;
7812 /* SPACING(NaN) = same NaN */
7813 if (mpfr_nan_p (x->value.real))
7815 mpfr_set (result->value.real, x->value.real, GFC_RND_MODE);
7816 return result;
7819 /* In the Fortran 95 standard, the result is b**(e - p) where b, e, and p
7820 are the radix, exponent of x, and precision. This excludes the
7821 possibility of subnormal numbers. Fortran 2003 states the result is
7822 b**max(e - p, emin - 1). */
7824 ep = (long int) mpfr_get_exp (x->value.real) - gfc_real_kinds[i].digits;
7825 en = (long int) gfc_real_kinds[i].min_exponent - 1;
7826 en = en > ep ? en : ep;
7828 mpfr_set_ui (result->value.real, 1, GFC_RND_MODE);
7829 mpfr_mul_2si (result->value.real, result->value.real, en, GFC_RND_MODE);
7831 return range_check (result, "SPACING");
7835 gfc_expr *
7836 gfc_simplify_spread (gfc_expr *source, gfc_expr *dim_expr, gfc_expr *ncopies_expr)
7838 gfc_expr *result = NULL;
7839 int nelem, i, j, dim, ncopies;
7840 mpz_t size;
7842 if ((!gfc_is_constant_expr (source)
7843 && !is_constant_array_expr (source))
7844 || !gfc_is_constant_expr (dim_expr)
7845 || !gfc_is_constant_expr (ncopies_expr))
7846 return NULL;
7848 gcc_assert (dim_expr->ts.type == BT_INTEGER);
7849 gfc_extract_int (dim_expr, &dim);
7850 dim -= 1; /* zero-base DIM */
7852 gcc_assert (ncopies_expr->ts.type == BT_INTEGER);
7853 gfc_extract_int (ncopies_expr, &ncopies);
7854 ncopies = MAX (ncopies, 0);
7856 /* Do not allow the array size to exceed the limit for an array
7857 constructor. */
7858 if (source->expr_type == EXPR_ARRAY)
7860 if (!gfc_array_size (source, &size))
7861 gfc_internal_error ("Failure getting length of a constant array.");
7863 else
7864 mpz_init_set_ui (size, 1);
7866 nelem = mpz_get_si (size) * ncopies;
7867 if (nelem > flag_max_array_constructor)
7869 if (gfc_init_expr_flag)
7871 gfc_error ("The number of elements (%d) in the array constructor "
7872 "at %L requires an increase of the allowed %d upper "
7873 "limit. See %<-fmax-array-constructor%> option.",
7874 nelem, &source->where, flag_max_array_constructor);
7875 return &gfc_bad_expr;
7877 else
7878 return NULL;
7881 if (source->expr_type == EXPR_CONSTANT
7882 || source->expr_type == EXPR_STRUCTURE)
7884 gcc_assert (dim == 0);
7886 result = gfc_get_array_expr (source->ts.type, source->ts.kind,
7887 &source->where);
7888 if (source->ts.type == BT_DERIVED)
7889 result->ts.u.derived = source->ts.u.derived;
7890 result->rank = 1;
7891 result->shape = gfc_get_shape (result->rank);
7892 mpz_init_set_si (result->shape[0], ncopies);
7894 for (i = 0; i < ncopies; ++i)
7895 gfc_constructor_append_expr (&result->value.constructor,
7896 gfc_copy_expr (source), NULL);
7898 else if (source->expr_type == EXPR_ARRAY)
7900 int offset, rstride[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS];
7901 gfc_constructor *source_ctor;
7903 gcc_assert (source->rank < GFC_MAX_DIMENSIONS);
7904 gcc_assert (dim >= 0 && dim <= source->rank);
7906 result = gfc_get_array_expr (source->ts.type, source->ts.kind,
7907 &source->where);
7908 if (source->ts.type == BT_DERIVED)
7909 result->ts.u.derived = source->ts.u.derived;
7910 result->rank = source->rank + 1;
7911 result->shape = gfc_get_shape (result->rank);
7913 for (i = 0, j = 0; i < result->rank; ++i)
7915 if (i != dim)
7916 mpz_init_set (result->shape[i], source->shape[j++]);
7917 else
7918 mpz_init_set_si (result->shape[i], ncopies);
7920 extent[i] = mpz_get_si (result->shape[i]);
7921 rstride[i] = (i == 0) ? 1 : rstride[i-1] * extent[i-1];
7924 offset = 0;
7925 for (source_ctor = gfc_constructor_first (source->value.constructor);
7926 source_ctor; source_ctor = gfc_constructor_next (source_ctor))
7928 for (i = 0; i < ncopies; ++i)
7929 gfc_constructor_insert_expr (&result->value.constructor,
7930 gfc_copy_expr (source_ctor->expr),
7931 NULL, offset + i * rstride[dim]);
7933 offset += (dim == 0 ? ncopies : 1);
7936 else
7938 gfc_error ("Simplification of SPREAD at %C not yet implemented");
7939 return &gfc_bad_expr;
7942 if (source->ts.type == BT_CHARACTER)
7943 result->ts.u.cl = source->ts.u.cl;
7945 return result;
7949 gfc_expr *
7950 gfc_simplify_sqrt (gfc_expr *e)
7952 gfc_expr *result = NULL;
7954 if (e->expr_type != EXPR_CONSTANT)
7955 return NULL;
7957 switch (e->ts.type)
7959 case BT_REAL:
7960 if (mpfr_cmp_si (e->value.real, 0) < 0)
7962 gfc_error ("Argument of SQRT at %L has a negative value",
7963 &e->where);
7964 return &gfc_bad_expr;
7966 result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
7967 mpfr_sqrt (result->value.real, e->value.real, GFC_RND_MODE);
7968 break;
7970 case BT_COMPLEX:
7971 gfc_set_model (e->value.real);
7973 result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
7974 mpc_sqrt (result->value.complex, e->value.complex, GFC_MPC_RND_MODE);
7975 break;
7977 default:
7978 gfc_internal_error ("invalid argument of SQRT at %L", &e->where);
7981 return range_check (result, "SQRT");
7985 gfc_expr *
7986 gfc_simplify_sum (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
7988 return simplify_transformation (array, dim, mask, 0, gfc_add);
7992 /* Simplify COTAN(X) where X has the unit of radian. */
7994 gfc_expr *
7995 gfc_simplify_cotan (gfc_expr *x)
7997 gfc_expr *result;
7998 mpc_t swp, *val;
8000 if (x->expr_type != EXPR_CONSTANT)
8001 return NULL;
8003 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
8005 switch (x->ts.type)
8007 case BT_REAL:
8008 mpfr_cot (result->value.real, x->value.real, GFC_RND_MODE);
8009 break;
8011 case BT_COMPLEX:
8012 /* There is no builtin mpc_cot, so compute cot = cos / sin. */
8013 val = &result->value.complex;
8014 mpc_init2 (swp, mpfr_get_default_prec ());
8015 mpc_sin_cos (*val, swp, x->value.complex, GFC_MPC_RND_MODE,
8016 GFC_MPC_RND_MODE);
8017 mpc_div (*val, swp, *val, GFC_MPC_RND_MODE);
8018 mpc_clear (swp);
8019 break;
8021 default:
8022 gcc_unreachable ();
8025 return range_check (result, "COTAN");
8029 gfc_expr *
8030 gfc_simplify_tan (gfc_expr *x)
8032 gfc_expr *result;
8034 if (x->expr_type != EXPR_CONSTANT)
8035 return NULL;
8037 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
8039 switch (x->ts.type)
8041 case BT_REAL:
8042 mpfr_tan (result->value.real, x->value.real, GFC_RND_MODE);
8043 break;
8045 case BT_COMPLEX:
8046 mpc_tan (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
8047 break;
8049 default:
8050 gcc_unreachable ();
8053 return range_check (result, "TAN");
8057 gfc_expr *
8058 gfc_simplify_tanh (gfc_expr *x)
8060 gfc_expr *result;
8062 if (x->expr_type != EXPR_CONSTANT)
8063 return NULL;
8065 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
8067 switch (x->ts.type)
8069 case BT_REAL:
8070 mpfr_tanh (result->value.real, x->value.real, GFC_RND_MODE);
8071 break;
8073 case BT_COMPLEX:
8074 mpc_tanh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
8075 break;
8077 default:
8078 gcc_unreachable ();
8081 return range_check (result, "TANH");
8085 gfc_expr *
8086 gfc_simplify_tiny (gfc_expr *e)
8088 gfc_expr *result;
8089 int i;
8091 i = gfc_validate_kind (BT_REAL, e->ts.kind, false);
8093 result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
8094 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
8096 return result;
8100 gfc_expr *
8101 gfc_simplify_trailz (gfc_expr *e)
8103 unsigned long tz, bs;
8104 int i;
8106 if (e->expr_type != EXPR_CONSTANT)
8107 return NULL;
8109 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
8110 bs = gfc_integer_kinds[i].bit_size;
8111 tz = mpz_scan1 (e->value.integer, 0);
8113 return gfc_get_int_expr (gfc_default_integer_kind,
8114 &e->where, MIN (tz, bs));
8118 gfc_expr *
8119 gfc_simplify_transfer (gfc_expr *source, gfc_expr *mold, gfc_expr *size)
8121 gfc_expr *result;
8122 gfc_expr *mold_element;
8123 size_t source_size;
8124 size_t result_size;
8125 size_t buffer_size;
8126 mpz_t tmp;
8127 unsigned char *buffer;
8128 size_t result_length;
8130 if (!gfc_is_constant_expr (source) || !gfc_is_constant_expr (size))
8131 return NULL;
8133 if (!gfc_resolve_expr (mold))
8134 return NULL;
8135 if (gfc_init_expr_flag && !gfc_is_constant_expr (mold))
8136 return NULL;
8138 if (!gfc_calculate_transfer_sizes (source, mold, size, &source_size,
8139 &result_size, &result_length))
8140 return NULL;
8142 /* Calculate the size of the source. */
8143 if (source->expr_type == EXPR_ARRAY && !gfc_array_size (source, &tmp))
8144 gfc_internal_error ("Failure getting length of a constant array.");
8146 /* Create an empty new expression with the appropriate characteristics. */
8147 result = gfc_get_constant_expr (mold->ts.type, mold->ts.kind,
8148 &source->where);
8149 result->ts = mold->ts;
8151 mold_element = (mold->expr_type == EXPR_ARRAY && mold->value.constructor)
8152 ? gfc_constructor_first (mold->value.constructor)->expr
8153 : mold;
8155 /* Set result character length, if needed. Note that this needs to be
8156 set even for array expressions, in order to pass this information into
8157 gfc_target_interpret_expr. */
8158 if (result->ts.type == BT_CHARACTER && gfc_is_constant_expr (mold_element))
8159 result->value.character.length = mold_element->value.character.length;
8161 /* Set the number of elements in the result, and determine its size. */
8163 if (mold->expr_type == EXPR_ARRAY || mold->rank || size)
8165 result->expr_type = EXPR_ARRAY;
8166 result->rank = 1;
8167 result->shape = gfc_get_shape (1);
8168 mpz_init_set_ui (result->shape[0], result_length);
8170 else
8171 result->rank = 0;
8173 /* Allocate the buffer to store the binary version of the source. */
8174 buffer_size = MAX (source_size, result_size);
8175 buffer = (unsigned char*)alloca (buffer_size);
8176 memset (buffer, 0, buffer_size);
8178 /* Now write source to the buffer. */
8179 gfc_target_encode_expr (source, buffer, buffer_size);
8181 /* And read the buffer back into the new expression. */
8182 gfc_target_interpret_expr (buffer, buffer_size, result, false);
8184 return result;
8188 gfc_expr *
8189 gfc_simplify_transpose (gfc_expr *matrix)
8191 int row, matrix_rows, col, matrix_cols;
8192 gfc_expr *result;
8194 if (!is_constant_array_expr (matrix))
8195 return NULL;
8197 gcc_assert (matrix->rank == 2);
8199 if (matrix->shape == NULL)
8200 return NULL;
8202 result = gfc_get_array_expr (matrix->ts.type, matrix->ts.kind,
8203 &matrix->where);
8204 result->rank = 2;
8205 result->shape = gfc_get_shape (result->rank);
8206 mpz_init_set (result->shape[0], matrix->shape[1]);
8207 mpz_init_set (result->shape[1], matrix->shape[0]);
8209 if (matrix->ts.type == BT_CHARACTER)
8210 result->ts.u.cl = matrix->ts.u.cl;
8211 else if (matrix->ts.type == BT_DERIVED)
8212 result->ts.u.derived = matrix->ts.u.derived;
8214 matrix_rows = mpz_get_si (matrix->shape[0]);
8215 matrix_cols = mpz_get_si (matrix->shape[1]);
8216 for (row = 0; row < matrix_rows; ++row)
8217 for (col = 0; col < matrix_cols; ++col)
8219 gfc_expr *e = gfc_constructor_lookup_expr (matrix->value.constructor,
8220 col * matrix_rows + row);
8221 gfc_constructor_insert_expr (&result->value.constructor,
8222 gfc_copy_expr (e), &matrix->where,
8223 row * matrix_cols + col);
8226 return result;
8230 gfc_expr *
8231 gfc_simplify_trim (gfc_expr *e)
8233 gfc_expr *result;
8234 int count, i, len, lentrim;
8236 if (e->expr_type != EXPR_CONSTANT)
8237 return NULL;
8239 len = e->value.character.length;
8240 for (count = 0, i = 1; i <= len; ++i)
8242 if (e->value.character.string[len - i] == ' ')
8243 count++;
8244 else
8245 break;
8248 lentrim = len - count;
8250 result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, lentrim);
8251 for (i = 0; i < lentrim; i++)
8252 result->value.character.string[i] = e->value.character.string[i];
8254 return result;
8258 gfc_expr *
8259 gfc_simplify_image_index (gfc_expr *coarray, gfc_expr *sub)
8261 gfc_expr *result;
8262 gfc_ref *ref;
8263 gfc_array_spec *as;
8264 gfc_constructor *sub_cons;
8265 bool first_image;
8266 int d;
8268 if (!is_constant_array_expr (sub))
8269 return NULL;
8271 /* Follow any component references. */
8272 as = coarray->symtree->n.sym->as;
8273 for (ref = coarray->ref; ref; ref = ref->next)
8274 if (ref->type == REF_COMPONENT)
8275 as = ref->u.ar.as;
8277 if (as->type == AS_DEFERRED)
8278 return NULL;
8280 /* "valid sequence of cosubscripts" are required; thus, return 0 unless
8281 the cosubscript addresses the first image. */
8283 sub_cons = gfc_constructor_first (sub->value.constructor);
8284 first_image = true;
8286 for (d = 1; d <= as->corank; d++)
8288 gfc_expr *ca_bound;
8289 int cmp;
8291 gcc_assert (sub_cons != NULL);
8293 ca_bound = simplify_bound_dim (coarray, NULL, d + as->rank, 0, as,
8294 NULL, true);
8295 if (ca_bound == NULL)
8296 return NULL;
8298 if (ca_bound == &gfc_bad_expr)
8299 return ca_bound;
8301 cmp = mpz_cmp (ca_bound->value.integer, sub_cons->expr->value.integer);
8303 if (cmp == 0)
8305 gfc_free_expr (ca_bound);
8306 sub_cons = gfc_constructor_next (sub_cons);
8307 continue;
8310 first_image = false;
8312 if (cmp > 0)
8314 gfc_error ("Out of bounds in IMAGE_INDEX at %L for dimension %d, "
8315 "SUB has %ld and COARRAY lower bound is %ld)",
8316 &coarray->where, d,
8317 mpz_get_si (sub_cons->expr->value.integer),
8318 mpz_get_si (ca_bound->value.integer));
8319 gfc_free_expr (ca_bound);
8320 return &gfc_bad_expr;
8323 gfc_free_expr (ca_bound);
8325 /* Check whether upperbound is valid for the multi-images case. */
8326 if (d < as->corank)
8328 ca_bound = simplify_bound_dim (coarray, NULL, d + as->rank, 1, as,
8329 NULL, true);
8330 if (ca_bound == &gfc_bad_expr)
8331 return ca_bound;
8333 if (ca_bound && ca_bound->expr_type == EXPR_CONSTANT
8334 && mpz_cmp (ca_bound->value.integer,
8335 sub_cons->expr->value.integer) < 0)
8337 gfc_error ("Out of bounds in IMAGE_INDEX at %L for dimension %d, "
8338 "SUB has %ld and COARRAY upper bound is %ld)",
8339 &coarray->where, d,
8340 mpz_get_si (sub_cons->expr->value.integer),
8341 mpz_get_si (ca_bound->value.integer));
8342 gfc_free_expr (ca_bound);
8343 return &gfc_bad_expr;
8346 if (ca_bound)
8347 gfc_free_expr (ca_bound);
8350 sub_cons = gfc_constructor_next (sub_cons);
8353 gcc_assert (sub_cons == NULL);
8355 if (flag_coarray != GFC_FCOARRAY_SINGLE && !first_image)
8356 return NULL;
8358 result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
8359 &gfc_current_locus);
8360 if (first_image)
8361 mpz_set_si (result->value.integer, 1);
8362 else
8363 mpz_set_si (result->value.integer, 0);
8365 return result;
8368 gfc_expr *
8369 gfc_simplify_image_status (gfc_expr *image, gfc_expr *team ATTRIBUTE_UNUSED)
8371 if (flag_coarray == GFC_FCOARRAY_NONE)
8373 gfc_current_locus = *gfc_current_intrinsic_where;
8374 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
8375 return &gfc_bad_expr;
8378 /* Simplification is possible for fcoarray = single only. For all other modes
8379 the result depends on runtime conditions. */
8380 if (flag_coarray != GFC_FCOARRAY_SINGLE)
8381 return NULL;
8383 if (gfc_is_constant_expr (image))
8385 gfc_expr *result;
8386 result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
8387 &image->where);
8388 if (mpz_get_si (image->value.integer) == 1)
8389 mpz_set_si (result->value.integer, 0);
8390 else
8391 mpz_set_si (result->value.integer, GFC_STAT_STOPPED_IMAGE);
8392 return result;
8394 else
8395 return NULL;
8399 gfc_expr *
8400 gfc_simplify_this_image (gfc_expr *coarray, gfc_expr *dim,
8401 gfc_expr *distance ATTRIBUTE_UNUSED)
8403 if (flag_coarray != GFC_FCOARRAY_SINGLE)
8404 return NULL;
8406 /* If no coarray argument has been passed or when the first argument
8407 is actually a distance argment. */
8408 if (coarray == NULL || !gfc_is_coarray (coarray))
8410 gfc_expr *result;
8411 /* FIXME: gfc_current_locus is wrong. */
8412 result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
8413 &gfc_current_locus);
8414 mpz_set_si (result->value.integer, 1);
8415 return result;
8418 /* For -fcoarray=single, this_image(A) is the same as lcobound(A). */
8419 return simplify_cobound (coarray, dim, NULL, 0);
8423 gfc_expr *
8424 gfc_simplify_ubound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
8426 return simplify_bound (array, dim, kind, 1);
8429 gfc_expr *
8430 gfc_simplify_ucobound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
8432 return simplify_cobound (array, dim, kind, 1);
8436 gfc_expr *
8437 gfc_simplify_unpack (gfc_expr *vector, gfc_expr *mask, gfc_expr *field)
8439 gfc_expr *result, *e;
8440 gfc_constructor *vector_ctor, *mask_ctor, *field_ctor;
8442 if (!is_constant_array_expr (vector)
8443 || !is_constant_array_expr (mask)
8444 || (!gfc_is_constant_expr (field)
8445 && !is_constant_array_expr (field)))
8446 return NULL;
8448 result = gfc_get_array_expr (vector->ts.type, vector->ts.kind,
8449 &vector->where);
8450 if (vector->ts.type == BT_DERIVED)
8451 result->ts.u.derived = vector->ts.u.derived;
8452 result->rank = mask->rank;
8453 result->shape = gfc_copy_shape (mask->shape, mask->rank);
8455 if (vector->ts.type == BT_CHARACTER)
8456 result->ts.u.cl = vector->ts.u.cl;
8458 vector_ctor = gfc_constructor_first (vector->value.constructor);
8459 mask_ctor = gfc_constructor_first (mask->value.constructor);
8460 field_ctor
8461 = field->expr_type == EXPR_ARRAY
8462 ? gfc_constructor_first (field->value.constructor)
8463 : NULL;
8465 while (mask_ctor)
8467 if (mask_ctor->expr->value.logical)
8469 gcc_assert (vector_ctor);
8470 e = gfc_copy_expr (vector_ctor->expr);
8471 vector_ctor = gfc_constructor_next (vector_ctor);
8473 else if (field->expr_type == EXPR_ARRAY)
8474 e = gfc_copy_expr (field_ctor->expr);
8475 else
8476 e = gfc_copy_expr (field);
8478 gfc_constructor_append_expr (&result->value.constructor, e, NULL);
8480 mask_ctor = gfc_constructor_next (mask_ctor);
8481 field_ctor = gfc_constructor_next (field_ctor);
8484 return result;
8488 gfc_expr *
8489 gfc_simplify_verify (gfc_expr *s, gfc_expr *set, gfc_expr *b, gfc_expr *kind)
8491 gfc_expr *result;
8492 int back;
8493 size_t index, len, lenset;
8494 size_t i;
8495 int k = get_kind (BT_INTEGER, kind, "VERIFY", gfc_default_integer_kind);
8497 if (k == -1)
8498 return &gfc_bad_expr;
8500 if (s->expr_type != EXPR_CONSTANT || set->expr_type != EXPR_CONSTANT
8501 || ( b != NULL && b->expr_type != EXPR_CONSTANT))
8502 return NULL;
8504 if (b != NULL && b->value.logical != 0)
8505 back = 1;
8506 else
8507 back = 0;
8509 result = gfc_get_constant_expr (BT_INTEGER, k, &s->where);
8511 len = s->value.character.length;
8512 lenset = set->value.character.length;
8514 if (len == 0)
8516 mpz_set_ui (result->value.integer, 0);
8517 return result;
8520 if (back == 0)
8522 if (lenset == 0)
8524 mpz_set_ui (result->value.integer, 1);
8525 return result;
8528 index = wide_strspn (s->value.character.string,
8529 set->value.character.string) + 1;
8530 if (index > len)
8531 index = 0;
8534 else
8536 if (lenset == 0)
8538 mpz_set_ui (result->value.integer, len);
8539 return result;
8541 for (index = len; index > 0; index --)
8543 for (i = 0; i < lenset; i++)
8545 if (s->value.character.string[index - 1]
8546 == set->value.character.string[i])
8547 break;
8549 if (i == lenset)
8550 break;
8554 mpz_set_ui (result->value.integer, index);
8555 return result;
8559 gfc_expr *
8560 gfc_simplify_xor (gfc_expr *x, gfc_expr *y)
8562 gfc_expr *result;
8563 int kind;
8565 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
8566 return NULL;
8568 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
8570 switch (x->ts.type)
8572 case BT_INTEGER:
8573 result = gfc_get_constant_expr (BT_INTEGER, kind, &x->where);
8574 mpz_xor (result->value.integer, x->value.integer, y->value.integer);
8575 return range_check (result, "XOR");
8577 case BT_LOGICAL:
8578 return gfc_get_logical_expr (kind, &x->where,
8579 (x->value.logical && !y->value.logical)
8580 || (!x->value.logical && y->value.logical));
8582 default:
8583 gcc_unreachable ();
8588 /****************** Constant simplification *****************/
8590 /* Master function to convert one constant to another. While this is
8591 used as a simplification function, it requires the destination type
8592 and kind information which is supplied by a special case in
8593 do_simplify(). */
8595 gfc_expr *
8596 gfc_convert_constant (gfc_expr *e, bt type, int kind)
8598 gfc_expr *result, *(*f) (gfc_expr *, int);
8599 gfc_constructor *c, *t;
8601 switch (e->ts.type)
8603 case BT_INTEGER:
8604 switch (type)
8606 case BT_INTEGER:
8607 f = gfc_int2int;
8608 break;
8609 case BT_REAL:
8610 f = gfc_int2real;
8611 break;
8612 case BT_COMPLEX:
8613 f = gfc_int2complex;
8614 break;
8615 case BT_LOGICAL:
8616 f = gfc_int2log;
8617 break;
8618 default:
8619 goto oops;
8621 break;
8623 case BT_REAL:
8624 switch (type)
8626 case BT_INTEGER:
8627 f = gfc_real2int;
8628 break;
8629 case BT_REAL:
8630 f = gfc_real2real;
8631 break;
8632 case BT_COMPLEX:
8633 f = gfc_real2complex;
8634 break;
8635 default:
8636 goto oops;
8638 break;
8640 case BT_COMPLEX:
8641 switch (type)
8643 case BT_INTEGER:
8644 f = gfc_complex2int;
8645 break;
8646 case BT_REAL:
8647 f = gfc_complex2real;
8648 break;
8649 case BT_COMPLEX:
8650 f = gfc_complex2complex;
8651 break;
8653 default:
8654 goto oops;
8656 break;
8658 case BT_LOGICAL:
8659 switch (type)
8661 case BT_INTEGER:
8662 f = gfc_log2int;
8663 break;
8664 case BT_LOGICAL:
8665 f = gfc_log2log;
8666 break;
8667 default:
8668 goto oops;
8670 break;
8672 case BT_HOLLERITH:
8673 switch (type)
8675 case BT_INTEGER:
8676 f = gfc_hollerith2int;
8677 break;
8679 case BT_REAL:
8680 f = gfc_hollerith2real;
8681 break;
8683 case BT_COMPLEX:
8684 f = gfc_hollerith2complex;
8685 break;
8687 case BT_CHARACTER:
8688 f = gfc_hollerith2character;
8689 break;
8691 case BT_LOGICAL:
8692 f = gfc_hollerith2logical;
8693 break;
8695 default:
8696 goto oops;
8698 break;
8700 case BT_CHARACTER:
8701 switch (type)
8703 case BT_INTEGER:
8704 f = gfc_character2int;
8705 break;
8707 case BT_REAL:
8708 f = gfc_character2real;
8709 break;
8711 case BT_COMPLEX:
8712 f = gfc_character2complex;
8713 break;
8715 case BT_CHARACTER:
8716 f = gfc_character2character;
8717 break;
8719 case BT_LOGICAL:
8720 f = gfc_character2logical;
8721 break;
8723 default:
8724 goto oops;
8726 break;
8728 default:
8729 oops:
8730 return &gfc_bad_expr;
8733 result = NULL;
8735 switch (e->expr_type)
8737 case EXPR_CONSTANT:
8738 result = f (e, kind);
8739 if (result == NULL)
8740 return &gfc_bad_expr;
8741 break;
8743 case EXPR_ARRAY:
8744 if (!gfc_is_constant_expr (e))
8745 break;
8747 result = gfc_get_array_expr (type, kind, &e->where);
8748 result->shape = gfc_copy_shape (e->shape, e->rank);
8749 result->rank = e->rank;
8751 for (c = gfc_constructor_first (e->value.constructor);
8752 c; c = gfc_constructor_next (c))
8754 gfc_expr *tmp;
8755 if (c->iterator == NULL)
8757 if (c->expr->expr_type == EXPR_ARRAY)
8758 tmp = gfc_convert_constant (c->expr, type, kind);
8759 else if (c->expr->expr_type == EXPR_OP)
8761 if (!gfc_simplify_expr (c->expr, 1))
8762 return &gfc_bad_expr;
8763 tmp = f (c->expr, kind);
8765 else
8766 tmp = f (c->expr, kind);
8768 else
8769 tmp = gfc_convert_constant (c->expr, type, kind);
8771 if (tmp == NULL || tmp == &gfc_bad_expr)
8773 gfc_free_expr (result);
8774 return NULL;
8777 t = gfc_constructor_append_expr (&result->value.constructor,
8778 tmp, &c->where);
8779 if (c->iterator)
8780 t->iterator = gfc_copy_iterator (c->iterator);
8783 break;
8785 default:
8786 break;
8789 return result;
8793 /* Function for converting character constants. */
8794 gfc_expr *
8795 gfc_convert_char_constant (gfc_expr *e, bt type ATTRIBUTE_UNUSED, int kind)
8797 gfc_expr *result;
8798 int i;
8800 if (!gfc_is_constant_expr (e))
8801 return NULL;
8803 if (e->expr_type == EXPR_CONSTANT)
8805 /* Simple case of a scalar. */
8806 result = gfc_get_constant_expr (BT_CHARACTER, kind, &e->where);
8807 if (result == NULL)
8808 return &gfc_bad_expr;
8810 result->value.character.length = e->value.character.length;
8811 result->value.character.string
8812 = gfc_get_wide_string (e->value.character.length + 1);
8813 memcpy (result->value.character.string, e->value.character.string,
8814 (e->value.character.length + 1) * sizeof (gfc_char_t));
8816 /* Check we only have values representable in the destination kind. */
8817 for (i = 0; i < result->value.character.length; i++)
8818 if (!gfc_check_character_range (result->value.character.string[i],
8819 kind))
8821 gfc_error ("Character %qs in string at %L cannot be converted "
8822 "into character kind %d",
8823 gfc_print_wide_char (result->value.character.string[i]),
8824 &e->where, kind);
8825 gfc_free_expr (result);
8826 return &gfc_bad_expr;
8829 return result;
8831 else if (e->expr_type == EXPR_ARRAY)
8833 /* For an array constructor, we convert each constructor element. */
8834 gfc_constructor *c;
8836 result = gfc_get_array_expr (type, kind, &e->where);
8837 result->shape = gfc_copy_shape (e->shape, e->rank);
8838 result->rank = e->rank;
8839 result->ts.u.cl = e->ts.u.cl;
8841 for (c = gfc_constructor_first (e->value.constructor);
8842 c; c = gfc_constructor_next (c))
8844 gfc_expr *tmp = gfc_convert_char_constant (c->expr, type, kind);
8845 if (tmp == &gfc_bad_expr)
8847 gfc_free_expr (result);
8848 return &gfc_bad_expr;
8851 if (tmp == NULL)
8853 gfc_free_expr (result);
8854 return NULL;
8857 gfc_constructor_append_expr (&result->value.constructor,
8858 tmp, &c->where);
8861 return result;
8863 else
8864 return NULL;
8868 gfc_expr *
8869 gfc_simplify_compiler_options (void)
8871 char *str;
8872 gfc_expr *result;
8874 str = gfc_get_option_string ();
8875 result = gfc_get_character_expr (gfc_default_character_kind,
8876 &gfc_current_locus, str, strlen (str));
8877 free (str);
8878 return result;
8882 gfc_expr *
8883 gfc_simplify_compiler_version (void)
8885 char *buffer;
8886 size_t len;
8888 len = strlen ("GCC version ") + strlen (version_string);
8889 buffer = XALLOCAVEC (char, len + 1);
8890 snprintf (buffer, len + 1, "GCC version %s", version_string);
8891 return gfc_get_character_expr (gfc_default_character_kind,
8892 &gfc_current_locus, buffer, len);
8895 /* Simplification routines for intrinsics of IEEE modules. */
8897 gfc_expr *
8898 simplify_ieee_selected_real_kind (gfc_expr *expr)
8900 gfc_actual_arglist *arg;
8901 gfc_expr *p = NULL, *q = NULL, *rdx = NULL;
8903 arg = expr->value.function.actual;
8904 p = arg->expr;
8905 if (arg->next)
8907 q = arg->next->expr;
8908 if (arg->next->next)
8909 rdx = arg->next->next->expr;
8912 /* Currently, if IEEE is supported and this module is built, it means
8913 all our floating-point types conform to IEEE. Hence, we simply handle
8914 IEEE_SELECTED_REAL_KIND like SELECTED_REAL_KIND. */
8915 return gfc_simplify_selected_real_kind (p, q, rdx);
8918 gfc_expr *
8919 simplify_ieee_support (gfc_expr *expr)
8921 /* We consider that if the IEEE modules are loaded, we have full support
8922 for flags, halting and rounding, which are the three functions
8923 (IEEE_SUPPORT_{FLAG,HALTING,ROUNDING}) allowed in constant
8924 expressions. One day, we will need libgfortran to detect support and
8925 communicate it back to us, allowing for partial support. */
8927 return gfc_get_logical_expr (gfc_default_logical_kind, &expr->where,
8928 true);
8931 bool
8932 matches_ieee_function_name (gfc_symbol *sym, const char *name)
8934 int n = strlen(name);
8936 if (!strncmp(sym->name, name, n))
8937 return true;
8939 /* If a generic was used and renamed, we need more work to find out.
8940 Compare the specific name. */
8941 if (sym->generic && !strncmp(sym->generic->sym->name, name, n))
8942 return true;
8944 return false;
8947 gfc_expr *
8948 gfc_simplify_ieee_functions (gfc_expr *expr)
8950 gfc_symbol* sym = expr->symtree->n.sym;
8952 if (matches_ieee_function_name(sym, "ieee_selected_real_kind"))
8953 return simplify_ieee_selected_real_kind (expr);
8954 else if (matches_ieee_function_name(sym, "ieee_support_flag")
8955 || matches_ieee_function_name(sym, "ieee_support_halting")
8956 || matches_ieee_function_name(sym, "ieee_support_rounding"))
8957 return simplify_ieee_support (expr);
8958 else
8959 return NULL;