1 /* Simplify intrinsic functions at compile-time.
2 Copyright (C) 2000-2021 Free Software Foundation, Inc.
3 Contributed by Andy Vaught & Katherine Holcomb
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
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
)
227 if (e
->expr_type
== EXPR_VARIABLE
&& e
->rank
> 0
228 && e
->symtree
->n
.sym
->attr
.flavor
== FL_PARAMETER
)
229 gfc_simplify_expr (e
, 1);
231 if (e
->expr_type
!= EXPR_ARRAY
|| !gfc_is_constant_expr (e
))
234 for (c
= gfc_constructor_first (e
->value
.constructor
);
235 c
; c
= gfc_constructor_next (c
))
236 if (c
->expr
->expr_type
!= EXPR_CONSTANT
237 && c
->expr
->expr_type
!= EXPR_STRUCTURE
)
243 /* Test for a size zero array. */
245 gfc_is_size_zero_array (gfc_expr
*array
)
248 if (array
->rank
== 0)
251 if (array
->expr_type
== EXPR_VARIABLE
&& array
->rank
> 0
252 && array
->symtree
->n
.sym
->attr
.flavor
== FL_PARAMETER
253 && array
->shape
!= NULL
)
255 for (int i
= 0; i
< array
->rank
; i
++)
256 if (mpz_cmp_si (array
->shape
[i
], 0) <= 0)
262 if (array
->expr_type
== EXPR_ARRAY
)
263 return array
->value
.constructor
== NULL
;
269 /* Initialize a transformational result expression with a given value. */
272 init_result_expr (gfc_expr
*e
, int init
, gfc_expr
*array
)
274 if (e
&& e
->expr_type
== EXPR_ARRAY
)
276 gfc_constructor
*ctor
= gfc_constructor_first (e
->value
.constructor
);
279 init_result_expr (ctor
->expr
, init
, array
);
280 ctor
= gfc_constructor_next (ctor
);
283 else if (e
&& e
->expr_type
== EXPR_CONSTANT
)
285 int i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
286 HOST_WIDE_INT length
;
292 e
->value
.logical
= (init
? 1 : 0);
297 mpz_set (e
->value
.integer
, gfc_integer_kinds
[i
].min_int
);
298 else if (init
== INT_MAX
)
299 mpz_set (e
->value
.integer
, gfc_integer_kinds
[i
].huge
);
301 mpz_set_si (e
->value
.integer
, init
);
307 mpfr_set (e
->value
.real
, gfc_real_kinds
[i
].huge
, GFC_RND_MODE
);
308 mpfr_neg (e
->value
.real
, e
->value
.real
, GFC_RND_MODE
);
310 else if (init
== INT_MAX
)
311 mpfr_set (e
->value
.real
, gfc_real_kinds
[i
].huge
, GFC_RND_MODE
);
313 mpfr_set_si (e
->value
.real
, init
, GFC_RND_MODE
);
317 mpc_set_si (e
->value
.complex, init
, GFC_MPC_RND_MODE
);
323 gfc_expr
*len
= gfc_simplify_len (array
, NULL
);
324 gfc_extract_hwi (len
, &length
);
325 string
= gfc_get_wide_string (length
+ 1);
326 gfc_wide_memset (string
, 0, length
);
328 else if (init
== INT_MAX
)
330 gfc_expr
*len
= gfc_simplify_len (array
, NULL
);
331 gfc_extract_hwi (len
, &length
);
332 string
= gfc_get_wide_string (length
+ 1);
333 gfc_wide_memset (string
, 255, length
);
338 string
= gfc_get_wide_string (1);
341 string
[length
] = '\0';
342 e
->value
.character
.length
= length
;
343 e
->value
.character
.string
= string
;
355 /* Helper function for gfc_simplify_dot_product() and gfc_simplify_matmul;
356 if conj_a is true, the matrix_a is complex conjugated. */
359 compute_dot_product (gfc_expr
*matrix_a
, int stride_a
, int offset_a
,
360 gfc_expr
*matrix_b
, int stride_b
, int offset_b
,
363 gfc_expr
*result
, *a
, *b
, *c
;
365 /* Set result to an INTEGER(1) 0 for numeric types and .false. for
366 LOGICAL. Mixed-mode math in the loop will promote result to the
367 correct type and kind. */
368 if (matrix_a
->ts
.type
== BT_LOGICAL
)
369 result
= gfc_get_logical_expr (gfc_default_logical_kind
, NULL
, false);
371 result
= gfc_get_int_expr (1, NULL
, 0);
372 result
->where
= matrix_a
->where
;
374 a
= gfc_constructor_lookup_expr (matrix_a
->value
.constructor
, offset_a
);
375 b
= gfc_constructor_lookup_expr (matrix_b
->value
.constructor
, offset_b
);
378 /* Copying of expressions is required as operands are free'd
379 by the gfc_arith routines. */
380 switch (result
->ts
.type
)
383 result
= gfc_or (result
,
384 gfc_and (gfc_copy_expr (a
),
391 if (conj_a
&& a
->ts
.type
== BT_COMPLEX
)
392 c
= gfc_simplify_conjg (a
);
394 c
= gfc_copy_expr (a
);
395 result
= gfc_add (result
, gfc_multiply (c
, gfc_copy_expr (b
)));
402 offset_a
+= stride_a
;
403 a
= gfc_constructor_lookup_expr (matrix_a
->value
.constructor
, offset_a
);
405 offset_b
+= stride_b
;
406 b
= gfc_constructor_lookup_expr (matrix_b
->value
.constructor
, offset_b
);
413 /* Build a result expression for transformational intrinsics,
417 transformational_result (gfc_expr
*array
, gfc_expr
*dim
, bt type
,
418 int kind
, locus
* where
)
423 if (!dim
|| array
->rank
== 1)
424 return gfc_get_constant_expr (type
, kind
, where
);
426 result
= gfc_get_array_expr (type
, kind
, where
);
427 result
->shape
= gfc_copy_shape_excluding (array
->shape
, array
->rank
, dim
);
428 result
->rank
= array
->rank
- 1;
430 /* gfc_array_size() would count the number of elements in the constructor,
431 we have not built those yet. */
433 for (i
= 0; i
< result
->rank
; ++i
)
434 nelem
*= mpz_get_ui (result
->shape
[i
]);
436 for (i
= 0; i
< nelem
; ++i
)
438 gfc_constructor_append_expr (&result
->value
.constructor
,
439 gfc_get_constant_expr (type
, kind
, where
),
447 typedef gfc_expr
* (*transformational_op
)(gfc_expr
*, gfc_expr
*);
449 /* Wrapper function, implements 'op1 += 1'. Only called if MASK
450 of COUNT intrinsic is .TRUE..
452 Interface and implementation mimics arith functions as
453 gfc_add, gfc_multiply, etc. */
456 gfc_count (gfc_expr
*op1
, gfc_expr
*op2
)
460 gcc_assert (op1
->ts
.type
== BT_INTEGER
);
461 gcc_assert (op2
->ts
.type
== BT_LOGICAL
);
462 gcc_assert (op2
->value
.logical
);
464 result
= gfc_copy_expr (op1
);
465 mpz_add_ui (result
->value
.integer
, result
->value
.integer
, 1);
473 /* Transforms an ARRAY with operation OP, according to MASK, to a
474 scalar RESULT. E.g. called if
476 REAL, PARAMETER :: array(n, m) = ...
477 REAL, PARAMETER :: s = SUM(array)
479 where OP == gfc_add(). */
482 simplify_transformation_to_scalar (gfc_expr
*result
, gfc_expr
*array
, gfc_expr
*mask
,
483 transformational_op op
)
486 gfc_constructor
*array_ctor
, *mask_ctor
;
488 /* Shortcut for constant .FALSE. MASK. */
490 && mask
->expr_type
== EXPR_CONSTANT
491 && !mask
->value
.logical
)
494 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
496 if (mask
&& mask
->expr_type
== EXPR_ARRAY
)
497 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
501 a
= array_ctor
->expr
;
502 array_ctor
= gfc_constructor_next (array_ctor
);
504 /* A constant MASK equals .TRUE. here and can be ignored. */
508 mask_ctor
= gfc_constructor_next (mask_ctor
);
509 if (!m
->value
.logical
)
513 result
= op (result
, gfc_copy_expr (a
));
521 /* Transforms an ARRAY with operation OP, according to MASK, to an
522 array RESULT. E.g. called if
524 REAL, PARAMETER :: array(n, m) = ...
525 REAL, PARAMETER :: s(n) = PROD(array, DIM=1)
527 where OP == gfc_multiply().
528 The result might be post processed using post_op. */
531 simplify_transformation_to_array (gfc_expr
*result
, gfc_expr
*array
, gfc_expr
*dim
,
532 gfc_expr
*mask
, transformational_op op
,
533 transformational_op post_op
)
536 int done
, i
, n
, arraysize
, resultsize
, dim_index
, dim_extent
, dim_stride
;
537 gfc_expr
**arrayvec
, **resultvec
, **base
, **src
, **dest
;
538 gfc_constructor
*array_ctor
, *mask_ctor
, *result_ctor
;
540 int count
[GFC_MAX_DIMENSIONS
], extent
[GFC_MAX_DIMENSIONS
],
541 sstride
[GFC_MAX_DIMENSIONS
], dstride
[GFC_MAX_DIMENSIONS
],
542 tmpstride
[GFC_MAX_DIMENSIONS
];
544 /* Shortcut for constant .FALSE. MASK. */
546 && mask
->expr_type
== EXPR_CONSTANT
547 && !mask
->value
.logical
)
550 /* Build an indexed table for array element expressions to minimize
551 linked-list traversal. Masked elements are set to NULL. */
552 gfc_array_size (array
, &size
);
553 arraysize
= mpz_get_ui (size
);
556 arrayvec
= XCNEWVEC (gfc_expr
*, arraysize
);
558 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
560 if (mask
&& mask
->expr_type
== EXPR_ARRAY
)
561 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
563 for (i
= 0; i
< arraysize
; ++i
)
565 arrayvec
[i
] = array_ctor
->expr
;
566 array_ctor
= gfc_constructor_next (array_ctor
);
570 if (!mask_ctor
->expr
->value
.logical
)
573 mask_ctor
= gfc_constructor_next (mask_ctor
);
577 /* Same for the result expression. */
578 gfc_array_size (result
, &size
);
579 resultsize
= mpz_get_ui (size
);
582 resultvec
= XCNEWVEC (gfc_expr
*, resultsize
);
583 result_ctor
= gfc_constructor_first (result
->value
.constructor
);
584 for (i
= 0; i
< resultsize
; ++i
)
586 resultvec
[i
] = result_ctor
->expr
;
587 result_ctor
= gfc_constructor_next (result_ctor
);
590 gfc_extract_int (dim
, &dim_index
);
591 dim_index
-= 1; /* zero-base index */
595 for (i
= 0, n
= 0; i
< array
->rank
; ++i
)
598 tmpstride
[i
] = (i
== 0) ? 1 : tmpstride
[i
-1] * mpz_get_si (array
->shape
[i
-1]);
601 dim_extent
= mpz_get_si (array
->shape
[i
]);
602 dim_stride
= tmpstride
[i
];
606 extent
[n
] = mpz_get_si (array
->shape
[i
]);
607 sstride
[n
] = tmpstride
[i
];
608 dstride
[n
] = (n
== 0) ? 1 : dstride
[n
-1] * extent
[n
-1];
612 done
= resultsize
<= 0;
617 for (src
= base
, n
= 0; n
< dim_extent
; src
+= dim_stride
, ++n
)
619 *dest
= op (*dest
, gfc_copy_expr (*src
));
622 *dest
= post_op (*dest
, *dest
);
629 while (!done
&& count
[n
] == extent
[n
])
632 base
-= sstride
[n
] * extent
[n
];
633 dest
-= dstride
[n
] * extent
[n
];
636 if (n
< result
->rank
)
638 /* If the nested loop is unrolled GFC_MAX_DIMENSIONS
639 times, we'd warn for the last iteration, because the
640 array index will have already been incremented to the
641 array sizes, and we can't tell that this must make
642 the test against result->rank false, because ranks
643 must not exceed GFC_MAX_DIMENSIONS. */
644 GCC_DIAGNOSTIC_PUSH_IGNORED (-Warray
-bounds
)
655 /* Place updated expression in result constructor. */
656 result_ctor
= gfc_constructor_first (result
->value
.constructor
);
657 for (i
= 0; i
< resultsize
; ++i
)
659 result_ctor
->expr
= resultvec
[i
];
660 result_ctor
= gfc_constructor_next (result_ctor
);
670 simplify_transformation (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
,
671 int init_val
, transformational_op op
)
676 size_zero
= gfc_is_size_zero_array (array
);
678 if (!(is_constant_array_expr (array
) || size_zero
)
679 || !gfc_is_constant_expr (dim
))
683 && !is_constant_array_expr (mask
)
684 && mask
->expr_type
!= EXPR_CONSTANT
)
687 result
= transformational_result (array
, dim
, array
->ts
.type
,
688 array
->ts
.kind
, &array
->where
);
689 init_result_expr (result
, init_val
, array
);
694 return !dim
|| array
->rank
== 1 ?
695 simplify_transformation_to_scalar (result
, array
, mask
, op
) :
696 simplify_transformation_to_array (result
, array
, dim
, mask
, op
, NULL
);
700 /********************** Simplification functions *****************************/
703 gfc_simplify_abs (gfc_expr
*e
)
707 if (e
->expr_type
!= EXPR_CONSTANT
)
713 result
= gfc_get_constant_expr (BT_INTEGER
, e
->ts
.kind
, &e
->where
);
714 mpz_abs (result
->value
.integer
, e
->value
.integer
);
715 return range_check (result
, "IABS");
718 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
719 mpfr_abs (result
->value
.real
, e
->value
.real
, GFC_RND_MODE
);
720 return range_check (result
, "ABS");
723 gfc_set_model_kind (e
->ts
.kind
);
724 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
725 mpc_abs (result
->value
.real
, e
->value
.complex, GFC_RND_MODE
);
726 return range_check (result
, "CABS");
729 gfc_internal_error ("gfc_simplify_abs(): Bad type");
735 simplify_achar_char (gfc_expr
*e
, gfc_expr
*k
, const char *name
, bool ascii
)
739 bool too_large
= false;
741 if (e
->expr_type
!= EXPR_CONSTANT
)
744 kind
= get_kind (BT_CHARACTER
, k
, name
, gfc_default_character_kind
);
746 return &gfc_bad_expr
;
748 if (mpz_cmp_si (e
->value
.integer
, 0) < 0)
750 gfc_error ("Argument of %s function at %L is negative", name
,
752 return &gfc_bad_expr
;
755 if (ascii
&& warn_surprising
&& mpz_cmp_si (e
->value
.integer
, 127) > 0)
756 gfc_warning (OPT_Wsurprising
,
757 "Argument of %s function at %L outside of range [0,127]",
760 if (kind
== 1 && mpz_cmp_si (e
->value
.integer
, 255) > 0)
765 mpz_init_set_ui (t
, 2);
766 mpz_pow_ui (t
, t
, 32);
767 mpz_sub_ui (t
, t
, 1);
768 if (mpz_cmp (e
->value
.integer
, t
) > 0)
775 gfc_error ("Argument of %s function at %L is too large for the "
776 "collating sequence of kind %d", name
, &e
->where
, kind
);
777 return &gfc_bad_expr
;
780 result
= gfc_get_character_expr (kind
, &e
->where
, NULL
, 1);
781 result
->value
.character
.string
[0] = mpz_get_ui (e
->value
.integer
);
788 /* We use the processor's collating sequence, because all
789 systems that gfortran currently works on are ASCII. */
792 gfc_simplify_achar (gfc_expr
*e
, gfc_expr
*k
)
794 return simplify_achar_char (e
, k
, "ACHAR", true);
799 gfc_simplify_acos (gfc_expr
*x
)
803 if (x
->expr_type
!= EXPR_CONSTANT
)
809 if (mpfr_cmp_si (x
->value
.real
, 1) > 0
810 || mpfr_cmp_si (x
->value
.real
, -1) < 0)
812 gfc_error ("Argument of ACOS at %L must be between -1 and 1",
814 return &gfc_bad_expr
;
816 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
817 mpfr_acos (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
821 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
822 mpc_acos (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
826 gfc_internal_error ("in gfc_simplify_acos(): Bad type");
829 return range_check (result
, "ACOS");
833 gfc_simplify_acosh (gfc_expr
*x
)
837 if (x
->expr_type
!= EXPR_CONSTANT
)
843 if (mpfr_cmp_si (x
->value
.real
, 1) < 0)
845 gfc_error ("Argument of ACOSH at %L must not be less than 1",
847 return &gfc_bad_expr
;
850 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
851 mpfr_acosh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
855 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
856 mpc_acosh (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
860 gfc_internal_error ("in gfc_simplify_acosh(): Bad type");
863 return range_check (result
, "ACOSH");
867 gfc_simplify_adjustl (gfc_expr
*e
)
873 if (e
->expr_type
!= EXPR_CONSTANT
)
876 len
= e
->value
.character
.length
;
878 for (count
= 0, i
= 0; i
< len
; ++i
)
880 ch
= e
->value
.character
.string
[i
];
886 result
= gfc_get_character_expr (e
->ts
.kind
, &e
->where
, NULL
, len
);
887 for (i
= 0; i
< len
- count
; ++i
)
888 result
->value
.character
.string
[i
] = e
->value
.character
.string
[count
+ i
];
895 gfc_simplify_adjustr (gfc_expr
*e
)
901 if (e
->expr_type
!= EXPR_CONSTANT
)
904 len
= e
->value
.character
.length
;
906 for (count
= 0, i
= len
- 1; i
>= 0; --i
)
908 ch
= e
->value
.character
.string
[i
];
914 result
= gfc_get_character_expr (e
->ts
.kind
, &e
->where
, NULL
, len
);
915 for (i
= 0; i
< count
; ++i
)
916 result
->value
.character
.string
[i
] = ' ';
918 for (i
= count
; i
< len
; ++i
)
919 result
->value
.character
.string
[i
] = e
->value
.character
.string
[i
- count
];
926 gfc_simplify_aimag (gfc_expr
*e
)
930 if (e
->expr_type
!= EXPR_CONSTANT
)
933 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
934 mpfr_set (result
->value
.real
, mpc_imagref (e
->value
.complex), GFC_RND_MODE
);
936 return range_check (result
, "AIMAG");
941 gfc_simplify_aint (gfc_expr
*e
, gfc_expr
*k
)
943 gfc_expr
*rtrunc
, *result
;
946 kind
= get_kind (BT_REAL
, k
, "AINT", e
->ts
.kind
);
948 return &gfc_bad_expr
;
950 if (e
->expr_type
!= EXPR_CONSTANT
)
953 rtrunc
= gfc_copy_expr (e
);
954 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
956 result
= gfc_real2real (rtrunc
, kind
);
958 gfc_free_expr (rtrunc
);
960 return range_check (result
, "AINT");
965 gfc_simplify_all (gfc_expr
*mask
, gfc_expr
*dim
)
967 return simplify_transformation (mask
, dim
, NULL
, true, gfc_and
);
972 gfc_simplify_dint (gfc_expr
*e
)
974 gfc_expr
*rtrunc
, *result
;
976 if (e
->expr_type
!= EXPR_CONSTANT
)
979 rtrunc
= gfc_copy_expr (e
);
980 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
982 result
= gfc_real2real (rtrunc
, gfc_default_double_kind
);
984 gfc_free_expr (rtrunc
);
986 return range_check (result
, "DINT");
991 gfc_simplify_dreal (gfc_expr
*e
)
993 gfc_expr
*result
= NULL
;
995 if (e
->expr_type
!= EXPR_CONSTANT
)
998 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
999 mpc_real (result
->value
.real
, e
->value
.complex, GFC_RND_MODE
);
1001 return range_check (result
, "DREAL");
1006 gfc_simplify_anint (gfc_expr
*e
, gfc_expr
*k
)
1011 kind
= get_kind (BT_REAL
, k
, "ANINT", e
->ts
.kind
);
1013 return &gfc_bad_expr
;
1015 if (e
->expr_type
!= EXPR_CONSTANT
)
1018 result
= gfc_get_constant_expr (e
->ts
.type
, kind
, &e
->where
);
1019 mpfr_round (result
->value
.real
, e
->value
.real
);
1021 return range_check (result
, "ANINT");
1026 gfc_simplify_and (gfc_expr
*x
, gfc_expr
*y
)
1031 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1034 kind
= x
->ts
.kind
> y
->ts
.kind
? x
->ts
.kind
: y
->ts
.kind
;
1039 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &x
->where
);
1040 mpz_and (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
1041 return range_check (result
, "AND");
1044 return gfc_get_logical_expr (kind
, &x
->where
,
1045 x
->value
.logical
&& y
->value
.logical
);
1054 gfc_simplify_any (gfc_expr
*mask
, gfc_expr
*dim
)
1056 return simplify_transformation (mask
, dim
, NULL
, false, gfc_or
);
1061 gfc_simplify_dnint (gfc_expr
*e
)
1065 if (e
->expr_type
!= EXPR_CONSTANT
)
1068 result
= gfc_get_constant_expr (BT_REAL
, gfc_default_double_kind
, &e
->where
);
1069 mpfr_round (result
->value
.real
, e
->value
.real
);
1071 return range_check (result
, "DNINT");
1076 gfc_simplify_asin (gfc_expr
*x
)
1080 if (x
->expr_type
!= EXPR_CONSTANT
)
1086 if (mpfr_cmp_si (x
->value
.real
, 1) > 0
1087 || mpfr_cmp_si (x
->value
.real
, -1) < 0)
1089 gfc_error ("Argument of ASIN at %L must be between -1 and 1",
1091 return &gfc_bad_expr
;
1093 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1094 mpfr_asin (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1098 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1099 mpc_asin (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1103 gfc_internal_error ("in gfc_simplify_asin(): Bad type");
1106 return range_check (result
, "ASIN");
1110 /* Convert radians to degrees, i.e., x * 180 / pi. */
1118 mpfr_const_pi (tmp
, GFC_RND_MODE
);
1119 mpfr_mul_ui (x
, x
, 180, GFC_RND_MODE
);
1120 mpfr_div (x
, x
, tmp
, GFC_RND_MODE
);
1125 /* Simplify ACOSD(X) where the returned value has units of degree. */
1128 gfc_simplify_acosd (gfc_expr
*x
)
1132 if (x
->expr_type
!= EXPR_CONSTANT
)
1135 if (mpfr_cmp_si (x
->value
.real
, 1) > 0
1136 || mpfr_cmp_si (x
->value
.real
, -1) < 0)
1138 gfc_error ("Argument of ACOSD at %L must be between -1 and 1",
1140 return &gfc_bad_expr
;
1143 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1144 mpfr_acos (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1145 rad2deg (result
->value
.real
);
1147 return range_check (result
, "ACOSD");
1151 /* Simplify asind (x) where the returned value has units of degree. */
1154 gfc_simplify_asind (gfc_expr
*x
)
1158 if (x
->expr_type
!= EXPR_CONSTANT
)
1161 if (mpfr_cmp_si (x
->value
.real
, 1) > 0
1162 || mpfr_cmp_si (x
->value
.real
, -1) < 0)
1164 gfc_error ("Argument of ASIND at %L must be between -1 and 1",
1166 return &gfc_bad_expr
;
1169 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1170 mpfr_asin (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1171 rad2deg (result
->value
.real
);
1173 return range_check (result
, "ASIND");
1177 /* Simplify atand (x) where the returned value has units of degree. */
1180 gfc_simplify_atand (gfc_expr
*x
)
1184 if (x
->expr_type
!= EXPR_CONSTANT
)
1187 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1188 mpfr_atan (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1189 rad2deg (result
->value
.real
);
1191 return range_check (result
, "ATAND");
1196 gfc_simplify_asinh (gfc_expr
*x
)
1200 if (x
->expr_type
!= EXPR_CONSTANT
)
1203 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1208 mpfr_asinh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1212 mpc_asinh (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1216 gfc_internal_error ("in gfc_simplify_asinh(): Bad type");
1219 return range_check (result
, "ASINH");
1224 gfc_simplify_atan (gfc_expr
*x
)
1228 if (x
->expr_type
!= EXPR_CONSTANT
)
1231 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1236 mpfr_atan (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1240 mpc_atan (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1244 gfc_internal_error ("in gfc_simplify_atan(): Bad type");
1247 return range_check (result
, "ATAN");
1252 gfc_simplify_atanh (gfc_expr
*x
)
1256 if (x
->expr_type
!= EXPR_CONSTANT
)
1262 if (mpfr_cmp_si (x
->value
.real
, 1) >= 0
1263 || mpfr_cmp_si (x
->value
.real
, -1) <= 0)
1265 gfc_error ("Argument of ATANH at %L must be inside the range -1 "
1267 return &gfc_bad_expr
;
1269 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1270 mpfr_atanh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1274 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1275 mpc_atanh (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1279 gfc_internal_error ("in gfc_simplify_atanh(): Bad type");
1282 return range_check (result
, "ATANH");
1287 gfc_simplify_atan2 (gfc_expr
*y
, gfc_expr
*x
)
1291 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1294 if (mpfr_zero_p (y
->value
.real
) && mpfr_zero_p (x
->value
.real
))
1296 gfc_error ("If first argument of ATAN2 at %L is zero, then the "
1297 "second argument must not be zero", &y
->where
);
1298 return &gfc_bad_expr
;
1301 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1302 mpfr_atan2 (result
->value
.real
, y
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1304 return range_check (result
, "ATAN2");
1309 gfc_simplify_bessel_j0 (gfc_expr
*x
)
1313 if (x
->expr_type
!= EXPR_CONSTANT
)
1316 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1317 mpfr_j0 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1319 return range_check (result
, "BESSEL_J0");
1324 gfc_simplify_bessel_j1 (gfc_expr
*x
)
1328 if (x
->expr_type
!= EXPR_CONSTANT
)
1331 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1332 mpfr_j1 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1334 return range_check (result
, "BESSEL_J1");
1339 gfc_simplify_bessel_jn (gfc_expr
*order
, gfc_expr
*x
)
1344 if (x
->expr_type
!= EXPR_CONSTANT
|| order
->expr_type
!= EXPR_CONSTANT
)
1347 n
= mpz_get_si (order
->value
.integer
);
1348 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1349 mpfr_jn (result
->value
.real
, n
, x
->value
.real
, GFC_RND_MODE
);
1351 return range_check (result
, "BESSEL_JN");
1355 /* Simplify transformational form of JN and YN. */
1358 gfc_simplify_bessel_n2 (gfc_expr
*order1
, gfc_expr
*order2
, gfc_expr
*x
,
1365 mpfr_t x2rev
, last1
, last2
;
1367 if (x
->expr_type
!= EXPR_CONSTANT
|| order1
->expr_type
!= EXPR_CONSTANT
1368 || order2
->expr_type
!= EXPR_CONSTANT
)
1371 n1
= mpz_get_si (order1
->value
.integer
);
1372 n2
= mpz_get_si (order2
->value
.integer
);
1373 result
= gfc_get_array_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1375 result
->shape
= gfc_get_shape (1);
1376 mpz_init_set_ui (result
->shape
[0], MAX (n2
-n1
+1, 0));
1381 /* Special case: x == 0; it is J0(0.0) == 1, JN(N > 0, 0.0) == 0; and
1382 YN(N, 0.0) = -Inf. */
1384 if (mpfr_cmp_ui (x
->value
.real
, 0.0) == 0)
1386 if (!jn
&& flag_range_check
)
1388 gfc_error ("Result of BESSEL_YN is -INF at %L", &result
->where
);
1389 gfc_free_expr (result
);
1390 return &gfc_bad_expr
;
1395 e
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1396 mpfr_set_ui (e
->value
.real
, 1, GFC_RND_MODE
);
1397 gfc_constructor_append_expr (&result
->value
.constructor
, e
,
1402 for (i
= n1
; i
<= n2
; i
++)
1404 e
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1406 mpfr_set_ui (e
->value
.real
, 0, GFC_RND_MODE
);
1408 mpfr_set_inf (e
->value
.real
, -1);
1409 gfc_constructor_append_expr (&result
->value
.constructor
, e
,
1416 /* Use the faster but more verbose recurrence algorithm. Bessel functions
1417 are stable for downward recursion and Neumann functions are stable
1418 for upward recursion. It is
1420 J(N-1, x) = x2rev * N * J(N, x) - J(N+1, x),
1421 Y(N+1, x) = x2rev * N * Y(N, x) - Y(N-1, x).
1422 Cf. http://dlmf.nist.gov/10.74#iv and http://dlmf.nist.gov/10.6#E1 */
1424 gfc_set_model_kind (x
->ts
.kind
);
1426 /* Get first recursion anchor. */
1430 mpfr_jn (last1
, n2
, x
->value
.real
, GFC_RND_MODE
);
1432 mpfr_yn (last1
, n1
, x
->value
.real
, GFC_RND_MODE
);
1434 e
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1435 mpfr_set (e
->value
.real
, last1
, GFC_RND_MODE
);
1436 if (range_check (e
, jn
? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr
)
1440 gfc_free_expr (result
);
1441 return &gfc_bad_expr
;
1443 gfc_constructor_append_expr (&result
->value
.constructor
, e
, &x
->where
);
1451 /* Get second recursion anchor. */
1455 mpfr_jn (last2
, n2
-1, x
->value
.real
, GFC_RND_MODE
);
1457 mpfr_yn (last2
, n1
+1, x
->value
.real
, GFC_RND_MODE
);
1459 e
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1460 mpfr_set (e
->value
.real
, last2
, GFC_RND_MODE
);
1461 if (range_check (e
, jn
? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr
)
1466 gfc_free_expr (result
);
1467 return &gfc_bad_expr
;
1470 gfc_constructor_insert_expr (&result
->value
.constructor
, e
, &x
->where
, -2);
1472 gfc_constructor_append_expr (&result
->value
.constructor
, e
, &x
->where
);
1481 /* Start actual recursion. */
1484 mpfr_ui_div (x2rev
, 2, x
->value
.real
, GFC_RND_MODE
);
1486 for (i
= 2; i
<= n2
-n1
; i
++)
1488 e
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1490 /* Special case: For YN, if the previous N gave -INF, set
1491 also N+1 to -INF. */
1492 if (!jn
&& !flag_range_check
&& mpfr_inf_p (last2
))
1494 mpfr_set_inf (e
->value
.real
, -1);
1495 gfc_constructor_append_expr (&result
->value
.constructor
, e
,
1500 mpfr_mul_si (e
->value
.real
, x2rev
, jn
? (n2
-i
+1) : (n1
+i
-1),
1502 mpfr_mul (e
->value
.real
, e
->value
.real
, last2
, GFC_RND_MODE
);
1503 mpfr_sub (e
->value
.real
, e
->value
.real
, last1
, GFC_RND_MODE
);
1505 if (range_check (e
, jn
? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr
)
1507 /* Range_check frees "e" in that case. */
1513 gfc_constructor_insert_expr (&result
->value
.constructor
, e
, &x
->where
,
1516 gfc_constructor_append_expr (&result
->value
.constructor
, e
, &x
->where
);
1518 mpfr_set (last1
, last2
, GFC_RND_MODE
);
1519 mpfr_set (last2
, e
->value
.real
, GFC_RND_MODE
);
1532 gfc_free_expr (result
);
1533 return &gfc_bad_expr
;
1538 gfc_simplify_bessel_jn2 (gfc_expr
*order1
, gfc_expr
*order2
, gfc_expr
*x
)
1540 return gfc_simplify_bessel_n2 (order1
, order2
, x
, true);
1545 gfc_simplify_bessel_y0 (gfc_expr
*x
)
1549 if (x
->expr_type
!= EXPR_CONSTANT
)
1552 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1553 mpfr_y0 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1555 return range_check (result
, "BESSEL_Y0");
1560 gfc_simplify_bessel_y1 (gfc_expr
*x
)
1564 if (x
->expr_type
!= EXPR_CONSTANT
)
1567 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1568 mpfr_y1 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1570 return range_check (result
, "BESSEL_Y1");
1575 gfc_simplify_bessel_yn (gfc_expr
*order
, gfc_expr
*x
)
1580 if (x
->expr_type
!= EXPR_CONSTANT
|| order
->expr_type
!= EXPR_CONSTANT
)
1583 n
= mpz_get_si (order
->value
.integer
);
1584 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1585 mpfr_yn (result
->value
.real
, n
, x
->value
.real
, GFC_RND_MODE
);
1587 return range_check (result
, "BESSEL_YN");
1592 gfc_simplify_bessel_yn2 (gfc_expr
*order1
, gfc_expr
*order2
, gfc_expr
*x
)
1594 return gfc_simplify_bessel_n2 (order1
, order2
, x
, false);
1599 gfc_simplify_bit_size (gfc_expr
*e
)
1601 int i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
1602 return gfc_get_int_expr (e
->ts
.kind
, &e
->where
,
1603 gfc_integer_kinds
[i
].bit_size
);
1608 gfc_simplify_btest (gfc_expr
*e
, gfc_expr
*bit
)
1612 if (e
->expr_type
!= EXPR_CONSTANT
|| bit
->expr_type
!= EXPR_CONSTANT
)
1615 if (gfc_extract_int (bit
, &b
) || b
< 0)
1616 return gfc_get_logical_expr (gfc_default_logical_kind
, &e
->where
, false);
1618 return gfc_get_logical_expr (gfc_default_logical_kind
, &e
->where
,
1619 mpz_tstbit (e
->value
.integer
, b
));
1624 compare_bitwise (gfc_expr
*i
, gfc_expr
*j
)
1629 gcc_assert (i
->ts
.type
== BT_INTEGER
);
1630 gcc_assert (j
->ts
.type
== BT_INTEGER
);
1632 mpz_init_set (x
, i
->value
.integer
);
1633 k
= gfc_validate_kind (i
->ts
.type
, i
->ts
.kind
, false);
1634 convert_mpz_to_unsigned (x
, gfc_integer_kinds
[k
].bit_size
);
1636 mpz_init_set (y
, j
->value
.integer
);
1637 k
= gfc_validate_kind (j
->ts
.type
, j
->ts
.kind
, false);
1638 convert_mpz_to_unsigned (y
, gfc_integer_kinds
[k
].bit_size
);
1640 res
= mpz_cmp (x
, y
);
1648 gfc_simplify_bge (gfc_expr
*i
, gfc_expr
*j
)
1650 if (i
->expr_type
!= EXPR_CONSTANT
|| j
->expr_type
!= EXPR_CONSTANT
)
1653 return gfc_get_logical_expr (gfc_default_logical_kind
, &i
->where
,
1654 compare_bitwise (i
, j
) >= 0);
1659 gfc_simplify_bgt (gfc_expr
*i
, gfc_expr
*j
)
1661 if (i
->expr_type
!= EXPR_CONSTANT
|| j
->expr_type
!= EXPR_CONSTANT
)
1664 return gfc_get_logical_expr (gfc_default_logical_kind
, &i
->where
,
1665 compare_bitwise (i
, j
) > 0);
1670 gfc_simplify_ble (gfc_expr
*i
, gfc_expr
*j
)
1672 if (i
->expr_type
!= EXPR_CONSTANT
|| j
->expr_type
!= EXPR_CONSTANT
)
1675 return gfc_get_logical_expr (gfc_default_logical_kind
, &i
->where
,
1676 compare_bitwise (i
, j
) <= 0);
1681 gfc_simplify_blt (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_ceiling (gfc_expr
*e
, gfc_expr
*k
)
1694 gfc_expr
*ceil
, *result
;
1697 kind
= get_kind (BT_INTEGER
, k
, "CEILING", gfc_default_integer_kind
);
1699 return &gfc_bad_expr
;
1701 if (e
->expr_type
!= EXPR_CONSTANT
)
1704 ceil
= gfc_copy_expr (e
);
1705 mpfr_ceil (ceil
->value
.real
, e
->value
.real
);
1707 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &e
->where
);
1708 gfc_mpfr_to_mpz (result
->value
.integer
, ceil
->value
.real
, &e
->where
);
1710 gfc_free_expr (ceil
);
1712 return range_check (result
, "CEILING");
1717 gfc_simplify_char (gfc_expr
*e
, gfc_expr
*k
)
1719 return simplify_achar_char (e
, k
, "CHAR", false);
1723 /* Common subroutine for simplifying CMPLX, COMPLEX and DCMPLX. */
1726 simplify_cmplx (const char *name
, gfc_expr
*x
, gfc_expr
*y
, int kind
)
1730 if (x
->expr_type
!= EXPR_CONSTANT
1731 || (y
!= NULL
&& y
->expr_type
!= EXPR_CONSTANT
))
1734 result
= gfc_get_constant_expr (BT_COMPLEX
, kind
, &x
->where
);
1739 mpc_set_z (result
->value
.complex, x
->value
.integer
, GFC_MPC_RND_MODE
);
1743 mpc_set_fr (result
->value
.complex, x
->value
.real
, GFC_RND_MODE
);
1747 mpc_set (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1751 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (x)");
1755 return range_check (result
, name
);
1760 mpfr_set_z (mpc_imagref (result
->value
.complex),
1761 y
->value
.integer
, GFC_RND_MODE
);
1765 mpfr_set (mpc_imagref (result
->value
.complex),
1766 y
->value
.real
, GFC_RND_MODE
);
1770 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (y)");
1773 return range_check (result
, name
);
1778 gfc_simplify_cmplx (gfc_expr
*x
, gfc_expr
*y
, gfc_expr
*k
)
1782 kind
= get_kind (BT_REAL
, k
, "CMPLX", gfc_default_complex_kind
);
1784 return &gfc_bad_expr
;
1786 return simplify_cmplx ("CMPLX", x
, y
, kind
);
1791 gfc_simplify_complex (gfc_expr
*x
, gfc_expr
*y
)
1795 if (x
->ts
.type
== BT_INTEGER
&& y
->ts
.type
== BT_INTEGER
)
1796 kind
= gfc_default_complex_kind
;
1797 else if (x
->ts
.type
== BT_REAL
|| y
->ts
.type
== BT_INTEGER
)
1799 else if (x
->ts
.type
== BT_INTEGER
|| y
->ts
.type
== BT_REAL
)
1801 else if (x
->ts
.type
== BT_REAL
&& y
->ts
.type
== BT_REAL
)
1802 kind
= (x
->ts
.kind
> y
->ts
.kind
) ? x
->ts
.kind
: y
->ts
.kind
;
1806 return simplify_cmplx ("COMPLEX", x
, y
, kind
);
1811 gfc_simplify_conjg (gfc_expr
*e
)
1815 if (e
->expr_type
!= EXPR_CONSTANT
)
1818 result
= gfc_copy_expr (e
);
1819 mpc_conj (result
->value
.complex, result
->value
.complex, GFC_MPC_RND_MODE
);
1821 return range_check (result
, "CONJG");
1825 /* Simplify atan2d (x) where the unit is degree. */
1828 gfc_simplify_atan2d (gfc_expr
*y
, gfc_expr
*x
)
1832 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1835 if (mpfr_zero_p (y
->value
.real
) && mpfr_zero_p (x
->value
.real
))
1837 gfc_error ("If first argument of ATAN2D at %L is zero, then the "
1838 "second argument must not be zero", &y
->where
);
1839 return &gfc_bad_expr
;
1842 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1843 mpfr_atan2 (result
->value
.real
, y
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1844 rad2deg (result
->value
.real
);
1846 return range_check (result
, "ATAN2D");
1851 gfc_simplify_cos (gfc_expr
*x
)
1855 if (x
->expr_type
!= EXPR_CONSTANT
)
1858 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1863 mpfr_cos (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1867 gfc_set_model_kind (x
->ts
.kind
);
1868 mpc_cos (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1872 gfc_internal_error ("in gfc_simplify_cos(): Bad type");
1875 return range_check (result
, "COS");
1885 mpfr_const_pi (d2r
, GFC_RND_MODE
);
1886 mpfr_div_ui (d2r
, d2r
, 180, GFC_RND_MODE
);
1887 mpfr_mul (x
, x
, d2r
, GFC_RND_MODE
);
1892 /* Simplification routines for SIND, COSD, TAND. */
1893 #include "trigd_fe.inc"
1896 /* Simplify COSD(X) where X has the unit of degree. */
1899 gfc_simplify_cosd (gfc_expr
*x
)
1903 if (x
->expr_type
!= EXPR_CONSTANT
)
1906 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1907 mpfr_set (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1908 simplify_cosd (result
->value
.real
);
1910 return range_check (result
, "COSD");
1914 /* Simplify SIND(X) where X has the unit of degree. */
1917 gfc_simplify_sind (gfc_expr
*x
)
1921 if (x
->expr_type
!= EXPR_CONSTANT
)
1924 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1925 mpfr_set (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1926 simplify_sind (result
->value
.real
);
1928 return range_check (result
, "SIND");
1932 /* Simplify TAND(X) where X has the unit of degree. */
1935 gfc_simplify_tand (gfc_expr
*x
)
1939 if (x
->expr_type
!= EXPR_CONSTANT
)
1942 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1943 mpfr_set (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1944 simplify_tand (result
->value
.real
);
1946 return range_check (result
, "TAND");
1950 /* Simplify COTAND(X) where X has the unit of degree. */
1953 gfc_simplify_cotand (gfc_expr
*x
)
1957 if (x
->expr_type
!= EXPR_CONSTANT
)
1960 /* Implement COTAND = -TAND(x+90).
1961 TAND offers correct exact values for multiples of 30 degrees.
1962 This implementation is also compatible with the behavior of some legacy
1963 compilers. Keep this consistent with gfc_conv_intrinsic_cotand. */
1964 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1965 mpfr_set (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1966 mpfr_add_ui (result
->value
.real
, result
->value
.real
, 90, GFC_RND_MODE
);
1967 simplify_tand (result
->value
.real
);
1968 mpfr_neg (result
->value
.real
, result
->value
.real
, GFC_RND_MODE
);
1970 return range_check (result
, "COTAND");
1975 gfc_simplify_cosh (gfc_expr
*x
)
1979 if (x
->expr_type
!= EXPR_CONSTANT
)
1982 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1987 mpfr_cosh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1991 mpc_cosh (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1998 return range_check (result
, "COSH");
2003 gfc_simplify_count (gfc_expr
*mask
, gfc_expr
*dim
, gfc_expr
*kind
)
2008 size_zero
= gfc_is_size_zero_array (mask
);
2010 if (!(is_constant_array_expr (mask
) || size_zero
)
2011 || !gfc_is_constant_expr (dim
)
2012 || !gfc_is_constant_expr (kind
))
2015 result
= transformational_result (mask
, dim
,
2017 get_kind (BT_INTEGER
, kind
, "COUNT",
2018 gfc_default_integer_kind
),
2021 init_result_expr (result
, 0, NULL
);
2026 /* Passing MASK twice, once as data array, once as mask.
2027 Whenever gfc_count is called, '1' is added to the result. */
2028 return !dim
|| mask
->rank
== 1 ?
2029 simplify_transformation_to_scalar (result
, mask
, mask
, gfc_count
) :
2030 simplify_transformation_to_array (result
, mask
, dim
, mask
, gfc_count
, NULL
);
2033 /* Simplification routine for cshift. This works by copying the array
2034 expressions into a one-dimensional array, shuffling the values into another
2035 one-dimensional array and creating the new array expression from this. The
2036 shuffling part is basically taken from the library routine. */
2039 gfc_simplify_cshift (gfc_expr
*array
, gfc_expr
*shift
, gfc_expr
*dim
)
2043 gfc_expr
**arrayvec
, **resultvec
;
2044 gfc_expr
**rptr
, **sptr
;
2046 size_t arraysize
, shiftsize
, i
;
2047 gfc_constructor
*array_ctor
, *shift_ctor
;
2048 ssize_t
*shiftvec
, *hptr
;
2049 ssize_t shift_val
, len
;
2050 ssize_t count
[GFC_MAX_DIMENSIONS
], extent
[GFC_MAX_DIMENSIONS
],
2051 hs_ex
[GFC_MAX_DIMENSIONS
+ 1],
2052 hstride
[GFC_MAX_DIMENSIONS
], sstride
[GFC_MAX_DIMENSIONS
],
2053 a_extent
[GFC_MAX_DIMENSIONS
], a_stride
[GFC_MAX_DIMENSIONS
],
2054 h_extent
[GFC_MAX_DIMENSIONS
],
2055 ss_ex
[GFC_MAX_DIMENSIONS
+ 1];
2059 gfc_expr
**src
, **dest
;
2061 if (!is_constant_array_expr (array
))
2064 if (shift
->rank
> 0)
2065 gfc_simplify_expr (shift
, 1);
2067 if (!gfc_is_constant_expr (shift
))
2070 /* Make dim zero-based. */
2073 if (!gfc_is_constant_expr (dim
))
2075 which
= mpz_get_si (dim
->value
.integer
) - 1;
2080 gfc_array_size (array
, &size
);
2081 arraysize
= mpz_get_ui (size
);
2084 result
= gfc_get_array_expr (array
->ts
.type
, array
->ts
.kind
, &array
->where
);
2085 result
->shape
= gfc_copy_shape (array
->shape
, array
->rank
);
2086 result
->rank
= array
->rank
;
2087 result
->ts
.u
.derived
= array
->ts
.u
.derived
;
2092 arrayvec
= XCNEWVEC (gfc_expr
*, arraysize
);
2093 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
2094 for (i
= 0; i
< arraysize
; i
++)
2096 arrayvec
[i
] = array_ctor
->expr
;
2097 array_ctor
= gfc_constructor_next (array_ctor
);
2100 resultvec
= XCNEWVEC (gfc_expr
*, arraysize
);
2105 for (d
=0; d
< array
->rank
; d
++)
2107 a_extent
[d
] = mpz_get_si (array
->shape
[d
]);
2108 a_stride
[d
] = d
== 0 ? 1 : a_stride
[d
-1] * a_extent
[d
-1];
2111 if (shift
->rank
> 0)
2113 gfc_array_size (shift
, &size
);
2114 shiftsize
= mpz_get_ui (size
);
2116 shiftvec
= XCNEWVEC (ssize_t
, shiftsize
);
2117 shift_ctor
= gfc_constructor_first (shift
->value
.constructor
);
2118 for (d
= 0; d
< shift
->rank
; d
++)
2120 h_extent
[d
] = mpz_get_si (shift
->shape
[d
]);
2121 hstride
[d
] = d
== 0 ? 1 : hstride
[d
-1] * h_extent
[d
-1];
2127 /* Shut up compiler */
2132 for (d
=0; d
< array
->rank
; d
++)
2136 rsoffset
= a_stride
[d
];
2142 extent
[n
] = a_extent
[d
];
2143 sstride
[n
] = a_stride
[d
];
2144 ss_ex
[n
] = sstride
[n
] * extent
[n
];
2146 hs_ex
[n
] = hstride
[n
] * extent
[n
];
2155 for (i
= 0; i
< shiftsize
; i
++)
2158 val
= mpz_get_si (shift_ctor
->expr
->value
.integer
);
2163 shift_ctor
= gfc_constructor_next (shift_ctor
);
2169 shift_val
= mpz_get_si (shift
->value
.integer
);
2170 shift_val
= shift_val
% len
;
2175 continue_loop
= true;
2181 while (continue_loop
)
2189 src
= &sptr
[sh
* rsoffset
];
2191 for (n
= 0; n
< len
- sh
; n
++)
2198 for ( n
= 0; n
< sh
; n
++)
2210 while (count
[n
] == extent
[n
])
2220 continue_loop
= false;
2234 for (i
= 0; i
< arraysize
; i
++)
2236 gfc_constructor_append_expr (&result
->value
.constructor
,
2237 gfc_copy_expr (resultvec
[i
]),
2245 gfc_simplify_dcmplx (gfc_expr
*x
, gfc_expr
*y
)
2247 return simplify_cmplx ("DCMPLX", x
, y
, gfc_default_double_kind
);
2252 gfc_simplify_dble (gfc_expr
*e
)
2254 gfc_expr
*result
= NULL
;
2257 if (e
->expr_type
!= EXPR_CONSTANT
)
2260 /* For explicit conversion, turn off -Wconversion and -Wconversion-extra
2262 tmp1
= warn_conversion
;
2263 tmp2
= warn_conversion_extra
;
2264 warn_conversion
= warn_conversion_extra
= 0;
2266 result
= gfc_convert_constant (e
, BT_REAL
, gfc_default_double_kind
);
2268 warn_conversion
= tmp1
;
2269 warn_conversion_extra
= tmp2
;
2271 if (result
== &gfc_bad_expr
)
2272 return &gfc_bad_expr
;
2274 return range_check (result
, "DBLE");
2279 gfc_simplify_digits (gfc_expr
*x
)
2283 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
2288 digits
= gfc_integer_kinds
[i
].digits
;
2293 digits
= gfc_real_kinds
[i
].digits
;
2300 return gfc_get_int_expr (gfc_default_integer_kind
, NULL
, digits
);
2305 gfc_simplify_dim (gfc_expr
*x
, gfc_expr
*y
)
2310 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
2313 kind
= x
->ts
.kind
> y
->ts
.kind
? x
->ts
.kind
: y
->ts
.kind
;
2314 result
= gfc_get_constant_expr (x
->ts
.type
, kind
, &x
->where
);
2319 if (mpz_cmp (x
->value
.integer
, y
->value
.integer
) > 0)
2320 mpz_sub (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
2322 mpz_set_ui (result
->value
.integer
, 0);
2327 if (mpfr_cmp (x
->value
.real
, y
->value
.real
) > 0)
2328 mpfr_sub (result
->value
.real
, x
->value
.real
, y
->value
.real
,
2331 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
2336 gfc_internal_error ("gfc_simplify_dim(): Bad type");
2339 return range_check (result
, "DIM");
2344 gfc_simplify_dot_product (gfc_expr
*vector_a
, gfc_expr
*vector_b
)
2346 /* If vector_a is a zero-sized array, the result is 0 for INTEGER,
2347 REAL, and COMPLEX types and .false. for LOGICAL. */
2348 if (vector_a
->shape
&& mpz_get_si (vector_a
->shape
[0]) == 0)
2350 if (vector_a
->ts
.type
== BT_LOGICAL
)
2351 return gfc_get_logical_expr (gfc_default_logical_kind
, NULL
, false);
2353 return gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 0);
2356 if (!is_constant_array_expr (vector_a
)
2357 || !is_constant_array_expr (vector_b
))
2360 return compute_dot_product (vector_a
, 1, 0, vector_b
, 1, 0, true);
2365 gfc_simplify_dprod (gfc_expr
*x
, gfc_expr
*y
)
2367 gfc_expr
*a1
, *a2
, *result
;
2369 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
2372 a1
= gfc_real2real (x
, gfc_default_double_kind
);
2373 a2
= gfc_real2real (y
, gfc_default_double_kind
);
2375 result
= gfc_get_constant_expr (BT_REAL
, gfc_default_double_kind
, &x
->where
);
2376 mpfr_mul (result
->value
.real
, a1
->value
.real
, a2
->value
.real
, GFC_RND_MODE
);
2381 return range_check (result
, "DPROD");
2386 simplify_dshift (gfc_expr
*arg1
, gfc_expr
*arg2
, gfc_expr
*shiftarg
,
2390 int i
, k
, size
, shift
;
2392 if (arg1
->expr_type
!= EXPR_CONSTANT
|| arg2
->expr_type
!= EXPR_CONSTANT
2393 || shiftarg
->expr_type
!= EXPR_CONSTANT
)
2396 k
= gfc_validate_kind (BT_INTEGER
, arg1
->ts
.kind
, false);
2397 size
= gfc_integer_kinds
[k
].bit_size
;
2399 gfc_extract_int (shiftarg
, &shift
);
2401 /* DSHIFTR(I,J,SHIFT) = DSHIFTL(I,J,SIZE-SHIFT). */
2403 shift
= size
- shift
;
2405 result
= gfc_get_constant_expr (BT_INTEGER
, arg1
->ts
.kind
, &arg1
->where
);
2406 mpz_set_ui (result
->value
.integer
, 0);
2408 for (i
= 0; i
< shift
; i
++)
2409 if (mpz_tstbit (arg2
->value
.integer
, size
- shift
+ i
))
2410 mpz_setbit (result
->value
.integer
, i
);
2412 for (i
= 0; i
< size
- shift
; i
++)
2413 if (mpz_tstbit (arg1
->value
.integer
, i
))
2414 mpz_setbit (result
->value
.integer
, shift
+ i
);
2416 /* Convert to a signed value. */
2417 gfc_convert_mpz_to_signed (result
->value
.integer
, size
);
2424 gfc_simplify_dshiftr (gfc_expr
*arg1
, gfc_expr
*arg2
, gfc_expr
*shiftarg
)
2426 return simplify_dshift (arg1
, arg2
, shiftarg
, true);
2431 gfc_simplify_dshiftl (gfc_expr
*arg1
, gfc_expr
*arg2
, gfc_expr
*shiftarg
)
2433 return simplify_dshift (arg1
, arg2
, shiftarg
, false);
2438 gfc_simplify_eoshift (gfc_expr
*array
, gfc_expr
*shift
, gfc_expr
*boundary
,
2445 gfc_expr
**arrayvec
, **resultvec
;
2446 gfc_expr
**rptr
, **sptr
;
2448 size_t arraysize
, i
;
2449 gfc_constructor
*array_ctor
, *shift_ctor
, *bnd_ctor
;
2450 ssize_t shift_val
, len
;
2451 ssize_t count
[GFC_MAX_DIMENSIONS
], extent
[GFC_MAX_DIMENSIONS
],
2452 sstride
[GFC_MAX_DIMENSIONS
], a_extent
[GFC_MAX_DIMENSIONS
],
2453 a_stride
[GFC_MAX_DIMENSIONS
], ss_ex
[GFC_MAX_DIMENSIONS
+ 1];
2457 gfc_expr
**src
, **dest
;
2460 if (!is_constant_array_expr (array
))
2463 if (shift
->rank
> 0)
2464 gfc_simplify_expr (shift
, 1);
2466 if (!gfc_is_constant_expr (shift
))
2471 if (boundary
->rank
> 0)
2472 gfc_simplify_expr (boundary
, 1);
2474 if (!gfc_is_constant_expr (boundary
))
2480 if (!gfc_is_constant_expr (dim
))
2482 which
= mpz_get_si (dim
->value
.integer
) - 1;
2488 if (boundary
== NULL
)
2490 temp_boundary
= true;
2491 switch (array
->ts
.type
)
2495 bnd
= gfc_get_int_expr (array
->ts
.kind
, NULL
, 0);
2499 bnd
= gfc_get_logical_expr (array
->ts
.kind
, NULL
, 0);
2503 bnd
= gfc_get_constant_expr (array
->ts
.type
, array
->ts
.kind
, &gfc_current_locus
);
2504 mpfr_set_ui (bnd
->value
.real
, 0, GFC_RND_MODE
);
2508 bnd
= gfc_get_constant_expr (array
->ts
.type
, array
->ts
.kind
, &gfc_current_locus
);
2509 mpc_set_ui (bnd
->value
.complex, 0, GFC_RND_MODE
);
2513 s_len
= mpz_get_ui (array
->ts
.u
.cl
->length
->value
.integer
);
2514 bnd
= gfc_get_character_expr (array
->ts
.kind
, &gfc_current_locus
, NULL
, s_len
);
2524 temp_boundary
= false;
2528 gfc_array_size (array
, &size
);
2529 arraysize
= mpz_get_ui (size
);
2532 result
= gfc_get_array_expr (array
->ts
.type
, array
->ts
.kind
, &array
->where
);
2533 result
->shape
= gfc_copy_shape (array
->shape
, array
->rank
);
2534 result
->rank
= array
->rank
;
2535 result
->ts
= array
->ts
;
2540 arrayvec
= XCNEWVEC (gfc_expr
*, arraysize
);
2541 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
2542 for (i
= 0; i
< arraysize
; i
++)
2544 arrayvec
[i
] = array_ctor
->expr
;
2545 array_ctor
= gfc_constructor_next (array_ctor
);
2548 resultvec
= XCNEWVEC (gfc_expr
*, arraysize
);
2553 for (d
=0; d
< array
->rank
; d
++)
2555 a_extent
[d
] = mpz_get_si (array
->shape
[d
]);
2556 a_stride
[d
] = d
== 0 ? 1 : a_stride
[d
-1] * a_extent
[d
-1];
2559 if (shift
->rank
> 0)
2561 shift_ctor
= gfc_constructor_first (shift
->value
.constructor
);
2567 shift_val
= mpz_get_si (shift
->value
.integer
);
2571 bnd_ctor
= gfc_constructor_first (bnd
->value
.constructor
);
2575 /* Shut up compiler */
2580 for (d
=0; d
< array
->rank
; d
++)
2584 rsoffset
= a_stride
[d
];
2590 extent
[n
] = a_extent
[d
];
2591 sstride
[n
] = a_stride
[d
];
2592 ss_ex
[n
] = sstride
[n
] * extent
[n
];
2598 continue_loop
= true;
2603 while (continue_loop
)
2608 sh
= mpz_get_si (shift_ctor
->expr
->value
.integer
);
2612 if (( sh
>= 0 ? sh
: -sh
) > len
)
2618 delta
= (sh
>= 0) ? sh
: -sh
;
2622 src
= &sptr
[delta
* rsoffset
];
2628 dest
= &rptr
[delta
* rsoffset
];
2631 for (n
= 0; n
< len
- delta
; n
++)
2647 *dest
= gfc_copy_expr (bnd_ctor
->expr
);
2655 *dest
= gfc_copy_expr (bnd
);
2662 shift_ctor
= gfc_constructor_next (shift_ctor
);
2665 bnd_ctor
= gfc_constructor_next (bnd_ctor
);
2669 while (count
[n
] == extent
[n
])
2677 continue_loop
= false;
2689 for (i
= 0; i
< arraysize
; i
++)
2691 gfc_constructor_append_expr (&result
->value
.constructor
,
2692 gfc_copy_expr (resultvec
[i
]),
2698 gfc_free_expr (bnd
);
2704 gfc_simplify_erf (gfc_expr
*x
)
2708 if (x
->expr_type
!= EXPR_CONSTANT
)
2711 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2712 mpfr_erf (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
2714 return range_check (result
, "ERF");
2719 gfc_simplify_erfc (gfc_expr
*x
)
2723 if (x
->expr_type
!= EXPR_CONSTANT
)
2726 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2727 mpfr_erfc (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
2729 return range_check (result
, "ERFC");
2733 /* Helper functions to simplify ERFC_SCALED(x) = ERFC(x) * EXP(X**2). */
2735 #define MAX_ITER 200
2736 #define ARG_LIMIT 12
2738 /* Calculate ERFC_SCALED directly by its definition:
2740 ERFC_SCALED(x) = ERFC(x) * EXP(X**2)
2742 using a large precision for intermediate results. This is used for all
2743 but large values of the argument. */
2745 fullprec_erfc_scaled (mpfr_t res
, mpfr_t arg
)
2750 prec
= mpfr_get_default_prec ();
2751 mpfr_set_default_prec (10 * prec
);
2756 mpfr_set (a
, arg
, GFC_RND_MODE
);
2757 mpfr_sqr (b
, a
, GFC_RND_MODE
);
2758 mpfr_exp (b
, b
, GFC_RND_MODE
);
2759 mpfr_erfc (a
, a
, GFC_RND_MODE
);
2760 mpfr_mul (a
, a
, b
, GFC_RND_MODE
);
2762 mpfr_set (res
, a
, GFC_RND_MODE
);
2763 mpfr_set_default_prec (prec
);
2769 /* Calculate ERFC_SCALED using a power series expansion in 1/arg:
2771 ERFC_SCALED(x) = 1 / (x * sqrt(pi))
2772 * (1 + Sum_n (-1)**n * (1 * 3 * 5 * ... * (2n-1))
2775 This is used for large values of the argument. Intermediate calculations
2776 are performed with twice the precision. We don't do a fixed number of
2777 iterations of the sum, but stop when it has converged to the required
2780 asympt_erfc_scaled (mpfr_t res
, mpfr_t arg
)
2782 mpfr_t sum
, x
, u
, v
, w
, oldsum
, sumtrunc
;
2787 prec
= mpfr_get_default_prec ();
2788 mpfr_set_default_prec (2 * prec
);
2798 mpfr_init (sumtrunc
);
2799 mpfr_set_prec (oldsum
, prec
);
2800 mpfr_set_prec (sumtrunc
, prec
);
2802 mpfr_set (x
, arg
, GFC_RND_MODE
);
2803 mpfr_set_ui (sum
, 1, GFC_RND_MODE
);
2804 mpz_set_ui (num
, 1);
2806 mpfr_set (u
, x
, GFC_RND_MODE
);
2807 mpfr_sqr (u
, u
, GFC_RND_MODE
);
2808 mpfr_mul_ui (u
, u
, 2, GFC_RND_MODE
);
2809 mpfr_pow_si (u
, u
, -1, GFC_RND_MODE
);
2811 for (i
= 1; i
< MAX_ITER
; i
++)
2813 mpfr_set (oldsum
, sum
, GFC_RND_MODE
);
2815 mpz_mul_ui (num
, num
, 2 * i
- 1);
2818 mpfr_set (w
, u
, GFC_RND_MODE
);
2819 mpfr_pow_ui (w
, w
, i
, GFC_RND_MODE
);
2821 mpfr_set_z (v
, num
, GFC_RND_MODE
);
2822 mpfr_mul (v
, v
, w
, GFC_RND_MODE
);
2824 mpfr_add (sum
, sum
, v
, GFC_RND_MODE
);
2826 mpfr_set (sumtrunc
, sum
, GFC_RND_MODE
);
2827 if (mpfr_cmp (sumtrunc
, oldsum
) == 0)
2831 /* We should have converged by now; otherwise, ARG_LIMIT is probably
2833 gcc_assert (i
< MAX_ITER
);
2835 /* Divide by x * sqrt(Pi). */
2836 mpfr_const_pi (u
, GFC_RND_MODE
);
2837 mpfr_sqrt (u
, u
, GFC_RND_MODE
);
2838 mpfr_mul (u
, u
, x
, GFC_RND_MODE
);
2839 mpfr_div (sum
, sum
, u
, GFC_RND_MODE
);
2841 mpfr_set (res
, sum
, GFC_RND_MODE
);
2842 mpfr_set_default_prec (prec
);
2844 mpfr_clears (sum
, x
, u
, v
, w
, oldsum
, sumtrunc
, NULL
);
2850 gfc_simplify_erfc_scaled (gfc_expr
*x
)
2854 if (x
->expr_type
!= EXPR_CONSTANT
)
2857 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2858 if (mpfr_cmp_d (x
->value
.real
, ARG_LIMIT
) >= 0)
2859 asympt_erfc_scaled (result
->value
.real
, x
->value
.real
);
2861 fullprec_erfc_scaled (result
->value
.real
, x
->value
.real
);
2863 return range_check (result
, "ERFC_SCALED");
2871 gfc_simplify_epsilon (gfc_expr
*e
)
2876 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
2878 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
2879 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].epsilon
, GFC_RND_MODE
);
2881 return range_check (result
, "EPSILON");
2886 gfc_simplify_exp (gfc_expr
*x
)
2890 if (x
->expr_type
!= EXPR_CONSTANT
)
2893 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2898 mpfr_exp (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
2902 gfc_set_model_kind (x
->ts
.kind
);
2903 mpc_exp (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
2907 gfc_internal_error ("in gfc_simplify_exp(): Bad type");
2910 return range_check (result
, "EXP");
2915 gfc_simplify_exponent (gfc_expr
*x
)
2920 if (x
->expr_type
!= EXPR_CONSTANT
)
2923 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
2926 /* EXPONENT(inf) = EXPONENT(nan) = HUGE(0) */
2927 if (mpfr_inf_p (x
->value
.real
) || mpfr_nan_p (x
->value
.real
))
2929 int i
= gfc_validate_kind (BT_INTEGER
, gfc_default_integer_kind
, false);
2930 mpz_set (result
->value
.integer
, gfc_integer_kinds
[i
].huge
);
2934 /* EXPONENT(+/- 0.0) = 0 */
2935 if (mpfr_zero_p (x
->value
.real
))
2937 mpz_set_ui (result
->value
.integer
, 0);
2941 gfc_set_model (x
->value
.real
);
2943 val
= (long int) mpfr_get_exp (x
->value
.real
);
2944 mpz_set_si (result
->value
.integer
, val
);
2946 return range_check (result
, "EXPONENT");
2951 gfc_simplify_failed_or_stopped_images (gfc_expr
*team ATTRIBUTE_UNUSED
,
2954 if (flag_coarray
== GFC_FCOARRAY_NONE
)
2956 gfc_current_locus
= *gfc_current_intrinsic_where
;
2957 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
2958 return &gfc_bad_expr
;
2961 if (flag_coarray
== GFC_FCOARRAY_SINGLE
)
2966 gfc_extract_int (kind
, &actual_kind
);
2968 actual_kind
= gfc_default_integer_kind
;
2970 result
= gfc_get_array_expr (BT_INTEGER
, actual_kind
, &gfc_current_locus
);
2975 /* For fcoarray = lib no simplification is possible, because it is not known
2976 what images failed or are stopped at compile time. */
2982 gfc_simplify_get_team (gfc_expr
*level ATTRIBUTE_UNUSED
)
2984 if (flag_coarray
== GFC_FCOARRAY_NONE
)
2986 gfc_current_locus
= *gfc_current_intrinsic_where
;
2987 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
2988 return &gfc_bad_expr
;
2991 if (flag_coarray
== GFC_FCOARRAY_SINGLE
)
2994 result
= gfc_get_array_expr (BT_INTEGER
, gfc_default_integer_kind
, &gfc_current_locus
);
2999 /* For fcoarray = lib no simplification is possible, because it is not known
3000 what images failed or are stopped at compile time. */
3006 gfc_simplify_float (gfc_expr
*a
)
3010 if (a
->expr_type
!= EXPR_CONSTANT
)
3013 result
= gfc_int2real (a
, gfc_default_real_kind
);
3015 return range_check (result
, "FLOAT");
3020 is_last_ref_vtab (gfc_expr
*e
)
3023 gfc_component
*comp
= NULL
;
3025 if (e
->expr_type
!= EXPR_VARIABLE
)
3028 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
3029 if (ref
->type
== REF_COMPONENT
)
3030 comp
= ref
->u
.c
.component
;
3032 if (!e
->ref
|| !comp
)
3033 return e
->symtree
->n
.sym
->attr
.vtab
;
3035 if (comp
->name
[0] == '_' && strcmp (comp
->name
, "_vptr") == 0)
3043 gfc_simplify_extends_type_of (gfc_expr
*a
, gfc_expr
*mold
)
3045 /* Avoid simplification of resolved symbols. */
3046 if (is_last_ref_vtab (a
) || is_last_ref_vtab (mold
))
3049 if (a
->ts
.type
== BT_DERIVED
&& mold
->ts
.type
== BT_DERIVED
)
3050 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
,
3051 gfc_type_is_extension_of (mold
->ts
.u
.derived
,
3054 if (UNLIMITED_POLY (a
) || UNLIMITED_POLY (mold
))
3057 /* Return .false. if the dynamic type can never be an extension. */
3058 if ((a
->ts
.type
== BT_CLASS
&& mold
->ts
.type
== BT_CLASS
3059 && !gfc_type_is_extension_of
3060 (mold
->ts
.u
.derived
->components
->ts
.u
.derived
,
3061 a
->ts
.u
.derived
->components
->ts
.u
.derived
)
3062 && !gfc_type_is_extension_of
3063 (a
->ts
.u
.derived
->components
->ts
.u
.derived
,
3064 mold
->ts
.u
.derived
->components
->ts
.u
.derived
))
3065 || (a
->ts
.type
== BT_DERIVED
&& mold
->ts
.type
== BT_CLASS
3066 && !gfc_type_is_extension_of
3067 (mold
->ts
.u
.derived
->components
->ts
.u
.derived
,
3069 || (a
->ts
.type
== BT_CLASS
&& mold
->ts
.type
== BT_DERIVED
3070 && !gfc_type_is_extension_of
3071 (mold
->ts
.u
.derived
,
3072 a
->ts
.u
.derived
->components
->ts
.u
.derived
)
3073 && !gfc_type_is_extension_of
3074 (a
->ts
.u
.derived
->components
->ts
.u
.derived
,
3075 mold
->ts
.u
.derived
)))
3076 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
, false);
3078 /* Return .true. if the dynamic type is guaranteed to be an extension. */
3079 if (a
->ts
.type
== BT_CLASS
&& mold
->ts
.type
== BT_DERIVED
3080 && gfc_type_is_extension_of (mold
->ts
.u
.derived
,
3081 a
->ts
.u
.derived
->components
->ts
.u
.derived
))
3082 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
, true);
3089 gfc_simplify_same_type_as (gfc_expr
*a
, gfc_expr
*b
)
3091 /* Avoid simplification of resolved symbols. */
3092 if (is_last_ref_vtab (a
) || is_last_ref_vtab (b
))
3095 /* Return .false. if the dynamic type can never be the
3097 if (((a
->ts
.type
== BT_CLASS
&& gfc_expr_attr (a
).class_ok
)
3098 || (b
->ts
.type
== BT_CLASS
&& gfc_expr_attr (b
).class_ok
))
3099 && !gfc_type_compatible (&a
->ts
, &b
->ts
)
3100 && !gfc_type_compatible (&b
->ts
, &a
->ts
))
3101 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
, false);
3103 if (a
->ts
.type
!= BT_DERIVED
|| b
->ts
.type
!= BT_DERIVED
)
3106 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
,
3107 gfc_compare_derived_types (a
->ts
.u
.derived
,
3113 gfc_simplify_floor (gfc_expr
*e
, gfc_expr
*k
)
3119 kind
= get_kind (BT_INTEGER
, k
, "FLOOR", gfc_default_integer_kind
);
3121 gfc_internal_error ("gfc_simplify_floor(): Bad kind");
3123 if (e
->expr_type
!= EXPR_CONSTANT
)
3126 mpfr_init2 (floor
, mpfr_get_prec (e
->value
.real
));
3127 mpfr_floor (floor
, e
->value
.real
);
3129 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &e
->where
);
3130 gfc_mpfr_to_mpz (result
->value
.integer
, floor
, &e
->where
);
3134 return range_check (result
, "FLOOR");
3139 gfc_simplify_fraction (gfc_expr
*x
)
3144 if (x
->expr_type
!= EXPR_CONSTANT
)
3147 result
= gfc_get_constant_expr (BT_REAL
, x
->ts
.kind
, &x
->where
);
3149 /* FRACTION(inf) = NaN. */
3150 if (mpfr_inf_p (x
->value
.real
))
3152 mpfr_set_nan (result
->value
.real
);
3156 /* mpfr_frexp() correctly handles zeros and NaNs. */
3157 mpfr_frexp (&e
, result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
3159 return range_check (result
, "FRACTION");
3164 gfc_simplify_gamma (gfc_expr
*x
)
3168 if (x
->expr_type
!= EXPR_CONSTANT
)
3171 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3172 mpfr_gamma (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
3174 return range_check (result
, "GAMMA");
3179 gfc_simplify_huge (gfc_expr
*e
)
3184 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
3185 result
= gfc_get_constant_expr (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
3190 mpz_set (result
->value
.integer
, gfc_integer_kinds
[i
].huge
);
3194 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].huge
, GFC_RND_MODE
);
3206 gfc_simplify_hypot (gfc_expr
*x
, gfc_expr
*y
)
3210 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
3213 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3214 mpfr_hypot (result
->value
.real
, x
->value
.real
, y
->value
.real
, GFC_RND_MODE
);
3215 return range_check (result
, "HYPOT");
3219 /* We use the processor's collating sequence, because all
3220 systems that gfortran currently works on are ASCII. */
3223 gfc_simplify_iachar (gfc_expr
*e
, gfc_expr
*kind
)
3229 if (e
->expr_type
!= EXPR_CONSTANT
)
3232 if (e
->value
.character
.length
!= 1)
3234 gfc_error ("Argument of IACHAR at %L must be of length one", &e
->where
);
3235 return &gfc_bad_expr
;
3238 index
= e
->value
.character
.string
[0];
3240 if (warn_surprising
&& index
> 127)
3241 gfc_warning (OPT_Wsurprising
,
3242 "Argument of IACHAR function at %L outside of range 0..127",
3245 k
= get_kind (BT_INTEGER
, kind
, "IACHAR", gfc_default_integer_kind
);
3247 return &gfc_bad_expr
;
3249 result
= gfc_get_int_expr (k
, &e
->where
, index
);
3251 return range_check (result
, "IACHAR");
3256 do_bit_and (gfc_expr
*result
, gfc_expr
*e
)
3258 gcc_assert (e
->ts
.type
== BT_INTEGER
&& e
->expr_type
== EXPR_CONSTANT
);
3259 gcc_assert (result
->ts
.type
== BT_INTEGER
3260 && result
->expr_type
== EXPR_CONSTANT
);
3262 mpz_and (result
->value
.integer
, result
->value
.integer
, e
->value
.integer
);
3268 gfc_simplify_iall (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
)
3270 return simplify_transformation (array
, dim
, mask
, -1, do_bit_and
);
3275 do_bit_ior (gfc_expr
*result
, gfc_expr
*e
)
3277 gcc_assert (e
->ts
.type
== BT_INTEGER
&& e
->expr_type
== EXPR_CONSTANT
);
3278 gcc_assert (result
->ts
.type
== BT_INTEGER
3279 && result
->expr_type
== EXPR_CONSTANT
);
3281 mpz_ior (result
->value
.integer
, result
->value
.integer
, e
->value
.integer
);
3287 gfc_simplify_iany (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
)
3289 return simplify_transformation (array
, dim
, mask
, 0, do_bit_ior
);
3294 gfc_simplify_iand (gfc_expr
*x
, gfc_expr
*y
)
3298 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
3301 result
= gfc_get_constant_expr (BT_INTEGER
, x
->ts
.kind
, &x
->where
);
3302 mpz_and (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
3304 return range_check (result
, "IAND");
3309 gfc_simplify_ibclr (gfc_expr
*x
, gfc_expr
*y
)
3314 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
3317 gfc_extract_int (y
, &pos
);
3319 k
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
3321 result
= gfc_copy_expr (x
);
3323 convert_mpz_to_unsigned (result
->value
.integer
,
3324 gfc_integer_kinds
[k
].bit_size
);
3326 mpz_clrbit (result
->value
.integer
, pos
);
3328 gfc_convert_mpz_to_signed (result
->value
.integer
,
3329 gfc_integer_kinds
[k
].bit_size
);
3336 gfc_simplify_ibits (gfc_expr
*x
, gfc_expr
*y
, gfc_expr
*z
)
3343 if (x
->expr_type
!= EXPR_CONSTANT
3344 || y
->expr_type
!= EXPR_CONSTANT
3345 || z
->expr_type
!= EXPR_CONSTANT
)
3348 gfc_extract_int (y
, &pos
);
3349 gfc_extract_int (z
, &len
);
3351 k
= gfc_validate_kind (BT_INTEGER
, x
->ts
.kind
, false);
3353 bitsize
= gfc_integer_kinds
[k
].bit_size
;
3355 if (pos
+ len
> bitsize
)
3357 gfc_error ("Sum of second and third arguments of IBITS exceeds "
3358 "bit size at %L", &y
->where
);
3359 return &gfc_bad_expr
;
3362 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3363 convert_mpz_to_unsigned (result
->value
.integer
,
3364 gfc_integer_kinds
[k
].bit_size
);
3366 bits
= XCNEWVEC (int, bitsize
);
3368 for (i
= 0; i
< bitsize
; i
++)
3371 for (i
= 0; i
< len
; i
++)
3372 bits
[i
] = mpz_tstbit (x
->value
.integer
, i
+ pos
);
3374 for (i
= 0; i
< bitsize
; i
++)
3377 mpz_clrbit (result
->value
.integer
, i
);
3378 else if (bits
[i
] == 1)
3379 mpz_setbit (result
->value
.integer
, i
);
3381 gfc_internal_error ("IBITS: Bad bit");
3386 gfc_convert_mpz_to_signed (result
->value
.integer
,
3387 gfc_integer_kinds
[k
].bit_size
);
3394 gfc_simplify_ibset (gfc_expr
*x
, gfc_expr
*y
)
3399 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
3402 gfc_extract_int (y
, &pos
);
3404 k
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
3406 result
= gfc_copy_expr (x
);
3408 convert_mpz_to_unsigned (result
->value
.integer
,
3409 gfc_integer_kinds
[k
].bit_size
);
3411 mpz_setbit (result
->value
.integer
, pos
);
3413 gfc_convert_mpz_to_signed (result
->value
.integer
,
3414 gfc_integer_kinds
[k
].bit_size
);
3421 gfc_simplify_ichar (gfc_expr
*e
, gfc_expr
*kind
)
3427 if (e
->expr_type
!= EXPR_CONSTANT
)
3430 if (e
->value
.character
.length
!= 1)
3432 gfc_error ("Argument of ICHAR at %L must be of length one", &e
->where
);
3433 return &gfc_bad_expr
;
3436 index
= e
->value
.character
.string
[0];
3438 k
= get_kind (BT_INTEGER
, kind
, "ICHAR", gfc_default_integer_kind
);
3440 return &gfc_bad_expr
;
3442 result
= gfc_get_int_expr (k
, &e
->where
, index
);
3444 return range_check (result
, "ICHAR");
3449 gfc_simplify_ieor (gfc_expr
*x
, gfc_expr
*y
)
3453 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
3456 result
= gfc_get_constant_expr (BT_INTEGER
, x
->ts
.kind
, &x
->where
);
3457 mpz_xor (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
3459 return range_check (result
, "IEOR");
3464 gfc_simplify_index (gfc_expr
*x
, gfc_expr
*y
, gfc_expr
*b
, gfc_expr
*kind
)
3467 int back
, len
, lensub
;
3468 int i
, j
, k
, count
, index
= 0, start
;
3470 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
3471 || ( b
!= NULL
&& b
->expr_type
!= EXPR_CONSTANT
))
3474 if (b
!= NULL
&& b
->value
.logical
!= 0)
3479 k
= get_kind (BT_INTEGER
, kind
, "INDEX", gfc_default_integer_kind
);
3481 return &gfc_bad_expr
;
3483 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &x
->where
);
3485 len
= x
->value
.character
.length
;
3486 lensub
= y
->value
.character
.length
;
3490 mpz_set_si (result
->value
.integer
, 0);
3498 mpz_set_si (result
->value
.integer
, 1);
3501 else if (lensub
== 1)
3503 for (i
= 0; i
< len
; i
++)
3505 for (j
= 0; j
< lensub
; j
++)
3507 if (y
->value
.character
.string
[j
]
3508 == x
->value
.character
.string
[i
])
3518 for (i
= 0; i
< len
; i
++)
3520 for (j
= 0; j
< lensub
; j
++)
3522 if (y
->value
.character
.string
[j
]
3523 == x
->value
.character
.string
[i
])
3528 for (k
= 0; k
< lensub
; k
++)
3530 if (y
->value
.character
.string
[k
]
3531 == x
->value
.character
.string
[k
+ start
])
3535 if (count
== lensub
)
3550 mpz_set_si (result
->value
.integer
, len
+ 1);
3553 else if (lensub
== 1)
3555 for (i
= 0; i
< len
; i
++)
3557 for (j
= 0; j
< lensub
; j
++)
3559 if (y
->value
.character
.string
[j
]
3560 == x
->value
.character
.string
[len
- i
])
3562 index
= len
- i
+ 1;
3570 for (i
= 0; i
< len
; i
++)
3572 for (j
= 0; j
< lensub
; j
++)
3574 if (y
->value
.character
.string
[j
]
3575 == x
->value
.character
.string
[len
- i
])
3578 if (start
<= len
- lensub
)
3581 for (k
= 0; k
< lensub
; k
++)
3582 if (y
->value
.character
.string
[k
]
3583 == x
->value
.character
.string
[k
+ start
])
3586 if (count
== lensub
)
3603 mpz_set_si (result
->value
.integer
, index
);
3604 return range_check (result
, "INDEX");
3609 simplify_intconv (gfc_expr
*e
, int kind
, const char *name
)
3611 gfc_expr
*result
= NULL
;
3614 /* Convert BOZ to integer, and return without range checking. */
3615 if (e
->ts
.type
== BT_BOZ
)
3617 if (!gfc_boz2int (e
, kind
))
3619 result
= gfc_copy_expr (e
);
3623 if (e
->expr_type
!= EXPR_CONSTANT
)
3626 /* For explicit conversion, turn off -Wconversion and -Wconversion-extra
3628 tmp1
= warn_conversion
;
3629 tmp2
= warn_conversion_extra
;
3630 warn_conversion
= warn_conversion_extra
= 0;
3632 result
= gfc_convert_constant (e
, BT_INTEGER
, kind
);
3634 warn_conversion
= tmp1
;
3635 warn_conversion_extra
= tmp2
;
3637 if (result
== &gfc_bad_expr
)
3638 return &gfc_bad_expr
;
3640 return range_check (result
, name
);
3645 gfc_simplify_int (gfc_expr
*e
, gfc_expr
*k
)
3649 kind
= get_kind (BT_INTEGER
, k
, "INT", gfc_default_integer_kind
);
3651 return &gfc_bad_expr
;
3653 return simplify_intconv (e
, kind
, "INT");
3657 gfc_simplify_int2 (gfc_expr
*e
)
3659 return simplify_intconv (e
, 2, "INT2");
3664 gfc_simplify_int8 (gfc_expr
*e
)
3666 return simplify_intconv (e
, 8, "INT8");
3671 gfc_simplify_long (gfc_expr
*e
)
3673 return simplify_intconv (e
, 4, "LONG");
3678 gfc_simplify_ifix (gfc_expr
*e
)
3680 gfc_expr
*rtrunc
, *result
;
3682 if (e
->expr_type
!= EXPR_CONSTANT
)
3685 rtrunc
= gfc_copy_expr (e
);
3686 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
3688 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
3690 gfc_mpfr_to_mpz (result
->value
.integer
, rtrunc
->value
.real
, &e
->where
);
3692 gfc_free_expr (rtrunc
);
3694 return range_check (result
, "IFIX");
3699 gfc_simplify_idint (gfc_expr
*e
)
3701 gfc_expr
*rtrunc
, *result
;
3703 if (e
->expr_type
!= EXPR_CONSTANT
)
3706 rtrunc
= gfc_copy_expr (e
);
3707 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
3709 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
3711 gfc_mpfr_to_mpz (result
->value
.integer
, rtrunc
->value
.real
, &e
->where
);
3713 gfc_free_expr (rtrunc
);
3715 return range_check (result
, "IDINT");
3720 gfc_simplify_ior (gfc_expr
*x
, gfc_expr
*y
)
3724 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
3727 result
= gfc_get_constant_expr (BT_INTEGER
, x
->ts
.kind
, &x
->where
);
3728 mpz_ior (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
3730 return range_check (result
, "IOR");
3735 do_bit_xor (gfc_expr
*result
, gfc_expr
*e
)
3737 gcc_assert (e
->ts
.type
== BT_INTEGER
&& e
->expr_type
== EXPR_CONSTANT
);
3738 gcc_assert (result
->ts
.type
== BT_INTEGER
3739 && result
->expr_type
== EXPR_CONSTANT
);
3741 mpz_xor (result
->value
.integer
, result
->value
.integer
, e
->value
.integer
);
3747 gfc_simplify_iparity (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
)
3749 return simplify_transformation (array
, dim
, mask
, 0, do_bit_xor
);
3754 gfc_simplify_is_iostat_end (gfc_expr
*x
)
3756 if (x
->expr_type
!= EXPR_CONSTANT
)
3759 return gfc_get_logical_expr (gfc_default_logical_kind
, &x
->where
,
3760 mpz_cmp_si (x
->value
.integer
,
3761 LIBERROR_END
) == 0);
3766 gfc_simplify_is_iostat_eor (gfc_expr
*x
)
3768 if (x
->expr_type
!= EXPR_CONSTANT
)
3771 return gfc_get_logical_expr (gfc_default_logical_kind
, &x
->where
,
3772 mpz_cmp_si (x
->value
.integer
,
3773 LIBERROR_EOR
) == 0);
3778 gfc_simplify_isnan (gfc_expr
*x
)
3780 if (x
->expr_type
!= EXPR_CONSTANT
)
3783 return gfc_get_logical_expr (gfc_default_logical_kind
, &x
->where
,
3784 mpfr_nan_p (x
->value
.real
));
3788 /* Performs a shift on its first argument. Depending on the last
3789 argument, the shift can be arithmetic, i.e. with filling from the
3790 left like in the SHIFTA intrinsic. */
3792 simplify_shift (gfc_expr
*e
, gfc_expr
*s
, const char *name
,
3793 bool arithmetic
, int direction
)
3796 int ashift
, *bits
, i
, k
, bitsize
, shift
;
3798 if (e
->expr_type
!= EXPR_CONSTANT
|| s
->expr_type
!= EXPR_CONSTANT
)
3801 gfc_extract_int (s
, &shift
);
3803 k
= gfc_validate_kind (BT_INTEGER
, e
->ts
.kind
, false);
3804 bitsize
= gfc_integer_kinds
[k
].bit_size
;
3806 result
= gfc_get_constant_expr (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
3810 mpz_set (result
->value
.integer
, e
->value
.integer
);
3814 if (direction
> 0 && shift
< 0)
3816 /* Left shift, as in SHIFTL. */
3817 gfc_error ("Second argument of %s is negative at %L", name
, &e
->where
);
3818 return &gfc_bad_expr
;
3820 else if (direction
< 0)
3822 /* Right shift, as in SHIFTR or SHIFTA. */
3825 gfc_error ("Second argument of %s is negative at %L",
3827 return &gfc_bad_expr
;
3833 ashift
= (shift
>= 0 ? shift
: -shift
);
3835 if (ashift
> bitsize
)
3837 gfc_error ("Magnitude of second argument of %s exceeds bit size "
3838 "at %L", name
, &e
->where
);
3839 return &gfc_bad_expr
;
3842 bits
= XCNEWVEC (int, bitsize
);
3844 for (i
= 0; i
< bitsize
; i
++)
3845 bits
[i
] = mpz_tstbit (e
->value
.integer
, i
);
3850 for (i
= 0; i
< shift
; i
++)
3851 mpz_clrbit (result
->value
.integer
, i
);
3853 for (i
= 0; i
< bitsize
- shift
; i
++)
3856 mpz_clrbit (result
->value
.integer
, i
+ shift
);
3858 mpz_setbit (result
->value
.integer
, i
+ shift
);
3864 if (arithmetic
&& bits
[bitsize
- 1])
3865 for (i
= bitsize
- 1; i
>= bitsize
- ashift
; i
--)
3866 mpz_setbit (result
->value
.integer
, i
);
3868 for (i
= bitsize
- 1; i
>= bitsize
- ashift
; i
--)
3869 mpz_clrbit (result
->value
.integer
, i
);
3871 for (i
= bitsize
- 1; i
>= ashift
; i
--)
3874 mpz_clrbit (result
->value
.integer
, i
- ashift
);
3876 mpz_setbit (result
->value
.integer
, i
- ashift
);
3880 gfc_convert_mpz_to_signed (result
->value
.integer
, bitsize
);
3888 gfc_simplify_ishft (gfc_expr
*e
, gfc_expr
*s
)
3890 return simplify_shift (e
, s
, "ISHFT", false, 0);
3895 gfc_simplify_lshift (gfc_expr
*e
, gfc_expr
*s
)
3897 return simplify_shift (e
, s
, "LSHIFT", false, 1);
3902 gfc_simplify_rshift (gfc_expr
*e
, gfc_expr
*s
)
3904 return simplify_shift (e
, s
, "RSHIFT", true, -1);
3909 gfc_simplify_shifta (gfc_expr
*e
, gfc_expr
*s
)
3911 return simplify_shift (e
, s
, "SHIFTA", true, -1);
3916 gfc_simplify_shiftl (gfc_expr
*e
, gfc_expr
*s
)
3918 return simplify_shift (e
, s
, "SHIFTL", false, 1);
3923 gfc_simplify_shiftr (gfc_expr
*e
, gfc_expr
*s
)
3925 return simplify_shift (e
, s
, "SHIFTR", false, -1);
3930 gfc_simplify_ishftc (gfc_expr
*e
, gfc_expr
*s
, gfc_expr
*sz
)
3933 int shift
, ashift
, isize
, ssize
, delta
, k
;
3936 if (e
->expr_type
!= EXPR_CONSTANT
|| s
->expr_type
!= EXPR_CONSTANT
)
3939 gfc_extract_int (s
, &shift
);
3941 k
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
3942 isize
= gfc_integer_kinds
[k
].bit_size
;
3946 if (sz
->expr_type
!= EXPR_CONSTANT
)
3949 gfc_extract_int (sz
, &ssize
);
3962 gfc_error ("Magnitude of second argument of ISHFTC exceeds "
3963 "BIT_SIZE of first argument at %C");
3965 gfc_error ("Absolute value of SHIFT shall be less than or equal "
3967 return &gfc_bad_expr
;
3970 result
= gfc_get_constant_expr (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
3972 mpz_set (result
->value
.integer
, e
->value
.integer
);
3977 convert_mpz_to_unsigned (result
->value
.integer
, isize
);
3979 bits
= XCNEWVEC (int, ssize
);
3981 for (i
= 0; i
< ssize
; i
++)
3982 bits
[i
] = mpz_tstbit (e
->value
.integer
, i
);
3984 delta
= ssize
- ashift
;
3988 for (i
= 0; i
< delta
; i
++)
3991 mpz_clrbit (result
->value
.integer
, i
+ shift
);
3993 mpz_setbit (result
->value
.integer
, i
+ shift
);
3996 for (i
= delta
; i
< ssize
; i
++)
3999 mpz_clrbit (result
->value
.integer
, i
- delta
);
4001 mpz_setbit (result
->value
.integer
, i
- delta
);
4006 for (i
= 0; i
< ashift
; i
++)
4009 mpz_clrbit (result
->value
.integer
, i
+ delta
);
4011 mpz_setbit (result
->value
.integer
, i
+ delta
);
4014 for (i
= ashift
; i
< ssize
; i
++)
4017 mpz_clrbit (result
->value
.integer
, i
+ shift
);
4019 mpz_setbit (result
->value
.integer
, i
+ shift
);
4023 gfc_convert_mpz_to_signed (result
->value
.integer
, isize
);
4031 gfc_simplify_kind (gfc_expr
*e
)
4033 return gfc_get_int_expr (gfc_default_integer_kind
, NULL
, e
->ts
.kind
);
4038 simplify_bound_dim (gfc_expr
*array
, gfc_expr
*kind
, int d
, int upper
,
4039 gfc_array_spec
*as
, gfc_ref
*ref
, bool coarray
)
4041 gfc_expr
*l
, *u
, *result
;
4044 k
= get_kind (BT_INTEGER
, kind
, upper
? "UBOUND" : "LBOUND",
4045 gfc_default_integer_kind
);
4047 return &gfc_bad_expr
;
4049 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &array
->where
);
4051 /* For non-variables, LBOUND(expr, DIM=n) = 1 and
4052 UBOUND(expr, DIM=n) = SIZE(expr, DIM=n). */
4053 if (!coarray
&& array
->expr_type
!= EXPR_VARIABLE
)
4057 gfc_expr
* dim
= result
;
4058 mpz_set_si (dim
->value
.integer
, d
);
4060 result
= simplify_size (array
, dim
, k
);
4061 gfc_free_expr (dim
);
4066 mpz_set_si (result
->value
.integer
, 1);
4071 /* Otherwise, we have a variable expression. */
4072 gcc_assert (array
->expr_type
== EXPR_VARIABLE
);
4075 if (!gfc_resolve_array_spec (as
, 0))
4078 /* The last dimension of an assumed-size array is special. */
4079 if ((!coarray
&& d
== as
->rank
&& as
->type
== AS_ASSUMED_SIZE
&& !upper
)
4080 || (coarray
&& d
== as
->rank
+ as
->corank
4081 && (!upper
|| flag_coarray
== GFC_FCOARRAY_SINGLE
)))
4083 if (as
->lower
[d
-1] && as
->lower
[d
-1]->expr_type
== EXPR_CONSTANT
)
4085 gfc_free_expr (result
);
4086 return gfc_copy_expr (as
->lower
[d
-1]);
4092 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &array
->where
);
4094 /* Then, we need to know the extent of the given dimension. */
4095 if (coarray
|| (ref
->u
.ar
.type
== AR_FULL
&& !ref
->next
))
4097 gfc_expr
*declared_bound
;
4099 bool constant_lbound
, constant_ubound
;
4104 gcc_assert (l
!= NULL
);
4106 constant_lbound
= l
->expr_type
== EXPR_CONSTANT
;
4107 constant_ubound
= u
&& u
->expr_type
== EXPR_CONSTANT
;
4109 empty_bound
= upper
? 0 : 1;
4110 declared_bound
= upper
? u
: l
;
4112 if ((!upper
&& !constant_lbound
)
4113 || (upper
&& !constant_ubound
))
4118 /* For {L,U}BOUND, the value depends on whether the array
4119 is empty. We can nevertheless simplify if the declared bound
4120 has the same value as that of an empty array, in which case
4121 the result isn't dependent on the array emptyness. */
4122 if (mpz_cmp_si (declared_bound
->value
.integer
, empty_bound
) == 0)
4123 mpz_set_si (result
->value
.integer
, empty_bound
);
4124 else if (!constant_lbound
|| !constant_ubound
)
4125 /* Array emptyness can't be determined, we can't simplify. */
4127 else if (mpz_cmp (l
->value
.integer
, u
->value
.integer
) > 0)
4128 mpz_set_si (result
->value
.integer
, empty_bound
);
4130 mpz_set (result
->value
.integer
, declared_bound
->value
.integer
);
4133 mpz_set (result
->value
.integer
, declared_bound
->value
.integer
);
4139 if (!gfc_ref_dimen_size (&ref
->u
.ar
, d
- 1, &result
->value
.integer
, NULL
))
4143 mpz_set_si (result
->value
.integer
, (long int) 1);
4147 return range_check (result
, upper
? "UBOUND" : "LBOUND");
4150 gfc_free_expr (result
);
4156 simplify_bound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
, int upper
)
4160 ar_type type
= AR_UNKNOWN
;
4163 if (array
->ts
.type
== BT_CLASS
)
4166 if (array
->expr_type
!= EXPR_VARIABLE
)
4173 /* Do not attempt to resolve if error has already been issued. */
4174 if (array
->symtree
->n
.sym
->error
)
4177 /* Follow any component references. */
4178 as
= array
->symtree
->n
.sym
->as
;
4179 for (ref
= array
->ref
; ref
; ref
= ref
->next
)
4184 type
= ref
->u
.ar
.type
;
4185 switch (ref
->u
.ar
.type
)
4192 /* We're done because 'as' has already been set in the
4193 previous iteration. */
4207 as
= ref
->u
.c
.component
->as
;
4220 if (as
&& (as
->type
== AS_DEFERRED
|| as
->type
== AS_ASSUMED_RANK
4221 || (as
->type
== AS_ASSUMED_SHAPE
&& upper
)))
4225 || (as
->type
!= AS_DEFERRED
4226 && array
->expr_type
== EXPR_VARIABLE
4227 && !gfc_expr_attr (array
).allocatable
4228 && !gfc_expr_attr (array
).pointer
));
4232 /* Multi-dimensional bounds. */
4233 gfc_expr
*bounds
[GFC_MAX_DIMENSIONS
];
4237 /* UBOUND(ARRAY) is not valid for an assumed-size array. */
4238 if (upper
&& type
== AR_FULL
&& as
&& as
->type
== AS_ASSUMED_SIZE
)
4240 /* An error message will be emitted in
4241 check_assumed_size_reference (resolve.c). */
4242 return &gfc_bad_expr
;
4245 /* Simplify the bounds for each dimension. */
4246 for (d
= 0; d
< array
->rank
; d
++)
4248 bounds
[d
] = simplify_bound_dim (array
, kind
, d
+ 1, upper
, as
, ref
,
4250 if (bounds
[d
] == NULL
|| bounds
[d
] == &gfc_bad_expr
)
4254 for (j
= 0; j
< d
; j
++)
4255 gfc_free_expr (bounds
[j
]);
4258 return &gfc_bad_expr
;
4264 /* Allocate the result expression. */
4265 k
= get_kind (BT_INTEGER
, kind
, upper
? "UBOUND" : "LBOUND",
4266 gfc_default_integer_kind
);
4268 return &gfc_bad_expr
;
4270 e
= gfc_get_array_expr (BT_INTEGER
, k
, &array
->where
);
4272 /* The result is a rank 1 array; its size is the rank of the first
4273 argument to {L,U}BOUND. */
4275 e
->shape
= gfc_get_shape (1);
4276 mpz_init_set_ui (e
->shape
[0], array
->rank
);
4278 /* Create the constructor for this array. */
4279 for (d
= 0; d
< array
->rank
; d
++)
4280 gfc_constructor_append_expr (&e
->value
.constructor
,
4281 bounds
[d
], &e
->where
);
4287 /* A DIM argument is specified. */
4288 if (dim
->expr_type
!= EXPR_CONSTANT
)
4291 d
= mpz_get_si (dim
->value
.integer
);
4293 if ((d
< 1 || d
> array
->rank
)
4294 || (d
== array
->rank
&& as
&& as
->type
== AS_ASSUMED_SIZE
&& upper
))
4296 gfc_error ("DIM argument at %L is out of bounds", &dim
->where
);
4297 return &gfc_bad_expr
;
4300 if (as
&& as
->type
== AS_ASSUMED_RANK
)
4303 return simplify_bound_dim (array
, kind
, d
, upper
, as
, ref
, false);
4309 simplify_cobound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
, int upper
)
4315 if (array
->expr_type
!= EXPR_VARIABLE
)
4318 /* Follow any component references. */
4319 as
= (array
->ts
.type
== BT_CLASS
&& array
->ts
.u
.derived
->components
)
4320 ? array
->ts
.u
.derived
->components
->as
4321 : array
->symtree
->n
.sym
->as
;
4322 for (ref
= array
->ref
; ref
; ref
= ref
->next
)
4327 switch (ref
->u
.ar
.type
)
4330 if (ref
->u
.ar
.as
->corank
> 0)
4332 gcc_assert (as
== ref
->u
.ar
.as
);
4339 /* We're done because 'as' has already been set in the
4340 previous iteration. */
4354 as
= ref
->u
.c
.component
->as
;
4368 if (as
->cotype
== AS_DEFERRED
|| as
->cotype
== AS_ASSUMED_SHAPE
)
4373 /* Multi-dimensional cobounds. */
4374 gfc_expr
*bounds
[GFC_MAX_DIMENSIONS
];
4378 /* Simplify the cobounds for each dimension. */
4379 for (d
= 0; d
< as
->corank
; d
++)
4381 bounds
[d
] = simplify_bound_dim (array
, kind
, d
+ 1 + as
->rank
,
4382 upper
, as
, ref
, true);
4383 if (bounds
[d
] == NULL
|| bounds
[d
] == &gfc_bad_expr
)
4387 for (j
= 0; j
< d
; j
++)
4388 gfc_free_expr (bounds
[j
]);
4393 /* Allocate the result expression. */
4394 e
= gfc_get_expr ();
4395 e
->where
= array
->where
;
4396 e
->expr_type
= EXPR_ARRAY
;
4397 e
->ts
.type
= BT_INTEGER
;
4398 k
= get_kind (BT_INTEGER
, kind
, upper
? "UCOBOUND" : "LCOBOUND",
4399 gfc_default_integer_kind
);
4403 return &gfc_bad_expr
;
4407 /* The result is a rank 1 array; its size is the rank of the first
4408 argument to {L,U}COBOUND. */
4410 e
->shape
= gfc_get_shape (1);
4411 mpz_init_set_ui (e
->shape
[0], as
->corank
);
4413 /* Create the constructor for this array. */
4414 for (d
= 0; d
< as
->corank
; d
++)
4415 gfc_constructor_append_expr (&e
->value
.constructor
,
4416 bounds
[d
], &e
->where
);
4421 /* A DIM argument is specified. */
4422 if (dim
->expr_type
!= EXPR_CONSTANT
)
4425 d
= mpz_get_si (dim
->value
.integer
);
4427 if (d
< 1 || d
> as
->corank
)
4429 gfc_error ("DIM argument at %L is out of bounds", &dim
->where
);
4430 return &gfc_bad_expr
;
4433 return simplify_bound_dim (array
, kind
, d
+as
->rank
, upper
, as
, ref
, true);
4439 gfc_simplify_lbound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
)
4441 return simplify_bound (array
, dim
, kind
, 0);
4446 gfc_simplify_lcobound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
)
4448 return simplify_cobound (array
, dim
, kind
, 0);
4452 gfc_simplify_leadz (gfc_expr
*e
)
4454 unsigned long lz
, bs
;
4457 if (e
->expr_type
!= EXPR_CONSTANT
)
4460 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
4461 bs
= gfc_integer_kinds
[i
].bit_size
;
4462 if (mpz_cmp_si (e
->value
.integer
, 0) == 0)
4464 else if (mpz_cmp_si (e
->value
.integer
, 0) < 0)
4467 lz
= bs
- mpz_sizeinbase (e
->value
.integer
, 2);
4469 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, lz
);
4474 gfc_simplify_len (gfc_expr
*e
, gfc_expr
*kind
)
4477 int k
= get_kind (BT_INTEGER
, kind
, "LEN", gfc_default_integer_kind
);
4480 return &gfc_bad_expr
;
4482 if (e
->expr_type
== EXPR_CONSTANT
)
4484 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &e
->where
);
4485 mpz_set_si (result
->value
.integer
, e
->value
.character
.length
);
4486 return range_check (result
, "LEN");
4488 else if (e
->ts
.u
.cl
!= NULL
&& e
->ts
.u
.cl
->length
!= NULL
4489 && e
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
4490 && e
->ts
.u
.cl
->length
->ts
.type
== BT_INTEGER
)
4492 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &e
->where
);
4493 mpz_set (result
->value
.integer
, e
->ts
.u
.cl
->length
->value
.integer
);
4494 return range_check (result
, "LEN");
4496 else if (e
->expr_type
== EXPR_VARIABLE
&& e
->ts
.type
== BT_CHARACTER
4497 && e
->symtree
->n
.sym
4498 && e
->symtree
->n
.sym
->ts
.type
!= BT_DERIVED
4499 && e
->symtree
->n
.sym
->assoc
&& e
->symtree
->n
.sym
->assoc
->target
4500 && e
->symtree
->n
.sym
->assoc
->target
->ts
.type
== BT_DERIVED
4501 && e
->symtree
->n
.sym
->assoc
->target
->symtree
->n
.sym
4502 && UNLIMITED_POLY (e
->symtree
->n
.sym
->assoc
->target
->symtree
->n
.sym
))
4504 /* The expression in assoc->target points to a ref to the _data component
4505 of the unlimited polymorphic entity. To get the _len component the last
4506 _data ref needs to be stripped and a ref to the _len component added. */
4507 return gfc_get_len_component (e
->symtree
->n
.sym
->assoc
->target
, k
);
4514 gfc_simplify_len_trim (gfc_expr
*e
, gfc_expr
*kind
)
4517 size_t count
, len
, i
;
4518 int k
= get_kind (BT_INTEGER
, kind
, "LEN_TRIM", gfc_default_integer_kind
);
4521 return &gfc_bad_expr
;
4523 if (e
->expr_type
!= EXPR_CONSTANT
)
4526 len
= e
->value
.character
.length
;
4527 for (count
= 0, i
= 1; i
<= len
; i
++)
4528 if (e
->value
.character
.string
[len
- i
] == ' ')
4533 result
= gfc_get_int_expr (k
, &e
->where
, len
- count
);
4534 return range_check (result
, "LEN_TRIM");
4538 gfc_simplify_lgamma (gfc_expr
*x
)
4543 if (x
->expr_type
!= EXPR_CONSTANT
)
4546 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
4547 mpfr_lgamma (result
->value
.real
, &sg
, x
->value
.real
, GFC_RND_MODE
);
4549 return range_check (result
, "LGAMMA");
4554 gfc_simplify_lge (gfc_expr
*a
, gfc_expr
*b
)
4556 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
4559 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
,
4560 gfc_compare_string (a
, b
) >= 0);
4565 gfc_simplify_lgt (gfc_expr
*a
, gfc_expr
*b
)
4567 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
4570 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
,
4571 gfc_compare_string (a
, b
) > 0);
4576 gfc_simplify_lle (gfc_expr
*a
, gfc_expr
*b
)
4578 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
4581 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
,
4582 gfc_compare_string (a
, b
) <= 0);
4587 gfc_simplify_llt (gfc_expr
*a
, gfc_expr
*b
)
4589 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
4592 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
,
4593 gfc_compare_string (a
, b
) < 0);
4598 gfc_simplify_log (gfc_expr
*x
)
4602 if (x
->expr_type
!= EXPR_CONSTANT
)
4605 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
4610 if (mpfr_sgn (x
->value
.real
) <= 0)
4612 gfc_error ("Argument of LOG at %L cannot be less than or equal "
4613 "to zero", &x
->where
);
4614 gfc_free_expr (result
);
4615 return &gfc_bad_expr
;
4618 mpfr_log (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
4622 if (mpfr_zero_p (mpc_realref (x
->value
.complex))
4623 && mpfr_zero_p (mpc_imagref (x
->value
.complex)))
4625 gfc_error ("Complex argument of LOG at %L cannot be zero",
4627 gfc_free_expr (result
);
4628 return &gfc_bad_expr
;
4631 gfc_set_model_kind (x
->ts
.kind
);
4632 mpc_log (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
4636 gfc_internal_error ("gfc_simplify_log: bad type");
4639 return range_check (result
, "LOG");
4644 gfc_simplify_log10 (gfc_expr
*x
)
4648 if (x
->expr_type
!= EXPR_CONSTANT
)
4651 if (mpfr_sgn (x
->value
.real
) <= 0)
4653 gfc_error ("Argument of LOG10 at %L cannot be less than or equal "
4654 "to zero", &x
->where
);
4655 return &gfc_bad_expr
;
4658 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
4659 mpfr_log10 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
4661 return range_check (result
, "LOG10");
4666 gfc_simplify_logical (gfc_expr
*e
, gfc_expr
*k
)
4670 kind
= get_kind (BT_LOGICAL
, k
, "LOGICAL", gfc_default_logical_kind
);
4672 return &gfc_bad_expr
;
4674 if (e
->expr_type
!= EXPR_CONSTANT
)
4677 return gfc_get_logical_expr (kind
, &e
->where
, e
->value
.logical
);
4682 gfc_simplify_matmul (gfc_expr
*matrix_a
, gfc_expr
*matrix_b
)
4685 int row
, result_rows
, col
, result_columns
;
4686 int stride_a
, offset_a
, stride_b
, offset_b
;
4688 if (!is_constant_array_expr (matrix_a
)
4689 || !is_constant_array_expr (matrix_b
))
4692 /* MATMUL should do mixed-mode arithmetic. Set the result type. */
4693 if (matrix_a
->ts
.type
!= matrix_b
->ts
.type
)
4696 e
.expr_type
= EXPR_OP
;
4697 gfc_clear_ts (&e
.ts
);
4698 e
.value
.op
.op
= INTRINSIC_NONE
;
4699 e
.value
.op
.op1
= matrix_a
;
4700 e
.value
.op
.op2
= matrix_b
;
4701 gfc_type_convert_binary (&e
, 1);
4702 result
= gfc_get_array_expr (e
.ts
.type
, e
.ts
.kind
, &matrix_a
->where
);
4706 result
= gfc_get_array_expr (matrix_a
->ts
.type
, matrix_a
->ts
.kind
,
4710 if (matrix_a
->rank
== 1 && matrix_b
->rank
== 2)
4713 result_columns
= mpz_get_si (matrix_b
->shape
[1]);
4715 stride_b
= mpz_get_si (matrix_b
->shape
[0]);
4718 result
->shape
= gfc_get_shape (result
->rank
);
4719 mpz_init_set_si (result
->shape
[0], result_columns
);
4721 else if (matrix_a
->rank
== 2 && matrix_b
->rank
== 1)
4723 result_rows
= mpz_get_si (matrix_a
->shape
[0]);
4725 stride_a
= mpz_get_si (matrix_a
->shape
[0]);
4729 result
->shape
= gfc_get_shape (result
->rank
);
4730 mpz_init_set_si (result
->shape
[0], result_rows
);
4732 else if (matrix_a
->rank
== 2 && matrix_b
->rank
== 2)
4734 result_rows
= mpz_get_si (matrix_a
->shape
[0]);
4735 result_columns
= mpz_get_si (matrix_b
->shape
[1]);
4736 stride_a
= mpz_get_si (matrix_a
->shape
[0]);
4737 stride_b
= mpz_get_si (matrix_b
->shape
[0]);
4740 result
->shape
= gfc_get_shape (result
->rank
);
4741 mpz_init_set_si (result
->shape
[0], result_rows
);
4742 mpz_init_set_si (result
->shape
[1], result_columns
);
4748 for (col
= 0; col
< result_columns
; ++col
)
4752 for (row
= 0; row
< result_rows
; ++row
)
4754 gfc_expr
*e
= compute_dot_product (matrix_a
, stride_a
, offset_a
,
4755 matrix_b
, 1, offset_b
, false);
4756 gfc_constructor_append_expr (&result
->value
.constructor
,
4762 offset_b
+= stride_b
;
4770 gfc_simplify_maskr (gfc_expr
*i
, gfc_expr
*kind_arg
)
4775 if (i
->expr_type
!= EXPR_CONSTANT
)
4778 kind
= get_kind (BT_INTEGER
, kind_arg
, "MASKR", gfc_default_integer_kind
);
4780 return &gfc_bad_expr
;
4781 k
= gfc_validate_kind (BT_INTEGER
, kind
, false);
4783 bool fail
= gfc_extract_int (i
, &arg
);
4786 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &i
->where
);
4788 /* MASKR(n) = 2^n - 1 */
4789 mpz_set_ui (result
->value
.integer
, 1);
4790 mpz_mul_2exp (result
->value
.integer
, result
->value
.integer
, arg
);
4791 mpz_sub_ui (result
->value
.integer
, result
->value
.integer
, 1);
4793 gfc_convert_mpz_to_signed (result
->value
.integer
, gfc_integer_kinds
[k
].bit_size
);
4800 gfc_simplify_maskl (gfc_expr
*i
, gfc_expr
*kind_arg
)
4806 if (i
->expr_type
!= EXPR_CONSTANT
)
4809 kind
= get_kind (BT_INTEGER
, kind_arg
, "MASKL", 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 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &i
->where
);
4819 /* MASKL(n) = 2^bit_size - 2^(bit_size - n) */
4820 mpz_init_set_ui (z
, 1);
4821 mpz_mul_2exp (z
, z
, gfc_integer_kinds
[k
].bit_size
);
4822 mpz_set_ui (result
->value
.integer
, 1);
4823 mpz_mul_2exp (result
->value
.integer
, result
->value
.integer
,
4824 gfc_integer_kinds
[k
].bit_size
- arg
);
4825 mpz_sub (result
->value
.integer
, z
, result
->value
.integer
);
4828 gfc_convert_mpz_to_signed (result
->value
.integer
, gfc_integer_kinds
[k
].bit_size
);
4835 gfc_simplify_merge (gfc_expr
*tsource
, gfc_expr
*fsource
, gfc_expr
*mask
)
4838 gfc_constructor
*tsource_ctor
, *fsource_ctor
, *mask_ctor
;
4840 if (mask
->expr_type
== EXPR_CONSTANT
)
4842 result
= gfc_copy_expr (mask
->value
.logical
? tsource
: fsource
);
4843 /* Parenthesis is needed to get lower bounds of 1. */
4844 result
= gfc_get_parentheses (result
);
4845 gfc_simplify_expr (result
, 1);
4849 if (!mask
->rank
|| !is_constant_array_expr (mask
)
4850 || !is_constant_array_expr (tsource
) || !is_constant_array_expr (fsource
))
4853 result
= gfc_get_array_expr (tsource
->ts
.type
, tsource
->ts
.kind
,
4855 if (tsource
->ts
.type
== BT_DERIVED
)
4856 result
->ts
.u
.derived
= tsource
->ts
.u
.derived
;
4857 else if (tsource
->ts
.type
== BT_CHARACTER
)
4858 result
->ts
.u
.cl
= tsource
->ts
.u
.cl
;
4860 tsource_ctor
= gfc_constructor_first (tsource
->value
.constructor
);
4861 fsource_ctor
= gfc_constructor_first (fsource
->value
.constructor
);
4862 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
4866 if (mask_ctor
->expr
->value
.logical
)
4867 gfc_constructor_append_expr (&result
->value
.constructor
,
4868 gfc_copy_expr (tsource_ctor
->expr
),
4871 gfc_constructor_append_expr (&result
->value
.constructor
,
4872 gfc_copy_expr (fsource_ctor
->expr
),
4874 tsource_ctor
= gfc_constructor_next (tsource_ctor
);
4875 fsource_ctor
= gfc_constructor_next (fsource_ctor
);
4876 mask_ctor
= gfc_constructor_next (mask_ctor
);
4879 result
->shape
= gfc_get_shape (1);
4880 gfc_array_size (result
, &result
->shape
[0]);
4887 gfc_simplify_merge_bits (gfc_expr
*i
, gfc_expr
*j
, gfc_expr
*mask_expr
)
4889 mpz_t arg1
, arg2
, mask
;
4892 if (i
->expr_type
!= EXPR_CONSTANT
|| j
->expr_type
!= EXPR_CONSTANT
4893 || mask_expr
->expr_type
!= EXPR_CONSTANT
)
4896 result
= gfc_get_constant_expr (BT_INTEGER
, i
->ts
.kind
, &i
->where
);
4898 /* Convert all argument to unsigned. */
4899 mpz_init_set (arg1
, i
->value
.integer
);
4900 mpz_init_set (arg2
, j
->value
.integer
);
4901 mpz_init_set (mask
, mask_expr
->value
.integer
);
4903 /* MERGE_BITS(I,J,MASK) = IOR (IAND (I, MASK), IAND (J, NOT (MASK))). */
4904 mpz_and (arg1
, arg1
, mask
);
4905 mpz_com (mask
, mask
);
4906 mpz_and (arg2
, arg2
, mask
);
4907 mpz_ior (result
->value
.integer
, arg1
, arg2
);
4917 /* Selects between current value and extremum for simplify_min_max
4918 and simplify_minval_maxval. */
4920 min_max_choose (gfc_expr
*arg
, gfc_expr
*extremum
, int sign
, bool back_val
)
4924 switch (arg
->ts
.type
)
4927 if (extremum
->ts
.kind
< arg
->ts
.kind
)
4928 extremum
->ts
.kind
= arg
->ts
.kind
;
4929 ret
= mpz_cmp (arg
->value
.integer
,
4930 extremum
->value
.integer
) * sign
;
4932 mpz_set (extremum
->value
.integer
, arg
->value
.integer
);
4936 if (extremum
->ts
.kind
< arg
->ts
.kind
)
4937 extremum
->ts
.kind
= arg
->ts
.kind
;
4938 if (mpfr_nan_p (extremum
->value
.real
))
4941 mpfr_set (extremum
->value
.real
, arg
->value
.real
, GFC_RND_MODE
);
4943 else if (mpfr_nan_p (arg
->value
.real
))
4947 ret
= mpfr_cmp (arg
->value
.real
, extremum
->value
.real
) * sign
;
4949 mpfr_set (extremum
->value
.real
, arg
->value
.real
, GFC_RND_MODE
);
4954 #define LENGTH(x) ((x)->value.character.length)
4955 #define STRING(x) ((x)->value.character.string)
4956 if (LENGTH (extremum
) < LENGTH(arg
))
4958 gfc_char_t
*tmp
= STRING(extremum
);
4960 STRING(extremum
) = gfc_get_wide_string (LENGTH(arg
) + 1);
4961 memcpy (STRING(extremum
), tmp
,
4962 LENGTH(extremum
) * sizeof (gfc_char_t
));
4963 gfc_wide_memset (&STRING(extremum
)[LENGTH(extremum
)], ' ',
4964 LENGTH(arg
) - LENGTH(extremum
));
4965 STRING(extremum
)[LENGTH(arg
)] = '\0'; /* For debugger */
4966 LENGTH(extremum
) = LENGTH(arg
);
4969 ret
= gfc_compare_string (arg
, extremum
) * sign
;
4972 free (STRING(extremum
));
4973 STRING(extremum
) = gfc_get_wide_string (LENGTH(extremum
) + 1);
4974 memcpy (STRING(extremum
), STRING(arg
),
4975 LENGTH(arg
) * sizeof (gfc_char_t
));
4976 gfc_wide_memset (&STRING(extremum
)[LENGTH(arg
)], ' ',
4977 LENGTH(extremum
) - LENGTH(arg
));
4978 STRING(extremum
)[LENGTH(extremum
)] = '\0'; /* For debugger */
4985 gfc_internal_error ("simplify_min_max(): Bad type in arglist");
4987 if (back_val
&& ret
== 0)
4994 /* This function is special since MAX() can take any number of
4995 arguments. The simplified expression is a rewritten version of the
4996 argument list containing at most one constant element. Other
4997 constant elements are deleted. Because the argument list has
4998 already been checked, this function always succeeds. sign is 1 for
4999 MAX(), -1 for MIN(). */
5002 simplify_min_max (gfc_expr
*expr
, int sign
)
5004 gfc_actual_arglist
*arg
, *last
, *extremum
;
5005 gfc_expr
*tmp
, *ret
;
5011 arg
= expr
->value
.function
.actual
;
5013 for (; arg
; last
= arg
, arg
= arg
->next
)
5015 if (arg
->expr
->expr_type
!= EXPR_CONSTANT
)
5018 if (extremum
== NULL
)
5024 min_max_choose (arg
->expr
, extremum
->expr
, sign
);
5026 /* Delete the extra constant argument. */
5027 last
->next
= arg
->next
;
5030 gfc_free_actual_arglist (arg
);
5034 /* If there is one value left, replace the function call with the
5036 if (expr
->value
.function
.actual
->next
!= NULL
)
5039 /* Handle special cases of specific functions (min|max)1 and
5042 tmp
= expr
->value
.function
.actual
->expr
;
5043 fname
= expr
->value
.function
.isym
->name
;
5045 if ((tmp
->ts
.type
!= BT_INTEGER
|| tmp
->ts
.kind
!= gfc_integer_4_kind
)
5046 && (strcmp (fname
, "min1") == 0 || strcmp (fname
, "max1") == 0))
5048 ret
= gfc_convert_constant (tmp
, BT_INTEGER
, gfc_integer_4_kind
);
5050 else if ((tmp
->ts
.type
!= BT_REAL
|| tmp
->ts
.kind
!= gfc_real_4_kind
)
5051 && (strcmp (fname
, "amin0") == 0 || strcmp (fname
, "amax0") == 0))
5053 ret
= gfc_convert_constant (tmp
, BT_REAL
, gfc_real_4_kind
);
5056 ret
= gfc_copy_expr (tmp
);
5064 gfc_simplify_min (gfc_expr
*e
)
5066 return simplify_min_max (e
, -1);
5071 gfc_simplify_max (gfc_expr
*e
)
5073 return simplify_min_max (e
, 1);
5076 /* Helper function for gfc_simplify_minval. */
5079 gfc_min (gfc_expr
*op1
, gfc_expr
*op2
)
5081 min_max_choose (op1
, op2
, -1);
5082 gfc_free_expr (op1
);
5086 /* Simplify minval for constant arrays. */
5089 gfc_simplify_minval (gfc_expr
*array
, gfc_expr
* dim
, gfc_expr
*mask
)
5091 return simplify_transformation (array
, dim
, mask
, INT_MAX
, gfc_min
);
5094 /* Helper function for gfc_simplify_maxval. */
5097 gfc_max (gfc_expr
*op1
, gfc_expr
*op2
)
5099 min_max_choose (op1
, op2
, 1);
5100 gfc_free_expr (op1
);
5105 /* Simplify maxval for constant arrays. */
5108 gfc_simplify_maxval (gfc_expr
*array
, gfc_expr
* dim
, gfc_expr
*mask
)
5110 return simplify_transformation (array
, dim
, mask
, INT_MIN
, gfc_max
);
5114 /* Transform minloc or maxloc of an array, according to MASK,
5115 to the scalar result. This code is mostly identical to
5116 simplify_transformation_to_scalar. */
5119 simplify_minmaxloc_to_scalar (gfc_expr
*result
, gfc_expr
*array
, gfc_expr
*mask
,
5120 gfc_expr
*extremum
, int sign
, bool back_val
)
5123 gfc_constructor
*array_ctor
, *mask_ctor
;
5126 mpz_set_si (result
->value
.integer
, 0);
5129 /* Shortcut for constant .FALSE. MASK. */
5131 && mask
->expr_type
== EXPR_CONSTANT
5132 && !mask
->value
.logical
)
5135 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
5136 if (mask
&& mask
->expr_type
== EXPR_ARRAY
)
5137 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
5141 mpz_init_set_si (count
, 0);
5144 mpz_add_ui (count
, count
, 1);
5145 a
= array_ctor
->expr
;
5146 array_ctor
= gfc_constructor_next (array_ctor
);
5147 /* A constant MASK equals .TRUE. here and can be ignored. */
5150 m
= mask_ctor
->expr
;
5151 mask_ctor
= gfc_constructor_next (mask_ctor
);
5152 if (!m
->value
.logical
)
5155 if (min_max_choose (a
, extremum
, sign
, back_val
) > 0)
5156 mpz_set (result
->value
.integer
, count
);
5159 gfc_free_expr (extremum
);
5163 /* Simplify minloc / maxloc in the absence of a dim argument. */
5166 simplify_minmaxloc_nodim (gfc_expr
*result
, gfc_expr
*extremum
,
5167 gfc_expr
*array
, gfc_expr
*mask
, int sign
,
5170 ssize_t res
[GFC_MAX_DIMENSIONS
];
5172 gfc_constructor
*result_ctor
, *array_ctor
, *mask_ctor
;
5173 ssize_t count
[GFC_MAX_DIMENSIONS
], extent
[GFC_MAX_DIMENSIONS
],
5174 sstride
[GFC_MAX_DIMENSIONS
];
5179 for (i
= 0; i
<array
->rank
; i
++)
5182 /* Shortcut for constant .FALSE. MASK. */
5184 && mask
->expr_type
== EXPR_CONSTANT
5185 && !mask
->value
.logical
)
5188 for (i
= 0; i
< array
->rank
; i
++)
5191 sstride
[i
] = (i
== 0) ? 1 : sstride
[i
-1] * mpz_get_si (array
->shape
[i
-1]);
5192 extent
[i
] = mpz_get_si (array
->shape
[i
]);
5197 continue_loop
= true;
5198 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
5199 if (mask
&& mask
->rank
> 0)
5200 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
5204 /* Loop over the array elements (and mask), keeping track of
5205 the indices to return. */
5206 while (continue_loop
)
5210 a
= array_ctor
->expr
;
5213 m
= mask_ctor
->expr
;
5214 ma
= m
->value
.logical
;
5215 mask_ctor
= gfc_constructor_next (mask_ctor
);
5220 if (ma
&& min_max_choose (a
, extremum
, sign
, back_val
) > 0)
5222 for (i
= 0; i
<array
->rank
; i
++)
5225 array_ctor
= gfc_constructor_next (array_ctor
);
5227 } while (count
[0] != extent
[0]);
5231 /* When we get to the end of a dimension, reset it and increment
5232 the next dimension. */
5235 if (n
>= array
->rank
)
5237 continue_loop
= false;
5242 } while (count
[n
] == extent
[n
]);
5246 gfc_free_expr (extremum
);
5247 result_ctor
= gfc_constructor_first (result
->value
.constructor
);
5248 for (i
= 0; i
<array
->rank
; i
++)
5251 r_expr
= result_ctor
->expr
;
5252 mpz_set_si (r_expr
->value
.integer
, res
[i
] + 1);
5253 result_ctor
= gfc_constructor_next (result_ctor
);
5258 /* Helper function for gfc_simplify_minmaxloc - build an array
5259 expression with n elements. */
5262 new_array (bt type
, int kind
, int n
, locus
*where
)
5267 result
= gfc_get_array_expr (type
, kind
, where
);
5269 result
->shape
= gfc_get_shape(1);
5270 mpz_init_set_si (result
->shape
[0], n
);
5271 for (i
= 0; i
< n
; i
++)
5273 gfc_constructor_append_expr (&result
->value
.constructor
,
5274 gfc_get_constant_expr (type
, kind
, where
),
5281 /* Simplify minloc and maxloc. This code is mostly identical to
5282 simplify_transformation_to_array. */
5285 simplify_minmaxloc_to_array (gfc_expr
*result
, gfc_expr
*array
,
5286 gfc_expr
*dim
, gfc_expr
*mask
,
5287 gfc_expr
*extremum
, int sign
, bool back_val
)
5290 int done
, i
, n
, arraysize
, resultsize
, dim_index
, dim_extent
, dim_stride
;
5291 gfc_expr
**arrayvec
, **resultvec
, **base
, **src
, **dest
;
5292 gfc_constructor
*array_ctor
, *mask_ctor
, *result_ctor
;
5294 int count
[GFC_MAX_DIMENSIONS
], extent
[GFC_MAX_DIMENSIONS
],
5295 sstride
[GFC_MAX_DIMENSIONS
], dstride
[GFC_MAX_DIMENSIONS
],
5296 tmpstride
[GFC_MAX_DIMENSIONS
];
5298 /* Shortcut for constant .FALSE. MASK. */
5300 && mask
->expr_type
== EXPR_CONSTANT
5301 && !mask
->value
.logical
)
5304 /* Build an indexed table for array element expressions to minimize
5305 linked-list traversal. Masked elements are set to NULL. */
5306 gfc_array_size (array
, &size
);
5307 arraysize
= mpz_get_ui (size
);
5310 arrayvec
= XCNEWVEC (gfc_expr
*, arraysize
);
5312 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
5314 if (mask
&& mask
->expr_type
== EXPR_ARRAY
)
5315 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
5317 for (i
= 0; i
< arraysize
; ++i
)
5319 arrayvec
[i
] = array_ctor
->expr
;
5320 array_ctor
= gfc_constructor_next (array_ctor
);
5324 if (!mask_ctor
->expr
->value
.logical
)
5327 mask_ctor
= gfc_constructor_next (mask_ctor
);
5331 /* Same for the result expression. */
5332 gfc_array_size (result
, &size
);
5333 resultsize
= mpz_get_ui (size
);
5336 resultvec
= XCNEWVEC (gfc_expr
*, resultsize
);
5337 result_ctor
= gfc_constructor_first (result
->value
.constructor
);
5338 for (i
= 0; i
< resultsize
; ++i
)
5340 resultvec
[i
] = result_ctor
->expr
;
5341 result_ctor
= gfc_constructor_next (result_ctor
);
5344 gfc_extract_int (dim
, &dim_index
);
5345 dim_index
-= 1; /* zero-base index */
5349 for (i
= 0, n
= 0; i
< array
->rank
; ++i
)
5352 tmpstride
[i
] = (i
== 0) ? 1 : tmpstride
[i
-1] * mpz_get_si (array
->shape
[i
-1]);
5355 dim_extent
= mpz_get_si (array
->shape
[i
]);
5356 dim_stride
= tmpstride
[i
];
5360 extent
[n
] = mpz_get_si (array
->shape
[i
]);
5361 sstride
[n
] = tmpstride
[i
];
5362 dstride
[n
] = (n
== 0) ? 1 : dstride
[n
-1] * extent
[n
-1];
5366 done
= resultsize
<= 0;
5372 ex
= gfc_copy_expr (extremum
);
5373 for (src
= base
, n
= 0; n
< dim_extent
; src
+= dim_stride
, ++n
)
5375 if (*src
&& min_max_choose (*src
, ex
, sign
, back_val
) > 0)
5376 mpz_set_si ((*dest
)->value
.integer
, n
+ 1);
5385 while (!done
&& count
[n
] == extent
[n
])
5388 base
-= sstride
[n
] * extent
[n
];
5389 dest
-= dstride
[n
] * extent
[n
];
5392 if (n
< result
->rank
)
5394 /* If the nested loop is unrolled GFC_MAX_DIMENSIONS
5395 times, we'd warn for the last iteration, because the
5396 array index will have already been incremented to the
5397 array sizes, and we can't tell that this must make
5398 the test against result->rank false, because ranks
5399 must not exceed GFC_MAX_DIMENSIONS. */
5400 GCC_DIAGNOSTIC_PUSH_IGNORED (-Warray
-bounds
)
5411 /* Place updated expression in result constructor. */
5412 result_ctor
= gfc_constructor_first (result
->value
.constructor
);
5413 for (i
= 0; i
< resultsize
; ++i
)
5415 result_ctor
->expr
= resultvec
[i
];
5416 result_ctor
= gfc_constructor_next (result_ctor
);
5425 /* Simplify minloc and maxloc for constant arrays. */
5428 gfc_simplify_minmaxloc (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
,
5429 gfc_expr
*kind
, gfc_expr
*back
, int sign
)
5435 bool back_val
= false;
5437 if (!is_constant_array_expr (array
)
5438 || !gfc_is_constant_expr (dim
))
5442 && !is_constant_array_expr (mask
)
5443 && mask
->expr_type
!= EXPR_CONSTANT
)
5448 if (gfc_extract_int (kind
, &ikind
, -1))
5452 ikind
= gfc_default_integer_kind
;
5456 if (back
->expr_type
!= EXPR_CONSTANT
)
5459 back_val
= back
->value
.logical
;
5469 extremum
= gfc_get_constant_expr (array
->ts
.type
, array
->ts
.kind
, &array
->where
);
5470 init_result_expr (extremum
, init_val
, array
);
5474 result
= transformational_result (array
, dim
, BT_INTEGER
,
5475 ikind
, &array
->where
);
5476 init_result_expr (result
, 0, array
);
5478 if (array
->rank
== 1)
5479 return simplify_minmaxloc_to_scalar (result
, array
, mask
, extremum
,
5482 return simplify_minmaxloc_to_array (result
, array
, dim
, mask
, extremum
,
5487 result
= new_array (BT_INTEGER
, ikind
, array
->rank
, &array
->where
);
5488 return simplify_minmaxloc_nodim (result
, extremum
, array
, mask
,
5494 gfc_simplify_minloc (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
, gfc_expr
*kind
,
5497 return gfc_simplify_minmaxloc (array
, dim
, mask
, kind
, back
, -1);
5501 gfc_simplify_maxloc (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
, gfc_expr
*kind
,
5504 return gfc_simplify_minmaxloc (array
, dim
, mask
, kind
, back
, 1);
5507 /* Simplify findloc to scalar. Similar to
5508 simplify_minmaxloc_to_scalar. */
5511 simplify_findloc_to_scalar (gfc_expr
*result
, gfc_expr
*array
, gfc_expr
*value
,
5512 gfc_expr
*mask
, int back_val
)
5515 gfc_constructor
*array_ctor
, *mask_ctor
;
5518 mpz_set_si (result
->value
.integer
, 0);
5520 /* Shortcut for constant .FALSE. MASK. */
5522 && mask
->expr_type
== EXPR_CONSTANT
5523 && !mask
->value
.logical
)
5526 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
5527 if (mask
&& mask
->expr_type
== EXPR_ARRAY
)
5528 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
5532 mpz_init_set_si (count
, 0);
5535 mpz_add_ui (count
, count
, 1);
5536 a
= array_ctor
->expr
;
5537 array_ctor
= gfc_constructor_next (array_ctor
);
5538 /* A constant MASK equals .TRUE. here and can be ignored. */
5541 m
= mask_ctor
->expr
;
5542 mask_ctor
= gfc_constructor_next (mask_ctor
);
5543 if (!m
->value
.logical
)
5546 if (gfc_compare_expr (a
, value
, INTRINSIC_EQ
) == 0)
5548 /* We have a match. If BACK is true, continue so we find
5550 mpz_set (result
->value
.integer
, count
);
5559 /* Simplify findloc in the absence of a dim argument. Similar to
5560 simplify_minmaxloc_nodim. */
5563 simplify_findloc_nodim (gfc_expr
*result
, gfc_expr
*value
, gfc_expr
*array
,
5564 gfc_expr
*mask
, bool back_val
)
5566 ssize_t res
[GFC_MAX_DIMENSIONS
];
5568 gfc_constructor
*result_ctor
, *array_ctor
, *mask_ctor
;
5569 ssize_t count
[GFC_MAX_DIMENSIONS
], extent
[GFC_MAX_DIMENSIONS
],
5570 sstride
[GFC_MAX_DIMENSIONS
];
5575 for (i
= 0; i
< array
->rank
; i
++)
5578 /* Shortcut for constant .FALSE. MASK. */
5580 && mask
->expr_type
== EXPR_CONSTANT
5581 && !mask
->value
.logical
)
5584 for (i
= 0; i
< array
->rank
; i
++)
5587 sstride
[i
] = (i
== 0) ? 1 : sstride
[i
-1] * mpz_get_si (array
->shape
[i
-1]);
5588 extent
[i
] = mpz_get_si (array
->shape
[i
]);
5593 continue_loop
= true;
5594 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
5595 if (mask
&& mask
->rank
> 0)
5596 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
5600 /* Loop over the array elements (and mask), keeping track of
5601 the indices to return. */
5602 while (continue_loop
)
5606 a
= array_ctor
->expr
;
5609 m
= mask_ctor
->expr
;
5610 ma
= m
->value
.logical
;
5611 mask_ctor
= gfc_constructor_next (mask_ctor
);
5616 if (ma
&& gfc_compare_expr (a
, value
, INTRINSIC_EQ
) == 0)
5618 for (i
= 0; i
< array
->rank
; i
++)
5623 array_ctor
= gfc_constructor_next (array_ctor
);
5625 } while (count
[0] != extent
[0]);
5629 /* When we get to the end of a dimension, reset it and increment
5630 the next dimension. */
5633 if (n
>= array
->rank
)
5635 continue_loop
= false;
5640 } while (count
[n
] == extent
[n
]);
5644 result_ctor
= gfc_constructor_first (result
->value
.constructor
);
5645 for (i
= 0; i
< array
->rank
; i
++)
5648 r_expr
= result_ctor
->expr
;
5649 mpz_set_si (r_expr
->value
.integer
, res
[i
] + 1);
5650 result_ctor
= gfc_constructor_next (result_ctor
);
5656 /* Simplify findloc to an array. Similar to
5657 simplify_minmaxloc_to_array. */
5660 simplify_findloc_to_array (gfc_expr
*result
, gfc_expr
*array
, gfc_expr
*value
,
5661 gfc_expr
*dim
, gfc_expr
*mask
, bool back_val
)
5664 int done
, i
, n
, arraysize
, resultsize
, dim_index
, dim_extent
, dim_stride
;
5665 gfc_expr
**arrayvec
, **resultvec
, **base
, **src
, **dest
;
5666 gfc_constructor
*array_ctor
, *mask_ctor
, *result_ctor
;
5668 int count
[GFC_MAX_DIMENSIONS
], extent
[GFC_MAX_DIMENSIONS
],
5669 sstride
[GFC_MAX_DIMENSIONS
], dstride
[GFC_MAX_DIMENSIONS
],
5670 tmpstride
[GFC_MAX_DIMENSIONS
];
5672 /* Shortcut for constant .FALSE. MASK. */
5674 && mask
->expr_type
== EXPR_CONSTANT
5675 && !mask
->value
.logical
)
5678 /* Build an indexed table for array element expressions to minimize
5679 linked-list traversal. Masked elements are set to NULL. */
5680 gfc_array_size (array
, &size
);
5681 arraysize
= mpz_get_ui (size
);
5684 arrayvec
= XCNEWVEC (gfc_expr
*, arraysize
);
5686 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
5688 if (mask
&& mask
->expr_type
== EXPR_ARRAY
)
5689 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
5691 for (i
= 0; i
< arraysize
; ++i
)
5693 arrayvec
[i
] = array_ctor
->expr
;
5694 array_ctor
= gfc_constructor_next (array_ctor
);
5698 if (!mask_ctor
->expr
->value
.logical
)
5701 mask_ctor
= gfc_constructor_next (mask_ctor
);
5705 /* Same for the result expression. */
5706 gfc_array_size (result
, &size
);
5707 resultsize
= mpz_get_ui (size
);
5710 resultvec
= XCNEWVEC (gfc_expr
*, resultsize
);
5711 result_ctor
= gfc_constructor_first (result
->value
.constructor
);
5712 for (i
= 0; i
< resultsize
; ++i
)
5714 resultvec
[i
] = result_ctor
->expr
;
5715 result_ctor
= gfc_constructor_next (result_ctor
);
5718 gfc_extract_int (dim
, &dim_index
);
5720 dim_index
-= 1; /* Zero-base index. */
5724 for (i
= 0, n
= 0; i
< array
->rank
; ++i
)
5727 tmpstride
[i
] = (i
== 0) ? 1 : tmpstride
[i
-1] * mpz_get_si (array
->shape
[i
-1]);
5730 dim_extent
= mpz_get_si (array
->shape
[i
]);
5731 dim_stride
= tmpstride
[i
];
5735 extent
[n
] = mpz_get_si (array
->shape
[i
]);
5736 sstride
[n
] = tmpstride
[i
];
5737 dstride
[n
] = (n
== 0) ? 1 : dstride
[n
-1] * extent
[n
-1];
5741 done
= resultsize
<= 0;
5746 for (src
= base
, n
= 0; n
< dim_extent
; src
+= dim_stride
, ++n
)
5748 if (*src
&& gfc_compare_expr (*src
, value
, INTRINSIC_EQ
) == 0)
5750 mpz_set_si ((*dest
)->value
.integer
, n
+ 1);
5761 while (!done
&& count
[n
] == extent
[n
])
5764 base
-= sstride
[n
] * extent
[n
];
5765 dest
-= dstride
[n
] * extent
[n
];
5768 if (n
< result
->rank
)
5770 /* If the nested loop is unrolled GFC_MAX_DIMENSIONS
5771 times, we'd warn for the last iteration, because the
5772 array index will have already been incremented to the
5773 array sizes, and we can't tell that this must make
5774 the test against result->rank false, because ranks
5775 must not exceed GFC_MAX_DIMENSIONS. */
5776 GCC_DIAGNOSTIC_PUSH_IGNORED (-Warray
-bounds
)
5787 /* Place updated expression in result constructor. */
5788 result_ctor
= gfc_constructor_first (result
->value
.constructor
);
5789 for (i
= 0; i
< resultsize
; ++i
)
5791 result_ctor
->expr
= resultvec
[i
];
5792 result_ctor
= gfc_constructor_next (result_ctor
);
5800 /* Simplify findloc. */
5803 gfc_simplify_findloc (gfc_expr
*array
, gfc_expr
*value
, gfc_expr
*dim
,
5804 gfc_expr
*mask
, gfc_expr
*kind
, gfc_expr
*back
)
5808 bool back_val
= false;
5810 if (!is_constant_array_expr (array
)
5811 || !gfc_is_constant_expr (dim
))
5814 if (! gfc_is_constant_expr (value
))
5818 && !is_constant_array_expr (mask
)
5819 && mask
->expr_type
!= EXPR_CONSTANT
)
5824 if (gfc_extract_int (kind
, &ikind
, -1))
5828 ikind
= gfc_default_integer_kind
;
5832 if (back
->expr_type
!= EXPR_CONSTANT
)
5835 back_val
= back
->value
.logical
;
5840 result
= transformational_result (array
, dim
, BT_INTEGER
,
5841 ikind
, &array
->where
);
5842 init_result_expr (result
, 0, array
);
5844 if (array
->rank
== 1)
5845 return simplify_findloc_to_scalar (result
, array
, value
, mask
,
5848 return simplify_findloc_to_array (result
, array
, value
, dim
, mask
,
5853 result
= new_array (BT_INTEGER
, ikind
, array
->rank
, &array
->where
);
5854 return simplify_findloc_nodim (result
, value
, array
, mask
, back_val
);
5860 gfc_simplify_maxexponent (gfc_expr
*x
)
5862 int i
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, false);
5863 return gfc_get_int_expr (gfc_default_integer_kind
, &x
->where
,
5864 gfc_real_kinds
[i
].max_exponent
);
5869 gfc_simplify_minexponent (gfc_expr
*x
)
5871 int i
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, false);
5872 return gfc_get_int_expr (gfc_default_integer_kind
, &x
->where
,
5873 gfc_real_kinds
[i
].min_exponent
);
5878 gfc_simplify_mod (gfc_expr
*a
, gfc_expr
*p
)
5883 /* First check p. */
5884 if (p
->expr_type
!= EXPR_CONSTANT
)
5887 /* p shall not be 0. */
5891 if (mpz_cmp_ui (p
->value
.integer
, 0) == 0)
5893 gfc_error ("Argument %qs of MOD at %L shall not be zero",
5895 return &gfc_bad_expr
;
5899 if (mpfr_cmp_ui (p
->value
.real
, 0) == 0)
5901 gfc_error ("Argument %qs of MOD at %L shall not be zero",
5903 return &gfc_bad_expr
;
5907 gfc_internal_error ("gfc_simplify_mod(): Bad arguments");
5910 if (a
->expr_type
!= EXPR_CONSTANT
)
5913 kind
= a
->ts
.kind
> p
->ts
.kind
? a
->ts
.kind
: p
->ts
.kind
;
5914 result
= gfc_get_constant_expr (a
->ts
.type
, kind
, &a
->where
);
5916 if (a
->ts
.type
== BT_INTEGER
)
5917 mpz_tdiv_r (result
->value
.integer
, a
->value
.integer
, p
->value
.integer
);
5920 gfc_set_model_kind (kind
);
5921 mpfr_fmod (result
->value
.real
, a
->value
.real
, p
->value
.real
,
5925 return range_check (result
, "MOD");
5930 gfc_simplify_modulo (gfc_expr
*a
, gfc_expr
*p
)
5935 /* First check p. */
5936 if (p
->expr_type
!= EXPR_CONSTANT
)
5939 /* p shall not be 0. */
5943 if (mpz_cmp_ui (p
->value
.integer
, 0) == 0)
5945 gfc_error ("Argument %qs of MODULO at %L shall not be zero",
5947 return &gfc_bad_expr
;
5951 if (mpfr_cmp_ui (p
->value
.real
, 0) == 0)
5953 gfc_error ("Argument %qs of MODULO at %L shall not be zero",
5955 return &gfc_bad_expr
;
5959 gfc_internal_error ("gfc_simplify_modulo(): Bad arguments");
5962 if (a
->expr_type
!= EXPR_CONSTANT
)
5965 kind
= a
->ts
.kind
> p
->ts
.kind
? a
->ts
.kind
: p
->ts
.kind
;
5966 result
= gfc_get_constant_expr (a
->ts
.type
, kind
, &a
->where
);
5968 if (a
->ts
.type
== BT_INTEGER
)
5969 mpz_fdiv_r (result
->value
.integer
, a
->value
.integer
, p
->value
.integer
);
5972 gfc_set_model_kind (kind
);
5973 mpfr_fmod (result
->value
.real
, a
->value
.real
, p
->value
.real
,
5975 if (mpfr_cmp_ui (result
->value
.real
, 0) != 0)
5977 if (mpfr_signbit (a
->value
.real
) != mpfr_signbit (p
->value
.real
))
5978 mpfr_add (result
->value
.real
, result
->value
.real
, p
->value
.real
,
5982 mpfr_copysign (result
->value
.real
, result
->value
.real
,
5983 p
->value
.real
, GFC_RND_MODE
);
5986 return range_check (result
, "MODULO");
5991 gfc_simplify_nearest (gfc_expr
*x
, gfc_expr
*s
)
5994 mpfr_exp_t emin
, emax
;
5997 if (x
->expr_type
!= EXPR_CONSTANT
|| s
->expr_type
!= EXPR_CONSTANT
)
6000 result
= gfc_copy_expr (x
);
6002 /* Save current values of emin and emax. */
6003 emin
= mpfr_get_emin ();
6004 emax
= mpfr_get_emax ();
6006 /* Set emin and emax for the current model number. */
6007 kind
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, 0);
6008 mpfr_set_emin ((mpfr_exp_t
) gfc_real_kinds
[kind
].min_exponent
-
6009 mpfr_get_prec(result
->value
.real
) + 1);
6010 mpfr_set_emax ((mpfr_exp_t
) gfc_real_kinds
[kind
].max_exponent
- 1);
6011 mpfr_check_range (result
->value
.real
, 0, MPFR_RNDU
);
6013 if (mpfr_sgn (s
->value
.real
) > 0)
6015 mpfr_nextabove (result
->value
.real
);
6016 mpfr_subnormalize (result
->value
.real
, 0, MPFR_RNDU
);
6020 mpfr_nextbelow (result
->value
.real
);
6021 mpfr_subnormalize (result
->value
.real
, 0, MPFR_RNDD
);
6024 mpfr_set_emin (emin
);
6025 mpfr_set_emax (emax
);
6027 /* Only NaN can occur. Do not use range check as it gives an
6028 error for denormal numbers. */
6029 if (mpfr_nan_p (result
->value
.real
) && flag_range_check
)
6031 gfc_error ("Result of NEAREST is NaN at %L", &result
->where
);
6032 gfc_free_expr (result
);
6033 return &gfc_bad_expr
;
6041 simplify_nint (const char *name
, gfc_expr
*e
, gfc_expr
*k
)
6043 gfc_expr
*itrunc
, *result
;
6046 kind
= get_kind (BT_INTEGER
, k
, name
, gfc_default_integer_kind
);
6048 return &gfc_bad_expr
;
6050 if (e
->expr_type
!= EXPR_CONSTANT
)
6053 itrunc
= gfc_copy_expr (e
);
6054 mpfr_round (itrunc
->value
.real
, e
->value
.real
);
6056 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &e
->where
);
6057 gfc_mpfr_to_mpz (result
->value
.integer
, itrunc
->value
.real
, &e
->where
);
6059 gfc_free_expr (itrunc
);
6061 return range_check (result
, name
);
6066 gfc_simplify_new_line (gfc_expr
*e
)
6070 result
= gfc_get_character_expr (e
->ts
.kind
, &e
->where
, NULL
, 1);
6071 result
->value
.character
.string
[0] = '\n';
6078 gfc_simplify_nint (gfc_expr
*e
, gfc_expr
*k
)
6080 return simplify_nint ("NINT", e
, k
);
6085 gfc_simplify_idnint (gfc_expr
*e
)
6087 return simplify_nint ("IDNINT", e
, NULL
);
6090 static int norm2_scale
;
6093 norm2_add_squared (gfc_expr
*result
, gfc_expr
*e
)
6097 gcc_assert (e
->ts
.type
== BT_REAL
&& e
->expr_type
== EXPR_CONSTANT
);
6098 gcc_assert (result
->ts
.type
== BT_REAL
6099 && result
->expr_type
== EXPR_CONSTANT
);
6101 gfc_set_model_kind (result
->ts
.kind
);
6102 int index
= gfc_validate_kind (BT_REAL
, result
->ts
.kind
, false);
6104 if (mpfr_regular_p (result
->value
.real
))
6106 exp
= mpfr_get_exp (result
->value
.real
);
6107 /* If result is getting close to overflowing, scale down. */
6108 if (exp
>= gfc_real_kinds
[index
].max_exponent
- 4
6109 && norm2_scale
<= gfc_real_kinds
[index
].max_exponent
- 2)
6112 mpfr_div_ui (result
->value
.real
, result
->value
.real
, 16,
6118 if (mpfr_regular_p (e
->value
.real
))
6120 exp
= mpfr_get_exp (e
->value
.real
);
6121 /* If e**2 would overflow or close to overflowing, scale down. */
6122 if (exp
- norm2_scale
>= gfc_real_kinds
[index
].max_exponent
/ 2 - 2)
6124 int new_scale
= gfc_real_kinds
[index
].max_exponent
/ 2 + 4;
6125 mpfr_set_ui (tmp
, 1, GFC_RND_MODE
);
6126 mpfr_set_exp (tmp
, new_scale
- norm2_scale
);
6127 mpfr_div (result
->value
.real
, result
->value
.real
, tmp
, GFC_RND_MODE
);
6128 mpfr_div (result
->value
.real
, result
->value
.real
, tmp
, GFC_RND_MODE
);
6129 norm2_scale
= new_scale
;
6134 mpfr_set_ui (tmp
, 1, GFC_RND_MODE
);
6135 mpfr_set_exp (tmp
, norm2_scale
);
6136 mpfr_div (tmp
, e
->value
.real
, tmp
, GFC_RND_MODE
);
6139 mpfr_set (tmp
, e
->value
.real
, GFC_RND_MODE
);
6140 mpfr_pow_ui (tmp
, tmp
, 2, GFC_RND_MODE
);
6141 mpfr_add (result
->value
.real
, result
->value
.real
, tmp
,
6150 norm2_do_sqrt (gfc_expr
*result
, gfc_expr
*e
)
6152 gcc_assert (e
->ts
.type
== BT_REAL
&& e
->expr_type
== EXPR_CONSTANT
);
6153 gcc_assert (result
->ts
.type
== BT_REAL
6154 && result
->expr_type
== EXPR_CONSTANT
);
6157 mpfr_set (result
->value
.real
, e
->value
.real
, GFC_RND_MODE
);
6158 mpfr_sqrt (result
->value
.real
, result
->value
.real
, GFC_RND_MODE
);
6159 if (norm2_scale
&& mpfr_regular_p (result
->value
.real
))
6163 mpfr_set_ui (tmp
, 1, GFC_RND_MODE
);
6164 mpfr_set_exp (tmp
, norm2_scale
);
6165 mpfr_mul (result
->value
.real
, result
->value
.real
, tmp
, GFC_RND_MODE
);
6175 gfc_simplify_norm2 (gfc_expr
*e
, gfc_expr
*dim
)
6180 size_zero
= gfc_is_size_zero_array (e
);
6182 if (!(is_constant_array_expr (e
) || size_zero
)
6183 || (dim
!= NULL
&& !gfc_is_constant_expr (dim
)))
6186 result
= transformational_result (e
, dim
, e
->ts
.type
, e
->ts
.kind
, &e
->where
);
6187 init_result_expr (result
, 0, NULL
);
6193 if (!dim
|| e
->rank
== 1)
6195 result
= simplify_transformation_to_scalar (result
, e
, NULL
,
6197 mpfr_sqrt (result
->value
.real
, result
->value
.real
, GFC_RND_MODE
);
6198 if (norm2_scale
&& mpfr_regular_p (result
->value
.real
))
6202 mpfr_set_ui (tmp
, 1, GFC_RND_MODE
);
6203 mpfr_set_exp (tmp
, norm2_scale
);
6204 mpfr_mul (result
->value
.real
, result
->value
.real
, tmp
, GFC_RND_MODE
);
6210 result
= simplify_transformation_to_array (result
, e
, dim
, NULL
,
6219 gfc_simplify_not (gfc_expr
*e
)
6223 if (e
->expr_type
!= EXPR_CONSTANT
)
6226 result
= gfc_get_constant_expr (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
6227 mpz_com (result
->value
.integer
, e
->value
.integer
);
6229 return range_check (result
, "NOT");
6234 gfc_simplify_null (gfc_expr
*mold
)
6240 result
= gfc_copy_expr (mold
);
6241 result
->expr_type
= EXPR_NULL
;
6244 result
= gfc_get_null_expr (NULL
);
6251 gfc_simplify_num_images (gfc_expr
*distance ATTRIBUTE_UNUSED
, gfc_expr
*failed
)
6255 if (flag_coarray
== GFC_FCOARRAY_NONE
)
6257 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
6258 return &gfc_bad_expr
;
6261 if (flag_coarray
!= GFC_FCOARRAY_SINGLE
)
6264 if (failed
&& failed
->expr_type
!= EXPR_CONSTANT
)
6267 /* FIXME: gfc_current_locus is wrong. */
6268 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
6269 &gfc_current_locus
);
6271 if (failed
&& failed
->value
.logical
!= 0)
6272 mpz_set_si (result
->value
.integer
, 0);
6274 mpz_set_si (result
->value
.integer
, 1);
6281 gfc_simplify_or (gfc_expr
*x
, gfc_expr
*y
)
6286 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
6289 kind
= x
->ts
.kind
> y
->ts
.kind
? x
->ts
.kind
: y
->ts
.kind
;
6294 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &x
->where
);
6295 mpz_ior (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
6296 return range_check (result
, "OR");
6299 return gfc_get_logical_expr (kind
, &x
->where
,
6300 x
->value
.logical
|| y
->value
.logical
);
6308 gfc_simplify_pack (gfc_expr
*array
, gfc_expr
*mask
, gfc_expr
*vector
)
6311 gfc_constructor
*array_ctor
, *mask_ctor
, *vector_ctor
;
6313 if (!is_constant_array_expr (array
)
6314 || !is_constant_array_expr (vector
)
6315 || (!gfc_is_constant_expr (mask
)
6316 && !is_constant_array_expr (mask
)))
6319 result
= gfc_get_array_expr (array
->ts
.type
, array
->ts
.kind
, &array
->where
);
6320 if (array
->ts
.type
== BT_DERIVED
)
6321 result
->ts
.u
.derived
= array
->ts
.u
.derived
;
6323 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
6324 vector_ctor
= vector
6325 ? gfc_constructor_first (vector
->value
.constructor
)
6328 if (mask
->expr_type
== EXPR_CONSTANT
6329 && mask
->value
.logical
)
6331 /* Copy all elements of ARRAY to RESULT. */
6334 gfc_constructor_append_expr (&result
->value
.constructor
,
6335 gfc_copy_expr (array_ctor
->expr
),
6338 array_ctor
= gfc_constructor_next (array_ctor
);
6339 vector_ctor
= gfc_constructor_next (vector_ctor
);
6342 else if (mask
->expr_type
== EXPR_ARRAY
)
6344 /* Copy only those elements of ARRAY to RESULT whose
6345 MASK equals .TRUE.. */
6346 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
6349 if (mask_ctor
->expr
->value
.logical
)
6351 gfc_constructor_append_expr (&result
->value
.constructor
,
6352 gfc_copy_expr (array_ctor
->expr
),
6354 vector_ctor
= gfc_constructor_next (vector_ctor
);
6357 array_ctor
= gfc_constructor_next (array_ctor
);
6358 mask_ctor
= gfc_constructor_next (mask_ctor
);
6362 /* Append any left-over elements from VECTOR to RESULT. */
6365 gfc_constructor_append_expr (&result
->value
.constructor
,
6366 gfc_copy_expr (vector_ctor
->expr
),
6368 vector_ctor
= gfc_constructor_next (vector_ctor
);
6371 result
->shape
= gfc_get_shape (1);
6372 gfc_array_size (result
, &result
->shape
[0]);
6374 if (array
->ts
.type
== BT_CHARACTER
)
6375 result
->ts
.u
.cl
= array
->ts
.u
.cl
;
6382 do_xor (gfc_expr
*result
, gfc_expr
*e
)
6384 gcc_assert (e
->ts
.type
== BT_LOGICAL
&& e
->expr_type
== EXPR_CONSTANT
);
6385 gcc_assert (result
->ts
.type
== BT_LOGICAL
6386 && result
->expr_type
== EXPR_CONSTANT
);
6388 result
->value
.logical
= result
->value
.logical
!= e
->value
.logical
;
6394 gfc_simplify_is_contiguous (gfc_expr
*array
)
6396 if (gfc_is_simply_contiguous (array
, false, true))
6397 return gfc_get_logical_expr (gfc_default_logical_kind
, &array
->where
, 1);
6399 if (gfc_is_not_contiguous (array
))
6400 return gfc_get_logical_expr (gfc_default_logical_kind
, &array
->where
, 0);
6407 gfc_simplify_parity (gfc_expr
*e
, gfc_expr
*dim
)
6409 return simplify_transformation (e
, dim
, NULL
, 0, do_xor
);
6414 gfc_simplify_popcnt (gfc_expr
*e
)
6419 if (e
->expr_type
!= EXPR_CONSTANT
)
6422 k
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
6424 /* Convert argument to unsigned, then count the '1' bits. */
6425 mpz_init_set (x
, e
->value
.integer
);
6426 convert_mpz_to_unsigned (x
, gfc_integer_kinds
[k
].bit_size
);
6427 res
= mpz_popcount (x
);
6430 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, res
);
6435 gfc_simplify_poppar (gfc_expr
*e
)
6440 if (e
->expr_type
!= EXPR_CONSTANT
)
6443 popcnt
= gfc_simplify_popcnt (e
);
6444 gcc_assert (popcnt
);
6446 bool fail
= gfc_extract_int (popcnt
, &i
);
6449 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, i
% 2);
6454 gfc_simplify_precision (gfc_expr
*e
)
6456 int i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
6457 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
,
6458 gfc_real_kinds
[i
].precision
);
6463 gfc_simplify_product (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
)
6465 return simplify_transformation (array
, dim
, mask
, 1, gfc_multiply
);
6470 gfc_simplify_radix (gfc_expr
*e
)
6473 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
6478 i
= gfc_integer_kinds
[i
].radix
;
6482 i
= gfc_real_kinds
[i
].radix
;
6489 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, i
);
6494 gfc_simplify_range (gfc_expr
*e
)
6497 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
6502 i
= gfc_integer_kinds
[i
].range
;
6507 i
= gfc_real_kinds
[i
].range
;
6514 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, i
);
6519 gfc_simplify_rank (gfc_expr
*e
)
6525 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, e
->rank
);
6530 gfc_simplify_real (gfc_expr
*e
, gfc_expr
*k
)
6532 gfc_expr
*result
= NULL
;
6533 int kind
, tmp1
, tmp2
;
6535 /* Convert BOZ to real, and return without range checking. */
6536 if (e
->ts
.type
== BT_BOZ
)
6538 /* Determine kind for conversion of the BOZ. */
6540 gfc_extract_int (k
, &kind
);
6542 kind
= gfc_default_real_kind
;
6544 if (!gfc_boz2real (e
, kind
))
6546 result
= gfc_copy_expr (e
);
6550 if (e
->ts
.type
== BT_COMPLEX
)
6551 kind
= get_kind (BT_REAL
, k
, "REAL", e
->ts
.kind
);
6553 kind
= get_kind (BT_REAL
, k
, "REAL", gfc_default_real_kind
);
6556 return &gfc_bad_expr
;
6558 if (e
->expr_type
!= EXPR_CONSTANT
)
6561 /* For explicit conversion, turn off -Wconversion and -Wconversion-extra
6563 tmp1
= warn_conversion
;
6564 tmp2
= warn_conversion_extra
;
6565 warn_conversion
= warn_conversion_extra
= 0;
6567 result
= gfc_convert_constant (e
, BT_REAL
, kind
);
6569 warn_conversion
= tmp1
;
6570 warn_conversion_extra
= tmp2
;
6572 if (result
== &gfc_bad_expr
)
6573 return &gfc_bad_expr
;
6575 return range_check (result
, "REAL");
6580 gfc_simplify_realpart (gfc_expr
*e
)
6584 if (e
->expr_type
!= EXPR_CONSTANT
)
6587 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
6588 mpc_real (result
->value
.real
, e
->value
.complex, GFC_RND_MODE
);
6590 return range_check (result
, "REALPART");
6594 gfc_simplify_repeat (gfc_expr
*e
, gfc_expr
*n
)
6599 bool have_length
= false;
6601 /* If NCOPIES isn't a constant, there's nothing we can do. */
6602 if (n
->expr_type
!= EXPR_CONSTANT
)
6605 /* If NCOPIES is negative, it's an error. */
6606 if (mpz_sgn (n
->value
.integer
) < 0)
6608 gfc_error ("Argument NCOPIES of REPEAT intrinsic is negative at %L",
6610 return &gfc_bad_expr
;
6613 /* If we don't know the character length, we can do no more. */
6614 if (e
->ts
.u
.cl
&& e
->ts
.u
.cl
->length
6615 && e
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
6617 len
= gfc_mpz_get_hwi (e
->ts
.u
.cl
->length
->value
.integer
);
6620 else if (e
->expr_type
== EXPR_CONSTANT
6621 && (e
->ts
.u
.cl
== NULL
|| e
->ts
.u
.cl
->length
== NULL
))
6623 len
= e
->value
.character
.length
;
6628 /* If the source length is 0, any value of NCOPIES is valid
6629 and everything behaves as if NCOPIES == 0. */
6632 mpz_set_ui (ncopies
, 0);
6634 mpz_set (ncopies
, n
->value
.integer
);
6636 /* Check that NCOPIES isn't too large. */
6642 /* Compute the maximum value allowed for NCOPIES: huge(cl) / len. */
6644 i
= gfc_validate_kind (BT_INTEGER
, gfc_charlen_int_kind
, false);
6648 mpz_tdiv_q (max
, gfc_integer_kinds
[i
].huge
,
6649 e
->ts
.u
.cl
->length
->value
.integer
);
6654 gfc_mpz_set_hwi (mlen
, len
);
6655 mpz_tdiv_q (max
, gfc_integer_kinds
[i
].huge
, mlen
);
6659 /* The check itself. */
6660 if (mpz_cmp (ncopies
, max
) > 0)
6663 mpz_clear (ncopies
);
6664 gfc_error ("Argument NCOPIES of REPEAT intrinsic is too large at %L",
6666 return &gfc_bad_expr
;
6671 mpz_clear (ncopies
);
6673 /* For further simplification, we need the character string to be
6675 if (e
->expr_type
!= EXPR_CONSTANT
)
6680 (e
->ts
.u
.cl
->length
&&
6681 mpz_sgn (e
->ts
.u
.cl
->length
->value
.integer
) != 0))
6683 bool fail
= gfc_extract_hwi (n
, &ncop
);
6690 return gfc_get_character_expr (e
->ts
.kind
, &e
->where
, NULL
, 0);
6692 len
= e
->value
.character
.length
;
6693 gfc_charlen_t nlen
= ncop
* len
;
6695 /* Here's a semi-arbitrary limit. If the string is longer than 1 GB
6696 (2**28 elements * 4 bytes (wide chars) per element) defer to
6697 runtime instead of consuming (unbounded) memory and CPU at
6699 if (nlen
> 268435456)
6701 gfc_warning_now (0, "Evaluation of string longer than 2**28 at %L"
6702 " deferred to runtime, expect bugs", &e
->where
);
6706 result
= gfc_get_character_expr (e
->ts
.kind
, &e
->where
, NULL
, nlen
);
6707 for (size_t i
= 0; i
< (size_t) ncop
; i
++)
6708 for (size_t j
= 0; j
< (size_t) len
; j
++)
6709 result
->value
.character
.string
[j
+i
*len
]= e
->value
.character
.string
[j
];
6711 result
->value
.character
.string
[nlen
] = '\0'; /* For debugger */
6716 /* This one is a bear, but mainly has to do with shuffling elements. */
6719 gfc_simplify_reshape (gfc_expr
*source
, gfc_expr
*shape_exp
,
6720 gfc_expr
*pad
, gfc_expr
*order_exp
)
6722 int order
[GFC_MAX_DIMENSIONS
], shape
[GFC_MAX_DIMENSIONS
];
6723 int i
, rank
, npad
, x
[GFC_MAX_DIMENSIONS
];
6727 gfc_expr
*e
, *result
;
6728 bool zerosize
= false;
6730 /* Check that argument expression types are OK. */
6731 if (!is_constant_array_expr (source
)
6732 || !is_constant_array_expr (shape_exp
)
6733 || !is_constant_array_expr (pad
)
6734 || !is_constant_array_expr (order_exp
))
6737 if (source
->shape
== NULL
)
6740 /* Proceed with simplification, unpacking the array. */
6745 for (i
= 0; i
< GFC_MAX_DIMENSIONS
; i
++)
6750 e
= gfc_constructor_lookup_expr (shape_exp
->value
.constructor
, rank
);
6754 gfc_extract_int (e
, &shape
[rank
]);
6756 gcc_assert (rank
>= 0 && rank
< GFC_MAX_DIMENSIONS
);
6757 gcc_assert (shape
[rank
] >= 0);
6762 gcc_assert (rank
> 0);
6764 /* Now unpack the order array if present. */
6765 if (order_exp
== NULL
)
6767 for (i
= 0; i
< rank
; i
++)
6773 int order_size
, shape_size
;
6775 if (order_exp
->rank
!= shape_exp
->rank
)
6777 gfc_error ("Shapes of ORDER at %L and SHAPE at %L are different",
6778 &order_exp
->where
, &shape_exp
->where
);
6779 return &gfc_bad_expr
;
6782 gfc_array_size (shape_exp
, &size
);
6783 shape_size
= mpz_get_ui (size
);
6785 gfc_array_size (order_exp
, &size
);
6786 order_size
= mpz_get_ui (size
);
6788 if (order_size
!= shape_size
)
6790 gfc_error ("Sizes of ORDER at %L and SHAPE at %L are different",
6791 &order_exp
->where
, &shape_exp
->where
);
6792 return &gfc_bad_expr
;
6795 for (i
= 0; i
< rank
; i
++)
6797 e
= gfc_constructor_lookup_expr (order_exp
->value
.constructor
, i
);
6800 gfc_extract_int (e
, &order
[i
]);
6802 if (order
[i
] < 1 || order
[i
] > rank
)
6804 gfc_error ("Element with a value of %d in ORDER at %L must be "
6805 "in the range [1, ..., %d] for the RESHAPE intrinsic "
6806 "near %L", order
[i
], &order_exp
->where
, rank
,
6808 return &gfc_bad_expr
;
6812 if (x
[order
[i
]] != 0)
6814 gfc_error ("ORDER at %L is not a permutation of the size of "
6815 "SHAPE at %L", &order_exp
->where
, &shape_exp
->where
);
6816 return &gfc_bad_expr
;
6822 /* Count the elements in the source and padding arrays. */
6827 gfc_array_size (pad
, &size
);
6828 npad
= mpz_get_ui (size
);
6832 gfc_array_size (source
, &size
);
6833 nsource
= mpz_get_ui (size
);
6836 /* If it weren't for that pesky permutation we could just loop
6837 through the source and round out any shortage with pad elements.
6838 But no, someone just had to have the compiler do something the
6839 user should be doing. */
6841 for (i
= 0; i
< rank
; i
++)
6844 result
= gfc_get_array_expr (source
->ts
.type
, source
->ts
.kind
,
6846 if (source
->ts
.type
== BT_DERIVED
)
6847 result
->ts
.u
.derived
= source
->ts
.u
.derived
;
6848 result
->rank
= rank
;
6849 result
->shape
= gfc_get_shape (rank
);
6850 for (i
= 0; i
< rank
; i
++)
6852 mpz_init_set_ui (result
->shape
[i
], shape
[i
]);
6860 while (nsource
> 0 || npad
> 0)
6862 /* Figure out which element to extract. */
6863 mpz_set_ui (index
, 0);
6865 for (i
= rank
- 1; i
>= 0; i
--)
6867 mpz_add_ui (index
, index
, x
[order
[i
]]);
6869 mpz_mul_ui (index
, index
, shape
[order
[i
- 1]]);
6872 if (mpz_cmp_ui (index
, INT_MAX
) > 0)
6873 gfc_internal_error ("Reshaped array too large at %C");
6875 j
= mpz_get_ui (index
);
6878 e
= gfc_constructor_lookup_expr (source
->value
.constructor
, j
);
6888 e
= gfc_constructor_lookup_expr (pad
->value
.constructor
, j
);
6892 gfc_constructor_append_expr (&result
->value
.constructor
,
6893 gfc_copy_expr (e
), &e
->where
);
6895 /* Calculate the next element. */
6899 if (++x
[i
] < shape
[i
])
6917 gfc_simplify_rrspacing (gfc_expr
*x
)
6923 if (x
->expr_type
!= EXPR_CONSTANT
)
6926 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
6928 result
= gfc_get_constant_expr (BT_REAL
, x
->ts
.kind
, &x
->where
);
6930 /* RRSPACING(+/- 0.0) = 0.0 */
6931 if (mpfr_zero_p (x
->value
.real
))
6933 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
6937 /* RRSPACING(inf) = NaN */
6938 if (mpfr_inf_p (x
->value
.real
))
6940 mpfr_set_nan (result
->value
.real
);
6944 /* RRSPACING(NaN) = same NaN */
6945 if (mpfr_nan_p (x
->value
.real
))
6947 mpfr_set (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
6951 /* | x * 2**(-e) | * 2**p. */
6952 mpfr_abs (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
6953 e
= - (long int) mpfr_get_exp (x
->value
.real
);
6954 mpfr_mul_2si (result
->value
.real
, result
->value
.real
, e
, GFC_RND_MODE
);
6956 p
= (long int) gfc_real_kinds
[i
].digits
;
6957 mpfr_mul_2si (result
->value
.real
, result
->value
.real
, p
, GFC_RND_MODE
);
6959 return range_check (result
, "RRSPACING");
6964 gfc_simplify_scale (gfc_expr
*x
, gfc_expr
*i
)
6966 int k
, neg_flag
, power
, exp_range
;
6967 mpfr_t scale
, radix
;
6970 if (x
->expr_type
!= EXPR_CONSTANT
|| i
->expr_type
!= EXPR_CONSTANT
)
6973 result
= gfc_get_constant_expr (BT_REAL
, x
->ts
.kind
, &x
->where
);
6975 if (mpfr_zero_p (x
->value
.real
))
6977 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
6981 k
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, false);
6983 exp_range
= gfc_real_kinds
[k
].max_exponent
- gfc_real_kinds
[k
].min_exponent
;
6985 /* This check filters out values of i that would overflow an int. */
6986 if (mpz_cmp_si (i
->value
.integer
, exp_range
+ 2) > 0
6987 || mpz_cmp_si (i
->value
.integer
, -exp_range
- 2) < 0)
6989 gfc_error ("Result of SCALE overflows its kind at %L", &result
->where
);
6990 gfc_free_expr (result
);
6991 return &gfc_bad_expr
;
6994 /* Compute scale = radix ** power. */
6995 power
= mpz_get_si (i
->value
.integer
);
7005 gfc_set_model_kind (x
->ts
.kind
);
7008 mpfr_set_ui (radix
, gfc_real_kinds
[k
].radix
, GFC_RND_MODE
);
7009 mpfr_pow_ui (scale
, radix
, power
, GFC_RND_MODE
);
7012 mpfr_div (result
->value
.real
, x
->value
.real
, scale
, GFC_RND_MODE
);
7014 mpfr_mul (result
->value
.real
, x
->value
.real
, scale
, GFC_RND_MODE
);
7016 mpfr_clears (scale
, radix
, NULL
);
7018 return range_check (result
, "SCALE");
7022 /* Variants of strspn and strcspn that operate on wide characters. */
7025 wide_strspn (const gfc_char_t
*s1
, const gfc_char_t
*s2
)
7028 const gfc_char_t
*c
;
7032 for (c
= s2
; *c
; c
++)
7046 wide_strcspn (const gfc_char_t
*s1
, const gfc_char_t
*s2
)
7049 const gfc_char_t
*c
;
7053 for (c
= s2
; *c
; c
++)
7068 gfc_simplify_scan (gfc_expr
*e
, gfc_expr
*c
, gfc_expr
*b
, gfc_expr
*kind
)
7073 size_t indx
, len
, lenc
;
7074 int k
= get_kind (BT_INTEGER
, kind
, "SCAN", gfc_default_integer_kind
);
7077 return &gfc_bad_expr
;
7079 if (e
->expr_type
!= EXPR_CONSTANT
|| c
->expr_type
!= EXPR_CONSTANT
7080 || ( b
!= NULL
&& b
->expr_type
!= EXPR_CONSTANT
))
7083 if (b
!= NULL
&& b
->value
.logical
!= 0)
7088 len
= e
->value
.character
.length
;
7089 lenc
= c
->value
.character
.length
;
7091 if (len
== 0 || lenc
== 0)
7099 indx
= wide_strcspn (e
->value
.character
.string
,
7100 c
->value
.character
.string
) + 1;
7105 for (indx
= len
; indx
> 0; indx
--)
7107 for (i
= 0; i
< lenc
; i
++)
7109 if (c
->value
.character
.string
[i
]
7110 == e
->value
.character
.string
[indx
- 1])
7118 result
= gfc_get_int_expr (k
, &e
->where
, indx
);
7119 return range_check (result
, "SCAN");
7124 gfc_simplify_selected_char_kind (gfc_expr
*e
)
7128 if (e
->expr_type
!= EXPR_CONSTANT
)
7131 if (gfc_compare_with_Cstring (e
, "ascii", false) == 0
7132 || gfc_compare_with_Cstring (e
, "default", false) == 0)
7134 else if (gfc_compare_with_Cstring (e
, "iso_10646", false) == 0)
7139 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, kind
);
7144 gfc_simplify_selected_int_kind (gfc_expr
*e
)
7148 if (e
->expr_type
!= EXPR_CONSTANT
|| gfc_extract_int (e
, &range
))
7153 for (i
= 0; gfc_integer_kinds
[i
].kind
!= 0; i
++)
7154 if (gfc_integer_kinds
[i
].range
>= range
7155 && gfc_integer_kinds
[i
].kind
< kind
)
7156 kind
= gfc_integer_kinds
[i
].kind
;
7158 if (kind
== INT_MAX
)
7161 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, kind
);
7166 gfc_simplify_selected_real_kind (gfc_expr
*p
, gfc_expr
*q
, gfc_expr
*rdx
)
7168 int range
, precision
, radix
, i
, kind
, found_precision
, found_range
,
7170 locus
*loc
= &gfc_current_locus
;
7176 if (p
->expr_type
!= EXPR_CONSTANT
7177 || gfc_extract_int (p
, &precision
))
7186 if (q
->expr_type
!= EXPR_CONSTANT
7187 || gfc_extract_int (q
, &range
))
7198 if (rdx
->expr_type
!= EXPR_CONSTANT
7199 || gfc_extract_int (rdx
, &radix
))
7207 found_precision
= 0;
7211 for (i
= 0; gfc_real_kinds
[i
].kind
!= 0; i
++)
7213 if (gfc_real_kinds
[i
].precision
>= precision
)
7214 found_precision
= 1;
7216 if (gfc_real_kinds
[i
].range
>= range
)
7219 if (radix
== 0 || gfc_real_kinds
[i
].radix
== radix
)
7222 if (gfc_real_kinds
[i
].precision
>= precision
7223 && gfc_real_kinds
[i
].range
>= range
7224 && (radix
== 0 || gfc_real_kinds
[i
].radix
== radix
)
7225 && gfc_real_kinds
[i
].kind
< kind
)
7226 kind
= gfc_real_kinds
[i
].kind
;
7229 if (kind
== INT_MAX
)
7231 if (found_radix
&& found_range
&& !found_precision
)
7233 else if (found_radix
&& found_precision
&& !found_range
)
7235 else if (found_radix
&& !found_precision
&& !found_range
)
7237 else if (found_radix
)
7243 return gfc_get_int_expr (gfc_default_integer_kind
, loc
, kind
);
7248 gfc_simplify_set_exponent (gfc_expr
*x
, gfc_expr
*i
)
7251 mpfr_t exp
, absv
, log2
, pow2
, frac
;
7254 if (x
->expr_type
!= EXPR_CONSTANT
|| i
->expr_type
!= EXPR_CONSTANT
)
7257 result
= gfc_get_constant_expr (BT_REAL
, x
->ts
.kind
, &x
->where
);
7259 /* SET_EXPONENT (+/-0.0, I) = +/- 0.0
7260 SET_EXPONENT (NaN) = same NaN */
7261 if (mpfr_zero_p (x
->value
.real
) || mpfr_nan_p (x
->value
.real
))
7263 mpfr_set (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
7267 /* SET_EXPONENT (inf) = NaN */
7268 if (mpfr_inf_p (x
->value
.real
))
7270 mpfr_set_nan (result
->value
.real
);
7274 gfc_set_model_kind (x
->ts
.kind
);
7281 mpfr_abs (absv
, x
->value
.real
, GFC_RND_MODE
);
7282 mpfr_log2 (log2
, absv
, GFC_RND_MODE
);
7284 mpfr_trunc (log2
, log2
);
7285 mpfr_add_ui (exp
, log2
, 1, GFC_RND_MODE
);
7287 /* Old exponent value, and fraction. */
7288 mpfr_ui_pow (pow2
, 2, exp
, GFC_RND_MODE
);
7290 mpfr_div (frac
, absv
, pow2
, GFC_RND_MODE
);
7293 exp2
= (unsigned long) mpz_get_d (i
->value
.integer
);
7294 mpfr_mul_2exp (result
->value
.real
, frac
, exp2
, GFC_RND_MODE
);
7296 mpfr_clears (absv
, log2
, pow2
, frac
, NULL
);
7298 return range_check (result
, "SET_EXPONENT");
7303 gfc_simplify_shape (gfc_expr
*source
, gfc_expr
*kind
)
7305 mpz_t shape
[GFC_MAX_DIMENSIONS
];
7306 gfc_expr
*result
, *e
, *f
;
7310 int k
= get_kind (BT_INTEGER
, kind
, "SHAPE", gfc_default_integer_kind
);
7312 if (source
->rank
== -1)
7315 result
= gfc_get_array_expr (BT_INTEGER
, k
, &source
->where
);
7316 result
->shape
= gfc_get_shape (1);
7317 mpz_init (result
->shape
[0]);
7319 if (source
->rank
== 0)
7322 if (source
->expr_type
== EXPR_VARIABLE
)
7324 ar
= gfc_find_array_ref (source
);
7325 t
= gfc_array_ref_shape (ar
, shape
);
7327 else if (source
->shape
)
7330 for (n
= 0; n
< source
->rank
; n
++)
7332 mpz_init (shape
[n
]);
7333 mpz_set (shape
[n
], source
->shape
[n
]);
7339 for (n
= 0; n
< source
->rank
; n
++)
7341 e
= gfc_get_constant_expr (BT_INTEGER
, k
, &source
->where
);
7344 mpz_set (e
->value
.integer
, shape
[n
]);
7347 mpz_set_ui (e
->value
.integer
, n
+ 1);
7349 f
= simplify_size (source
, e
, k
);
7353 gfc_free_expr (result
);
7360 if (e
== &gfc_bad_expr
|| range_check (e
, "SHAPE") == &gfc_bad_expr
)
7362 gfc_free_expr (result
);
7364 gfc_clear_shape (shape
, source
->rank
);
7365 return &gfc_bad_expr
;
7368 gfc_constructor_append_expr (&result
->value
.constructor
, e
, NULL
);
7372 gfc_clear_shape (shape
, source
->rank
);
7374 mpz_set_si (result
->shape
[0], source
->rank
);
7381 simplify_size (gfc_expr
*array
, gfc_expr
*dim
, int k
)
7384 gfc_expr
*return_value
;
7387 /* For unary operations, the size of the result is given by the size
7388 of the operand. For binary ones, it's the size of the first operand
7389 unless it is scalar, then it is the size of the second. */
7390 if (array
->expr_type
== EXPR_OP
&& !array
->value
.op
.uop
)
7392 gfc_expr
* replacement
;
7393 gfc_expr
* simplified
;
7395 switch (array
->value
.op
.op
)
7397 /* Unary operations. */
7399 case INTRINSIC_UPLUS
:
7400 case INTRINSIC_UMINUS
:
7401 case INTRINSIC_PARENTHESES
:
7402 replacement
= array
->value
.op
.op1
;
7405 /* Binary operations. If any one of the operands is scalar, take
7406 the other one's size. If both of them are arrays, it does not
7407 matter -- try to find one with known shape, if possible. */
7409 if (array
->value
.op
.op1
->rank
== 0)
7410 replacement
= array
->value
.op
.op2
;
7411 else if (array
->value
.op
.op2
->rank
== 0)
7412 replacement
= array
->value
.op
.op1
;
7415 simplified
= simplify_size (array
->value
.op
.op1
, dim
, k
);
7419 replacement
= array
->value
.op
.op2
;
7424 /* Try to reduce it directly if possible. */
7425 simplified
= simplify_size (replacement
, dim
, k
);
7427 /* Otherwise, we build a new SIZE call. This is hopefully at least
7428 simpler than the original one. */
7431 gfc_expr
*kind
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, k
);
7432 simplified
= gfc_build_intrinsic_call (gfc_current_ns
,
7433 GFC_ISYM_SIZE
, "size",
7435 gfc_copy_expr (replacement
),
7436 gfc_copy_expr (dim
),
7444 if (!gfc_array_size (array
, &size
))
7449 if (dim
->expr_type
!= EXPR_CONSTANT
)
7452 d
= mpz_get_ui (dim
->value
.integer
) - 1;
7453 if (!gfc_array_dimen_size (array
, d
, &size
))
7457 return_value
= gfc_get_constant_expr (BT_INTEGER
, k
, &array
->where
);
7458 mpz_set (return_value
->value
.integer
, size
);
7461 return return_value
;
7466 gfc_simplify_size (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
)
7469 int k
= get_kind (BT_INTEGER
, kind
, "SIZE", gfc_default_integer_kind
);
7472 return &gfc_bad_expr
;
7474 result
= simplify_size (array
, dim
, k
);
7475 if (result
== NULL
|| result
== &gfc_bad_expr
)
7478 return range_check (result
, "SIZE");
7482 /* SIZEOF and C_SIZEOF return the size in bytes of an array element
7483 multiplied by the array size. */
7486 gfc_simplify_sizeof (gfc_expr
*x
)
7488 gfc_expr
*result
= NULL
;
7492 if (x
->ts
.type
== BT_CLASS
|| x
->ts
.deferred
)
7495 if (x
->ts
.type
== BT_CHARACTER
7496 && (!x
->ts
.u
.cl
|| !x
->ts
.u
.cl
->length
7497 || x
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
))
7500 if (x
->rank
&& x
->expr_type
!= EXPR_ARRAY
7501 && !gfc_array_size (x
, &array_size
))
7504 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_index_integer_kind
,
7506 gfc_target_expr_size (x
, &res_size
);
7507 mpz_set_si (result
->value
.integer
, res_size
);
7513 /* STORAGE_SIZE returns the size in bits of a single array element. */
7516 gfc_simplify_storage_size (gfc_expr
*x
,
7519 gfc_expr
*result
= NULL
;
7523 if (x
->ts
.type
== BT_CLASS
|| x
->ts
.deferred
)
7526 if (x
->ts
.type
== BT_CHARACTER
&& x
->expr_type
!= EXPR_CONSTANT
7527 && (!x
->ts
.u
.cl
|| !x
->ts
.u
.cl
->length
7528 || x
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
))
7531 k
= get_kind (BT_INTEGER
, kind
, "STORAGE_SIZE", gfc_default_integer_kind
);
7533 return &gfc_bad_expr
;
7535 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &x
->where
);
7537 gfc_element_size (x
, &siz
);
7538 mpz_set_si (result
->value
.integer
, siz
);
7539 mpz_mul_ui (result
->value
.integer
, result
->value
.integer
, BITS_PER_UNIT
);
7541 return range_check (result
, "STORAGE_SIZE");
7546 gfc_simplify_sign (gfc_expr
*x
, gfc_expr
*y
)
7550 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
7553 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
7558 mpz_abs (result
->value
.integer
, x
->value
.integer
);
7559 if (mpz_sgn (y
->value
.integer
) < 0)
7560 mpz_neg (result
->value
.integer
, result
->value
.integer
);
7565 mpfr_copysign (result
->value
.real
, x
->value
.real
, y
->value
.real
,
7568 mpfr_setsign (result
->value
.real
, x
->value
.real
,
7569 mpfr_sgn (y
->value
.real
) < 0 ? 1 : 0, GFC_RND_MODE
);
7573 gfc_internal_error ("Bad type in gfc_simplify_sign");
7581 gfc_simplify_sin (gfc_expr
*x
)
7585 if (x
->expr_type
!= EXPR_CONSTANT
)
7588 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
7593 mpfr_sin (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
7597 gfc_set_model (x
->value
.real
);
7598 mpc_sin (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
7602 gfc_internal_error ("in gfc_simplify_sin(): Bad type");
7605 return range_check (result
, "SIN");
7610 gfc_simplify_sinh (gfc_expr
*x
)
7614 if (x
->expr_type
!= EXPR_CONSTANT
)
7617 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
7622 mpfr_sinh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
7626 mpc_sinh (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
7633 return range_check (result
, "SINH");
7637 /* The argument is always a double precision real that is converted to
7638 single precision. TODO: Rounding! */
7641 gfc_simplify_sngl (gfc_expr
*a
)
7646 if (a
->expr_type
!= EXPR_CONSTANT
)
7649 /* For explicit conversion, turn off -Wconversion and -Wconversion-extra
7651 tmp1
= warn_conversion
;
7652 tmp2
= warn_conversion_extra
;
7653 warn_conversion
= warn_conversion_extra
= 0;
7655 result
= gfc_real2real (a
, gfc_default_real_kind
);
7657 warn_conversion
= tmp1
;
7658 warn_conversion_extra
= tmp2
;
7660 return range_check (result
, "SNGL");
7665 gfc_simplify_spacing (gfc_expr
*x
)
7671 if (x
->expr_type
!= EXPR_CONSTANT
)
7674 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
7675 result
= gfc_get_constant_expr (BT_REAL
, x
->ts
.kind
, &x
->where
);
7677 /* SPACING(+/- 0.0) = SPACING(TINY(0.0)) = TINY(0.0) */
7678 if (mpfr_zero_p (x
->value
.real
))
7680 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].tiny
, GFC_RND_MODE
);
7684 /* SPACING(inf) = NaN */
7685 if (mpfr_inf_p (x
->value
.real
))
7687 mpfr_set_nan (result
->value
.real
);
7691 /* SPACING(NaN) = same NaN */
7692 if (mpfr_nan_p (x
->value
.real
))
7694 mpfr_set (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
7698 /* In the Fortran 95 standard, the result is b**(e - p) where b, e, and p
7699 are the radix, exponent of x, and precision. This excludes the
7700 possibility of subnormal numbers. Fortran 2003 states the result is
7701 b**max(e - p, emin - 1). */
7703 ep
= (long int) mpfr_get_exp (x
->value
.real
) - gfc_real_kinds
[i
].digits
;
7704 en
= (long int) gfc_real_kinds
[i
].min_exponent
- 1;
7705 en
= en
> ep
? en
: ep
;
7707 mpfr_set_ui (result
->value
.real
, 1, GFC_RND_MODE
);
7708 mpfr_mul_2si (result
->value
.real
, result
->value
.real
, en
, GFC_RND_MODE
);
7710 return range_check (result
, "SPACING");
7715 gfc_simplify_spread (gfc_expr
*source
, gfc_expr
*dim_expr
, gfc_expr
*ncopies_expr
)
7717 gfc_expr
*result
= NULL
;
7718 int nelem
, i
, j
, dim
, ncopies
;
7721 if ((!gfc_is_constant_expr (source
)
7722 && !is_constant_array_expr (source
))
7723 || !gfc_is_constant_expr (dim_expr
)
7724 || !gfc_is_constant_expr (ncopies_expr
))
7727 gcc_assert (dim_expr
->ts
.type
== BT_INTEGER
);
7728 gfc_extract_int (dim_expr
, &dim
);
7729 dim
-= 1; /* zero-base DIM */
7731 gcc_assert (ncopies_expr
->ts
.type
== BT_INTEGER
);
7732 gfc_extract_int (ncopies_expr
, &ncopies
);
7733 ncopies
= MAX (ncopies
, 0);
7735 /* Do not allow the array size to exceed the limit for an array
7737 if (source
->expr_type
== EXPR_ARRAY
)
7739 if (!gfc_array_size (source
, &size
))
7740 gfc_internal_error ("Failure getting length of a constant array.");
7743 mpz_init_set_ui (size
, 1);
7745 nelem
= mpz_get_si (size
) * ncopies
;
7746 if (nelem
> flag_max_array_constructor
)
7748 if (gfc_init_expr_flag
)
7750 gfc_error ("The number of elements (%d) in the array constructor "
7751 "at %L requires an increase of the allowed %d upper "
7752 "limit. See %<-fmax-array-constructor%> option.",
7753 nelem
, &source
->where
, flag_max_array_constructor
);
7754 return &gfc_bad_expr
;
7760 if (source
->expr_type
== EXPR_CONSTANT
7761 || source
->expr_type
== EXPR_STRUCTURE
)
7763 gcc_assert (dim
== 0);
7765 result
= gfc_get_array_expr (source
->ts
.type
, source
->ts
.kind
,
7767 if (source
->ts
.type
== BT_DERIVED
)
7768 result
->ts
.u
.derived
= source
->ts
.u
.derived
;
7770 result
->shape
= gfc_get_shape (result
->rank
);
7771 mpz_init_set_si (result
->shape
[0], ncopies
);
7773 for (i
= 0; i
< ncopies
; ++i
)
7774 gfc_constructor_append_expr (&result
->value
.constructor
,
7775 gfc_copy_expr (source
), NULL
);
7777 else if (source
->expr_type
== EXPR_ARRAY
)
7779 int offset
, rstride
[GFC_MAX_DIMENSIONS
], extent
[GFC_MAX_DIMENSIONS
];
7780 gfc_constructor
*source_ctor
;
7782 gcc_assert (source
->rank
< GFC_MAX_DIMENSIONS
);
7783 gcc_assert (dim
>= 0 && dim
<= source
->rank
);
7785 result
= gfc_get_array_expr (source
->ts
.type
, source
->ts
.kind
,
7787 if (source
->ts
.type
== BT_DERIVED
)
7788 result
->ts
.u
.derived
= source
->ts
.u
.derived
;
7789 result
->rank
= source
->rank
+ 1;
7790 result
->shape
= gfc_get_shape (result
->rank
);
7792 for (i
= 0, j
= 0; i
< result
->rank
; ++i
)
7795 mpz_init_set (result
->shape
[i
], source
->shape
[j
++]);
7797 mpz_init_set_si (result
->shape
[i
], ncopies
);
7799 extent
[i
] = mpz_get_si (result
->shape
[i
]);
7800 rstride
[i
] = (i
== 0) ? 1 : rstride
[i
-1] * extent
[i
-1];
7804 for (source_ctor
= gfc_constructor_first (source
->value
.constructor
);
7805 source_ctor
; source_ctor
= gfc_constructor_next (source_ctor
))
7807 for (i
= 0; i
< ncopies
; ++i
)
7808 gfc_constructor_insert_expr (&result
->value
.constructor
,
7809 gfc_copy_expr (source_ctor
->expr
),
7810 NULL
, offset
+ i
* rstride
[dim
]);
7812 offset
+= (dim
== 0 ? ncopies
: 1);
7817 gfc_error ("Simplification of SPREAD at %C not yet implemented");
7818 return &gfc_bad_expr
;
7821 if (source
->ts
.type
== BT_CHARACTER
)
7822 result
->ts
.u
.cl
= source
->ts
.u
.cl
;
7829 gfc_simplify_sqrt (gfc_expr
*e
)
7831 gfc_expr
*result
= NULL
;
7833 if (e
->expr_type
!= EXPR_CONSTANT
)
7839 if (mpfr_cmp_si (e
->value
.real
, 0) < 0)
7841 gfc_error ("Argument of SQRT at %L has a negative value",
7843 return &gfc_bad_expr
;
7845 result
= gfc_get_constant_expr (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
7846 mpfr_sqrt (result
->value
.real
, e
->value
.real
, GFC_RND_MODE
);
7850 gfc_set_model (e
->value
.real
);
7852 result
= gfc_get_constant_expr (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
7853 mpc_sqrt (result
->value
.complex, e
->value
.complex, GFC_MPC_RND_MODE
);
7857 gfc_internal_error ("invalid argument of SQRT at %L", &e
->where
);
7860 return range_check (result
, "SQRT");
7865 gfc_simplify_sum (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
)
7867 return simplify_transformation (array
, dim
, mask
, 0, gfc_add
);
7871 /* Simplify COTAN(X) where X has the unit of radian. */
7874 gfc_simplify_cotan (gfc_expr
*x
)
7879 if (x
->expr_type
!= EXPR_CONSTANT
)
7882 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
7887 mpfr_cot (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
7891 /* There is no builtin mpc_cot, so compute cot = cos / sin. */
7892 val
= &result
->value
.complex;
7893 mpc_init2 (swp
, mpfr_get_default_prec ());
7894 mpc_sin_cos (*val
, swp
, x
->value
.complex, GFC_MPC_RND_MODE
,
7896 mpc_div (*val
, swp
, *val
, GFC_MPC_RND_MODE
);
7904 return range_check (result
, "COTAN");
7909 gfc_simplify_tan (gfc_expr
*x
)
7913 if (x
->expr_type
!= EXPR_CONSTANT
)
7916 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
7921 mpfr_tan (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
7925 mpc_tan (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
7932 return range_check (result
, "TAN");
7937 gfc_simplify_tanh (gfc_expr
*x
)
7941 if (x
->expr_type
!= EXPR_CONSTANT
)
7944 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
7949 mpfr_tanh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
7953 mpc_tanh (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
7960 return range_check (result
, "TANH");
7965 gfc_simplify_tiny (gfc_expr
*e
)
7970 i
= gfc_validate_kind (BT_REAL
, e
->ts
.kind
, false);
7972 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
7973 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].tiny
, GFC_RND_MODE
);
7980 gfc_simplify_trailz (gfc_expr
*e
)
7982 unsigned long tz
, bs
;
7985 if (e
->expr_type
!= EXPR_CONSTANT
)
7988 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
7989 bs
= gfc_integer_kinds
[i
].bit_size
;
7990 tz
= mpz_scan1 (e
->value
.integer
, 0);
7992 return gfc_get_int_expr (gfc_default_integer_kind
,
7993 &e
->where
, MIN (tz
, bs
));
7998 gfc_simplify_transfer (gfc_expr
*source
, gfc_expr
*mold
, gfc_expr
*size
)
8001 gfc_expr
*mold_element
;
8006 unsigned char *buffer
;
8007 size_t result_length
;
8009 if (!gfc_is_constant_expr (source
) || !gfc_is_constant_expr (size
))
8012 if (!gfc_resolve_expr (mold
))
8014 if (gfc_init_expr_flag
&& !gfc_is_constant_expr (mold
))
8017 if (!gfc_calculate_transfer_sizes (source
, mold
, size
, &source_size
,
8018 &result_size
, &result_length
))
8021 /* Calculate the size of the source. */
8022 if (source
->expr_type
== EXPR_ARRAY
&& !gfc_array_size (source
, &tmp
))
8023 gfc_internal_error ("Failure getting length of a constant array.");
8025 /* Create an empty new expression with the appropriate characteristics. */
8026 result
= gfc_get_constant_expr (mold
->ts
.type
, mold
->ts
.kind
,
8028 result
->ts
= mold
->ts
;
8030 mold_element
= (mold
->expr_type
== EXPR_ARRAY
&& mold
->value
.constructor
)
8031 ? gfc_constructor_first (mold
->value
.constructor
)->expr
8034 /* Set result character length, if needed. Note that this needs to be
8035 set even for array expressions, in order to pass this information into
8036 gfc_target_interpret_expr. */
8037 if (result
->ts
.type
== BT_CHARACTER
&& gfc_is_constant_expr (mold_element
))
8038 result
->value
.character
.length
= mold_element
->value
.character
.length
;
8040 /* Set the number of elements in the result, and determine its size. */
8042 if (mold
->expr_type
== EXPR_ARRAY
|| mold
->rank
|| size
)
8044 result
->expr_type
= EXPR_ARRAY
;
8046 result
->shape
= gfc_get_shape (1);
8047 mpz_init_set_ui (result
->shape
[0], result_length
);
8052 /* Allocate the buffer to store the binary version of the source. */
8053 buffer_size
= MAX (source_size
, result_size
);
8054 buffer
= (unsigned char*)alloca (buffer_size
);
8055 memset (buffer
, 0, buffer_size
);
8057 /* Now write source to the buffer. */
8058 gfc_target_encode_expr (source
, buffer
, buffer_size
);
8060 /* And read the buffer back into the new expression. */
8061 gfc_target_interpret_expr (buffer
, buffer_size
, result
, false);
8068 gfc_simplify_transpose (gfc_expr
*matrix
)
8070 int row
, matrix_rows
, col
, matrix_cols
;
8073 if (!is_constant_array_expr (matrix
))
8076 gcc_assert (matrix
->rank
== 2);
8078 result
= gfc_get_array_expr (matrix
->ts
.type
, matrix
->ts
.kind
,
8081 result
->shape
= gfc_get_shape (result
->rank
);
8082 mpz_set (result
->shape
[0], matrix
->shape
[1]);
8083 mpz_set (result
->shape
[1], matrix
->shape
[0]);
8085 if (matrix
->ts
.type
== BT_CHARACTER
)
8086 result
->ts
.u
.cl
= matrix
->ts
.u
.cl
;
8087 else if (matrix
->ts
.type
== BT_DERIVED
)
8088 result
->ts
.u
.derived
= matrix
->ts
.u
.derived
;
8090 matrix_rows
= mpz_get_si (matrix
->shape
[0]);
8091 matrix_cols
= mpz_get_si (matrix
->shape
[1]);
8092 for (row
= 0; row
< matrix_rows
; ++row
)
8093 for (col
= 0; col
< matrix_cols
; ++col
)
8095 gfc_expr
*e
= gfc_constructor_lookup_expr (matrix
->value
.constructor
,
8096 col
* matrix_rows
+ row
);
8097 gfc_constructor_insert_expr (&result
->value
.constructor
,
8098 gfc_copy_expr (e
), &matrix
->where
,
8099 row
* matrix_cols
+ col
);
8107 gfc_simplify_trim (gfc_expr
*e
)
8110 int count
, i
, len
, lentrim
;
8112 if (e
->expr_type
!= EXPR_CONSTANT
)
8115 len
= e
->value
.character
.length
;
8116 for (count
= 0, i
= 1; i
<= len
; ++i
)
8118 if (e
->value
.character
.string
[len
- i
] == ' ')
8124 lentrim
= len
- count
;
8126 result
= gfc_get_character_expr (e
->ts
.kind
, &e
->where
, NULL
, lentrim
);
8127 for (i
= 0; i
< lentrim
; i
++)
8128 result
->value
.character
.string
[i
] = e
->value
.character
.string
[i
];
8135 gfc_simplify_image_index (gfc_expr
*coarray
, gfc_expr
*sub
)
8140 gfc_constructor
*sub_cons
;
8144 if (!is_constant_array_expr (sub
))
8147 /* Follow any component references. */
8148 as
= coarray
->symtree
->n
.sym
->as
;
8149 for (ref
= coarray
->ref
; ref
; ref
= ref
->next
)
8150 if (ref
->type
== REF_COMPONENT
)
8153 if (as
->type
== AS_DEFERRED
)
8156 /* "valid sequence of cosubscripts" are required; thus, return 0 unless
8157 the cosubscript addresses the first image. */
8159 sub_cons
= gfc_constructor_first (sub
->value
.constructor
);
8162 for (d
= 1; d
<= as
->corank
; d
++)
8167 gcc_assert (sub_cons
!= NULL
);
8169 ca_bound
= simplify_bound_dim (coarray
, NULL
, d
+ as
->rank
, 0, as
,
8171 if (ca_bound
== NULL
)
8174 if (ca_bound
== &gfc_bad_expr
)
8177 cmp
= mpz_cmp (ca_bound
->value
.integer
, sub_cons
->expr
->value
.integer
);
8181 gfc_free_expr (ca_bound
);
8182 sub_cons
= gfc_constructor_next (sub_cons
);
8186 first_image
= false;
8190 gfc_error ("Out of bounds in IMAGE_INDEX at %L for dimension %d, "
8191 "SUB has %ld and COARRAY lower bound is %ld)",
8193 mpz_get_si (sub_cons
->expr
->value
.integer
),
8194 mpz_get_si (ca_bound
->value
.integer
));
8195 gfc_free_expr (ca_bound
);
8196 return &gfc_bad_expr
;
8199 gfc_free_expr (ca_bound
);
8201 /* Check whether upperbound is valid for the multi-images case. */
8204 ca_bound
= simplify_bound_dim (coarray
, NULL
, d
+ as
->rank
, 1, as
,
8206 if (ca_bound
== &gfc_bad_expr
)
8209 if (ca_bound
&& ca_bound
->expr_type
== EXPR_CONSTANT
8210 && mpz_cmp (ca_bound
->value
.integer
,
8211 sub_cons
->expr
->value
.integer
) < 0)
8213 gfc_error ("Out of bounds in IMAGE_INDEX at %L for dimension %d, "
8214 "SUB has %ld and COARRAY upper bound is %ld)",
8216 mpz_get_si (sub_cons
->expr
->value
.integer
),
8217 mpz_get_si (ca_bound
->value
.integer
));
8218 gfc_free_expr (ca_bound
);
8219 return &gfc_bad_expr
;
8223 gfc_free_expr (ca_bound
);
8226 sub_cons
= gfc_constructor_next (sub_cons
);
8229 gcc_assert (sub_cons
== NULL
);
8231 if (flag_coarray
!= GFC_FCOARRAY_SINGLE
&& !first_image
)
8234 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
8235 &gfc_current_locus
);
8237 mpz_set_si (result
->value
.integer
, 1);
8239 mpz_set_si (result
->value
.integer
, 0);
8245 gfc_simplify_image_status (gfc_expr
*image
, gfc_expr
*team ATTRIBUTE_UNUSED
)
8247 if (flag_coarray
== GFC_FCOARRAY_NONE
)
8249 gfc_current_locus
= *gfc_current_intrinsic_where
;
8250 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
8251 return &gfc_bad_expr
;
8254 /* Simplification is possible for fcoarray = single only. For all other modes
8255 the result depends on runtime conditions. */
8256 if (flag_coarray
!= GFC_FCOARRAY_SINGLE
)
8259 if (gfc_is_constant_expr (image
))
8262 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
8264 if (mpz_get_si (image
->value
.integer
) == 1)
8265 mpz_set_si (result
->value
.integer
, 0);
8267 mpz_set_si (result
->value
.integer
, GFC_STAT_STOPPED_IMAGE
);
8276 gfc_simplify_this_image (gfc_expr
*coarray
, gfc_expr
*dim
,
8277 gfc_expr
*distance ATTRIBUTE_UNUSED
)
8279 if (flag_coarray
!= GFC_FCOARRAY_SINGLE
)
8282 /* If no coarray argument has been passed or when the first argument
8283 is actually a distance argment. */
8284 if (coarray
== NULL
|| !gfc_is_coarray (coarray
))
8287 /* FIXME: gfc_current_locus is wrong. */
8288 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
8289 &gfc_current_locus
);
8290 mpz_set_si (result
->value
.integer
, 1);
8294 /* For -fcoarray=single, this_image(A) is the same as lcobound(A). */
8295 return simplify_cobound (coarray
, dim
, NULL
, 0);
8300 gfc_simplify_ubound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
)
8302 return simplify_bound (array
, dim
, kind
, 1);
8306 gfc_simplify_ucobound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
)
8308 return simplify_cobound (array
, dim
, kind
, 1);
8313 gfc_simplify_unpack (gfc_expr
*vector
, gfc_expr
*mask
, gfc_expr
*field
)
8315 gfc_expr
*result
, *e
;
8316 gfc_constructor
*vector_ctor
, *mask_ctor
, *field_ctor
;
8318 if (!is_constant_array_expr (vector
)
8319 || !is_constant_array_expr (mask
)
8320 || (!gfc_is_constant_expr (field
)
8321 && !is_constant_array_expr (field
)))
8324 result
= gfc_get_array_expr (vector
->ts
.type
, vector
->ts
.kind
,
8326 if (vector
->ts
.type
== BT_DERIVED
)
8327 result
->ts
.u
.derived
= vector
->ts
.u
.derived
;
8328 result
->rank
= mask
->rank
;
8329 result
->shape
= gfc_copy_shape (mask
->shape
, mask
->rank
);
8331 if (vector
->ts
.type
== BT_CHARACTER
)
8332 result
->ts
.u
.cl
= vector
->ts
.u
.cl
;
8334 vector_ctor
= gfc_constructor_first (vector
->value
.constructor
);
8335 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
8337 = field
->expr_type
== EXPR_ARRAY
8338 ? gfc_constructor_first (field
->value
.constructor
)
8343 if (mask_ctor
->expr
->value
.logical
)
8345 gcc_assert (vector_ctor
);
8346 e
= gfc_copy_expr (vector_ctor
->expr
);
8347 vector_ctor
= gfc_constructor_next (vector_ctor
);
8349 else if (field
->expr_type
== EXPR_ARRAY
)
8350 e
= gfc_copy_expr (field_ctor
->expr
);
8352 e
= gfc_copy_expr (field
);
8354 gfc_constructor_append_expr (&result
->value
.constructor
, e
, NULL
);
8356 mask_ctor
= gfc_constructor_next (mask_ctor
);
8357 field_ctor
= gfc_constructor_next (field_ctor
);
8365 gfc_simplify_verify (gfc_expr
*s
, gfc_expr
*set
, gfc_expr
*b
, gfc_expr
*kind
)
8369 size_t index
, len
, lenset
;
8371 int k
= get_kind (BT_INTEGER
, kind
, "VERIFY", gfc_default_integer_kind
);
8374 return &gfc_bad_expr
;
8376 if (s
->expr_type
!= EXPR_CONSTANT
|| set
->expr_type
!= EXPR_CONSTANT
8377 || ( b
!= NULL
&& b
->expr_type
!= EXPR_CONSTANT
))
8380 if (b
!= NULL
&& b
->value
.logical
!= 0)
8385 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &s
->where
);
8387 len
= s
->value
.character
.length
;
8388 lenset
= set
->value
.character
.length
;
8392 mpz_set_ui (result
->value
.integer
, 0);
8400 mpz_set_ui (result
->value
.integer
, 1);
8404 index
= wide_strspn (s
->value
.character
.string
,
8405 set
->value
.character
.string
) + 1;
8414 mpz_set_ui (result
->value
.integer
, len
);
8417 for (index
= len
; index
> 0; index
--)
8419 for (i
= 0; i
< lenset
; i
++)
8421 if (s
->value
.character
.string
[index
- 1]
8422 == set
->value
.character
.string
[i
])
8430 mpz_set_ui (result
->value
.integer
, index
);
8436 gfc_simplify_xor (gfc_expr
*x
, gfc_expr
*y
)
8441 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
8444 kind
= x
->ts
.kind
> y
->ts
.kind
? x
->ts
.kind
: y
->ts
.kind
;
8449 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &x
->where
);
8450 mpz_xor (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
8451 return range_check (result
, "XOR");
8454 return gfc_get_logical_expr (kind
, &x
->where
,
8455 (x
->value
.logical
&& !y
->value
.logical
)
8456 || (!x
->value
.logical
&& y
->value
.logical
));
8464 /****************** Constant simplification *****************/
8466 /* Master function to convert one constant to another. While this is
8467 used as a simplification function, it requires the destination type
8468 and kind information which is supplied by a special case in
8472 gfc_convert_constant (gfc_expr
*e
, bt type
, int kind
)
8474 gfc_expr
*result
, *(*f
) (gfc_expr
*, int);
8475 gfc_constructor
*c
, *t
;
8489 f
= gfc_int2complex
;
8509 f
= gfc_real2complex
;
8520 f
= gfc_complex2int
;
8523 f
= gfc_complex2real
;
8526 f
= gfc_complex2complex
;
8552 f
= gfc_hollerith2int
;
8556 f
= gfc_hollerith2real
;
8560 f
= gfc_hollerith2complex
;
8564 f
= gfc_hollerith2character
;
8568 f
= gfc_hollerith2logical
;
8580 f
= gfc_character2int
;
8584 f
= gfc_character2real
;
8588 f
= gfc_character2complex
;
8592 f
= gfc_character2character
;
8596 f
= gfc_character2logical
;
8606 return &gfc_bad_expr
;
8611 switch (e
->expr_type
)
8614 result
= f (e
, kind
);
8616 return &gfc_bad_expr
;
8620 if (!gfc_is_constant_expr (e
))
8623 result
= gfc_get_array_expr (type
, kind
, &e
->where
);
8624 result
->shape
= gfc_copy_shape (e
->shape
, e
->rank
);
8625 result
->rank
= e
->rank
;
8627 for (c
= gfc_constructor_first (e
->value
.constructor
);
8628 c
; c
= gfc_constructor_next (c
))
8631 if (c
->iterator
== NULL
)
8633 if (c
->expr
->expr_type
== EXPR_ARRAY
)
8634 tmp
= gfc_convert_constant (c
->expr
, type
, kind
);
8635 else if (c
->expr
->expr_type
== EXPR_OP
)
8637 if (!gfc_simplify_expr (c
->expr
, 1))
8638 return &gfc_bad_expr
;
8639 tmp
= f (c
->expr
, kind
);
8642 tmp
= f (c
->expr
, kind
);
8645 tmp
= gfc_convert_constant (c
->expr
, type
, kind
);
8647 if (tmp
== NULL
|| tmp
== &gfc_bad_expr
)
8649 gfc_free_expr (result
);
8653 t
= gfc_constructor_append_expr (&result
->value
.constructor
,
8656 t
->iterator
= gfc_copy_iterator (c
->iterator
);
8669 /* Function for converting character constants. */
8671 gfc_convert_char_constant (gfc_expr
*e
, bt type ATTRIBUTE_UNUSED
, int kind
)
8676 if (!gfc_is_constant_expr (e
))
8679 if (e
->expr_type
== EXPR_CONSTANT
)
8681 /* Simple case of a scalar. */
8682 result
= gfc_get_constant_expr (BT_CHARACTER
, kind
, &e
->where
);
8684 return &gfc_bad_expr
;
8686 result
->value
.character
.length
= e
->value
.character
.length
;
8687 result
->value
.character
.string
8688 = gfc_get_wide_string (e
->value
.character
.length
+ 1);
8689 memcpy (result
->value
.character
.string
, e
->value
.character
.string
,
8690 (e
->value
.character
.length
+ 1) * sizeof (gfc_char_t
));
8692 /* Check we only have values representable in the destination kind. */
8693 for (i
= 0; i
< result
->value
.character
.length
; i
++)
8694 if (!gfc_check_character_range (result
->value
.character
.string
[i
],
8697 gfc_error ("Character %qs in string at %L cannot be converted "
8698 "into character kind %d",
8699 gfc_print_wide_char (result
->value
.character
.string
[i
]),
8701 gfc_free_expr (result
);
8702 return &gfc_bad_expr
;
8707 else if (e
->expr_type
== EXPR_ARRAY
)
8709 /* For an array constructor, we convert each constructor element. */
8712 result
= gfc_get_array_expr (type
, kind
, &e
->where
);
8713 result
->shape
= gfc_copy_shape (e
->shape
, e
->rank
);
8714 result
->rank
= e
->rank
;
8715 result
->ts
.u
.cl
= e
->ts
.u
.cl
;
8717 for (c
= gfc_constructor_first (e
->value
.constructor
);
8718 c
; c
= gfc_constructor_next (c
))
8720 gfc_expr
*tmp
= gfc_convert_char_constant (c
->expr
, type
, kind
);
8721 if (tmp
== &gfc_bad_expr
)
8723 gfc_free_expr (result
);
8724 return &gfc_bad_expr
;
8729 gfc_free_expr (result
);
8733 gfc_constructor_append_expr (&result
->value
.constructor
,
8745 gfc_simplify_compiler_options (void)
8750 str
= gfc_get_option_string ();
8751 result
= gfc_get_character_expr (gfc_default_character_kind
,
8752 &gfc_current_locus
, str
, strlen (str
));
8759 gfc_simplify_compiler_version (void)
8764 len
= strlen ("GCC version ") + strlen (version_string
);
8765 buffer
= XALLOCAVEC (char, len
+ 1);
8766 snprintf (buffer
, len
+ 1, "GCC version %s", version_string
);
8767 return gfc_get_character_expr (gfc_default_character_kind
,
8768 &gfc_current_locus
, buffer
, len
);
8771 /* Simplification routines for intrinsics of IEEE modules. */
8774 simplify_ieee_selected_real_kind (gfc_expr
*expr
)
8776 gfc_actual_arglist
*arg
;
8777 gfc_expr
*p
= NULL
, *q
= NULL
, *rdx
= NULL
;
8779 arg
= expr
->value
.function
.actual
;
8783 q
= arg
->next
->expr
;
8784 if (arg
->next
->next
)
8785 rdx
= arg
->next
->next
->expr
;
8788 /* Currently, if IEEE is supported and this module is built, it means
8789 all our floating-point types conform to IEEE. Hence, we simply handle
8790 IEEE_SELECTED_REAL_KIND like SELECTED_REAL_KIND. */
8791 return gfc_simplify_selected_real_kind (p
, q
, rdx
);
8795 simplify_ieee_support (gfc_expr
*expr
)
8797 /* We consider that if the IEEE modules are loaded, we have full support
8798 for flags, halting and rounding, which are the three functions
8799 (IEEE_SUPPORT_{FLAG,HALTING,ROUNDING}) allowed in constant
8800 expressions. One day, we will need libgfortran to detect support and
8801 communicate it back to us, allowing for partial support. */
8803 return gfc_get_logical_expr (gfc_default_logical_kind
, &expr
->where
,
8808 matches_ieee_function_name (gfc_symbol
*sym
, const char *name
)
8810 int n
= strlen(name
);
8812 if (!strncmp(sym
->name
, name
, n
))
8815 /* If a generic was used and renamed, we need more work to find out.
8816 Compare the specific name. */
8817 if (sym
->generic
&& !strncmp(sym
->generic
->sym
->name
, name
, n
))
8824 gfc_simplify_ieee_functions (gfc_expr
*expr
)
8826 gfc_symbol
* sym
= expr
->symtree
->n
.sym
;
8828 if (matches_ieee_function_name(sym
, "ieee_selected_real_kind"))
8829 return simplify_ieee_selected_real_kind (expr
);
8830 else if (matches_ieee_function_name(sym
, "ieee_support_flag")
8831 || matches_ieee_function_name(sym
, "ieee_support_halting")
8832 || matches_ieee_function_name(sym
, "ieee_support_rounding"))
8833 return simplify_ieee_support (expr
);