1 /* Simplify intrinsic functions at compile-time.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004 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
31 #include "intrinsic.h"
33 gfc_expr gfc_bad_expr
;
36 /* Note that 'simplification' is not just transforming expressions.
37 For functions that are not simplified at compile time, range
38 checking is done if possible.
40 The return convention is that each simplification function returns:
42 A new expression node corresponding to the simplified arguments.
43 The original arguments are destroyed by the caller, and must not
44 be a part of the new expression.
46 NULL pointer indicating that no simplification was possible and
47 the original expression should remain intact. If the
48 simplification function sets the type and/or the function name
49 via the pointer gfc_simple_expression, then this type is
52 An expression pointer to gfc_bad_expr (a static placeholder)
53 indicating that some error has prevented simplification. For
54 example, sqrt(-1.0). The error is generated within the function
55 and should be propagated upwards
57 By the time a simplification function gets control, it has been
58 decided that the function call is really supposed to be the
59 intrinsic. No type checking is strictly necessary, since only
60 valid types will be passed on. On the other hand, a simplification
61 subroutine may have to look at the type of an argument as part of
64 Array arguments are never passed to these subroutines.
66 The functions in this file don't have much comment with them, but
67 everything is reasonably straight-forward. The Standard, chapter 13
68 is the best comment you'll find for this file anyway. */
70 /* Static table for converting non-ascii character sets to ascii.
71 The xascii_table[] is the inverse table. */
73 static int ascii_table
[256] = {
74 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
75 '\b', '\t', '\n', '\v', '\0', '\r', '\0', '\0',
76 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
77 '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
78 ' ', '!', '\'', '#', '$', '%', '&', '\'',
79 '(', ')', '*', '+', ',', '-', '.', '/',
80 '0', '1', '2', '3', '4', '5', '6', '7',
81 '8', '9', ':', ';', '<', '=', '>', '?',
82 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
83 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
84 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
85 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
86 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
87 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
88 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
89 'x', 'y', 'z', '{', '|', '}', '~', '\?'
92 static int xascii_table
[256];
95 /* Range checks an expression node. If all goes well, returns the
96 node, otherwise returns &gfc_bad_expr and frees the node. */
99 range_check (gfc_expr
* result
, const char *name
)
101 if (gfc_range_check (result
) == ARITH_OK
)
104 gfc_error ("Result of %s overflows its kind at %L", name
, &result
->where
);
105 gfc_free_expr (result
);
106 return &gfc_bad_expr
;
110 /* A helper function that gets an optional and possibly missing
111 kind parameter. Returns the kind, -1 if something went wrong. */
114 get_kind (bt type
, gfc_expr
* k
, const char *name
, int default_kind
)
121 if (k
->expr_type
!= EXPR_CONSTANT
)
123 gfc_error ("KIND parameter of %s at %L must be an initialization "
124 "expression", name
, &k
->where
);
129 if (gfc_extract_int (k
, &kind
) != NULL
130 || gfc_validate_kind (type
, kind
, true) < 0)
133 gfc_error ("Invalid KIND parameter of %s at %L", name
, &k
->where
);
141 /* Checks if X, which is assumed to represent a two's complement
142 integer of binary width BITSIZE, has the signbit set. If so, makes
143 X the corresponding negative number. */
146 twos_complement (mpz_t x
, int bitsize
)
150 if (mpz_tstbit (x
, bitsize
- 1) == 1)
152 mpz_init_set_ui(mask
, 1);
153 mpz_mul_2exp(mask
, mask
, bitsize
);
154 mpz_sub_ui(mask
, mask
, 1);
156 /* We negate the number by hand, zeroing the high bits, that is
157 make it the corresponding positive number, and then have it
158 negated by GMP, giving the correct representation of the
161 mpz_add_ui (x
, x
, 1);
162 mpz_and (x
, x
, mask
);
171 /********************** Simplification functions *****************************/
174 gfc_simplify_abs (gfc_expr
* e
)
178 if (e
->expr_type
!= EXPR_CONSTANT
)
184 result
= gfc_constant_result (BT_INTEGER
, e
->ts
.kind
, &e
->where
);
186 mpz_abs (result
->value
.integer
, e
->value
.integer
);
188 result
= range_check (result
, "IABS");
192 result
= gfc_constant_result (BT_REAL
, e
->ts
.kind
, &e
->where
);
194 mpfr_abs (result
->value
.real
, e
->value
.real
, GFC_RND_MODE
);
196 result
= range_check (result
, "ABS");
200 result
= gfc_constant_result (BT_REAL
, e
->ts
.kind
, &e
->where
);
202 gfc_set_model_kind (e
->ts
.kind
);
204 mpfr_hypot (result
->value
.real
, e
->value
.complex.r
,
205 e
->value
.complex.i
, GFC_RND_MODE
);
206 result
= range_check (result
, "CABS");
210 gfc_internal_error ("gfc_simplify_abs(): Bad type");
218 gfc_simplify_achar (gfc_expr
* e
)
223 if (e
->expr_type
!= EXPR_CONSTANT
)
226 /* We cannot assume that the native character set is ASCII in this
228 if (gfc_extract_int (e
, &index
) != NULL
|| index
< 0 || index
> 127)
230 gfc_error ("Extended ASCII not implemented: argument of ACHAR at %L "
231 "must be between 0 and 127", &e
->where
);
232 return &gfc_bad_expr
;
235 result
= gfc_constant_result (BT_CHARACTER
, gfc_default_character_kind
,
238 result
->value
.character
.string
= gfc_getmem (2);
240 result
->value
.character
.length
= 1;
241 result
->value
.character
.string
[0] = ascii_table
[index
];
242 result
->value
.character
.string
[1] = '\0'; /* For debugger */
248 gfc_simplify_acos (gfc_expr
* x
)
252 if (x
->expr_type
!= EXPR_CONSTANT
)
255 if (mpfr_cmp_si (x
->value
.real
, 1) > 0 || mpfr_cmp_si (x
->value
.real
, -1) < 0)
257 gfc_error ("Argument of ACOS at %L must be between -1 and 1",
259 return &gfc_bad_expr
;
262 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
264 mpfr_acos (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
266 return range_check (result
, "ACOS");
271 gfc_simplify_adjustl (gfc_expr
* e
)
277 if (e
->expr_type
!= EXPR_CONSTANT
)
280 len
= e
->value
.character
.length
;
282 result
= gfc_constant_result (BT_CHARACTER
, e
->ts
.kind
, &e
->where
);
284 result
->value
.character
.length
= len
;
285 result
->value
.character
.string
= gfc_getmem (len
+ 1);
287 for (count
= 0, i
= 0; i
< len
; ++i
)
289 ch
= e
->value
.character
.string
[i
];
295 for (i
= 0; i
< len
- count
; ++i
)
297 result
->value
.character
.string
[i
] =
298 e
->value
.character
.string
[count
+ i
];
301 for (i
= len
- count
; i
< len
; ++i
)
303 result
->value
.character
.string
[i
] = ' ';
306 result
->value
.character
.string
[len
] = '\0'; /* For debugger */
313 gfc_simplify_adjustr (gfc_expr
* e
)
319 if (e
->expr_type
!= EXPR_CONSTANT
)
322 len
= e
->value
.character
.length
;
324 result
= gfc_constant_result (BT_CHARACTER
, e
->ts
.kind
, &e
->where
);
326 result
->value
.character
.length
= len
;
327 result
->value
.character
.string
= gfc_getmem (len
+ 1);
329 for (count
= 0, i
= len
- 1; i
>= 0; --i
)
331 ch
= e
->value
.character
.string
[i
];
337 for (i
= 0; i
< count
; ++i
)
339 result
->value
.character
.string
[i
] = ' ';
342 for (i
= count
; i
< len
; ++i
)
344 result
->value
.character
.string
[i
] =
345 e
->value
.character
.string
[i
- count
];
348 result
->value
.character
.string
[len
] = '\0'; /* For debugger */
355 gfc_simplify_aimag (gfc_expr
* e
)
359 if (e
->expr_type
!= EXPR_CONSTANT
)
362 result
= gfc_constant_result (BT_REAL
, e
->ts
.kind
, &e
->where
);
363 mpfr_set (result
->value
.real
, e
->value
.complex.i
, GFC_RND_MODE
);
365 return range_check (result
, "AIMAG");
370 gfc_simplify_aint (gfc_expr
* e
, gfc_expr
* k
)
372 gfc_expr
*rtrunc
, *result
;
375 kind
= get_kind (BT_REAL
, k
, "AINT", e
->ts
.kind
);
377 return &gfc_bad_expr
;
379 if (e
->expr_type
!= EXPR_CONSTANT
)
382 rtrunc
= gfc_copy_expr (e
);
384 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
386 result
= gfc_real2real (rtrunc
, kind
);
387 gfc_free_expr (rtrunc
);
389 return range_check (result
, "AINT");
394 gfc_simplify_dint (gfc_expr
* e
)
396 gfc_expr
*rtrunc
, *result
;
398 if (e
->expr_type
!= EXPR_CONSTANT
)
401 rtrunc
= gfc_copy_expr (e
);
403 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
405 result
= gfc_real2real (rtrunc
, gfc_default_double_kind
);
406 gfc_free_expr (rtrunc
);
408 return range_check (result
, "DINT");
413 gfc_simplify_anint (gfc_expr
* e
, gfc_expr
* k
)
415 gfc_expr
*rtrunc
, *result
;
419 kind
= get_kind (BT_REAL
, k
, "ANINT", e
->ts
.kind
);
421 return &gfc_bad_expr
;
423 if (e
->expr_type
!= EXPR_CONSTANT
)
426 result
= gfc_constant_result (e
->ts
.type
, kind
, &e
->where
);
428 rtrunc
= gfc_copy_expr (e
);
430 cmp
= mpfr_cmp_ui (e
->value
.real
, 0);
432 gfc_set_model_kind (kind
);
434 mpfr_set_str (half
, "0.5", 10, GFC_RND_MODE
);
438 mpfr_add (rtrunc
->value
.real
, e
->value
.real
, half
, GFC_RND_MODE
);
439 mpfr_trunc (result
->value
.real
, rtrunc
->value
.real
);
443 mpfr_sub (rtrunc
->value
.real
, e
->value
.real
, half
, GFC_RND_MODE
);
444 mpfr_trunc (result
->value
.real
, rtrunc
->value
.real
);
447 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
449 gfc_free_expr (rtrunc
);
452 return range_check (result
, "ANINT");
457 gfc_simplify_dnint (gfc_expr
* e
)
459 gfc_expr
*rtrunc
, *result
;
463 if (e
->expr_type
!= EXPR_CONSTANT
)
467 gfc_constant_result (BT_REAL
, gfc_default_double_kind
, &e
->where
);
469 rtrunc
= gfc_copy_expr (e
);
471 cmp
= mpfr_cmp_ui (e
->value
.real
, 0);
473 gfc_set_model_kind (gfc_default_double_kind
);
475 mpfr_set_str (half
, "0.5", 10, GFC_RND_MODE
);
479 mpfr_add (rtrunc
->value
.real
, e
->value
.real
, half
, GFC_RND_MODE
);
480 mpfr_trunc (result
->value
.real
, rtrunc
->value
.real
);
484 mpfr_sub (rtrunc
->value
.real
, e
->value
.real
, half
, GFC_RND_MODE
);
485 mpfr_trunc (result
->value
.real
, rtrunc
->value
.real
);
488 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
490 gfc_free_expr (rtrunc
);
493 return range_check (result
, "DNINT");
498 gfc_simplify_asin (gfc_expr
* x
)
502 if (x
->expr_type
!= EXPR_CONSTANT
)
505 if (mpfr_cmp_si (x
->value
.real
, 1) > 0 || mpfr_cmp_si (x
->value
.real
, -1) < 0)
507 gfc_error ("Argument of ASIN at %L must be between -1 and 1",
509 return &gfc_bad_expr
;
512 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
514 mpfr_asin(result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
516 return range_check (result
, "ASIN");
521 gfc_simplify_atan (gfc_expr
* x
)
525 if (x
->expr_type
!= EXPR_CONSTANT
)
528 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
530 mpfr_atan(result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
532 return range_check (result
, "ATAN");
538 gfc_simplify_atan2 (gfc_expr
* y
, gfc_expr
* x
)
542 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
545 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
547 if (mpfr_sgn (y
->value
.real
) == 0 && mpfr_sgn (x
->value
.real
) == 0)
550 ("If first argument of ATAN2 %L is zero, then the second argument "
551 "must not be zero", &x
->where
);
552 gfc_free_expr (result
);
553 return &gfc_bad_expr
;
556 arctangent2 (y
->value
.real
, x
->value
.real
, result
->value
.real
);
558 return range_check (result
, "ATAN2");
564 gfc_simplify_bit_size (gfc_expr
* e
)
569 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
570 result
= gfc_constant_result (BT_INTEGER
, e
->ts
.kind
, &e
->where
);
571 mpz_set_ui (result
->value
.integer
, gfc_integer_kinds
[i
].bit_size
);
578 gfc_simplify_btest (gfc_expr
* e
, gfc_expr
* bit
)
582 if (e
->expr_type
!= EXPR_CONSTANT
|| bit
->expr_type
!= EXPR_CONSTANT
)
585 if (gfc_extract_int (bit
, &b
) != NULL
|| b
< 0)
586 return gfc_logical_expr (0, &e
->where
);
588 return gfc_logical_expr (mpz_tstbit (e
->value
.integer
, b
), &e
->where
);
593 gfc_simplify_ceiling (gfc_expr
* e
, gfc_expr
* k
)
595 gfc_expr
*ceil
, *result
;
598 kind
= get_kind (BT_REAL
, k
, "CEILING", gfc_default_real_kind
);
600 return &gfc_bad_expr
;
602 if (e
->expr_type
!= EXPR_CONSTANT
)
605 result
= gfc_constant_result (BT_INTEGER
, kind
, &e
->where
);
607 ceil
= gfc_copy_expr (e
);
609 mpfr_ceil (ceil
->value
.real
, e
->value
.real
);
610 gfc_mpfr_to_mpz(result
->value
.integer
, ceil
->value
.real
);
612 gfc_free_expr (ceil
);
614 return range_check (result
, "CEILING");
619 gfc_simplify_char (gfc_expr
* e
, gfc_expr
* k
)
624 kind
= get_kind (BT_CHARACTER
, k
, "CHAR", gfc_default_character_kind
);
626 return &gfc_bad_expr
;
628 if (e
->expr_type
!= EXPR_CONSTANT
)
631 if (gfc_extract_int (e
, &c
) != NULL
|| c
< 0 || c
> 255)
633 gfc_error ("Bad character in CHAR function at %L", &e
->where
);
634 return &gfc_bad_expr
;
637 result
= gfc_constant_result (BT_CHARACTER
, kind
, &e
->where
);
639 result
->value
.character
.length
= 1;
640 result
->value
.character
.string
= gfc_getmem (2);
642 result
->value
.character
.string
[0] = c
;
643 result
->value
.character
.string
[1] = '\0'; /* For debugger */
649 /* Common subroutine for simplifying CMPLX and DCMPLX. */
652 simplify_cmplx (const char *name
, gfc_expr
* x
, gfc_expr
* y
, int kind
)
656 result
= gfc_constant_result (BT_COMPLEX
, kind
, &x
->where
);
658 mpfr_set_ui (result
->value
.complex.i
, 0, GFC_RND_MODE
);
663 mpfr_set_z (result
->value
.complex.r
, x
->value
.integer
, GFC_RND_MODE
);
667 mpfr_set (result
->value
.complex.r
, x
->value
.real
, GFC_RND_MODE
);
671 mpfr_set (result
->value
.complex.r
, x
->value
.complex.r
, GFC_RND_MODE
);
672 mpfr_set (result
->value
.complex.i
, x
->value
.complex.i
, GFC_RND_MODE
);
676 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (x)");
684 mpfr_set_z (result
->value
.complex.i
, y
->value
.integer
, GFC_RND_MODE
);
688 mpfr_set (result
->value
.complex.i
, y
->value
.real
, GFC_RND_MODE
);
692 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (y)");
696 return range_check (result
, name
);
701 gfc_simplify_cmplx (gfc_expr
* x
, gfc_expr
* y
, gfc_expr
* k
)
705 if (x
->expr_type
!= EXPR_CONSTANT
706 || (y
!= NULL
&& y
->expr_type
!= EXPR_CONSTANT
))
709 kind
= get_kind (BT_REAL
, k
, "CMPLX", gfc_default_real_kind
);
711 return &gfc_bad_expr
;
713 return simplify_cmplx ("CMPLX", x
, y
, kind
);
718 gfc_simplify_conjg (gfc_expr
* e
)
722 if (e
->expr_type
!= EXPR_CONSTANT
)
725 result
= gfc_copy_expr (e
);
726 mpfr_neg (result
->value
.complex.i
, result
->value
.complex.i
, GFC_RND_MODE
);
728 return range_check (result
, "CONJG");
733 gfc_simplify_cos (gfc_expr
* x
)
738 if (x
->expr_type
!= EXPR_CONSTANT
)
741 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
746 mpfr_cos (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
749 gfc_set_model_kind (x
->ts
.kind
);
753 mpfr_cos (xp
, x
->value
.complex.r
, GFC_RND_MODE
);
754 mpfr_cosh (xq
, x
->value
.complex.i
, GFC_RND_MODE
);
755 mpfr_mul(result
->value
.complex.r
, xp
, xq
, GFC_RND_MODE
);
757 mpfr_sin (xp
, x
->value
.complex.r
, GFC_RND_MODE
);
758 mpfr_sinh (xq
, x
->value
.complex.i
, GFC_RND_MODE
);
759 mpfr_mul (xp
, xp
, xq
, GFC_RND_MODE
);
760 mpfr_neg (result
->value
.complex.i
, xp
, GFC_RND_MODE
);
766 gfc_internal_error ("in gfc_simplify_cos(): Bad type");
769 return range_check (result
, "COS");
775 gfc_simplify_cosh (gfc_expr
* x
)
779 if (x
->expr_type
!= EXPR_CONSTANT
)
782 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
784 mpfr_cosh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
786 return range_check (result
, "COSH");
791 gfc_simplify_dcmplx (gfc_expr
* x
, gfc_expr
* y
)
794 if (x
->expr_type
!= EXPR_CONSTANT
795 || (y
!= NULL
&& y
->expr_type
!= EXPR_CONSTANT
))
798 return simplify_cmplx ("DCMPLX", x
, y
, gfc_default_double_kind
);
803 gfc_simplify_dble (gfc_expr
* e
)
807 if (e
->expr_type
!= EXPR_CONSTANT
)
813 result
= gfc_int2real (e
, gfc_default_double_kind
);
817 result
= gfc_real2real (e
, gfc_default_double_kind
);
821 result
= gfc_complex2real (e
, gfc_default_double_kind
);
825 gfc_internal_error ("gfc_simplify_dble(): bad type at %L", &e
->where
);
828 return range_check (result
, "DBLE");
833 gfc_simplify_digits (gfc_expr
* x
)
837 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
841 digits
= gfc_integer_kinds
[i
].digits
;
846 digits
= gfc_real_kinds
[i
].digits
;
853 return gfc_int_expr (digits
);
858 gfc_simplify_dim (gfc_expr
* x
, gfc_expr
* y
)
862 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
865 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
870 if (mpz_cmp (x
->value
.integer
, y
->value
.integer
) > 0)
871 mpz_sub (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
873 mpz_set_ui (result
->value
.integer
, 0);
878 if (mpfr_cmp (x
->value
.real
, y
->value
.real
) > 0)
879 mpfr_sub (result
->value
.real
, x
->value
.real
, y
->value
.real
, GFC_RND_MODE
);
881 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
886 gfc_internal_error ("gfc_simplify_dim(): Bad type");
889 return range_check (result
, "DIM");
894 gfc_simplify_dprod (gfc_expr
* x
, gfc_expr
* y
)
896 gfc_expr
*a1
, *a2
, *result
;
898 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
902 gfc_constant_result (BT_REAL
, gfc_default_double_kind
, &x
->where
);
904 a1
= gfc_real2real (x
, gfc_default_double_kind
);
905 a2
= gfc_real2real (y
, gfc_default_double_kind
);
907 mpfr_mul (result
->value
.real
, a1
->value
.real
, a2
->value
.real
, GFC_RND_MODE
);
912 return range_check (result
, "DPROD");
917 gfc_simplify_epsilon (gfc_expr
* e
)
922 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
924 result
= gfc_constant_result (BT_REAL
, e
->ts
.kind
, &e
->where
);
926 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].epsilon
, GFC_RND_MODE
);
928 return range_check (result
, "EPSILON");
933 gfc_simplify_exp (gfc_expr
* x
)
938 if (x
->expr_type
!= EXPR_CONSTANT
)
941 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
946 mpfr_exp(result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
950 gfc_set_model_kind (x
->ts
.kind
);
953 mpfr_exp (xq
, x
->value
.complex.r
, GFC_RND_MODE
);
954 mpfr_cos (xp
, x
->value
.complex.i
, GFC_RND_MODE
);
955 mpfr_mul (result
->value
.complex.r
, xq
, xp
, GFC_RND_MODE
);
956 mpfr_sin (xp
, x
->value
.complex.i
, GFC_RND_MODE
);
957 mpfr_mul (result
->value
.complex.i
, xq
, xp
, GFC_RND_MODE
);
963 gfc_internal_error ("in gfc_simplify_exp(): Bad type");
966 return range_check (result
, "EXP");
969 /* FIXME: MPFR should be able to do this better */
971 gfc_simplify_exponent (gfc_expr
* x
)
976 if (x
->expr_type
!= EXPR_CONSTANT
)
979 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
982 gfc_set_model (x
->value
.real
);
984 if (mpfr_sgn (x
->value
.real
) == 0)
986 mpz_set_ui (result
->value
.integer
, 0);
992 mpfr_abs (tmp
, x
->value
.real
, GFC_RND_MODE
);
993 mpfr_log2 (tmp
, tmp
, GFC_RND_MODE
);
995 gfc_mpfr_to_mpz (result
->value
.integer
, tmp
);
999 return range_check (result
, "EXPONENT");
1004 gfc_simplify_float (gfc_expr
* a
)
1008 if (a
->expr_type
!= EXPR_CONSTANT
)
1011 result
= gfc_int2real (a
, gfc_default_real_kind
);
1012 return range_check (result
, "FLOAT");
1017 gfc_simplify_floor (gfc_expr
* e
, gfc_expr
* k
)
1023 kind
= get_kind (BT_REAL
, k
, "FLOOR", gfc_default_real_kind
);
1025 gfc_internal_error ("gfc_simplify_floor(): Bad kind");
1027 if (e
->expr_type
!= EXPR_CONSTANT
)
1030 result
= gfc_constant_result (BT_INTEGER
, kind
, &e
->where
);
1032 gfc_set_model_kind (kind
);
1034 mpfr_floor (floor
, e
->value
.real
);
1036 gfc_mpfr_to_mpz (result
->value
.integer
, floor
);
1040 return range_check (result
, "FLOOR");
1045 gfc_simplify_fraction (gfc_expr
* x
)
1048 mpfr_t absv
, exp
, pow2
;
1050 if (x
->expr_type
!= EXPR_CONSTANT
)
1053 result
= gfc_constant_result (BT_REAL
, x
->ts
.kind
, &x
->where
);
1055 gfc_set_model_kind (x
->ts
.kind
);
1057 if (mpfr_sgn (x
->value
.real
) == 0)
1059 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
1067 mpfr_abs (absv
, x
->value
.real
, GFC_RND_MODE
);
1068 mpfr_log2 (exp
, absv
, GFC_RND_MODE
);
1070 mpfr_trunc (exp
, exp
);
1071 mpfr_add_ui (exp
, exp
, 1, GFC_RND_MODE
);
1073 mpfr_ui_pow (pow2
, 2, exp
, GFC_RND_MODE
);
1075 mpfr_div (result
->value
.real
, absv
, pow2
, GFC_RND_MODE
);
1081 return range_check (result
, "FRACTION");
1086 gfc_simplify_huge (gfc_expr
* e
)
1091 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
1093 result
= gfc_constant_result (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
1098 mpz_set (result
->value
.integer
, gfc_integer_kinds
[i
].huge
);
1102 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].huge
, GFC_RND_MODE
);
1114 gfc_simplify_iachar (gfc_expr
* e
)
1119 if (e
->expr_type
!= EXPR_CONSTANT
)
1122 if (e
->value
.character
.length
!= 1)
1124 gfc_error ("Argument of IACHAR at %L must be of length one", &e
->where
);
1125 return &gfc_bad_expr
;
1128 index
= xascii_table
[(int) e
->value
.character
.string
[0] & 0xFF];
1130 result
= gfc_int_expr (index
);
1131 result
->where
= e
->where
;
1133 return range_check (result
, "IACHAR");
1138 gfc_simplify_iand (gfc_expr
* x
, gfc_expr
* y
)
1142 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1145 result
= gfc_constant_result (BT_INTEGER
, x
->ts
.kind
, &x
->where
);
1147 mpz_and (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
1149 return range_check (result
, "IAND");
1154 gfc_simplify_ibclr (gfc_expr
* x
, gfc_expr
* y
)
1159 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1162 if (gfc_extract_int (y
, &pos
) != NULL
|| pos
< 0)
1164 gfc_error ("Invalid second argument of IBCLR at %L", &y
->where
);
1165 return &gfc_bad_expr
;
1168 k
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
1170 if (pos
> gfc_integer_kinds
[k
].bit_size
)
1172 gfc_error ("Second argument of IBCLR exceeds bit size at %L",
1174 return &gfc_bad_expr
;
1177 result
= gfc_copy_expr (x
);
1179 mpz_clrbit (result
->value
.integer
, pos
);
1180 return range_check (result
, "IBCLR");
1185 gfc_simplify_ibits (gfc_expr
* x
, gfc_expr
* y
, gfc_expr
* z
)
1192 if (x
->expr_type
!= EXPR_CONSTANT
1193 || y
->expr_type
!= EXPR_CONSTANT
1194 || z
->expr_type
!= EXPR_CONSTANT
)
1197 if (gfc_extract_int (y
, &pos
) != NULL
|| pos
< 0)
1199 gfc_error ("Invalid second argument of IBITS at %L", &y
->where
);
1200 return &gfc_bad_expr
;
1203 if (gfc_extract_int (z
, &len
) != NULL
|| len
< 0)
1205 gfc_error ("Invalid third argument of IBITS at %L", &z
->where
);
1206 return &gfc_bad_expr
;
1209 k
= gfc_validate_kind (BT_INTEGER
, x
->ts
.kind
, false);
1211 bitsize
= gfc_integer_kinds
[k
].bit_size
;
1213 if (pos
+ len
> bitsize
)
1216 ("Sum of second and third arguments of IBITS exceeds bit size "
1217 "at %L", &y
->where
);
1218 return &gfc_bad_expr
;
1221 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1223 bits
= gfc_getmem (bitsize
* sizeof (int));
1225 for (i
= 0; i
< bitsize
; i
++)
1228 for (i
= 0; i
< len
; i
++)
1229 bits
[i
] = mpz_tstbit (x
->value
.integer
, i
+ pos
);
1231 for (i
= 0; i
< bitsize
; i
++)
1235 mpz_clrbit (result
->value
.integer
, i
);
1237 else if (bits
[i
] == 1)
1239 mpz_setbit (result
->value
.integer
, i
);
1243 gfc_internal_error ("IBITS: Bad bit");
1249 return range_check (result
, "IBITS");
1254 gfc_simplify_ibset (gfc_expr
* x
, gfc_expr
* y
)
1259 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1262 if (gfc_extract_int (y
, &pos
) != NULL
|| pos
< 0)
1264 gfc_error ("Invalid second argument of IBSET at %L", &y
->where
);
1265 return &gfc_bad_expr
;
1268 k
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
1270 if (pos
> gfc_integer_kinds
[k
].bit_size
)
1272 gfc_error ("Second argument of IBSET exceeds bit size at %L",
1274 return &gfc_bad_expr
;
1277 result
= gfc_copy_expr (x
);
1279 mpz_setbit (result
->value
.integer
, pos
);
1280 return range_check (result
, "IBSET");
1285 gfc_simplify_ichar (gfc_expr
* e
)
1290 if (e
->expr_type
!= EXPR_CONSTANT
)
1293 if (e
->value
.character
.length
!= 1)
1295 gfc_error ("Argument of ICHAR at %L must be of length one", &e
->where
);
1296 return &gfc_bad_expr
;
1299 index
= (int) e
->value
.character
.string
[0];
1301 if (index
< CHAR_MIN
|| index
> CHAR_MAX
)
1303 gfc_error ("Argument of ICHAR at %L out of range of this processor",
1305 return &gfc_bad_expr
;
1308 result
= gfc_int_expr (index
);
1309 result
->where
= e
->where
;
1310 return range_check (result
, "ICHAR");
1315 gfc_simplify_ieor (gfc_expr
* x
, gfc_expr
* y
)
1319 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1322 result
= gfc_constant_result (BT_INTEGER
, x
->ts
.kind
, &x
->where
);
1324 mpz_xor (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
1326 return range_check (result
, "IEOR");
1331 gfc_simplify_index (gfc_expr
* x
, gfc_expr
* y
, gfc_expr
* b
)
1334 int back
, len
, lensub
;
1335 int i
, j
, k
, count
, index
= 0, start
;
1337 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1340 if (b
!= NULL
&& b
->value
.logical
!= 0)
1345 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
1348 len
= x
->value
.character
.length
;
1349 lensub
= y
->value
.character
.length
;
1353 mpz_set_si (result
->value
.integer
, 0);
1362 mpz_set_si (result
->value
.integer
, 1);
1365 else if (lensub
== 1)
1367 for (i
= 0; i
< len
; i
++)
1369 for (j
= 0; j
< lensub
; j
++)
1371 if (y
->value
.character
.string
[j
] ==
1372 x
->value
.character
.string
[i
])
1382 for (i
= 0; i
< len
; i
++)
1384 for (j
= 0; j
< lensub
; j
++)
1386 if (y
->value
.character
.string
[j
] ==
1387 x
->value
.character
.string
[i
])
1392 for (k
= 0; k
< lensub
; k
++)
1394 if (y
->value
.character
.string
[k
] ==
1395 x
->value
.character
.string
[k
+ start
])
1399 if (count
== lensub
)
1415 mpz_set_si (result
->value
.integer
, len
+ 1);
1418 else if (lensub
== 1)
1420 for (i
= 0; i
< len
; i
++)
1422 for (j
= 0; j
< lensub
; j
++)
1424 if (y
->value
.character
.string
[j
] ==
1425 x
->value
.character
.string
[len
- i
])
1427 index
= len
- i
+ 1;
1435 for (i
= 0; i
< len
; i
++)
1437 for (j
= 0; j
< lensub
; j
++)
1439 if (y
->value
.character
.string
[j
] ==
1440 x
->value
.character
.string
[len
- i
])
1443 if (start
<= len
- lensub
)
1446 for (k
= 0; k
< lensub
; k
++)
1447 if (y
->value
.character
.string
[k
] ==
1448 x
->value
.character
.string
[k
+ start
])
1451 if (count
== lensub
)
1468 mpz_set_si (result
->value
.integer
, index
);
1469 return range_check (result
, "INDEX");
1474 gfc_simplify_int (gfc_expr
* e
, gfc_expr
* k
)
1476 gfc_expr
*rpart
, *rtrunc
, *result
;
1479 kind
= get_kind (BT_REAL
, k
, "INT", gfc_default_real_kind
);
1481 return &gfc_bad_expr
;
1483 if (e
->expr_type
!= EXPR_CONSTANT
)
1486 result
= gfc_constant_result (BT_INTEGER
, kind
, &e
->where
);
1491 mpz_set (result
->value
.integer
, e
->value
.integer
);
1495 rtrunc
= gfc_copy_expr (e
);
1496 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
1497 gfc_mpfr_to_mpz (result
->value
.integer
, rtrunc
->value
.real
);
1498 gfc_free_expr (rtrunc
);
1502 rpart
= gfc_complex2real (e
, kind
);
1503 rtrunc
= gfc_copy_expr (rpart
);
1504 mpfr_trunc (rtrunc
->value
.real
, rpart
->value
.real
);
1505 gfc_mpfr_to_mpz (result
->value
.integer
, rtrunc
->value
.real
);
1506 gfc_free_expr (rpart
);
1507 gfc_free_expr (rtrunc
);
1511 gfc_error ("Argument of INT at %L is not a valid type", &e
->where
);
1512 gfc_free_expr (result
);
1513 return &gfc_bad_expr
;
1516 return range_check (result
, "INT");
1521 gfc_simplify_ifix (gfc_expr
* e
)
1523 gfc_expr
*rtrunc
, *result
;
1525 if (e
->expr_type
!= EXPR_CONSTANT
)
1528 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
1531 rtrunc
= gfc_copy_expr (e
);
1533 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
1534 gfc_mpfr_to_mpz (result
->value
.integer
, rtrunc
->value
.real
);
1536 gfc_free_expr (rtrunc
);
1537 return range_check (result
, "IFIX");
1542 gfc_simplify_idint (gfc_expr
* e
)
1544 gfc_expr
*rtrunc
, *result
;
1546 if (e
->expr_type
!= EXPR_CONSTANT
)
1549 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
1552 rtrunc
= gfc_copy_expr (e
);
1554 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
1555 gfc_mpfr_to_mpz (result
->value
.integer
, rtrunc
->value
.real
);
1557 gfc_free_expr (rtrunc
);
1558 return range_check (result
, "IDINT");
1563 gfc_simplify_ior (gfc_expr
* x
, gfc_expr
* y
)
1567 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1570 result
= gfc_constant_result (BT_INTEGER
, x
->ts
.kind
, &x
->where
);
1572 mpz_ior (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
1573 return range_check (result
, "IOR");
1578 gfc_simplify_ishft (gfc_expr
* e
, gfc_expr
* s
)
1581 int shift
, ashift
, isize
, k
, *bits
, i
;
1583 if (e
->expr_type
!= EXPR_CONSTANT
|| s
->expr_type
!= EXPR_CONSTANT
)
1586 if (gfc_extract_int (s
, &shift
) != NULL
)
1588 gfc_error ("Invalid second argument of ISHFT at %L", &s
->where
);
1589 return &gfc_bad_expr
;
1592 k
= gfc_validate_kind (BT_INTEGER
, e
->ts
.kind
, false);
1594 isize
= gfc_integer_kinds
[k
].bit_size
;
1604 ("Magnitude of second argument of ISHFT exceeds bit size at %L",
1606 return &gfc_bad_expr
;
1609 result
= gfc_constant_result (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
1613 mpz_set (result
->value
.integer
, e
->value
.integer
);
1614 return range_check (result
, "ISHFT");
1617 bits
= gfc_getmem (isize
* sizeof (int));
1619 for (i
= 0; i
< isize
; i
++)
1620 bits
[i
] = mpz_tstbit (e
->value
.integer
, i
);
1624 for (i
= 0; i
< shift
; i
++)
1625 mpz_clrbit (result
->value
.integer
, i
);
1627 for (i
= 0; i
< isize
- shift
; i
++)
1630 mpz_clrbit (result
->value
.integer
, i
+ shift
);
1632 mpz_setbit (result
->value
.integer
, i
+ shift
);
1637 for (i
= isize
- 1; i
>= isize
- ashift
; i
--)
1638 mpz_clrbit (result
->value
.integer
, i
);
1640 for (i
= isize
- 1; i
>= ashift
; i
--)
1643 mpz_clrbit (result
->value
.integer
, i
- ashift
);
1645 mpz_setbit (result
->value
.integer
, i
- ashift
);
1649 twos_complement (result
->value
.integer
, isize
);
1657 gfc_simplify_ishftc (gfc_expr
* e
, gfc_expr
* s
, gfc_expr
* sz
)
1660 int shift
, ashift
, isize
, delta
, k
;
1663 if (e
->expr_type
!= EXPR_CONSTANT
|| s
->expr_type
!= EXPR_CONSTANT
)
1666 if (gfc_extract_int (s
, &shift
) != NULL
)
1668 gfc_error ("Invalid second argument of ISHFTC at %L", &s
->where
);
1669 return &gfc_bad_expr
;
1672 k
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
1676 if (gfc_extract_int (sz
, &isize
) != NULL
|| isize
< 0)
1678 gfc_error ("Invalid third argument of ISHFTC at %L", &sz
->where
);
1679 return &gfc_bad_expr
;
1683 isize
= gfc_integer_kinds
[k
].bit_size
;
1693 ("Magnitude of second argument of ISHFTC exceeds third argument "
1694 "at %L", &s
->where
);
1695 return &gfc_bad_expr
;
1698 result
= gfc_constant_result (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
1702 mpz_set (result
->value
.integer
, e
->value
.integer
);
1706 bits
= gfc_getmem (isize
* sizeof (int));
1708 for (i
= 0; i
< isize
; i
++)
1709 bits
[i
] = mpz_tstbit (e
->value
.integer
, i
);
1711 delta
= isize
- ashift
;
1715 for (i
= 0; i
< delta
; i
++)
1718 mpz_clrbit (result
->value
.integer
, i
+ shift
);
1720 mpz_setbit (result
->value
.integer
, i
+ shift
);
1723 for (i
= delta
; i
< isize
; i
++)
1726 mpz_clrbit (result
->value
.integer
, i
- delta
);
1728 mpz_setbit (result
->value
.integer
, i
- delta
);
1733 for (i
= 0; i
< ashift
; i
++)
1736 mpz_clrbit (result
->value
.integer
, i
+ delta
);
1738 mpz_setbit (result
->value
.integer
, i
+ delta
);
1741 for (i
= ashift
; i
< isize
; i
++)
1744 mpz_clrbit (result
->value
.integer
, i
+ shift
);
1746 mpz_setbit (result
->value
.integer
, i
+ shift
);
1750 twos_complement (result
->value
.integer
, isize
);
1758 gfc_simplify_kind (gfc_expr
* e
)
1761 if (e
->ts
.type
== BT_DERIVED
)
1763 gfc_error ("Argument of KIND at %L is a DERIVED type", &e
->where
);
1764 return &gfc_bad_expr
;
1767 return gfc_int_expr (e
->ts
.kind
);
1772 gfc_simplify_bound (gfc_expr
* array
, gfc_expr
* dim
, int upper
)
1778 if (array
->expr_type
!= EXPR_VARIABLE
)
1784 if (dim
->expr_type
!= EXPR_CONSTANT
)
1787 /* Follow any component references. */
1788 as
= array
->symtree
->n
.sym
->as
;
1790 while (ref
->next
!= NULL
)
1792 if (ref
->type
== REF_COMPONENT
)
1793 as
= ref
->u
.c
.sym
->as
;
1797 if (ref
->type
!= REF_ARRAY
|| ref
->u
.ar
.type
!= AR_FULL
)
1800 i
= mpz_get_si (dim
->value
.integer
);
1802 return gfc_copy_expr (as
->upper
[i
-1]);
1804 return gfc_copy_expr (as
->lower
[i
-1]);
1809 gfc_simplify_lbound (gfc_expr
* array
, gfc_expr
* dim
)
1811 return gfc_simplify_bound (array
, dim
, 0);
1816 gfc_simplify_len (gfc_expr
* e
)
1820 if (e
->expr_type
!= EXPR_CONSTANT
)
1823 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
1826 mpz_set_si (result
->value
.integer
, e
->value
.character
.length
);
1827 return range_check (result
, "LEN");
1832 gfc_simplify_len_trim (gfc_expr
* e
)
1835 int count
, len
, lentrim
, i
;
1837 if (e
->expr_type
!= EXPR_CONSTANT
)
1840 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
1843 len
= e
->value
.character
.length
;
1845 for (count
= 0, i
= 1; i
<= len
; i
++)
1846 if (e
->value
.character
.string
[len
- i
] == ' ')
1851 lentrim
= len
- count
;
1853 mpz_set_si (result
->value
.integer
, lentrim
);
1854 return range_check (result
, "LEN_TRIM");
1859 gfc_simplify_lge (gfc_expr
* a
, gfc_expr
* b
)
1862 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
1865 return gfc_logical_expr (gfc_compare_string (a
, b
, xascii_table
) >= 0,
1871 gfc_simplify_lgt (gfc_expr
* a
, gfc_expr
* b
)
1874 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
1877 return gfc_logical_expr (gfc_compare_string (a
, b
, xascii_table
) > 0,
1883 gfc_simplify_lle (gfc_expr
* a
, gfc_expr
* b
)
1886 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
1889 return gfc_logical_expr (gfc_compare_string (a
, b
, xascii_table
) <= 0,
1895 gfc_simplify_llt (gfc_expr
* a
, gfc_expr
* b
)
1898 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
1901 return gfc_logical_expr (gfc_compare_string (a
, b
, xascii_table
) < 0,
1907 gfc_simplify_log (gfc_expr
* x
)
1912 if (x
->expr_type
!= EXPR_CONSTANT
)
1915 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1917 gfc_set_model_kind (x
->ts
.kind
);
1922 if (mpfr_sgn (x
->value
.real
) <= 0)
1925 ("Argument of LOG at %L cannot be less than or equal to zero",
1927 gfc_free_expr (result
);
1928 return &gfc_bad_expr
;
1931 mpfr_log(result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1935 if ((mpfr_sgn (x
->value
.complex.r
) == 0)
1936 && (mpfr_sgn (x
->value
.complex.i
) == 0))
1938 gfc_error ("Complex argument of LOG at %L cannot be zero",
1940 gfc_free_expr (result
);
1941 return &gfc_bad_expr
;
1947 arctangent2 (x
->value
.complex.i
, x
->value
.complex.r
,
1948 result
->value
.complex.i
);
1950 mpfr_mul (xr
, x
->value
.complex.r
, x
->value
.complex.r
, GFC_RND_MODE
);
1951 mpfr_mul (xi
, x
->value
.complex.i
, x
->value
.complex.i
, GFC_RND_MODE
);
1952 mpfr_add (xr
, xr
, xi
, GFC_RND_MODE
);
1953 mpfr_sqrt (xr
, xr
, GFC_RND_MODE
);
1954 mpfr_log (result
->value
.complex.r
, xr
, GFC_RND_MODE
);
1962 gfc_internal_error ("gfc_simplify_log: bad type");
1965 return range_check (result
, "LOG");
1970 gfc_simplify_log10 (gfc_expr
* x
)
1974 if (x
->expr_type
!= EXPR_CONSTANT
)
1977 gfc_set_model_kind (x
->ts
.kind
);
1979 if (mpfr_sgn (x
->value
.real
) <= 0)
1982 ("Argument of LOG10 at %L cannot be less than or equal to zero",
1984 return &gfc_bad_expr
;
1987 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1989 mpfr_log10 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1991 return range_check (result
, "LOG10");
1996 gfc_simplify_logical (gfc_expr
* e
, gfc_expr
* k
)
2001 kind
= get_kind (BT_LOGICAL
, k
, "LOGICAL", gfc_default_logical_kind
);
2003 return &gfc_bad_expr
;
2005 if (e
->expr_type
!= EXPR_CONSTANT
)
2008 result
= gfc_constant_result (BT_LOGICAL
, kind
, &e
->where
);
2010 result
->value
.logical
= e
->value
.logical
;
2016 /* This function is special since MAX() can take any number of
2017 arguments. The simplified expression is a rewritten version of the
2018 argument list containing at most one constant element. Other
2019 constant elements are deleted. Because the argument list has
2020 already been checked, this function always succeeds. sign is 1 for
2021 MAX(), -1 for MIN(). */
2024 simplify_min_max (gfc_expr
* expr
, int sign
)
2026 gfc_actual_arglist
*arg
, *last
, *extremum
;
2027 gfc_intrinsic_sym
* specific
;
2031 specific
= expr
->value
.function
.isym
;
2033 arg
= expr
->value
.function
.actual
;
2035 for (; arg
; last
= arg
, arg
= arg
->next
)
2037 if (arg
->expr
->expr_type
!= EXPR_CONSTANT
)
2040 if (extremum
== NULL
)
2046 switch (arg
->expr
->ts
.type
)
2049 if (mpz_cmp (arg
->expr
->value
.integer
,
2050 extremum
->expr
->value
.integer
) * sign
> 0)
2051 mpz_set (extremum
->expr
->value
.integer
, arg
->expr
->value
.integer
);
2056 if (mpfr_cmp (arg
->expr
->value
.real
, extremum
->expr
->value
.real
) *
2058 mpfr_set (extremum
->expr
->value
.real
, arg
->expr
->value
.real
,
2064 gfc_internal_error ("gfc_simplify_max(): Bad type in arglist");
2067 /* Delete the extra constant argument. */
2069 expr
->value
.function
.actual
= arg
->next
;
2071 last
->next
= arg
->next
;
2074 gfc_free_actual_arglist (arg
);
2078 /* If there is one value left, replace the function call with the
2080 if (expr
->value
.function
.actual
->next
!= NULL
)
2083 /* Convert to the correct type and kind. */
2084 if (expr
->ts
.type
!= BT_UNKNOWN
)
2085 return gfc_convert_constant (expr
->value
.function
.actual
->expr
,
2086 expr
->ts
.type
, expr
->ts
.kind
);
2088 if (specific
->ts
.type
!= BT_UNKNOWN
)
2089 return gfc_convert_constant (expr
->value
.function
.actual
->expr
,
2090 specific
->ts
.type
, specific
->ts
.kind
);
2092 return gfc_copy_expr (expr
->value
.function
.actual
->expr
);
2097 gfc_simplify_min (gfc_expr
* e
)
2099 return simplify_min_max (e
, -1);
2104 gfc_simplify_max (gfc_expr
* e
)
2106 return simplify_min_max (e
, 1);
2111 gfc_simplify_maxexponent (gfc_expr
* x
)
2116 i
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, false);
2118 result
= gfc_int_expr (gfc_real_kinds
[i
].max_exponent
);
2119 result
->where
= x
->where
;
2126 gfc_simplify_minexponent (gfc_expr
* x
)
2131 i
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, false);
2133 result
= gfc_int_expr (gfc_real_kinds
[i
].min_exponent
);
2134 result
->where
= x
->where
;
2141 gfc_simplify_mod (gfc_expr
* a
, gfc_expr
* p
)
2144 mpfr_t quot
, iquot
, term
;
2146 if (a
->expr_type
!= EXPR_CONSTANT
|| p
->expr_type
!= EXPR_CONSTANT
)
2149 result
= gfc_constant_result (a
->ts
.type
, a
->ts
.kind
, &a
->where
);
2154 if (mpz_cmp_ui (p
->value
.integer
, 0) == 0)
2156 /* Result is processor-dependent. */
2157 gfc_error ("Second argument MOD at %L is zero", &a
->where
);
2158 gfc_free_expr (result
);
2159 return &gfc_bad_expr
;
2161 mpz_tdiv_r (result
->value
.integer
, a
->value
.integer
, p
->value
.integer
);
2165 if (mpfr_cmp_ui (p
->value
.real
, 0) == 0)
2167 /* Result is processor-dependent. */
2168 gfc_error ("Second argument of MOD at %L is zero", &p
->where
);
2169 gfc_free_expr (result
);
2170 return &gfc_bad_expr
;
2173 gfc_set_model_kind (a
->ts
.kind
);
2178 mpfr_div (quot
, a
->value
.real
, p
->value
.real
, GFC_RND_MODE
);
2179 mpfr_trunc (iquot
, quot
);
2180 mpfr_mul (term
, iquot
, p
->value
.real
, GFC_RND_MODE
);
2181 mpfr_sub (result
->value
.real
, a
->value
.real
, term
, GFC_RND_MODE
);
2189 gfc_internal_error ("gfc_simplify_mod(): Bad arguments");
2192 return range_check (result
, "MOD");
2197 gfc_simplify_modulo (gfc_expr
* a
, gfc_expr
* p
)
2200 mpfr_t quot
, iquot
, term
;
2202 if (a
->expr_type
!= EXPR_CONSTANT
|| p
->expr_type
!= EXPR_CONSTANT
)
2205 result
= gfc_constant_result (a
->ts
.type
, a
->ts
.kind
, &a
->where
);
2210 if (mpz_cmp_ui (p
->value
.integer
, 0) == 0)
2212 /* Result is processor-dependent. This processor just opts
2213 to not handle it at all. */
2214 gfc_error ("Second argument of MODULO at %L is zero", &a
->where
);
2215 gfc_free_expr (result
);
2216 return &gfc_bad_expr
;
2218 mpz_fdiv_r (result
->value
.integer
, a
->value
.integer
, p
->value
.integer
);
2223 if (mpfr_cmp_ui (p
->value
.real
, 0) == 0)
2225 /* Result is processor-dependent. */
2226 gfc_error ("Second argument of MODULO at %L is zero", &p
->where
);
2227 gfc_free_expr (result
);
2228 return &gfc_bad_expr
;
2231 gfc_set_model_kind (a
->ts
.kind
);
2236 mpfr_div (quot
, a
->value
.real
, p
->value
.real
, GFC_RND_MODE
);
2237 mpfr_floor (iquot
, quot
);
2238 mpfr_mul (term
, iquot
, p
->value
.real
, GFC_RND_MODE
);
2244 mpfr_sub (result
->value
.real
, a
->value
.real
, term
, GFC_RND_MODE
);
2248 gfc_internal_error ("gfc_simplify_modulo(): Bad arguments");
2251 return range_check (result
, "MODULO");
2255 /* Exists for the sole purpose of consistency with other intrinsics. */
2257 gfc_simplify_mvbits (gfc_expr
* f ATTRIBUTE_UNUSED
,
2258 gfc_expr
* fp ATTRIBUTE_UNUSED
,
2259 gfc_expr
* l ATTRIBUTE_UNUSED
,
2260 gfc_expr
* to ATTRIBUTE_UNUSED
,
2261 gfc_expr
* tp ATTRIBUTE_UNUSED
)
2268 gfc_simplify_nearest (gfc_expr
* x
, gfc_expr
* s
)
2273 int p
, i
, k
, match_float
;
2275 /* FIXME: This implementation is dopey and probably not quite right,
2276 but it's a start. */
2278 if (x
->expr_type
!= EXPR_CONSTANT
)
2281 k
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
2283 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2285 val
= mpfr_get_d (x
->value
.real
, GFC_RND_MODE
);
2286 p
= gfc_real_kinds
[k
].digits
;
2289 for (i
= 1; i
< p
; ++i
)
2294 /* TODO we should make sure that 'float' matches kind 4 */
2295 match_float
= gfc_real_kinds
[k
].kind
== 4;
2296 if (mpfr_cmp_ui (s
->value
.real
, 0) > 0)
2302 mpfr_set_d (result
->value
.real
, rval
, GFC_RND_MODE
);
2307 mpfr_set_d (result
->value
.real
, val
, GFC_RND_MODE
);
2310 else if (mpfr_cmp_ui (s
->value
.real
, 0) < 0)
2316 mpfr_set_d (result
->value
.real
, rval
, GFC_RND_MODE
);
2321 mpfr_set_d (result
->value
.real
, val
, GFC_RND_MODE
);
2326 gfc_error ("Invalid second argument of NEAREST at %L", &s
->where
);
2328 return &gfc_bad_expr
;
2331 return range_check (result
, "NEAREST");
2336 simplify_nint (const char *name
, gfc_expr
* e
, gfc_expr
* k
)
2338 gfc_expr
*rtrunc
, *itrunc
, *result
;
2342 kind
= get_kind (BT_INTEGER
, k
, name
, gfc_default_integer_kind
);
2344 return &gfc_bad_expr
;
2346 if (e
->expr_type
!= EXPR_CONSTANT
)
2349 result
= gfc_constant_result (BT_INTEGER
, kind
, &e
->where
);
2351 rtrunc
= gfc_copy_expr (e
);
2352 itrunc
= gfc_copy_expr (e
);
2354 cmp
= mpfr_cmp_ui (e
->value
.real
, 0);
2356 gfc_set_model (e
->value
.real
);
2358 mpfr_set_str (half
, "0.5", 10, GFC_RND_MODE
);
2362 mpfr_add (rtrunc
->value
.real
, e
->value
.real
, half
, GFC_RND_MODE
);
2363 mpfr_trunc (itrunc
->value
.real
, rtrunc
->value
.real
);
2367 mpfr_sub (rtrunc
->value
.real
, e
->value
.real
, half
, GFC_RND_MODE
);
2368 mpfr_trunc (itrunc
->value
.real
, rtrunc
->value
.real
);
2371 mpfr_set_ui (itrunc
->value
.real
, 0, GFC_RND_MODE
);
2373 gfc_mpfr_to_mpz (result
->value
.integer
, itrunc
->value
.real
);
2375 gfc_free_expr (itrunc
);
2376 gfc_free_expr (rtrunc
);
2379 return range_check (result
, name
);
2384 gfc_simplify_nint (gfc_expr
* e
, gfc_expr
* k
)
2386 return simplify_nint ("NINT", e
, k
);
2391 gfc_simplify_idnint (gfc_expr
* e
)
2393 return simplify_nint ("IDNINT", e
, NULL
);
2398 gfc_simplify_not (gfc_expr
* e
)
2403 if (e
->expr_type
!= EXPR_CONSTANT
)
2406 result
= gfc_constant_result (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
2408 mpz_com (result
->value
.integer
, e
->value
.integer
);
2410 /* Because of how GMP handles numbers, the result must be ANDed with
2411 the max_int mask. For radices <> 2, this will require change. */
2413 i
= gfc_validate_kind (BT_INTEGER
, e
->ts
.kind
, false);
2415 mpz_and (result
->value
.integer
, result
->value
.integer
,
2416 gfc_integer_kinds
[i
].max_int
);
2418 return range_check (result
, "NOT");
2423 gfc_simplify_null (gfc_expr
* mold
)
2427 result
= gfc_get_expr ();
2428 result
->expr_type
= EXPR_NULL
;
2431 result
->ts
.type
= BT_UNKNOWN
;
2434 result
->ts
= mold
->ts
;
2435 result
->where
= mold
->where
;
2443 gfc_simplify_precision (gfc_expr
* e
)
2448 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
2450 result
= gfc_int_expr (gfc_real_kinds
[i
].precision
);
2451 result
->where
= e
->where
;
2458 gfc_simplify_radix (gfc_expr
* e
)
2463 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
2467 i
= gfc_integer_kinds
[i
].radix
;
2471 i
= gfc_real_kinds
[i
].radix
;
2478 result
= gfc_int_expr (i
);
2479 result
->where
= e
->where
;
2486 gfc_simplify_range (gfc_expr
* e
)
2492 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
2497 j
= gfc_integer_kinds
[i
].range
;
2502 j
= gfc_real_kinds
[i
].range
;
2509 result
= gfc_int_expr (j
);
2510 result
->where
= e
->where
;
2517 gfc_simplify_real (gfc_expr
* e
, gfc_expr
* k
)
2522 if (e
->ts
.type
== BT_COMPLEX
)
2523 kind
= get_kind (BT_REAL
, k
, "REAL", e
->ts
.kind
);
2525 kind
= get_kind (BT_REAL
, k
, "REAL", gfc_default_real_kind
);
2528 return &gfc_bad_expr
;
2530 if (e
->expr_type
!= EXPR_CONSTANT
)
2536 result
= gfc_int2real (e
, kind
);
2540 result
= gfc_real2real (e
, kind
);
2544 result
= gfc_complex2real (e
, kind
);
2548 gfc_internal_error ("bad type in REAL");
2552 return range_check (result
, "REAL");
2556 gfc_simplify_repeat (gfc_expr
* e
, gfc_expr
* n
)
2559 int i
, j
, len
, ncopies
, nlen
;
2561 if (e
->expr_type
!= EXPR_CONSTANT
|| n
->expr_type
!= EXPR_CONSTANT
)
2564 if (n
!= NULL
&& (gfc_extract_int (n
, &ncopies
) != NULL
|| ncopies
< 0))
2566 gfc_error ("Invalid second argument of REPEAT at %L", &n
->where
);
2567 return &gfc_bad_expr
;
2570 len
= e
->value
.character
.length
;
2571 nlen
= ncopies
* len
;
2573 result
= gfc_constant_result (BT_CHARACTER
, e
->ts
.kind
, &e
->where
);
2577 result
->value
.character
.string
= gfc_getmem (1);
2578 result
->value
.character
.length
= 0;
2579 result
->value
.character
.string
[0] = '\0';
2583 result
->value
.character
.length
= nlen
;
2584 result
->value
.character
.string
= gfc_getmem (nlen
+ 1);
2586 for (i
= 0; i
< ncopies
; i
++)
2587 for (j
= 0; j
< len
; j
++)
2588 result
->value
.character
.string
[j
+ i
* len
] =
2589 e
->value
.character
.string
[j
];
2591 result
->value
.character
.string
[nlen
] = '\0'; /* For debugger */
2596 /* This one is a bear, but mainly has to do with shuffling elements. */
2599 gfc_simplify_reshape (gfc_expr
* source
, gfc_expr
* shape_exp
,
2600 gfc_expr
* pad
, gfc_expr
* order_exp
)
2603 int order
[GFC_MAX_DIMENSIONS
], shape
[GFC_MAX_DIMENSIONS
];
2604 int i
, rank
, npad
, x
[GFC_MAX_DIMENSIONS
];
2605 gfc_constructor
*head
, *tail
;
2611 /* Unpack the shape array. */
2612 if (source
->expr_type
!= EXPR_ARRAY
|| !gfc_is_constant_expr (source
))
2615 if (shape_exp
->expr_type
!= EXPR_ARRAY
|| !gfc_is_constant_expr (shape_exp
))
2619 && (pad
->expr_type
!= EXPR_ARRAY
2620 || !gfc_is_constant_expr (pad
)))
2623 if (order_exp
!= NULL
2624 && (order_exp
->expr_type
!= EXPR_ARRAY
2625 || !gfc_is_constant_expr (order_exp
)))
2634 e
= gfc_get_array_element (shape_exp
, rank
);
2638 if (gfc_extract_int (e
, &shape
[rank
]) != NULL
)
2640 gfc_error ("Integer too large in shape specification at %L",
2648 if (rank
>= GFC_MAX_DIMENSIONS
)
2650 gfc_error ("Too many dimensions in shape specification for RESHAPE "
2651 "at %L", &e
->where
);
2656 if (shape
[rank
] < 0)
2658 gfc_error ("Shape specification at %L cannot be negative",
2668 gfc_error ("Shape specification at %L cannot be the null array",
2673 /* Now unpack the order array if present. */
2674 if (order_exp
== NULL
)
2676 for (i
= 0; i
< rank
; i
++)
2683 for (i
= 0; i
< rank
; i
++)
2686 for (i
= 0; i
< rank
; i
++)
2688 e
= gfc_get_array_element (order_exp
, i
);
2692 ("ORDER parameter of RESHAPE at %L is not the same size "
2693 "as SHAPE parameter", &order_exp
->where
);
2697 if (gfc_extract_int (e
, &order
[i
]) != NULL
)
2699 gfc_error ("Error in ORDER parameter of RESHAPE at %L",
2707 if (order
[i
] < 1 || order
[i
] > rank
)
2709 gfc_error ("ORDER parameter of RESHAPE at %L is out of range",
2718 gfc_error ("Invalid permutation in ORDER parameter at %L",
2727 /* Count the elements in the source and padding arrays. */
2732 gfc_array_size (pad
, &size
);
2733 npad
= mpz_get_ui (size
);
2737 gfc_array_size (source
, &size
);
2738 nsource
= mpz_get_ui (size
);
2741 /* If it weren't for that pesky permutation we could just loop
2742 through the source and round out any shortage with pad elements.
2743 But no, someone just had to have the compiler do something the
2744 user should be doing. */
2746 for (i
= 0; i
< rank
; i
++)
2751 /* Figure out which element to extract. */
2752 mpz_set_ui (index
, 0);
2754 for (i
= rank
- 1; i
>= 0; i
--)
2756 mpz_add_ui (index
, index
, x
[order
[i
]]);
2758 mpz_mul_ui (index
, index
, shape
[order
[i
- 1]]);
2761 if (mpz_cmp_ui (index
, INT_MAX
) > 0)
2762 gfc_internal_error ("Reshaped array too large at %L", &e
->where
);
2764 j
= mpz_get_ui (index
);
2767 e
= gfc_get_array_element (source
, j
);
2775 ("PAD parameter required for short SOURCE parameter at %L",
2781 e
= gfc_get_array_element (pad
, j
);
2785 head
= tail
= gfc_get_constructor ();
2788 tail
->next
= gfc_get_constructor ();
2795 tail
->where
= e
->where
;
2798 /* Calculate the next element. */
2802 if (++x
[i
] < shape
[i
])
2813 e
= gfc_get_expr ();
2814 e
->where
= source
->where
;
2815 e
->expr_type
= EXPR_ARRAY
;
2816 e
->value
.constructor
= head
;
2817 e
->shape
= gfc_get_shape (rank
);
2819 for (i
= 0; i
< rank
; i
++)
2820 mpz_init_set_ui (e
->shape
[i
], shape
[i
]);
2822 e
->ts
= head
->expr
->ts
;
2828 gfc_free_constructor (head
);
2830 return &gfc_bad_expr
;
2835 gfc_simplify_rrspacing (gfc_expr
* x
)
2838 mpfr_t absv
, log2
, exp
, frac
, pow2
;
2841 if (x
->expr_type
!= EXPR_CONSTANT
)
2844 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
2846 result
= gfc_constant_result (BT_REAL
, x
->ts
.kind
, &x
->where
);
2848 p
= gfc_real_kinds
[i
].digits
;
2850 gfc_set_model_kind (x
->ts
.kind
);
2852 if (mpfr_sgn (x
->value
.real
) == 0)
2854 mpfr_ui_div (result
->value
.real
, 1, gfc_real_kinds
[i
].tiny
, GFC_RND_MODE
);
2863 mpfr_abs (absv
, x
->value
.real
, GFC_RND_MODE
);
2864 mpfr_log2 (log2
, absv
, GFC_RND_MODE
);
2866 mpfr_trunc (log2
, log2
);
2867 mpfr_add_ui (exp
, log2
, 1, GFC_RND_MODE
);
2869 mpfr_ui_pow (pow2
, 2, exp
, GFC_RND_MODE
);
2870 mpfr_div (frac
, absv
, pow2
, GFC_RND_MODE
);
2872 mpfr_mul_2exp (result
->value
.real
, frac
, (unsigned long)p
, GFC_RND_MODE
);
2879 return range_check (result
, "RRSPACING");
2884 gfc_simplify_scale (gfc_expr
* x
, gfc_expr
* i
)
2886 int k
, neg_flag
, power
, exp_range
;
2887 mpfr_t scale
, radix
;
2890 if (x
->expr_type
!= EXPR_CONSTANT
|| i
->expr_type
!= EXPR_CONSTANT
)
2893 result
= gfc_constant_result (BT_REAL
, x
->ts
.kind
, &x
->where
);
2895 if (mpfr_sgn (x
->value
.real
) == 0)
2897 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
2901 k
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, false);
2903 exp_range
= gfc_real_kinds
[k
].max_exponent
- gfc_real_kinds
[k
].min_exponent
;
2905 /* This check filters out values of i that would overflow an int. */
2906 if (mpz_cmp_si (i
->value
.integer
, exp_range
+ 2) > 0
2907 || mpz_cmp_si (i
->value
.integer
, -exp_range
- 2) < 0)
2909 gfc_error ("Result of SCALE overflows its kind at %L", &result
->where
);
2910 return &gfc_bad_expr
;
2913 /* Compute scale = radix ** power. */
2914 power
= mpz_get_si (i
->value
.integer
);
2924 gfc_set_model_kind (x
->ts
.kind
);
2927 mpfr_set_ui (radix
, gfc_real_kinds
[k
].radix
, GFC_RND_MODE
);
2928 mpfr_pow_ui (scale
, radix
, power
, GFC_RND_MODE
);
2931 mpfr_div (result
->value
.real
, x
->value
.real
, scale
, GFC_RND_MODE
);
2933 mpfr_mul (result
->value
.real
, x
->value
.real
, scale
, GFC_RND_MODE
);
2938 return range_check (result
, "SCALE");
2943 gfc_simplify_scan (gfc_expr
* e
, gfc_expr
* c
, gfc_expr
* b
)
2948 size_t indx
, len
, lenc
;
2950 if (e
->expr_type
!= EXPR_CONSTANT
|| c
->expr_type
!= EXPR_CONSTANT
)
2953 if (b
!= NULL
&& b
->value
.logical
!= 0)
2958 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
2961 len
= e
->value
.character
.length
;
2962 lenc
= c
->value
.character
.length
;
2964 if (len
== 0 || lenc
== 0)
2973 strcspn (e
->value
.character
.string
, c
->value
.character
.string
) + 1;
2980 for (indx
= len
; indx
> 0; indx
--)
2982 for (i
= 0; i
< lenc
; i
++)
2984 if (c
->value
.character
.string
[i
]
2985 == e
->value
.character
.string
[indx
- 1])
2993 mpz_set_ui (result
->value
.integer
, indx
);
2994 return range_check (result
, "SCAN");
2999 gfc_simplify_selected_int_kind (gfc_expr
* e
)
3004 if (e
->expr_type
!= EXPR_CONSTANT
|| gfc_extract_int (e
, &range
) != NULL
)
3009 for (i
= 0; gfc_integer_kinds
[i
].kind
!= 0; i
++)
3010 if (gfc_integer_kinds
[i
].range
>= range
3011 && gfc_integer_kinds
[i
].kind
< kind
)
3012 kind
= gfc_integer_kinds
[i
].kind
;
3014 if (kind
== INT_MAX
)
3017 result
= gfc_int_expr (kind
);
3018 result
->where
= e
->where
;
3025 gfc_simplify_selected_real_kind (gfc_expr
* p
, gfc_expr
* q
)
3027 int range
, precision
, i
, kind
, found_precision
, found_range
;
3034 if (p
->expr_type
!= EXPR_CONSTANT
3035 || gfc_extract_int (p
, &precision
) != NULL
)
3043 if (q
->expr_type
!= EXPR_CONSTANT
3044 || gfc_extract_int (q
, &range
) != NULL
)
3049 found_precision
= 0;
3052 for (i
= 0; gfc_real_kinds
[i
].kind
!= 0; i
++)
3054 if (gfc_real_kinds
[i
].precision
>= precision
)
3055 found_precision
= 1;
3057 if (gfc_real_kinds
[i
].range
>= range
)
3060 if (gfc_real_kinds
[i
].precision
>= precision
3061 && gfc_real_kinds
[i
].range
>= range
&& gfc_real_kinds
[i
].kind
< kind
)
3062 kind
= gfc_real_kinds
[i
].kind
;
3065 if (kind
== INT_MAX
)
3069 if (!found_precision
)
3075 result
= gfc_int_expr (kind
);
3076 result
->where
= (p
!= NULL
) ? p
->where
: q
->where
;
3083 gfc_simplify_set_exponent (gfc_expr
* x
, gfc_expr
* i
)
3086 mpfr_t exp
, absv
, log2
, pow2
, frac
;
3089 if (x
->expr_type
!= EXPR_CONSTANT
|| i
->expr_type
!= EXPR_CONSTANT
)
3092 result
= gfc_constant_result (BT_REAL
, x
->ts
.kind
, &x
->where
);
3094 gfc_set_model_kind (x
->ts
.kind
);
3096 if (mpfr_sgn (x
->value
.real
) == 0)
3098 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
3108 mpfr_abs (absv
, x
->value
.real
, GFC_RND_MODE
);
3109 mpfr_log2 (log2
, absv
, GFC_RND_MODE
);
3111 mpfr_trunc (log2
, log2
);
3112 mpfr_add_ui (exp
, log2
, 1, GFC_RND_MODE
);
3114 /* Old exponent value, and fraction. */
3115 mpfr_ui_pow (pow2
, 2, exp
, GFC_RND_MODE
);
3117 mpfr_div (frac
, absv
, pow2
, GFC_RND_MODE
);
3120 exp2
= (unsigned long) mpz_get_d (i
->value
.integer
);
3121 mpfr_mul_2exp (result
->value
.real
, frac
, exp2
, GFC_RND_MODE
);
3128 return range_check (result
, "SET_EXPONENT");
3133 gfc_simplify_shape (gfc_expr
* source
)
3135 mpz_t shape
[GFC_MAX_DIMENSIONS
];
3136 gfc_expr
*result
, *e
, *f
;
3141 if (source
->rank
== 0 || source
->expr_type
!= EXPR_VARIABLE
)
3144 result
= gfc_start_constructor (BT_INTEGER
, gfc_default_integer_kind
,
3147 ar
= gfc_find_array_ref (source
);
3149 t
= gfc_array_ref_shape (ar
, shape
);
3151 for (n
= 0; n
< source
->rank
; n
++)
3153 e
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
3158 mpz_set (e
->value
.integer
, shape
[n
]);
3159 mpz_clear (shape
[n
]);
3163 mpz_set_ui (e
->value
.integer
, n
+ 1);
3165 f
= gfc_simplify_size (source
, e
);
3169 gfc_free_expr (result
);
3178 gfc_append_constructor (result
, e
);
3186 gfc_simplify_size (gfc_expr
* array
, gfc_expr
* dim
)
3194 if (gfc_array_size (array
, &size
) == FAILURE
)
3199 if (dim
->expr_type
!= EXPR_CONSTANT
)
3202 d
= mpz_get_ui (dim
->value
.integer
) - 1;
3203 if (gfc_array_dimen_size (array
, d
, &size
) == FAILURE
)
3207 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
3210 mpz_set (result
->value
.integer
, size
);
3217 gfc_simplify_sign (gfc_expr
* x
, gfc_expr
* y
)
3221 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
3224 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3229 mpz_abs (result
->value
.integer
, x
->value
.integer
);
3230 if (mpz_sgn (y
->value
.integer
) < 0)
3231 mpz_neg (result
->value
.integer
, result
->value
.integer
);
3236 /* TODO: Handle -0.0 and +0.0 correctly on machines that support
3238 mpfr_abs (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
3239 if (mpfr_sgn (y
->value
.real
) < 0)
3240 mpfr_neg (result
->value
.real
, result
->value
.real
, GFC_RND_MODE
);
3245 gfc_internal_error ("Bad type in gfc_simplify_sign");
3253 gfc_simplify_sin (gfc_expr
* x
)
3258 if (x
->expr_type
!= EXPR_CONSTANT
)
3261 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3266 mpfr_sin (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
3270 gfc_set_model (x
->value
.real
);
3274 mpfr_sin (xp
, x
->value
.complex.r
, GFC_RND_MODE
);
3275 mpfr_cosh (xq
, x
->value
.complex.i
, GFC_RND_MODE
);
3276 mpfr_mul (result
->value
.complex.r
, xp
, xq
, GFC_RND_MODE
);
3278 mpfr_cos (xp
, x
->value
.complex.r
, GFC_RND_MODE
);
3279 mpfr_sinh (xq
, x
->value
.complex.i
, GFC_RND_MODE
);
3280 mpfr_mul (result
->value
.complex.i
, xp
, xq
, GFC_RND_MODE
);
3287 gfc_internal_error ("in gfc_simplify_sin(): Bad type");
3290 return range_check (result
, "SIN");
3295 gfc_simplify_sinh (gfc_expr
* x
)
3299 if (x
->expr_type
!= EXPR_CONSTANT
)
3302 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3304 mpfr_sinh(result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
3306 return range_check (result
, "SINH");
3310 /* The argument is always a double precision real that is converted to
3311 single precision. TODO: Rounding! */
3314 gfc_simplify_sngl (gfc_expr
* a
)
3318 if (a
->expr_type
!= EXPR_CONSTANT
)
3321 result
= gfc_real2real (a
, gfc_default_real_kind
);
3322 return range_check (result
, "SNGL");
3327 gfc_simplify_spacing (gfc_expr
* x
)
3334 if (x
->expr_type
!= EXPR_CONSTANT
)
3337 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
3339 p
= gfc_real_kinds
[i
].digits
;
3341 result
= gfc_constant_result (BT_REAL
, x
->ts
.kind
, &x
->where
);
3343 gfc_set_model_kind (x
->ts
.kind
);
3345 if (mpfr_sgn (x
->value
.real
) == 0)
3347 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].tiny
, GFC_RND_MODE
);
3354 mpfr_abs (absv
, x
->value
.real
, GFC_RND_MODE
);
3355 mpfr_log2 (log2
, absv
, GFC_RND_MODE
);
3356 mpfr_trunc (log2
, log2
);
3358 mpfr_add_ui (log2
, log2
, 1, GFC_RND_MODE
);
3360 /* FIXME: We should be using mpfr_get_si here, but this function is
3361 not available with the version of mpfr distributed with gmp (as of
3362 2004-09-17). Replace once mpfr has been imported into the gcc cvs
3364 diff
= (long)mpfr_get_d (log2
, GFC_RND_MODE
) - (long)p
;
3365 mpfr_set_ui (result
->value
.real
, 1, GFC_RND_MODE
);
3366 mpfr_mul_2si (result
->value
.real
, result
->value
.real
, diff
, GFC_RND_MODE
);
3371 if (mpfr_cmp (result
->value
.real
, gfc_real_kinds
[i
].tiny
) < 0)
3372 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].tiny
, GFC_RND_MODE
);
3374 return range_check (result
, "SPACING");
3379 gfc_simplify_sqrt (gfc_expr
* e
)
3382 mpfr_t ac
, ad
, s
, t
, w
;
3384 if (e
->expr_type
!= EXPR_CONSTANT
)
3387 result
= gfc_constant_result (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
3392 if (mpfr_cmp_si (e
->value
.real
, 0) < 0)
3394 mpfr_sqrt (result
->value
.real
, e
->value
.real
, GFC_RND_MODE
);
3399 /* Formula taken from Numerical Recipes to avoid over- and
3402 gfc_set_model (e
->value
.real
);
3409 if (mpfr_cmp_ui (e
->value
.complex.r
, 0) == 0
3410 && mpfr_cmp_ui (e
->value
.complex.i
, 0) == 0)
3413 mpfr_set_ui (result
->value
.complex.r
, 0, GFC_RND_MODE
);
3414 mpfr_set_ui (result
->value
.complex.i
, 0, GFC_RND_MODE
);
3418 mpfr_abs (ac
, e
->value
.complex.r
, GFC_RND_MODE
);
3419 mpfr_abs (ad
, e
->value
.complex.i
, GFC_RND_MODE
);
3421 if (mpfr_cmp (ac
, ad
) >= 0)
3423 mpfr_div (t
, e
->value
.complex.i
, e
->value
.complex.r
, GFC_RND_MODE
);
3424 mpfr_mul (t
, t
, t
, GFC_RND_MODE
);
3425 mpfr_add_ui (t
, t
, 1, GFC_RND_MODE
);
3426 mpfr_sqrt (t
, t
, GFC_RND_MODE
);
3427 mpfr_add_ui (t
, t
, 1, GFC_RND_MODE
);
3428 mpfr_div_ui (t
, t
, 2, GFC_RND_MODE
);
3429 mpfr_sqrt (t
, t
, GFC_RND_MODE
);
3430 mpfr_sqrt (s
, ac
, GFC_RND_MODE
);
3431 mpfr_mul (w
, s
, t
, GFC_RND_MODE
);
3435 mpfr_div (s
, e
->value
.complex.r
, e
->value
.complex.i
, GFC_RND_MODE
);
3436 mpfr_mul (t
, s
, s
, GFC_RND_MODE
);
3437 mpfr_add_ui (t
, t
, 1, GFC_RND_MODE
);
3438 mpfr_sqrt (t
, t
, GFC_RND_MODE
);
3439 mpfr_abs (s
, s
, GFC_RND_MODE
);
3440 mpfr_add (t
, t
, s
, GFC_RND_MODE
);
3441 mpfr_div_ui (t
, t
, 2, GFC_RND_MODE
);
3442 mpfr_sqrt (t
, t
, GFC_RND_MODE
);
3443 mpfr_sqrt (s
, ad
, GFC_RND_MODE
);
3444 mpfr_mul (w
, s
, t
, GFC_RND_MODE
);
3447 if (mpfr_cmp_ui (w
, 0) != 0 && mpfr_cmp_ui (e
->value
.complex.r
, 0) >= 0)
3449 mpfr_mul_ui (t
, w
, 2, GFC_RND_MODE
);
3450 mpfr_div (result
->value
.complex.i
, e
->value
.complex.i
, t
, GFC_RND_MODE
);
3451 mpfr_set (result
->value
.complex.r
, w
, GFC_RND_MODE
);
3453 else if (mpfr_cmp_ui (w
, 0) != 0
3454 && mpfr_cmp_ui (e
->value
.complex.r
, 0) < 0
3455 && mpfr_cmp_ui (e
->value
.complex.i
, 0) >= 0)
3457 mpfr_mul_ui (t
, w
, 2, GFC_RND_MODE
);
3458 mpfr_div (result
->value
.complex.r
, e
->value
.complex.i
, t
, GFC_RND_MODE
);
3459 mpfr_set (result
->value
.complex.i
, w
, GFC_RND_MODE
);
3461 else if (mpfr_cmp_ui (w
, 0) != 0
3462 && mpfr_cmp_ui (e
->value
.complex.r
, 0) < 0
3463 && mpfr_cmp_ui (e
->value
.complex.i
, 0) < 0)
3465 mpfr_mul_ui (t
, w
, 2, GFC_RND_MODE
);
3466 mpfr_div (result
->value
.complex.r
, ad
, t
, GFC_RND_MODE
);
3467 mpfr_neg (w
, w
, GFC_RND_MODE
);
3468 mpfr_set (result
->value
.complex.i
, w
, GFC_RND_MODE
);
3471 gfc_internal_error ("invalid complex argument of SQRT at %L",
3483 gfc_internal_error ("invalid argument of SQRT at %L", &e
->where
);
3486 return range_check (result
, "SQRT");
3489 gfc_free_expr (result
);
3490 gfc_error ("Argument of SQRT at %L has a negative value", &e
->where
);
3491 return &gfc_bad_expr
;
3496 gfc_simplify_tan (gfc_expr
* x
)
3501 if (x
->expr_type
!= EXPR_CONSTANT
)
3504 i
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, false);
3506 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3508 mpfr_tan (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
3510 return range_check (result
, "TAN");
3515 gfc_simplify_tanh (gfc_expr
* x
)
3519 if (x
->expr_type
!= EXPR_CONSTANT
)
3522 result
= gfc_constant_result (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3524 mpfr_tanh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
3526 return range_check (result
, "TANH");
3532 gfc_simplify_tiny (gfc_expr
* e
)
3537 i
= gfc_validate_kind (BT_REAL
, e
->ts
.kind
, false);
3539 result
= gfc_constant_result (BT_REAL
, e
->ts
.kind
, &e
->where
);
3540 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].tiny
, GFC_RND_MODE
);
3547 gfc_simplify_trim (gfc_expr
* e
)
3550 int count
, i
, len
, lentrim
;
3552 if (e
->expr_type
!= EXPR_CONSTANT
)
3555 len
= e
->value
.character
.length
;
3557 result
= gfc_constant_result (BT_CHARACTER
, e
->ts
.kind
, &e
->where
);
3559 for (count
= 0, i
= 1; i
<= len
; ++i
)
3561 if (e
->value
.character
.string
[len
- i
] == ' ')
3567 lentrim
= len
- count
;
3569 result
->value
.character
.length
= lentrim
;
3570 result
->value
.character
.string
= gfc_getmem (lentrim
+ 1);
3572 for (i
= 0; i
< lentrim
; i
++)
3573 result
->value
.character
.string
[i
] = e
->value
.character
.string
[i
];
3575 result
->value
.character
.string
[lentrim
] = '\0'; /* For debugger */
3582 gfc_simplify_ubound (gfc_expr
* array
, gfc_expr
* dim
)
3584 return gfc_simplify_bound (array
, dim
, 1);
3589 gfc_simplify_verify (gfc_expr
* s
, gfc_expr
* set
, gfc_expr
* b
)
3593 size_t index
, len
, lenset
;
3596 if (s
->expr_type
!= EXPR_CONSTANT
|| set
->expr_type
!= EXPR_CONSTANT
)
3599 if (b
!= NULL
&& b
->value
.logical
!= 0)
3604 result
= gfc_constant_result (BT_INTEGER
, gfc_default_integer_kind
,
3607 len
= s
->value
.character
.length
;
3608 lenset
= set
->value
.character
.length
;
3612 mpz_set_ui (result
->value
.integer
, 0);
3620 mpz_set_ui (result
->value
.integer
, len
);
3625 strspn (s
->value
.character
.string
, set
->value
.character
.string
) + 1;
3634 mpz_set_ui (result
->value
.integer
, 1);
3637 for (index
= len
; index
> 0; index
--)
3639 for (i
= 0; i
< lenset
; i
++)
3641 if (s
->value
.character
.string
[index
- 1]
3642 == set
->value
.character
.string
[i
])
3650 mpz_set_ui (result
->value
.integer
, index
);
3654 /****************** Constant simplification *****************/
3656 /* Master function to convert one constant to another. While this is
3657 used as a simplification function, it requires the destination type
3658 and kind information which is supplied by a special case in
3662 gfc_convert_constant (gfc_expr
* e
, bt type
, int kind
)
3664 gfc_expr
*g
, *result
, *(*f
) (gfc_expr
*, int);
3665 gfc_constructor
*head
, *c
, *tail
= NULL
;
3679 f
= gfc_int2complex
;
3696 f
= gfc_real2complex
;
3707 f
= gfc_complex2int
;
3710 f
= gfc_complex2real
;
3713 f
= gfc_complex2complex
;
3722 if (type
!= BT_LOGICAL
)
3729 gfc_internal_error ("gfc_convert_constant(): Unexpected type");
3734 switch (e
->expr_type
)
3737 result
= f (e
, kind
);
3739 return &gfc_bad_expr
;
3743 if (!gfc_is_constant_expr (e
))
3748 for (c
= e
->value
.constructor
; c
; c
= c
->next
)
3751 head
= tail
= gfc_get_constructor ();
3754 tail
->next
= gfc_get_constructor ();
3758 tail
->where
= c
->where
;
3760 if (c
->iterator
== NULL
)
3761 tail
->expr
= f (c
->expr
, kind
);
3764 g
= gfc_convert_constant (c
->expr
, type
, kind
);
3765 if (g
== &gfc_bad_expr
)
3770 if (tail
->expr
== NULL
)
3772 gfc_free_constructor (head
);
3777 result
= gfc_get_expr ();
3778 result
->ts
.type
= type
;
3779 result
->ts
.kind
= kind
;
3780 result
->expr_type
= EXPR_ARRAY
;
3781 result
->value
.constructor
= head
;
3782 result
->shape
= gfc_copy_shape (e
->shape
, e
->rank
);
3783 result
->where
= e
->where
;
3784 result
->rank
= e
->rank
;
3795 /****************** Helper functions ***********************/
3797 /* Given a collating table, create the inverse table. */
3800 invert_table (const int *table
, int *xtable
)
3804 for (i
= 0; i
< 256; i
++)
3807 for (i
= 0; i
< 256; i
++)
3808 xtable
[table
[i
]] = i
;
3813 gfc_simplify_init_1 (void)
3816 invert_table (ascii_table
, xascii_table
);