1 /* Simplify intrinsic functions at compile-time.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught & Katherine Holcomb
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
27 #include "intrinsic.h"
28 #include "target-memory.h"
30 gfc_expr gfc_bad_expr
;
33 /* Note that 'simplification' is not just transforming expressions.
34 For functions that are not simplified at compile time, range
35 checking is done if possible.
37 The return convention is that each simplification function returns:
39 A new expression node corresponding to the simplified arguments.
40 The original arguments are destroyed by the caller, and must not
41 be a part of the new expression.
43 NULL pointer indicating that no simplification was possible and
44 the original expression should remain intact. If the
45 simplification function sets the type and/or the function name
46 via the pointer gfc_simple_expression, then this type is
49 An expression pointer to gfc_bad_expr (a static placeholder)
50 indicating that some error has prevented simplification. For
51 example, sqrt(-1.0). The error is generated within the function
52 and should be propagated upwards
54 By the time a simplification function gets control, it has been
55 decided that the function call is really supposed to be the
56 intrinsic. No type checking is strictly necessary, since only
57 valid types will be passed on. On the other hand, a simplification
58 subroutine may have to look at the type of an argument as part of
61 Array arguments are never passed to these subroutines.
63 The functions in this file don't have much comment with them, but
64 everything is reasonably straight-forward. The Standard, chapter 13
65 is the best comment you'll find for this file anyway. */
67 /* Range checks an expression node. If all goes well, returns the
68 node, otherwise returns &gfc_bad_expr and frees the node. */
71 range_check (gfc_expr
*result
, const char *name
)
76 switch (gfc_range_check (result
))
82 gfc_error ("Result of %s overflows its kind at %L", name
,
87 gfc_error ("Result of %s underflows its kind at %L", name
,
92 gfc_error ("Result of %s is NaN at %L", name
, &result
->where
);
96 gfc_error ("Result of %s gives range error for its kind at %L", name
,
101 gfc_free_expr (result
);
102 return &gfc_bad_expr
;
106 /* A helper function that gets an optional and possibly missing
107 kind parameter. Returns the kind, -1 if something went wrong. */
110 get_kind (bt type
, gfc_expr
*k
, const char *name
, int default_kind
)
117 if (k
->expr_type
!= EXPR_CONSTANT
)
119 gfc_error ("KIND parameter of %s at %L must be an initialization "
120 "expression", name
, &k
->where
);
124 if (gfc_extract_int (k
, &kind
) != NULL
125 || gfc_validate_kind (type
, kind
, true) < 0)
127 gfc_error ("Invalid KIND parameter of %s at %L", name
, &k
->where
);
135 /* Helper function to get an integer constant with a kind number given
136 by an integer constant expression. */
138 int_expr_with_kind (int i
, gfc_expr
*kind
, const char *name
)
140 gfc_expr
*res
= gfc_int_expr (i
);
141 res
->ts
.kind
= get_kind (BT_INTEGER
, kind
, name
, gfc_default_integer_kind
);
142 if (res
->ts
.kind
== -1)
149 /* Converts an mpz_t signed variable into an unsigned one, assuming
150 two's complement representations and a binary width of bitsize.
151 The conversion is a no-op unless x is negative; otherwise, it can
152 be accomplished by masking out the high bits. */
155 convert_mpz_to_unsigned (mpz_t x
, int bitsize
)
161 /* Confirm that no bits above the signed range are unset. */
162 gcc_assert (mpz_scan0 (x
, bitsize
-1) == ULONG_MAX
);
164 mpz_init_set_ui (mask
, 1);
165 mpz_mul_2exp (mask
, mask
, bitsize
);
166 mpz_sub_ui (mask
, mask
, 1);
168 mpz_and (x
, x
, mask
);
174 /* Confirm that no bits above the signed range are set. */
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 convert_mpz_to_signed (mpz_t x
, int bitsize
)
190 /* Confirm that no bits above the unsigned range are set. */
191 gcc_assert (mpz_scan1 (x
, bitsize
) == ULONG_MAX
);
193 if (mpz_tstbit (x
, bitsize
- 1) == 1)
195 mpz_init_set_ui (mask
, 1);
196 mpz_mul_2exp (mask
, mask
, bitsize
);
197 mpz_sub_ui (mask
, mask
, 1);
199 /* We negate the number by hand, zeroing the high bits, that is
200 make it the corresponding positive number, and then have it
201 negated by GMP, giving the correct representation of the
204 mpz_add_ui (x
, x
, 1);
205 mpz_and (x
, x
, mask
);
214 /********************** Simplification functions *****************************/
217 gfc_simplify_abs (gfc_expr
*e
)
221 if (e
->expr_type
!= EXPR_CONSTANT
)
227 result
= gfc_constant_result (BT_INTEGER
, e
->ts
.kind
, &e
->where
);
229 mpz_abs (result
->value
.integer
, e
->value
.integer
);
231 result
= range_check (result
, "IABS");
235 result
= gfc_constant_result (BT_REAL
, e
->ts
.kind
, &e
->where
);
237 mpfr_abs (result
->value
.real
, e
->value
.real
, GFC_RND_MODE
);
239 result
= range_check (result
, "ABS");
243 result
= gfc_constant_result (BT_REAL
, e
->ts
.kind
, &e
->where
);
245 gfc_set_model_kind (e
->ts
.kind
);
247 mpfr_hypot (result
->value
.real
, e
->value
.complex.r
,
248 e
->value
.complex.i
, GFC_RND_MODE
);
249 result
= range_check (result
, "CABS");
253 gfc_internal_error ("gfc_simplify_abs(): Bad type");
261 simplify_achar_char (gfc_expr
*e
, gfc_expr
*k
, const char *name
, bool ascii
)
265 bool too_large
= false;
267 if (e
->expr_type
!= EXPR_CONSTANT
)
270 kind
= get_kind (BT_CHARACTER
, k
, name
, gfc_default_character_kind
);
272 return &gfc_bad_expr
;
274 if (mpz_cmp_si (e
->value
.integer
, 0) < 0)
276 gfc_error ("Argument of %s function at %L is negative", name
,
278 return &gfc_bad_expr
;
281 if (ascii
&& gfc_option
.warn_surprising
282 && mpz_cmp_si (e
->value
.integer
, 127) > 0)
283 gfc_warning ("Argument of %s function at %L outside of range [0,127]",
286 if (kind
== 1 && mpz_cmp_si (e
->value
.integer
, 255) > 0)
291 mpz_init_set_ui (t
, 2);
292 mpz_pow_ui (t
, t
, 32);
293 mpz_sub_ui (t
, t
, 1);
294 if (mpz_cmp (e
->value
.integer
, t
) > 0)
301 gfc_error ("Argument of %s function at %L is too large for the "
302 "collating sequence of kind %d", name
, &e
->where
, kind
);
303 return &gfc_bad_expr
;
306 result
= gfc_constant_result (BT_CHARACTER
, kind
, &e
->where
);
307 result
->value
.character
.string
= gfc_get_wide_string (2);
308 result
->value
.character
.length
= 1;
309 result
->value
.character
.string
[0] = mpz_get_ui (e
->value
.integer
);
310 result
->value
.character
.string
[1] = '\0'; /* For debugger */
316 /* We use the processor's collating sequence, because all
317 systems that gfortran currently works on are ASCII. */
320 gfc_simplify_achar (gfc_expr
*e
, gfc_expr
*k
)
322 return simplify_achar_char (e
, k
, "ACHAR", true);
327 gfc_simplify_acos (gfc_expr
*x
)
331 if (x
->expr_type
!= EXPR_CONSTANT
)
334 if (mpfr_cmp_si (x
->value
.real
, 1) > 0
335 || mpfr_cmp_si (x
->value
.real
, -1) < 0)
337 gfc_error ("Argument of ACOS at %L must be between -1 and 1",
339 return &gfc_bad_expr
;
342 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
344 mpfr_acos (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
346 return range_check (result
, "ACOS");
350 gfc_simplify_acosh (gfc_expr
*x
)
354 if (x
->expr_type
!= EXPR_CONSTANT
)
357 if (mpfr_cmp_si (x
->value
.real
, 1) < 0)
359 gfc_error ("Argument of ACOSH at %L must not be less than 1",
361 return &gfc_bad_expr
;
364 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
366 mpfr_acosh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
368 return range_check (result
, "ACOSH");
372 gfc_simplify_adjustl (gfc_expr
*e
)
378 if (e
->expr_type
!= EXPR_CONSTANT
)
381 len
= e
->value
.character
.length
;
383 result
= gfc_constant_result (BT_CHARACTER
, e
->ts
.kind
, &e
->where
);
385 result
->value
.character
.length
= len
;
386 result
->value
.character
.string
= gfc_get_wide_string (len
+ 1);
388 for (count
= 0, i
= 0; i
< len
; ++i
)
390 ch
= e
->value
.character
.string
[i
];
396 for (i
= 0; i
< len
- count
; ++i
)
397 result
->value
.character
.string
[i
] = e
->value
.character
.string
[count
+ i
];
399 for (i
= len
- count
; i
< len
; ++i
)
400 result
->value
.character
.string
[i
] = ' ';
402 result
->value
.character
.string
[len
] = '\0'; /* For debugger */
409 gfc_simplify_adjustr (gfc_expr
*e
)
415 if (e
->expr_type
!= EXPR_CONSTANT
)
418 len
= e
->value
.character
.length
;
420 result
= gfc_constant_result (BT_CHARACTER
, e
->ts
.kind
, &e
->where
);
422 result
->value
.character
.length
= len
;
423 result
->value
.character
.string
= gfc_get_wide_string (len
+ 1);
425 for (count
= 0, i
= len
- 1; i
>= 0; --i
)
427 ch
= e
->value
.character
.string
[i
];
433 for (i
= 0; i
< count
; ++i
)
434 result
->value
.character
.string
[i
] = ' ';
436 for (i
= count
; i
< len
; ++i
)
437 result
->value
.character
.string
[i
] = e
->value
.character
.string
[i
- count
];
439 result
->value
.character
.string
[len
] = '\0'; /* For debugger */
446 gfc_simplify_aimag (gfc_expr
*e
)
450 if (e
->expr_type
!= EXPR_CONSTANT
)
453 result
= gfc_constant_result (BT_REAL
, e
->ts
.kind
, &e
->where
);
454 mpfr_set (result
->value
.real
, e
->value
.complex.i
, GFC_RND_MODE
);
456 return range_check (result
, "AIMAG");
461 gfc_simplify_aint (gfc_expr
*e
, gfc_expr
*k
)
463 gfc_expr
*rtrunc
, *result
;
466 kind
= get_kind (BT_REAL
, k
, "AINT", e
->ts
.kind
);
468 return &gfc_bad_expr
;
470 if (e
->expr_type
!= EXPR_CONSTANT
)
473 rtrunc
= gfc_copy_expr (e
);
475 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
477 result
= gfc_real2real (rtrunc
, kind
);
478 gfc_free_expr (rtrunc
);
480 return range_check (result
, "AINT");
485 gfc_simplify_dint (gfc_expr
*e
)
487 gfc_expr
*rtrunc
, *result
;
489 if (e
->expr_type
!= EXPR_CONSTANT
)
492 rtrunc
= gfc_copy_expr (e
);
494 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
496 result
= gfc_real2real (rtrunc
, gfc_default_double_kind
);
497 gfc_free_expr (rtrunc
);
499 return range_check (result
, "DINT");
504 gfc_simplify_anint (gfc_expr
*e
, gfc_expr
*k
)
509 kind
= get_kind (BT_REAL
, k
, "ANINT", e
->ts
.kind
);
511 return &gfc_bad_expr
;
513 if (e
->expr_type
!= EXPR_CONSTANT
)
516 result
= gfc_constant_result (e
->ts
.type
, kind
, &e
->where
);
518 mpfr_round (result
->value
.real
, e
->value
.real
);
520 return range_check (result
, "ANINT");
525 gfc_simplify_and (gfc_expr
*x
, gfc_expr
*y
)
530 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
533 kind
= x
->ts
.kind
> y
->ts
.kind
? x
->ts
.kind
: y
->ts
.kind
;
534 if (x
->ts
.type
== BT_INTEGER
)
536 result
= gfc_constant_result (BT_INTEGER
, kind
, &x
->where
);
537 mpz_and (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
538 return range_check (result
, "AND");
540 else /* BT_LOGICAL */
542 result
= gfc_constant_result (BT_LOGICAL
, kind
, &x
->where
);
543 result
->value
.logical
= x
->value
.logical
&& y
->value
.logical
;
550 gfc_simplify_dnint (gfc_expr
*e
)
554 if (e
->expr_type
!= EXPR_CONSTANT
)
557 result
= gfc_constant_result (BT_REAL
, gfc_default_double_kind
, &e
->where
);
559 mpfr_round (result
->value
.real
, e
->value
.real
);
561 return range_check (result
, "DNINT");
566 gfc_simplify_asin (gfc_expr
*x
)
570 if (x
->expr_type
!= EXPR_CONSTANT
)
573 if (mpfr_cmp_si (x
->value
.real
, 1) > 0
574 || mpfr_cmp_si (x
->value
.real
, -1) < 0)
576 gfc_error ("Argument of ASIN at %L must be between -1 and 1",
578 return &gfc_bad_expr
;
581 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
583 mpfr_asin (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
585 return range_check (result
, "ASIN");
590 gfc_simplify_asinh (gfc_expr
*x
)
594 if (x
->expr_type
!= EXPR_CONSTANT
)
597 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
599 mpfr_asinh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
601 return range_check (result
, "ASINH");
606 gfc_simplify_atan (gfc_expr
*x
)
610 if (x
->expr_type
!= EXPR_CONSTANT
)
613 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
615 mpfr_atan (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
617 return range_check (result
, "ATAN");
622 gfc_simplify_atanh (gfc_expr
*x
)
626 if (x
->expr_type
!= EXPR_CONSTANT
)
629 if (mpfr_cmp_si (x
->value
.real
, 1) >= 0
630 || mpfr_cmp_si (x
->value
.real
, -1) <= 0)
632 gfc_error ("Argument of ATANH at %L must be inside the range -1 to 1",
634 return &gfc_bad_expr
;
637 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
639 mpfr_atanh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
641 return range_check (result
, "ATANH");
646 gfc_simplify_atan2 (gfc_expr
*y
, gfc_expr
*x
)
650 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
653 if (mpfr_sgn (y
->value
.real
) == 0 && mpfr_sgn (x
->value
.real
) == 0)
655 gfc_error ("If first argument of ATAN2 %L is zero, then the "
656 "second argument must not be zero", &x
->where
);
657 return &gfc_bad_expr
;
660 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
662 mpfr_atan2 (result
->value
.real
, y
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
664 return range_check (result
, "ATAN2");
669 gfc_simplify_bessel_j0 (gfc_expr
*x ATTRIBUTE_UNUSED
)
671 #if MPFR_VERSION >= MPFR_VERSION_NUM(2,3,0)
674 if (x
->expr_type
!= EXPR_CONSTANT
)
677 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
678 mpfr_j0 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
680 return range_check (result
, "BESSEL_J0");
688 gfc_simplify_bessel_j1 (gfc_expr
*x ATTRIBUTE_UNUSED
)
690 #if MPFR_VERSION >= MPFR_VERSION_NUM(2,3,0)
693 if (x
->expr_type
!= EXPR_CONSTANT
)
696 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
697 mpfr_j1 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
699 return range_check (result
, "BESSEL_J1");
707 gfc_simplify_bessel_jn (gfc_expr
*order ATTRIBUTE_UNUSED
,
708 gfc_expr
*x ATTRIBUTE_UNUSED
)
710 #if MPFR_VERSION >= MPFR_VERSION_NUM(2,3,0)
714 if (x
->expr_type
!= EXPR_CONSTANT
|| order
->expr_type
!= EXPR_CONSTANT
)
717 n
= mpz_get_si (order
->value
.integer
);
718 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
719 mpfr_jn (result
->value
.real
, n
, x
->value
.real
, GFC_RND_MODE
);
721 return range_check (result
, "BESSEL_JN");
729 gfc_simplify_bessel_y0 (gfc_expr
*x ATTRIBUTE_UNUSED
)
731 #if MPFR_VERSION >= MPFR_VERSION_NUM(2,3,0)
734 if (x
->expr_type
!= EXPR_CONSTANT
)
737 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
738 mpfr_y0 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
740 return range_check (result
, "BESSEL_Y0");
748 gfc_simplify_bessel_y1 (gfc_expr
*x ATTRIBUTE_UNUSED
)
750 #if MPFR_VERSION >= MPFR_VERSION_NUM(2,3,0)
753 if (x
->expr_type
!= EXPR_CONSTANT
)
756 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
757 mpfr_y1 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
759 return range_check (result
, "BESSEL_Y1");
767 gfc_simplify_bessel_yn (gfc_expr
*order ATTRIBUTE_UNUSED
,
768 gfc_expr
*x ATTRIBUTE_UNUSED
)
770 #if MPFR_VERSION >= MPFR_VERSION_NUM(2,3,0)
774 if (x
->expr_type
!= EXPR_CONSTANT
|| order
->expr_type
!= EXPR_CONSTANT
)
777 n
= mpz_get_si (order
->value
.integer
);
778 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
779 mpfr_yn (result
->value
.real
, n
, x
->value
.real
, GFC_RND_MODE
);
781 return range_check (result
, "BESSEL_YN");
789 gfc_simplify_bit_size (gfc_expr
*e
)
794 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
795 result
= gfc_constant_result (BT_INTEGER
, e
->ts
.kind
, &e
->where
);
796 mpz_set_ui (result
->value
.integer
, gfc_integer_kinds
[i
].bit_size
);
803 gfc_simplify_btest (gfc_expr
*e
, gfc_expr
*bit
)
807 if (e
->expr_type
!= EXPR_CONSTANT
|| bit
->expr_type
!= EXPR_CONSTANT
)
810 if (gfc_extract_int (bit
, &b
) != NULL
|| b
< 0)
811 return gfc_logical_expr (0, &e
->where
);
813 return gfc_logical_expr (mpz_tstbit (e
->value
.integer
, b
), &e
->where
);
818 gfc_simplify_ceiling (gfc_expr
*e
, gfc_expr
*k
)
820 gfc_expr
*ceil
, *result
;
823 kind
= get_kind (BT_INTEGER
, k
, "CEILING", gfc_default_integer_kind
);
825 return &gfc_bad_expr
;
827 if (e
->expr_type
!= EXPR_CONSTANT
)
830 result
= gfc_constant_result (BT_INTEGER
, kind
, &e
->where
);
832 ceil
= gfc_copy_expr (e
);
834 mpfr_ceil (ceil
->value
.real
, e
->value
.real
);
835 gfc_mpfr_to_mpz (result
->value
.integer
, ceil
->value
.real
);
837 gfc_free_expr (ceil
);
839 return range_check (result
, "CEILING");
844 gfc_simplify_char (gfc_expr
*e
, gfc_expr
*k
)
846 return simplify_achar_char (e
, k
, "CHAR", false);
850 /* Common subroutine for simplifying CMPLX and DCMPLX. */
853 simplify_cmplx (const char *name
, gfc_expr
*x
, gfc_expr
*y
, int kind
)
857 result
= gfc_constant_result (BT_COMPLEX
, kind
, &x
->where
);
859 mpfr_set_ui (result
->value
.complex.i
, 0, GFC_RND_MODE
);
865 mpfr_set_z (result
->value
.complex.r
, x
->value
.integer
, GFC_RND_MODE
);
869 mpfr_set (result
->value
.complex.r
, x
->value
.real
, GFC_RND_MODE
);
873 mpfr_set (result
->value
.complex.r
, x
->value
.complex.r
, GFC_RND_MODE
);
874 mpfr_set (result
->value
.complex.i
, x
->value
.complex.i
, GFC_RND_MODE
);
878 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (x)");
887 mpfr_set_z (result
->value
.complex.i
, y
->value
.integer
,
892 mpfr_set (result
->value
.complex.i
, y
->value
.real
, GFC_RND_MODE
);
896 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (y)");
905 ts
.kind
= result
->ts
.kind
;
907 if (!gfc_convert_boz (x
, &ts
))
908 return &gfc_bad_expr
;
909 mpfr_set (result
->value
.complex.r
, x
->value
.real
, GFC_RND_MODE
);
916 ts
.kind
= result
->ts
.kind
;
918 if (!gfc_convert_boz (y
, &ts
))
919 return &gfc_bad_expr
;
920 mpfr_set (result
->value
.complex.i
, y
->value
.real
, GFC_RND_MODE
);
923 return range_check (result
, name
);
927 /* Function called when we won't simplify an expression like CMPLX (or
928 COMPLEX or DCMPLX) but still want to convert BOZ arguments. */
931 only_convert_cmplx_boz (gfc_expr
*x
, gfc_expr
*y
, int kind
)
938 if (x
->is_boz
&& !gfc_convert_boz (x
, &ts
))
939 return &gfc_bad_expr
;
941 if (y
&& y
->is_boz
&& !gfc_convert_boz (y
, &ts
))
942 return &gfc_bad_expr
;
949 gfc_simplify_cmplx (gfc_expr
*x
, gfc_expr
*y
, gfc_expr
*k
)
953 kind
= get_kind (BT_REAL
, k
, "CMPLX", gfc_default_real_kind
);
955 return &gfc_bad_expr
;
957 if (x
->expr_type
!= EXPR_CONSTANT
958 || (y
!= NULL
&& y
->expr_type
!= EXPR_CONSTANT
))
959 return only_convert_cmplx_boz (x
, y
, kind
);
961 return simplify_cmplx ("CMPLX", x
, y
, kind
);
966 gfc_simplify_complex (gfc_expr
*x
, gfc_expr
*y
)
970 if (x
->ts
.type
== BT_INTEGER
)
972 if (y
->ts
.type
== BT_INTEGER
)
973 kind
= gfc_default_real_kind
;
979 if (y
->ts
.type
== BT_REAL
)
980 kind
= (x
->ts
.kind
> y
->ts
.kind
) ? x
->ts
.kind
: y
->ts
.kind
;
985 if (x
->expr_type
!= EXPR_CONSTANT
986 || (y
!= NULL
&& y
->expr_type
!= EXPR_CONSTANT
))
987 return only_convert_cmplx_boz (x
, y
, kind
);
989 return simplify_cmplx ("COMPLEX", x
, y
, kind
);
994 gfc_simplify_conjg (gfc_expr
*e
)
998 if (e
->expr_type
!= EXPR_CONSTANT
)
1001 result
= gfc_copy_expr (e
);
1002 mpfr_neg (result
->value
.complex.i
, result
->value
.complex.i
, GFC_RND_MODE
);
1004 return range_check (result
, "CONJG");
1009 gfc_simplify_cos (gfc_expr
*x
)
1014 if (x
->expr_type
!= EXPR_CONSTANT
)
1017 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1022 mpfr_cos (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1025 gfc_set_model_kind (x
->ts
.kind
);
1029 mpfr_cos (xp
, x
->value
.complex.r
, GFC_RND_MODE
);
1030 mpfr_cosh (xq
, x
->value
.complex.i
, GFC_RND_MODE
);
1031 mpfr_mul (result
->value
.complex.r
, xp
, xq
, GFC_RND_MODE
);
1033 mpfr_sin (xp
, x
->value
.complex.r
, GFC_RND_MODE
);
1034 mpfr_sinh (xq
, x
->value
.complex.i
, GFC_RND_MODE
);
1035 mpfr_mul (xp
, xp
, xq
, GFC_RND_MODE
);
1036 mpfr_neg (result
->value
.complex.i
, xp
, GFC_RND_MODE
);
1038 mpfr_clears (xp
, xq
, NULL
);
1041 gfc_internal_error ("in gfc_simplify_cos(): Bad type");
1044 return range_check (result
, "COS");
1050 gfc_simplify_cosh (gfc_expr
*x
)
1054 if (x
->expr_type
!= EXPR_CONSTANT
)
1057 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1059 mpfr_cosh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1061 return range_check (result
, "COSH");
1066 gfc_simplify_dcmplx (gfc_expr
*x
, gfc_expr
*y
)
1069 if (x
->expr_type
!= EXPR_CONSTANT
1070 || (y
!= NULL
&& y
->expr_type
!= EXPR_CONSTANT
))
1071 return only_convert_cmplx_boz (x
, y
, gfc_default_double_kind
);
1073 return simplify_cmplx ("DCMPLX", x
, y
, gfc_default_double_kind
);
1078 gfc_simplify_dble (gfc_expr
*e
)
1080 gfc_expr
*result
= NULL
;
1082 if (e
->expr_type
!= EXPR_CONSTANT
)
1089 result
= gfc_int2real (e
, gfc_default_double_kind
);
1093 result
= gfc_real2real (e
, gfc_default_double_kind
);
1097 result
= gfc_complex2real (e
, gfc_default_double_kind
);
1101 gfc_internal_error ("gfc_simplify_dble(): bad type at %L", &e
->where
);
1104 if (e
->ts
.type
== BT_INTEGER
&& e
->is_boz
)
1109 ts
.kind
= gfc_default_double_kind
;
1110 result
= gfc_copy_expr (e
);
1111 if (!gfc_convert_boz (result
, &ts
))
1113 gfc_free_expr (result
);
1114 return &gfc_bad_expr
;
1118 return range_check (result
, "DBLE");
1123 gfc_simplify_digits (gfc_expr
*x
)
1127 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
1131 digits
= gfc_integer_kinds
[i
].digits
;
1136 digits
= gfc_real_kinds
[i
].digits
;
1143 return gfc_int_expr (digits
);
1148 gfc_simplify_dim (gfc_expr
*x
, gfc_expr
*y
)
1153 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1156 kind
= x
->ts
.kind
> y
->ts
.kind
? x
->ts
.kind
: y
->ts
.kind
;
1157 result
= gfc_constant_result (x
->ts
.type
, kind
, &x
->where
);
1162 if (mpz_cmp (x
->value
.integer
, y
->value
.integer
) > 0)
1163 mpz_sub (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
1165 mpz_set_ui (result
->value
.integer
, 0);
1170 if (mpfr_cmp (x
->value
.real
, y
->value
.real
) > 0)
1171 mpfr_sub (result
->value
.real
, x
->value
.real
, y
->value
.real
,
1174 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
1179 gfc_internal_error ("gfc_simplify_dim(): Bad type");
1182 return range_check (result
, "DIM");
1187 gfc_simplify_dprod (gfc_expr
*x
, gfc_expr
*y
)
1189 gfc_expr
*a1
, *a2
, *result
;
1191 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1194 result
= gfc_constant_result (BT_REAL
, gfc_default_double_kind
, &x
->where
);
1196 a1
= gfc_real2real (x
, gfc_default_double_kind
);
1197 a2
= gfc_real2real (y
, gfc_default_double_kind
);
1199 mpfr_mul (result
->value
.real
, a1
->value
.real
, a2
->value
.real
, GFC_RND_MODE
);
1204 return range_check (result
, "DPROD");
1209 gfc_simplify_erf (gfc_expr
*x
)
1213 if (x
->expr_type
!= EXPR_CONSTANT
)
1216 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1218 mpfr_erf (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1220 return range_check (result
, "ERF");
1225 gfc_simplify_erfc (gfc_expr
*x
)
1229 if (x
->expr_type
!= EXPR_CONSTANT
)
1232 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1234 mpfr_erfc (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1236 return range_check (result
, "ERFC");
1241 gfc_simplify_epsilon (gfc_expr
*e
)
1246 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
1248 result
= gfc_constant_result (BT_REAL
, e
->ts
.kind
, &e
->where
);
1250 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].epsilon
, GFC_RND_MODE
);
1252 return range_check (result
, "EPSILON");
1257 gfc_simplify_exp (gfc_expr
*x
)
1262 if (x
->expr_type
!= EXPR_CONSTANT
)
1265 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1270 mpfr_exp (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1274 gfc_set_model_kind (x
->ts
.kind
);
1277 mpfr_exp (xq
, x
->value
.complex.r
, GFC_RND_MODE
);
1278 mpfr_cos (xp
, x
->value
.complex.i
, GFC_RND_MODE
);
1279 mpfr_mul (result
->value
.complex.r
, xq
, xp
, GFC_RND_MODE
);
1280 mpfr_sin (xp
, x
->value
.complex.i
, GFC_RND_MODE
);
1281 mpfr_mul (result
->value
.complex.i
, xq
, xp
, GFC_RND_MODE
);
1282 mpfr_clears (xp
, xq
, NULL
);
1286 gfc_internal_error ("in gfc_simplify_exp(): Bad type");
1289 return range_check (result
, "EXP");
1293 gfc_simplify_exponent (gfc_expr
*x
)
1298 if (x
->expr_type
!= EXPR_CONSTANT
)
1301 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
1304 gfc_set_model (x
->value
.real
);
1306 if (mpfr_sgn (x
->value
.real
) == 0)
1308 mpz_set_ui (result
->value
.integer
, 0);
1312 i
= (int) mpfr_get_exp (x
->value
.real
);
1313 mpz_set_si (result
->value
.integer
, i
);
1315 return range_check (result
, "EXPONENT");
1320 gfc_simplify_float (gfc_expr
*a
)
1324 if (a
->expr_type
!= EXPR_CONSTANT
)
1333 ts
.kind
= gfc_default_real_kind
;
1335 result
= gfc_copy_expr (a
);
1336 if (!gfc_convert_boz (result
, &ts
))
1338 gfc_free_expr (result
);
1339 return &gfc_bad_expr
;
1343 result
= gfc_int2real (a
, gfc_default_real_kind
);
1344 return range_check (result
, "FLOAT");
1349 gfc_simplify_floor (gfc_expr
*e
, gfc_expr
*k
)
1355 kind
= get_kind (BT_INTEGER
, k
, "FLOOR", gfc_default_integer_kind
);
1357 gfc_internal_error ("gfc_simplify_floor(): Bad kind");
1359 if (e
->expr_type
!= EXPR_CONSTANT
)
1362 result
= gfc_constant_result (BT_INTEGER
, kind
, &e
->where
);
1364 gfc_set_model_kind (kind
);
1366 mpfr_floor (floor
, e
->value
.real
);
1368 gfc_mpfr_to_mpz (result
->value
.integer
, floor
);
1372 return range_check (result
, "FLOOR");
1377 gfc_simplify_fraction (gfc_expr
*x
)
1380 mpfr_t absv
, exp
, pow2
;
1382 if (x
->expr_type
!= EXPR_CONSTANT
)
1385 result
= gfc_constant_result (BT_REAL
, x
->ts
.kind
, &x
->where
);
1387 if (mpfr_sgn (x
->value
.real
) == 0)
1389 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
1393 gfc_set_model_kind (x
->ts
.kind
);
1398 mpfr_abs (absv
, x
->value
.real
, GFC_RND_MODE
);
1399 mpfr_log2 (exp
, absv
, GFC_RND_MODE
);
1401 mpfr_trunc (exp
, exp
);
1402 mpfr_add_ui (exp
, exp
, 1, GFC_RND_MODE
);
1404 mpfr_ui_pow (pow2
, 2, exp
, GFC_RND_MODE
);
1406 mpfr_div (result
->value
.real
, absv
, pow2
, GFC_RND_MODE
);
1408 mpfr_clears (exp
, absv
, pow2
, NULL
);
1410 return range_check (result
, "FRACTION");
1415 gfc_simplify_gamma (gfc_expr
*x
)
1419 if (x
->expr_type
!= EXPR_CONSTANT
)
1422 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1424 mpfr_gamma (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1426 return range_check (result
, "GAMMA");
1431 gfc_simplify_huge (gfc_expr
*e
)
1436 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
1438 result
= gfc_constant_result (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
1443 mpz_set (result
->value
.integer
, gfc_integer_kinds
[i
].huge
);
1447 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].huge
, GFC_RND_MODE
);
1459 gfc_simplify_hypot (gfc_expr
*x
, gfc_expr
*y
)
1463 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1466 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1467 mpfr_hypot (result
->value
.real
, x
->value
.real
, y
->value
.real
, GFC_RND_MODE
);
1468 return range_check (result
, "HYPOT");
1472 /* We use the processor's collating sequence, because all
1473 systems that gfortran currently works on are ASCII. */
1476 gfc_simplify_iachar (gfc_expr
*e
, gfc_expr
*kind
)
1481 if (e
->expr_type
!= EXPR_CONSTANT
)
1484 if (e
->value
.character
.length
!= 1)
1486 gfc_error ("Argument of IACHAR at %L must be of length one", &e
->where
);
1487 return &gfc_bad_expr
;
1490 index
= e
->value
.character
.string
[0];
1492 if (gfc_option
.warn_surprising
&& index
> 127)
1493 gfc_warning ("Argument of IACHAR function at %L outside of range 0..127",
1496 if ((result
= int_expr_with_kind (index
, kind
, "IACHAR")) == NULL
)
1497 return &gfc_bad_expr
;
1499 result
->where
= e
->where
;
1501 return range_check (result
, "IACHAR");
1506 gfc_simplify_iand (gfc_expr
*x
, gfc_expr
*y
)
1510 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1513 result
= gfc_constant_result (BT_INTEGER
, x
->ts
.kind
, &x
->where
);
1515 mpz_and (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
1517 return range_check (result
, "IAND");
1522 gfc_simplify_ibclr (gfc_expr
*x
, gfc_expr
*y
)
1527 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1530 if (gfc_extract_int (y
, &pos
) != NULL
|| pos
< 0)
1532 gfc_error ("Invalid second argument of IBCLR at %L", &y
->where
);
1533 return &gfc_bad_expr
;
1536 k
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
1538 if (pos
>= gfc_integer_kinds
[k
].bit_size
)
1540 gfc_error ("Second argument of IBCLR exceeds bit size at %L",
1542 return &gfc_bad_expr
;
1545 result
= gfc_copy_expr (x
);
1547 convert_mpz_to_unsigned (result
->value
.integer
,
1548 gfc_integer_kinds
[k
].bit_size
);
1550 mpz_clrbit (result
->value
.integer
, pos
);
1552 convert_mpz_to_signed (result
->value
.integer
,
1553 gfc_integer_kinds
[k
].bit_size
);
1560 gfc_simplify_ibits (gfc_expr
*x
, gfc_expr
*y
, gfc_expr
*z
)
1567 if (x
->expr_type
!= EXPR_CONSTANT
1568 || y
->expr_type
!= EXPR_CONSTANT
1569 || z
->expr_type
!= EXPR_CONSTANT
)
1572 if (gfc_extract_int (y
, &pos
) != NULL
|| pos
< 0)
1574 gfc_error ("Invalid second argument of IBITS at %L", &y
->where
);
1575 return &gfc_bad_expr
;
1578 if (gfc_extract_int (z
, &len
) != NULL
|| len
< 0)
1580 gfc_error ("Invalid third argument of IBITS at %L", &z
->where
);
1581 return &gfc_bad_expr
;
1584 k
= gfc_validate_kind (BT_INTEGER
, x
->ts
.kind
, false);
1586 bitsize
= gfc_integer_kinds
[k
].bit_size
;
1588 if (pos
+ len
> bitsize
)
1590 gfc_error ("Sum of second and third arguments of IBITS exceeds "
1591 "bit size at %L", &y
->where
);
1592 return &gfc_bad_expr
;
1595 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1596 convert_mpz_to_unsigned (result
->value
.integer
,
1597 gfc_integer_kinds
[k
].bit_size
);
1599 bits
= XCNEWVEC (int, bitsize
);
1601 for (i
= 0; i
< bitsize
; i
++)
1604 for (i
= 0; i
< len
; i
++)
1605 bits
[i
] = mpz_tstbit (x
->value
.integer
, i
+ pos
);
1607 for (i
= 0; i
< bitsize
; i
++)
1610 mpz_clrbit (result
->value
.integer
, i
);
1611 else if (bits
[i
] == 1)
1612 mpz_setbit (result
->value
.integer
, i
);
1614 gfc_internal_error ("IBITS: Bad bit");
1619 convert_mpz_to_signed (result
->value
.integer
,
1620 gfc_integer_kinds
[k
].bit_size
);
1627 gfc_simplify_ibset (gfc_expr
*x
, gfc_expr
*y
)
1632 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1635 if (gfc_extract_int (y
, &pos
) != NULL
|| pos
< 0)
1637 gfc_error ("Invalid second argument of IBSET at %L", &y
->where
);
1638 return &gfc_bad_expr
;
1641 k
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
1643 if (pos
>= gfc_integer_kinds
[k
].bit_size
)
1645 gfc_error ("Second argument of IBSET exceeds bit size at %L",
1647 return &gfc_bad_expr
;
1650 result
= gfc_copy_expr (x
);
1652 convert_mpz_to_unsigned (result
->value
.integer
,
1653 gfc_integer_kinds
[k
].bit_size
);
1655 mpz_setbit (result
->value
.integer
, pos
);
1657 convert_mpz_to_signed (result
->value
.integer
,
1658 gfc_integer_kinds
[k
].bit_size
);
1665 gfc_simplify_ichar (gfc_expr
*e
, gfc_expr
*kind
)
1670 if (e
->expr_type
!= EXPR_CONSTANT
)
1673 if (e
->value
.character
.length
!= 1)
1675 gfc_error ("Argument of ICHAR at %L must be of length one", &e
->where
);
1676 return &gfc_bad_expr
;
1679 index
= e
->value
.character
.string
[0];
1681 if ((result
= int_expr_with_kind (index
, kind
, "ICHAR")) == NULL
)
1682 return &gfc_bad_expr
;
1684 result
->where
= e
->where
;
1685 return range_check (result
, "ICHAR");
1690 gfc_simplify_ieor (gfc_expr
*x
, gfc_expr
*y
)
1694 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1697 result
= gfc_constant_result (BT_INTEGER
, x
->ts
.kind
, &x
->where
);
1699 mpz_xor (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
1701 return range_check (result
, "IEOR");
1706 gfc_simplify_index (gfc_expr
*x
, gfc_expr
*y
, gfc_expr
*b
, gfc_expr
*kind
)
1709 int back
, len
, lensub
;
1710 int i
, j
, k
, count
, index
= 0, start
;
1712 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
1713 || ( b
!= NULL
&& b
->expr_type
!= EXPR_CONSTANT
))
1716 if (b
!= NULL
&& b
->value
.logical
!= 0)
1721 k
= get_kind (BT_INTEGER
, kind
, "INDEX", gfc_default_integer_kind
);
1723 return &gfc_bad_expr
;
1725 result
= gfc_constant_result (BT_INTEGER
, k
, &x
->where
);
1727 len
= x
->value
.character
.length
;
1728 lensub
= y
->value
.character
.length
;
1732 mpz_set_si (result
->value
.integer
, 0);
1740 mpz_set_si (result
->value
.integer
, 1);
1743 else if (lensub
== 1)
1745 for (i
= 0; i
< len
; i
++)
1747 for (j
= 0; j
< lensub
; j
++)
1749 if (y
->value
.character
.string
[j
]
1750 == x
->value
.character
.string
[i
])
1760 for (i
= 0; i
< len
; i
++)
1762 for (j
= 0; j
< lensub
; j
++)
1764 if (y
->value
.character
.string
[j
]
1765 == x
->value
.character
.string
[i
])
1770 for (k
= 0; k
< lensub
; k
++)
1772 if (y
->value
.character
.string
[k
]
1773 == x
->value
.character
.string
[k
+ start
])
1777 if (count
== lensub
)
1792 mpz_set_si (result
->value
.integer
, len
+ 1);
1795 else if (lensub
== 1)
1797 for (i
= 0; i
< len
; i
++)
1799 for (j
= 0; j
< lensub
; j
++)
1801 if (y
->value
.character
.string
[j
]
1802 == x
->value
.character
.string
[len
- i
])
1804 index
= len
- i
+ 1;
1812 for (i
= 0; i
< len
; i
++)
1814 for (j
= 0; j
< lensub
; j
++)
1816 if (y
->value
.character
.string
[j
]
1817 == x
->value
.character
.string
[len
- i
])
1820 if (start
<= len
- lensub
)
1823 for (k
= 0; k
< lensub
; k
++)
1824 if (y
->value
.character
.string
[k
]
1825 == x
->value
.character
.string
[k
+ start
])
1828 if (count
== lensub
)
1845 mpz_set_si (result
->value
.integer
, index
);
1846 return range_check (result
, "INDEX");
1851 gfc_simplify_int (gfc_expr
*e
, gfc_expr
*k
)
1853 gfc_expr
*result
= NULL
;
1856 kind
= get_kind (BT_INTEGER
, k
, "INT", gfc_default_integer_kind
);
1858 return &gfc_bad_expr
;
1860 if (e
->expr_type
!= EXPR_CONSTANT
)
1866 result
= gfc_int2int (e
, kind
);
1870 result
= gfc_real2int (e
, kind
);
1874 result
= gfc_complex2int (e
, kind
);
1878 gfc_error ("Argument of INT at %L is not a valid type", &e
->where
);
1879 return &gfc_bad_expr
;
1882 return range_check (result
, "INT");
1887 simplify_intconv (gfc_expr
*e
, int kind
, const char *name
)
1889 gfc_expr
*result
= NULL
;
1891 if (e
->expr_type
!= EXPR_CONSTANT
)
1897 result
= gfc_int2int (e
, kind
);
1901 result
= gfc_real2int (e
, kind
);
1905 result
= gfc_complex2int (e
, kind
);
1909 gfc_error ("Argument of %s at %L is not a valid type", name
, &e
->where
);
1910 return &gfc_bad_expr
;
1913 return range_check (result
, name
);
1918 gfc_simplify_int2 (gfc_expr
*e
)
1920 return simplify_intconv (e
, 2, "INT2");
1925 gfc_simplify_int8 (gfc_expr
*e
)
1927 return simplify_intconv (e
, 8, "INT8");
1932 gfc_simplify_long (gfc_expr
*e
)
1934 return simplify_intconv (e
, 4, "LONG");
1939 gfc_simplify_ifix (gfc_expr
*e
)
1941 gfc_expr
*rtrunc
, *result
;
1943 if (e
->expr_type
!= EXPR_CONSTANT
)
1946 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
1949 rtrunc
= gfc_copy_expr (e
);
1951 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
1952 gfc_mpfr_to_mpz (result
->value
.integer
, rtrunc
->value
.real
);
1954 gfc_free_expr (rtrunc
);
1955 return range_check (result
, "IFIX");
1960 gfc_simplify_idint (gfc_expr
*e
)
1962 gfc_expr
*rtrunc
, *result
;
1964 if (e
->expr_type
!= EXPR_CONSTANT
)
1967 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
1970 rtrunc
= gfc_copy_expr (e
);
1972 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
1973 gfc_mpfr_to_mpz (result
->value
.integer
, rtrunc
->value
.real
);
1975 gfc_free_expr (rtrunc
);
1976 return range_check (result
, "IDINT");
1981 gfc_simplify_ior (gfc_expr
*x
, gfc_expr
*y
)
1985 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1988 result
= gfc_constant_result (BT_INTEGER
, x
->ts
.kind
, &x
->where
);
1990 mpz_ior (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
1991 return range_check (result
, "IOR");
1996 gfc_simplify_ishft (gfc_expr
*e
, gfc_expr
*s
)
1999 int shift
, ashift
, isize
, k
, *bits
, i
;
2001 if (e
->expr_type
!= EXPR_CONSTANT
|| s
->expr_type
!= EXPR_CONSTANT
)
2004 if (gfc_extract_int (s
, &shift
) != NULL
)
2006 gfc_error ("Invalid second argument of ISHFT at %L", &s
->where
);
2007 return &gfc_bad_expr
;
2010 k
= gfc_validate_kind (BT_INTEGER
, e
->ts
.kind
, false);
2012 isize
= gfc_integer_kinds
[k
].bit_size
;
2021 gfc_error ("Magnitude of second argument of ISHFT exceeds bit size "
2022 "at %L", &s
->where
);
2023 return &gfc_bad_expr
;
2026 result
= gfc_constant_result (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
2030 mpz_set (result
->value
.integer
, e
->value
.integer
);
2031 return range_check (result
, "ISHFT");
2034 bits
= XCNEWVEC (int, isize
);
2036 for (i
= 0; i
< isize
; i
++)
2037 bits
[i
] = mpz_tstbit (e
->value
.integer
, i
);
2041 for (i
= 0; i
< shift
; i
++)
2042 mpz_clrbit (result
->value
.integer
, i
);
2044 for (i
= 0; i
< isize
- shift
; i
++)
2047 mpz_clrbit (result
->value
.integer
, i
+ shift
);
2049 mpz_setbit (result
->value
.integer
, i
+ shift
);
2054 for (i
= isize
- 1; i
>= isize
- ashift
; i
--)
2055 mpz_clrbit (result
->value
.integer
, i
);
2057 for (i
= isize
- 1; i
>= ashift
; i
--)
2060 mpz_clrbit (result
->value
.integer
, i
- ashift
);
2062 mpz_setbit (result
->value
.integer
, i
- ashift
);
2066 convert_mpz_to_signed (result
->value
.integer
, isize
);
2074 gfc_simplify_ishftc (gfc_expr
*e
, gfc_expr
*s
, gfc_expr
*sz
)
2077 int shift
, ashift
, isize
, ssize
, delta
, k
;
2080 if (e
->expr_type
!= EXPR_CONSTANT
|| s
->expr_type
!= EXPR_CONSTANT
)
2083 if (gfc_extract_int (s
, &shift
) != NULL
)
2085 gfc_error ("Invalid second argument of ISHFTC at %L", &s
->where
);
2086 return &gfc_bad_expr
;
2089 k
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
2090 isize
= gfc_integer_kinds
[k
].bit_size
;
2094 if (sz
->expr_type
!= EXPR_CONSTANT
)
2097 if (gfc_extract_int (sz
, &ssize
) != NULL
|| ssize
<= 0)
2099 gfc_error ("Invalid third argument of ISHFTC at %L", &sz
->where
);
2100 return &gfc_bad_expr
;
2105 gfc_error ("Magnitude of third argument of ISHFTC exceeds "
2106 "BIT_SIZE of first argument at %L", &s
->where
);
2107 return &gfc_bad_expr
;
2121 gfc_error ("Magnitude of second argument of ISHFTC exceeds "
2122 "third argument at %L", &s
->where
);
2124 gfc_error ("Magnitude of second argument of ISHFTC exceeds "
2125 "BIT_SIZE of first argument at %L", &s
->where
);
2126 return &gfc_bad_expr
;
2129 result
= gfc_constant_result (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
2131 mpz_set (result
->value
.integer
, e
->value
.integer
);
2136 convert_mpz_to_unsigned (result
->value
.integer
, isize
);
2138 bits
= XCNEWVEC (int, ssize
);
2140 for (i
= 0; i
< ssize
; i
++)
2141 bits
[i
] = mpz_tstbit (e
->value
.integer
, i
);
2143 delta
= ssize
- ashift
;
2147 for (i
= 0; i
< delta
; i
++)
2150 mpz_clrbit (result
->value
.integer
, i
+ shift
);
2152 mpz_setbit (result
->value
.integer
, i
+ shift
);
2155 for (i
= delta
; i
< ssize
; i
++)
2158 mpz_clrbit (result
->value
.integer
, i
- delta
);
2160 mpz_setbit (result
->value
.integer
, i
- delta
);
2165 for (i
= 0; i
< ashift
; i
++)
2168 mpz_clrbit (result
->value
.integer
, i
+ delta
);
2170 mpz_setbit (result
->value
.integer
, i
+ delta
);
2173 for (i
= ashift
; i
< ssize
; i
++)
2176 mpz_clrbit (result
->value
.integer
, i
+ shift
);
2178 mpz_setbit (result
->value
.integer
, i
+ shift
);
2182 convert_mpz_to_signed (result
->value
.integer
, isize
);
2190 gfc_simplify_kind (gfc_expr
*e
)
2193 if (e
->ts
.type
== BT_DERIVED
)
2195 gfc_error ("Argument of KIND at %L is a DERIVED type", &e
->where
);
2196 return &gfc_bad_expr
;
2199 return gfc_int_expr (e
->ts
.kind
);
2204 simplify_bound_dim (gfc_expr
*array
, gfc_expr
*kind
, int d
, int upper
,
2207 gfc_expr
*l
, *u
, *result
;
2210 /* The last dimension of an assumed-size array is special. */
2211 if (d
== as
->rank
&& as
->type
== AS_ASSUMED_SIZE
&& !upper
)
2213 if (as
->lower
[d
-1]->expr_type
== EXPR_CONSTANT
)
2214 return gfc_copy_expr (as
->lower
[d
-1]);
2219 /* Then, we need to know the extent of the given dimension. */
2223 if (l
->expr_type
!= EXPR_CONSTANT
|| u
->expr_type
!= EXPR_CONSTANT
)
2226 k
= get_kind (BT_INTEGER
, kind
, upper
? "UBOUND" : "LBOUND",
2227 gfc_default_integer_kind
);
2229 return &gfc_bad_expr
;
2231 result
= gfc_constant_result (BT_INTEGER
, k
, &array
->where
);
2233 if (mpz_cmp (l
->value
.integer
, u
->value
.integer
) > 0)
2237 mpz_set_si (result
->value
.integer
, 0);
2239 mpz_set_si (result
->value
.integer
, 1);
2243 /* Nonzero extent. */
2245 mpz_set (result
->value
.integer
, u
->value
.integer
);
2247 mpz_set (result
->value
.integer
, l
->value
.integer
);
2250 return range_check (result
, upper
? "UBOUND" : "LBOUND");
2255 simplify_bound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
, int upper
)
2261 if (array
->expr_type
!= EXPR_VARIABLE
)
2264 /* Follow any component references. */
2265 as
= array
->symtree
->n
.sym
->as
;
2266 for (ref
= array
->ref
; ref
; ref
= ref
->next
)
2271 switch (ref
->u
.ar
.type
)
2278 /* We're done because 'as' has already been set in the
2279 previous iteration. */
2290 as
= ref
->u
.c
.component
->as
;
2302 if (as
->type
== AS_DEFERRED
|| as
->type
== AS_ASSUMED_SHAPE
)
2307 /* Multi-dimensional bounds. */
2308 gfc_expr
*bounds
[GFC_MAX_DIMENSIONS
];
2310 gfc_constructor
*head
, *tail
;
2313 /* UBOUND(ARRAY) is not valid for an assumed-size array. */
2314 if (upper
&& as
->type
== AS_ASSUMED_SIZE
)
2316 /* An error message will be emitted in
2317 check_assumed_size_reference (resolve.c). */
2318 return &gfc_bad_expr
;
2321 /* Simplify the bounds for each dimension. */
2322 for (d
= 0; d
< array
->rank
; d
++)
2324 bounds
[d
] = simplify_bound_dim (array
, kind
, d
+ 1, upper
, as
);
2325 if (bounds
[d
] == NULL
|| bounds
[d
] == &gfc_bad_expr
)
2329 for (j
= 0; j
< d
; j
++)
2330 gfc_free_expr (bounds
[j
]);
2335 /* Allocate the result expression. */
2336 e
= gfc_get_expr ();
2337 e
->where
= array
->where
;
2338 e
->expr_type
= EXPR_ARRAY
;
2339 e
->ts
.type
= BT_INTEGER
;
2340 k
= get_kind (BT_INTEGER
, kind
, upper
? "UBOUND" : "LBOUND",
2341 gfc_default_integer_kind
);
2345 return &gfc_bad_expr
;
2349 /* The result is a rank 1 array; its size is the rank of the first
2350 argument to {L,U}BOUND. */
2352 e
->shape
= gfc_get_shape (1);
2353 mpz_init_set_ui (e
->shape
[0], array
->rank
);
2355 /* Create the constructor for this array. */
2357 for (d
= 0; d
< array
->rank
; d
++)
2359 /* Get a new constructor element. */
2361 head
= tail
= gfc_get_constructor ();
2364 tail
->next
= gfc_get_constructor ();
2368 tail
->where
= e
->where
;
2369 tail
->expr
= bounds
[d
];
2371 e
->value
.constructor
= head
;
2377 /* A DIM argument is specified. */
2378 if (dim
->expr_type
!= EXPR_CONSTANT
)
2381 d
= mpz_get_si (dim
->value
.integer
);
2383 if (d
< 1 || d
> as
->rank
2384 || (d
== as
->rank
&& as
->type
== AS_ASSUMED_SIZE
&& upper
))
2386 gfc_error ("DIM argument at %L is out of bounds", &dim
->where
);
2387 return &gfc_bad_expr
;
2390 return simplify_bound_dim (array
, kind
, d
, upper
, as
);
2396 gfc_simplify_lbound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
)
2398 return simplify_bound (array
, dim
, kind
, 0);
2403 gfc_simplify_leadz (gfc_expr
*e
)
2406 unsigned long lz
, bs
;
2409 if (e
->expr_type
!= EXPR_CONSTANT
)
2412 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
2413 bs
= gfc_integer_kinds
[i
].bit_size
;
2414 if (mpz_cmp_si (e
->value
.integer
, 0) == 0)
2417 lz
= bs
- mpz_sizeinbase (e
->value
.integer
, 2);
2419 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
, &e
->where
);
2420 mpz_set_ui (result
->value
.integer
, lz
);
2427 gfc_simplify_len (gfc_expr
*e
, gfc_expr
*kind
)
2430 int k
= get_kind (BT_INTEGER
, kind
, "LEN", gfc_default_integer_kind
);
2433 return &gfc_bad_expr
;
2435 if (e
->expr_type
== EXPR_CONSTANT
)
2437 result
= gfc_constant_result (BT_INTEGER
, k
, &e
->where
);
2438 mpz_set_si (result
->value
.integer
, e
->value
.character
.length
);
2439 return range_check (result
, "LEN");
2442 if (e
->ts
.cl
!= NULL
&& e
->ts
.cl
->length
!= NULL
2443 && e
->ts
.cl
->length
->expr_type
== EXPR_CONSTANT
2444 && e
->ts
.cl
->length
->ts
.type
== BT_INTEGER
)
2446 result
= gfc_constant_result (BT_INTEGER
, k
, &e
->where
);
2447 mpz_set (result
->value
.integer
, e
->ts
.cl
->length
->value
.integer
);
2448 return range_check (result
, "LEN");
2456 gfc_simplify_len_trim (gfc_expr
*e
, gfc_expr
*kind
)
2459 int count
, len
, lentrim
, i
;
2460 int k
= get_kind (BT_INTEGER
, kind
, "LEN_TRIM", gfc_default_integer_kind
);
2463 return &gfc_bad_expr
;
2465 if (e
->expr_type
!= EXPR_CONSTANT
)
2468 result
= gfc_constant_result (BT_INTEGER
, k
, &e
->where
);
2469 len
= e
->value
.character
.length
;
2471 for (count
= 0, i
= 1; i
<= len
; i
++)
2472 if (e
->value
.character
.string
[len
- i
] == ' ')
2477 lentrim
= len
- count
;
2479 mpz_set_si (result
->value
.integer
, lentrim
);
2480 return range_check (result
, "LEN_TRIM");
2484 gfc_simplify_lgamma (gfc_expr
*x ATTRIBUTE_UNUSED
)
2486 #if MPFR_VERSION >= MPFR_VERSION_NUM(2,3,0)
2490 if (x
->expr_type
!= EXPR_CONSTANT
)
2493 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2495 mpfr_lgamma (result
->value
.real
, &sg
, x
->value
.real
, GFC_RND_MODE
);
2497 return range_check (result
, "LGAMMA");
2505 gfc_simplify_lge (gfc_expr
*a
, gfc_expr
*b
)
2507 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
2510 return gfc_logical_expr (gfc_compare_string (a
, b
) >= 0, &a
->where
);
2515 gfc_simplify_lgt (gfc_expr
*a
, gfc_expr
*b
)
2517 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
2520 return gfc_logical_expr (gfc_compare_string (a
, b
) > 0,
2526 gfc_simplify_lle (gfc_expr
*a
, gfc_expr
*b
)
2528 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
2531 return gfc_logical_expr (gfc_compare_string (a
, b
) <= 0, &a
->where
);
2536 gfc_simplify_llt (gfc_expr
*a
, gfc_expr
*b
)
2538 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
2541 return gfc_logical_expr (gfc_compare_string (a
, b
) < 0, &a
->where
);
2546 gfc_simplify_log (gfc_expr
*x
)
2551 if (x
->expr_type
!= EXPR_CONSTANT
)
2554 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2560 if (mpfr_sgn (x
->value
.real
) <= 0)
2562 gfc_error ("Argument of LOG at %L cannot be less than or equal "
2563 "to zero", &x
->where
);
2564 gfc_free_expr (result
);
2565 return &gfc_bad_expr
;
2568 mpfr_log (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
2572 if ((mpfr_sgn (x
->value
.complex.r
) == 0)
2573 && (mpfr_sgn (x
->value
.complex.i
) == 0))
2575 gfc_error ("Complex argument of LOG at %L cannot be zero",
2577 gfc_free_expr (result
);
2578 return &gfc_bad_expr
;
2581 gfc_set_model_kind (x
->ts
.kind
);
2585 mpfr_atan2 (result
->value
.complex.i
, x
->value
.complex.i
,
2586 x
->value
.complex.r
, GFC_RND_MODE
);
2588 mpfr_mul (xr
, x
->value
.complex.r
, x
->value
.complex.r
, GFC_RND_MODE
);
2589 mpfr_mul (xi
, x
->value
.complex.i
, x
->value
.complex.i
, GFC_RND_MODE
);
2590 mpfr_add (xr
, xr
, xi
, GFC_RND_MODE
);
2591 mpfr_sqrt (xr
, xr
, GFC_RND_MODE
);
2592 mpfr_log (result
->value
.complex.r
, xr
, GFC_RND_MODE
);
2594 mpfr_clears (xr
, xi
, NULL
);
2599 gfc_internal_error ("gfc_simplify_log: bad type");
2602 return range_check (result
, "LOG");
2607 gfc_simplify_log10 (gfc_expr
*x
)
2611 if (x
->expr_type
!= EXPR_CONSTANT
)
2614 if (mpfr_sgn (x
->value
.real
) <= 0)
2616 gfc_error ("Argument of LOG10 at %L cannot be less than or equal "
2617 "to zero", &x
->where
);
2618 return &gfc_bad_expr
;
2621 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2623 mpfr_log10 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
2625 return range_check (result
, "LOG10");
2630 gfc_simplify_logical (gfc_expr
*e
, gfc_expr
*k
)
2635 kind
= get_kind (BT_LOGICAL
, k
, "LOGICAL", gfc_default_logical_kind
);
2637 return &gfc_bad_expr
;
2639 if (e
->expr_type
!= EXPR_CONSTANT
)
2642 result
= gfc_constant_result (BT_LOGICAL
, kind
, &e
->where
);
2644 result
->value
.logical
= e
->value
.logical
;
2650 /* This function is special since MAX() can take any number of
2651 arguments. The simplified expression is a rewritten version of the
2652 argument list containing at most one constant element. Other
2653 constant elements are deleted. Because the argument list has
2654 already been checked, this function always succeeds. sign is 1 for
2655 MAX(), -1 for MIN(). */
2658 simplify_min_max (gfc_expr
*expr
, int sign
)
2660 gfc_actual_arglist
*arg
, *last
, *extremum
;
2661 gfc_intrinsic_sym
* specific
;
2665 specific
= expr
->value
.function
.isym
;
2667 arg
= expr
->value
.function
.actual
;
2669 for (; arg
; last
= arg
, arg
= arg
->next
)
2671 if (arg
->expr
->expr_type
!= EXPR_CONSTANT
)
2674 if (extremum
== NULL
)
2680 switch (arg
->expr
->ts
.type
)
2683 if (mpz_cmp (arg
->expr
->value
.integer
,
2684 extremum
->expr
->value
.integer
) * sign
> 0)
2685 mpz_set (extremum
->expr
->value
.integer
, arg
->expr
->value
.integer
);
2689 /* We need to use mpfr_min and mpfr_max to treat NaN properly. */
2691 mpfr_max (extremum
->expr
->value
.real
, extremum
->expr
->value
.real
,
2692 arg
->expr
->value
.real
, GFC_RND_MODE
);
2694 mpfr_min (extremum
->expr
->value
.real
, extremum
->expr
->value
.real
,
2695 arg
->expr
->value
.real
, GFC_RND_MODE
);
2699 #define LENGTH(x) ((x)->expr->value.character.length)
2700 #define STRING(x) ((x)->expr->value.character.string)
2701 if (LENGTH(extremum
) < LENGTH(arg
))
2703 gfc_char_t
*tmp
= STRING(extremum
);
2705 STRING(extremum
) = gfc_get_wide_string (LENGTH(arg
) + 1);
2706 memcpy (STRING(extremum
), tmp
,
2707 LENGTH(extremum
) * sizeof (gfc_char_t
));
2708 gfc_wide_memset (&STRING(extremum
)[LENGTH(extremum
)], ' ',
2709 LENGTH(arg
) - LENGTH(extremum
));
2710 STRING(extremum
)[LENGTH(arg
)] = '\0'; /* For debugger */
2711 LENGTH(extremum
) = LENGTH(arg
);
2715 if (gfc_compare_string (arg
->expr
, extremum
->expr
) * sign
> 0)
2717 gfc_free (STRING(extremum
));
2718 STRING(extremum
) = gfc_get_wide_string (LENGTH(extremum
) + 1);
2719 memcpy (STRING(extremum
), STRING(arg
),
2720 LENGTH(arg
) * sizeof (gfc_char_t
));
2721 gfc_wide_memset (&STRING(extremum
)[LENGTH(arg
)], ' ',
2722 LENGTH(extremum
) - LENGTH(arg
));
2723 STRING(extremum
)[LENGTH(extremum
)] = '\0'; /* For debugger */
2731 gfc_internal_error ("simplify_min_max(): Bad type in arglist");
2734 /* Delete the extra constant argument. */
2736 expr
->value
.function
.actual
= arg
->next
;
2738 last
->next
= arg
->next
;
2741 gfc_free_actual_arglist (arg
);
2745 /* If there is one value left, replace the function call with the
2747 if (expr
->value
.function
.actual
->next
!= NULL
)
2750 /* Convert to the correct type and kind. */
2751 if (expr
->ts
.type
!= BT_UNKNOWN
)
2752 return gfc_convert_constant (expr
->value
.function
.actual
->expr
,
2753 expr
->ts
.type
, expr
->ts
.kind
);
2755 if (specific
->ts
.type
!= BT_UNKNOWN
)
2756 return gfc_convert_constant (expr
->value
.function
.actual
->expr
,
2757 specific
->ts
.type
, specific
->ts
.kind
);
2759 return gfc_copy_expr (expr
->value
.function
.actual
->expr
);
2764 gfc_simplify_min (gfc_expr
*e
)
2766 return simplify_min_max (e
, -1);
2771 gfc_simplify_max (gfc_expr
*e
)
2773 return simplify_min_max (e
, 1);
2778 gfc_simplify_maxexponent (gfc_expr
*x
)
2783 i
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, false);
2785 result
= gfc_int_expr (gfc_real_kinds
[i
].max_exponent
);
2786 result
->where
= x
->where
;
2793 gfc_simplify_minexponent (gfc_expr
*x
)
2798 i
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, false);
2800 result
= gfc_int_expr (gfc_real_kinds
[i
].min_exponent
);
2801 result
->where
= x
->where
;
2808 gfc_simplify_mod (gfc_expr
*a
, gfc_expr
*p
)
2814 if (a
->expr_type
!= EXPR_CONSTANT
|| p
->expr_type
!= EXPR_CONSTANT
)
2817 kind
= a
->ts
.kind
> p
->ts
.kind
? a
->ts
.kind
: p
->ts
.kind
;
2818 result
= gfc_constant_result (a
->ts
.type
, kind
, &a
->where
);
2823 if (mpz_cmp_ui (p
->value
.integer
, 0) == 0)
2825 /* Result is processor-dependent. */
2826 gfc_error ("Second argument MOD at %L is zero", &a
->where
);
2827 gfc_free_expr (result
);
2828 return &gfc_bad_expr
;
2830 mpz_tdiv_r (result
->value
.integer
, a
->value
.integer
, p
->value
.integer
);
2834 if (mpfr_cmp_ui (p
->value
.real
, 0) == 0)
2836 /* Result is processor-dependent. */
2837 gfc_error ("Second argument of MOD at %L is zero", &p
->where
);
2838 gfc_free_expr (result
);
2839 return &gfc_bad_expr
;
2842 gfc_set_model_kind (kind
);
2844 mpfr_div (tmp
, a
->value
.real
, p
->value
.real
, GFC_RND_MODE
);
2845 mpfr_trunc (tmp
, tmp
);
2846 mpfr_mul (tmp
, tmp
, p
->value
.real
, GFC_RND_MODE
);
2847 mpfr_sub (result
->value
.real
, a
->value
.real
, tmp
, GFC_RND_MODE
);
2852 gfc_internal_error ("gfc_simplify_mod(): Bad arguments");
2855 return range_check (result
, "MOD");
2860 gfc_simplify_modulo (gfc_expr
*a
, gfc_expr
*p
)
2866 if (a
->expr_type
!= EXPR_CONSTANT
|| p
->expr_type
!= EXPR_CONSTANT
)
2869 kind
= a
->ts
.kind
> p
->ts
.kind
? a
->ts
.kind
: p
->ts
.kind
;
2870 result
= gfc_constant_result (a
->ts
.type
, kind
, &a
->where
);
2875 if (mpz_cmp_ui (p
->value
.integer
, 0) == 0)
2877 /* Result is processor-dependent. This processor just opts
2878 to not handle it at all. */
2879 gfc_error ("Second argument of MODULO at %L is zero", &a
->where
);
2880 gfc_free_expr (result
);
2881 return &gfc_bad_expr
;
2883 mpz_fdiv_r (result
->value
.integer
, a
->value
.integer
, p
->value
.integer
);
2888 if (mpfr_cmp_ui (p
->value
.real
, 0) == 0)
2890 /* Result is processor-dependent. */
2891 gfc_error ("Second argument of MODULO at %L is zero", &p
->where
);
2892 gfc_free_expr (result
);
2893 return &gfc_bad_expr
;
2896 gfc_set_model_kind (kind
);
2898 mpfr_div (tmp
, a
->value
.real
, p
->value
.real
, GFC_RND_MODE
);
2899 mpfr_floor (tmp
, tmp
);
2900 mpfr_mul (tmp
, tmp
, p
->value
.real
, GFC_RND_MODE
);
2901 mpfr_sub (result
->value
.real
, a
->value
.real
, tmp
, GFC_RND_MODE
);
2906 gfc_internal_error ("gfc_simplify_modulo(): Bad arguments");
2909 return range_check (result
, "MODULO");
2913 /* Exists for the sole purpose of consistency with other intrinsics. */
2915 gfc_simplify_mvbits (gfc_expr
*f ATTRIBUTE_UNUSED
,
2916 gfc_expr
*fp ATTRIBUTE_UNUSED
,
2917 gfc_expr
*l ATTRIBUTE_UNUSED
,
2918 gfc_expr
*to ATTRIBUTE_UNUSED
,
2919 gfc_expr
*tp ATTRIBUTE_UNUSED
)
2926 gfc_simplify_nearest (gfc_expr
*x
, gfc_expr
*s
)
2929 mp_exp_t emin
, emax
;
2932 if (x
->expr_type
!= EXPR_CONSTANT
|| s
->expr_type
!= EXPR_CONSTANT
)
2935 if (mpfr_sgn (s
->value
.real
) == 0)
2937 gfc_error ("Second argument of NEAREST at %L shall not be zero",
2939 return &gfc_bad_expr
;
2942 result
= gfc_copy_expr (x
);
2944 /* Save current values of emin and emax. */
2945 emin
= mpfr_get_emin ();
2946 emax
= mpfr_get_emax ();
2948 /* Set emin and emax for the current model number. */
2949 kind
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, 0);
2950 mpfr_set_emin ((mp_exp_t
) gfc_real_kinds
[kind
].min_exponent
-
2951 mpfr_get_prec(result
->value
.real
) + 1);
2952 mpfr_set_emax ((mp_exp_t
) gfc_real_kinds
[kind
].max_exponent
- 1);
2954 if (mpfr_sgn (s
->value
.real
) > 0)
2956 mpfr_nextabove (result
->value
.real
);
2957 mpfr_subnormalize (result
->value
.real
, 0, GMP_RNDU
);
2961 mpfr_nextbelow (result
->value
.real
);
2962 mpfr_subnormalize (result
->value
.real
, 0, GMP_RNDD
);
2965 mpfr_set_emin (emin
);
2966 mpfr_set_emax (emax
);
2968 /* Only NaN can occur. Do not use range check as it gives an
2969 error for denormal numbers. */
2970 if (mpfr_nan_p (result
->value
.real
) && gfc_option
.flag_range_check
)
2972 gfc_error ("Result of NEAREST is NaN at %L", &result
->where
);
2973 gfc_free_expr (result
);
2974 return &gfc_bad_expr
;
2982 simplify_nint (const char *name
, gfc_expr
*e
, gfc_expr
*k
)
2984 gfc_expr
*itrunc
, *result
;
2987 kind
= get_kind (BT_INTEGER
, k
, name
, gfc_default_integer_kind
);
2989 return &gfc_bad_expr
;
2991 if (e
->expr_type
!= EXPR_CONSTANT
)
2994 result
= gfc_constant_result (BT_INTEGER
, kind
, &e
->where
);
2996 itrunc
= gfc_copy_expr (e
);
2998 mpfr_round (itrunc
->value
.real
, e
->value
.real
);
3000 gfc_mpfr_to_mpz (result
->value
.integer
, itrunc
->value
.real
);
3002 gfc_free_expr (itrunc
);
3004 return range_check (result
, name
);
3009 gfc_simplify_new_line (gfc_expr
*e
)
3013 result
= gfc_constant_result (BT_CHARACTER
, e
->ts
.kind
, &e
->where
);
3014 result
->value
.character
.string
= gfc_get_wide_string (2);
3015 result
->value
.character
.length
= 1;
3016 result
->value
.character
.string
[0] = '\n';
3017 result
->value
.character
.string
[1] = '\0'; /* For debugger */
3023 gfc_simplify_nint (gfc_expr
*e
, gfc_expr
*k
)
3025 return simplify_nint ("NINT", e
, k
);
3030 gfc_simplify_idnint (gfc_expr
*e
)
3032 return simplify_nint ("IDNINT", e
, NULL
);
3037 gfc_simplify_not (gfc_expr
*e
)
3041 if (e
->expr_type
!= EXPR_CONSTANT
)
3044 result
= gfc_constant_result (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
3046 mpz_com (result
->value
.integer
, e
->value
.integer
);
3048 return range_check (result
, "NOT");
3053 gfc_simplify_null (gfc_expr
*mold
)
3059 result
= gfc_get_expr ();
3060 result
->ts
.type
= BT_UNKNOWN
;
3063 result
= gfc_copy_expr (mold
);
3064 result
->expr_type
= EXPR_NULL
;
3071 gfc_simplify_or (gfc_expr
*x
, gfc_expr
*y
)
3076 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
3079 kind
= x
->ts
.kind
> y
->ts
.kind
? x
->ts
.kind
: y
->ts
.kind
;
3080 if (x
->ts
.type
== BT_INTEGER
)
3082 result
= gfc_constant_result (BT_INTEGER
, kind
, &x
->where
);
3083 mpz_ior (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
3084 return range_check (result
, "OR");
3086 else /* BT_LOGICAL */
3088 result
= gfc_constant_result (BT_LOGICAL
, kind
, &x
->where
);
3089 result
->value
.logical
= x
->value
.logical
|| y
->value
.logical
;
3096 gfc_simplify_precision (gfc_expr
*e
)
3101 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
3103 result
= gfc_int_expr (gfc_real_kinds
[i
].precision
);
3104 result
->where
= e
->where
;
3111 gfc_simplify_radix (gfc_expr
*e
)
3116 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
3120 i
= gfc_integer_kinds
[i
].radix
;
3124 i
= gfc_real_kinds
[i
].radix
;
3131 result
= gfc_int_expr (i
);
3132 result
->where
= e
->where
;
3139 gfc_simplify_range (gfc_expr
*e
)
3145 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
3150 j
= gfc_integer_kinds
[i
].range
;
3155 j
= gfc_real_kinds
[i
].range
;
3162 result
= gfc_int_expr (j
);
3163 result
->where
= e
->where
;
3170 gfc_simplify_real (gfc_expr
*e
, gfc_expr
*k
)
3172 gfc_expr
*result
= NULL
;
3175 if (e
->ts
.type
== BT_COMPLEX
)
3176 kind
= get_kind (BT_REAL
, k
, "REAL", e
->ts
.kind
);
3178 kind
= get_kind (BT_REAL
, k
, "REAL", gfc_default_real_kind
);
3181 return &gfc_bad_expr
;
3183 if (e
->expr_type
!= EXPR_CONSTANT
)
3190 result
= gfc_int2real (e
, kind
);
3194 result
= gfc_real2real (e
, kind
);
3198 result
= gfc_complex2real (e
, kind
);
3202 gfc_internal_error ("bad type in REAL");
3206 if (e
->ts
.type
== BT_INTEGER
&& e
->is_boz
)
3212 result
= gfc_copy_expr (e
);
3213 if (!gfc_convert_boz (result
, &ts
))
3215 gfc_free_expr (result
);
3216 return &gfc_bad_expr
;
3220 return range_check (result
, "REAL");
3225 gfc_simplify_realpart (gfc_expr
*e
)
3229 if (e
->expr_type
!= EXPR_CONSTANT
)
3232 result
= gfc_constant_result (BT_REAL
, e
->ts
.kind
, &e
->where
);
3233 mpfr_set (result
->value
.real
, e
->value
.complex.r
, GFC_RND_MODE
);
3235 return range_check (result
, "REALPART");
3239 gfc_simplify_repeat (gfc_expr
*e
, gfc_expr
*n
)
3242 int i
, j
, len
, ncop
, nlen
;
3244 bool have_length
= false;
3246 /* If NCOPIES isn't a constant, there's nothing we can do. */
3247 if (n
->expr_type
!= EXPR_CONSTANT
)
3250 /* If NCOPIES is negative, it's an error. */
3251 if (mpz_sgn (n
->value
.integer
) < 0)
3253 gfc_error ("Argument NCOPIES of REPEAT intrinsic is negative at %L",
3255 return &gfc_bad_expr
;
3258 /* If we don't know the character length, we can do no more. */
3259 if (e
->ts
.cl
&& e
->ts
.cl
->length
3260 && e
->ts
.cl
->length
->expr_type
== EXPR_CONSTANT
)
3262 len
= mpz_get_si (e
->ts
.cl
->length
->value
.integer
);
3265 else if (e
->expr_type
== EXPR_CONSTANT
3266 && (e
->ts
.cl
== NULL
|| e
->ts
.cl
->length
== NULL
))
3268 len
= e
->value
.character
.length
;
3273 /* If the source length is 0, any value of NCOPIES is valid
3274 and everything behaves as if NCOPIES == 0. */
3277 mpz_set_ui (ncopies
, 0);
3279 mpz_set (ncopies
, n
->value
.integer
);
3281 /* Check that NCOPIES isn't too large. */
3287 /* Compute the maximum value allowed for NCOPIES: huge(cl) / len. */
3289 i
= gfc_validate_kind (BT_INTEGER
, gfc_charlen_int_kind
, false);
3293 mpz_tdiv_q (max
, gfc_integer_kinds
[i
].huge
,
3294 e
->ts
.cl
->length
->value
.integer
);
3298 mpz_init_set_si (mlen
, len
);
3299 mpz_tdiv_q (max
, gfc_integer_kinds
[i
].huge
, mlen
);
3303 /* The check itself. */
3304 if (mpz_cmp (ncopies
, max
) > 0)
3307 mpz_clear (ncopies
);
3308 gfc_error ("Argument NCOPIES of REPEAT intrinsic is too large at %L",
3310 return &gfc_bad_expr
;
3315 mpz_clear (ncopies
);
3317 /* For further simplification, we need the character string to be
3319 if (e
->expr_type
!= EXPR_CONSTANT
)
3323 (e
->ts
.cl
->length
&&
3324 mpz_sgn (e
->ts
.cl
->length
->value
.integer
)) != 0)
3326 const char *res
= gfc_extract_int (n
, &ncop
);
3327 gcc_assert (res
== NULL
);
3332 len
= e
->value
.character
.length
;
3335 result
= gfc_constant_result (BT_CHARACTER
, e
->ts
.kind
, &e
->where
);
3339 result
->value
.character
.string
= gfc_get_wide_string (1);
3340 result
->value
.character
.length
= 0;
3341 result
->value
.character
.string
[0] = '\0';
3345 result
->value
.character
.length
= nlen
;
3346 result
->value
.character
.string
= gfc_get_wide_string (nlen
+ 1);
3348 for (i
= 0; i
< ncop
; i
++)
3349 for (j
= 0; j
< len
; j
++)
3350 result
->value
.character
.string
[j
+i
*len
]= e
->value
.character
.string
[j
];
3352 result
->value
.character
.string
[nlen
] = '\0'; /* For debugger */
3357 /* Test that the expression is an constant array. */
3360 is_constant_array_expr (gfc_expr
*e
)
3367 if (e
->expr_type
!= EXPR_ARRAY
|| !gfc_is_constant_expr (e
))
3370 if (e
->value
.constructor
== NULL
)
3373 for (c
= e
->value
.constructor
; c
; c
= c
->next
)
3374 if (c
->expr
->expr_type
!= EXPR_CONSTANT
)
3381 /* This one is a bear, but mainly has to do with shuffling elements. */
3384 gfc_simplify_reshape (gfc_expr
*source
, gfc_expr
*shape_exp
,
3385 gfc_expr
*pad
, gfc_expr
*order_exp
)
3387 int order
[GFC_MAX_DIMENSIONS
], shape
[GFC_MAX_DIMENSIONS
];
3388 int i
, rank
, npad
, x
[GFC_MAX_DIMENSIONS
];
3389 gfc_constructor
*head
, *tail
;
3395 /* Check that argument expression types are OK. */
3396 if (!is_constant_array_expr (source
))
3399 if (!is_constant_array_expr (shape_exp
))
3402 if (!is_constant_array_expr (pad
))
3405 if (!is_constant_array_expr (order_exp
))
3408 /* Proceed with simplification, unpacking the array. */
3416 e
= gfc_get_array_element (shape_exp
, rank
);
3420 if (gfc_extract_int (e
, &shape
[rank
]) != NULL
)
3422 gfc_error ("Integer too large in shape specification at %L",
3428 if (rank
>= GFC_MAX_DIMENSIONS
)
3430 gfc_error ("Too many dimensions in shape specification for RESHAPE "
3431 "at %L", &e
->where
);
3436 if (shape
[rank
] < 0)
3438 gfc_error ("Shape specification at %L cannot be negative",
3450 gfc_error ("Shape specification at %L cannot be the null array",
3455 /* Now unpack the order array if present. */
3456 if (order_exp
== NULL
)
3458 for (i
= 0; i
< rank
; i
++)
3463 for (i
= 0; i
< rank
; i
++)
3466 for (i
= 0; i
< rank
; i
++)
3468 e
= gfc_get_array_element (order_exp
, i
);
3471 gfc_error ("ORDER parameter of RESHAPE at %L is not the same "
3472 "size as SHAPE parameter", &order_exp
->where
);
3476 if (gfc_extract_int (e
, &order
[i
]) != NULL
)
3478 gfc_error ("Error in ORDER parameter of RESHAPE at %L",
3484 if (order
[i
] < 1 || order
[i
] > rank
)
3486 gfc_error ("ORDER parameter of RESHAPE at %L is out of range",
3496 gfc_error ("Invalid permutation in ORDER parameter at %L",
3508 /* Count the elements in the source and padding arrays. */
3513 gfc_array_size (pad
, &size
);
3514 npad
= mpz_get_ui (size
);
3518 gfc_array_size (source
, &size
);
3519 nsource
= mpz_get_ui (size
);
3522 /* If it weren't for that pesky permutation we could just loop
3523 through the source and round out any shortage with pad elements.
3524 But no, someone just had to have the compiler do something the
3525 user should be doing. */
3527 for (i
= 0; i
< rank
; i
++)
3532 /* Figure out which element to extract. */
3533 mpz_set_ui (index
, 0);
3535 for (i
= rank
- 1; i
>= 0; i
--)
3537 mpz_add_ui (index
, index
, x
[order
[i
]]);
3539 mpz_mul_ui (index
, index
, shape
[order
[i
- 1]]);
3542 if (mpz_cmp_ui (index
, INT_MAX
) > 0)
3543 gfc_internal_error ("Reshaped array too large at %C");
3545 j
= mpz_get_ui (index
);
3548 e
= gfc_get_array_element (source
, j
);
3555 gfc_error ("PAD parameter required for short SOURCE parameter "
3556 "at %L", &source
->where
);
3561 e
= gfc_get_array_element (pad
, j
);
3565 head
= tail
= gfc_get_constructor ();
3568 tail
->next
= gfc_get_constructor ();
3575 tail
->where
= e
->where
;
3578 /* Calculate the next element. */
3582 if (++x
[i
] < shape
[i
])
3593 e
= gfc_get_expr ();
3594 e
->where
= source
->where
;
3595 e
->expr_type
= EXPR_ARRAY
;
3596 e
->value
.constructor
= head
;
3597 e
->shape
= gfc_get_shape (rank
);
3599 for (i
= 0; i
< rank
; i
++)
3600 mpz_init_set_ui (e
->shape
[i
], shape
[i
]);
3608 gfc_free_constructor (head
);
3610 return &gfc_bad_expr
;
3615 gfc_simplify_rrspacing (gfc_expr
*x
)
3621 if (x
->expr_type
!= EXPR_CONSTANT
)
3624 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
3626 result
= gfc_constant_result (BT_REAL
, x
->ts
.kind
, &x
->where
);
3628 mpfr_abs (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
3630 /* Special case x = -0 and 0. */
3631 if (mpfr_sgn (result
->value
.real
) == 0)
3633 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
3637 /* | x * 2**(-e) | * 2**p. */
3638 e
= - (long int) mpfr_get_exp (x
->value
.real
);
3639 mpfr_mul_2si (result
->value
.real
, result
->value
.real
, e
, GFC_RND_MODE
);
3641 p
= (long int) gfc_real_kinds
[i
].digits
;
3642 mpfr_mul_2si (result
->value
.real
, result
->value
.real
, p
, GFC_RND_MODE
);
3644 return range_check (result
, "RRSPACING");
3649 gfc_simplify_scale (gfc_expr
*x
, gfc_expr
*i
)
3651 int k
, neg_flag
, power
, exp_range
;
3652 mpfr_t scale
, radix
;
3655 if (x
->expr_type
!= EXPR_CONSTANT
|| i
->expr_type
!= EXPR_CONSTANT
)
3658 result
= gfc_constant_result (BT_REAL
, x
->ts
.kind
, &x
->where
);
3660 if (mpfr_sgn (x
->value
.real
) == 0)
3662 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
3666 k
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, false);
3668 exp_range
= gfc_real_kinds
[k
].max_exponent
- gfc_real_kinds
[k
].min_exponent
;
3670 /* This check filters out values of i that would overflow an int. */
3671 if (mpz_cmp_si (i
->value
.integer
, exp_range
+ 2) > 0
3672 || mpz_cmp_si (i
->value
.integer
, -exp_range
- 2) < 0)
3674 gfc_error ("Result of SCALE overflows its kind at %L", &result
->where
);
3675 gfc_free_expr (result
);
3676 return &gfc_bad_expr
;
3679 /* Compute scale = radix ** power. */
3680 power
= mpz_get_si (i
->value
.integer
);
3690 gfc_set_model_kind (x
->ts
.kind
);
3693 mpfr_set_ui (radix
, gfc_real_kinds
[k
].radix
, GFC_RND_MODE
);
3694 mpfr_pow_ui (scale
, radix
, power
, GFC_RND_MODE
);
3697 mpfr_div (result
->value
.real
, x
->value
.real
, scale
, GFC_RND_MODE
);
3699 mpfr_mul (result
->value
.real
, x
->value
.real
, scale
, GFC_RND_MODE
);
3701 mpfr_clears (scale
, radix
, NULL
);
3703 return range_check (result
, "SCALE");
3707 /* Variants of strspn and strcspn that operate on wide characters. */
3710 wide_strspn (const gfc_char_t
*s1
, const gfc_char_t
*s2
)
3713 const gfc_char_t
*c
;
3717 for (c
= s2
; *c
; c
++)
3731 wide_strcspn (const gfc_char_t
*s1
, const gfc_char_t
*s2
)
3734 const gfc_char_t
*c
;
3738 for (c
= s2
; *c
; c
++)
3753 gfc_simplify_scan (gfc_expr
*e
, gfc_expr
*c
, gfc_expr
*b
, gfc_expr
*kind
)
3758 size_t indx
, len
, lenc
;
3759 int k
= get_kind (BT_INTEGER
, kind
, "SCAN", gfc_default_integer_kind
);
3762 return &gfc_bad_expr
;
3764 if (e
->expr_type
!= EXPR_CONSTANT
|| c
->expr_type
!= EXPR_CONSTANT
)
3767 if (b
!= NULL
&& b
->value
.logical
!= 0)
3772 result
= gfc_constant_result (BT_INTEGER
, k
, &e
->where
);
3774 len
= e
->value
.character
.length
;
3775 lenc
= c
->value
.character
.length
;
3777 if (len
== 0 || lenc
== 0)
3785 indx
= wide_strcspn (e
->value
.character
.string
,
3786 c
->value
.character
.string
) + 1;
3793 for (indx
= len
; indx
> 0; indx
--)
3795 for (i
= 0; i
< lenc
; i
++)
3797 if (c
->value
.character
.string
[i
]
3798 == e
->value
.character
.string
[indx
- 1])
3806 mpz_set_ui (result
->value
.integer
, indx
);
3807 return range_check (result
, "SCAN");
3812 gfc_simplify_selected_char_kind (gfc_expr
*e
)
3817 if (e
->expr_type
!= EXPR_CONSTANT
)
3820 if (gfc_compare_with_Cstring (e
, "ascii", false) == 0
3821 || gfc_compare_with_Cstring (e
, "default", false) == 0)
3823 else if (gfc_compare_with_Cstring (e
, "iso_10646", false) == 0)
3828 result
= gfc_int_expr (kind
);
3829 result
->where
= e
->where
;
3836 gfc_simplify_selected_int_kind (gfc_expr
*e
)
3841 if (e
->expr_type
!= EXPR_CONSTANT
|| gfc_extract_int (e
, &range
) != NULL
)
3846 for (i
= 0; gfc_integer_kinds
[i
].kind
!= 0; i
++)
3847 if (gfc_integer_kinds
[i
].range
>= range
3848 && gfc_integer_kinds
[i
].kind
< kind
)
3849 kind
= gfc_integer_kinds
[i
].kind
;
3851 if (kind
== INT_MAX
)
3854 result
= gfc_int_expr (kind
);
3855 result
->where
= e
->where
;
3862 gfc_simplify_selected_real_kind (gfc_expr
*p
, gfc_expr
*q
)
3864 int range
, precision
, i
, kind
, found_precision
, found_range
;
3871 if (p
->expr_type
!= EXPR_CONSTANT
3872 || gfc_extract_int (p
, &precision
) != NULL
)
3880 if (q
->expr_type
!= EXPR_CONSTANT
3881 || gfc_extract_int (q
, &range
) != NULL
)
3886 found_precision
= 0;
3889 for (i
= 0; gfc_real_kinds
[i
].kind
!= 0; i
++)
3891 if (gfc_real_kinds
[i
].precision
>= precision
)
3892 found_precision
= 1;
3894 if (gfc_real_kinds
[i
].range
>= range
)
3897 if (gfc_real_kinds
[i
].precision
>= precision
3898 && gfc_real_kinds
[i
].range
>= range
&& gfc_real_kinds
[i
].kind
< kind
)
3899 kind
= gfc_real_kinds
[i
].kind
;
3902 if (kind
== INT_MAX
)
3906 if (!found_precision
)
3912 result
= gfc_int_expr (kind
);
3913 result
->where
= (p
!= NULL
) ? p
->where
: q
->where
;
3920 gfc_simplify_set_exponent (gfc_expr
*x
, gfc_expr
*i
)
3923 mpfr_t exp
, absv
, log2
, pow2
, frac
;
3926 if (x
->expr_type
!= EXPR_CONSTANT
|| i
->expr_type
!= EXPR_CONSTANT
)
3929 result
= gfc_constant_result (BT_REAL
, x
->ts
.kind
, &x
->where
);
3931 if (mpfr_sgn (x
->value
.real
) == 0)
3933 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
3937 gfc_set_model_kind (x
->ts
.kind
);
3944 mpfr_abs (absv
, x
->value
.real
, GFC_RND_MODE
);
3945 mpfr_log2 (log2
, absv
, GFC_RND_MODE
);
3947 mpfr_trunc (log2
, log2
);
3948 mpfr_add_ui (exp
, log2
, 1, GFC_RND_MODE
);
3950 /* Old exponent value, and fraction. */
3951 mpfr_ui_pow (pow2
, 2, exp
, GFC_RND_MODE
);
3953 mpfr_div (frac
, absv
, pow2
, GFC_RND_MODE
);
3956 exp2
= (unsigned long) mpz_get_d (i
->value
.integer
);
3957 mpfr_mul_2exp (result
->value
.real
, frac
, exp2
, GFC_RND_MODE
);
3959 mpfr_clears (absv
, log2
, pow2
, frac
, NULL
);
3961 return range_check (result
, "SET_EXPONENT");
3966 gfc_simplify_shape (gfc_expr
*source
)
3968 mpz_t shape
[GFC_MAX_DIMENSIONS
];
3969 gfc_expr
*result
, *e
, *f
;
3974 if (source
->rank
== 0)
3975 return gfc_start_constructor (BT_INTEGER
, gfc_default_integer_kind
,
3978 if (source
->expr_type
!= EXPR_VARIABLE
)
3981 result
= gfc_start_constructor (BT_INTEGER
, gfc_default_integer_kind
,
3984 ar
= gfc_find_array_ref (source
);
3986 t
= gfc_array_ref_shape (ar
, shape
);
3988 for (n
= 0; n
< source
->rank
; n
++)
3990 e
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
3995 mpz_set (e
->value
.integer
, shape
[n
]);
3996 mpz_clear (shape
[n
]);
4000 mpz_set_ui (e
->value
.integer
, n
+ 1);
4002 f
= gfc_simplify_size (source
, e
, NULL
);
4006 gfc_free_expr (result
);
4015 gfc_append_constructor (result
, e
);
4023 gfc_simplify_size (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
)
4028 int k
= get_kind (BT_INTEGER
, kind
, "SIZE", gfc_default_integer_kind
);
4031 return &gfc_bad_expr
;
4035 if (gfc_array_size (array
, &size
) == FAILURE
)
4040 if (dim
->expr_type
!= EXPR_CONSTANT
)
4043 d
= mpz_get_ui (dim
->value
.integer
) - 1;
4044 if (gfc_array_dimen_size (array
, d
, &size
) == FAILURE
)
4048 result
= gfc_constant_result (BT_INTEGER
, k
, &array
->where
);
4049 mpz_set (result
->value
.integer
, size
);
4055 gfc_simplify_sign (gfc_expr
*x
, gfc_expr
*y
)
4059 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
4062 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
4067 mpz_abs (result
->value
.integer
, x
->value
.integer
);
4068 if (mpz_sgn (y
->value
.integer
) < 0)
4069 mpz_neg (result
->value
.integer
, result
->value
.integer
);
4074 /* TODO: Handle -0.0 and +0.0 correctly on machines that support
4076 mpfr_abs (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
4077 if (mpfr_sgn (y
->value
.real
) < 0)
4078 mpfr_neg (result
->value
.real
, result
->value
.real
, GFC_RND_MODE
);
4083 gfc_internal_error ("Bad type in gfc_simplify_sign");
4091 gfc_simplify_sin (gfc_expr
*x
)
4096 if (x
->expr_type
!= EXPR_CONSTANT
)
4099 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
4104 mpfr_sin (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
4108 gfc_set_model (x
->value
.real
);
4112 mpfr_sin (xp
, x
->value
.complex.r
, GFC_RND_MODE
);
4113 mpfr_cosh (xq
, x
->value
.complex.i
, GFC_RND_MODE
);
4114 mpfr_mul (result
->value
.complex.r
, xp
, xq
, GFC_RND_MODE
);
4116 mpfr_cos (xp
, x
->value
.complex.r
, GFC_RND_MODE
);
4117 mpfr_sinh (xq
, x
->value
.complex.i
, GFC_RND_MODE
);
4118 mpfr_mul (result
->value
.complex.i
, xp
, xq
, GFC_RND_MODE
);
4120 mpfr_clears (xp
, xq
, NULL
);
4124 gfc_internal_error ("in gfc_simplify_sin(): Bad type");
4127 return range_check (result
, "SIN");
4132 gfc_simplify_sinh (gfc_expr
*x
)
4136 if (x
->expr_type
!= EXPR_CONSTANT
)
4139 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
4141 mpfr_sinh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
4143 return range_check (result
, "SINH");
4147 /* The argument is always a double precision real that is converted to
4148 single precision. TODO: Rounding! */
4151 gfc_simplify_sngl (gfc_expr
*a
)
4155 if (a
->expr_type
!= EXPR_CONSTANT
)
4158 result
= gfc_real2real (a
, gfc_default_real_kind
);
4159 return range_check (result
, "SNGL");
4164 gfc_simplify_spacing (gfc_expr
*x
)
4170 if (x
->expr_type
!= EXPR_CONSTANT
)
4173 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
4175 result
= gfc_constant_result (BT_REAL
, x
->ts
.kind
, &x
->where
);
4177 /* Special case x = 0 and -0. */
4178 mpfr_abs (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
4179 if (mpfr_sgn (result
->value
.real
) == 0)
4181 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].tiny
, GFC_RND_MODE
);
4185 /* In the Fortran 95 standard, the result is b**(e - p) where b, e, and p
4186 are the radix, exponent of x, and precision. This excludes the
4187 possibility of subnormal numbers. Fortran 2003 states the result is
4188 b**max(e - p, emin - 1). */
4190 ep
= (long int) mpfr_get_exp (x
->value
.real
) - gfc_real_kinds
[i
].digits
;
4191 en
= (long int) gfc_real_kinds
[i
].min_exponent
- 1;
4192 en
= en
> ep
? en
: ep
;
4194 mpfr_set_ui (result
->value
.real
, 1, GFC_RND_MODE
);
4195 mpfr_mul_2si (result
->value
.real
, result
->value
.real
, en
, GFC_RND_MODE
);
4197 return range_check (result
, "SPACING");
4202 gfc_simplify_sqrt (gfc_expr
*e
)
4205 mpfr_t ac
, ad
, s
, t
, w
;
4207 if (e
->expr_type
!= EXPR_CONSTANT
)
4210 result
= gfc_constant_result (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
4215 if (mpfr_cmp_si (e
->value
.real
, 0) < 0)
4217 mpfr_sqrt (result
->value
.real
, e
->value
.real
, GFC_RND_MODE
);
4222 /* Formula taken from Numerical Recipes to avoid over- and
4225 gfc_set_model (e
->value
.real
);
4232 if (mpfr_cmp_ui (e
->value
.complex.r
, 0) == 0
4233 && mpfr_cmp_ui (e
->value
.complex.i
, 0) == 0)
4235 mpfr_set_ui (result
->value
.complex.r
, 0, GFC_RND_MODE
);
4236 mpfr_set_ui (result
->value
.complex.i
, 0, GFC_RND_MODE
);
4240 mpfr_abs (ac
, e
->value
.complex.r
, GFC_RND_MODE
);
4241 mpfr_abs (ad
, e
->value
.complex.i
, GFC_RND_MODE
);
4243 if (mpfr_cmp (ac
, ad
) >= 0)
4245 mpfr_div (t
, e
->value
.complex.i
, e
->value
.complex.r
, GFC_RND_MODE
);
4246 mpfr_mul (t
, t
, t
, GFC_RND_MODE
);
4247 mpfr_add_ui (t
, t
, 1, GFC_RND_MODE
);
4248 mpfr_sqrt (t
, t
, GFC_RND_MODE
);
4249 mpfr_add_ui (t
, t
, 1, GFC_RND_MODE
);
4250 mpfr_div_ui (t
, t
, 2, GFC_RND_MODE
);
4251 mpfr_sqrt (t
, t
, GFC_RND_MODE
);
4252 mpfr_sqrt (s
, ac
, GFC_RND_MODE
);
4253 mpfr_mul (w
, s
, t
, GFC_RND_MODE
);
4257 mpfr_div (s
, e
->value
.complex.r
, e
->value
.complex.i
, GFC_RND_MODE
);
4258 mpfr_mul (t
, s
, s
, GFC_RND_MODE
);
4259 mpfr_add_ui (t
, t
, 1, GFC_RND_MODE
);
4260 mpfr_sqrt (t
, t
, GFC_RND_MODE
);
4261 mpfr_abs (s
, s
, GFC_RND_MODE
);
4262 mpfr_add (t
, t
, s
, GFC_RND_MODE
);
4263 mpfr_div_ui (t
, t
, 2, GFC_RND_MODE
);
4264 mpfr_sqrt (t
, t
, GFC_RND_MODE
);
4265 mpfr_sqrt (s
, ad
, GFC_RND_MODE
);
4266 mpfr_mul (w
, s
, t
, GFC_RND_MODE
);
4269 if (mpfr_cmp_ui (w
, 0) != 0 && mpfr_cmp_ui (e
->value
.complex.r
, 0) >= 0)
4271 mpfr_mul_ui (t
, w
, 2, GFC_RND_MODE
);
4272 mpfr_div (result
->value
.complex.i
, e
->value
.complex.i
, t
, GFC_RND_MODE
);
4273 mpfr_set (result
->value
.complex.r
, w
, GFC_RND_MODE
);
4275 else if (mpfr_cmp_ui (w
, 0) != 0
4276 && mpfr_cmp_ui (e
->value
.complex.r
, 0) < 0
4277 && mpfr_cmp_ui (e
->value
.complex.i
, 0) >= 0)
4279 mpfr_mul_ui (t
, w
, 2, GFC_RND_MODE
);
4280 mpfr_div (result
->value
.complex.r
, e
->value
.complex.i
, t
, GFC_RND_MODE
);
4281 mpfr_set (result
->value
.complex.i
, w
, GFC_RND_MODE
);
4283 else if (mpfr_cmp_ui (w
, 0) != 0
4284 && mpfr_cmp_ui (e
->value
.complex.r
, 0) < 0
4285 && mpfr_cmp_ui (e
->value
.complex.i
, 0) < 0)
4287 mpfr_mul_ui (t
, w
, 2, GFC_RND_MODE
);
4288 mpfr_div (result
->value
.complex.r
, ad
, t
, GFC_RND_MODE
);
4289 mpfr_neg (w
, w
, GFC_RND_MODE
);
4290 mpfr_set (result
->value
.complex.i
, w
, GFC_RND_MODE
);
4293 gfc_internal_error ("invalid complex argument of SQRT at %L",
4296 mpfr_clears (s
, t
, ac
, ad
, w
, NULL
);
4301 gfc_internal_error ("invalid argument of SQRT at %L", &e
->where
);
4304 return range_check (result
, "SQRT");
4307 gfc_free_expr (result
);
4308 gfc_error ("Argument of SQRT at %L has a negative value", &e
->where
);
4309 return &gfc_bad_expr
;
4314 gfc_simplify_tan (gfc_expr
*x
)
4319 if (x
->expr_type
!= EXPR_CONSTANT
)
4322 i
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, false);
4324 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
4326 mpfr_tan (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
4328 return range_check (result
, "TAN");
4333 gfc_simplify_tanh (gfc_expr
*x
)
4337 if (x
->expr_type
!= EXPR_CONSTANT
)
4340 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
4342 mpfr_tanh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
4344 return range_check (result
, "TANH");
4350 gfc_simplify_tiny (gfc_expr
*e
)
4355 i
= gfc_validate_kind (BT_REAL
, e
->ts
.kind
, false);
4357 result
= gfc_constant_result (BT_REAL
, e
->ts
.kind
, &e
->where
);
4358 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].tiny
, GFC_RND_MODE
);
4365 gfc_simplify_trailz (gfc_expr
*e
)
4368 unsigned long tz
, bs
;
4371 if (e
->expr_type
!= EXPR_CONSTANT
)
4374 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
4375 bs
= gfc_integer_kinds
[i
].bit_size
;
4376 tz
= mpz_scan1 (e
->value
.integer
, 0);
4378 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
, &e
->where
);
4379 mpz_set_ui (result
->value
.integer
, MIN (tz
, bs
));
4386 gfc_simplify_transfer (gfc_expr
*source
, gfc_expr
*mold
, gfc_expr
*size
)
4389 gfc_expr
*mold_element
;
4392 size_t result_elt_size
;
4395 unsigned char *buffer
;
4397 if (!gfc_is_constant_expr (source
)
4398 || (gfc_init_expr
&& !gfc_is_constant_expr (mold
))
4399 || !gfc_is_constant_expr (size
))
4402 if (source
->expr_type
== EXPR_FUNCTION
)
4405 /* Calculate the size of the source. */
4406 if (source
->expr_type
== EXPR_ARRAY
4407 && gfc_array_size (source
, &tmp
) == FAILURE
)
4408 gfc_internal_error ("Failure getting length of a constant array.");
4410 source_size
= gfc_target_expr_size (source
);
4412 /* Create an empty new expression with the appropriate characteristics. */
4413 result
= gfc_constant_result (mold
->ts
.type
, mold
->ts
.kind
,
4415 result
->ts
= mold
->ts
;
4417 mold_element
= mold
->expr_type
== EXPR_ARRAY
4418 ? mold
->value
.constructor
->expr
4421 /* Set result character length, if needed. Note that this needs to be
4422 set even for array expressions, in order to pass this information into
4423 gfc_target_interpret_expr. */
4424 if (result
->ts
.type
== BT_CHARACTER
&& gfc_is_constant_expr (mold_element
))
4425 result
->value
.character
.length
= mold_element
->value
.character
.length
;
4427 /* Set the number of elements in the result, and determine its size. */
4428 result_elt_size
= gfc_target_expr_size (mold_element
);
4429 if (result_elt_size
== 0)
4431 gfc_free_expr (result
);
4435 if (mold
->expr_type
== EXPR_ARRAY
|| mold
->rank
|| size
)
4439 result
->expr_type
= EXPR_ARRAY
;
4443 result_length
= (size_t)mpz_get_ui (size
->value
.integer
);
4446 result_length
= source_size
/ result_elt_size
;
4447 if (result_length
* result_elt_size
< source_size
)
4451 result
->shape
= gfc_get_shape (1);
4452 mpz_init_set_ui (result
->shape
[0], result_length
);
4454 result_size
= result_length
* result_elt_size
;
4459 result_size
= result_elt_size
;
4462 if (gfc_option
.warn_surprising
&& source_size
< result_size
)
4463 gfc_warning("Intrinsic TRANSFER at %L has partly undefined result: "
4464 "source size %ld < result size %ld", &source
->where
,
4465 (long) source_size
, (long) result_size
);
4467 /* Allocate the buffer to store the binary version of the source. */
4468 buffer_size
= MAX (source_size
, result_size
);
4469 buffer
= (unsigned char*)alloca (buffer_size
);
4471 /* Now write source to the buffer. */
4472 gfc_target_encode_expr (source
, buffer
, buffer_size
);
4474 /* And read the buffer back into the new expression. */
4475 gfc_target_interpret_expr (buffer
, buffer_size
, result
);
4482 gfc_simplify_trim (gfc_expr
*e
)
4485 int count
, i
, len
, lentrim
;
4487 if (e
->expr_type
!= EXPR_CONSTANT
)
4490 len
= e
->value
.character
.length
;
4492 result
= gfc_constant_result (BT_CHARACTER
, e
->ts
.kind
, &e
->where
);
4494 for (count
= 0, i
= 1; i
<= len
; ++i
)
4496 if (e
->value
.character
.string
[len
- i
] == ' ')
4502 lentrim
= len
- count
;
4504 result
->value
.character
.length
= lentrim
;
4505 result
->value
.character
.string
= gfc_get_wide_string (lentrim
+ 1);
4507 for (i
= 0; i
< lentrim
; i
++)
4508 result
->value
.character
.string
[i
] = e
->value
.character
.string
[i
];
4510 result
->value
.character
.string
[lentrim
] = '\0'; /* For debugger */
4517 gfc_simplify_ubound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
)
4519 return simplify_bound (array
, dim
, kind
, 1);
4524 gfc_simplify_verify (gfc_expr
*s
, gfc_expr
*set
, gfc_expr
*b
, gfc_expr
*kind
)
4528 size_t index
, len
, lenset
;
4530 int k
= get_kind (BT_INTEGER
, kind
, "VERIFY", gfc_default_integer_kind
);
4533 return &gfc_bad_expr
;
4535 if (s
->expr_type
!= EXPR_CONSTANT
|| set
->expr_type
!= EXPR_CONSTANT
)
4538 if (b
!= NULL
&& b
->value
.logical
!= 0)
4543 result
= gfc_constant_result (BT_INTEGER
, k
, &s
->where
);
4545 len
= s
->value
.character
.length
;
4546 lenset
= set
->value
.character
.length
;
4550 mpz_set_ui (result
->value
.integer
, 0);
4558 mpz_set_ui (result
->value
.integer
, 1);
4562 index
= wide_strspn (s
->value
.character
.string
,
4563 set
->value
.character
.string
) + 1;
4572 mpz_set_ui (result
->value
.integer
, len
);
4575 for (index
= len
; index
> 0; index
--)
4577 for (i
= 0; i
< lenset
; i
++)
4579 if (s
->value
.character
.string
[index
- 1]
4580 == set
->value
.character
.string
[i
])
4588 mpz_set_ui (result
->value
.integer
, index
);
4594 gfc_simplify_xor (gfc_expr
*x
, gfc_expr
*y
)
4599 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
4602 kind
= x
->ts
.kind
> y
->ts
.kind
? x
->ts
.kind
: y
->ts
.kind
;
4603 if (x
->ts
.type
== BT_INTEGER
)
4605 result
= gfc_constant_result (BT_INTEGER
, kind
, &x
->where
);
4606 mpz_xor (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
4607 return range_check (result
, "XOR");
4609 else /* BT_LOGICAL */
4611 result
= gfc_constant_result (BT_LOGICAL
, kind
, &x
->where
);
4612 result
->value
.logical
= (x
->value
.logical
&& !y
->value
.logical
)
4613 || (!x
->value
.logical
&& y
->value
.logical
);
4620 /****************** Constant simplification *****************/
4622 /* Master function to convert one constant to another. While this is
4623 used as a simplification function, it requires the destination type
4624 and kind information which is supplied by a special case in
4628 gfc_convert_constant (gfc_expr
*e
, bt type
, int kind
)
4630 gfc_expr
*g
, *result
, *(*f
) (gfc_expr
*, int);
4631 gfc_constructor
*head
, *c
, *tail
= NULL
;
4645 f
= gfc_int2complex
;
4665 f
= gfc_real2complex
;
4676 f
= gfc_complex2int
;
4679 f
= gfc_complex2real
;
4682 f
= gfc_complex2complex
;
4708 f
= gfc_hollerith2int
;
4712 f
= gfc_hollerith2real
;
4716 f
= gfc_hollerith2complex
;
4720 f
= gfc_hollerith2character
;
4724 f
= gfc_hollerith2logical
;
4734 gfc_internal_error ("gfc_convert_constant(): Unexpected type");
4739 switch (e
->expr_type
)
4742 result
= f (e
, kind
);
4744 return &gfc_bad_expr
;
4748 if (!gfc_is_constant_expr (e
))
4753 for (c
= e
->value
.constructor
; c
; c
= c
->next
)
4756 head
= tail
= gfc_get_constructor ();
4759 tail
->next
= gfc_get_constructor ();
4763 tail
->where
= c
->where
;
4765 if (c
->iterator
== NULL
)
4766 tail
->expr
= f (c
->expr
, kind
);
4769 g
= gfc_convert_constant (c
->expr
, type
, kind
);
4770 if (g
== &gfc_bad_expr
)
4775 if (tail
->expr
== NULL
)
4777 gfc_free_constructor (head
);
4782 result
= gfc_get_expr ();
4783 result
->ts
.type
= type
;
4784 result
->ts
.kind
= kind
;
4785 result
->expr_type
= EXPR_ARRAY
;
4786 result
->value
.constructor
= head
;
4787 result
->shape
= gfc_copy_shape (e
->shape
, e
->rank
);
4788 result
->where
= e
->where
;
4789 result
->rank
= e
->rank
;
4800 /* Function for converting character constants. */
4802 gfc_convert_char_constant (gfc_expr
*e
, bt type ATTRIBUTE_UNUSED
, int kind
)
4807 if (!gfc_is_constant_expr (e
))
4810 if (e
->expr_type
== EXPR_CONSTANT
)
4812 /* Simple case of a scalar. */
4813 result
= gfc_constant_result (BT_CHARACTER
, kind
, &e
->where
);
4815 return &gfc_bad_expr
;
4817 result
->value
.character
.length
= e
->value
.character
.length
;
4818 result
->value
.character
.string
4819 = gfc_get_wide_string (e
->value
.character
.length
+ 1);
4820 memcpy (result
->value
.character
.string
, e
->value
.character
.string
,
4821 (e
->value
.character
.length
+ 1) * sizeof (gfc_char_t
));
4823 /* Check we only have values representable in the destination kind. */
4824 for (i
= 0; i
< result
->value
.character
.length
; i
++)
4825 if (!gfc_check_character_range (result
->value
.character
.string
[i
],
4828 gfc_error ("Character '%s' in string at %L cannot be converted "
4829 "into character kind %d",
4830 gfc_print_wide_char (result
->value
.character
.string
[i
]),
4832 return &gfc_bad_expr
;
4837 else if (e
->expr_type
== EXPR_ARRAY
)
4839 /* For an array constructor, we convert each constructor element. */
4840 gfc_constructor
*head
= NULL
, *tail
= NULL
, *c
;
4842 for (c
= e
->value
.constructor
; c
; c
= c
->next
)
4845 head
= tail
= gfc_get_constructor ();
4848 tail
->next
= gfc_get_constructor ();
4852 tail
->where
= c
->where
;
4853 tail
->expr
= gfc_convert_char_constant (c
->expr
, type
, kind
);
4854 if (tail
->expr
== &gfc_bad_expr
)
4857 return &gfc_bad_expr
;
4860 if (tail
->expr
== NULL
)
4862 gfc_free_constructor (head
);
4867 result
= gfc_get_expr ();
4868 result
->ts
.type
= type
;
4869 result
->ts
.kind
= kind
;
4870 result
->expr_type
= EXPR_ARRAY
;
4871 result
->value
.constructor
= head
;
4872 result
->shape
= gfc_copy_shape (e
->shape
, e
->rank
);
4873 result
->where
= e
->where
;
4874 result
->rank
= e
->rank
;
4875 result
->ts
.cl
= e
->ts
.cl
;