1 /* Simplify intrinsic functions at compile-time.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation,
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, 59 Temple Place - Suite 330, 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
)
98 if (gfc_range_check (result
) == ARITH_OK
)
101 gfc_error ("Result of %s overflows its kind at %L", name
, &result
->where
);
102 gfc_free_expr (result
);
103 return &gfc_bad_expr
;
107 /* A helper function that gets an optional and possibly missing
108 kind parameter. Returns the kind, -1 if something went wrong. */
111 get_kind (bt type
, gfc_expr
* k
, const char *name
, int default_kind
)
118 if (k
->expr_type
!= EXPR_CONSTANT
)
120 gfc_error ("KIND parameter of %s at %L must be an initialization "
121 "expression", name
, &k
->where
);
126 if (gfc_extract_int (k
, &kind
) != NULL
127 || gfc_validate_kind (type
, kind
, true) < 0)
130 gfc_error ("Invalid KIND parameter of %s at %L", name
, &k
->where
);
138 /* Checks if X, which is assumed to represent a two's complement
139 integer of binary width BITSIZE, has the signbit set. If so, makes
140 X the corresponding negative number. */
143 twos_complement (mpz_t x
, int bitsize
)
147 if (mpz_tstbit (x
, bitsize
- 1) == 1)
149 mpz_init_set_ui(mask
, 1);
150 mpz_mul_2exp(mask
, mask
, bitsize
);
151 mpz_sub_ui(mask
, mask
, 1);
153 /* We negate the number by hand, zeroing the high bits, that is
154 make it the corresponding positive number, and then have it
155 negated by GMP, giving the correct representation of the
158 mpz_add_ui (x
, x
, 1);
159 mpz_and (x
, x
, mask
);
168 /********************** Simplification functions *****************************/
171 gfc_simplify_abs (gfc_expr
* e
)
175 if (e
->expr_type
!= EXPR_CONSTANT
)
181 result
= gfc_constant_result (BT_INTEGER
, e
->ts
.kind
, &e
->where
);
183 mpz_abs (result
->value
.integer
, e
->value
.integer
);
185 result
= range_check (result
, "IABS");
189 result
= gfc_constant_result (BT_REAL
, e
->ts
.kind
, &e
->where
);
191 mpfr_abs (result
->value
.real
, e
->value
.real
, GFC_RND_MODE
);
193 result
= range_check (result
, "ABS");
197 result
= gfc_constant_result (BT_REAL
, e
->ts
.kind
, &e
->where
);
199 gfc_set_model_kind (e
->ts
.kind
);
201 mpfr_hypot (result
->value
.real
, e
->value
.complex.r
,
202 e
->value
.complex.i
, GFC_RND_MODE
);
203 result
= range_check (result
, "CABS");
207 gfc_internal_error ("gfc_simplify_abs(): Bad type");
215 gfc_simplify_achar (gfc_expr
* e
)
220 if (e
->expr_type
!= EXPR_CONSTANT
)
223 /* We cannot assume that the native character set is ASCII in this
225 if (gfc_extract_int (e
, &index
) != NULL
|| index
< 0 || index
> 127)
227 gfc_error ("Extended ASCII not implemented: argument of ACHAR at %L "
228 "must be between 0 and 127", &e
->where
);
229 return &gfc_bad_expr
;
232 result
= gfc_constant_result (BT_CHARACTER
, gfc_default_character_kind
,
235 result
->value
.character
.string
= gfc_getmem (2);
237 result
->value
.character
.length
= 1;
238 result
->value
.character
.string
[0] = ascii_table
[index
];
239 result
->value
.character
.string
[1] = '\0'; /* For debugger */
245 gfc_simplify_acos (gfc_expr
* x
)
249 if (x
->expr_type
!= EXPR_CONSTANT
)
252 if (mpfr_cmp_si (x
->value
.real
, 1) > 0 || mpfr_cmp_si (x
->value
.real
, -1) < 0)
254 gfc_error ("Argument of ACOS at %L must be between -1 and 1",
256 return &gfc_bad_expr
;
259 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
261 mpfr_acos (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
263 return range_check (result
, "ACOS");
268 gfc_simplify_adjustl (gfc_expr
* e
)
274 if (e
->expr_type
!= EXPR_CONSTANT
)
277 len
= e
->value
.character
.length
;
279 result
= gfc_constant_result (BT_CHARACTER
, e
->ts
.kind
, &e
->where
);
281 result
->value
.character
.length
= len
;
282 result
->value
.character
.string
= gfc_getmem (len
+ 1);
284 for (count
= 0, i
= 0; i
< len
; ++i
)
286 ch
= e
->value
.character
.string
[i
];
292 for (i
= 0; i
< len
- count
; ++i
)
294 result
->value
.character
.string
[i
] =
295 e
->value
.character
.string
[count
+ i
];
298 for (i
= len
- count
; i
< len
; ++i
)
300 result
->value
.character
.string
[i
] = ' ';
303 result
->value
.character
.string
[len
] = '\0'; /* For debugger */
310 gfc_simplify_adjustr (gfc_expr
* e
)
316 if (e
->expr_type
!= EXPR_CONSTANT
)
319 len
= e
->value
.character
.length
;
321 result
= gfc_constant_result (BT_CHARACTER
, e
->ts
.kind
, &e
->where
);
323 result
->value
.character
.length
= len
;
324 result
->value
.character
.string
= gfc_getmem (len
+ 1);
326 for (count
= 0, i
= len
- 1; i
>= 0; --i
)
328 ch
= e
->value
.character
.string
[i
];
334 for (i
= 0; i
< count
; ++i
)
336 result
->value
.character
.string
[i
] = ' ';
339 for (i
= count
; i
< len
; ++i
)
341 result
->value
.character
.string
[i
] =
342 e
->value
.character
.string
[i
- count
];
345 result
->value
.character
.string
[len
] = '\0'; /* For debugger */
352 gfc_simplify_aimag (gfc_expr
* e
)
356 if (e
->expr_type
!= EXPR_CONSTANT
)
359 result
= gfc_constant_result (BT_REAL
, e
->ts
.kind
, &e
->where
);
360 mpfr_set (result
->value
.real
, e
->value
.complex.i
, GFC_RND_MODE
);
362 return range_check (result
, "AIMAG");
367 gfc_simplify_aint (gfc_expr
* e
, gfc_expr
* k
)
369 gfc_expr
*rtrunc
, *result
;
372 kind
= get_kind (BT_REAL
, k
, "AINT", e
->ts
.kind
);
374 return &gfc_bad_expr
;
376 if (e
->expr_type
!= EXPR_CONSTANT
)
379 rtrunc
= gfc_copy_expr (e
);
381 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
383 result
= gfc_real2real (rtrunc
, kind
);
384 gfc_free_expr (rtrunc
);
386 return range_check (result
, "AINT");
391 gfc_simplify_dint (gfc_expr
* e
)
393 gfc_expr
*rtrunc
, *result
;
395 if (e
->expr_type
!= EXPR_CONSTANT
)
398 rtrunc
= gfc_copy_expr (e
);
400 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
402 result
= gfc_real2real (rtrunc
, gfc_default_double_kind
);
403 gfc_free_expr (rtrunc
);
405 return range_check (result
, "DINT");
410 gfc_simplify_anint (gfc_expr
* e
, gfc_expr
* k
)
412 gfc_expr
*rtrunc
, *result
;
416 kind
= get_kind (BT_REAL
, k
, "ANINT", e
->ts
.kind
);
418 return &gfc_bad_expr
;
420 if (e
->expr_type
!= EXPR_CONSTANT
)
423 result
= gfc_constant_result (e
->ts
.type
, kind
, &e
->where
);
425 rtrunc
= gfc_copy_expr (e
);
427 cmp
= mpfr_cmp_ui (e
->value
.real
, 0);
429 gfc_set_model_kind (kind
);
431 mpfr_set_str (half
, "0.5", 10, GFC_RND_MODE
);
435 mpfr_add (rtrunc
->value
.real
, e
->value
.real
, half
, GFC_RND_MODE
);
436 mpfr_trunc (result
->value
.real
, rtrunc
->value
.real
);
440 mpfr_sub (rtrunc
->value
.real
, e
->value
.real
, half
, GFC_RND_MODE
);
441 mpfr_trunc (result
->value
.real
, rtrunc
->value
.real
);
444 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
446 gfc_free_expr (rtrunc
);
449 return range_check (result
, "ANINT");
454 gfc_simplify_dnint (gfc_expr
* e
)
456 gfc_expr
*rtrunc
, *result
;
460 if (e
->expr_type
!= EXPR_CONSTANT
)
464 gfc_constant_result (BT_REAL
, gfc_default_double_kind
, &e
->where
);
466 rtrunc
= gfc_copy_expr (e
);
468 cmp
= mpfr_cmp_ui (e
->value
.real
, 0);
470 gfc_set_model_kind (gfc_default_double_kind
);
472 mpfr_set_str (half
, "0.5", 10, GFC_RND_MODE
);
476 mpfr_add (rtrunc
->value
.real
, e
->value
.real
, half
, GFC_RND_MODE
);
477 mpfr_trunc (result
->value
.real
, rtrunc
->value
.real
);
481 mpfr_sub (rtrunc
->value
.real
, e
->value
.real
, half
, GFC_RND_MODE
);
482 mpfr_trunc (result
->value
.real
, rtrunc
->value
.real
);
485 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
487 gfc_free_expr (rtrunc
);
490 return range_check (result
, "DNINT");
495 gfc_simplify_asin (gfc_expr
* x
)
499 if (x
->expr_type
!= EXPR_CONSTANT
)
502 if (mpfr_cmp_si (x
->value
.real
, 1) > 0 || mpfr_cmp_si (x
->value
.real
, -1) < 0)
504 gfc_error ("Argument of ASIN at %L must be between -1 and 1",
506 return &gfc_bad_expr
;
509 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
511 mpfr_asin(result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
513 return range_check (result
, "ASIN");
518 gfc_simplify_atan (gfc_expr
* x
)
522 if (x
->expr_type
!= EXPR_CONSTANT
)
525 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
527 mpfr_atan(result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
529 return range_check (result
, "ATAN");
535 gfc_simplify_atan2 (gfc_expr
* y
, gfc_expr
* x
)
539 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
542 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
544 if (mpfr_sgn (y
->value
.real
) == 0 && mpfr_sgn (x
->value
.real
) == 0)
547 ("If first argument of ATAN2 %L is zero, then the second argument "
548 "must not be zero", &x
->where
);
549 gfc_free_expr (result
);
550 return &gfc_bad_expr
;
553 arctangent2 (y
->value
.real
, x
->value
.real
, result
->value
.real
);
555 return range_check (result
, "ATAN2");
561 gfc_simplify_bit_size (gfc_expr
* e
)
566 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
567 result
= gfc_constant_result (BT_INTEGER
, e
->ts
.kind
, &e
->where
);
568 mpz_set_ui (result
->value
.integer
, gfc_integer_kinds
[i
].bit_size
);
575 gfc_simplify_btest (gfc_expr
* e
, gfc_expr
* bit
)
579 if (e
->expr_type
!= EXPR_CONSTANT
|| bit
->expr_type
!= EXPR_CONSTANT
)
582 if (gfc_extract_int (bit
, &b
) != NULL
|| b
< 0)
583 return gfc_logical_expr (0, &e
->where
);
585 return gfc_logical_expr (mpz_tstbit (e
->value
.integer
, b
), &e
->where
);
590 gfc_simplify_ceiling (gfc_expr
* e
, gfc_expr
* k
)
592 gfc_expr
*ceil
, *result
;
595 kind
= get_kind (BT_INTEGER
, k
, "CEILING", gfc_default_integer_kind
);
597 return &gfc_bad_expr
;
599 if (e
->expr_type
!= EXPR_CONSTANT
)
602 result
= gfc_constant_result (BT_INTEGER
, kind
, &e
->where
);
604 ceil
= gfc_copy_expr (e
);
606 mpfr_ceil (ceil
->value
.real
, e
->value
.real
);
607 gfc_mpfr_to_mpz(result
->value
.integer
, ceil
->value
.real
);
609 gfc_free_expr (ceil
);
611 return range_check (result
, "CEILING");
616 gfc_simplify_char (gfc_expr
* e
, gfc_expr
* k
)
621 kind
= get_kind (BT_CHARACTER
, k
, "CHAR", gfc_default_character_kind
);
623 return &gfc_bad_expr
;
625 if (e
->expr_type
!= EXPR_CONSTANT
)
628 if (gfc_extract_int (e
, &c
) != NULL
|| c
< 0 || c
> 255)
630 gfc_error ("Bad character in CHAR function at %L", &e
->where
);
631 return &gfc_bad_expr
;
634 result
= gfc_constant_result (BT_CHARACTER
, kind
, &e
->where
);
636 result
->value
.character
.length
= 1;
637 result
->value
.character
.string
= gfc_getmem (2);
639 result
->value
.character
.string
[0] = c
;
640 result
->value
.character
.string
[1] = '\0'; /* For debugger */
646 /* Common subroutine for simplifying CMPLX and DCMPLX. */
649 simplify_cmplx (const char *name
, gfc_expr
* x
, gfc_expr
* y
, int kind
)
653 result
= gfc_constant_result (BT_COMPLEX
, kind
, &x
->where
);
655 mpfr_set_ui (result
->value
.complex.i
, 0, GFC_RND_MODE
);
660 mpfr_set_z (result
->value
.complex.r
, x
->value
.integer
, GFC_RND_MODE
);
664 mpfr_set (result
->value
.complex.r
, x
->value
.real
, GFC_RND_MODE
);
668 mpfr_set (result
->value
.complex.r
, x
->value
.complex.r
, GFC_RND_MODE
);
669 mpfr_set (result
->value
.complex.i
, x
->value
.complex.i
, GFC_RND_MODE
);
673 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (x)");
681 mpfr_set_z (result
->value
.complex.i
, y
->value
.integer
, GFC_RND_MODE
);
685 mpfr_set (result
->value
.complex.i
, y
->value
.real
, GFC_RND_MODE
);
689 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (y)");
693 return range_check (result
, name
);
698 gfc_simplify_cmplx (gfc_expr
* x
, gfc_expr
* y
, gfc_expr
* k
)
702 if (x
->expr_type
!= EXPR_CONSTANT
703 || (y
!= NULL
&& y
->expr_type
!= EXPR_CONSTANT
))
706 kind
= get_kind (BT_REAL
, k
, "CMPLX", gfc_default_real_kind
);
708 return &gfc_bad_expr
;
710 return simplify_cmplx ("CMPLX", x
, y
, kind
);
715 gfc_simplify_conjg (gfc_expr
* e
)
719 if (e
->expr_type
!= EXPR_CONSTANT
)
722 result
= gfc_copy_expr (e
);
723 mpfr_neg (result
->value
.complex.i
, result
->value
.complex.i
, GFC_RND_MODE
);
725 return range_check (result
, "CONJG");
730 gfc_simplify_cos (gfc_expr
* x
)
735 if (x
->expr_type
!= EXPR_CONSTANT
)
738 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
743 mpfr_cos (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
746 gfc_set_model_kind (x
->ts
.kind
);
750 mpfr_cos (xp
, x
->value
.complex.r
, GFC_RND_MODE
);
751 mpfr_cosh (xq
, x
->value
.complex.i
, GFC_RND_MODE
);
752 mpfr_mul(result
->value
.complex.r
, xp
, xq
, GFC_RND_MODE
);
754 mpfr_sin (xp
, x
->value
.complex.r
, GFC_RND_MODE
);
755 mpfr_sinh (xq
, x
->value
.complex.i
, GFC_RND_MODE
);
756 mpfr_mul (xp
, xp
, xq
, GFC_RND_MODE
);
757 mpfr_neg (result
->value
.complex.i
, xp
, GFC_RND_MODE
);
763 gfc_internal_error ("in gfc_simplify_cos(): Bad type");
766 return range_check (result
, "COS");
772 gfc_simplify_cosh (gfc_expr
* x
)
776 if (x
->expr_type
!= EXPR_CONSTANT
)
779 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
781 mpfr_cosh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
783 return range_check (result
, "COSH");
788 gfc_simplify_dcmplx (gfc_expr
* x
, gfc_expr
* y
)
791 if (x
->expr_type
!= EXPR_CONSTANT
792 || (y
!= NULL
&& y
->expr_type
!= EXPR_CONSTANT
))
795 return simplify_cmplx ("DCMPLX", x
, y
, gfc_default_double_kind
);
800 gfc_simplify_dble (gfc_expr
* e
)
804 if (e
->expr_type
!= EXPR_CONSTANT
)
810 result
= gfc_int2real (e
, gfc_default_double_kind
);
814 result
= gfc_real2real (e
, gfc_default_double_kind
);
818 result
= gfc_complex2real (e
, gfc_default_double_kind
);
822 gfc_internal_error ("gfc_simplify_dble(): bad type at %L", &e
->where
);
825 return range_check (result
, "DBLE");
830 gfc_simplify_digits (gfc_expr
* x
)
834 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
838 digits
= gfc_integer_kinds
[i
].digits
;
843 digits
= gfc_real_kinds
[i
].digits
;
850 return gfc_int_expr (digits
);
855 gfc_simplify_dim (gfc_expr
* x
, gfc_expr
* y
)
859 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
862 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
867 if (mpz_cmp (x
->value
.integer
, y
->value
.integer
) > 0)
868 mpz_sub (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
870 mpz_set_ui (result
->value
.integer
, 0);
875 if (mpfr_cmp (x
->value
.real
, y
->value
.real
) > 0)
876 mpfr_sub (result
->value
.real
, x
->value
.real
, y
->value
.real
, GFC_RND_MODE
);
878 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
883 gfc_internal_error ("gfc_simplify_dim(): Bad type");
886 return range_check (result
, "DIM");
891 gfc_simplify_dprod (gfc_expr
* x
, gfc_expr
* y
)
893 gfc_expr
*a1
, *a2
, *result
;
895 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
899 gfc_constant_result (BT_REAL
, gfc_default_double_kind
, &x
->where
);
901 a1
= gfc_real2real (x
, gfc_default_double_kind
);
902 a2
= gfc_real2real (y
, gfc_default_double_kind
);
904 mpfr_mul (result
->value
.real
, a1
->value
.real
, a2
->value
.real
, GFC_RND_MODE
);
909 return range_check (result
, "DPROD");
914 gfc_simplify_epsilon (gfc_expr
* e
)
919 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
921 result
= gfc_constant_result (BT_REAL
, e
->ts
.kind
, &e
->where
);
923 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].epsilon
, GFC_RND_MODE
);
925 return range_check (result
, "EPSILON");
930 gfc_simplify_exp (gfc_expr
* x
)
935 if (x
->expr_type
!= EXPR_CONSTANT
)
938 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
943 mpfr_exp(result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
947 gfc_set_model_kind (x
->ts
.kind
);
950 mpfr_exp (xq
, x
->value
.complex.r
, GFC_RND_MODE
);
951 mpfr_cos (xp
, x
->value
.complex.i
, GFC_RND_MODE
);
952 mpfr_mul (result
->value
.complex.r
, xq
, xp
, GFC_RND_MODE
);
953 mpfr_sin (xp
, x
->value
.complex.i
, GFC_RND_MODE
);
954 mpfr_mul (result
->value
.complex.i
, xq
, xp
, GFC_RND_MODE
);
960 gfc_internal_error ("in gfc_simplify_exp(): Bad type");
963 return range_check (result
, "EXP");
966 /* FIXME: MPFR should be able to do this better */
968 gfc_simplify_exponent (gfc_expr
* x
)
974 if (x
->expr_type
!= EXPR_CONSTANT
)
977 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
980 gfc_set_model (x
->value
.real
);
982 if (mpfr_sgn (x
->value
.real
) == 0)
984 mpz_set_ui (result
->value
.integer
, 0);
990 mpfr_abs (tmp
, x
->value
.real
, GFC_RND_MODE
);
991 mpfr_log2 (tmp
, tmp
, GFC_RND_MODE
);
993 gfc_mpfr_to_mpz (result
->value
.integer
, tmp
);
995 /* The model number for tiny(x) is b**(emin - 1) where b is the base and emin
996 is the smallest exponent value. So, we need to add 1 if x is tiny(x). */
997 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
998 if (mpfr_cmp (x
->value
.real
, gfc_real_kinds
[i
].tiny
) == 0)
999 mpz_add_ui (result
->value
.integer
,result
->value
.integer
, 1);
1003 return range_check (result
, "EXPONENT");
1008 gfc_simplify_float (gfc_expr
* a
)
1012 if (a
->expr_type
!= EXPR_CONSTANT
)
1015 result
= gfc_int2real (a
, gfc_default_real_kind
);
1016 return range_check (result
, "FLOAT");
1021 gfc_simplify_floor (gfc_expr
* e
, gfc_expr
* k
)
1027 kind
= get_kind (BT_INTEGER
, k
, "FLOOR", gfc_default_integer_kind
);
1029 gfc_internal_error ("gfc_simplify_floor(): Bad kind");
1031 if (e
->expr_type
!= EXPR_CONSTANT
)
1034 result
= gfc_constant_result (BT_INTEGER
, kind
, &e
->where
);
1036 gfc_set_model_kind (kind
);
1038 mpfr_floor (floor
, e
->value
.real
);
1040 gfc_mpfr_to_mpz (result
->value
.integer
, floor
);
1044 return range_check (result
, "FLOOR");
1049 gfc_simplify_fraction (gfc_expr
* x
)
1052 mpfr_t absv
, exp
, pow2
;
1054 if (x
->expr_type
!= EXPR_CONSTANT
)
1057 result
= gfc_constant_result (BT_REAL
, x
->ts
.kind
, &x
->where
);
1059 gfc_set_model_kind (x
->ts
.kind
);
1061 if (mpfr_sgn (x
->value
.real
) == 0)
1063 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
1071 mpfr_abs (absv
, x
->value
.real
, GFC_RND_MODE
);
1072 mpfr_log2 (exp
, absv
, GFC_RND_MODE
);
1074 mpfr_trunc (exp
, exp
);
1075 mpfr_add_ui (exp
, exp
, 1, GFC_RND_MODE
);
1077 mpfr_ui_pow (pow2
, 2, exp
, GFC_RND_MODE
);
1079 mpfr_div (result
->value
.real
, absv
, pow2
, GFC_RND_MODE
);
1085 return range_check (result
, "FRACTION");
1090 gfc_simplify_huge (gfc_expr
* e
)
1095 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
1097 result
= gfc_constant_result (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
1102 mpz_set (result
->value
.integer
, gfc_integer_kinds
[i
].huge
);
1106 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].huge
, GFC_RND_MODE
);
1118 gfc_simplify_iachar (gfc_expr
* e
)
1123 if (e
->expr_type
!= EXPR_CONSTANT
)
1126 if (e
->value
.character
.length
!= 1)
1128 gfc_error ("Argument of IACHAR at %L must be of length one", &e
->where
);
1129 return &gfc_bad_expr
;
1132 index
= xascii_table
[(int) e
->value
.character
.string
[0] & 0xFF];
1134 result
= gfc_int_expr (index
);
1135 result
->where
= e
->where
;
1137 return range_check (result
, "IACHAR");
1142 gfc_simplify_iand (gfc_expr
* x
, gfc_expr
* y
)
1146 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1149 result
= gfc_constant_result (BT_INTEGER
, x
->ts
.kind
, &x
->where
);
1151 mpz_and (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
1153 return range_check (result
, "IAND");
1158 gfc_simplify_ibclr (gfc_expr
* x
, gfc_expr
* y
)
1163 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1166 if (gfc_extract_int (y
, &pos
) != NULL
|| pos
< 0)
1168 gfc_error ("Invalid second argument of IBCLR at %L", &y
->where
);
1169 return &gfc_bad_expr
;
1172 k
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
1174 if (pos
> gfc_integer_kinds
[k
].bit_size
)
1176 gfc_error ("Second argument of IBCLR exceeds bit size at %L",
1178 return &gfc_bad_expr
;
1181 result
= gfc_copy_expr (x
);
1183 mpz_clrbit (result
->value
.integer
, pos
);
1184 return range_check (result
, "IBCLR");
1189 gfc_simplify_ibits (gfc_expr
* x
, gfc_expr
* y
, gfc_expr
* z
)
1196 if (x
->expr_type
!= EXPR_CONSTANT
1197 || y
->expr_type
!= EXPR_CONSTANT
1198 || z
->expr_type
!= EXPR_CONSTANT
)
1201 if (gfc_extract_int (y
, &pos
) != NULL
|| pos
< 0)
1203 gfc_error ("Invalid second argument of IBITS at %L", &y
->where
);
1204 return &gfc_bad_expr
;
1207 if (gfc_extract_int (z
, &len
) != NULL
|| len
< 0)
1209 gfc_error ("Invalid third argument of IBITS at %L", &z
->where
);
1210 return &gfc_bad_expr
;
1213 k
= gfc_validate_kind (BT_INTEGER
, x
->ts
.kind
, false);
1215 bitsize
= gfc_integer_kinds
[k
].bit_size
;
1217 if (pos
+ len
> bitsize
)
1220 ("Sum of second and third arguments of IBITS exceeds bit size "
1221 "at %L", &y
->where
);
1222 return &gfc_bad_expr
;
1225 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1227 bits
= gfc_getmem (bitsize
* sizeof (int));
1229 for (i
= 0; i
< bitsize
; i
++)
1232 for (i
= 0; i
< len
; i
++)
1233 bits
[i
] = mpz_tstbit (x
->value
.integer
, i
+ pos
);
1235 for (i
= 0; i
< bitsize
; i
++)
1239 mpz_clrbit (result
->value
.integer
, i
);
1241 else if (bits
[i
] == 1)
1243 mpz_setbit (result
->value
.integer
, i
);
1247 gfc_internal_error ("IBITS: Bad bit");
1253 return range_check (result
, "IBITS");
1258 gfc_simplify_ibset (gfc_expr
* x
, gfc_expr
* y
)
1263 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1266 if (gfc_extract_int (y
, &pos
) != NULL
|| pos
< 0)
1268 gfc_error ("Invalid second argument of IBSET at %L", &y
->where
);
1269 return &gfc_bad_expr
;
1272 k
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
1274 if (pos
> gfc_integer_kinds
[k
].bit_size
)
1276 gfc_error ("Second argument of IBSET exceeds bit size at %L",
1278 return &gfc_bad_expr
;
1281 result
= gfc_copy_expr (x
);
1283 mpz_setbit (result
->value
.integer
, pos
);
1284 return range_check (result
, "IBSET");
1289 gfc_simplify_ichar (gfc_expr
* e
)
1294 if (e
->expr_type
!= EXPR_CONSTANT
)
1297 if (e
->value
.character
.length
!= 1)
1299 gfc_error ("Argument of ICHAR at %L must be of length one", &e
->where
);
1300 return &gfc_bad_expr
;
1303 index
= (int) e
->value
.character
.string
[0];
1305 if (index
< CHAR_MIN
|| index
> CHAR_MAX
)
1307 gfc_error ("Argument of ICHAR at %L out of range of this processor",
1309 return &gfc_bad_expr
;
1312 result
= gfc_int_expr (index
);
1313 result
->where
= e
->where
;
1314 return range_check (result
, "ICHAR");
1319 gfc_simplify_ieor (gfc_expr
* x
, gfc_expr
* y
)
1323 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1326 result
= gfc_constant_result (BT_INTEGER
, x
->ts
.kind
, &x
->where
);
1328 mpz_xor (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
1330 return range_check (result
, "IEOR");
1335 gfc_simplify_index (gfc_expr
* x
, gfc_expr
* y
, gfc_expr
* b
)
1338 int back
, len
, lensub
;
1339 int i
, j
, k
, count
, index
= 0, start
;
1341 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1344 if (b
!= NULL
&& b
->value
.logical
!= 0)
1349 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
1352 len
= x
->value
.character
.length
;
1353 lensub
= y
->value
.character
.length
;
1357 mpz_set_si (result
->value
.integer
, 0);
1366 mpz_set_si (result
->value
.integer
, 1);
1369 else if (lensub
== 1)
1371 for (i
= 0; i
< len
; i
++)
1373 for (j
= 0; j
< lensub
; j
++)
1375 if (y
->value
.character
.string
[j
] ==
1376 x
->value
.character
.string
[i
])
1386 for (i
= 0; i
< len
; i
++)
1388 for (j
= 0; j
< lensub
; j
++)
1390 if (y
->value
.character
.string
[j
] ==
1391 x
->value
.character
.string
[i
])
1396 for (k
= 0; k
< lensub
; k
++)
1398 if (y
->value
.character
.string
[k
] ==
1399 x
->value
.character
.string
[k
+ start
])
1403 if (count
== lensub
)
1419 mpz_set_si (result
->value
.integer
, len
+ 1);
1422 else if (lensub
== 1)
1424 for (i
= 0; i
< len
; i
++)
1426 for (j
= 0; j
< lensub
; j
++)
1428 if (y
->value
.character
.string
[j
] ==
1429 x
->value
.character
.string
[len
- i
])
1431 index
= len
- i
+ 1;
1439 for (i
= 0; i
< len
; i
++)
1441 for (j
= 0; j
< lensub
; j
++)
1443 if (y
->value
.character
.string
[j
] ==
1444 x
->value
.character
.string
[len
- i
])
1447 if (start
<= len
- lensub
)
1450 for (k
= 0; k
< lensub
; k
++)
1451 if (y
->value
.character
.string
[k
] ==
1452 x
->value
.character
.string
[k
+ start
])
1455 if (count
== lensub
)
1472 mpz_set_si (result
->value
.integer
, index
);
1473 return range_check (result
, "INDEX");
1478 gfc_simplify_int (gfc_expr
* e
, gfc_expr
* k
)
1480 gfc_expr
*rpart
, *rtrunc
, *result
;
1483 kind
= get_kind (BT_INTEGER
, k
, "INT", gfc_default_integer_kind
);
1485 return &gfc_bad_expr
;
1487 if (e
->expr_type
!= EXPR_CONSTANT
)
1490 result
= gfc_constant_result (BT_INTEGER
, kind
, &e
->where
);
1495 mpz_set (result
->value
.integer
, e
->value
.integer
);
1499 rtrunc
= gfc_copy_expr (e
);
1500 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
1501 gfc_mpfr_to_mpz (result
->value
.integer
, rtrunc
->value
.real
);
1502 gfc_free_expr (rtrunc
);
1506 rpart
= gfc_complex2real (e
, kind
);
1507 rtrunc
= gfc_copy_expr (rpart
);
1508 mpfr_trunc (rtrunc
->value
.real
, rpart
->value
.real
);
1509 gfc_mpfr_to_mpz (result
->value
.integer
, rtrunc
->value
.real
);
1510 gfc_free_expr (rpart
);
1511 gfc_free_expr (rtrunc
);
1515 gfc_error ("Argument of INT at %L is not a valid type", &e
->where
);
1516 gfc_free_expr (result
);
1517 return &gfc_bad_expr
;
1520 return range_check (result
, "INT");
1525 gfc_simplify_ifix (gfc_expr
* e
)
1527 gfc_expr
*rtrunc
, *result
;
1529 if (e
->expr_type
!= EXPR_CONSTANT
)
1532 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
1535 rtrunc
= gfc_copy_expr (e
);
1537 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
1538 gfc_mpfr_to_mpz (result
->value
.integer
, rtrunc
->value
.real
);
1540 gfc_free_expr (rtrunc
);
1541 return range_check (result
, "IFIX");
1546 gfc_simplify_idint (gfc_expr
* e
)
1548 gfc_expr
*rtrunc
, *result
;
1550 if (e
->expr_type
!= EXPR_CONSTANT
)
1553 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
1556 rtrunc
= gfc_copy_expr (e
);
1558 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
1559 gfc_mpfr_to_mpz (result
->value
.integer
, rtrunc
->value
.real
);
1561 gfc_free_expr (rtrunc
);
1562 return range_check (result
, "IDINT");
1567 gfc_simplify_ior (gfc_expr
* x
, gfc_expr
* y
)
1571 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1574 result
= gfc_constant_result (BT_INTEGER
, x
->ts
.kind
, &x
->where
);
1576 mpz_ior (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
1577 return range_check (result
, "IOR");
1582 gfc_simplify_ishft (gfc_expr
* e
, gfc_expr
* s
)
1585 int shift
, ashift
, isize
, k
, *bits
, i
;
1587 if (e
->expr_type
!= EXPR_CONSTANT
|| s
->expr_type
!= EXPR_CONSTANT
)
1590 if (gfc_extract_int (s
, &shift
) != NULL
)
1592 gfc_error ("Invalid second argument of ISHFT at %L", &s
->where
);
1593 return &gfc_bad_expr
;
1596 k
= gfc_validate_kind (BT_INTEGER
, e
->ts
.kind
, false);
1598 isize
= gfc_integer_kinds
[k
].bit_size
;
1608 ("Magnitude of second argument of ISHFT exceeds bit size at %L",
1610 return &gfc_bad_expr
;
1613 result
= gfc_constant_result (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
1617 mpz_set (result
->value
.integer
, e
->value
.integer
);
1618 return range_check (result
, "ISHFT");
1621 bits
= gfc_getmem (isize
* sizeof (int));
1623 for (i
= 0; i
< isize
; i
++)
1624 bits
[i
] = mpz_tstbit (e
->value
.integer
, i
);
1628 for (i
= 0; i
< shift
; i
++)
1629 mpz_clrbit (result
->value
.integer
, i
);
1631 for (i
= 0; i
< isize
- shift
; i
++)
1634 mpz_clrbit (result
->value
.integer
, i
+ shift
);
1636 mpz_setbit (result
->value
.integer
, i
+ shift
);
1641 for (i
= isize
- 1; i
>= isize
- ashift
; i
--)
1642 mpz_clrbit (result
->value
.integer
, i
);
1644 for (i
= isize
- 1; i
>= ashift
; i
--)
1647 mpz_clrbit (result
->value
.integer
, i
- ashift
);
1649 mpz_setbit (result
->value
.integer
, i
- ashift
);
1653 twos_complement (result
->value
.integer
, isize
);
1661 gfc_simplify_ishftc (gfc_expr
* e
, gfc_expr
* s
, gfc_expr
* sz
)
1664 int shift
, ashift
, isize
, delta
, k
;
1667 if (e
->expr_type
!= EXPR_CONSTANT
|| s
->expr_type
!= EXPR_CONSTANT
)
1670 if (gfc_extract_int (s
, &shift
) != NULL
)
1672 gfc_error ("Invalid second argument of ISHFTC at %L", &s
->where
);
1673 return &gfc_bad_expr
;
1676 k
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
1680 if (gfc_extract_int (sz
, &isize
) != NULL
|| isize
< 0)
1682 gfc_error ("Invalid third argument of ISHFTC at %L", &sz
->where
);
1683 return &gfc_bad_expr
;
1687 isize
= gfc_integer_kinds
[k
].bit_size
;
1697 ("Magnitude of second argument of ISHFTC exceeds third argument "
1698 "at %L", &s
->where
);
1699 return &gfc_bad_expr
;
1702 result
= gfc_constant_result (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
1706 mpz_set (result
->value
.integer
, e
->value
.integer
);
1710 bits
= gfc_getmem (isize
* sizeof (int));
1712 for (i
= 0; i
< isize
; i
++)
1713 bits
[i
] = mpz_tstbit (e
->value
.integer
, i
);
1715 delta
= isize
- ashift
;
1719 for (i
= 0; i
< delta
; i
++)
1722 mpz_clrbit (result
->value
.integer
, i
+ shift
);
1724 mpz_setbit (result
->value
.integer
, i
+ shift
);
1727 for (i
= delta
; i
< isize
; i
++)
1730 mpz_clrbit (result
->value
.integer
, i
- delta
);
1732 mpz_setbit (result
->value
.integer
, i
- delta
);
1737 for (i
= 0; i
< ashift
; i
++)
1740 mpz_clrbit (result
->value
.integer
, i
+ delta
);
1742 mpz_setbit (result
->value
.integer
, i
+ delta
);
1745 for (i
= ashift
; i
< isize
; i
++)
1748 mpz_clrbit (result
->value
.integer
, i
+ shift
);
1750 mpz_setbit (result
->value
.integer
, i
+ shift
);
1754 twos_complement (result
->value
.integer
, isize
);
1762 gfc_simplify_kind (gfc_expr
* e
)
1765 if (e
->ts
.type
== BT_DERIVED
)
1767 gfc_error ("Argument of KIND at %L is a DERIVED type", &e
->where
);
1768 return &gfc_bad_expr
;
1771 return gfc_int_expr (e
->ts
.kind
);
1776 simplify_bound (gfc_expr
* array
, gfc_expr
* dim
, int upper
)
1783 if (array
->expr_type
!= EXPR_VARIABLE
)
1787 /* TODO: Simplify constant multi-dimensional bounds. */
1790 if (dim
->expr_type
!= EXPR_CONSTANT
)
1793 /* Follow any component references. */
1794 as
= array
->symtree
->n
.sym
->as
;
1795 for (ref
= array
->ref
; ref
; ref
= ref
->next
)
1800 switch (ref
->u
.ar
.type
)
1807 /* We're done because 'as' has already been set in the
1808 previous iteration. */
1819 as
= ref
->u
.c
.component
->as
;
1830 if (as
->type
== AS_DEFERRED
|| as
->type
== AS_ASSUMED_SHAPE
)
1833 d
= mpz_get_si (dim
->value
.integer
);
1835 if (d
< 1 || d
> as
->rank
1836 || (d
== as
->rank
&& as
->type
== AS_ASSUMED_SIZE
&& upper
))
1838 gfc_error ("DIM argument at %L is out of bounds", &dim
->where
);
1839 return &gfc_bad_expr
;
1842 e
= upper
? as
->upper
[d
-1] : as
->lower
[d
-1];
1844 if (e
->expr_type
!= EXPR_CONSTANT
)
1847 return gfc_copy_expr (e
);
1852 gfc_simplify_lbound (gfc_expr
* array
, gfc_expr
* dim
)
1854 return simplify_bound (array
, dim
, 0);
1859 gfc_simplify_len (gfc_expr
* e
)
1863 if (e
->expr_type
!= EXPR_CONSTANT
)
1866 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
1869 mpz_set_si (result
->value
.integer
, e
->value
.character
.length
);
1870 return range_check (result
, "LEN");
1875 gfc_simplify_len_trim (gfc_expr
* e
)
1878 int count
, len
, lentrim
, i
;
1880 if (e
->expr_type
!= EXPR_CONSTANT
)
1883 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
1886 len
= e
->value
.character
.length
;
1888 for (count
= 0, i
= 1; i
<= len
; i
++)
1889 if (e
->value
.character
.string
[len
- i
] == ' ')
1894 lentrim
= len
- count
;
1896 mpz_set_si (result
->value
.integer
, lentrim
);
1897 return range_check (result
, "LEN_TRIM");
1902 gfc_simplify_lge (gfc_expr
* a
, gfc_expr
* b
)
1905 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
1908 return gfc_logical_expr (gfc_compare_string (a
, b
, xascii_table
) >= 0,
1914 gfc_simplify_lgt (gfc_expr
* a
, gfc_expr
* b
)
1917 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
1920 return gfc_logical_expr (gfc_compare_string (a
, b
, xascii_table
) > 0,
1926 gfc_simplify_lle (gfc_expr
* a
, gfc_expr
* b
)
1929 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
1932 return gfc_logical_expr (gfc_compare_string (a
, b
, xascii_table
) <= 0,
1938 gfc_simplify_llt (gfc_expr
* a
, gfc_expr
* b
)
1941 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
1944 return gfc_logical_expr (gfc_compare_string (a
, b
, xascii_table
) < 0,
1950 gfc_simplify_log (gfc_expr
* x
)
1955 if (x
->expr_type
!= EXPR_CONSTANT
)
1958 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1960 gfc_set_model_kind (x
->ts
.kind
);
1965 if (mpfr_sgn (x
->value
.real
) <= 0)
1968 ("Argument of LOG at %L cannot be less than or equal to zero",
1970 gfc_free_expr (result
);
1971 return &gfc_bad_expr
;
1974 mpfr_log(result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1978 if ((mpfr_sgn (x
->value
.complex.r
) == 0)
1979 && (mpfr_sgn (x
->value
.complex.i
) == 0))
1981 gfc_error ("Complex argument of LOG at %L cannot be zero",
1983 gfc_free_expr (result
);
1984 return &gfc_bad_expr
;
1990 arctangent2 (x
->value
.complex.i
, x
->value
.complex.r
,
1991 result
->value
.complex.i
);
1993 mpfr_mul (xr
, x
->value
.complex.r
, x
->value
.complex.r
, GFC_RND_MODE
);
1994 mpfr_mul (xi
, x
->value
.complex.i
, x
->value
.complex.i
, GFC_RND_MODE
);
1995 mpfr_add (xr
, xr
, xi
, GFC_RND_MODE
);
1996 mpfr_sqrt (xr
, xr
, GFC_RND_MODE
);
1997 mpfr_log (result
->value
.complex.r
, xr
, GFC_RND_MODE
);
2005 gfc_internal_error ("gfc_simplify_log: bad type");
2008 return range_check (result
, "LOG");
2013 gfc_simplify_log10 (gfc_expr
* x
)
2017 if (x
->expr_type
!= EXPR_CONSTANT
)
2020 gfc_set_model_kind (x
->ts
.kind
);
2022 if (mpfr_sgn (x
->value
.real
) <= 0)
2025 ("Argument of LOG10 at %L cannot be less than or equal to zero",
2027 return &gfc_bad_expr
;
2030 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2032 mpfr_log10 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
2034 return range_check (result
, "LOG10");
2039 gfc_simplify_logical (gfc_expr
* e
, gfc_expr
* k
)
2044 kind
= get_kind (BT_LOGICAL
, k
, "LOGICAL", gfc_default_logical_kind
);
2046 return &gfc_bad_expr
;
2048 if (e
->expr_type
!= EXPR_CONSTANT
)
2051 result
= gfc_constant_result (BT_LOGICAL
, kind
, &e
->where
);
2053 result
->value
.logical
= e
->value
.logical
;
2059 /* This function is special since MAX() can take any number of
2060 arguments. The simplified expression is a rewritten version of the
2061 argument list containing at most one constant element. Other
2062 constant elements are deleted. Because the argument list has
2063 already been checked, this function always succeeds. sign is 1 for
2064 MAX(), -1 for MIN(). */
2067 simplify_min_max (gfc_expr
* expr
, int sign
)
2069 gfc_actual_arglist
*arg
, *last
, *extremum
;
2070 gfc_intrinsic_sym
* specific
;
2074 specific
= expr
->value
.function
.isym
;
2076 arg
= expr
->value
.function
.actual
;
2078 for (; arg
; last
= arg
, arg
= arg
->next
)
2080 if (arg
->expr
->expr_type
!= EXPR_CONSTANT
)
2083 if (extremum
== NULL
)
2089 switch (arg
->expr
->ts
.type
)
2092 if (mpz_cmp (arg
->expr
->value
.integer
,
2093 extremum
->expr
->value
.integer
) * sign
> 0)
2094 mpz_set (extremum
->expr
->value
.integer
, arg
->expr
->value
.integer
);
2099 if (mpfr_cmp (arg
->expr
->value
.real
, extremum
->expr
->value
.real
) *
2101 mpfr_set (extremum
->expr
->value
.real
, arg
->expr
->value
.real
,
2107 gfc_internal_error ("gfc_simplify_max(): Bad type in arglist");
2110 /* Delete the extra constant argument. */
2112 expr
->value
.function
.actual
= arg
->next
;
2114 last
->next
= arg
->next
;
2117 gfc_free_actual_arglist (arg
);
2121 /* If there is one value left, replace the function call with the
2123 if (expr
->value
.function
.actual
->next
!= NULL
)
2126 /* Convert to the correct type and kind. */
2127 if (expr
->ts
.type
!= BT_UNKNOWN
)
2128 return gfc_convert_constant (expr
->value
.function
.actual
->expr
,
2129 expr
->ts
.type
, expr
->ts
.kind
);
2131 if (specific
->ts
.type
!= BT_UNKNOWN
)
2132 return gfc_convert_constant (expr
->value
.function
.actual
->expr
,
2133 specific
->ts
.type
, specific
->ts
.kind
);
2135 return gfc_copy_expr (expr
->value
.function
.actual
->expr
);
2140 gfc_simplify_min (gfc_expr
* e
)
2142 return simplify_min_max (e
, -1);
2147 gfc_simplify_max (gfc_expr
* e
)
2149 return simplify_min_max (e
, 1);
2154 gfc_simplify_maxexponent (gfc_expr
* x
)
2159 i
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, false);
2161 result
= gfc_int_expr (gfc_real_kinds
[i
].max_exponent
);
2162 result
->where
= x
->where
;
2169 gfc_simplify_minexponent (gfc_expr
* x
)
2174 i
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, false);
2176 result
= gfc_int_expr (gfc_real_kinds
[i
].min_exponent
);
2177 result
->where
= x
->where
;
2184 gfc_simplify_mod (gfc_expr
* a
, gfc_expr
* p
)
2187 mpfr_t quot
, iquot
, term
;
2189 if (a
->expr_type
!= EXPR_CONSTANT
|| p
->expr_type
!= EXPR_CONSTANT
)
2192 result
= gfc_constant_result (a
->ts
.type
, a
->ts
.kind
, &a
->where
);
2197 if (mpz_cmp_ui (p
->value
.integer
, 0) == 0)
2199 /* Result is processor-dependent. */
2200 gfc_error ("Second argument MOD at %L is zero", &a
->where
);
2201 gfc_free_expr (result
);
2202 return &gfc_bad_expr
;
2204 mpz_tdiv_r (result
->value
.integer
, a
->value
.integer
, p
->value
.integer
);
2208 if (mpfr_cmp_ui (p
->value
.real
, 0) == 0)
2210 /* Result is processor-dependent. */
2211 gfc_error ("Second argument of MOD at %L is zero", &p
->where
);
2212 gfc_free_expr (result
);
2213 return &gfc_bad_expr
;
2216 gfc_set_model_kind (a
->ts
.kind
);
2221 mpfr_div (quot
, a
->value
.real
, p
->value
.real
, GFC_RND_MODE
);
2222 mpfr_trunc (iquot
, quot
);
2223 mpfr_mul (term
, iquot
, p
->value
.real
, GFC_RND_MODE
);
2224 mpfr_sub (result
->value
.real
, a
->value
.real
, term
, GFC_RND_MODE
);
2232 gfc_internal_error ("gfc_simplify_mod(): Bad arguments");
2235 return range_check (result
, "MOD");
2240 gfc_simplify_modulo (gfc_expr
* a
, gfc_expr
* p
)
2243 mpfr_t quot
, iquot
, term
;
2245 if (a
->expr_type
!= EXPR_CONSTANT
|| p
->expr_type
!= EXPR_CONSTANT
)
2248 result
= gfc_constant_result (a
->ts
.type
, a
->ts
.kind
, &a
->where
);
2253 if (mpz_cmp_ui (p
->value
.integer
, 0) == 0)
2255 /* Result is processor-dependent. This processor just opts
2256 to not handle it at all. */
2257 gfc_error ("Second argument of MODULO at %L is zero", &a
->where
);
2258 gfc_free_expr (result
);
2259 return &gfc_bad_expr
;
2261 mpz_fdiv_r (result
->value
.integer
, a
->value
.integer
, p
->value
.integer
);
2266 if (mpfr_cmp_ui (p
->value
.real
, 0) == 0)
2268 /* Result is processor-dependent. */
2269 gfc_error ("Second argument of MODULO at %L is zero", &p
->where
);
2270 gfc_free_expr (result
);
2271 return &gfc_bad_expr
;
2274 gfc_set_model_kind (a
->ts
.kind
);
2279 mpfr_div (quot
, a
->value
.real
, p
->value
.real
, GFC_RND_MODE
);
2280 mpfr_floor (iquot
, quot
);
2281 mpfr_mul (term
, iquot
, p
->value
.real
, GFC_RND_MODE
);
2287 mpfr_sub (result
->value
.real
, a
->value
.real
, term
, GFC_RND_MODE
);
2291 gfc_internal_error ("gfc_simplify_modulo(): Bad arguments");
2294 return range_check (result
, "MODULO");
2298 /* Exists for the sole purpose of consistency with other intrinsics. */
2300 gfc_simplify_mvbits (gfc_expr
* f ATTRIBUTE_UNUSED
,
2301 gfc_expr
* fp ATTRIBUTE_UNUSED
,
2302 gfc_expr
* l ATTRIBUTE_UNUSED
,
2303 gfc_expr
* to ATTRIBUTE_UNUSED
,
2304 gfc_expr
* tp ATTRIBUTE_UNUSED
)
2311 gfc_simplify_nearest (gfc_expr
* x
, gfc_expr
* s
)
2316 int p
, i
, k
, match_float
;
2318 /* FIXME: This implementation is dopey and probably not quite right,
2319 but it's a start. */
2321 if (x
->expr_type
!= EXPR_CONSTANT
)
2324 k
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
2326 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2328 val
= mpfr_get_d (x
->value
.real
, GFC_RND_MODE
);
2329 p
= gfc_real_kinds
[k
].digits
;
2332 for (i
= 1; i
< p
; ++i
)
2337 /* TODO we should make sure that 'float' matches kind 4 */
2338 match_float
= gfc_real_kinds
[k
].kind
== 4;
2339 if (mpfr_cmp_ui (s
->value
.real
, 0) > 0)
2345 mpfr_set_d (result
->value
.real
, rval
, GFC_RND_MODE
);
2350 mpfr_set_d (result
->value
.real
, val
, GFC_RND_MODE
);
2353 else if (mpfr_cmp_ui (s
->value
.real
, 0) < 0)
2359 mpfr_set_d (result
->value
.real
, rval
, GFC_RND_MODE
);
2364 mpfr_set_d (result
->value
.real
, val
, GFC_RND_MODE
);
2369 gfc_error ("Invalid second argument of NEAREST at %L", &s
->where
);
2371 return &gfc_bad_expr
;
2374 return range_check (result
, "NEAREST");
2379 simplify_nint (const char *name
, gfc_expr
* e
, gfc_expr
* k
)
2381 gfc_expr
*rtrunc
, *itrunc
, *result
;
2385 kind
= get_kind (BT_INTEGER
, k
, name
, gfc_default_integer_kind
);
2387 return &gfc_bad_expr
;
2389 if (e
->expr_type
!= EXPR_CONSTANT
)
2392 result
= gfc_constant_result (BT_INTEGER
, kind
, &e
->where
);
2394 rtrunc
= gfc_copy_expr (e
);
2395 itrunc
= gfc_copy_expr (e
);
2397 cmp
= mpfr_cmp_ui (e
->value
.real
, 0);
2399 gfc_set_model (e
->value
.real
);
2401 mpfr_set_str (half
, "0.5", 10, GFC_RND_MODE
);
2405 mpfr_add (rtrunc
->value
.real
, e
->value
.real
, half
, GFC_RND_MODE
);
2406 mpfr_trunc (itrunc
->value
.real
, rtrunc
->value
.real
);
2410 mpfr_sub (rtrunc
->value
.real
, e
->value
.real
, half
, GFC_RND_MODE
);
2411 mpfr_trunc (itrunc
->value
.real
, rtrunc
->value
.real
);
2414 mpfr_set_ui (itrunc
->value
.real
, 0, GFC_RND_MODE
);
2416 gfc_mpfr_to_mpz (result
->value
.integer
, itrunc
->value
.real
);
2418 gfc_free_expr (itrunc
);
2419 gfc_free_expr (rtrunc
);
2422 return range_check (result
, name
);
2427 gfc_simplify_nint (gfc_expr
* e
, gfc_expr
* k
)
2429 return simplify_nint ("NINT", e
, k
);
2434 gfc_simplify_idnint (gfc_expr
* e
)
2436 return simplify_nint ("IDNINT", e
, NULL
);
2441 gfc_simplify_not (gfc_expr
* e
)
2446 if (e
->expr_type
!= EXPR_CONSTANT
)
2449 result
= gfc_constant_result (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
2451 mpz_com (result
->value
.integer
, e
->value
.integer
);
2453 /* Because of how GMP handles numbers, the result must be ANDed with
2454 the max_int mask. For radices <> 2, this will require change. */
2456 i
= gfc_validate_kind (BT_INTEGER
, e
->ts
.kind
, false);
2458 mpz_and (result
->value
.integer
, result
->value
.integer
,
2459 gfc_integer_kinds
[i
].max_int
);
2461 return range_check (result
, "NOT");
2466 gfc_simplify_null (gfc_expr
* mold
)
2470 result
= gfc_get_expr ();
2471 result
->expr_type
= EXPR_NULL
;
2474 result
->ts
.type
= BT_UNKNOWN
;
2477 result
->ts
= mold
->ts
;
2478 result
->where
= mold
->where
;
2486 gfc_simplify_precision (gfc_expr
* e
)
2491 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
2493 result
= gfc_int_expr (gfc_real_kinds
[i
].precision
);
2494 result
->where
= e
->where
;
2501 gfc_simplify_radix (gfc_expr
* e
)
2506 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
2510 i
= gfc_integer_kinds
[i
].radix
;
2514 i
= gfc_real_kinds
[i
].radix
;
2521 result
= gfc_int_expr (i
);
2522 result
->where
= e
->where
;
2529 gfc_simplify_range (gfc_expr
* e
)
2535 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
2540 j
= gfc_integer_kinds
[i
].range
;
2545 j
= gfc_real_kinds
[i
].range
;
2552 result
= gfc_int_expr (j
);
2553 result
->where
= e
->where
;
2560 gfc_simplify_real (gfc_expr
* e
, gfc_expr
* k
)
2565 if (e
->ts
.type
== BT_COMPLEX
)
2566 kind
= get_kind (BT_REAL
, k
, "REAL", e
->ts
.kind
);
2568 kind
= get_kind (BT_REAL
, k
, "REAL", gfc_default_real_kind
);
2571 return &gfc_bad_expr
;
2573 if (e
->expr_type
!= EXPR_CONSTANT
)
2579 result
= gfc_int2real (e
, kind
);
2583 result
= gfc_real2real (e
, kind
);
2587 result
= gfc_complex2real (e
, kind
);
2591 gfc_internal_error ("bad type in REAL");
2595 return range_check (result
, "REAL");
2599 gfc_simplify_repeat (gfc_expr
* e
, gfc_expr
* n
)
2602 int i
, j
, len
, ncopies
, nlen
;
2604 if (e
->expr_type
!= EXPR_CONSTANT
|| n
->expr_type
!= EXPR_CONSTANT
)
2607 if (n
!= NULL
&& (gfc_extract_int (n
, &ncopies
) != NULL
|| ncopies
< 0))
2609 gfc_error ("Invalid second argument of REPEAT at %L", &n
->where
);
2610 return &gfc_bad_expr
;
2613 len
= e
->value
.character
.length
;
2614 nlen
= ncopies
* len
;
2616 result
= gfc_constant_result (BT_CHARACTER
, e
->ts
.kind
, &e
->where
);
2620 result
->value
.character
.string
= gfc_getmem (1);
2621 result
->value
.character
.length
= 0;
2622 result
->value
.character
.string
[0] = '\0';
2626 result
->value
.character
.length
= nlen
;
2627 result
->value
.character
.string
= gfc_getmem (nlen
+ 1);
2629 for (i
= 0; i
< ncopies
; i
++)
2630 for (j
= 0; j
< len
; j
++)
2631 result
->value
.character
.string
[j
+ i
* len
] =
2632 e
->value
.character
.string
[j
];
2634 result
->value
.character
.string
[nlen
] = '\0'; /* For debugger */
2639 /* This one is a bear, but mainly has to do with shuffling elements. */
2642 gfc_simplify_reshape (gfc_expr
* source
, gfc_expr
* shape_exp
,
2643 gfc_expr
* pad
, gfc_expr
* order_exp
)
2646 int order
[GFC_MAX_DIMENSIONS
], shape
[GFC_MAX_DIMENSIONS
];
2647 int i
, rank
, npad
, x
[GFC_MAX_DIMENSIONS
];
2648 gfc_constructor
*head
, *tail
;
2654 /* Unpack the shape array. */
2655 if (source
->expr_type
!= EXPR_ARRAY
|| !gfc_is_constant_expr (source
))
2658 if (shape_exp
->expr_type
!= EXPR_ARRAY
|| !gfc_is_constant_expr (shape_exp
))
2662 && (pad
->expr_type
!= EXPR_ARRAY
2663 || !gfc_is_constant_expr (pad
)))
2666 if (order_exp
!= NULL
2667 && (order_exp
->expr_type
!= EXPR_ARRAY
2668 || !gfc_is_constant_expr (order_exp
)))
2677 e
= gfc_get_array_element (shape_exp
, rank
);
2681 if (gfc_extract_int (e
, &shape
[rank
]) != NULL
)
2683 gfc_error ("Integer too large in shape specification at %L",
2691 if (rank
>= GFC_MAX_DIMENSIONS
)
2693 gfc_error ("Too many dimensions in shape specification for RESHAPE "
2694 "at %L", &e
->where
);
2699 if (shape
[rank
] < 0)
2701 gfc_error ("Shape specification at %L cannot be negative",
2711 gfc_error ("Shape specification at %L cannot be the null array",
2716 /* Now unpack the order array if present. */
2717 if (order_exp
== NULL
)
2719 for (i
= 0; i
< rank
; i
++)
2726 for (i
= 0; i
< rank
; i
++)
2729 for (i
= 0; i
< rank
; i
++)
2731 e
= gfc_get_array_element (order_exp
, i
);
2735 ("ORDER parameter of RESHAPE at %L is not the same size "
2736 "as SHAPE parameter", &order_exp
->where
);
2740 if (gfc_extract_int (e
, &order
[i
]) != NULL
)
2742 gfc_error ("Error in ORDER parameter of RESHAPE at %L",
2750 if (order
[i
] < 1 || order
[i
] > rank
)
2752 gfc_error ("ORDER parameter of RESHAPE at %L is out of range",
2761 gfc_error ("Invalid permutation in ORDER parameter at %L",
2770 /* Count the elements in the source and padding arrays. */
2775 gfc_array_size (pad
, &size
);
2776 npad
= mpz_get_ui (size
);
2780 gfc_array_size (source
, &size
);
2781 nsource
= mpz_get_ui (size
);
2784 /* If it weren't for that pesky permutation we could just loop
2785 through the source and round out any shortage with pad elements.
2786 But no, someone just had to have the compiler do something the
2787 user should be doing. */
2789 for (i
= 0; i
< rank
; i
++)
2794 /* Figure out which element to extract. */
2795 mpz_set_ui (index
, 0);
2797 for (i
= rank
- 1; i
>= 0; i
--)
2799 mpz_add_ui (index
, index
, x
[order
[i
]]);
2801 mpz_mul_ui (index
, index
, shape
[order
[i
- 1]]);
2804 if (mpz_cmp_ui (index
, INT_MAX
) > 0)
2805 gfc_internal_error ("Reshaped array too large at %L", &e
->where
);
2807 j
= mpz_get_ui (index
);
2810 e
= gfc_get_array_element (source
, j
);
2818 ("PAD parameter required for short SOURCE parameter at %L",
2824 e
= gfc_get_array_element (pad
, j
);
2828 head
= tail
= gfc_get_constructor ();
2831 tail
->next
= gfc_get_constructor ();
2838 tail
->where
= e
->where
;
2841 /* Calculate the next element. */
2845 if (++x
[i
] < shape
[i
])
2856 e
= gfc_get_expr ();
2857 e
->where
= source
->where
;
2858 e
->expr_type
= EXPR_ARRAY
;
2859 e
->value
.constructor
= head
;
2860 e
->shape
= gfc_get_shape (rank
);
2862 for (i
= 0; i
< rank
; i
++)
2863 mpz_init_set_ui (e
->shape
[i
], shape
[i
]);
2865 e
->ts
= head
->expr
->ts
;
2871 gfc_free_constructor (head
);
2873 return &gfc_bad_expr
;
2878 gfc_simplify_rrspacing (gfc_expr
* x
)
2881 mpfr_t absv
, log2
, exp
, frac
, pow2
;
2884 if (x
->expr_type
!= EXPR_CONSTANT
)
2887 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
2889 result
= gfc_constant_result (BT_REAL
, x
->ts
.kind
, &x
->where
);
2891 p
= gfc_real_kinds
[i
].digits
;
2893 gfc_set_model_kind (x
->ts
.kind
);
2895 if (mpfr_sgn (x
->value
.real
) == 0)
2897 mpfr_ui_div (result
->value
.real
, 1, gfc_real_kinds
[i
].tiny
, GFC_RND_MODE
);
2906 mpfr_abs (absv
, x
->value
.real
, GFC_RND_MODE
);
2907 mpfr_log2 (log2
, absv
, GFC_RND_MODE
);
2909 mpfr_trunc (log2
, log2
);
2910 mpfr_add_ui (exp
, log2
, 1, GFC_RND_MODE
);
2912 mpfr_ui_pow (pow2
, 2, exp
, GFC_RND_MODE
);
2913 mpfr_div (frac
, absv
, pow2
, GFC_RND_MODE
);
2915 mpfr_mul_2exp (result
->value
.real
, frac
, (unsigned long)p
, GFC_RND_MODE
);
2922 return range_check (result
, "RRSPACING");
2927 gfc_simplify_scale (gfc_expr
* x
, gfc_expr
* i
)
2929 int k
, neg_flag
, power
, exp_range
;
2930 mpfr_t scale
, radix
;
2933 if (x
->expr_type
!= EXPR_CONSTANT
|| i
->expr_type
!= EXPR_CONSTANT
)
2936 result
= gfc_constant_result (BT_REAL
, x
->ts
.kind
, &x
->where
);
2938 if (mpfr_sgn (x
->value
.real
) == 0)
2940 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
2944 k
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, false);
2946 exp_range
= gfc_real_kinds
[k
].max_exponent
- gfc_real_kinds
[k
].min_exponent
;
2948 /* This check filters out values of i that would overflow an int. */
2949 if (mpz_cmp_si (i
->value
.integer
, exp_range
+ 2) > 0
2950 || mpz_cmp_si (i
->value
.integer
, -exp_range
- 2) < 0)
2952 gfc_error ("Result of SCALE overflows its kind at %L", &result
->where
);
2953 return &gfc_bad_expr
;
2956 /* Compute scale = radix ** power. */
2957 power
= mpz_get_si (i
->value
.integer
);
2967 gfc_set_model_kind (x
->ts
.kind
);
2970 mpfr_set_ui (radix
, gfc_real_kinds
[k
].radix
, GFC_RND_MODE
);
2971 mpfr_pow_ui (scale
, radix
, power
, GFC_RND_MODE
);
2974 mpfr_div (result
->value
.real
, x
->value
.real
, scale
, GFC_RND_MODE
);
2976 mpfr_mul (result
->value
.real
, x
->value
.real
, scale
, GFC_RND_MODE
);
2981 return range_check (result
, "SCALE");
2986 gfc_simplify_scan (gfc_expr
* e
, gfc_expr
* c
, gfc_expr
* b
)
2991 size_t indx
, len
, lenc
;
2993 if (e
->expr_type
!= EXPR_CONSTANT
|| c
->expr_type
!= EXPR_CONSTANT
)
2996 if (b
!= NULL
&& b
->value
.logical
!= 0)
3001 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
3004 len
= e
->value
.character
.length
;
3005 lenc
= c
->value
.character
.length
;
3007 if (len
== 0 || lenc
== 0)
3016 strcspn (e
->value
.character
.string
, c
->value
.character
.string
) + 1;
3023 for (indx
= len
; indx
> 0; indx
--)
3025 for (i
= 0; i
< lenc
; i
++)
3027 if (c
->value
.character
.string
[i
]
3028 == e
->value
.character
.string
[indx
- 1])
3036 mpz_set_ui (result
->value
.integer
, indx
);
3037 return range_check (result
, "SCAN");
3042 gfc_simplify_selected_int_kind (gfc_expr
* e
)
3047 if (e
->expr_type
!= EXPR_CONSTANT
|| gfc_extract_int (e
, &range
) != NULL
)
3052 for (i
= 0; gfc_integer_kinds
[i
].kind
!= 0; i
++)
3053 if (gfc_integer_kinds
[i
].range
>= range
3054 && gfc_integer_kinds
[i
].kind
< kind
)
3055 kind
= gfc_integer_kinds
[i
].kind
;
3057 if (kind
== INT_MAX
)
3060 result
= gfc_int_expr (kind
);
3061 result
->where
= e
->where
;
3068 gfc_simplify_selected_real_kind (gfc_expr
* p
, gfc_expr
* q
)
3070 int range
, precision
, i
, kind
, found_precision
, found_range
;
3077 if (p
->expr_type
!= EXPR_CONSTANT
3078 || gfc_extract_int (p
, &precision
) != NULL
)
3086 if (q
->expr_type
!= EXPR_CONSTANT
3087 || gfc_extract_int (q
, &range
) != NULL
)
3092 found_precision
= 0;
3095 for (i
= 0; gfc_real_kinds
[i
].kind
!= 0; i
++)
3097 if (gfc_real_kinds
[i
].precision
>= precision
)
3098 found_precision
= 1;
3100 if (gfc_real_kinds
[i
].range
>= range
)
3103 if (gfc_real_kinds
[i
].precision
>= precision
3104 && gfc_real_kinds
[i
].range
>= range
&& gfc_real_kinds
[i
].kind
< kind
)
3105 kind
= gfc_real_kinds
[i
].kind
;
3108 if (kind
== INT_MAX
)
3112 if (!found_precision
)
3118 result
= gfc_int_expr (kind
);
3119 result
->where
= (p
!= NULL
) ? p
->where
: q
->where
;
3126 gfc_simplify_set_exponent (gfc_expr
* x
, gfc_expr
* i
)
3129 mpfr_t exp
, absv
, log2
, pow2
, frac
;
3132 if (x
->expr_type
!= EXPR_CONSTANT
|| i
->expr_type
!= EXPR_CONSTANT
)
3135 result
= gfc_constant_result (BT_REAL
, x
->ts
.kind
, &x
->where
);
3137 gfc_set_model_kind (x
->ts
.kind
);
3139 if (mpfr_sgn (x
->value
.real
) == 0)
3141 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
3151 mpfr_abs (absv
, x
->value
.real
, GFC_RND_MODE
);
3152 mpfr_log2 (log2
, absv
, GFC_RND_MODE
);
3154 mpfr_trunc (log2
, log2
);
3155 mpfr_add_ui (exp
, log2
, 1, GFC_RND_MODE
);
3157 /* Old exponent value, and fraction. */
3158 mpfr_ui_pow (pow2
, 2, exp
, GFC_RND_MODE
);
3160 mpfr_div (frac
, absv
, pow2
, GFC_RND_MODE
);
3163 exp2
= (unsigned long) mpz_get_d (i
->value
.integer
);
3164 mpfr_mul_2exp (result
->value
.real
, frac
, exp2
, GFC_RND_MODE
);
3171 return range_check (result
, "SET_EXPONENT");
3176 gfc_simplify_shape (gfc_expr
* source
)
3178 mpz_t shape
[GFC_MAX_DIMENSIONS
];
3179 gfc_expr
*result
, *e
, *f
;
3184 if (source
->rank
== 0 || source
->expr_type
!= EXPR_VARIABLE
)
3187 result
= gfc_start_constructor (BT_INTEGER
, gfc_default_integer_kind
,
3190 ar
= gfc_find_array_ref (source
);
3192 t
= gfc_array_ref_shape (ar
, shape
);
3194 for (n
= 0; n
< source
->rank
; n
++)
3196 e
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
3201 mpz_set (e
->value
.integer
, shape
[n
]);
3202 mpz_clear (shape
[n
]);
3206 mpz_set_ui (e
->value
.integer
, n
+ 1);
3208 f
= gfc_simplify_size (source
, e
);
3212 gfc_free_expr (result
);
3221 gfc_append_constructor (result
, e
);
3229 gfc_simplify_size (gfc_expr
* array
, gfc_expr
* dim
)
3237 if (gfc_array_size (array
, &size
) == FAILURE
)
3242 if (dim
->expr_type
!= EXPR_CONSTANT
)
3245 d
= mpz_get_ui (dim
->value
.integer
) - 1;
3246 if (gfc_array_dimen_size (array
, d
, &size
) == FAILURE
)
3250 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
3253 mpz_set (result
->value
.integer
, size
);
3260 gfc_simplify_sign (gfc_expr
* x
, gfc_expr
* y
)
3264 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
3267 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3272 mpz_abs (result
->value
.integer
, x
->value
.integer
);
3273 if (mpz_sgn (y
->value
.integer
) < 0)
3274 mpz_neg (result
->value
.integer
, result
->value
.integer
);
3279 /* TODO: Handle -0.0 and +0.0 correctly on machines that support
3281 mpfr_abs (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
3282 if (mpfr_sgn (y
->value
.real
) < 0)
3283 mpfr_neg (result
->value
.real
, result
->value
.real
, GFC_RND_MODE
);
3288 gfc_internal_error ("Bad type in gfc_simplify_sign");
3296 gfc_simplify_sin (gfc_expr
* x
)
3301 if (x
->expr_type
!= EXPR_CONSTANT
)
3304 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3309 mpfr_sin (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
3313 gfc_set_model (x
->value
.real
);
3317 mpfr_sin (xp
, x
->value
.complex.r
, GFC_RND_MODE
);
3318 mpfr_cosh (xq
, x
->value
.complex.i
, GFC_RND_MODE
);
3319 mpfr_mul (result
->value
.complex.r
, xp
, xq
, GFC_RND_MODE
);
3321 mpfr_cos (xp
, x
->value
.complex.r
, GFC_RND_MODE
);
3322 mpfr_sinh (xq
, x
->value
.complex.i
, GFC_RND_MODE
);
3323 mpfr_mul (result
->value
.complex.i
, xp
, xq
, GFC_RND_MODE
);
3330 gfc_internal_error ("in gfc_simplify_sin(): Bad type");
3333 return range_check (result
, "SIN");
3338 gfc_simplify_sinh (gfc_expr
* x
)
3342 if (x
->expr_type
!= EXPR_CONSTANT
)
3345 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3347 mpfr_sinh(result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
3349 return range_check (result
, "SINH");
3353 /* The argument is always a double precision real that is converted to
3354 single precision. TODO: Rounding! */
3357 gfc_simplify_sngl (gfc_expr
* a
)
3361 if (a
->expr_type
!= EXPR_CONSTANT
)
3364 result
= gfc_real2real (a
, gfc_default_real_kind
);
3365 return range_check (result
, "SNGL");
3370 gfc_simplify_spacing (gfc_expr
* x
)
3377 if (x
->expr_type
!= EXPR_CONSTANT
)
3380 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
3382 p
= gfc_real_kinds
[i
].digits
;
3384 result
= gfc_constant_result (BT_REAL
, x
->ts
.kind
, &x
->where
);
3386 gfc_set_model_kind (x
->ts
.kind
);
3388 if (mpfr_sgn (x
->value
.real
) == 0)
3390 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].tiny
, GFC_RND_MODE
);
3397 mpfr_abs (absv
, x
->value
.real
, GFC_RND_MODE
);
3398 mpfr_log2 (log2
, absv
, GFC_RND_MODE
);
3399 mpfr_trunc (log2
, log2
);
3401 mpfr_add_ui (log2
, log2
, 1, GFC_RND_MODE
);
3403 /* FIXME: We should be using mpfr_get_si here, but this function is
3404 not available with the version of mpfr distributed with gmp (as of
3405 2004-09-17). Replace once mpfr has been imported into the gcc cvs
3407 diff
= (long)mpfr_get_d (log2
, GFC_RND_MODE
) - (long)p
;
3408 mpfr_set_ui (result
->value
.real
, 1, GFC_RND_MODE
);
3409 mpfr_mul_2si (result
->value
.real
, result
->value
.real
, diff
, GFC_RND_MODE
);
3414 if (mpfr_cmp (result
->value
.real
, gfc_real_kinds
[i
].tiny
) < 0)
3415 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].tiny
, GFC_RND_MODE
);
3417 return range_check (result
, "SPACING");
3422 gfc_simplify_sqrt (gfc_expr
* e
)
3425 mpfr_t ac
, ad
, s
, t
, w
;
3427 if (e
->expr_type
!= EXPR_CONSTANT
)
3430 result
= gfc_constant_result (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
3435 if (mpfr_cmp_si (e
->value
.real
, 0) < 0)
3437 mpfr_sqrt (result
->value
.real
, e
->value
.real
, GFC_RND_MODE
);
3442 /* Formula taken from Numerical Recipes to avoid over- and
3445 gfc_set_model (e
->value
.real
);
3452 if (mpfr_cmp_ui (e
->value
.complex.r
, 0) == 0
3453 && mpfr_cmp_ui (e
->value
.complex.i
, 0) == 0)
3456 mpfr_set_ui (result
->value
.complex.r
, 0, GFC_RND_MODE
);
3457 mpfr_set_ui (result
->value
.complex.i
, 0, GFC_RND_MODE
);
3461 mpfr_abs (ac
, e
->value
.complex.r
, GFC_RND_MODE
);
3462 mpfr_abs (ad
, e
->value
.complex.i
, GFC_RND_MODE
);
3464 if (mpfr_cmp (ac
, ad
) >= 0)
3466 mpfr_div (t
, e
->value
.complex.i
, e
->value
.complex.r
, GFC_RND_MODE
);
3467 mpfr_mul (t
, t
, t
, GFC_RND_MODE
);
3468 mpfr_add_ui (t
, t
, 1, GFC_RND_MODE
);
3469 mpfr_sqrt (t
, t
, GFC_RND_MODE
);
3470 mpfr_add_ui (t
, t
, 1, GFC_RND_MODE
);
3471 mpfr_div_ui (t
, t
, 2, GFC_RND_MODE
);
3472 mpfr_sqrt (t
, t
, GFC_RND_MODE
);
3473 mpfr_sqrt (s
, ac
, GFC_RND_MODE
);
3474 mpfr_mul (w
, s
, t
, GFC_RND_MODE
);
3478 mpfr_div (s
, e
->value
.complex.r
, e
->value
.complex.i
, GFC_RND_MODE
);
3479 mpfr_mul (t
, s
, s
, GFC_RND_MODE
);
3480 mpfr_add_ui (t
, t
, 1, GFC_RND_MODE
);
3481 mpfr_sqrt (t
, t
, GFC_RND_MODE
);
3482 mpfr_abs (s
, s
, GFC_RND_MODE
);
3483 mpfr_add (t
, t
, s
, GFC_RND_MODE
);
3484 mpfr_div_ui (t
, t
, 2, GFC_RND_MODE
);
3485 mpfr_sqrt (t
, t
, GFC_RND_MODE
);
3486 mpfr_sqrt (s
, ad
, GFC_RND_MODE
);
3487 mpfr_mul (w
, s
, t
, GFC_RND_MODE
);
3490 if (mpfr_cmp_ui (w
, 0) != 0 && mpfr_cmp_ui (e
->value
.complex.r
, 0) >= 0)
3492 mpfr_mul_ui (t
, w
, 2, GFC_RND_MODE
);
3493 mpfr_div (result
->value
.complex.i
, e
->value
.complex.i
, t
, GFC_RND_MODE
);
3494 mpfr_set (result
->value
.complex.r
, w
, GFC_RND_MODE
);
3496 else if (mpfr_cmp_ui (w
, 0) != 0
3497 && mpfr_cmp_ui (e
->value
.complex.r
, 0) < 0
3498 && mpfr_cmp_ui (e
->value
.complex.i
, 0) >= 0)
3500 mpfr_mul_ui (t
, w
, 2, GFC_RND_MODE
);
3501 mpfr_div (result
->value
.complex.r
, e
->value
.complex.i
, t
, GFC_RND_MODE
);
3502 mpfr_set (result
->value
.complex.i
, w
, GFC_RND_MODE
);
3504 else if (mpfr_cmp_ui (w
, 0) != 0
3505 && mpfr_cmp_ui (e
->value
.complex.r
, 0) < 0
3506 && mpfr_cmp_ui (e
->value
.complex.i
, 0) < 0)
3508 mpfr_mul_ui (t
, w
, 2, GFC_RND_MODE
);
3509 mpfr_div (result
->value
.complex.r
, ad
, t
, GFC_RND_MODE
);
3510 mpfr_neg (w
, w
, GFC_RND_MODE
);
3511 mpfr_set (result
->value
.complex.i
, w
, GFC_RND_MODE
);
3514 gfc_internal_error ("invalid complex argument of SQRT at %L",
3526 gfc_internal_error ("invalid argument of SQRT at %L", &e
->where
);
3529 return range_check (result
, "SQRT");
3532 gfc_free_expr (result
);
3533 gfc_error ("Argument of SQRT at %L has a negative value", &e
->where
);
3534 return &gfc_bad_expr
;
3539 gfc_simplify_tan (gfc_expr
* x
)
3544 if (x
->expr_type
!= EXPR_CONSTANT
)
3547 i
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, false);
3549 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3551 mpfr_tan (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
3553 return range_check (result
, "TAN");
3558 gfc_simplify_tanh (gfc_expr
* x
)
3562 if (x
->expr_type
!= EXPR_CONSTANT
)
3565 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3567 mpfr_tanh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
3569 return range_check (result
, "TANH");
3575 gfc_simplify_tiny (gfc_expr
* e
)
3580 i
= gfc_validate_kind (BT_REAL
, e
->ts
.kind
, false);
3582 result
= gfc_constant_result (BT_REAL
, e
->ts
.kind
, &e
->where
);
3583 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].tiny
, GFC_RND_MODE
);
3590 gfc_simplify_trim (gfc_expr
* e
)
3593 int count
, i
, len
, lentrim
;
3595 if (e
->expr_type
!= EXPR_CONSTANT
)
3598 len
= e
->value
.character
.length
;
3600 result
= gfc_constant_result (BT_CHARACTER
, e
->ts
.kind
, &e
->where
);
3602 for (count
= 0, i
= 1; i
<= len
; ++i
)
3604 if (e
->value
.character
.string
[len
- i
] == ' ')
3610 lentrim
= len
- count
;
3612 result
->value
.character
.length
= lentrim
;
3613 result
->value
.character
.string
= gfc_getmem (lentrim
+ 1);
3615 for (i
= 0; i
< lentrim
; i
++)
3616 result
->value
.character
.string
[i
] = e
->value
.character
.string
[i
];
3618 result
->value
.character
.string
[lentrim
] = '\0'; /* For debugger */
3625 gfc_simplify_ubound (gfc_expr
* array
, gfc_expr
* dim
)
3627 return simplify_bound (array
, dim
, 1);
3632 gfc_simplify_verify (gfc_expr
* s
, gfc_expr
* set
, gfc_expr
* b
)
3636 size_t index
, len
, lenset
;
3639 if (s
->expr_type
!= EXPR_CONSTANT
|| set
->expr_type
!= EXPR_CONSTANT
)
3642 if (b
!= NULL
&& b
->value
.logical
!= 0)
3647 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
3650 len
= s
->value
.character
.length
;
3651 lenset
= set
->value
.character
.length
;
3655 mpz_set_ui (result
->value
.integer
, 0);
3663 mpz_set_ui (result
->value
.integer
, len
);
3668 strspn (s
->value
.character
.string
, set
->value
.character
.string
) + 1;
3677 mpz_set_ui (result
->value
.integer
, 1);
3680 for (index
= len
; index
> 0; index
--)
3682 for (i
= 0; i
< lenset
; i
++)
3684 if (s
->value
.character
.string
[index
- 1]
3685 == set
->value
.character
.string
[i
])
3693 mpz_set_ui (result
->value
.integer
, index
);
3697 /****************** Constant simplification *****************/
3699 /* Master function to convert one constant to another. While this is
3700 used as a simplification function, it requires the destination type
3701 and kind information which is supplied by a special case in
3705 gfc_convert_constant (gfc_expr
* e
, bt type
, int kind
)
3707 gfc_expr
*g
, *result
, *(*f
) (gfc_expr
*, int);
3708 gfc_constructor
*head
, *c
, *tail
= NULL
;
3722 f
= gfc_int2complex
;
3739 f
= gfc_real2complex
;
3750 f
= gfc_complex2int
;
3753 f
= gfc_complex2real
;
3756 f
= gfc_complex2complex
;
3765 if (type
!= BT_LOGICAL
)
3772 gfc_internal_error ("gfc_convert_constant(): Unexpected type");
3777 switch (e
->expr_type
)
3780 result
= f (e
, kind
);
3782 return &gfc_bad_expr
;
3786 if (!gfc_is_constant_expr (e
))
3791 for (c
= e
->value
.constructor
; c
; c
= c
->next
)
3794 head
= tail
= gfc_get_constructor ();
3797 tail
->next
= gfc_get_constructor ();
3801 tail
->where
= c
->where
;
3803 if (c
->iterator
== NULL
)
3804 tail
->expr
= f (c
->expr
, kind
);
3807 g
= gfc_convert_constant (c
->expr
, type
, kind
);
3808 if (g
== &gfc_bad_expr
)
3813 if (tail
->expr
== NULL
)
3815 gfc_free_constructor (head
);
3820 result
= gfc_get_expr ();
3821 result
->ts
.type
= type
;
3822 result
->ts
.kind
= kind
;
3823 result
->expr_type
= EXPR_ARRAY
;
3824 result
->value
.constructor
= head
;
3825 result
->shape
= gfc_copy_shape (e
->shape
, e
->rank
);
3826 result
->where
= e
->where
;
3827 result
->rank
= e
->rank
;
3838 /****************** Helper functions ***********************/
3840 /* Given a collating table, create the inverse table. */
3843 invert_table (const int *table
, int *xtable
)
3847 for (i
= 0; i
< 256; i
++)
3850 for (i
= 0; i
< 256; i
++)
3851 xtable
[table
[i
]] = i
;
3856 gfc_simplify_init_1 (void)
3859 invert_table (ascii_table
, xascii_table
);