Remove outermost loop parameter.
[official-gcc/graphite-test-results.git] / gcc / fortran / arith.c
blob1e90584be49819344abbec273352b57ce9f2b5b6
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);
266 /* Given a wide character value and a character kind, determine whether
267 the character is representable for that kind. */
268 bool
269 gfc_check_character_range (gfc_char_t c, int kind)
271 /* As wide characters are stored as 32-bit values, they're all
272 representable in UCS=4. */
273 if (kind == 4)
274 return true;
276 if (kind == 1)
277 return c <= 255 ? true : false;
279 gcc_unreachable ();
283 /* Given an integer and a kind, make sure that the integer lies within
284 the range of the kind. Returns ARITH_OK, ARITH_ASYMMETRIC or
285 ARITH_OVERFLOW. */
287 arith
288 gfc_check_integer_range (mpz_t p, int kind)
290 arith result;
291 int i;
293 i = gfc_validate_kind (BT_INTEGER, kind, false);
294 result = ARITH_OK;
296 if (pedantic)
298 if (mpz_cmp (p, gfc_integer_kinds[i].pedantic_min_int) < 0)
299 result = ARITH_ASYMMETRIC;
303 if (gfc_option.flag_range_check == 0)
304 return result;
306 if (mpz_cmp (p, gfc_integer_kinds[i].min_int) < 0
307 || mpz_cmp (p, gfc_integer_kinds[i].huge) > 0)
308 result = ARITH_OVERFLOW;
310 return result;
314 /* Given a real and a kind, make sure that the real lies within the
315 range of the kind. Returns ARITH_OK, ARITH_OVERFLOW or
316 ARITH_UNDERFLOW. */
318 static arith
319 gfc_check_real_range (mpfr_t p, int kind)
321 arith retval;
322 mpfr_t q;
323 int i;
325 i = gfc_validate_kind (BT_REAL, kind, false);
327 gfc_set_model (p);
328 mpfr_init (q);
329 mpfr_abs (q, p, GFC_RND_MODE);
331 retval = ARITH_OK;
333 if (mpfr_inf_p (p))
335 if (gfc_option.flag_range_check != 0)
336 retval = ARITH_OVERFLOW;
338 else if (mpfr_nan_p (p))
340 if (gfc_option.flag_range_check != 0)
341 retval = ARITH_NAN;
343 else if (mpfr_sgn (q) == 0)
345 mpfr_clear (q);
346 return retval;
348 else if (mpfr_cmp (q, gfc_real_kinds[i].huge) > 0)
350 if (gfc_option.flag_range_check == 0)
351 mpfr_set_inf (p, mpfr_sgn (p));
352 else
353 retval = ARITH_OVERFLOW;
355 else if (mpfr_cmp (q, gfc_real_kinds[i].subnormal) < 0)
357 if (gfc_option.flag_range_check == 0)
359 if (mpfr_sgn (p) < 0)
361 mpfr_set_ui (p, 0, GFC_RND_MODE);
362 mpfr_set_si (q, -1, GFC_RND_MODE);
363 mpfr_copysign (p, p, q, GFC_RND_MODE);
365 else
366 mpfr_set_ui (p, 0, GFC_RND_MODE);
368 else
369 retval = ARITH_UNDERFLOW;
371 else if (mpfr_cmp (q, gfc_real_kinds[i].tiny) < 0)
373 mp_exp_t emin, emax;
374 int en;
376 /* Save current values of emin and emax. */
377 emin = mpfr_get_emin ();
378 emax = mpfr_get_emax ();
380 /* Set emin and emax for the current model number. */
381 en = gfc_real_kinds[i].min_exponent - gfc_real_kinds[i].digits + 1;
382 mpfr_set_emin ((mp_exp_t) en);
383 mpfr_set_emax ((mp_exp_t) gfc_real_kinds[i].max_exponent);
384 mpfr_check_range (q, 0, GFC_RND_MODE);
385 mpfr_subnormalize (q, 0, GFC_RND_MODE);
387 /* Reset emin and emax. */
388 mpfr_set_emin (emin);
389 mpfr_set_emax (emax);
391 /* Copy sign if needed. */
392 if (mpfr_sgn (p) < 0)
393 mpfr_neg (p, q, GMP_RNDN);
394 else
395 mpfr_set (p, q, GMP_RNDN);
398 mpfr_clear (q);
400 return retval;
404 /* Low-level arithmetic functions. All of these subroutines assume
405 that all operands are of the same type and return an operand of the
406 same type. The other thing about these subroutines is that they
407 can fail in various ways -- overflow, underflow, division by zero,
408 zero raised to the zero, etc. */
410 static arith
411 gfc_arith_not (gfc_expr *op1, gfc_expr **resultp)
413 gfc_expr *result;
415 result = gfc_get_constant_expr (BT_LOGICAL, op1->ts.kind, &op1->where);
416 result->value.logical = !op1->value.logical;
417 *resultp = result;
419 return ARITH_OK;
423 static arith
424 gfc_arith_and (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
426 gfc_expr *result;
428 result = gfc_get_constant_expr (BT_LOGICAL, gfc_kind_max (op1, op2),
429 &op1->where);
430 result->value.logical = op1->value.logical && op2->value.logical;
431 *resultp = result;
433 return ARITH_OK;
437 static arith
438 gfc_arith_or (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
440 gfc_expr *result;
442 result = gfc_get_constant_expr (BT_LOGICAL, gfc_kind_max (op1, op2),
443 &op1->where);
444 result->value.logical = op1->value.logical || op2->value.logical;
445 *resultp = result;
447 return ARITH_OK;
451 static arith
452 gfc_arith_eqv (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
454 gfc_expr *result;
456 result = gfc_get_constant_expr (BT_LOGICAL, gfc_kind_max (op1, op2),
457 &op1->where);
458 result->value.logical = op1->value.logical == op2->value.logical;
459 *resultp = result;
461 return ARITH_OK;
465 static arith
466 gfc_arith_neqv (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
468 gfc_expr *result;
470 result = gfc_get_constant_expr (BT_LOGICAL, gfc_kind_max (op1, op2),
471 &op1->where);
472 result->value.logical = op1->value.logical != op2->value.logical;
473 *resultp = result;
475 return ARITH_OK;
479 /* Make sure a constant numeric expression is within the range for
480 its type and kind. Note that there's also a gfc_check_range(),
481 but that one deals with the intrinsic RANGE function. */
483 arith
484 gfc_range_check (gfc_expr *e)
486 arith rc;
487 arith rc2;
489 switch (e->ts.type)
491 case BT_INTEGER:
492 rc = gfc_check_integer_range (e->value.integer, e->ts.kind);
493 break;
495 case BT_REAL:
496 rc = gfc_check_real_range (e->value.real, e->ts.kind);
497 if (rc == ARITH_UNDERFLOW)
498 mpfr_set_ui (e->value.real, 0, GFC_RND_MODE);
499 if (rc == ARITH_OVERFLOW)
500 mpfr_set_inf (e->value.real, mpfr_sgn (e->value.real));
501 if (rc == ARITH_NAN)
502 mpfr_set_nan (e->value.real);
503 break;
505 case BT_COMPLEX:
506 rc = gfc_check_real_range (mpc_realref (e->value.complex), e->ts.kind);
507 if (rc == ARITH_UNDERFLOW)
508 mpfr_set_ui (mpc_realref (e->value.complex), 0, GFC_RND_MODE);
509 if (rc == ARITH_OVERFLOW)
510 mpfr_set_inf (mpc_realref (e->value.complex),
511 mpfr_sgn (mpc_realref (e->value.complex)));
512 if (rc == ARITH_NAN)
513 mpfr_set_nan (mpc_realref (e->value.complex));
515 rc2 = gfc_check_real_range (mpc_imagref (e->value.complex), e->ts.kind);
516 if (rc == ARITH_UNDERFLOW)
517 mpfr_set_ui (mpc_imagref (e->value.complex), 0, GFC_RND_MODE);
518 if (rc == ARITH_OVERFLOW)
519 mpfr_set_inf (mpc_imagref (e->value.complex),
520 mpfr_sgn (mpc_imagref (e->value.complex)));
521 if (rc == ARITH_NAN)
522 mpfr_set_nan (mpc_imagref (e->value.complex));
524 if (rc == ARITH_OK)
525 rc = rc2;
526 break;
528 default:
529 gfc_internal_error ("gfc_range_check(): Bad type");
532 return rc;
536 /* Several of the following routines use the same set of statements to
537 check the validity of the result. Encapsulate the checking here. */
539 static arith
540 check_result (arith rc, gfc_expr *x, gfc_expr *r, gfc_expr **rp)
542 arith val = rc;
544 if (val == ARITH_UNDERFLOW)
546 if (gfc_option.warn_underflow)
547 gfc_warning (gfc_arith_error (val), &x->where);
548 val = ARITH_OK;
551 if (val == ARITH_ASYMMETRIC)
553 gfc_warning (gfc_arith_error (val), &x->where);
554 val = ARITH_OK;
557 if (val != ARITH_OK)
558 gfc_free_expr (r);
559 else
560 *rp = r;
562 return val;
566 /* It may seem silly to have a subroutine that actually computes the
567 unary plus of a constant, but it prevents us from making exceptions
568 in the code elsewhere. Used for unary plus and parenthesized
569 expressions. */
571 static arith
572 gfc_arith_identity (gfc_expr *op1, gfc_expr **resultp)
574 *resultp = gfc_copy_expr (op1);
575 return ARITH_OK;
579 static arith
580 gfc_arith_uminus (gfc_expr *op1, gfc_expr **resultp)
582 gfc_expr *result;
583 arith rc;
585 result = gfc_get_constant_expr (op1->ts.type, op1->ts.kind, &op1->where);
587 switch (op1->ts.type)
589 case BT_INTEGER:
590 mpz_neg (result->value.integer, op1->value.integer);
591 break;
593 case BT_REAL:
594 mpfr_neg (result->value.real, op1->value.real, GFC_RND_MODE);
595 break;
597 case BT_COMPLEX:
598 mpc_neg (result->value.complex, op1->value.complex, GFC_MPC_RND_MODE);
599 break;
601 default:
602 gfc_internal_error ("gfc_arith_uminus(): Bad basic type");
605 rc = gfc_range_check (result);
607 return check_result (rc, op1, result, resultp);
611 static arith
612 gfc_arith_plus (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
614 gfc_expr *result;
615 arith rc;
617 result = gfc_get_constant_expr (op1->ts.type, op1->ts.kind, &op1->where);
619 switch (op1->ts.type)
621 case BT_INTEGER:
622 mpz_add (result->value.integer, op1->value.integer, op2->value.integer);
623 break;
625 case BT_REAL:
626 mpfr_add (result->value.real, op1->value.real, op2->value.real,
627 GFC_RND_MODE);
628 break;
630 case BT_COMPLEX:
631 mpc_add (result->value.complex, op1->value.complex, op2->value.complex,
632 GFC_MPC_RND_MODE);
633 break;
635 default:
636 gfc_internal_error ("gfc_arith_plus(): Bad basic type");
639 rc = gfc_range_check (result);
641 return check_result (rc, op1, result, resultp);
645 static arith
646 gfc_arith_minus (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
648 gfc_expr *result;
649 arith rc;
651 result = gfc_get_constant_expr (op1->ts.type, op1->ts.kind, &op1->where);
653 switch (op1->ts.type)
655 case BT_INTEGER:
656 mpz_sub (result->value.integer, op1->value.integer, op2->value.integer);
657 break;
659 case BT_REAL:
660 mpfr_sub (result->value.real, op1->value.real, op2->value.real,
661 GFC_RND_MODE);
662 break;
664 case BT_COMPLEX:
665 mpc_sub (result->value.complex, op1->value.complex,
666 op2->value.complex, GFC_MPC_RND_MODE);
667 break;
669 default:
670 gfc_internal_error ("gfc_arith_minus(): Bad basic type");
673 rc = gfc_range_check (result);
675 return check_result (rc, op1, result, resultp);
679 static arith
680 gfc_arith_times (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
682 gfc_expr *result;
683 arith rc;
685 result = gfc_get_constant_expr (op1->ts.type, op1->ts.kind, &op1->where);
687 switch (op1->ts.type)
689 case BT_INTEGER:
690 mpz_mul (result->value.integer, op1->value.integer, op2->value.integer);
691 break;
693 case BT_REAL:
694 mpfr_mul (result->value.real, op1->value.real, op2->value.real,
695 GFC_RND_MODE);
696 break;
698 case BT_COMPLEX:
699 gfc_set_model (mpc_realref (op1->value.complex));
700 mpc_mul (result->value.complex, op1->value.complex, op2->value.complex,
701 GFC_MPC_RND_MODE);
702 break;
704 default:
705 gfc_internal_error ("gfc_arith_times(): Bad basic type");
708 rc = gfc_range_check (result);
710 return check_result (rc, op1, result, resultp);
714 static arith
715 gfc_arith_divide (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
717 gfc_expr *result;
718 arith rc;
720 rc = ARITH_OK;
722 result = gfc_get_constant_expr (op1->ts.type, op1->ts.kind, &op1->where);
724 switch (op1->ts.type)
726 case BT_INTEGER:
727 if (mpz_sgn (op2->value.integer) == 0)
729 rc = ARITH_DIV0;
730 break;
733 mpz_tdiv_q (result->value.integer, op1->value.integer,
734 op2->value.integer);
735 break;
737 case BT_REAL:
738 if (mpfr_sgn (op2->value.real) == 0 && gfc_option.flag_range_check == 1)
740 rc = ARITH_DIV0;
741 break;
744 mpfr_div (result->value.real, op1->value.real, op2->value.real,
745 GFC_RND_MODE);
746 break;
748 case BT_COMPLEX:
749 if (mpc_cmp_si_si (op2->value.complex, 0, 0) == 0
750 && gfc_option.flag_range_check == 1)
752 rc = ARITH_DIV0;
753 break;
756 gfc_set_model (mpc_realref (op1->value.complex));
757 if (mpc_cmp_si_si (op2->value.complex, 0, 0) == 0)
759 /* In Fortran, return (NaN + NaN I) for any zero divisor. See
760 PR 40318. */
761 mpfr_set_nan (mpc_realref (result->value.complex));
762 mpfr_set_nan (mpc_imagref (result->value.complex));
764 else
765 mpc_div (result->value.complex, op1->value.complex, op2->value.complex,
766 GFC_MPC_RND_MODE);
767 break;
769 default:
770 gfc_internal_error ("gfc_arith_divide(): Bad basic type");
773 if (rc == ARITH_OK)
774 rc = gfc_range_check (result);
776 return check_result (rc, op1, result, resultp);
779 /* Raise a number to a power. */
781 static arith
782 arith_power (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
784 int power_sign;
785 gfc_expr *result;
786 arith rc;
788 rc = ARITH_OK;
789 result = gfc_get_constant_expr (op1->ts.type, op1->ts.kind, &op1->where);
791 switch (op2->ts.type)
793 case BT_INTEGER:
794 power_sign = mpz_sgn (op2->value.integer);
796 if (power_sign == 0)
798 /* Handle something to the zeroth power. Since we're dealing
799 with integral exponents, there is no ambiguity in the
800 limiting procedure used to determine the value of 0**0. */
801 switch (op1->ts.type)
803 case BT_INTEGER:
804 mpz_set_ui (result->value.integer, 1);
805 break;
807 case BT_REAL:
808 mpfr_set_ui (result->value.real, 1, GFC_RND_MODE);
809 break;
811 case BT_COMPLEX:
812 mpc_set_ui (result->value.complex, 1, GFC_MPC_RND_MODE);
813 break;
815 default:
816 gfc_internal_error ("arith_power(): Bad base");
819 else
821 switch (op1->ts.type)
823 case BT_INTEGER:
825 int power;
827 /* First, we simplify the cases of op1 == 1, 0 or -1. */
828 if (mpz_cmp_si (op1->value.integer, 1) == 0)
830 /* 1**op2 == 1 */
831 mpz_set_si (result->value.integer, 1);
833 else if (mpz_cmp_si (op1->value.integer, 0) == 0)
835 /* 0**op2 == 0, if op2 > 0
836 0**op2 overflow, if op2 < 0 ; in that case, we
837 set the result to 0 and return ARITH_DIV0. */
838 mpz_set_si (result->value.integer, 0);
839 if (mpz_cmp_si (op2->value.integer, 0) < 0)
840 rc = ARITH_DIV0;
842 else if (mpz_cmp_si (op1->value.integer, -1) == 0)
844 /* (-1)**op2 == (-1)**(mod(op2,2)) */
845 unsigned int odd = mpz_fdiv_ui (op2->value.integer, 2);
846 if (odd)
847 mpz_set_si (result->value.integer, -1);
848 else
849 mpz_set_si (result->value.integer, 1);
851 /* Then, we take care of op2 < 0. */
852 else if (mpz_cmp_si (op2->value.integer, 0) < 0)
854 /* if op2 < 0, op1**op2 == 0 because abs(op1) > 1. */
855 mpz_set_si (result->value.integer, 0);
857 else if (gfc_extract_int (op2, &power) != NULL)
859 /* If op2 doesn't fit in an int, the exponentiation will
860 overflow, because op2 > 0 and abs(op1) > 1. */
861 mpz_t max;
862 int i;
863 i = gfc_validate_kind (BT_INTEGER, result->ts.kind, false);
865 if (gfc_option.flag_range_check)
866 rc = ARITH_OVERFLOW;
868 /* Still, we want to give the same value as the
869 processor. */
870 mpz_init (max);
871 mpz_add_ui (max, gfc_integer_kinds[i].huge, 1);
872 mpz_mul_ui (max, max, 2);
873 mpz_powm (result->value.integer, op1->value.integer,
874 op2->value.integer, max);
875 mpz_clear (max);
877 else
878 mpz_pow_ui (result->value.integer, op1->value.integer,
879 power);
881 break;
883 case BT_REAL:
884 mpfr_pow_z (result->value.real, op1->value.real,
885 op2->value.integer, GFC_RND_MODE);
886 break;
888 case BT_COMPLEX:
889 mpc_pow_z (result->value.complex, op1->value.complex,
890 op2->value.integer, GFC_MPC_RND_MODE);
891 break;
893 default:
894 break;
897 break;
899 case BT_REAL:
901 if (gfc_init_expr_flag)
903 if (gfc_notify_std (GFC_STD_F2003,"Fortran 2003: Noninteger "
904 "exponent in an initialization "
905 "expression at %L", &op2->where) == FAILURE)
906 return ARITH_PROHIBIT;
909 if (mpfr_cmp_si (op1->value.real, 0) < 0)
911 gfc_error ("Raising a negative REAL at %L to "
912 "a REAL power is prohibited", &op1->where);
913 gfc_free (result);
914 return ARITH_PROHIBIT;
917 mpfr_pow (result->value.real, op1->value.real, op2->value.real,
918 GFC_RND_MODE);
919 break;
921 case BT_COMPLEX:
923 if (gfc_init_expr_flag)
925 if (gfc_notify_std (GFC_STD_F2003,"Fortran 2003: Noninteger "
926 "exponent in an initialization "
927 "expression at %L", &op2->where) == FAILURE)
928 return ARITH_PROHIBIT;
931 mpc_pow (result->value.complex, op1->value.complex,
932 op2->value.complex, GFC_MPC_RND_MODE);
934 break;
935 default:
936 gfc_internal_error ("arith_power(): unknown type");
939 if (rc == ARITH_OK)
940 rc = gfc_range_check (result);
942 return check_result (rc, op1, result, resultp);
946 /* Concatenate two string constants. */
948 static arith
949 gfc_arith_concat (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
951 gfc_expr *result;
952 int len;
954 gcc_assert (op1->ts.kind == op2->ts.kind);
955 result = gfc_get_constant_expr (BT_CHARACTER, op1->ts.kind,
956 &op1->where);
958 len = op1->value.character.length + op2->value.character.length;
960 result->value.character.string = gfc_get_wide_string (len + 1);
961 result->value.character.length = len;
963 memcpy (result->value.character.string, op1->value.character.string,
964 op1->value.character.length * sizeof (gfc_char_t));
966 memcpy (&result->value.character.string[op1->value.character.length],
967 op2->value.character.string,
968 op2->value.character.length * sizeof (gfc_char_t));
970 result->value.character.string[len] = '\0';
972 *resultp = result;
974 return ARITH_OK;
977 /* Comparison between real values; returns 0 if (op1 .op. op2) is true.
978 This function mimics mpfr_cmp but takes NaN into account. */
980 static int
981 compare_real (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
983 int rc;
984 switch (op)
986 case INTRINSIC_EQ:
987 rc = mpfr_equal_p (op1->value.real, op2->value.real) ? 0 : 1;
988 break;
989 case INTRINSIC_GT:
990 rc = mpfr_greater_p (op1->value.real, op2->value.real) ? 1 : -1;
991 break;
992 case INTRINSIC_GE:
993 rc = mpfr_greaterequal_p (op1->value.real, op2->value.real) ? 1 : -1;
994 break;
995 case INTRINSIC_LT:
996 rc = mpfr_less_p (op1->value.real, op2->value.real) ? -1 : 1;
997 break;
998 case INTRINSIC_LE:
999 rc = mpfr_lessequal_p (op1->value.real, op2->value.real) ? -1 : 1;
1000 break;
1001 default:
1002 gfc_internal_error ("compare_real(): Bad operator");
1005 return rc;
1008 /* Comparison operators. Assumes that the two expression nodes
1009 contain two constants of the same type. The op argument is
1010 needed to handle NaN correctly. */
1013 gfc_compare_expr (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1015 int rc;
1017 switch (op1->ts.type)
1019 case BT_INTEGER:
1020 rc = mpz_cmp (op1->value.integer, op2->value.integer);
1021 break;
1023 case BT_REAL:
1024 rc = compare_real (op1, op2, op);
1025 break;
1027 case BT_CHARACTER:
1028 rc = gfc_compare_string (op1, op2);
1029 break;
1031 case BT_LOGICAL:
1032 rc = ((!op1->value.logical && op2->value.logical)
1033 || (op1->value.logical && !op2->value.logical));
1034 break;
1036 default:
1037 gfc_internal_error ("gfc_compare_expr(): Bad basic type");
1040 return rc;
1044 /* Compare a pair of complex numbers. Naturally, this is only for
1045 equality and inequality. */
1047 static int
1048 compare_complex (gfc_expr *op1, gfc_expr *op2)
1050 return mpc_cmp (op1->value.complex, op2->value.complex) == 0;
1054 /* Given two constant strings and the inverse collating sequence, compare the
1055 strings. We return -1 for a < b, 0 for a == b and 1 for a > b.
1056 We use the processor's default collating sequence. */
1059 gfc_compare_string (gfc_expr *a, gfc_expr *b)
1061 int len, alen, blen, i;
1062 gfc_char_t ac, bc;
1064 alen = a->value.character.length;
1065 blen = b->value.character.length;
1067 len = MAX(alen, blen);
1069 for (i = 0; i < len; i++)
1071 ac = ((i < alen) ? a->value.character.string[i] : ' ');
1072 bc = ((i < blen) ? b->value.character.string[i] : ' ');
1074 if (ac < bc)
1075 return -1;
1076 if (ac > bc)
1077 return 1;
1080 /* Strings are equal */
1081 return 0;
1086 gfc_compare_with_Cstring (gfc_expr *a, const char *b, bool case_sensitive)
1088 int len, alen, blen, i;
1089 gfc_char_t ac, bc;
1091 alen = a->value.character.length;
1092 blen = strlen (b);
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[i] : ' ');
1101 if (!case_sensitive)
1103 ac = TOLOWER (ac);
1104 bc = TOLOWER (bc);
1107 if (ac < bc)
1108 return -1;
1109 if (ac > bc)
1110 return 1;
1113 /* Strings are equal */
1114 return 0;
1118 /* Specific comparison subroutines. */
1120 static arith
1121 gfc_arith_eq (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1123 gfc_expr *result;
1125 result = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
1126 &op1->where);
1127 result->value.logical = (op1->ts.type == BT_COMPLEX)
1128 ? compare_complex (op1, op2)
1129 : (gfc_compare_expr (op1, op2, INTRINSIC_EQ) == 0);
1131 *resultp = result;
1132 return ARITH_OK;
1136 static arith
1137 gfc_arith_ne (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1139 gfc_expr *result;
1141 result = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
1142 &op1->where);
1143 result->value.logical = (op1->ts.type == BT_COMPLEX)
1144 ? !compare_complex (op1, op2)
1145 : (gfc_compare_expr (op1, op2, INTRINSIC_EQ) != 0);
1147 *resultp = result;
1148 return ARITH_OK;
1152 static arith
1153 gfc_arith_gt (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1155 gfc_expr *result;
1157 result = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
1158 &op1->where);
1159 result->value.logical = (gfc_compare_expr (op1, op2, INTRINSIC_GT) > 0);
1160 *resultp = result;
1162 return ARITH_OK;
1166 static arith
1167 gfc_arith_ge (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1169 gfc_expr *result;
1171 result = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
1172 &op1->where);
1173 result->value.logical = (gfc_compare_expr (op1, op2, INTRINSIC_GE) >= 0);
1174 *resultp = result;
1176 return ARITH_OK;
1180 static arith
1181 gfc_arith_lt (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1183 gfc_expr *result;
1185 result = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
1186 &op1->where);
1187 result->value.logical = (gfc_compare_expr (op1, op2, INTRINSIC_LT) < 0);
1188 *resultp = result;
1190 return ARITH_OK;
1194 static arith
1195 gfc_arith_le (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp)
1197 gfc_expr *result;
1199 result = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
1200 &op1->where);
1201 result->value.logical = (gfc_compare_expr (op1, op2, INTRINSIC_LE) <= 0);
1202 *resultp = result;
1204 return ARITH_OK;
1208 static arith
1209 reduce_unary (arith (*eval) (gfc_expr *, gfc_expr **), gfc_expr *op,
1210 gfc_expr **result)
1212 gfc_constructor_base head;
1213 gfc_constructor *c;
1214 gfc_expr *r;
1215 arith rc;
1217 if (op->expr_type == EXPR_CONSTANT)
1218 return eval (op, result);
1220 rc = ARITH_OK;
1221 head = gfc_constructor_copy (op->value.constructor);
1222 for (c = gfc_constructor_first (head); c; c = gfc_constructor_next (c))
1224 rc = reduce_unary (eval, c->expr, &r);
1226 if (rc != ARITH_OK)
1227 break;
1229 gfc_replace_expr (c->expr, r);
1232 if (rc != ARITH_OK)
1233 gfc_constructor_free (head);
1234 else
1236 gfc_constructor *c = gfc_constructor_first (head);
1237 r = gfc_get_array_expr (c->expr->ts.type, c->expr->ts.kind,
1238 &op->where);
1239 r->shape = gfc_copy_shape (op->shape, op->rank);
1240 r->rank = op->rank;
1241 r->value.constructor = head;
1242 *result = r;
1245 return rc;
1249 static arith
1250 reduce_binary_ac (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1251 gfc_expr *op1, gfc_expr *op2, gfc_expr **result)
1253 gfc_constructor_base head;
1254 gfc_constructor *c;
1255 gfc_expr *r;
1256 arith rc = ARITH_OK;
1258 head = gfc_constructor_copy (op1->value.constructor);
1259 for (c = gfc_constructor_first (head); c; c = gfc_constructor_next (c))
1261 if (c->expr->expr_type == EXPR_CONSTANT)
1262 rc = eval (c->expr, op2, &r);
1263 else
1264 rc = reduce_binary_ac (eval, c->expr, op2, &r);
1266 if (rc != ARITH_OK)
1267 break;
1269 gfc_replace_expr (c->expr, r);
1272 if (rc != ARITH_OK)
1273 gfc_constructor_free (head);
1274 else
1276 gfc_constructor *c = gfc_constructor_first (head);
1277 r = gfc_get_array_expr (c->expr->ts.type, c->expr->ts.kind,
1278 &op1->where);
1279 r->shape = gfc_copy_shape (op1->shape, op1->rank);
1280 r->rank = op1->rank;
1281 r->value.constructor = head;
1282 *result = r;
1285 return rc;
1289 static arith
1290 reduce_binary_ca (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1291 gfc_expr *op1, gfc_expr *op2, gfc_expr **result)
1293 gfc_constructor_base head;
1294 gfc_constructor *c;
1295 gfc_expr *r;
1296 arith rc = ARITH_OK;
1298 head = gfc_constructor_copy (op2->value.constructor);
1299 for (c = gfc_constructor_first (head); c; c = gfc_constructor_next (c))
1301 if (c->expr->expr_type == EXPR_CONSTANT)
1302 rc = eval (op1, c->expr, &r);
1303 else
1304 rc = reduce_binary_ca (eval, op1, c->expr, &r);
1306 if (rc != ARITH_OK)
1307 break;
1309 gfc_replace_expr (c->expr, r);
1312 if (rc != ARITH_OK)
1313 gfc_constructor_free (head);
1314 else
1316 gfc_constructor *c = gfc_constructor_first (head);
1317 r = gfc_get_array_expr (c->expr->ts.type, c->expr->ts.kind,
1318 &op2->where);
1319 r->shape = gfc_copy_shape (op2->shape, op2->rank);
1320 r->rank = op2->rank;
1321 r->value.constructor = head;
1322 *result = r;
1325 return rc;
1329 /* We need a forward declaration of reduce_binary. */
1330 static arith reduce_binary (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1331 gfc_expr *op1, gfc_expr *op2, gfc_expr **result);
1334 static arith
1335 reduce_binary_aa (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1336 gfc_expr *op1, gfc_expr *op2, gfc_expr **result)
1338 gfc_constructor_base head;
1339 gfc_constructor *c, *d;
1340 gfc_expr *r;
1341 arith rc = ARITH_OK;
1343 if (gfc_check_conformance (op1, op2,
1344 "elemental binary operation") != SUCCESS)
1345 return ARITH_INCOMMENSURATE;
1347 head = gfc_constructor_copy (op1->value.constructor);
1348 for (c = gfc_constructor_first (head),
1349 d = gfc_constructor_first (op2->value.constructor);
1350 c && d;
1351 c = gfc_constructor_next (c), d = gfc_constructor_next (d))
1353 rc = reduce_binary (eval, c->expr, d->expr, &r);
1354 if (rc != ARITH_OK)
1355 break;
1357 gfc_replace_expr (c->expr, r);
1360 if (c || d)
1361 rc = ARITH_INCOMMENSURATE;
1363 if (rc != ARITH_OK)
1364 gfc_constructor_free (head);
1365 else
1367 gfc_constructor *c = gfc_constructor_first (head);
1368 r = gfc_get_array_expr (c->expr->ts.type, c->expr->ts.kind,
1369 &op1->where);
1370 r->shape = gfc_copy_shape (op1->shape, op1->rank);
1371 r->rank = op1->rank;
1372 r->value.constructor = head;
1373 *result = r;
1376 return rc;
1380 static arith
1381 reduce_binary (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1382 gfc_expr *op1, gfc_expr *op2, gfc_expr **result)
1384 if (op1->expr_type == EXPR_CONSTANT && op2->expr_type == EXPR_CONSTANT)
1385 return eval (op1, op2, result);
1387 if (op1->expr_type == EXPR_CONSTANT && op2->expr_type == EXPR_ARRAY)
1388 return reduce_binary_ca (eval, op1, op2, result);
1390 if (op1->expr_type == EXPR_ARRAY && op2->expr_type == EXPR_CONSTANT)
1391 return reduce_binary_ac (eval, op1, op2, result);
1393 return reduce_binary_aa (eval, op1, op2, result);
1397 typedef union
1399 arith (*f2)(gfc_expr *, gfc_expr **);
1400 arith (*f3)(gfc_expr *, gfc_expr *, gfc_expr **);
1402 eval_f;
1404 /* High level arithmetic subroutines. These subroutines go into
1405 eval_intrinsic(), which can do one of several things to its
1406 operands. If the operands are incompatible with the intrinsic
1407 operation, we return a node pointing to the operands and hope that
1408 an operator interface is found during resolution.
1410 If the operands are compatible and are constants, then we try doing
1411 the arithmetic. We also handle the cases where either or both
1412 operands are array constructors. */
1414 static gfc_expr *
1415 eval_intrinsic (gfc_intrinsic_op op,
1416 eval_f eval, gfc_expr *op1, gfc_expr *op2)
1418 gfc_expr temp, *result;
1419 int unary;
1420 arith rc;
1422 gfc_clear_ts (&temp.ts);
1424 switch (op)
1426 /* Logical unary */
1427 case INTRINSIC_NOT:
1428 if (op1->ts.type != BT_LOGICAL)
1429 goto runtime;
1431 temp.ts.type = BT_LOGICAL;
1432 temp.ts.kind = gfc_default_logical_kind;
1433 unary = 1;
1434 break;
1436 /* Logical binary operators */
1437 case INTRINSIC_OR:
1438 case INTRINSIC_AND:
1439 case INTRINSIC_NEQV:
1440 case INTRINSIC_EQV:
1441 if (op1->ts.type != BT_LOGICAL || op2->ts.type != BT_LOGICAL)
1442 goto runtime;
1444 temp.ts.type = BT_LOGICAL;
1445 temp.ts.kind = gfc_default_logical_kind;
1446 unary = 0;
1447 break;
1449 /* Numeric unary */
1450 case INTRINSIC_UPLUS:
1451 case INTRINSIC_UMINUS:
1452 if (!gfc_numeric_ts (&op1->ts))
1453 goto runtime;
1455 temp.ts = op1->ts;
1456 unary = 1;
1457 break;
1459 case INTRINSIC_PARENTHESES:
1460 temp.ts = op1->ts;
1461 unary = 1;
1462 break;
1464 /* Additional restrictions for ordering relations. */
1465 case INTRINSIC_GE:
1466 case INTRINSIC_GE_OS:
1467 case INTRINSIC_LT:
1468 case INTRINSIC_LT_OS:
1469 case INTRINSIC_LE:
1470 case INTRINSIC_LE_OS:
1471 case INTRINSIC_GT:
1472 case INTRINSIC_GT_OS:
1473 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
1475 temp.ts.type = BT_LOGICAL;
1476 temp.ts.kind = gfc_default_logical_kind;
1477 goto runtime;
1480 /* Fall through */
1481 case INTRINSIC_EQ:
1482 case INTRINSIC_EQ_OS:
1483 case INTRINSIC_NE:
1484 case INTRINSIC_NE_OS:
1485 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
1487 unary = 0;
1488 temp.ts.type = BT_LOGICAL;
1489 temp.ts.kind = gfc_default_logical_kind;
1491 /* If kind mismatch, exit and we'll error out later. */
1492 if (op1->ts.kind != op2->ts.kind)
1493 goto runtime;
1495 break;
1498 /* Fall through */
1499 /* Numeric binary */
1500 case INTRINSIC_PLUS:
1501 case INTRINSIC_MINUS:
1502 case INTRINSIC_TIMES:
1503 case INTRINSIC_DIVIDE:
1504 case INTRINSIC_POWER:
1505 if (!gfc_numeric_ts (&op1->ts) || !gfc_numeric_ts (&op2->ts))
1506 goto runtime;
1508 /* Insert any necessary type conversions to make the operands
1509 compatible. */
1511 temp.expr_type = EXPR_OP;
1512 gfc_clear_ts (&temp.ts);
1513 temp.value.op.op = op;
1515 temp.value.op.op1 = op1;
1516 temp.value.op.op2 = op2;
1518 gfc_type_convert_binary (&temp, 0);
1520 if (op == INTRINSIC_EQ || op == INTRINSIC_NE
1521 || op == INTRINSIC_GE || op == INTRINSIC_GT
1522 || op == INTRINSIC_LE || op == INTRINSIC_LT
1523 || op == INTRINSIC_EQ_OS || op == INTRINSIC_NE_OS
1524 || op == INTRINSIC_GE_OS || op == INTRINSIC_GT_OS
1525 || op == INTRINSIC_LE_OS || op == INTRINSIC_LT_OS)
1527 temp.ts.type = BT_LOGICAL;
1528 temp.ts.kind = gfc_default_logical_kind;
1531 unary = 0;
1532 break;
1534 /* Character binary */
1535 case INTRINSIC_CONCAT:
1536 if (op1->ts.type != BT_CHARACTER || op2->ts.type != BT_CHARACTER
1537 || op1->ts.kind != op2->ts.kind)
1538 goto runtime;
1540 temp.ts.type = BT_CHARACTER;
1541 temp.ts.kind = op1->ts.kind;
1542 unary = 0;
1543 break;
1545 case INTRINSIC_USER:
1546 goto runtime;
1548 default:
1549 gfc_internal_error ("eval_intrinsic(): Bad operator");
1552 if (op1->expr_type != EXPR_CONSTANT
1553 && (op1->expr_type != EXPR_ARRAY
1554 || !gfc_is_constant_expr (op1) || !gfc_expanded_ac (op1)))
1555 goto runtime;
1557 if (op2 != NULL
1558 && op2->expr_type != EXPR_CONSTANT
1559 && (op2->expr_type != EXPR_ARRAY
1560 || !gfc_is_constant_expr (op2) || !gfc_expanded_ac (op2)))
1561 goto runtime;
1563 if (unary)
1564 rc = reduce_unary (eval.f2, op1, &result);
1565 else
1566 rc = reduce_binary (eval.f3, op1, op2, &result);
1569 /* Something went wrong. */
1570 if (op == INTRINSIC_POWER && rc == ARITH_PROHIBIT)
1571 return NULL;
1573 if (rc != ARITH_OK)
1575 gfc_error (gfc_arith_error (rc), &op1->where);
1576 return NULL;
1579 gfc_free_expr (op1);
1580 gfc_free_expr (op2);
1581 return result;
1583 runtime:
1584 /* Create a run-time expression. */
1585 result = gfc_get_operator_expr (&op1->where, op, op1, op2);
1586 result->ts = temp.ts;
1588 return result;
1592 /* Modify type of expression for zero size array. */
1594 static gfc_expr *
1595 eval_type_intrinsic0 (gfc_intrinsic_op iop, gfc_expr *op)
1597 if (op == NULL)
1598 gfc_internal_error ("eval_type_intrinsic0(): op NULL");
1600 switch (iop)
1602 case INTRINSIC_GE:
1603 case INTRINSIC_GE_OS:
1604 case INTRINSIC_LT:
1605 case INTRINSIC_LT_OS:
1606 case INTRINSIC_LE:
1607 case INTRINSIC_LE_OS:
1608 case INTRINSIC_GT:
1609 case INTRINSIC_GT_OS:
1610 case INTRINSIC_EQ:
1611 case INTRINSIC_EQ_OS:
1612 case INTRINSIC_NE:
1613 case INTRINSIC_NE_OS:
1614 op->ts.type = BT_LOGICAL;
1615 op->ts.kind = gfc_default_logical_kind;
1616 break;
1618 default:
1619 break;
1622 return op;
1626 /* Return nonzero if the expression is a zero size array. */
1628 static int
1629 gfc_zero_size_array (gfc_expr *e)
1631 if (e->expr_type != EXPR_ARRAY)
1632 return 0;
1634 return e->value.constructor == NULL;
1638 /* Reduce a binary expression where at least one of the operands
1639 involves a zero-length array. Returns NULL if neither of the
1640 operands is a zero-length array. */
1642 static gfc_expr *
1643 reduce_binary0 (gfc_expr *op1, gfc_expr *op2)
1645 if (gfc_zero_size_array (op1))
1647 gfc_free_expr (op2);
1648 return op1;
1651 if (gfc_zero_size_array (op2))
1653 gfc_free_expr (op1);
1654 return op2;
1657 return NULL;
1661 static gfc_expr *
1662 eval_intrinsic_f2 (gfc_intrinsic_op op,
1663 arith (*eval) (gfc_expr *, gfc_expr **),
1664 gfc_expr *op1, gfc_expr *op2)
1666 gfc_expr *result;
1667 eval_f f;
1669 if (op2 == NULL)
1671 if (gfc_zero_size_array (op1))
1672 return eval_type_intrinsic0 (op, op1);
1674 else
1676 result = reduce_binary0 (op1, op2);
1677 if (result != NULL)
1678 return eval_type_intrinsic0 (op, result);
1681 f.f2 = eval;
1682 return eval_intrinsic (op, f, op1, op2);
1686 static gfc_expr *
1687 eval_intrinsic_f3 (gfc_intrinsic_op op,
1688 arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1689 gfc_expr *op1, gfc_expr *op2)
1691 gfc_expr *result;
1692 eval_f f;
1694 result = reduce_binary0 (op1, op2);
1695 if (result != NULL)
1696 return eval_type_intrinsic0(op, result);
1698 f.f3 = eval;
1699 return eval_intrinsic (op, f, op1, op2);
1703 gfc_expr *
1704 gfc_parentheses (gfc_expr *op)
1706 if (gfc_is_constant_expr (op))
1707 return op;
1709 return eval_intrinsic_f2 (INTRINSIC_PARENTHESES, gfc_arith_identity,
1710 op, NULL);
1713 gfc_expr *
1714 gfc_uplus (gfc_expr *op)
1716 return eval_intrinsic_f2 (INTRINSIC_UPLUS, gfc_arith_identity, op, NULL);
1720 gfc_expr *
1721 gfc_uminus (gfc_expr *op)
1723 return eval_intrinsic_f2 (INTRINSIC_UMINUS, gfc_arith_uminus, op, NULL);
1727 gfc_expr *
1728 gfc_add (gfc_expr *op1, gfc_expr *op2)
1730 return eval_intrinsic_f3 (INTRINSIC_PLUS, gfc_arith_plus, op1, op2);
1734 gfc_expr *
1735 gfc_subtract (gfc_expr *op1, gfc_expr *op2)
1737 return eval_intrinsic_f3 (INTRINSIC_MINUS, gfc_arith_minus, op1, op2);
1741 gfc_expr *
1742 gfc_multiply (gfc_expr *op1, gfc_expr *op2)
1744 return eval_intrinsic_f3 (INTRINSIC_TIMES, gfc_arith_times, op1, op2);
1748 gfc_expr *
1749 gfc_divide (gfc_expr *op1, gfc_expr *op2)
1751 return eval_intrinsic_f3 (INTRINSIC_DIVIDE, gfc_arith_divide, op1, op2);
1755 gfc_expr *
1756 gfc_power (gfc_expr *op1, gfc_expr *op2)
1758 return eval_intrinsic_f3 (INTRINSIC_POWER, arith_power, op1, op2);
1762 gfc_expr *
1763 gfc_concat (gfc_expr *op1, gfc_expr *op2)
1765 return eval_intrinsic_f3 (INTRINSIC_CONCAT, gfc_arith_concat, op1, op2);
1769 gfc_expr *
1770 gfc_and (gfc_expr *op1, gfc_expr *op2)
1772 return eval_intrinsic_f3 (INTRINSIC_AND, gfc_arith_and, op1, op2);
1776 gfc_expr *
1777 gfc_or (gfc_expr *op1, gfc_expr *op2)
1779 return eval_intrinsic_f3 (INTRINSIC_OR, gfc_arith_or, op1, op2);
1783 gfc_expr *
1784 gfc_not (gfc_expr *op1)
1786 return eval_intrinsic_f2 (INTRINSIC_NOT, gfc_arith_not, op1, NULL);
1790 gfc_expr *
1791 gfc_eqv (gfc_expr *op1, gfc_expr *op2)
1793 return eval_intrinsic_f3 (INTRINSIC_EQV, gfc_arith_eqv, op1, op2);
1797 gfc_expr *
1798 gfc_neqv (gfc_expr *op1, gfc_expr *op2)
1800 return eval_intrinsic_f3 (INTRINSIC_NEQV, gfc_arith_neqv, op1, op2);
1804 gfc_expr *
1805 gfc_eq (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1807 return eval_intrinsic_f3 (op, gfc_arith_eq, op1, op2);
1811 gfc_expr *
1812 gfc_ne (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1814 return eval_intrinsic_f3 (op, gfc_arith_ne, op1, op2);
1818 gfc_expr *
1819 gfc_gt (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1821 return eval_intrinsic_f3 (op, gfc_arith_gt, op1, op2);
1825 gfc_expr *
1826 gfc_ge (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1828 return eval_intrinsic_f3 (op, gfc_arith_ge, op1, op2);
1832 gfc_expr *
1833 gfc_lt (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1835 return eval_intrinsic_f3 (op, gfc_arith_lt, op1, op2);
1839 gfc_expr *
1840 gfc_le (gfc_expr *op1, gfc_expr *op2, gfc_intrinsic_op op)
1842 return eval_intrinsic_f3 (op, gfc_arith_le, op1, op2);
1846 /* Convert an integer string to an expression node. */
1848 gfc_expr *
1849 gfc_convert_integer (const char *buffer, int kind, int radix, locus *where)
1851 gfc_expr *e;
1852 const char *t;
1854 e = gfc_get_constant_expr (BT_INTEGER, kind, where);
1855 /* A leading plus is allowed, but not by mpz_set_str. */
1856 if (buffer[0] == '+')
1857 t = buffer + 1;
1858 else
1859 t = buffer;
1860 mpz_set_str (e->value.integer, t, radix);
1862 return e;
1866 /* Convert a real string to an expression node. */
1868 gfc_expr *
1869 gfc_convert_real (const char *buffer, int kind, locus *where)
1871 gfc_expr *e;
1873 e = gfc_get_constant_expr (BT_REAL, kind, where);
1874 mpfr_set_str (e->value.real, buffer, 10, GFC_RND_MODE);
1876 return e;
1880 /* Convert a pair of real, constant expression nodes to a single
1881 complex expression node. */
1883 gfc_expr *
1884 gfc_convert_complex (gfc_expr *real, gfc_expr *imag, int kind)
1886 gfc_expr *e;
1888 e = gfc_get_constant_expr (BT_COMPLEX, kind, &real->where);
1889 mpc_set_fr_fr (e->value.complex, real->value.real, imag->value.real,
1890 GFC_MPC_RND_MODE);
1892 return e;
1896 /******* Simplification of intrinsic functions with constant arguments *****/
1899 /* Deal with an arithmetic error. */
1901 static void
1902 arith_error (arith rc, gfc_typespec *from, gfc_typespec *to, locus *where)
1904 switch (rc)
1906 case ARITH_OK:
1907 gfc_error ("Arithmetic OK converting %s to %s at %L",
1908 gfc_typename (from), gfc_typename (to), where);
1909 break;
1910 case ARITH_OVERFLOW:
1911 gfc_error ("Arithmetic overflow converting %s to %s at %L. This check "
1912 "can be disabled with the option -fno-range-check",
1913 gfc_typename (from), gfc_typename (to), where);
1914 break;
1915 case ARITH_UNDERFLOW:
1916 gfc_error ("Arithmetic underflow converting %s to %s at %L. This check "
1917 "can be disabled with the option -fno-range-check",
1918 gfc_typename (from), gfc_typename (to), where);
1919 break;
1920 case ARITH_NAN:
1921 gfc_error ("Arithmetic NaN converting %s to %s at %L. This check "
1922 "can be disabled with the option -fno-range-check",
1923 gfc_typename (from), gfc_typename (to), where);
1924 break;
1925 case ARITH_DIV0:
1926 gfc_error ("Division by zero converting %s to %s at %L",
1927 gfc_typename (from), gfc_typename (to), where);
1928 break;
1929 case ARITH_INCOMMENSURATE:
1930 gfc_error ("Array operands are incommensurate converting %s to %s at %L",
1931 gfc_typename (from), gfc_typename (to), where);
1932 break;
1933 case ARITH_ASYMMETRIC:
1934 gfc_error ("Integer outside symmetric range implied by Standard Fortran"
1935 " converting %s to %s at %L",
1936 gfc_typename (from), gfc_typename (to), where);
1937 break;
1938 default:
1939 gfc_internal_error ("gfc_arith_error(): Bad error code");
1942 /* TODO: Do something about the error, i.e., throw exception, return
1943 NaN, etc. */
1947 /* Convert integers to integers. */
1949 gfc_expr *
1950 gfc_int2int (gfc_expr *src, int kind)
1952 gfc_expr *result;
1953 arith rc;
1955 result = gfc_get_constant_expr (BT_INTEGER, kind, &src->where);
1957 mpz_set (result->value.integer, src->value.integer);
1959 if ((rc = gfc_check_integer_range (result->value.integer, kind)) != ARITH_OK)
1961 if (rc == ARITH_ASYMMETRIC)
1963 gfc_warning (gfc_arith_error (rc), &src->where);
1965 else
1967 arith_error (rc, &src->ts, &result->ts, &src->where);
1968 gfc_free_expr (result);
1969 return NULL;
1973 return result;
1977 /* Convert integers to reals. */
1979 gfc_expr *
1980 gfc_int2real (gfc_expr *src, int kind)
1982 gfc_expr *result;
1983 arith rc;
1985 result = gfc_get_constant_expr (BT_REAL, kind, &src->where);
1987 mpfr_set_z (result->value.real, src->value.integer, GFC_RND_MODE);
1989 if ((rc = gfc_check_real_range (result->value.real, kind)) != ARITH_OK)
1991 arith_error (rc, &src->ts, &result->ts, &src->where);
1992 gfc_free_expr (result);
1993 return NULL;
1996 return result;
2000 /* Convert default integer to default complex. */
2002 gfc_expr *
2003 gfc_int2complex (gfc_expr *src, int kind)
2005 gfc_expr *result;
2006 arith rc;
2008 result = gfc_get_constant_expr (BT_COMPLEX, kind, &src->where);
2010 mpc_set_z (result->value.complex, src->value.integer, GFC_MPC_RND_MODE);
2012 if ((rc = gfc_check_real_range (mpc_realref (result->value.complex), kind))
2013 != ARITH_OK)
2015 arith_error (rc, &src->ts, &result->ts, &src->where);
2016 gfc_free_expr (result);
2017 return NULL;
2020 return result;
2024 /* Convert default real to default integer. */
2026 gfc_expr *
2027 gfc_real2int (gfc_expr *src, int kind)
2029 gfc_expr *result;
2030 arith rc;
2032 result = gfc_get_constant_expr (BT_INTEGER, kind, &src->where);
2034 gfc_mpfr_to_mpz (result->value.integer, src->value.real, &src->where);
2036 if ((rc = gfc_check_integer_range (result->value.integer, kind)) != ARITH_OK)
2038 arith_error (rc, &src->ts, &result->ts, &src->where);
2039 gfc_free_expr (result);
2040 return NULL;
2043 return result;
2047 /* Convert real to real. */
2049 gfc_expr *
2050 gfc_real2real (gfc_expr *src, int kind)
2052 gfc_expr *result;
2053 arith rc;
2055 result = gfc_get_constant_expr (BT_REAL, kind, &src->where);
2057 mpfr_set (result->value.real, src->value.real, GFC_RND_MODE);
2059 rc = gfc_check_real_range (result->value.real, kind);
2061 if (rc == ARITH_UNDERFLOW)
2063 if (gfc_option.warn_underflow)
2064 gfc_warning (gfc_arith_error (rc), &src->where);
2065 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
2067 else if (rc != ARITH_OK)
2069 arith_error (rc, &src->ts, &result->ts, &src->where);
2070 gfc_free_expr (result);
2071 return NULL;
2074 return result;
2078 /* Convert real to complex. */
2080 gfc_expr *
2081 gfc_real2complex (gfc_expr *src, int kind)
2083 gfc_expr *result;
2084 arith rc;
2086 result = gfc_get_constant_expr (BT_COMPLEX, kind, &src->where);
2088 mpc_set_fr (result->value.complex, src->value.real, GFC_MPC_RND_MODE);
2090 rc = gfc_check_real_range (mpc_realref (result->value.complex), kind);
2092 if (rc == ARITH_UNDERFLOW)
2094 if (gfc_option.warn_underflow)
2095 gfc_warning (gfc_arith_error (rc), &src->where);
2096 mpfr_set_ui (mpc_realref (result->value.complex), 0, GFC_RND_MODE);
2098 else if (rc != ARITH_OK)
2100 arith_error (rc, &src->ts, &result->ts, &src->where);
2101 gfc_free_expr (result);
2102 return NULL;
2105 return result;
2109 /* Convert complex to integer. */
2111 gfc_expr *
2112 gfc_complex2int (gfc_expr *src, int kind)
2114 gfc_expr *result;
2115 arith rc;
2117 result = gfc_get_constant_expr (BT_INTEGER, kind, &src->where);
2119 gfc_mpfr_to_mpz (result->value.integer, mpc_realref (src->value.complex),
2120 &src->where);
2122 if ((rc = gfc_check_integer_range (result->value.integer, kind)) != ARITH_OK)
2124 arith_error (rc, &src->ts, &result->ts, &src->where);
2125 gfc_free_expr (result);
2126 return NULL;
2129 return result;
2133 /* Convert complex to real. */
2135 gfc_expr *
2136 gfc_complex2real (gfc_expr *src, int kind)
2138 gfc_expr *result;
2139 arith rc;
2141 result = gfc_get_constant_expr (BT_REAL, kind, &src->where);
2143 mpc_real (result->value.real, src->value.complex, GFC_RND_MODE);
2145 rc = gfc_check_real_range (result->value.real, kind);
2147 if (rc == ARITH_UNDERFLOW)
2149 if (gfc_option.warn_underflow)
2150 gfc_warning (gfc_arith_error (rc), &src->where);
2151 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
2153 if (rc != ARITH_OK)
2155 arith_error (rc, &src->ts, &result->ts, &src->where);
2156 gfc_free_expr (result);
2157 return NULL;
2160 return result;
2164 /* Convert complex to complex. */
2166 gfc_expr *
2167 gfc_complex2complex (gfc_expr *src, int kind)
2169 gfc_expr *result;
2170 arith rc;
2172 result = gfc_get_constant_expr (BT_COMPLEX, kind, &src->where);
2174 mpc_set (result->value.complex, src->value.complex, GFC_MPC_RND_MODE);
2176 rc = gfc_check_real_range (mpc_realref (result->value.complex), kind);
2178 if (rc == ARITH_UNDERFLOW)
2180 if (gfc_option.warn_underflow)
2181 gfc_warning (gfc_arith_error (rc), &src->where);
2182 mpfr_set_ui (mpc_realref (result->value.complex), 0, GFC_RND_MODE);
2184 else if (rc != ARITH_OK)
2186 arith_error (rc, &src->ts, &result->ts, &src->where);
2187 gfc_free_expr (result);
2188 return NULL;
2191 rc = gfc_check_real_range (mpc_imagref (result->value.complex), kind);
2193 if (rc == ARITH_UNDERFLOW)
2195 if (gfc_option.warn_underflow)
2196 gfc_warning (gfc_arith_error (rc), &src->where);
2197 mpfr_set_ui (mpc_imagref (result->value.complex), 0, GFC_RND_MODE);
2199 else if (rc != ARITH_OK)
2201 arith_error (rc, &src->ts, &result->ts, &src->where);
2202 gfc_free_expr (result);
2203 return NULL;
2206 return result;
2210 /* Logical kind conversion. */
2212 gfc_expr *
2213 gfc_log2log (gfc_expr *src, int kind)
2215 gfc_expr *result;
2217 result = gfc_get_constant_expr (BT_LOGICAL, kind, &src->where);
2218 result->value.logical = src->value.logical;
2220 return result;
2224 /* Convert logical to integer. */
2226 gfc_expr *
2227 gfc_log2int (gfc_expr *src, int kind)
2229 gfc_expr *result;
2231 result = gfc_get_constant_expr (BT_INTEGER, kind, &src->where);
2232 mpz_set_si (result->value.integer, src->value.logical);
2234 return result;
2238 /* Convert integer to logical. */
2240 gfc_expr *
2241 gfc_int2log (gfc_expr *src, int kind)
2243 gfc_expr *result;
2245 result = gfc_get_constant_expr (BT_LOGICAL, kind, &src->where);
2246 result->value.logical = (mpz_cmp_si (src->value.integer, 0) != 0);
2248 return result;
2252 /* Helper function to set the representation in a Hollerith conversion.
2253 This assumes that the ts.type and ts.kind of the result have already
2254 been set. */
2256 static void
2257 hollerith2representation (gfc_expr *result, gfc_expr *src)
2259 int src_len, result_len;
2261 src_len = src->representation.length;
2262 result_len = gfc_target_expr_size (result);
2264 if (src_len > result_len)
2266 gfc_warning ("The Hollerith constant at %L is too long to convert to %s",
2267 &src->where, gfc_typename(&result->ts));
2270 result->representation.string = XCNEWVEC (char, result_len + 1);
2271 memcpy (result->representation.string, src->representation.string,
2272 MIN (result_len, src_len));
2274 if (src_len < result_len)
2275 memset (&result->representation.string[src_len], ' ', result_len - src_len);
2277 result->representation.string[result_len] = '\0'; /* For debugger */
2278 result->representation.length = result_len;
2282 /* Convert Hollerith to integer. The constant will be padded or truncated. */
2284 gfc_expr *
2285 gfc_hollerith2int (gfc_expr *src, int kind)
2287 gfc_expr *result;
2288 result = gfc_get_constant_expr (BT_INTEGER, kind, &src->where);
2290 hollerith2representation (result, src);
2291 gfc_interpret_integer (kind, (unsigned char *) result->representation.string,
2292 result->representation.length, result->value.integer);
2294 return result;
2298 /* Convert Hollerith to real. The constant will be padded or truncated. */
2300 gfc_expr *
2301 gfc_hollerith2real (gfc_expr *src, int kind)
2303 gfc_expr *result;
2304 result = gfc_get_constant_expr (BT_REAL, kind, &src->where);
2306 hollerith2representation (result, src);
2307 gfc_interpret_float (kind, (unsigned char *) result->representation.string,
2308 result->representation.length, result->value.real);
2310 return result;
2314 /* Convert Hollerith to complex. The constant will be padded or truncated. */
2316 gfc_expr *
2317 gfc_hollerith2complex (gfc_expr *src, int kind)
2319 gfc_expr *result;
2320 result = gfc_get_constant_expr (BT_COMPLEX, kind, &src->where);
2322 hollerith2representation (result, src);
2323 gfc_interpret_complex (kind, (unsigned char *) result->representation.string,
2324 result->representation.length, result->value.complex);
2326 return result;
2330 /* Convert Hollerith to character. */
2332 gfc_expr *
2333 gfc_hollerith2character (gfc_expr *src, int kind)
2335 gfc_expr *result;
2337 result = gfc_copy_expr (src);
2338 result->ts.type = BT_CHARACTER;
2339 result->ts.kind = kind;
2341 result->value.character.length = result->representation.length;
2342 result->value.character.string
2343 = gfc_char_to_widechar (result->representation.string);
2345 return result;
2349 /* Convert Hollerith to logical. The constant will be padded or truncated. */
2351 gfc_expr *
2352 gfc_hollerith2logical (gfc_expr *src, int kind)
2354 gfc_expr *result;
2355 result = gfc_get_constant_expr (BT_LOGICAL, kind, &src->where);
2357 hollerith2representation (result, src);
2358 gfc_interpret_logical (kind, (unsigned char *) result->representation.string,
2359 result->representation.length, &result->value.logical);
2361 return result;