1 /* Simplify intrinsic functions at compile-time.
2 Copyright (C) 2000-2024 Free Software Foundation, Inc.
3 Contributed by Andy Vaught & Katherine Holcomb
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
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
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/>. */
23 #include "coretypes.h"
24 #include "tm.h" /* For BITS_PER_UNIT. */
27 #include "intrinsic.h"
29 #include "target-memory.h"
30 #include "constructor.h"
31 #include "version.h" /* For version_string. */
35 static int min_max_choose (gfc_expr
*, gfc_expr
*, int, bool back_val
= false);
37 gfc_expr gfc_bad_expr
;
39 static gfc_expr
*simplify_size (gfc_expr
*, gfc_expr
*, int);
42 /* Note that 'simplification' is not just transforming expressions.
43 For functions that are not simplified at compile time, range
44 checking is done if possible.
46 The return convention is that each simplification function returns:
48 A new expression node corresponding to the simplified arguments.
49 The original arguments are destroyed by the caller, and must not
50 be a part of the new expression.
52 NULL pointer indicating that no simplification was possible and
53 the original expression should remain intact.
55 An expression pointer to gfc_bad_expr (a static placeholder)
56 indicating that some error has prevented simplification. The
57 error is generated within the function and should be propagated
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
67 Array arguments are only passed to these subroutines that implement
68 the simplification of transformational intrinsics.
70 The functions in this file don't have much comment with them, but
71 everything is reasonably straight-forward. The Standard, chapter 13
72 is the best comment you'll find for this file anyway. */
74 /* Range checks an expression node. If all goes well, returns the
75 node, otherwise returns &gfc_bad_expr and frees the node. */
78 range_check (gfc_expr
*result
, const char *name
)
83 if (result
->expr_type
!= EXPR_CONSTANT
)
86 switch (gfc_range_check (result
))
92 gfc_error ("Result of %s overflows its kind at %L", name
,
97 gfc_error ("Result of %s underflows its kind at %L", name
,
102 gfc_error ("Result of %s is NaN at %L", name
, &result
->where
);
106 gfc_error ("Result of %s gives range error for its kind at %L", name
,
111 gfc_free_expr (result
);
112 return &gfc_bad_expr
;
116 /* A helper function that gets an optional and possibly missing
117 kind parameter. Returns the kind, -1 if something went wrong. */
120 get_kind (bt type
, gfc_expr
*k
, const char *name
, int default_kind
)
127 if (k
->expr_type
!= EXPR_CONSTANT
)
129 gfc_error ("KIND parameter of %s at %L must be an initialization "
130 "expression", name
, &k
->where
);
134 if (gfc_extract_int (k
, &kind
)
135 || gfc_validate_kind (type
, kind
, true) < 0)
137 gfc_error ("Invalid KIND parameter of %s at %L", name
, &k
->where
);
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. */
151 gfc_convert_mpz_to_unsigned (mpz_t x
, int bitsize
, bool sign
)
157 /* Confirm that no bits above the signed range are unset if we
158 are doing range checking. */
159 if (sign
&& flag_range_check
!= 0)
160 gcc_assert (mpz_scan0 (x
, bitsize
-1) == ULONG_MAX
);
162 mpz_init_set_ui (mask
, 1);
163 mpz_mul_2exp (mask
, mask
, bitsize
);
164 mpz_sub_ui (mask
, mask
, 1);
166 mpz_and (x
, x
, mask
);
172 /* Confirm that no bits above the signed range are set if we
173 are doing range checking. */
174 if (sign
&& flag_range_check
!= 0)
175 gcc_assert (mpz_scan1 (x
, bitsize
-1) == ULONG_MAX
);
180 /* Converts an mpz_t unsigned variable into a signed one, assuming
181 two's complement representations and a binary width of bitsize.
182 If the bitsize-1 bit is set, this is taken as a sign bit and
183 the number is converted to the corresponding negative number. */
186 gfc_convert_mpz_to_signed (mpz_t x
, int bitsize
)
190 /* Confirm that no bits above the unsigned range are set if we are
191 doing range checking. */
192 if (flag_range_check
!= 0)
193 gcc_assert (mpz_scan1 (x
, bitsize
) == ULONG_MAX
);
195 if (mpz_tstbit (x
, bitsize
- 1) == 1)
197 mpz_init_set_ui (mask
, 1);
198 mpz_mul_2exp (mask
, mask
, bitsize
);
199 mpz_sub_ui (mask
, mask
, 1);
201 /* We negate the number by hand, zeroing the high bits, that is
202 make it the corresponding positive number, and then have it
203 negated by GMP, giving the correct representation of the
206 mpz_add_ui (x
, x
, 1);
207 mpz_and (x
, x
, mask
);
216 /* Test that the expression is a constant array, simplifying if
217 we are dealing with a parameter array. */
220 is_constant_array_expr (gfc_expr
*e
)
223 bool array_OK
= true;
229 if (e
->expr_type
== EXPR_VARIABLE
&& e
->rank
> 0
230 && e
->symtree
->n
.sym
->attr
.flavor
== FL_PARAMETER
)
231 gfc_simplify_expr (e
, 1);
233 if (e
->expr_type
!= EXPR_ARRAY
|| !gfc_is_constant_expr (e
))
236 /* A non-zero-sized constant array shall have a non-empty constructor. */
237 if (e
->rank
> 0 && e
->shape
!= NULL
&& e
->value
.constructor
== NULL
)
239 mpz_init_set_ui (size
, 1);
240 for (int j
= 0; j
< e
->rank
; j
++)
241 mpz_mul (size
, size
, e
->shape
[j
]);
242 bool not_size0
= (mpz_cmp_si (size
, 0) != 0);
248 for (c
= gfc_constructor_first (e
->value
.constructor
);
249 c
; c
= gfc_constructor_next (c
))
250 if (c
->expr
->expr_type
!= EXPR_CONSTANT
251 && c
->expr
->expr_type
!= EXPR_STRUCTURE
)
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. */
260 expand
= (e
->rank
== 1
262 && (mpz_cmp_ui (e
->shape
[0], flag_max_array_constructor
) < 0));
264 if (!array_OK
&& (gfc_init_expr_flag
|| expand
) && e
->rank
== 1)
266 bool saved_init_expr_flag
= gfc_init_expr_flag
;
267 array_OK
= gfc_reduce_init_expr (e
);
268 /* gfc_reduce_init_expr resets the flag. */
269 gfc_init_expr_flag
= saved_init_expr_flag
;
274 /* Recheck to make sure that any EXPR_ARRAYs have gone. */
275 for (c
= gfc_constructor_first (e
->value
.constructor
);
276 c
; c
= gfc_constructor_next (c
))
277 if (c
->expr
->expr_type
!= EXPR_CONSTANT
278 && c
->expr
->expr_type
!= EXPR_STRUCTURE
)
281 /* Make sure that the array has a valid shape. */
282 if (e
->shape
== NULL
&& e
->rank
== 1)
284 if (!gfc_array_size(e
, &size
))
286 e
->shape
= gfc_get_shape (1);
287 mpz_init_set (e
->shape
[0], size
);
295 gfc_is_constant_array_expr (gfc_expr
*e
)
297 return is_constant_array_expr (e
);
301 /* Test for a size zero array. */
303 gfc_is_size_zero_array (gfc_expr
*array
)
306 if (array
->rank
== 0)
309 if (array
->expr_type
== EXPR_VARIABLE
&& array
->rank
> 0
310 && array
->symtree
->n
.sym
->attr
.flavor
== FL_PARAMETER
311 && array
->shape
!= NULL
)
313 for (int i
= 0; i
< array
->rank
; i
++)
314 if (mpz_cmp_si (array
->shape
[i
], 0) <= 0)
320 if (array
->expr_type
== EXPR_ARRAY
)
321 return array
->value
.constructor
== NULL
;
327 /* Initialize a transformational result expression with a given value. */
330 init_result_expr (gfc_expr
*e
, int init
, gfc_expr
*array
)
332 if (e
&& e
->expr_type
== EXPR_ARRAY
)
334 gfc_constructor
*ctor
= gfc_constructor_first (e
->value
.constructor
);
337 init_result_expr (ctor
->expr
, init
, array
);
338 ctor
= gfc_constructor_next (ctor
);
341 else if (e
&& e
->expr_type
== EXPR_CONSTANT
)
343 int i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
344 HOST_WIDE_INT length
;
350 e
->value
.logical
= (init
? 1 : 0);
355 mpz_set (e
->value
.integer
, gfc_integer_kinds
[i
].min_int
);
356 else if (init
== INT_MAX
)
357 mpz_set (e
->value
.integer
, gfc_integer_kinds
[i
].huge
);
359 mpz_set_si (e
->value
.integer
, init
);
364 mpz_set_ui (e
->value
.integer
, 0);
365 else if (init
== INT_MAX
)
366 mpz_set (e
->value
.integer
, gfc_unsigned_kinds
[i
].huge
);
368 mpz_set_ui (e
->value
.integer
, init
);
374 mpfr_set (e
->value
.real
, gfc_real_kinds
[i
].huge
, GFC_RND_MODE
);
375 mpfr_neg (e
->value
.real
, e
->value
.real
, GFC_RND_MODE
);
377 else if (init
== INT_MAX
)
378 mpfr_set (e
->value
.real
, gfc_real_kinds
[i
].huge
, GFC_RND_MODE
);
380 mpfr_set_si (e
->value
.real
, init
, GFC_RND_MODE
);
384 mpc_set_si (e
->value
.complex, init
, GFC_MPC_RND_MODE
);
390 gfc_expr
*len
= gfc_simplify_len (array
, NULL
);
391 gfc_extract_hwi (len
, &length
);
392 string
= gfc_get_wide_string (length
+ 1);
393 gfc_wide_memset (string
, 0, length
);
395 else if (init
== INT_MAX
)
397 gfc_expr
*len
= gfc_simplify_len (array
, NULL
);
398 gfc_extract_hwi (len
, &length
);
399 string
= gfc_get_wide_string (length
+ 1);
400 gfc_wide_memset (string
, 255, length
);
405 string
= gfc_get_wide_string (1);
408 string
[length
] = '\0';
409 e
->value
.character
.length
= length
;
410 e
->value
.character
.string
= string
;
422 /* Helper function for gfc_simplify_dot_product() and gfc_simplify_matmul;
423 if conj_a is true, the matrix_a is complex conjugated. */
426 compute_dot_product (gfc_expr
*matrix_a
, int stride_a
, int offset_a
,
427 gfc_expr
*matrix_b
, int stride_b
, int offset_b
,
430 gfc_expr
*result
, *a
, *b
, *c
;
432 /* Set result to an UNSIGNED of correct kind for unsigned,
433 INTEGER(1) 0 for other numeric types, and .false. for
434 LOGICAL. Mixed-mode math in the loop will promote result to the
435 correct type and kind. */
436 if (matrix_a
->ts
.type
== BT_LOGICAL
)
437 result
= gfc_get_logical_expr (gfc_default_logical_kind
, NULL
, false);
438 else if (matrix_a
->ts
.type
== BT_UNSIGNED
)
440 int kind
= MAX (matrix_a
->ts
.kind
, matrix_b
->ts
.kind
);
441 result
= gfc_get_unsigned_expr (kind
, NULL
, 0);
444 result
= gfc_get_int_expr (1, NULL
, 0);
446 result
->where
= matrix_a
->where
;
448 a
= gfc_constructor_lookup_expr (matrix_a
->value
.constructor
, offset_a
);
449 b
= gfc_constructor_lookup_expr (matrix_b
->value
.constructor
, offset_b
);
452 /* Copying of expressions is required as operands are free'd
453 by the gfc_arith routines. */
454 switch (result
->ts
.type
)
457 result
= gfc_or (result
,
458 gfc_and (gfc_copy_expr (a
),
466 if (conj_a
&& a
->ts
.type
== BT_COMPLEX
)
467 c
= gfc_simplify_conjg (a
);
469 c
= gfc_copy_expr (a
);
470 result
= gfc_add (result
, gfc_multiply (c
, gfc_copy_expr (b
)));
477 offset_a
+= stride_a
;
478 a
= gfc_constructor_lookup_expr (matrix_a
->value
.constructor
, offset_a
);
480 offset_b
+= stride_b
;
481 b
= gfc_constructor_lookup_expr (matrix_b
->value
.constructor
, offset_b
);
488 /* Build a result expression for transformational intrinsics,
492 transformational_result (gfc_expr
*array
, gfc_expr
*dim
, bt type
,
493 int kind
, locus
* where
)
498 if (!dim
|| array
->rank
== 1)
499 return gfc_get_constant_expr (type
, kind
, where
);
501 result
= gfc_get_array_expr (type
, kind
, where
);
502 result
->shape
= gfc_copy_shape_excluding (array
->shape
, array
->rank
, dim
);
503 result
->rank
= array
->rank
- 1;
505 /* gfc_array_size() would count the number of elements in the constructor,
506 we have not built those yet. */
508 for (i
= 0; i
< result
->rank
; ++i
)
509 nelem
*= mpz_get_ui (result
->shape
[i
]);
511 for (i
= 0; i
< nelem
; ++i
)
513 gfc_constructor_append_expr (&result
->value
.constructor
,
514 gfc_get_constant_expr (type
, kind
, where
),
522 typedef gfc_expr
* (*transformational_op
)(gfc_expr
*, gfc_expr
*);
524 /* Wrapper function, implements 'op1 += 1'. Only called if MASK
525 of COUNT intrinsic is .TRUE..
527 Interface and implementation mimics arith functions as
528 gfc_add, gfc_multiply, etc. */
531 gfc_count (gfc_expr
*op1
, gfc_expr
*op2
)
535 gcc_assert (op1
->ts
.type
== BT_INTEGER
);
536 gcc_assert (op2
->ts
.type
== BT_LOGICAL
);
537 gcc_assert (op2
->value
.logical
);
539 result
= gfc_copy_expr (op1
);
540 mpz_add_ui (result
->value
.integer
, result
->value
.integer
, 1);
548 /* Transforms an ARRAY with operation OP, according to MASK, to a
549 scalar RESULT. E.g. called if
551 REAL, PARAMETER :: array(n, m) = ...
552 REAL, PARAMETER :: s = SUM(array)
554 where OP == gfc_add(). */
557 simplify_transformation_to_scalar (gfc_expr
*result
, gfc_expr
*array
, gfc_expr
*mask
,
558 transformational_op op
)
561 gfc_constructor
*array_ctor
, *mask_ctor
;
563 /* Shortcut for constant .FALSE. MASK. */
565 && mask
->expr_type
== EXPR_CONSTANT
566 && !mask
->value
.logical
)
569 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
571 if (mask
&& mask
->expr_type
== EXPR_ARRAY
)
572 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
576 a
= array_ctor
->expr
;
577 array_ctor
= gfc_constructor_next (array_ctor
);
579 /* A constant MASK equals .TRUE. here and can be ignored. */
583 mask_ctor
= gfc_constructor_next (mask_ctor
);
584 if (!m
->value
.logical
)
588 result
= op (result
, gfc_copy_expr (a
));
596 /* Transforms an ARRAY with operation OP, according to MASK, to an
597 array RESULT. E.g. called if
599 REAL, PARAMETER :: array(n, m) = ...
600 REAL, PARAMETER :: s(n) = PROD(array, DIM=1)
602 where OP == gfc_multiply().
603 The result might be post processed using post_op. */
606 simplify_transformation_to_array (gfc_expr
*result
, gfc_expr
*array
, gfc_expr
*dim
,
607 gfc_expr
*mask
, transformational_op op
,
608 transformational_op post_op
)
611 int done
, i
, n
, arraysize
, resultsize
, dim_index
, dim_extent
, dim_stride
;
612 gfc_expr
**arrayvec
, **resultvec
, **base
, **src
, **dest
;
613 gfc_constructor
*array_ctor
, *mask_ctor
, *result_ctor
;
615 int count
[GFC_MAX_DIMENSIONS
], extent
[GFC_MAX_DIMENSIONS
],
616 sstride
[GFC_MAX_DIMENSIONS
], dstride
[GFC_MAX_DIMENSIONS
],
617 tmpstride
[GFC_MAX_DIMENSIONS
];
619 /* Shortcut for constant .FALSE. MASK. */
621 && mask
->expr_type
== EXPR_CONSTANT
622 && !mask
->value
.logical
)
625 /* Build an indexed table for array element expressions to minimize
626 linked-list traversal. Masked elements are set to NULL. */
627 gfc_array_size (array
, &size
);
628 arraysize
= mpz_get_ui (size
);
631 arrayvec
= XCNEWVEC (gfc_expr
*, arraysize
);
633 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
635 if (mask
&& mask
->expr_type
== EXPR_ARRAY
)
636 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
638 for (i
= 0; i
< arraysize
; ++i
)
640 arrayvec
[i
] = array_ctor
->expr
;
641 array_ctor
= gfc_constructor_next (array_ctor
);
645 if (!mask_ctor
->expr
->value
.logical
)
648 mask_ctor
= gfc_constructor_next (mask_ctor
);
652 /* Same for the result expression. */
653 gfc_array_size (result
, &size
);
654 resultsize
= mpz_get_ui (size
);
657 resultvec
= XCNEWVEC (gfc_expr
*, resultsize
);
658 result_ctor
= gfc_constructor_first (result
->value
.constructor
);
659 for (i
= 0; i
< resultsize
; ++i
)
661 resultvec
[i
] = result_ctor
->expr
;
662 result_ctor
= gfc_constructor_next (result_ctor
);
665 gfc_extract_int (dim
, &dim_index
);
666 dim_index
-= 1; /* zero-base index */
670 for (i
= 0, n
= 0; i
< array
->rank
; ++i
)
673 tmpstride
[i
] = (i
== 0) ? 1 : tmpstride
[i
-1] * mpz_get_si (array
->shape
[i
-1]);
676 dim_extent
= mpz_get_si (array
->shape
[i
]);
677 dim_stride
= tmpstride
[i
];
681 extent
[n
] = mpz_get_si (array
->shape
[i
]);
682 sstride
[n
] = tmpstride
[i
];
683 dstride
[n
] = (n
== 0) ? 1 : dstride
[n
-1] * extent
[n
-1];
687 done
= resultsize
<= 0;
692 for (src
= base
, n
= 0; n
< dim_extent
; src
+= dim_stride
, ++n
)
694 *dest
= op (*dest
, gfc_copy_expr (*src
));
697 *dest
= post_op (*dest
, *dest
);
704 while (!done
&& count
[n
] == extent
[n
])
707 base
-= sstride
[n
] * extent
[n
];
708 dest
-= dstride
[n
] * extent
[n
];
711 if (n
< result
->rank
)
713 /* If the nested loop is unrolled GFC_MAX_DIMENSIONS
714 times, we'd warn for the last iteration, because the
715 array index will have already been incremented to the
716 array sizes, and we can't tell that this must make
717 the test against result->rank false, because ranks
718 must not exceed GFC_MAX_DIMENSIONS. */
719 GCC_DIAGNOSTIC_PUSH_IGNORED (-Warray
-bounds
)
730 /* Place updated expression in result constructor. */
731 result_ctor
= gfc_constructor_first (result
->value
.constructor
);
732 for (i
= 0; i
< resultsize
; ++i
)
734 result_ctor
->expr
= resultvec
[i
];
735 result_ctor
= gfc_constructor_next (result_ctor
);
745 simplify_transformation (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
,
746 int init_val
, transformational_op op
)
751 size_zero
= gfc_is_size_zero_array (array
);
753 if (!(is_constant_array_expr (array
) || size_zero
)
754 || array
->shape
== NULL
755 || !gfc_is_constant_expr (dim
))
759 && !is_constant_array_expr (mask
)
760 && mask
->expr_type
!= EXPR_CONSTANT
)
763 result
= transformational_result (array
, dim
, array
->ts
.type
,
764 array
->ts
.kind
, &array
->where
);
765 init_result_expr (result
, init_val
, array
);
770 return !dim
|| array
->rank
== 1 ?
771 simplify_transformation_to_scalar (result
, array
, mask
, op
) :
772 simplify_transformation_to_array (result
, array
, dim
, mask
, op
, NULL
);
776 /********************** Simplification functions *****************************/
779 gfc_simplify_abs (gfc_expr
*e
)
783 if (e
->expr_type
!= EXPR_CONSTANT
)
789 result
= gfc_get_constant_expr (BT_INTEGER
, e
->ts
.kind
, &e
->where
);
790 mpz_abs (result
->value
.integer
, e
->value
.integer
);
791 return range_check (result
, "IABS");
794 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
795 mpfr_abs (result
->value
.real
, e
->value
.real
, GFC_RND_MODE
);
796 return range_check (result
, "ABS");
799 gfc_set_model_kind (e
->ts
.kind
);
800 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
801 mpc_abs (result
->value
.real
, e
->value
.complex, GFC_RND_MODE
);
802 return range_check (result
, "CABS");
805 gfc_internal_error ("gfc_simplify_abs(): Bad type");
811 simplify_achar_char (gfc_expr
*e
, gfc_expr
*k
, const char *name
, bool ascii
)
815 bool too_large
= false;
817 if (e
->expr_type
!= EXPR_CONSTANT
)
820 kind
= get_kind (BT_CHARACTER
, k
, name
, gfc_default_character_kind
);
822 return &gfc_bad_expr
;
824 if (mpz_cmp_si (e
->value
.integer
, 0) < 0)
826 gfc_error ("Argument of %s function at %L is negative", name
,
828 return &gfc_bad_expr
;
831 if (ascii
&& warn_surprising
&& mpz_cmp_si (e
->value
.integer
, 127) > 0)
832 gfc_warning (OPT_Wsurprising
,
833 "Argument of %s function at %L outside of range [0,127]",
836 if (kind
== 1 && mpz_cmp_si (e
->value
.integer
, 255) > 0)
841 mpz_init_set_ui (t
, 2);
842 mpz_pow_ui (t
, t
, 32);
843 mpz_sub_ui (t
, t
, 1);
844 if (mpz_cmp (e
->value
.integer
, t
) > 0)
851 gfc_error ("Argument of %s function at %L is too large for the "
852 "collating sequence of kind %d", name
, &e
->where
, kind
);
853 return &gfc_bad_expr
;
856 result
= gfc_get_character_expr (kind
, &e
->where
, NULL
, 1);
857 result
->value
.character
.string
[0] = mpz_get_ui (e
->value
.integer
);
864 /* We use the processor's collating sequence, because all
865 systems that gfortran currently works on are ASCII. */
868 gfc_simplify_achar (gfc_expr
*e
, gfc_expr
*k
)
870 return simplify_achar_char (e
, k
, "ACHAR", true);
875 gfc_simplify_acos (gfc_expr
*x
)
879 if (x
->expr_type
!= EXPR_CONSTANT
)
885 if (mpfr_cmp_si (x
->value
.real
, 1) > 0
886 || mpfr_cmp_si (x
->value
.real
, -1) < 0)
888 gfc_error ("Argument of ACOS at %L must be between -1 and 1",
890 return &gfc_bad_expr
;
892 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
893 mpfr_acos (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
897 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
898 mpc_acos (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
902 gfc_internal_error ("in gfc_simplify_acos(): Bad type");
905 return range_check (result
, "ACOS");
909 gfc_simplify_acosh (gfc_expr
*x
)
913 if (x
->expr_type
!= EXPR_CONSTANT
)
919 if (mpfr_cmp_si (x
->value
.real
, 1) < 0)
921 gfc_error ("Argument of ACOSH at %L must not be less than 1",
923 return &gfc_bad_expr
;
926 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
927 mpfr_acosh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
931 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
932 mpc_acosh (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
936 gfc_internal_error ("in gfc_simplify_acosh(): Bad type");
939 return range_check (result
, "ACOSH");
943 gfc_simplify_adjustl (gfc_expr
*e
)
949 if (e
->expr_type
!= EXPR_CONSTANT
)
952 len
= e
->value
.character
.length
;
954 for (count
= 0, i
= 0; i
< len
; ++i
)
956 ch
= e
->value
.character
.string
[i
];
962 result
= gfc_get_character_expr (e
->ts
.kind
, &e
->where
, NULL
, len
);
963 for (i
= 0; i
< len
- count
; ++i
)
964 result
->value
.character
.string
[i
] = e
->value
.character
.string
[count
+ i
];
971 gfc_simplify_adjustr (gfc_expr
*e
)
977 if (e
->expr_type
!= EXPR_CONSTANT
)
980 len
= e
->value
.character
.length
;
982 for (count
= 0, i
= len
- 1; i
>= 0; --i
)
984 ch
= e
->value
.character
.string
[i
];
990 result
= gfc_get_character_expr (e
->ts
.kind
, &e
->where
, NULL
, len
);
991 for (i
= 0; i
< count
; ++i
)
992 result
->value
.character
.string
[i
] = ' ';
994 for (i
= count
; i
< len
; ++i
)
995 result
->value
.character
.string
[i
] = e
->value
.character
.string
[i
- count
];
1002 gfc_simplify_aimag (gfc_expr
*e
)
1006 if (e
->expr_type
!= EXPR_CONSTANT
)
1009 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
1010 mpfr_set (result
->value
.real
, mpc_imagref (e
->value
.complex), GFC_RND_MODE
);
1012 return range_check (result
, "AIMAG");
1017 gfc_simplify_aint (gfc_expr
*e
, gfc_expr
*k
)
1019 gfc_expr
*rtrunc
, *result
;
1022 kind
= get_kind (BT_REAL
, k
, "AINT", e
->ts
.kind
);
1024 return &gfc_bad_expr
;
1026 if (e
->expr_type
!= EXPR_CONSTANT
)
1029 rtrunc
= gfc_copy_expr (e
);
1030 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
1032 result
= gfc_real2real (rtrunc
, kind
);
1034 gfc_free_expr (rtrunc
);
1036 return range_check (result
, "AINT");
1041 gfc_simplify_all (gfc_expr
*mask
, gfc_expr
*dim
)
1043 return simplify_transformation (mask
, dim
, NULL
, true, gfc_and
);
1048 gfc_simplify_dint (gfc_expr
*e
)
1050 gfc_expr
*rtrunc
, *result
;
1052 if (e
->expr_type
!= EXPR_CONSTANT
)
1055 rtrunc
= gfc_copy_expr (e
);
1056 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
1058 result
= gfc_real2real (rtrunc
, gfc_default_double_kind
);
1060 gfc_free_expr (rtrunc
);
1062 return range_check (result
, "DINT");
1067 gfc_simplify_dreal (gfc_expr
*e
)
1069 gfc_expr
*result
= NULL
;
1071 if (e
->expr_type
!= EXPR_CONSTANT
)
1074 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
1075 mpc_real (result
->value
.real
, e
->value
.complex, GFC_RND_MODE
);
1077 return range_check (result
, "DREAL");
1082 gfc_simplify_anint (gfc_expr
*e
, gfc_expr
*k
)
1087 kind
= get_kind (BT_REAL
, k
, "ANINT", e
->ts
.kind
);
1089 return &gfc_bad_expr
;
1091 if (e
->expr_type
!= EXPR_CONSTANT
)
1094 result
= gfc_get_constant_expr (e
->ts
.type
, kind
, &e
->where
);
1095 mpfr_round (result
->value
.real
, e
->value
.real
);
1097 return range_check (result
, "ANINT");
1102 gfc_simplify_and (gfc_expr
*x
, gfc_expr
*y
)
1107 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1110 kind
= x
->ts
.kind
> y
->ts
.kind
? x
->ts
.kind
: y
->ts
.kind
;
1115 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &x
->where
);
1116 mpz_and (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
1117 return range_check (result
, "AND");
1120 return gfc_get_logical_expr (kind
, &x
->where
,
1121 x
->value
.logical
&& y
->value
.logical
);
1130 gfc_simplify_any (gfc_expr
*mask
, gfc_expr
*dim
)
1132 return simplify_transformation (mask
, dim
, NULL
, false, gfc_or
);
1137 gfc_simplify_dnint (gfc_expr
*e
)
1141 if (e
->expr_type
!= EXPR_CONSTANT
)
1144 result
= gfc_get_constant_expr (BT_REAL
, gfc_default_double_kind
, &e
->where
);
1145 mpfr_round (result
->value
.real
, e
->value
.real
);
1147 return range_check (result
, "DNINT");
1152 gfc_simplify_asin (gfc_expr
*x
)
1156 if (x
->expr_type
!= EXPR_CONSTANT
)
1162 if (mpfr_cmp_si (x
->value
.real
, 1) > 0
1163 || mpfr_cmp_si (x
->value
.real
, -1) < 0)
1165 gfc_error ("Argument of ASIN at %L must be between -1 and 1",
1167 return &gfc_bad_expr
;
1169 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1170 mpfr_asin (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1174 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1175 mpc_asin (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1179 gfc_internal_error ("in gfc_simplify_asin(): Bad type");
1182 return range_check (result
, "ASIN");
1186 /* Convert radians to degrees, i.e., x * 180 / pi. */
1194 mpfr_const_pi (tmp
, GFC_RND_MODE
);
1195 mpfr_mul_ui (x
, x
, 180, GFC_RND_MODE
);
1196 mpfr_div (x
, x
, tmp
, GFC_RND_MODE
);
1201 /* Simplify ACOSD(X) where the returned value has units of degree. */
1204 gfc_simplify_acosd (gfc_expr
*x
)
1208 if (x
->expr_type
!= EXPR_CONSTANT
)
1211 if (mpfr_cmp_si (x
->value
.real
, 1) > 0
1212 || mpfr_cmp_si (x
->value
.real
, -1) < 0)
1214 gfc_error ("Argument of ACOSD at %L must be between -1 and 1",
1216 return &gfc_bad_expr
;
1219 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1220 mpfr_acos (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1221 rad2deg (result
->value
.real
);
1223 return range_check (result
, "ACOSD");
1227 /* Simplify asind (x) where the returned value has units of degree. */
1230 gfc_simplify_asind (gfc_expr
*x
)
1234 if (x
->expr_type
!= EXPR_CONSTANT
)
1237 if (mpfr_cmp_si (x
->value
.real
, 1) > 0
1238 || mpfr_cmp_si (x
->value
.real
, -1) < 0)
1240 gfc_error ("Argument of ASIND at %L must be between -1 and 1",
1242 return &gfc_bad_expr
;
1245 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1246 mpfr_asin (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1247 rad2deg (result
->value
.real
);
1249 return range_check (result
, "ASIND");
1253 /* Simplify atand (x) where the returned value has units of degree. */
1256 gfc_simplify_atand (gfc_expr
*x
)
1260 if (x
->expr_type
!= EXPR_CONSTANT
)
1263 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1264 mpfr_atan (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1265 rad2deg (result
->value
.real
);
1267 return range_check (result
, "ATAND");
1272 gfc_simplify_asinh (gfc_expr
*x
)
1276 if (x
->expr_type
!= EXPR_CONSTANT
)
1279 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1284 mpfr_asinh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1288 mpc_asinh (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1292 gfc_internal_error ("in gfc_simplify_asinh(): Bad type");
1295 return range_check (result
, "ASINH");
1300 gfc_simplify_atan (gfc_expr
*x
)
1304 if (x
->expr_type
!= EXPR_CONSTANT
)
1307 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1312 mpfr_atan (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1316 mpc_atan (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1320 gfc_internal_error ("in gfc_simplify_atan(): Bad type");
1323 return range_check (result
, "ATAN");
1328 gfc_simplify_atanh (gfc_expr
*x
)
1332 if (x
->expr_type
!= EXPR_CONSTANT
)
1338 if (mpfr_cmp_si (x
->value
.real
, 1) >= 0
1339 || mpfr_cmp_si (x
->value
.real
, -1) <= 0)
1341 gfc_error ("Argument of ATANH at %L must be inside the range -1 "
1343 return &gfc_bad_expr
;
1345 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1346 mpfr_atanh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1350 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1351 mpc_atanh (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1355 gfc_internal_error ("in gfc_simplify_atanh(): Bad type");
1358 return range_check (result
, "ATANH");
1363 gfc_simplify_atan2 (gfc_expr
*y
, gfc_expr
*x
)
1367 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1370 if (mpfr_zero_p (y
->value
.real
) && mpfr_zero_p (x
->value
.real
))
1372 gfc_error ("If first argument of ATAN2 at %L is zero, then the "
1373 "second argument must not be zero", &y
->where
);
1374 return &gfc_bad_expr
;
1377 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1378 mpfr_atan2 (result
->value
.real
, y
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1380 return range_check (result
, "ATAN2");
1385 gfc_simplify_bessel_j0 (gfc_expr
*x
)
1389 if (x
->expr_type
!= EXPR_CONSTANT
)
1392 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1393 mpfr_j0 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1395 return range_check (result
, "BESSEL_J0");
1400 gfc_simplify_bessel_j1 (gfc_expr
*x
)
1404 if (x
->expr_type
!= EXPR_CONSTANT
)
1407 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1408 mpfr_j1 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1410 return range_check (result
, "BESSEL_J1");
1415 gfc_simplify_bessel_jn (gfc_expr
*order
, gfc_expr
*x
)
1420 if (x
->expr_type
!= EXPR_CONSTANT
|| order
->expr_type
!= EXPR_CONSTANT
)
1423 n
= mpz_get_si (order
->value
.integer
);
1424 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1425 mpfr_jn (result
->value
.real
, n
, x
->value
.real
, GFC_RND_MODE
);
1427 return range_check (result
, "BESSEL_JN");
1431 /* Simplify transformational form of JN and YN. */
1434 gfc_simplify_bessel_n2 (gfc_expr
*order1
, gfc_expr
*order2
, gfc_expr
*x
,
1441 mpfr_t x2rev
, last1
, last2
;
1443 if (x
->expr_type
!= EXPR_CONSTANT
|| order1
->expr_type
!= EXPR_CONSTANT
1444 || order2
->expr_type
!= EXPR_CONSTANT
)
1447 n1
= mpz_get_si (order1
->value
.integer
);
1448 n2
= mpz_get_si (order2
->value
.integer
);
1449 result
= gfc_get_array_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1451 result
->shape
= gfc_get_shape (1);
1452 mpz_init_set_ui (result
->shape
[0], MAX (n2
-n1
+1, 0));
1457 /* Special case: x == 0; it is J0(0.0) == 1, JN(N > 0, 0.0) == 0; and
1458 YN(N, 0.0) = -Inf. */
1460 if (mpfr_cmp_ui (x
->value
.real
, 0.0) == 0)
1462 if (!jn
&& flag_range_check
)
1464 gfc_error ("Result of BESSEL_YN is -INF at %L", &result
->where
);
1465 gfc_free_expr (result
);
1466 return &gfc_bad_expr
;
1471 e
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1472 mpfr_set_ui (e
->value
.real
, 1, GFC_RND_MODE
);
1473 gfc_constructor_append_expr (&result
->value
.constructor
, e
,
1478 for (i
= n1
; i
<= n2
; i
++)
1480 e
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1482 mpfr_set_ui (e
->value
.real
, 0, GFC_RND_MODE
);
1484 mpfr_set_inf (e
->value
.real
, -1);
1485 gfc_constructor_append_expr (&result
->value
.constructor
, e
,
1492 /* Use the faster but more verbose recurrence algorithm. Bessel functions
1493 are stable for downward recursion and Neumann functions are stable
1494 for upward recursion. It is
1496 J(N-1, x) = x2rev * N * J(N, x) - J(N+1, x),
1497 Y(N+1, x) = x2rev * N * Y(N, x) - Y(N-1, x).
1498 Cf. http://dlmf.nist.gov/10.74#iv and http://dlmf.nist.gov/10.6#E1 */
1500 gfc_set_model_kind (x
->ts
.kind
);
1502 /* Get first recursion anchor. */
1506 mpfr_jn (last1
, n2
, x
->value
.real
, GFC_RND_MODE
);
1508 mpfr_yn (last1
, n1
, x
->value
.real
, GFC_RND_MODE
);
1510 e
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1511 mpfr_set (e
->value
.real
, last1
, GFC_RND_MODE
);
1512 if (range_check (e
, jn
? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr
)
1516 gfc_free_expr (result
);
1517 return &gfc_bad_expr
;
1519 gfc_constructor_append_expr (&result
->value
.constructor
, e
, &x
->where
);
1527 /* Get second recursion anchor. */
1531 mpfr_jn (last2
, n2
-1, x
->value
.real
, GFC_RND_MODE
);
1533 mpfr_yn (last2
, n1
+1, x
->value
.real
, GFC_RND_MODE
);
1535 e
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1536 mpfr_set (e
->value
.real
, last2
, GFC_RND_MODE
);
1537 if (range_check (e
, jn
? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr
)
1542 gfc_free_expr (result
);
1543 return &gfc_bad_expr
;
1546 gfc_constructor_insert_expr (&result
->value
.constructor
, e
, &x
->where
, -2);
1548 gfc_constructor_append_expr (&result
->value
.constructor
, e
, &x
->where
);
1557 /* Start actual recursion. */
1560 mpfr_ui_div (x2rev
, 2, x
->value
.real
, GFC_RND_MODE
);
1562 for (i
= 2; i
<= n2
-n1
; i
++)
1564 e
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1566 /* Special case: For YN, if the previous N gave -INF, set
1567 also N+1 to -INF. */
1568 if (!jn
&& !flag_range_check
&& mpfr_inf_p (last2
))
1570 mpfr_set_inf (e
->value
.real
, -1);
1571 gfc_constructor_append_expr (&result
->value
.constructor
, e
,
1576 mpfr_mul_si (e
->value
.real
, x2rev
, jn
? (n2
-i
+1) : (n1
+i
-1),
1578 mpfr_mul (e
->value
.real
, e
->value
.real
, last2
, GFC_RND_MODE
);
1579 mpfr_sub (e
->value
.real
, e
->value
.real
, last1
, GFC_RND_MODE
);
1581 if (range_check (e
, jn
? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr
)
1583 /* Range_check frees "e" in that case. */
1589 gfc_constructor_insert_expr (&result
->value
.constructor
, e
, &x
->where
,
1592 gfc_constructor_append_expr (&result
->value
.constructor
, e
, &x
->where
);
1594 mpfr_set (last1
, last2
, GFC_RND_MODE
);
1595 mpfr_set (last2
, e
->value
.real
, GFC_RND_MODE
);
1608 gfc_free_expr (result
);
1609 return &gfc_bad_expr
;
1614 gfc_simplify_bessel_jn2 (gfc_expr
*order1
, gfc_expr
*order2
, gfc_expr
*x
)
1616 return gfc_simplify_bessel_n2 (order1
, order2
, x
, true);
1621 gfc_simplify_bessel_y0 (gfc_expr
*x
)
1625 if (x
->expr_type
!= EXPR_CONSTANT
)
1628 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1629 mpfr_y0 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1631 return range_check (result
, "BESSEL_Y0");
1636 gfc_simplify_bessel_y1 (gfc_expr
*x
)
1640 if (x
->expr_type
!= EXPR_CONSTANT
)
1643 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1644 mpfr_y1 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1646 return range_check (result
, "BESSEL_Y1");
1651 gfc_simplify_bessel_yn (gfc_expr
*order
, gfc_expr
*x
)
1656 if (x
->expr_type
!= EXPR_CONSTANT
|| order
->expr_type
!= EXPR_CONSTANT
)
1659 n
= mpz_get_si (order
->value
.integer
);
1660 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1661 mpfr_yn (result
->value
.real
, n
, x
->value
.real
, GFC_RND_MODE
);
1663 return range_check (result
, "BESSEL_YN");
1668 gfc_simplify_bessel_yn2 (gfc_expr
*order1
, gfc_expr
*order2
, gfc_expr
*x
)
1670 return gfc_simplify_bessel_n2 (order1
, order2
, x
, false);
1675 gfc_simplify_bit_size (gfc_expr
*e
)
1677 int i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
1680 if (flag_unsigned
&& e
->ts
.type
== BT_UNSIGNED
)
1681 bit_size
= gfc_unsigned_kinds
[i
].bit_size
;
1683 bit_size
= gfc_integer_kinds
[i
].bit_size
;
1685 return gfc_get_int_expr (e
->ts
.kind
, &e
->where
, bit_size
);
1690 gfc_simplify_btest (gfc_expr
*e
, gfc_expr
*bit
)
1694 if (e
->expr_type
!= EXPR_CONSTANT
|| bit
->expr_type
!= EXPR_CONSTANT
)
1697 if (!gfc_check_bitfcn (e
, bit
))
1698 return &gfc_bad_expr
;
1700 if (gfc_extract_int (bit
, &b
) || b
< 0)
1701 return gfc_get_logical_expr (gfc_default_logical_kind
, &e
->where
, false);
1703 return gfc_get_logical_expr (gfc_default_logical_kind
, &e
->where
,
1704 mpz_tstbit (e
->value
.integer
, b
));
1709 compare_bitwise (gfc_expr
*i
, gfc_expr
*j
)
1714 gcc_assert (i
->ts
.type
== BT_INTEGER
);
1715 gcc_assert (j
->ts
.type
== BT_INTEGER
);
1717 mpz_init_set (x
, i
->value
.integer
);
1718 k
= gfc_validate_kind (i
->ts
.type
, i
->ts
.kind
, false);
1719 gfc_convert_mpz_to_unsigned (x
, gfc_integer_kinds
[k
].bit_size
);
1721 mpz_init_set (y
, j
->value
.integer
);
1722 k
= gfc_validate_kind (j
->ts
.type
, j
->ts
.kind
, false);
1723 gfc_convert_mpz_to_unsigned (y
, gfc_integer_kinds
[k
].bit_size
);
1725 res
= mpz_cmp (x
, y
);
1733 gfc_simplify_bge (gfc_expr
*i
, gfc_expr
*j
)
1737 if (i
->expr_type
!= EXPR_CONSTANT
|| j
->expr_type
!= EXPR_CONSTANT
)
1740 if (flag_unsigned
&& i
->ts
.type
== BT_UNSIGNED
)
1741 result
= mpz_cmp (i
->value
.integer
, j
->value
.integer
) >= 0;
1743 result
= compare_bitwise (i
, j
) >= 0;
1745 return gfc_get_logical_expr (gfc_default_logical_kind
, &i
->where
,
1751 gfc_simplify_bgt (gfc_expr
*i
, gfc_expr
*j
)
1755 if (i
->expr_type
!= EXPR_CONSTANT
|| j
->expr_type
!= EXPR_CONSTANT
)
1758 if (flag_unsigned
&& i
->ts
.type
== BT_UNSIGNED
)
1759 result
= mpz_cmp (i
->value
.integer
, j
->value
.integer
) > 0;
1761 result
= compare_bitwise (i
, j
) > 0;
1763 return gfc_get_logical_expr (gfc_default_logical_kind
, &i
->where
,
1769 gfc_simplify_ble (gfc_expr
*i
, gfc_expr
*j
)
1773 if (i
->expr_type
!= EXPR_CONSTANT
|| j
->expr_type
!= EXPR_CONSTANT
)
1776 if (flag_unsigned
&& i
->ts
.type
== BT_UNSIGNED
)
1777 result
= mpz_cmp (i
->value
.integer
, j
->value
.integer
) <= 0;
1779 result
= compare_bitwise (i
, j
) <= 0;
1781 return gfc_get_logical_expr (gfc_default_logical_kind
, &i
->where
,
1787 gfc_simplify_blt (gfc_expr
*i
, gfc_expr
*j
)
1791 if (i
->expr_type
!= EXPR_CONSTANT
|| j
->expr_type
!= EXPR_CONSTANT
)
1794 if (flag_unsigned
&& i
->ts
.type
== BT_UNSIGNED
)
1795 result
= mpz_cmp (i
->value
.integer
, j
->value
.integer
) < 0;
1797 result
= compare_bitwise (i
, j
) < 0;
1799 return gfc_get_logical_expr (gfc_default_logical_kind
, &i
->where
,
1804 gfc_simplify_ceiling (gfc_expr
*e
, gfc_expr
*k
)
1806 gfc_expr
*ceil
, *result
;
1809 kind
= get_kind (BT_INTEGER
, k
, "CEILING", gfc_default_integer_kind
);
1811 return &gfc_bad_expr
;
1813 if (e
->expr_type
!= EXPR_CONSTANT
)
1816 ceil
= gfc_copy_expr (e
);
1817 mpfr_ceil (ceil
->value
.real
, e
->value
.real
);
1819 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &e
->where
);
1820 gfc_mpfr_to_mpz (result
->value
.integer
, ceil
->value
.real
, &e
->where
);
1822 gfc_free_expr (ceil
);
1824 return range_check (result
, "CEILING");
1829 gfc_simplify_char (gfc_expr
*e
, gfc_expr
*k
)
1831 return simplify_achar_char (e
, k
, "CHAR", false);
1835 /* Common subroutine for simplifying CMPLX, COMPLEX and DCMPLX. */
1838 simplify_cmplx (const char *name
, gfc_expr
*x
, gfc_expr
*y
, int kind
)
1842 if (x
->expr_type
!= EXPR_CONSTANT
1843 || (y
!= NULL
&& y
->expr_type
!= EXPR_CONSTANT
))
1846 result
= gfc_get_constant_expr (BT_COMPLEX
, kind
, &x
->where
);
1852 mpc_set_z (result
->value
.complex, x
->value
.integer
, GFC_MPC_RND_MODE
);
1856 mpc_set_fr (result
->value
.complex, x
->value
.real
, GFC_RND_MODE
);
1860 mpc_set (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1864 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (x)");
1868 return range_check (result
, name
);
1874 mpfr_set_z (mpc_imagref (result
->value
.complex),
1875 y
->value
.integer
, GFC_RND_MODE
);
1879 mpfr_set (mpc_imagref (result
->value
.complex),
1880 y
->value
.real
, GFC_RND_MODE
);
1884 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (y)");
1887 return range_check (result
, name
);
1892 gfc_simplify_cmplx (gfc_expr
*x
, gfc_expr
*y
, gfc_expr
*k
)
1896 kind
= get_kind (BT_REAL
, k
, "CMPLX", gfc_default_complex_kind
);
1898 return &gfc_bad_expr
;
1900 return simplify_cmplx ("CMPLX", x
, y
, kind
);
1905 gfc_simplify_complex (gfc_expr
*x
, gfc_expr
*y
)
1909 if (x
->ts
.type
== BT_INTEGER
&& y
->ts
.type
== BT_INTEGER
)
1910 kind
= gfc_default_complex_kind
;
1911 else if (x
->ts
.type
== BT_REAL
|| y
->ts
.type
== BT_INTEGER
)
1913 else if (x
->ts
.type
== BT_INTEGER
|| y
->ts
.type
== BT_REAL
)
1915 else if (x
->ts
.type
== BT_REAL
&& y
->ts
.type
== BT_REAL
)
1916 kind
= (x
->ts
.kind
> y
->ts
.kind
) ? x
->ts
.kind
: y
->ts
.kind
;
1920 return simplify_cmplx ("COMPLEX", x
, y
, kind
);
1925 gfc_simplify_conjg (gfc_expr
*e
)
1929 if (e
->expr_type
!= EXPR_CONSTANT
)
1932 result
= gfc_copy_expr (e
);
1933 mpc_conj (result
->value
.complex, result
->value
.complex, GFC_MPC_RND_MODE
);
1935 return range_check (result
, "CONJG");
1939 /* Simplify atan2d (x) where the unit is degree. */
1942 gfc_simplify_atan2d (gfc_expr
*y
, gfc_expr
*x
)
1946 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1949 if (mpfr_zero_p (y
->value
.real
) && mpfr_zero_p (x
->value
.real
))
1951 gfc_error ("If first argument of ATAN2D at %L is zero, then the "
1952 "second argument must not be zero", &y
->where
);
1953 return &gfc_bad_expr
;
1956 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1957 mpfr_atan2 (result
->value
.real
, y
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1958 rad2deg (result
->value
.real
);
1960 return range_check (result
, "ATAN2D");
1965 gfc_simplify_cos (gfc_expr
*x
)
1969 if (x
->expr_type
!= EXPR_CONSTANT
)
1972 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1977 mpfr_cos (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1981 gfc_set_model_kind (x
->ts
.kind
);
1982 mpc_cos (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1986 gfc_internal_error ("in gfc_simplify_cos(): Bad type");
1989 return range_check (result
, "COS");
1999 mpfr_const_pi (d2r
, GFC_RND_MODE
);
2000 mpfr_div_ui (d2r
, d2r
, 180, GFC_RND_MODE
);
2001 mpfr_mul (x
, x
, d2r
, GFC_RND_MODE
);
2006 /* Simplification routines for SIND, COSD, TAND. */
2007 #include "trigd_fe.inc"
2010 /* Simplify COSD(X) where X has the unit of degree. */
2013 gfc_simplify_cosd (gfc_expr
*x
)
2017 if (x
->expr_type
!= EXPR_CONSTANT
)
2020 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2021 mpfr_set (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
2022 simplify_cosd (result
->value
.real
);
2024 return range_check (result
, "COSD");
2028 /* Simplify SIND(X) where X has the unit of degree. */
2031 gfc_simplify_sind (gfc_expr
*x
)
2035 if (x
->expr_type
!= EXPR_CONSTANT
)
2038 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2039 mpfr_set (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
2040 simplify_sind (result
->value
.real
);
2042 return range_check (result
, "SIND");
2046 /* Simplify TAND(X) where X has the unit of degree. */
2049 gfc_simplify_tand (gfc_expr
*x
)
2053 if (x
->expr_type
!= EXPR_CONSTANT
)
2056 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2057 mpfr_set (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
2058 simplify_tand (result
->value
.real
);
2060 return range_check (result
, "TAND");
2064 /* Simplify COTAND(X) where X has the unit of degree. */
2067 gfc_simplify_cotand (gfc_expr
*x
)
2071 if (x
->expr_type
!= EXPR_CONSTANT
)
2074 /* Implement COTAND = -TAND(x+90).
2075 TAND offers correct exact values for multiples of 30 degrees.
2076 This implementation is also compatible with the behavior of some legacy
2077 compilers. Keep this consistent with gfc_conv_intrinsic_cotand. */
2078 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2079 mpfr_set (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
2080 mpfr_add_ui (result
->value
.real
, result
->value
.real
, 90, GFC_RND_MODE
);
2081 simplify_tand (result
->value
.real
);
2082 mpfr_neg (result
->value
.real
, result
->value
.real
, GFC_RND_MODE
);
2084 return range_check (result
, "COTAND");
2089 gfc_simplify_cosh (gfc_expr
*x
)
2093 if (x
->expr_type
!= EXPR_CONSTANT
)
2096 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2101 mpfr_cosh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
2105 mpc_cosh (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
2112 return range_check (result
, "COSH");
2117 gfc_simplify_count (gfc_expr
*mask
, gfc_expr
*dim
, gfc_expr
*kind
)
2122 size_zero
= gfc_is_size_zero_array (mask
);
2124 if (!(is_constant_array_expr (mask
) || size_zero
)
2125 || !gfc_is_constant_expr (dim
)
2126 || !gfc_is_constant_expr (kind
))
2129 result
= transformational_result (mask
, dim
,
2131 get_kind (BT_INTEGER
, kind
, "COUNT",
2132 gfc_default_integer_kind
),
2135 init_result_expr (result
, 0, NULL
);
2140 /* Passing MASK twice, once as data array, once as mask.
2141 Whenever gfc_count is called, '1' is added to the result. */
2142 return !dim
|| mask
->rank
== 1 ?
2143 simplify_transformation_to_scalar (result
, mask
, mask
, gfc_count
) :
2144 simplify_transformation_to_array (result
, mask
, dim
, mask
, gfc_count
, NULL
);
2147 /* Simplification routine for cshift. This works by copying the array
2148 expressions into a one-dimensional array, shuffling the values into another
2149 one-dimensional array and creating the new array expression from this. The
2150 shuffling part is basically taken from the library routine. */
2153 gfc_simplify_cshift (gfc_expr
*array
, gfc_expr
*shift
, gfc_expr
*dim
)
2157 gfc_expr
**arrayvec
, **resultvec
;
2158 gfc_expr
**rptr
, **sptr
;
2160 size_t arraysize
, shiftsize
, i
;
2161 gfc_constructor
*array_ctor
, *shift_ctor
;
2162 ssize_t
*shiftvec
, *hptr
;
2163 ssize_t shift_val
, len
;
2164 ssize_t count
[GFC_MAX_DIMENSIONS
], extent
[GFC_MAX_DIMENSIONS
],
2165 hs_ex
[GFC_MAX_DIMENSIONS
+ 1],
2166 hstride
[GFC_MAX_DIMENSIONS
], sstride
[GFC_MAX_DIMENSIONS
],
2167 a_extent
[GFC_MAX_DIMENSIONS
], a_stride
[GFC_MAX_DIMENSIONS
],
2168 h_extent
[GFC_MAX_DIMENSIONS
],
2169 ss_ex
[GFC_MAX_DIMENSIONS
+ 1];
2173 gfc_expr
**src
, **dest
;
2175 if (!is_constant_array_expr (array
))
2178 if (shift
->rank
> 0)
2179 gfc_simplify_expr (shift
, 1);
2181 if (!gfc_is_constant_expr (shift
))
2184 /* Make dim zero-based. */
2187 if (!gfc_is_constant_expr (dim
))
2189 which
= mpz_get_si (dim
->value
.integer
) - 1;
2194 if (array
->shape
== NULL
)
2197 gfc_array_size (array
, &size
);
2198 arraysize
= mpz_get_ui (size
);
2201 result
= gfc_get_array_expr (array
->ts
.type
, array
->ts
.kind
, &array
->where
);
2202 result
->shape
= gfc_copy_shape (array
->shape
, array
->rank
);
2203 result
->rank
= array
->rank
;
2204 result
->ts
.u
.derived
= array
->ts
.u
.derived
;
2209 arrayvec
= XCNEWVEC (gfc_expr
*, arraysize
);
2210 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
2211 for (i
= 0; i
< arraysize
; i
++)
2213 arrayvec
[i
] = array_ctor
->expr
;
2214 array_ctor
= gfc_constructor_next (array_ctor
);
2217 resultvec
= XCNEWVEC (gfc_expr
*, arraysize
);
2223 for (d
=0; d
< array
->rank
; d
++)
2225 a_extent
[d
] = mpz_get_si (array
->shape
[d
]);
2226 a_stride
[d
] = d
== 0 ? 1 : a_stride
[d
-1] * a_extent
[d
-1];
2229 if (shift
->rank
> 0)
2231 gfc_array_size (shift
, &size
);
2232 shiftsize
= mpz_get_ui (size
);
2234 shiftvec
= XCNEWVEC (ssize_t
, shiftsize
);
2235 shift_ctor
= gfc_constructor_first (shift
->value
.constructor
);
2236 for (d
= 0; d
< shift
->rank
; d
++)
2238 h_extent
[d
] = mpz_get_si (shift
->shape
[d
]);
2239 hstride
[d
] = d
== 0 ? 1 : hstride
[d
-1] * h_extent
[d
-1];
2245 /* Shut up compiler */
2250 for (d
=0; d
< array
->rank
; d
++)
2254 rsoffset
= a_stride
[d
];
2260 extent
[n
] = a_extent
[d
];
2261 sstride
[n
] = a_stride
[d
];
2262 ss_ex
[n
] = sstride
[n
] * extent
[n
];
2264 hs_ex
[n
] = hstride
[n
] * extent
[n
];
2273 for (i
= 0; i
< shiftsize
; i
++)
2276 val
= mpz_get_si (shift_ctor
->expr
->value
.integer
);
2281 shift_ctor
= gfc_constructor_next (shift_ctor
);
2287 shift_val
= mpz_get_si (shift
->value
.integer
);
2288 shift_val
= shift_val
% len
;
2293 continue_loop
= true;
2299 while (continue_loop
)
2307 src
= &sptr
[sh
* rsoffset
];
2309 for (n
= 0; n
< len
- sh
; n
++)
2316 for ( n
= 0; n
< sh
; n
++)
2328 while (count
[n
] == extent
[n
])
2338 continue_loop
= false;
2352 for (i
= 0; i
< arraysize
; i
++)
2354 gfc_constructor_append_expr (&result
->value
.constructor
,
2355 gfc_copy_expr (resultvec
[i
]),
2363 gfc_simplify_dcmplx (gfc_expr
*x
, gfc_expr
*y
)
2365 return simplify_cmplx ("DCMPLX", x
, y
, gfc_default_double_kind
);
2370 gfc_simplify_dble (gfc_expr
*e
)
2372 gfc_expr
*result
= NULL
;
2375 if (e
->expr_type
!= EXPR_CONSTANT
)
2378 /* For explicit conversion, turn off -Wconversion and -Wconversion-extra
2380 tmp1
= warn_conversion
;
2381 tmp2
= warn_conversion_extra
;
2382 warn_conversion
= warn_conversion_extra
= 0;
2384 result
= gfc_convert_constant (e
, BT_REAL
, gfc_default_double_kind
);
2386 warn_conversion
= tmp1
;
2387 warn_conversion_extra
= tmp2
;
2389 if (result
== &gfc_bad_expr
)
2390 return &gfc_bad_expr
;
2392 return range_check (result
, "DBLE");
2397 gfc_simplify_digits (gfc_expr
*x
)
2401 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
2406 digits
= gfc_integer_kinds
[i
].digits
;
2410 digits
= gfc_unsigned_kinds
[i
].digits
;
2415 digits
= gfc_real_kinds
[i
].digits
;
2422 return gfc_get_int_expr (gfc_default_integer_kind
, NULL
, digits
);
2427 gfc_simplify_dim (gfc_expr
*x
, gfc_expr
*y
)
2432 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
2435 kind
= x
->ts
.kind
> y
->ts
.kind
? x
->ts
.kind
: y
->ts
.kind
;
2436 result
= gfc_get_constant_expr (x
->ts
.type
, kind
, &x
->where
);
2441 if (mpz_cmp (x
->value
.integer
, y
->value
.integer
) > 0)
2442 mpz_sub (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
2444 mpz_set_ui (result
->value
.integer
, 0);
2449 if (mpfr_cmp (x
->value
.real
, y
->value
.real
) > 0)
2450 mpfr_sub (result
->value
.real
, x
->value
.real
, y
->value
.real
,
2453 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
2458 gfc_internal_error ("gfc_simplify_dim(): Bad type");
2461 return range_check (result
, "DIM");
2466 gfc_simplify_dot_product (gfc_expr
*vector_a
, gfc_expr
*vector_b
)
2468 /* If vector_a is a zero-sized array, the result is 0 for INTEGER,
2469 REAL, and COMPLEX types and .false. for LOGICAL. */
2470 if (vector_a
->shape
&& mpz_get_si (vector_a
->shape
[0]) == 0)
2472 if (vector_a
->ts
.type
== BT_LOGICAL
)
2473 return gfc_get_logical_expr (gfc_default_logical_kind
, NULL
, false);
2475 return gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 0);
2478 if (!is_constant_array_expr (vector_a
)
2479 || !is_constant_array_expr (vector_b
))
2482 return compute_dot_product (vector_a
, 1, 0, vector_b
, 1, 0, true);
2487 gfc_simplify_dprod (gfc_expr
*x
, gfc_expr
*y
)
2489 gfc_expr
*a1
, *a2
, *result
;
2491 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
2494 a1
= gfc_real2real (x
, gfc_default_double_kind
);
2495 a2
= gfc_real2real (y
, gfc_default_double_kind
);
2497 result
= gfc_get_constant_expr (BT_REAL
, gfc_default_double_kind
, &x
->where
);
2498 mpfr_mul (result
->value
.real
, a1
->value
.real
, a2
->value
.real
, GFC_RND_MODE
);
2503 return range_check (result
, "DPROD");
2508 simplify_dshift (gfc_expr
*arg1
, gfc_expr
*arg2
, gfc_expr
*shiftarg
,
2512 int i
, k
, size
, shift
;
2513 bt type
= BT_INTEGER
;
2515 if (arg1
->expr_type
!= EXPR_CONSTANT
|| arg2
->expr_type
!= EXPR_CONSTANT
2516 || shiftarg
->expr_type
!= EXPR_CONSTANT
)
2519 if (flag_unsigned
&& arg1
->ts
.type
== BT_UNSIGNED
)
2521 k
= gfc_validate_kind (BT_UNSIGNED
, arg1
->ts
.kind
, false);
2522 size
= gfc_unsigned_kinds
[k
].bit_size
;
2527 k
= gfc_validate_kind (BT_INTEGER
, arg1
->ts
.kind
, false);
2528 size
= gfc_integer_kinds
[k
].bit_size
;
2531 gfc_extract_int (shiftarg
, &shift
);
2533 /* DSHIFTR(I,J,SHIFT) = DSHIFTL(I,J,SIZE-SHIFT). */
2535 shift
= size
- shift
;
2537 result
= gfc_get_constant_expr (type
, arg1
->ts
.kind
, &arg1
->where
);
2538 mpz_set_ui (result
->value
.integer
, 0);
2540 for (i
= 0; i
< shift
; i
++)
2541 if (mpz_tstbit (arg2
->value
.integer
, size
- shift
+ i
))
2542 mpz_setbit (result
->value
.integer
, i
);
2544 for (i
= 0; i
< size
- shift
; i
++)
2545 if (mpz_tstbit (arg1
->value
.integer
, i
))
2546 mpz_setbit (result
->value
.integer
, shift
+ i
);
2548 /* Convert to a signed value if needed. */
2549 if (type
== BT_INTEGER
)
2550 gfc_convert_mpz_to_signed (result
->value
.integer
, size
);
2552 gfc_reduce_unsigned (result
);
2559 gfc_simplify_dshiftr (gfc_expr
*arg1
, gfc_expr
*arg2
, gfc_expr
*shiftarg
)
2561 return simplify_dshift (arg1
, arg2
, shiftarg
, true);
2566 gfc_simplify_dshiftl (gfc_expr
*arg1
, gfc_expr
*arg2
, gfc_expr
*shiftarg
)
2568 return simplify_dshift (arg1
, arg2
, shiftarg
, false);
2573 gfc_simplify_eoshift (gfc_expr
*array
, gfc_expr
*shift
, gfc_expr
*boundary
,
2580 gfc_expr
**arrayvec
, **resultvec
;
2581 gfc_expr
**rptr
, **sptr
;
2583 size_t arraysize
, i
;
2584 gfc_constructor
*array_ctor
, *shift_ctor
, *bnd_ctor
;
2585 ssize_t shift_val
, len
;
2586 ssize_t count
[GFC_MAX_DIMENSIONS
], extent
[GFC_MAX_DIMENSIONS
],
2587 sstride
[GFC_MAX_DIMENSIONS
], a_extent
[GFC_MAX_DIMENSIONS
],
2588 a_stride
[GFC_MAX_DIMENSIONS
], ss_ex
[GFC_MAX_DIMENSIONS
+ 1];
2592 gfc_expr
**src
, **dest
;
2595 if (!is_constant_array_expr (array
))
2598 if (shift
->rank
> 0)
2599 gfc_simplify_expr (shift
, 1);
2601 if (!gfc_is_constant_expr (shift
))
2606 if (boundary
->rank
> 0)
2607 gfc_simplify_expr (boundary
, 1);
2609 if (!gfc_is_constant_expr (boundary
))
2615 if (!gfc_is_constant_expr (dim
))
2617 which
= mpz_get_si (dim
->value
.integer
) - 1;
2623 if (boundary
== NULL
)
2625 temp_boundary
= true;
2626 switch (array
->ts
.type
)
2630 bnd
= gfc_get_int_expr (array
->ts
.kind
, NULL
, 0);
2634 bnd
= gfc_get_unsigned_expr (array
->ts
.kind
, NULL
, 0);
2638 bnd
= gfc_get_logical_expr (array
->ts
.kind
, NULL
, 0);
2642 bnd
= gfc_get_constant_expr (array
->ts
.type
, array
->ts
.kind
, &gfc_current_locus
);
2643 mpfr_set_ui (bnd
->value
.real
, 0, GFC_RND_MODE
);
2647 bnd
= gfc_get_constant_expr (array
->ts
.type
, array
->ts
.kind
, &gfc_current_locus
);
2648 mpc_set_ui (bnd
->value
.complex, 0, GFC_RND_MODE
);
2652 s_len
= mpz_get_ui (array
->ts
.u
.cl
->length
->value
.integer
);
2653 bnd
= gfc_get_character_expr (array
->ts
.kind
, &gfc_current_locus
, NULL
, s_len
);
2663 temp_boundary
= false;
2667 gfc_array_size (array
, &size
);
2668 arraysize
= mpz_get_ui (size
);
2671 result
= gfc_get_array_expr (array
->ts
.type
, array
->ts
.kind
, &array
->where
);
2672 result
->shape
= gfc_copy_shape (array
->shape
, array
->rank
);
2673 result
->rank
= array
->rank
;
2674 result
->ts
= array
->ts
;
2679 if (array
->shape
== NULL
)
2682 arrayvec
= XCNEWVEC (gfc_expr
*, arraysize
);
2683 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
2684 for (i
= 0; i
< arraysize
; i
++)
2686 arrayvec
[i
] = array_ctor
->expr
;
2687 array_ctor
= gfc_constructor_next (array_ctor
);
2690 resultvec
= XCNEWVEC (gfc_expr
*, arraysize
);
2695 for (d
=0; d
< array
->rank
; d
++)
2697 a_extent
[d
] = mpz_get_si (array
->shape
[d
]);
2698 a_stride
[d
] = d
== 0 ? 1 : a_stride
[d
-1] * a_extent
[d
-1];
2701 if (shift
->rank
> 0)
2703 shift_ctor
= gfc_constructor_first (shift
->value
.constructor
);
2709 shift_val
= mpz_get_si (shift
->value
.integer
);
2713 bnd_ctor
= gfc_constructor_first (bnd
->value
.constructor
);
2717 /* Shut up compiler */
2722 for (d
=0; d
< array
->rank
; d
++)
2726 rsoffset
= a_stride
[d
];
2732 extent
[n
] = a_extent
[d
];
2733 sstride
[n
] = a_stride
[d
];
2734 ss_ex
[n
] = sstride
[n
] * extent
[n
];
2740 continue_loop
= true;
2745 while (continue_loop
)
2750 sh
= mpz_get_si (shift_ctor
->expr
->value
.integer
);
2754 if (( sh
>= 0 ? sh
: -sh
) > len
)
2760 delta
= (sh
>= 0) ? sh
: -sh
;
2764 src
= &sptr
[delta
* rsoffset
];
2770 dest
= &rptr
[delta
* rsoffset
];
2773 for (n
= 0; n
< len
- delta
; n
++)
2789 *dest
= gfc_copy_expr (bnd_ctor
->expr
);
2797 *dest
= gfc_copy_expr (bnd
);
2804 shift_ctor
= gfc_constructor_next (shift_ctor
);
2807 bnd_ctor
= gfc_constructor_next (bnd_ctor
);
2811 while (count
[n
] == extent
[n
])
2819 continue_loop
= false;
2831 for (i
= 0; i
< arraysize
; i
++)
2833 gfc_constructor_append_expr (&result
->value
.constructor
,
2834 gfc_copy_expr (resultvec
[i
]),
2840 gfc_free_expr (bnd
);
2846 gfc_simplify_erf (gfc_expr
*x
)
2850 if (x
->expr_type
!= EXPR_CONSTANT
)
2853 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2854 mpfr_erf (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
2856 return range_check (result
, "ERF");
2861 gfc_simplify_erfc (gfc_expr
*x
)
2865 if (x
->expr_type
!= EXPR_CONSTANT
)
2868 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2869 mpfr_erfc (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
2871 return range_check (result
, "ERFC");
2875 /* Helper functions to simplify ERFC_SCALED(x) = ERFC(x) * EXP(X**2). */
2877 #define MAX_ITER 200
2878 #define ARG_LIMIT 12
2880 /* Calculate ERFC_SCALED directly by its definition:
2882 ERFC_SCALED(x) = ERFC(x) * EXP(X**2)
2884 using a large precision for intermediate results. This is used for all
2885 but large values of the argument. */
2887 fullprec_erfc_scaled (mpfr_t res
, mpfr_t arg
)
2892 prec
= mpfr_get_default_prec ();
2893 mpfr_set_default_prec (10 * prec
);
2898 mpfr_set (a
, arg
, GFC_RND_MODE
);
2899 mpfr_sqr (b
, a
, GFC_RND_MODE
);
2900 mpfr_exp (b
, b
, GFC_RND_MODE
);
2901 mpfr_erfc (a
, a
, GFC_RND_MODE
);
2902 mpfr_mul (a
, a
, b
, GFC_RND_MODE
);
2904 mpfr_set (res
, a
, GFC_RND_MODE
);
2905 mpfr_set_default_prec (prec
);
2911 /* Calculate ERFC_SCALED using a power series expansion in 1/arg:
2913 ERFC_SCALED(x) = 1 / (x * sqrt(pi))
2914 * (1 + Sum_n (-1)**n * (1 * 3 * 5 * ... * (2n-1))
2917 This is used for large values of the argument. Intermediate calculations
2918 are performed with twice the precision. We don't do a fixed number of
2919 iterations of the sum, but stop when it has converged to the required
2922 asympt_erfc_scaled (mpfr_t res
, mpfr_t arg
)
2924 mpfr_t sum
, x
, u
, v
, w
, oldsum
, sumtrunc
;
2929 prec
= mpfr_get_default_prec ();
2930 mpfr_set_default_prec (2 * prec
);
2940 mpfr_init (sumtrunc
);
2941 mpfr_set_prec (oldsum
, prec
);
2942 mpfr_set_prec (sumtrunc
, prec
);
2944 mpfr_set (x
, arg
, GFC_RND_MODE
);
2945 mpfr_set_ui (sum
, 1, GFC_RND_MODE
);
2946 mpz_set_ui (num
, 1);
2948 mpfr_set (u
, x
, GFC_RND_MODE
);
2949 mpfr_sqr (u
, u
, GFC_RND_MODE
);
2950 mpfr_mul_ui (u
, u
, 2, GFC_RND_MODE
);
2951 mpfr_pow_si (u
, u
, -1, GFC_RND_MODE
);
2953 for (i
= 1; i
< MAX_ITER
; i
++)
2955 mpfr_set (oldsum
, sum
, GFC_RND_MODE
);
2957 mpz_mul_ui (num
, num
, 2 * i
- 1);
2960 mpfr_set (w
, u
, GFC_RND_MODE
);
2961 mpfr_pow_ui (w
, w
, i
, GFC_RND_MODE
);
2963 mpfr_set_z (v
, num
, GFC_RND_MODE
);
2964 mpfr_mul (v
, v
, w
, GFC_RND_MODE
);
2966 mpfr_add (sum
, sum
, v
, GFC_RND_MODE
);
2968 mpfr_set (sumtrunc
, sum
, GFC_RND_MODE
);
2969 if (mpfr_cmp (sumtrunc
, oldsum
) == 0)
2973 /* We should have converged by now; otherwise, ARG_LIMIT is probably
2975 gcc_assert (i
< MAX_ITER
);
2977 /* Divide by x * sqrt(Pi). */
2978 mpfr_const_pi (u
, GFC_RND_MODE
);
2979 mpfr_sqrt (u
, u
, GFC_RND_MODE
);
2980 mpfr_mul (u
, u
, x
, GFC_RND_MODE
);
2981 mpfr_div (sum
, sum
, u
, GFC_RND_MODE
);
2983 mpfr_set (res
, sum
, GFC_RND_MODE
);
2984 mpfr_set_default_prec (prec
);
2986 mpfr_clears (sum
, x
, u
, v
, w
, oldsum
, sumtrunc
, NULL
);
2992 gfc_simplify_erfc_scaled (gfc_expr
*x
)
2996 if (x
->expr_type
!= EXPR_CONSTANT
)
2999 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3000 if (mpfr_cmp_d (x
->value
.real
, ARG_LIMIT
) >= 0)
3001 asympt_erfc_scaled (result
->value
.real
, x
->value
.real
);
3003 fullprec_erfc_scaled (result
->value
.real
, x
->value
.real
);
3005 return range_check (result
, "ERFC_SCALED");
3013 gfc_simplify_epsilon (gfc_expr
*e
)
3018 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
3020 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
3021 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].epsilon
, GFC_RND_MODE
);
3023 return range_check (result
, "EPSILON");
3028 gfc_simplify_exp (gfc_expr
*x
)
3032 if (x
->expr_type
!= EXPR_CONSTANT
)
3035 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3040 mpfr_exp (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
3044 gfc_set_model_kind (x
->ts
.kind
);
3045 mpc_exp (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
3049 gfc_internal_error ("in gfc_simplify_exp(): Bad type");
3052 return range_check (result
, "EXP");
3057 gfc_simplify_exponent (gfc_expr
*x
)
3062 if (x
->expr_type
!= EXPR_CONSTANT
)
3065 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
3068 /* EXPONENT(inf) = EXPONENT(nan) = HUGE(0) */
3069 if (mpfr_inf_p (x
->value
.real
) || mpfr_nan_p (x
->value
.real
))
3071 int i
= gfc_validate_kind (BT_INTEGER
, gfc_default_integer_kind
, false);
3072 mpz_set (result
->value
.integer
, gfc_integer_kinds
[i
].huge
);
3076 /* EXPONENT(+/- 0.0) = 0 */
3077 if (mpfr_zero_p (x
->value
.real
))
3079 mpz_set_ui (result
->value
.integer
, 0);
3083 gfc_set_model (x
->value
.real
);
3085 val
= (long int) mpfr_get_exp (x
->value
.real
);
3086 mpz_set_si (result
->value
.integer
, val
);
3088 return range_check (result
, "EXPONENT");
3093 gfc_simplify_failed_or_stopped_images (gfc_expr
*team ATTRIBUTE_UNUSED
,
3096 if (flag_coarray
== GFC_FCOARRAY_NONE
)
3098 gfc_current_locus
= *gfc_current_intrinsic_where
;
3099 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3100 return &gfc_bad_expr
;
3103 if (flag_coarray
== GFC_FCOARRAY_SINGLE
)
3108 gfc_extract_int (kind
, &actual_kind
);
3110 actual_kind
= gfc_default_integer_kind
;
3112 result
= gfc_get_array_expr (BT_INTEGER
, actual_kind
, &gfc_current_locus
);
3117 /* For fcoarray = lib no simplification is possible, because it is not known
3118 what images failed or are stopped at compile time. */
3124 gfc_simplify_get_team (gfc_expr
*level ATTRIBUTE_UNUSED
)
3126 if (flag_coarray
== GFC_FCOARRAY_NONE
)
3128 gfc_current_locus
= *gfc_current_intrinsic_where
;
3129 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3130 return &gfc_bad_expr
;
3133 if (flag_coarray
== GFC_FCOARRAY_SINGLE
)
3136 result
= gfc_get_array_expr (BT_INTEGER
, gfc_default_integer_kind
, &gfc_current_locus
);
3141 /* For fcoarray = lib no simplification is possible, because it is not known
3142 what images failed or are stopped at compile time. */
3148 gfc_simplify_float (gfc_expr
*a
)
3152 if (a
->expr_type
!= EXPR_CONSTANT
)
3155 result
= gfc_int2real (a
, gfc_default_real_kind
);
3157 return range_check (result
, "FLOAT");
3162 is_last_ref_vtab (gfc_expr
*e
)
3165 gfc_component
*comp
= NULL
;
3167 if (e
->expr_type
!= EXPR_VARIABLE
)
3170 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
3171 if (ref
->type
== REF_COMPONENT
)
3172 comp
= ref
->u
.c
.component
;
3174 if (!e
->ref
|| !comp
)
3175 return e
->symtree
->n
.sym
->attr
.vtab
;
3177 if (comp
->name
[0] == '_' && strcmp (comp
->name
, "_vptr") == 0)
3185 gfc_simplify_extends_type_of (gfc_expr
*a
, gfc_expr
*mold
)
3187 /* Avoid simplification of resolved symbols. */
3188 if (is_last_ref_vtab (a
) || is_last_ref_vtab (mold
))
3191 if (a
->ts
.type
== BT_DERIVED
&& mold
->ts
.type
== BT_DERIVED
)
3192 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
,
3193 gfc_type_is_extension_of (mold
->ts
.u
.derived
,
3196 if (UNLIMITED_POLY (a
) || UNLIMITED_POLY (mold
))
3199 if ((a
->ts
.type
== BT_CLASS
&& !gfc_expr_attr (a
).class_ok
)
3200 || (mold
->ts
.type
== BT_CLASS
&& !gfc_expr_attr (mold
).class_ok
))
3203 /* Return .false. if the dynamic type can never be an extension. */
3204 if ((a
->ts
.type
== BT_CLASS
&& mold
->ts
.type
== BT_CLASS
3205 && !gfc_type_is_extension_of
3206 (CLASS_DATA (mold
)->ts
.u
.derived
,
3207 CLASS_DATA (a
)->ts
.u
.derived
)
3208 && !gfc_type_is_extension_of
3209 (CLASS_DATA (a
)->ts
.u
.derived
,
3210 CLASS_DATA (mold
)->ts
.u
.derived
))
3211 || (a
->ts
.type
== BT_DERIVED
&& mold
->ts
.type
== BT_CLASS
3212 && !gfc_type_is_extension_of
3213 (CLASS_DATA (mold
)->ts
.u
.derived
,
3215 || (a
->ts
.type
== BT_CLASS
&& mold
->ts
.type
== BT_DERIVED
3216 && !gfc_type_is_extension_of
3217 (mold
->ts
.u
.derived
,
3218 CLASS_DATA (a
)->ts
.u
.derived
)
3219 && !gfc_type_is_extension_of
3220 (CLASS_DATA (a
)->ts
.u
.derived
,
3221 mold
->ts
.u
.derived
)))
3222 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
, false);
3224 /* Return .true. if the dynamic type is guaranteed to be an extension. */
3225 if (a
->ts
.type
== BT_CLASS
&& mold
->ts
.type
== BT_DERIVED
3226 && gfc_type_is_extension_of (mold
->ts
.u
.derived
,
3227 CLASS_DATA (a
)->ts
.u
.derived
))
3228 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
, true);
3235 gfc_simplify_same_type_as (gfc_expr
*a
, gfc_expr
*b
)
3237 /* Avoid simplification of resolved symbols. */
3238 if (is_last_ref_vtab (a
) || is_last_ref_vtab (b
))
3241 /* Return .false. if the dynamic type can never be the
3243 if (((a
->ts
.type
== BT_CLASS
&& gfc_expr_attr (a
).class_ok
)
3244 || (b
->ts
.type
== BT_CLASS
&& gfc_expr_attr (b
).class_ok
))
3245 && !gfc_type_compatible (&a
->ts
, &b
->ts
)
3246 && !gfc_type_compatible (&b
->ts
, &a
->ts
))
3247 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
, false);
3249 if (a
->ts
.type
!= BT_DERIVED
|| b
->ts
.type
!= BT_DERIVED
)
3252 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
,
3253 gfc_compare_derived_types (a
->ts
.u
.derived
,
3259 gfc_simplify_floor (gfc_expr
*e
, gfc_expr
*k
)
3265 kind
= get_kind (BT_INTEGER
, k
, "FLOOR", gfc_default_integer_kind
);
3267 gfc_internal_error ("gfc_simplify_floor(): Bad kind");
3269 if (e
->expr_type
!= EXPR_CONSTANT
)
3272 mpfr_init2 (floor
, mpfr_get_prec (e
->value
.real
));
3273 mpfr_floor (floor
, e
->value
.real
);
3275 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &e
->where
);
3276 gfc_mpfr_to_mpz (result
->value
.integer
, floor
, &e
->where
);
3280 return range_check (result
, "FLOOR");
3285 gfc_simplify_fraction (gfc_expr
*x
)
3290 if (x
->expr_type
!= EXPR_CONSTANT
)
3293 result
= gfc_get_constant_expr (BT_REAL
, x
->ts
.kind
, &x
->where
);
3295 /* FRACTION(inf) = NaN. */
3296 if (mpfr_inf_p (x
->value
.real
))
3298 mpfr_set_nan (result
->value
.real
);
3302 /* mpfr_frexp() correctly handles zeros and NaNs. */
3303 mpfr_frexp (&e
, result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
3305 return range_check (result
, "FRACTION");
3310 gfc_simplify_gamma (gfc_expr
*x
)
3314 if (x
->expr_type
!= EXPR_CONSTANT
)
3317 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3318 mpfr_gamma (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
3320 return range_check (result
, "GAMMA");
3325 gfc_simplify_huge (gfc_expr
*e
)
3330 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
3331 result
= gfc_get_constant_expr (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
3336 mpz_set (result
->value
.integer
, gfc_integer_kinds
[i
].huge
);
3340 mpz_set (result
->value
.integer
, gfc_unsigned_kinds
[i
].huge
);
3344 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].huge
, GFC_RND_MODE
);
3356 gfc_simplify_hypot (gfc_expr
*x
, gfc_expr
*y
)
3360 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
3363 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3364 mpfr_hypot (result
->value
.real
, x
->value
.real
, y
->value
.real
, GFC_RND_MODE
);
3365 return range_check (result
, "HYPOT");
3369 /* We use the processor's collating sequence, because all
3370 systems that gfortran currently works on are ASCII. */
3373 gfc_simplify_iachar (gfc_expr
*e
, gfc_expr
*kind
)
3379 if (e
->expr_type
!= EXPR_CONSTANT
)
3382 if (e
->value
.character
.length
!= 1)
3384 gfc_error ("Argument of IACHAR at %L must be of length one", &e
->where
);
3385 return &gfc_bad_expr
;
3388 index
= e
->value
.character
.string
[0];
3390 if (warn_surprising
&& index
> 127)
3391 gfc_warning (OPT_Wsurprising
,
3392 "Argument of IACHAR function at %L outside of range 0..127",
3395 k
= get_kind (BT_INTEGER
, kind
, "IACHAR", gfc_default_integer_kind
);
3397 return &gfc_bad_expr
;
3399 result
= gfc_get_int_expr (k
, &e
->where
, index
);
3401 return range_check (result
, "IACHAR");
3406 do_bit_and (gfc_expr
*result
, gfc_expr
*e
)
3410 gcc_assert ((e
->ts
.type
== BT_INTEGER
|| e
->ts
.type
== BT_UNSIGNED
)
3411 && e
->expr_type
== EXPR_CONSTANT
);
3412 gcc_assert ((result
->ts
.type
== BT_INTEGER
3413 || result
->ts
.type
== BT_UNSIGNED
)
3414 && result
->expr_type
== EXPR_CONSTANT
);
3418 gcc_assert (e
->ts
.type
== BT_INTEGER
&& e
->expr_type
== EXPR_CONSTANT
);
3419 gcc_assert (result
->ts
.type
== BT_INTEGER
3420 && result
->expr_type
== EXPR_CONSTANT
);
3423 mpz_and (result
->value
.integer
, result
->value
.integer
, e
->value
.integer
);
3429 gfc_simplify_iall (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
)
3431 return simplify_transformation (array
, dim
, mask
, -1, do_bit_and
);
3436 do_bit_ior (gfc_expr
*result
, gfc_expr
*e
)
3440 gcc_assert ((e
->ts
.type
== BT_INTEGER
|| e
->ts
.type
== BT_UNSIGNED
)
3441 && e
->expr_type
== EXPR_CONSTANT
);
3442 gcc_assert ((result
->ts
.type
== BT_INTEGER
3443 || result
->ts
.type
== BT_UNSIGNED
)
3444 && result
->expr_type
== EXPR_CONSTANT
);
3448 gcc_assert (e
->ts
.type
== BT_INTEGER
&& e
->expr_type
== EXPR_CONSTANT
);
3449 gcc_assert (result
->ts
.type
== BT_INTEGER
3450 && result
->expr_type
== EXPR_CONSTANT
);
3453 mpz_ior (result
->value
.integer
, result
->value
.integer
, e
->value
.integer
);
3459 gfc_simplify_iany (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
)
3461 return simplify_transformation (array
, dim
, mask
, 0, do_bit_ior
);
3466 gfc_simplify_iand (gfc_expr
*x
, gfc_expr
*y
)
3471 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
3474 type
= x
->ts
.type
== BT_UNSIGNED
? BT_UNSIGNED
: BT_INTEGER
;
3475 result
= gfc_get_constant_expr (type
, x
->ts
.kind
, &x
->where
);
3476 mpz_and (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
3478 return range_check (result
, "IAND");
3483 gfc_simplify_ibclr (gfc_expr
*x
, gfc_expr
*y
)
3488 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
3491 if (!gfc_check_bitfcn (x
, y
))
3492 return &gfc_bad_expr
;
3494 gfc_extract_int (y
, &pos
);
3496 k
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
3498 result
= gfc_copy_expr (x
);
3499 /* Drop any separate memory representation of x to avoid potential
3500 inconsistencies in result. */
3501 if (result
->representation
.string
)
3503 free (result
->representation
.string
);
3504 result
->representation
.string
= NULL
;
3507 if (x
->ts
.type
== BT_INTEGER
)
3509 gfc_convert_mpz_to_unsigned (result
->value
.integer
,
3510 gfc_integer_kinds
[k
].bit_size
);
3512 mpz_clrbit (result
->value
.integer
, pos
);
3514 gfc_convert_mpz_to_signed (result
->value
.integer
,
3515 gfc_integer_kinds
[k
].bit_size
);
3518 mpz_clrbit (result
->value
.integer
, pos
);
3525 gfc_simplify_ibits (gfc_expr
*x
, gfc_expr
*y
, gfc_expr
*z
)
3532 if (x
->expr_type
!= EXPR_CONSTANT
3533 || y
->expr_type
!= EXPR_CONSTANT
3534 || z
->expr_type
!= EXPR_CONSTANT
)
3537 if (!gfc_check_ibits (x
, y
, z
))
3538 return &gfc_bad_expr
;
3540 gfc_extract_int (y
, &pos
);
3541 gfc_extract_int (z
, &len
);
3543 k
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
3545 if (x
->ts
.type
== BT_INTEGER
)
3546 bitsize
= gfc_integer_kinds
[k
].bit_size
;
3548 bitsize
= gfc_unsigned_kinds
[k
].bit_size
;
3551 if (pos
+ len
> bitsize
)
3553 gfc_error ("Sum of second and third arguments of IBITS exceeds "
3554 "bit size at %L", &y
->where
);
3555 return &gfc_bad_expr
;
3558 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3560 if (x
->ts
.type
== BT_INTEGER
)
3561 gfc_convert_mpz_to_unsigned (result
->value
.integer
,
3562 gfc_integer_kinds
[k
].bit_size
);
3564 bits
= XCNEWVEC (int, bitsize
);
3566 for (i
= 0; i
< bitsize
; i
++)
3569 for (i
= 0; i
< len
; i
++)
3570 bits
[i
] = mpz_tstbit (x
->value
.integer
, i
+ pos
);
3572 for (i
= 0; i
< bitsize
; i
++)
3575 mpz_clrbit (result
->value
.integer
, i
);
3576 else if (bits
[i
] == 1)
3577 mpz_setbit (result
->value
.integer
, i
);
3579 gfc_internal_error ("IBITS: Bad bit");
3584 if (x
->ts
.type
== BT_INTEGER
)
3585 gfc_convert_mpz_to_signed (result
->value
.integer
,
3586 gfc_integer_kinds
[k
].bit_size
);
3593 gfc_simplify_ibset (gfc_expr
*x
, gfc_expr
*y
)
3598 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
3601 if (!gfc_check_bitfcn (x
, y
))
3602 return &gfc_bad_expr
;
3604 gfc_extract_int (y
, &pos
);
3606 k
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
3608 result
= gfc_copy_expr (x
);
3609 /* Drop any separate memory representation of x to avoid potential
3610 inconsistencies in result. */
3611 if (result
->representation
.string
)
3613 free (result
->representation
.string
);
3614 result
->representation
.string
= NULL
;
3617 if (x
->ts
.type
== BT_INTEGER
)
3619 gfc_convert_mpz_to_unsigned (result
->value
.integer
,
3620 gfc_integer_kinds
[k
].bit_size
);
3622 mpz_setbit (result
->value
.integer
, pos
);
3624 gfc_convert_mpz_to_signed (result
->value
.integer
,
3625 gfc_integer_kinds
[k
].bit_size
);
3628 mpz_setbit (result
->value
.integer
, pos
);
3635 gfc_simplify_ichar (gfc_expr
*e
, gfc_expr
*kind
)
3641 if (e
->expr_type
!= EXPR_CONSTANT
)
3644 if (e
->value
.character
.length
!= 1)
3646 gfc_error ("Argument of ICHAR at %L must be of length one", &e
->where
);
3647 return &gfc_bad_expr
;
3650 index
= e
->value
.character
.string
[0];
3652 k
= get_kind (BT_INTEGER
, kind
, "ICHAR", gfc_default_integer_kind
);
3654 return &gfc_bad_expr
;
3656 result
= gfc_get_int_expr (k
, &e
->where
, index
);
3658 return range_check (result
, "ICHAR");
3663 gfc_simplify_ieor (gfc_expr
*x
, gfc_expr
*y
)
3668 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
3671 type
= x
->ts
.type
== BT_UNSIGNED
? BT_UNSIGNED
: BT_INTEGER
;
3672 result
= gfc_get_constant_expr (type
, x
->ts
.kind
, &x
->where
);
3673 mpz_xor (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
3675 return range_check (result
, "IEOR");
3680 gfc_simplify_index (gfc_expr
*x
, gfc_expr
*y
, gfc_expr
*b
, gfc_expr
*kind
)
3684 HOST_WIDE_INT len
, lensub
, start
, last
, i
, index
= 0;
3687 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
3688 || ( b
!= NULL
&& b
->expr_type
!= EXPR_CONSTANT
))
3691 back
= (b
!= NULL
&& b
->value
.logical
!= 0);
3693 k
= get_kind (BT_INTEGER
, kind
, "INDEX", gfc_default_integer_kind
);
3695 return &gfc_bad_expr
;
3697 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &x
->where
);
3699 len
= x
->value
.character
.length
;
3700 lensub
= y
->value
.character
.length
;
3704 mpz_set_si (result
->value
.integer
, 0);
3719 last
= len
+ 1 - lensub
;
3726 start
= len
- lensub
;
3730 for (; start
!= last
; start
+= delta
)
3732 for (i
= 0; i
< lensub
; i
++)
3734 if (x
->value
.character
.string
[start
+ i
]
3735 != y
->value
.character
.string
[i
])
3746 mpz_set_si (result
->value
.integer
, index
);
3747 return range_check (result
, "INDEX");
3751 simplify_intconv (gfc_expr
*e
, int kind
, const char *name
)
3753 gfc_expr
*result
= NULL
;
3756 /* Convert BOZ to integer, and return without range checking. */
3757 if (e
->ts
.type
== BT_BOZ
)
3759 if (!gfc_boz2int (e
, kind
))
3761 result
= gfc_copy_expr (e
);
3765 if (e
->expr_type
!= EXPR_CONSTANT
)
3768 /* For explicit conversion, turn off -Wconversion and -Wconversion-extra
3770 tmp1
= warn_conversion
;
3771 tmp2
= warn_conversion_extra
;
3772 warn_conversion
= warn_conversion_extra
= 0;
3774 result
= gfc_convert_constant (e
, BT_INTEGER
, kind
);
3776 warn_conversion
= tmp1
;
3777 warn_conversion_extra
= tmp2
;
3779 if (result
== &gfc_bad_expr
)
3780 return &gfc_bad_expr
;
3782 return range_check (result
, name
);
3787 gfc_simplify_int (gfc_expr
*e
, gfc_expr
*k
)
3791 kind
= get_kind (BT_INTEGER
, k
, "INT", gfc_default_integer_kind
);
3793 return &gfc_bad_expr
;
3795 return simplify_intconv (e
, kind
, "INT");
3799 gfc_simplify_int2 (gfc_expr
*e
)
3801 return simplify_intconv (e
, 2, "INT2");
3806 gfc_simplify_int8 (gfc_expr
*e
)
3808 return simplify_intconv (e
, 8, "INT8");
3813 gfc_simplify_long (gfc_expr
*e
)
3815 return simplify_intconv (e
, 4, "LONG");
3820 gfc_simplify_ifix (gfc_expr
*e
)
3822 gfc_expr
*rtrunc
, *result
;
3824 if (e
->expr_type
!= EXPR_CONSTANT
)
3827 rtrunc
= gfc_copy_expr (e
);
3828 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
3830 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
3832 gfc_mpfr_to_mpz (result
->value
.integer
, rtrunc
->value
.real
, &e
->where
);
3834 gfc_free_expr (rtrunc
);
3836 return range_check (result
, "IFIX");
3841 gfc_simplify_idint (gfc_expr
*e
)
3843 gfc_expr
*rtrunc
, *result
;
3845 if (e
->expr_type
!= EXPR_CONSTANT
)
3848 rtrunc
= gfc_copy_expr (e
);
3849 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
3851 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
3853 gfc_mpfr_to_mpz (result
->value
.integer
, rtrunc
->value
.real
, &e
->where
);
3855 gfc_free_expr (rtrunc
);
3857 return range_check (result
, "IDINT");
3861 gfc_simplify_uint (gfc_expr
*e
, gfc_expr
*k
)
3863 gfc_expr
*result
= NULL
;
3866 /* KIND is always an integer. */
3868 kind
= get_kind (BT_INTEGER
, k
, "INT", gfc_default_integer_kind
);
3870 return &gfc_bad_expr
;
3872 /* Convert BOZ to integer, and return without range checking. */
3873 if (e
->ts
.type
== BT_BOZ
)
3875 if (!gfc_boz2uint (e
, kind
))
3877 result
= gfc_copy_expr (e
);
3881 if (e
->expr_type
!= EXPR_CONSTANT
)
3884 result
= gfc_convert_constant (e
, BT_UNSIGNED
, kind
);
3886 if (result
== &gfc_bad_expr
)
3887 return &gfc_bad_expr
;
3889 return range_check (result
, "UINT");
3894 gfc_simplify_ior (gfc_expr
*x
, gfc_expr
*y
)
3899 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
3902 type
= x
->ts
.type
== BT_UNSIGNED
? BT_UNSIGNED
: BT_INTEGER
;
3903 result
= gfc_get_constant_expr (type
, x
->ts
.kind
, &x
->where
);
3904 mpz_ior (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
3906 return range_check (result
, "IOR");
3911 do_bit_xor (gfc_expr
*result
, gfc_expr
*e
)
3915 gcc_assert ((e
->ts
.type
== BT_INTEGER
|| e
->ts
.type
== BT_UNSIGNED
)
3916 && e
->expr_type
== EXPR_CONSTANT
);
3917 gcc_assert ((result
->ts
.type
== BT_INTEGER
3918 || result
->ts
.type
== BT_UNSIGNED
)
3919 && result
->expr_type
== EXPR_CONSTANT
);
3923 gcc_assert (e
->ts
.type
== BT_INTEGER
&& e
->expr_type
== EXPR_CONSTANT
);
3924 gcc_assert (result
->ts
.type
== BT_INTEGER
3925 && result
->expr_type
== EXPR_CONSTANT
);
3928 mpz_xor (result
->value
.integer
, result
->value
.integer
, e
->value
.integer
);
3934 gfc_simplify_iparity (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
)
3936 return simplify_transformation (array
, dim
, mask
, 0, do_bit_xor
);
3941 gfc_simplify_is_iostat_end (gfc_expr
*x
)
3943 if (x
->expr_type
!= EXPR_CONSTANT
)
3946 return gfc_get_logical_expr (gfc_default_logical_kind
, &x
->where
,
3947 mpz_cmp_si (x
->value
.integer
,
3948 LIBERROR_END
) == 0);
3953 gfc_simplify_is_iostat_eor (gfc_expr
*x
)
3955 if (x
->expr_type
!= EXPR_CONSTANT
)
3958 return gfc_get_logical_expr (gfc_default_logical_kind
, &x
->where
,
3959 mpz_cmp_si (x
->value
.integer
,
3960 LIBERROR_EOR
) == 0);
3965 gfc_simplify_isnan (gfc_expr
*x
)
3967 if (x
->expr_type
!= EXPR_CONSTANT
)
3970 return gfc_get_logical_expr (gfc_default_logical_kind
, &x
->where
,
3971 mpfr_nan_p (x
->value
.real
));
3975 /* Performs a shift on its first argument. Depending on the last
3976 argument, the shift can be arithmetic, i.e. with filling from the
3977 left like in the SHIFTA intrinsic. */
3979 simplify_shift (gfc_expr
*e
, gfc_expr
*s
, const char *name
,
3980 bool arithmetic
, int direction
)
3983 int ashift
, *bits
, i
, k
, bitsize
, shift
;
3985 if (e
->expr_type
!= EXPR_CONSTANT
|| s
->expr_type
!= EXPR_CONSTANT
)
3988 gfc_extract_int (s
, &shift
);
3990 k
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
3991 if (e
->ts
.type
== BT_INTEGER
)
3992 bitsize
= gfc_integer_kinds
[k
].bit_size
;
3994 bitsize
= gfc_unsigned_kinds
[k
].bit_size
;
3996 result
= gfc_get_constant_expr (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
4000 mpz_set (result
->value
.integer
, e
->value
.integer
);
4004 if (direction
> 0 && shift
< 0)
4006 /* Left shift, as in SHIFTL. */
4007 gfc_error ("Second argument of %s is negative at %L", name
, &e
->where
);
4008 return &gfc_bad_expr
;
4010 else if (direction
< 0)
4012 /* Right shift, as in SHIFTR or SHIFTA. */
4015 gfc_error ("Second argument of %s is negative at %L",
4017 return &gfc_bad_expr
;
4023 ashift
= (shift
>= 0 ? shift
: -shift
);
4025 if (ashift
> bitsize
)
4027 gfc_error ("Magnitude of second argument of %s exceeds bit size "
4028 "at %L", name
, &e
->where
);
4029 return &gfc_bad_expr
;
4032 bits
= XCNEWVEC (int, bitsize
);
4034 for (i
= 0; i
< bitsize
; i
++)
4035 bits
[i
] = mpz_tstbit (e
->value
.integer
, i
);
4040 for (i
= 0; i
< shift
; i
++)
4041 mpz_clrbit (result
->value
.integer
, i
);
4043 for (i
= 0; i
< bitsize
- shift
; i
++)
4046 mpz_clrbit (result
->value
.integer
, i
+ shift
);
4048 mpz_setbit (result
->value
.integer
, i
+ shift
);
4054 if (arithmetic
&& bits
[bitsize
- 1])
4055 for (i
= bitsize
- 1; i
>= bitsize
- ashift
; i
--)
4056 mpz_setbit (result
->value
.integer
, i
);
4058 for (i
= bitsize
- 1; i
>= bitsize
- ashift
; i
--)
4059 mpz_clrbit (result
->value
.integer
, i
);
4061 for (i
= bitsize
- 1; i
>= ashift
; i
--)
4064 mpz_clrbit (result
->value
.integer
, i
- ashift
);
4066 mpz_setbit (result
->value
.integer
, i
- ashift
);
4070 if (result
->ts
.type
== BT_INTEGER
)
4071 gfc_convert_mpz_to_signed (result
->value
.integer
, bitsize
);
4073 gfc_reduce_unsigned(result
);
4082 gfc_simplify_ishft (gfc_expr
*e
, gfc_expr
*s
)
4084 return simplify_shift (e
, s
, "ISHFT", false, 0);
4089 gfc_simplify_lshift (gfc_expr
*e
, gfc_expr
*s
)
4091 return simplify_shift (e
, s
, "LSHIFT", false, 1);
4096 gfc_simplify_rshift (gfc_expr
*e
, gfc_expr
*s
)
4098 return simplify_shift (e
, s
, "RSHIFT", true, -1);
4103 gfc_simplify_shifta (gfc_expr
*e
, gfc_expr
*s
)
4105 return simplify_shift (e
, s
, "SHIFTA", true, -1);
4110 gfc_simplify_shiftl (gfc_expr
*e
, gfc_expr
*s
)
4112 return simplify_shift (e
, s
, "SHIFTL", false, 1);
4117 gfc_simplify_shiftr (gfc_expr
*e
, gfc_expr
*s
)
4119 return simplify_shift (e
, s
, "SHIFTR", false, -1);
4124 gfc_simplify_ishftc (gfc_expr
*e
, gfc_expr
*s
, gfc_expr
*sz
)
4127 int shift
, ashift
, isize
, ssize
, delta
, k
;
4130 if (e
->expr_type
!= EXPR_CONSTANT
|| s
->expr_type
!= EXPR_CONSTANT
)
4133 gfc_extract_int (s
, &shift
);
4135 k
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
4136 isize
= gfc_integer_kinds
[k
].bit_size
;
4140 if (sz
->expr_type
!= EXPR_CONSTANT
)
4143 gfc_extract_int (sz
, &ssize
);
4145 if (ssize
> isize
|| ssize
<= 0)
4146 return &gfc_bad_expr
;
4159 gfc_error ("Magnitude of second argument of ISHFTC exceeds "
4160 "BIT_SIZE of first argument at %C");
4162 gfc_error ("Absolute value of SHIFT shall be less than or equal "
4164 return &gfc_bad_expr
;
4167 result
= gfc_get_constant_expr (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
4169 mpz_set (result
->value
.integer
, e
->value
.integer
);
4174 if (result
->ts
.type
== BT_INTEGER
)
4175 gfc_convert_mpz_to_unsigned (result
->value
.integer
, isize
);
4177 bits
= XCNEWVEC (int, ssize
);
4179 for (i
= 0; i
< ssize
; i
++)
4180 bits
[i
] = mpz_tstbit (e
->value
.integer
, i
);
4182 delta
= ssize
- ashift
;
4186 for (i
= 0; i
< delta
; i
++)
4189 mpz_clrbit (result
->value
.integer
, i
+ shift
);
4191 mpz_setbit (result
->value
.integer
, i
+ shift
);
4194 for (i
= delta
; i
< ssize
; i
++)
4197 mpz_clrbit (result
->value
.integer
, i
- delta
);
4199 mpz_setbit (result
->value
.integer
, i
- delta
);
4204 for (i
= 0; i
< ashift
; i
++)
4207 mpz_clrbit (result
->value
.integer
, i
+ delta
);
4209 mpz_setbit (result
->value
.integer
, i
+ delta
);
4212 for (i
= ashift
; i
< ssize
; i
++)
4215 mpz_clrbit (result
->value
.integer
, i
+ shift
);
4217 mpz_setbit (result
->value
.integer
, i
+ shift
);
4221 if (result
->ts
.type
== BT_INTEGER
)
4222 gfc_convert_mpz_to_signed (result
->value
.integer
, isize
);
4230 gfc_simplify_kind (gfc_expr
*e
)
4232 return gfc_get_int_expr (gfc_default_integer_kind
, NULL
, e
->ts
.kind
);
4237 simplify_bound_dim (gfc_expr
*array
, gfc_expr
*kind
, int d
, int upper
,
4238 gfc_array_spec
*as
, gfc_ref
*ref
, bool coarray
)
4240 gfc_expr
*l
, *u
, *result
;
4243 k
= get_kind (BT_INTEGER
, kind
, upper
? "UBOUND" : "LBOUND",
4244 gfc_default_integer_kind
);
4246 return &gfc_bad_expr
;
4248 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &array
->where
);
4250 /* For non-variables, LBOUND(expr, DIM=n) = 1 and
4251 UBOUND(expr, DIM=n) = SIZE(expr, DIM=n). */
4252 if (!coarray
&& array
->expr_type
!= EXPR_VARIABLE
)
4256 gfc_expr
* dim
= result
;
4257 mpz_set_si (dim
->value
.integer
, d
);
4259 result
= simplify_size (array
, dim
, k
);
4260 gfc_free_expr (dim
);
4265 mpz_set_si (result
->value
.integer
, 1);
4270 /* Otherwise, we have a variable expression. */
4271 gcc_assert (array
->expr_type
== EXPR_VARIABLE
);
4274 if (!gfc_resolve_array_spec (as
, 0))
4277 /* The last dimension of an assumed-size array is special. */
4278 if ((!coarray
&& d
== as
->rank
&& as
->type
== AS_ASSUMED_SIZE
&& !upper
)
4279 || (coarray
&& d
== as
->rank
+ as
->corank
4280 && (!upper
|| flag_coarray
== GFC_FCOARRAY_SINGLE
)))
4282 if (as
->lower
[d
-1] && as
->lower
[d
-1]->expr_type
== EXPR_CONSTANT
)
4284 gfc_free_expr (result
);
4285 return gfc_copy_expr (as
->lower
[d
-1]);
4291 /* Then, we need to know the extent of the given dimension. */
4292 if (coarray
|| (ref
->u
.ar
.type
== AR_FULL
&& !ref
->next
))
4294 gfc_expr
*declared_bound
;
4296 bool constant_lbound
, constant_ubound
;
4301 gcc_assert (l
!= NULL
);
4303 constant_lbound
= l
->expr_type
== EXPR_CONSTANT
;
4304 constant_ubound
= u
&& u
->expr_type
== EXPR_CONSTANT
;
4306 empty_bound
= upper
? 0 : 1;
4307 declared_bound
= upper
? u
: l
;
4309 if ((!upper
&& !constant_lbound
)
4310 || (upper
&& !constant_ubound
))
4315 /* For {L,U}BOUND, the value depends on whether the array
4316 is empty. We can nevertheless simplify if the declared bound
4317 has the same value as that of an empty array, in which case
4318 the result isn't dependent on the array emptiness. */
4319 if (mpz_cmp_si (declared_bound
->value
.integer
, empty_bound
) == 0)
4320 mpz_set_si (result
->value
.integer
, empty_bound
);
4321 else if (!constant_lbound
|| !constant_ubound
)
4322 /* Array emptiness can't be determined, we can't simplify. */
4324 else if (mpz_cmp (l
->value
.integer
, u
->value
.integer
) > 0)
4325 mpz_set_si (result
->value
.integer
, empty_bound
);
4327 mpz_set (result
->value
.integer
, declared_bound
->value
.integer
);
4330 mpz_set (result
->value
.integer
, declared_bound
->value
.integer
);
4336 int d2
= 0, cnt
= 0;
4337 for (int idx
= 0; idx
< ref
->u
.ar
.dimen
; ++idx
)
4339 if (ref
->u
.ar
.dimen_type
[idx
] == DIMEN_ELEMENT
)
4341 else if (cnt
< d
- 1)
4346 if (!gfc_ref_dimen_size (&ref
->u
.ar
, d2
+ d
- 1, &result
->value
.integer
, NULL
))
4350 mpz_set_si (result
->value
.integer
, (long int) 1);
4354 return range_check (result
, upper
? "UBOUND" : "LBOUND");
4357 gfc_free_expr (result
);
4363 simplify_bound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
, int upper
)
4367 ar_type type
= AR_UNKNOWN
;
4370 if (array
->ts
.type
== BT_CLASS
)
4373 if (array
->expr_type
!= EXPR_VARIABLE
)
4380 /* Do not attempt to resolve if error has already been issued. */
4381 if (array
->symtree
->n
.sym
->error
)
4384 /* Follow any component references. */
4385 as
= array
->symtree
->n
.sym
->as
;
4386 for (ref
= array
->ref
; ref
; ref
= ref
->next
)
4391 type
= ref
->u
.ar
.type
;
4392 switch (ref
->u
.ar
.type
)
4399 /* We're done because 'as' has already been set in the
4400 previous iteration. */
4414 as
= ref
->u
.c
.component
->as
;
4427 if (as
&& (as
->type
== AS_DEFERRED
|| as
->type
== AS_ASSUMED_RANK
4428 || (as
->type
== AS_ASSUMED_SHAPE
&& upper
)))
4431 /* 'array' shall not be an unallocated allocatable variable or a pointer that
4432 is not associated. */
4433 if (array
->expr_type
== EXPR_VARIABLE
4434 && (gfc_expr_attr (array
).allocatable
|| gfc_expr_attr (array
).pointer
))
4438 || (as
->type
!= AS_DEFERRED
4439 && array
->expr_type
== EXPR_VARIABLE
4440 && !gfc_expr_attr (array
).allocatable
4441 && !gfc_expr_attr (array
).pointer
));
4445 /* Multi-dimensional bounds. */
4446 gfc_expr
*bounds
[GFC_MAX_DIMENSIONS
];
4450 /* UBOUND(ARRAY) is not valid for an assumed-size array. */
4451 if (upper
&& type
== AR_FULL
&& as
&& as
->type
== AS_ASSUMED_SIZE
)
4453 /* An error message will be emitted in
4454 check_assumed_size_reference (resolve.cc). */
4455 return &gfc_bad_expr
;
4458 /* Simplify the bounds for each dimension. */
4459 for (d
= 0; d
< array
->rank
; d
++)
4461 bounds
[d
] = simplify_bound_dim (array
, kind
, d
+ 1, upper
, as
, ref
,
4463 if (bounds
[d
] == NULL
|| bounds
[d
] == &gfc_bad_expr
)
4467 for (j
= 0; j
< d
; j
++)
4468 gfc_free_expr (bounds
[j
]);
4471 return &gfc_bad_expr
;
4477 /* Allocate the result expression. */
4478 k
= get_kind (BT_INTEGER
, kind
, upper
? "UBOUND" : "LBOUND",
4479 gfc_default_integer_kind
);
4481 return &gfc_bad_expr
;
4483 e
= gfc_get_array_expr (BT_INTEGER
, k
, &array
->where
);
4485 /* The result is a rank 1 array; its size is the rank of the first
4486 argument to {L,U}BOUND. */
4488 e
->shape
= gfc_get_shape (1);
4489 mpz_init_set_ui (e
->shape
[0], array
->rank
);
4491 /* Create the constructor for this array. */
4492 for (d
= 0; d
< array
->rank
; d
++)
4493 gfc_constructor_append_expr (&e
->value
.constructor
,
4494 bounds
[d
], &e
->where
);
4500 /* A DIM argument is specified. */
4501 if (dim
->expr_type
!= EXPR_CONSTANT
)
4504 d
= mpz_get_si (dim
->value
.integer
);
4506 if ((d
< 1 || d
> array
->rank
)
4507 || (d
== array
->rank
&& as
&& as
->type
== AS_ASSUMED_SIZE
&& upper
))
4509 gfc_error ("DIM argument at %L is out of bounds", &dim
->where
);
4510 return &gfc_bad_expr
;
4513 if (as
&& as
->type
== AS_ASSUMED_RANK
)
4516 return simplify_bound_dim (array
, kind
, d
, upper
, as
, ref
, false);
4522 simplify_cobound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
, int upper
)
4528 if (array
->expr_type
!= EXPR_VARIABLE
)
4531 /* Follow any component references. */
4532 as
= (array
->ts
.type
== BT_CLASS
&& CLASS_DATA (array
))
4533 ? CLASS_DATA (array
)->as
4534 : array
->symtree
->n
.sym
->as
;
4535 for (ref
= array
->ref
; ref
; ref
= ref
->next
)
4540 switch (ref
->u
.ar
.type
)
4543 if (ref
->u
.ar
.as
->corank
> 0)
4545 gcc_assert (as
== ref
->u
.ar
.as
);
4552 /* We're done because 'as' has already been set in the
4553 previous iteration. */
4567 as
= ref
->u
.c
.component
->as
;
4581 if (as
->cotype
== AS_DEFERRED
|| as
->cotype
== AS_ASSUMED_SHAPE
)
4586 /* Multi-dimensional cobounds. */
4587 gfc_expr
*bounds
[GFC_MAX_DIMENSIONS
];
4591 /* Simplify the cobounds for each dimension. */
4592 for (d
= 0; d
< as
->corank
; d
++)
4594 bounds
[d
] = simplify_bound_dim (array
, kind
, d
+ 1 + as
->rank
,
4595 upper
, as
, ref
, true);
4596 if (bounds
[d
] == NULL
|| bounds
[d
] == &gfc_bad_expr
)
4600 for (j
= 0; j
< d
; j
++)
4601 gfc_free_expr (bounds
[j
]);
4606 /* Allocate the result expression. */
4607 e
= gfc_get_expr ();
4608 e
->where
= array
->where
;
4609 e
->expr_type
= EXPR_ARRAY
;
4610 e
->ts
.type
= BT_INTEGER
;
4611 k
= get_kind (BT_INTEGER
, kind
, upper
? "UCOBOUND" : "LCOBOUND",
4612 gfc_default_integer_kind
);
4616 return &gfc_bad_expr
;
4620 /* The result is a rank 1 array; its size is the rank of the first
4621 argument to {L,U}COBOUND. */
4623 e
->shape
= gfc_get_shape (1);
4624 mpz_init_set_ui (e
->shape
[0], as
->corank
);
4626 /* Create the constructor for this array. */
4627 for (d
= 0; d
< as
->corank
; d
++)
4628 gfc_constructor_append_expr (&e
->value
.constructor
,
4629 bounds
[d
], &e
->where
);
4634 /* A DIM argument is specified. */
4635 if (dim
->expr_type
!= EXPR_CONSTANT
)
4638 d
= mpz_get_si (dim
->value
.integer
);
4640 if (d
< 1 || d
> as
->corank
)
4642 gfc_error ("DIM argument at %L is out of bounds", &dim
->where
);
4643 return &gfc_bad_expr
;
4646 return simplify_bound_dim (array
, kind
, d
+as
->rank
, upper
, as
, ref
, true);
4652 gfc_simplify_lbound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
)
4654 return simplify_bound (array
, dim
, kind
, 0);
4659 gfc_simplify_lcobound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
)
4661 return simplify_cobound (array
, dim
, kind
, 0);
4665 gfc_simplify_leadz (gfc_expr
*e
)
4667 unsigned long lz
, bs
;
4670 if (e
->expr_type
!= EXPR_CONSTANT
)
4673 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
4674 bs
= gfc_integer_kinds
[i
].bit_size
;
4675 if (mpz_cmp_si (e
->value
.integer
, 0) == 0)
4677 else if (mpz_cmp_si (e
->value
.integer
, 0) < 0)
4680 lz
= bs
- mpz_sizeinbase (e
->value
.integer
, 2);
4682 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, lz
);
4686 /* Check for constant length of a substring. */
4689 substring_has_constant_len (gfc_expr
*e
)
4692 HOST_WIDE_INT istart
, iend
, length
;
4693 bool equal_length
= false;
4695 if (e
->ts
.type
!= BT_CHARACTER
)
4698 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
4699 if (ref
->type
!= REF_COMPONENT
&& ref
->type
!= REF_ARRAY
)
4703 || ref
->type
!= REF_SUBSTRING
4705 || ref
->u
.ss
.start
->expr_type
!= EXPR_CONSTANT
4707 || ref
->u
.ss
.end
->expr_type
!= EXPR_CONSTANT
)
4710 /* Basic checks on substring starting and ending indices. */
4711 if (!gfc_resolve_substring (ref
, &equal_length
))
4714 istart
= gfc_mpz_get_hwi (ref
->u
.ss
.start
->value
.integer
);
4715 iend
= gfc_mpz_get_hwi (ref
->u
.ss
.end
->value
.integer
);
4718 length
= iend
- istart
+ 1;
4722 /* Fix substring length. */
4723 e
->value
.character
.length
= length
;
4730 gfc_simplify_len (gfc_expr
*e
, gfc_expr
*kind
)
4733 int k
= get_kind (BT_INTEGER
, kind
, "LEN", gfc_default_integer_kind
);
4736 return &gfc_bad_expr
;
4738 if (e
->expr_type
== EXPR_CONSTANT
4739 || substring_has_constant_len (e
))
4741 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &e
->where
);
4742 mpz_set_si (result
->value
.integer
, e
->value
.character
.length
);
4743 return range_check (result
, "LEN");
4745 else if (e
->ts
.u
.cl
!= NULL
&& e
->ts
.u
.cl
->length
!= NULL
4746 && e
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
4747 && e
->ts
.u
.cl
->length
->ts
.type
== BT_INTEGER
)
4749 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &e
->where
);
4750 mpz_set (result
->value
.integer
, e
->ts
.u
.cl
->length
->value
.integer
);
4751 return range_check (result
, "LEN");
4753 else if (e
->expr_type
== EXPR_VARIABLE
&& e
->ts
.type
== BT_CHARACTER
4754 && e
->symtree
->n
.sym
)
4756 if (e
->symtree
->n
.sym
->ts
.type
!= BT_DERIVED
4757 && e
->symtree
->n
.sym
->assoc
&& e
->symtree
->n
.sym
->assoc
->target
4758 && e
->symtree
->n
.sym
->assoc
->target
->ts
.type
== BT_DERIVED
4759 && e
->symtree
->n
.sym
->assoc
->target
->symtree
->n
.sym
4760 && UNLIMITED_POLY (e
->symtree
->n
.sym
->assoc
->target
->symtree
->n
.sym
))
4761 /* The expression in assoc->target points to a ref to the _data
4762 component of the unlimited polymorphic entity. To get the _len
4763 component the last _data ref needs to be stripped and a ref to the
4764 _len component added. */
4765 return gfc_get_len_component (e
->symtree
->n
.sym
->assoc
->target
, k
);
4766 else if (e
->symtree
->n
.sym
->ts
.type
== BT_DERIVED
4767 && e
->ref
&& e
->ref
->type
== REF_COMPONENT
4768 && e
->ref
->u
.c
.component
->attr
.pdt_string
4769 && e
->ref
->u
.c
.component
->ts
.type
== BT_CHARACTER
4770 && e
->ref
->u
.c
.component
->ts
.u
.cl
->length
)
4772 if (gfc_init_expr_flag
)
4775 tmp
= gfc_pdt_find_component_copy_initializer (e
->symtree
->n
.sym
,
4785 gfc_expr
*len_expr
= gfc_copy_expr (e
);
4786 gfc_free_ref_list (len_expr
->ref
);
4787 len_expr
->ref
= NULL
;
4788 gfc_find_component (len_expr
->symtree
->n
.sym
->ts
.u
.derived
, e
->ref
4789 ->u
.c
.component
->ts
.u
.cl
->length
->symtree
4791 false, true, &len_expr
->ref
);
4792 len_expr
->ts
= len_expr
->ref
->u
.c
.component
->ts
;
4802 gfc_simplify_len_trim (gfc_expr
*e
, gfc_expr
*kind
)
4805 size_t count
, len
, i
;
4806 int k
= get_kind (BT_INTEGER
, kind
, "LEN_TRIM", gfc_default_integer_kind
);
4809 return &gfc_bad_expr
;
4811 /* If the expression is either an array element or section, an array
4812 parameter must be built so that the reference can be applied. Constant
4813 references should have already been simplified away. All other cases
4814 can proceed to translation, where kind conversion will occur silently. */
4815 if (e
->expr_type
== EXPR_VARIABLE
4816 && e
->ts
.type
== BT_CHARACTER
4817 && e
->symtree
->n
.sym
->attr
.flavor
== FL_PARAMETER
4818 && e
->ref
&& e
->ref
->type
== REF_ARRAY
4819 && e
->ref
->u
.ar
.type
!= AR_FULL
4820 && e
->symtree
->n
.sym
->value
)
4822 char name
[2*GFC_MAX_SYMBOL_LEN
+ 12];
4823 gfc_namespace
*ns
= e
->symtree
->n
.sym
->ns
;
4830 sprintf (name
, "_len_trim_%s_%s", e
->symtree
->n
.sym
->name
,
4831 ns
->proc_name
->name
);
4832 st
= gfc_find_symtree (ns
->sym_root
, name
);
4836 /* Recursively call this fcn to simplify the constructor elements. */
4837 expr
= gfc_copy_expr (e
->symtree
->n
.sym
->value
);
4838 expr
->ts
.type
= BT_INTEGER
;
4840 expr
->ts
.u
.cl
= NULL
;
4841 c
= gfc_constructor_first (expr
->value
.constructor
);
4842 for (; c
; c
= gfc_constructor_next (c
))
4847 if (c
->expr
&& c
->expr
->ts
.type
== BT_CHARACTER
)
4849 p
= gfc_simplify_len_trim (c
->expr
, kind
);
4852 gfc_replace_expr (c
->expr
, p
);
4859 /* Build a new parameter to take the result. */
4860 st
= gfc_new_symtree (&ns
->sym_root
, name
);
4861 st
->n
.sym
= gfc_new_symbol (st
->name
, ns
);
4862 st
->n
.sym
->value
= expr
;
4863 st
->n
.sym
->ts
= expr
->ts
;
4864 st
->n
.sym
->attr
.dimension
= 1;
4865 st
->n
.sym
->attr
.save
= SAVE_IMPLICIT
;
4866 st
->n
.sym
->attr
.flavor
= FL_PARAMETER
;
4867 st
->n
.sym
->as
= gfc_copy_array_spec (e
->symtree
->n
.sym
->as
);
4868 gfc_set_sym_referenced (st
->n
.sym
);
4870 gfc_commit_symbol (st
->n
.sym
);
4873 /* Build a return expression. */
4874 expr
= gfc_copy_expr (e
);
4875 expr
->ts
= st
->n
.sym
->ts
;
4877 gfc_expression_rank (expr
);
4882 gfc_free_expr (expr
);
4886 if (e
->expr_type
!= EXPR_CONSTANT
)
4889 len
= e
->value
.character
.length
;
4890 for (count
= 0, i
= 1; i
<= len
; i
++)
4891 if (e
->value
.character
.string
[len
- i
] == ' ')
4896 result
= gfc_get_int_expr (k
, &e
->where
, len
- count
);
4897 return range_check (result
, "LEN_TRIM");
4901 gfc_simplify_lgamma (gfc_expr
*x
)
4906 if (x
->expr_type
!= EXPR_CONSTANT
)
4909 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
4910 mpfr_lgamma (result
->value
.real
, &sg
, x
->value
.real
, GFC_RND_MODE
);
4912 return range_check (result
, "LGAMMA");
4917 gfc_simplify_lge (gfc_expr
*a
, gfc_expr
*b
)
4919 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
4922 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
,
4923 gfc_compare_string (a
, b
) >= 0);
4928 gfc_simplify_lgt (gfc_expr
*a
, gfc_expr
*b
)
4930 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
4933 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
,
4934 gfc_compare_string (a
, b
) > 0);
4939 gfc_simplify_lle (gfc_expr
*a
, gfc_expr
*b
)
4941 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
4944 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
,
4945 gfc_compare_string (a
, b
) <= 0);
4950 gfc_simplify_llt (gfc_expr
*a
, gfc_expr
*b
)
4952 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
4955 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
,
4956 gfc_compare_string (a
, b
) < 0);
4961 gfc_simplify_log (gfc_expr
*x
)
4965 if (x
->expr_type
!= EXPR_CONSTANT
)
4968 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
4973 if (mpfr_sgn (x
->value
.real
) <= 0)
4975 gfc_error ("Argument of LOG at %L cannot be less than or equal "
4976 "to zero", &x
->where
);
4977 gfc_free_expr (result
);
4978 return &gfc_bad_expr
;
4981 mpfr_log (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
4985 if (mpfr_zero_p (mpc_realref (x
->value
.complex))
4986 && mpfr_zero_p (mpc_imagref (x
->value
.complex)))
4988 gfc_error ("Complex argument of LOG at %L cannot be zero",
4990 gfc_free_expr (result
);
4991 return &gfc_bad_expr
;
4994 gfc_set_model_kind (x
->ts
.kind
);
4995 mpc_log (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
4999 gfc_internal_error ("gfc_simplify_log: bad type");
5002 return range_check (result
, "LOG");
5007 gfc_simplify_log10 (gfc_expr
*x
)
5011 if (x
->expr_type
!= EXPR_CONSTANT
)
5014 if (mpfr_sgn (x
->value
.real
) <= 0)
5016 gfc_error ("Argument of LOG10 at %L cannot be less than or equal "
5017 "to zero", &x
->where
);
5018 return &gfc_bad_expr
;
5021 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
5022 mpfr_log10 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
5024 return range_check (result
, "LOG10");
5029 gfc_simplify_logical (gfc_expr
*e
, gfc_expr
*k
)
5033 kind
= get_kind (BT_LOGICAL
, k
, "LOGICAL", gfc_default_logical_kind
);
5035 return &gfc_bad_expr
;
5037 if (e
->expr_type
!= EXPR_CONSTANT
)
5040 return gfc_get_logical_expr (kind
, &e
->where
, e
->value
.logical
);
5045 gfc_simplify_matmul (gfc_expr
*matrix_a
, gfc_expr
*matrix_b
)
5048 int row
, result_rows
, col
, result_columns
;
5049 int stride_a
, offset_a
, stride_b
, offset_b
;
5051 if (!is_constant_array_expr (matrix_a
)
5052 || !is_constant_array_expr (matrix_b
))
5055 /* MATMUL should do mixed-mode arithmetic. Set the result type. */
5056 if (matrix_a
->ts
.type
!= matrix_b
->ts
.type
)
5059 e
.expr_type
= EXPR_OP
;
5060 gfc_clear_ts (&e
.ts
);
5061 e
.value
.op
.op
= INTRINSIC_NONE
;
5062 e
.value
.op
.op1
= matrix_a
;
5063 e
.value
.op
.op2
= matrix_b
;
5064 gfc_type_convert_binary (&e
, 1);
5065 result
= gfc_get_array_expr (e
.ts
.type
, e
.ts
.kind
, &matrix_a
->where
);
5069 result
= gfc_get_array_expr (matrix_a
->ts
.type
, matrix_a
->ts
.kind
,
5073 if (matrix_a
->rank
== 1 && matrix_b
->rank
== 2)
5076 result_columns
= mpz_get_si (matrix_b
->shape
[1]);
5078 stride_b
= mpz_get_si (matrix_b
->shape
[0]);
5081 result
->shape
= gfc_get_shape (result
->rank
);
5082 mpz_init_set_si (result
->shape
[0], result_columns
);
5084 else if (matrix_a
->rank
== 2 && matrix_b
->rank
== 1)
5086 result_rows
= mpz_get_si (matrix_a
->shape
[0]);
5088 stride_a
= mpz_get_si (matrix_a
->shape
[0]);
5092 result
->shape
= gfc_get_shape (result
->rank
);
5093 mpz_init_set_si (result
->shape
[0], result_rows
);
5095 else if (matrix_a
->rank
== 2 && matrix_b
->rank
== 2)
5097 result_rows
= mpz_get_si (matrix_a
->shape
[0]);
5098 result_columns
= mpz_get_si (matrix_b
->shape
[1]);
5099 stride_a
= mpz_get_si (matrix_a
->shape
[0]);
5100 stride_b
= mpz_get_si (matrix_b
->shape
[0]);
5103 result
->shape
= gfc_get_shape (result
->rank
);
5104 mpz_init_set_si (result
->shape
[0], result_rows
);
5105 mpz_init_set_si (result
->shape
[1], result_columns
);
5111 for (col
= 0; col
< result_columns
; ++col
)
5115 for (row
= 0; row
< result_rows
; ++row
)
5117 gfc_expr
*e
= compute_dot_product (matrix_a
, stride_a
, offset_a
,
5118 matrix_b
, 1, offset_b
, false);
5119 gfc_constructor_append_expr (&result
->value
.constructor
,
5125 offset_b
+= stride_b
;
5133 gfc_simplify_maskr (gfc_expr
*i
, gfc_expr
*kind_arg
)
5138 if (i
->expr_type
!= EXPR_CONSTANT
)
5141 kind
= get_kind (BT_INTEGER
, kind_arg
, "MASKR", gfc_default_integer_kind
);
5143 return &gfc_bad_expr
;
5144 k
= gfc_validate_kind (BT_INTEGER
, kind
, false);
5146 bool fail
= gfc_extract_int (i
, &arg
);
5149 if (!gfc_check_mask (i
, kind_arg
))
5150 return &gfc_bad_expr
;
5152 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &i
->where
);
5154 /* MASKR(n) = 2^n - 1 */
5155 mpz_set_ui (result
->value
.integer
, 1);
5156 mpz_mul_2exp (result
->value
.integer
, result
->value
.integer
, arg
);
5157 mpz_sub_ui (result
->value
.integer
, result
->value
.integer
, 1);
5159 gfc_convert_mpz_to_signed (result
->value
.integer
, gfc_integer_kinds
[k
].bit_size
);
5166 gfc_simplify_maskl (gfc_expr
*i
, gfc_expr
*kind_arg
)
5172 if (i
->expr_type
!= EXPR_CONSTANT
)
5175 kind
= get_kind (BT_INTEGER
, kind_arg
, "MASKL", gfc_default_integer_kind
);
5177 return &gfc_bad_expr
;
5178 k
= gfc_validate_kind (BT_INTEGER
, kind
, false);
5180 bool fail
= gfc_extract_int (i
, &arg
);
5183 if (!gfc_check_mask (i
, kind_arg
))
5184 return &gfc_bad_expr
;
5186 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &i
->where
);
5188 /* MASKL(n) = 2^bit_size - 2^(bit_size - n) */
5189 mpz_init_set_ui (z
, 1);
5190 mpz_mul_2exp (z
, z
, gfc_integer_kinds
[k
].bit_size
);
5191 mpz_set_ui (result
->value
.integer
, 1);
5192 mpz_mul_2exp (result
->value
.integer
, result
->value
.integer
,
5193 gfc_integer_kinds
[k
].bit_size
- arg
);
5194 mpz_sub (result
->value
.integer
, z
, result
->value
.integer
);
5197 gfc_convert_mpz_to_signed (result
->value
.integer
, gfc_integer_kinds
[k
].bit_size
);
5204 gfc_simplify_merge (gfc_expr
*tsource
, gfc_expr
*fsource
, gfc_expr
*mask
)
5207 gfc_constructor
*tsource_ctor
, *fsource_ctor
, *mask_ctor
;
5209 if (mask
->expr_type
== EXPR_CONSTANT
)
5211 /* The standard requires evaluation of all function arguments.
5212 Simplify only when the other dropped argument (FSOURCE or TSOURCE)
5213 is a constant expression. */
5214 if (mask
->value
.logical
)
5216 if (!gfc_is_constant_expr (fsource
))
5218 result
= gfc_copy_expr (tsource
);
5222 if (!gfc_is_constant_expr (tsource
))
5224 result
= gfc_copy_expr (fsource
);
5227 /* Parenthesis is needed to get lower bounds of 1. */
5228 result
= gfc_get_parentheses (result
);
5229 gfc_simplify_expr (result
, 1);
5233 if (!mask
->rank
|| !is_constant_array_expr (mask
)
5234 || !is_constant_array_expr (tsource
) || !is_constant_array_expr (fsource
))
5237 result
= gfc_get_array_expr (tsource
->ts
.type
, tsource
->ts
.kind
,
5239 if (tsource
->ts
.type
== BT_DERIVED
)
5240 result
->ts
.u
.derived
= tsource
->ts
.u
.derived
;
5241 else if (tsource
->ts
.type
== BT_CHARACTER
)
5242 result
->ts
.u
.cl
= tsource
->ts
.u
.cl
;
5244 tsource_ctor
= gfc_constructor_first (tsource
->value
.constructor
);
5245 fsource_ctor
= gfc_constructor_first (fsource
->value
.constructor
);
5246 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
5250 if (mask_ctor
->expr
->value
.logical
)
5251 gfc_constructor_append_expr (&result
->value
.constructor
,
5252 gfc_copy_expr (tsource_ctor
->expr
),
5255 gfc_constructor_append_expr (&result
->value
.constructor
,
5256 gfc_copy_expr (fsource_ctor
->expr
),
5258 tsource_ctor
= gfc_constructor_next (tsource_ctor
);
5259 fsource_ctor
= gfc_constructor_next (fsource_ctor
);
5260 mask_ctor
= gfc_constructor_next (mask_ctor
);
5263 result
->shape
= gfc_get_shape (1);
5264 gfc_array_size (result
, &result
->shape
[0]);
5271 gfc_simplify_merge_bits (gfc_expr
*i
, gfc_expr
*j
, gfc_expr
*mask_expr
)
5273 mpz_t arg1
, arg2
, mask
;
5276 if (i
->expr_type
!= EXPR_CONSTANT
|| j
->expr_type
!= EXPR_CONSTANT
5277 || mask_expr
->expr_type
!= EXPR_CONSTANT
)
5280 result
= gfc_get_constant_expr (i
->ts
.type
, i
->ts
.kind
, &i
->where
);
5282 /* Convert all argument to unsigned. */
5283 mpz_init_set (arg1
, i
->value
.integer
);
5284 mpz_init_set (arg2
, j
->value
.integer
);
5285 mpz_init_set (mask
, mask_expr
->value
.integer
);
5287 /* MERGE_BITS(I,J,MASK) = IOR (IAND (I, MASK), IAND (J, NOT (MASK))). */
5288 mpz_and (arg1
, arg1
, mask
);
5289 mpz_com (mask
, mask
);
5290 mpz_and (arg2
, arg2
, mask
);
5291 mpz_ior (result
->value
.integer
, arg1
, arg2
);
5301 /* Selects between current value and extremum for simplify_min_max
5302 and simplify_minval_maxval. */
5304 min_max_choose (gfc_expr
*arg
, gfc_expr
*extremum
, int sign
, bool back_val
)
5308 switch (arg
->ts
.type
)
5312 if (extremum
->ts
.kind
< arg
->ts
.kind
)
5313 extremum
->ts
.kind
= arg
->ts
.kind
;
5314 ret
= mpz_cmp (arg
->value
.integer
,
5315 extremum
->value
.integer
) * sign
;
5317 mpz_set (extremum
->value
.integer
, arg
->value
.integer
);
5321 if (extremum
->ts
.kind
< arg
->ts
.kind
)
5322 extremum
->ts
.kind
= arg
->ts
.kind
;
5323 if (mpfr_nan_p (extremum
->value
.real
))
5326 mpfr_set (extremum
->value
.real
, arg
->value
.real
, GFC_RND_MODE
);
5328 else if (mpfr_nan_p (arg
->value
.real
))
5332 ret
= mpfr_cmp (arg
->value
.real
, extremum
->value
.real
) * sign
;
5334 mpfr_set (extremum
->value
.real
, arg
->value
.real
, GFC_RND_MODE
);
5339 #define LENGTH(x) ((x)->value.character.length)
5340 #define STRING(x) ((x)->value.character.string)
5341 if (LENGTH (extremum
) < LENGTH(arg
))
5343 gfc_char_t
*tmp
= STRING(extremum
);
5345 STRING(extremum
) = gfc_get_wide_string (LENGTH(arg
) + 1);
5346 memcpy (STRING(extremum
), tmp
,
5347 LENGTH(extremum
) * sizeof (gfc_char_t
));
5348 gfc_wide_memset (&STRING(extremum
)[LENGTH(extremum
)], ' ',
5349 LENGTH(arg
) - LENGTH(extremum
));
5350 STRING(extremum
)[LENGTH(arg
)] = '\0'; /* For debugger */
5351 LENGTH(extremum
) = LENGTH(arg
);
5354 ret
= gfc_compare_string (arg
, extremum
) * sign
;
5357 free (STRING(extremum
));
5358 STRING(extremum
) = gfc_get_wide_string (LENGTH(extremum
) + 1);
5359 memcpy (STRING(extremum
), STRING(arg
),
5360 LENGTH(arg
) * sizeof (gfc_char_t
));
5361 gfc_wide_memset (&STRING(extremum
)[LENGTH(arg
)], ' ',
5362 LENGTH(extremum
) - LENGTH(arg
));
5363 STRING(extremum
)[LENGTH(extremum
)] = '\0'; /* For debugger */
5370 gfc_internal_error ("simplify_min_max(): Bad type in arglist");
5372 if (back_val
&& ret
== 0)
5379 /* This function is special since MAX() can take any number of
5380 arguments. The simplified expression is a rewritten version of the
5381 argument list containing at most one constant element. Other
5382 constant elements are deleted. Because the argument list has
5383 already been checked, this function always succeeds. sign is 1 for
5384 MAX(), -1 for MIN(). */
5387 simplify_min_max (gfc_expr
*expr
, int sign
)
5390 gfc_actual_arglist
*arg
, *last
, *extremum
;
5391 gfc_expr
*tmp
, *ret
;
5397 arg
= expr
->value
.function
.actual
;
5399 for (; arg
; last
= arg
, arg
= arg
->next
)
5401 if (arg
->expr
->expr_type
!= EXPR_CONSTANT
)
5404 if (extremum
== NULL
)
5410 min_max_choose (arg
->expr
, extremum
->expr
, sign
);
5412 /* Delete the extra constant argument. */
5413 last
->next
= arg
->next
;
5416 gfc_free_actual_arglist (arg
);
5420 /* If there is one value left, replace the function call with the
5422 if (expr
->value
.function
.actual
->next
!= NULL
)
5425 /* Handle special cases of specific functions (min|max)1 and
5428 tmp
= expr
->value
.function
.actual
->expr
;
5429 fname
= expr
->value
.function
.isym
->name
;
5431 if ((tmp
->ts
.type
!= BT_INTEGER
|| tmp
->ts
.kind
!= gfc_integer_4_kind
)
5432 && (strcmp (fname
, "min1") == 0 || strcmp (fname
, "max1") == 0))
5434 /* Explicit conversion, turn off -Wconversion and -Wconversion-extra
5436 tmp1
= warn_conversion
;
5437 tmp2
= warn_conversion_extra
;
5438 warn_conversion
= warn_conversion_extra
= 0;
5440 ret
= gfc_convert_constant (tmp
, BT_INTEGER
, gfc_integer_4_kind
);
5442 warn_conversion
= tmp1
;
5443 warn_conversion_extra
= tmp2
;
5445 else if ((tmp
->ts
.type
!= BT_REAL
|| tmp
->ts
.kind
!= gfc_real_4_kind
)
5446 && (strcmp (fname
, "amin0") == 0 || strcmp (fname
, "amax0") == 0))
5448 ret
= gfc_convert_constant (tmp
, BT_REAL
, gfc_real_4_kind
);
5451 ret
= gfc_copy_expr (tmp
);
5459 gfc_simplify_min (gfc_expr
*e
)
5461 return simplify_min_max (e
, -1);
5466 gfc_simplify_max (gfc_expr
*e
)
5468 return simplify_min_max (e
, 1);
5471 /* Helper function for gfc_simplify_minval. */
5474 gfc_min (gfc_expr
*op1
, gfc_expr
*op2
)
5476 min_max_choose (op1
, op2
, -1);
5477 gfc_free_expr (op1
);
5481 /* Simplify minval for constant arrays. */
5484 gfc_simplify_minval (gfc_expr
*array
, gfc_expr
* dim
, gfc_expr
*mask
)
5486 return simplify_transformation (array
, dim
, mask
, INT_MAX
, gfc_min
);
5489 /* Helper function for gfc_simplify_maxval. */
5492 gfc_max (gfc_expr
*op1
, gfc_expr
*op2
)
5494 min_max_choose (op1
, op2
, 1);
5495 gfc_free_expr (op1
);
5500 /* Simplify maxval for constant arrays. */
5503 gfc_simplify_maxval (gfc_expr
*array
, gfc_expr
* dim
, gfc_expr
*mask
)
5505 return simplify_transformation (array
, dim
, mask
, INT_MIN
, gfc_max
);
5509 /* Transform minloc or maxloc of an array, according to MASK,
5510 to the scalar result. This code is mostly identical to
5511 simplify_transformation_to_scalar. */
5514 simplify_minmaxloc_to_scalar (gfc_expr
*result
, gfc_expr
*array
, gfc_expr
*mask
,
5515 gfc_expr
*extremum
, int sign
, bool back_val
)
5518 gfc_constructor
*array_ctor
, *mask_ctor
;
5521 mpz_set_si (result
->value
.integer
, 0);
5524 /* Shortcut for constant .FALSE. MASK. */
5526 && mask
->expr_type
== EXPR_CONSTANT
5527 && !mask
->value
.logical
)
5530 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
5531 if (mask
&& mask
->expr_type
== EXPR_ARRAY
)
5532 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
5536 mpz_init_set_si (count
, 0);
5539 mpz_add_ui (count
, count
, 1);
5540 a
= array_ctor
->expr
;
5541 array_ctor
= gfc_constructor_next (array_ctor
);
5542 /* A constant MASK equals .TRUE. here and can be ignored. */
5545 m
= mask_ctor
->expr
;
5546 mask_ctor
= gfc_constructor_next (mask_ctor
);
5547 if (!m
->value
.logical
)
5550 if (min_max_choose (a
, extremum
, sign
, back_val
) > 0)
5551 mpz_set (result
->value
.integer
, count
);
5554 gfc_free_expr (extremum
);
5558 /* Simplify minloc / maxloc in the absence of a dim argument. */
5561 simplify_minmaxloc_nodim (gfc_expr
*result
, gfc_expr
*extremum
,
5562 gfc_expr
*array
, gfc_expr
*mask
, int sign
,
5565 ssize_t res
[GFC_MAX_DIMENSIONS
];
5567 gfc_constructor
*result_ctor
, *array_ctor
, *mask_ctor
;
5568 ssize_t count
[GFC_MAX_DIMENSIONS
], extent
[GFC_MAX_DIMENSIONS
],
5569 sstride
[GFC_MAX_DIMENSIONS
];
5574 for (i
= 0; i
<array
->rank
; i
++)
5577 /* Shortcut for constant .FALSE. MASK. */
5579 && mask
->expr_type
== EXPR_CONSTANT
5580 && !mask
->value
.logical
)
5583 if (array
->shape
== NULL
)
5586 for (i
= 0; i
< array
->rank
; i
++)
5589 sstride
[i
] = (i
== 0) ? 1 : sstride
[i
-1] * mpz_get_si (array
->shape
[i
-1]);
5590 extent
[i
] = mpz_get_si (array
->shape
[i
]);
5595 continue_loop
= true;
5596 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
5597 if (mask
&& mask
->rank
> 0)
5598 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
5602 /* Loop over the array elements (and mask), keeping track of
5603 the indices to return. */
5604 while (continue_loop
)
5608 a
= array_ctor
->expr
;
5611 m
= mask_ctor
->expr
;
5612 ma
= m
->value
.logical
;
5613 mask_ctor
= gfc_constructor_next (mask_ctor
);
5618 if (ma
&& min_max_choose (a
, extremum
, sign
, back_val
) > 0)
5620 for (i
= 0; i
<array
->rank
; i
++)
5623 array_ctor
= gfc_constructor_next (array_ctor
);
5625 } while (count
[0] != extent
[0]);
5629 /* When we get to the end of a dimension, reset it and increment
5630 the next dimension. */
5633 if (n
>= array
->rank
)
5635 continue_loop
= false;
5640 } while (count
[n
] == extent
[n
]);
5644 gfc_free_expr (extremum
);
5645 result_ctor
= gfc_constructor_first (result
->value
.constructor
);
5646 for (i
= 0; i
<array
->rank
; i
++)
5649 r_expr
= result_ctor
->expr
;
5650 mpz_set_si (r_expr
->value
.integer
, res
[i
] + 1);
5651 result_ctor
= gfc_constructor_next (result_ctor
);
5656 /* Helper function for gfc_simplify_minmaxloc - build an array
5657 expression with n elements. */
5660 new_array (bt type
, int kind
, int n
, locus
*where
)
5665 result
= gfc_get_array_expr (type
, kind
, where
);
5667 result
->shape
= gfc_get_shape(1);
5668 mpz_init_set_si (result
->shape
[0], n
);
5669 for (i
= 0; i
< n
; i
++)
5671 gfc_constructor_append_expr (&result
->value
.constructor
,
5672 gfc_get_constant_expr (type
, kind
, where
),
5679 /* Simplify minloc and maxloc. This code is mostly identical to
5680 simplify_transformation_to_array. */
5683 simplify_minmaxloc_to_array (gfc_expr
*result
, gfc_expr
*array
,
5684 gfc_expr
*dim
, gfc_expr
*mask
,
5685 gfc_expr
*extremum
, int sign
, bool back_val
)
5688 int done
, i
, n
, arraysize
, resultsize
, dim_index
, dim_extent
, dim_stride
;
5689 gfc_expr
**arrayvec
, **resultvec
, **base
, **src
, **dest
;
5690 gfc_constructor
*array_ctor
, *mask_ctor
, *result_ctor
;
5692 int count
[GFC_MAX_DIMENSIONS
], extent
[GFC_MAX_DIMENSIONS
],
5693 sstride
[GFC_MAX_DIMENSIONS
], dstride
[GFC_MAX_DIMENSIONS
],
5694 tmpstride
[GFC_MAX_DIMENSIONS
];
5696 /* Shortcut for constant .FALSE. MASK. */
5698 && mask
->expr_type
== EXPR_CONSTANT
5699 && !mask
->value
.logical
)
5702 /* Build an indexed table for array element expressions to minimize
5703 linked-list traversal. Masked elements are set to NULL. */
5704 gfc_array_size (array
, &size
);
5705 arraysize
= mpz_get_ui (size
);
5708 arrayvec
= XCNEWVEC (gfc_expr
*, arraysize
);
5710 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
5712 if (mask
&& mask
->expr_type
== EXPR_ARRAY
)
5713 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
5715 for (i
= 0; i
< arraysize
; ++i
)
5717 arrayvec
[i
] = array_ctor
->expr
;
5718 array_ctor
= gfc_constructor_next (array_ctor
);
5722 if (!mask_ctor
->expr
->value
.logical
)
5725 mask_ctor
= gfc_constructor_next (mask_ctor
);
5729 /* Same for the result expression. */
5730 gfc_array_size (result
, &size
);
5731 resultsize
= mpz_get_ui (size
);
5734 resultvec
= XCNEWVEC (gfc_expr
*, resultsize
);
5735 result_ctor
= gfc_constructor_first (result
->value
.constructor
);
5736 for (i
= 0; i
< resultsize
; ++i
)
5738 resultvec
[i
] = result_ctor
->expr
;
5739 result_ctor
= gfc_constructor_next (result_ctor
);
5742 gfc_extract_int (dim
, &dim_index
);
5743 dim_index
-= 1; /* zero-base index */
5747 for (i
= 0, n
= 0; i
< array
->rank
; ++i
)
5750 tmpstride
[i
] = (i
== 0) ? 1 : tmpstride
[i
-1] * mpz_get_si (array
->shape
[i
-1]);
5753 dim_extent
= mpz_get_si (array
->shape
[i
]);
5754 dim_stride
= tmpstride
[i
];
5758 extent
[n
] = mpz_get_si (array
->shape
[i
]);
5759 sstride
[n
] = tmpstride
[i
];
5760 dstride
[n
] = (n
== 0) ? 1 : dstride
[n
-1] * extent
[n
-1];
5764 done
= resultsize
<= 0;
5770 ex
= gfc_copy_expr (extremum
);
5771 for (src
= base
, n
= 0; n
< dim_extent
; src
+= dim_stride
, ++n
)
5773 if (*src
&& min_max_choose (*src
, ex
, sign
, back_val
) > 0)
5774 mpz_set_si ((*dest
)->value
.integer
, n
+ 1);
5783 while (!done
&& count
[n
] == extent
[n
])
5786 base
-= sstride
[n
] * extent
[n
];
5787 dest
-= dstride
[n
] * extent
[n
];
5790 if (n
< result
->rank
)
5792 /* If the nested loop is unrolled GFC_MAX_DIMENSIONS
5793 times, we'd warn for the last iteration, because the
5794 array index will have already been incremented to the
5795 array sizes, and we can't tell that this must make
5796 the test against result->rank false, because ranks
5797 must not exceed GFC_MAX_DIMENSIONS. */
5798 GCC_DIAGNOSTIC_PUSH_IGNORED (-Warray
-bounds
)
5809 /* Place updated expression in result constructor. */
5810 result_ctor
= gfc_constructor_first (result
->value
.constructor
);
5811 for (i
= 0; i
< resultsize
; ++i
)
5813 result_ctor
->expr
= resultvec
[i
];
5814 result_ctor
= gfc_constructor_next (result_ctor
);
5823 /* Simplify minloc and maxloc for constant arrays. */
5826 gfc_simplify_minmaxloc (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
,
5827 gfc_expr
*kind
, gfc_expr
*back
, int sign
)
5833 bool back_val
= false;
5835 if (!is_constant_array_expr (array
)
5836 || !gfc_is_constant_expr (dim
))
5840 && !is_constant_array_expr (mask
)
5841 && mask
->expr_type
!= EXPR_CONSTANT
)
5846 if (gfc_extract_int (kind
, &ikind
, -1))
5850 ikind
= gfc_default_integer_kind
;
5854 if (back
->expr_type
!= EXPR_CONSTANT
)
5857 back_val
= back
->value
.logical
;
5867 extremum
= gfc_get_constant_expr (array
->ts
.type
, array
->ts
.kind
, &array
->where
);
5868 init_result_expr (extremum
, init_val
, array
);
5872 result
= transformational_result (array
, dim
, BT_INTEGER
,
5873 ikind
, &array
->where
);
5874 init_result_expr (result
, 0, array
);
5876 if (array
->rank
== 1)
5877 return simplify_minmaxloc_to_scalar (result
, array
, mask
, extremum
,
5880 return simplify_minmaxloc_to_array (result
, array
, dim
, mask
, extremum
,
5885 result
= new_array (BT_INTEGER
, ikind
, array
->rank
, &array
->where
);
5886 return simplify_minmaxloc_nodim (result
, extremum
, array
, mask
,
5892 gfc_simplify_minloc (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
, gfc_expr
*kind
,
5895 return gfc_simplify_minmaxloc (array
, dim
, mask
, kind
, back
, -1);
5899 gfc_simplify_maxloc (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
, gfc_expr
*kind
,
5902 return gfc_simplify_minmaxloc (array
, dim
, mask
, kind
, back
, 1);
5905 /* Simplify findloc to scalar. Similar to
5906 simplify_minmaxloc_to_scalar. */
5909 simplify_findloc_to_scalar (gfc_expr
*result
, gfc_expr
*array
, gfc_expr
*value
,
5910 gfc_expr
*mask
, int back_val
)
5913 gfc_constructor
*array_ctor
, *mask_ctor
;
5916 mpz_set_si (result
->value
.integer
, 0);
5918 /* Shortcut for constant .FALSE. MASK. */
5920 && mask
->expr_type
== EXPR_CONSTANT
5921 && !mask
->value
.logical
)
5924 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
5925 if (mask
&& mask
->expr_type
== EXPR_ARRAY
)
5926 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
5930 mpz_init_set_si (count
, 0);
5933 mpz_add_ui (count
, count
, 1);
5934 a
= array_ctor
->expr
;
5935 array_ctor
= gfc_constructor_next (array_ctor
);
5936 /* A constant MASK equals .TRUE. here and can be ignored. */
5939 m
= mask_ctor
->expr
;
5940 mask_ctor
= gfc_constructor_next (mask_ctor
);
5941 if (!m
->value
.logical
)
5944 if (gfc_compare_expr (a
, value
, INTRINSIC_EQ
) == 0)
5946 /* We have a match. If BACK is true, continue so we find
5948 mpz_set (result
->value
.integer
, count
);
5957 /* Simplify findloc in the absence of a dim argument. Similar to
5958 simplify_minmaxloc_nodim. */
5961 simplify_findloc_nodim (gfc_expr
*result
, gfc_expr
*value
, gfc_expr
*array
,
5962 gfc_expr
*mask
, bool back_val
)
5964 ssize_t res
[GFC_MAX_DIMENSIONS
];
5966 gfc_constructor
*result_ctor
, *array_ctor
, *mask_ctor
;
5967 ssize_t count
[GFC_MAX_DIMENSIONS
], extent
[GFC_MAX_DIMENSIONS
],
5968 sstride
[GFC_MAX_DIMENSIONS
];
5973 for (i
= 0; i
< array
->rank
; i
++)
5976 /* Shortcut for constant .FALSE. MASK. */
5978 && mask
->expr_type
== EXPR_CONSTANT
5979 && !mask
->value
.logical
)
5982 for (i
= 0; i
< array
->rank
; i
++)
5985 sstride
[i
] = (i
== 0) ? 1 : sstride
[i
-1] * mpz_get_si (array
->shape
[i
-1]);
5986 extent
[i
] = mpz_get_si (array
->shape
[i
]);
5991 continue_loop
= true;
5992 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
5993 if (mask
&& mask
->rank
> 0)
5994 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
5998 /* Loop over the array elements (and mask), keeping track of
5999 the indices to return. */
6000 while (continue_loop
)
6004 a
= array_ctor
->expr
;
6007 m
= mask_ctor
->expr
;
6008 ma
= m
->value
.logical
;
6009 mask_ctor
= gfc_constructor_next (mask_ctor
);
6014 if (ma
&& gfc_compare_expr (a
, value
, INTRINSIC_EQ
) == 0)
6016 for (i
= 0; i
< array
->rank
; i
++)
6021 array_ctor
= gfc_constructor_next (array_ctor
);
6023 } while (count
[0] != extent
[0]);
6027 /* When we get to the end of a dimension, reset it and increment
6028 the next dimension. */
6031 if (n
>= array
->rank
)
6033 continue_loop
= false;
6038 } while (count
[n
] == extent
[n
]);
6042 result_ctor
= gfc_constructor_first (result
->value
.constructor
);
6043 for (i
= 0; i
< array
->rank
; i
++)
6046 r_expr
= result_ctor
->expr
;
6047 mpz_set_si (r_expr
->value
.integer
, res
[i
] + 1);
6048 result_ctor
= gfc_constructor_next (result_ctor
);
6054 /* Simplify findloc to an array. Similar to
6055 simplify_minmaxloc_to_array. */
6058 simplify_findloc_to_array (gfc_expr
*result
, gfc_expr
*array
, gfc_expr
*value
,
6059 gfc_expr
*dim
, gfc_expr
*mask
, bool back_val
)
6062 int done
, i
, n
, arraysize
, resultsize
, dim_index
, dim_extent
, dim_stride
;
6063 gfc_expr
**arrayvec
, **resultvec
, **base
, **src
, **dest
;
6064 gfc_constructor
*array_ctor
, *mask_ctor
, *result_ctor
;
6066 int count
[GFC_MAX_DIMENSIONS
], extent
[GFC_MAX_DIMENSIONS
],
6067 sstride
[GFC_MAX_DIMENSIONS
], dstride
[GFC_MAX_DIMENSIONS
],
6068 tmpstride
[GFC_MAX_DIMENSIONS
];
6070 /* Shortcut for constant .FALSE. MASK. */
6072 && mask
->expr_type
== EXPR_CONSTANT
6073 && !mask
->value
.logical
)
6076 /* Build an indexed table for array element expressions to minimize
6077 linked-list traversal. Masked elements are set to NULL. */
6078 gfc_array_size (array
, &size
);
6079 arraysize
= mpz_get_ui (size
);
6082 arrayvec
= XCNEWVEC (gfc_expr
*, arraysize
);
6084 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
6086 if (mask
&& mask
->expr_type
== EXPR_ARRAY
)
6087 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
6089 for (i
= 0; i
< arraysize
; ++i
)
6091 arrayvec
[i
] = array_ctor
->expr
;
6092 array_ctor
= gfc_constructor_next (array_ctor
);
6096 if (!mask_ctor
->expr
->value
.logical
)
6099 mask_ctor
= gfc_constructor_next (mask_ctor
);
6103 /* Same for the result expression. */
6104 gfc_array_size (result
, &size
);
6105 resultsize
= mpz_get_ui (size
);
6108 resultvec
= XCNEWVEC (gfc_expr
*, resultsize
);
6109 result_ctor
= gfc_constructor_first (result
->value
.constructor
);
6110 for (i
= 0; i
< resultsize
; ++i
)
6112 resultvec
[i
] = result_ctor
->expr
;
6113 result_ctor
= gfc_constructor_next (result_ctor
);
6116 gfc_extract_int (dim
, &dim_index
);
6118 dim_index
-= 1; /* Zero-base index. */
6122 for (i
= 0, n
= 0; i
< array
->rank
; ++i
)
6125 tmpstride
[i
] = (i
== 0) ? 1 : tmpstride
[i
-1] * mpz_get_si (array
->shape
[i
-1]);
6128 dim_extent
= mpz_get_si (array
->shape
[i
]);
6129 dim_stride
= tmpstride
[i
];
6133 extent
[n
] = mpz_get_si (array
->shape
[i
]);
6134 sstride
[n
] = tmpstride
[i
];
6135 dstride
[n
] = (n
== 0) ? 1 : dstride
[n
-1] * extent
[n
-1];
6139 done
= resultsize
<= 0;
6144 for (src
= base
, n
= 0; n
< dim_extent
; src
+= dim_stride
, ++n
)
6146 if (*src
&& gfc_compare_expr (*src
, value
, INTRINSIC_EQ
) == 0)
6148 mpz_set_si ((*dest
)->value
.integer
, n
+ 1);
6159 while (!done
&& count
[n
] == extent
[n
])
6162 base
-= sstride
[n
] * extent
[n
];
6163 dest
-= dstride
[n
] * extent
[n
];
6166 if (n
< result
->rank
)
6168 /* If the nested loop is unrolled GFC_MAX_DIMENSIONS
6169 times, we'd warn for the last iteration, because the
6170 array index will have already been incremented to the
6171 array sizes, and we can't tell that this must make
6172 the test against result->rank false, because ranks
6173 must not exceed GFC_MAX_DIMENSIONS. */
6174 GCC_DIAGNOSTIC_PUSH_IGNORED (-Warray
-bounds
)
6185 /* Place updated expression in result constructor. */
6186 result_ctor
= gfc_constructor_first (result
->value
.constructor
);
6187 for (i
= 0; i
< resultsize
; ++i
)
6189 result_ctor
->expr
= resultvec
[i
];
6190 result_ctor
= gfc_constructor_next (result_ctor
);
6198 /* Simplify findloc. */
6201 gfc_simplify_findloc (gfc_expr
*array
, gfc_expr
*value
, gfc_expr
*dim
,
6202 gfc_expr
*mask
, gfc_expr
*kind
, gfc_expr
*back
)
6206 bool back_val
= false;
6208 if (!is_constant_array_expr (array
)
6209 || array
->shape
== NULL
6210 || !gfc_is_constant_expr (dim
))
6213 if (! gfc_is_constant_expr (value
))
6217 && !is_constant_array_expr (mask
)
6218 && mask
->expr_type
!= EXPR_CONSTANT
)
6223 if (gfc_extract_int (kind
, &ikind
, -1))
6227 ikind
= gfc_default_integer_kind
;
6231 if (back
->expr_type
!= EXPR_CONSTANT
)
6234 back_val
= back
->value
.logical
;
6239 result
= transformational_result (array
, dim
, BT_INTEGER
,
6240 ikind
, &array
->where
);
6241 init_result_expr (result
, 0, array
);
6243 if (array
->rank
== 1)
6244 return simplify_findloc_to_scalar (result
, array
, value
, mask
,
6247 return simplify_findloc_to_array (result
, array
, value
, dim
, mask
,
6252 result
= new_array (BT_INTEGER
, ikind
, array
->rank
, &array
->where
);
6253 return simplify_findloc_nodim (result
, value
, array
, mask
, back_val
);
6259 gfc_simplify_maxexponent (gfc_expr
*x
)
6261 int i
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, false);
6262 return gfc_get_int_expr (gfc_default_integer_kind
, &x
->where
,
6263 gfc_real_kinds
[i
].max_exponent
);
6268 gfc_simplify_minexponent (gfc_expr
*x
)
6270 int i
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, false);
6271 return gfc_get_int_expr (gfc_default_integer_kind
, &x
->where
,
6272 gfc_real_kinds
[i
].min_exponent
);
6277 gfc_simplify_mod (gfc_expr
*a
, gfc_expr
*p
)
6282 /* First check p. */
6283 if (p
->expr_type
!= EXPR_CONSTANT
)
6286 /* p shall not be 0. */
6291 if (mpz_cmp_ui (p
->value
.integer
, 0) == 0)
6293 gfc_error ("Argument %qs of MOD at %L shall not be zero",
6295 return &gfc_bad_expr
;
6299 if (mpfr_cmp_ui (p
->value
.real
, 0) == 0)
6301 gfc_error ("Argument %qs of MOD at %L shall not be zero",
6303 return &gfc_bad_expr
;
6307 gfc_internal_error ("gfc_simplify_mod(): Bad arguments");
6310 if (a
->expr_type
!= EXPR_CONSTANT
)
6313 kind
= a
->ts
.kind
> p
->ts
.kind
? a
->ts
.kind
: p
->ts
.kind
;
6314 result
= gfc_get_constant_expr (a
->ts
.type
, kind
, &a
->where
);
6316 if (a
->ts
.type
== BT_INTEGER
|| a
->ts
.type
== BT_UNSIGNED
)
6317 mpz_tdiv_r (result
->value
.integer
, a
->value
.integer
, p
->value
.integer
);
6320 gfc_set_model_kind (kind
);
6321 mpfr_fmod (result
->value
.real
, a
->value
.real
, p
->value
.real
,
6325 return range_check (result
, "MOD");
6330 gfc_simplify_modulo (gfc_expr
*a
, gfc_expr
*p
)
6335 /* First check p. */
6336 if (p
->expr_type
!= EXPR_CONSTANT
)
6339 /* p shall not be 0. */
6344 if (mpz_cmp_ui (p
->value
.integer
, 0) == 0)
6346 gfc_error ("Argument %qs of MODULO at %L shall not be zero",
6348 return &gfc_bad_expr
;
6352 if (mpfr_cmp_ui (p
->value
.real
, 0) == 0)
6354 gfc_error ("Argument %qs of MODULO at %L shall not be zero",
6356 return &gfc_bad_expr
;
6360 gfc_internal_error ("gfc_simplify_modulo(): Bad arguments");
6363 if (a
->expr_type
!= EXPR_CONSTANT
)
6366 kind
= a
->ts
.kind
> p
->ts
.kind
? a
->ts
.kind
: p
->ts
.kind
;
6367 result
= gfc_get_constant_expr (a
->ts
.type
, kind
, &a
->where
);
6369 if (a
->ts
.type
== BT_INTEGER
|| a
->ts
.type
== BT_UNSIGNED
)
6370 mpz_fdiv_r (result
->value
.integer
, a
->value
.integer
, p
->value
.integer
);
6373 gfc_set_model_kind (kind
);
6374 mpfr_fmod (result
->value
.real
, a
->value
.real
, p
->value
.real
,
6376 if (mpfr_cmp_ui (result
->value
.real
, 0) != 0)
6378 if (mpfr_signbit (a
->value
.real
) != mpfr_signbit (p
->value
.real
))
6379 mpfr_add (result
->value
.real
, result
->value
.real
, p
->value
.real
,
6383 mpfr_copysign (result
->value
.real
, result
->value
.real
,
6384 p
->value
.real
, GFC_RND_MODE
);
6387 return range_check (result
, "MODULO");
6392 gfc_simplify_nearest (gfc_expr
*x
, gfc_expr
*s
)
6395 mpfr_exp_t emin
, emax
;
6398 if (x
->expr_type
!= EXPR_CONSTANT
|| s
->expr_type
!= EXPR_CONSTANT
)
6401 result
= gfc_copy_expr (x
);
6403 /* Save current values of emin and emax. */
6404 emin
= mpfr_get_emin ();
6405 emax
= mpfr_get_emax ();
6407 /* Set emin and emax for the current model number. */
6408 kind
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, 0);
6409 mpfr_set_emin ((mpfr_exp_t
) gfc_real_kinds
[kind
].min_exponent
-
6410 mpfr_get_prec(result
->value
.real
) + 1);
6411 mpfr_set_emax ((mpfr_exp_t
) gfc_real_kinds
[kind
].max_exponent
);
6412 mpfr_check_range (result
->value
.real
, 0, MPFR_RNDU
);
6414 if (mpfr_sgn (s
->value
.real
) > 0)
6416 mpfr_nextabove (result
->value
.real
);
6417 mpfr_subnormalize (result
->value
.real
, 0, MPFR_RNDU
);
6421 mpfr_nextbelow (result
->value
.real
);
6422 mpfr_subnormalize (result
->value
.real
, 0, MPFR_RNDD
);
6425 mpfr_set_emin (emin
);
6426 mpfr_set_emax (emax
);
6428 /* Only NaN can occur. Do not use range check as it gives an
6429 error for denormal numbers. */
6430 if (mpfr_nan_p (result
->value
.real
) && flag_range_check
)
6432 gfc_error ("Result of NEAREST is NaN at %L", &result
->where
);
6433 gfc_free_expr (result
);
6434 return &gfc_bad_expr
;
6442 simplify_nint (const char *name
, gfc_expr
*e
, gfc_expr
*k
)
6444 gfc_expr
*itrunc
, *result
;
6447 kind
= get_kind (BT_INTEGER
, k
, name
, gfc_default_integer_kind
);
6449 return &gfc_bad_expr
;
6451 if (e
->expr_type
!= EXPR_CONSTANT
)
6454 itrunc
= gfc_copy_expr (e
);
6455 mpfr_round (itrunc
->value
.real
, e
->value
.real
);
6457 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &e
->where
);
6458 gfc_mpfr_to_mpz (result
->value
.integer
, itrunc
->value
.real
, &e
->where
);
6460 gfc_free_expr (itrunc
);
6462 return range_check (result
, name
);
6467 gfc_simplify_new_line (gfc_expr
*e
)
6471 result
= gfc_get_character_expr (e
->ts
.kind
, &e
->where
, NULL
, 1);
6472 result
->value
.character
.string
[0] = '\n';
6479 gfc_simplify_nint (gfc_expr
*e
, gfc_expr
*k
)
6481 return simplify_nint ("NINT", e
, k
);
6486 gfc_simplify_idnint (gfc_expr
*e
)
6488 return simplify_nint ("IDNINT", e
, NULL
);
6491 static int norm2_scale
;
6494 norm2_add_squared (gfc_expr
*result
, gfc_expr
*e
)
6498 gcc_assert (e
->ts
.type
== BT_REAL
&& e
->expr_type
== EXPR_CONSTANT
);
6499 gcc_assert (result
->ts
.type
== BT_REAL
6500 && result
->expr_type
== EXPR_CONSTANT
);
6502 gfc_set_model_kind (result
->ts
.kind
);
6503 int index
= gfc_validate_kind (BT_REAL
, result
->ts
.kind
, false);
6505 if (mpfr_regular_p (result
->value
.real
))
6507 exp
= mpfr_get_exp (result
->value
.real
);
6508 /* If result is getting close to overflowing, scale down. */
6509 if (exp
>= gfc_real_kinds
[index
].max_exponent
- 4
6510 && norm2_scale
<= gfc_real_kinds
[index
].max_exponent
- 2)
6513 mpfr_div_ui (result
->value
.real
, result
->value
.real
, 16,
6519 if (mpfr_regular_p (e
->value
.real
))
6521 exp
= mpfr_get_exp (e
->value
.real
);
6522 /* If e**2 would overflow or close to overflowing, scale down. */
6523 if (exp
- norm2_scale
>= gfc_real_kinds
[index
].max_exponent
/ 2 - 2)
6525 int new_scale
= gfc_real_kinds
[index
].max_exponent
/ 2 + 4;
6526 mpfr_set_ui (tmp
, 1, GFC_RND_MODE
);
6527 mpfr_set_exp (tmp
, new_scale
- norm2_scale
);
6528 mpfr_div (result
->value
.real
, result
->value
.real
, tmp
, GFC_RND_MODE
);
6529 mpfr_div (result
->value
.real
, result
->value
.real
, tmp
, GFC_RND_MODE
);
6530 norm2_scale
= new_scale
;
6535 mpfr_set_ui (tmp
, 1, GFC_RND_MODE
);
6536 mpfr_set_exp (tmp
, norm2_scale
);
6537 mpfr_div (tmp
, e
->value
.real
, tmp
, GFC_RND_MODE
);
6540 mpfr_set (tmp
, e
->value
.real
, GFC_RND_MODE
);
6541 mpfr_pow_ui (tmp
, tmp
, 2, GFC_RND_MODE
);
6542 mpfr_add (result
->value
.real
, result
->value
.real
, tmp
,
6551 norm2_do_sqrt (gfc_expr
*result
, gfc_expr
*e
)
6553 gcc_assert (e
->ts
.type
== BT_REAL
&& e
->expr_type
== EXPR_CONSTANT
);
6554 gcc_assert (result
->ts
.type
== BT_REAL
6555 && result
->expr_type
== EXPR_CONSTANT
);
6558 mpfr_set (result
->value
.real
, e
->value
.real
, GFC_RND_MODE
);
6559 mpfr_sqrt (result
->value
.real
, result
->value
.real
, GFC_RND_MODE
);
6560 if (norm2_scale
&& mpfr_regular_p (result
->value
.real
))
6564 mpfr_set_ui (tmp
, 1, GFC_RND_MODE
);
6565 mpfr_set_exp (tmp
, norm2_scale
);
6566 mpfr_mul (result
->value
.real
, result
->value
.real
, tmp
, GFC_RND_MODE
);
6576 gfc_simplify_norm2 (gfc_expr
*e
, gfc_expr
*dim
)
6581 size_zero
= gfc_is_size_zero_array (e
);
6583 if (!(is_constant_array_expr (e
) || size_zero
)
6584 || (dim
!= NULL
&& !gfc_is_constant_expr (dim
)))
6587 result
= transformational_result (e
, dim
, e
->ts
.type
, e
->ts
.kind
, &e
->where
);
6588 init_result_expr (result
, 0, NULL
);
6594 if (!dim
|| e
->rank
== 1)
6596 result
= simplify_transformation_to_scalar (result
, e
, NULL
,
6598 mpfr_sqrt (result
->value
.real
, result
->value
.real
, GFC_RND_MODE
);
6599 if (norm2_scale
&& mpfr_regular_p (result
->value
.real
))
6603 mpfr_set_ui (tmp
, 1, GFC_RND_MODE
);
6604 mpfr_set_exp (tmp
, norm2_scale
);
6605 mpfr_mul (result
->value
.real
, result
->value
.real
, tmp
, GFC_RND_MODE
);
6611 result
= simplify_transformation_to_array (result
, e
, dim
, NULL
,
6620 gfc_simplify_not (gfc_expr
*e
)
6624 if (e
->expr_type
!= EXPR_CONSTANT
)
6627 result
= gfc_get_constant_expr (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
6628 mpz_com (result
->value
.integer
, e
->value
.integer
);
6630 return range_check (result
, "NOT");
6635 gfc_simplify_null (gfc_expr
*mold
)
6641 result
= gfc_copy_expr (mold
);
6642 result
->expr_type
= EXPR_NULL
;
6645 result
= gfc_get_null_expr (NULL
);
6652 gfc_simplify_num_images (gfc_expr
*distance ATTRIBUTE_UNUSED
, gfc_expr
*failed
)
6656 if (flag_coarray
== GFC_FCOARRAY_NONE
)
6658 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
6659 return &gfc_bad_expr
;
6662 if (flag_coarray
!= GFC_FCOARRAY_SINGLE
)
6665 if (failed
&& failed
->expr_type
!= EXPR_CONSTANT
)
6668 /* FIXME: gfc_current_locus is wrong. */
6669 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
6670 &gfc_current_locus
);
6672 if (failed
&& failed
->value
.logical
!= 0)
6673 mpz_set_si (result
->value
.integer
, 0);
6675 mpz_set_si (result
->value
.integer
, 1);
6682 gfc_simplify_or (gfc_expr
*x
, gfc_expr
*y
)
6687 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
6690 kind
= x
->ts
.kind
> y
->ts
.kind
? x
->ts
.kind
: y
->ts
.kind
;
6695 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &x
->where
);
6696 mpz_ior (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
6697 return range_check (result
, "OR");
6700 return gfc_get_logical_expr (kind
, &x
->where
,
6701 x
->value
.logical
|| y
->value
.logical
);
6709 gfc_simplify_pack (gfc_expr
*array
, gfc_expr
*mask
, gfc_expr
*vector
)
6712 gfc_constructor
*array_ctor
, *mask_ctor
, *vector_ctor
;
6714 if (!is_constant_array_expr (array
)
6715 || !is_constant_array_expr (vector
)
6716 || (!gfc_is_constant_expr (mask
)
6717 && !is_constant_array_expr (mask
)))
6720 result
= gfc_get_array_expr (array
->ts
.type
, array
->ts
.kind
, &array
->where
);
6721 if (array
->ts
.type
== BT_DERIVED
)
6722 result
->ts
.u
.derived
= array
->ts
.u
.derived
;
6724 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
6725 vector_ctor
= vector
6726 ? gfc_constructor_first (vector
->value
.constructor
)
6729 if (mask
->expr_type
== EXPR_CONSTANT
6730 && mask
->value
.logical
)
6732 /* Copy all elements of ARRAY to RESULT. */
6735 gfc_constructor_append_expr (&result
->value
.constructor
,
6736 gfc_copy_expr (array_ctor
->expr
),
6739 array_ctor
= gfc_constructor_next (array_ctor
);
6740 vector_ctor
= gfc_constructor_next (vector_ctor
);
6743 else if (mask
->expr_type
== EXPR_ARRAY
)
6745 /* Copy only those elements of ARRAY to RESULT whose
6746 MASK equals .TRUE.. */
6747 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
6748 while (mask_ctor
&& array_ctor
)
6750 if (mask_ctor
->expr
->value
.logical
)
6752 gfc_constructor_append_expr (&result
->value
.constructor
,
6753 gfc_copy_expr (array_ctor
->expr
),
6755 vector_ctor
= gfc_constructor_next (vector_ctor
);
6758 array_ctor
= gfc_constructor_next (array_ctor
);
6759 mask_ctor
= gfc_constructor_next (mask_ctor
);
6763 /* Append any left-over elements from VECTOR to RESULT. */
6766 gfc_constructor_append_expr (&result
->value
.constructor
,
6767 gfc_copy_expr (vector_ctor
->expr
),
6769 vector_ctor
= gfc_constructor_next (vector_ctor
);
6772 result
->shape
= gfc_get_shape (1);
6773 gfc_array_size (result
, &result
->shape
[0]);
6775 if (array
->ts
.type
== BT_CHARACTER
)
6776 result
->ts
.u
.cl
= array
->ts
.u
.cl
;
6783 do_xor (gfc_expr
*result
, gfc_expr
*e
)
6785 gcc_assert (e
->ts
.type
== BT_LOGICAL
&& e
->expr_type
== EXPR_CONSTANT
);
6786 gcc_assert (result
->ts
.type
== BT_LOGICAL
6787 && result
->expr_type
== EXPR_CONSTANT
);
6789 result
->value
.logical
= result
->value
.logical
!= e
->value
.logical
;
6795 gfc_simplify_is_contiguous (gfc_expr
*array
)
6797 if (gfc_is_simply_contiguous (array
, false, true))
6798 return gfc_get_logical_expr (gfc_default_logical_kind
, &array
->where
, 1);
6800 if (gfc_is_not_contiguous (array
))
6801 return gfc_get_logical_expr (gfc_default_logical_kind
, &array
->where
, 0);
6808 gfc_simplify_parity (gfc_expr
*e
, gfc_expr
*dim
)
6810 return simplify_transformation (e
, dim
, NULL
, 0, do_xor
);
6815 gfc_simplify_popcnt (gfc_expr
*e
)
6820 if (e
->expr_type
!= EXPR_CONSTANT
)
6823 k
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
6825 if (flag_unsigned
&& e
->ts
.type
== BT_UNSIGNED
)
6826 res
= mpz_popcount (e
->value
.integer
);
6829 /* Convert argument to unsigned, then count the '1' bits. */
6830 mpz_init_set (x
, e
->value
.integer
);
6831 gfc_convert_mpz_to_unsigned (x
, gfc_integer_kinds
[k
].bit_size
);
6832 res
= mpz_popcount (x
);
6836 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, res
);
6841 gfc_simplify_poppar (gfc_expr
*e
)
6846 if (e
->expr_type
!= EXPR_CONSTANT
)
6849 popcnt
= gfc_simplify_popcnt (e
);
6850 gcc_assert (popcnt
);
6852 bool fail
= gfc_extract_int (popcnt
, &i
);
6855 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, i
% 2);
6860 gfc_simplify_precision (gfc_expr
*e
)
6862 int i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
6863 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
,
6864 gfc_real_kinds
[i
].precision
);
6869 gfc_simplify_product (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
)
6871 return simplify_transformation (array
, dim
, mask
, 1, gfc_multiply
);
6876 gfc_simplify_radix (gfc_expr
*e
)
6879 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
6884 i
= gfc_integer_kinds
[i
].radix
;
6888 i
= gfc_real_kinds
[i
].radix
;
6895 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, i
);
6900 gfc_simplify_range (gfc_expr
*e
)
6903 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
6908 i
= gfc_integer_kinds
[i
].range
;
6912 i
= gfc_unsigned_kinds
[i
].range
;
6917 i
= gfc_real_kinds
[i
].range
;
6924 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, i
);
6929 gfc_simplify_rank (gfc_expr
*e
)
6935 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, e
->rank
);
6940 gfc_simplify_real (gfc_expr
*e
, gfc_expr
*k
)
6942 gfc_expr
*result
= NULL
;
6943 int kind
, tmp1
, tmp2
;
6945 /* Convert BOZ to real, and return without range checking. */
6946 if (e
->ts
.type
== BT_BOZ
)
6948 /* Determine kind for conversion of the BOZ. */
6950 gfc_extract_int (k
, &kind
);
6952 kind
= gfc_default_real_kind
;
6954 if (!gfc_boz2real (e
, kind
))
6956 result
= gfc_copy_expr (e
);
6960 if (e
->ts
.type
== BT_COMPLEX
)
6961 kind
= get_kind (BT_REAL
, k
, "REAL", e
->ts
.kind
);
6963 kind
= get_kind (BT_REAL
, k
, "REAL", gfc_default_real_kind
);
6966 return &gfc_bad_expr
;
6968 if (e
->expr_type
!= EXPR_CONSTANT
)
6971 /* For explicit conversion, turn off -Wconversion and -Wconversion-extra
6973 tmp1
= warn_conversion
;
6974 tmp2
= warn_conversion_extra
;
6975 warn_conversion
= warn_conversion_extra
= 0;
6977 result
= gfc_convert_constant (e
, BT_REAL
, kind
);
6979 warn_conversion
= tmp1
;
6980 warn_conversion_extra
= tmp2
;
6982 if (result
== &gfc_bad_expr
)
6983 return &gfc_bad_expr
;
6985 return range_check (result
, "REAL");
6990 gfc_simplify_realpart (gfc_expr
*e
)
6994 if (e
->expr_type
!= EXPR_CONSTANT
)
6997 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
6998 mpc_real (result
->value
.real
, e
->value
.complex, GFC_RND_MODE
);
7000 return range_check (result
, "REALPART");
7004 gfc_simplify_repeat (gfc_expr
*e
, gfc_expr
*n
)
7009 bool have_length
= false;
7011 /* If NCOPIES isn't a constant, there's nothing we can do. */
7012 if (n
->expr_type
!= EXPR_CONSTANT
)
7015 /* If NCOPIES is negative, it's an error. */
7016 if (mpz_sgn (n
->value
.integer
) < 0)
7018 gfc_error ("Argument NCOPIES of REPEAT intrinsic is negative at %L",
7020 return &gfc_bad_expr
;
7023 /* If we don't know the character length, we can do no more. */
7024 if (e
->ts
.u
.cl
&& e
->ts
.u
.cl
->length
7025 && e
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
7027 len
= gfc_mpz_get_hwi (e
->ts
.u
.cl
->length
->value
.integer
);
7030 else if (e
->expr_type
== EXPR_CONSTANT
7031 && (e
->ts
.u
.cl
== NULL
|| e
->ts
.u
.cl
->length
== NULL
))
7033 len
= e
->value
.character
.length
;
7038 /* If the source length is 0, any value of NCOPIES is valid
7039 and everything behaves as if NCOPIES == 0. */
7042 mpz_set_ui (ncopies
, 0);
7044 mpz_set (ncopies
, n
->value
.integer
);
7046 /* Check that NCOPIES isn't too large. */
7052 /* Compute the maximum value allowed for NCOPIES: huge(cl) / len. */
7054 i
= gfc_validate_kind (BT_INTEGER
, gfc_charlen_int_kind
, false);
7058 mpz_tdiv_q (max
, gfc_integer_kinds
[i
].huge
,
7059 e
->ts
.u
.cl
->length
->value
.integer
);
7064 gfc_mpz_set_hwi (mlen
, len
);
7065 mpz_tdiv_q (max
, gfc_integer_kinds
[i
].huge
, mlen
);
7069 /* The check itself. */
7070 if (mpz_cmp (ncopies
, max
) > 0)
7073 mpz_clear (ncopies
);
7074 gfc_error ("Argument NCOPIES of REPEAT intrinsic is too large at %L",
7076 return &gfc_bad_expr
;
7081 mpz_clear (ncopies
);
7083 /* For further simplification, we need the character string to be
7085 if (e
->expr_type
!= EXPR_CONSTANT
)
7090 (e
->ts
.u
.cl
->length
&&
7091 mpz_sgn (e
->ts
.u
.cl
->length
->value
.integer
) != 0))
7093 bool fail
= gfc_extract_hwi (n
, &ncop
);
7100 return gfc_get_character_expr (e
->ts
.kind
, &e
->where
, NULL
, 0);
7102 len
= e
->value
.character
.length
;
7103 gfc_charlen_t nlen
= ncop
* len
;
7105 /* Here's a semi-arbitrary limit. If the string is longer than 1 GB
7106 (2**28 elements * 4 bytes (wide chars) per element) defer to
7107 runtime instead of consuming (unbounded) memory and CPU at
7109 if (nlen
> 268435456)
7111 gfc_warning_now (0, "Evaluation of string longer than 2**28 at %L"
7112 " deferred to runtime, expect bugs", &e
->where
);
7116 result
= gfc_get_character_expr (e
->ts
.kind
, &e
->where
, NULL
, nlen
);
7117 for (size_t i
= 0; i
< (size_t) ncop
; i
++)
7118 for (size_t j
= 0; j
< (size_t) len
; j
++)
7119 result
->value
.character
.string
[j
+i
*len
]= e
->value
.character
.string
[j
];
7121 result
->value
.character
.string
[nlen
] = '\0'; /* For debugger */
7126 /* This one is a bear, but mainly has to do with shuffling elements. */
7129 gfc_simplify_reshape (gfc_expr
*source
, gfc_expr
*shape_exp
,
7130 gfc_expr
*pad
, gfc_expr
*order_exp
)
7132 int order
[GFC_MAX_DIMENSIONS
], shape
[GFC_MAX_DIMENSIONS
];
7133 int i
, rank
, npad
, x
[GFC_MAX_DIMENSIONS
];
7137 gfc_expr
*e
, *result
;
7138 bool zerosize
= false;
7140 /* Check that argument expression types are OK. */
7141 if (!is_constant_array_expr (source
)
7142 || !is_constant_array_expr (shape_exp
)
7143 || !is_constant_array_expr (pad
)
7144 || !is_constant_array_expr (order_exp
))
7147 if (source
->shape
== NULL
)
7150 /* Proceed with simplification, unpacking the array. */
7155 for (i
= 0; i
< GFC_MAX_DIMENSIONS
; i
++)
7160 e
= gfc_constructor_lookup_expr (shape_exp
->value
.constructor
, rank
);
7164 gfc_extract_int (e
, &shape
[rank
]);
7166 gcc_assert (rank
>= 0 && rank
< GFC_MAX_DIMENSIONS
);
7167 if (shape
[rank
] < 0)
7169 gfc_error ("The SHAPE array for the RESHAPE intrinsic at %L has a "
7170 "negative value %d for dimension %d",
7171 &shape_exp
->where
, shape
[rank
], rank
+1);
7173 return &gfc_bad_expr
;
7179 gcc_assert (rank
> 0);
7181 /* Now unpack the order array if present. */
7182 if (order_exp
== NULL
)
7184 for (i
= 0; i
< rank
; i
++)
7190 int order_size
, shape_size
;
7192 if (order_exp
->rank
!= shape_exp
->rank
)
7194 gfc_error ("Shapes of ORDER at %L and SHAPE at %L are different",
7195 &order_exp
->where
, &shape_exp
->where
);
7197 return &gfc_bad_expr
;
7200 gfc_array_size (shape_exp
, &size
);
7201 shape_size
= mpz_get_ui (size
);
7203 gfc_array_size (order_exp
, &size
);
7204 order_size
= mpz_get_ui (size
);
7206 if (order_size
!= shape_size
)
7208 gfc_error ("Sizes of ORDER at %L and SHAPE at %L are different",
7209 &order_exp
->where
, &shape_exp
->where
);
7211 return &gfc_bad_expr
;
7214 for (i
= 0; i
< rank
; i
++)
7216 e
= gfc_constructor_lookup_expr (order_exp
->value
.constructor
, i
);
7219 gfc_extract_int (e
, &order
[i
]);
7221 if (order
[i
] < 1 || order
[i
] > rank
)
7223 gfc_error ("Element with a value of %d in ORDER at %L must be "
7224 "in the range [1, ..., %d] for the RESHAPE intrinsic "
7225 "near %L", order
[i
], &order_exp
->where
, rank
,
7228 return &gfc_bad_expr
;
7232 if (x
[order
[i
]] != 0)
7234 gfc_error ("ORDER at %L is not a permutation of the size of "
7235 "SHAPE at %L", &order_exp
->where
, &shape_exp
->where
);
7237 return &gfc_bad_expr
;
7243 /* Count the elements in the source and padding arrays. */
7248 gfc_array_size (pad
, &size
);
7249 npad
= mpz_get_ui (size
);
7253 gfc_array_size (source
, &size
);
7254 nsource
= mpz_get_ui (size
);
7257 /* If it weren't for that pesky permutation we could just loop
7258 through the source and round out any shortage with pad elements.
7259 But no, someone just had to have the compiler do something the
7260 user should be doing. */
7262 for (i
= 0; i
< rank
; i
++)
7265 result
= gfc_get_array_expr (source
->ts
.type
, source
->ts
.kind
,
7267 if (source
->ts
.type
== BT_DERIVED
)
7268 result
->ts
.u
.derived
= source
->ts
.u
.derived
;
7269 if (source
->ts
.type
== BT_CHARACTER
&& result
->ts
.u
.cl
== NULL
)
7270 result
->ts
= source
->ts
;
7271 result
->rank
= rank
;
7272 result
->shape
= gfc_get_shape (rank
);
7273 for (i
= 0; i
< rank
; i
++)
7275 mpz_init_set_ui (result
->shape
[i
], shape
[i
]);
7283 while (nsource
> 0 || npad
> 0)
7285 /* Figure out which element to extract. */
7286 mpz_set_ui (index
, 0);
7288 for (i
= rank
- 1; i
>= 0; i
--)
7290 mpz_add_ui (index
, index
, x
[order
[i
]]);
7292 mpz_mul_ui (index
, index
, shape
[order
[i
- 1]]);
7295 if (mpz_cmp_ui (index
, INT_MAX
) > 0)
7296 gfc_internal_error ("Reshaped array too large at %C");
7298 j
= mpz_get_ui (index
);
7301 e
= gfc_constructor_lookup_expr (source
->value
.constructor
, j
);
7308 gfc_error ("Without padding, there are not enough elements "
7309 "in the intrinsic RESHAPE source at %L to match "
7310 "the shape", &source
->where
);
7311 gfc_free_expr (result
);
7316 e
= gfc_constructor_lookup_expr (pad
->value
.constructor
, j
);
7320 gfc_constructor_append_expr (&result
->value
.constructor
,
7321 gfc_copy_expr (e
), &e
->where
);
7323 /* Calculate the next element. */
7327 if (++x
[i
] < shape
[i
])
7345 gfc_simplify_rrspacing (gfc_expr
*x
)
7351 if (x
->expr_type
!= EXPR_CONSTANT
)
7354 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
7356 result
= gfc_get_constant_expr (BT_REAL
, x
->ts
.kind
, &x
->where
);
7358 /* RRSPACING(+/- 0.0) = 0.0 */
7359 if (mpfr_zero_p (x
->value
.real
))
7361 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
7365 /* RRSPACING(inf) = NaN */
7366 if (mpfr_inf_p (x
->value
.real
))
7368 mpfr_set_nan (result
->value
.real
);
7372 /* RRSPACING(NaN) = same NaN */
7373 if (mpfr_nan_p (x
->value
.real
))
7375 mpfr_set (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
7379 /* | x * 2**(-e) | * 2**p. */
7380 mpfr_abs (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
7381 e
= - (long int) mpfr_get_exp (x
->value
.real
);
7382 mpfr_mul_2si (result
->value
.real
, result
->value
.real
, e
, GFC_RND_MODE
);
7384 p
= (long int) gfc_real_kinds
[i
].digits
;
7385 mpfr_mul_2si (result
->value
.real
, result
->value
.real
, p
, GFC_RND_MODE
);
7387 return range_check (result
, "RRSPACING");
7392 gfc_simplify_scale (gfc_expr
*x
, gfc_expr
*i
)
7394 int k
, neg_flag
, power
, exp_range
;
7395 mpfr_t scale
, radix
;
7398 if (x
->expr_type
!= EXPR_CONSTANT
|| i
->expr_type
!= EXPR_CONSTANT
)
7401 result
= gfc_get_constant_expr (BT_REAL
, x
->ts
.kind
, &x
->where
);
7403 if (mpfr_zero_p (x
->value
.real
))
7405 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
7409 k
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, false);
7411 exp_range
= gfc_real_kinds
[k
].max_exponent
- gfc_real_kinds
[k
].min_exponent
;
7413 /* This check filters out values of i that would overflow an int. */
7414 if (mpz_cmp_si (i
->value
.integer
, exp_range
+ 2) > 0
7415 || mpz_cmp_si (i
->value
.integer
, -exp_range
- 2) < 0)
7417 gfc_error ("Result of SCALE overflows its kind at %L", &result
->where
);
7418 gfc_free_expr (result
);
7419 return &gfc_bad_expr
;
7422 /* Compute scale = radix ** power. */
7423 power
= mpz_get_si (i
->value
.integer
);
7433 gfc_set_model_kind (x
->ts
.kind
);
7436 mpfr_set_ui (radix
, gfc_real_kinds
[k
].radix
, GFC_RND_MODE
);
7437 mpfr_pow_ui (scale
, radix
, power
, GFC_RND_MODE
);
7440 mpfr_div (result
->value
.real
, x
->value
.real
, scale
, GFC_RND_MODE
);
7442 mpfr_mul (result
->value
.real
, x
->value
.real
, scale
, GFC_RND_MODE
);
7444 mpfr_clears (scale
, radix
, NULL
);
7446 return range_check (result
, "SCALE");
7450 /* Variants of strspn and strcspn that operate on wide characters. */
7453 wide_strspn (const gfc_char_t
*s1
, const gfc_char_t
*s2
)
7456 const gfc_char_t
*c
;
7460 for (c
= s2
; *c
; c
++)
7474 wide_strcspn (const gfc_char_t
*s1
, const gfc_char_t
*s2
)
7477 const gfc_char_t
*c
;
7481 for (c
= s2
; *c
; c
++)
7496 gfc_simplify_scan (gfc_expr
*e
, gfc_expr
*c
, gfc_expr
*b
, gfc_expr
*kind
)
7501 size_t indx
, len
, lenc
;
7502 int k
= get_kind (BT_INTEGER
, kind
, "SCAN", gfc_default_integer_kind
);
7505 return &gfc_bad_expr
;
7507 if (e
->expr_type
!= EXPR_CONSTANT
|| c
->expr_type
!= EXPR_CONSTANT
7508 || ( b
!= NULL
&& b
->expr_type
!= EXPR_CONSTANT
))
7511 if (b
!= NULL
&& b
->value
.logical
!= 0)
7516 len
= e
->value
.character
.length
;
7517 lenc
= c
->value
.character
.length
;
7519 if (len
== 0 || lenc
== 0)
7527 indx
= wide_strcspn (e
->value
.character
.string
,
7528 c
->value
.character
.string
) + 1;
7533 for (indx
= len
; indx
> 0; indx
--)
7535 for (i
= 0; i
< lenc
; i
++)
7537 if (c
->value
.character
.string
[i
]
7538 == e
->value
.character
.string
[indx
- 1])
7546 result
= gfc_get_int_expr (k
, &e
->where
, indx
);
7547 return range_check (result
, "SCAN");
7552 gfc_simplify_selected_char_kind (gfc_expr
*e
)
7556 if (e
->expr_type
!= EXPR_CONSTANT
)
7559 if (gfc_compare_with_Cstring (e
, "ascii", false) == 0
7560 || gfc_compare_with_Cstring (e
, "default", false) == 0)
7562 else if (gfc_compare_with_Cstring (e
, "iso_10646", false) == 0)
7567 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, kind
);
7572 gfc_simplify_selected_int_kind (gfc_expr
*e
)
7576 if (e
->expr_type
!= EXPR_CONSTANT
|| gfc_extract_int (e
, &range
))
7581 for (i
= 0; gfc_integer_kinds
[i
].kind
!= 0; i
++)
7582 if (gfc_integer_kinds
[i
].range
>= range
7583 && gfc_integer_kinds
[i
].kind
< kind
)
7584 kind
= gfc_integer_kinds
[i
].kind
;
7586 if (kind
== INT_MAX
)
7589 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, kind
);
7592 /* Same as above, but with unsigneds. */
7595 gfc_simplify_selected_unsigned_kind (gfc_expr
*e
)
7599 if (e
->expr_type
!= EXPR_CONSTANT
|| gfc_extract_int (e
, &range
))
7604 for (i
= 0; gfc_unsigned_kinds
[i
].kind
!= 0; i
++)
7605 if (gfc_unsigned_kinds
[i
].range
>= range
7606 && gfc_unsigned_kinds
[i
].kind
< kind
)
7607 kind
= gfc_unsigned_kinds
[i
].kind
;
7609 if (kind
== INT_MAX
)
7612 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, kind
);
7617 gfc_simplify_selected_logical_kind (gfc_expr
*e
)
7621 if (e
->expr_type
!= EXPR_CONSTANT
|| gfc_extract_int (e
, &bits
))
7626 for (i
= 0; gfc_logical_kinds
[i
].kind
!= 0; i
++)
7627 if (gfc_logical_kinds
[i
].bit_size
>= bits
7628 && gfc_logical_kinds
[i
].kind
< kind
)
7629 kind
= gfc_logical_kinds
[i
].kind
;
7631 if (kind
== INT_MAX
)
7634 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, kind
);
7639 gfc_simplify_selected_real_kind (gfc_expr
*p
, gfc_expr
*q
, gfc_expr
*rdx
)
7641 int range
, precision
, radix
, i
, kind
, found_precision
, found_range
,
7643 locus
*loc
= &gfc_current_locus
;
7649 if (p
->expr_type
!= EXPR_CONSTANT
7650 || gfc_extract_int (p
, &precision
))
7659 if (q
->expr_type
!= EXPR_CONSTANT
7660 || gfc_extract_int (q
, &range
))
7671 if (rdx
->expr_type
!= EXPR_CONSTANT
7672 || gfc_extract_int (rdx
, &radix
))
7680 found_precision
= 0;
7684 for (i
= 0; gfc_real_kinds
[i
].kind
!= 0; i
++)
7686 if (gfc_real_kinds
[i
].precision
>= precision
)
7687 found_precision
= 1;
7689 if (gfc_real_kinds
[i
].range
>= range
)
7692 if (radix
== 0 || gfc_real_kinds
[i
].radix
== radix
)
7695 if (gfc_real_kinds
[i
].precision
>= precision
7696 && gfc_real_kinds
[i
].range
>= range
7697 && (radix
== 0 || gfc_real_kinds
[i
].radix
== radix
)
7698 && gfc_real_kinds
[i
].kind
< kind
)
7699 kind
= gfc_real_kinds
[i
].kind
;
7702 if (kind
== INT_MAX
)
7704 if (found_radix
&& found_range
&& !found_precision
)
7706 else if (found_radix
&& found_precision
&& !found_range
)
7708 else if (found_radix
&& !found_precision
&& !found_range
)
7710 else if (found_radix
)
7716 return gfc_get_int_expr (gfc_default_integer_kind
, loc
, kind
);
7721 gfc_simplify_set_exponent (gfc_expr
*x
, gfc_expr
*i
)
7724 mpfr_t exp
, absv
, log2
, pow2
, frac
;
7727 if (x
->expr_type
!= EXPR_CONSTANT
|| i
->expr_type
!= EXPR_CONSTANT
)
7730 result
= gfc_get_constant_expr (BT_REAL
, x
->ts
.kind
, &x
->where
);
7732 /* SET_EXPONENT (+/-0.0, I) = +/- 0.0
7733 SET_EXPONENT (NaN) = same NaN */
7734 if (mpfr_zero_p (x
->value
.real
) || mpfr_nan_p (x
->value
.real
))
7736 mpfr_set (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
7740 /* SET_EXPONENT (inf) = NaN */
7741 if (mpfr_inf_p (x
->value
.real
))
7743 mpfr_set_nan (result
->value
.real
);
7747 gfc_set_model_kind (x
->ts
.kind
);
7754 mpfr_abs (absv
, x
->value
.real
, GFC_RND_MODE
);
7755 mpfr_log2 (log2
, absv
, GFC_RND_MODE
);
7757 mpfr_floor (log2
, log2
);
7758 mpfr_add_ui (exp
, log2
, 1, GFC_RND_MODE
);
7760 /* Old exponent value, and fraction. */
7761 mpfr_ui_pow (pow2
, 2, exp
, GFC_RND_MODE
);
7763 mpfr_div (frac
, x
->value
.real
, pow2
, GFC_RND_MODE
);
7766 exp2
= mpz_get_si (i
->value
.integer
);
7767 mpfr_mul_2si (result
->value
.real
, frac
, exp2
, GFC_RND_MODE
);
7769 mpfr_clears (absv
, log2
, exp
, pow2
, frac
, NULL
);
7771 return range_check (result
, "SET_EXPONENT");
7776 gfc_simplify_shape (gfc_expr
*source
, gfc_expr
*kind
)
7778 mpz_t shape
[GFC_MAX_DIMENSIONS
];
7779 gfc_expr
*result
, *e
, *f
;
7783 int k
= get_kind (BT_INTEGER
, kind
, "SHAPE", gfc_default_integer_kind
);
7785 if (source
->rank
== -1)
7788 result
= gfc_get_array_expr (BT_INTEGER
, k
, &source
->where
);
7789 result
->shape
= gfc_get_shape (1);
7790 mpz_init (result
->shape
[0]);
7792 if (source
->rank
== 0)
7795 if (source
->expr_type
== EXPR_VARIABLE
)
7797 ar
= gfc_find_array_ref (source
);
7798 t
= gfc_array_ref_shape (ar
, shape
);
7800 else if (source
->shape
)
7803 for (n
= 0; n
< source
->rank
; n
++)
7805 mpz_init (shape
[n
]);
7806 mpz_set (shape
[n
], source
->shape
[n
]);
7812 for (n
= 0; n
< source
->rank
; n
++)
7814 e
= gfc_get_constant_expr (BT_INTEGER
, k
, &source
->where
);
7817 mpz_set (e
->value
.integer
, shape
[n
]);
7820 mpz_set_ui (e
->value
.integer
, n
+ 1);
7822 f
= simplify_size (source
, e
, k
);
7826 gfc_free_expr (result
);
7833 if (e
== &gfc_bad_expr
|| range_check (e
, "SHAPE") == &gfc_bad_expr
)
7835 gfc_free_expr (result
);
7837 gfc_clear_shape (shape
, source
->rank
);
7838 return &gfc_bad_expr
;
7841 gfc_constructor_append_expr (&result
->value
.constructor
, e
, NULL
);
7845 gfc_clear_shape (shape
, source
->rank
);
7847 mpz_set_si (result
->shape
[0], source
->rank
);
7854 simplify_size (gfc_expr
*array
, gfc_expr
*dim
, int k
)
7857 gfc_expr
*return_value
;
7861 /* For unary operations, the size of the result is given by the size
7862 of the operand. For binary ones, it's the size of the first operand
7863 unless it is scalar, then it is the size of the second. */
7864 if (array
->expr_type
== EXPR_OP
&& !array
->value
.op
.uop
)
7866 gfc_expr
* replacement
;
7867 gfc_expr
* simplified
;
7869 switch (array
->value
.op
.op
)
7871 /* Unary operations. */
7873 case INTRINSIC_UPLUS
:
7874 case INTRINSIC_UMINUS
:
7875 case INTRINSIC_PARENTHESES
:
7876 replacement
= array
->value
.op
.op1
;
7879 /* Binary operations. If any one of the operands is scalar, take
7880 the other one's size. If both of them are arrays, it does not
7881 matter -- try to find one with known shape, if possible. */
7883 if (array
->value
.op
.op1
->rank
== 0)
7884 replacement
= array
->value
.op
.op2
;
7885 else if (array
->value
.op
.op2
->rank
== 0)
7886 replacement
= array
->value
.op
.op1
;
7889 simplified
= simplify_size (array
->value
.op
.op1
, dim
, k
);
7893 replacement
= array
->value
.op
.op2
;
7898 /* Try to reduce it directly if possible. */
7899 simplified
= simplify_size (replacement
, dim
, k
);
7901 /* Otherwise, we build a new SIZE call. This is hopefully at least
7902 simpler than the original one. */
7905 gfc_expr
*kind
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, k
);
7906 simplified
= gfc_build_intrinsic_call (gfc_current_ns
,
7907 GFC_ISYM_SIZE
, "size",
7909 gfc_copy_expr (replacement
),
7910 gfc_copy_expr (dim
),
7916 for (ref
= array
->ref
; ref
; ref
= ref
->next
)
7917 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.as
7918 && !gfc_resolve_array_spec (ref
->u
.ar
.as
, 0))
7923 if (!gfc_array_size (array
, &size
))
7928 if (dim
->expr_type
!= EXPR_CONSTANT
)
7931 if (array
->rank
== -1)
7934 d
= mpz_get_si (dim
->value
.integer
) - 1;
7935 if (d
< 0 || d
> array
->rank
- 1)
7937 gfc_error ("DIM argument (%d) to intrinsic SIZE at %L out of range "
7938 "(1:%d)", d
+1, &array
->where
, array
->rank
);
7939 return &gfc_bad_expr
;
7942 if (!gfc_array_dimen_size (array
, d
, &size
))
7946 return_value
= gfc_get_constant_expr (BT_INTEGER
, k
, &array
->where
);
7947 mpz_set (return_value
->value
.integer
, size
);
7950 return return_value
;
7955 gfc_simplify_size (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
)
7958 int k
= get_kind (BT_INTEGER
, kind
, "SIZE", gfc_default_integer_kind
);
7961 return &gfc_bad_expr
;
7963 result
= simplify_size (array
, dim
, k
);
7964 if (result
== NULL
|| result
== &gfc_bad_expr
)
7967 return range_check (result
, "SIZE");
7971 /* SIZEOF and C_SIZEOF return the size in bytes of an array element
7972 multiplied by the array size. */
7975 gfc_simplify_sizeof (gfc_expr
*x
)
7977 gfc_expr
*result
= NULL
;
7981 if (x
->ts
.type
== BT_CLASS
|| x
->ts
.deferred
)
7984 if (x
->ts
.type
== BT_CHARACTER
7985 && (!x
->ts
.u
.cl
|| !x
->ts
.u
.cl
->length
7986 || x
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
))
7989 if (x
->rank
&& x
->expr_type
!= EXPR_ARRAY
)
7991 if (!gfc_array_size (x
, &array_size
))
7994 mpz_clear (array_size
);
7997 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_index_integer_kind
,
7999 gfc_target_expr_size (x
, &res_size
);
8000 mpz_set_si (result
->value
.integer
, res_size
);
8006 /* STORAGE_SIZE returns the size in bits of a single array element. */
8009 gfc_simplify_storage_size (gfc_expr
*x
,
8012 gfc_expr
*result
= NULL
;
8016 if (x
->ts
.type
== BT_CLASS
|| x
->ts
.deferred
)
8019 if (x
->ts
.type
== BT_CHARACTER
&& x
->expr_type
!= EXPR_CONSTANT
8020 && (!x
->ts
.u
.cl
|| !x
->ts
.u
.cl
->length
8021 || x
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
))
8024 k
= get_kind (BT_INTEGER
, kind
, "STORAGE_SIZE", gfc_default_integer_kind
);
8026 return &gfc_bad_expr
;
8028 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &x
->where
);
8030 gfc_element_size (x
, &siz
);
8031 mpz_set_si (result
->value
.integer
, siz
);
8032 mpz_mul_ui (result
->value
.integer
, result
->value
.integer
, BITS_PER_UNIT
);
8034 return range_check (result
, "STORAGE_SIZE");
8039 gfc_simplify_sign (gfc_expr
*x
, gfc_expr
*y
)
8043 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
8046 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
8051 mpz_abs (result
->value
.integer
, x
->value
.integer
);
8052 if (mpz_sgn (y
->value
.integer
) < 0)
8053 mpz_neg (result
->value
.integer
, result
->value
.integer
);
8058 mpfr_copysign (result
->value
.real
, x
->value
.real
, y
->value
.real
,
8061 mpfr_setsign (result
->value
.real
, x
->value
.real
,
8062 mpfr_sgn (y
->value
.real
) < 0 ? 1 : 0, GFC_RND_MODE
);
8066 gfc_internal_error ("Bad type in gfc_simplify_sign");
8074 gfc_simplify_sin (gfc_expr
*x
)
8078 if (x
->expr_type
!= EXPR_CONSTANT
)
8081 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
8086 mpfr_sin (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
8090 gfc_set_model (x
->value
.real
);
8091 mpc_sin (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
8095 gfc_internal_error ("in gfc_simplify_sin(): Bad type");
8098 return range_check (result
, "SIN");
8103 gfc_simplify_sinh (gfc_expr
*x
)
8107 if (x
->expr_type
!= EXPR_CONSTANT
)
8110 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
8115 mpfr_sinh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
8119 mpc_sinh (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
8126 return range_check (result
, "SINH");
8130 /* The argument is always a double precision real that is converted to
8131 single precision. TODO: Rounding! */
8134 gfc_simplify_sngl (gfc_expr
*a
)
8139 if (a
->expr_type
!= EXPR_CONSTANT
)
8142 /* For explicit conversion, turn off -Wconversion and -Wconversion-extra
8144 tmp1
= warn_conversion
;
8145 tmp2
= warn_conversion_extra
;
8146 warn_conversion
= warn_conversion_extra
= 0;
8148 result
= gfc_real2real (a
, gfc_default_real_kind
);
8150 warn_conversion
= tmp1
;
8151 warn_conversion_extra
= tmp2
;
8153 return range_check (result
, "SNGL");
8158 gfc_simplify_spacing (gfc_expr
*x
)
8164 if (x
->expr_type
!= EXPR_CONSTANT
)
8167 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
8168 result
= gfc_get_constant_expr (BT_REAL
, x
->ts
.kind
, &x
->where
);
8170 /* SPACING(+/- 0.0) = SPACING(TINY(0.0)) = TINY(0.0) */
8171 if (mpfr_zero_p (x
->value
.real
))
8173 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].tiny
, GFC_RND_MODE
);
8177 /* SPACING(inf) = NaN */
8178 if (mpfr_inf_p (x
->value
.real
))
8180 mpfr_set_nan (result
->value
.real
);
8184 /* SPACING(NaN) = same NaN */
8185 if (mpfr_nan_p (x
->value
.real
))
8187 mpfr_set (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
8191 /* In the Fortran 95 standard, the result is b**(e - p) where b, e, and p
8192 are the radix, exponent of x, and precision. This excludes the
8193 possibility of subnormal numbers. Fortran 2003 states the result is
8194 b**max(e - p, emin - 1). */
8196 ep
= (long int) mpfr_get_exp (x
->value
.real
) - gfc_real_kinds
[i
].digits
;
8197 en
= (long int) gfc_real_kinds
[i
].min_exponent
- 1;
8198 en
= en
> ep
? en
: ep
;
8200 mpfr_set_ui (result
->value
.real
, 1, GFC_RND_MODE
);
8201 mpfr_mul_2si (result
->value
.real
, result
->value
.real
, en
, GFC_RND_MODE
);
8203 return range_check (result
, "SPACING");
8208 gfc_simplify_spread (gfc_expr
*source
, gfc_expr
*dim_expr
, gfc_expr
*ncopies_expr
)
8210 gfc_expr
*result
= NULL
;
8211 int nelem
, i
, j
, dim
, ncopies
;
8214 if ((!gfc_is_constant_expr (source
)
8215 && !is_constant_array_expr (source
))
8216 || !gfc_is_constant_expr (dim_expr
)
8217 || !gfc_is_constant_expr (ncopies_expr
))
8220 gcc_assert (dim_expr
->ts
.type
== BT_INTEGER
);
8221 gfc_extract_int (dim_expr
, &dim
);
8222 dim
-= 1; /* zero-base DIM */
8224 gcc_assert (ncopies_expr
->ts
.type
== BT_INTEGER
);
8225 gfc_extract_int (ncopies_expr
, &ncopies
);
8226 ncopies
= MAX (ncopies
, 0);
8228 /* Do not allow the array size to exceed the limit for an array
8230 if (source
->expr_type
== EXPR_ARRAY
)
8232 if (!gfc_array_size (source
, &size
))
8233 gfc_internal_error ("Failure getting length of a constant array.");
8236 mpz_init_set_ui (size
, 1);
8238 nelem
= mpz_get_si (size
) * ncopies
;
8239 if (nelem
> flag_max_array_constructor
)
8241 if (gfc_init_expr_flag
)
8243 gfc_error ("The number of elements (%d) in the array constructor "
8244 "at %L requires an increase of the allowed %d upper "
8245 "limit. See %<-fmax-array-constructor%> option.",
8246 nelem
, &source
->where
, flag_max_array_constructor
);
8247 return &gfc_bad_expr
;
8253 if (source
->expr_type
== EXPR_CONSTANT
8254 || source
->expr_type
== EXPR_STRUCTURE
)
8256 gcc_assert (dim
== 0);
8258 result
= gfc_get_array_expr (source
->ts
.type
, source
->ts
.kind
,
8260 if (source
->ts
.type
== BT_DERIVED
)
8261 result
->ts
.u
.derived
= source
->ts
.u
.derived
;
8263 result
->shape
= gfc_get_shape (result
->rank
);
8264 mpz_init_set_si (result
->shape
[0], ncopies
);
8266 for (i
= 0; i
< ncopies
; ++i
)
8267 gfc_constructor_append_expr (&result
->value
.constructor
,
8268 gfc_copy_expr (source
), NULL
);
8270 else if (source
->expr_type
== EXPR_ARRAY
)
8272 int offset
, rstride
[GFC_MAX_DIMENSIONS
], extent
[GFC_MAX_DIMENSIONS
];
8273 gfc_constructor
*source_ctor
;
8275 gcc_assert (source
->rank
< GFC_MAX_DIMENSIONS
);
8276 gcc_assert (dim
>= 0 && dim
<= source
->rank
);
8278 result
= gfc_get_array_expr (source
->ts
.type
, source
->ts
.kind
,
8280 if (source
->ts
.type
== BT_DERIVED
)
8281 result
->ts
.u
.derived
= source
->ts
.u
.derived
;
8282 result
->rank
= source
->rank
+ 1;
8283 result
->shape
= gfc_get_shape (result
->rank
);
8285 for (i
= 0, j
= 0; i
< result
->rank
; ++i
)
8288 mpz_init_set (result
->shape
[i
], source
->shape
[j
++]);
8290 mpz_init_set_si (result
->shape
[i
], ncopies
);
8292 extent
[i
] = mpz_get_si (result
->shape
[i
]);
8293 rstride
[i
] = (i
== 0) ? 1 : rstride
[i
-1] * extent
[i
-1];
8297 for (source_ctor
= gfc_constructor_first (source
->value
.constructor
);
8298 source_ctor
; source_ctor
= gfc_constructor_next (source_ctor
))
8300 for (i
= 0; i
< ncopies
; ++i
)
8301 gfc_constructor_insert_expr (&result
->value
.constructor
,
8302 gfc_copy_expr (source_ctor
->expr
),
8303 NULL
, offset
+ i
* rstride
[dim
]);
8305 offset
+= (dim
== 0 ? ncopies
: 1);
8310 gfc_error ("Simplification of SPREAD at %C not yet implemented");
8311 return &gfc_bad_expr
;
8314 if (source
->ts
.type
== BT_CHARACTER
)
8315 result
->ts
.u
.cl
= source
->ts
.u
.cl
;
8322 gfc_simplify_sqrt (gfc_expr
*e
)
8324 gfc_expr
*result
= NULL
;
8326 if (e
->expr_type
!= EXPR_CONSTANT
)
8332 if (mpfr_cmp_si (e
->value
.real
, 0) < 0)
8334 gfc_error ("Argument of SQRT at %L has a negative value",
8336 return &gfc_bad_expr
;
8338 result
= gfc_get_constant_expr (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
8339 mpfr_sqrt (result
->value
.real
, e
->value
.real
, GFC_RND_MODE
);
8343 gfc_set_model (e
->value
.real
);
8345 result
= gfc_get_constant_expr (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
8346 mpc_sqrt (result
->value
.complex, e
->value
.complex, GFC_MPC_RND_MODE
);
8350 gfc_internal_error ("invalid argument of SQRT at %L", &e
->where
);
8353 return range_check (result
, "SQRT");
8358 gfc_simplify_sum (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
)
8360 return simplify_transformation (array
, dim
, mask
, 0, gfc_add
);
8364 /* Simplify COTAN(X) where X has the unit of radian. */
8367 gfc_simplify_cotan (gfc_expr
*x
)
8372 if (x
->expr_type
!= EXPR_CONSTANT
)
8375 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
8380 mpfr_cot (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
8384 /* There is no builtin mpc_cot, so compute cot = cos / sin. */
8385 val
= &result
->value
.complex;
8386 mpc_init2 (swp
, mpfr_get_default_prec ());
8387 mpc_sin_cos (*val
, swp
, x
->value
.complex, GFC_MPC_RND_MODE
,
8389 mpc_div (*val
, swp
, *val
, GFC_MPC_RND_MODE
);
8397 return range_check (result
, "COTAN");
8402 gfc_simplify_tan (gfc_expr
*x
)
8406 if (x
->expr_type
!= EXPR_CONSTANT
)
8409 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
8414 mpfr_tan (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
8418 mpc_tan (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
8425 return range_check (result
, "TAN");
8430 gfc_simplify_tanh (gfc_expr
*x
)
8434 if (x
->expr_type
!= EXPR_CONSTANT
)
8437 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
8442 mpfr_tanh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
8446 mpc_tanh (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
8453 return range_check (result
, "TANH");
8458 gfc_simplify_tiny (gfc_expr
*e
)
8463 i
= gfc_validate_kind (BT_REAL
, e
->ts
.kind
, false);
8465 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
8466 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].tiny
, GFC_RND_MODE
);
8473 gfc_simplify_trailz (gfc_expr
*e
)
8475 unsigned long tz
, bs
;
8478 if (e
->expr_type
!= EXPR_CONSTANT
)
8481 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
8482 bs
= gfc_integer_kinds
[i
].bit_size
;
8483 tz
= mpz_scan1 (e
->value
.integer
, 0);
8485 return gfc_get_int_expr (gfc_default_integer_kind
,
8486 &e
->where
, MIN (tz
, bs
));
8491 gfc_simplify_transfer (gfc_expr
*source
, gfc_expr
*mold
, gfc_expr
*size
)
8494 gfc_expr
*mold_element
;
8499 unsigned char *buffer
;
8500 size_t result_length
;
8502 if (!gfc_is_constant_expr (source
) || !gfc_is_constant_expr (size
))
8505 if (!gfc_resolve_expr (mold
))
8507 if (gfc_init_expr_flag
&& !gfc_is_constant_expr (mold
))
8510 if (!gfc_calculate_transfer_sizes (source
, mold
, size
, &source_size
,
8511 &result_size
, &result_length
))
8514 /* Calculate the size of the source. */
8515 if (source
->expr_type
== EXPR_ARRAY
&& !gfc_array_size (source
, &tmp
))
8516 gfc_internal_error ("Failure getting length of a constant array.");
8518 /* Create an empty new expression with the appropriate characteristics. */
8519 result
= gfc_get_constant_expr (mold
->ts
.type
, mold
->ts
.kind
,
8521 result
->ts
= mold
->ts
;
8523 mold_element
= (mold
->expr_type
== EXPR_ARRAY
&& mold
->value
.constructor
)
8524 ? gfc_constructor_first (mold
->value
.constructor
)->expr
8527 /* Set result character length, if needed. Note that this needs to be
8528 set even for array expressions, in order to pass this information into
8529 gfc_target_interpret_expr. */
8530 if (result
->ts
.type
== BT_CHARACTER
&& gfc_is_constant_expr (mold_element
))
8532 result
->value
.character
.length
= mold_element
->value
.character
.length
;
8534 /* Let the typespec of the result inherit the string length.
8535 This is crucial if a resulting array has size zero. */
8536 if (mold_element
->ts
.u
.cl
->length
)
8537 result
->ts
.u
.cl
->length
= gfc_copy_expr (mold_element
->ts
.u
.cl
->length
);
8539 result
->ts
.u
.cl
->length
=
8540 gfc_get_int_expr (gfc_charlen_int_kind
, NULL
,
8541 mold_element
->value
.character
.length
);
8544 /* Set the number of elements in the result, and determine its size. */
8546 if (mold
->expr_type
== EXPR_ARRAY
|| mold
->rank
|| size
)
8548 result
->expr_type
= EXPR_ARRAY
;
8550 result
->shape
= gfc_get_shape (1);
8551 mpz_init_set_ui (result
->shape
[0], result_length
);
8556 /* Allocate the buffer to store the binary version of the source. */
8557 buffer_size
= MAX (source_size
, result_size
);
8558 buffer
= (unsigned char*)alloca (buffer_size
);
8559 memset (buffer
, 0, buffer_size
);
8561 /* Now write source to the buffer. */
8562 gfc_target_encode_expr (source
, buffer
, buffer_size
);
8564 /* And read the buffer back into the new expression. */
8565 gfc_target_interpret_expr (buffer
, buffer_size
, result
, false);
8572 gfc_simplify_transpose (gfc_expr
*matrix
)
8574 int row
, matrix_rows
, col
, matrix_cols
;
8577 if (!is_constant_array_expr (matrix
))
8580 gcc_assert (matrix
->rank
== 2);
8582 if (matrix
->shape
== NULL
)
8585 result
= gfc_get_array_expr (matrix
->ts
.type
, matrix
->ts
.kind
,
8588 result
->shape
= gfc_get_shape (result
->rank
);
8589 mpz_init_set (result
->shape
[0], matrix
->shape
[1]);
8590 mpz_init_set (result
->shape
[1], matrix
->shape
[0]);
8592 if (matrix
->ts
.type
== BT_CHARACTER
)
8593 result
->ts
.u
.cl
= matrix
->ts
.u
.cl
;
8594 else if (matrix
->ts
.type
== BT_DERIVED
)
8595 result
->ts
.u
.derived
= matrix
->ts
.u
.derived
;
8597 matrix_rows
= mpz_get_si (matrix
->shape
[0]);
8598 matrix_cols
= mpz_get_si (matrix
->shape
[1]);
8599 for (row
= 0; row
< matrix_rows
; ++row
)
8600 for (col
= 0; col
< matrix_cols
; ++col
)
8602 gfc_expr
*e
= gfc_constructor_lookup_expr (matrix
->value
.constructor
,
8603 col
* matrix_rows
+ row
);
8604 gfc_constructor_insert_expr (&result
->value
.constructor
,
8605 gfc_copy_expr (e
), &matrix
->where
,
8606 row
* matrix_cols
+ col
);
8614 gfc_simplify_trim (gfc_expr
*e
)
8617 int count
, i
, len
, lentrim
;
8619 if (e
->expr_type
!= EXPR_CONSTANT
)
8622 len
= e
->value
.character
.length
;
8623 for (count
= 0, i
= 1; i
<= len
; ++i
)
8625 if (e
->value
.character
.string
[len
- i
] == ' ')
8631 lentrim
= len
- count
;
8633 result
= gfc_get_character_expr (e
->ts
.kind
, &e
->where
, NULL
, lentrim
);
8634 for (i
= 0; i
< lentrim
; i
++)
8635 result
->value
.character
.string
[i
] = e
->value
.character
.string
[i
];
8642 gfc_simplify_image_index (gfc_expr
*coarray
, gfc_expr
*sub
)
8647 gfc_constructor
*sub_cons
;
8651 if (!is_constant_array_expr (sub
))
8654 /* Follow any component references. */
8655 as
= coarray
->symtree
->n
.sym
->as
;
8656 for (ref
= coarray
->ref
; ref
; ref
= ref
->next
)
8657 if (ref
->type
== REF_COMPONENT
)
8660 if (!as
|| as
->type
== AS_DEFERRED
)
8663 /* "valid sequence of cosubscripts" are required; thus, return 0 unless
8664 the cosubscript addresses the first image. */
8666 sub_cons
= gfc_constructor_first (sub
->value
.constructor
);
8669 for (d
= 1; d
<= as
->corank
; d
++)
8674 gcc_assert (sub_cons
!= NULL
);
8676 ca_bound
= simplify_bound_dim (coarray
, NULL
, d
+ as
->rank
, 0, as
,
8678 if (ca_bound
== NULL
)
8681 if (ca_bound
== &gfc_bad_expr
)
8684 cmp
= mpz_cmp (ca_bound
->value
.integer
, sub_cons
->expr
->value
.integer
);
8688 gfc_free_expr (ca_bound
);
8689 sub_cons
= gfc_constructor_next (sub_cons
);
8693 first_image
= false;
8697 gfc_error ("Out of bounds in IMAGE_INDEX at %L for dimension %d, "
8698 "SUB has %ld and COARRAY lower bound is %ld)",
8700 mpz_get_si (sub_cons
->expr
->value
.integer
),
8701 mpz_get_si (ca_bound
->value
.integer
));
8702 gfc_free_expr (ca_bound
);
8703 return &gfc_bad_expr
;
8706 gfc_free_expr (ca_bound
);
8708 /* Check whether upperbound is valid for the multi-images case. */
8711 ca_bound
= simplify_bound_dim (coarray
, NULL
, d
+ as
->rank
, 1, as
,
8713 if (ca_bound
== &gfc_bad_expr
)
8716 if (ca_bound
&& ca_bound
->expr_type
== EXPR_CONSTANT
8717 && mpz_cmp (ca_bound
->value
.integer
,
8718 sub_cons
->expr
->value
.integer
) < 0)
8720 gfc_error ("Out of bounds in IMAGE_INDEX at %L for dimension %d, "
8721 "SUB has %ld and COARRAY upper bound is %ld)",
8723 mpz_get_si (sub_cons
->expr
->value
.integer
),
8724 mpz_get_si (ca_bound
->value
.integer
));
8725 gfc_free_expr (ca_bound
);
8726 return &gfc_bad_expr
;
8730 gfc_free_expr (ca_bound
);
8733 sub_cons
= gfc_constructor_next (sub_cons
);
8736 gcc_assert (sub_cons
== NULL
);
8738 if (flag_coarray
!= GFC_FCOARRAY_SINGLE
&& !first_image
)
8741 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
8742 &gfc_current_locus
);
8744 mpz_set_si (result
->value
.integer
, 1);
8746 mpz_set_si (result
->value
.integer
, 0);
8752 gfc_simplify_image_status (gfc_expr
*image
, gfc_expr
*team ATTRIBUTE_UNUSED
)
8754 if (flag_coarray
== GFC_FCOARRAY_NONE
)
8756 gfc_current_locus
= *gfc_current_intrinsic_where
;
8757 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
8758 return &gfc_bad_expr
;
8761 /* Simplification is possible for fcoarray = single only. For all other modes
8762 the result depends on runtime conditions. */
8763 if (flag_coarray
!= GFC_FCOARRAY_SINGLE
)
8766 if (gfc_is_constant_expr (image
))
8769 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
8771 if (mpz_get_si (image
->value
.integer
) == 1)
8772 mpz_set_si (result
->value
.integer
, 0);
8774 mpz_set_si (result
->value
.integer
, GFC_STAT_STOPPED_IMAGE
);
8783 gfc_simplify_this_image (gfc_expr
*coarray
, gfc_expr
*dim
,
8784 gfc_expr
*distance ATTRIBUTE_UNUSED
)
8786 if (flag_coarray
!= GFC_FCOARRAY_SINGLE
)
8789 /* If no coarray argument has been passed or when the first argument
8790 is actually a distance argument. */
8791 if (coarray
== NULL
|| !gfc_is_coarray (coarray
))
8794 /* FIXME: gfc_current_locus is wrong. */
8795 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
8796 &gfc_current_locus
);
8797 mpz_set_si (result
->value
.integer
, 1);
8801 /* For -fcoarray=single, this_image(A) is the same as lcobound(A). */
8802 return simplify_cobound (coarray
, dim
, NULL
, 0);
8807 gfc_simplify_ubound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
)
8809 return simplify_bound (array
, dim
, kind
, 1);
8813 gfc_simplify_ucobound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
)
8815 return simplify_cobound (array
, dim
, kind
, 1);
8820 gfc_simplify_unpack (gfc_expr
*vector
, gfc_expr
*mask
, gfc_expr
*field
)
8822 gfc_expr
*result
, *e
;
8823 gfc_constructor
*vector_ctor
, *mask_ctor
, *field_ctor
;
8825 if (!is_constant_array_expr (vector
)
8826 || !is_constant_array_expr (mask
)
8827 || (!gfc_is_constant_expr (field
)
8828 && !is_constant_array_expr (field
)))
8831 result
= gfc_get_array_expr (vector
->ts
.type
, vector
->ts
.kind
,
8833 if (vector
->ts
.type
== BT_DERIVED
)
8834 result
->ts
.u
.derived
= vector
->ts
.u
.derived
;
8835 result
->rank
= mask
->rank
;
8836 result
->shape
= gfc_copy_shape (mask
->shape
, mask
->rank
);
8838 if (vector
->ts
.type
== BT_CHARACTER
)
8839 result
->ts
.u
.cl
= vector
->ts
.u
.cl
;
8841 vector_ctor
= gfc_constructor_first (vector
->value
.constructor
);
8842 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
8844 = field
->expr_type
== EXPR_ARRAY
8845 ? gfc_constructor_first (field
->value
.constructor
)
8850 if (mask_ctor
->expr
->value
.logical
)
8854 e
= gfc_copy_expr (vector_ctor
->expr
);
8855 vector_ctor
= gfc_constructor_next (vector_ctor
);
8859 gfc_free_expr (result
);
8863 else if (field
->expr_type
== EXPR_ARRAY
)
8866 e
= gfc_copy_expr (field_ctor
->expr
);
8869 /* Not enough elements in array FIELD. */
8870 gfc_free_expr (result
);
8871 return &gfc_bad_expr
;
8875 e
= gfc_copy_expr (field
);
8877 gfc_constructor_append_expr (&result
->value
.constructor
, e
, NULL
);
8879 mask_ctor
= gfc_constructor_next (mask_ctor
);
8880 field_ctor
= gfc_constructor_next (field_ctor
);
8888 gfc_simplify_verify (gfc_expr
*s
, gfc_expr
*set
, gfc_expr
*b
, gfc_expr
*kind
)
8892 size_t index
, len
, lenset
;
8894 int k
= get_kind (BT_INTEGER
, kind
, "VERIFY", gfc_default_integer_kind
);
8897 return &gfc_bad_expr
;
8899 if (s
->expr_type
!= EXPR_CONSTANT
|| set
->expr_type
!= EXPR_CONSTANT
8900 || ( b
!= NULL
&& b
->expr_type
!= EXPR_CONSTANT
))
8903 if (b
!= NULL
&& b
->value
.logical
!= 0)
8908 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &s
->where
);
8910 len
= s
->value
.character
.length
;
8911 lenset
= set
->value
.character
.length
;
8915 mpz_set_ui (result
->value
.integer
, 0);
8923 mpz_set_ui (result
->value
.integer
, 1);
8927 index
= wide_strspn (s
->value
.character
.string
,
8928 set
->value
.character
.string
) + 1;
8937 mpz_set_ui (result
->value
.integer
, len
);
8940 for (index
= len
; index
> 0; index
--)
8942 for (i
= 0; i
< lenset
; i
++)
8944 if (s
->value
.character
.string
[index
- 1]
8945 == set
->value
.character
.string
[i
])
8953 mpz_set_ui (result
->value
.integer
, index
);
8959 gfc_simplify_xor (gfc_expr
*x
, gfc_expr
*y
)
8964 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
8967 kind
= x
->ts
.kind
> y
->ts
.kind
? x
->ts
.kind
: y
->ts
.kind
;
8972 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &x
->where
);
8973 mpz_xor (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
8974 return range_check (result
, "XOR");
8977 return gfc_get_logical_expr (kind
, &x
->where
,
8978 (x
->value
.logical
&& !y
->value
.logical
)
8979 || (!x
->value
.logical
&& y
->value
.logical
));
8987 /****************** Constant simplification *****************/
8989 /* Master function to convert one constant to another. While this is
8990 used as a simplification function, it requires the destination type
8991 and kind information which is supplied by a special case in
8995 gfc_convert_constant (gfc_expr
*e
, bt type
, int kind
)
8997 gfc_expr
*result
, *(*f
) (gfc_expr
*, int);
8998 gfc_constructor
*c
, *t
;
9015 f
= gfc_int2complex
;
9038 f
= gfc_uint2complex
;
9061 f
= gfc_real2complex
;
9072 f
= gfc_complex2int
;
9075 f
= gfc_complex2uint
;
9078 f
= gfc_complex2real
;
9081 f
= gfc_complex2complex
;
9110 f
= gfc_hollerith2int
;
9113 /* Hollerith is for legacy code, we do not currently support
9114 converting this to UNSIGNED. */
9119 f
= gfc_hollerith2real
;
9123 f
= gfc_hollerith2complex
;
9127 f
= gfc_hollerith2character
;
9131 f
= gfc_hollerith2logical
;
9143 f
= gfc_character2int
;
9150 f
= gfc_character2real
;
9154 f
= gfc_character2complex
;
9158 f
= gfc_character2character
;
9162 f
= gfc_character2logical
;
9172 return &gfc_bad_expr
;
9177 switch (e
->expr_type
)
9180 result
= f (e
, kind
);
9182 return &gfc_bad_expr
;
9186 if (!gfc_is_constant_expr (e
))
9189 result
= gfc_get_array_expr (type
, kind
, &e
->where
);
9190 result
->shape
= gfc_copy_shape (e
->shape
, e
->rank
);
9191 result
->rank
= e
->rank
;
9193 for (c
= gfc_constructor_first (e
->value
.constructor
);
9194 c
; c
= gfc_constructor_next (c
))
9197 if (c
->iterator
== NULL
)
9199 if (c
->expr
->expr_type
== EXPR_ARRAY
)
9200 tmp
= gfc_convert_constant (c
->expr
, type
, kind
);
9201 else if (c
->expr
->expr_type
== EXPR_OP
)
9203 if (!gfc_simplify_expr (c
->expr
, 1))
9204 return &gfc_bad_expr
;
9205 tmp
= f (c
->expr
, kind
);
9208 tmp
= f (c
->expr
, kind
);
9211 tmp
= gfc_convert_constant (c
->expr
, type
, kind
);
9213 if (tmp
== NULL
|| tmp
== &gfc_bad_expr
)
9215 gfc_free_expr (result
);
9219 t
= gfc_constructor_append_expr (&result
->value
.constructor
,
9222 t
->iterator
= gfc_copy_iterator (c
->iterator
);
9235 /* Function for converting character constants. */
9237 gfc_convert_char_constant (gfc_expr
*e
, bt type ATTRIBUTE_UNUSED
, int kind
)
9242 if (!gfc_is_constant_expr (e
))
9245 if (e
->expr_type
== EXPR_CONSTANT
)
9247 /* Simple case of a scalar. */
9248 result
= gfc_get_constant_expr (BT_CHARACTER
, kind
, &e
->where
);
9250 return &gfc_bad_expr
;
9252 result
->value
.character
.length
= e
->value
.character
.length
;
9253 result
->value
.character
.string
9254 = gfc_get_wide_string (e
->value
.character
.length
+ 1);
9255 memcpy (result
->value
.character
.string
, e
->value
.character
.string
,
9256 (e
->value
.character
.length
+ 1) * sizeof (gfc_char_t
));
9258 /* Check we only have values representable in the destination kind. */
9259 for (i
= 0; i
< result
->value
.character
.length
; i
++)
9260 if (!gfc_check_character_range (result
->value
.character
.string
[i
],
9263 gfc_error ("Character %qs in string at %L cannot be converted "
9264 "into character kind %d",
9265 gfc_print_wide_char (result
->value
.character
.string
[i
]),
9267 gfc_free_expr (result
);
9268 return &gfc_bad_expr
;
9273 else if (e
->expr_type
== EXPR_ARRAY
)
9275 /* For an array constructor, we convert each constructor element. */
9278 result
= gfc_get_array_expr (type
, kind
, &e
->where
);
9279 result
->shape
= gfc_copy_shape (e
->shape
, e
->rank
);
9280 result
->rank
= e
->rank
;
9281 result
->ts
.u
.cl
= e
->ts
.u
.cl
;
9283 for (c
= gfc_constructor_first (e
->value
.constructor
);
9284 c
; c
= gfc_constructor_next (c
))
9286 gfc_expr
*tmp
= gfc_convert_char_constant (c
->expr
, type
, kind
);
9287 if (tmp
== &gfc_bad_expr
)
9289 gfc_free_expr (result
);
9290 return &gfc_bad_expr
;
9295 gfc_free_expr (result
);
9299 gfc_constructor_append_expr (&result
->value
.constructor
,
9311 gfc_simplify_compiler_options (void)
9316 str
= gfc_get_option_string ();
9317 result
= gfc_get_character_expr (gfc_default_character_kind
,
9318 &gfc_current_locus
, str
, strlen (str
));
9325 gfc_simplify_compiler_version (void)
9330 len
= strlen ("GCC version ") + strlen (version_string
);
9331 buffer
= XALLOCAVEC (char, len
+ 1);
9332 snprintf (buffer
, len
+ 1, "GCC version %s", version_string
);
9333 return gfc_get_character_expr (gfc_default_character_kind
,
9334 &gfc_current_locus
, buffer
, len
);
9337 /* Simplification routines for intrinsics of IEEE modules. */
9340 simplify_ieee_selected_real_kind (gfc_expr
*expr
)
9342 gfc_actual_arglist
*arg
;
9343 gfc_expr
*p
= NULL
, *q
= NULL
, *rdx
= NULL
;
9345 arg
= expr
->value
.function
.actual
;
9349 q
= arg
->next
->expr
;
9350 if (arg
->next
->next
)
9351 rdx
= arg
->next
->next
->expr
;
9354 /* Currently, if IEEE is supported and this module is built, it means
9355 all our floating-point types conform to IEEE. Hence, we simply handle
9356 IEEE_SELECTED_REAL_KIND like SELECTED_REAL_KIND. */
9357 return gfc_simplify_selected_real_kind (p
, q
, rdx
);
9361 simplify_ieee_support (gfc_expr
*expr
)
9363 /* We consider that if the IEEE modules are loaded, we have full support
9364 for flags, halting and rounding, which are the three functions
9365 (IEEE_SUPPORT_{FLAG,HALTING,ROUNDING}) allowed in constant
9366 expressions. One day, we will need libgfortran to detect support and
9367 communicate it back to us, allowing for partial support. */
9369 return gfc_get_logical_expr (gfc_default_logical_kind
, &expr
->where
,
9374 matches_ieee_function_name (gfc_symbol
*sym
, const char *name
)
9376 int n
= strlen(name
);
9378 if (!strncmp(sym
->name
, name
, n
))
9381 /* If a generic was used and renamed, we need more work to find out.
9382 Compare the specific name. */
9383 if (sym
->generic
&& !strncmp(sym
->generic
->sym
->name
, name
, n
))
9390 gfc_simplify_ieee_functions (gfc_expr
*expr
)
9392 gfc_symbol
* sym
= expr
->symtree
->n
.sym
;
9394 if (matches_ieee_function_name(sym
, "ieee_selected_real_kind"))
9395 return simplify_ieee_selected_real_kind (expr
);
9396 else if (matches_ieee_function_name(sym
, "ieee_support_flag")
9397 || matches_ieee_function_name(sym
, "ieee_support_halting")
9398 || matches_ieee_function_name(sym
, "ieee_support_rounding"))
9399 return simplify_ieee_support (expr
);