libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / fortran / simplify.cc
blob2f6c3c39dad883721c9d729fafcb2afaa8e03a9d
1 /* Simplify intrinsic functions at compile-time.
2 Copyright (C) 2000-2024 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 void
151 gfc_convert_mpz_to_unsigned (mpz_t x, int bitsize, bool sign)
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 (sign && 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 (sign && 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 /* A non-zero-sized constant array shall have a non-empty constructor. */
237 if (e->rank > 0 && e->shape != NULL && e->value.constructor == NULL)
239 mpz_init_set_ui (size, 1);
240 for (int j = 0; j < e->rank; j++)
241 mpz_mul (size, size, e->shape[j]);
242 bool not_size0 = (mpz_cmp_si (size, 0) != 0);
243 mpz_clear (size);
244 if (not_size0)
245 return false;
248 for (c = gfc_constructor_first (e->value.constructor);
249 c; c = gfc_constructor_next (c))
250 if (c->expr->expr_type != EXPR_CONSTANT
251 && c->expr->expr_type != EXPR_STRUCTURE)
253 array_OK = false;
254 break;
257 /* Check and expand the constructor. We do this when either
258 gfc_init_expr_flag is set or for not too large array constructors. */
259 bool expand;
260 expand = (e->rank == 1
261 && e->shape
262 && (mpz_cmp_ui (e->shape[0], flag_max_array_constructor) < 0));
264 if (!array_OK && (gfc_init_expr_flag || expand) && e->rank == 1)
266 bool saved_init_expr_flag = gfc_init_expr_flag;
267 array_OK = gfc_reduce_init_expr (e);
268 /* gfc_reduce_init_expr resets the flag. */
269 gfc_init_expr_flag = saved_init_expr_flag;
271 else
272 return array_OK;
274 /* Recheck to make sure that any EXPR_ARRAYs have gone. */
275 for (c = gfc_constructor_first (e->value.constructor);
276 c; c = gfc_constructor_next (c))
277 if (c->expr->expr_type != EXPR_CONSTANT
278 && c->expr->expr_type != EXPR_STRUCTURE)
279 return false;
281 /* Make sure that the array has a valid shape. */
282 if (e->shape == NULL && e->rank == 1)
284 if (!gfc_array_size(e, &size))
285 return false;
286 e->shape = gfc_get_shape (1);
287 mpz_init_set (e->shape[0], size);
288 mpz_clear (size);
291 return array_OK;
294 bool
295 gfc_is_constant_array_expr (gfc_expr *e)
297 return is_constant_array_expr (e);
301 /* Test for a size zero array. */
302 bool
303 gfc_is_size_zero_array (gfc_expr *array)
306 if (array->rank == 0)
307 return false;
309 if (array->expr_type == EXPR_VARIABLE && array->rank > 0
310 && array->symtree->n.sym->attr.flavor == FL_PARAMETER
311 && array->shape != NULL)
313 for (int i = 0; i < array->rank; i++)
314 if (mpz_cmp_si (array->shape[i], 0) <= 0)
315 return true;
317 return false;
320 if (array->expr_type == EXPR_ARRAY)
321 return array->value.constructor == NULL;
323 return false;
327 /* Initialize a transformational result expression with a given value. */
329 static void
330 init_result_expr (gfc_expr *e, int init, gfc_expr *array)
332 if (e && e->expr_type == EXPR_ARRAY)
334 gfc_constructor *ctor = gfc_constructor_first (e->value.constructor);
335 while (ctor)
337 init_result_expr (ctor->expr, init, array);
338 ctor = gfc_constructor_next (ctor);
341 else if (e && e->expr_type == EXPR_CONSTANT)
343 int i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
344 HOST_WIDE_INT length;
345 gfc_char_t *string;
347 switch (e->ts.type)
349 case BT_LOGICAL:
350 e->value.logical = (init ? 1 : 0);
351 break;
353 case BT_INTEGER:
354 if (init == INT_MIN)
355 mpz_set (e->value.integer, gfc_integer_kinds[i].min_int);
356 else if (init == INT_MAX)
357 mpz_set (e->value.integer, gfc_integer_kinds[i].huge);
358 else
359 mpz_set_si (e->value.integer, init);
360 break;
362 case BT_UNSIGNED:
363 if (init == INT_MIN)
364 mpz_set_ui (e->value.integer, 0);
365 else if (init == INT_MAX)
366 mpz_set (e->value.integer, gfc_unsigned_kinds[i].huge);
367 else
368 mpz_set_ui (e->value.integer, init);
369 break;
371 case BT_REAL:
372 if (init == INT_MIN)
374 mpfr_set (e->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
375 mpfr_neg (e->value.real, e->value.real, GFC_RND_MODE);
377 else if (init == INT_MAX)
378 mpfr_set (e->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
379 else
380 mpfr_set_si (e->value.real, init, GFC_RND_MODE);
381 break;
383 case BT_COMPLEX:
384 mpc_set_si (e->value.complex, init, GFC_MPC_RND_MODE);
385 break;
387 case BT_CHARACTER:
388 if (init == INT_MIN)
390 gfc_expr *len = gfc_simplify_len (array, NULL);
391 gfc_extract_hwi (len, &length);
392 string = gfc_get_wide_string (length + 1);
393 gfc_wide_memset (string, 0, length);
395 else if (init == INT_MAX)
397 gfc_expr *len = gfc_simplify_len (array, NULL);
398 gfc_extract_hwi (len, &length);
399 string = gfc_get_wide_string (length + 1);
400 gfc_wide_memset (string, 255, length);
402 else
404 length = 0;
405 string = gfc_get_wide_string (1);
408 string[length] = '\0';
409 e->value.character.length = length;
410 e->value.character.string = string;
411 break;
413 default:
414 gcc_unreachable();
417 else
418 gcc_unreachable();
422 /* Helper function for gfc_simplify_dot_product() and gfc_simplify_matmul;
423 if conj_a is true, the matrix_a is complex conjugated. */
425 static gfc_expr *
426 compute_dot_product (gfc_expr *matrix_a, int stride_a, int offset_a,
427 gfc_expr *matrix_b, int stride_b, int offset_b,
428 bool conj_a)
430 gfc_expr *result, *a, *b, *c;
432 /* Set result to an UNSIGNED of correct kind for unsigned,
433 INTEGER(1) 0 for other numeric types, and .false. for
434 LOGICAL. Mixed-mode math in the loop will promote result to the
435 correct type and kind. */
436 if (matrix_a->ts.type == BT_LOGICAL)
437 result = gfc_get_logical_expr (gfc_default_logical_kind, NULL, false);
438 else if (matrix_a->ts.type == BT_UNSIGNED)
440 int kind = MAX (matrix_a->ts.kind, matrix_b->ts.kind);
441 result = gfc_get_unsigned_expr (kind, NULL, 0);
443 else
444 result = gfc_get_int_expr (1, NULL, 0);
446 result->where = matrix_a->where;
448 a = gfc_constructor_lookup_expr (matrix_a->value.constructor, offset_a);
449 b = gfc_constructor_lookup_expr (matrix_b->value.constructor, offset_b);
450 while (a && b)
452 /* Copying of expressions is required as operands are free'd
453 by the gfc_arith routines. */
454 switch (result->ts.type)
456 case BT_LOGICAL:
457 result = gfc_or (result,
458 gfc_and (gfc_copy_expr (a),
459 gfc_copy_expr (b)));
460 break;
462 case BT_INTEGER:
463 case BT_REAL:
464 case BT_COMPLEX:
465 case BT_UNSIGNED:
466 if (conj_a && a->ts.type == BT_COMPLEX)
467 c = gfc_simplify_conjg (a);
468 else
469 c = gfc_copy_expr (a);
470 result = gfc_add (result, gfc_multiply (c, gfc_copy_expr (b)));
471 break;
473 default:
474 gcc_unreachable();
477 offset_a += stride_a;
478 a = gfc_constructor_lookup_expr (matrix_a->value.constructor, offset_a);
480 offset_b += stride_b;
481 b = gfc_constructor_lookup_expr (matrix_b->value.constructor, offset_b);
484 return result;
488 /* Build a result expression for transformational intrinsics,
489 depending on DIM. */
491 static gfc_expr *
492 transformational_result (gfc_expr *array, gfc_expr *dim, bt type,
493 int kind, locus* where)
495 gfc_expr *result;
496 int i, nelem;
498 if (!dim || array->rank == 1)
499 return gfc_get_constant_expr (type, kind, where);
501 result = gfc_get_array_expr (type, kind, where);
502 result->shape = gfc_copy_shape_excluding (array->shape, array->rank, dim);
503 result->rank = array->rank - 1;
505 /* gfc_array_size() would count the number of elements in the constructor,
506 we have not built those yet. */
507 nelem = 1;
508 for (i = 0; i < result->rank; ++i)
509 nelem *= mpz_get_ui (result->shape[i]);
511 for (i = 0; i < nelem; ++i)
513 gfc_constructor_append_expr (&result->value.constructor,
514 gfc_get_constant_expr (type, kind, where),
515 NULL);
518 return result;
522 typedef gfc_expr* (*transformational_op)(gfc_expr*, gfc_expr*);
524 /* Wrapper function, implements 'op1 += 1'. Only called if MASK
525 of COUNT intrinsic is .TRUE..
527 Interface and implementation mimics arith functions as
528 gfc_add, gfc_multiply, etc. */
530 static gfc_expr *
531 gfc_count (gfc_expr *op1, gfc_expr *op2)
533 gfc_expr *result;
535 gcc_assert (op1->ts.type == BT_INTEGER);
536 gcc_assert (op2->ts.type == BT_LOGICAL);
537 gcc_assert (op2->value.logical);
539 result = gfc_copy_expr (op1);
540 mpz_add_ui (result->value.integer, result->value.integer, 1);
542 gfc_free_expr (op1);
543 gfc_free_expr (op2);
544 return result;
548 /* Transforms an ARRAY with operation OP, according to MASK, to a
549 scalar RESULT. E.g. called if
551 REAL, PARAMETER :: array(n, m) = ...
552 REAL, PARAMETER :: s = SUM(array)
554 where OP == gfc_add(). */
556 static gfc_expr *
557 simplify_transformation_to_scalar (gfc_expr *result, gfc_expr *array, gfc_expr *mask,
558 transformational_op op)
560 gfc_expr *a, *m;
561 gfc_constructor *array_ctor, *mask_ctor;
563 /* Shortcut for constant .FALSE. MASK. */
564 if (mask
565 && mask->expr_type == EXPR_CONSTANT
566 && !mask->value.logical)
567 return result;
569 array_ctor = gfc_constructor_first (array->value.constructor);
570 mask_ctor = NULL;
571 if (mask && mask->expr_type == EXPR_ARRAY)
572 mask_ctor = gfc_constructor_first (mask->value.constructor);
574 while (array_ctor)
576 a = array_ctor->expr;
577 array_ctor = gfc_constructor_next (array_ctor);
579 /* A constant MASK equals .TRUE. here and can be ignored. */
580 if (mask_ctor)
582 m = mask_ctor->expr;
583 mask_ctor = gfc_constructor_next (mask_ctor);
584 if (!m->value.logical)
585 continue;
588 result = op (result, gfc_copy_expr (a));
589 if (!result)
590 return result;
593 return result;
596 /* Transforms an ARRAY with operation OP, according to MASK, to an
597 array RESULT. E.g. called if
599 REAL, PARAMETER :: array(n, m) = ...
600 REAL, PARAMETER :: s(n) = PROD(array, DIM=1)
602 where OP == gfc_multiply().
603 The result might be post processed using post_op. */
605 static gfc_expr *
606 simplify_transformation_to_array (gfc_expr *result, gfc_expr *array, gfc_expr *dim,
607 gfc_expr *mask, transformational_op op,
608 transformational_op post_op)
610 mpz_t size;
611 int done, i, n, arraysize, resultsize, dim_index, dim_extent, dim_stride;
612 gfc_expr **arrayvec, **resultvec, **base, **src, **dest;
613 gfc_constructor *array_ctor, *mask_ctor, *result_ctor;
615 int count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS],
616 sstride[GFC_MAX_DIMENSIONS], dstride[GFC_MAX_DIMENSIONS],
617 tmpstride[GFC_MAX_DIMENSIONS];
619 /* Shortcut for constant .FALSE. MASK. */
620 if (mask
621 && mask->expr_type == EXPR_CONSTANT
622 && !mask->value.logical)
623 return result;
625 /* Build an indexed table for array element expressions to minimize
626 linked-list traversal. Masked elements are set to NULL. */
627 gfc_array_size (array, &size);
628 arraysize = mpz_get_ui (size);
629 mpz_clear (size);
631 arrayvec = XCNEWVEC (gfc_expr*, arraysize);
633 array_ctor = gfc_constructor_first (array->value.constructor);
634 mask_ctor = NULL;
635 if (mask && mask->expr_type == EXPR_ARRAY)
636 mask_ctor = gfc_constructor_first (mask->value.constructor);
638 for (i = 0; i < arraysize; ++i)
640 arrayvec[i] = array_ctor->expr;
641 array_ctor = gfc_constructor_next (array_ctor);
643 if (mask_ctor)
645 if (!mask_ctor->expr->value.logical)
646 arrayvec[i] = NULL;
648 mask_ctor = gfc_constructor_next (mask_ctor);
652 /* Same for the result expression. */
653 gfc_array_size (result, &size);
654 resultsize = mpz_get_ui (size);
655 mpz_clear (size);
657 resultvec = XCNEWVEC (gfc_expr*, resultsize);
658 result_ctor = gfc_constructor_first (result->value.constructor);
659 for (i = 0; i < resultsize; ++i)
661 resultvec[i] = result_ctor->expr;
662 result_ctor = gfc_constructor_next (result_ctor);
665 gfc_extract_int (dim, &dim_index);
666 dim_index -= 1; /* zero-base index */
667 dim_extent = 0;
668 dim_stride = 0;
670 for (i = 0, n = 0; i < array->rank; ++i)
672 count[i] = 0;
673 tmpstride[i] = (i == 0) ? 1 : tmpstride[i-1] * mpz_get_si (array->shape[i-1]);
674 if (i == dim_index)
676 dim_extent = mpz_get_si (array->shape[i]);
677 dim_stride = tmpstride[i];
678 continue;
681 extent[n] = mpz_get_si (array->shape[i]);
682 sstride[n] = tmpstride[i];
683 dstride[n] = (n == 0) ? 1 : dstride[n-1] * extent[n-1];
684 n += 1;
687 done = resultsize <= 0;
688 base = arrayvec;
689 dest = resultvec;
690 while (!done)
692 for (src = base, n = 0; n < dim_extent; src += dim_stride, ++n)
693 if (*src)
694 *dest = op (*dest, gfc_copy_expr (*src));
696 if (post_op)
697 *dest = post_op (*dest, *dest);
699 count[0]++;
700 base += sstride[0];
701 dest += dstride[0];
703 n = 0;
704 while (!done && count[n] == extent[n])
706 count[n] = 0;
707 base -= sstride[n] * extent[n];
708 dest -= dstride[n] * extent[n];
710 n++;
711 if (n < result->rank)
713 /* If the nested loop is unrolled GFC_MAX_DIMENSIONS
714 times, we'd warn for the last iteration, because the
715 array index will have already been incremented to the
716 array sizes, and we can't tell that this must make
717 the test against result->rank false, because ranks
718 must not exceed GFC_MAX_DIMENSIONS. */
719 GCC_DIAGNOSTIC_PUSH_IGNORED (-Warray-bounds)
720 count[n]++;
721 base += sstride[n];
722 dest += dstride[n];
723 GCC_DIAGNOSTIC_POP
725 else
726 done = true;
730 /* Place updated expression in result constructor. */
731 result_ctor = gfc_constructor_first (result->value.constructor);
732 for (i = 0; i < resultsize; ++i)
734 result_ctor->expr = resultvec[i];
735 result_ctor = gfc_constructor_next (result_ctor);
738 free (arrayvec);
739 free (resultvec);
740 return result;
744 static gfc_expr *
745 simplify_transformation (gfc_expr *array, gfc_expr *dim, gfc_expr *mask,
746 int init_val, transformational_op op)
748 gfc_expr *result;
749 bool size_zero;
751 size_zero = gfc_is_size_zero_array (array);
753 if (!(is_constant_array_expr (array) || size_zero)
754 || array->shape == NULL
755 || !gfc_is_constant_expr (dim))
756 return NULL;
758 if (mask
759 && !is_constant_array_expr (mask)
760 && mask->expr_type != EXPR_CONSTANT)
761 return NULL;
763 result = transformational_result (array, dim, array->ts.type,
764 array->ts.kind, &array->where);
765 init_result_expr (result, init_val, array);
767 if (size_zero)
768 return result;
770 return !dim || array->rank == 1 ?
771 simplify_transformation_to_scalar (result, array, mask, op) :
772 simplify_transformation_to_array (result, array, dim, mask, op, NULL);
776 /********************** Simplification functions *****************************/
778 gfc_expr *
779 gfc_simplify_abs (gfc_expr *e)
781 gfc_expr *result;
783 if (e->expr_type != EXPR_CONSTANT)
784 return NULL;
786 switch (e->ts.type)
788 case BT_INTEGER:
789 result = gfc_get_constant_expr (BT_INTEGER, e->ts.kind, &e->where);
790 mpz_abs (result->value.integer, e->value.integer);
791 return range_check (result, "IABS");
793 case BT_REAL:
794 result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
795 mpfr_abs (result->value.real, e->value.real, GFC_RND_MODE);
796 return range_check (result, "ABS");
798 case BT_COMPLEX:
799 gfc_set_model_kind (e->ts.kind);
800 result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
801 mpc_abs (result->value.real, e->value.complex, GFC_RND_MODE);
802 return range_check (result, "CABS");
804 default:
805 gfc_internal_error ("gfc_simplify_abs(): Bad type");
810 static gfc_expr *
811 simplify_achar_char (gfc_expr *e, gfc_expr *k, const char *name, bool ascii)
813 gfc_expr *result;
814 int kind;
815 bool too_large = false;
817 if (e->expr_type != EXPR_CONSTANT)
818 return NULL;
820 kind = get_kind (BT_CHARACTER, k, name, gfc_default_character_kind);
821 if (kind == -1)
822 return &gfc_bad_expr;
824 if (mpz_cmp_si (e->value.integer, 0) < 0)
826 gfc_error ("Argument of %s function at %L is negative", name,
827 &e->where);
828 return &gfc_bad_expr;
831 if (ascii && warn_surprising && mpz_cmp_si (e->value.integer, 127) > 0)
832 gfc_warning (OPT_Wsurprising,
833 "Argument of %s function at %L outside of range [0,127]",
834 name, &e->where);
836 if (kind == 1 && mpz_cmp_si (e->value.integer, 255) > 0)
837 too_large = true;
838 else if (kind == 4)
840 mpz_t t;
841 mpz_init_set_ui (t, 2);
842 mpz_pow_ui (t, t, 32);
843 mpz_sub_ui (t, t, 1);
844 if (mpz_cmp (e->value.integer, t) > 0)
845 too_large = true;
846 mpz_clear (t);
849 if (too_large)
851 gfc_error ("Argument of %s function at %L is too large for the "
852 "collating sequence of kind %d", name, &e->where, kind);
853 return &gfc_bad_expr;
856 result = gfc_get_character_expr (kind, &e->where, NULL, 1);
857 result->value.character.string[0] = mpz_get_ui (e->value.integer);
859 return result;
864 /* We use the processor's collating sequence, because all
865 systems that gfortran currently works on are ASCII. */
867 gfc_expr *
868 gfc_simplify_achar (gfc_expr *e, gfc_expr *k)
870 return simplify_achar_char (e, k, "ACHAR", true);
874 gfc_expr *
875 gfc_simplify_acos (gfc_expr *x)
877 gfc_expr *result;
879 if (x->expr_type != EXPR_CONSTANT)
880 return NULL;
882 switch (x->ts.type)
884 case BT_REAL:
885 if (mpfr_cmp_si (x->value.real, 1) > 0
886 || mpfr_cmp_si (x->value.real, -1) < 0)
888 gfc_error ("Argument of ACOS at %L must be between -1 and 1",
889 &x->where);
890 return &gfc_bad_expr;
892 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
893 mpfr_acos (result->value.real, x->value.real, GFC_RND_MODE);
894 break;
896 case BT_COMPLEX:
897 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
898 mpc_acos (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
899 break;
901 default:
902 gfc_internal_error ("in gfc_simplify_acos(): Bad type");
905 return range_check (result, "ACOS");
908 gfc_expr *
909 gfc_simplify_acosh (gfc_expr *x)
911 gfc_expr *result;
913 if (x->expr_type != EXPR_CONSTANT)
914 return NULL;
916 switch (x->ts.type)
918 case BT_REAL:
919 if (mpfr_cmp_si (x->value.real, 1) < 0)
921 gfc_error ("Argument of ACOSH at %L must not be less than 1",
922 &x->where);
923 return &gfc_bad_expr;
926 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
927 mpfr_acosh (result->value.real, x->value.real, GFC_RND_MODE);
928 break;
930 case BT_COMPLEX:
931 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
932 mpc_acosh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
933 break;
935 default:
936 gfc_internal_error ("in gfc_simplify_acosh(): Bad type");
939 return range_check (result, "ACOSH");
942 gfc_expr *
943 gfc_simplify_adjustl (gfc_expr *e)
945 gfc_expr *result;
946 int count, i, len;
947 gfc_char_t ch;
949 if (e->expr_type != EXPR_CONSTANT)
950 return NULL;
952 len = e->value.character.length;
954 for (count = 0, i = 0; i < len; ++i)
956 ch = e->value.character.string[i];
957 if (ch != ' ')
958 break;
959 ++count;
962 result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, len);
963 for (i = 0; i < len - count; ++i)
964 result->value.character.string[i] = e->value.character.string[count + i];
966 return result;
970 gfc_expr *
971 gfc_simplify_adjustr (gfc_expr *e)
973 gfc_expr *result;
974 int count, i, len;
975 gfc_char_t ch;
977 if (e->expr_type != EXPR_CONSTANT)
978 return NULL;
980 len = e->value.character.length;
982 for (count = 0, i = len - 1; i >= 0; --i)
984 ch = e->value.character.string[i];
985 if (ch != ' ')
986 break;
987 ++count;
990 result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, len);
991 for (i = 0; i < count; ++i)
992 result->value.character.string[i] = ' ';
994 for (i = count; i < len; ++i)
995 result->value.character.string[i] = e->value.character.string[i - count];
997 return result;
1001 gfc_expr *
1002 gfc_simplify_aimag (gfc_expr *e)
1004 gfc_expr *result;
1006 if (e->expr_type != EXPR_CONSTANT)
1007 return NULL;
1009 result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
1010 mpfr_set (result->value.real, mpc_imagref (e->value.complex), GFC_RND_MODE);
1012 return range_check (result, "AIMAG");
1016 gfc_expr *
1017 gfc_simplify_aint (gfc_expr *e, gfc_expr *k)
1019 gfc_expr *rtrunc, *result;
1020 int kind;
1022 kind = get_kind (BT_REAL, k, "AINT", e->ts.kind);
1023 if (kind == -1)
1024 return &gfc_bad_expr;
1026 if (e->expr_type != EXPR_CONSTANT)
1027 return NULL;
1029 rtrunc = gfc_copy_expr (e);
1030 mpfr_trunc (rtrunc->value.real, e->value.real);
1032 result = gfc_real2real (rtrunc, kind);
1034 gfc_free_expr (rtrunc);
1036 return range_check (result, "AINT");
1040 gfc_expr *
1041 gfc_simplify_all (gfc_expr *mask, gfc_expr *dim)
1043 return simplify_transformation (mask, dim, NULL, true, gfc_and);
1047 gfc_expr *
1048 gfc_simplify_dint (gfc_expr *e)
1050 gfc_expr *rtrunc, *result;
1052 if (e->expr_type != EXPR_CONSTANT)
1053 return NULL;
1055 rtrunc = gfc_copy_expr (e);
1056 mpfr_trunc (rtrunc->value.real, e->value.real);
1058 result = gfc_real2real (rtrunc, gfc_default_double_kind);
1060 gfc_free_expr (rtrunc);
1062 return range_check (result, "DINT");
1066 gfc_expr *
1067 gfc_simplify_dreal (gfc_expr *e)
1069 gfc_expr *result = NULL;
1071 if (e->expr_type != EXPR_CONSTANT)
1072 return NULL;
1074 result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
1075 mpc_real (result->value.real, e->value.complex, GFC_RND_MODE);
1077 return range_check (result, "DREAL");
1081 gfc_expr *
1082 gfc_simplify_anint (gfc_expr *e, gfc_expr *k)
1084 gfc_expr *result;
1085 int kind;
1087 kind = get_kind (BT_REAL, k, "ANINT", e->ts.kind);
1088 if (kind == -1)
1089 return &gfc_bad_expr;
1091 if (e->expr_type != EXPR_CONSTANT)
1092 return NULL;
1094 result = gfc_get_constant_expr (e->ts.type, kind, &e->where);
1095 mpfr_round (result->value.real, e->value.real);
1097 return range_check (result, "ANINT");
1101 gfc_expr *
1102 gfc_simplify_and (gfc_expr *x, gfc_expr *y)
1104 gfc_expr *result;
1105 int kind;
1107 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1108 return NULL;
1110 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
1112 switch (x->ts.type)
1114 case BT_INTEGER:
1115 result = gfc_get_constant_expr (BT_INTEGER, kind, &x->where);
1116 mpz_and (result->value.integer, x->value.integer, y->value.integer);
1117 return range_check (result, "AND");
1119 case BT_LOGICAL:
1120 return gfc_get_logical_expr (kind, &x->where,
1121 x->value.logical && y->value.logical);
1123 default:
1124 gcc_unreachable ();
1129 gfc_expr *
1130 gfc_simplify_any (gfc_expr *mask, gfc_expr *dim)
1132 return simplify_transformation (mask, dim, NULL, false, gfc_or);
1136 gfc_expr *
1137 gfc_simplify_dnint (gfc_expr *e)
1139 gfc_expr *result;
1141 if (e->expr_type != EXPR_CONSTANT)
1142 return NULL;
1144 result = gfc_get_constant_expr (BT_REAL, gfc_default_double_kind, &e->where);
1145 mpfr_round (result->value.real, e->value.real);
1147 return range_check (result, "DNINT");
1151 gfc_expr *
1152 gfc_simplify_asin (gfc_expr *x)
1154 gfc_expr *result;
1156 if (x->expr_type != EXPR_CONSTANT)
1157 return NULL;
1159 switch (x->ts.type)
1161 case BT_REAL:
1162 if (mpfr_cmp_si (x->value.real, 1) > 0
1163 || mpfr_cmp_si (x->value.real, -1) < 0)
1165 gfc_error ("Argument of ASIN at %L must be between -1 and 1",
1166 &x->where);
1167 return &gfc_bad_expr;
1169 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1170 mpfr_asin (result->value.real, x->value.real, GFC_RND_MODE);
1171 break;
1173 case BT_COMPLEX:
1174 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1175 mpc_asin (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1176 break;
1178 default:
1179 gfc_internal_error ("in gfc_simplify_asin(): Bad type");
1182 return range_check (result, "ASIN");
1186 /* Convert radians to degrees, i.e., x * 180 / pi. */
1188 static void
1189 rad2deg (mpfr_t x)
1191 mpfr_t tmp;
1193 mpfr_init (tmp);
1194 mpfr_const_pi (tmp, GFC_RND_MODE);
1195 mpfr_mul_ui (x, x, 180, GFC_RND_MODE);
1196 mpfr_div (x, x, tmp, GFC_RND_MODE);
1197 mpfr_clear (tmp);
1201 /* Simplify ACOSD(X) where the returned value has units of degree. */
1203 gfc_expr *
1204 gfc_simplify_acosd (gfc_expr *x)
1206 gfc_expr *result;
1208 if (x->expr_type != EXPR_CONSTANT)
1209 return NULL;
1211 if (mpfr_cmp_si (x->value.real, 1) > 0
1212 || mpfr_cmp_si (x->value.real, -1) < 0)
1214 gfc_error ("Argument of ACOSD at %L must be between -1 and 1",
1215 &x->where);
1216 return &gfc_bad_expr;
1219 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1220 mpfr_acos (result->value.real, x->value.real, GFC_RND_MODE);
1221 rad2deg (result->value.real);
1223 return range_check (result, "ACOSD");
1227 /* Simplify asind (x) where the returned value has units of degree. */
1229 gfc_expr *
1230 gfc_simplify_asind (gfc_expr *x)
1232 gfc_expr *result;
1234 if (x->expr_type != EXPR_CONSTANT)
1235 return NULL;
1237 if (mpfr_cmp_si (x->value.real, 1) > 0
1238 || mpfr_cmp_si (x->value.real, -1) < 0)
1240 gfc_error ("Argument of ASIND at %L must be between -1 and 1",
1241 &x->where);
1242 return &gfc_bad_expr;
1245 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1246 mpfr_asin (result->value.real, x->value.real, GFC_RND_MODE);
1247 rad2deg (result->value.real);
1249 return range_check (result, "ASIND");
1253 /* Simplify atand (x) where the returned value has units of degree. */
1255 gfc_expr *
1256 gfc_simplify_atand (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);
1264 mpfr_atan (result->value.real, x->value.real, GFC_RND_MODE);
1265 rad2deg (result->value.real);
1267 return range_check (result, "ATAND");
1271 gfc_expr *
1272 gfc_simplify_asinh (gfc_expr *x)
1274 gfc_expr *result;
1276 if (x->expr_type != EXPR_CONSTANT)
1277 return NULL;
1279 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1281 switch (x->ts.type)
1283 case BT_REAL:
1284 mpfr_asinh (result->value.real, x->value.real, GFC_RND_MODE);
1285 break;
1287 case BT_COMPLEX:
1288 mpc_asinh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1289 break;
1291 default:
1292 gfc_internal_error ("in gfc_simplify_asinh(): Bad type");
1295 return range_check (result, "ASINH");
1299 gfc_expr *
1300 gfc_simplify_atan (gfc_expr *x)
1302 gfc_expr *result;
1304 if (x->expr_type != EXPR_CONSTANT)
1305 return NULL;
1307 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1309 switch (x->ts.type)
1311 case BT_REAL:
1312 mpfr_atan (result->value.real, x->value.real, GFC_RND_MODE);
1313 break;
1315 case BT_COMPLEX:
1316 mpc_atan (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1317 break;
1319 default:
1320 gfc_internal_error ("in gfc_simplify_atan(): Bad type");
1323 return range_check (result, "ATAN");
1327 gfc_expr *
1328 gfc_simplify_atanh (gfc_expr *x)
1330 gfc_expr *result;
1332 if (x->expr_type != EXPR_CONSTANT)
1333 return NULL;
1335 switch (x->ts.type)
1337 case BT_REAL:
1338 if (mpfr_cmp_si (x->value.real, 1) >= 0
1339 || mpfr_cmp_si (x->value.real, -1) <= 0)
1341 gfc_error ("Argument of ATANH at %L must be inside the range -1 "
1342 "to 1", &x->where);
1343 return &gfc_bad_expr;
1345 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1346 mpfr_atanh (result->value.real, x->value.real, GFC_RND_MODE);
1347 break;
1349 case BT_COMPLEX:
1350 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1351 mpc_atanh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1352 break;
1354 default:
1355 gfc_internal_error ("in gfc_simplify_atanh(): Bad type");
1358 return range_check (result, "ATANH");
1362 gfc_expr *
1363 gfc_simplify_atan2 (gfc_expr *y, gfc_expr *x)
1365 gfc_expr *result;
1367 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1368 return NULL;
1370 if (mpfr_zero_p (y->value.real) && mpfr_zero_p (x->value.real))
1372 gfc_error ("If first argument of ATAN2 at %L is zero, then the "
1373 "second argument must not be zero", &y->where);
1374 return &gfc_bad_expr;
1377 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1378 mpfr_atan2 (result->value.real, y->value.real, x->value.real, GFC_RND_MODE);
1380 return range_check (result, "ATAN2");
1384 gfc_expr *
1385 gfc_simplify_bessel_j0 (gfc_expr *x)
1387 gfc_expr *result;
1389 if (x->expr_type != EXPR_CONSTANT)
1390 return NULL;
1392 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1393 mpfr_j0 (result->value.real, x->value.real, GFC_RND_MODE);
1395 return range_check (result, "BESSEL_J0");
1399 gfc_expr *
1400 gfc_simplify_bessel_j1 (gfc_expr *x)
1402 gfc_expr *result;
1404 if (x->expr_type != EXPR_CONSTANT)
1405 return NULL;
1407 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1408 mpfr_j1 (result->value.real, x->value.real, GFC_RND_MODE);
1410 return range_check (result, "BESSEL_J1");
1414 gfc_expr *
1415 gfc_simplify_bessel_jn (gfc_expr *order, gfc_expr *x)
1417 gfc_expr *result;
1418 long n;
1420 if (x->expr_type != EXPR_CONSTANT || order->expr_type != EXPR_CONSTANT)
1421 return NULL;
1423 n = mpz_get_si (order->value.integer);
1424 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1425 mpfr_jn (result->value.real, n, x->value.real, GFC_RND_MODE);
1427 return range_check (result, "BESSEL_JN");
1431 /* Simplify transformational form of JN and YN. */
1433 static gfc_expr *
1434 gfc_simplify_bessel_n2 (gfc_expr *order1, gfc_expr *order2, gfc_expr *x,
1435 bool jn)
1437 gfc_expr *result;
1438 gfc_expr *e;
1439 long n1, n2;
1440 int i;
1441 mpfr_t x2rev, last1, last2;
1443 if (x->expr_type != EXPR_CONSTANT || order1->expr_type != EXPR_CONSTANT
1444 || order2->expr_type != EXPR_CONSTANT)
1445 return NULL;
1447 n1 = mpz_get_si (order1->value.integer);
1448 n2 = mpz_get_si (order2->value.integer);
1449 result = gfc_get_array_expr (x->ts.type, x->ts.kind, &x->where);
1450 result->rank = 1;
1451 result->shape = gfc_get_shape (1);
1452 mpz_init_set_ui (result->shape[0], MAX (n2-n1+1, 0));
1454 if (n2 < n1)
1455 return result;
1457 /* Special case: x == 0; it is J0(0.0) == 1, JN(N > 0, 0.0) == 0; and
1458 YN(N, 0.0) = -Inf. */
1460 if (mpfr_cmp_ui (x->value.real, 0.0) == 0)
1462 if (!jn && flag_range_check)
1464 gfc_error ("Result of BESSEL_YN is -INF at %L", &result->where);
1465 gfc_free_expr (result);
1466 return &gfc_bad_expr;
1469 if (jn && n1 == 0)
1471 e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1472 mpfr_set_ui (e->value.real, 1, GFC_RND_MODE);
1473 gfc_constructor_append_expr (&result->value.constructor, e,
1474 &x->where);
1475 n1++;
1478 for (i = n1; i <= n2; i++)
1480 e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1481 if (jn)
1482 mpfr_set_ui (e->value.real, 0, GFC_RND_MODE);
1483 else
1484 mpfr_set_inf (e->value.real, -1);
1485 gfc_constructor_append_expr (&result->value.constructor, e,
1486 &x->where);
1489 return result;
1492 /* Use the faster but more verbose recurrence algorithm. Bessel functions
1493 are stable for downward recursion and Neumann functions are stable
1494 for upward recursion. It is
1495 x2rev = 2.0/x,
1496 J(N-1, x) = x2rev * N * J(N, x) - J(N+1, x),
1497 Y(N+1, x) = x2rev * N * Y(N, x) - Y(N-1, x).
1498 Cf. http://dlmf.nist.gov/10.74#iv and http://dlmf.nist.gov/10.6#E1 */
1500 gfc_set_model_kind (x->ts.kind);
1502 /* Get first recursion anchor. */
1504 mpfr_init (last1);
1505 if (jn)
1506 mpfr_jn (last1, n2, x->value.real, GFC_RND_MODE);
1507 else
1508 mpfr_yn (last1, n1, x->value.real, GFC_RND_MODE);
1510 e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1511 mpfr_set (e->value.real, last1, GFC_RND_MODE);
1512 if (range_check (e, jn ? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr)
1514 mpfr_clear (last1);
1515 gfc_free_expr (e);
1516 gfc_free_expr (result);
1517 return &gfc_bad_expr;
1519 gfc_constructor_append_expr (&result->value.constructor, e, &x->where);
1521 if (n1 == n2)
1523 mpfr_clear (last1);
1524 return result;
1527 /* Get second recursion anchor. */
1529 mpfr_init (last2);
1530 if (jn)
1531 mpfr_jn (last2, n2-1, x->value.real, GFC_RND_MODE);
1532 else
1533 mpfr_yn (last2, n1+1, x->value.real, GFC_RND_MODE);
1535 e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1536 mpfr_set (e->value.real, last2, GFC_RND_MODE);
1537 if (range_check (e, jn ? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr)
1539 mpfr_clear (last1);
1540 mpfr_clear (last2);
1541 gfc_free_expr (e);
1542 gfc_free_expr (result);
1543 return &gfc_bad_expr;
1545 if (jn)
1546 gfc_constructor_insert_expr (&result->value.constructor, e, &x->where, -2);
1547 else
1548 gfc_constructor_append_expr (&result->value.constructor, e, &x->where);
1550 if (n1 + 1 == n2)
1552 mpfr_clear (last1);
1553 mpfr_clear (last2);
1554 return result;
1557 /* Start actual recursion. */
1559 mpfr_init (x2rev);
1560 mpfr_ui_div (x2rev, 2, x->value.real, GFC_RND_MODE);
1562 for (i = 2; i <= n2-n1; i++)
1564 e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1566 /* Special case: For YN, if the previous N gave -INF, set
1567 also N+1 to -INF. */
1568 if (!jn && !flag_range_check && mpfr_inf_p (last2))
1570 mpfr_set_inf (e->value.real, -1);
1571 gfc_constructor_append_expr (&result->value.constructor, e,
1572 &x->where);
1573 continue;
1576 mpfr_mul_si (e->value.real, x2rev, jn ? (n2-i+1) : (n1+i-1),
1577 GFC_RND_MODE);
1578 mpfr_mul (e->value.real, e->value.real, last2, GFC_RND_MODE);
1579 mpfr_sub (e->value.real, e->value.real, last1, GFC_RND_MODE);
1581 if (range_check (e, jn ? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr)
1583 /* Range_check frees "e" in that case. */
1584 e = NULL;
1585 goto error;
1588 if (jn)
1589 gfc_constructor_insert_expr (&result->value.constructor, e, &x->where,
1590 -i-1);
1591 else
1592 gfc_constructor_append_expr (&result->value.constructor, e, &x->where);
1594 mpfr_set (last1, last2, GFC_RND_MODE);
1595 mpfr_set (last2, e->value.real, GFC_RND_MODE);
1598 mpfr_clear (last1);
1599 mpfr_clear (last2);
1600 mpfr_clear (x2rev);
1601 return result;
1603 error:
1604 mpfr_clear (last1);
1605 mpfr_clear (last2);
1606 mpfr_clear (x2rev);
1607 gfc_free_expr (e);
1608 gfc_free_expr (result);
1609 return &gfc_bad_expr;
1613 gfc_expr *
1614 gfc_simplify_bessel_jn2 (gfc_expr *order1, gfc_expr *order2, gfc_expr *x)
1616 return gfc_simplify_bessel_n2 (order1, order2, x, true);
1620 gfc_expr *
1621 gfc_simplify_bessel_y0 (gfc_expr *x)
1623 gfc_expr *result;
1625 if (x->expr_type != EXPR_CONSTANT)
1626 return NULL;
1628 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1629 mpfr_y0 (result->value.real, x->value.real, GFC_RND_MODE);
1631 return range_check (result, "BESSEL_Y0");
1635 gfc_expr *
1636 gfc_simplify_bessel_y1 (gfc_expr *x)
1638 gfc_expr *result;
1640 if (x->expr_type != EXPR_CONSTANT)
1641 return NULL;
1643 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1644 mpfr_y1 (result->value.real, x->value.real, GFC_RND_MODE);
1646 return range_check (result, "BESSEL_Y1");
1650 gfc_expr *
1651 gfc_simplify_bessel_yn (gfc_expr *order, gfc_expr *x)
1653 gfc_expr *result;
1654 long n;
1656 if (x->expr_type != EXPR_CONSTANT || order->expr_type != EXPR_CONSTANT)
1657 return NULL;
1659 n = mpz_get_si (order->value.integer);
1660 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1661 mpfr_yn (result->value.real, n, x->value.real, GFC_RND_MODE);
1663 return range_check (result, "BESSEL_YN");
1667 gfc_expr *
1668 gfc_simplify_bessel_yn2 (gfc_expr *order1, gfc_expr *order2, gfc_expr *x)
1670 return gfc_simplify_bessel_n2 (order1, order2, x, false);
1674 gfc_expr *
1675 gfc_simplify_bit_size (gfc_expr *e)
1677 int i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
1678 int bit_size;
1680 if (flag_unsigned && e->ts.type == BT_UNSIGNED)
1681 bit_size = gfc_unsigned_kinds[i].bit_size;
1682 else
1683 bit_size = gfc_integer_kinds[i].bit_size;
1685 return gfc_get_int_expr (e->ts.kind, &e->where, bit_size);
1689 gfc_expr *
1690 gfc_simplify_btest (gfc_expr *e, gfc_expr *bit)
1692 int b;
1694 if (e->expr_type != EXPR_CONSTANT || bit->expr_type != EXPR_CONSTANT)
1695 return NULL;
1697 if (!gfc_check_bitfcn (e, bit))
1698 return &gfc_bad_expr;
1700 if (gfc_extract_int (bit, &b) || b < 0)
1701 return gfc_get_logical_expr (gfc_default_logical_kind, &e->where, false);
1703 return gfc_get_logical_expr (gfc_default_logical_kind, &e->where,
1704 mpz_tstbit (e->value.integer, b));
1708 static int
1709 compare_bitwise (gfc_expr *i, gfc_expr *j)
1711 mpz_t x, y;
1712 int k, res;
1714 gcc_assert (i->ts.type == BT_INTEGER);
1715 gcc_assert (j->ts.type == BT_INTEGER);
1717 mpz_init_set (x, i->value.integer);
1718 k = gfc_validate_kind (i->ts.type, i->ts.kind, false);
1719 gfc_convert_mpz_to_unsigned (x, gfc_integer_kinds[k].bit_size);
1721 mpz_init_set (y, j->value.integer);
1722 k = gfc_validate_kind (j->ts.type, j->ts.kind, false);
1723 gfc_convert_mpz_to_unsigned (y, gfc_integer_kinds[k].bit_size);
1725 res = mpz_cmp (x, y);
1726 mpz_clear (x);
1727 mpz_clear (y);
1728 return res;
1732 gfc_expr *
1733 gfc_simplify_bge (gfc_expr *i, gfc_expr *j)
1735 bool result;
1737 if (i->expr_type != EXPR_CONSTANT || j->expr_type != EXPR_CONSTANT)
1738 return NULL;
1740 if (flag_unsigned && i->ts.type == BT_UNSIGNED)
1741 result = mpz_cmp (i->value.integer, j->value.integer) >= 0;
1742 else
1743 result = compare_bitwise (i, j) >= 0;
1745 return gfc_get_logical_expr (gfc_default_logical_kind, &i->where,
1746 result);
1750 gfc_expr *
1751 gfc_simplify_bgt (gfc_expr *i, gfc_expr *j)
1753 bool result;
1755 if (i->expr_type != EXPR_CONSTANT || j->expr_type != EXPR_CONSTANT)
1756 return NULL;
1758 if (flag_unsigned && i->ts.type == BT_UNSIGNED)
1759 result = mpz_cmp (i->value.integer, j->value.integer) > 0;
1760 else
1761 result = compare_bitwise (i, j) > 0;
1763 return gfc_get_logical_expr (gfc_default_logical_kind, &i->where,
1764 result);
1768 gfc_expr *
1769 gfc_simplify_ble (gfc_expr *i, gfc_expr *j)
1771 bool result;
1773 if (i->expr_type != EXPR_CONSTANT || j->expr_type != EXPR_CONSTANT)
1774 return NULL;
1776 if (flag_unsigned && i->ts.type == BT_UNSIGNED)
1777 result = mpz_cmp (i->value.integer, j->value.integer) <= 0;
1778 else
1779 result = compare_bitwise (i, j) <= 0;
1781 return gfc_get_logical_expr (gfc_default_logical_kind, &i->where,
1782 result);
1786 gfc_expr *
1787 gfc_simplify_blt (gfc_expr *i, gfc_expr *j)
1789 bool result;
1791 if (i->expr_type != EXPR_CONSTANT || j->expr_type != EXPR_CONSTANT)
1792 return NULL;
1794 if (flag_unsigned && i->ts.type == BT_UNSIGNED)
1795 result = mpz_cmp (i->value.integer, j->value.integer) < 0;
1796 else
1797 result = compare_bitwise (i, j) < 0;
1799 return gfc_get_logical_expr (gfc_default_logical_kind, &i->where,
1800 result);
1803 gfc_expr *
1804 gfc_simplify_ceiling (gfc_expr *e, gfc_expr *k)
1806 gfc_expr *ceil, *result;
1807 int kind;
1809 kind = get_kind (BT_INTEGER, k, "CEILING", gfc_default_integer_kind);
1810 if (kind == -1)
1811 return &gfc_bad_expr;
1813 if (e->expr_type != EXPR_CONSTANT)
1814 return NULL;
1816 ceil = gfc_copy_expr (e);
1817 mpfr_ceil (ceil->value.real, e->value.real);
1819 result = gfc_get_constant_expr (BT_INTEGER, kind, &e->where);
1820 gfc_mpfr_to_mpz (result->value.integer, ceil->value.real, &e->where);
1822 gfc_free_expr (ceil);
1824 return range_check (result, "CEILING");
1828 gfc_expr *
1829 gfc_simplify_char (gfc_expr *e, gfc_expr *k)
1831 return simplify_achar_char (e, k, "CHAR", false);
1835 /* Common subroutine for simplifying CMPLX, COMPLEX and DCMPLX. */
1837 static gfc_expr *
1838 simplify_cmplx (const char *name, gfc_expr *x, gfc_expr *y, int kind)
1840 gfc_expr *result;
1842 if (x->expr_type != EXPR_CONSTANT
1843 || (y != NULL && y->expr_type != EXPR_CONSTANT))
1844 return NULL;
1846 result = gfc_get_constant_expr (BT_COMPLEX, kind, &x->where);
1848 switch (x->ts.type)
1850 case BT_INTEGER:
1851 case BT_UNSIGNED:
1852 mpc_set_z (result->value.complex, x->value.integer, GFC_MPC_RND_MODE);
1853 break;
1855 case BT_REAL:
1856 mpc_set_fr (result->value.complex, x->value.real, GFC_RND_MODE);
1857 break;
1859 case BT_COMPLEX:
1860 mpc_set (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1861 break;
1863 default:
1864 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (x)");
1867 if (!y)
1868 return range_check (result, name);
1870 switch (y->ts.type)
1872 case BT_INTEGER:
1873 case BT_UNSIGNED:
1874 mpfr_set_z (mpc_imagref (result->value.complex),
1875 y->value.integer, GFC_RND_MODE);
1876 break;
1878 case BT_REAL:
1879 mpfr_set (mpc_imagref (result->value.complex),
1880 y->value.real, GFC_RND_MODE);
1881 break;
1883 default:
1884 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (y)");
1887 return range_check (result, name);
1891 gfc_expr *
1892 gfc_simplify_cmplx (gfc_expr *x, gfc_expr *y, gfc_expr *k)
1894 int kind;
1896 kind = get_kind (BT_REAL, k, "CMPLX", gfc_default_complex_kind);
1897 if (kind == -1)
1898 return &gfc_bad_expr;
1900 return simplify_cmplx ("CMPLX", x, y, kind);
1904 gfc_expr *
1905 gfc_simplify_complex (gfc_expr *x, gfc_expr *y)
1907 int kind;
1909 if (x->ts.type == BT_INTEGER && y->ts.type == BT_INTEGER)
1910 kind = gfc_default_complex_kind;
1911 else if (x->ts.type == BT_REAL || y->ts.type == BT_INTEGER)
1912 kind = x->ts.kind;
1913 else if (x->ts.type == BT_INTEGER || y->ts.type == BT_REAL)
1914 kind = y->ts.kind;
1915 else if (x->ts.type == BT_REAL && y->ts.type == BT_REAL)
1916 kind = (x->ts.kind > y->ts.kind) ? x->ts.kind : y->ts.kind;
1917 else
1918 gcc_unreachable ();
1920 return simplify_cmplx ("COMPLEX", x, y, kind);
1924 gfc_expr *
1925 gfc_simplify_conjg (gfc_expr *e)
1927 gfc_expr *result;
1929 if (e->expr_type != EXPR_CONSTANT)
1930 return NULL;
1932 result = gfc_copy_expr (e);
1933 mpc_conj (result->value.complex, result->value.complex, GFC_MPC_RND_MODE);
1935 return range_check (result, "CONJG");
1939 /* Simplify atan2d (x) where the unit is degree. */
1941 gfc_expr *
1942 gfc_simplify_atan2d (gfc_expr *y, gfc_expr *x)
1944 gfc_expr *result;
1946 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1947 return NULL;
1949 if (mpfr_zero_p (y->value.real) && mpfr_zero_p (x->value.real))
1951 gfc_error ("If first argument of ATAN2D at %L is zero, then the "
1952 "second argument must not be zero", &y->where);
1953 return &gfc_bad_expr;
1956 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1957 mpfr_atan2 (result->value.real, y->value.real, x->value.real, GFC_RND_MODE);
1958 rad2deg (result->value.real);
1960 return range_check (result, "ATAN2D");
1964 gfc_expr *
1965 gfc_simplify_cos (gfc_expr *x)
1967 gfc_expr *result;
1969 if (x->expr_type != EXPR_CONSTANT)
1970 return NULL;
1972 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1974 switch (x->ts.type)
1976 case BT_REAL:
1977 mpfr_cos (result->value.real, x->value.real, GFC_RND_MODE);
1978 break;
1980 case BT_COMPLEX:
1981 gfc_set_model_kind (x->ts.kind);
1982 mpc_cos (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1983 break;
1985 default:
1986 gfc_internal_error ("in gfc_simplify_cos(): Bad type");
1989 return range_check (result, "COS");
1993 static void
1994 deg2rad (mpfr_t x)
1996 mpfr_t d2r;
1998 mpfr_init (d2r);
1999 mpfr_const_pi (d2r, GFC_RND_MODE);
2000 mpfr_div_ui (d2r, d2r, 180, GFC_RND_MODE);
2001 mpfr_mul (x, x, d2r, GFC_RND_MODE);
2002 mpfr_clear (d2r);
2006 /* Simplification routines for SIND, COSD, TAND. */
2007 #include "trigd_fe.inc"
2010 /* Simplify COSD(X) where X has the unit of degree. */
2012 gfc_expr *
2013 gfc_simplify_cosd (gfc_expr *x)
2015 gfc_expr *result;
2017 if (x->expr_type != EXPR_CONSTANT)
2018 return NULL;
2020 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2021 mpfr_set (result->value.real, x->value.real, GFC_RND_MODE);
2022 simplify_cosd (result->value.real);
2024 return range_check (result, "COSD");
2028 /* Simplify SIND(X) where X has the unit of degree. */
2030 gfc_expr *
2031 gfc_simplify_sind (gfc_expr *x)
2033 gfc_expr *result;
2035 if (x->expr_type != EXPR_CONSTANT)
2036 return NULL;
2038 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2039 mpfr_set (result->value.real, x->value.real, GFC_RND_MODE);
2040 simplify_sind (result->value.real);
2042 return range_check (result, "SIND");
2046 /* Simplify TAND(X) where X has the unit of degree. */
2048 gfc_expr *
2049 gfc_simplify_tand (gfc_expr *x)
2051 gfc_expr *result;
2053 if (x->expr_type != EXPR_CONSTANT)
2054 return NULL;
2056 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2057 mpfr_set (result->value.real, x->value.real, GFC_RND_MODE);
2058 simplify_tand (result->value.real);
2060 return range_check (result, "TAND");
2064 /* Simplify COTAND(X) where X has the unit of degree. */
2066 gfc_expr *
2067 gfc_simplify_cotand (gfc_expr *x)
2069 gfc_expr *result;
2071 if (x->expr_type != EXPR_CONSTANT)
2072 return NULL;
2074 /* Implement COTAND = -TAND(x+90).
2075 TAND offers correct exact values for multiples of 30 degrees.
2076 This implementation is also compatible with the behavior of some legacy
2077 compilers. Keep this consistent with gfc_conv_intrinsic_cotand. */
2078 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2079 mpfr_set (result->value.real, x->value.real, GFC_RND_MODE);
2080 mpfr_add_ui (result->value.real, result->value.real, 90, GFC_RND_MODE);
2081 simplify_tand (result->value.real);
2082 mpfr_neg (result->value.real, result->value.real, GFC_RND_MODE);
2084 return range_check (result, "COTAND");
2088 gfc_expr *
2089 gfc_simplify_cosh (gfc_expr *x)
2091 gfc_expr *result;
2093 if (x->expr_type != EXPR_CONSTANT)
2094 return NULL;
2096 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2098 switch (x->ts.type)
2100 case BT_REAL:
2101 mpfr_cosh (result->value.real, x->value.real, GFC_RND_MODE);
2102 break;
2104 case BT_COMPLEX:
2105 mpc_cosh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
2106 break;
2108 default:
2109 gcc_unreachable ();
2112 return range_check (result, "COSH");
2116 gfc_expr *
2117 gfc_simplify_count (gfc_expr *mask, gfc_expr *dim, gfc_expr *kind)
2119 gfc_expr *result;
2120 bool size_zero;
2122 size_zero = gfc_is_size_zero_array (mask);
2124 if (!(is_constant_array_expr (mask) || size_zero)
2125 || !gfc_is_constant_expr (dim)
2126 || !gfc_is_constant_expr (kind))
2127 return NULL;
2129 result = transformational_result (mask, dim,
2130 BT_INTEGER,
2131 get_kind (BT_INTEGER, kind, "COUNT",
2132 gfc_default_integer_kind),
2133 &mask->where);
2135 init_result_expr (result, 0, NULL);
2137 if (size_zero)
2138 return result;
2140 /* Passing MASK twice, once as data array, once as mask.
2141 Whenever gfc_count is called, '1' is added to the result. */
2142 return !dim || mask->rank == 1 ?
2143 simplify_transformation_to_scalar (result, mask, mask, gfc_count) :
2144 simplify_transformation_to_array (result, mask, dim, mask, gfc_count, NULL);
2147 /* Simplification routine for cshift. This works by copying the array
2148 expressions into a one-dimensional array, shuffling the values into another
2149 one-dimensional array and creating the new array expression from this. The
2150 shuffling part is basically taken from the library routine. */
2152 gfc_expr *
2153 gfc_simplify_cshift (gfc_expr *array, gfc_expr *shift, gfc_expr *dim)
2155 gfc_expr *result;
2156 int which;
2157 gfc_expr **arrayvec, **resultvec;
2158 gfc_expr **rptr, **sptr;
2159 mpz_t size;
2160 size_t arraysize, shiftsize, i;
2161 gfc_constructor *array_ctor, *shift_ctor;
2162 ssize_t *shiftvec, *hptr;
2163 ssize_t shift_val, len;
2164 ssize_t count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS],
2165 hs_ex[GFC_MAX_DIMENSIONS + 1],
2166 hstride[GFC_MAX_DIMENSIONS], sstride[GFC_MAX_DIMENSIONS],
2167 a_extent[GFC_MAX_DIMENSIONS], a_stride[GFC_MAX_DIMENSIONS],
2168 h_extent[GFC_MAX_DIMENSIONS],
2169 ss_ex[GFC_MAX_DIMENSIONS + 1];
2170 ssize_t rsoffset;
2171 int d, n;
2172 bool continue_loop;
2173 gfc_expr **src, **dest;
2175 if (!is_constant_array_expr (array))
2176 return NULL;
2178 if (shift->rank > 0)
2179 gfc_simplify_expr (shift, 1);
2181 if (!gfc_is_constant_expr (shift))
2182 return NULL;
2184 /* Make dim zero-based. */
2185 if (dim)
2187 if (!gfc_is_constant_expr (dim))
2188 return NULL;
2189 which = mpz_get_si (dim->value.integer) - 1;
2191 else
2192 which = 0;
2194 if (array->shape == NULL)
2195 return NULL;
2197 gfc_array_size (array, &size);
2198 arraysize = mpz_get_ui (size);
2199 mpz_clear (size);
2201 result = gfc_get_array_expr (array->ts.type, array->ts.kind, &array->where);
2202 result->shape = gfc_copy_shape (array->shape, array->rank);
2203 result->rank = array->rank;
2204 result->ts.u.derived = array->ts.u.derived;
2206 if (arraysize == 0)
2207 return result;
2209 arrayvec = XCNEWVEC (gfc_expr *, arraysize);
2210 array_ctor = gfc_constructor_first (array->value.constructor);
2211 for (i = 0; i < arraysize; i++)
2213 arrayvec[i] = array_ctor->expr;
2214 array_ctor = gfc_constructor_next (array_ctor);
2217 resultvec = XCNEWVEC (gfc_expr *, arraysize);
2219 sstride[0] = 0;
2220 extent[0] = 1;
2221 count[0] = 0;
2223 for (d=0; d < array->rank; d++)
2225 a_extent[d] = mpz_get_si (array->shape[d]);
2226 a_stride[d] = d == 0 ? 1 : a_stride[d-1] * a_extent[d-1];
2229 if (shift->rank > 0)
2231 gfc_array_size (shift, &size);
2232 shiftsize = mpz_get_ui (size);
2233 mpz_clear (size);
2234 shiftvec = XCNEWVEC (ssize_t, shiftsize);
2235 shift_ctor = gfc_constructor_first (shift->value.constructor);
2236 for (d = 0; d < shift->rank; d++)
2238 h_extent[d] = mpz_get_si (shift->shape[d]);
2239 hstride[d] = d == 0 ? 1 : hstride[d-1] * h_extent[d-1];
2242 else
2243 shiftvec = NULL;
2245 /* Shut up compiler */
2246 len = 1;
2247 rsoffset = 1;
2249 n = 0;
2250 for (d=0; d < array->rank; d++)
2252 if (d == which)
2254 rsoffset = a_stride[d];
2255 len = a_extent[d];
2257 else
2259 count[n] = 0;
2260 extent[n] = a_extent[d];
2261 sstride[n] = a_stride[d];
2262 ss_ex[n] = sstride[n] * extent[n];
2263 if (shiftvec)
2264 hs_ex[n] = hstride[n] * extent[n];
2265 n++;
2268 ss_ex[n] = 0;
2269 hs_ex[n] = 0;
2271 if (shiftvec)
2273 for (i = 0; i < shiftsize; i++)
2275 ssize_t val;
2276 val = mpz_get_si (shift_ctor->expr->value.integer);
2277 val = val % len;
2278 if (val < 0)
2279 val += len;
2280 shiftvec[i] = val;
2281 shift_ctor = gfc_constructor_next (shift_ctor);
2283 shift_val = 0;
2285 else
2287 shift_val = mpz_get_si (shift->value.integer);
2288 shift_val = shift_val % len;
2289 if (shift_val < 0)
2290 shift_val += len;
2293 continue_loop = true;
2294 d = array->rank;
2295 rptr = resultvec;
2296 sptr = arrayvec;
2297 hptr = shiftvec;
2299 while (continue_loop)
2301 ssize_t sh;
2302 if (shiftvec)
2303 sh = *hptr;
2304 else
2305 sh = shift_val;
2307 src = &sptr[sh * rsoffset];
2308 dest = rptr;
2309 for (n = 0; n < len - sh; n++)
2311 *dest = *src;
2312 dest += rsoffset;
2313 src += rsoffset;
2315 src = sptr;
2316 for ( n = 0; n < sh; n++)
2318 *dest = *src;
2319 dest += rsoffset;
2320 src += rsoffset;
2322 rptr += sstride[0];
2323 sptr += sstride[0];
2324 if (shiftvec)
2325 hptr += hstride[0];
2326 count[0]++;
2327 n = 0;
2328 while (count[n] == extent[n])
2330 count[n] = 0;
2331 rptr -= ss_ex[n];
2332 sptr -= ss_ex[n];
2333 if (shiftvec)
2334 hptr -= hs_ex[n];
2335 n++;
2336 if (n >= d - 1)
2338 continue_loop = false;
2339 break;
2341 else
2343 count[n]++;
2344 rptr += sstride[n];
2345 sptr += sstride[n];
2346 if (shiftvec)
2347 hptr += hstride[n];
2352 for (i = 0; i < arraysize; i++)
2354 gfc_constructor_append_expr (&result->value.constructor,
2355 gfc_copy_expr (resultvec[i]),
2356 NULL);
2358 return result;
2362 gfc_expr *
2363 gfc_simplify_dcmplx (gfc_expr *x, gfc_expr *y)
2365 return simplify_cmplx ("DCMPLX", x, y, gfc_default_double_kind);
2369 gfc_expr *
2370 gfc_simplify_dble (gfc_expr *e)
2372 gfc_expr *result = NULL;
2373 int tmp1, tmp2;
2375 if (e->expr_type != EXPR_CONSTANT)
2376 return NULL;
2378 /* For explicit conversion, turn off -Wconversion and -Wconversion-extra
2379 warnings. */
2380 tmp1 = warn_conversion;
2381 tmp2 = warn_conversion_extra;
2382 warn_conversion = warn_conversion_extra = 0;
2384 result = gfc_convert_constant (e, BT_REAL, gfc_default_double_kind);
2386 warn_conversion = tmp1;
2387 warn_conversion_extra = tmp2;
2389 if (result == &gfc_bad_expr)
2390 return &gfc_bad_expr;
2392 return range_check (result, "DBLE");
2396 gfc_expr *
2397 gfc_simplify_digits (gfc_expr *x)
2399 int i, digits;
2401 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
2403 switch (x->ts.type)
2405 case BT_INTEGER:
2406 digits = gfc_integer_kinds[i].digits;
2407 break;
2409 case BT_UNSIGNED:
2410 digits = gfc_unsigned_kinds[i].digits;
2411 break;
2413 case BT_REAL:
2414 case BT_COMPLEX:
2415 digits = gfc_real_kinds[i].digits;
2416 break;
2418 default:
2419 gcc_unreachable ();
2422 return gfc_get_int_expr (gfc_default_integer_kind, NULL, digits);
2426 gfc_expr *
2427 gfc_simplify_dim (gfc_expr *x, gfc_expr *y)
2429 gfc_expr *result;
2430 int kind;
2432 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2433 return NULL;
2435 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
2436 result = gfc_get_constant_expr (x->ts.type, kind, &x->where);
2438 switch (x->ts.type)
2440 case BT_INTEGER:
2441 if (mpz_cmp (x->value.integer, y->value.integer) > 0)
2442 mpz_sub (result->value.integer, x->value.integer, y->value.integer);
2443 else
2444 mpz_set_ui (result->value.integer, 0);
2446 break;
2448 case BT_REAL:
2449 if (mpfr_cmp (x->value.real, y->value.real) > 0)
2450 mpfr_sub (result->value.real, x->value.real, y->value.real,
2451 GFC_RND_MODE);
2452 else
2453 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
2455 break;
2457 default:
2458 gfc_internal_error ("gfc_simplify_dim(): Bad type");
2461 return range_check (result, "DIM");
2465 gfc_expr*
2466 gfc_simplify_dot_product (gfc_expr *vector_a, gfc_expr *vector_b)
2468 /* If vector_a is a zero-sized array, the result is 0 for INTEGER,
2469 REAL, and COMPLEX types and .false. for LOGICAL. */
2470 if (vector_a->shape && mpz_get_si (vector_a->shape[0]) == 0)
2472 if (vector_a->ts.type == BT_LOGICAL)
2473 return gfc_get_logical_expr (gfc_default_logical_kind, NULL, false);
2474 else
2475 return gfc_get_int_expr (gfc_default_integer_kind, NULL, 0);
2478 if (!is_constant_array_expr (vector_a)
2479 || !is_constant_array_expr (vector_b))
2480 return NULL;
2482 return compute_dot_product (vector_a, 1, 0, vector_b, 1, 0, true);
2486 gfc_expr *
2487 gfc_simplify_dprod (gfc_expr *x, gfc_expr *y)
2489 gfc_expr *a1, *a2, *result;
2491 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2492 return NULL;
2494 a1 = gfc_real2real (x, gfc_default_double_kind);
2495 a2 = gfc_real2real (y, gfc_default_double_kind);
2497 result = gfc_get_constant_expr (BT_REAL, gfc_default_double_kind, &x->where);
2498 mpfr_mul (result->value.real, a1->value.real, a2->value.real, GFC_RND_MODE);
2500 gfc_free_expr (a2);
2501 gfc_free_expr (a1);
2503 return range_check (result, "DPROD");
2507 static gfc_expr *
2508 simplify_dshift (gfc_expr *arg1, gfc_expr *arg2, gfc_expr *shiftarg,
2509 bool right)
2511 gfc_expr *result;
2512 int i, k, size, shift;
2513 bt type = BT_INTEGER;
2515 if (arg1->expr_type != EXPR_CONSTANT || arg2->expr_type != EXPR_CONSTANT
2516 || shiftarg->expr_type != EXPR_CONSTANT)
2517 return NULL;
2519 if (flag_unsigned && arg1->ts.type == BT_UNSIGNED)
2521 k = gfc_validate_kind (BT_UNSIGNED, arg1->ts.kind, false);
2522 size = gfc_unsigned_kinds[k].bit_size;
2523 type = BT_UNSIGNED;
2525 else
2527 k = gfc_validate_kind (BT_INTEGER, arg1->ts.kind, false);
2528 size = gfc_integer_kinds[k].bit_size;
2531 gfc_extract_int (shiftarg, &shift);
2533 /* DSHIFTR(I,J,SHIFT) = DSHIFTL(I,J,SIZE-SHIFT). */
2534 if (right)
2535 shift = size - shift;
2537 result = gfc_get_constant_expr (type, arg1->ts.kind, &arg1->where);
2538 mpz_set_ui (result->value.integer, 0);
2540 for (i = 0; i < shift; i++)
2541 if (mpz_tstbit (arg2->value.integer, size - shift + i))
2542 mpz_setbit (result->value.integer, i);
2544 for (i = 0; i < size - shift; i++)
2545 if (mpz_tstbit (arg1->value.integer, i))
2546 mpz_setbit (result->value.integer, shift + i);
2548 /* Convert to a signed value if needed. */
2549 if (type == BT_INTEGER)
2550 gfc_convert_mpz_to_signed (result->value.integer, size);
2551 else
2552 gfc_reduce_unsigned (result);
2554 return result;
2558 gfc_expr *
2559 gfc_simplify_dshiftr (gfc_expr *arg1, gfc_expr *arg2, gfc_expr *shiftarg)
2561 return simplify_dshift (arg1, arg2, shiftarg, true);
2565 gfc_expr *
2566 gfc_simplify_dshiftl (gfc_expr *arg1, gfc_expr *arg2, gfc_expr *shiftarg)
2568 return simplify_dshift (arg1, arg2, shiftarg, false);
2572 gfc_expr *
2573 gfc_simplify_eoshift (gfc_expr *array, gfc_expr *shift, gfc_expr *boundary,
2574 gfc_expr *dim)
2576 bool temp_boundary;
2577 gfc_expr *bnd;
2578 gfc_expr *result;
2579 int which;
2580 gfc_expr **arrayvec, **resultvec;
2581 gfc_expr **rptr, **sptr;
2582 mpz_t size;
2583 size_t arraysize, i;
2584 gfc_constructor *array_ctor, *shift_ctor, *bnd_ctor;
2585 ssize_t shift_val, len;
2586 ssize_t count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS],
2587 sstride[GFC_MAX_DIMENSIONS], a_extent[GFC_MAX_DIMENSIONS],
2588 a_stride[GFC_MAX_DIMENSIONS], ss_ex[GFC_MAX_DIMENSIONS + 1];
2589 ssize_t rsoffset;
2590 int d, n;
2591 bool continue_loop;
2592 gfc_expr **src, **dest;
2593 size_t s_len;
2595 if (!is_constant_array_expr (array))
2596 return NULL;
2598 if (shift->rank > 0)
2599 gfc_simplify_expr (shift, 1);
2601 if (!gfc_is_constant_expr (shift))
2602 return NULL;
2604 if (boundary)
2606 if (boundary->rank > 0)
2607 gfc_simplify_expr (boundary, 1);
2609 if (!gfc_is_constant_expr (boundary))
2610 return NULL;
2613 if (dim)
2615 if (!gfc_is_constant_expr (dim))
2616 return NULL;
2617 which = mpz_get_si (dim->value.integer) - 1;
2619 else
2620 which = 0;
2622 s_len = 0;
2623 if (boundary == NULL)
2625 temp_boundary = true;
2626 switch (array->ts.type)
2629 case BT_INTEGER:
2630 bnd = gfc_get_int_expr (array->ts.kind, NULL, 0);
2631 break;
2633 case BT_UNSIGNED:
2634 bnd = gfc_get_unsigned_expr (array->ts.kind, NULL, 0);
2635 break;
2637 case BT_LOGICAL:
2638 bnd = gfc_get_logical_expr (array->ts.kind, NULL, 0);
2639 break;
2641 case BT_REAL:
2642 bnd = gfc_get_constant_expr (array->ts.type, array->ts.kind, &gfc_current_locus);
2643 mpfr_set_ui (bnd->value.real, 0, GFC_RND_MODE);
2644 break;
2646 case BT_COMPLEX:
2647 bnd = gfc_get_constant_expr (array->ts.type, array->ts.kind, &gfc_current_locus);
2648 mpc_set_ui (bnd->value.complex, 0, GFC_RND_MODE);
2649 break;
2651 case BT_CHARACTER:
2652 s_len = mpz_get_ui (array->ts.u.cl->length->value.integer);
2653 bnd = gfc_get_character_expr (array->ts.kind, &gfc_current_locus, NULL, s_len);
2654 break;
2656 default:
2657 gcc_unreachable();
2661 else
2663 temp_boundary = false;
2664 bnd = boundary;
2667 gfc_array_size (array, &size);
2668 arraysize = mpz_get_ui (size);
2669 mpz_clear (size);
2671 result = gfc_get_array_expr (array->ts.type, array->ts.kind, &array->where);
2672 result->shape = gfc_copy_shape (array->shape, array->rank);
2673 result->rank = array->rank;
2674 result->ts = array->ts;
2676 if (arraysize == 0)
2677 goto final;
2679 if (array->shape == NULL)
2680 goto final;
2682 arrayvec = XCNEWVEC (gfc_expr *, arraysize);
2683 array_ctor = gfc_constructor_first (array->value.constructor);
2684 for (i = 0; i < arraysize; i++)
2686 arrayvec[i] = array_ctor->expr;
2687 array_ctor = gfc_constructor_next (array_ctor);
2690 resultvec = XCNEWVEC (gfc_expr *, arraysize);
2692 extent[0] = 1;
2693 count[0] = 0;
2695 for (d=0; d < array->rank; d++)
2697 a_extent[d] = mpz_get_si (array->shape[d]);
2698 a_stride[d] = d == 0 ? 1 : a_stride[d-1] * a_extent[d-1];
2701 if (shift->rank > 0)
2703 shift_ctor = gfc_constructor_first (shift->value.constructor);
2704 shift_val = 0;
2706 else
2708 shift_ctor = NULL;
2709 shift_val = mpz_get_si (shift->value.integer);
2712 if (bnd->rank > 0)
2713 bnd_ctor = gfc_constructor_first (bnd->value.constructor);
2714 else
2715 bnd_ctor = NULL;
2717 /* Shut up compiler */
2718 len = 1;
2719 rsoffset = 1;
2721 n = 0;
2722 for (d=0; d < array->rank; d++)
2724 if (d == which)
2726 rsoffset = a_stride[d];
2727 len = a_extent[d];
2729 else
2731 count[n] = 0;
2732 extent[n] = a_extent[d];
2733 sstride[n] = a_stride[d];
2734 ss_ex[n] = sstride[n] * extent[n];
2735 n++;
2738 ss_ex[n] = 0;
2740 continue_loop = true;
2741 d = array->rank;
2742 rptr = resultvec;
2743 sptr = arrayvec;
2745 while (continue_loop)
2747 ssize_t sh, delta;
2749 if (shift_ctor)
2750 sh = mpz_get_si (shift_ctor->expr->value.integer);
2751 else
2752 sh = shift_val;
2754 if (( sh >= 0 ? sh : -sh ) > len)
2756 delta = len;
2757 sh = len;
2759 else
2760 delta = (sh >= 0) ? sh: -sh;
2762 if (sh > 0)
2764 src = &sptr[delta * rsoffset];
2765 dest = rptr;
2767 else
2769 src = sptr;
2770 dest = &rptr[delta * rsoffset];
2773 for (n = 0; n < len - delta; n++)
2775 *dest = *src;
2776 dest += rsoffset;
2777 src += rsoffset;
2780 if (sh < 0)
2781 dest = rptr;
2783 n = delta;
2785 if (bnd_ctor)
2787 while (n--)
2789 *dest = gfc_copy_expr (bnd_ctor->expr);
2790 dest += rsoffset;
2793 else
2795 while (n--)
2797 *dest = gfc_copy_expr (bnd);
2798 dest += rsoffset;
2801 rptr += sstride[0];
2802 sptr += sstride[0];
2803 if (shift_ctor)
2804 shift_ctor = gfc_constructor_next (shift_ctor);
2806 if (bnd_ctor)
2807 bnd_ctor = gfc_constructor_next (bnd_ctor);
2809 count[0]++;
2810 n = 0;
2811 while (count[n] == extent[n])
2813 count[n] = 0;
2814 rptr -= ss_ex[n];
2815 sptr -= ss_ex[n];
2816 n++;
2817 if (n >= d - 1)
2819 continue_loop = false;
2820 break;
2822 else
2824 count[n]++;
2825 rptr += sstride[n];
2826 sptr += sstride[n];
2831 for (i = 0; i < arraysize; i++)
2833 gfc_constructor_append_expr (&result->value.constructor,
2834 gfc_copy_expr (resultvec[i]),
2835 NULL);
2838 final:
2839 if (temp_boundary)
2840 gfc_free_expr (bnd);
2842 return result;
2845 gfc_expr *
2846 gfc_simplify_erf (gfc_expr *x)
2848 gfc_expr *result;
2850 if (x->expr_type != EXPR_CONSTANT)
2851 return NULL;
2853 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2854 mpfr_erf (result->value.real, x->value.real, GFC_RND_MODE);
2856 return range_check (result, "ERF");
2860 gfc_expr *
2861 gfc_simplify_erfc (gfc_expr *x)
2863 gfc_expr *result;
2865 if (x->expr_type != EXPR_CONSTANT)
2866 return NULL;
2868 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2869 mpfr_erfc (result->value.real, x->value.real, GFC_RND_MODE);
2871 return range_check (result, "ERFC");
2875 /* Helper functions to simplify ERFC_SCALED(x) = ERFC(x) * EXP(X**2). */
2877 #define MAX_ITER 200
2878 #define ARG_LIMIT 12
2880 /* Calculate ERFC_SCALED directly by its definition:
2882 ERFC_SCALED(x) = ERFC(x) * EXP(X**2)
2884 using a large precision for intermediate results. This is used for all
2885 but large values of the argument. */
2886 static void
2887 fullprec_erfc_scaled (mpfr_t res, mpfr_t arg)
2889 mpfr_prec_t prec;
2890 mpfr_t a, b;
2892 prec = mpfr_get_default_prec ();
2893 mpfr_set_default_prec (10 * prec);
2895 mpfr_init (a);
2896 mpfr_init (b);
2898 mpfr_set (a, arg, GFC_RND_MODE);
2899 mpfr_sqr (b, a, GFC_RND_MODE);
2900 mpfr_exp (b, b, GFC_RND_MODE);
2901 mpfr_erfc (a, a, GFC_RND_MODE);
2902 mpfr_mul (a, a, b, GFC_RND_MODE);
2904 mpfr_set (res, a, GFC_RND_MODE);
2905 mpfr_set_default_prec (prec);
2907 mpfr_clear (a);
2908 mpfr_clear (b);
2911 /* Calculate ERFC_SCALED using a power series expansion in 1/arg:
2913 ERFC_SCALED(x) = 1 / (x * sqrt(pi))
2914 * (1 + Sum_n (-1)**n * (1 * 3 * 5 * ... * (2n-1))
2915 / (2 * x**2)**n)
2917 This is used for large values of the argument. Intermediate calculations
2918 are performed with twice the precision. We don't do a fixed number of
2919 iterations of the sum, but stop when it has converged to the required
2920 precision. */
2921 static void
2922 asympt_erfc_scaled (mpfr_t res, mpfr_t arg)
2924 mpfr_t sum, x, u, v, w, oldsum, sumtrunc;
2925 mpz_t num;
2926 mpfr_prec_t prec;
2927 unsigned i;
2929 prec = mpfr_get_default_prec ();
2930 mpfr_set_default_prec (2 * prec);
2932 mpfr_init (sum);
2933 mpfr_init (x);
2934 mpfr_init (u);
2935 mpfr_init (v);
2936 mpfr_init (w);
2937 mpz_init (num);
2939 mpfr_init (oldsum);
2940 mpfr_init (sumtrunc);
2941 mpfr_set_prec (oldsum, prec);
2942 mpfr_set_prec (sumtrunc, prec);
2944 mpfr_set (x, arg, GFC_RND_MODE);
2945 mpfr_set_ui (sum, 1, GFC_RND_MODE);
2946 mpz_set_ui (num, 1);
2948 mpfr_set (u, x, GFC_RND_MODE);
2949 mpfr_sqr (u, u, GFC_RND_MODE);
2950 mpfr_mul_ui (u, u, 2, GFC_RND_MODE);
2951 mpfr_pow_si (u, u, -1, GFC_RND_MODE);
2953 for (i = 1; i < MAX_ITER; i++)
2955 mpfr_set (oldsum, sum, GFC_RND_MODE);
2957 mpz_mul_ui (num, num, 2 * i - 1);
2958 mpz_neg (num, num);
2960 mpfr_set (w, u, GFC_RND_MODE);
2961 mpfr_pow_ui (w, w, i, GFC_RND_MODE);
2963 mpfr_set_z (v, num, GFC_RND_MODE);
2964 mpfr_mul (v, v, w, GFC_RND_MODE);
2966 mpfr_add (sum, sum, v, GFC_RND_MODE);
2968 mpfr_set (sumtrunc, sum, GFC_RND_MODE);
2969 if (mpfr_cmp (sumtrunc, oldsum) == 0)
2970 break;
2973 /* We should have converged by now; otherwise, ARG_LIMIT is probably
2974 set too low. */
2975 gcc_assert (i < MAX_ITER);
2977 /* Divide by x * sqrt(Pi). */
2978 mpfr_const_pi (u, GFC_RND_MODE);
2979 mpfr_sqrt (u, u, GFC_RND_MODE);
2980 mpfr_mul (u, u, x, GFC_RND_MODE);
2981 mpfr_div (sum, sum, u, GFC_RND_MODE);
2983 mpfr_set (res, sum, GFC_RND_MODE);
2984 mpfr_set_default_prec (prec);
2986 mpfr_clears (sum, x, u, v, w, oldsum, sumtrunc, NULL);
2987 mpz_clear (num);
2991 gfc_expr *
2992 gfc_simplify_erfc_scaled (gfc_expr *x)
2994 gfc_expr *result;
2996 if (x->expr_type != EXPR_CONSTANT)
2997 return NULL;
2999 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
3000 if (mpfr_cmp_d (x->value.real, ARG_LIMIT) >= 0)
3001 asympt_erfc_scaled (result->value.real, x->value.real);
3002 else
3003 fullprec_erfc_scaled (result->value.real, x->value.real);
3005 return range_check (result, "ERFC_SCALED");
3008 #undef MAX_ITER
3009 #undef ARG_LIMIT
3012 gfc_expr *
3013 gfc_simplify_epsilon (gfc_expr *e)
3015 gfc_expr *result;
3016 int i;
3018 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
3020 result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
3021 mpfr_set (result->value.real, gfc_real_kinds[i].epsilon, GFC_RND_MODE);
3023 return range_check (result, "EPSILON");
3027 gfc_expr *
3028 gfc_simplify_exp (gfc_expr *x)
3030 gfc_expr *result;
3032 if (x->expr_type != EXPR_CONSTANT)
3033 return NULL;
3035 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
3037 switch (x->ts.type)
3039 case BT_REAL:
3040 mpfr_exp (result->value.real, x->value.real, GFC_RND_MODE);
3041 break;
3043 case BT_COMPLEX:
3044 gfc_set_model_kind (x->ts.kind);
3045 mpc_exp (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
3046 break;
3048 default:
3049 gfc_internal_error ("in gfc_simplify_exp(): Bad type");
3052 return range_check (result, "EXP");
3056 gfc_expr *
3057 gfc_simplify_exponent (gfc_expr *x)
3059 long int val;
3060 gfc_expr *result;
3062 if (x->expr_type != EXPR_CONSTANT)
3063 return NULL;
3065 result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
3066 &x->where);
3068 /* EXPONENT(inf) = EXPONENT(nan) = HUGE(0) */
3069 if (mpfr_inf_p (x->value.real) || mpfr_nan_p (x->value.real))
3071 int i = gfc_validate_kind (BT_INTEGER, gfc_default_integer_kind, false);
3072 mpz_set (result->value.integer, gfc_integer_kinds[i].huge);
3073 return result;
3076 /* EXPONENT(+/- 0.0) = 0 */
3077 if (mpfr_zero_p (x->value.real))
3079 mpz_set_ui (result->value.integer, 0);
3080 return result;
3083 gfc_set_model (x->value.real);
3085 val = (long int) mpfr_get_exp (x->value.real);
3086 mpz_set_si (result->value.integer, val);
3088 return range_check (result, "EXPONENT");
3092 gfc_expr *
3093 gfc_simplify_failed_or_stopped_images (gfc_expr *team ATTRIBUTE_UNUSED,
3094 gfc_expr *kind)
3096 if (flag_coarray == GFC_FCOARRAY_NONE)
3098 gfc_current_locus = *gfc_current_intrinsic_where;
3099 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3100 return &gfc_bad_expr;
3103 if (flag_coarray == GFC_FCOARRAY_SINGLE)
3105 gfc_expr *result;
3106 int actual_kind;
3107 if (kind)
3108 gfc_extract_int (kind, &actual_kind);
3109 else
3110 actual_kind = gfc_default_integer_kind;
3112 result = gfc_get_array_expr (BT_INTEGER, actual_kind, &gfc_current_locus);
3113 result->rank = 1;
3114 return result;
3117 /* For fcoarray = lib no simplification is possible, because it is not known
3118 what images failed or are stopped at compile time. */
3119 return NULL;
3123 gfc_expr *
3124 gfc_simplify_get_team (gfc_expr *level ATTRIBUTE_UNUSED)
3126 if (flag_coarray == GFC_FCOARRAY_NONE)
3128 gfc_current_locus = *gfc_current_intrinsic_where;
3129 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3130 return &gfc_bad_expr;
3133 if (flag_coarray == GFC_FCOARRAY_SINGLE)
3135 gfc_expr *result;
3136 result = gfc_get_array_expr (BT_INTEGER, gfc_default_integer_kind, &gfc_current_locus);
3137 result->rank = 0;
3138 return result;
3141 /* For fcoarray = lib no simplification is possible, because it is not known
3142 what images failed or are stopped at compile time. */
3143 return NULL;
3147 gfc_expr *
3148 gfc_simplify_float (gfc_expr *a)
3150 gfc_expr *result;
3152 if (a->expr_type != EXPR_CONSTANT)
3153 return NULL;
3155 result = gfc_int2real (a, gfc_default_real_kind);
3157 return range_check (result, "FLOAT");
3161 static bool
3162 is_last_ref_vtab (gfc_expr *e)
3164 gfc_ref *ref;
3165 gfc_component *comp = NULL;
3167 if (e->expr_type != EXPR_VARIABLE)
3168 return false;
3170 for (ref = e->ref; ref; ref = ref->next)
3171 if (ref->type == REF_COMPONENT)
3172 comp = ref->u.c.component;
3174 if (!e->ref || !comp)
3175 return e->symtree->n.sym->attr.vtab;
3177 if (comp->name[0] == '_' && strcmp (comp->name, "_vptr") == 0)
3178 return true;
3180 return false;
3184 gfc_expr *
3185 gfc_simplify_extends_type_of (gfc_expr *a, gfc_expr *mold)
3187 /* Avoid simplification of resolved symbols. */
3188 if (is_last_ref_vtab (a) || is_last_ref_vtab (mold))
3189 return NULL;
3191 if (a->ts.type == BT_DERIVED && mold->ts.type == BT_DERIVED)
3192 return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
3193 gfc_type_is_extension_of (mold->ts.u.derived,
3194 a->ts.u.derived));
3196 if (UNLIMITED_POLY (a) || UNLIMITED_POLY (mold))
3197 return NULL;
3199 if ((a->ts.type == BT_CLASS && !gfc_expr_attr (a).class_ok)
3200 || (mold->ts.type == BT_CLASS && !gfc_expr_attr (mold).class_ok))
3201 return NULL;
3203 /* Return .false. if the dynamic type can never be an extension. */
3204 if ((a->ts.type == BT_CLASS && mold->ts.type == BT_CLASS
3205 && !gfc_type_is_extension_of
3206 (CLASS_DATA (mold)->ts.u.derived,
3207 CLASS_DATA (a)->ts.u.derived)
3208 && !gfc_type_is_extension_of
3209 (CLASS_DATA (a)->ts.u.derived,
3210 CLASS_DATA (mold)->ts.u.derived))
3211 || (a->ts.type == BT_DERIVED && mold->ts.type == BT_CLASS
3212 && !gfc_type_is_extension_of
3213 (CLASS_DATA (mold)->ts.u.derived,
3214 a->ts.u.derived))
3215 || (a->ts.type == BT_CLASS && mold->ts.type == BT_DERIVED
3216 && !gfc_type_is_extension_of
3217 (mold->ts.u.derived,
3218 CLASS_DATA (a)->ts.u.derived)
3219 && !gfc_type_is_extension_of
3220 (CLASS_DATA (a)->ts.u.derived,
3221 mold->ts.u.derived)))
3222 return gfc_get_logical_expr (gfc_default_logical_kind, &a->where, false);
3224 /* Return .true. if the dynamic type is guaranteed to be an extension. */
3225 if (a->ts.type == BT_CLASS && mold->ts.type == BT_DERIVED
3226 && gfc_type_is_extension_of (mold->ts.u.derived,
3227 CLASS_DATA (a)->ts.u.derived))
3228 return gfc_get_logical_expr (gfc_default_logical_kind, &a->where, true);
3230 return NULL;
3234 gfc_expr *
3235 gfc_simplify_same_type_as (gfc_expr *a, gfc_expr *b)
3237 /* Avoid simplification of resolved symbols. */
3238 if (is_last_ref_vtab (a) || is_last_ref_vtab (b))
3239 return NULL;
3241 /* Return .false. if the dynamic type can never be the
3242 same. */
3243 if (((a->ts.type == BT_CLASS && gfc_expr_attr (a).class_ok)
3244 || (b->ts.type == BT_CLASS && gfc_expr_attr (b).class_ok))
3245 && !gfc_type_compatible (&a->ts, &b->ts)
3246 && !gfc_type_compatible (&b->ts, &a->ts))
3247 return gfc_get_logical_expr (gfc_default_logical_kind, &a->where, false);
3249 if (a->ts.type != BT_DERIVED || b->ts.type != BT_DERIVED)
3250 return NULL;
3252 return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
3253 gfc_compare_derived_types (a->ts.u.derived,
3254 b->ts.u.derived));
3258 gfc_expr *
3259 gfc_simplify_floor (gfc_expr *e, gfc_expr *k)
3261 gfc_expr *result;
3262 mpfr_t floor;
3263 int kind;
3265 kind = get_kind (BT_INTEGER, k, "FLOOR", gfc_default_integer_kind);
3266 if (kind == -1)
3267 gfc_internal_error ("gfc_simplify_floor(): Bad kind");
3269 if (e->expr_type != EXPR_CONSTANT)
3270 return NULL;
3272 mpfr_init2 (floor, mpfr_get_prec (e->value.real));
3273 mpfr_floor (floor, e->value.real);
3275 result = gfc_get_constant_expr (BT_INTEGER, kind, &e->where);
3276 gfc_mpfr_to_mpz (result->value.integer, floor, &e->where);
3278 mpfr_clear (floor);
3280 return range_check (result, "FLOOR");
3284 gfc_expr *
3285 gfc_simplify_fraction (gfc_expr *x)
3287 gfc_expr *result;
3288 mpfr_exp_t e;
3290 if (x->expr_type != EXPR_CONSTANT)
3291 return NULL;
3293 result = gfc_get_constant_expr (BT_REAL, x->ts.kind, &x->where);
3295 /* FRACTION(inf) = NaN. */
3296 if (mpfr_inf_p (x->value.real))
3298 mpfr_set_nan (result->value.real);
3299 return result;
3302 /* mpfr_frexp() correctly handles zeros and NaNs. */
3303 mpfr_frexp (&e, result->value.real, x->value.real, GFC_RND_MODE);
3305 return range_check (result, "FRACTION");
3309 gfc_expr *
3310 gfc_simplify_gamma (gfc_expr *x)
3312 gfc_expr *result;
3314 if (x->expr_type != EXPR_CONSTANT)
3315 return NULL;
3317 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
3318 mpfr_gamma (result->value.real, x->value.real, GFC_RND_MODE);
3320 return range_check (result, "GAMMA");
3324 gfc_expr *
3325 gfc_simplify_huge (gfc_expr *e)
3327 gfc_expr *result;
3328 int i;
3330 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
3331 result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
3333 switch (e->ts.type)
3335 case BT_INTEGER:
3336 mpz_set (result->value.integer, gfc_integer_kinds[i].huge);
3337 break;
3339 case BT_UNSIGNED:
3340 mpz_set (result->value.integer, gfc_unsigned_kinds[i].huge);
3341 break;
3343 case BT_REAL:
3344 mpfr_set (result->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
3345 break;
3347 default:
3348 gcc_unreachable ();
3351 return result;
3355 gfc_expr *
3356 gfc_simplify_hypot (gfc_expr *x, gfc_expr *y)
3358 gfc_expr *result;
3360 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
3361 return NULL;
3363 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
3364 mpfr_hypot (result->value.real, x->value.real, y->value.real, GFC_RND_MODE);
3365 return range_check (result, "HYPOT");
3369 /* We use the processor's collating sequence, because all
3370 systems that gfortran currently works on are ASCII. */
3372 gfc_expr *
3373 gfc_simplify_iachar (gfc_expr *e, gfc_expr *kind)
3375 gfc_expr *result;
3376 gfc_char_t index;
3377 int k;
3379 if (e->expr_type != EXPR_CONSTANT)
3380 return NULL;
3382 if (e->value.character.length != 1)
3384 gfc_error ("Argument of IACHAR at %L must be of length one", &e->where);
3385 return &gfc_bad_expr;
3388 index = e->value.character.string[0];
3390 if (warn_surprising && index > 127)
3391 gfc_warning (OPT_Wsurprising,
3392 "Argument of IACHAR function at %L outside of range 0..127",
3393 &e->where);
3395 k = get_kind (BT_INTEGER, kind, "IACHAR", gfc_default_integer_kind);
3396 if (k == -1)
3397 return &gfc_bad_expr;
3399 result = gfc_get_int_expr (k, &e->where, index);
3401 return range_check (result, "IACHAR");
3405 static gfc_expr *
3406 do_bit_and (gfc_expr *result, gfc_expr *e)
3408 if (flag_unsigned)
3410 gcc_assert ((e->ts.type == BT_INTEGER || e->ts.type == BT_UNSIGNED)
3411 && e->expr_type == EXPR_CONSTANT);
3412 gcc_assert ((result->ts.type == BT_INTEGER
3413 || result->ts.type == BT_UNSIGNED)
3414 && result->expr_type == EXPR_CONSTANT);
3416 else
3418 gcc_assert (e->ts.type == BT_INTEGER && e->expr_type == EXPR_CONSTANT);
3419 gcc_assert (result->ts.type == BT_INTEGER
3420 && result->expr_type == EXPR_CONSTANT);
3423 mpz_and (result->value.integer, result->value.integer, e->value.integer);
3424 return result;
3428 gfc_expr *
3429 gfc_simplify_iall (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
3431 return simplify_transformation (array, dim, mask, -1, do_bit_and);
3435 static gfc_expr *
3436 do_bit_ior (gfc_expr *result, gfc_expr *e)
3438 if (flag_unsigned)
3440 gcc_assert ((e->ts.type == BT_INTEGER || e->ts.type == BT_UNSIGNED)
3441 && e->expr_type == EXPR_CONSTANT);
3442 gcc_assert ((result->ts.type == BT_INTEGER
3443 || result->ts.type == BT_UNSIGNED)
3444 && result->expr_type == EXPR_CONSTANT);
3446 else
3448 gcc_assert (e->ts.type == BT_INTEGER && e->expr_type == EXPR_CONSTANT);
3449 gcc_assert (result->ts.type == BT_INTEGER
3450 && result->expr_type == EXPR_CONSTANT);
3453 mpz_ior (result->value.integer, result->value.integer, e->value.integer);
3454 return result;
3458 gfc_expr *
3459 gfc_simplify_iany (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
3461 return simplify_transformation (array, dim, mask, 0, do_bit_ior);
3465 gfc_expr *
3466 gfc_simplify_iand (gfc_expr *x, gfc_expr *y)
3468 gfc_expr *result;
3469 bt type;
3471 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
3472 return NULL;
3474 type = x->ts.type == BT_UNSIGNED ? BT_UNSIGNED : BT_INTEGER;
3475 result = gfc_get_constant_expr (type, x->ts.kind, &x->where);
3476 mpz_and (result->value.integer, x->value.integer, y->value.integer);
3478 return range_check (result, "IAND");
3482 gfc_expr *
3483 gfc_simplify_ibclr (gfc_expr *x, gfc_expr *y)
3485 gfc_expr *result;
3486 int k, pos;
3488 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
3489 return NULL;
3491 if (!gfc_check_bitfcn (x, y))
3492 return &gfc_bad_expr;
3494 gfc_extract_int (y, &pos);
3496 k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
3498 result = gfc_copy_expr (x);
3499 /* Drop any separate memory representation of x to avoid potential
3500 inconsistencies in result. */
3501 if (result->representation.string)
3503 free (result->representation.string);
3504 result->representation.string = NULL;
3507 if (x->ts.type == BT_INTEGER)
3509 gfc_convert_mpz_to_unsigned (result->value.integer,
3510 gfc_integer_kinds[k].bit_size);
3512 mpz_clrbit (result->value.integer, pos);
3514 gfc_convert_mpz_to_signed (result->value.integer,
3515 gfc_integer_kinds[k].bit_size);
3517 else
3518 mpz_clrbit (result->value.integer, pos);
3520 return result;
3524 gfc_expr *
3525 gfc_simplify_ibits (gfc_expr *x, gfc_expr *y, gfc_expr *z)
3527 gfc_expr *result;
3528 int pos, len;
3529 int i, k, bitsize;
3530 int *bits;
3532 if (x->expr_type != EXPR_CONSTANT
3533 || y->expr_type != EXPR_CONSTANT
3534 || z->expr_type != EXPR_CONSTANT)
3535 return NULL;
3537 if (!gfc_check_ibits (x, y, z))
3538 return &gfc_bad_expr;
3540 gfc_extract_int (y, &pos);
3541 gfc_extract_int (z, &len);
3543 k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
3545 if (x->ts.type == BT_INTEGER)
3546 bitsize = gfc_integer_kinds[k].bit_size;
3547 else
3548 bitsize = gfc_unsigned_kinds[k].bit_size;
3551 if (pos + len > bitsize)
3553 gfc_error ("Sum of second and third arguments of IBITS exceeds "
3554 "bit size at %L", &y->where);
3555 return &gfc_bad_expr;
3558 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
3560 if (x->ts.type == BT_INTEGER)
3561 gfc_convert_mpz_to_unsigned (result->value.integer,
3562 gfc_integer_kinds[k].bit_size);
3564 bits = XCNEWVEC (int, bitsize);
3566 for (i = 0; i < bitsize; i++)
3567 bits[i] = 0;
3569 for (i = 0; i < len; i++)
3570 bits[i] = mpz_tstbit (x->value.integer, i + pos);
3572 for (i = 0; i < bitsize; i++)
3574 if (bits[i] == 0)
3575 mpz_clrbit (result->value.integer, i);
3576 else if (bits[i] == 1)
3577 mpz_setbit (result->value.integer, i);
3578 else
3579 gfc_internal_error ("IBITS: Bad bit");
3582 free (bits);
3584 if (x->ts.type == BT_INTEGER)
3585 gfc_convert_mpz_to_signed (result->value.integer,
3586 gfc_integer_kinds[k].bit_size);
3588 return result;
3592 gfc_expr *
3593 gfc_simplify_ibset (gfc_expr *x, gfc_expr *y)
3595 gfc_expr *result;
3596 int k, pos;
3598 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
3599 return NULL;
3601 if (!gfc_check_bitfcn (x, y))
3602 return &gfc_bad_expr;
3604 gfc_extract_int (y, &pos);
3606 k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
3608 result = gfc_copy_expr (x);
3609 /* Drop any separate memory representation of x to avoid potential
3610 inconsistencies in result. */
3611 if (result->representation.string)
3613 free (result->representation.string);
3614 result->representation.string = NULL;
3617 if (x->ts.type == BT_INTEGER)
3619 gfc_convert_mpz_to_unsigned (result->value.integer,
3620 gfc_integer_kinds[k].bit_size);
3622 mpz_setbit (result->value.integer, pos);
3624 gfc_convert_mpz_to_signed (result->value.integer,
3625 gfc_integer_kinds[k].bit_size);
3627 else
3628 mpz_setbit (result->value.integer, pos);
3630 return result;
3634 gfc_expr *
3635 gfc_simplify_ichar (gfc_expr *e, gfc_expr *kind)
3637 gfc_expr *result;
3638 gfc_char_t index;
3639 int k;
3641 if (e->expr_type != EXPR_CONSTANT)
3642 return NULL;
3644 if (e->value.character.length != 1)
3646 gfc_error ("Argument of ICHAR at %L must be of length one", &e->where);
3647 return &gfc_bad_expr;
3650 index = e->value.character.string[0];
3652 k = get_kind (BT_INTEGER, kind, "ICHAR", gfc_default_integer_kind);
3653 if (k == -1)
3654 return &gfc_bad_expr;
3656 result = gfc_get_int_expr (k, &e->where, index);
3658 return range_check (result, "ICHAR");
3662 gfc_expr *
3663 gfc_simplify_ieor (gfc_expr *x, gfc_expr *y)
3665 gfc_expr *result;
3666 bt type;
3668 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
3669 return NULL;
3671 type = x->ts.type == BT_UNSIGNED ? BT_UNSIGNED : BT_INTEGER;
3672 result = gfc_get_constant_expr (type, x->ts.kind, &x->where);
3673 mpz_xor (result->value.integer, x->value.integer, y->value.integer);
3675 return range_check (result, "IEOR");
3679 gfc_expr *
3680 gfc_simplify_index (gfc_expr *x, gfc_expr *y, gfc_expr *b, gfc_expr *kind)
3682 gfc_expr *result;
3683 bool back;
3684 HOST_WIDE_INT len, lensub, start, last, i, index = 0;
3685 int k, delta;
3687 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT
3688 || ( b != NULL && b->expr_type != EXPR_CONSTANT))
3689 return NULL;
3691 back = (b != NULL && b->value.logical != 0);
3693 k = get_kind (BT_INTEGER, kind, "INDEX", gfc_default_integer_kind);
3694 if (k == -1)
3695 return &gfc_bad_expr;
3697 result = gfc_get_constant_expr (BT_INTEGER, k, &x->where);
3699 len = x->value.character.length;
3700 lensub = y->value.character.length;
3702 if (len < lensub)
3704 mpz_set_si (result->value.integer, 0);
3705 return result;
3708 if (lensub == 0)
3710 if (back)
3711 index = len + 1;
3712 else
3713 index = 1;
3714 goto done;
3717 if (!back)
3719 last = len + 1 - lensub;
3720 start = 0;
3721 delta = 1;
3723 else
3725 last = -1;
3726 start = len - lensub;
3727 delta = -1;
3730 for (; start != last; start += delta)
3732 for (i = 0; i < lensub; i++)
3734 if (x->value.character.string[start + i]
3735 != y->value.character.string[i])
3736 break;
3738 if (i == lensub)
3740 index = start + 1;
3741 goto done;
3745 done:
3746 mpz_set_si (result->value.integer, index);
3747 return range_check (result, "INDEX");
3750 static gfc_expr *
3751 simplify_intconv (gfc_expr *e, int kind, const char *name)
3753 gfc_expr *result = NULL;
3754 int tmp1, tmp2;
3756 /* Convert BOZ to integer, and return without range checking. */
3757 if (e->ts.type == BT_BOZ)
3759 if (!gfc_boz2int (e, kind))
3760 return NULL;
3761 result = gfc_copy_expr (e);
3762 return result;
3765 if (e->expr_type != EXPR_CONSTANT)
3766 return NULL;
3768 /* For explicit conversion, turn off -Wconversion and -Wconversion-extra
3769 warnings. */
3770 tmp1 = warn_conversion;
3771 tmp2 = warn_conversion_extra;
3772 warn_conversion = warn_conversion_extra = 0;
3774 result = gfc_convert_constant (e, BT_INTEGER, kind);
3776 warn_conversion = tmp1;
3777 warn_conversion_extra = tmp2;
3779 if (result == &gfc_bad_expr)
3780 return &gfc_bad_expr;
3782 return range_check (result, name);
3786 gfc_expr *
3787 gfc_simplify_int (gfc_expr *e, gfc_expr *k)
3789 int kind;
3791 kind = get_kind (BT_INTEGER, k, "INT", gfc_default_integer_kind);
3792 if (kind == -1)
3793 return &gfc_bad_expr;
3795 return simplify_intconv (e, kind, "INT");
3798 gfc_expr *
3799 gfc_simplify_int2 (gfc_expr *e)
3801 return simplify_intconv (e, 2, "INT2");
3805 gfc_expr *
3806 gfc_simplify_int8 (gfc_expr *e)
3808 return simplify_intconv (e, 8, "INT8");
3812 gfc_expr *
3813 gfc_simplify_long (gfc_expr *e)
3815 return simplify_intconv (e, 4, "LONG");
3819 gfc_expr *
3820 gfc_simplify_ifix (gfc_expr *e)
3822 gfc_expr *rtrunc, *result;
3824 if (e->expr_type != EXPR_CONSTANT)
3825 return NULL;
3827 rtrunc = gfc_copy_expr (e);
3828 mpfr_trunc (rtrunc->value.real, e->value.real);
3830 result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
3831 &e->where);
3832 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real, &e->where);
3834 gfc_free_expr (rtrunc);
3836 return range_check (result, "IFIX");
3840 gfc_expr *
3841 gfc_simplify_idint (gfc_expr *e)
3843 gfc_expr *rtrunc, *result;
3845 if (e->expr_type != EXPR_CONSTANT)
3846 return NULL;
3848 rtrunc = gfc_copy_expr (e);
3849 mpfr_trunc (rtrunc->value.real, e->value.real);
3851 result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
3852 &e->where);
3853 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real, &e->where);
3855 gfc_free_expr (rtrunc);
3857 return range_check (result, "IDINT");
3860 gfc_expr *
3861 gfc_simplify_uint (gfc_expr *e, gfc_expr *k)
3863 gfc_expr *result = NULL;
3864 int kind;
3866 /* KIND is always an integer. */
3868 kind = get_kind (BT_INTEGER, k, "INT", gfc_default_integer_kind);
3869 if (kind == -1)
3870 return &gfc_bad_expr;
3872 /* Convert BOZ to integer, and return without range checking. */
3873 if (e->ts.type == BT_BOZ)
3875 if (!gfc_boz2uint (e, kind))
3876 return NULL;
3877 result = gfc_copy_expr (e);
3878 return result;
3881 if (e->expr_type != EXPR_CONSTANT)
3882 return NULL;
3884 result = gfc_convert_constant (e, BT_UNSIGNED, kind);
3886 if (result == &gfc_bad_expr)
3887 return &gfc_bad_expr;
3889 return range_check (result, "UINT");
3893 gfc_expr *
3894 gfc_simplify_ior (gfc_expr *x, gfc_expr *y)
3896 gfc_expr *result;
3897 bt type;
3899 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
3900 return NULL;
3902 type = x->ts.type == BT_UNSIGNED ? BT_UNSIGNED : BT_INTEGER;
3903 result = gfc_get_constant_expr (type, x->ts.kind, &x->where);
3904 mpz_ior (result->value.integer, x->value.integer, y->value.integer);
3906 return range_check (result, "IOR");
3910 static gfc_expr *
3911 do_bit_xor (gfc_expr *result, gfc_expr *e)
3913 if (flag_unsigned)
3915 gcc_assert ((e->ts.type == BT_INTEGER || e->ts.type == BT_UNSIGNED)
3916 && e->expr_type == EXPR_CONSTANT);
3917 gcc_assert ((result->ts.type == BT_INTEGER
3918 || result->ts.type == BT_UNSIGNED)
3919 && result->expr_type == EXPR_CONSTANT);
3921 else
3923 gcc_assert (e->ts.type == BT_INTEGER && e->expr_type == EXPR_CONSTANT);
3924 gcc_assert (result->ts.type == BT_INTEGER
3925 && result->expr_type == EXPR_CONSTANT);
3928 mpz_xor (result->value.integer, result->value.integer, e->value.integer);
3929 return result;
3933 gfc_expr *
3934 gfc_simplify_iparity (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
3936 return simplify_transformation (array, dim, mask, 0, do_bit_xor);
3940 gfc_expr *
3941 gfc_simplify_is_iostat_end (gfc_expr *x)
3943 if (x->expr_type != EXPR_CONSTANT)
3944 return NULL;
3946 return gfc_get_logical_expr (gfc_default_logical_kind, &x->where,
3947 mpz_cmp_si (x->value.integer,
3948 LIBERROR_END) == 0);
3952 gfc_expr *
3953 gfc_simplify_is_iostat_eor (gfc_expr *x)
3955 if (x->expr_type != EXPR_CONSTANT)
3956 return NULL;
3958 return gfc_get_logical_expr (gfc_default_logical_kind, &x->where,
3959 mpz_cmp_si (x->value.integer,
3960 LIBERROR_EOR) == 0);
3964 gfc_expr *
3965 gfc_simplify_isnan (gfc_expr *x)
3967 if (x->expr_type != EXPR_CONSTANT)
3968 return NULL;
3970 return gfc_get_logical_expr (gfc_default_logical_kind, &x->where,
3971 mpfr_nan_p (x->value.real));
3975 /* Performs a shift on its first argument. Depending on the last
3976 argument, the shift can be arithmetic, i.e. with filling from the
3977 left like in the SHIFTA intrinsic. */
3978 static gfc_expr *
3979 simplify_shift (gfc_expr *e, gfc_expr *s, const char *name,
3980 bool arithmetic, int direction)
3982 gfc_expr *result;
3983 int ashift, *bits, i, k, bitsize, shift;
3985 if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
3986 return NULL;
3988 gfc_extract_int (s, &shift);
3990 k = gfc_validate_kind (e->ts.type, e->ts.kind, false);
3991 if (e->ts.type == BT_INTEGER)
3992 bitsize = gfc_integer_kinds[k].bit_size;
3993 else
3994 bitsize = gfc_unsigned_kinds[k].bit_size;
3996 result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
3998 if (shift == 0)
4000 mpz_set (result->value.integer, e->value.integer);
4001 return result;
4004 if (direction > 0 && shift < 0)
4006 /* Left shift, as in SHIFTL. */
4007 gfc_error ("Second argument of %s is negative at %L", name, &e->where);
4008 return &gfc_bad_expr;
4010 else if (direction < 0)
4012 /* Right shift, as in SHIFTR or SHIFTA. */
4013 if (shift < 0)
4015 gfc_error ("Second argument of %s is negative at %L",
4016 name, &e->where);
4017 return &gfc_bad_expr;
4020 shift = -shift;
4023 ashift = (shift >= 0 ? shift : -shift);
4025 if (ashift > bitsize)
4027 gfc_error ("Magnitude of second argument of %s exceeds bit size "
4028 "at %L", name, &e->where);
4029 return &gfc_bad_expr;
4032 bits = XCNEWVEC (int, bitsize);
4034 for (i = 0; i < bitsize; i++)
4035 bits[i] = mpz_tstbit (e->value.integer, i);
4037 if (shift > 0)
4039 /* Left shift. */
4040 for (i = 0; i < shift; i++)
4041 mpz_clrbit (result->value.integer, i);
4043 for (i = 0; i < bitsize - shift; i++)
4045 if (bits[i] == 0)
4046 mpz_clrbit (result->value.integer, i + shift);
4047 else
4048 mpz_setbit (result->value.integer, i + shift);
4051 else
4053 /* Right shift. */
4054 if (arithmetic && bits[bitsize - 1])
4055 for (i = bitsize - 1; i >= bitsize - ashift; i--)
4056 mpz_setbit (result->value.integer, i);
4057 else
4058 for (i = bitsize - 1; i >= bitsize - ashift; i--)
4059 mpz_clrbit (result->value.integer, i);
4061 for (i = bitsize - 1; i >= ashift; i--)
4063 if (bits[i] == 0)
4064 mpz_clrbit (result->value.integer, i - ashift);
4065 else
4066 mpz_setbit (result->value.integer, i - ashift);
4070 if (result->ts.type == BT_INTEGER)
4071 gfc_convert_mpz_to_signed (result->value.integer, bitsize);
4072 else
4073 gfc_reduce_unsigned(result);
4075 free (bits);
4077 return result;
4081 gfc_expr *
4082 gfc_simplify_ishft (gfc_expr *e, gfc_expr *s)
4084 return simplify_shift (e, s, "ISHFT", false, 0);
4088 gfc_expr *
4089 gfc_simplify_lshift (gfc_expr *e, gfc_expr *s)
4091 return simplify_shift (e, s, "LSHIFT", false, 1);
4095 gfc_expr *
4096 gfc_simplify_rshift (gfc_expr *e, gfc_expr *s)
4098 return simplify_shift (e, s, "RSHIFT", true, -1);
4102 gfc_expr *
4103 gfc_simplify_shifta (gfc_expr *e, gfc_expr *s)
4105 return simplify_shift (e, s, "SHIFTA", true, -1);
4109 gfc_expr *
4110 gfc_simplify_shiftl (gfc_expr *e, gfc_expr *s)
4112 return simplify_shift (e, s, "SHIFTL", false, 1);
4116 gfc_expr *
4117 gfc_simplify_shiftr (gfc_expr *e, gfc_expr *s)
4119 return simplify_shift (e, s, "SHIFTR", false, -1);
4123 gfc_expr *
4124 gfc_simplify_ishftc (gfc_expr *e, gfc_expr *s, gfc_expr *sz)
4126 gfc_expr *result;
4127 int shift, ashift, isize, ssize, delta, k;
4128 int i, *bits;
4130 if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
4131 return NULL;
4133 gfc_extract_int (s, &shift);
4135 k = gfc_validate_kind (e->ts.type, e->ts.kind, false);
4136 isize = gfc_integer_kinds[k].bit_size;
4138 if (sz != NULL)
4140 if (sz->expr_type != EXPR_CONSTANT)
4141 return NULL;
4143 gfc_extract_int (sz, &ssize);
4145 if (ssize > isize || ssize <= 0)
4146 return &gfc_bad_expr;
4148 else
4149 ssize = isize;
4151 if (shift >= 0)
4152 ashift = shift;
4153 else
4154 ashift = -shift;
4156 if (ashift > ssize)
4158 if (sz == NULL)
4159 gfc_error ("Magnitude of second argument of ISHFTC exceeds "
4160 "BIT_SIZE of first argument at %C");
4161 else
4162 gfc_error ("Absolute value of SHIFT shall be less than or equal "
4163 "to SIZE at %C");
4164 return &gfc_bad_expr;
4167 result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
4169 mpz_set (result->value.integer, e->value.integer);
4171 if (shift == 0)
4172 return result;
4174 if (result->ts.type == BT_INTEGER)
4175 gfc_convert_mpz_to_unsigned (result->value.integer, isize);
4177 bits = XCNEWVEC (int, ssize);
4179 for (i = 0; i < ssize; i++)
4180 bits[i] = mpz_tstbit (e->value.integer, i);
4182 delta = ssize - ashift;
4184 if (shift > 0)
4186 for (i = 0; i < delta; i++)
4188 if (bits[i] == 0)
4189 mpz_clrbit (result->value.integer, i + shift);
4190 else
4191 mpz_setbit (result->value.integer, i + shift);
4194 for (i = delta; i < ssize; i++)
4196 if (bits[i] == 0)
4197 mpz_clrbit (result->value.integer, i - delta);
4198 else
4199 mpz_setbit (result->value.integer, i - delta);
4202 else
4204 for (i = 0; i < ashift; i++)
4206 if (bits[i] == 0)
4207 mpz_clrbit (result->value.integer, i + delta);
4208 else
4209 mpz_setbit (result->value.integer, i + delta);
4212 for (i = ashift; i < ssize; i++)
4214 if (bits[i] == 0)
4215 mpz_clrbit (result->value.integer, i + shift);
4216 else
4217 mpz_setbit (result->value.integer, i + shift);
4221 if (result->ts.type == BT_INTEGER)
4222 gfc_convert_mpz_to_signed (result->value.integer, isize);
4224 free (bits);
4225 return result;
4229 gfc_expr *
4230 gfc_simplify_kind (gfc_expr *e)
4232 return gfc_get_int_expr (gfc_default_integer_kind, NULL, e->ts.kind);
4236 static gfc_expr *
4237 simplify_bound_dim (gfc_expr *array, gfc_expr *kind, int d, int upper,
4238 gfc_array_spec *as, gfc_ref *ref, bool coarray)
4240 gfc_expr *l, *u, *result;
4241 int k;
4243 k = get_kind (BT_INTEGER, kind, upper ? "UBOUND" : "LBOUND",
4244 gfc_default_integer_kind);
4245 if (k == -1)
4246 return &gfc_bad_expr;
4248 result = gfc_get_constant_expr (BT_INTEGER, k, &array->where);
4250 /* For non-variables, LBOUND(expr, DIM=n) = 1 and
4251 UBOUND(expr, DIM=n) = SIZE(expr, DIM=n). */
4252 if (!coarray && array->expr_type != EXPR_VARIABLE)
4254 if (upper)
4256 gfc_expr* dim = result;
4257 mpz_set_si (dim->value.integer, d);
4259 result = simplify_size (array, dim, k);
4260 gfc_free_expr (dim);
4261 if (!result)
4262 goto returnNull;
4264 else
4265 mpz_set_si (result->value.integer, 1);
4267 goto done;
4270 /* Otherwise, we have a variable expression. */
4271 gcc_assert (array->expr_type == EXPR_VARIABLE);
4272 gcc_assert (as);
4274 if (!gfc_resolve_array_spec (as, 0))
4275 return NULL;
4277 /* The last dimension of an assumed-size array is special. */
4278 if ((!coarray && d == as->rank && as->type == AS_ASSUMED_SIZE && !upper)
4279 || (coarray && d == as->rank + as->corank
4280 && (!upper || flag_coarray == GFC_FCOARRAY_SINGLE)))
4282 if (as->lower[d-1] && as->lower[d-1]->expr_type == EXPR_CONSTANT)
4284 gfc_free_expr (result);
4285 return gfc_copy_expr (as->lower[d-1]);
4288 goto returnNull;
4291 /* Then, we need to know the extent of the given dimension. */
4292 if (coarray || (ref->u.ar.type == AR_FULL && !ref->next))
4294 gfc_expr *declared_bound;
4295 int empty_bound;
4296 bool constant_lbound, constant_ubound;
4298 l = as->lower[d-1];
4299 u = as->upper[d-1];
4301 gcc_assert (l != NULL);
4303 constant_lbound = l->expr_type == EXPR_CONSTANT;
4304 constant_ubound = u && u->expr_type == EXPR_CONSTANT;
4306 empty_bound = upper ? 0 : 1;
4307 declared_bound = upper ? u : l;
4309 if ((!upper && !constant_lbound)
4310 || (upper && !constant_ubound))
4311 goto returnNull;
4313 if (!coarray)
4315 /* For {L,U}BOUND, the value depends on whether the array
4316 is empty. We can nevertheless simplify if the declared bound
4317 has the same value as that of an empty array, in which case
4318 the result isn't dependent on the array emptiness. */
4319 if (mpz_cmp_si (declared_bound->value.integer, empty_bound) == 0)
4320 mpz_set_si (result->value.integer, empty_bound);
4321 else if (!constant_lbound || !constant_ubound)
4322 /* Array emptiness can't be determined, we can't simplify. */
4323 goto returnNull;
4324 else if (mpz_cmp (l->value.integer, u->value.integer) > 0)
4325 mpz_set_si (result->value.integer, empty_bound);
4326 else
4327 mpz_set (result->value.integer, declared_bound->value.integer);
4329 else
4330 mpz_set (result->value.integer, declared_bound->value.integer);
4332 else
4334 if (upper)
4336 int d2 = 0, cnt = 0;
4337 for (int idx = 0; idx < ref->u.ar.dimen; ++idx)
4339 if (ref->u.ar.dimen_type[idx] == DIMEN_ELEMENT)
4340 d2++;
4341 else if (cnt < d - 1)
4342 cnt++;
4343 else
4344 break;
4346 if (!gfc_ref_dimen_size (&ref->u.ar, d2 + d - 1, &result->value.integer, NULL))
4347 goto returnNull;
4349 else
4350 mpz_set_si (result->value.integer, (long int) 1);
4353 done:
4354 return range_check (result, upper ? "UBOUND" : "LBOUND");
4356 returnNull:
4357 gfc_free_expr (result);
4358 return NULL;
4362 static gfc_expr *
4363 simplify_bound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind, int upper)
4365 gfc_ref *ref;
4366 gfc_array_spec *as;
4367 ar_type type = AR_UNKNOWN;
4368 int d;
4370 if (array->ts.type == BT_CLASS)
4371 return NULL;
4373 if (array->expr_type != EXPR_VARIABLE)
4375 as = NULL;
4376 ref = NULL;
4377 goto done;
4380 /* Do not attempt to resolve if error has already been issued. */
4381 if (array->symtree->n.sym->error)
4382 return NULL;
4384 /* Follow any component references. */
4385 as = array->symtree->n.sym->as;
4386 for (ref = array->ref; ref; ref = ref->next)
4388 switch (ref->type)
4390 case REF_ARRAY:
4391 type = ref->u.ar.type;
4392 switch (ref->u.ar.type)
4394 case AR_ELEMENT:
4395 as = NULL;
4396 continue;
4398 case AR_FULL:
4399 /* We're done because 'as' has already been set in the
4400 previous iteration. */
4401 goto done;
4403 case AR_UNKNOWN:
4404 return NULL;
4406 case AR_SECTION:
4407 as = ref->u.ar.as;
4408 goto done;
4411 gcc_unreachable ();
4413 case REF_COMPONENT:
4414 as = ref->u.c.component->as;
4415 continue;
4417 case REF_SUBSTRING:
4418 case REF_INQUIRY:
4419 continue;
4423 gcc_unreachable ();
4425 done:
4427 if (as && (as->type == AS_DEFERRED || as->type == AS_ASSUMED_RANK
4428 || (as->type == AS_ASSUMED_SHAPE && upper)))
4429 return NULL;
4431 /* 'array' shall not be an unallocated allocatable variable or a pointer that
4432 is not associated. */
4433 if (array->expr_type == EXPR_VARIABLE
4434 && (gfc_expr_attr (array).allocatable || gfc_expr_attr (array).pointer))
4435 return NULL;
4437 gcc_assert (!as
4438 || (as->type != AS_DEFERRED
4439 && array->expr_type == EXPR_VARIABLE
4440 && !gfc_expr_attr (array).allocatable
4441 && !gfc_expr_attr (array).pointer));
4443 if (dim == NULL)
4445 /* Multi-dimensional bounds. */
4446 gfc_expr *bounds[GFC_MAX_DIMENSIONS];
4447 gfc_expr *e;
4448 int k;
4450 /* UBOUND(ARRAY) is not valid for an assumed-size array. */
4451 if (upper && type == AR_FULL && as && as->type == AS_ASSUMED_SIZE)
4453 /* An error message will be emitted in
4454 check_assumed_size_reference (resolve.cc). */
4455 return &gfc_bad_expr;
4458 /* Simplify the bounds for each dimension. */
4459 for (d = 0; d < array->rank; d++)
4461 bounds[d] = simplify_bound_dim (array, kind, d + 1, upper, as, ref,
4462 false);
4463 if (bounds[d] == NULL || bounds[d] == &gfc_bad_expr)
4465 int j;
4467 for (j = 0; j < d; j++)
4468 gfc_free_expr (bounds[j]);
4470 if (gfc_seen_div0)
4471 return &gfc_bad_expr;
4472 else
4473 return bounds[d];
4477 /* Allocate the result expression. */
4478 k = get_kind (BT_INTEGER, kind, upper ? "UBOUND" : "LBOUND",
4479 gfc_default_integer_kind);
4480 if (k == -1)
4481 return &gfc_bad_expr;
4483 e = gfc_get_array_expr (BT_INTEGER, k, &array->where);
4485 /* The result is a rank 1 array; its size is the rank of the first
4486 argument to {L,U}BOUND. */
4487 e->rank = 1;
4488 e->shape = gfc_get_shape (1);
4489 mpz_init_set_ui (e->shape[0], array->rank);
4491 /* Create the constructor for this array. */
4492 for (d = 0; d < array->rank; d++)
4493 gfc_constructor_append_expr (&e->value.constructor,
4494 bounds[d], &e->where);
4496 return e;
4498 else
4500 /* A DIM argument is specified. */
4501 if (dim->expr_type != EXPR_CONSTANT)
4502 return NULL;
4504 d = mpz_get_si (dim->value.integer);
4506 if ((d < 1 || d > array->rank)
4507 || (d == array->rank && as && as->type == AS_ASSUMED_SIZE && upper))
4509 gfc_error ("DIM argument at %L is out of bounds", &dim->where);
4510 return &gfc_bad_expr;
4513 if (as && as->type == AS_ASSUMED_RANK)
4514 return NULL;
4516 return simplify_bound_dim (array, kind, d, upper, as, ref, false);
4521 static gfc_expr *
4522 simplify_cobound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind, int upper)
4524 gfc_ref *ref;
4525 gfc_array_spec *as;
4526 int d;
4528 if (array->expr_type != EXPR_VARIABLE)
4529 return NULL;
4531 /* Follow any component references. */
4532 as = (array->ts.type == BT_CLASS && CLASS_DATA (array))
4533 ? CLASS_DATA (array)->as
4534 : array->symtree->n.sym->as;
4535 for (ref = array->ref; ref; ref = ref->next)
4537 switch (ref->type)
4539 case REF_ARRAY:
4540 switch (ref->u.ar.type)
4542 case AR_ELEMENT:
4543 if (ref->u.ar.as->corank > 0)
4545 gcc_assert (as == ref->u.ar.as);
4546 goto done;
4548 as = NULL;
4549 continue;
4551 case AR_FULL:
4552 /* We're done because 'as' has already been set in the
4553 previous iteration. */
4554 goto done;
4556 case AR_UNKNOWN:
4557 return NULL;
4559 case AR_SECTION:
4560 as = ref->u.ar.as;
4561 goto done;
4564 gcc_unreachable ();
4566 case REF_COMPONENT:
4567 as = ref->u.c.component->as;
4568 continue;
4570 case REF_SUBSTRING:
4571 case REF_INQUIRY:
4572 continue;
4576 if (!as)
4577 gcc_unreachable ();
4579 done:
4581 if (as->cotype == AS_DEFERRED || as->cotype == AS_ASSUMED_SHAPE)
4582 return NULL;
4584 if (dim == NULL)
4586 /* Multi-dimensional cobounds. */
4587 gfc_expr *bounds[GFC_MAX_DIMENSIONS];
4588 gfc_expr *e;
4589 int k;
4591 /* Simplify the cobounds for each dimension. */
4592 for (d = 0; d < as->corank; d++)
4594 bounds[d] = simplify_bound_dim (array, kind, d + 1 + as->rank,
4595 upper, as, ref, true);
4596 if (bounds[d] == NULL || bounds[d] == &gfc_bad_expr)
4598 int j;
4600 for (j = 0; j < d; j++)
4601 gfc_free_expr (bounds[j]);
4602 return bounds[d];
4606 /* Allocate the result expression. */
4607 e = gfc_get_expr ();
4608 e->where = array->where;
4609 e->expr_type = EXPR_ARRAY;
4610 e->ts.type = BT_INTEGER;
4611 k = get_kind (BT_INTEGER, kind, upper ? "UCOBOUND" : "LCOBOUND",
4612 gfc_default_integer_kind);
4613 if (k == -1)
4615 gfc_free_expr (e);
4616 return &gfc_bad_expr;
4618 e->ts.kind = k;
4620 /* The result is a rank 1 array; its size is the rank of the first
4621 argument to {L,U}COBOUND. */
4622 e->rank = 1;
4623 e->shape = gfc_get_shape (1);
4624 mpz_init_set_ui (e->shape[0], as->corank);
4626 /* Create the constructor for this array. */
4627 for (d = 0; d < as->corank; d++)
4628 gfc_constructor_append_expr (&e->value.constructor,
4629 bounds[d], &e->where);
4630 return e;
4632 else
4634 /* A DIM argument is specified. */
4635 if (dim->expr_type != EXPR_CONSTANT)
4636 return NULL;
4638 d = mpz_get_si (dim->value.integer);
4640 if (d < 1 || d > as->corank)
4642 gfc_error ("DIM argument at %L is out of bounds", &dim->where);
4643 return &gfc_bad_expr;
4646 return simplify_bound_dim (array, kind, d+as->rank, upper, as, ref, true);
4651 gfc_expr *
4652 gfc_simplify_lbound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
4654 return simplify_bound (array, dim, kind, 0);
4658 gfc_expr *
4659 gfc_simplify_lcobound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
4661 return simplify_cobound (array, dim, kind, 0);
4664 gfc_expr *
4665 gfc_simplify_leadz (gfc_expr *e)
4667 unsigned long lz, bs;
4668 int i;
4670 if (e->expr_type != EXPR_CONSTANT)
4671 return NULL;
4673 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
4674 bs = gfc_integer_kinds[i].bit_size;
4675 if (mpz_cmp_si (e->value.integer, 0) == 0)
4676 lz = bs;
4677 else if (mpz_cmp_si (e->value.integer, 0) < 0)
4678 lz = 0;
4679 else
4680 lz = bs - mpz_sizeinbase (e->value.integer, 2);
4682 return gfc_get_int_expr (gfc_default_integer_kind, &e->where, lz);
4686 /* Check for constant length of a substring. */
4688 static bool
4689 substring_has_constant_len (gfc_expr *e)
4691 gfc_ref *ref;
4692 HOST_WIDE_INT istart, iend, length;
4693 bool equal_length = false;
4695 if (e->ts.type != BT_CHARACTER)
4696 return false;
4698 for (ref = e->ref; ref; ref = ref->next)
4699 if (ref->type != REF_COMPONENT && ref->type != REF_ARRAY)
4700 break;
4702 if (!ref
4703 || ref->type != REF_SUBSTRING
4704 || !ref->u.ss.start
4705 || ref->u.ss.start->expr_type != EXPR_CONSTANT
4706 || !ref->u.ss.end
4707 || ref->u.ss.end->expr_type != EXPR_CONSTANT)
4708 return false;
4710 /* Basic checks on substring starting and ending indices. */
4711 if (!gfc_resolve_substring (ref, &equal_length))
4712 return false;
4714 istart = gfc_mpz_get_hwi (ref->u.ss.start->value.integer);
4715 iend = gfc_mpz_get_hwi (ref->u.ss.end->value.integer);
4717 if (istart <= iend)
4718 length = iend - istart + 1;
4719 else
4720 length = 0;
4722 /* Fix substring length. */
4723 e->value.character.length = length;
4725 return true;
4729 gfc_expr *
4730 gfc_simplify_len (gfc_expr *e, gfc_expr *kind)
4732 gfc_expr *result;
4733 int k = get_kind (BT_INTEGER, kind, "LEN", gfc_default_integer_kind);
4735 if (k == -1)
4736 return &gfc_bad_expr;
4738 if (e->expr_type == EXPR_CONSTANT
4739 || substring_has_constant_len (e))
4741 result = gfc_get_constant_expr (BT_INTEGER, k, &e->where);
4742 mpz_set_si (result->value.integer, e->value.character.length);
4743 return range_check (result, "LEN");
4745 else if (e->ts.u.cl != NULL && e->ts.u.cl->length != NULL
4746 && e->ts.u.cl->length->expr_type == EXPR_CONSTANT
4747 && e->ts.u.cl->length->ts.type == BT_INTEGER)
4749 result = gfc_get_constant_expr (BT_INTEGER, k, &e->where);
4750 mpz_set (result->value.integer, e->ts.u.cl->length->value.integer);
4751 return range_check (result, "LEN");
4753 else if (e->expr_type == EXPR_VARIABLE && e->ts.type == BT_CHARACTER
4754 && e->symtree->n.sym)
4756 if (e->symtree->n.sym->ts.type != BT_DERIVED
4757 && e->symtree->n.sym->assoc && e->symtree->n.sym->assoc->target
4758 && e->symtree->n.sym->assoc->target->ts.type == BT_DERIVED
4759 && e->symtree->n.sym->assoc->target->symtree->n.sym
4760 && UNLIMITED_POLY (e->symtree->n.sym->assoc->target->symtree->n.sym))
4761 /* The expression in assoc->target points to a ref to the _data
4762 component of the unlimited polymorphic entity. To get the _len
4763 component the last _data ref needs to be stripped and a ref to the
4764 _len component added. */
4765 return gfc_get_len_component (e->symtree->n.sym->assoc->target, k);
4766 else if (e->symtree->n.sym->ts.type == BT_DERIVED
4767 && e->ref && e->ref->type == REF_COMPONENT
4768 && e->ref->u.c.component->attr.pdt_string
4769 && e->ref->u.c.component->ts.type == BT_CHARACTER
4770 && e->ref->u.c.component->ts.u.cl->length)
4772 if (gfc_init_expr_flag)
4774 gfc_expr* tmp;
4775 tmp = gfc_pdt_find_component_copy_initializer (e->symtree->n.sym,
4776 e->ref->u.c
4777 .component->ts.u.cl
4778 ->length->symtree
4779 ->name);
4780 if (tmp)
4781 return tmp;
4783 else
4785 gfc_expr *len_expr = gfc_copy_expr (e);
4786 gfc_free_ref_list (len_expr->ref);
4787 len_expr->ref = NULL;
4788 gfc_find_component (len_expr->symtree->n.sym->ts.u.derived, e->ref
4789 ->u.c.component->ts.u.cl->length->symtree
4790 ->name,
4791 false, true, &len_expr->ref);
4792 len_expr->ts = len_expr->ref->u.c.component->ts;
4793 return len_expr;
4797 return NULL;
4801 gfc_expr *
4802 gfc_simplify_len_trim (gfc_expr *e, gfc_expr *kind)
4804 gfc_expr *result;
4805 size_t count, len, i;
4806 int k = get_kind (BT_INTEGER, kind, "LEN_TRIM", gfc_default_integer_kind);
4808 if (k == -1)
4809 return &gfc_bad_expr;
4811 /* If the expression is either an array element or section, an array
4812 parameter must be built so that the reference can be applied. Constant
4813 references should have already been simplified away. All other cases
4814 can proceed to translation, where kind conversion will occur silently. */
4815 if (e->expr_type == EXPR_VARIABLE
4816 && e->ts.type == BT_CHARACTER
4817 && e->symtree->n.sym->attr.flavor == FL_PARAMETER
4818 && e->ref && e->ref->type == REF_ARRAY
4819 && e->ref->u.ar.type != AR_FULL
4820 && e->symtree->n.sym->value)
4822 char name[2*GFC_MAX_SYMBOL_LEN + 12];
4823 gfc_namespace *ns = e->symtree->n.sym->ns;
4824 gfc_symtree *st;
4825 gfc_expr *expr;
4826 gfc_expr *p;
4827 gfc_constructor *c;
4828 int cnt = 0;
4830 sprintf (name, "_len_trim_%s_%s", e->symtree->n.sym->name,
4831 ns->proc_name->name);
4832 st = gfc_find_symtree (ns->sym_root, name);
4833 if (st)
4834 goto already_built;
4836 /* Recursively call this fcn to simplify the constructor elements. */
4837 expr = gfc_copy_expr (e->symtree->n.sym->value);
4838 expr->ts.type = BT_INTEGER;
4839 expr->ts.kind = k;
4840 expr->ts.u.cl = NULL;
4841 c = gfc_constructor_first (expr->value.constructor);
4842 for (; c; c = gfc_constructor_next (c))
4844 if (c->iterator)
4845 continue;
4847 if (c->expr && c->expr->ts.type == BT_CHARACTER)
4849 p = gfc_simplify_len_trim (c->expr, kind);
4850 if (p == NULL)
4851 goto clean_up;
4852 gfc_replace_expr (c->expr, p);
4853 cnt++;
4857 if (cnt)
4859 /* Build a new parameter to take the result. */
4860 st = gfc_new_symtree (&ns->sym_root, name);
4861 st->n.sym = gfc_new_symbol (st->name, ns);
4862 st->n.sym->value = expr;
4863 st->n.sym->ts = expr->ts;
4864 st->n.sym->attr.dimension = 1;
4865 st->n.sym->attr.save = SAVE_IMPLICIT;
4866 st->n.sym->attr.flavor = FL_PARAMETER;
4867 st->n.sym->as = gfc_copy_array_spec (e->symtree->n.sym->as);
4868 gfc_set_sym_referenced (st->n.sym);
4869 st->n.sym->refs++;
4870 gfc_commit_symbol (st->n.sym);
4872 already_built:
4873 /* Build a return expression. */
4874 expr = gfc_copy_expr (e);
4875 expr->ts = st->n.sym->ts;
4876 expr->symtree = st;
4877 gfc_expression_rank (expr);
4878 return expr;
4881 clean_up:
4882 gfc_free_expr (expr);
4883 return NULL;
4886 if (e->expr_type != EXPR_CONSTANT)
4887 return NULL;
4889 len = e->value.character.length;
4890 for (count = 0, i = 1; i <= len; i++)
4891 if (e->value.character.string[len - i] == ' ')
4892 count++;
4893 else
4894 break;
4896 result = gfc_get_int_expr (k, &e->where, len - count);
4897 return range_check (result, "LEN_TRIM");
4900 gfc_expr *
4901 gfc_simplify_lgamma (gfc_expr *x)
4903 gfc_expr *result;
4904 int sg;
4906 if (x->expr_type != EXPR_CONSTANT)
4907 return NULL;
4909 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
4910 mpfr_lgamma (result->value.real, &sg, x->value.real, GFC_RND_MODE);
4912 return range_check (result, "LGAMMA");
4916 gfc_expr *
4917 gfc_simplify_lge (gfc_expr *a, gfc_expr *b)
4919 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
4920 return NULL;
4922 return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
4923 gfc_compare_string (a, b) >= 0);
4927 gfc_expr *
4928 gfc_simplify_lgt (gfc_expr *a, gfc_expr *b)
4930 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
4931 return NULL;
4933 return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
4934 gfc_compare_string (a, b) > 0);
4938 gfc_expr *
4939 gfc_simplify_lle (gfc_expr *a, gfc_expr *b)
4941 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
4942 return NULL;
4944 return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
4945 gfc_compare_string (a, b) <= 0);
4949 gfc_expr *
4950 gfc_simplify_llt (gfc_expr *a, gfc_expr *b)
4952 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
4953 return NULL;
4955 return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
4956 gfc_compare_string (a, b) < 0);
4960 gfc_expr *
4961 gfc_simplify_log (gfc_expr *x)
4963 gfc_expr *result;
4965 if (x->expr_type != EXPR_CONSTANT)
4966 return NULL;
4968 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
4970 switch (x->ts.type)
4972 case BT_REAL:
4973 if (mpfr_sgn (x->value.real) <= 0)
4975 gfc_error ("Argument of LOG at %L cannot be less than or equal "
4976 "to zero", &x->where);
4977 gfc_free_expr (result);
4978 return &gfc_bad_expr;
4981 mpfr_log (result->value.real, x->value.real, GFC_RND_MODE);
4982 break;
4984 case BT_COMPLEX:
4985 if (mpfr_zero_p (mpc_realref (x->value.complex))
4986 && mpfr_zero_p (mpc_imagref (x->value.complex)))
4988 gfc_error ("Complex argument of LOG at %L cannot be zero",
4989 &x->where);
4990 gfc_free_expr (result);
4991 return &gfc_bad_expr;
4994 gfc_set_model_kind (x->ts.kind);
4995 mpc_log (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
4996 break;
4998 default:
4999 gfc_internal_error ("gfc_simplify_log: bad type");
5002 return range_check (result, "LOG");
5006 gfc_expr *
5007 gfc_simplify_log10 (gfc_expr *x)
5009 gfc_expr *result;
5011 if (x->expr_type != EXPR_CONSTANT)
5012 return NULL;
5014 if (mpfr_sgn (x->value.real) <= 0)
5016 gfc_error ("Argument of LOG10 at %L cannot be less than or equal "
5017 "to zero", &x->where);
5018 return &gfc_bad_expr;
5021 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
5022 mpfr_log10 (result->value.real, x->value.real, GFC_RND_MODE);
5024 return range_check (result, "LOG10");
5028 gfc_expr *
5029 gfc_simplify_logical (gfc_expr *e, gfc_expr *k)
5031 int kind;
5033 kind = get_kind (BT_LOGICAL, k, "LOGICAL", gfc_default_logical_kind);
5034 if (kind < 0)
5035 return &gfc_bad_expr;
5037 if (e->expr_type != EXPR_CONSTANT)
5038 return NULL;
5040 return gfc_get_logical_expr (kind, &e->where, e->value.logical);
5044 gfc_expr*
5045 gfc_simplify_matmul (gfc_expr *matrix_a, gfc_expr *matrix_b)
5047 gfc_expr *result;
5048 int row, result_rows, col, result_columns;
5049 int stride_a, offset_a, stride_b, offset_b;
5051 if (!is_constant_array_expr (matrix_a)
5052 || !is_constant_array_expr (matrix_b))
5053 return NULL;
5055 /* MATMUL should do mixed-mode arithmetic. Set the result type. */
5056 if (matrix_a->ts.type != matrix_b->ts.type)
5058 gfc_expr e;
5059 e.expr_type = EXPR_OP;
5060 gfc_clear_ts (&e.ts);
5061 e.value.op.op = INTRINSIC_NONE;
5062 e.value.op.op1 = matrix_a;
5063 e.value.op.op2 = matrix_b;
5064 gfc_type_convert_binary (&e, 1);
5065 result = gfc_get_array_expr (e.ts.type, e.ts.kind, &matrix_a->where);
5067 else
5069 result = gfc_get_array_expr (matrix_a->ts.type, matrix_a->ts.kind,
5070 &matrix_a->where);
5073 if (matrix_a->rank == 1 && matrix_b->rank == 2)
5075 result_rows = 1;
5076 result_columns = mpz_get_si (matrix_b->shape[1]);
5077 stride_a = 1;
5078 stride_b = mpz_get_si (matrix_b->shape[0]);
5080 result->rank = 1;
5081 result->shape = gfc_get_shape (result->rank);
5082 mpz_init_set_si (result->shape[0], result_columns);
5084 else if (matrix_a->rank == 2 && matrix_b->rank == 1)
5086 result_rows = mpz_get_si (matrix_a->shape[0]);
5087 result_columns = 1;
5088 stride_a = mpz_get_si (matrix_a->shape[0]);
5089 stride_b = 1;
5091 result->rank = 1;
5092 result->shape = gfc_get_shape (result->rank);
5093 mpz_init_set_si (result->shape[0], result_rows);
5095 else if (matrix_a->rank == 2 && matrix_b->rank == 2)
5097 result_rows = mpz_get_si (matrix_a->shape[0]);
5098 result_columns = mpz_get_si (matrix_b->shape[1]);
5099 stride_a = mpz_get_si (matrix_a->shape[0]);
5100 stride_b = mpz_get_si (matrix_b->shape[0]);
5102 result->rank = 2;
5103 result->shape = gfc_get_shape (result->rank);
5104 mpz_init_set_si (result->shape[0], result_rows);
5105 mpz_init_set_si (result->shape[1], result_columns);
5107 else
5108 gcc_unreachable();
5110 offset_b = 0;
5111 for (col = 0; col < result_columns; ++col)
5113 offset_a = 0;
5115 for (row = 0; row < result_rows; ++row)
5117 gfc_expr *e = compute_dot_product (matrix_a, stride_a, offset_a,
5118 matrix_b, 1, offset_b, false);
5119 gfc_constructor_append_expr (&result->value.constructor,
5120 e, NULL);
5122 offset_a += 1;
5125 offset_b += stride_b;
5128 return result;
5132 gfc_expr *
5133 gfc_simplify_maskr (gfc_expr *i, gfc_expr *kind_arg)
5135 gfc_expr *result;
5136 int kind, arg, k;
5138 if (i->expr_type != EXPR_CONSTANT)
5139 return NULL;
5141 kind = get_kind (BT_INTEGER, kind_arg, "MASKR", gfc_default_integer_kind);
5142 if (kind == -1)
5143 return &gfc_bad_expr;
5144 k = gfc_validate_kind (BT_INTEGER, kind, false);
5146 bool fail = gfc_extract_int (i, &arg);
5147 gcc_assert (!fail);
5149 if (!gfc_check_mask (i, kind_arg))
5150 return &gfc_bad_expr;
5152 result = gfc_get_constant_expr (BT_INTEGER, kind, &i->where);
5154 /* MASKR(n) = 2^n - 1 */
5155 mpz_set_ui (result->value.integer, 1);
5156 mpz_mul_2exp (result->value.integer, result->value.integer, arg);
5157 mpz_sub_ui (result->value.integer, result->value.integer, 1);
5159 gfc_convert_mpz_to_signed (result->value.integer, gfc_integer_kinds[k].bit_size);
5161 return result;
5165 gfc_expr *
5166 gfc_simplify_maskl (gfc_expr *i, gfc_expr *kind_arg)
5168 gfc_expr *result;
5169 int kind, arg, k;
5170 mpz_t z;
5172 if (i->expr_type != EXPR_CONSTANT)
5173 return NULL;
5175 kind = get_kind (BT_INTEGER, kind_arg, "MASKL", gfc_default_integer_kind);
5176 if (kind == -1)
5177 return &gfc_bad_expr;
5178 k = gfc_validate_kind (BT_INTEGER, kind, false);
5180 bool fail = gfc_extract_int (i, &arg);
5181 gcc_assert (!fail);
5183 if (!gfc_check_mask (i, kind_arg))
5184 return &gfc_bad_expr;
5186 result = gfc_get_constant_expr (BT_INTEGER, kind, &i->where);
5188 /* MASKL(n) = 2^bit_size - 2^(bit_size - n) */
5189 mpz_init_set_ui (z, 1);
5190 mpz_mul_2exp (z, z, gfc_integer_kinds[k].bit_size);
5191 mpz_set_ui (result->value.integer, 1);
5192 mpz_mul_2exp (result->value.integer, result->value.integer,
5193 gfc_integer_kinds[k].bit_size - arg);
5194 mpz_sub (result->value.integer, z, result->value.integer);
5195 mpz_clear (z);
5197 gfc_convert_mpz_to_signed (result->value.integer, gfc_integer_kinds[k].bit_size);
5199 return result;
5203 gfc_expr *
5204 gfc_simplify_merge (gfc_expr *tsource, gfc_expr *fsource, gfc_expr *mask)
5206 gfc_expr * result;
5207 gfc_constructor *tsource_ctor, *fsource_ctor, *mask_ctor;
5209 if (mask->expr_type == EXPR_CONSTANT)
5211 /* The standard requires evaluation of all function arguments.
5212 Simplify only when the other dropped argument (FSOURCE or TSOURCE)
5213 is a constant expression. */
5214 if (mask->value.logical)
5216 if (!gfc_is_constant_expr (fsource))
5217 return NULL;
5218 result = gfc_copy_expr (tsource);
5220 else
5222 if (!gfc_is_constant_expr (tsource))
5223 return NULL;
5224 result = gfc_copy_expr (fsource);
5227 /* Parenthesis is needed to get lower bounds of 1. */
5228 result = gfc_get_parentheses (result);
5229 gfc_simplify_expr (result, 1);
5230 return result;
5233 if (!mask->rank || !is_constant_array_expr (mask)
5234 || !is_constant_array_expr (tsource) || !is_constant_array_expr (fsource))
5235 return NULL;
5237 result = gfc_get_array_expr (tsource->ts.type, tsource->ts.kind,
5238 &tsource->where);
5239 if (tsource->ts.type == BT_DERIVED)
5240 result->ts.u.derived = tsource->ts.u.derived;
5241 else if (tsource->ts.type == BT_CHARACTER)
5242 result->ts.u.cl = tsource->ts.u.cl;
5244 tsource_ctor = gfc_constructor_first (tsource->value.constructor);
5245 fsource_ctor = gfc_constructor_first (fsource->value.constructor);
5246 mask_ctor = gfc_constructor_first (mask->value.constructor);
5248 while (mask_ctor)
5250 if (mask_ctor->expr->value.logical)
5251 gfc_constructor_append_expr (&result->value.constructor,
5252 gfc_copy_expr (tsource_ctor->expr),
5253 NULL);
5254 else
5255 gfc_constructor_append_expr (&result->value.constructor,
5256 gfc_copy_expr (fsource_ctor->expr),
5257 NULL);
5258 tsource_ctor = gfc_constructor_next (tsource_ctor);
5259 fsource_ctor = gfc_constructor_next (fsource_ctor);
5260 mask_ctor = gfc_constructor_next (mask_ctor);
5263 result->shape = gfc_get_shape (1);
5264 gfc_array_size (result, &result->shape[0]);
5266 return result;
5270 gfc_expr *
5271 gfc_simplify_merge_bits (gfc_expr *i, gfc_expr *j, gfc_expr *mask_expr)
5273 mpz_t arg1, arg2, mask;
5274 gfc_expr *result;
5276 if (i->expr_type != EXPR_CONSTANT || j->expr_type != EXPR_CONSTANT
5277 || mask_expr->expr_type != EXPR_CONSTANT)
5278 return NULL;
5280 result = gfc_get_constant_expr (i->ts.type, i->ts.kind, &i->where);
5282 /* Convert all argument to unsigned. */
5283 mpz_init_set (arg1, i->value.integer);
5284 mpz_init_set (arg2, j->value.integer);
5285 mpz_init_set (mask, mask_expr->value.integer);
5287 /* MERGE_BITS(I,J,MASK) = IOR (IAND (I, MASK), IAND (J, NOT (MASK))). */
5288 mpz_and (arg1, arg1, mask);
5289 mpz_com (mask, mask);
5290 mpz_and (arg2, arg2, mask);
5291 mpz_ior (result->value.integer, arg1, arg2);
5293 mpz_clear (arg1);
5294 mpz_clear (arg2);
5295 mpz_clear (mask);
5297 return result;
5301 /* Selects between current value and extremum for simplify_min_max
5302 and simplify_minval_maxval. */
5303 static int
5304 min_max_choose (gfc_expr *arg, gfc_expr *extremum, int sign, bool back_val)
5306 int ret;
5308 switch (arg->ts.type)
5310 case BT_INTEGER:
5311 case BT_UNSIGNED:
5312 if (extremum->ts.kind < arg->ts.kind)
5313 extremum->ts.kind = arg->ts.kind;
5314 ret = mpz_cmp (arg->value.integer,
5315 extremum->value.integer) * sign;
5316 if (ret > 0)
5317 mpz_set (extremum->value.integer, arg->value.integer);
5318 break;
5320 case BT_REAL:
5321 if (extremum->ts.kind < arg->ts.kind)
5322 extremum->ts.kind = arg->ts.kind;
5323 if (mpfr_nan_p (extremum->value.real))
5325 ret = 1;
5326 mpfr_set (extremum->value.real, arg->value.real, GFC_RND_MODE);
5328 else if (mpfr_nan_p (arg->value.real))
5329 ret = -1;
5330 else
5332 ret = mpfr_cmp (arg->value.real, extremum->value.real) * sign;
5333 if (ret > 0)
5334 mpfr_set (extremum->value.real, arg->value.real, GFC_RND_MODE);
5336 break;
5338 case BT_CHARACTER:
5339 #define LENGTH(x) ((x)->value.character.length)
5340 #define STRING(x) ((x)->value.character.string)
5341 if (LENGTH (extremum) < LENGTH(arg))
5343 gfc_char_t *tmp = STRING(extremum);
5345 STRING(extremum) = gfc_get_wide_string (LENGTH(arg) + 1);
5346 memcpy (STRING(extremum), tmp,
5347 LENGTH(extremum) * sizeof (gfc_char_t));
5348 gfc_wide_memset (&STRING(extremum)[LENGTH(extremum)], ' ',
5349 LENGTH(arg) - LENGTH(extremum));
5350 STRING(extremum)[LENGTH(arg)] = '\0'; /* For debugger */
5351 LENGTH(extremum) = LENGTH(arg);
5352 free (tmp);
5354 ret = gfc_compare_string (arg, extremum) * sign;
5355 if (ret > 0)
5357 free (STRING(extremum));
5358 STRING(extremum) = gfc_get_wide_string (LENGTH(extremum) + 1);
5359 memcpy (STRING(extremum), STRING(arg),
5360 LENGTH(arg) * sizeof (gfc_char_t));
5361 gfc_wide_memset (&STRING(extremum)[LENGTH(arg)], ' ',
5362 LENGTH(extremum) - LENGTH(arg));
5363 STRING(extremum)[LENGTH(extremum)] = '\0'; /* For debugger */
5365 #undef LENGTH
5366 #undef STRING
5367 break;
5369 default:
5370 gfc_internal_error ("simplify_min_max(): Bad type in arglist");
5372 if (back_val && ret == 0)
5373 ret = 1;
5375 return ret;
5379 /* This function is special since MAX() can take any number of
5380 arguments. The simplified expression is a rewritten version of the
5381 argument list containing at most one constant element. Other
5382 constant elements are deleted. Because the argument list has
5383 already been checked, this function always succeeds. sign is 1 for
5384 MAX(), -1 for MIN(). */
5386 static gfc_expr *
5387 simplify_min_max (gfc_expr *expr, int sign)
5389 int tmp1, tmp2;
5390 gfc_actual_arglist *arg, *last, *extremum;
5391 gfc_expr *tmp, *ret;
5392 const char *fname;
5394 last = NULL;
5395 extremum = NULL;
5397 arg = expr->value.function.actual;
5399 for (; arg; last = arg, arg = arg->next)
5401 if (arg->expr->expr_type != EXPR_CONSTANT)
5402 continue;
5404 if (extremum == NULL)
5406 extremum = arg;
5407 continue;
5410 min_max_choose (arg->expr, extremum->expr, sign);
5412 /* Delete the extra constant argument. */
5413 last->next = arg->next;
5415 arg->next = NULL;
5416 gfc_free_actual_arglist (arg);
5417 arg = last;
5420 /* If there is one value left, replace the function call with the
5421 expression. */
5422 if (expr->value.function.actual->next != NULL)
5423 return NULL;
5425 /* Handle special cases of specific functions (min|max)1 and
5426 a(min|max)0. */
5428 tmp = expr->value.function.actual->expr;
5429 fname = expr->value.function.isym->name;
5431 if ((tmp->ts.type != BT_INTEGER || tmp->ts.kind != gfc_integer_4_kind)
5432 && (strcmp (fname, "min1") == 0 || strcmp (fname, "max1") == 0))
5434 /* Explicit conversion, turn off -Wconversion and -Wconversion-extra
5435 warnings. */
5436 tmp1 = warn_conversion;
5437 tmp2 = warn_conversion_extra;
5438 warn_conversion = warn_conversion_extra = 0;
5440 ret = gfc_convert_constant (tmp, BT_INTEGER, gfc_integer_4_kind);
5442 warn_conversion = tmp1;
5443 warn_conversion_extra = tmp2;
5445 else if ((tmp->ts.type != BT_REAL || tmp->ts.kind != gfc_real_4_kind)
5446 && (strcmp (fname, "amin0") == 0 || strcmp (fname, "amax0") == 0))
5448 ret = gfc_convert_constant (tmp, BT_REAL, gfc_real_4_kind);
5450 else
5451 ret = gfc_copy_expr (tmp);
5453 return ret;
5458 gfc_expr *
5459 gfc_simplify_min (gfc_expr *e)
5461 return simplify_min_max (e, -1);
5465 gfc_expr *
5466 gfc_simplify_max (gfc_expr *e)
5468 return simplify_min_max (e, 1);
5471 /* Helper function for gfc_simplify_minval. */
5473 static gfc_expr *
5474 gfc_min (gfc_expr *op1, gfc_expr *op2)
5476 min_max_choose (op1, op2, -1);
5477 gfc_free_expr (op1);
5478 return op2;
5481 /* Simplify minval for constant arrays. */
5483 gfc_expr *
5484 gfc_simplify_minval (gfc_expr *array, gfc_expr* dim, gfc_expr *mask)
5486 return simplify_transformation (array, dim, mask, INT_MAX, gfc_min);
5489 /* Helper function for gfc_simplify_maxval. */
5491 static gfc_expr *
5492 gfc_max (gfc_expr *op1, gfc_expr *op2)
5494 min_max_choose (op1, op2, 1);
5495 gfc_free_expr (op1);
5496 return op2;
5500 /* Simplify maxval for constant arrays. */
5502 gfc_expr *
5503 gfc_simplify_maxval (gfc_expr *array, gfc_expr* dim, gfc_expr *mask)
5505 return simplify_transformation (array, dim, mask, INT_MIN, gfc_max);
5509 /* Transform minloc or maxloc of an array, according to MASK,
5510 to the scalar result. This code is mostly identical to
5511 simplify_transformation_to_scalar. */
5513 static gfc_expr *
5514 simplify_minmaxloc_to_scalar (gfc_expr *result, gfc_expr *array, gfc_expr *mask,
5515 gfc_expr *extremum, int sign, bool back_val)
5517 gfc_expr *a, *m;
5518 gfc_constructor *array_ctor, *mask_ctor;
5519 mpz_t count;
5521 mpz_set_si (result->value.integer, 0);
5524 /* Shortcut for constant .FALSE. MASK. */
5525 if (mask
5526 && mask->expr_type == EXPR_CONSTANT
5527 && !mask->value.logical)
5528 return result;
5530 array_ctor = gfc_constructor_first (array->value.constructor);
5531 if (mask && mask->expr_type == EXPR_ARRAY)
5532 mask_ctor = gfc_constructor_first (mask->value.constructor);
5533 else
5534 mask_ctor = NULL;
5536 mpz_init_set_si (count, 0);
5537 while (array_ctor)
5539 mpz_add_ui (count, count, 1);
5540 a = array_ctor->expr;
5541 array_ctor = gfc_constructor_next (array_ctor);
5542 /* A constant MASK equals .TRUE. here and can be ignored. */
5543 if (mask_ctor)
5545 m = mask_ctor->expr;
5546 mask_ctor = gfc_constructor_next (mask_ctor);
5547 if (!m->value.logical)
5548 continue;
5550 if (min_max_choose (a, extremum, sign, back_val) > 0)
5551 mpz_set (result->value.integer, count);
5553 mpz_clear (count);
5554 gfc_free_expr (extremum);
5555 return result;
5558 /* Simplify minloc / maxloc in the absence of a dim argument. */
5560 static gfc_expr *
5561 simplify_minmaxloc_nodim (gfc_expr *result, gfc_expr *extremum,
5562 gfc_expr *array, gfc_expr *mask, int sign,
5563 bool back_val)
5565 ssize_t res[GFC_MAX_DIMENSIONS];
5566 int i, n;
5567 gfc_constructor *result_ctor, *array_ctor, *mask_ctor;
5568 ssize_t count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS],
5569 sstride[GFC_MAX_DIMENSIONS];
5570 gfc_expr *a, *m;
5571 bool continue_loop;
5572 bool ma;
5574 for (i = 0; i<array->rank; i++)
5575 res[i] = -1;
5577 /* Shortcut for constant .FALSE. MASK. */
5578 if (mask
5579 && mask->expr_type == EXPR_CONSTANT
5580 && !mask->value.logical)
5581 goto finish;
5583 if (array->shape == NULL)
5584 goto finish;
5586 for (i = 0; i < array->rank; i++)
5588 count[i] = 0;
5589 sstride[i] = (i == 0) ? 1 : sstride[i-1] * mpz_get_si (array->shape[i-1]);
5590 extent[i] = mpz_get_si (array->shape[i]);
5591 if (extent[i] <= 0)
5592 goto finish;
5595 continue_loop = true;
5596 array_ctor = gfc_constructor_first (array->value.constructor);
5597 if (mask && mask->rank > 0)
5598 mask_ctor = gfc_constructor_first (mask->value.constructor);
5599 else
5600 mask_ctor = NULL;
5602 /* Loop over the array elements (and mask), keeping track of
5603 the indices to return. */
5604 while (continue_loop)
5608 a = array_ctor->expr;
5609 if (mask_ctor)
5611 m = mask_ctor->expr;
5612 ma = m->value.logical;
5613 mask_ctor = gfc_constructor_next (mask_ctor);
5615 else
5616 ma = true;
5618 if (ma && min_max_choose (a, extremum, sign, back_val) > 0)
5620 for (i = 0; i<array->rank; i++)
5621 res[i] = count[i];
5623 array_ctor = gfc_constructor_next (array_ctor);
5624 count[0] ++;
5625 } while (count[0] != extent[0]);
5626 n = 0;
5629 /* When we get to the end of a dimension, reset it and increment
5630 the next dimension. */
5631 count[n] = 0;
5632 n++;
5633 if (n >= array->rank)
5635 continue_loop = false;
5636 break;
5638 else
5639 count[n] ++;
5640 } while (count[n] == extent[n]);
5643 finish:
5644 gfc_free_expr (extremum);
5645 result_ctor = gfc_constructor_first (result->value.constructor);
5646 for (i = 0; i<array->rank; i++)
5648 gfc_expr *r_expr;
5649 r_expr = result_ctor->expr;
5650 mpz_set_si (r_expr->value.integer, res[i] + 1);
5651 result_ctor = gfc_constructor_next (result_ctor);
5653 return result;
5656 /* Helper function for gfc_simplify_minmaxloc - build an array
5657 expression with n elements. */
5659 static gfc_expr *
5660 new_array (bt type, int kind, int n, locus *where)
5662 gfc_expr *result;
5663 int i;
5665 result = gfc_get_array_expr (type, kind, where);
5666 result->rank = 1;
5667 result->shape = gfc_get_shape(1);
5668 mpz_init_set_si (result->shape[0], n);
5669 for (i = 0; i < n; i++)
5671 gfc_constructor_append_expr (&result->value.constructor,
5672 gfc_get_constant_expr (type, kind, where),
5673 NULL);
5676 return result;
5679 /* Simplify minloc and maxloc. This code is mostly identical to
5680 simplify_transformation_to_array. */
5682 static gfc_expr *
5683 simplify_minmaxloc_to_array (gfc_expr *result, gfc_expr *array,
5684 gfc_expr *dim, gfc_expr *mask,
5685 gfc_expr *extremum, int sign, bool back_val)
5687 mpz_t size;
5688 int done, i, n, arraysize, resultsize, dim_index, dim_extent, dim_stride;
5689 gfc_expr **arrayvec, **resultvec, **base, **src, **dest;
5690 gfc_constructor *array_ctor, *mask_ctor, *result_ctor;
5692 int count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS],
5693 sstride[GFC_MAX_DIMENSIONS], dstride[GFC_MAX_DIMENSIONS],
5694 tmpstride[GFC_MAX_DIMENSIONS];
5696 /* Shortcut for constant .FALSE. MASK. */
5697 if (mask
5698 && mask->expr_type == EXPR_CONSTANT
5699 && !mask->value.logical)
5700 return result;
5702 /* Build an indexed table for array element expressions to minimize
5703 linked-list traversal. Masked elements are set to NULL. */
5704 gfc_array_size (array, &size);
5705 arraysize = mpz_get_ui (size);
5706 mpz_clear (size);
5708 arrayvec = XCNEWVEC (gfc_expr*, arraysize);
5710 array_ctor = gfc_constructor_first (array->value.constructor);
5711 mask_ctor = NULL;
5712 if (mask && mask->expr_type == EXPR_ARRAY)
5713 mask_ctor = gfc_constructor_first (mask->value.constructor);
5715 for (i = 0; i < arraysize; ++i)
5717 arrayvec[i] = array_ctor->expr;
5718 array_ctor = gfc_constructor_next (array_ctor);
5720 if (mask_ctor)
5722 if (!mask_ctor->expr->value.logical)
5723 arrayvec[i] = NULL;
5725 mask_ctor = gfc_constructor_next (mask_ctor);
5729 /* Same for the result expression. */
5730 gfc_array_size (result, &size);
5731 resultsize = mpz_get_ui (size);
5732 mpz_clear (size);
5734 resultvec = XCNEWVEC (gfc_expr*, resultsize);
5735 result_ctor = gfc_constructor_first (result->value.constructor);
5736 for (i = 0; i < resultsize; ++i)
5738 resultvec[i] = result_ctor->expr;
5739 result_ctor = gfc_constructor_next (result_ctor);
5742 gfc_extract_int (dim, &dim_index);
5743 dim_index -= 1; /* zero-base index */
5744 dim_extent = 0;
5745 dim_stride = 0;
5747 for (i = 0, n = 0; i < array->rank; ++i)
5749 count[i] = 0;
5750 tmpstride[i] = (i == 0) ? 1 : tmpstride[i-1] * mpz_get_si (array->shape[i-1]);
5751 if (i == dim_index)
5753 dim_extent = mpz_get_si (array->shape[i]);
5754 dim_stride = tmpstride[i];
5755 continue;
5758 extent[n] = mpz_get_si (array->shape[i]);
5759 sstride[n] = tmpstride[i];
5760 dstride[n] = (n == 0) ? 1 : dstride[n-1] * extent[n-1];
5761 n += 1;
5764 done = resultsize <= 0;
5765 base = arrayvec;
5766 dest = resultvec;
5767 while (!done)
5769 gfc_expr *ex;
5770 ex = gfc_copy_expr (extremum);
5771 for (src = base, n = 0; n < dim_extent; src += dim_stride, ++n)
5773 if (*src && min_max_choose (*src, ex, sign, back_val) > 0)
5774 mpz_set_si ((*dest)->value.integer, n + 1);
5777 count[0]++;
5778 base += sstride[0];
5779 dest += dstride[0];
5780 gfc_free_expr (ex);
5782 n = 0;
5783 while (!done && count[n] == extent[n])
5785 count[n] = 0;
5786 base -= sstride[n] * extent[n];
5787 dest -= dstride[n] * extent[n];
5789 n++;
5790 if (n < result->rank)
5792 /* If the nested loop is unrolled GFC_MAX_DIMENSIONS
5793 times, we'd warn for the last iteration, because the
5794 array index will have already been incremented to the
5795 array sizes, and we can't tell that this must make
5796 the test against result->rank false, because ranks
5797 must not exceed GFC_MAX_DIMENSIONS. */
5798 GCC_DIAGNOSTIC_PUSH_IGNORED (-Warray-bounds)
5799 count[n]++;
5800 base += sstride[n];
5801 dest += dstride[n];
5802 GCC_DIAGNOSTIC_POP
5804 else
5805 done = true;
5809 /* Place updated expression in result constructor. */
5810 result_ctor = gfc_constructor_first (result->value.constructor);
5811 for (i = 0; i < resultsize; ++i)
5813 result_ctor->expr = resultvec[i];
5814 result_ctor = gfc_constructor_next (result_ctor);
5817 free (arrayvec);
5818 free (resultvec);
5819 free (extremum);
5820 return result;
5823 /* Simplify minloc and maxloc for constant arrays. */
5825 static gfc_expr *
5826 gfc_simplify_minmaxloc (gfc_expr *array, gfc_expr *dim, gfc_expr *mask,
5827 gfc_expr *kind, gfc_expr *back, int sign)
5829 gfc_expr *result;
5830 gfc_expr *extremum;
5831 int ikind;
5832 int init_val;
5833 bool back_val = false;
5835 if (!is_constant_array_expr (array)
5836 || !gfc_is_constant_expr (dim))
5837 return NULL;
5839 if (mask
5840 && !is_constant_array_expr (mask)
5841 && mask->expr_type != EXPR_CONSTANT)
5842 return NULL;
5844 if (kind)
5846 if (gfc_extract_int (kind, &ikind, -1))
5847 return NULL;
5849 else
5850 ikind = gfc_default_integer_kind;
5852 if (back)
5854 if (back->expr_type != EXPR_CONSTANT)
5855 return NULL;
5857 back_val = back->value.logical;
5860 if (sign < 0)
5861 init_val = INT_MAX;
5862 else if (sign > 0)
5863 init_val = INT_MIN;
5864 else
5865 gcc_unreachable();
5867 extremum = gfc_get_constant_expr (array->ts.type, array->ts.kind, &array->where);
5868 init_result_expr (extremum, init_val, array);
5870 if (dim)
5872 result = transformational_result (array, dim, BT_INTEGER,
5873 ikind, &array->where);
5874 init_result_expr (result, 0, array);
5876 if (array->rank == 1)
5877 return simplify_minmaxloc_to_scalar (result, array, mask, extremum,
5878 sign, back_val);
5879 else
5880 return simplify_minmaxloc_to_array (result, array, dim, mask, extremum,
5881 sign, back_val);
5883 else
5885 result = new_array (BT_INTEGER, ikind, array->rank, &array->where);
5886 return simplify_minmaxloc_nodim (result, extremum, array, mask,
5887 sign, back_val);
5891 gfc_expr *
5892 gfc_simplify_minloc (gfc_expr *array, gfc_expr *dim, gfc_expr *mask, gfc_expr *kind,
5893 gfc_expr *back)
5895 return gfc_simplify_minmaxloc (array, dim, mask, kind, back, -1);
5898 gfc_expr *
5899 gfc_simplify_maxloc (gfc_expr *array, gfc_expr *dim, gfc_expr *mask, gfc_expr *kind,
5900 gfc_expr *back)
5902 return gfc_simplify_minmaxloc (array, dim, mask, kind, back, 1);
5905 /* Simplify findloc to scalar. Similar to
5906 simplify_minmaxloc_to_scalar. */
5908 static gfc_expr *
5909 simplify_findloc_to_scalar (gfc_expr *result, gfc_expr *array, gfc_expr *value,
5910 gfc_expr *mask, int back_val)
5912 gfc_expr *a, *m;
5913 gfc_constructor *array_ctor, *mask_ctor;
5914 mpz_t count;
5916 mpz_set_si (result->value.integer, 0);
5918 /* Shortcut for constant .FALSE. MASK. */
5919 if (mask
5920 && mask->expr_type == EXPR_CONSTANT
5921 && !mask->value.logical)
5922 return result;
5924 array_ctor = gfc_constructor_first (array->value.constructor);
5925 if (mask && mask->expr_type == EXPR_ARRAY)
5926 mask_ctor = gfc_constructor_first (mask->value.constructor);
5927 else
5928 mask_ctor = NULL;
5930 mpz_init_set_si (count, 0);
5931 while (array_ctor)
5933 mpz_add_ui (count, count, 1);
5934 a = array_ctor->expr;
5935 array_ctor = gfc_constructor_next (array_ctor);
5936 /* A constant MASK equals .TRUE. here and can be ignored. */
5937 if (mask_ctor)
5939 m = mask_ctor->expr;
5940 mask_ctor = gfc_constructor_next (mask_ctor);
5941 if (!m->value.logical)
5942 continue;
5944 if (gfc_compare_expr (a, value, INTRINSIC_EQ) == 0)
5946 /* We have a match. If BACK is true, continue so we find
5947 the last one. */
5948 mpz_set (result->value.integer, count);
5949 if (!back_val)
5950 break;
5953 mpz_clear (count);
5954 return result;
5957 /* Simplify findloc in the absence of a dim argument. Similar to
5958 simplify_minmaxloc_nodim. */
5960 static gfc_expr *
5961 simplify_findloc_nodim (gfc_expr *result, gfc_expr *value, gfc_expr *array,
5962 gfc_expr *mask, bool back_val)
5964 ssize_t res[GFC_MAX_DIMENSIONS];
5965 int i, n;
5966 gfc_constructor *result_ctor, *array_ctor, *mask_ctor;
5967 ssize_t count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS],
5968 sstride[GFC_MAX_DIMENSIONS];
5969 gfc_expr *a, *m;
5970 bool continue_loop;
5971 bool ma;
5973 for (i = 0; i < array->rank; i++)
5974 res[i] = -1;
5976 /* Shortcut for constant .FALSE. MASK. */
5977 if (mask
5978 && mask->expr_type == EXPR_CONSTANT
5979 && !mask->value.logical)
5980 goto finish;
5982 for (i = 0; i < array->rank; i++)
5984 count[i] = 0;
5985 sstride[i] = (i == 0) ? 1 : sstride[i-1] * mpz_get_si (array->shape[i-1]);
5986 extent[i] = mpz_get_si (array->shape[i]);
5987 if (extent[i] <= 0)
5988 goto finish;
5991 continue_loop = true;
5992 array_ctor = gfc_constructor_first (array->value.constructor);
5993 if (mask && mask->rank > 0)
5994 mask_ctor = gfc_constructor_first (mask->value.constructor);
5995 else
5996 mask_ctor = NULL;
5998 /* Loop over the array elements (and mask), keeping track of
5999 the indices to return. */
6000 while (continue_loop)
6004 a = array_ctor->expr;
6005 if (mask_ctor)
6007 m = mask_ctor->expr;
6008 ma = m->value.logical;
6009 mask_ctor = gfc_constructor_next (mask_ctor);
6011 else
6012 ma = true;
6014 if (ma && gfc_compare_expr (a, value, INTRINSIC_EQ) == 0)
6016 for (i = 0; i < array->rank; i++)
6017 res[i] = count[i];
6018 if (!back_val)
6019 goto finish;
6021 array_ctor = gfc_constructor_next (array_ctor);
6022 count[0] ++;
6023 } while (count[0] != extent[0]);
6024 n = 0;
6027 /* When we get to the end of a dimension, reset it and increment
6028 the next dimension. */
6029 count[n] = 0;
6030 n++;
6031 if (n >= array->rank)
6033 continue_loop = false;
6034 break;
6036 else
6037 count[n] ++;
6038 } while (count[n] == extent[n]);
6041 finish:
6042 result_ctor = gfc_constructor_first (result->value.constructor);
6043 for (i = 0; i < array->rank; i++)
6045 gfc_expr *r_expr;
6046 r_expr = result_ctor->expr;
6047 mpz_set_si (r_expr->value.integer, res[i] + 1);
6048 result_ctor = gfc_constructor_next (result_ctor);
6050 return result;
6054 /* Simplify findloc to an array. Similar to
6055 simplify_minmaxloc_to_array. */
6057 static gfc_expr *
6058 simplify_findloc_to_array (gfc_expr *result, gfc_expr *array, gfc_expr *value,
6059 gfc_expr *dim, gfc_expr *mask, bool back_val)
6061 mpz_t size;
6062 int done, i, n, arraysize, resultsize, dim_index, dim_extent, dim_stride;
6063 gfc_expr **arrayvec, **resultvec, **base, **src, **dest;
6064 gfc_constructor *array_ctor, *mask_ctor, *result_ctor;
6066 int count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS],
6067 sstride[GFC_MAX_DIMENSIONS], dstride[GFC_MAX_DIMENSIONS],
6068 tmpstride[GFC_MAX_DIMENSIONS];
6070 /* Shortcut for constant .FALSE. MASK. */
6071 if (mask
6072 && mask->expr_type == EXPR_CONSTANT
6073 && !mask->value.logical)
6074 return result;
6076 /* Build an indexed table for array element expressions to minimize
6077 linked-list traversal. Masked elements are set to NULL. */
6078 gfc_array_size (array, &size);
6079 arraysize = mpz_get_ui (size);
6080 mpz_clear (size);
6082 arrayvec = XCNEWVEC (gfc_expr*, arraysize);
6084 array_ctor = gfc_constructor_first (array->value.constructor);
6085 mask_ctor = NULL;
6086 if (mask && mask->expr_type == EXPR_ARRAY)
6087 mask_ctor = gfc_constructor_first (mask->value.constructor);
6089 for (i = 0; i < arraysize; ++i)
6091 arrayvec[i] = array_ctor->expr;
6092 array_ctor = gfc_constructor_next (array_ctor);
6094 if (mask_ctor)
6096 if (!mask_ctor->expr->value.logical)
6097 arrayvec[i] = NULL;
6099 mask_ctor = gfc_constructor_next (mask_ctor);
6103 /* Same for the result expression. */
6104 gfc_array_size (result, &size);
6105 resultsize = mpz_get_ui (size);
6106 mpz_clear (size);
6108 resultvec = XCNEWVEC (gfc_expr*, resultsize);
6109 result_ctor = gfc_constructor_first (result->value.constructor);
6110 for (i = 0; i < resultsize; ++i)
6112 resultvec[i] = result_ctor->expr;
6113 result_ctor = gfc_constructor_next (result_ctor);
6116 gfc_extract_int (dim, &dim_index);
6118 dim_index -= 1; /* Zero-base index. */
6119 dim_extent = 0;
6120 dim_stride = 0;
6122 for (i = 0, n = 0; i < array->rank; ++i)
6124 count[i] = 0;
6125 tmpstride[i] = (i == 0) ? 1 : tmpstride[i-1] * mpz_get_si (array->shape[i-1]);
6126 if (i == dim_index)
6128 dim_extent = mpz_get_si (array->shape[i]);
6129 dim_stride = tmpstride[i];
6130 continue;
6133 extent[n] = mpz_get_si (array->shape[i]);
6134 sstride[n] = tmpstride[i];
6135 dstride[n] = (n == 0) ? 1 : dstride[n-1] * extent[n-1];
6136 n += 1;
6139 done = resultsize <= 0;
6140 base = arrayvec;
6141 dest = resultvec;
6142 while (!done)
6144 for (src = base, n = 0; n < dim_extent; src += dim_stride, ++n)
6146 if (*src && gfc_compare_expr (*src, value, INTRINSIC_EQ) == 0)
6148 mpz_set_si ((*dest)->value.integer, n + 1);
6149 if (!back_val)
6150 break;
6154 count[0]++;
6155 base += sstride[0];
6156 dest += dstride[0];
6158 n = 0;
6159 while (!done && count[n] == extent[n])
6161 count[n] = 0;
6162 base -= sstride[n] * extent[n];
6163 dest -= dstride[n] * extent[n];
6165 n++;
6166 if (n < result->rank)
6168 /* If the nested loop is unrolled GFC_MAX_DIMENSIONS
6169 times, we'd warn for the last iteration, because the
6170 array index will have already been incremented to the
6171 array sizes, and we can't tell that this must make
6172 the test against result->rank false, because ranks
6173 must not exceed GFC_MAX_DIMENSIONS. */
6174 GCC_DIAGNOSTIC_PUSH_IGNORED (-Warray-bounds)
6175 count[n]++;
6176 base += sstride[n];
6177 dest += dstride[n];
6178 GCC_DIAGNOSTIC_POP
6180 else
6181 done = true;
6185 /* Place updated expression in result constructor. */
6186 result_ctor = gfc_constructor_first (result->value.constructor);
6187 for (i = 0; i < resultsize; ++i)
6189 result_ctor->expr = resultvec[i];
6190 result_ctor = gfc_constructor_next (result_ctor);
6193 free (arrayvec);
6194 free (resultvec);
6195 return result;
6198 /* Simplify findloc. */
6200 gfc_expr *
6201 gfc_simplify_findloc (gfc_expr *array, gfc_expr *value, gfc_expr *dim,
6202 gfc_expr *mask, gfc_expr *kind, gfc_expr *back)
6204 gfc_expr *result;
6205 int ikind;
6206 bool back_val = false;
6208 if (!is_constant_array_expr (array)
6209 || array->shape == NULL
6210 || !gfc_is_constant_expr (dim))
6211 return NULL;
6213 if (! gfc_is_constant_expr (value))
6214 return 0;
6216 if (mask
6217 && !is_constant_array_expr (mask)
6218 && mask->expr_type != EXPR_CONSTANT)
6219 return NULL;
6221 if (kind)
6223 if (gfc_extract_int (kind, &ikind, -1))
6224 return NULL;
6226 else
6227 ikind = gfc_default_integer_kind;
6229 if (back)
6231 if (back->expr_type != EXPR_CONSTANT)
6232 return NULL;
6234 back_val = back->value.logical;
6237 if (dim)
6239 result = transformational_result (array, dim, BT_INTEGER,
6240 ikind, &array->where);
6241 init_result_expr (result, 0, array);
6243 if (array->rank == 1)
6244 return simplify_findloc_to_scalar (result, array, value, mask,
6245 back_val);
6246 else
6247 return simplify_findloc_to_array (result, array, value, dim, mask,
6248 back_val);
6250 else
6252 result = new_array (BT_INTEGER, ikind, array->rank, &array->where);
6253 return simplify_findloc_nodim (result, value, array, mask, back_val);
6255 return NULL;
6258 gfc_expr *
6259 gfc_simplify_maxexponent (gfc_expr *x)
6261 int i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
6262 return gfc_get_int_expr (gfc_default_integer_kind, &x->where,
6263 gfc_real_kinds[i].max_exponent);
6267 gfc_expr *
6268 gfc_simplify_minexponent (gfc_expr *x)
6270 int i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
6271 return gfc_get_int_expr (gfc_default_integer_kind, &x->where,
6272 gfc_real_kinds[i].min_exponent);
6276 gfc_expr *
6277 gfc_simplify_mod (gfc_expr *a, gfc_expr *p)
6279 gfc_expr *result;
6280 int kind;
6282 /* First check p. */
6283 if (p->expr_type != EXPR_CONSTANT)
6284 return NULL;
6286 /* p shall not be 0. */
6287 switch (p->ts.type)
6289 case BT_INTEGER:
6290 case BT_UNSIGNED:
6291 if (mpz_cmp_ui (p->value.integer, 0) == 0)
6293 gfc_error ("Argument %qs of MOD at %L shall not be zero",
6294 "P", &p->where);
6295 return &gfc_bad_expr;
6297 break;
6298 case BT_REAL:
6299 if (mpfr_cmp_ui (p->value.real, 0) == 0)
6301 gfc_error ("Argument %qs of MOD at %L shall not be zero",
6302 "P", &p->where);
6303 return &gfc_bad_expr;
6305 break;
6306 default:
6307 gfc_internal_error ("gfc_simplify_mod(): Bad arguments");
6310 if (a->expr_type != EXPR_CONSTANT)
6311 return NULL;
6313 kind = a->ts.kind > p->ts.kind ? a->ts.kind : p->ts.kind;
6314 result = gfc_get_constant_expr (a->ts.type, kind, &a->where);
6316 if (a->ts.type == BT_INTEGER || a->ts.type == BT_UNSIGNED)
6317 mpz_tdiv_r (result->value.integer, a->value.integer, p->value.integer);
6318 else
6320 gfc_set_model_kind (kind);
6321 mpfr_fmod (result->value.real, a->value.real, p->value.real,
6322 GFC_RND_MODE);
6325 return range_check (result, "MOD");
6329 gfc_expr *
6330 gfc_simplify_modulo (gfc_expr *a, gfc_expr *p)
6332 gfc_expr *result;
6333 int kind;
6335 /* First check p. */
6336 if (p->expr_type != EXPR_CONSTANT)
6337 return NULL;
6339 /* p shall not be 0. */
6340 switch (p->ts.type)
6342 case BT_INTEGER:
6343 case BT_UNSIGNED:
6344 if (mpz_cmp_ui (p->value.integer, 0) == 0)
6346 gfc_error ("Argument %qs of MODULO at %L shall not be zero",
6347 "P", &p->where);
6348 return &gfc_bad_expr;
6350 break;
6351 case BT_REAL:
6352 if (mpfr_cmp_ui (p->value.real, 0) == 0)
6354 gfc_error ("Argument %qs of MODULO at %L shall not be zero",
6355 "P", &p->where);
6356 return &gfc_bad_expr;
6358 break;
6359 default:
6360 gfc_internal_error ("gfc_simplify_modulo(): Bad arguments");
6363 if (a->expr_type != EXPR_CONSTANT)
6364 return NULL;
6366 kind = a->ts.kind > p->ts.kind ? a->ts.kind : p->ts.kind;
6367 result = gfc_get_constant_expr (a->ts.type, kind, &a->where);
6369 if (a->ts.type == BT_INTEGER || a->ts.type == BT_UNSIGNED)
6370 mpz_fdiv_r (result->value.integer, a->value.integer, p->value.integer);
6371 else
6373 gfc_set_model_kind (kind);
6374 mpfr_fmod (result->value.real, a->value.real, p->value.real,
6375 GFC_RND_MODE);
6376 if (mpfr_cmp_ui (result->value.real, 0) != 0)
6378 if (mpfr_signbit (a->value.real) != mpfr_signbit (p->value.real))
6379 mpfr_add (result->value.real, result->value.real, p->value.real,
6380 GFC_RND_MODE);
6382 else
6383 mpfr_copysign (result->value.real, result->value.real,
6384 p->value.real, GFC_RND_MODE);
6387 return range_check (result, "MODULO");
6391 gfc_expr *
6392 gfc_simplify_nearest (gfc_expr *x, gfc_expr *s)
6394 gfc_expr *result;
6395 mpfr_exp_t emin, emax;
6396 int kind;
6398 if (x->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
6399 return NULL;
6401 result = gfc_copy_expr (x);
6403 /* Save current values of emin and emax. */
6404 emin = mpfr_get_emin ();
6405 emax = mpfr_get_emax ();
6407 /* Set emin and emax for the current model number. */
6408 kind = gfc_validate_kind (BT_REAL, x->ts.kind, 0);
6409 mpfr_set_emin ((mpfr_exp_t) gfc_real_kinds[kind].min_exponent -
6410 mpfr_get_prec(result->value.real) + 1);
6411 mpfr_set_emax ((mpfr_exp_t) gfc_real_kinds[kind].max_exponent);
6412 mpfr_check_range (result->value.real, 0, MPFR_RNDU);
6414 if (mpfr_sgn (s->value.real) > 0)
6416 mpfr_nextabove (result->value.real);
6417 mpfr_subnormalize (result->value.real, 0, MPFR_RNDU);
6419 else
6421 mpfr_nextbelow (result->value.real);
6422 mpfr_subnormalize (result->value.real, 0, MPFR_RNDD);
6425 mpfr_set_emin (emin);
6426 mpfr_set_emax (emax);
6428 /* Only NaN can occur. Do not use range check as it gives an
6429 error for denormal numbers. */
6430 if (mpfr_nan_p (result->value.real) && flag_range_check)
6432 gfc_error ("Result of NEAREST is NaN at %L", &result->where);
6433 gfc_free_expr (result);
6434 return &gfc_bad_expr;
6437 return result;
6441 static gfc_expr *
6442 simplify_nint (const char *name, gfc_expr *e, gfc_expr *k)
6444 gfc_expr *itrunc, *result;
6445 int kind;
6447 kind = get_kind (BT_INTEGER, k, name, gfc_default_integer_kind);
6448 if (kind == -1)
6449 return &gfc_bad_expr;
6451 if (e->expr_type != EXPR_CONSTANT)
6452 return NULL;
6454 itrunc = gfc_copy_expr (e);
6455 mpfr_round (itrunc->value.real, e->value.real);
6457 result = gfc_get_constant_expr (BT_INTEGER, kind, &e->where);
6458 gfc_mpfr_to_mpz (result->value.integer, itrunc->value.real, &e->where);
6460 gfc_free_expr (itrunc);
6462 return range_check (result, name);
6466 gfc_expr *
6467 gfc_simplify_new_line (gfc_expr *e)
6469 gfc_expr *result;
6471 result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, 1);
6472 result->value.character.string[0] = '\n';
6474 return result;
6478 gfc_expr *
6479 gfc_simplify_nint (gfc_expr *e, gfc_expr *k)
6481 return simplify_nint ("NINT", e, k);
6485 gfc_expr *
6486 gfc_simplify_idnint (gfc_expr *e)
6488 return simplify_nint ("IDNINT", e, NULL);
6491 static int norm2_scale;
6493 static gfc_expr *
6494 norm2_add_squared (gfc_expr *result, gfc_expr *e)
6496 mpfr_t tmp;
6498 gcc_assert (e->ts.type == BT_REAL && e->expr_type == EXPR_CONSTANT);
6499 gcc_assert (result->ts.type == BT_REAL
6500 && result->expr_type == EXPR_CONSTANT);
6502 gfc_set_model_kind (result->ts.kind);
6503 int index = gfc_validate_kind (BT_REAL, result->ts.kind, false);
6504 mpfr_exp_t exp;
6505 if (mpfr_regular_p (result->value.real))
6507 exp = mpfr_get_exp (result->value.real);
6508 /* If result is getting close to overflowing, scale down. */
6509 if (exp >= gfc_real_kinds[index].max_exponent - 4
6510 && norm2_scale <= gfc_real_kinds[index].max_exponent - 2)
6512 norm2_scale += 2;
6513 mpfr_div_ui (result->value.real, result->value.real, 16,
6514 GFC_RND_MODE);
6518 mpfr_init (tmp);
6519 if (mpfr_regular_p (e->value.real))
6521 exp = mpfr_get_exp (e->value.real);
6522 /* If e**2 would overflow or close to overflowing, scale down. */
6523 if (exp - norm2_scale >= gfc_real_kinds[index].max_exponent / 2 - 2)
6525 int new_scale = gfc_real_kinds[index].max_exponent / 2 + 4;
6526 mpfr_set_ui (tmp, 1, GFC_RND_MODE);
6527 mpfr_set_exp (tmp, new_scale - norm2_scale);
6528 mpfr_div (result->value.real, result->value.real, tmp, GFC_RND_MODE);
6529 mpfr_div (result->value.real, result->value.real, tmp, GFC_RND_MODE);
6530 norm2_scale = new_scale;
6533 if (norm2_scale)
6535 mpfr_set_ui (tmp, 1, GFC_RND_MODE);
6536 mpfr_set_exp (tmp, norm2_scale);
6537 mpfr_div (tmp, e->value.real, tmp, GFC_RND_MODE);
6539 else
6540 mpfr_set (tmp, e->value.real, GFC_RND_MODE);
6541 mpfr_pow_ui (tmp, tmp, 2, GFC_RND_MODE);
6542 mpfr_add (result->value.real, result->value.real, tmp,
6543 GFC_RND_MODE);
6544 mpfr_clear (tmp);
6546 return result;
6550 static gfc_expr *
6551 norm2_do_sqrt (gfc_expr *result, gfc_expr *e)
6553 gcc_assert (e->ts.type == BT_REAL && e->expr_type == EXPR_CONSTANT);
6554 gcc_assert (result->ts.type == BT_REAL
6555 && result->expr_type == EXPR_CONSTANT);
6557 if (result != e)
6558 mpfr_set (result->value.real, e->value.real, GFC_RND_MODE);
6559 mpfr_sqrt (result->value.real, result->value.real, GFC_RND_MODE);
6560 if (norm2_scale && mpfr_regular_p (result->value.real))
6562 mpfr_t tmp;
6563 mpfr_init (tmp);
6564 mpfr_set_ui (tmp, 1, GFC_RND_MODE);
6565 mpfr_set_exp (tmp, norm2_scale);
6566 mpfr_mul (result->value.real, result->value.real, tmp, GFC_RND_MODE);
6567 mpfr_clear (tmp);
6569 norm2_scale = 0;
6571 return result;
6575 gfc_expr *
6576 gfc_simplify_norm2 (gfc_expr *e, gfc_expr *dim)
6578 gfc_expr *result;
6579 bool size_zero;
6581 size_zero = gfc_is_size_zero_array (e);
6583 if (!(is_constant_array_expr (e) || size_zero)
6584 || (dim != NULL && !gfc_is_constant_expr (dim)))
6585 return NULL;
6587 result = transformational_result (e, dim, e->ts.type, e->ts.kind, &e->where);
6588 init_result_expr (result, 0, NULL);
6590 if (size_zero)
6591 return result;
6593 norm2_scale = 0;
6594 if (!dim || e->rank == 1)
6596 result = simplify_transformation_to_scalar (result, e, NULL,
6597 norm2_add_squared);
6598 mpfr_sqrt (result->value.real, result->value.real, GFC_RND_MODE);
6599 if (norm2_scale && mpfr_regular_p (result->value.real))
6601 mpfr_t tmp;
6602 mpfr_init (tmp);
6603 mpfr_set_ui (tmp, 1, GFC_RND_MODE);
6604 mpfr_set_exp (tmp, norm2_scale);
6605 mpfr_mul (result->value.real, result->value.real, tmp, GFC_RND_MODE);
6606 mpfr_clear (tmp);
6608 norm2_scale = 0;
6610 else
6611 result = simplify_transformation_to_array (result, e, dim, NULL,
6612 norm2_add_squared,
6613 norm2_do_sqrt);
6615 return result;
6619 gfc_expr *
6620 gfc_simplify_not (gfc_expr *e)
6622 gfc_expr *result;
6624 if (e->expr_type != EXPR_CONSTANT)
6625 return NULL;
6627 result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
6628 mpz_com (result->value.integer, e->value.integer);
6630 return range_check (result, "NOT");
6634 gfc_expr *
6635 gfc_simplify_null (gfc_expr *mold)
6637 gfc_expr *result;
6639 if (mold)
6641 result = gfc_copy_expr (mold);
6642 result->expr_type = EXPR_NULL;
6644 else
6645 result = gfc_get_null_expr (NULL);
6647 return result;
6651 gfc_expr *
6652 gfc_simplify_num_images (gfc_expr *distance ATTRIBUTE_UNUSED, gfc_expr *failed)
6654 gfc_expr *result;
6656 if (flag_coarray == GFC_FCOARRAY_NONE)
6658 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
6659 return &gfc_bad_expr;
6662 if (flag_coarray != GFC_FCOARRAY_SINGLE)
6663 return NULL;
6665 if (failed && failed->expr_type != EXPR_CONSTANT)
6666 return NULL;
6668 /* FIXME: gfc_current_locus is wrong. */
6669 result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
6670 &gfc_current_locus);
6672 if (failed && failed->value.logical != 0)
6673 mpz_set_si (result->value.integer, 0);
6674 else
6675 mpz_set_si (result->value.integer, 1);
6677 return result;
6681 gfc_expr *
6682 gfc_simplify_or (gfc_expr *x, gfc_expr *y)
6684 gfc_expr *result;
6685 int kind;
6687 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
6688 return NULL;
6690 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
6692 switch (x->ts.type)
6694 case BT_INTEGER:
6695 result = gfc_get_constant_expr (BT_INTEGER, kind, &x->where);
6696 mpz_ior (result->value.integer, x->value.integer, y->value.integer);
6697 return range_check (result, "OR");
6699 case BT_LOGICAL:
6700 return gfc_get_logical_expr (kind, &x->where,
6701 x->value.logical || y->value.logical);
6702 default:
6703 gcc_unreachable();
6708 gfc_expr *
6709 gfc_simplify_pack (gfc_expr *array, gfc_expr *mask, gfc_expr *vector)
6711 gfc_expr *result;
6712 gfc_constructor *array_ctor, *mask_ctor, *vector_ctor;
6714 if (!is_constant_array_expr (array)
6715 || !is_constant_array_expr (vector)
6716 || (!gfc_is_constant_expr (mask)
6717 && !is_constant_array_expr (mask)))
6718 return NULL;
6720 result = gfc_get_array_expr (array->ts.type, array->ts.kind, &array->where);
6721 if (array->ts.type == BT_DERIVED)
6722 result->ts.u.derived = array->ts.u.derived;
6724 array_ctor = gfc_constructor_first (array->value.constructor);
6725 vector_ctor = vector
6726 ? gfc_constructor_first (vector->value.constructor)
6727 : NULL;
6729 if (mask->expr_type == EXPR_CONSTANT
6730 && mask->value.logical)
6732 /* Copy all elements of ARRAY to RESULT. */
6733 while (array_ctor)
6735 gfc_constructor_append_expr (&result->value.constructor,
6736 gfc_copy_expr (array_ctor->expr),
6737 NULL);
6739 array_ctor = gfc_constructor_next (array_ctor);
6740 vector_ctor = gfc_constructor_next (vector_ctor);
6743 else if (mask->expr_type == EXPR_ARRAY)
6745 /* Copy only those elements of ARRAY to RESULT whose
6746 MASK equals .TRUE.. */
6747 mask_ctor = gfc_constructor_first (mask->value.constructor);
6748 while (mask_ctor && array_ctor)
6750 if (mask_ctor->expr->value.logical)
6752 gfc_constructor_append_expr (&result->value.constructor,
6753 gfc_copy_expr (array_ctor->expr),
6754 NULL);
6755 vector_ctor = gfc_constructor_next (vector_ctor);
6758 array_ctor = gfc_constructor_next (array_ctor);
6759 mask_ctor = gfc_constructor_next (mask_ctor);
6763 /* Append any left-over elements from VECTOR to RESULT. */
6764 while (vector_ctor)
6766 gfc_constructor_append_expr (&result->value.constructor,
6767 gfc_copy_expr (vector_ctor->expr),
6768 NULL);
6769 vector_ctor = gfc_constructor_next (vector_ctor);
6772 result->shape = gfc_get_shape (1);
6773 gfc_array_size (result, &result->shape[0]);
6775 if (array->ts.type == BT_CHARACTER)
6776 result->ts.u.cl = array->ts.u.cl;
6778 return result;
6782 static gfc_expr *
6783 do_xor (gfc_expr *result, gfc_expr *e)
6785 gcc_assert (e->ts.type == BT_LOGICAL && e->expr_type == EXPR_CONSTANT);
6786 gcc_assert (result->ts.type == BT_LOGICAL
6787 && result->expr_type == EXPR_CONSTANT);
6789 result->value.logical = result->value.logical != e->value.logical;
6790 return result;
6794 gfc_expr *
6795 gfc_simplify_is_contiguous (gfc_expr *array)
6797 if (gfc_is_simply_contiguous (array, false, true))
6798 return gfc_get_logical_expr (gfc_default_logical_kind, &array->where, 1);
6800 if (gfc_is_not_contiguous (array))
6801 return gfc_get_logical_expr (gfc_default_logical_kind, &array->where, 0);
6803 return NULL;
6807 gfc_expr *
6808 gfc_simplify_parity (gfc_expr *e, gfc_expr *dim)
6810 return simplify_transformation (e, dim, NULL, 0, do_xor);
6814 gfc_expr *
6815 gfc_simplify_popcnt (gfc_expr *e)
6817 int res, k;
6818 mpz_t x;
6820 if (e->expr_type != EXPR_CONSTANT)
6821 return NULL;
6823 k = gfc_validate_kind (e->ts.type, e->ts.kind, false);
6825 if (flag_unsigned && e->ts.type == BT_UNSIGNED)
6826 res = mpz_popcount (e->value.integer);
6827 else
6829 /* Convert argument to unsigned, then count the '1' bits. */
6830 mpz_init_set (x, e->value.integer);
6831 gfc_convert_mpz_to_unsigned (x, gfc_integer_kinds[k].bit_size);
6832 res = mpz_popcount (x);
6833 mpz_clear (x);
6836 return gfc_get_int_expr (gfc_default_integer_kind, &e->where, res);
6840 gfc_expr *
6841 gfc_simplify_poppar (gfc_expr *e)
6843 gfc_expr *popcnt;
6844 int i;
6846 if (e->expr_type != EXPR_CONSTANT)
6847 return NULL;
6849 popcnt = gfc_simplify_popcnt (e);
6850 gcc_assert (popcnt);
6852 bool fail = gfc_extract_int (popcnt, &i);
6853 gcc_assert (!fail);
6855 return gfc_get_int_expr (gfc_default_integer_kind, &e->where, i % 2);
6859 gfc_expr *
6860 gfc_simplify_precision (gfc_expr *e)
6862 int i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
6863 return gfc_get_int_expr (gfc_default_integer_kind, &e->where,
6864 gfc_real_kinds[i].precision);
6868 gfc_expr *
6869 gfc_simplify_product (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
6871 return simplify_transformation (array, dim, mask, 1, gfc_multiply);
6875 gfc_expr *
6876 gfc_simplify_radix (gfc_expr *e)
6878 int i;
6879 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
6881 switch (e->ts.type)
6883 case BT_INTEGER:
6884 i = gfc_integer_kinds[i].radix;
6885 break;
6887 case BT_REAL:
6888 i = gfc_real_kinds[i].radix;
6889 break;
6891 default:
6892 gcc_unreachable ();
6895 return gfc_get_int_expr (gfc_default_integer_kind, &e->where, i);
6899 gfc_expr *
6900 gfc_simplify_range (gfc_expr *e)
6902 int i;
6903 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
6905 switch (e->ts.type)
6907 case BT_INTEGER:
6908 i = gfc_integer_kinds[i].range;
6909 break;
6911 case BT_UNSIGNED:
6912 i = gfc_unsigned_kinds[i].range;
6913 break;
6915 case BT_REAL:
6916 case BT_COMPLEX:
6917 i = gfc_real_kinds[i].range;
6918 break;
6920 default:
6921 gcc_unreachable ();
6924 return gfc_get_int_expr (gfc_default_integer_kind, &e->where, i);
6928 gfc_expr *
6929 gfc_simplify_rank (gfc_expr *e)
6931 /* Assumed rank. */
6932 if (e->rank == -1)
6933 return NULL;
6935 return gfc_get_int_expr (gfc_default_integer_kind, &e->where, e->rank);
6939 gfc_expr *
6940 gfc_simplify_real (gfc_expr *e, gfc_expr *k)
6942 gfc_expr *result = NULL;
6943 int kind, tmp1, tmp2;
6945 /* Convert BOZ to real, and return without range checking. */
6946 if (e->ts.type == BT_BOZ)
6948 /* Determine kind for conversion of the BOZ. */
6949 if (k)
6950 gfc_extract_int (k, &kind);
6951 else
6952 kind = gfc_default_real_kind;
6954 if (!gfc_boz2real (e, kind))
6955 return NULL;
6956 result = gfc_copy_expr (e);
6957 return result;
6960 if (e->ts.type == BT_COMPLEX)
6961 kind = get_kind (BT_REAL, k, "REAL", e->ts.kind);
6962 else
6963 kind = get_kind (BT_REAL, k, "REAL", gfc_default_real_kind);
6965 if (kind == -1)
6966 return &gfc_bad_expr;
6968 if (e->expr_type != EXPR_CONSTANT)
6969 return NULL;
6971 /* For explicit conversion, turn off -Wconversion and -Wconversion-extra
6972 warnings. */
6973 tmp1 = warn_conversion;
6974 tmp2 = warn_conversion_extra;
6975 warn_conversion = warn_conversion_extra = 0;
6977 result = gfc_convert_constant (e, BT_REAL, kind);
6979 warn_conversion = tmp1;
6980 warn_conversion_extra = tmp2;
6982 if (result == &gfc_bad_expr)
6983 return &gfc_bad_expr;
6985 return range_check (result, "REAL");
6989 gfc_expr *
6990 gfc_simplify_realpart (gfc_expr *e)
6992 gfc_expr *result;
6994 if (e->expr_type != EXPR_CONSTANT)
6995 return NULL;
6997 result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
6998 mpc_real (result->value.real, e->value.complex, GFC_RND_MODE);
7000 return range_check (result, "REALPART");
7003 gfc_expr *
7004 gfc_simplify_repeat (gfc_expr *e, gfc_expr *n)
7006 gfc_expr *result;
7007 gfc_charlen_t len;
7008 mpz_t ncopies;
7009 bool have_length = false;
7011 /* If NCOPIES isn't a constant, there's nothing we can do. */
7012 if (n->expr_type != EXPR_CONSTANT)
7013 return NULL;
7015 /* If NCOPIES is negative, it's an error. */
7016 if (mpz_sgn (n->value.integer) < 0)
7018 gfc_error ("Argument NCOPIES of REPEAT intrinsic is negative at %L",
7019 &n->where);
7020 return &gfc_bad_expr;
7023 /* If we don't know the character length, we can do no more. */
7024 if (e->ts.u.cl && e->ts.u.cl->length
7025 && e->ts.u.cl->length->expr_type == EXPR_CONSTANT)
7027 len = gfc_mpz_get_hwi (e->ts.u.cl->length->value.integer);
7028 have_length = true;
7030 else if (e->expr_type == EXPR_CONSTANT
7031 && (e->ts.u.cl == NULL || e->ts.u.cl->length == NULL))
7033 len = e->value.character.length;
7035 else
7036 return NULL;
7038 /* If the source length is 0, any value of NCOPIES is valid
7039 and everything behaves as if NCOPIES == 0. */
7040 mpz_init (ncopies);
7041 if (len == 0)
7042 mpz_set_ui (ncopies, 0);
7043 else
7044 mpz_set (ncopies, n->value.integer);
7046 /* Check that NCOPIES isn't too large. */
7047 if (len)
7049 mpz_t max, mlen;
7050 int i;
7052 /* Compute the maximum value allowed for NCOPIES: huge(cl) / len. */
7053 mpz_init (max);
7054 i = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
7056 if (have_length)
7058 mpz_tdiv_q (max, gfc_integer_kinds[i].huge,
7059 e->ts.u.cl->length->value.integer);
7061 else
7063 mpz_init (mlen);
7064 gfc_mpz_set_hwi (mlen, len);
7065 mpz_tdiv_q (max, gfc_integer_kinds[i].huge, mlen);
7066 mpz_clear (mlen);
7069 /* The check itself. */
7070 if (mpz_cmp (ncopies, max) > 0)
7072 mpz_clear (max);
7073 mpz_clear (ncopies);
7074 gfc_error ("Argument NCOPIES of REPEAT intrinsic is too large at %L",
7075 &n->where);
7076 return &gfc_bad_expr;
7079 mpz_clear (max);
7081 mpz_clear (ncopies);
7083 /* For further simplification, we need the character string to be
7084 constant. */
7085 if (e->expr_type != EXPR_CONSTANT)
7086 return NULL;
7088 HOST_WIDE_INT ncop;
7089 if (len ||
7090 (e->ts.u.cl->length &&
7091 mpz_sgn (e->ts.u.cl->length->value.integer) != 0))
7093 bool fail = gfc_extract_hwi (n, &ncop);
7094 gcc_assert (!fail);
7096 else
7097 ncop = 0;
7099 if (ncop == 0)
7100 return gfc_get_character_expr (e->ts.kind, &e->where, NULL, 0);
7102 len = e->value.character.length;
7103 gfc_charlen_t nlen = ncop * len;
7105 /* Here's a semi-arbitrary limit. If the string is longer than 1 GB
7106 (2**28 elements * 4 bytes (wide chars) per element) defer to
7107 runtime instead of consuming (unbounded) memory and CPU at
7108 compile time. */
7109 if (nlen > 268435456)
7111 gfc_warning_now (0, "Evaluation of string longer than 2**28 at %L"
7112 " deferred to runtime, expect bugs", &e->where);
7113 return NULL;
7116 result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, nlen);
7117 for (size_t i = 0; i < (size_t) ncop; i++)
7118 for (size_t j = 0; j < (size_t) len; j++)
7119 result->value.character.string[j+i*len]= e->value.character.string[j];
7121 result->value.character.string[nlen] = '\0'; /* For debugger */
7122 return result;
7126 /* This one is a bear, but mainly has to do with shuffling elements. */
7128 gfc_expr *
7129 gfc_simplify_reshape (gfc_expr *source, gfc_expr *shape_exp,
7130 gfc_expr *pad, gfc_expr *order_exp)
7132 int order[GFC_MAX_DIMENSIONS], shape[GFC_MAX_DIMENSIONS];
7133 int i, rank, npad, x[GFC_MAX_DIMENSIONS];
7134 mpz_t index, size;
7135 unsigned long j;
7136 size_t nsource;
7137 gfc_expr *e, *result;
7138 bool zerosize = false;
7140 /* Check that argument expression types are OK. */
7141 if (!is_constant_array_expr (source)
7142 || !is_constant_array_expr (shape_exp)
7143 || !is_constant_array_expr (pad)
7144 || !is_constant_array_expr (order_exp))
7145 return NULL;
7147 if (source->shape == NULL)
7148 return NULL;
7150 /* Proceed with simplification, unpacking the array. */
7152 mpz_init (index);
7153 rank = 0;
7155 for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
7156 x[i] = 0;
7158 for (;;)
7160 e = gfc_constructor_lookup_expr (shape_exp->value.constructor, rank);
7161 if (e == NULL)
7162 break;
7164 gfc_extract_int (e, &shape[rank]);
7166 gcc_assert (rank >= 0 && rank < GFC_MAX_DIMENSIONS);
7167 if (shape[rank] < 0)
7169 gfc_error ("The SHAPE array for the RESHAPE intrinsic at %L has a "
7170 "negative value %d for dimension %d",
7171 &shape_exp->where, shape[rank], rank+1);
7172 mpz_clear (index);
7173 return &gfc_bad_expr;
7176 rank++;
7179 gcc_assert (rank > 0);
7181 /* Now unpack the order array if present. */
7182 if (order_exp == NULL)
7184 for (i = 0; i < rank; i++)
7185 order[i] = i;
7187 else
7189 mpz_t size;
7190 int order_size, shape_size;
7192 if (order_exp->rank != shape_exp->rank)
7194 gfc_error ("Shapes of ORDER at %L and SHAPE at %L are different",
7195 &order_exp->where, &shape_exp->where);
7196 mpz_clear (index);
7197 return &gfc_bad_expr;
7200 gfc_array_size (shape_exp, &size);
7201 shape_size = mpz_get_ui (size);
7202 mpz_clear (size);
7203 gfc_array_size (order_exp, &size);
7204 order_size = mpz_get_ui (size);
7205 mpz_clear (size);
7206 if (order_size != shape_size)
7208 gfc_error ("Sizes of ORDER at %L and SHAPE at %L are different",
7209 &order_exp->where, &shape_exp->where);
7210 mpz_clear (index);
7211 return &gfc_bad_expr;
7214 for (i = 0; i < rank; i++)
7216 e = gfc_constructor_lookup_expr (order_exp->value.constructor, i);
7217 gcc_assert (e);
7219 gfc_extract_int (e, &order[i]);
7221 if (order[i] < 1 || order[i] > rank)
7223 gfc_error ("Element with a value of %d in ORDER at %L must be "
7224 "in the range [1, ..., %d] for the RESHAPE intrinsic "
7225 "near %L", order[i], &order_exp->where, rank,
7226 &shape_exp->where);
7227 mpz_clear (index);
7228 return &gfc_bad_expr;
7231 order[i]--;
7232 if (x[order[i]] != 0)
7234 gfc_error ("ORDER at %L is not a permutation of the size of "
7235 "SHAPE at %L", &order_exp->where, &shape_exp->where);
7236 mpz_clear (index);
7237 return &gfc_bad_expr;
7239 x[order[i]] = 1;
7243 /* Count the elements in the source and padding arrays. */
7245 npad = 0;
7246 if (pad != NULL)
7248 gfc_array_size (pad, &size);
7249 npad = mpz_get_ui (size);
7250 mpz_clear (size);
7253 gfc_array_size (source, &size);
7254 nsource = mpz_get_ui (size);
7255 mpz_clear (size);
7257 /* If it weren't for that pesky permutation we could just loop
7258 through the source and round out any shortage with pad elements.
7259 But no, someone just had to have the compiler do something the
7260 user should be doing. */
7262 for (i = 0; i < rank; i++)
7263 x[i] = 0;
7265 result = gfc_get_array_expr (source->ts.type, source->ts.kind,
7266 &source->where);
7267 if (source->ts.type == BT_DERIVED)
7268 result->ts.u.derived = source->ts.u.derived;
7269 if (source->ts.type == BT_CHARACTER && result->ts.u.cl == NULL)
7270 result->ts = source->ts;
7271 result->rank = rank;
7272 result->shape = gfc_get_shape (rank);
7273 for (i = 0; i < rank; i++)
7275 mpz_init_set_ui (result->shape[i], shape[i]);
7276 if (shape[i] == 0)
7277 zerosize = true;
7280 if (zerosize)
7281 goto sizezero;
7283 while (nsource > 0 || npad > 0)
7285 /* Figure out which element to extract. */
7286 mpz_set_ui (index, 0);
7288 for (i = rank - 1; i >= 0; i--)
7290 mpz_add_ui (index, index, x[order[i]]);
7291 if (i != 0)
7292 mpz_mul_ui (index, index, shape[order[i - 1]]);
7295 if (mpz_cmp_ui (index, INT_MAX) > 0)
7296 gfc_internal_error ("Reshaped array too large at %C");
7298 j = mpz_get_ui (index);
7300 if (j < nsource)
7301 e = gfc_constructor_lookup_expr (source->value.constructor, j);
7302 else
7304 if (npad <= 0)
7306 mpz_clear (index);
7307 if (pad == NULL)
7308 gfc_error ("Without padding, there are not enough elements "
7309 "in the intrinsic RESHAPE source at %L to match "
7310 "the shape", &source->where);
7311 gfc_free_expr (result);
7312 return NULL;
7314 j = j - nsource;
7315 j = j % npad;
7316 e = gfc_constructor_lookup_expr (pad->value.constructor, j);
7318 gcc_assert (e);
7320 gfc_constructor_append_expr (&result->value.constructor,
7321 gfc_copy_expr (e), &e->where);
7323 /* Calculate the next element. */
7324 i = 0;
7326 inc:
7327 if (++x[i] < shape[i])
7328 continue;
7329 x[i++] = 0;
7330 if (i < rank)
7331 goto inc;
7333 break;
7336 sizezero:
7338 mpz_clear (index);
7340 return result;
7344 gfc_expr *
7345 gfc_simplify_rrspacing (gfc_expr *x)
7347 gfc_expr *result;
7348 int i;
7349 long int e, p;
7351 if (x->expr_type != EXPR_CONSTANT)
7352 return NULL;
7354 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
7356 result = gfc_get_constant_expr (BT_REAL, x->ts.kind, &x->where);
7358 /* RRSPACING(+/- 0.0) = 0.0 */
7359 if (mpfr_zero_p (x->value.real))
7361 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
7362 return result;
7365 /* RRSPACING(inf) = NaN */
7366 if (mpfr_inf_p (x->value.real))
7368 mpfr_set_nan (result->value.real);
7369 return result;
7372 /* RRSPACING(NaN) = same NaN */
7373 if (mpfr_nan_p (x->value.real))
7375 mpfr_set (result->value.real, x->value.real, GFC_RND_MODE);
7376 return result;
7379 /* | x * 2**(-e) | * 2**p. */
7380 mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE);
7381 e = - (long int) mpfr_get_exp (x->value.real);
7382 mpfr_mul_2si (result->value.real, result->value.real, e, GFC_RND_MODE);
7384 p = (long int) gfc_real_kinds[i].digits;
7385 mpfr_mul_2si (result->value.real, result->value.real, p, GFC_RND_MODE);
7387 return range_check (result, "RRSPACING");
7391 gfc_expr *
7392 gfc_simplify_scale (gfc_expr *x, gfc_expr *i)
7394 int k, neg_flag, power, exp_range;
7395 mpfr_t scale, radix;
7396 gfc_expr *result;
7398 if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
7399 return NULL;
7401 result = gfc_get_constant_expr (BT_REAL, x->ts.kind, &x->where);
7403 if (mpfr_zero_p (x->value.real))
7405 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
7406 return result;
7409 k = gfc_validate_kind (BT_REAL, x->ts.kind, false);
7411 exp_range = gfc_real_kinds[k].max_exponent - gfc_real_kinds[k].min_exponent;
7413 /* This check filters out values of i that would overflow an int. */
7414 if (mpz_cmp_si (i->value.integer, exp_range + 2) > 0
7415 || mpz_cmp_si (i->value.integer, -exp_range - 2) < 0)
7417 gfc_error ("Result of SCALE overflows its kind at %L", &result->where);
7418 gfc_free_expr (result);
7419 return &gfc_bad_expr;
7422 /* Compute scale = radix ** power. */
7423 power = mpz_get_si (i->value.integer);
7425 if (power >= 0)
7426 neg_flag = 0;
7427 else
7429 neg_flag = 1;
7430 power = -power;
7433 gfc_set_model_kind (x->ts.kind);
7434 mpfr_init (scale);
7435 mpfr_init (radix);
7436 mpfr_set_ui (radix, gfc_real_kinds[k].radix, GFC_RND_MODE);
7437 mpfr_pow_ui (scale, radix, power, GFC_RND_MODE);
7439 if (neg_flag)
7440 mpfr_div (result->value.real, x->value.real, scale, GFC_RND_MODE);
7441 else
7442 mpfr_mul (result->value.real, x->value.real, scale, GFC_RND_MODE);
7444 mpfr_clears (scale, radix, NULL);
7446 return range_check (result, "SCALE");
7450 /* Variants of strspn and strcspn that operate on wide characters. */
7452 static size_t
7453 wide_strspn (const gfc_char_t *s1, const gfc_char_t *s2)
7455 size_t i = 0;
7456 const gfc_char_t *c;
7458 while (s1[i])
7460 for (c = s2; *c; c++)
7462 if (s1[i] == *c)
7463 break;
7465 if (*c == '\0')
7466 break;
7467 i++;
7470 return i;
7473 static size_t
7474 wide_strcspn (const gfc_char_t *s1, const gfc_char_t *s2)
7476 size_t i = 0;
7477 const gfc_char_t *c;
7479 while (s1[i])
7481 for (c = s2; *c; c++)
7483 if (s1[i] == *c)
7484 break;
7486 if (*c)
7487 break;
7488 i++;
7491 return i;
7495 gfc_expr *
7496 gfc_simplify_scan (gfc_expr *e, gfc_expr *c, gfc_expr *b, gfc_expr *kind)
7498 gfc_expr *result;
7499 int back;
7500 size_t i;
7501 size_t indx, len, lenc;
7502 int k = get_kind (BT_INTEGER, kind, "SCAN", gfc_default_integer_kind);
7504 if (k == -1)
7505 return &gfc_bad_expr;
7507 if (e->expr_type != EXPR_CONSTANT || c->expr_type != EXPR_CONSTANT
7508 || ( b != NULL && b->expr_type != EXPR_CONSTANT))
7509 return NULL;
7511 if (b != NULL && b->value.logical != 0)
7512 back = 1;
7513 else
7514 back = 0;
7516 len = e->value.character.length;
7517 lenc = c->value.character.length;
7519 if (len == 0 || lenc == 0)
7521 indx = 0;
7523 else
7525 if (back == 0)
7527 indx = wide_strcspn (e->value.character.string,
7528 c->value.character.string) + 1;
7529 if (indx > len)
7530 indx = 0;
7532 else
7533 for (indx = len; indx > 0; indx--)
7535 for (i = 0; i < lenc; i++)
7537 if (c->value.character.string[i]
7538 == e->value.character.string[indx - 1])
7539 break;
7541 if (i < lenc)
7542 break;
7546 result = gfc_get_int_expr (k, &e->where, indx);
7547 return range_check (result, "SCAN");
7551 gfc_expr *
7552 gfc_simplify_selected_char_kind (gfc_expr *e)
7554 int kind;
7556 if (e->expr_type != EXPR_CONSTANT)
7557 return NULL;
7559 if (gfc_compare_with_Cstring (e, "ascii", false) == 0
7560 || gfc_compare_with_Cstring (e, "default", false) == 0)
7561 kind = 1;
7562 else if (gfc_compare_with_Cstring (e, "iso_10646", false) == 0)
7563 kind = 4;
7564 else
7565 kind = -1;
7567 return gfc_get_int_expr (gfc_default_integer_kind, &e->where, kind);
7571 gfc_expr *
7572 gfc_simplify_selected_int_kind (gfc_expr *e)
7574 int i, kind, range;
7576 if (e->expr_type != EXPR_CONSTANT || gfc_extract_int (e, &range))
7577 return NULL;
7579 kind = INT_MAX;
7581 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
7582 if (gfc_integer_kinds[i].range >= range
7583 && gfc_integer_kinds[i].kind < kind)
7584 kind = gfc_integer_kinds[i].kind;
7586 if (kind == INT_MAX)
7587 kind = -1;
7589 return gfc_get_int_expr (gfc_default_integer_kind, &e->where, kind);
7592 /* Same as above, but with unsigneds. */
7594 gfc_expr *
7595 gfc_simplify_selected_unsigned_kind (gfc_expr *e)
7597 int i, kind, range;
7599 if (e->expr_type != EXPR_CONSTANT || gfc_extract_int (e, &range))
7600 return NULL;
7602 kind = INT_MAX;
7604 for (i = 0; gfc_unsigned_kinds[i].kind != 0; i++)
7605 if (gfc_unsigned_kinds[i].range >= range
7606 && gfc_unsigned_kinds[i].kind < kind)
7607 kind = gfc_unsigned_kinds[i].kind;
7609 if (kind == INT_MAX)
7610 kind = -1;
7612 return gfc_get_int_expr (gfc_default_integer_kind, &e->where, kind);
7616 gfc_expr *
7617 gfc_simplify_selected_logical_kind (gfc_expr *e)
7619 int i, kind, bits;
7621 if (e->expr_type != EXPR_CONSTANT || gfc_extract_int (e, &bits))
7622 return NULL;
7624 kind = INT_MAX;
7626 for (i = 0; gfc_logical_kinds[i].kind != 0; i++)
7627 if (gfc_logical_kinds[i].bit_size >= bits
7628 && gfc_logical_kinds[i].kind < kind)
7629 kind = gfc_logical_kinds[i].kind;
7631 if (kind == INT_MAX)
7632 kind = -1;
7634 return gfc_get_int_expr (gfc_default_integer_kind, &e->where, kind);
7638 gfc_expr *
7639 gfc_simplify_selected_real_kind (gfc_expr *p, gfc_expr *q, gfc_expr *rdx)
7641 int range, precision, radix, i, kind, found_precision, found_range,
7642 found_radix;
7643 locus *loc = &gfc_current_locus;
7645 if (p == NULL)
7646 precision = 0;
7647 else
7649 if (p->expr_type != EXPR_CONSTANT
7650 || gfc_extract_int (p, &precision))
7651 return NULL;
7652 loc = &p->where;
7655 if (q == NULL)
7656 range = 0;
7657 else
7659 if (q->expr_type != EXPR_CONSTANT
7660 || gfc_extract_int (q, &range))
7661 return NULL;
7663 if (!loc)
7664 loc = &q->where;
7667 if (rdx == NULL)
7668 radix = 0;
7669 else
7671 if (rdx->expr_type != EXPR_CONSTANT
7672 || gfc_extract_int (rdx, &radix))
7673 return NULL;
7675 if (!loc)
7676 loc = &rdx->where;
7679 kind = INT_MAX;
7680 found_precision = 0;
7681 found_range = 0;
7682 found_radix = 0;
7684 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
7686 if (gfc_real_kinds[i].precision >= precision)
7687 found_precision = 1;
7689 if (gfc_real_kinds[i].range >= range)
7690 found_range = 1;
7692 if (radix == 0 || gfc_real_kinds[i].radix == radix)
7693 found_radix = 1;
7695 if (gfc_real_kinds[i].precision >= precision
7696 && gfc_real_kinds[i].range >= range
7697 && (radix == 0 || gfc_real_kinds[i].radix == radix)
7698 && gfc_real_kinds[i].kind < kind)
7699 kind = gfc_real_kinds[i].kind;
7702 if (kind == INT_MAX)
7704 if (found_radix && found_range && !found_precision)
7705 kind = -1;
7706 else if (found_radix && found_precision && !found_range)
7707 kind = -2;
7708 else if (found_radix && !found_precision && !found_range)
7709 kind = -3;
7710 else if (found_radix)
7711 kind = -4;
7712 else
7713 kind = -5;
7716 return gfc_get_int_expr (gfc_default_integer_kind, loc, kind);
7720 gfc_expr *
7721 gfc_simplify_set_exponent (gfc_expr *x, gfc_expr *i)
7723 gfc_expr *result;
7724 mpfr_t exp, absv, log2, pow2, frac;
7725 long exp2;
7727 if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
7728 return NULL;
7730 result = gfc_get_constant_expr (BT_REAL, x->ts.kind, &x->where);
7732 /* SET_EXPONENT (+/-0.0, I) = +/- 0.0
7733 SET_EXPONENT (NaN) = same NaN */
7734 if (mpfr_zero_p (x->value.real) || mpfr_nan_p (x->value.real))
7736 mpfr_set (result->value.real, x->value.real, GFC_RND_MODE);
7737 return result;
7740 /* SET_EXPONENT (inf) = NaN */
7741 if (mpfr_inf_p (x->value.real))
7743 mpfr_set_nan (result->value.real);
7744 return result;
7747 gfc_set_model_kind (x->ts.kind);
7748 mpfr_init (absv);
7749 mpfr_init (log2);
7750 mpfr_init (exp);
7751 mpfr_init (pow2);
7752 mpfr_init (frac);
7754 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
7755 mpfr_log2 (log2, absv, GFC_RND_MODE);
7757 mpfr_floor (log2, log2);
7758 mpfr_add_ui (exp, log2, 1, GFC_RND_MODE);
7760 /* Old exponent value, and fraction. */
7761 mpfr_ui_pow (pow2, 2, exp, GFC_RND_MODE);
7763 mpfr_div (frac, x->value.real, pow2, GFC_RND_MODE);
7765 /* New exponent. */
7766 exp2 = mpz_get_si (i->value.integer);
7767 mpfr_mul_2si (result->value.real, frac, exp2, GFC_RND_MODE);
7769 mpfr_clears (absv, log2, exp, pow2, frac, NULL);
7771 return range_check (result, "SET_EXPONENT");
7775 gfc_expr *
7776 gfc_simplify_shape (gfc_expr *source, gfc_expr *kind)
7778 mpz_t shape[GFC_MAX_DIMENSIONS];
7779 gfc_expr *result, *e, *f;
7780 gfc_array_ref *ar;
7781 int n;
7782 bool t;
7783 int k = get_kind (BT_INTEGER, kind, "SHAPE", gfc_default_integer_kind);
7785 if (source->rank == -1)
7786 return NULL;
7788 result = gfc_get_array_expr (BT_INTEGER, k, &source->where);
7789 result->shape = gfc_get_shape (1);
7790 mpz_init (result->shape[0]);
7792 if (source->rank == 0)
7793 return result;
7795 if (source->expr_type == EXPR_VARIABLE)
7797 ar = gfc_find_array_ref (source);
7798 t = gfc_array_ref_shape (ar, shape);
7800 else if (source->shape)
7802 t = true;
7803 for (n = 0; n < source->rank; n++)
7805 mpz_init (shape[n]);
7806 mpz_set (shape[n], source->shape[n]);
7809 else
7810 t = false;
7812 for (n = 0; n < source->rank; n++)
7814 e = gfc_get_constant_expr (BT_INTEGER, k, &source->where);
7816 if (t)
7817 mpz_set (e->value.integer, shape[n]);
7818 else
7820 mpz_set_ui (e->value.integer, n + 1);
7822 f = simplify_size (source, e, k);
7823 gfc_free_expr (e);
7824 if (f == NULL)
7826 gfc_free_expr (result);
7827 return NULL;
7829 else
7830 e = f;
7833 if (e == &gfc_bad_expr || range_check (e, "SHAPE") == &gfc_bad_expr)
7835 gfc_free_expr (result);
7836 if (t)
7837 gfc_clear_shape (shape, source->rank);
7838 return &gfc_bad_expr;
7841 gfc_constructor_append_expr (&result->value.constructor, e, NULL);
7844 if (t)
7845 gfc_clear_shape (shape, source->rank);
7847 mpz_set_si (result->shape[0], source->rank);
7849 return result;
7853 static gfc_expr *
7854 simplify_size (gfc_expr *array, gfc_expr *dim, int k)
7856 mpz_t size;
7857 gfc_expr *return_value;
7858 int d;
7859 gfc_ref *ref;
7861 /* For unary operations, the size of the result is given by the size
7862 of the operand. For binary ones, it's the size of the first operand
7863 unless it is scalar, then it is the size of the second. */
7864 if (array->expr_type == EXPR_OP && !array->value.op.uop)
7866 gfc_expr* replacement;
7867 gfc_expr* simplified;
7869 switch (array->value.op.op)
7871 /* Unary operations. */
7872 case INTRINSIC_NOT:
7873 case INTRINSIC_UPLUS:
7874 case INTRINSIC_UMINUS:
7875 case INTRINSIC_PARENTHESES:
7876 replacement = array->value.op.op1;
7877 break;
7879 /* Binary operations. If any one of the operands is scalar, take
7880 the other one's size. If both of them are arrays, it does not
7881 matter -- try to find one with known shape, if possible. */
7882 default:
7883 if (array->value.op.op1->rank == 0)
7884 replacement = array->value.op.op2;
7885 else if (array->value.op.op2->rank == 0)
7886 replacement = array->value.op.op1;
7887 else
7889 simplified = simplify_size (array->value.op.op1, dim, k);
7890 if (simplified)
7891 return simplified;
7893 replacement = array->value.op.op2;
7895 break;
7898 /* Try to reduce it directly if possible. */
7899 simplified = simplify_size (replacement, dim, k);
7901 /* Otherwise, we build a new SIZE call. This is hopefully at least
7902 simpler than the original one. */
7903 if (!simplified)
7905 gfc_expr *kind = gfc_get_int_expr (gfc_default_integer_kind, NULL, k);
7906 simplified = gfc_build_intrinsic_call (gfc_current_ns,
7907 GFC_ISYM_SIZE, "size",
7908 array->where, 3,
7909 gfc_copy_expr (replacement),
7910 gfc_copy_expr (dim),
7911 kind);
7913 return simplified;
7916 for (ref = array->ref; ref; ref = ref->next)
7917 if (ref->type == REF_ARRAY && ref->u.ar.as
7918 && !gfc_resolve_array_spec (ref->u.ar.as, 0))
7919 return NULL;
7921 if (dim == NULL)
7923 if (!gfc_array_size (array, &size))
7924 return NULL;
7926 else
7928 if (dim->expr_type != EXPR_CONSTANT)
7929 return NULL;
7931 if (array->rank == -1)
7932 return NULL;
7934 d = mpz_get_si (dim->value.integer) - 1;
7935 if (d < 0 || d > array->rank - 1)
7937 gfc_error ("DIM argument (%d) to intrinsic SIZE at %L out of range "
7938 "(1:%d)", d+1, &array->where, array->rank);
7939 return &gfc_bad_expr;
7942 if (!gfc_array_dimen_size (array, d, &size))
7943 return NULL;
7946 return_value = gfc_get_constant_expr (BT_INTEGER, k, &array->where);
7947 mpz_set (return_value->value.integer, size);
7948 mpz_clear (size);
7950 return return_value;
7954 gfc_expr *
7955 gfc_simplify_size (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
7957 gfc_expr *result;
7958 int k = get_kind (BT_INTEGER, kind, "SIZE", gfc_default_integer_kind);
7960 if (k == -1)
7961 return &gfc_bad_expr;
7963 result = simplify_size (array, dim, k);
7964 if (result == NULL || result == &gfc_bad_expr)
7965 return result;
7967 return range_check (result, "SIZE");
7971 /* SIZEOF and C_SIZEOF return the size in bytes of an array element
7972 multiplied by the array size. */
7974 gfc_expr *
7975 gfc_simplify_sizeof (gfc_expr *x)
7977 gfc_expr *result = NULL;
7978 mpz_t array_size;
7979 size_t res_size;
7981 if (x->ts.type == BT_CLASS || x->ts.deferred)
7982 return NULL;
7984 if (x->ts.type == BT_CHARACTER
7985 && (!x->ts.u.cl || !x->ts.u.cl->length
7986 || x->ts.u.cl->length->expr_type != EXPR_CONSTANT))
7987 return NULL;
7989 if (x->rank && x->expr_type != EXPR_ARRAY)
7991 if (!gfc_array_size (x, &array_size))
7992 return NULL;
7994 mpz_clear (array_size);
7997 result = gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
7998 &x->where);
7999 gfc_target_expr_size (x, &res_size);
8000 mpz_set_si (result->value.integer, res_size);
8002 return result;
8006 /* STORAGE_SIZE returns the size in bits of a single array element. */
8008 gfc_expr *
8009 gfc_simplify_storage_size (gfc_expr *x,
8010 gfc_expr *kind)
8012 gfc_expr *result = NULL;
8013 int k;
8014 size_t siz;
8016 if (x->ts.type == BT_CLASS || x->ts.deferred)
8017 return NULL;
8019 if (x->ts.type == BT_CHARACTER && x->expr_type != EXPR_CONSTANT
8020 && (!x->ts.u.cl || !x->ts.u.cl->length
8021 || x->ts.u.cl->length->expr_type != EXPR_CONSTANT))
8022 return NULL;
8024 k = get_kind (BT_INTEGER, kind, "STORAGE_SIZE", gfc_default_integer_kind);
8025 if (k == -1)
8026 return &gfc_bad_expr;
8028 result = gfc_get_constant_expr (BT_INTEGER, k, &x->where);
8030 gfc_element_size (x, &siz);
8031 mpz_set_si (result->value.integer, siz);
8032 mpz_mul_ui (result->value.integer, result->value.integer, BITS_PER_UNIT);
8034 return range_check (result, "STORAGE_SIZE");
8038 gfc_expr *
8039 gfc_simplify_sign (gfc_expr *x, gfc_expr *y)
8041 gfc_expr *result;
8043 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
8044 return NULL;
8046 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
8048 switch (x->ts.type)
8050 case BT_INTEGER:
8051 mpz_abs (result->value.integer, x->value.integer);
8052 if (mpz_sgn (y->value.integer) < 0)
8053 mpz_neg (result->value.integer, result->value.integer);
8054 break;
8056 case BT_REAL:
8057 if (flag_sign_zero)
8058 mpfr_copysign (result->value.real, x->value.real, y->value.real,
8059 GFC_RND_MODE);
8060 else
8061 mpfr_setsign (result->value.real, x->value.real,
8062 mpfr_sgn (y->value.real) < 0 ? 1 : 0, GFC_RND_MODE);
8063 break;
8065 default:
8066 gfc_internal_error ("Bad type in gfc_simplify_sign");
8069 return result;
8073 gfc_expr *
8074 gfc_simplify_sin (gfc_expr *x)
8076 gfc_expr *result;
8078 if (x->expr_type != EXPR_CONSTANT)
8079 return NULL;
8081 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
8083 switch (x->ts.type)
8085 case BT_REAL:
8086 mpfr_sin (result->value.real, x->value.real, GFC_RND_MODE);
8087 break;
8089 case BT_COMPLEX:
8090 gfc_set_model (x->value.real);
8091 mpc_sin (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
8092 break;
8094 default:
8095 gfc_internal_error ("in gfc_simplify_sin(): Bad type");
8098 return range_check (result, "SIN");
8102 gfc_expr *
8103 gfc_simplify_sinh (gfc_expr *x)
8105 gfc_expr *result;
8107 if (x->expr_type != EXPR_CONSTANT)
8108 return NULL;
8110 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
8112 switch (x->ts.type)
8114 case BT_REAL:
8115 mpfr_sinh (result->value.real, x->value.real, GFC_RND_MODE);
8116 break;
8118 case BT_COMPLEX:
8119 mpc_sinh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
8120 break;
8122 default:
8123 gcc_unreachable ();
8126 return range_check (result, "SINH");
8130 /* The argument is always a double precision real that is converted to
8131 single precision. TODO: Rounding! */
8133 gfc_expr *
8134 gfc_simplify_sngl (gfc_expr *a)
8136 gfc_expr *result;
8137 int tmp1, tmp2;
8139 if (a->expr_type != EXPR_CONSTANT)
8140 return NULL;
8142 /* For explicit conversion, turn off -Wconversion and -Wconversion-extra
8143 warnings. */
8144 tmp1 = warn_conversion;
8145 tmp2 = warn_conversion_extra;
8146 warn_conversion = warn_conversion_extra = 0;
8148 result = gfc_real2real (a, gfc_default_real_kind);
8150 warn_conversion = tmp1;
8151 warn_conversion_extra = tmp2;
8153 return range_check (result, "SNGL");
8157 gfc_expr *
8158 gfc_simplify_spacing (gfc_expr *x)
8160 gfc_expr *result;
8161 int i;
8162 long int en, ep;
8164 if (x->expr_type != EXPR_CONSTANT)
8165 return NULL;
8167 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
8168 result = gfc_get_constant_expr (BT_REAL, x->ts.kind, &x->where);
8170 /* SPACING(+/- 0.0) = SPACING(TINY(0.0)) = TINY(0.0) */
8171 if (mpfr_zero_p (x->value.real))
8173 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
8174 return result;
8177 /* SPACING(inf) = NaN */
8178 if (mpfr_inf_p (x->value.real))
8180 mpfr_set_nan (result->value.real);
8181 return result;
8184 /* SPACING(NaN) = same NaN */
8185 if (mpfr_nan_p (x->value.real))
8187 mpfr_set (result->value.real, x->value.real, GFC_RND_MODE);
8188 return result;
8191 /* In the Fortran 95 standard, the result is b**(e - p) where b, e, and p
8192 are the radix, exponent of x, and precision. This excludes the
8193 possibility of subnormal numbers. Fortran 2003 states the result is
8194 b**max(e - p, emin - 1). */
8196 ep = (long int) mpfr_get_exp (x->value.real) - gfc_real_kinds[i].digits;
8197 en = (long int) gfc_real_kinds[i].min_exponent - 1;
8198 en = en > ep ? en : ep;
8200 mpfr_set_ui (result->value.real, 1, GFC_RND_MODE);
8201 mpfr_mul_2si (result->value.real, result->value.real, en, GFC_RND_MODE);
8203 return range_check (result, "SPACING");
8207 gfc_expr *
8208 gfc_simplify_spread (gfc_expr *source, gfc_expr *dim_expr, gfc_expr *ncopies_expr)
8210 gfc_expr *result = NULL;
8211 int nelem, i, j, dim, ncopies;
8212 mpz_t size;
8214 if ((!gfc_is_constant_expr (source)
8215 && !is_constant_array_expr (source))
8216 || !gfc_is_constant_expr (dim_expr)
8217 || !gfc_is_constant_expr (ncopies_expr))
8218 return NULL;
8220 gcc_assert (dim_expr->ts.type == BT_INTEGER);
8221 gfc_extract_int (dim_expr, &dim);
8222 dim -= 1; /* zero-base DIM */
8224 gcc_assert (ncopies_expr->ts.type == BT_INTEGER);
8225 gfc_extract_int (ncopies_expr, &ncopies);
8226 ncopies = MAX (ncopies, 0);
8228 /* Do not allow the array size to exceed the limit for an array
8229 constructor. */
8230 if (source->expr_type == EXPR_ARRAY)
8232 if (!gfc_array_size (source, &size))
8233 gfc_internal_error ("Failure getting length of a constant array.");
8235 else
8236 mpz_init_set_ui (size, 1);
8238 nelem = mpz_get_si (size) * ncopies;
8239 if (nelem > flag_max_array_constructor)
8241 if (gfc_init_expr_flag)
8243 gfc_error ("The number of elements (%d) in the array constructor "
8244 "at %L requires an increase of the allowed %d upper "
8245 "limit. See %<-fmax-array-constructor%> option.",
8246 nelem, &source->where, flag_max_array_constructor);
8247 return &gfc_bad_expr;
8249 else
8250 return NULL;
8253 if (source->expr_type == EXPR_CONSTANT
8254 || source->expr_type == EXPR_STRUCTURE)
8256 gcc_assert (dim == 0);
8258 result = gfc_get_array_expr (source->ts.type, source->ts.kind,
8259 &source->where);
8260 if (source->ts.type == BT_DERIVED)
8261 result->ts.u.derived = source->ts.u.derived;
8262 result->rank = 1;
8263 result->shape = gfc_get_shape (result->rank);
8264 mpz_init_set_si (result->shape[0], ncopies);
8266 for (i = 0; i < ncopies; ++i)
8267 gfc_constructor_append_expr (&result->value.constructor,
8268 gfc_copy_expr (source), NULL);
8270 else if (source->expr_type == EXPR_ARRAY)
8272 int offset, rstride[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS];
8273 gfc_constructor *source_ctor;
8275 gcc_assert (source->rank < GFC_MAX_DIMENSIONS);
8276 gcc_assert (dim >= 0 && dim <= source->rank);
8278 result = gfc_get_array_expr (source->ts.type, source->ts.kind,
8279 &source->where);
8280 if (source->ts.type == BT_DERIVED)
8281 result->ts.u.derived = source->ts.u.derived;
8282 result->rank = source->rank + 1;
8283 result->shape = gfc_get_shape (result->rank);
8285 for (i = 0, j = 0; i < result->rank; ++i)
8287 if (i != dim)
8288 mpz_init_set (result->shape[i], source->shape[j++]);
8289 else
8290 mpz_init_set_si (result->shape[i], ncopies);
8292 extent[i] = mpz_get_si (result->shape[i]);
8293 rstride[i] = (i == 0) ? 1 : rstride[i-1] * extent[i-1];
8296 offset = 0;
8297 for (source_ctor = gfc_constructor_first (source->value.constructor);
8298 source_ctor; source_ctor = gfc_constructor_next (source_ctor))
8300 for (i = 0; i < ncopies; ++i)
8301 gfc_constructor_insert_expr (&result->value.constructor,
8302 gfc_copy_expr (source_ctor->expr),
8303 NULL, offset + i * rstride[dim]);
8305 offset += (dim == 0 ? ncopies : 1);
8308 else
8310 gfc_error ("Simplification of SPREAD at %C not yet implemented");
8311 return &gfc_bad_expr;
8314 if (source->ts.type == BT_CHARACTER)
8315 result->ts.u.cl = source->ts.u.cl;
8317 return result;
8321 gfc_expr *
8322 gfc_simplify_sqrt (gfc_expr *e)
8324 gfc_expr *result = NULL;
8326 if (e->expr_type != EXPR_CONSTANT)
8327 return NULL;
8329 switch (e->ts.type)
8331 case BT_REAL:
8332 if (mpfr_cmp_si (e->value.real, 0) < 0)
8334 gfc_error ("Argument of SQRT at %L has a negative value",
8335 &e->where);
8336 return &gfc_bad_expr;
8338 result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
8339 mpfr_sqrt (result->value.real, e->value.real, GFC_RND_MODE);
8340 break;
8342 case BT_COMPLEX:
8343 gfc_set_model (e->value.real);
8345 result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
8346 mpc_sqrt (result->value.complex, e->value.complex, GFC_MPC_RND_MODE);
8347 break;
8349 default:
8350 gfc_internal_error ("invalid argument of SQRT at %L", &e->where);
8353 return range_check (result, "SQRT");
8357 gfc_expr *
8358 gfc_simplify_sum (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
8360 return simplify_transformation (array, dim, mask, 0, gfc_add);
8364 /* Simplify COTAN(X) where X has the unit of radian. */
8366 gfc_expr *
8367 gfc_simplify_cotan (gfc_expr *x)
8369 gfc_expr *result;
8370 mpc_t swp, *val;
8372 if (x->expr_type != EXPR_CONSTANT)
8373 return NULL;
8375 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
8377 switch (x->ts.type)
8379 case BT_REAL:
8380 mpfr_cot (result->value.real, x->value.real, GFC_RND_MODE);
8381 break;
8383 case BT_COMPLEX:
8384 /* There is no builtin mpc_cot, so compute cot = cos / sin. */
8385 val = &result->value.complex;
8386 mpc_init2 (swp, mpfr_get_default_prec ());
8387 mpc_sin_cos (*val, swp, x->value.complex, GFC_MPC_RND_MODE,
8388 GFC_MPC_RND_MODE);
8389 mpc_div (*val, swp, *val, GFC_MPC_RND_MODE);
8390 mpc_clear (swp);
8391 break;
8393 default:
8394 gcc_unreachable ();
8397 return range_check (result, "COTAN");
8401 gfc_expr *
8402 gfc_simplify_tan (gfc_expr *x)
8404 gfc_expr *result;
8406 if (x->expr_type != EXPR_CONSTANT)
8407 return NULL;
8409 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
8411 switch (x->ts.type)
8413 case BT_REAL:
8414 mpfr_tan (result->value.real, x->value.real, GFC_RND_MODE);
8415 break;
8417 case BT_COMPLEX:
8418 mpc_tan (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
8419 break;
8421 default:
8422 gcc_unreachable ();
8425 return range_check (result, "TAN");
8429 gfc_expr *
8430 gfc_simplify_tanh (gfc_expr *x)
8432 gfc_expr *result;
8434 if (x->expr_type != EXPR_CONSTANT)
8435 return NULL;
8437 result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
8439 switch (x->ts.type)
8441 case BT_REAL:
8442 mpfr_tanh (result->value.real, x->value.real, GFC_RND_MODE);
8443 break;
8445 case BT_COMPLEX:
8446 mpc_tanh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
8447 break;
8449 default:
8450 gcc_unreachable ();
8453 return range_check (result, "TANH");
8457 gfc_expr *
8458 gfc_simplify_tiny (gfc_expr *e)
8460 gfc_expr *result;
8461 int i;
8463 i = gfc_validate_kind (BT_REAL, e->ts.kind, false);
8465 result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
8466 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
8468 return result;
8472 gfc_expr *
8473 gfc_simplify_trailz (gfc_expr *e)
8475 unsigned long tz, bs;
8476 int i;
8478 if (e->expr_type != EXPR_CONSTANT)
8479 return NULL;
8481 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
8482 bs = gfc_integer_kinds[i].bit_size;
8483 tz = mpz_scan1 (e->value.integer, 0);
8485 return gfc_get_int_expr (gfc_default_integer_kind,
8486 &e->where, MIN (tz, bs));
8490 gfc_expr *
8491 gfc_simplify_transfer (gfc_expr *source, gfc_expr *mold, gfc_expr *size)
8493 gfc_expr *result;
8494 gfc_expr *mold_element;
8495 size_t source_size;
8496 size_t result_size;
8497 size_t buffer_size;
8498 mpz_t tmp;
8499 unsigned char *buffer;
8500 size_t result_length;
8502 if (!gfc_is_constant_expr (source) || !gfc_is_constant_expr (size))
8503 return NULL;
8505 if (!gfc_resolve_expr (mold))
8506 return NULL;
8507 if (gfc_init_expr_flag && !gfc_is_constant_expr (mold))
8508 return NULL;
8510 if (!gfc_calculate_transfer_sizes (source, mold, size, &source_size,
8511 &result_size, &result_length))
8512 return NULL;
8514 /* Calculate the size of the source. */
8515 if (source->expr_type == EXPR_ARRAY && !gfc_array_size (source, &tmp))
8516 gfc_internal_error ("Failure getting length of a constant array.");
8518 /* Create an empty new expression with the appropriate characteristics. */
8519 result = gfc_get_constant_expr (mold->ts.type, mold->ts.kind,
8520 &source->where);
8521 result->ts = mold->ts;
8523 mold_element = (mold->expr_type == EXPR_ARRAY && mold->value.constructor)
8524 ? gfc_constructor_first (mold->value.constructor)->expr
8525 : mold;
8527 /* Set result character length, if needed. Note that this needs to be
8528 set even for array expressions, in order to pass this information into
8529 gfc_target_interpret_expr. */
8530 if (result->ts.type == BT_CHARACTER && gfc_is_constant_expr (mold_element))
8532 result->value.character.length = mold_element->value.character.length;
8534 /* Let the typespec of the result inherit the string length.
8535 This is crucial if a resulting array has size zero. */
8536 if (mold_element->ts.u.cl->length)
8537 result->ts.u.cl->length = gfc_copy_expr (mold_element->ts.u.cl->length);
8538 else
8539 result->ts.u.cl->length =
8540 gfc_get_int_expr (gfc_charlen_int_kind, NULL,
8541 mold_element->value.character.length);
8544 /* Set the number of elements in the result, and determine its size. */
8546 if (mold->expr_type == EXPR_ARRAY || mold->rank || size)
8548 result->expr_type = EXPR_ARRAY;
8549 result->rank = 1;
8550 result->shape = gfc_get_shape (1);
8551 mpz_init_set_ui (result->shape[0], result_length);
8553 else
8554 result->rank = 0;
8556 /* Allocate the buffer to store the binary version of the source. */
8557 buffer_size = MAX (source_size, result_size);
8558 buffer = (unsigned char*)alloca (buffer_size);
8559 memset (buffer, 0, buffer_size);
8561 /* Now write source to the buffer. */
8562 gfc_target_encode_expr (source, buffer, buffer_size);
8564 /* And read the buffer back into the new expression. */
8565 gfc_target_interpret_expr (buffer, buffer_size, result, false);
8567 return result;
8571 gfc_expr *
8572 gfc_simplify_transpose (gfc_expr *matrix)
8574 int row, matrix_rows, col, matrix_cols;
8575 gfc_expr *result;
8577 if (!is_constant_array_expr (matrix))
8578 return NULL;
8580 gcc_assert (matrix->rank == 2);
8582 if (matrix->shape == NULL)
8583 return NULL;
8585 result = gfc_get_array_expr (matrix->ts.type, matrix->ts.kind,
8586 &matrix->where);
8587 result->rank = 2;
8588 result->shape = gfc_get_shape (result->rank);
8589 mpz_init_set (result->shape[0], matrix->shape[1]);
8590 mpz_init_set (result->shape[1], matrix->shape[0]);
8592 if (matrix->ts.type == BT_CHARACTER)
8593 result->ts.u.cl = matrix->ts.u.cl;
8594 else if (matrix->ts.type == BT_DERIVED)
8595 result->ts.u.derived = matrix->ts.u.derived;
8597 matrix_rows = mpz_get_si (matrix->shape[0]);
8598 matrix_cols = mpz_get_si (matrix->shape[1]);
8599 for (row = 0; row < matrix_rows; ++row)
8600 for (col = 0; col < matrix_cols; ++col)
8602 gfc_expr *e = gfc_constructor_lookup_expr (matrix->value.constructor,
8603 col * matrix_rows + row);
8604 gfc_constructor_insert_expr (&result->value.constructor,
8605 gfc_copy_expr (e), &matrix->where,
8606 row * matrix_cols + col);
8609 return result;
8613 gfc_expr *
8614 gfc_simplify_trim (gfc_expr *e)
8616 gfc_expr *result;
8617 int count, i, len, lentrim;
8619 if (e->expr_type != EXPR_CONSTANT)
8620 return NULL;
8622 len = e->value.character.length;
8623 for (count = 0, i = 1; i <= len; ++i)
8625 if (e->value.character.string[len - i] == ' ')
8626 count++;
8627 else
8628 break;
8631 lentrim = len - count;
8633 result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, lentrim);
8634 for (i = 0; i < lentrim; i++)
8635 result->value.character.string[i] = e->value.character.string[i];
8637 return result;
8641 gfc_expr *
8642 gfc_simplify_image_index (gfc_expr *coarray, gfc_expr *sub)
8644 gfc_expr *result;
8645 gfc_ref *ref;
8646 gfc_array_spec *as;
8647 gfc_constructor *sub_cons;
8648 bool first_image;
8649 int d;
8651 if (!is_constant_array_expr (sub))
8652 return NULL;
8654 /* Follow any component references. */
8655 as = coarray->symtree->n.sym->as;
8656 for (ref = coarray->ref; ref; ref = ref->next)
8657 if (ref->type == REF_COMPONENT)
8658 as = ref->u.ar.as;
8660 if (!as || as->type == AS_DEFERRED)
8661 return NULL;
8663 /* "valid sequence of cosubscripts" are required; thus, return 0 unless
8664 the cosubscript addresses the first image. */
8666 sub_cons = gfc_constructor_first (sub->value.constructor);
8667 first_image = true;
8669 for (d = 1; d <= as->corank; d++)
8671 gfc_expr *ca_bound;
8672 int cmp;
8674 gcc_assert (sub_cons != NULL);
8676 ca_bound = simplify_bound_dim (coarray, NULL, d + as->rank, 0, as,
8677 NULL, true);
8678 if (ca_bound == NULL)
8679 return NULL;
8681 if (ca_bound == &gfc_bad_expr)
8682 return ca_bound;
8684 cmp = mpz_cmp (ca_bound->value.integer, sub_cons->expr->value.integer);
8686 if (cmp == 0)
8688 gfc_free_expr (ca_bound);
8689 sub_cons = gfc_constructor_next (sub_cons);
8690 continue;
8693 first_image = false;
8695 if (cmp > 0)
8697 gfc_error ("Out of bounds in IMAGE_INDEX at %L for dimension %d, "
8698 "SUB has %ld and COARRAY lower bound is %ld)",
8699 &coarray->where, d,
8700 mpz_get_si (sub_cons->expr->value.integer),
8701 mpz_get_si (ca_bound->value.integer));
8702 gfc_free_expr (ca_bound);
8703 return &gfc_bad_expr;
8706 gfc_free_expr (ca_bound);
8708 /* Check whether upperbound is valid for the multi-images case. */
8709 if (d < as->corank)
8711 ca_bound = simplify_bound_dim (coarray, NULL, d + as->rank, 1, as,
8712 NULL, true);
8713 if (ca_bound == &gfc_bad_expr)
8714 return ca_bound;
8716 if (ca_bound && ca_bound->expr_type == EXPR_CONSTANT
8717 && mpz_cmp (ca_bound->value.integer,
8718 sub_cons->expr->value.integer) < 0)
8720 gfc_error ("Out of bounds in IMAGE_INDEX at %L for dimension %d, "
8721 "SUB has %ld and COARRAY upper bound is %ld)",
8722 &coarray->where, d,
8723 mpz_get_si (sub_cons->expr->value.integer),
8724 mpz_get_si (ca_bound->value.integer));
8725 gfc_free_expr (ca_bound);
8726 return &gfc_bad_expr;
8729 if (ca_bound)
8730 gfc_free_expr (ca_bound);
8733 sub_cons = gfc_constructor_next (sub_cons);
8736 gcc_assert (sub_cons == NULL);
8738 if (flag_coarray != GFC_FCOARRAY_SINGLE && !first_image)
8739 return NULL;
8741 result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
8742 &gfc_current_locus);
8743 if (first_image)
8744 mpz_set_si (result->value.integer, 1);
8745 else
8746 mpz_set_si (result->value.integer, 0);
8748 return result;
8751 gfc_expr *
8752 gfc_simplify_image_status (gfc_expr *image, gfc_expr *team ATTRIBUTE_UNUSED)
8754 if (flag_coarray == GFC_FCOARRAY_NONE)
8756 gfc_current_locus = *gfc_current_intrinsic_where;
8757 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
8758 return &gfc_bad_expr;
8761 /* Simplification is possible for fcoarray = single only. For all other modes
8762 the result depends on runtime conditions. */
8763 if (flag_coarray != GFC_FCOARRAY_SINGLE)
8764 return NULL;
8766 if (gfc_is_constant_expr (image))
8768 gfc_expr *result;
8769 result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
8770 &image->where);
8771 if (mpz_get_si (image->value.integer) == 1)
8772 mpz_set_si (result->value.integer, 0);
8773 else
8774 mpz_set_si (result->value.integer, GFC_STAT_STOPPED_IMAGE);
8775 return result;
8777 else
8778 return NULL;
8782 gfc_expr *
8783 gfc_simplify_this_image (gfc_expr *coarray, gfc_expr *dim,
8784 gfc_expr *distance ATTRIBUTE_UNUSED)
8786 if (flag_coarray != GFC_FCOARRAY_SINGLE)
8787 return NULL;
8789 /* If no coarray argument has been passed or when the first argument
8790 is actually a distance argument. */
8791 if (coarray == NULL || !gfc_is_coarray (coarray))
8793 gfc_expr *result;
8794 /* FIXME: gfc_current_locus is wrong. */
8795 result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
8796 &gfc_current_locus);
8797 mpz_set_si (result->value.integer, 1);
8798 return result;
8801 /* For -fcoarray=single, this_image(A) is the same as lcobound(A). */
8802 return simplify_cobound (coarray, dim, NULL, 0);
8806 gfc_expr *
8807 gfc_simplify_ubound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
8809 return simplify_bound (array, dim, kind, 1);
8812 gfc_expr *
8813 gfc_simplify_ucobound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
8815 return simplify_cobound (array, dim, kind, 1);
8819 gfc_expr *
8820 gfc_simplify_unpack (gfc_expr *vector, gfc_expr *mask, gfc_expr *field)
8822 gfc_expr *result, *e;
8823 gfc_constructor *vector_ctor, *mask_ctor, *field_ctor;
8825 if (!is_constant_array_expr (vector)
8826 || !is_constant_array_expr (mask)
8827 || (!gfc_is_constant_expr (field)
8828 && !is_constant_array_expr (field)))
8829 return NULL;
8831 result = gfc_get_array_expr (vector->ts.type, vector->ts.kind,
8832 &vector->where);
8833 if (vector->ts.type == BT_DERIVED)
8834 result->ts.u.derived = vector->ts.u.derived;
8835 result->rank = mask->rank;
8836 result->shape = gfc_copy_shape (mask->shape, mask->rank);
8838 if (vector->ts.type == BT_CHARACTER)
8839 result->ts.u.cl = vector->ts.u.cl;
8841 vector_ctor = gfc_constructor_first (vector->value.constructor);
8842 mask_ctor = gfc_constructor_first (mask->value.constructor);
8843 field_ctor
8844 = field->expr_type == EXPR_ARRAY
8845 ? gfc_constructor_first (field->value.constructor)
8846 : NULL;
8848 while (mask_ctor)
8850 if (mask_ctor->expr->value.logical)
8852 if (vector_ctor)
8854 e = gfc_copy_expr (vector_ctor->expr);
8855 vector_ctor = gfc_constructor_next (vector_ctor);
8857 else
8859 gfc_free_expr (result);
8860 return NULL;
8863 else if (field->expr_type == EXPR_ARRAY)
8865 if (field_ctor)
8866 e = gfc_copy_expr (field_ctor->expr);
8867 else
8869 /* Not enough elements in array FIELD. */
8870 gfc_free_expr (result);
8871 return &gfc_bad_expr;
8874 else
8875 e = gfc_copy_expr (field);
8877 gfc_constructor_append_expr (&result->value.constructor, e, NULL);
8879 mask_ctor = gfc_constructor_next (mask_ctor);
8880 field_ctor = gfc_constructor_next (field_ctor);
8883 return result;
8887 gfc_expr *
8888 gfc_simplify_verify (gfc_expr *s, gfc_expr *set, gfc_expr *b, gfc_expr *kind)
8890 gfc_expr *result;
8891 int back;
8892 size_t index, len, lenset;
8893 size_t i;
8894 int k = get_kind (BT_INTEGER, kind, "VERIFY", gfc_default_integer_kind);
8896 if (k == -1)
8897 return &gfc_bad_expr;
8899 if (s->expr_type != EXPR_CONSTANT || set->expr_type != EXPR_CONSTANT
8900 || ( b != NULL && b->expr_type != EXPR_CONSTANT))
8901 return NULL;
8903 if (b != NULL && b->value.logical != 0)
8904 back = 1;
8905 else
8906 back = 0;
8908 result = gfc_get_constant_expr (BT_INTEGER, k, &s->where);
8910 len = s->value.character.length;
8911 lenset = set->value.character.length;
8913 if (len == 0)
8915 mpz_set_ui (result->value.integer, 0);
8916 return result;
8919 if (back == 0)
8921 if (lenset == 0)
8923 mpz_set_ui (result->value.integer, 1);
8924 return result;
8927 index = wide_strspn (s->value.character.string,
8928 set->value.character.string) + 1;
8929 if (index > len)
8930 index = 0;
8933 else
8935 if (lenset == 0)
8937 mpz_set_ui (result->value.integer, len);
8938 return result;
8940 for (index = len; index > 0; index --)
8942 for (i = 0; i < lenset; i++)
8944 if (s->value.character.string[index - 1]
8945 == set->value.character.string[i])
8946 break;
8948 if (i == lenset)
8949 break;
8953 mpz_set_ui (result->value.integer, index);
8954 return result;
8958 gfc_expr *
8959 gfc_simplify_xor (gfc_expr *x, gfc_expr *y)
8961 gfc_expr *result;
8962 int kind;
8964 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
8965 return NULL;
8967 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
8969 switch (x->ts.type)
8971 case BT_INTEGER:
8972 result = gfc_get_constant_expr (BT_INTEGER, kind, &x->where);
8973 mpz_xor (result->value.integer, x->value.integer, y->value.integer);
8974 return range_check (result, "XOR");
8976 case BT_LOGICAL:
8977 return gfc_get_logical_expr (kind, &x->where,
8978 (x->value.logical && !y->value.logical)
8979 || (!x->value.logical && y->value.logical));
8981 default:
8982 gcc_unreachable ();
8987 /****************** Constant simplification *****************/
8989 /* Master function to convert one constant to another. While this is
8990 used as a simplification function, it requires the destination type
8991 and kind information which is supplied by a special case in
8992 do_simplify(). */
8994 gfc_expr *
8995 gfc_convert_constant (gfc_expr *e, bt type, int kind)
8997 gfc_expr *result, *(*f) (gfc_expr *, int);
8998 gfc_constructor *c, *t;
9000 switch (e->ts.type)
9002 case BT_INTEGER:
9003 switch (type)
9005 case BT_INTEGER:
9006 f = gfc_int2int;
9007 break;
9008 case BT_UNSIGNED:
9009 f = gfc_int2uint;
9010 break;
9011 case BT_REAL:
9012 f = gfc_int2real;
9013 break;
9014 case BT_COMPLEX:
9015 f = gfc_int2complex;
9016 break;
9017 case BT_LOGICAL:
9018 f = gfc_int2log;
9019 break;
9020 default:
9021 goto oops;
9023 break;
9025 case BT_UNSIGNED:
9026 switch (type)
9028 case BT_INTEGER:
9029 f = gfc_uint2int;
9030 break;
9031 case BT_UNSIGNED:
9032 f = gfc_uint2uint;
9033 break;
9034 case BT_REAL:
9035 f = gfc_uint2real;
9036 break;
9037 case BT_COMPLEX:
9038 f = gfc_uint2complex;
9039 break;
9040 case BT_LOGICAL:
9041 f = gfc_uint2log;
9042 break;
9043 default:
9044 goto oops;
9046 break;
9048 case BT_REAL:
9049 switch (type)
9051 case BT_INTEGER:
9052 f = gfc_real2int;
9053 break;
9054 case BT_UNSIGNED:
9055 f = gfc_real2uint;
9056 break;
9057 case BT_REAL:
9058 f = gfc_real2real;
9059 break;
9060 case BT_COMPLEX:
9061 f = gfc_real2complex;
9062 break;
9063 default:
9064 goto oops;
9066 break;
9068 case BT_COMPLEX:
9069 switch (type)
9071 case BT_INTEGER:
9072 f = gfc_complex2int;
9073 break;
9074 case BT_UNSIGNED:
9075 f = gfc_complex2uint;
9076 break;
9077 case BT_REAL:
9078 f = gfc_complex2real;
9079 break;
9080 case BT_COMPLEX:
9081 f = gfc_complex2complex;
9082 break;
9084 default:
9085 goto oops;
9087 break;
9089 case BT_LOGICAL:
9090 switch (type)
9092 case BT_INTEGER:
9093 f = gfc_log2int;
9094 break;
9095 case BT_UNSIGNED:
9096 f = gfc_log2uint;
9097 break;
9098 case BT_LOGICAL:
9099 f = gfc_log2log;
9100 break;
9101 default:
9102 goto oops;
9104 break;
9106 case BT_HOLLERITH:
9107 switch (type)
9109 case BT_INTEGER:
9110 f = gfc_hollerith2int;
9111 break;
9113 /* Hollerith is for legacy code, we do not currently support
9114 converting this to UNSIGNED. */
9115 case BT_UNSIGNED:
9116 goto oops;
9118 case BT_REAL:
9119 f = gfc_hollerith2real;
9120 break;
9122 case BT_COMPLEX:
9123 f = gfc_hollerith2complex;
9124 break;
9126 case BT_CHARACTER:
9127 f = gfc_hollerith2character;
9128 break;
9130 case BT_LOGICAL:
9131 f = gfc_hollerith2logical;
9132 break;
9134 default:
9135 goto oops;
9137 break;
9139 case BT_CHARACTER:
9140 switch (type)
9142 case BT_INTEGER:
9143 f = gfc_character2int;
9144 break;
9146 case BT_UNSIGNED:
9147 goto oops;
9149 case BT_REAL:
9150 f = gfc_character2real;
9151 break;
9153 case BT_COMPLEX:
9154 f = gfc_character2complex;
9155 break;
9157 case BT_CHARACTER:
9158 f = gfc_character2character;
9159 break;
9161 case BT_LOGICAL:
9162 f = gfc_character2logical;
9163 break;
9165 default:
9166 goto oops;
9168 break;
9170 default:
9171 oops:
9172 return &gfc_bad_expr;
9175 result = NULL;
9177 switch (e->expr_type)
9179 case EXPR_CONSTANT:
9180 result = f (e, kind);
9181 if (result == NULL)
9182 return &gfc_bad_expr;
9183 break;
9185 case EXPR_ARRAY:
9186 if (!gfc_is_constant_expr (e))
9187 break;
9189 result = gfc_get_array_expr (type, kind, &e->where);
9190 result->shape = gfc_copy_shape (e->shape, e->rank);
9191 result->rank = e->rank;
9193 for (c = gfc_constructor_first (e->value.constructor);
9194 c; c = gfc_constructor_next (c))
9196 gfc_expr *tmp;
9197 if (c->iterator == NULL)
9199 if (c->expr->expr_type == EXPR_ARRAY)
9200 tmp = gfc_convert_constant (c->expr, type, kind);
9201 else if (c->expr->expr_type == EXPR_OP)
9203 if (!gfc_simplify_expr (c->expr, 1))
9204 return &gfc_bad_expr;
9205 tmp = f (c->expr, kind);
9207 else
9208 tmp = f (c->expr, kind);
9210 else
9211 tmp = gfc_convert_constant (c->expr, type, kind);
9213 if (tmp == NULL || tmp == &gfc_bad_expr)
9215 gfc_free_expr (result);
9216 return NULL;
9219 t = gfc_constructor_append_expr (&result->value.constructor,
9220 tmp, &c->where);
9221 if (c->iterator)
9222 t->iterator = gfc_copy_iterator (c->iterator);
9225 break;
9227 default:
9228 break;
9231 return result;
9235 /* Function for converting character constants. */
9236 gfc_expr *
9237 gfc_convert_char_constant (gfc_expr *e, bt type ATTRIBUTE_UNUSED, int kind)
9239 gfc_expr *result;
9240 int i;
9242 if (!gfc_is_constant_expr (e))
9243 return NULL;
9245 if (e->expr_type == EXPR_CONSTANT)
9247 /* Simple case of a scalar. */
9248 result = gfc_get_constant_expr (BT_CHARACTER, kind, &e->where);
9249 if (result == NULL)
9250 return &gfc_bad_expr;
9252 result->value.character.length = e->value.character.length;
9253 result->value.character.string
9254 = gfc_get_wide_string (e->value.character.length + 1);
9255 memcpy (result->value.character.string, e->value.character.string,
9256 (e->value.character.length + 1) * sizeof (gfc_char_t));
9258 /* Check we only have values representable in the destination kind. */
9259 for (i = 0; i < result->value.character.length; i++)
9260 if (!gfc_check_character_range (result->value.character.string[i],
9261 kind))
9263 gfc_error ("Character %qs in string at %L cannot be converted "
9264 "into character kind %d",
9265 gfc_print_wide_char (result->value.character.string[i]),
9266 &e->where, kind);
9267 gfc_free_expr (result);
9268 return &gfc_bad_expr;
9271 return result;
9273 else if (e->expr_type == EXPR_ARRAY)
9275 /* For an array constructor, we convert each constructor element. */
9276 gfc_constructor *c;
9278 result = gfc_get_array_expr (type, kind, &e->where);
9279 result->shape = gfc_copy_shape (e->shape, e->rank);
9280 result->rank = e->rank;
9281 result->ts.u.cl = e->ts.u.cl;
9283 for (c = gfc_constructor_first (e->value.constructor);
9284 c; c = gfc_constructor_next (c))
9286 gfc_expr *tmp = gfc_convert_char_constant (c->expr, type, kind);
9287 if (tmp == &gfc_bad_expr)
9289 gfc_free_expr (result);
9290 return &gfc_bad_expr;
9293 if (tmp == NULL)
9295 gfc_free_expr (result);
9296 return NULL;
9299 gfc_constructor_append_expr (&result->value.constructor,
9300 tmp, &c->where);
9303 return result;
9305 else
9306 return NULL;
9310 gfc_expr *
9311 gfc_simplify_compiler_options (void)
9313 char *str;
9314 gfc_expr *result;
9316 str = gfc_get_option_string ();
9317 result = gfc_get_character_expr (gfc_default_character_kind,
9318 &gfc_current_locus, str, strlen (str));
9319 free (str);
9320 return result;
9324 gfc_expr *
9325 gfc_simplify_compiler_version (void)
9327 char *buffer;
9328 size_t len;
9330 len = strlen ("GCC version ") + strlen (version_string);
9331 buffer = XALLOCAVEC (char, len + 1);
9332 snprintf (buffer, len + 1, "GCC version %s", version_string);
9333 return gfc_get_character_expr (gfc_default_character_kind,
9334 &gfc_current_locus, buffer, len);
9337 /* Simplification routines for intrinsics of IEEE modules. */
9339 gfc_expr *
9340 simplify_ieee_selected_real_kind (gfc_expr *expr)
9342 gfc_actual_arglist *arg;
9343 gfc_expr *p = NULL, *q = NULL, *rdx = NULL;
9345 arg = expr->value.function.actual;
9346 p = arg->expr;
9347 if (arg->next)
9349 q = arg->next->expr;
9350 if (arg->next->next)
9351 rdx = arg->next->next->expr;
9354 /* Currently, if IEEE is supported and this module is built, it means
9355 all our floating-point types conform to IEEE. Hence, we simply handle
9356 IEEE_SELECTED_REAL_KIND like SELECTED_REAL_KIND. */
9357 return gfc_simplify_selected_real_kind (p, q, rdx);
9360 gfc_expr *
9361 simplify_ieee_support (gfc_expr *expr)
9363 /* We consider that if the IEEE modules are loaded, we have full support
9364 for flags, halting and rounding, which are the three functions
9365 (IEEE_SUPPORT_{FLAG,HALTING,ROUNDING}) allowed in constant
9366 expressions. One day, we will need libgfortran to detect support and
9367 communicate it back to us, allowing for partial support. */
9369 return gfc_get_logical_expr (gfc_default_logical_kind, &expr->where,
9370 true);
9373 bool
9374 matches_ieee_function_name (gfc_symbol *sym, const char *name)
9376 int n = strlen(name);
9378 if (!strncmp(sym->name, name, n))
9379 return true;
9381 /* If a generic was used and renamed, we need more work to find out.
9382 Compare the specific name. */
9383 if (sym->generic && !strncmp(sym->generic->sym->name, name, n))
9384 return true;
9386 return false;
9389 gfc_expr *
9390 gfc_simplify_ieee_functions (gfc_expr *expr)
9392 gfc_symbol* sym = expr->symtree->n.sym;
9394 if (matches_ieee_function_name(sym, "ieee_selected_real_kind"))
9395 return simplify_ieee_selected_real_kind (expr);
9396 else if (matches_ieee_function_name(sym, "ieee_support_flag")
9397 || matches_ieee_function_name(sym, "ieee_support_halting")
9398 || matches_ieee_function_name(sym, "ieee_support_rounding"))
9399 return simplify_ieee_support (expr);
9400 else
9401 return NULL;