1 /* Simplify intrinsic functions at compile-time.
2 Copyright (C) 2000-2022 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 convert_mpz_to_unsigned (mpz_t x
, int bitsize
)
157 /* Confirm that no bits above the signed range are unset if we
158 are doing range checking. */
159 if (flag_range_check
!= 0)
160 gcc_assert (mpz_scan0 (x
, bitsize
-1) == ULONG_MAX
);
162 mpz_init_set_ui (mask
, 1);
163 mpz_mul_2exp (mask
, mask
, bitsize
);
164 mpz_sub_ui (mask
, mask
, 1);
166 mpz_and (x
, x
, mask
);
172 /* Confirm that no bits above the signed range are set if we
173 are doing range checking. */
174 if (flag_range_check
!= 0)
175 gcc_assert (mpz_scan1 (x
, bitsize
-1) == ULONG_MAX
);
180 /* Converts an mpz_t unsigned variable into a signed one, assuming
181 two's complement representations and a binary width of bitsize.
182 If the bitsize-1 bit is set, this is taken as a sign bit and
183 the number is converted to the corresponding negative number. */
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 for (c
= gfc_constructor_first (e
->value
.constructor
);
237 c
; c
= gfc_constructor_next (c
))
238 if (c
->expr
->expr_type
!= EXPR_CONSTANT
239 && c
->expr
->expr_type
!= EXPR_STRUCTURE
)
245 /* Check and expand the constructor. */
246 if (!array_OK
&& gfc_init_expr_flag
&& e
->rank
== 1)
248 array_OK
= gfc_reduce_init_expr (e
);
249 /* gfc_reduce_init_expr resets the flag. */
250 gfc_init_expr_flag
= true;
255 /* Recheck to make sure that any EXPR_ARRAYs have gone. */
256 for (c
= gfc_constructor_first (e
->value
.constructor
);
257 c
; c
= gfc_constructor_next (c
))
258 if (c
->expr
->expr_type
!= EXPR_CONSTANT
259 && c
->expr
->expr_type
!= EXPR_STRUCTURE
)
262 /* Make sure that the array has a valid shape. */
263 if (e
->shape
== NULL
&& e
->rank
== 1)
265 if (!gfc_array_size(e
, &size
))
267 e
->shape
= gfc_get_shape (1);
268 mpz_init_set (e
->shape
[0], size
);
275 /* Test for a size zero array. */
277 gfc_is_size_zero_array (gfc_expr
*array
)
280 if (array
->rank
== 0)
283 if (array
->expr_type
== EXPR_VARIABLE
&& array
->rank
> 0
284 && array
->symtree
->n
.sym
->attr
.flavor
== FL_PARAMETER
285 && array
->shape
!= NULL
)
287 for (int i
= 0; i
< array
->rank
; i
++)
288 if (mpz_cmp_si (array
->shape
[i
], 0) <= 0)
294 if (array
->expr_type
== EXPR_ARRAY
)
295 return array
->value
.constructor
== NULL
;
301 /* Initialize a transformational result expression with a given value. */
304 init_result_expr (gfc_expr
*e
, int init
, gfc_expr
*array
)
306 if (e
&& e
->expr_type
== EXPR_ARRAY
)
308 gfc_constructor
*ctor
= gfc_constructor_first (e
->value
.constructor
);
311 init_result_expr (ctor
->expr
, init
, array
);
312 ctor
= gfc_constructor_next (ctor
);
315 else if (e
&& e
->expr_type
== EXPR_CONSTANT
)
317 int i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
318 HOST_WIDE_INT length
;
324 e
->value
.logical
= (init
? 1 : 0);
329 mpz_set (e
->value
.integer
, gfc_integer_kinds
[i
].min_int
);
330 else if (init
== INT_MAX
)
331 mpz_set (e
->value
.integer
, gfc_integer_kinds
[i
].huge
);
333 mpz_set_si (e
->value
.integer
, init
);
339 mpfr_set (e
->value
.real
, gfc_real_kinds
[i
].huge
, GFC_RND_MODE
);
340 mpfr_neg (e
->value
.real
, e
->value
.real
, GFC_RND_MODE
);
342 else if (init
== INT_MAX
)
343 mpfr_set (e
->value
.real
, gfc_real_kinds
[i
].huge
, GFC_RND_MODE
);
345 mpfr_set_si (e
->value
.real
, init
, GFC_RND_MODE
);
349 mpc_set_si (e
->value
.complex, init
, GFC_MPC_RND_MODE
);
355 gfc_expr
*len
= gfc_simplify_len (array
, NULL
);
356 gfc_extract_hwi (len
, &length
);
357 string
= gfc_get_wide_string (length
+ 1);
358 gfc_wide_memset (string
, 0, length
);
360 else if (init
== INT_MAX
)
362 gfc_expr
*len
= gfc_simplify_len (array
, NULL
);
363 gfc_extract_hwi (len
, &length
);
364 string
= gfc_get_wide_string (length
+ 1);
365 gfc_wide_memset (string
, 255, length
);
370 string
= gfc_get_wide_string (1);
373 string
[length
] = '\0';
374 e
->value
.character
.length
= length
;
375 e
->value
.character
.string
= string
;
387 /* Helper function for gfc_simplify_dot_product() and gfc_simplify_matmul;
388 if conj_a is true, the matrix_a is complex conjugated. */
391 compute_dot_product (gfc_expr
*matrix_a
, int stride_a
, int offset_a
,
392 gfc_expr
*matrix_b
, int stride_b
, int offset_b
,
395 gfc_expr
*result
, *a
, *b
, *c
;
397 /* Set result to an INTEGER(1) 0 for numeric types and .false. for
398 LOGICAL. Mixed-mode math in the loop will promote result to the
399 correct type and kind. */
400 if (matrix_a
->ts
.type
== BT_LOGICAL
)
401 result
= gfc_get_logical_expr (gfc_default_logical_kind
, NULL
, false);
403 result
= gfc_get_int_expr (1, NULL
, 0);
404 result
->where
= matrix_a
->where
;
406 a
= gfc_constructor_lookup_expr (matrix_a
->value
.constructor
, offset_a
);
407 b
= gfc_constructor_lookup_expr (matrix_b
->value
.constructor
, offset_b
);
410 /* Copying of expressions is required as operands are free'd
411 by the gfc_arith routines. */
412 switch (result
->ts
.type
)
415 result
= gfc_or (result
,
416 gfc_and (gfc_copy_expr (a
),
423 if (conj_a
&& a
->ts
.type
== BT_COMPLEX
)
424 c
= gfc_simplify_conjg (a
);
426 c
= gfc_copy_expr (a
);
427 result
= gfc_add (result
, gfc_multiply (c
, gfc_copy_expr (b
)));
434 offset_a
+= stride_a
;
435 a
= gfc_constructor_lookup_expr (matrix_a
->value
.constructor
, offset_a
);
437 offset_b
+= stride_b
;
438 b
= gfc_constructor_lookup_expr (matrix_b
->value
.constructor
, offset_b
);
445 /* Build a result expression for transformational intrinsics,
449 transformational_result (gfc_expr
*array
, gfc_expr
*dim
, bt type
,
450 int kind
, locus
* where
)
455 if (!dim
|| array
->rank
== 1)
456 return gfc_get_constant_expr (type
, kind
, where
);
458 result
= gfc_get_array_expr (type
, kind
, where
);
459 result
->shape
= gfc_copy_shape_excluding (array
->shape
, array
->rank
, dim
);
460 result
->rank
= array
->rank
- 1;
462 /* gfc_array_size() would count the number of elements in the constructor,
463 we have not built those yet. */
465 for (i
= 0; i
< result
->rank
; ++i
)
466 nelem
*= mpz_get_ui (result
->shape
[i
]);
468 for (i
= 0; i
< nelem
; ++i
)
470 gfc_constructor_append_expr (&result
->value
.constructor
,
471 gfc_get_constant_expr (type
, kind
, where
),
479 typedef gfc_expr
* (*transformational_op
)(gfc_expr
*, gfc_expr
*);
481 /* Wrapper function, implements 'op1 += 1'. Only called if MASK
482 of COUNT intrinsic is .TRUE..
484 Interface and implementation mimics arith functions as
485 gfc_add, gfc_multiply, etc. */
488 gfc_count (gfc_expr
*op1
, gfc_expr
*op2
)
492 gcc_assert (op1
->ts
.type
== BT_INTEGER
);
493 gcc_assert (op2
->ts
.type
== BT_LOGICAL
);
494 gcc_assert (op2
->value
.logical
);
496 result
= gfc_copy_expr (op1
);
497 mpz_add_ui (result
->value
.integer
, result
->value
.integer
, 1);
505 /* Transforms an ARRAY with operation OP, according to MASK, to a
506 scalar RESULT. E.g. called if
508 REAL, PARAMETER :: array(n, m) = ...
509 REAL, PARAMETER :: s = SUM(array)
511 where OP == gfc_add(). */
514 simplify_transformation_to_scalar (gfc_expr
*result
, gfc_expr
*array
, gfc_expr
*mask
,
515 transformational_op op
)
518 gfc_constructor
*array_ctor
, *mask_ctor
;
520 /* Shortcut for constant .FALSE. MASK. */
522 && mask
->expr_type
== EXPR_CONSTANT
523 && !mask
->value
.logical
)
526 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
528 if (mask
&& mask
->expr_type
== EXPR_ARRAY
)
529 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
533 a
= array_ctor
->expr
;
534 array_ctor
= gfc_constructor_next (array_ctor
);
536 /* A constant MASK equals .TRUE. here and can be ignored. */
540 mask_ctor
= gfc_constructor_next (mask_ctor
);
541 if (!m
->value
.logical
)
545 result
= op (result
, gfc_copy_expr (a
));
553 /* Transforms an ARRAY with operation OP, according to MASK, to an
554 array RESULT. E.g. called if
556 REAL, PARAMETER :: array(n, m) = ...
557 REAL, PARAMETER :: s(n) = PROD(array, DIM=1)
559 where OP == gfc_multiply().
560 The result might be post processed using post_op. */
563 simplify_transformation_to_array (gfc_expr
*result
, gfc_expr
*array
, gfc_expr
*dim
,
564 gfc_expr
*mask
, transformational_op op
,
565 transformational_op post_op
)
568 int done
, i
, n
, arraysize
, resultsize
, dim_index
, dim_extent
, dim_stride
;
569 gfc_expr
**arrayvec
, **resultvec
, **base
, **src
, **dest
;
570 gfc_constructor
*array_ctor
, *mask_ctor
, *result_ctor
;
572 int count
[GFC_MAX_DIMENSIONS
], extent
[GFC_MAX_DIMENSIONS
],
573 sstride
[GFC_MAX_DIMENSIONS
], dstride
[GFC_MAX_DIMENSIONS
],
574 tmpstride
[GFC_MAX_DIMENSIONS
];
576 /* Shortcut for constant .FALSE. MASK. */
578 && mask
->expr_type
== EXPR_CONSTANT
579 && !mask
->value
.logical
)
582 /* Build an indexed table for array element expressions to minimize
583 linked-list traversal. Masked elements are set to NULL. */
584 gfc_array_size (array
, &size
);
585 arraysize
= mpz_get_ui (size
);
588 arrayvec
= XCNEWVEC (gfc_expr
*, arraysize
);
590 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
592 if (mask
&& mask
->expr_type
== EXPR_ARRAY
)
593 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
595 for (i
= 0; i
< arraysize
; ++i
)
597 arrayvec
[i
] = array_ctor
->expr
;
598 array_ctor
= gfc_constructor_next (array_ctor
);
602 if (!mask_ctor
->expr
->value
.logical
)
605 mask_ctor
= gfc_constructor_next (mask_ctor
);
609 /* Same for the result expression. */
610 gfc_array_size (result
, &size
);
611 resultsize
= mpz_get_ui (size
);
614 resultvec
= XCNEWVEC (gfc_expr
*, resultsize
);
615 result_ctor
= gfc_constructor_first (result
->value
.constructor
);
616 for (i
= 0; i
< resultsize
; ++i
)
618 resultvec
[i
] = result_ctor
->expr
;
619 result_ctor
= gfc_constructor_next (result_ctor
);
622 gfc_extract_int (dim
, &dim_index
);
623 dim_index
-= 1; /* zero-base index */
627 for (i
= 0, n
= 0; i
< array
->rank
; ++i
)
630 tmpstride
[i
] = (i
== 0) ? 1 : tmpstride
[i
-1] * mpz_get_si (array
->shape
[i
-1]);
633 dim_extent
= mpz_get_si (array
->shape
[i
]);
634 dim_stride
= tmpstride
[i
];
638 extent
[n
] = mpz_get_si (array
->shape
[i
]);
639 sstride
[n
] = tmpstride
[i
];
640 dstride
[n
] = (n
== 0) ? 1 : dstride
[n
-1] * extent
[n
-1];
644 done
= resultsize
<= 0;
649 for (src
= base
, n
= 0; n
< dim_extent
; src
+= dim_stride
, ++n
)
651 *dest
= op (*dest
, gfc_copy_expr (*src
));
654 *dest
= post_op (*dest
, *dest
);
661 while (!done
&& count
[n
] == extent
[n
])
664 base
-= sstride
[n
] * extent
[n
];
665 dest
-= dstride
[n
] * extent
[n
];
668 if (n
< result
->rank
)
670 /* If the nested loop is unrolled GFC_MAX_DIMENSIONS
671 times, we'd warn for the last iteration, because the
672 array index will have already been incremented to the
673 array sizes, and we can't tell that this must make
674 the test against result->rank false, because ranks
675 must not exceed GFC_MAX_DIMENSIONS. */
676 GCC_DIAGNOSTIC_PUSH_IGNORED (-Warray
-bounds
)
687 /* Place updated expression in result constructor. */
688 result_ctor
= gfc_constructor_first (result
->value
.constructor
);
689 for (i
= 0; i
< resultsize
; ++i
)
691 result_ctor
->expr
= resultvec
[i
];
692 result_ctor
= gfc_constructor_next (result_ctor
);
702 simplify_transformation (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
,
703 int init_val
, transformational_op op
)
708 size_zero
= gfc_is_size_zero_array (array
);
710 if (!(is_constant_array_expr (array
) || size_zero
)
711 || array
->shape
== NULL
712 || !gfc_is_constant_expr (dim
))
716 && !is_constant_array_expr (mask
)
717 && mask
->expr_type
!= EXPR_CONSTANT
)
720 result
= transformational_result (array
, dim
, array
->ts
.type
,
721 array
->ts
.kind
, &array
->where
);
722 init_result_expr (result
, init_val
, array
);
727 return !dim
|| array
->rank
== 1 ?
728 simplify_transformation_to_scalar (result
, array
, mask
, op
) :
729 simplify_transformation_to_array (result
, array
, dim
, mask
, op
, NULL
);
733 /********************** Simplification functions *****************************/
736 gfc_simplify_abs (gfc_expr
*e
)
740 if (e
->expr_type
!= EXPR_CONSTANT
)
746 result
= gfc_get_constant_expr (BT_INTEGER
, e
->ts
.kind
, &e
->where
);
747 mpz_abs (result
->value
.integer
, e
->value
.integer
);
748 return range_check (result
, "IABS");
751 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
752 mpfr_abs (result
->value
.real
, e
->value
.real
, GFC_RND_MODE
);
753 return range_check (result
, "ABS");
756 gfc_set_model_kind (e
->ts
.kind
);
757 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
758 mpc_abs (result
->value
.real
, e
->value
.complex, GFC_RND_MODE
);
759 return range_check (result
, "CABS");
762 gfc_internal_error ("gfc_simplify_abs(): Bad type");
768 simplify_achar_char (gfc_expr
*e
, gfc_expr
*k
, const char *name
, bool ascii
)
772 bool too_large
= false;
774 if (e
->expr_type
!= EXPR_CONSTANT
)
777 kind
= get_kind (BT_CHARACTER
, k
, name
, gfc_default_character_kind
);
779 return &gfc_bad_expr
;
781 if (mpz_cmp_si (e
->value
.integer
, 0) < 0)
783 gfc_error ("Argument of %s function at %L is negative", name
,
785 return &gfc_bad_expr
;
788 if (ascii
&& warn_surprising
&& mpz_cmp_si (e
->value
.integer
, 127) > 0)
789 gfc_warning (OPT_Wsurprising
,
790 "Argument of %s function at %L outside of range [0,127]",
793 if (kind
== 1 && mpz_cmp_si (e
->value
.integer
, 255) > 0)
798 mpz_init_set_ui (t
, 2);
799 mpz_pow_ui (t
, t
, 32);
800 mpz_sub_ui (t
, t
, 1);
801 if (mpz_cmp (e
->value
.integer
, t
) > 0)
808 gfc_error ("Argument of %s function at %L is too large for the "
809 "collating sequence of kind %d", name
, &e
->where
, kind
);
810 return &gfc_bad_expr
;
813 result
= gfc_get_character_expr (kind
, &e
->where
, NULL
, 1);
814 result
->value
.character
.string
[0] = mpz_get_ui (e
->value
.integer
);
821 /* We use the processor's collating sequence, because all
822 systems that gfortran currently works on are ASCII. */
825 gfc_simplify_achar (gfc_expr
*e
, gfc_expr
*k
)
827 return simplify_achar_char (e
, k
, "ACHAR", true);
832 gfc_simplify_acos (gfc_expr
*x
)
836 if (x
->expr_type
!= EXPR_CONSTANT
)
842 if (mpfr_cmp_si (x
->value
.real
, 1) > 0
843 || mpfr_cmp_si (x
->value
.real
, -1) < 0)
845 gfc_error ("Argument of ACOS at %L must be between -1 and 1",
847 return &gfc_bad_expr
;
849 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
850 mpfr_acos (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
854 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
855 mpc_acos (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
859 gfc_internal_error ("in gfc_simplify_acos(): Bad type");
862 return range_check (result
, "ACOS");
866 gfc_simplify_acosh (gfc_expr
*x
)
870 if (x
->expr_type
!= EXPR_CONSTANT
)
876 if (mpfr_cmp_si (x
->value
.real
, 1) < 0)
878 gfc_error ("Argument of ACOSH at %L must not be less than 1",
880 return &gfc_bad_expr
;
883 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
884 mpfr_acosh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
888 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
889 mpc_acosh (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
893 gfc_internal_error ("in gfc_simplify_acosh(): Bad type");
896 return range_check (result
, "ACOSH");
900 gfc_simplify_adjustl (gfc_expr
*e
)
906 if (e
->expr_type
!= EXPR_CONSTANT
)
909 len
= e
->value
.character
.length
;
911 for (count
= 0, i
= 0; i
< len
; ++i
)
913 ch
= e
->value
.character
.string
[i
];
919 result
= gfc_get_character_expr (e
->ts
.kind
, &e
->where
, NULL
, len
);
920 for (i
= 0; i
< len
- count
; ++i
)
921 result
->value
.character
.string
[i
] = e
->value
.character
.string
[count
+ i
];
928 gfc_simplify_adjustr (gfc_expr
*e
)
934 if (e
->expr_type
!= EXPR_CONSTANT
)
937 len
= e
->value
.character
.length
;
939 for (count
= 0, i
= len
- 1; i
>= 0; --i
)
941 ch
= e
->value
.character
.string
[i
];
947 result
= gfc_get_character_expr (e
->ts
.kind
, &e
->where
, NULL
, len
);
948 for (i
= 0; i
< count
; ++i
)
949 result
->value
.character
.string
[i
] = ' ';
951 for (i
= count
; i
< len
; ++i
)
952 result
->value
.character
.string
[i
] = e
->value
.character
.string
[i
- count
];
959 gfc_simplify_aimag (gfc_expr
*e
)
963 if (e
->expr_type
!= EXPR_CONSTANT
)
966 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
967 mpfr_set (result
->value
.real
, mpc_imagref (e
->value
.complex), GFC_RND_MODE
);
969 return range_check (result
, "AIMAG");
974 gfc_simplify_aint (gfc_expr
*e
, gfc_expr
*k
)
976 gfc_expr
*rtrunc
, *result
;
979 kind
= get_kind (BT_REAL
, k
, "AINT", e
->ts
.kind
);
981 return &gfc_bad_expr
;
983 if (e
->expr_type
!= EXPR_CONSTANT
)
986 rtrunc
= gfc_copy_expr (e
);
987 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
989 result
= gfc_real2real (rtrunc
, kind
);
991 gfc_free_expr (rtrunc
);
993 return range_check (result
, "AINT");
998 gfc_simplify_all (gfc_expr
*mask
, gfc_expr
*dim
)
1000 return simplify_transformation (mask
, dim
, NULL
, true, gfc_and
);
1005 gfc_simplify_dint (gfc_expr
*e
)
1007 gfc_expr
*rtrunc
, *result
;
1009 if (e
->expr_type
!= EXPR_CONSTANT
)
1012 rtrunc
= gfc_copy_expr (e
);
1013 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
1015 result
= gfc_real2real (rtrunc
, gfc_default_double_kind
);
1017 gfc_free_expr (rtrunc
);
1019 return range_check (result
, "DINT");
1024 gfc_simplify_dreal (gfc_expr
*e
)
1026 gfc_expr
*result
= NULL
;
1028 if (e
->expr_type
!= EXPR_CONSTANT
)
1031 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
1032 mpc_real (result
->value
.real
, e
->value
.complex, GFC_RND_MODE
);
1034 return range_check (result
, "DREAL");
1039 gfc_simplify_anint (gfc_expr
*e
, gfc_expr
*k
)
1044 kind
= get_kind (BT_REAL
, k
, "ANINT", e
->ts
.kind
);
1046 return &gfc_bad_expr
;
1048 if (e
->expr_type
!= EXPR_CONSTANT
)
1051 result
= gfc_get_constant_expr (e
->ts
.type
, kind
, &e
->where
);
1052 mpfr_round (result
->value
.real
, e
->value
.real
);
1054 return range_check (result
, "ANINT");
1059 gfc_simplify_and (gfc_expr
*x
, gfc_expr
*y
)
1064 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1067 kind
= x
->ts
.kind
> y
->ts
.kind
? x
->ts
.kind
: y
->ts
.kind
;
1072 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &x
->where
);
1073 mpz_and (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
1074 return range_check (result
, "AND");
1077 return gfc_get_logical_expr (kind
, &x
->where
,
1078 x
->value
.logical
&& y
->value
.logical
);
1087 gfc_simplify_any (gfc_expr
*mask
, gfc_expr
*dim
)
1089 return simplify_transformation (mask
, dim
, NULL
, false, gfc_or
);
1094 gfc_simplify_dnint (gfc_expr
*e
)
1098 if (e
->expr_type
!= EXPR_CONSTANT
)
1101 result
= gfc_get_constant_expr (BT_REAL
, gfc_default_double_kind
, &e
->where
);
1102 mpfr_round (result
->value
.real
, e
->value
.real
);
1104 return range_check (result
, "DNINT");
1109 gfc_simplify_asin (gfc_expr
*x
)
1113 if (x
->expr_type
!= EXPR_CONSTANT
)
1119 if (mpfr_cmp_si (x
->value
.real
, 1) > 0
1120 || mpfr_cmp_si (x
->value
.real
, -1) < 0)
1122 gfc_error ("Argument of ASIN at %L must be between -1 and 1",
1124 return &gfc_bad_expr
;
1126 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1127 mpfr_asin (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1131 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1132 mpc_asin (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1136 gfc_internal_error ("in gfc_simplify_asin(): Bad type");
1139 return range_check (result
, "ASIN");
1143 /* Convert radians to degrees, i.e., x * 180 / pi. */
1151 mpfr_const_pi (tmp
, GFC_RND_MODE
);
1152 mpfr_mul_ui (x
, x
, 180, GFC_RND_MODE
);
1153 mpfr_div (x
, x
, tmp
, GFC_RND_MODE
);
1158 /* Simplify ACOSD(X) where the returned value has units of degree. */
1161 gfc_simplify_acosd (gfc_expr
*x
)
1165 if (x
->expr_type
!= EXPR_CONSTANT
)
1168 if (mpfr_cmp_si (x
->value
.real
, 1) > 0
1169 || mpfr_cmp_si (x
->value
.real
, -1) < 0)
1171 gfc_error ("Argument of ACOSD at %L must be between -1 and 1",
1173 return &gfc_bad_expr
;
1176 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1177 mpfr_acos (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1178 rad2deg (result
->value
.real
);
1180 return range_check (result
, "ACOSD");
1184 /* Simplify asind (x) where the returned value has units of degree. */
1187 gfc_simplify_asind (gfc_expr
*x
)
1191 if (x
->expr_type
!= EXPR_CONSTANT
)
1194 if (mpfr_cmp_si (x
->value
.real
, 1) > 0
1195 || mpfr_cmp_si (x
->value
.real
, -1) < 0)
1197 gfc_error ("Argument of ASIND at %L must be between -1 and 1",
1199 return &gfc_bad_expr
;
1202 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1203 mpfr_asin (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1204 rad2deg (result
->value
.real
);
1206 return range_check (result
, "ASIND");
1210 /* Simplify atand (x) where the returned value has units of degree. */
1213 gfc_simplify_atand (gfc_expr
*x
)
1217 if (x
->expr_type
!= EXPR_CONSTANT
)
1220 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1221 mpfr_atan (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1222 rad2deg (result
->value
.real
);
1224 return range_check (result
, "ATAND");
1229 gfc_simplify_asinh (gfc_expr
*x
)
1233 if (x
->expr_type
!= EXPR_CONSTANT
)
1236 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1241 mpfr_asinh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1245 mpc_asinh (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1249 gfc_internal_error ("in gfc_simplify_asinh(): Bad type");
1252 return range_check (result
, "ASINH");
1257 gfc_simplify_atan (gfc_expr
*x
)
1261 if (x
->expr_type
!= EXPR_CONSTANT
)
1264 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1269 mpfr_atan (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1273 mpc_atan (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1277 gfc_internal_error ("in gfc_simplify_atan(): Bad type");
1280 return range_check (result
, "ATAN");
1285 gfc_simplify_atanh (gfc_expr
*x
)
1289 if (x
->expr_type
!= EXPR_CONSTANT
)
1295 if (mpfr_cmp_si (x
->value
.real
, 1) >= 0
1296 || mpfr_cmp_si (x
->value
.real
, -1) <= 0)
1298 gfc_error ("Argument of ATANH at %L must be inside the range -1 "
1300 return &gfc_bad_expr
;
1302 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1303 mpfr_atanh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1307 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1308 mpc_atanh (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1312 gfc_internal_error ("in gfc_simplify_atanh(): Bad type");
1315 return range_check (result
, "ATANH");
1320 gfc_simplify_atan2 (gfc_expr
*y
, gfc_expr
*x
)
1324 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1327 if (mpfr_zero_p (y
->value
.real
) && mpfr_zero_p (x
->value
.real
))
1329 gfc_error ("If first argument of ATAN2 at %L is zero, then the "
1330 "second argument must not be zero", &y
->where
);
1331 return &gfc_bad_expr
;
1334 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1335 mpfr_atan2 (result
->value
.real
, y
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1337 return range_check (result
, "ATAN2");
1342 gfc_simplify_bessel_j0 (gfc_expr
*x
)
1346 if (x
->expr_type
!= EXPR_CONSTANT
)
1349 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1350 mpfr_j0 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1352 return range_check (result
, "BESSEL_J0");
1357 gfc_simplify_bessel_j1 (gfc_expr
*x
)
1361 if (x
->expr_type
!= EXPR_CONSTANT
)
1364 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1365 mpfr_j1 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1367 return range_check (result
, "BESSEL_J1");
1372 gfc_simplify_bessel_jn (gfc_expr
*order
, gfc_expr
*x
)
1377 if (x
->expr_type
!= EXPR_CONSTANT
|| order
->expr_type
!= EXPR_CONSTANT
)
1380 n
= mpz_get_si (order
->value
.integer
);
1381 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1382 mpfr_jn (result
->value
.real
, n
, x
->value
.real
, GFC_RND_MODE
);
1384 return range_check (result
, "BESSEL_JN");
1388 /* Simplify transformational form of JN and YN. */
1391 gfc_simplify_bessel_n2 (gfc_expr
*order1
, gfc_expr
*order2
, gfc_expr
*x
,
1398 mpfr_t x2rev
, last1
, last2
;
1400 if (x
->expr_type
!= EXPR_CONSTANT
|| order1
->expr_type
!= EXPR_CONSTANT
1401 || order2
->expr_type
!= EXPR_CONSTANT
)
1404 n1
= mpz_get_si (order1
->value
.integer
);
1405 n2
= mpz_get_si (order2
->value
.integer
);
1406 result
= gfc_get_array_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1408 result
->shape
= gfc_get_shape (1);
1409 mpz_init_set_ui (result
->shape
[0], MAX (n2
-n1
+1, 0));
1414 /* Special case: x == 0; it is J0(0.0) == 1, JN(N > 0, 0.0) == 0; and
1415 YN(N, 0.0) = -Inf. */
1417 if (mpfr_cmp_ui (x
->value
.real
, 0.0) == 0)
1419 if (!jn
&& flag_range_check
)
1421 gfc_error ("Result of BESSEL_YN is -INF at %L", &result
->where
);
1422 gfc_free_expr (result
);
1423 return &gfc_bad_expr
;
1428 e
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1429 mpfr_set_ui (e
->value
.real
, 1, GFC_RND_MODE
);
1430 gfc_constructor_append_expr (&result
->value
.constructor
, e
,
1435 for (i
= n1
; i
<= n2
; i
++)
1437 e
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1439 mpfr_set_ui (e
->value
.real
, 0, GFC_RND_MODE
);
1441 mpfr_set_inf (e
->value
.real
, -1);
1442 gfc_constructor_append_expr (&result
->value
.constructor
, e
,
1449 /* Use the faster but more verbose recurrence algorithm. Bessel functions
1450 are stable for downward recursion and Neumann functions are stable
1451 for upward recursion. It is
1453 J(N-1, x) = x2rev * N * J(N, x) - J(N+1, x),
1454 Y(N+1, x) = x2rev * N * Y(N, x) - Y(N-1, x).
1455 Cf. http://dlmf.nist.gov/10.74#iv and http://dlmf.nist.gov/10.6#E1 */
1457 gfc_set_model_kind (x
->ts
.kind
);
1459 /* Get first recursion anchor. */
1463 mpfr_jn (last1
, n2
, x
->value
.real
, GFC_RND_MODE
);
1465 mpfr_yn (last1
, n1
, x
->value
.real
, GFC_RND_MODE
);
1467 e
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1468 mpfr_set (e
->value
.real
, last1
, GFC_RND_MODE
);
1469 if (range_check (e
, jn
? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr
)
1473 gfc_free_expr (result
);
1474 return &gfc_bad_expr
;
1476 gfc_constructor_append_expr (&result
->value
.constructor
, e
, &x
->where
);
1484 /* Get second recursion anchor. */
1488 mpfr_jn (last2
, n2
-1, x
->value
.real
, GFC_RND_MODE
);
1490 mpfr_yn (last2
, n1
+1, x
->value
.real
, GFC_RND_MODE
);
1492 e
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1493 mpfr_set (e
->value
.real
, last2
, GFC_RND_MODE
);
1494 if (range_check (e
, jn
? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr
)
1499 gfc_free_expr (result
);
1500 return &gfc_bad_expr
;
1503 gfc_constructor_insert_expr (&result
->value
.constructor
, e
, &x
->where
, -2);
1505 gfc_constructor_append_expr (&result
->value
.constructor
, e
, &x
->where
);
1514 /* Start actual recursion. */
1517 mpfr_ui_div (x2rev
, 2, x
->value
.real
, GFC_RND_MODE
);
1519 for (i
= 2; i
<= n2
-n1
; i
++)
1521 e
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1523 /* Special case: For YN, if the previous N gave -INF, set
1524 also N+1 to -INF. */
1525 if (!jn
&& !flag_range_check
&& mpfr_inf_p (last2
))
1527 mpfr_set_inf (e
->value
.real
, -1);
1528 gfc_constructor_append_expr (&result
->value
.constructor
, e
,
1533 mpfr_mul_si (e
->value
.real
, x2rev
, jn
? (n2
-i
+1) : (n1
+i
-1),
1535 mpfr_mul (e
->value
.real
, e
->value
.real
, last2
, GFC_RND_MODE
);
1536 mpfr_sub (e
->value
.real
, e
->value
.real
, last1
, GFC_RND_MODE
);
1538 if (range_check (e
, jn
? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr
)
1540 /* Range_check frees "e" in that case. */
1546 gfc_constructor_insert_expr (&result
->value
.constructor
, e
, &x
->where
,
1549 gfc_constructor_append_expr (&result
->value
.constructor
, e
, &x
->where
);
1551 mpfr_set (last1
, last2
, GFC_RND_MODE
);
1552 mpfr_set (last2
, e
->value
.real
, GFC_RND_MODE
);
1565 gfc_free_expr (result
);
1566 return &gfc_bad_expr
;
1571 gfc_simplify_bessel_jn2 (gfc_expr
*order1
, gfc_expr
*order2
, gfc_expr
*x
)
1573 return gfc_simplify_bessel_n2 (order1
, order2
, x
, true);
1578 gfc_simplify_bessel_y0 (gfc_expr
*x
)
1582 if (x
->expr_type
!= EXPR_CONSTANT
)
1585 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1586 mpfr_y0 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1588 return range_check (result
, "BESSEL_Y0");
1593 gfc_simplify_bessel_y1 (gfc_expr
*x
)
1597 if (x
->expr_type
!= EXPR_CONSTANT
)
1600 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1601 mpfr_y1 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1603 return range_check (result
, "BESSEL_Y1");
1608 gfc_simplify_bessel_yn (gfc_expr
*order
, gfc_expr
*x
)
1613 if (x
->expr_type
!= EXPR_CONSTANT
|| order
->expr_type
!= EXPR_CONSTANT
)
1616 n
= mpz_get_si (order
->value
.integer
);
1617 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1618 mpfr_yn (result
->value
.real
, n
, x
->value
.real
, GFC_RND_MODE
);
1620 return range_check (result
, "BESSEL_YN");
1625 gfc_simplify_bessel_yn2 (gfc_expr
*order1
, gfc_expr
*order2
, gfc_expr
*x
)
1627 return gfc_simplify_bessel_n2 (order1
, order2
, x
, false);
1632 gfc_simplify_bit_size (gfc_expr
*e
)
1634 int i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
1635 return gfc_get_int_expr (e
->ts
.kind
, &e
->where
,
1636 gfc_integer_kinds
[i
].bit_size
);
1641 gfc_simplify_btest (gfc_expr
*e
, gfc_expr
*bit
)
1645 if (e
->expr_type
!= EXPR_CONSTANT
|| bit
->expr_type
!= EXPR_CONSTANT
)
1648 if (gfc_extract_int (bit
, &b
) || b
< 0)
1649 return gfc_get_logical_expr (gfc_default_logical_kind
, &e
->where
, false);
1651 return gfc_get_logical_expr (gfc_default_logical_kind
, &e
->where
,
1652 mpz_tstbit (e
->value
.integer
, b
));
1657 compare_bitwise (gfc_expr
*i
, gfc_expr
*j
)
1662 gcc_assert (i
->ts
.type
== BT_INTEGER
);
1663 gcc_assert (j
->ts
.type
== BT_INTEGER
);
1665 mpz_init_set (x
, i
->value
.integer
);
1666 k
= gfc_validate_kind (i
->ts
.type
, i
->ts
.kind
, false);
1667 convert_mpz_to_unsigned (x
, gfc_integer_kinds
[k
].bit_size
);
1669 mpz_init_set (y
, j
->value
.integer
);
1670 k
= gfc_validate_kind (j
->ts
.type
, j
->ts
.kind
, false);
1671 convert_mpz_to_unsigned (y
, gfc_integer_kinds
[k
].bit_size
);
1673 res
= mpz_cmp (x
, y
);
1681 gfc_simplify_bge (gfc_expr
*i
, gfc_expr
*j
)
1683 if (i
->expr_type
!= EXPR_CONSTANT
|| j
->expr_type
!= EXPR_CONSTANT
)
1686 return gfc_get_logical_expr (gfc_default_logical_kind
, &i
->where
,
1687 compare_bitwise (i
, j
) >= 0);
1692 gfc_simplify_bgt (gfc_expr
*i
, gfc_expr
*j
)
1694 if (i
->expr_type
!= EXPR_CONSTANT
|| j
->expr_type
!= EXPR_CONSTANT
)
1697 return gfc_get_logical_expr (gfc_default_logical_kind
, &i
->where
,
1698 compare_bitwise (i
, j
) > 0);
1703 gfc_simplify_ble (gfc_expr
*i
, gfc_expr
*j
)
1705 if (i
->expr_type
!= EXPR_CONSTANT
|| j
->expr_type
!= EXPR_CONSTANT
)
1708 return gfc_get_logical_expr (gfc_default_logical_kind
, &i
->where
,
1709 compare_bitwise (i
, j
) <= 0);
1714 gfc_simplify_blt (gfc_expr
*i
, gfc_expr
*j
)
1716 if (i
->expr_type
!= EXPR_CONSTANT
|| j
->expr_type
!= EXPR_CONSTANT
)
1719 return gfc_get_logical_expr (gfc_default_logical_kind
, &i
->where
,
1720 compare_bitwise (i
, j
) < 0);
1725 gfc_simplify_ceiling (gfc_expr
*e
, gfc_expr
*k
)
1727 gfc_expr
*ceil
, *result
;
1730 kind
= get_kind (BT_INTEGER
, k
, "CEILING", gfc_default_integer_kind
);
1732 return &gfc_bad_expr
;
1734 if (e
->expr_type
!= EXPR_CONSTANT
)
1737 ceil
= gfc_copy_expr (e
);
1738 mpfr_ceil (ceil
->value
.real
, e
->value
.real
);
1740 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &e
->where
);
1741 gfc_mpfr_to_mpz (result
->value
.integer
, ceil
->value
.real
, &e
->where
);
1743 gfc_free_expr (ceil
);
1745 return range_check (result
, "CEILING");
1750 gfc_simplify_char (gfc_expr
*e
, gfc_expr
*k
)
1752 return simplify_achar_char (e
, k
, "CHAR", false);
1756 /* Common subroutine for simplifying CMPLX, COMPLEX and DCMPLX. */
1759 simplify_cmplx (const char *name
, gfc_expr
*x
, gfc_expr
*y
, int kind
)
1763 if (x
->expr_type
!= EXPR_CONSTANT
1764 || (y
!= NULL
&& y
->expr_type
!= EXPR_CONSTANT
))
1767 result
= gfc_get_constant_expr (BT_COMPLEX
, kind
, &x
->where
);
1772 mpc_set_z (result
->value
.complex, x
->value
.integer
, GFC_MPC_RND_MODE
);
1776 mpc_set_fr (result
->value
.complex, x
->value
.real
, GFC_RND_MODE
);
1780 mpc_set (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1784 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (x)");
1788 return range_check (result
, name
);
1793 mpfr_set_z (mpc_imagref (result
->value
.complex),
1794 y
->value
.integer
, GFC_RND_MODE
);
1798 mpfr_set (mpc_imagref (result
->value
.complex),
1799 y
->value
.real
, GFC_RND_MODE
);
1803 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (y)");
1806 return range_check (result
, name
);
1811 gfc_simplify_cmplx (gfc_expr
*x
, gfc_expr
*y
, gfc_expr
*k
)
1815 kind
= get_kind (BT_REAL
, k
, "CMPLX", gfc_default_complex_kind
);
1817 return &gfc_bad_expr
;
1819 return simplify_cmplx ("CMPLX", x
, y
, kind
);
1824 gfc_simplify_complex (gfc_expr
*x
, gfc_expr
*y
)
1828 if (x
->ts
.type
== BT_INTEGER
&& y
->ts
.type
== BT_INTEGER
)
1829 kind
= gfc_default_complex_kind
;
1830 else if (x
->ts
.type
== BT_REAL
|| y
->ts
.type
== BT_INTEGER
)
1832 else if (x
->ts
.type
== BT_INTEGER
|| y
->ts
.type
== BT_REAL
)
1834 else if (x
->ts
.type
== BT_REAL
&& y
->ts
.type
== BT_REAL
)
1835 kind
= (x
->ts
.kind
> y
->ts
.kind
) ? x
->ts
.kind
: y
->ts
.kind
;
1839 return simplify_cmplx ("COMPLEX", x
, y
, kind
);
1844 gfc_simplify_conjg (gfc_expr
*e
)
1848 if (e
->expr_type
!= EXPR_CONSTANT
)
1851 result
= gfc_copy_expr (e
);
1852 mpc_conj (result
->value
.complex, result
->value
.complex, GFC_MPC_RND_MODE
);
1854 return range_check (result
, "CONJG");
1858 /* Simplify atan2d (x) where the unit is degree. */
1861 gfc_simplify_atan2d (gfc_expr
*y
, gfc_expr
*x
)
1865 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1868 if (mpfr_zero_p (y
->value
.real
) && mpfr_zero_p (x
->value
.real
))
1870 gfc_error ("If first argument of ATAN2D at %L is zero, then the "
1871 "second argument must not be zero", &y
->where
);
1872 return &gfc_bad_expr
;
1875 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1876 mpfr_atan2 (result
->value
.real
, y
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1877 rad2deg (result
->value
.real
);
1879 return range_check (result
, "ATAN2D");
1884 gfc_simplify_cos (gfc_expr
*x
)
1888 if (x
->expr_type
!= EXPR_CONSTANT
)
1891 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1896 mpfr_cos (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1900 gfc_set_model_kind (x
->ts
.kind
);
1901 mpc_cos (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1905 gfc_internal_error ("in gfc_simplify_cos(): Bad type");
1908 return range_check (result
, "COS");
1918 mpfr_const_pi (d2r
, GFC_RND_MODE
);
1919 mpfr_div_ui (d2r
, d2r
, 180, GFC_RND_MODE
);
1920 mpfr_mul (x
, x
, d2r
, GFC_RND_MODE
);
1925 /* Simplification routines for SIND, COSD, TAND. */
1926 #include "trigd_fe.inc"
1929 /* Simplify COSD(X) where X has the unit of degree. */
1932 gfc_simplify_cosd (gfc_expr
*x
)
1936 if (x
->expr_type
!= EXPR_CONSTANT
)
1939 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1940 mpfr_set (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1941 simplify_cosd (result
->value
.real
);
1943 return range_check (result
, "COSD");
1947 /* Simplify SIND(X) where X has the unit of degree. */
1950 gfc_simplify_sind (gfc_expr
*x
)
1954 if (x
->expr_type
!= EXPR_CONSTANT
)
1957 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1958 mpfr_set (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1959 simplify_sind (result
->value
.real
);
1961 return range_check (result
, "SIND");
1965 /* Simplify TAND(X) where X has the unit of degree. */
1968 gfc_simplify_tand (gfc_expr
*x
)
1972 if (x
->expr_type
!= EXPR_CONSTANT
)
1975 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1976 mpfr_set (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1977 simplify_tand (result
->value
.real
);
1979 return range_check (result
, "TAND");
1983 /* Simplify COTAND(X) where X has the unit of degree. */
1986 gfc_simplify_cotand (gfc_expr
*x
)
1990 if (x
->expr_type
!= EXPR_CONSTANT
)
1993 /* Implement COTAND = -TAND(x+90).
1994 TAND offers correct exact values for multiples of 30 degrees.
1995 This implementation is also compatible with the behavior of some legacy
1996 compilers. Keep this consistent with gfc_conv_intrinsic_cotand. */
1997 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1998 mpfr_set (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1999 mpfr_add_ui (result
->value
.real
, result
->value
.real
, 90, GFC_RND_MODE
);
2000 simplify_tand (result
->value
.real
);
2001 mpfr_neg (result
->value
.real
, result
->value
.real
, GFC_RND_MODE
);
2003 return range_check (result
, "COTAND");
2008 gfc_simplify_cosh (gfc_expr
*x
)
2012 if (x
->expr_type
!= EXPR_CONSTANT
)
2015 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2020 mpfr_cosh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
2024 mpc_cosh (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
2031 return range_check (result
, "COSH");
2036 gfc_simplify_count (gfc_expr
*mask
, gfc_expr
*dim
, gfc_expr
*kind
)
2041 size_zero
= gfc_is_size_zero_array (mask
);
2043 if (!(is_constant_array_expr (mask
) || size_zero
)
2044 || !gfc_is_constant_expr (dim
)
2045 || !gfc_is_constant_expr (kind
))
2048 result
= transformational_result (mask
, dim
,
2050 get_kind (BT_INTEGER
, kind
, "COUNT",
2051 gfc_default_integer_kind
),
2054 init_result_expr (result
, 0, NULL
);
2059 /* Passing MASK twice, once as data array, once as mask.
2060 Whenever gfc_count is called, '1' is added to the result. */
2061 return !dim
|| mask
->rank
== 1 ?
2062 simplify_transformation_to_scalar (result
, mask
, mask
, gfc_count
) :
2063 simplify_transformation_to_array (result
, mask
, dim
, mask
, gfc_count
, NULL
);
2066 /* Simplification routine for cshift. This works by copying the array
2067 expressions into a one-dimensional array, shuffling the values into another
2068 one-dimensional array and creating the new array expression from this. The
2069 shuffling part is basically taken from the library routine. */
2072 gfc_simplify_cshift (gfc_expr
*array
, gfc_expr
*shift
, gfc_expr
*dim
)
2076 gfc_expr
**arrayvec
, **resultvec
;
2077 gfc_expr
**rptr
, **sptr
;
2079 size_t arraysize
, shiftsize
, i
;
2080 gfc_constructor
*array_ctor
, *shift_ctor
;
2081 ssize_t
*shiftvec
, *hptr
;
2082 ssize_t shift_val
, len
;
2083 ssize_t count
[GFC_MAX_DIMENSIONS
], extent
[GFC_MAX_DIMENSIONS
],
2084 hs_ex
[GFC_MAX_DIMENSIONS
+ 1],
2085 hstride
[GFC_MAX_DIMENSIONS
], sstride
[GFC_MAX_DIMENSIONS
],
2086 a_extent
[GFC_MAX_DIMENSIONS
], a_stride
[GFC_MAX_DIMENSIONS
],
2087 h_extent
[GFC_MAX_DIMENSIONS
],
2088 ss_ex
[GFC_MAX_DIMENSIONS
+ 1];
2092 gfc_expr
**src
, **dest
;
2094 if (!is_constant_array_expr (array
))
2097 if (shift
->rank
> 0)
2098 gfc_simplify_expr (shift
, 1);
2100 if (!gfc_is_constant_expr (shift
))
2103 /* Make dim zero-based. */
2106 if (!gfc_is_constant_expr (dim
))
2108 which
= mpz_get_si (dim
->value
.integer
) - 1;
2113 if (array
->shape
== NULL
)
2116 gfc_array_size (array
, &size
);
2117 arraysize
= mpz_get_ui (size
);
2120 result
= gfc_get_array_expr (array
->ts
.type
, array
->ts
.kind
, &array
->where
);
2121 result
->shape
= gfc_copy_shape (array
->shape
, array
->rank
);
2122 result
->rank
= array
->rank
;
2123 result
->ts
.u
.derived
= array
->ts
.u
.derived
;
2128 arrayvec
= XCNEWVEC (gfc_expr
*, arraysize
);
2129 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
2130 for (i
= 0; i
< arraysize
; i
++)
2132 arrayvec
[i
] = array_ctor
->expr
;
2133 array_ctor
= gfc_constructor_next (array_ctor
);
2136 resultvec
= XCNEWVEC (gfc_expr
*, arraysize
);
2142 for (d
=0; d
< array
->rank
; d
++)
2144 a_extent
[d
] = mpz_get_si (array
->shape
[d
]);
2145 a_stride
[d
] = d
== 0 ? 1 : a_stride
[d
-1] * a_extent
[d
-1];
2148 if (shift
->rank
> 0)
2150 gfc_array_size (shift
, &size
);
2151 shiftsize
= mpz_get_ui (size
);
2153 shiftvec
= XCNEWVEC (ssize_t
, shiftsize
);
2154 shift_ctor
= gfc_constructor_first (shift
->value
.constructor
);
2155 for (d
= 0; d
< shift
->rank
; d
++)
2157 h_extent
[d
] = mpz_get_si (shift
->shape
[d
]);
2158 hstride
[d
] = d
== 0 ? 1 : hstride
[d
-1] * h_extent
[d
-1];
2164 /* Shut up compiler */
2169 for (d
=0; d
< array
->rank
; d
++)
2173 rsoffset
= a_stride
[d
];
2179 extent
[n
] = a_extent
[d
];
2180 sstride
[n
] = a_stride
[d
];
2181 ss_ex
[n
] = sstride
[n
] * extent
[n
];
2183 hs_ex
[n
] = hstride
[n
] * extent
[n
];
2192 for (i
= 0; i
< shiftsize
; i
++)
2195 val
= mpz_get_si (shift_ctor
->expr
->value
.integer
);
2200 shift_ctor
= gfc_constructor_next (shift_ctor
);
2206 shift_val
= mpz_get_si (shift
->value
.integer
);
2207 shift_val
= shift_val
% len
;
2212 continue_loop
= true;
2218 while (continue_loop
)
2226 src
= &sptr
[sh
* rsoffset
];
2228 for (n
= 0; n
< len
- sh
; n
++)
2235 for ( n
= 0; n
< sh
; n
++)
2247 while (count
[n
] == extent
[n
])
2257 continue_loop
= false;
2271 for (i
= 0; i
< arraysize
; i
++)
2273 gfc_constructor_append_expr (&result
->value
.constructor
,
2274 gfc_copy_expr (resultvec
[i
]),
2282 gfc_simplify_dcmplx (gfc_expr
*x
, gfc_expr
*y
)
2284 return simplify_cmplx ("DCMPLX", x
, y
, gfc_default_double_kind
);
2289 gfc_simplify_dble (gfc_expr
*e
)
2291 gfc_expr
*result
= NULL
;
2294 if (e
->expr_type
!= EXPR_CONSTANT
)
2297 /* For explicit conversion, turn off -Wconversion and -Wconversion-extra
2299 tmp1
= warn_conversion
;
2300 tmp2
= warn_conversion_extra
;
2301 warn_conversion
= warn_conversion_extra
= 0;
2303 result
= gfc_convert_constant (e
, BT_REAL
, gfc_default_double_kind
);
2305 warn_conversion
= tmp1
;
2306 warn_conversion_extra
= tmp2
;
2308 if (result
== &gfc_bad_expr
)
2309 return &gfc_bad_expr
;
2311 return range_check (result
, "DBLE");
2316 gfc_simplify_digits (gfc_expr
*x
)
2320 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
2325 digits
= gfc_integer_kinds
[i
].digits
;
2330 digits
= gfc_real_kinds
[i
].digits
;
2337 return gfc_get_int_expr (gfc_default_integer_kind
, NULL
, digits
);
2342 gfc_simplify_dim (gfc_expr
*x
, gfc_expr
*y
)
2347 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
2350 kind
= x
->ts
.kind
> y
->ts
.kind
? x
->ts
.kind
: y
->ts
.kind
;
2351 result
= gfc_get_constant_expr (x
->ts
.type
, kind
, &x
->where
);
2356 if (mpz_cmp (x
->value
.integer
, y
->value
.integer
) > 0)
2357 mpz_sub (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
2359 mpz_set_ui (result
->value
.integer
, 0);
2364 if (mpfr_cmp (x
->value
.real
, y
->value
.real
) > 0)
2365 mpfr_sub (result
->value
.real
, x
->value
.real
, y
->value
.real
,
2368 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
2373 gfc_internal_error ("gfc_simplify_dim(): Bad type");
2376 return range_check (result
, "DIM");
2381 gfc_simplify_dot_product (gfc_expr
*vector_a
, gfc_expr
*vector_b
)
2383 /* If vector_a is a zero-sized array, the result is 0 for INTEGER,
2384 REAL, and COMPLEX types and .false. for LOGICAL. */
2385 if (vector_a
->shape
&& mpz_get_si (vector_a
->shape
[0]) == 0)
2387 if (vector_a
->ts
.type
== BT_LOGICAL
)
2388 return gfc_get_logical_expr (gfc_default_logical_kind
, NULL
, false);
2390 return gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 0);
2393 if (!is_constant_array_expr (vector_a
)
2394 || !is_constant_array_expr (vector_b
))
2397 return compute_dot_product (vector_a
, 1, 0, vector_b
, 1, 0, true);
2402 gfc_simplify_dprod (gfc_expr
*x
, gfc_expr
*y
)
2404 gfc_expr
*a1
, *a2
, *result
;
2406 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
2409 a1
= gfc_real2real (x
, gfc_default_double_kind
);
2410 a2
= gfc_real2real (y
, gfc_default_double_kind
);
2412 result
= gfc_get_constant_expr (BT_REAL
, gfc_default_double_kind
, &x
->where
);
2413 mpfr_mul (result
->value
.real
, a1
->value
.real
, a2
->value
.real
, GFC_RND_MODE
);
2418 return range_check (result
, "DPROD");
2423 simplify_dshift (gfc_expr
*arg1
, gfc_expr
*arg2
, gfc_expr
*shiftarg
,
2427 int i
, k
, size
, shift
;
2429 if (arg1
->expr_type
!= EXPR_CONSTANT
|| arg2
->expr_type
!= EXPR_CONSTANT
2430 || shiftarg
->expr_type
!= EXPR_CONSTANT
)
2433 k
= gfc_validate_kind (BT_INTEGER
, arg1
->ts
.kind
, false);
2434 size
= gfc_integer_kinds
[k
].bit_size
;
2436 gfc_extract_int (shiftarg
, &shift
);
2438 /* DSHIFTR(I,J,SHIFT) = DSHIFTL(I,J,SIZE-SHIFT). */
2440 shift
= size
- shift
;
2442 result
= gfc_get_constant_expr (BT_INTEGER
, arg1
->ts
.kind
, &arg1
->where
);
2443 mpz_set_ui (result
->value
.integer
, 0);
2445 for (i
= 0; i
< shift
; i
++)
2446 if (mpz_tstbit (arg2
->value
.integer
, size
- shift
+ i
))
2447 mpz_setbit (result
->value
.integer
, i
);
2449 for (i
= 0; i
< size
- shift
; i
++)
2450 if (mpz_tstbit (arg1
->value
.integer
, i
))
2451 mpz_setbit (result
->value
.integer
, shift
+ i
);
2453 /* Convert to a signed value. */
2454 gfc_convert_mpz_to_signed (result
->value
.integer
, size
);
2461 gfc_simplify_dshiftr (gfc_expr
*arg1
, gfc_expr
*arg2
, gfc_expr
*shiftarg
)
2463 return simplify_dshift (arg1
, arg2
, shiftarg
, true);
2468 gfc_simplify_dshiftl (gfc_expr
*arg1
, gfc_expr
*arg2
, gfc_expr
*shiftarg
)
2470 return simplify_dshift (arg1
, arg2
, shiftarg
, false);
2475 gfc_simplify_eoshift (gfc_expr
*array
, gfc_expr
*shift
, gfc_expr
*boundary
,
2482 gfc_expr
**arrayvec
, **resultvec
;
2483 gfc_expr
**rptr
, **sptr
;
2485 size_t arraysize
, i
;
2486 gfc_constructor
*array_ctor
, *shift_ctor
, *bnd_ctor
;
2487 ssize_t shift_val
, len
;
2488 ssize_t count
[GFC_MAX_DIMENSIONS
], extent
[GFC_MAX_DIMENSIONS
],
2489 sstride
[GFC_MAX_DIMENSIONS
], a_extent
[GFC_MAX_DIMENSIONS
],
2490 a_stride
[GFC_MAX_DIMENSIONS
], ss_ex
[GFC_MAX_DIMENSIONS
+ 1];
2494 gfc_expr
**src
, **dest
;
2497 if (!is_constant_array_expr (array
))
2500 if (shift
->rank
> 0)
2501 gfc_simplify_expr (shift
, 1);
2503 if (!gfc_is_constant_expr (shift
))
2508 if (boundary
->rank
> 0)
2509 gfc_simplify_expr (boundary
, 1);
2511 if (!gfc_is_constant_expr (boundary
))
2517 if (!gfc_is_constant_expr (dim
))
2519 which
= mpz_get_si (dim
->value
.integer
) - 1;
2525 if (boundary
== NULL
)
2527 temp_boundary
= true;
2528 switch (array
->ts
.type
)
2532 bnd
= gfc_get_int_expr (array
->ts
.kind
, NULL
, 0);
2536 bnd
= gfc_get_logical_expr (array
->ts
.kind
, NULL
, 0);
2540 bnd
= gfc_get_constant_expr (array
->ts
.type
, array
->ts
.kind
, &gfc_current_locus
);
2541 mpfr_set_ui (bnd
->value
.real
, 0, GFC_RND_MODE
);
2545 bnd
= gfc_get_constant_expr (array
->ts
.type
, array
->ts
.kind
, &gfc_current_locus
);
2546 mpc_set_ui (bnd
->value
.complex, 0, GFC_RND_MODE
);
2550 s_len
= mpz_get_ui (array
->ts
.u
.cl
->length
->value
.integer
);
2551 bnd
= gfc_get_character_expr (array
->ts
.kind
, &gfc_current_locus
, NULL
, s_len
);
2561 temp_boundary
= false;
2565 gfc_array_size (array
, &size
);
2566 arraysize
= mpz_get_ui (size
);
2569 result
= gfc_get_array_expr (array
->ts
.type
, array
->ts
.kind
, &array
->where
);
2570 result
->shape
= gfc_copy_shape (array
->shape
, array
->rank
);
2571 result
->rank
= array
->rank
;
2572 result
->ts
= array
->ts
;
2577 if (array
->shape
== NULL
)
2580 arrayvec
= XCNEWVEC (gfc_expr
*, arraysize
);
2581 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
2582 for (i
= 0; i
< arraysize
; i
++)
2584 arrayvec
[i
] = array_ctor
->expr
;
2585 array_ctor
= gfc_constructor_next (array_ctor
);
2588 resultvec
= XCNEWVEC (gfc_expr
*, arraysize
);
2593 for (d
=0; d
< array
->rank
; d
++)
2595 a_extent
[d
] = mpz_get_si (array
->shape
[d
]);
2596 a_stride
[d
] = d
== 0 ? 1 : a_stride
[d
-1] * a_extent
[d
-1];
2599 if (shift
->rank
> 0)
2601 shift_ctor
= gfc_constructor_first (shift
->value
.constructor
);
2607 shift_val
= mpz_get_si (shift
->value
.integer
);
2611 bnd_ctor
= gfc_constructor_first (bnd
->value
.constructor
);
2615 /* Shut up compiler */
2620 for (d
=0; d
< array
->rank
; d
++)
2624 rsoffset
= a_stride
[d
];
2630 extent
[n
] = a_extent
[d
];
2631 sstride
[n
] = a_stride
[d
];
2632 ss_ex
[n
] = sstride
[n
] * extent
[n
];
2638 continue_loop
= true;
2643 while (continue_loop
)
2648 sh
= mpz_get_si (shift_ctor
->expr
->value
.integer
);
2652 if (( sh
>= 0 ? sh
: -sh
) > len
)
2658 delta
= (sh
>= 0) ? sh
: -sh
;
2662 src
= &sptr
[delta
* rsoffset
];
2668 dest
= &rptr
[delta
* rsoffset
];
2671 for (n
= 0; n
< len
- delta
; n
++)
2687 *dest
= gfc_copy_expr (bnd_ctor
->expr
);
2695 *dest
= gfc_copy_expr (bnd
);
2702 shift_ctor
= gfc_constructor_next (shift_ctor
);
2705 bnd_ctor
= gfc_constructor_next (bnd_ctor
);
2709 while (count
[n
] == extent
[n
])
2717 continue_loop
= false;
2729 for (i
= 0; i
< arraysize
; i
++)
2731 gfc_constructor_append_expr (&result
->value
.constructor
,
2732 gfc_copy_expr (resultvec
[i
]),
2738 gfc_free_expr (bnd
);
2744 gfc_simplify_erf (gfc_expr
*x
)
2748 if (x
->expr_type
!= EXPR_CONSTANT
)
2751 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2752 mpfr_erf (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
2754 return range_check (result
, "ERF");
2759 gfc_simplify_erfc (gfc_expr
*x
)
2763 if (x
->expr_type
!= EXPR_CONSTANT
)
2766 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2767 mpfr_erfc (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
2769 return range_check (result
, "ERFC");
2773 /* Helper functions to simplify ERFC_SCALED(x) = ERFC(x) * EXP(X**2). */
2775 #define MAX_ITER 200
2776 #define ARG_LIMIT 12
2778 /* Calculate ERFC_SCALED directly by its definition:
2780 ERFC_SCALED(x) = ERFC(x) * EXP(X**2)
2782 using a large precision for intermediate results. This is used for all
2783 but large values of the argument. */
2785 fullprec_erfc_scaled (mpfr_t res
, mpfr_t arg
)
2790 prec
= mpfr_get_default_prec ();
2791 mpfr_set_default_prec (10 * prec
);
2796 mpfr_set (a
, arg
, GFC_RND_MODE
);
2797 mpfr_sqr (b
, a
, GFC_RND_MODE
);
2798 mpfr_exp (b
, b
, GFC_RND_MODE
);
2799 mpfr_erfc (a
, a
, GFC_RND_MODE
);
2800 mpfr_mul (a
, a
, b
, GFC_RND_MODE
);
2802 mpfr_set (res
, a
, GFC_RND_MODE
);
2803 mpfr_set_default_prec (prec
);
2809 /* Calculate ERFC_SCALED using a power series expansion in 1/arg:
2811 ERFC_SCALED(x) = 1 / (x * sqrt(pi))
2812 * (1 + Sum_n (-1)**n * (1 * 3 * 5 * ... * (2n-1))
2815 This is used for large values of the argument. Intermediate calculations
2816 are performed with twice the precision. We don't do a fixed number of
2817 iterations of the sum, but stop when it has converged to the required
2820 asympt_erfc_scaled (mpfr_t res
, mpfr_t arg
)
2822 mpfr_t sum
, x
, u
, v
, w
, oldsum
, sumtrunc
;
2827 prec
= mpfr_get_default_prec ();
2828 mpfr_set_default_prec (2 * prec
);
2838 mpfr_init (sumtrunc
);
2839 mpfr_set_prec (oldsum
, prec
);
2840 mpfr_set_prec (sumtrunc
, prec
);
2842 mpfr_set (x
, arg
, GFC_RND_MODE
);
2843 mpfr_set_ui (sum
, 1, GFC_RND_MODE
);
2844 mpz_set_ui (num
, 1);
2846 mpfr_set (u
, x
, GFC_RND_MODE
);
2847 mpfr_sqr (u
, u
, GFC_RND_MODE
);
2848 mpfr_mul_ui (u
, u
, 2, GFC_RND_MODE
);
2849 mpfr_pow_si (u
, u
, -1, GFC_RND_MODE
);
2851 for (i
= 1; i
< MAX_ITER
; i
++)
2853 mpfr_set (oldsum
, sum
, GFC_RND_MODE
);
2855 mpz_mul_ui (num
, num
, 2 * i
- 1);
2858 mpfr_set (w
, u
, GFC_RND_MODE
);
2859 mpfr_pow_ui (w
, w
, i
, GFC_RND_MODE
);
2861 mpfr_set_z (v
, num
, GFC_RND_MODE
);
2862 mpfr_mul (v
, v
, w
, GFC_RND_MODE
);
2864 mpfr_add (sum
, sum
, v
, GFC_RND_MODE
);
2866 mpfr_set (sumtrunc
, sum
, GFC_RND_MODE
);
2867 if (mpfr_cmp (sumtrunc
, oldsum
) == 0)
2871 /* We should have converged by now; otherwise, ARG_LIMIT is probably
2873 gcc_assert (i
< MAX_ITER
);
2875 /* Divide by x * sqrt(Pi). */
2876 mpfr_const_pi (u
, GFC_RND_MODE
);
2877 mpfr_sqrt (u
, u
, GFC_RND_MODE
);
2878 mpfr_mul (u
, u
, x
, GFC_RND_MODE
);
2879 mpfr_div (sum
, sum
, u
, GFC_RND_MODE
);
2881 mpfr_set (res
, sum
, GFC_RND_MODE
);
2882 mpfr_set_default_prec (prec
);
2884 mpfr_clears (sum
, x
, u
, v
, w
, oldsum
, sumtrunc
, NULL
);
2890 gfc_simplify_erfc_scaled (gfc_expr
*x
)
2894 if (x
->expr_type
!= EXPR_CONSTANT
)
2897 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2898 if (mpfr_cmp_d (x
->value
.real
, ARG_LIMIT
) >= 0)
2899 asympt_erfc_scaled (result
->value
.real
, x
->value
.real
);
2901 fullprec_erfc_scaled (result
->value
.real
, x
->value
.real
);
2903 return range_check (result
, "ERFC_SCALED");
2911 gfc_simplify_epsilon (gfc_expr
*e
)
2916 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
2918 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
2919 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].epsilon
, GFC_RND_MODE
);
2921 return range_check (result
, "EPSILON");
2926 gfc_simplify_exp (gfc_expr
*x
)
2930 if (x
->expr_type
!= EXPR_CONSTANT
)
2933 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2938 mpfr_exp (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
2942 gfc_set_model_kind (x
->ts
.kind
);
2943 mpc_exp (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
2947 gfc_internal_error ("in gfc_simplify_exp(): Bad type");
2950 return range_check (result
, "EXP");
2955 gfc_simplify_exponent (gfc_expr
*x
)
2960 if (x
->expr_type
!= EXPR_CONSTANT
)
2963 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
2966 /* EXPONENT(inf) = EXPONENT(nan) = HUGE(0) */
2967 if (mpfr_inf_p (x
->value
.real
) || mpfr_nan_p (x
->value
.real
))
2969 int i
= gfc_validate_kind (BT_INTEGER
, gfc_default_integer_kind
, false);
2970 mpz_set (result
->value
.integer
, gfc_integer_kinds
[i
].huge
);
2974 /* EXPONENT(+/- 0.0) = 0 */
2975 if (mpfr_zero_p (x
->value
.real
))
2977 mpz_set_ui (result
->value
.integer
, 0);
2981 gfc_set_model (x
->value
.real
);
2983 val
= (long int) mpfr_get_exp (x
->value
.real
);
2984 mpz_set_si (result
->value
.integer
, val
);
2986 return range_check (result
, "EXPONENT");
2991 gfc_simplify_failed_or_stopped_images (gfc_expr
*team ATTRIBUTE_UNUSED
,
2994 if (flag_coarray
== GFC_FCOARRAY_NONE
)
2996 gfc_current_locus
= *gfc_current_intrinsic_where
;
2997 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
2998 return &gfc_bad_expr
;
3001 if (flag_coarray
== GFC_FCOARRAY_SINGLE
)
3006 gfc_extract_int (kind
, &actual_kind
);
3008 actual_kind
= gfc_default_integer_kind
;
3010 result
= gfc_get_array_expr (BT_INTEGER
, actual_kind
, &gfc_current_locus
);
3015 /* For fcoarray = lib no simplification is possible, because it is not known
3016 what images failed or are stopped at compile time. */
3022 gfc_simplify_get_team (gfc_expr
*level ATTRIBUTE_UNUSED
)
3024 if (flag_coarray
== GFC_FCOARRAY_NONE
)
3026 gfc_current_locus
= *gfc_current_intrinsic_where
;
3027 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3028 return &gfc_bad_expr
;
3031 if (flag_coarray
== GFC_FCOARRAY_SINGLE
)
3034 result
= gfc_get_array_expr (BT_INTEGER
, gfc_default_integer_kind
, &gfc_current_locus
);
3039 /* For fcoarray = lib no simplification is possible, because it is not known
3040 what images failed or are stopped at compile time. */
3046 gfc_simplify_float (gfc_expr
*a
)
3050 if (a
->expr_type
!= EXPR_CONSTANT
)
3053 result
= gfc_int2real (a
, gfc_default_real_kind
);
3055 return range_check (result
, "FLOAT");
3060 is_last_ref_vtab (gfc_expr
*e
)
3063 gfc_component
*comp
= NULL
;
3065 if (e
->expr_type
!= EXPR_VARIABLE
)
3068 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
3069 if (ref
->type
== REF_COMPONENT
)
3070 comp
= ref
->u
.c
.component
;
3072 if (!e
->ref
|| !comp
)
3073 return e
->symtree
->n
.sym
->attr
.vtab
;
3075 if (comp
->name
[0] == '_' && strcmp (comp
->name
, "_vptr") == 0)
3083 gfc_simplify_extends_type_of (gfc_expr
*a
, gfc_expr
*mold
)
3085 /* Avoid simplification of resolved symbols. */
3086 if (is_last_ref_vtab (a
) || is_last_ref_vtab (mold
))
3089 if (a
->ts
.type
== BT_DERIVED
&& mold
->ts
.type
== BT_DERIVED
)
3090 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
,
3091 gfc_type_is_extension_of (mold
->ts
.u
.derived
,
3094 if (UNLIMITED_POLY (a
) || UNLIMITED_POLY (mold
))
3097 if ((a
->ts
.type
== BT_CLASS
&& !gfc_expr_attr (a
).class_ok
)
3098 || (mold
->ts
.type
== BT_CLASS
&& !gfc_expr_attr (mold
).class_ok
))
3101 /* Return .false. if the dynamic type can never be an extension. */
3102 if ((a
->ts
.type
== BT_CLASS
&& mold
->ts
.type
== BT_CLASS
3103 && !gfc_type_is_extension_of
3104 (mold
->ts
.u
.derived
->components
->ts
.u
.derived
,
3105 a
->ts
.u
.derived
->components
->ts
.u
.derived
)
3106 && !gfc_type_is_extension_of
3107 (a
->ts
.u
.derived
->components
->ts
.u
.derived
,
3108 mold
->ts
.u
.derived
->components
->ts
.u
.derived
))
3109 || (a
->ts
.type
== BT_DERIVED
&& mold
->ts
.type
== BT_CLASS
3110 && !gfc_type_is_extension_of
3111 (mold
->ts
.u
.derived
->components
->ts
.u
.derived
,
3113 || (a
->ts
.type
== BT_CLASS
&& mold
->ts
.type
== BT_DERIVED
3114 && !gfc_type_is_extension_of
3115 (mold
->ts
.u
.derived
,
3116 a
->ts
.u
.derived
->components
->ts
.u
.derived
)
3117 && !gfc_type_is_extension_of
3118 (a
->ts
.u
.derived
->components
->ts
.u
.derived
,
3119 mold
->ts
.u
.derived
)))
3120 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
, false);
3122 /* Return .true. if the dynamic type is guaranteed to be an extension. */
3123 if (a
->ts
.type
== BT_CLASS
&& mold
->ts
.type
== BT_DERIVED
3124 && gfc_type_is_extension_of (mold
->ts
.u
.derived
,
3125 a
->ts
.u
.derived
->components
->ts
.u
.derived
))
3126 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
, true);
3133 gfc_simplify_same_type_as (gfc_expr
*a
, gfc_expr
*b
)
3135 /* Avoid simplification of resolved symbols. */
3136 if (is_last_ref_vtab (a
) || is_last_ref_vtab (b
))
3139 /* Return .false. if the dynamic type can never be the
3141 if (((a
->ts
.type
== BT_CLASS
&& gfc_expr_attr (a
).class_ok
)
3142 || (b
->ts
.type
== BT_CLASS
&& gfc_expr_attr (b
).class_ok
))
3143 && !gfc_type_compatible (&a
->ts
, &b
->ts
)
3144 && !gfc_type_compatible (&b
->ts
, &a
->ts
))
3145 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
, false);
3147 if (a
->ts
.type
!= BT_DERIVED
|| b
->ts
.type
!= BT_DERIVED
)
3150 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
,
3151 gfc_compare_derived_types (a
->ts
.u
.derived
,
3157 gfc_simplify_floor (gfc_expr
*e
, gfc_expr
*k
)
3163 kind
= get_kind (BT_INTEGER
, k
, "FLOOR", gfc_default_integer_kind
);
3165 gfc_internal_error ("gfc_simplify_floor(): Bad kind");
3167 if (e
->expr_type
!= EXPR_CONSTANT
)
3170 mpfr_init2 (floor
, mpfr_get_prec (e
->value
.real
));
3171 mpfr_floor (floor
, e
->value
.real
);
3173 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &e
->where
);
3174 gfc_mpfr_to_mpz (result
->value
.integer
, floor
, &e
->where
);
3178 return range_check (result
, "FLOOR");
3183 gfc_simplify_fraction (gfc_expr
*x
)
3188 if (x
->expr_type
!= EXPR_CONSTANT
)
3191 result
= gfc_get_constant_expr (BT_REAL
, x
->ts
.kind
, &x
->where
);
3193 /* FRACTION(inf) = NaN. */
3194 if (mpfr_inf_p (x
->value
.real
))
3196 mpfr_set_nan (result
->value
.real
);
3200 /* mpfr_frexp() correctly handles zeros and NaNs. */
3201 mpfr_frexp (&e
, result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
3203 return range_check (result
, "FRACTION");
3208 gfc_simplify_gamma (gfc_expr
*x
)
3212 if (x
->expr_type
!= EXPR_CONSTANT
)
3215 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3216 mpfr_gamma (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
3218 return range_check (result
, "GAMMA");
3223 gfc_simplify_huge (gfc_expr
*e
)
3228 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
3229 result
= gfc_get_constant_expr (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
3234 mpz_set (result
->value
.integer
, gfc_integer_kinds
[i
].huge
);
3238 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].huge
, GFC_RND_MODE
);
3250 gfc_simplify_hypot (gfc_expr
*x
, gfc_expr
*y
)
3254 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
3257 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3258 mpfr_hypot (result
->value
.real
, x
->value
.real
, y
->value
.real
, GFC_RND_MODE
);
3259 return range_check (result
, "HYPOT");
3263 /* We use the processor's collating sequence, because all
3264 systems that gfortran currently works on are ASCII. */
3267 gfc_simplify_iachar (gfc_expr
*e
, gfc_expr
*kind
)
3273 if (e
->expr_type
!= EXPR_CONSTANT
)
3276 if (e
->value
.character
.length
!= 1)
3278 gfc_error ("Argument of IACHAR at %L must be of length one", &e
->where
);
3279 return &gfc_bad_expr
;
3282 index
= e
->value
.character
.string
[0];
3284 if (warn_surprising
&& index
> 127)
3285 gfc_warning (OPT_Wsurprising
,
3286 "Argument of IACHAR function at %L outside of range 0..127",
3289 k
= get_kind (BT_INTEGER
, kind
, "IACHAR", gfc_default_integer_kind
);
3291 return &gfc_bad_expr
;
3293 result
= gfc_get_int_expr (k
, &e
->where
, index
);
3295 return range_check (result
, "IACHAR");
3300 do_bit_and (gfc_expr
*result
, gfc_expr
*e
)
3302 gcc_assert (e
->ts
.type
== BT_INTEGER
&& e
->expr_type
== EXPR_CONSTANT
);
3303 gcc_assert (result
->ts
.type
== BT_INTEGER
3304 && result
->expr_type
== EXPR_CONSTANT
);
3306 mpz_and (result
->value
.integer
, result
->value
.integer
, e
->value
.integer
);
3312 gfc_simplify_iall (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
)
3314 return simplify_transformation (array
, dim
, mask
, -1, do_bit_and
);
3319 do_bit_ior (gfc_expr
*result
, gfc_expr
*e
)
3321 gcc_assert (e
->ts
.type
== BT_INTEGER
&& e
->expr_type
== EXPR_CONSTANT
);
3322 gcc_assert (result
->ts
.type
== BT_INTEGER
3323 && result
->expr_type
== EXPR_CONSTANT
);
3325 mpz_ior (result
->value
.integer
, result
->value
.integer
, e
->value
.integer
);
3331 gfc_simplify_iany (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
)
3333 return simplify_transformation (array
, dim
, mask
, 0, do_bit_ior
);
3338 gfc_simplify_iand (gfc_expr
*x
, gfc_expr
*y
)
3342 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
3345 result
= gfc_get_constant_expr (BT_INTEGER
, x
->ts
.kind
, &x
->where
);
3346 mpz_and (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
3348 return range_check (result
, "IAND");
3353 gfc_simplify_ibclr (gfc_expr
*x
, gfc_expr
*y
)
3358 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
3361 gfc_extract_int (y
, &pos
);
3363 k
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
3365 result
= gfc_copy_expr (x
);
3367 convert_mpz_to_unsigned (result
->value
.integer
,
3368 gfc_integer_kinds
[k
].bit_size
);
3370 mpz_clrbit (result
->value
.integer
, pos
);
3372 gfc_convert_mpz_to_signed (result
->value
.integer
,
3373 gfc_integer_kinds
[k
].bit_size
);
3380 gfc_simplify_ibits (gfc_expr
*x
, gfc_expr
*y
, gfc_expr
*z
)
3387 if (x
->expr_type
!= EXPR_CONSTANT
3388 || y
->expr_type
!= EXPR_CONSTANT
3389 || z
->expr_type
!= EXPR_CONSTANT
)
3392 gfc_extract_int (y
, &pos
);
3393 gfc_extract_int (z
, &len
);
3395 k
= gfc_validate_kind (BT_INTEGER
, x
->ts
.kind
, false);
3397 bitsize
= gfc_integer_kinds
[k
].bit_size
;
3399 if (pos
+ len
> bitsize
)
3401 gfc_error ("Sum of second and third arguments of IBITS exceeds "
3402 "bit size at %L", &y
->where
);
3403 return &gfc_bad_expr
;
3406 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3407 convert_mpz_to_unsigned (result
->value
.integer
,
3408 gfc_integer_kinds
[k
].bit_size
);
3410 bits
= XCNEWVEC (int, bitsize
);
3412 for (i
= 0; i
< bitsize
; i
++)
3415 for (i
= 0; i
< len
; i
++)
3416 bits
[i
] = mpz_tstbit (x
->value
.integer
, i
+ pos
);
3418 for (i
= 0; i
< bitsize
; i
++)
3421 mpz_clrbit (result
->value
.integer
, i
);
3422 else if (bits
[i
] == 1)
3423 mpz_setbit (result
->value
.integer
, i
);
3425 gfc_internal_error ("IBITS: Bad bit");
3430 gfc_convert_mpz_to_signed (result
->value
.integer
,
3431 gfc_integer_kinds
[k
].bit_size
);
3438 gfc_simplify_ibset (gfc_expr
*x
, gfc_expr
*y
)
3443 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
3446 gfc_extract_int (y
, &pos
);
3448 k
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
3450 result
= gfc_copy_expr (x
);
3452 convert_mpz_to_unsigned (result
->value
.integer
,
3453 gfc_integer_kinds
[k
].bit_size
);
3455 mpz_setbit (result
->value
.integer
, pos
);
3457 gfc_convert_mpz_to_signed (result
->value
.integer
,
3458 gfc_integer_kinds
[k
].bit_size
);
3465 gfc_simplify_ichar (gfc_expr
*e
, gfc_expr
*kind
)
3471 if (e
->expr_type
!= EXPR_CONSTANT
)
3474 if (e
->value
.character
.length
!= 1)
3476 gfc_error ("Argument of ICHAR at %L must be of length one", &e
->where
);
3477 return &gfc_bad_expr
;
3480 index
= e
->value
.character
.string
[0];
3482 k
= get_kind (BT_INTEGER
, kind
, "ICHAR", gfc_default_integer_kind
);
3484 return &gfc_bad_expr
;
3486 result
= gfc_get_int_expr (k
, &e
->where
, index
);
3488 return range_check (result
, "ICHAR");
3493 gfc_simplify_ieor (gfc_expr
*x
, gfc_expr
*y
)
3497 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
3500 result
= gfc_get_constant_expr (BT_INTEGER
, x
->ts
.kind
, &x
->where
);
3501 mpz_xor (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
3503 return range_check (result
, "IEOR");
3508 gfc_simplify_index (gfc_expr
*x
, gfc_expr
*y
, gfc_expr
*b
, gfc_expr
*kind
)
3512 HOST_WIDE_INT len
, lensub
, start
, last
, i
, index
= 0;
3515 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
3516 || ( b
!= NULL
&& b
->expr_type
!= EXPR_CONSTANT
))
3519 back
= (b
!= NULL
&& b
->value
.logical
!= 0);
3521 k
= get_kind (BT_INTEGER
, kind
, "INDEX", gfc_default_integer_kind
);
3523 return &gfc_bad_expr
;
3525 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &x
->where
);
3527 len
= x
->value
.character
.length
;
3528 lensub
= y
->value
.character
.length
;
3532 mpz_set_si (result
->value
.integer
, 0);
3547 last
= len
+ 1 - lensub
;
3554 start
= len
- lensub
;
3558 for (; start
!= last
; start
+= delta
)
3560 for (i
= 0; i
< lensub
; i
++)
3562 if (x
->value
.character
.string
[start
+ i
]
3563 != y
->value
.character
.string
[i
])
3574 mpz_set_si (result
->value
.integer
, index
);
3575 return range_check (result
, "INDEX");
3580 simplify_intconv (gfc_expr
*e
, int kind
, const char *name
)
3582 gfc_expr
*result
= NULL
;
3585 /* Convert BOZ to integer, and return without range checking. */
3586 if (e
->ts
.type
== BT_BOZ
)
3588 if (!gfc_boz2int (e
, kind
))
3590 result
= gfc_copy_expr (e
);
3594 if (e
->expr_type
!= EXPR_CONSTANT
)
3597 /* For explicit conversion, turn off -Wconversion and -Wconversion-extra
3599 tmp1
= warn_conversion
;
3600 tmp2
= warn_conversion_extra
;
3601 warn_conversion
= warn_conversion_extra
= 0;
3603 result
= gfc_convert_constant (e
, BT_INTEGER
, kind
);
3605 warn_conversion
= tmp1
;
3606 warn_conversion_extra
= tmp2
;
3608 if (result
== &gfc_bad_expr
)
3609 return &gfc_bad_expr
;
3611 return range_check (result
, name
);
3616 gfc_simplify_int (gfc_expr
*e
, gfc_expr
*k
)
3620 kind
= get_kind (BT_INTEGER
, k
, "INT", gfc_default_integer_kind
);
3622 return &gfc_bad_expr
;
3624 return simplify_intconv (e
, kind
, "INT");
3628 gfc_simplify_int2 (gfc_expr
*e
)
3630 return simplify_intconv (e
, 2, "INT2");
3635 gfc_simplify_int8 (gfc_expr
*e
)
3637 return simplify_intconv (e
, 8, "INT8");
3642 gfc_simplify_long (gfc_expr
*e
)
3644 return simplify_intconv (e
, 4, "LONG");
3649 gfc_simplify_ifix (gfc_expr
*e
)
3651 gfc_expr
*rtrunc
, *result
;
3653 if (e
->expr_type
!= EXPR_CONSTANT
)
3656 rtrunc
= gfc_copy_expr (e
);
3657 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
3659 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
3661 gfc_mpfr_to_mpz (result
->value
.integer
, rtrunc
->value
.real
, &e
->where
);
3663 gfc_free_expr (rtrunc
);
3665 return range_check (result
, "IFIX");
3670 gfc_simplify_idint (gfc_expr
*e
)
3672 gfc_expr
*rtrunc
, *result
;
3674 if (e
->expr_type
!= EXPR_CONSTANT
)
3677 rtrunc
= gfc_copy_expr (e
);
3678 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
3680 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
3682 gfc_mpfr_to_mpz (result
->value
.integer
, rtrunc
->value
.real
, &e
->where
);
3684 gfc_free_expr (rtrunc
);
3686 return range_check (result
, "IDINT");
3691 gfc_simplify_ior (gfc_expr
*x
, gfc_expr
*y
)
3695 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
3698 result
= gfc_get_constant_expr (BT_INTEGER
, x
->ts
.kind
, &x
->where
);
3699 mpz_ior (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
3701 return range_check (result
, "IOR");
3706 do_bit_xor (gfc_expr
*result
, gfc_expr
*e
)
3708 gcc_assert (e
->ts
.type
== BT_INTEGER
&& e
->expr_type
== EXPR_CONSTANT
);
3709 gcc_assert (result
->ts
.type
== BT_INTEGER
3710 && result
->expr_type
== EXPR_CONSTANT
);
3712 mpz_xor (result
->value
.integer
, result
->value
.integer
, e
->value
.integer
);
3718 gfc_simplify_iparity (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
)
3720 return simplify_transformation (array
, dim
, mask
, 0, do_bit_xor
);
3725 gfc_simplify_is_iostat_end (gfc_expr
*x
)
3727 if (x
->expr_type
!= EXPR_CONSTANT
)
3730 return gfc_get_logical_expr (gfc_default_logical_kind
, &x
->where
,
3731 mpz_cmp_si (x
->value
.integer
,
3732 LIBERROR_END
) == 0);
3737 gfc_simplify_is_iostat_eor (gfc_expr
*x
)
3739 if (x
->expr_type
!= EXPR_CONSTANT
)
3742 return gfc_get_logical_expr (gfc_default_logical_kind
, &x
->where
,
3743 mpz_cmp_si (x
->value
.integer
,
3744 LIBERROR_EOR
) == 0);
3749 gfc_simplify_isnan (gfc_expr
*x
)
3751 if (x
->expr_type
!= EXPR_CONSTANT
)
3754 return gfc_get_logical_expr (gfc_default_logical_kind
, &x
->where
,
3755 mpfr_nan_p (x
->value
.real
));
3759 /* Performs a shift on its first argument. Depending on the last
3760 argument, the shift can be arithmetic, i.e. with filling from the
3761 left like in the SHIFTA intrinsic. */
3763 simplify_shift (gfc_expr
*e
, gfc_expr
*s
, const char *name
,
3764 bool arithmetic
, int direction
)
3767 int ashift
, *bits
, i
, k
, bitsize
, shift
;
3769 if (e
->expr_type
!= EXPR_CONSTANT
|| s
->expr_type
!= EXPR_CONSTANT
)
3772 gfc_extract_int (s
, &shift
);
3774 k
= gfc_validate_kind (BT_INTEGER
, e
->ts
.kind
, false);
3775 bitsize
= gfc_integer_kinds
[k
].bit_size
;
3777 result
= gfc_get_constant_expr (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
3781 mpz_set (result
->value
.integer
, e
->value
.integer
);
3785 if (direction
> 0 && shift
< 0)
3787 /* Left shift, as in SHIFTL. */
3788 gfc_error ("Second argument of %s is negative at %L", name
, &e
->where
);
3789 return &gfc_bad_expr
;
3791 else if (direction
< 0)
3793 /* Right shift, as in SHIFTR or SHIFTA. */
3796 gfc_error ("Second argument of %s is negative at %L",
3798 return &gfc_bad_expr
;
3804 ashift
= (shift
>= 0 ? shift
: -shift
);
3806 if (ashift
> bitsize
)
3808 gfc_error ("Magnitude of second argument of %s exceeds bit size "
3809 "at %L", name
, &e
->where
);
3810 return &gfc_bad_expr
;
3813 bits
= XCNEWVEC (int, bitsize
);
3815 for (i
= 0; i
< bitsize
; i
++)
3816 bits
[i
] = mpz_tstbit (e
->value
.integer
, i
);
3821 for (i
= 0; i
< shift
; i
++)
3822 mpz_clrbit (result
->value
.integer
, i
);
3824 for (i
= 0; i
< bitsize
- shift
; i
++)
3827 mpz_clrbit (result
->value
.integer
, i
+ shift
);
3829 mpz_setbit (result
->value
.integer
, i
+ shift
);
3835 if (arithmetic
&& bits
[bitsize
- 1])
3836 for (i
= bitsize
- 1; i
>= bitsize
- ashift
; i
--)
3837 mpz_setbit (result
->value
.integer
, i
);
3839 for (i
= bitsize
- 1; i
>= bitsize
- ashift
; i
--)
3840 mpz_clrbit (result
->value
.integer
, i
);
3842 for (i
= bitsize
- 1; i
>= ashift
; i
--)
3845 mpz_clrbit (result
->value
.integer
, i
- ashift
);
3847 mpz_setbit (result
->value
.integer
, i
- ashift
);
3851 gfc_convert_mpz_to_signed (result
->value
.integer
, bitsize
);
3859 gfc_simplify_ishft (gfc_expr
*e
, gfc_expr
*s
)
3861 return simplify_shift (e
, s
, "ISHFT", false, 0);
3866 gfc_simplify_lshift (gfc_expr
*e
, gfc_expr
*s
)
3868 return simplify_shift (e
, s
, "LSHIFT", false, 1);
3873 gfc_simplify_rshift (gfc_expr
*e
, gfc_expr
*s
)
3875 return simplify_shift (e
, s
, "RSHIFT", true, -1);
3880 gfc_simplify_shifta (gfc_expr
*e
, gfc_expr
*s
)
3882 return simplify_shift (e
, s
, "SHIFTA", true, -1);
3887 gfc_simplify_shiftl (gfc_expr
*e
, gfc_expr
*s
)
3889 return simplify_shift (e
, s
, "SHIFTL", false, 1);
3894 gfc_simplify_shiftr (gfc_expr
*e
, gfc_expr
*s
)
3896 return simplify_shift (e
, s
, "SHIFTR", false, -1);
3901 gfc_simplify_ishftc (gfc_expr
*e
, gfc_expr
*s
, gfc_expr
*sz
)
3904 int shift
, ashift
, isize
, ssize
, delta
, k
;
3907 if (e
->expr_type
!= EXPR_CONSTANT
|| s
->expr_type
!= EXPR_CONSTANT
)
3910 gfc_extract_int (s
, &shift
);
3912 k
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
3913 isize
= gfc_integer_kinds
[k
].bit_size
;
3917 if (sz
->expr_type
!= EXPR_CONSTANT
)
3920 gfc_extract_int (sz
, &ssize
);
3933 gfc_error ("Magnitude of second argument of ISHFTC exceeds "
3934 "BIT_SIZE of first argument at %C");
3936 gfc_error ("Absolute value of SHIFT shall be less than or equal "
3938 return &gfc_bad_expr
;
3941 result
= gfc_get_constant_expr (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
3943 mpz_set (result
->value
.integer
, e
->value
.integer
);
3948 convert_mpz_to_unsigned (result
->value
.integer
, isize
);
3950 bits
= XCNEWVEC (int, ssize
);
3952 for (i
= 0; i
< ssize
; i
++)
3953 bits
[i
] = mpz_tstbit (e
->value
.integer
, i
);
3955 delta
= ssize
- ashift
;
3959 for (i
= 0; i
< delta
; i
++)
3962 mpz_clrbit (result
->value
.integer
, i
+ shift
);
3964 mpz_setbit (result
->value
.integer
, i
+ shift
);
3967 for (i
= delta
; i
< ssize
; i
++)
3970 mpz_clrbit (result
->value
.integer
, i
- delta
);
3972 mpz_setbit (result
->value
.integer
, i
- delta
);
3977 for (i
= 0; i
< ashift
; i
++)
3980 mpz_clrbit (result
->value
.integer
, i
+ delta
);
3982 mpz_setbit (result
->value
.integer
, i
+ delta
);
3985 for (i
= ashift
; i
< ssize
; i
++)
3988 mpz_clrbit (result
->value
.integer
, i
+ shift
);
3990 mpz_setbit (result
->value
.integer
, i
+ shift
);
3994 gfc_convert_mpz_to_signed (result
->value
.integer
, isize
);
4002 gfc_simplify_kind (gfc_expr
*e
)
4004 return gfc_get_int_expr (gfc_default_integer_kind
, NULL
, e
->ts
.kind
);
4009 simplify_bound_dim (gfc_expr
*array
, gfc_expr
*kind
, int d
, int upper
,
4010 gfc_array_spec
*as
, gfc_ref
*ref
, bool coarray
)
4012 gfc_expr
*l
, *u
, *result
;
4015 k
= get_kind (BT_INTEGER
, kind
, upper
? "UBOUND" : "LBOUND",
4016 gfc_default_integer_kind
);
4018 return &gfc_bad_expr
;
4020 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &array
->where
);
4022 /* For non-variables, LBOUND(expr, DIM=n) = 1 and
4023 UBOUND(expr, DIM=n) = SIZE(expr, DIM=n). */
4024 if (!coarray
&& array
->expr_type
!= EXPR_VARIABLE
)
4028 gfc_expr
* dim
= result
;
4029 mpz_set_si (dim
->value
.integer
, d
);
4031 result
= simplify_size (array
, dim
, k
);
4032 gfc_free_expr (dim
);
4037 mpz_set_si (result
->value
.integer
, 1);
4042 /* Otherwise, we have a variable expression. */
4043 gcc_assert (array
->expr_type
== EXPR_VARIABLE
);
4046 if (!gfc_resolve_array_spec (as
, 0))
4049 /* The last dimension of an assumed-size array is special. */
4050 if ((!coarray
&& d
== as
->rank
&& as
->type
== AS_ASSUMED_SIZE
&& !upper
)
4051 || (coarray
&& d
== as
->rank
+ as
->corank
4052 && (!upper
|| flag_coarray
== GFC_FCOARRAY_SINGLE
)))
4054 if (as
->lower
[d
-1] && as
->lower
[d
-1]->expr_type
== EXPR_CONSTANT
)
4056 gfc_free_expr (result
);
4057 return gfc_copy_expr (as
->lower
[d
-1]);
4063 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &array
->where
);
4065 /* Then, we need to know the extent of the given dimension. */
4066 if (coarray
|| (ref
->u
.ar
.type
== AR_FULL
&& !ref
->next
))
4068 gfc_expr
*declared_bound
;
4070 bool constant_lbound
, constant_ubound
;
4075 gcc_assert (l
!= NULL
);
4077 constant_lbound
= l
->expr_type
== EXPR_CONSTANT
;
4078 constant_ubound
= u
&& u
->expr_type
== EXPR_CONSTANT
;
4080 empty_bound
= upper
? 0 : 1;
4081 declared_bound
= upper
? u
: l
;
4083 if ((!upper
&& !constant_lbound
)
4084 || (upper
&& !constant_ubound
))
4089 /* For {L,U}BOUND, the value depends on whether the array
4090 is empty. We can nevertheless simplify if the declared bound
4091 has the same value as that of an empty array, in which case
4092 the result isn't dependent on the array emptyness. */
4093 if (mpz_cmp_si (declared_bound
->value
.integer
, empty_bound
) == 0)
4094 mpz_set_si (result
->value
.integer
, empty_bound
);
4095 else if (!constant_lbound
|| !constant_ubound
)
4096 /* Array emptyness can't be determined, we can't simplify. */
4098 else if (mpz_cmp (l
->value
.integer
, u
->value
.integer
) > 0)
4099 mpz_set_si (result
->value
.integer
, empty_bound
);
4101 mpz_set (result
->value
.integer
, declared_bound
->value
.integer
);
4104 mpz_set (result
->value
.integer
, declared_bound
->value
.integer
);
4110 int d2
= 0, cnt
= 0;
4111 for (int idx
= 0; idx
< ref
->u
.ar
.dimen
; ++idx
)
4113 if (ref
->u
.ar
.dimen_type
[idx
] == DIMEN_ELEMENT
)
4115 else if (cnt
< d
- 1)
4120 if (!gfc_ref_dimen_size (&ref
->u
.ar
, d2
+ d
- 1, &result
->value
.integer
, NULL
))
4124 mpz_set_si (result
->value
.integer
, (long int) 1);
4128 return range_check (result
, upper
? "UBOUND" : "LBOUND");
4131 gfc_free_expr (result
);
4137 simplify_bound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
, int upper
)
4141 ar_type type
= AR_UNKNOWN
;
4144 if (array
->ts
.type
== BT_CLASS
)
4147 if (array
->expr_type
!= EXPR_VARIABLE
)
4154 /* Do not attempt to resolve if error has already been issued. */
4155 if (array
->symtree
->n
.sym
->error
)
4158 /* Follow any component references. */
4159 as
= array
->symtree
->n
.sym
->as
;
4160 for (ref
= array
->ref
; ref
; ref
= ref
->next
)
4165 type
= ref
->u
.ar
.type
;
4166 switch (ref
->u
.ar
.type
)
4173 /* We're done because 'as' has already been set in the
4174 previous iteration. */
4188 as
= ref
->u
.c
.component
->as
;
4201 if (as
&& (as
->type
== AS_DEFERRED
|| as
->type
== AS_ASSUMED_RANK
4202 || (as
->type
== AS_ASSUMED_SHAPE
&& upper
)))
4205 /* 'array' shall not be an unallocated allocatable variable or a pointer that
4206 is not associated. */
4207 if (array
->expr_type
== EXPR_VARIABLE
4208 && (gfc_expr_attr (array
).allocatable
|| gfc_expr_attr (array
).pointer
))
4212 || (as
->type
!= AS_DEFERRED
4213 && array
->expr_type
== EXPR_VARIABLE
4214 && !gfc_expr_attr (array
).allocatable
4215 && !gfc_expr_attr (array
).pointer
));
4219 /* Multi-dimensional bounds. */
4220 gfc_expr
*bounds
[GFC_MAX_DIMENSIONS
];
4224 /* UBOUND(ARRAY) is not valid for an assumed-size array. */
4225 if (upper
&& type
== AR_FULL
&& as
&& as
->type
== AS_ASSUMED_SIZE
)
4227 /* An error message will be emitted in
4228 check_assumed_size_reference (resolve.cc). */
4229 return &gfc_bad_expr
;
4232 /* Simplify the bounds for each dimension. */
4233 for (d
= 0; d
< array
->rank
; d
++)
4235 bounds
[d
] = simplify_bound_dim (array
, kind
, d
+ 1, upper
, as
, ref
,
4237 if (bounds
[d
] == NULL
|| bounds
[d
] == &gfc_bad_expr
)
4241 for (j
= 0; j
< d
; j
++)
4242 gfc_free_expr (bounds
[j
]);
4245 return &gfc_bad_expr
;
4251 /* Allocate the result expression. */
4252 k
= get_kind (BT_INTEGER
, kind
, upper
? "UBOUND" : "LBOUND",
4253 gfc_default_integer_kind
);
4255 return &gfc_bad_expr
;
4257 e
= gfc_get_array_expr (BT_INTEGER
, k
, &array
->where
);
4259 /* The result is a rank 1 array; its size is the rank of the first
4260 argument to {L,U}BOUND. */
4262 e
->shape
= gfc_get_shape (1);
4263 mpz_init_set_ui (e
->shape
[0], array
->rank
);
4265 /* Create the constructor for this array. */
4266 for (d
= 0; d
< array
->rank
; d
++)
4267 gfc_constructor_append_expr (&e
->value
.constructor
,
4268 bounds
[d
], &e
->where
);
4274 /* A DIM argument is specified. */
4275 if (dim
->expr_type
!= EXPR_CONSTANT
)
4278 d
= mpz_get_si (dim
->value
.integer
);
4280 if ((d
< 1 || d
> array
->rank
)
4281 || (d
== array
->rank
&& as
&& as
->type
== AS_ASSUMED_SIZE
&& upper
))
4283 gfc_error ("DIM argument at %L is out of bounds", &dim
->where
);
4284 return &gfc_bad_expr
;
4287 if (as
&& as
->type
== AS_ASSUMED_RANK
)
4290 return simplify_bound_dim (array
, kind
, d
, upper
, as
, ref
, false);
4296 simplify_cobound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
, int upper
)
4302 if (array
->expr_type
!= EXPR_VARIABLE
)
4305 /* Follow any component references. */
4306 as
= (array
->ts
.type
== BT_CLASS
&& array
->ts
.u
.derived
->components
)
4307 ? array
->ts
.u
.derived
->components
->as
4308 : array
->symtree
->n
.sym
->as
;
4309 for (ref
= array
->ref
; ref
; ref
= ref
->next
)
4314 switch (ref
->u
.ar
.type
)
4317 if (ref
->u
.ar
.as
->corank
> 0)
4319 gcc_assert (as
== ref
->u
.ar
.as
);
4326 /* We're done because 'as' has already been set in the
4327 previous iteration. */
4341 as
= ref
->u
.c
.component
->as
;
4355 if (as
->cotype
== AS_DEFERRED
|| as
->cotype
== AS_ASSUMED_SHAPE
)
4360 /* Multi-dimensional cobounds. */
4361 gfc_expr
*bounds
[GFC_MAX_DIMENSIONS
];
4365 /* Simplify the cobounds for each dimension. */
4366 for (d
= 0; d
< as
->corank
; d
++)
4368 bounds
[d
] = simplify_bound_dim (array
, kind
, d
+ 1 + as
->rank
,
4369 upper
, as
, ref
, true);
4370 if (bounds
[d
] == NULL
|| bounds
[d
] == &gfc_bad_expr
)
4374 for (j
= 0; j
< d
; j
++)
4375 gfc_free_expr (bounds
[j
]);
4380 /* Allocate the result expression. */
4381 e
= gfc_get_expr ();
4382 e
->where
= array
->where
;
4383 e
->expr_type
= EXPR_ARRAY
;
4384 e
->ts
.type
= BT_INTEGER
;
4385 k
= get_kind (BT_INTEGER
, kind
, upper
? "UCOBOUND" : "LCOBOUND",
4386 gfc_default_integer_kind
);
4390 return &gfc_bad_expr
;
4394 /* The result is a rank 1 array; its size is the rank of the first
4395 argument to {L,U}COBOUND. */
4397 e
->shape
= gfc_get_shape (1);
4398 mpz_init_set_ui (e
->shape
[0], as
->corank
);
4400 /* Create the constructor for this array. */
4401 for (d
= 0; d
< as
->corank
; d
++)
4402 gfc_constructor_append_expr (&e
->value
.constructor
,
4403 bounds
[d
], &e
->where
);
4408 /* A DIM argument is specified. */
4409 if (dim
->expr_type
!= EXPR_CONSTANT
)
4412 d
= mpz_get_si (dim
->value
.integer
);
4414 if (d
< 1 || d
> as
->corank
)
4416 gfc_error ("DIM argument at %L is out of bounds", &dim
->where
);
4417 return &gfc_bad_expr
;
4420 return simplify_bound_dim (array
, kind
, d
+as
->rank
, upper
, as
, ref
, true);
4426 gfc_simplify_lbound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
)
4428 return simplify_bound (array
, dim
, kind
, 0);
4433 gfc_simplify_lcobound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
)
4435 return simplify_cobound (array
, dim
, kind
, 0);
4439 gfc_simplify_leadz (gfc_expr
*e
)
4441 unsigned long lz
, bs
;
4444 if (e
->expr_type
!= EXPR_CONSTANT
)
4447 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
4448 bs
= gfc_integer_kinds
[i
].bit_size
;
4449 if (mpz_cmp_si (e
->value
.integer
, 0) == 0)
4451 else if (mpz_cmp_si (e
->value
.integer
, 0) < 0)
4454 lz
= bs
- mpz_sizeinbase (e
->value
.integer
, 2);
4456 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, lz
);
4460 /* Check for constant length of a substring. */
4463 substring_has_constant_len (gfc_expr
*e
)
4466 HOST_WIDE_INT istart
, iend
, length
;
4467 bool equal_length
= false;
4469 if (e
->ts
.type
!= BT_CHARACTER
)
4472 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
4473 if (ref
->type
!= REF_COMPONENT
&& ref
->type
!= REF_ARRAY
)
4477 || ref
->type
!= REF_SUBSTRING
4479 || ref
->u
.ss
.start
->expr_type
!= EXPR_CONSTANT
4481 || ref
->u
.ss
.end
->expr_type
!= EXPR_CONSTANT
)
4484 /* Basic checks on substring starting and ending indices. */
4485 if (!gfc_resolve_substring (ref
, &equal_length
))
4488 istart
= gfc_mpz_get_hwi (ref
->u
.ss
.start
->value
.integer
);
4489 iend
= gfc_mpz_get_hwi (ref
->u
.ss
.end
->value
.integer
);
4492 length
= iend
- istart
+ 1;
4496 /* Fix substring length. */
4497 e
->value
.character
.length
= length
;
4504 gfc_simplify_len (gfc_expr
*e
, gfc_expr
*kind
)
4507 int k
= get_kind (BT_INTEGER
, kind
, "LEN", gfc_default_integer_kind
);
4510 return &gfc_bad_expr
;
4512 if (e
->expr_type
== EXPR_CONSTANT
4513 || substring_has_constant_len (e
))
4515 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &e
->where
);
4516 mpz_set_si (result
->value
.integer
, e
->value
.character
.length
);
4517 return range_check (result
, "LEN");
4519 else if (e
->ts
.u
.cl
!= NULL
&& e
->ts
.u
.cl
->length
!= NULL
4520 && e
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
4521 && e
->ts
.u
.cl
->length
->ts
.type
== BT_INTEGER
)
4523 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &e
->where
);
4524 mpz_set (result
->value
.integer
, e
->ts
.u
.cl
->length
->value
.integer
);
4525 return range_check (result
, "LEN");
4527 else if (e
->expr_type
== EXPR_VARIABLE
&& e
->ts
.type
== BT_CHARACTER
4528 && e
->symtree
->n
.sym
4529 && e
->symtree
->n
.sym
->ts
.type
!= BT_DERIVED
4530 && e
->symtree
->n
.sym
->assoc
&& e
->symtree
->n
.sym
->assoc
->target
4531 && e
->symtree
->n
.sym
->assoc
->target
->ts
.type
== BT_DERIVED
4532 && e
->symtree
->n
.sym
->assoc
->target
->symtree
->n
.sym
4533 && UNLIMITED_POLY (e
->symtree
->n
.sym
->assoc
->target
->symtree
->n
.sym
))
4535 /* The expression in assoc->target points to a ref to the _data component
4536 of the unlimited polymorphic entity. To get the _len component the last
4537 _data ref needs to be stripped and a ref to the _len component added. */
4538 return gfc_get_len_component (e
->symtree
->n
.sym
->assoc
->target
, k
);
4545 gfc_simplify_len_trim (gfc_expr
*e
, gfc_expr
*kind
)
4548 size_t count
, len
, i
;
4549 int k
= get_kind (BT_INTEGER
, kind
, "LEN_TRIM", gfc_default_integer_kind
);
4552 return &gfc_bad_expr
;
4554 if (e
->expr_type
!= EXPR_CONSTANT
)
4557 len
= e
->value
.character
.length
;
4558 for (count
= 0, i
= 1; i
<= len
; i
++)
4559 if (e
->value
.character
.string
[len
- i
] == ' ')
4564 result
= gfc_get_int_expr (k
, &e
->where
, len
- count
);
4565 return range_check (result
, "LEN_TRIM");
4569 gfc_simplify_lgamma (gfc_expr
*x
)
4574 if (x
->expr_type
!= EXPR_CONSTANT
)
4577 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
4578 mpfr_lgamma (result
->value
.real
, &sg
, x
->value
.real
, GFC_RND_MODE
);
4580 return range_check (result
, "LGAMMA");
4585 gfc_simplify_lge (gfc_expr
*a
, gfc_expr
*b
)
4587 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
4590 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
,
4591 gfc_compare_string (a
, b
) >= 0);
4596 gfc_simplify_lgt (gfc_expr
*a
, gfc_expr
*b
)
4598 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
4601 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
,
4602 gfc_compare_string (a
, b
) > 0);
4607 gfc_simplify_lle (gfc_expr
*a
, gfc_expr
*b
)
4609 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
4612 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
,
4613 gfc_compare_string (a
, b
) <= 0);
4618 gfc_simplify_llt (gfc_expr
*a
, gfc_expr
*b
)
4620 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
4623 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
,
4624 gfc_compare_string (a
, b
) < 0);
4629 gfc_simplify_log (gfc_expr
*x
)
4633 if (x
->expr_type
!= EXPR_CONSTANT
)
4636 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
4641 if (mpfr_sgn (x
->value
.real
) <= 0)
4643 gfc_error ("Argument of LOG at %L cannot be less than or equal "
4644 "to zero", &x
->where
);
4645 gfc_free_expr (result
);
4646 return &gfc_bad_expr
;
4649 mpfr_log (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
4653 if (mpfr_zero_p (mpc_realref (x
->value
.complex))
4654 && mpfr_zero_p (mpc_imagref (x
->value
.complex)))
4656 gfc_error ("Complex argument of LOG at %L cannot be zero",
4658 gfc_free_expr (result
);
4659 return &gfc_bad_expr
;
4662 gfc_set_model_kind (x
->ts
.kind
);
4663 mpc_log (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
4667 gfc_internal_error ("gfc_simplify_log: bad type");
4670 return range_check (result
, "LOG");
4675 gfc_simplify_log10 (gfc_expr
*x
)
4679 if (x
->expr_type
!= EXPR_CONSTANT
)
4682 if (mpfr_sgn (x
->value
.real
) <= 0)
4684 gfc_error ("Argument of LOG10 at %L cannot be less than or equal "
4685 "to zero", &x
->where
);
4686 return &gfc_bad_expr
;
4689 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
4690 mpfr_log10 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
4692 return range_check (result
, "LOG10");
4697 gfc_simplify_logical (gfc_expr
*e
, gfc_expr
*k
)
4701 kind
= get_kind (BT_LOGICAL
, k
, "LOGICAL", gfc_default_logical_kind
);
4703 return &gfc_bad_expr
;
4705 if (e
->expr_type
!= EXPR_CONSTANT
)
4708 return gfc_get_logical_expr (kind
, &e
->where
, e
->value
.logical
);
4713 gfc_simplify_matmul (gfc_expr
*matrix_a
, gfc_expr
*matrix_b
)
4716 int row
, result_rows
, col
, result_columns
;
4717 int stride_a
, offset_a
, stride_b
, offset_b
;
4719 if (!is_constant_array_expr (matrix_a
)
4720 || !is_constant_array_expr (matrix_b
))
4723 /* MATMUL should do mixed-mode arithmetic. Set the result type. */
4724 if (matrix_a
->ts
.type
!= matrix_b
->ts
.type
)
4727 e
.expr_type
= EXPR_OP
;
4728 gfc_clear_ts (&e
.ts
);
4729 e
.value
.op
.op
= INTRINSIC_NONE
;
4730 e
.value
.op
.op1
= matrix_a
;
4731 e
.value
.op
.op2
= matrix_b
;
4732 gfc_type_convert_binary (&e
, 1);
4733 result
= gfc_get_array_expr (e
.ts
.type
, e
.ts
.kind
, &matrix_a
->where
);
4737 result
= gfc_get_array_expr (matrix_a
->ts
.type
, matrix_a
->ts
.kind
,
4741 if (matrix_a
->rank
== 1 && matrix_b
->rank
== 2)
4744 result_columns
= mpz_get_si (matrix_b
->shape
[1]);
4746 stride_b
= mpz_get_si (matrix_b
->shape
[0]);
4749 result
->shape
= gfc_get_shape (result
->rank
);
4750 mpz_init_set_si (result
->shape
[0], result_columns
);
4752 else if (matrix_a
->rank
== 2 && matrix_b
->rank
== 1)
4754 result_rows
= mpz_get_si (matrix_a
->shape
[0]);
4756 stride_a
= mpz_get_si (matrix_a
->shape
[0]);
4760 result
->shape
= gfc_get_shape (result
->rank
);
4761 mpz_init_set_si (result
->shape
[0], result_rows
);
4763 else if (matrix_a
->rank
== 2 && matrix_b
->rank
== 2)
4765 result_rows
= mpz_get_si (matrix_a
->shape
[0]);
4766 result_columns
= mpz_get_si (matrix_b
->shape
[1]);
4767 stride_a
= mpz_get_si (matrix_a
->shape
[0]);
4768 stride_b
= mpz_get_si (matrix_b
->shape
[0]);
4771 result
->shape
= gfc_get_shape (result
->rank
);
4772 mpz_init_set_si (result
->shape
[0], result_rows
);
4773 mpz_init_set_si (result
->shape
[1], result_columns
);
4779 for (col
= 0; col
< result_columns
; ++col
)
4783 for (row
= 0; row
< result_rows
; ++row
)
4785 gfc_expr
*e
= compute_dot_product (matrix_a
, stride_a
, offset_a
,
4786 matrix_b
, 1, offset_b
, false);
4787 gfc_constructor_append_expr (&result
->value
.constructor
,
4793 offset_b
+= stride_b
;
4801 gfc_simplify_maskr (gfc_expr
*i
, gfc_expr
*kind_arg
)
4806 if (i
->expr_type
!= EXPR_CONSTANT
)
4809 kind
= get_kind (BT_INTEGER
, kind_arg
, "MASKR", gfc_default_integer_kind
);
4811 return &gfc_bad_expr
;
4812 k
= gfc_validate_kind (BT_INTEGER
, kind
, false);
4814 bool fail
= gfc_extract_int (i
, &arg
);
4817 if (!gfc_check_mask (i
, kind_arg
))
4818 return &gfc_bad_expr
;
4820 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &i
->where
);
4822 /* MASKR(n) = 2^n - 1 */
4823 mpz_set_ui (result
->value
.integer
, 1);
4824 mpz_mul_2exp (result
->value
.integer
, result
->value
.integer
, arg
);
4825 mpz_sub_ui (result
->value
.integer
, result
->value
.integer
, 1);
4827 gfc_convert_mpz_to_signed (result
->value
.integer
, gfc_integer_kinds
[k
].bit_size
);
4834 gfc_simplify_maskl (gfc_expr
*i
, gfc_expr
*kind_arg
)
4840 if (i
->expr_type
!= EXPR_CONSTANT
)
4843 kind
= get_kind (BT_INTEGER
, kind_arg
, "MASKL", gfc_default_integer_kind
);
4845 return &gfc_bad_expr
;
4846 k
= gfc_validate_kind (BT_INTEGER
, kind
, false);
4848 bool fail
= gfc_extract_int (i
, &arg
);
4851 if (!gfc_check_mask (i
, kind_arg
))
4852 return &gfc_bad_expr
;
4854 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &i
->where
);
4856 /* MASKL(n) = 2^bit_size - 2^(bit_size - n) */
4857 mpz_init_set_ui (z
, 1);
4858 mpz_mul_2exp (z
, z
, gfc_integer_kinds
[k
].bit_size
);
4859 mpz_set_ui (result
->value
.integer
, 1);
4860 mpz_mul_2exp (result
->value
.integer
, result
->value
.integer
,
4861 gfc_integer_kinds
[k
].bit_size
- arg
);
4862 mpz_sub (result
->value
.integer
, z
, result
->value
.integer
);
4865 gfc_convert_mpz_to_signed (result
->value
.integer
, gfc_integer_kinds
[k
].bit_size
);
4872 gfc_simplify_merge (gfc_expr
*tsource
, gfc_expr
*fsource
, gfc_expr
*mask
)
4875 gfc_constructor
*tsource_ctor
, *fsource_ctor
, *mask_ctor
;
4877 if (mask
->expr_type
== EXPR_CONSTANT
)
4879 result
= gfc_copy_expr (mask
->value
.logical
? tsource
: fsource
);
4880 /* Parenthesis is needed to get lower bounds of 1. */
4881 result
= gfc_get_parentheses (result
);
4882 gfc_simplify_expr (result
, 1);
4886 if (!mask
->rank
|| !is_constant_array_expr (mask
)
4887 || !is_constant_array_expr (tsource
) || !is_constant_array_expr (fsource
))
4890 result
= gfc_get_array_expr (tsource
->ts
.type
, tsource
->ts
.kind
,
4892 if (tsource
->ts
.type
== BT_DERIVED
)
4893 result
->ts
.u
.derived
= tsource
->ts
.u
.derived
;
4894 else if (tsource
->ts
.type
== BT_CHARACTER
)
4895 result
->ts
.u
.cl
= tsource
->ts
.u
.cl
;
4897 tsource_ctor
= gfc_constructor_first (tsource
->value
.constructor
);
4898 fsource_ctor
= gfc_constructor_first (fsource
->value
.constructor
);
4899 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
4903 if (mask_ctor
->expr
->value
.logical
)
4904 gfc_constructor_append_expr (&result
->value
.constructor
,
4905 gfc_copy_expr (tsource_ctor
->expr
),
4908 gfc_constructor_append_expr (&result
->value
.constructor
,
4909 gfc_copy_expr (fsource_ctor
->expr
),
4911 tsource_ctor
= gfc_constructor_next (tsource_ctor
);
4912 fsource_ctor
= gfc_constructor_next (fsource_ctor
);
4913 mask_ctor
= gfc_constructor_next (mask_ctor
);
4916 result
->shape
= gfc_get_shape (1);
4917 gfc_array_size (result
, &result
->shape
[0]);
4924 gfc_simplify_merge_bits (gfc_expr
*i
, gfc_expr
*j
, gfc_expr
*mask_expr
)
4926 mpz_t arg1
, arg2
, mask
;
4929 if (i
->expr_type
!= EXPR_CONSTANT
|| j
->expr_type
!= EXPR_CONSTANT
4930 || mask_expr
->expr_type
!= EXPR_CONSTANT
)
4933 result
= gfc_get_constant_expr (BT_INTEGER
, i
->ts
.kind
, &i
->where
);
4935 /* Convert all argument to unsigned. */
4936 mpz_init_set (arg1
, i
->value
.integer
);
4937 mpz_init_set (arg2
, j
->value
.integer
);
4938 mpz_init_set (mask
, mask_expr
->value
.integer
);
4940 /* MERGE_BITS(I,J,MASK) = IOR (IAND (I, MASK), IAND (J, NOT (MASK))). */
4941 mpz_and (arg1
, arg1
, mask
);
4942 mpz_com (mask
, mask
);
4943 mpz_and (arg2
, arg2
, mask
);
4944 mpz_ior (result
->value
.integer
, arg1
, arg2
);
4954 /* Selects between current value and extremum for simplify_min_max
4955 and simplify_minval_maxval. */
4957 min_max_choose (gfc_expr
*arg
, gfc_expr
*extremum
, int sign
, bool back_val
)
4961 switch (arg
->ts
.type
)
4964 if (extremum
->ts
.kind
< arg
->ts
.kind
)
4965 extremum
->ts
.kind
= arg
->ts
.kind
;
4966 ret
= mpz_cmp (arg
->value
.integer
,
4967 extremum
->value
.integer
) * sign
;
4969 mpz_set (extremum
->value
.integer
, arg
->value
.integer
);
4973 if (extremum
->ts
.kind
< arg
->ts
.kind
)
4974 extremum
->ts
.kind
= arg
->ts
.kind
;
4975 if (mpfr_nan_p (extremum
->value
.real
))
4978 mpfr_set (extremum
->value
.real
, arg
->value
.real
, GFC_RND_MODE
);
4980 else if (mpfr_nan_p (arg
->value
.real
))
4984 ret
= mpfr_cmp (arg
->value
.real
, extremum
->value
.real
) * sign
;
4986 mpfr_set (extremum
->value
.real
, arg
->value
.real
, GFC_RND_MODE
);
4991 #define LENGTH(x) ((x)->value.character.length)
4992 #define STRING(x) ((x)->value.character.string)
4993 if (LENGTH (extremum
) < LENGTH(arg
))
4995 gfc_char_t
*tmp
= STRING(extremum
);
4997 STRING(extremum
) = gfc_get_wide_string (LENGTH(arg
) + 1);
4998 memcpy (STRING(extremum
), tmp
,
4999 LENGTH(extremum
) * sizeof (gfc_char_t
));
5000 gfc_wide_memset (&STRING(extremum
)[LENGTH(extremum
)], ' ',
5001 LENGTH(arg
) - LENGTH(extremum
));
5002 STRING(extremum
)[LENGTH(arg
)] = '\0'; /* For debugger */
5003 LENGTH(extremum
) = LENGTH(arg
);
5006 ret
= gfc_compare_string (arg
, extremum
) * sign
;
5009 free (STRING(extremum
));
5010 STRING(extremum
) = gfc_get_wide_string (LENGTH(extremum
) + 1);
5011 memcpy (STRING(extremum
), STRING(arg
),
5012 LENGTH(arg
) * sizeof (gfc_char_t
));
5013 gfc_wide_memset (&STRING(extremum
)[LENGTH(arg
)], ' ',
5014 LENGTH(extremum
) - LENGTH(arg
));
5015 STRING(extremum
)[LENGTH(extremum
)] = '\0'; /* For debugger */
5022 gfc_internal_error ("simplify_min_max(): Bad type in arglist");
5024 if (back_val
&& ret
== 0)
5031 /* This function is special since MAX() can take any number of
5032 arguments. The simplified expression is a rewritten version of the
5033 argument list containing at most one constant element. Other
5034 constant elements are deleted. Because the argument list has
5035 already been checked, this function always succeeds. sign is 1 for
5036 MAX(), -1 for MIN(). */
5039 simplify_min_max (gfc_expr
*expr
, int sign
)
5042 gfc_actual_arglist
*arg
, *last
, *extremum
;
5043 gfc_expr
*tmp
, *ret
;
5049 arg
= expr
->value
.function
.actual
;
5051 for (; arg
; last
= arg
, arg
= arg
->next
)
5053 if (arg
->expr
->expr_type
!= EXPR_CONSTANT
)
5056 if (extremum
== NULL
)
5062 min_max_choose (arg
->expr
, extremum
->expr
, sign
);
5064 /* Delete the extra constant argument. */
5065 last
->next
= arg
->next
;
5068 gfc_free_actual_arglist (arg
);
5072 /* If there is one value left, replace the function call with the
5074 if (expr
->value
.function
.actual
->next
!= NULL
)
5077 /* Handle special cases of specific functions (min|max)1 and
5080 tmp
= expr
->value
.function
.actual
->expr
;
5081 fname
= expr
->value
.function
.isym
->name
;
5083 if ((tmp
->ts
.type
!= BT_INTEGER
|| tmp
->ts
.kind
!= gfc_integer_4_kind
)
5084 && (strcmp (fname
, "min1") == 0 || strcmp (fname
, "max1") == 0))
5086 /* Explicit conversion, turn off -Wconversion and -Wconversion-extra
5088 tmp1
= warn_conversion
;
5089 tmp2
= warn_conversion_extra
;
5090 warn_conversion
= warn_conversion_extra
= 0;
5092 ret
= gfc_convert_constant (tmp
, BT_INTEGER
, gfc_integer_4_kind
);
5094 warn_conversion
= tmp1
;
5095 warn_conversion_extra
= tmp2
;
5097 else if ((tmp
->ts
.type
!= BT_REAL
|| tmp
->ts
.kind
!= gfc_real_4_kind
)
5098 && (strcmp (fname
, "amin0") == 0 || strcmp (fname
, "amax0") == 0))
5100 ret
= gfc_convert_constant (tmp
, BT_REAL
, gfc_real_4_kind
);
5103 ret
= gfc_copy_expr (tmp
);
5111 gfc_simplify_min (gfc_expr
*e
)
5113 return simplify_min_max (e
, -1);
5118 gfc_simplify_max (gfc_expr
*e
)
5120 return simplify_min_max (e
, 1);
5123 /* Helper function for gfc_simplify_minval. */
5126 gfc_min (gfc_expr
*op1
, gfc_expr
*op2
)
5128 min_max_choose (op1
, op2
, -1);
5129 gfc_free_expr (op1
);
5133 /* Simplify minval for constant arrays. */
5136 gfc_simplify_minval (gfc_expr
*array
, gfc_expr
* dim
, gfc_expr
*mask
)
5138 return simplify_transformation (array
, dim
, mask
, INT_MAX
, gfc_min
);
5141 /* Helper function for gfc_simplify_maxval. */
5144 gfc_max (gfc_expr
*op1
, gfc_expr
*op2
)
5146 min_max_choose (op1
, op2
, 1);
5147 gfc_free_expr (op1
);
5152 /* Simplify maxval for constant arrays. */
5155 gfc_simplify_maxval (gfc_expr
*array
, gfc_expr
* dim
, gfc_expr
*mask
)
5157 return simplify_transformation (array
, dim
, mask
, INT_MIN
, gfc_max
);
5161 /* Transform minloc or maxloc of an array, according to MASK,
5162 to the scalar result. This code is mostly identical to
5163 simplify_transformation_to_scalar. */
5166 simplify_minmaxloc_to_scalar (gfc_expr
*result
, gfc_expr
*array
, gfc_expr
*mask
,
5167 gfc_expr
*extremum
, int sign
, bool back_val
)
5170 gfc_constructor
*array_ctor
, *mask_ctor
;
5173 mpz_set_si (result
->value
.integer
, 0);
5176 /* Shortcut for constant .FALSE. MASK. */
5178 && mask
->expr_type
== EXPR_CONSTANT
5179 && !mask
->value
.logical
)
5182 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
5183 if (mask
&& mask
->expr_type
== EXPR_ARRAY
)
5184 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
5188 mpz_init_set_si (count
, 0);
5191 mpz_add_ui (count
, count
, 1);
5192 a
= array_ctor
->expr
;
5193 array_ctor
= gfc_constructor_next (array_ctor
);
5194 /* A constant MASK equals .TRUE. here and can be ignored. */
5197 m
= mask_ctor
->expr
;
5198 mask_ctor
= gfc_constructor_next (mask_ctor
);
5199 if (!m
->value
.logical
)
5202 if (min_max_choose (a
, extremum
, sign
, back_val
) > 0)
5203 mpz_set (result
->value
.integer
, count
);
5206 gfc_free_expr (extremum
);
5210 /* Simplify minloc / maxloc in the absence of a dim argument. */
5213 simplify_minmaxloc_nodim (gfc_expr
*result
, gfc_expr
*extremum
,
5214 gfc_expr
*array
, gfc_expr
*mask
, int sign
,
5217 ssize_t res
[GFC_MAX_DIMENSIONS
];
5219 gfc_constructor
*result_ctor
, *array_ctor
, *mask_ctor
;
5220 ssize_t count
[GFC_MAX_DIMENSIONS
], extent
[GFC_MAX_DIMENSIONS
],
5221 sstride
[GFC_MAX_DIMENSIONS
];
5226 for (i
= 0; i
<array
->rank
; i
++)
5229 /* Shortcut for constant .FALSE. MASK. */
5231 && mask
->expr_type
== EXPR_CONSTANT
5232 && !mask
->value
.logical
)
5235 if (array
->shape
== NULL
)
5238 for (i
= 0; i
< array
->rank
; i
++)
5241 sstride
[i
] = (i
== 0) ? 1 : sstride
[i
-1] * mpz_get_si (array
->shape
[i
-1]);
5242 extent
[i
] = mpz_get_si (array
->shape
[i
]);
5247 continue_loop
= true;
5248 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
5249 if (mask
&& mask
->rank
> 0)
5250 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
5254 /* Loop over the array elements (and mask), keeping track of
5255 the indices to return. */
5256 while (continue_loop
)
5260 a
= array_ctor
->expr
;
5263 m
= mask_ctor
->expr
;
5264 ma
= m
->value
.logical
;
5265 mask_ctor
= gfc_constructor_next (mask_ctor
);
5270 if (ma
&& min_max_choose (a
, extremum
, sign
, back_val
) > 0)
5272 for (i
= 0; i
<array
->rank
; i
++)
5275 array_ctor
= gfc_constructor_next (array_ctor
);
5277 } while (count
[0] != extent
[0]);
5281 /* When we get to the end of a dimension, reset it and increment
5282 the next dimension. */
5285 if (n
>= array
->rank
)
5287 continue_loop
= false;
5292 } while (count
[n
] == extent
[n
]);
5296 gfc_free_expr (extremum
);
5297 result_ctor
= gfc_constructor_first (result
->value
.constructor
);
5298 for (i
= 0; i
<array
->rank
; i
++)
5301 r_expr
= result_ctor
->expr
;
5302 mpz_set_si (r_expr
->value
.integer
, res
[i
] + 1);
5303 result_ctor
= gfc_constructor_next (result_ctor
);
5308 /* Helper function for gfc_simplify_minmaxloc - build an array
5309 expression with n elements. */
5312 new_array (bt type
, int kind
, int n
, locus
*where
)
5317 result
= gfc_get_array_expr (type
, kind
, where
);
5319 result
->shape
= gfc_get_shape(1);
5320 mpz_init_set_si (result
->shape
[0], n
);
5321 for (i
= 0; i
< n
; i
++)
5323 gfc_constructor_append_expr (&result
->value
.constructor
,
5324 gfc_get_constant_expr (type
, kind
, where
),
5331 /* Simplify minloc and maxloc. This code is mostly identical to
5332 simplify_transformation_to_array. */
5335 simplify_minmaxloc_to_array (gfc_expr
*result
, gfc_expr
*array
,
5336 gfc_expr
*dim
, gfc_expr
*mask
,
5337 gfc_expr
*extremum
, int sign
, bool back_val
)
5340 int done
, i
, n
, arraysize
, resultsize
, dim_index
, dim_extent
, dim_stride
;
5341 gfc_expr
**arrayvec
, **resultvec
, **base
, **src
, **dest
;
5342 gfc_constructor
*array_ctor
, *mask_ctor
, *result_ctor
;
5344 int count
[GFC_MAX_DIMENSIONS
], extent
[GFC_MAX_DIMENSIONS
],
5345 sstride
[GFC_MAX_DIMENSIONS
], dstride
[GFC_MAX_DIMENSIONS
],
5346 tmpstride
[GFC_MAX_DIMENSIONS
];
5348 /* Shortcut for constant .FALSE. MASK. */
5350 && mask
->expr_type
== EXPR_CONSTANT
5351 && !mask
->value
.logical
)
5354 /* Build an indexed table for array element expressions to minimize
5355 linked-list traversal. Masked elements are set to NULL. */
5356 gfc_array_size (array
, &size
);
5357 arraysize
= mpz_get_ui (size
);
5360 arrayvec
= XCNEWVEC (gfc_expr
*, arraysize
);
5362 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
5364 if (mask
&& mask
->expr_type
== EXPR_ARRAY
)
5365 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
5367 for (i
= 0; i
< arraysize
; ++i
)
5369 arrayvec
[i
] = array_ctor
->expr
;
5370 array_ctor
= gfc_constructor_next (array_ctor
);
5374 if (!mask_ctor
->expr
->value
.logical
)
5377 mask_ctor
= gfc_constructor_next (mask_ctor
);
5381 /* Same for the result expression. */
5382 gfc_array_size (result
, &size
);
5383 resultsize
= mpz_get_ui (size
);
5386 resultvec
= XCNEWVEC (gfc_expr
*, resultsize
);
5387 result_ctor
= gfc_constructor_first (result
->value
.constructor
);
5388 for (i
= 0; i
< resultsize
; ++i
)
5390 resultvec
[i
] = result_ctor
->expr
;
5391 result_ctor
= gfc_constructor_next (result_ctor
);
5394 gfc_extract_int (dim
, &dim_index
);
5395 dim_index
-= 1; /* zero-base index */
5399 for (i
= 0, n
= 0; i
< array
->rank
; ++i
)
5402 tmpstride
[i
] = (i
== 0) ? 1 : tmpstride
[i
-1] * mpz_get_si (array
->shape
[i
-1]);
5405 dim_extent
= mpz_get_si (array
->shape
[i
]);
5406 dim_stride
= tmpstride
[i
];
5410 extent
[n
] = mpz_get_si (array
->shape
[i
]);
5411 sstride
[n
] = tmpstride
[i
];
5412 dstride
[n
] = (n
== 0) ? 1 : dstride
[n
-1] * extent
[n
-1];
5416 done
= resultsize
<= 0;
5422 ex
= gfc_copy_expr (extremum
);
5423 for (src
= base
, n
= 0; n
< dim_extent
; src
+= dim_stride
, ++n
)
5425 if (*src
&& min_max_choose (*src
, ex
, sign
, back_val
) > 0)
5426 mpz_set_si ((*dest
)->value
.integer
, n
+ 1);
5435 while (!done
&& count
[n
] == extent
[n
])
5438 base
-= sstride
[n
] * extent
[n
];
5439 dest
-= dstride
[n
] * extent
[n
];
5442 if (n
< result
->rank
)
5444 /* If the nested loop is unrolled GFC_MAX_DIMENSIONS
5445 times, we'd warn for the last iteration, because the
5446 array index will have already been incremented to the
5447 array sizes, and we can't tell that this must make
5448 the test against result->rank false, because ranks
5449 must not exceed GFC_MAX_DIMENSIONS. */
5450 GCC_DIAGNOSTIC_PUSH_IGNORED (-Warray
-bounds
)
5461 /* Place updated expression in result constructor. */
5462 result_ctor
= gfc_constructor_first (result
->value
.constructor
);
5463 for (i
= 0; i
< resultsize
; ++i
)
5465 result_ctor
->expr
= resultvec
[i
];
5466 result_ctor
= gfc_constructor_next (result_ctor
);
5475 /* Simplify minloc and maxloc for constant arrays. */
5478 gfc_simplify_minmaxloc (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
,
5479 gfc_expr
*kind
, gfc_expr
*back
, int sign
)
5485 bool back_val
= false;
5487 if (!is_constant_array_expr (array
)
5488 || !gfc_is_constant_expr (dim
))
5492 && !is_constant_array_expr (mask
)
5493 && mask
->expr_type
!= EXPR_CONSTANT
)
5498 if (gfc_extract_int (kind
, &ikind
, -1))
5502 ikind
= gfc_default_integer_kind
;
5506 if (back
->expr_type
!= EXPR_CONSTANT
)
5509 back_val
= back
->value
.logical
;
5519 extremum
= gfc_get_constant_expr (array
->ts
.type
, array
->ts
.kind
, &array
->where
);
5520 init_result_expr (extremum
, init_val
, array
);
5524 result
= transformational_result (array
, dim
, BT_INTEGER
,
5525 ikind
, &array
->where
);
5526 init_result_expr (result
, 0, array
);
5528 if (array
->rank
== 1)
5529 return simplify_minmaxloc_to_scalar (result
, array
, mask
, extremum
,
5532 return simplify_minmaxloc_to_array (result
, array
, dim
, mask
, extremum
,
5537 result
= new_array (BT_INTEGER
, ikind
, array
->rank
, &array
->where
);
5538 return simplify_minmaxloc_nodim (result
, extremum
, array
, mask
,
5544 gfc_simplify_minloc (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
, gfc_expr
*kind
,
5547 return gfc_simplify_minmaxloc (array
, dim
, mask
, kind
, back
, -1);
5551 gfc_simplify_maxloc (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
, gfc_expr
*kind
,
5554 return gfc_simplify_minmaxloc (array
, dim
, mask
, kind
, back
, 1);
5557 /* Simplify findloc to scalar. Similar to
5558 simplify_minmaxloc_to_scalar. */
5561 simplify_findloc_to_scalar (gfc_expr
*result
, gfc_expr
*array
, gfc_expr
*value
,
5562 gfc_expr
*mask
, int back_val
)
5565 gfc_constructor
*array_ctor
, *mask_ctor
;
5568 mpz_set_si (result
->value
.integer
, 0);
5570 /* Shortcut for constant .FALSE. MASK. */
5572 && mask
->expr_type
== EXPR_CONSTANT
5573 && !mask
->value
.logical
)
5576 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
5577 if (mask
&& mask
->expr_type
== EXPR_ARRAY
)
5578 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
5582 mpz_init_set_si (count
, 0);
5585 mpz_add_ui (count
, count
, 1);
5586 a
= array_ctor
->expr
;
5587 array_ctor
= gfc_constructor_next (array_ctor
);
5588 /* A constant MASK equals .TRUE. here and can be ignored. */
5591 m
= mask_ctor
->expr
;
5592 mask_ctor
= gfc_constructor_next (mask_ctor
);
5593 if (!m
->value
.logical
)
5596 if (gfc_compare_expr (a
, value
, INTRINSIC_EQ
) == 0)
5598 /* We have a match. If BACK is true, continue so we find
5600 mpz_set (result
->value
.integer
, count
);
5609 /* Simplify findloc in the absence of a dim argument. Similar to
5610 simplify_minmaxloc_nodim. */
5613 simplify_findloc_nodim (gfc_expr
*result
, gfc_expr
*value
, gfc_expr
*array
,
5614 gfc_expr
*mask
, bool back_val
)
5616 ssize_t res
[GFC_MAX_DIMENSIONS
];
5618 gfc_constructor
*result_ctor
, *array_ctor
, *mask_ctor
;
5619 ssize_t count
[GFC_MAX_DIMENSIONS
], extent
[GFC_MAX_DIMENSIONS
],
5620 sstride
[GFC_MAX_DIMENSIONS
];
5625 for (i
= 0; i
< array
->rank
; i
++)
5628 /* Shortcut for constant .FALSE. MASK. */
5630 && mask
->expr_type
== EXPR_CONSTANT
5631 && !mask
->value
.logical
)
5634 for (i
= 0; i
< array
->rank
; i
++)
5637 sstride
[i
] = (i
== 0) ? 1 : sstride
[i
-1] * mpz_get_si (array
->shape
[i
-1]);
5638 extent
[i
] = mpz_get_si (array
->shape
[i
]);
5643 continue_loop
= true;
5644 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
5645 if (mask
&& mask
->rank
> 0)
5646 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
5650 /* Loop over the array elements (and mask), keeping track of
5651 the indices to return. */
5652 while (continue_loop
)
5656 a
= array_ctor
->expr
;
5659 m
= mask_ctor
->expr
;
5660 ma
= m
->value
.logical
;
5661 mask_ctor
= gfc_constructor_next (mask_ctor
);
5666 if (ma
&& gfc_compare_expr (a
, value
, INTRINSIC_EQ
) == 0)
5668 for (i
= 0; i
< array
->rank
; i
++)
5673 array_ctor
= gfc_constructor_next (array_ctor
);
5675 } while (count
[0] != extent
[0]);
5679 /* When we get to the end of a dimension, reset it and increment
5680 the next dimension. */
5683 if (n
>= array
->rank
)
5685 continue_loop
= false;
5690 } while (count
[n
] == extent
[n
]);
5694 result_ctor
= gfc_constructor_first (result
->value
.constructor
);
5695 for (i
= 0; i
< array
->rank
; i
++)
5698 r_expr
= result_ctor
->expr
;
5699 mpz_set_si (r_expr
->value
.integer
, res
[i
] + 1);
5700 result_ctor
= gfc_constructor_next (result_ctor
);
5706 /* Simplify findloc to an array. Similar to
5707 simplify_minmaxloc_to_array. */
5710 simplify_findloc_to_array (gfc_expr
*result
, gfc_expr
*array
, gfc_expr
*value
,
5711 gfc_expr
*dim
, gfc_expr
*mask
, bool back_val
)
5714 int done
, i
, n
, arraysize
, resultsize
, dim_index
, dim_extent
, dim_stride
;
5715 gfc_expr
**arrayvec
, **resultvec
, **base
, **src
, **dest
;
5716 gfc_constructor
*array_ctor
, *mask_ctor
, *result_ctor
;
5718 int count
[GFC_MAX_DIMENSIONS
], extent
[GFC_MAX_DIMENSIONS
],
5719 sstride
[GFC_MAX_DIMENSIONS
], dstride
[GFC_MAX_DIMENSIONS
],
5720 tmpstride
[GFC_MAX_DIMENSIONS
];
5722 /* Shortcut for constant .FALSE. MASK. */
5724 && mask
->expr_type
== EXPR_CONSTANT
5725 && !mask
->value
.logical
)
5728 /* Build an indexed table for array element expressions to minimize
5729 linked-list traversal. Masked elements are set to NULL. */
5730 gfc_array_size (array
, &size
);
5731 arraysize
= mpz_get_ui (size
);
5734 arrayvec
= XCNEWVEC (gfc_expr
*, arraysize
);
5736 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
5738 if (mask
&& mask
->expr_type
== EXPR_ARRAY
)
5739 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
5741 for (i
= 0; i
< arraysize
; ++i
)
5743 arrayvec
[i
] = array_ctor
->expr
;
5744 array_ctor
= gfc_constructor_next (array_ctor
);
5748 if (!mask_ctor
->expr
->value
.logical
)
5751 mask_ctor
= gfc_constructor_next (mask_ctor
);
5755 /* Same for the result expression. */
5756 gfc_array_size (result
, &size
);
5757 resultsize
= mpz_get_ui (size
);
5760 resultvec
= XCNEWVEC (gfc_expr
*, resultsize
);
5761 result_ctor
= gfc_constructor_first (result
->value
.constructor
);
5762 for (i
= 0; i
< resultsize
; ++i
)
5764 resultvec
[i
] = result_ctor
->expr
;
5765 result_ctor
= gfc_constructor_next (result_ctor
);
5768 gfc_extract_int (dim
, &dim_index
);
5770 dim_index
-= 1; /* Zero-base index. */
5774 for (i
= 0, n
= 0; i
< array
->rank
; ++i
)
5777 tmpstride
[i
] = (i
== 0) ? 1 : tmpstride
[i
-1] * mpz_get_si (array
->shape
[i
-1]);
5780 dim_extent
= mpz_get_si (array
->shape
[i
]);
5781 dim_stride
= tmpstride
[i
];
5785 extent
[n
] = mpz_get_si (array
->shape
[i
]);
5786 sstride
[n
] = tmpstride
[i
];
5787 dstride
[n
] = (n
== 0) ? 1 : dstride
[n
-1] * extent
[n
-1];
5791 done
= resultsize
<= 0;
5796 for (src
= base
, n
= 0; n
< dim_extent
; src
+= dim_stride
, ++n
)
5798 if (*src
&& gfc_compare_expr (*src
, value
, INTRINSIC_EQ
) == 0)
5800 mpz_set_si ((*dest
)->value
.integer
, n
+ 1);
5811 while (!done
&& count
[n
] == extent
[n
])
5814 base
-= sstride
[n
] * extent
[n
];
5815 dest
-= dstride
[n
] * extent
[n
];
5818 if (n
< result
->rank
)
5820 /* If the nested loop is unrolled GFC_MAX_DIMENSIONS
5821 times, we'd warn for the last iteration, because the
5822 array index will have already been incremented to the
5823 array sizes, and we can't tell that this must make
5824 the test against result->rank false, because ranks
5825 must not exceed GFC_MAX_DIMENSIONS. */
5826 GCC_DIAGNOSTIC_PUSH_IGNORED (-Warray
-bounds
)
5837 /* Place updated expression in result constructor. */
5838 result_ctor
= gfc_constructor_first (result
->value
.constructor
);
5839 for (i
= 0; i
< resultsize
; ++i
)
5841 result_ctor
->expr
= resultvec
[i
];
5842 result_ctor
= gfc_constructor_next (result_ctor
);
5850 /* Simplify findloc. */
5853 gfc_simplify_findloc (gfc_expr
*array
, gfc_expr
*value
, gfc_expr
*dim
,
5854 gfc_expr
*mask
, gfc_expr
*kind
, gfc_expr
*back
)
5858 bool back_val
= false;
5860 if (!is_constant_array_expr (array
)
5861 || array
->shape
== NULL
5862 || !gfc_is_constant_expr (dim
))
5865 if (! gfc_is_constant_expr (value
))
5869 && !is_constant_array_expr (mask
)
5870 && mask
->expr_type
!= EXPR_CONSTANT
)
5875 if (gfc_extract_int (kind
, &ikind
, -1))
5879 ikind
= gfc_default_integer_kind
;
5883 if (back
->expr_type
!= EXPR_CONSTANT
)
5886 back_val
= back
->value
.logical
;
5891 result
= transformational_result (array
, dim
, BT_INTEGER
,
5892 ikind
, &array
->where
);
5893 init_result_expr (result
, 0, array
);
5895 if (array
->rank
== 1)
5896 return simplify_findloc_to_scalar (result
, array
, value
, mask
,
5899 return simplify_findloc_to_array (result
, array
, value
, dim
, mask
,
5904 result
= new_array (BT_INTEGER
, ikind
, array
->rank
, &array
->where
);
5905 return simplify_findloc_nodim (result
, value
, array
, mask
, back_val
);
5911 gfc_simplify_maxexponent (gfc_expr
*x
)
5913 int i
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, false);
5914 return gfc_get_int_expr (gfc_default_integer_kind
, &x
->where
,
5915 gfc_real_kinds
[i
].max_exponent
);
5920 gfc_simplify_minexponent (gfc_expr
*x
)
5922 int i
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, false);
5923 return gfc_get_int_expr (gfc_default_integer_kind
, &x
->where
,
5924 gfc_real_kinds
[i
].min_exponent
);
5929 gfc_simplify_mod (gfc_expr
*a
, gfc_expr
*p
)
5934 /* First check p. */
5935 if (p
->expr_type
!= EXPR_CONSTANT
)
5938 /* p shall not be 0. */
5942 if (mpz_cmp_ui (p
->value
.integer
, 0) == 0)
5944 gfc_error ("Argument %qs of MOD at %L shall not be zero",
5946 return &gfc_bad_expr
;
5950 if (mpfr_cmp_ui (p
->value
.real
, 0) == 0)
5952 gfc_error ("Argument %qs of MOD at %L shall not be zero",
5954 return &gfc_bad_expr
;
5958 gfc_internal_error ("gfc_simplify_mod(): Bad arguments");
5961 if (a
->expr_type
!= EXPR_CONSTANT
)
5964 kind
= a
->ts
.kind
> p
->ts
.kind
? a
->ts
.kind
: p
->ts
.kind
;
5965 result
= gfc_get_constant_expr (a
->ts
.type
, kind
, &a
->where
);
5967 if (a
->ts
.type
== BT_INTEGER
)
5968 mpz_tdiv_r (result
->value
.integer
, a
->value
.integer
, p
->value
.integer
);
5971 gfc_set_model_kind (kind
);
5972 mpfr_fmod (result
->value
.real
, a
->value
.real
, p
->value
.real
,
5976 return range_check (result
, "MOD");
5981 gfc_simplify_modulo (gfc_expr
*a
, gfc_expr
*p
)
5986 /* First check p. */
5987 if (p
->expr_type
!= EXPR_CONSTANT
)
5990 /* p shall not be 0. */
5994 if (mpz_cmp_ui (p
->value
.integer
, 0) == 0)
5996 gfc_error ("Argument %qs of MODULO at %L shall not be zero",
5998 return &gfc_bad_expr
;
6002 if (mpfr_cmp_ui (p
->value
.real
, 0) == 0)
6004 gfc_error ("Argument %qs of MODULO at %L shall not be zero",
6006 return &gfc_bad_expr
;
6010 gfc_internal_error ("gfc_simplify_modulo(): Bad arguments");
6013 if (a
->expr_type
!= EXPR_CONSTANT
)
6016 kind
= a
->ts
.kind
> p
->ts
.kind
? a
->ts
.kind
: p
->ts
.kind
;
6017 result
= gfc_get_constant_expr (a
->ts
.type
, kind
, &a
->where
);
6019 if (a
->ts
.type
== BT_INTEGER
)
6020 mpz_fdiv_r (result
->value
.integer
, a
->value
.integer
, p
->value
.integer
);
6023 gfc_set_model_kind (kind
);
6024 mpfr_fmod (result
->value
.real
, a
->value
.real
, p
->value
.real
,
6026 if (mpfr_cmp_ui (result
->value
.real
, 0) != 0)
6028 if (mpfr_signbit (a
->value
.real
) != mpfr_signbit (p
->value
.real
))
6029 mpfr_add (result
->value
.real
, result
->value
.real
, p
->value
.real
,
6033 mpfr_copysign (result
->value
.real
, result
->value
.real
,
6034 p
->value
.real
, GFC_RND_MODE
);
6037 return range_check (result
, "MODULO");
6042 gfc_simplify_nearest (gfc_expr
*x
, gfc_expr
*s
)
6045 mpfr_exp_t emin
, emax
;
6048 if (x
->expr_type
!= EXPR_CONSTANT
|| s
->expr_type
!= EXPR_CONSTANT
)
6051 result
= gfc_copy_expr (x
);
6053 /* Save current values of emin and emax. */
6054 emin
= mpfr_get_emin ();
6055 emax
= mpfr_get_emax ();
6057 /* Set emin and emax for the current model number. */
6058 kind
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, 0);
6059 mpfr_set_emin ((mpfr_exp_t
) gfc_real_kinds
[kind
].min_exponent
-
6060 mpfr_get_prec(result
->value
.real
) + 1);
6061 mpfr_set_emax ((mpfr_exp_t
) gfc_real_kinds
[kind
].max_exponent
);
6062 mpfr_check_range (result
->value
.real
, 0, MPFR_RNDU
);
6064 if (mpfr_sgn (s
->value
.real
) > 0)
6066 mpfr_nextabove (result
->value
.real
);
6067 mpfr_subnormalize (result
->value
.real
, 0, MPFR_RNDU
);
6071 mpfr_nextbelow (result
->value
.real
);
6072 mpfr_subnormalize (result
->value
.real
, 0, MPFR_RNDD
);
6075 mpfr_set_emin (emin
);
6076 mpfr_set_emax (emax
);
6078 /* Only NaN can occur. Do not use range check as it gives an
6079 error for denormal numbers. */
6080 if (mpfr_nan_p (result
->value
.real
) && flag_range_check
)
6082 gfc_error ("Result of NEAREST is NaN at %L", &result
->where
);
6083 gfc_free_expr (result
);
6084 return &gfc_bad_expr
;
6092 simplify_nint (const char *name
, gfc_expr
*e
, gfc_expr
*k
)
6094 gfc_expr
*itrunc
, *result
;
6097 kind
= get_kind (BT_INTEGER
, k
, name
, gfc_default_integer_kind
);
6099 return &gfc_bad_expr
;
6101 if (e
->expr_type
!= EXPR_CONSTANT
)
6104 itrunc
= gfc_copy_expr (e
);
6105 mpfr_round (itrunc
->value
.real
, e
->value
.real
);
6107 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &e
->where
);
6108 gfc_mpfr_to_mpz (result
->value
.integer
, itrunc
->value
.real
, &e
->where
);
6110 gfc_free_expr (itrunc
);
6112 return range_check (result
, name
);
6117 gfc_simplify_new_line (gfc_expr
*e
)
6121 result
= gfc_get_character_expr (e
->ts
.kind
, &e
->where
, NULL
, 1);
6122 result
->value
.character
.string
[0] = '\n';
6129 gfc_simplify_nint (gfc_expr
*e
, gfc_expr
*k
)
6131 return simplify_nint ("NINT", e
, k
);
6136 gfc_simplify_idnint (gfc_expr
*e
)
6138 return simplify_nint ("IDNINT", e
, NULL
);
6141 static int norm2_scale
;
6144 norm2_add_squared (gfc_expr
*result
, gfc_expr
*e
)
6148 gcc_assert (e
->ts
.type
== BT_REAL
&& e
->expr_type
== EXPR_CONSTANT
);
6149 gcc_assert (result
->ts
.type
== BT_REAL
6150 && result
->expr_type
== EXPR_CONSTANT
);
6152 gfc_set_model_kind (result
->ts
.kind
);
6153 int index
= gfc_validate_kind (BT_REAL
, result
->ts
.kind
, false);
6155 if (mpfr_regular_p (result
->value
.real
))
6157 exp
= mpfr_get_exp (result
->value
.real
);
6158 /* If result is getting close to overflowing, scale down. */
6159 if (exp
>= gfc_real_kinds
[index
].max_exponent
- 4
6160 && norm2_scale
<= gfc_real_kinds
[index
].max_exponent
- 2)
6163 mpfr_div_ui (result
->value
.real
, result
->value
.real
, 16,
6169 if (mpfr_regular_p (e
->value
.real
))
6171 exp
= mpfr_get_exp (e
->value
.real
);
6172 /* If e**2 would overflow or close to overflowing, scale down. */
6173 if (exp
- norm2_scale
>= gfc_real_kinds
[index
].max_exponent
/ 2 - 2)
6175 int new_scale
= gfc_real_kinds
[index
].max_exponent
/ 2 + 4;
6176 mpfr_set_ui (tmp
, 1, GFC_RND_MODE
);
6177 mpfr_set_exp (tmp
, new_scale
- norm2_scale
);
6178 mpfr_div (result
->value
.real
, result
->value
.real
, tmp
, GFC_RND_MODE
);
6179 mpfr_div (result
->value
.real
, result
->value
.real
, tmp
, GFC_RND_MODE
);
6180 norm2_scale
= new_scale
;
6185 mpfr_set_ui (tmp
, 1, GFC_RND_MODE
);
6186 mpfr_set_exp (tmp
, norm2_scale
);
6187 mpfr_div (tmp
, e
->value
.real
, tmp
, GFC_RND_MODE
);
6190 mpfr_set (tmp
, e
->value
.real
, GFC_RND_MODE
);
6191 mpfr_pow_ui (tmp
, tmp
, 2, GFC_RND_MODE
);
6192 mpfr_add (result
->value
.real
, result
->value
.real
, tmp
,
6201 norm2_do_sqrt (gfc_expr
*result
, gfc_expr
*e
)
6203 gcc_assert (e
->ts
.type
== BT_REAL
&& e
->expr_type
== EXPR_CONSTANT
);
6204 gcc_assert (result
->ts
.type
== BT_REAL
6205 && result
->expr_type
== EXPR_CONSTANT
);
6208 mpfr_set (result
->value
.real
, e
->value
.real
, GFC_RND_MODE
);
6209 mpfr_sqrt (result
->value
.real
, result
->value
.real
, GFC_RND_MODE
);
6210 if (norm2_scale
&& mpfr_regular_p (result
->value
.real
))
6214 mpfr_set_ui (tmp
, 1, GFC_RND_MODE
);
6215 mpfr_set_exp (tmp
, norm2_scale
);
6216 mpfr_mul (result
->value
.real
, result
->value
.real
, tmp
, GFC_RND_MODE
);
6226 gfc_simplify_norm2 (gfc_expr
*e
, gfc_expr
*dim
)
6231 size_zero
= gfc_is_size_zero_array (e
);
6233 if (!(is_constant_array_expr (e
) || size_zero
)
6234 || (dim
!= NULL
&& !gfc_is_constant_expr (dim
)))
6237 result
= transformational_result (e
, dim
, e
->ts
.type
, e
->ts
.kind
, &e
->where
);
6238 init_result_expr (result
, 0, NULL
);
6244 if (!dim
|| e
->rank
== 1)
6246 result
= simplify_transformation_to_scalar (result
, e
, NULL
,
6248 mpfr_sqrt (result
->value
.real
, result
->value
.real
, GFC_RND_MODE
);
6249 if (norm2_scale
&& mpfr_regular_p (result
->value
.real
))
6253 mpfr_set_ui (tmp
, 1, GFC_RND_MODE
);
6254 mpfr_set_exp (tmp
, norm2_scale
);
6255 mpfr_mul (result
->value
.real
, result
->value
.real
, tmp
, GFC_RND_MODE
);
6261 result
= simplify_transformation_to_array (result
, e
, dim
, NULL
,
6270 gfc_simplify_not (gfc_expr
*e
)
6274 if (e
->expr_type
!= EXPR_CONSTANT
)
6277 result
= gfc_get_constant_expr (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
6278 mpz_com (result
->value
.integer
, e
->value
.integer
);
6280 return range_check (result
, "NOT");
6285 gfc_simplify_null (gfc_expr
*mold
)
6291 result
= gfc_copy_expr (mold
);
6292 result
->expr_type
= EXPR_NULL
;
6295 result
= gfc_get_null_expr (NULL
);
6302 gfc_simplify_num_images (gfc_expr
*distance ATTRIBUTE_UNUSED
, gfc_expr
*failed
)
6306 if (flag_coarray
== GFC_FCOARRAY_NONE
)
6308 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
6309 return &gfc_bad_expr
;
6312 if (flag_coarray
!= GFC_FCOARRAY_SINGLE
)
6315 if (failed
&& failed
->expr_type
!= EXPR_CONSTANT
)
6318 /* FIXME: gfc_current_locus is wrong. */
6319 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
6320 &gfc_current_locus
);
6322 if (failed
&& failed
->value
.logical
!= 0)
6323 mpz_set_si (result
->value
.integer
, 0);
6325 mpz_set_si (result
->value
.integer
, 1);
6332 gfc_simplify_or (gfc_expr
*x
, gfc_expr
*y
)
6337 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
6340 kind
= x
->ts
.kind
> y
->ts
.kind
? x
->ts
.kind
: y
->ts
.kind
;
6345 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &x
->where
);
6346 mpz_ior (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
6347 return range_check (result
, "OR");
6350 return gfc_get_logical_expr (kind
, &x
->where
,
6351 x
->value
.logical
|| y
->value
.logical
);
6359 gfc_simplify_pack (gfc_expr
*array
, gfc_expr
*mask
, gfc_expr
*vector
)
6362 gfc_constructor
*array_ctor
, *mask_ctor
, *vector_ctor
;
6364 if (!is_constant_array_expr (array
)
6365 || !is_constant_array_expr (vector
)
6366 || (!gfc_is_constant_expr (mask
)
6367 && !is_constant_array_expr (mask
)))
6370 result
= gfc_get_array_expr (array
->ts
.type
, array
->ts
.kind
, &array
->where
);
6371 if (array
->ts
.type
== BT_DERIVED
)
6372 result
->ts
.u
.derived
= array
->ts
.u
.derived
;
6374 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
6375 vector_ctor
= vector
6376 ? gfc_constructor_first (vector
->value
.constructor
)
6379 if (mask
->expr_type
== EXPR_CONSTANT
6380 && mask
->value
.logical
)
6382 /* Copy all elements of ARRAY to RESULT. */
6385 gfc_constructor_append_expr (&result
->value
.constructor
,
6386 gfc_copy_expr (array_ctor
->expr
),
6389 array_ctor
= gfc_constructor_next (array_ctor
);
6390 vector_ctor
= gfc_constructor_next (vector_ctor
);
6393 else if (mask
->expr_type
== EXPR_ARRAY
)
6395 /* Copy only those elements of ARRAY to RESULT whose
6396 MASK equals .TRUE.. */
6397 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
6398 while (mask_ctor
&& array_ctor
)
6400 if (mask_ctor
->expr
->value
.logical
)
6402 gfc_constructor_append_expr (&result
->value
.constructor
,
6403 gfc_copy_expr (array_ctor
->expr
),
6405 vector_ctor
= gfc_constructor_next (vector_ctor
);
6408 array_ctor
= gfc_constructor_next (array_ctor
);
6409 mask_ctor
= gfc_constructor_next (mask_ctor
);
6413 /* Append any left-over elements from VECTOR to RESULT. */
6416 gfc_constructor_append_expr (&result
->value
.constructor
,
6417 gfc_copy_expr (vector_ctor
->expr
),
6419 vector_ctor
= gfc_constructor_next (vector_ctor
);
6422 result
->shape
= gfc_get_shape (1);
6423 gfc_array_size (result
, &result
->shape
[0]);
6425 if (array
->ts
.type
== BT_CHARACTER
)
6426 result
->ts
.u
.cl
= array
->ts
.u
.cl
;
6433 do_xor (gfc_expr
*result
, gfc_expr
*e
)
6435 gcc_assert (e
->ts
.type
== BT_LOGICAL
&& e
->expr_type
== EXPR_CONSTANT
);
6436 gcc_assert (result
->ts
.type
== BT_LOGICAL
6437 && result
->expr_type
== EXPR_CONSTANT
);
6439 result
->value
.logical
= result
->value
.logical
!= e
->value
.logical
;
6445 gfc_simplify_is_contiguous (gfc_expr
*array
)
6447 if (gfc_is_simply_contiguous (array
, false, true))
6448 return gfc_get_logical_expr (gfc_default_logical_kind
, &array
->where
, 1);
6450 if (gfc_is_not_contiguous (array
))
6451 return gfc_get_logical_expr (gfc_default_logical_kind
, &array
->where
, 0);
6458 gfc_simplify_parity (gfc_expr
*e
, gfc_expr
*dim
)
6460 return simplify_transformation (e
, dim
, NULL
, 0, do_xor
);
6465 gfc_simplify_popcnt (gfc_expr
*e
)
6470 if (e
->expr_type
!= EXPR_CONSTANT
)
6473 k
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
6475 /* Convert argument to unsigned, then count the '1' bits. */
6476 mpz_init_set (x
, e
->value
.integer
);
6477 convert_mpz_to_unsigned (x
, gfc_integer_kinds
[k
].bit_size
);
6478 res
= mpz_popcount (x
);
6481 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, res
);
6486 gfc_simplify_poppar (gfc_expr
*e
)
6491 if (e
->expr_type
!= EXPR_CONSTANT
)
6494 popcnt
= gfc_simplify_popcnt (e
);
6495 gcc_assert (popcnt
);
6497 bool fail
= gfc_extract_int (popcnt
, &i
);
6500 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, i
% 2);
6505 gfc_simplify_precision (gfc_expr
*e
)
6507 int i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
6508 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
,
6509 gfc_real_kinds
[i
].precision
);
6514 gfc_simplify_product (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
)
6516 return simplify_transformation (array
, dim
, mask
, 1, gfc_multiply
);
6521 gfc_simplify_radix (gfc_expr
*e
)
6524 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
6529 i
= gfc_integer_kinds
[i
].radix
;
6533 i
= gfc_real_kinds
[i
].radix
;
6540 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, i
);
6545 gfc_simplify_range (gfc_expr
*e
)
6548 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
6553 i
= gfc_integer_kinds
[i
].range
;
6558 i
= gfc_real_kinds
[i
].range
;
6565 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, i
);
6570 gfc_simplify_rank (gfc_expr
*e
)
6576 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, e
->rank
);
6581 gfc_simplify_real (gfc_expr
*e
, gfc_expr
*k
)
6583 gfc_expr
*result
= NULL
;
6584 int kind
, tmp1
, tmp2
;
6586 /* Convert BOZ to real, and return without range checking. */
6587 if (e
->ts
.type
== BT_BOZ
)
6589 /* Determine kind for conversion of the BOZ. */
6591 gfc_extract_int (k
, &kind
);
6593 kind
= gfc_default_real_kind
;
6595 if (!gfc_boz2real (e
, kind
))
6597 result
= gfc_copy_expr (e
);
6601 if (e
->ts
.type
== BT_COMPLEX
)
6602 kind
= get_kind (BT_REAL
, k
, "REAL", e
->ts
.kind
);
6604 kind
= get_kind (BT_REAL
, k
, "REAL", gfc_default_real_kind
);
6607 return &gfc_bad_expr
;
6609 if (e
->expr_type
!= EXPR_CONSTANT
)
6612 /* For explicit conversion, turn off -Wconversion and -Wconversion-extra
6614 tmp1
= warn_conversion
;
6615 tmp2
= warn_conversion_extra
;
6616 warn_conversion
= warn_conversion_extra
= 0;
6618 result
= gfc_convert_constant (e
, BT_REAL
, kind
);
6620 warn_conversion
= tmp1
;
6621 warn_conversion_extra
= tmp2
;
6623 if (result
== &gfc_bad_expr
)
6624 return &gfc_bad_expr
;
6626 return range_check (result
, "REAL");
6631 gfc_simplify_realpart (gfc_expr
*e
)
6635 if (e
->expr_type
!= EXPR_CONSTANT
)
6638 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
6639 mpc_real (result
->value
.real
, e
->value
.complex, GFC_RND_MODE
);
6641 return range_check (result
, "REALPART");
6645 gfc_simplify_repeat (gfc_expr
*e
, gfc_expr
*n
)
6650 bool have_length
= false;
6652 /* If NCOPIES isn't a constant, there's nothing we can do. */
6653 if (n
->expr_type
!= EXPR_CONSTANT
)
6656 /* If NCOPIES is negative, it's an error. */
6657 if (mpz_sgn (n
->value
.integer
) < 0)
6659 gfc_error ("Argument NCOPIES of REPEAT intrinsic is negative at %L",
6661 return &gfc_bad_expr
;
6664 /* If we don't know the character length, we can do no more. */
6665 if (e
->ts
.u
.cl
&& e
->ts
.u
.cl
->length
6666 && e
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
6668 len
= gfc_mpz_get_hwi (e
->ts
.u
.cl
->length
->value
.integer
);
6671 else if (e
->expr_type
== EXPR_CONSTANT
6672 && (e
->ts
.u
.cl
== NULL
|| e
->ts
.u
.cl
->length
== NULL
))
6674 len
= e
->value
.character
.length
;
6679 /* If the source length is 0, any value of NCOPIES is valid
6680 and everything behaves as if NCOPIES == 0. */
6683 mpz_set_ui (ncopies
, 0);
6685 mpz_set (ncopies
, n
->value
.integer
);
6687 /* Check that NCOPIES isn't too large. */
6693 /* Compute the maximum value allowed for NCOPIES: huge(cl) / len. */
6695 i
= gfc_validate_kind (BT_INTEGER
, gfc_charlen_int_kind
, false);
6699 mpz_tdiv_q (max
, gfc_integer_kinds
[i
].huge
,
6700 e
->ts
.u
.cl
->length
->value
.integer
);
6705 gfc_mpz_set_hwi (mlen
, len
);
6706 mpz_tdiv_q (max
, gfc_integer_kinds
[i
].huge
, mlen
);
6710 /* The check itself. */
6711 if (mpz_cmp (ncopies
, max
) > 0)
6714 mpz_clear (ncopies
);
6715 gfc_error ("Argument NCOPIES of REPEAT intrinsic is too large at %L",
6717 return &gfc_bad_expr
;
6722 mpz_clear (ncopies
);
6724 /* For further simplification, we need the character string to be
6726 if (e
->expr_type
!= EXPR_CONSTANT
)
6731 (e
->ts
.u
.cl
->length
&&
6732 mpz_sgn (e
->ts
.u
.cl
->length
->value
.integer
) != 0))
6734 bool fail
= gfc_extract_hwi (n
, &ncop
);
6741 return gfc_get_character_expr (e
->ts
.kind
, &e
->where
, NULL
, 0);
6743 len
= e
->value
.character
.length
;
6744 gfc_charlen_t nlen
= ncop
* len
;
6746 /* Here's a semi-arbitrary limit. If the string is longer than 1 GB
6747 (2**28 elements * 4 bytes (wide chars) per element) defer to
6748 runtime instead of consuming (unbounded) memory and CPU at
6750 if (nlen
> 268435456)
6752 gfc_warning_now (0, "Evaluation of string longer than 2**28 at %L"
6753 " deferred to runtime, expect bugs", &e
->where
);
6757 result
= gfc_get_character_expr (e
->ts
.kind
, &e
->where
, NULL
, nlen
);
6758 for (size_t i
= 0; i
< (size_t) ncop
; i
++)
6759 for (size_t j
= 0; j
< (size_t) len
; j
++)
6760 result
->value
.character
.string
[j
+i
*len
]= e
->value
.character
.string
[j
];
6762 result
->value
.character
.string
[nlen
] = '\0'; /* For debugger */
6767 /* This one is a bear, but mainly has to do with shuffling elements. */
6770 gfc_simplify_reshape (gfc_expr
*source
, gfc_expr
*shape_exp
,
6771 gfc_expr
*pad
, gfc_expr
*order_exp
)
6773 int order
[GFC_MAX_DIMENSIONS
], shape
[GFC_MAX_DIMENSIONS
];
6774 int i
, rank
, npad
, x
[GFC_MAX_DIMENSIONS
];
6778 gfc_expr
*e
, *result
;
6779 bool zerosize
= false;
6781 /* Check that argument expression types are OK. */
6782 if (!is_constant_array_expr (source
)
6783 || !is_constant_array_expr (shape_exp
)
6784 || !is_constant_array_expr (pad
)
6785 || !is_constant_array_expr (order_exp
))
6788 if (source
->shape
== NULL
)
6791 /* Proceed with simplification, unpacking the array. */
6796 for (i
= 0; i
< GFC_MAX_DIMENSIONS
; i
++)
6801 e
= gfc_constructor_lookup_expr (shape_exp
->value
.constructor
, rank
);
6805 gfc_extract_int (e
, &shape
[rank
]);
6807 gcc_assert (rank
>= 0 && rank
< GFC_MAX_DIMENSIONS
);
6808 if (shape
[rank
] < 0)
6810 gfc_error ("The SHAPE array for the RESHAPE intrinsic at %L has a "
6811 "negative value %d for dimension %d",
6812 &shape_exp
->where
, shape
[rank
], rank
+1);
6813 return &gfc_bad_expr
;
6819 gcc_assert (rank
> 0);
6821 /* Now unpack the order array if present. */
6822 if (order_exp
== NULL
)
6824 for (i
= 0; i
< rank
; i
++)
6830 int order_size
, shape_size
;
6832 if (order_exp
->rank
!= shape_exp
->rank
)
6834 gfc_error ("Shapes of ORDER at %L and SHAPE at %L are different",
6835 &order_exp
->where
, &shape_exp
->where
);
6836 return &gfc_bad_expr
;
6839 gfc_array_size (shape_exp
, &size
);
6840 shape_size
= mpz_get_ui (size
);
6842 gfc_array_size (order_exp
, &size
);
6843 order_size
= mpz_get_ui (size
);
6845 if (order_size
!= shape_size
)
6847 gfc_error ("Sizes of ORDER at %L and SHAPE at %L are different",
6848 &order_exp
->where
, &shape_exp
->where
);
6849 return &gfc_bad_expr
;
6852 for (i
= 0; i
< rank
; i
++)
6854 e
= gfc_constructor_lookup_expr (order_exp
->value
.constructor
, i
);
6857 gfc_extract_int (e
, &order
[i
]);
6859 if (order
[i
] < 1 || order
[i
] > rank
)
6861 gfc_error ("Element with a value of %d in ORDER at %L must be "
6862 "in the range [1, ..., %d] for the RESHAPE intrinsic "
6863 "near %L", order
[i
], &order_exp
->where
, rank
,
6865 return &gfc_bad_expr
;
6869 if (x
[order
[i
]] != 0)
6871 gfc_error ("ORDER at %L is not a permutation of the size of "
6872 "SHAPE at %L", &order_exp
->where
, &shape_exp
->where
);
6873 return &gfc_bad_expr
;
6879 /* Count the elements in the source and padding arrays. */
6884 gfc_array_size (pad
, &size
);
6885 npad
= mpz_get_ui (size
);
6889 gfc_array_size (source
, &size
);
6890 nsource
= mpz_get_ui (size
);
6893 /* If it weren't for that pesky permutation we could just loop
6894 through the source and round out any shortage with pad elements.
6895 But no, someone just had to have the compiler do something the
6896 user should be doing. */
6898 for (i
= 0; i
< rank
; i
++)
6901 result
= gfc_get_array_expr (source
->ts
.type
, source
->ts
.kind
,
6903 if (source
->ts
.type
== BT_DERIVED
)
6904 result
->ts
.u
.derived
= source
->ts
.u
.derived
;
6905 if (source
->ts
.type
== BT_CHARACTER
&& result
->ts
.u
.cl
== NULL
)
6906 result
->ts
= source
->ts
;
6907 result
->rank
= rank
;
6908 result
->shape
= gfc_get_shape (rank
);
6909 for (i
= 0; i
< rank
; i
++)
6911 mpz_init_set_ui (result
->shape
[i
], shape
[i
]);
6919 while (nsource
> 0 || npad
> 0)
6921 /* Figure out which element to extract. */
6922 mpz_set_ui (index
, 0);
6924 for (i
= rank
- 1; i
>= 0; i
--)
6926 mpz_add_ui (index
, index
, x
[order
[i
]]);
6928 mpz_mul_ui (index
, index
, shape
[order
[i
- 1]]);
6931 if (mpz_cmp_ui (index
, INT_MAX
) > 0)
6932 gfc_internal_error ("Reshaped array too large at %C");
6934 j
= mpz_get_ui (index
);
6937 e
= gfc_constructor_lookup_expr (source
->value
.constructor
, j
);
6947 e
= gfc_constructor_lookup_expr (pad
->value
.constructor
, j
);
6951 gfc_constructor_append_expr (&result
->value
.constructor
,
6952 gfc_copy_expr (e
), &e
->where
);
6954 /* Calculate the next element. */
6958 if (++x
[i
] < shape
[i
])
6976 gfc_simplify_rrspacing (gfc_expr
*x
)
6982 if (x
->expr_type
!= EXPR_CONSTANT
)
6985 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
6987 result
= gfc_get_constant_expr (BT_REAL
, x
->ts
.kind
, &x
->where
);
6989 /* RRSPACING(+/- 0.0) = 0.0 */
6990 if (mpfr_zero_p (x
->value
.real
))
6992 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
6996 /* RRSPACING(inf) = NaN */
6997 if (mpfr_inf_p (x
->value
.real
))
6999 mpfr_set_nan (result
->value
.real
);
7003 /* RRSPACING(NaN) = same NaN */
7004 if (mpfr_nan_p (x
->value
.real
))
7006 mpfr_set (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
7010 /* | x * 2**(-e) | * 2**p. */
7011 mpfr_abs (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
7012 e
= - (long int) mpfr_get_exp (x
->value
.real
);
7013 mpfr_mul_2si (result
->value
.real
, result
->value
.real
, e
, GFC_RND_MODE
);
7015 p
= (long int) gfc_real_kinds
[i
].digits
;
7016 mpfr_mul_2si (result
->value
.real
, result
->value
.real
, p
, GFC_RND_MODE
);
7018 return range_check (result
, "RRSPACING");
7023 gfc_simplify_scale (gfc_expr
*x
, gfc_expr
*i
)
7025 int k
, neg_flag
, power
, exp_range
;
7026 mpfr_t scale
, radix
;
7029 if (x
->expr_type
!= EXPR_CONSTANT
|| i
->expr_type
!= EXPR_CONSTANT
)
7032 result
= gfc_get_constant_expr (BT_REAL
, x
->ts
.kind
, &x
->where
);
7034 if (mpfr_zero_p (x
->value
.real
))
7036 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
7040 k
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, false);
7042 exp_range
= gfc_real_kinds
[k
].max_exponent
- gfc_real_kinds
[k
].min_exponent
;
7044 /* This check filters out values of i that would overflow an int. */
7045 if (mpz_cmp_si (i
->value
.integer
, exp_range
+ 2) > 0
7046 || mpz_cmp_si (i
->value
.integer
, -exp_range
- 2) < 0)
7048 gfc_error ("Result of SCALE overflows its kind at %L", &result
->where
);
7049 gfc_free_expr (result
);
7050 return &gfc_bad_expr
;
7053 /* Compute scale = radix ** power. */
7054 power
= mpz_get_si (i
->value
.integer
);
7064 gfc_set_model_kind (x
->ts
.kind
);
7067 mpfr_set_ui (radix
, gfc_real_kinds
[k
].radix
, GFC_RND_MODE
);
7068 mpfr_pow_ui (scale
, radix
, power
, GFC_RND_MODE
);
7071 mpfr_div (result
->value
.real
, x
->value
.real
, scale
, GFC_RND_MODE
);
7073 mpfr_mul (result
->value
.real
, x
->value
.real
, scale
, GFC_RND_MODE
);
7075 mpfr_clears (scale
, radix
, NULL
);
7077 return range_check (result
, "SCALE");
7081 /* Variants of strspn and strcspn that operate on wide characters. */
7084 wide_strspn (const gfc_char_t
*s1
, const gfc_char_t
*s2
)
7087 const gfc_char_t
*c
;
7091 for (c
= s2
; *c
; c
++)
7105 wide_strcspn (const gfc_char_t
*s1
, const gfc_char_t
*s2
)
7108 const gfc_char_t
*c
;
7112 for (c
= s2
; *c
; c
++)
7127 gfc_simplify_scan (gfc_expr
*e
, gfc_expr
*c
, gfc_expr
*b
, gfc_expr
*kind
)
7132 size_t indx
, len
, lenc
;
7133 int k
= get_kind (BT_INTEGER
, kind
, "SCAN", gfc_default_integer_kind
);
7136 return &gfc_bad_expr
;
7138 if (e
->expr_type
!= EXPR_CONSTANT
|| c
->expr_type
!= EXPR_CONSTANT
7139 || ( b
!= NULL
&& b
->expr_type
!= EXPR_CONSTANT
))
7142 if (b
!= NULL
&& b
->value
.logical
!= 0)
7147 len
= e
->value
.character
.length
;
7148 lenc
= c
->value
.character
.length
;
7150 if (len
== 0 || lenc
== 0)
7158 indx
= wide_strcspn (e
->value
.character
.string
,
7159 c
->value
.character
.string
) + 1;
7164 for (indx
= len
; indx
> 0; indx
--)
7166 for (i
= 0; i
< lenc
; i
++)
7168 if (c
->value
.character
.string
[i
]
7169 == e
->value
.character
.string
[indx
- 1])
7177 result
= gfc_get_int_expr (k
, &e
->where
, indx
);
7178 return range_check (result
, "SCAN");
7183 gfc_simplify_selected_char_kind (gfc_expr
*e
)
7187 if (e
->expr_type
!= EXPR_CONSTANT
)
7190 if (gfc_compare_with_Cstring (e
, "ascii", false) == 0
7191 || gfc_compare_with_Cstring (e
, "default", false) == 0)
7193 else if (gfc_compare_with_Cstring (e
, "iso_10646", false) == 0)
7198 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, kind
);
7203 gfc_simplify_selected_int_kind (gfc_expr
*e
)
7207 if (e
->expr_type
!= EXPR_CONSTANT
|| gfc_extract_int (e
, &range
))
7212 for (i
= 0; gfc_integer_kinds
[i
].kind
!= 0; i
++)
7213 if (gfc_integer_kinds
[i
].range
>= range
7214 && gfc_integer_kinds
[i
].kind
< kind
)
7215 kind
= gfc_integer_kinds
[i
].kind
;
7217 if (kind
== INT_MAX
)
7220 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, kind
);
7225 gfc_simplify_selected_real_kind (gfc_expr
*p
, gfc_expr
*q
, gfc_expr
*rdx
)
7227 int range
, precision
, radix
, i
, kind
, found_precision
, found_range
,
7229 locus
*loc
= &gfc_current_locus
;
7235 if (p
->expr_type
!= EXPR_CONSTANT
7236 || gfc_extract_int (p
, &precision
))
7245 if (q
->expr_type
!= EXPR_CONSTANT
7246 || gfc_extract_int (q
, &range
))
7257 if (rdx
->expr_type
!= EXPR_CONSTANT
7258 || gfc_extract_int (rdx
, &radix
))
7266 found_precision
= 0;
7270 for (i
= 0; gfc_real_kinds
[i
].kind
!= 0; i
++)
7272 if (gfc_real_kinds
[i
].precision
>= precision
)
7273 found_precision
= 1;
7275 if (gfc_real_kinds
[i
].range
>= range
)
7278 if (radix
== 0 || gfc_real_kinds
[i
].radix
== radix
)
7281 if (gfc_real_kinds
[i
].precision
>= precision
7282 && gfc_real_kinds
[i
].range
>= range
7283 && (radix
== 0 || gfc_real_kinds
[i
].radix
== radix
)
7284 && gfc_real_kinds
[i
].kind
< kind
)
7285 kind
= gfc_real_kinds
[i
].kind
;
7288 if (kind
== INT_MAX
)
7290 if (found_radix
&& found_range
&& !found_precision
)
7292 else if (found_radix
&& found_precision
&& !found_range
)
7294 else if (found_radix
&& !found_precision
&& !found_range
)
7296 else if (found_radix
)
7302 return gfc_get_int_expr (gfc_default_integer_kind
, loc
, kind
);
7307 gfc_simplify_set_exponent (gfc_expr
*x
, gfc_expr
*i
)
7310 mpfr_t exp
, absv
, log2
, pow2
, frac
;
7313 if (x
->expr_type
!= EXPR_CONSTANT
|| i
->expr_type
!= EXPR_CONSTANT
)
7316 result
= gfc_get_constant_expr (BT_REAL
, x
->ts
.kind
, &x
->where
);
7318 /* SET_EXPONENT (+/-0.0, I) = +/- 0.0
7319 SET_EXPONENT (NaN) = same NaN */
7320 if (mpfr_zero_p (x
->value
.real
) || mpfr_nan_p (x
->value
.real
))
7322 mpfr_set (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
7326 /* SET_EXPONENT (inf) = NaN */
7327 if (mpfr_inf_p (x
->value
.real
))
7329 mpfr_set_nan (result
->value
.real
);
7333 gfc_set_model_kind (x
->ts
.kind
);
7340 mpfr_abs (absv
, x
->value
.real
, GFC_RND_MODE
);
7341 mpfr_log2 (log2
, absv
, GFC_RND_MODE
);
7343 mpfr_floor (log2
, log2
);
7344 mpfr_add_ui (exp
, log2
, 1, GFC_RND_MODE
);
7346 /* Old exponent value, and fraction. */
7347 mpfr_ui_pow (pow2
, 2, exp
, GFC_RND_MODE
);
7349 mpfr_div (frac
, x
->value
.real
, pow2
, GFC_RND_MODE
);
7352 exp2
= mpz_get_si (i
->value
.integer
);
7353 mpfr_mul_2si (result
->value
.real
, frac
, exp2
, GFC_RND_MODE
);
7355 mpfr_clears (absv
, log2
, exp
, pow2
, frac
, NULL
);
7357 return range_check (result
, "SET_EXPONENT");
7362 gfc_simplify_shape (gfc_expr
*source
, gfc_expr
*kind
)
7364 mpz_t shape
[GFC_MAX_DIMENSIONS
];
7365 gfc_expr
*result
, *e
, *f
;
7369 int k
= get_kind (BT_INTEGER
, kind
, "SHAPE", gfc_default_integer_kind
);
7371 if (source
->rank
== -1)
7374 result
= gfc_get_array_expr (BT_INTEGER
, k
, &source
->where
);
7375 result
->shape
= gfc_get_shape (1);
7376 mpz_init (result
->shape
[0]);
7378 if (source
->rank
== 0)
7381 if (source
->expr_type
== EXPR_VARIABLE
)
7383 ar
= gfc_find_array_ref (source
);
7384 t
= gfc_array_ref_shape (ar
, shape
);
7386 else if (source
->shape
)
7389 for (n
= 0; n
< source
->rank
; n
++)
7391 mpz_init (shape
[n
]);
7392 mpz_set (shape
[n
], source
->shape
[n
]);
7398 for (n
= 0; n
< source
->rank
; n
++)
7400 e
= gfc_get_constant_expr (BT_INTEGER
, k
, &source
->where
);
7403 mpz_set (e
->value
.integer
, shape
[n
]);
7406 mpz_set_ui (e
->value
.integer
, n
+ 1);
7408 f
= simplify_size (source
, e
, k
);
7412 gfc_free_expr (result
);
7419 if (e
== &gfc_bad_expr
|| range_check (e
, "SHAPE") == &gfc_bad_expr
)
7421 gfc_free_expr (result
);
7423 gfc_clear_shape (shape
, source
->rank
);
7424 return &gfc_bad_expr
;
7427 gfc_constructor_append_expr (&result
->value
.constructor
, e
, NULL
);
7431 gfc_clear_shape (shape
, source
->rank
);
7433 mpz_set_si (result
->shape
[0], source
->rank
);
7440 simplify_size (gfc_expr
*array
, gfc_expr
*dim
, int k
)
7443 gfc_expr
*return_value
;
7447 /* For unary operations, the size of the result is given by the size
7448 of the operand. For binary ones, it's the size of the first operand
7449 unless it is scalar, then it is the size of the second. */
7450 if (array
->expr_type
== EXPR_OP
&& !array
->value
.op
.uop
)
7452 gfc_expr
* replacement
;
7453 gfc_expr
* simplified
;
7455 switch (array
->value
.op
.op
)
7457 /* Unary operations. */
7459 case INTRINSIC_UPLUS
:
7460 case INTRINSIC_UMINUS
:
7461 case INTRINSIC_PARENTHESES
:
7462 replacement
= array
->value
.op
.op1
;
7465 /* Binary operations. If any one of the operands is scalar, take
7466 the other one's size. If both of them are arrays, it does not
7467 matter -- try to find one with known shape, if possible. */
7469 if (array
->value
.op
.op1
->rank
== 0)
7470 replacement
= array
->value
.op
.op2
;
7471 else if (array
->value
.op
.op2
->rank
== 0)
7472 replacement
= array
->value
.op
.op1
;
7475 simplified
= simplify_size (array
->value
.op
.op1
, dim
, k
);
7479 replacement
= array
->value
.op
.op2
;
7484 /* Try to reduce it directly if possible. */
7485 simplified
= simplify_size (replacement
, dim
, k
);
7487 /* Otherwise, we build a new SIZE call. This is hopefully at least
7488 simpler than the original one. */
7491 gfc_expr
*kind
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, k
);
7492 simplified
= gfc_build_intrinsic_call (gfc_current_ns
,
7493 GFC_ISYM_SIZE
, "size",
7495 gfc_copy_expr (replacement
),
7496 gfc_copy_expr (dim
),
7502 for (ref
= array
->ref
; ref
; ref
= ref
->next
)
7503 if (ref
->type
== REF_ARRAY
&& ref
->u
.ar
.as
7504 && !gfc_resolve_array_spec (ref
->u
.ar
.as
, 0))
7509 if (!gfc_array_size (array
, &size
))
7514 if (dim
->expr_type
!= EXPR_CONSTANT
)
7517 d
= mpz_get_ui (dim
->value
.integer
) - 1;
7518 if (!gfc_array_dimen_size (array
, d
, &size
))
7522 return_value
= gfc_get_constant_expr (BT_INTEGER
, k
, &array
->where
);
7523 mpz_set (return_value
->value
.integer
, size
);
7526 return return_value
;
7531 gfc_simplify_size (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
)
7534 int k
= get_kind (BT_INTEGER
, kind
, "SIZE", gfc_default_integer_kind
);
7537 return &gfc_bad_expr
;
7539 result
= simplify_size (array
, dim
, k
);
7540 if (result
== NULL
|| result
== &gfc_bad_expr
)
7543 return range_check (result
, "SIZE");
7547 /* SIZEOF and C_SIZEOF return the size in bytes of an array element
7548 multiplied by the array size. */
7551 gfc_simplify_sizeof (gfc_expr
*x
)
7553 gfc_expr
*result
= NULL
;
7557 if (x
->ts
.type
== BT_CLASS
|| x
->ts
.deferred
)
7560 if (x
->ts
.type
== BT_CHARACTER
7561 && (!x
->ts
.u
.cl
|| !x
->ts
.u
.cl
->length
7562 || x
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
))
7565 if (x
->rank
&& x
->expr_type
!= EXPR_ARRAY
7566 && !gfc_array_size (x
, &array_size
))
7569 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_index_integer_kind
,
7571 gfc_target_expr_size (x
, &res_size
);
7572 mpz_set_si (result
->value
.integer
, res_size
);
7578 /* STORAGE_SIZE returns the size in bits of a single array element. */
7581 gfc_simplify_storage_size (gfc_expr
*x
,
7584 gfc_expr
*result
= NULL
;
7588 if (x
->ts
.type
== BT_CLASS
|| x
->ts
.deferred
)
7591 if (x
->ts
.type
== BT_CHARACTER
&& x
->expr_type
!= EXPR_CONSTANT
7592 && (!x
->ts
.u
.cl
|| !x
->ts
.u
.cl
->length
7593 || x
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
))
7596 k
= get_kind (BT_INTEGER
, kind
, "STORAGE_SIZE", gfc_default_integer_kind
);
7598 return &gfc_bad_expr
;
7600 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &x
->where
);
7602 gfc_element_size (x
, &siz
);
7603 mpz_set_si (result
->value
.integer
, siz
);
7604 mpz_mul_ui (result
->value
.integer
, result
->value
.integer
, BITS_PER_UNIT
);
7606 return range_check (result
, "STORAGE_SIZE");
7611 gfc_simplify_sign (gfc_expr
*x
, gfc_expr
*y
)
7615 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
7618 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
7623 mpz_abs (result
->value
.integer
, x
->value
.integer
);
7624 if (mpz_sgn (y
->value
.integer
) < 0)
7625 mpz_neg (result
->value
.integer
, result
->value
.integer
);
7630 mpfr_copysign (result
->value
.real
, x
->value
.real
, y
->value
.real
,
7633 mpfr_setsign (result
->value
.real
, x
->value
.real
,
7634 mpfr_sgn (y
->value
.real
) < 0 ? 1 : 0, GFC_RND_MODE
);
7638 gfc_internal_error ("Bad type in gfc_simplify_sign");
7646 gfc_simplify_sin (gfc_expr
*x
)
7650 if (x
->expr_type
!= EXPR_CONSTANT
)
7653 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
7658 mpfr_sin (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
7662 gfc_set_model (x
->value
.real
);
7663 mpc_sin (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
7667 gfc_internal_error ("in gfc_simplify_sin(): Bad type");
7670 return range_check (result
, "SIN");
7675 gfc_simplify_sinh (gfc_expr
*x
)
7679 if (x
->expr_type
!= EXPR_CONSTANT
)
7682 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
7687 mpfr_sinh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
7691 mpc_sinh (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
7698 return range_check (result
, "SINH");
7702 /* The argument is always a double precision real that is converted to
7703 single precision. TODO: Rounding! */
7706 gfc_simplify_sngl (gfc_expr
*a
)
7711 if (a
->expr_type
!= EXPR_CONSTANT
)
7714 /* For explicit conversion, turn off -Wconversion and -Wconversion-extra
7716 tmp1
= warn_conversion
;
7717 tmp2
= warn_conversion_extra
;
7718 warn_conversion
= warn_conversion_extra
= 0;
7720 result
= gfc_real2real (a
, gfc_default_real_kind
);
7722 warn_conversion
= tmp1
;
7723 warn_conversion_extra
= tmp2
;
7725 return range_check (result
, "SNGL");
7730 gfc_simplify_spacing (gfc_expr
*x
)
7736 if (x
->expr_type
!= EXPR_CONSTANT
)
7739 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
7740 result
= gfc_get_constant_expr (BT_REAL
, x
->ts
.kind
, &x
->where
);
7742 /* SPACING(+/- 0.0) = SPACING(TINY(0.0)) = TINY(0.0) */
7743 if (mpfr_zero_p (x
->value
.real
))
7745 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].tiny
, GFC_RND_MODE
);
7749 /* SPACING(inf) = NaN */
7750 if (mpfr_inf_p (x
->value
.real
))
7752 mpfr_set_nan (result
->value
.real
);
7756 /* SPACING(NaN) = same NaN */
7757 if (mpfr_nan_p (x
->value
.real
))
7759 mpfr_set (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
7763 /* In the Fortran 95 standard, the result is b**(e - p) where b, e, and p
7764 are the radix, exponent of x, and precision. This excludes the
7765 possibility of subnormal numbers. Fortran 2003 states the result is
7766 b**max(e - p, emin - 1). */
7768 ep
= (long int) mpfr_get_exp (x
->value
.real
) - gfc_real_kinds
[i
].digits
;
7769 en
= (long int) gfc_real_kinds
[i
].min_exponent
- 1;
7770 en
= en
> ep
? en
: ep
;
7772 mpfr_set_ui (result
->value
.real
, 1, GFC_RND_MODE
);
7773 mpfr_mul_2si (result
->value
.real
, result
->value
.real
, en
, GFC_RND_MODE
);
7775 return range_check (result
, "SPACING");
7780 gfc_simplify_spread (gfc_expr
*source
, gfc_expr
*dim_expr
, gfc_expr
*ncopies_expr
)
7782 gfc_expr
*result
= NULL
;
7783 int nelem
, i
, j
, dim
, ncopies
;
7786 if ((!gfc_is_constant_expr (source
)
7787 && !is_constant_array_expr (source
))
7788 || !gfc_is_constant_expr (dim_expr
)
7789 || !gfc_is_constant_expr (ncopies_expr
))
7792 gcc_assert (dim_expr
->ts
.type
== BT_INTEGER
);
7793 gfc_extract_int (dim_expr
, &dim
);
7794 dim
-= 1; /* zero-base DIM */
7796 gcc_assert (ncopies_expr
->ts
.type
== BT_INTEGER
);
7797 gfc_extract_int (ncopies_expr
, &ncopies
);
7798 ncopies
= MAX (ncopies
, 0);
7800 /* Do not allow the array size to exceed the limit for an array
7802 if (source
->expr_type
== EXPR_ARRAY
)
7804 if (!gfc_array_size (source
, &size
))
7805 gfc_internal_error ("Failure getting length of a constant array.");
7808 mpz_init_set_ui (size
, 1);
7810 nelem
= mpz_get_si (size
) * ncopies
;
7811 if (nelem
> flag_max_array_constructor
)
7813 if (gfc_init_expr_flag
)
7815 gfc_error ("The number of elements (%d) in the array constructor "
7816 "at %L requires an increase of the allowed %d upper "
7817 "limit. See %<-fmax-array-constructor%> option.",
7818 nelem
, &source
->where
, flag_max_array_constructor
);
7819 return &gfc_bad_expr
;
7825 if (source
->expr_type
== EXPR_CONSTANT
7826 || source
->expr_type
== EXPR_STRUCTURE
)
7828 gcc_assert (dim
== 0);
7830 result
= gfc_get_array_expr (source
->ts
.type
, source
->ts
.kind
,
7832 if (source
->ts
.type
== BT_DERIVED
)
7833 result
->ts
.u
.derived
= source
->ts
.u
.derived
;
7835 result
->shape
= gfc_get_shape (result
->rank
);
7836 mpz_init_set_si (result
->shape
[0], ncopies
);
7838 for (i
= 0; i
< ncopies
; ++i
)
7839 gfc_constructor_append_expr (&result
->value
.constructor
,
7840 gfc_copy_expr (source
), NULL
);
7842 else if (source
->expr_type
== EXPR_ARRAY
)
7844 int offset
, rstride
[GFC_MAX_DIMENSIONS
], extent
[GFC_MAX_DIMENSIONS
];
7845 gfc_constructor
*source_ctor
;
7847 gcc_assert (source
->rank
< GFC_MAX_DIMENSIONS
);
7848 gcc_assert (dim
>= 0 && dim
<= source
->rank
);
7850 result
= gfc_get_array_expr (source
->ts
.type
, source
->ts
.kind
,
7852 if (source
->ts
.type
== BT_DERIVED
)
7853 result
->ts
.u
.derived
= source
->ts
.u
.derived
;
7854 result
->rank
= source
->rank
+ 1;
7855 result
->shape
= gfc_get_shape (result
->rank
);
7857 for (i
= 0, j
= 0; i
< result
->rank
; ++i
)
7860 mpz_init_set (result
->shape
[i
], source
->shape
[j
++]);
7862 mpz_init_set_si (result
->shape
[i
], ncopies
);
7864 extent
[i
] = mpz_get_si (result
->shape
[i
]);
7865 rstride
[i
] = (i
== 0) ? 1 : rstride
[i
-1] * extent
[i
-1];
7869 for (source_ctor
= gfc_constructor_first (source
->value
.constructor
);
7870 source_ctor
; source_ctor
= gfc_constructor_next (source_ctor
))
7872 for (i
= 0; i
< ncopies
; ++i
)
7873 gfc_constructor_insert_expr (&result
->value
.constructor
,
7874 gfc_copy_expr (source_ctor
->expr
),
7875 NULL
, offset
+ i
* rstride
[dim
]);
7877 offset
+= (dim
== 0 ? ncopies
: 1);
7882 gfc_error ("Simplification of SPREAD at %C not yet implemented");
7883 return &gfc_bad_expr
;
7886 if (source
->ts
.type
== BT_CHARACTER
)
7887 result
->ts
.u
.cl
= source
->ts
.u
.cl
;
7894 gfc_simplify_sqrt (gfc_expr
*e
)
7896 gfc_expr
*result
= NULL
;
7898 if (e
->expr_type
!= EXPR_CONSTANT
)
7904 if (mpfr_cmp_si (e
->value
.real
, 0) < 0)
7906 gfc_error ("Argument of SQRT at %L has a negative value",
7908 return &gfc_bad_expr
;
7910 result
= gfc_get_constant_expr (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
7911 mpfr_sqrt (result
->value
.real
, e
->value
.real
, GFC_RND_MODE
);
7915 gfc_set_model (e
->value
.real
);
7917 result
= gfc_get_constant_expr (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
7918 mpc_sqrt (result
->value
.complex, e
->value
.complex, GFC_MPC_RND_MODE
);
7922 gfc_internal_error ("invalid argument of SQRT at %L", &e
->where
);
7925 return range_check (result
, "SQRT");
7930 gfc_simplify_sum (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
)
7932 return simplify_transformation (array
, dim
, mask
, 0, gfc_add
);
7936 /* Simplify COTAN(X) where X has the unit of radian. */
7939 gfc_simplify_cotan (gfc_expr
*x
)
7944 if (x
->expr_type
!= EXPR_CONSTANT
)
7947 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
7952 mpfr_cot (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
7956 /* There is no builtin mpc_cot, so compute cot = cos / sin. */
7957 val
= &result
->value
.complex;
7958 mpc_init2 (swp
, mpfr_get_default_prec ());
7959 mpc_sin_cos (*val
, swp
, x
->value
.complex, GFC_MPC_RND_MODE
,
7961 mpc_div (*val
, swp
, *val
, GFC_MPC_RND_MODE
);
7969 return range_check (result
, "COTAN");
7974 gfc_simplify_tan (gfc_expr
*x
)
7978 if (x
->expr_type
!= EXPR_CONSTANT
)
7981 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
7986 mpfr_tan (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
7990 mpc_tan (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
7997 return range_check (result
, "TAN");
8002 gfc_simplify_tanh (gfc_expr
*x
)
8006 if (x
->expr_type
!= EXPR_CONSTANT
)
8009 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
8014 mpfr_tanh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
8018 mpc_tanh (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
8025 return range_check (result
, "TANH");
8030 gfc_simplify_tiny (gfc_expr
*e
)
8035 i
= gfc_validate_kind (BT_REAL
, e
->ts
.kind
, false);
8037 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
8038 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].tiny
, GFC_RND_MODE
);
8045 gfc_simplify_trailz (gfc_expr
*e
)
8047 unsigned long tz
, bs
;
8050 if (e
->expr_type
!= EXPR_CONSTANT
)
8053 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
8054 bs
= gfc_integer_kinds
[i
].bit_size
;
8055 tz
= mpz_scan1 (e
->value
.integer
, 0);
8057 return gfc_get_int_expr (gfc_default_integer_kind
,
8058 &e
->where
, MIN (tz
, bs
));
8063 gfc_simplify_transfer (gfc_expr
*source
, gfc_expr
*mold
, gfc_expr
*size
)
8066 gfc_expr
*mold_element
;
8071 unsigned char *buffer
;
8072 size_t result_length
;
8074 if (!gfc_is_constant_expr (source
) || !gfc_is_constant_expr (size
))
8077 if (!gfc_resolve_expr (mold
))
8079 if (gfc_init_expr_flag
&& !gfc_is_constant_expr (mold
))
8082 if (!gfc_calculate_transfer_sizes (source
, mold
, size
, &source_size
,
8083 &result_size
, &result_length
))
8086 /* Calculate the size of the source. */
8087 if (source
->expr_type
== EXPR_ARRAY
&& !gfc_array_size (source
, &tmp
))
8088 gfc_internal_error ("Failure getting length of a constant array.");
8090 /* Create an empty new expression with the appropriate characteristics. */
8091 result
= gfc_get_constant_expr (mold
->ts
.type
, mold
->ts
.kind
,
8093 result
->ts
= mold
->ts
;
8095 mold_element
= (mold
->expr_type
== EXPR_ARRAY
&& mold
->value
.constructor
)
8096 ? gfc_constructor_first (mold
->value
.constructor
)->expr
8099 /* Set result character length, if needed. Note that this needs to be
8100 set even for array expressions, in order to pass this information into
8101 gfc_target_interpret_expr. */
8102 if (result
->ts
.type
== BT_CHARACTER
&& gfc_is_constant_expr (mold_element
))
8104 result
->value
.character
.length
= mold_element
->value
.character
.length
;
8106 /* Let the typespec of the result inherit the string length.
8107 This is crucial if a resulting array has size zero. */
8108 if (mold_element
->ts
.u
.cl
->length
)
8109 result
->ts
.u
.cl
->length
= gfc_copy_expr (mold_element
->ts
.u
.cl
->length
);
8111 result
->ts
.u
.cl
->length
=
8112 gfc_get_int_expr (gfc_charlen_int_kind
, NULL
,
8113 mold_element
->value
.character
.length
);
8116 /* Set the number of elements in the result, and determine its size. */
8118 if (mold
->expr_type
== EXPR_ARRAY
|| mold
->rank
|| size
)
8120 result
->expr_type
= EXPR_ARRAY
;
8122 result
->shape
= gfc_get_shape (1);
8123 mpz_init_set_ui (result
->shape
[0], result_length
);
8128 /* Allocate the buffer to store the binary version of the source. */
8129 buffer_size
= MAX (source_size
, result_size
);
8130 buffer
= (unsigned char*)alloca (buffer_size
);
8131 memset (buffer
, 0, buffer_size
);
8133 /* Now write source to the buffer. */
8134 gfc_target_encode_expr (source
, buffer
, buffer_size
);
8136 /* And read the buffer back into the new expression. */
8137 gfc_target_interpret_expr (buffer
, buffer_size
, result
, false);
8144 gfc_simplify_transpose (gfc_expr
*matrix
)
8146 int row
, matrix_rows
, col
, matrix_cols
;
8149 if (!is_constant_array_expr (matrix
))
8152 gcc_assert (matrix
->rank
== 2);
8154 if (matrix
->shape
== NULL
)
8157 result
= gfc_get_array_expr (matrix
->ts
.type
, matrix
->ts
.kind
,
8160 result
->shape
= gfc_get_shape (result
->rank
);
8161 mpz_init_set (result
->shape
[0], matrix
->shape
[1]);
8162 mpz_init_set (result
->shape
[1], matrix
->shape
[0]);
8164 if (matrix
->ts
.type
== BT_CHARACTER
)
8165 result
->ts
.u
.cl
= matrix
->ts
.u
.cl
;
8166 else if (matrix
->ts
.type
== BT_DERIVED
)
8167 result
->ts
.u
.derived
= matrix
->ts
.u
.derived
;
8169 matrix_rows
= mpz_get_si (matrix
->shape
[0]);
8170 matrix_cols
= mpz_get_si (matrix
->shape
[1]);
8171 for (row
= 0; row
< matrix_rows
; ++row
)
8172 for (col
= 0; col
< matrix_cols
; ++col
)
8174 gfc_expr
*e
= gfc_constructor_lookup_expr (matrix
->value
.constructor
,
8175 col
* matrix_rows
+ row
);
8176 gfc_constructor_insert_expr (&result
->value
.constructor
,
8177 gfc_copy_expr (e
), &matrix
->where
,
8178 row
* matrix_cols
+ col
);
8186 gfc_simplify_trim (gfc_expr
*e
)
8189 int count
, i
, len
, lentrim
;
8191 if (e
->expr_type
!= EXPR_CONSTANT
)
8194 len
= e
->value
.character
.length
;
8195 for (count
= 0, i
= 1; i
<= len
; ++i
)
8197 if (e
->value
.character
.string
[len
- i
] == ' ')
8203 lentrim
= len
- count
;
8205 result
= gfc_get_character_expr (e
->ts
.kind
, &e
->where
, NULL
, lentrim
);
8206 for (i
= 0; i
< lentrim
; i
++)
8207 result
->value
.character
.string
[i
] = e
->value
.character
.string
[i
];
8214 gfc_simplify_image_index (gfc_expr
*coarray
, gfc_expr
*sub
)
8219 gfc_constructor
*sub_cons
;
8223 if (!is_constant_array_expr (sub
))
8226 /* Follow any component references. */
8227 as
= coarray
->symtree
->n
.sym
->as
;
8228 for (ref
= coarray
->ref
; ref
; ref
= ref
->next
)
8229 if (ref
->type
== REF_COMPONENT
)
8232 if (as
->type
== AS_DEFERRED
)
8235 /* "valid sequence of cosubscripts" are required; thus, return 0 unless
8236 the cosubscript addresses the first image. */
8238 sub_cons
= gfc_constructor_first (sub
->value
.constructor
);
8241 for (d
= 1; d
<= as
->corank
; d
++)
8246 gcc_assert (sub_cons
!= NULL
);
8248 ca_bound
= simplify_bound_dim (coarray
, NULL
, d
+ as
->rank
, 0, as
,
8250 if (ca_bound
== NULL
)
8253 if (ca_bound
== &gfc_bad_expr
)
8256 cmp
= mpz_cmp (ca_bound
->value
.integer
, sub_cons
->expr
->value
.integer
);
8260 gfc_free_expr (ca_bound
);
8261 sub_cons
= gfc_constructor_next (sub_cons
);
8265 first_image
= false;
8269 gfc_error ("Out of bounds in IMAGE_INDEX at %L for dimension %d, "
8270 "SUB has %ld and COARRAY lower bound is %ld)",
8272 mpz_get_si (sub_cons
->expr
->value
.integer
),
8273 mpz_get_si (ca_bound
->value
.integer
));
8274 gfc_free_expr (ca_bound
);
8275 return &gfc_bad_expr
;
8278 gfc_free_expr (ca_bound
);
8280 /* Check whether upperbound is valid for the multi-images case. */
8283 ca_bound
= simplify_bound_dim (coarray
, NULL
, d
+ as
->rank
, 1, as
,
8285 if (ca_bound
== &gfc_bad_expr
)
8288 if (ca_bound
&& ca_bound
->expr_type
== EXPR_CONSTANT
8289 && mpz_cmp (ca_bound
->value
.integer
,
8290 sub_cons
->expr
->value
.integer
) < 0)
8292 gfc_error ("Out of bounds in IMAGE_INDEX at %L for dimension %d, "
8293 "SUB has %ld and COARRAY upper bound is %ld)",
8295 mpz_get_si (sub_cons
->expr
->value
.integer
),
8296 mpz_get_si (ca_bound
->value
.integer
));
8297 gfc_free_expr (ca_bound
);
8298 return &gfc_bad_expr
;
8302 gfc_free_expr (ca_bound
);
8305 sub_cons
= gfc_constructor_next (sub_cons
);
8308 gcc_assert (sub_cons
== NULL
);
8310 if (flag_coarray
!= GFC_FCOARRAY_SINGLE
&& !first_image
)
8313 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
8314 &gfc_current_locus
);
8316 mpz_set_si (result
->value
.integer
, 1);
8318 mpz_set_si (result
->value
.integer
, 0);
8324 gfc_simplify_image_status (gfc_expr
*image
, gfc_expr
*team ATTRIBUTE_UNUSED
)
8326 if (flag_coarray
== GFC_FCOARRAY_NONE
)
8328 gfc_current_locus
= *gfc_current_intrinsic_where
;
8329 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
8330 return &gfc_bad_expr
;
8333 /* Simplification is possible for fcoarray = single only. For all other modes
8334 the result depends on runtime conditions. */
8335 if (flag_coarray
!= GFC_FCOARRAY_SINGLE
)
8338 if (gfc_is_constant_expr (image
))
8341 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
8343 if (mpz_get_si (image
->value
.integer
) == 1)
8344 mpz_set_si (result
->value
.integer
, 0);
8346 mpz_set_si (result
->value
.integer
, GFC_STAT_STOPPED_IMAGE
);
8355 gfc_simplify_this_image (gfc_expr
*coarray
, gfc_expr
*dim
,
8356 gfc_expr
*distance ATTRIBUTE_UNUSED
)
8358 if (flag_coarray
!= GFC_FCOARRAY_SINGLE
)
8361 /* If no coarray argument has been passed or when the first argument
8362 is actually a distance argument. */
8363 if (coarray
== NULL
|| !gfc_is_coarray (coarray
))
8366 /* FIXME: gfc_current_locus is wrong. */
8367 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
8368 &gfc_current_locus
);
8369 mpz_set_si (result
->value
.integer
, 1);
8373 /* For -fcoarray=single, this_image(A) is the same as lcobound(A). */
8374 return simplify_cobound (coarray
, dim
, NULL
, 0);
8379 gfc_simplify_ubound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
)
8381 return simplify_bound (array
, dim
, kind
, 1);
8385 gfc_simplify_ucobound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
)
8387 return simplify_cobound (array
, dim
, kind
, 1);
8392 gfc_simplify_unpack (gfc_expr
*vector
, gfc_expr
*mask
, gfc_expr
*field
)
8394 gfc_expr
*result
, *e
;
8395 gfc_constructor
*vector_ctor
, *mask_ctor
, *field_ctor
;
8397 if (!is_constant_array_expr (vector
)
8398 || !is_constant_array_expr (mask
)
8399 || (!gfc_is_constant_expr (field
)
8400 && !is_constant_array_expr (field
)))
8403 result
= gfc_get_array_expr (vector
->ts
.type
, vector
->ts
.kind
,
8405 if (vector
->ts
.type
== BT_DERIVED
)
8406 result
->ts
.u
.derived
= vector
->ts
.u
.derived
;
8407 result
->rank
= mask
->rank
;
8408 result
->shape
= gfc_copy_shape (mask
->shape
, mask
->rank
);
8410 if (vector
->ts
.type
== BT_CHARACTER
)
8411 result
->ts
.u
.cl
= vector
->ts
.u
.cl
;
8413 vector_ctor
= gfc_constructor_first (vector
->value
.constructor
);
8414 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
8416 = field
->expr_type
== EXPR_ARRAY
8417 ? gfc_constructor_first (field
->value
.constructor
)
8422 if (mask_ctor
->expr
->value
.logical
)
8426 e
= gfc_copy_expr (vector_ctor
->expr
);
8427 vector_ctor
= gfc_constructor_next (vector_ctor
);
8431 gfc_free_expr (result
);
8435 else if (field
->expr_type
== EXPR_ARRAY
)
8436 e
= gfc_copy_expr (field_ctor
->expr
);
8438 e
= gfc_copy_expr (field
);
8440 gfc_constructor_append_expr (&result
->value
.constructor
, e
, NULL
);
8442 mask_ctor
= gfc_constructor_next (mask_ctor
);
8443 field_ctor
= gfc_constructor_next (field_ctor
);
8451 gfc_simplify_verify (gfc_expr
*s
, gfc_expr
*set
, gfc_expr
*b
, gfc_expr
*kind
)
8455 size_t index
, len
, lenset
;
8457 int k
= get_kind (BT_INTEGER
, kind
, "VERIFY", gfc_default_integer_kind
);
8460 return &gfc_bad_expr
;
8462 if (s
->expr_type
!= EXPR_CONSTANT
|| set
->expr_type
!= EXPR_CONSTANT
8463 || ( b
!= NULL
&& b
->expr_type
!= EXPR_CONSTANT
))
8466 if (b
!= NULL
&& b
->value
.logical
!= 0)
8471 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &s
->where
);
8473 len
= s
->value
.character
.length
;
8474 lenset
= set
->value
.character
.length
;
8478 mpz_set_ui (result
->value
.integer
, 0);
8486 mpz_set_ui (result
->value
.integer
, 1);
8490 index
= wide_strspn (s
->value
.character
.string
,
8491 set
->value
.character
.string
) + 1;
8500 mpz_set_ui (result
->value
.integer
, len
);
8503 for (index
= len
; index
> 0; index
--)
8505 for (i
= 0; i
< lenset
; i
++)
8507 if (s
->value
.character
.string
[index
- 1]
8508 == set
->value
.character
.string
[i
])
8516 mpz_set_ui (result
->value
.integer
, index
);
8522 gfc_simplify_xor (gfc_expr
*x
, gfc_expr
*y
)
8527 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
8530 kind
= x
->ts
.kind
> y
->ts
.kind
? x
->ts
.kind
: y
->ts
.kind
;
8535 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &x
->where
);
8536 mpz_xor (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
8537 return range_check (result
, "XOR");
8540 return gfc_get_logical_expr (kind
, &x
->where
,
8541 (x
->value
.logical
&& !y
->value
.logical
)
8542 || (!x
->value
.logical
&& y
->value
.logical
));
8550 /****************** Constant simplification *****************/
8552 /* Master function to convert one constant to another. While this is
8553 used as a simplification function, it requires the destination type
8554 and kind information which is supplied by a special case in
8558 gfc_convert_constant (gfc_expr
*e
, bt type
, int kind
)
8560 gfc_expr
*result
, *(*f
) (gfc_expr
*, int);
8561 gfc_constructor
*c
, *t
;
8575 f
= gfc_int2complex
;
8595 f
= gfc_real2complex
;
8606 f
= gfc_complex2int
;
8609 f
= gfc_complex2real
;
8612 f
= gfc_complex2complex
;
8638 f
= gfc_hollerith2int
;
8642 f
= gfc_hollerith2real
;
8646 f
= gfc_hollerith2complex
;
8650 f
= gfc_hollerith2character
;
8654 f
= gfc_hollerith2logical
;
8666 f
= gfc_character2int
;
8670 f
= gfc_character2real
;
8674 f
= gfc_character2complex
;
8678 f
= gfc_character2character
;
8682 f
= gfc_character2logical
;
8692 return &gfc_bad_expr
;
8697 switch (e
->expr_type
)
8700 result
= f (e
, kind
);
8702 return &gfc_bad_expr
;
8706 if (!gfc_is_constant_expr (e
))
8709 result
= gfc_get_array_expr (type
, kind
, &e
->where
);
8710 result
->shape
= gfc_copy_shape (e
->shape
, e
->rank
);
8711 result
->rank
= e
->rank
;
8713 for (c
= gfc_constructor_first (e
->value
.constructor
);
8714 c
; c
= gfc_constructor_next (c
))
8717 if (c
->iterator
== NULL
)
8719 if (c
->expr
->expr_type
== EXPR_ARRAY
)
8720 tmp
= gfc_convert_constant (c
->expr
, type
, kind
);
8721 else if (c
->expr
->expr_type
== EXPR_OP
)
8723 if (!gfc_simplify_expr (c
->expr
, 1))
8724 return &gfc_bad_expr
;
8725 tmp
= f (c
->expr
, kind
);
8728 tmp
= f (c
->expr
, kind
);
8731 tmp
= gfc_convert_constant (c
->expr
, type
, kind
);
8733 if (tmp
== NULL
|| tmp
== &gfc_bad_expr
)
8735 gfc_free_expr (result
);
8739 t
= gfc_constructor_append_expr (&result
->value
.constructor
,
8742 t
->iterator
= gfc_copy_iterator (c
->iterator
);
8755 /* Function for converting character constants. */
8757 gfc_convert_char_constant (gfc_expr
*e
, bt type ATTRIBUTE_UNUSED
, int kind
)
8762 if (!gfc_is_constant_expr (e
))
8765 if (e
->expr_type
== EXPR_CONSTANT
)
8767 /* Simple case of a scalar. */
8768 result
= gfc_get_constant_expr (BT_CHARACTER
, kind
, &e
->where
);
8770 return &gfc_bad_expr
;
8772 result
->value
.character
.length
= e
->value
.character
.length
;
8773 result
->value
.character
.string
8774 = gfc_get_wide_string (e
->value
.character
.length
+ 1);
8775 memcpy (result
->value
.character
.string
, e
->value
.character
.string
,
8776 (e
->value
.character
.length
+ 1) * sizeof (gfc_char_t
));
8778 /* Check we only have values representable in the destination kind. */
8779 for (i
= 0; i
< result
->value
.character
.length
; i
++)
8780 if (!gfc_check_character_range (result
->value
.character
.string
[i
],
8783 gfc_error ("Character %qs in string at %L cannot be converted "
8784 "into character kind %d",
8785 gfc_print_wide_char (result
->value
.character
.string
[i
]),
8787 gfc_free_expr (result
);
8788 return &gfc_bad_expr
;
8793 else if (e
->expr_type
== EXPR_ARRAY
)
8795 /* For an array constructor, we convert each constructor element. */
8798 result
= gfc_get_array_expr (type
, kind
, &e
->where
);
8799 result
->shape
= gfc_copy_shape (e
->shape
, e
->rank
);
8800 result
->rank
= e
->rank
;
8801 result
->ts
.u
.cl
= e
->ts
.u
.cl
;
8803 for (c
= gfc_constructor_first (e
->value
.constructor
);
8804 c
; c
= gfc_constructor_next (c
))
8806 gfc_expr
*tmp
= gfc_convert_char_constant (c
->expr
, type
, kind
);
8807 if (tmp
== &gfc_bad_expr
)
8809 gfc_free_expr (result
);
8810 return &gfc_bad_expr
;
8815 gfc_free_expr (result
);
8819 gfc_constructor_append_expr (&result
->value
.constructor
,
8831 gfc_simplify_compiler_options (void)
8836 str
= gfc_get_option_string ();
8837 result
= gfc_get_character_expr (gfc_default_character_kind
,
8838 &gfc_current_locus
, str
, strlen (str
));
8845 gfc_simplify_compiler_version (void)
8850 len
= strlen ("GCC version ") + strlen (version_string
);
8851 buffer
= XALLOCAVEC (char, len
+ 1);
8852 snprintf (buffer
, len
+ 1, "GCC version %s", version_string
);
8853 return gfc_get_character_expr (gfc_default_character_kind
,
8854 &gfc_current_locus
, buffer
, len
);
8857 /* Simplification routines for intrinsics of IEEE modules. */
8860 simplify_ieee_selected_real_kind (gfc_expr
*expr
)
8862 gfc_actual_arglist
*arg
;
8863 gfc_expr
*p
= NULL
, *q
= NULL
, *rdx
= NULL
;
8865 arg
= expr
->value
.function
.actual
;
8869 q
= arg
->next
->expr
;
8870 if (arg
->next
->next
)
8871 rdx
= arg
->next
->next
->expr
;
8874 /* Currently, if IEEE is supported and this module is built, it means
8875 all our floating-point types conform to IEEE. Hence, we simply handle
8876 IEEE_SELECTED_REAL_KIND like SELECTED_REAL_KIND. */
8877 return gfc_simplify_selected_real_kind (p
, q
, rdx
);
8881 simplify_ieee_support (gfc_expr
*expr
)
8883 /* We consider that if the IEEE modules are loaded, we have full support
8884 for flags, halting and rounding, which are the three functions
8885 (IEEE_SUPPORT_{FLAG,HALTING,ROUNDING}) allowed in constant
8886 expressions. One day, we will need libgfortran to detect support and
8887 communicate it back to us, allowing for partial support. */
8889 return gfc_get_logical_expr (gfc_default_logical_kind
, &expr
->where
,
8894 matches_ieee_function_name (gfc_symbol
*sym
, const char *name
)
8896 int n
= strlen(name
);
8898 if (!strncmp(sym
->name
, name
, n
))
8901 /* If a generic was used and renamed, we need more work to find out.
8902 Compare the specific name. */
8903 if (sym
->generic
&& !strncmp(sym
->generic
->sym
->name
, name
, n
))
8910 gfc_simplify_ieee_functions (gfc_expr
*expr
)
8912 gfc_symbol
* sym
= expr
->symtree
->n
.sym
;
8914 if (matches_ieee_function_name(sym
, "ieee_selected_real_kind"))
8915 return simplify_ieee_selected_real_kind (expr
);
8916 else if (matches_ieee_function_name(sym
, "ieee_support_flag")
8917 || matches_ieee_function_name(sym
, "ieee_support_halting")
8918 || matches_ieee_function_name(sym
, "ieee_support_rounding"))
8919 return simplify_ieee_support (expr
);