2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 Free Software Foundation, Inc.
5 Contributed by Andy Vaught
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* Since target arithmetic must be done on the host, there has to
24 be some way of evaluating arithmetic expressions as the host
25 would evaluate them. We use the GNU MP library and the MPFR
26 library to do arithmetic, and this file provides the interface. */
33 #include "target-memory.h"
34 #include "constructor.h"
36 /* MPFR does not have a direct replacement for mpz_set_f() from GMP.
37 It's easily implemented with a few calls though. */
40 gfc_mpfr_to_mpz (mpz_t z
, mpfr_t x
, locus
*where
)
44 if (mpfr_inf_p (x
) || mpfr_nan_p (x
))
46 gfc_error ("Conversion of an Infinity or Not-a-Number at %L "
52 e
= mpfr_get_z_exp (z
, x
);
55 mpz_mul_2exp (z
, z
, e
);
57 mpz_tdiv_q_2exp (z
, z
, -e
);
61 /* Set the model number precision by the requested KIND. */
64 gfc_set_model_kind (int kind
)
66 int index
= gfc_validate_kind (BT_REAL
, kind
, false);
69 base2prec
= gfc_real_kinds
[index
].digits
;
70 if (gfc_real_kinds
[index
].radix
!= 2)
71 base2prec
*= gfc_real_kinds
[index
].radix
/ 2;
72 mpfr_set_default_prec (base2prec
);
76 /* Set the model number precision from mpfr_t x. */
79 gfc_set_model (mpfr_t x
)
81 mpfr_set_default_prec (mpfr_get_prec (x
));
85 /* Given an arithmetic error code, return a pointer to a string that
86 explains the error. */
89 gfc_arith_error (arith code
)
96 p
= _("Arithmetic OK at %L");
99 p
= _("Arithmetic overflow at %L");
101 case ARITH_UNDERFLOW
:
102 p
= _("Arithmetic underflow at %L");
105 p
= _("Arithmetic NaN at %L");
108 p
= _("Division by zero at %L");
110 case ARITH_INCOMMENSURATE
:
111 p
= _("Array operands are incommensurate at %L");
113 case ARITH_ASYMMETRIC
:
115 _("Integer outside symmetric range implied by Standard Fortran at %L");
118 gfc_internal_error ("gfc_arith_error(): Bad error code");
125 /* Get things ready to do math. */
128 gfc_arith_init_1 (void)
130 gfc_integer_info
*int_info
;
131 gfc_real_info
*real_info
;
135 mpfr_set_default_prec (128);
138 /* Convert the minimum and maximum values for each kind into their
139 GNU MP representation. */
140 for (int_info
= gfc_integer_kinds
; int_info
->kind
!= 0; int_info
++)
143 mpz_init (int_info
->huge
);
144 mpz_set_ui (int_info
->huge
, int_info
->radix
);
145 mpz_pow_ui (int_info
->huge
, int_info
->huge
, int_info
->digits
);
146 mpz_sub_ui (int_info
->huge
, int_info
->huge
, 1);
148 /* These are the numbers that are actually representable by the
149 target. For bases other than two, this needs to be changed. */
150 if (int_info
->radix
!= 2)
151 gfc_internal_error ("Fix min_int calculation");
153 /* See PRs 13490 and 17912, related to integer ranges.
154 The pedantic_min_int exists for range checking when a program
155 is compiled with -pedantic, and reflects the belief that
156 Standard Fortran requires integers to be symmetrical, i.e.
157 every negative integer must have a representable positive
158 absolute value, and vice versa. */
160 mpz_init (int_info
->pedantic_min_int
);
161 mpz_neg (int_info
->pedantic_min_int
, int_info
->huge
);
163 mpz_init (int_info
->min_int
);
164 mpz_sub_ui (int_info
->min_int
, int_info
->pedantic_min_int
, 1);
167 mpfr_set_z (a
, int_info
->huge
, GFC_RND_MODE
);
168 mpfr_log10 (a
, a
, GFC_RND_MODE
);
170 int_info
->range
= (int) mpfr_get_si (a
, GFC_RND_MODE
);
175 for (real_info
= gfc_real_kinds
; real_info
->kind
!= 0; real_info
++)
177 gfc_set_model_kind (real_info
->kind
);
182 /* huge(x) = (1 - b**(-p)) * b**(emax-1) * b */
184 mpfr_init (real_info
->huge
);
185 mpfr_set_ui (real_info
->huge
, 1, GFC_RND_MODE
);
186 mpfr_set_ui (a
, real_info
->radix
, GFC_RND_MODE
);
187 mpfr_pow_si (a
, a
, -real_info
->digits
, GFC_RND_MODE
);
188 mpfr_sub (real_info
->huge
, real_info
->huge
, a
, GFC_RND_MODE
);
191 mpfr_set_ui (a
, real_info
->radix
, GFC_RND_MODE
);
192 mpfr_pow_ui (a
, a
, real_info
->max_exponent
- 1, GFC_RND_MODE
);
194 /* (1 - b**(-p)) * b**(emax-1) */
195 mpfr_mul (real_info
->huge
, real_info
->huge
, a
, GFC_RND_MODE
);
197 /* (1 - b**(-p)) * b**(emax-1) * b */
198 mpfr_mul_ui (real_info
->huge
, real_info
->huge
, real_info
->radix
,
201 /* tiny(x) = b**(emin-1) */
202 mpfr_init (real_info
->tiny
);
203 mpfr_set_ui (real_info
->tiny
, real_info
->radix
, GFC_RND_MODE
);
204 mpfr_pow_si (real_info
->tiny
, real_info
->tiny
,
205 real_info
->min_exponent
- 1, GFC_RND_MODE
);
207 /* subnormal (x) = b**(emin - digit) */
208 mpfr_init (real_info
->subnormal
);
209 mpfr_set_ui (real_info
->subnormal
, real_info
->radix
, GFC_RND_MODE
);
210 mpfr_pow_si (real_info
->subnormal
, real_info
->subnormal
,
211 real_info
->min_exponent
- real_info
->digits
, GFC_RND_MODE
);
213 /* epsilon(x) = b**(1-p) */
214 mpfr_init (real_info
->epsilon
);
215 mpfr_set_ui (real_info
->epsilon
, real_info
->radix
, GFC_RND_MODE
);
216 mpfr_pow_si (real_info
->epsilon
, real_info
->epsilon
,
217 1 - real_info
->digits
, GFC_RND_MODE
);
219 /* range(x) = int(min(log10(huge(x)), -log10(tiny)) */
220 mpfr_log10 (a
, real_info
->huge
, GFC_RND_MODE
);
221 mpfr_log10 (b
, real_info
->tiny
, GFC_RND_MODE
);
222 mpfr_neg (b
, b
, GFC_RND_MODE
);
225 mpfr_min (a
, a
, b
, GFC_RND_MODE
);
227 real_info
->range
= (int) mpfr_get_si (a
, GFC_RND_MODE
);
229 /* precision(x) = int((p - 1) * log10(b)) + k */
230 mpfr_set_ui (a
, real_info
->radix
, GFC_RND_MODE
);
231 mpfr_log10 (a
, a
, GFC_RND_MODE
);
232 mpfr_mul_ui (a
, a
, real_info
->digits
- 1, GFC_RND_MODE
);
234 real_info
->precision
= (int) mpfr_get_si (a
, GFC_RND_MODE
);
236 /* If the radix is an integral power of 10, add one to the precision. */
237 for (i
= 10; i
<= real_info
->radix
; i
*= 10)
238 if (i
== real_info
->radix
)
239 real_info
->precision
++;
241 mpfr_clears (a
, b
, NULL
);
246 /* Clean up, get rid of numeric constants. */
249 gfc_arith_done_1 (void)
251 gfc_integer_info
*ip
;
254 for (ip
= gfc_integer_kinds
; ip
->kind
; ip
++)
256 mpz_clear (ip
->min_int
);
257 mpz_clear (ip
->pedantic_min_int
);
258 mpz_clear (ip
->huge
);
261 for (rp
= gfc_real_kinds
; rp
->kind
; rp
++)
262 mpfr_clears (rp
->epsilon
, rp
->huge
, rp
->tiny
, rp
->subnormal
, NULL
);
268 /* Given a wide character value and a character kind, determine whether
269 the character is representable for that kind. */
271 gfc_check_character_range (gfc_char_t c
, int kind
)
273 /* As wide characters are stored as 32-bit values, they're all
274 representable in UCS=4. */
279 return c
<= 255 ? true : false;
285 /* Given an integer and a kind, make sure that the integer lies within
286 the range of the kind. Returns ARITH_OK, ARITH_ASYMMETRIC or
290 gfc_check_integer_range (mpz_t p
, int kind
)
295 i
= gfc_validate_kind (BT_INTEGER
, kind
, false);
300 if (mpz_cmp (p
, gfc_integer_kinds
[i
].pedantic_min_int
) < 0)
301 result
= ARITH_ASYMMETRIC
;
305 if (gfc_option
.flag_range_check
== 0)
308 if (mpz_cmp (p
, gfc_integer_kinds
[i
].min_int
) < 0
309 || mpz_cmp (p
, gfc_integer_kinds
[i
].huge
) > 0)
310 result
= ARITH_OVERFLOW
;
316 /* Given a real and a kind, make sure that the real lies within the
317 range of the kind. Returns ARITH_OK, ARITH_OVERFLOW or
321 gfc_check_real_range (mpfr_t p
, int kind
)
327 i
= gfc_validate_kind (BT_REAL
, kind
, false);
331 mpfr_abs (q
, p
, GFC_RND_MODE
);
337 if (gfc_option
.flag_range_check
!= 0)
338 retval
= ARITH_OVERFLOW
;
340 else if (mpfr_nan_p (p
))
342 if (gfc_option
.flag_range_check
!= 0)
345 else if (mpfr_sgn (q
) == 0)
350 else if (mpfr_cmp (q
, gfc_real_kinds
[i
].huge
) > 0)
352 if (gfc_option
.flag_range_check
== 0)
353 mpfr_set_inf (p
, mpfr_sgn (p
));
355 retval
= ARITH_OVERFLOW
;
357 else if (mpfr_cmp (q
, gfc_real_kinds
[i
].subnormal
) < 0)
359 if (gfc_option
.flag_range_check
== 0)
361 if (mpfr_sgn (p
) < 0)
363 mpfr_set_ui (p
, 0, GFC_RND_MODE
);
364 mpfr_set_si (q
, -1, GFC_RND_MODE
);
365 mpfr_copysign (p
, p
, q
, GFC_RND_MODE
);
368 mpfr_set_ui (p
, 0, GFC_RND_MODE
);
371 retval
= ARITH_UNDERFLOW
;
373 else if (mpfr_cmp (q
, gfc_real_kinds
[i
].tiny
) < 0)
378 /* Save current values of emin and emax. */
379 emin
= mpfr_get_emin ();
380 emax
= mpfr_get_emax ();
382 /* Set emin and emax for the current model number. */
383 en
= gfc_real_kinds
[i
].min_exponent
- gfc_real_kinds
[i
].digits
+ 1;
384 mpfr_set_emin ((mp_exp_t
) en
);
385 mpfr_set_emax ((mp_exp_t
) gfc_real_kinds
[i
].max_exponent
);
386 mpfr_check_range (q
, 0, GFC_RND_MODE
);
387 mpfr_subnormalize (q
, 0, GFC_RND_MODE
);
389 /* Reset emin and emax. */
390 mpfr_set_emin (emin
);
391 mpfr_set_emax (emax
);
393 /* Copy sign if needed. */
394 if (mpfr_sgn (p
) < 0)
395 mpfr_neg (p
, q
, GMP_RNDN
);
397 mpfr_set (p
, q
, GMP_RNDN
);
406 /* Low-level arithmetic functions. All of these subroutines assume
407 that all operands are of the same type and return an operand of the
408 same type. The other thing about these subroutines is that they
409 can fail in various ways -- overflow, underflow, division by zero,
410 zero raised to the zero, etc. */
413 gfc_arith_not (gfc_expr
*op1
, gfc_expr
**resultp
)
417 result
= gfc_get_constant_expr (BT_LOGICAL
, op1
->ts
.kind
, &op1
->where
);
418 result
->value
.logical
= !op1
->value
.logical
;
426 gfc_arith_and (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
430 result
= gfc_get_constant_expr (BT_LOGICAL
, gfc_kind_max (op1
, op2
),
432 result
->value
.logical
= op1
->value
.logical
&& op2
->value
.logical
;
440 gfc_arith_or (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
444 result
= gfc_get_constant_expr (BT_LOGICAL
, gfc_kind_max (op1
, op2
),
446 result
->value
.logical
= op1
->value
.logical
|| op2
->value
.logical
;
454 gfc_arith_eqv (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
458 result
= gfc_get_constant_expr (BT_LOGICAL
, gfc_kind_max (op1
, op2
),
460 result
->value
.logical
= op1
->value
.logical
== op2
->value
.logical
;
468 gfc_arith_neqv (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
472 result
= gfc_get_constant_expr (BT_LOGICAL
, gfc_kind_max (op1
, op2
),
474 result
->value
.logical
= op1
->value
.logical
!= op2
->value
.logical
;
481 /* Make sure a constant numeric expression is within the range for
482 its type and kind. Note that there's also a gfc_check_range(),
483 but that one deals with the intrinsic RANGE function. */
486 gfc_range_check (gfc_expr
*e
)
494 rc
= gfc_check_integer_range (e
->value
.integer
, e
->ts
.kind
);
498 rc
= gfc_check_real_range (e
->value
.real
, e
->ts
.kind
);
499 if (rc
== ARITH_UNDERFLOW
)
500 mpfr_set_ui (e
->value
.real
, 0, GFC_RND_MODE
);
501 if (rc
== ARITH_OVERFLOW
)
502 mpfr_set_inf (e
->value
.real
, mpfr_sgn (e
->value
.real
));
504 mpfr_set_nan (e
->value
.real
);
508 rc
= gfc_check_real_range (mpc_realref (e
->value
.complex), e
->ts
.kind
);
509 if (rc
== ARITH_UNDERFLOW
)
510 mpfr_set_ui (mpc_realref (e
->value
.complex), 0, GFC_RND_MODE
);
511 if (rc
== ARITH_OVERFLOW
)
512 mpfr_set_inf (mpc_realref (e
->value
.complex),
513 mpfr_sgn (mpc_realref (e
->value
.complex)));
515 mpfr_set_nan (mpc_realref (e
->value
.complex));
517 rc2
= gfc_check_real_range (mpc_imagref (e
->value
.complex), e
->ts
.kind
);
518 if (rc
== ARITH_UNDERFLOW
)
519 mpfr_set_ui (mpc_imagref (e
->value
.complex), 0, GFC_RND_MODE
);
520 if (rc
== ARITH_OVERFLOW
)
521 mpfr_set_inf (mpc_imagref (e
->value
.complex),
522 mpfr_sgn (mpc_imagref (e
->value
.complex)));
524 mpfr_set_nan (mpc_imagref (e
->value
.complex));
531 gfc_internal_error ("gfc_range_check(): Bad type");
538 /* Several of the following routines use the same set of statements to
539 check the validity of the result. Encapsulate the checking here. */
542 check_result (arith rc
, gfc_expr
*x
, gfc_expr
*r
, gfc_expr
**rp
)
546 if (val
== ARITH_UNDERFLOW
)
548 if (gfc_option
.warn_underflow
)
549 gfc_warning (gfc_arith_error (val
), &x
->where
);
553 if (val
== ARITH_ASYMMETRIC
)
555 gfc_warning (gfc_arith_error (val
), &x
->where
);
568 /* It may seem silly to have a subroutine that actually computes the
569 unary plus of a constant, but it prevents us from making exceptions
570 in the code elsewhere. Used for unary plus and parenthesized
574 gfc_arith_identity (gfc_expr
*op1
, gfc_expr
**resultp
)
576 *resultp
= gfc_copy_expr (op1
);
582 gfc_arith_uminus (gfc_expr
*op1
, gfc_expr
**resultp
)
587 result
= gfc_get_constant_expr (op1
->ts
.type
, op1
->ts
.kind
, &op1
->where
);
589 switch (op1
->ts
.type
)
592 mpz_neg (result
->value
.integer
, op1
->value
.integer
);
596 mpfr_neg (result
->value
.real
, op1
->value
.real
, GFC_RND_MODE
);
600 mpc_neg (result
->value
.complex, op1
->value
.complex, GFC_MPC_RND_MODE
);
604 gfc_internal_error ("gfc_arith_uminus(): Bad basic type");
607 rc
= gfc_range_check (result
);
609 return check_result (rc
, op1
, result
, resultp
);
614 gfc_arith_plus (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
619 result
= gfc_get_constant_expr (op1
->ts
.type
, op1
->ts
.kind
, &op1
->where
);
621 switch (op1
->ts
.type
)
624 mpz_add (result
->value
.integer
, op1
->value
.integer
, op2
->value
.integer
);
628 mpfr_add (result
->value
.real
, op1
->value
.real
, op2
->value
.real
,
633 mpc_add (result
->value
.complex, op1
->value
.complex, op2
->value
.complex,
638 gfc_internal_error ("gfc_arith_plus(): Bad basic type");
641 rc
= gfc_range_check (result
);
643 return check_result (rc
, op1
, result
, resultp
);
648 gfc_arith_minus (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
653 result
= gfc_get_constant_expr (op1
->ts
.type
, op1
->ts
.kind
, &op1
->where
);
655 switch (op1
->ts
.type
)
658 mpz_sub (result
->value
.integer
, op1
->value
.integer
, op2
->value
.integer
);
662 mpfr_sub (result
->value
.real
, op1
->value
.real
, op2
->value
.real
,
667 mpc_sub (result
->value
.complex, op1
->value
.complex,
668 op2
->value
.complex, GFC_MPC_RND_MODE
);
672 gfc_internal_error ("gfc_arith_minus(): Bad basic type");
675 rc
= gfc_range_check (result
);
677 return check_result (rc
, op1
, result
, resultp
);
682 gfc_arith_times (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
687 result
= gfc_get_constant_expr (op1
->ts
.type
, op1
->ts
.kind
, &op1
->where
);
689 switch (op1
->ts
.type
)
692 mpz_mul (result
->value
.integer
, op1
->value
.integer
, op2
->value
.integer
);
696 mpfr_mul (result
->value
.real
, op1
->value
.real
, op2
->value
.real
,
701 gfc_set_model (mpc_realref (op1
->value
.complex));
702 mpc_mul (result
->value
.complex, op1
->value
.complex, op2
->value
.complex,
707 gfc_internal_error ("gfc_arith_times(): Bad basic type");
710 rc
= gfc_range_check (result
);
712 return check_result (rc
, op1
, result
, resultp
);
717 gfc_arith_divide (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
724 result
= gfc_get_constant_expr (op1
->ts
.type
, op1
->ts
.kind
, &op1
->where
);
726 switch (op1
->ts
.type
)
729 if (mpz_sgn (op2
->value
.integer
) == 0)
735 mpz_tdiv_q (result
->value
.integer
, op1
->value
.integer
,
740 if (mpfr_sgn (op2
->value
.real
) == 0 && gfc_option
.flag_range_check
== 1)
746 mpfr_div (result
->value
.real
, op1
->value
.real
, op2
->value
.real
,
751 if (mpc_cmp_si_si (op2
->value
.complex, 0, 0) == 0
752 && gfc_option
.flag_range_check
== 1)
758 gfc_set_model (mpc_realref (op1
->value
.complex));
759 if (mpc_cmp_si_si (op2
->value
.complex, 0, 0) == 0)
761 /* In Fortran, return (NaN + NaN I) for any zero divisor. See
763 mpfr_set_nan (mpc_realref (result
->value
.complex));
764 mpfr_set_nan (mpc_imagref (result
->value
.complex));
767 mpc_div (result
->value
.complex, op1
->value
.complex, op2
->value
.complex,
772 gfc_internal_error ("gfc_arith_divide(): Bad basic type");
776 rc
= gfc_range_check (result
);
778 return check_result (rc
, op1
, result
, resultp
);
781 /* Raise a number to a power. */
784 arith_power (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
791 result
= gfc_get_constant_expr (op1
->ts
.type
, op1
->ts
.kind
, &op1
->where
);
793 switch (op2
->ts
.type
)
796 power_sign
= mpz_sgn (op2
->value
.integer
);
800 /* Handle something to the zeroth power. Since we're dealing
801 with integral exponents, there is no ambiguity in the
802 limiting procedure used to determine the value of 0**0. */
803 switch (op1
->ts
.type
)
806 mpz_set_ui (result
->value
.integer
, 1);
810 mpfr_set_ui (result
->value
.real
, 1, GFC_RND_MODE
);
814 mpc_set_ui (result
->value
.complex, 1, GFC_MPC_RND_MODE
);
818 gfc_internal_error ("arith_power(): Bad base");
823 switch (op1
->ts
.type
)
829 /* First, we simplify the cases of op1 == 1, 0 or -1. */
830 if (mpz_cmp_si (op1
->value
.integer
, 1) == 0)
833 mpz_set_si (result
->value
.integer
, 1);
835 else if (mpz_cmp_si (op1
->value
.integer
, 0) == 0)
837 /* 0**op2 == 0, if op2 > 0
838 0**op2 overflow, if op2 < 0 ; in that case, we
839 set the result to 0 and return ARITH_DIV0. */
840 mpz_set_si (result
->value
.integer
, 0);
841 if (mpz_cmp_si (op2
->value
.integer
, 0) < 0)
844 else if (mpz_cmp_si (op1
->value
.integer
, -1) == 0)
846 /* (-1)**op2 == (-1)**(mod(op2,2)) */
847 unsigned int odd
= mpz_fdiv_ui (op2
->value
.integer
, 2);
849 mpz_set_si (result
->value
.integer
, -1);
851 mpz_set_si (result
->value
.integer
, 1);
853 /* Then, we take care of op2 < 0. */
854 else if (mpz_cmp_si (op2
->value
.integer
, 0) < 0)
856 /* if op2 < 0, op1**op2 == 0 because abs(op1) > 1. */
857 mpz_set_si (result
->value
.integer
, 0);
859 else if (gfc_extract_int (op2
, &power
) != NULL
)
861 /* If op2 doesn't fit in an int, the exponentiation will
862 overflow, because op2 > 0 and abs(op1) > 1. */
865 i
= gfc_validate_kind (BT_INTEGER
, result
->ts
.kind
, false);
867 if (gfc_option
.flag_range_check
)
870 /* Still, we want to give the same value as the
873 mpz_add_ui (max
, gfc_integer_kinds
[i
].huge
, 1);
874 mpz_mul_ui (max
, max
, 2);
875 mpz_powm (result
->value
.integer
, op1
->value
.integer
,
876 op2
->value
.integer
, max
);
880 mpz_pow_ui (result
->value
.integer
, op1
->value
.integer
,
886 mpfr_pow_z (result
->value
.real
, op1
->value
.real
,
887 op2
->value
.integer
, GFC_RND_MODE
);
891 mpc_pow_z (result
->value
.complex, op1
->value
.complex,
892 op2
->value
.integer
, GFC_MPC_RND_MODE
);
903 if (gfc_init_expr_flag
)
905 if (gfc_notify_std (GFC_STD_F2003
,"Fortran 2003: Noninteger "
906 "exponent in an initialization "
907 "expression at %L", &op2
->where
) == FAILURE
)
908 return ARITH_PROHIBIT
;
911 if (mpfr_cmp_si (op1
->value
.real
, 0) < 0)
913 gfc_error ("Raising a negative REAL at %L to "
914 "a REAL power is prohibited", &op1
->where
);
916 return ARITH_PROHIBIT
;
919 mpfr_pow (result
->value
.real
, op1
->value
.real
, op2
->value
.real
,
925 if (gfc_init_expr_flag
)
927 if (gfc_notify_std (GFC_STD_F2003
,"Fortran 2003: Noninteger "
928 "exponent in an initialization "
929 "expression at %L", &op2
->where
) == FAILURE
)
930 return ARITH_PROHIBIT
;
933 mpc_pow (result
->value
.complex, op1
->value
.complex,
934 op2
->value
.complex, GFC_MPC_RND_MODE
);
938 gfc_internal_error ("arith_power(): unknown type");
942 rc
= gfc_range_check (result
);
944 return check_result (rc
, op1
, result
, resultp
);
948 /* Concatenate two string constants. */
951 gfc_arith_concat (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
956 gcc_assert (op1
->ts
.kind
== op2
->ts
.kind
);
957 result
= gfc_get_constant_expr (BT_CHARACTER
, op1
->ts
.kind
,
960 len
= op1
->value
.character
.length
+ op2
->value
.character
.length
;
962 result
->value
.character
.string
= gfc_get_wide_string (len
+ 1);
963 result
->value
.character
.length
= len
;
965 memcpy (result
->value
.character
.string
, op1
->value
.character
.string
,
966 op1
->value
.character
.length
* sizeof (gfc_char_t
));
968 memcpy (&result
->value
.character
.string
[op1
->value
.character
.length
],
969 op2
->value
.character
.string
,
970 op2
->value
.character
.length
* sizeof (gfc_char_t
));
972 result
->value
.character
.string
[len
] = '\0';
979 /* Comparison between real values; returns 0 if (op1 .op. op2) is true.
980 This function mimics mpfr_cmp but takes NaN into account. */
983 compare_real (gfc_expr
*op1
, gfc_expr
*op2
, gfc_intrinsic_op op
)
989 rc
= mpfr_equal_p (op1
->value
.real
, op2
->value
.real
) ? 0 : 1;
992 rc
= mpfr_greater_p (op1
->value
.real
, op2
->value
.real
) ? 1 : -1;
995 rc
= mpfr_greaterequal_p (op1
->value
.real
, op2
->value
.real
) ? 1 : -1;
998 rc
= mpfr_less_p (op1
->value
.real
, op2
->value
.real
) ? -1 : 1;
1001 rc
= mpfr_lessequal_p (op1
->value
.real
, op2
->value
.real
) ? -1 : 1;
1004 gfc_internal_error ("compare_real(): Bad operator");
1010 /* Comparison operators. Assumes that the two expression nodes
1011 contain two constants of the same type. The op argument is
1012 needed to handle NaN correctly. */
1015 gfc_compare_expr (gfc_expr
*op1
, gfc_expr
*op2
, gfc_intrinsic_op op
)
1019 switch (op1
->ts
.type
)
1022 rc
= mpz_cmp (op1
->value
.integer
, op2
->value
.integer
);
1026 rc
= compare_real (op1
, op2
, op
);
1030 rc
= gfc_compare_string (op1
, op2
);
1034 rc
= ((!op1
->value
.logical
&& op2
->value
.logical
)
1035 || (op1
->value
.logical
&& !op2
->value
.logical
));
1039 gfc_internal_error ("gfc_compare_expr(): Bad basic type");
1046 /* Compare a pair of complex numbers. Naturally, this is only for
1047 equality and inequality. */
1050 compare_complex (gfc_expr
*op1
, gfc_expr
*op2
)
1052 return mpc_cmp (op1
->value
.complex, op2
->value
.complex) == 0;
1056 /* Given two constant strings and the inverse collating sequence, compare the
1057 strings. We return -1 for a < b, 0 for a == b and 1 for a > b.
1058 We use the processor's default collating sequence. */
1061 gfc_compare_string (gfc_expr
*a
, gfc_expr
*b
)
1063 int len
, alen
, blen
, i
;
1066 alen
= a
->value
.character
.length
;
1067 blen
= b
->value
.character
.length
;
1069 len
= MAX(alen
, blen
);
1071 for (i
= 0; i
< len
; i
++)
1073 ac
= ((i
< alen
) ? a
->value
.character
.string
[i
] : ' ');
1074 bc
= ((i
< blen
) ? b
->value
.character
.string
[i
] : ' ');
1082 /* Strings are equal */
1088 gfc_compare_with_Cstring (gfc_expr
*a
, const char *b
, bool case_sensitive
)
1090 int len
, alen
, blen
, i
;
1093 alen
= a
->value
.character
.length
;
1096 len
= MAX(alen
, blen
);
1098 for (i
= 0; i
< len
; i
++)
1100 ac
= ((i
< alen
) ? a
->value
.character
.string
[i
] : ' ');
1101 bc
= ((i
< blen
) ? b
[i
] : ' ');
1103 if (!case_sensitive
)
1115 /* Strings are equal */
1120 /* Specific comparison subroutines. */
1123 gfc_arith_eq (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
1127 result
= gfc_get_constant_expr (BT_LOGICAL
, gfc_default_logical_kind
,
1129 result
->value
.logical
= (op1
->ts
.type
== BT_COMPLEX
)
1130 ? compare_complex (op1
, op2
)
1131 : (gfc_compare_expr (op1
, op2
, INTRINSIC_EQ
) == 0);
1139 gfc_arith_ne (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
1143 result
= gfc_get_constant_expr (BT_LOGICAL
, gfc_default_logical_kind
,
1145 result
->value
.logical
= (op1
->ts
.type
== BT_COMPLEX
)
1146 ? !compare_complex (op1
, op2
)
1147 : (gfc_compare_expr (op1
, op2
, INTRINSIC_EQ
) != 0);
1155 gfc_arith_gt (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
1159 result
= gfc_get_constant_expr (BT_LOGICAL
, gfc_default_logical_kind
,
1161 result
->value
.logical
= (gfc_compare_expr (op1
, op2
, INTRINSIC_GT
) > 0);
1169 gfc_arith_ge (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
1173 result
= gfc_get_constant_expr (BT_LOGICAL
, gfc_default_logical_kind
,
1175 result
->value
.logical
= (gfc_compare_expr (op1
, op2
, INTRINSIC_GE
) >= 0);
1183 gfc_arith_lt (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
1187 result
= gfc_get_constant_expr (BT_LOGICAL
, gfc_default_logical_kind
,
1189 result
->value
.logical
= (gfc_compare_expr (op1
, op2
, INTRINSIC_LT
) < 0);
1197 gfc_arith_le (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
1201 result
= gfc_get_constant_expr (BT_LOGICAL
, gfc_default_logical_kind
,
1203 result
->value
.logical
= (gfc_compare_expr (op1
, op2
, INTRINSIC_LE
) <= 0);
1211 reduce_unary (arith (*eval
) (gfc_expr
*, gfc_expr
**), gfc_expr
*op
,
1214 gfc_constructor_base head
;
1219 if (op
->expr_type
== EXPR_CONSTANT
)
1220 return eval (op
, result
);
1223 head
= gfc_constructor_copy (op
->value
.constructor
);
1224 for (c
= gfc_constructor_first (head
); c
; c
= gfc_constructor_next (c
))
1226 rc
= reduce_unary (eval
, c
->expr
, &r
);
1231 gfc_replace_expr (c
->expr
, r
);
1235 gfc_constructor_free (head
);
1238 gfc_constructor
*c
= gfc_constructor_first (head
);
1239 r
= gfc_get_array_expr (c
->expr
->ts
.type
, c
->expr
->ts
.kind
,
1241 r
->shape
= gfc_copy_shape (op
->shape
, op
->rank
);
1243 r
->value
.constructor
= head
;
1252 reduce_binary_ac (arith (*eval
) (gfc_expr
*, gfc_expr
*, gfc_expr
**),
1253 gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**result
)
1255 gfc_constructor_base head
;
1258 arith rc
= ARITH_OK
;
1260 head
= gfc_constructor_copy (op1
->value
.constructor
);
1261 for (c
= gfc_constructor_first (head
); c
; c
= gfc_constructor_next (c
))
1263 if (c
->expr
->expr_type
== EXPR_CONSTANT
)
1264 rc
= eval (c
->expr
, op2
, &r
);
1266 rc
= reduce_binary_ac (eval
, c
->expr
, op2
, &r
);
1271 gfc_replace_expr (c
->expr
, r
);
1275 gfc_constructor_free (head
);
1278 gfc_constructor
*c
= gfc_constructor_first (head
);
1279 r
= gfc_get_array_expr (c
->expr
->ts
.type
, c
->expr
->ts
.kind
,
1281 r
->shape
= gfc_copy_shape (op1
->shape
, op1
->rank
);
1282 r
->rank
= op1
->rank
;
1283 r
->value
.constructor
= head
;
1292 reduce_binary_ca (arith (*eval
) (gfc_expr
*, gfc_expr
*, gfc_expr
**),
1293 gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**result
)
1295 gfc_constructor_base head
;
1298 arith rc
= ARITH_OK
;
1300 head
= gfc_constructor_copy (op2
->value
.constructor
);
1301 for (c
= gfc_constructor_first (head
); c
; c
= gfc_constructor_next (c
))
1303 if (c
->expr
->expr_type
== EXPR_CONSTANT
)
1304 rc
= eval (op1
, c
->expr
, &r
);
1306 rc
= reduce_binary_ca (eval
, op1
, c
->expr
, &r
);
1311 gfc_replace_expr (c
->expr
, r
);
1315 gfc_constructor_free (head
);
1318 gfc_constructor
*c
= gfc_constructor_first (head
);
1319 r
= gfc_get_array_expr (c
->expr
->ts
.type
, c
->expr
->ts
.kind
,
1321 r
->shape
= gfc_copy_shape (op2
->shape
, op2
->rank
);
1322 r
->rank
= op2
->rank
;
1323 r
->value
.constructor
= head
;
1331 /* We need a forward declaration of reduce_binary. */
1332 static arith
reduce_binary (arith (*eval
) (gfc_expr
*, gfc_expr
*, gfc_expr
**),
1333 gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**result
);
1337 reduce_binary_aa (arith (*eval
) (gfc_expr
*, gfc_expr
*, gfc_expr
**),
1338 gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**result
)
1340 gfc_constructor_base head
;
1341 gfc_constructor
*c
, *d
;
1343 arith rc
= ARITH_OK
;
1345 if (gfc_check_conformance (op1
, op2
,
1346 "elemental binary operation") != SUCCESS
)
1347 return ARITH_INCOMMENSURATE
;
1349 head
= gfc_constructor_copy (op1
->value
.constructor
);
1350 for (c
= gfc_constructor_first (head
),
1351 d
= gfc_constructor_first (op2
->value
.constructor
);
1353 c
= gfc_constructor_next (c
), d
= gfc_constructor_next (d
))
1355 rc
= reduce_binary (eval
, c
->expr
, d
->expr
, &r
);
1359 gfc_replace_expr (c
->expr
, r
);
1363 rc
= ARITH_INCOMMENSURATE
;
1366 gfc_constructor_free (head
);
1369 gfc_constructor
*c
= gfc_constructor_first (head
);
1370 r
= gfc_get_array_expr (c
->expr
->ts
.type
, c
->expr
->ts
.kind
,
1372 r
->shape
= gfc_copy_shape (op1
->shape
, op1
->rank
);
1373 r
->rank
= op1
->rank
;
1374 r
->value
.constructor
= head
;
1383 reduce_binary (arith (*eval
) (gfc_expr
*, gfc_expr
*, gfc_expr
**),
1384 gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**result
)
1386 if (op1
->expr_type
== EXPR_CONSTANT
&& op2
->expr_type
== EXPR_CONSTANT
)
1387 return eval (op1
, op2
, result
);
1389 if (op1
->expr_type
== EXPR_CONSTANT
&& op2
->expr_type
== EXPR_ARRAY
)
1390 return reduce_binary_ca (eval
, op1
, op2
, result
);
1392 if (op1
->expr_type
== EXPR_ARRAY
&& op2
->expr_type
== EXPR_CONSTANT
)
1393 return reduce_binary_ac (eval
, op1
, op2
, result
);
1395 return reduce_binary_aa (eval
, op1
, op2
, result
);
1401 arith (*f2
)(gfc_expr
*, gfc_expr
**);
1402 arith (*f3
)(gfc_expr
*, gfc_expr
*, gfc_expr
**);
1406 /* High level arithmetic subroutines. These subroutines go into
1407 eval_intrinsic(), which can do one of several things to its
1408 operands. If the operands are incompatible with the intrinsic
1409 operation, we return a node pointing to the operands and hope that
1410 an operator interface is found during resolution.
1412 If the operands are compatible and are constants, then we try doing
1413 the arithmetic. We also handle the cases where either or both
1414 operands are array constructors. */
1417 eval_intrinsic (gfc_intrinsic_op op
,
1418 eval_f eval
, gfc_expr
*op1
, gfc_expr
*op2
)
1420 gfc_expr temp
, *result
;
1424 gfc_clear_ts (&temp
.ts
);
1430 if (op1
->ts
.type
!= BT_LOGICAL
)
1433 temp
.ts
.type
= BT_LOGICAL
;
1434 temp
.ts
.kind
= gfc_default_logical_kind
;
1438 /* Logical binary operators */
1441 case INTRINSIC_NEQV
:
1443 if (op1
->ts
.type
!= BT_LOGICAL
|| op2
->ts
.type
!= BT_LOGICAL
)
1446 temp
.ts
.type
= BT_LOGICAL
;
1447 temp
.ts
.kind
= gfc_default_logical_kind
;
1452 case INTRINSIC_UPLUS
:
1453 case INTRINSIC_UMINUS
:
1454 if (!gfc_numeric_ts (&op1
->ts
))
1461 case INTRINSIC_PARENTHESES
:
1466 /* Additional restrictions for ordering relations. */
1468 case INTRINSIC_GE_OS
:
1470 case INTRINSIC_LT_OS
:
1472 case INTRINSIC_LE_OS
:
1474 case INTRINSIC_GT_OS
:
1475 if (op1
->ts
.type
== BT_COMPLEX
|| op2
->ts
.type
== BT_COMPLEX
)
1477 temp
.ts
.type
= BT_LOGICAL
;
1478 temp
.ts
.kind
= gfc_default_logical_kind
;
1484 case INTRINSIC_EQ_OS
:
1486 case INTRINSIC_NE_OS
:
1487 if (op1
->ts
.type
== BT_CHARACTER
&& op2
->ts
.type
== BT_CHARACTER
)
1490 temp
.ts
.type
= BT_LOGICAL
;
1491 temp
.ts
.kind
= gfc_default_logical_kind
;
1493 /* If kind mismatch, exit and we'll error out later. */
1494 if (op1
->ts
.kind
!= op2
->ts
.kind
)
1501 /* Numeric binary */
1502 case INTRINSIC_PLUS
:
1503 case INTRINSIC_MINUS
:
1504 case INTRINSIC_TIMES
:
1505 case INTRINSIC_DIVIDE
:
1506 case INTRINSIC_POWER
:
1507 if (!gfc_numeric_ts (&op1
->ts
) || !gfc_numeric_ts (&op2
->ts
))
1510 /* Insert any necessary type conversions to make the operands
1513 temp
.expr_type
= EXPR_OP
;
1514 gfc_clear_ts (&temp
.ts
);
1515 temp
.value
.op
.op
= op
;
1517 temp
.value
.op
.op1
= op1
;
1518 temp
.value
.op
.op2
= op2
;
1520 gfc_type_convert_binary (&temp
, 0);
1522 if (op
== INTRINSIC_EQ
|| op
== INTRINSIC_NE
1523 || op
== INTRINSIC_GE
|| op
== INTRINSIC_GT
1524 || op
== INTRINSIC_LE
|| op
== INTRINSIC_LT
1525 || op
== INTRINSIC_EQ_OS
|| op
== INTRINSIC_NE_OS
1526 || op
== INTRINSIC_GE_OS
|| op
== INTRINSIC_GT_OS
1527 || op
== INTRINSIC_LE_OS
|| op
== INTRINSIC_LT_OS
)
1529 temp
.ts
.type
= BT_LOGICAL
;
1530 temp
.ts
.kind
= gfc_default_logical_kind
;
1536 /* Character binary */
1537 case INTRINSIC_CONCAT
:
1538 if (op1
->ts
.type
!= BT_CHARACTER
|| op2
->ts
.type
!= BT_CHARACTER
1539 || op1
->ts
.kind
!= op2
->ts
.kind
)
1542 temp
.ts
.type
= BT_CHARACTER
;
1543 temp
.ts
.kind
= op1
->ts
.kind
;
1547 case INTRINSIC_USER
:
1551 gfc_internal_error ("eval_intrinsic(): Bad operator");
1554 if (op1
->expr_type
!= EXPR_CONSTANT
1555 && (op1
->expr_type
!= EXPR_ARRAY
1556 || !gfc_is_constant_expr (op1
) || !gfc_expanded_ac (op1
)))
1560 && op2
->expr_type
!= EXPR_CONSTANT
1561 && (op2
->expr_type
!= EXPR_ARRAY
1562 || !gfc_is_constant_expr (op2
) || !gfc_expanded_ac (op2
)))
1566 rc
= reduce_unary (eval
.f2
, op1
, &result
);
1568 rc
= reduce_binary (eval
.f3
, op1
, op2
, &result
);
1571 /* Something went wrong. */
1572 if (op
== INTRINSIC_POWER
&& rc
== ARITH_PROHIBIT
)
1577 gfc_error (gfc_arith_error (rc
), &op1
->where
);
1581 gfc_free_expr (op1
);
1582 gfc_free_expr (op2
);
1586 /* Create a run-time expression. */
1587 result
= gfc_get_operator_expr (&op1
->where
, op
, op1
, op2
);
1588 result
->ts
= temp
.ts
;
1594 /* Modify type of expression for zero size array. */
1597 eval_type_intrinsic0 (gfc_intrinsic_op iop
, gfc_expr
*op
)
1600 gfc_internal_error ("eval_type_intrinsic0(): op NULL");
1605 case INTRINSIC_GE_OS
:
1607 case INTRINSIC_LT_OS
:
1609 case INTRINSIC_LE_OS
:
1611 case INTRINSIC_GT_OS
:
1613 case INTRINSIC_EQ_OS
:
1615 case INTRINSIC_NE_OS
:
1616 op
->ts
.type
= BT_LOGICAL
;
1617 op
->ts
.kind
= gfc_default_logical_kind
;
1628 /* Return nonzero if the expression is a zero size array. */
1631 gfc_zero_size_array (gfc_expr
*e
)
1633 if (e
->expr_type
!= EXPR_ARRAY
)
1636 return e
->value
.constructor
== NULL
;
1640 /* Reduce a binary expression where at least one of the operands
1641 involves a zero-length array. Returns NULL if neither of the
1642 operands is a zero-length array. */
1645 reduce_binary0 (gfc_expr
*op1
, gfc_expr
*op2
)
1647 if (gfc_zero_size_array (op1
))
1649 gfc_free_expr (op2
);
1653 if (gfc_zero_size_array (op2
))
1655 gfc_free_expr (op1
);
1664 eval_intrinsic_f2 (gfc_intrinsic_op op
,
1665 arith (*eval
) (gfc_expr
*, gfc_expr
**),
1666 gfc_expr
*op1
, gfc_expr
*op2
)
1673 if (gfc_zero_size_array (op1
))
1674 return eval_type_intrinsic0 (op
, op1
);
1678 result
= reduce_binary0 (op1
, op2
);
1680 return eval_type_intrinsic0 (op
, result
);
1684 return eval_intrinsic (op
, f
, op1
, op2
);
1689 eval_intrinsic_f3 (gfc_intrinsic_op op
,
1690 arith (*eval
) (gfc_expr
*, gfc_expr
*, gfc_expr
**),
1691 gfc_expr
*op1
, gfc_expr
*op2
)
1696 result
= reduce_binary0 (op1
, op2
);
1698 return eval_type_intrinsic0(op
, result
);
1701 return eval_intrinsic (op
, f
, op1
, op2
);
1706 gfc_parentheses (gfc_expr
*op
)
1708 if (gfc_is_constant_expr (op
))
1711 return eval_intrinsic_f2 (INTRINSIC_PARENTHESES
, gfc_arith_identity
,
1716 gfc_uplus (gfc_expr
*op
)
1718 return eval_intrinsic_f2 (INTRINSIC_UPLUS
, gfc_arith_identity
, op
, NULL
);
1723 gfc_uminus (gfc_expr
*op
)
1725 return eval_intrinsic_f2 (INTRINSIC_UMINUS
, gfc_arith_uminus
, op
, NULL
);
1730 gfc_add (gfc_expr
*op1
, gfc_expr
*op2
)
1732 return eval_intrinsic_f3 (INTRINSIC_PLUS
, gfc_arith_plus
, op1
, op2
);
1737 gfc_subtract (gfc_expr
*op1
, gfc_expr
*op2
)
1739 return eval_intrinsic_f3 (INTRINSIC_MINUS
, gfc_arith_minus
, op1
, op2
);
1744 gfc_multiply (gfc_expr
*op1
, gfc_expr
*op2
)
1746 return eval_intrinsic_f3 (INTRINSIC_TIMES
, gfc_arith_times
, op1
, op2
);
1751 gfc_divide (gfc_expr
*op1
, gfc_expr
*op2
)
1753 return eval_intrinsic_f3 (INTRINSIC_DIVIDE
, gfc_arith_divide
, op1
, op2
);
1758 gfc_power (gfc_expr
*op1
, gfc_expr
*op2
)
1760 return eval_intrinsic_f3 (INTRINSIC_POWER
, arith_power
, op1
, op2
);
1765 gfc_concat (gfc_expr
*op1
, gfc_expr
*op2
)
1767 return eval_intrinsic_f3 (INTRINSIC_CONCAT
, gfc_arith_concat
, op1
, op2
);
1772 gfc_and (gfc_expr
*op1
, gfc_expr
*op2
)
1774 return eval_intrinsic_f3 (INTRINSIC_AND
, gfc_arith_and
, op1
, op2
);
1779 gfc_or (gfc_expr
*op1
, gfc_expr
*op2
)
1781 return eval_intrinsic_f3 (INTRINSIC_OR
, gfc_arith_or
, op1
, op2
);
1786 gfc_not (gfc_expr
*op1
)
1788 return eval_intrinsic_f2 (INTRINSIC_NOT
, gfc_arith_not
, op1
, NULL
);
1793 gfc_eqv (gfc_expr
*op1
, gfc_expr
*op2
)
1795 return eval_intrinsic_f3 (INTRINSIC_EQV
, gfc_arith_eqv
, op1
, op2
);
1800 gfc_neqv (gfc_expr
*op1
, gfc_expr
*op2
)
1802 return eval_intrinsic_f3 (INTRINSIC_NEQV
, gfc_arith_neqv
, op1
, op2
);
1807 gfc_eq (gfc_expr
*op1
, gfc_expr
*op2
, gfc_intrinsic_op op
)
1809 return eval_intrinsic_f3 (op
, gfc_arith_eq
, op1
, op2
);
1814 gfc_ne (gfc_expr
*op1
, gfc_expr
*op2
, gfc_intrinsic_op op
)
1816 return eval_intrinsic_f3 (op
, gfc_arith_ne
, op1
, op2
);
1821 gfc_gt (gfc_expr
*op1
, gfc_expr
*op2
, gfc_intrinsic_op op
)
1823 return eval_intrinsic_f3 (op
, gfc_arith_gt
, op1
, op2
);
1828 gfc_ge (gfc_expr
*op1
, gfc_expr
*op2
, gfc_intrinsic_op op
)
1830 return eval_intrinsic_f3 (op
, gfc_arith_ge
, op1
, op2
);
1835 gfc_lt (gfc_expr
*op1
, gfc_expr
*op2
, gfc_intrinsic_op op
)
1837 return eval_intrinsic_f3 (op
, gfc_arith_lt
, op1
, op2
);
1842 gfc_le (gfc_expr
*op1
, gfc_expr
*op2
, gfc_intrinsic_op op
)
1844 return eval_intrinsic_f3 (op
, gfc_arith_le
, op1
, op2
);
1848 /* Convert an integer string to an expression node. */
1851 gfc_convert_integer (const char *buffer
, int kind
, int radix
, locus
*where
)
1856 e
= gfc_get_constant_expr (BT_INTEGER
, kind
, where
);
1857 /* A leading plus is allowed, but not by mpz_set_str. */
1858 if (buffer
[0] == '+')
1862 mpz_set_str (e
->value
.integer
, t
, radix
);
1868 /* Convert a real string to an expression node. */
1871 gfc_convert_real (const char *buffer
, int kind
, locus
*where
)
1875 e
= gfc_get_constant_expr (BT_REAL
, kind
, where
);
1876 mpfr_set_str (e
->value
.real
, buffer
, 10, GFC_RND_MODE
);
1882 /* Convert a pair of real, constant expression nodes to a single
1883 complex expression node. */
1886 gfc_convert_complex (gfc_expr
*real
, gfc_expr
*imag
, int kind
)
1890 e
= gfc_get_constant_expr (BT_COMPLEX
, kind
, &real
->where
);
1891 mpc_set_fr_fr (e
->value
.complex, real
->value
.real
, imag
->value
.real
,
1898 /******* Simplification of intrinsic functions with constant arguments *****/
1901 /* Deal with an arithmetic error. */
1904 arith_error (arith rc
, gfc_typespec
*from
, gfc_typespec
*to
, locus
*where
)
1909 gfc_error ("Arithmetic OK converting %s to %s at %L",
1910 gfc_typename (from
), gfc_typename (to
), where
);
1912 case ARITH_OVERFLOW
:
1913 gfc_error ("Arithmetic overflow converting %s to %s at %L. This check "
1914 "can be disabled with the option -fno-range-check",
1915 gfc_typename (from
), gfc_typename (to
), where
);
1917 case ARITH_UNDERFLOW
:
1918 gfc_error ("Arithmetic underflow converting %s to %s at %L. This check "
1919 "can be disabled with the option -fno-range-check",
1920 gfc_typename (from
), gfc_typename (to
), where
);
1923 gfc_error ("Arithmetic NaN converting %s to %s at %L. This check "
1924 "can be disabled with the option -fno-range-check",
1925 gfc_typename (from
), gfc_typename (to
), where
);
1928 gfc_error ("Division by zero converting %s to %s at %L",
1929 gfc_typename (from
), gfc_typename (to
), where
);
1931 case ARITH_INCOMMENSURATE
:
1932 gfc_error ("Array operands are incommensurate converting %s to %s at %L",
1933 gfc_typename (from
), gfc_typename (to
), where
);
1935 case ARITH_ASYMMETRIC
:
1936 gfc_error ("Integer outside symmetric range implied by Standard Fortran"
1937 " converting %s to %s at %L",
1938 gfc_typename (from
), gfc_typename (to
), where
);
1941 gfc_internal_error ("gfc_arith_error(): Bad error code");
1944 /* TODO: Do something about the error, i.e., throw exception, return
1949 /* Convert integers to integers. */
1952 gfc_int2int (gfc_expr
*src
, int kind
)
1957 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &src
->where
);
1959 mpz_set (result
->value
.integer
, src
->value
.integer
);
1961 if ((rc
= gfc_check_integer_range (result
->value
.integer
, kind
)) != ARITH_OK
)
1963 if (rc
== ARITH_ASYMMETRIC
)
1965 gfc_warning (gfc_arith_error (rc
), &src
->where
);
1969 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
1970 gfc_free_expr (result
);
1979 /* Convert integers to reals. */
1982 gfc_int2real (gfc_expr
*src
, int kind
)
1987 result
= gfc_get_constant_expr (BT_REAL
, kind
, &src
->where
);
1989 mpfr_set_z (result
->value
.real
, src
->value
.integer
, GFC_RND_MODE
);
1991 if ((rc
= gfc_check_real_range (result
->value
.real
, kind
)) != ARITH_OK
)
1993 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
1994 gfc_free_expr (result
);
2002 /* Convert default integer to default complex. */
2005 gfc_int2complex (gfc_expr
*src
, int kind
)
2010 result
= gfc_get_constant_expr (BT_COMPLEX
, kind
, &src
->where
);
2012 mpc_set_z (result
->value
.complex, src
->value
.integer
, GFC_MPC_RND_MODE
);
2014 if ((rc
= gfc_check_real_range (mpc_realref (result
->value
.complex), kind
))
2017 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2018 gfc_free_expr (result
);
2026 /* Convert default real to default integer. */
2029 gfc_real2int (gfc_expr
*src
, int kind
)
2034 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &src
->where
);
2036 gfc_mpfr_to_mpz (result
->value
.integer
, src
->value
.real
, &src
->where
);
2038 if ((rc
= gfc_check_integer_range (result
->value
.integer
, kind
)) != ARITH_OK
)
2040 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2041 gfc_free_expr (result
);
2049 /* Convert real to real. */
2052 gfc_real2real (gfc_expr
*src
, int kind
)
2057 result
= gfc_get_constant_expr (BT_REAL
, kind
, &src
->where
);
2059 mpfr_set (result
->value
.real
, src
->value
.real
, GFC_RND_MODE
);
2061 rc
= gfc_check_real_range (result
->value
.real
, kind
);
2063 if (rc
== ARITH_UNDERFLOW
)
2065 if (gfc_option
.warn_underflow
)
2066 gfc_warning (gfc_arith_error (rc
), &src
->where
);
2067 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
2069 else if (rc
!= ARITH_OK
)
2071 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2072 gfc_free_expr (result
);
2080 /* Convert real to complex. */
2083 gfc_real2complex (gfc_expr
*src
, int kind
)
2088 result
= gfc_get_constant_expr (BT_COMPLEX
, kind
, &src
->where
);
2090 mpc_set_fr (result
->value
.complex, src
->value
.real
, GFC_MPC_RND_MODE
);
2092 rc
= gfc_check_real_range (mpc_realref (result
->value
.complex), kind
);
2094 if (rc
== ARITH_UNDERFLOW
)
2096 if (gfc_option
.warn_underflow
)
2097 gfc_warning (gfc_arith_error (rc
), &src
->where
);
2098 mpfr_set_ui (mpc_realref (result
->value
.complex), 0, GFC_RND_MODE
);
2100 else if (rc
!= ARITH_OK
)
2102 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2103 gfc_free_expr (result
);
2111 /* Convert complex to integer. */
2114 gfc_complex2int (gfc_expr
*src
, int kind
)
2119 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &src
->where
);
2121 gfc_mpfr_to_mpz (result
->value
.integer
, mpc_realref (src
->value
.complex),
2124 if ((rc
= gfc_check_integer_range (result
->value
.integer
, kind
)) != ARITH_OK
)
2126 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2127 gfc_free_expr (result
);
2135 /* Convert complex to real. */
2138 gfc_complex2real (gfc_expr
*src
, int kind
)
2143 result
= gfc_get_constant_expr (BT_REAL
, kind
, &src
->where
);
2145 mpc_real (result
->value
.real
, src
->value
.complex, GFC_RND_MODE
);
2147 rc
= gfc_check_real_range (result
->value
.real
, kind
);
2149 if (rc
== ARITH_UNDERFLOW
)
2151 if (gfc_option
.warn_underflow
)
2152 gfc_warning (gfc_arith_error (rc
), &src
->where
);
2153 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
2157 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2158 gfc_free_expr (result
);
2166 /* Convert complex to complex. */
2169 gfc_complex2complex (gfc_expr
*src
, int kind
)
2174 result
= gfc_get_constant_expr (BT_COMPLEX
, kind
, &src
->where
);
2176 mpc_set (result
->value
.complex, src
->value
.complex, GFC_MPC_RND_MODE
);
2178 rc
= gfc_check_real_range (mpc_realref (result
->value
.complex), kind
);
2180 if (rc
== ARITH_UNDERFLOW
)
2182 if (gfc_option
.warn_underflow
)
2183 gfc_warning (gfc_arith_error (rc
), &src
->where
);
2184 mpfr_set_ui (mpc_realref (result
->value
.complex), 0, GFC_RND_MODE
);
2186 else if (rc
!= ARITH_OK
)
2188 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2189 gfc_free_expr (result
);
2193 rc
= gfc_check_real_range (mpc_imagref (result
->value
.complex), kind
);
2195 if (rc
== ARITH_UNDERFLOW
)
2197 if (gfc_option
.warn_underflow
)
2198 gfc_warning (gfc_arith_error (rc
), &src
->where
);
2199 mpfr_set_ui (mpc_imagref (result
->value
.complex), 0, GFC_RND_MODE
);
2201 else if (rc
!= ARITH_OK
)
2203 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2204 gfc_free_expr (result
);
2212 /* Logical kind conversion. */
2215 gfc_log2log (gfc_expr
*src
, int kind
)
2219 result
= gfc_get_constant_expr (BT_LOGICAL
, kind
, &src
->where
);
2220 result
->value
.logical
= src
->value
.logical
;
2226 /* Convert logical to integer. */
2229 gfc_log2int (gfc_expr
*src
, int kind
)
2233 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &src
->where
);
2234 mpz_set_si (result
->value
.integer
, src
->value
.logical
);
2240 /* Convert integer to logical. */
2243 gfc_int2log (gfc_expr
*src
, int kind
)
2247 result
= gfc_get_constant_expr (BT_LOGICAL
, kind
, &src
->where
);
2248 result
->value
.logical
= (mpz_cmp_si (src
->value
.integer
, 0) != 0);
2254 /* Helper function to set the representation in a Hollerith conversion.
2255 This assumes that the ts.type and ts.kind of the result have already
2259 hollerith2representation (gfc_expr
*result
, gfc_expr
*src
)
2261 int src_len
, result_len
;
2263 src_len
= src
->representation
.length
- src
->ts
.u
.pad
;
2264 result_len
= gfc_target_expr_size (result
);
2266 if (src_len
> result_len
)
2268 gfc_warning ("The Hollerith constant at %L is too long to convert to %s",
2269 &src
->where
, gfc_typename(&result
->ts
));
2272 result
->representation
.string
= XCNEWVEC (char, result_len
+ 1);
2273 memcpy (result
->representation
.string
, src
->representation
.string
,
2274 MIN (result_len
, src_len
));
2276 if (src_len
< result_len
)
2277 memset (&result
->representation
.string
[src_len
], ' ', result_len
- src_len
);
2279 result
->representation
.string
[result_len
] = '\0'; /* For debugger */
2280 result
->representation
.length
= result_len
;
2284 /* Convert Hollerith to integer. The constant will be padded or truncated. */
2287 gfc_hollerith2int (gfc_expr
*src
, int kind
)
2290 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &src
->where
);
2292 hollerith2representation (result
, src
);
2293 gfc_interpret_integer (kind
, (unsigned char *) result
->representation
.string
,
2294 result
->representation
.length
, result
->value
.integer
);
2300 /* Convert Hollerith to real. The constant will be padded or truncated. */
2303 gfc_hollerith2real (gfc_expr
*src
, int kind
)
2306 result
= gfc_get_constant_expr (BT_REAL
, kind
, &src
->where
);
2308 hollerith2representation (result
, src
);
2309 gfc_interpret_float (kind
, (unsigned char *) result
->representation
.string
,
2310 result
->representation
.length
, result
->value
.real
);
2316 /* Convert Hollerith to complex. The constant will be padded or truncated. */
2319 gfc_hollerith2complex (gfc_expr
*src
, int kind
)
2322 result
= gfc_get_constant_expr (BT_COMPLEX
, kind
, &src
->where
);
2324 hollerith2representation (result
, src
);
2325 gfc_interpret_complex (kind
, (unsigned char *) result
->representation
.string
,
2326 result
->representation
.length
, result
->value
.complex);
2332 /* Convert Hollerith to character. */
2335 gfc_hollerith2character (gfc_expr
*src
, int kind
)
2339 result
= gfc_copy_expr (src
);
2340 result
->ts
.type
= BT_CHARACTER
;
2341 result
->ts
.kind
= kind
;
2343 result
->value
.character
.length
= result
->representation
.length
;
2344 result
->value
.character
.string
2345 = gfc_char_to_widechar (result
->representation
.string
);
2351 /* Convert Hollerith to logical. The constant will be padded or truncated. */
2354 gfc_hollerith2logical (gfc_expr
*src
, int kind
)
2357 result
= gfc_get_constant_expr (BT_LOGICAL
, kind
, &src
->where
);
2359 hollerith2representation (result
, src
);
2360 gfc_interpret_logical (kind
, (unsigned char *) result
->representation
.string
,
2361 result
->representation
.length
, &result
->value
.logical
);