2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Since target arithmetic must be done on the host, there has to
23 be some way of evaluating arithmetic expressions as the host
24 would evaluate them. We use the GNU MP library and the MPFR
25 library to do arithmetic, and this file provides the interface. */
32 #include "target-memory.h"
34 /* MPFR does not have a direct replacement for mpz_set_f() from GMP.
35 It's easily implemented with a few calls though. */
38 gfc_mpfr_to_mpz (mpz_t z, mpfr_t x, locus *where)
42 if (mpfr_inf_p (x) || mpfr_nan_p (x))
44 gfc_error ("Conversion of an Infinity or Not-a-Number at %L "
50 e = mpfr_get_z_exp (z, x);
53 mpz_mul_2exp (z, z, e);
55 mpz_tdiv_q_2exp (z, z, -e);
59 /* Set the model number precision by the requested KIND. */
62 gfc_set_model_kind (int kind)
64 int index = gfc_validate_kind (BT_REAL, kind, false);
67 base2prec = gfc_real_kinds[index].digits;
68 if (gfc_real_kinds[index].radix != 2)
69 base2prec *= gfc_real_kinds[index].radix / 2;
70 mpfr_set_default_prec (base2prec);
74 /* Set the model number precision from mpfr_t x. */
77 gfc_set_model (mpfr_t x)
79 mpfr_set_default_prec (mpfr_get_prec (x));
83 /* Given an arithmetic error code, return a pointer to a string that
84 explains the error. */
87 gfc_arith_error (arith code)
94 p = _("Arithmetic OK at %L");
97 p = _("Arithmetic overflow at %L");
100 p = _("Arithmetic underflow at %L");
103 p = _("Arithmetic NaN at %L");
106 p = _("Division by zero at %L");
108 case ARITH_INCOMMENSURATE:
109 p = _("Array operands are incommensurate at %L");
111 case ARITH_ASYMMETRIC:
113 _("Integer outside symmetric range implied by Standard Fortran at %L");
116 gfc_internal_error ("gfc_arith_error(): Bad error code");
123 /* Get things ready to do math. */
126 gfc_arith_init_1 (void)
128 gfc_integer_info *int_info;
129 gfc_real_info *real_info;
133 mpfr_set_default_prec (128);
136 /* Convert the minimum and maximum values for each kind into their
137 GNU MP representation. */
138 for (int_info = gfc_integer_kinds; int_info->kind != 0; int_info++)
141 mpz_init (int_info->huge);
142 mpz_set_ui (int_info->huge, int_info->radix);
143 mpz_pow_ui (int_info->huge, int_info->huge, int_info->digits);
144 mpz_sub_ui (int_info->huge, int_info->huge, 1);
146 /* These are the numbers that are actually representable by the
147 target. For bases other than two, this needs to be changed. */
148 if (int_info->radix != 2)
149 gfc_internal_error ("Fix min_int calculation");
151 /* See PRs 13490 and 17912, related to integer ranges.
152 The pedantic_min_int exists for range checking when a program
153 is compiled with -pedantic, and reflects the belief that
154 Standard Fortran requires integers to be symmetrical, i.e.
155 every negative integer must have a representable positive
156 absolute value, and vice versa. */
158 mpz_init (int_info->pedantic_min_int);
159 mpz_neg (int_info->pedantic_min_int, int_info->huge);
161 mpz_init (int_info->min_int);
162 mpz_sub_ui (int_info->min_int, int_info->pedantic_min_int, 1);
165 mpfr_set_z (a, int_info->huge, GFC_RND_MODE);
166 mpfr_log10 (a, a, GFC_RND_MODE);
168 int_info->range = (int) mpfr_get_si (a, GFC_RND_MODE);
173 for (real_info = gfc_real_kinds; real_info->kind != 0; real_info++)
175 gfc_set_model_kind (real_info->kind);
180 /* huge(x) = (1 - b**(-p)) * b**(emax-1) * b */
182 mpfr_init (real_info->huge);
183 mpfr_set_ui (real_info->huge, 1, GFC_RND_MODE);
184 mpfr_set_ui (a, real_info->radix, GFC_RND_MODE);
185 mpfr_pow_si (a, a, -real_info->digits, GFC_RND_MODE);
186 mpfr_sub (real_info->huge, real_info->huge, a, GFC_RND_MODE);
189 mpfr_set_ui (a, real_info->radix, GFC_RND_MODE);
190 mpfr_pow_ui (a, a, real_info->max_exponent - 1, GFC_RND_MODE);
192 /* (1 - b**(-p)) * b**(emax-1) */
193 mpfr_mul (real_info->huge, real_info->huge, a, GFC_RND_MODE);
195 /* (1 - b**(-p)) * b**(emax-1) * b */
196 mpfr_mul_ui (real_info->huge, real_info->huge, real_info->radix,
199 /* tiny(x) = b**(emin-1) */
200 mpfr_init (real_info->tiny);
201 mpfr_set_ui (real_info->tiny, real_info->radix, GFC_RND_MODE);
202 mpfr_pow_si (real_info->tiny, real_info->tiny,
203 real_info->min_exponent - 1, GFC_RND_MODE);
205 /* subnormal (x) = b**(emin - digit) */
206 mpfr_init (real_info->subnormal);
207 mpfr_set_ui (real_info->subnormal, real_info->radix, GFC_RND_MODE);
208 mpfr_pow_si (real_info->subnormal, real_info->subnormal,
209 real_info->min_exponent - real_info->digits, GFC_RND_MODE);
211 /* epsilon(x) = b**(1-p) */
212 mpfr_init (real_info->epsilon);
213 mpfr_set_ui (real_info->epsilon, real_info->radix, GFC_RND_MODE);
214 mpfr_pow_si (real_info->epsilon, real_info->epsilon,
215 1 - real_info->digits, GFC_RND_MODE);
217 /* range(x) = int(min(log10(huge(x)), -log10(tiny)) */
218 mpfr_log10 (a, real_info->huge, GFC_RND_MODE);
219 mpfr_log10 (b, real_info->tiny, GFC_RND_MODE);
220 mpfr_neg (b, b, GFC_RND_MODE);
223 mpfr_min (a, a, b, GFC_RND_MODE);
225 real_info->range = (int) mpfr_get_si (a, GFC_RND_MODE);
227 /* precision(x) = int((p - 1) * log10(b)) + k */
228 mpfr_set_ui (a, real_info->radix, GFC_RND_MODE);
229 mpfr_log10 (a, a, GFC_RND_MODE);
230 mpfr_mul_ui (a, a, real_info->digits - 1, GFC_RND_MODE);
232 real_info->precision = (int) mpfr_get_si (a, GFC_RND_MODE);
234 /* If the radix is an integral power of 10, add one to the precision. */
235 for (i = 10; i <= real_info->radix; i *= 10)
236 if (i == real_info->radix)
237 real_info->precision++;
239 mpfr_clears (a, b, NULL);
244 /* Clean up, get rid of numeric constants. */
247 gfc_arith_done_1 (void)
249 gfc_integer_info *ip;
252 for (ip = gfc_integer_kinds; ip->kind; ip++)
254 mpz_clear (ip->min_int);
255 mpz_clear (ip->pedantic_min_int);
256 mpz_clear (ip->huge);
259 for (rp = gfc_real_kinds; rp->kind; rp++)
260 mpfr_clears (rp->epsilon, rp->huge, rp->tiny, rp->subnormal, NULL);
264 /* Given a wide character value and a character kind, determine whether
265 the character is representable for that kind. */
267 gfc_check_character_range (gfc_char_t c, int kind)
269 /* As wide characters are stored as 32-bit values, they're all
270 representable in UCS=4. */
275 return c <= 255 ? true : false;
281 /* Given an integer and a kind, make sure that the integer lies within
282 the range of the kind. Returns ARITH_OK, ARITH_ASYMMETRIC or
286 gfc_check_integer_range (mpz_t p, int kind)
291 i = gfc_validate_kind (BT_INTEGER, kind, false);
296 if (mpz_cmp (p, gfc_integer_kinds[i].pedantic_min_int) < 0)
297 result = ARITH_ASYMMETRIC;
301 if (gfc_option.flag_range_check == 0)
304 if (mpz_cmp (p, gfc_integer_kinds[i].min_int) < 0
305 || mpz_cmp (p, gfc_integer_kinds[i].huge) > 0)
306 result = ARITH_OVERFLOW;
312 /* Given a real and a kind, make sure that the real lies within the
313 range of the kind. Returns ARITH_OK, ARITH_OVERFLOW or
317 gfc_check_real_range (mpfr_t p, int kind)
323 i = gfc_validate_kind (BT_REAL, kind, false);
327 mpfr_abs (q, p, GFC_RND_MODE);
333 if (gfc_option.flag_range_check != 0)
334 retval = ARITH_OVERFLOW;
336 else if (mpfr_nan_p (p))
338 if (gfc_option.flag_range_check != 0)
341 else if (mpfr_sgn (q) == 0)
346 else if (mpfr_cmp (q, gfc_real_kinds[i].huge) > 0)
348 if (gfc_option.flag_range_check == 0)
349 mpfr_set_inf (p, mpfr_sgn (p));
351 retval = ARITH_OVERFLOW;
353 else if (mpfr_cmp (q, gfc_real_kinds[i].subnormal) < 0)
355 if (gfc_option.flag_range_check == 0)
357 if (mpfr_sgn (p) < 0)
359 mpfr_set_ui (p, 0, GFC_RND_MODE);
360 mpfr_set_si (q, -1, GFC_RND_MODE);
361 mpfr_copysign (p, p, q, GFC_RND_MODE);
364 mpfr_set_ui (p, 0, GFC_RND_MODE);
367 retval = ARITH_UNDERFLOW;
369 else if (mpfr_cmp (q, gfc_real_kinds[i].tiny) < 0)
374 /* Save current values of emin and emax. */
375 emin = mpfr_get_emin ();
376 emax = mpfr_get_emax ();
378 /* Set emin and emax for the current model number. */
379 en = gfc_real_kinds[i].min_exponent - gfc_real_kinds[i].digits + 1;
380 mpfr_set_emin ((mp_exp_t) en);
381 mpfr_set_emax ((mp_exp_t) gfc_real_kinds[i].max_exponent);
382 mpfr_check_range (q, 0, GFC_RND_MODE);
383 mpfr_subnormalize (q, 0, GFC_RND_MODE);
385 /* Reset emin and emax. */
386 mpfr_set_emin (emin);
387 mpfr_set_emax (emax);
389 /* Copy sign if needed. */
390 if (mpfr_sgn (p) < 0)
391 mpfr_neg (p, q, GMP_RNDN);
393 mpfr_set (p, q, GMP_RNDN);
402 /* Function to return a constant expression node of a given type and kind. */
405 gfc_constant_result (bt type, int kind, locus *where)
410 gfc_internal_error ("gfc_constant_result(): locus 'where' cannot be NULL");
412 result = gfc_get_expr ();
414 result->expr_type = EXPR_CONSTANT;
415 result->ts.type = type;
416 result->ts.kind = kind;
417 result->where = *where;
422 mpz_init (result->value.integer);
426 gfc_set_model_kind (kind);
427 mpfr_init (result->value.real);
431 gfc_set_model_kind (kind);
432 mpfr_init (result->value.complex.r);
433 mpfr_init (result->value.complex.i);
444 /* Low-level arithmetic functions. All of these subroutines assume
445 that all operands are of the same type and return an operand of the
446 same type. The other thing about these subroutines is that they
447 can fail in various ways -- overflow, underflow, division by zero,
448 zero raised to the zero, etc. */
451 gfc_arith_not (gfc_expr *op1, gfc_expr **resultp)
455 result = gfc_constant_result (BT_LOGICAL, op1->ts.kind, &op1->where);
456 result->value.logical = !op1->value.logical;
464 gfc_arith_and (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
468 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
470 result->value.logical = op1->value.logical && op2->value.logical;
478 gfc_arith_or (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
482 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
484 result->value.logical = op1->value.logical || op2->value.logical;
492 gfc_arith_eqv (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
496 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
498 result->value.logical = op1->value.logical == op2->value.logical;
506 gfc_arith_neqv (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
510 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
512 result->value.logical = op1->value.logical != op2->value.logical;
519 /* Make sure a constant numeric expression is within the range for
520 its type and kind. Note that there's also a gfc_check_range(),
521 but that one deals with the intrinsic RANGE function. */
524 gfc_range_check (gfc_expr *e)
532 rc = gfc_check_integer_range (e->value.integer, e->ts.kind);
536 rc = gfc_check_real_range (e->value.real, e->ts.kind);
537 if (rc == ARITH_UNDERFLOW)
538 mpfr_set_ui (e->value.real, 0, GFC_RND_MODE);
539 if (rc == ARITH_OVERFLOW)
540 mpfr_set_inf (e->value.real, mpfr_sgn (e->value.real));
542 mpfr_set_nan (e->value.real);
546 rc = gfc_check_real_range (e->value.complex.r, e->ts.kind);
547 if (rc == ARITH_UNDERFLOW)
548 mpfr_set_ui (e->value.complex.r, 0, GFC_RND_MODE);
549 if (rc == ARITH_OVERFLOW)
550 mpfr_set_inf (e->value.complex.r, mpfr_sgn (e->value.complex.r));
552 mpfr_set_nan (e->value.complex.r);
554 rc2 = gfc_check_real_range (e->value.complex.i, e->ts.kind);
555 if (rc == ARITH_UNDERFLOW)
556 mpfr_set_ui (e->value.complex.i, 0, GFC_RND_MODE);
557 if (rc == ARITH_OVERFLOW)
558 mpfr_set_inf (e->value.complex.i, mpfr_sgn (e->value.complex.i));
560 mpfr_set_nan (e->value.complex.i);
567 gfc_internal_error ("gfc_range_check(): Bad type");
574 /* Several of the following routines use the same set of statements to
575 check the validity of the result. Encapsulate the checking here. */
578 check_result (arith rc, gfc_expr *x, gfc_expr *r, gfc_expr **rp)
582 if (val == ARITH_UNDERFLOW)
584 if (gfc_option.warn_underflow)
585 gfc_warning (gfc_arith_error (val), &x->where);
589 if (val == ARITH_ASYMMETRIC)
591 gfc_warning (gfc_arith_error (val), &x->where);
604 /* It may seem silly to have a subroutine that actually computes the
605 unary plus of a constant, but it prevents us from making exceptions
606 in the code elsewhere. Used for unary plus and parenthesized
610 gfc_arith_identity (gfc_expr *op1, gfc_expr **resultp)
612 *resultp = gfc_copy_expr (op1);
618 gfc_arith_uminus (gfc_expr *op1, gfc_expr **resultp)
623 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
625 switch (op1->ts.type)
628 mpz_neg (result->value.integer, op1->value.integer);
632 mpfr_neg (result->value.real, op1->value.real, GFC_RND_MODE);
636 mpfr_neg (result->value.complex.r, op1->value.complex.r, GFC_RND_MODE);
637 mpfr_neg (result->value.complex.i, op1->value.complex.i, GFC_RND_MODE);
641 gfc_internal_error ("gfc_arith_uminus(): Bad basic type");
644 rc = gfc_range_check (result);
646 return check_result (rc, op1, result, resultp);
651 gfc_arith_plus (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
656 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
658 switch (op1->ts.type)
661 mpz_add (result->value.integer, op1->value.integer, op2->value.integer);
665 mpfr_add (result->value.real, op1->value.real, op2->value.real,
670 mpfr_add (result->value.complex.r, op1->value.complex.r,
671 op2->value.complex.r, GFC_RND_MODE);
673 mpfr_add (result->value.complex.i, op1->value.complex.i,
674 op2->value.complex.i, GFC_RND_MODE);
678 gfc_internal_error ("gfc_arith_plus(): Bad basic type");
681 rc = gfc_range_check (result);
683 return check_result (rc, op1, result, resultp);
688 gfc_arith_minus (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
693 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
695 switch (op1->ts.type)
698 mpz_sub (result->value.integer, op1->value.integer, op2->value.integer);
702 mpfr_sub (result->value.real, op1->value.real, op2->value.real,
707 mpfr_sub (result->value.complex.r, op1->value.complex.r,
708 op2->value.complex.r, GFC_RND_MODE);
710 mpfr_sub (result->value.complex.i, op1->value.complex.i,
711 op2->value.complex.i, GFC_RND_MODE);
715 gfc_internal_error ("gfc_arith_minus(): Bad basic type");
718 rc = gfc_range_check (result);
720 return check_result (rc, op1, result, resultp);
725 gfc_arith_times (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
731 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
733 switch (op1->ts.type)
736 mpz_mul (result->value.integer, op1->value.integer, op2->value.integer);
740 mpfr_mul (result->value.real, op1->value.real, op2->value.real,
745 gfc_set_model (op1->value.complex.r);
749 mpfr_mul (x, op1->value.complex.r, op2->value.complex.r, GFC_RND_MODE);
750 mpfr_mul (y, op1->value.complex.i, op2->value.complex.i, GFC_RND_MODE);
751 mpfr_sub (result->value.complex.r, x, y, GFC_RND_MODE);
753 mpfr_mul (x, op1->value.complex.r, op2->value.complex.i, GFC_RND_MODE);
754 mpfr_mul (y, op1->value.complex.i, op2->value.complex.r, GFC_RND_MODE);
755 mpfr_add (result->value.complex.i, x, y, GFC_RND_MODE);
757 mpfr_clears (x, y, NULL);
761 gfc_internal_error ("gfc_arith_times(): Bad basic type");
764 rc = gfc_range_check (result);
766 return check_result (rc, op1, result, resultp);
771 gfc_arith_divide (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
779 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
781 switch (op1->ts.type)
784 if (mpz_sgn (op2->value.integer) == 0)
790 mpz_tdiv_q (result->value.integer, op1->value.integer,
795 if (mpfr_sgn (op2->value.real) == 0 && gfc_option.flag_range_check == 1)
801 mpfr_div (result->value.real, op1->value.real, op2->value.real,
806 if (mpfr_sgn (op2->value.complex.r) == 0
807 && mpfr_sgn (op2->value.complex.i) == 0
808 && gfc_option.flag_range_check == 1)
814 gfc_set_model (op1->value.complex.r);
819 mpfr_mul (x, op2->value.complex.r, op2->value.complex.r, GFC_RND_MODE);
820 mpfr_mul (y, op2->value.complex.i, op2->value.complex.i, GFC_RND_MODE);
821 mpfr_add (div, x, y, GFC_RND_MODE);
823 mpfr_mul (x, op1->value.complex.r, op2->value.complex.r, GFC_RND_MODE);
824 mpfr_mul (y, op1->value.complex.i, op2->value.complex.i, GFC_RND_MODE);
825 mpfr_add (result->value.complex.r, x, y, GFC_RND_MODE);
826 mpfr_div (result->value.complex.r, result->value.complex.r, div,
829 mpfr_mul (x, op1->value.complex.i, op2->value.complex.r, GFC_RND_MODE);
830 mpfr_mul (y, op1->value.complex.r, op2->value.complex.i, GFC_RND_MODE);
831 mpfr_sub (result->value.complex.i, x, y, GFC_RND_MODE);
832 mpfr_div (result->value.complex.i, result->value.complex.i, div,
835 mpfr_clears (x, y, div, NULL);
839 gfc_internal_error ("gfc_arith_divide(): Bad basic type");
843 rc = gfc_range_check (result);
845 return check_result (rc, op1, result, resultp);
849 /* Compute the reciprocal of a complex number (guaranteed nonzero). */
852 complex_reciprocal (gfc_expr *op)
856 gfc_set_model (op->value.complex.r);
860 mpfr_mul (mod, op->value.complex.r, op->value.complex.r, GFC_RND_MODE);
861 mpfr_mul (tmp, op->value.complex.i, op->value.complex.i, GFC_RND_MODE);
862 mpfr_add (mod, mod, tmp, GFC_RND_MODE);
864 mpfr_div (op->value.complex.r, op->value.complex.r, mod, GFC_RND_MODE);
866 mpfr_neg (op->value.complex.i, op->value.complex.i, GFC_RND_MODE);
867 mpfr_div (op->value.complex.i, op->value.complex.i, mod, GFC_RND_MODE);
869 mpfr_clears (tmp, mod, NULL);
873 /* Raise a complex number to positive power (power > 0).
874 This function will modify the content of power.
876 Use Binary Method, which is not an optimal but a simple and reasonable
877 arithmetic. See section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
878 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
879 3rd Edition, 1998. */
882 complex_pow (gfc_expr *result, gfc_expr *base, mpz_t power)
884 mpfr_t x_r, x_i, tmp, re, im;
886 gfc_set_model (base->value.complex.r);
894 mpfr_set_ui (result->value.complex.r, 1, GFC_RND_MODE);
895 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
898 mpfr_set (x_r, base->value.complex.r, GFC_RND_MODE);
899 mpfr_set (x_i, base->value.complex.i, GFC_RND_MODE);
901 /* Macro for complex multiplication. We have to take care that
902 res_r/res_i and a_r/a_i can (and will) be the same variable. */
903 #define CMULT(res_r,res_i,a_r,a_i,b_r,b_i) \
904 mpfr_mul (re, a_r, b_r, GFC_RND_MODE), \
905 mpfr_mul (tmp, a_i, b_i, GFC_RND_MODE), \
906 mpfr_sub (re, re, tmp, GFC_RND_MODE), \
908 mpfr_mul (im, a_r, b_i, GFC_RND_MODE), \
909 mpfr_mul (tmp, a_i, b_r, GFC_RND_MODE), \
910 mpfr_add (res_i, im, tmp, GFC_RND_MODE), \
911 mpfr_set (res_r, re, GFC_RND_MODE)
913 #define res_r result->value.complex.r
914 #define res_i result->value.complex.i
916 /* for (; power > 0; x *= x) */
917 for (; mpz_cmp_si (power, 0) > 0; CMULT(x_r,x_i,x_r,x_i,x_r,x_i))
919 /* if (power & 1) res = res * x; */
920 if (mpz_congruent_ui_p (power, 1, 2))
921 CMULT(res_r,res_i,res_r,res_i,x_r,x_i);
924 mpz_fdiv_q_ui (power, power, 2);
931 mpfr_clears (x_r, x_i, tmp, re, im, NULL);
935 /* Raise a number to an integer power. */
938 gfc_arith_power (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
944 gcc_assert (op2->expr_type == EXPR_CONSTANT && op2->ts.type == BT_INTEGER);
947 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
948 power_sign = mpz_sgn (op2->value.integer);
952 /* Handle something to the zeroth power. Since we're dealing
953 with integral exponents, there is no ambiguity in the
954 limiting procedure used to determine the value of 0**0. */
955 switch (op1->ts.type)
958 mpz_set_ui (result->value.integer, 1);
962 mpfr_set_ui (result->value.real, 1, GFC_RND_MODE);
966 mpfr_set_ui (result->value.complex.r, 1, GFC_RND_MODE);
967 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
971 gfc_internal_error ("gfc_arith_power(): Bad base");
976 switch (op1->ts.type)
982 /* First, we simplify the cases of op1 == 1, 0 or -1. */
983 if (mpz_cmp_si (op1->value.integer, 1) == 0)
986 mpz_set_si (result->value.integer, 1);
988 else if (mpz_cmp_si (op1->value.integer, 0) == 0)
990 /* 0**op2 == 0, if op2 > 0
991 0**op2 overflow, if op2 < 0 ; in that case, we
992 set the result to 0 and return ARITH_DIV0. */
993 mpz_set_si (result->value.integer, 0);
994 if (mpz_cmp_si (op2->value.integer, 0) < 0)
997 else if (mpz_cmp_si (op1->value.integer, -1) == 0)
999 /* (-1)**op2 == (-1)**(mod(op2,2)) */
1000 unsigned int odd = mpz_fdiv_ui (op2->value.integer, 2);
1002 mpz_set_si (result->value.integer, -1);
1004 mpz_set_si (result->value.integer, 1);
1006 /* Then, we take care of op2 < 0. */
1007 else if (mpz_cmp_si (op2->value.integer, 0) < 0)
1009 /* if op2 < 0, op1**op2 == 0 because abs(op1) > 1. */
1010 mpz_set_si (result->value.integer, 0);
1012 else if (gfc_extract_int (op2, &power) != NULL)
1014 /* If op2 doesn't fit in an int, the exponentiation will
1015 overflow, because op2 > 0 and abs(op1) > 1. */
1017 int i = gfc_validate_kind (BT_INTEGER, result->ts.kind, false);
1019 if (gfc_option.flag_range_check)
1020 rc = ARITH_OVERFLOW;
1022 /* Still, we want to give the same value as the processor. */
1024 mpz_add_ui (max, gfc_integer_kinds[i].huge, 1);
1025 mpz_mul_ui (max, max, 2);
1026 mpz_powm (result->value.integer, op1->value.integer,
1027 op2->value.integer, max);
1031 mpz_pow_ui (result->value.integer, op1->value.integer, power);
1036 mpfr_pow_z (result->value.real, op1->value.real, op2->value.integer,
1044 /* Compute op1**abs(op2) */
1046 mpz_abs (apower, op2->value.integer);
1047 complex_pow (result, op1, apower);
1050 /* If (op2 < 0), compute the inverse. */
1052 complex_reciprocal (result);
1063 rc = gfc_range_check (result);
1065 return check_result (rc, op1, result, resultp);
1069 /* Concatenate two string constants. */
1072 gfc_arith_concat (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1077 gcc_assert (op1->ts.kind == op2->ts.kind);
1078 result = gfc_constant_result (BT_CHARACTER, op1->ts.kind,
1081 len = op1->value.character.length + op2->value.character.length;
1083 result->value.character.string = gfc_get_wide_string (len + 1);
1084 result->value.character.length = len;
1086 memcpy (result->value.character.string, op1->value.character.string,
1087 op1->value.character.length * sizeof (gfc_char_t));
1089 memcpy (&result->value.character.string[op1->value.character.length],
1090 op2->value.character.string,
1091 op2->value.character.length * sizeof (gfc_char_t));
1093 result->value.character.string[len] = '\0';
1100 /* Comparison between real values; returns 0 if (op1 .op. op2) is true.
1101 This function mimics mpfr_cmp but takes NaN into account. */
1104 compare_real (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1110 rc = mpfr_equal_p (op1->value.real, op2->value.real) ? 0 : 1;
1113 rc = mpfr_greater_p (op1->value.real, op2->value.real) ? 1 : -1;
1116 rc = mpfr_greaterequal_p (op1->value.real, op2->value.real) ? 1 : -1;
1119 rc = mpfr_less_p (op1->value.real, op2->value.real) ? -1 : 1;
1122 rc = mpfr_lessequal_p (op1->value.real, op2->value.real) ? -1 : 1;
1125 gfc_internal_error ("compare_real(): Bad operator");
1131 /* Comparison operators. Assumes that the two expression nodes
1132 contain two constants of the same type. The op argument is
1133 needed to handle NaN correctly. */
1136 gfc_compare_expr (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1140 switch (op1->ts.type)
1143 rc = mpz_cmp (op1->value.integer, op2->value.integer);
1147 rc = compare_real (op1, op2, op);
1151 rc = gfc_compare_string (op1, op2);
1155 rc = ((!op1->value.logical && op2->value.logical)
1156 || (op1->value.logical && !op2->value.logical));
1160 gfc_internal_error ("gfc_compare_expr(): Bad basic type");
1167 /* Compare a pair of complex numbers. Naturally, this is only for
1168 equality and inequality. */
1171 compare_complex (gfc_expr *op1, gfc_expr *op2)
1173 return (mpfr_equal_p (op1->value.complex.r, op2->value.complex.r)
1174 && mpfr_equal_p (op1->value.complex.i, op2->value.complex.i));
1178 /* Given two constant strings and the inverse collating sequence, compare the
1179 strings. We return -1 for a < b, 0 for a == b and 1 for a > b.
1180 We use the processor's default collating sequence. */
1183 gfc_compare_string (gfc_expr *a, gfc_expr *b)
1185 int len, alen, blen, i;
1188 alen = a->value.character.length;
1189 blen = b->value.character.length;
1191 len = MAX(alen, blen);
1193 for (i = 0; i < len; i++)
1195 ac = ((i < alen) ? a->value.character.string[i] : ' ');
1196 bc = ((i < blen) ? b->value.character.string[i] : ' ');
1204 /* Strings are equal */
1210 gfc_compare_with_Cstring (gfc_expr *a, const char *b, bool case_sensitive)
1212 int len, alen, blen, i;
1215 alen = a->value.character.length;
1218 len = MAX(alen, blen);
1220 for (i = 0; i < len; i++)
1222 ac = ((i < alen) ? a->value.character.string[i] : ' ');
1223 bc = ((i < blen) ? b[i] : ' ');
1225 if (!case_sensitive)
1237 /* Strings are equal */
1242 /* Specific comparison subroutines. */
1245 gfc_arith_eq (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1249 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
1251 result->value.logical = (op1->ts.type == BT_COMPLEX)
1252 ? compare_complex (op1, op2)
1253 : (gfc_compare_expr (op1, op2, INTRINSIC_EQ) == 0);
1261 gfc_arith_ne (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1265 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
1267 result->value.logical = (op1->ts.type == BT_COMPLEX)
1268 ? !compare_complex (op1, op2)
1269 : (gfc_compare_expr (op1, op2, INTRINSIC_EQ) != 0);
1277 gfc_arith_gt (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1281 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
1283 result->value.logical = (gfc_compare_expr (op1, op2, INTRINSIC_GT) > 0);
1291 gfc_arith_ge (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1295 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
1297 result->value.logical = (gfc_compare_expr (op1, op2, INTRINSIC_GE) >= 0);
1305 gfc_arith_lt (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1309 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
1311 result->value.logical = (gfc_compare_expr (op1, op2, INTRINSIC_LT) < 0);
1319 gfc_arith_le (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1323 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
1325 result->value.logical = (gfc_compare_expr (op1, op2, INTRINSIC_LE) <= 0);
1333 reduce_unary (arith (*eval) (gfc_expr *, gfc_expr **), gfc_expr *op,
1336 gfc_constructor *c, *head;
1340 if (op->expr_type == EXPR_CONSTANT)
1341 return eval (op, result);
1344 head = gfc_copy_constructor (op->value.constructor);
1346 for (c = head; c; c = c->next)
1348 rc = reduce_unary (eval, c->expr, &r);
1353 gfc_replace_expr (c->expr, r);
1357 gfc_free_constructor (head);
1360 r = gfc_get_expr ();
1361 r->expr_type = EXPR_ARRAY;
1362 r->value.constructor = head;
1363 r->shape = gfc_copy_shape (op->shape, op->rank);
1365 r->ts = head->expr->ts;
1366 r->where = op->where;
1377 reduce_binary_ac (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1378 gfc_expr *op1, gfc_expr *op2, gfc_expr **result)
1380 gfc_constructor *c, *head;
1384 head = gfc_copy_constructor (op1->value.constructor);
1387 for (c = head; c; c = c->next)
1389 if (c->expr->expr_type == EXPR_CONSTANT)
1390 rc = eval (c->expr, op2, &r);
1392 rc = reduce_binary_ac (eval, c->expr, op2, &r);
1397 gfc_replace_expr (c->expr, r);
1401 gfc_free_constructor (head);
1404 r = gfc_get_expr ();
1405 r->expr_type = EXPR_ARRAY;
1406 r->value.constructor = head;
1407 r->shape = gfc_copy_shape (op1->shape, op1->rank);
1409 r->ts = head->expr->ts;
1410 r->where = op1->where;
1411 r->rank = op1->rank;
1421 reduce_binary_ca (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1422 gfc_expr *op1, gfc_expr *op2, gfc_expr **result)
1424 gfc_constructor *c, *head;
1428 head = gfc_copy_constructor (op2->value.constructor);
1431 for (c = head; c; c = c->next)
1433 if (c->expr->expr_type == EXPR_CONSTANT)
1434 rc = eval (op1, c->expr, &r);
1436 rc = reduce_binary_ca (eval, op1, c->expr, &r);
1441 gfc_replace_expr (c->expr, r);
1445 gfc_free_constructor (head);
1448 r = gfc_get_expr ();
1449 r->expr_type = EXPR_ARRAY;
1450 r->value.constructor = head;
1451 r->shape = gfc_copy_shape (op2->shape, op2->rank);
1453 r->ts = head->expr->ts;
1454 r->where = op2->where;
1455 r->rank = op2->rank;
1464 /* We need a forward declaration of reduce_binary. */
1465 static arith reduce_binary (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1466 gfc_expr *op1, gfc_expr *op2, gfc_expr **result);
1470 reduce_binary_aa (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1471 gfc_expr *op1, gfc_expr *op2, gfc_expr **result)
1473 gfc_constructor *c, *d, *head;
1477 head = gfc_copy_constructor (op1->value.constructor);
1480 d = op2->value.constructor;
1482 if (gfc_check_conformance ("elemental binary operation", op1, op2)
1484 rc = ARITH_INCOMMENSURATE;
1487 for (c = head; c; c = c->next, d = d->next)
1491 rc = ARITH_INCOMMENSURATE;
1495 rc = reduce_binary (eval, c->expr, d->expr, &r);
1499 gfc_replace_expr (c->expr, r);
1503 rc = ARITH_INCOMMENSURATE;
1507 gfc_free_constructor (head);
1510 r = gfc_get_expr ();
1511 r->expr_type = EXPR_ARRAY;
1512 r->value.constructor = head;
1513 r->shape = gfc_copy_shape (op1->shape, op1->rank);
1515 r->ts = head->expr->ts;
1516 r->where = op1->where;
1517 r->rank = op1->rank;
1527 reduce_binary (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1528 gfc_expr *op1, gfc_expr *op2, gfc_expr **result)
1530 if (op1->expr_type == EXPR_CONSTANT && op2->expr_type == EXPR_CONSTANT)
1531 return eval (op1, op2, result);
1533 if (op1->expr_type == EXPR_CONSTANT && op2->expr_type == EXPR_ARRAY)
1534 return reduce_binary_ca (eval, op1, op2, result);
1536 if (op1->expr_type == EXPR_ARRAY && op2->expr_type == EXPR_CONSTANT)
1537 return reduce_binary_ac (eval, op1, op2, result);
1539 return reduce_binary_aa (eval, op1, op2, result);
1545 arith (*f2)(gfc_expr *, gfc_expr **);
1546 arith (*f3)(gfc_expr *, gfc_expr *, gfc_expr **);
1550 /* High level arithmetic subroutines. These subroutines go into
1551 eval_intrinsic(), which can do one of several things to its
1552 operands. If the operands are incompatible with the intrinsic
1553 operation, we return a node pointing to the operands and hope that
1554 an operator interface is found during resolution.
1556 If the operands are compatible and are constants, then we try doing
1557 the arithmetic. We also handle the cases where either or both
1558 operands are array constructors. */
1561 eval_intrinsic (gfc_intrinsic_op op,
1562 eval_f eval, gfc_expr *op1, gfc_expr *op2)
1564 gfc_expr temp, *result;
1568 gfc_clear_ts (&temp.ts);
1574 if (op1->ts.type != BT_LOGICAL)
1577 temp.ts.type = BT_LOGICAL;
1578 temp.ts.kind = gfc_default_logical_kind;
1582 /* Logical binary operators */
1585 case INTRINSIC_NEQV:
1587 if (op1->ts.type != BT_LOGICAL || op2->ts.type != BT_LOGICAL)
1590 temp.ts.type = BT_LOGICAL;
1591 temp.ts.kind = gfc_default_logical_kind;
1596 case INTRINSIC_UPLUS:
1597 case INTRINSIC_UMINUS:
1598 if (!gfc_numeric_ts (&op1->ts))
1605 case INTRINSIC_PARENTHESES:
1610 /* Additional restrictions for ordering relations. */
1612 case INTRINSIC_GE_OS:
1614 case INTRINSIC_LT_OS:
1616 case INTRINSIC_LE_OS:
1618 case INTRINSIC_GT_OS:
1619 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
1621 temp.ts.type = BT_LOGICAL;
1622 temp.ts.kind = gfc_default_logical_kind;
1628 case INTRINSIC_EQ_OS:
1630 case INTRINSIC_NE_OS:
1631 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
1634 temp.ts.type = BT_LOGICAL;
1635 temp.ts.kind = gfc_default_logical_kind;
1637 /* If kind mismatch, exit and we'll error out later. */
1638 if (op1->ts.kind != op2->ts.kind)
1645 /* Numeric binary */
1646 case INTRINSIC_PLUS:
1647 case INTRINSIC_MINUS:
1648 case INTRINSIC_TIMES:
1649 case INTRINSIC_DIVIDE:
1650 case INTRINSIC_POWER:
1651 if (!gfc_numeric_ts (&op1->ts) || !gfc_numeric_ts (&op2->ts))
1654 /* Insert any necessary type conversions to make the operands
1657 temp.expr_type = EXPR_OP;
1658 gfc_clear_ts (&temp.ts);
1659 temp.value.op.op = op;
1661 temp.value.op.op1 = op1;
1662 temp.value.op.op2 = op2;
1664 gfc_type_convert_binary (&temp);
1666 if (op == INTRINSIC_EQ || op == INTRINSIC_NE
1667 || op == INTRINSIC_GE || op == INTRINSIC_GT
1668 || op == INTRINSIC_LE || op == INTRINSIC_LT
1669 || op == INTRINSIC_EQ_OS || op == INTRINSIC_NE_OS
1670 || op == INTRINSIC_GE_OS || op == INTRINSIC_GT_OS
1671 || op == INTRINSIC_LE_OS || op == INTRINSIC_LT_OS)
1673 temp.ts.type = BT_LOGICAL;
1674 temp.ts.kind = gfc_default_logical_kind;
1680 /* Character binary */
1681 case INTRINSIC_CONCAT:
1682 if (op1->ts.type != BT_CHARACTER || op2->ts.type != BT_CHARACTER
1683 || op1->ts.kind != op2->ts.kind)
1686 temp.ts.type = BT_CHARACTER;
1687 temp.ts.kind = op1->ts.kind;
1691 case INTRINSIC_USER:
1695 gfc_internal_error ("eval_intrinsic(): Bad operator");
1698 /* Try to combine the operators. */
1699 if (op == INTRINSIC_POWER && op2->ts.type != BT_INTEGER)
1702 if (op1->expr_type != EXPR_CONSTANT
1703 && (op1->expr_type != EXPR_ARRAY
1704 || !gfc_is_constant_expr (op1) || !gfc_expanded_ac (op1)))
1708 && op2->expr_type != EXPR_CONSTANT
1709 && (op2->expr_type != EXPR_ARRAY
1710 || !gfc_is_constant_expr (op2) || !gfc_expanded_ac (op2)))
1714 rc = reduce_unary (eval.f2, op1, &result);
1716 rc = reduce_binary (eval.f3, op1, op2, &result);
1719 { /* Something went wrong. */
1720 gfc_error (gfc_arith_error (rc), &op1->where);
1724 gfc_free_expr (op1);
1725 gfc_free_expr (op2);
1729 /* Create a run-time expression. */
1730 result = gfc_get_expr ();
1731 result->ts = temp.ts;
1733 result->expr_type = EXPR_OP;
1734 result->value.op.op = op;
1736 result->value.op.op1 = op1;
1737 result->value.op.op2 = op2;
1739 result->where = op1->where;
1745 /* Modify type of expression for zero size array. */
1748 eval_type_intrinsic0 (gfc_intrinsic_op iop, gfc_expr *op)
1751 gfc_internal_error ("eval_type_intrinsic0(): op NULL");
1756 case INTRINSIC_GE_OS:
1758 case INTRINSIC_LT_OS:
1760 case INTRINSIC_LE_OS:
1762 case INTRINSIC_GT_OS:
1764 case INTRINSIC_EQ_OS:
1766 case INTRINSIC_NE_OS:
1767 op->ts.type = BT_LOGICAL;
1768 op->ts.kind = gfc_default_logical_kind;
1779 /* Return nonzero if the expression is a zero size array. */
1782 gfc_zero_size_array (gfc_expr *e)
1784 if (e->expr_type != EXPR_ARRAY)
1787 return e->value.constructor == NULL;
1791 /* Reduce a binary expression where at least one of the operands
1792 involves a zero-length array. Returns NULL if neither of the
1793 operands is a zero-length array. */
1796 reduce_binary0 (gfc_expr *op1, gfc_expr *op2)
1798 if (gfc_zero_size_array (op1))
1800 gfc_free_expr (op2);
1804 if (gfc_zero_size_array (op2))
1806 gfc_free_expr (op1);
1815 eval_intrinsic_f2 (gfc_intrinsic_op op,
1816 arith (*eval) (gfc_expr *, gfc_expr **),
1817 gfc_expr *op1, gfc_expr *op2)
1824 if (gfc_zero_size_array (op1))
1825 return eval_type_intrinsic0 (op, op1);
1829 result = reduce_binary0 (op1, op2);
1831 return eval_type_intrinsic0 (op, result);
1835 return eval_intrinsic (op, f, op1, op2);
1840 eval_intrinsic_f3 (gfc_intrinsic_op op,
1841 arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1842 gfc_expr *op1, gfc_expr *op2)
1847 result = reduce_binary0 (op1, op2);
1849 return eval_type_intrinsic0(op, result);
1852 return eval_intrinsic (op, f, op1, op2);
1857 gfc_parentheses (gfc_expr *op)
1859 if (gfc_is_constant_expr (op))
1862 return eval_intrinsic_f2 (INTRINSIC_PARENTHESES, gfc_arith_identity,
1867 gfc_uplus (gfc_expr *op)
1869 return eval_intrinsic_f2 (INTRINSIC_UPLUS, gfc_arith_identity, op, NULL);
1874 gfc_uminus (gfc_expr *op)
1876 return eval_intrinsic_f2 (INTRINSIC_UMINUS, gfc_arith_uminus, op, NULL);
1881 gfc_add (gfc_expr *op1, gfc_expr *op2)
1883 return eval_intrinsic_f3 (INTRINSIC_PLUS, gfc_arith_plus, op1, op2);
1888 gfc_subtract (gfc_expr *op1, gfc_expr *op2)
1890 return eval_intrinsic_f3 (INTRINSIC_MINUS, gfc_arith_minus, op1, op2);
1895 gfc_multiply (gfc_expr *op1, gfc_expr *op2)
1897 return eval_intrinsic_f3 (INTRINSIC_TIMES, gfc_arith_times, op1, op2);
1902 gfc_divide (gfc_expr *op1, gfc_expr *op2)
1904 return eval_intrinsic_f3 (INTRINSIC_DIVIDE, gfc_arith_divide, op1, op2);
1909 gfc_power (gfc_expr *op1, gfc_expr *op2)
1911 return eval_intrinsic_f3 (INTRINSIC_POWER, gfc_arith_power, op1, op2);
1916 gfc_concat (gfc_expr *op1, gfc_expr *op2)
1918 return eval_intrinsic_f3 (INTRINSIC_CONCAT, gfc_arith_concat, op1, op2);
1923 gfc_and (gfc_expr *op1, gfc_expr *op2)
1925 return eval_intrinsic_f3 (INTRINSIC_AND, gfc_arith_and, op1, op2);
1930 gfc_or (gfc_expr *op1, gfc_expr *op2)
1932 return eval_intrinsic_f3 (INTRINSIC_OR, gfc_arith_or, op1, op2);
1937 gfc_not (gfc_expr *op1)
1939 return eval_intrinsic_f2 (INTRINSIC_NOT, gfc_arith_not, op1, NULL);
1944 gfc_eqv (gfc_expr *op1, gfc_expr *op2)
1946 return eval_intrinsic_f3 (INTRINSIC_EQV, gfc_arith_eqv, op1, op2);
1951 gfc_neqv (gfc_expr *op1, gfc_expr *op2)
1953 return eval_intrinsic_f3 (INTRINSIC_NEQV, gfc_arith_neqv, op1, op2);
1958 gfc_eq (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1960 return eval_intrinsic_f3 (op, gfc_arith_eq, op1, op2);
1965 gfc_ne (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1967 return eval_intrinsic_f3 (op, gfc_arith_ne, op1, op2);
1972 gfc_gt (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1974 return eval_intrinsic_f3 (op, gfc_arith_gt, op1, op2);
1979 gfc_ge (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1981 return eval_intrinsic_f3 (op, gfc_arith_ge, op1, op2);
1986 gfc_lt (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1988 return eval_intrinsic_f3 (op, gfc_arith_lt, op1, op2);
1993 gfc_le (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1995 return eval_intrinsic_f3 (op, gfc_arith_le, op1, op2);
1999 /* Convert an integer string to an expression node. */
2002 gfc_convert_integer (const char *buffer, int kind, int radix, locus *where)
2007 e = gfc_constant_result (BT_INTEGER, kind, where);
2008 /* A leading plus is allowed, but not by mpz_set_str. */
2009 if (buffer[0] == '+')
2013 mpz_set_str (e->value.integer, t, radix);
2019 /* Convert a real string to an expression node. */
2022 gfc_convert_real (const char *buffer, int kind, locus *where)
2026 e = gfc_constant_result (BT_REAL, kind, where);
2027 mpfr_set_str (e->value.real, buffer, 10, GFC_RND_MODE);
2033 /* Convert a pair of real, constant expression nodes to a single
2034 complex expression node. */
2037 gfc_convert_complex (gfc_expr *real, gfc_expr *imag, int kind)
2041 e = gfc_constant_result (BT_COMPLEX, kind, &real->where);
2042 mpfr_set (e->value.complex.r, real->value.real, GFC_RND_MODE);
2043 mpfr_set (e->value.complex.i, imag->value.real, GFC_RND_MODE);
2049 /******* Simplification of intrinsic functions with constant arguments *****/
2052 /* Deal with an arithmetic error. */
2055 arith_error (arith rc, gfc_typespec *from, gfc_typespec *to, locus *where)
2060 gfc_error ("Arithmetic OK converting %s to %s at %L",
2061 gfc_typename (from), gfc_typename (to), where);
2063 case ARITH_OVERFLOW:
2064 gfc_error ("Arithmetic overflow converting %s to %s at %L. This check "
2065 "can be disabled with the option -fno-range-check",
2066 gfc_typename (from), gfc_typename (to), where);
2068 case ARITH_UNDERFLOW:
2069 gfc_error ("Arithmetic underflow converting %s to %s at %L. This check "
2070 "can be disabled with the option -fno-range-check",
2071 gfc_typename (from), gfc_typename (to), where);
2074 gfc_error ("Arithmetic NaN converting %s to %s at %L. This check "
2075 "can be disabled with the option -fno-range-check",
2076 gfc_typename (from), gfc_typename (to), where);
2079 gfc_error ("Division by zero converting %s to %s at %L",
2080 gfc_typename (from), gfc_typename (to), where);
2082 case ARITH_INCOMMENSURATE:
2083 gfc_error ("Array operands are incommensurate converting %s to %s at %L",
2084 gfc_typename (from), gfc_typename (to), where);
2086 case ARITH_ASYMMETRIC:
2087 gfc_error ("Integer outside symmetric range implied by Standard Fortran"
2088 " converting %s to %s at %L",
2089 gfc_typename (from), gfc_typename (to), where);
2092 gfc_internal_error ("gfc_arith_error(): Bad error code");
2095 /* TODO: Do something about the error, i.e., throw exception, return
2100 /* Convert integers to integers. */
2103 gfc_int2int (gfc_expr *src, int kind)
2108 result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2110 mpz_set (result->value.integer, src->value.integer);
2112 if ((rc = gfc_check_integer_range (result->value.integer, kind)) != ARITH_OK)
2114 if (rc == ARITH_ASYMMETRIC)
2116 gfc_warning (gfc_arith_error (rc), &src->where);
2120 arith_error (rc, &src->ts, &result->ts, &src->where);
2121 gfc_free_expr (result);
2130 /* Convert integers to reals. */
2133 gfc_int2real (gfc_expr *src, int kind)
2138 result = gfc_constant_result (BT_REAL, kind, &src->where);
2140 mpfr_set_z (result->value.real, src->value.integer, GFC_RND_MODE);
2142 if ((rc = gfc_check_real_range (result->value.real, kind)) != ARITH_OK)
2144 arith_error (rc, &src->ts, &result->ts, &src->where);
2145 gfc_free_expr (result);
2153 /* Convert default integer to default complex. */
2156 gfc_int2complex (gfc_expr *src, int kind)
2161 result = gfc_constant_result (BT_COMPLEX, kind, &src->where);
2163 mpfr_set_z (result->value.complex.r, src->value.integer, GFC_RND_MODE);
2164 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
2166 if ((rc = gfc_check_real_range (result->value.complex.r, kind)) != ARITH_OK)
2168 arith_error (rc, &src->ts, &result->ts, &src->where);
2169 gfc_free_expr (result);
2177 /* Convert default real to default integer. */
2180 gfc_real2int (gfc_expr *src, int kind)
2185 result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2187 gfc_mpfr_to_mpz (result->value.integer, src->value.real, &src->where);
2189 if ((rc = gfc_check_integer_range (result->value.integer, kind)) != ARITH_OK)
2191 arith_error (rc, &src->ts, &result->ts, &src->where);
2192 gfc_free_expr (result);
2200 /* Convert real to real. */
2203 gfc_real2real (gfc_expr *src, int kind)
2208 result = gfc_constant_result (BT_REAL, kind, &src->where);
2210 mpfr_set (result->value.real, src->value.real, GFC_RND_MODE);
2212 rc = gfc_check_real_range (result->value.real, kind);
2214 if (rc == ARITH_UNDERFLOW)
2216 if (gfc_option.warn_underflow)
2217 gfc_warning (gfc_arith_error (rc), &src->where);
2218 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
2220 else if (rc != ARITH_OK)
2222 arith_error (rc, &src->ts, &result->ts, &src->where);
2223 gfc_free_expr (result);
2231 /* Convert real to complex. */
2234 gfc_real2complex (gfc_expr *src, int kind)
2239 result = gfc_constant_result (BT_COMPLEX, kind, &src->where);
2241 mpfr_set (result->value.complex.r, src->value.real, GFC_RND_MODE);
2242 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
2244 rc = gfc_check_real_range (result->value.complex.r, kind);
2246 if (rc == ARITH_UNDERFLOW)
2248 if (gfc_option.warn_underflow)
2249 gfc_warning (gfc_arith_error (rc), &src->where);
2250 mpfr_set_ui (result->value.complex.r, 0, GFC_RND_MODE);
2252 else if (rc != ARITH_OK)
2254 arith_error (rc, &src->ts, &result->ts, &src->where);
2255 gfc_free_expr (result);
2263 /* Convert complex to integer. */
2266 gfc_complex2int (gfc_expr *src, int kind)
2271 result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2273 gfc_mpfr_to_mpz (result->value.integer, src->value.complex.r, &src->where);
2275 if ((rc = gfc_check_integer_range (result->value.integer, kind)) != ARITH_OK)
2277 arith_error (rc, &src->ts, &result->ts, &src->where);
2278 gfc_free_expr (result);
2286 /* Convert complex to real. */
2289 gfc_complex2real (gfc_expr *src, int kind)
2294 result = gfc_constant_result (BT_REAL, kind, &src->where);
2296 mpfr_set (result->value.real, src->value.complex.r, GFC_RND_MODE);
2298 rc = gfc_check_real_range (result->value.real, kind);
2300 if (rc == ARITH_UNDERFLOW)
2302 if (gfc_option.warn_underflow)
2303 gfc_warning (gfc_arith_error (rc), &src->where);
2304 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
2308 arith_error (rc, &src->ts, &result->ts, &src->where);
2309 gfc_free_expr (result);
2317 /* Convert complex to complex. */
2320 gfc_complex2complex (gfc_expr *src, int kind)
2325 result = gfc_constant_result (BT_COMPLEX, kind, &src->where);
2327 mpfr_set (result->value.complex.r, src->value.complex.r, GFC_RND_MODE);
2328 mpfr_set (result->value.complex.i, src->value.complex.i, GFC_RND_MODE);
2330 rc = gfc_check_real_range (result->value.complex.r, kind);
2332 if (rc == ARITH_UNDERFLOW)
2334 if (gfc_option.warn_underflow)
2335 gfc_warning (gfc_arith_error (rc), &src->where);
2336 mpfr_set_ui (result->value.complex.r, 0, GFC_RND_MODE);
2338 else if (rc != ARITH_OK)
2340 arith_error (rc, &src->ts, &result->ts, &src->where);
2341 gfc_free_expr (result);
2345 rc = gfc_check_real_range (result->value.complex.i, kind);
2347 if (rc == ARITH_UNDERFLOW)
2349 if (gfc_option.warn_underflow)
2350 gfc_warning (gfc_arith_error (rc), &src->where);
2351 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
2353 else if (rc != ARITH_OK)
2355 arith_error (rc, &src->ts, &result->ts, &src->where);
2356 gfc_free_expr (result);
2364 /* Logical kind conversion. */
2367 gfc_log2log (gfc_expr *src, int kind)
2371 result = gfc_constant_result (BT_LOGICAL, kind, &src->where);
2372 result->value.logical = src->value.logical;
2378 /* Convert logical to integer. */
2381 gfc_log2int (gfc_expr *src, int kind)
2385 result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2386 mpz_set_si (result->value.integer, src->value.logical);
2392 /* Convert integer to logical. */
2395 gfc_int2log (gfc_expr *src, int kind)
2399 result = gfc_constant_result (BT_LOGICAL, kind, &src->where);
2400 result->value.logical = (mpz_cmp_si (src->value.integer, 0) != 0);
2406 /* Helper function to set the representation in a Hollerith conversion.
2407 This assumes that the ts.type and ts.kind of the result have already
2411 hollerith2representation (gfc_expr *result, gfc_expr *src)
2413 int src_len, result_len;
2415 src_len = src->representation.length;
2416 result_len = gfc_target_expr_size (result);
2418 if (src_len > result_len)
2420 gfc_warning ("The Hollerith constant at %L is too long to convert to %s",
2421 &src->where, gfc_typename(&result->ts));
2424 result->representation.string = XCNEWVEC (char, result_len + 1);
2425 memcpy (result->representation.string, src->representation.string,
2426 MIN (result_len, src_len));
2428 if (src_len < result_len)
2429 memset (&result->representation.string[src_len], ' ', result_len - src_len);
2431 result->representation.string[result_len] = '\0'; /* For debugger */
2432 result->representation.length = result_len;
2436 /* Convert Hollerith to integer. The constant will be padded or truncated. */
2439 gfc_hollerith2int (gfc_expr *src, int kind)
2443 result = gfc_get_expr ();
2444 result->expr_type = EXPR_CONSTANT;
2445 result->ts.type = BT_INTEGER;
2446 result->ts.kind = kind;
2447 result->where = src->where;
2449 hollerith2representation (result, src);
2450 gfc_interpret_integer (kind, (unsigned char *) result->representation.string,
2451 result->representation.length, result->value.integer);
2457 /* Convert Hollerith to real. The constant will be padded or truncated. */
2460 gfc_hollerith2real (gfc_expr *src, int kind)
2465 len = src->value.character.length;
2467 result = gfc_get_expr ();
2468 result->expr_type = EXPR_CONSTANT;
2469 result->ts.type = BT_REAL;
2470 result->ts.kind = kind;
2471 result->where = src->where;
2473 hollerith2representation (result, src);
2474 gfc_interpret_float (kind, (unsigned char *) result->representation.string,
2475 result->representation.length, result->value.real);
2481 /* Convert Hollerith to complex. The constant will be padded or truncated. */
2484 gfc_hollerith2complex (gfc_expr *src, int kind)
2489 len = src->value.character.length;
2491 result = gfc_get_expr ();
2492 result->expr_type = EXPR_CONSTANT;
2493 result->ts.type = BT_COMPLEX;
2494 result->ts.kind = kind;
2495 result->where = src->where;
2497 hollerith2representation (result, src);
2498 gfc_interpret_complex (kind, (unsigned char *) result->representation.string,
2499 result->representation.length, result->value.complex.r,
2500 result->value.complex.i);
2506 /* Convert Hollerith to character. */
2509 gfc_hollerith2character (gfc_expr *src, int kind)
2513 result = gfc_copy_expr (src);
2514 result->ts.type = BT_CHARACTER;
2515 result->ts.kind = kind;
2517 result->value.character.length = result->representation.length;
2518 result->value.character.string
2519 = gfc_char_to_widechar (result->representation.string);
2525 /* Convert Hollerith to logical. The constant will be padded or truncated. */
2528 gfc_hollerith2logical (gfc_expr *src, int kind)
2533 len = src->value.character.length;
2535 result = gfc_get_expr ();
2536 result->expr_type = EXPR_CONSTANT;
2537 result->ts.type = BT_LOGICAL;
2538 result->ts.kind = kind;
2539 result->where = src->where;
2541 hollerith2representation (result, src);
2542 gfc_interpret_logical (kind, (unsigned char *) result->representation.string,
2543 result->representation.length, &result->value.logical);
2549 /* Returns an initializer whose value is one higher than the value of the
2550 LAST_INITIALIZER argument. If the argument is NULL, the
2551 initializers value will be set to zero. The initializer's kind
2552 will be set to gfc_c_int_kind.
2554 If -fshort-enums is given, the appropriate kind will be selected
2555 later after all enumerators have been parsed. A warning is issued
2556 here if an initializer exceeds gfc_c_int_kind. */
2559 gfc_enum_initializer (gfc_expr *last_initializer, locus where)
2563 result = gfc_get_expr ();
2564 result->expr_type = EXPR_CONSTANT;
2565 result->ts.type = BT_INTEGER;
2566 result->ts.kind = gfc_c_int_kind;
2567 result->where = where;
2569 mpz_init (result->value.integer);
2571 if (last_initializer != NULL)
2573 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
2574 result->where = last_initializer->where;
2576 if (gfc_check_integer_range (result->value.integer,
2577 gfc_c_int_kind) != ARITH_OK)
2579 gfc_error ("Enumerator exceeds the C integer type at %C");
2585 /* Control comes here, if it's the very first enumerator and no
2586 initializer has been given. It will be initialized to zero. */
2587 mpz_set_si (result->value.integer, 0);