2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
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)
42 e = mpfr_get_z_exp (z, x);
43 /* MPFR 2.0.1 (included with GMP 4.1) has a bug whereby mpfr_get_z_exp
44 may set the sign of z incorrectly. Work around that here. */
45 if (mpfr_sgn (x) != mpz_sgn (z))
49 mpz_mul_2exp (z, z, e);
51 mpz_tdiv_q_2exp (z, z, -e);
55 /* Set the model number precision by the requested KIND. */
58 gfc_set_model_kind (int kind)
60 int index = gfc_validate_kind (BT_REAL, kind, false);
63 base2prec = gfc_real_kinds[index].digits;
64 if (gfc_real_kinds[index].radix != 2)
65 base2prec *= gfc_real_kinds[index].radix / 2;
66 mpfr_set_default_prec (base2prec);
70 /* Set the model number precision from mpfr_t x. */
73 gfc_set_model (mpfr_t x)
75 mpfr_set_default_prec (mpfr_get_prec (x));
79 /* Given an arithmetic error code, return a pointer to a string that
80 explains the error. */
83 gfc_arith_error (arith code)
90 p = _("Arithmetic OK at %L");
93 p = _("Arithmetic overflow at %L");
96 p = _("Arithmetic underflow at %L");
99 p = _("Arithmetic NaN at %L");
102 p = _("Division by zero at %L");
104 case ARITH_INCOMMENSURATE:
105 p = _("Array operands are incommensurate at %L");
107 case ARITH_ASYMMETRIC:
109 _("Integer outside symmetric range implied by Standard Fortran at %L");
112 gfc_internal_error ("gfc_arith_error(): Bad error code");
119 /* Get things ready to do math. */
122 gfc_arith_init_1 (void)
124 gfc_integer_info *int_info;
125 gfc_real_info *real_info;
130 mpfr_set_default_prec (128);
134 /* Convert the minimum and maximum values for each kind into their
135 GNU MP representation. */
136 for (int_info = gfc_integer_kinds; int_info->kind != 0; int_info++)
139 mpz_set_ui (r, int_info->radix);
140 mpz_pow_ui (r, r, int_info->digits);
142 mpz_init (int_info->huge);
143 mpz_sub_ui (int_info->huge, r, 1);
145 /* These are the numbers that are actually representable by the
146 target. For bases other than two, this needs to be changed. */
147 if (int_info->radix != 2)
148 gfc_internal_error ("Fix min_int calculation");
150 /* See PRs 13490 and 17912, related to integer ranges.
151 The pedantic_min_int exists for range checking when a program
152 is compiled with -pedantic, and reflects the belief that
153 Standard Fortran requires integers to be symmetrical, i.e.
154 every negative integer must have a representable positive
155 absolute value, and vice versa. */
157 mpz_init (int_info->pedantic_min_int);
158 mpz_neg (int_info->pedantic_min_int, int_info->huge);
160 mpz_init (int_info->min_int);
161 mpz_sub_ui (int_info->min_int, int_info->pedantic_min_int, 1);
164 mpfr_set_z (a, int_info->huge, GFC_RND_MODE);
165 mpfr_log10 (a, a, GFC_RND_MODE);
167 gfc_mpfr_to_mpz (r, a);
168 int_info->range = mpz_get_si (r);
173 for (real_info = gfc_real_kinds; real_info->kind != 0; real_info++)
175 gfc_set_model_kind (real_info->kind);
181 /* huge(x) = (1 - b**(-p)) * b**(emax-1) * b */
182 /* a = 1 - b**(-p) */
183 mpfr_set_ui (a, 1, GFC_RND_MODE);
184 mpfr_set_ui (b, real_info->radix, GFC_RND_MODE);
185 mpfr_pow_si (b, b, -real_info->digits, GFC_RND_MODE);
186 mpfr_sub (a, a, b, GFC_RND_MODE);
188 /* c = b**(emax-1) */
189 mpfr_set_ui (b, real_info->radix, GFC_RND_MODE);
190 mpfr_pow_ui (c, b, real_info->max_exponent - 1, GFC_RND_MODE);
192 /* a = a * c = (1 - b**(-p)) * b**(emax-1) */
193 mpfr_mul (a, a, c, GFC_RND_MODE);
195 /* a = (1 - b**(-p)) * b**(emax-1) * b */
196 mpfr_mul_ui (a, a, real_info->radix, GFC_RND_MODE);
198 mpfr_init (real_info->huge);
199 mpfr_set (real_info->huge, a, GFC_RND_MODE);
201 /* tiny(x) = b**(emin-1) */
202 mpfr_set_ui (b, real_info->radix, GFC_RND_MODE);
203 mpfr_pow_si (b, b, real_info->min_exponent - 1, GFC_RND_MODE);
205 mpfr_init (real_info->tiny);
206 mpfr_set (real_info->tiny, b, GFC_RND_MODE);
208 /* subnormal (x) = b**(emin - digit) */
209 mpfr_set_ui (b, real_info->radix, GFC_RND_MODE);
210 mpfr_pow_si (b, b, real_info->min_exponent - real_info->digits,
213 mpfr_init (real_info->subnormal);
214 mpfr_set (real_info->subnormal, b, GFC_RND_MODE);
216 /* epsilon(x) = b**(1-p) */
217 mpfr_set_ui (b, real_info->radix, GFC_RND_MODE);
218 mpfr_pow_si (b, b, 1 - real_info->digits, GFC_RND_MODE);
220 mpfr_init (real_info->epsilon);
221 mpfr_set (real_info->epsilon, b, GFC_RND_MODE);
223 /* range(x) = int(min(log10(huge(x)), -log10(tiny)) */
224 mpfr_log10 (a, real_info->huge, GFC_RND_MODE);
225 mpfr_log10 (b, real_info->tiny, GFC_RND_MODE);
226 mpfr_neg (b, b, GFC_RND_MODE);
229 if (mpfr_cmp (a, b) > 0)
230 mpfr_set (a, b, GFC_RND_MODE);
233 gfc_mpfr_to_mpz (r, a);
234 real_info->range = mpz_get_si (r);
236 /* precision(x) = int((p - 1) * log10(b)) + k */
237 mpfr_set_ui (a, real_info->radix, GFC_RND_MODE);
238 mpfr_log10 (a, a, GFC_RND_MODE);
240 mpfr_mul_ui (a, a, real_info->digits - 1, GFC_RND_MODE);
242 gfc_mpfr_to_mpz (r, a);
243 real_info->precision = mpz_get_si (r);
245 /* If the radix is an integral power of 10, add one to the precision. */
246 for (i = 10; i <= real_info->radix; i *= 10)
247 if (i == real_info->radix)
248 real_info->precision++;
259 /* Clean up, get rid of numeric constants. */
262 gfc_arith_done_1 (void)
264 gfc_integer_info *ip;
267 for (ip = gfc_integer_kinds; ip->kind; ip++)
269 mpz_clear (ip->min_int);
270 mpz_clear (ip->pedantic_min_int);
271 mpz_clear (ip->huge);
274 for (rp = gfc_real_kinds; rp->kind; rp++)
276 mpfr_clear (rp->epsilon);
277 mpfr_clear (rp->huge);
278 mpfr_clear (rp->tiny);
279 mpfr_clear (rp->subnormal);
284 /* Given an integer and a kind, make sure that the integer lies within
285 the range of the kind. Returns ARITH_OK, ARITH_ASYMMETRIC or
289 gfc_check_integer_range (mpz_t p, int kind)
294 i = gfc_validate_kind (BT_INTEGER, kind, false);
299 if (mpz_cmp (p, gfc_integer_kinds[i].pedantic_min_int) < 0)
300 result = ARITH_ASYMMETRIC;
304 if (gfc_option.flag_range_check == 0)
307 if (mpz_cmp (p, gfc_integer_kinds[i].min_int) < 0
308 || mpz_cmp (p, gfc_integer_kinds[i].huge) > 0)
309 result = ARITH_OVERFLOW;
315 /* Given a real and a kind, make sure that the real lies within the
316 range of the kind. Returns ARITH_OK, ARITH_OVERFLOW or
320 gfc_check_real_range (mpfr_t p, int kind)
326 i = gfc_validate_kind (BT_REAL, kind, false);
330 mpfr_abs (q, p, GFC_RND_MODE);
334 if (gfc_option.flag_range_check == 0)
337 retval = ARITH_OVERFLOW;
339 else if (mpfr_nan_p (p))
341 if (gfc_option.flag_range_check == 0)
346 else if (mpfr_sgn (q) == 0)
348 else if (mpfr_cmp (q, gfc_real_kinds[i].huge) > 0)
350 if (gfc_option.flag_range_check == 0)
353 retval = ARITH_OVERFLOW;
355 else if (mpfr_cmp (q, gfc_real_kinds[i].subnormal) < 0)
357 if (gfc_option.flag_range_check == 0)
360 retval = ARITH_UNDERFLOW;
362 else if (mpfr_cmp (q, gfc_real_kinds[i].tiny) < 0)
367 /* Save current values of emin and emax. */
368 emin = mpfr_get_emin ();
369 emax = mpfr_get_emax ();
371 /* Set emin and emax for the current model number. */
372 en = gfc_real_kinds[i].min_exponent - gfc_real_kinds[i].digits + 1;
373 mpfr_set_emin ((mp_exp_t) en);
374 mpfr_set_emax ((mp_exp_t) gfc_real_kinds[i].max_exponent);
375 mpfr_subnormalize (q, 0, GFC_RND_MODE);
377 /* Reset emin and emax. */
378 mpfr_set_emin (emin);
379 mpfr_set_emax (emax);
381 /* Copy sign if needed. */
382 if (mpfr_sgn (p) < 0)
383 mpfr_neg (p, q, GMP_RNDN);
385 mpfr_set (p, q, GMP_RNDN);
398 /* Function to return a constant expression node of a given type and kind. */
401 gfc_constant_result (bt type, int kind, locus *where)
406 gfc_internal_error ("gfc_constant_result(): locus 'where' cannot be NULL");
408 result = gfc_get_expr ();
410 result->expr_type = EXPR_CONSTANT;
411 result->ts.type = type;
412 result->ts.kind = kind;
413 result->where = *where;
418 mpz_init (result->value.integer);
422 gfc_set_model_kind (kind);
423 mpfr_init (result->value.real);
427 gfc_set_model_kind (kind);
428 mpfr_init (result->value.complex.r);
429 mpfr_init (result->value.complex.i);
440 /* Low-level arithmetic functions. All of these subroutines assume
441 that all operands are of the same type and return an operand of the
442 same type. The other thing about these subroutines is that they
443 can fail in various ways -- overflow, underflow, division by zero,
444 zero raised to the zero, etc. */
447 gfc_arith_not (gfc_expr *op1, gfc_expr **resultp)
451 result = gfc_constant_result (BT_LOGICAL, op1->ts.kind, &op1->where);
452 result->value.logical = !op1->value.logical;
460 gfc_arith_and (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
464 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
466 result->value.logical = op1->value.logical && op2->value.logical;
474 gfc_arith_or (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
478 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
480 result->value.logical = op1->value.logical || op2->value.logical;
488 gfc_arith_eqv (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
492 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
494 result->value.logical = op1->value.logical == op2->value.logical;
502 gfc_arith_neqv (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
506 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
508 result->value.logical = op1->value.logical != op2->value.logical;
515 /* Make sure a constant numeric expression is within the range for
516 its type and kind. Note that there's also a gfc_check_range(),
517 but that one deals with the intrinsic RANGE function. */
520 gfc_range_check (gfc_expr *e)
527 rc = gfc_check_integer_range (e->value.integer, e->ts.kind);
531 rc = gfc_check_real_range (e->value.real, e->ts.kind);
532 if (rc == ARITH_UNDERFLOW)
533 mpfr_set_ui (e->value.real, 0, GFC_RND_MODE);
534 if (rc == ARITH_OVERFLOW)
535 mpfr_set_inf (e->value.real, mpfr_sgn (e->value.real));
537 mpfr_set_nan (e->value.real);
541 rc = gfc_check_real_range (e->value.complex.r, e->ts.kind);
542 if (rc == ARITH_UNDERFLOW)
543 mpfr_set_ui (e->value.complex.r, 0, GFC_RND_MODE);
544 if (rc == ARITH_OVERFLOW)
545 mpfr_set_inf (e->value.complex.r, mpfr_sgn (e->value.complex.r));
547 mpfr_set_nan (e->value.complex.r);
549 rc = gfc_check_real_range (e->value.complex.i, e->ts.kind);
550 if (rc == ARITH_UNDERFLOW)
551 mpfr_set_ui (e->value.complex.i, 0, GFC_RND_MODE);
552 if (rc == ARITH_OVERFLOW)
553 mpfr_set_inf (e->value.complex.i, mpfr_sgn (e->value.complex.i));
555 mpfr_set_nan (e->value.complex.i);
559 gfc_internal_error ("gfc_range_check(): Bad type");
566 /* Several of the following routines use the same set of statements to
567 check the validity of the result. Encapsulate the checking here. */
570 check_result (arith rc, gfc_expr *x, gfc_expr *r, gfc_expr **rp)
574 if (val == ARITH_UNDERFLOW)
576 if (gfc_option.warn_underflow)
577 gfc_warning (gfc_arith_error (val), &x->where);
581 if (val == ARITH_ASYMMETRIC)
583 gfc_warning (gfc_arith_error (val), &x->where);
596 /* It may seem silly to have a subroutine that actually computes the
597 unary plus of a constant, but it prevents us from making exceptions
598 in the code elsewhere. Used for unary plus and parenthesized
602 gfc_arith_identity (gfc_expr *op1, gfc_expr **resultp)
604 *resultp = gfc_copy_expr (op1);
610 gfc_arith_uminus (gfc_expr *op1, gfc_expr **resultp)
615 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
617 switch (op1->ts.type)
620 mpz_neg (result->value.integer, op1->value.integer);
624 mpfr_neg (result->value.real, op1->value.real, GFC_RND_MODE);
628 mpfr_neg (result->value.complex.r, op1->value.complex.r, GFC_RND_MODE);
629 mpfr_neg (result->value.complex.i, op1->value.complex.i, GFC_RND_MODE);
633 gfc_internal_error ("gfc_arith_uminus(): Bad basic type");
636 rc = gfc_range_check (result);
638 return check_result (rc, op1, result, resultp);
643 gfc_arith_plus (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
648 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
650 switch (op1->ts.type)
653 mpz_add (result->value.integer, op1->value.integer, op2->value.integer);
657 mpfr_add (result->value.real, op1->value.real, op2->value.real,
662 mpfr_add (result->value.complex.r, op1->value.complex.r,
663 op2->value.complex.r, GFC_RND_MODE);
665 mpfr_add (result->value.complex.i, op1->value.complex.i,
666 op2->value.complex.i, GFC_RND_MODE);
670 gfc_internal_error ("gfc_arith_plus(): Bad basic type");
673 rc = gfc_range_check (result);
675 return check_result (rc, op1, result, resultp);
680 gfc_arith_minus (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
685 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
687 switch (op1->ts.type)
690 mpz_sub (result->value.integer, op1->value.integer, op2->value.integer);
694 mpfr_sub (result->value.real, op1->value.real, op2->value.real,
699 mpfr_sub (result->value.complex.r, op1->value.complex.r,
700 op2->value.complex.r, GFC_RND_MODE);
702 mpfr_sub (result->value.complex.i, op1->value.complex.i,
703 op2->value.complex.i, GFC_RND_MODE);
707 gfc_internal_error ("gfc_arith_minus(): Bad basic type");
710 rc = gfc_range_check (result);
712 return check_result (rc, op1, result, resultp);
717 gfc_arith_times (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
723 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
725 switch (op1->ts.type)
728 mpz_mul (result->value.integer, op1->value.integer, op2->value.integer);
732 mpfr_mul (result->value.real, op1->value.real, op2->value.real,
737 gfc_set_model (op1->value.complex.r);
741 mpfr_mul (x, op1->value.complex.r, op2->value.complex.r, GFC_RND_MODE);
742 mpfr_mul (y, op1->value.complex.i, op2->value.complex.i, GFC_RND_MODE);
743 mpfr_sub (result->value.complex.r, x, y, GFC_RND_MODE);
745 mpfr_mul (x, op1->value.complex.r, op2->value.complex.i, GFC_RND_MODE);
746 mpfr_mul (y, op1->value.complex.i, op2->value.complex.r, GFC_RND_MODE);
747 mpfr_add (result->value.complex.i, x, y, GFC_RND_MODE);
754 gfc_internal_error ("gfc_arith_times(): Bad basic type");
757 rc = gfc_range_check (result);
759 return check_result (rc, op1, result, resultp);
764 gfc_arith_divide (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
772 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
774 switch (op1->ts.type)
777 if (mpz_sgn (op2->value.integer) == 0)
783 mpz_tdiv_q (result->value.integer, op1->value.integer,
788 if (mpfr_sgn (op2->value.real) == 0 && gfc_option.flag_range_check == 1)
794 mpfr_div (result->value.real, op1->value.real, op2->value.real,
799 if (mpfr_sgn (op2->value.complex.r) == 0
800 && mpfr_sgn (op2->value.complex.i) == 0
801 && gfc_option.flag_range_check == 1)
807 gfc_set_model (op1->value.complex.r);
812 mpfr_mul (x, op2->value.complex.r, op2->value.complex.r, GFC_RND_MODE);
813 mpfr_mul (y, op2->value.complex.i, op2->value.complex.i, GFC_RND_MODE);
814 mpfr_add (div, x, y, GFC_RND_MODE);
816 mpfr_mul (x, op1->value.complex.r, op2->value.complex.r, GFC_RND_MODE);
817 mpfr_mul (y, op1->value.complex.i, op2->value.complex.i, GFC_RND_MODE);
818 mpfr_add (result->value.complex.r, x, y, GFC_RND_MODE);
819 mpfr_div (result->value.complex.r, result->value.complex.r, div,
822 mpfr_mul (x, op1->value.complex.i, op2->value.complex.r, GFC_RND_MODE);
823 mpfr_mul (y, op1->value.complex.r, op2->value.complex.i, GFC_RND_MODE);
824 mpfr_sub (result->value.complex.i, x, y, GFC_RND_MODE);
825 mpfr_div (result->value.complex.i, result->value.complex.i, div,
834 gfc_internal_error ("gfc_arith_divide(): Bad basic type");
838 rc = gfc_range_check (result);
840 return check_result (rc, op1, result, resultp);
844 /* Compute the reciprocal of a complex number (guaranteed nonzero). */
847 complex_reciprocal (gfc_expr *op)
849 mpfr_t mod, a, re, im;
851 gfc_set_model (op->value.complex.r);
857 mpfr_mul (mod, op->value.complex.r, op->value.complex.r, GFC_RND_MODE);
858 mpfr_mul (a, op->value.complex.i, op->value.complex.i, GFC_RND_MODE);
859 mpfr_add (mod, mod, a, GFC_RND_MODE);
861 mpfr_div (re, op->value.complex.r, mod, GFC_RND_MODE);
863 mpfr_neg (im, op->value.complex.i, GFC_RND_MODE);
864 mpfr_div (im, im, mod, GFC_RND_MODE);
866 mpfr_set (op->value.complex.r, re, GFC_RND_MODE);
867 mpfr_set (op->value.complex.i, im, GFC_RND_MODE);
876 /* Raise a complex number to positive power (power > 0).
877 This function will modify the content of power.
879 Use Binary Method, which is not an optimal but a simple and reasonable
880 arithmetic. See section 4.6.3, "Evaluation of Powers" of Donald E. Knuth,
881 "Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming",
882 3rd Edition, 1998. */
885 complex_pow (gfc_expr *result, gfc_expr *base, mpz_t power)
887 mpfr_t x_r, x_i, tmp, re, im;
889 gfc_set_model (base->value.complex.r);
897 mpfr_set_ui (result->value.complex.r, 1, GFC_RND_MODE);
898 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
901 mpfr_set (x_r, base->value.complex.r, GFC_RND_MODE);
902 mpfr_set (x_i, base->value.complex.i, GFC_RND_MODE);
904 /* Macro for complex multiplication. We have to take care that
905 res_r/res_i and a_r/a_i can (and will) be the same variable. */
906 #define CMULT(res_r,res_i,a_r,a_i,b_r,b_i) \
907 mpfr_mul (re, a_r, b_r, GFC_RND_MODE), \
908 mpfr_mul (tmp, a_i, b_i, GFC_RND_MODE), \
909 mpfr_sub (re, re, tmp, GFC_RND_MODE), \
911 mpfr_mul (im, a_r, b_i, GFC_RND_MODE), \
912 mpfr_mul (tmp, a_i, b_r, GFC_RND_MODE), \
913 mpfr_add (res_i, im, tmp, GFC_RND_MODE), \
914 mpfr_set (res_r, re, GFC_RND_MODE)
916 #define res_r result->value.complex.r
917 #define res_i result->value.complex.i
919 /* for (; power > 0; x *= x) */
920 for (; mpz_cmp_si (power, 0) > 0; CMULT(x_r,x_i,x_r,x_i,x_r,x_i))
922 /* if (power & 1) res = res * x; */
923 if (mpz_congruent_ui_p (power, 1, 2))
924 CMULT(res_r,res_i,res_r,res_i,x_r,x_i);
927 mpz_fdiv_q_ui (power, power, 2);
942 /* Raise a number to an integer power. */
945 gfc_arith_power (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
951 gcc_assert (op2->expr_type == EXPR_CONSTANT && op2->ts.type == BT_INTEGER);
954 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
955 power_sign = mpz_sgn (op2->value.integer);
959 /* Handle something to the zeroth power. Since we're dealing
960 with integral exponents, there is no ambiguity in the
961 limiting procedure used to determine the value of 0**0. */
962 switch (op1->ts.type)
965 mpz_set_ui (result->value.integer, 1);
969 mpfr_set_ui (result->value.real, 1, GFC_RND_MODE);
973 mpfr_set_ui (result->value.complex.r, 1, GFC_RND_MODE);
974 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
978 gfc_internal_error ("gfc_arith_power(): Bad base");
983 switch (op1->ts.type)
989 /* First, we simplify the cases of op1 == 1, 0 or -1. */
990 if (mpz_cmp_si (op1->value.integer, 1) == 0)
993 mpz_set_si (result->value.integer, 1);
995 else if (mpz_cmp_si (op1->value.integer, 0) == 0)
997 /* 0**op2 == 0, if op2 > 0
998 0**op2 overflow, if op2 < 0 ; in that case, we
999 set the result to 0 and return ARITH_DIV0. */
1000 mpz_set_si (result->value.integer, 0);
1001 if (mpz_cmp_si (op2->value.integer, 0) < 0)
1004 else if (mpz_cmp_si (op1->value.integer, -1) == 0)
1006 /* (-1)**op2 == (-1)**(mod(op2,2)) */
1007 unsigned int odd = mpz_fdiv_ui (op2->value.integer, 2);
1009 mpz_set_si (result->value.integer, -1);
1011 mpz_set_si (result->value.integer, 1);
1013 /* Then, we take care of op2 < 0. */
1014 else if (mpz_cmp_si (op2->value.integer, 0) < 0)
1016 /* if op2 < 0, op1**op2 == 0 because abs(op1) > 1. */
1017 mpz_set_si (result->value.integer, 0);
1019 else if (gfc_extract_int (op2, &power) != NULL)
1021 /* If op2 doesn't fit in an int, the exponentiation will
1022 overflow, because op2 > 0 and abs(op1) > 1. */
1024 int i = gfc_validate_kind (BT_INTEGER, result->ts.kind, false);
1026 if (gfc_option.flag_range_check)
1027 rc = ARITH_OVERFLOW;
1029 /* Still, we want to give the same value as the processor. */
1031 mpz_add_ui (max, gfc_integer_kinds[i].huge, 1);
1032 mpz_mul_ui (max, max, 2);
1033 mpz_powm (result->value.integer, op1->value.integer,
1034 op2->value.integer, max);
1038 mpz_pow_ui (result->value.integer, op1->value.integer, power);
1043 mpfr_pow_z (result->value.real, op1->value.real, op2->value.integer,
1051 /* Compute op1**abs(op2) */
1053 mpz_abs (apower, op2->value.integer);
1054 complex_pow (result, op1, apower);
1057 /* If (op2 < 0), compute the inverse. */
1059 complex_reciprocal (result);
1070 rc = gfc_range_check (result);
1072 return check_result (rc, op1, result, resultp);
1076 /* Concatenate two string constants. */
1079 gfc_arith_concat (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1084 result = gfc_constant_result (BT_CHARACTER, gfc_default_character_kind,
1087 len = op1->value.character.length + op2->value.character.length;
1089 result->value.character.string = gfc_getmem (len + 1);
1090 result->value.character.length = len;
1092 memcpy (result->value.character.string, op1->value.character.string,
1093 op1->value.character.length);
1095 memcpy (result->value.character.string + op1->value.character.length,
1096 op2->value.character.string, op2->value.character.length);
1098 result->value.character.string[len] = '\0';
1106 /* Comparison operators. Assumes that the two expression nodes
1107 contain two constants of the same type. */
1110 gfc_compare_expr (gfc_expr *op1, gfc_expr *op2)
1114 switch (op1->ts.type)
1117 rc = mpz_cmp (op1->value.integer, op2->value.integer);
1121 rc = mpfr_cmp (op1->value.real, op2->value.real);
1125 rc = gfc_compare_string (op1, op2);
1129 rc = ((!op1->value.logical && op2->value.logical)
1130 || (op1->value.logical && !op2->value.logical));
1134 gfc_internal_error ("gfc_compare_expr(): Bad basic type");
1141 /* Compare a pair of complex numbers. Naturally, this is only for
1142 equality and nonequality. */
1145 compare_complex (gfc_expr *op1, gfc_expr *op2)
1147 return (mpfr_cmp (op1->value.complex.r, op2->value.complex.r) == 0
1148 && mpfr_cmp (op1->value.complex.i, op2->value.complex.i) == 0);
1152 /* Given two constant strings and the inverse collating sequence, compare the
1153 strings. We return -1 for a < b, 0 for a == b and 1 for a > b.
1154 We use the processor's default collating sequence. */
1157 gfc_compare_string (gfc_expr *a, gfc_expr *b)
1159 int len, alen, blen, i, ac, bc;
1161 alen = a->value.character.length;
1162 blen = b->value.character.length;
1164 len = (alen > blen) ? alen : blen;
1166 for (i = 0; i < len; i++)
1168 /* We cast to unsigned char because default char, if it is signed,
1169 would lead to ac < 0 for string[i] > 127. */
1170 ac = (unsigned char) ((i < alen) ? a->value.character.string[i] : ' ');
1171 bc = (unsigned char) ((i < blen) ? b->value.character.string[i] : ' ');
1179 /* Strings are equal */
1185 /* Specific comparison subroutines. */
1188 gfc_arith_eq (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1192 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
1194 result->value.logical = (op1->ts.type == BT_COMPLEX)
1195 ? compare_complex (op1, op2)
1196 : (gfc_compare_expr (op1, op2) == 0);
1204 gfc_arith_ne (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1208 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
1210 result->value.logical = (op1->ts.type == BT_COMPLEX)
1211 ? !compare_complex (op1, op2)
1212 : (gfc_compare_expr (op1, op2) != 0);
1220 gfc_arith_gt (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1224 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
1226 result->value.logical = (gfc_compare_expr (op1, op2) > 0);
1234 gfc_arith_ge (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1238 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
1240 result->value.logical = (gfc_compare_expr (op1, op2) >= 0);
1248 gfc_arith_lt (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1252 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
1254 result->value.logical = (gfc_compare_expr (op1, op2) < 0);
1262 gfc_arith_le (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1266 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
1268 result->value.logical = (gfc_compare_expr (op1, op2) <= 0);
1276 reduce_unary (arith (*eval) (gfc_expr *, gfc_expr **), gfc_expr *op,
1279 gfc_constructor *c, *head;
1283 if (op->expr_type == EXPR_CONSTANT)
1284 return eval (op, result);
1287 head = gfc_copy_constructor (op->value.constructor);
1289 for (c = head; c; c = c->next)
1291 rc = eval (c->expr, &r);
1295 gfc_replace_expr (c->expr, r);
1299 gfc_free_constructor (head);
1302 r = gfc_get_expr ();
1303 r->expr_type = EXPR_ARRAY;
1304 r->value.constructor = head;
1305 r->shape = gfc_copy_shape (op->shape, op->rank);
1307 r->ts = head->expr->ts;
1308 r->where = op->where;
1319 reduce_binary_ac (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1320 gfc_expr *op1, gfc_expr *op2, gfc_expr **result)
1322 gfc_constructor *c, *head;
1326 head = gfc_copy_constructor (op1->value.constructor);
1329 for (c = head; c; c = c->next)
1331 rc = eval (c->expr, op2, &r);
1335 gfc_replace_expr (c->expr, r);
1339 gfc_free_constructor (head);
1342 r = gfc_get_expr ();
1343 r->expr_type = EXPR_ARRAY;
1344 r->value.constructor = head;
1345 r->shape = gfc_copy_shape (op1->shape, op1->rank);
1347 r->ts = head->expr->ts;
1348 r->where = op1->where;
1349 r->rank = op1->rank;
1359 reduce_binary_ca (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1360 gfc_expr *op1, gfc_expr *op2, gfc_expr **result)
1362 gfc_constructor *c, *head;
1366 head = gfc_copy_constructor (op2->value.constructor);
1369 for (c = head; c; c = c->next)
1371 rc = eval (op1, c->expr, &r);
1375 gfc_replace_expr (c->expr, r);
1379 gfc_free_constructor (head);
1382 r = gfc_get_expr ();
1383 r->expr_type = EXPR_ARRAY;
1384 r->value.constructor = head;
1385 r->shape = gfc_copy_shape (op2->shape, op2->rank);
1387 r->ts = head->expr->ts;
1388 r->where = op2->where;
1389 r->rank = op2->rank;
1399 reduce_binary_aa (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1400 gfc_expr *op1, gfc_expr *op2, gfc_expr **result)
1402 gfc_constructor *c, *d, *head;
1406 head = gfc_copy_constructor (op1->value.constructor);
1409 d = op2->value.constructor;
1411 if (gfc_check_conformance ("Elemental binary operation", op1, op2)
1413 rc = ARITH_INCOMMENSURATE;
1416 for (c = head; c; c = c->next, d = d->next)
1420 rc = ARITH_INCOMMENSURATE;
1424 rc = eval (c->expr, d->expr, &r);
1428 gfc_replace_expr (c->expr, r);
1432 rc = ARITH_INCOMMENSURATE;
1436 gfc_free_constructor (head);
1439 r = gfc_get_expr ();
1440 r->expr_type = EXPR_ARRAY;
1441 r->value.constructor = head;
1442 r->shape = gfc_copy_shape (op1->shape, op1->rank);
1444 r->ts = head->expr->ts;
1445 r->where = op1->where;
1446 r->rank = op1->rank;
1456 reduce_binary (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1457 gfc_expr *op1, gfc_expr *op2, gfc_expr **result)
1459 if (op1->expr_type == EXPR_CONSTANT && op2->expr_type == EXPR_CONSTANT)
1460 return eval (op1, op2, result);
1462 if (op1->expr_type == EXPR_CONSTANT && op2->expr_type == EXPR_ARRAY)
1463 return reduce_binary_ca (eval, op1, op2, result);
1465 if (op1->expr_type == EXPR_ARRAY && op2->expr_type == EXPR_CONSTANT)
1466 return reduce_binary_ac (eval, op1, op2, result);
1468 return reduce_binary_aa (eval, op1, op2, result);
1474 arith (*f2)(gfc_expr *, gfc_expr **);
1475 arith (*f3)(gfc_expr *, gfc_expr *, gfc_expr **);
1479 /* High level arithmetic subroutines. These subroutines go into
1480 eval_intrinsic(), which can do one of several things to its
1481 operands. If the operands are incompatible with the intrinsic
1482 operation, we return a node pointing to the operands and hope that
1483 an operator interface is found during resolution.
1485 If the operands are compatible and are constants, then we try doing
1486 the arithmetic. We also handle the cases where either or both
1487 operands are array constructors. */
1490 eval_intrinsic (gfc_intrinsic_op operator,
1491 eval_f eval, gfc_expr *op1, gfc_expr *op2)
1493 gfc_expr temp, *result;
1497 gfc_clear_ts (&temp.ts);
1503 if (op1->ts.type != BT_LOGICAL)
1506 temp.ts.type = BT_LOGICAL;
1507 temp.ts.kind = gfc_default_logical_kind;
1511 /* Logical binary operators */
1514 case INTRINSIC_NEQV:
1516 if (op1->ts.type != BT_LOGICAL || op2->ts.type != BT_LOGICAL)
1519 temp.ts.type = BT_LOGICAL;
1520 temp.ts.kind = gfc_default_logical_kind;
1525 case INTRINSIC_UPLUS:
1526 case INTRINSIC_UMINUS:
1527 if (!gfc_numeric_ts (&op1->ts))
1534 case INTRINSIC_PARENTHESES:
1539 /* Additional restrictions for ordering relations. */
1541 case INTRINSIC_GE_OS:
1543 case INTRINSIC_LT_OS:
1545 case INTRINSIC_LE_OS:
1547 case INTRINSIC_GT_OS:
1548 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
1550 temp.ts.type = BT_LOGICAL;
1551 temp.ts.kind = gfc_default_logical_kind;
1557 case INTRINSIC_EQ_OS:
1559 case INTRINSIC_NE_OS:
1560 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
1563 temp.ts.type = BT_LOGICAL;
1564 temp.ts.kind = gfc_default_logical_kind;
1569 /* Numeric binary */
1570 case INTRINSIC_PLUS:
1571 case INTRINSIC_MINUS:
1572 case INTRINSIC_TIMES:
1573 case INTRINSIC_DIVIDE:
1574 case INTRINSIC_POWER:
1575 if (!gfc_numeric_ts (&op1->ts) || !gfc_numeric_ts (&op2->ts))
1578 /* Insert any necessary type conversions to make the operands
1581 temp.expr_type = EXPR_OP;
1582 gfc_clear_ts (&temp.ts);
1583 temp.value.op.operator = operator;
1585 temp.value.op.op1 = op1;
1586 temp.value.op.op2 = op2;
1588 gfc_type_convert_binary (&temp);
1590 if (operator == INTRINSIC_EQ || operator == INTRINSIC_NE
1591 || operator == INTRINSIC_GE || operator == INTRINSIC_GT
1592 || operator == INTRINSIC_LE || operator == INTRINSIC_LT
1593 || operator == INTRINSIC_EQ_OS || operator == INTRINSIC_NE_OS
1594 || operator == INTRINSIC_GE_OS || operator == INTRINSIC_GT_OS
1595 || operator == INTRINSIC_LE_OS || operator == INTRINSIC_LT_OS)
1597 temp.ts.type = BT_LOGICAL;
1598 temp.ts.kind = gfc_default_logical_kind;
1604 /* Character binary */
1605 case INTRINSIC_CONCAT:
1606 if (op1->ts.type != BT_CHARACTER || op2->ts.type != BT_CHARACTER)
1609 temp.ts.type = BT_CHARACTER;
1610 temp.ts.kind = gfc_default_character_kind;
1614 case INTRINSIC_USER:
1618 gfc_internal_error ("eval_intrinsic(): Bad operator");
1621 /* Try to combine the operators. */
1622 if (operator == INTRINSIC_POWER && op2->ts.type != BT_INTEGER)
1625 if (op1->expr_type != EXPR_CONSTANT
1626 && (op1->expr_type != EXPR_ARRAY
1627 || !gfc_is_constant_expr (op1) || !gfc_expanded_ac (op1)))
1631 && op2->expr_type != EXPR_CONSTANT
1632 && (op2->expr_type != EXPR_ARRAY
1633 || !gfc_is_constant_expr (op2) || !gfc_expanded_ac (op2)))
1637 rc = reduce_unary (eval.f2, op1, &result);
1639 rc = reduce_binary (eval.f3, op1, op2, &result);
1642 { /* Something went wrong. */
1643 gfc_error (gfc_arith_error (rc), &op1->where);
1647 gfc_free_expr (op1);
1648 gfc_free_expr (op2);
1652 /* Create a run-time expression. */
1653 result = gfc_get_expr ();
1654 result->ts = temp.ts;
1656 result->expr_type = EXPR_OP;
1657 result->value.op.operator = operator;
1659 result->value.op.op1 = op1;
1660 result->value.op.op2 = op2;
1662 result->where = op1->where;
1668 /* Modify type of expression for zero size array. */
1671 eval_type_intrinsic0 (gfc_intrinsic_op operator, gfc_expr *op)
1674 gfc_internal_error ("eval_type_intrinsic0(): op NULL");
1679 case INTRINSIC_GE_OS:
1681 case INTRINSIC_LT_OS:
1683 case INTRINSIC_LE_OS:
1685 case INTRINSIC_GT_OS:
1687 case INTRINSIC_EQ_OS:
1689 case INTRINSIC_NE_OS:
1690 op->ts.type = BT_LOGICAL;
1691 op->ts.kind = gfc_default_logical_kind;
1702 /* Return nonzero if the expression is a zero size array. */
1705 gfc_zero_size_array (gfc_expr *e)
1707 if (e->expr_type != EXPR_ARRAY)
1710 return e->value.constructor == NULL;
1714 /* Reduce a binary expression where at least one of the operands
1715 involves a zero-length array. Returns NULL if neither of the
1716 operands is a zero-length array. */
1719 reduce_binary0 (gfc_expr *op1, gfc_expr *op2)
1721 if (gfc_zero_size_array (op1))
1723 gfc_free_expr (op2);
1727 if (gfc_zero_size_array (op2))
1729 gfc_free_expr (op1);
1738 eval_intrinsic_f2 (gfc_intrinsic_op operator,
1739 arith (*eval) (gfc_expr *, gfc_expr **),
1740 gfc_expr *op1, gfc_expr *op2)
1747 if (gfc_zero_size_array (op1))
1748 return eval_type_intrinsic0 (operator, op1);
1752 result = reduce_binary0 (op1, op2);
1754 return eval_type_intrinsic0 (operator, result);
1758 return eval_intrinsic (operator, f, op1, op2);
1763 eval_intrinsic_f3 (gfc_intrinsic_op operator,
1764 arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1765 gfc_expr *op1, gfc_expr *op2)
1770 result = reduce_binary0 (op1, op2);
1772 return eval_type_intrinsic0(operator, result);
1775 return eval_intrinsic (operator, f, op1, op2);
1780 gfc_parentheses (gfc_expr *op)
1782 return eval_intrinsic_f2 (INTRINSIC_PARENTHESES, gfc_arith_identity,
1787 gfc_uplus (gfc_expr *op)
1789 return eval_intrinsic_f2 (INTRINSIC_UPLUS, gfc_arith_identity, op, NULL);
1794 gfc_uminus (gfc_expr *op)
1796 return eval_intrinsic_f2 (INTRINSIC_UMINUS, gfc_arith_uminus, op, NULL);
1801 gfc_add (gfc_expr *op1, gfc_expr *op2)
1803 return eval_intrinsic_f3 (INTRINSIC_PLUS, gfc_arith_plus, op1, op2);
1808 gfc_subtract (gfc_expr *op1, gfc_expr *op2)
1810 return eval_intrinsic_f3 (INTRINSIC_MINUS, gfc_arith_minus, op1, op2);
1815 gfc_multiply (gfc_expr *op1, gfc_expr *op2)
1817 return eval_intrinsic_f3 (INTRINSIC_TIMES, gfc_arith_times, op1, op2);
1822 gfc_divide (gfc_expr *op1, gfc_expr *op2)
1824 return eval_intrinsic_f3 (INTRINSIC_DIVIDE, gfc_arith_divide, op1, op2);
1829 gfc_power (gfc_expr *op1, gfc_expr *op2)
1831 return eval_intrinsic_f3 (INTRINSIC_POWER, gfc_arith_power, op1, op2);
1836 gfc_concat (gfc_expr *op1, gfc_expr *op2)
1838 return eval_intrinsic_f3 (INTRINSIC_CONCAT, gfc_arith_concat, op1, op2);
1843 gfc_and (gfc_expr *op1, gfc_expr *op2)
1845 return eval_intrinsic_f3 (INTRINSIC_AND, gfc_arith_and, op1, op2);
1850 gfc_or (gfc_expr *op1, gfc_expr *op2)
1852 return eval_intrinsic_f3 (INTRINSIC_OR, gfc_arith_or, op1, op2);
1857 gfc_not (gfc_expr *op1)
1859 return eval_intrinsic_f2 (INTRINSIC_NOT, gfc_arith_not, op1, NULL);
1864 gfc_eqv (gfc_expr *op1, gfc_expr *op2)
1866 return eval_intrinsic_f3 (INTRINSIC_EQV, gfc_arith_eqv, op1, op2);
1871 gfc_neqv (gfc_expr *op1, gfc_expr *op2)
1873 return eval_intrinsic_f3 (INTRINSIC_NEQV, gfc_arith_neqv, op1, op2);
1878 gfc_eq (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1880 return eval_intrinsic_f3 (op, gfc_arith_eq, op1, op2);
1885 gfc_ne (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1887 return eval_intrinsic_f3 (op, gfc_arith_ne, op1, op2);
1892 gfc_gt (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1894 return eval_intrinsic_f3 (op, gfc_arith_gt, op1, op2);
1899 gfc_ge (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1901 return eval_intrinsic_f3 (op, gfc_arith_ge, op1, op2);
1906 gfc_lt (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1908 return eval_intrinsic_f3 (op, gfc_arith_lt, op1, op2);
1913 gfc_le (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1915 return eval_intrinsic_f3 (op, gfc_arith_le, op1, op2);
1919 /* Convert an integer string to an expression node. */
1922 gfc_convert_integer (const char *buffer, int kind, int radix, locus *where)
1927 e = gfc_constant_result (BT_INTEGER, kind, where);
1928 /* A leading plus is allowed, but not by mpz_set_str. */
1929 if (buffer[0] == '+')
1933 mpz_set_str (e->value.integer, t, radix);
1939 /* Convert a real string to an expression node. */
1942 gfc_convert_real (const char *buffer, int kind, locus *where)
1946 e = gfc_constant_result (BT_REAL, kind, where);
1947 mpfr_set_str (e->value.real, buffer, 10, GFC_RND_MODE);
1953 /* Convert a pair of real, constant expression nodes to a single
1954 complex expression node. */
1957 gfc_convert_complex (gfc_expr *real, gfc_expr *imag, int kind)
1961 e = gfc_constant_result (BT_COMPLEX, kind, &real->where);
1962 mpfr_set (e->value.complex.r, real->value.real, GFC_RND_MODE);
1963 mpfr_set (e->value.complex.i, imag->value.real, GFC_RND_MODE);
1969 /******* Simplification of intrinsic functions with constant arguments *****/
1972 /* Deal with an arithmetic error. */
1975 arith_error (arith rc, gfc_typespec *from, gfc_typespec *to, locus *where)
1980 gfc_error ("Arithmetic OK converting %s to %s at %L",
1981 gfc_typename (from), gfc_typename (to), where);
1983 case ARITH_OVERFLOW:
1984 gfc_error ("Arithmetic overflow converting %s to %s at %L. This check "
1985 "can be disabled with the option -fno-range-check",
1986 gfc_typename (from), gfc_typename (to), where);
1988 case ARITH_UNDERFLOW:
1989 gfc_error ("Arithmetic underflow converting %s to %s at %L",
1990 gfc_typename (from), gfc_typename (to), where);
1993 gfc_error ("Arithmetic NaN converting %s to %s at %L",
1994 gfc_typename (from), gfc_typename (to), where);
1997 gfc_error ("Division by zero converting %s to %s at %L",
1998 gfc_typename (from), gfc_typename (to), where);
2000 case ARITH_INCOMMENSURATE:
2001 gfc_error ("Array operands are incommensurate converting %s to %s at %L",
2002 gfc_typename (from), gfc_typename (to), where);
2004 case ARITH_ASYMMETRIC:
2005 gfc_error ("Integer outside symmetric range implied by Standard Fortran"
2006 " converting %s to %s at %L",
2007 gfc_typename (from), gfc_typename (to), where);
2010 gfc_internal_error ("gfc_arith_error(): Bad error code");
2013 /* TODO: Do something about the error, ie, throw exception, return
2018 /* Convert integers to integers. */
2021 gfc_int2int (gfc_expr *src, int kind)
2026 result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2028 mpz_set (result->value.integer, src->value.integer);
2030 if ((rc = gfc_check_integer_range (result->value.integer, kind)) != ARITH_OK)
2032 if (rc == ARITH_ASYMMETRIC)
2034 gfc_warning (gfc_arith_error (rc), &src->where);
2038 arith_error (rc, &src->ts, &result->ts, &src->where);
2039 gfc_free_expr (result);
2048 /* Convert integers to reals. */
2051 gfc_int2real (gfc_expr *src, int kind)
2056 result = gfc_constant_result (BT_REAL, kind, &src->where);
2058 mpfr_set_z (result->value.real, src->value.integer, GFC_RND_MODE);
2060 if ((rc = gfc_check_real_range (result->value.real, kind)) != ARITH_OK)
2062 arith_error (rc, &src->ts, &result->ts, &src->where);
2063 gfc_free_expr (result);
2071 /* Convert default integer to default complex. */
2074 gfc_int2complex (gfc_expr *src, int kind)
2079 result = gfc_constant_result (BT_COMPLEX, kind, &src->where);
2081 mpfr_set_z (result->value.complex.r, src->value.integer, GFC_RND_MODE);
2082 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
2084 if ((rc = gfc_check_real_range (result->value.complex.r, kind)) != ARITH_OK)
2086 arith_error (rc, &src->ts, &result->ts, &src->where);
2087 gfc_free_expr (result);
2095 /* Convert default real to default integer. */
2098 gfc_real2int (gfc_expr *src, int kind)
2103 result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2105 gfc_mpfr_to_mpz (result->value.integer, src->value.real);
2107 if ((rc = gfc_check_integer_range (result->value.integer, kind)) != ARITH_OK)
2109 arith_error (rc, &src->ts, &result->ts, &src->where);
2110 gfc_free_expr (result);
2118 /* Convert real to real. */
2121 gfc_real2real (gfc_expr *src, int kind)
2126 result = gfc_constant_result (BT_REAL, kind, &src->where);
2128 mpfr_set (result->value.real, src->value.real, GFC_RND_MODE);
2130 rc = gfc_check_real_range (result->value.real, kind);
2132 if (rc == ARITH_UNDERFLOW)
2134 if (gfc_option.warn_underflow)
2135 gfc_warning (gfc_arith_error (rc), &src->where);
2136 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
2138 else if (rc != ARITH_OK)
2140 arith_error (rc, &src->ts, &result->ts, &src->where);
2141 gfc_free_expr (result);
2149 /* Convert real to complex. */
2152 gfc_real2complex (gfc_expr *src, int kind)
2157 result = gfc_constant_result (BT_COMPLEX, kind, &src->where);
2159 mpfr_set (result->value.complex.r, src->value.real, GFC_RND_MODE);
2160 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
2162 rc = gfc_check_real_range (result->value.complex.r, kind);
2164 if (rc == ARITH_UNDERFLOW)
2166 if (gfc_option.warn_underflow)
2167 gfc_warning (gfc_arith_error (rc), &src->where);
2168 mpfr_set_ui (result->value.complex.r, 0, GFC_RND_MODE);
2170 else if (rc != ARITH_OK)
2172 arith_error (rc, &src->ts, &result->ts, &src->where);
2173 gfc_free_expr (result);
2181 /* Convert complex to integer. */
2184 gfc_complex2int (gfc_expr *src, int kind)
2189 result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2191 gfc_mpfr_to_mpz (result->value.integer, src->value.complex.r);
2193 if ((rc = gfc_check_integer_range (result->value.integer, kind)) != ARITH_OK)
2195 arith_error (rc, &src->ts, &result->ts, &src->where);
2196 gfc_free_expr (result);
2204 /* Convert complex to real. */
2207 gfc_complex2real (gfc_expr *src, int kind)
2212 result = gfc_constant_result (BT_REAL, kind, &src->where);
2214 mpfr_set (result->value.real, src->value.complex.r, GFC_RND_MODE);
2216 rc = gfc_check_real_range (result->value.real, kind);
2218 if (rc == ARITH_UNDERFLOW)
2220 if (gfc_option.warn_underflow)
2221 gfc_warning (gfc_arith_error (rc), &src->where);
2222 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
2226 arith_error (rc, &src->ts, &result->ts, &src->where);
2227 gfc_free_expr (result);
2235 /* Convert complex to complex. */
2238 gfc_complex2complex (gfc_expr *src, int kind)
2243 result = gfc_constant_result (BT_COMPLEX, kind, &src->where);
2245 mpfr_set (result->value.complex.r, src->value.complex.r, GFC_RND_MODE);
2246 mpfr_set (result->value.complex.i, src->value.complex.i, GFC_RND_MODE);
2248 rc = gfc_check_real_range (result->value.complex.r, kind);
2250 if (rc == ARITH_UNDERFLOW)
2252 if (gfc_option.warn_underflow)
2253 gfc_warning (gfc_arith_error (rc), &src->where);
2254 mpfr_set_ui (result->value.complex.r, 0, GFC_RND_MODE);
2256 else if (rc != ARITH_OK)
2258 arith_error (rc, &src->ts, &result->ts, &src->where);
2259 gfc_free_expr (result);
2263 rc = gfc_check_real_range (result->value.complex.i, kind);
2265 if (rc == ARITH_UNDERFLOW)
2267 if (gfc_option.warn_underflow)
2268 gfc_warning (gfc_arith_error (rc), &src->where);
2269 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
2271 else if (rc != ARITH_OK)
2273 arith_error (rc, &src->ts, &result->ts, &src->where);
2274 gfc_free_expr (result);
2282 /* Logical kind conversion. */
2285 gfc_log2log (gfc_expr *src, int kind)
2289 result = gfc_constant_result (BT_LOGICAL, kind, &src->where);
2290 result->value.logical = src->value.logical;
2296 /* Convert logical to integer. */
2299 gfc_log2int (gfc_expr *src, int kind)
2303 result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2304 mpz_set_si (result->value.integer, src->value.logical);
2310 /* Convert integer to logical. */
2313 gfc_int2log (gfc_expr *src, int kind)
2317 result = gfc_constant_result (BT_LOGICAL, kind, &src->where);
2318 result->value.logical = (mpz_cmp_si (src->value.integer, 0) != 0);
2324 /* Helper function to set the representation in a Hollerith conversion.
2325 This assumes that the ts.type and ts.kind of the result have already
2329 hollerith2representation (gfc_expr *result, gfc_expr *src)
2331 int src_len, result_len;
2333 src_len = src->representation.length;
2334 result_len = gfc_target_expr_size (result);
2336 if (src_len > result_len)
2338 gfc_warning ("The Hollerith constant at %L is too long to convert to %s",
2339 &src->where, gfc_typename(&result->ts));
2342 result->representation.string = gfc_getmem (result_len + 1);
2343 memcpy (result->representation.string, src->representation.string,
2344 MIN (result_len, src_len));
2346 if (src_len < result_len)
2347 memset (&result->representation.string[src_len], ' ', result_len - src_len);
2349 result->representation.string[result_len] = '\0'; /* For debugger */
2350 result->representation.length = result_len;
2354 /* Convert Hollerith to integer. The constant will be padded or truncated. */
2357 gfc_hollerith2int (gfc_expr *src, int kind)
2361 result = gfc_get_expr ();
2362 result->expr_type = EXPR_CONSTANT;
2363 result->ts.type = BT_INTEGER;
2364 result->ts.kind = kind;
2365 result->where = src->where;
2367 hollerith2representation (result, src);
2368 gfc_interpret_integer(kind, (unsigned char *) result->representation.string,
2369 result->representation.length, result->value.integer);
2375 /* Convert Hollerith to real. The constant will be padded or truncated. */
2378 gfc_hollerith2real (gfc_expr *src, int kind)
2383 len = src->value.character.length;
2385 result = gfc_get_expr ();
2386 result->expr_type = EXPR_CONSTANT;
2387 result->ts.type = BT_REAL;
2388 result->ts.kind = kind;
2389 result->where = src->where;
2391 hollerith2representation (result, src);
2392 gfc_interpret_float(kind, (unsigned char *) result->representation.string,
2393 result->representation.length, result->value.real);
2399 /* Convert Hollerith to complex. The constant will be padded or truncated. */
2402 gfc_hollerith2complex (gfc_expr *src, int kind)
2407 len = src->value.character.length;
2409 result = gfc_get_expr ();
2410 result->expr_type = EXPR_CONSTANT;
2411 result->ts.type = BT_COMPLEX;
2412 result->ts.kind = kind;
2413 result->where = src->where;
2415 hollerith2representation (result, src);
2416 gfc_interpret_complex(kind, (unsigned char *) result->representation.string,
2417 result->representation.length, result->value.complex.r,
2418 result->value.complex.i);
2424 /* Convert Hollerith to character. */
2427 gfc_hollerith2character (gfc_expr *src, int kind)
2431 result = gfc_copy_expr (src);
2432 result->ts.type = BT_CHARACTER;
2433 result->ts.kind = kind;
2435 result->value.character.string = result->representation.string;
2436 result->value.character.length = result->representation.length;
2442 /* Convert Hollerith to logical. The constant will be padded or truncated. */
2445 gfc_hollerith2logical (gfc_expr *src, int kind)
2450 len = src->value.character.length;
2452 result = gfc_get_expr ();
2453 result->expr_type = EXPR_CONSTANT;
2454 result->ts.type = BT_LOGICAL;
2455 result->ts.kind = kind;
2456 result->where = src->where;
2458 hollerith2representation (result, src);
2459 gfc_interpret_logical(kind, (unsigned char *) result->representation.string,
2460 result->representation.length, &result->value.logical);
2466 /* Returns an initializer whose value is one higher than the value of the
2467 LAST_INITIALIZER argument. If the argument is NULL, the
2468 initializers value will be set to zero. The initializer's kind
2469 will be set to gfc_c_int_kind.
2471 If -fshort-enums is given, the appropriate kind will be selected
2472 later after all enumerators have been parsed. A warning is issued
2473 here if an initializer exceeds gfc_c_int_kind. */
2476 gfc_enum_initializer (gfc_expr *last_initializer, locus where)
2480 result = gfc_get_expr ();
2481 result->expr_type = EXPR_CONSTANT;
2482 result->ts.type = BT_INTEGER;
2483 result->ts.kind = gfc_c_int_kind;
2484 result->where = where;
2486 mpz_init (result->value.integer);
2488 if (last_initializer != NULL)
2490 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
2491 result->where = last_initializer->where;
2493 if (gfc_check_integer_range (result->value.integer,
2494 gfc_c_int_kind) != ARITH_OK)
2496 gfc_error ("Enumerator exceeds the C integer type at %C");
2502 /* Control comes here, if it's the very first enumerator and no
2503 initializer has been given. It will be initialized to zero. */
2504 mpz_set_si (result->value.integer, 0);