1 /* Simplify intrinsic functions at compile-time.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006
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
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
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
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
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 /* Static table for converting non-ascii character sets to ascii.
68 The xascii_table[] is the inverse table. */
70 static int ascii_table[256] = {
71 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
72 '\b', '\t', '\n', '\v', '\0', '\r', '\0', '\0',
73 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
74 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
75 ' ', '!', '"', '#', '$', '%', '&', '\'',
76 '(', ')', '*', '+', ',', '-', '.', '/',
77 '0', '1', '2', '3', '4', '5', '6', '7',
78 '8', '9', ':', ';', '<', '=', '>', '?',
79 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
80 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
81 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
82 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
83 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
84 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
85 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
86 'x', 'y', 'z', '{', '|', '}', '~', '\?'
89 static int xascii_table[256];
92 /* Range checks an expression node. If all goes well, returns the
93 node, otherwise returns &gfc_bad_expr and frees the node. */
96 range_check (gfc_expr * result, const char *name)
99 switch (gfc_range_check (result))
105 gfc_error ("Result of %s overflows its kind at %L", name, &result->where);
108 case ARITH_UNDERFLOW:
109 gfc_error ("Result of %s underflows its kind at %L", name, &result->where);
113 gfc_error ("Result of %s is NaN at %L", name, &result->where);
117 gfc_error ("Result of %s gives range error for its kind at %L", name, &result->where);
121 gfc_free_expr (result);
122 return &gfc_bad_expr;
126 /* A helper function that gets an optional and possibly missing
127 kind parameter. Returns the kind, -1 if something went wrong. */
130 get_kind (bt type, gfc_expr * k, const char *name, int default_kind)
137 if (k->expr_type != EXPR_CONSTANT)
139 gfc_error ("KIND parameter of %s at %L must be an initialization "
140 "expression", name, &k->where);
145 if (gfc_extract_int (k, &kind) != NULL
146 || gfc_validate_kind (type, kind, true) < 0)
149 gfc_error ("Invalid KIND parameter of %s at %L", name, &k->where);
157 /* Converts an mpz_t signed variable into an unsigned one, assuming
158 two's complement representations and a binary width of bitsize.
159 The conversion is a no-op unless x is negative; otherwise, it can
160 be accomplished by masking out the high bits. */
163 convert_mpz_to_unsigned (mpz_t x, int bitsize)
169 /* Confirm that no bits above the signed range are unset. */
170 gcc_assert (mpz_scan0 (x, bitsize-1) == ULONG_MAX);
172 mpz_init_set_ui (mask, 1);
173 mpz_mul_2exp (mask, mask, bitsize);
174 mpz_sub_ui (mask, mask, 1);
176 mpz_and (x, x, mask);
182 /* Confirm that no bits above the signed range are set. */
183 gcc_assert (mpz_scan1 (x, bitsize-1) == ULONG_MAX);
188 /* Converts an mpz_t unsigned variable into a signed one, assuming
189 two's complement representations and a binary width of bitsize.
190 If the bitsize-1 bit is set, this is taken as a sign bit and
191 the number is converted to the corresponding negative number. */
195 convert_mpz_to_signed (mpz_t x, int bitsize)
199 /* Confirm that no bits above the unsigned range are set. */
200 gcc_assert (mpz_scan1 (x, bitsize) == ULONG_MAX);
202 if (mpz_tstbit (x, bitsize - 1) == 1)
204 mpz_init_set_ui (mask, 1);
205 mpz_mul_2exp (mask, mask, bitsize);
206 mpz_sub_ui (mask, mask, 1);
208 /* We negate the number by hand, zeroing the high bits, that is
209 make it the corresponding positive number, and then have it
210 negated by GMP, giving the correct representation of the
213 mpz_add_ui (x, x, 1);
214 mpz_and (x, x, mask);
223 /********************** Simplification functions *****************************/
226 gfc_simplify_abs (gfc_expr * e)
230 if (e->expr_type != EXPR_CONSTANT)
236 result = gfc_constant_result (BT_INTEGER, e->ts.kind, &e->where);
238 mpz_abs (result->value.integer, e->value.integer);
240 result = range_check (result, "IABS");
244 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
246 mpfr_abs (result->value.real, e->value.real, GFC_RND_MODE);
248 result = range_check (result, "ABS");
252 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
254 gfc_set_model_kind (e->ts.kind);
256 mpfr_hypot (result->value.real, e->value.complex.r,
257 e->value.complex.i, GFC_RND_MODE);
258 result = range_check (result, "CABS");
262 gfc_internal_error ("gfc_simplify_abs(): Bad type");
270 gfc_simplify_achar (gfc_expr * e)
275 if (e->expr_type != EXPR_CONSTANT)
278 /* We cannot assume that the native character set is ASCII in this
280 if (gfc_extract_int (e, &index) != NULL || index < 0 || index > 127)
282 gfc_error ("Extended ASCII not implemented: argument of ACHAR at %L "
283 "must be between 0 and 127", &e->where);
284 return &gfc_bad_expr;
287 result = gfc_constant_result (BT_CHARACTER, gfc_default_character_kind,
290 result->value.character.string = gfc_getmem (2);
292 result->value.character.length = 1;
293 result->value.character.string[0] = ascii_table[index];
294 result->value.character.string[1] = '\0'; /* For debugger */
300 gfc_simplify_acos (gfc_expr * x)
304 if (x->expr_type != EXPR_CONSTANT)
307 if (mpfr_cmp_si (x->value.real, 1) > 0 || mpfr_cmp_si (x->value.real, -1) < 0)
309 gfc_error ("Argument of ACOS at %L must be between -1 and 1",
311 return &gfc_bad_expr;
314 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
316 mpfr_acos (result->value.real, x->value.real, GFC_RND_MODE);
318 return range_check (result, "ACOS");
322 gfc_simplify_acosh (gfc_expr * x)
326 if (x->expr_type != EXPR_CONSTANT)
329 if (mpfr_cmp_si (x->value.real, 1) < 0)
331 gfc_error ("Argument of ACOSH at %L must not be less than 1",
333 return &gfc_bad_expr;
336 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
338 mpfr_acosh (result->value.real, x->value.real, GFC_RND_MODE);
340 return range_check (result, "ACOSH");
344 gfc_simplify_adjustl (gfc_expr * e)
350 if (e->expr_type != EXPR_CONSTANT)
353 len = e->value.character.length;
355 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
357 result->value.character.length = len;
358 result->value.character.string = gfc_getmem (len + 1);
360 for (count = 0, i = 0; i < len; ++i)
362 ch = e->value.character.string[i];
368 for (i = 0; i < len - count; ++i)
370 result->value.character.string[i] =
371 e->value.character.string[count + i];
374 for (i = len - count; i < len; ++i)
376 result->value.character.string[i] = ' ';
379 result->value.character.string[len] = '\0'; /* For debugger */
386 gfc_simplify_adjustr (gfc_expr * e)
392 if (e->expr_type != EXPR_CONSTANT)
395 len = e->value.character.length;
397 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
399 result->value.character.length = len;
400 result->value.character.string = gfc_getmem (len + 1);
402 for (count = 0, i = len - 1; i >= 0; --i)
404 ch = e->value.character.string[i];
410 for (i = 0; i < count; ++i)
412 result->value.character.string[i] = ' ';
415 for (i = count; i < len; ++i)
417 result->value.character.string[i] =
418 e->value.character.string[i - count];
421 result->value.character.string[len] = '\0'; /* For debugger */
428 gfc_simplify_aimag (gfc_expr * e)
433 if (e->expr_type != EXPR_CONSTANT)
436 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
437 mpfr_set (result->value.real, e->value.complex.i, GFC_RND_MODE);
439 return range_check (result, "AIMAG");
444 gfc_simplify_aint (gfc_expr * e, gfc_expr * k)
446 gfc_expr *rtrunc, *result;
449 kind = get_kind (BT_REAL, k, "AINT", e->ts.kind);
451 return &gfc_bad_expr;
453 if (e->expr_type != EXPR_CONSTANT)
456 rtrunc = gfc_copy_expr (e);
458 mpfr_trunc (rtrunc->value.real, e->value.real);
460 result = gfc_real2real (rtrunc, kind);
461 gfc_free_expr (rtrunc);
463 return range_check (result, "AINT");
468 gfc_simplify_dint (gfc_expr * e)
470 gfc_expr *rtrunc, *result;
472 if (e->expr_type != EXPR_CONSTANT)
475 rtrunc = gfc_copy_expr (e);
477 mpfr_trunc (rtrunc->value.real, e->value.real);
479 result = gfc_real2real (rtrunc, gfc_default_double_kind);
480 gfc_free_expr (rtrunc);
482 return range_check (result, "DINT");
487 gfc_simplify_anint (gfc_expr * e, gfc_expr * k)
492 kind = get_kind (BT_REAL, k, "ANINT", e->ts.kind);
494 return &gfc_bad_expr;
496 if (e->expr_type != EXPR_CONSTANT)
499 result = gfc_constant_result (e->ts.type, kind, &e->where);
501 mpfr_round (result->value.real, e->value.real);
503 return range_check (result, "ANINT");
508 gfc_simplify_and (gfc_expr * x, gfc_expr * y)
513 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
516 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
517 if (x->ts.type == BT_INTEGER)
519 result = gfc_constant_result (BT_INTEGER, kind, &x->where);
520 mpz_and (result->value.integer, x->value.integer, y->value.integer);
522 else /* BT_LOGICAL */
524 result = gfc_constant_result (BT_LOGICAL, kind, &x->where);
525 result->value.logical = x->value.logical && y->value.logical;
528 return range_check (result, "AND");
533 gfc_simplify_dnint (gfc_expr * e)
537 if (e->expr_type != EXPR_CONSTANT)
540 result = gfc_constant_result (BT_REAL, gfc_default_double_kind, &e->where);
542 mpfr_round (result->value.real, e->value.real);
544 return range_check (result, "DNINT");
549 gfc_simplify_asin (gfc_expr * x)
553 if (x->expr_type != EXPR_CONSTANT)
556 if (mpfr_cmp_si (x->value.real, 1) > 0 || mpfr_cmp_si (x->value.real, -1) < 0)
558 gfc_error ("Argument of ASIN at %L must be between -1 and 1",
560 return &gfc_bad_expr;
563 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
565 mpfr_asin(result->value.real, x->value.real, GFC_RND_MODE);
567 return range_check (result, "ASIN");
572 gfc_simplify_asinh (gfc_expr * x)
576 if (x->expr_type != EXPR_CONSTANT)
579 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
581 mpfr_asinh(result->value.real, x->value.real, GFC_RND_MODE);
583 return range_check (result, "ASINH");
588 gfc_simplify_atan (gfc_expr * x)
592 if (x->expr_type != EXPR_CONSTANT)
595 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
597 mpfr_atan(result->value.real, x->value.real, GFC_RND_MODE);
599 return range_check (result, "ATAN");
604 gfc_simplify_atanh (gfc_expr * x)
608 if (x->expr_type != EXPR_CONSTANT)
611 if (mpfr_cmp_si (x->value.real, 1) >= 0 ||
612 mpfr_cmp_si (x->value.real, -1) <= 0)
614 gfc_error ("Argument of ATANH at %L must be inside the range -1 to 1",
616 return &gfc_bad_expr;
619 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
621 mpfr_atanh(result->value.real, x->value.real, GFC_RND_MODE);
623 return range_check (result, "ATANH");
628 gfc_simplify_atan2 (gfc_expr * y, gfc_expr * x)
632 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
635 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
637 if (mpfr_sgn (y->value.real) == 0 && mpfr_sgn (x->value.real) == 0)
640 ("If first argument of ATAN2 %L is zero, then the second argument "
641 "must not be zero", &x->where);
642 gfc_free_expr (result);
643 return &gfc_bad_expr;
646 mpfr_atan2 (result->value.real, y->value.real, x->value.real, GFC_RND_MODE);
648 return range_check (result, "ATAN2");
653 gfc_simplify_bit_size (gfc_expr * e)
658 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
659 result = gfc_constant_result (BT_INTEGER, e->ts.kind, &e->where);
660 mpz_set_ui (result->value.integer, gfc_integer_kinds[i].bit_size);
667 gfc_simplify_btest (gfc_expr * e, gfc_expr * bit)
671 if (e->expr_type != EXPR_CONSTANT || bit->expr_type != EXPR_CONSTANT)
674 if (gfc_extract_int (bit, &b) != NULL || b < 0)
675 return gfc_logical_expr (0, &e->where);
677 return gfc_logical_expr (mpz_tstbit (e->value.integer, b), &e->where);
682 gfc_simplify_ceiling (gfc_expr * e, gfc_expr * k)
684 gfc_expr *ceil, *result;
687 kind = get_kind (BT_INTEGER, k, "CEILING", gfc_default_integer_kind);
689 return &gfc_bad_expr;
691 if (e->expr_type != EXPR_CONSTANT)
694 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
696 ceil = gfc_copy_expr (e);
698 mpfr_ceil (ceil->value.real, e->value.real);
699 gfc_mpfr_to_mpz(result->value.integer, ceil->value.real);
701 gfc_free_expr (ceil);
703 return range_check (result, "CEILING");
708 gfc_simplify_char (gfc_expr * e, gfc_expr * k)
713 kind = get_kind (BT_CHARACTER, k, "CHAR", gfc_default_character_kind);
715 return &gfc_bad_expr;
717 if (e->expr_type != EXPR_CONSTANT)
720 if (gfc_extract_int (e, &c) != NULL || c < 0 || c > UCHAR_MAX)
722 gfc_error ("Bad character in CHAR function at %L", &e->where);
723 return &gfc_bad_expr;
726 result = gfc_constant_result (BT_CHARACTER, kind, &e->where);
728 result->value.character.length = 1;
729 result->value.character.string = gfc_getmem (2);
731 result->value.character.string[0] = c;
732 result->value.character.string[1] = '\0'; /* For debugger */
738 /* Common subroutine for simplifying CMPLX and DCMPLX. */
741 simplify_cmplx (const char *name, gfc_expr * x, gfc_expr * y, int kind)
745 result = gfc_constant_result (BT_COMPLEX, kind, &x->where);
747 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
752 mpfr_set_z (result->value.complex.r, x->value.integer, GFC_RND_MODE);
756 mpfr_set (result->value.complex.r, x->value.real, GFC_RND_MODE);
760 mpfr_set (result->value.complex.r, x->value.complex.r, GFC_RND_MODE);
761 mpfr_set (result->value.complex.i, x->value.complex.i, GFC_RND_MODE);
765 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (x)");
773 mpfr_set_z (result->value.complex.i, y->value.integer, GFC_RND_MODE);
777 mpfr_set (result->value.complex.i, y->value.real, GFC_RND_MODE);
781 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (y)");
785 return range_check (result, name);
790 gfc_simplify_cmplx (gfc_expr * x, gfc_expr * y, gfc_expr * k)
794 if (x->expr_type != EXPR_CONSTANT
795 || (y != NULL && y->expr_type != EXPR_CONSTANT))
798 kind = get_kind (BT_REAL, k, "CMPLX", gfc_default_real_kind);
800 return &gfc_bad_expr;
802 return simplify_cmplx ("CMPLX", x, y, kind);
807 gfc_simplify_complex (gfc_expr * x, gfc_expr * y)
811 if (x->expr_type != EXPR_CONSTANT
812 || (y != NULL && y->expr_type != EXPR_CONSTANT))
815 if (x->ts.type == BT_INTEGER)
817 if (y->ts.type == BT_INTEGER)
818 kind = gfc_default_real_kind;
824 if (y->ts.type == BT_REAL)
825 kind = (x->ts.kind > y->ts.kind) ? x->ts.kind : y->ts.kind;
830 return simplify_cmplx ("COMPLEX", x, y, kind);
835 gfc_simplify_conjg (gfc_expr * e)
839 if (e->expr_type != EXPR_CONSTANT)
842 result = gfc_copy_expr (e);
843 mpfr_neg (result->value.complex.i, result->value.complex.i, GFC_RND_MODE);
845 return range_check (result, "CONJG");
850 gfc_simplify_cos (gfc_expr * x)
855 if (x->expr_type != EXPR_CONSTANT)
858 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
863 mpfr_cos (result->value.real, x->value.real, GFC_RND_MODE);
866 gfc_set_model_kind (x->ts.kind);
870 mpfr_cos (xp, x->value.complex.r, GFC_RND_MODE);
871 mpfr_cosh (xq, x->value.complex.i, GFC_RND_MODE);
872 mpfr_mul(result->value.complex.r, xp, xq, GFC_RND_MODE);
874 mpfr_sin (xp, x->value.complex.r, GFC_RND_MODE);
875 mpfr_sinh (xq, x->value.complex.i, GFC_RND_MODE);
876 mpfr_mul (xp, xp, xq, GFC_RND_MODE);
877 mpfr_neg (result->value.complex.i, xp, GFC_RND_MODE );
883 gfc_internal_error ("in gfc_simplify_cos(): Bad type");
886 return range_check (result, "COS");
892 gfc_simplify_cosh (gfc_expr * x)
896 if (x->expr_type != EXPR_CONSTANT)
899 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
901 mpfr_cosh (result->value.real, x->value.real, GFC_RND_MODE);
903 return range_check (result, "COSH");
908 gfc_simplify_dcmplx (gfc_expr * x, gfc_expr * y)
911 if (x->expr_type != EXPR_CONSTANT
912 || (y != NULL && y->expr_type != EXPR_CONSTANT))
915 return simplify_cmplx ("DCMPLX", x, y, gfc_default_double_kind);
920 gfc_simplify_dble (gfc_expr * e)
924 if (e->expr_type != EXPR_CONSTANT)
930 result = gfc_int2real (e, gfc_default_double_kind);
934 result = gfc_real2real (e, gfc_default_double_kind);
938 result = gfc_complex2real (e, gfc_default_double_kind);
942 gfc_internal_error ("gfc_simplify_dble(): bad type at %L", &e->where);
945 return range_check (result, "DBLE");
950 gfc_simplify_digits (gfc_expr * x)
954 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
958 digits = gfc_integer_kinds[i].digits;
963 digits = gfc_real_kinds[i].digits;
970 return gfc_int_expr (digits);
975 gfc_simplify_dim (gfc_expr * x, gfc_expr * y)
980 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
983 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
984 result = gfc_constant_result (x->ts.type, kind, &x->where);
989 if (mpz_cmp (x->value.integer, y->value.integer) > 0)
990 mpz_sub (result->value.integer, x->value.integer, y->value.integer);
992 mpz_set_ui (result->value.integer, 0);
997 if (mpfr_cmp (x->value.real, y->value.real) > 0)
998 mpfr_sub (result->value.real, x->value.real, y->value.real, GFC_RND_MODE);
1000 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
1005 gfc_internal_error ("gfc_simplify_dim(): Bad type");
1008 return range_check (result, "DIM");
1013 gfc_simplify_dprod (gfc_expr * x, gfc_expr * y)
1015 gfc_expr *a1, *a2, *result;
1017 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1021 gfc_constant_result (BT_REAL, gfc_default_double_kind, &x->where);
1023 a1 = gfc_real2real (x, gfc_default_double_kind);
1024 a2 = gfc_real2real (y, gfc_default_double_kind);
1026 mpfr_mul (result->value.real, a1->value.real, a2->value.real, GFC_RND_MODE);
1031 return range_check (result, "DPROD");
1036 gfc_simplify_epsilon (gfc_expr * e)
1041 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
1043 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
1045 mpfr_set (result->value.real, gfc_real_kinds[i].epsilon, GFC_RND_MODE);
1047 return range_check (result, "EPSILON");
1052 gfc_simplify_exp (gfc_expr * x)
1057 if (x->expr_type != EXPR_CONSTANT)
1060 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1065 mpfr_exp(result->value.real, x->value.real, GFC_RND_MODE);
1069 gfc_set_model_kind (x->ts.kind);
1072 mpfr_exp (xq, x->value.complex.r, GFC_RND_MODE);
1073 mpfr_cos (xp, x->value.complex.i, GFC_RND_MODE);
1074 mpfr_mul (result->value.complex.r, xq, xp, GFC_RND_MODE);
1075 mpfr_sin (xp, x->value.complex.i, GFC_RND_MODE);
1076 mpfr_mul (result->value.complex.i, xq, xp, GFC_RND_MODE);
1082 gfc_internal_error ("in gfc_simplify_exp(): Bad type");
1085 return range_check (result, "EXP");
1088 /* FIXME: MPFR should be able to do this better */
1090 gfc_simplify_exponent (gfc_expr * x)
1095 if (x->expr_type != EXPR_CONSTANT)
1098 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
1101 gfc_set_model (x->value.real);
1103 if (mpfr_sgn (x->value.real) == 0)
1105 mpz_set_ui (result->value.integer, 0);
1109 i = (int) mpfr_get_exp (x->value.real);
1110 mpz_set_si (result->value.integer, i);
1112 return range_check (result, "EXPONENT");
1117 gfc_simplify_float (gfc_expr * a)
1121 if (a->expr_type != EXPR_CONSTANT)
1124 result = gfc_int2real (a, gfc_default_real_kind);
1125 return range_check (result, "FLOAT");
1130 gfc_simplify_floor (gfc_expr * e, gfc_expr * k)
1136 kind = get_kind (BT_INTEGER, k, "FLOOR", gfc_default_integer_kind);
1138 gfc_internal_error ("gfc_simplify_floor(): Bad kind");
1140 if (e->expr_type != EXPR_CONSTANT)
1143 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
1145 gfc_set_model_kind (kind);
1147 mpfr_floor (floor, e->value.real);
1149 gfc_mpfr_to_mpz (result->value.integer, floor);
1153 return range_check (result, "FLOOR");
1158 gfc_simplify_fraction (gfc_expr * x)
1161 mpfr_t absv, exp, pow2;
1163 if (x->expr_type != EXPR_CONSTANT)
1166 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
1168 gfc_set_model_kind (x->ts.kind);
1170 if (mpfr_sgn (x->value.real) == 0)
1172 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
1180 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
1181 mpfr_log2 (exp, absv, GFC_RND_MODE);
1183 mpfr_trunc (exp, exp);
1184 mpfr_add_ui (exp, exp, 1, GFC_RND_MODE);
1186 mpfr_ui_pow (pow2, 2, exp, GFC_RND_MODE);
1188 mpfr_div (result->value.real, absv, pow2, GFC_RND_MODE);
1194 return range_check (result, "FRACTION");
1199 gfc_simplify_huge (gfc_expr * e)
1204 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
1206 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
1211 mpz_set (result->value.integer, gfc_integer_kinds[i].huge);
1215 mpfr_set (result->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
1227 gfc_simplify_iachar (gfc_expr * e)
1232 if (e->expr_type != EXPR_CONSTANT)
1235 if (e->value.character.length != 1)
1237 gfc_error ("Argument of IACHAR at %L must be of length one", &e->where);
1238 return &gfc_bad_expr;
1241 index = xascii_table[(int) e->value.character.string[0] & 0xFF];
1243 result = gfc_int_expr (index);
1244 result->where = e->where;
1246 return range_check (result, "IACHAR");
1251 gfc_simplify_iand (gfc_expr * x, gfc_expr * y)
1255 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1258 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
1260 mpz_and (result->value.integer, x->value.integer, y->value.integer);
1262 return range_check (result, "IAND");
1267 gfc_simplify_ibclr (gfc_expr * x, gfc_expr * y)
1272 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1275 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
1277 gfc_error ("Invalid second argument of IBCLR at %L", &y->where);
1278 return &gfc_bad_expr;
1281 k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
1283 if (pos > gfc_integer_kinds[k].bit_size)
1285 gfc_error ("Second argument of IBCLR exceeds bit size at %L",
1287 return &gfc_bad_expr;
1290 result = gfc_copy_expr (x);
1292 convert_mpz_to_unsigned (result->value.integer,
1293 gfc_integer_kinds[k].bit_size);
1295 mpz_clrbit (result->value.integer, pos);
1297 convert_mpz_to_signed (result->value.integer,
1298 gfc_integer_kinds[k].bit_size);
1300 return range_check (result, "IBCLR");
1305 gfc_simplify_ibits (gfc_expr * x, gfc_expr * y, gfc_expr * z)
1312 if (x->expr_type != EXPR_CONSTANT
1313 || y->expr_type != EXPR_CONSTANT
1314 || z->expr_type != EXPR_CONSTANT)
1317 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
1319 gfc_error ("Invalid second argument of IBITS at %L", &y->where);
1320 return &gfc_bad_expr;
1323 if (gfc_extract_int (z, &len) != NULL || len < 0)
1325 gfc_error ("Invalid third argument of IBITS at %L", &z->where);
1326 return &gfc_bad_expr;
1329 k = gfc_validate_kind (BT_INTEGER, x->ts.kind, false);
1331 bitsize = gfc_integer_kinds[k].bit_size;
1333 if (pos + len > bitsize)
1335 gfc_error ("Sum of second and third arguments of IBITS exceeds "
1336 "bit size at %L", &y->where);
1337 return &gfc_bad_expr;
1340 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1342 bits = gfc_getmem (bitsize * sizeof (int));
1344 for (i = 0; i < bitsize; i++)
1347 for (i = 0; i < len; i++)
1348 bits[i] = mpz_tstbit (x->value.integer, i + pos);
1350 for (i = 0; i < bitsize; i++)
1354 mpz_clrbit (result->value.integer, i);
1356 else if (bits[i] == 1)
1358 mpz_setbit (result->value.integer, i);
1362 gfc_internal_error ("IBITS: Bad bit");
1368 return range_check (result, "IBITS");
1373 gfc_simplify_ibset (gfc_expr * x, gfc_expr * y)
1378 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1381 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
1383 gfc_error ("Invalid second argument of IBSET at %L", &y->where);
1384 return &gfc_bad_expr;
1387 k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
1389 if (pos > gfc_integer_kinds[k].bit_size)
1391 gfc_error ("Second argument of IBSET exceeds bit size at %L",
1393 return &gfc_bad_expr;
1396 result = gfc_copy_expr (x);
1398 convert_mpz_to_unsigned (result->value.integer,
1399 gfc_integer_kinds[k].bit_size);
1401 mpz_setbit (result->value.integer, pos);
1403 convert_mpz_to_signed (result->value.integer,
1404 gfc_integer_kinds[k].bit_size);
1406 return range_check (result, "IBSET");
1411 gfc_simplify_ichar (gfc_expr * e)
1416 if (e->expr_type != EXPR_CONSTANT)
1419 if (e->value.character.length != 1)
1421 gfc_error ("Argument of ICHAR at %L must be of length one", &e->where);
1422 return &gfc_bad_expr;
1425 index = (unsigned char) e->value.character.string[0];
1427 if (index < 0 || index > UCHAR_MAX)
1429 gfc_error ("Argument of ICHAR at %L out of range of this processor",
1431 return &gfc_bad_expr;
1434 result = gfc_int_expr (index);
1435 result->where = e->where;
1436 return range_check (result, "ICHAR");
1441 gfc_simplify_ieor (gfc_expr * x, gfc_expr * y)
1445 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1448 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
1450 mpz_xor (result->value.integer, x->value.integer, y->value.integer);
1452 return range_check (result, "IEOR");
1457 gfc_simplify_index (gfc_expr * x, gfc_expr * y, gfc_expr * b)
1460 int back, len, lensub;
1461 int i, j, k, count, index = 0, start;
1463 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1466 if (b != NULL && b->value.logical != 0)
1471 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
1474 len = x->value.character.length;
1475 lensub = y->value.character.length;
1479 mpz_set_si (result->value.integer, 0);
1488 mpz_set_si (result->value.integer, 1);
1491 else if (lensub == 1)
1493 for (i = 0; i < len; i++)
1495 for (j = 0; j < lensub; j++)
1497 if (y->value.character.string[j] ==
1498 x->value.character.string[i])
1508 for (i = 0; i < len; i++)
1510 for (j = 0; j < lensub; j++)
1512 if (y->value.character.string[j] ==
1513 x->value.character.string[i])
1518 for (k = 0; k < lensub; k++)
1520 if (y->value.character.string[k] ==
1521 x->value.character.string[k + start])
1525 if (count == lensub)
1541 mpz_set_si (result->value.integer, len + 1);
1544 else if (lensub == 1)
1546 for (i = 0; i < len; i++)
1548 for (j = 0; j < lensub; j++)
1550 if (y->value.character.string[j] ==
1551 x->value.character.string[len - i])
1553 index = len - i + 1;
1561 for (i = 0; i < len; i++)
1563 for (j = 0; j < lensub; j++)
1565 if (y->value.character.string[j] ==
1566 x->value.character.string[len - i])
1569 if (start <= len - lensub)
1572 for (k = 0; k < lensub; k++)
1573 if (y->value.character.string[k] ==
1574 x->value.character.string[k + start])
1577 if (count == lensub)
1594 mpz_set_si (result->value.integer, index);
1595 return range_check (result, "INDEX");
1600 gfc_simplify_int (gfc_expr * e, gfc_expr * k)
1602 gfc_expr *rpart, *rtrunc, *result;
1605 kind = get_kind (BT_INTEGER, k, "INT", gfc_default_integer_kind);
1607 return &gfc_bad_expr;
1609 if (e->expr_type != EXPR_CONSTANT)
1612 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
1617 mpz_set (result->value.integer, e->value.integer);
1621 rtrunc = gfc_copy_expr (e);
1622 mpfr_trunc (rtrunc->value.real, e->value.real);
1623 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1624 gfc_free_expr (rtrunc);
1628 rpart = gfc_complex2real (e, kind);
1629 rtrunc = gfc_copy_expr (rpart);
1630 mpfr_trunc (rtrunc->value.real, rpart->value.real);
1631 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1632 gfc_free_expr (rpart);
1633 gfc_free_expr (rtrunc);
1637 gfc_error ("Argument of INT at %L is not a valid type", &e->where);
1638 gfc_free_expr (result);
1639 return &gfc_bad_expr;
1642 return range_check (result, "INT");
1647 gfc_simplify_intconv (gfc_expr * e, int kind, const char *name)
1649 gfc_expr *rpart, *rtrunc, *result;
1651 if (e->expr_type != EXPR_CONSTANT)
1654 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
1659 mpz_set (result->value.integer, e->value.integer);
1663 rtrunc = gfc_copy_expr (e);
1664 mpfr_trunc (rtrunc->value.real, e->value.real);
1665 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1666 gfc_free_expr (rtrunc);
1670 rpart = gfc_complex2real (e, kind);
1671 rtrunc = gfc_copy_expr (rpart);
1672 mpfr_trunc (rtrunc->value.real, rpart->value.real);
1673 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1674 gfc_free_expr (rpart);
1675 gfc_free_expr (rtrunc);
1679 gfc_error ("Argument of %s at %L is not a valid type", name, &e->where);
1680 gfc_free_expr (result);
1681 return &gfc_bad_expr;
1684 return range_check (result, name);
1688 gfc_simplify_int2 (gfc_expr * e)
1690 return gfc_simplify_intconv (e, 2, "INT2");
1694 gfc_simplify_int8 (gfc_expr * e)
1696 return gfc_simplify_intconv (e, 8, "INT8");
1700 gfc_simplify_long (gfc_expr * e)
1702 return gfc_simplify_intconv (e, 4, "LONG");
1707 gfc_simplify_ifix (gfc_expr * e)
1709 gfc_expr *rtrunc, *result;
1711 if (e->expr_type != EXPR_CONSTANT)
1714 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
1717 rtrunc = gfc_copy_expr (e);
1719 mpfr_trunc (rtrunc->value.real, e->value.real);
1720 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1722 gfc_free_expr (rtrunc);
1723 return range_check (result, "IFIX");
1728 gfc_simplify_idint (gfc_expr * e)
1730 gfc_expr *rtrunc, *result;
1732 if (e->expr_type != EXPR_CONSTANT)
1735 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
1738 rtrunc = gfc_copy_expr (e);
1740 mpfr_trunc (rtrunc->value.real, e->value.real);
1741 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1743 gfc_free_expr (rtrunc);
1744 return range_check (result, "IDINT");
1749 gfc_simplify_ior (gfc_expr * x, gfc_expr * y)
1753 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1756 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
1758 mpz_ior (result->value.integer, x->value.integer, y->value.integer);
1759 return range_check (result, "IOR");
1764 gfc_simplify_ishft (gfc_expr * e, gfc_expr * s)
1767 int shift, ashift, isize, k, *bits, i;
1769 if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
1772 if (gfc_extract_int (s, &shift) != NULL)
1774 gfc_error ("Invalid second argument of ISHFT at %L", &s->where);
1775 return &gfc_bad_expr;
1778 k = gfc_validate_kind (BT_INTEGER, e->ts.kind, false);
1780 isize = gfc_integer_kinds[k].bit_size;
1790 ("Magnitude of second argument of ISHFT exceeds bit size at %L",
1792 return &gfc_bad_expr;
1795 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
1799 mpz_set (result->value.integer, e->value.integer);
1800 return range_check (result, "ISHFT");
1803 bits = gfc_getmem (isize * sizeof (int));
1805 for (i = 0; i < isize; i++)
1806 bits[i] = mpz_tstbit (e->value.integer, i);
1810 for (i = 0; i < shift; i++)
1811 mpz_clrbit (result->value.integer, i);
1813 for (i = 0; i < isize - shift; i++)
1816 mpz_clrbit (result->value.integer, i + shift);
1818 mpz_setbit (result->value.integer, i + shift);
1823 for (i = isize - 1; i >= isize - ashift; i--)
1824 mpz_clrbit (result->value.integer, i);
1826 for (i = isize - 1; i >= ashift; i--)
1829 mpz_clrbit (result->value.integer, i - ashift);
1831 mpz_setbit (result->value.integer, i - ashift);
1835 convert_mpz_to_signed (result->value.integer, isize);
1843 gfc_simplify_ishftc (gfc_expr * e, gfc_expr * s, gfc_expr * sz)
1846 int shift, ashift, isize, ssize, delta, k;
1849 if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
1852 if (gfc_extract_int (s, &shift) != NULL)
1854 gfc_error ("Invalid second argument of ISHFTC at %L", &s->where);
1855 return &gfc_bad_expr;
1858 k = gfc_validate_kind (e->ts.type, e->ts.kind, false);
1859 isize = gfc_integer_kinds[k].bit_size;
1863 if (sz->expr_type != EXPR_CONSTANT)
1866 if (gfc_extract_int (sz, &ssize) != NULL || ssize <= 0)
1868 gfc_error ("Invalid third argument of ISHFTC at %L", &sz->where);
1869 return &gfc_bad_expr;
1874 gfc_error ("Magnitude of third argument of ISHFTC exceeds "
1875 "BIT_SIZE of first argument at %L", &s->where);
1876 return &gfc_bad_expr;
1890 gfc_error ("Magnitude of second argument of ISHFTC exceeds "
1891 "third argument at %L", &s->where);
1893 gfc_error ("Magnitude of second argument of ISHFTC exceeds "
1894 "BIT_SIZE of first argument at %L", &s->where);
1895 return &gfc_bad_expr;
1898 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
1900 mpz_set (result->value.integer, e->value.integer);
1905 convert_mpz_to_unsigned (result->value.integer, isize);
1907 bits = gfc_getmem (ssize * sizeof (int));
1909 for (i = 0; i < ssize; i++)
1910 bits[i] = mpz_tstbit (e->value.integer, i);
1912 delta = ssize - ashift;
1916 for (i = 0; i < delta; i++)
1919 mpz_clrbit (result->value.integer, i + shift);
1921 mpz_setbit (result->value.integer, i + shift);
1924 for (i = delta; i < ssize; i++)
1927 mpz_clrbit (result->value.integer, i - delta);
1929 mpz_setbit (result->value.integer, i - delta);
1934 for (i = 0; i < ashift; i++)
1937 mpz_clrbit (result->value.integer, i + delta);
1939 mpz_setbit (result->value.integer, i + delta);
1942 for (i = ashift; i < ssize; i++)
1945 mpz_clrbit (result->value.integer, i + shift);
1947 mpz_setbit (result->value.integer, i + shift);
1951 convert_mpz_to_signed (result->value.integer, isize);
1959 gfc_simplify_kind (gfc_expr * e)
1962 if (e->ts.type == BT_DERIVED)
1964 gfc_error ("Argument of KIND at %L is a DERIVED type", &e->where);
1965 return &gfc_bad_expr;
1968 return gfc_int_expr (e->ts.kind);
1973 simplify_bound (gfc_expr * array, gfc_expr * dim, int upper)
1977 gfc_expr *l, *u, *result;
1981 /* TODO: Simplify constant multi-dimensional bounds. */
1984 if (dim->expr_type != EXPR_CONSTANT)
1987 if (array->expr_type != EXPR_VARIABLE)
1990 /* Follow any component references. */
1991 as = array->symtree->n.sym->as;
1992 for (ref = array->ref; ref; ref = ref->next)
1997 switch (ref->u.ar.type)
2004 /* We're done because 'as' has already been set in the
2005 previous iteration. */
2016 as = ref->u.c.component->as;
2027 if (as->type == AS_DEFERRED || as->type == AS_ASSUMED_SHAPE)
2030 d = mpz_get_si (dim->value.integer);
2032 if (d < 1 || d > as->rank
2033 || (d == as->rank && as->type == AS_ASSUMED_SIZE && upper))
2035 gfc_error ("DIM argument at %L is out of bounds", &dim->where);
2036 return &gfc_bad_expr;
2039 /* The last dimension of an assumed-size array is special. */
2040 if (d == as->rank && as->type == AS_ASSUMED_SIZE && !upper)
2042 if (as->lower[d-1]->expr_type == EXPR_CONSTANT)
2043 return gfc_copy_expr (as->lower[d-1]);
2048 /* Then, we need to know the extent of the given dimension. */
2052 if (l->expr_type != EXPR_CONSTANT || u->expr_type != EXPR_CONSTANT)
2055 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
2058 if (mpz_cmp (l->value.integer, u->value.integer) > 0)
2062 mpz_set_si (result->value.integer, 0);
2064 mpz_set_si (result->value.integer, 1);
2068 /* Nonzero extent. */
2070 mpz_set (result->value.integer, u->value.integer);
2072 mpz_set (result->value.integer, l->value.integer);
2075 return range_check (result, upper ? "UBOUND" : "LBOUND");
2080 gfc_simplify_lbound (gfc_expr * array, gfc_expr * dim)
2082 return simplify_bound (array, dim, 0);
2087 gfc_simplify_len (gfc_expr * e)
2091 if (e->expr_type == EXPR_CONSTANT)
2093 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
2095 mpz_set_si (result->value.integer, e->value.character.length);
2096 return range_check (result, "LEN");
2099 if (e->ts.cl != NULL && e->ts.cl->length != NULL
2100 && e->ts.cl->length->expr_type == EXPR_CONSTANT)
2102 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
2104 mpz_set (result->value.integer, e->ts.cl->length->value.integer);
2105 return range_check (result, "LEN");
2113 gfc_simplify_len_trim (gfc_expr * e)
2116 int count, len, lentrim, i;
2118 if (e->expr_type != EXPR_CONSTANT)
2121 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
2124 len = e->value.character.length;
2126 for (count = 0, i = 1; i <= len; i++)
2127 if (e->value.character.string[len - i] == ' ')
2132 lentrim = len - count;
2134 mpz_set_si (result->value.integer, lentrim);
2135 return range_check (result, "LEN_TRIM");
2140 gfc_simplify_lge (gfc_expr * a, gfc_expr * b)
2143 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
2146 return gfc_logical_expr (gfc_compare_string (a, b, xascii_table) >= 0,
2152 gfc_simplify_lgt (gfc_expr * a, gfc_expr * b)
2155 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
2158 return gfc_logical_expr (gfc_compare_string (a, b, xascii_table) > 0,
2164 gfc_simplify_lle (gfc_expr * a, gfc_expr * b)
2167 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
2170 return gfc_logical_expr (gfc_compare_string (a, b, xascii_table) <= 0,
2176 gfc_simplify_llt (gfc_expr * a, gfc_expr * b)
2179 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
2182 return gfc_logical_expr (gfc_compare_string (a, b, xascii_table) < 0,
2188 gfc_simplify_log (gfc_expr * x)
2193 if (x->expr_type != EXPR_CONSTANT)
2196 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
2198 gfc_set_model_kind (x->ts.kind);
2203 if (mpfr_sgn (x->value.real) <= 0)
2206 ("Argument of LOG at %L cannot be less than or equal to zero",
2208 gfc_free_expr (result);
2209 return &gfc_bad_expr;
2212 mpfr_log(result->value.real, x->value.real, GFC_RND_MODE);
2216 if ((mpfr_sgn (x->value.complex.r) == 0)
2217 && (mpfr_sgn (x->value.complex.i) == 0))
2219 gfc_error ("Complex argument of LOG at %L cannot be zero",
2221 gfc_free_expr (result);
2222 return &gfc_bad_expr;
2228 mpfr_atan2 (result->value.complex.i, x->value.complex.i, x->value.complex.r,
2231 mpfr_mul (xr, x->value.complex.r, x->value.complex.r, GFC_RND_MODE);
2232 mpfr_mul (xi, x->value.complex.i, x->value.complex.i, GFC_RND_MODE);
2233 mpfr_add (xr, xr, xi, GFC_RND_MODE);
2234 mpfr_sqrt (xr, xr, GFC_RND_MODE);
2235 mpfr_log (result->value.complex.r, xr, GFC_RND_MODE);
2243 gfc_internal_error ("gfc_simplify_log: bad type");
2246 return range_check (result, "LOG");
2251 gfc_simplify_log10 (gfc_expr * x)
2255 if (x->expr_type != EXPR_CONSTANT)
2258 gfc_set_model_kind (x->ts.kind);
2260 if (mpfr_sgn (x->value.real) <= 0)
2263 ("Argument of LOG10 at %L cannot be less than or equal to zero",
2265 return &gfc_bad_expr;
2268 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
2270 mpfr_log10 (result->value.real, x->value.real, GFC_RND_MODE);
2272 return range_check (result, "LOG10");
2277 gfc_simplify_logical (gfc_expr * e, gfc_expr * k)
2282 kind = get_kind (BT_LOGICAL, k, "LOGICAL", gfc_default_logical_kind);
2284 return &gfc_bad_expr;
2286 if (e->expr_type != EXPR_CONSTANT)
2289 result = gfc_constant_result (BT_LOGICAL, kind, &e->where);
2291 result->value.logical = e->value.logical;
2297 /* This function is special since MAX() can take any number of
2298 arguments. The simplified expression is a rewritten version of the
2299 argument list containing at most one constant element. Other
2300 constant elements are deleted. Because the argument list has
2301 already been checked, this function always succeeds. sign is 1 for
2302 MAX(), -1 for MIN(). */
2305 simplify_min_max (gfc_expr * expr, int sign)
2307 gfc_actual_arglist *arg, *last, *extremum;
2308 gfc_intrinsic_sym * specific;
2312 specific = expr->value.function.isym;
2314 arg = expr->value.function.actual;
2316 for (; arg; last = arg, arg = arg->next)
2318 if (arg->expr->expr_type != EXPR_CONSTANT)
2321 if (extremum == NULL)
2327 switch (arg->expr->ts.type)
2330 if (mpz_cmp (arg->expr->value.integer,
2331 extremum->expr->value.integer) * sign > 0)
2332 mpz_set (extremum->expr->value.integer, arg->expr->value.integer);
2337 if (mpfr_cmp (arg->expr->value.real, extremum->expr->value.real) *
2339 mpfr_set (extremum->expr->value.real, arg->expr->value.real,
2345 gfc_internal_error ("gfc_simplify_max(): Bad type in arglist");
2348 /* Delete the extra constant argument. */
2350 expr->value.function.actual = arg->next;
2352 last->next = arg->next;
2355 gfc_free_actual_arglist (arg);
2359 /* If there is one value left, replace the function call with the
2361 if (expr->value.function.actual->next != NULL)
2364 /* Convert to the correct type and kind. */
2365 if (expr->ts.type != BT_UNKNOWN)
2366 return gfc_convert_constant (expr->value.function.actual->expr,
2367 expr->ts.type, expr->ts.kind);
2369 if (specific->ts.type != BT_UNKNOWN)
2370 return gfc_convert_constant (expr->value.function.actual->expr,
2371 specific->ts.type, specific->ts.kind);
2373 return gfc_copy_expr (expr->value.function.actual->expr);
2378 gfc_simplify_min (gfc_expr * e)
2380 return simplify_min_max (e, -1);
2385 gfc_simplify_max (gfc_expr * e)
2387 return simplify_min_max (e, 1);
2392 gfc_simplify_maxexponent (gfc_expr * x)
2397 i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
2399 result = gfc_int_expr (gfc_real_kinds[i].max_exponent);
2400 result->where = x->where;
2407 gfc_simplify_minexponent (gfc_expr * x)
2412 i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
2414 result = gfc_int_expr (gfc_real_kinds[i].min_exponent);
2415 result->where = x->where;
2422 gfc_simplify_mod (gfc_expr * a, gfc_expr * p)
2425 mpfr_t quot, iquot, term;
2428 if (a->expr_type != EXPR_CONSTANT || p->expr_type != EXPR_CONSTANT)
2431 kind = a->ts.kind > p->ts.kind ? a->ts.kind : p->ts.kind;
2432 result = gfc_constant_result (a->ts.type, kind, &a->where);
2437 if (mpz_cmp_ui (p->value.integer, 0) == 0)
2439 /* Result is processor-dependent. */
2440 gfc_error ("Second argument MOD at %L is zero", &a->where);
2441 gfc_free_expr (result);
2442 return &gfc_bad_expr;
2444 mpz_tdiv_r (result->value.integer, a->value.integer, p->value.integer);
2448 if (mpfr_cmp_ui (p->value.real, 0) == 0)
2450 /* Result is processor-dependent. */
2451 gfc_error ("Second argument of MOD at %L is zero", &p->where);
2452 gfc_free_expr (result);
2453 return &gfc_bad_expr;
2456 gfc_set_model_kind (kind);
2461 mpfr_div (quot, a->value.real, p->value.real, GFC_RND_MODE);
2462 mpfr_trunc (iquot, quot);
2463 mpfr_mul (term, iquot, p->value.real, GFC_RND_MODE);
2464 mpfr_sub (result->value.real, a->value.real, term, GFC_RND_MODE);
2472 gfc_internal_error ("gfc_simplify_mod(): Bad arguments");
2475 return range_check (result, "MOD");
2480 gfc_simplify_modulo (gfc_expr * a, gfc_expr * p)
2483 mpfr_t quot, iquot, term;
2486 if (a->expr_type != EXPR_CONSTANT || p->expr_type != EXPR_CONSTANT)
2489 kind = a->ts.kind > p->ts.kind ? a->ts.kind : p->ts.kind;
2490 result = gfc_constant_result (a->ts.type, kind, &a->where);
2495 if (mpz_cmp_ui (p->value.integer, 0) == 0)
2497 /* Result is processor-dependent. This processor just opts
2498 to not handle it at all. */
2499 gfc_error ("Second argument of MODULO at %L is zero", &a->where);
2500 gfc_free_expr (result);
2501 return &gfc_bad_expr;
2503 mpz_fdiv_r (result->value.integer, a->value.integer, p->value.integer);
2508 if (mpfr_cmp_ui (p->value.real, 0) == 0)
2510 /* Result is processor-dependent. */
2511 gfc_error ("Second argument of MODULO at %L is zero", &p->where);
2512 gfc_free_expr (result);
2513 return &gfc_bad_expr;
2516 gfc_set_model_kind (kind);
2521 mpfr_div (quot, a->value.real, p->value.real, GFC_RND_MODE);
2522 mpfr_floor (iquot, quot);
2523 mpfr_mul (term, iquot, p->value.real, GFC_RND_MODE);
2524 mpfr_sub (result->value.real, a->value.real, term, GFC_RND_MODE);
2532 gfc_internal_error ("gfc_simplify_modulo(): Bad arguments");
2535 return range_check (result, "MODULO");
2539 /* Exists for the sole purpose of consistency with other intrinsics. */
2541 gfc_simplify_mvbits (gfc_expr * f ATTRIBUTE_UNUSED,
2542 gfc_expr * fp ATTRIBUTE_UNUSED,
2543 gfc_expr * l ATTRIBUTE_UNUSED,
2544 gfc_expr * to ATTRIBUTE_UNUSED,
2545 gfc_expr * tp ATTRIBUTE_UNUSED)
2552 gfc_simplify_nearest (gfc_expr * x, gfc_expr * s)
2558 if (x->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
2561 if (mpfr_sgn (s->value.real) == 0)
2563 gfc_error ("Second argument of NEAREST at %L shall not be zero", &s->where);
2564 return &gfc_bad_expr;
2567 gfc_set_model_kind (x->ts.kind);
2568 result = gfc_copy_expr (x);
2570 sgn = mpfr_sgn (s->value.real);
2572 mpfr_set_inf (tmp, sgn);
2573 mpfr_nexttoward (result->value.real, tmp);
2576 return range_check (result, "NEAREST");
2581 simplify_nint (const char *name, gfc_expr * e, gfc_expr * k)
2583 gfc_expr *itrunc, *result;
2586 kind = get_kind (BT_INTEGER, k, name, gfc_default_integer_kind);
2588 return &gfc_bad_expr;
2590 if (e->expr_type != EXPR_CONSTANT)
2593 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
2595 itrunc = gfc_copy_expr (e);
2597 mpfr_round(itrunc->value.real, e->value.real);
2599 gfc_mpfr_to_mpz (result->value.integer, itrunc->value.real);
2601 gfc_free_expr (itrunc);
2603 return range_check (result, name);
2608 gfc_simplify_new_line (gfc_expr * e)
2612 if (e->expr_type != EXPR_CONSTANT)
2615 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
2617 result->value.character.string = gfc_getmem (2);
2619 result->value.character.length = 1;
2620 result->value.character.string[0] = '\n';
2621 result->value.character.string[1] = '\0'; /* For debugger */
2627 gfc_simplify_nint (gfc_expr * e, gfc_expr * k)
2629 return simplify_nint ("NINT", e, k);
2634 gfc_simplify_idnint (gfc_expr * e)
2636 return simplify_nint ("IDNINT", e, NULL);
2641 gfc_simplify_not (gfc_expr * e)
2645 if (e->expr_type != EXPR_CONSTANT)
2648 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
2650 mpz_com (result->value.integer, e->value.integer);
2652 return range_check (result, "NOT");
2657 gfc_simplify_null (gfc_expr * mold)
2663 result = gfc_get_expr ();
2664 result->ts.type = BT_UNKNOWN;
2667 result = gfc_copy_expr (mold);
2668 result->expr_type = EXPR_NULL;
2675 gfc_simplify_or (gfc_expr * x, gfc_expr * y)
2680 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2683 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
2684 if (x->ts.type == BT_INTEGER)
2686 result = gfc_constant_result (BT_INTEGER, kind, &x->where);
2687 mpz_ior (result->value.integer, x->value.integer, y->value.integer);
2689 else /* BT_LOGICAL */
2691 result = gfc_constant_result (BT_LOGICAL, kind, &x->where);
2692 result->value.logical = x->value.logical || y->value.logical;
2695 return range_check (result, "OR");
2700 gfc_simplify_precision (gfc_expr * e)
2705 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2707 result = gfc_int_expr (gfc_real_kinds[i].precision);
2708 result->where = e->where;
2715 gfc_simplify_radix (gfc_expr * e)
2720 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2724 i = gfc_integer_kinds[i].radix;
2728 i = gfc_real_kinds[i].radix;
2735 result = gfc_int_expr (i);
2736 result->where = e->where;
2743 gfc_simplify_range (gfc_expr * e)
2749 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2754 j = gfc_integer_kinds[i].range;
2759 j = gfc_real_kinds[i].range;
2766 result = gfc_int_expr (j);
2767 result->where = e->where;
2774 gfc_simplify_real (gfc_expr * e, gfc_expr * k)
2779 if (e->ts.type == BT_COMPLEX)
2780 kind = get_kind (BT_REAL, k, "REAL", e->ts.kind);
2782 kind = get_kind (BT_REAL, k, "REAL", gfc_default_real_kind);
2785 return &gfc_bad_expr;
2787 if (e->expr_type != EXPR_CONSTANT)
2793 result = gfc_int2real (e, kind);
2797 result = gfc_real2real (e, kind);
2801 result = gfc_complex2real (e, kind);
2805 gfc_internal_error ("bad type in REAL");
2809 return range_check (result, "REAL");
2814 gfc_simplify_realpart (gfc_expr * e)
2818 if (e->expr_type != EXPR_CONSTANT)
2821 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
2822 mpfr_set (result->value.real, e->value.complex.r, GFC_RND_MODE);
2824 return range_check (result, "REALPART");
2828 gfc_simplify_repeat (gfc_expr * e, gfc_expr * n)
2831 int i, j, len, ncopies, nlen;
2833 if (e->expr_type != EXPR_CONSTANT || n->expr_type != EXPR_CONSTANT)
2836 if (n != NULL && (gfc_extract_int (n, &ncopies) != NULL || ncopies < 0))
2838 gfc_error ("Invalid second argument of REPEAT at %L", &n->where);
2839 return &gfc_bad_expr;
2842 len = e->value.character.length;
2843 nlen = ncopies * len;
2845 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
2849 result->value.character.string = gfc_getmem (1);
2850 result->value.character.length = 0;
2851 result->value.character.string[0] = '\0';
2855 result->value.character.length = nlen;
2856 result->value.character.string = gfc_getmem (nlen + 1);
2858 for (i = 0; i < ncopies; i++)
2859 for (j = 0; j < len; j++)
2860 result->value.character.string[j + i * len] =
2861 e->value.character.string[j];
2863 result->value.character.string[nlen] = '\0'; /* For debugger */
2868 /* This one is a bear, but mainly has to do with shuffling elements. */
2871 gfc_simplify_reshape (gfc_expr * source, gfc_expr * shape_exp,
2872 gfc_expr * pad, gfc_expr * order_exp)
2875 int order[GFC_MAX_DIMENSIONS], shape[GFC_MAX_DIMENSIONS];
2876 int i, rank, npad, x[GFC_MAX_DIMENSIONS];
2877 gfc_constructor *head, *tail;
2883 /* Unpack the shape array. */
2884 if (source->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (source))
2887 if (shape_exp->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (shape_exp))
2891 && (pad->expr_type != EXPR_ARRAY
2892 || !gfc_is_constant_expr (pad)))
2895 if (order_exp != NULL
2896 && (order_exp->expr_type != EXPR_ARRAY
2897 || !gfc_is_constant_expr (order_exp)))
2906 e = gfc_get_array_element (shape_exp, rank);
2910 if (gfc_extract_int (e, &shape[rank]) != NULL)
2912 gfc_error ("Integer too large in shape specification at %L",
2920 if (rank >= GFC_MAX_DIMENSIONS)
2922 gfc_error ("Too many dimensions in shape specification for RESHAPE "
2923 "at %L", &e->where);
2928 if (shape[rank] < 0)
2930 gfc_error ("Shape specification at %L cannot be negative",
2940 gfc_error ("Shape specification at %L cannot be the null array",
2945 /* Now unpack the order array if present. */
2946 if (order_exp == NULL)
2948 for (i = 0; i < rank; i++)
2955 for (i = 0; i < rank; i++)
2958 for (i = 0; i < rank; i++)
2960 e = gfc_get_array_element (order_exp, i);
2964 ("ORDER parameter of RESHAPE at %L is not the same size "
2965 "as SHAPE parameter", &order_exp->where);
2969 if (gfc_extract_int (e, &order[i]) != NULL)
2971 gfc_error ("Error in ORDER parameter of RESHAPE at %L",
2979 if (order[i] < 1 || order[i] > rank)
2981 gfc_error ("ORDER parameter of RESHAPE at %L is out of range",
2990 gfc_error ("Invalid permutation in ORDER parameter at %L",
2999 /* Count the elements in the source and padding arrays. */
3004 gfc_array_size (pad, &size);
3005 npad = mpz_get_ui (size);
3009 gfc_array_size (source, &size);
3010 nsource = mpz_get_ui (size);
3013 /* If it weren't for that pesky permutation we could just loop
3014 through the source and round out any shortage with pad elements.
3015 But no, someone just had to have the compiler do something the
3016 user should be doing. */
3018 for (i = 0; i < rank; i++)
3023 /* Figure out which element to extract. */
3024 mpz_set_ui (index, 0);
3026 for (i = rank - 1; i >= 0; i--)
3028 mpz_add_ui (index, index, x[order[i]]);
3030 mpz_mul_ui (index, index, shape[order[i - 1]]);
3033 if (mpz_cmp_ui (index, INT_MAX) > 0)
3034 gfc_internal_error ("Reshaped array too large at %L", &e->where);
3036 j = mpz_get_ui (index);
3039 e = gfc_get_array_element (source, j);
3047 ("PAD parameter required for short SOURCE parameter at %L",
3053 e = gfc_get_array_element (pad, j);
3057 head = tail = gfc_get_constructor ();
3060 tail->next = gfc_get_constructor ();
3067 tail->where = e->where;
3070 /* Calculate the next element. */
3074 if (++x[i] < shape[i])
3085 e = gfc_get_expr ();
3086 e->where = source->where;
3087 e->expr_type = EXPR_ARRAY;
3088 e->value.constructor = head;
3089 e->shape = gfc_get_shape (rank);
3091 for (i = 0; i < rank; i++)
3092 mpz_init_set_ui (e->shape[i], shape[i]);
3100 gfc_free_constructor (head);
3102 return &gfc_bad_expr;
3107 gfc_simplify_rrspacing (gfc_expr * x)
3113 if (x->expr_type != EXPR_CONSTANT)
3116 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
3118 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
3120 mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE);
3122 /* Special case x = 0 and 0. */
3123 if (mpfr_sgn (result->value.real) == 0)
3125 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
3129 /* | x * 2**(-e) | * 2**p. */
3130 e = - (long int) mpfr_get_exp (x->value.real);
3131 mpfr_mul_2si (result->value.real, result->value.real, e, GFC_RND_MODE);
3133 p = (long int) gfc_real_kinds[i].digits;
3134 mpfr_mul_2si (result->value.real, result->value.real, p, GFC_RND_MODE);
3136 return range_check (result, "RRSPACING");
3141 gfc_simplify_scale (gfc_expr * x, gfc_expr * i)
3143 int k, neg_flag, power, exp_range;
3144 mpfr_t scale, radix;
3147 if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
3150 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
3152 if (mpfr_sgn (x->value.real) == 0)
3154 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
3158 k = gfc_validate_kind (BT_REAL, x->ts.kind, false);
3160 exp_range = gfc_real_kinds[k].max_exponent - gfc_real_kinds[k].min_exponent;
3162 /* This check filters out values of i that would overflow an int. */
3163 if (mpz_cmp_si (i->value.integer, exp_range + 2) > 0
3164 || mpz_cmp_si (i->value.integer, -exp_range - 2) < 0)
3166 gfc_error ("Result of SCALE overflows its kind at %L", &result->where);
3167 return &gfc_bad_expr;
3170 /* Compute scale = radix ** power. */
3171 power = mpz_get_si (i->value.integer);
3181 gfc_set_model_kind (x->ts.kind);
3184 mpfr_set_ui (radix, gfc_real_kinds[k].radix, GFC_RND_MODE);
3185 mpfr_pow_ui (scale, radix, power, GFC_RND_MODE);
3188 mpfr_div (result->value.real, x->value.real, scale, GFC_RND_MODE);
3190 mpfr_mul (result->value.real, x->value.real, scale, GFC_RND_MODE);
3195 return range_check (result, "SCALE");
3200 gfc_simplify_scan (gfc_expr * e, gfc_expr * c, gfc_expr * b)
3205 size_t indx, len, lenc;
3207 if (e->expr_type != EXPR_CONSTANT || c->expr_type != EXPR_CONSTANT)
3210 if (b != NULL && b->value.logical != 0)
3215 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
3218 len = e->value.character.length;
3219 lenc = c->value.character.length;
3221 if (len == 0 || lenc == 0)
3230 strcspn (e->value.character.string, c->value.character.string) + 1;
3237 for (indx = len; indx > 0; indx--)
3239 for (i = 0; i < lenc; i++)
3241 if (c->value.character.string[i]
3242 == e->value.character.string[indx - 1])
3250 mpz_set_ui (result->value.integer, indx);
3251 return range_check (result, "SCAN");
3256 gfc_simplify_selected_int_kind (gfc_expr * e)
3261 if (e->expr_type != EXPR_CONSTANT || gfc_extract_int (e, &range) != NULL)
3266 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
3267 if (gfc_integer_kinds[i].range >= range
3268 && gfc_integer_kinds[i].kind < kind)
3269 kind = gfc_integer_kinds[i].kind;
3271 if (kind == INT_MAX)
3274 result = gfc_int_expr (kind);
3275 result->where = e->where;
3282 gfc_simplify_selected_real_kind (gfc_expr * p, gfc_expr * q)
3284 int range, precision, i, kind, found_precision, found_range;
3291 if (p->expr_type != EXPR_CONSTANT
3292 || gfc_extract_int (p, &precision) != NULL)
3300 if (q->expr_type != EXPR_CONSTANT
3301 || gfc_extract_int (q, &range) != NULL)
3306 found_precision = 0;
3309 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
3311 if (gfc_real_kinds[i].precision >= precision)
3312 found_precision = 1;
3314 if (gfc_real_kinds[i].range >= range)
3317 if (gfc_real_kinds[i].precision >= precision
3318 && gfc_real_kinds[i].range >= range && gfc_real_kinds[i].kind < kind)
3319 kind = gfc_real_kinds[i].kind;
3322 if (kind == INT_MAX)
3326 if (!found_precision)
3332 result = gfc_int_expr (kind);
3333 result->where = (p != NULL) ? p->where : q->where;
3340 gfc_simplify_set_exponent (gfc_expr * x, gfc_expr * i)
3343 mpfr_t exp, absv, log2, pow2, frac;
3346 if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
3349 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
3351 gfc_set_model_kind (x->ts.kind);
3353 if (mpfr_sgn (x->value.real) == 0)
3355 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
3365 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
3366 mpfr_log2 (log2, absv, GFC_RND_MODE);
3368 mpfr_trunc (log2, log2);
3369 mpfr_add_ui (exp, log2, 1, GFC_RND_MODE);
3371 /* Old exponent value, and fraction. */
3372 mpfr_ui_pow (pow2, 2, exp, GFC_RND_MODE);
3374 mpfr_div (frac, absv, pow2, GFC_RND_MODE);
3377 exp2 = (unsigned long) mpz_get_d (i->value.integer);
3378 mpfr_mul_2exp (result->value.real, frac, exp2, GFC_RND_MODE);
3385 return range_check (result, "SET_EXPONENT");
3390 gfc_simplify_shape (gfc_expr * source)
3392 mpz_t shape[GFC_MAX_DIMENSIONS];
3393 gfc_expr *result, *e, *f;
3398 if (source->rank == 0 || source->expr_type != EXPR_VARIABLE)
3401 result = gfc_start_constructor (BT_INTEGER, gfc_default_integer_kind,
3404 ar = gfc_find_array_ref (source);
3406 t = gfc_array_ref_shape (ar, shape);
3408 for (n = 0; n < source->rank; n++)
3410 e = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
3415 mpz_set (e->value.integer, shape[n]);
3416 mpz_clear (shape[n]);
3420 mpz_set_ui (e->value.integer, n + 1);
3422 f = gfc_simplify_size (source, e);
3426 gfc_free_expr (result);
3435 gfc_append_constructor (result, e);
3443 gfc_simplify_size (gfc_expr * array, gfc_expr * dim)
3451 if (gfc_array_size (array, &size) == FAILURE)
3456 if (dim->expr_type != EXPR_CONSTANT)
3459 d = mpz_get_ui (dim->value.integer) - 1;
3460 if (gfc_array_dimen_size (array, d, &size) == FAILURE)
3464 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
3467 mpz_set (result->value.integer, size);
3474 gfc_simplify_sign (gfc_expr * x, gfc_expr * y)
3478 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
3481 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3486 mpz_abs (result->value.integer, x->value.integer);
3487 if (mpz_sgn (y->value.integer) < 0)
3488 mpz_neg (result->value.integer, result->value.integer);
3493 /* TODO: Handle -0.0 and +0.0 correctly on machines that support
3495 mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE);
3496 if (mpfr_sgn (y->value.real) < 0)
3497 mpfr_neg (result->value.real, result->value.real, GFC_RND_MODE);
3502 gfc_internal_error ("Bad type in gfc_simplify_sign");
3510 gfc_simplify_sin (gfc_expr * x)
3515 if (x->expr_type != EXPR_CONSTANT)
3518 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3523 mpfr_sin (result->value.real, x->value.real, GFC_RND_MODE);
3527 gfc_set_model (x->value.real);
3531 mpfr_sin (xp, x->value.complex.r, GFC_RND_MODE);
3532 mpfr_cosh (xq, x->value.complex.i, GFC_RND_MODE);
3533 mpfr_mul (result->value.complex.r, xp, xq, GFC_RND_MODE);
3535 mpfr_cos (xp, x->value.complex.r, GFC_RND_MODE);
3536 mpfr_sinh (xq, x->value.complex.i, GFC_RND_MODE);
3537 mpfr_mul (result->value.complex.i, xp, xq, GFC_RND_MODE);
3544 gfc_internal_error ("in gfc_simplify_sin(): Bad type");
3547 return range_check (result, "SIN");
3552 gfc_simplify_sinh (gfc_expr * x)
3556 if (x->expr_type != EXPR_CONSTANT)
3559 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3561 mpfr_sinh(result->value.real, x->value.real, GFC_RND_MODE);
3563 return range_check (result, "SINH");
3567 /* The argument is always a double precision real that is converted to
3568 single precision. TODO: Rounding! */
3571 gfc_simplify_sngl (gfc_expr * a)
3575 if (a->expr_type != EXPR_CONSTANT)
3578 result = gfc_real2real (a, gfc_default_real_kind);
3579 return range_check (result, "SNGL");
3584 gfc_simplify_spacing (gfc_expr * x)
3590 if (x->expr_type != EXPR_CONSTANT)
3593 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
3595 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
3597 /* Special case x = 0 and -0. */
3598 mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE);
3599 if (mpfr_sgn (result->value.real) == 0)
3601 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
3605 /* In the Fortran 95 standard, the result is b**(e - p) where b, e, and p
3606 are the radix, exponent of x, and precision. This excludes the
3607 possibility of subnormal numbers. Fortran 2003 states the result is
3608 b**max(e - p, emin - 1). */
3610 ep = (long int) mpfr_get_exp (x->value.real) - gfc_real_kinds[i].digits;
3611 en = (long int) gfc_real_kinds[i].min_exponent - 1;
3612 en = en > ep ? en : ep;
3614 mpfr_set_ui (result->value.real, 1, GFC_RND_MODE);
3615 mpfr_mul_2si (result->value.real, result->value.real, en, GFC_RND_MODE);
3617 return range_check (result, "SPACING");
3622 gfc_simplify_sqrt (gfc_expr * e)
3625 mpfr_t ac, ad, s, t, w;
3627 if (e->expr_type != EXPR_CONSTANT)
3630 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
3635 if (mpfr_cmp_si (e->value.real, 0) < 0)
3637 mpfr_sqrt (result->value.real, e->value.real, GFC_RND_MODE);
3642 /* Formula taken from Numerical Recipes to avoid over- and
3645 gfc_set_model (e->value.real);
3652 if (mpfr_cmp_ui (e->value.complex.r, 0) == 0
3653 && mpfr_cmp_ui (e->value.complex.i, 0) == 0)
3656 mpfr_set_ui (result->value.complex.r, 0, GFC_RND_MODE);
3657 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
3661 mpfr_abs (ac, e->value.complex.r, GFC_RND_MODE);
3662 mpfr_abs (ad, e->value.complex.i, GFC_RND_MODE);
3664 if (mpfr_cmp (ac, ad) >= 0)
3666 mpfr_div (t, e->value.complex.i, e->value.complex.r, GFC_RND_MODE);
3667 mpfr_mul (t, t, t, GFC_RND_MODE);
3668 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
3669 mpfr_sqrt (t, t, GFC_RND_MODE);
3670 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
3671 mpfr_div_ui (t, t, 2, GFC_RND_MODE);
3672 mpfr_sqrt (t, t, GFC_RND_MODE);
3673 mpfr_sqrt (s, ac, GFC_RND_MODE);
3674 mpfr_mul (w, s, t, GFC_RND_MODE);
3678 mpfr_div (s, e->value.complex.r, e->value.complex.i, GFC_RND_MODE);
3679 mpfr_mul (t, s, s, GFC_RND_MODE);
3680 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
3681 mpfr_sqrt (t, t, GFC_RND_MODE);
3682 mpfr_abs (s, s, GFC_RND_MODE);
3683 mpfr_add (t, t, s, GFC_RND_MODE);
3684 mpfr_div_ui (t, t, 2, GFC_RND_MODE);
3685 mpfr_sqrt (t, t, GFC_RND_MODE);
3686 mpfr_sqrt (s, ad, GFC_RND_MODE);
3687 mpfr_mul (w, s, t, GFC_RND_MODE);
3690 if (mpfr_cmp_ui (w, 0) != 0 && mpfr_cmp_ui (e->value.complex.r, 0) >= 0)
3692 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
3693 mpfr_div (result->value.complex.i, e->value.complex.i, t, GFC_RND_MODE);
3694 mpfr_set (result->value.complex.r, w, GFC_RND_MODE);
3696 else if (mpfr_cmp_ui (w, 0) != 0
3697 && mpfr_cmp_ui (e->value.complex.r, 0) < 0
3698 && mpfr_cmp_ui (e->value.complex.i, 0) >= 0)
3700 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
3701 mpfr_div (result->value.complex.r, e->value.complex.i, t, GFC_RND_MODE);
3702 mpfr_set (result->value.complex.i, w, GFC_RND_MODE);
3704 else if (mpfr_cmp_ui (w, 0) != 0
3705 && mpfr_cmp_ui (e->value.complex.r, 0) < 0
3706 && mpfr_cmp_ui (e->value.complex.i, 0) < 0)
3708 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
3709 mpfr_div (result->value.complex.r, ad, t, GFC_RND_MODE);
3710 mpfr_neg (w, w, GFC_RND_MODE);
3711 mpfr_set (result->value.complex.i, w, GFC_RND_MODE);
3714 gfc_internal_error ("invalid complex argument of SQRT at %L",
3726 gfc_internal_error ("invalid argument of SQRT at %L", &e->where);
3729 return range_check (result, "SQRT");
3732 gfc_free_expr (result);
3733 gfc_error ("Argument of SQRT at %L has a negative value", &e->where);
3734 return &gfc_bad_expr;
3739 gfc_simplify_tan (gfc_expr * x)
3744 if (x->expr_type != EXPR_CONSTANT)
3747 i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
3749 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3751 mpfr_tan (result->value.real, x->value.real, GFC_RND_MODE);
3753 return range_check (result, "TAN");
3758 gfc_simplify_tanh (gfc_expr * x)
3762 if (x->expr_type != EXPR_CONSTANT)
3765 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3767 mpfr_tanh (result->value.real, x->value.real, GFC_RND_MODE);
3769 return range_check (result, "TANH");
3775 gfc_simplify_tiny (gfc_expr * e)
3780 i = gfc_validate_kind (BT_REAL, e->ts.kind, false);
3782 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
3783 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
3790 gfc_simplify_transfer (gfc_expr * source, gfc_expr *mold, gfc_expr * size)
3793 /* Reference mold and size to suppress warning. */
3794 if (gfc_init_expr && (mold || size))
3795 gfc_error ("TRANSFER intrinsic not implemented for initialization at %L",
3803 gfc_simplify_trim (gfc_expr * e)
3806 int count, i, len, lentrim;
3808 if (e->expr_type != EXPR_CONSTANT)
3811 len = e->value.character.length;
3813 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
3815 for (count = 0, i = 1; i <= len; ++i)
3817 if (e->value.character.string[len - i] == ' ')
3823 lentrim = len - count;
3825 result->value.character.length = lentrim;
3826 result->value.character.string = gfc_getmem (lentrim + 1);
3828 for (i = 0; i < lentrim; i++)
3829 result->value.character.string[i] = e->value.character.string[i];
3831 result->value.character.string[lentrim] = '\0'; /* For debugger */
3838 gfc_simplify_ubound (gfc_expr * array, gfc_expr * dim)
3840 return simplify_bound (array, dim, 1);
3845 gfc_simplify_verify (gfc_expr * s, gfc_expr * set, gfc_expr * b)
3849 size_t index, len, lenset;
3852 if (s->expr_type != EXPR_CONSTANT || set->expr_type != EXPR_CONSTANT)
3855 if (b != NULL && b->value.logical != 0)
3860 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
3863 len = s->value.character.length;
3864 lenset = set->value.character.length;
3868 mpz_set_ui (result->value.integer, 0);
3876 mpz_set_ui (result->value.integer, 1);
3881 strspn (s->value.character.string, set->value.character.string) + 1;
3890 mpz_set_ui (result->value.integer, len);
3893 for (index = len; index > 0; index --)
3895 for (i = 0; i < lenset; i++)
3897 if (s->value.character.string[index - 1]
3898 == set->value.character.string[i])
3906 mpz_set_ui (result->value.integer, index);
3912 gfc_simplify_xor (gfc_expr * x, gfc_expr * y)
3917 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
3920 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
3921 if (x->ts.type == BT_INTEGER)
3923 result = gfc_constant_result (BT_INTEGER, kind, &x->where);
3924 mpz_xor (result->value.integer, x->value.integer, y->value.integer);
3926 else /* BT_LOGICAL */
3928 result = gfc_constant_result (BT_LOGICAL, kind, &x->where);
3929 result->value.logical = (x->value.logical && ! y->value.logical)
3930 || (! x->value.logical && y->value.logical);
3933 return range_check (result, "XOR");
3938 /****************** Constant simplification *****************/
3940 /* Master function to convert one constant to another. While this is
3941 used as a simplification function, it requires the destination type
3942 and kind information which is supplied by a special case in
3946 gfc_convert_constant (gfc_expr * e, bt type, int kind)
3948 gfc_expr *g, *result, *(*f) (gfc_expr *, int);
3949 gfc_constructor *head, *c, *tail = NULL;
3963 f = gfc_int2complex;
3983 f = gfc_real2complex;
3994 f = gfc_complex2int;
3997 f = gfc_complex2real;
4000 f = gfc_complex2complex;
4026 f = gfc_hollerith2int;
4030 f = gfc_hollerith2real;
4034 f = gfc_hollerith2complex;
4038 f = gfc_hollerith2character;
4042 f = gfc_hollerith2logical;
4052 gfc_internal_error ("gfc_convert_constant(): Unexpected type");
4057 switch (e->expr_type)
4060 result = f (e, kind);
4062 return &gfc_bad_expr;
4066 if (!gfc_is_constant_expr (e))
4071 for (c = e->value.constructor; c; c = c->next)
4074 head = tail = gfc_get_constructor ();
4077 tail->next = gfc_get_constructor ();
4081 tail->where = c->where;
4083 if (c->iterator == NULL)
4084 tail->expr = f (c->expr, kind);
4087 g = gfc_convert_constant (c->expr, type, kind);
4088 if (g == &gfc_bad_expr)
4093 if (tail->expr == NULL)
4095 gfc_free_constructor (head);
4100 result = gfc_get_expr ();
4101 result->ts.type = type;
4102 result->ts.kind = kind;
4103 result->expr_type = EXPR_ARRAY;
4104 result->value.constructor = head;
4105 result->shape = gfc_copy_shape (e->shape, e->rank);
4106 result->where = e->where;
4107 result->rank = e->rank;
4118 /****************** Helper functions ***********************/
4120 /* Given a collating table, create the inverse table. */
4123 invert_table (const int *table, int *xtable)
4127 for (i = 0; i < 256; i++)
4130 for (i = 0; i < 256; i++)
4131 xtable[table[i]] = i;
4136 gfc_simplify_init_1 (void)
4139 invert_table (ascii_table, xascii_table);