1 /* Simplify intrinsic functions at compile-time.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
3 2010 Free Software Foundation, Inc.
4 Contributed by Andy Vaught & Katherine Holcomb
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
27 #include "intrinsic.h"
28 #include "target-memory.h"
29 #include "constructor.h"
32 gfc_expr gfc_bad_expr
;
35 /* Note that 'simplification' is not just transforming expressions.
36 For functions that are not simplified at compile time, range
37 checking is done if possible.
39 The return convention is that each simplification function returns:
41 A new expression node corresponding to the simplified arguments.
42 The original arguments are destroyed by the caller, and must not
43 be a part of the new expression.
45 NULL pointer indicating that no simplification was possible and
46 the original expression should remain intact.
48 An expression pointer to gfc_bad_expr (a static placeholder)
49 indicating that some error has prevented simplification. The
50 error is generated within the function and should be propagated
53 By the time a simplification function gets control, it has been
54 decided that the function call is really supposed to be the
55 intrinsic. No type checking is strictly necessary, since only
56 valid types will be passed on. On the other hand, a simplification
57 subroutine may have to look at the type of an argument as part of
60 Array arguments are only passed to these subroutines that implement
61 the simplification of transformational intrinsics.
63 The functions in this file don't have much comment with them, but
64 everything is reasonably straight-forward. The Standard, chapter 13
65 is the best comment you'll find for this file anyway. */
67 /* Range checks an expression node. If all goes well, returns the
68 node, otherwise returns &gfc_bad_expr and frees the node. */
71 range_check (gfc_expr
*result
, const char *name
)
76 if (result
->expr_type
!= EXPR_CONSTANT
)
79 switch (gfc_range_check (result
))
85 gfc_error ("Result of %s overflows its kind at %L", name
,
90 gfc_error ("Result of %s underflows its kind at %L", name
,
95 gfc_error ("Result of %s is NaN at %L", name
, &result
->where
);
99 gfc_error ("Result of %s gives range error for its kind at %L", name
,
104 gfc_free_expr (result
);
105 return &gfc_bad_expr
;
109 /* A helper function that gets an optional and possibly missing
110 kind parameter. Returns the kind, -1 if something went wrong. */
113 get_kind (bt type
, gfc_expr
*k
, const char *name
, int default_kind
)
120 if (k
->expr_type
!= EXPR_CONSTANT
)
122 gfc_error ("KIND parameter of %s at %L must be an initialization "
123 "expression", name
, &k
->where
);
127 if (gfc_extract_int (k
, &kind
) != NULL
128 || gfc_validate_kind (type
, kind
, true) < 0)
130 gfc_error ("Invalid KIND parameter of %s at %L", name
, &k
->where
);
138 /* Converts an mpz_t signed variable into an unsigned one, assuming
139 two's complement representations and a binary width of bitsize.
140 The conversion is a no-op unless x is negative; otherwise, it can
141 be accomplished by masking out the high bits. */
144 convert_mpz_to_unsigned (mpz_t x
, int bitsize
)
150 /* Confirm that no bits above the signed range are unset. */
151 gcc_assert (mpz_scan0 (x
, bitsize
-1) == ULONG_MAX
);
153 mpz_init_set_ui (mask
, 1);
154 mpz_mul_2exp (mask
, mask
, bitsize
);
155 mpz_sub_ui (mask
, mask
, 1);
157 mpz_and (x
, x
, mask
);
163 /* Confirm that no bits above the signed range are set. */
164 gcc_assert (mpz_scan1 (x
, bitsize
-1) == ULONG_MAX
);
169 /* Converts an mpz_t unsigned variable into a signed one, assuming
170 two's complement representations and a binary width of bitsize.
171 If the bitsize-1 bit is set, this is taken as a sign bit and
172 the number is converted to the corresponding negative number. */
175 convert_mpz_to_signed (mpz_t x
, int bitsize
)
179 /* Confirm that no bits above the unsigned range are set. */
180 gcc_assert (mpz_scan1 (x
, bitsize
) == ULONG_MAX
);
182 if (mpz_tstbit (x
, bitsize
- 1) == 1)
184 mpz_init_set_ui (mask
, 1);
185 mpz_mul_2exp (mask
, mask
, bitsize
);
186 mpz_sub_ui (mask
, mask
, 1);
188 /* We negate the number by hand, zeroing the high bits, that is
189 make it the corresponding positive number, and then have it
190 negated by GMP, giving the correct representation of the
193 mpz_add_ui (x
, x
, 1);
194 mpz_and (x
, x
, mask
);
203 /* In-place convert BOZ to REAL of the specified kind. */
206 convert_boz (gfc_expr
*x
, int kind
)
208 if (x
&& x
->ts
.type
== BT_INTEGER
&& x
->is_boz
)
215 if (!gfc_convert_boz (x
, &ts
))
216 return &gfc_bad_expr
;
223 /* Test that the expression is an constant array. */
226 is_constant_array_expr (gfc_expr
*e
)
233 if (e
->expr_type
!= EXPR_ARRAY
|| !gfc_is_constant_expr (e
))
236 for (c
= gfc_constructor_first (e
->value
.constructor
);
237 c
; c
= gfc_constructor_next (c
))
238 if (c
->expr
->expr_type
!= EXPR_CONSTANT
239 && c
->expr
->expr_type
!= EXPR_STRUCTURE
)
246 /* Initialize a transformational result expression with a given value. */
249 init_result_expr (gfc_expr
*e
, int init
, gfc_expr
*array
)
251 if (e
&& e
->expr_type
== EXPR_ARRAY
)
253 gfc_constructor
*ctor
= gfc_constructor_first (e
->value
.constructor
);
256 init_result_expr (ctor
->expr
, init
, array
);
257 ctor
= gfc_constructor_next (ctor
);
260 else if (e
&& e
->expr_type
== EXPR_CONSTANT
)
262 int i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
269 e
->value
.logical
= (init
? 1 : 0);
274 mpz_set (e
->value
.integer
, gfc_integer_kinds
[i
].min_int
);
275 else if (init
== INT_MAX
)
276 mpz_set (e
->value
.integer
, gfc_integer_kinds
[i
].huge
);
278 mpz_set_si (e
->value
.integer
, init
);
284 mpfr_set (e
->value
.real
, gfc_real_kinds
[i
].huge
, GFC_RND_MODE
);
285 mpfr_neg (e
->value
.real
, e
->value
.real
, GFC_RND_MODE
);
287 else if (init
== INT_MAX
)
288 mpfr_set (e
->value
.real
, gfc_real_kinds
[i
].huge
, GFC_RND_MODE
);
290 mpfr_set_si (e
->value
.real
, init
, GFC_RND_MODE
);
294 mpc_set_si (e
->value
.complex, init
, GFC_MPC_RND_MODE
);
300 gfc_expr
*len
= gfc_simplify_len (array
, NULL
);
301 gfc_extract_int (len
, &length
);
302 string
= gfc_get_wide_string (length
+ 1);
303 gfc_wide_memset (string
, 0, length
);
305 else if (init
== INT_MAX
)
307 gfc_expr
*len
= gfc_simplify_len (array
, NULL
);
308 gfc_extract_int (len
, &length
);
309 string
= gfc_get_wide_string (length
+ 1);
310 gfc_wide_memset (string
, 255, length
);
315 string
= gfc_get_wide_string (1);
318 string
[length
] = '\0';
319 e
->value
.character
.length
= length
;
320 e
->value
.character
.string
= string
;
332 /* Helper function for gfc_simplify_dot_product() and gfc_simplify_matmul. */
335 compute_dot_product (gfc_expr
*matrix_a
, int stride_a
, int offset_a
,
336 gfc_expr
*matrix_b
, int stride_b
, int offset_b
)
338 gfc_expr
*result
, *a
, *b
;
340 result
= gfc_get_constant_expr (matrix_a
->ts
.type
, matrix_a
->ts
.kind
,
342 init_result_expr (result
, 0, NULL
);
344 a
= gfc_constructor_lookup_expr (matrix_a
->value
.constructor
, offset_a
);
345 b
= gfc_constructor_lookup_expr (matrix_b
->value
.constructor
, offset_b
);
348 /* Copying of expressions is required as operands are free'd
349 by the gfc_arith routines. */
350 switch (result
->ts
.type
)
353 result
= gfc_or (result
,
354 gfc_and (gfc_copy_expr (a
),
361 result
= gfc_add (result
,
362 gfc_multiply (gfc_copy_expr (a
),
370 offset_a
+= stride_a
;
371 a
= gfc_constructor_lookup_expr (matrix_a
->value
.constructor
, offset_a
);
373 offset_b
+= stride_b
;
374 b
= gfc_constructor_lookup_expr (matrix_b
->value
.constructor
, offset_b
);
381 /* Build a result expression for transformational intrinsics,
385 transformational_result (gfc_expr
*array
, gfc_expr
*dim
, bt type
,
386 int kind
, locus
* where
)
391 if (!dim
|| array
->rank
== 1)
392 return gfc_get_constant_expr (type
, kind
, where
);
394 result
= gfc_get_array_expr (type
, kind
, where
);
395 result
->shape
= gfc_copy_shape_excluding (array
->shape
, array
->rank
, dim
);
396 result
->rank
= array
->rank
- 1;
398 /* gfc_array_size() would count the number of elements in the constructor,
399 we have not built those yet. */
401 for (i
= 0; i
< result
->rank
; ++i
)
402 nelem
*= mpz_get_ui (result
->shape
[i
]);
404 for (i
= 0; i
< nelem
; ++i
)
406 gfc_constructor_append_expr (&result
->value
.constructor
,
407 gfc_get_constant_expr (type
, kind
, where
),
415 typedef gfc_expr
* (*transformational_op
)(gfc_expr
*, gfc_expr
*);
417 /* Wrapper function, implements 'op1 += 1'. Only called if MASK
418 of COUNT intrinsic is .TRUE..
420 Interface and implimentation mimics arith functions as
421 gfc_add, gfc_multiply, etc. */
423 static gfc_expr
* gfc_count (gfc_expr
*op1
, gfc_expr
*op2
)
427 gcc_assert (op1
->ts
.type
== BT_INTEGER
);
428 gcc_assert (op2
->ts
.type
== BT_LOGICAL
);
429 gcc_assert (op2
->value
.logical
);
431 result
= gfc_copy_expr (op1
);
432 mpz_add_ui (result
->value
.integer
, result
->value
.integer
, 1);
440 /* Transforms an ARRAY with operation OP, according to MASK, to a
441 scalar RESULT. E.g. called if
443 REAL, PARAMETER :: array(n, m) = ...
444 REAL, PARAMETER :: s = SUM(array)
446 where OP == gfc_add(). */
449 simplify_transformation_to_scalar (gfc_expr
*result
, gfc_expr
*array
, gfc_expr
*mask
,
450 transformational_op op
)
453 gfc_constructor
*array_ctor
, *mask_ctor
;
455 /* Shortcut for constant .FALSE. MASK. */
457 && mask
->expr_type
== EXPR_CONSTANT
458 && !mask
->value
.logical
)
461 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
463 if (mask
&& mask
->expr_type
== EXPR_ARRAY
)
464 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
468 a
= array_ctor
->expr
;
469 array_ctor
= gfc_constructor_next (array_ctor
);
471 /* A constant MASK equals .TRUE. here and can be ignored. */
475 mask_ctor
= gfc_constructor_next (mask_ctor
);
476 if (!m
->value
.logical
)
480 result
= op (result
, gfc_copy_expr (a
));
486 /* Transforms an ARRAY with operation OP, according to MASK, to an
487 array RESULT. E.g. called if
489 REAL, PARAMETER :: array(n, m) = ...
490 REAL, PARAMETER :: s(n) = PROD(array, DIM=1)
492 where OP == gfc_multiply(). The result might be post processed using post_op. */
495 simplify_transformation_to_array (gfc_expr
*result
, gfc_expr
*array
, gfc_expr
*dim
,
496 gfc_expr
*mask
, transformational_op op
,
497 transformational_op post_op
)
500 int done
, i
, n
, arraysize
, resultsize
, dim_index
, dim_extent
, dim_stride
;
501 gfc_expr
**arrayvec
, **resultvec
, **base
, **src
, **dest
;
502 gfc_constructor
*array_ctor
, *mask_ctor
, *result_ctor
;
504 int count
[GFC_MAX_DIMENSIONS
], extent
[GFC_MAX_DIMENSIONS
],
505 sstride
[GFC_MAX_DIMENSIONS
], dstride
[GFC_MAX_DIMENSIONS
],
506 tmpstride
[GFC_MAX_DIMENSIONS
];
508 /* Shortcut for constant .FALSE. MASK. */
510 && mask
->expr_type
== EXPR_CONSTANT
511 && !mask
->value
.logical
)
514 /* Build an indexed table for array element expressions to minimize
515 linked-list traversal. Masked elements are set to NULL. */
516 gfc_array_size (array
, &size
);
517 arraysize
= mpz_get_ui (size
);
519 arrayvec
= (gfc_expr
**) gfc_getmem (sizeof (gfc_expr
*) * arraysize
);
521 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
523 if (mask
&& mask
->expr_type
== EXPR_ARRAY
)
524 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
526 for (i
= 0; i
< arraysize
; ++i
)
528 arrayvec
[i
] = array_ctor
->expr
;
529 array_ctor
= gfc_constructor_next (array_ctor
);
533 if (!mask_ctor
->expr
->value
.logical
)
536 mask_ctor
= gfc_constructor_next (mask_ctor
);
540 /* Same for the result expression. */
541 gfc_array_size (result
, &size
);
542 resultsize
= mpz_get_ui (size
);
545 resultvec
= (gfc_expr
**) gfc_getmem (sizeof (gfc_expr
*) * resultsize
);
546 result_ctor
= gfc_constructor_first (result
->value
.constructor
);
547 for (i
= 0; i
< resultsize
; ++i
)
549 resultvec
[i
] = result_ctor
->expr
;
550 result_ctor
= gfc_constructor_next (result_ctor
);
553 gfc_extract_int (dim
, &dim_index
);
554 dim_index
-= 1; /* zero-base index */
558 for (i
= 0, n
= 0; i
< array
->rank
; ++i
)
561 tmpstride
[i
] = (i
== 0) ? 1 : tmpstride
[i
-1] * mpz_get_si (array
->shape
[i
-1]);
564 dim_extent
= mpz_get_si (array
->shape
[i
]);
565 dim_stride
= tmpstride
[i
];
569 extent
[n
] = mpz_get_si (array
->shape
[i
]);
570 sstride
[n
] = tmpstride
[i
];
571 dstride
[n
] = (n
== 0) ? 1 : dstride
[n
-1] * extent
[n
-1];
580 for (src
= base
, n
= 0; n
< dim_extent
; src
+= dim_stride
, ++n
)
582 *dest
= op (*dest
, gfc_copy_expr (*src
));
589 while (!done
&& count
[n
] == extent
[n
])
592 base
-= sstride
[n
] * extent
[n
];
593 dest
-= dstride
[n
] * extent
[n
];
596 if (n
< result
->rank
)
607 /* Place updated expression in result constructor. */
608 result_ctor
= gfc_constructor_first (result
->value
.constructor
);
609 for (i
= 0; i
< resultsize
; ++i
)
612 result_ctor
->expr
= post_op (result_ctor
->expr
, resultvec
[i
]);
614 result_ctor
->expr
= resultvec
[i
];
615 result_ctor
= gfc_constructor_next (result_ctor
);
619 gfc_free (resultvec
);
625 simplify_transformation (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
,
626 int init_val
, transformational_op op
)
630 if (!is_constant_array_expr (array
)
631 || !gfc_is_constant_expr (dim
))
635 && !is_constant_array_expr (mask
)
636 && mask
->expr_type
!= EXPR_CONSTANT
)
639 result
= transformational_result (array
, dim
, array
->ts
.type
,
640 array
->ts
.kind
, &array
->where
);
641 init_result_expr (result
, init_val
, NULL
);
643 return !dim
|| array
->rank
== 1 ?
644 simplify_transformation_to_scalar (result
, array
, mask
, op
) :
645 simplify_transformation_to_array (result
, array
, dim
, mask
, op
, NULL
);
649 /********************** Simplification functions *****************************/
652 gfc_simplify_abs (gfc_expr
*e
)
656 if (e
->expr_type
!= EXPR_CONSTANT
)
662 result
= gfc_get_constant_expr (BT_INTEGER
, e
->ts
.kind
, &e
->where
);
663 mpz_abs (result
->value
.integer
, e
->value
.integer
);
664 return range_check (result
, "IABS");
667 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
668 mpfr_abs (result
->value
.real
, e
->value
.real
, GFC_RND_MODE
);
669 return range_check (result
, "ABS");
672 gfc_set_model_kind (e
->ts
.kind
);
673 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
674 mpc_abs (result
->value
.real
, e
->value
.complex, GFC_RND_MODE
);
675 return range_check (result
, "CABS");
678 gfc_internal_error ("gfc_simplify_abs(): Bad type");
684 simplify_achar_char (gfc_expr
*e
, gfc_expr
*k
, const char *name
, bool ascii
)
688 bool too_large
= false;
690 if (e
->expr_type
!= EXPR_CONSTANT
)
693 kind
= get_kind (BT_CHARACTER
, k
, name
, gfc_default_character_kind
);
695 return &gfc_bad_expr
;
697 if (mpz_cmp_si (e
->value
.integer
, 0) < 0)
699 gfc_error ("Argument of %s function at %L is negative", name
,
701 return &gfc_bad_expr
;
704 if (ascii
&& gfc_option
.warn_surprising
705 && mpz_cmp_si (e
->value
.integer
, 127) > 0)
706 gfc_warning ("Argument of %s function at %L outside of range [0,127]",
709 if (kind
== 1 && mpz_cmp_si (e
->value
.integer
, 255) > 0)
714 mpz_init_set_ui (t
, 2);
715 mpz_pow_ui (t
, t
, 32);
716 mpz_sub_ui (t
, t
, 1);
717 if (mpz_cmp (e
->value
.integer
, t
) > 0)
724 gfc_error ("Argument of %s function at %L is too large for the "
725 "collating sequence of kind %d", name
, &e
->where
, kind
);
726 return &gfc_bad_expr
;
729 result
= gfc_get_character_expr (kind
, &e
->where
, NULL
, 1);
730 result
->value
.character
.string
[0] = mpz_get_ui (e
->value
.integer
);
737 /* We use the processor's collating sequence, because all
738 systems that gfortran currently works on are ASCII. */
741 gfc_simplify_achar (gfc_expr
*e
, gfc_expr
*k
)
743 return simplify_achar_char (e
, k
, "ACHAR", true);
748 gfc_simplify_acos (gfc_expr
*x
)
752 if (x
->expr_type
!= EXPR_CONSTANT
)
758 if (mpfr_cmp_si (x
->value
.real
, 1) > 0
759 || mpfr_cmp_si (x
->value
.real
, -1) < 0)
761 gfc_error ("Argument of ACOS at %L must be between -1 and 1",
763 return &gfc_bad_expr
;
765 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
766 mpfr_acos (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
770 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
771 mpc_acos (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
775 gfc_internal_error ("in gfc_simplify_acos(): Bad type");
778 return range_check (result
, "ACOS");
782 gfc_simplify_acosh (gfc_expr
*x
)
786 if (x
->expr_type
!= EXPR_CONSTANT
)
792 if (mpfr_cmp_si (x
->value
.real
, 1) < 0)
794 gfc_error ("Argument of ACOSH at %L must not be less than 1",
796 return &gfc_bad_expr
;
799 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
800 mpfr_acosh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
804 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
805 mpc_acosh (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
809 gfc_internal_error ("in gfc_simplify_acosh(): Bad type");
812 return range_check (result
, "ACOSH");
816 gfc_simplify_adjustl (gfc_expr
*e
)
822 if (e
->expr_type
!= EXPR_CONSTANT
)
825 len
= e
->value
.character
.length
;
827 for (count
= 0, i
= 0; i
< len
; ++i
)
829 ch
= e
->value
.character
.string
[i
];
835 result
= gfc_get_character_expr (e
->ts
.kind
, &e
->where
, NULL
, len
);
836 for (i
= 0; i
< len
- count
; ++i
)
837 result
->value
.character
.string
[i
] = e
->value
.character
.string
[count
+ i
];
844 gfc_simplify_adjustr (gfc_expr
*e
)
850 if (e
->expr_type
!= EXPR_CONSTANT
)
853 len
= e
->value
.character
.length
;
855 for (count
= 0, i
= len
- 1; i
>= 0; --i
)
857 ch
= e
->value
.character
.string
[i
];
863 result
= gfc_get_character_expr (e
->ts
.kind
, &e
->where
, NULL
, len
);
864 for (i
= 0; i
< count
; ++i
)
865 result
->value
.character
.string
[i
] = ' ';
867 for (i
= count
; i
< len
; ++i
)
868 result
->value
.character
.string
[i
] = e
->value
.character
.string
[i
- count
];
875 gfc_simplify_aimag (gfc_expr
*e
)
879 if (e
->expr_type
!= EXPR_CONSTANT
)
882 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
883 mpfr_set (result
->value
.real
, mpc_imagref (e
->value
.complex), GFC_RND_MODE
);
885 return range_check (result
, "AIMAG");
890 gfc_simplify_aint (gfc_expr
*e
, gfc_expr
*k
)
892 gfc_expr
*rtrunc
, *result
;
895 kind
= get_kind (BT_REAL
, k
, "AINT", e
->ts
.kind
);
897 return &gfc_bad_expr
;
899 if (e
->expr_type
!= EXPR_CONSTANT
)
902 rtrunc
= gfc_copy_expr (e
);
903 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
905 result
= gfc_real2real (rtrunc
, kind
);
907 gfc_free_expr (rtrunc
);
909 return range_check (result
, "AINT");
914 gfc_simplify_all (gfc_expr
*mask
, gfc_expr
*dim
)
916 return simplify_transformation (mask
, dim
, NULL
, true, gfc_and
);
921 gfc_simplify_dint (gfc_expr
*e
)
923 gfc_expr
*rtrunc
, *result
;
925 if (e
->expr_type
!= EXPR_CONSTANT
)
928 rtrunc
= gfc_copy_expr (e
);
929 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
931 result
= gfc_real2real (rtrunc
, gfc_default_double_kind
);
933 gfc_free_expr (rtrunc
);
935 return range_check (result
, "DINT");
940 gfc_simplify_anint (gfc_expr
*e
, gfc_expr
*k
)
945 kind
= get_kind (BT_REAL
, k
, "ANINT", e
->ts
.kind
);
947 return &gfc_bad_expr
;
949 if (e
->expr_type
!= EXPR_CONSTANT
)
952 result
= gfc_get_constant_expr (e
->ts
.type
, kind
, &e
->where
);
953 mpfr_round (result
->value
.real
, e
->value
.real
);
955 return range_check (result
, "ANINT");
960 gfc_simplify_and (gfc_expr
*x
, gfc_expr
*y
)
965 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
968 kind
= x
->ts
.kind
> y
->ts
.kind
? x
->ts
.kind
: y
->ts
.kind
;
973 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &x
->where
);
974 mpz_and (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
975 return range_check (result
, "AND");
978 return gfc_get_logical_expr (kind
, &x
->where
,
979 x
->value
.logical
&& y
->value
.logical
);
988 gfc_simplify_any (gfc_expr
*mask
, gfc_expr
*dim
)
990 return simplify_transformation (mask
, dim
, NULL
, false, gfc_or
);
995 gfc_simplify_dnint (gfc_expr
*e
)
999 if (e
->expr_type
!= EXPR_CONSTANT
)
1002 result
= gfc_get_constant_expr (BT_REAL
, gfc_default_double_kind
, &e
->where
);
1003 mpfr_round (result
->value
.real
, e
->value
.real
);
1005 return range_check (result
, "DNINT");
1010 gfc_simplify_asin (gfc_expr
*x
)
1014 if (x
->expr_type
!= EXPR_CONSTANT
)
1020 if (mpfr_cmp_si (x
->value
.real
, 1) > 0
1021 || mpfr_cmp_si (x
->value
.real
, -1) < 0)
1023 gfc_error ("Argument of ASIN at %L must be between -1 and 1",
1025 return &gfc_bad_expr
;
1027 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1028 mpfr_asin (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1032 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1033 mpc_asin (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1037 gfc_internal_error ("in gfc_simplify_asin(): Bad type");
1040 return range_check (result
, "ASIN");
1045 gfc_simplify_asinh (gfc_expr
*x
)
1049 if (x
->expr_type
!= EXPR_CONSTANT
)
1052 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1057 mpfr_asinh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1061 mpc_asinh (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1065 gfc_internal_error ("in gfc_simplify_asinh(): Bad type");
1068 return range_check (result
, "ASINH");
1073 gfc_simplify_atan (gfc_expr
*x
)
1077 if (x
->expr_type
!= EXPR_CONSTANT
)
1080 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1085 mpfr_atan (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1089 mpc_atan (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1093 gfc_internal_error ("in gfc_simplify_atan(): Bad type");
1096 return range_check (result
, "ATAN");
1101 gfc_simplify_atanh (gfc_expr
*x
)
1105 if (x
->expr_type
!= EXPR_CONSTANT
)
1111 if (mpfr_cmp_si (x
->value
.real
, 1) >= 0
1112 || mpfr_cmp_si (x
->value
.real
, -1) <= 0)
1114 gfc_error ("Argument of ATANH at %L must be inside the range -1 "
1116 return &gfc_bad_expr
;
1118 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1119 mpfr_atanh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1123 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1124 mpc_atanh (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1128 gfc_internal_error ("in gfc_simplify_atanh(): Bad type");
1131 return range_check (result
, "ATANH");
1136 gfc_simplify_atan2 (gfc_expr
*y
, gfc_expr
*x
)
1140 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1143 if (mpfr_sgn (y
->value
.real
) == 0 && mpfr_sgn (x
->value
.real
) == 0)
1145 gfc_error ("If first argument of ATAN2 %L is zero, then the "
1146 "second argument must not be zero", &x
->where
);
1147 return &gfc_bad_expr
;
1150 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1151 mpfr_atan2 (result
->value
.real
, y
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1153 return range_check (result
, "ATAN2");
1158 gfc_simplify_bessel_j0 (gfc_expr
*x
)
1162 if (x
->expr_type
!= EXPR_CONSTANT
)
1165 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1166 mpfr_j0 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1168 return range_check (result
, "BESSEL_J0");
1173 gfc_simplify_bessel_j1 (gfc_expr
*x
)
1177 if (x
->expr_type
!= EXPR_CONSTANT
)
1180 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1181 mpfr_j1 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1183 return range_check (result
, "BESSEL_J1");
1188 gfc_simplify_bessel_jn (gfc_expr
*order
, gfc_expr
*x
)
1193 if (x
->expr_type
!= EXPR_CONSTANT
|| order
->expr_type
!= EXPR_CONSTANT
)
1196 n
= mpz_get_si (order
->value
.integer
);
1197 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1198 mpfr_jn (result
->value
.real
, n
, x
->value
.real
, GFC_RND_MODE
);
1200 return range_check (result
, "BESSEL_JN");
1204 /* Simplify transformational form of JN and YN. */
1207 gfc_simplify_bessel_n2 (gfc_expr
*order1
, gfc_expr
*order2
, gfc_expr
*x
,
1214 mpfr_t x2rev
, last1
, last2
;
1216 if (x
->expr_type
!= EXPR_CONSTANT
|| order1
->expr_type
!= EXPR_CONSTANT
1217 || order2
->expr_type
!= EXPR_CONSTANT
)
1220 n1
= mpz_get_si (order1
->value
.integer
);
1221 n2
= mpz_get_si (order2
->value
.integer
);
1222 result
= gfc_get_array_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1224 result
->shape
= gfc_get_shape (1);
1225 mpz_init_set_ui (result
->shape
[0], MAX (n2
-n1
+1, 0));
1230 /* Special case: x == 0; it is J0(0.0) == 1, JN(N > 0, 0.0) == 0; and
1231 YN(N, 0.0) = -Inf. */
1233 if (mpfr_cmp_ui (x
->value
.real
, 0.0) == 0)
1235 if (!jn
&& gfc_option
.flag_range_check
)
1237 gfc_error ("Result of BESSEL_YN is -INF at %L", &result
->where
);
1238 gfc_free_expr (result
);
1239 return &gfc_bad_expr
;
1244 e
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1245 mpfr_set_ui (e
->value
.real
, 1, GFC_RND_MODE
);
1246 gfc_constructor_append_expr (&result
->value
.constructor
, e
,
1251 for (i
= n1
; i
<= n2
; i
++)
1253 e
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1255 mpfr_set_ui (e
->value
.real
, 0, GFC_RND_MODE
);
1257 mpfr_set_inf (e
->value
.real
, -1);
1258 gfc_constructor_append_expr (&result
->value
.constructor
, e
,
1265 /* Use the faster but more verbose recurrence algorithm. Bessel functions
1266 are stable for downward recursion and Neumann functions are stable
1267 for upward recursion. It is
1269 J(N-1, x) = x2rev * N * J(N, x) - J(N+1, x),
1270 Y(N+1, x) = x2rev * N * Y(N, x) - Y(N-1, x).
1271 Cf. http://dlmf.nist.gov/10.74#iv and http://dlmf.nist.gov/10.6#E1 */
1273 gfc_set_model_kind (x
->ts
.kind
);
1275 /* Get first recursion anchor. */
1279 mpfr_jn (last1
, n2
, x
->value
.real
, GFC_RND_MODE
);
1281 mpfr_yn (last1
, n1
, x
->value
.real
, GFC_RND_MODE
);
1283 e
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1284 mpfr_set (e
->value
.real
, last1
, GFC_RND_MODE
);
1285 if (range_check (e
, jn
? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr
)
1289 gfc_free_expr (result
);
1290 return &gfc_bad_expr
;
1292 gfc_constructor_append_expr (&result
->value
.constructor
, e
, &x
->where
);
1300 /* Get second recursion anchor. */
1304 mpfr_jn (last2
, n2
-1, x
->value
.real
, GFC_RND_MODE
);
1306 mpfr_yn (last2
, n1
+1, x
->value
.real
, GFC_RND_MODE
);
1308 e
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1309 mpfr_set (e
->value
.real
, last2
, GFC_RND_MODE
);
1310 if (range_check (e
, jn
? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr
)
1315 gfc_free_expr (result
);
1316 return &gfc_bad_expr
;
1319 gfc_constructor_insert_expr (&result
->value
.constructor
, e
, &x
->where
, -2);
1321 gfc_constructor_append_expr (&result
->value
.constructor
, e
, &x
->where
);
1330 /* Start actual recursion. */
1333 mpfr_ui_div (x2rev
, 2, x
->value
.real
, GFC_RND_MODE
);
1335 for (i
= 2; i
<= n2
-n1
; i
++)
1337 e
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1339 /* Special case: For YN, if the previous N gave -INF, set
1340 also N+1 to -INF. */
1341 if (!jn
&& !gfc_option
.flag_range_check
&& mpfr_inf_p (last2
))
1343 mpfr_set_inf (e
->value
.real
, -1);
1344 gfc_constructor_append_expr (&result
->value
.constructor
, e
,
1349 mpfr_mul_si (e
->value
.real
, x2rev
, jn
? (n2
-i
+1) : (n1
+i
-1),
1351 mpfr_mul (e
->value
.real
, e
->value
.real
, last2
, GFC_RND_MODE
);
1352 mpfr_sub (e
->value
.real
, e
->value
.real
, last1
, GFC_RND_MODE
);
1354 if (range_check (e
, jn
? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr
)
1358 gfc_constructor_insert_expr (&result
->value
.constructor
, e
, &x
->where
,
1361 gfc_constructor_append_expr (&result
->value
.constructor
, e
, &x
->where
);
1363 mpfr_set (last1
, last2
, GFC_RND_MODE
);
1364 mpfr_set (last2
, e
->value
.real
, GFC_RND_MODE
);
1377 gfc_free_expr (result
);
1378 return &gfc_bad_expr
;
1383 gfc_simplify_bessel_jn2 (gfc_expr
*order1
, gfc_expr
*order2
, gfc_expr
*x
)
1385 return gfc_simplify_bessel_n2 (order1
, order2
, x
, true);
1390 gfc_simplify_bessel_y0 (gfc_expr
*x
)
1394 if (x
->expr_type
!= EXPR_CONSTANT
)
1397 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1398 mpfr_y0 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1400 return range_check (result
, "BESSEL_Y0");
1405 gfc_simplify_bessel_y1 (gfc_expr
*x
)
1409 if (x
->expr_type
!= EXPR_CONSTANT
)
1412 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1413 mpfr_y1 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1415 return range_check (result
, "BESSEL_Y1");
1420 gfc_simplify_bessel_yn (gfc_expr
*order
, gfc_expr
*x
)
1425 if (x
->expr_type
!= EXPR_CONSTANT
|| order
->expr_type
!= EXPR_CONSTANT
)
1428 n
= mpz_get_si (order
->value
.integer
);
1429 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1430 mpfr_yn (result
->value
.real
, n
, x
->value
.real
, GFC_RND_MODE
);
1432 return range_check (result
, "BESSEL_YN");
1437 gfc_simplify_bessel_yn2 (gfc_expr
*order1
, gfc_expr
*order2
, gfc_expr
*x
)
1439 return gfc_simplify_bessel_n2 (order1
, order2
, x
, false);
1444 gfc_simplify_bit_size (gfc_expr
*e
)
1446 int i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
1447 return gfc_get_int_expr (e
->ts
.kind
, &e
->where
,
1448 gfc_integer_kinds
[i
].bit_size
);
1453 gfc_simplify_btest (gfc_expr
*e
, gfc_expr
*bit
)
1457 if (e
->expr_type
!= EXPR_CONSTANT
|| bit
->expr_type
!= EXPR_CONSTANT
)
1460 if (gfc_extract_int (bit
, &b
) != NULL
|| b
< 0)
1461 return gfc_get_logical_expr (gfc_default_logical_kind
, &e
->where
, false);
1463 return gfc_get_logical_expr (gfc_default_logical_kind
, &e
->where
,
1464 mpz_tstbit (e
->value
.integer
, b
));
1469 compare_bitwise (gfc_expr
*i
, gfc_expr
*j
)
1474 gcc_assert (i
->ts
.type
== BT_INTEGER
);
1475 gcc_assert (j
->ts
.type
== BT_INTEGER
);
1477 mpz_init_set (x
, i
->value
.integer
);
1478 k
= gfc_validate_kind (i
->ts
.type
, i
->ts
.kind
, false);
1479 convert_mpz_to_unsigned (x
, gfc_integer_kinds
[k
].bit_size
);
1481 mpz_init_set (y
, j
->value
.integer
);
1482 k
= gfc_validate_kind (j
->ts
.type
, j
->ts
.kind
, false);
1483 convert_mpz_to_unsigned (y
, gfc_integer_kinds
[k
].bit_size
);
1485 res
= mpz_cmp (x
, y
);
1493 gfc_simplify_bge (gfc_expr
*i
, gfc_expr
*j
)
1495 if (i
->expr_type
!= EXPR_CONSTANT
|| j
->expr_type
!= EXPR_CONSTANT
)
1498 return gfc_get_logical_expr (gfc_default_logical_kind
, &i
->where
,
1499 compare_bitwise (i
, j
) >= 0);
1504 gfc_simplify_bgt (gfc_expr
*i
, gfc_expr
*j
)
1506 if (i
->expr_type
!= EXPR_CONSTANT
|| j
->expr_type
!= EXPR_CONSTANT
)
1509 return gfc_get_logical_expr (gfc_default_logical_kind
, &i
->where
,
1510 compare_bitwise (i
, j
) > 0);
1515 gfc_simplify_ble (gfc_expr
*i
, gfc_expr
*j
)
1517 if (i
->expr_type
!= EXPR_CONSTANT
|| j
->expr_type
!= EXPR_CONSTANT
)
1520 return gfc_get_logical_expr (gfc_default_logical_kind
, &i
->where
,
1521 compare_bitwise (i
, j
) <= 0);
1526 gfc_simplify_blt (gfc_expr
*i
, gfc_expr
*j
)
1528 if (i
->expr_type
!= EXPR_CONSTANT
|| j
->expr_type
!= EXPR_CONSTANT
)
1531 return gfc_get_logical_expr (gfc_default_logical_kind
, &i
->where
,
1532 compare_bitwise (i
, j
) < 0);
1537 gfc_simplify_ceiling (gfc_expr
*e
, gfc_expr
*k
)
1539 gfc_expr
*ceil
, *result
;
1542 kind
= get_kind (BT_INTEGER
, k
, "CEILING", gfc_default_integer_kind
);
1544 return &gfc_bad_expr
;
1546 if (e
->expr_type
!= EXPR_CONSTANT
)
1549 ceil
= gfc_copy_expr (e
);
1550 mpfr_ceil (ceil
->value
.real
, e
->value
.real
);
1552 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &e
->where
);
1553 gfc_mpfr_to_mpz (result
->value
.integer
, ceil
->value
.real
, &e
->where
);
1555 gfc_free_expr (ceil
);
1557 return range_check (result
, "CEILING");
1562 gfc_simplify_char (gfc_expr
*e
, gfc_expr
*k
)
1564 return simplify_achar_char (e
, k
, "CHAR", false);
1568 /* Common subroutine for simplifying CMPLX, COMPLEX and DCMPLX. */
1571 simplify_cmplx (const char *name
, gfc_expr
*x
, gfc_expr
*y
, int kind
)
1575 if (convert_boz (x
, kind
) == &gfc_bad_expr
)
1576 return &gfc_bad_expr
;
1578 if (convert_boz (y
, kind
) == &gfc_bad_expr
)
1579 return &gfc_bad_expr
;
1581 if (x
->expr_type
!= EXPR_CONSTANT
1582 || (y
!= NULL
&& y
->expr_type
!= EXPR_CONSTANT
))
1585 result
= gfc_get_constant_expr (BT_COMPLEX
, kind
, &x
->where
);
1590 mpc_set_z (result
->value
.complex, x
->value
.integer
, GFC_MPC_RND_MODE
);
1594 mpc_set_fr (result
->value
.complex, x
->value
.real
, GFC_RND_MODE
);
1598 mpc_set (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1602 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (x)");
1606 return range_check (result
, name
);
1611 mpfr_set_z (mpc_imagref (result
->value
.complex),
1612 y
->value
.integer
, GFC_RND_MODE
);
1616 mpfr_set (mpc_imagref (result
->value
.complex),
1617 y
->value
.real
, GFC_RND_MODE
);
1621 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (y)");
1624 return range_check (result
, name
);
1629 gfc_simplify_cmplx (gfc_expr
*x
, gfc_expr
*y
, gfc_expr
*k
)
1633 kind
= get_kind (BT_REAL
, k
, "CMPLX", gfc_default_complex_kind
);
1635 return &gfc_bad_expr
;
1637 return simplify_cmplx ("CMPLX", x
, y
, kind
);
1642 gfc_simplify_complex (gfc_expr
*x
, gfc_expr
*y
)
1646 if (x
->ts
.type
== BT_INTEGER
&& y
->ts
.type
== BT_INTEGER
)
1647 kind
= gfc_default_complex_kind
;
1648 else if (x
->ts
.type
== BT_REAL
|| y
->ts
.type
== BT_INTEGER
)
1650 else if (x
->ts
.type
== BT_INTEGER
|| y
->ts
.type
== BT_REAL
)
1652 else if (x
->ts
.type
== BT_REAL
&& y
->ts
.type
== BT_REAL
)
1653 kind
= (x
->ts
.kind
> y
->ts
.kind
) ? x
->ts
.kind
: y
->ts
.kind
;
1657 return simplify_cmplx ("COMPLEX", x
, y
, kind
);
1662 gfc_simplify_conjg (gfc_expr
*e
)
1666 if (e
->expr_type
!= EXPR_CONSTANT
)
1669 result
= gfc_copy_expr (e
);
1670 mpc_conj (result
->value
.complex, result
->value
.complex, GFC_MPC_RND_MODE
);
1672 return range_check (result
, "CONJG");
1677 gfc_simplify_cos (gfc_expr
*x
)
1681 if (x
->expr_type
!= EXPR_CONSTANT
)
1684 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1689 mpfr_cos (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1693 gfc_set_model_kind (x
->ts
.kind
);
1694 mpc_cos (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1698 gfc_internal_error ("in gfc_simplify_cos(): Bad type");
1701 return range_check (result
, "COS");
1706 gfc_simplify_cosh (gfc_expr
*x
)
1710 if (x
->expr_type
!= EXPR_CONSTANT
)
1713 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1718 mpfr_cosh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1722 mpc_cosh (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1729 return range_check (result
, "COSH");
1734 gfc_simplify_count (gfc_expr
*mask
, gfc_expr
*dim
, gfc_expr
*kind
)
1738 if (!is_constant_array_expr (mask
)
1739 || !gfc_is_constant_expr (dim
)
1740 || !gfc_is_constant_expr (kind
))
1743 result
= transformational_result (mask
, dim
,
1745 get_kind (BT_INTEGER
, kind
, "COUNT",
1746 gfc_default_integer_kind
),
1749 init_result_expr (result
, 0, NULL
);
1751 /* Passing MASK twice, once as data array, once as mask.
1752 Whenever gfc_count is called, '1' is added to the result. */
1753 return !dim
|| mask
->rank
== 1 ?
1754 simplify_transformation_to_scalar (result
, mask
, mask
, gfc_count
) :
1755 simplify_transformation_to_array (result
, mask
, dim
, mask
, gfc_count
, NULL
);
1760 gfc_simplify_dcmplx (gfc_expr
*x
, gfc_expr
*y
)
1762 return simplify_cmplx ("DCMPLX", x
, y
, gfc_default_double_kind
);
1767 gfc_simplify_dble (gfc_expr
*e
)
1769 gfc_expr
*result
= NULL
;
1771 if (e
->expr_type
!= EXPR_CONSTANT
)
1774 if (convert_boz (e
, gfc_default_double_kind
) == &gfc_bad_expr
)
1775 return &gfc_bad_expr
;
1777 result
= gfc_convert_constant (e
, BT_REAL
, gfc_default_double_kind
);
1778 if (result
== &gfc_bad_expr
)
1779 return &gfc_bad_expr
;
1781 return range_check (result
, "DBLE");
1786 gfc_simplify_digits (gfc_expr
*x
)
1790 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
1795 digits
= gfc_integer_kinds
[i
].digits
;
1800 digits
= gfc_real_kinds
[i
].digits
;
1807 return gfc_get_int_expr (gfc_default_integer_kind
, NULL
, digits
);
1812 gfc_simplify_dim (gfc_expr
*x
, gfc_expr
*y
)
1817 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1820 kind
= x
->ts
.kind
> y
->ts
.kind
? x
->ts
.kind
: y
->ts
.kind
;
1821 result
= gfc_get_constant_expr (x
->ts
.type
, kind
, &x
->where
);
1826 if (mpz_cmp (x
->value
.integer
, y
->value
.integer
) > 0)
1827 mpz_sub (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
1829 mpz_set_ui (result
->value
.integer
, 0);
1834 if (mpfr_cmp (x
->value
.real
, y
->value
.real
) > 0)
1835 mpfr_sub (result
->value
.real
, x
->value
.real
, y
->value
.real
,
1838 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
1843 gfc_internal_error ("gfc_simplify_dim(): Bad type");
1846 return range_check (result
, "DIM");
1851 gfc_simplify_dot_product (gfc_expr
*vector_a
, gfc_expr
*vector_b
)
1853 if (!is_constant_array_expr (vector_a
)
1854 || !is_constant_array_expr (vector_b
))
1857 gcc_assert (vector_a
->rank
== 1);
1858 gcc_assert (vector_b
->rank
== 1);
1859 gcc_assert (gfc_compare_types (&vector_a
->ts
, &vector_b
->ts
));
1861 return compute_dot_product (vector_a
, 1, 0, vector_b
, 1, 0);
1866 gfc_simplify_dprod (gfc_expr
*x
, gfc_expr
*y
)
1868 gfc_expr
*a1
, *a2
, *result
;
1870 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1873 a1
= gfc_real2real (x
, gfc_default_double_kind
);
1874 a2
= gfc_real2real (y
, gfc_default_double_kind
);
1876 result
= gfc_get_constant_expr (BT_REAL
, gfc_default_double_kind
, &x
->where
);
1877 mpfr_mul (result
->value
.real
, a1
->value
.real
, a2
->value
.real
, GFC_RND_MODE
);
1882 return range_check (result
, "DPROD");
1887 simplify_dshift (gfc_expr
*arg1
, gfc_expr
*arg2
, gfc_expr
*shiftarg
,
1891 int i
, k
, size
, shift
;
1893 if (arg1
->expr_type
!= EXPR_CONSTANT
|| arg2
->expr_type
!= EXPR_CONSTANT
1894 || shiftarg
->expr_type
!= EXPR_CONSTANT
)
1897 k
= gfc_validate_kind (BT_INTEGER
, arg1
->ts
.kind
, false);
1898 size
= gfc_integer_kinds
[k
].bit_size
;
1900 if (gfc_extract_int (shiftarg
, &shift
) != NULL
)
1902 gfc_error ("Invalid SHIFT argument of DSHIFTL at %L", &shiftarg
->where
);
1903 return &gfc_bad_expr
;
1906 gcc_assert (shift
>= 0 && shift
<= size
);
1908 /* DSHIFTR(I,J,SHIFT) = DSHIFTL(I,J,SIZE-SHIFT). */
1910 shift
= size
- shift
;
1912 result
= gfc_get_constant_expr (BT_INTEGER
, arg1
->ts
.kind
, &arg1
->where
);
1913 mpz_set_ui (result
->value
.integer
, 0);
1915 for (i
= 0; i
< shift
; i
++)
1916 if (mpz_tstbit (arg2
->value
.integer
, size
- shift
+ i
))
1917 mpz_setbit (result
->value
.integer
, i
);
1919 for (i
= 0; i
< size
- shift
; i
++)
1920 if (mpz_tstbit (arg1
->value
.integer
, i
))
1921 mpz_setbit (result
->value
.integer
, shift
+ i
);
1923 /* Convert to a signed value. */
1924 convert_mpz_to_signed (result
->value
.integer
, size
);
1931 gfc_simplify_dshiftr (gfc_expr
*arg1
, gfc_expr
*arg2
, gfc_expr
*shiftarg
)
1933 return simplify_dshift (arg1
, arg2
, shiftarg
, true);
1938 gfc_simplify_dshiftl (gfc_expr
*arg1
, gfc_expr
*arg2
, gfc_expr
*shiftarg
)
1940 return simplify_dshift (arg1
, arg2
, shiftarg
, false);
1945 gfc_simplify_erf (gfc_expr
*x
)
1949 if (x
->expr_type
!= EXPR_CONSTANT
)
1952 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1953 mpfr_erf (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1955 return range_check (result
, "ERF");
1960 gfc_simplify_erfc (gfc_expr
*x
)
1964 if (x
->expr_type
!= EXPR_CONSTANT
)
1967 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1968 mpfr_erfc (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1970 return range_check (result
, "ERFC");
1974 /* Helper functions to simplify ERFC_SCALED(x) = ERFC(x) * EXP(X**2). */
1976 #define MAX_ITER 200
1977 #define ARG_LIMIT 12
1979 /* Calculate ERFC_SCALED directly by its definition:
1981 ERFC_SCALED(x) = ERFC(x) * EXP(X**2)
1983 using a large precision for intermediate results. This is used for all
1984 but large values of the argument. */
1986 fullprec_erfc_scaled (mpfr_t res
, mpfr_t arg
)
1991 prec
= mpfr_get_default_prec ();
1992 mpfr_set_default_prec (10 * prec
);
1997 mpfr_set (a
, arg
, GFC_RND_MODE
);
1998 mpfr_sqr (b
, a
, GFC_RND_MODE
);
1999 mpfr_exp (b
, b
, GFC_RND_MODE
);
2000 mpfr_erfc (a
, a
, GFC_RND_MODE
);
2001 mpfr_mul (a
, a
, b
, GFC_RND_MODE
);
2003 mpfr_set (res
, a
, GFC_RND_MODE
);
2004 mpfr_set_default_prec (prec
);
2010 /* Calculate ERFC_SCALED using a power series expansion in 1/arg:
2012 ERFC_SCALED(x) = 1 / (x * sqrt(pi))
2013 * (1 + Sum_n (-1)**n * (1 * 3 * 5 * ... * (2n-1))
2016 This is used for large values of the argument. Intermediate calculations
2017 are performed with twice the precision. We don't do a fixed number of
2018 iterations of the sum, but stop when it has converged to the required
2021 asympt_erfc_scaled (mpfr_t res
, mpfr_t arg
)
2023 mpfr_t sum
, x
, u
, v
, w
, oldsum
, sumtrunc
;
2028 prec
= mpfr_get_default_prec ();
2029 mpfr_set_default_prec (2 * prec
);
2039 mpfr_init (sumtrunc
);
2040 mpfr_set_prec (oldsum
, prec
);
2041 mpfr_set_prec (sumtrunc
, prec
);
2043 mpfr_set (x
, arg
, GFC_RND_MODE
);
2044 mpfr_set_ui (sum
, 1, GFC_RND_MODE
);
2045 mpz_set_ui (num
, 1);
2047 mpfr_set (u
, x
, GFC_RND_MODE
);
2048 mpfr_sqr (u
, u
, GFC_RND_MODE
);
2049 mpfr_mul_ui (u
, u
, 2, GFC_RND_MODE
);
2050 mpfr_pow_si (u
, u
, -1, GFC_RND_MODE
);
2052 for (i
= 1; i
< MAX_ITER
; i
++)
2054 mpfr_set (oldsum
, sum
, GFC_RND_MODE
);
2056 mpz_mul_ui (num
, num
, 2 * i
- 1);
2059 mpfr_set (w
, u
, GFC_RND_MODE
);
2060 mpfr_pow_ui (w
, w
, i
, GFC_RND_MODE
);
2062 mpfr_set_z (v
, num
, GFC_RND_MODE
);
2063 mpfr_mul (v
, v
, w
, GFC_RND_MODE
);
2065 mpfr_add (sum
, sum
, v
, GFC_RND_MODE
);
2067 mpfr_set (sumtrunc
, sum
, GFC_RND_MODE
);
2068 if (mpfr_cmp (sumtrunc
, oldsum
) == 0)
2072 /* We should have converged by now; otherwise, ARG_LIMIT is probably
2074 gcc_assert (i
< MAX_ITER
);
2076 /* Divide by x * sqrt(Pi). */
2077 mpfr_const_pi (u
, GFC_RND_MODE
);
2078 mpfr_sqrt (u
, u
, GFC_RND_MODE
);
2079 mpfr_mul (u
, u
, x
, GFC_RND_MODE
);
2080 mpfr_div (sum
, sum
, u
, GFC_RND_MODE
);
2082 mpfr_set (res
, sum
, GFC_RND_MODE
);
2083 mpfr_set_default_prec (prec
);
2085 mpfr_clears (sum
, x
, u
, v
, w
, oldsum
, sumtrunc
, NULL
);
2091 gfc_simplify_erfc_scaled (gfc_expr
*x
)
2095 if (x
->expr_type
!= EXPR_CONSTANT
)
2098 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2099 if (mpfr_cmp_d (x
->value
.real
, ARG_LIMIT
) >= 0)
2100 asympt_erfc_scaled (result
->value
.real
, x
->value
.real
);
2102 fullprec_erfc_scaled (result
->value
.real
, x
->value
.real
);
2104 return range_check (result
, "ERFC_SCALED");
2112 gfc_simplify_epsilon (gfc_expr
*e
)
2117 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
2119 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
2120 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].epsilon
, GFC_RND_MODE
);
2122 return range_check (result
, "EPSILON");
2127 gfc_simplify_exp (gfc_expr
*x
)
2131 if (x
->expr_type
!= EXPR_CONSTANT
)
2134 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2139 mpfr_exp (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
2143 gfc_set_model_kind (x
->ts
.kind
);
2144 mpc_exp (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
2148 gfc_internal_error ("in gfc_simplify_exp(): Bad type");
2151 return range_check (result
, "EXP");
2156 gfc_simplify_exponent (gfc_expr
*x
)
2161 if (x
->expr_type
!= EXPR_CONSTANT
)
2164 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
2167 gfc_set_model (x
->value
.real
);
2169 if (mpfr_sgn (x
->value
.real
) == 0)
2171 mpz_set_ui (result
->value
.integer
, 0);
2175 i
= (int) mpfr_get_exp (x
->value
.real
);
2176 mpz_set_si (result
->value
.integer
, i
);
2178 return range_check (result
, "EXPONENT");
2183 gfc_simplify_float (gfc_expr
*a
)
2187 if (a
->expr_type
!= EXPR_CONSTANT
)
2192 if (convert_boz (a
, gfc_default_real_kind
) == &gfc_bad_expr
)
2193 return &gfc_bad_expr
;
2195 result
= gfc_copy_expr (a
);
2198 result
= gfc_int2real (a
, gfc_default_real_kind
);
2200 return range_check (result
, "FLOAT");
2205 gfc_simplify_floor (gfc_expr
*e
, gfc_expr
*k
)
2211 kind
= get_kind (BT_INTEGER
, k
, "FLOOR", gfc_default_integer_kind
);
2213 gfc_internal_error ("gfc_simplify_floor(): Bad kind");
2215 if (e
->expr_type
!= EXPR_CONSTANT
)
2218 gfc_set_model_kind (kind
);
2221 mpfr_floor (floor
, e
->value
.real
);
2223 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &e
->where
);
2224 gfc_mpfr_to_mpz (result
->value
.integer
, floor
, &e
->where
);
2228 return range_check (result
, "FLOOR");
2233 gfc_simplify_fraction (gfc_expr
*x
)
2236 mpfr_t absv
, exp
, pow2
;
2238 if (x
->expr_type
!= EXPR_CONSTANT
)
2241 result
= gfc_get_constant_expr (BT_REAL
, x
->ts
.kind
, &x
->where
);
2243 if (mpfr_sgn (x
->value
.real
) == 0)
2245 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
2249 gfc_set_model_kind (x
->ts
.kind
);
2254 mpfr_abs (absv
, x
->value
.real
, GFC_RND_MODE
);
2255 mpfr_log2 (exp
, absv
, GFC_RND_MODE
);
2257 mpfr_trunc (exp
, exp
);
2258 mpfr_add_ui (exp
, exp
, 1, GFC_RND_MODE
);
2260 mpfr_ui_pow (pow2
, 2, exp
, GFC_RND_MODE
);
2262 mpfr_div (result
->value
.real
, absv
, pow2
, GFC_RND_MODE
);
2264 mpfr_clears (exp
, absv
, pow2
, NULL
);
2266 return range_check (result
, "FRACTION");
2271 gfc_simplify_gamma (gfc_expr
*x
)
2275 if (x
->expr_type
!= EXPR_CONSTANT
)
2278 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2279 mpfr_gamma (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
2281 return range_check (result
, "GAMMA");
2286 gfc_simplify_huge (gfc_expr
*e
)
2291 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
2292 result
= gfc_get_constant_expr (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
2297 mpz_set (result
->value
.integer
, gfc_integer_kinds
[i
].huge
);
2301 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].huge
, GFC_RND_MODE
);
2313 gfc_simplify_hypot (gfc_expr
*x
, gfc_expr
*y
)
2317 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
2320 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2321 mpfr_hypot (result
->value
.real
, x
->value
.real
, y
->value
.real
, GFC_RND_MODE
);
2322 return range_check (result
, "HYPOT");
2326 /* We use the processor's collating sequence, because all
2327 systems that gfortran currently works on are ASCII. */
2330 gfc_simplify_iachar (gfc_expr
*e
, gfc_expr
*kind
)
2336 if (e
->expr_type
!= EXPR_CONSTANT
)
2339 if (e
->value
.character
.length
!= 1)
2341 gfc_error ("Argument of IACHAR at %L must be of length one", &e
->where
);
2342 return &gfc_bad_expr
;
2345 index
= e
->value
.character
.string
[0];
2347 if (gfc_option
.warn_surprising
&& index
> 127)
2348 gfc_warning ("Argument of IACHAR function at %L outside of range 0..127",
2351 k
= get_kind (BT_INTEGER
, kind
, "IACHAR", gfc_default_integer_kind
);
2353 return &gfc_bad_expr
;
2355 result
= gfc_get_int_expr (k
, &e
->where
, index
);
2357 return range_check (result
, "IACHAR");
2362 do_bit_and (gfc_expr
*result
, gfc_expr
*e
)
2364 gcc_assert (e
->ts
.type
== BT_INTEGER
&& e
->expr_type
== EXPR_CONSTANT
);
2365 gcc_assert (result
->ts
.type
== BT_INTEGER
2366 && result
->expr_type
== EXPR_CONSTANT
);
2368 mpz_and (result
->value
.integer
, result
->value
.integer
, e
->value
.integer
);
2374 gfc_simplify_iall (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
)
2376 return simplify_transformation (array
, dim
, mask
, -1, do_bit_and
);
2381 do_bit_ior (gfc_expr
*result
, gfc_expr
*e
)
2383 gcc_assert (e
->ts
.type
== BT_INTEGER
&& e
->expr_type
== EXPR_CONSTANT
);
2384 gcc_assert (result
->ts
.type
== BT_INTEGER
2385 && result
->expr_type
== EXPR_CONSTANT
);
2387 mpz_ior (result
->value
.integer
, result
->value
.integer
, e
->value
.integer
);
2393 gfc_simplify_iany (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
)
2395 return simplify_transformation (array
, dim
, mask
, 0, do_bit_ior
);
2400 gfc_simplify_iand (gfc_expr
*x
, gfc_expr
*y
)
2404 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
2407 result
= gfc_get_constant_expr (BT_INTEGER
, x
->ts
.kind
, &x
->where
);
2408 mpz_and (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
2410 return range_check (result
, "IAND");
2415 gfc_simplify_ibclr (gfc_expr
*x
, gfc_expr
*y
)
2420 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
2423 if (gfc_extract_int (y
, &pos
) != NULL
|| pos
< 0)
2425 gfc_error ("Invalid second argument of IBCLR at %L", &y
->where
);
2426 return &gfc_bad_expr
;
2429 k
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
2431 if (pos
>= gfc_integer_kinds
[k
].bit_size
)
2433 gfc_error ("Second argument of IBCLR exceeds bit size at %L",
2435 return &gfc_bad_expr
;
2438 result
= gfc_copy_expr (x
);
2440 convert_mpz_to_unsigned (result
->value
.integer
,
2441 gfc_integer_kinds
[k
].bit_size
);
2443 mpz_clrbit (result
->value
.integer
, pos
);
2445 convert_mpz_to_signed (result
->value
.integer
,
2446 gfc_integer_kinds
[k
].bit_size
);
2453 gfc_simplify_ibits (gfc_expr
*x
, gfc_expr
*y
, gfc_expr
*z
)
2460 if (x
->expr_type
!= EXPR_CONSTANT
2461 || y
->expr_type
!= EXPR_CONSTANT
2462 || z
->expr_type
!= EXPR_CONSTANT
)
2465 if (gfc_extract_int (y
, &pos
) != NULL
|| pos
< 0)
2467 gfc_error ("Invalid second argument of IBITS at %L", &y
->where
);
2468 return &gfc_bad_expr
;
2471 if (gfc_extract_int (z
, &len
) != NULL
|| len
< 0)
2473 gfc_error ("Invalid third argument of IBITS at %L", &z
->where
);
2474 return &gfc_bad_expr
;
2477 k
= gfc_validate_kind (BT_INTEGER
, x
->ts
.kind
, false);
2479 bitsize
= gfc_integer_kinds
[k
].bit_size
;
2481 if (pos
+ len
> bitsize
)
2483 gfc_error ("Sum of second and third arguments of IBITS exceeds "
2484 "bit size at %L", &y
->where
);
2485 return &gfc_bad_expr
;
2488 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2489 convert_mpz_to_unsigned (result
->value
.integer
,
2490 gfc_integer_kinds
[k
].bit_size
);
2492 bits
= XCNEWVEC (int, bitsize
);
2494 for (i
= 0; i
< bitsize
; i
++)
2497 for (i
= 0; i
< len
; i
++)
2498 bits
[i
] = mpz_tstbit (x
->value
.integer
, i
+ pos
);
2500 for (i
= 0; i
< bitsize
; i
++)
2503 mpz_clrbit (result
->value
.integer
, i
);
2504 else if (bits
[i
] == 1)
2505 mpz_setbit (result
->value
.integer
, i
);
2507 gfc_internal_error ("IBITS: Bad bit");
2512 convert_mpz_to_signed (result
->value
.integer
,
2513 gfc_integer_kinds
[k
].bit_size
);
2520 gfc_simplify_ibset (gfc_expr
*x
, gfc_expr
*y
)
2525 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
2528 if (gfc_extract_int (y
, &pos
) != NULL
|| pos
< 0)
2530 gfc_error ("Invalid second argument of IBSET at %L", &y
->where
);
2531 return &gfc_bad_expr
;
2534 k
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
2536 if (pos
>= gfc_integer_kinds
[k
].bit_size
)
2538 gfc_error ("Second argument of IBSET exceeds bit size at %L",
2540 return &gfc_bad_expr
;
2543 result
= gfc_copy_expr (x
);
2545 convert_mpz_to_unsigned (result
->value
.integer
,
2546 gfc_integer_kinds
[k
].bit_size
);
2548 mpz_setbit (result
->value
.integer
, pos
);
2550 convert_mpz_to_signed (result
->value
.integer
,
2551 gfc_integer_kinds
[k
].bit_size
);
2558 gfc_simplify_ichar (gfc_expr
*e
, gfc_expr
*kind
)
2564 if (e
->expr_type
!= EXPR_CONSTANT
)
2567 if (e
->value
.character
.length
!= 1)
2569 gfc_error ("Argument of ICHAR at %L must be of length one", &e
->where
);
2570 return &gfc_bad_expr
;
2573 index
= e
->value
.character
.string
[0];
2575 k
= get_kind (BT_INTEGER
, kind
, "ICHAR", gfc_default_integer_kind
);
2577 return &gfc_bad_expr
;
2579 result
= gfc_get_int_expr (k
, &e
->where
, index
);
2581 return range_check (result
, "ICHAR");
2586 gfc_simplify_ieor (gfc_expr
*x
, gfc_expr
*y
)
2590 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
2593 result
= gfc_get_constant_expr (BT_INTEGER
, x
->ts
.kind
, &x
->where
);
2594 mpz_xor (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
2596 return range_check (result
, "IEOR");
2601 gfc_simplify_index (gfc_expr
*x
, gfc_expr
*y
, gfc_expr
*b
, gfc_expr
*kind
)
2604 int back
, len
, lensub
;
2605 int i
, j
, k
, count
, index
= 0, start
;
2607 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
2608 || ( b
!= NULL
&& b
->expr_type
!= EXPR_CONSTANT
))
2611 if (b
!= NULL
&& b
->value
.logical
!= 0)
2616 k
= get_kind (BT_INTEGER
, kind
, "INDEX", gfc_default_integer_kind
);
2618 return &gfc_bad_expr
;
2620 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &x
->where
);
2622 len
= x
->value
.character
.length
;
2623 lensub
= y
->value
.character
.length
;
2627 mpz_set_si (result
->value
.integer
, 0);
2635 mpz_set_si (result
->value
.integer
, 1);
2638 else if (lensub
== 1)
2640 for (i
= 0; i
< len
; i
++)
2642 for (j
= 0; j
< lensub
; j
++)
2644 if (y
->value
.character
.string
[j
]
2645 == x
->value
.character
.string
[i
])
2655 for (i
= 0; i
< len
; i
++)
2657 for (j
= 0; j
< lensub
; j
++)
2659 if (y
->value
.character
.string
[j
]
2660 == x
->value
.character
.string
[i
])
2665 for (k
= 0; k
< lensub
; k
++)
2667 if (y
->value
.character
.string
[k
]
2668 == x
->value
.character
.string
[k
+ start
])
2672 if (count
== lensub
)
2687 mpz_set_si (result
->value
.integer
, len
+ 1);
2690 else if (lensub
== 1)
2692 for (i
= 0; i
< len
; i
++)
2694 for (j
= 0; j
< lensub
; j
++)
2696 if (y
->value
.character
.string
[j
]
2697 == x
->value
.character
.string
[len
- i
])
2699 index
= len
- i
+ 1;
2707 for (i
= 0; i
< len
; i
++)
2709 for (j
= 0; j
< lensub
; j
++)
2711 if (y
->value
.character
.string
[j
]
2712 == x
->value
.character
.string
[len
- i
])
2715 if (start
<= len
- lensub
)
2718 for (k
= 0; k
< lensub
; k
++)
2719 if (y
->value
.character
.string
[k
]
2720 == x
->value
.character
.string
[k
+ start
])
2723 if (count
== lensub
)
2740 mpz_set_si (result
->value
.integer
, index
);
2741 return range_check (result
, "INDEX");
2746 simplify_intconv (gfc_expr
*e
, int kind
, const char *name
)
2748 gfc_expr
*result
= NULL
;
2750 if (e
->expr_type
!= EXPR_CONSTANT
)
2753 result
= gfc_convert_constant (e
, BT_INTEGER
, kind
);
2754 if (result
== &gfc_bad_expr
)
2755 return &gfc_bad_expr
;
2757 return range_check (result
, name
);
2762 gfc_simplify_int (gfc_expr
*e
, gfc_expr
*k
)
2766 kind
= get_kind (BT_INTEGER
, k
, "INT", gfc_default_integer_kind
);
2768 return &gfc_bad_expr
;
2770 return simplify_intconv (e
, kind
, "INT");
2774 gfc_simplify_int2 (gfc_expr
*e
)
2776 return simplify_intconv (e
, 2, "INT2");
2781 gfc_simplify_int8 (gfc_expr
*e
)
2783 return simplify_intconv (e
, 8, "INT8");
2788 gfc_simplify_long (gfc_expr
*e
)
2790 return simplify_intconv (e
, 4, "LONG");
2795 gfc_simplify_ifix (gfc_expr
*e
)
2797 gfc_expr
*rtrunc
, *result
;
2799 if (e
->expr_type
!= EXPR_CONSTANT
)
2802 rtrunc
= gfc_copy_expr (e
);
2803 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
2805 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
2807 gfc_mpfr_to_mpz (result
->value
.integer
, rtrunc
->value
.real
, &e
->where
);
2809 gfc_free_expr (rtrunc
);
2811 return range_check (result
, "IFIX");
2816 gfc_simplify_idint (gfc_expr
*e
)
2818 gfc_expr
*rtrunc
, *result
;
2820 if (e
->expr_type
!= EXPR_CONSTANT
)
2823 rtrunc
= gfc_copy_expr (e
);
2824 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
2826 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
2828 gfc_mpfr_to_mpz (result
->value
.integer
, rtrunc
->value
.real
, &e
->where
);
2830 gfc_free_expr (rtrunc
);
2832 return range_check (result
, "IDINT");
2837 gfc_simplify_ior (gfc_expr
*x
, gfc_expr
*y
)
2841 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
2844 result
= gfc_get_constant_expr (BT_INTEGER
, x
->ts
.kind
, &x
->where
);
2845 mpz_ior (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
2847 return range_check (result
, "IOR");
2852 do_bit_xor (gfc_expr
*result
, gfc_expr
*e
)
2854 gcc_assert (e
->ts
.type
== BT_INTEGER
&& e
->expr_type
== EXPR_CONSTANT
);
2855 gcc_assert (result
->ts
.type
== BT_INTEGER
2856 && result
->expr_type
== EXPR_CONSTANT
);
2858 mpz_xor (result
->value
.integer
, result
->value
.integer
, e
->value
.integer
);
2864 gfc_simplify_iparity (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
)
2866 return simplify_transformation (array
, dim
, mask
, 0, do_bit_xor
);
2872 gfc_simplify_is_iostat_end (gfc_expr
*x
)
2874 if (x
->expr_type
!= EXPR_CONSTANT
)
2877 return gfc_get_logical_expr (gfc_default_logical_kind
, &x
->where
,
2878 mpz_cmp_si (x
->value
.integer
,
2879 LIBERROR_END
) == 0);
2884 gfc_simplify_is_iostat_eor (gfc_expr
*x
)
2886 if (x
->expr_type
!= EXPR_CONSTANT
)
2889 return gfc_get_logical_expr (gfc_default_logical_kind
, &x
->where
,
2890 mpz_cmp_si (x
->value
.integer
,
2891 LIBERROR_EOR
) == 0);
2896 gfc_simplify_isnan (gfc_expr
*x
)
2898 if (x
->expr_type
!= EXPR_CONSTANT
)
2901 return gfc_get_logical_expr (gfc_default_logical_kind
, &x
->where
,
2902 mpfr_nan_p (x
->value
.real
));
2906 /* Performs a shift on its first argument. Depending on the last
2907 argument, the shift can be arithmetic, i.e. with filling from the
2908 left like in the SHIFTA intrinsic. */
2910 simplify_shift (gfc_expr
*e
, gfc_expr
*s
, const char *name
,
2911 bool arithmetic
, int direction
)
2914 int ashift
, *bits
, i
, k
, bitsize
, shift
;
2916 if (e
->expr_type
!= EXPR_CONSTANT
|| s
->expr_type
!= EXPR_CONSTANT
)
2918 if (gfc_extract_int (s
, &shift
) != NULL
)
2920 gfc_error ("Invalid second argument of %s at %L", name
, &s
->where
);
2921 return &gfc_bad_expr
;
2924 k
= gfc_validate_kind (BT_INTEGER
, e
->ts
.kind
, false);
2925 bitsize
= gfc_integer_kinds
[k
].bit_size
;
2927 result
= gfc_get_constant_expr (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
2931 mpz_set (result
->value
.integer
, e
->value
.integer
);
2935 if (direction
> 0 && shift
< 0)
2937 /* Left shift, as in SHIFTL. */
2938 gfc_error ("Second argument of %s is negative at %L", name
, &e
->where
);
2939 return &gfc_bad_expr
;
2941 else if (direction
< 0)
2943 /* Right shift, as in SHIFTR or SHIFTA. */
2946 gfc_error ("Second argument of %s is negative at %L",
2948 return &gfc_bad_expr
;
2954 ashift
= (shift
>= 0 ? shift
: -shift
);
2956 if (ashift
> bitsize
)
2958 gfc_error ("Magnitude of second argument of %s exceeds bit size "
2959 "at %L", name
, &e
->where
);
2960 return &gfc_bad_expr
;
2963 bits
= XCNEWVEC (int, bitsize
);
2965 for (i
= 0; i
< bitsize
; i
++)
2966 bits
[i
] = mpz_tstbit (e
->value
.integer
, i
);
2971 for (i
= 0; i
< shift
; i
++)
2972 mpz_clrbit (result
->value
.integer
, i
);
2974 for (i
= 0; i
< bitsize
- shift
; i
++)
2977 mpz_clrbit (result
->value
.integer
, i
+ shift
);
2979 mpz_setbit (result
->value
.integer
, i
+ shift
);
2985 if (arithmetic
&& bits
[bitsize
- 1])
2986 for (i
= bitsize
- 1; i
>= bitsize
- ashift
; i
--)
2987 mpz_setbit (result
->value
.integer
, i
);
2989 for (i
= bitsize
- 1; i
>= bitsize
- ashift
; i
--)
2990 mpz_clrbit (result
->value
.integer
, i
);
2992 for (i
= bitsize
- 1; i
>= ashift
; i
--)
2995 mpz_clrbit (result
->value
.integer
, i
- ashift
);
2997 mpz_setbit (result
->value
.integer
, i
- ashift
);
3001 convert_mpz_to_signed (result
->value
.integer
, bitsize
);
3009 gfc_simplify_ishft (gfc_expr
*e
, gfc_expr
*s
)
3011 return simplify_shift (e
, s
, "ISHFT", false, 0);
3016 gfc_simplify_lshift (gfc_expr
*e
, gfc_expr
*s
)
3018 return simplify_shift (e
, s
, "LSHIFT", false, 1);
3023 gfc_simplify_rshift (gfc_expr
*e
, gfc_expr
*s
)
3025 return simplify_shift (e
, s
, "RSHIFT", true, -1);
3030 gfc_simplify_shifta (gfc_expr
*e
, gfc_expr
*s
)
3032 return simplify_shift (e
, s
, "SHIFTA", true, -1);
3037 gfc_simplify_shiftl (gfc_expr
*e
, gfc_expr
*s
)
3039 return simplify_shift (e
, s
, "SHIFTL", false, 1);
3044 gfc_simplify_shiftr (gfc_expr
*e
, gfc_expr
*s
)
3046 return simplify_shift (e
, s
, "SHIFTR", false, -1);
3051 gfc_simplify_ishftc (gfc_expr
*e
, gfc_expr
*s
, gfc_expr
*sz
)
3054 int shift
, ashift
, isize
, ssize
, delta
, k
;
3057 if (e
->expr_type
!= EXPR_CONSTANT
|| s
->expr_type
!= EXPR_CONSTANT
)
3060 if (gfc_extract_int (s
, &shift
) != NULL
)
3062 gfc_error ("Invalid second argument of ISHFTC at %L", &s
->where
);
3063 return &gfc_bad_expr
;
3066 k
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
3067 isize
= gfc_integer_kinds
[k
].bit_size
;
3071 if (sz
->expr_type
!= EXPR_CONSTANT
)
3074 if (gfc_extract_int (sz
, &ssize
) != NULL
|| ssize
<= 0)
3076 gfc_error ("Invalid third argument of ISHFTC at %L", &sz
->where
);
3077 return &gfc_bad_expr
;
3082 gfc_error ("Magnitude of third argument of ISHFTC exceeds "
3083 "BIT_SIZE of first argument at %L", &s
->where
);
3084 return &gfc_bad_expr
;
3098 gfc_error ("Magnitude of second argument of ISHFTC exceeds "
3099 "third argument at %L", &s
->where
);
3101 gfc_error ("Magnitude of second argument of ISHFTC exceeds "
3102 "BIT_SIZE of first argument at %L", &s
->where
);
3103 return &gfc_bad_expr
;
3106 result
= gfc_get_constant_expr (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
3108 mpz_set (result
->value
.integer
, e
->value
.integer
);
3113 convert_mpz_to_unsigned (result
->value
.integer
, isize
);
3115 bits
= XCNEWVEC (int, ssize
);
3117 for (i
= 0; i
< ssize
; i
++)
3118 bits
[i
] = mpz_tstbit (e
->value
.integer
, i
);
3120 delta
= ssize
- ashift
;
3124 for (i
= 0; i
< delta
; i
++)
3127 mpz_clrbit (result
->value
.integer
, i
+ shift
);
3129 mpz_setbit (result
->value
.integer
, i
+ shift
);
3132 for (i
= delta
; i
< ssize
; i
++)
3135 mpz_clrbit (result
->value
.integer
, i
- delta
);
3137 mpz_setbit (result
->value
.integer
, i
- delta
);
3142 for (i
= 0; i
< ashift
; i
++)
3145 mpz_clrbit (result
->value
.integer
, i
+ delta
);
3147 mpz_setbit (result
->value
.integer
, i
+ delta
);
3150 for (i
= ashift
; i
< ssize
; i
++)
3153 mpz_clrbit (result
->value
.integer
, i
+ shift
);
3155 mpz_setbit (result
->value
.integer
, i
+ shift
);
3159 convert_mpz_to_signed (result
->value
.integer
, isize
);
3167 gfc_simplify_kind (gfc_expr
*e
)
3169 return gfc_get_int_expr (gfc_default_integer_kind
, NULL
, e
->ts
.kind
);
3174 simplify_bound_dim (gfc_expr
*array
, gfc_expr
*kind
, int d
, int upper
,
3175 gfc_array_spec
*as
, gfc_ref
*ref
, bool coarray
)
3177 gfc_expr
*l
, *u
, *result
;
3180 k
= get_kind (BT_INTEGER
, kind
, upper
? "UBOUND" : "LBOUND",
3181 gfc_default_integer_kind
);
3183 return &gfc_bad_expr
;
3185 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &array
->where
);
3187 /* For non-variables, LBOUND(expr, DIM=n) = 1 and
3188 UBOUND(expr, DIM=n) = SIZE(expr, DIM=n). */
3189 if (!coarray
&& array
->expr_type
!= EXPR_VARIABLE
)
3193 gfc_expr
* dim
= result
;
3194 mpz_set_si (dim
->value
.integer
, d
);
3196 result
= gfc_simplify_size (array
, dim
, kind
);
3197 gfc_free_expr (dim
);
3202 mpz_set_si (result
->value
.integer
, 1);
3207 /* Otherwise, we have a variable expression. */
3208 gcc_assert (array
->expr_type
== EXPR_VARIABLE
);
3211 /* The last dimension of an assumed-size array is special. */
3212 if ((!coarray
&& d
== as
->rank
&& as
->type
== AS_ASSUMED_SIZE
&& !upper
)
3213 || (coarray
&& d
== as
->rank
+ as
->corank
))
3215 if (as
->lower
[d
-1]->expr_type
== EXPR_CONSTANT
)
3217 gfc_free_expr (result
);
3218 return gfc_copy_expr (as
->lower
[d
-1]);
3224 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &array
->where
);
3226 /* Then, we need to know the extent of the given dimension. */
3227 if (coarray
|| ref
->u
.ar
.type
== AR_FULL
)
3232 if (l
->expr_type
!= EXPR_CONSTANT
|| u
== NULL
3233 || u
->expr_type
!= EXPR_CONSTANT
)
3236 if (mpz_cmp (l
->value
.integer
, u
->value
.integer
) > 0)
3240 mpz_set_si (result
->value
.integer
, 0);
3242 mpz_set_si (result
->value
.integer
, 1);
3246 /* Nonzero extent. */
3248 mpz_set (result
->value
.integer
, u
->value
.integer
);
3250 mpz_set (result
->value
.integer
, l
->value
.integer
);
3257 if (gfc_ref_dimen_size (&ref
->u
.ar
, d
-1, &result
->value
.integer
, NULL
)
3262 mpz_set_si (result
->value
.integer
, (long int) 1);
3266 return range_check (result
, upper
? "UBOUND" : "LBOUND");
3269 gfc_free_expr (result
);
3275 simplify_bound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
, int upper
)
3281 if (array
->expr_type
!= EXPR_VARIABLE
)
3288 /* Follow any component references. */
3289 as
= array
->symtree
->n
.sym
->as
;
3290 for (ref
= array
->ref
; ref
; ref
= ref
->next
)
3295 switch (ref
->u
.ar
.type
)
3302 /* We're done because 'as' has already been set in the
3303 previous iteration. */
3320 as
= ref
->u
.c
.component
->as
;
3332 if (as
&& (as
->type
== AS_DEFERRED
|| as
->type
== AS_ASSUMED_SHAPE
))
3337 /* Multi-dimensional bounds. */
3338 gfc_expr
*bounds
[GFC_MAX_DIMENSIONS
];
3342 /* UBOUND(ARRAY) is not valid for an assumed-size array. */
3343 if (upper
&& as
&& as
->type
== AS_ASSUMED_SIZE
)
3345 /* An error message will be emitted in
3346 check_assumed_size_reference (resolve.c). */
3347 return &gfc_bad_expr
;
3350 /* Simplify the bounds for each dimension. */
3351 for (d
= 0; d
< array
->rank
; d
++)
3353 bounds
[d
] = simplify_bound_dim (array
, kind
, d
+ 1, upper
, as
, ref
,
3355 if (bounds
[d
] == NULL
|| bounds
[d
] == &gfc_bad_expr
)
3359 for (j
= 0; j
< d
; j
++)
3360 gfc_free_expr (bounds
[j
]);
3365 /* Allocate the result expression. */
3366 k
= get_kind (BT_INTEGER
, kind
, upper
? "UBOUND" : "LBOUND",
3367 gfc_default_integer_kind
);
3369 return &gfc_bad_expr
;
3371 e
= gfc_get_array_expr (BT_INTEGER
, k
, &array
->where
);
3373 /* The result is a rank 1 array; its size is the rank of the first
3374 argument to {L,U}BOUND. */
3376 e
->shape
= gfc_get_shape (1);
3377 mpz_init_set_ui (e
->shape
[0], array
->rank
);
3379 /* Create the constructor for this array. */
3380 for (d
= 0; d
< array
->rank
; d
++)
3381 gfc_constructor_append_expr (&e
->value
.constructor
,
3382 bounds
[d
], &e
->where
);
3388 /* A DIM argument is specified. */
3389 if (dim
->expr_type
!= EXPR_CONSTANT
)
3392 d
= mpz_get_si (dim
->value
.integer
);
3394 if (d
< 1 || d
> array
->rank
3395 || (d
== array
->rank
&& as
&& as
->type
== AS_ASSUMED_SIZE
&& upper
))
3397 gfc_error ("DIM argument at %L is out of bounds", &dim
->where
);
3398 return &gfc_bad_expr
;
3401 return simplify_bound_dim (array
, kind
, d
, upper
, as
, ref
, false);
3407 simplify_cobound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
, int upper
)
3413 if (array
->expr_type
!= EXPR_VARIABLE
)
3416 /* Follow any component references. */
3417 as
= array
->symtree
->n
.sym
->as
;
3418 for (ref
= array
->ref
; ref
; ref
= ref
->next
)
3423 switch (ref
->u
.ar
.type
)
3426 if (ref
->next
== NULL
)
3428 gcc_assert (ref
->u
.ar
.as
->corank
> 0
3429 && ref
->u
.ar
.as
->rank
== 0);
3437 /* We're done because 'as' has already been set in the
3438 previous iteration. */
3455 as
= ref
->u
.c
.component
->as
;
3467 if (as
->type
== AS_DEFERRED
|| as
->type
== AS_ASSUMED_SHAPE
)
3472 /* Multi-dimensional cobounds. */
3473 gfc_expr
*bounds
[GFC_MAX_DIMENSIONS
];
3477 /* Simplify the cobounds for each dimension. */
3478 for (d
= 0; d
< as
->corank
; d
++)
3480 bounds
[d
] = simplify_bound_dim (array
, kind
, d
+ 1 + array
->rank
,
3481 upper
, as
, ref
, true);
3482 if (bounds
[d
] == NULL
|| bounds
[d
] == &gfc_bad_expr
)
3486 for (j
= 0; j
< d
; j
++)
3487 gfc_free_expr (bounds
[j
]);
3492 /* Allocate the result expression. */
3493 e
= gfc_get_expr ();
3494 e
->where
= array
->where
;
3495 e
->expr_type
= EXPR_ARRAY
;
3496 e
->ts
.type
= BT_INTEGER
;
3497 k
= get_kind (BT_INTEGER
, kind
, upper
? "UCOBOUND" : "LCOBOUND",
3498 gfc_default_integer_kind
);
3502 return &gfc_bad_expr
;
3506 /* The result is a rank 1 array; its size is the rank of the first
3507 argument to {L,U}COBOUND. */
3509 e
->shape
= gfc_get_shape (1);
3510 mpz_init_set_ui (e
->shape
[0], as
->corank
);
3512 /* Create the constructor for this array. */
3513 for (d
= 0; d
< as
->corank
; d
++)
3514 gfc_constructor_append_expr (&e
->value
.constructor
,
3515 bounds
[d
], &e
->where
);
3520 /* A DIM argument is specified. */
3521 if (dim
->expr_type
!= EXPR_CONSTANT
)
3524 d
= mpz_get_si (dim
->value
.integer
);
3526 if (d
< 1 || d
> as
->corank
)
3528 gfc_error ("DIM argument at %L is out of bounds", &dim
->where
);
3529 return &gfc_bad_expr
;
3532 return simplify_bound_dim (array
, kind
, d
+array
->rank
, upper
, as
, ref
, true);
3538 gfc_simplify_lbound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
)
3540 return simplify_bound (array
, dim
, kind
, 0);
3545 gfc_simplify_lcobound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
)
3548 /* return simplify_cobound (array, dim, kind, 0);*/
3550 e
= simplify_cobound (array
, dim
, kind
, 0);
3554 gfc_error ("Not yet implemented: LCOBOUND for coarray with non-constant "
3555 "cobounds at %L", &array
->where
);
3556 return &gfc_bad_expr
;
3560 gfc_simplify_leadz (gfc_expr
*e
)
3562 unsigned long lz
, bs
;
3565 if (e
->expr_type
!= EXPR_CONSTANT
)
3568 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
3569 bs
= gfc_integer_kinds
[i
].bit_size
;
3570 if (mpz_cmp_si (e
->value
.integer
, 0) == 0)
3572 else if (mpz_cmp_si (e
->value
.integer
, 0) < 0)
3575 lz
= bs
- mpz_sizeinbase (e
->value
.integer
, 2);
3577 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, lz
);
3582 gfc_simplify_len (gfc_expr
*e
, gfc_expr
*kind
)
3585 int k
= get_kind (BT_INTEGER
, kind
, "LEN", gfc_default_integer_kind
);
3588 return &gfc_bad_expr
;
3590 if (e
->expr_type
== EXPR_CONSTANT
)
3592 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &e
->where
);
3593 mpz_set_si (result
->value
.integer
, e
->value
.character
.length
);
3594 return range_check (result
, "LEN");
3596 else if (e
->ts
.u
.cl
!= NULL
&& e
->ts
.u
.cl
->length
!= NULL
3597 && e
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
3598 && e
->ts
.u
.cl
->length
->ts
.type
== BT_INTEGER
)
3600 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &e
->where
);
3601 mpz_set (result
->value
.integer
, e
->ts
.u
.cl
->length
->value
.integer
);
3602 return range_check (result
, "LEN");
3610 gfc_simplify_len_trim (gfc_expr
*e
, gfc_expr
*kind
)
3614 int k
= get_kind (BT_INTEGER
, kind
, "LEN_TRIM", gfc_default_integer_kind
);
3617 return &gfc_bad_expr
;
3619 if (e
->expr_type
!= EXPR_CONSTANT
)
3622 len
= e
->value
.character
.length
;
3623 for (count
= 0, i
= 1; i
<= len
; i
++)
3624 if (e
->value
.character
.string
[len
- i
] == ' ')
3629 result
= gfc_get_int_expr (k
, &e
->where
, len
- count
);
3630 return range_check (result
, "LEN_TRIM");
3634 gfc_simplify_lgamma (gfc_expr
*x
)
3639 if (x
->expr_type
!= EXPR_CONSTANT
)
3642 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3643 mpfr_lgamma (result
->value
.real
, &sg
, x
->value
.real
, GFC_RND_MODE
);
3645 return range_check (result
, "LGAMMA");
3650 gfc_simplify_lge (gfc_expr
*a
, gfc_expr
*b
)
3652 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
3655 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
,
3656 gfc_compare_string (a
, b
) >= 0);
3661 gfc_simplify_lgt (gfc_expr
*a
, gfc_expr
*b
)
3663 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
3666 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
,
3667 gfc_compare_string (a
, b
) > 0);
3672 gfc_simplify_lle (gfc_expr
*a
, gfc_expr
*b
)
3674 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
3677 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
,
3678 gfc_compare_string (a
, b
) <= 0);
3683 gfc_simplify_llt (gfc_expr
*a
, gfc_expr
*b
)
3685 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
3688 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
,
3689 gfc_compare_string (a
, b
) < 0);
3694 gfc_simplify_log (gfc_expr
*x
)
3698 if (x
->expr_type
!= EXPR_CONSTANT
)
3701 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3706 if (mpfr_sgn (x
->value
.real
) <= 0)
3708 gfc_error ("Argument of LOG at %L cannot be less than or equal "
3709 "to zero", &x
->where
);
3710 gfc_free_expr (result
);
3711 return &gfc_bad_expr
;
3714 mpfr_log (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
3718 if ((mpfr_sgn (mpc_realref (x
->value
.complex)) == 0)
3719 && (mpfr_sgn (mpc_imagref (x
->value
.complex)) == 0))
3721 gfc_error ("Complex argument of LOG at %L cannot be zero",
3723 gfc_free_expr (result
);
3724 return &gfc_bad_expr
;
3727 gfc_set_model_kind (x
->ts
.kind
);
3728 mpc_log (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
3732 gfc_internal_error ("gfc_simplify_log: bad type");
3735 return range_check (result
, "LOG");
3740 gfc_simplify_log10 (gfc_expr
*x
)
3744 if (x
->expr_type
!= EXPR_CONSTANT
)
3747 if (mpfr_sgn (x
->value
.real
) <= 0)
3749 gfc_error ("Argument of LOG10 at %L cannot be less than or equal "
3750 "to zero", &x
->where
);
3751 return &gfc_bad_expr
;
3754 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3755 mpfr_log10 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
3757 return range_check (result
, "LOG10");
3762 gfc_simplify_logical (gfc_expr
*e
, gfc_expr
*k
)
3766 kind
= get_kind (BT_LOGICAL
, k
, "LOGICAL", gfc_default_logical_kind
);
3768 return &gfc_bad_expr
;
3770 if (e
->expr_type
!= EXPR_CONSTANT
)
3773 return gfc_get_logical_expr (kind
, &e
->where
, e
->value
.logical
);
3778 gfc_simplify_matmul (gfc_expr
*matrix_a
, gfc_expr
*matrix_b
)
3781 int row
, result_rows
, col
, result_columns
;
3782 int stride_a
, offset_a
, stride_b
, offset_b
;
3784 if (!is_constant_array_expr (matrix_a
)
3785 || !is_constant_array_expr (matrix_b
))
3788 gcc_assert (gfc_compare_types (&matrix_a
->ts
, &matrix_b
->ts
));
3789 result
= gfc_get_array_expr (matrix_a
->ts
.type
,
3793 if (matrix_a
->rank
== 1 && matrix_b
->rank
== 2)
3796 result_columns
= mpz_get_si (matrix_b
->shape
[0]);
3798 stride_b
= mpz_get_si (matrix_b
->shape
[0]);
3801 result
->shape
= gfc_get_shape (result
->rank
);
3802 mpz_init_set_si (result
->shape
[0], result_columns
);
3804 else if (matrix_a
->rank
== 2 && matrix_b
->rank
== 1)
3806 result_rows
= mpz_get_si (matrix_b
->shape
[0]);
3808 stride_a
= mpz_get_si (matrix_a
->shape
[0]);
3812 result
->shape
= gfc_get_shape (result
->rank
);
3813 mpz_init_set_si (result
->shape
[0], result_rows
);
3815 else if (matrix_a
->rank
== 2 && matrix_b
->rank
== 2)
3817 result_rows
= mpz_get_si (matrix_a
->shape
[0]);
3818 result_columns
= mpz_get_si (matrix_b
->shape
[1]);
3819 stride_a
= mpz_get_si (matrix_a
->shape
[1]);
3820 stride_b
= mpz_get_si (matrix_b
->shape
[0]);
3823 result
->shape
= gfc_get_shape (result
->rank
);
3824 mpz_init_set_si (result
->shape
[0], result_rows
);
3825 mpz_init_set_si (result
->shape
[1], result_columns
);
3830 offset_a
= offset_b
= 0;
3831 for (col
= 0; col
< result_columns
; ++col
)
3835 for (row
= 0; row
< result_rows
; ++row
)
3837 gfc_expr
*e
= compute_dot_product (matrix_a
, stride_a
, offset_a
,
3838 matrix_b
, 1, offset_b
);
3839 gfc_constructor_append_expr (&result
->value
.constructor
,
3845 offset_b
+= stride_b
;
3853 gfc_simplify_maskr (gfc_expr
*i
, gfc_expr
*kind_arg
)
3859 if (i
->expr_type
!= EXPR_CONSTANT
)
3862 kind
= get_kind (BT_INTEGER
, kind_arg
, "MASKR", gfc_default_integer_kind
);
3864 return &gfc_bad_expr
;
3865 k
= gfc_validate_kind (BT_INTEGER
, kind
, false);
3867 s
= gfc_extract_int (i
, &arg
);
3870 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &i
->where
);
3872 /* MASKR(n) = 2^n - 1 */
3873 mpz_set_ui (result
->value
.integer
, 1);
3874 mpz_mul_2exp (result
->value
.integer
, result
->value
.integer
, arg
);
3875 mpz_sub_ui (result
->value
.integer
, result
->value
.integer
, 1);
3877 convert_mpz_to_signed (result
->value
.integer
, gfc_integer_kinds
[k
].bit_size
);
3884 gfc_simplify_maskl (gfc_expr
*i
, gfc_expr
*kind_arg
)
3891 if (i
->expr_type
!= EXPR_CONSTANT
)
3894 kind
= get_kind (BT_INTEGER
, kind_arg
, "MASKL", gfc_default_integer_kind
);
3896 return &gfc_bad_expr
;
3897 k
= gfc_validate_kind (BT_INTEGER
, kind
, false);
3899 s
= gfc_extract_int (i
, &arg
);
3902 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &i
->where
);
3904 /* MASKL(n) = 2^bit_size - 2^(bit_size - n) */
3905 mpz_init_set_ui (z
, 1);
3906 mpz_mul_2exp (z
, z
, gfc_integer_kinds
[k
].bit_size
);
3907 mpz_set_ui (result
->value
.integer
, 1);
3908 mpz_mul_2exp (result
->value
.integer
, result
->value
.integer
,
3909 gfc_integer_kinds
[k
].bit_size
- arg
);
3910 mpz_sub (result
->value
.integer
, z
, result
->value
.integer
);
3913 convert_mpz_to_signed (result
->value
.integer
, gfc_integer_kinds
[k
].bit_size
);
3920 gfc_simplify_merge (gfc_expr
*tsource
, gfc_expr
*fsource
, gfc_expr
*mask
)
3922 if (tsource
->expr_type
!= EXPR_CONSTANT
3923 || fsource
->expr_type
!= EXPR_CONSTANT
3924 || mask
->expr_type
!= EXPR_CONSTANT
)
3927 return gfc_copy_expr (mask
->value
.logical
? tsource
: fsource
);
3932 gfc_simplify_merge_bits (gfc_expr
*i
, gfc_expr
*j
, gfc_expr
*mask_expr
)
3934 mpz_t arg1
, arg2
, mask
;
3937 if (i
->expr_type
!= EXPR_CONSTANT
|| j
->expr_type
!= EXPR_CONSTANT
3938 || mask_expr
->expr_type
!= EXPR_CONSTANT
)
3941 result
= gfc_get_constant_expr (BT_INTEGER
, i
->ts
.kind
, &i
->where
);
3943 /* Convert all argument to unsigned. */
3944 mpz_init_set (arg1
, i
->value
.integer
);
3945 mpz_init_set (arg2
, j
->value
.integer
);
3946 mpz_init_set (mask
, mask_expr
->value
.integer
);
3948 /* MERGE_BITS(I,J,MASK) = IOR (IAND (I, MASK), IAND (J, NOT (MASK))). */
3949 mpz_and (arg1
, arg1
, mask
);
3950 mpz_com (mask
, mask
);
3951 mpz_and (arg2
, arg2
, mask
);
3952 mpz_ior (result
->value
.integer
, arg1
, arg2
);
3962 /* Selects between current value and extremum for simplify_min_max
3963 and simplify_minval_maxval. */
3965 min_max_choose (gfc_expr
*arg
, gfc_expr
*extremum
, int sign
)
3967 switch (arg
->ts
.type
)
3970 if (mpz_cmp (arg
->value
.integer
,
3971 extremum
->value
.integer
) * sign
> 0)
3972 mpz_set (extremum
->value
.integer
, arg
->value
.integer
);
3976 /* We need to use mpfr_min and mpfr_max to treat NaN properly. */
3978 mpfr_max (extremum
->value
.real
, extremum
->value
.real
,
3979 arg
->value
.real
, GFC_RND_MODE
);
3981 mpfr_min (extremum
->value
.real
, extremum
->value
.real
,
3982 arg
->value
.real
, GFC_RND_MODE
);
3986 #define LENGTH(x) ((x)->value.character.length)
3987 #define STRING(x) ((x)->value.character.string)
3988 if (LENGTH(extremum
) < LENGTH(arg
))
3990 gfc_char_t
*tmp
= STRING(extremum
);
3992 STRING(extremum
) = gfc_get_wide_string (LENGTH(arg
) + 1);
3993 memcpy (STRING(extremum
), tmp
,
3994 LENGTH(extremum
) * sizeof (gfc_char_t
));
3995 gfc_wide_memset (&STRING(extremum
)[LENGTH(extremum
)], ' ',
3996 LENGTH(arg
) - LENGTH(extremum
));
3997 STRING(extremum
)[LENGTH(arg
)] = '\0'; /* For debugger */
3998 LENGTH(extremum
) = LENGTH(arg
);
4002 if (gfc_compare_string (arg
, extremum
) * sign
> 0)
4004 gfc_free (STRING(extremum
));
4005 STRING(extremum
) = gfc_get_wide_string (LENGTH(extremum
) + 1);
4006 memcpy (STRING(extremum
), STRING(arg
),
4007 LENGTH(arg
) * sizeof (gfc_char_t
));
4008 gfc_wide_memset (&STRING(extremum
)[LENGTH(arg
)], ' ',
4009 LENGTH(extremum
) - LENGTH(arg
));
4010 STRING(extremum
)[LENGTH(extremum
)] = '\0'; /* For debugger */
4017 gfc_internal_error ("simplify_min_max(): Bad type in arglist");
4022 /* This function is special since MAX() can take any number of
4023 arguments. The simplified expression is a rewritten version of the
4024 argument list containing at most one constant element. Other
4025 constant elements are deleted. Because the argument list has
4026 already been checked, this function always succeeds. sign is 1 for
4027 MAX(), -1 for MIN(). */
4030 simplify_min_max (gfc_expr
*expr
, int sign
)
4032 gfc_actual_arglist
*arg
, *last
, *extremum
;
4033 gfc_intrinsic_sym
* specific
;
4037 specific
= expr
->value
.function
.isym
;
4039 arg
= expr
->value
.function
.actual
;
4041 for (; arg
; last
= arg
, arg
= arg
->next
)
4043 if (arg
->expr
->expr_type
!= EXPR_CONSTANT
)
4046 if (extremum
== NULL
)
4052 min_max_choose (arg
->expr
, extremum
->expr
, sign
);
4054 /* Delete the extra constant argument. */
4056 expr
->value
.function
.actual
= arg
->next
;
4058 last
->next
= arg
->next
;
4061 gfc_free_actual_arglist (arg
);
4065 /* If there is one value left, replace the function call with the
4067 if (expr
->value
.function
.actual
->next
!= NULL
)
4070 /* Convert to the correct type and kind. */
4071 if (expr
->ts
.type
!= BT_UNKNOWN
)
4072 return gfc_convert_constant (expr
->value
.function
.actual
->expr
,
4073 expr
->ts
.type
, expr
->ts
.kind
);
4075 if (specific
->ts
.type
!= BT_UNKNOWN
)
4076 return gfc_convert_constant (expr
->value
.function
.actual
->expr
,
4077 specific
->ts
.type
, specific
->ts
.kind
);
4079 return gfc_copy_expr (expr
->value
.function
.actual
->expr
);
4084 gfc_simplify_min (gfc_expr
*e
)
4086 return simplify_min_max (e
, -1);
4091 gfc_simplify_max (gfc_expr
*e
)
4093 return simplify_min_max (e
, 1);
4097 /* This is a simplified version of simplify_min_max to provide
4098 simplification of minval and maxval for a vector. */
4101 simplify_minval_maxval (gfc_expr
*expr
, int sign
)
4103 gfc_constructor
*c
, *extremum
;
4104 gfc_intrinsic_sym
* specific
;
4107 specific
= expr
->value
.function
.isym
;
4109 for (c
= gfc_constructor_first (expr
->value
.constructor
);
4110 c
; c
= gfc_constructor_next (c
))
4112 if (c
->expr
->expr_type
!= EXPR_CONSTANT
)
4115 if (extremum
== NULL
)
4121 min_max_choose (c
->expr
, extremum
->expr
, sign
);
4124 if (extremum
== NULL
)
4127 /* Convert to the correct type and kind. */
4128 if (expr
->ts
.type
!= BT_UNKNOWN
)
4129 return gfc_convert_constant (extremum
->expr
,
4130 expr
->ts
.type
, expr
->ts
.kind
);
4132 if (specific
->ts
.type
!= BT_UNKNOWN
)
4133 return gfc_convert_constant (extremum
->expr
,
4134 specific
->ts
.type
, specific
->ts
.kind
);
4136 return gfc_copy_expr (extremum
->expr
);
4141 gfc_simplify_minval (gfc_expr
*array
, gfc_expr
* dim
, gfc_expr
*mask
)
4143 if (array
->expr_type
!= EXPR_ARRAY
|| array
->rank
!= 1 || dim
|| mask
)
4146 return simplify_minval_maxval (array
, -1);
4151 gfc_simplify_maxval (gfc_expr
*array
, gfc_expr
* dim
, gfc_expr
*mask
)
4153 if (array
->expr_type
!= EXPR_ARRAY
|| array
->rank
!= 1 || dim
|| mask
)
4156 return simplify_minval_maxval (array
, 1);
4161 gfc_simplify_maxexponent (gfc_expr
*x
)
4163 int i
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, false);
4164 return gfc_get_int_expr (gfc_default_integer_kind
, &x
->where
,
4165 gfc_real_kinds
[i
].max_exponent
);
4170 gfc_simplify_minexponent (gfc_expr
*x
)
4172 int i
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, false);
4173 return gfc_get_int_expr (gfc_default_integer_kind
, &x
->where
,
4174 gfc_real_kinds
[i
].min_exponent
);
4179 gfc_simplify_mod (gfc_expr
*a
, gfc_expr
*p
)
4185 if (a
->expr_type
!= EXPR_CONSTANT
|| p
->expr_type
!= EXPR_CONSTANT
)
4188 kind
= a
->ts
.kind
> p
->ts
.kind
? a
->ts
.kind
: p
->ts
.kind
;
4189 result
= gfc_get_constant_expr (a
->ts
.type
, kind
, &a
->where
);
4194 if (mpz_cmp_ui (p
->value
.integer
, 0) == 0)
4196 /* Result is processor-dependent. */
4197 gfc_error ("Second argument MOD at %L is zero", &a
->where
);
4198 gfc_free_expr (result
);
4199 return &gfc_bad_expr
;
4201 mpz_tdiv_r (result
->value
.integer
, a
->value
.integer
, p
->value
.integer
);
4205 if (mpfr_cmp_ui (p
->value
.real
, 0) == 0)
4207 /* Result is processor-dependent. */
4208 gfc_error ("Second argument of MOD at %L is zero", &p
->where
);
4209 gfc_free_expr (result
);
4210 return &gfc_bad_expr
;
4213 gfc_set_model_kind (kind
);
4215 mpfr_div (tmp
, a
->value
.real
, p
->value
.real
, GFC_RND_MODE
);
4216 mpfr_trunc (tmp
, tmp
);
4217 mpfr_mul (tmp
, tmp
, p
->value
.real
, GFC_RND_MODE
);
4218 mpfr_sub (result
->value
.real
, a
->value
.real
, tmp
, GFC_RND_MODE
);
4223 gfc_internal_error ("gfc_simplify_mod(): Bad arguments");
4226 return range_check (result
, "MOD");
4231 gfc_simplify_modulo (gfc_expr
*a
, gfc_expr
*p
)
4237 if (a
->expr_type
!= EXPR_CONSTANT
|| p
->expr_type
!= EXPR_CONSTANT
)
4240 kind
= a
->ts
.kind
> p
->ts
.kind
? a
->ts
.kind
: p
->ts
.kind
;
4241 result
= gfc_get_constant_expr (a
->ts
.type
, kind
, &a
->where
);
4246 if (mpz_cmp_ui (p
->value
.integer
, 0) == 0)
4248 /* Result is processor-dependent. This processor just opts
4249 to not handle it at all. */
4250 gfc_error ("Second argument of MODULO at %L is zero", &a
->where
);
4251 gfc_free_expr (result
);
4252 return &gfc_bad_expr
;
4254 mpz_fdiv_r (result
->value
.integer
, a
->value
.integer
, p
->value
.integer
);
4259 if (mpfr_cmp_ui (p
->value
.real
, 0) == 0)
4261 /* Result is processor-dependent. */
4262 gfc_error ("Second argument of MODULO at %L is zero", &p
->where
);
4263 gfc_free_expr (result
);
4264 return &gfc_bad_expr
;
4267 gfc_set_model_kind (kind
);
4269 mpfr_div (tmp
, a
->value
.real
, p
->value
.real
, GFC_RND_MODE
);
4270 mpfr_floor (tmp
, tmp
);
4271 mpfr_mul (tmp
, tmp
, p
->value
.real
, GFC_RND_MODE
);
4272 mpfr_sub (result
->value
.real
, a
->value
.real
, tmp
, GFC_RND_MODE
);
4277 gfc_internal_error ("gfc_simplify_modulo(): Bad arguments");
4280 return range_check (result
, "MODULO");
4284 /* Exists for the sole purpose of consistency with other intrinsics. */
4286 gfc_simplify_mvbits (gfc_expr
*f ATTRIBUTE_UNUSED
,
4287 gfc_expr
*fp ATTRIBUTE_UNUSED
,
4288 gfc_expr
*l ATTRIBUTE_UNUSED
,
4289 gfc_expr
*to ATTRIBUTE_UNUSED
,
4290 gfc_expr
*tp ATTRIBUTE_UNUSED
)
4297 gfc_simplify_nearest (gfc_expr
*x
, gfc_expr
*s
)
4300 mp_exp_t emin
, emax
;
4303 if (x
->expr_type
!= EXPR_CONSTANT
|| s
->expr_type
!= EXPR_CONSTANT
)
4306 if (mpfr_sgn (s
->value
.real
) == 0)
4308 gfc_error ("Second argument of NEAREST at %L shall not be zero",
4310 return &gfc_bad_expr
;
4313 result
= gfc_copy_expr (x
);
4315 /* Save current values of emin and emax. */
4316 emin
= mpfr_get_emin ();
4317 emax
= mpfr_get_emax ();
4319 /* Set emin and emax for the current model number. */
4320 kind
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, 0);
4321 mpfr_set_emin ((mp_exp_t
) gfc_real_kinds
[kind
].min_exponent
-
4322 mpfr_get_prec(result
->value
.real
) + 1);
4323 mpfr_set_emax ((mp_exp_t
) gfc_real_kinds
[kind
].max_exponent
- 1);
4324 mpfr_check_range (result
->value
.real
, 0, GMP_RNDU
);
4326 if (mpfr_sgn (s
->value
.real
) > 0)
4328 mpfr_nextabove (result
->value
.real
);
4329 mpfr_subnormalize (result
->value
.real
, 0, GMP_RNDU
);
4333 mpfr_nextbelow (result
->value
.real
);
4334 mpfr_subnormalize (result
->value
.real
, 0, GMP_RNDD
);
4337 mpfr_set_emin (emin
);
4338 mpfr_set_emax (emax
);
4340 /* Only NaN can occur. Do not use range check as it gives an
4341 error for denormal numbers. */
4342 if (mpfr_nan_p (result
->value
.real
) && gfc_option
.flag_range_check
)
4344 gfc_error ("Result of NEAREST is NaN at %L", &result
->where
);
4345 gfc_free_expr (result
);
4346 return &gfc_bad_expr
;
4354 simplify_nint (const char *name
, gfc_expr
*e
, gfc_expr
*k
)
4356 gfc_expr
*itrunc
, *result
;
4359 kind
= get_kind (BT_INTEGER
, k
, name
, gfc_default_integer_kind
);
4361 return &gfc_bad_expr
;
4363 if (e
->expr_type
!= EXPR_CONSTANT
)
4366 itrunc
= gfc_copy_expr (e
);
4367 mpfr_round (itrunc
->value
.real
, e
->value
.real
);
4369 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &e
->where
);
4370 gfc_mpfr_to_mpz (result
->value
.integer
, itrunc
->value
.real
, &e
->where
);
4372 gfc_free_expr (itrunc
);
4374 return range_check (result
, name
);
4379 gfc_simplify_new_line (gfc_expr
*e
)
4383 result
= gfc_get_character_expr (e
->ts
.kind
, &e
->where
, NULL
, 1);
4384 result
->value
.character
.string
[0] = '\n';
4391 gfc_simplify_nint (gfc_expr
*e
, gfc_expr
*k
)
4393 return simplify_nint ("NINT", e
, k
);
4398 gfc_simplify_idnint (gfc_expr
*e
)
4400 return simplify_nint ("IDNINT", e
, NULL
);
4405 add_squared (gfc_expr
*result
, gfc_expr
*e
)
4409 gcc_assert (e
->ts
.type
== BT_REAL
&& e
->expr_type
== EXPR_CONSTANT
);
4410 gcc_assert (result
->ts
.type
== BT_REAL
4411 && result
->expr_type
== EXPR_CONSTANT
);
4413 gfc_set_model_kind (result
->ts
.kind
);
4415 mpfr_pow_ui (tmp
, e
->value
.real
, 2, GFC_RND_MODE
);
4416 mpfr_add (result
->value
.real
, result
->value
.real
, tmp
,
4425 do_sqrt (gfc_expr
*result
, gfc_expr
*e
)
4427 gcc_assert (e
->ts
.type
== BT_REAL
&& e
->expr_type
== EXPR_CONSTANT
);
4428 gcc_assert (result
->ts
.type
== BT_REAL
4429 && result
->expr_type
== EXPR_CONSTANT
);
4431 mpfr_set (result
->value
.real
, e
->value
.real
, GFC_RND_MODE
);
4432 mpfr_sqrt (result
->value
.real
, result
->value
.real
, GFC_RND_MODE
);
4438 gfc_simplify_norm2 (gfc_expr
*e
, gfc_expr
*dim
)
4442 if (!is_constant_array_expr (e
)
4443 || (dim
!= NULL
&& !gfc_is_constant_expr (dim
)))
4446 result
= transformational_result (e
, dim
, e
->ts
.type
, e
->ts
.kind
, &e
->where
);
4447 init_result_expr (result
, 0, NULL
);
4449 if (!dim
|| e
->rank
== 1)
4451 result
= simplify_transformation_to_scalar (result
, e
, NULL
,
4453 mpfr_sqrt (result
->value
.real
, result
->value
.real
, GFC_RND_MODE
);
4456 result
= simplify_transformation_to_array (result
, e
, dim
, NULL
,
4457 add_squared
, &do_sqrt
);
4464 gfc_simplify_not (gfc_expr
*e
)
4468 if (e
->expr_type
!= EXPR_CONSTANT
)
4471 result
= gfc_get_constant_expr (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
4472 mpz_com (result
->value
.integer
, e
->value
.integer
);
4474 return range_check (result
, "NOT");
4479 gfc_simplify_null (gfc_expr
*mold
)
4485 result
= gfc_copy_expr (mold
);
4486 result
->expr_type
= EXPR_NULL
;
4489 result
= gfc_get_null_expr (NULL
);
4496 gfc_simplify_num_images (void)
4500 if (gfc_option
.coarray
== GFC_FCOARRAY_NONE
)
4502 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
4503 return &gfc_bad_expr
;
4506 /* FIXME: gfc_current_locus is wrong. */
4507 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
4508 &gfc_current_locus
);
4509 mpz_set_si (result
->value
.integer
, 1);
4515 gfc_simplify_or (gfc_expr
*x
, gfc_expr
*y
)
4520 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
4523 kind
= x
->ts
.kind
> y
->ts
.kind
? x
->ts
.kind
: y
->ts
.kind
;
4528 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &x
->where
);
4529 mpz_ior (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
4530 return range_check (result
, "OR");
4533 return gfc_get_logical_expr (kind
, &x
->where
,
4534 x
->value
.logical
|| y
->value
.logical
);
4542 gfc_simplify_pack (gfc_expr
*array
, gfc_expr
*mask
, gfc_expr
*vector
)
4545 gfc_constructor
*array_ctor
, *mask_ctor
, *vector_ctor
;
4547 if (!is_constant_array_expr(array
)
4548 || !is_constant_array_expr(vector
)
4549 || (!gfc_is_constant_expr (mask
)
4550 && !is_constant_array_expr(mask
)))
4553 result
= gfc_get_array_expr (array
->ts
.type
, array
->ts
.kind
, &array
->where
);
4554 if (array
->ts
.type
== BT_DERIVED
)
4555 result
->ts
.u
.derived
= array
->ts
.u
.derived
;
4557 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
4558 vector_ctor
= vector
4559 ? gfc_constructor_first (vector
->value
.constructor
)
4562 if (mask
->expr_type
== EXPR_CONSTANT
4563 && mask
->value
.logical
)
4565 /* Copy all elements of ARRAY to RESULT. */
4568 gfc_constructor_append_expr (&result
->value
.constructor
,
4569 gfc_copy_expr (array_ctor
->expr
),
4572 array_ctor
= gfc_constructor_next (array_ctor
);
4573 vector_ctor
= gfc_constructor_next (vector_ctor
);
4576 else if (mask
->expr_type
== EXPR_ARRAY
)
4578 /* Copy only those elements of ARRAY to RESULT whose
4579 MASK equals .TRUE.. */
4580 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
4583 if (mask_ctor
->expr
->value
.logical
)
4585 gfc_constructor_append_expr (&result
->value
.constructor
,
4586 gfc_copy_expr (array_ctor
->expr
),
4588 vector_ctor
= gfc_constructor_next (vector_ctor
);
4591 array_ctor
= gfc_constructor_next (array_ctor
);
4592 mask_ctor
= gfc_constructor_next (mask_ctor
);
4596 /* Append any left-over elements from VECTOR to RESULT. */
4599 gfc_constructor_append_expr (&result
->value
.constructor
,
4600 gfc_copy_expr (vector_ctor
->expr
),
4602 vector_ctor
= gfc_constructor_next (vector_ctor
);
4605 result
->shape
= gfc_get_shape (1);
4606 gfc_array_size (result
, &result
->shape
[0]);
4608 if (array
->ts
.type
== BT_CHARACTER
)
4609 result
->ts
.u
.cl
= array
->ts
.u
.cl
;
4616 do_xor (gfc_expr
*result
, gfc_expr
*e
)
4618 gcc_assert (e
->ts
.type
== BT_LOGICAL
&& e
->expr_type
== EXPR_CONSTANT
);
4619 gcc_assert (result
->ts
.type
== BT_LOGICAL
4620 && result
->expr_type
== EXPR_CONSTANT
);
4622 result
->value
.logical
= result
->value
.logical
!= e
->value
.logical
;
4629 gfc_simplify_parity (gfc_expr
*e
, gfc_expr
*dim
)
4631 return simplify_transformation (e
, dim
, NULL
, 0, do_xor
);
4636 gfc_simplify_popcnt (gfc_expr
*e
)
4641 if (e
->expr_type
!= EXPR_CONSTANT
)
4644 k
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
4646 /* Convert argument to unsigned, then count the '1' bits. */
4647 mpz_init_set (x
, e
->value
.integer
);
4648 convert_mpz_to_unsigned (x
, gfc_integer_kinds
[k
].bit_size
);
4649 res
= mpz_popcount (x
);
4652 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, res
);
4657 gfc_simplify_poppar (gfc_expr
*e
)
4663 if (e
->expr_type
!= EXPR_CONSTANT
)
4666 popcnt
= gfc_simplify_popcnt (e
);
4667 gcc_assert (popcnt
);
4669 s
= gfc_extract_int (popcnt
, &i
);
4672 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, i
% 2);
4677 gfc_simplify_precision (gfc_expr
*e
)
4679 int i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
4680 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
,
4681 gfc_real_kinds
[i
].precision
);
4686 gfc_simplify_product (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
)
4688 return simplify_transformation (array
, dim
, mask
, 1, gfc_multiply
);
4693 gfc_simplify_radix (gfc_expr
*e
)
4696 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
4701 i
= gfc_integer_kinds
[i
].radix
;
4705 i
= gfc_real_kinds
[i
].radix
;
4712 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, i
);
4717 gfc_simplify_range (gfc_expr
*e
)
4720 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
4725 i
= gfc_integer_kinds
[i
].range
;
4730 i
= gfc_real_kinds
[i
].range
;
4737 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, i
);
4742 gfc_simplify_real (gfc_expr
*e
, gfc_expr
*k
)
4744 gfc_expr
*result
= NULL
;
4747 if (e
->ts
.type
== BT_COMPLEX
)
4748 kind
= get_kind (BT_REAL
, k
, "REAL", e
->ts
.kind
);
4750 kind
= get_kind (BT_REAL
, k
, "REAL", gfc_default_real_kind
);
4753 return &gfc_bad_expr
;
4755 if (e
->expr_type
!= EXPR_CONSTANT
)
4758 if (convert_boz (e
, kind
) == &gfc_bad_expr
)
4759 return &gfc_bad_expr
;
4761 result
= gfc_convert_constant (e
, BT_REAL
, kind
);
4762 if (result
== &gfc_bad_expr
)
4763 return &gfc_bad_expr
;
4765 return range_check (result
, "REAL");
4770 gfc_simplify_realpart (gfc_expr
*e
)
4774 if (e
->expr_type
!= EXPR_CONSTANT
)
4777 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
4778 mpc_real (result
->value
.real
, e
->value
.complex, GFC_RND_MODE
);
4780 return range_check (result
, "REALPART");
4784 gfc_simplify_repeat (gfc_expr
*e
, gfc_expr
*n
)
4787 int i
, j
, len
, ncop
, nlen
;
4789 bool have_length
= false;
4791 /* If NCOPIES isn't a constant, there's nothing we can do. */
4792 if (n
->expr_type
!= EXPR_CONSTANT
)
4795 /* If NCOPIES is negative, it's an error. */
4796 if (mpz_sgn (n
->value
.integer
) < 0)
4798 gfc_error ("Argument NCOPIES of REPEAT intrinsic is negative at %L",
4800 return &gfc_bad_expr
;
4803 /* If we don't know the character length, we can do no more. */
4804 if (e
->ts
.u
.cl
&& e
->ts
.u
.cl
->length
4805 && e
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
4807 len
= mpz_get_si (e
->ts
.u
.cl
->length
->value
.integer
);
4810 else if (e
->expr_type
== EXPR_CONSTANT
4811 && (e
->ts
.u
.cl
== NULL
|| e
->ts
.u
.cl
->length
== NULL
))
4813 len
= e
->value
.character
.length
;
4818 /* If the source length is 0, any value of NCOPIES is valid
4819 and everything behaves as if NCOPIES == 0. */
4822 mpz_set_ui (ncopies
, 0);
4824 mpz_set (ncopies
, n
->value
.integer
);
4826 /* Check that NCOPIES isn't too large. */
4832 /* Compute the maximum value allowed for NCOPIES: huge(cl) / len. */
4834 i
= gfc_validate_kind (BT_INTEGER
, gfc_charlen_int_kind
, false);
4838 mpz_tdiv_q (max
, gfc_integer_kinds
[i
].huge
,
4839 e
->ts
.u
.cl
->length
->value
.integer
);
4843 mpz_init_set_si (mlen
, len
);
4844 mpz_tdiv_q (max
, gfc_integer_kinds
[i
].huge
, mlen
);
4848 /* The check itself. */
4849 if (mpz_cmp (ncopies
, max
) > 0)
4852 mpz_clear (ncopies
);
4853 gfc_error ("Argument NCOPIES of REPEAT intrinsic is too large at %L",
4855 return &gfc_bad_expr
;
4860 mpz_clear (ncopies
);
4862 /* For further simplification, we need the character string to be
4864 if (e
->expr_type
!= EXPR_CONSTANT
)
4868 (e
->ts
.u
.cl
->length
&&
4869 mpz_sgn (e
->ts
.u
.cl
->length
->value
.integer
)) != 0)
4871 const char *res
= gfc_extract_int (n
, &ncop
);
4872 gcc_assert (res
== NULL
);
4877 len
= e
->value
.character
.length
;
4880 result
= gfc_get_constant_expr (BT_CHARACTER
, e
->ts
.kind
, &e
->where
);
4883 return gfc_get_character_expr (e
->ts
.kind
, &e
->where
, NULL
, 0);
4885 len
= e
->value
.character
.length
;
4888 result
= gfc_get_character_expr (e
->ts
.kind
, &e
->where
, NULL
, nlen
);
4889 for (i
= 0; i
< ncop
; i
++)
4890 for (j
= 0; j
< len
; j
++)
4891 result
->value
.character
.string
[j
+i
*len
]= e
->value
.character
.string
[j
];
4893 result
->value
.character
.string
[nlen
] = '\0'; /* For debugger */
4898 /* This one is a bear, but mainly has to do with shuffling elements. */
4901 gfc_simplify_reshape (gfc_expr
*source
, gfc_expr
*shape_exp
,
4902 gfc_expr
*pad
, gfc_expr
*order_exp
)
4904 int order
[GFC_MAX_DIMENSIONS
], shape
[GFC_MAX_DIMENSIONS
];
4905 int i
, rank
, npad
, x
[GFC_MAX_DIMENSIONS
];
4909 gfc_expr
*e
, *result
;
4911 /* Check that argument expression types are OK. */
4912 if (!is_constant_array_expr (source
)
4913 || !is_constant_array_expr (shape_exp
)
4914 || !is_constant_array_expr (pad
)
4915 || !is_constant_array_expr (order_exp
))
4918 /* Proceed with simplification, unpacking the array. */
4925 e
= gfc_constructor_lookup_expr (shape_exp
->value
.constructor
, rank
);
4929 gfc_extract_int (e
, &shape
[rank
]);
4931 gcc_assert (rank
>= 0 && rank
< GFC_MAX_DIMENSIONS
);
4932 gcc_assert (shape
[rank
] >= 0);
4937 gcc_assert (rank
> 0);
4939 /* Now unpack the order array if present. */
4940 if (order_exp
== NULL
)
4942 for (i
= 0; i
< rank
; i
++)
4947 for (i
= 0; i
< rank
; i
++)
4950 for (i
= 0; i
< rank
; i
++)
4952 e
= gfc_constructor_lookup_expr (order_exp
->value
.constructor
, i
);
4955 gfc_extract_int (e
, &order
[i
]);
4957 gcc_assert (order
[i
] >= 1 && order
[i
] <= rank
);
4959 gcc_assert (x
[order
[i
]] == 0);
4964 /* Count the elements in the source and padding arrays. */
4969 gfc_array_size (pad
, &size
);
4970 npad
= mpz_get_ui (size
);
4974 gfc_array_size (source
, &size
);
4975 nsource
= mpz_get_ui (size
);
4978 /* If it weren't for that pesky permutation we could just loop
4979 through the source and round out any shortage with pad elements.
4980 But no, someone just had to have the compiler do something the
4981 user should be doing. */
4983 for (i
= 0; i
< rank
; i
++)
4986 result
= gfc_get_array_expr (source
->ts
.type
, source
->ts
.kind
,
4988 if (source
->ts
.type
== BT_DERIVED
)
4989 result
->ts
.u
.derived
= source
->ts
.u
.derived
;
4990 result
->rank
= rank
;
4991 result
->shape
= gfc_get_shape (rank
);
4992 for (i
= 0; i
< rank
; i
++)
4993 mpz_init_set_ui (result
->shape
[i
], shape
[i
]);
4995 while (nsource
> 0 || npad
> 0)
4997 /* Figure out which element to extract. */
4998 mpz_set_ui (index
, 0);
5000 for (i
= rank
- 1; i
>= 0; i
--)
5002 mpz_add_ui (index
, index
, x
[order
[i
]]);
5004 mpz_mul_ui (index
, index
, shape
[order
[i
- 1]]);
5007 if (mpz_cmp_ui (index
, INT_MAX
) > 0)
5008 gfc_internal_error ("Reshaped array too large at %C");
5010 j
= mpz_get_ui (index
);
5013 e
= gfc_constructor_lookup_expr (source
->value
.constructor
, j
);
5016 gcc_assert (npad
> 0);
5020 e
= gfc_constructor_lookup_expr (pad
->value
.constructor
, j
);
5024 gfc_constructor_append_expr (&result
->value
.constructor
,
5025 gfc_copy_expr (e
), &e
->where
);
5027 /* Calculate the next element. */
5031 if (++x
[i
] < shape
[i
])
5047 gfc_simplify_rrspacing (gfc_expr
*x
)
5053 if (x
->expr_type
!= EXPR_CONSTANT
)
5056 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
5058 result
= gfc_get_constant_expr (BT_REAL
, x
->ts
.kind
, &x
->where
);
5059 mpfr_abs (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
5061 /* Special case x = -0 and 0. */
5062 if (mpfr_sgn (result
->value
.real
) == 0)
5064 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
5068 /* | x * 2**(-e) | * 2**p. */
5069 e
= - (long int) mpfr_get_exp (x
->value
.real
);
5070 mpfr_mul_2si (result
->value
.real
, result
->value
.real
, e
, GFC_RND_MODE
);
5072 p
= (long int) gfc_real_kinds
[i
].digits
;
5073 mpfr_mul_2si (result
->value
.real
, result
->value
.real
, p
, GFC_RND_MODE
);
5075 return range_check (result
, "RRSPACING");
5080 gfc_simplify_scale (gfc_expr
*x
, gfc_expr
*i
)
5082 int k
, neg_flag
, power
, exp_range
;
5083 mpfr_t scale
, radix
;
5086 if (x
->expr_type
!= EXPR_CONSTANT
|| i
->expr_type
!= EXPR_CONSTANT
)
5089 result
= gfc_get_constant_expr (BT_REAL
, x
->ts
.kind
, &x
->where
);
5091 if (mpfr_sgn (x
->value
.real
) == 0)
5093 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
5097 k
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, false);
5099 exp_range
= gfc_real_kinds
[k
].max_exponent
- gfc_real_kinds
[k
].min_exponent
;
5101 /* This check filters out values of i that would overflow an int. */
5102 if (mpz_cmp_si (i
->value
.integer
, exp_range
+ 2) > 0
5103 || mpz_cmp_si (i
->value
.integer
, -exp_range
- 2) < 0)
5105 gfc_error ("Result of SCALE overflows its kind at %L", &result
->where
);
5106 gfc_free_expr (result
);
5107 return &gfc_bad_expr
;
5110 /* Compute scale = radix ** power. */
5111 power
= mpz_get_si (i
->value
.integer
);
5121 gfc_set_model_kind (x
->ts
.kind
);
5124 mpfr_set_ui (radix
, gfc_real_kinds
[k
].radix
, GFC_RND_MODE
);
5125 mpfr_pow_ui (scale
, radix
, power
, GFC_RND_MODE
);
5128 mpfr_div (result
->value
.real
, x
->value
.real
, scale
, GFC_RND_MODE
);
5130 mpfr_mul (result
->value
.real
, x
->value
.real
, scale
, GFC_RND_MODE
);
5132 mpfr_clears (scale
, radix
, NULL
);
5134 return range_check (result
, "SCALE");
5138 /* Variants of strspn and strcspn that operate on wide characters. */
5141 wide_strspn (const gfc_char_t
*s1
, const gfc_char_t
*s2
)
5144 const gfc_char_t
*c
;
5148 for (c
= s2
; *c
; c
++)
5162 wide_strcspn (const gfc_char_t
*s1
, const gfc_char_t
*s2
)
5165 const gfc_char_t
*c
;
5169 for (c
= s2
; *c
; c
++)
5184 gfc_simplify_scan (gfc_expr
*e
, gfc_expr
*c
, gfc_expr
*b
, gfc_expr
*kind
)
5189 size_t indx
, len
, lenc
;
5190 int k
= get_kind (BT_INTEGER
, kind
, "SCAN", gfc_default_integer_kind
);
5193 return &gfc_bad_expr
;
5195 if (e
->expr_type
!= EXPR_CONSTANT
|| c
->expr_type
!= EXPR_CONSTANT
)
5198 if (b
!= NULL
&& b
->value
.logical
!= 0)
5203 len
= e
->value
.character
.length
;
5204 lenc
= c
->value
.character
.length
;
5206 if (len
== 0 || lenc
== 0)
5214 indx
= wide_strcspn (e
->value
.character
.string
,
5215 c
->value
.character
.string
) + 1;
5222 for (indx
= len
; indx
> 0; indx
--)
5224 for (i
= 0; i
< lenc
; i
++)
5226 if (c
->value
.character
.string
[i
]
5227 == e
->value
.character
.string
[indx
- 1])
5236 result
= gfc_get_int_expr (k
, &e
->where
, indx
);
5237 return range_check (result
, "SCAN");
5242 gfc_simplify_selected_char_kind (gfc_expr
*e
)
5246 if (e
->expr_type
!= EXPR_CONSTANT
)
5249 if (gfc_compare_with_Cstring (e
, "ascii", false) == 0
5250 || gfc_compare_with_Cstring (e
, "default", false) == 0)
5252 else if (gfc_compare_with_Cstring (e
, "iso_10646", false) == 0)
5257 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, kind
);
5262 gfc_simplify_selected_int_kind (gfc_expr
*e
)
5266 if (e
->expr_type
!= EXPR_CONSTANT
|| gfc_extract_int (e
, &range
) != NULL
)
5271 for (i
= 0; gfc_integer_kinds
[i
].kind
!= 0; i
++)
5272 if (gfc_integer_kinds
[i
].range
>= range
5273 && gfc_integer_kinds
[i
].kind
< kind
)
5274 kind
= gfc_integer_kinds
[i
].kind
;
5276 if (kind
== INT_MAX
)
5279 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, kind
);
5284 gfc_simplify_selected_real_kind (gfc_expr
*p
, gfc_expr
*q
, gfc_expr
*rdx
)
5286 int range
, precision
, radix
, i
, kind
, found_precision
, found_range
,
5288 locus
*loc
= &gfc_current_locus
;
5294 if (p
->expr_type
!= EXPR_CONSTANT
5295 || gfc_extract_int (p
, &precision
) != NULL
)
5304 if (q
->expr_type
!= EXPR_CONSTANT
5305 || gfc_extract_int (q
, &range
) != NULL
)
5316 if (rdx
->expr_type
!= EXPR_CONSTANT
5317 || gfc_extract_int (rdx
, &radix
) != NULL
)
5325 found_precision
= 0;
5329 for (i
= 0; gfc_real_kinds
[i
].kind
!= 0; i
++)
5331 if (gfc_real_kinds
[i
].precision
>= precision
)
5332 found_precision
= 1;
5334 if (gfc_real_kinds
[i
].range
>= range
)
5337 if (gfc_real_kinds
[i
].radix
>= radix
)
5340 if (gfc_real_kinds
[i
].precision
>= precision
5341 && gfc_real_kinds
[i
].range
>= range
5342 && gfc_real_kinds
[i
].radix
>= radix
&& gfc_real_kinds
[i
].kind
< kind
)
5343 kind
= gfc_real_kinds
[i
].kind
;
5346 if (kind
== INT_MAX
)
5348 if (found_radix
&& found_range
&& !found_precision
)
5350 else if (found_radix
&& found_precision
&& !found_range
)
5352 else if (found_radix
&& !found_precision
&& !found_range
)
5354 else if (found_radix
)
5360 return gfc_get_int_expr (gfc_default_integer_kind
, loc
, kind
);
5365 gfc_simplify_set_exponent (gfc_expr
*x
, gfc_expr
*i
)
5368 mpfr_t exp
, absv
, log2
, pow2
, frac
;
5371 if (x
->expr_type
!= EXPR_CONSTANT
|| i
->expr_type
!= EXPR_CONSTANT
)
5374 result
= gfc_get_constant_expr (BT_REAL
, x
->ts
.kind
, &x
->where
);
5376 if (mpfr_sgn (x
->value
.real
) == 0)
5378 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
5382 gfc_set_model_kind (x
->ts
.kind
);
5389 mpfr_abs (absv
, x
->value
.real
, GFC_RND_MODE
);
5390 mpfr_log2 (log2
, absv
, GFC_RND_MODE
);
5392 mpfr_trunc (log2
, log2
);
5393 mpfr_add_ui (exp
, log2
, 1, GFC_RND_MODE
);
5395 /* Old exponent value, and fraction. */
5396 mpfr_ui_pow (pow2
, 2, exp
, GFC_RND_MODE
);
5398 mpfr_div (frac
, absv
, pow2
, GFC_RND_MODE
);
5401 exp2
= (unsigned long) mpz_get_d (i
->value
.integer
);
5402 mpfr_mul_2exp (result
->value
.real
, frac
, exp2
, GFC_RND_MODE
);
5404 mpfr_clears (absv
, log2
, pow2
, frac
, NULL
);
5406 return range_check (result
, "SET_EXPONENT");
5411 gfc_simplify_shape (gfc_expr
*source
)
5413 mpz_t shape
[GFC_MAX_DIMENSIONS
];
5414 gfc_expr
*result
, *e
, *f
;
5419 if (source
->rank
== 0)
5420 return gfc_get_array_expr (BT_INTEGER
, gfc_default_integer_kind
,
5423 result
= gfc_get_array_expr (BT_INTEGER
, gfc_default_integer_kind
,
5426 if (source
->expr_type
== EXPR_VARIABLE
)
5428 ar
= gfc_find_array_ref (source
);
5429 t
= gfc_array_ref_shape (ar
, shape
);
5431 else if (source
->shape
)
5434 for (n
= 0; n
< source
->rank
; n
++)
5436 mpz_init (shape
[n
]);
5437 mpz_set (shape
[n
], source
->shape
[n
]);
5443 for (n
= 0; n
< source
->rank
; n
++)
5445 e
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
5450 mpz_set (e
->value
.integer
, shape
[n
]);
5451 mpz_clear (shape
[n
]);
5455 mpz_set_ui (e
->value
.integer
, n
+ 1);
5457 f
= gfc_simplify_size (source
, e
, NULL
);
5461 gfc_free_expr (result
);
5468 gfc_constructor_append_expr (&result
->value
.constructor
, e
, NULL
);
5476 gfc_simplify_size (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
)
5480 int k
= get_kind (BT_INTEGER
, kind
, "SIZE", gfc_default_integer_kind
);
5483 return &gfc_bad_expr
;
5485 /* For unary operations, the size of the result is given by the size
5486 of the operand. For binary ones, it's the size of the first operand
5487 unless it is scalar, then it is the size of the second. */
5488 if (array
->expr_type
== EXPR_OP
&& !array
->value
.op
.uop
)
5490 gfc_expr
* replacement
;
5491 gfc_expr
* simplified
;
5493 switch (array
->value
.op
.op
)
5495 /* Unary operations. */
5497 case INTRINSIC_UPLUS
:
5498 case INTRINSIC_UMINUS
:
5499 replacement
= array
->value
.op
.op1
;
5502 /* Binary operations. If any one of the operands is scalar, take
5503 the other one's size. If both of them are arrays, it does not
5504 matter -- try to find one with known shape, if possible. */
5506 if (array
->value
.op
.op1
->rank
== 0)
5507 replacement
= array
->value
.op
.op2
;
5508 else if (array
->value
.op
.op2
->rank
== 0)
5509 replacement
= array
->value
.op
.op1
;
5512 simplified
= gfc_simplify_size (array
->value
.op
.op1
, dim
, kind
);
5516 replacement
= array
->value
.op
.op2
;
5521 /* Try to reduce it directly if possible. */
5522 simplified
= gfc_simplify_size (replacement
, dim
, kind
);
5524 /* Otherwise, we build a new SIZE call. This is hopefully at least
5525 simpler than the original one. */
5527 simplified
= gfc_build_intrinsic_call ("size", array
->where
, 3,
5528 gfc_copy_expr (replacement
),
5529 gfc_copy_expr (dim
),
5530 gfc_copy_expr (kind
));
5537 if (gfc_array_size (array
, &size
) == FAILURE
)
5542 if (dim
->expr_type
!= EXPR_CONSTANT
)
5545 d
= mpz_get_ui (dim
->value
.integer
) - 1;
5546 if (gfc_array_dimen_size (array
, d
, &size
) == FAILURE
)
5550 return gfc_get_int_expr (k
, &array
->where
, mpz_get_si (size
));
5555 gfc_simplify_sign (gfc_expr
*x
, gfc_expr
*y
)
5559 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
5562 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
5567 mpz_abs (result
->value
.integer
, x
->value
.integer
);
5568 if (mpz_sgn (y
->value
.integer
) < 0)
5569 mpz_neg (result
->value
.integer
, result
->value
.integer
);
5573 if (gfc_option
.flag_sign_zero
)
5574 mpfr_copysign (result
->value
.real
, x
->value
.real
, y
->value
.real
,
5577 mpfr_setsign (result
->value
.real
, x
->value
.real
,
5578 mpfr_sgn (y
->value
.real
) < 0 ? 1 : 0, GFC_RND_MODE
);
5582 gfc_internal_error ("Bad type in gfc_simplify_sign");
5590 gfc_simplify_sin (gfc_expr
*x
)
5594 if (x
->expr_type
!= EXPR_CONSTANT
)
5597 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
5602 mpfr_sin (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
5606 gfc_set_model (x
->value
.real
);
5607 mpc_sin (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
5611 gfc_internal_error ("in gfc_simplify_sin(): Bad type");
5614 return range_check (result
, "SIN");
5619 gfc_simplify_sinh (gfc_expr
*x
)
5623 if (x
->expr_type
!= EXPR_CONSTANT
)
5626 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
5631 mpfr_sinh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
5635 mpc_sinh (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
5642 return range_check (result
, "SINH");
5646 /* The argument is always a double precision real that is converted to
5647 single precision. TODO: Rounding! */
5650 gfc_simplify_sngl (gfc_expr
*a
)
5654 if (a
->expr_type
!= EXPR_CONSTANT
)
5657 result
= gfc_real2real (a
, gfc_default_real_kind
);
5658 return range_check (result
, "SNGL");
5663 gfc_simplify_spacing (gfc_expr
*x
)
5669 if (x
->expr_type
!= EXPR_CONSTANT
)
5672 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
5674 result
= gfc_get_constant_expr (BT_REAL
, x
->ts
.kind
, &x
->where
);
5676 /* Special case x = 0 and -0. */
5677 mpfr_abs (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
5678 if (mpfr_sgn (result
->value
.real
) == 0)
5680 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].tiny
, GFC_RND_MODE
);
5684 /* In the Fortran 95 standard, the result is b**(e - p) where b, e, and p
5685 are the radix, exponent of x, and precision. This excludes the
5686 possibility of subnormal numbers. Fortran 2003 states the result is
5687 b**max(e - p, emin - 1). */
5689 ep
= (long int) mpfr_get_exp (x
->value
.real
) - gfc_real_kinds
[i
].digits
;
5690 en
= (long int) gfc_real_kinds
[i
].min_exponent
- 1;
5691 en
= en
> ep
? en
: ep
;
5693 mpfr_set_ui (result
->value
.real
, 1, GFC_RND_MODE
);
5694 mpfr_mul_2si (result
->value
.real
, result
->value
.real
, en
, GFC_RND_MODE
);
5696 return range_check (result
, "SPACING");
5701 gfc_simplify_spread (gfc_expr
*source
, gfc_expr
*dim_expr
, gfc_expr
*ncopies_expr
)
5703 gfc_expr
*result
= 0L;
5704 int i
, j
, dim
, ncopies
;
5707 if ((!gfc_is_constant_expr (source
)
5708 && !is_constant_array_expr (source
))
5709 || !gfc_is_constant_expr (dim_expr
)
5710 || !gfc_is_constant_expr (ncopies_expr
))
5713 gcc_assert (dim_expr
->ts
.type
== BT_INTEGER
);
5714 gfc_extract_int (dim_expr
, &dim
);
5715 dim
-= 1; /* zero-base DIM */
5717 gcc_assert (ncopies_expr
->ts
.type
== BT_INTEGER
);
5718 gfc_extract_int (ncopies_expr
, &ncopies
);
5719 ncopies
= MAX (ncopies
, 0);
5721 /* Do not allow the array size to exceed the limit for an array
5723 if (source
->expr_type
== EXPR_ARRAY
)
5725 if (gfc_array_size (source
, &size
) == FAILURE
)
5726 gfc_internal_error ("Failure getting length of a constant array.");
5729 mpz_init_set_ui (size
, 1);
5731 if (mpz_get_si (size
)*ncopies
> gfc_option
.flag_max_array_constructor
)
5734 if (source
->expr_type
== EXPR_CONSTANT
)
5736 gcc_assert (dim
== 0);
5738 result
= gfc_get_array_expr (source
->ts
.type
, source
->ts
.kind
,
5740 if (source
->ts
.type
== BT_DERIVED
)
5741 result
->ts
.u
.derived
= source
->ts
.u
.derived
;
5743 result
->shape
= gfc_get_shape (result
->rank
);
5744 mpz_init_set_si (result
->shape
[0], ncopies
);
5746 for (i
= 0; i
< ncopies
; ++i
)
5747 gfc_constructor_append_expr (&result
->value
.constructor
,
5748 gfc_copy_expr (source
), NULL
);
5750 else if (source
->expr_type
== EXPR_ARRAY
)
5752 int offset
, rstride
[GFC_MAX_DIMENSIONS
], extent
[GFC_MAX_DIMENSIONS
];
5753 gfc_constructor
*source_ctor
;
5755 gcc_assert (source
->rank
< GFC_MAX_DIMENSIONS
);
5756 gcc_assert (dim
>= 0 && dim
<= source
->rank
);
5758 result
= gfc_get_array_expr (source
->ts
.type
, source
->ts
.kind
,
5760 if (source
->ts
.type
== BT_DERIVED
)
5761 result
->ts
.u
.derived
= source
->ts
.u
.derived
;
5762 result
->rank
= source
->rank
+ 1;
5763 result
->shape
= gfc_get_shape (result
->rank
);
5765 for (i
= 0, j
= 0; i
< result
->rank
; ++i
)
5768 mpz_init_set (result
->shape
[i
], source
->shape
[j
++]);
5770 mpz_init_set_si (result
->shape
[i
], ncopies
);
5772 extent
[i
] = mpz_get_si (result
->shape
[i
]);
5773 rstride
[i
] = (i
== 0) ? 1 : rstride
[i
-1] * extent
[i
-1];
5777 for (source_ctor
= gfc_constructor_first (source
->value
.constructor
);
5778 source_ctor
; source_ctor
= gfc_constructor_next (source_ctor
))
5780 for (i
= 0; i
< ncopies
; ++i
)
5781 gfc_constructor_insert_expr (&result
->value
.constructor
,
5782 gfc_copy_expr (source_ctor
->expr
),
5783 NULL
, offset
+ i
* rstride
[dim
]);
5785 offset
+= (dim
== 0 ? ncopies
: 1);
5789 /* FIXME: Returning here avoids a regression in array_simplify_1.f90.
5790 Replace NULL with gcc_unreachable() after implementing
5791 gfc_simplify_cshift(). */
5794 if (source
->ts
.type
== BT_CHARACTER
)
5795 result
->ts
.u
.cl
= source
->ts
.u
.cl
;
5802 gfc_simplify_sqrt (gfc_expr
*e
)
5804 gfc_expr
*result
= NULL
;
5806 if (e
->expr_type
!= EXPR_CONSTANT
)
5812 if (mpfr_cmp_si (e
->value
.real
, 0) < 0)
5814 gfc_error ("Argument of SQRT at %L has a negative value",
5816 return &gfc_bad_expr
;
5818 result
= gfc_get_constant_expr (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
5819 mpfr_sqrt (result
->value
.real
, e
->value
.real
, GFC_RND_MODE
);
5823 gfc_set_model (e
->value
.real
);
5825 result
= gfc_get_constant_expr (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
5826 mpc_sqrt (result
->value
.complex, e
->value
.complex, GFC_MPC_RND_MODE
);
5830 gfc_internal_error ("invalid argument of SQRT at %L", &e
->where
);
5833 return range_check (result
, "SQRT");
5838 gfc_simplify_sum (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
)
5840 return simplify_transformation (array
, dim
, mask
, 0, gfc_add
);
5845 gfc_simplify_tan (gfc_expr
*x
)
5849 if (x
->expr_type
!= EXPR_CONSTANT
)
5852 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
5857 mpfr_tan (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
5861 mpc_tan (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
5868 return range_check (result
, "TAN");
5873 gfc_simplify_tanh (gfc_expr
*x
)
5877 if (x
->expr_type
!= EXPR_CONSTANT
)
5880 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
5885 mpfr_tanh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
5889 mpc_tanh (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
5896 return range_check (result
, "TANH");
5901 gfc_simplify_tiny (gfc_expr
*e
)
5906 i
= gfc_validate_kind (BT_REAL
, e
->ts
.kind
, false);
5908 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
5909 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].tiny
, GFC_RND_MODE
);
5916 gfc_simplify_trailz (gfc_expr
*e
)
5918 unsigned long tz
, bs
;
5921 if (e
->expr_type
!= EXPR_CONSTANT
)
5924 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
5925 bs
= gfc_integer_kinds
[i
].bit_size
;
5926 tz
= mpz_scan1 (e
->value
.integer
, 0);
5928 return gfc_get_int_expr (gfc_default_integer_kind
,
5929 &e
->where
, MIN (tz
, bs
));
5934 gfc_simplify_transfer (gfc_expr
*source
, gfc_expr
*mold
, gfc_expr
*size
)
5937 gfc_expr
*mold_element
;
5940 size_t result_elt_size
;
5943 unsigned char *buffer
;
5945 if (!gfc_is_constant_expr (source
)
5946 || (gfc_init_expr_flag
&& !gfc_is_constant_expr (mold
))
5947 || !gfc_is_constant_expr (size
))
5950 if (source
->expr_type
== EXPR_FUNCTION
)
5953 /* Calculate the size of the source. */
5954 if (source
->expr_type
== EXPR_ARRAY
5955 && gfc_array_size (source
, &tmp
) == FAILURE
)
5956 gfc_internal_error ("Failure getting length of a constant array.");
5958 source_size
= gfc_target_expr_size (source
);
5960 /* Create an empty new expression with the appropriate characteristics. */
5961 result
= gfc_get_constant_expr (mold
->ts
.type
, mold
->ts
.kind
,
5963 result
->ts
= mold
->ts
;
5965 mold_element
= mold
->expr_type
== EXPR_ARRAY
5966 ? gfc_constructor_first (mold
->value
.constructor
)->expr
5969 /* Set result character length, if needed. Note that this needs to be
5970 set even for array expressions, in order to pass this information into
5971 gfc_target_interpret_expr. */
5972 if (result
->ts
.type
== BT_CHARACTER
&& gfc_is_constant_expr (mold_element
))
5973 result
->value
.character
.length
= mold_element
->value
.character
.length
;
5975 /* Set the number of elements in the result, and determine its size. */
5976 result_elt_size
= gfc_target_expr_size (mold_element
);
5977 if (result_elt_size
== 0)
5979 gfc_free_expr (result
);
5983 if (mold
->expr_type
== EXPR_ARRAY
|| mold
->rank
|| size
)
5987 result
->expr_type
= EXPR_ARRAY
;
5991 result_length
= (size_t)mpz_get_ui (size
->value
.integer
);
5994 result_length
= source_size
/ result_elt_size
;
5995 if (result_length
* result_elt_size
< source_size
)
5999 result
->shape
= gfc_get_shape (1);
6000 mpz_init_set_ui (result
->shape
[0], result_length
);
6002 result_size
= result_length
* result_elt_size
;
6007 result_size
= result_elt_size
;
6010 if (gfc_option
.warn_surprising
&& source_size
< result_size
)
6011 gfc_warning("Intrinsic TRANSFER at %L has partly undefined result: "
6012 "source size %ld < result size %ld", &source
->where
,
6013 (long) source_size
, (long) result_size
);
6015 /* Allocate the buffer to store the binary version of the source. */
6016 buffer_size
= MAX (source_size
, result_size
);
6017 buffer
= (unsigned char*)alloca (buffer_size
);
6018 memset (buffer
, 0, buffer_size
);
6020 /* Now write source to the buffer. */
6021 gfc_target_encode_expr (source
, buffer
, buffer_size
);
6023 /* And read the buffer back into the new expression. */
6024 gfc_target_interpret_expr (buffer
, buffer_size
, result
);
6031 gfc_simplify_transpose (gfc_expr
*matrix
)
6033 int row
, matrix_rows
, col
, matrix_cols
;
6036 if (!is_constant_array_expr (matrix
))
6039 gcc_assert (matrix
->rank
== 2);
6041 result
= gfc_get_array_expr (matrix
->ts
.type
, matrix
->ts
.kind
,
6044 result
->shape
= gfc_get_shape (result
->rank
);
6045 mpz_set (result
->shape
[0], matrix
->shape
[1]);
6046 mpz_set (result
->shape
[1], matrix
->shape
[0]);
6048 if (matrix
->ts
.type
== BT_CHARACTER
)
6049 result
->ts
.u
.cl
= matrix
->ts
.u
.cl
;
6050 else if (matrix
->ts
.type
== BT_DERIVED
)
6051 result
->ts
.u
.derived
= matrix
->ts
.u
.derived
;
6053 matrix_rows
= mpz_get_si (matrix
->shape
[0]);
6054 matrix_cols
= mpz_get_si (matrix
->shape
[1]);
6055 for (row
= 0; row
< matrix_rows
; ++row
)
6056 for (col
= 0; col
< matrix_cols
; ++col
)
6058 gfc_expr
*e
= gfc_constructor_lookup_expr (matrix
->value
.constructor
,
6059 col
* matrix_rows
+ row
);
6060 gfc_constructor_insert_expr (&result
->value
.constructor
,
6061 gfc_copy_expr (e
), &matrix
->where
,
6062 row
* matrix_cols
+ col
);
6070 gfc_simplify_trim (gfc_expr
*e
)
6073 int count
, i
, len
, lentrim
;
6075 if (e
->expr_type
!= EXPR_CONSTANT
)
6078 len
= e
->value
.character
.length
;
6079 for (count
= 0, i
= 1; i
<= len
; ++i
)
6081 if (e
->value
.character
.string
[len
- i
] == ' ')
6087 lentrim
= len
- count
;
6089 result
= gfc_get_character_expr (e
->ts
.kind
, &e
->where
, NULL
, lentrim
);
6090 for (i
= 0; i
< lentrim
; i
++)
6091 result
->value
.character
.string
[i
] = e
->value
.character
.string
[i
];
6098 gfc_simplify_image_index (gfc_expr
*coarray
, gfc_expr
*sub
)
6103 gfc_constructor
*sub_cons
;
6107 if (!is_constant_array_expr (sub
))
6108 goto not_implemented
; /* return NULL;*/
6110 /* Follow any component references. */
6111 as
= coarray
->symtree
->n
.sym
->as
;
6112 for (ref
= coarray
->ref
; ref
; ref
= ref
->next
)
6113 if (ref
->type
== REF_COMPONENT
)
6116 if (as
->type
== AS_DEFERRED
)
6117 goto not_implemented
; /* return NULL;*/
6119 /* "valid sequence of cosubscripts" are required; thus, return 0 unless
6120 the cosubscript addresses the first image. */
6122 sub_cons
= gfc_constructor_first (sub
->value
.constructor
);
6125 for (d
= 1; d
<= as
->corank
; d
++)
6130 if (sub_cons
== NULL
)
6132 gfc_error ("Too few elements in expression for SUB= argument at %L",
6134 return &gfc_bad_expr
;
6137 ca_bound
= simplify_bound_dim (coarray
, NULL
, d
+ as
->rank
, 0, as
,
6139 if (ca_bound
== NULL
)
6140 goto not_implemented
; /* return NULL */
6142 if (ca_bound
== &gfc_bad_expr
)
6145 cmp
= mpz_cmp (ca_bound
->value
.integer
, sub_cons
->expr
->value
.integer
);
6149 gfc_free_expr (ca_bound
);
6150 sub_cons
= gfc_constructor_next (sub_cons
);
6154 first_image
= false;
6158 gfc_error ("Out of bounds in IMAGE_INDEX at %L for dimension %d, "
6159 "SUB has %ld and COARRAY lower bound is %ld)",
6161 mpz_get_si (sub_cons
->expr
->value
.integer
),
6162 mpz_get_si (ca_bound
->value
.integer
));
6163 gfc_free_expr (ca_bound
);
6164 return &gfc_bad_expr
;
6167 gfc_free_expr (ca_bound
);
6169 /* Check whether upperbound is valid for the multi-images case. */
6172 ca_bound
= simplify_bound_dim (coarray
, NULL
, d
+ as
->rank
, 1, as
,
6174 if (ca_bound
== &gfc_bad_expr
)
6177 if (ca_bound
&& ca_bound
->expr_type
== EXPR_CONSTANT
6178 && mpz_cmp (ca_bound
->value
.integer
,
6179 sub_cons
->expr
->value
.integer
) < 0)
6181 gfc_error ("Out of bounds in IMAGE_INDEX at %L for dimension %d, "
6182 "SUB has %ld and COARRAY upper bound is %ld)",
6184 mpz_get_si (sub_cons
->expr
->value
.integer
),
6185 mpz_get_si (ca_bound
->value
.integer
));
6186 gfc_free_expr (ca_bound
);
6187 return &gfc_bad_expr
;
6191 gfc_free_expr (ca_bound
);
6194 sub_cons
= gfc_constructor_next (sub_cons
);
6197 if (sub_cons
!= NULL
)
6199 gfc_error ("Too many elements in expression for SUB= argument at %L",
6201 return &gfc_bad_expr
;
6204 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
6205 &gfc_current_locus
);
6207 mpz_set_si (result
->value
.integer
, 1);
6209 mpz_set_si (result
->value
.integer
, 0);
6214 gfc_error ("Not yet implemented: IMAGE_INDEX for coarray with non-constant "
6215 "cobounds at %L", &coarray
->where
);
6216 return &gfc_bad_expr
;
6221 gfc_simplify_this_image (gfc_expr
*coarray
, gfc_expr
*dim
)
6227 if (coarray
== NULL
)
6230 /* FIXME: gfc_current_locus is wrong. */
6231 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
6232 &gfc_current_locus
);
6233 mpz_set_si (result
->value
.integer
, 1);
6237 gcc_assert (coarray
->expr_type
== EXPR_VARIABLE
);
6239 /* Follow any component references. */
6240 as
= coarray
->symtree
->n
.sym
->as
;
6241 for (ref
= coarray
->ref
; ref
; ref
= ref
->next
)
6242 if (ref
->type
== REF_COMPONENT
)
6245 if (as
->type
== AS_DEFERRED
)
6246 goto not_implemented
; /* return NULL;*/
6250 /* Multi-dimensional bounds. */
6251 gfc_expr
*bounds
[GFC_MAX_DIMENSIONS
];
6254 /* Simplify the bounds for each dimension. */
6255 for (d
= 0; d
< as
->corank
; d
++)
6257 bounds
[d
] = simplify_bound_dim (coarray
, NULL
, d
+ as
->rank
+ 1, 0,
6259 if (bounds
[d
] == NULL
|| bounds
[d
] == &gfc_bad_expr
)
6263 for (j
= 0; j
< d
; j
++)
6264 gfc_free_expr (bounds
[j
]);
6265 if (bounds
[d
] == NULL
)
6266 goto not_implemented
;
6271 /* Allocate the result expression. */
6272 e
= gfc_get_expr ();
6273 e
->where
= coarray
->where
;
6274 e
->expr_type
= EXPR_ARRAY
;
6275 e
->ts
.type
= BT_INTEGER
;
6276 e
->ts
.kind
= gfc_default_integer_kind
;
6279 e
->shape
= gfc_get_shape (1);
6280 mpz_init_set_ui (e
->shape
[0], as
->corank
);
6282 /* Create the constructor for this array. */
6283 for (d
= 0; d
< as
->corank
; d
++)
6284 gfc_constructor_append_expr (&e
->value
.constructor
,
6285 bounds
[d
], &e
->where
);
6292 /* A DIM argument is specified. */
6293 if (dim
->expr_type
!= EXPR_CONSTANT
)
6294 goto not_implemented
; /*return NULL;*/
6296 d
= mpz_get_si (dim
->value
.integer
);
6298 if (d
< 1 || d
> as
->corank
)
6300 gfc_error ("DIM argument at %L is out of bounds", &dim
->where
);
6301 return &gfc_bad_expr
;
6304 /*return simplify_bound_dim (coarray, NULL, d + as->rank, 0, as, NULL, true);*/
6305 e
= simplify_bound_dim (coarray
, NULL
, d
+ as
->rank
, 0, as
, NULL
, true);
6309 goto not_implemented
;
6313 gfc_error ("Not yet implemented: THIS_IMAGE for coarray with non-constant "
6314 "cobounds at %L", &coarray
->where
);
6315 return &gfc_bad_expr
;
6320 gfc_simplify_ubound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
)
6322 return simplify_bound (array
, dim
, kind
, 1);
6326 gfc_simplify_ucobound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
)
6329 /* return simplify_cobound (array, dim, kind, 1);*/
6331 e
= simplify_cobound (array
, dim
, kind
, 1);
6335 gfc_error ("Not yet implemented: UCOBOUND for coarray with non-constant "
6336 "cobounds at %L", &array
->where
);
6337 return &gfc_bad_expr
;
6342 gfc_simplify_unpack (gfc_expr
*vector
, gfc_expr
*mask
, gfc_expr
*field
)
6344 gfc_expr
*result
, *e
;
6345 gfc_constructor
*vector_ctor
, *mask_ctor
, *field_ctor
;
6347 if (!is_constant_array_expr (vector
)
6348 || !is_constant_array_expr (mask
)
6349 || (!gfc_is_constant_expr (field
)
6350 && !is_constant_array_expr(field
)))
6353 result
= gfc_get_array_expr (vector
->ts
.type
, vector
->ts
.kind
,
6355 if (vector
->ts
.type
== BT_DERIVED
)
6356 result
->ts
.u
.derived
= vector
->ts
.u
.derived
;
6357 result
->rank
= mask
->rank
;
6358 result
->shape
= gfc_copy_shape (mask
->shape
, mask
->rank
);
6360 if (vector
->ts
.type
== BT_CHARACTER
)
6361 result
->ts
.u
.cl
= vector
->ts
.u
.cl
;
6363 vector_ctor
= gfc_constructor_first (vector
->value
.constructor
);
6364 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
6366 = field
->expr_type
== EXPR_ARRAY
6367 ? gfc_constructor_first (field
->value
.constructor
)
6372 if (mask_ctor
->expr
->value
.logical
)
6374 gcc_assert (vector_ctor
);
6375 e
= gfc_copy_expr (vector_ctor
->expr
);
6376 vector_ctor
= gfc_constructor_next (vector_ctor
);
6378 else if (field
->expr_type
== EXPR_ARRAY
)
6379 e
= gfc_copy_expr (field_ctor
->expr
);
6381 e
= gfc_copy_expr (field
);
6383 gfc_constructor_append_expr (&result
->value
.constructor
, e
, NULL
);
6385 mask_ctor
= gfc_constructor_next (mask_ctor
);
6386 field_ctor
= gfc_constructor_next (field_ctor
);
6394 gfc_simplify_verify (gfc_expr
*s
, gfc_expr
*set
, gfc_expr
*b
, gfc_expr
*kind
)
6398 size_t index
, len
, lenset
;
6400 int k
= get_kind (BT_INTEGER
, kind
, "VERIFY", gfc_default_integer_kind
);
6403 return &gfc_bad_expr
;
6405 if (s
->expr_type
!= EXPR_CONSTANT
|| set
->expr_type
!= EXPR_CONSTANT
)
6408 if (b
!= NULL
&& b
->value
.logical
!= 0)
6413 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &s
->where
);
6415 len
= s
->value
.character
.length
;
6416 lenset
= set
->value
.character
.length
;
6420 mpz_set_ui (result
->value
.integer
, 0);
6428 mpz_set_ui (result
->value
.integer
, 1);
6432 index
= wide_strspn (s
->value
.character
.string
,
6433 set
->value
.character
.string
) + 1;
6442 mpz_set_ui (result
->value
.integer
, len
);
6445 for (index
= len
; index
> 0; index
--)
6447 for (i
= 0; i
< lenset
; i
++)
6449 if (s
->value
.character
.string
[index
- 1]
6450 == set
->value
.character
.string
[i
])
6458 mpz_set_ui (result
->value
.integer
, index
);
6464 gfc_simplify_xor (gfc_expr
*x
, gfc_expr
*y
)
6469 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
6472 kind
= x
->ts
.kind
> y
->ts
.kind
? x
->ts
.kind
: y
->ts
.kind
;
6477 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &x
->where
);
6478 mpz_xor (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
6479 return range_check (result
, "XOR");
6482 return gfc_get_logical_expr (kind
, &x
->where
,
6483 (x
->value
.logical
&& !y
->value
.logical
)
6484 || (!x
->value
.logical
&& y
->value
.logical
));
6492 /****************** Constant simplification *****************/
6494 /* Master function to convert one constant to another. While this is
6495 used as a simplification function, it requires the destination type
6496 and kind information which is supplied by a special case in
6500 gfc_convert_constant (gfc_expr
*e
, bt type
, int kind
)
6502 gfc_expr
*g
, *result
, *(*f
) (gfc_expr
*, int);
6517 f
= gfc_int2complex
;
6537 f
= gfc_real2complex
;
6548 f
= gfc_complex2int
;
6551 f
= gfc_complex2real
;
6554 f
= gfc_complex2complex
;
6580 f
= gfc_hollerith2int
;
6584 f
= gfc_hollerith2real
;
6588 f
= gfc_hollerith2complex
;
6592 f
= gfc_hollerith2character
;
6596 f
= gfc_hollerith2logical
;
6606 gfc_internal_error ("gfc_convert_constant(): Unexpected type");
6611 switch (e
->expr_type
)
6614 result
= f (e
, kind
);
6616 return &gfc_bad_expr
;
6620 if (!gfc_is_constant_expr (e
))
6623 result
= gfc_get_array_expr (type
, kind
, &e
->where
);
6624 result
->shape
= gfc_copy_shape (e
->shape
, e
->rank
);
6625 result
->rank
= e
->rank
;
6627 for (c
= gfc_constructor_first (e
->value
.constructor
);
6628 c
; c
= gfc_constructor_next (c
))
6631 if (c
->iterator
== NULL
)
6632 tmp
= f (c
->expr
, kind
);
6635 g
= gfc_convert_constant (c
->expr
, type
, kind
);
6636 if (g
== &gfc_bad_expr
)
6638 gfc_free_expr (result
);
6646 gfc_free_expr (result
);
6650 gfc_constructor_append_expr (&result
->value
.constructor
,
6664 /* Function for converting character constants. */
6666 gfc_convert_char_constant (gfc_expr
*e
, bt type ATTRIBUTE_UNUSED
, int kind
)
6671 if (!gfc_is_constant_expr (e
))
6674 if (e
->expr_type
== EXPR_CONSTANT
)
6676 /* Simple case of a scalar. */
6677 result
= gfc_get_constant_expr (BT_CHARACTER
, kind
, &e
->where
);
6679 return &gfc_bad_expr
;
6681 result
->value
.character
.length
= e
->value
.character
.length
;
6682 result
->value
.character
.string
6683 = gfc_get_wide_string (e
->value
.character
.length
+ 1);
6684 memcpy (result
->value
.character
.string
, e
->value
.character
.string
,
6685 (e
->value
.character
.length
+ 1) * sizeof (gfc_char_t
));
6687 /* Check we only have values representable in the destination kind. */
6688 for (i
= 0; i
< result
->value
.character
.length
; i
++)
6689 if (!gfc_check_character_range (result
->value
.character
.string
[i
],
6692 gfc_error ("Character '%s' in string at %L cannot be converted "
6693 "into character kind %d",
6694 gfc_print_wide_char (result
->value
.character
.string
[i
]),
6696 return &gfc_bad_expr
;
6701 else if (e
->expr_type
== EXPR_ARRAY
)
6703 /* For an array constructor, we convert each constructor element. */
6706 result
= gfc_get_array_expr (type
, kind
, &e
->where
);
6707 result
->shape
= gfc_copy_shape (e
->shape
, e
->rank
);
6708 result
->rank
= e
->rank
;
6709 result
->ts
.u
.cl
= e
->ts
.u
.cl
;
6711 for (c
= gfc_constructor_first (e
->value
.constructor
);
6712 c
; c
= gfc_constructor_next (c
))
6714 gfc_expr
*tmp
= gfc_convert_char_constant (c
->expr
, type
, kind
);
6715 if (tmp
== &gfc_bad_expr
)
6717 gfc_free_expr (result
);
6718 return &gfc_bad_expr
;
6723 gfc_free_expr (result
);
6727 gfc_constructor_append_expr (&result
->value
.constructor
,