Branch data Line data Source code
1 : : /* Simplify intrinsic functions at compile-time.
2 : : Copyright (C) 2000-2025 Free Software Foundation, Inc.
3 : : Contributed by Andy Vaught & Katherine Holcomb
4 : :
5 : : This file is part of GCC.
6 : :
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.
11 : :
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.
16 : :
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/>. */
20 : :
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. */
32 : :
33 : : /* Prototypes. */
34 : :
35 : : static int min_max_choose (gfc_expr *, gfc_expr *, int, bool back_val = false);
36 : :
37 : : gfc_expr gfc_bad_expr;
38 : :
39 : : static gfc_expr *simplify_size (gfc_expr *, gfc_expr *, int);
40 : :
41 : :
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.
45 : :
46 : : The return convention is that each simplification function returns:
47 : :
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.
51 : :
52 : : NULL pointer indicating that no simplification was possible and
53 : : the original expression should remain intact.
54 : :
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
59 : :
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.
66 : :
67 : : Array arguments are only passed to these subroutines that implement
68 : : the simplification of transformational intrinsics.
69 : :
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. */
73 : :
74 : : /* Range checks an expression node. If all goes well, returns the
75 : : node, otherwise returns &gfc_bad_expr and frees the node. */
76 : :
77 : : static gfc_expr *
78 : 339133 : range_check (gfc_expr *result, const char *name)
79 : : {
80 : 339133 : if (result == NULL)
81 : : return &gfc_bad_expr;
82 : :
83 : 339133 : if (result->expr_type != EXPR_CONSTANT)
84 : : return result;
85 : :
86 : 339113 : switch (gfc_range_check (result))
87 : : {
88 : : case ARITH_OK:
89 : : return result;
90 : :
91 : 5 : case ARITH_OVERFLOW:
92 : 5 : gfc_error ("Result of %s overflows its kind at %L", name,
93 : : &result->where);
94 : 5 : break;
95 : :
96 : 0 : case ARITH_UNDERFLOW:
97 : 0 : gfc_error ("Result of %s underflows its kind at %L", name,
98 : : &result->where);
99 : 0 : break;
100 : :
101 : 0 : case ARITH_NAN:
102 : 0 : gfc_error ("Result of %s is NaN at %L", name, &result->where);
103 : 0 : break;
104 : :
105 : 0 : default:
106 : 0 : gfc_error ("Result of %s gives range error for its kind at %L", name,
107 : : &result->where);
108 : 0 : break;
109 : : }
110 : :
111 : 5 : gfc_free_expr (result);
112 : 5 : return &gfc_bad_expr;
113 : : }
114 : :
115 : :
116 : : /* A helper function that gets an optional and possibly missing
117 : : kind parameter. Returns the kind, -1 if something went wrong. */
118 : :
119 : : static int
120 : 148252 : get_kind (bt type, gfc_expr *k, const char *name, int default_kind)
121 : : {
122 : 148252 : int kind;
123 : :
124 : 148252 : if (k == NULL)
125 : : return default_kind;
126 : :
127 : 31479 : if (k->expr_type != EXPR_CONSTANT)
128 : : {
129 : 0 : gfc_error ("KIND parameter of %s at %L must be an initialization "
130 : : "expression", name, &k->where);
131 : 0 : return -1;
132 : : }
133 : :
134 : 31479 : if (gfc_extract_int (k, &kind)
135 : 31479 : || gfc_validate_kind (type, kind, true) < 0)
136 : : {
137 : 0 : gfc_error ("Invalid KIND parameter of %s at %L", name, &k->where);
138 : 0 : return -1;
139 : : }
140 : :
141 : 31479 : return kind;
142 : : }
143 : :
144 : :
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. */
149 : :
150 : : void
151 : 104994 : gfc_convert_mpz_to_unsigned (mpz_t x, int bitsize, bool sign)
152 : : {
153 : 104994 : mpz_t mask;
154 : :
155 : 104994 : if (mpz_sgn (x) < 0)
156 : : {
157 : : /* Confirm that no bits above the signed range are unset if we
158 : : are doing range checking. */
159 : 720 : if (sign && flag_range_check != 0)
160 : 720 : gcc_assert (mpz_scan0 (x, bitsize-1) == ULONG_MAX);
161 : :
162 : 720 : mpz_init_set_ui (mask, 1);
163 : 720 : mpz_mul_2exp (mask, mask, bitsize);
164 : 720 : mpz_sub_ui (mask, mask, 1);
165 : :
166 : 720 : mpz_and (x, x, mask);
167 : :
168 : 720 : mpz_clear (mask);
169 : : }
170 : : else
171 : : {
172 : : /* Confirm that no bits above the signed range are set if we
173 : : are doing range checking. */
174 : 104274 : if (sign && flag_range_check != 0)
175 : 2794 : gcc_assert (mpz_scan1 (x, bitsize-1) == ULONG_MAX);
176 : : }
177 : 104994 : }
178 : :
179 : :
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. */
184 : :
185 : : void
186 : 8937 : gfc_convert_mpz_to_signed (mpz_t x, int bitsize)
187 : : {
188 : 8937 : mpz_t mask;
189 : :
190 : : /* Confirm that no bits above the unsigned range are set if we are
191 : : doing range checking. */
192 : 8937 : if (flag_range_check != 0)
193 : 8805 : gcc_assert (mpz_scan1 (x, bitsize) == ULONG_MAX);
194 : :
195 : 8937 : if (mpz_tstbit (x, bitsize - 1) == 1)
196 : : {
197 : 1788 : mpz_init_set_ui (mask, 1);
198 : 1788 : mpz_mul_2exp (mask, mask, bitsize);
199 : 1788 : mpz_sub_ui (mask, mask, 1);
200 : :
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 : 1788 : mpz_com (x, x);
206 : 1788 : mpz_add_ui (x, x, 1);
207 : 1788 : mpz_and (x, x, mask);
208 : :
209 : 1788 : mpz_neg (x, x);
210 : :
211 : 1788 : mpz_clear (mask);
212 : : }
213 : 8937 : }
214 : :
215 : :
216 : : /* Test that the expression is a constant array, simplifying if
217 : : we are dealing with a parameter array. */
218 : :
219 : : static bool
220 : 128695 : is_constant_array_expr (gfc_expr *e)
221 : : {
222 : 128695 : gfc_constructor *c;
223 : 128695 : bool array_OK = true;
224 : 128695 : mpz_t size;
225 : :
226 : 128695 : if (e == NULL)
227 : : return true;
228 : :
229 : 116864 : if (e->expr_type == EXPR_VARIABLE && e->rank > 0
230 : 45364 : && e->symtree->n.sym->attr.flavor == FL_PARAMETER)
231 : 3310 : gfc_simplify_expr (e, 1);
232 : :
233 : 116864 : if (e->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (e))
234 : 89874 : return false;
235 : :
236 : : /* A non-zero-sized constant array shall have a non-empty constructor. */
237 : 26990 : if (e->rank > 0 && e->shape != NULL && e->value.constructor == NULL)
238 : : {
239 : 1219 : mpz_init_set_ui (size, 1);
240 : 2648 : for (int j = 0; j < e->rank; j++)
241 : 1429 : mpz_mul (size, size, e->shape[j]);
242 : 1219 : bool not_size0 = (mpz_cmp_si (size, 0) != 0);
243 : 1219 : mpz_clear (size);
244 : 1219 : if (not_size0)
245 : : return false;
246 : : }
247 : :
248 : 26987 : for (c = gfc_constructor_first (e->value.constructor);
249 : 470163 : c; c = gfc_constructor_next (c))
250 : 443283 : if (c->expr->expr_type != EXPR_CONSTANT
251 : 1045 : && c->expr->expr_type != EXPR_STRUCTURE)
252 : : {
253 : : array_OK = false;
254 : : break;
255 : : }
256 : :
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 : 26987 : bool expand;
260 : 53974 : expand = (e->rank == 1
261 : 26068 : && e->shape
262 : 53048 : && (mpz_cmp_ui (e->shape[0], flag_max_array_constructor) < 0));
263 : :
264 : 26987 : if (!array_OK && (gfc_init_expr_flag || expand) && e->rank == 1)
265 : : {
266 : 101 : bool saved_init_expr_flag = gfc_init_expr_flag;
267 : 101 : array_OK = gfc_reduce_init_expr (e);
268 : : /* gfc_reduce_init_expr resets the flag. */
269 : 101 : gfc_init_expr_flag = saved_init_expr_flag;
270 : : }
271 : : else
272 : : return array_OK;
273 : :
274 : : /* Recheck to make sure that any EXPR_ARRAYs have gone. */
275 : 101 : for (c = gfc_constructor_first (e->value.constructor);
276 : 1446 : c; c = gfc_constructor_next (c))
277 : 1349 : if (c->expr->expr_type != EXPR_CONSTANT
278 : 4 : && c->expr->expr_type != EXPR_STRUCTURE)
279 : : return false;
280 : :
281 : : /* Make sure that the array has a valid shape. */
282 : 97 : if (e->shape == NULL && e->rank == 1)
283 : : {
284 : 0 : if (!gfc_array_size(e, &size))
285 : : return false;
286 : 0 : e->shape = gfc_get_shape (1);
287 : 0 : mpz_init_set (e->shape[0], size);
288 : 0 : mpz_clear (size);
289 : : }
290 : :
291 : : return array_OK;
292 : : }
293 : :
294 : : bool
295 : 10949 : gfc_is_constant_array_expr (gfc_expr *e)
296 : : {
297 : 10949 : return is_constant_array_expr (e);
298 : : }
299 : :
300 : :
301 : : /* Test for a size zero array. */
302 : : bool
303 : 161998 : gfc_is_size_zero_array (gfc_expr *array)
304 : : {
305 : :
306 : 161998 : if (array->rank == 0)
307 : : return false;
308 : :
309 : 158261 : if (array->expr_type == EXPR_VARIABLE && array->rank > 0
310 : 21970 : && array->symtree->n.sym->attr.flavor == FL_PARAMETER
311 : 10966 : && array->shape != NULL)
312 : : {
313 : 22478 : for (int i = 0; i < array->rank; i++)
314 : 12673 : if (mpz_cmp_si (array->shape[i], 0) <= 0)
315 : : return true;
316 : :
317 : : return false;
318 : : }
319 : :
320 : 147549 : if (array->expr_type == EXPR_ARRAY)
321 : 93580 : return array->value.constructor == NULL;
322 : :
323 : : return false;
324 : : }
325 : :
326 : :
327 : : /* Initialize a transformational result expression with a given value. */
328 : :
329 : : static void
330 : 3947 : init_result_expr (gfc_expr *e, int init, gfc_expr *array)
331 : : {
332 : 3947 : if (e && e->expr_type == EXPR_ARRAY)
333 : : {
334 : 225 : gfc_constructor *ctor = gfc_constructor_first (e->value.constructor);
335 : 1049 : while (ctor)
336 : : {
337 : 599 : init_result_expr (ctor->expr, init, array);
338 : 599 : ctor = gfc_constructor_next (ctor);
339 : : }
340 : : }
341 : 3722 : else if (e && e->expr_type == EXPR_CONSTANT)
342 : : {
343 : 3722 : int i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
344 : 3722 : HOST_WIDE_INT length;
345 : 3722 : gfc_char_t *string;
346 : :
347 : 3722 : switch (e->ts.type)
348 : : {
349 : 2147 : case BT_LOGICAL:
350 : 2147 : e->value.logical = (init ? 1 : 0);
351 : 2147 : break;
352 : :
353 : 1017 : case BT_INTEGER:
354 : 1017 : if (init == INT_MIN)
355 : 144 : mpz_set (e->value.integer, gfc_integer_kinds[i].min_int);
356 : 873 : else if (init == INT_MAX)
357 : 158 : mpz_set (e->value.integer, gfc_integer_kinds[i].huge);
358 : : else
359 : 715 : mpz_set_si (e->value.integer, init);
360 : : break;
361 : :
362 : 186 : case BT_UNSIGNED:
363 : 186 : if (init == INT_MIN)
364 : 48 : mpz_set_ui (e->value.integer, 0);
365 : 138 : else if (init == INT_MAX)
366 : 48 : mpz_set (e->value.integer, gfc_unsigned_kinds[i].huge);
367 : : else
368 : 90 : mpz_set_ui (e->value.integer, init);
369 : : break;
370 : :
371 : 280 : case BT_REAL:
372 : 280 : if (init == INT_MIN)
373 : : {
374 : 26 : mpfr_set (e->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
375 : 26 : mpfr_neg (e->value.real, e->value.real, GFC_RND_MODE);
376 : : }
377 : 254 : else if (init == INT_MAX)
378 : 27 : mpfr_set (e->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
379 : : else
380 : 227 : mpfr_set_si (e->value.real, init, GFC_RND_MODE);
381 : : break;
382 : :
383 : 48 : case BT_COMPLEX:
384 : 48 : mpc_set_si (e->value.complex, init, GFC_MPC_RND_MODE);
385 : 48 : break;
386 : :
387 : 44 : case BT_CHARACTER:
388 : 44 : if (init == INT_MIN)
389 : : {
390 : 22 : gfc_expr *len = gfc_simplify_len (array, NULL);
391 : 22 : gfc_extract_hwi (len, &length);
392 : 22 : string = gfc_get_wide_string (length + 1);
393 : 22 : gfc_wide_memset (string, 0, length);
394 : : }
395 : 22 : else if (init == INT_MAX)
396 : : {
397 : 22 : gfc_expr *len = gfc_simplify_len (array, NULL);
398 : 22 : gfc_extract_hwi (len, &length);
399 : 22 : string = gfc_get_wide_string (length + 1);
400 : 22 : gfc_wide_memset (string, 255, length);
401 : : }
402 : : else
403 : : {
404 : 0 : length = 0;
405 : 0 : string = gfc_get_wide_string (1);
406 : : }
407 : :
408 : 44 : string[length] = '\0';
409 : 44 : e->value.character.length = length;
410 : 44 : e->value.character.string = string;
411 : 44 : break;
412 : :
413 : 0 : default:
414 : 0 : gcc_unreachable();
415 : : }
416 : 3722 : }
417 : : else
418 : 0 : gcc_unreachable();
419 : 3947 : }
420 : :
421 : :
422 : : /* Helper function for gfc_simplify_dot_product() and gfc_simplify_matmul;
423 : : if conj_a is true, the matrix_a is complex conjugated. */
424 : :
425 : : static gfc_expr *
426 : 458 : 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)
429 : : {
430 : 458 : gfc_expr *result, *a, *b, *c;
431 : :
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 : 458 : if (matrix_a->ts.type == BT_LOGICAL)
437 : 0 : result = gfc_get_logical_expr (gfc_default_logical_kind, NULL, false);
438 : 458 : else if (matrix_a->ts.type == BT_UNSIGNED)
439 : : {
440 : 60 : int kind = MAX (matrix_a->ts.kind, matrix_b->ts.kind);
441 : 60 : result = gfc_get_unsigned_expr (kind, NULL, 0);
442 : : }
443 : : else
444 : 398 : result = gfc_get_int_expr (1, NULL, 0);
445 : :
446 : 458 : result->where = matrix_a->where;
447 : :
448 : 458 : a = gfc_constructor_lookup_expr (matrix_a->value.constructor, offset_a);
449 : 458 : b = gfc_constructor_lookup_expr (matrix_b->value.constructor, offset_b);
450 : 2050 : while (a && b)
451 : : {
452 : : /* Copying of expressions is required as operands are free'd
453 : : by the gfc_arith routines. */
454 : 1134 : switch (result->ts.type)
455 : : {
456 : 0 : case BT_LOGICAL:
457 : 0 : result = gfc_or (result,
458 : : gfc_and (gfc_copy_expr (a),
459 : : gfc_copy_expr (b)));
460 : 0 : break;
461 : :
462 : 1134 : case BT_INTEGER:
463 : 1134 : case BT_REAL:
464 : 1134 : case BT_COMPLEX:
465 : 1134 : case BT_UNSIGNED:
466 : 1134 : if (conj_a && a->ts.type == BT_COMPLEX)
467 : 2 : c = gfc_simplify_conjg (a);
468 : : else
469 : 1132 : c = gfc_copy_expr (a);
470 : 1134 : result = gfc_add (result, gfc_multiply (c, gfc_copy_expr (b)));
471 : 1134 : break;
472 : :
473 : 0 : default:
474 : 0 : gcc_unreachable();
475 : : }
476 : :
477 : 1134 : offset_a += stride_a;
478 : 1134 : a = gfc_constructor_lookup_expr (matrix_a->value.constructor, offset_a);
479 : :
480 : 1134 : offset_b += stride_b;
481 : 1134 : b = gfc_constructor_lookup_expr (matrix_b->value.constructor, offset_b);
482 : : }
483 : :
484 : 458 : return result;
485 : : }
486 : :
487 : :
488 : : /* Build a result expression for transformational intrinsics,
489 : : depending on DIM. */
490 : :
491 : : static gfc_expr *
492 : 3149 : transformational_result (gfc_expr *array, gfc_expr *dim, bt type,
493 : : int kind, locus* where)
494 : : {
495 : 3149 : gfc_expr *result;
496 : 3149 : int i, nelem;
497 : :
498 : 3149 : if (!dim || array->rank == 1)
499 : 2924 : return gfc_get_constant_expr (type, kind, where);
500 : :
501 : 225 : result = gfc_get_array_expr (type, kind, where);
502 : 225 : result->shape = gfc_copy_shape_excluding (array->shape, array->rank, dim);
503 : 225 : result->rank = array->rank - 1;
504 : :
505 : : /* gfc_array_size() would count the number of elements in the constructor,
506 : : we have not built those yet. */
507 : 225 : nelem = 1;
508 : 450 : for (i = 0; i < result->rank; ++i)
509 : 230 : nelem *= mpz_get_ui (result->shape[i]);
510 : :
511 : 824 : for (i = 0; i < nelem; ++i)
512 : : {
513 : 599 : gfc_constructor_append_expr (&result->value.constructor,
514 : : gfc_get_constant_expr (type, kind, where),
515 : : NULL);
516 : : }
517 : :
518 : : return result;
519 : : }
520 : :
521 : :
522 : : typedef gfc_expr* (*transformational_op)(gfc_expr*, gfc_expr*);
523 : :
524 : : /* Wrapper function, implements 'op1 += 1'. Only called if MASK
525 : : of COUNT intrinsic is .TRUE..
526 : :
527 : : Interface and implementation mimics arith functions as
528 : : gfc_add, gfc_multiply, etc. */
529 : :
530 : : static gfc_expr *
531 : 108 : gfc_count (gfc_expr *op1, gfc_expr *op2)
532 : : {
533 : 108 : gfc_expr *result;
534 : :
535 : 108 : gcc_assert (op1->ts.type == BT_INTEGER);
536 : 108 : gcc_assert (op2->ts.type == BT_LOGICAL);
537 : 108 : gcc_assert (op2->value.logical);
538 : :
539 : 108 : result = gfc_copy_expr (op1);
540 : 108 : mpz_add_ui (result->value.integer, result->value.integer, 1);
541 : :
542 : 108 : gfc_free_expr (op1);
543 : 108 : gfc_free_expr (op2);
544 : 108 : return result;
545 : : }
546 : :
547 : :
548 : : /* Transforms an ARRAY with operation OP, according to MASK, to a
549 : : scalar RESULT. E.g. called if
550 : :
551 : : REAL, PARAMETER :: array(n, m) = ...
552 : : REAL, PARAMETER :: s = SUM(array)
553 : :
554 : : where OP == gfc_add(). */
555 : :
556 : : static gfc_expr *
557 : 2508 : simplify_transformation_to_scalar (gfc_expr *result, gfc_expr *array, gfc_expr *mask,
558 : : transformational_op op)
559 : : {
560 : 2508 : gfc_expr *a, *m;
561 : 2508 : gfc_constructor *array_ctor, *mask_ctor;
562 : :
563 : : /* Shortcut for constant .FALSE. MASK. */
564 : 2508 : if (mask
565 : 98 : && mask->expr_type == EXPR_CONSTANT
566 : 24 : && !mask->value.logical)
567 : : return result;
568 : :
569 : 2484 : array_ctor = gfc_constructor_first (array->value.constructor);
570 : 2484 : mask_ctor = NULL;
571 : 2484 : if (mask && mask->expr_type == EXPR_ARRAY)
572 : 74 : mask_ctor = gfc_constructor_first (mask->value.constructor);
573 : :
574 : 70940 : while (array_ctor)
575 : : {
576 : 68456 : a = array_ctor->expr;
577 : 68456 : array_ctor = gfc_constructor_next (array_ctor);
578 : :
579 : : /* A constant MASK equals .TRUE. here and can be ignored. */
580 : 68456 : if (mask_ctor)
581 : : {
582 : 430 : m = mask_ctor->expr;
583 : 430 : mask_ctor = gfc_constructor_next (mask_ctor);
584 : 430 : if (!m->value.logical)
585 : 304 : continue;
586 : : }
587 : :
588 : 68152 : result = op (result, gfc_copy_expr (a));
589 : 68152 : if (!result)
590 : : return result;
591 : : }
592 : :
593 : : return result;
594 : : }
595 : :
596 : : /* Transforms an ARRAY with operation OP, according to MASK, to an
597 : : array RESULT. E.g. called if
598 : :
599 : : REAL, PARAMETER :: array(n, m) = ...
600 : : REAL, PARAMETER :: s(n) = PROD(array, DIM=1)
601 : :
602 : : where OP == gfc_multiply().
603 : : The result might be post processed using post_op. */
604 : :
605 : : static gfc_expr *
606 : 150 : 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)
609 : : {
610 : 150 : mpz_t size;
611 : 150 : int done, i, n, arraysize, resultsize, dim_index, dim_extent, dim_stride;
612 : 150 : gfc_expr **arrayvec, **resultvec, **base, **src, **dest;
613 : 150 : gfc_constructor *array_ctor, *mask_ctor, *result_ctor;
614 : :
615 : 150 : int count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS],
616 : : sstride[GFC_MAX_DIMENSIONS], dstride[GFC_MAX_DIMENSIONS],
617 : : tmpstride[GFC_MAX_DIMENSIONS];
618 : :
619 : : /* Shortcut for constant .FALSE. MASK. */
620 : 150 : if (mask
621 : 16 : && mask->expr_type == EXPR_CONSTANT
622 : 0 : && !mask->value.logical)
623 : : return result;
624 : :
625 : : /* Build an indexed table for array element expressions to minimize
626 : : linked-list traversal. Masked elements are set to NULL. */
627 : 150 : gfc_array_size (array, &size);
628 : 150 : arraysize = mpz_get_ui (size);
629 : 150 : mpz_clear (size);
630 : :
631 : 150 : arrayvec = XCNEWVEC (gfc_expr*, arraysize);
632 : :
633 : 150 : array_ctor = gfc_constructor_first (array->value.constructor);
634 : 150 : mask_ctor = NULL;
635 : 150 : if (mask && mask->expr_type == EXPR_ARRAY)
636 : 16 : mask_ctor = gfc_constructor_first (mask->value.constructor);
637 : :
638 : 1174 : for (i = 0; i < arraysize; ++i)
639 : : {
640 : 1024 : arrayvec[i] = array_ctor->expr;
641 : 1024 : array_ctor = gfc_constructor_next (array_ctor);
642 : :
643 : 1024 : if (mask_ctor)
644 : : {
645 : 156 : if (!mask_ctor->expr->value.logical)
646 : 83 : arrayvec[i] = NULL;
647 : :
648 : 156 : mask_ctor = gfc_constructor_next (mask_ctor);
649 : : }
650 : : }
651 : :
652 : : /* Same for the result expression. */
653 : 150 : gfc_array_size (result, &size);
654 : 150 : resultsize = mpz_get_ui (size);
655 : 150 : mpz_clear (size);
656 : :
657 : 150 : resultvec = XCNEWVEC (gfc_expr*, resultsize);
658 : 150 : result_ctor = gfc_constructor_first (result->value.constructor);
659 : 696 : for (i = 0; i < resultsize; ++i)
660 : : {
661 : 396 : resultvec[i] = result_ctor->expr;
662 : 396 : result_ctor = gfc_constructor_next (result_ctor);
663 : : }
664 : :
665 : 150 : gfc_extract_int (dim, &dim_index);
666 : 150 : dim_index -= 1; /* zero-base index */
667 : 150 : dim_extent = 0;
668 : 150 : dim_stride = 0;
669 : :
670 : 450 : for (i = 0, n = 0; i < array->rank; ++i)
671 : : {
672 : 300 : count[i] = 0;
673 : 300 : tmpstride[i] = (i == 0) ? 1 : tmpstride[i-1] * mpz_get_si (array->shape[i-1]);
674 : 300 : if (i == dim_index)
675 : : {
676 : 150 : dim_extent = mpz_get_si (array->shape[i]);
677 : 150 : dim_stride = tmpstride[i];
678 : 150 : continue;
679 : : }
680 : :
681 : 150 : extent[n] = mpz_get_si (array->shape[i]);
682 : 150 : sstride[n] = tmpstride[i];
683 : 150 : dstride[n] = (n == 0) ? 1 : dstride[n-1] * extent[n-1];
684 : 150 : n += 1;
685 : : }
686 : :
687 : 150 : done = resultsize <= 0;
688 : 150 : base = arrayvec;
689 : 150 : dest = resultvec;
690 : 696 : while (!done)
691 : : {
692 : 1420 : for (src = base, n = 0; n < dim_extent; src += dim_stride, ++n)
693 : 1024 : if (*src)
694 : 941 : *dest = op (*dest, gfc_copy_expr (*src));
695 : :
696 : 396 : if (post_op)
697 : 2 : *dest = post_op (*dest, *dest);
698 : :
699 : 396 : count[0]++;
700 : 396 : base += sstride[0];
701 : 396 : dest += dstride[0];
702 : :
703 : 396 : n = 0;
704 : 396 : while (!done && count[n] == extent[n])
705 : : {
706 : 150 : count[n] = 0;
707 : 150 : base -= sstride[n] * extent[n];
708 : 150 : dest -= dstride[n] * extent[n];
709 : :
710 : 150 : n++;
711 : 150 : if (n < result->rank)
712 : : {
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 : 0 : GCC_DIAGNOSTIC_PUSH_IGNORED (-Warray-bounds)
720 : 0 : count[n]++;
721 : 0 : base += sstride[n];
722 : 0 : dest += dstride[n];
723 : 0 : GCC_DIAGNOSTIC_POP
724 : : }
725 : : else
726 : : done = true;
727 : : }
728 : : }
729 : :
730 : : /* Place updated expression in result constructor. */
731 : 150 : result_ctor = gfc_constructor_first (result->value.constructor);
732 : 696 : for (i = 0; i < resultsize; ++i)
733 : : {
734 : 396 : result_ctor->expr = resultvec[i];
735 : 396 : result_ctor = gfc_constructor_next (result_ctor);
736 : : }
737 : :
738 : 150 : free (arrayvec);
739 : 150 : free (resultvec);
740 : 150 : return result;
741 : : }
742 : :
743 : :
744 : : static gfc_expr *
745 : 56598 : simplify_transformation (gfc_expr *array, gfc_expr *dim, gfc_expr *mask,
746 : : int init_val, transformational_op op)
747 : : {
748 : 56598 : gfc_expr *result;
749 : 56598 : bool size_zero;
750 : :
751 : 56598 : size_zero = gfc_is_size_zero_array (array);
752 : :
753 : 110198 : if (!(is_constant_array_expr (array) || size_zero)
754 : 2998 : || array->shape == NULL
755 : 59593 : || !gfc_is_constant_expr (dim))
756 : 53603 : return NULL;
757 : :
758 : 2995 : if (mask
759 : 242 : && !is_constant_array_expr (mask)
760 : 3177 : && mask->expr_type != EXPR_CONSTANT)
761 : : return NULL;
762 : :
763 : 2837 : result = transformational_result (array, dim, array->ts.type,
764 : : array->ts.kind, &array->where);
765 : 2837 : init_result_expr (result, init_val, array);
766 : :
767 : 2837 : if (size_zero)
768 : : return result;
769 : :
770 : 2590 : return !dim || array->rank == 1 ?
771 : 2447 : simplify_transformation_to_scalar (result, array, mask, op) :
772 : 2590 : simplify_transformation_to_array (result, array, dim, mask, op, NULL);
773 : : }
774 : :
775 : :
776 : : /********************** Simplification functions *****************************/
777 : :
778 : : gfc_expr *
779 : 24897 : gfc_simplify_abs (gfc_expr *e)
780 : : {
781 : 24897 : gfc_expr *result;
782 : :
783 : 24897 : if (e->expr_type != EXPR_CONSTANT)
784 : : return NULL;
785 : :
786 : 980 : switch (e->ts.type)
787 : : {
788 : 36 : case BT_INTEGER:
789 : 36 : result = gfc_get_constant_expr (BT_INTEGER, e->ts.kind, &e->where);
790 : 36 : mpz_abs (result->value.integer, e->value.integer);
791 : 36 : return range_check (result, "IABS");
792 : :
793 : 782 : case BT_REAL:
794 : 782 : result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
795 : 782 : mpfr_abs (result->value.real, e->value.real, GFC_RND_MODE);
796 : 782 : return range_check (result, "ABS");
797 : :
798 : 162 : case BT_COMPLEX:
799 : 162 : gfc_set_model_kind (e->ts.kind);
800 : 162 : result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
801 : 162 : mpc_abs (result->value.real, e->value.complex, GFC_RND_MODE);
802 : 162 : return range_check (result, "CABS");
803 : :
804 : 0 : default:
805 : 0 : gfc_internal_error ("gfc_simplify_abs(): Bad type");
806 : : }
807 : : }
808 : :
809 : :
810 : : static gfc_expr *
811 : 21912 : simplify_achar_char (gfc_expr *e, gfc_expr *k, const char *name, bool ascii)
812 : : {
813 : 21912 : gfc_expr *result;
814 : 21912 : int kind;
815 : 21912 : bool too_large = false;
816 : :
817 : 21912 : if (e->expr_type != EXPR_CONSTANT)
818 : : return NULL;
819 : :
820 : 14524 : kind = get_kind (BT_CHARACTER, k, name, gfc_default_character_kind);
821 : 14524 : if (kind == -1)
822 : : return &gfc_bad_expr;
823 : :
824 : 14524 : if (mpz_cmp_si (e->value.integer, 0) < 0)
825 : : {
826 : 8 : gfc_error ("Argument of %s function at %L is negative", name,
827 : : &e->where);
828 : 8 : return &gfc_bad_expr;
829 : : }
830 : :
831 : 14516 : if (ascii && warn_surprising && mpz_cmp_si (e->value.integer, 127) > 0)
832 : 1 : gfc_warning (OPT_Wsurprising,
833 : : "Argument of %s function at %L outside of range [0,127]",
834 : : name, &e->where);
835 : :
836 : 14516 : if (kind == 1 && mpz_cmp_si (e->value.integer, 255) > 0)
837 : : too_large = true;
838 : 14507 : else if (kind == 4)
839 : : {
840 : 1420 : mpz_t t;
841 : 1420 : mpz_init_set_ui (t, 2);
842 : 1420 : mpz_pow_ui (t, t, 32);
843 : 1420 : mpz_sub_ui (t, t, 1);
844 : 1420 : if (mpz_cmp (e->value.integer, t) > 0)
845 : 2 : too_large = true;
846 : 1420 : mpz_clear (t);
847 : : }
848 : :
849 : 1420 : if (too_large)
850 : : {
851 : 11 : gfc_error ("Argument of %s function at %L is too large for the "
852 : : "collating sequence of kind %d", name, &e->where, kind);
853 : 11 : return &gfc_bad_expr;
854 : : }
855 : :
856 : 14505 : result = gfc_get_character_expr (kind, &e->where, NULL, 1);
857 : 14505 : result->value.character.string[0] = mpz_get_ui (e->value.integer);
858 : :
859 : 14505 : return result;
860 : : }
861 : :
862 : :
863 : :
864 : : /* We use the processor's collating sequence, because all
865 : : systems that gfortran currently works on are ASCII. */
866 : :
867 : : gfc_expr *
868 : 13258 : gfc_simplify_achar (gfc_expr *e, gfc_expr *k)
869 : : {
870 : 13258 : return simplify_achar_char (e, k, "ACHAR", true);
871 : : }
872 : :
873 : :
874 : : gfc_expr *
875 : 552 : gfc_simplify_acos (gfc_expr *x)
876 : : {
877 : 552 : gfc_expr *result;
878 : :
879 : 552 : if (x->expr_type != EXPR_CONSTANT)
880 : : return NULL;
881 : :
882 : 88 : switch (x->ts.type)
883 : : {
884 : 84 : case BT_REAL:
885 : 84 : if (mpfr_cmp_si (x->value.real, 1) > 0
886 : 84 : || mpfr_cmp_si (x->value.real, -1) < 0)
887 : : {
888 : 0 : gfc_error ("Argument of ACOS at %L must be within the closed "
889 : : "interval [-1, 1]",
890 : : &x->where);
891 : 0 : return &gfc_bad_expr;
892 : : }
893 : 84 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
894 : 84 : mpfr_acos (result->value.real, x->value.real, GFC_RND_MODE);
895 : 84 : break;
896 : :
897 : 4 : case BT_COMPLEX:
898 : 4 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
899 : 4 : mpc_acos (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
900 : 4 : break;
901 : :
902 : 0 : default:
903 : 0 : gfc_internal_error ("in gfc_simplify_acos(): Bad type");
904 : : }
905 : :
906 : 88 : return range_check (result, "ACOS");
907 : : }
908 : :
909 : : gfc_expr *
910 : 266 : gfc_simplify_acosh (gfc_expr *x)
911 : : {
912 : 266 : gfc_expr *result;
913 : :
914 : 266 : if (x->expr_type != EXPR_CONSTANT)
915 : : return NULL;
916 : :
917 : 34 : switch (x->ts.type)
918 : : {
919 : 30 : case BT_REAL:
920 : 30 : if (mpfr_cmp_si (x->value.real, 1) < 0)
921 : : {
922 : 0 : gfc_error ("Argument of ACOSH at %L must not be less than 1",
923 : : &x->where);
924 : 0 : return &gfc_bad_expr;
925 : : }
926 : :
927 : 30 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
928 : 30 : mpfr_acosh (result->value.real, x->value.real, GFC_RND_MODE);
929 : 30 : break;
930 : :
931 : 4 : case BT_COMPLEX:
932 : 4 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
933 : 4 : mpc_acosh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
934 : 4 : break;
935 : :
936 : 0 : default:
937 : 0 : gfc_internal_error ("in gfc_simplify_acosh(): Bad type");
938 : : }
939 : :
940 : 34 : return range_check (result, "ACOSH");
941 : : }
942 : :
943 : : gfc_expr *
944 : 1185 : gfc_simplify_adjustl (gfc_expr *e)
945 : : {
946 : 1185 : gfc_expr *result;
947 : 1185 : int count, i, len;
948 : 1185 : gfc_char_t ch;
949 : :
950 : 1185 : if (e->expr_type != EXPR_CONSTANT)
951 : : return NULL;
952 : :
953 : 31 : len = e->value.character.length;
954 : :
955 : 89 : for (count = 0, i = 0; i < len; ++i)
956 : : {
957 : 89 : ch = e->value.character.string[i];
958 : 89 : if (ch != ' ')
959 : : break;
960 : 58 : ++count;
961 : : }
962 : :
963 : 31 : result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, len);
964 : 476 : for (i = 0; i < len - count; ++i)
965 : 414 : result->value.character.string[i] = e->value.character.string[count + i];
966 : :
967 : : return result;
968 : : }
969 : :
970 : :
971 : : gfc_expr *
972 : 347 : gfc_simplify_adjustr (gfc_expr *e)
973 : : {
974 : 347 : gfc_expr *result;
975 : 347 : int count, i, len;
976 : 347 : gfc_char_t ch;
977 : :
978 : 347 : if (e->expr_type != EXPR_CONSTANT)
979 : : return NULL;
980 : :
981 : 23 : len = e->value.character.length;
982 : :
983 : 173 : for (count = 0, i = len - 1; i >= 0; --i)
984 : : {
985 : 173 : ch = e->value.character.string[i];
986 : 173 : if (ch != ' ')
987 : : break;
988 : 150 : ++count;
989 : : }
990 : :
991 : 23 : result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, len);
992 : 196 : for (i = 0; i < count; ++i)
993 : 150 : result->value.character.string[i] = ' ';
994 : :
995 : 260 : for (i = count; i < len; ++i)
996 : 237 : result->value.character.string[i] = e->value.character.string[i - count];
997 : :
998 : : return result;
999 : : }
1000 : :
1001 : :
1002 : : gfc_expr *
1003 : 1719 : gfc_simplify_aimag (gfc_expr *e)
1004 : : {
1005 : 1719 : gfc_expr *result;
1006 : :
1007 : 1719 : if (e->expr_type != EXPR_CONSTANT)
1008 : : return NULL;
1009 : :
1010 : 164 : result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
1011 : 164 : mpfr_set (result->value.real, mpc_imagref (e->value.complex), GFC_RND_MODE);
1012 : :
1013 : 164 : return range_check (result, "AIMAG");
1014 : : }
1015 : :
1016 : :
1017 : : gfc_expr *
1018 : 594 : gfc_simplify_aint (gfc_expr *e, gfc_expr *k)
1019 : : {
1020 : 594 : gfc_expr *rtrunc, *result;
1021 : 594 : int kind;
1022 : :
1023 : 594 : kind = get_kind (BT_REAL, k, "AINT", e->ts.kind);
1024 : 594 : if (kind == -1)
1025 : : return &gfc_bad_expr;
1026 : :
1027 : 594 : if (e->expr_type != EXPR_CONSTANT)
1028 : : return NULL;
1029 : :
1030 : 31 : rtrunc = gfc_copy_expr (e);
1031 : 31 : mpfr_trunc (rtrunc->value.real, e->value.real);
1032 : :
1033 : 31 : result = gfc_real2real (rtrunc, kind);
1034 : :
1035 : 31 : gfc_free_expr (rtrunc);
1036 : :
1037 : 31 : return range_check (result, "AINT");
1038 : : }
1039 : :
1040 : :
1041 : : gfc_expr *
1042 : 1337 : gfc_simplify_all (gfc_expr *mask, gfc_expr *dim)
1043 : : {
1044 : 1337 : return simplify_transformation (mask, dim, NULL, true, gfc_and);
1045 : : }
1046 : :
1047 : :
1048 : : gfc_expr *
1049 : 63 : gfc_simplify_dint (gfc_expr *e)
1050 : : {
1051 : 63 : gfc_expr *rtrunc, *result;
1052 : :
1053 : 63 : if (e->expr_type != EXPR_CONSTANT)
1054 : : return NULL;
1055 : :
1056 : 16 : rtrunc = gfc_copy_expr (e);
1057 : 16 : mpfr_trunc (rtrunc->value.real, e->value.real);
1058 : :
1059 : 16 : result = gfc_real2real (rtrunc, gfc_default_double_kind);
1060 : :
1061 : 16 : gfc_free_expr (rtrunc);
1062 : :
1063 : 16 : return range_check (result, "DINT");
1064 : : }
1065 : :
1066 : :
1067 : : gfc_expr *
1068 : 3 : gfc_simplify_dreal (gfc_expr *e)
1069 : : {
1070 : 3 : gfc_expr *result = NULL;
1071 : :
1072 : 3 : if (e->expr_type != EXPR_CONSTANT)
1073 : : return NULL;
1074 : :
1075 : 1 : result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
1076 : 1 : mpc_real (result->value.real, e->value.complex, GFC_RND_MODE);
1077 : :
1078 : 1 : return range_check (result, "DREAL");
1079 : : }
1080 : :
1081 : :
1082 : : gfc_expr *
1083 : 162 : gfc_simplify_anint (gfc_expr *e, gfc_expr *k)
1084 : : {
1085 : 162 : gfc_expr *result;
1086 : 162 : int kind;
1087 : :
1088 : 162 : kind = get_kind (BT_REAL, k, "ANINT", e->ts.kind);
1089 : 162 : if (kind == -1)
1090 : : return &gfc_bad_expr;
1091 : :
1092 : 162 : if (e->expr_type != EXPR_CONSTANT)
1093 : : return NULL;
1094 : :
1095 : 55 : result = gfc_get_constant_expr (e->ts.type, kind, &e->where);
1096 : 55 : mpfr_round (result->value.real, e->value.real);
1097 : :
1098 : 55 : return range_check (result, "ANINT");
1099 : : }
1100 : :
1101 : :
1102 : : gfc_expr *
1103 : 334 : gfc_simplify_and (gfc_expr *x, gfc_expr *y)
1104 : : {
1105 : 334 : gfc_expr *result;
1106 : 334 : int kind;
1107 : :
1108 : 334 : if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1109 : : return NULL;
1110 : :
1111 : 7 : kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
1112 : :
1113 : 7 : switch (x->ts.type)
1114 : : {
1115 : 1 : case BT_INTEGER:
1116 : 1 : result = gfc_get_constant_expr (BT_INTEGER, kind, &x->where);
1117 : 1 : mpz_and (result->value.integer, x->value.integer, y->value.integer);
1118 : 1 : return range_check (result, "AND");
1119 : :
1120 : 6 : case BT_LOGICAL:
1121 : 6 : return gfc_get_logical_expr (kind, &x->where,
1122 : 12 : x->value.logical && y->value.logical);
1123 : :
1124 : 0 : default:
1125 : 0 : gcc_unreachable ();
1126 : : }
1127 : : }
1128 : :
1129 : :
1130 : : gfc_expr *
1131 : 42061 : gfc_simplify_any (gfc_expr *mask, gfc_expr *dim)
1132 : : {
1133 : 42061 : return simplify_transformation (mask, dim, NULL, false, gfc_or);
1134 : : }
1135 : :
1136 : :
1137 : : gfc_expr *
1138 : 105 : gfc_simplify_dnint (gfc_expr *e)
1139 : : {
1140 : 105 : gfc_expr *result;
1141 : :
1142 : 105 : if (e->expr_type != EXPR_CONSTANT)
1143 : : return NULL;
1144 : :
1145 : 46 : result = gfc_get_constant_expr (BT_REAL, gfc_default_double_kind, &e->where);
1146 : 46 : mpfr_round (result->value.real, e->value.real);
1147 : :
1148 : 46 : return range_check (result, "DNINT");
1149 : : }
1150 : :
1151 : :
1152 : : gfc_expr *
1153 : 546 : gfc_simplify_asin (gfc_expr *x)
1154 : : {
1155 : 546 : gfc_expr *result;
1156 : :
1157 : 546 : if (x->expr_type != EXPR_CONSTANT)
1158 : : return NULL;
1159 : :
1160 : 49 : switch (x->ts.type)
1161 : : {
1162 : 45 : case BT_REAL:
1163 : 45 : if (mpfr_cmp_si (x->value.real, 1) > 0
1164 : 45 : || mpfr_cmp_si (x->value.real, -1) < 0)
1165 : : {
1166 : 0 : gfc_error ("Argument of ASIN at %L must be within the closed "
1167 : : "interval [-1, 1]",
1168 : : &x->where);
1169 : 0 : return &gfc_bad_expr;
1170 : : }
1171 : 45 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1172 : 45 : mpfr_asin (result->value.real, x->value.real, GFC_RND_MODE);
1173 : 45 : break;
1174 : :
1175 : 4 : case BT_COMPLEX:
1176 : 4 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1177 : 4 : mpc_asin (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1178 : 4 : break;
1179 : :
1180 : 0 : default:
1181 : 0 : gfc_internal_error ("in gfc_simplify_asin(): Bad type");
1182 : : }
1183 : :
1184 : 49 : return range_check (result, "ASIN");
1185 : : }
1186 : :
1187 : :
1188 : : #if MPFR_VERSION < MPFR_VERSION_NUM(4,2,0)
1189 : : /* Convert radians to degrees, i.e., x * 180 / pi. */
1190 : :
1191 : : static void
1192 : : rad2deg (mpfr_t x)
1193 : : {
1194 : : mpfr_t tmp;
1195 : :
1196 : : mpfr_init (tmp);
1197 : : mpfr_const_pi (tmp, GFC_RND_MODE);
1198 : : mpfr_mul_ui (x, x, 180, GFC_RND_MODE);
1199 : : mpfr_div (x, x, tmp, GFC_RND_MODE);
1200 : : mpfr_clear (tmp);
1201 : : }
1202 : : #endif
1203 : :
1204 : :
1205 : : /* Simplify ACOSD(X) where the returned value has units of degree. */
1206 : :
1207 : : gfc_expr *
1208 : 169 : gfc_simplify_acosd (gfc_expr *x)
1209 : : {
1210 : 169 : gfc_expr *result;
1211 : :
1212 : 169 : if (x->expr_type != EXPR_CONSTANT)
1213 : : return NULL;
1214 : :
1215 : 25 : if (mpfr_cmp_si (x->value.real, 1) > 0
1216 : 25 : || mpfr_cmp_si (x->value.real, -1) < 0)
1217 : : {
1218 : 1 : gfc_error (
1219 : : "Argument of ACOSD at %L must be within the closed interval [-1, 1]",
1220 : : &x->where);
1221 : 1 : return &gfc_bad_expr;
1222 : : }
1223 : :
1224 : 24 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1225 : : #if MPFR_VERSION >= MPFR_VERSION_NUM(4,2,0)
1226 : 24 : mpfr_acosu (result->value.real, x->value.real, 360, GFC_RND_MODE);
1227 : : #else
1228 : : mpfr_acos (result->value.real, x->value.real, GFC_RND_MODE);
1229 : : rad2deg (result->value.real);
1230 : : #endif
1231 : :
1232 : 24 : return range_check (result, "ACOSD");
1233 : : }
1234 : :
1235 : :
1236 : : /* Simplify asind (x) where the returned value has units of degree. */
1237 : :
1238 : : gfc_expr *
1239 : 169 : gfc_simplify_asind (gfc_expr *x)
1240 : : {
1241 : 169 : gfc_expr *result;
1242 : :
1243 : 169 : if (x->expr_type != EXPR_CONSTANT)
1244 : : return NULL;
1245 : :
1246 : 25 : if (mpfr_cmp_si (x->value.real, 1) > 0
1247 : 25 : || mpfr_cmp_si (x->value.real, -1) < 0)
1248 : : {
1249 : 1 : gfc_error (
1250 : : "Argument of ASIND at %L must be within the closed interval [-1, 1]",
1251 : : &x->where);
1252 : 1 : return &gfc_bad_expr;
1253 : : }
1254 : :
1255 : 24 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1256 : : #if MPFR_VERSION >= MPFR_VERSION_NUM(4,2,0)
1257 : 24 : mpfr_asinu (result->value.real, x->value.real, 360, GFC_RND_MODE);
1258 : : #else
1259 : : mpfr_asin (result->value.real, x->value.real, GFC_RND_MODE);
1260 : : rad2deg (result->value.real);
1261 : : #endif
1262 : :
1263 : 24 : return range_check (result, "ASIND");
1264 : : }
1265 : :
1266 : :
1267 : : /* Simplify atand (x) where the returned value has units of degree. */
1268 : :
1269 : : gfc_expr *
1270 : 168 : gfc_simplify_atand (gfc_expr *x)
1271 : : {
1272 : 168 : gfc_expr *result;
1273 : :
1274 : 168 : if (x->expr_type != EXPR_CONSTANT)
1275 : : return NULL;
1276 : :
1277 : 24 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1278 : : #if MPFR_VERSION >= MPFR_VERSION_NUM(4,2,0)
1279 : 24 : mpfr_atanu (result->value.real, x->value.real, 360, GFC_RND_MODE);
1280 : : #else
1281 : : mpfr_atan (result->value.real, x->value.real, GFC_RND_MODE);
1282 : : rad2deg (result->value.real);
1283 : : #endif
1284 : :
1285 : 24 : return range_check (result, "ATAND");
1286 : : }
1287 : :
1288 : :
1289 : : gfc_expr *
1290 : 269 : gfc_simplify_asinh (gfc_expr *x)
1291 : : {
1292 : 269 : gfc_expr *result;
1293 : :
1294 : 269 : if (x->expr_type != EXPR_CONSTANT)
1295 : : return NULL;
1296 : :
1297 : 37 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1298 : :
1299 : 37 : switch (x->ts.type)
1300 : : {
1301 : 33 : case BT_REAL:
1302 : 33 : mpfr_asinh (result->value.real, x->value.real, GFC_RND_MODE);
1303 : 33 : break;
1304 : :
1305 : 4 : case BT_COMPLEX:
1306 : 4 : mpc_asinh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1307 : 4 : break;
1308 : :
1309 : 0 : default:
1310 : 0 : gfc_internal_error ("in gfc_simplify_asinh(): Bad type");
1311 : : }
1312 : :
1313 : 37 : return range_check (result, "ASINH");
1314 : : }
1315 : :
1316 : :
1317 : : gfc_expr *
1318 : 611 : gfc_simplify_atan (gfc_expr *x)
1319 : : {
1320 : 611 : gfc_expr *result;
1321 : :
1322 : 611 : if (x->expr_type != EXPR_CONSTANT)
1323 : : return NULL;
1324 : :
1325 : 109 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1326 : :
1327 : 109 : switch (x->ts.type)
1328 : : {
1329 : 105 : case BT_REAL:
1330 : 105 : mpfr_atan (result->value.real, x->value.real, GFC_RND_MODE);
1331 : 105 : break;
1332 : :
1333 : 4 : case BT_COMPLEX:
1334 : 4 : mpc_atan (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1335 : 4 : break;
1336 : :
1337 : 0 : default:
1338 : 0 : gfc_internal_error ("in gfc_simplify_atan(): Bad type");
1339 : : }
1340 : :
1341 : 109 : return range_check (result, "ATAN");
1342 : : }
1343 : :
1344 : :
1345 : : gfc_expr *
1346 : 266 : gfc_simplify_atanh (gfc_expr *x)
1347 : : {
1348 : 266 : gfc_expr *result;
1349 : :
1350 : 266 : if (x->expr_type != EXPR_CONSTANT)
1351 : : return NULL;
1352 : :
1353 : 34 : switch (x->ts.type)
1354 : : {
1355 : 30 : case BT_REAL:
1356 : 30 : if (mpfr_cmp_si (x->value.real, 1) >= 0
1357 : 30 : || mpfr_cmp_si (x->value.real, -1) <= 0)
1358 : : {
1359 : 0 : gfc_error ("Argument of ATANH at %L must be inside the range -1 "
1360 : : "to 1", &x->where);
1361 : 0 : return &gfc_bad_expr;
1362 : : }
1363 : 30 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1364 : 30 : mpfr_atanh (result->value.real, x->value.real, GFC_RND_MODE);
1365 : 30 : break;
1366 : :
1367 : 4 : case BT_COMPLEX:
1368 : 4 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1369 : 4 : mpc_atanh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1370 : 4 : break;
1371 : :
1372 : 0 : default:
1373 : 0 : gfc_internal_error ("in gfc_simplify_atanh(): Bad type");
1374 : : }
1375 : :
1376 : 34 : return range_check (result, "ATANH");
1377 : : }
1378 : :
1379 : :
1380 : : gfc_expr *
1381 : 887 : gfc_simplify_atan2 (gfc_expr *y, gfc_expr *x)
1382 : : {
1383 : 887 : gfc_expr *result;
1384 : :
1385 : 887 : if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1386 : : return NULL;
1387 : :
1388 : 324 : if (mpfr_zero_p (y->value.real) && mpfr_zero_p (x->value.real))
1389 : : {
1390 : 0 : gfc_error ("If the first argument of ATAN2 at %L is zero, then the "
1391 : : "second argument must not be zero", &y->where);
1392 : 0 : return &gfc_bad_expr;
1393 : : }
1394 : :
1395 : 324 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1396 : 324 : mpfr_atan2 (result->value.real, y->value.real, x->value.real, GFC_RND_MODE);
1397 : :
1398 : 324 : return range_check (result, "ATAN2");
1399 : : }
1400 : :
1401 : :
1402 : : gfc_expr *
1403 : 82 : gfc_simplify_bessel_j0 (gfc_expr *x)
1404 : : {
1405 : 82 : gfc_expr *result;
1406 : :
1407 : 82 : if (x->expr_type != EXPR_CONSTANT)
1408 : : return NULL;
1409 : :
1410 : 14 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1411 : 14 : mpfr_j0 (result->value.real, x->value.real, GFC_RND_MODE);
1412 : :
1413 : 14 : return range_check (result, "BESSEL_J0");
1414 : : }
1415 : :
1416 : :
1417 : : gfc_expr *
1418 : 80 : gfc_simplify_bessel_j1 (gfc_expr *x)
1419 : : {
1420 : 80 : gfc_expr *result;
1421 : :
1422 : 80 : if (x->expr_type != EXPR_CONSTANT)
1423 : : return NULL;
1424 : :
1425 : 12 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1426 : 12 : mpfr_j1 (result->value.real, x->value.real, GFC_RND_MODE);
1427 : :
1428 : 12 : return range_check (result, "BESSEL_J1");
1429 : : }
1430 : :
1431 : :
1432 : : gfc_expr *
1433 : 1287 : gfc_simplify_bessel_jn (gfc_expr *order, gfc_expr *x)
1434 : : {
1435 : 1287 : gfc_expr *result;
1436 : 1287 : long n;
1437 : :
1438 : 1287 : if (x->expr_type != EXPR_CONSTANT || order->expr_type != EXPR_CONSTANT)
1439 : : return NULL;
1440 : :
1441 : 1054 : n = mpz_get_si (order->value.integer);
1442 : 1054 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1443 : 1054 : mpfr_jn (result->value.real, n, x->value.real, GFC_RND_MODE);
1444 : :
1445 : 1054 : return range_check (result, "BESSEL_JN");
1446 : : }
1447 : :
1448 : :
1449 : : /* Simplify transformational form of JN and YN. */
1450 : :
1451 : : static gfc_expr *
1452 : 71 : gfc_simplify_bessel_n2 (gfc_expr *order1, gfc_expr *order2, gfc_expr *x,
1453 : : bool jn)
1454 : : {
1455 : 71 : gfc_expr *result;
1456 : 71 : gfc_expr *e;
1457 : 71 : long n1, n2;
1458 : 71 : int i;
1459 : 71 : mpfr_t x2rev, last1, last2;
1460 : :
1461 : 71 : if (x->expr_type != EXPR_CONSTANT || order1->expr_type != EXPR_CONSTANT
1462 : 57 : || order2->expr_type != EXPR_CONSTANT)
1463 : : return NULL;
1464 : :
1465 : 57 : n1 = mpz_get_si (order1->value.integer);
1466 : 57 : n2 = mpz_get_si (order2->value.integer);
1467 : 57 : result = gfc_get_array_expr (x->ts.type, x->ts.kind, &x->where);
1468 : 57 : result->rank = 1;
1469 : 57 : result->shape = gfc_get_shape (1);
1470 : 57 : mpz_init_set_ui (result->shape[0], MAX (n2-n1+1, 0));
1471 : :
1472 : 57 : if (n2 < n1)
1473 : : return result;
1474 : :
1475 : : /* Special case: x == 0; it is J0(0.0) == 1, JN(N > 0, 0.0) == 0; and
1476 : : YN(N, 0.0) = -Inf. */
1477 : :
1478 : 57 : if (mpfr_cmp_ui (x->value.real, 0.0) == 0)
1479 : : {
1480 : 14 : if (!jn && flag_range_check)
1481 : : {
1482 : 1 : gfc_error ("Result of BESSEL_YN is -INF at %L", &result->where);
1483 : 1 : gfc_free_expr (result);
1484 : 1 : return &gfc_bad_expr;
1485 : : }
1486 : :
1487 : 13 : if (jn && n1 == 0)
1488 : : {
1489 : 7 : e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1490 : 7 : mpfr_set_ui (e->value.real, 1, GFC_RND_MODE);
1491 : 7 : gfc_constructor_append_expr (&result->value.constructor, e,
1492 : : &x->where);
1493 : 7 : n1++;
1494 : : }
1495 : :
1496 : 149 : for (i = n1; i <= n2; i++)
1497 : : {
1498 : 136 : e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1499 : 136 : if (jn)
1500 : 70 : mpfr_set_ui (e->value.real, 0, GFC_RND_MODE);
1501 : : else
1502 : 66 : mpfr_set_inf (e->value.real, -1);
1503 : 136 : gfc_constructor_append_expr (&result->value.constructor, e,
1504 : : &x->where);
1505 : : }
1506 : :
1507 : : return result;
1508 : : }
1509 : :
1510 : : /* Use the faster but more verbose recurrence algorithm. Bessel functions
1511 : : are stable for downward recursion and Neumann functions are stable
1512 : : for upward recursion. It is
1513 : : x2rev = 2.0/x,
1514 : : J(N-1, x) = x2rev * N * J(N, x) - J(N+1, x),
1515 : : Y(N+1, x) = x2rev * N * Y(N, x) - Y(N-1, x).
1516 : : Cf. http://dlmf.nist.gov/10.74#iv and http://dlmf.nist.gov/10.6#E1 */
1517 : :
1518 : 43 : gfc_set_model_kind (x->ts.kind);
1519 : :
1520 : : /* Get first recursion anchor. */
1521 : :
1522 : 43 : mpfr_init (last1);
1523 : 43 : if (jn)
1524 : 22 : mpfr_jn (last1, n2, x->value.real, GFC_RND_MODE);
1525 : : else
1526 : 21 : mpfr_yn (last1, n1, x->value.real, GFC_RND_MODE);
1527 : :
1528 : 43 : e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1529 : 43 : mpfr_set (e->value.real, last1, GFC_RND_MODE);
1530 : 64 : if (range_check (e, jn ? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr)
1531 : : {
1532 : 0 : mpfr_clear (last1);
1533 : 0 : gfc_free_expr (e);
1534 : 0 : gfc_free_expr (result);
1535 : 0 : return &gfc_bad_expr;
1536 : : }
1537 : 43 : gfc_constructor_append_expr (&result->value.constructor, e, &x->where);
1538 : :
1539 : 43 : if (n1 == n2)
1540 : : {
1541 : 0 : mpfr_clear (last1);
1542 : 0 : return result;
1543 : : }
1544 : :
1545 : : /* Get second recursion anchor. */
1546 : :
1547 : 43 : mpfr_init (last2);
1548 : 43 : if (jn)
1549 : 22 : mpfr_jn (last2, n2-1, x->value.real, GFC_RND_MODE);
1550 : : else
1551 : 21 : mpfr_yn (last2, n1+1, x->value.real, GFC_RND_MODE);
1552 : :
1553 : 43 : e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1554 : 43 : mpfr_set (e->value.real, last2, GFC_RND_MODE);
1555 : 43 : if (range_check (e, jn ? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr)
1556 : : {
1557 : 0 : mpfr_clear (last1);
1558 : 0 : mpfr_clear (last2);
1559 : 0 : gfc_free_expr (e);
1560 : 0 : gfc_free_expr (result);
1561 : 0 : return &gfc_bad_expr;
1562 : : }
1563 : 43 : if (jn)
1564 : 22 : gfc_constructor_insert_expr (&result->value.constructor, e, &x->where, -2);
1565 : : else
1566 : 21 : gfc_constructor_append_expr (&result->value.constructor, e, &x->where);
1567 : :
1568 : 43 : if (n1 + 1 == n2)
1569 : : {
1570 : 1 : mpfr_clear (last1);
1571 : 1 : mpfr_clear (last2);
1572 : 1 : return result;
1573 : : }
1574 : :
1575 : : /* Start actual recursion. */
1576 : :
1577 : 42 : mpfr_init (x2rev);
1578 : 42 : mpfr_ui_div (x2rev, 2, x->value.real, GFC_RND_MODE);
1579 : :
1580 : 322 : for (i = 2; i <= n2-n1; i++)
1581 : : {
1582 : 280 : e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1583 : :
1584 : : /* Special case: For YN, if the previous N gave -INF, set
1585 : : also N+1 to -INF. */
1586 : 280 : if (!jn && !flag_range_check && mpfr_inf_p (last2))
1587 : : {
1588 : 0 : mpfr_set_inf (e->value.real, -1);
1589 : 0 : gfc_constructor_append_expr (&result->value.constructor, e,
1590 : : &x->where);
1591 : 0 : continue;
1592 : : }
1593 : :
1594 : 280 : mpfr_mul_si (e->value.real, x2rev, jn ? (n2-i+1) : (n1+i-1),
1595 : : GFC_RND_MODE);
1596 : 280 : mpfr_mul (e->value.real, e->value.real, last2, GFC_RND_MODE);
1597 : 280 : mpfr_sub (e->value.real, e->value.real, last1, GFC_RND_MODE);
1598 : :
1599 : 280 : if (range_check (e, jn ? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr)
1600 : : {
1601 : : /* Range_check frees "e" in that case. */
1602 : 0 : e = NULL;
1603 : 0 : goto error;
1604 : : }
1605 : :
1606 : 280 : if (jn)
1607 : 140 : gfc_constructor_insert_expr (&result->value.constructor, e, &x->where,
1608 : : -i-1);
1609 : : else
1610 : 140 : gfc_constructor_append_expr (&result->value.constructor, e, &x->where);
1611 : :
1612 : 280 : mpfr_set (last1, last2, GFC_RND_MODE);
1613 : 280 : mpfr_set (last2, e->value.real, GFC_RND_MODE);
1614 : : }
1615 : :
1616 : 42 : mpfr_clear (last1);
1617 : 42 : mpfr_clear (last2);
1618 : 42 : mpfr_clear (x2rev);
1619 : 42 : return result;
1620 : :
1621 : 0 : error:
1622 : 0 : mpfr_clear (last1);
1623 : 0 : mpfr_clear (last2);
1624 : 0 : mpfr_clear (x2rev);
1625 : 0 : gfc_free_expr (e);
1626 : 0 : gfc_free_expr (result);
1627 : 0 : return &gfc_bad_expr;
1628 : : }
1629 : :
1630 : :
1631 : : gfc_expr *
1632 : 31 : gfc_simplify_bessel_jn2 (gfc_expr *order1, gfc_expr *order2, gfc_expr *x)
1633 : : {
1634 : 31 : return gfc_simplify_bessel_n2 (order1, order2, x, true);
1635 : : }
1636 : :
1637 : :
1638 : : gfc_expr *
1639 : 80 : gfc_simplify_bessel_y0 (gfc_expr *x)
1640 : : {
1641 : 80 : gfc_expr *result;
1642 : :
1643 : 80 : if (x->expr_type != EXPR_CONSTANT)
1644 : : return NULL;
1645 : :
1646 : 12 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1647 : 12 : mpfr_y0 (result->value.real, x->value.real, GFC_RND_MODE);
1648 : :
1649 : 12 : return range_check (result, "BESSEL_Y0");
1650 : : }
1651 : :
1652 : :
1653 : : gfc_expr *
1654 : 80 : gfc_simplify_bessel_y1 (gfc_expr *x)
1655 : : {
1656 : 80 : gfc_expr *result;
1657 : :
1658 : 80 : if (x->expr_type != EXPR_CONSTANT)
1659 : : return NULL;
1660 : :
1661 : 12 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1662 : 12 : mpfr_y1 (result->value.real, x->value.real, GFC_RND_MODE);
1663 : :
1664 : 12 : return range_check (result, "BESSEL_Y1");
1665 : : }
1666 : :
1667 : :
1668 : : gfc_expr *
1669 : 1868 : gfc_simplify_bessel_yn (gfc_expr *order, gfc_expr *x)
1670 : : {
1671 : 1868 : gfc_expr *result;
1672 : 1868 : long n;
1673 : :
1674 : 1868 : if (x->expr_type != EXPR_CONSTANT || order->expr_type != EXPR_CONSTANT)
1675 : : return NULL;
1676 : :
1677 : 1010 : n = mpz_get_si (order->value.integer);
1678 : 1010 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1679 : 1010 : mpfr_yn (result->value.real, n, x->value.real, GFC_RND_MODE);
1680 : :
1681 : 1010 : return range_check (result, "BESSEL_YN");
1682 : : }
1683 : :
1684 : :
1685 : : gfc_expr *
1686 : 40 : gfc_simplify_bessel_yn2 (gfc_expr *order1, gfc_expr *order2, gfc_expr *x)
1687 : : {
1688 : 40 : return gfc_simplify_bessel_n2 (order1, order2, x, false);
1689 : : }
1690 : :
1691 : :
1692 : : gfc_expr *
1693 : 3655 : gfc_simplify_bit_size (gfc_expr *e)
1694 : : {
1695 : 3655 : int i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
1696 : 3655 : int bit_size;
1697 : :
1698 : 3655 : if (flag_unsigned && e->ts.type == BT_UNSIGNED)
1699 : 24 : bit_size = gfc_unsigned_kinds[i].bit_size;
1700 : : else
1701 : 3631 : bit_size = gfc_integer_kinds[i].bit_size;
1702 : :
1703 : 3655 : return gfc_get_int_expr (e->ts.kind, &e->where, bit_size);
1704 : : }
1705 : :
1706 : :
1707 : : gfc_expr *
1708 : 342 : gfc_simplify_btest (gfc_expr *e, gfc_expr *bit)
1709 : : {
1710 : 342 : int b;
1711 : :
1712 : 342 : if (e->expr_type != EXPR_CONSTANT || bit->expr_type != EXPR_CONSTANT)
1713 : : return NULL;
1714 : :
1715 : 31 : if (!gfc_check_bitfcn (e, bit))
1716 : : return &gfc_bad_expr;
1717 : :
1718 : 23 : if (gfc_extract_int (bit, &b) || b < 0)
1719 : 0 : return gfc_get_logical_expr (gfc_default_logical_kind, &e->where, false);
1720 : :
1721 : 23 : return gfc_get_logical_expr (gfc_default_logical_kind, &e->where,
1722 : 23 : mpz_tstbit (e->value.integer, b));
1723 : : }
1724 : :
1725 : :
1726 : : static int
1727 : 1230 : compare_bitwise (gfc_expr *i, gfc_expr *j)
1728 : : {
1729 : 1230 : mpz_t x, y;
1730 : 1230 : int k, res;
1731 : :
1732 : 1230 : gcc_assert (i->ts.type == BT_INTEGER);
1733 : 1230 : gcc_assert (j->ts.type == BT_INTEGER);
1734 : :
1735 : 1230 : mpz_init_set (x, i->value.integer);
1736 : 1230 : k = gfc_validate_kind (i->ts.type, i->ts.kind, false);
1737 : 1230 : gfc_convert_mpz_to_unsigned (x, gfc_integer_kinds[k].bit_size);
1738 : :
1739 : 1230 : mpz_init_set (y, j->value.integer);
1740 : 1230 : k = gfc_validate_kind (j->ts.type, j->ts.kind, false);
1741 : 1230 : gfc_convert_mpz_to_unsigned (y, gfc_integer_kinds[k].bit_size);
1742 : :
1743 : 1230 : res = mpz_cmp (x, y);
1744 : 1230 : mpz_clear (x);
1745 : 1230 : mpz_clear (y);
1746 : 1230 : return res;
1747 : : }
1748 : :
1749 : :
1750 : : gfc_expr *
1751 : 504 : gfc_simplify_bge (gfc_expr *i, gfc_expr *j)
1752 : : {
1753 : 504 : bool result;
1754 : :
1755 : 504 : if (i->expr_type != EXPR_CONSTANT || j->expr_type != EXPR_CONSTANT)
1756 : : return NULL;
1757 : :
1758 : 384 : if (flag_unsigned && i->ts.type == BT_UNSIGNED)
1759 : 54 : result = mpz_cmp (i->value.integer, j->value.integer) >= 0;
1760 : : else
1761 : 330 : result = compare_bitwise (i, j) >= 0;
1762 : :
1763 : 384 : return gfc_get_logical_expr (gfc_default_logical_kind, &i->where,
1764 : 384 : result);
1765 : : }
1766 : :
1767 : :
1768 : : gfc_expr *
1769 : 474 : gfc_simplify_bgt (gfc_expr *i, gfc_expr *j)
1770 : : {
1771 : 474 : bool result;
1772 : :
1773 : 474 : if (i->expr_type != EXPR_CONSTANT || j->expr_type != EXPR_CONSTANT)
1774 : : return NULL;
1775 : :
1776 : 354 : if (flag_unsigned && i->ts.type == BT_UNSIGNED)
1777 : 54 : result = mpz_cmp (i->value.integer, j->value.integer) > 0;
1778 : : else
1779 : 300 : result = compare_bitwise (i, j) > 0;
1780 : :
1781 : 354 : return gfc_get_logical_expr (gfc_default_logical_kind, &i->where,
1782 : 354 : result);
1783 : : }
1784 : :
1785 : :
1786 : : gfc_expr *
1787 : 474 : gfc_simplify_ble (gfc_expr *i, gfc_expr *j)
1788 : : {
1789 : 474 : bool result;
1790 : :
1791 : 474 : if (i->expr_type != EXPR_CONSTANT || j->expr_type != EXPR_CONSTANT)
1792 : : return NULL;
1793 : :
1794 : 354 : if (flag_unsigned && i->ts.type == BT_UNSIGNED)
1795 : 54 : result = mpz_cmp (i->value.integer, j->value.integer) <= 0;
1796 : : else
1797 : 300 : result = compare_bitwise (i, j) <= 0;
1798 : :
1799 : 354 : return gfc_get_logical_expr (gfc_default_logical_kind, &i->where,
1800 : 354 : result);
1801 : : }
1802 : :
1803 : :
1804 : : gfc_expr *
1805 : 474 : gfc_simplify_blt (gfc_expr *i, gfc_expr *j)
1806 : : {
1807 : 474 : bool result;
1808 : :
1809 : 474 : if (i->expr_type != EXPR_CONSTANT || j->expr_type != EXPR_CONSTANT)
1810 : : return NULL;
1811 : :
1812 : 354 : if (flag_unsigned && i->ts.type == BT_UNSIGNED)
1813 : 54 : result = mpz_cmp (i->value.integer, j->value.integer) < 0;
1814 : : else
1815 : 300 : result = compare_bitwise (i, j) < 0;
1816 : :
1817 : 354 : return gfc_get_logical_expr (gfc_default_logical_kind, &i->where,
1818 : 354 : result);
1819 : : }
1820 : :
1821 : : gfc_expr *
1822 : 90 : gfc_simplify_ceiling (gfc_expr *e, gfc_expr *k)
1823 : : {
1824 : 90 : gfc_expr *ceil, *result;
1825 : 90 : int kind;
1826 : :
1827 : 90 : kind = get_kind (BT_INTEGER, k, "CEILING", gfc_default_integer_kind);
1828 : 90 : if (kind == -1)
1829 : : return &gfc_bad_expr;
1830 : :
1831 : 90 : if (e->expr_type != EXPR_CONSTANT)
1832 : : return NULL;
1833 : :
1834 : 13 : ceil = gfc_copy_expr (e);
1835 : 13 : mpfr_ceil (ceil->value.real, e->value.real);
1836 : :
1837 : 13 : result = gfc_get_constant_expr (BT_INTEGER, kind, &e->where);
1838 : 13 : gfc_mpfr_to_mpz (result->value.integer, ceil->value.real, &e->where);
1839 : :
1840 : 13 : gfc_free_expr (ceil);
1841 : :
1842 : 13 : return range_check (result, "CEILING");
1843 : : }
1844 : :
1845 : :
1846 : : gfc_expr *
1847 : 8654 : gfc_simplify_char (gfc_expr *e, gfc_expr *k)
1848 : : {
1849 : 8654 : return simplify_achar_char (e, k, "CHAR", false);
1850 : : }
1851 : :
1852 : :
1853 : : /* Common subroutine for simplifying CMPLX, COMPLEX and DCMPLX. */
1854 : :
1855 : : static gfc_expr *
1856 : 6987 : simplify_cmplx (const char *name, gfc_expr *x, gfc_expr *y, int kind)
1857 : : {
1858 : 6987 : gfc_expr *result;
1859 : :
1860 : 6987 : if (x->expr_type != EXPR_CONSTANT
1861 : 5415 : || (y != NULL && y->expr_type != EXPR_CONSTANT))
1862 : : return NULL;
1863 : :
1864 : 5209 : result = gfc_get_constant_expr (BT_COMPLEX, kind, &x->where);
1865 : :
1866 : 5209 : switch (x->ts.type)
1867 : : {
1868 : 3670 : case BT_INTEGER:
1869 : 3670 : case BT_UNSIGNED:
1870 : 3670 : mpc_set_z (result->value.complex, x->value.integer, GFC_MPC_RND_MODE);
1871 : 3670 : break;
1872 : :
1873 : 1539 : case BT_REAL:
1874 : 1539 : mpc_set_fr (result->value.complex, x->value.real, GFC_RND_MODE);
1875 : 1539 : break;
1876 : :
1877 : 0 : case BT_COMPLEX:
1878 : 0 : mpc_set (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
1879 : 0 : break;
1880 : :
1881 : 0 : default:
1882 : 0 : gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (x)");
1883 : : }
1884 : :
1885 : 5209 : if (!y)
1886 : 224 : return range_check (result, name);
1887 : :
1888 : 4985 : switch (y->ts.type)
1889 : : {
1890 : 3558 : case BT_INTEGER:
1891 : 3558 : case BT_UNSIGNED:
1892 : 3558 : mpfr_set_z (mpc_imagref (result->value.complex),
1893 : 3558 : y->value.integer, GFC_RND_MODE);
1894 : 3558 : break;
1895 : :
1896 : 1427 : case BT_REAL:
1897 : 1427 : mpfr_set (mpc_imagref (result->value.complex),
1898 : : y->value.real, GFC_RND_MODE);
1899 : 1427 : break;
1900 : :
1901 : 0 : default:
1902 : 0 : gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (y)");
1903 : : }
1904 : :
1905 : 4985 : return range_check (result, name);
1906 : : }
1907 : :
1908 : :
1909 : : gfc_expr *
1910 : 6633 : gfc_simplify_cmplx (gfc_expr *x, gfc_expr *y, gfc_expr *k)
1911 : : {
1912 : 6633 : int kind;
1913 : :
1914 : 6633 : kind = get_kind (BT_REAL, k, "CMPLX", gfc_default_complex_kind);
1915 : 6633 : if (kind == -1)
1916 : : return &gfc_bad_expr;
1917 : :
1918 : 6633 : return simplify_cmplx ("CMPLX", x, y, kind);
1919 : : }
1920 : :
1921 : :
1922 : : gfc_expr *
1923 : 55 : gfc_simplify_complex (gfc_expr *x, gfc_expr *y)
1924 : : {
1925 : 55 : int kind;
1926 : :
1927 : 55 : if (x->ts.type == BT_INTEGER && y->ts.type == BT_INTEGER)
1928 : 15 : kind = gfc_default_complex_kind;
1929 : 40 : else if (x->ts.type == BT_REAL || y->ts.type == BT_INTEGER)
1930 : 34 : kind = x->ts.kind;
1931 : 6 : else if (x->ts.type == BT_INTEGER || y->ts.type == BT_REAL)
1932 : 6 : kind = y->ts.kind;
1933 : 0 : else if (x->ts.type == BT_REAL && y->ts.type == BT_REAL)
1934 : : kind = (x->ts.kind > y->ts.kind) ? x->ts.kind : y->ts.kind;
1935 : : else
1936 : 0 : gcc_unreachable ();
1937 : :
1938 : 55 : return simplify_cmplx ("COMPLEX", x, y, kind);
1939 : : }
1940 : :
1941 : :
1942 : : gfc_expr *
1943 : 720 : gfc_simplify_conjg (gfc_expr *e)
1944 : : {
1945 : 720 : gfc_expr *result;
1946 : :
1947 : 720 : if (e->expr_type != EXPR_CONSTANT)
1948 : : return NULL;
1949 : :
1950 : 47 : result = gfc_copy_expr (e);
1951 : 47 : mpc_conj (result->value.complex, result->value.complex, GFC_MPC_RND_MODE);
1952 : :
1953 : 47 : return range_check (result, "CONJG");
1954 : : }
1955 : :
1956 : :
1957 : : /* Simplify atan2d (x) where the unit is degree. */
1958 : :
1959 : : gfc_expr *
1960 : 289 : gfc_simplify_atan2d (gfc_expr *y, gfc_expr *x)
1961 : : {
1962 : 289 : gfc_expr *result;
1963 : :
1964 : 289 : if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1965 : : return NULL;
1966 : :
1967 : 49 : if (mpfr_zero_p (y->value.real) && mpfr_zero_p (x->value.real))
1968 : : {
1969 : 1 : gfc_error ("If the first argument of ATAN2D at %L is zero, then the "
1970 : : "second argument must not be zero", &y->where);
1971 : 1 : return &gfc_bad_expr;
1972 : : }
1973 : :
1974 : 48 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1975 : : #if MPFR_VERSION >= MPFR_VERSION_NUM(4,2,0)
1976 : 48 : mpfr_atan2u (result->value.real, y->value.real, x->value.real, 360,
1977 : : GFC_RND_MODE);
1978 : : #else
1979 : : mpfr_atan2 (result->value.real, y->value.real, x->value.real, GFC_RND_MODE);
1980 : : rad2deg (result->value.real);
1981 : : #endif
1982 : :
1983 : 48 : return range_check (result, "ATAN2D");
1984 : : }
1985 : :
1986 : :
1987 : : gfc_expr *
1988 : 910 : gfc_simplify_cos (gfc_expr *x)
1989 : : {
1990 : 910 : gfc_expr *result;
1991 : :
1992 : 910 : if (x->expr_type != EXPR_CONSTANT)
1993 : : return NULL;
1994 : :
1995 : 162 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1996 : :
1997 : 162 : switch (x->ts.type)
1998 : : {
1999 : 105 : case BT_REAL:
2000 : 105 : mpfr_cos (result->value.real, x->value.real, GFC_RND_MODE);
2001 : 105 : break;
2002 : :
2003 : 57 : case BT_COMPLEX:
2004 : 57 : gfc_set_model_kind (x->ts.kind);
2005 : 57 : mpc_cos (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
2006 : 57 : break;
2007 : :
2008 : 0 : default:
2009 : 0 : gfc_internal_error ("in gfc_simplify_cos(): Bad type");
2010 : : }
2011 : :
2012 : 162 : return range_check (result, "COS");
2013 : : }
2014 : :
2015 : :
2016 : : #if MPFR_VERSION < MPFR_VERSION_NUM(4,2,0)
2017 : : /* Used by trigd_fe.inc. */
2018 : : static void
2019 : : deg2rad (mpfr_t x)
2020 : : {
2021 : : mpfr_t d2r;
2022 : :
2023 : : mpfr_init (d2r);
2024 : : mpfr_const_pi (d2r, GFC_RND_MODE);
2025 : : mpfr_div_ui (d2r, d2r, 180, GFC_RND_MODE);
2026 : : mpfr_mul (x, x, d2r, GFC_RND_MODE);
2027 : : mpfr_clear (d2r);
2028 : : }
2029 : : #endif
2030 : :
2031 : :
2032 : : #if MPFR_VERSION < MPFR_VERSION_NUM(4,2,0)
2033 : : /* Simplification routines for SIND, COSD, TAND. */
2034 : : #include "trigd_fe.inc"
2035 : : #endif
2036 : :
2037 : : /* Simplify COSD(X) where X has the unit of degree. */
2038 : :
2039 : : gfc_expr *
2040 : 181 : gfc_simplify_cosd (gfc_expr *x)
2041 : : {
2042 : 181 : gfc_expr *result;
2043 : :
2044 : 181 : if (x->expr_type != EXPR_CONSTANT)
2045 : : return NULL;
2046 : :
2047 : 25 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2048 : : #if MPFR_VERSION >= MPFR_VERSION_NUM(4,2,0)
2049 : 25 : mpfr_cosu (result->value.real, x->value.real, 360, GFC_RND_MODE);
2050 : : #else
2051 : : mpfr_set (result->value.real, x->value.real, GFC_RND_MODE);
2052 : : simplify_cosd (result->value.real);
2053 : : #endif
2054 : :
2055 : 25 : return range_check (result, "COSD");
2056 : : }
2057 : :
2058 : :
2059 : : /* Simplify SIND(X) where X has the unit of degree. */
2060 : :
2061 : : gfc_expr *
2062 : 181 : gfc_simplify_sind (gfc_expr *x)
2063 : : {
2064 : 181 : gfc_expr *result;
2065 : :
2066 : 181 : if (x->expr_type != EXPR_CONSTANT)
2067 : : return NULL;
2068 : :
2069 : 25 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2070 : : #if MPFR_VERSION >= MPFR_VERSION_NUM(4,2,0)
2071 : 25 : mpfr_sinu (result->value.real, x->value.real, 360, GFC_RND_MODE);
2072 : : #else
2073 : : mpfr_set (result->value.real, x->value.real, GFC_RND_MODE);
2074 : : simplify_sind (result->value.real);
2075 : : #endif
2076 : :
2077 : 25 : return range_check (result, "SIND");
2078 : : }
2079 : :
2080 : :
2081 : : /* Simplify TAND(X) where X has the unit of degree. */
2082 : :
2083 : : gfc_expr *
2084 : 265 : gfc_simplify_tand (gfc_expr *x)
2085 : : {
2086 : 265 : gfc_expr *result;
2087 : :
2088 : 265 : if (x->expr_type != EXPR_CONSTANT)
2089 : : return NULL;
2090 : :
2091 : 25 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2092 : : #if MPFR_VERSION >= MPFR_VERSION_NUM(4,2,0)
2093 : 25 : mpfr_tanu (result->value.real, x->value.real, 360, GFC_RND_MODE);
2094 : : #else
2095 : : mpfr_set (result->value.real, x->value.real, GFC_RND_MODE);
2096 : : simplify_tand (result->value.real);
2097 : : #endif
2098 : :
2099 : 25 : return range_check (result, "TAND");
2100 : : }
2101 : :
2102 : :
2103 : : /* Simplify COTAND(X) where X has the unit of degree. */
2104 : :
2105 : : gfc_expr *
2106 : 241 : gfc_simplify_cotand (gfc_expr *x)
2107 : : {
2108 : 241 : gfc_expr *result;
2109 : :
2110 : 241 : if (x->expr_type != EXPR_CONSTANT)
2111 : : return NULL;
2112 : :
2113 : : /* Implement COTAND = -TAND(x+90).
2114 : : TAND offers correct exact values for multiples of 30 degrees.
2115 : : This implementation is also compatible with the behavior of some legacy
2116 : : compilers. Keep this consistent with gfc_conv_intrinsic_cotand. */
2117 : 25 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2118 : 25 : mpfr_set (result->value.real, x->value.real, GFC_RND_MODE);
2119 : 25 : mpfr_add_ui (result->value.real, result->value.real, 90, GFC_RND_MODE);
2120 : : #if MPFR_VERSION >= MPFR_VERSION_NUM(4,2,0)
2121 : 25 : mpfr_tanu (result->value.real, result->value.real, 360, GFC_RND_MODE);
2122 : : #else
2123 : : simplify_tand (result->value.real);
2124 : : #endif
2125 : 25 : mpfr_neg (result->value.real, result->value.real, GFC_RND_MODE);
2126 : :
2127 : 25 : return range_check (result, "COTAND");
2128 : : }
2129 : :
2130 : :
2131 : : gfc_expr *
2132 : 317 : gfc_simplify_cosh (gfc_expr *x)
2133 : : {
2134 : 317 : gfc_expr *result;
2135 : :
2136 : 317 : if (x->expr_type != EXPR_CONSTANT)
2137 : : return NULL;
2138 : :
2139 : 47 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2140 : :
2141 : 47 : switch (x->ts.type)
2142 : : {
2143 : 43 : case BT_REAL:
2144 : 43 : mpfr_cosh (result->value.real, x->value.real, GFC_RND_MODE);
2145 : 43 : break;
2146 : :
2147 : 4 : case BT_COMPLEX:
2148 : 4 : mpc_cosh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
2149 : 4 : break;
2150 : :
2151 : 0 : default:
2152 : 0 : gcc_unreachable ();
2153 : : }
2154 : :
2155 : 47 : return range_check (result, "COSH");
2156 : : }
2157 : :
2158 : : gfc_expr *
2159 : 25 : gfc_simplify_acospi (gfc_expr *x)
2160 : : {
2161 : 25 : gfc_expr *result;
2162 : :
2163 : 25 : if (x->expr_type != EXPR_CONSTANT)
2164 : : return NULL;
2165 : :
2166 : 25 : if (mpfr_cmp_si (x->value.real, 1) > 0 || mpfr_cmp_si (x->value.real, -1) < 0)
2167 : : {
2168 : 1 : gfc_error (
2169 : : "Argument of ACOSPI at %L must be within the closed interval [-1, 1]",
2170 : : &x->where);
2171 : 1 : return &gfc_bad_expr;
2172 : : }
2173 : :
2174 : 24 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2175 : :
2176 : : #if MPFR_VERSION >= MPFR_VERSION_NUM(4, 2, 0)
2177 : 24 : mpfr_acospi (result->value.real, x->value.real, GFC_RND_MODE);
2178 : : #else
2179 : : mpfr_t pi, tmp;
2180 : : mpfr_inits2 (2 * mpfr_get_prec (x->value.real), pi, tmp, NULL);
2181 : : mpfr_const_pi (pi, GFC_RND_MODE);
2182 : : mpfr_acos (tmp, x->value.real, GFC_RND_MODE);
2183 : : mpfr_div (result->value.real, tmp, pi, GFC_RND_MODE);
2184 : : mpfr_clears (pi, tmp, NULL);
2185 : : #endif
2186 : :
2187 : 24 : return result;
2188 : : }
2189 : :
2190 : : gfc_expr *
2191 : 25 : gfc_simplify_asinpi (gfc_expr *x)
2192 : : {
2193 : 25 : gfc_expr *result;
2194 : :
2195 : 25 : if (x->expr_type != EXPR_CONSTANT)
2196 : : return NULL;
2197 : :
2198 : 25 : if (mpfr_cmp_si (x->value.real, 1) > 0 || mpfr_cmp_si (x->value.real, -1) < 0)
2199 : : {
2200 : 1 : gfc_error (
2201 : : "Argument of ASINPI at %L must be within the closed interval [-1, 1]",
2202 : : &x->where);
2203 : 1 : return &gfc_bad_expr;
2204 : : }
2205 : :
2206 : 24 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2207 : :
2208 : : #if MPFR_VERSION >= MPFR_VERSION_NUM(4, 2, 0)
2209 : 24 : mpfr_asinpi (result->value.real, x->value.real, GFC_RND_MODE);
2210 : : #else
2211 : : mpfr_t pi, tmp;
2212 : : mpfr_inits2 (2 * mpfr_get_prec (x->value.real), pi, tmp, NULL);
2213 : : mpfr_const_pi (pi, GFC_RND_MODE);
2214 : : mpfr_asin (tmp, x->value.real, GFC_RND_MODE);
2215 : : mpfr_div (result->value.real, tmp, pi, GFC_RND_MODE);
2216 : : mpfr_clears (pi, tmp, NULL);
2217 : : #endif
2218 : :
2219 : 24 : return result;
2220 : : }
2221 : :
2222 : : gfc_expr *
2223 : 24 : gfc_simplify_atanpi (gfc_expr *x)
2224 : : {
2225 : 24 : gfc_expr *result;
2226 : :
2227 : 24 : if (x->expr_type != EXPR_CONSTANT)
2228 : : return NULL;
2229 : :
2230 : 24 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2231 : :
2232 : : #if MPFR_VERSION >= MPFR_VERSION_NUM(4, 2, 0)
2233 : 24 : mpfr_atanpi (result->value.real, x->value.real, GFC_RND_MODE);
2234 : : #else
2235 : : mpfr_t pi, tmp;
2236 : : mpfr_inits2 (2 * mpfr_get_prec (x->value.real), pi, tmp, NULL);
2237 : : mpfr_const_pi (pi, GFC_RND_MODE);
2238 : : mpfr_atan (tmp, x->value.real, GFC_RND_MODE);
2239 : : mpfr_div (result->value.real, tmp, pi, GFC_RND_MODE);
2240 : : mpfr_clears (pi, tmp, NULL);
2241 : : #endif
2242 : :
2243 : 24 : return range_check (result, "ATANPI");
2244 : : }
2245 : :
2246 : : gfc_expr *
2247 : 25 : gfc_simplify_atan2pi (gfc_expr *y, gfc_expr *x)
2248 : : {
2249 : 25 : gfc_expr *result;
2250 : :
2251 : 25 : if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2252 : : return NULL;
2253 : :
2254 : 25 : if (mpfr_zero_p (y->value.real) && mpfr_zero_p (x->value.real))
2255 : : {
2256 : 1 : gfc_error ("If the first argument of ATAN2PI at %L is zero, then the "
2257 : : "second argument must not be zero",
2258 : : &y->where);
2259 : 1 : return &gfc_bad_expr;
2260 : : }
2261 : :
2262 : 24 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2263 : :
2264 : : #if MPFR_VERSION >= MPFR_VERSION_NUM(4, 2, 0)
2265 : 24 : mpfr_atan2pi (result->value.real, y->value.real, x->value.real, GFC_RND_MODE);
2266 : : #else
2267 : : mpfr_t pi, tmp;
2268 : : mpfr_inits2 (2 * mpfr_get_prec (x->value.real), pi, tmp, NULL);
2269 : : mpfr_const_pi (pi, GFC_RND_MODE);
2270 : : mpfr_atan2 (tmp, y->value.real, x->value.real, GFC_RND_MODE);
2271 : : mpfr_div (result->value.real, tmp, pi, GFC_RND_MODE);
2272 : : mpfr_clears (pi, tmp, NULL);
2273 : : #endif
2274 : :
2275 : 24 : return range_check (result, "ATAN2PI");
2276 : : }
2277 : :
2278 : : gfc_expr *
2279 : 24 : gfc_simplify_cospi (gfc_expr *x)
2280 : : {
2281 : 24 : gfc_expr *result;
2282 : :
2283 : 24 : if (x->expr_type != EXPR_CONSTANT)
2284 : : return NULL;
2285 : :
2286 : 24 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2287 : :
2288 : : #if MPFR_VERSION >= MPFR_VERSION_NUM(4, 2, 0)
2289 : 24 : mpfr_cospi (result->value.real, x->value.real, GFC_RND_MODE);
2290 : : #else
2291 : : mpfr_t cs, n, r, two;
2292 : : int s;
2293 : :
2294 : : mpfr_inits2 (2 * mpfr_get_prec (x->value.real), cs, n, r, two, NULL);
2295 : :
2296 : : mpfr_abs (r, x->value.real, GFC_RND_MODE);
2297 : : mpfr_modf (n, r, r, GFC_RND_MODE);
2298 : :
2299 : : if (mpfr_cmp_d (r, 0.5) == 0)
2300 : : {
2301 : : mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
2302 : : return result;
2303 : : }
2304 : :
2305 : : mpfr_set_ui (two, 2, GFC_RND_MODE);
2306 : : mpfr_fmod (cs, n, two, GFC_RND_MODE);
2307 : : s = mpfr_cmp_ui (cs, 0) == 0 ? 1 : -1;
2308 : :
2309 : : mpfr_const_pi (cs, GFC_RND_MODE);
2310 : : mpfr_mul (cs, cs, r, GFC_RND_MODE);
2311 : : mpfr_cos (cs, cs, GFC_RND_MODE);
2312 : : mpfr_mul_si (result->value.real, cs, s, GFC_RND_MODE);
2313 : :
2314 : : mpfr_clears (cs, n, r, two, NULL);
2315 : : #endif
2316 : :
2317 : 24 : return range_check (result, "COSPI");
2318 : : }
2319 : :
2320 : : gfc_expr *
2321 : 24 : gfc_simplify_sinpi (gfc_expr *x)
2322 : : {
2323 : 24 : gfc_expr *result;
2324 : :
2325 : 24 : if (x->expr_type != EXPR_CONSTANT)
2326 : : return NULL;
2327 : :
2328 : 24 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2329 : :
2330 : : #if MPFR_VERSION >= MPFR_VERSION_NUM(4, 2, 0)
2331 : 24 : mpfr_sinpi (result->value.real, x->value.real, GFC_RND_MODE);
2332 : : #else
2333 : : mpfr_t sn, n, r, two;
2334 : : int s;
2335 : :
2336 : : mpfr_inits2 (2 * mpfr_get_prec (x->value.real), sn, n, r, two, NULL);
2337 : :
2338 : : mpfr_abs (r, x->value.real, GFC_RND_MODE);
2339 : : mpfr_modf (n, r, r, GFC_RND_MODE);
2340 : :
2341 : : if (mpfr_cmp_d (r, 0.0) == 0)
2342 : : {
2343 : : mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
2344 : : return result;
2345 : : }
2346 : :
2347 : : mpfr_set_ui (two, 2, GFC_RND_MODE);
2348 : : mpfr_fmod (sn, n, two, GFC_RND_MODE);
2349 : : s = mpfr_cmp_si (x->value.real, 0) < 0 ? -1 : 1;
2350 : : s *= mpfr_cmp_ui (sn, 0) == 0 ? 1 : -1;
2351 : :
2352 : : mpfr_const_pi (sn, GFC_RND_MODE);
2353 : : mpfr_mul (sn, sn, r, GFC_RND_MODE);
2354 : : mpfr_sin (sn, sn, GFC_RND_MODE);
2355 : : mpfr_mul_si (result->value.real, sn, s, GFC_RND_MODE);
2356 : :
2357 : : mpfr_clears (sn, n, r, two, NULL);
2358 : : #endif
2359 : :
2360 : 24 : return range_check (result, "SINPI");
2361 : : }
2362 : :
2363 : : gfc_expr *
2364 : 24 : gfc_simplify_tanpi (gfc_expr *x)
2365 : : {
2366 : 24 : gfc_expr *result;
2367 : :
2368 : 24 : if (x->expr_type != EXPR_CONSTANT)
2369 : : return NULL;
2370 : :
2371 : 24 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2372 : :
2373 : : #if MPFR_VERSION >= MPFR_VERSION_NUM(4, 2, 0)
2374 : 24 : mpfr_tanpi (result->value.real, x->value.real, GFC_RND_MODE);
2375 : : #else
2376 : : mpfr_t tn, n, r;
2377 : : int s;
2378 : :
2379 : : mpfr_inits2 (2 * mpfr_get_prec (x->value.real), tn, n, r, NULL);
2380 : :
2381 : : mpfr_abs (r, x->value.real, GFC_RND_MODE);
2382 : : mpfr_modf (n, r, r, GFC_RND_MODE);
2383 : :
2384 : : if (mpfr_cmp_d (r, 0.0) == 0)
2385 : : {
2386 : : mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
2387 : : return result;
2388 : : }
2389 : :
2390 : : s = mpfr_cmp_si (x->value.real, 0) < 0 ? -1 : 1;
2391 : :
2392 : : mpfr_const_pi (tn, GFC_RND_MODE);
2393 : : mpfr_mul (tn, tn, r, GFC_RND_MODE);
2394 : : mpfr_tan (tn, tn, GFC_RND_MODE);
2395 : : mpfr_mul_si (result->value.real, tn, s, GFC_RND_MODE);
2396 : :
2397 : : mpfr_clears (tn, n, r, NULL);
2398 : : #endif
2399 : :
2400 : 24 : return range_check (result, "TANPI");
2401 : : }
2402 : :
2403 : : gfc_expr *
2404 : 441 : gfc_simplify_count (gfc_expr *mask, gfc_expr *dim, gfc_expr *kind)
2405 : : {
2406 : 441 : gfc_expr *result;
2407 : 441 : bool size_zero;
2408 : :
2409 : 441 : size_zero = gfc_is_size_zero_array (mask);
2410 : :
2411 : 827 : if (!(is_constant_array_expr (mask) || size_zero)
2412 : 55 : || !gfc_is_constant_expr (dim)
2413 : 496 : || !gfc_is_constant_expr (kind))
2414 : 386 : return NULL;
2415 : :
2416 : 55 : result = transformational_result (mask, dim,
2417 : : BT_INTEGER,
2418 : : get_kind (BT_INTEGER, kind, "COUNT",
2419 : : gfc_default_integer_kind),
2420 : : &mask->where);
2421 : :
2422 : 55 : init_result_expr (result, 0, NULL);
2423 : :
2424 : 55 : if (size_zero)
2425 : : return result;
2426 : :
2427 : : /* Passing MASK twice, once as data array, once as mask.
2428 : : Whenever gfc_count is called, '1' is added to the result. */
2429 : 30 : return !dim || mask->rank == 1 ?
2430 : 24 : simplify_transformation_to_scalar (result, mask, mask, gfc_count) :
2431 : 30 : simplify_transformation_to_array (result, mask, dim, mask, gfc_count, NULL);
2432 : : }
2433 : :
2434 : : /* Simplification routine for cshift. This works by copying the array
2435 : : expressions into a one-dimensional array, shuffling the values into another
2436 : : one-dimensional array and creating the new array expression from this. The
2437 : : shuffling part is basically taken from the library routine. */
2438 : :
2439 : : gfc_expr *
2440 : 947 : gfc_simplify_cshift (gfc_expr *array, gfc_expr *shift, gfc_expr *dim)
2441 : : {
2442 : 947 : gfc_expr *result;
2443 : 947 : int which;
2444 : 947 : gfc_expr **arrayvec, **resultvec;
2445 : 947 : gfc_expr **rptr, **sptr;
2446 : 947 : mpz_t size;
2447 : 947 : size_t arraysize, shiftsize, i;
2448 : 947 : gfc_constructor *array_ctor, *shift_ctor;
2449 : 947 : ssize_t *shiftvec, *hptr;
2450 : 947 : ssize_t shift_val, len;
2451 : 947 : ssize_t count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS],
2452 : : hs_ex[GFC_MAX_DIMENSIONS + 1],
2453 : : hstride[GFC_MAX_DIMENSIONS], sstride[GFC_MAX_DIMENSIONS],
2454 : : a_extent[GFC_MAX_DIMENSIONS], a_stride[GFC_MAX_DIMENSIONS],
2455 : : h_extent[GFC_MAX_DIMENSIONS],
2456 : : ss_ex[GFC_MAX_DIMENSIONS + 1];
2457 : 947 : ssize_t rsoffset;
2458 : 947 : int d, n;
2459 : 947 : bool continue_loop;
2460 : 947 : gfc_expr **src, **dest;
2461 : :
2462 : 947 : if (!is_constant_array_expr (array))
2463 : : return NULL;
2464 : :
2465 : 80 : if (shift->rank > 0)
2466 : 9 : gfc_simplify_expr (shift, 1);
2467 : :
2468 : 80 : if (!gfc_is_constant_expr (shift))
2469 : : return NULL;
2470 : :
2471 : : /* Make dim zero-based. */
2472 : 80 : if (dim)
2473 : : {
2474 : 25 : if (!gfc_is_constant_expr (dim))
2475 : : return NULL;
2476 : 13 : which = mpz_get_si (dim->value.integer) - 1;
2477 : : }
2478 : : else
2479 : : which = 0;
2480 : :
2481 : 68 : if (array->shape == NULL)
2482 : : return NULL;
2483 : :
2484 : 68 : gfc_array_size (array, &size);
2485 : 68 : arraysize = mpz_get_ui (size);
2486 : 68 : mpz_clear (size);
2487 : :
2488 : 68 : result = gfc_get_array_expr (array->ts.type, array->ts.kind, &array->where);
2489 : 68 : result->shape = gfc_copy_shape (array->shape, array->rank);
2490 : 68 : result->rank = array->rank;
2491 : 68 : result->ts.u.derived = array->ts.u.derived;
2492 : :
2493 : 68 : if (arraysize == 0)
2494 : : return result;
2495 : :
2496 : 67 : arrayvec = XCNEWVEC (gfc_expr *, arraysize);
2497 : 67 : array_ctor = gfc_constructor_first (array->value.constructor);
2498 : 985 : for (i = 0; i < arraysize; i++)
2499 : : {
2500 : 851 : arrayvec[i] = array_ctor->expr;
2501 : 851 : array_ctor = gfc_constructor_next (array_ctor);
2502 : : }
2503 : :
2504 : 67 : resultvec = XCNEWVEC (gfc_expr *, arraysize);
2505 : :
2506 : 67 : sstride[0] = 0;
2507 : 67 : extent[0] = 1;
2508 : 67 : count[0] = 0;
2509 : :
2510 : 161 : for (d=0; d < array->rank; d++)
2511 : : {
2512 : 94 : a_extent[d] = mpz_get_si (array->shape[d]);
2513 : 94 : a_stride[d] = d == 0 ? 1 : a_stride[d-1] * a_extent[d-1];
2514 : : }
2515 : :
2516 : 67 : if (shift->rank > 0)
2517 : : {
2518 : 9 : gfc_array_size (shift, &size);
2519 : 9 : shiftsize = mpz_get_ui (size);
2520 : 9 : mpz_clear (size);
2521 : 9 : shiftvec = XCNEWVEC (ssize_t, shiftsize);
2522 : 9 : shift_ctor = gfc_constructor_first (shift->value.constructor);
2523 : 30 : for (d = 0; d < shift->rank; d++)
2524 : : {
2525 : 12 : h_extent[d] = mpz_get_si (shift->shape[d]);
2526 : 12 : hstride[d] = d == 0 ? 1 : hstride[d-1] * h_extent[d-1];
2527 : : }
2528 : : }
2529 : : else
2530 : : shiftvec = NULL;
2531 : :
2532 : : /* Shut up compiler */
2533 : 67 : len = 1;
2534 : 67 : rsoffset = 1;
2535 : :
2536 : 67 : n = 0;
2537 : 161 : for (d=0; d < array->rank; d++)
2538 : : {
2539 : 94 : if (d == which)
2540 : : {
2541 : 67 : rsoffset = a_stride[d];
2542 : 67 : len = a_extent[d];
2543 : : }
2544 : : else
2545 : : {
2546 : 27 : count[n] = 0;
2547 : 27 : extent[n] = a_extent[d];
2548 : 27 : sstride[n] = a_stride[d];
2549 : 27 : ss_ex[n] = sstride[n] * extent[n];
2550 : 27 : if (shiftvec)
2551 : 12 : hs_ex[n] = hstride[n] * extent[n];
2552 : 27 : n++;
2553 : : }
2554 : : }
2555 : 67 : ss_ex[n] = 0;
2556 : 67 : hs_ex[n] = 0;
2557 : :
2558 : 67 : if (shiftvec)
2559 : : {
2560 : 74 : for (i = 0; i < shiftsize; i++)
2561 : : {
2562 : 65 : ssize_t val;
2563 : 65 : val = mpz_get_si (shift_ctor->expr->value.integer);
2564 : 65 : val = val % len;
2565 : 65 : if (val < 0)
2566 : 18 : val += len;
2567 : 65 : shiftvec[i] = val;
2568 : 65 : shift_ctor = gfc_constructor_next (shift_ctor);
2569 : : }
2570 : : shift_val = 0;
2571 : : }
2572 : : else
2573 : : {
2574 : 58 : shift_val = mpz_get_si (shift->value.integer);
2575 : 58 : shift_val = shift_val % len;
2576 : 58 : if (shift_val < 0)
2577 : 6 : shift_val += len;
2578 : : }
2579 : :
2580 : 67 : continue_loop = true;
2581 : 67 : d = array->rank;
2582 : 67 : rptr = resultvec;
2583 : 67 : sptr = arrayvec;
2584 : 67 : hptr = shiftvec;
2585 : :
2586 : 359 : while (continue_loop)
2587 : : {
2588 : 225 : ssize_t sh;
2589 : 225 : if (shiftvec)
2590 : 65 : sh = *hptr;
2591 : : else
2592 : : sh = shift_val;
2593 : :
2594 : 225 : src = &sptr[sh * rsoffset];
2595 : 225 : dest = rptr;
2596 : 807 : for (n = 0; n < len - sh; n++)
2597 : : {
2598 : 582 : *dest = *src;
2599 : 582 : dest += rsoffset;
2600 : 582 : src += rsoffset;
2601 : : }
2602 : : src = sptr;
2603 : 494 : for ( n = 0; n < sh; n++)
2604 : : {
2605 : 269 : *dest = *src;
2606 : 269 : dest += rsoffset;
2607 : 269 : src += rsoffset;
2608 : : }
2609 : 225 : rptr += sstride[0];
2610 : 225 : sptr += sstride[0];
2611 : 225 : if (shiftvec)
2612 : 65 : hptr += hstride[0];
2613 : 225 : count[0]++;
2614 : 225 : n = 0;
2615 : 268 : while (count[n] == extent[n])
2616 : : {
2617 : 110 : count[n] = 0;
2618 : 110 : rptr -= ss_ex[n];
2619 : 110 : sptr -= ss_ex[n];
2620 : 110 : if (shiftvec)
2621 : 23 : hptr -= hs_ex[n];
2622 : 110 : n++;
2623 : 110 : if (n >= d - 1)
2624 : : {
2625 : : continue_loop = false;
2626 : : break;
2627 : : }
2628 : : else
2629 : : {
2630 : 43 : count[n]++;
2631 : 43 : rptr += sstride[n];
2632 : 43 : sptr += sstride[n];
2633 : 43 : if (shiftvec)
2634 : 14 : hptr += hstride[n];
2635 : : }
2636 : : }
2637 : : }
2638 : :
2639 : 918 : for (i = 0; i < arraysize; i++)
2640 : : {
2641 : 851 : gfc_constructor_append_expr (&result->value.constructor,
2642 : 851 : gfc_copy_expr (resultvec[i]),
2643 : : NULL);
2644 : : }
2645 : : return result;
2646 : : }
2647 : :
2648 : :
2649 : : gfc_expr *
2650 : 299 : gfc_simplify_dcmplx (gfc_expr *x, gfc_expr *y)
2651 : : {
2652 : 299 : return simplify_cmplx ("DCMPLX", x, y, gfc_default_double_kind);
2653 : : }
2654 : :
2655 : :
2656 : : gfc_expr *
2657 : 620 : gfc_simplify_dble (gfc_expr *e)
2658 : : {
2659 : 620 : gfc_expr *result = NULL;
2660 : 620 : int tmp1, tmp2;
2661 : :
2662 : 620 : if (e->expr_type != EXPR_CONSTANT)
2663 : : return NULL;
2664 : :
2665 : : /* For explicit conversion, turn off -Wconversion and -Wconversion-extra
2666 : : warnings. */
2667 : 119 : tmp1 = warn_conversion;
2668 : 119 : tmp2 = warn_conversion_extra;
2669 : 119 : warn_conversion = warn_conversion_extra = 0;
2670 : :
2671 : 119 : result = gfc_convert_constant (e, BT_REAL, gfc_default_double_kind);
2672 : :
2673 : 119 : warn_conversion = tmp1;
2674 : 119 : warn_conversion_extra = tmp2;
2675 : :
2676 : 119 : if (result == &gfc_bad_expr)
2677 : : return &gfc_bad_expr;
2678 : :
2679 : 119 : return range_check (result, "DBLE");
2680 : : }
2681 : :
2682 : :
2683 : : gfc_expr *
2684 : 40 : gfc_simplify_digits (gfc_expr *x)
2685 : : {
2686 : 40 : int i, digits;
2687 : :
2688 : 40 : i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
2689 : :
2690 : 40 : switch (x->ts.type)
2691 : : {
2692 : 1 : case BT_INTEGER:
2693 : 1 : digits = gfc_integer_kinds[i].digits;
2694 : 1 : break;
2695 : :
2696 : 6 : case BT_UNSIGNED:
2697 : 6 : digits = gfc_unsigned_kinds[i].digits;
2698 : 6 : break;
2699 : :
2700 : 33 : case BT_REAL:
2701 : 33 : case BT_COMPLEX:
2702 : 33 : digits = gfc_real_kinds[i].digits;
2703 : 33 : break;
2704 : :
2705 : 0 : default:
2706 : 0 : gcc_unreachable ();
2707 : : }
2708 : :
2709 : 40 : return gfc_get_int_expr (gfc_default_integer_kind, NULL, digits);
2710 : : }
2711 : :
2712 : :
2713 : : gfc_expr *
2714 : 324 : gfc_simplify_dim (gfc_expr *x, gfc_expr *y)
2715 : : {
2716 : 324 : gfc_expr *result;
2717 : 324 : int kind;
2718 : :
2719 : 324 : if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2720 : : return NULL;
2721 : :
2722 : 78 : kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
2723 : 78 : result = gfc_get_constant_expr (x->ts.type, kind, &x->where);
2724 : :
2725 : 78 : switch (x->ts.type)
2726 : : {
2727 : 36 : case BT_INTEGER:
2728 : 36 : if (mpz_cmp (x->value.integer, y->value.integer) > 0)
2729 : 15 : mpz_sub (result->value.integer, x->value.integer, y->value.integer);
2730 : : else
2731 : 21 : mpz_set_ui (result->value.integer, 0);
2732 : :
2733 : : break;
2734 : :
2735 : 42 : case BT_REAL:
2736 : 42 : if (mpfr_cmp (x->value.real, y->value.real) > 0)
2737 : 30 : mpfr_sub (result->value.real, x->value.real, y->value.real,
2738 : : GFC_RND_MODE);
2739 : : else
2740 : 12 : mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
2741 : :
2742 : : break;
2743 : :
2744 : 0 : default:
2745 : 0 : gfc_internal_error ("gfc_simplify_dim(): Bad type");
2746 : : }
2747 : :
2748 : 78 : return range_check (result, "DIM");
2749 : : }
2750 : :
2751 : :
2752 : : gfc_expr*
2753 : 236 : gfc_simplify_dot_product (gfc_expr *vector_a, gfc_expr *vector_b)
2754 : : {
2755 : : /* If vector_a is a zero-sized array, the result is 0 for INTEGER,
2756 : : REAL, and COMPLEX types and .false. for LOGICAL. */
2757 : 236 : if (vector_a->shape && mpz_get_si (vector_a->shape[0]) == 0)
2758 : : {
2759 : 30 : if (vector_a->ts.type == BT_LOGICAL)
2760 : 6 : return gfc_get_logical_expr (gfc_default_logical_kind, NULL, false);
2761 : : else
2762 : 24 : return gfc_get_int_expr (gfc_default_integer_kind, NULL, 0);
2763 : : }
2764 : :
2765 : 206 : if (!is_constant_array_expr (vector_a)
2766 : 206 : || !is_constant_array_expr (vector_b))
2767 : 166 : return NULL;
2768 : :
2769 : 40 : return compute_dot_product (vector_a, 1, 0, vector_b, 1, 0, true);
2770 : : }
2771 : :
2772 : :
2773 : : gfc_expr *
2774 : 34 : gfc_simplify_dprod (gfc_expr *x, gfc_expr *y)
2775 : : {
2776 : 34 : gfc_expr *a1, *a2, *result;
2777 : :
2778 : 34 : if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2779 : : return NULL;
2780 : :
2781 : 6 : a1 = gfc_real2real (x, gfc_default_double_kind);
2782 : 6 : a2 = gfc_real2real (y, gfc_default_double_kind);
2783 : :
2784 : 6 : result = gfc_get_constant_expr (BT_REAL, gfc_default_double_kind, &x->where);
2785 : 6 : mpfr_mul (result->value.real, a1->value.real, a2->value.real, GFC_RND_MODE);
2786 : :
2787 : 6 : gfc_free_expr (a2);
2788 : 6 : gfc_free_expr (a1);
2789 : :
2790 : 6 : return range_check (result, "DPROD");
2791 : : }
2792 : :
2793 : :
2794 : : static gfc_expr *
2795 : 1876 : simplify_dshift (gfc_expr *arg1, gfc_expr *arg2, gfc_expr *shiftarg,
2796 : : bool right)
2797 : : {
2798 : 1876 : gfc_expr *result;
2799 : 1876 : int i, k, size, shift;
2800 : 1876 : bt type = BT_INTEGER;
2801 : :
2802 : 1876 : if (arg1->expr_type != EXPR_CONSTANT || arg2->expr_type != EXPR_CONSTANT
2803 : 1572 : || shiftarg->expr_type != EXPR_CONSTANT)
2804 : : return NULL;
2805 : :
2806 : 1488 : if (flag_unsigned && arg1->ts.type == BT_UNSIGNED)
2807 : : {
2808 : 12 : k = gfc_validate_kind (BT_UNSIGNED, arg1->ts.kind, false);
2809 : 12 : size = gfc_unsigned_kinds[k].bit_size;
2810 : 12 : type = BT_UNSIGNED;
2811 : : }
2812 : : else
2813 : : {
2814 : 1476 : k = gfc_validate_kind (BT_INTEGER, arg1->ts.kind, false);
2815 : 1476 : size = gfc_integer_kinds[k].bit_size;
2816 : : }
2817 : :
2818 : 1488 : gfc_extract_int (shiftarg, &shift);
2819 : :
2820 : : /* DSHIFTR(I,J,SHIFT) = DSHIFTL(I,J,SIZE-SHIFT). */
2821 : 1488 : if (right)
2822 : 744 : shift = size - shift;
2823 : :
2824 : 1488 : result = gfc_get_constant_expr (type, arg1->ts.kind, &arg1->where);
2825 : 1488 : mpz_set_ui (result->value.integer, 0);
2826 : :
2827 : 39456 : for (i = 0; i < shift; i++)
2828 : 36480 : if (mpz_tstbit (arg2->value.integer, size - shift + i))
2829 : 15006 : mpz_setbit (result->value.integer, i);
2830 : :
2831 : 37968 : for (i = 0; i < size - shift; i++)
2832 : 36480 : if (mpz_tstbit (arg1->value.integer, i))
2833 : 14424 : mpz_setbit (result->value.integer, shift + i);
2834 : :
2835 : : /* Convert to a signed value if needed. */
2836 : 1488 : if (type == BT_INTEGER)
2837 : 1476 : gfc_convert_mpz_to_signed (result->value.integer, size);
2838 : : else
2839 : 12 : gfc_reduce_unsigned (result);
2840 : :
2841 : : return result;
2842 : : }
2843 : :
2844 : :
2845 : : gfc_expr *
2846 : 938 : gfc_simplify_dshiftr (gfc_expr *arg1, gfc_expr *arg2, gfc_expr *shiftarg)
2847 : : {
2848 : 938 : return simplify_dshift (arg1, arg2, shiftarg, true);
2849 : : }
2850 : :
2851 : :
2852 : : gfc_expr *
2853 : 938 : gfc_simplify_dshiftl (gfc_expr *arg1, gfc_expr *arg2, gfc_expr *shiftarg)
2854 : : {
2855 : 938 : return simplify_dshift (arg1, arg2, shiftarg, false);
2856 : : }
2857 : :
2858 : :
2859 : : gfc_expr *
2860 : 1568 : gfc_simplify_eoshift (gfc_expr *array, gfc_expr *shift, gfc_expr *boundary,
2861 : : gfc_expr *dim)
2862 : : {
2863 : 1568 : bool temp_boundary;
2864 : 1568 : gfc_expr *bnd;
2865 : 1568 : gfc_expr *result;
2866 : 1568 : int which;
2867 : 1568 : gfc_expr **arrayvec, **resultvec;
2868 : 1568 : gfc_expr **rptr, **sptr;
2869 : 1568 : mpz_t size;
2870 : 1568 : size_t arraysize, i;
2871 : 1568 : gfc_constructor *array_ctor, *shift_ctor, *bnd_ctor;
2872 : 1568 : ssize_t shift_val, len;
2873 : 1568 : ssize_t count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS],
2874 : : sstride[GFC_MAX_DIMENSIONS], a_extent[GFC_MAX_DIMENSIONS],
2875 : : a_stride[GFC_MAX_DIMENSIONS], ss_ex[GFC_MAX_DIMENSIONS + 1];
2876 : 1568 : ssize_t rsoffset;
2877 : 1568 : int d, n;
2878 : 1568 : bool continue_loop;
2879 : 1568 : gfc_expr **src, **dest;
2880 : 1568 : size_t s_len;
2881 : :
2882 : 1568 : if (!is_constant_array_expr (array))
2883 : : return NULL;
2884 : :
2885 : 60 : if (shift->rank > 0)
2886 : 13 : gfc_simplify_expr (shift, 1);
2887 : :
2888 : 60 : if (!gfc_is_constant_expr (shift))
2889 : : return NULL;
2890 : :
2891 : 60 : if (boundary)
2892 : : {
2893 : 29 : if (boundary->rank > 0)
2894 : 6 : gfc_simplify_expr (boundary, 1);
2895 : :
2896 : 29 : if (!gfc_is_constant_expr (boundary))
2897 : : return NULL;
2898 : : }
2899 : :
2900 : 48 : if (dim)
2901 : : {
2902 : 25 : if (!gfc_is_constant_expr (dim))
2903 : : return NULL;
2904 : 19 : which = mpz_get_si (dim->value.integer) - 1;
2905 : : }
2906 : : else
2907 : : which = 0;
2908 : :
2909 : 42 : s_len = 0;
2910 : 42 : if (boundary == NULL)
2911 : : {
2912 : 29 : temp_boundary = true;
2913 : 29 : switch (array->ts.type)
2914 : : {
2915 : :
2916 : 17 : case BT_INTEGER:
2917 : 17 : bnd = gfc_get_int_expr (array->ts.kind, NULL, 0);
2918 : 17 : break;
2919 : :
2920 : 6 : case BT_UNSIGNED:
2921 : 6 : bnd = gfc_get_unsigned_expr (array->ts.kind, NULL, 0);
2922 : 6 : break;
2923 : :
2924 : 0 : case BT_LOGICAL:
2925 : 0 : bnd = gfc_get_logical_expr (array->ts.kind, NULL, 0);
2926 : 0 : break;
2927 : :
2928 : 2 : case BT_REAL:
2929 : 2 : bnd = gfc_get_constant_expr (array->ts.type, array->ts.kind, &gfc_current_locus);
2930 : 2 : mpfr_set_ui (bnd->value.real, 0, GFC_RND_MODE);
2931 : 2 : break;
2932 : :
2933 : 1 : case BT_COMPLEX:
2934 : 1 : bnd = gfc_get_constant_expr (array->ts.type, array->ts.kind, &gfc_current_locus);
2935 : 1 : mpc_set_ui (bnd->value.complex, 0, GFC_RND_MODE);
2936 : 1 : break;
2937 : :
2938 : 3 : case BT_CHARACTER:
2939 : 3 : s_len = mpz_get_ui (array->ts.u.cl->length->value.integer);
2940 : 3 : bnd = gfc_get_character_expr (array->ts.kind, &gfc_current_locus, NULL, s_len);
2941 : 3 : break;
2942 : :
2943 : 0 : default:
2944 : 0 : gcc_unreachable();
2945 : :
2946 : : }
2947 : : }
2948 : : else
2949 : : {
2950 : : temp_boundary = false;
2951 : : bnd = boundary;
2952 : : }
2953 : :
2954 : 42 : gfc_array_size (array, &size);
2955 : 42 : arraysize = mpz_get_ui (size);
2956 : 42 : mpz_clear (size);
2957 : :
2958 : 42 : result = gfc_get_array_expr (array->ts.type, array->ts.kind, &array->where);
2959 : 42 : result->shape = gfc_copy_shape (array->shape, array->rank);
2960 : 42 : result->rank = array->rank;
2961 : 42 : result->ts = array->ts;
2962 : :
2963 : 42 : if (arraysize == 0)
2964 : 1 : goto final;
2965 : :
2966 : 41 : if (array->shape == NULL)
2967 : 1 : goto final;
2968 : :
2969 : 40 : arrayvec = XCNEWVEC (gfc_expr *, arraysize);
2970 : 40 : array_ctor = gfc_constructor_first (array->value.constructor);
2971 : 536 : for (i = 0; i < arraysize; i++)
2972 : : {
2973 : 456 : arrayvec[i] = array_ctor->expr;
2974 : 456 : array_ctor = gfc_constructor_next (array_ctor);
2975 : : }
2976 : :
2977 : 40 : resultvec = XCNEWVEC (gfc_expr *, arraysize);
2978 : :
2979 : 40 : extent[0] = 1;
2980 : 40 : count[0] = 0;
2981 : :
2982 : 110 : for (d=0; d < array->rank; d++)
2983 : : {
2984 : 70 : a_extent[d] = mpz_get_si (array->shape[d]);
2985 : 70 : a_stride[d] = d == 0 ? 1 : a_stride[d-1] * a_extent[d-1];
2986 : : }
2987 : :
2988 : 40 : if (shift->rank > 0)
2989 : : {
2990 : 13 : shift_ctor = gfc_constructor_first (shift->value.constructor);
2991 : 13 : shift_val = 0;
2992 : : }
2993 : : else
2994 : : {
2995 : 27 : shift_ctor = NULL;
2996 : 27 : shift_val = mpz_get_si (shift->value.integer);
2997 : : }
2998 : :
2999 : 40 : if (bnd->rank > 0)
3000 : 4 : bnd_ctor = gfc_constructor_first (bnd->value.constructor);
3001 : : else
3002 : : bnd_ctor = NULL;
3003 : :
3004 : : /* Shut up compiler */
3005 : 40 : len = 1;
3006 : 40 : rsoffset = 1;
3007 : :
3008 : 40 : n = 0;
3009 : 110 : for (d=0; d < array->rank; d++)
3010 : : {
3011 : 70 : if (d == which)
3012 : : {
3013 : 40 : rsoffset = a_stride[d];
3014 : 40 : len = a_extent[d];
3015 : : }
3016 : : else
3017 : : {
3018 : 30 : count[n] = 0;
3019 : 30 : extent[n] = a_extent[d];
3020 : 30 : sstride[n] = a_stride[d];
3021 : 30 : ss_ex[n] = sstride[n] * extent[n];
3022 : 30 : n++;
3023 : : }
3024 : : }
3025 : 40 : ss_ex[n] = 0;
3026 : :
3027 : 40 : continue_loop = true;
3028 : 40 : d = array->rank;
3029 : 40 : rptr = resultvec;
3030 : 40 : sptr = arrayvec;
3031 : :
3032 : 172 : while (continue_loop)
3033 : : {
3034 : 132 : ssize_t sh, delta;
3035 : :
3036 : 132 : if (shift_ctor)
3037 : 60 : sh = mpz_get_si (shift_ctor->expr->value.integer);
3038 : : else
3039 : : sh = shift_val;
3040 : :
3041 : 132 : if (( sh >= 0 ? sh : -sh ) > len)
3042 : : {
3043 : : delta = len;
3044 : : sh = len;
3045 : : }
3046 : : else
3047 : 118 : delta = (sh >= 0) ? sh: -sh;
3048 : :
3049 : 132 : if (sh > 0)
3050 : : {
3051 : 81 : src = &sptr[delta * rsoffset];
3052 : 81 : dest = rptr;
3053 : : }
3054 : : else
3055 : : {
3056 : 51 : src = sptr;
3057 : 51 : dest = &rptr[delta * rsoffset];
3058 : : }
3059 : :
3060 : 387 : for (n = 0; n < len - delta; n++)
3061 : : {
3062 : 255 : *dest = *src;
3063 : 255 : dest += rsoffset;
3064 : 255 : src += rsoffset;
3065 : : }
3066 : :
3067 : 132 : if (sh < 0)
3068 : 45 : dest = rptr;
3069 : :
3070 : 132 : n = delta;
3071 : :
3072 : 132 : if (bnd_ctor)
3073 : : {
3074 : 73 : while (n--)
3075 : : {
3076 : 47 : *dest = gfc_copy_expr (bnd_ctor->expr);
3077 : 47 : dest += rsoffset;
3078 : : }
3079 : : }
3080 : : else
3081 : : {
3082 : 260 : while (n--)
3083 : : {
3084 : 154 : *dest = gfc_copy_expr (bnd);
3085 : 154 : dest += rsoffset;
3086 : : }
3087 : : }
3088 : 132 : rptr += sstride[0];
3089 : 132 : sptr += sstride[0];
3090 : 132 : if (shift_ctor)
3091 : 60 : shift_ctor = gfc_constructor_next (shift_ctor);
3092 : :
3093 : 132 : if (bnd_ctor)
3094 : 26 : bnd_ctor = gfc_constructor_next (bnd_ctor);
3095 : :
3096 : 132 : count[0]++;
3097 : 132 : n = 0;
3098 : 155 : while (count[n] == extent[n])
3099 : : {
3100 : 63 : count[n] = 0;
3101 : 63 : rptr -= ss_ex[n];
3102 : 63 : sptr -= ss_ex[n];
3103 : 63 : n++;
3104 : 63 : if (n >= d - 1)
3105 : : {
3106 : : continue_loop = false;
3107 : : break;
3108 : : }
3109 : : else
3110 : : {
3111 : 23 : count[n]++;
3112 : 23 : rptr += sstride[n];
3113 : 23 : sptr += sstride[n];
3114 : : }
3115 : : }
3116 : : }
3117 : :
3118 : 496 : for (i = 0; i < arraysize; i++)
3119 : : {
3120 : 456 : gfc_constructor_append_expr (&result->value.constructor,
3121 : 456 : gfc_copy_expr (resultvec[i]),
3122 : : NULL);
3123 : : }
3124 : :
3125 : 40 : final:
3126 : 42 : if (temp_boundary)
3127 : 29 : gfc_free_expr (bnd);
3128 : :
3129 : : return result;
3130 : : }
3131 : :
3132 : : gfc_expr *
3133 : 169 : gfc_simplify_erf (gfc_expr *x)
3134 : : {
3135 : 169 : gfc_expr *result;
3136 : :
3137 : 169 : if (x->expr_type != EXPR_CONSTANT)
3138 : : return NULL;
3139 : :
3140 : 35 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
3141 : 35 : mpfr_erf (result->value.real, x->value.real, GFC_RND_MODE);
3142 : :
3143 : 35 : return range_check (result, "ERF");
3144 : : }
3145 : :
3146 : :
3147 : : gfc_expr *
3148 : 242 : gfc_simplify_erfc (gfc_expr *x)
3149 : : {
3150 : 242 : gfc_expr *result;
3151 : :
3152 : 242 : if (x->expr_type != EXPR_CONSTANT)
3153 : : return NULL;
3154 : :
3155 : 36 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
3156 : 36 : mpfr_erfc (result->value.real, x->value.real, GFC_RND_MODE);
3157 : :
3158 : 36 : return range_check (result, "ERFC");
3159 : : }
3160 : :
3161 : :
3162 : : /* Helper functions to simplify ERFC_SCALED(x) = ERFC(x) * EXP(X**2). */
3163 : :
3164 : : #define MAX_ITER 200
3165 : : #define ARG_LIMIT 12
3166 : :
3167 : : /* Calculate ERFC_SCALED directly by its definition:
3168 : :
3169 : : ERFC_SCALED(x) = ERFC(x) * EXP(X**2)
3170 : :
3171 : : using a large precision for intermediate results. This is used for all
3172 : : but large values of the argument. */
3173 : : static void
3174 : 39 : fullprec_erfc_scaled (mpfr_t res, mpfr_t arg)
3175 : : {
3176 : 39 : mpfr_prec_t prec;
3177 : 39 : mpfr_t a, b;
3178 : :
3179 : 39 : prec = mpfr_get_default_prec ();
3180 : 39 : mpfr_set_default_prec (10 * prec);
3181 : :
3182 : 39 : mpfr_init (a);
3183 : 39 : mpfr_init (b);
3184 : :
3185 : 39 : mpfr_set (a, arg, GFC_RND_MODE);
3186 : 39 : mpfr_sqr (b, a, GFC_RND_MODE);
3187 : 39 : mpfr_exp (b, b, GFC_RND_MODE);
3188 : 39 : mpfr_erfc (a, a, GFC_RND_MODE);
3189 : 39 : mpfr_mul (a, a, b, GFC_RND_MODE);
3190 : :
3191 : 39 : mpfr_set (res, a, GFC_RND_MODE);
3192 : 39 : mpfr_set_default_prec (prec);
3193 : :
3194 : 39 : mpfr_clear (a);
3195 : 39 : mpfr_clear (b);
3196 : 39 : }
3197 : :
3198 : : /* Calculate ERFC_SCALED using a power series expansion in 1/arg:
3199 : :
3200 : : ERFC_SCALED(x) = 1 / (x * sqrt(pi))
3201 : : * (1 + Sum_n (-1)**n * (1 * 3 * 5 * ... * (2n-1))
3202 : : / (2 * x**2)**n)
3203 : :
3204 : : This is used for large values of the argument. Intermediate calculations
3205 : : are performed with twice the precision. We don't do a fixed number of
3206 : : iterations of the sum, but stop when it has converged to the required
3207 : : precision. */
3208 : : static void
3209 : 10 : asympt_erfc_scaled (mpfr_t res, mpfr_t arg)
3210 : : {
3211 : 10 : mpfr_t sum, x, u, v, w, oldsum, sumtrunc;
3212 : 10 : mpz_t num;
3213 : 10 : mpfr_prec_t prec;
3214 : 10 : unsigned i;
3215 : :
3216 : 10 : prec = mpfr_get_default_prec ();
3217 : 10 : mpfr_set_default_prec (2 * prec);
3218 : :
3219 : 10 : mpfr_init (sum);
3220 : 10 : mpfr_init (x);
3221 : 10 : mpfr_init (u);
3222 : 10 : mpfr_init (v);
3223 : 10 : mpfr_init (w);
3224 : 10 : mpz_init (num);
3225 : :
3226 : 10 : mpfr_init (oldsum);
3227 : 10 : mpfr_init (sumtrunc);
3228 : 10 : mpfr_set_prec (oldsum, prec);
3229 : 10 : mpfr_set_prec (sumtrunc, prec);
3230 : :
3231 : 10 : mpfr_set (x, arg, GFC_RND_MODE);
3232 : 10 : mpfr_set_ui (sum, 1, GFC_RND_MODE);
3233 : 10 : mpz_set_ui (num, 1);
3234 : :
3235 : 10 : mpfr_set (u, x, GFC_RND_MODE);
3236 : 10 : mpfr_sqr (u, u, GFC_RND_MODE);
3237 : 10 : mpfr_mul_ui (u, u, 2, GFC_RND_MODE);
3238 : 10 : mpfr_pow_si (u, u, -1, GFC_RND_MODE);
3239 : :
3240 : 132 : for (i = 1; i < MAX_ITER; i++)
3241 : : {
3242 : 132 : mpfr_set (oldsum, sum, GFC_RND_MODE);
3243 : :
3244 : 132 : mpz_mul_ui (num, num, 2 * i - 1);
3245 : 132 : mpz_neg (num, num);
3246 : :
3247 : 132 : mpfr_set (w, u, GFC_RND_MODE);
3248 : 132 : mpfr_pow_ui (w, w, i, GFC_RND_MODE);
3249 : :
3250 : 132 : mpfr_set_z (v, num, GFC_RND_MODE);
3251 : 132 : mpfr_mul (v, v, w, GFC_RND_MODE);
3252 : :
3253 : 132 : mpfr_add (sum, sum, v, GFC_RND_MODE);
3254 : :
3255 : 132 : mpfr_set (sumtrunc, sum, GFC_RND_MODE);
3256 : 132 : if (mpfr_cmp (sumtrunc, oldsum) == 0)
3257 : : break;
3258 : : }
3259 : :
3260 : : /* We should have converged by now; otherwise, ARG_LIMIT is probably
3261 : : set too low. */
3262 : 10 : gcc_assert (i < MAX_ITER);
3263 : :
3264 : : /* Divide by x * sqrt(Pi). */
3265 : 10 : mpfr_const_pi (u, GFC_RND_MODE);
3266 : 10 : mpfr_sqrt (u, u, GFC_RND_MODE);
3267 : 10 : mpfr_mul (u, u, x, GFC_RND_MODE);
3268 : 10 : mpfr_div (sum, sum, u, GFC_RND_MODE);
3269 : :
3270 : 10 : mpfr_set (res, sum, GFC_RND_MODE);
3271 : 10 : mpfr_set_default_prec (prec);
3272 : :
3273 : 10 : mpfr_clears (sum, x, u, v, w, oldsum, sumtrunc, NULL);
3274 : 10 : mpz_clear (num);
3275 : 10 : }
3276 : :
3277 : :
3278 : : gfc_expr *
3279 : 143 : gfc_simplify_erfc_scaled (gfc_expr *x)
3280 : : {
3281 : 143 : gfc_expr *result;
3282 : :
3283 : 143 : if (x->expr_type != EXPR_CONSTANT)
3284 : : return NULL;
3285 : :
3286 : 49 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
3287 : 49 : if (mpfr_cmp_d (x->value.real, ARG_LIMIT) >= 0)
3288 : 10 : asympt_erfc_scaled (result->value.real, x->value.real);
3289 : : else
3290 : 39 : fullprec_erfc_scaled (result->value.real, x->value.real);
3291 : :
3292 : 49 : return range_check (result, "ERFC_SCALED");
3293 : : }
3294 : :
3295 : : #undef MAX_ITER
3296 : : #undef ARG_LIMIT
3297 : :
3298 : :
3299 : : gfc_expr *
3300 : 3624 : gfc_simplify_epsilon (gfc_expr *e)
3301 : : {
3302 : 3624 : gfc_expr *result;
3303 : 3624 : int i;
3304 : :
3305 : 3624 : i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
3306 : :
3307 : 3624 : result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
3308 : 3624 : mpfr_set (result->value.real, gfc_real_kinds[i].epsilon, GFC_RND_MODE);
3309 : :
3310 : 3624 : return range_check (result, "EPSILON");
3311 : : }
3312 : :
3313 : :
3314 : : gfc_expr *
3315 : 1218 : gfc_simplify_exp (gfc_expr *x)
3316 : : {
3317 : 1218 : gfc_expr *result;
3318 : :
3319 : 1218 : if (x->expr_type != EXPR_CONSTANT)
3320 : : return NULL;
3321 : :
3322 : 151 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
3323 : :
3324 : 151 : switch (x->ts.type)
3325 : : {
3326 : 88 : case BT_REAL:
3327 : 88 : mpfr_exp (result->value.real, x->value.real, GFC_RND_MODE);
3328 : 88 : break;
3329 : :
3330 : 63 : case BT_COMPLEX:
3331 : 63 : gfc_set_model_kind (x->ts.kind);
3332 : 63 : mpc_exp (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
3333 : 63 : break;
3334 : :
3335 : 0 : default:
3336 : 0 : gfc_internal_error ("in gfc_simplify_exp(): Bad type");
3337 : : }
3338 : :
3339 : 151 : return range_check (result, "EXP");
3340 : : }
3341 : :
3342 : :
3343 : : gfc_expr *
3344 : 1020 : gfc_simplify_exponent (gfc_expr *x)
3345 : : {
3346 : 1020 : long int val;
3347 : 1020 : gfc_expr *result;
3348 : :
3349 : 1020 : if (x->expr_type != EXPR_CONSTANT)
3350 : : return NULL;
3351 : :
3352 : 150 : result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
3353 : : &x->where);
3354 : :
3355 : : /* EXPONENT(inf) = EXPONENT(nan) = HUGE(0) */
3356 : 150 : if (mpfr_inf_p (x->value.real) || mpfr_nan_p (x->value.real))
3357 : : {
3358 : 18 : int i = gfc_validate_kind (BT_INTEGER, gfc_default_integer_kind, false);
3359 : 18 : mpz_set (result->value.integer, gfc_integer_kinds[i].huge);
3360 : 18 : return result;
3361 : : }
3362 : :
3363 : : /* EXPONENT(+/- 0.0) = 0 */
3364 : 132 : if (mpfr_zero_p (x->value.real))
3365 : : {
3366 : 12 : mpz_set_ui (result->value.integer, 0);
3367 : 12 : return result;
3368 : : }
3369 : :
3370 : 120 : gfc_set_model (x->value.real);
3371 : :
3372 : 120 : val = (long int) mpfr_get_exp (x->value.real);
3373 : 120 : mpz_set_si (result->value.integer, val);
3374 : :
3375 : 120 : return range_check (result, "EXPONENT");
3376 : : }
3377 : :
3378 : :
3379 : : gfc_expr *
3380 : 60 : gfc_simplify_failed_or_stopped_images (gfc_expr *team ATTRIBUTE_UNUSED,
3381 : : gfc_expr *kind)
3382 : : {
3383 : 60 : if (flag_coarray == GFC_FCOARRAY_NONE)
3384 : : {
3385 : 0 : gfc_current_locus = *gfc_current_intrinsic_where;
3386 : 0 : gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3387 : : return &gfc_bad_expr;
3388 : : }
3389 : :
3390 : 60 : if (flag_coarray == GFC_FCOARRAY_SINGLE)
3391 : : {
3392 : 12 : gfc_expr *result;
3393 : 12 : int actual_kind;
3394 : 12 : if (kind)
3395 : 8 : gfc_extract_int (kind, &actual_kind);
3396 : : else
3397 : 4 : actual_kind = gfc_default_integer_kind;
3398 : :
3399 : 12 : result = gfc_get_array_expr (BT_INTEGER, actual_kind, &gfc_current_locus);
3400 : 12 : result->rank = 1;
3401 : 12 : return result;
3402 : : }
3403 : :
3404 : : /* For fcoarray = lib no simplification is possible, because it is not known
3405 : : what images failed or are stopped at compile time. */
3406 : : return NULL;
3407 : : }
3408 : :
3409 : :
3410 : : gfc_expr *
3411 : 25 : gfc_simplify_get_team (gfc_expr *level ATTRIBUTE_UNUSED)
3412 : : {
3413 : 25 : if (flag_coarray == GFC_FCOARRAY_NONE)
3414 : : {
3415 : 0 : gfc_current_locus = *gfc_current_intrinsic_where;
3416 : 0 : gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3417 : : return &gfc_bad_expr;
3418 : : }
3419 : :
3420 : 25 : if (flag_coarray == GFC_FCOARRAY_SINGLE)
3421 : : {
3422 : 9 : gfc_expr *result;
3423 : 9 : result = gfc_get_null_expr (&gfc_current_locus);
3424 : 9 : result->ts.type = BT_DERIVED;
3425 : 9 : gfc_find_symbol ("team_type", gfc_current_ns, 1, &result->ts.u.derived);
3426 : :
3427 : 9 : return result;
3428 : : }
3429 : :
3430 : : /* For fcoarray = lib no simplification is possible, because it is not known
3431 : : what images failed or are stopped at compile time. */
3432 : : return NULL;
3433 : : }
3434 : :
3435 : :
3436 : : gfc_expr *
3437 : 865 : gfc_simplify_float (gfc_expr *a)
3438 : : {
3439 : 865 : gfc_expr *result;
3440 : :
3441 : 865 : if (a->expr_type != EXPR_CONSTANT)
3442 : : return NULL;
3443 : :
3444 : 493 : result = gfc_int2real (a, gfc_default_real_kind);
3445 : :
3446 : 493 : return range_check (result, "FLOAT");
3447 : : }
3448 : :
3449 : :
3450 : : static bool
3451 : 2384 : is_last_ref_vtab (gfc_expr *e)
3452 : : {
3453 : 2384 : gfc_ref *ref;
3454 : 2384 : gfc_component *comp = NULL;
3455 : :
3456 : 2384 : if (e->expr_type != EXPR_VARIABLE)
3457 : : return false;
3458 : :
3459 : 3424 : for (ref = e->ref; ref; ref = ref->next)
3460 : 1058 : if (ref->type == REF_COMPONENT)
3461 : 444 : comp = ref->u.c.component;
3462 : :
3463 : 2366 : if (!e->ref || !comp)
3464 : 1946 : return e->symtree->n.sym->attr.vtab;
3465 : :
3466 : 420 : if (comp->name[0] == '_' && strcmp (comp->name, "_vptr") == 0)
3467 : 147 : return true;
3468 : :
3469 : : return false;
3470 : : }
3471 : :
3472 : :
3473 : : gfc_expr *
3474 : 542 : gfc_simplify_extends_type_of (gfc_expr *a, gfc_expr *mold)
3475 : : {
3476 : : /* Avoid simplification of resolved symbols. */
3477 : 542 : if (is_last_ref_vtab (a) || is_last_ref_vtab (mold))
3478 : : return NULL;
3479 : :
3480 : 324 : if (a->ts.type == BT_DERIVED && mold->ts.type == BT_DERIVED)
3481 : 27 : return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
3482 : 27 : gfc_type_is_extension_of (mold->ts.u.derived,
3483 : 54 : a->ts.u.derived));
3484 : :
3485 : 297 : if (UNLIMITED_POLY (a) || UNLIMITED_POLY (mold))
3486 : : return NULL;
3487 : :
3488 : 105 : if ((a->ts.type == BT_CLASS && !gfc_expr_attr (a).class_ok)
3489 : 239 : || (mold->ts.type == BT_CLASS && !gfc_expr_attr (mold).class_ok))
3490 : 4 : return NULL;
3491 : :
3492 : : /* Return .false. if the dynamic type can never be an extension. */
3493 : 104 : if ((a->ts.type == BT_CLASS && mold->ts.type == BT_CLASS
3494 : 40 : && !gfc_type_is_extension_of
3495 : 40 : (CLASS_DATA (mold)->ts.u.derived,
3496 : 40 : CLASS_DATA (a)->ts.u.derived)
3497 : 5 : && !gfc_type_is_extension_of
3498 : 5 : (CLASS_DATA (a)->ts.u.derived,
3499 : 5 : CLASS_DATA (mold)->ts.u.derived))
3500 : 127 : || (a->ts.type == BT_DERIVED && mold->ts.type == BT_CLASS
3501 : 27 : && !gfc_type_is_extension_of
3502 : 27 : (CLASS_DATA (mold)->ts.u.derived,
3503 : 27 : a->ts.u.derived))
3504 : 253 : || (a->ts.type == BT_CLASS && mold->ts.type == BT_DERIVED
3505 : 64 : && !gfc_type_is_extension_of
3506 : 64 : (mold->ts.u.derived,
3507 : 64 : CLASS_DATA (a)->ts.u.derived)
3508 : 19 : && !gfc_type_is_extension_of
3509 : 19 : (CLASS_DATA (a)->ts.u.derived,
3510 : 19 : mold->ts.u.derived)))
3511 : 13 : return gfc_get_logical_expr (gfc_default_logical_kind, &a->where, false);
3512 : :
3513 : : /* Return .true. if the dynamic type is guaranteed to be an extension. */
3514 : 96 : if (a->ts.type == BT_CLASS && mold->ts.type == BT_DERIVED
3515 : 178 : && gfc_type_is_extension_of (mold->ts.u.derived,
3516 : 60 : CLASS_DATA (a)->ts.u.derived))
3517 : 45 : return gfc_get_logical_expr (gfc_default_logical_kind, &a->where, true);
3518 : :
3519 : : return NULL;
3520 : : }
3521 : :
3522 : :
3523 : : gfc_expr *
3524 : 759 : gfc_simplify_same_type_as (gfc_expr *a, gfc_expr *b)
3525 : : {
3526 : : /* Avoid simplification of resolved symbols. */
3527 : 759 : if (is_last_ref_vtab (a) || is_last_ref_vtab (b))
3528 : : return NULL;
3529 : :
3530 : : /* Return .false. if the dynamic type can never be the
3531 : : same. */
3532 : 657 : if (((a->ts.type == BT_CLASS && gfc_expr_attr (a).class_ok)
3533 : 103 : || (b->ts.type == BT_CLASS && gfc_expr_attr (b).class_ok))
3534 : 740 : && !gfc_type_compatible (&a->ts, &b->ts)
3535 : 801 : && !gfc_type_compatible (&b->ts, &a->ts))
3536 : 6 : return gfc_get_logical_expr (gfc_default_logical_kind, &a->where, false);
3537 : :
3538 : 753 : if (a->ts.type != BT_DERIVED || b->ts.type != BT_DERIVED)
3539 : : return NULL;
3540 : :
3541 : 18 : return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
3542 : 18 : gfc_compare_derived_types (a->ts.u.derived,
3543 : 36 : b->ts.u.derived));
3544 : : }
3545 : :
3546 : :
3547 : : gfc_expr *
3548 : 414 : gfc_simplify_floor (gfc_expr *e, gfc_expr *k)
3549 : : {
3550 : 414 : gfc_expr *result;
3551 : 414 : mpfr_t floor;
3552 : 414 : int kind;
3553 : :
3554 : 414 : kind = get_kind (BT_INTEGER, k, "FLOOR", gfc_default_integer_kind);
3555 : 414 : if (kind == -1)
3556 : 0 : gfc_internal_error ("gfc_simplify_floor(): Bad kind");
3557 : :
3558 : 414 : if (e->expr_type != EXPR_CONSTANT)
3559 : : return NULL;
3560 : :
3561 : 28 : mpfr_init2 (floor, mpfr_get_prec (e->value.real));
3562 : 28 : mpfr_floor (floor, e->value.real);
3563 : :
3564 : 28 : result = gfc_get_constant_expr (BT_INTEGER, kind, &e->where);
3565 : 28 : gfc_mpfr_to_mpz (result->value.integer, floor, &e->where);
3566 : :
3567 : 28 : mpfr_clear (floor);
3568 : :
3569 : 28 : return range_check (result, "FLOOR");
3570 : : }
3571 : :
3572 : :
3573 : : gfc_expr *
3574 : 264 : gfc_simplify_fraction (gfc_expr *x)
3575 : : {
3576 : 264 : gfc_expr *result;
3577 : 264 : mpfr_exp_t e;
3578 : :
3579 : 264 : if (x->expr_type != EXPR_CONSTANT)
3580 : : return NULL;
3581 : :
3582 : 84 : result = gfc_get_constant_expr (BT_REAL, x->ts.kind, &x->where);
3583 : :
3584 : : /* FRACTION(inf) = NaN. */
3585 : 84 : if (mpfr_inf_p (x->value.real))
3586 : : {
3587 : 12 : mpfr_set_nan (result->value.real);
3588 : 12 : return result;
3589 : : }
3590 : :
3591 : : /* mpfr_frexp() correctly handles zeros and NaNs. */
3592 : 72 : mpfr_frexp (&e, result->value.real, x->value.real, GFC_RND_MODE);
3593 : :
3594 : 72 : return range_check (result, "FRACTION");
3595 : : }
3596 : :
3597 : :
3598 : : gfc_expr *
3599 : 204 : gfc_simplify_gamma (gfc_expr *x)
3600 : : {
3601 : 204 : gfc_expr *result;
3602 : :
3603 : 204 : if (x->expr_type != EXPR_CONSTANT)
3604 : : return NULL;
3605 : :
3606 : 54 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
3607 : 54 : mpfr_gamma (result->value.real, x->value.real, GFC_RND_MODE);
3608 : :
3609 : 54 : return range_check (result, "GAMMA");
3610 : : }
3611 : :
3612 : :
3613 : : gfc_expr *
3614 : 5790 : gfc_simplify_huge (gfc_expr *e)
3615 : : {
3616 : 5790 : gfc_expr *result;
3617 : 5790 : int i;
3618 : :
3619 : 5790 : i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
3620 : 5790 : result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
3621 : :
3622 : 5790 : switch (e->ts.type)
3623 : : {
3624 : 4326 : case BT_INTEGER:
3625 : 4326 : mpz_set (result->value.integer, gfc_integer_kinds[i].huge);
3626 : 4326 : break;
3627 : :
3628 : 156 : case BT_UNSIGNED:
3629 : 156 : mpz_set (result->value.integer, gfc_unsigned_kinds[i].huge);
3630 : 156 : break;
3631 : :
3632 : 1308 : case BT_REAL:
3633 : 1308 : mpfr_set (result->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
3634 : 1308 : break;
3635 : :
3636 : 0 : default:
3637 : 0 : gcc_unreachable ();
3638 : : }
3639 : :
3640 : 5790 : return result;
3641 : : }
3642 : :
3643 : :
3644 : : gfc_expr *
3645 : 36 : gfc_simplify_hypot (gfc_expr *x, gfc_expr *y)
3646 : : {
3647 : 36 : gfc_expr *result;
3648 : :
3649 : 36 : if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
3650 : : return NULL;
3651 : :
3652 : 12 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
3653 : 12 : mpfr_hypot (result->value.real, x->value.real, y->value.real, GFC_RND_MODE);
3654 : 12 : return range_check (result, "HYPOT");
3655 : : }
3656 : :
3657 : :
3658 : : /* We use the processor's collating sequence, because all
3659 : : systems that gfortran currently works on are ASCII. */
3660 : :
3661 : : gfc_expr *
3662 : 9877 : gfc_simplify_iachar (gfc_expr *e, gfc_expr *kind)
3663 : : {
3664 : 9877 : gfc_expr *result;
3665 : 9877 : gfc_char_t index;
3666 : 9877 : int k;
3667 : :
3668 : 9877 : if (e->expr_type != EXPR_CONSTANT)
3669 : : return NULL;
3670 : :
3671 : 4964 : if (e->value.character.length != 1)
3672 : : {
3673 : 0 : gfc_error ("Argument of IACHAR at %L must be of length one", &e->where);
3674 : 0 : return &gfc_bad_expr;
3675 : : }
3676 : :
3677 : 4964 : index = e->value.character.string[0];
3678 : :
3679 : 4964 : if (warn_surprising && index > 127)
3680 : 1 : gfc_warning (OPT_Wsurprising,
3681 : : "Argument of IACHAR function at %L outside of range 0..127",
3682 : : &e->where);
3683 : :
3684 : 4964 : k = get_kind (BT_INTEGER, kind, "IACHAR", gfc_default_integer_kind);
3685 : 4964 : if (k == -1)
3686 : : return &gfc_bad_expr;
3687 : :
3688 : 4964 : result = gfc_get_int_expr (k, &e->where, index);
3689 : :
3690 : 4964 : return range_check (result, "IACHAR");
3691 : : }
3692 : :
3693 : :
3694 : : static gfc_expr *
3695 : 96 : do_bit_and (gfc_expr *result, gfc_expr *e)
3696 : : {
3697 : 96 : if (flag_unsigned)
3698 : : {
3699 : 72 : gcc_assert ((e->ts.type == BT_INTEGER || e->ts.type == BT_UNSIGNED)
3700 : : && e->expr_type == EXPR_CONSTANT);
3701 : 72 : gcc_assert ((result->ts.type == BT_INTEGER
3702 : : || result->ts.type == BT_UNSIGNED)
3703 : : && result->expr_type == EXPR_CONSTANT);
3704 : : }
3705 : : else
3706 : : {
3707 : 24 : gcc_assert (e->ts.type == BT_INTEGER && e->expr_type == EXPR_CONSTANT);
3708 : 24 : gcc_assert (result->ts.type == BT_INTEGER
3709 : : && result->expr_type == EXPR_CONSTANT);
3710 : : }
3711 : :
3712 : 96 : mpz_and (result->value.integer, result->value.integer, e->value.integer);
3713 : 96 : return result;
3714 : : }
3715 : :
3716 : :
3717 : : gfc_expr *
3718 : 217 : gfc_simplify_iall (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
3719 : : {
3720 : 217 : return simplify_transformation (array, dim, mask, -1, do_bit_and);
3721 : : }
3722 : :
3723 : :
3724 : : static gfc_expr *
3725 : 96 : do_bit_ior (gfc_expr *result, gfc_expr *e)
3726 : : {
3727 : 96 : if (flag_unsigned)
3728 : : {
3729 : 72 : gcc_assert ((e->ts.type == BT_INTEGER || e->ts.type == BT_UNSIGNED)
3730 : : && e->expr_type == EXPR_CONSTANT);
3731 : 72 : gcc_assert ((result->ts.type == BT_INTEGER
3732 : : || result->ts.type == BT_UNSIGNED)
3733 : : && result->expr_type == EXPR_CONSTANT);
3734 : : }
3735 : : else
3736 : : {
3737 : 24 : gcc_assert (e->ts.type == BT_INTEGER && e->expr_type == EXPR_CONSTANT);
3738 : 24 : gcc_assert (result->ts.type == BT_INTEGER
3739 : : && result->expr_type == EXPR_CONSTANT);
3740 : : }
3741 : :
3742 : 96 : mpz_ior (result->value.integer, result->value.integer, e->value.integer);
3743 : 96 : return result;
3744 : : }
3745 : :
3746 : :
3747 : : gfc_expr *
3748 : 169 : gfc_simplify_iany (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
3749 : : {
3750 : 169 : return simplify_transformation (array, dim, mask, 0, do_bit_ior);
3751 : : }
3752 : :
3753 : :
3754 : : gfc_expr *
3755 : 1863 : gfc_simplify_iand (gfc_expr *x, gfc_expr *y)
3756 : : {
3757 : 1863 : gfc_expr *result;
3758 : 1863 : bt type;
3759 : :
3760 : 1863 : if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
3761 : : return NULL;
3762 : :
3763 : 269 : type = x->ts.type == BT_UNSIGNED ? BT_UNSIGNED : BT_INTEGER;
3764 : 269 : result = gfc_get_constant_expr (type, x->ts.kind, &x->where);
3765 : 269 : mpz_and (result->value.integer, x->value.integer, y->value.integer);
3766 : :
3767 : 269 : return range_check (result, "IAND");
3768 : : }
3769 : :
3770 : :
3771 : : gfc_expr *
3772 : 448 : gfc_simplify_ibclr (gfc_expr *x, gfc_expr *y)
3773 : : {
3774 : 448 : gfc_expr *result;
3775 : 448 : int k, pos;
3776 : :
3777 : 448 : if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
3778 : : return NULL;
3779 : :
3780 : 66 : if (!gfc_check_bitfcn (x, y))
3781 : : return &gfc_bad_expr;
3782 : :
3783 : 58 : gfc_extract_int (y, &pos);
3784 : :
3785 : 58 : k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
3786 : :
3787 : 58 : result = gfc_copy_expr (x);
3788 : : /* Drop any separate memory representation of x to avoid potential
3789 : : inconsistencies in result. */
3790 : 58 : if (result->representation.string)
3791 : : {
3792 : 12 : free (result->representation.string);
3793 : 12 : result->representation.string = NULL;
3794 : : }
3795 : :
3796 : 58 : if (x->ts.type == BT_INTEGER)
3797 : : {
3798 : 52 : gfc_convert_mpz_to_unsigned (result->value.integer,
3799 : : gfc_integer_kinds[k].bit_size);
3800 : :
3801 : 52 : mpz_clrbit (result->value.integer, pos);
3802 : :
3803 : 52 : gfc_convert_mpz_to_signed (result->value.integer,
3804 : : gfc_integer_kinds[k].bit_size);
3805 : : }
3806 : : else
3807 : 6 : mpz_clrbit (result->value.integer, pos);
3808 : :
3809 : : return result;
3810 : : }
3811 : :
3812 : :
3813 : : gfc_expr *
3814 : 106 : gfc_simplify_ibits (gfc_expr *x, gfc_expr *y, gfc_expr *z)
3815 : : {
3816 : 106 : gfc_expr *result;
3817 : 106 : int pos, len;
3818 : 106 : int i, k, bitsize;
3819 : 106 : int *bits;
3820 : :
3821 : 106 : if (x->expr_type != EXPR_CONSTANT
3822 : 43 : || y->expr_type != EXPR_CONSTANT
3823 : 33 : || z->expr_type != EXPR_CONSTANT)
3824 : : return NULL;
3825 : :
3826 : 28 : if (!gfc_check_ibits (x, y, z))
3827 : : return &gfc_bad_expr;
3828 : :
3829 : 16 : gfc_extract_int (y, &pos);
3830 : 16 : gfc_extract_int (z, &len);
3831 : :
3832 : 16 : k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
3833 : :
3834 : 16 : if (x->ts.type == BT_INTEGER)
3835 : 10 : bitsize = gfc_integer_kinds[k].bit_size;
3836 : : else
3837 : 6 : bitsize = gfc_unsigned_kinds[k].bit_size;
3838 : :
3839 : :
3840 : 16 : if (pos + len > bitsize)
3841 : : {
3842 : 0 : gfc_error ("Sum of second and third arguments of IBITS exceeds "
3843 : : "bit size at %L", &y->where);
3844 : 0 : return &gfc_bad_expr;
3845 : : }
3846 : :
3847 : 16 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
3848 : :
3849 : 16 : if (x->ts.type == BT_INTEGER)
3850 : 10 : gfc_convert_mpz_to_unsigned (result->value.integer,
3851 : : gfc_integer_kinds[k].bit_size);
3852 : :
3853 : 16 : bits = XCNEWVEC (int, bitsize);
3854 : :
3855 : 576 : for (i = 0; i < bitsize; i++)
3856 : 544 : bits[i] = 0;
3857 : :
3858 : 60 : for (i = 0; i < len; i++)
3859 : 44 : bits[i] = mpz_tstbit (x->value.integer, i + pos);
3860 : :
3861 : 560 : for (i = 0; i < bitsize; i++)
3862 : : {
3863 : 544 : if (bits[i] == 0)
3864 : 544 : mpz_clrbit (result->value.integer, i);
3865 : 0 : else if (bits[i] == 1)
3866 : 0 : mpz_setbit (result->value.integer, i);
3867 : : else
3868 : 0 : gfc_internal_error ("IBITS: Bad bit");
3869 : : }
3870 : :
3871 : 16 : free (bits);
3872 : :
3873 : 16 : if (x->ts.type == BT_INTEGER)
3874 : 10 : gfc_convert_mpz_to_signed (result->value.integer,
3875 : : gfc_integer_kinds[k].bit_size);
3876 : :
3877 : : return result;
3878 : : }
3879 : :
3880 : :
3881 : : gfc_expr *
3882 : 394 : gfc_simplify_ibset (gfc_expr *x, gfc_expr *y)
3883 : : {
3884 : 394 : gfc_expr *result;
3885 : 394 : int k, pos;
3886 : :
3887 : 394 : if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
3888 : : return NULL;
3889 : :
3890 : 72 : if (!gfc_check_bitfcn (x, y))
3891 : : return &gfc_bad_expr;
3892 : :
3893 : 64 : gfc_extract_int (y, &pos);
3894 : :
3895 : 64 : k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
3896 : :
3897 : 64 : result = gfc_copy_expr (x);
3898 : : /* Drop any separate memory representation of x to avoid potential
3899 : : inconsistencies in result. */
3900 : 64 : if (result->representation.string)
3901 : : {
3902 : 12 : free (result->representation.string);
3903 : 12 : result->representation.string = NULL;
3904 : : }
3905 : :
3906 : 64 : if (x->ts.type == BT_INTEGER)
3907 : : {
3908 : 58 : gfc_convert_mpz_to_unsigned (result->value.integer,
3909 : : gfc_integer_kinds[k].bit_size);
3910 : :
3911 : 58 : mpz_setbit (result->value.integer, pos);
3912 : :
3913 : 58 : gfc_convert_mpz_to_signed (result->value.integer,
3914 : : gfc_integer_kinds[k].bit_size);
3915 : : }
3916 : : else
3917 : 6 : mpz_setbit (result->value.integer, pos);
3918 : :
3919 : : return result;
3920 : : }
3921 : :
3922 : :
3923 : : gfc_expr *
3924 : 3667 : gfc_simplify_ichar (gfc_expr *e, gfc_expr *kind)
3925 : : {
3926 : 3667 : gfc_expr *result;
3927 : 3667 : gfc_char_t index;
3928 : 3667 : int k;
3929 : :
3930 : 3667 : if (e->expr_type != EXPR_CONSTANT)
3931 : : return NULL;
3932 : :
3933 : 1957 : if (e->value.character.length != 1)
3934 : : {
3935 : 2 : gfc_error ("Argument of ICHAR at %L must be of length one", &e->where);
3936 : 2 : return &gfc_bad_expr;
3937 : : }
3938 : :
3939 : 1955 : index = e->value.character.string[0];
3940 : :
3941 : 1955 : k = get_kind (BT_INTEGER, kind, "ICHAR", gfc_default_integer_kind);
3942 : 1955 : if (k == -1)
3943 : : return &gfc_bad_expr;
3944 : :
3945 : 1955 : result = gfc_get_int_expr (k, &e->where, index);
3946 : :
3947 : 1955 : return range_check (result, "ICHAR");
3948 : : }
3949 : :
3950 : :
3951 : : gfc_expr *
3952 : 1926 : gfc_simplify_ieor (gfc_expr *x, gfc_expr *y)
3953 : : {
3954 : 1926 : gfc_expr *result;
3955 : 1926 : bt type;
3956 : :
3957 : 1926 : if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
3958 : : return NULL;
3959 : :
3960 : 155 : type = x->ts.type == BT_UNSIGNED ? BT_UNSIGNED : BT_INTEGER;
3961 : 155 : result = gfc_get_constant_expr (type, x->ts.kind, &x->where);
3962 : 155 : mpz_xor (result->value.integer, x->value.integer, y->value.integer);
3963 : :
3964 : 155 : return range_check (result, "IEOR");
3965 : : }
3966 : :
3967 : :
3968 : : gfc_expr *
3969 : 1302 : gfc_simplify_index (gfc_expr *x, gfc_expr *y, gfc_expr *b, gfc_expr *kind)
3970 : : {
3971 : 1302 : gfc_expr *result;
3972 : 1302 : bool back;
3973 : 1302 : HOST_WIDE_INT len, lensub, start, last, i, index = 0;
3974 : 1302 : int k, delta;
3975 : :
3976 : 1302 : if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT
3977 : 332 : || ( b != NULL && b->expr_type != EXPR_CONSTANT))
3978 : : return NULL;
3979 : :
3980 : 206 : back = (b != NULL && b->value.logical != 0);
3981 : :
3982 : 274 : k = get_kind (BT_INTEGER, kind, "INDEX", gfc_default_integer_kind);
3983 : 274 : if (k == -1)
3984 : : return &gfc_bad_expr;
3985 : :
3986 : 274 : result = gfc_get_constant_expr (BT_INTEGER, k, &x->where);
3987 : :
3988 : 274 : len = x->value.character.length;
3989 : 274 : lensub = y->value.character.length;
3990 : :
3991 : 274 : if (len < lensub)
3992 : : {
3993 : 12 : mpz_set_si (result->value.integer, 0);
3994 : 12 : return result;
3995 : : }
3996 : :
3997 : 262 : if (lensub == 0)
3998 : : {
3999 : 24 : if (back)
4000 : 12 : index = len + 1;
4001 : : else
4002 : : index = 1;
4003 : 24 : goto done;
4004 : : }
4005 : :
4006 : 238 : if (!back)
4007 : : {
4008 : 126 : last = len + 1 - lensub;
4009 : 126 : start = 0;
4010 : 126 : delta = 1;
4011 : : }
4012 : : else
4013 : : {
4014 : 112 : last = -1;
4015 : 112 : start = len - lensub;
4016 : 112 : delta = -1;
4017 : : }
4018 : :
4019 : 1210 : for (; start != last; start += delta)
4020 : : {
4021 : 2060 : for (i = 0; i < lensub; i++)
4022 : : {
4023 : 1852 : if (x->value.character.string[start + i]
4024 : 1852 : != y->value.character.string[i])
4025 : : break;
4026 : : }
4027 : 1180 : if (i == lensub)
4028 : : {
4029 : 208 : index = start + 1;
4030 : 208 : goto done;
4031 : : }
4032 : : }
4033 : :
4034 : 30 : done:
4035 : 262 : mpz_set_si (result->value.integer, index);
4036 : 262 : return range_check (result, "INDEX");
4037 : : }
4038 : :
4039 : : static gfc_expr *
4040 : 7325 : simplify_intconv (gfc_expr *e, int kind, const char *name)
4041 : : {
4042 : 7325 : gfc_expr *result = NULL;
4043 : 7325 : int tmp1, tmp2;
4044 : :
4045 : : /* Convert BOZ to integer, and return without range checking. */
4046 : 7325 : if (e->ts.type == BT_BOZ)
4047 : : {
4048 : 1589 : if (!gfc_boz2int (e, kind))
4049 : : return NULL;
4050 : 1589 : result = gfc_copy_expr (e);
4051 : 1589 : return result;
4052 : : }
4053 : :
4054 : 5736 : if (e->expr_type != EXPR_CONSTANT)
4055 : : return NULL;
4056 : :
4057 : : /* For explicit conversion, turn off -Wconversion and -Wconversion-extra
4058 : : warnings. */
4059 : 1362 : tmp1 = warn_conversion;
4060 : 1362 : tmp2 = warn_conversion_extra;
4061 : 1362 : warn_conversion = warn_conversion_extra = 0;
4062 : :
4063 : 1362 : result = gfc_convert_constant (e, BT_INTEGER, kind);
4064 : :
4065 : 1362 : warn_conversion = tmp1;
4066 : 1362 : warn_conversion_extra = tmp2;
4067 : :
4068 : 1362 : if (result == &gfc_bad_expr)
4069 : : return &gfc_bad_expr;
4070 : :
4071 : 1362 : return range_check (result, name);
4072 : : }
4073 : :
4074 : :
4075 : : gfc_expr *
4076 : 7222 : gfc_simplify_int (gfc_expr *e, gfc_expr *k)
4077 : : {
4078 : 7222 : int kind;
4079 : :
4080 : 7222 : kind = get_kind (BT_INTEGER, k, "INT", gfc_default_integer_kind);
4081 : 7222 : if (kind == -1)
4082 : : return &gfc_bad_expr;
4083 : :
4084 : 7222 : return simplify_intconv (e, kind, "INT");
4085 : : }
4086 : :
4087 : : gfc_expr *
4088 : 58 : gfc_simplify_int2 (gfc_expr *e)
4089 : : {
4090 : 58 : return simplify_intconv (e, 2, "INT2");
4091 : : }
4092 : :
4093 : :
4094 : : gfc_expr *
4095 : 45 : gfc_simplify_int8 (gfc_expr *e)
4096 : : {
4097 : 45 : return simplify_intconv (e, 8, "INT8");
4098 : : }
4099 : :
4100 : :
4101 : : gfc_expr *
4102 : 0 : gfc_simplify_long (gfc_expr *e)
4103 : : {
4104 : 0 : return simplify_intconv (e, 4, "LONG");
4105 : : }
4106 : :
4107 : :
4108 : : gfc_expr *
4109 : 1648 : gfc_simplify_ifix (gfc_expr *e)
4110 : : {
4111 : 1648 : gfc_expr *rtrunc, *result;
4112 : :
4113 : 1648 : if (e->expr_type != EXPR_CONSTANT)
4114 : : return NULL;
4115 : :
4116 : 127 : rtrunc = gfc_copy_expr (e);
4117 : 127 : mpfr_trunc (rtrunc->value.real, e->value.real);
4118 : :
4119 : 127 : result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
4120 : : &e->where);
4121 : 127 : gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real, &e->where);
4122 : :
4123 : 127 : gfc_free_expr (rtrunc);
4124 : :
4125 : 127 : return range_check (result, "IFIX");
4126 : : }
4127 : :
4128 : :
4129 : : gfc_expr *
4130 : 696 : gfc_simplify_idint (gfc_expr *e)
4131 : : {
4132 : 696 : gfc_expr *rtrunc, *result;
4133 : :
4134 : 696 : if (e->expr_type != EXPR_CONSTANT)
4135 : : return NULL;
4136 : :
4137 : 50 : rtrunc = gfc_copy_expr (e);
4138 : 50 : mpfr_trunc (rtrunc->value.real, e->value.real);
4139 : :
4140 : 50 : result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
4141 : : &e->where);
4142 : 50 : gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real, &e->where);
4143 : :
4144 : 50 : gfc_free_expr (rtrunc);
4145 : :
4146 : 50 : return range_check (result, "IDINT");
4147 : : }
4148 : :
4149 : : gfc_expr *
4150 : 459 : gfc_simplify_uint (gfc_expr *e, gfc_expr *k)
4151 : : {
4152 : 459 : gfc_expr *result = NULL;
4153 : 459 : int kind;
4154 : :
4155 : : /* KIND is always an integer. */
4156 : :
4157 : 459 : kind = get_kind (BT_INTEGER, k, "INT", gfc_default_integer_kind);
4158 : 459 : if (kind == -1)
4159 : : return &gfc_bad_expr;
4160 : :
4161 : : /* Convert BOZ to integer, and return without range checking. */
4162 : 459 : if (e->ts.type == BT_BOZ)
4163 : : {
4164 : 6 : if (!gfc_boz2uint (e, kind))
4165 : : return NULL;
4166 : 6 : result = gfc_copy_expr (e);
4167 : 6 : return result;
4168 : : }
4169 : :
4170 : 453 : if (e->expr_type != EXPR_CONSTANT)
4171 : : return NULL;
4172 : :
4173 : 165 : result = gfc_convert_constant (e, BT_UNSIGNED, kind);
4174 : :
4175 : 165 : if (result == &gfc_bad_expr)
4176 : : return &gfc_bad_expr;
4177 : :
4178 : 165 : return range_check (result, "UINT");
4179 : : }
4180 : :
4181 : :
4182 : : gfc_expr *
4183 : 4358 : gfc_simplify_ior (gfc_expr *x, gfc_expr *y)
4184 : : {
4185 : 4358 : gfc_expr *result;
4186 : 4358 : bt type;
4187 : :
4188 : 4358 : if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
4189 : : return NULL;
4190 : :
4191 : 3055 : type = x->ts.type == BT_UNSIGNED ? BT_UNSIGNED : BT_INTEGER;
4192 : 3055 : result = gfc_get_constant_expr (type, x->ts.kind, &x->where);
4193 : 3055 : mpz_ior (result->value.integer, x->value.integer, y->value.integer);
4194 : :
4195 : 3055 : return range_check (result, "IOR");
4196 : : }
4197 : :
4198 : :
4199 : : static gfc_expr *
4200 : 96 : do_bit_xor (gfc_expr *result, gfc_expr *e)
4201 : : {
4202 : 96 : if (flag_unsigned)
4203 : : {
4204 : 72 : gcc_assert ((e->ts.type == BT_INTEGER || e->ts.type == BT_UNSIGNED)
4205 : : && e->expr_type == EXPR_CONSTANT);
4206 : 72 : gcc_assert ((result->ts.type == BT_INTEGER
4207 : : || result->ts.type == BT_UNSIGNED)
4208 : : && result->expr_type == EXPR_CONSTANT);
4209 : : }
4210 : : else
4211 : : {
4212 : 24 : gcc_assert (e->ts.type == BT_INTEGER && e->expr_type == EXPR_CONSTANT);
4213 : 24 : gcc_assert (result->ts.type == BT_INTEGER
4214 : : && result->expr_type == EXPR_CONSTANT);
4215 : : }
4216 : :
4217 : 96 : mpz_xor (result->value.integer, result->value.integer, e->value.integer);
4218 : 96 : return result;
4219 : : }
4220 : :
4221 : :
4222 : : gfc_expr *
4223 : 259 : gfc_simplify_iparity (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
4224 : : {
4225 : 259 : return simplify_transformation (array, dim, mask, 0, do_bit_xor);
4226 : : }
4227 : :
4228 : :
4229 : : gfc_expr *
4230 : 46 : gfc_simplify_is_iostat_end (gfc_expr *x)
4231 : : {
4232 : 46 : if (x->expr_type != EXPR_CONSTANT)
4233 : : return NULL;
4234 : :
4235 : 28 : return gfc_get_logical_expr (gfc_default_logical_kind, &x->where,
4236 : 28 : mpz_cmp_si (x->value.integer,
4237 : 28 : LIBERROR_END) == 0);
4238 : : }
4239 : :
4240 : :
4241 : : gfc_expr *
4242 : 70 : gfc_simplify_is_iostat_eor (gfc_expr *x)
4243 : : {
4244 : 70 : if (x->expr_type != EXPR_CONSTANT)
4245 : : return NULL;
4246 : :
4247 : 16 : return gfc_get_logical_expr (gfc_default_logical_kind, &x->where,
4248 : 16 : mpz_cmp_si (x->value.integer,
4249 : 16 : LIBERROR_EOR) == 0);
4250 : : }
4251 : :
4252 : :
4253 : : gfc_expr *
4254 : 1568 : gfc_simplify_isnan (gfc_expr *x)
4255 : : {
4256 : 1568 : if (x->expr_type != EXPR_CONSTANT)
4257 : : return NULL;
4258 : :
4259 : 194 : return gfc_get_logical_expr (gfc_default_logical_kind, &x->where,
4260 : 194 : mpfr_nan_p (x->value.real));
4261 : : }
4262 : :
4263 : :
4264 : : /* Performs a shift on its first argument. Depending on the last
4265 : : argument, the shift can be arithmetic, i.e. with filling from the
4266 : : left like in the SHIFTA intrinsic. */
4267 : : static gfc_expr *
4268 : 9696 : simplify_shift (gfc_expr *e, gfc_expr *s, const char *name,
4269 : : bool arithmetic, int direction)
4270 : : {
4271 : 9696 : gfc_expr *result;
4272 : 9696 : int ashift, *bits, i, k, bitsize, shift;
4273 : :
4274 : 9696 : if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
4275 : : return NULL;
4276 : :
4277 : 7729 : gfc_extract_int (s, &shift);
4278 : :
4279 : 7729 : k = gfc_validate_kind (e->ts.type, e->ts.kind, false);
4280 : 7729 : if (e->ts.type == BT_INTEGER)
4281 : 7627 : bitsize = gfc_integer_kinds[k].bit_size;
4282 : : else
4283 : 102 : bitsize = gfc_unsigned_kinds[k].bit_size;
4284 : :
4285 : 7729 : result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
4286 : :
4287 : 7729 : if (shift == 0)
4288 : : {
4289 : 1194 : mpz_set (result->value.integer, e->value.integer);
4290 : 1194 : return result;
4291 : : }
4292 : :
4293 : 6535 : if (direction > 0 && shift < 0)
4294 : : {
4295 : : /* Left shift, as in SHIFTL. */
4296 : 0 : gfc_error ("Second argument of %s is negative at %L", name, &e->where);
4297 : 0 : return &gfc_bad_expr;
4298 : : }
4299 : 6535 : else if (direction < 0)
4300 : : {
4301 : : /* Right shift, as in SHIFTR or SHIFTA. */
4302 : 2832 : if (shift < 0)
4303 : : {
4304 : 0 : gfc_error ("Second argument of %s is negative at %L",
4305 : : name, &e->where);
4306 : 0 : return &gfc_bad_expr;
4307 : : }
4308 : :
4309 : 2832 : shift = -shift;
4310 : : }
4311 : :
4312 : 6535 : ashift = (shift >= 0 ? shift : -shift);
4313 : :
4314 : 6535 : if (ashift > bitsize)
4315 : : {
4316 : 0 : gfc_error ("Magnitude of second argument of %s exceeds bit size "
4317 : : "at %L", name, &e->where);
4318 : 0 : return &gfc_bad_expr;
4319 : : }
4320 : :
4321 : 6535 : bits = XCNEWVEC (int, bitsize);
4322 : :
4323 : 325358 : for (i = 0; i < bitsize; i++)
4324 : 312288 : bits[i] = mpz_tstbit (e->value.integer, i);
4325 : :
4326 : 6535 : if (shift > 0)
4327 : : {
4328 : : /* Left shift. */
4329 : 86026 : for (i = 0; i < shift; i++)
4330 : 82467 : mpz_clrbit (result->value.integer, i);
4331 : :
4332 : 85300 : for (i = 0; i < bitsize - shift; i++)
4333 : : {
4334 : 81741 : if (bits[i] == 0)
4335 : 53126 : mpz_clrbit (result->value.integer, i + shift);
4336 : : else
4337 : 28615 : mpz_setbit (result->value.integer, i + shift);
4338 : : }
4339 : : }
4340 : : else
4341 : : {
4342 : : /* Right shift. */
4343 : 2976 : if (arithmetic && bits[bitsize - 1])
4344 : 504 : for (i = bitsize - 1; i >= bitsize - ashift; i--)
4345 : 438 : mpz_setbit (result->value.integer, i);
4346 : : else
4347 : 75186 : for (i = bitsize - 1; i >= bitsize - ashift; i--)
4348 : 72276 : mpz_clrbit (result->value.integer, i);
4349 : :
4350 : 78342 : for (i = bitsize - 1; i >= ashift; i--)
4351 : : {
4352 : 75366 : if (bits[i] == 0)
4353 : 46920 : mpz_clrbit (result->value.integer, i - ashift);
4354 : : else
4355 : 28446 : mpz_setbit (result->value.integer, i - ashift);
4356 : : }
4357 : : }
4358 : :
4359 : 6535 : if (result->ts.type == BT_INTEGER)
4360 : 6433 : gfc_convert_mpz_to_signed (result->value.integer, bitsize);
4361 : : else
4362 : 102 : gfc_reduce_unsigned(result);
4363 : :
4364 : 6535 : free (bits);
4365 : :
4366 : 6535 : return result;
4367 : : }
4368 : :
4369 : :
4370 : : gfc_expr *
4371 : 2103 : gfc_simplify_ishft (gfc_expr *e, gfc_expr *s)
4372 : : {
4373 : 2103 : return simplify_shift (e, s, "ISHFT", false, 0);
4374 : : }
4375 : :
4376 : :
4377 : : gfc_expr *
4378 : 192 : gfc_simplify_lshift (gfc_expr *e, gfc_expr *s)
4379 : : {
4380 : 192 : return simplify_shift (e, s, "LSHIFT", false, 1);
4381 : : }
4382 : :
4383 : :
4384 : : gfc_expr *
4385 : 66 : gfc_simplify_rshift (gfc_expr *e, gfc_expr *s)
4386 : : {
4387 : 66 : return simplify_shift (e, s, "RSHIFT", true, -1);
4388 : : }
4389 : :
4390 : :
4391 : : gfc_expr *
4392 : 438 : gfc_simplify_shifta (gfc_expr *e, gfc_expr *s)
4393 : : {
4394 : 438 : return simplify_shift (e, s, "SHIFTA", true, -1);
4395 : : }
4396 : :
4397 : :
4398 : : gfc_expr *
4399 : 3621 : gfc_simplify_shiftl (gfc_expr *e, gfc_expr *s)
4400 : : {
4401 : 3621 : return simplify_shift (e, s, "SHIFTL", false, 1);
4402 : : }
4403 : :
4404 : :
4405 : : gfc_expr *
4406 : 3276 : gfc_simplify_shiftr (gfc_expr *e, gfc_expr *s)
4407 : : {
4408 : 3276 : return simplify_shift (e, s, "SHIFTR", false, -1);
4409 : : }
4410 : :
4411 : :
4412 : : gfc_expr *
4413 : 1929 : gfc_simplify_ishftc (gfc_expr *e, gfc_expr *s, gfc_expr *sz)
4414 : : {
4415 : 1929 : gfc_expr *result;
4416 : 1929 : int shift, ashift, isize, ssize, delta, k;
4417 : 1929 : int i, *bits;
4418 : :
4419 : 1929 : if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
4420 : : return NULL;
4421 : :
4422 : 411 : gfc_extract_int (s, &shift);
4423 : :
4424 : 411 : k = gfc_validate_kind (e->ts.type, e->ts.kind, false);
4425 : 411 : isize = gfc_integer_kinds[k].bit_size;
4426 : :
4427 : 411 : if (sz != NULL)
4428 : : {
4429 : 213 : if (sz->expr_type != EXPR_CONSTANT)
4430 : : return NULL;
4431 : :
4432 : 213 : gfc_extract_int (sz, &ssize);
4433 : :
4434 : 213 : if (ssize > isize || ssize <= 0)
4435 : : return &gfc_bad_expr;
4436 : : }
4437 : : else
4438 : 198 : ssize = isize;
4439 : :
4440 : 411 : if (shift >= 0)
4441 : : ashift = shift;
4442 : : else
4443 : : ashift = -shift;
4444 : :
4445 : 411 : if (ashift > ssize)
4446 : : {
4447 : 11 : if (sz == NULL)
4448 : 4 : gfc_error ("Magnitude of second argument of ISHFTC exceeds "
4449 : : "BIT_SIZE of first argument at %C");
4450 : : else
4451 : 7 : gfc_error ("Absolute value of SHIFT shall be less than or equal "
4452 : : "to SIZE at %C");
4453 : 11 : return &gfc_bad_expr;
4454 : : }
4455 : :
4456 : 400 : result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
4457 : :
4458 : 400 : mpz_set (result->value.integer, e->value.integer);
4459 : :
4460 : 400 : if (shift == 0)
4461 : : return result;
4462 : :
4463 : 364 : if (result->ts.type == BT_INTEGER)
4464 : 352 : gfc_convert_mpz_to_unsigned (result->value.integer, isize);
4465 : :
4466 : 364 : bits = XCNEWVEC (int, ssize);
4467 : :
4468 : 6877 : for (i = 0; i < ssize; i++)
4469 : 6149 : bits[i] = mpz_tstbit (e->value.integer, i);
4470 : :
4471 : 364 : delta = ssize - ashift;
4472 : :
4473 : 364 : if (shift > 0)
4474 : : {
4475 : 3975 : for (i = 0; i < delta; i++)
4476 : : {
4477 : 3707 : if (bits[i] == 0)
4478 : 2226 : mpz_clrbit (result->value.integer, i + shift);
4479 : : else
4480 : 1481 : mpz_setbit (result->value.integer, i + shift);
4481 : : }
4482 : :
4483 : 1030 : for (i = delta; i < ssize; i++)
4484 : : {
4485 : 762 : if (bits[i] == 0)
4486 : 612 : mpz_clrbit (result->value.integer, i - delta);
4487 : : else
4488 : 150 : mpz_setbit (result->value.integer, i - delta);
4489 : : }
4490 : : }
4491 : : else
4492 : : {
4493 : 288 : for (i = 0; i < ashift; i++)
4494 : : {
4495 : 192 : if (bits[i] == 0)
4496 : 90 : mpz_clrbit (result->value.integer, i + delta);
4497 : : else
4498 : 102 : mpz_setbit (result->value.integer, i + delta);
4499 : : }
4500 : :
4501 : 1584 : for (i = ashift; i < ssize; i++)
4502 : : {
4503 : 1488 : if (bits[i] == 0)
4504 : 624 : mpz_clrbit (result->value.integer, i + shift);
4505 : : else
4506 : 864 : mpz_setbit (result->value.integer, i + shift);
4507 : : }
4508 : : }
4509 : :
4510 : 364 : if (result->ts.type == BT_INTEGER)
4511 : 352 : gfc_convert_mpz_to_signed (result->value.integer, isize);
4512 : :
4513 : 364 : free (bits);
4514 : 364 : return result;
4515 : : }
4516 : :
4517 : :
4518 : : gfc_expr *
4519 : 4365 : gfc_simplify_kind (gfc_expr *e)
4520 : : {
4521 : 4365 : return gfc_get_int_expr (gfc_default_integer_kind, NULL, e->ts.kind);
4522 : : }
4523 : :
4524 : :
4525 : : static gfc_expr *
4526 : 12818 : simplify_bound_dim (gfc_expr *array, gfc_expr *kind, int d, int upper,
4527 : : gfc_array_spec *as, gfc_ref *ref, bool coarray)
4528 : : {
4529 : 12818 : gfc_expr *l, *u, *result;
4530 : 12818 : int k;
4531 : :
4532 : 21557 : k = get_kind (BT_INTEGER, kind, upper ? "UBOUND" : "LBOUND",
4533 : : gfc_default_integer_kind);
4534 : 12818 : if (k == -1)
4535 : : return &gfc_bad_expr;
4536 : :
4537 : 12818 : result = gfc_get_constant_expr (BT_INTEGER, k, &array->where);
4538 : :
4539 : : /* For non-variables, LBOUND(expr, DIM=n) = 1 and
4540 : : UBOUND(expr, DIM=n) = SIZE(expr, DIM=n). */
4541 : 12818 : if (!coarray && array->expr_type != EXPR_VARIABLE)
4542 : : {
4543 : 1414 : if (upper)
4544 : : {
4545 : 782 : gfc_expr* dim = result;
4546 : 782 : mpz_set_si (dim->value.integer, d);
4547 : :
4548 : 782 : result = simplify_size (array, dim, k);
4549 : 782 : gfc_free_expr (dim);
4550 : 782 : if (!result)
4551 : 375 : goto returnNull;
4552 : : }
4553 : : else
4554 : 632 : mpz_set_si (result->value.integer, 1);
4555 : :
4556 : 1039 : goto done;
4557 : : }
4558 : :
4559 : : /* Otherwise, we have a variable expression. */
4560 : 11404 : gcc_assert (array->expr_type == EXPR_VARIABLE);
4561 : 11404 : gcc_assert (as);
4562 : :
4563 : 11404 : if (!gfc_resolve_array_spec (as, 0))
4564 : : return NULL;
4565 : :
4566 : : /* The last dimension of an assumed-size array is special. */
4567 : 11401 : if ((!coarray && d == as->rank && as->type == AS_ASSUMED_SIZE && !upper)
4568 : 1239 : || (coarray && d == as->rank + as->corank
4569 : 452 : && (!upper || flag_coarray == GFC_FCOARRAY_SINGLE)))
4570 : : {
4571 : 604 : if (as->lower[d-1] && as->lower[d-1]->expr_type == EXPR_CONSTANT)
4572 : : {
4573 : 379 : gfc_free_expr (result);
4574 : 379 : return gfc_copy_expr (as->lower[d-1]);
4575 : : }
4576 : :
4577 : 225 : goto returnNull;
4578 : : }
4579 : :
4580 : : /* Then, we need to know the extent of the given dimension. */
4581 : 9966 : if (coarray || (ref->u.ar.type == AR_FULL && !ref->next))
4582 : : {
4583 : 10294 : gfc_expr *declared_bound;
4584 : 10294 : int empty_bound;
4585 : 10294 : bool constant_lbound, constant_ubound;
4586 : :
4587 : 10294 : l = as->lower[d-1];
4588 : 10294 : u = as->upper[d-1];
4589 : :
4590 : 10294 : gcc_assert (l != NULL);
4591 : :
4592 : 10294 : constant_lbound = l->expr_type == EXPR_CONSTANT;
4593 : 10294 : constant_ubound = u && u->expr_type == EXPR_CONSTANT;
4594 : :
4595 : 10294 : empty_bound = upper ? 0 : 1;
4596 : 10294 : declared_bound = upper ? u : l;
4597 : :
4598 : 10294 : if ((!upper && !constant_lbound)
4599 : 9420 : || (upper && !constant_ubound))
4600 : 2176 : goto returnNull;
4601 : :
4602 : 8118 : if (!coarray)
4603 : : {
4604 : : /* For {L,U}BOUND, the value depends on whether the array
4605 : : is empty. We can nevertheless simplify if the declared bound
4606 : : has the same value as that of an empty array, in which case
4607 : : the result isn't dependent on the array emptiness. */
4608 : 7527 : if (mpz_cmp_si (declared_bound->value.integer, empty_bound) == 0)
4609 : 3483 : mpz_set_si (result->value.integer, empty_bound);
4610 : 4044 : else if (!constant_lbound || !constant_ubound)
4611 : : /* Array emptiness can't be determined, we can't simplify. */
4612 : 1815 : goto returnNull;
4613 : 2229 : else if (mpz_cmp (l->value.integer, u->value.integer) > 0)
4614 : 97 : mpz_set_si (result->value.integer, empty_bound);
4615 : : else
4616 : 2132 : mpz_set (result->value.integer, declared_bound->value.integer);
4617 : : }
4618 : : else
4619 : 591 : mpz_set (result->value.integer, declared_bound->value.integer);
4620 : : }
4621 : : else
4622 : : {
4623 : 503 : if (upper)
4624 : : {
4625 : : int d2 = 0, cnt = 0;
4626 : 523 : for (int idx = 0; idx < ref->u.ar.dimen; ++idx)
4627 : : {
4628 : 523 : if (ref->u.ar.dimen_type[idx] == DIMEN_ELEMENT)
4629 : 120 : d2++;
4630 : 403 : else if (cnt < d - 1)
4631 : 102 : cnt++;
4632 : : else
4633 : : break;
4634 : : }
4635 : 301 : if (!gfc_ref_dimen_size (&ref->u.ar, d2 + d - 1, &result->value.integer, NULL))
4636 : 73 : goto returnNull;
4637 : : }
4638 : : else
4639 : 202 : mpz_set_si (result->value.integer, (long int) 1);
4640 : : }
4641 : :
4642 : 7772 : done:
4643 : 7772 : return range_check (result, upper ? "UBOUND" : "LBOUND");
4644 : :
4645 : 4664 : returnNull:
4646 : 4664 : gfc_free_expr (result);
4647 : 4664 : return NULL;
4648 : : }
4649 : :
4650 : :
4651 : : static gfc_expr *
4652 : 34372 : simplify_bound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind, int upper)
4653 : : {
4654 : 34372 : gfc_ref *ref;
4655 : 34372 : gfc_array_spec *as;
4656 : 34372 : ar_type type = AR_UNKNOWN;
4657 : 34372 : int d;
4658 : :
4659 : 34372 : if (array->ts.type == BT_CLASS)
4660 : : return NULL;
4661 : :
4662 : 32998 : if (array->expr_type != EXPR_VARIABLE)
4663 : : {
4664 : 1242 : as = NULL;
4665 : 1242 : ref = NULL;
4666 : 1242 : goto done;
4667 : : }
4668 : :
4669 : : /* Do not attempt to resolve if error has already been issued. */
4670 : 31756 : if (array->symtree->n.sym->error)
4671 : : return NULL;
4672 : :
4673 : : /* Follow any component references. */
4674 : 31755 : as = array->symtree->n.sym->as;
4675 : 32841 : for (ref = array->ref; ref; ref = ref->next)
4676 : : {
4677 : 32841 : switch (ref->type)
4678 : : {
4679 : 31889 : case REF_ARRAY:
4680 : 31889 : type = ref->u.ar.type;
4681 : 31889 : switch (ref->u.ar.type)
4682 : : {
4683 : 134 : case AR_ELEMENT:
4684 : 134 : as = NULL;
4685 : 134 : continue;
4686 : :
4687 : 30892 : case AR_FULL:
4688 : : /* We're done because 'as' has already been set in the
4689 : : previous iteration. */
4690 : 30892 : goto done;
4691 : :
4692 : : case AR_UNKNOWN:
4693 : : return NULL;
4694 : :
4695 : 863 : case AR_SECTION:
4696 : 863 : as = ref->u.ar.as;
4697 : 863 : goto done;
4698 : : }
4699 : :
4700 : 0 : gcc_unreachable ();
4701 : :
4702 : 952 : case REF_COMPONENT:
4703 : 952 : as = ref->u.c.component->as;
4704 : 952 : continue;
4705 : :
4706 : 0 : case REF_SUBSTRING:
4707 : 0 : case REF_INQUIRY:
4708 : 0 : continue;
4709 : : }
4710 : : }
4711 : :
4712 : 0 : gcc_unreachable ();
4713 : :
4714 : 32997 : done:
4715 : :
4716 : 32997 : if (as && (as->type == AS_DEFERRED || as->type == AS_ASSUMED_RANK
4717 : 11198 : || (as->type == AS_ASSUMED_SHAPE && upper)))
4718 : : return NULL;
4719 : :
4720 : : /* 'array' shall not be an unallocated allocatable variable or a pointer that
4721 : : is not associated. */
4722 : 10312 : if (array->expr_type == EXPR_VARIABLE
4723 : 10312 : && (gfc_expr_attr (array).allocatable || gfc_expr_attr (array).pointer))
4724 : 6 : return NULL;
4725 : :
4726 : 10306 : gcc_assert (!as
4727 : : || (as->type != AS_DEFERRED
4728 : : && array->expr_type == EXPR_VARIABLE
4729 : : && !gfc_expr_attr (array).allocatable
4730 : : && !gfc_expr_attr (array).pointer));
4731 : :
4732 : 10306 : if (dim == NULL)
4733 : : {
4734 : : /* Multi-dimensional bounds. */
4735 : 1549 : gfc_expr *bounds[GFC_MAX_DIMENSIONS];
4736 : 1549 : gfc_expr *e;
4737 : 1549 : int k;
4738 : :
4739 : : /* UBOUND(ARRAY) is not valid for an assumed-size array. */
4740 : 1549 : if (upper && type == AR_FULL && as && as->type == AS_ASSUMED_SIZE)
4741 : : {
4742 : : /* An error message will be emitted in
4743 : : check_assumed_size_reference (resolve.cc). */
4744 : : return &gfc_bad_expr;
4745 : : }
4746 : :
4747 : : /* Simplify the bounds for each dimension. */
4748 : 4034 : for (d = 0; d < array->rank; d++)
4749 : : {
4750 : 2820 : bounds[d] = simplify_bound_dim (array, kind, d + 1, upper, as, ref,
4751 : : false);
4752 : 2820 : if (bounds[d] == NULL || bounds[d] == &gfc_bad_expr)
4753 : : {
4754 : : int j;
4755 : :
4756 : 340 : for (j = 0; j < d; j++)
4757 : 6 : gfc_free_expr (bounds[j]);
4758 : :
4759 : 334 : if (gfc_seen_div0)
4760 : : return &gfc_bad_expr;
4761 : : else
4762 : : return bounds[d];
4763 : : }
4764 : : }
4765 : :
4766 : : /* Allocate the result expression. */
4767 : 1897 : k = get_kind (BT_INTEGER, kind, upper ? "UBOUND" : "LBOUND",
4768 : : gfc_default_integer_kind);
4769 : 1214 : if (k == -1)
4770 : : return &gfc_bad_expr;
4771 : :
4772 : 1214 : e = gfc_get_array_expr (BT_INTEGER, k, &array->where);
4773 : :
4774 : : /* The result is a rank 1 array; its size is the rank of the first
4775 : : argument to {L,U}BOUND. */
4776 : 1214 : e->rank = 1;
4777 : 1214 : e->shape = gfc_get_shape (1);
4778 : 1214 : mpz_init_set_ui (e->shape[0], array->rank);
4779 : :
4780 : : /* Create the constructor for this array. */
4781 : 4908 : for (d = 0; d < array->rank; d++)
4782 : 2480 : gfc_constructor_append_expr (&e->value.constructor,
4783 : : bounds[d], &e->where);
4784 : :
4785 : : return e;
4786 : : }
4787 : : else
4788 : : {
4789 : : /* A DIM argument is specified. */
4790 : 8757 : if (dim->expr_type != EXPR_CONSTANT)
4791 : : return NULL;
4792 : :
4793 : 8757 : d = mpz_get_si (dim->value.integer);
4794 : :
4795 : 8757 : if ((d < 1 || d > array->rank)
4796 : 8757 : || (d == array->rank && as && as->type == AS_ASSUMED_SIZE && upper))
4797 : : {
4798 : 0 : gfc_error ("DIM argument at %L is out of bounds", &dim->where);
4799 : 0 : return &gfc_bad_expr;
4800 : : }
4801 : :
4802 : 8340 : if (as && as->type == AS_ASSUMED_RANK)
4803 : : return NULL;
4804 : :
4805 : 8757 : return simplify_bound_dim (array, kind, d, upper, as, ref, false);
4806 : : }
4807 : : }
4808 : :
4809 : :
4810 : : static gfc_expr *
4811 : 1479 : simplify_cobound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind, int upper)
4812 : : {
4813 : 1479 : gfc_ref *ref;
4814 : 1479 : gfc_array_spec *as;
4815 : 1479 : int d;
4816 : :
4817 : 1479 : if (array->expr_type != EXPR_VARIABLE)
4818 : : return NULL;
4819 : :
4820 : : /* Follow any component references. */
4821 : 157 : as = (array->ts.type == BT_CLASS && CLASS_DATA (array))
4822 : 1479 : ? CLASS_DATA (array)->as
4823 : 1323 : : array->symtree->n.sym->as;
4824 : 1695 : for (ref = array->ref; ref; ref = ref->next)
4825 : : {
4826 : 1694 : switch (ref->type)
4827 : : {
4828 : 1478 : case REF_ARRAY:
4829 : 1478 : switch (ref->u.ar.type)
4830 : : {
4831 : 357 : case AR_ELEMENT:
4832 : 357 : if (ref->u.ar.as->corank > 0)
4833 : : {
4834 : 357 : gcc_assert (as == ref->u.ar.as);
4835 : 357 : goto done;
4836 : : }
4837 : 0 : as = NULL;
4838 : 0 : continue;
4839 : :
4840 : 1121 : case AR_FULL:
4841 : : /* We're done because 'as' has already been set in the
4842 : : previous iteration. */
4843 : 1121 : goto done;
4844 : :
4845 : : case AR_UNKNOWN:
4846 : : return NULL;
4847 : :
4848 : 0 : case AR_SECTION:
4849 : 0 : as = ref->u.ar.as;
4850 : 0 : goto done;
4851 : : }
4852 : :
4853 : 0 : gcc_unreachable ();
4854 : :
4855 : 216 : case REF_COMPONENT:
4856 : 216 : as = ref->u.c.component->as;
4857 : 216 : continue;
4858 : :
4859 : 0 : case REF_SUBSTRING:
4860 : 0 : case REF_INQUIRY:
4861 : 0 : continue;
4862 : : }
4863 : : }
4864 : :
4865 : 1 : if (!as)
4866 : 0 : gcc_unreachable ();
4867 : :
4868 : 1 : done:
4869 : :
4870 : 1479 : if (as->cotype == AS_DEFERRED || as->cotype == AS_ASSUMED_SHAPE)
4871 : : return NULL;
4872 : :
4873 : 779 : if (dim == NULL)
4874 : : {
4875 : : /* Multi-dimensional cobounds. */
4876 : : gfc_expr *bounds[GFC_MAX_DIMENSIONS];
4877 : : gfc_expr *e;
4878 : : int k;
4879 : :
4880 : : /* Simplify the cobounds for each dimension. */
4881 : 739 : for (d = 0; d < as->corank; d++)
4882 : : {
4883 : 632 : bounds[d] = simplify_bound_dim (array, kind, d + 1 + as->rank,
4884 : : upper, as, ref, true);
4885 : 632 : if (bounds[d] == NULL || bounds[d] == &gfc_bad_expr)
4886 : : {
4887 : : int j;
4888 : :
4889 : 270 : for (j = 0; j < d; j++)
4890 : 120 : gfc_free_expr (bounds[j]);
4891 : : return bounds[d];
4892 : : }
4893 : : }
4894 : :
4895 : : /* Allocate the result expression. */
4896 : 107 : e = gfc_get_expr ();
4897 : 107 : e->where = array->where;
4898 : 107 : e->expr_type = EXPR_ARRAY;
4899 : 107 : e->ts.type = BT_INTEGER;
4900 : 191 : k = get_kind (BT_INTEGER, kind, upper ? "UCOBOUND" : "LCOBOUND",
4901 : : gfc_default_integer_kind);
4902 : 107 : if (k == -1)
4903 : : {
4904 : 0 : gfc_free_expr (e);
4905 : 0 : return &gfc_bad_expr;
4906 : : }
4907 : 107 : e->ts.kind = k;
4908 : :
4909 : : /* The result is a rank 1 array; its size is the rank of the first
4910 : : argument to {L,U}COBOUND. */
4911 : 107 : e->rank = 1;
4912 : 107 : e->shape = gfc_get_shape (1);
4913 : 107 : mpz_init_set_ui (e->shape[0], as->corank);
4914 : :
4915 : : /* Create the constructor for this array. */
4916 : 576 : for (d = 0; d < as->corank; d++)
4917 : 362 : gfc_constructor_append_expr (&e->value.constructor,
4918 : : bounds[d], &e->where);
4919 : : return e;
4920 : : }
4921 : : else
4922 : : {
4923 : : /* A DIM argument is specified. */
4924 : 522 : if (dim->expr_type != EXPR_CONSTANT)
4925 : : return NULL;
4926 : :
4927 : 382 : d = mpz_get_si (dim->value.integer);
4928 : :
4929 : 382 : if (d < 1 || d > as->corank)
4930 : : {
4931 : 0 : gfc_error ("DIM argument at %L is out of bounds", &dim->where);
4932 : 0 : return &gfc_bad_expr;
4933 : : }
4934 : :
4935 : 382 : return simplify_bound_dim (array, kind, d+as->rank, upper, as, ref, true);
4936 : : }
4937 : : }
4938 : :
4939 : :
4940 : : gfc_expr *
4941 : 19466 : gfc_simplify_lbound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
4942 : : {
4943 : 19466 : return simplify_bound (array, dim, kind, 0);
4944 : : }
4945 : :
4946 : :
4947 : : gfc_expr *
4948 : 509 : gfc_simplify_lcobound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
4949 : : {
4950 : 509 : return simplify_cobound (array, dim, kind, 0);
4951 : : }
4952 : :
4953 : : gfc_expr *
4954 : 1068 : gfc_simplify_leadz (gfc_expr *e)
4955 : : {
4956 : 1068 : unsigned long lz, bs;
4957 : 1068 : int i;
4958 : :
4959 : 1068 : if (e->expr_type != EXPR_CONSTANT)
4960 : : return NULL;
4961 : :
4962 : 258 : i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
4963 : 258 : bs = gfc_integer_kinds[i].bit_size;
4964 : 258 : if (mpz_cmp_si (e->value.integer, 0) == 0)
4965 : : lz = bs;
4966 : 222 : else if (mpz_cmp_si (e->value.integer, 0) < 0)
4967 : : lz = 0;
4968 : : else
4969 : 132 : lz = bs - mpz_sizeinbase (e->value.integer, 2);
4970 : :
4971 : 258 : return gfc_get_int_expr (gfc_default_integer_kind, &e->where, lz);
4972 : : }
4973 : :
4974 : :
4975 : : /* Check for constant length of a substring. */
4976 : :
4977 : : static bool
4978 : 16572 : substring_has_constant_len (gfc_expr *e)
4979 : : {
4980 : 16572 : gfc_ref *ref;
4981 : 16572 : HOST_WIDE_INT istart, iend, length;
4982 : 16572 : bool equal_length = false;
4983 : :
4984 : 16572 : if (e->ts.type != BT_CHARACTER)
4985 : : return false;
4986 : :
4987 : 23613 : for (ref = e->ref; ref; ref = ref->next)
4988 : 7564 : if (ref->type != REF_COMPONENT && ref->type != REF_ARRAY)
4989 : : break;
4990 : :
4991 : 16572 : if (!ref
4992 : 523 : || ref->type != REF_SUBSTRING
4993 : 523 : || !ref->u.ss.start
4994 : 523 : || ref->u.ss.start->expr_type != EXPR_CONSTANT
4995 : 208 : || !ref->u.ss.end
4996 : 208 : || ref->u.ss.end->expr_type != EXPR_CONSTANT)
4997 : : return false;
4998 : :
4999 : : /* Basic checks on substring starting and ending indices. */
5000 : 207 : if (!gfc_resolve_substring (ref, &equal_length))
5001 : : return false;
5002 : :
5003 : 207 : istart = gfc_mpz_get_hwi (ref->u.ss.start->value.integer);
5004 : 207 : iend = gfc_mpz_get_hwi (ref->u.ss.end->value.integer);
5005 : :
5006 : 207 : if (istart <= iend)
5007 : 199 : length = iend - istart + 1;
5008 : : else
5009 : : length = 0;
5010 : :
5011 : : /* Fix substring length. */
5012 : 207 : e->value.character.length = length;
5013 : :
5014 : 207 : return true;
5015 : : }
5016 : :
5017 : :
5018 : : gfc_expr *
5019 : 17079 : gfc_simplify_len (gfc_expr *e, gfc_expr *kind)
5020 : : {
5021 : 17079 : gfc_expr *result;
5022 : 17079 : int k = get_kind (BT_INTEGER, kind, "LEN", gfc_default_integer_kind);
5023 : :
5024 : 17079 : if (k == -1)
5025 : : return &gfc_bad_expr;
5026 : :
5027 : 17079 : if (e->expr_type == EXPR_CONSTANT
5028 : 17079 : || substring_has_constant_len (e))
5029 : : {
5030 : 714 : result = gfc_get_constant_expr (BT_INTEGER, k, &e->where);
5031 : 714 : mpz_set_si (result->value.integer, e->value.character.length);
5032 : 714 : return range_check (result, "LEN");
5033 : : }
5034 : 16365 : else if (e->ts.u.cl != NULL && e->ts.u.cl->length != NULL
5035 : 5369 : && e->ts.u.cl->length->expr_type == EXPR_CONSTANT
5036 : 2837 : && e->ts.u.cl->length->ts.type == BT_INTEGER)
5037 : : {
5038 : 2837 : result = gfc_get_constant_expr (BT_INTEGER, k, &e->where);
5039 : 2837 : mpz_set (result->value.integer, e->ts.u.cl->length->value.integer);
5040 : 2837 : return range_check (result, "LEN");
5041 : : }
5042 : 13528 : else if (e->expr_type == EXPR_VARIABLE && e->ts.type == BT_CHARACTER
5043 : 11772 : && e->symtree->n.sym)
5044 : : {
5045 : 11772 : if (e->symtree->n.sym->ts.type != BT_DERIVED
5046 : 11360 : && e->symtree->n.sym->assoc && e->symtree->n.sym->assoc->target
5047 : 965 : && e->symtree->n.sym->assoc->target->ts.type == BT_DERIVED
5048 : 367 : && e->symtree->n.sym->assoc->target->symtree->n.sym
5049 : 367 : && UNLIMITED_POLY (e->symtree->n.sym->assoc->target->symtree->n.sym))
5050 : : /* The expression in assoc->target points to a ref to the _data
5051 : : component of the unlimited polymorphic entity. To get the _len
5052 : : component the last _data ref needs to be stripped and a ref to the
5053 : : _len component added. */
5054 : 367 : return gfc_get_len_component (e->symtree->n.sym->assoc->target, k);
5055 : 11405 : else if (e->symtree->n.sym->ts.type == BT_DERIVED
5056 : 412 : && e->ref && e->ref->type == REF_COMPONENT
5057 : 412 : && e->ref->u.c.component->attr.pdt_string
5058 : 36 : && e->ref->u.c.component->ts.type == BT_CHARACTER
5059 : 36 : && e->ref->u.c.component->ts.u.cl->length)
5060 : : {
5061 : 36 : if (gfc_init_expr_flag)
5062 : : {
5063 : 6 : gfc_expr* tmp;
5064 : 12 : tmp = gfc_pdt_find_component_copy_initializer (e->symtree->n.sym,
5065 : : e->ref->u.c
5066 : : .component->ts.u.cl
5067 : 6 : ->length->symtree
5068 : : ->name);
5069 : 6 : if (tmp)
5070 : : return tmp;
5071 : : }
5072 : : else
5073 : : {
5074 : 30 : gfc_expr *len_expr = gfc_copy_expr (e);
5075 : 30 : gfc_free_ref_list (len_expr->ref);
5076 : 30 : len_expr->ref = NULL;
5077 : 30 : gfc_find_component (len_expr->symtree->n.sym->ts.u.derived, e->ref
5078 : 30 : ->u.c.component->ts.u.cl->length->symtree
5079 : : ->name,
5080 : : false, true, &len_expr->ref);
5081 : 30 : len_expr->ts = len_expr->ref->u.c.component->ts;
5082 : 30 : return len_expr;
5083 : : }
5084 : : }
5085 : : }
5086 : : return NULL;
5087 : : }
5088 : :
5089 : :
5090 : : gfc_expr *
5091 : 4161 : gfc_simplify_len_trim (gfc_expr *e, gfc_expr *kind)
5092 : : {
5093 : 4161 : gfc_expr *result;
5094 : 4161 : size_t count, len, i;
5095 : 4161 : int k = get_kind (BT_INTEGER, kind, "LEN_TRIM", gfc_default_integer_kind);
5096 : :
5097 : 4161 : if (k == -1)
5098 : : return &gfc_bad_expr;
5099 : :
5100 : : /* If the expression is either an array element or section, an array
5101 : : parameter must be built so that the reference can be applied. Constant
5102 : : references should have already been simplified away. All other cases
5103 : : can proceed to translation, where kind conversion will occur silently. */
5104 : 4161 : if (e->expr_type == EXPR_VARIABLE
5105 : 3314 : && e->ts.type == BT_CHARACTER
5106 : 3314 : && e->symtree->n.sym->attr.flavor == FL_PARAMETER
5107 : 129 : && e->ref && e->ref->type == REF_ARRAY
5108 : 129 : && e->ref->u.ar.type != AR_FULL
5109 : 82 : && e->symtree->n.sym->value)
5110 : : {
5111 : 82 : char name[2*GFC_MAX_SYMBOL_LEN + 12];
5112 : 82 : gfc_namespace *ns = e->symtree->n.sym->ns;
5113 : 82 : gfc_symtree *st;
5114 : 82 : gfc_expr *expr;
5115 : 82 : gfc_expr *p;
5116 : 82 : gfc_constructor *c;
5117 : 82 : int cnt = 0;
5118 : :
5119 : 82 : sprintf (name, "_len_trim_%s_%s", e->symtree->n.sym->name,
5120 : 82 : ns->proc_name->name);
5121 : 82 : st = gfc_find_symtree (ns->sym_root, name);
5122 : 82 : if (st)
5123 : 44 : goto already_built;
5124 : :
5125 : : /* Recursively call this fcn to simplify the constructor elements. */
5126 : 38 : expr = gfc_copy_expr (e->symtree->n.sym->value);
5127 : 38 : expr->ts.type = BT_INTEGER;
5128 : 38 : expr->ts.kind = k;
5129 : 38 : expr->ts.u.cl = NULL;
5130 : 38 : c = gfc_constructor_first (expr->value.constructor);
5131 : 237 : for (; c; c = gfc_constructor_next (c))
5132 : : {
5133 : 161 : if (c->iterator)
5134 : 0 : continue;
5135 : :
5136 : 161 : if (c->expr && c->expr->ts.type == BT_CHARACTER)
5137 : : {
5138 : 161 : p = gfc_simplify_len_trim (c->expr, kind);
5139 : 161 : if (p == NULL)
5140 : 0 : goto clean_up;
5141 : 161 : gfc_replace_expr (c->expr, p);
5142 : 161 : cnt++;
5143 : : }
5144 : : }
5145 : :
5146 : 38 : if (cnt)
5147 : : {
5148 : : /* Build a new parameter to take the result. */
5149 : 38 : st = gfc_new_symtree (&ns->sym_root, name);
5150 : 38 : st->n.sym = gfc_new_symbol (st->name, ns);
5151 : 38 : st->n.sym->value = expr;
5152 : 38 : st->n.sym->ts = expr->ts;
5153 : 38 : st->n.sym->attr.dimension = 1;
5154 : 38 : st->n.sym->attr.save = SAVE_IMPLICIT;
5155 : 38 : st->n.sym->attr.flavor = FL_PARAMETER;
5156 : 38 : st->n.sym->as = gfc_copy_array_spec (e->symtree->n.sym->as);
5157 : 38 : gfc_set_sym_referenced (st->n.sym);
5158 : 38 : st->n.sym->refs++;
5159 : 38 : gfc_commit_symbol (st->n.sym);
5160 : :
5161 : 82 : already_built:
5162 : : /* Build a return expression. */
5163 : 82 : expr = gfc_copy_expr (e);
5164 : 82 : expr->ts = st->n.sym->ts;
5165 : 82 : expr->symtree = st;
5166 : 82 : gfc_expression_rank (expr);
5167 : 82 : return expr;
5168 : : }
5169 : :
5170 : 0 : clean_up:
5171 : 0 : gfc_free_expr (expr);
5172 : 0 : return NULL;
5173 : : }
5174 : :
5175 : 4079 : if (e->expr_type != EXPR_CONSTANT)
5176 : : return NULL;
5177 : :
5178 : 388 : len = e->value.character.length;
5179 : 1215 : for (count = 0, i = 1; i <= len; i++)
5180 : 1203 : if (e->value.character.string[len - i] == ' ')
5181 : 827 : count++;
5182 : : else
5183 : : break;
5184 : :
5185 : 388 : result = gfc_get_int_expr (k, &e->where, len - count);
5186 : 388 : return range_check (result, "LEN_TRIM");
5187 : : }
5188 : :
5189 : : gfc_expr *
5190 : 50 : gfc_simplify_lgamma (gfc_expr *x)
5191 : : {
5192 : 50 : gfc_expr *result;
5193 : 50 : int sg;
5194 : :
5195 : 50 : if (x->expr_type != EXPR_CONSTANT)
5196 : : return NULL;
5197 : :
5198 : 42 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
5199 : 42 : mpfr_lgamma (result->value.real, &sg, x->value.real, GFC_RND_MODE);
5200 : :
5201 : 42 : return range_check (result, "LGAMMA");
5202 : : }
5203 : :
5204 : :
5205 : : gfc_expr *
5206 : 55 : gfc_simplify_lge (gfc_expr *a, gfc_expr *b)
5207 : : {
5208 : 55 : if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
5209 : : return NULL;
5210 : :
5211 : 1 : return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
5212 : 2 : gfc_compare_string (a, b) >= 0);
5213 : : }
5214 : :
5215 : :
5216 : : gfc_expr *
5217 : 81 : gfc_simplify_lgt (gfc_expr *a, gfc_expr *b)
5218 : : {
5219 : 81 : if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
5220 : : return NULL;
5221 : :
5222 : 1 : return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
5223 : 2 : gfc_compare_string (a, b) > 0);
5224 : : }
5225 : :
5226 : :
5227 : : gfc_expr *
5228 : 64 : gfc_simplify_lle (gfc_expr *a, gfc_expr *b)
5229 : : {
5230 : 64 : if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
5231 : : return NULL;
5232 : :
5233 : 1 : return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
5234 : 2 : gfc_compare_string (a, b) <= 0);
5235 : : }
5236 : :
5237 : :
5238 : : gfc_expr *
5239 : 72 : gfc_simplify_llt (gfc_expr *a, gfc_expr *b)
5240 : : {
5241 : 72 : if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
5242 : : return NULL;
5243 : :
5244 : 1 : return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
5245 : 2 : gfc_compare_string (a, b) < 0);
5246 : : }
5247 : :
5248 : :
5249 : : gfc_expr *
5250 : 494 : gfc_simplify_log (gfc_expr *x)
5251 : : {
5252 : 494 : gfc_expr *result;
5253 : :
5254 : 494 : if (x->expr_type != EXPR_CONSTANT)
5255 : : return NULL;
5256 : :
5257 : 229 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
5258 : :
5259 : 229 : switch (x->ts.type)
5260 : : {
5261 : 106 : case BT_REAL:
5262 : 106 : if (mpfr_sgn (x->value.real) <= 0)
5263 : : {
5264 : 0 : gfc_error ("Argument of LOG at %L cannot be less than or equal "
5265 : : "to zero", &x->where);
5266 : 0 : gfc_free_expr (result);
5267 : 0 : return &gfc_bad_expr;
5268 : : }
5269 : :
5270 : 106 : mpfr_log (result->value.real, x->value.real, GFC_RND_MODE);
5271 : 106 : break;
5272 : :
5273 : 123 : case BT_COMPLEX:
5274 : 123 : if (mpfr_zero_p (mpc_realref (x->value.complex))
5275 : 0 : && mpfr_zero_p (mpc_imagref (x->value.complex)))
5276 : : {
5277 : 0 : gfc_error ("Complex argument of LOG at %L cannot be zero",
5278 : : &x->where);
5279 : 0 : gfc_free_expr (result);
5280 : 0 : return &gfc_bad_expr;
5281 : : }
5282 : :
5283 : 123 : gfc_set_model_kind (x->ts.kind);
5284 : 123 : mpc_log (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
5285 : 123 : break;
5286 : :
5287 : 0 : default:
5288 : 0 : gfc_internal_error ("gfc_simplify_log: bad type");
5289 : : }
5290 : :
5291 : 229 : return range_check (result, "LOG");
5292 : : }
5293 : :
5294 : :
5295 : : gfc_expr *
5296 : 328 : gfc_simplify_log10 (gfc_expr *x)
5297 : : {
5298 : 328 : gfc_expr *result;
5299 : :
5300 : 328 : if (x->expr_type != EXPR_CONSTANT)
5301 : : return NULL;
5302 : :
5303 : 82 : if (mpfr_sgn (x->value.real) <= 0)
5304 : : {
5305 : 0 : gfc_error ("Argument of LOG10 at %L cannot be less than or equal "
5306 : : "to zero", &x->where);
5307 : 0 : return &gfc_bad_expr;
5308 : : }
5309 : :
5310 : 82 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
5311 : 82 : mpfr_log10 (result->value.real, x->value.real, GFC_RND_MODE);
5312 : :
5313 : 82 : return range_check (result, "LOG10");
5314 : : }
5315 : :
5316 : :
5317 : : gfc_expr *
5318 : 52 : gfc_simplify_logical (gfc_expr *e, gfc_expr *k)
5319 : : {
5320 : 52 : int kind;
5321 : :
5322 : 52 : kind = get_kind (BT_LOGICAL, k, "LOGICAL", gfc_default_logical_kind);
5323 : 52 : if (kind < 0)
5324 : : return &gfc_bad_expr;
5325 : :
5326 : 52 : if (e->expr_type != EXPR_CONSTANT)
5327 : : return NULL;
5328 : :
5329 : 4 : return gfc_get_logical_expr (kind, &e->where, e->value.logical);
5330 : : }
5331 : :
5332 : :
5333 : : gfc_expr*
5334 : 1148 : gfc_simplify_matmul (gfc_expr *matrix_a, gfc_expr *matrix_b)
5335 : : {
5336 : 1148 : gfc_expr *result;
5337 : 1148 : int row, result_rows, col, result_columns;
5338 : 1148 : int stride_a, offset_a, stride_b, offset_b;
5339 : :
5340 : 1148 : if (!is_constant_array_expr (matrix_a)
5341 : 1148 : || !is_constant_array_expr (matrix_b))
5342 : 1085 : return NULL;
5343 : :
5344 : : /* MATMUL should do mixed-mode arithmetic. Set the result type. */
5345 : 63 : if (matrix_a->ts.type != matrix_b->ts.type)
5346 : : {
5347 : 12 : gfc_expr e;
5348 : 12 : e.expr_type = EXPR_OP;
5349 : 12 : gfc_clear_ts (&e.ts);
5350 : 12 : e.value.op.op = INTRINSIC_NONE;
5351 : 12 : e.value.op.op1 = matrix_a;
5352 : 12 : e.value.op.op2 = matrix_b;
5353 : 12 : gfc_type_convert_binary (&e, 1);
5354 : 12 : result = gfc_get_array_expr (e.ts.type, e.ts.kind, &matrix_a->where);
5355 : : }
5356 : : else
5357 : : {
5358 : 51 : result = gfc_get_array_expr (matrix_a->ts.type, matrix_a->ts.kind,
5359 : : &matrix_a->where);
5360 : : }
5361 : :
5362 : 63 : if (matrix_a->rank == 1 && matrix_b->rank == 2)
5363 : : {
5364 : 7 : result_rows = 1;
5365 : 7 : result_columns = mpz_get_si (matrix_b->shape[1]);
5366 : 7 : stride_a = 1;
5367 : 7 : stride_b = mpz_get_si (matrix_b->shape[0]);
5368 : :
5369 : 7 : result->rank = 1;
5370 : 7 : result->shape = gfc_get_shape (result->rank);
5371 : 7 : mpz_init_set_si (result->shape[0], result_columns);
5372 : : }
5373 : 56 : else if (matrix_a->rank == 2 && matrix_b->rank == 1)
5374 : : {
5375 : 6 : result_rows = mpz_get_si (matrix_a->shape[0]);
5376 : 6 : result_columns = 1;
5377 : 6 : stride_a = mpz_get_si (matrix_a->shape[0]);
5378 : 6 : stride_b = 1;
5379 : :
5380 : 6 : result->rank = 1;
5381 : 6 : result->shape = gfc_get_shape (result->rank);
5382 : 6 : mpz_init_set_si (result->shape[0], result_rows);
5383 : : }
5384 : 50 : else if (matrix_a->rank == 2 && matrix_b->rank == 2)
5385 : : {
5386 : 50 : result_rows = mpz_get_si (matrix_a->shape[0]);
5387 : 50 : result_columns = mpz_get_si (matrix_b->shape[1]);
5388 : 50 : stride_a = mpz_get_si (matrix_a->shape[0]);
5389 : 50 : stride_b = mpz_get_si (matrix_b->shape[0]);
5390 : :
5391 : 50 : result->rank = 2;
5392 : 50 : result->shape = gfc_get_shape (result->rank);
5393 : 50 : mpz_init_set_si (result->shape[0], result_rows);
5394 : 50 : mpz_init_set_si (result->shape[1], result_columns);
5395 : : }
5396 : : else
5397 : 0 : gcc_unreachable();
5398 : :
5399 : 63 : offset_b = 0;
5400 : 223 : for (col = 0; col < result_columns; ++col)
5401 : : {
5402 : : offset_a = 0;
5403 : :
5404 : 578 : for (row = 0; row < result_rows; ++row)
5405 : : {
5406 : 418 : gfc_expr *e = compute_dot_product (matrix_a, stride_a, offset_a,
5407 : : matrix_b, 1, offset_b, false);
5408 : 418 : gfc_constructor_append_expr (&result->value.constructor,
5409 : : e, NULL);
5410 : :
5411 : 418 : offset_a += 1;
5412 : : }
5413 : :
5414 : 160 : offset_b += stride_b;
5415 : : }
5416 : :
5417 : : return result;
5418 : : }
5419 : :
5420 : :
5421 : : gfc_expr *
5422 : 285 : gfc_simplify_maskr (gfc_expr *i, gfc_expr *kind_arg)
5423 : : {
5424 : 285 : gfc_expr *result;
5425 : 285 : int kind, arg, k;
5426 : :
5427 : 285 : if (i->expr_type != EXPR_CONSTANT)
5428 : : return NULL;
5429 : :
5430 : 213 : kind = get_kind (BT_INTEGER, kind_arg, "MASKR", gfc_default_integer_kind);
5431 : 213 : if (kind == -1)
5432 : : return &gfc_bad_expr;
5433 : 213 : k = gfc_validate_kind (BT_INTEGER, kind, false);
5434 : :
5435 : 213 : bool fail = gfc_extract_int (i, &arg);
5436 : 213 : gcc_assert (!fail);
5437 : :
5438 : 213 : if (!gfc_check_mask (i, kind_arg))
5439 : : return &gfc_bad_expr;
5440 : :
5441 : 211 : result = gfc_get_constant_expr (BT_INTEGER, kind, &i->where);
5442 : :
5443 : : /* MASKR(n) = 2^n - 1 */
5444 : 211 : mpz_set_ui (result->value.integer, 1);
5445 : 211 : mpz_mul_2exp (result->value.integer, result->value.integer, arg);
5446 : 211 : mpz_sub_ui (result->value.integer, result->value.integer, 1);
5447 : :
5448 : 211 : gfc_convert_mpz_to_signed (result->value.integer, gfc_integer_kinds[k].bit_size);
5449 : :
5450 : 211 : return result;
5451 : : }
5452 : :
5453 : :
5454 : : gfc_expr *
5455 : 297 : gfc_simplify_maskl (gfc_expr *i, gfc_expr *kind_arg)
5456 : : {
5457 : 297 : gfc_expr *result;
5458 : 297 : int kind, arg, k;
5459 : 297 : mpz_t z;
5460 : :
5461 : 297 : if (i->expr_type != EXPR_CONSTANT)
5462 : : return NULL;
5463 : :
5464 : 217 : kind = get_kind (BT_INTEGER, kind_arg, "MASKL", gfc_default_integer_kind);
5465 : 217 : if (kind == -1)
5466 : : return &gfc_bad_expr;
5467 : 217 : k = gfc_validate_kind (BT_INTEGER, kind, false);
5468 : :
5469 : 217 : bool fail = gfc_extract_int (i, &arg);
5470 : 217 : gcc_assert (!fail);
5471 : :
5472 : 217 : if (!gfc_check_mask (i, kind_arg))
5473 : : return &gfc_bad_expr;
5474 : :
5475 : 213 : result = gfc_get_constant_expr (BT_INTEGER, kind, &i->where);
5476 : :
5477 : : /* MASKL(n) = 2^bit_size - 2^(bit_size - n) */
5478 : 213 : mpz_init_set_ui (z, 1);
5479 : 213 : mpz_mul_2exp (z, z, gfc_integer_kinds[k].bit_size);
5480 : 213 : mpz_set_ui (result->value.integer, 1);
5481 : 213 : mpz_mul_2exp (result->value.integer, result->value.integer,
5482 : 213 : gfc_integer_kinds[k].bit_size - arg);
5483 : 213 : mpz_sub (result->value.integer, z, result->value.integer);
5484 : 213 : mpz_clear (z);
5485 : :
5486 : 213 : gfc_convert_mpz_to_signed (result->value.integer, gfc_integer_kinds[k].bit_size);
5487 : :
5488 : 213 : return result;
5489 : : }
5490 : :
5491 : : /* Similar to gfc_simplify_maskr, but code paths are different enough to make
5492 : : this into a separate function. */
5493 : :
5494 : : gfc_expr *
5495 : 24 : gfc_simplify_umaskr (gfc_expr *i, gfc_expr *kind_arg)
5496 : : {
5497 : 24 : gfc_expr *result;
5498 : 24 : int kind, arg, k;
5499 : :
5500 : 24 : if (i->expr_type != EXPR_CONSTANT)
5501 : : return NULL;
5502 : :
5503 : 24 : kind = get_kind (BT_UNSIGNED, kind_arg, "UMASKR", gfc_default_unsigned_kind);
5504 : 24 : if (kind == -1)
5505 : : return &gfc_bad_expr;
5506 : 24 : k = gfc_validate_kind (BT_UNSIGNED, kind, false);
5507 : :
5508 : 24 : bool fail = gfc_extract_int (i, &arg);
5509 : 24 : gcc_assert (!fail);
5510 : :
5511 : 24 : if (!gfc_check_mask (i, kind_arg))
5512 : : return &gfc_bad_expr;
5513 : :
5514 : 24 : result = gfc_get_constant_expr (BT_UNSIGNED, kind, &i->where);
5515 : :
5516 : : /* MASKR(n) = 2^n - 1 */
5517 : 24 : mpz_set_ui (result->value.integer, 1);
5518 : 24 : mpz_mul_2exp (result->value.integer, result->value.integer, arg);
5519 : 24 : mpz_sub_ui (result->value.integer, result->value.integer, 1);
5520 : :
5521 : 24 : gfc_convert_mpz_to_unsigned (result->value.integer,
5522 : : gfc_unsigned_kinds[k].bit_size,
5523 : : false);
5524 : :
5525 : 24 : return result;
5526 : : }
5527 : :
5528 : : /* Likewise, similar to gfc_simplify_maskl. */
5529 : :
5530 : : gfc_expr *
5531 : 24 : gfc_simplify_umaskl (gfc_expr *i, gfc_expr *kind_arg)
5532 : : {
5533 : 24 : gfc_expr *result;
5534 : 24 : int kind, arg, k;
5535 : 24 : mpz_t z;
5536 : :
5537 : 24 : if (i->expr_type != EXPR_CONSTANT)
5538 : : return NULL;
5539 : :
5540 : 24 : kind = get_kind (BT_UNSIGNED, kind_arg, "UMASKL", gfc_default_integer_kind);
5541 : 24 : if (kind == -1)
5542 : : return &gfc_bad_expr;
5543 : 24 : k = gfc_validate_kind (BT_UNSIGNED, kind, false);
5544 : :
5545 : 24 : bool fail = gfc_extract_int (i, &arg);
5546 : 24 : gcc_assert (!fail);
5547 : :
5548 : 24 : if (!gfc_check_mask (i, kind_arg))
5549 : : return &gfc_bad_expr;
5550 : :
5551 : 24 : result = gfc_get_constant_expr (BT_UNSIGNED, kind, &i->where);
5552 : :
5553 : : /* MASKL(n) = 2^bit_size - 2^(bit_size - n) */
5554 : 24 : mpz_init_set_ui (z, 1);
5555 : 24 : mpz_mul_2exp (z, z, gfc_unsigned_kinds[k].bit_size);
5556 : 24 : mpz_set_ui (result->value.integer, 1);
5557 : 24 : mpz_mul_2exp (result->value.integer, result->value.integer,
5558 : 24 : gfc_integer_kinds[k].bit_size - arg);
5559 : 24 : mpz_sub (result->value.integer, z, result->value.integer);
5560 : 24 : mpz_clear (z);
5561 : :
5562 : 24 : gfc_convert_mpz_to_unsigned (result->value.integer,
5563 : : gfc_unsigned_kinds[k].bit_size,
5564 : : false);
5565 : :
5566 : 24 : return result;
5567 : : }
5568 : :
5569 : :
5570 : : gfc_expr *
5571 : 4063 : gfc_simplify_merge (gfc_expr *tsource, gfc_expr *fsource, gfc_expr *mask)
5572 : : {
5573 : 4063 : gfc_expr * result;
5574 : 4063 : gfc_constructor *tsource_ctor, *fsource_ctor, *mask_ctor;
5575 : :
5576 : 4063 : if (mask->expr_type == EXPR_CONSTANT)
5577 : : {
5578 : : /* The standard requires evaluation of all function arguments.
5579 : : Simplify only when the other dropped argument (FSOURCE or TSOURCE)
5580 : : is a constant expression. */
5581 : 699 : if (mask->value.logical)
5582 : : {
5583 : 482 : if (!gfc_is_constant_expr (fsource))
5584 : : return NULL;
5585 : 168 : result = gfc_copy_expr (tsource);
5586 : : }
5587 : : else
5588 : : {
5589 : 217 : if (!gfc_is_constant_expr (tsource))
5590 : : return NULL;
5591 : 67 : result = gfc_copy_expr (fsource);
5592 : : }
5593 : :
5594 : : /* Parenthesis is needed to get lower bounds of 1. */
5595 : 235 : result = gfc_get_parentheses (result);
5596 : 235 : gfc_simplify_expr (result, 1);
5597 : 235 : return result;
5598 : : }
5599 : :
5600 : 761 : if (!mask->rank || !is_constant_array_expr (mask)
5601 : 3411 : || !is_constant_array_expr (tsource) || !is_constant_array_expr (fsource))
5602 : 3345 : return NULL;
5603 : :
5604 : 19 : result = gfc_get_array_expr (tsource->ts.type, tsource->ts.kind,
5605 : : &tsource->where);
5606 : 19 : if (tsource->ts.type == BT_DERIVED)
5607 : 1 : result->ts.u.derived = tsource->ts.u.derived;
5608 : 18 : else if (tsource->ts.type == BT_CHARACTER)
5609 : 6 : result->ts.u.cl = tsource->ts.u.cl;
5610 : :
5611 : 19 : tsource_ctor = gfc_constructor_first (tsource->value.constructor);
5612 : 19 : fsource_ctor = gfc_constructor_first (fsource->value.constructor);
5613 : 19 : mask_ctor = gfc_constructor_first (mask->value.constructor);
5614 : :
5615 : 87 : while (mask_ctor)
5616 : : {
5617 : 49 : if (mask_ctor->expr->value.logical)
5618 : 31 : gfc_constructor_append_expr (&result->value.constructor,
5619 : : gfc_copy_expr (tsource_ctor->expr),
5620 : : NULL);
5621 : : else
5622 : 18 : gfc_constructor_append_expr (&result->value.constructor,
5623 : : gfc_copy_expr (fsource_ctor->expr),
5624 : : NULL);
5625 : 49 : tsource_ctor = gfc_constructor_next (tsource_ctor);
5626 : 49 : fsource_ctor = gfc_constructor_next (fsource_ctor);
5627 : 49 : mask_ctor = gfc_constructor_next (mask_ctor);
5628 : : }
5629 : :
5630 : 19 : result->shape = gfc_get_shape (1);
5631 : 19 : gfc_array_size (result, &result->shape[0]);
5632 : :
5633 : 19 : return result;
5634 : : }
5635 : :
5636 : :
5637 : : gfc_expr *
5638 : 390 : gfc_simplify_merge_bits (gfc_expr *i, gfc_expr *j, gfc_expr *mask_expr)
5639 : : {
5640 : 390 : mpz_t arg1, arg2, mask;
5641 : 390 : gfc_expr *result;
5642 : :
5643 : 390 : if (i->expr_type != EXPR_CONSTANT || j->expr_type != EXPR_CONSTANT
5644 : 294 : || mask_expr->expr_type != EXPR_CONSTANT)
5645 : : return NULL;
5646 : :
5647 : 294 : result = gfc_get_constant_expr (i->ts.type, i->ts.kind, &i->where);
5648 : :
5649 : : /* Convert all argument to unsigned. */
5650 : 294 : mpz_init_set (arg1, i->value.integer);
5651 : 294 : mpz_init_set (arg2, j->value.integer);
5652 : 294 : mpz_init_set (mask, mask_expr->value.integer);
5653 : :
5654 : : /* MERGE_BITS(I,J,MASK) = IOR (IAND (I, MASK), IAND (J, NOT (MASK))). */
5655 : 294 : mpz_and (arg1, arg1, mask);
5656 : 294 : mpz_com (mask, mask);
5657 : 294 : mpz_and (arg2, arg2, mask);
5658 : 294 : mpz_ior (result->value.integer, arg1, arg2);
5659 : :
5660 : 294 : mpz_clear (arg1);
5661 : 294 : mpz_clear (arg2);
5662 : 294 : mpz_clear (mask);
5663 : :
5664 : 294 : return result;
5665 : : }
5666 : :
5667 : :
5668 : : /* Selects between current value and extremum for simplify_min_max
5669 : : and simplify_minval_maxval. */
5670 : : static int
5671 : 3194 : min_max_choose (gfc_expr *arg, gfc_expr *extremum, int sign, bool back_val)
5672 : : {
5673 : 3194 : int ret;
5674 : :
5675 : 3194 : switch (arg->ts.type)
5676 : : {
5677 : 2099 : case BT_INTEGER:
5678 : 2099 : case BT_UNSIGNED:
5679 : 2099 : if (extremum->ts.kind < arg->ts.kind)
5680 : 1 : extremum->ts.kind = arg->ts.kind;
5681 : 2099 : ret = mpz_cmp (arg->value.integer,
5682 : 2099 : extremum->value.integer) * sign;
5683 : 2099 : if (ret > 0)
5684 : 1277 : mpz_set (extremum->value.integer, arg->value.integer);
5685 : : break;
5686 : :
5687 : 598 : case BT_REAL:
5688 : 598 : if (extremum->ts.kind < arg->ts.kind)
5689 : 25 : extremum->ts.kind = arg->ts.kind;
5690 : 598 : if (mpfr_nan_p (extremum->value.real))
5691 : : {
5692 : 192 : ret = 1;
5693 : 192 : mpfr_set (extremum->value.real, arg->value.real, GFC_RND_MODE);
5694 : : }
5695 : 406 : else if (mpfr_nan_p (arg->value.real))
5696 : : ret = -1;
5697 : : else
5698 : : {
5699 : 286 : ret = mpfr_cmp (arg->value.real, extremum->value.real) * sign;
5700 : 286 : if (ret > 0)
5701 : 140 : mpfr_set (extremum->value.real, arg->value.real, GFC_RND_MODE);
5702 : : }
5703 : : break;
5704 : :
5705 : 497 : case BT_CHARACTER:
5706 : : #define LENGTH(x) ((x)->value.character.length)
5707 : : #define STRING(x) ((x)->value.character.string)
5708 : 497 : if (LENGTH (extremum) < LENGTH(arg))
5709 : : {
5710 : 12 : gfc_char_t *tmp = STRING(extremum);
5711 : :
5712 : 12 : STRING(extremum) = gfc_get_wide_string (LENGTH(arg) + 1);
5713 : 12 : memcpy (STRING(extremum), tmp,
5714 : 12 : LENGTH(extremum) * sizeof (gfc_char_t));
5715 : 12 : gfc_wide_memset (&STRING(extremum)[LENGTH(extremum)], ' ',
5716 : 12 : LENGTH(arg) - LENGTH(extremum));
5717 : 12 : STRING(extremum)[LENGTH(arg)] = '\0'; /* For debugger */
5718 : 12 : LENGTH(extremum) = LENGTH(arg);
5719 : 12 : free (tmp);
5720 : : }
5721 : 497 : ret = gfc_compare_string (arg, extremum) * sign;
5722 : 497 : if (ret > 0)
5723 : : {
5724 : 187 : free (STRING(extremum));
5725 : 187 : STRING(extremum) = gfc_get_wide_string (LENGTH(extremum) + 1);
5726 : 187 : memcpy (STRING(extremum), STRING(arg),
5727 : 187 : LENGTH(arg) * sizeof (gfc_char_t));
5728 : 187 : gfc_wide_memset (&STRING(extremum)[LENGTH(arg)], ' ',
5729 : 187 : LENGTH(extremum) - LENGTH(arg));
5730 : 187 : STRING(extremum)[LENGTH(extremum)] = '\0'; /* For debugger */
5731 : : }
5732 : : #undef LENGTH
5733 : : #undef STRING
5734 : : break;
5735 : :
5736 : 0 : default:
5737 : 0 : gfc_internal_error ("simplify_min_max(): Bad type in arglist");
5738 : : }
5739 : 3194 : if (back_val && ret == 0)
5740 : 59 : ret = 1;
5741 : :
5742 : 3194 : return ret;
5743 : : }
5744 : :
5745 : :
5746 : : /* This function is special since MAX() can take any number of
5747 : : arguments. The simplified expression is a rewritten version of the
5748 : : argument list containing at most one constant element. Other
5749 : : constant elements are deleted. Because the argument list has
5750 : : already been checked, this function always succeeds. sign is 1 for
5751 : : MAX(), -1 for MIN(). */
5752 : :
5753 : : static gfc_expr *
5754 : 5958 : simplify_min_max (gfc_expr *expr, int sign)
5755 : : {
5756 : 5958 : int tmp1, tmp2;
5757 : 5958 : gfc_actual_arglist *arg, *last, *extremum;
5758 : 5958 : gfc_expr *tmp, *ret;
5759 : 5958 : const char *fname;
5760 : :
5761 : 5958 : last = NULL;
5762 : 5958 : extremum = NULL;
5763 : :
5764 : 5958 : arg = expr->value.function.actual;
5765 : :
5766 : 19150 : for (; arg; last = arg, arg = arg->next)
5767 : : {
5768 : 13192 : if (arg->expr->expr_type != EXPR_CONSTANT)
5769 : 7711 : continue;
5770 : :
5771 : 5481 : if (extremum == NULL)
5772 : : {
5773 : 3418 : extremum = arg;
5774 : 3418 : continue;
5775 : : }
5776 : :
5777 : 2063 : min_max_choose (arg->expr, extremum->expr, sign);
5778 : :
5779 : : /* Delete the extra constant argument. */
5780 : 2063 : last->next = arg->next;
5781 : :
5782 : 2063 : arg->next = NULL;
5783 : 2063 : gfc_free_actual_arglist (arg);
5784 : 2063 : arg = last;
5785 : : }
5786 : :
5787 : : /* If there is one value left, replace the function call with the
5788 : : expression. */
5789 : 5958 : if (expr->value.function.actual->next != NULL)
5790 : : return NULL;
5791 : :
5792 : : /* Handle special cases of specific functions (min|max)1 and
5793 : : a(min|max)0. */
5794 : :
5795 : 1682 : tmp = expr->value.function.actual->expr;
5796 : 1682 : fname = expr->value.function.isym->name;
5797 : :
5798 : 1682 : if ((tmp->ts.type != BT_INTEGER || tmp->ts.kind != gfc_integer_4_kind)
5799 : 582 : && (strcmp (fname, "min1") == 0 || strcmp (fname, "max1") == 0))
5800 : : {
5801 : : /* Explicit conversion, turn off -Wconversion and -Wconversion-extra
5802 : : warnings. */
5803 : 15 : tmp1 = warn_conversion;
5804 : 15 : tmp2 = warn_conversion_extra;
5805 : 15 : warn_conversion = warn_conversion_extra = 0;
5806 : :
5807 : 15 : ret = gfc_convert_constant (tmp, BT_INTEGER, gfc_integer_4_kind);
5808 : :
5809 : 15 : warn_conversion = tmp1;
5810 : 15 : warn_conversion_extra = tmp2;
5811 : : }
5812 : 1667 : else if ((tmp->ts.type != BT_REAL || tmp->ts.kind != gfc_real_4_kind)
5813 : 1450 : && (strcmp (fname, "amin0") == 0 || strcmp (fname, "amax0") == 0))
5814 : : {
5815 : 15 : ret = gfc_convert_constant (tmp, BT_REAL, gfc_real_4_kind);
5816 : : }
5817 : : else
5818 : 1652 : ret = gfc_copy_expr (tmp);
5819 : :
5820 : : return ret;
5821 : :
5822 : : }
5823 : :
5824 : :
5825 : : gfc_expr *
5826 : 1875 : gfc_simplify_min (gfc_expr *e)
5827 : : {
5828 : 1875 : return simplify_min_max (e, -1);
5829 : : }
5830 : :
5831 : :
5832 : : gfc_expr *
5833 : 4083 : gfc_simplify_max (gfc_expr *e)
5834 : : {
5835 : 4083 : return simplify_min_max (e, 1);
5836 : : }
5837 : :
5838 : : /* Helper function for gfc_simplify_minval. */
5839 : :
5840 : : static gfc_expr *
5841 : 295 : gfc_min (gfc_expr *op1, gfc_expr *op2)
5842 : : {
5843 : 295 : min_max_choose (op1, op2, -1);
5844 : 295 : gfc_free_expr (op1);
5845 : 295 : return op2;
5846 : : }
5847 : :
5848 : : /* Simplify minval for constant arrays. */
5849 : :
5850 : : gfc_expr *
5851 : 3981 : gfc_simplify_minval (gfc_expr *array, gfc_expr* dim, gfc_expr *mask)
5852 : : {
5853 : 3981 : return simplify_transformation (array, dim, mask, INT_MAX, gfc_min);
5854 : : }
5855 : :
5856 : : /* Helper function for gfc_simplify_maxval. */
5857 : :
5858 : : static gfc_expr *
5859 : 271 : gfc_max (gfc_expr *op1, gfc_expr *op2)
5860 : : {
5861 : 271 : min_max_choose (op1, op2, 1);
5862 : 271 : gfc_free_expr (op1);
5863 : 271 : return op2;
5864 : : }
5865 : :
5866 : :
5867 : : /* Simplify maxval for constant arrays. */
5868 : :
5869 : : gfc_expr *
5870 : 3010 : gfc_simplify_maxval (gfc_expr *array, gfc_expr* dim, gfc_expr *mask)
5871 : : {
5872 : 3010 : return simplify_transformation (array, dim, mask, INT_MIN, gfc_max);
5873 : : }
5874 : :
5875 : :
5876 : : /* Transform minloc or maxloc of an array, according to MASK,
5877 : : to the scalar result. This code is mostly identical to
5878 : : simplify_transformation_to_scalar. */
5879 : :
5880 : : static gfc_expr *
5881 : 82 : simplify_minmaxloc_to_scalar (gfc_expr *result, gfc_expr *array, gfc_expr *mask,
5882 : : gfc_expr *extremum, int sign, bool back_val)
5883 : : {
5884 : 82 : gfc_expr *a, *m;
5885 : 82 : gfc_constructor *array_ctor, *mask_ctor;
5886 : 82 : mpz_t count;
5887 : :
5888 : 82 : mpz_set_si (result->value.integer, 0);
5889 : :
5890 : :
5891 : : /* Shortcut for constant .FALSE. MASK. */
5892 : 82 : if (mask
5893 : 42 : && mask->expr_type == EXPR_CONSTANT
5894 : 36 : && !mask->value.logical)
5895 : : return result;
5896 : :
5897 : 46 : array_ctor = gfc_constructor_first (array->value.constructor);
5898 : 46 : if (mask && mask->expr_type == EXPR_ARRAY)
5899 : 6 : mask_ctor = gfc_constructor_first (mask->value.constructor);
5900 : : else
5901 : : mask_ctor = NULL;
5902 : :
5903 : 46 : mpz_init_set_si (count, 0);
5904 : 216 : while (array_ctor)
5905 : : {
5906 : 124 : mpz_add_ui (count, count, 1);
5907 : 124 : a = array_ctor->expr;
5908 : 124 : array_ctor = gfc_constructor_next (array_ctor);
5909 : : /* A constant MASK equals .TRUE. here and can be ignored. */
5910 : 124 : if (mask_ctor)
5911 : : {
5912 : 28 : m = mask_ctor->expr;
5913 : 28 : mask_ctor = gfc_constructor_next (mask_ctor);
5914 : 28 : if (!m->value.logical)
5915 : 12 : continue;
5916 : : }
5917 : 112 : if (min_max_choose (a, extremum, sign, back_val) > 0)
5918 : 60 : mpz_set (result->value.integer, count);
5919 : : }
5920 : 46 : mpz_clear (count);
5921 : 46 : gfc_free_expr (extremum);
5922 : 46 : return result;
5923 : : }
5924 : :
5925 : : /* Simplify minloc / maxloc in the absence of a dim argument. */
5926 : :
5927 : : static gfc_expr *
5928 : 69 : simplify_minmaxloc_nodim (gfc_expr *result, gfc_expr *extremum,
5929 : : gfc_expr *array, gfc_expr *mask, int sign,
5930 : : bool back_val)
5931 : : {
5932 : 69 : ssize_t res[GFC_MAX_DIMENSIONS];
5933 : 69 : int i, n;
5934 : 69 : gfc_constructor *result_ctor, *array_ctor, *mask_ctor;
5935 : 69 : ssize_t count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS],
5936 : : sstride[GFC_MAX_DIMENSIONS];
5937 : 69 : gfc_expr *a, *m;
5938 : 69 : bool continue_loop;
5939 : 69 : bool ma;
5940 : :
5941 : 154 : for (i = 0; i<array->rank; i++)
5942 : 85 : res[i] = -1;
5943 : :
5944 : : /* Shortcut for constant .FALSE. MASK. */
5945 : 69 : if (mask
5946 : 56 : && mask->expr_type == EXPR_CONSTANT
5947 : 40 : && !mask->value.logical)
5948 : 38 : goto finish;
5949 : :
5950 : 31 : if (array->shape == NULL)
5951 : 1 : goto finish;
5952 : :
5953 : 66 : for (i = 0; i < array->rank; i++)
5954 : : {
5955 : 44 : count[i] = 0;
5956 : 44 : sstride[i] = (i == 0) ? 1 : sstride[i-1] * mpz_get_si (array->shape[i-1]);
5957 : 44 : extent[i] = mpz_get_si (array->shape[i]);
5958 : 44 : if (extent[i] <= 0)
5959 : 8 : goto finish;
5960 : : }
5961 : :
5962 : 22 : continue_loop = true;
5963 : 22 : array_ctor = gfc_constructor_first (array->value.constructor);
5964 : 22 : if (mask && mask->rank > 0)
5965 : 12 : mask_ctor = gfc_constructor_first (mask->value.constructor);
5966 : : else
5967 : : mask_ctor = NULL;
5968 : :
5969 : : /* Loop over the array elements (and mask), keeping track of
5970 : : the indices to return. */
5971 : 66 : while (continue_loop)
5972 : : {
5973 : 120 : do
5974 : : {
5975 : 120 : a = array_ctor->expr;
5976 : 120 : if (mask_ctor)
5977 : : {
5978 : 46 : m = mask_ctor->expr;
5979 : 46 : ma = m->value.logical;
5980 : 46 : mask_ctor = gfc_constructor_next (mask_ctor);
5981 : : }
5982 : : else
5983 : : ma = true;
5984 : :
5985 : 120 : if (ma && min_max_choose (a, extremum, sign, back_val) > 0)
5986 : : {
5987 : 130 : for (i = 0; i<array->rank; i++)
5988 : 86 : res[i] = count[i];
5989 : : }
5990 : 120 : array_ctor = gfc_constructor_next (array_ctor);
5991 : 120 : count[0] ++;
5992 : 120 : } while (count[0] != extent[0]);
5993 : : n = 0;
5994 : 58 : do
5995 : : {
5996 : : /* When we get to the end of a dimension, reset it and increment
5997 : : the next dimension. */
5998 : 58 : count[n] = 0;
5999 : 58 : n++;
6000 : 58 : if (n >= array->rank)
6001 : : {
6002 : : continue_loop = false;
6003 : : break;
6004 : : }
6005 : : else
6006 : 36 : count[n] ++;
6007 : 36 : } while (count[n] == extent[n]);
6008 : : }
6009 : :
6010 : 22 : finish:
6011 : 69 : gfc_free_expr (extremum);
6012 : 69 : result_ctor = gfc_constructor_first (result->value.constructor);
6013 : 154 : for (i = 0; i<array->rank; i++)
6014 : : {
6015 : 85 : gfc_expr *r_expr;
6016 : 85 : r_expr = result_ctor->expr;
6017 : 85 : mpz_set_si (r_expr->value.integer, res[i] + 1);
6018 : 85 : result_ctor = gfc_constructor_next (result_ctor);
6019 : : }
6020 : 69 : return result;
6021 : : }
6022 : :
6023 : : /* Helper function for gfc_simplify_minmaxloc - build an array
6024 : : expression with n elements. */
6025 : :
6026 : : static gfc_expr *
6027 : 116 : new_array (bt type, int kind, int n, locus *where)
6028 : : {
6029 : 116 : gfc_expr *result;
6030 : 116 : int i;
6031 : :
6032 : 116 : result = gfc_get_array_expr (type, kind, where);
6033 : 116 : result->rank = 1;
6034 : 116 : result->shape = gfc_get_shape(1);
6035 : 116 : mpz_init_set_si (result->shape[0], n);
6036 : 401 : for (i = 0; i < n; i++)
6037 : : {
6038 : 169 : gfc_constructor_append_expr (&result->value.constructor,
6039 : : gfc_get_constant_expr (type, kind, where),
6040 : : NULL);
6041 : : }
6042 : :
6043 : 116 : return result;
6044 : : }
6045 : :
6046 : : /* Simplify minloc and maxloc. This code is mostly identical to
6047 : : simplify_transformation_to_array. */
6048 : :
6049 : : static gfc_expr *
6050 : 48 : simplify_minmaxloc_to_array (gfc_expr *result, gfc_expr *array,
6051 : : gfc_expr *dim, gfc_expr *mask,
6052 : : gfc_expr *extremum, int sign, bool back_val)
6053 : : {
6054 : 48 : mpz_t size;
6055 : 48 : int done, i, n, arraysize, resultsize, dim_index, dim_extent, dim_stride;
6056 : 48 : gfc_expr **arrayvec, **resultvec, **base, **src, **dest;
6057 : 48 : gfc_constructor *array_ctor, *mask_ctor, *result_ctor;
6058 : :
6059 : 48 : int count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS],
6060 : : sstride[GFC_MAX_DIMENSIONS], dstride[GFC_MAX_DIMENSIONS],
6061 : : tmpstride[GFC_MAX_DIMENSIONS];
6062 : :
6063 : : /* Shortcut for constant .FALSE. MASK. */
6064 : 48 : if (mask
6065 : 10 : && mask->expr_type == EXPR_CONSTANT
6066 : 0 : && !mask->value.logical)
6067 : : return result;
6068 : :
6069 : : /* Build an indexed table for array element expressions to minimize
6070 : : linked-list traversal. Masked elements are set to NULL. */
6071 : 48 : gfc_array_size (array, &size);
6072 : 48 : arraysize = mpz_get_ui (size);
6073 : 48 : mpz_clear (size);
6074 : :
6075 : 48 : arrayvec = XCNEWVEC (gfc_expr*, arraysize);
6076 : :
6077 : 48 : array_ctor = gfc_constructor_first (array->value.constructor);
6078 : 48 : mask_ctor = NULL;
6079 : 48 : if (mask && mask->expr_type == EXPR_ARRAY)
6080 : 10 : mask_ctor = gfc_constructor_first (mask->value.constructor);
6081 : :
6082 : 474 : for (i = 0; i < arraysize; ++i)
6083 : : {
6084 : 426 : arrayvec[i] = array_ctor->expr;
6085 : 426 : array_ctor = gfc_constructor_next (array_ctor);
6086 : :
6087 : 426 : if (mask_ctor)
6088 : : {
6089 : 106 : if (!mask_ctor->expr->value.logical)
6090 : 65 : arrayvec[i] = NULL;
6091 : :
6092 : 106 : mask_ctor = gfc_constructor_next (mask_ctor);
6093 : : }
6094 : : }
6095 : :
6096 : : /* Same for the result expression. */
6097 : 48 : gfc_array_size (result, &size);
6098 : 48 : resultsize = mpz_get_ui (size);
6099 : 48 : mpz_clear (size);
6100 : :
6101 : 48 : resultvec = XCNEWVEC (gfc_expr*, resultsize);
6102 : 48 : result_ctor = gfc_constructor_first (result->value.constructor);
6103 : 234 : for (i = 0; i < resultsize; ++i)
6104 : : {
6105 : 138 : resultvec[i] = result_ctor->expr;
6106 : 138 : result_ctor = gfc_constructor_next (result_ctor);
6107 : : }
6108 : :
6109 : 48 : gfc_extract_int (dim, &dim_index);
6110 : 48 : dim_index -= 1; /* zero-base index */
6111 : 48 : dim_extent = 0;
6112 : 48 : dim_stride = 0;
6113 : :
6114 : 144 : for (i = 0, n = 0; i < array->rank; ++i)
6115 : : {
6116 : 96 : count[i] = 0;
6117 : 96 : tmpstride[i] = (i == 0) ? 1 : tmpstride[i-1] * mpz_get_si (array->shape[i-1]);
6118 : 96 : if (i == dim_index)
6119 : : {
6120 : 48 : dim_extent = mpz_get_si (array->shape[i]);
6121 : 48 : dim_stride = tmpstride[i];
6122 : 48 : continue;
6123 : : }
6124 : :
6125 : 48 : extent[n] = mpz_get_si (array->shape[i]);
6126 : 48 : sstride[n] = tmpstride[i];
6127 : 48 : dstride[n] = (n == 0) ? 1 : dstride[n-1] * extent[n-1];
6128 : 48 : n += 1;
6129 : : }
6130 : :
6131 : 48 : done = resultsize <= 0;
6132 : 48 : base = arrayvec;
6133 : 48 : dest = resultvec;
6134 : 234 : while (!done)
6135 : : {
6136 : 138 : gfc_expr *ex;
6137 : 138 : ex = gfc_copy_expr (extremum);
6138 : 702 : for (src = base, n = 0; n < dim_extent; src += dim_stride, ++n)
6139 : : {
6140 : 426 : if (*src && min_max_choose (*src, ex, sign, back_val) > 0)
6141 : 215 : mpz_set_si ((*dest)->value.integer, n + 1);
6142 : : }
6143 : :
6144 : 138 : count[0]++;
6145 : 138 : base += sstride[0];
6146 : 138 : dest += dstride[0];
6147 : 138 : gfc_free_expr (ex);
6148 : :
6149 : 138 : n = 0;
6150 : 276 : while (!done && count[n] == extent[n])
6151 : : {
6152 : 46 : count[n] = 0;
6153 : 46 : base -= sstride[n] * extent[n];
6154 : 46 : dest -= dstride[n] * extent[n];
6155 : :
6156 : 46 : n++;
6157 : 46 : if (n < result->rank)
6158 : : {
6159 : : /* If the nested loop is unrolled GFC_MAX_DIMENSIONS
6160 : : times, we'd warn for the last iteration, because the
6161 : : array index will have already been incremented to the
6162 : : array sizes, and we can't tell that this must make
6163 : : the test against result->rank false, because ranks
6164 : : must not exceed GFC_MAX_DIMENSIONS. */
6165 : 0 : GCC_DIAGNOSTIC_PUSH_IGNORED (-Warray-bounds)
6166 : 0 : count[n]++;
6167 : 0 : base += sstride[n];
6168 : 0 : dest += dstride[n];
6169 : 0 : GCC_DIAGNOSTIC_POP
6170 : : }
6171 : : else
6172 : : done = true;
6173 : : }
6174 : : }
6175 : :
6176 : : /* Place updated expression in result constructor. */
6177 : 48 : result_ctor = gfc_constructor_first (result->value.constructor);
6178 : 234 : for (i = 0; i < resultsize; ++i)
6179 : : {
6180 : 138 : result_ctor->expr = resultvec[i];
6181 : 138 : result_ctor = gfc_constructor_next (result_ctor);
6182 : : }
6183 : :
6184 : 48 : free (arrayvec);
6185 : 48 : free (resultvec);
6186 : 48 : free (extremum);
6187 : 48 : return result;
6188 : : }
6189 : :
6190 : : /* Simplify minloc and maxloc for constant arrays. */
6191 : :
6192 : : static gfc_expr *
6193 : 20917 : gfc_simplify_minmaxloc (gfc_expr *array, gfc_expr *dim, gfc_expr *mask,
6194 : : gfc_expr *kind, gfc_expr *back, int sign)
6195 : : {
6196 : 20917 : gfc_expr *result;
6197 : 20917 : gfc_expr *extremum;
6198 : 20917 : int ikind;
6199 : 20917 : int init_val;
6200 : 20917 : bool back_val = false;
6201 : :
6202 : 20917 : if (!is_constant_array_expr (array)
6203 : 20917 : || !gfc_is_constant_expr (dim))
6204 : 20610 : return NULL;
6205 : :
6206 : 307 : if (mask
6207 : 216 : && !is_constant_array_expr (mask)
6208 : 491 : && mask->expr_type != EXPR_CONSTANT)
6209 : : return NULL;
6210 : :
6211 : 199 : if (kind)
6212 : : {
6213 : 0 : if (gfc_extract_int (kind, &ikind, -1))
6214 : : return NULL;
6215 : : }
6216 : : else
6217 : 199 : ikind = gfc_default_integer_kind;
6218 : :
6219 : 199 : if (back)
6220 : : {
6221 : 199 : if (back->expr_type != EXPR_CONSTANT)
6222 : : return NULL;
6223 : :
6224 : 199 : back_val = back->value.logical;
6225 : : }
6226 : :
6227 : 199 : if (sign < 0)
6228 : : init_val = INT_MAX;
6229 : 101 : else if (sign > 0)
6230 : : init_val = INT_MIN;
6231 : : else
6232 : 0 : gcc_unreachable();
6233 : :
6234 : 199 : extremum = gfc_get_constant_expr (array->ts.type, array->ts.kind, &array->where);
6235 : 199 : init_result_expr (extremum, init_val, array);
6236 : :
6237 : 199 : if (dim)
6238 : : {
6239 : 130 : result = transformational_result (array, dim, BT_INTEGER,
6240 : : ikind, &array->where);
6241 : 130 : init_result_expr (result, 0, array);
6242 : :
6243 : 130 : if (array->rank == 1)
6244 : 82 : return simplify_minmaxloc_to_scalar (result, array, mask, extremum,
6245 : 82 : sign, back_val);
6246 : : else
6247 : 48 : return simplify_minmaxloc_to_array (result, array, dim, mask, extremum,
6248 : 48 : sign, back_val);
6249 : : }
6250 : : else
6251 : : {
6252 : 69 : result = new_array (BT_INTEGER, ikind, array->rank, &array->where);
6253 : 69 : return simplify_minmaxloc_nodim (result, extremum, array, mask,
6254 : 69 : sign, back_val);
6255 : : }
6256 : : }
6257 : :
6258 : : gfc_expr *
6259 : 11240 : gfc_simplify_minloc (gfc_expr *array, gfc_expr *dim, gfc_expr *mask, gfc_expr *kind,
6260 : : gfc_expr *back)
6261 : : {
6262 : 11240 : return gfc_simplify_minmaxloc (array, dim, mask, kind, back, -1);
6263 : : }
6264 : :
6265 : : gfc_expr *
6266 : 9677 : gfc_simplify_maxloc (gfc_expr *array, gfc_expr *dim, gfc_expr *mask, gfc_expr *kind,
6267 : : gfc_expr *back)
6268 : : {
6269 : 9677 : return gfc_simplify_minmaxloc (array, dim, mask, kind, back, 1);
6270 : : }
6271 : :
6272 : : /* Simplify findloc to scalar. Similar to
6273 : : simplify_minmaxloc_to_scalar. */
6274 : :
6275 : : static gfc_expr *
6276 : 50 : simplify_findloc_to_scalar (gfc_expr *result, gfc_expr *array, gfc_expr *value,
6277 : : gfc_expr *mask, int back_val)
6278 : : {
6279 : 50 : gfc_expr *a, *m;
6280 : 50 : gfc_constructor *array_ctor, *mask_ctor;
6281 : 50 : mpz_t count;
6282 : :
6283 : 50 : mpz_set_si (result->value.integer, 0);
6284 : :
6285 : : /* Shortcut for constant .FALSE. MASK. */
6286 : 50 : if (mask
6287 : 14 : && mask->expr_type == EXPR_CONSTANT
6288 : 0 : && !mask->value.logical)
6289 : : return result;
6290 : :
6291 : 50 : array_ctor = gfc_constructor_first (array->value.constructor);
6292 : 50 : if (mask && mask->expr_type == EXPR_ARRAY)
6293 : 14 : mask_ctor = gfc_constructor_first (mask->value.constructor);
6294 : : else
6295 : : mask_ctor = NULL;
6296 : :
6297 : 50 : mpz_init_set_si (count, 0);
6298 : 227 : while (array_ctor)
6299 : : {
6300 : 156 : mpz_add_ui (count, count, 1);
6301 : 156 : a = array_ctor->expr;
6302 : 156 : array_ctor = gfc_constructor_next (array_ctor);
6303 : : /* A constant MASK equals .TRUE. here and can be ignored. */
6304 : 156 : if (mask_ctor)
6305 : : {
6306 : 56 : m = mask_ctor->expr;
6307 : 56 : mask_ctor = gfc_constructor_next (mask_ctor);
6308 : 56 : if (!m->value.logical)
6309 : 14 : continue;
6310 : : }
6311 : 142 : if (gfc_compare_expr (a, value, INTRINSIC_EQ) == 0)
6312 : : {
6313 : : /* We have a match. If BACK is true, continue so we find
6314 : : the last one. */
6315 : 50 : mpz_set (result->value.integer, count);
6316 : 50 : if (!back_val)
6317 : : break;
6318 : : }
6319 : : }
6320 : 50 : mpz_clear (count);
6321 : 50 : return result;
6322 : : }
6323 : :
6324 : : /* Simplify findloc in the absence of a dim argument. Similar to
6325 : : simplify_minmaxloc_nodim. */
6326 : :
6327 : : static gfc_expr *
6328 : 47 : simplify_findloc_nodim (gfc_expr *result, gfc_expr *value, gfc_expr *array,
6329 : : gfc_expr *mask, bool back_val)
6330 : : {
6331 : 47 : ssize_t res[GFC_MAX_DIMENSIONS];
6332 : 47 : int i, n;
6333 : 47 : gfc_constructor *result_ctor, *array_ctor, *mask_ctor;
6334 : 47 : ssize_t count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS],
6335 : : sstride[GFC_MAX_DIMENSIONS];
6336 : 47 : gfc_expr *a, *m;
6337 : 47 : bool continue_loop;
6338 : 47 : bool ma;
6339 : :
6340 : 131 : for (i = 0; i < array->rank; i++)
6341 : 84 : res[i] = -1;
6342 : :
6343 : : /* Shortcut for constant .FALSE. MASK. */
6344 : 47 : if (mask
6345 : 7 : && mask->expr_type == EXPR_CONSTANT
6346 : 0 : && !mask->value.logical)
6347 : 0 : goto finish;
6348 : :
6349 : 125 : for (i = 0; i < array->rank; i++)
6350 : : {
6351 : 84 : count[i] = 0;
6352 : 84 : sstride[i] = (i == 0) ? 1 : sstride[i-1] * mpz_get_si (array->shape[i-1]);
6353 : 84 : extent[i] = mpz_get_si (array->shape[i]);
6354 : 84 : if (extent[i] <= 0)
6355 : 6 : goto finish;
6356 : : }
6357 : :
6358 : 41 : continue_loop = true;
6359 : 41 : array_ctor = gfc_constructor_first (array->value.constructor);
6360 : 41 : if (mask && mask->rank > 0)
6361 : 7 : mask_ctor = gfc_constructor_first (mask->value.constructor);
6362 : : else
6363 : : mask_ctor = NULL;
6364 : :
6365 : : /* Loop over the array elements (and mask), keeping track of
6366 : : the indices to return. */
6367 : 93 : while (continue_loop)
6368 : : {
6369 : 138 : do
6370 : : {
6371 : 138 : a = array_ctor->expr;
6372 : 138 : if (mask_ctor)
6373 : : {
6374 : 28 : m = mask_ctor->expr;
6375 : 28 : ma = m->value.logical;
6376 : 28 : mask_ctor = gfc_constructor_next (mask_ctor);
6377 : : }
6378 : : else
6379 : : ma = true;
6380 : :
6381 : 138 : if (ma && gfc_compare_expr (a, value, INTRINSIC_EQ) == 0)
6382 : : {
6383 : 73 : for (i = 0; i < array->rank; i++)
6384 : 48 : res[i] = count[i];
6385 : 25 : if (!back_val)
6386 : 17 : goto finish;
6387 : : }
6388 : 121 : array_ctor = gfc_constructor_next (array_ctor);
6389 : 121 : count[0] ++;
6390 : 121 : } while (count[0] != extent[0]);
6391 : : n = 0;
6392 : 73 : do
6393 : : {
6394 : : /* When we get to the end of a dimension, reset it and increment
6395 : : the next dimension. */
6396 : 73 : count[n] = 0;
6397 : 73 : n++;
6398 : 73 : if (n >= array->rank)
6399 : : {
6400 : : continue_loop = false;
6401 : : break;
6402 : : }
6403 : : else
6404 : 49 : count[n] ++;
6405 : 49 : } while (count[n] == extent[n]);
6406 : : }
6407 : :
6408 : 24 : finish:
6409 : 47 : result_ctor = gfc_constructor_first (result->value.constructor);
6410 : 131 : for (i = 0; i < array->rank; i++)
6411 : : {
6412 : 84 : gfc_expr *r_expr;
6413 : 84 : r_expr = result_ctor->expr;
6414 : 84 : mpz_set_si (r_expr->value.integer, res[i] + 1);
6415 : 84 : result_ctor = gfc_constructor_next (result_ctor);
6416 : : }
6417 : 47 : return result;
6418 : : }
6419 : :
6420 : :
6421 : : /* Simplify findloc to an array. Similar to
6422 : : simplify_minmaxloc_to_array. */
6423 : :
6424 : : static gfc_expr *
6425 : 14 : simplify_findloc_to_array (gfc_expr *result, gfc_expr *array, gfc_expr *value,
6426 : : gfc_expr *dim, gfc_expr *mask, bool back_val)
6427 : : {
6428 : 14 : mpz_t size;
6429 : 14 : int done, i, n, arraysize, resultsize, dim_index, dim_extent, dim_stride;
6430 : 14 : gfc_expr **arrayvec, **resultvec, **base, **src, **dest;
6431 : 14 : gfc_constructor *array_ctor, *mask_ctor, *result_ctor;
6432 : :
6433 : 14 : int count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS],
6434 : : sstride[GFC_MAX_DIMENSIONS], dstride[GFC_MAX_DIMENSIONS],
6435 : : tmpstride[GFC_MAX_DIMENSIONS];
6436 : :
6437 : : /* Shortcut for constant .FALSE. MASK. */
6438 : 14 : if (mask
6439 : 0 : && mask->expr_type == EXPR_CONSTANT
6440 : 0 : && !mask->value.logical)
6441 : : return result;
6442 : :
6443 : : /* Build an indexed table for array element expressions to minimize
6444 : : linked-list traversal. Masked elements are set to NULL. */
6445 : 14 : gfc_array_size (array, &size);
6446 : 14 : arraysize = mpz_get_ui (size);
6447 : 14 : mpz_clear (size);
6448 : :
6449 : 14 : arrayvec = XCNEWVEC (gfc_expr*, arraysize);
6450 : :
6451 : 14 : array_ctor = gfc_constructor_first (array->value.constructor);
6452 : 14 : mask_ctor = NULL;
6453 : 14 : if (mask && mask->expr_type == EXPR_ARRAY)
6454 : 0 : mask_ctor = gfc_constructor_first (mask->value.constructor);
6455 : :
6456 : 98 : for (i = 0; i < arraysize; ++i)
6457 : : {
6458 : 84 : arrayvec[i] = array_ctor->expr;
6459 : 84 : array_ctor = gfc_constructor_next (array_ctor);
6460 : :
6461 : 84 : if (mask_ctor)
6462 : : {
6463 : 0 : if (!mask_ctor->expr->value.logical)
6464 : 0 : arrayvec[i] = NULL;
6465 : :
6466 : 0 : mask_ctor = gfc_constructor_next (mask_ctor);
6467 : : }
6468 : : }
6469 : :
6470 : : /* Same for the result expression. */
6471 : 14 : gfc_array_size (result, &size);
6472 : 14 : resultsize = mpz_get_ui (size);
6473 : 14 : mpz_clear (size);
6474 : :
6475 : 14 : resultvec = XCNEWVEC (gfc_expr*, resultsize);
6476 : 14 : result_ctor = gfc_constructor_first (result->value.constructor);
6477 : 63 : for (i = 0; i < resultsize; ++i)
6478 : : {
6479 : 35 : resultvec[i] = result_ctor->expr;
6480 : 35 : result_ctor = gfc_constructor_next (result_ctor);
6481 : : }
6482 : :
6483 : 14 : gfc_extract_int (dim, &dim_index);
6484 : :
6485 : 14 : dim_index -= 1; /* Zero-base index. */
6486 : 14 : dim_extent = 0;
6487 : 14 : dim_stride = 0;
6488 : :
6489 : 42 : for (i = 0, n = 0; i < array->rank; ++i)
6490 : : {
6491 : 28 : count[i] = 0;
6492 : 28 : tmpstride[i] = (i == 0) ? 1 : tmpstride[i-1] * mpz_get_si (array->shape[i-1]);
6493 : 28 : if (i == dim_index)
6494 : : {
6495 : 14 : dim_extent = mpz_get_si (array->shape[i]);
6496 : 14 : dim_stride = tmpstride[i];
6497 : 14 : continue;
6498 : : }
6499 : :
6500 : 14 : extent[n] = mpz_get_si (array->shape[i]);
6501 : 14 : sstride[n] = tmpstride[i];
6502 : 14 : dstride[n] = (n == 0) ? 1 : dstride[n-1] * extent[n-1];
6503 : 14 : n += 1;
6504 : : }
6505 : :
6506 : 14 : done = resultsize <= 0;
6507 : 14 : base = arrayvec;
6508 : 14 : dest = resultvec;
6509 : 63 : while (!done)
6510 : : {
6511 : 63 : for (src = base, n = 0; n < dim_extent; src += dim_stride, ++n)
6512 : : {
6513 : 56 : if (*src && gfc_compare_expr (*src, value, INTRINSIC_EQ) == 0)
6514 : : {
6515 : 28 : mpz_set_si ((*dest)->value.integer, n + 1);
6516 : 28 : if (!back_val)
6517 : : break;
6518 : : }
6519 : : }
6520 : :
6521 : 35 : count[0]++;
6522 : 35 : base += sstride[0];
6523 : 35 : dest += dstride[0];
6524 : :
6525 : 35 : n = 0;
6526 : 35 : while (!done && count[n] == extent[n])
6527 : : {
6528 : 14 : count[n] = 0;
6529 : 14 : base -= sstride[n] * extent[n];
6530 : 14 : dest -= dstride[n] * extent[n];
6531 : :
6532 : 14 : n++;
6533 : 14 : if (n < result->rank)
6534 : : {
6535 : : /* If the nested loop is unrolled GFC_MAX_DIMENSIONS
6536 : : times, we'd warn for the last iteration, because the
6537 : : array index will have already been incremented to the
6538 : : array sizes, and we can't tell that this must make
6539 : : the test against result->rank false, because ranks
6540 : : must not exceed GFC_MAX_DIMENSIONS. */
6541 : 0 : GCC_DIAGNOSTIC_PUSH_IGNORED (-Warray-bounds)
6542 : 0 : count[n]++;
6543 : 0 : base += sstride[n];
6544 : 0 : dest += dstride[n];
6545 : 0 : GCC_DIAGNOSTIC_POP
6546 : : }
6547 : : else
6548 : : done = true;
6549 : : }
6550 : : }
6551 : :
6552 : : /* Place updated expression in result constructor. */
6553 : 14 : result_ctor = gfc_constructor_first (result->value.constructor);
6554 : 63 : for (i = 0; i < resultsize; ++i)
6555 : : {
6556 : 35 : result_ctor->expr = resultvec[i];
6557 : 35 : result_ctor = gfc_constructor_next (result_ctor);
6558 : : }
6559 : :
6560 : 14 : free (arrayvec);
6561 : 14 : free (resultvec);
6562 : 14 : return result;
6563 : : }
6564 : :
6565 : : /* Simplify findloc. */
6566 : :
6567 : : gfc_expr *
6568 : 1380 : gfc_simplify_findloc (gfc_expr *array, gfc_expr *value, gfc_expr *dim,
6569 : : gfc_expr *mask, gfc_expr *kind, gfc_expr *back)
6570 : : {
6571 : 1380 : gfc_expr *result;
6572 : 1380 : int ikind;
6573 : 1380 : bool back_val = false;
6574 : :
6575 : 1380 : if (!is_constant_array_expr (array)
6576 : 114 : || array->shape == NULL
6577 : 1493 : || !gfc_is_constant_expr (dim))
6578 : 1267 : return NULL;
6579 : :
6580 : 113 : if (! gfc_is_constant_expr (value))
6581 : : return 0;
6582 : :
6583 : 113 : if (mask
6584 : 21 : && !is_constant_array_expr (mask)
6585 : 113 : && mask->expr_type != EXPR_CONSTANT)
6586 : : return NULL;
6587 : :
6588 : 113 : if (kind)
6589 : : {
6590 : 0 : if (gfc_extract_int (kind, &ikind, -1))
6591 : : return NULL;
6592 : : }
6593 : : else
6594 : 113 : ikind = gfc_default_integer_kind;
6595 : :
6596 : 113 : if (back)
6597 : : {
6598 : 113 : if (back->expr_type != EXPR_CONSTANT)
6599 : : return NULL;
6600 : :
6601 : 111 : back_val = back->value.logical;
6602 : : }
6603 : :
6604 : 111 : if (dim)
6605 : : {
6606 : 64 : result = transformational_result (array, dim, BT_INTEGER,
6607 : : ikind, &array->where);
6608 : 64 : init_result_expr (result, 0, array);
6609 : :
6610 : 64 : if (array->rank == 1)
6611 : 50 : return simplify_findloc_to_scalar (result, array, value, mask,
6612 : 50 : back_val);
6613 : : else
6614 : 14 : return simplify_findloc_to_array (result, array, value, dim, mask,
6615 : 14 : back_val);
6616 : : }
6617 : : else
6618 : : {
6619 : 47 : result = new_array (BT_INTEGER, ikind, array->rank, &array->where);
6620 : 47 : return simplify_findloc_nodim (result, value, array, mask, back_val);
6621 : : }
6622 : : return NULL;
6623 : : }
6624 : :
6625 : : gfc_expr *
6626 : 1 : gfc_simplify_maxexponent (gfc_expr *x)
6627 : : {
6628 : 1 : int i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
6629 : 1 : return gfc_get_int_expr (gfc_default_integer_kind, &x->where,
6630 : 1 : gfc_real_kinds[i].max_exponent);
6631 : : }
6632 : :
6633 : :
6634 : : gfc_expr *
6635 : 25 : gfc_simplify_minexponent (gfc_expr *x)
6636 : : {
6637 : 25 : int i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
6638 : 25 : return gfc_get_int_expr (gfc_default_integer_kind, &x->where,
6639 : 25 : gfc_real_kinds[i].min_exponent);
6640 : : }
6641 : :
6642 : :
6643 : : gfc_expr *
6644 : 266798 : gfc_simplify_mod (gfc_expr *a, gfc_expr *p)
6645 : : {
6646 : 266798 : gfc_expr *result;
6647 : 266798 : int kind;
6648 : :
6649 : : /* First check p. */
6650 : 266798 : if (p->expr_type != EXPR_CONSTANT)
6651 : : return NULL;
6652 : :
6653 : : /* p shall not be 0. */
6654 : 266017 : switch (p->ts.type)
6655 : : {
6656 : 265909 : case BT_INTEGER:
6657 : 265909 : case BT_UNSIGNED:
6658 : 265909 : if (mpz_cmp_ui (p->value.integer, 0) == 0)
6659 : : {
6660 : 4 : gfc_error ("Argument %qs of MOD at %L shall not be zero",
6661 : : "P", &p->where);
6662 : 4 : return &gfc_bad_expr;
6663 : : }
6664 : : break;
6665 : 108 : case BT_REAL:
6666 : 108 : if (mpfr_cmp_ui (p->value.real, 0) == 0)
6667 : : {
6668 : 0 : gfc_error ("Argument %qs of MOD at %L shall not be zero",
6669 : : "P", &p->where);
6670 : 0 : return &gfc_bad_expr;
6671 : : }
6672 : : break;
6673 : 0 : default:
6674 : 0 : gfc_internal_error ("gfc_simplify_mod(): Bad arguments");
6675 : : }
6676 : :
6677 : 266013 : if (a->expr_type != EXPR_CONSTANT)
6678 : : return NULL;
6679 : :
6680 : 262810 : kind = a->ts.kind > p->ts.kind ? a->ts.kind : p->ts.kind;
6681 : 262810 : result = gfc_get_constant_expr (a->ts.type, kind, &a->where);
6682 : :
6683 : 262810 : if (a->ts.type == BT_INTEGER || a->ts.type == BT_UNSIGNED)
6684 : 262702 : mpz_tdiv_r (result->value.integer, a->value.integer, p->value.integer);
6685 : : else
6686 : : {
6687 : 108 : gfc_set_model_kind (kind);
6688 : 108 : mpfr_fmod (result->value.real, a->value.real, p->value.real,
6689 : : GFC_RND_MODE);
6690 : : }
6691 : :
6692 : 262810 : return range_check (result, "MOD");
6693 : : }
6694 : :
6695 : :
6696 : : gfc_expr *
6697 : 1916 : gfc_simplify_modulo (gfc_expr *a, gfc_expr *p)
6698 : : {
6699 : 1916 : gfc_expr *result;
6700 : 1916 : int kind;
6701 : :
6702 : : /* First check p. */
6703 : 1916 : if (p->expr_type != EXPR_CONSTANT)
6704 : : return NULL;
6705 : :
6706 : : /* p shall not be 0. */
6707 : 1743 : switch (p->ts.type)
6708 : : {
6709 : 1707 : case BT_INTEGER:
6710 : 1707 : case BT_UNSIGNED:
6711 : 1707 : if (mpz_cmp_ui (p->value.integer, 0) == 0)
6712 : : {
6713 : 4 : gfc_error ("Argument %qs of MODULO at %L shall not be zero",
6714 : : "P", &p->where);
6715 : 4 : return &gfc_bad_expr;
6716 : : }
6717 : : break;
6718 : 36 : case BT_REAL:
6719 : 36 : if (mpfr_cmp_ui (p->value.real, 0) == 0)
6720 : : {
6721 : 0 : gfc_error ("Argument %qs of MODULO at %L shall not be zero",
6722 : : "P", &p->where);
6723 : 0 : return &gfc_bad_expr;
6724 : : }
6725 : : break;
6726 : 0 : default:
6727 : 0 : gfc_internal_error ("gfc_simplify_modulo(): Bad arguments");
6728 : : }
6729 : :
6730 : 1739 : if (a->expr_type != EXPR_CONSTANT)
6731 : : return NULL;
6732 : :
6733 : 252 : kind = a->ts.kind > p->ts.kind ? a->ts.kind : p->ts.kind;
6734 : 252 : result = gfc_get_constant_expr (a->ts.type, kind, &a->where);
6735 : :
6736 : 252 : if (a->ts.type == BT_INTEGER || a->ts.type == BT_UNSIGNED)
6737 : 216 : mpz_fdiv_r (result->value.integer, a->value.integer, p->value.integer);
6738 : : else
6739 : : {
6740 : 36 : gfc_set_model_kind (kind);
6741 : 36 : mpfr_fmod (result->value.real, a->value.real, p->value.real,
6742 : : GFC_RND_MODE);
6743 : 36 : if (mpfr_cmp_ui (result->value.real, 0) != 0)
6744 : : {
6745 : 12 : if (mpfr_signbit (a->value.real) != mpfr_signbit (p->value.real))
6746 : 6 : mpfr_add (result->value.real, result->value.real, p->value.real,
6747 : : GFC_RND_MODE);
6748 : : }
6749 : : else
6750 : 24 : mpfr_copysign (result->value.real, result->value.real,
6751 : : p->value.real, GFC_RND_MODE);
6752 : : }
6753 : :
6754 : 252 : return range_check (result, "MODULO");
6755 : : }
6756 : :
6757 : :
6758 : : gfc_expr *
6759 : 6255 : gfc_simplify_nearest (gfc_expr *x, gfc_expr *s)
6760 : : {
6761 : 6255 : gfc_expr *result;
6762 : 6255 : mpfr_exp_t emin, emax;
6763 : 6255 : int kind;
6764 : :
6765 : 6255 : if (x->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
6766 : : return NULL;
6767 : :
6768 : 821 : result = gfc_copy_expr (x);
6769 : :
6770 : : /* Save current values of emin and emax. */
6771 : 821 : emin = mpfr_get_emin ();
6772 : 821 : emax = mpfr_get_emax ();
6773 : :
6774 : : /* Set emin and emax for the current model number. */
6775 : 821 : kind = gfc_validate_kind (BT_REAL, x->ts.kind, 0);
6776 : 821 : mpfr_set_emin ((mpfr_exp_t) gfc_real_kinds[kind].min_exponent -
6777 : 821 : mpfr_get_prec(result->value.real) + 1);
6778 : 821 : mpfr_set_emax ((mpfr_exp_t) gfc_real_kinds[kind].max_exponent);
6779 : 821 : mpfr_check_range (result->value.real, 0, MPFR_RNDU);
6780 : :
6781 : 821 : if (mpfr_sgn (s->value.real) > 0)
6782 : : {
6783 : 404 : mpfr_nextabove (result->value.real);
6784 : 404 : mpfr_subnormalize (result->value.real, 0, MPFR_RNDU);
6785 : : }
6786 : : else
6787 : : {
6788 : 417 : mpfr_nextbelow (result->value.real);
6789 : 417 : mpfr_subnormalize (result->value.real, 0, MPFR_RNDD);
6790 : : }
6791 : :
6792 : 821 : mpfr_set_emin (emin);
6793 : 821 : mpfr_set_emax (emax);
6794 : :
6795 : : /* Only NaN can occur. Do not use range check as it gives an
6796 : : error for denormal numbers. */
6797 : 821 : if (mpfr_nan_p (result->value.real) && flag_range_check)
6798 : : {
6799 : 0 : gfc_error ("Result of NEAREST is NaN at %L", &result->where);
6800 : 0 : gfc_free_expr (result);
6801 : 0 : return &gfc_bad_expr;
6802 : : }
6803 : :
6804 : : return result;
6805 : : }
6806 : :
6807 : :
6808 : : static gfc_expr *
6809 : 518 : simplify_nint (const char *name, gfc_expr *e, gfc_expr *k)
6810 : : {
6811 : 518 : gfc_expr *itrunc, *result;
6812 : 518 : int kind;
6813 : :
6814 : 518 : kind = get_kind (BT_INTEGER, k, name, gfc_default_integer_kind);
6815 : 518 : if (kind == -1)
6816 : : return &gfc_bad_expr;
6817 : :
6818 : 518 : if (e->expr_type != EXPR_CONSTANT)
6819 : : return NULL;
6820 : :
6821 : 156 : itrunc = gfc_copy_expr (e);
6822 : 156 : mpfr_round (itrunc->value.real, e->value.real);
6823 : :
6824 : 156 : result = gfc_get_constant_expr (BT_INTEGER, kind, &e->where);
6825 : 156 : gfc_mpfr_to_mpz (result->value.integer, itrunc->value.real, &e->where);
6826 : :
6827 : 156 : gfc_free_expr (itrunc);
6828 : :
6829 : 156 : return range_check (result, name);
6830 : : }
6831 : :
6832 : :
6833 : : gfc_expr *
6834 : 331 : gfc_simplify_new_line (gfc_expr *e)
6835 : : {
6836 : 331 : gfc_expr *result;
6837 : :
6838 : 331 : result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, 1);
6839 : 331 : result->value.character.string[0] = '\n';
6840 : :
6841 : 331 : return result;
6842 : : }
6843 : :
6844 : :
6845 : : gfc_expr *
6846 : 406 : gfc_simplify_nint (gfc_expr *e, gfc_expr *k)
6847 : : {
6848 : 406 : return simplify_nint ("NINT", e, k);
6849 : : }
6850 : :
6851 : :
6852 : : gfc_expr *
6853 : 112 : gfc_simplify_idnint (gfc_expr *e)
6854 : : {
6855 : 112 : return simplify_nint ("IDNINT", e, NULL);
6856 : : }
6857 : :
6858 : : static int norm2_scale;
6859 : :
6860 : : static gfc_expr *
6861 : 124 : norm2_add_squared (gfc_expr *result, gfc_expr *e)
6862 : : {
6863 : 124 : mpfr_t tmp;
6864 : :
6865 : 124 : gcc_assert (e->ts.type == BT_REAL && e->expr_type == EXPR_CONSTANT);
6866 : 124 : gcc_assert (result->ts.type == BT_REAL
6867 : : && result->expr_type == EXPR_CONSTANT);
6868 : :
6869 : 124 : gfc_set_model_kind (result->ts.kind);
6870 : 124 : int index = gfc_validate_kind (BT_REAL, result->ts.kind, false);
6871 : 124 : mpfr_exp_t exp;
6872 : 124 : if (mpfr_regular_p (result->value.real))
6873 : : {
6874 : 61 : exp = mpfr_get_exp (result->value.real);
6875 : : /* If result is getting close to overflowing, scale down. */
6876 : 61 : if (exp >= gfc_real_kinds[index].max_exponent - 4
6877 : 0 : && norm2_scale <= gfc_real_kinds[index].max_exponent - 2)
6878 : : {
6879 : 0 : norm2_scale += 2;
6880 : 0 : mpfr_div_ui (result->value.real, result->value.real, 16,
6881 : : GFC_RND_MODE);
6882 : : }
6883 : : }
6884 : :
6885 : 124 : mpfr_init (tmp);
6886 : 124 : if (mpfr_regular_p (e->value.real))
6887 : : {
6888 : 88 : exp = mpfr_get_exp (e->value.real);
6889 : : /* If e**2 would overflow or close to overflowing, scale down. */
6890 : 88 : if (exp - norm2_scale >= gfc_real_kinds[index].max_exponent / 2 - 2)
6891 : : {
6892 : 12 : int new_scale = gfc_real_kinds[index].max_exponent / 2 + 4;
6893 : 12 : mpfr_set_ui (tmp, 1, GFC_RND_MODE);
6894 : 12 : mpfr_set_exp (tmp, new_scale - norm2_scale);
6895 : 12 : mpfr_div (result->value.real, result->value.real, tmp, GFC_RND_MODE);
6896 : 12 : mpfr_div (result->value.real, result->value.real, tmp, GFC_RND_MODE);
6897 : 12 : norm2_scale = new_scale;
6898 : : }
6899 : : }
6900 : 124 : if (norm2_scale)
6901 : : {
6902 : 12 : mpfr_set_ui (tmp, 1, GFC_RND_MODE);
6903 : 12 : mpfr_set_exp (tmp, norm2_scale);
6904 : 12 : mpfr_div (tmp, e->value.real, tmp, GFC_RND_MODE);
6905 : : }
6906 : : else
6907 : 112 : mpfr_set (tmp, e->value.real, GFC_RND_MODE);
6908 : 124 : mpfr_pow_ui (tmp, tmp, 2, GFC_RND_MODE);
6909 : 124 : mpfr_add (result->value.real, result->value.real, tmp,
6910 : : GFC_RND_MODE);
6911 : 124 : mpfr_clear (tmp);
6912 : :
6913 : 124 : return result;
6914 : : }
6915 : :
6916 : :
6917 : : static gfc_expr *
6918 : 2 : norm2_do_sqrt (gfc_expr *result, gfc_expr *e)
6919 : : {
6920 : 2 : gcc_assert (e->ts.type == BT_REAL && e->expr_type == EXPR_CONSTANT);
6921 : 2 : gcc_assert (result->ts.type == BT_REAL
6922 : : && result->expr_type == EXPR_CONSTANT);
6923 : :
6924 : 2 : if (result != e)
6925 : 0 : mpfr_set (result->value.real, e->value.real, GFC_RND_MODE);
6926 : 2 : mpfr_sqrt (result->value.real, result->value.real, GFC_RND_MODE);
6927 : 2 : if (norm2_scale && mpfr_regular_p (result->value.real))
6928 : : {
6929 : 0 : mpfr_t tmp;
6930 : 0 : mpfr_init (tmp);
6931 : 0 : mpfr_set_ui (tmp, 1, GFC_RND_MODE);
6932 : 0 : mpfr_set_exp (tmp, norm2_scale);
6933 : 0 : mpfr_mul (result->value.real, result->value.real, tmp, GFC_RND_MODE);
6934 : 0 : mpfr_clear (tmp);
6935 : : }
6936 : 2 : norm2_scale = 0;
6937 : :
6938 : 2 : return result;
6939 : : }
6940 : :
6941 : :
6942 : : gfc_expr *
6943 : 449 : gfc_simplify_norm2 (gfc_expr *e, gfc_expr *dim)
6944 : : {
6945 : 449 : gfc_expr *result;
6946 : 449 : bool size_zero;
6947 : :
6948 : 449 : size_zero = gfc_is_size_zero_array (e);
6949 : :
6950 : 835 : if (!(is_constant_array_expr (e) || size_zero)
6951 : 449 : || (dim != NULL && !gfc_is_constant_expr (dim)))
6952 : 386 : return NULL;
6953 : :
6954 : 63 : result = transformational_result (e, dim, e->ts.type, e->ts.kind, &e->where);
6955 : 63 : init_result_expr (result, 0, NULL);
6956 : :
6957 : 63 : if (size_zero)
6958 : : return result;
6959 : :
6960 : 38 : norm2_scale = 0;
6961 : 38 : if (!dim || e->rank == 1)
6962 : : {
6963 : 37 : result = simplify_transformation_to_scalar (result, e, NULL,
6964 : : norm2_add_squared);
6965 : 37 : mpfr_sqrt (result->value.real, result->value.real, GFC_RND_MODE);
6966 : 37 : if (norm2_scale && mpfr_regular_p (result->value.real))
6967 : : {
6968 : 12 : mpfr_t tmp;
6969 : 12 : mpfr_init (tmp);
6970 : 12 : mpfr_set_ui (tmp, 1, GFC_RND_MODE);
6971 : 12 : mpfr_set_exp (tmp, norm2_scale);
6972 : 12 : mpfr_mul (result->value.real, result->value.real, tmp, GFC_RND_MODE);
6973 : 12 : mpfr_clear (tmp);
6974 : : }
6975 : 37 : norm2_scale = 0;
6976 : 37 : }
6977 : : else
6978 : 1 : result = simplify_transformation_to_array (result, e, dim, NULL,
6979 : : norm2_add_squared,
6980 : : norm2_do_sqrt);
6981 : :
6982 : : return result;
6983 : : }
6984 : :
6985 : :
6986 : : gfc_expr *
6987 : 597 : gfc_simplify_not (gfc_expr *e)
6988 : : {
6989 : 597 : gfc_expr *result;
6990 : :
6991 : 597 : if (e->expr_type != EXPR_CONSTANT)
6992 : : return NULL;
6993 : :
6994 : 211 : result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
6995 : 211 : mpz_com (result->value.integer, e->value.integer);
6996 : :
6997 : 211 : return range_check (result, "NOT");
6998 : : }
6999 : :
7000 : :
7001 : : gfc_expr *
7002 : 1947 : gfc_simplify_null (gfc_expr *mold)
7003 : : {
7004 : 1947 : gfc_expr *result;
7005 : :
7006 : 1947 : if (mold)
7007 : : {
7008 : 564 : result = gfc_copy_expr (mold);
7009 : 564 : result->expr_type = EXPR_NULL;
7010 : : }
7011 : : else
7012 : 1383 : result = gfc_get_null_expr (NULL);
7013 : :
7014 : 1947 : return result;
7015 : : }
7016 : :
7017 : :
7018 : : gfc_expr *
7019 : 1257 : gfc_simplify_num_images (gfc_expr *team_or_team_number ATTRIBUTE_UNUSED)
7020 : : {
7021 : 1257 : gfc_expr *result;
7022 : :
7023 : 1257 : if (flag_coarray == GFC_FCOARRAY_NONE)
7024 : : {
7025 : 0 : gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
7026 : : return &gfc_bad_expr;
7027 : : }
7028 : :
7029 : 1257 : if (flag_coarray != GFC_FCOARRAY_SINGLE)
7030 : : return NULL;
7031 : :
7032 : : /* FIXME: gfc_current_locus is wrong. */
7033 : 405 : result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
7034 : : &gfc_current_locus);
7035 : 405 : mpz_set_si (result->value.integer, 1);
7036 : :
7037 : 405 : return result;
7038 : : }
7039 : :
7040 : :
7041 : : gfc_expr *
7042 : 20 : gfc_simplify_or (gfc_expr *x, gfc_expr *y)
7043 : : {
7044 : 20 : gfc_expr *result;
7045 : 20 : int kind;
7046 : :
7047 : 20 : if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
7048 : : return NULL;
7049 : :
7050 : 6 : kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
7051 : :
7052 : 6 : switch (x->ts.type)
7053 : : {
7054 : 0 : case BT_INTEGER:
7055 : 0 : result = gfc_get_constant_expr (BT_INTEGER, kind, &x->where);
7056 : 0 : mpz_ior (result->value.integer, x->value.integer, y->value.integer);
7057 : 0 : return range_check (result, "OR");
7058 : :
7059 : 6 : case BT_LOGICAL:
7060 : 6 : return gfc_get_logical_expr (kind, &x->where,
7061 : 12 : x->value.logical || y->value.logical);
7062 : 0 : default:
7063 : 0 : gcc_unreachable();
7064 : : }
7065 : : }
7066 : :
7067 : :
7068 : : gfc_expr *
7069 : 1602 : gfc_simplify_out_of_range (gfc_expr *x, gfc_expr *mold, gfc_expr *round)
7070 : : {
7071 : 1602 : gfc_expr *result;
7072 : 1602 : mpfr_t a;
7073 : 1602 : mpz_t b;
7074 : 1602 : int i, k;
7075 : 1602 : bool res = false;
7076 : 1602 : bool rnd = false;
7077 : :
7078 : 1602 : i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
7079 : 1602 : k = gfc_validate_kind (mold->ts.type, mold->ts.kind, false);
7080 : :
7081 : 1602 : mpfr_init (a);
7082 : :
7083 : 1602 : switch (x->ts.type)
7084 : : {
7085 : 1242 : case BT_REAL:
7086 : 1242 : if (mold->ts.type == BT_REAL)
7087 : : {
7088 : 90 : if (mpfr_cmp (gfc_real_kinds[i].huge,
7089 : : gfc_real_kinds[k].huge) <= 0)
7090 : : {
7091 : : /* Range of MOLD is always sufficient. */
7092 : 42 : res = false;
7093 : 42 : goto done;
7094 : : }
7095 : 48 : else if (x->expr_type == EXPR_CONSTANT)
7096 : : {
7097 : 0 : mpfr_neg (a, gfc_real_kinds[k].huge, GFC_RND_MODE);
7098 : 0 : res = (mpfr_cmp (x->value.real, a) < 0
7099 : 0 : || mpfr_cmp (x->value.real, gfc_real_kinds[k].huge) > 0);
7100 : 0 : goto done;
7101 : : }
7102 : : }
7103 : 1152 : else if (mold->ts.type == BT_INTEGER)
7104 : : {
7105 : 582 : if (x->expr_type == EXPR_CONSTANT)
7106 : : {
7107 : 48 : res = mpfr_inf_p (x->value.real) || mpfr_nan_p (x->value.real);
7108 : 48 : if (res)
7109 : 0 : goto done;
7110 : :
7111 : 48 : if (round && round->expr_type != EXPR_CONSTANT)
7112 : : break;
7113 : :
7114 : 24 : if (round && round->expr_type == EXPR_CONSTANT)
7115 : 24 : rnd = round->value.logical;
7116 : :
7117 : 48 : if (rnd)
7118 : 24 : mpfr_round (a, x->value.real);
7119 : : else
7120 : 24 : mpfr_trunc (a, x->value.real);
7121 : :
7122 : 48 : mpz_init (b);
7123 : 48 : mpfr_get_z (b, a, GFC_RND_MODE);
7124 : 96 : res = (mpz_cmp (b, gfc_integer_kinds[k].min_int) < 0
7125 : 48 : || mpz_cmp (b, gfc_integer_kinds[k].huge) > 0);
7126 : 48 : mpz_clear (b);
7127 : 48 : goto done;
7128 : : }
7129 : : }
7130 : 570 : else if (mold->ts.type == BT_UNSIGNED)
7131 : : {
7132 : 570 : if (x->expr_type == EXPR_CONSTANT)
7133 : : {
7134 : 48 : res = mpfr_inf_p (x->value.real) || mpfr_nan_p (x->value.real);
7135 : 48 : if (res)
7136 : 0 : goto done;
7137 : :
7138 : 48 : if (round && round->expr_type != EXPR_CONSTANT)
7139 : : break;
7140 : :
7141 : 24 : if (round && round->expr_type == EXPR_CONSTANT)
7142 : 24 : rnd = round->value.logical;
7143 : :
7144 : 24 : if (rnd)
7145 : 24 : mpfr_round (a, x->value.real);
7146 : : else
7147 : 24 : mpfr_trunc (a, x->value.real);
7148 : :
7149 : 48 : mpz_init (b);
7150 : 48 : mpfr_get_z (b, a, GFC_RND_MODE);
7151 : 96 : res = (mpz_cmp (b, gfc_unsigned_kinds[k].huge) > 0
7152 : 48 : || mpz_cmp_si (b, 0) < 0);
7153 : 48 : mpz_clear (b);
7154 : 48 : goto done;
7155 : : }
7156 : : }
7157 : : break;
7158 : :
7159 : 168 : case BT_INTEGER:
7160 : 168 : gcc_assert (round == NULL);
7161 : 168 : if (mold->ts.type == BT_INTEGER)
7162 : : {
7163 : 54 : if (mpz_cmp (gfc_integer_kinds[i].huge,
7164 : 54 : gfc_integer_kinds[k].huge) <= 0)
7165 : : {
7166 : : /* Range of MOLD is always sufficient. */
7167 : 18 : res = false;
7168 : 18 : goto done;
7169 : : }
7170 : 36 : else if (x->expr_type == EXPR_CONSTANT)
7171 : : {
7172 : 0 : res = (mpz_cmp (x->value.integer,
7173 : 0 : gfc_integer_kinds[k].min_int) < 0
7174 : 0 : || mpz_cmp (x->value.integer,
7175 : : gfc_integer_kinds[k].huge) > 0);
7176 : 0 : goto done;
7177 : : }
7178 : : }
7179 : 114 : else if (mold->ts.type == BT_UNSIGNED)
7180 : : {
7181 : 90 : if (x->expr_type == EXPR_CONSTANT)
7182 : : {
7183 : 0 : res = (mpz_cmp_si (x->value.integer, 0) < 0
7184 : 0 : || mpz_cmp (x->value.integer,
7185 : 0 : gfc_unsigned_kinds[k].huge) > 0);
7186 : 0 : goto done;
7187 : : }
7188 : : }
7189 : 24 : else if (mold->ts.type == BT_REAL)
7190 : : {
7191 : 24 : mpfr_set_z (a, gfc_integer_kinds[i].min_int, GFC_RND_MODE);
7192 : 24 : mpfr_neg (a, a, GFC_RND_MODE);
7193 : 24 : res = mpfr_cmp (a, gfc_real_kinds[k].huge) > 0;
7194 : : /* When false, range of MOLD is always sufficient. */
7195 : 24 : if (!res)
7196 : 24 : goto done;
7197 : :
7198 : 0 : if (x->expr_type == EXPR_CONSTANT)
7199 : : {
7200 : 0 : mpfr_set_z (a, x->value.integer, GFC_RND_MODE);
7201 : 0 : mpfr_abs (a, a, GFC_RND_MODE);
7202 : 0 : res = mpfr_cmp (a, gfc_real_kinds[k].huge) > 0;
7203 : 0 : goto done;
7204 : : }
7205 : : }
7206 : : break;
7207 : :
7208 : 192 : case BT_UNSIGNED:
7209 : 192 : gcc_assert (round == NULL);
7210 : 192 : if (mold->ts.type == BT_UNSIGNED)
7211 : : {
7212 : 54 : if (mpz_cmp (gfc_unsigned_kinds[i].huge,
7213 : 54 : gfc_unsigned_kinds[k].huge) <= 0)
7214 : : {
7215 : : /* Range of MOLD is always sufficient. */
7216 : 18 : res = false;
7217 : 18 : goto done;
7218 : : }
7219 : 36 : else if (x->expr_type == EXPR_CONSTANT)
7220 : : {
7221 : 0 : res = mpz_cmp (x->value.integer,
7222 : : gfc_unsigned_kinds[k].huge) > 0;
7223 : 0 : goto done;
7224 : : }
7225 : : }
7226 : 138 : else if (mold->ts.type == BT_INTEGER)
7227 : : {
7228 : 60 : if (mpz_cmp (gfc_unsigned_kinds[i].huge,
7229 : 60 : gfc_integer_kinds[k].huge) <= 0)
7230 : : {
7231 : : /* Range of MOLD is always sufficient. */
7232 : 6 : res = false;
7233 : 6 : goto done;
7234 : : }
7235 : 54 : else if (x->expr_type == EXPR_CONSTANT)
7236 : : {
7237 : 0 : res = mpz_cmp (x->value.integer,
7238 : : gfc_integer_kinds[k].huge) > 0;
7239 : 0 : goto done;
7240 : : }
7241 : : }
7242 : 78 : else if (mold->ts.type == BT_REAL)
7243 : : {
7244 : 78 : mpfr_set_z (a, gfc_unsigned_kinds[i].huge, GFC_RND_MODE);
7245 : 78 : res = mpfr_cmp (a, gfc_real_kinds[k].huge) > 0;
7246 : : /* When false, range of MOLD is always sufficient. */
7247 : 78 : if (!res)
7248 : 36 : goto done;
7249 : :
7250 : 42 : if (x->expr_type == EXPR_CONSTANT)
7251 : : {
7252 : 12 : mpfr_set_z (a, x->value.integer, GFC_RND_MODE);
7253 : 12 : res = mpfr_cmp (a, gfc_real_kinds[k].huge) > 0;
7254 : 12 : goto done;
7255 : : }
7256 : : }
7257 : : break;
7258 : :
7259 : 0 : default:
7260 : 0 : gcc_unreachable ();
7261 : : }
7262 : :
7263 : 1350 : mpfr_clear (a);
7264 : :
7265 : 1350 : return NULL;
7266 : :
7267 : 252 : done:
7268 : 252 : result = gfc_get_logical_expr (gfc_default_logical_kind, &x->where, res);
7269 : :
7270 : 252 : mpfr_clear (a);
7271 : :
7272 : 252 : return result;
7273 : : }
7274 : :
7275 : :
7276 : : gfc_expr *
7277 : 982 : gfc_simplify_pack (gfc_expr *array, gfc_expr *mask, gfc_expr *vector)
7278 : : {
7279 : 982 : gfc_expr *result;
7280 : 982 : gfc_constructor *array_ctor, *mask_ctor, *vector_ctor;
7281 : :
7282 : 982 : if (!is_constant_array_expr (array)
7283 : 58 : || !is_constant_array_expr (vector)
7284 : 1040 : || (!gfc_is_constant_expr (mask)
7285 : 2 : && !is_constant_array_expr (mask)))
7286 : 925 : return NULL;
7287 : :
7288 : 57 : result = gfc_get_array_expr (array->ts.type, array->ts.kind, &array->where);
7289 : 57 : if (array->ts.type == BT_DERIVED)
7290 : 5 : result->ts.u.derived = array->ts.u.derived;
7291 : :
7292 : 57 : array_ctor = gfc_constructor_first (array->value.constructor);
7293 : 57 : vector_ctor = vector
7294 : 57 : ? gfc_constructor_first (vector->value.constructor)
7295 : : : NULL;
7296 : :
7297 : 57 : if (mask->expr_type == EXPR_CONSTANT
7298 : 0 : && mask->value.logical)
7299 : : {
7300 : : /* Copy all elements of ARRAY to RESULT. */
7301 : 0 : while (array_ctor)
7302 : : {
7303 : 0 : gfc_constructor_append_expr (&result->value.constructor,
7304 : : gfc_copy_expr (array_ctor->expr),
7305 : : NULL);
7306 : :
7307 : 0 : array_ctor = gfc_constructor_next (array_ctor);
7308 : 0 : vector_ctor = gfc_constructor_next (vector_ctor);
7309 : : }
7310 : : }
7311 : 57 : else if (mask->expr_type == EXPR_ARRAY)
7312 : : {
7313 : : /* Copy only those elements of ARRAY to RESULT whose
7314 : : MASK equals .TRUE.. */
7315 : 57 : mask_ctor = gfc_constructor_first (mask->value.constructor);
7316 : 303 : while (mask_ctor && array_ctor)
7317 : : {
7318 : 189 : if (mask_ctor->expr->value.logical)
7319 : : {
7320 : 130 : gfc_constructor_append_expr (&result->value.constructor,
7321 : : gfc_copy_expr (array_ctor->expr),
7322 : : NULL);
7323 : 130 : vector_ctor = gfc_constructor_next (vector_ctor);
7324 : : }
7325 : :
7326 : 189 : array_ctor = gfc_constructor_next (array_ctor);
7327 : 189 : mask_ctor = gfc_constructor_next (mask_ctor);
7328 : : }
7329 : : }
7330 : :
7331 : : /* Append any left-over elements from VECTOR to RESULT. */
7332 : 85 : while (vector_ctor)
7333 : : {
7334 : 28 : gfc_constructor_append_expr (&result->value.constructor,
7335 : : gfc_copy_expr (vector_ctor->expr),
7336 : : NULL);
7337 : 28 : vector_ctor = gfc_constructor_next (vector_ctor);
7338 : : }
7339 : :
7340 : 57 : result->shape = gfc_get_shape (1);
7341 : 57 : gfc_array_size (result, &result->shape[0]);
7342 : :
7343 : 57 : if (array->ts.type == BT_CHARACTER)
7344 : 51 : result->ts.u.cl = array->ts.u.cl;
7345 : :
7346 : : return result;
7347 : : }
7348 : :
7349 : :
7350 : : static gfc_expr *
7351 : 124 : do_xor (gfc_expr *result, gfc_expr *e)
7352 : : {
7353 : 124 : gcc_assert (e->ts.type == BT_LOGICAL && e->expr_type == EXPR_CONSTANT);
7354 : 124 : gcc_assert (result->ts.type == BT_LOGICAL
7355 : : && result->expr_type == EXPR_CONSTANT);
7356 : :
7357 : 124 : result->value.logical = result->value.logical != e->value.logical;
7358 : 124 : return result;
7359 : : }
7360 : :
7361 : :
7362 : : gfc_expr *
7363 : 992 : gfc_simplify_is_contiguous (gfc_expr *array)
7364 : : {
7365 : 992 : if (gfc_is_simply_contiguous (array, false, true))
7366 : 33 : return gfc_get_logical_expr (gfc_default_logical_kind, &array->where, 1);
7367 : :
7368 : 959 : if (gfc_is_not_contiguous (array))
7369 : 6 : return gfc_get_logical_expr (gfc_default_logical_kind, &array->where, 0);
7370 : :
7371 : : return NULL;
7372 : : }
7373 : :
7374 : :
7375 : : gfc_expr *
7376 : 147 : gfc_simplify_parity (gfc_expr *e, gfc_expr *dim)
7377 : : {
7378 : 147 : return simplify_transformation (e, dim, NULL, 0, do_xor);
7379 : : }
7380 : :
7381 : :
7382 : : gfc_expr *
7383 : 1064 : gfc_simplify_popcnt (gfc_expr *e)
7384 : : {
7385 : 1064 : int res, k;
7386 : 1064 : mpz_t x;
7387 : :
7388 : 1064 : if (e->expr_type != EXPR_CONSTANT)
7389 : : return NULL;
7390 : :
7391 : 642 : k = gfc_validate_kind (e->ts.type, e->ts.kind, false);
7392 : :
7393 : 642 : if (flag_unsigned && e->ts.type == BT_UNSIGNED)
7394 : 0 : res = mpz_popcount (e->value.integer);
7395 : : else
7396 : : {
7397 : : /* Convert argument to unsigned, then count the '1' bits. */
7398 : 642 : mpz_init_set (x, e->value.integer);
7399 : 642 : gfc_convert_mpz_to_unsigned (x, gfc_integer_kinds[k].bit_size);
7400 : 642 : res = mpz_popcount (x);
7401 : 642 : mpz_clear (x);
7402 : : }
7403 : :
7404 : 642 : return gfc_get_int_expr (gfc_default_integer_kind, &e->where, res);
7405 : : }
7406 : :
7407 : :
7408 : : gfc_expr *
7409 : 362 : gfc_simplify_poppar (gfc_expr *e)
7410 : : {
7411 : 362 : gfc_expr *popcnt;
7412 : 362 : int i;
7413 : :
7414 : 362 : if (e->expr_type != EXPR_CONSTANT)
7415 : : return NULL;
7416 : :
7417 : 300 : popcnt = gfc_simplify_popcnt (e);
7418 : 300 : gcc_assert (popcnt);
7419 : :
7420 : 300 : bool fail = gfc_extract_int (popcnt, &i);
7421 : 300 : gcc_assert (!fail);
7422 : :
7423 : 300 : return gfc_get_int_expr (gfc_default_integer_kind, &e->where, i % 2);
7424 : : }
7425 : :
7426 : :
7427 : : gfc_expr *
7428 : 459 : gfc_simplify_precision (gfc_expr *e)
7429 : : {
7430 : 459 : int i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
7431 : 459 : return gfc_get_int_expr (gfc_default_integer_kind, &e->where,
7432 : 459 : gfc_real_kinds[i].precision);
7433 : : }
7434 : :
7435 : :
7436 : : gfc_expr *
7437 : 825 : gfc_simplify_product (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
7438 : : {
7439 : 825 : return simplify_transformation (array, dim, mask, 1, gfc_multiply);
7440 : : }
7441 : :
7442 : :
7443 : : gfc_expr *
7444 : 61 : gfc_simplify_radix (gfc_expr *e)
7445 : : {
7446 : 61 : int i;
7447 : 61 : i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
7448 : :
7449 : 61 : switch (e->ts.type)
7450 : : {
7451 : 0 : case BT_INTEGER:
7452 : 0 : i = gfc_integer_kinds[i].radix;
7453 : 0 : break;
7454 : :
7455 : 61 : case BT_REAL:
7456 : 61 : i = gfc_real_kinds[i].radix;
7457 : 61 : break;
7458 : :
7459 : 0 : default:
7460 : 0 : gcc_unreachable ();
7461 : : }
7462 : :
7463 : 61 : return gfc_get_int_expr (gfc_default_integer_kind, &e->where, i);
7464 : : }
7465 : :
7466 : :
7467 : : gfc_expr *
7468 : 182 : gfc_simplify_range (gfc_expr *e)
7469 : : {
7470 : 182 : int i;
7471 : 182 : i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
7472 : :
7473 : 182 : switch (e->ts.type)
7474 : : {
7475 : 87 : case BT_INTEGER:
7476 : 87 : i = gfc_integer_kinds[i].range;
7477 : 87 : break;
7478 : :
7479 : 24 : case BT_UNSIGNED:
7480 : 24 : i = gfc_unsigned_kinds[i].range;
7481 : 24 : break;
7482 : :
7483 : 71 : case BT_REAL:
7484 : 71 : case BT_COMPLEX:
7485 : 71 : i = gfc_real_kinds[i].range;
7486 : 71 : break;
7487 : :
7488 : 0 : default:
7489 : 0 : gcc_unreachable ();
7490 : : }
7491 : :
7492 : 182 : return gfc_get_int_expr (gfc_default_integer_kind, &e->where, i);
7493 : : }
7494 : :
7495 : :
7496 : : gfc_expr *
7497 : 2101 : gfc_simplify_rank (gfc_expr *e)
7498 : : {
7499 : : /* Assumed rank. */
7500 : 2101 : if (e->rank == -1)
7501 : : return NULL;
7502 : :
7503 : 590 : return gfc_get_int_expr (gfc_default_integer_kind, &e->where, e->rank);
7504 : : }
7505 : :
7506 : :
7507 : : gfc_expr *
7508 : 29244 : gfc_simplify_real (gfc_expr *e, gfc_expr *k)
7509 : : {
7510 : 29244 : gfc_expr *result = NULL;
7511 : 29244 : int kind, tmp1, tmp2;
7512 : :
7513 : : /* Convert BOZ to real, and return without range checking. */
7514 : 29244 : if (e->ts.type == BT_BOZ)
7515 : : {
7516 : : /* Determine kind for conversion of the BOZ. */
7517 : 85 : if (k)
7518 : 63 : gfc_extract_int (k, &kind);
7519 : : else
7520 : 22 : kind = gfc_default_real_kind;
7521 : :
7522 : 85 : if (!gfc_boz2real (e, kind))
7523 : : return NULL;
7524 : 85 : result = gfc_copy_expr (e);
7525 : 85 : return result;
7526 : : }
7527 : :
7528 : 29159 : if (e->ts.type == BT_COMPLEX)
7529 : 2023 : kind = get_kind (BT_REAL, k, "REAL", e->ts.kind);
7530 : : else
7531 : 27136 : kind = get_kind (BT_REAL, k, "REAL", gfc_default_real_kind);
7532 : :
7533 : 29159 : if (kind == -1)
7534 : : return &gfc_bad_expr;
7535 : :
7536 : 29159 : if (e->expr_type != EXPR_CONSTANT)
7537 : : return NULL;
7538 : :
7539 : : /* For explicit conversion, turn off -Wconversion and -Wconversion-extra
7540 : : warnings. */
7541 : 23700 : tmp1 = warn_conversion;
7542 : 23700 : tmp2 = warn_conversion_extra;
7543 : 23700 : warn_conversion = warn_conversion_extra = 0;
7544 : :
7545 : 23700 : result = gfc_convert_constant (e, BT_REAL, kind);
7546 : :
7547 : 23700 : warn_conversion = tmp1;
7548 : 23700 : warn_conversion_extra = tmp2;
7549 : :
7550 : 23700 : if (result == &gfc_bad_expr)
7551 : : return &gfc_bad_expr;
7552 : :
7553 : 23699 : return range_check (result, "REAL");
7554 : : }
7555 : :
7556 : :
7557 : : gfc_expr *
7558 : 7 : gfc_simplify_realpart (gfc_expr *e)
7559 : : {
7560 : 7 : gfc_expr *result;
7561 : :
7562 : 7 : if (e->expr_type != EXPR_CONSTANT)
7563 : : return NULL;
7564 : :
7565 : 1 : result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
7566 : 1 : mpc_real (result->value.real, e->value.complex, GFC_RND_MODE);
7567 : :
7568 : 1 : return range_check (result, "REALPART");
7569 : : }
7570 : :
7571 : : gfc_expr *
7572 : 2645 : gfc_simplify_repeat (gfc_expr *e, gfc_expr *n)
7573 : : {
7574 : 2645 : gfc_expr *result;
7575 : 2645 : gfc_charlen_t len;
7576 : 2645 : mpz_t ncopies;
7577 : 2645 : bool have_length = false;
7578 : :
7579 : : /* If NCOPIES isn't a constant, there's nothing we can do. */
7580 : 2645 : if (n->expr_type != EXPR_CONSTANT)
7581 : : return NULL;
7582 : :
7583 : : /* If NCOPIES is negative, it's an error. */
7584 : 2087 : if (mpz_sgn (n->value.integer) < 0)
7585 : : {
7586 : 6 : gfc_error ("Argument NCOPIES of REPEAT intrinsic is negative at %L",
7587 : : &n->where);
7588 : 6 : return &gfc_bad_expr;
7589 : : }
7590 : :
7591 : : /* If we don't know the character length, we can do no more. */
7592 : 2081 : if (e->ts.u.cl && e->ts.u.cl->length
7593 : 410 : && e->ts.u.cl->length->expr_type == EXPR_CONSTANT)
7594 : : {
7595 : 410 : len = gfc_mpz_get_hwi (e->ts.u.cl->length->value.integer);
7596 : 410 : have_length = true;
7597 : : }
7598 : 1671 : else if (e->expr_type == EXPR_CONSTANT
7599 : 1671 : && (e->ts.u.cl == NULL || e->ts.u.cl->length == NULL))
7600 : : {
7601 : 1671 : len = e->value.character.length;
7602 : : }
7603 : : else
7604 : : return NULL;
7605 : :
7606 : : /* If the source length is 0, any value of NCOPIES is valid
7607 : : and everything behaves as if NCOPIES == 0. */
7608 : 2081 : mpz_init (ncopies);
7609 : 2081 : if (len == 0)
7610 : 63 : mpz_set_ui (ncopies, 0);
7611 : : else
7612 : 2018 : mpz_set (ncopies, n->value.integer);
7613 : :
7614 : : /* Check that NCOPIES isn't too large. */
7615 : 2081 : if (len)
7616 : : {
7617 : 2018 : mpz_t max, mlen;
7618 : 2018 : int i;
7619 : :
7620 : : /* Compute the maximum value allowed for NCOPIES: huge(cl) / len. */
7621 : 2018 : mpz_init (max);
7622 : 2018 : i = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
7623 : :
7624 : 2018 : if (have_length)
7625 : : {
7626 : 353 : mpz_tdiv_q (max, gfc_integer_kinds[i].huge,
7627 : 353 : e->ts.u.cl->length->value.integer);
7628 : : }
7629 : : else
7630 : : {
7631 : 1665 : mpz_init (mlen);
7632 : 1665 : gfc_mpz_set_hwi (mlen, len);
7633 : 1665 : mpz_tdiv_q (max, gfc_integer_kinds[i].huge, mlen);
7634 : 1665 : mpz_clear (mlen);
7635 : : }
7636 : :
7637 : : /* The check itself. */
7638 : 2018 : if (mpz_cmp (ncopies, max) > 0)
7639 : : {
7640 : 4 : mpz_clear (max);
7641 : 4 : mpz_clear (ncopies);
7642 : 4 : gfc_error ("Argument NCOPIES of REPEAT intrinsic is too large at %L",
7643 : : &n->where);
7644 : 4 : return &gfc_bad_expr;
7645 : : }
7646 : :
7647 : 2014 : mpz_clear (max);
7648 : : }
7649 : 2077 : mpz_clear (ncopies);
7650 : :
7651 : : /* For further simplification, we need the character string to be
7652 : : constant. */
7653 : 2077 : if (e->expr_type != EXPR_CONSTANT)
7654 : : return NULL;
7655 : :
7656 : 1732 : HOST_WIDE_INT ncop;
7657 : 1732 : if (len ||
7658 : 42 : (e->ts.u.cl->length &&
7659 : 18 : mpz_sgn (e->ts.u.cl->length->value.integer) != 0))
7660 : : {
7661 : 1708 : bool fail = gfc_extract_hwi (n, &ncop);
7662 : 1708 : gcc_assert (!fail);
7663 : : }
7664 : : else
7665 : 24 : ncop = 0;
7666 : :
7667 : 1732 : if (ncop == 0)
7668 : 54 : return gfc_get_character_expr (e->ts.kind, &e->where, NULL, 0);
7669 : :
7670 : 1678 : len = e->value.character.length;
7671 : 1678 : gfc_charlen_t nlen = ncop * len;
7672 : :
7673 : : /* Here's a semi-arbitrary limit. If the string is longer than 1 GB
7674 : : (2**28 elements * 4 bytes (wide chars) per element) defer to
7675 : : runtime instead of consuming (unbounded) memory and CPU at
7676 : : compile time. */
7677 : 1678 : if (nlen > 268435456)
7678 : : {
7679 : 1 : gfc_warning_now (0, "Evaluation of string longer than 2**28 at %L"
7680 : : " deferred to runtime, expect bugs", &e->where);
7681 : 1 : return NULL;
7682 : : }
7683 : :
7684 : 1677 : result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, nlen);
7685 : 59140 : for (size_t i = 0; i < (size_t) ncop; i++)
7686 : 115256 : for (size_t j = 0; j < (size_t) len; j++)
7687 : 57793 : result->value.character.string[j+i*len]= e->value.character.string[j];
7688 : :
7689 : 1677 : result->value.character.string[nlen] = '\0'; /* For debugger */
7690 : 1677 : return result;
7691 : : }
7692 : :
7693 : :
7694 : : /* This one is a bear, but mainly has to do with shuffling elements. */
7695 : :
7696 : : gfc_expr *
7697 : 9410 : gfc_simplify_reshape (gfc_expr *source, gfc_expr *shape_exp,
7698 : : gfc_expr *pad, gfc_expr *order_exp)
7699 : : {
7700 : 9410 : int order[GFC_MAX_DIMENSIONS], shape[GFC_MAX_DIMENSIONS];
7701 : 9410 : int i, rank, npad, x[GFC_MAX_DIMENSIONS];
7702 : 9410 : mpz_t index, size;
7703 : 9410 : unsigned long j;
7704 : 9410 : size_t nsource;
7705 : 9410 : gfc_expr *e, *result;
7706 : 9410 : bool zerosize = false;
7707 : :
7708 : : /* Check that argument expression types are OK. */
7709 : 9410 : if (!is_constant_array_expr (source)
7710 : 7034 : || !is_constant_array_expr (shape_exp)
7711 : 5938 : || !is_constant_array_expr (pad)
7712 : 15348 : || !is_constant_array_expr (order_exp))
7713 : 3484 : return NULL;
7714 : :
7715 : 5926 : if (source->shape == NULL)
7716 : : return NULL;
7717 : :
7718 : : /* Proceed with simplification, unpacking the array. */
7719 : :
7720 : 5923 : mpz_init (index);
7721 : 5923 : rank = 0;
7722 : :
7723 : 100691 : for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
7724 : 88845 : x[i] = 0;
7725 : :
7726 : 33387 : for (;;)
7727 : : {
7728 : 19655 : e = gfc_constructor_lookup_expr (shape_exp->value.constructor, rank);
7729 : 19655 : if (e == NULL)
7730 : : break;
7731 : :
7732 : 13732 : gfc_extract_int (e, &shape[rank]);
7733 : :
7734 : 13732 : gcc_assert (rank >= 0 && rank < GFC_MAX_DIMENSIONS);
7735 : 13732 : if (shape[rank] < 0)
7736 : : {
7737 : 0 : gfc_error ("The SHAPE array for the RESHAPE intrinsic at %L has a "
7738 : : "negative value %d for dimension %d",
7739 : : &shape_exp->where, shape[rank], rank+1);
7740 : 0 : mpz_clear (index);
7741 : 0 : return &gfc_bad_expr;
7742 : : }
7743 : :
7744 : 13732 : rank++;
7745 : : }
7746 : :
7747 : 5923 : gcc_assert (rank > 0);
7748 : :
7749 : : /* Now unpack the order array if present. */
7750 : 5923 : if (order_exp == NULL)
7751 : : {
7752 : 19589 : for (i = 0; i < rank; i++)
7753 : 13688 : order[i] = i;
7754 : : }
7755 : : else
7756 : : {
7757 : 22 : mpz_t size;
7758 : 22 : int order_size, shape_size;
7759 : :
7760 : 22 : if (order_exp->rank != shape_exp->rank)
7761 : : {
7762 : 1 : gfc_error ("Shapes of ORDER at %L and SHAPE at %L are different",
7763 : : &order_exp->where, &shape_exp->where);
7764 : 1 : mpz_clear (index);
7765 : 4 : return &gfc_bad_expr;
7766 : : }
7767 : :
7768 : 21 : gfc_array_size (shape_exp, &size);
7769 : 21 : shape_size = mpz_get_ui (size);
7770 : 21 : mpz_clear (size);
7771 : 21 : gfc_array_size (order_exp, &size);
7772 : 21 : order_size = mpz_get_ui (size);
7773 : 21 : mpz_clear (size);
7774 : 21 : if (order_size != shape_size)
7775 : : {
7776 : 1 : gfc_error ("Sizes of ORDER at %L and SHAPE at %L are different",
7777 : : &order_exp->where, &shape_exp->where);
7778 : 1 : mpz_clear (index);
7779 : 1 : return &gfc_bad_expr;
7780 : : }
7781 : :
7782 : 58 : for (i = 0; i < rank; i++)
7783 : : {
7784 : 40 : e = gfc_constructor_lookup_expr (order_exp->value.constructor, i);
7785 : 40 : gcc_assert (e);
7786 : :
7787 : 40 : gfc_extract_int (e, &order[i]);
7788 : :
7789 : 40 : if (order[i] < 1 || order[i] > rank)
7790 : : {
7791 : 1 : gfc_error ("Element with a value of %d in ORDER at %L must be "
7792 : : "in the range [1, ..., %d] for the RESHAPE intrinsic "
7793 : : "near %L", order[i], &order_exp->where, rank,
7794 : : &shape_exp->where);
7795 : 1 : mpz_clear (index);
7796 : 1 : return &gfc_bad_expr;
7797 : : }
7798 : :
7799 : 39 : order[i]--;
7800 : 39 : if (x[order[i]] != 0)
7801 : : {
7802 : 1 : gfc_error ("ORDER at %L is not a permutation of the size of "
7803 : : "SHAPE at %L", &order_exp->where, &shape_exp->where);
7804 : 1 : mpz_clear (index);
7805 : 1 : return &gfc_bad_expr;
7806 : : }
7807 : 38 : x[order[i]] = 1;
7808 : : }
7809 : : }
7810 : :
7811 : : /* Count the elements in the source and padding arrays. */
7812 : :
7813 : 5919 : npad = 0;
7814 : 5919 : if (pad != NULL)
7815 : : {
7816 : 56 : gfc_array_size (pad, &size);
7817 : 56 : npad = mpz_get_ui (size);
7818 : 56 : mpz_clear (size);
7819 : : }
7820 : :
7821 : 5919 : gfc_array_size (source, &size);
7822 : 5919 : nsource = mpz_get_ui (size);
7823 : 5919 : mpz_clear (size);
7824 : :
7825 : : /* If it weren't for that pesky permutation we could just loop
7826 : : through the source and round out any shortage with pad elements.
7827 : : But no, someone just had to have the compiler do something the
7828 : : user should be doing. */
7829 : :
7830 : 25562 : for (i = 0; i < rank; i++)
7831 : 13724 : x[i] = 0;
7832 : :
7833 : 5919 : result = gfc_get_array_expr (source->ts.type, source->ts.kind,
7834 : : &source->where);
7835 : 5919 : if (source->ts.type == BT_DERIVED)
7836 : 116 : result->ts.u.derived = source->ts.u.derived;
7837 : 5919 : if (source->ts.type == BT_CHARACTER && result->ts.u.cl == NULL)
7838 : 224 : result->ts = source->ts;
7839 : 5919 : result->rank = rank;
7840 : 5919 : result->shape = gfc_get_shape (rank);
7841 : 19643 : for (i = 0; i < rank; i++)
7842 : : {
7843 : 13724 : mpz_init_set_ui (result->shape[i], shape[i]);
7844 : 13724 : if (shape[i] == 0)
7845 : 723 : zerosize = true;
7846 : : }
7847 : :
7848 : 5919 : if (zerosize)
7849 : 699 : goto sizezero;
7850 : :
7851 : 94617 : while (nsource > 0 || npad > 0)
7852 : : {
7853 : : /* Figure out which element to extract. */
7854 : 94617 : mpz_set_ui (index, 0);
7855 : :
7856 : 326309 : for (i = rank - 1; i >= 0; i--)
7857 : : {
7858 : 231692 : mpz_add_ui (index, index, x[order[i]]);
7859 : 231692 : if (i != 0)
7860 : 137075 : mpz_mul_ui (index, index, shape[order[i - 1]]);
7861 : : }
7862 : :
7863 : 94617 : if (mpz_cmp_ui (index, INT_MAX) > 0)
7864 : 0 : gfc_internal_error ("Reshaped array too large at %C");
7865 : :
7866 : 94617 : j = mpz_get_ui (index);
7867 : :
7868 : 94617 : if (j < nsource)
7869 : 94429 : e = gfc_constructor_lookup_expr (source->value.constructor, j);
7870 : : else
7871 : : {
7872 : 188 : if (npad <= 0)
7873 : : {
7874 : 16 : mpz_clear (index);
7875 : 16 : if (pad == NULL)
7876 : 16 : gfc_error ("Without padding, there are not enough elements "
7877 : : "in the intrinsic RESHAPE source at %L to match "
7878 : : "the shape", &source->where);
7879 : 16 : gfc_free_expr (result);
7880 : 16 : return NULL;
7881 : : }
7882 : 172 : j = j - nsource;
7883 : 172 : j = j % npad;
7884 : 172 : e = gfc_constructor_lookup_expr (pad->value.constructor, j);
7885 : : }
7886 : 94601 : gcc_assert (e);
7887 : :
7888 : 94601 : gfc_constructor_append_expr (&result->value.constructor,
7889 : : gfc_copy_expr (e), &e->where);
7890 : :
7891 : : /* Calculate the next element. */
7892 : 94601 : i = 0;
7893 : :
7894 : 124270 : inc:
7895 : 124270 : if (++x[i] < shape[i])
7896 : 89397 : continue;
7897 : 34873 : x[i++] = 0;
7898 : 34873 : if (i < rank)
7899 : 29669 : goto inc;
7900 : :
7901 : : break;
7902 : : }
7903 : :
7904 : 0 : sizezero:
7905 : :
7906 : 5903 : mpz_clear (index);
7907 : :
7908 : 5903 : return result;
7909 : : }
7910 : :
7911 : :
7912 : : gfc_expr *
7913 : 192 : gfc_simplify_rrspacing (gfc_expr *x)
7914 : : {
7915 : 192 : gfc_expr *result;
7916 : 192 : int i;
7917 : 192 : long int e, p;
7918 : :
7919 : 192 : if (x->expr_type != EXPR_CONSTANT)
7920 : : return NULL;
7921 : :
7922 : 60 : i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
7923 : :
7924 : 60 : result = gfc_get_constant_expr (BT_REAL, x->ts.kind, &x->where);
7925 : :
7926 : : /* RRSPACING(+/- 0.0) = 0.0 */
7927 : 60 : if (mpfr_zero_p (x->value.real))
7928 : : {
7929 : 12 : mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
7930 : 12 : return result;
7931 : : }
7932 : :
7933 : : /* RRSPACING(inf) = NaN */
7934 : 48 : if (mpfr_inf_p (x->value.real))
7935 : : {
7936 : 12 : mpfr_set_nan (result->value.real);
7937 : 12 : return result;
7938 : : }
7939 : :
7940 : : /* RRSPACING(NaN) = same NaN */
7941 : 36 : if (mpfr_nan_p (x->value.real))
7942 : : {
7943 : 6 : mpfr_set (result->value.real, x->value.real, GFC_RND_MODE);
7944 : 6 : return result;
7945 : : }
7946 : :
7947 : : /* | x * 2**(-e) | * 2**p. */
7948 : 30 : mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE);
7949 : 30 : e = - (long int) mpfr_get_exp (x->value.real);
7950 : 30 : mpfr_mul_2si (result->value.real, result->value.real, e, GFC_RND_MODE);
7951 : :
7952 : 30 : p = (long int) gfc_real_kinds[i].digits;
7953 : 30 : mpfr_mul_2si (result->value.real, result->value.real, p, GFC_RND_MODE);
7954 : :
7955 : 30 : return range_check (result, "RRSPACING");
7956 : : }
7957 : :
7958 : :
7959 : : gfc_expr *
7960 : 168 : gfc_simplify_scale (gfc_expr *x, gfc_expr *i)
7961 : : {
7962 : 168 : int k, neg_flag, power, exp_range;
7963 : 168 : mpfr_t scale, radix;
7964 : 168 : gfc_expr *result;
7965 : :
7966 : 168 : if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
7967 : : return NULL;
7968 : :
7969 : 12 : result = gfc_get_constant_expr (BT_REAL, x->ts.kind, &x->where);
7970 : :
7971 : 12 : if (mpfr_zero_p (x->value.real))
7972 : : {
7973 : 0 : mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
7974 : 0 : return result;
7975 : : }
7976 : :
7977 : 12 : k = gfc_validate_kind (BT_REAL, x->ts.kind, false);
7978 : :
7979 : 12 : exp_range = gfc_real_kinds[k].max_exponent - gfc_real_kinds[k].min_exponent;
7980 : :
7981 : : /* This check filters out values of i that would overflow an int. */
7982 : 12 : if (mpz_cmp_si (i->value.integer, exp_range + 2) > 0
7983 : 12 : || mpz_cmp_si (i->value.integer, -exp_range - 2) < 0)
7984 : : {
7985 : 0 : gfc_error ("Result of SCALE overflows its kind at %L", &result->where);
7986 : 0 : gfc_free_expr (result);
7987 : 0 : return &gfc_bad_expr;
7988 : : }
7989 : :
7990 : : /* Compute scale = radix ** power. */
7991 : 12 : power = mpz_get_si (i->value.integer);
7992 : :
7993 : 12 : if (power >= 0)
7994 : : neg_flag = 0;
7995 : : else
7996 : : {
7997 : 0 : neg_flag = 1;
7998 : 0 : power = -power;
7999 : : }
8000 : :
8001 : 12 : gfc_set_model_kind (x->ts.kind);
8002 : 12 : mpfr_init (scale);
8003 : 12 : mpfr_init (radix);
8004 : 12 : mpfr_set_ui (radix, gfc_real_kinds[k].radix, GFC_RND_MODE);
8005 : 12 : mpfr_pow_ui (scale, radix, power, GFC_RND_MODE);
8006 : :
8007 : 12 : if (neg_flag)
8008 : 0 : mpfr_div (result->value.real, x->value.real, scale, GFC_RND_MODE);
8009 : : else
8010 : 12 : mpfr_mul (result->value.real, x->value.real, scale, GFC_RND_MODE);
8011 : :
8012 : 12 : mpfr_clears (scale, radix, NULL);
8013 : :
8014 : 12 : return range_check (result, "SCALE");
8015 : : }
8016 : :
8017 : :
8018 : : /* Variants of strspn and strcspn that operate on wide characters. */
8019 : :
8020 : : static size_t
8021 : 60 : wide_strspn (const gfc_char_t *s1, const gfc_char_t *s2)
8022 : : {
8023 : 60 : size_t i = 0;
8024 : 60 : const gfc_char_t *c;
8025 : :
8026 : 144 : while (s1[i])
8027 : : {
8028 : 354 : for (c = s2; *c; c++)
8029 : : {
8030 : 294 : if (s1[i] == *c)
8031 : : break;
8032 : : }
8033 : 144 : if (*c == '\0')
8034 : : break;
8035 : 84 : i++;
8036 : : }
8037 : :
8038 : 60 : return i;
8039 : : }
8040 : :
8041 : : static size_t
8042 : 60 : wide_strcspn (const gfc_char_t *s1, const gfc_char_t *s2)
8043 : : {
8044 : 60 : size_t i = 0;
8045 : 60 : const gfc_char_t *c;
8046 : :
8047 : 396 : while (s1[i])
8048 : : {
8049 : 1392 : for (c = s2; *c; c++)
8050 : : {
8051 : 1056 : if (s1[i] == *c)
8052 : : break;
8053 : : }
8054 : 384 : if (*c)
8055 : : break;
8056 : 336 : i++;
8057 : : }
8058 : :
8059 : 60 : return i;
8060 : : }
8061 : :
8062 : :
8063 : : gfc_expr *
8064 : 958 : gfc_simplify_scan (gfc_expr *e, gfc_expr *c, gfc_expr *b, gfc_expr *kind)
8065 : : {
8066 : 958 : gfc_expr *result;
8067 : 958 : int back;
8068 : 958 : size_t i;
8069 : 958 : size_t indx, len, lenc;
8070 : 958 : int k = get_kind (BT_INTEGER, kind, "SCAN", gfc_default_integer_kind);
8071 : :
8072 : 958 : if (k == -1)
8073 : : return &gfc_bad_expr;
8074 : :
8075 : 958 : if (e->expr_type != EXPR_CONSTANT || c->expr_type != EXPR_CONSTANT
8076 : 182 : || ( b != NULL && b->expr_type != EXPR_CONSTANT))
8077 : : return NULL;
8078 : :
8079 : 144 : if (b != NULL && b->value.logical != 0)
8080 : : back = 1;
8081 : : else
8082 : 72 : back = 0;
8083 : :
8084 : 144 : len = e->value.character.length;
8085 : 144 : lenc = c->value.character.length;
8086 : :
8087 : 144 : if (len == 0 || lenc == 0)
8088 : : {
8089 : : indx = 0;
8090 : : }
8091 : : else
8092 : : {
8093 : 120 : if (back == 0)
8094 : : {
8095 : 60 : indx = wide_strcspn (e->value.character.string,
8096 : 60 : c->value.character.string) + 1;
8097 : 60 : if (indx > len)
8098 : 48 : indx = 0;
8099 : : }
8100 : : else
8101 : 408 : for (indx = len; indx > 0; indx--)
8102 : : {
8103 : 1488 : for (i = 0; i < lenc; i++)
8104 : : {
8105 : 1140 : if (c->value.character.string[i]
8106 : 1140 : == e->value.character.string[indx - 1])
8107 : : break;
8108 : : }
8109 : 396 : if (i < lenc)
8110 : : break;
8111 : : }
8112 : : }
8113 : :
8114 : 144 : result = gfc_get_int_expr (k, &e->where, indx);
8115 : 144 : return range_check (result, "SCAN");
8116 : : }
8117 : :
8118 : :
8119 : : gfc_expr *
8120 : 258 : gfc_simplify_selected_char_kind (gfc_expr *e)
8121 : : {
8122 : 258 : int kind;
8123 : :
8124 : 258 : if (e->expr_type != EXPR_CONSTANT)
8125 : : return NULL;
8126 : :
8127 : 173 : if (gfc_compare_with_Cstring (e, "ascii", false) == 0
8128 : 173 : || gfc_compare_with_Cstring (e, "default", false) == 0)
8129 : : kind = 1;
8130 : 89 : else if (gfc_compare_with_Cstring (e, "iso_10646", false) == 0)
8131 : : kind = 4;
8132 : : else
8133 : 39 : kind = -1;
8134 : :
8135 : 173 : return gfc_get_int_expr (gfc_default_integer_kind, &e->where, kind);
8136 : : }
8137 : :
8138 : :
8139 : : gfc_expr *
8140 : 255 : gfc_simplify_selected_int_kind (gfc_expr *e)
8141 : : {
8142 : 255 : int i, kind, range;
8143 : :
8144 : 255 : if (e->expr_type != EXPR_CONSTANT || gfc_extract_int (e, &range))
8145 : 49 : return NULL;
8146 : :
8147 : : kind = INT_MAX;
8148 : :
8149 : 1236 : for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
8150 : 1030 : if (gfc_integer_kinds[i].range >= range
8151 : 529 : && gfc_integer_kinds[i].kind < kind)
8152 : 1030 : kind = gfc_integer_kinds[i].kind;
8153 : :
8154 : 206 : if (kind == INT_MAX)
8155 : 0 : kind = -1;
8156 : :
8157 : 206 : return gfc_get_int_expr (gfc_default_integer_kind, &e->where, kind);
8158 : : }
8159 : :
8160 : : /* Same as above, but with unsigneds. */
8161 : :
8162 : : gfc_expr *
8163 : 25 : gfc_simplify_selected_unsigned_kind (gfc_expr *e)
8164 : : {
8165 : 25 : int i, kind, range;
8166 : :
8167 : 25 : if (e->expr_type != EXPR_CONSTANT || gfc_extract_int (e, &range))
8168 : 0 : return NULL;
8169 : :
8170 : : kind = INT_MAX;
8171 : :
8172 : 150 : for (i = 0; gfc_unsigned_kinds[i].kind != 0; i++)
8173 : 125 : if (gfc_unsigned_kinds[i].range >= range
8174 : 86 : && gfc_unsigned_kinds[i].kind < kind)
8175 : 125 : kind = gfc_unsigned_kinds[i].kind;
8176 : :
8177 : 25 : if (kind == INT_MAX)
8178 : 0 : kind = -1;
8179 : :
8180 : 25 : return gfc_get_int_expr (gfc_default_integer_kind, &e->where, kind);
8181 : : }
8182 : :
8183 : :
8184 : : gfc_expr *
8185 : 78 : gfc_simplify_selected_logical_kind (gfc_expr *e)
8186 : : {
8187 : 78 : int i, kind, bits;
8188 : :
8189 : 78 : if (e->expr_type != EXPR_CONSTANT || gfc_extract_int (e, &bits))
8190 : 12 : return NULL;
8191 : :
8192 : : kind = INT_MAX;
8193 : :
8194 : 396 : for (i = 0; gfc_logical_kinds[i].kind != 0; i++)
8195 : 330 : if (gfc_logical_kinds[i].bit_size >= bits
8196 : 180 : && gfc_logical_kinds[i].kind < kind)
8197 : 330 : kind = gfc_logical_kinds[i].kind;
8198 : :
8199 : 66 : if (kind == INT_MAX)
8200 : 6 : kind = -1;
8201 : :
8202 : 66 : return gfc_get_int_expr (gfc_default_integer_kind, &e->where, kind);
8203 : : }
8204 : :
8205 : :
8206 : : gfc_expr *
8207 : 982 : gfc_simplify_selected_real_kind (gfc_expr *p, gfc_expr *q, gfc_expr *rdx)
8208 : : {
8209 : 982 : int range, precision, radix, i, kind, found_precision, found_range,
8210 : : found_radix;
8211 : 982 : locus *loc = &gfc_current_locus;
8212 : :
8213 : 982 : if (p == NULL)
8214 : 60 : precision = 0;
8215 : : else
8216 : : {
8217 : 922 : if (p->expr_type != EXPR_CONSTANT
8218 : 922 : || gfc_extract_int (p, &precision))
8219 : 46 : return NULL;
8220 : 876 : loc = &p->where;
8221 : : }
8222 : :
8223 : 936 : if (q == NULL)
8224 : 676 : range = 0;
8225 : : else
8226 : : {
8227 : 260 : if (q->expr_type != EXPR_CONSTANT
8228 : 260 : || gfc_extract_int (q, &range))
8229 : 54 : return NULL;
8230 : :
8231 : : if (!loc)
8232 : : loc = &q->where;
8233 : : }
8234 : :
8235 : 882 : if (rdx == NULL)
8236 : 822 : radix = 0;
8237 : : else
8238 : : {
8239 : 60 : if (rdx->expr_type != EXPR_CONSTANT
8240 : 60 : || gfc_extract_int (rdx, &radix))
8241 : 24 : return NULL;
8242 : :
8243 : : if (!loc)
8244 : : loc = &rdx->where;
8245 : : }
8246 : :
8247 : 858 : kind = INT_MAX;
8248 : 858 : found_precision = 0;
8249 : 858 : found_range = 0;
8250 : 858 : found_radix = 0;
8251 : :
8252 : 4290 : for (i = 0; gfc_real_kinds[i].kind != 0; i++)
8253 : : {
8254 : 3432 : if (gfc_real_kinds[i].precision >= precision)
8255 : 2322 : found_precision = 1;
8256 : :
8257 : 3432 : if (gfc_real_kinds[i].range >= range)
8258 : 3315 : found_range = 1;
8259 : :
8260 : 3432 : if (radix == 0 || gfc_real_kinds[i].radix == radix)
8261 : 3408 : found_radix = 1;
8262 : :
8263 : 3432 : if (gfc_real_kinds[i].precision >= precision
8264 : 2322 : && gfc_real_kinds[i].range >= range
8265 : 2322 : && (radix == 0 || gfc_real_kinds[i].radix == radix)
8266 : 2298 : && gfc_real_kinds[i].kind < kind)
8267 : 3432 : kind = gfc_real_kinds[i].kind;
8268 : : }
8269 : :
8270 : 858 : if (kind == INT_MAX)
8271 : : {
8272 : 12 : if (found_radix && found_range && !found_precision)
8273 : : kind = -1;
8274 : 6 : else if (found_radix && found_precision && !found_range)
8275 : : kind = -2;
8276 : 6 : else if (found_radix && !found_precision && !found_range)
8277 : : kind = -3;
8278 : 6 : else if (found_radix)
8279 : : kind = -4;
8280 : : else
8281 : 6 : kind = -5;
8282 : : }
8283 : :
8284 : 858 : return gfc_get_int_expr (gfc_default_integer_kind, loc, kind);
8285 : : }
8286 : :
8287 : :
8288 : : gfc_expr *
8289 : 770 : gfc_simplify_set_exponent (gfc_expr *x, gfc_expr *i)
8290 : : {
8291 : 770 : gfc_expr *result;
8292 : 770 : mpfr_t exp, absv, log2, pow2, frac;
8293 : 770 : long exp2;
8294 : :
8295 : 770 : if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
8296 : : return NULL;
8297 : :
8298 : 150 : result = gfc_get_constant_expr (BT_REAL, x->ts.kind, &x->where);
8299 : :
8300 : : /* SET_EXPONENT (+/-0.0, I) = +/- 0.0
8301 : : SET_EXPONENT (NaN) = same NaN */
8302 : 150 : if (mpfr_zero_p (x->value.real) || mpfr_nan_p (x->value.real))
8303 : : {
8304 : 18 : mpfr_set (result->value.real, x->value.real, GFC_RND_MODE);
8305 : 18 : return result;
8306 : : }
8307 : :
8308 : : /* SET_EXPONENT (inf) = NaN */
8309 : 132 : if (mpfr_inf_p (x->value.real))
8310 : : {
8311 : 12 : mpfr_set_nan (result->value.real);
8312 : 12 : return result;
8313 : : }
8314 : :
8315 : 120 : gfc_set_model_kind (x->ts.kind);
8316 : 120 : mpfr_init (absv);
8317 : 120 : mpfr_init (log2);
8318 : 120 : mpfr_init (exp);
8319 : 120 : mpfr_init (pow2);
8320 : 120 : mpfr_init (frac);
8321 : :
8322 : 120 : mpfr_abs (absv, x->value.real, GFC_RND_MODE);
8323 : 120 : mpfr_log2 (log2, absv, GFC_RND_MODE);
8324 : :
8325 : 120 : mpfr_floor (log2, log2);
8326 : 120 : mpfr_add_ui (exp, log2, 1, GFC_RND_MODE);
8327 : :
8328 : : /* Old exponent value, and fraction. */
8329 : 120 : mpfr_ui_pow (pow2, 2, exp, GFC_RND_MODE);
8330 : :
8331 : 120 : mpfr_div (frac, x->value.real, pow2, GFC_RND_MODE);
8332 : :
8333 : : /* New exponent. */
8334 : 120 : exp2 = mpz_get_si (i->value.integer);
8335 : 120 : mpfr_mul_2si (result->value.real, frac, exp2, GFC_RND_MODE);
8336 : :
8337 : 120 : mpfr_clears (absv, log2, exp, pow2, frac, NULL);
8338 : :
8339 : 120 : return range_check (result, "SET_EXPONENT");
8340 : : }
8341 : :
8342 : :
8343 : : gfc_expr *
8344 : 11820 : gfc_simplify_shape (gfc_expr *source, gfc_expr *kind)
8345 : : {
8346 : 11820 : mpz_t shape[GFC_MAX_DIMENSIONS];
8347 : 11820 : gfc_expr *result, *e, *f;
8348 : 11820 : gfc_array_ref *ar;
8349 : 11820 : int n;
8350 : 11820 : bool t;
8351 : 11820 : int k = get_kind (BT_INTEGER, kind, "SHAPE", gfc_default_integer_kind);
8352 : :
8353 : 11820 : if (source->rank == -1)
8354 : : return NULL;
8355 : :
8356 : 10932 : result = gfc_get_array_expr (BT_INTEGER, k, &source->where);
8357 : 10932 : result->shape = gfc_get_shape (1);
8358 : 10932 : mpz_init (result->shape[0]);
8359 : :
8360 : 10932 : if (source->rank == 0)
8361 : : return result;
8362 : :
8363 : 10881 : if (source->expr_type == EXPR_VARIABLE)
8364 : : {
8365 : 10837 : ar = gfc_find_array_ref (source);
8366 : 10837 : t = gfc_array_ref_shape (ar, shape);
8367 : : }
8368 : 44 : else if (source->shape)
8369 : : {
8370 : 37 : t = true;
8371 : 37 : for (n = 0; n < source->rank; n++)
8372 : : {
8373 : 24 : mpz_init (shape[n]);
8374 : 24 : mpz_set (shape[n], source->shape[n]);
8375 : : }
8376 : : }
8377 : : else
8378 : : t = false;
8379 : :
8380 : 17433 : for (n = 0; n < source->rank; n++)
8381 : : {
8382 : 15108 : e = gfc_get_constant_expr (BT_INTEGER, k, &source->where);
8383 : :
8384 : 15108 : if (t)
8385 : 6538 : mpz_set (e->value.integer, shape[n]);
8386 : : else
8387 : : {
8388 : 8570 : mpz_set_ui (e->value.integer, n + 1);
8389 : :
8390 : 8570 : f = simplify_size (source, e, k);
8391 : 8570 : gfc_free_expr (e);
8392 : 8570 : if (f == NULL)
8393 : : {
8394 : 8555 : gfc_free_expr (result);
8395 : 8555 : return NULL;
8396 : : }
8397 : : else
8398 : : e = f;
8399 : : }
8400 : :
8401 : 6553 : if (e == &gfc_bad_expr || range_check (e, "SHAPE") == &gfc_bad_expr)
8402 : : {
8403 : 1 : gfc_free_expr (result);
8404 : 1 : if (t)
8405 : 1 : gfc_clear_shape (shape, source->rank);
8406 : 1 : return &gfc_bad_expr;
8407 : : }
8408 : :
8409 : 6552 : gfc_constructor_append_expr (&result->value.constructor, e, NULL);
8410 : : }
8411 : :
8412 : 2325 : if (t)
8413 : 2325 : gfc_clear_shape (shape, source->rank);
8414 : :
8415 : 2325 : mpz_set_si (result->shape[0], source->rank);
8416 : :
8417 : 2325 : return result;
8418 : : }
8419 : :
8420 : :
8421 : : static gfc_expr *
8422 : 40968 : simplify_size (gfc_expr *array, gfc_expr *dim, int k)
8423 : : {
8424 : 40968 : mpz_t size;
8425 : 40968 : gfc_expr *return_value;
8426 : 40968 : int d;
8427 : 40968 : gfc_ref *ref;
8428 : :
8429 : : /* For unary operations, the size of the result is given by the size
8430 : : of the operand. For binary ones, it's the size of the first operand
8431 : : unless it is scalar, then it is the size of the second. */
8432 : 40968 : if (array->expr_type == EXPR_OP && !array->value.op.uop)
8433 : : {
8434 : 44 : gfc_expr* replacement;
8435 : 44 : gfc_expr* simplified;
8436 : :
8437 : 44 : switch (array->value.op.op)
8438 : : {
8439 : : /* Unary operations. */
8440 : 7 : case INTRINSIC_NOT:
8441 : 7 : case INTRINSIC_UPLUS:
8442 : 7 : case INTRINSIC_UMINUS:
8443 : 7 : case INTRINSIC_PARENTHESES:
8444 : 7 : replacement = array->value.op.op1;
8445 : 7 : break;
8446 : :
8447 : : /* Binary operations. If any one of the operands is scalar, take
8448 : : the other one's size. If both of them are arrays, it does not
8449 : : matter -- try to find one with known shape, if possible. */
8450 : 37 : default:
8451 : 37 : if (array->value.op.op1->rank == 0)
8452 : 25 : replacement = array->value.op.op2;
8453 : 12 : else if (array->value.op.op2->rank == 0)
8454 : : replacement = array->value.op.op1;
8455 : : else
8456 : : {
8457 : 0 : simplified = simplify_size (array->value.op.op1, dim, k);
8458 : 0 : if (simplified)
8459 : : return simplified;
8460 : :
8461 : 0 : replacement = array->value.op.op2;
8462 : : }
8463 : : break;
8464 : : }
8465 : :
8466 : : /* Try to reduce it directly if possible. */
8467 : 44 : simplified = simplify_size (replacement, dim, k);
8468 : :
8469 : : /* Otherwise, we build a new SIZE call. This is hopefully at least
8470 : : simpler than the original one. */
8471 : 44 : if (!simplified)
8472 : : {
8473 : 20 : gfc_expr *kind = gfc_get_int_expr (gfc_default_integer_kind, NULL, k);
8474 : 20 : simplified = gfc_build_intrinsic_call (gfc_current_ns,
8475 : : GFC_ISYM_SIZE, "size",
8476 : : array->where, 3,
8477 : : gfc_copy_expr (replacement),
8478 : : gfc_copy_expr (dim),
8479 : : kind);
8480 : : }
8481 : 44 : return simplified;
8482 : : }
8483 : :
8484 : 82857 : for (ref = array->ref; ref; ref = ref->next)
8485 : 39108 : if (ref->type == REF_ARRAY && ref->u.ar.as
8486 : 81045 : && !gfc_resolve_array_spec (ref->u.ar.as, 0))
8487 : : return NULL;
8488 : :
8489 : 40920 : if (dim == NULL)
8490 : : {
8491 : 15612 : if (!gfc_array_size (array, &size))
8492 : : return NULL;
8493 : : }
8494 : : else
8495 : : {
8496 : 25308 : if (dim->expr_type != EXPR_CONSTANT)
8497 : : return NULL;
8498 : :
8499 : 24974 : if (array->rank == -1)
8500 : : return NULL;
8501 : :
8502 : 24332 : d = mpz_get_si (dim->value.integer) - 1;
8503 : 24332 : if (d < 0 || d > array->rank - 1)
8504 : : {
8505 : 6 : gfc_error ("DIM argument (%d) to intrinsic SIZE at %L out of range "
8506 : : "(1:%d)", d+1, &array->where, array->rank);
8507 : 6 : return &gfc_bad_expr;
8508 : : }
8509 : :
8510 : 24326 : if (!gfc_array_dimen_size (array, d, &size))
8511 : : return NULL;
8512 : : }
8513 : :
8514 : 4802 : return_value = gfc_get_constant_expr (BT_INTEGER, k, &array->where);
8515 : 4802 : mpz_set (return_value->value.integer, size);
8516 : 4802 : mpz_clear (size);
8517 : :
8518 : 4802 : return return_value;
8519 : : }
8520 : :
8521 : :
8522 : : gfc_expr *
8523 : 31572 : gfc_simplify_size (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
8524 : : {
8525 : 31572 : gfc_expr *result;
8526 : 31572 : int k = get_kind (BT_INTEGER, kind, "SIZE", gfc_default_integer_kind);
8527 : :
8528 : 31572 : if (k == -1)
8529 : : return &gfc_bad_expr;
8530 : :
8531 : 31572 : result = simplify_size (array, dim, k);
8532 : 31572 : if (result == NULL || result == &gfc_bad_expr)
8533 : : return result;
8534 : :
8535 : 4400 : return range_check (result, "SIZE");
8536 : : }
8537 : :
8538 : :
8539 : : /* SIZEOF and C_SIZEOF return the size in bytes of an array element
8540 : : multiplied by the array size. */
8541 : :
8542 : : gfc_expr *
8543 : 3355 : gfc_simplify_sizeof (gfc_expr *x)
8544 : : {
8545 : 3355 : gfc_expr *result = NULL;
8546 : 3355 : mpz_t array_size;
8547 : 3355 : size_t res_size;
8548 : :
8549 : 3355 : if (x->ts.type == BT_CLASS || x->ts.deferred)
8550 : : return NULL;
8551 : :
8552 : 2283 : if (x->ts.type == BT_CHARACTER
8553 : 249 : && (!x->ts.u.cl || !x->ts.u.cl->length
8554 : 75 : || x->ts.u.cl->length->expr_type != EXPR_CONSTANT))
8555 : : return NULL;
8556 : :
8557 : 2091 : if (x->rank && x->expr_type != EXPR_ARRAY)
8558 : : {
8559 : 1380 : if (!gfc_array_size (x, &array_size))
8560 : : return NULL;
8561 : :
8562 : 168 : mpz_clear (array_size);
8563 : : }
8564 : :
8565 : 879 : result = gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
8566 : : &x->where);
8567 : 879 : gfc_target_expr_size (x, &res_size);
8568 : 879 : mpz_set_si (result->value.integer, res_size);
8569 : :
8570 : 879 : return result;
8571 : : }
8572 : :
8573 : :
8574 : : /* STORAGE_SIZE returns the size in bits of a single array element. */
8575 : :
8576 : : gfc_expr *
8577 : 1308 : gfc_simplify_storage_size (gfc_expr *x,
8578 : : gfc_expr *kind)
8579 : : {
8580 : 1308 : gfc_expr *result = NULL;
8581 : 1308 : int k;
8582 : 1308 : size_t siz;
8583 : :
8584 : 1308 : if (x->ts.type == BT_CLASS || x->ts.deferred)
8585 : : return NULL;
8586 : :
8587 : 761 : if (x->ts.type == BT_CHARACTER && x->expr_type != EXPR_CONSTANT
8588 : 297 : && (!x->ts.u.cl || !x->ts.u.cl->length
8589 : 96 : || x->ts.u.cl->length->expr_type != EXPR_CONSTANT))
8590 : : return NULL;
8591 : :
8592 : 560 : k = get_kind (BT_INTEGER, kind, "STORAGE_SIZE", gfc_default_integer_kind);
8593 : 560 : if (k == -1)
8594 : : return &gfc_bad_expr;
8595 : :
8596 : 560 : result = gfc_get_constant_expr (BT_INTEGER, k, &x->where);
8597 : :
8598 : 560 : gfc_element_size (x, &siz);
8599 : 560 : mpz_set_si (result->value.integer, siz);
8600 : 560 : mpz_mul_ui (result->value.integer, result->value.integer, BITS_PER_UNIT);
8601 : :
8602 : 560 : return range_check (result, "STORAGE_SIZE");
8603 : : }
8604 : :
8605 : :
8606 : : gfc_expr *
8607 : 1370 : gfc_simplify_sign (gfc_expr *x, gfc_expr *y)
8608 : : {
8609 : 1370 : gfc_expr *result;
8610 : :
8611 : 1370 : if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
8612 : : return NULL;
8613 : :
8614 : 95 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
8615 : :
8616 : 95 : switch (x->ts.type)
8617 : : {
8618 : 22 : case BT_INTEGER:
8619 : 22 : mpz_abs (result->value.integer, x->value.integer);
8620 : 22 : if (mpz_sgn (y->value.integer) < 0)
8621 : 0 : mpz_neg (result->value.integer, result->value.integer);
8622 : : break;
8623 : :
8624 : 73 : case BT_REAL:
8625 : 73 : if (flag_sign_zero)
8626 : 61 : mpfr_copysign (result->value.real, x->value.real, y->value.real,
8627 : : GFC_RND_MODE);
8628 : : else
8629 : 24 : mpfr_setsign (result->value.real, x->value.real,
8630 : : mpfr_sgn (y->value.real) < 0 ? 1 : 0, GFC_RND_MODE);
8631 : : break;
8632 : :
8633 : 0 : default:
8634 : 0 : gfc_internal_error ("Bad type in gfc_simplify_sign");
8635 : : }
8636 : :
8637 : : return result;
8638 : : }
8639 : :
8640 : :
8641 : : gfc_expr *
8642 : 801 : gfc_simplify_sin (gfc_expr *x)
8643 : : {
8644 : 801 : gfc_expr *result;
8645 : :
8646 : 801 : if (x->expr_type != EXPR_CONSTANT)
8647 : : return NULL;
8648 : :
8649 : 163 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
8650 : :
8651 : 163 : switch (x->ts.type)
8652 : : {
8653 : 106 : case BT_REAL:
8654 : 106 : mpfr_sin (result->value.real, x->value.real, GFC_RND_MODE);
8655 : 106 : break;
8656 : :
8657 : 57 : case BT_COMPLEX:
8658 : 57 : gfc_set_model (x->value.real);
8659 : 57 : mpc_sin (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
8660 : 57 : break;
8661 : :
8662 : 0 : default:
8663 : 0 : gfc_internal_error ("in gfc_simplify_sin(): Bad type");
8664 : : }
8665 : :
8666 : 163 : return range_check (result, "SIN");
8667 : : }
8668 : :
8669 : :
8670 : : gfc_expr *
8671 : 316 : gfc_simplify_sinh (gfc_expr *x)
8672 : : {
8673 : 316 : gfc_expr *result;
8674 : :
8675 : 316 : if (x->expr_type != EXPR_CONSTANT)
8676 : : return NULL;
8677 : :
8678 : 46 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
8679 : :
8680 : 46 : switch (x->ts.type)
8681 : : {
8682 : 42 : case BT_REAL:
8683 : 42 : mpfr_sinh (result->value.real, x->value.real, GFC_RND_MODE);
8684 : 42 : break;
8685 : :
8686 : 4 : case BT_COMPLEX:
8687 : 4 : mpc_sinh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
8688 : 4 : break;
8689 : :
8690 : 0 : default:
8691 : 0 : gcc_unreachable ();
8692 : : }
8693 : :
8694 : 46 : return range_check (result, "SINH");
8695 : : }
8696 : :
8697 : :
8698 : : /* The argument is always a double precision real that is converted to
8699 : : single precision. TODO: Rounding! */
8700 : :
8701 : : gfc_expr *
8702 : 3 : gfc_simplify_sngl (gfc_expr *a)
8703 : : {
8704 : 3 : gfc_expr *result;
8705 : 3 : int tmp1, tmp2;
8706 : :
8707 : 3 : if (a->expr_type != EXPR_CONSTANT)
8708 : : return NULL;
8709 : :
8710 : : /* For explicit conversion, turn off -Wconversion and -Wconversion-extra
8711 : : warnings. */
8712 : 3 : tmp1 = warn_conversion;
8713 : 3 : tmp2 = warn_conversion_extra;
8714 : 3 : warn_conversion = warn_conversion_extra = 0;
8715 : :
8716 : 3 : result = gfc_real2real (a, gfc_default_real_kind);
8717 : :
8718 : 3 : warn_conversion = tmp1;
8719 : 3 : warn_conversion_extra = tmp2;
8720 : :
8721 : 3 : return range_check (result, "SNGL");
8722 : : }
8723 : :
8724 : :
8725 : : gfc_expr *
8726 : 309 : gfc_simplify_spacing (gfc_expr *x)
8727 : : {
8728 : 309 : gfc_expr *result;
8729 : 309 : int i;
8730 : 309 : long int en, ep;
8731 : :
8732 : 309 : if (x->expr_type != EXPR_CONSTANT)
8733 : : return NULL;
8734 : :
8735 : 96 : i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
8736 : 96 : result = gfc_get_constant_expr (BT_REAL, x->ts.kind, &x->where);
8737 : :
8738 : : /* SPACING(+/- 0.0) = SPACING(TINY(0.0)) = TINY(0.0) */
8739 : 96 : if (mpfr_zero_p (x->value.real))
8740 : : {
8741 : 12 : mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
8742 : 12 : return result;
8743 : : }
8744 : :
8745 : : /* SPACING(inf) = NaN */
8746 : 84 : if (mpfr_inf_p (x->value.real))
8747 : : {
8748 : 12 : mpfr_set_nan (result->value.real);
8749 : 12 : return result;
8750 : : }
8751 : :
8752 : : /* SPACING(NaN) = same NaN */
8753 : 72 : if (mpfr_nan_p (x->value.real))
8754 : : {
8755 : 6 : mpfr_set (result->value.real, x->value.real, GFC_RND_MODE);
8756 : 6 : return result;
8757 : : }
8758 : :
8759 : : /* In the Fortran 95 standard, the result is b**(e - p) where b, e, and p
8760 : : are the radix, exponent of x, and precision. This excludes the
8761 : : possibility of subnormal numbers. Fortran 2003 states the result is
8762 : : b**max(e - p, emin - 1). */
8763 : :
8764 : 66 : ep = (long int) mpfr_get_exp (x->value.real) - gfc_real_kinds[i].digits;
8765 : 66 : en = (long int) gfc_real_kinds[i].min_exponent - 1;
8766 : 66 : en = en > ep ? en : ep;
8767 : :
8768 : 66 : mpfr_set_ui (result->value.real, 1, GFC_RND_MODE);
8769 : 66 : mpfr_mul_2si (result->value.real, result->value.real, en, GFC_RND_MODE);
8770 : :
8771 : 66 : return range_check (result, "SPACING");
8772 : : }
8773 : :
8774 : :
8775 : : gfc_expr *
8776 : 840 : gfc_simplify_spread (gfc_expr *source, gfc_expr *dim_expr, gfc_expr *ncopies_expr)
8777 : : {
8778 : 840 : gfc_expr *result = NULL;
8779 : 840 : int nelem, i, j, dim, ncopies;
8780 : 840 : mpz_t size;
8781 : :
8782 : 840 : if ((!gfc_is_constant_expr (source)
8783 : 727 : && !is_constant_array_expr (source))
8784 : 132 : || !gfc_is_constant_expr (dim_expr)
8785 : 972 : || !gfc_is_constant_expr (ncopies_expr))
8786 : 708 : return NULL;
8787 : :
8788 : 132 : gcc_assert (dim_expr->ts.type == BT_INTEGER);
8789 : 132 : gfc_extract_int (dim_expr, &dim);
8790 : 132 : dim -= 1; /* zero-base DIM */
8791 : :
8792 : 132 : gcc_assert (ncopies_expr->ts.type == BT_INTEGER);
8793 : 132 : gfc_extract_int (ncopies_expr, &ncopies);
8794 : 132 : ncopies = MAX (ncopies, 0);
8795 : :
8796 : : /* Do not allow the array size to exceed the limit for an array
8797 : : constructor. */
8798 : 132 : if (source->expr_type == EXPR_ARRAY)
8799 : : {
8800 : 37 : if (!gfc_array_size (source, &size))
8801 : 0 : gfc_internal_error ("Failure getting length of a constant array.");
8802 : : }
8803 : : else
8804 : 95 : mpz_init_set_ui (size, 1);
8805 : :
8806 : 132 : nelem = mpz_get_si (size) * ncopies;
8807 : 132 : if (nelem > flag_max_array_constructor)
8808 : : {
8809 : 3 : if (gfc_init_expr_flag)
8810 : : {
8811 : 2 : gfc_error ("The number of elements (%d) in the array constructor "
8812 : : "at %L requires an increase of the allowed %d upper "
8813 : : "limit. See %<-fmax-array-constructor%> option.",
8814 : : nelem, &source->where, flag_max_array_constructor);
8815 : 2 : return &gfc_bad_expr;
8816 : : }
8817 : : else
8818 : : return NULL;
8819 : : }
8820 : :
8821 : 129 : if (source->expr_type == EXPR_CONSTANT
8822 : 40 : || source->expr_type == EXPR_STRUCTURE)
8823 : : {
8824 : 95 : gcc_assert (dim == 0);
8825 : :
8826 : 95 : result = gfc_get_array_expr (source->ts.type, source->ts.kind,
8827 : : &source->where);
8828 : 95 : if (source->ts.type == BT_DERIVED)
8829 : 6 : result->ts.u.derived = source->ts.u.derived;
8830 : 95 : result->rank = 1;
8831 : 95 : result->shape = gfc_get_shape (result->rank);
8832 : 95 : mpz_init_set_si (result->shape[0], ncopies);
8833 : :
8834 : 919 : for (i = 0; i < ncopies; ++i)
8835 : 729 : gfc_constructor_append_expr (&result->value.constructor,
8836 : : gfc_copy_expr (source), NULL);
8837 : : }
8838 : 34 : else if (source->expr_type == EXPR_ARRAY)
8839 : : {
8840 : 34 : int offset, rstride[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS];
8841 : 34 : gfc_constructor *source_ctor;
8842 : :
8843 : 34 : gcc_assert (source->rank < GFC_MAX_DIMENSIONS);
8844 : 34 : gcc_assert (dim >= 0 && dim <= source->rank);
8845 : :
8846 : 34 : result = gfc_get_array_expr (source->ts.type, source->ts.kind,
8847 : : &source->where);
8848 : 34 : if (source->ts.type == BT_DERIVED)
8849 : 1 : result->ts.u.derived = source->ts.u.derived;
8850 : 34 : result->rank = source->rank + 1;
8851 : 34 : result->shape = gfc_get_shape (result->rank);
8852 : :
8853 : 120 : for (i = 0, j = 0; i < result->rank; ++i)
8854 : : {
8855 : 86 : if (i != dim)
8856 : 52 : mpz_init_set (result->shape[i], source->shape[j++]);
8857 : : else
8858 : 34 : mpz_init_set_si (result->shape[i], ncopies);
8859 : :
8860 : 86 : extent[i] = mpz_get_si (result->shape[i]);
8861 : 86 : rstride[i] = (i == 0) ? 1 : rstride[i-1] * extent[i-1];
8862 : : }
8863 : :
8864 : 34 : offset = 0;
8865 : 34 : for (source_ctor = gfc_constructor_first (source->value.constructor);
8866 : 242 : source_ctor; source_ctor = gfc_constructor_next (source_ctor))
8867 : : {
8868 : 732 : for (i = 0; i < ncopies; ++i)
8869 : 524 : gfc_constructor_insert_expr (&result->value.constructor,
8870 : : gfc_copy_expr (source_ctor->expr),
8871 : 524 : NULL, offset + i * rstride[dim]);
8872 : :
8873 : 390 : offset += (dim == 0 ? ncopies : 1);
8874 : : }
8875 : : }
8876 : : else
8877 : : {
8878 : 0 : gfc_error ("Simplification of SPREAD at %C not yet implemented");
8879 : 0 : return &gfc_bad_expr;
8880 : : }
8881 : :
8882 : 129 : if (source->ts.type == BT_CHARACTER)
8883 : 20 : result->ts.u.cl = source->ts.u.cl;
8884 : :
8885 : : return result;
8886 : : }
8887 : :
8888 : :
8889 : : gfc_expr *
8890 : 1355 : gfc_simplify_sqrt (gfc_expr *e)
8891 : : {
8892 : 1355 : gfc_expr *result = NULL;
8893 : :
8894 : 1355 : if (e->expr_type != EXPR_CONSTANT)
8895 : : return NULL;
8896 : :
8897 : 217 : switch (e->ts.type)
8898 : : {
8899 : 160 : case BT_REAL:
8900 : 160 : if (mpfr_cmp_si (e->value.real, 0) < 0)
8901 : : {
8902 : 0 : gfc_error ("Argument of SQRT at %L has a negative value",
8903 : : &e->where);
8904 : 0 : return &gfc_bad_expr;
8905 : : }
8906 : 160 : result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
8907 : 160 : mpfr_sqrt (result->value.real, e->value.real, GFC_RND_MODE);
8908 : 160 : break;
8909 : :
8910 : 57 : case BT_COMPLEX:
8911 : 57 : gfc_set_model (e->value.real);
8912 : :
8913 : 57 : result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
8914 : 57 : mpc_sqrt (result->value.complex, e->value.complex, GFC_MPC_RND_MODE);
8915 : 57 : break;
8916 : :
8917 : 0 : default:
8918 : 0 : gfc_internal_error ("invalid argument of SQRT at %L", &e->where);
8919 : : }
8920 : :
8921 : 217 : return range_check (result, "SQRT");
8922 : : }
8923 : :
8924 : :
8925 : : gfc_expr *
8926 : 4592 : gfc_simplify_sum (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
8927 : : {
8928 : 4592 : return simplify_transformation (array, dim, mask, 0, gfc_add);
8929 : : }
8930 : :
8931 : :
8932 : : /* Simplify COTAN(X) where X has the unit of radian. */
8933 : :
8934 : : gfc_expr *
8935 : 230 : gfc_simplify_cotan (gfc_expr *x)
8936 : : {
8937 : 230 : gfc_expr *result;
8938 : 230 : mpc_t swp, *val;
8939 : :
8940 : 230 : if (x->expr_type != EXPR_CONSTANT)
8941 : : return NULL;
8942 : :
8943 : 26 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
8944 : :
8945 : 26 : switch (x->ts.type)
8946 : : {
8947 : 25 : case BT_REAL:
8948 : 25 : mpfr_cot (result->value.real, x->value.real, GFC_RND_MODE);
8949 : 25 : break;
8950 : :
8951 : 1 : case BT_COMPLEX:
8952 : : /* There is no builtin mpc_cot, so compute cot = cos / sin. */
8953 : 1 : val = &result->value.complex;
8954 : 1 : mpc_init2 (swp, mpfr_get_default_prec ());
8955 : 1 : mpc_sin_cos (*val, swp, x->value.complex, GFC_MPC_RND_MODE,
8956 : : GFC_MPC_RND_MODE);
8957 : 1 : mpc_div (*val, swp, *val, GFC_MPC_RND_MODE);
8958 : 1 : mpc_clear (swp);
8959 : 1 : break;
8960 : :
8961 : 0 : default:
8962 : 0 : gcc_unreachable ();
8963 : : }
8964 : :
8965 : 26 : return range_check (result, "COTAN");
8966 : : }
8967 : :
8968 : :
8969 : : gfc_expr *
8970 : 586 : gfc_simplify_tan (gfc_expr *x)
8971 : : {
8972 : 586 : gfc_expr *result;
8973 : :
8974 : 586 : if (x->expr_type != EXPR_CONSTANT)
8975 : : return NULL;
8976 : :
8977 : 46 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
8978 : :
8979 : 46 : switch (x->ts.type)
8980 : : {
8981 : 42 : case BT_REAL:
8982 : 42 : mpfr_tan (result->value.real, x->value.real, GFC_RND_MODE);
8983 : 42 : break;
8984 : :
8985 : 4 : case BT_COMPLEX:
8986 : 4 : mpc_tan (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
8987 : 4 : break;
8988 : :
8989 : 0 : default:
8990 : 0 : gcc_unreachable ();
8991 : : }
8992 : :
8993 : 46 : return range_check (result, "TAN");
8994 : : }
8995 : :
8996 : :
8997 : : gfc_expr *
8998 : 316 : gfc_simplify_tanh (gfc_expr *x)
8999 : : {
9000 : 316 : gfc_expr *result;
9001 : :
9002 : 316 : if (x->expr_type != EXPR_CONSTANT)
9003 : : return NULL;
9004 : :
9005 : 46 : result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
9006 : :
9007 : 46 : switch (x->ts.type)
9008 : : {
9009 : 42 : case BT_REAL:
9010 : 42 : mpfr_tanh (result->value.real, x->value.real, GFC_RND_MODE);
9011 : 42 : break;
9012 : :
9013 : 4 : case BT_COMPLEX:
9014 : 4 : mpc_tanh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
9015 : 4 : break;
9016 : :
9017 : 0 : default:
9018 : 0 : gcc_unreachable ();
9019 : : }
9020 : :
9021 : 46 : return range_check (result, "TANH");
9022 : : }
9023 : :
9024 : :
9025 : : gfc_expr *
9026 : 804 : gfc_simplify_tiny (gfc_expr *e)
9027 : : {
9028 : 804 : gfc_expr *result;
9029 : 804 : int i;
9030 : :
9031 : 804 : i = gfc_validate_kind (BT_REAL, e->ts.kind, false);
9032 : :
9033 : 804 : result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
9034 : 804 : mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
9035 : :
9036 : 804 : return result;
9037 : : }
9038 : :
9039 : :
9040 : : gfc_expr *
9041 : 1104 : gfc_simplify_trailz (gfc_expr *e)
9042 : : {
9043 : 1104 : unsigned long tz, bs;
9044 : 1104 : int i;
9045 : :
9046 : 1104 : if (e->expr_type != EXPR_CONSTANT)
9047 : : return NULL;
9048 : :
9049 : 258 : i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
9050 : 258 : bs = gfc_integer_kinds[i].bit_size;
9051 : 258 : tz = mpz_scan1 (e->value.integer, 0);
9052 : :
9053 : 258 : return gfc_get_int_expr (gfc_default_integer_kind,
9054 : 258 : &e->where, MIN (tz, bs));
9055 : : }
9056 : :
9057 : :
9058 : : gfc_expr *
9059 : 2742 : gfc_simplify_transfer (gfc_expr *source, gfc_expr *mold, gfc_expr *size)
9060 : : {
9061 : 2742 : gfc_expr *result;
9062 : 2742 : gfc_expr *mold_element;
9063 : 2742 : size_t source_size;
9064 : 2742 : size_t result_size;
9065 : 2742 : size_t buffer_size;
9066 : 2742 : mpz_t tmp;
9067 : 2742 : unsigned char *buffer;
9068 : 2742 : size_t result_length;
9069 : :
9070 : 2742 : if (!gfc_is_constant_expr (source) || !gfc_is_constant_expr (size))
9071 : 1804 : return NULL;
9072 : :
9073 : 938 : if (!gfc_resolve_expr (mold))
9074 : : return NULL;
9075 : 938 : if (gfc_init_expr_flag && !gfc_is_constant_expr (mold))
9076 : : return NULL;
9077 : :
9078 : 892 : if (!gfc_calculate_transfer_sizes (source, mold, size, &source_size,
9079 : : &result_size, &result_length))
9080 : : return NULL;
9081 : :
9082 : : /* Calculate the size of the source. */
9083 : 858 : if (source->expr_type == EXPR_ARRAY && !gfc_array_size (source, &tmp))
9084 : 0 : gfc_internal_error ("Failure getting length of a constant array.");
9085 : :
9086 : : /* Create an empty new expression with the appropriate characteristics. */
9087 : 858 : result = gfc_get_constant_expr (mold->ts.type, mold->ts.kind,
9088 : : &source->where);
9089 : 858 : result->ts = mold->ts;
9090 : :
9091 : 336 : mold_element = (mold->expr_type == EXPR_ARRAY && mold->value.constructor)
9092 : 1017 : ? gfc_constructor_first (mold->value.constructor)->expr
9093 : : : mold;
9094 : :
9095 : : /* Set result character length, if needed. Note that this needs to be
9096 : : set even for array expressions, in order to pass this information into
9097 : : gfc_target_interpret_expr. */
9098 : 858 : if (result->ts.type == BT_CHARACTER && gfc_is_constant_expr (mold_element))
9099 : : {
9100 : 341 : result->value.character.length = mold_element->value.character.length;
9101 : :
9102 : : /* Let the typespec of the result inherit the string length.
9103 : : This is crucial if a resulting array has size zero. */
9104 : 341 : if (mold_element->ts.u.cl->length)
9105 : 230 : result->ts.u.cl->length = gfc_copy_expr (mold_element->ts.u.cl->length);
9106 : : else
9107 : 111 : result->ts.u.cl->length =
9108 : 111 : gfc_get_int_expr (gfc_charlen_int_kind, NULL,
9109 : : mold_element->value.character.length);
9110 : : }
9111 : :
9112 : : /* Set the number of elements in the result, and determine its size. */
9113 : :
9114 : 858 : if (mold->expr_type == EXPR_ARRAY || mold->rank || size)
9115 : : {
9116 : 273 : result->expr_type = EXPR_ARRAY;
9117 : 273 : result->rank = 1;
9118 : 273 : result->shape = gfc_get_shape (1);
9119 : 273 : mpz_init_set_ui (result->shape[0], result_length);
9120 : : }
9121 : : else
9122 : 585 : result->rank = 0;
9123 : :
9124 : : /* Allocate the buffer to store the binary version of the source. */
9125 : 858 : buffer_size = MAX (source_size, result_size);
9126 : 858 : buffer = (unsigned char*)alloca (buffer_size);
9127 : 858 : memset (buffer, 0, buffer_size);
9128 : :
9129 : : /* Now write source to the buffer. */
9130 : 858 : gfc_target_encode_expr (source, buffer, buffer_size);
9131 : :
9132 : : /* And read the buffer back into the new expression. */
9133 : 858 : gfc_target_interpret_expr (buffer, buffer_size, result, false);
9134 : :
9135 : 858 : return result;
9136 : : }
9137 : :
9138 : :
9139 : : gfc_expr *
9140 : 1625 : gfc_simplify_transpose (gfc_expr *matrix)
9141 : : {
9142 : 1625 : int row, matrix_rows, col, matrix_cols;
9143 : 1625 : gfc_expr *result;
9144 : :
9145 : 1625 : if (!is_constant_array_expr (matrix))
9146 : : return NULL;
9147 : :
9148 : 45 : gcc_assert (matrix->rank == 2);
9149 : :
9150 : 45 : if (matrix->shape == NULL)
9151 : : return NULL;
9152 : :
9153 : 45 : result = gfc_get_array_expr (matrix->ts.type, matrix->ts.kind,
9154 : : &matrix->where);
9155 : 45 : result->rank = 2;
9156 : 45 : result->shape = gfc_get_shape (result->rank);
9157 : 45 : mpz_init_set (result->shape[0], matrix->shape[1]);
9158 : 45 : mpz_init_set (result->shape[1], matrix->shape[0]);
9159 : :
9160 : 45 : if (matrix->ts.type == BT_CHARACTER)
9161 : 18 : result->ts.u.cl = matrix->ts.u.cl;
9162 : 27 : else if (matrix->ts.type == BT_DERIVED)
9163 : 7 : result->ts.u.derived = matrix->ts.u.derived;
9164 : :
9165 : 45 : matrix_rows = mpz_get_si (matrix->shape[0]);
9166 : 45 : matrix_cols = mpz_get_si (matrix->shape[1]);
9167 : 201 : for (row = 0; row < matrix_rows; ++row)
9168 : 530 : for (col = 0; col < matrix_cols; ++col)
9169 : : {
9170 : 748 : gfc_expr *e = gfc_constructor_lookup_expr (matrix->value.constructor,
9171 : 374 : col * matrix_rows + row);
9172 : 374 : gfc_constructor_insert_expr (&result->value.constructor,
9173 : : gfc_copy_expr (e), &matrix->where,
9174 : 374 : row * matrix_cols + col);
9175 : : }
9176 : :
9177 : : return result;
9178 : : }
9179 : :
9180 : :
9181 : : gfc_expr *
9182 : 4486 : gfc_simplify_trim (gfc_expr *e)
9183 : : {
9184 : 4486 : gfc_expr *result;
9185 : 4486 : int count, i, len, lentrim;
9186 : :
9187 : 4486 : if (e->expr_type != EXPR_CONSTANT)
9188 : : return NULL;
9189 : :
9190 : 44 : len = e->value.character.length;
9191 : 196 : for (count = 0, i = 1; i <= len; ++i)
9192 : : {
9193 : 196 : if (e->value.character.string[len - i] == ' ')
9194 : 152 : count++;
9195 : : else
9196 : : break;
9197 : : }
9198 : :
9199 : 44 : lentrim = len - count;
9200 : :
9201 : 44 : result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, lentrim);
9202 : 769 : for (i = 0; i < lentrim; i++)
9203 : 681 : result->value.character.string[i] = e->value.character.string[i];
9204 : :
9205 : : return result;
9206 : : }
9207 : :
9208 : :
9209 : : gfc_expr *
9210 : 230 : gfc_simplify_image_index (gfc_expr *coarray, gfc_expr *sub,
9211 : : gfc_expr *team_or_team_number ATTRIBUTE_UNUSED)
9212 : : {
9213 : 230 : gfc_expr *result;
9214 : 230 : gfc_ref *ref;
9215 : 230 : gfc_array_spec *as;
9216 : 230 : gfc_constructor *sub_cons;
9217 : 230 : bool first_image;
9218 : 230 : int d;
9219 : :
9220 : 230 : if (!is_constant_array_expr (sub))
9221 : : return NULL;
9222 : :
9223 : : /* Follow any component references. */
9224 : 226 : as = coarray->symtree->n.sym->as;
9225 : 454 : for (ref = coarray->ref; ref; ref = ref->next)
9226 : 228 : if (ref->type == REF_COMPONENT)
9227 : 2 : as = ref->u.ar.as;
9228 : :
9229 : 226 : if (!as || as->type == AS_DEFERRED)
9230 : : return NULL;
9231 : :
9232 : : /* "valid sequence of cosubscripts" are required; thus, return 0 unless
9233 : : the cosubscript addresses the first image. */
9234 : :
9235 : 131 : sub_cons = gfc_constructor_first (sub->value.constructor);
9236 : 131 : first_image = true;
9237 : :
9238 : 289 : for (d = 1; d <= as->corank; d++)
9239 : : {
9240 : 204 : gfc_expr *ca_bound;
9241 : 204 : int cmp;
9242 : :
9243 : 204 : gcc_assert (sub_cons != NULL);
9244 : :
9245 : 204 : ca_bound = simplify_bound_dim (coarray, NULL, d + as->rank, 0, as,
9246 : : NULL, true);
9247 : 204 : if (ca_bound == NULL)
9248 : : return NULL;
9249 : :
9250 : 160 : if (ca_bound == &gfc_bad_expr)
9251 : : return ca_bound;
9252 : :
9253 : 160 : cmp = mpz_cmp (ca_bound->value.integer, sub_cons->expr->value.integer);
9254 : :
9255 : 160 : if (cmp == 0)
9256 : : {
9257 : 109 : gfc_free_expr (ca_bound);
9258 : 109 : sub_cons = gfc_constructor_next (sub_cons);
9259 : 109 : continue;
9260 : : }
9261 : :
9262 : 51 : first_image = false;
9263 : :
9264 : 51 : if (cmp > 0)
9265 : : {
9266 : 1 : gfc_error ("Out of bounds in IMAGE_INDEX at %L for dimension %d, "
9267 : : "SUB has %ld and COARRAY lower bound is %ld)",
9268 : : &coarray->where, d,
9269 : : mpz_get_si (sub_cons->expr->value.integer),
9270 : : mpz_get_si (ca_bound->value.integer));
9271 : 1 : gfc_free_expr (ca_bound);
9272 : 1 : return &gfc_bad_expr;
9273 : : }
9274 : :
9275 : 50 : gfc_free_expr (ca_bound);
9276 : :
9277 : : /* Check whether upperbound is valid for the multi-images case. */
9278 : 50 : if (d < as->corank)
9279 : : {
9280 : 23 : ca_bound = simplify_bound_dim (coarray, NULL, d + as->rank, 1, as,
9281 : : NULL, true);
9282 : 23 : if (ca_bound == &gfc_bad_expr)
9283 : : return ca_bound;
9284 : :
9285 : 23 : if (ca_bound && ca_bound->expr_type == EXPR_CONSTANT
9286 : 23 : && mpz_cmp (ca_bound->value.integer,
9287 : 23 : sub_cons->expr->value.integer) < 0)
9288 : : {
9289 : 1 : gfc_error ("Out of bounds in IMAGE_INDEX at %L for dimension %d, "
9290 : : "SUB has %ld and COARRAY upper bound is %ld)",
9291 : : &coarray->where, d,
9292 : : mpz_get_si (sub_cons->expr->value.integer),
9293 : : mpz_get_si (ca_bound->value.integer));
9294 : 1 : gfc_free_expr (ca_bound);
9295 : 1 : return &gfc_bad_expr;
9296 : : }
9297 : :
9298 : : if (ca_bound)
9299 : 22 : gfc_free_expr (ca_bound);
9300 : : }
9301 : :
9302 : 49 : sub_cons = gfc_constructor_next (sub_cons);
9303 : : }
9304 : :
9305 : 85 : gcc_assert (sub_cons == NULL);
9306 : :
9307 : 85 : if (flag_coarray != GFC_FCOARRAY_SINGLE && !first_image)
9308 : : return NULL;
9309 : :
9310 : 74 : result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
9311 : : &gfc_current_locus);
9312 : 74 : if (first_image)
9313 : 41 : mpz_set_si (result->value.integer, 1);
9314 : : else
9315 : 33 : mpz_set_si (result->value.integer, 0);
9316 : :
9317 : : return result;
9318 : : }
9319 : :
9320 : : gfc_expr *
9321 : 70 : gfc_simplify_image_status (gfc_expr *image, gfc_expr *team ATTRIBUTE_UNUSED)
9322 : : {
9323 : 70 : if (flag_coarray == GFC_FCOARRAY_NONE)
9324 : : {
9325 : 0 : gfc_current_locus = *gfc_current_intrinsic_where;
9326 : 0 : gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
9327 : : return &gfc_bad_expr;
9328 : : }
9329 : :
9330 : : /* Simplification is possible for fcoarray = single only. For all other modes
9331 : : the result depends on runtime conditions. */
9332 : 70 : if (flag_coarray != GFC_FCOARRAY_SINGLE)
9333 : : return NULL;
9334 : :
9335 : 12 : if (gfc_is_constant_expr (image))
9336 : : {
9337 : 4 : gfc_expr *result;
9338 : 4 : result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
9339 : : &image->where);
9340 : 4 : if (mpz_get_si (image->value.integer) == 1)
9341 : 2 : mpz_set_si (result->value.integer, 0);
9342 : : else
9343 : 2 : mpz_set_si (result->value.integer, GFC_STAT_STOPPED_IMAGE);
9344 : 4 : return result;
9345 : : }
9346 : : else
9347 : : return NULL;
9348 : : }
9349 : :
9350 : :
9351 : : gfc_expr *
9352 : 2467 : gfc_simplify_this_image (gfc_expr *coarray, gfc_expr *dim,
9353 : : gfc_expr *team ATTRIBUTE_UNUSED)
9354 : : {
9355 : 2467 : if (flag_coarray != GFC_FCOARRAY_SINGLE)
9356 : : return NULL;
9357 : :
9358 : : /* If no coarray argument has been passed. */
9359 : 1086 : if (coarray == NULL)
9360 : : {
9361 : 582 : gfc_expr *result;
9362 : : /* FIXME: gfc_current_locus is wrong. */
9363 : 582 : result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
9364 : : &gfc_current_locus);
9365 : 582 : mpz_set_si (result->value.integer, 1);
9366 : 582 : return result;
9367 : : }
9368 : :
9369 : : /* For -fcoarray=single, this_image(A) is the same as lcobound(A). */
9370 : 504 : return simplify_cobound (coarray, dim, NULL, 0);
9371 : : }
9372 : :
9373 : :
9374 : : gfc_expr *
9375 : 14906 : gfc_simplify_ubound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
9376 : : {
9377 : 14906 : return simplify_bound (array, dim, kind, 1);
9378 : : }
9379 : :
9380 : : gfc_expr *
9381 : 466 : gfc_simplify_ucobound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
9382 : : {
9383 : 466 : return simplify_cobound (array, dim, kind, 1);
9384 : : }
9385 : :
9386 : :
9387 : : gfc_expr *
9388 : 480 : gfc_simplify_unpack (gfc_expr *vector, gfc_expr *mask, gfc_expr *field)
9389 : : {
9390 : 480 : gfc_expr *result, *e;
9391 : 480 : gfc_constructor *vector_ctor, *mask_ctor, *field_ctor;
9392 : :
9393 : 480 : if (!is_constant_array_expr (vector)
9394 : 242 : || !is_constant_array_expr (mask)
9395 : 503 : || (!gfc_is_constant_expr (field)
9396 : 12 : && !is_constant_array_expr (field)))
9397 : 457 : return NULL;
9398 : :
9399 : 23 : result = gfc_get_array_expr (vector->ts.type, vector->ts.kind,
9400 : : &vector->where);
9401 : 23 : if (vector->ts.type == BT_DERIVED)
9402 : 4 : result->ts.u.derived = vector->ts.u.derived;
9403 : 23 : result->rank = mask->rank;
9404 : 23 : result->shape = gfc_copy_shape (mask->shape, mask->rank);
9405 : :
9406 : 23 : if (vector->ts.type == BT_CHARACTER)
9407 : 0 : result->ts.u.cl = vector->ts.u.cl;
9408 : :
9409 : 23 : vector_ctor = gfc_constructor_first (vector->value.constructor);
9410 : 23 : mask_ctor = gfc_constructor_first (mask->value.constructor);
9411 : 23 : field_ctor
9412 : 23 : = field->expr_type == EXPR_ARRAY
9413 : 23 : ? gfc_constructor_first (field->value.constructor)
9414 : : : NULL;
9415 : :
9416 : 168 : while (mask_ctor)
9417 : : {
9418 : 151 : if (mask_ctor->expr->value.logical)
9419 : : {
9420 : 55 : if (vector_ctor)
9421 : : {
9422 : 52 : e = gfc_copy_expr (vector_ctor->expr);
9423 : 52 : vector_ctor = gfc_constructor_next (vector_ctor);
9424 : : }
9425 : : else
9426 : : {
9427 : 3 : gfc_free_expr (result);
9428 : 3 : return NULL;
9429 : : }
9430 : : }
9431 : 96 : else if (field->expr_type == EXPR_ARRAY)
9432 : : {
9433 : 52 : if (field_ctor)
9434 : 49 : e = gfc_copy_expr (field_ctor->expr);
9435 : : else
9436 : : {
9437 : : /* Not enough elements in array FIELD. */
9438 : 3 : gfc_free_expr (result);
9439 : 3 : return &gfc_bad_expr;
9440 : : }
9441 : : }
9442 : : else
9443 : 44 : e = gfc_copy_expr (field);
9444 : :
9445 : 145 : gfc_constructor_append_expr (&result->value.constructor, e, NULL);
9446 : :
9447 : 145 : mask_ctor = gfc_constructor_next (mask_ctor);
9448 : 145 : field_ctor = gfc_constructor_next (field_ctor);
9449 : : }
9450 : :
9451 : : return result;
9452 : : }
9453 : :
9454 : :
9455 : : gfc_expr *
9456 : 410 : gfc_simplify_verify (gfc_expr *s, gfc_expr *set, gfc_expr *b, gfc_expr *kind)
9457 : : {
9458 : 410 : gfc_expr *result;
9459 : 410 : int back;
9460 : 410 : size_t index, len, lenset;
9461 : 410 : size_t i;
9462 : 410 : int k = get_kind (BT_INTEGER, kind, "VERIFY", gfc_default_integer_kind);
9463 : :
9464 : 410 : if (k == -1)
9465 : : return &gfc_bad_expr;
9466 : :
9467 : 410 : if (s->expr_type != EXPR_CONSTANT || set->expr_type != EXPR_CONSTANT
9468 : 158 : || ( b != NULL && b->expr_type != EXPR_CONSTANT))
9469 : : return NULL;
9470 : :
9471 : 150 : if (b != NULL && b->value.logical != 0)
9472 : : back = 1;
9473 : : else
9474 : 78 : back = 0;
9475 : :
9476 : 156 : result = gfc_get_constant_expr (BT_INTEGER, k, &s->where);
9477 : :
9478 : 156 : len = s->value.character.length;
9479 : 156 : lenset = set->value.character.length;
9480 : :
9481 : 156 : if (len == 0)
9482 : : {
9483 : 0 : mpz_set_ui (result->value.integer, 0);
9484 : 0 : return result;
9485 : : }
9486 : :
9487 : 156 : if (back == 0)
9488 : : {
9489 : 78 : if (lenset == 0)
9490 : : {
9491 : 18 : mpz_set_ui (result->value.integer, 1);
9492 : 18 : return result;
9493 : : }
9494 : :
9495 : 60 : index = wide_strspn (s->value.character.string,
9496 : 60 : set->value.character.string) + 1;
9497 : 60 : if (index > len)
9498 : 0 : index = 0;
9499 : :
9500 : : }
9501 : : else
9502 : : {
9503 : 78 : if (lenset == 0)
9504 : : {
9505 : 18 : mpz_set_ui (result->value.integer, len);
9506 : 18 : return result;
9507 : : }
9508 : 96 : for (index = len; index > 0; index --)
9509 : : {
9510 : 300 : for (i = 0; i < lenset; i++)
9511 : : {
9512 : 240 : if (s->value.character.string[index - 1]
9513 : 240 : == set->value.character.string[i])
9514 : : break;
9515 : : }
9516 : 96 : if (i == lenset)
9517 : : break;
9518 : : }
9519 : : }
9520 : :
9521 : 120 : mpz_set_ui (result->value.integer, index);
9522 : 120 : return result;
9523 : : }
9524 : :
9525 : :
9526 : : gfc_expr *
9527 : 26 : gfc_simplify_xor (gfc_expr *x, gfc_expr *y)
9528 : : {
9529 : 26 : gfc_expr *result;
9530 : 26 : int kind;
9531 : :
9532 : 26 : if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
9533 : : return NULL;
9534 : :
9535 : 6 : kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
9536 : :
9537 : 6 : switch (x->ts.type)
9538 : : {
9539 : 0 : case BT_INTEGER:
9540 : 0 : result = gfc_get_constant_expr (BT_INTEGER, kind, &x->where);
9541 : 0 : mpz_xor (result->value.integer, x->value.integer, y->value.integer);
9542 : 0 : return range_check (result, "XOR");
9543 : :
9544 : 6 : case BT_LOGICAL:
9545 : 6 : return gfc_get_logical_expr (kind, &x->where,
9546 : 6 : (x->value.logical && !y->value.logical)
9547 : 18 : || (!x->value.logical && y->value.logical));
9548 : :
9549 : 0 : default:
9550 : 0 : gcc_unreachable ();
9551 : : }
9552 : : }
9553 : :
9554 : :
9555 : : /****************** Constant simplification *****************/
9556 : :
9557 : : /* Master function to convert one constant to another. While this is
9558 : : used as a simplification function, it requires the destination type
9559 : : and kind information which is supplied by a special case in
9560 : : do_simplify(). */
9561 : :
9562 : : gfc_expr *
9563 : 161849 : gfc_convert_constant (gfc_expr *e, bt type, int kind)
9564 : : {
9565 : 161849 : gfc_expr *result, *(*f) (gfc_expr *, int);
9566 : 161849 : gfc_constructor *c, *t;
9567 : :
9568 : 161849 : switch (e->ts.type)
9569 : : {
9570 : 141365 : case BT_INTEGER:
9571 : 141365 : switch (type)
9572 : : {
9573 : : case BT_INTEGER:
9574 : : f = gfc_int2int;
9575 : : break;
9576 : 152 : case BT_UNSIGNED:
9577 : 152 : f = gfc_int2uint;
9578 : 152 : break;
9579 : 61779 : case BT_REAL:
9580 : 61779 : f = gfc_int2real;
9581 : 61779 : break;
9582 : 1388 : case BT_COMPLEX:
9583 : 1388 : f = gfc_int2complex;
9584 : 1388 : break;
9585 : 0 : case BT_LOGICAL:
9586 : 0 : f = gfc_int2log;
9587 : 0 : break;
9588 : 0 : default:
9589 : 0 : goto oops;
9590 : : }
9591 : : break;
9592 : :
9593 : 596 : case BT_UNSIGNED:
9594 : 596 : switch (type)
9595 : : {
9596 : : case BT_INTEGER:
9597 : : f = gfc_uint2int;
9598 : : break;
9599 : 223 : case BT_UNSIGNED:
9600 : 223 : f = gfc_uint2uint;
9601 : 223 : break;
9602 : 48 : case BT_REAL:
9603 : 48 : f = gfc_uint2real;
9604 : 48 : break;
9605 : 0 : case BT_COMPLEX:
9606 : 0 : f = gfc_uint2complex;
9607 : 0 : break;
9608 : 0 : case BT_LOGICAL:
9609 : 0 : f = gfc_uint2log;
9610 : 0 : break;
9611 : 0 : default:
9612 : 0 : goto oops;
9613 : : }
9614 : : break;
9615 : :
9616 : 12972 : case BT_REAL:
9617 : 12972 : switch (type)
9618 : : {
9619 : : case BT_INTEGER:
9620 : : f = gfc_real2int;
9621 : : break;
9622 : 6 : case BT_UNSIGNED:
9623 : 6 : f = gfc_real2uint;
9624 : 6 : break;
9625 : 10230 : case BT_REAL:
9626 : 10230 : f = gfc_real2real;
9627 : 10230 : break;
9628 : 1981 : case BT_COMPLEX:
9629 : 1981 : f = gfc_real2complex;
9630 : 1981 : break;
9631 : 0 : default:
9632 : 0 : goto oops;
9633 : : }
9634 : : break;
9635 : :
9636 : 2913 : case BT_COMPLEX:
9637 : 2913 : switch (type)
9638 : : {
9639 : : case BT_INTEGER:
9640 : : f = gfc_complex2int;
9641 : : break;
9642 : 6 : case BT_UNSIGNED:
9643 : 6 : f = gfc_complex2uint;
9644 : 6 : break;
9645 : 204 : case BT_REAL:
9646 : 204 : f = gfc_complex2real;
9647 : 204 : break;
9648 : 2647 : case BT_COMPLEX:
9649 : 2647 : f = gfc_complex2complex;
9650 : 2647 : break;
9651 : :
9652 : 0 : default:
9653 : 0 : goto oops;
9654 : : }
9655 : : break;
9656 : :
9657 : 1913 : case BT_LOGICAL:
9658 : 1913 : switch (type)
9659 : : {
9660 : : case BT_INTEGER:
9661 : : f = gfc_log2int;
9662 : : break;
9663 : 0 : case BT_UNSIGNED:
9664 : 0 : f = gfc_log2uint;
9665 : 0 : break;
9666 : 1683 : case BT_LOGICAL:
9667 : 1683 : f = gfc_log2log;
9668 : 1683 : break;
9669 : 0 : default:
9670 : 0 : goto oops;
9671 : : }
9672 : : break;
9673 : :
9674 : 1330 : case BT_HOLLERITH:
9675 : 1330 : switch (type)
9676 : : {
9677 : : case BT_INTEGER:
9678 : : f = gfc_hollerith2int;
9679 : : break;
9680 : :
9681 : : /* Hollerith is for legacy code, we do not currently support
9682 : : converting this to UNSIGNED. */
9683 : 0 : case BT_UNSIGNED:
9684 : 0 : goto oops;
9685 : :
9686 : 327 : case BT_REAL:
9687 : 327 : f = gfc_hollerith2real;
9688 : 327 : break;
9689 : :
9690 : 288 : case BT_COMPLEX:
9691 : 288 : f = gfc_hollerith2complex;
9692 : 288 : break;
9693 : :
9694 : 146 : case BT_CHARACTER:
9695 : 146 : f = gfc_hollerith2character;
9696 : 146 : break;
9697 : :
9698 : 195 : case BT_LOGICAL:
9699 : 195 : f = gfc_hollerith2logical;
9700 : 195 : break;
9701 : :
9702 : 0 : default:
9703 : 0 : goto oops;
9704 : : }
9705 : : break;
9706 : :
9707 : 747 : case BT_CHARACTER:
9708 : 747 : switch (type)
9709 : : {
9710 : : case BT_INTEGER:
9711 : : f = gfc_character2int;
9712 : : break;
9713 : :
9714 : 0 : case BT_UNSIGNED:
9715 : 0 : goto oops;
9716 : :
9717 : 187 : case BT_REAL:
9718 : 187 : f = gfc_character2real;
9719 : 187 : break;
9720 : :
9721 : 187 : case BT_COMPLEX:
9722 : 187 : f = gfc_character2complex;
9723 : 187 : break;
9724 : :
9725 : 0 : case BT_CHARACTER:
9726 : 0 : f = gfc_character2character;
9727 : 0 : break;
9728 : :
9729 : 186 : case BT_LOGICAL:
9730 : 186 : f = gfc_character2logical;
9731 : 186 : break;
9732 : :
9733 : 0 : default:
9734 : 0 : goto oops;
9735 : : }
9736 : : break;
9737 : :
9738 : : default:
9739 : 161849 : oops:
9740 : : return &gfc_bad_expr;
9741 : : }
9742 : :
9743 : 161836 : result = NULL;
9744 : :
9745 : 161836 : switch (e->expr_type)
9746 : : {
9747 : 124567 : case EXPR_CONSTANT:
9748 : 124567 : result = f (e, kind);
9749 : 124567 : if (result == NULL)
9750 : : return &gfc_bad_expr;
9751 : : break;
9752 : :
9753 : 5587 : case EXPR_ARRAY:
9754 : 5587 : if (!gfc_is_constant_expr (e))
9755 : : break;
9756 : :
9757 : 5413 : result = gfc_get_array_expr (type, kind, &e->where);
9758 : 5413 : result->shape = gfc_copy_shape (e->shape, e->rank);
9759 : 5413 : result->rank = e->rank;
9760 : :
9761 : 5413 : for (c = gfc_constructor_first (e->value.constructor);
9762 : 80366 : c; c = gfc_constructor_next (c))
9763 : : {
9764 : 74990 : gfc_expr *tmp;
9765 : 74990 : if (c->iterator == NULL)
9766 : : {
9767 : 74967 : if (c->expr->expr_type == EXPR_ARRAY)
9768 : 69 : tmp = gfc_convert_constant (c->expr, type, kind);
9769 : 74898 : else if (c->expr->expr_type == EXPR_OP)
9770 : : {
9771 : 29 : if (!gfc_simplify_expr (c->expr, 1))
9772 : : return &gfc_bad_expr;
9773 : 29 : tmp = f (c->expr, kind);
9774 : : }
9775 : : else
9776 : 74869 : tmp = f (c->expr, kind);
9777 : : }
9778 : : else
9779 : 23 : tmp = gfc_convert_constant (c->expr, type, kind);
9780 : :
9781 : 74990 : if (tmp == NULL || tmp == &gfc_bad_expr)
9782 : : {
9783 : 37 : gfc_free_expr (result);
9784 : 37 : return NULL;
9785 : : }
9786 : :
9787 : 74953 : t = gfc_constructor_append_expr (&result->value.constructor,
9788 : : tmp, &c->where);
9789 : 74953 : if (c->iterator)
9790 : 4 : t->iterator = gfc_copy_iterator (c->iterator);
9791 : : }
9792 : :
9793 : : break;
9794 : :
9795 : : default:
9796 : : break;
9797 : : }
9798 : :
9799 : : return result;
9800 : : }
9801 : :
9802 : :
9803 : : /* Function for converting character constants. */
9804 : : gfc_expr *
9805 : 209 : gfc_convert_char_constant (gfc_expr *e, bt type ATTRIBUTE_UNUSED, int kind)
9806 : : {
9807 : 209 : gfc_expr *result;
9808 : 209 : int i;
9809 : :
9810 : 209 : if (!gfc_is_constant_expr (e))
9811 : : return NULL;
9812 : :
9813 : 209 : if (e->expr_type == EXPR_CONSTANT)
9814 : : {
9815 : : /* Simple case of a scalar. */
9816 : 196 : result = gfc_get_constant_expr (BT_CHARACTER, kind, &e->where);
9817 : 196 : if (result == NULL)
9818 : : return &gfc_bad_expr;
9819 : :
9820 : 196 : result->value.character.length = e->value.character.length;
9821 : 196 : result->value.character.string
9822 : 196 : = gfc_get_wide_string (e->value.character.length + 1);
9823 : 196 : memcpy (result->value.character.string, e->value.character.string,
9824 : 196 : (e->value.character.length + 1) * sizeof (gfc_char_t));
9825 : :
9826 : : /* Check we only have values representable in the destination kind. */
9827 : 1164 : for (i = 0; i < result->value.character.length; i++)
9828 : 972 : if (!gfc_check_character_range (result->value.character.string[i],
9829 : : kind))
9830 : : {
9831 : 4 : gfc_error ("Character %qs in string at %L cannot be converted "
9832 : : "into character kind %d",
9833 : 4 : gfc_print_wide_char (result->value.character.string[i]),
9834 : : &e->where, kind);
9835 : 4 : gfc_free_expr (result);
9836 : 4 : return &gfc_bad_expr;
9837 : : }
9838 : :
9839 : : return result;
9840 : : }
9841 : 13 : else if (e->expr_type == EXPR_ARRAY)
9842 : : {
9843 : : /* For an array constructor, we convert each constructor element. */
9844 : 13 : gfc_constructor *c;
9845 : :
9846 : 13 : result = gfc_get_array_expr (type, kind, &e->where);
9847 : 13 : result->shape = gfc_copy_shape (e->shape, e->rank);
9848 : 13 : result->rank = e->rank;
9849 : 13 : result->ts.u.cl = e->ts.u.cl;
9850 : :
9851 : 13 : for (c = gfc_constructor_first (e->value.constructor);
9852 : 40 : c; c = gfc_constructor_next (c))
9853 : : {
9854 : 27 : gfc_expr *tmp = gfc_convert_char_constant (c->expr, type, kind);
9855 : 27 : if (tmp == &gfc_bad_expr)
9856 : : {
9857 : 0 : gfc_free_expr (result);
9858 : 0 : return &gfc_bad_expr;
9859 : : }
9860 : :
9861 : 27 : if (tmp == NULL)
9862 : : {
9863 : 0 : gfc_free_expr (result);
9864 : 0 : return NULL;
9865 : : }
9866 : :
9867 : 27 : gfc_constructor_append_expr (&result->value.constructor,
9868 : : tmp, &c->where);
9869 : : }
9870 : :
9871 : : return result;
9872 : : }
9873 : : else
9874 : : return NULL;
9875 : : }
9876 : :
9877 : :
9878 : : gfc_expr *
9879 : 8 : gfc_simplify_compiler_options (void)
9880 : : {
9881 : 8 : char *str;
9882 : 8 : gfc_expr *result;
9883 : :
9884 : 8 : str = gfc_get_option_string ();
9885 : 16 : result = gfc_get_character_expr (gfc_default_character_kind,
9886 : 8 : &gfc_current_locus, str, strlen (str));
9887 : 8 : free (str);
9888 : 8 : return result;
9889 : : }
9890 : :
9891 : :
9892 : : gfc_expr *
9893 : 10 : gfc_simplify_compiler_version (void)
9894 : : {
9895 : 10 : char *buffer;
9896 : 10 : size_t len;
9897 : :
9898 : 10 : len = strlen ("GCC version ") + strlen (version_string);
9899 : 10 : buffer = XALLOCAVEC (char, len + 1);
9900 : 10 : snprintf (buffer, len + 1, "GCC version %s", version_string);
9901 : 10 : return gfc_get_character_expr (gfc_default_character_kind,
9902 : 10 : &gfc_current_locus, buffer, len);
9903 : : }
9904 : :
9905 : : /* Simplification routines for intrinsics of IEEE modules. */
9906 : :
9907 : : gfc_expr *
9908 : 243 : simplify_ieee_selected_real_kind (gfc_expr *expr)
9909 : : {
9910 : 243 : gfc_actual_arglist *arg;
9911 : 243 : gfc_expr *p = NULL, *q = NULL, *rdx = NULL;
9912 : :
9913 : 243 : arg = expr->value.function.actual;
9914 : 243 : p = arg->expr;
9915 : 243 : if (arg->next)
9916 : : {
9917 : 241 : q = arg->next->expr;
9918 : 241 : if (arg->next->next)
9919 : 241 : rdx = arg->next->next->expr;
9920 : : }
9921 : :
9922 : : /* Currently, if IEEE is supported and this module is built, it means
9923 : : all our floating-point types conform to IEEE. Hence, we simply handle
9924 : : IEEE_SELECTED_REAL_KIND like SELECTED_REAL_KIND. */
9925 : 243 : return gfc_simplify_selected_real_kind (p, q, rdx);
9926 : : }
9927 : :
9928 : : gfc_expr *
9929 : 102 : simplify_ieee_support (gfc_expr *expr)
9930 : : {
9931 : : /* We consider that if the IEEE modules are loaded, we have full support
9932 : : for flags, halting and rounding, which are the three functions
9933 : : (IEEE_SUPPORT_{FLAG,HALTING,ROUNDING}) allowed in constant
9934 : : expressions. One day, we will need libgfortran to detect support and
9935 : : communicate it back to us, allowing for partial support. */
9936 : :
9937 : 102 : return gfc_get_logical_expr (gfc_default_logical_kind, &expr->where,
9938 : 102 : true);
9939 : : }
9940 : :
9941 : : bool
9942 : 993 : matches_ieee_function_name (gfc_symbol *sym, const char *name)
9943 : : {
9944 : 993 : int n = strlen(name);
9945 : :
9946 : 993 : if (!strncmp(sym->name, name, n))
9947 : : return true;
9948 : :
9949 : : /* If a generic was used and renamed, we need more work to find out.
9950 : : Compare the specific name. */
9951 : 654 : if (sym->generic && !strncmp(sym->generic->sym->name, name, n))
9952 : 6 : return true;
9953 : :
9954 : : return false;
9955 : : }
9956 : :
9957 : : gfc_expr *
9958 : 453 : gfc_simplify_ieee_functions (gfc_expr *expr)
9959 : : {
9960 : 453 : gfc_symbol* sym = expr->symtree->n.sym;
9961 : :
9962 : 453 : if (matches_ieee_function_name(sym, "ieee_selected_real_kind"))
9963 : 243 : return simplify_ieee_selected_real_kind (expr);
9964 : 210 : else if (matches_ieee_function_name(sym, "ieee_support_flag")
9965 : 174 : || matches_ieee_function_name(sym, "ieee_support_halting")
9966 : 366 : || matches_ieee_function_name(sym, "ieee_support_rounding"))
9967 : 102 : return simplify_ieee_support (expr);
9968 : : else
9969 : : return NULL;
9970 : : }
|