2015-09-25 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / fortran / arith.c
blobe4da3b927fc8727c03e4834ae7fbd068217b1318
1 /* Compiler arithmetic
2 Copyright (C) 2000-2015 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* Since target arithmetic must be done on the host, there has to
22 be some way of evaluating arithmetic expressions as the host
23 would evaluate them. We use the GNU MP library and the MPFR
24 library to do arithmetic, and this file provides the interface. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "options.h"
30 #include "gfortran.h"
31 #include "arith.h"
32 #include "target-memory.h"
33 #include "constructor.h"
35 /* MPFR does not have a direct replacement for mpz_set_f() from GMP.
36 It's easily implemented with a few calls though. */
38 void
39 gfc_mpfr_to_mpz (mpz_t z, mpfr_t x, locus *where)
41 mp_exp_t e;
43 if (mpfr_inf_p (x) || mpfr_nan_p (x))
45 gfc_error ("Conversion of an Infinity or Not-a-Number at %L "
46 "to INTEGER", where);
47 mpz_set_ui (z, 0);
48 return;
51 e = mpfr_get_z_exp (z, x);
53 if (e > 0)
54 mpz_mul_2exp (z, z, e);
55 else
56 mpz_tdiv_q_2exp (z, z, -e);
60 /* Set the model number precision by the requested KIND. */
62 void
63 gfc_set_model_kind (int kind)
65 int index = gfc_validate_kind (BT_REAL, kind, false);
66 int base2prec;
68 base2prec = gfc_real_kinds[index].digits;
69 if (gfc_real_kinds[index].radix != 2)
70 base2prec *= gfc_real_kinds[index].radix / 2;
71 mpfr_set_default_prec (base2prec);
75 /* Set the model number precision from mpfr_t x. */
77 void
78 gfc_set_model (mpfr_t x)
80 mpfr_set_default_prec (mpfr_get_prec (x));
84 /* Given an arithmetic error code, return a pointer to a string that
85 explains the error. */
87 static const char *
88 gfc_arith_error (arith code)
90 const char *p;
92 switch (code)
94 case ARITH_OK:
95 p = _("Arithmetic OK at %L");
96 break;
97 case ARITH_OVERFLOW:
98 p = _("Arithmetic overflow at %L");
99 break;
100 case ARITH_UNDERFLOW:
101 p = _("Arithmetic underflow at %L");
102 break;
103 case ARITH_NAN:
104 p = _("Arithmetic NaN at %L");
105 break;
106 case ARITH_DIV0:
107 p = _("Division by zero at %L");
108 break;
109 case ARITH_INCOMMENSURATE:
110 p = _("Array operands are incommensurate at %L");
111 break;
112 case ARITH_ASYMMETRIC:
114 _("Integer outside symmetric range implied by Standard Fortran at %L");
115 break;
116 default:
117 gfc_internal_error ("gfc_arith_error(): Bad error code");
120 return p;
124 /* Get things ready to do math. */
126 void
127 gfc_arith_init_1 (void)
129 gfc_integer_info *int_info;
130 gfc_real_info *real_info;
131 mpfr_t a, b;
132 int i;
134 mpfr_set_default_prec (128);
135 mpfr_init (a);
137 /* Convert the minimum and maximum values for each kind into their
138 GNU MP representation. */
139 for (int_info = gfc_integer_kinds; int_info->kind != 0; int_info++)
141 /* Huge */
142 mpz_init (int_info->huge);
143 mpz_set_ui (int_info->huge, int_info->radix);
144 mpz_pow_ui (int_info->huge, int_info->huge, int_info->digits);
145 mpz_sub_ui (int_info->huge, int_info->huge, 1);
147 /* These are the numbers that are actually representable by the
148 target. For bases other than two, this needs to be changed. */
149 if (int_info->radix != 2)
150 gfc_internal_error ("Fix min_int calculation");
152 /* See PRs 13490 and 17912, related to integer ranges.
153 The pedantic_min_int exists for range checking when a program
154 is compiled with -pedantic, and reflects the belief that
155 Standard Fortran requires integers to be symmetrical, i.e.
156 every negative integer must have a representable positive
157 absolute value, and vice versa. */
159 mpz_init (int_info->pedantic_min_int);
160 mpz_neg (int_info->pedantic_min_int, int_info->huge);
162 mpz_init (int_info->min_int);
163 mpz_sub_ui (int_info->min_int, int_info->pedantic_min_int, 1);
165 /* Range */
166 mpfr_set_z (a, int_info->huge, GFC_RND_MODE);
167 mpfr_log10 (a, a, GFC_RND_MODE);
168 mpfr_trunc (a, a);
169 int_info->range = (int) mpfr_get_si (a, GFC_RND_MODE);
172 mpfr_clear (a);
174 for (real_info = gfc_real_kinds; real_info->kind != 0; real_info++)
176 gfc_set_model_kind (real_info->kind);
178 mpfr_init (a);
179 mpfr_init (b);
181 /* huge(x) = (1 - b**(-p)) * b**(emax-1) * b */
182 /* 1 - b**(-p) */
183 mpfr_init (real_info->huge);
184 mpfr_set_ui (real_info->huge, 1, GFC_RND_MODE);
185 mpfr_set_ui (a, real_info->radix, GFC_RND_MODE);
186 mpfr_pow_si (a, a, -real_info->digits, GFC_RND_MODE);
187 mpfr_sub (real_info->huge, real_info->huge, a, GFC_RND_MODE);
189 /* b**(emax-1) */
190 mpfr_set_ui (a, real_info->radix, GFC_RND_MODE);
191 mpfr_pow_ui (a, a, real_info->max_exponent - 1, GFC_RND_MODE);
193 /* (1 - b**(-p)) * b**(emax-1) */
194 mpfr_mul (real_info->huge, real_info->huge, a, GFC_RND_MODE);
196 /* (1 - b**(-p)) * b**(emax-1) * b */
197 mpfr_mul_ui (real_info->huge, real_info->huge, real_info->radix,
198 GFC_RND_MODE);
200 /* tiny(x) = b**(emin-1) */
201 mpfr_init (real_info->tiny);
202 mpfr_set_ui (real_info->tiny, real_info->radix, GFC_RND_MODE);
203 mpfr_pow_si (real_info->tiny, real_info->tiny,
204 real_info->min_exponent - 1, GFC_RND_MODE);
206 /* subnormal (x) = b**(emin - digit) */
207 mpfr_init (real_info->subnormal);
208 mpfr_set_ui (real_info->subnormal, real_info->radix, GFC_RND_MODE);
209 mpfr_pow_si (real_info->subnormal, real_info->subnormal,
210 real_info->min_exponent - real_info->digits, GFC_RND_MODE);
212 /* epsilon(x) = b**(1-p) */
213 mpfr_init (real_info->epsilon);
214 mpfr_set_ui (real_info->epsilon, real_info->radix, GFC_RND_MODE);
215 mpfr_pow_si (real_info->epsilon, real_info->epsilon,
216 1 - real_info->digits, GFC_RND_MODE);
218 /* range(x) = int(min(log10(huge(x)), -log10(tiny)) */
219 mpfr_log10 (a, real_info->huge, GFC_RND_MODE);
220 mpfr_log10 (b, real_info->tiny, GFC_RND_MODE);
221 mpfr_neg (b, b, GFC_RND_MODE);
223 /* a = min(a, b) */
224 mpfr_min (a, a, b, GFC_RND_MODE);
225 mpfr_trunc (a, a);
226 real_info->range = (int) mpfr_get_si (a, GFC_RND_MODE);
228 /* precision(x) = int((p - 1) * log10(b)) + k */
229 mpfr_set_ui (a, real_info->radix, GFC_RND_MODE);
230 mpfr_log10 (a, a, GFC_RND_MODE);
231 mpfr_mul_ui (a, a, real_info->digits - 1, GFC_RND_MODE);
232 mpfr_trunc (a, a);
233 real_info->precision = (int) mpfr_get_si (a, GFC_RND_MODE);
235 /* If the radix is an integral power of 10, add one to the precision. */
236 for (i = 10; i <= real_info->radix; i *= 10)
237 if (i == real_info->radix)
238 real_info->precision++;
240 mpfr_clears (a, b, NULL);
245 /* Clean up, get rid of numeric constants. */
247 void
248 gfc_arith_done_1 (void)
250 gfc_integer_info *ip;
251 gfc_real_info *rp;
253 for (ip = gfc_integer_kinds; ip->kind; ip++)
255 mpz_clear (ip->min_int);
256 mpz_clear (ip->pedantic_min_int);
257 mpz_clear (ip->huge);
260 for (rp = gfc_real_kinds; rp->kind; rp++)
261 mpfr_clears (rp->epsilon, rp->huge, rp->tiny, rp->subnormal, NULL);
263 mpfr_free_cache ();
267 /* Given a wide character value and a character kind, determine whether
268 the character is representable for that kind. */
269 bool
270 gfc_check_character_range (gfc_char_t c, int kind)
272 /* As wide characters are stored as 32-bit values, they're all
273 representable in UCS=4. */
274 if (kind == 4)
275 return true;
277 if (kind == 1)
278 return c <= 255 ? true : false;
280 gcc_unreachable ();
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
286 ARITH_OVERFLOW. */
288 arith
289 gfc_check_integer_range (mpz_t p, int kind)
291 arith result;
292 int i;
294 i = gfc_validate_kind (BT_INTEGER, kind, false);
295 result = ARITH_OK;
297 if (pedantic)
299 if (mpz_cmp (p, gfc_integer_kinds[i].pedantic_min_int) < 0)
300 result = ARITH_ASYMMETRIC;
304 if (flag_range_check == 0)
305 return result;
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;
311 return result;
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
317 ARITH_UNDERFLOW. */
319 static arith
320 gfc_check_real_range (mpfr_t p, int kind)
322 arith retval;
323 mpfr_t q;
324 int i;
326 i = gfc_validate_kind (BT_REAL, kind, false);
328 gfc_set_model (p);
329 mpfr_init (q);
330 mpfr_abs (q, p, GFC_RND_MODE);
332 retval = ARITH_OK;
334 if (mpfr_inf_p (p))
336 if (flag_range_check != 0)
337 retval = ARITH_OVERFLOW;
339 else if (mpfr_nan_p (p))
341 if (flag_range_check != 0)
342 retval = ARITH_NAN;
344 else if (mpfr_sgn (q) == 0)
346 mpfr_clear (q);
347 return retval;
349 else if (mpfr_cmp (q, gfc_real_kinds[i].huge) > 0)
351 if (flag_range_check == 0)
352 mpfr_set_inf (p, mpfr_sgn (p));
353 else
354 retval = ARITH_OVERFLOW;
356 else if (mpfr_cmp (q, gfc_real_kinds[i].subnormal) < 0)
358 if (flag_range_check == 0)
360 if (mpfr_sgn (p) < 0)
362 mpfr_set_ui (p, 0, GFC_RND_MODE);
363 mpfr_set_si (q, -1, GFC_RND_MODE);
364 mpfr_copysign (p, p, q, GFC_RND_MODE);
366 else
367 mpfr_set_ui (p, 0, GFC_RND_MODE);
369 else
370 retval = ARITH_UNDERFLOW;
372 else if (mpfr_cmp (q, gfc_real_kinds[i].tiny) < 0)
374 mp_exp_t emin, emax;
375 int en;
377 /* Save current values of emin and emax. */
378 emin = mpfr_get_emin ();
379 emax = mpfr_get_emax ();
381 /* Set emin and emax for the current model number. */
382 en = gfc_real_kinds[i].min_exponent - gfc_real_kinds[i].digits + 1;
383 mpfr_set_emin ((mp_exp_t) en);
384 mpfr_set_emax ((mp_exp_t) gfc_real_kinds[i].max_exponent);
385 mpfr_check_range (q, 0, GFC_RND_MODE);
386 mpfr_subnormalize (q, 0, GFC_RND_MODE);
388 /* Reset emin and emax. */
389 mpfr_set_emin (emin);
390 mpfr_set_emax (emax);
392 /* Copy sign if needed. */
393 if (mpfr_sgn (p) < 0)
394 mpfr_neg (p, q, GMP_RNDN);
395 else
396 mpfr_set (p, q, GMP_RNDN);
399 mpfr_clear (q);
401 return retval;
405 /* Low-level arithmetic functions. All of these subroutines assume
406 that all operands are of the same type and return an operand of the
407 same type. The other thing about these subroutines is that they
408 can fail in various ways -- overflow, underflow, division by zero,
409 zero raised to the zero, etc. */
411 static arith
412 gfc_arith_not (gfc_expr *op1, gfc_expr **resultp)
414 gfc_expr *result;
416 result = gfc_get_constant_expr (BT_LOGICAL, op1->ts.kind, &op1->where);
417 result->value.logical = !op1->value.logical;
418 *resultp = result;
420 return ARITH_OK;
424 static arith
425 gfc_arith_and (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
427 gfc_expr *result;
429 result = gfc_get_constant_expr (BT_LOGICAL, gfc_kind_max (op1, op2),
430 &op1->where);
431 result->value.logical = op1->value.logical && op2->value.logical;
432 *resultp = result;
434 return ARITH_OK;
438 static arith
439 gfc_arith_or (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
441 gfc_expr *result;
443 result = gfc_get_constant_expr (BT_LOGICAL, gfc_kind_max (op1, op2),
444 &op1->where);
445 result->value.logical = op1->value.logical || op2->value.logical;
446 *resultp = result;
448 return ARITH_OK;
452 static arith
453 gfc_arith_eqv (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
455 gfc_expr *result;
457 result = gfc_get_constant_expr (BT_LOGICAL, gfc_kind_max (op1, op2),
458 &op1->where);
459 result->value.logical = op1->value.logical == op2->value.logical;
460 *resultp = result;
462 return ARITH_OK;
466 static arith
467 gfc_arith_neqv (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
469 gfc_expr *result;
471 result = gfc_get_constant_expr (BT_LOGICAL, gfc_kind_max (op1, op2),
472 &op1->where);
473 result->value.logical = op1->value.logical != op2->value.logical;
474 *resultp = result;
476 return ARITH_OK;
480 /* Make sure a constant numeric expression is within the range for
481 its type and kind. Note that there's also a gfc_check_range(),
482 but that one deals with the intrinsic RANGE function. */
484 arith
485 gfc_range_check (gfc_expr *e)
487 arith rc;
488 arith rc2;
490 switch (e->ts.type)
492 case BT_INTEGER:
493 rc = gfc_check_integer_range (e->value.integer, e->ts.kind);
494 break;
496 case BT_REAL:
497 rc = gfc_check_real_range (e->value.real, e->ts.kind);
498 if (rc == ARITH_UNDERFLOW)
499 mpfr_set_ui (e->value.real, 0, GFC_RND_MODE);
500 if (rc == ARITH_OVERFLOW)
501 mpfr_set_inf (e->value.real, mpfr_sgn (e->value.real));
502 if (rc == ARITH_NAN)
503 mpfr_set_nan (e->value.real);
504 break;
506 case BT_COMPLEX:
507 rc = gfc_check_real_range (mpc_realref (e->value.complex), e->ts.kind);
508 if (rc == ARITH_UNDERFLOW)
509 mpfr_set_ui (mpc_realref (e->value.complex), 0, GFC_RND_MODE);
510 if (rc == ARITH_OVERFLOW)
511 mpfr_set_inf (mpc_realref (e->value.complex),
512 mpfr_sgn (mpc_realref (e->value.complex)));
513 if (rc == ARITH_NAN)
514 mpfr_set_nan (mpc_realref (e->value.complex));
516 rc2 = gfc_check_real_range (mpc_imagref (e->value.complex), e->ts.kind);
517 if (rc == ARITH_UNDERFLOW)
518 mpfr_set_ui (mpc_imagref (e->value.complex), 0, GFC_RND_MODE);
519 if (rc == ARITH_OVERFLOW)
520 mpfr_set_inf (mpc_imagref (e->value.complex),
521 mpfr_sgn (mpc_imagref (e->value.complex)));
522 if (rc == ARITH_NAN)
523 mpfr_set_nan (mpc_imagref (e->value.complex));
525 if (rc == ARITH_OK)
526 rc = rc2;
527 break;
529 default:
530 gfc_internal_error ("gfc_range_check(): Bad type");
533 return rc;
537 /* Several of the following routines use the same set of statements to
538 check the validity of the result. Encapsulate the checking here. */
540 static arith
541 check_result (arith rc, gfc_expr *x, gfc_expr *r, gfc_expr **rp)
543 arith val = rc;
545 if (val == ARITH_UNDERFLOW)
547 if (warn_underflow)
548 gfc_warning (OPT_Wunderflow, gfc_arith_error (val), &x->where);
549 val = ARITH_OK;
552 if (val == ARITH_ASYMMETRIC)
554 gfc_warning (0, gfc_arith_error (val), &x->where);
555 val = ARITH_OK;
558 if (val != ARITH_OK)
559 gfc_free_expr (r);
560 else
561 *rp = r;
563 return val;
567 /* It may seem silly to have a subroutine that actually computes the
568 unary plus of a constant, but it prevents us from making exceptions
569 in the code elsewhere. Used for unary plus and parenthesized
570 expressions. */
572 static arith
573 gfc_arith_identity (gfc_expr *op1, gfc_expr **resultp)
575 *resultp = gfc_copy_expr (op1);
576 return ARITH_OK;
580 static arith
581 gfc_arith_uminus (gfc_expr *op1, gfc_expr **resultp)
583 gfc_expr *result;
584 arith rc;
586 result = gfc_get_constant_expr (op1->ts.type, op1->ts.kind, &op1->where);
588 switch (op1->ts.type)
590 case BT_INTEGER:
591 mpz_neg (result->value.integer, op1->value.integer);
592 break;
594 case BT_REAL:
595 mpfr_neg (result->value.real, op1->value.real, GFC_RND_MODE);
596 break;
598 case BT_COMPLEX:
599 mpc_neg (result->value.complex, op1->value.complex, GFC_MPC_RND_MODE);
600 break;
602 default:
603 gfc_internal_error ("gfc_arith_uminus(): Bad basic type");
606 rc = gfc_range_check (result);
608 return check_result (rc, op1, result, resultp);
612 static arith
613 gfc_arith_plus (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
615 gfc_expr *result;
616 arith rc;
618 result = gfc_get_constant_expr (op1->ts.type, op1->ts.kind, &op1->where);
620 switch (op1->ts.type)
622 case BT_INTEGER:
623 mpz_add (result->value.integer, op1->value.integer, op2->value.integer);
624 break;
626 case BT_REAL:
627 mpfr_add (result->value.real, op1->value.real, op2->value.real,
628 GFC_RND_MODE);
629 break;
631 case BT_COMPLEX:
632 mpc_add (result->value.complex, op1->value.complex, op2->value.complex,
633 GFC_MPC_RND_MODE);
634 break;
636 default:
637 gfc_internal_error ("gfc_arith_plus(): Bad basic type");
640 rc = gfc_range_check (result);
642 return check_result (rc, op1, result, resultp);
646 static arith
647 gfc_arith_minus (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
649 gfc_expr *result;
650 arith rc;
652 result = gfc_get_constant_expr (op1->ts.type, op1->ts.kind, &op1->where);
654 switch (op1->ts.type)
656 case BT_INTEGER:
657 mpz_sub (result->value.integer, op1->value.integer, op2->value.integer);
658 break;
660 case BT_REAL:
661 mpfr_sub (result->value.real, op1->value.real, op2->value.real,
662 GFC_RND_MODE);
663 break;
665 case BT_COMPLEX:
666 mpc_sub (result->value.complex, op1->value.complex,
667 op2->value.complex, GFC_MPC_RND_MODE);
668 break;
670 default:
671 gfc_internal_error ("gfc_arith_minus(): Bad basic type");
674 rc = gfc_range_check (result);
676 return check_result (rc, op1, result, resultp);
680 static arith
681 gfc_arith_times (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
683 gfc_expr *result;
684 arith rc;
686 result = gfc_get_constant_expr (op1->ts.type, op1->ts.kind, &op1->where);
688 switch (op1->ts.type)
690 case BT_INTEGER:
691 mpz_mul (result->value.integer, op1->value.integer, op2->value.integer);
692 break;
694 case BT_REAL:
695 mpfr_mul (result->value.real, op1->value.real, op2->value.real,
696 GFC_RND_MODE);
697 break;
699 case BT_COMPLEX:
700 gfc_set_model (mpc_realref (op1->value.complex));
701 mpc_mul (result->value.complex, op1->value.complex, op2->value.complex,
702 GFC_MPC_RND_MODE);
703 break;
705 default:
706 gfc_internal_error ("gfc_arith_times(): Bad basic type");
709 rc = gfc_range_check (result);
711 return check_result (rc, op1, result, resultp);
715 static arith
716 gfc_arith_divide (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
718 gfc_expr *result;
719 arith rc;
721 rc = ARITH_OK;
723 result = gfc_get_constant_expr (op1->ts.type, op1->ts.kind, &op1->where);
725 switch (op1->ts.type)
727 case BT_INTEGER:
728 if (mpz_sgn (op2->value.integer) == 0)
730 rc = ARITH_DIV0;
731 break;
734 if (warn_integer_division)
736 mpz_t r;
737 mpz_init (r);
738 mpz_tdiv_qr (result->value.integer, r, op1->value.integer,
739 op2->value.integer);
741 if (mpz_cmp_si (r, 0) != 0)
743 char *p;
744 p = mpz_get_str (NULL, 10, result->value.integer);
745 gfc_warning_now (OPT_Winteger_division, "Integer division "
746 "truncated to constant %qs at %L", p,
747 &op1->where);
748 free (p);
750 mpz_clear (r);
752 else
753 mpz_tdiv_q (result->value.integer, op1->value.integer,
754 op2->value.integer);
756 break;
758 case BT_REAL:
759 if (mpfr_sgn (op2->value.real) == 0 && flag_range_check == 1)
761 rc = ARITH_DIV0;
762 break;
765 mpfr_div (result->value.real, op1->value.real, op2->value.real,
766 GFC_RND_MODE);
767 break;
769 case BT_COMPLEX:
770 if (mpc_cmp_si_si (op2->value.complex, 0, 0) == 0
771 && flag_range_check == 1)
773 rc = ARITH_DIV0;
774 break;
777 gfc_set_model (mpc_realref (op1->value.complex));
778 if (mpc_cmp_si_si (op2->value.complex, 0, 0) == 0)
780 /* In Fortran, return (NaN + NaN I) for any zero divisor. See
781 PR 40318. */
782 mpfr_set_nan (mpc_realref (result->value.complex));
783 mpfr_set_nan (mpc_imagref (result->value.complex));
785 else
786 mpc_div (result->value.complex, op1->value.complex, op2->value.complex,
787 GFC_MPC_RND_MODE);
788 break;
790 default:
791 gfc_internal_error ("gfc_arith_divide(): Bad basic type");
794 if (rc == ARITH_OK)
795 rc = gfc_range_check (result);
797 return check_result (rc, op1, result, resultp);
800 /* Raise a number to a power. */
802 static arith
803 arith_power (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
805 int power_sign;
806 gfc_expr *result;
807 arith rc;
809 rc = ARITH_OK;
810 result = gfc_get_constant_expr (op1->ts.type, op1->ts.kind, &op1->where);
812 switch (op2->ts.type)
814 case BT_INTEGER:
815 power_sign = mpz_sgn (op2->value.integer);
817 if (power_sign == 0)
819 /* Handle something to the zeroth power. Since we're dealing
820 with integral exponents, there is no ambiguity in the
821 limiting procedure used to determine the value of 0**0. */
822 switch (op1->ts.type)
824 case BT_INTEGER:
825 mpz_set_ui (result->value.integer, 1);
826 break;
828 case BT_REAL:
829 mpfr_set_ui (result->value.real, 1, GFC_RND_MODE);
830 break;
832 case BT_COMPLEX:
833 mpc_set_ui (result->value.complex, 1, GFC_MPC_RND_MODE);
834 break;
836 default:
837 gfc_internal_error ("arith_power(): Bad base");
840 else
842 switch (op1->ts.type)
844 case BT_INTEGER:
846 int power;
848 /* First, we simplify the cases of op1 == 1, 0 or -1. */
849 if (mpz_cmp_si (op1->value.integer, 1) == 0)
851 /* 1**op2 == 1 */
852 mpz_set_si (result->value.integer, 1);
854 else if (mpz_cmp_si (op1->value.integer, 0) == 0)
856 /* 0**op2 == 0, if op2 > 0
857 0**op2 overflow, if op2 < 0 ; in that case, we
858 set the result to 0 and return ARITH_DIV0. */
859 mpz_set_si (result->value.integer, 0);
860 if (mpz_cmp_si (op2->value.integer, 0) < 0)
861 rc = ARITH_DIV0;
863 else if (mpz_cmp_si (op1->value.integer, -1) == 0)
865 /* (-1)**op2 == (-1)**(mod(op2,2)) */
866 unsigned int odd = mpz_fdiv_ui (op2->value.integer, 2);
867 if (odd)
868 mpz_set_si (result->value.integer, -1);
869 else
870 mpz_set_si (result->value.integer, 1);
872 /* Then, we take care of op2 < 0. */
873 else if (mpz_cmp_si (op2->value.integer, 0) < 0)
875 /* if op2 < 0, op1**op2 == 0 because abs(op1) > 1. */
876 mpz_set_si (result->value.integer, 0);
878 else if (gfc_extract_int (op2, &power) != NULL)
880 /* If op2 doesn't fit in an int, the exponentiation will
881 overflow, because op2 > 0 and abs(op1) > 1. */
882 mpz_t max;
883 int i;
884 i = gfc_validate_kind (BT_INTEGER, result->ts.kind, false);
886 if (flag_range_check)
887 rc = ARITH_OVERFLOW;
889 /* Still, we want to give the same value as the
890 processor. */
891 mpz_init (max);
892 mpz_add_ui (max, gfc_integer_kinds[i].huge, 1);
893 mpz_mul_ui (max, max, 2);
894 mpz_powm (result->value.integer, op1->value.integer,
895 op2->value.integer, max);
896 mpz_clear (max);
898 else
899 mpz_pow_ui (result->value.integer, op1->value.integer,
900 power);
902 break;
904 case BT_REAL:
905 mpfr_pow_z (result->value.real, op1->value.real,
906 op2->value.integer, GFC_RND_MODE);
907 break;
909 case BT_COMPLEX:
910 mpc_pow_z (result->value.complex, op1->value.complex,
911 op2->value.integer, GFC_MPC_RND_MODE);
912 break;
914 default:
915 break;
918 break;
920 case BT_REAL:
922 if (gfc_init_expr_flag)
924 if (!gfc_notify_std (GFC_STD_F2003, "Noninteger "
925 "exponent in an initialization "
926 "expression at %L", &op2->where))
928 gfc_free_expr (result);
929 return ARITH_PROHIBIT;
933 if (mpfr_cmp_si (op1->value.real, 0) < 0)
935 gfc_error ("Raising a negative REAL at %L to "
936 "a REAL power is prohibited", &op1->where);
937 gfc_free_expr (result);
938 return ARITH_PROHIBIT;
941 mpfr_pow (result->value.real, op1->value.real, op2->value.real,
942 GFC_RND_MODE);
943 break;
945 case BT_COMPLEX:
947 if (gfc_init_expr_flag)
949 if (!gfc_notify_std (GFC_STD_F2003, "Noninteger "
950 "exponent in an initialization "
951 "expression at %L", &op2->where))
953 gfc_free_expr (result);
954 return ARITH_PROHIBIT;
958 mpc_pow (result->value.complex, op1->value.complex,
959 op2->value.complex, GFC_MPC_RND_MODE);
961 break;
962 default:
963 gfc_internal_error ("arith_power(): unknown type");
966 if (rc == ARITH_OK)
967 rc = gfc_range_check (result);
969 return check_result (rc, op1, result, resultp);
973 /* Concatenate two string constants. */
975 static arith
976 gfc_arith_concat (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
978 gfc_expr *result;
979 int len;
981 gcc_assert (op1->ts.kind == op2->ts.kind);
982 result = gfc_get_constant_expr (BT_CHARACTER, op1->ts.kind,
983 &op1->where);
985 len = op1->value.character.length + op2->value.character.length;
987 result->value.character.string = gfc_get_wide_string (len + 1);
988 result->value.character.length = len;
990 memcpy (result->value.character.string, op1->value.character.string,
991 op1->value.character.length * sizeof (gfc_char_t));
993 memcpy (&result->value.character.string[op1->value.character.length],
994 op2->value.character.string,
995 op2->value.character.length * sizeof (gfc_char_t));
997 result->value.character.string[len] = '\0';
999 *resultp = result;
1001 return ARITH_OK;
1004 /* Comparison between real values; returns 0 if (op1 .op. op2) is true.
1005 This function mimics mpfr_cmp but takes NaN into account. */
1007 static int
1008 compare_real (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1010 int rc;
1011 switch (op)
1013 case INTRINSIC_EQ:
1014 rc = mpfr_equal_p (op1->value.real, op2->value.real) ? 0 : 1;
1015 break;
1016 case INTRINSIC_GT:
1017 rc = mpfr_greater_p (op1->value.real, op2->value.real) ? 1 : -1;
1018 break;
1019 case INTRINSIC_GE:
1020 rc = mpfr_greaterequal_p (op1->value.real, op2->value.real) ? 1 : -1;
1021 break;
1022 case INTRINSIC_LT:
1023 rc = mpfr_less_p (op1->value.real, op2->value.real) ? -1 : 1;
1024 break;
1025 case INTRINSIC_LE:
1026 rc = mpfr_lessequal_p (op1->value.real, op2->value.real) ? -1 : 1;
1027 break;
1028 default:
1029 gfc_internal_error ("compare_real(): Bad operator");
1032 return rc;
1035 /* Comparison operators. Assumes that the two expression nodes
1036 contain two constants of the same type. The op argument is
1037 needed to handle NaN correctly. */
1040 gfc_compare_expr (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1042 int rc;
1044 switch (op1->ts.type)
1046 case BT_INTEGER:
1047 rc = mpz_cmp (op1->value.integer, op2->value.integer);
1048 break;
1050 case BT_REAL:
1051 rc = compare_real (op1, op2, op);
1052 break;
1054 case BT_CHARACTER:
1055 rc = gfc_compare_string (op1, op2);
1056 break;
1058 case BT_LOGICAL:
1059 rc = ((!op1->value.logical && op2->value.logical)
1060 || (op1->value.logical && !op2->value.logical));
1061 break;
1063 default:
1064 gfc_internal_error ("gfc_compare_expr(): Bad basic type");
1067 return rc;
1071 /* Compare a pair of complex numbers. Naturally, this is only for
1072 equality and inequality. */
1074 static int
1075 compare_complex (gfc_expr *op1, gfc_expr *op2)
1077 return mpc_cmp (op1->value.complex, op2->value.complex) == 0;
1081 /* Given two constant strings and the inverse collating sequence, compare the
1082 strings. We return -1 for a < b, 0 for a == b and 1 for a > b.
1083 We use the processor's default collating sequence. */
1086 gfc_compare_string (gfc_expr *a, gfc_expr *b)
1088 int len, alen, blen, i;
1089 gfc_char_t ac, bc;
1091 alen = a->value.character.length;
1092 blen = b->value.character.length;
1094 len = MAX(alen, blen);
1096 for (i = 0; i < len; i++)
1098 ac = ((i < alen) ? a->value.character.string[i] : ' ');
1099 bc = ((i < blen) ? b->value.character.string[i] : ' ');
1101 if (ac < bc)
1102 return -1;
1103 if (ac > bc)
1104 return 1;
1107 /* Strings are equal */
1108 return 0;
1113 gfc_compare_with_Cstring (gfc_expr *a, const char *b, bool case_sensitive)
1115 int len, alen, blen, i;
1116 gfc_char_t ac, bc;
1118 alen = a->value.character.length;
1119 blen = strlen (b);
1121 len = MAX(alen, blen);
1123 for (i = 0; i < len; i++)
1125 ac = ((i < alen) ? a->value.character.string[i] : ' ');
1126 bc = ((i < blen) ? b[i] : ' ');
1128 if (!case_sensitive)
1130 ac = TOLOWER (ac);
1131 bc = TOLOWER (bc);
1134 if (ac < bc)
1135 return -1;
1136 if (ac > bc)
1137 return 1;
1140 /* Strings are equal */
1141 return 0;
1145 /* Specific comparison subroutines. */
1147 static arith
1148 gfc_arith_eq (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1150 gfc_expr *result;
1152 result = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
1153 &op1->where);
1154 result->value.logical = (op1->ts.type == BT_COMPLEX)
1155 ? compare_complex (op1, op2)
1156 : (gfc_compare_expr (op1, op2, INTRINSIC_EQ) == 0);
1158 *resultp = result;
1159 return ARITH_OK;
1163 static arith
1164 gfc_arith_ne (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1166 gfc_expr *result;
1168 result = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
1169 &op1->where);
1170 result->value.logical = (op1->ts.type == BT_COMPLEX)
1171 ? !compare_complex (op1, op2)
1172 : (gfc_compare_expr (op1, op2, INTRINSIC_EQ) != 0);
1174 *resultp = result;
1175 return ARITH_OK;
1179 static arith
1180 gfc_arith_gt (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1182 gfc_expr *result;
1184 result = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
1185 &op1->where);
1186 result->value.logical = (gfc_compare_expr (op1, op2, INTRINSIC_GT) > 0);
1187 *resultp = result;
1189 return ARITH_OK;
1193 static arith
1194 gfc_arith_ge (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1196 gfc_expr *result;
1198 result = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
1199 &op1->where);
1200 result->value.logical = (gfc_compare_expr (op1, op2, INTRINSIC_GE) >= 0);
1201 *resultp = result;
1203 return ARITH_OK;
1207 static arith
1208 gfc_arith_lt (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1210 gfc_expr *result;
1212 result = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
1213 &op1->where);
1214 result->value.logical = (gfc_compare_expr (op1, op2, INTRINSIC_LT) < 0);
1215 *resultp = result;
1217 return ARITH_OK;
1221 static arith
1222 gfc_arith_le (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1224 gfc_expr *result;
1226 result = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
1227 &op1->where);
1228 result->value.logical = (gfc_compare_expr (op1, op2, INTRINSIC_LE) <= 0);
1229 *resultp = result;
1231 return ARITH_OK;
1235 static arith
1236 reduce_unary (arith (*eval) (gfc_expr *, gfc_expr **), gfc_expr *op,
1237 gfc_expr **result)
1239 gfc_constructor_base head;
1240 gfc_constructor *c;
1241 gfc_expr *r;
1242 arith rc;
1244 if (op->expr_type == EXPR_CONSTANT)
1245 return eval (op, result);
1247 rc = ARITH_OK;
1248 head = gfc_constructor_copy (op->value.constructor);
1249 for (c = gfc_constructor_first (head); c; c = gfc_constructor_next (c))
1251 rc = reduce_unary (eval, c->expr, &r);
1253 if (rc != ARITH_OK)
1254 break;
1256 gfc_replace_expr (c->expr, r);
1259 if (rc != ARITH_OK)
1260 gfc_constructor_free (head);
1261 else
1263 gfc_constructor *c = gfc_constructor_first (head);
1264 r = gfc_get_array_expr (c->expr->ts.type, c->expr->ts.kind,
1265 &op->where);
1266 r->shape = gfc_copy_shape (op->shape, op->rank);
1267 r->rank = op->rank;
1268 r->value.constructor = head;
1269 *result = r;
1272 return rc;
1276 static arith
1277 reduce_binary_ac (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1278 gfc_expr *op1, gfc_expr *op2, gfc_expr **result)
1280 gfc_constructor_base head;
1281 gfc_constructor *c;
1282 gfc_expr *r;
1283 arith rc = ARITH_OK;
1285 head = gfc_constructor_copy (op1->value.constructor);
1286 for (c = gfc_constructor_first (head); c; c = gfc_constructor_next (c))
1288 if (c->expr->expr_type == EXPR_CONSTANT)
1289 rc = eval (c->expr, op2, &r);
1290 else
1291 rc = reduce_binary_ac (eval, c->expr, op2, &r);
1293 if (rc != ARITH_OK)
1294 break;
1296 gfc_replace_expr (c->expr, r);
1299 if (rc != ARITH_OK)
1300 gfc_constructor_free (head);
1301 else
1303 gfc_constructor *c = gfc_constructor_first (head);
1304 r = gfc_get_array_expr (c->expr->ts.type, c->expr->ts.kind,
1305 &op1->where);
1306 r->shape = gfc_copy_shape (op1->shape, op1->rank);
1307 r->rank = op1->rank;
1308 r->value.constructor = head;
1309 *result = r;
1312 return rc;
1316 static arith
1317 reduce_binary_ca (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1318 gfc_expr *op1, gfc_expr *op2, gfc_expr **result)
1320 gfc_constructor_base head;
1321 gfc_constructor *c;
1322 gfc_expr *r;
1323 arith rc = ARITH_OK;
1325 head = gfc_constructor_copy (op2->value.constructor);
1326 for (c = gfc_constructor_first (head); c; c = gfc_constructor_next (c))
1328 if (c->expr->expr_type == EXPR_CONSTANT)
1329 rc = eval (op1, c->expr, &r);
1330 else
1331 rc = reduce_binary_ca (eval, op1, c->expr, &r);
1333 if (rc != ARITH_OK)
1334 break;
1336 gfc_replace_expr (c->expr, r);
1339 if (rc != ARITH_OK)
1340 gfc_constructor_free (head);
1341 else
1343 gfc_constructor *c = gfc_constructor_first (head);
1344 r = gfc_get_array_expr (c->expr->ts.type, c->expr->ts.kind,
1345 &op2->where);
1346 r->shape = gfc_copy_shape (op2->shape, op2->rank);
1347 r->rank = op2->rank;
1348 r->value.constructor = head;
1349 *result = r;
1352 return rc;
1356 /* We need a forward declaration of reduce_binary. */
1357 static arith reduce_binary (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1358 gfc_expr *op1, gfc_expr *op2, gfc_expr **result);
1361 static arith
1362 reduce_binary_aa (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1363 gfc_expr *op1, gfc_expr *op2, gfc_expr **result)
1365 gfc_constructor_base head;
1366 gfc_constructor *c, *d;
1367 gfc_expr *r;
1368 arith rc = ARITH_OK;
1370 if (!gfc_check_conformance (op1, op2, "elemental binary operation"))
1371 return ARITH_INCOMMENSURATE;
1373 head = gfc_constructor_copy (op1->value.constructor);
1374 for (c = gfc_constructor_first (head),
1375 d = gfc_constructor_first (op2->value.constructor);
1376 c && d;
1377 c = gfc_constructor_next (c), d = gfc_constructor_next (d))
1379 rc = reduce_binary (eval, c->expr, d->expr, &r);
1380 if (rc != ARITH_OK)
1381 break;
1383 gfc_replace_expr (c->expr, r);
1386 if (c || d)
1387 rc = ARITH_INCOMMENSURATE;
1389 if (rc != ARITH_OK)
1390 gfc_constructor_free (head);
1391 else
1393 gfc_constructor *c = gfc_constructor_first (head);
1394 r = gfc_get_array_expr (c->expr->ts.type, c->expr->ts.kind,
1395 &op1->where);
1396 r->shape = gfc_copy_shape (op1->shape, op1->rank);
1397 r->rank = op1->rank;
1398 r->value.constructor = head;
1399 *result = r;
1402 return rc;
1406 static arith
1407 reduce_binary (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1408 gfc_expr *op1, gfc_expr *op2, gfc_expr **result)
1410 if (op1->expr_type == EXPR_CONSTANT && op2->expr_type == EXPR_CONSTANT)
1411 return eval (op1, op2, result);
1413 if (op1->expr_type == EXPR_CONSTANT && op2->expr_type == EXPR_ARRAY)
1414 return reduce_binary_ca (eval, op1, op2, result);
1416 if (op1->expr_type == EXPR_ARRAY && op2->expr_type == EXPR_CONSTANT)
1417 return reduce_binary_ac (eval, op1, op2, result);
1419 return reduce_binary_aa (eval, op1, op2, result);
1423 typedef union
1425 arith (*f2)(gfc_expr *, gfc_expr **);
1426 arith (*f3)(gfc_expr *, gfc_expr *, gfc_expr **);
1428 eval_f;
1430 /* High level arithmetic subroutines. These subroutines go into
1431 eval_intrinsic(), which can do one of several things to its
1432 operands. If the operands are incompatible with the intrinsic
1433 operation, we return a node pointing to the operands and hope that
1434 an operator interface is found during resolution.
1436 If the operands are compatible and are constants, then we try doing
1437 the arithmetic. We also handle the cases where either or both
1438 operands are array constructors. */
1440 static gfc_expr *
1441 eval_intrinsic (gfc_intrinsic_op op,
1442 eval_f eval, gfc_expr *op1, gfc_expr *op2)
1444 gfc_expr temp, *result;
1445 int unary;
1446 arith rc;
1448 gfc_clear_ts (&temp.ts);
1450 switch (op)
1452 /* Logical unary */
1453 case INTRINSIC_NOT:
1454 if (op1->ts.type != BT_LOGICAL)
1455 goto runtime;
1457 temp.ts.type = BT_LOGICAL;
1458 temp.ts.kind = gfc_default_logical_kind;
1459 unary = 1;
1460 break;
1462 /* Logical binary operators */
1463 case INTRINSIC_OR:
1464 case INTRINSIC_AND:
1465 case INTRINSIC_NEQV:
1466 case INTRINSIC_EQV:
1467 if (op1->ts.type != BT_LOGICAL || op2->ts.type != BT_LOGICAL)
1468 goto runtime;
1470 temp.ts.type = BT_LOGICAL;
1471 temp.ts.kind = gfc_default_logical_kind;
1472 unary = 0;
1473 break;
1475 /* Numeric unary */
1476 case INTRINSIC_UPLUS:
1477 case INTRINSIC_UMINUS:
1478 if (!gfc_numeric_ts (&op1->ts))
1479 goto runtime;
1481 temp.ts = op1->ts;
1482 unary = 1;
1483 break;
1485 case INTRINSIC_PARENTHESES:
1486 temp.ts = op1->ts;
1487 unary = 1;
1488 break;
1490 /* Additional restrictions for ordering relations. */
1491 case INTRINSIC_GE:
1492 case INTRINSIC_GE_OS:
1493 case INTRINSIC_LT:
1494 case INTRINSIC_LT_OS:
1495 case INTRINSIC_LE:
1496 case INTRINSIC_LE_OS:
1497 case INTRINSIC_GT:
1498 case INTRINSIC_GT_OS:
1499 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
1501 temp.ts.type = BT_LOGICAL;
1502 temp.ts.kind = gfc_default_logical_kind;
1503 goto runtime;
1506 /* Fall through */
1507 case INTRINSIC_EQ:
1508 case INTRINSIC_EQ_OS:
1509 case INTRINSIC_NE:
1510 case INTRINSIC_NE_OS:
1511 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
1513 unary = 0;
1514 temp.ts.type = BT_LOGICAL;
1515 temp.ts.kind = gfc_default_logical_kind;
1517 /* If kind mismatch, exit and we'll error out later. */
1518 if (op1->ts.kind != op2->ts.kind)
1519 goto runtime;
1521 break;
1524 /* Fall through */
1525 /* Numeric binary */
1526 case INTRINSIC_PLUS:
1527 case INTRINSIC_MINUS:
1528 case INTRINSIC_TIMES:
1529 case INTRINSIC_DIVIDE:
1530 case INTRINSIC_POWER:
1531 if (!gfc_numeric_ts (&op1->ts) || !gfc_numeric_ts (&op2->ts))
1532 goto runtime;
1534 /* Insert any necessary type conversions to make the operands
1535 compatible. */
1537 temp.expr_type = EXPR_OP;
1538 gfc_clear_ts (&temp.ts);
1539 temp.value.op.op = op;
1541 temp.value.op.op1 = op1;
1542 temp.value.op.op2 = op2;
1544 gfc_type_convert_binary (&temp, warn_conversion || warn_conversion_extra);
1546 if (op == INTRINSIC_EQ || op == INTRINSIC_NE
1547 || op == INTRINSIC_GE || op == INTRINSIC_GT
1548 || op == INTRINSIC_LE || op == INTRINSIC_LT
1549 || op == INTRINSIC_EQ_OS || op == INTRINSIC_NE_OS
1550 || op == INTRINSIC_GE_OS || op == INTRINSIC_GT_OS
1551 || op == INTRINSIC_LE_OS || op == INTRINSIC_LT_OS)
1553 temp.ts.type = BT_LOGICAL;
1554 temp.ts.kind = gfc_default_logical_kind;
1557 unary = 0;
1558 break;
1560 /* Character binary */
1561 case INTRINSIC_CONCAT:
1562 if (op1->ts.type != BT_CHARACTER || op2->ts.type != BT_CHARACTER
1563 || op1->ts.kind != op2->ts.kind)
1564 goto runtime;
1566 temp.ts.type = BT_CHARACTER;
1567 temp.ts.kind = op1->ts.kind;
1568 unary = 0;
1569 break;
1571 case INTRINSIC_USER:
1572 goto runtime;
1574 default:
1575 gfc_internal_error ("eval_intrinsic(): Bad operator");
1578 if (op1->expr_type != EXPR_CONSTANT
1579 && (op1->expr_type != EXPR_ARRAY
1580 || !gfc_is_constant_expr (op1) || !gfc_expanded_ac (op1)))
1581 goto runtime;
1583 if (op2 != NULL
1584 && op2->expr_type != EXPR_CONSTANT
1585 && (op2->expr_type != EXPR_ARRAY
1586 || !gfc_is_constant_expr (op2) || !gfc_expanded_ac (op2)))
1587 goto runtime;
1589 if (unary)
1590 rc = reduce_unary (eval.f2, op1, &result);
1591 else
1592 rc = reduce_binary (eval.f3, op1, op2, &result);
1595 /* Something went wrong. */
1596 if (op == INTRINSIC_POWER && rc == ARITH_PROHIBIT)
1597 return NULL;
1599 if (rc != ARITH_OK)
1601 gfc_error (gfc_arith_error (rc), &op1->where);
1602 return NULL;
1605 gfc_free_expr (op1);
1606 gfc_free_expr (op2);
1607 return result;
1609 runtime:
1610 /* Create a run-time expression. */
1611 result = gfc_get_operator_expr (&op1->where, op, op1, op2);
1612 result->ts = temp.ts;
1614 return result;
1618 /* Modify type of expression for zero size array. */
1620 static gfc_expr *
1621 eval_type_intrinsic0 (gfc_intrinsic_op iop, gfc_expr *op)
1623 if (op == NULL)
1624 gfc_internal_error ("eval_type_intrinsic0(): op NULL");
1626 switch (iop)
1628 case INTRINSIC_GE:
1629 case INTRINSIC_GE_OS:
1630 case INTRINSIC_LT:
1631 case INTRINSIC_LT_OS:
1632 case INTRINSIC_LE:
1633 case INTRINSIC_LE_OS:
1634 case INTRINSIC_GT:
1635 case INTRINSIC_GT_OS:
1636 case INTRINSIC_EQ:
1637 case INTRINSIC_EQ_OS:
1638 case INTRINSIC_NE:
1639 case INTRINSIC_NE_OS:
1640 op->ts.type = BT_LOGICAL;
1641 op->ts.kind = gfc_default_logical_kind;
1642 break;
1644 default:
1645 break;
1648 return op;
1652 /* Return nonzero if the expression is a zero size array. */
1654 static int
1655 gfc_zero_size_array (gfc_expr *e)
1657 if (e->expr_type != EXPR_ARRAY)
1658 return 0;
1660 return e->value.constructor == NULL;
1664 /* Reduce a binary expression where at least one of the operands
1665 involves a zero-length array. Returns NULL if neither of the
1666 operands is a zero-length array. */
1668 static gfc_expr *
1669 reduce_binary0 (gfc_expr *op1, gfc_expr *op2)
1671 if (gfc_zero_size_array (op1))
1673 gfc_free_expr (op2);
1674 return op1;
1677 if (gfc_zero_size_array (op2))
1679 gfc_free_expr (op1);
1680 return op2;
1683 return NULL;
1687 static gfc_expr *
1688 eval_intrinsic_f2 (gfc_intrinsic_op op,
1689 arith (*eval) (gfc_expr *, gfc_expr **),
1690 gfc_expr *op1, gfc_expr *op2)
1692 gfc_expr *result;
1693 eval_f f;
1695 if (op2 == NULL)
1697 if (gfc_zero_size_array (op1))
1698 return eval_type_intrinsic0 (op, op1);
1700 else
1702 result = reduce_binary0 (op1, op2);
1703 if (result != NULL)
1704 return eval_type_intrinsic0 (op, result);
1707 f.f2 = eval;
1708 return eval_intrinsic (op, f, op1, op2);
1712 static gfc_expr *
1713 eval_intrinsic_f3 (gfc_intrinsic_op op,
1714 arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1715 gfc_expr *op1, gfc_expr *op2)
1717 gfc_expr *result;
1718 eval_f f;
1720 result = reduce_binary0 (op1, op2);
1721 if (result != NULL)
1722 return eval_type_intrinsic0(op, result);
1724 f.f3 = eval;
1725 return eval_intrinsic (op, f, op1, op2);
1729 gfc_expr *
1730 gfc_parentheses (gfc_expr *op)
1732 if (gfc_is_constant_expr (op))
1733 return op;
1735 return eval_intrinsic_f2 (INTRINSIC_PARENTHESES, gfc_arith_identity,
1736 op, NULL);
1739 gfc_expr *
1740 gfc_uplus (gfc_expr *op)
1742 return eval_intrinsic_f2 (INTRINSIC_UPLUS, gfc_arith_identity, op, NULL);
1746 gfc_expr *
1747 gfc_uminus (gfc_expr *op)
1749 return eval_intrinsic_f2 (INTRINSIC_UMINUS, gfc_arith_uminus, op, NULL);
1753 gfc_expr *
1754 gfc_add (gfc_expr *op1, gfc_expr *op2)
1756 return eval_intrinsic_f3 (INTRINSIC_PLUS, gfc_arith_plus, op1, op2);
1760 gfc_expr *
1761 gfc_subtract (gfc_expr *op1, gfc_expr *op2)
1763 return eval_intrinsic_f3 (INTRINSIC_MINUS, gfc_arith_minus, op1, op2);
1767 gfc_expr *
1768 gfc_multiply (gfc_expr *op1, gfc_expr *op2)
1770 return eval_intrinsic_f3 (INTRINSIC_TIMES, gfc_arith_times, op1, op2);
1774 gfc_expr *
1775 gfc_divide (gfc_expr *op1, gfc_expr *op2)
1777 return eval_intrinsic_f3 (INTRINSIC_DIVIDE, gfc_arith_divide, op1, op2);
1781 gfc_expr *
1782 gfc_power (gfc_expr *op1, gfc_expr *op2)
1784 return eval_intrinsic_f3 (INTRINSIC_POWER, arith_power, op1, op2);
1788 gfc_expr *
1789 gfc_concat (gfc_expr *op1, gfc_expr *op2)
1791 return eval_intrinsic_f3 (INTRINSIC_CONCAT, gfc_arith_concat, op1, op2);
1795 gfc_expr *
1796 gfc_and (gfc_expr *op1, gfc_expr *op2)
1798 return eval_intrinsic_f3 (INTRINSIC_AND, gfc_arith_and, op1, op2);
1802 gfc_expr *
1803 gfc_or (gfc_expr *op1, gfc_expr *op2)
1805 return eval_intrinsic_f3 (INTRINSIC_OR, gfc_arith_or, op1, op2);
1809 gfc_expr *
1810 gfc_not (gfc_expr *op1)
1812 return eval_intrinsic_f2 (INTRINSIC_NOT, gfc_arith_not, op1, NULL);
1816 gfc_expr *
1817 gfc_eqv (gfc_expr *op1, gfc_expr *op2)
1819 return eval_intrinsic_f3 (INTRINSIC_EQV, gfc_arith_eqv, op1, op2);
1823 gfc_expr *
1824 gfc_neqv (gfc_expr *op1, gfc_expr *op2)
1826 return eval_intrinsic_f3 (INTRINSIC_NEQV, gfc_arith_neqv, op1, op2);
1830 gfc_expr *
1831 gfc_eq (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1833 return eval_intrinsic_f3 (op, gfc_arith_eq, op1, op2);
1837 gfc_expr *
1838 gfc_ne (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1840 return eval_intrinsic_f3 (op, gfc_arith_ne, op1, op2);
1844 gfc_expr *
1845 gfc_gt (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1847 return eval_intrinsic_f3 (op, gfc_arith_gt, op1, op2);
1851 gfc_expr *
1852 gfc_ge (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1854 return eval_intrinsic_f3 (op, gfc_arith_ge, op1, op2);
1858 gfc_expr *
1859 gfc_lt (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1861 return eval_intrinsic_f3 (op, gfc_arith_lt, op1, op2);
1865 gfc_expr *
1866 gfc_le (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1868 return eval_intrinsic_f3 (op, gfc_arith_le, op1, op2);
1872 /* Convert an integer string to an expression node. */
1874 gfc_expr *
1875 gfc_convert_integer (const char *buffer, int kind, int radix, locus *where)
1877 gfc_expr *e;
1878 const char *t;
1880 e = gfc_get_constant_expr (BT_INTEGER, kind, where);
1881 /* A leading plus is allowed, but not by mpz_set_str. */
1882 if (buffer[0] == '+')
1883 t = buffer + 1;
1884 else
1885 t = buffer;
1886 mpz_set_str (e->value.integer, t, radix);
1888 return e;
1892 /* Convert a real string to an expression node. */
1894 gfc_expr *
1895 gfc_convert_real (const char *buffer, int kind, locus *where)
1897 gfc_expr *e;
1899 e = gfc_get_constant_expr (BT_REAL, kind, where);
1900 mpfr_set_str (e->value.real, buffer, 10, GFC_RND_MODE);
1902 return e;
1906 /* Convert a pair of real, constant expression nodes to a single
1907 complex expression node. */
1909 gfc_expr *
1910 gfc_convert_complex (gfc_expr *real, gfc_expr *imag, int kind)
1912 gfc_expr *e;
1914 e = gfc_get_constant_expr (BT_COMPLEX, kind, &real->where);
1915 mpc_set_fr_fr (e->value.complex, real->value.real, imag->value.real,
1916 GFC_MPC_RND_MODE);
1918 return e;
1922 /******* Simplification of intrinsic functions with constant arguments *****/
1925 /* Deal with an arithmetic error. */
1927 static void
1928 arith_error (arith rc, gfc_typespec *from, gfc_typespec *to, locus *where)
1930 switch (rc)
1932 case ARITH_OK:
1933 gfc_error ("Arithmetic OK converting %s to %s at %L",
1934 gfc_typename (from), gfc_typename (to), where);
1935 break;
1936 case ARITH_OVERFLOW:
1937 gfc_error ("Arithmetic overflow converting %s to %s at %L. This check "
1938 "can be disabled with the option %<-fno-range-check%>",
1939 gfc_typename (from), gfc_typename (to), where);
1940 break;
1941 case ARITH_UNDERFLOW:
1942 gfc_error ("Arithmetic underflow converting %s to %s at %L. This check "
1943 "can be disabled with the option %<-fno-range-check%>",
1944 gfc_typename (from), gfc_typename (to), where);
1945 break;
1946 case ARITH_NAN:
1947 gfc_error ("Arithmetic NaN converting %s to %s at %L. This check "
1948 "can be disabled with the option %<-fno-range-check%>",
1949 gfc_typename (from), gfc_typename (to), where);
1950 break;
1951 case ARITH_DIV0:
1952 gfc_error ("Division by zero converting %s to %s at %L",
1953 gfc_typename (from), gfc_typename (to), where);
1954 break;
1955 case ARITH_INCOMMENSURATE:
1956 gfc_error ("Array operands are incommensurate converting %s to %s at %L",
1957 gfc_typename (from), gfc_typename (to), where);
1958 break;
1959 case ARITH_ASYMMETRIC:
1960 gfc_error ("Integer outside symmetric range implied by Standard Fortran"
1961 " converting %s to %s at %L",
1962 gfc_typename (from), gfc_typename (to), where);
1963 break;
1964 default:
1965 gfc_internal_error ("gfc_arith_error(): Bad error code");
1968 /* TODO: Do something about the error, i.e., throw exception, return
1969 NaN, etc. */
1972 /* Returns true if significant bits were lost when converting real
1973 constant r from from_kind to to_kind. */
1975 static bool
1976 wprecision_real_real (mpfr_t r, int from_kind, int to_kind)
1978 mpfr_t rv, diff;
1979 bool ret;
1981 gfc_set_model_kind (to_kind);
1982 mpfr_init (rv);
1983 gfc_set_model_kind (from_kind);
1984 mpfr_init (diff);
1986 mpfr_set (rv, r, GFC_RND_MODE);
1987 mpfr_sub (diff, rv, r, GFC_RND_MODE);
1989 ret = ! mpfr_zero_p (diff);
1990 mpfr_clear (rv);
1991 mpfr_clear (diff);
1992 return ret;
1995 /* Return true if conversion from an integer to a real loses precision. */
1997 static bool
1998 wprecision_int_real (mpz_t n, mpfr_t r)
2000 mpz_t i;
2001 mpz_init (i);
2002 mpfr_get_z (i, r, GFC_RND_MODE);
2003 mpz_sub (i, i, n);
2004 return mpz_cmp_si (i, 0) != 0;
2005 mpz_clear (i);
2009 /* Convert integers to integers. */
2011 gfc_expr *
2012 gfc_int2int (gfc_expr *src, int kind)
2014 gfc_expr *result;
2015 arith rc;
2017 result = gfc_get_constant_expr (BT_INTEGER, kind, &src->where);
2019 mpz_set (result->value.integer, src->value.integer);
2021 if ((rc = gfc_check_integer_range (result->value.integer, kind)) != ARITH_OK)
2023 if (rc == ARITH_ASYMMETRIC)
2025 gfc_warning (0, gfc_arith_error (rc), &src->where);
2027 else
2029 arith_error (rc, &src->ts, &result->ts, &src->where);
2030 gfc_free_expr (result);
2031 return NULL;
2035 /* If we do not trap numeric overflow, we need to convert the number to
2036 signed, throwing away high-order bits if necessary. */
2037 if (flag_range_check == 0)
2039 int k;
2041 k = gfc_validate_kind (BT_INTEGER, kind, false);
2042 gfc_convert_mpz_to_signed (result->value.integer,
2043 gfc_integer_kinds[k].bit_size);
2045 if (warn_conversion && kind < src->ts.kind)
2046 gfc_warning_now (OPT_Wconversion, "Conversion from %qs to %qs at %L",
2047 gfc_typename (&src->ts), gfc_typename (&result->ts),
2048 &src->where);
2050 return result;
2054 /* Convert integers to reals. */
2056 gfc_expr *
2057 gfc_int2real (gfc_expr *src, int kind)
2059 gfc_expr *result;
2060 arith rc;
2062 result = gfc_get_constant_expr (BT_REAL, kind, &src->where);
2064 mpfr_set_z (result->value.real, src->value.integer, GFC_RND_MODE);
2066 if ((rc = gfc_check_real_range (result->value.real, kind)) != ARITH_OK)
2068 arith_error (rc, &src->ts, &result->ts, &src->where);
2069 gfc_free_expr (result);
2070 return NULL;
2073 if (warn_conversion
2074 && wprecision_int_real (src->value.integer, result->value.real))
2075 gfc_warning_now (OPT_Wconversion, "Change of value in conversion "
2076 "from %qs to %qs at %L",
2077 gfc_typename (&src->ts),
2078 gfc_typename (&result->ts),
2079 &src->where);
2081 return result;
2085 /* Convert default integer to default complex. */
2087 gfc_expr *
2088 gfc_int2complex (gfc_expr *src, int kind)
2090 gfc_expr *result;
2091 arith rc;
2093 result = gfc_get_constant_expr (BT_COMPLEX, kind, &src->where);
2095 mpc_set_z (result->value.complex, src->value.integer, GFC_MPC_RND_MODE);
2097 if ((rc = gfc_check_real_range (mpc_realref (result->value.complex), kind))
2098 != ARITH_OK)
2100 arith_error (rc, &src->ts, &result->ts, &src->where);
2101 gfc_free_expr (result);
2102 return NULL;
2105 if (warn_conversion
2106 && wprecision_int_real (src->value.integer,
2107 mpc_realref (result->value.complex)))
2108 gfc_warning_now (OPT_Wconversion, "Change of value in conversion "
2109 "from %qs to %qs at %L",
2110 gfc_typename (&src->ts),
2111 gfc_typename (&result->ts),
2112 &src->where);
2114 return result;
2118 /* Convert default real to default integer. */
2120 gfc_expr *
2121 gfc_real2int (gfc_expr *src, int kind)
2123 gfc_expr *result;
2124 arith rc;
2125 bool did_warn = false;
2127 result = gfc_get_constant_expr (BT_INTEGER, kind, &src->where);
2129 gfc_mpfr_to_mpz (result->value.integer, src->value.real, &src->where);
2131 if ((rc = gfc_check_integer_range (result->value.integer, kind)) != ARITH_OK)
2133 arith_error (rc, &src->ts, &result->ts, &src->where);
2134 gfc_free_expr (result);
2135 return NULL;
2138 /* If there was a fractional part, warn about this. */
2140 if (warn_conversion)
2142 mpfr_t f;
2143 mpfr_init (f);
2144 mpfr_frac (f, src->value.real, GFC_RND_MODE);
2145 if (mpfr_cmp_si (f, 0) != 0)
2147 gfc_warning_now (OPT_Wconversion, "Change of value in conversion "
2148 "from %qs to %qs at %L", gfc_typename (&src->ts),
2149 gfc_typename (&result->ts), &src->where);
2150 did_warn = true;
2153 if (!did_warn && warn_conversion_extra)
2155 gfc_warning_now (OPT_Wconversion_extra, "Conversion from %qs to %qs "
2156 "at %L", gfc_typename (&src->ts),
2157 gfc_typename (&result->ts), &src->where);
2160 return result;
2164 /* Convert real to real. */
2166 gfc_expr *
2167 gfc_real2real (gfc_expr *src, int kind)
2169 gfc_expr *result;
2170 arith rc;
2171 bool did_warn = false;
2173 result = gfc_get_constant_expr (BT_REAL, kind, &src->where);
2175 mpfr_set (result->value.real, src->value.real, GFC_RND_MODE);
2177 rc = gfc_check_real_range (result->value.real, kind);
2179 if (rc == ARITH_UNDERFLOW)
2181 if (warn_underflow)
2182 gfc_warning (OPT_Woverflow, gfc_arith_error (rc), &src->where);
2183 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
2185 else if (rc != ARITH_OK)
2187 arith_error (rc, &src->ts, &result->ts, &src->where);
2188 gfc_free_expr (result);
2189 return NULL;
2192 /* As a special bonus, don't warn about REAL values which are not changed by
2193 the conversion if -Wconversion is specified and -Wconversion-extra is
2194 not. */
2196 if ((warn_conversion || warn_conversion_extra) && src->ts.kind > kind)
2198 int w = warn_conversion ? OPT_Wconversion : OPT_Wconversion_extra;
2200 /* Calculate the difference between the constant and the rounded
2201 value and check it against zero. */
2203 if (wprecision_real_real (src->value.real, src->ts.kind, kind))
2205 gfc_warning_now (w, "Change of value in conversion from "
2206 "%qs to %qs at %L",
2207 gfc_typename (&src->ts), gfc_typename (&result->ts),
2208 &src->where);
2209 /* Make sure the conversion warning is not emitted again. */
2210 did_warn = true;
2214 if (!did_warn && warn_conversion_extra)
2215 gfc_warning_now (OPT_Wconversion_extra, "Conversion from %qs to %qs "
2216 "at %L", gfc_typename(&src->ts),
2217 gfc_typename(&result->ts), &src->where);
2219 return result;
2223 /* Convert real to complex. */
2225 gfc_expr *
2226 gfc_real2complex (gfc_expr *src, int kind)
2228 gfc_expr *result;
2229 arith rc;
2230 bool did_warn = false;
2232 result = gfc_get_constant_expr (BT_COMPLEX, kind, &src->where);
2234 mpc_set_fr (result->value.complex, src->value.real, GFC_MPC_RND_MODE);
2236 rc = gfc_check_real_range (mpc_realref (result->value.complex), kind);
2238 if (rc == ARITH_UNDERFLOW)
2240 if (warn_underflow)
2241 gfc_warning (OPT_Woverflow, gfc_arith_error (rc), &src->where);
2242 mpfr_set_ui (mpc_realref (result->value.complex), 0, GFC_RND_MODE);
2244 else if (rc != ARITH_OK)
2246 arith_error (rc, &src->ts, &result->ts, &src->where);
2247 gfc_free_expr (result);
2248 return NULL;
2251 if ((warn_conversion || warn_conversion_extra) && src->ts.kind > kind)
2253 int w = warn_conversion ? OPT_Wconversion : OPT_Wconversion_extra;
2255 if (wprecision_real_real (src->value.real, src->ts.kind, kind))
2257 gfc_warning_now (w, "Change of value in conversion from "
2258 "%qs to %qs at %L",
2259 gfc_typename (&src->ts), gfc_typename (&result->ts),
2260 &src->where);
2261 /* Make sure the conversion warning is not emitted again. */
2262 did_warn = true;
2266 if (!did_warn && warn_conversion_extra)
2267 gfc_warning_now (OPT_Wconversion_extra, "Conversion from %qs to %qs "
2268 "at %L", gfc_typename(&src->ts),
2269 gfc_typename(&result->ts), &src->where);
2271 return result;
2275 /* Convert complex to integer. */
2277 gfc_expr *
2278 gfc_complex2int (gfc_expr *src, int kind)
2280 gfc_expr *result;
2281 arith rc;
2282 bool did_warn = false;
2284 result = gfc_get_constant_expr (BT_INTEGER, kind, &src->where);
2286 gfc_mpfr_to_mpz (result->value.integer, mpc_realref (src->value.complex),
2287 &src->where);
2289 if ((rc = gfc_check_integer_range (result->value.integer, kind)) != ARITH_OK)
2291 arith_error (rc, &src->ts, &result->ts, &src->where);
2292 gfc_free_expr (result);
2293 return NULL;
2296 if (warn_conversion || warn_conversion_extra)
2298 int w = warn_conversion ? OPT_Wconversion : OPT_Wconversion_extra;
2300 /* See if we discarded an imaginary part. */
2301 if (mpfr_cmp_si (mpc_imagref (src->value.complex), 0) != 0)
2303 gfc_warning_now (w, "Non-zero imaginary part discarded "
2304 "in conversion from %qs to %qs at %L",
2305 gfc_typename(&src->ts), gfc_typename (&result->ts),
2306 &src->where);
2307 did_warn = true;
2310 else {
2311 mpfr_t f;
2313 mpfr_init (f);
2314 mpfr_frac (f, src->value.real, GFC_RND_MODE);
2315 if (mpfr_cmp_si (f, 0) != 0)
2317 gfc_warning_now (w, "Change of value in conversion from "
2318 "%qs to %qs at %L", gfc_typename (&src->ts),
2319 gfc_typename (&result->ts), &src->where);
2320 did_warn = true;
2322 mpfr_clear (f);
2325 if (!did_warn && warn_conversion_extra)
2327 gfc_warning_now (OPT_Wconversion_extra, "Conversion from %qs to %qs "
2328 "at %L", gfc_typename (&src->ts),
2329 gfc_typename (&result->ts), &src->where);
2333 return result;
2337 /* Convert complex to real. */
2339 gfc_expr *
2340 gfc_complex2real (gfc_expr *src, int kind)
2342 gfc_expr *result;
2343 arith rc;
2344 bool did_warn = false;
2346 result = gfc_get_constant_expr (BT_REAL, kind, &src->where);
2348 mpc_real (result->value.real, src->value.complex, GFC_RND_MODE);
2350 rc = gfc_check_real_range (result->value.real, kind);
2352 if (rc == ARITH_UNDERFLOW)
2354 if (warn_underflow)
2355 gfc_warning (OPT_Woverflow, gfc_arith_error (rc), &src->where);
2356 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
2358 if (rc != ARITH_OK)
2360 arith_error (rc, &src->ts, &result->ts, &src->where);
2361 gfc_free_expr (result);
2362 return NULL;
2365 if (warn_conversion || warn_conversion_extra)
2367 int w = warn_conversion ? OPT_Wconversion : OPT_Wconversion_extra;
2369 /* See if we discarded an imaginary part. */
2370 if (mpfr_cmp_si (mpc_imagref (src->value.complex), 0) != 0)
2372 gfc_warning_now (w, "Non-zero imaginary part discarded "
2373 "in conversion from %qs to %qs at %L",
2374 gfc_typename(&src->ts), gfc_typename (&result->ts),
2375 &src->where);
2376 did_warn = true;
2379 /* Calculate the difference between the real constant and the rounded
2380 value and check it against zero. */
2382 if (kind > src->ts.kind
2383 && wprecision_real_real (mpc_realref (src->value.complex),
2384 src->ts.kind, kind))
2386 gfc_warning_now (w, "Change of value in conversion from "
2387 "%qs to %qs at %L",
2388 gfc_typename (&src->ts), gfc_typename (&result->ts),
2389 &src->where);
2390 /* Make sure the conversion warning is not emitted again. */
2391 did_warn = true;
2395 if (!did_warn && warn_conversion_extra)
2396 gfc_warning_now (OPT_Wconversion, "Conversion from %qs to %qs at %L",
2397 gfc_typename(&src->ts), gfc_typename (&result->ts),
2398 &src->where);
2400 return result;
2404 /* Convert complex to complex. */
2406 gfc_expr *
2407 gfc_complex2complex (gfc_expr *src, int kind)
2409 gfc_expr *result;
2410 arith rc;
2411 bool did_warn = false;
2413 result = gfc_get_constant_expr (BT_COMPLEX, kind, &src->where);
2415 mpc_set (result->value.complex, src->value.complex, GFC_MPC_RND_MODE);
2417 rc = gfc_check_real_range (mpc_realref (result->value.complex), kind);
2419 if (rc == ARITH_UNDERFLOW)
2421 if (warn_underflow)
2422 gfc_warning (OPT_Woverflow, gfc_arith_error (rc), &src->where);
2423 mpfr_set_ui (mpc_realref (result->value.complex), 0, GFC_RND_MODE);
2425 else if (rc != ARITH_OK)
2427 arith_error (rc, &src->ts, &result->ts, &src->where);
2428 gfc_free_expr (result);
2429 return NULL;
2432 rc = gfc_check_real_range (mpc_imagref (result->value.complex), kind);
2434 if (rc == ARITH_UNDERFLOW)
2436 if (warn_underflow)
2437 gfc_warning (OPT_Woverflow, gfc_arith_error (rc), &src->where);
2438 mpfr_set_ui (mpc_imagref (result->value.complex), 0, GFC_RND_MODE);
2440 else if (rc != ARITH_OK)
2442 arith_error (rc, &src->ts, &result->ts, &src->where);
2443 gfc_free_expr (result);
2444 return NULL;
2447 if ((warn_conversion || warn_conversion_extra) && src->ts.kind > kind
2448 && (wprecision_real_real (mpc_realref (src->value.complex),
2449 src->ts.kind, kind)
2450 || wprecision_real_real (mpc_imagref (src->value.complex),
2451 src->ts.kind, kind)))
2453 int w = warn_conversion ? OPT_Wconversion : OPT_Wconversion_extra;
2455 gfc_warning_now (w, "Change of value in conversion from "
2456 " %qs to %qs at %L",
2457 gfc_typename (&src->ts), gfc_typename (&result->ts),
2458 &src->where);
2459 did_warn = true;
2462 if (!did_warn && warn_conversion_extra && src->ts.kind != kind)
2463 gfc_warning_now (OPT_Wconversion_extra, "Conversion from %qs to %qs "
2464 "at %L", gfc_typename(&src->ts),
2465 gfc_typename (&result->ts), &src->where);
2467 return result;
2471 /* Logical kind conversion. */
2473 gfc_expr *
2474 gfc_log2log (gfc_expr *src, int kind)
2476 gfc_expr *result;
2478 result = gfc_get_constant_expr (BT_LOGICAL, kind, &src->where);
2479 result->value.logical = src->value.logical;
2481 return result;
2485 /* Convert logical to integer. */
2487 gfc_expr *
2488 gfc_log2int (gfc_expr *src, int kind)
2490 gfc_expr *result;
2492 result = gfc_get_constant_expr (BT_INTEGER, kind, &src->where);
2493 mpz_set_si (result->value.integer, src->value.logical);
2495 return result;
2499 /* Convert integer to logical. */
2501 gfc_expr *
2502 gfc_int2log (gfc_expr *src, int kind)
2504 gfc_expr *result;
2506 result = gfc_get_constant_expr (BT_LOGICAL, kind, &src->where);
2507 result->value.logical = (mpz_cmp_si (src->value.integer, 0) != 0);
2509 return result;
2513 /* Helper function to set the representation in a Hollerith conversion.
2514 This assumes that the ts.type and ts.kind of the result have already
2515 been set. */
2517 static void
2518 hollerith2representation (gfc_expr *result, gfc_expr *src)
2520 int src_len, result_len;
2522 src_len = src->representation.length - src->ts.u.pad;
2523 result_len = gfc_target_expr_size (result);
2525 if (src_len > result_len)
2527 gfc_warning (0,
2528 "The Hollerith constant at %L is too long to convert to %qs",
2529 &src->where, gfc_typename(&result->ts));
2532 result->representation.string = XCNEWVEC (char, result_len + 1);
2533 memcpy (result->representation.string, src->representation.string,
2534 MIN (result_len, src_len));
2536 if (src_len < result_len)
2537 memset (&result->representation.string[src_len], ' ', result_len - src_len);
2539 result->representation.string[result_len] = '\0'; /* For debugger */
2540 result->representation.length = result_len;
2544 /* Convert Hollerith to integer. The constant will be padded or truncated. */
2546 gfc_expr *
2547 gfc_hollerith2int (gfc_expr *src, int kind)
2549 gfc_expr *result;
2550 result = gfc_get_constant_expr (BT_INTEGER, kind, &src->where);
2552 hollerith2representation (result, src);
2553 gfc_interpret_integer (kind, (unsigned char *) result->representation.string,
2554 result->representation.length, result->value.integer);
2556 return result;
2560 /* Convert Hollerith to real. The constant will be padded or truncated. */
2562 gfc_expr *
2563 gfc_hollerith2real (gfc_expr *src, int kind)
2565 gfc_expr *result;
2566 result = gfc_get_constant_expr (BT_REAL, kind, &src->where);
2568 hollerith2representation (result, src);
2569 gfc_interpret_float (kind, (unsigned char *) result->representation.string,
2570 result->representation.length, result->value.real);
2572 return result;
2576 /* Convert Hollerith to complex. The constant will be padded or truncated. */
2578 gfc_expr *
2579 gfc_hollerith2complex (gfc_expr *src, int kind)
2581 gfc_expr *result;
2582 result = gfc_get_constant_expr (BT_COMPLEX, kind, &src->where);
2584 hollerith2representation (result, src);
2585 gfc_interpret_complex (kind, (unsigned char *) result->representation.string,
2586 result->representation.length, result->value.complex);
2588 return result;
2592 /* Convert Hollerith to character. */
2594 gfc_expr *
2595 gfc_hollerith2character (gfc_expr *src, int kind)
2597 gfc_expr *result;
2599 result = gfc_copy_expr (src);
2600 result->ts.type = BT_CHARACTER;
2601 result->ts.kind = kind;
2603 result->value.character.length = result->representation.length;
2604 result->value.character.string
2605 = gfc_char_to_widechar (result->representation.string);
2607 return result;
2611 /* Convert Hollerith to logical. The constant will be padded or truncated. */
2613 gfc_expr *
2614 gfc_hollerith2logical (gfc_expr *src, int kind)
2616 gfc_expr *result;
2617 result = gfc_get_constant_expr (BT_LOGICAL, kind, &src->where);
2619 hollerith2representation (result, src);
2620 gfc_interpret_logical (kind, (unsigned char *) result->representation.string,
2621 result->representation.length, &result->value.logical);
2623 return result;