2010-10-20 Jerry DeLisle <jvdelisle@gcc.gnu.org>
[official-gcc.git] / gcc / fortran / arith.c
blob2a9ea7501036e0e9a3f1052bc887119b8fb275d6
1 /* Compiler arithmetic
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3 2009, 2010
4 Free Software Foundation, Inc.
5 Contributed by Andy Vaught
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* Since target arithmetic must be done on the host, there has to
24 be some way of evaluating arithmetic expressions as the host
25 would evaluate them. We use the GNU MP library and the MPFR
26 library to do arithmetic, and this file provides the interface. */
28 #include "config.h"
29 #include "system.h"
30 #include "flags.h"
31 #include "gfortran.h"
32 #include "arith.h"
33 #include "target-memory.h"
34 #include "constructor.h"
36 /* MPFR does not have a direct replacement for mpz_set_f() from GMP.
37 It's easily implemented with a few calls though. */
39 void
40 gfc_mpfr_to_mpz (mpz_t z, mpfr_t x, locus *where)
42 mp_exp_t e;
44 if (mpfr_inf_p (x) || mpfr_nan_p (x))
46 gfc_error ("Conversion of an Infinity or Not-a-Number at %L "
47 "to INTEGER", where);
48 mpz_set_ui (z, 0);
49 return;
52 e = mpfr_get_z_exp (z, x);
54 if (e > 0)
55 mpz_mul_2exp (z, z, e);
56 else
57 mpz_tdiv_q_2exp (z, z, -e);
61 /* Set the model number precision by the requested KIND. */
63 void
64 gfc_set_model_kind (int kind)
66 int index = gfc_validate_kind (BT_REAL, kind, false);
67 int base2prec;
69 base2prec = gfc_real_kinds[index].digits;
70 if (gfc_real_kinds[index].radix != 2)
71 base2prec *= gfc_real_kinds[index].radix / 2;
72 mpfr_set_default_prec (base2prec);
76 /* Set the model number precision from mpfr_t x. */
78 void
79 gfc_set_model (mpfr_t x)
81 mpfr_set_default_prec (mpfr_get_prec (x));
85 /* Given an arithmetic error code, return a pointer to a string that
86 explains the error. */
88 static const char *
89 gfc_arith_error (arith code)
91 const char *p;
93 switch (code)
95 case ARITH_OK:
96 p = _("Arithmetic OK at %L");
97 break;
98 case ARITH_OVERFLOW:
99 p = _("Arithmetic overflow at %L");
100 break;
101 case ARITH_UNDERFLOW:
102 p = _("Arithmetic underflow at %L");
103 break;
104 case ARITH_NAN:
105 p = _("Arithmetic NaN at %L");
106 break;
107 case ARITH_DIV0:
108 p = _("Division by zero at %L");
109 break;
110 case ARITH_INCOMMENSURATE:
111 p = _("Array operands are incommensurate at %L");
112 break;
113 case ARITH_ASYMMETRIC:
115 _("Integer outside symmetric range implied by Standard Fortran at %L");
116 break;
117 default:
118 gfc_internal_error ("gfc_arith_error(): Bad error code");
121 return p;
125 /* Get things ready to do math. */
127 void
128 gfc_arith_init_1 (void)
130 gfc_integer_info *int_info;
131 gfc_real_info *real_info;
132 mpfr_t a, b;
133 int i;
135 mpfr_set_default_prec (128);
136 mpfr_init (a);
138 /* Convert the minimum and maximum values for each kind into their
139 GNU MP representation. */
140 for (int_info = gfc_integer_kinds; int_info->kind != 0; int_info++)
142 /* Huge */
143 mpz_init (int_info->huge);
144 mpz_set_ui (int_info->huge, int_info->radix);
145 mpz_pow_ui (int_info->huge, int_info->huge, int_info->digits);
146 mpz_sub_ui (int_info->huge, int_info->huge, 1);
148 /* These are the numbers that are actually representable by the
149 target. For bases other than two, this needs to be changed. */
150 if (int_info->radix != 2)
151 gfc_internal_error ("Fix min_int calculation");
153 /* See PRs 13490 and 17912, related to integer ranges.
154 The pedantic_min_int exists for range checking when a program
155 is compiled with -pedantic, and reflects the belief that
156 Standard Fortran requires integers to be symmetrical, i.e.
157 every negative integer must have a representable positive
158 absolute value, and vice versa. */
160 mpz_init (int_info->pedantic_min_int);
161 mpz_neg (int_info->pedantic_min_int, int_info->huge);
163 mpz_init (int_info->min_int);
164 mpz_sub_ui (int_info->min_int, int_info->pedantic_min_int, 1);
166 /* Range */
167 mpfr_set_z (a, int_info->huge, GFC_RND_MODE);
168 mpfr_log10 (a, a, GFC_RND_MODE);
169 mpfr_trunc (a, a);
170 int_info->range = (int) mpfr_get_si (a, GFC_RND_MODE);
173 mpfr_clear (a);
175 for (real_info = gfc_real_kinds; real_info->kind != 0; real_info++)
177 gfc_set_model_kind (real_info->kind);
179 mpfr_init (a);
180 mpfr_init (b);
182 /* huge(x) = (1 - b**(-p)) * b**(emax-1) * b */
183 /* 1 - b**(-p) */
184 mpfr_init (real_info->huge);
185 mpfr_set_ui (real_info->huge, 1, GFC_RND_MODE);
186 mpfr_set_ui (a, real_info->radix, GFC_RND_MODE);
187 mpfr_pow_si (a, a, -real_info->digits, GFC_RND_MODE);
188 mpfr_sub (real_info->huge, real_info->huge, a, GFC_RND_MODE);
190 /* b**(emax-1) */
191 mpfr_set_ui (a, real_info->radix, GFC_RND_MODE);
192 mpfr_pow_ui (a, a, real_info->max_exponent - 1, GFC_RND_MODE);
194 /* (1 - b**(-p)) * b**(emax-1) */
195 mpfr_mul (real_info->huge, real_info->huge, a, GFC_RND_MODE);
197 /* (1 - b**(-p)) * b**(emax-1) * b */
198 mpfr_mul_ui (real_info->huge, real_info->huge, real_info->radix,
199 GFC_RND_MODE);
201 /* tiny(x) = b**(emin-1) */
202 mpfr_init (real_info->tiny);
203 mpfr_set_ui (real_info->tiny, real_info->radix, GFC_RND_MODE);
204 mpfr_pow_si (real_info->tiny, real_info->tiny,
205 real_info->min_exponent - 1, GFC_RND_MODE);
207 /* subnormal (x) = b**(emin - digit) */
208 mpfr_init (real_info->subnormal);
209 mpfr_set_ui (real_info->subnormal, real_info->radix, GFC_RND_MODE);
210 mpfr_pow_si (real_info->subnormal, real_info->subnormal,
211 real_info->min_exponent - real_info->digits, GFC_RND_MODE);
213 /* epsilon(x) = b**(1-p) */
214 mpfr_init (real_info->epsilon);
215 mpfr_set_ui (real_info->epsilon, real_info->radix, GFC_RND_MODE);
216 mpfr_pow_si (real_info->epsilon, real_info->epsilon,
217 1 - real_info->digits, GFC_RND_MODE);
219 /* range(x) = int(min(log10(huge(x)), -log10(tiny)) */
220 mpfr_log10 (a, real_info->huge, GFC_RND_MODE);
221 mpfr_log10 (b, real_info->tiny, GFC_RND_MODE);
222 mpfr_neg (b, b, GFC_RND_MODE);
224 /* a = min(a, b) */
225 mpfr_min (a, a, b, GFC_RND_MODE);
226 mpfr_trunc (a, a);
227 real_info->range = (int) mpfr_get_si (a, GFC_RND_MODE);
229 /* precision(x) = int((p - 1) * log10(b)) + k */
230 mpfr_set_ui (a, real_info->radix, GFC_RND_MODE);
231 mpfr_log10 (a, a, GFC_RND_MODE);
232 mpfr_mul_ui (a, a, real_info->digits - 1, GFC_RND_MODE);
233 mpfr_trunc (a, a);
234 real_info->precision = (int) mpfr_get_si (a, GFC_RND_MODE);
236 /* If the radix is an integral power of 10, add one to the precision. */
237 for (i = 10; i <= real_info->radix; i *= 10)
238 if (i == real_info->radix)
239 real_info->precision++;
241 mpfr_clears (a, b, NULL);
246 /* Clean up, get rid of numeric constants. */
248 void
249 gfc_arith_done_1 (void)
251 gfc_integer_info *ip;
252 gfc_real_info *rp;
254 for (ip = gfc_integer_kinds; ip->kind; ip++)
256 mpz_clear (ip->min_int);
257 mpz_clear (ip->pedantic_min_int);
258 mpz_clear (ip->huge);
261 for (rp = gfc_real_kinds; rp->kind; rp++)
262 mpfr_clears (rp->epsilon, rp->huge, rp->tiny, rp->subnormal, NULL);
264 mpfr_free_cache ();
268 /* Given a wide character value and a character kind, determine whether
269 the character is representable for that kind. */
270 bool
271 gfc_check_character_range (gfc_char_t c, int kind)
273 /* As wide characters are stored as 32-bit values, they're all
274 representable in UCS=4. */
275 if (kind == 4)
276 return true;
278 if (kind == 1)
279 return c <= 255 ? true : false;
281 gcc_unreachable ();
285 /* Given an integer and a kind, make sure that the integer lies within
286 the range of the kind. Returns ARITH_OK, ARITH_ASYMMETRIC or
287 ARITH_OVERFLOW. */
289 arith
290 gfc_check_integer_range (mpz_t p, int kind)
292 arith result;
293 int i;
295 i = gfc_validate_kind (BT_INTEGER, kind, false);
296 result = ARITH_OK;
298 if (pedantic)
300 if (mpz_cmp (p, gfc_integer_kinds[i].pedantic_min_int) < 0)
301 result = ARITH_ASYMMETRIC;
305 if (gfc_option.flag_range_check == 0)
306 return result;
308 if (mpz_cmp (p, gfc_integer_kinds[i].min_int) < 0
309 || mpz_cmp (p, gfc_integer_kinds[i].huge) > 0)
310 result = ARITH_OVERFLOW;
312 return result;
316 /* Given a real and a kind, make sure that the real lies within the
317 range of the kind. Returns ARITH_OK, ARITH_OVERFLOW or
318 ARITH_UNDERFLOW. */
320 static arith
321 gfc_check_real_range (mpfr_t p, int kind)
323 arith retval;
324 mpfr_t q;
325 int i;
327 i = gfc_validate_kind (BT_REAL, kind, false);
329 gfc_set_model (p);
330 mpfr_init (q);
331 mpfr_abs (q, p, GFC_RND_MODE);
333 retval = ARITH_OK;
335 if (mpfr_inf_p (p))
337 if (gfc_option.flag_range_check != 0)
338 retval = ARITH_OVERFLOW;
340 else if (mpfr_nan_p (p))
342 if (gfc_option.flag_range_check != 0)
343 retval = ARITH_NAN;
345 else if (mpfr_sgn (q) == 0)
347 mpfr_clear (q);
348 return retval;
350 else if (mpfr_cmp (q, gfc_real_kinds[i].huge) > 0)
352 if (gfc_option.flag_range_check == 0)
353 mpfr_set_inf (p, mpfr_sgn (p));
354 else
355 retval = ARITH_OVERFLOW;
357 else if (mpfr_cmp (q, gfc_real_kinds[i].subnormal) < 0)
359 if (gfc_option.flag_range_check == 0)
361 if (mpfr_sgn (p) < 0)
363 mpfr_set_ui (p, 0, GFC_RND_MODE);
364 mpfr_set_si (q, -1, GFC_RND_MODE);
365 mpfr_copysign (p, p, q, GFC_RND_MODE);
367 else
368 mpfr_set_ui (p, 0, GFC_RND_MODE);
370 else
371 retval = ARITH_UNDERFLOW;
373 else if (mpfr_cmp (q, gfc_real_kinds[i].tiny) < 0)
375 mp_exp_t emin, emax;
376 int en;
378 /* Save current values of emin and emax. */
379 emin = mpfr_get_emin ();
380 emax = mpfr_get_emax ();
382 /* Set emin and emax for the current model number. */
383 en = gfc_real_kinds[i].min_exponent - gfc_real_kinds[i].digits + 1;
384 mpfr_set_emin ((mp_exp_t) en);
385 mpfr_set_emax ((mp_exp_t) gfc_real_kinds[i].max_exponent);
386 mpfr_check_range (q, 0, GFC_RND_MODE);
387 mpfr_subnormalize (q, 0, GFC_RND_MODE);
389 /* Reset emin and emax. */
390 mpfr_set_emin (emin);
391 mpfr_set_emax (emax);
393 /* Copy sign if needed. */
394 if (mpfr_sgn (p) < 0)
395 mpfr_neg (p, q, GMP_RNDN);
396 else
397 mpfr_set (p, q, GMP_RNDN);
400 mpfr_clear (q);
402 return retval;
406 /* Low-level arithmetic functions. All of these subroutines assume
407 that all operands are of the same type and return an operand of the
408 same type. The other thing about these subroutines is that they
409 can fail in various ways -- overflow, underflow, division by zero,
410 zero raised to the zero, etc. */
412 static arith
413 gfc_arith_not (gfc_expr *op1, gfc_expr **resultp)
415 gfc_expr *result;
417 result = gfc_get_constant_expr (BT_LOGICAL, op1->ts.kind, &op1->where);
418 result->value.logical = !op1->value.logical;
419 *resultp = result;
421 return ARITH_OK;
425 static arith
426 gfc_arith_and (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
428 gfc_expr *result;
430 result = gfc_get_constant_expr (BT_LOGICAL, gfc_kind_max (op1, op2),
431 &op1->where);
432 result->value.logical = op1->value.logical && op2->value.logical;
433 *resultp = result;
435 return ARITH_OK;
439 static arith
440 gfc_arith_or (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
442 gfc_expr *result;
444 result = gfc_get_constant_expr (BT_LOGICAL, gfc_kind_max (op1, op2),
445 &op1->where);
446 result->value.logical = op1->value.logical || op2->value.logical;
447 *resultp = result;
449 return ARITH_OK;
453 static arith
454 gfc_arith_eqv (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
456 gfc_expr *result;
458 result = gfc_get_constant_expr (BT_LOGICAL, gfc_kind_max (op1, op2),
459 &op1->where);
460 result->value.logical = op1->value.logical == op2->value.logical;
461 *resultp = result;
463 return ARITH_OK;
467 static arith
468 gfc_arith_neqv (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
470 gfc_expr *result;
472 result = gfc_get_constant_expr (BT_LOGICAL, gfc_kind_max (op1, op2),
473 &op1->where);
474 result->value.logical = op1->value.logical != op2->value.logical;
475 *resultp = result;
477 return ARITH_OK;
481 /* Make sure a constant numeric expression is within the range for
482 its type and kind. Note that there's also a gfc_check_range(),
483 but that one deals with the intrinsic RANGE function. */
485 arith
486 gfc_range_check (gfc_expr *e)
488 arith rc;
489 arith rc2;
491 switch (e->ts.type)
493 case BT_INTEGER:
494 rc = gfc_check_integer_range (e->value.integer, e->ts.kind);
495 break;
497 case BT_REAL:
498 rc = gfc_check_real_range (e->value.real, e->ts.kind);
499 if (rc == ARITH_UNDERFLOW)
500 mpfr_set_ui (e->value.real, 0, GFC_RND_MODE);
501 if (rc == ARITH_OVERFLOW)
502 mpfr_set_inf (e->value.real, mpfr_sgn (e->value.real));
503 if (rc == ARITH_NAN)
504 mpfr_set_nan (e->value.real);
505 break;
507 case BT_COMPLEX:
508 rc = gfc_check_real_range (mpc_realref (e->value.complex), e->ts.kind);
509 if (rc == ARITH_UNDERFLOW)
510 mpfr_set_ui (mpc_realref (e->value.complex), 0, GFC_RND_MODE);
511 if (rc == ARITH_OVERFLOW)
512 mpfr_set_inf (mpc_realref (e->value.complex),
513 mpfr_sgn (mpc_realref (e->value.complex)));
514 if (rc == ARITH_NAN)
515 mpfr_set_nan (mpc_realref (e->value.complex));
517 rc2 = gfc_check_real_range (mpc_imagref (e->value.complex), e->ts.kind);
518 if (rc == ARITH_UNDERFLOW)
519 mpfr_set_ui (mpc_imagref (e->value.complex), 0, GFC_RND_MODE);
520 if (rc == ARITH_OVERFLOW)
521 mpfr_set_inf (mpc_imagref (e->value.complex),
522 mpfr_sgn (mpc_imagref (e->value.complex)));
523 if (rc == ARITH_NAN)
524 mpfr_set_nan (mpc_imagref (e->value.complex));
526 if (rc == ARITH_OK)
527 rc = rc2;
528 break;
530 default:
531 gfc_internal_error ("gfc_range_check(): Bad type");
534 return rc;
538 /* Several of the following routines use the same set of statements to
539 check the validity of the result. Encapsulate the checking here. */
541 static arith
542 check_result (arith rc, gfc_expr *x, gfc_expr *r, gfc_expr **rp)
544 arith val = rc;
546 if (val == ARITH_UNDERFLOW)
548 if (gfc_option.warn_underflow)
549 gfc_warning (gfc_arith_error (val), &x->where);
550 val = ARITH_OK;
553 if (val == ARITH_ASYMMETRIC)
555 gfc_warning (gfc_arith_error (val), &x->where);
556 val = ARITH_OK;
559 if (val != ARITH_OK)
560 gfc_free_expr (r);
561 else
562 *rp = r;
564 return val;
568 /* It may seem silly to have a subroutine that actually computes the
569 unary plus of a constant, but it prevents us from making exceptions
570 in the code elsewhere. Used for unary plus and parenthesized
571 expressions. */
573 static arith
574 gfc_arith_identity (gfc_expr *op1, gfc_expr **resultp)
576 *resultp = gfc_copy_expr (op1);
577 return ARITH_OK;
581 static arith
582 gfc_arith_uminus (gfc_expr *op1, gfc_expr **resultp)
584 gfc_expr *result;
585 arith rc;
587 result = gfc_get_constant_expr (op1->ts.type, op1->ts.kind, &op1->where);
589 switch (op1->ts.type)
591 case BT_INTEGER:
592 mpz_neg (result->value.integer, op1->value.integer);
593 break;
595 case BT_REAL:
596 mpfr_neg (result->value.real, op1->value.real, GFC_RND_MODE);
597 break;
599 case BT_COMPLEX:
600 mpc_neg (result->value.complex, op1->value.complex, GFC_MPC_RND_MODE);
601 break;
603 default:
604 gfc_internal_error ("gfc_arith_uminus(): Bad basic type");
607 rc = gfc_range_check (result);
609 return check_result (rc, op1, result, resultp);
613 static arith
614 gfc_arith_plus (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
616 gfc_expr *result;
617 arith rc;
619 result = gfc_get_constant_expr (op1->ts.type, op1->ts.kind, &op1->where);
621 switch (op1->ts.type)
623 case BT_INTEGER:
624 mpz_add (result->value.integer, op1->value.integer, op2->value.integer);
625 break;
627 case BT_REAL:
628 mpfr_add (result->value.real, op1->value.real, op2->value.real,
629 GFC_RND_MODE);
630 break;
632 case BT_COMPLEX:
633 mpc_add (result->value.complex, op1->value.complex, op2->value.complex,
634 GFC_MPC_RND_MODE);
635 break;
637 default:
638 gfc_internal_error ("gfc_arith_plus(): Bad basic type");
641 rc = gfc_range_check (result);
643 return check_result (rc, op1, result, resultp);
647 static arith
648 gfc_arith_minus (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
650 gfc_expr *result;
651 arith rc;
653 result = gfc_get_constant_expr (op1->ts.type, op1->ts.kind, &op1->where);
655 switch (op1->ts.type)
657 case BT_INTEGER:
658 mpz_sub (result->value.integer, op1->value.integer, op2->value.integer);
659 break;
661 case BT_REAL:
662 mpfr_sub (result->value.real, op1->value.real, op2->value.real,
663 GFC_RND_MODE);
664 break;
666 case BT_COMPLEX:
667 mpc_sub (result->value.complex, op1->value.complex,
668 op2->value.complex, GFC_MPC_RND_MODE);
669 break;
671 default:
672 gfc_internal_error ("gfc_arith_minus(): Bad basic type");
675 rc = gfc_range_check (result);
677 return check_result (rc, op1, result, resultp);
681 static arith
682 gfc_arith_times (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
684 gfc_expr *result;
685 arith rc;
687 result = gfc_get_constant_expr (op1->ts.type, op1->ts.kind, &op1->where);
689 switch (op1->ts.type)
691 case BT_INTEGER:
692 mpz_mul (result->value.integer, op1->value.integer, op2->value.integer);
693 break;
695 case BT_REAL:
696 mpfr_mul (result->value.real, op1->value.real, op2->value.real,
697 GFC_RND_MODE);
698 break;
700 case BT_COMPLEX:
701 gfc_set_model (mpc_realref (op1->value.complex));
702 mpc_mul (result->value.complex, op1->value.complex, op2->value.complex,
703 GFC_MPC_RND_MODE);
704 break;
706 default:
707 gfc_internal_error ("gfc_arith_times(): Bad basic type");
710 rc = gfc_range_check (result);
712 return check_result (rc, op1, result, resultp);
716 static arith
717 gfc_arith_divide (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
719 gfc_expr *result;
720 arith rc;
722 rc = ARITH_OK;
724 result = gfc_get_constant_expr (op1->ts.type, op1->ts.kind, &op1->where);
726 switch (op1->ts.type)
728 case BT_INTEGER:
729 if (mpz_sgn (op2->value.integer) == 0)
731 rc = ARITH_DIV0;
732 break;
735 mpz_tdiv_q (result->value.integer, op1->value.integer,
736 op2->value.integer);
737 break;
739 case BT_REAL:
740 if (mpfr_sgn (op2->value.real) == 0 && gfc_option.flag_range_check == 1)
742 rc = ARITH_DIV0;
743 break;
746 mpfr_div (result->value.real, op1->value.real, op2->value.real,
747 GFC_RND_MODE);
748 break;
750 case BT_COMPLEX:
751 if (mpc_cmp_si_si (op2->value.complex, 0, 0) == 0
752 && gfc_option.flag_range_check == 1)
754 rc = ARITH_DIV0;
755 break;
758 gfc_set_model (mpc_realref (op1->value.complex));
759 if (mpc_cmp_si_si (op2->value.complex, 0, 0) == 0)
761 /* In Fortran, return (NaN + NaN I) for any zero divisor. See
762 PR 40318. */
763 mpfr_set_nan (mpc_realref (result->value.complex));
764 mpfr_set_nan (mpc_imagref (result->value.complex));
766 else
767 mpc_div (result->value.complex, op1->value.complex, op2->value.complex,
768 GFC_MPC_RND_MODE);
769 break;
771 default:
772 gfc_internal_error ("gfc_arith_divide(): Bad basic type");
775 if (rc == ARITH_OK)
776 rc = gfc_range_check (result);
778 return check_result (rc, op1, result, resultp);
781 /* Raise a number to a power. */
783 static arith
784 arith_power (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
786 int power_sign;
787 gfc_expr *result;
788 arith rc;
790 rc = ARITH_OK;
791 result = gfc_get_constant_expr (op1->ts.type, op1->ts.kind, &op1->where);
793 switch (op2->ts.type)
795 case BT_INTEGER:
796 power_sign = mpz_sgn (op2->value.integer);
798 if (power_sign == 0)
800 /* Handle something to the zeroth power. Since we're dealing
801 with integral exponents, there is no ambiguity in the
802 limiting procedure used to determine the value of 0**0. */
803 switch (op1->ts.type)
805 case BT_INTEGER:
806 mpz_set_ui (result->value.integer, 1);
807 break;
809 case BT_REAL:
810 mpfr_set_ui (result->value.real, 1, GFC_RND_MODE);
811 break;
813 case BT_COMPLEX:
814 mpc_set_ui (result->value.complex, 1, GFC_MPC_RND_MODE);
815 break;
817 default:
818 gfc_internal_error ("arith_power(): Bad base");
821 else
823 switch (op1->ts.type)
825 case BT_INTEGER:
827 int power;
829 /* First, we simplify the cases of op1 == 1, 0 or -1. */
830 if (mpz_cmp_si (op1->value.integer, 1) == 0)
832 /* 1**op2 == 1 */
833 mpz_set_si (result->value.integer, 1);
835 else if (mpz_cmp_si (op1->value.integer, 0) == 0)
837 /* 0**op2 == 0, if op2 > 0
838 0**op2 overflow, if op2 < 0 ; in that case, we
839 set the result to 0 and return ARITH_DIV0. */
840 mpz_set_si (result->value.integer, 0);
841 if (mpz_cmp_si (op2->value.integer, 0) < 0)
842 rc = ARITH_DIV0;
844 else if (mpz_cmp_si (op1->value.integer, -1) == 0)
846 /* (-1)**op2 == (-1)**(mod(op2,2)) */
847 unsigned int odd = mpz_fdiv_ui (op2->value.integer, 2);
848 if (odd)
849 mpz_set_si (result->value.integer, -1);
850 else
851 mpz_set_si (result->value.integer, 1);
853 /* Then, we take care of op2 < 0. */
854 else if (mpz_cmp_si (op2->value.integer, 0) < 0)
856 /* if op2 < 0, op1**op2 == 0 because abs(op1) > 1. */
857 mpz_set_si (result->value.integer, 0);
859 else if (gfc_extract_int (op2, &power) != NULL)
861 /* If op2 doesn't fit in an int, the exponentiation will
862 overflow, because op2 > 0 and abs(op1) > 1. */
863 mpz_t max;
864 int i;
865 i = gfc_validate_kind (BT_INTEGER, result->ts.kind, false);
867 if (gfc_option.flag_range_check)
868 rc = ARITH_OVERFLOW;
870 /* Still, we want to give the same value as the
871 processor. */
872 mpz_init (max);
873 mpz_add_ui (max, gfc_integer_kinds[i].huge, 1);
874 mpz_mul_ui (max, max, 2);
875 mpz_powm (result->value.integer, op1->value.integer,
876 op2->value.integer, max);
877 mpz_clear (max);
879 else
880 mpz_pow_ui (result->value.integer, op1->value.integer,
881 power);
883 break;
885 case BT_REAL:
886 mpfr_pow_z (result->value.real, op1->value.real,
887 op2->value.integer, GFC_RND_MODE);
888 break;
890 case BT_COMPLEX:
891 mpc_pow_z (result->value.complex, op1->value.complex,
892 op2->value.integer, GFC_MPC_RND_MODE);
893 break;
895 default:
896 break;
899 break;
901 case BT_REAL:
903 if (gfc_init_expr_flag)
905 if (gfc_notify_std (GFC_STD_F2003,"Fortran 2003: Noninteger "
906 "exponent in an initialization "
907 "expression at %L", &op2->where) == FAILURE)
908 return ARITH_PROHIBIT;
911 if (mpfr_cmp_si (op1->value.real, 0) < 0)
913 gfc_error ("Raising a negative REAL at %L to "
914 "a REAL power is prohibited", &op1->where);
915 gfc_free (result);
916 return ARITH_PROHIBIT;
919 mpfr_pow (result->value.real, op1->value.real, op2->value.real,
920 GFC_RND_MODE);
921 break;
923 case BT_COMPLEX:
925 if (gfc_init_expr_flag)
927 if (gfc_notify_std (GFC_STD_F2003,"Fortran 2003: Noninteger "
928 "exponent in an initialization "
929 "expression at %L", &op2->where) == FAILURE)
930 return ARITH_PROHIBIT;
933 mpc_pow (result->value.complex, op1->value.complex,
934 op2->value.complex, GFC_MPC_RND_MODE);
936 break;
937 default:
938 gfc_internal_error ("arith_power(): unknown type");
941 if (rc == ARITH_OK)
942 rc = gfc_range_check (result);
944 return check_result (rc, op1, result, resultp);
948 /* Concatenate two string constants. */
950 static arith
951 gfc_arith_concat (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
953 gfc_expr *result;
954 int len;
956 gcc_assert (op1->ts.kind == op2->ts.kind);
957 result = gfc_get_constant_expr (BT_CHARACTER, op1->ts.kind,
958 &op1->where);
960 len = op1->value.character.length + op2->value.character.length;
962 result->value.character.string = gfc_get_wide_string (len + 1);
963 result->value.character.length = len;
965 memcpy (result->value.character.string, op1->value.character.string,
966 op1->value.character.length * sizeof (gfc_char_t));
968 memcpy (&result->value.character.string[op1->value.character.length],
969 op2->value.character.string,
970 op2->value.character.length * sizeof (gfc_char_t));
972 result->value.character.string[len] = '\0';
974 *resultp = result;
976 return ARITH_OK;
979 /* Comparison between real values; returns 0 if (op1 .op. op2) is true.
980 This function mimics mpfr_cmp but takes NaN into account. */
982 static int
983 compare_real (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
985 int rc;
986 switch (op)
988 case INTRINSIC_EQ:
989 rc = mpfr_equal_p (op1->value.real, op2->value.real) ? 0 : 1;
990 break;
991 case INTRINSIC_GT:
992 rc = mpfr_greater_p (op1->value.real, op2->value.real) ? 1 : -1;
993 break;
994 case INTRINSIC_GE:
995 rc = mpfr_greaterequal_p (op1->value.real, op2->value.real) ? 1 : -1;
996 break;
997 case INTRINSIC_LT:
998 rc = mpfr_less_p (op1->value.real, op2->value.real) ? -1 : 1;
999 break;
1000 case INTRINSIC_LE:
1001 rc = mpfr_lessequal_p (op1->value.real, op2->value.real) ? -1 : 1;
1002 break;
1003 default:
1004 gfc_internal_error ("compare_real(): Bad operator");
1007 return rc;
1010 /* Comparison operators. Assumes that the two expression nodes
1011 contain two constants of the same type. The op argument is
1012 needed to handle NaN correctly. */
1015 gfc_compare_expr (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1017 int rc;
1019 switch (op1->ts.type)
1021 case BT_INTEGER:
1022 rc = mpz_cmp (op1->value.integer, op2->value.integer);
1023 break;
1025 case BT_REAL:
1026 rc = compare_real (op1, op2, op);
1027 break;
1029 case BT_CHARACTER:
1030 rc = gfc_compare_string (op1, op2);
1031 break;
1033 case BT_LOGICAL:
1034 rc = ((!op1->value.logical && op2->value.logical)
1035 || (op1->value.logical && !op2->value.logical));
1036 break;
1038 default:
1039 gfc_internal_error ("gfc_compare_expr(): Bad basic type");
1042 return rc;
1046 /* Compare a pair of complex numbers. Naturally, this is only for
1047 equality and inequality. */
1049 static int
1050 compare_complex (gfc_expr *op1, gfc_expr *op2)
1052 return mpc_cmp (op1->value.complex, op2->value.complex) == 0;
1056 /* Given two constant strings and the inverse collating sequence, compare the
1057 strings. We return -1 for a < b, 0 for a == b and 1 for a > b.
1058 We use the processor's default collating sequence. */
1061 gfc_compare_string (gfc_expr *a, gfc_expr *b)
1063 int len, alen, blen, i;
1064 gfc_char_t ac, bc;
1066 alen = a->value.character.length;
1067 blen = b->value.character.length;
1069 len = MAX(alen, blen);
1071 for (i = 0; i < len; i++)
1073 ac = ((i < alen) ? a->value.character.string[i] : ' ');
1074 bc = ((i < blen) ? b->value.character.string[i] : ' ');
1076 if (ac < bc)
1077 return -1;
1078 if (ac > bc)
1079 return 1;
1082 /* Strings are equal */
1083 return 0;
1088 gfc_compare_with_Cstring (gfc_expr *a, const char *b, bool case_sensitive)
1090 int len, alen, blen, i;
1091 gfc_char_t ac, bc;
1093 alen = a->value.character.length;
1094 blen = strlen (b);
1096 len = MAX(alen, blen);
1098 for (i = 0; i < len; i++)
1100 ac = ((i < alen) ? a->value.character.string[i] : ' ');
1101 bc = ((i < blen) ? b[i] : ' ');
1103 if (!case_sensitive)
1105 ac = TOLOWER (ac);
1106 bc = TOLOWER (bc);
1109 if (ac < bc)
1110 return -1;
1111 if (ac > bc)
1112 return 1;
1115 /* Strings are equal */
1116 return 0;
1120 /* Specific comparison subroutines. */
1122 static arith
1123 gfc_arith_eq (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1125 gfc_expr *result;
1127 result = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
1128 &op1->where);
1129 result->value.logical = (op1->ts.type == BT_COMPLEX)
1130 ? compare_complex (op1, op2)
1131 : (gfc_compare_expr (op1, op2, INTRINSIC_EQ) == 0);
1133 *resultp = result;
1134 return ARITH_OK;
1138 static arith
1139 gfc_arith_ne (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1141 gfc_expr *result;
1143 result = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
1144 &op1->where);
1145 result->value.logical = (op1->ts.type == BT_COMPLEX)
1146 ? !compare_complex (op1, op2)
1147 : (gfc_compare_expr (op1, op2, INTRINSIC_EQ) != 0);
1149 *resultp = result;
1150 return ARITH_OK;
1154 static arith
1155 gfc_arith_gt (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1157 gfc_expr *result;
1159 result = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
1160 &op1->where);
1161 result->value.logical = (gfc_compare_expr (op1, op2, INTRINSIC_GT) > 0);
1162 *resultp = result;
1164 return ARITH_OK;
1168 static arith
1169 gfc_arith_ge (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1171 gfc_expr *result;
1173 result = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
1174 &op1->where);
1175 result->value.logical = (gfc_compare_expr (op1, op2, INTRINSIC_GE) >= 0);
1176 *resultp = result;
1178 return ARITH_OK;
1182 static arith
1183 gfc_arith_lt (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1185 gfc_expr *result;
1187 result = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
1188 &op1->where);
1189 result->value.logical = (gfc_compare_expr (op1, op2, INTRINSIC_LT) < 0);
1190 *resultp = result;
1192 return ARITH_OK;
1196 static arith
1197 gfc_arith_le (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1199 gfc_expr *result;
1201 result = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
1202 &op1->where);
1203 result->value.logical = (gfc_compare_expr (op1, op2, INTRINSIC_LE) <= 0);
1204 *resultp = result;
1206 return ARITH_OK;
1210 static arith
1211 reduce_unary (arith (*eval) (gfc_expr *, gfc_expr **), gfc_expr *op,
1212 gfc_expr **result)
1214 gfc_constructor_base head;
1215 gfc_constructor *c;
1216 gfc_expr *r;
1217 arith rc;
1219 if (op->expr_type == EXPR_CONSTANT)
1220 return eval (op, result);
1222 rc = ARITH_OK;
1223 head = gfc_constructor_copy (op->value.constructor);
1224 for (c = gfc_constructor_first (head); c; c = gfc_constructor_next (c))
1226 rc = reduce_unary (eval, c->expr, &r);
1228 if (rc != ARITH_OK)
1229 break;
1231 gfc_replace_expr (c->expr, r);
1234 if (rc != ARITH_OK)
1235 gfc_constructor_free (head);
1236 else
1238 gfc_constructor *c = gfc_constructor_first (head);
1239 r = gfc_get_array_expr (c->expr->ts.type, c->expr->ts.kind,
1240 &op->where);
1241 r->shape = gfc_copy_shape (op->shape, op->rank);
1242 r->rank = op->rank;
1243 r->value.constructor = head;
1244 *result = r;
1247 return rc;
1251 static arith
1252 reduce_binary_ac (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1253 gfc_expr *op1, gfc_expr *op2, gfc_expr **result)
1255 gfc_constructor_base head;
1256 gfc_constructor *c;
1257 gfc_expr *r;
1258 arith rc = ARITH_OK;
1260 head = gfc_constructor_copy (op1->value.constructor);
1261 for (c = gfc_constructor_first (head); c; c = gfc_constructor_next (c))
1263 if (c->expr->expr_type == EXPR_CONSTANT)
1264 rc = eval (c->expr, op2, &r);
1265 else
1266 rc = reduce_binary_ac (eval, c->expr, op2, &r);
1268 if (rc != ARITH_OK)
1269 break;
1271 gfc_replace_expr (c->expr, r);
1274 if (rc != ARITH_OK)
1275 gfc_constructor_free (head);
1276 else
1278 gfc_constructor *c = gfc_constructor_first (head);
1279 r = gfc_get_array_expr (c->expr->ts.type, c->expr->ts.kind,
1280 &op1->where);
1281 r->shape = gfc_copy_shape (op1->shape, op1->rank);
1282 r->rank = op1->rank;
1283 r->value.constructor = head;
1284 *result = r;
1287 return rc;
1291 static arith
1292 reduce_binary_ca (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1293 gfc_expr *op1, gfc_expr *op2, gfc_expr **result)
1295 gfc_constructor_base head;
1296 gfc_constructor *c;
1297 gfc_expr *r;
1298 arith rc = ARITH_OK;
1300 head = gfc_constructor_copy (op2->value.constructor);
1301 for (c = gfc_constructor_first (head); c; c = gfc_constructor_next (c))
1303 if (c->expr->expr_type == EXPR_CONSTANT)
1304 rc = eval (op1, c->expr, &r);
1305 else
1306 rc = reduce_binary_ca (eval, op1, c->expr, &r);
1308 if (rc != ARITH_OK)
1309 break;
1311 gfc_replace_expr (c->expr, r);
1314 if (rc != ARITH_OK)
1315 gfc_constructor_free (head);
1316 else
1318 gfc_constructor *c = gfc_constructor_first (head);
1319 r = gfc_get_array_expr (c->expr->ts.type, c->expr->ts.kind,
1320 &op2->where);
1321 r->shape = gfc_copy_shape (op2->shape, op2->rank);
1322 r->rank = op2->rank;
1323 r->value.constructor = head;
1324 *result = r;
1327 return rc;
1331 /* We need a forward declaration of reduce_binary. */
1332 static arith reduce_binary (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1333 gfc_expr *op1, gfc_expr *op2, gfc_expr **result);
1336 static arith
1337 reduce_binary_aa (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1338 gfc_expr *op1, gfc_expr *op2, gfc_expr **result)
1340 gfc_constructor_base head;
1341 gfc_constructor *c, *d;
1342 gfc_expr *r;
1343 arith rc = ARITH_OK;
1345 if (gfc_check_conformance (op1, op2,
1346 "elemental binary operation") != SUCCESS)
1347 return ARITH_INCOMMENSURATE;
1349 head = gfc_constructor_copy (op1->value.constructor);
1350 for (c = gfc_constructor_first (head),
1351 d = gfc_constructor_first (op2->value.constructor);
1352 c && d;
1353 c = gfc_constructor_next (c), d = gfc_constructor_next (d))
1355 rc = reduce_binary (eval, c->expr, d->expr, &r);
1356 if (rc != ARITH_OK)
1357 break;
1359 gfc_replace_expr (c->expr, r);
1362 if (c || d)
1363 rc = ARITH_INCOMMENSURATE;
1365 if (rc != ARITH_OK)
1366 gfc_constructor_free (head);
1367 else
1369 gfc_constructor *c = gfc_constructor_first (head);
1370 r = gfc_get_array_expr (c->expr->ts.type, c->expr->ts.kind,
1371 &op1->where);
1372 r->shape = gfc_copy_shape (op1->shape, op1->rank);
1373 r->rank = op1->rank;
1374 r->value.constructor = head;
1375 *result = r;
1378 return rc;
1382 static arith
1383 reduce_binary (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1384 gfc_expr *op1, gfc_expr *op2, gfc_expr **result)
1386 if (op1->expr_type == EXPR_CONSTANT && op2->expr_type == EXPR_CONSTANT)
1387 return eval (op1, op2, result);
1389 if (op1->expr_type == EXPR_CONSTANT && op2->expr_type == EXPR_ARRAY)
1390 return reduce_binary_ca (eval, op1, op2, result);
1392 if (op1->expr_type == EXPR_ARRAY && op2->expr_type == EXPR_CONSTANT)
1393 return reduce_binary_ac (eval, op1, op2, result);
1395 return reduce_binary_aa (eval, op1, op2, result);
1399 typedef union
1401 arith (*f2)(gfc_expr *, gfc_expr **);
1402 arith (*f3)(gfc_expr *, gfc_expr *, gfc_expr **);
1404 eval_f;
1406 /* High level arithmetic subroutines. These subroutines go into
1407 eval_intrinsic(), which can do one of several things to its
1408 operands. If the operands are incompatible with the intrinsic
1409 operation, we return a node pointing to the operands and hope that
1410 an operator interface is found during resolution.
1412 If the operands are compatible and are constants, then we try doing
1413 the arithmetic. We also handle the cases where either or both
1414 operands are array constructors. */
1416 static gfc_expr *
1417 eval_intrinsic (gfc_intrinsic_op op,
1418 eval_f eval, gfc_expr *op1, gfc_expr *op2)
1420 gfc_expr temp, *result;
1421 int unary;
1422 arith rc;
1424 gfc_clear_ts (&temp.ts);
1426 switch (op)
1428 /* Logical unary */
1429 case INTRINSIC_NOT:
1430 if (op1->ts.type != BT_LOGICAL)
1431 goto runtime;
1433 temp.ts.type = BT_LOGICAL;
1434 temp.ts.kind = gfc_default_logical_kind;
1435 unary = 1;
1436 break;
1438 /* Logical binary operators */
1439 case INTRINSIC_OR:
1440 case INTRINSIC_AND:
1441 case INTRINSIC_NEQV:
1442 case INTRINSIC_EQV:
1443 if (op1->ts.type != BT_LOGICAL || op2->ts.type != BT_LOGICAL)
1444 goto runtime;
1446 temp.ts.type = BT_LOGICAL;
1447 temp.ts.kind = gfc_default_logical_kind;
1448 unary = 0;
1449 break;
1451 /* Numeric unary */
1452 case INTRINSIC_UPLUS:
1453 case INTRINSIC_UMINUS:
1454 if (!gfc_numeric_ts (&op1->ts))
1455 goto runtime;
1457 temp.ts = op1->ts;
1458 unary = 1;
1459 break;
1461 case INTRINSIC_PARENTHESES:
1462 temp.ts = op1->ts;
1463 unary = 1;
1464 break;
1466 /* Additional restrictions for ordering relations. */
1467 case INTRINSIC_GE:
1468 case INTRINSIC_GE_OS:
1469 case INTRINSIC_LT:
1470 case INTRINSIC_LT_OS:
1471 case INTRINSIC_LE:
1472 case INTRINSIC_LE_OS:
1473 case INTRINSIC_GT:
1474 case INTRINSIC_GT_OS:
1475 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
1477 temp.ts.type = BT_LOGICAL;
1478 temp.ts.kind = gfc_default_logical_kind;
1479 goto runtime;
1482 /* Fall through */
1483 case INTRINSIC_EQ:
1484 case INTRINSIC_EQ_OS:
1485 case INTRINSIC_NE:
1486 case INTRINSIC_NE_OS:
1487 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
1489 unary = 0;
1490 temp.ts.type = BT_LOGICAL;
1491 temp.ts.kind = gfc_default_logical_kind;
1493 /* If kind mismatch, exit and we'll error out later. */
1494 if (op1->ts.kind != op2->ts.kind)
1495 goto runtime;
1497 break;
1500 /* Fall through */
1501 /* Numeric binary */
1502 case INTRINSIC_PLUS:
1503 case INTRINSIC_MINUS:
1504 case INTRINSIC_TIMES:
1505 case INTRINSIC_DIVIDE:
1506 case INTRINSIC_POWER:
1507 if (!gfc_numeric_ts (&op1->ts) || !gfc_numeric_ts (&op2->ts))
1508 goto runtime;
1510 /* Insert any necessary type conversions to make the operands
1511 compatible. */
1513 temp.expr_type = EXPR_OP;
1514 gfc_clear_ts (&temp.ts);
1515 temp.value.op.op = op;
1517 temp.value.op.op1 = op1;
1518 temp.value.op.op2 = op2;
1520 gfc_type_convert_binary (&temp, 0);
1522 if (op == INTRINSIC_EQ || op == INTRINSIC_NE
1523 || op == INTRINSIC_GE || op == INTRINSIC_GT
1524 || op == INTRINSIC_LE || op == INTRINSIC_LT
1525 || op == INTRINSIC_EQ_OS || op == INTRINSIC_NE_OS
1526 || op == INTRINSIC_GE_OS || op == INTRINSIC_GT_OS
1527 || op == INTRINSIC_LE_OS || op == INTRINSIC_LT_OS)
1529 temp.ts.type = BT_LOGICAL;
1530 temp.ts.kind = gfc_default_logical_kind;
1533 unary = 0;
1534 break;
1536 /* Character binary */
1537 case INTRINSIC_CONCAT:
1538 if (op1->ts.type != BT_CHARACTER || op2->ts.type != BT_CHARACTER
1539 || op1->ts.kind != op2->ts.kind)
1540 goto runtime;
1542 temp.ts.type = BT_CHARACTER;
1543 temp.ts.kind = op1->ts.kind;
1544 unary = 0;
1545 break;
1547 case INTRINSIC_USER:
1548 goto runtime;
1550 default:
1551 gfc_internal_error ("eval_intrinsic(): Bad operator");
1554 if (op1->expr_type != EXPR_CONSTANT
1555 && (op1->expr_type != EXPR_ARRAY
1556 || !gfc_is_constant_expr (op1) || !gfc_expanded_ac (op1)))
1557 goto runtime;
1559 if (op2 != NULL
1560 && op2->expr_type != EXPR_CONSTANT
1561 && (op2->expr_type != EXPR_ARRAY
1562 || !gfc_is_constant_expr (op2) || !gfc_expanded_ac (op2)))
1563 goto runtime;
1565 if (unary)
1566 rc = reduce_unary (eval.f2, op1, &result);
1567 else
1568 rc = reduce_binary (eval.f3, op1, op2, &result);
1571 /* Something went wrong. */
1572 if (op == INTRINSIC_POWER && rc == ARITH_PROHIBIT)
1573 return NULL;
1575 if (rc != ARITH_OK)
1577 gfc_error (gfc_arith_error (rc), &op1->where);
1578 return NULL;
1581 gfc_free_expr (op1);
1582 gfc_free_expr (op2);
1583 return result;
1585 runtime:
1586 /* Create a run-time expression. */
1587 result = gfc_get_operator_expr (&op1->where, op, op1, op2);
1588 result->ts = temp.ts;
1590 return result;
1594 /* Modify type of expression for zero size array. */
1596 static gfc_expr *
1597 eval_type_intrinsic0 (gfc_intrinsic_op iop, gfc_expr *op)
1599 if (op == NULL)
1600 gfc_internal_error ("eval_type_intrinsic0(): op NULL");
1602 switch (iop)
1604 case INTRINSIC_GE:
1605 case INTRINSIC_GE_OS:
1606 case INTRINSIC_LT:
1607 case INTRINSIC_LT_OS:
1608 case INTRINSIC_LE:
1609 case INTRINSIC_LE_OS:
1610 case INTRINSIC_GT:
1611 case INTRINSIC_GT_OS:
1612 case INTRINSIC_EQ:
1613 case INTRINSIC_EQ_OS:
1614 case INTRINSIC_NE:
1615 case INTRINSIC_NE_OS:
1616 op->ts.type = BT_LOGICAL;
1617 op->ts.kind = gfc_default_logical_kind;
1618 break;
1620 default:
1621 break;
1624 return op;
1628 /* Return nonzero if the expression is a zero size array. */
1630 static int
1631 gfc_zero_size_array (gfc_expr *e)
1633 if (e->expr_type != EXPR_ARRAY)
1634 return 0;
1636 return e->value.constructor == NULL;
1640 /* Reduce a binary expression where at least one of the operands
1641 involves a zero-length array. Returns NULL if neither of the
1642 operands is a zero-length array. */
1644 static gfc_expr *
1645 reduce_binary0 (gfc_expr *op1, gfc_expr *op2)
1647 if (gfc_zero_size_array (op1))
1649 gfc_free_expr (op2);
1650 return op1;
1653 if (gfc_zero_size_array (op2))
1655 gfc_free_expr (op1);
1656 return op2;
1659 return NULL;
1663 static gfc_expr *
1664 eval_intrinsic_f2 (gfc_intrinsic_op op,
1665 arith (*eval) (gfc_expr *, gfc_expr **),
1666 gfc_expr *op1, gfc_expr *op2)
1668 gfc_expr *result;
1669 eval_f f;
1671 if (op2 == NULL)
1673 if (gfc_zero_size_array (op1))
1674 return eval_type_intrinsic0 (op, op1);
1676 else
1678 result = reduce_binary0 (op1, op2);
1679 if (result != NULL)
1680 return eval_type_intrinsic0 (op, result);
1683 f.f2 = eval;
1684 return eval_intrinsic (op, f, op1, op2);
1688 static gfc_expr *
1689 eval_intrinsic_f3 (gfc_intrinsic_op op,
1690 arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1691 gfc_expr *op1, gfc_expr *op2)
1693 gfc_expr *result;
1694 eval_f f;
1696 result = reduce_binary0 (op1, op2);
1697 if (result != NULL)
1698 return eval_type_intrinsic0(op, result);
1700 f.f3 = eval;
1701 return eval_intrinsic (op, f, op1, op2);
1705 gfc_expr *
1706 gfc_parentheses (gfc_expr *op)
1708 if (gfc_is_constant_expr (op))
1709 return op;
1711 return eval_intrinsic_f2 (INTRINSIC_PARENTHESES, gfc_arith_identity,
1712 op, NULL);
1715 gfc_expr *
1716 gfc_uplus (gfc_expr *op)
1718 return eval_intrinsic_f2 (INTRINSIC_UPLUS, gfc_arith_identity, op, NULL);
1722 gfc_expr *
1723 gfc_uminus (gfc_expr *op)
1725 return eval_intrinsic_f2 (INTRINSIC_UMINUS, gfc_arith_uminus, op, NULL);
1729 gfc_expr *
1730 gfc_add (gfc_expr *op1, gfc_expr *op2)
1732 return eval_intrinsic_f3 (INTRINSIC_PLUS, gfc_arith_plus, op1, op2);
1736 gfc_expr *
1737 gfc_subtract (gfc_expr *op1, gfc_expr *op2)
1739 return eval_intrinsic_f3 (INTRINSIC_MINUS, gfc_arith_minus, op1, op2);
1743 gfc_expr *
1744 gfc_multiply (gfc_expr *op1, gfc_expr *op2)
1746 return eval_intrinsic_f3 (INTRINSIC_TIMES, gfc_arith_times, op1, op2);
1750 gfc_expr *
1751 gfc_divide (gfc_expr *op1, gfc_expr *op2)
1753 return eval_intrinsic_f3 (INTRINSIC_DIVIDE, gfc_arith_divide, op1, op2);
1757 gfc_expr *
1758 gfc_power (gfc_expr *op1, gfc_expr *op2)
1760 return eval_intrinsic_f3 (INTRINSIC_POWER, arith_power, op1, op2);
1764 gfc_expr *
1765 gfc_concat (gfc_expr *op1, gfc_expr *op2)
1767 return eval_intrinsic_f3 (INTRINSIC_CONCAT, gfc_arith_concat, op1, op2);
1771 gfc_expr *
1772 gfc_and (gfc_expr *op1, gfc_expr *op2)
1774 return eval_intrinsic_f3 (INTRINSIC_AND, gfc_arith_and, op1, op2);
1778 gfc_expr *
1779 gfc_or (gfc_expr *op1, gfc_expr *op2)
1781 return eval_intrinsic_f3 (INTRINSIC_OR, gfc_arith_or, op1, op2);
1785 gfc_expr *
1786 gfc_not (gfc_expr *op1)
1788 return eval_intrinsic_f2 (INTRINSIC_NOT, gfc_arith_not, op1, NULL);
1792 gfc_expr *
1793 gfc_eqv (gfc_expr *op1, gfc_expr *op2)
1795 return eval_intrinsic_f3 (INTRINSIC_EQV, gfc_arith_eqv, op1, op2);
1799 gfc_expr *
1800 gfc_neqv (gfc_expr *op1, gfc_expr *op2)
1802 return eval_intrinsic_f3 (INTRINSIC_NEQV, gfc_arith_neqv, op1, op2);
1806 gfc_expr *
1807 gfc_eq (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1809 return eval_intrinsic_f3 (op, gfc_arith_eq, op1, op2);
1813 gfc_expr *
1814 gfc_ne (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1816 return eval_intrinsic_f3 (op, gfc_arith_ne, op1, op2);
1820 gfc_expr *
1821 gfc_gt (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1823 return eval_intrinsic_f3 (op, gfc_arith_gt, op1, op2);
1827 gfc_expr *
1828 gfc_ge (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1830 return eval_intrinsic_f3 (op, gfc_arith_ge, op1, op2);
1834 gfc_expr *
1835 gfc_lt (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1837 return eval_intrinsic_f3 (op, gfc_arith_lt, op1, op2);
1841 gfc_expr *
1842 gfc_le (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1844 return eval_intrinsic_f3 (op, gfc_arith_le, op1, op2);
1848 /* Convert an integer string to an expression node. */
1850 gfc_expr *
1851 gfc_convert_integer (const char *buffer, int kind, int radix, locus *where)
1853 gfc_expr *e;
1854 const char *t;
1856 e = gfc_get_constant_expr (BT_INTEGER, kind, where);
1857 /* A leading plus is allowed, but not by mpz_set_str. */
1858 if (buffer[0] == '+')
1859 t = buffer + 1;
1860 else
1861 t = buffer;
1862 mpz_set_str (e->value.integer, t, radix);
1864 return e;
1868 /* Convert a real string to an expression node. */
1870 gfc_expr *
1871 gfc_convert_real (const char *buffer, int kind, locus *where)
1873 gfc_expr *e;
1875 e = gfc_get_constant_expr (BT_REAL, kind, where);
1876 mpfr_set_str (e->value.real, buffer, 10, GFC_RND_MODE);
1878 return e;
1882 /* Convert a pair of real, constant expression nodes to a single
1883 complex expression node. */
1885 gfc_expr *
1886 gfc_convert_complex (gfc_expr *real, gfc_expr *imag, int kind)
1888 gfc_expr *e;
1890 e = gfc_get_constant_expr (BT_COMPLEX, kind, &real->where);
1891 mpc_set_fr_fr (e->value.complex, real->value.real, imag->value.real,
1892 GFC_MPC_RND_MODE);
1894 return e;
1898 /******* Simplification of intrinsic functions with constant arguments *****/
1901 /* Deal with an arithmetic error. */
1903 static void
1904 arith_error (arith rc, gfc_typespec *from, gfc_typespec *to, locus *where)
1906 switch (rc)
1908 case ARITH_OK:
1909 gfc_error ("Arithmetic OK converting %s to %s at %L",
1910 gfc_typename (from), gfc_typename (to), where);
1911 break;
1912 case ARITH_OVERFLOW:
1913 gfc_error ("Arithmetic overflow converting %s to %s at %L. This check "
1914 "can be disabled with the option -fno-range-check",
1915 gfc_typename (from), gfc_typename (to), where);
1916 break;
1917 case ARITH_UNDERFLOW:
1918 gfc_error ("Arithmetic underflow converting %s to %s at %L. This check "
1919 "can be disabled with the option -fno-range-check",
1920 gfc_typename (from), gfc_typename (to), where);
1921 break;
1922 case ARITH_NAN:
1923 gfc_error ("Arithmetic NaN converting %s to %s at %L. This check "
1924 "can be disabled with the option -fno-range-check",
1925 gfc_typename (from), gfc_typename (to), where);
1926 break;
1927 case ARITH_DIV0:
1928 gfc_error ("Division by zero converting %s to %s at %L",
1929 gfc_typename (from), gfc_typename (to), where);
1930 break;
1931 case ARITH_INCOMMENSURATE:
1932 gfc_error ("Array operands are incommensurate converting %s to %s at %L",
1933 gfc_typename (from), gfc_typename (to), where);
1934 break;
1935 case ARITH_ASYMMETRIC:
1936 gfc_error ("Integer outside symmetric range implied by Standard Fortran"
1937 " converting %s to %s at %L",
1938 gfc_typename (from), gfc_typename (to), where);
1939 break;
1940 default:
1941 gfc_internal_error ("gfc_arith_error(): Bad error code");
1944 /* TODO: Do something about the error, i.e., throw exception, return
1945 NaN, etc. */
1949 /* Convert integers to integers. */
1951 gfc_expr *
1952 gfc_int2int (gfc_expr *src, int kind)
1954 gfc_expr *result;
1955 arith rc;
1957 result = gfc_get_constant_expr (BT_INTEGER, kind, &src->where);
1959 mpz_set (result->value.integer, src->value.integer);
1961 if ((rc = gfc_check_integer_range (result->value.integer, kind)) != ARITH_OK)
1963 if (rc == ARITH_ASYMMETRIC)
1965 gfc_warning (gfc_arith_error (rc), &src->where);
1967 else
1969 arith_error (rc, &src->ts, &result->ts, &src->where);
1970 gfc_free_expr (result);
1971 return NULL;
1975 return result;
1979 /* Convert integers to reals. */
1981 gfc_expr *
1982 gfc_int2real (gfc_expr *src, int kind)
1984 gfc_expr *result;
1985 arith rc;
1987 result = gfc_get_constant_expr (BT_REAL, kind, &src->where);
1989 mpfr_set_z (result->value.real, src->value.integer, GFC_RND_MODE);
1991 if ((rc = gfc_check_real_range (result->value.real, kind)) != ARITH_OK)
1993 arith_error (rc, &src->ts, &result->ts, &src->where);
1994 gfc_free_expr (result);
1995 return NULL;
1998 return result;
2002 /* Convert default integer to default complex. */
2004 gfc_expr *
2005 gfc_int2complex (gfc_expr *src, int kind)
2007 gfc_expr *result;
2008 arith rc;
2010 result = gfc_get_constant_expr (BT_COMPLEX, kind, &src->where);
2012 mpc_set_z (result->value.complex, src->value.integer, GFC_MPC_RND_MODE);
2014 if ((rc = gfc_check_real_range (mpc_realref (result->value.complex), kind))
2015 != ARITH_OK)
2017 arith_error (rc, &src->ts, &result->ts, &src->where);
2018 gfc_free_expr (result);
2019 return NULL;
2022 return result;
2026 /* Convert default real to default integer. */
2028 gfc_expr *
2029 gfc_real2int (gfc_expr *src, int kind)
2031 gfc_expr *result;
2032 arith rc;
2034 result = gfc_get_constant_expr (BT_INTEGER, kind, &src->where);
2036 gfc_mpfr_to_mpz (result->value.integer, src->value.real, &src->where);
2038 if ((rc = gfc_check_integer_range (result->value.integer, kind)) != ARITH_OK)
2040 arith_error (rc, &src->ts, &result->ts, &src->where);
2041 gfc_free_expr (result);
2042 return NULL;
2045 return result;
2049 /* Convert real to real. */
2051 gfc_expr *
2052 gfc_real2real (gfc_expr *src, int kind)
2054 gfc_expr *result;
2055 arith rc;
2057 result = gfc_get_constant_expr (BT_REAL, kind, &src->where);
2059 mpfr_set (result->value.real, src->value.real, GFC_RND_MODE);
2061 rc = gfc_check_real_range (result->value.real, kind);
2063 if (rc == ARITH_UNDERFLOW)
2065 if (gfc_option.warn_underflow)
2066 gfc_warning (gfc_arith_error (rc), &src->where);
2067 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
2069 else if (rc != ARITH_OK)
2071 arith_error (rc, &src->ts, &result->ts, &src->where);
2072 gfc_free_expr (result);
2073 return NULL;
2076 return result;
2080 /* Convert real to complex. */
2082 gfc_expr *
2083 gfc_real2complex (gfc_expr *src, int kind)
2085 gfc_expr *result;
2086 arith rc;
2088 result = gfc_get_constant_expr (BT_COMPLEX, kind, &src->where);
2090 mpc_set_fr (result->value.complex, src->value.real, GFC_MPC_RND_MODE);
2092 rc = gfc_check_real_range (mpc_realref (result->value.complex), kind);
2094 if (rc == ARITH_UNDERFLOW)
2096 if (gfc_option.warn_underflow)
2097 gfc_warning (gfc_arith_error (rc), &src->where);
2098 mpfr_set_ui (mpc_realref (result->value.complex), 0, GFC_RND_MODE);
2100 else if (rc != ARITH_OK)
2102 arith_error (rc, &src->ts, &result->ts, &src->where);
2103 gfc_free_expr (result);
2104 return NULL;
2107 return result;
2111 /* Convert complex to integer. */
2113 gfc_expr *
2114 gfc_complex2int (gfc_expr *src, int kind)
2116 gfc_expr *result;
2117 arith rc;
2119 result = gfc_get_constant_expr (BT_INTEGER, kind, &src->where);
2121 gfc_mpfr_to_mpz (result->value.integer, mpc_realref (src->value.complex),
2122 &src->where);
2124 if ((rc = gfc_check_integer_range (result->value.integer, kind)) != ARITH_OK)
2126 arith_error (rc, &src->ts, &result->ts, &src->where);
2127 gfc_free_expr (result);
2128 return NULL;
2131 return result;
2135 /* Convert complex to real. */
2137 gfc_expr *
2138 gfc_complex2real (gfc_expr *src, int kind)
2140 gfc_expr *result;
2141 arith rc;
2143 result = gfc_get_constant_expr (BT_REAL, kind, &src->where);
2145 mpc_real (result->value.real, src->value.complex, GFC_RND_MODE);
2147 rc = gfc_check_real_range (result->value.real, kind);
2149 if (rc == ARITH_UNDERFLOW)
2151 if (gfc_option.warn_underflow)
2152 gfc_warning (gfc_arith_error (rc), &src->where);
2153 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
2155 if (rc != ARITH_OK)
2157 arith_error (rc, &src->ts, &result->ts, &src->where);
2158 gfc_free_expr (result);
2159 return NULL;
2162 return result;
2166 /* Convert complex to complex. */
2168 gfc_expr *
2169 gfc_complex2complex (gfc_expr *src, int kind)
2171 gfc_expr *result;
2172 arith rc;
2174 result = gfc_get_constant_expr (BT_COMPLEX, kind, &src->where);
2176 mpc_set (result->value.complex, src->value.complex, GFC_MPC_RND_MODE);
2178 rc = gfc_check_real_range (mpc_realref (result->value.complex), kind);
2180 if (rc == ARITH_UNDERFLOW)
2182 if (gfc_option.warn_underflow)
2183 gfc_warning (gfc_arith_error (rc), &src->where);
2184 mpfr_set_ui (mpc_realref (result->value.complex), 0, GFC_RND_MODE);
2186 else if (rc != ARITH_OK)
2188 arith_error (rc, &src->ts, &result->ts, &src->where);
2189 gfc_free_expr (result);
2190 return NULL;
2193 rc = gfc_check_real_range (mpc_imagref (result->value.complex), 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 (mpc_imagref (result->value.complex), 0, GFC_RND_MODE);
2201 else if (rc != ARITH_OK)
2203 arith_error (rc, &src->ts, &result->ts, &src->where);
2204 gfc_free_expr (result);
2205 return NULL;
2208 return result;
2212 /* Logical kind conversion. */
2214 gfc_expr *
2215 gfc_log2log (gfc_expr *src, int kind)
2217 gfc_expr *result;
2219 result = gfc_get_constant_expr (BT_LOGICAL, kind, &src->where);
2220 result->value.logical = src->value.logical;
2222 return result;
2226 /* Convert logical to integer. */
2228 gfc_expr *
2229 gfc_log2int (gfc_expr *src, int kind)
2231 gfc_expr *result;
2233 result = gfc_get_constant_expr (BT_INTEGER, kind, &src->where);
2234 mpz_set_si (result->value.integer, src->value.logical);
2236 return result;
2240 /* Convert integer to logical. */
2242 gfc_expr *
2243 gfc_int2log (gfc_expr *src, int kind)
2245 gfc_expr *result;
2247 result = gfc_get_constant_expr (BT_LOGICAL, kind, &src->where);
2248 result->value.logical = (mpz_cmp_si (src->value.integer, 0) != 0);
2250 return result;
2254 /* Helper function to set the representation in a Hollerith conversion.
2255 This assumes that the ts.type and ts.kind of the result have already
2256 been set. */
2258 static void
2259 hollerith2representation (gfc_expr *result, gfc_expr *src)
2261 int src_len, result_len;
2263 src_len = src->representation.length - src->ts.u.pad;
2264 result_len = gfc_target_expr_size (result);
2266 if (src_len > result_len)
2268 gfc_warning ("The Hollerith constant at %L is too long to convert to %s",
2269 &src->where, gfc_typename(&result->ts));
2272 result->representation.string = XCNEWVEC (char, result_len + 1);
2273 memcpy (result->representation.string, src->representation.string,
2274 MIN (result_len, src_len));
2276 if (src_len < result_len)
2277 memset (&result->representation.string[src_len], ' ', result_len - src_len);
2279 result->representation.string[result_len] = '\0'; /* For debugger */
2280 result->representation.length = result_len;
2284 /* Convert Hollerith to integer. The constant will be padded or truncated. */
2286 gfc_expr *
2287 gfc_hollerith2int (gfc_expr *src, int kind)
2289 gfc_expr *result;
2290 result = gfc_get_constant_expr (BT_INTEGER, kind, &src->where);
2292 hollerith2representation (result, src);
2293 gfc_interpret_integer (kind, (unsigned char *) result->representation.string,
2294 result->representation.length, result->value.integer);
2296 return result;
2300 /* Convert Hollerith to real. The constant will be padded or truncated. */
2302 gfc_expr *
2303 gfc_hollerith2real (gfc_expr *src, int kind)
2305 gfc_expr *result;
2306 result = gfc_get_constant_expr (BT_REAL, kind, &src->where);
2308 hollerith2representation (result, src);
2309 gfc_interpret_float (kind, (unsigned char *) result->representation.string,
2310 result->representation.length, result->value.real);
2312 return result;
2316 /* Convert Hollerith to complex. The constant will be padded or truncated. */
2318 gfc_expr *
2319 gfc_hollerith2complex (gfc_expr *src, int kind)
2321 gfc_expr *result;
2322 result = gfc_get_constant_expr (BT_COMPLEX, kind, &src->where);
2324 hollerith2representation (result, src);
2325 gfc_interpret_complex (kind, (unsigned char *) result->representation.string,
2326 result->representation.length, result->value.complex);
2328 return result;
2332 /* Convert Hollerith to character. */
2334 gfc_expr *
2335 gfc_hollerith2character (gfc_expr *src, int kind)
2337 gfc_expr *result;
2339 result = gfc_copy_expr (src);
2340 result->ts.type = BT_CHARACTER;
2341 result->ts.kind = kind;
2343 result->value.character.length = result->representation.length;
2344 result->value.character.string
2345 = gfc_char_to_widechar (result->representation.string);
2347 return result;
2351 /* Convert Hollerith to logical. The constant will be padded or truncated. */
2353 gfc_expr *
2354 gfc_hollerith2logical (gfc_expr *src, int kind)
2356 gfc_expr *result;
2357 result = gfc_get_constant_expr (BT_LOGICAL, kind, &src->where);
2359 hollerith2representation (result, src);
2360 gfc_interpret_logical (kind, (unsigned char *) result->representation.string,
2361 result->representation.length, &result->value.logical);
2363 return result;