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. */
30 #include "coretypes.h"
34 #include "target-memory.h"
35 #include "constructor.h"
37 /* MPFR does not have a direct replacement for mpz_set_f() from GMP.
38 It's easily implemented with a few calls though. */
41 gfc_mpfr_to_mpz (mpz_t z
, mpfr_t x
, locus
*where
)
45 if (mpfr_inf_p (x
) || mpfr_nan_p (x
))
47 gfc_error ("Conversion of an Infinity or Not-a-Number at %L "
53 e
= mpfr_get_z_exp (z
, x
);
56 mpz_mul_2exp (z
, z
, e
);
58 mpz_tdiv_q_2exp (z
, z
, -e
);
62 /* Set the model number precision by the requested KIND. */
65 gfc_set_model_kind (int kind
)
67 int index
= gfc_validate_kind (BT_REAL
, kind
, false);
70 base2prec
= gfc_real_kinds
[index
].digits
;
71 if (gfc_real_kinds
[index
].radix
!= 2)
72 base2prec
*= gfc_real_kinds
[index
].radix
/ 2;
73 mpfr_set_default_prec (base2prec
);
77 /* Set the model number precision from mpfr_t x. */
80 gfc_set_model (mpfr_t x
)
82 mpfr_set_default_prec (mpfr_get_prec (x
));
86 /* Given an arithmetic error code, return a pointer to a string that
87 explains the error. */
90 gfc_arith_error (arith code
)
97 p
= _("Arithmetic OK at %L");
100 p
= _("Arithmetic overflow at %L");
102 case ARITH_UNDERFLOW
:
103 p
= _("Arithmetic underflow at %L");
106 p
= _("Arithmetic NaN at %L");
109 p
= _("Division by zero at %L");
111 case ARITH_INCOMMENSURATE
:
112 p
= _("Array operands are incommensurate at %L");
114 case ARITH_ASYMMETRIC
:
116 _("Integer outside symmetric range implied by Standard Fortran at %L");
119 gfc_internal_error ("gfc_arith_error(): Bad error code");
126 /* Get things ready to do math. */
129 gfc_arith_init_1 (void)
131 gfc_integer_info
*int_info
;
132 gfc_real_info
*real_info
;
136 mpfr_set_default_prec (128);
139 /* Convert the minimum and maximum values for each kind into their
140 GNU MP representation. */
141 for (int_info
= gfc_integer_kinds
; int_info
->kind
!= 0; int_info
++)
144 mpz_init (int_info
->huge
);
145 mpz_set_ui (int_info
->huge
, int_info
->radix
);
146 mpz_pow_ui (int_info
->huge
, int_info
->huge
, int_info
->digits
);
147 mpz_sub_ui (int_info
->huge
, int_info
->huge
, 1);
149 /* These are the numbers that are actually representable by the
150 target. For bases other than two, this needs to be changed. */
151 if (int_info
->radix
!= 2)
152 gfc_internal_error ("Fix min_int calculation");
154 /* See PRs 13490 and 17912, related to integer ranges.
155 The pedantic_min_int exists for range checking when a program
156 is compiled with -pedantic, and reflects the belief that
157 Standard Fortran requires integers to be symmetrical, i.e.
158 every negative integer must have a representable positive
159 absolute value, and vice versa. */
161 mpz_init (int_info
->pedantic_min_int
);
162 mpz_neg (int_info
->pedantic_min_int
, int_info
->huge
);
164 mpz_init (int_info
->min_int
);
165 mpz_sub_ui (int_info
->min_int
, int_info
->pedantic_min_int
, 1);
168 mpfr_set_z (a
, int_info
->huge
, GFC_RND_MODE
);
169 mpfr_log10 (a
, a
, GFC_RND_MODE
);
171 int_info
->range
= (int) mpfr_get_si (a
, GFC_RND_MODE
);
176 for (real_info
= gfc_real_kinds
; real_info
->kind
!= 0; real_info
++)
178 gfc_set_model_kind (real_info
->kind
);
183 /* huge(x) = (1 - b**(-p)) * b**(emax-1) * b */
185 mpfr_init (real_info
->huge
);
186 mpfr_set_ui (real_info
->huge
, 1, GFC_RND_MODE
);
187 mpfr_set_ui (a
, real_info
->radix
, GFC_RND_MODE
);
188 mpfr_pow_si (a
, a
, -real_info
->digits
, GFC_RND_MODE
);
189 mpfr_sub (real_info
->huge
, real_info
->huge
, a
, GFC_RND_MODE
);
192 mpfr_set_ui (a
, real_info
->radix
, GFC_RND_MODE
);
193 mpfr_pow_ui (a
, a
, real_info
->max_exponent
- 1, GFC_RND_MODE
);
195 /* (1 - b**(-p)) * b**(emax-1) */
196 mpfr_mul (real_info
->huge
, real_info
->huge
, a
, GFC_RND_MODE
);
198 /* (1 - b**(-p)) * b**(emax-1) * b */
199 mpfr_mul_ui (real_info
->huge
, real_info
->huge
, real_info
->radix
,
202 /* tiny(x) = b**(emin-1) */
203 mpfr_init (real_info
->tiny
);
204 mpfr_set_ui (real_info
->tiny
, real_info
->radix
, GFC_RND_MODE
);
205 mpfr_pow_si (real_info
->tiny
, real_info
->tiny
,
206 real_info
->min_exponent
- 1, GFC_RND_MODE
);
208 /* subnormal (x) = b**(emin - digit) */
209 mpfr_init (real_info
->subnormal
);
210 mpfr_set_ui (real_info
->subnormal
, real_info
->radix
, GFC_RND_MODE
);
211 mpfr_pow_si (real_info
->subnormal
, real_info
->subnormal
,
212 real_info
->min_exponent
- real_info
->digits
, GFC_RND_MODE
);
214 /* epsilon(x) = b**(1-p) */
215 mpfr_init (real_info
->epsilon
);
216 mpfr_set_ui (real_info
->epsilon
, real_info
->radix
, GFC_RND_MODE
);
217 mpfr_pow_si (real_info
->epsilon
, real_info
->epsilon
,
218 1 - real_info
->digits
, GFC_RND_MODE
);
220 /* range(x) = int(min(log10(huge(x)), -log10(tiny)) */
221 mpfr_log10 (a
, real_info
->huge
, GFC_RND_MODE
);
222 mpfr_log10 (b
, real_info
->tiny
, GFC_RND_MODE
);
223 mpfr_neg (b
, b
, GFC_RND_MODE
);
226 mpfr_min (a
, a
, b
, GFC_RND_MODE
);
228 real_info
->range
= (int) mpfr_get_si (a
, GFC_RND_MODE
);
230 /* precision(x) = int((p - 1) * log10(b)) + k */
231 mpfr_set_ui (a
, real_info
->radix
, GFC_RND_MODE
);
232 mpfr_log10 (a
, a
, GFC_RND_MODE
);
233 mpfr_mul_ui (a
, a
, real_info
->digits
- 1, GFC_RND_MODE
);
235 real_info
->precision
= (int) mpfr_get_si (a
, GFC_RND_MODE
);
237 /* If the radix is an integral power of 10, add one to the precision. */
238 for (i
= 10; i
<= real_info
->radix
; i
*= 10)
239 if (i
== real_info
->radix
)
240 real_info
->precision
++;
242 mpfr_clears (a
, b
, NULL
);
247 /* Clean up, get rid of numeric constants. */
250 gfc_arith_done_1 (void)
252 gfc_integer_info
*ip
;
255 for (ip
= gfc_integer_kinds
; ip
->kind
; ip
++)
257 mpz_clear (ip
->min_int
);
258 mpz_clear (ip
->pedantic_min_int
);
259 mpz_clear (ip
->huge
);
262 for (rp
= gfc_real_kinds
; rp
->kind
; rp
++)
263 mpfr_clears (rp
->epsilon
, rp
->huge
, rp
->tiny
, rp
->subnormal
, NULL
);
269 /* Given a wide character value and a character kind, determine whether
270 the character is representable for that kind. */
272 gfc_check_character_range (gfc_char_t c
, int kind
)
274 /* As wide characters are stored as 32-bit values, they're all
275 representable in UCS=4. */
280 return c
<= 255 ? true : false;
286 /* Given an integer and a kind, make sure that the integer lies within
287 the range of the kind. Returns ARITH_OK, ARITH_ASYMMETRIC or
291 gfc_check_integer_range (mpz_t p
, int kind
)
296 i
= gfc_validate_kind (BT_INTEGER
, kind
, false);
301 if (mpz_cmp (p
, gfc_integer_kinds
[i
].pedantic_min_int
) < 0)
302 result
= ARITH_ASYMMETRIC
;
306 if (gfc_option
.flag_range_check
== 0)
309 if (mpz_cmp (p
, gfc_integer_kinds
[i
].min_int
) < 0
310 || mpz_cmp (p
, gfc_integer_kinds
[i
].huge
) > 0)
311 result
= ARITH_OVERFLOW
;
317 /* Given a real and a kind, make sure that the real lies within the
318 range of the kind. Returns ARITH_OK, ARITH_OVERFLOW or
322 gfc_check_real_range (mpfr_t p
, int kind
)
328 i
= gfc_validate_kind (BT_REAL
, kind
, false);
332 mpfr_abs (q
, p
, GFC_RND_MODE
);
338 if (gfc_option
.flag_range_check
!= 0)
339 retval
= ARITH_OVERFLOW
;
341 else if (mpfr_nan_p (p
))
343 if (gfc_option
.flag_range_check
!= 0)
346 else if (mpfr_sgn (q
) == 0)
351 else if (mpfr_cmp (q
, gfc_real_kinds
[i
].huge
) > 0)
353 if (gfc_option
.flag_range_check
== 0)
354 mpfr_set_inf (p
, mpfr_sgn (p
));
356 retval
= ARITH_OVERFLOW
;
358 else if (mpfr_cmp (q
, gfc_real_kinds
[i
].subnormal
) < 0)
360 if (gfc_option
.flag_range_check
== 0)
362 if (mpfr_sgn (p
) < 0)
364 mpfr_set_ui (p
, 0, GFC_RND_MODE
);
365 mpfr_set_si (q
, -1, GFC_RND_MODE
);
366 mpfr_copysign (p
, p
, q
, GFC_RND_MODE
);
369 mpfr_set_ui (p
, 0, GFC_RND_MODE
);
372 retval
= ARITH_UNDERFLOW
;
374 else if (mpfr_cmp (q
, gfc_real_kinds
[i
].tiny
) < 0)
379 /* Save current values of emin and emax. */
380 emin
= mpfr_get_emin ();
381 emax
= mpfr_get_emax ();
383 /* Set emin and emax for the current model number. */
384 en
= gfc_real_kinds
[i
].min_exponent
- gfc_real_kinds
[i
].digits
+ 1;
385 mpfr_set_emin ((mp_exp_t
) en
);
386 mpfr_set_emax ((mp_exp_t
) gfc_real_kinds
[i
].max_exponent
);
387 mpfr_check_range (q
, 0, GFC_RND_MODE
);
388 mpfr_subnormalize (q
, 0, GFC_RND_MODE
);
390 /* Reset emin and emax. */
391 mpfr_set_emin (emin
);
392 mpfr_set_emax (emax
);
394 /* Copy sign if needed. */
395 if (mpfr_sgn (p
) < 0)
396 mpfr_neg (p
, q
, GMP_RNDN
);
398 mpfr_set (p
, q
, GMP_RNDN
);
407 /* Low-level arithmetic functions. All of these subroutines assume
408 that all operands are of the same type and return an operand of the
409 same type. The other thing about these subroutines is that they
410 can fail in various ways -- overflow, underflow, division by zero,
411 zero raised to the zero, etc. */
414 gfc_arith_not (gfc_expr
*op1
, gfc_expr
**resultp
)
418 result
= gfc_get_constant_expr (BT_LOGICAL
, op1
->ts
.kind
, &op1
->where
);
419 result
->value
.logical
= !op1
->value
.logical
;
427 gfc_arith_and (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
431 result
= gfc_get_constant_expr (BT_LOGICAL
, gfc_kind_max (op1
, op2
),
433 result
->value
.logical
= op1
->value
.logical
&& op2
->value
.logical
;
441 gfc_arith_or (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
445 result
= gfc_get_constant_expr (BT_LOGICAL
, gfc_kind_max (op1
, op2
),
447 result
->value
.logical
= op1
->value
.logical
|| op2
->value
.logical
;
455 gfc_arith_eqv (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
459 result
= gfc_get_constant_expr (BT_LOGICAL
, gfc_kind_max (op1
, op2
),
461 result
->value
.logical
= op1
->value
.logical
== op2
->value
.logical
;
469 gfc_arith_neqv (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
473 result
= gfc_get_constant_expr (BT_LOGICAL
, gfc_kind_max (op1
, op2
),
475 result
->value
.logical
= op1
->value
.logical
!= op2
->value
.logical
;
482 /* Make sure a constant numeric expression is within the range for
483 its type and kind. Note that there's also a gfc_check_range(),
484 but that one deals with the intrinsic RANGE function. */
487 gfc_range_check (gfc_expr
*e
)
495 rc
= gfc_check_integer_range (e
->value
.integer
, e
->ts
.kind
);
499 rc
= gfc_check_real_range (e
->value
.real
, e
->ts
.kind
);
500 if (rc
== ARITH_UNDERFLOW
)
501 mpfr_set_ui (e
->value
.real
, 0, GFC_RND_MODE
);
502 if (rc
== ARITH_OVERFLOW
)
503 mpfr_set_inf (e
->value
.real
, mpfr_sgn (e
->value
.real
));
505 mpfr_set_nan (e
->value
.real
);
509 rc
= gfc_check_real_range (mpc_realref (e
->value
.complex), e
->ts
.kind
);
510 if (rc
== ARITH_UNDERFLOW
)
511 mpfr_set_ui (mpc_realref (e
->value
.complex), 0, GFC_RND_MODE
);
512 if (rc
== ARITH_OVERFLOW
)
513 mpfr_set_inf (mpc_realref (e
->value
.complex),
514 mpfr_sgn (mpc_realref (e
->value
.complex)));
516 mpfr_set_nan (mpc_realref (e
->value
.complex));
518 rc2
= gfc_check_real_range (mpc_imagref (e
->value
.complex), e
->ts
.kind
);
519 if (rc
== ARITH_UNDERFLOW
)
520 mpfr_set_ui (mpc_imagref (e
->value
.complex), 0, GFC_RND_MODE
);
521 if (rc
== ARITH_OVERFLOW
)
522 mpfr_set_inf (mpc_imagref (e
->value
.complex),
523 mpfr_sgn (mpc_imagref (e
->value
.complex)));
525 mpfr_set_nan (mpc_imagref (e
->value
.complex));
532 gfc_internal_error ("gfc_range_check(): Bad type");
539 /* Several of the following routines use the same set of statements to
540 check the validity of the result. Encapsulate the checking here. */
543 check_result (arith rc
, gfc_expr
*x
, gfc_expr
*r
, gfc_expr
**rp
)
547 if (val
== ARITH_UNDERFLOW
)
549 if (gfc_option
.warn_underflow
)
550 gfc_warning (gfc_arith_error (val
), &x
->where
);
554 if (val
== ARITH_ASYMMETRIC
)
556 gfc_warning (gfc_arith_error (val
), &x
->where
);
569 /* It may seem silly to have a subroutine that actually computes the
570 unary plus of a constant, but it prevents us from making exceptions
571 in the code elsewhere. Used for unary plus and parenthesized
575 gfc_arith_identity (gfc_expr
*op1
, gfc_expr
**resultp
)
577 *resultp
= gfc_copy_expr (op1
);
583 gfc_arith_uminus (gfc_expr
*op1
, gfc_expr
**resultp
)
588 result
= gfc_get_constant_expr (op1
->ts
.type
, op1
->ts
.kind
, &op1
->where
);
590 switch (op1
->ts
.type
)
593 mpz_neg (result
->value
.integer
, op1
->value
.integer
);
597 mpfr_neg (result
->value
.real
, op1
->value
.real
, GFC_RND_MODE
);
601 mpc_neg (result
->value
.complex, op1
->value
.complex, GFC_MPC_RND_MODE
);
605 gfc_internal_error ("gfc_arith_uminus(): Bad basic type");
608 rc
= gfc_range_check (result
);
610 return check_result (rc
, op1
, result
, resultp
);
615 gfc_arith_plus (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
620 result
= gfc_get_constant_expr (op1
->ts
.type
, op1
->ts
.kind
, &op1
->where
);
622 switch (op1
->ts
.type
)
625 mpz_add (result
->value
.integer
, op1
->value
.integer
, op2
->value
.integer
);
629 mpfr_add (result
->value
.real
, op1
->value
.real
, op2
->value
.real
,
634 mpc_add (result
->value
.complex, op1
->value
.complex, op2
->value
.complex,
639 gfc_internal_error ("gfc_arith_plus(): Bad basic type");
642 rc
= gfc_range_check (result
);
644 return check_result (rc
, op1
, result
, resultp
);
649 gfc_arith_minus (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
654 result
= gfc_get_constant_expr (op1
->ts
.type
, op1
->ts
.kind
, &op1
->where
);
656 switch (op1
->ts
.type
)
659 mpz_sub (result
->value
.integer
, op1
->value
.integer
, op2
->value
.integer
);
663 mpfr_sub (result
->value
.real
, op1
->value
.real
, op2
->value
.real
,
668 mpc_sub (result
->value
.complex, op1
->value
.complex,
669 op2
->value
.complex, GFC_MPC_RND_MODE
);
673 gfc_internal_error ("gfc_arith_minus(): Bad basic type");
676 rc
= gfc_range_check (result
);
678 return check_result (rc
, op1
, result
, resultp
);
683 gfc_arith_times (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
688 result
= gfc_get_constant_expr (op1
->ts
.type
, op1
->ts
.kind
, &op1
->where
);
690 switch (op1
->ts
.type
)
693 mpz_mul (result
->value
.integer
, op1
->value
.integer
, op2
->value
.integer
);
697 mpfr_mul (result
->value
.real
, op1
->value
.real
, op2
->value
.real
,
702 gfc_set_model (mpc_realref (op1
->value
.complex));
703 mpc_mul (result
->value
.complex, op1
->value
.complex, op2
->value
.complex,
708 gfc_internal_error ("gfc_arith_times(): Bad basic type");
711 rc
= gfc_range_check (result
);
713 return check_result (rc
, op1
, result
, resultp
);
718 gfc_arith_divide (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
725 result
= gfc_get_constant_expr (op1
->ts
.type
, op1
->ts
.kind
, &op1
->where
);
727 switch (op1
->ts
.type
)
730 if (mpz_sgn (op2
->value
.integer
) == 0)
736 mpz_tdiv_q (result
->value
.integer
, op1
->value
.integer
,
741 if (mpfr_sgn (op2
->value
.real
) == 0 && gfc_option
.flag_range_check
== 1)
747 mpfr_div (result
->value
.real
, op1
->value
.real
, op2
->value
.real
,
752 if (mpc_cmp_si_si (op2
->value
.complex, 0, 0) == 0
753 && gfc_option
.flag_range_check
== 1)
759 gfc_set_model (mpc_realref (op1
->value
.complex));
760 if (mpc_cmp_si_si (op2
->value
.complex, 0, 0) == 0)
762 /* In Fortran, return (NaN + NaN I) for any zero divisor. See
764 mpfr_set_nan (mpc_realref (result
->value
.complex));
765 mpfr_set_nan (mpc_imagref (result
->value
.complex));
768 mpc_div (result
->value
.complex, op1
->value
.complex, op2
->value
.complex,
773 gfc_internal_error ("gfc_arith_divide(): Bad basic type");
777 rc
= gfc_range_check (result
);
779 return check_result (rc
, op1
, result
, resultp
);
782 /* Raise a number to a power. */
785 arith_power (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
792 result
= gfc_get_constant_expr (op1
->ts
.type
, op1
->ts
.kind
, &op1
->where
);
794 switch (op2
->ts
.type
)
797 power_sign
= mpz_sgn (op2
->value
.integer
);
801 /* Handle something to the zeroth power. Since we're dealing
802 with integral exponents, there is no ambiguity in the
803 limiting procedure used to determine the value of 0**0. */
804 switch (op1
->ts
.type
)
807 mpz_set_ui (result
->value
.integer
, 1);
811 mpfr_set_ui (result
->value
.real
, 1, GFC_RND_MODE
);
815 mpc_set_ui (result
->value
.complex, 1, GFC_MPC_RND_MODE
);
819 gfc_internal_error ("arith_power(): Bad base");
824 switch (op1
->ts
.type
)
830 /* First, we simplify the cases of op1 == 1, 0 or -1. */
831 if (mpz_cmp_si (op1
->value
.integer
, 1) == 0)
834 mpz_set_si (result
->value
.integer
, 1);
836 else if (mpz_cmp_si (op1
->value
.integer
, 0) == 0)
838 /* 0**op2 == 0, if op2 > 0
839 0**op2 overflow, if op2 < 0 ; in that case, we
840 set the result to 0 and return ARITH_DIV0. */
841 mpz_set_si (result
->value
.integer
, 0);
842 if (mpz_cmp_si (op2
->value
.integer
, 0) < 0)
845 else if (mpz_cmp_si (op1
->value
.integer
, -1) == 0)
847 /* (-1)**op2 == (-1)**(mod(op2,2)) */
848 unsigned int odd
= mpz_fdiv_ui (op2
->value
.integer
, 2);
850 mpz_set_si (result
->value
.integer
, -1);
852 mpz_set_si (result
->value
.integer
, 1);
854 /* Then, we take care of op2 < 0. */
855 else if (mpz_cmp_si (op2
->value
.integer
, 0) < 0)
857 /* if op2 < 0, op1**op2 == 0 because abs(op1) > 1. */
858 mpz_set_si (result
->value
.integer
, 0);
860 else if (gfc_extract_int (op2
, &power
) != NULL
)
862 /* If op2 doesn't fit in an int, the exponentiation will
863 overflow, because op2 > 0 and abs(op1) > 1. */
866 i
= gfc_validate_kind (BT_INTEGER
, result
->ts
.kind
, false);
868 if (gfc_option
.flag_range_check
)
871 /* Still, we want to give the same value as the
874 mpz_add_ui (max
, gfc_integer_kinds
[i
].huge
, 1);
875 mpz_mul_ui (max
, max
, 2);
876 mpz_powm (result
->value
.integer
, op1
->value
.integer
,
877 op2
->value
.integer
, max
);
881 mpz_pow_ui (result
->value
.integer
, op1
->value
.integer
,
887 mpfr_pow_z (result
->value
.real
, op1
->value
.real
,
888 op2
->value
.integer
, GFC_RND_MODE
);
892 mpc_pow_z (result
->value
.complex, op1
->value
.complex,
893 op2
->value
.integer
, GFC_MPC_RND_MODE
);
904 if (gfc_init_expr_flag
)
906 if (gfc_notify_std (GFC_STD_F2003
, "Noninteger "
907 "exponent in an initialization "
908 "expression at %L", &op2
->where
) == FAILURE
)
910 gfc_free_expr (result
);
911 return ARITH_PROHIBIT
;
915 if (mpfr_cmp_si (op1
->value
.real
, 0) < 0)
917 gfc_error ("Raising a negative REAL at %L to "
918 "a REAL power is prohibited", &op1
->where
);
919 gfc_free_expr (result
);
920 return ARITH_PROHIBIT
;
923 mpfr_pow (result
->value
.real
, op1
->value
.real
, op2
->value
.real
,
929 if (gfc_init_expr_flag
)
931 if (gfc_notify_std (GFC_STD_F2003
, "Noninteger "
932 "exponent in an initialization "
933 "expression at %L", &op2
->where
) == FAILURE
)
935 gfc_free_expr (result
);
936 return ARITH_PROHIBIT
;
940 mpc_pow (result
->value
.complex, op1
->value
.complex,
941 op2
->value
.complex, GFC_MPC_RND_MODE
);
945 gfc_internal_error ("arith_power(): unknown type");
949 rc
= gfc_range_check (result
);
951 return check_result (rc
, op1
, result
, resultp
);
955 /* Concatenate two string constants. */
958 gfc_arith_concat (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
963 gcc_assert (op1
->ts
.kind
== op2
->ts
.kind
);
964 result
= gfc_get_constant_expr (BT_CHARACTER
, op1
->ts
.kind
,
967 len
= op1
->value
.character
.length
+ op2
->value
.character
.length
;
969 result
->value
.character
.string
= gfc_get_wide_string (len
+ 1);
970 result
->value
.character
.length
= len
;
972 memcpy (result
->value
.character
.string
, op1
->value
.character
.string
,
973 op1
->value
.character
.length
* sizeof (gfc_char_t
));
975 memcpy (&result
->value
.character
.string
[op1
->value
.character
.length
],
976 op2
->value
.character
.string
,
977 op2
->value
.character
.length
* sizeof (gfc_char_t
));
979 result
->value
.character
.string
[len
] = '\0';
986 /* Comparison between real values; returns 0 if (op1 .op. op2) is true.
987 This function mimics mpfr_cmp but takes NaN into account. */
990 compare_real (gfc_expr
*op1
, gfc_expr
*op2
, gfc_intrinsic_op op
)
996 rc
= mpfr_equal_p (op1
->value
.real
, op2
->value
.real
) ? 0 : 1;
999 rc
= mpfr_greater_p (op1
->value
.real
, op2
->value
.real
) ? 1 : -1;
1002 rc
= mpfr_greaterequal_p (op1
->value
.real
, op2
->value
.real
) ? 1 : -1;
1005 rc
= mpfr_less_p (op1
->value
.real
, op2
->value
.real
) ? -1 : 1;
1008 rc
= mpfr_lessequal_p (op1
->value
.real
, op2
->value
.real
) ? -1 : 1;
1011 gfc_internal_error ("compare_real(): Bad operator");
1017 /* Comparison operators. Assumes that the two expression nodes
1018 contain two constants of the same type. The op argument is
1019 needed to handle NaN correctly. */
1022 gfc_compare_expr (gfc_expr
*op1
, gfc_expr
*op2
, gfc_intrinsic_op op
)
1026 switch (op1
->ts
.type
)
1029 rc
= mpz_cmp (op1
->value
.integer
, op2
->value
.integer
);
1033 rc
= compare_real (op1
, op2
, op
);
1037 rc
= gfc_compare_string (op1
, op2
);
1041 rc
= ((!op1
->value
.logical
&& op2
->value
.logical
)
1042 || (op1
->value
.logical
&& !op2
->value
.logical
));
1046 gfc_internal_error ("gfc_compare_expr(): Bad basic type");
1053 /* Compare a pair of complex numbers. Naturally, this is only for
1054 equality and inequality. */
1057 compare_complex (gfc_expr
*op1
, gfc_expr
*op2
)
1059 return mpc_cmp (op1
->value
.complex, op2
->value
.complex) == 0;
1063 /* Given two constant strings and the inverse collating sequence, compare the
1064 strings. We return -1 for a < b, 0 for a == b and 1 for a > b.
1065 We use the processor's default collating sequence. */
1068 gfc_compare_string (gfc_expr
*a
, gfc_expr
*b
)
1070 int len
, alen
, blen
, i
;
1073 alen
= a
->value
.character
.length
;
1074 blen
= b
->value
.character
.length
;
1076 len
= MAX(alen
, blen
);
1078 for (i
= 0; i
< len
; i
++)
1080 ac
= ((i
< alen
) ? a
->value
.character
.string
[i
] : ' ');
1081 bc
= ((i
< blen
) ? b
->value
.character
.string
[i
] : ' ');
1089 /* Strings are equal */
1095 gfc_compare_with_Cstring (gfc_expr
*a
, const char *b
, bool case_sensitive
)
1097 int len
, alen
, blen
, i
;
1100 alen
= a
->value
.character
.length
;
1103 len
= MAX(alen
, blen
);
1105 for (i
= 0; i
< len
; i
++)
1107 ac
= ((i
< alen
) ? a
->value
.character
.string
[i
] : ' ');
1108 bc
= ((i
< blen
) ? b
[i
] : ' ');
1110 if (!case_sensitive
)
1122 /* Strings are equal */
1127 /* Specific comparison subroutines. */
1130 gfc_arith_eq (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
1134 result
= gfc_get_constant_expr (BT_LOGICAL
, gfc_default_logical_kind
,
1136 result
->value
.logical
= (op1
->ts
.type
== BT_COMPLEX
)
1137 ? compare_complex (op1
, op2
)
1138 : (gfc_compare_expr (op1
, op2
, INTRINSIC_EQ
) == 0);
1146 gfc_arith_ne (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
1150 result
= gfc_get_constant_expr (BT_LOGICAL
, gfc_default_logical_kind
,
1152 result
->value
.logical
= (op1
->ts
.type
== BT_COMPLEX
)
1153 ? !compare_complex (op1
, op2
)
1154 : (gfc_compare_expr (op1
, op2
, INTRINSIC_EQ
) != 0);
1162 gfc_arith_gt (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
1166 result
= gfc_get_constant_expr (BT_LOGICAL
, gfc_default_logical_kind
,
1168 result
->value
.logical
= (gfc_compare_expr (op1
, op2
, INTRINSIC_GT
) > 0);
1176 gfc_arith_ge (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
1180 result
= gfc_get_constant_expr (BT_LOGICAL
, gfc_default_logical_kind
,
1182 result
->value
.logical
= (gfc_compare_expr (op1
, op2
, INTRINSIC_GE
) >= 0);
1190 gfc_arith_lt (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
1194 result
= gfc_get_constant_expr (BT_LOGICAL
, gfc_default_logical_kind
,
1196 result
->value
.logical
= (gfc_compare_expr (op1
, op2
, INTRINSIC_LT
) < 0);
1204 gfc_arith_le (gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**resultp
)
1208 result
= gfc_get_constant_expr (BT_LOGICAL
, gfc_default_logical_kind
,
1210 result
->value
.logical
= (gfc_compare_expr (op1
, op2
, INTRINSIC_LE
) <= 0);
1218 reduce_unary (arith (*eval
) (gfc_expr
*, gfc_expr
**), gfc_expr
*op
,
1221 gfc_constructor_base head
;
1226 if (op
->expr_type
== EXPR_CONSTANT
)
1227 return eval (op
, result
);
1230 head
= gfc_constructor_copy (op
->value
.constructor
);
1231 for (c
= gfc_constructor_first (head
); c
; c
= gfc_constructor_next (c
))
1233 rc
= reduce_unary (eval
, c
->expr
, &r
);
1238 gfc_replace_expr (c
->expr
, r
);
1242 gfc_constructor_free (head
);
1245 gfc_constructor
*c
= gfc_constructor_first (head
);
1246 r
= gfc_get_array_expr (c
->expr
->ts
.type
, c
->expr
->ts
.kind
,
1248 r
->shape
= gfc_copy_shape (op
->shape
, op
->rank
);
1250 r
->value
.constructor
= head
;
1259 reduce_binary_ac (arith (*eval
) (gfc_expr
*, gfc_expr
*, gfc_expr
**),
1260 gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**result
)
1262 gfc_constructor_base head
;
1265 arith rc
= ARITH_OK
;
1267 head
= gfc_constructor_copy (op1
->value
.constructor
);
1268 for (c
= gfc_constructor_first (head
); c
; c
= gfc_constructor_next (c
))
1270 if (c
->expr
->expr_type
== EXPR_CONSTANT
)
1271 rc
= eval (c
->expr
, op2
, &r
);
1273 rc
= reduce_binary_ac (eval
, c
->expr
, op2
, &r
);
1278 gfc_replace_expr (c
->expr
, r
);
1282 gfc_constructor_free (head
);
1285 gfc_constructor
*c
= gfc_constructor_first (head
);
1286 r
= gfc_get_array_expr (c
->expr
->ts
.type
, c
->expr
->ts
.kind
,
1288 r
->shape
= gfc_copy_shape (op1
->shape
, op1
->rank
);
1289 r
->rank
= op1
->rank
;
1290 r
->value
.constructor
= head
;
1299 reduce_binary_ca (arith (*eval
) (gfc_expr
*, gfc_expr
*, gfc_expr
**),
1300 gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**result
)
1302 gfc_constructor_base head
;
1305 arith rc
= ARITH_OK
;
1307 head
= gfc_constructor_copy (op2
->value
.constructor
);
1308 for (c
= gfc_constructor_first (head
); c
; c
= gfc_constructor_next (c
))
1310 if (c
->expr
->expr_type
== EXPR_CONSTANT
)
1311 rc
= eval (op1
, c
->expr
, &r
);
1313 rc
= reduce_binary_ca (eval
, op1
, c
->expr
, &r
);
1318 gfc_replace_expr (c
->expr
, r
);
1322 gfc_constructor_free (head
);
1325 gfc_constructor
*c
= gfc_constructor_first (head
);
1326 r
= gfc_get_array_expr (c
->expr
->ts
.type
, c
->expr
->ts
.kind
,
1328 r
->shape
= gfc_copy_shape (op2
->shape
, op2
->rank
);
1329 r
->rank
= op2
->rank
;
1330 r
->value
.constructor
= head
;
1338 /* We need a forward declaration of reduce_binary. */
1339 static arith
reduce_binary (arith (*eval
) (gfc_expr
*, gfc_expr
*, gfc_expr
**),
1340 gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**result
);
1344 reduce_binary_aa (arith (*eval
) (gfc_expr
*, gfc_expr
*, gfc_expr
**),
1345 gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**result
)
1347 gfc_constructor_base head
;
1348 gfc_constructor
*c
, *d
;
1350 arith rc
= ARITH_OK
;
1352 if (gfc_check_conformance (op1
, op2
,
1353 "elemental binary operation") != SUCCESS
)
1354 return ARITH_INCOMMENSURATE
;
1356 head
= gfc_constructor_copy (op1
->value
.constructor
);
1357 for (c
= gfc_constructor_first (head
),
1358 d
= gfc_constructor_first (op2
->value
.constructor
);
1360 c
= gfc_constructor_next (c
), d
= gfc_constructor_next (d
))
1362 rc
= reduce_binary (eval
, c
->expr
, d
->expr
, &r
);
1366 gfc_replace_expr (c
->expr
, r
);
1370 rc
= ARITH_INCOMMENSURATE
;
1373 gfc_constructor_free (head
);
1376 gfc_constructor
*c
= gfc_constructor_first (head
);
1377 r
= gfc_get_array_expr (c
->expr
->ts
.type
, c
->expr
->ts
.kind
,
1379 r
->shape
= gfc_copy_shape (op1
->shape
, op1
->rank
);
1380 r
->rank
= op1
->rank
;
1381 r
->value
.constructor
= head
;
1390 reduce_binary (arith (*eval
) (gfc_expr
*, gfc_expr
*, gfc_expr
**),
1391 gfc_expr
*op1
, gfc_expr
*op2
, gfc_expr
**result
)
1393 if (op1
->expr_type
== EXPR_CONSTANT
&& op2
->expr_type
== EXPR_CONSTANT
)
1394 return eval (op1
, op2
, result
);
1396 if (op1
->expr_type
== EXPR_CONSTANT
&& op2
->expr_type
== EXPR_ARRAY
)
1397 return reduce_binary_ca (eval
, op1
, op2
, result
);
1399 if (op1
->expr_type
== EXPR_ARRAY
&& op2
->expr_type
== EXPR_CONSTANT
)
1400 return reduce_binary_ac (eval
, op1
, op2
, result
);
1402 return reduce_binary_aa (eval
, op1
, op2
, result
);
1408 arith (*f2
)(gfc_expr
*, gfc_expr
**);
1409 arith (*f3
)(gfc_expr
*, gfc_expr
*, gfc_expr
**);
1413 /* High level arithmetic subroutines. These subroutines go into
1414 eval_intrinsic(), which can do one of several things to its
1415 operands. If the operands are incompatible with the intrinsic
1416 operation, we return a node pointing to the operands and hope that
1417 an operator interface is found during resolution.
1419 If the operands are compatible and are constants, then we try doing
1420 the arithmetic. We also handle the cases where either or both
1421 operands are array constructors. */
1424 eval_intrinsic (gfc_intrinsic_op op
,
1425 eval_f eval
, gfc_expr
*op1
, gfc_expr
*op2
)
1427 gfc_expr temp
, *result
;
1431 gfc_clear_ts (&temp
.ts
);
1437 if (op1
->ts
.type
!= BT_LOGICAL
)
1440 temp
.ts
.type
= BT_LOGICAL
;
1441 temp
.ts
.kind
= gfc_default_logical_kind
;
1445 /* Logical binary operators */
1448 case INTRINSIC_NEQV
:
1450 if (op1
->ts
.type
!= BT_LOGICAL
|| op2
->ts
.type
!= BT_LOGICAL
)
1453 temp
.ts
.type
= BT_LOGICAL
;
1454 temp
.ts
.kind
= gfc_default_logical_kind
;
1459 case INTRINSIC_UPLUS
:
1460 case INTRINSIC_UMINUS
:
1461 if (!gfc_numeric_ts (&op1
->ts
))
1468 case INTRINSIC_PARENTHESES
:
1473 /* Additional restrictions for ordering relations. */
1475 case INTRINSIC_GE_OS
:
1477 case INTRINSIC_LT_OS
:
1479 case INTRINSIC_LE_OS
:
1481 case INTRINSIC_GT_OS
:
1482 if (op1
->ts
.type
== BT_COMPLEX
|| op2
->ts
.type
== BT_COMPLEX
)
1484 temp
.ts
.type
= BT_LOGICAL
;
1485 temp
.ts
.kind
= gfc_default_logical_kind
;
1491 case INTRINSIC_EQ_OS
:
1493 case INTRINSIC_NE_OS
:
1494 if (op1
->ts
.type
== BT_CHARACTER
&& op2
->ts
.type
== BT_CHARACTER
)
1497 temp
.ts
.type
= BT_LOGICAL
;
1498 temp
.ts
.kind
= gfc_default_logical_kind
;
1500 /* If kind mismatch, exit and we'll error out later. */
1501 if (op1
->ts
.kind
!= op2
->ts
.kind
)
1508 /* Numeric binary */
1509 case INTRINSIC_PLUS
:
1510 case INTRINSIC_MINUS
:
1511 case INTRINSIC_TIMES
:
1512 case INTRINSIC_DIVIDE
:
1513 case INTRINSIC_POWER
:
1514 if (!gfc_numeric_ts (&op1
->ts
) || !gfc_numeric_ts (&op2
->ts
))
1517 /* Insert any necessary type conversions to make the operands
1520 temp
.expr_type
= EXPR_OP
;
1521 gfc_clear_ts (&temp
.ts
);
1522 temp
.value
.op
.op
= op
;
1524 temp
.value
.op
.op1
= op1
;
1525 temp
.value
.op
.op2
= op2
;
1527 gfc_type_convert_binary (&temp
, 0);
1529 if (op
== INTRINSIC_EQ
|| op
== INTRINSIC_NE
1530 || op
== INTRINSIC_GE
|| op
== INTRINSIC_GT
1531 || op
== INTRINSIC_LE
|| op
== INTRINSIC_LT
1532 || op
== INTRINSIC_EQ_OS
|| op
== INTRINSIC_NE_OS
1533 || op
== INTRINSIC_GE_OS
|| op
== INTRINSIC_GT_OS
1534 || op
== INTRINSIC_LE_OS
|| op
== INTRINSIC_LT_OS
)
1536 temp
.ts
.type
= BT_LOGICAL
;
1537 temp
.ts
.kind
= gfc_default_logical_kind
;
1543 /* Character binary */
1544 case INTRINSIC_CONCAT
:
1545 if (op1
->ts
.type
!= BT_CHARACTER
|| op2
->ts
.type
!= BT_CHARACTER
1546 || op1
->ts
.kind
!= op2
->ts
.kind
)
1549 temp
.ts
.type
= BT_CHARACTER
;
1550 temp
.ts
.kind
= op1
->ts
.kind
;
1554 case INTRINSIC_USER
:
1558 gfc_internal_error ("eval_intrinsic(): Bad operator");
1561 if (op1
->expr_type
!= EXPR_CONSTANT
1562 && (op1
->expr_type
!= EXPR_ARRAY
1563 || !gfc_is_constant_expr (op1
) || !gfc_expanded_ac (op1
)))
1567 && op2
->expr_type
!= EXPR_CONSTANT
1568 && (op2
->expr_type
!= EXPR_ARRAY
1569 || !gfc_is_constant_expr (op2
) || !gfc_expanded_ac (op2
)))
1573 rc
= reduce_unary (eval
.f2
, op1
, &result
);
1575 rc
= reduce_binary (eval
.f3
, op1
, op2
, &result
);
1578 /* Something went wrong. */
1579 if (op
== INTRINSIC_POWER
&& rc
== ARITH_PROHIBIT
)
1584 gfc_error (gfc_arith_error (rc
), &op1
->where
);
1588 gfc_free_expr (op1
);
1589 gfc_free_expr (op2
);
1593 /* Create a run-time expression. */
1594 result
= gfc_get_operator_expr (&op1
->where
, op
, op1
, op2
);
1595 result
->ts
= temp
.ts
;
1601 /* Modify type of expression for zero size array. */
1604 eval_type_intrinsic0 (gfc_intrinsic_op iop
, gfc_expr
*op
)
1607 gfc_internal_error ("eval_type_intrinsic0(): op NULL");
1612 case INTRINSIC_GE_OS
:
1614 case INTRINSIC_LT_OS
:
1616 case INTRINSIC_LE_OS
:
1618 case INTRINSIC_GT_OS
:
1620 case INTRINSIC_EQ_OS
:
1622 case INTRINSIC_NE_OS
:
1623 op
->ts
.type
= BT_LOGICAL
;
1624 op
->ts
.kind
= gfc_default_logical_kind
;
1635 /* Return nonzero if the expression is a zero size array. */
1638 gfc_zero_size_array (gfc_expr
*e
)
1640 if (e
->expr_type
!= EXPR_ARRAY
)
1643 return e
->value
.constructor
== NULL
;
1647 /* Reduce a binary expression where at least one of the operands
1648 involves a zero-length array. Returns NULL if neither of the
1649 operands is a zero-length array. */
1652 reduce_binary0 (gfc_expr
*op1
, gfc_expr
*op2
)
1654 if (gfc_zero_size_array (op1
))
1656 gfc_free_expr (op2
);
1660 if (gfc_zero_size_array (op2
))
1662 gfc_free_expr (op1
);
1671 eval_intrinsic_f2 (gfc_intrinsic_op op
,
1672 arith (*eval
) (gfc_expr
*, gfc_expr
**),
1673 gfc_expr
*op1
, gfc_expr
*op2
)
1680 if (gfc_zero_size_array (op1
))
1681 return eval_type_intrinsic0 (op
, op1
);
1685 result
= reduce_binary0 (op1
, op2
);
1687 return eval_type_intrinsic0 (op
, result
);
1691 return eval_intrinsic (op
, f
, op1
, op2
);
1696 eval_intrinsic_f3 (gfc_intrinsic_op op
,
1697 arith (*eval
) (gfc_expr
*, gfc_expr
*, gfc_expr
**),
1698 gfc_expr
*op1
, gfc_expr
*op2
)
1703 result
= reduce_binary0 (op1
, op2
);
1705 return eval_type_intrinsic0(op
, result
);
1708 return eval_intrinsic (op
, f
, op1
, op2
);
1713 gfc_parentheses (gfc_expr
*op
)
1715 if (gfc_is_constant_expr (op
))
1718 return eval_intrinsic_f2 (INTRINSIC_PARENTHESES
, gfc_arith_identity
,
1723 gfc_uplus (gfc_expr
*op
)
1725 return eval_intrinsic_f2 (INTRINSIC_UPLUS
, gfc_arith_identity
, op
, NULL
);
1730 gfc_uminus (gfc_expr
*op
)
1732 return eval_intrinsic_f2 (INTRINSIC_UMINUS
, gfc_arith_uminus
, op
, NULL
);
1737 gfc_add (gfc_expr
*op1
, gfc_expr
*op2
)
1739 return eval_intrinsic_f3 (INTRINSIC_PLUS
, gfc_arith_plus
, op1
, op2
);
1744 gfc_subtract (gfc_expr
*op1
, gfc_expr
*op2
)
1746 return eval_intrinsic_f3 (INTRINSIC_MINUS
, gfc_arith_minus
, op1
, op2
);
1751 gfc_multiply (gfc_expr
*op1
, gfc_expr
*op2
)
1753 return eval_intrinsic_f3 (INTRINSIC_TIMES
, gfc_arith_times
, op1
, op2
);
1758 gfc_divide (gfc_expr
*op1
, gfc_expr
*op2
)
1760 return eval_intrinsic_f3 (INTRINSIC_DIVIDE
, gfc_arith_divide
, op1
, op2
);
1765 gfc_power (gfc_expr
*op1
, gfc_expr
*op2
)
1767 return eval_intrinsic_f3 (INTRINSIC_POWER
, arith_power
, op1
, op2
);
1772 gfc_concat (gfc_expr
*op1
, gfc_expr
*op2
)
1774 return eval_intrinsic_f3 (INTRINSIC_CONCAT
, gfc_arith_concat
, op1
, op2
);
1779 gfc_and (gfc_expr
*op1
, gfc_expr
*op2
)
1781 return eval_intrinsic_f3 (INTRINSIC_AND
, gfc_arith_and
, op1
, op2
);
1786 gfc_or (gfc_expr
*op1
, gfc_expr
*op2
)
1788 return eval_intrinsic_f3 (INTRINSIC_OR
, gfc_arith_or
, op1
, op2
);
1793 gfc_not (gfc_expr
*op1
)
1795 return eval_intrinsic_f2 (INTRINSIC_NOT
, gfc_arith_not
, op1
, NULL
);
1800 gfc_eqv (gfc_expr
*op1
, gfc_expr
*op2
)
1802 return eval_intrinsic_f3 (INTRINSIC_EQV
, gfc_arith_eqv
, op1
, op2
);
1807 gfc_neqv (gfc_expr
*op1
, gfc_expr
*op2
)
1809 return eval_intrinsic_f3 (INTRINSIC_NEQV
, gfc_arith_neqv
, op1
, op2
);
1814 gfc_eq (gfc_expr
*op1
, gfc_expr
*op2
, gfc_intrinsic_op op
)
1816 return eval_intrinsic_f3 (op
, gfc_arith_eq
, op1
, op2
);
1821 gfc_ne (gfc_expr
*op1
, gfc_expr
*op2
, gfc_intrinsic_op op
)
1823 return eval_intrinsic_f3 (op
, gfc_arith_ne
, op1
, op2
);
1828 gfc_gt (gfc_expr
*op1
, gfc_expr
*op2
, gfc_intrinsic_op op
)
1830 return eval_intrinsic_f3 (op
, gfc_arith_gt
, op1
, op2
);
1835 gfc_ge (gfc_expr
*op1
, gfc_expr
*op2
, gfc_intrinsic_op op
)
1837 return eval_intrinsic_f3 (op
, gfc_arith_ge
, op1
, op2
);
1842 gfc_lt (gfc_expr
*op1
, gfc_expr
*op2
, gfc_intrinsic_op op
)
1844 return eval_intrinsic_f3 (op
, gfc_arith_lt
, op1
, op2
);
1849 gfc_le (gfc_expr
*op1
, gfc_expr
*op2
, gfc_intrinsic_op op
)
1851 return eval_intrinsic_f3 (op
, gfc_arith_le
, op1
, op2
);
1855 /* Convert an integer string to an expression node. */
1858 gfc_convert_integer (const char *buffer
, int kind
, int radix
, locus
*where
)
1863 e
= gfc_get_constant_expr (BT_INTEGER
, kind
, where
);
1864 /* A leading plus is allowed, but not by mpz_set_str. */
1865 if (buffer
[0] == '+')
1869 mpz_set_str (e
->value
.integer
, t
, radix
);
1875 /* Convert a real string to an expression node. */
1878 gfc_convert_real (const char *buffer
, int kind
, locus
*where
)
1882 e
= gfc_get_constant_expr (BT_REAL
, kind
, where
);
1883 mpfr_set_str (e
->value
.real
, buffer
, 10, GFC_RND_MODE
);
1889 /* Convert a pair of real, constant expression nodes to a single
1890 complex expression node. */
1893 gfc_convert_complex (gfc_expr
*real
, gfc_expr
*imag
, int kind
)
1897 e
= gfc_get_constant_expr (BT_COMPLEX
, kind
, &real
->where
);
1898 mpc_set_fr_fr (e
->value
.complex, real
->value
.real
, imag
->value
.real
,
1905 /******* Simplification of intrinsic functions with constant arguments *****/
1908 /* Deal with an arithmetic error. */
1911 arith_error (arith rc
, gfc_typespec
*from
, gfc_typespec
*to
, locus
*where
)
1916 gfc_error ("Arithmetic OK converting %s to %s at %L",
1917 gfc_typename (from
), gfc_typename (to
), where
);
1919 case ARITH_OVERFLOW
:
1920 gfc_error ("Arithmetic overflow converting %s to %s at %L. This check "
1921 "can be disabled with the option -fno-range-check",
1922 gfc_typename (from
), gfc_typename (to
), where
);
1924 case ARITH_UNDERFLOW
:
1925 gfc_error ("Arithmetic underflow converting %s to %s at %L. This check "
1926 "can be disabled with the option -fno-range-check",
1927 gfc_typename (from
), gfc_typename (to
), where
);
1930 gfc_error ("Arithmetic NaN converting %s to %s at %L. This check "
1931 "can be disabled with the option -fno-range-check",
1932 gfc_typename (from
), gfc_typename (to
), where
);
1935 gfc_error ("Division by zero converting %s to %s at %L",
1936 gfc_typename (from
), gfc_typename (to
), where
);
1938 case ARITH_INCOMMENSURATE
:
1939 gfc_error ("Array operands are incommensurate converting %s to %s at %L",
1940 gfc_typename (from
), gfc_typename (to
), where
);
1942 case ARITH_ASYMMETRIC
:
1943 gfc_error ("Integer outside symmetric range implied by Standard Fortran"
1944 " converting %s to %s at %L",
1945 gfc_typename (from
), gfc_typename (to
), where
);
1948 gfc_internal_error ("gfc_arith_error(): Bad error code");
1951 /* TODO: Do something about the error, i.e., throw exception, return
1956 /* Convert integers to integers. */
1959 gfc_int2int (gfc_expr
*src
, int kind
)
1964 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &src
->where
);
1966 mpz_set (result
->value
.integer
, src
->value
.integer
);
1968 if ((rc
= gfc_check_integer_range (result
->value
.integer
, kind
)) != ARITH_OK
)
1970 if (rc
== ARITH_ASYMMETRIC
)
1972 gfc_warning (gfc_arith_error (rc
), &src
->where
);
1976 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
1977 gfc_free_expr (result
);
1986 /* Convert integers to reals. */
1989 gfc_int2real (gfc_expr
*src
, int kind
)
1994 result
= gfc_get_constant_expr (BT_REAL
, kind
, &src
->where
);
1996 mpfr_set_z (result
->value
.real
, src
->value
.integer
, GFC_RND_MODE
);
1998 if ((rc
= gfc_check_real_range (result
->value
.real
, kind
)) != ARITH_OK
)
2000 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2001 gfc_free_expr (result
);
2009 /* Convert default integer to default complex. */
2012 gfc_int2complex (gfc_expr
*src
, int kind
)
2017 result
= gfc_get_constant_expr (BT_COMPLEX
, kind
, &src
->where
);
2019 mpc_set_z (result
->value
.complex, src
->value
.integer
, GFC_MPC_RND_MODE
);
2021 if ((rc
= gfc_check_real_range (mpc_realref (result
->value
.complex), kind
))
2024 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2025 gfc_free_expr (result
);
2033 /* Convert default real to default integer. */
2036 gfc_real2int (gfc_expr
*src
, int kind
)
2041 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &src
->where
);
2043 gfc_mpfr_to_mpz (result
->value
.integer
, src
->value
.real
, &src
->where
);
2045 if ((rc
= gfc_check_integer_range (result
->value
.integer
, kind
)) != ARITH_OK
)
2047 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2048 gfc_free_expr (result
);
2056 /* Convert real to real. */
2059 gfc_real2real (gfc_expr
*src
, int kind
)
2064 result
= gfc_get_constant_expr (BT_REAL
, kind
, &src
->where
);
2066 mpfr_set (result
->value
.real
, src
->value
.real
, GFC_RND_MODE
);
2068 rc
= gfc_check_real_range (result
->value
.real
, kind
);
2070 if (rc
== ARITH_UNDERFLOW
)
2072 if (gfc_option
.warn_underflow
)
2073 gfc_warning (gfc_arith_error (rc
), &src
->where
);
2074 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
2076 else if (rc
!= ARITH_OK
)
2078 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2079 gfc_free_expr (result
);
2087 /* Convert real to complex. */
2090 gfc_real2complex (gfc_expr
*src
, int kind
)
2095 result
= gfc_get_constant_expr (BT_COMPLEX
, kind
, &src
->where
);
2097 mpc_set_fr (result
->value
.complex, src
->value
.real
, GFC_MPC_RND_MODE
);
2099 rc
= gfc_check_real_range (mpc_realref (result
->value
.complex), kind
);
2101 if (rc
== ARITH_UNDERFLOW
)
2103 if (gfc_option
.warn_underflow
)
2104 gfc_warning (gfc_arith_error (rc
), &src
->where
);
2105 mpfr_set_ui (mpc_realref (result
->value
.complex), 0, GFC_RND_MODE
);
2107 else if (rc
!= ARITH_OK
)
2109 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2110 gfc_free_expr (result
);
2118 /* Convert complex to integer. */
2121 gfc_complex2int (gfc_expr
*src
, int kind
)
2126 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &src
->where
);
2128 gfc_mpfr_to_mpz (result
->value
.integer
, mpc_realref (src
->value
.complex),
2131 if ((rc
= gfc_check_integer_range (result
->value
.integer
, kind
)) != ARITH_OK
)
2133 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2134 gfc_free_expr (result
);
2142 /* Convert complex to real. */
2145 gfc_complex2real (gfc_expr
*src
, int kind
)
2150 result
= gfc_get_constant_expr (BT_REAL
, kind
, &src
->where
);
2152 mpc_real (result
->value
.real
, src
->value
.complex, GFC_RND_MODE
);
2154 rc
= gfc_check_real_range (result
->value
.real
, kind
);
2156 if (rc
== ARITH_UNDERFLOW
)
2158 if (gfc_option
.warn_underflow
)
2159 gfc_warning (gfc_arith_error (rc
), &src
->where
);
2160 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
2164 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2165 gfc_free_expr (result
);
2173 /* Convert complex to complex. */
2176 gfc_complex2complex (gfc_expr
*src
, int kind
)
2181 result
= gfc_get_constant_expr (BT_COMPLEX
, kind
, &src
->where
);
2183 mpc_set (result
->value
.complex, src
->value
.complex, GFC_MPC_RND_MODE
);
2185 rc
= gfc_check_real_range (mpc_realref (result
->value
.complex), kind
);
2187 if (rc
== ARITH_UNDERFLOW
)
2189 if (gfc_option
.warn_underflow
)
2190 gfc_warning (gfc_arith_error (rc
), &src
->where
);
2191 mpfr_set_ui (mpc_realref (result
->value
.complex), 0, GFC_RND_MODE
);
2193 else if (rc
!= ARITH_OK
)
2195 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2196 gfc_free_expr (result
);
2200 rc
= gfc_check_real_range (mpc_imagref (result
->value
.complex), kind
);
2202 if (rc
== ARITH_UNDERFLOW
)
2204 if (gfc_option
.warn_underflow
)
2205 gfc_warning (gfc_arith_error (rc
), &src
->where
);
2206 mpfr_set_ui (mpc_imagref (result
->value
.complex), 0, GFC_RND_MODE
);
2208 else if (rc
!= ARITH_OK
)
2210 arith_error (rc
, &src
->ts
, &result
->ts
, &src
->where
);
2211 gfc_free_expr (result
);
2219 /* Logical kind conversion. */
2222 gfc_log2log (gfc_expr
*src
, int kind
)
2226 result
= gfc_get_constant_expr (BT_LOGICAL
, kind
, &src
->where
);
2227 result
->value
.logical
= src
->value
.logical
;
2233 /* Convert logical to integer. */
2236 gfc_log2int (gfc_expr
*src
, int kind
)
2240 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &src
->where
);
2241 mpz_set_si (result
->value
.integer
, src
->value
.logical
);
2247 /* Convert integer to logical. */
2250 gfc_int2log (gfc_expr
*src
, int kind
)
2254 result
= gfc_get_constant_expr (BT_LOGICAL
, kind
, &src
->where
);
2255 result
->value
.logical
= (mpz_cmp_si (src
->value
.integer
, 0) != 0);
2261 /* Helper function to set the representation in a Hollerith conversion.
2262 This assumes that the ts.type and ts.kind of the result have already
2266 hollerith2representation (gfc_expr
*result
, gfc_expr
*src
)
2268 int src_len
, result_len
;
2270 src_len
= src
->representation
.length
- src
->ts
.u
.pad
;
2271 result_len
= gfc_target_expr_size (result
);
2273 if (src_len
> result_len
)
2275 gfc_warning ("The Hollerith constant at %L is too long to convert to %s",
2276 &src
->where
, gfc_typename(&result
->ts
));
2279 result
->representation
.string
= XCNEWVEC (char, result_len
+ 1);
2280 memcpy (result
->representation
.string
, src
->representation
.string
,
2281 MIN (result_len
, src_len
));
2283 if (src_len
< result_len
)
2284 memset (&result
->representation
.string
[src_len
], ' ', result_len
- src_len
);
2286 result
->representation
.string
[result_len
] = '\0'; /* For debugger */
2287 result
->representation
.length
= result_len
;
2291 /* Convert Hollerith to integer. The constant will be padded or truncated. */
2294 gfc_hollerith2int (gfc_expr
*src
, int kind
)
2297 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &src
->where
);
2299 hollerith2representation (result
, src
);
2300 gfc_interpret_integer (kind
, (unsigned char *) result
->representation
.string
,
2301 result
->representation
.length
, result
->value
.integer
);
2307 /* Convert Hollerith to real. The constant will be padded or truncated. */
2310 gfc_hollerith2real (gfc_expr
*src
, int kind
)
2313 result
= gfc_get_constant_expr (BT_REAL
, kind
, &src
->where
);
2315 hollerith2representation (result
, src
);
2316 gfc_interpret_float (kind
, (unsigned char *) result
->representation
.string
,
2317 result
->representation
.length
, result
->value
.real
);
2323 /* Convert Hollerith to complex. The constant will be padded or truncated. */
2326 gfc_hollerith2complex (gfc_expr
*src
, int kind
)
2329 result
= gfc_get_constant_expr (BT_COMPLEX
, kind
, &src
->where
);
2331 hollerith2representation (result
, src
);
2332 gfc_interpret_complex (kind
, (unsigned char *) result
->representation
.string
,
2333 result
->representation
.length
, result
->value
.complex);
2339 /* Convert Hollerith to character. */
2342 gfc_hollerith2character (gfc_expr
*src
, int kind
)
2346 result
= gfc_copy_expr (src
);
2347 result
->ts
.type
= BT_CHARACTER
;
2348 result
->ts
.kind
= kind
;
2350 result
->value
.character
.length
= result
->representation
.length
;
2351 result
->value
.character
.string
2352 = gfc_char_to_widechar (result
->representation
.string
);
2358 /* Convert Hollerith to logical. The constant will be padded or truncated. */
2361 gfc_hollerith2logical (gfc_expr
*src
, int kind
)
2364 result
= gfc_get_constant_expr (BT_LOGICAL
, kind
, &src
->where
);
2366 hollerith2representation (result
, src
);
2367 gfc_interpret_logical (kind
, (unsigned char *) result
->representation
.string
,
2368 result
->representation
.length
, &result
->value
.logical
);