* c-common.c (get_priority): Add check for
[official-gcc.git] / gcc / fortran / simplify.c
blob8c6847ba6d714b42f65a6abe9e81f48caade6d2d
1 /* Simplify intrinsic functions at compile-time.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught & Katherine Holcomb
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "flags.h"
26 #include "gfortran.h"
27 #include "arith.h"
28 #include "intrinsic.h"
30 gfc_expr gfc_bad_expr;
33 /* Note that 'simplification' is not just transforming expressions.
34 For functions that are not simplified at compile time, range
35 checking is done if possible.
37 The return convention is that each simplification function returns:
39 A new expression node corresponding to the simplified arguments.
40 The original arguments are destroyed by the caller, and must not
41 be a part of the new expression.
43 NULL pointer indicating that no simplification was possible and
44 the original expression should remain intact. If the
45 simplification function sets the type and/or the function name
46 via the pointer gfc_simple_expression, then this type is
47 retained.
49 An expression pointer to gfc_bad_expr (a static placeholder)
50 indicating that some error has prevented simplification. For
51 example, sqrt(-1.0). The error is generated within the function
52 and should be propagated upwards
54 By the time a simplification function gets control, it has been
55 decided that the function call is really supposed to be the
56 intrinsic. No type checking is strictly necessary, since only
57 valid types will be passed on. On the other hand, a simplification
58 subroutine may have to look at the type of an argument as part of
59 its processing.
61 Array arguments are never passed to these subroutines.
63 The functions in this file don't have much comment with them, but
64 everything is reasonably straight-forward. The Standard, chapter 13
65 is the best comment you'll find for this file anyway. */
67 /* Range checks an expression node. If all goes well, returns the
68 node, otherwise returns &gfc_bad_expr and frees the node. */
70 static gfc_expr *
71 range_check (gfc_expr *result, const char *name)
73 switch (gfc_range_check (result))
75 case ARITH_OK:
76 return result;
78 case ARITH_OVERFLOW:
79 gfc_error ("Result of %s overflows its kind at %L", name,
80 &result->where);
81 break;
83 case ARITH_UNDERFLOW:
84 gfc_error ("Result of %s underflows its kind at %L", name,
85 &result->where);
86 break;
88 case ARITH_NAN:
89 gfc_error ("Result of %s is NaN at %L", name, &result->where);
90 break;
92 default:
93 gfc_error ("Result of %s gives range error for its kind at %L", name,
94 &result->where);
95 break;
98 gfc_free_expr (result);
99 return &gfc_bad_expr;
103 /* A helper function that gets an optional and possibly missing
104 kind parameter. Returns the kind, -1 if something went wrong. */
106 static int
107 get_kind (bt type, gfc_expr *k, const char *name, int default_kind)
109 int kind;
111 if (k == NULL)
112 return default_kind;
114 if (k->expr_type != EXPR_CONSTANT)
116 gfc_error ("KIND parameter of %s at %L must be an initialization "
117 "expression", name, &k->where);
119 return -1;
122 if (gfc_extract_int (k, &kind) != NULL
123 || gfc_validate_kind (type, kind, true) < 0)
126 gfc_error ("Invalid KIND parameter of %s at %L", name, &k->where);
127 return -1;
130 return kind;
134 /* Converts an mpz_t signed variable into an unsigned one, assuming
135 two's complement representations and a binary width of bitsize.
136 The conversion is a no-op unless x is negative; otherwise, it can
137 be accomplished by masking out the high bits. */
139 static void
140 convert_mpz_to_unsigned (mpz_t x, int bitsize)
142 mpz_t mask;
144 if (mpz_sgn (x) < 0)
146 /* Confirm that no bits above the signed range are unset. */
147 gcc_assert (mpz_scan0 (x, bitsize-1) == ULONG_MAX);
149 mpz_init_set_ui (mask, 1);
150 mpz_mul_2exp (mask, mask, bitsize);
151 mpz_sub_ui (mask, mask, 1);
153 mpz_and (x, x, mask);
155 mpz_clear (mask);
157 else
159 /* Confirm that no bits above the signed range are set. */
160 gcc_assert (mpz_scan1 (x, bitsize-1) == ULONG_MAX);
165 /* Converts an mpz_t unsigned variable into a signed one, assuming
166 two's complement representations and a binary width of bitsize.
167 If the bitsize-1 bit is set, this is taken as a sign bit and
168 the number is converted to the corresponding negative number. */
170 static void
171 convert_mpz_to_signed (mpz_t x, int bitsize)
173 mpz_t mask;
175 /* Confirm that no bits above the unsigned range are set. */
176 gcc_assert (mpz_scan1 (x, bitsize) == ULONG_MAX);
178 if (mpz_tstbit (x, bitsize - 1) == 1)
180 mpz_init_set_ui (mask, 1);
181 mpz_mul_2exp (mask, mask, bitsize);
182 mpz_sub_ui (mask, mask, 1);
184 /* We negate the number by hand, zeroing the high bits, that is
185 make it the corresponding positive number, and then have it
186 negated by GMP, giving the correct representation of the
187 negative number. */
188 mpz_com (x, x);
189 mpz_add_ui (x, x, 1);
190 mpz_and (x, x, mask);
192 mpz_neg (x, x);
194 mpz_clear (mask);
199 /********************** Simplification functions *****************************/
201 gfc_expr *
202 gfc_simplify_abs (gfc_expr *e)
204 gfc_expr *result;
206 if (e->expr_type != EXPR_CONSTANT)
207 return NULL;
209 switch (e->ts.type)
211 case BT_INTEGER:
212 result = gfc_constant_result (BT_INTEGER, e->ts.kind, &e->where);
214 mpz_abs (result->value.integer, e->value.integer);
216 result = range_check (result, "IABS");
217 break;
219 case BT_REAL:
220 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
222 mpfr_abs (result->value.real, e->value.real, GFC_RND_MODE);
224 result = range_check (result, "ABS");
225 break;
227 case BT_COMPLEX:
228 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
230 gfc_set_model_kind (e->ts.kind);
232 mpfr_hypot (result->value.real, e->value.complex.r,
233 e->value.complex.i, GFC_RND_MODE);
234 result = range_check (result, "CABS");
235 break;
237 default:
238 gfc_internal_error ("gfc_simplify_abs(): Bad type");
241 return result;
244 /* We use the processor's collating sequence, because all
245 systems that gfortran currently works on are ASCII. */
247 gfc_expr *
248 gfc_simplify_achar (gfc_expr *e)
250 gfc_expr *result;
251 int c;
252 const char *ch;
254 if (e->expr_type != EXPR_CONSTANT)
255 return NULL;
257 ch = gfc_extract_int (e, &c);
259 if (ch != NULL)
260 gfc_internal_error ("gfc_simplify_achar: %s", ch);
262 if (gfc_option.warn_surprising && (c < 0 || c > 127))
263 gfc_warning ("Argument of ACHAR function at %L outside of range [0,127]",
264 &e->where);
266 result = gfc_constant_result (BT_CHARACTER, gfc_default_character_kind,
267 &e->where);
269 result->value.character.string = gfc_getmem (2);
271 result->value.character.length = 1;
272 result->value.character.string[0] = c;
273 result->value.character.string[1] = '\0'; /* For debugger */
274 return result;
278 gfc_expr *
279 gfc_simplify_acos (gfc_expr *x)
281 gfc_expr *result;
283 if (x->expr_type != EXPR_CONSTANT)
284 return NULL;
286 if (mpfr_cmp_si (x->value.real, 1) > 0
287 || mpfr_cmp_si (x->value.real, -1) < 0)
289 gfc_error ("Argument of ACOS at %L must be between -1 and 1",
290 &x->where);
291 return &gfc_bad_expr;
294 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
296 mpfr_acos (result->value.real, x->value.real, GFC_RND_MODE);
298 return range_check (result, "ACOS");
301 gfc_expr *
302 gfc_simplify_acosh (gfc_expr *x)
304 gfc_expr *result;
306 if (x->expr_type != EXPR_CONSTANT)
307 return NULL;
309 if (mpfr_cmp_si (x->value.real, 1) < 0)
311 gfc_error ("Argument of ACOSH at %L must not be less than 1",
312 &x->where);
313 return &gfc_bad_expr;
316 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
318 mpfr_acosh (result->value.real, x->value.real, GFC_RND_MODE);
320 return range_check (result, "ACOSH");
323 gfc_expr *
324 gfc_simplify_adjustl (gfc_expr *e)
326 gfc_expr *result;
327 int count, i, len;
328 char ch;
330 if (e->expr_type != EXPR_CONSTANT)
331 return NULL;
333 len = e->value.character.length;
335 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
337 result->value.character.length = len;
338 result->value.character.string = gfc_getmem (len + 1);
340 for (count = 0, i = 0; i < len; ++i)
342 ch = e->value.character.string[i];
343 if (ch != ' ')
344 break;
345 ++count;
348 for (i = 0; i < len - count; ++i)
349 result->value.character.string[i] = e->value.character.string[count + i];
351 for (i = len - count; i < len; ++i)
352 result->value.character.string[i] = ' ';
354 result->value.character.string[len] = '\0'; /* For debugger */
356 return result;
360 gfc_expr *
361 gfc_simplify_adjustr (gfc_expr *e)
363 gfc_expr *result;
364 int count, i, len;
365 char ch;
367 if (e->expr_type != EXPR_CONSTANT)
368 return NULL;
370 len = e->value.character.length;
372 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
374 result->value.character.length = len;
375 result->value.character.string = gfc_getmem (len + 1);
377 for (count = 0, i = len - 1; i >= 0; --i)
379 ch = e->value.character.string[i];
380 if (ch != ' ')
381 break;
382 ++count;
385 for (i = 0; i < count; ++i)
386 result->value.character.string[i] = ' ';
388 for (i = count; i < len; ++i)
389 result->value.character.string[i] = e->value.character.string[i - count];
391 result->value.character.string[len] = '\0'; /* For debugger */
393 return result;
397 gfc_expr *
398 gfc_simplify_aimag (gfc_expr *e)
400 gfc_expr *result;
402 if (e->expr_type != EXPR_CONSTANT)
403 return NULL;
405 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
406 mpfr_set (result->value.real, e->value.complex.i, GFC_RND_MODE);
408 return range_check (result, "AIMAG");
412 gfc_expr *
413 gfc_simplify_aint (gfc_expr *e, gfc_expr *k)
415 gfc_expr *rtrunc, *result;
416 int kind;
418 kind = get_kind (BT_REAL, k, "AINT", e->ts.kind);
419 if (kind == -1)
420 return &gfc_bad_expr;
422 if (e->expr_type != EXPR_CONSTANT)
423 return NULL;
425 rtrunc = gfc_copy_expr (e);
427 mpfr_trunc (rtrunc->value.real, e->value.real);
429 result = gfc_real2real (rtrunc, kind);
430 gfc_free_expr (rtrunc);
432 return range_check (result, "AINT");
436 gfc_expr *
437 gfc_simplify_dint (gfc_expr *e)
439 gfc_expr *rtrunc, *result;
441 if (e->expr_type != EXPR_CONSTANT)
442 return NULL;
444 rtrunc = gfc_copy_expr (e);
446 mpfr_trunc (rtrunc->value.real, e->value.real);
448 result = gfc_real2real (rtrunc, gfc_default_double_kind);
449 gfc_free_expr (rtrunc);
451 return range_check (result, "DINT");
455 gfc_expr *
456 gfc_simplify_anint (gfc_expr *e, gfc_expr *k)
458 gfc_expr *result;
459 int kind;
461 kind = get_kind (BT_REAL, k, "ANINT", e->ts.kind);
462 if (kind == -1)
463 return &gfc_bad_expr;
465 if (e->expr_type != EXPR_CONSTANT)
466 return NULL;
468 result = gfc_constant_result (e->ts.type, kind, &e->where);
470 mpfr_round (result->value.real, e->value.real);
472 return range_check (result, "ANINT");
476 gfc_expr *
477 gfc_simplify_and (gfc_expr *x, gfc_expr *y)
479 gfc_expr *result;
480 int kind;
482 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
483 return NULL;
485 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
486 if (x->ts.type == BT_INTEGER)
488 result = gfc_constant_result (BT_INTEGER, kind, &x->where);
489 mpz_and (result->value.integer, x->value.integer, y->value.integer);
491 else /* BT_LOGICAL */
493 result = gfc_constant_result (BT_LOGICAL, kind, &x->where);
494 result->value.logical = x->value.logical && y->value.logical;
497 return range_check (result, "AND");
501 gfc_expr *
502 gfc_simplify_dnint (gfc_expr *e)
504 gfc_expr *result;
506 if (e->expr_type != EXPR_CONSTANT)
507 return NULL;
509 result = gfc_constant_result (BT_REAL, gfc_default_double_kind, &e->where);
511 mpfr_round (result->value.real, e->value.real);
513 return range_check (result, "DNINT");
517 gfc_expr *
518 gfc_simplify_asin (gfc_expr *x)
520 gfc_expr *result;
522 if (x->expr_type != EXPR_CONSTANT)
523 return NULL;
525 if (mpfr_cmp_si (x->value.real, 1) > 0
526 || mpfr_cmp_si (x->value.real, -1) < 0)
528 gfc_error ("Argument of ASIN at %L must be between -1 and 1",
529 &x->where);
530 return &gfc_bad_expr;
533 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
535 mpfr_asin (result->value.real, x->value.real, GFC_RND_MODE);
537 return range_check (result, "ASIN");
541 gfc_expr *
542 gfc_simplify_asinh (gfc_expr *x)
544 gfc_expr *result;
546 if (x->expr_type != EXPR_CONSTANT)
547 return NULL;
549 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
551 mpfr_asinh (result->value.real, x->value.real, GFC_RND_MODE);
553 return range_check (result, "ASINH");
557 gfc_expr *
558 gfc_simplify_atan (gfc_expr *x)
560 gfc_expr *result;
562 if (x->expr_type != EXPR_CONSTANT)
563 return NULL;
565 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
567 mpfr_atan (result->value.real, x->value.real, GFC_RND_MODE);
569 return range_check (result, "ATAN");
573 gfc_expr *
574 gfc_simplify_atanh (gfc_expr *x)
576 gfc_expr *result;
578 if (x->expr_type != EXPR_CONSTANT)
579 return NULL;
581 if (mpfr_cmp_si (x->value.real, 1) >= 0
582 || mpfr_cmp_si (x->value.real, -1) <= 0)
584 gfc_error ("Argument of ATANH at %L must be inside the range -1 to 1",
585 &x->where);
586 return &gfc_bad_expr;
589 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
591 mpfr_atanh (result->value.real, x->value.real, GFC_RND_MODE);
593 return range_check (result, "ATANH");
597 gfc_expr *
598 gfc_simplify_atan2 (gfc_expr *y, gfc_expr *x)
600 gfc_expr *result;
602 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
603 return NULL;
605 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
607 if (mpfr_sgn (y->value.real) == 0 && mpfr_sgn (x->value.real) == 0)
609 gfc_error ("If first argument of ATAN2 %L is zero, then the "
610 "second argument must not be zero", &x->where);
611 gfc_free_expr (result);
612 return &gfc_bad_expr;
615 mpfr_atan2 (result->value.real, y->value.real, x->value.real, GFC_RND_MODE);
617 return range_check (result, "ATAN2");
621 gfc_expr *
622 gfc_simplify_bit_size (gfc_expr *e)
624 gfc_expr *result;
625 int i;
627 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
628 result = gfc_constant_result (BT_INTEGER, e->ts.kind, &e->where);
629 mpz_set_ui (result->value.integer, gfc_integer_kinds[i].bit_size);
631 return result;
635 gfc_expr *
636 gfc_simplify_btest (gfc_expr *e, gfc_expr *bit)
638 int b;
640 if (e->expr_type != EXPR_CONSTANT || bit->expr_type != EXPR_CONSTANT)
641 return NULL;
643 if (gfc_extract_int (bit, &b) != NULL || b < 0)
644 return gfc_logical_expr (0, &e->where);
646 return gfc_logical_expr (mpz_tstbit (e->value.integer, b), &e->where);
650 gfc_expr *
651 gfc_simplify_ceiling (gfc_expr *e, gfc_expr *k)
653 gfc_expr *ceil, *result;
654 int kind;
656 kind = get_kind (BT_INTEGER, k, "CEILING", gfc_default_integer_kind);
657 if (kind == -1)
658 return &gfc_bad_expr;
660 if (e->expr_type != EXPR_CONSTANT)
661 return NULL;
663 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
665 ceil = gfc_copy_expr (e);
667 mpfr_ceil (ceil->value.real, e->value.real);
668 gfc_mpfr_to_mpz (result->value.integer, ceil->value.real);
670 gfc_free_expr (ceil);
672 return range_check (result, "CEILING");
676 gfc_expr *
677 gfc_simplify_char (gfc_expr *e, gfc_expr *k)
679 gfc_expr *result;
680 int c, kind;
681 const char *ch;
683 kind = get_kind (BT_CHARACTER, k, "CHAR", gfc_default_character_kind);
684 if (kind == -1)
685 return &gfc_bad_expr;
687 if (e->expr_type != EXPR_CONSTANT)
688 return NULL;
690 ch = gfc_extract_int (e, &c);
692 if (ch != NULL)
693 gfc_internal_error ("gfc_simplify_char: %s", ch);
695 if (c < 0 || c > UCHAR_MAX)
696 gfc_error ("Argument of CHAR function at %L outside of range [0,255]",
697 &e->where);
699 result = gfc_constant_result (BT_CHARACTER, kind, &e->where);
701 result->value.character.length = 1;
702 result->value.character.string = gfc_getmem (2);
704 result->value.character.string[0] = c;
705 result->value.character.string[1] = '\0'; /* For debugger */
707 return result;
711 /* Common subroutine for simplifying CMPLX and DCMPLX. */
713 static gfc_expr *
714 simplify_cmplx (const char *name, gfc_expr *x, gfc_expr *y, int kind)
716 gfc_expr *result;
718 result = gfc_constant_result (BT_COMPLEX, kind, &x->where);
720 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
722 switch (x->ts.type)
724 case BT_INTEGER:
725 mpfr_set_z (result->value.complex.r, x->value.integer, GFC_RND_MODE);
726 break;
728 case BT_REAL:
729 mpfr_set (result->value.complex.r, x->value.real, GFC_RND_MODE);
730 break;
732 case BT_COMPLEX:
733 mpfr_set (result->value.complex.r, x->value.complex.r, GFC_RND_MODE);
734 mpfr_set (result->value.complex.i, x->value.complex.i, GFC_RND_MODE);
735 break;
737 default:
738 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (x)");
741 if (y != NULL)
743 switch (y->ts.type)
745 case BT_INTEGER:
746 mpfr_set_z (result->value.complex.i, y->value.integer, GFC_RND_MODE);
747 break;
749 case BT_REAL:
750 mpfr_set (result->value.complex.i, y->value.real, GFC_RND_MODE);
751 break;
753 default:
754 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (y)");
758 return range_check (result, name);
762 gfc_expr *
763 gfc_simplify_cmplx (gfc_expr *x, gfc_expr *y, gfc_expr *k)
765 int kind;
767 if (x->expr_type != EXPR_CONSTANT
768 || (y != NULL && y->expr_type != EXPR_CONSTANT))
769 return NULL;
771 kind = get_kind (BT_REAL, k, "CMPLX", gfc_default_real_kind);
772 if (kind == -1)
773 return &gfc_bad_expr;
775 return simplify_cmplx ("CMPLX", x, y, kind);
779 gfc_expr *
780 gfc_simplify_complex (gfc_expr *x, gfc_expr *y)
782 int kind;
784 if (x->expr_type != EXPR_CONSTANT
785 || (y != NULL && y->expr_type != EXPR_CONSTANT))
786 return NULL;
788 if (x->ts.type == BT_INTEGER)
790 if (y->ts.type == BT_INTEGER)
791 kind = gfc_default_real_kind;
792 else
793 kind = y->ts.kind;
795 else
797 if (y->ts.type == BT_REAL)
798 kind = (x->ts.kind > y->ts.kind) ? x->ts.kind : y->ts.kind;
799 else
800 kind = x->ts.kind;
803 return simplify_cmplx ("COMPLEX", x, y, kind);
807 gfc_expr *
808 gfc_simplify_conjg (gfc_expr *e)
810 gfc_expr *result;
812 if (e->expr_type != EXPR_CONSTANT)
813 return NULL;
815 result = gfc_copy_expr (e);
816 mpfr_neg (result->value.complex.i, result->value.complex.i, GFC_RND_MODE);
818 return range_check (result, "CONJG");
822 gfc_expr *
823 gfc_simplify_cos (gfc_expr *x)
825 gfc_expr *result;
826 mpfr_t xp, xq;
828 if (x->expr_type != EXPR_CONSTANT)
829 return NULL;
831 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
833 switch (x->ts.type)
835 case BT_REAL:
836 mpfr_cos (result->value.real, x->value.real, GFC_RND_MODE);
837 break;
838 case BT_COMPLEX:
839 gfc_set_model_kind (x->ts.kind);
840 mpfr_init (xp);
841 mpfr_init (xq);
843 mpfr_cos (xp, x->value.complex.r, GFC_RND_MODE);
844 mpfr_cosh (xq, x->value.complex.i, GFC_RND_MODE);
845 mpfr_mul (result->value.complex.r, xp, xq, GFC_RND_MODE);
847 mpfr_sin (xp, x->value.complex.r, GFC_RND_MODE);
848 mpfr_sinh (xq, x->value.complex.i, GFC_RND_MODE);
849 mpfr_mul (xp, xp, xq, GFC_RND_MODE);
850 mpfr_neg (result->value.complex.i, xp, GFC_RND_MODE );
852 mpfr_clear (xp);
853 mpfr_clear (xq);
854 break;
855 default:
856 gfc_internal_error ("in gfc_simplify_cos(): Bad type");
859 return range_check (result, "COS");
864 gfc_expr *
865 gfc_simplify_cosh (gfc_expr *x)
867 gfc_expr *result;
869 if (x->expr_type != EXPR_CONSTANT)
870 return NULL;
872 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
874 mpfr_cosh (result->value.real, x->value.real, GFC_RND_MODE);
876 return range_check (result, "COSH");
880 gfc_expr *
881 gfc_simplify_dcmplx (gfc_expr *x, gfc_expr *y)
884 if (x->expr_type != EXPR_CONSTANT
885 || (y != NULL && y->expr_type != EXPR_CONSTANT))
886 return NULL;
888 return simplify_cmplx ("DCMPLX", x, y, gfc_default_double_kind);
892 gfc_expr *
893 gfc_simplify_dble (gfc_expr *e)
895 gfc_expr *result;
897 if (e->expr_type != EXPR_CONSTANT)
898 return NULL;
900 switch (e->ts.type)
902 case BT_INTEGER:
903 result = gfc_int2real (e, gfc_default_double_kind);
904 break;
906 case BT_REAL:
907 result = gfc_real2real (e, gfc_default_double_kind);
908 break;
910 case BT_COMPLEX:
911 result = gfc_complex2real (e, gfc_default_double_kind);
912 break;
914 default:
915 gfc_internal_error ("gfc_simplify_dble(): bad type at %L", &e->where);
918 return range_check (result, "DBLE");
922 gfc_expr *
923 gfc_simplify_digits (gfc_expr *x)
925 int i, digits;
927 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
928 switch (x->ts.type)
930 case BT_INTEGER:
931 digits = gfc_integer_kinds[i].digits;
932 break;
934 case BT_REAL:
935 case BT_COMPLEX:
936 digits = gfc_real_kinds[i].digits;
937 break;
939 default:
940 gcc_unreachable ();
943 return gfc_int_expr (digits);
947 gfc_expr *
948 gfc_simplify_dim (gfc_expr *x, gfc_expr *y)
950 gfc_expr *result;
951 int kind;
953 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
954 return NULL;
956 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
957 result = gfc_constant_result (x->ts.type, kind, &x->where);
959 switch (x->ts.type)
961 case BT_INTEGER:
962 if (mpz_cmp (x->value.integer, y->value.integer) > 0)
963 mpz_sub (result->value.integer, x->value.integer, y->value.integer);
964 else
965 mpz_set_ui (result->value.integer, 0);
967 break;
969 case BT_REAL:
970 if (mpfr_cmp (x->value.real, y->value.real) > 0)
971 mpfr_sub (result->value.real, x->value.real, y->value.real,
972 GFC_RND_MODE);
973 else
974 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
976 break;
978 default:
979 gfc_internal_error ("gfc_simplify_dim(): Bad type");
982 return range_check (result, "DIM");
986 gfc_expr *
987 gfc_simplify_dprod (gfc_expr *x, gfc_expr *y)
989 gfc_expr *a1, *a2, *result;
991 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
992 return NULL;
994 result = gfc_constant_result (BT_REAL, gfc_default_double_kind, &x->where);
996 a1 = gfc_real2real (x, gfc_default_double_kind);
997 a2 = gfc_real2real (y, gfc_default_double_kind);
999 mpfr_mul (result->value.real, a1->value.real, a2->value.real, GFC_RND_MODE);
1001 gfc_free_expr (a1);
1002 gfc_free_expr (a2);
1004 return range_check (result, "DPROD");
1008 gfc_expr *
1009 gfc_simplify_epsilon (gfc_expr *e)
1011 gfc_expr *result;
1012 int i;
1014 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
1016 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
1018 mpfr_set (result->value.real, gfc_real_kinds[i].epsilon, GFC_RND_MODE);
1020 return range_check (result, "EPSILON");
1024 gfc_expr *
1025 gfc_simplify_exp (gfc_expr *x)
1027 gfc_expr *result;
1028 mpfr_t xp, xq;
1030 if (x->expr_type != EXPR_CONSTANT)
1031 return NULL;
1033 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1035 switch (x->ts.type)
1037 case BT_REAL:
1038 mpfr_exp (result->value.real, x->value.real, GFC_RND_MODE);
1039 break;
1041 case BT_COMPLEX:
1042 gfc_set_model_kind (x->ts.kind);
1043 mpfr_init (xp);
1044 mpfr_init (xq);
1045 mpfr_exp (xq, x->value.complex.r, GFC_RND_MODE);
1046 mpfr_cos (xp, x->value.complex.i, GFC_RND_MODE);
1047 mpfr_mul (result->value.complex.r, xq, xp, GFC_RND_MODE);
1048 mpfr_sin (xp, x->value.complex.i, GFC_RND_MODE);
1049 mpfr_mul (result->value.complex.i, xq, xp, GFC_RND_MODE);
1050 mpfr_clear (xp);
1051 mpfr_clear (xq);
1052 break;
1054 default:
1055 gfc_internal_error ("in gfc_simplify_exp(): Bad type");
1058 return range_check (result, "EXP");
1061 gfc_expr *
1062 gfc_simplify_exponent (gfc_expr *x)
1064 int i;
1065 gfc_expr *result;
1067 if (x->expr_type != EXPR_CONSTANT)
1068 return NULL;
1070 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
1071 &x->where);
1073 gfc_set_model (x->value.real);
1075 if (mpfr_sgn (x->value.real) == 0)
1077 mpz_set_ui (result->value.integer, 0);
1078 return result;
1081 i = (int) mpfr_get_exp (x->value.real);
1082 mpz_set_si (result->value.integer, i);
1084 return range_check (result, "EXPONENT");
1088 gfc_expr *
1089 gfc_simplify_float (gfc_expr *a)
1091 gfc_expr *result;
1093 if (a->expr_type != EXPR_CONSTANT)
1094 return NULL;
1096 result = gfc_int2real (a, gfc_default_real_kind);
1097 return range_check (result, "FLOAT");
1101 gfc_expr *
1102 gfc_simplify_floor (gfc_expr *e, gfc_expr *k)
1104 gfc_expr *result;
1105 mpfr_t floor;
1106 int kind;
1108 kind = get_kind (BT_INTEGER, k, "FLOOR", gfc_default_integer_kind);
1109 if (kind == -1)
1110 gfc_internal_error ("gfc_simplify_floor(): Bad kind");
1112 if (e->expr_type != EXPR_CONSTANT)
1113 return NULL;
1115 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
1117 gfc_set_model_kind (kind);
1118 mpfr_init (floor);
1119 mpfr_floor (floor, e->value.real);
1121 gfc_mpfr_to_mpz (result->value.integer, floor);
1123 mpfr_clear (floor);
1125 return range_check (result, "FLOOR");
1129 gfc_expr *
1130 gfc_simplify_fraction (gfc_expr *x)
1132 gfc_expr *result;
1133 mpfr_t absv, exp, pow2;
1135 if (x->expr_type != EXPR_CONSTANT)
1136 return NULL;
1138 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
1140 gfc_set_model_kind (x->ts.kind);
1142 if (mpfr_sgn (x->value.real) == 0)
1144 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
1145 return result;
1148 mpfr_init (exp);
1149 mpfr_init (absv);
1150 mpfr_init (pow2);
1152 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
1153 mpfr_log2 (exp, absv, GFC_RND_MODE);
1155 mpfr_trunc (exp, exp);
1156 mpfr_add_ui (exp, exp, 1, GFC_RND_MODE);
1158 mpfr_ui_pow (pow2, 2, exp, GFC_RND_MODE);
1160 mpfr_div (result->value.real, absv, pow2, GFC_RND_MODE);
1162 mpfr_clear (exp);
1163 mpfr_clear (absv);
1164 mpfr_clear (pow2);
1166 return range_check (result, "FRACTION");
1170 gfc_expr *
1171 gfc_simplify_huge (gfc_expr *e)
1173 gfc_expr *result;
1174 int i;
1176 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
1178 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
1180 switch (e->ts.type)
1182 case BT_INTEGER:
1183 mpz_set (result->value.integer, gfc_integer_kinds[i].huge);
1184 break;
1186 case BT_REAL:
1187 mpfr_set (result->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
1188 break;
1190 default:
1191 gcc_unreachable ();
1194 return result;
1197 /* We use the processor's collating sequence, because all
1198 systems that gfortran currently works on are ASCII. */
1200 gfc_expr *
1201 gfc_simplify_iachar (gfc_expr *e)
1203 gfc_expr *result;
1204 int index;
1206 if (e->expr_type != EXPR_CONSTANT)
1207 return NULL;
1209 if (e->value.character.length != 1)
1211 gfc_error ("Argument of IACHAR at %L must be of length one", &e->where);
1212 return &gfc_bad_expr;
1215 index = (unsigned char) e->value.character.string[0];
1217 if (gfc_option.warn_surprising && index > 127)
1218 gfc_warning ("Argument of IACHAR function at %L outside of range 0..127",
1219 &e->where);
1221 result = gfc_int_expr (index);
1222 result->where = e->where;
1224 return range_check (result, "IACHAR");
1228 gfc_expr *
1229 gfc_simplify_iand (gfc_expr *x, gfc_expr *y)
1231 gfc_expr *result;
1233 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1234 return NULL;
1236 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
1238 mpz_and (result->value.integer, x->value.integer, y->value.integer);
1240 return range_check (result, "IAND");
1244 gfc_expr *
1245 gfc_simplify_ibclr (gfc_expr *x, gfc_expr *y)
1247 gfc_expr *result;
1248 int k, pos;
1250 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1251 return NULL;
1253 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
1255 gfc_error ("Invalid second argument of IBCLR at %L", &y->where);
1256 return &gfc_bad_expr;
1259 k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
1261 if (pos >= gfc_integer_kinds[k].bit_size)
1263 gfc_error ("Second argument of IBCLR exceeds bit size at %L",
1264 &y->where);
1265 return &gfc_bad_expr;
1268 result = gfc_copy_expr (x);
1270 convert_mpz_to_unsigned (result->value.integer,
1271 gfc_integer_kinds[k].bit_size);
1273 mpz_clrbit (result->value.integer, pos);
1275 convert_mpz_to_signed (result->value.integer,
1276 gfc_integer_kinds[k].bit_size);
1278 return range_check (result, "IBCLR");
1282 gfc_expr *
1283 gfc_simplify_ibits (gfc_expr *x, gfc_expr *y, gfc_expr *z)
1285 gfc_expr *result;
1286 int pos, len;
1287 int i, k, bitsize;
1288 int *bits;
1290 if (x->expr_type != EXPR_CONSTANT
1291 || y->expr_type != EXPR_CONSTANT
1292 || z->expr_type != EXPR_CONSTANT)
1293 return NULL;
1295 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
1297 gfc_error ("Invalid second argument of IBITS at %L", &y->where);
1298 return &gfc_bad_expr;
1301 if (gfc_extract_int (z, &len) != NULL || len < 0)
1303 gfc_error ("Invalid third argument of IBITS at %L", &z->where);
1304 return &gfc_bad_expr;
1307 k = gfc_validate_kind (BT_INTEGER, x->ts.kind, false);
1309 bitsize = gfc_integer_kinds[k].bit_size;
1311 if (pos + len > bitsize)
1313 gfc_error ("Sum of second and third arguments of IBITS exceeds "
1314 "bit size at %L", &y->where);
1315 return &gfc_bad_expr;
1318 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1320 bits = gfc_getmem (bitsize * sizeof (int));
1322 for (i = 0; i < bitsize; i++)
1323 bits[i] = 0;
1325 for (i = 0; i < len; i++)
1326 bits[i] = mpz_tstbit (x->value.integer, i + pos);
1328 for (i = 0; i < bitsize; i++)
1330 if (bits[i] == 0)
1331 mpz_clrbit (result->value.integer, i);
1332 else if (bits[i] == 1)
1333 mpz_setbit (result->value.integer, i);
1334 else
1335 gfc_internal_error ("IBITS: Bad bit");
1338 gfc_free (bits);
1340 return range_check (result, "IBITS");
1344 gfc_expr *
1345 gfc_simplify_ibset (gfc_expr *x, gfc_expr *y)
1347 gfc_expr *result;
1348 int k, pos;
1350 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1351 return NULL;
1353 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
1355 gfc_error ("Invalid second argument of IBSET at %L", &y->where);
1356 return &gfc_bad_expr;
1359 k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
1361 if (pos >= gfc_integer_kinds[k].bit_size)
1363 gfc_error ("Second argument of IBSET exceeds bit size at %L",
1364 &y->where);
1365 return &gfc_bad_expr;
1368 result = gfc_copy_expr (x);
1370 convert_mpz_to_unsigned (result->value.integer,
1371 gfc_integer_kinds[k].bit_size);
1373 mpz_setbit (result->value.integer, pos);
1375 convert_mpz_to_signed (result->value.integer,
1376 gfc_integer_kinds[k].bit_size);
1378 return range_check (result, "IBSET");
1382 gfc_expr *
1383 gfc_simplify_ichar (gfc_expr *e)
1385 gfc_expr *result;
1386 int index;
1388 if (e->expr_type != EXPR_CONSTANT)
1389 return NULL;
1391 if (e->value.character.length != 1)
1393 gfc_error ("Argument of ICHAR at %L must be of length one", &e->where);
1394 return &gfc_bad_expr;
1397 index = (unsigned char) e->value.character.string[0];
1399 if (index < 0 || index > UCHAR_MAX)
1400 gfc_internal_error("Argument of ICHAR at %L out of range", &e->where);
1402 result = gfc_int_expr (index);
1403 result->where = e->where;
1404 return range_check (result, "ICHAR");
1408 gfc_expr *
1409 gfc_simplify_ieor (gfc_expr *x, gfc_expr *y)
1411 gfc_expr *result;
1413 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1414 return NULL;
1416 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
1418 mpz_xor (result->value.integer, x->value.integer, y->value.integer);
1420 return range_check (result, "IEOR");
1424 gfc_expr *
1425 gfc_simplify_index (gfc_expr *x, gfc_expr *y, gfc_expr *b)
1427 gfc_expr *result;
1428 int back, len, lensub;
1429 int i, j, k, count, index = 0, start;
1431 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1432 return NULL;
1434 if (b != NULL && b->value.logical != 0)
1435 back = 1;
1436 else
1437 back = 0;
1439 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
1440 &x->where);
1442 len = x->value.character.length;
1443 lensub = y->value.character.length;
1445 if (len < lensub)
1447 mpz_set_si (result->value.integer, 0);
1448 return result;
1451 if (back == 0)
1453 if (lensub == 0)
1455 mpz_set_si (result->value.integer, 1);
1456 return result;
1458 else if (lensub == 1)
1460 for (i = 0; i < len; i++)
1462 for (j = 0; j < lensub; j++)
1464 if (y->value.character.string[j]
1465 == x->value.character.string[i])
1467 index = i + 1;
1468 goto done;
1473 else
1475 for (i = 0; i < len; i++)
1477 for (j = 0; j < lensub; j++)
1479 if (y->value.character.string[j]
1480 == x->value.character.string[i])
1482 start = i;
1483 count = 0;
1485 for (k = 0; k < lensub; k++)
1487 if (y->value.character.string[k]
1488 == x->value.character.string[k + start])
1489 count++;
1492 if (count == lensub)
1494 index = start + 1;
1495 goto done;
1503 else
1505 if (lensub == 0)
1507 mpz_set_si (result->value.integer, len + 1);
1508 return result;
1510 else if (lensub == 1)
1512 for (i = 0; i < len; i++)
1514 for (j = 0; j < lensub; j++)
1516 if (y->value.character.string[j]
1517 == x->value.character.string[len - i])
1519 index = len - i + 1;
1520 goto done;
1525 else
1527 for (i = 0; i < len; i++)
1529 for (j = 0; j < lensub; j++)
1531 if (y->value.character.string[j]
1532 == x->value.character.string[len - i])
1534 start = len - i;
1535 if (start <= len - lensub)
1537 count = 0;
1538 for (k = 0; k < lensub; k++)
1539 if (y->value.character.string[k]
1540 == x->value.character.string[k + start])
1541 count++;
1543 if (count == lensub)
1545 index = start + 1;
1546 goto done;
1549 else
1551 continue;
1559 done:
1560 mpz_set_si (result->value.integer, index);
1561 return range_check (result, "INDEX");
1565 gfc_expr *
1566 gfc_simplify_int (gfc_expr *e, gfc_expr *k)
1568 gfc_expr *rpart, *rtrunc, *result;
1569 int kind;
1571 kind = get_kind (BT_INTEGER, k, "INT", gfc_default_integer_kind);
1572 if (kind == -1)
1573 return &gfc_bad_expr;
1575 if (e->expr_type != EXPR_CONSTANT)
1576 return NULL;
1578 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
1580 switch (e->ts.type)
1582 case BT_INTEGER:
1583 mpz_set (result->value.integer, e->value.integer);
1584 break;
1586 case BT_REAL:
1587 rtrunc = gfc_copy_expr (e);
1588 mpfr_trunc (rtrunc->value.real, e->value.real);
1589 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1590 gfc_free_expr (rtrunc);
1591 break;
1593 case BT_COMPLEX:
1594 rpart = gfc_complex2real (e, kind);
1595 rtrunc = gfc_copy_expr (rpart);
1596 mpfr_trunc (rtrunc->value.real, rpart->value.real);
1597 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1598 gfc_free_expr (rpart);
1599 gfc_free_expr (rtrunc);
1600 break;
1602 default:
1603 gfc_error ("Argument of INT at %L is not a valid type", &e->where);
1604 gfc_free_expr (result);
1605 return &gfc_bad_expr;
1608 return range_check (result, "INT");
1612 static gfc_expr *
1613 gfc_simplify_intconv (gfc_expr *e, int kind, const char *name)
1615 gfc_expr *rpart, *rtrunc, *result;
1617 if (e->expr_type != EXPR_CONSTANT)
1618 return NULL;
1620 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
1622 switch (e->ts.type)
1624 case BT_INTEGER:
1625 mpz_set (result->value.integer, e->value.integer);
1626 break;
1628 case BT_REAL:
1629 rtrunc = gfc_copy_expr (e);
1630 mpfr_trunc (rtrunc->value.real, e->value.real);
1631 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1632 gfc_free_expr (rtrunc);
1633 break;
1635 case BT_COMPLEX:
1636 rpart = gfc_complex2real (e, kind);
1637 rtrunc = gfc_copy_expr (rpart);
1638 mpfr_trunc (rtrunc->value.real, rpart->value.real);
1639 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1640 gfc_free_expr (rpart);
1641 gfc_free_expr (rtrunc);
1642 break;
1644 default:
1645 gfc_error ("Argument of %s at %L is not a valid type", name, &e->where);
1646 gfc_free_expr (result);
1647 return &gfc_bad_expr;
1650 return range_check (result, name);
1654 gfc_expr *
1655 gfc_simplify_int2 (gfc_expr *e)
1657 return gfc_simplify_intconv (e, 2, "INT2");
1661 gfc_expr *
1662 gfc_simplify_int8 (gfc_expr *e)
1664 return gfc_simplify_intconv (e, 8, "INT8");
1668 gfc_expr *
1669 gfc_simplify_long (gfc_expr *e)
1671 return gfc_simplify_intconv (e, 4, "LONG");
1675 gfc_expr *
1676 gfc_simplify_ifix (gfc_expr *e)
1678 gfc_expr *rtrunc, *result;
1680 if (e->expr_type != EXPR_CONSTANT)
1681 return NULL;
1683 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
1684 &e->where);
1686 rtrunc = gfc_copy_expr (e);
1688 mpfr_trunc (rtrunc->value.real, e->value.real);
1689 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1691 gfc_free_expr (rtrunc);
1692 return range_check (result, "IFIX");
1696 gfc_expr *
1697 gfc_simplify_idint (gfc_expr *e)
1699 gfc_expr *rtrunc, *result;
1701 if (e->expr_type != EXPR_CONSTANT)
1702 return NULL;
1704 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
1705 &e->where);
1707 rtrunc = gfc_copy_expr (e);
1709 mpfr_trunc (rtrunc->value.real, e->value.real);
1710 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1712 gfc_free_expr (rtrunc);
1713 return range_check (result, "IDINT");
1717 gfc_expr *
1718 gfc_simplify_ior (gfc_expr *x, gfc_expr *y)
1720 gfc_expr *result;
1722 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1723 return NULL;
1725 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
1727 mpz_ior (result->value.integer, x->value.integer, y->value.integer);
1728 return range_check (result, "IOR");
1732 gfc_expr *
1733 gfc_simplify_ishft (gfc_expr *e, gfc_expr *s)
1735 gfc_expr *result;
1736 int shift, ashift, isize, k, *bits, i;
1738 if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
1739 return NULL;
1741 if (gfc_extract_int (s, &shift) != NULL)
1743 gfc_error ("Invalid second argument of ISHFT at %L", &s->where);
1744 return &gfc_bad_expr;
1747 k = gfc_validate_kind (BT_INTEGER, e->ts.kind, false);
1749 isize = gfc_integer_kinds[k].bit_size;
1751 if (shift >= 0)
1752 ashift = shift;
1753 else
1754 ashift = -shift;
1756 if (ashift > isize)
1758 gfc_error ("Magnitude of second argument of ISHFT exceeds bit size "
1759 "at %L", &s->where);
1760 return &gfc_bad_expr;
1763 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
1765 if (shift == 0)
1767 mpz_set (result->value.integer, e->value.integer);
1768 return range_check (result, "ISHFT");
1771 bits = gfc_getmem (isize * sizeof (int));
1773 for (i = 0; i < isize; i++)
1774 bits[i] = mpz_tstbit (e->value.integer, i);
1776 if (shift > 0)
1778 for (i = 0; i < shift; i++)
1779 mpz_clrbit (result->value.integer, i);
1781 for (i = 0; i < isize - shift; i++)
1783 if (bits[i] == 0)
1784 mpz_clrbit (result->value.integer, i + shift);
1785 else
1786 mpz_setbit (result->value.integer, i + shift);
1789 else
1791 for (i = isize - 1; i >= isize - ashift; i--)
1792 mpz_clrbit (result->value.integer, i);
1794 for (i = isize - 1; i >= ashift; i--)
1796 if (bits[i] == 0)
1797 mpz_clrbit (result->value.integer, i - ashift);
1798 else
1799 mpz_setbit (result->value.integer, i - ashift);
1803 convert_mpz_to_signed (result->value.integer, isize);
1805 gfc_free (bits);
1806 return result;
1810 gfc_expr *
1811 gfc_simplify_ishftc (gfc_expr *e, gfc_expr *s, gfc_expr *sz)
1813 gfc_expr *result;
1814 int shift, ashift, isize, ssize, delta, k;
1815 int i, *bits;
1817 if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
1818 return NULL;
1820 if (gfc_extract_int (s, &shift) != NULL)
1822 gfc_error ("Invalid second argument of ISHFTC at %L", &s->where);
1823 return &gfc_bad_expr;
1826 k = gfc_validate_kind (e->ts.type, e->ts.kind, false);
1827 isize = gfc_integer_kinds[k].bit_size;
1829 if (sz != NULL)
1831 if (sz->expr_type != EXPR_CONSTANT)
1832 return NULL;
1834 if (gfc_extract_int (sz, &ssize) != NULL || ssize <= 0)
1836 gfc_error ("Invalid third argument of ISHFTC at %L", &sz->where);
1837 return &gfc_bad_expr;
1840 if (ssize > isize)
1842 gfc_error ("Magnitude of third argument of ISHFTC exceeds "
1843 "BIT_SIZE of first argument at %L", &s->where);
1844 return &gfc_bad_expr;
1847 else
1848 ssize = isize;
1850 if (shift >= 0)
1851 ashift = shift;
1852 else
1853 ashift = -shift;
1855 if (ashift > ssize)
1857 if (sz != NULL)
1858 gfc_error ("Magnitude of second argument of ISHFTC exceeds "
1859 "third argument at %L", &s->where);
1860 else
1861 gfc_error ("Magnitude of second argument of ISHFTC exceeds "
1862 "BIT_SIZE of first argument at %L", &s->where);
1863 return &gfc_bad_expr;
1866 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
1868 mpz_set (result->value.integer, e->value.integer);
1870 if (shift == 0)
1871 return result;
1873 convert_mpz_to_unsigned (result->value.integer, isize);
1875 bits = gfc_getmem (ssize * sizeof (int));
1877 for (i = 0; i < ssize; i++)
1878 bits[i] = mpz_tstbit (e->value.integer, i);
1880 delta = ssize - ashift;
1882 if (shift > 0)
1884 for (i = 0; i < delta; i++)
1886 if (bits[i] == 0)
1887 mpz_clrbit (result->value.integer, i + shift);
1888 else
1889 mpz_setbit (result->value.integer, i + shift);
1892 for (i = delta; i < ssize; i++)
1894 if (bits[i] == 0)
1895 mpz_clrbit (result->value.integer, i - delta);
1896 else
1897 mpz_setbit (result->value.integer, i - delta);
1900 else
1902 for (i = 0; i < ashift; i++)
1904 if (bits[i] == 0)
1905 mpz_clrbit (result->value.integer, i + delta);
1906 else
1907 mpz_setbit (result->value.integer, i + delta);
1910 for (i = ashift; i < ssize; i++)
1912 if (bits[i] == 0)
1913 mpz_clrbit (result->value.integer, i + shift);
1914 else
1915 mpz_setbit (result->value.integer, i + shift);
1919 convert_mpz_to_signed (result->value.integer, isize);
1921 gfc_free (bits);
1922 return result;
1926 gfc_expr *
1927 gfc_simplify_kind (gfc_expr *e)
1930 if (e->ts.type == BT_DERIVED)
1932 gfc_error ("Argument of KIND at %L is a DERIVED type", &e->where);
1933 return &gfc_bad_expr;
1936 return gfc_int_expr (e->ts.kind);
1940 static gfc_expr *
1941 simplify_bound (gfc_expr *array, gfc_expr *dim, int upper)
1943 gfc_ref *ref;
1944 gfc_array_spec *as;
1945 gfc_expr *l, *u, *result;
1946 int d;
1948 if (dim == NULL)
1949 /* TODO: Simplify constant multi-dimensional bounds. */
1950 return NULL;
1952 if (dim->expr_type != EXPR_CONSTANT)
1953 return NULL;
1955 if (array->expr_type != EXPR_VARIABLE)
1956 return NULL;
1958 /* Follow any component references. */
1959 as = array->symtree->n.sym->as;
1960 for (ref = array->ref; ref; ref = ref->next)
1962 switch (ref->type)
1964 case REF_ARRAY:
1965 switch (ref->u.ar.type)
1967 case AR_ELEMENT:
1968 as = NULL;
1969 continue;
1971 case AR_FULL:
1972 /* We're done because 'as' has already been set in the
1973 previous iteration. */
1974 goto done;
1976 case AR_SECTION:
1977 case AR_UNKNOWN:
1978 return NULL;
1981 gcc_unreachable ();
1983 case REF_COMPONENT:
1984 as = ref->u.c.component->as;
1985 continue;
1987 case REF_SUBSTRING:
1988 continue;
1992 gcc_unreachable ();
1994 done:
1995 if (as->type == AS_DEFERRED || as->type == AS_ASSUMED_SHAPE)
1996 return NULL;
1998 d = mpz_get_si (dim->value.integer);
2000 if (d < 1 || d > as->rank
2001 || (d == as->rank && as->type == AS_ASSUMED_SIZE && upper))
2003 gfc_error ("DIM argument at %L is out of bounds", &dim->where);
2004 return &gfc_bad_expr;
2007 /* The last dimension of an assumed-size array is special. */
2008 if (d == as->rank && as->type == AS_ASSUMED_SIZE && !upper)
2010 if (as->lower[d-1]->expr_type == EXPR_CONSTANT)
2011 return gfc_copy_expr (as->lower[d-1]);
2012 else
2013 return NULL;
2016 /* Then, we need to know the extent of the given dimension. */
2017 l = as->lower[d-1];
2018 u = as->upper[d-1];
2020 if (l->expr_type != EXPR_CONSTANT || u->expr_type != EXPR_CONSTANT)
2021 return NULL;
2023 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
2024 &array->where);
2026 if (mpz_cmp (l->value.integer, u->value.integer) > 0)
2028 /* Zero extent. */
2029 if (upper)
2030 mpz_set_si (result->value.integer, 0);
2031 else
2032 mpz_set_si (result->value.integer, 1);
2034 else
2036 /* Nonzero extent. */
2037 if (upper)
2038 mpz_set (result->value.integer, u->value.integer);
2039 else
2040 mpz_set (result->value.integer, l->value.integer);
2043 return range_check (result, upper ? "UBOUND" : "LBOUND");
2047 gfc_expr *
2048 gfc_simplify_lbound (gfc_expr *array, gfc_expr *dim)
2050 return simplify_bound (array, dim, 0);
2054 gfc_expr *
2055 gfc_simplify_len (gfc_expr *e)
2057 gfc_expr *result;
2059 if (e->expr_type == EXPR_CONSTANT)
2061 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
2062 &e->where);
2063 mpz_set_si (result->value.integer, e->value.character.length);
2064 return range_check (result, "LEN");
2067 if (e->ts.cl != NULL && e->ts.cl->length != NULL
2068 && e->ts.cl->length->expr_type == EXPR_CONSTANT)
2070 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
2071 &e->where);
2072 mpz_set (result->value.integer, e->ts.cl->length->value.integer);
2073 return range_check (result, "LEN");
2076 return NULL;
2080 gfc_expr *
2081 gfc_simplify_len_trim (gfc_expr *e)
2083 gfc_expr *result;
2084 int count, len, lentrim, i;
2086 if (e->expr_type != EXPR_CONSTANT)
2087 return NULL;
2089 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
2090 &e->where);
2092 len = e->value.character.length;
2094 for (count = 0, i = 1; i <= len; i++)
2095 if (e->value.character.string[len - i] == ' ')
2096 count++;
2097 else
2098 break;
2100 lentrim = len - count;
2102 mpz_set_si (result->value.integer, lentrim);
2103 return range_check (result, "LEN_TRIM");
2107 gfc_expr *
2108 gfc_simplify_lge (gfc_expr *a, gfc_expr *b)
2110 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
2111 return NULL;
2113 return gfc_logical_expr (gfc_compare_string (a, b) >= 0, &a->where);
2117 gfc_expr *
2118 gfc_simplify_lgt (gfc_expr *a, gfc_expr *b)
2120 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
2121 return NULL;
2123 return gfc_logical_expr (gfc_compare_string (a, b) > 0,
2124 &a->where);
2128 gfc_expr *
2129 gfc_simplify_lle (gfc_expr *a, gfc_expr *b)
2131 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
2132 return NULL;
2134 return gfc_logical_expr (gfc_compare_string (a, b) <= 0, &a->where);
2138 gfc_expr *
2139 gfc_simplify_llt (gfc_expr *a, gfc_expr *b)
2141 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
2142 return NULL;
2144 return gfc_logical_expr (gfc_compare_string (a, b) < 0, &a->where);
2148 gfc_expr *
2149 gfc_simplify_log (gfc_expr *x)
2151 gfc_expr *result;
2152 mpfr_t xr, xi;
2154 if (x->expr_type != EXPR_CONSTANT)
2155 return NULL;
2157 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
2159 gfc_set_model_kind (x->ts.kind);
2161 switch (x->ts.type)
2163 case BT_REAL:
2164 if (mpfr_sgn (x->value.real) <= 0)
2166 gfc_error ("Argument of LOG at %L cannot be less than or equal "
2167 "to zero", &x->where);
2168 gfc_free_expr (result);
2169 return &gfc_bad_expr;
2172 mpfr_log (result->value.real, x->value.real, GFC_RND_MODE);
2173 break;
2175 case BT_COMPLEX:
2176 if ((mpfr_sgn (x->value.complex.r) == 0)
2177 && (mpfr_sgn (x->value.complex.i) == 0))
2179 gfc_error ("Complex argument of LOG at %L cannot be zero",
2180 &x->where);
2181 gfc_free_expr (result);
2182 return &gfc_bad_expr;
2185 mpfr_init (xr);
2186 mpfr_init (xi);
2188 mpfr_atan2 (result->value.complex.i, x->value.complex.i,
2189 x->value.complex.r, GFC_RND_MODE);
2191 mpfr_mul (xr, x->value.complex.r, x->value.complex.r, GFC_RND_MODE);
2192 mpfr_mul (xi, x->value.complex.i, x->value.complex.i, GFC_RND_MODE);
2193 mpfr_add (xr, xr, xi, GFC_RND_MODE);
2194 mpfr_sqrt (xr, xr, GFC_RND_MODE);
2195 mpfr_log (result->value.complex.r, xr, GFC_RND_MODE);
2197 mpfr_clear (xr);
2198 mpfr_clear (xi);
2200 break;
2202 default:
2203 gfc_internal_error ("gfc_simplify_log: bad type");
2206 return range_check (result, "LOG");
2210 gfc_expr *
2211 gfc_simplify_log10 (gfc_expr *x)
2213 gfc_expr *result;
2215 if (x->expr_type != EXPR_CONSTANT)
2216 return NULL;
2218 gfc_set_model_kind (x->ts.kind);
2220 if (mpfr_sgn (x->value.real) <= 0)
2222 gfc_error ("Argument of LOG10 at %L cannot be less than or equal "
2223 "to zero", &x->where);
2224 return &gfc_bad_expr;
2227 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
2229 mpfr_log10 (result->value.real, x->value.real, GFC_RND_MODE);
2231 return range_check (result, "LOG10");
2235 gfc_expr *
2236 gfc_simplify_logical (gfc_expr *e, gfc_expr *k)
2238 gfc_expr *result;
2239 int kind;
2241 kind = get_kind (BT_LOGICAL, k, "LOGICAL", gfc_default_logical_kind);
2242 if (kind < 0)
2243 return &gfc_bad_expr;
2245 if (e->expr_type != EXPR_CONSTANT)
2246 return NULL;
2248 result = gfc_constant_result (BT_LOGICAL, kind, &e->where);
2250 result->value.logical = e->value.logical;
2252 return result;
2256 /* This function is special since MAX() can take any number of
2257 arguments. The simplified expression is a rewritten version of the
2258 argument list containing at most one constant element. Other
2259 constant elements are deleted. Because the argument list has
2260 already been checked, this function always succeeds. sign is 1 for
2261 MAX(), -1 for MIN(). */
2263 static gfc_expr *
2264 simplify_min_max (gfc_expr *expr, int sign)
2266 gfc_actual_arglist *arg, *last, *extremum;
2267 gfc_intrinsic_sym * specific;
2269 last = NULL;
2270 extremum = NULL;
2271 specific = expr->value.function.isym;
2273 arg = expr->value.function.actual;
2275 for (; arg; last = arg, arg = arg->next)
2277 if (arg->expr->expr_type != EXPR_CONSTANT)
2278 continue;
2280 if (extremum == NULL)
2282 extremum = arg;
2283 continue;
2286 switch (arg->expr->ts.type)
2288 case BT_INTEGER:
2289 if (mpz_cmp (arg->expr->value.integer,
2290 extremum->expr->value.integer) * sign > 0)
2291 mpz_set (extremum->expr->value.integer, arg->expr->value.integer);
2293 break;
2295 case BT_REAL:
2296 if (mpfr_cmp (arg->expr->value.real, extremum->expr->value.real)
2297 * sign > 0)
2298 mpfr_set (extremum->expr->value.real, arg->expr->value.real,
2299 GFC_RND_MODE);
2301 break;
2303 default:
2304 gfc_internal_error ("gfc_simplify_max(): Bad type in arglist");
2307 /* Delete the extra constant argument. */
2308 if (last == NULL)
2309 expr->value.function.actual = arg->next;
2310 else
2311 last->next = arg->next;
2313 arg->next = NULL;
2314 gfc_free_actual_arglist (arg);
2315 arg = last;
2318 /* If there is one value left, replace the function call with the
2319 expression. */
2320 if (expr->value.function.actual->next != NULL)
2321 return NULL;
2323 /* Convert to the correct type and kind. */
2324 if (expr->ts.type != BT_UNKNOWN)
2325 return gfc_convert_constant (expr->value.function.actual->expr,
2326 expr->ts.type, expr->ts.kind);
2328 if (specific->ts.type != BT_UNKNOWN)
2329 return gfc_convert_constant (expr->value.function.actual->expr,
2330 specific->ts.type, specific->ts.kind);
2332 return gfc_copy_expr (expr->value.function.actual->expr);
2336 gfc_expr *
2337 gfc_simplify_min (gfc_expr *e)
2339 return simplify_min_max (e, -1);
2343 gfc_expr *
2344 gfc_simplify_max (gfc_expr *e)
2346 return simplify_min_max (e, 1);
2350 gfc_expr *
2351 gfc_simplify_maxexponent (gfc_expr *x)
2353 gfc_expr *result;
2354 int i;
2356 i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
2358 result = gfc_int_expr (gfc_real_kinds[i].max_exponent);
2359 result->where = x->where;
2361 return result;
2365 gfc_expr *
2366 gfc_simplify_minexponent (gfc_expr *x)
2368 gfc_expr *result;
2369 int i;
2371 i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
2373 result = gfc_int_expr (gfc_real_kinds[i].min_exponent);
2374 result->where = x->where;
2376 return result;
2380 gfc_expr *
2381 gfc_simplify_mod (gfc_expr *a, gfc_expr *p)
2383 gfc_expr *result;
2384 mpfr_t quot, iquot, term;
2385 int kind;
2387 if (a->expr_type != EXPR_CONSTANT || p->expr_type != EXPR_CONSTANT)
2388 return NULL;
2390 kind = a->ts.kind > p->ts.kind ? a->ts.kind : p->ts.kind;
2391 result = gfc_constant_result (a->ts.type, kind, &a->where);
2393 switch (a->ts.type)
2395 case BT_INTEGER:
2396 if (mpz_cmp_ui (p->value.integer, 0) == 0)
2398 /* Result is processor-dependent. */
2399 gfc_error ("Second argument MOD at %L is zero", &a->where);
2400 gfc_free_expr (result);
2401 return &gfc_bad_expr;
2403 mpz_tdiv_r (result->value.integer, a->value.integer, p->value.integer);
2404 break;
2406 case BT_REAL:
2407 if (mpfr_cmp_ui (p->value.real, 0) == 0)
2409 /* Result is processor-dependent. */
2410 gfc_error ("Second argument of MOD at %L is zero", &p->where);
2411 gfc_free_expr (result);
2412 return &gfc_bad_expr;
2415 gfc_set_model_kind (kind);
2416 mpfr_init (quot);
2417 mpfr_init (iquot);
2418 mpfr_init (term);
2420 mpfr_div (quot, a->value.real, p->value.real, GFC_RND_MODE);
2421 mpfr_trunc (iquot, quot);
2422 mpfr_mul (term, iquot, p->value.real, GFC_RND_MODE);
2423 mpfr_sub (result->value.real, a->value.real, term, GFC_RND_MODE);
2425 mpfr_clear (quot);
2426 mpfr_clear (iquot);
2427 mpfr_clear (term);
2428 break;
2430 default:
2431 gfc_internal_error ("gfc_simplify_mod(): Bad arguments");
2434 return range_check (result, "MOD");
2438 gfc_expr *
2439 gfc_simplify_modulo (gfc_expr *a, gfc_expr *p)
2441 gfc_expr *result;
2442 mpfr_t quot, iquot, term;
2443 int kind;
2445 if (a->expr_type != EXPR_CONSTANT || p->expr_type != EXPR_CONSTANT)
2446 return NULL;
2448 kind = a->ts.kind > p->ts.kind ? a->ts.kind : p->ts.kind;
2449 result = gfc_constant_result (a->ts.type, kind, &a->where);
2451 switch (a->ts.type)
2453 case BT_INTEGER:
2454 if (mpz_cmp_ui (p->value.integer, 0) == 0)
2456 /* Result is processor-dependent. This processor just opts
2457 to not handle it at all. */
2458 gfc_error ("Second argument of MODULO at %L is zero", &a->where);
2459 gfc_free_expr (result);
2460 return &gfc_bad_expr;
2462 mpz_fdiv_r (result->value.integer, a->value.integer, p->value.integer);
2464 break;
2466 case BT_REAL:
2467 if (mpfr_cmp_ui (p->value.real, 0) == 0)
2469 /* Result is processor-dependent. */
2470 gfc_error ("Second argument of MODULO at %L is zero", &p->where);
2471 gfc_free_expr (result);
2472 return &gfc_bad_expr;
2475 gfc_set_model_kind (kind);
2476 mpfr_init (quot);
2477 mpfr_init (iquot);
2478 mpfr_init (term);
2480 mpfr_div (quot, a->value.real, p->value.real, GFC_RND_MODE);
2481 mpfr_floor (iquot, quot);
2482 mpfr_mul (term, iquot, p->value.real, GFC_RND_MODE);
2483 mpfr_sub (result->value.real, a->value.real, term, GFC_RND_MODE);
2485 mpfr_clear (quot);
2486 mpfr_clear (iquot);
2487 mpfr_clear (term);
2488 break;
2490 default:
2491 gfc_internal_error ("gfc_simplify_modulo(): Bad arguments");
2494 return range_check (result, "MODULO");
2498 /* Exists for the sole purpose of consistency with other intrinsics. */
2499 gfc_expr *
2500 gfc_simplify_mvbits (gfc_expr *f ATTRIBUTE_UNUSED,
2501 gfc_expr *fp ATTRIBUTE_UNUSED,
2502 gfc_expr *l ATTRIBUTE_UNUSED,
2503 gfc_expr *to ATTRIBUTE_UNUSED,
2504 gfc_expr *tp ATTRIBUTE_UNUSED)
2506 return NULL;
2510 gfc_expr *
2511 gfc_simplify_nearest (gfc_expr *x, gfc_expr *s)
2513 gfc_expr *result;
2514 mpfr_t tmp;
2515 int sgn;
2517 if (x->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
2518 return NULL;
2520 if (mpfr_sgn (s->value.real) == 0)
2522 gfc_error ("Second argument of NEAREST at %L shall not be zero",
2523 &s->where);
2524 return &gfc_bad_expr;
2527 gfc_set_model_kind (x->ts.kind);
2528 result = gfc_copy_expr (x);
2530 sgn = mpfr_sgn (s->value.real);
2531 mpfr_init (tmp);
2532 mpfr_set_inf (tmp, sgn);
2533 mpfr_nexttoward (result->value.real, tmp);
2534 mpfr_clear (tmp);
2536 return range_check (result, "NEAREST");
2540 static gfc_expr *
2541 simplify_nint (const char *name, gfc_expr *e, gfc_expr *k)
2543 gfc_expr *itrunc, *result;
2544 int kind;
2546 kind = get_kind (BT_INTEGER, k, name, gfc_default_integer_kind);
2547 if (kind == -1)
2548 return &gfc_bad_expr;
2550 if (e->expr_type != EXPR_CONSTANT)
2551 return NULL;
2553 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
2555 itrunc = gfc_copy_expr (e);
2557 mpfr_round (itrunc->value.real, e->value.real);
2559 gfc_mpfr_to_mpz (result->value.integer, itrunc->value.real);
2561 gfc_free_expr (itrunc);
2563 return range_check (result, name);
2567 gfc_expr *
2568 gfc_simplify_new_line (gfc_expr *e)
2570 gfc_expr *result;
2572 if (e->expr_type != EXPR_CONSTANT)
2573 return NULL;
2575 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
2577 result->value.character.string = gfc_getmem (2);
2579 result->value.character.length = 1;
2580 result->value.character.string[0] = '\n';
2581 result->value.character.string[1] = '\0'; /* For debugger */
2582 return result;
2586 gfc_expr *
2587 gfc_simplify_nint (gfc_expr *e, gfc_expr *k)
2589 return simplify_nint ("NINT", e, k);
2593 gfc_expr *
2594 gfc_simplify_idnint (gfc_expr *e)
2596 return simplify_nint ("IDNINT", e, NULL);
2600 gfc_expr *
2601 gfc_simplify_not (gfc_expr *e)
2603 gfc_expr *result;
2605 if (e->expr_type != EXPR_CONSTANT)
2606 return NULL;
2608 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
2610 mpz_com (result->value.integer, e->value.integer);
2612 return range_check (result, "NOT");
2616 gfc_expr *
2617 gfc_simplify_null (gfc_expr *mold)
2619 gfc_expr *result;
2621 if (mold == NULL)
2623 result = gfc_get_expr ();
2624 result->ts.type = BT_UNKNOWN;
2626 else
2627 result = gfc_copy_expr (mold);
2628 result->expr_type = EXPR_NULL;
2630 return result;
2634 gfc_expr *
2635 gfc_simplify_or (gfc_expr *x, gfc_expr *y)
2637 gfc_expr *result;
2638 int kind;
2640 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2641 return NULL;
2643 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
2644 if (x->ts.type == BT_INTEGER)
2646 result = gfc_constant_result (BT_INTEGER, kind, &x->where);
2647 mpz_ior (result->value.integer, x->value.integer, y->value.integer);
2649 else /* BT_LOGICAL */
2651 result = gfc_constant_result (BT_LOGICAL, kind, &x->where);
2652 result->value.logical = x->value.logical || y->value.logical;
2655 return range_check (result, "OR");
2659 gfc_expr *
2660 gfc_simplify_precision (gfc_expr *e)
2662 gfc_expr *result;
2663 int i;
2665 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2667 result = gfc_int_expr (gfc_real_kinds[i].precision);
2668 result->where = e->where;
2670 return result;
2674 gfc_expr *
2675 gfc_simplify_radix (gfc_expr *e)
2677 gfc_expr *result;
2678 int i;
2680 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2681 switch (e->ts.type)
2683 case BT_INTEGER:
2684 i = gfc_integer_kinds[i].radix;
2685 break;
2687 case BT_REAL:
2688 i = gfc_real_kinds[i].radix;
2689 break;
2691 default:
2692 gcc_unreachable ();
2695 result = gfc_int_expr (i);
2696 result->where = e->where;
2698 return result;
2702 gfc_expr *
2703 gfc_simplify_range (gfc_expr *e)
2705 gfc_expr *result;
2706 int i;
2707 long j;
2709 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2711 switch (e->ts.type)
2713 case BT_INTEGER:
2714 j = gfc_integer_kinds[i].range;
2715 break;
2717 case BT_REAL:
2718 case BT_COMPLEX:
2719 j = gfc_real_kinds[i].range;
2720 break;
2722 default:
2723 gcc_unreachable ();
2726 result = gfc_int_expr (j);
2727 result->where = e->where;
2729 return result;
2733 gfc_expr *
2734 gfc_simplify_real (gfc_expr *e, gfc_expr *k)
2736 gfc_expr *result;
2737 int kind;
2739 if (e->ts.type == BT_COMPLEX)
2740 kind = get_kind (BT_REAL, k, "REAL", e->ts.kind);
2741 else
2742 kind = get_kind (BT_REAL, k, "REAL", gfc_default_real_kind);
2744 if (kind == -1)
2745 return &gfc_bad_expr;
2747 if (e->expr_type != EXPR_CONSTANT)
2748 return NULL;
2750 switch (e->ts.type)
2752 case BT_INTEGER:
2753 result = gfc_int2real (e, kind);
2754 break;
2756 case BT_REAL:
2757 result = gfc_real2real (e, kind);
2758 break;
2760 case BT_COMPLEX:
2761 result = gfc_complex2real (e, kind);
2762 break;
2764 default:
2765 gfc_internal_error ("bad type in REAL");
2766 /* Not reached */
2769 return range_check (result, "REAL");
2773 gfc_expr *
2774 gfc_simplify_realpart (gfc_expr *e)
2776 gfc_expr *result;
2778 if (e->expr_type != EXPR_CONSTANT)
2779 return NULL;
2781 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
2782 mpfr_set (result->value.real, e->value.complex.r, GFC_RND_MODE);
2784 return range_check (result, "REALPART");
2787 gfc_expr *
2788 gfc_simplify_repeat (gfc_expr *e, gfc_expr *n)
2790 gfc_expr *result;
2791 int i, j, len, ncopies, nlen;
2793 if (e->expr_type != EXPR_CONSTANT || n->expr_type != EXPR_CONSTANT)
2794 return NULL;
2796 if (n != NULL && (gfc_extract_int (n, &ncopies) != NULL || ncopies < 0))
2798 gfc_error ("Invalid second argument of REPEAT at %L", &n->where);
2799 return &gfc_bad_expr;
2802 len = e->value.character.length;
2803 nlen = ncopies * len;
2805 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
2807 if (ncopies == 0)
2809 result->value.character.string = gfc_getmem (1);
2810 result->value.character.length = 0;
2811 result->value.character.string[0] = '\0';
2812 return result;
2815 result->value.character.length = nlen;
2816 result->value.character.string = gfc_getmem (nlen + 1);
2818 for (i = 0; i < ncopies; i++)
2819 for (j = 0; j < len; j++)
2820 result->value.character.string[j + i * len]
2821 = e->value.character.string[j];
2823 result->value.character.string[nlen] = '\0'; /* For debugger */
2824 return result;
2828 /* This one is a bear, but mainly has to do with shuffling elements. */
2830 gfc_expr *
2831 gfc_simplify_reshape (gfc_expr *source, gfc_expr *shape_exp,
2832 gfc_expr *pad, gfc_expr *order_exp)
2834 int order[GFC_MAX_DIMENSIONS], shape[GFC_MAX_DIMENSIONS];
2835 int i, rank, npad, x[GFC_MAX_DIMENSIONS];
2836 gfc_constructor *head, *tail;
2837 mpz_t index, size;
2838 unsigned long j;
2839 size_t nsource;
2840 gfc_expr *e;
2842 /* Unpack the shape array. */
2843 if (source->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (source))
2844 return NULL;
2846 if (shape_exp->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (shape_exp))
2847 return NULL;
2849 if (pad != NULL
2850 && (pad->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (pad)))
2851 return NULL;
2853 if (order_exp != NULL
2854 && (order_exp->expr_type != EXPR_ARRAY
2855 || !gfc_is_constant_expr (order_exp)))
2856 return NULL;
2858 mpz_init (index);
2859 rank = 0;
2860 head = tail = NULL;
2862 for (;;)
2864 e = gfc_get_array_element (shape_exp, rank);
2865 if (e == NULL)
2866 break;
2868 if (gfc_extract_int (e, &shape[rank]) != NULL)
2870 gfc_error ("Integer too large in shape specification at %L",
2871 &e->where);
2872 gfc_free_expr (e);
2873 goto bad_reshape;
2876 gfc_free_expr (e);
2878 if (rank >= GFC_MAX_DIMENSIONS)
2880 gfc_error ("Too many dimensions in shape specification for RESHAPE "
2881 "at %L", &e->where);
2883 goto bad_reshape;
2886 if (shape[rank] < 0)
2888 gfc_error ("Shape specification at %L cannot be negative",
2889 &e->where);
2890 goto bad_reshape;
2893 rank++;
2896 if (rank == 0)
2898 gfc_error ("Shape specification at %L cannot be the null array",
2899 &shape_exp->where);
2900 goto bad_reshape;
2903 /* Now unpack the order array if present. */
2904 if (order_exp == NULL)
2906 for (i = 0; i < rank; i++)
2907 order[i] = i;
2909 else
2911 for (i = 0; i < rank; i++)
2912 x[i] = 0;
2914 for (i = 0; i < rank; i++)
2916 e = gfc_get_array_element (order_exp, i);
2917 if (e == NULL)
2919 gfc_error ("ORDER parameter of RESHAPE at %L is not the same "
2920 "size as SHAPE parameter", &order_exp->where);
2921 goto bad_reshape;
2924 if (gfc_extract_int (e, &order[i]) != NULL)
2926 gfc_error ("Error in ORDER parameter of RESHAPE at %L",
2927 &e->where);
2928 gfc_free_expr (e);
2929 goto bad_reshape;
2932 gfc_free_expr (e);
2934 if (order[i] < 1 || order[i] > rank)
2936 gfc_error ("ORDER parameter of RESHAPE at %L is out of range",
2937 &e->where);
2938 goto bad_reshape;
2941 order[i]--;
2943 if (x[order[i]])
2945 gfc_error ("Invalid permutation in ORDER parameter at %L",
2946 &e->where);
2947 goto bad_reshape;
2950 x[order[i]] = 1;
2954 /* Count the elements in the source and padding arrays. */
2956 npad = 0;
2957 if (pad != NULL)
2959 gfc_array_size (pad, &size);
2960 npad = mpz_get_ui (size);
2961 mpz_clear (size);
2964 gfc_array_size (source, &size);
2965 nsource = mpz_get_ui (size);
2966 mpz_clear (size);
2968 /* If it weren't for that pesky permutation we could just loop
2969 through the source and round out any shortage with pad elements.
2970 But no, someone just had to have the compiler do something the
2971 user should be doing. */
2973 for (i = 0; i < rank; i++)
2974 x[i] = 0;
2976 for (;;)
2978 /* Figure out which element to extract. */
2979 mpz_set_ui (index, 0);
2981 for (i = rank - 1; i >= 0; i--)
2983 mpz_add_ui (index, index, x[order[i]]);
2984 if (i != 0)
2985 mpz_mul_ui (index, index, shape[order[i - 1]]);
2988 if (mpz_cmp_ui (index, INT_MAX) > 0)
2989 gfc_internal_error ("Reshaped array too large at %L", &e->where);
2991 j = mpz_get_ui (index);
2993 if (j < nsource)
2994 e = gfc_get_array_element (source, j);
2995 else
2997 j = j - nsource;
2999 if (npad == 0)
3001 gfc_error ("PAD parameter required for short SOURCE parameter "
3002 "at %L", &source->where);
3003 goto bad_reshape;
3006 j = j % npad;
3007 e = gfc_get_array_element (pad, j);
3010 if (head == NULL)
3011 head = tail = gfc_get_constructor ();
3012 else
3014 tail->next = gfc_get_constructor ();
3015 tail = tail->next;
3018 if (e == NULL)
3019 goto bad_reshape;
3021 tail->where = e->where;
3022 tail->expr = e;
3024 /* Calculate the next element. */
3025 i = 0;
3027 inc:
3028 if (++x[i] < shape[i])
3029 continue;
3030 x[i++] = 0;
3031 if (i < rank)
3032 goto inc;
3034 break;
3037 mpz_clear (index);
3039 e = gfc_get_expr ();
3040 e->where = source->where;
3041 e->expr_type = EXPR_ARRAY;
3042 e->value.constructor = head;
3043 e->shape = gfc_get_shape (rank);
3045 for (i = 0; i < rank; i++)
3046 mpz_init_set_ui (e->shape[i], shape[i]);
3048 e->ts = source->ts;
3049 e->rank = rank;
3051 return e;
3053 bad_reshape:
3054 gfc_free_constructor (head);
3055 mpz_clear (index);
3056 return &gfc_bad_expr;
3060 gfc_expr *
3061 gfc_simplify_rrspacing (gfc_expr *x)
3063 gfc_expr *result;
3064 int i;
3065 long int e, p;
3067 if (x->expr_type != EXPR_CONSTANT)
3068 return NULL;
3070 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
3072 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
3074 mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE);
3076 /* Special case x = -0 and 0. */
3077 if (mpfr_sgn (result->value.real) == 0)
3079 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
3080 return result;
3083 /* | x * 2**(-e) | * 2**p. */
3084 e = - (long int) mpfr_get_exp (x->value.real);
3085 mpfr_mul_2si (result->value.real, result->value.real, e, GFC_RND_MODE);
3087 p = (long int) gfc_real_kinds[i].digits;
3088 mpfr_mul_2si (result->value.real, result->value.real, p, GFC_RND_MODE);
3090 return range_check (result, "RRSPACING");
3094 gfc_expr *
3095 gfc_simplify_scale (gfc_expr *x, gfc_expr *i)
3097 int k, neg_flag, power, exp_range;
3098 mpfr_t scale, radix;
3099 gfc_expr *result;
3101 if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
3102 return NULL;
3104 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
3106 if (mpfr_sgn (x->value.real) == 0)
3108 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
3109 return result;
3112 k = gfc_validate_kind (BT_REAL, x->ts.kind, false);
3114 exp_range = gfc_real_kinds[k].max_exponent - gfc_real_kinds[k].min_exponent;
3116 /* This check filters out values of i that would overflow an int. */
3117 if (mpz_cmp_si (i->value.integer, exp_range + 2) > 0
3118 || mpz_cmp_si (i->value.integer, -exp_range - 2) < 0)
3120 gfc_error ("Result of SCALE overflows its kind at %L", &result->where);
3121 return &gfc_bad_expr;
3124 /* Compute scale = radix ** power. */
3125 power = mpz_get_si (i->value.integer);
3127 if (power >= 0)
3128 neg_flag = 0;
3129 else
3131 neg_flag = 1;
3132 power = -power;
3135 gfc_set_model_kind (x->ts.kind);
3136 mpfr_init (scale);
3137 mpfr_init (radix);
3138 mpfr_set_ui (radix, gfc_real_kinds[k].radix, GFC_RND_MODE);
3139 mpfr_pow_ui (scale, radix, power, GFC_RND_MODE);
3141 if (neg_flag)
3142 mpfr_div (result->value.real, x->value.real, scale, GFC_RND_MODE);
3143 else
3144 mpfr_mul (result->value.real, x->value.real, scale, GFC_RND_MODE);
3146 mpfr_clear (scale);
3147 mpfr_clear (radix);
3149 return range_check (result, "SCALE");
3153 gfc_expr *
3154 gfc_simplify_scan (gfc_expr *e, gfc_expr *c, gfc_expr *b)
3156 gfc_expr *result;
3157 int back;
3158 size_t i;
3159 size_t indx, len, lenc;
3161 if (e->expr_type != EXPR_CONSTANT || c->expr_type != EXPR_CONSTANT)
3162 return NULL;
3164 if (b != NULL && b->value.logical != 0)
3165 back = 1;
3166 else
3167 back = 0;
3169 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
3170 &e->where);
3172 len = e->value.character.length;
3173 lenc = c->value.character.length;
3175 if (len == 0 || lenc == 0)
3177 indx = 0;
3179 else
3181 if (back == 0)
3183 indx = strcspn (e->value.character.string, c->value.character.string)
3184 + 1;
3185 if (indx > len)
3186 indx = 0;
3188 else
3190 i = 0;
3191 for (indx = len; indx > 0; indx--)
3193 for (i = 0; i < lenc; i++)
3195 if (c->value.character.string[i]
3196 == e->value.character.string[indx - 1])
3197 break;
3199 if (i < lenc)
3200 break;
3204 mpz_set_ui (result->value.integer, indx);
3205 return range_check (result, "SCAN");
3209 gfc_expr *
3210 gfc_simplify_selected_int_kind (gfc_expr *e)
3212 int i, kind, range;
3213 gfc_expr *result;
3215 if (e->expr_type != EXPR_CONSTANT || gfc_extract_int (e, &range) != NULL)
3216 return NULL;
3218 kind = INT_MAX;
3220 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
3221 if (gfc_integer_kinds[i].range >= range
3222 && gfc_integer_kinds[i].kind < kind)
3223 kind = gfc_integer_kinds[i].kind;
3225 if (kind == INT_MAX)
3226 kind = -1;
3228 result = gfc_int_expr (kind);
3229 result->where = e->where;
3231 return result;
3235 gfc_expr *
3236 gfc_simplify_selected_real_kind (gfc_expr *p, gfc_expr *q)
3238 int range, precision, i, kind, found_precision, found_range;
3239 gfc_expr *result;
3241 if (p == NULL)
3242 precision = 0;
3243 else
3245 if (p->expr_type != EXPR_CONSTANT
3246 || gfc_extract_int (p, &precision) != NULL)
3247 return NULL;
3250 if (q == NULL)
3251 range = 0;
3252 else
3254 if (q->expr_type != EXPR_CONSTANT
3255 || gfc_extract_int (q, &range) != NULL)
3256 return NULL;
3259 kind = INT_MAX;
3260 found_precision = 0;
3261 found_range = 0;
3263 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
3265 if (gfc_real_kinds[i].precision >= precision)
3266 found_precision = 1;
3268 if (gfc_real_kinds[i].range >= range)
3269 found_range = 1;
3271 if (gfc_real_kinds[i].precision >= precision
3272 && gfc_real_kinds[i].range >= range && gfc_real_kinds[i].kind < kind)
3273 kind = gfc_real_kinds[i].kind;
3276 if (kind == INT_MAX)
3278 kind = 0;
3280 if (!found_precision)
3281 kind = -1;
3282 if (!found_range)
3283 kind -= 2;
3286 result = gfc_int_expr (kind);
3287 result->where = (p != NULL) ? p->where : q->where;
3289 return result;
3293 gfc_expr *
3294 gfc_simplify_set_exponent (gfc_expr *x, gfc_expr *i)
3296 gfc_expr *result;
3297 mpfr_t exp, absv, log2, pow2, frac;
3298 unsigned long exp2;
3300 if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
3301 return NULL;
3303 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
3305 gfc_set_model_kind (x->ts.kind);
3307 if (mpfr_sgn (x->value.real) == 0)
3309 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
3310 return result;
3313 mpfr_init (absv);
3314 mpfr_init (log2);
3315 mpfr_init (exp);
3316 mpfr_init (pow2);
3317 mpfr_init (frac);
3319 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
3320 mpfr_log2 (log2, absv, GFC_RND_MODE);
3322 mpfr_trunc (log2, log2);
3323 mpfr_add_ui (exp, log2, 1, GFC_RND_MODE);
3325 /* Old exponent value, and fraction. */
3326 mpfr_ui_pow (pow2, 2, exp, GFC_RND_MODE);
3328 mpfr_div (frac, absv, pow2, GFC_RND_MODE);
3330 /* New exponent. */
3331 exp2 = (unsigned long) mpz_get_d (i->value.integer);
3332 mpfr_mul_2exp (result->value.real, frac, exp2, GFC_RND_MODE);
3334 mpfr_clear (absv);
3335 mpfr_clear (log2);
3336 mpfr_clear (pow2);
3337 mpfr_clear (frac);
3339 return range_check (result, "SET_EXPONENT");
3343 gfc_expr *
3344 gfc_simplify_shape (gfc_expr *source)
3346 mpz_t shape[GFC_MAX_DIMENSIONS];
3347 gfc_expr *result, *e, *f;
3348 gfc_array_ref *ar;
3349 int n;
3350 try t;
3352 if (source->rank == 0 || source->expr_type != EXPR_VARIABLE)
3353 return NULL;
3355 result = gfc_start_constructor (BT_INTEGER, gfc_default_integer_kind,
3356 &source->where);
3358 ar = gfc_find_array_ref (source);
3360 t = gfc_array_ref_shape (ar, shape);
3362 for (n = 0; n < source->rank; n++)
3364 e = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
3365 &source->where);
3367 if (t == SUCCESS)
3369 mpz_set (e->value.integer, shape[n]);
3370 mpz_clear (shape[n]);
3372 else
3374 mpz_set_ui (e->value.integer, n + 1);
3376 f = gfc_simplify_size (source, e);
3377 gfc_free_expr (e);
3378 if (f == NULL)
3380 gfc_free_expr (result);
3381 return NULL;
3383 else
3385 e = f;
3389 gfc_append_constructor (result, e);
3392 return result;
3396 gfc_expr *
3397 gfc_simplify_size (gfc_expr *array, gfc_expr *dim)
3399 mpz_t size;
3400 gfc_expr *result;
3401 int d;
3403 if (dim == NULL)
3405 if (gfc_array_size (array, &size) == FAILURE)
3406 return NULL;
3408 else
3410 if (dim->expr_type != EXPR_CONSTANT)
3411 return NULL;
3413 d = mpz_get_ui (dim->value.integer) - 1;
3414 if (gfc_array_dimen_size (array, d, &size) == FAILURE)
3415 return NULL;
3418 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
3419 &array->where);
3421 mpz_set (result->value.integer, size);
3423 return result;
3427 gfc_expr *
3428 gfc_simplify_sign (gfc_expr *x, gfc_expr *y)
3430 gfc_expr *result;
3432 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
3433 return NULL;
3435 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3437 switch (x->ts.type)
3439 case BT_INTEGER:
3440 mpz_abs (result->value.integer, x->value.integer);
3441 if (mpz_sgn (y->value.integer) < 0)
3442 mpz_neg (result->value.integer, result->value.integer);
3444 break;
3446 case BT_REAL:
3447 /* TODO: Handle -0.0 and +0.0 correctly on machines that support
3448 it. */
3449 mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE);
3450 if (mpfr_sgn (y->value.real) < 0)
3451 mpfr_neg (result->value.real, result->value.real, GFC_RND_MODE);
3453 break;
3455 default:
3456 gfc_internal_error ("Bad type in gfc_simplify_sign");
3459 return result;
3463 gfc_expr *
3464 gfc_simplify_sin (gfc_expr *x)
3466 gfc_expr *result;
3467 mpfr_t xp, xq;
3469 if (x->expr_type != EXPR_CONSTANT)
3470 return NULL;
3472 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3474 switch (x->ts.type)
3476 case BT_REAL:
3477 mpfr_sin (result->value.real, x->value.real, GFC_RND_MODE);
3478 break;
3480 case BT_COMPLEX:
3481 gfc_set_model (x->value.real);
3482 mpfr_init (xp);
3483 mpfr_init (xq);
3485 mpfr_sin (xp, x->value.complex.r, GFC_RND_MODE);
3486 mpfr_cosh (xq, x->value.complex.i, GFC_RND_MODE);
3487 mpfr_mul (result->value.complex.r, xp, xq, GFC_RND_MODE);
3489 mpfr_cos (xp, x->value.complex.r, GFC_RND_MODE);
3490 mpfr_sinh (xq, x->value.complex.i, GFC_RND_MODE);
3491 mpfr_mul (result->value.complex.i, xp, xq, GFC_RND_MODE);
3493 mpfr_clear (xp);
3494 mpfr_clear (xq);
3495 break;
3497 default:
3498 gfc_internal_error ("in gfc_simplify_sin(): Bad type");
3501 return range_check (result, "SIN");
3505 gfc_expr *
3506 gfc_simplify_sinh (gfc_expr *x)
3508 gfc_expr *result;
3510 if (x->expr_type != EXPR_CONSTANT)
3511 return NULL;
3513 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3515 mpfr_sinh (result->value.real, x->value.real, GFC_RND_MODE);
3517 return range_check (result, "SINH");
3521 /* The argument is always a double precision real that is converted to
3522 single precision. TODO: Rounding! */
3524 gfc_expr *
3525 gfc_simplify_sngl (gfc_expr *a)
3527 gfc_expr *result;
3529 if (a->expr_type != EXPR_CONSTANT)
3530 return NULL;
3532 result = gfc_real2real (a, gfc_default_real_kind);
3533 return range_check (result, "SNGL");
3537 gfc_expr *
3538 gfc_simplify_spacing (gfc_expr *x)
3540 gfc_expr *result;
3541 int i;
3542 long int en, ep;
3544 if (x->expr_type != EXPR_CONSTANT)
3545 return NULL;
3547 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
3549 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
3551 /* Special case x = 0 and -0. */
3552 mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE);
3553 if (mpfr_sgn (result->value.real) == 0)
3555 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
3556 return result;
3559 /* In the Fortran 95 standard, the result is b**(e - p) where b, e, and p
3560 are the radix, exponent of x, and precision. This excludes the
3561 possibility of subnormal numbers. Fortran 2003 states the result is
3562 b**max(e - p, emin - 1). */
3564 ep = (long int) mpfr_get_exp (x->value.real) - gfc_real_kinds[i].digits;
3565 en = (long int) gfc_real_kinds[i].min_exponent - 1;
3566 en = en > ep ? en : ep;
3568 mpfr_set_ui (result->value.real, 1, GFC_RND_MODE);
3569 mpfr_mul_2si (result->value.real, result->value.real, en, GFC_RND_MODE);
3571 return range_check (result, "SPACING");
3575 gfc_expr *
3576 gfc_simplify_sqrt (gfc_expr *e)
3578 gfc_expr *result;
3579 mpfr_t ac, ad, s, t, w;
3581 if (e->expr_type != EXPR_CONSTANT)
3582 return NULL;
3584 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
3586 switch (e->ts.type)
3588 case BT_REAL:
3589 if (mpfr_cmp_si (e->value.real, 0) < 0)
3590 goto negative_arg;
3591 mpfr_sqrt (result->value.real, e->value.real, GFC_RND_MODE);
3593 break;
3595 case BT_COMPLEX:
3596 /* Formula taken from Numerical Recipes to avoid over- and
3597 underflow. */
3599 gfc_set_model (e->value.real);
3600 mpfr_init (ac);
3601 mpfr_init (ad);
3602 mpfr_init (s);
3603 mpfr_init (t);
3604 mpfr_init (w);
3606 if (mpfr_cmp_ui (e->value.complex.r, 0) == 0
3607 && mpfr_cmp_ui (e->value.complex.i, 0) == 0)
3609 mpfr_set_ui (result->value.complex.r, 0, GFC_RND_MODE);
3610 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
3611 break;
3614 mpfr_abs (ac, e->value.complex.r, GFC_RND_MODE);
3615 mpfr_abs (ad, e->value.complex.i, GFC_RND_MODE);
3617 if (mpfr_cmp (ac, ad) >= 0)
3619 mpfr_div (t, e->value.complex.i, e->value.complex.r, GFC_RND_MODE);
3620 mpfr_mul (t, t, t, GFC_RND_MODE);
3621 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
3622 mpfr_sqrt (t, t, GFC_RND_MODE);
3623 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
3624 mpfr_div_ui (t, t, 2, GFC_RND_MODE);
3625 mpfr_sqrt (t, t, GFC_RND_MODE);
3626 mpfr_sqrt (s, ac, GFC_RND_MODE);
3627 mpfr_mul (w, s, t, GFC_RND_MODE);
3629 else
3631 mpfr_div (s, e->value.complex.r, e->value.complex.i, GFC_RND_MODE);
3632 mpfr_mul (t, s, s, GFC_RND_MODE);
3633 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
3634 mpfr_sqrt (t, t, GFC_RND_MODE);
3635 mpfr_abs (s, s, GFC_RND_MODE);
3636 mpfr_add (t, t, s, GFC_RND_MODE);
3637 mpfr_div_ui (t, t, 2, GFC_RND_MODE);
3638 mpfr_sqrt (t, t, GFC_RND_MODE);
3639 mpfr_sqrt (s, ad, GFC_RND_MODE);
3640 mpfr_mul (w, s, t, GFC_RND_MODE);
3643 if (mpfr_cmp_ui (w, 0) != 0 && mpfr_cmp_ui (e->value.complex.r, 0) >= 0)
3645 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
3646 mpfr_div (result->value.complex.i, e->value.complex.i, t, GFC_RND_MODE);
3647 mpfr_set (result->value.complex.r, w, GFC_RND_MODE);
3649 else if (mpfr_cmp_ui (w, 0) != 0
3650 && mpfr_cmp_ui (e->value.complex.r, 0) < 0
3651 && mpfr_cmp_ui (e->value.complex.i, 0) >= 0)
3653 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
3654 mpfr_div (result->value.complex.r, e->value.complex.i, t, GFC_RND_MODE);
3655 mpfr_set (result->value.complex.i, w, GFC_RND_MODE);
3657 else if (mpfr_cmp_ui (w, 0) != 0
3658 && mpfr_cmp_ui (e->value.complex.r, 0) < 0
3659 && mpfr_cmp_ui (e->value.complex.i, 0) < 0)
3661 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
3662 mpfr_div (result->value.complex.r, ad, t, GFC_RND_MODE);
3663 mpfr_neg (w, w, GFC_RND_MODE);
3664 mpfr_set (result->value.complex.i, w, GFC_RND_MODE);
3666 else
3667 gfc_internal_error ("invalid complex argument of SQRT at %L",
3668 &e->where);
3670 mpfr_clear (s);
3671 mpfr_clear (t);
3672 mpfr_clear (ac);
3673 mpfr_clear (ad);
3674 mpfr_clear (w);
3676 break;
3678 default:
3679 gfc_internal_error ("invalid argument of SQRT at %L", &e->where);
3682 return range_check (result, "SQRT");
3684 negative_arg:
3685 gfc_free_expr (result);
3686 gfc_error ("Argument of SQRT at %L has a negative value", &e->where);
3687 return &gfc_bad_expr;
3691 gfc_expr *
3692 gfc_simplify_tan (gfc_expr *x)
3694 int i;
3695 gfc_expr *result;
3697 if (x->expr_type != EXPR_CONSTANT)
3698 return NULL;
3700 i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
3702 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3704 mpfr_tan (result->value.real, x->value.real, GFC_RND_MODE);
3706 return range_check (result, "TAN");
3710 gfc_expr *
3711 gfc_simplify_tanh (gfc_expr *x)
3713 gfc_expr *result;
3715 if (x->expr_type != EXPR_CONSTANT)
3716 return NULL;
3718 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3720 mpfr_tanh (result->value.real, x->value.real, GFC_RND_MODE);
3722 return range_check (result, "TANH");
3727 gfc_expr *
3728 gfc_simplify_tiny (gfc_expr *e)
3730 gfc_expr *result;
3731 int i;
3733 i = gfc_validate_kind (BT_REAL, e->ts.kind, false);
3735 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
3736 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
3738 return result;
3742 gfc_expr *
3743 gfc_simplify_transfer (gfc_expr *source, gfc_expr *mold, gfc_expr *size)
3745 /* Reference mold and size to suppress warning. */
3746 if (gfc_init_expr && (mold || size))
3747 gfc_error ("TRANSFER intrinsic not implemented for initialization at %L",
3748 &source->where);
3750 return NULL;
3754 gfc_expr *
3755 gfc_simplify_trim (gfc_expr *e)
3757 gfc_expr *result;
3758 int count, i, len, lentrim;
3760 if (e->expr_type != EXPR_CONSTANT)
3761 return NULL;
3763 len = e->value.character.length;
3765 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
3767 for (count = 0, i = 1; i <= len; ++i)
3769 if (e->value.character.string[len - i] == ' ')
3770 count++;
3771 else
3772 break;
3775 lentrim = len - count;
3777 result->value.character.length = lentrim;
3778 result->value.character.string = gfc_getmem (lentrim + 1);
3780 for (i = 0; i < lentrim; i++)
3781 result->value.character.string[i] = e->value.character.string[i];
3783 result->value.character.string[lentrim] = '\0'; /* For debugger */
3785 return result;
3789 gfc_expr *
3790 gfc_simplify_ubound (gfc_expr *array, gfc_expr *dim)
3792 return simplify_bound (array, dim, 1);
3796 gfc_expr *
3797 gfc_simplify_verify (gfc_expr *s, gfc_expr *set, gfc_expr *b)
3799 gfc_expr *result;
3800 int back;
3801 size_t index, len, lenset;
3802 size_t i;
3804 if (s->expr_type != EXPR_CONSTANT || set->expr_type != EXPR_CONSTANT)
3805 return NULL;
3807 if (b != NULL && b->value.logical != 0)
3808 back = 1;
3809 else
3810 back = 0;
3812 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
3813 &s->where);
3815 len = s->value.character.length;
3816 lenset = set->value.character.length;
3818 if (len == 0)
3820 mpz_set_ui (result->value.integer, 0);
3821 return result;
3824 if (back == 0)
3826 if (lenset == 0)
3828 mpz_set_ui (result->value.integer, 1);
3829 return result;
3832 index = strspn (s->value.character.string, set->value.character.string)
3833 + 1;
3834 if (index > len)
3835 index = 0;
3838 else
3840 if (lenset == 0)
3842 mpz_set_ui (result->value.integer, len);
3843 return result;
3845 for (index = len; index > 0; index --)
3847 for (i = 0; i < lenset; i++)
3849 if (s->value.character.string[index - 1]
3850 == set->value.character.string[i])
3851 break;
3853 if (i == lenset)
3854 break;
3858 mpz_set_ui (result->value.integer, index);
3859 return result;
3863 gfc_expr *
3864 gfc_simplify_xor (gfc_expr *x, gfc_expr *y)
3866 gfc_expr *result;
3867 int kind;
3869 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
3870 return NULL;
3872 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
3873 if (x->ts.type == BT_INTEGER)
3875 result = gfc_constant_result (BT_INTEGER, kind, &x->where);
3876 mpz_xor (result->value.integer, x->value.integer, y->value.integer);
3878 else /* BT_LOGICAL */
3880 result = gfc_constant_result (BT_LOGICAL, kind, &x->where);
3881 result->value.logical = (x->value.logical && !y->value.logical)
3882 || (!x->value.logical && y->value.logical);
3885 return range_check (result, "XOR");
3889 /****************** Constant simplification *****************/
3891 /* Master function to convert one constant to another. While this is
3892 used as a simplification function, it requires the destination type
3893 and kind information which is supplied by a special case in
3894 do_simplify(). */
3896 gfc_expr *
3897 gfc_convert_constant (gfc_expr *e, bt type, int kind)
3899 gfc_expr *g, *result, *(*f) (gfc_expr *, int);
3900 gfc_constructor *head, *c, *tail = NULL;
3902 switch (e->ts.type)
3904 case BT_INTEGER:
3905 switch (type)
3907 case BT_INTEGER:
3908 f = gfc_int2int;
3909 break;
3910 case BT_REAL:
3911 f = gfc_int2real;
3912 break;
3913 case BT_COMPLEX:
3914 f = gfc_int2complex;
3915 break;
3916 case BT_LOGICAL:
3917 f = gfc_int2log;
3918 break;
3919 default:
3920 goto oops;
3922 break;
3924 case BT_REAL:
3925 switch (type)
3927 case BT_INTEGER:
3928 f = gfc_real2int;
3929 break;
3930 case BT_REAL:
3931 f = gfc_real2real;
3932 break;
3933 case BT_COMPLEX:
3934 f = gfc_real2complex;
3935 break;
3936 default:
3937 goto oops;
3939 break;
3941 case BT_COMPLEX:
3942 switch (type)
3944 case BT_INTEGER:
3945 f = gfc_complex2int;
3946 break;
3947 case BT_REAL:
3948 f = gfc_complex2real;
3949 break;
3950 case BT_COMPLEX:
3951 f = gfc_complex2complex;
3952 break;
3954 default:
3955 goto oops;
3957 break;
3959 case BT_LOGICAL:
3960 switch (type)
3962 case BT_INTEGER:
3963 f = gfc_log2int;
3964 break;
3965 case BT_LOGICAL:
3966 f = gfc_log2log;
3967 break;
3968 default:
3969 goto oops;
3971 break;
3973 case BT_HOLLERITH:
3974 switch (type)
3976 case BT_INTEGER:
3977 f = gfc_hollerith2int;
3978 break;
3980 case BT_REAL:
3981 f = gfc_hollerith2real;
3982 break;
3984 case BT_COMPLEX:
3985 f = gfc_hollerith2complex;
3986 break;
3988 case BT_CHARACTER:
3989 f = gfc_hollerith2character;
3990 break;
3992 case BT_LOGICAL:
3993 f = gfc_hollerith2logical;
3994 break;
3996 default:
3997 goto oops;
3999 break;
4001 default:
4002 oops:
4003 gfc_internal_error ("gfc_convert_constant(): Unexpected type");
4006 result = NULL;
4008 switch (e->expr_type)
4010 case EXPR_CONSTANT:
4011 result = f (e, kind);
4012 if (result == NULL)
4013 return &gfc_bad_expr;
4014 break;
4016 case EXPR_ARRAY:
4017 if (!gfc_is_constant_expr (e))
4018 break;
4020 head = NULL;
4022 for (c = e->value.constructor; c; c = c->next)
4024 if (head == NULL)
4025 head = tail = gfc_get_constructor ();
4026 else
4028 tail->next = gfc_get_constructor ();
4029 tail = tail->next;
4032 tail->where = c->where;
4034 if (c->iterator == NULL)
4035 tail->expr = f (c->expr, kind);
4036 else
4038 g = gfc_convert_constant (c->expr, type, kind);
4039 if (g == &gfc_bad_expr)
4040 return g;
4041 tail->expr = g;
4044 if (tail->expr == NULL)
4046 gfc_free_constructor (head);
4047 return NULL;
4051 result = gfc_get_expr ();
4052 result->ts.type = type;
4053 result->ts.kind = kind;
4054 result->expr_type = EXPR_ARRAY;
4055 result->value.constructor = head;
4056 result->shape = gfc_copy_shape (e->shape, e->rank);
4057 result->where = e->where;
4058 result->rank = e->rank;
4059 break;
4061 default:
4062 break;
4065 return result;