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 /* Checks if X, which is assumed to represent a two's complement
158 integer of binary width BITSIZE, has the signbit set. If so, makes
159 X the corresponding negative number. */
162 twos_complement (mpz_t x, int bitsize)
166 if (mpz_tstbit (x, bitsize - 1) == 1)
168 mpz_init_set_ui(mask, 1);
169 mpz_mul_2exp(mask, mask, bitsize);
170 mpz_sub_ui(mask, mask, 1);
172 /* We negate the number by hand, zeroing the high bits, that is
173 make it the corresponding positive number, and then have it
174 negated by GMP, giving the correct representation of the
177 mpz_add_ui (x, x, 1);
178 mpz_and (x, x, mask);
187 /********************** Simplification functions *****************************/
190 gfc_simplify_abs (gfc_expr * e)
194 if (e->expr_type != EXPR_CONSTANT)
200 result = gfc_constant_result (BT_INTEGER, e->ts.kind, &e->where);
202 mpz_abs (result->value.integer, e->value.integer);
204 result = range_check (result, "IABS");
208 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
210 mpfr_abs (result->value.real, e->value.real, GFC_RND_MODE);
212 result = range_check (result, "ABS");
216 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
218 gfc_set_model_kind (e->ts.kind);
220 mpfr_hypot (result->value.real, e->value.complex.r,
221 e->value.complex.i, GFC_RND_MODE);
222 result = range_check (result, "CABS");
226 gfc_internal_error ("gfc_simplify_abs(): Bad type");
234 gfc_simplify_achar (gfc_expr * e)
239 if (e->expr_type != EXPR_CONSTANT)
242 /* We cannot assume that the native character set is ASCII in this
244 if (gfc_extract_int (e, &index) != NULL || index < 0 || index > 127)
246 gfc_error ("Extended ASCII not implemented: argument of ACHAR at %L "
247 "must be between 0 and 127", &e->where);
248 return &gfc_bad_expr;
251 result = gfc_constant_result (BT_CHARACTER, gfc_default_character_kind,
254 result->value.character.string = gfc_getmem (2);
256 result->value.character.length = 1;
257 result->value.character.string[0] = ascii_table[index];
258 result->value.character.string[1] = '\0'; /* For debugger */
264 gfc_simplify_acos (gfc_expr * x)
268 if (x->expr_type != EXPR_CONSTANT)
271 if (mpfr_cmp_si (x->value.real, 1) > 0 || mpfr_cmp_si (x->value.real, -1) < 0)
273 gfc_error ("Argument of ACOS at %L must be between -1 and 1",
275 return &gfc_bad_expr;
278 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
280 mpfr_acos (result->value.real, x->value.real, GFC_RND_MODE);
282 return range_check (result, "ACOS");
286 gfc_simplify_acosh (gfc_expr * x)
290 if (x->expr_type != EXPR_CONSTANT)
293 if (mpfr_cmp_si (x->value.real, 1) < 0)
295 gfc_error ("Argument of ACOSH at %L must not be less than 1",
297 return &gfc_bad_expr;
300 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
302 mpfr_acosh (result->value.real, x->value.real, GFC_RND_MODE);
304 return range_check (result, "ACOSH");
308 gfc_simplify_adjustl (gfc_expr * e)
314 if (e->expr_type != EXPR_CONSTANT)
317 len = e->value.character.length;
319 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
321 result->value.character.length = len;
322 result->value.character.string = gfc_getmem (len + 1);
324 for (count = 0, i = 0; i < len; ++i)
326 ch = e->value.character.string[i];
332 for (i = 0; i < len - count; ++i)
334 result->value.character.string[i] =
335 e->value.character.string[count + i];
338 for (i = len - count; i < len; ++i)
340 result->value.character.string[i] = ' ';
343 result->value.character.string[len] = '\0'; /* For debugger */
350 gfc_simplify_adjustr (gfc_expr * e)
356 if (e->expr_type != EXPR_CONSTANT)
359 len = e->value.character.length;
361 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
363 result->value.character.length = len;
364 result->value.character.string = gfc_getmem (len + 1);
366 for (count = 0, i = len - 1; i >= 0; --i)
368 ch = e->value.character.string[i];
374 for (i = 0; i < count; ++i)
376 result->value.character.string[i] = ' ';
379 for (i = count; i < len; ++i)
381 result->value.character.string[i] =
382 e->value.character.string[i - count];
385 result->value.character.string[len] = '\0'; /* For debugger */
392 gfc_simplify_aimag (gfc_expr * e)
397 if (e->expr_type != EXPR_CONSTANT)
400 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
401 mpfr_set (result->value.real, e->value.complex.i, GFC_RND_MODE);
403 return range_check (result, "AIMAG");
408 gfc_simplify_aint (gfc_expr * e, gfc_expr * k)
410 gfc_expr *rtrunc, *result;
413 kind = get_kind (BT_REAL, k, "AINT", e->ts.kind);
415 return &gfc_bad_expr;
417 if (e->expr_type != EXPR_CONSTANT)
420 rtrunc = gfc_copy_expr (e);
422 mpfr_trunc (rtrunc->value.real, e->value.real);
424 result = gfc_real2real (rtrunc, kind);
425 gfc_free_expr (rtrunc);
427 return range_check (result, "AINT");
432 gfc_simplify_dint (gfc_expr * e)
434 gfc_expr *rtrunc, *result;
436 if (e->expr_type != EXPR_CONSTANT)
439 rtrunc = gfc_copy_expr (e);
441 mpfr_trunc (rtrunc->value.real, e->value.real);
443 result = gfc_real2real (rtrunc, gfc_default_double_kind);
444 gfc_free_expr (rtrunc);
446 return range_check (result, "DINT");
451 gfc_simplify_anint (gfc_expr * e, gfc_expr * k)
456 kind = get_kind (BT_REAL, k, "ANINT", e->ts.kind);
458 return &gfc_bad_expr;
460 if (e->expr_type != EXPR_CONSTANT)
463 result = gfc_constant_result (e->ts.type, kind, &e->where);
465 mpfr_round (result->value.real, e->value.real);
467 return range_check (result, "ANINT");
472 gfc_simplify_and (gfc_expr * x, gfc_expr * y)
477 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
480 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
481 if (x->ts.type == BT_INTEGER)
483 result = gfc_constant_result (BT_INTEGER, kind, &x->where);
484 mpz_and (result->value.integer, x->value.integer, y->value.integer);
486 else /* BT_LOGICAL */
488 result = gfc_constant_result (BT_LOGICAL, kind, &x->where);
489 result->value.logical = x->value.logical && y->value.logical;
492 return range_check (result, "AND");
497 gfc_simplify_dnint (gfc_expr * e)
501 if (e->expr_type != EXPR_CONSTANT)
504 result = gfc_constant_result (BT_REAL, gfc_default_double_kind, &e->where);
506 mpfr_round (result->value.real, e->value.real);
508 return range_check (result, "DNINT");
513 gfc_simplify_asin (gfc_expr * x)
517 if (x->expr_type != EXPR_CONSTANT)
520 if (mpfr_cmp_si (x->value.real, 1) > 0 || mpfr_cmp_si (x->value.real, -1) < 0)
522 gfc_error ("Argument of ASIN at %L must be between -1 and 1",
524 return &gfc_bad_expr;
527 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
529 mpfr_asin(result->value.real, x->value.real, GFC_RND_MODE);
531 return range_check (result, "ASIN");
536 gfc_simplify_asinh (gfc_expr * x)
540 if (x->expr_type != EXPR_CONSTANT)
543 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
545 mpfr_asinh(result->value.real, x->value.real, GFC_RND_MODE);
547 return range_check (result, "ASINH");
552 gfc_simplify_atan (gfc_expr * x)
556 if (x->expr_type != EXPR_CONSTANT)
559 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
561 mpfr_atan(result->value.real, x->value.real, GFC_RND_MODE);
563 return range_check (result, "ATAN");
568 gfc_simplify_atanh (gfc_expr * x)
572 if (x->expr_type != EXPR_CONSTANT)
575 if (mpfr_cmp_si (x->value.real, 1) >= 0 ||
576 mpfr_cmp_si (x->value.real, -1) <= 0)
578 gfc_error ("Argument of ATANH at %L must be inside the range -1 to 1",
580 return &gfc_bad_expr;
583 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
585 mpfr_atanh(result->value.real, x->value.real, GFC_RND_MODE);
587 return range_check (result, "ATANH");
592 gfc_simplify_atan2 (gfc_expr * y, gfc_expr * x)
596 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
599 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
601 if (mpfr_sgn (y->value.real) == 0 && mpfr_sgn (x->value.real) == 0)
604 ("If first argument of ATAN2 %L is zero, then the second argument "
605 "must not be zero", &x->where);
606 gfc_free_expr (result);
607 return &gfc_bad_expr;
610 mpfr_atan2 (result->value.real, y->value.real, x->value.real, GFC_RND_MODE);
612 return range_check (result, "ATAN2");
617 gfc_simplify_bit_size (gfc_expr * e)
622 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
623 result = gfc_constant_result (BT_INTEGER, e->ts.kind, &e->where);
624 mpz_set_ui (result->value.integer, gfc_integer_kinds[i].bit_size);
631 gfc_simplify_btest (gfc_expr * e, gfc_expr * bit)
635 if (e->expr_type != EXPR_CONSTANT || bit->expr_type != EXPR_CONSTANT)
638 if (gfc_extract_int (bit, &b) != NULL || b < 0)
639 return gfc_logical_expr (0, &e->where);
641 return gfc_logical_expr (mpz_tstbit (e->value.integer, b), &e->where);
646 gfc_simplify_ceiling (gfc_expr * e, gfc_expr * k)
648 gfc_expr *ceil, *result;
651 kind = get_kind (BT_INTEGER, k, "CEILING", gfc_default_integer_kind);
653 return &gfc_bad_expr;
655 if (e->expr_type != EXPR_CONSTANT)
658 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
660 ceil = gfc_copy_expr (e);
662 mpfr_ceil (ceil->value.real, e->value.real);
663 gfc_mpfr_to_mpz(result->value.integer, ceil->value.real);
665 gfc_free_expr (ceil);
667 return range_check (result, "CEILING");
672 gfc_simplify_char (gfc_expr * e, gfc_expr * k)
677 kind = get_kind (BT_CHARACTER, k, "CHAR", gfc_default_character_kind);
679 return &gfc_bad_expr;
681 if (e->expr_type != EXPR_CONSTANT)
684 if (gfc_extract_int (e, &c) != NULL || c < 0 || c > UCHAR_MAX)
686 gfc_error ("Bad character in CHAR function at %L", &e->where);
687 return &gfc_bad_expr;
690 result = gfc_constant_result (BT_CHARACTER, kind, &e->where);
692 result->value.character.length = 1;
693 result->value.character.string = gfc_getmem (2);
695 result->value.character.string[0] = c;
696 result->value.character.string[1] = '\0'; /* For debugger */
702 /* Common subroutine for simplifying CMPLX and DCMPLX. */
705 simplify_cmplx (const char *name, gfc_expr * x, gfc_expr * y, int kind)
709 result = gfc_constant_result (BT_COMPLEX, kind, &x->where);
711 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
716 mpfr_set_z (result->value.complex.r, x->value.integer, GFC_RND_MODE);
720 mpfr_set (result->value.complex.r, x->value.real, GFC_RND_MODE);
724 mpfr_set (result->value.complex.r, x->value.complex.r, GFC_RND_MODE);
725 mpfr_set (result->value.complex.i, x->value.complex.i, GFC_RND_MODE);
729 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (x)");
737 mpfr_set_z (result->value.complex.i, y->value.integer, GFC_RND_MODE);
741 mpfr_set (result->value.complex.i, y->value.real, GFC_RND_MODE);
745 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (y)");
749 return range_check (result, name);
754 gfc_simplify_cmplx (gfc_expr * x, gfc_expr * y, gfc_expr * k)
758 if (x->expr_type != EXPR_CONSTANT
759 || (y != NULL && y->expr_type != EXPR_CONSTANT))
762 kind = get_kind (BT_REAL, k, "CMPLX", gfc_default_real_kind);
764 return &gfc_bad_expr;
766 return simplify_cmplx ("CMPLX", x, y, kind);
771 gfc_simplify_complex (gfc_expr * x, gfc_expr * y)
775 if (x->expr_type != EXPR_CONSTANT
776 || (y != NULL && y->expr_type != EXPR_CONSTANT))
779 if (x->ts.type == BT_INTEGER)
781 if (y->ts.type == BT_INTEGER)
782 kind = gfc_default_real_kind;
788 if (y->ts.type == BT_REAL)
789 kind = (x->ts.kind > y->ts.kind) ? x->ts.kind : y->ts.kind;
794 return simplify_cmplx ("COMPLEX", x, y, kind);
799 gfc_simplify_conjg (gfc_expr * e)
803 if (e->expr_type != EXPR_CONSTANT)
806 result = gfc_copy_expr (e);
807 mpfr_neg (result->value.complex.i, result->value.complex.i, GFC_RND_MODE);
809 return range_check (result, "CONJG");
814 gfc_simplify_cos (gfc_expr * x)
819 if (x->expr_type != EXPR_CONSTANT)
822 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
827 mpfr_cos (result->value.real, x->value.real, GFC_RND_MODE);
830 gfc_set_model_kind (x->ts.kind);
834 mpfr_cos (xp, x->value.complex.r, GFC_RND_MODE);
835 mpfr_cosh (xq, x->value.complex.i, GFC_RND_MODE);
836 mpfr_mul(result->value.complex.r, xp, xq, GFC_RND_MODE);
838 mpfr_sin (xp, x->value.complex.r, GFC_RND_MODE);
839 mpfr_sinh (xq, x->value.complex.i, GFC_RND_MODE);
840 mpfr_mul (xp, xp, xq, GFC_RND_MODE);
841 mpfr_neg (result->value.complex.i, xp, GFC_RND_MODE );
847 gfc_internal_error ("in gfc_simplify_cos(): Bad type");
850 return range_check (result, "COS");
856 gfc_simplify_cosh (gfc_expr * x)
860 if (x->expr_type != EXPR_CONSTANT)
863 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
865 mpfr_cosh (result->value.real, x->value.real, GFC_RND_MODE);
867 return range_check (result, "COSH");
872 gfc_simplify_dcmplx (gfc_expr * x, gfc_expr * y)
875 if (x->expr_type != EXPR_CONSTANT
876 || (y != NULL && y->expr_type != EXPR_CONSTANT))
879 return simplify_cmplx ("DCMPLX", x, y, gfc_default_double_kind);
884 gfc_simplify_dble (gfc_expr * e)
888 if (e->expr_type != EXPR_CONSTANT)
894 result = gfc_int2real (e, gfc_default_double_kind);
898 result = gfc_real2real (e, gfc_default_double_kind);
902 result = gfc_complex2real (e, gfc_default_double_kind);
906 gfc_internal_error ("gfc_simplify_dble(): bad type at %L", &e->where);
909 return range_check (result, "DBLE");
914 gfc_simplify_digits (gfc_expr * x)
918 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
922 digits = gfc_integer_kinds[i].digits;
927 digits = gfc_real_kinds[i].digits;
934 return gfc_int_expr (digits);
939 gfc_simplify_dim (gfc_expr * x, gfc_expr * y)
944 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
947 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
948 result = gfc_constant_result (x->ts.type, kind, &x->where);
953 if (mpz_cmp (x->value.integer, y->value.integer) > 0)
954 mpz_sub (result->value.integer, x->value.integer, y->value.integer);
956 mpz_set_ui (result->value.integer, 0);
961 if (mpfr_cmp (x->value.real, y->value.real) > 0)
962 mpfr_sub (result->value.real, x->value.real, y->value.real, GFC_RND_MODE);
964 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
969 gfc_internal_error ("gfc_simplify_dim(): Bad type");
972 return range_check (result, "DIM");
977 gfc_simplify_dprod (gfc_expr * x, gfc_expr * y)
979 gfc_expr *a1, *a2, *result;
981 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
985 gfc_constant_result (BT_REAL, gfc_default_double_kind, &x->where);
987 a1 = gfc_real2real (x, gfc_default_double_kind);
988 a2 = gfc_real2real (y, gfc_default_double_kind);
990 mpfr_mul (result->value.real, a1->value.real, a2->value.real, GFC_RND_MODE);
995 return range_check (result, "DPROD");
1000 gfc_simplify_epsilon (gfc_expr * e)
1005 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
1007 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
1009 mpfr_set (result->value.real, gfc_real_kinds[i].epsilon, GFC_RND_MODE);
1011 return range_check (result, "EPSILON");
1016 gfc_simplify_exp (gfc_expr * x)
1021 if (x->expr_type != EXPR_CONSTANT)
1024 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1029 mpfr_exp(result->value.real, x->value.real, GFC_RND_MODE);
1033 gfc_set_model_kind (x->ts.kind);
1036 mpfr_exp (xq, x->value.complex.r, GFC_RND_MODE);
1037 mpfr_cos (xp, x->value.complex.i, GFC_RND_MODE);
1038 mpfr_mul (result->value.complex.r, xq, xp, GFC_RND_MODE);
1039 mpfr_sin (xp, x->value.complex.i, GFC_RND_MODE);
1040 mpfr_mul (result->value.complex.i, xq, xp, GFC_RND_MODE);
1046 gfc_internal_error ("in gfc_simplify_exp(): Bad type");
1049 return range_check (result, "EXP");
1052 /* FIXME: MPFR should be able to do this better */
1054 gfc_simplify_exponent (gfc_expr * x)
1059 if (x->expr_type != EXPR_CONSTANT)
1062 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
1065 gfc_set_model (x->value.real);
1067 if (mpfr_sgn (x->value.real) == 0)
1069 mpz_set_ui (result->value.integer, 0);
1073 i = (int) mpfr_get_exp (x->value.real);
1074 mpz_set_si (result->value.integer, i);
1076 return range_check (result, "EXPONENT");
1081 gfc_simplify_float (gfc_expr * a)
1085 if (a->expr_type != EXPR_CONSTANT)
1088 result = gfc_int2real (a, gfc_default_real_kind);
1089 return range_check (result, "FLOAT");
1094 gfc_simplify_floor (gfc_expr * e, gfc_expr * k)
1100 kind = get_kind (BT_INTEGER, k, "FLOOR", gfc_default_integer_kind);
1102 gfc_internal_error ("gfc_simplify_floor(): Bad kind");
1104 if (e->expr_type != EXPR_CONSTANT)
1107 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
1109 gfc_set_model_kind (kind);
1111 mpfr_floor (floor, e->value.real);
1113 gfc_mpfr_to_mpz (result->value.integer, floor);
1117 return range_check (result, "FLOOR");
1122 gfc_simplify_fraction (gfc_expr * x)
1125 mpfr_t absv, exp, pow2;
1127 if (x->expr_type != EXPR_CONSTANT)
1130 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
1132 gfc_set_model_kind (x->ts.kind);
1134 if (mpfr_sgn (x->value.real) == 0)
1136 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
1144 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
1145 mpfr_log2 (exp, absv, GFC_RND_MODE);
1147 mpfr_trunc (exp, exp);
1148 mpfr_add_ui (exp, exp, 1, GFC_RND_MODE);
1150 mpfr_ui_pow (pow2, 2, exp, GFC_RND_MODE);
1152 mpfr_div (result->value.real, absv, pow2, GFC_RND_MODE);
1158 return range_check (result, "FRACTION");
1163 gfc_simplify_huge (gfc_expr * e)
1168 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
1170 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
1175 mpz_set (result->value.integer, gfc_integer_kinds[i].huge);
1179 mpfr_set (result->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
1191 gfc_simplify_iachar (gfc_expr * e)
1196 if (e->expr_type != EXPR_CONSTANT)
1199 if (e->value.character.length != 1)
1201 gfc_error ("Argument of IACHAR at %L must be of length one", &e->where);
1202 return &gfc_bad_expr;
1205 index = xascii_table[(int) e->value.character.string[0] & 0xFF];
1207 result = gfc_int_expr (index);
1208 result->where = e->where;
1210 return range_check (result, "IACHAR");
1215 gfc_simplify_iand (gfc_expr * x, gfc_expr * y)
1219 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1222 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
1224 mpz_and (result->value.integer, x->value.integer, y->value.integer);
1226 return range_check (result, "IAND");
1231 gfc_simplify_ibclr (gfc_expr * x, gfc_expr * y)
1236 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1239 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
1241 gfc_error ("Invalid second argument of IBCLR at %L", &y->where);
1242 return &gfc_bad_expr;
1245 k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
1247 if (pos > gfc_integer_kinds[k].bit_size)
1249 gfc_error ("Second argument of IBCLR exceeds bit size at %L",
1251 return &gfc_bad_expr;
1254 result = gfc_copy_expr (x);
1256 mpz_clrbit (result->value.integer, pos);
1257 return range_check (result, "IBCLR");
1262 gfc_simplify_ibits (gfc_expr * x, gfc_expr * y, gfc_expr * z)
1269 if (x->expr_type != EXPR_CONSTANT
1270 || y->expr_type != EXPR_CONSTANT
1271 || z->expr_type != EXPR_CONSTANT)
1274 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
1276 gfc_error ("Invalid second argument of IBITS at %L", &y->where);
1277 return &gfc_bad_expr;
1280 if (gfc_extract_int (z, &len) != NULL || len < 0)
1282 gfc_error ("Invalid third argument of IBITS at %L", &z->where);
1283 return &gfc_bad_expr;
1286 k = gfc_validate_kind (BT_INTEGER, x->ts.kind, false);
1288 bitsize = gfc_integer_kinds[k].bit_size;
1290 if (pos + len > bitsize)
1293 ("Sum of second and third arguments of IBITS exceeds bit size "
1294 "at %L", &y->where);
1295 return &gfc_bad_expr;
1298 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
1300 bits = gfc_getmem (bitsize * sizeof (int));
1302 for (i = 0; i < bitsize; i++)
1305 for (i = 0; i < len; i++)
1306 bits[i] = mpz_tstbit (x->value.integer, i + pos);
1308 for (i = 0; i < bitsize; i++)
1312 mpz_clrbit (result->value.integer, i);
1314 else if (bits[i] == 1)
1316 mpz_setbit (result->value.integer, i);
1320 gfc_internal_error ("IBITS: Bad bit");
1326 return range_check (result, "IBITS");
1331 gfc_simplify_ibset (gfc_expr * x, gfc_expr * y)
1336 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1339 if (gfc_extract_int (y, &pos) != NULL || pos < 0)
1341 gfc_error ("Invalid second argument of IBSET at %L", &y->where);
1342 return &gfc_bad_expr;
1345 k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
1347 if (pos > gfc_integer_kinds[k].bit_size)
1349 gfc_error ("Second argument of IBSET exceeds bit size at %L",
1351 return &gfc_bad_expr;
1354 result = gfc_copy_expr (x);
1356 mpz_setbit (result->value.integer, pos);
1358 twos_complement (result->value.integer, gfc_integer_kinds[k].bit_size);
1360 return range_check (result, "IBSET");
1365 gfc_simplify_ichar (gfc_expr * e)
1370 if (e->expr_type != EXPR_CONSTANT)
1373 if (e->value.character.length != 1)
1375 gfc_error ("Argument of ICHAR at %L must be of length one", &e->where);
1376 return &gfc_bad_expr;
1379 index = (unsigned char) e->value.character.string[0];
1381 if (index < 0 || index > UCHAR_MAX)
1383 gfc_error ("Argument of ICHAR at %L out of range of this processor",
1385 return &gfc_bad_expr;
1388 result = gfc_int_expr (index);
1389 result->where = e->where;
1390 return range_check (result, "ICHAR");
1395 gfc_simplify_ieor (gfc_expr * x, gfc_expr * y)
1399 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1402 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
1404 mpz_xor (result->value.integer, x->value.integer, y->value.integer);
1406 return range_check (result, "IEOR");
1411 gfc_simplify_index (gfc_expr * x, gfc_expr * y, gfc_expr * b)
1414 int back, len, lensub;
1415 int i, j, k, count, index = 0, start;
1417 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1420 if (b != NULL && b->value.logical != 0)
1425 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
1428 len = x->value.character.length;
1429 lensub = y->value.character.length;
1433 mpz_set_si (result->value.integer, 0);
1442 mpz_set_si (result->value.integer, 1);
1445 else if (lensub == 1)
1447 for (i = 0; i < len; i++)
1449 for (j = 0; j < lensub; j++)
1451 if (y->value.character.string[j] ==
1452 x->value.character.string[i])
1462 for (i = 0; i < len; i++)
1464 for (j = 0; j < lensub; j++)
1466 if (y->value.character.string[j] ==
1467 x->value.character.string[i])
1472 for (k = 0; k < lensub; k++)
1474 if (y->value.character.string[k] ==
1475 x->value.character.string[k + start])
1479 if (count == lensub)
1495 mpz_set_si (result->value.integer, len + 1);
1498 else if (lensub == 1)
1500 for (i = 0; i < len; i++)
1502 for (j = 0; j < lensub; j++)
1504 if (y->value.character.string[j] ==
1505 x->value.character.string[len - i])
1507 index = len - i + 1;
1515 for (i = 0; i < len; i++)
1517 for (j = 0; j < lensub; j++)
1519 if (y->value.character.string[j] ==
1520 x->value.character.string[len - i])
1523 if (start <= len - lensub)
1526 for (k = 0; k < lensub; k++)
1527 if (y->value.character.string[k] ==
1528 x->value.character.string[k + start])
1531 if (count == lensub)
1548 mpz_set_si (result->value.integer, index);
1549 return range_check (result, "INDEX");
1554 gfc_simplify_int (gfc_expr * e, gfc_expr * k)
1556 gfc_expr *rpart, *rtrunc, *result;
1559 kind = get_kind (BT_INTEGER, k, "INT", gfc_default_integer_kind);
1561 return &gfc_bad_expr;
1563 if (e->expr_type != EXPR_CONSTANT)
1566 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
1571 mpz_set (result->value.integer, e->value.integer);
1575 rtrunc = gfc_copy_expr (e);
1576 mpfr_trunc (rtrunc->value.real, e->value.real);
1577 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1578 gfc_free_expr (rtrunc);
1582 rpart = gfc_complex2real (e, kind);
1583 rtrunc = gfc_copy_expr (rpart);
1584 mpfr_trunc (rtrunc->value.real, rpart->value.real);
1585 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1586 gfc_free_expr (rpart);
1587 gfc_free_expr (rtrunc);
1591 gfc_error ("Argument of INT at %L is not a valid type", &e->where);
1592 gfc_free_expr (result);
1593 return &gfc_bad_expr;
1596 return range_check (result, "INT");
1601 gfc_simplify_intconv (gfc_expr * e, int kind, const char *name)
1603 gfc_expr *rpart, *rtrunc, *result;
1605 if (e->expr_type != EXPR_CONSTANT)
1608 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
1613 mpz_set (result->value.integer, e->value.integer);
1617 rtrunc = gfc_copy_expr (e);
1618 mpfr_trunc (rtrunc->value.real, e->value.real);
1619 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1620 gfc_free_expr (rtrunc);
1624 rpart = gfc_complex2real (e, kind);
1625 rtrunc = gfc_copy_expr (rpart);
1626 mpfr_trunc (rtrunc->value.real, rpart->value.real);
1627 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1628 gfc_free_expr (rpart);
1629 gfc_free_expr (rtrunc);
1633 gfc_error ("Argument of %s at %L is not a valid type", name, &e->where);
1634 gfc_free_expr (result);
1635 return &gfc_bad_expr;
1638 return range_check (result, name);
1642 gfc_simplify_int2 (gfc_expr * e)
1644 return gfc_simplify_intconv (e, 2, "INT2");
1648 gfc_simplify_int8 (gfc_expr * e)
1650 return gfc_simplify_intconv (e, 8, "INT8");
1654 gfc_simplify_long (gfc_expr * e)
1656 return gfc_simplify_intconv (e, 4, "LONG");
1661 gfc_simplify_ifix (gfc_expr * e)
1663 gfc_expr *rtrunc, *result;
1665 if (e->expr_type != EXPR_CONSTANT)
1668 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
1671 rtrunc = gfc_copy_expr (e);
1673 mpfr_trunc (rtrunc->value.real, e->value.real);
1674 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1676 gfc_free_expr (rtrunc);
1677 return range_check (result, "IFIX");
1682 gfc_simplify_idint (gfc_expr * e)
1684 gfc_expr *rtrunc, *result;
1686 if (e->expr_type != EXPR_CONSTANT)
1689 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
1692 rtrunc = gfc_copy_expr (e);
1694 mpfr_trunc (rtrunc->value.real, e->value.real);
1695 gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real);
1697 gfc_free_expr (rtrunc);
1698 return range_check (result, "IDINT");
1703 gfc_simplify_ior (gfc_expr * x, gfc_expr * y)
1707 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
1710 result = gfc_constant_result (BT_INTEGER, x->ts.kind, &x->where);
1712 mpz_ior (result->value.integer, x->value.integer, y->value.integer);
1713 return range_check (result, "IOR");
1718 gfc_simplify_ishft (gfc_expr * e, gfc_expr * s)
1721 int shift, ashift, isize, k, *bits, i;
1723 if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
1726 if (gfc_extract_int (s, &shift) != NULL)
1728 gfc_error ("Invalid second argument of ISHFT at %L", &s->where);
1729 return &gfc_bad_expr;
1732 k = gfc_validate_kind (BT_INTEGER, e->ts.kind, false);
1734 isize = gfc_integer_kinds[k].bit_size;
1744 ("Magnitude of second argument of ISHFT exceeds bit size at %L",
1746 return &gfc_bad_expr;
1749 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
1753 mpz_set (result->value.integer, e->value.integer);
1754 return range_check (result, "ISHFT");
1757 bits = gfc_getmem (isize * sizeof (int));
1759 for (i = 0; i < isize; i++)
1760 bits[i] = mpz_tstbit (e->value.integer, i);
1764 for (i = 0; i < shift; i++)
1765 mpz_clrbit (result->value.integer, i);
1767 for (i = 0; i < isize - shift; i++)
1770 mpz_clrbit (result->value.integer, i + shift);
1772 mpz_setbit (result->value.integer, i + shift);
1777 for (i = isize - 1; i >= isize - ashift; i--)
1778 mpz_clrbit (result->value.integer, i);
1780 for (i = isize - 1; i >= ashift; i--)
1783 mpz_clrbit (result->value.integer, i - ashift);
1785 mpz_setbit (result->value.integer, i - ashift);
1789 twos_complement (result->value.integer, isize);
1797 gfc_simplify_ishftc (gfc_expr * e, gfc_expr * s, gfc_expr * sz)
1800 int shift, ashift, isize, delta, k;
1803 if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
1806 if (gfc_extract_int (s, &shift) != NULL)
1808 gfc_error ("Invalid second argument of ISHFTC at %L", &s->where);
1809 return &gfc_bad_expr;
1812 k = gfc_validate_kind (e->ts.type, e->ts.kind, false);
1816 if (gfc_extract_int (sz, &isize) != NULL || isize < 0)
1818 gfc_error ("Invalid third argument of ISHFTC at %L", &sz->where);
1819 return &gfc_bad_expr;
1823 isize = gfc_integer_kinds[k].bit_size;
1833 ("Magnitude of second argument of ISHFTC exceeds third argument "
1834 "at %L", &s->where);
1835 return &gfc_bad_expr;
1838 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
1842 mpz_set (result->value.integer, e->value.integer);
1846 bits = gfc_getmem (isize * sizeof (int));
1848 for (i = 0; i < isize; i++)
1849 bits[i] = mpz_tstbit (e->value.integer, i);
1851 delta = isize - ashift;
1855 for (i = 0; i < delta; i++)
1858 mpz_clrbit (result->value.integer, i + shift);
1860 mpz_setbit (result->value.integer, i + shift);
1863 for (i = delta; i < isize; i++)
1866 mpz_clrbit (result->value.integer, i - delta);
1868 mpz_setbit (result->value.integer, i - delta);
1873 for (i = 0; i < ashift; i++)
1876 mpz_clrbit (result->value.integer, i + delta);
1878 mpz_setbit (result->value.integer, i + delta);
1881 for (i = ashift; i < isize; i++)
1884 mpz_clrbit (result->value.integer, i + shift);
1886 mpz_setbit (result->value.integer, i + shift);
1890 twos_complement (result->value.integer, isize);
1898 gfc_simplify_kind (gfc_expr * e)
1901 if (e->ts.type == BT_DERIVED)
1903 gfc_error ("Argument of KIND at %L is a DERIVED type", &e->where);
1904 return &gfc_bad_expr;
1907 return gfc_int_expr (e->ts.kind);
1912 simplify_bound (gfc_expr * array, gfc_expr * dim, int upper)
1919 if (array->expr_type != EXPR_VARIABLE)
1923 /* TODO: Simplify constant multi-dimensional bounds. */
1926 if (dim->expr_type != EXPR_CONSTANT)
1929 /* Follow any component references. */
1930 as = array->symtree->n.sym->as;
1931 for (ref = array->ref; ref; ref = ref->next)
1936 switch (ref->u.ar.type)
1943 /* We're done because 'as' has already been set in the
1944 previous iteration. */
1955 as = ref->u.c.component->as;
1966 if (as->type == AS_DEFERRED || as->type == AS_ASSUMED_SHAPE)
1969 d = mpz_get_si (dim->value.integer);
1971 if (d < 1 || d > as->rank
1972 || (d == as->rank && as->type == AS_ASSUMED_SIZE && upper))
1974 gfc_error ("DIM argument at %L is out of bounds", &dim->where);
1975 return &gfc_bad_expr;
1978 e = upper ? as->upper[d-1] : as->lower[d-1];
1980 if (e->expr_type != EXPR_CONSTANT)
1983 return gfc_copy_expr (e);
1988 gfc_simplify_lbound (gfc_expr * array, gfc_expr * dim)
1990 return simplify_bound (array, dim, 0);
1995 gfc_simplify_len (gfc_expr * e)
1999 if (e->expr_type == EXPR_CONSTANT)
2001 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
2003 mpz_set_si (result->value.integer, e->value.character.length);
2004 return range_check (result, "LEN");
2007 if (e->ts.cl != NULL && e->ts.cl->length != NULL
2008 && e->ts.cl->length->expr_type == EXPR_CONSTANT)
2010 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
2012 mpz_set (result->value.integer, e->ts.cl->length->value.integer);
2013 return range_check (result, "LEN");
2021 gfc_simplify_len_trim (gfc_expr * e)
2024 int count, len, lentrim, i;
2026 if (e->expr_type != EXPR_CONSTANT)
2029 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
2032 len = e->value.character.length;
2034 for (count = 0, i = 1; i <= len; i++)
2035 if (e->value.character.string[len - i] == ' ')
2040 lentrim = len - count;
2042 mpz_set_si (result->value.integer, lentrim);
2043 return range_check (result, "LEN_TRIM");
2048 gfc_simplify_lge (gfc_expr * a, gfc_expr * b)
2051 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
2054 return gfc_logical_expr (gfc_compare_string (a, b, xascii_table) >= 0,
2060 gfc_simplify_lgt (gfc_expr * a, gfc_expr * b)
2063 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
2066 return gfc_logical_expr (gfc_compare_string (a, b, xascii_table) > 0,
2072 gfc_simplify_lle (gfc_expr * a, gfc_expr * b)
2075 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
2078 return gfc_logical_expr (gfc_compare_string (a, b, xascii_table) <= 0,
2084 gfc_simplify_llt (gfc_expr * a, gfc_expr * b)
2087 if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
2090 return gfc_logical_expr (gfc_compare_string (a, b, xascii_table) < 0,
2096 gfc_simplify_log (gfc_expr * x)
2101 if (x->expr_type != EXPR_CONSTANT)
2104 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
2106 gfc_set_model_kind (x->ts.kind);
2111 if (mpfr_sgn (x->value.real) <= 0)
2114 ("Argument of LOG at %L cannot be less than or equal to zero",
2116 gfc_free_expr (result);
2117 return &gfc_bad_expr;
2120 mpfr_log(result->value.real, x->value.real, GFC_RND_MODE);
2124 if ((mpfr_sgn (x->value.complex.r) == 0)
2125 && (mpfr_sgn (x->value.complex.i) == 0))
2127 gfc_error ("Complex argument of LOG at %L cannot be zero",
2129 gfc_free_expr (result);
2130 return &gfc_bad_expr;
2136 mpfr_atan2 (result->value.complex.i, x->value.complex.i, x->value.complex.r,
2139 mpfr_mul (xr, x->value.complex.r, x->value.complex.r, GFC_RND_MODE);
2140 mpfr_mul (xi, x->value.complex.i, x->value.complex.i, GFC_RND_MODE);
2141 mpfr_add (xr, xr, xi, GFC_RND_MODE);
2142 mpfr_sqrt (xr, xr, GFC_RND_MODE);
2143 mpfr_log (result->value.complex.r, xr, GFC_RND_MODE);
2151 gfc_internal_error ("gfc_simplify_log: bad type");
2154 return range_check (result, "LOG");
2159 gfc_simplify_log10 (gfc_expr * x)
2163 if (x->expr_type != EXPR_CONSTANT)
2166 gfc_set_model_kind (x->ts.kind);
2168 if (mpfr_sgn (x->value.real) <= 0)
2171 ("Argument of LOG10 at %L cannot be less than or equal to zero",
2173 return &gfc_bad_expr;
2176 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
2178 mpfr_log10 (result->value.real, x->value.real, GFC_RND_MODE);
2180 return range_check (result, "LOG10");
2185 gfc_simplify_logical (gfc_expr * e, gfc_expr * k)
2190 kind = get_kind (BT_LOGICAL, k, "LOGICAL", gfc_default_logical_kind);
2192 return &gfc_bad_expr;
2194 if (e->expr_type != EXPR_CONSTANT)
2197 result = gfc_constant_result (BT_LOGICAL, kind, &e->where);
2199 result->value.logical = e->value.logical;
2205 /* This function is special since MAX() can take any number of
2206 arguments. The simplified expression is a rewritten version of the
2207 argument list containing at most one constant element. Other
2208 constant elements are deleted. Because the argument list has
2209 already been checked, this function always succeeds. sign is 1 for
2210 MAX(), -1 for MIN(). */
2213 simplify_min_max (gfc_expr * expr, int sign)
2215 gfc_actual_arglist *arg, *last, *extremum;
2216 gfc_intrinsic_sym * specific;
2220 specific = expr->value.function.isym;
2222 arg = expr->value.function.actual;
2224 for (; arg; last = arg, arg = arg->next)
2226 if (arg->expr->expr_type != EXPR_CONSTANT)
2229 if (extremum == NULL)
2235 switch (arg->expr->ts.type)
2238 if (mpz_cmp (arg->expr->value.integer,
2239 extremum->expr->value.integer) * sign > 0)
2240 mpz_set (extremum->expr->value.integer, arg->expr->value.integer);
2245 if (mpfr_cmp (arg->expr->value.real, extremum->expr->value.real) *
2247 mpfr_set (extremum->expr->value.real, arg->expr->value.real,
2253 gfc_internal_error ("gfc_simplify_max(): Bad type in arglist");
2256 /* Delete the extra constant argument. */
2258 expr->value.function.actual = arg->next;
2260 last->next = arg->next;
2263 gfc_free_actual_arglist (arg);
2267 /* If there is one value left, replace the function call with the
2269 if (expr->value.function.actual->next != NULL)
2272 /* Convert to the correct type and kind. */
2273 if (expr->ts.type != BT_UNKNOWN)
2274 return gfc_convert_constant (expr->value.function.actual->expr,
2275 expr->ts.type, expr->ts.kind);
2277 if (specific->ts.type != BT_UNKNOWN)
2278 return gfc_convert_constant (expr->value.function.actual->expr,
2279 specific->ts.type, specific->ts.kind);
2281 return gfc_copy_expr (expr->value.function.actual->expr);
2286 gfc_simplify_min (gfc_expr * e)
2288 return simplify_min_max (e, -1);
2293 gfc_simplify_max (gfc_expr * e)
2295 return simplify_min_max (e, 1);
2300 gfc_simplify_maxexponent (gfc_expr * x)
2305 i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
2307 result = gfc_int_expr (gfc_real_kinds[i].max_exponent);
2308 result->where = x->where;
2315 gfc_simplify_minexponent (gfc_expr * x)
2320 i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
2322 result = gfc_int_expr (gfc_real_kinds[i].min_exponent);
2323 result->where = x->where;
2330 gfc_simplify_mod (gfc_expr * a, gfc_expr * p)
2333 mpfr_t quot, iquot, term;
2336 if (a->expr_type != EXPR_CONSTANT || p->expr_type != EXPR_CONSTANT)
2339 kind = a->ts.kind > p->ts.kind ? a->ts.kind : p->ts.kind;
2340 result = gfc_constant_result (a->ts.type, kind, &a->where);
2345 if (mpz_cmp_ui (p->value.integer, 0) == 0)
2347 /* Result is processor-dependent. */
2348 gfc_error ("Second argument MOD at %L is zero", &a->where);
2349 gfc_free_expr (result);
2350 return &gfc_bad_expr;
2352 mpz_tdiv_r (result->value.integer, a->value.integer, p->value.integer);
2356 if (mpfr_cmp_ui (p->value.real, 0) == 0)
2358 /* Result is processor-dependent. */
2359 gfc_error ("Second argument of MOD at %L is zero", &p->where);
2360 gfc_free_expr (result);
2361 return &gfc_bad_expr;
2364 gfc_set_model_kind (kind);
2369 mpfr_div (quot, a->value.real, p->value.real, GFC_RND_MODE);
2370 mpfr_trunc (iquot, quot);
2371 mpfr_mul (term, iquot, p->value.real, GFC_RND_MODE);
2372 mpfr_sub (result->value.real, a->value.real, term, GFC_RND_MODE);
2380 gfc_internal_error ("gfc_simplify_mod(): Bad arguments");
2383 return range_check (result, "MOD");
2388 gfc_simplify_modulo (gfc_expr * a, gfc_expr * p)
2391 mpfr_t quot, iquot, term;
2394 if (a->expr_type != EXPR_CONSTANT || p->expr_type != EXPR_CONSTANT)
2397 kind = a->ts.kind > p->ts.kind ? a->ts.kind : p->ts.kind;
2398 result = gfc_constant_result (a->ts.type, kind, &a->where);
2403 if (mpz_cmp_ui (p->value.integer, 0) == 0)
2405 /* Result is processor-dependent. This processor just opts
2406 to not handle it at all. */
2407 gfc_error ("Second argument of MODULO at %L is zero", &a->where);
2408 gfc_free_expr (result);
2409 return &gfc_bad_expr;
2411 mpz_fdiv_r (result->value.integer, a->value.integer, p->value.integer);
2416 if (mpfr_cmp_ui (p->value.real, 0) == 0)
2418 /* Result is processor-dependent. */
2419 gfc_error ("Second argument of MODULO at %L is zero", &p->where);
2420 gfc_free_expr (result);
2421 return &gfc_bad_expr;
2424 gfc_set_model_kind (kind);
2429 mpfr_div (quot, a->value.real, p->value.real, GFC_RND_MODE);
2430 mpfr_floor (iquot, quot);
2431 mpfr_mul (term, iquot, p->value.real, GFC_RND_MODE);
2432 mpfr_sub (result->value.real, a->value.real, term, GFC_RND_MODE);
2440 gfc_internal_error ("gfc_simplify_modulo(): Bad arguments");
2443 return range_check (result, "MODULO");
2447 /* Exists for the sole purpose of consistency with other intrinsics. */
2449 gfc_simplify_mvbits (gfc_expr * f ATTRIBUTE_UNUSED,
2450 gfc_expr * fp ATTRIBUTE_UNUSED,
2451 gfc_expr * l ATTRIBUTE_UNUSED,
2452 gfc_expr * to ATTRIBUTE_UNUSED,
2453 gfc_expr * tp ATTRIBUTE_UNUSED)
2460 gfc_simplify_nearest (gfc_expr * x, gfc_expr * s)
2466 if (x->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
2469 if (mpfr_sgn (s->value.real) == 0)
2471 gfc_error ("Second argument of NEAREST at %L shall not be zero", &s->where);
2472 return &gfc_bad_expr;
2475 gfc_set_model_kind (x->ts.kind);
2476 result = gfc_copy_expr (x);
2478 sgn = mpfr_sgn (s->value.real);
2480 mpfr_set_inf (tmp, sgn);
2481 mpfr_nexttoward (result->value.real, tmp);
2484 return range_check (result, "NEAREST");
2489 simplify_nint (const char *name, gfc_expr * e, gfc_expr * k)
2491 gfc_expr *itrunc, *result;
2494 kind = get_kind (BT_INTEGER, k, name, gfc_default_integer_kind);
2496 return &gfc_bad_expr;
2498 if (e->expr_type != EXPR_CONSTANT)
2501 result = gfc_constant_result (BT_INTEGER, kind, &e->where);
2503 itrunc = gfc_copy_expr (e);
2505 mpfr_round(itrunc->value.real, e->value.real);
2507 gfc_mpfr_to_mpz (result->value.integer, itrunc->value.real);
2509 gfc_free_expr (itrunc);
2511 return range_check (result, name);
2516 gfc_simplify_new_line (gfc_expr * e)
2520 if (e->expr_type != EXPR_CONSTANT)
2523 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
2525 result->value.character.string = gfc_getmem (2);
2527 result->value.character.length = 1;
2528 result->value.character.string[0] = '\n';
2529 result->value.character.string[1] = '\0'; /* For debugger */
2535 gfc_simplify_nint (gfc_expr * e, gfc_expr * k)
2537 return simplify_nint ("NINT", e, k);
2542 gfc_simplify_idnint (gfc_expr * e)
2544 return simplify_nint ("IDNINT", e, NULL);
2549 gfc_simplify_not (gfc_expr * e)
2555 if (e->expr_type != EXPR_CONSTANT)
2558 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
2560 mpz_com (result->value.integer, e->value.integer);
2562 /* Because of how GMP handles numbers, the result must be ANDed with
2563 a mask. For radices <> 2, this will require change. */
2565 i = gfc_validate_kind (BT_INTEGER, e->ts.kind, false);
2568 mpz_add (mask, gfc_integer_kinds[i].huge, gfc_integer_kinds[i].huge);
2569 mpz_add_ui (mask, mask, 1);
2571 mpz_and (result->value.integer, result->value.integer, mask);
2573 twos_complement (result->value.integer, gfc_integer_kinds[i].bit_size);
2577 return range_check (result, "NOT");
2582 gfc_simplify_null (gfc_expr * mold)
2588 result = gfc_get_expr ();
2589 result->ts.type = BT_UNKNOWN;
2592 result = gfc_copy_expr (mold);
2593 result->expr_type = EXPR_NULL;
2600 gfc_simplify_or (gfc_expr * x, gfc_expr * y)
2605 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
2608 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
2609 if (x->ts.type == BT_INTEGER)
2611 result = gfc_constant_result (BT_INTEGER, kind, &x->where);
2612 mpz_ior (result->value.integer, x->value.integer, y->value.integer);
2614 else /* BT_LOGICAL */
2616 result = gfc_constant_result (BT_LOGICAL, kind, &x->where);
2617 result->value.logical = x->value.logical || y->value.logical;
2620 return range_check (result, "OR");
2625 gfc_simplify_precision (gfc_expr * e)
2630 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2632 result = gfc_int_expr (gfc_real_kinds[i].precision);
2633 result->where = e->where;
2640 gfc_simplify_radix (gfc_expr * e)
2645 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2649 i = gfc_integer_kinds[i].radix;
2653 i = gfc_real_kinds[i].radix;
2660 result = gfc_int_expr (i);
2661 result->where = e->where;
2668 gfc_simplify_range (gfc_expr * e)
2674 i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2679 j = gfc_integer_kinds[i].range;
2684 j = gfc_real_kinds[i].range;
2691 result = gfc_int_expr (j);
2692 result->where = e->where;
2699 gfc_simplify_real (gfc_expr * e, gfc_expr * k)
2704 if (e->ts.type == BT_COMPLEX)
2705 kind = get_kind (BT_REAL, k, "REAL", e->ts.kind);
2707 kind = get_kind (BT_REAL, k, "REAL", gfc_default_real_kind);
2710 return &gfc_bad_expr;
2712 if (e->expr_type != EXPR_CONSTANT)
2718 result = gfc_int2real (e, kind);
2722 result = gfc_real2real (e, kind);
2726 result = gfc_complex2real (e, kind);
2730 gfc_internal_error ("bad type in REAL");
2734 return range_check (result, "REAL");
2739 gfc_simplify_realpart (gfc_expr * e)
2743 if (e->expr_type != EXPR_CONSTANT)
2746 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
2747 mpfr_set (result->value.real, e->value.complex.r, GFC_RND_MODE);
2749 return range_check (result, "REALPART");
2753 gfc_simplify_repeat (gfc_expr * e, gfc_expr * n)
2756 int i, j, len, ncopies, nlen;
2758 if (e->expr_type != EXPR_CONSTANT || n->expr_type != EXPR_CONSTANT)
2761 if (n != NULL && (gfc_extract_int (n, &ncopies) != NULL || ncopies < 0))
2763 gfc_error ("Invalid second argument of REPEAT at %L", &n->where);
2764 return &gfc_bad_expr;
2767 len = e->value.character.length;
2768 nlen = ncopies * len;
2770 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
2774 result->value.character.string = gfc_getmem (1);
2775 result->value.character.length = 0;
2776 result->value.character.string[0] = '\0';
2780 result->value.character.length = nlen;
2781 result->value.character.string = gfc_getmem (nlen + 1);
2783 for (i = 0; i < ncopies; i++)
2784 for (j = 0; j < len; j++)
2785 result->value.character.string[j + i * len] =
2786 e->value.character.string[j];
2788 result->value.character.string[nlen] = '\0'; /* For debugger */
2793 /* This one is a bear, but mainly has to do with shuffling elements. */
2796 gfc_simplify_reshape (gfc_expr * source, gfc_expr * shape_exp,
2797 gfc_expr * pad, gfc_expr * order_exp)
2800 int order[GFC_MAX_DIMENSIONS], shape[GFC_MAX_DIMENSIONS];
2801 int i, rank, npad, x[GFC_MAX_DIMENSIONS];
2802 gfc_constructor *head, *tail;
2808 /* Unpack the shape array. */
2809 if (source->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (source))
2812 if (shape_exp->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (shape_exp))
2816 && (pad->expr_type != EXPR_ARRAY
2817 || !gfc_is_constant_expr (pad)))
2820 if (order_exp != NULL
2821 && (order_exp->expr_type != EXPR_ARRAY
2822 || !gfc_is_constant_expr (order_exp)))
2831 e = gfc_get_array_element (shape_exp, rank);
2835 if (gfc_extract_int (e, &shape[rank]) != NULL)
2837 gfc_error ("Integer too large in shape specification at %L",
2845 if (rank >= GFC_MAX_DIMENSIONS)
2847 gfc_error ("Too many dimensions in shape specification for RESHAPE "
2848 "at %L", &e->where);
2853 if (shape[rank] < 0)
2855 gfc_error ("Shape specification at %L cannot be negative",
2865 gfc_error ("Shape specification at %L cannot be the null array",
2870 /* Now unpack the order array if present. */
2871 if (order_exp == NULL)
2873 for (i = 0; i < rank; i++)
2880 for (i = 0; i < rank; i++)
2883 for (i = 0; i < rank; i++)
2885 e = gfc_get_array_element (order_exp, i);
2889 ("ORDER parameter of RESHAPE at %L is not the same size "
2890 "as SHAPE parameter", &order_exp->where);
2894 if (gfc_extract_int (e, &order[i]) != NULL)
2896 gfc_error ("Error in ORDER parameter of RESHAPE at %L",
2904 if (order[i] < 1 || order[i] > rank)
2906 gfc_error ("ORDER parameter of RESHAPE at %L is out of range",
2915 gfc_error ("Invalid permutation in ORDER parameter at %L",
2924 /* Count the elements in the source and padding arrays. */
2929 gfc_array_size (pad, &size);
2930 npad = mpz_get_ui (size);
2934 gfc_array_size (source, &size);
2935 nsource = mpz_get_ui (size);
2938 /* If it weren't for that pesky permutation we could just loop
2939 through the source and round out any shortage with pad elements.
2940 But no, someone just had to have the compiler do something the
2941 user should be doing. */
2943 for (i = 0; i < rank; i++)
2948 /* Figure out which element to extract. */
2949 mpz_set_ui (index, 0);
2951 for (i = rank - 1; i >= 0; i--)
2953 mpz_add_ui (index, index, x[order[i]]);
2955 mpz_mul_ui (index, index, shape[order[i - 1]]);
2958 if (mpz_cmp_ui (index, INT_MAX) > 0)
2959 gfc_internal_error ("Reshaped array too large at %L", &e->where);
2961 j = mpz_get_ui (index);
2964 e = gfc_get_array_element (source, j);
2972 ("PAD parameter required for short SOURCE parameter at %L",
2978 e = gfc_get_array_element (pad, j);
2982 head = tail = gfc_get_constructor ();
2985 tail->next = gfc_get_constructor ();
2992 tail->where = e->where;
2995 /* Calculate the next element. */
2999 if (++x[i] < shape[i])
3010 e = gfc_get_expr ();
3011 e->where = source->where;
3012 e->expr_type = EXPR_ARRAY;
3013 e->value.constructor = head;
3014 e->shape = gfc_get_shape (rank);
3016 for (i = 0; i < rank; i++)
3017 mpz_init_set_ui (e->shape[i], shape[i]);
3025 gfc_free_constructor (head);
3027 return &gfc_bad_expr;
3032 gfc_simplify_rrspacing (gfc_expr * x)
3038 if (x->expr_type != EXPR_CONSTANT)
3041 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
3043 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
3045 mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE);
3047 /* Special case x = 0 and 0. */
3048 if (mpfr_sgn (result->value.real) == 0)
3050 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
3054 /* | x * 2**(-e) | * 2**p. */
3055 e = - (long int) mpfr_get_exp (x->value.real);
3056 mpfr_mul_2si (result->value.real, result->value.real, e, GFC_RND_MODE);
3058 p = (long int) gfc_real_kinds[i].digits;
3059 mpfr_mul_2si (result->value.real, result->value.real, p, GFC_RND_MODE);
3061 return range_check (result, "RRSPACING");
3066 gfc_simplify_scale (gfc_expr * x, gfc_expr * i)
3068 int k, neg_flag, power, exp_range;
3069 mpfr_t scale, radix;
3072 if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
3075 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
3077 if (mpfr_sgn (x->value.real) == 0)
3079 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
3083 k = gfc_validate_kind (BT_REAL, x->ts.kind, false);
3085 exp_range = gfc_real_kinds[k].max_exponent - gfc_real_kinds[k].min_exponent;
3087 /* This check filters out values of i that would overflow an int. */
3088 if (mpz_cmp_si (i->value.integer, exp_range + 2) > 0
3089 || mpz_cmp_si (i->value.integer, -exp_range - 2) < 0)
3091 gfc_error ("Result of SCALE overflows its kind at %L", &result->where);
3092 return &gfc_bad_expr;
3095 /* Compute scale = radix ** power. */
3096 power = mpz_get_si (i->value.integer);
3106 gfc_set_model_kind (x->ts.kind);
3109 mpfr_set_ui (radix, gfc_real_kinds[k].radix, GFC_RND_MODE);
3110 mpfr_pow_ui (scale, radix, power, GFC_RND_MODE);
3113 mpfr_div (result->value.real, x->value.real, scale, GFC_RND_MODE);
3115 mpfr_mul (result->value.real, x->value.real, scale, GFC_RND_MODE);
3120 return range_check (result, "SCALE");
3125 gfc_simplify_scan (gfc_expr * e, gfc_expr * c, gfc_expr * b)
3130 size_t indx, len, lenc;
3132 if (e->expr_type != EXPR_CONSTANT || c->expr_type != EXPR_CONSTANT)
3135 if (b != NULL && b->value.logical != 0)
3140 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
3143 len = e->value.character.length;
3144 lenc = c->value.character.length;
3146 if (len == 0 || lenc == 0)
3155 strcspn (e->value.character.string, c->value.character.string) + 1;
3162 for (indx = len; indx > 0; indx--)
3164 for (i = 0; i < lenc; i++)
3166 if (c->value.character.string[i]
3167 == e->value.character.string[indx - 1])
3175 mpz_set_ui (result->value.integer, indx);
3176 return range_check (result, "SCAN");
3181 gfc_simplify_selected_int_kind (gfc_expr * e)
3186 if (e->expr_type != EXPR_CONSTANT || gfc_extract_int (e, &range) != NULL)
3191 for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
3192 if (gfc_integer_kinds[i].range >= range
3193 && gfc_integer_kinds[i].kind < kind)
3194 kind = gfc_integer_kinds[i].kind;
3196 if (kind == INT_MAX)
3199 result = gfc_int_expr (kind);
3200 result->where = e->where;
3207 gfc_simplify_selected_real_kind (gfc_expr * p, gfc_expr * q)
3209 int range, precision, i, kind, found_precision, found_range;
3216 if (p->expr_type != EXPR_CONSTANT
3217 || gfc_extract_int (p, &precision) != NULL)
3225 if (q->expr_type != EXPR_CONSTANT
3226 || gfc_extract_int (q, &range) != NULL)
3231 found_precision = 0;
3234 for (i = 0; gfc_real_kinds[i].kind != 0; i++)
3236 if (gfc_real_kinds[i].precision >= precision)
3237 found_precision = 1;
3239 if (gfc_real_kinds[i].range >= range)
3242 if (gfc_real_kinds[i].precision >= precision
3243 && gfc_real_kinds[i].range >= range && gfc_real_kinds[i].kind < kind)
3244 kind = gfc_real_kinds[i].kind;
3247 if (kind == INT_MAX)
3251 if (!found_precision)
3257 result = gfc_int_expr (kind);
3258 result->where = (p != NULL) ? p->where : q->where;
3265 gfc_simplify_set_exponent (gfc_expr * x, gfc_expr * i)
3268 mpfr_t exp, absv, log2, pow2, frac;
3271 if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
3274 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
3276 gfc_set_model_kind (x->ts.kind);
3278 if (mpfr_sgn (x->value.real) == 0)
3280 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
3290 mpfr_abs (absv, x->value.real, GFC_RND_MODE);
3291 mpfr_log2 (log2, absv, GFC_RND_MODE);
3293 mpfr_trunc (log2, log2);
3294 mpfr_add_ui (exp, log2, 1, GFC_RND_MODE);
3296 /* Old exponent value, and fraction. */
3297 mpfr_ui_pow (pow2, 2, exp, GFC_RND_MODE);
3299 mpfr_div (frac, absv, pow2, GFC_RND_MODE);
3302 exp2 = (unsigned long) mpz_get_d (i->value.integer);
3303 mpfr_mul_2exp (result->value.real, frac, exp2, GFC_RND_MODE);
3310 return range_check (result, "SET_EXPONENT");
3315 gfc_simplify_shape (gfc_expr * source)
3317 mpz_t shape[GFC_MAX_DIMENSIONS];
3318 gfc_expr *result, *e, *f;
3323 if (source->rank == 0 || source->expr_type != EXPR_VARIABLE)
3326 result = gfc_start_constructor (BT_INTEGER, gfc_default_integer_kind,
3329 ar = gfc_find_array_ref (source);
3331 t = gfc_array_ref_shape (ar, shape);
3333 for (n = 0; n < source->rank; n++)
3335 e = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
3340 mpz_set (e->value.integer, shape[n]);
3341 mpz_clear (shape[n]);
3345 mpz_set_ui (e->value.integer, n + 1);
3347 f = gfc_simplify_size (source, e);
3351 gfc_free_expr (result);
3360 gfc_append_constructor (result, e);
3368 gfc_simplify_size (gfc_expr * array, gfc_expr * dim)
3376 if (gfc_array_size (array, &size) == FAILURE)
3381 if (dim->expr_type != EXPR_CONSTANT)
3384 d = mpz_get_ui (dim->value.integer) - 1;
3385 if (gfc_array_dimen_size (array, d, &size) == FAILURE)
3389 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
3392 mpz_set (result->value.integer, size);
3399 gfc_simplify_sign (gfc_expr * x, gfc_expr * y)
3403 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
3406 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3411 mpz_abs (result->value.integer, x->value.integer);
3412 if (mpz_sgn (y->value.integer) < 0)
3413 mpz_neg (result->value.integer, result->value.integer);
3418 /* TODO: Handle -0.0 and +0.0 correctly on machines that support
3420 mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE);
3421 if (mpfr_sgn (y->value.real) < 0)
3422 mpfr_neg (result->value.real, result->value.real, GFC_RND_MODE);
3427 gfc_internal_error ("Bad type in gfc_simplify_sign");
3435 gfc_simplify_sin (gfc_expr * x)
3440 if (x->expr_type != EXPR_CONSTANT)
3443 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3448 mpfr_sin (result->value.real, x->value.real, GFC_RND_MODE);
3452 gfc_set_model (x->value.real);
3456 mpfr_sin (xp, x->value.complex.r, GFC_RND_MODE);
3457 mpfr_cosh (xq, x->value.complex.i, GFC_RND_MODE);
3458 mpfr_mul (result->value.complex.r, xp, xq, GFC_RND_MODE);
3460 mpfr_cos (xp, x->value.complex.r, GFC_RND_MODE);
3461 mpfr_sinh (xq, x->value.complex.i, GFC_RND_MODE);
3462 mpfr_mul (result->value.complex.i, xp, xq, GFC_RND_MODE);
3469 gfc_internal_error ("in gfc_simplify_sin(): Bad type");
3472 return range_check (result, "SIN");
3477 gfc_simplify_sinh (gfc_expr * x)
3481 if (x->expr_type != EXPR_CONSTANT)
3484 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3486 mpfr_sinh(result->value.real, x->value.real, GFC_RND_MODE);
3488 return range_check (result, "SINH");
3492 /* The argument is always a double precision real that is converted to
3493 single precision. TODO: Rounding! */
3496 gfc_simplify_sngl (gfc_expr * a)
3500 if (a->expr_type != EXPR_CONSTANT)
3503 result = gfc_real2real (a, gfc_default_real_kind);
3504 return range_check (result, "SNGL");
3509 gfc_simplify_spacing (gfc_expr * x)
3515 if (x->expr_type != EXPR_CONSTANT)
3518 i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
3520 result = gfc_constant_result (BT_REAL, x->ts.kind, &x->where);
3522 /* Special case x = 0 and -0. */
3523 mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE);
3524 if (mpfr_sgn (result->value.real) == 0)
3526 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
3530 /* In the Fortran 95 standard, the result is b**(e - p) where b, e, and p
3531 are the radix, exponent of x, and precision. This excludes the
3532 possibility of subnormal numbers. Fortran 2003 states the result is
3533 b**max(e - p, emin - 1). */
3535 ep = (long int) mpfr_get_exp (x->value.real) - gfc_real_kinds[i].digits;
3536 en = (long int) gfc_real_kinds[i].min_exponent - 1;
3537 en = en > ep ? en : ep;
3539 mpfr_set_ui (result->value.real, 1, GFC_RND_MODE);
3540 mpfr_mul_2si (result->value.real, result->value.real, en, GFC_RND_MODE);
3542 return range_check (result, "SPACING");
3547 gfc_simplify_sqrt (gfc_expr * e)
3550 mpfr_t ac, ad, s, t, w;
3552 if (e->expr_type != EXPR_CONSTANT)
3555 result = gfc_constant_result (e->ts.type, e->ts.kind, &e->where);
3560 if (mpfr_cmp_si (e->value.real, 0) < 0)
3562 mpfr_sqrt (result->value.real, e->value.real, GFC_RND_MODE);
3567 /* Formula taken from Numerical Recipes to avoid over- and
3570 gfc_set_model (e->value.real);
3577 if (mpfr_cmp_ui (e->value.complex.r, 0) == 0
3578 && mpfr_cmp_ui (e->value.complex.i, 0) == 0)
3581 mpfr_set_ui (result->value.complex.r, 0, GFC_RND_MODE);
3582 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
3586 mpfr_abs (ac, e->value.complex.r, GFC_RND_MODE);
3587 mpfr_abs (ad, e->value.complex.i, GFC_RND_MODE);
3589 if (mpfr_cmp (ac, ad) >= 0)
3591 mpfr_div (t, e->value.complex.i, e->value.complex.r, GFC_RND_MODE);
3592 mpfr_mul (t, t, t, GFC_RND_MODE);
3593 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
3594 mpfr_sqrt (t, t, GFC_RND_MODE);
3595 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
3596 mpfr_div_ui (t, t, 2, GFC_RND_MODE);
3597 mpfr_sqrt (t, t, GFC_RND_MODE);
3598 mpfr_sqrt (s, ac, GFC_RND_MODE);
3599 mpfr_mul (w, s, t, GFC_RND_MODE);
3603 mpfr_div (s, e->value.complex.r, e->value.complex.i, GFC_RND_MODE);
3604 mpfr_mul (t, s, s, GFC_RND_MODE);
3605 mpfr_add_ui (t, t, 1, GFC_RND_MODE);
3606 mpfr_sqrt (t, t, GFC_RND_MODE);
3607 mpfr_abs (s, s, GFC_RND_MODE);
3608 mpfr_add (t, t, s, GFC_RND_MODE);
3609 mpfr_div_ui (t, t, 2, GFC_RND_MODE);
3610 mpfr_sqrt (t, t, GFC_RND_MODE);
3611 mpfr_sqrt (s, ad, GFC_RND_MODE);
3612 mpfr_mul (w, s, t, GFC_RND_MODE);
3615 if (mpfr_cmp_ui (w, 0) != 0 && mpfr_cmp_ui (e->value.complex.r, 0) >= 0)
3617 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
3618 mpfr_div (result->value.complex.i, e->value.complex.i, t, GFC_RND_MODE);
3619 mpfr_set (result->value.complex.r, w, GFC_RND_MODE);
3621 else if (mpfr_cmp_ui (w, 0) != 0
3622 && mpfr_cmp_ui (e->value.complex.r, 0) < 0
3623 && mpfr_cmp_ui (e->value.complex.i, 0) >= 0)
3625 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
3626 mpfr_div (result->value.complex.r, e->value.complex.i, t, GFC_RND_MODE);
3627 mpfr_set (result->value.complex.i, w, GFC_RND_MODE);
3629 else if (mpfr_cmp_ui (w, 0) != 0
3630 && mpfr_cmp_ui (e->value.complex.r, 0) < 0
3631 && mpfr_cmp_ui (e->value.complex.i, 0) < 0)
3633 mpfr_mul_ui (t, w, 2, GFC_RND_MODE);
3634 mpfr_div (result->value.complex.r, ad, t, GFC_RND_MODE);
3635 mpfr_neg (w, w, GFC_RND_MODE);
3636 mpfr_set (result->value.complex.i, w, GFC_RND_MODE);
3639 gfc_internal_error ("invalid complex argument of SQRT at %L",
3651 gfc_internal_error ("invalid argument of SQRT at %L", &e->where);
3654 return range_check (result, "SQRT");
3657 gfc_free_expr (result);
3658 gfc_error ("Argument of SQRT at %L has a negative value", &e->where);
3659 return &gfc_bad_expr;
3664 gfc_simplify_tan (gfc_expr * x)
3669 if (x->expr_type != EXPR_CONSTANT)
3672 i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
3674 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3676 mpfr_tan (result->value.real, x->value.real, GFC_RND_MODE);
3678 return range_check (result, "TAN");
3683 gfc_simplify_tanh (gfc_expr * x)
3687 if (x->expr_type != EXPR_CONSTANT)
3690 result = gfc_constant_result (x->ts.type, x->ts.kind, &x->where);
3692 mpfr_tanh (result->value.real, x->value.real, GFC_RND_MODE);
3694 return range_check (result, "TANH");
3700 gfc_simplify_tiny (gfc_expr * e)
3705 i = gfc_validate_kind (BT_REAL, e->ts.kind, false);
3707 result = gfc_constant_result (BT_REAL, e->ts.kind, &e->where);
3708 mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
3715 gfc_simplify_transfer (gfc_expr * source, gfc_expr *mold, gfc_expr * size)
3718 /* Reference mold and size to suppress warning. */
3719 if (gfc_init_expr && (mold || size))
3720 gfc_error ("TRANSFER intrinsic not implemented for initialization at %L",
3728 gfc_simplify_trim (gfc_expr * e)
3731 int count, i, len, lentrim;
3733 if (e->expr_type != EXPR_CONSTANT)
3736 len = e->value.character.length;
3738 result = gfc_constant_result (BT_CHARACTER, e->ts.kind, &e->where);
3740 for (count = 0, i = 1; i <= len; ++i)
3742 if (e->value.character.string[len - i] == ' ')
3748 lentrim = len - count;
3750 result->value.character.length = lentrim;
3751 result->value.character.string = gfc_getmem (lentrim + 1);
3753 for (i = 0; i < lentrim; i++)
3754 result->value.character.string[i] = e->value.character.string[i];
3756 result->value.character.string[lentrim] = '\0'; /* For debugger */
3763 gfc_simplify_ubound (gfc_expr * array, gfc_expr * dim)
3765 return simplify_bound (array, dim, 1);
3770 gfc_simplify_verify (gfc_expr * s, gfc_expr * set, gfc_expr * b)
3774 size_t index, len, lenset;
3777 if (s->expr_type != EXPR_CONSTANT || set->expr_type != EXPR_CONSTANT)
3780 if (b != NULL && b->value.logical != 0)
3785 result = gfc_constant_result (BT_INTEGER, gfc_default_integer_kind,
3788 len = s->value.character.length;
3789 lenset = set->value.character.length;
3793 mpz_set_ui (result->value.integer, 0);
3801 mpz_set_ui (result->value.integer, 1);
3806 strspn (s->value.character.string, set->value.character.string) + 1;
3815 mpz_set_ui (result->value.integer, len);
3818 for (index = len; index > 0; index --)
3820 for (i = 0; i < lenset; i++)
3822 if (s->value.character.string[index - 1]
3823 == set->value.character.string[i])
3831 mpz_set_ui (result->value.integer, index);
3837 gfc_simplify_xor (gfc_expr * x, gfc_expr * y)
3842 if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
3845 kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
3846 if (x->ts.type == BT_INTEGER)
3848 result = gfc_constant_result (BT_INTEGER, kind, &x->where);
3849 mpz_xor (result->value.integer, x->value.integer, y->value.integer);
3851 else /* BT_LOGICAL */
3853 result = gfc_constant_result (BT_LOGICAL, kind, &x->where);
3854 result->value.logical = (x->value.logical && ! y->value.logical)
3855 || (! x->value.logical && y->value.logical);
3858 return range_check (result, "XOR");
3863 /****************** Constant simplification *****************/
3865 /* Master function to convert one constant to another. While this is
3866 used as a simplification function, it requires the destination type
3867 and kind information which is supplied by a special case in
3871 gfc_convert_constant (gfc_expr * e, bt type, int kind)
3873 gfc_expr *g, *result, *(*f) (gfc_expr *, int);
3874 gfc_constructor *head, *c, *tail = NULL;
3888 f = gfc_int2complex;
3908 f = gfc_real2complex;
3919 f = gfc_complex2int;
3922 f = gfc_complex2real;
3925 f = gfc_complex2complex;
3951 f = gfc_hollerith2int;
3955 f = gfc_hollerith2real;
3959 f = gfc_hollerith2complex;
3963 f = gfc_hollerith2character;
3967 f = gfc_hollerith2logical;
3977 gfc_internal_error ("gfc_convert_constant(): Unexpected type");
3982 switch (e->expr_type)
3985 result = f (e, kind);
3987 return &gfc_bad_expr;
3991 if (!gfc_is_constant_expr (e))
3996 for (c = e->value.constructor; c; c = c->next)
3999 head = tail = gfc_get_constructor ();
4002 tail->next = gfc_get_constructor ();
4006 tail->where = c->where;
4008 if (c->iterator == NULL)
4009 tail->expr = f (c->expr, kind);
4012 g = gfc_convert_constant (c->expr, type, kind);
4013 if (g == &gfc_bad_expr)
4018 if (tail->expr == NULL)
4020 gfc_free_constructor (head);
4025 result = gfc_get_expr ();
4026 result->ts.type = type;
4027 result->ts.kind = kind;
4028 result->expr_type = EXPR_ARRAY;
4029 result->value.constructor = head;
4030 result->shape = gfc_copy_shape (e->shape, e->rank);
4031 result->where = e->where;
4032 result->rank = e->rank;
4043 /****************** Helper functions ***********************/
4045 /* Given a collating table, create the inverse table. */
4048 invert_table (const int *table, int *xtable)
4052 for (i = 0; i < 256; i++)
4055 for (i = 0; i < 256; i++)
4056 xtable[table[i]] = i;
4061 gfc_simplify_init_1 (void)
4064 invert_table (ascii_table, xascii_table);