2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation,
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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
23 /* Since target arithmetic must be done on the host, there has to
24 be some way of evaluating arithmetic expressions as the host
25 would evaluate them. We use the GNU MP library to do arithmetic,
26 and this file provides the interface. */
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));
78 /* Calculate atan2 (y, x)
80 atan2(y, x) = atan(y/x) if x > 0,
81 sign(y)*(pi - atan(|y/x|)) if x < 0,
83 sign(y)*pi/2 if x = 0 && y != 0.
87 arctangent2 (mpfr_t y, mpfr_t x, mpfr_t result)
99 mpfr_div (t, y, x, GFC_RND_MODE);
100 mpfr_atan (result, t, GFC_RND_MODE);
104 mpfr_const_pi (result, GFC_RND_MODE);
105 mpfr_div (t, y, x, GFC_RND_MODE);
106 mpfr_abs (t, t, GFC_RND_MODE);
107 mpfr_atan (t, t, GFC_RND_MODE);
108 mpfr_sub (result, result, t, GFC_RND_MODE);
109 if (mpfr_sgn (y) < 0)
110 mpfr_neg (result, result, GFC_RND_MODE);
114 if (mpfr_sgn (y) == 0)
115 mpfr_set_ui (result, 0, GFC_RND_MODE);
118 mpfr_const_pi (result, GFC_RND_MODE);
119 mpfr_div_ui (result, result, 2, GFC_RND_MODE);
120 if (mpfr_sgn (y) < 0)
121 mpfr_neg (result, result, GFC_RND_MODE);
130 /* Given an arithmetic error code, return a pointer to a string that
131 explains the error. */
134 gfc_arith_error (arith code)
141 p = _("Arithmetic OK at %L");
144 p = _("Arithmetic overflow at %L");
146 case ARITH_UNDERFLOW:
147 p = _("Arithmetic underflow at %L");
150 p = _("Arithmetic NaN at %L");
153 p = _("Division by zero at %L");
155 case ARITH_INCOMMENSURATE:
156 p = _("Array operands are incommensurate at %L");
158 case ARITH_ASYMMETRIC:
160 _("Integer outside symmetric range implied by Standard Fortran at %L");
163 gfc_internal_error ("gfc_arith_error(): Bad error code");
170 /* Get things ready to do math. */
173 gfc_arith_init_1 (void)
175 gfc_integer_info *int_info;
176 gfc_real_info *real_info;
181 mpfr_set_default_prec (128);
185 /* Convert the minimum/maximum values for each kind into their
186 GNU MP representation. */
187 for (int_info = gfc_integer_kinds; int_info->kind != 0; int_info++)
190 mpz_set_ui (r, int_info->radix);
191 mpz_pow_ui (r, r, int_info->digits);
193 mpz_init (int_info->huge);
194 mpz_sub_ui (int_info->huge, r, 1);
196 /* These are the numbers that are actually representable by the
197 target. For bases other than two, this needs to be changed. */
198 if (int_info->radix != 2)
199 gfc_internal_error ("Fix min_int, max_int calculation");
201 /* See PRs 13490 and 17912, related to integer ranges.
202 The pedantic_min_int exists for range checking when a program
203 is compiled with -pedantic, and reflects the belief that
204 Standard Fortran requires integers to be symmetrical, i.e.
205 every negative integer must have a representable positive
206 absolute value, and vice versa. */
208 mpz_init (int_info->pedantic_min_int);
209 mpz_neg (int_info->pedantic_min_int, int_info->huge);
211 mpz_init (int_info->min_int);
212 mpz_sub_ui (int_info->min_int, int_info->pedantic_min_int, 1);
214 mpz_init (int_info->max_int);
215 mpz_add (int_info->max_int, int_info->huge, int_info->huge);
216 mpz_add_ui (int_info->max_int, int_info->max_int, 1);
219 mpfr_set_z (a, int_info->huge, GFC_RND_MODE);
220 mpfr_log10 (a, a, GFC_RND_MODE);
222 gfc_mpfr_to_mpz (r, a);
223 int_info->range = mpz_get_si (r);
228 for (real_info = gfc_real_kinds; real_info->kind != 0; real_info++)
230 gfc_set_model_kind (real_info->kind);
236 /* huge(x) = (1 - b**(-p)) * b**(emax-1) * b */
237 /* a = 1 - b**(-p) */
238 mpfr_set_ui (a, 1, GFC_RND_MODE);
239 mpfr_set_ui (b, real_info->radix, GFC_RND_MODE);
240 mpfr_pow_si (b, b, -real_info->digits, GFC_RND_MODE);
241 mpfr_sub (a, a, b, GFC_RND_MODE);
243 /* c = b**(emax-1) */
244 mpfr_set_ui (b, real_info->radix, GFC_RND_MODE);
245 mpfr_pow_ui (c, b, real_info->max_exponent - 1, GFC_RND_MODE);
247 /* a = a * c = (1 - b**(-p)) * b**(emax-1) */
248 mpfr_mul (a, a, c, GFC_RND_MODE);
250 /* a = (1 - b**(-p)) * b**(emax-1) * b */
251 mpfr_mul_ui (a, a, real_info->radix, GFC_RND_MODE);
253 mpfr_init (real_info->huge);
254 mpfr_set (real_info->huge, a, GFC_RND_MODE);
256 /* tiny(x) = b**(emin-1) */
257 mpfr_set_ui (b, real_info->radix, GFC_RND_MODE);
258 mpfr_pow_si (b, b, real_info->min_exponent - 1, GFC_RND_MODE);
260 mpfr_init (real_info->tiny);
261 mpfr_set (real_info->tiny, b, GFC_RND_MODE);
263 /* subnormal (x) = b**(emin - digit) */
264 mpfr_set_ui (b, real_info->radix, GFC_RND_MODE);
265 mpfr_pow_si (b, b, real_info->min_exponent - real_info->digits,
268 mpfr_init (real_info->subnormal);
269 mpfr_set (real_info->subnormal, b, GFC_RND_MODE);
271 /* epsilon(x) = b**(1-p) */
272 mpfr_set_ui (b, real_info->radix, GFC_RND_MODE);
273 mpfr_pow_si (b, b, 1 - real_info->digits, GFC_RND_MODE);
275 mpfr_init (real_info->epsilon);
276 mpfr_set (real_info->epsilon, b, GFC_RND_MODE);
278 /* range(x) = int(min(log10(huge(x)), -log10(tiny)) */
279 mpfr_log10 (a, real_info->huge, GFC_RND_MODE);
280 mpfr_log10 (b, real_info->tiny, GFC_RND_MODE);
281 mpfr_neg (b, b, GFC_RND_MODE);
283 if (mpfr_cmp (a, b) > 0)
284 mpfr_set (a, b, GFC_RND_MODE); /* a = min(a, b) */
287 gfc_mpfr_to_mpz (r, a);
288 real_info->range = mpz_get_si (r);
290 /* precision(x) = int((p - 1) * log10(b)) + k */
291 mpfr_set_ui (a, real_info->radix, GFC_RND_MODE);
292 mpfr_log10 (a, a, GFC_RND_MODE);
294 mpfr_mul_ui (a, a, real_info->digits - 1, GFC_RND_MODE);
296 gfc_mpfr_to_mpz (r, a);
297 real_info->precision = mpz_get_si (r);
299 /* If the radix is an integral power of 10, add one to the
301 for (i = 10; i <= real_info->radix; i *= 10)
302 if (i == real_info->radix)
303 real_info->precision++;
314 /* Clean up, get rid of numeric constants. */
317 gfc_arith_done_1 (void)
319 gfc_integer_info *ip;
322 for (ip = gfc_integer_kinds; ip->kind; ip++)
324 mpz_clear (ip->min_int);
325 mpz_clear (ip->max_int);
326 mpz_clear (ip->huge);
329 for (rp = gfc_real_kinds; rp->kind; rp++)
331 mpfr_clear (rp->epsilon);
332 mpfr_clear (rp->huge);
333 mpfr_clear (rp->tiny);
338 /* Given an integer and a kind, make sure that the integer lies within
339 the range of the kind. Returns ARITH_OK, ARITH_ASYMMETRIC or
343 gfc_check_integer_range (mpz_t p, int kind)
348 i = gfc_validate_kind (BT_INTEGER, kind, false);
353 if (mpz_cmp (p, gfc_integer_kinds[i].pedantic_min_int) < 0)
354 result = ARITH_ASYMMETRIC;
357 if (mpz_cmp (p, gfc_integer_kinds[i].min_int) < 0
358 || mpz_cmp (p, gfc_integer_kinds[i].max_int) > 0)
359 result = ARITH_OVERFLOW;
365 /* Given a real and a kind, make sure that the real lies within the
366 range of the kind. Returns ARITH_OK, ARITH_OVERFLOW or
370 gfc_check_real_range (mpfr_t p, int kind)
376 i = gfc_validate_kind (BT_REAL, kind, false);
380 mpfr_abs (q, p, GFC_RND_MODE);
384 if (gfc_option.flag_range_check == 0)
387 retval = ARITH_OVERFLOW;
389 else if (mpfr_nan_p (p))
391 if (gfc_option.flag_range_check == 0)
396 else if (mpfr_sgn (q) == 0)
398 else if (mpfr_cmp (q, gfc_real_kinds[i].huge) > 0)
400 if (gfc_option.flag_range_check == 0)
403 retval = ARITH_OVERFLOW;
405 else if (mpfr_cmp (q, gfc_real_kinds[i].subnormal) < 0)
407 if (gfc_option.flag_range_check == 0)
410 retval = ARITH_UNDERFLOW;
412 else if (mpfr_cmp (q, gfc_real_kinds[i].tiny) < 0)
414 /* MPFR operates on a numbers with a given precision and enormous
415 exponential range. To represent subnormal numbers the exponent is
416 allowed to become smaller than emin, but always retains the full
417 precision. This function resets unused bits to 0 to alleviate
418 rounding problems. Note, a future version of MPFR will have a
419 mpfr_subnormalize() function, which handles this truncation in a
420 more efficient and robust way. */
426 bin = mpfr_get_str (NULL, &e, gfc_real_kinds[i].radix, 0, q, GMP_RNDN);
427 k = gfc_real_kinds[i].digits - (gfc_real_kinds[i].min_exponent - e);
428 for (j = k; j < gfc_real_kinds[i].digits; j++)
430 /* Need space for '0.', bin, 'E', and e */
431 s = (char *) gfc_getmem (strlen(bin)+10);
432 sprintf (s, "0.%sE%d", bin, (int) e);
433 mpfr_set_str (q, s, gfc_real_kinds[i].radix, GMP_RNDN);
435 if (mpfr_sgn (p) < 0)
436 mpfr_neg (p, q, GMP_RNDN);
438 mpfr_set (p, q, GMP_RNDN);
454 /* Function to return a constant expression node of a given type and
458 gfc_constant_result (bt type, int kind, locus * where)
464 ("gfc_constant_result(): locus 'where' cannot be NULL");
466 result = gfc_get_expr ();
468 result->expr_type = EXPR_CONSTANT;
469 result->ts.type = type;
470 result->ts.kind = kind;
471 result->where = *where;
476 mpz_init (result->value.integer);
480 gfc_set_model_kind (kind);
481 mpfr_init (result->value.real);
485 gfc_set_model_kind (kind);
486 mpfr_init (result->value.complex.r);
487 mpfr_init (result->value.complex.i);
498 /* Low-level arithmetic functions. All of these subroutines assume
499 that all operands are of the same type and return an operand of the
500 same type. The other thing about these subroutines is that they
501 can fail in various ways -- overflow, underflow, division by zero,
502 zero raised to the zero, etc. */
505 gfc_arith_not (gfc_expr * op1, gfc_expr ** resultp)
509 result = gfc_constant_result (BT_LOGICAL, op1->ts.kind, &op1->where);
510 result->value.logical = !op1->value.logical;
518 gfc_arith_and (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
522 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
524 result->value.logical = op1->value.logical && op2->value.logical;
532 gfc_arith_or (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
536 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
538 result->value.logical = op1->value.logical || op2->value.logical;
546 gfc_arith_eqv (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
550 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
552 result->value.logical = op1->value.logical == op2->value.logical;
560 gfc_arith_neqv (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
564 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
566 result->value.logical = op1->value.logical != op2->value.logical;
573 /* Make sure a constant numeric expression is within the range for
574 its type and kind. Note that there's also a gfc_check_range(),
575 but that one deals with the intrinsic RANGE function. */
578 gfc_range_check (gfc_expr * e)
585 rc = gfc_check_integer_range (e->value.integer, e->ts.kind);
589 rc = gfc_check_real_range (e->value.real, e->ts.kind);
590 if (rc == ARITH_UNDERFLOW)
591 mpfr_set_ui (e->value.real, 0, GFC_RND_MODE);
592 if (rc == ARITH_OVERFLOW)
593 mpfr_set_inf (e->value.real, mpfr_sgn (e->value.real));
595 mpfr_set_nan (e->value.real);
599 rc = gfc_check_real_range (e->value.complex.r, e->ts.kind);
600 if (rc == ARITH_UNDERFLOW)
601 mpfr_set_ui (e->value.complex.r, 0, GFC_RND_MODE);
602 if (rc == ARITH_OVERFLOW)
603 mpfr_set_inf (e->value.complex.r, mpfr_sgn (e->value.complex.r));
605 mpfr_set_nan (e->value.complex.r);
607 rc = gfc_check_real_range (e->value.complex.i, e->ts.kind);
608 if (rc == ARITH_UNDERFLOW)
609 mpfr_set_ui (e->value.complex.i, 0, GFC_RND_MODE);
610 if (rc == ARITH_OVERFLOW)
611 mpfr_set_inf (e->value.complex.i, mpfr_sgn (e->value.complex.i));
613 mpfr_set_nan (e->value.complex.i);
618 gfc_internal_error ("gfc_range_check(): Bad type");
625 /* Several of the following routines use the same set of statements to
626 check the validity of the result. Encapsulate the checking here. */
629 check_result (arith rc, gfc_expr * x, gfc_expr * r, gfc_expr ** rp)
633 if (val == ARITH_UNDERFLOW)
635 if (gfc_option.warn_underflow)
636 gfc_warning (gfc_arith_error (val), &x->where);
640 if (val == ARITH_ASYMMETRIC)
642 gfc_warning (gfc_arith_error (val), &x->where);
655 /* It may seem silly to have a subroutine that actually computes the
656 unary plus of a constant, but it prevents us from making exceptions
657 in the code elsewhere. */
660 gfc_arith_uplus (gfc_expr * op1, gfc_expr ** resultp)
662 *resultp = gfc_copy_expr (op1);
668 gfc_arith_uminus (gfc_expr * op1, gfc_expr ** resultp)
673 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
675 switch (op1->ts.type)
678 mpz_neg (result->value.integer, op1->value.integer);
682 mpfr_neg (result->value.real, op1->value.real, GFC_RND_MODE);
686 mpfr_neg (result->value.complex.r, op1->value.complex.r, GFC_RND_MODE);
687 mpfr_neg (result->value.complex.i, op1->value.complex.i, GFC_RND_MODE);
691 gfc_internal_error ("gfc_arith_uminus(): Bad basic type");
694 rc = gfc_range_check (result);
696 return check_result (rc, op1, result, resultp);
701 gfc_arith_plus (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
706 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
708 switch (op1->ts.type)
711 mpz_add (result->value.integer, op1->value.integer, op2->value.integer);
715 mpfr_add (result->value.real, op1->value.real, op2->value.real,
720 mpfr_add (result->value.complex.r, op1->value.complex.r,
721 op2->value.complex.r, GFC_RND_MODE);
723 mpfr_add (result->value.complex.i, op1->value.complex.i,
724 op2->value.complex.i, GFC_RND_MODE);
728 gfc_internal_error ("gfc_arith_plus(): Bad basic type");
731 rc = gfc_range_check (result);
733 return check_result (rc, op1, result, resultp);
738 gfc_arith_minus (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
743 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
745 switch (op1->ts.type)
748 mpz_sub (result->value.integer, op1->value.integer, op2->value.integer);
752 mpfr_sub (result->value.real, op1->value.real, op2->value.real,
757 mpfr_sub (result->value.complex.r, op1->value.complex.r,
758 op2->value.complex.r, GFC_RND_MODE);
760 mpfr_sub (result->value.complex.i, op1->value.complex.i,
761 op2->value.complex.i, GFC_RND_MODE);
765 gfc_internal_error ("gfc_arith_minus(): Bad basic type");
768 rc = gfc_range_check (result);
770 return check_result (rc, op1, result, resultp);
775 gfc_arith_times (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
781 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
783 switch (op1->ts.type)
786 mpz_mul (result->value.integer, op1->value.integer, op2->value.integer);
790 mpfr_mul (result->value.real, op1->value.real, op2->value.real,
796 /* FIXME: possible numericals problem. */
798 gfc_set_model (op1->value.complex.r);
802 mpfr_mul (x, op1->value.complex.r, op2->value.complex.r, GFC_RND_MODE);
803 mpfr_mul (y, op1->value.complex.i, op2->value.complex.i, GFC_RND_MODE);
804 mpfr_sub (result->value.complex.r, x, y, GFC_RND_MODE);
806 mpfr_mul (x, op1->value.complex.r, op2->value.complex.i, GFC_RND_MODE);
807 mpfr_mul (y, op1->value.complex.i, op2->value.complex.r, GFC_RND_MODE);
808 mpfr_add (result->value.complex.i, x, y, GFC_RND_MODE);
816 gfc_internal_error ("gfc_arith_times(): Bad basic type");
819 rc = gfc_range_check (result);
821 return check_result (rc, op1, result, resultp);
826 gfc_arith_divide (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
834 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
836 switch (op1->ts.type)
839 if (mpz_sgn (op2->value.integer) == 0)
845 mpz_tdiv_q (result->value.integer, op1->value.integer,
850 if (mpfr_sgn (op2->value.real) == 0
851 && gfc_option.flag_range_check == 1)
857 mpfr_div (result->value.real, op1->value.real, op2->value.real,
862 if (mpfr_sgn (op2->value.complex.r) == 0
863 && mpfr_sgn (op2->value.complex.i) == 0
864 && gfc_option.flag_range_check == 1)
870 gfc_set_model (op1->value.complex.r);
875 /* FIXME: possible numerical problems. */
876 mpfr_mul (x, op2->value.complex.r, op2->value.complex.r, GFC_RND_MODE);
877 mpfr_mul (y, op2->value.complex.i, op2->value.complex.i, GFC_RND_MODE);
878 mpfr_add (div, x, y, GFC_RND_MODE);
880 mpfr_mul (x, op1->value.complex.r, op2->value.complex.r, GFC_RND_MODE);
881 mpfr_mul (y, op1->value.complex.i, op2->value.complex.i, GFC_RND_MODE);
882 mpfr_add (result->value.complex.r, x, y, GFC_RND_MODE);
883 mpfr_div (result->value.complex.r, result->value.complex.r, div,
886 mpfr_mul (x, op1->value.complex.i, op2->value.complex.r, GFC_RND_MODE);
887 mpfr_mul (y, op1->value.complex.r, op2->value.complex.i, GFC_RND_MODE);
888 mpfr_sub (result->value.complex.i, x, y, GFC_RND_MODE);
889 mpfr_div (result->value.complex.i, result->value.complex.i, div,
899 gfc_internal_error ("gfc_arith_divide(): Bad basic type");
903 rc = gfc_range_check (result);
905 return check_result (rc, op1, result, resultp);
909 /* Compute the reciprocal of a complex number (guaranteed nonzero). */
912 complex_reciprocal (gfc_expr * op)
914 mpfr_t mod, a, re, im;
916 gfc_set_model (op->value.complex.r);
922 /* FIXME: another possible numerical problem. */
923 mpfr_mul (mod, op->value.complex.r, op->value.complex.r, GFC_RND_MODE);
924 mpfr_mul (a, op->value.complex.i, op->value.complex.i, GFC_RND_MODE);
925 mpfr_add (mod, mod, a, GFC_RND_MODE);
927 mpfr_div (re, op->value.complex.r, mod, GFC_RND_MODE);
929 mpfr_neg (im, op->value.complex.i, GFC_RND_MODE);
930 mpfr_div (im, im, mod, GFC_RND_MODE);
932 mpfr_set (op->value.complex.r, re, GFC_RND_MODE);
933 mpfr_set (op->value.complex.i, im, GFC_RND_MODE);
942 /* Raise a complex number to positive power. */
945 complex_pow_ui (gfc_expr * base, int power, gfc_expr * result)
949 gfc_set_model (base->value.complex.r);
954 mpfr_set_ui (result->value.complex.r, 1, GFC_RND_MODE);
955 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
957 for (; power > 0; power--)
959 mpfr_mul (re, base->value.complex.r, result->value.complex.r,
961 mpfr_mul (a, base->value.complex.i, result->value.complex.i,
963 mpfr_sub (re, re, a, GFC_RND_MODE);
965 mpfr_mul (im, base->value.complex.r, result->value.complex.i,
967 mpfr_mul (a, base->value.complex.i, result->value.complex.r,
969 mpfr_add (im, im, a, GFC_RND_MODE);
971 mpfr_set (result->value.complex.r, re, GFC_RND_MODE);
972 mpfr_set (result->value.complex.i, im, GFC_RND_MODE);
981 /* Raise a number to an integer power. */
984 gfc_arith_power (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
994 if (gfc_extract_int (op2, &power) != NULL)
995 gfc_internal_error ("gfc_arith_power(): Bad exponent");
997 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
1001 /* Handle something to the zeroth power. Since we're dealing
1002 with integral exponents, there is no ambiguity in the
1003 limiting procedure used to determine the value of 0**0. */
1004 switch (op1->ts.type)
1007 mpz_set_ui (result->value.integer, 1);
1011 mpfr_set_ui (result->value.real, 1, GFC_RND_MODE);
1015 mpfr_set_ui (result->value.complex.r, 1, GFC_RND_MODE);
1016 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
1020 gfc_internal_error ("gfc_arith_power(): Bad base");
1029 switch (op1->ts.type)
1032 mpz_pow_ui (result->value.integer, op1->value.integer, apower);
1036 mpz_init_set_ui (unity_z, 1);
1037 mpz_tdiv_q (result->value.integer, unity_z,
1038 result->value.integer);
1039 mpz_clear (unity_z);
1045 mpfr_pow_ui (result->value.real, op1->value.real, apower,
1050 gfc_set_model (op1->value.real);
1051 mpfr_init (unity_f);
1052 mpfr_set_ui (unity_f, 1, GFC_RND_MODE);
1053 mpfr_div (result->value.real, unity_f, result->value.real,
1055 mpfr_clear (unity_f);
1060 complex_pow_ui (op1, apower, result);
1062 complex_reciprocal (result);
1071 rc = gfc_range_check (result);
1073 return check_result (rc, op1, result, resultp);
1077 /* Concatenate two string constants. */
1080 gfc_arith_concat (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1085 result = gfc_constant_result (BT_CHARACTER, gfc_default_character_kind,
1088 len = op1->value.character.length + op2->value.character.length;
1090 result->value.character.string = gfc_getmem (len + 1);
1091 result->value.character.length = len;
1093 memcpy (result->value.character.string, op1->value.character.string,
1094 op1->value.character.length);
1096 memcpy (result->value.character.string + op1->value.character.length,
1097 op2->value.character.string, op2->value.character.length);
1099 result->value.character.string[len] = '\0';
1107 /* Comparison operators. Assumes that the two expression nodes
1108 contain two constants of the same type. */
1111 gfc_compare_expr (gfc_expr * op1, gfc_expr * op2)
1115 switch (op1->ts.type)
1118 rc = mpz_cmp (op1->value.integer, op2->value.integer);
1122 rc = mpfr_cmp (op1->value.real, op2->value.real);
1126 rc = gfc_compare_string (op1, op2, NULL);
1130 rc = ((!op1->value.logical && op2->value.logical)
1131 || (op1->value.logical && !op2->value.logical));
1135 gfc_internal_error ("gfc_compare_expr(): Bad basic type");
1142 /* Compare a pair of complex numbers. Naturally, this is only for
1143 equality/nonequality. */
1146 compare_complex (gfc_expr * op1, gfc_expr * op2)
1148 return (mpfr_cmp (op1->value.complex.r, op2->value.complex.r) == 0
1149 && mpfr_cmp (op1->value.complex.i, op2->value.complex.i) == 0);
1153 /* Given two constant strings and the inverse collating sequence,
1154 compare the strings. We return -1 for a<b, 0 for a==b and 1 for
1155 a>b. If the xcoll_table is NULL, we use the processor's default
1156 collating sequence. */
1159 gfc_compare_string (gfc_expr * a, gfc_expr * b, const int *xcoll_table)
1161 int len, alen, blen, i, ac, bc;
1163 alen = a->value.character.length;
1164 blen = b->value.character.length;
1166 len = (alen > blen) ? alen : blen;
1168 for (i = 0; i < len; i++)
1170 /* We cast to unsigned char because default char, if it is signed,
1171 would lead to ac<0 for string[i] > 127. */
1172 ac = (unsigned char) ((i < alen) ? a->value.character.string[i] : ' ');
1173 bc = (unsigned char) ((i < blen) ? b->value.character.string[i] : ' ');
1175 if (xcoll_table != NULL)
1177 ac = xcoll_table[ac];
1178 bc = xcoll_table[bc];
1187 /* Strings are equal */
1193 /* Specific comparison subroutines. */
1196 gfc_arith_eq (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1200 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
1202 result->value.logical = (op1->ts.type == BT_COMPLEX) ?
1203 compare_complex (op1, op2) : (gfc_compare_expr (op1, op2) == 0);
1211 gfc_arith_ne (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1215 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
1217 result->value.logical = (op1->ts.type == BT_COMPLEX) ?
1218 !compare_complex (op1, op2) : (gfc_compare_expr (op1, op2) != 0);
1226 gfc_arith_gt (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1230 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
1232 result->value.logical = (gfc_compare_expr (op1, op2) > 0);
1240 gfc_arith_ge (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1244 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
1246 result->value.logical = (gfc_compare_expr (op1, op2) >= 0);
1254 gfc_arith_lt (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1258 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
1260 result->value.logical = (gfc_compare_expr (op1, op2) < 0);
1268 gfc_arith_le (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1272 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
1274 result->value.logical = (gfc_compare_expr (op1, op2) <= 0);
1282 reduce_unary (arith (*eval) (gfc_expr *, gfc_expr **), gfc_expr * op,
1285 gfc_constructor *c, *head;
1289 if (op->expr_type == EXPR_CONSTANT)
1290 return eval (op, result);
1293 head = gfc_copy_constructor (op->value.constructor);
1295 for (c = head; c; c = c->next)
1297 rc = eval (c->expr, &r);
1301 gfc_replace_expr (c->expr, r);
1305 gfc_free_constructor (head);
1308 r = gfc_get_expr ();
1309 r->expr_type = EXPR_ARRAY;
1310 r->value.constructor = head;
1311 r->shape = gfc_copy_shape (op->shape, op->rank);
1313 r->ts = head->expr->ts;
1314 r->where = op->where;
1325 reduce_binary_ac (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1326 gfc_expr * op1, gfc_expr * op2,
1329 gfc_constructor *c, *head;
1333 head = gfc_copy_constructor (op1->value.constructor);
1336 for (c = head; c; c = c->next)
1338 rc = eval (c->expr, op2, &r);
1342 gfc_replace_expr (c->expr, r);
1346 gfc_free_constructor (head);
1349 r = gfc_get_expr ();
1350 r->expr_type = EXPR_ARRAY;
1351 r->value.constructor = head;
1352 r->shape = gfc_copy_shape (op1->shape, op1->rank);
1354 r->ts = head->expr->ts;
1355 r->where = op1->where;
1356 r->rank = op1->rank;
1366 reduce_binary_ca (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1367 gfc_expr * op1, gfc_expr * op2,
1370 gfc_constructor *c, *head;
1374 head = gfc_copy_constructor (op2->value.constructor);
1377 for (c = head; c; c = c->next)
1379 rc = eval (op1, c->expr, &r);
1383 gfc_replace_expr (c->expr, r);
1387 gfc_free_constructor (head);
1390 r = gfc_get_expr ();
1391 r->expr_type = EXPR_ARRAY;
1392 r->value.constructor = head;
1393 r->shape = gfc_copy_shape (op2->shape, op2->rank);
1395 r->ts = head->expr->ts;
1396 r->where = op2->where;
1397 r->rank = op2->rank;
1407 reduce_binary_aa (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1408 gfc_expr * op1, gfc_expr * op2,
1411 gfc_constructor *c, *d, *head;
1415 head = gfc_copy_constructor (op1->value.constructor);
1418 d = op2->value.constructor;
1420 if (gfc_check_conformance ("Elemental binary operation", op1, op2)
1422 rc = ARITH_INCOMMENSURATE;
1426 for (c = head; c; c = c->next, d = d->next)
1430 rc = ARITH_INCOMMENSURATE;
1434 rc = eval (c->expr, d->expr, &r);
1438 gfc_replace_expr (c->expr, r);
1442 rc = ARITH_INCOMMENSURATE;
1446 gfc_free_constructor (head);
1449 r = gfc_get_expr ();
1450 r->expr_type = EXPR_ARRAY;
1451 r->value.constructor = head;
1452 r->shape = gfc_copy_shape (op1->shape, op1->rank);
1454 r->ts = head->expr->ts;
1455 r->where = op1->where;
1456 r->rank = op1->rank;
1466 reduce_binary (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1467 gfc_expr * op1, gfc_expr * op2,
1470 if (op1->expr_type == EXPR_CONSTANT && op2->expr_type == EXPR_CONSTANT)
1471 return eval (op1, op2, result);
1473 if (op1->expr_type == EXPR_CONSTANT && op2->expr_type == EXPR_ARRAY)
1474 return reduce_binary_ca (eval, op1, op2, result);
1476 if (op1->expr_type == EXPR_ARRAY && op2->expr_type == EXPR_CONSTANT)
1477 return reduce_binary_ac (eval, op1, op2, result);
1479 return reduce_binary_aa (eval, op1, op2, result);
1485 arith (*f2)(gfc_expr *, gfc_expr **);
1486 arith (*f3)(gfc_expr *, gfc_expr *, gfc_expr **);
1490 /* High level arithmetic subroutines. These subroutines go into
1491 eval_intrinsic(), which can do one of several things to its
1492 operands. If the operands are incompatible with the intrinsic
1493 operation, we return a node pointing to the operands and hope that
1494 an operator interface is found during resolution.
1496 If the operands are compatible and are constants, then we try doing
1497 the arithmetic. We also handle the cases where either or both
1498 operands are array constructors. */
1501 eval_intrinsic (gfc_intrinsic_op operator,
1502 eval_f eval, gfc_expr * op1, gfc_expr * op2)
1504 gfc_expr temp, *result;
1508 gfc_clear_ts (&temp.ts);
1512 case INTRINSIC_NOT: /* Logical unary */
1513 if (op1->ts.type != BT_LOGICAL)
1516 temp.ts.type = BT_LOGICAL;
1517 temp.ts.kind = gfc_default_logical_kind;
1522 /* Logical binary operators */
1525 case INTRINSIC_NEQV:
1527 if (op1->ts.type != BT_LOGICAL || op2->ts.type != BT_LOGICAL)
1530 temp.ts.type = BT_LOGICAL;
1531 temp.ts.kind = gfc_default_logical_kind;
1536 case INTRINSIC_UPLUS:
1537 case INTRINSIC_UMINUS: /* Numeric unary */
1538 if (!gfc_numeric_ts (&op1->ts))
1546 case INTRINSIC_PARENTHESES:
1553 case INTRINSIC_LT: /* Additional restrictions */
1554 case INTRINSIC_LE: /* for ordering relations. */
1556 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
1558 temp.ts.type = BT_LOGICAL;
1559 temp.ts.kind = gfc_default_logical_kind;
1563 /* else fall through */
1567 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
1570 temp.ts.type = BT_LOGICAL;
1571 temp.ts.kind = gfc_default_logical_kind;
1575 /* else fall through */
1577 case INTRINSIC_PLUS:
1578 case INTRINSIC_MINUS:
1579 case INTRINSIC_TIMES:
1580 case INTRINSIC_DIVIDE:
1581 case INTRINSIC_POWER: /* Numeric binary */
1582 if (!gfc_numeric_ts (&op1->ts) || !gfc_numeric_ts (&op2->ts))
1585 /* Insert any necessary type conversions to make the operands compatible. */
1587 temp.expr_type = EXPR_OP;
1588 gfc_clear_ts (&temp.ts);
1589 temp.value.op.operator = operator;
1591 temp.value.op.op1 = op1;
1592 temp.value.op.op2 = op2;
1594 gfc_type_convert_binary (&temp);
1596 if (operator == INTRINSIC_EQ || operator == INTRINSIC_NE
1597 || operator == INTRINSIC_GE || operator == INTRINSIC_GT
1598 || operator == INTRINSIC_LE || operator == INTRINSIC_LT)
1600 temp.ts.type = BT_LOGICAL;
1601 temp.ts.kind = gfc_default_logical_kind;
1607 case INTRINSIC_CONCAT: /* Character binary */
1608 if (op1->ts.type != BT_CHARACTER || op2->ts.type != BT_CHARACTER)
1611 temp.ts.type = BT_CHARACTER;
1612 temp.ts.kind = gfc_default_character_kind;
1617 case INTRINSIC_USER:
1621 gfc_internal_error ("eval_intrinsic(): Bad operator");
1624 /* Try to combine the operators. */
1625 if (operator == INTRINSIC_POWER && op2->ts.type != BT_INTEGER)
1629 || (op1->expr_type != EXPR_CONSTANT
1630 && (op1->expr_type != EXPR_ARRAY
1631 || !gfc_is_constant_expr (op1)
1632 || !gfc_expanded_ac (op1))))
1637 || (op2->expr_type != EXPR_CONSTANT
1638 && (op2->expr_type != EXPR_ARRAY
1639 || !gfc_is_constant_expr (op2)
1640 || !gfc_expanded_ac (op2)))))
1644 rc = reduce_unary (eval.f2, op1, &result);
1646 rc = reduce_binary (eval.f3, op1, op2, &result);
1649 { /* Something went wrong */
1650 gfc_error (gfc_arith_error (rc), &op1->where);
1654 gfc_free_expr (op1);
1655 gfc_free_expr (op2);
1659 /* Create a run-time expression */
1660 result = gfc_get_expr ();
1661 result->ts = temp.ts;
1663 result->expr_type = EXPR_OP;
1664 result->value.op.operator = operator;
1666 result->value.op.op1 = op1;
1667 result->value.op.op2 = op2;
1669 result->where = op1->where;
1675 /* Modify type of expression for zero size array. */
1677 eval_type_intrinsic0 (gfc_intrinsic_op operator, gfc_expr *op)
1680 gfc_internal_error ("eval_type_intrinsic0(): op NULL");
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);
1781 gfc_uplus (gfc_expr * op)
1783 return eval_intrinsic_f2 (INTRINSIC_UPLUS, gfc_arith_uplus, op, NULL);
1787 gfc_uminus (gfc_expr * op)
1789 return eval_intrinsic_f2 (INTRINSIC_UMINUS, gfc_arith_uminus, op, NULL);
1793 gfc_add (gfc_expr * op1, gfc_expr * op2)
1795 return eval_intrinsic_f3 (INTRINSIC_PLUS, gfc_arith_plus, op1, op2);
1799 gfc_subtract (gfc_expr * op1, gfc_expr * op2)
1801 return eval_intrinsic_f3 (INTRINSIC_MINUS, gfc_arith_minus, op1, op2);
1805 gfc_multiply (gfc_expr * op1, gfc_expr * op2)
1807 return eval_intrinsic_f3 (INTRINSIC_TIMES, gfc_arith_times, op1, op2);
1811 gfc_divide (gfc_expr * op1, gfc_expr * op2)
1813 return eval_intrinsic_f3 (INTRINSIC_DIVIDE, gfc_arith_divide, op1, op2);
1817 gfc_power (gfc_expr * op1, gfc_expr * op2)
1819 return eval_intrinsic_f3 (INTRINSIC_POWER, gfc_arith_power, op1, op2);
1823 gfc_concat (gfc_expr * op1, gfc_expr * op2)
1825 return eval_intrinsic_f3 (INTRINSIC_CONCAT, gfc_arith_concat, op1, op2);
1829 gfc_and (gfc_expr * op1, gfc_expr * op2)
1831 return eval_intrinsic_f3 (INTRINSIC_AND, gfc_arith_and, op1, op2);
1835 gfc_or (gfc_expr * op1, gfc_expr * op2)
1837 return eval_intrinsic_f3 (INTRINSIC_OR, gfc_arith_or, op1, op2);
1841 gfc_not (gfc_expr * op1)
1843 return eval_intrinsic_f2 (INTRINSIC_NOT, gfc_arith_not, op1, NULL);
1847 gfc_eqv (gfc_expr * op1, gfc_expr * op2)
1849 return eval_intrinsic_f3 (INTRINSIC_EQV, gfc_arith_eqv, op1, op2);
1853 gfc_neqv (gfc_expr * op1, gfc_expr * op2)
1855 return eval_intrinsic_f3 (INTRINSIC_NEQV, gfc_arith_neqv, op1, op2);
1859 gfc_eq (gfc_expr * op1, gfc_expr * op2)
1861 return eval_intrinsic_f3 (INTRINSIC_EQ, gfc_arith_eq, op1, op2);
1865 gfc_ne (gfc_expr * op1, gfc_expr * op2)
1867 return eval_intrinsic_f3 (INTRINSIC_NE, gfc_arith_ne, op1, op2);
1871 gfc_gt (gfc_expr * op1, gfc_expr * op2)
1873 return eval_intrinsic_f3 (INTRINSIC_GT, gfc_arith_gt, op1, op2);
1877 gfc_ge (gfc_expr * op1, gfc_expr * op2)
1879 return eval_intrinsic_f3 (INTRINSIC_GE, gfc_arith_ge, op1, op2);
1883 gfc_lt (gfc_expr * op1, gfc_expr * op2)
1885 return eval_intrinsic_f3 (INTRINSIC_LT, gfc_arith_lt, op1, op2);
1889 gfc_le (gfc_expr * op1, gfc_expr * op2)
1891 return eval_intrinsic_f3 (INTRINSIC_LE, gfc_arith_le, op1, op2);
1895 /* Convert an integer string to an expression node. */
1898 gfc_convert_integer (const char *buffer, int kind, int radix, locus * where)
1903 e = gfc_constant_result (BT_INTEGER, kind, where);
1904 /* a leading plus is allowed, but not by mpz_set_str */
1905 if (buffer[0] == '+')
1909 mpz_set_str (e->value.integer, t, radix);
1915 /* Convert a real string to an expression node. */
1918 gfc_convert_real (const char *buffer, int kind, locus * where)
1922 e = gfc_constant_result (BT_REAL, kind, where);
1923 mpfr_set_str (e->value.real, buffer, 10, GFC_RND_MODE);
1929 /* Convert a pair of real, constant expression nodes to a single
1930 complex expression node. */
1933 gfc_convert_complex (gfc_expr * real, gfc_expr * imag, int kind)
1937 e = gfc_constant_result (BT_COMPLEX, kind, &real->where);
1938 mpfr_set (e->value.complex.r, real->value.real, GFC_RND_MODE);
1939 mpfr_set (e->value.complex.i, imag->value.real, GFC_RND_MODE);
1945 /******* Simplification of intrinsic functions with constant arguments *****/
1948 /* Deal with an arithmetic error. */
1951 arith_error (arith rc, gfc_typespec * from, gfc_typespec * to, locus * where)
1956 gfc_error ("Arithmetic OK converting %s to %s at %L",
1957 gfc_typename (from), gfc_typename (to), where);
1959 case ARITH_OVERFLOW:
1960 gfc_error ("Arithmetic overflow converting %s to %s at %L",
1961 gfc_typename (from), gfc_typename (to), where);
1963 case ARITH_UNDERFLOW:
1964 gfc_error ("Arithmetic underflow converting %s to %s at %L",
1965 gfc_typename (from), gfc_typename (to), where);
1968 gfc_error ("Arithmetic NaN converting %s to %s at %L",
1969 gfc_typename (from), gfc_typename (to), where);
1972 gfc_error ("Division by zero converting %s to %s at %L",
1973 gfc_typename (from), gfc_typename (to), where);
1975 case ARITH_INCOMMENSURATE:
1976 gfc_error ("Array operands are incommensurate converting %s to %s at %L",
1977 gfc_typename (from), gfc_typename (to), where);
1979 case ARITH_ASYMMETRIC:
1980 gfc_error ("Integer outside symmetric range implied by Standard Fortran"
1981 " converting %s to %s at %L",
1982 gfc_typename (from), gfc_typename (to), where);
1985 gfc_internal_error ("gfc_arith_error(): Bad error code");
1988 /* TODO: Do something about the error, ie, throw exception, return
1992 /* Convert integers to integers. */
1995 gfc_int2int (gfc_expr * src, int kind)
2000 result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2002 mpz_set (result->value.integer, src->value.integer);
2004 if ((rc = gfc_check_integer_range (result->value.integer, kind))
2007 if (rc == ARITH_ASYMMETRIC)
2009 gfc_warning (gfc_arith_error (rc), &src->where);
2013 arith_error (rc, &src->ts, &result->ts, &src->where);
2014 gfc_free_expr (result);
2023 /* Convert integers to reals. */
2026 gfc_int2real (gfc_expr * src, int kind)
2031 result = gfc_constant_result (BT_REAL, kind, &src->where);
2033 mpfr_set_z (result->value.real, src->value.integer, GFC_RND_MODE);
2035 if ((rc = gfc_check_real_range (result->value.real, kind)) != ARITH_OK)
2037 arith_error (rc, &src->ts, &result->ts, &src->where);
2038 gfc_free_expr (result);
2046 /* Convert default integer to default complex. */
2049 gfc_int2complex (gfc_expr * src, int kind)
2054 result = gfc_constant_result (BT_COMPLEX, kind, &src->where);
2056 mpfr_set_z (result->value.complex.r, src->value.integer, GFC_RND_MODE);
2057 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
2059 if ((rc = gfc_check_real_range (result->value.complex.r, kind)) != ARITH_OK)
2061 arith_error (rc, &src->ts, &result->ts, &src->where);
2062 gfc_free_expr (result);
2070 /* Convert default real to default integer. */
2073 gfc_real2int (gfc_expr * src, int kind)
2078 result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2080 gfc_mpfr_to_mpz (result->value.integer, src->value.real);
2082 if ((rc = gfc_check_integer_range (result->value.integer, kind))
2085 arith_error (rc, &src->ts, &result->ts, &src->where);
2086 gfc_free_expr (result);
2094 /* Convert real to real. */
2097 gfc_real2real (gfc_expr * src, int kind)
2102 result = gfc_constant_result (BT_REAL, kind, &src->where);
2104 mpfr_set (result->value.real, src->value.real, GFC_RND_MODE);
2106 rc = gfc_check_real_range (result->value.real, kind);
2108 if (rc == ARITH_UNDERFLOW)
2110 if (gfc_option.warn_underflow)
2111 gfc_warning (gfc_arith_error (rc), &src->where);
2112 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
2114 else if (rc != ARITH_OK)
2116 arith_error (rc, &src->ts, &result->ts, &src->where);
2117 gfc_free_expr (result);
2125 /* Convert real to complex. */
2128 gfc_real2complex (gfc_expr * src, int kind)
2133 result = gfc_constant_result (BT_COMPLEX, kind, &src->where);
2135 mpfr_set (result->value.complex.r, src->value.real, GFC_RND_MODE);
2136 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
2138 rc = gfc_check_real_range (result->value.complex.r, kind);
2140 if (rc == ARITH_UNDERFLOW)
2142 if (gfc_option.warn_underflow)
2143 gfc_warning (gfc_arith_error (rc), &src->where);
2144 mpfr_set_ui (result->value.complex.r, 0, GFC_RND_MODE);
2146 else if (rc != ARITH_OK)
2148 arith_error (rc, &src->ts, &result->ts, &src->where);
2149 gfc_free_expr (result);
2157 /* Convert complex to integer. */
2160 gfc_complex2int (gfc_expr * src, int kind)
2165 result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2167 gfc_mpfr_to_mpz (result->value.integer, src->value.complex.r);
2169 if ((rc = gfc_check_integer_range (result->value.integer, kind))
2172 arith_error (rc, &src->ts, &result->ts, &src->where);
2173 gfc_free_expr (result);
2181 /* Convert complex to real. */
2184 gfc_complex2real (gfc_expr * src, int kind)
2189 result = gfc_constant_result (BT_REAL, kind, &src->where);
2191 mpfr_set (result->value.real, src->value.complex.r, GFC_RND_MODE);
2193 rc = gfc_check_real_range (result->value.real, kind);
2195 if (rc == ARITH_UNDERFLOW)
2197 if (gfc_option.warn_underflow)
2198 gfc_warning (gfc_arith_error (rc), &src->where);
2199 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
2203 arith_error (rc, &src->ts, &result->ts, &src->where);
2204 gfc_free_expr (result);
2212 /* Convert complex to complex. */
2215 gfc_complex2complex (gfc_expr * src, int kind)
2220 result = gfc_constant_result (BT_COMPLEX, kind, &src->where);
2222 mpfr_set (result->value.complex.r, src->value.complex.r, GFC_RND_MODE);
2223 mpfr_set (result->value.complex.i, src->value.complex.i, GFC_RND_MODE);
2225 rc = gfc_check_real_range (result->value.complex.r, kind);
2227 if (rc == ARITH_UNDERFLOW)
2229 if (gfc_option.warn_underflow)
2230 gfc_warning (gfc_arith_error (rc), &src->where);
2231 mpfr_set_ui (result->value.complex.r, 0, GFC_RND_MODE);
2233 else if (rc != ARITH_OK)
2235 arith_error (rc, &src->ts, &result->ts, &src->where);
2236 gfc_free_expr (result);
2240 rc = gfc_check_real_range (result->value.complex.i, kind);
2242 if (rc == ARITH_UNDERFLOW)
2244 if (gfc_option.warn_underflow)
2245 gfc_warning (gfc_arith_error (rc), &src->where);
2246 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
2248 else if (rc != ARITH_OK)
2250 arith_error (rc, &src->ts, &result->ts, &src->where);
2251 gfc_free_expr (result);
2259 /* Logical kind conversion. */
2262 gfc_log2log (gfc_expr * src, int kind)
2266 result = gfc_constant_result (BT_LOGICAL, kind, &src->where);
2267 result->value.logical = src->value.logical;
2272 /* Convert logical to integer. */
2275 gfc_log2int (gfc_expr *src, int kind)
2278 result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2279 mpz_set_si (result->value.integer, src->value.logical);
2283 /* Convert integer to logical. */
2286 gfc_int2log (gfc_expr *src, int kind)
2289 result = gfc_constant_result (BT_LOGICAL, kind, &src->where);
2290 result->value.logical = (mpz_cmp_si (src->value.integer, 0) != 0);
2294 /* Convert Hollerith to integer. The constant will be padded or truncated. */
2297 gfc_hollerith2int (gfc_expr * src, int kind)
2302 len = src->value.character.length;
2304 result = gfc_get_expr ();
2305 result->expr_type = EXPR_CONSTANT;
2306 result->ts.type = BT_INTEGER;
2307 result->ts.kind = kind;
2308 result->where = src->where;
2313 gfc_warning ("The Hollerith constant at %L is too long to convert to %s",
2314 &src->where, gfc_typename(&result->ts));
2316 result->value.character.string = gfc_getmem (kind + 1);
2317 memcpy (result->value.character.string, src->value.character.string,
2321 memset (&result->value.character.string[len], ' ', kind - len);
2323 result->value.character.string[kind] = '\0'; /* For debugger */
2324 result->value.character.length = kind;
2329 /* Convert Hollerith to real. The constant will be padded or truncated. */
2332 gfc_hollerith2real (gfc_expr * src, int kind)
2337 len = src->value.character.length;
2339 result = gfc_get_expr ();
2340 result->expr_type = EXPR_CONSTANT;
2341 result->ts.type = BT_REAL;
2342 result->ts.kind = kind;
2343 result->where = src->where;
2348 gfc_warning ("The Hollerith constant at %L is too long to convert to %s",
2349 &src->where, gfc_typename(&result->ts));
2351 result->value.character.string = gfc_getmem (kind + 1);
2352 memcpy (result->value.character.string, src->value.character.string,
2356 memset (&result->value.character.string[len], ' ', kind - len);
2358 result->value.character.string[kind] = '\0'; /* For debugger */
2359 result->value.character.length = kind;
2364 /* Convert Hollerith to complex. The constant will be padded or truncated. */
2367 gfc_hollerith2complex (gfc_expr * src, int kind)
2372 len = src->value.character.length;
2374 result = gfc_get_expr ();
2375 result->expr_type = EXPR_CONSTANT;
2376 result->ts.type = BT_COMPLEX;
2377 result->ts.kind = kind;
2378 result->where = src->where;
2385 gfc_warning ("The Hollerith constant at %L is too long to convert to %s",
2386 &src->where, gfc_typename(&result->ts));
2388 result->value.character.string = gfc_getmem (kind + 1);
2389 memcpy (result->value.character.string, src->value.character.string,
2393 memset (&result->value.character.string[len], ' ', kind - len);
2395 result->value.character.string[kind] = '\0'; /* For debugger */
2396 result->value.character.length = kind;
2401 /* Convert Hollerith to character. */
2404 gfc_hollerith2character (gfc_expr * src, int kind)
2408 result = gfc_copy_expr (src);
2409 result->ts.type = BT_CHARACTER;
2410 result->ts.kind = kind;
2416 /* Convert Hollerith to logical. The constant will be padded or truncated. */
2419 gfc_hollerith2logical (gfc_expr * src, int kind)
2424 len = src->value.character.length;
2426 result = gfc_get_expr ();
2427 result->expr_type = EXPR_CONSTANT;
2428 result->ts.type = BT_LOGICAL;
2429 result->ts.kind = kind;
2430 result->where = src->where;
2435 gfc_warning ("The Hollerith constant at %L is too long to convert to %s",
2436 &src->where, gfc_typename(&result->ts));
2438 result->value.character.string = gfc_getmem (kind + 1);
2439 memcpy (result->value.character.string, src->value.character.string,
2443 memset (&result->value.character.string[len], ' ', kind - len);
2445 result->value.character.string[kind] = '\0'; /* For debugger */
2446 result->value.character.length = kind;
2451 /* Returns an initializer whose value is one higher than the value of the
2452 LAST_INITIALIZER argument. If that is argument is NULL, the
2453 initializers value will be set to zero. The initializer's kind
2454 will be set to gfc_c_int_kind.
2456 If -fshort-enums is given, the appropriate kind will be selected
2457 later after all enumerators have been parsed. A warning is issued
2458 here if an initializer exceeds gfc_c_int_kind. */
2461 gfc_enum_initializer (gfc_expr *last_initializer, locus where)
2465 result = gfc_get_expr ();
2466 result->expr_type = EXPR_CONSTANT;
2467 result->ts.type = BT_INTEGER;
2468 result->ts.kind = gfc_c_int_kind;
2469 result->where = where;
2471 mpz_init (result->value.integer);
2473 if (last_initializer != NULL)
2475 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
2476 result->where = last_initializer->where;
2478 if (gfc_check_integer_range (result->value.integer,
2479 gfc_c_int_kind) != ARITH_OK)
2481 gfc_error ("Enumerator exceeds the C integer type at %C");
2487 /* Control comes here, if it's the very first enumerator and no
2488 initializer has been given. It will be initialized to ZERO (0). */
2489 mpz_set_si (result->value.integer, 0);