1 /* Simplify intrinsic functions at compile-time.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
3 Contributed by Andy Vaught & Katherine Holcomb
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
27 #include "intrinsic.h"
28 #include "target-memory.h"
29 #include "constructor.h"
30 #include "tm.h" /* For BITS_PER_UNIT. */
31 #include "version.h" /* For version_string. */
34 gfc_expr gfc_bad_expr
;
36 static gfc_expr
*simplify_size (gfc_expr
*, gfc_expr
*, int);
39 /* Note that 'simplification' is not just transforming expressions.
40 For functions that are not simplified at compile time, range
41 checking is done if possible.
43 The return convention is that each simplification function returns:
45 A new expression node corresponding to the simplified arguments.
46 The original arguments are destroyed by the caller, and must not
47 be a part of the new expression.
49 NULL pointer indicating that no simplification was possible and
50 the original expression should remain intact.
52 An expression pointer to gfc_bad_expr (a static placeholder)
53 indicating that some error has prevented simplification. The
54 error is generated within the function and should be propagated
57 By the time a simplification function gets control, it has been
58 decided that the function call is really supposed to be the
59 intrinsic. No type checking is strictly necessary, since only
60 valid types will be passed on. On the other hand, a simplification
61 subroutine may have to look at the type of an argument as part of
64 Array arguments are only passed to these subroutines that implement
65 the simplification of transformational intrinsics.
67 The functions in this file don't have much comment with them, but
68 everything is reasonably straight-forward. The Standard, chapter 13
69 is the best comment you'll find for this file anyway. */
71 /* Range checks an expression node. If all goes well, returns the
72 node, otherwise returns &gfc_bad_expr and frees the node. */
75 range_check (gfc_expr
*result
, const char *name
)
80 if (result
->expr_type
!= EXPR_CONSTANT
)
83 switch (gfc_range_check (result
))
89 gfc_error ("Result of %s overflows its kind at %L", name
,
94 gfc_error ("Result of %s underflows its kind at %L", name
,
99 gfc_error ("Result of %s is NaN at %L", name
, &result
->where
);
103 gfc_error ("Result of %s gives range error for its kind at %L", name
,
108 gfc_free_expr (result
);
109 return &gfc_bad_expr
;
113 /* A helper function that gets an optional and possibly missing
114 kind parameter. Returns the kind, -1 if something went wrong. */
117 get_kind (bt type
, gfc_expr
*k
, const char *name
, int default_kind
)
124 if (k
->expr_type
!= EXPR_CONSTANT
)
126 gfc_error ("KIND parameter of %s at %L must be an initialization "
127 "expression", name
, &k
->where
);
131 if (gfc_extract_int (k
, &kind
) != NULL
132 || gfc_validate_kind (type
, kind
, true) < 0)
134 gfc_error ("Invalid KIND parameter of %s at %L", name
, &k
->where
);
142 /* Converts an mpz_t signed variable into an unsigned one, assuming
143 two's complement representations and a binary width of bitsize.
144 The conversion is a no-op unless x is negative; otherwise, it can
145 be accomplished by masking out the high bits. */
148 convert_mpz_to_unsigned (mpz_t x
, int bitsize
)
154 /* Confirm that no bits above the signed range are unset. */
155 gcc_assert (mpz_scan0 (x
, bitsize
-1) == ULONG_MAX
);
157 mpz_init_set_ui (mask
, 1);
158 mpz_mul_2exp (mask
, mask
, bitsize
);
159 mpz_sub_ui (mask
, mask
, 1);
161 mpz_and (x
, x
, mask
);
167 /* Confirm that no bits above the signed range are set. */
168 gcc_assert (mpz_scan1 (x
, bitsize
-1) == ULONG_MAX
);
173 /* Converts an mpz_t unsigned variable into a signed one, assuming
174 two's complement representations and a binary width of bitsize.
175 If the bitsize-1 bit is set, this is taken as a sign bit and
176 the number is converted to the corresponding negative number. */
179 convert_mpz_to_signed (mpz_t x
, int bitsize
)
183 /* Confirm that no bits above the unsigned range are set. */
184 gcc_assert (mpz_scan1 (x
, bitsize
) == ULONG_MAX
);
186 if (mpz_tstbit (x
, bitsize
- 1) == 1)
188 mpz_init_set_ui (mask
, 1);
189 mpz_mul_2exp (mask
, mask
, bitsize
);
190 mpz_sub_ui (mask
, mask
, 1);
192 /* We negate the number by hand, zeroing the high bits, that is
193 make it the corresponding positive number, and then have it
194 negated by GMP, giving the correct representation of the
197 mpz_add_ui (x
, x
, 1);
198 mpz_and (x
, x
, mask
);
207 /* In-place convert BOZ to REAL of the specified kind. */
210 convert_boz (gfc_expr
*x
, int kind
)
212 if (x
&& x
->ts
.type
== BT_INTEGER
&& x
->is_boz
)
219 if (!gfc_convert_boz (x
, &ts
))
220 return &gfc_bad_expr
;
227 /* Test that the expression is an constant array. */
230 is_constant_array_expr (gfc_expr
*e
)
237 if (e
->expr_type
!= EXPR_ARRAY
|| !gfc_is_constant_expr (e
))
240 for (c
= gfc_constructor_first (e
->value
.constructor
);
241 c
; c
= gfc_constructor_next (c
))
242 if (c
->expr
->expr_type
!= EXPR_CONSTANT
243 && c
->expr
->expr_type
!= EXPR_STRUCTURE
)
250 /* Initialize a transformational result expression with a given value. */
253 init_result_expr (gfc_expr
*e
, int init
, gfc_expr
*array
)
255 if (e
&& e
->expr_type
== EXPR_ARRAY
)
257 gfc_constructor
*ctor
= gfc_constructor_first (e
->value
.constructor
);
260 init_result_expr (ctor
->expr
, init
, array
);
261 ctor
= gfc_constructor_next (ctor
);
264 else if (e
&& e
->expr_type
== EXPR_CONSTANT
)
266 int i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
273 e
->value
.logical
= (init
? 1 : 0);
278 mpz_set (e
->value
.integer
, gfc_integer_kinds
[i
].min_int
);
279 else if (init
== INT_MAX
)
280 mpz_set (e
->value
.integer
, gfc_integer_kinds
[i
].huge
);
282 mpz_set_si (e
->value
.integer
, init
);
288 mpfr_set (e
->value
.real
, gfc_real_kinds
[i
].huge
, GFC_RND_MODE
);
289 mpfr_neg (e
->value
.real
, e
->value
.real
, GFC_RND_MODE
);
291 else if (init
== INT_MAX
)
292 mpfr_set (e
->value
.real
, gfc_real_kinds
[i
].huge
, GFC_RND_MODE
);
294 mpfr_set_si (e
->value
.real
, init
, GFC_RND_MODE
);
298 mpc_set_si (e
->value
.complex, init
, GFC_MPC_RND_MODE
);
304 gfc_expr
*len
= gfc_simplify_len (array
, NULL
);
305 gfc_extract_int (len
, &length
);
306 string
= gfc_get_wide_string (length
+ 1);
307 gfc_wide_memset (string
, 0, length
);
309 else if (init
== INT_MAX
)
311 gfc_expr
*len
= gfc_simplify_len (array
, NULL
);
312 gfc_extract_int (len
, &length
);
313 string
= gfc_get_wide_string (length
+ 1);
314 gfc_wide_memset (string
, 255, length
);
319 string
= gfc_get_wide_string (1);
322 string
[length
] = '\0';
323 e
->value
.character
.length
= length
;
324 e
->value
.character
.string
= string
;
336 /* Helper function for gfc_simplify_dot_product() and gfc_simplify_matmul;
337 if conj_a is true, the matrix_a is complex conjugated. */
340 compute_dot_product (gfc_expr
*matrix_a
, int stride_a
, int offset_a
,
341 gfc_expr
*matrix_b
, int stride_b
, int offset_b
,
344 gfc_expr
*result
, *a
, *b
, *c
;
346 result
= gfc_get_constant_expr (matrix_a
->ts
.type
, matrix_a
->ts
.kind
,
348 init_result_expr (result
, 0, NULL
);
350 a
= gfc_constructor_lookup_expr (matrix_a
->value
.constructor
, offset_a
);
351 b
= gfc_constructor_lookup_expr (matrix_b
->value
.constructor
, offset_b
);
354 /* Copying of expressions is required as operands are free'd
355 by the gfc_arith routines. */
356 switch (result
->ts
.type
)
359 result
= gfc_or (result
,
360 gfc_and (gfc_copy_expr (a
),
367 if (conj_a
&& a
->ts
.type
== BT_COMPLEX
)
368 c
= gfc_simplify_conjg (a
);
370 c
= gfc_copy_expr (a
);
371 result
= gfc_add (result
, gfc_multiply (c
, gfc_copy_expr (b
)));
378 offset_a
+= stride_a
;
379 a
= gfc_constructor_lookup_expr (matrix_a
->value
.constructor
, offset_a
);
381 offset_b
+= stride_b
;
382 b
= gfc_constructor_lookup_expr (matrix_b
->value
.constructor
, offset_b
);
389 /* Build a result expression for transformational intrinsics,
393 transformational_result (gfc_expr
*array
, gfc_expr
*dim
, bt type
,
394 int kind
, locus
* where
)
399 if (!dim
|| array
->rank
== 1)
400 return gfc_get_constant_expr (type
, kind
, where
);
402 result
= gfc_get_array_expr (type
, kind
, where
);
403 result
->shape
= gfc_copy_shape_excluding (array
->shape
, array
->rank
, dim
);
404 result
->rank
= array
->rank
- 1;
406 /* gfc_array_size() would count the number of elements in the constructor,
407 we have not built those yet. */
409 for (i
= 0; i
< result
->rank
; ++i
)
410 nelem
*= mpz_get_ui (result
->shape
[i
]);
412 for (i
= 0; i
< nelem
; ++i
)
414 gfc_constructor_append_expr (&result
->value
.constructor
,
415 gfc_get_constant_expr (type
, kind
, where
),
423 typedef gfc_expr
* (*transformational_op
)(gfc_expr
*, gfc_expr
*);
425 /* Wrapper function, implements 'op1 += 1'. Only called if MASK
426 of COUNT intrinsic is .TRUE..
428 Interface and implementation mimics arith functions as
429 gfc_add, gfc_multiply, etc. */
431 static gfc_expr
* gfc_count (gfc_expr
*op1
, gfc_expr
*op2
)
435 gcc_assert (op1
->ts
.type
== BT_INTEGER
);
436 gcc_assert (op2
->ts
.type
== BT_LOGICAL
);
437 gcc_assert (op2
->value
.logical
);
439 result
= gfc_copy_expr (op1
);
440 mpz_add_ui (result
->value
.integer
, result
->value
.integer
, 1);
448 /* Transforms an ARRAY with operation OP, according to MASK, to a
449 scalar RESULT. E.g. called if
451 REAL, PARAMETER :: array(n, m) = ...
452 REAL, PARAMETER :: s = SUM(array)
454 where OP == gfc_add(). */
457 simplify_transformation_to_scalar (gfc_expr
*result
, gfc_expr
*array
, gfc_expr
*mask
,
458 transformational_op op
)
461 gfc_constructor
*array_ctor
, *mask_ctor
;
463 /* Shortcut for constant .FALSE. MASK. */
465 && mask
->expr_type
== EXPR_CONSTANT
466 && !mask
->value
.logical
)
469 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
471 if (mask
&& mask
->expr_type
== EXPR_ARRAY
)
472 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
476 a
= array_ctor
->expr
;
477 array_ctor
= gfc_constructor_next (array_ctor
);
479 /* A constant MASK equals .TRUE. here and can be ignored. */
483 mask_ctor
= gfc_constructor_next (mask_ctor
);
484 if (!m
->value
.logical
)
488 result
= op (result
, gfc_copy_expr (a
));
494 /* Transforms an ARRAY with operation OP, according to MASK, to an
495 array RESULT. E.g. called if
497 REAL, PARAMETER :: array(n, m) = ...
498 REAL, PARAMETER :: s(n) = PROD(array, DIM=1)
500 where OP == gfc_multiply(). The result might be post processed using post_op. */
503 simplify_transformation_to_array (gfc_expr
*result
, gfc_expr
*array
, gfc_expr
*dim
,
504 gfc_expr
*mask
, transformational_op op
,
505 transformational_op post_op
)
508 int done
, i
, n
, arraysize
, resultsize
, dim_index
, dim_extent
, dim_stride
;
509 gfc_expr
**arrayvec
, **resultvec
, **base
, **src
, **dest
;
510 gfc_constructor
*array_ctor
, *mask_ctor
, *result_ctor
;
512 int count
[GFC_MAX_DIMENSIONS
], extent
[GFC_MAX_DIMENSIONS
],
513 sstride
[GFC_MAX_DIMENSIONS
], dstride
[GFC_MAX_DIMENSIONS
],
514 tmpstride
[GFC_MAX_DIMENSIONS
];
516 /* Shortcut for constant .FALSE. MASK. */
518 && mask
->expr_type
== EXPR_CONSTANT
519 && !mask
->value
.logical
)
522 /* Build an indexed table for array element expressions to minimize
523 linked-list traversal. Masked elements are set to NULL. */
524 gfc_array_size (array
, &size
);
525 arraysize
= mpz_get_ui (size
);
528 arrayvec
= XCNEWVEC (gfc_expr
*, arraysize
);
530 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
532 if (mask
&& mask
->expr_type
== EXPR_ARRAY
)
533 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
535 for (i
= 0; i
< arraysize
; ++i
)
537 arrayvec
[i
] = array_ctor
->expr
;
538 array_ctor
= gfc_constructor_next (array_ctor
);
542 if (!mask_ctor
->expr
->value
.logical
)
545 mask_ctor
= gfc_constructor_next (mask_ctor
);
549 /* Same for the result expression. */
550 gfc_array_size (result
, &size
);
551 resultsize
= mpz_get_ui (size
);
554 resultvec
= XCNEWVEC (gfc_expr
*, resultsize
);
555 result_ctor
= gfc_constructor_first (result
->value
.constructor
);
556 for (i
= 0; i
< resultsize
; ++i
)
558 resultvec
[i
] = result_ctor
->expr
;
559 result_ctor
= gfc_constructor_next (result_ctor
);
562 gfc_extract_int (dim
, &dim_index
);
563 dim_index
-= 1; /* zero-base index */
567 for (i
= 0, n
= 0; i
< array
->rank
; ++i
)
570 tmpstride
[i
] = (i
== 0) ? 1 : tmpstride
[i
-1] * mpz_get_si (array
->shape
[i
-1]);
573 dim_extent
= mpz_get_si (array
->shape
[i
]);
574 dim_stride
= tmpstride
[i
];
578 extent
[n
] = mpz_get_si (array
->shape
[i
]);
579 sstride
[n
] = tmpstride
[i
];
580 dstride
[n
] = (n
== 0) ? 1 : dstride
[n
-1] * extent
[n
-1];
589 for (src
= base
, n
= 0; n
< dim_extent
; src
+= dim_stride
, ++n
)
591 *dest
= op (*dest
, gfc_copy_expr (*src
));
598 while (!done
&& count
[n
] == extent
[n
])
601 base
-= sstride
[n
] * extent
[n
];
602 dest
-= dstride
[n
] * extent
[n
];
605 if (n
< result
->rank
)
616 /* Place updated expression in result constructor. */
617 result_ctor
= gfc_constructor_first (result
->value
.constructor
);
618 for (i
= 0; i
< resultsize
; ++i
)
621 result_ctor
->expr
= post_op (result_ctor
->expr
, resultvec
[i
]);
623 result_ctor
->expr
= resultvec
[i
];
624 result_ctor
= gfc_constructor_next (result_ctor
);
634 simplify_transformation (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
,
635 int init_val
, transformational_op op
)
639 if (!is_constant_array_expr (array
)
640 || !gfc_is_constant_expr (dim
))
644 && !is_constant_array_expr (mask
)
645 && mask
->expr_type
!= EXPR_CONSTANT
)
648 result
= transformational_result (array
, dim
, array
->ts
.type
,
649 array
->ts
.kind
, &array
->where
);
650 init_result_expr (result
, init_val
, NULL
);
652 return !dim
|| array
->rank
== 1 ?
653 simplify_transformation_to_scalar (result
, array
, mask
, op
) :
654 simplify_transformation_to_array (result
, array
, dim
, mask
, op
, NULL
);
658 /********************** Simplification functions *****************************/
661 gfc_simplify_abs (gfc_expr
*e
)
665 if (e
->expr_type
!= EXPR_CONSTANT
)
671 result
= gfc_get_constant_expr (BT_INTEGER
, e
->ts
.kind
, &e
->where
);
672 mpz_abs (result
->value
.integer
, e
->value
.integer
);
673 return range_check (result
, "IABS");
676 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
677 mpfr_abs (result
->value
.real
, e
->value
.real
, GFC_RND_MODE
);
678 return range_check (result
, "ABS");
681 gfc_set_model_kind (e
->ts
.kind
);
682 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
683 mpc_abs (result
->value
.real
, e
->value
.complex, GFC_RND_MODE
);
684 return range_check (result
, "CABS");
687 gfc_internal_error ("gfc_simplify_abs(): Bad type");
693 simplify_achar_char (gfc_expr
*e
, gfc_expr
*k
, const char *name
, bool ascii
)
697 bool too_large
= false;
699 if (e
->expr_type
!= EXPR_CONSTANT
)
702 kind
= get_kind (BT_CHARACTER
, k
, name
, gfc_default_character_kind
);
704 return &gfc_bad_expr
;
706 if (mpz_cmp_si (e
->value
.integer
, 0) < 0)
708 gfc_error ("Argument of %s function at %L is negative", name
,
710 return &gfc_bad_expr
;
713 if (ascii
&& gfc_option
.warn_surprising
714 && mpz_cmp_si (e
->value
.integer
, 127) > 0)
715 gfc_warning ("Argument of %s function at %L outside of range [0,127]",
718 if (kind
== 1 && mpz_cmp_si (e
->value
.integer
, 255) > 0)
723 mpz_init_set_ui (t
, 2);
724 mpz_pow_ui (t
, t
, 32);
725 mpz_sub_ui (t
, t
, 1);
726 if (mpz_cmp (e
->value
.integer
, t
) > 0)
733 gfc_error ("Argument of %s function at %L is too large for the "
734 "collating sequence of kind %d", name
, &e
->where
, kind
);
735 return &gfc_bad_expr
;
738 result
= gfc_get_character_expr (kind
, &e
->where
, NULL
, 1);
739 result
->value
.character
.string
[0] = mpz_get_ui (e
->value
.integer
);
746 /* We use the processor's collating sequence, because all
747 systems that gfortran currently works on are ASCII. */
750 gfc_simplify_achar (gfc_expr
*e
, gfc_expr
*k
)
752 return simplify_achar_char (e
, k
, "ACHAR", true);
757 gfc_simplify_acos (gfc_expr
*x
)
761 if (x
->expr_type
!= EXPR_CONSTANT
)
767 if (mpfr_cmp_si (x
->value
.real
, 1) > 0
768 || mpfr_cmp_si (x
->value
.real
, -1) < 0)
770 gfc_error ("Argument of ACOS at %L must be between -1 and 1",
772 return &gfc_bad_expr
;
774 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
775 mpfr_acos (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
779 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
780 mpc_acos (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
784 gfc_internal_error ("in gfc_simplify_acos(): Bad type");
787 return range_check (result
, "ACOS");
791 gfc_simplify_acosh (gfc_expr
*x
)
795 if (x
->expr_type
!= EXPR_CONSTANT
)
801 if (mpfr_cmp_si (x
->value
.real
, 1) < 0)
803 gfc_error ("Argument of ACOSH at %L must not be less than 1",
805 return &gfc_bad_expr
;
808 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
809 mpfr_acosh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
813 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
814 mpc_acosh (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
818 gfc_internal_error ("in gfc_simplify_acosh(): Bad type");
821 return range_check (result
, "ACOSH");
825 gfc_simplify_adjustl (gfc_expr
*e
)
831 if (e
->expr_type
!= EXPR_CONSTANT
)
834 len
= e
->value
.character
.length
;
836 for (count
= 0, i
= 0; i
< len
; ++i
)
838 ch
= e
->value
.character
.string
[i
];
844 result
= gfc_get_character_expr (e
->ts
.kind
, &e
->where
, NULL
, len
);
845 for (i
= 0; i
< len
- count
; ++i
)
846 result
->value
.character
.string
[i
] = e
->value
.character
.string
[count
+ i
];
853 gfc_simplify_adjustr (gfc_expr
*e
)
859 if (e
->expr_type
!= EXPR_CONSTANT
)
862 len
= e
->value
.character
.length
;
864 for (count
= 0, i
= len
- 1; i
>= 0; --i
)
866 ch
= e
->value
.character
.string
[i
];
872 result
= gfc_get_character_expr (e
->ts
.kind
, &e
->where
, NULL
, len
);
873 for (i
= 0; i
< count
; ++i
)
874 result
->value
.character
.string
[i
] = ' ';
876 for (i
= count
; i
< len
; ++i
)
877 result
->value
.character
.string
[i
] = e
->value
.character
.string
[i
- count
];
884 gfc_simplify_aimag (gfc_expr
*e
)
888 if (e
->expr_type
!= EXPR_CONSTANT
)
891 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
892 mpfr_set (result
->value
.real
, mpc_imagref (e
->value
.complex), GFC_RND_MODE
);
894 return range_check (result
, "AIMAG");
899 gfc_simplify_aint (gfc_expr
*e
, gfc_expr
*k
)
901 gfc_expr
*rtrunc
, *result
;
904 kind
= get_kind (BT_REAL
, k
, "AINT", e
->ts
.kind
);
906 return &gfc_bad_expr
;
908 if (e
->expr_type
!= EXPR_CONSTANT
)
911 rtrunc
= gfc_copy_expr (e
);
912 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
914 result
= gfc_real2real (rtrunc
, kind
);
916 gfc_free_expr (rtrunc
);
918 return range_check (result
, "AINT");
923 gfc_simplify_all (gfc_expr
*mask
, gfc_expr
*dim
)
925 return simplify_transformation (mask
, dim
, NULL
, true, gfc_and
);
930 gfc_simplify_dint (gfc_expr
*e
)
932 gfc_expr
*rtrunc
, *result
;
934 if (e
->expr_type
!= EXPR_CONSTANT
)
937 rtrunc
= gfc_copy_expr (e
);
938 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
940 result
= gfc_real2real (rtrunc
, gfc_default_double_kind
);
942 gfc_free_expr (rtrunc
);
944 return range_check (result
, "DINT");
949 gfc_simplify_dreal (gfc_expr
*e
)
951 gfc_expr
*result
= NULL
;
953 if (e
->expr_type
!= EXPR_CONSTANT
)
956 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
957 mpc_real (result
->value
.real
, e
->value
.complex, GFC_RND_MODE
);
959 return range_check (result
, "DREAL");
964 gfc_simplify_anint (gfc_expr
*e
, gfc_expr
*k
)
969 kind
= get_kind (BT_REAL
, k
, "ANINT", e
->ts
.kind
);
971 return &gfc_bad_expr
;
973 if (e
->expr_type
!= EXPR_CONSTANT
)
976 result
= gfc_get_constant_expr (e
->ts
.type
, kind
, &e
->where
);
977 mpfr_round (result
->value
.real
, e
->value
.real
);
979 return range_check (result
, "ANINT");
984 gfc_simplify_and (gfc_expr
*x
, gfc_expr
*y
)
989 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
992 kind
= x
->ts
.kind
> y
->ts
.kind
? x
->ts
.kind
: y
->ts
.kind
;
997 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &x
->where
);
998 mpz_and (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
999 return range_check (result
, "AND");
1002 return gfc_get_logical_expr (kind
, &x
->where
,
1003 x
->value
.logical
&& y
->value
.logical
);
1012 gfc_simplify_any (gfc_expr
*mask
, gfc_expr
*dim
)
1014 return simplify_transformation (mask
, dim
, NULL
, false, gfc_or
);
1019 gfc_simplify_dnint (gfc_expr
*e
)
1023 if (e
->expr_type
!= EXPR_CONSTANT
)
1026 result
= gfc_get_constant_expr (BT_REAL
, gfc_default_double_kind
, &e
->where
);
1027 mpfr_round (result
->value
.real
, e
->value
.real
);
1029 return range_check (result
, "DNINT");
1034 gfc_simplify_asin (gfc_expr
*x
)
1038 if (x
->expr_type
!= EXPR_CONSTANT
)
1044 if (mpfr_cmp_si (x
->value
.real
, 1) > 0
1045 || mpfr_cmp_si (x
->value
.real
, -1) < 0)
1047 gfc_error ("Argument of ASIN at %L must be between -1 and 1",
1049 return &gfc_bad_expr
;
1051 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1052 mpfr_asin (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1056 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1057 mpc_asin (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1061 gfc_internal_error ("in gfc_simplify_asin(): Bad type");
1064 return range_check (result
, "ASIN");
1069 gfc_simplify_asinh (gfc_expr
*x
)
1073 if (x
->expr_type
!= EXPR_CONSTANT
)
1076 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1081 mpfr_asinh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1085 mpc_asinh (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1089 gfc_internal_error ("in gfc_simplify_asinh(): Bad type");
1092 return range_check (result
, "ASINH");
1097 gfc_simplify_atan (gfc_expr
*x
)
1101 if (x
->expr_type
!= EXPR_CONSTANT
)
1104 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1109 mpfr_atan (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1113 mpc_atan (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1117 gfc_internal_error ("in gfc_simplify_atan(): Bad type");
1120 return range_check (result
, "ATAN");
1125 gfc_simplify_atanh (gfc_expr
*x
)
1129 if (x
->expr_type
!= EXPR_CONSTANT
)
1135 if (mpfr_cmp_si (x
->value
.real
, 1) >= 0
1136 || mpfr_cmp_si (x
->value
.real
, -1) <= 0)
1138 gfc_error ("Argument of ATANH at %L must be inside the range -1 "
1140 return &gfc_bad_expr
;
1142 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1143 mpfr_atanh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1147 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1148 mpc_atanh (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1152 gfc_internal_error ("in gfc_simplify_atanh(): Bad type");
1155 return range_check (result
, "ATANH");
1160 gfc_simplify_atan2 (gfc_expr
*y
, gfc_expr
*x
)
1164 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1167 if (mpfr_sgn (y
->value
.real
) == 0 && mpfr_sgn (x
->value
.real
) == 0)
1169 gfc_error ("If first argument of ATAN2 %L is zero, then the "
1170 "second argument must not be zero", &x
->where
);
1171 return &gfc_bad_expr
;
1174 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1175 mpfr_atan2 (result
->value
.real
, y
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1177 return range_check (result
, "ATAN2");
1182 gfc_simplify_bessel_j0 (gfc_expr
*x
)
1186 if (x
->expr_type
!= EXPR_CONSTANT
)
1189 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1190 mpfr_j0 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1192 return range_check (result
, "BESSEL_J0");
1197 gfc_simplify_bessel_j1 (gfc_expr
*x
)
1201 if (x
->expr_type
!= EXPR_CONSTANT
)
1204 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1205 mpfr_j1 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1207 return range_check (result
, "BESSEL_J1");
1212 gfc_simplify_bessel_jn (gfc_expr
*order
, gfc_expr
*x
)
1217 if (x
->expr_type
!= EXPR_CONSTANT
|| order
->expr_type
!= EXPR_CONSTANT
)
1220 n
= mpz_get_si (order
->value
.integer
);
1221 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1222 mpfr_jn (result
->value
.real
, n
, x
->value
.real
, GFC_RND_MODE
);
1224 return range_check (result
, "BESSEL_JN");
1228 /* Simplify transformational form of JN and YN. */
1231 gfc_simplify_bessel_n2 (gfc_expr
*order1
, gfc_expr
*order2
, gfc_expr
*x
,
1238 mpfr_t x2rev
, last1
, last2
;
1240 if (x
->expr_type
!= EXPR_CONSTANT
|| order1
->expr_type
!= EXPR_CONSTANT
1241 || order2
->expr_type
!= EXPR_CONSTANT
)
1244 n1
= mpz_get_si (order1
->value
.integer
);
1245 n2
= mpz_get_si (order2
->value
.integer
);
1246 result
= gfc_get_array_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1248 result
->shape
= gfc_get_shape (1);
1249 mpz_init_set_ui (result
->shape
[0], MAX (n2
-n1
+1, 0));
1254 /* Special case: x == 0; it is J0(0.0) == 1, JN(N > 0, 0.0) == 0; and
1255 YN(N, 0.0) = -Inf. */
1257 if (mpfr_cmp_ui (x
->value
.real
, 0.0) == 0)
1259 if (!jn
&& gfc_option
.flag_range_check
)
1261 gfc_error ("Result of BESSEL_YN is -INF at %L", &result
->where
);
1262 gfc_free_expr (result
);
1263 return &gfc_bad_expr
;
1268 e
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1269 mpfr_set_ui (e
->value
.real
, 1, GFC_RND_MODE
);
1270 gfc_constructor_append_expr (&result
->value
.constructor
, e
,
1275 for (i
= n1
; i
<= n2
; i
++)
1277 e
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1279 mpfr_set_ui (e
->value
.real
, 0, GFC_RND_MODE
);
1281 mpfr_set_inf (e
->value
.real
, -1);
1282 gfc_constructor_append_expr (&result
->value
.constructor
, e
,
1289 /* Use the faster but more verbose recurrence algorithm. Bessel functions
1290 are stable for downward recursion and Neumann functions are stable
1291 for upward recursion. It is
1293 J(N-1, x) = x2rev * N * J(N, x) - J(N+1, x),
1294 Y(N+1, x) = x2rev * N * Y(N, x) - Y(N-1, x).
1295 Cf. http://dlmf.nist.gov/10.74#iv and http://dlmf.nist.gov/10.6#E1 */
1297 gfc_set_model_kind (x
->ts
.kind
);
1299 /* Get first recursion anchor. */
1303 mpfr_jn (last1
, n2
, x
->value
.real
, GFC_RND_MODE
);
1305 mpfr_yn (last1
, n1
, x
->value
.real
, GFC_RND_MODE
);
1307 e
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1308 mpfr_set (e
->value
.real
, last1
, GFC_RND_MODE
);
1309 if (range_check (e
, jn
? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr
)
1313 gfc_free_expr (result
);
1314 return &gfc_bad_expr
;
1316 gfc_constructor_append_expr (&result
->value
.constructor
, e
, &x
->where
);
1324 /* Get second recursion anchor. */
1328 mpfr_jn (last2
, n2
-1, x
->value
.real
, GFC_RND_MODE
);
1330 mpfr_yn (last2
, n1
+1, x
->value
.real
, GFC_RND_MODE
);
1332 e
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1333 mpfr_set (e
->value
.real
, last2
, GFC_RND_MODE
);
1334 if (range_check (e
, jn
? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr
)
1339 gfc_free_expr (result
);
1340 return &gfc_bad_expr
;
1343 gfc_constructor_insert_expr (&result
->value
.constructor
, e
, &x
->where
, -2);
1345 gfc_constructor_append_expr (&result
->value
.constructor
, e
, &x
->where
);
1354 /* Start actual recursion. */
1357 mpfr_ui_div (x2rev
, 2, x
->value
.real
, GFC_RND_MODE
);
1359 for (i
= 2; i
<= n2
-n1
; i
++)
1361 e
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1363 /* Special case: For YN, if the previous N gave -INF, set
1364 also N+1 to -INF. */
1365 if (!jn
&& !gfc_option
.flag_range_check
&& mpfr_inf_p (last2
))
1367 mpfr_set_inf (e
->value
.real
, -1);
1368 gfc_constructor_append_expr (&result
->value
.constructor
, e
,
1373 mpfr_mul_si (e
->value
.real
, x2rev
, jn
? (n2
-i
+1) : (n1
+i
-1),
1375 mpfr_mul (e
->value
.real
, e
->value
.real
, last2
, GFC_RND_MODE
);
1376 mpfr_sub (e
->value
.real
, e
->value
.real
, last1
, GFC_RND_MODE
);
1378 if (range_check (e
, jn
? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr
)
1380 /* Range_check frees "e" in that case. */
1386 gfc_constructor_insert_expr (&result
->value
.constructor
, e
, &x
->where
,
1389 gfc_constructor_append_expr (&result
->value
.constructor
, e
, &x
->where
);
1391 mpfr_set (last1
, last2
, GFC_RND_MODE
);
1392 mpfr_set (last2
, e
->value
.real
, GFC_RND_MODE
);
1405 gfc_free_expr (result
);
1406 return &gfc_bad_expr
;
1411 gfc_simplify_bessel_jn2 (gfc_expr
*order1
, gfc_expr
*order2
, gfc_expr
*x
)
1413 return gfc_simplify_bessel_n2 (order1
, order2
, x
, true);
1418 gfc_simplify_bessel_y0 (gfc_expr
*x
)
1422 if (x
->expr_type
!= EXPR_CONSTANT
)
1425 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1426 mpfr_y0 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1428 return range_check (result
, "BESSEL_Y0");
1433 gfc_simplify_bessel_y1 (gfc_expr
*x
)
1437 if (x
->expr_type
!= EXPR_CONSTANT
)
1440 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1441 mpfr_y1 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1443 return range_check (result
, "BESSEL_Y1");
1448 gfc_simplify_bessel_yn (gfc_expr
*order
, gfc_expr
*x
)
1453 if (x
->expr_type
!= EXPR_CONSTANT
|| order
->expr_type
!= EXPR_CONSTANT
)
1456 n
= mpz_get_si (order
->value
.integer
);
1457 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1458 mpfr_yn (result
->value
.real
, n
, x
->value
.real
, GFC_RND_MODE
);
1460 return range_check (result
, "BESSEL_YN");
1465 gfc_simplify_bessel_yn2 (gfc_expr
*order1
, gfc_expr
*order2
, gfc_expr
*x
)
1467 return gfc_simplify_bessel_n2 (order1
, order2
, x
, false);
1472 gfc_simplify_bit_size (gfc_expr
*e
)
1474 int i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
1475 return gfc_get_int_expr (e
->ts
.kind
, &e
->where
,
1476 gfc_integer_kinds
[i
].bit_size
);
1481 gfc_simplify_btest (gfc_expr
*e
, gfc_expr
*bit
)
1485 if (e
->expr_type
!= EXPR_CONSTANT
|| bit
->expr_type
!= EXPR_CONSTANT
)
1488 if (gfc_extract_int (bit
, &b
) != NULL
|| b
< 0)
1489 return gfc_get_logical_expr (gfc_default_logical_kind
, &e
->where
, false);
1491 return gfc_get_logical_expr (gfc_default_logical_kind
, &e
->where
,
1492 mpz_tstbit (e
->value
.integer
, b
));
1497 compare_bitwise (gfc_expr
*i
, gfc_expr
*j
)
1502 gcc_assert (i
->ts
.type
== BT_INTEGER
);
1503 gcc_assert (j
->ts
.type
== BT_INTEGER
);
1505 mpz_init_set (x
, i
->value
.integer
);
1506 k
= gfc_validate_kind (i
->ts
.type
, i
->ts
.kind
, false);
1507 convert_mpz_to_unsigned (x
, gfc_integer_kinds
[k
].bit_size
);
1509 mpz_init_set (y
, j
->value
.integer
);
1510 k
= gfc_validate_kind (j
->ts
.type
, j
->ts
.kind
, false);
1511 convert_mpz_to_unsigned (y
, gfc_integer_kinds
[k
].bit_size
);
1513 res
= mpz_cmp (x
, y
);
1521 gfc_simplify_bge (gfc_expr
*i
, gfc_expr
*j
)
1523 if (i
->expr_type
!= EXPR_CONSTANT
|| j
->expr_type
!= EXPR_CONSTANT
)
1526 return gfc_get_logical_expr (gfc_default_logical_kind
, &i
->where
,
1527 compare_bitwise (i
, j
) >= 0);
1532 gfc_simplify_bgt (gfc_expr
*i
, gfc_expr
*j
)
1534 if (i
->expr_type
!= EXPR_CONSTANT
|| j
->expr_type
!= EXPR_CONSTANT
)
1537 return gfc_get_logical_expr (gfc_default_logical_kind
, &i
->where
,
1538 compare_bitwise (i
, j
) > 0);
1543 gfc_simplify_ble (gfc_expr
*i
, gfc_expr
*j
)
1545 if (i
->expr_type
!= EXPR_CONSTANT
|| j
->expr_type
!= EXPR_CONSTANT
)
1548 return gfc_get_logical_expr (gfc_default_logical_kind
, &i
->where
,
1549 compare_bitwise (i
, j
) <= 0);
1554 gfc_simplify_blt (gfc_expr
*i
, gfc_expr
*j
)
1556 if (i
->expr_type
!= EXPR_CONSTANT
|| j
->expr_type
!= EXPR_CONSTANT
)
1559 return gfc_get_logical_expr (gfc_default_logical_kind
, &i
->where
,
1560 compare_bitwise (i
, j
) < 0);
1565 gfc_simplify_ceiling (gfc_expr
*e
, gfc_expr
*k
)
1567 gfc_expr
*ceil
, *result
;
1570 kind
= get_kind (BT_INTEGER
, k
, "CEILING", gfc_default_integer_kind
);
1572 return &gfc_bad_expr
;
1574 if (e
->expr_type
!= EXPR_CONSTANT
)
1577 ceil
= gfc_copy_expr (e
);
1578 mpfr_ceil (ceil
->value
.real
, e
->value
.real
);
1580 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &e
->where
);
1581 gfc_mpfr_to_mpz (result
->value
.integer
, ceil
->value
.real
, &e
->where
);
1583 gfc_free_expr (ceil
);
1585 return range_check (result
, "CEILING");
1590 gfc_simplify_char (gfc_expr
*e
, gfc_expr
*k
)
1592 return simplify_achar_char (e
, k
, "CHAR", false);
1596 /* Common subroutine for simplifying CMPLX, COMPLEX and DCMPLX. */
1599 simplify_cmplx (const char *name
, gfc_expr
*x
, gfc_expr
*y
, int kind
)
1603 if (convert_boz (x
, kind
) == &gfc_bad_expr
)
1604 return &gfc_bad_expr
;
1606 if (convert_boz (y
, kind
) == &gfc_bad_expr
)
1607 return &gfc_bad_expr
;
1609 if (x
->expr_type
!= EXPR_CONSTANT
1610 || (y
!= NULL
&& y
->expr_type
!= EXPR_CONSTANT
))
1613 result
= gfc_get_constant_expr (BT_COMPLEX
, kind
, &x
->where
);
1618 mpc_set_z (result
->value
.complex, x
->value
.integer
, GFC_MPC_RND_MODE
);
1622 mpc_set_fr (result
->value
.complex, x
->value
.real
, GFC_RND_MODE
);
1626 mpc_set (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1630 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (x)");
1634 return range_check (result
, name
);
1639 mpfr_set_z (mpc_imagref (result
->value
.complex),
1640 y
->value
.integer
, GFC_RND_MODE
);
1644 mpfr_set (mpc_imagref (result
->value
.complex),
1645 y
->value
.real
, GFC_RND_MODE
);
1649 gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (y)");
1652 return range_check (result
, name
);
1657 gfc_simplify_cmplx (gfc_expr
*x
, gfc_expr
*y
, gfc_expr
*k
)
1661 kind
= get_kind (BT_REAL
, k
, "CMPLX", gfc_default_complex_kind
);
1663 return &gfc_bad_expr
;
1665 return simplify_cmplx ("CMPLX", x
, y
, kind
);
1670 gfc_simplify_complex (gfc_expr
*x
, gfc_expr
*y
)
1674 if (x
->ts
.type
== BT_INTEGER
&& y
->ts
.type
== BT_INTEGER
)
1675 kind
= gfc_default_complex_kind
;
1676 else if (x
->ts
.type
== BT_REAL
|| y
->ts
.type
== BT_INTEGER
)
1678 else if (x
->ts
.type
== BT_INTEGER
|| y
->ts
.type
== BT_REAL
)
1680 else if (x
->ts
.type
== BT_REAL
&& y
->ts
.type
== BT_REAL
)
1681 kind
= (x
->ts
.kind
> y
->ts
.kind
) ? x
->ts
.kind
: y
->ts
.kind
;
1685 return simplify_cmplx ("COMPLEX", x
, y
, kind
);
1690 gfc_simplify_conjg (gfc_expr
*e
)
1694 if (e
->expr_type
!= EXPR_CONSTANT
)
1697 result
= gfc_copy_expr (e
);
1698 mpc_conj (result
->value
.complex, result
->value
.complex, GFC_MPC_RND_MODE
);
1700 return range_check (result
, "CONJG");
1705 gfc_simplify_cos (gfc_expr
*x
)
1709 if (x
->expr_type
!= EXPR_CONSTANT
)
1712 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1717 mpfr_cos (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1721 gfc_set_model_kind (x
->ts
.kind
);
1722 mpc_cos (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1726 gfc_internal_error ("in gfc_simplify_cos(): Bad type");
1729 return range_check (result
, "COS");
1734 gfc_simplify_cosh (gfc_expr
*x
)
1738 if (x
->expr_type
!= EXPR_CONSTANT
)
1741 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1746 mpfr_cosh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1750 mpc_cosh (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
1757 return range_check (result
, "COSH");
1762 gfc_simplify_count (gfc_expr
*mask
, gfc_expr
*dim
, gfc_expr
*kind
)
1766 if (!is_constant_array_expr (mask
)
1767 || !gfc_is_constant_expr (dim
)
1768 || !gfc_is_constant_expr (kind
))
1771 result
= transformational_result (mask
, dim
,
1773 get_kind (BT_INTEGER
, kind
, "COUNT",
1774 gfc_default_integer_kind
),
1777 init_result_expr (result
, 0, NULL
);
1779 /* Passing MASK twice, once as data array, once as mask.
1780 Whenever gfc_count is called, '1' is added to the result. */
1781 return !dim
|| mask
->rank
== 1 ?
1782 simplify_transformation_to_scalar (result
, mask
, mask
, gfc_count
) :
1783 simplify_transformation_to_array (result
, mask
, dim
, mask
, gfc_count
, NULL
);
1788 gfc_simplify_dcmplx (gfc_expr
*x
, gfc_expr
*y
)
1790 return simplify_cmplx ("DCMPLX", x
, y
, gfc_default_double_kind
);
1795 gfc_simplify_dble (gfc_expr
*e
)
1797 gfc_expr
*result
= NULL
;
1799 if (e
->expr_type
!= EXPR_CONSTANT
)
1802 if (convert_boz (e
, gfc_default_double_kind
) == &gfc_bad_expr
)
1803 return &gfc_bad_expr
;
1805 result
= gfc_convert_constant (e
, BT_REAL
, gfc_default_double_kind
);
1806 if (result
== &gfc_bad_expr
)
1807 return &gfc_bad_expr
;
1809 return range_check (result
, "DBLE");
1814 gfc_simplify_digits (gfc_expr
*x
)
1818 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
1823 digits
= gfc_integer_kinds
[i
].digits
;
1828 digits
= gfc_real_kinds
[i
].digits
;
1835 return gfc_get_int_expr (gfc_default_integer_kind
, NULL
, digits
);
1840 gfc_simplify_dim (gfc_expr
*x
, gfc_expr
*y
)
1845 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1848 kind
= x
->ts
.kind
> y
->ts
.kind
? x
->ts
.kind
: y
->ts
.kind
;
1849 result
= gfc_get_constant_expr (x
->ts
.type
, kind
, &x
->where
);
1854 if (mpz_cmp (x
->value
.integer
, y
->value
.integer
) > 0)
1855 mpz_sub (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
1857 mpz_set_ui (result
->value
.integer
, 0);
1862 if (mpfr_cmp (x
->value
.real
, y
->value
.real
) > 0)
1863 mpfr_sub (result
->value
.real
, x
->value
.real
, y
->value
.real
,
1866 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
1871 gfc_internal_error ("gfc_simplify_dim(): Bad type");
1874 return range_check (result
, "DIM");
1879 gfc_simplify_dot_product (gfc_expr
*vector_a
, gfc_expr
*vector_b
)
1881 if (!is_constant_array_expr (vector_a
)
1882 || !is_constant_array_expr (vector_b
))
1885 gcc_assert (vector_a
->rank
== 1);
1886 gcc_assert (vector_b
->rank
== 1);
1887 gcc_assert (gfc_compare_types (&vector_a
->ts
, &vector_b
->ts
));
1889 return compute_dot_product (vector_a
, 1, 0, vector_b
, 1, 0, true);
1894 gfc_simplify_dprod (gfc_expr
*x
, gfc_expr
*y
)
1896 gfc_expr
*a1
, *a2
, *result
;
1898 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
1901 a1
= gfc_real2real (x
, gfc_default_double_kind
);
1902 a2
= gfc_real2real (y
, gfc_default_double_kind
);
1904 result
= gfc_get_constant_expr (BT_REAL
, gfc_default_double_kind
, &x
->where
);
1905 mpfr_mul (result
->value
.real
, a1
->value
.real
, a2
->value
.real
, GFC_RND_MODE
);
1910 return range_check (result
, "DPROD");
1915 simplify_dshift (gfc_expr
*arg1
, gfc_expr
*arg2
, gfc_expr
*shiftarg
,
1919 int i
, k
, size
, shift
;
1921 if (arg1
->expr_type
!= EXPR_CONSTANT
|| arg2
->expr_type
!= EXPR_CONSTANT
1922 || shiftarg
->expr_type
!= EXPR_CONSTANT
)
1925 k
= gfc_validate_kind (BT_INTEGER
, arg1
->ts
.kind
, false);
1926 size
= gfc_integer_kinds
[k
].bit_size
;
1928 gfc_extract_int (shiftarg
, &shift
);
1930 /* DSHIFTR(I,J,SHIFT) = DSHIFTL(I,J,SIZE-SHIFT). */
1932 shift
= size
- shift
;
1934 result
= gfc_get_constant_expr (BT_INTEGER
, arg1
->ts
.kind
, &arg1
->where
);
1935 mpz_set_ui (result
->value
.integer
, 0);
1937 for (i
= 0; i
< shift
; i
++)
1938 if (mpz_tstbit (arg2
->value
.integer
, size
- shift
+ i
))
1939 mpz_setbit (result
->value
.integer
, i
);
1941 for (i
= 0; i
< size
- shift
; i
++)
1942 if (mpz_tstbit (arg1
->value
.integer
, i
))
1943 mpz_setbit (result
->value
.integer
, shift
+ i
);
1945 /* Convert to a signed value. */
1946 convert_mpz_to_signed (result
->value
.integer
, size
);
1953 gfc_simplify_dshiftr (gfc_expr
*arg1
, gfc_expr
*arg2
, gfc_expr
*shiftarg
)
1955 return simplify_dshift (arg1
, arg2
, shiftarg
, true);
1960 gfc_simplify_dshiftl (gfc_expr
*arg1
, gfc_expr
*arg2
, gfc_expr
*shiftarg
)
1962 return simplify_dshift (arg1
, arg2
, shiftarg
, false);
1967 gfc_simplify_erf (gfc_expr
*x
)
1971 if (x
->expr_type
!= EXPR_CONSTANT
)
1974 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1975 mpfr_erf (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1977 return range_check (result
, "ERF");
1982 gfc_simplify_erfc (gfc_expr
*x
)
1986 if (x
->expr_type
!= EXPR_CONSTANT
)
1989 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
1990 mpfr_erfc (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
1992 return range_check (result
, "ERFC");
1996 /* Helper functions to simplify ERFC_SCALED(x) = ERFC(x) * EXP(X**2). */
1998 #define MAX_ITER 200
1999 #define ARG_LIMIT 12
2001 /* Calculate ERFC_SCALED directly by its definition:
2003 ERFC_SCALED(x) = ERFC(x) * EXP(X**2)
2005 using a large precision for intermediate results. This is used for all
2006 but large values of the argument. */
2008 fullprec_erfc_scaled (mpfr_t res
, mpfr_t arg
)
2013 prec
= mpfr_get_default_prec ();
2014 mpfr_set_default_prec (10 * prec
);
2019 mpfr_set (a
, arg
, GFC_RND_MODE
);
2020 mpfr_sqr (b
, a
, GFC_RND_MODE
);
2021 mpfr_exp (b
, b
, GFC_RND_MODE
);
2022 mpfr_erfc (a
, a
, GFC_RND_MODE
);
2023 mpfr_mul (a
, a
, b
, GFC_RND_MODE
);
2025 mpfr_set (res
, a
, GFC_RND_MODE
);
2026 mpfr_set_default_prec (prec
);
2032 /* Calculate ERFC_SCALED using a power series expansion in 1/arg:
2034 ERFC_SCALED(x) = 1 / (x * sqrt(pi))
2035 * (1 + Sum_n (-1)**n * (1 * 3 * 5 * ... * (2n-1))
2038 This is used for large values of the argument. Intermediate calculations
2039 are performed with twice the precision. We don't do a fixed number of
2040 iterations of the sum, but stop when it has converged to the required
2043 asympt_erfc_scaled (mpfr_t res
, mpfr_t arg
)
2045 mpfr_t sum
, x
, u
, v
, w
, oldsum
, sumtrunc
;
2050 prec
= mpfr_get_default_prec ();
2051 mpfr_set_default_prec (2 * prec
);
2061 mpfr_init (sumtrunc
);
2062 mpfr_set_prec (oldsum
, prec
);
2063 mpfr_set_prec (sumtrunc
, prec
);
2065 mpfr_set (x
, arg
, GFC_RND_MODE
);
2066 mpfr_set_ui (sum
, 1, GFC_RND_MODE
);
2067 mpz_set_ui (num
, 1);
2069 mpfr_set (u
, x
, GFC_RND_MODE
);
2070 mpfr_sqr (u
, u
, GFC_RND_MODE
);
2071 mpfr_mul_ui (u
, u
, 2, GFC_RND_MODE
);
2072 mpfr_pow_si (u
, u
, -1, GFC_RND_MODE
);
2074 for (i
= 1; i
< MAX_ITER
; i
++)
2076 mpfr_set (oldsum
, sum
, GFC_RND_MODE
);
2078 mpz_mul_ui (num
, num
, 2 * i
- 1);
2081 mpfr_set (w
, u
, GFC_RND_MODE
);
2082 mpfr_pow_ui (w
, w
, i
, GFC_RND_MODE
);
2084 mpfr_set_z (v
, num
, GFC_RND_MODE
);
2085 mpfr_mul (v
, v
, w
, GFC_RND_MODE
);
2087 mpfr_add (sum
, sum
, v
, GFC_RND_MODE
);
2089 mpfr_set (sumtrunc
, sum
, GFC_RND_MODE
);
2090 if (mpfr_cmp (sumtrunc
, oldsum
) == 0)
2094 /* We should have converged by now; otherwise, ARG_LIMIT is probably
2096 gcc_assert (i
< MAX_ITER
);
2098 /* Divide by x * sqrt(Pi). */
2099 mpfr_const_pi (u
, GFC_RND_MODE
);
2100 mpfr_sqrt (u
, u
, GFC_RND_MODE
);
2101 mpfr_mul (u
, u
, x
, GFC_RND_MODE
);
2102 mpfr_div (sum
, sum
, u
, GFC_RND_MODE
);
2104 mpfr_set (res
, sum
, GFC_RND_MODE
);
2105 mpfr_set_default_prec (prec
);
2107 mpfr_clears (sum
, x
, u
, v
, w
, oldsum
, sumtrunc
, NULL
);
2113 gfc_simplify_erfc_scaled (gfc_expr
*x
)
2117 if (x
->expr_type
!= EXPR_CONSTANT
)
2120 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2121 if (mpfr_cmp_d (x
->value
.real
, ARG_LIMIT
) >= 0)
2122 asympt_erfc_scaled (result
->value
.real
, x
->value
.real
);
2124 fullprec_erfc_scaled (result
->value
.real
, x
->value
.real
);
2126 return range_check (result
, "ERFC_SCALED");
2134 gfc_simplify_epsilon (gfc_expr
*e
)
2139 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
2141 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
2142 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].epsilon
, GFC_RND_MODE
);
2144 return range_check (result
, "EPSILON");
2149 gfc_simplify_exp (gfc_expr
*x
)
2153 if (x
->expr_type
!= EXPR_CONSTANT
)
2156 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2161 mpfr_exp (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
2165 gfc_set_model_kind (x
->ts
.kind
);
2166 mpc_exp (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
2170 gfc_internal_error ("in gfc_simplify_exp(): Bad type");
2173 return range_check (result
, "EXP");
2178 gfc_simplify_exponent (gfc_expr
*x
)
2183 if (x
->expr_type
!= EXPR_CONSTANT
)
2186 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
2189 gfc_set_model (x
->value
.real
);
2191 if (mpfr_sgn (x
->value
.real
) == 0)
2193 mpz_set_ui (result
->value
.integer
, 0);
2197 i
= (int) mpfr_get_exp (x
->value
.real
);
2198 mpz_set_si (result
->value
.integer
, i
);
2200 return range_check (result
, "EXPONENT");
2205 gfc_simplify_float (gfc_expr
*a
)
2209 if (a
->expr_type
!= EXPR_CONSTANT
)
2214 if (convert_boz (a
, gfc_default_real_kind
) == &gfc_bad_expr
)
2215 return &gfc_bad_expr
;
2217 result
= gfc_copy_expr (a
);
2220 result
= gfc_int2real (a
, gfc_default_real_kind
);
2222 return range_check (result
, "FLOAT");
2227 is_last_ref_vtab (gfc_expr
*e
)
2230 gfc_component
*comp
= NULL
;
2232 if (e
->expr_type
!= EXPR_VARIABLE
)
2235 for (ref
= e
->ref
; ref
; ref
= ref
->next
)
2236 if (ref
->type
== REF_COMPONENT
)
2237 comp
= ref
->u
.c
.component
;
2239 if (!e
->ref
|| !comp
)
2240 return e
->symtree
->n
.sym
->attr
.vtab
;
2242 if (comp
->name
[0] == '_' && strcmp (comp
->name
, "_vptr") == 0)
2250 gfc_simplify_extends_type_of (gfc_expr
*a
, gfc_expr
*mold
)
2252 /* Avoid simplification of resolved symbols. */
2253 if (is_last_ref_vtab (a
) || is_last_ref_vtab (mold
))
2256 if (a
->ts
.type
== BT_DERIVED
&& mold
->ts
.type
== BT_DERIVED
)
2257 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
,
2258 gfc_type_is_extension_of (mold
->ts
.u
.derived
,
2261 if (UNLIMITED_POLY (a
) || UNLIMITED_POLY (mold
))
2264 /* Return .false. if the dynamic type can never be the same. */
2265 if ((a
->ts
.type
== BT_CLASS
&& mold
->ts
.type
== BT_CLASS
2266 && !gfc_type_is_extension_of
2267 (mold
->ts
.u
.derived
->components
->ts
.u
.derived
,
2268 a
->ts
.u
.derived
->components
->ts
.u
.derived
)
2269 && !gfc_type_is_extension_of
2270 (a
->ts
.u
.derived
->components
->ts
.u
.derived
,
2271 mold
->ts
.u
.derived
->components
->ts
.u
.derived
))
2272 || (a
->ts
.type
== BT_DERIVED
&& mold
->ts
.type
== BT_CLASS
2273 && !gfc_type_is_extension_of
2275 mold
->ts
.u
.derived
->components
->ts
.u
.derived
)
2276 && !gfc_type_is_extension_of
2277 (mold
->ts
.u
.derived
->components
->ts
.u
.derived
,
2279 || (a
->ts
.type
== BT_CLASS
&& mold
->ts
.type
== BT_DERIVED
2280 && !gfc_type_is_extension_of
2281 (mold
->ts
.u
.derived
,
2282 a
->ts
.u
.derived
->components
->ts
.u
.derived
)))
2283 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
, false);
2285 if (mold
->ts
.type
== BT_DERIVED
2286 && gfc_type_is_extension_of (mold
->ts
.u
.derived
,
2287 a
->ts
.u
.derived
->components
->ts
.u
.derived
))
2288 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
, true);
2295 gfc_simplify_same_type_as (gfc_expr
*a
, gfc_expr
*b
)
2297 /* Avoid simplification of resolved symbols. */
2298 if (is_last_ref_vtab (a
) || is_last_ref_vtab (b
))
2301 /* Return .false. if the dynamic type can never be the
2303 if (((a
->ts
.type
== BT_CLASS
&& gfc_expr_attr (a
).class_ok
)
2304 || (b
->ts
.type
== BT_CLASS
&& gfc_expr_attr (b
).class_ok
))
2305 && !gfc_type_compatible (&a
->ts
, &b
->ts
)
2306 && !gfc_type_compatible (&b
->ts
, &a
->ts
))
2307 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
, false);
2309 if (a
->ts
.type
!= BT_DERIVED
|| b
->ts
.type
!= BT_DERIVED
)
2312 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
,
2313 gfc_compare_derived_types (a
->ts
.u
.derived
,
2319 gfc_simplify_floor (gfc_expr
*e
, gfc_expr
*k
)
2325 kind
= get_kind (BT_INTEGER
, k
, "FLOOR", gfc_default_integer_kind
);
2327 gfc_internal_error ("gfc_simplify_floor(): Bad kind");
2329 if (e
->expr_type
!= EXPR_CONSTANT
)
2332 gfc_set_model_kind (kind
);
2335 mpfr_floor (floor
, e
->value
.real
);
2337 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &e
->where
);
2338 gfc_mpfr_to_mpz (result
->value
.integer
, floor
, &e
->where
);
2342 return range_check (result
, "FLOOR");
2347 gfc_simplify_fraction (gfc_expr
*x
)
2351 #if MPFR_VERSION < MPFR_VERSION_NUM(3,1,0)
2352 mpfr_t absv
, exp
, pow2
;
2357 if (x
->expr_type
!= EXPR_CONSTANT
)
2360 result
= gfc_get_constant_expr (BT_REAL
, x
->ts
.kind
, &x
->where
);
2362 #if MPFR_VERSION < MPFR_VERSION_NUM(3,1,0)
2364 /* MPFR versions before 3.1.0 do not include mpfr_frexp.
2365 TODO: remove the kludge when MPFR 3.1.0 or newer will be required */
2367 if (mpfr_sgn (x
->value
.real
) == 0)
2369 mpfr_set (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
2373 gfc_set_model_kind (x
->ts
.kind
);
2378 mpfr_abs (absv
, x
->value
.real
, GFC_RND_MODE
);
2379 mpfr_log2 (exp
, absv
, GFC_RND_MODE
);
2381 mpfr_trunc (exp
, exp
);
2382 mpfr_add_ui (exp
, exp
, 1, GFC_RND_MODE
);
2384 mpfr_ui_pow (pow2
, 2, exp
, GFC_RND_MODE
);
2386 mpfr_div (result
->value
.real
, x
->value
.real
, pow2
, GFC_RND_MODE
);
2388 mpfr_clears (exp
, absv
, pow2
, NULL
);
2392 mpfr_frexp (&e
, result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
2396 return range_check (result
, "FRACTION");
2401 gfc_simplify_gamma (gfc_expr
*x
)
2405 if (x
->expr_type
!= EXPR_CONSTANT
)
2408 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2409 mpfr_gamma (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
2411 return range_check (result
, "GAMMA");
2416 gfc_simplify_huge (gfc_expr
*e
)
2421 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
2422 result
= gfc_get_constant_expr (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
2427 mpz_set (result
->value
.integer
, gfc_integer_kinds
[i
].huge
);
2431 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].huge
, GFC_RND_MODE
);
2443 gfc_simplify_hypot (gfc_expr
*x
, gfc_expr
*y
)
2447 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
2450 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2451 mpfr_hypot (result
->value
.real
, x
->value
.real
, y
->value
.real
, GFC_RND_MODE
);
2452 return range_check (result
, "HYPOT");
2456 /* We use the processor's collating sequence, because all
2457 systems that gfortran currently works on are ASCII. */
2460 gfc_simplify_iachar (gfc_expr
*e
, gfc_expr
*kind
)
2466 if (e
->expr_type
!= EXPR_CONSTANT
)
2469 if (e
->value
.character
.length
!= 1)
2471 gfc_error ("Argument of IACHAR at %L must be of length one", &e
->where
);
2472 return &gfc_bad_expr
;
2475 index
= e
->value
.character
.string
[0];
2477 if (gfc_option
.warn_surprising
&& index
> 127)
2478 gfc_warning ("Argument of IACHAR function at %L outside of range 0..127",
2481 k
= get_kind (BT_INTEGER
, kind
, "IACHAR", gfc_default_integer_kind
);
2483 return &gfc_bad_expr
;
2485 result
= gfc_get_int_expr (k
, &e
->where
, index
);
2487 return range_check (result
, "IACHAR");
2492 do_bit_and (gfc_expr
*result
, gfc_expr
*e
)
2494 gcc_assert (e
->ts
.type
== BT_INTEGER
&& e
->expr_type
== EXPR_CONSTANT
);
2495 gcc_assert (result
->ts
.type
== BT_INTEGER
2496 && result
->expr_type
== EXPR_CONSTANT
);
2498 mpz_and (result
->value
.integer
, result
->value
.integer
, e
->value
.integer
);
2504 gfc_simplify_iall (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
)
2506 return simplify_transformation (array
, dim
, mask
, -1, do_bit_and
);
2511 do_bit_ior (gfc_expr
*result
, gfc_expr
*e
)
2513 gcc_assert (e
->ts
.type
== BT_INTEGER
&& e
->expr_type
== EXPR_CONSTANT
);
2514 gcc_assert (result
->ts
.type
== BT_INTEGER
2515 && result
->expr_type
== EXPR_CONSTANT
);
2517 mpz_ior (result
->value
.integer
, result
->value
.integer
, e
->value
.integer
);
2523 gfc_simplify_iany (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
)
2525 return simplify_transformation (array
, dim
, mask
, 0, do_bit_ior
);
2530 gfc_simplify_iand (gfc_expr
*x
, gfc_expr
*y
)
2534 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
2537 result
= gfc_get_constant_expr (BT_INTEGER
, x
->ts
.kind
, &x
->where
);
2538 mpz_and (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
2540 return range_check (result
, "IAND");
2545 gfc_simplify_ibclr (gfc_expr
*x
, gfc_expr
*y
)
2550 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
2553 gfc_extract_int (y
, &pos
);
2555 k
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
2557 result
= gfc_copy_expr (x
);
2559 convert_mpz_to_unsigned (result
->value
.integer
,
2560 gfc_integer_kinds
[k
].bit_size
);
2562 mpz_clrbit (result
->value
.integer
, pos
);
2564 convert_mpz_to_signed (result
->value
.integer
,
2565 gfc_integer_kinds
[k
].bit_size
);
2572 gfc_simplify_ibits (gfc_expr
*x
, gfc_expr
*y
, gfc_expr
*z
)
2579 if (x
->expr_type
!= EXPR_CONSTANT
2580 || y
->expr_type
!= EXPR_CONSTANT
2581 || z
->expr_type
!= EXPR_CONSTANT
)
2584 gfc_extract_int (y
, &pos
);
2585 gfc_extract_int (z
, &len
);
2587 k
= gfc_validate_kind (BT_INTEGER
, x
->ts
.kind
, false);
2589 bitsize
= gfc_integer_kinds
[k
].bit_size
;
2591 if (pos
+ len
> bitsize
)
2593 gfc_error ("Sum of second and third arguments of IBITS exceeds "
2594 "bit size at %L", &y
->where
);
2595 return &gfc_bad_expr
;
2598 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
2599 convert_mpz_to_unsigned (result
->value
.integer
,
2600 gfc_integer_kinds
[k
].bit_size
);
2602 bits
= XCNEWVEC (int, bitsize
);
2604 for (i
= 0; i
< bitsize
; i
++)
2607 for (i
= 0; i
< len
; i
++)
2608 bits
[i
] = mpz_tstbit (x
->value
.integer
, i
+ pos
);
2610 for (i
= 0; i
< bitsize
; i
++)
2613 mpz_clrbit (result
->value
.integer
, i
);
2614 else if (bits
[i
] == 1)
2615 mpz_setbit (result
->value
.integer
, i
);
2617 gfc_internal_error ("IBITS: Bad bit");
2622 convert_mpz_to_signed (result
->value
.integer
,
2623 gfc_integer_kinds
[k
].bit_size
);
2630 gfc_simplify_ibset (gfc_expr
*x
, gfc_expr
*y
)
2635 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
2638 gfc_extract_int (y
, &pos
);
2640 k
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
2642 result
= gfc_copy_expr (x
);
2644 convert_mpz_to_unsigned (result
->value
.integer
,
2645 gfc_integer_kinds
[k
].bit_size
);
2647 mpz_setbit (result
->value
.integer
, pos
);
2649 convert_mpz_to_signed (result
->value
.integer
,
2650 gfc_integer_kinds
[k
].bit_size
);
2657 gfc_simplify_ichar (gfc_expr
*e
, gfc_expr
*kind
)
2663 if (e
->expr_type
!= EXPR_CONSTANT
)
2666 if (e
->value
.character
.length
!= 1)
2668 gfc_error ("Argument of ICHAR at %L must be of length one", &e
->where
);
2669 return &gfc_bad_expr
;
2672 index
= e
->value
.character
.string
[0];
2674 k
= get_kind (BT_INTEGER
, kind
, "ICHAR", gfc_default_integer_kind
);
2676 return &gfc_bad_expr
;
2678 result
= gfc_get_int_expr (k
, &e
->where
, index
);
2680 return range_check (result
, "ICHAR");
2685 gfc_simplify_ieor (gfc_expr
*x
, gfc_expr
*y
)
2689 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
2692 result
= gfc_get_constant_expr (BT_INTEGER
, x
->ts
.kind
, &x
->where
);
2693 mpz_xor (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
2695 return range_check (result
, "IEOR");
2700 gfc_simplify_index (gfc_expr
*x
, gfc_expr
*y
, gfc_expr
*b
, gfc_expr
*kind
)
2703 int back
, len
, lensub
;
2704 int i
, j
, k
, count
, index
= 0, start
;
2706 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
2707 || ( b
!= NULL
&& b
->expr_type
!= EXPR_CONSTANT
))
2710 if (b
!= NULL
&& b
->value
.logical
!= 0)
2715 k
= get_kind (BT_INTEGER
, kind
, "INDEX", gfc_default_integer_kind
);
2717 return &gfc_bad_expr
;
2719 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &x
->where
);
2721 len
= x
->value
.character
.length
;
2722 lensub
= y
->value
.character
.length
;
2726 mpz_set_si (result
->value
.integer
, 0);
2734 mpz_set_si (result
->value
.integer
, 1);
2737 else if (lensub
== 1)
2739 for (i
= 0; i
< len
; i
++)
2741 for (j
= 0; j
< lensub
; j
++)
2743 if (y
->value
.character
.string
[j
]
2744 == x
->value
.character
.string
[i
])
2754 for (i
= 0; i
< len
; i
++)
2756 for (j
= 0; j
< lensub
; j
++)
2758 if (y
->value
.character
.string
[j
]
2759 == x
->value
.character
.string
[i
])
2764 for (k
= 0; k
< lensub
; k
++)
2766 if (y
->value
.character
.string
[k
]
2767 == x
->value
.character
.string
[k
+ start
])
2771 if (count
== lensub
)
2786 mpz_set_si (result
->value
.integer
, len
+ 1);
2789 else if (lensub
== 1)
2791 for (i
= 0; i
< len
; i
++)
2793 for (j
= 0; j
< lensub
; j
++)
2795 if (y
->value
.character
.string
[j
]
2796 == x
->value
.character
.string
[len
- i
])
2798 index
= len
- i
+ 1;
2806 for (i
= 0; i
< len
; i
++)
2808 for (j
= 0; j
< lensub
; j
++)
2810 if (y
->value
.character
.string
[j
]
2811 == x
->value
.character
.string
[len
- i
])
2814 if (start
<= len
- lensub
)
2817 for (k
= 0; k
< lensub
; k
++)
2818 if (y
->value
.character
.string
[k
]
2819 == x
->value
.character
.string
[k
+ start
])
2822 if (count
== lensub
)
2839 mpz_set_si (result
->value
.integer
, index
);
2840 return range_check (result
, "INDEX");
2845 simplify_intconv (gfc_expr
*e
, int kind
, const char *name
)
2847 gfc_expr
*result
= NULL
;
2849 if (e
->expr_type
!= EXPR_CONSTANT
)
2852 result
= gfc_convert_constant (e
, BT_INTEGER
, kind
);
2853 if (result
== &gfc_bad_expr
)
2854 return &gfc_bad_expr
;
2856 return range_check (result
, name
);
2861 gfc_simplify_int (gfc_expr
*e
, gfc_expr
*k
)
2865 kind
= get_kind (BT_INTEGER
, k
, "INT", gfc_default_integer_kind
);
2867 return &gfc_bad_expr
;
2869 return simplify_intconv (e
, kind
, "INT");
2873 gfc_simplify_int2 (gfc_expr
*e
)
2875 return simplify_intconv (e
, 2, "INT2");
2880 gfc_simplify_int8 (gfc_expr
*e
)
2882 return simplify_intconv (e
, 8, "INT8");
2887 gfc_simplify_long (gfc_expr
*e
)
2889 return simplify_intconv (e
, 4, "LONG");
2894 gfc_simplify_ifix (gfc_expr
*e
)
2896 gfc_expr
*rtrunc
, *result
;
2898 if (e
->expr_type
!= EXPR_CONSTANT
)
2901 rtrunc
= gfc_copy_expr (e
);
2902 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
2904 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
2906 gfc_mpfr_to_mpz (result
->value
.integer
, rtrunc
->value
.real
, &e
->where
);
2908 gfc_free_expr (rtrunc
);
2910 return range_check (result
, "IFIX");
2915 gfc_simplify_idint (gfc_expr
*e
)
2917 gfc_expr
*rtrunc
, *result
;
2919 if (e
->expr_type
!= EXPR_CONSTANT
)
2922 rtrunc
= gfc_copy_expr (e
);
2923 mpfr_trunc (rtrunc
->value
.real
, e
->value
.real
);
2925 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
2927 gfc_mpfr_to_mpz (result
->value
.integer
, rtrunc
->value
.real
, &e
->where
);
2929 gfc_free_expr (rtrunc
);
2931 return range_check (result
, "IDINT");
2936 gfc_simplify_ior (gfc_expr
*x
, gfc_expr
*y
)
2940 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
2943 result
= gfc_get_constant_expr (BT_INTEGER
, x
->ts
.kind
, &x
->where
);
2944 mpz_ior (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
2946 return range_check (result
, "IOR");
2951 do_bit_xor (gfc_expr
*result
, gfc_expr
*e
)
2953 gcc_assert (e
->ts
.type
== BT_INTEGER
&& e
->expr_type
== EXPR_CONSTANT
);
2954 gcc_assert (result
->ts
.type
== BT_INTEGER
2955 && result
->expr_type
== EXPR_CONSTANT
);
2957 mpz_xor (result
->value
.integer
, result
->value
.integer
, e
->value
.integer
);
2963 gfc_simplify_iparity (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
)
2965 return simplify_transformation (array
, dim
, mask
, 0, do_bit_xor
);
2970 gfc_simplify_is_iostat_end (gfc_expr
*x
)
2972 if (x
->expr_type
!= EXPR_CONSTANT
)
2975 return gfc_get_logical_expr (gfc_default_logical_kind
, &x
->where
,
2976 mpz_cmp_si (x
->value
.integer
,
2977 LIBERROR_END
) == 0);
2982 gfc_simplify_is_iostat_eor (gfc_expr
*x
)
2984 if (x
->expr_type
!= EXPR_CONSTANT
)
2987 return gfc_get_logical_expr (gfc_default_logical_kind
, &x
->where
,
2988 mpz_cmp_si (x
->value
.integer
,
2989 LIBERROR_EOR
) == 0);
2994 gfc_simplify_isnan (gfc_expr
*x
)
2996 if (x
->expr_type
!= EXPR_CONSTANT
)
2999 return gfc_get_logical_expr (gfc_default_logical_kind
, &x
->where
,
3000 mpfr_nan_p (x
->value
.real
));
3004 /* Performs a shift on its first argument. Depending on the last
3005 argument, the shift can be arithmetic, i.e. with filling from the
3006 left like in the SHIFTA intrinsic. */
3008 simplify_shift (gfc_expr
*e
, gfc_expr
*s
, const char *name
,
3009 bool arithmetic
, int direction
)
3012 int ashift
, *bits
, i
, k
, bitsize
, shift
;
3014 if (e
->expr_type
!= EXPR_CONSTANT
|| s
->expr_type
!= EXPR_CONSTANT
)
3017 gfc_extract_int (s
, &shift
);
3019 k
= gfc_validate_kind (BT_INTEGER
, e
->ts
.kind
, false);
3020 bitsize
= gfc_integer_kinds
[k
].bit_size
;
3022 result
= gfc_get_constant_expr (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
3026 mpz_set (result
->value
.integer
, e
->value
.integer
);
3030 if (direction
> 0 && shift
< 0)
3032 /* Left shift, as in SHIFTL. */
3033 gfc_error ("Second argument of %s is negative at %L", name
, &e
->where
);
3034 return &gfc_bad_expr
;
3036 else if (direction
< 0)
3038 /* Right shift, as in SHIFTR or SHIFTA. */
3041 gfc_error ("Second argument of %s is negative at %L",
3043 return &gfc_bad_expr
;
3049 ashift
= (shift
>= 0 ? shift
: -shift
);
3051 if (ashift
> bitsize
)
3053 gfc_error ("Magnitude of second argument of %s exceeds bit size "
3054 "at %L", name
, &e
->where
);
3055 return &gfc_bad_expr
;
3058 bits
= XCNEWVEC (int, bitsize
);
3060 for (i
= 0; i
< bitsize
; i
++)
3061 bits
[i
] = mpz_tstbit (e
->value
.integer
, i
);
3066 for (i
= 0; i
< shift
; i
++)
3067 mpz_clrbit (result
->value
.integer
, i
);
3069 for (i
= 0; i
< bitsize
- shift
; i
++)
3072 mpz_clrbit (result
->value
.integer
, i
+ shift
);
3074 mpz_setbit (result
->value
.integer
, i
+ shift
);
3080 if (arithmetic
&& bits
[bitsize
- 1])
3081 for (i
= bitsize
- 1; i
>= bitsize
- ashift
; i
--)
3082 mpz_setbit (result
->value
.integer
, i
);
3084 for (i
= bitsize
- 1; i
>= bitsize
- ashift
; i
--)
3085 mpz_clrbit (result
->value
.integer
, i
);
3087 for (i
= bitsize
- 1; i
>= ashift
; i
--)
3090 mpz_clrbit (result
->value
.integer
, i
- ashift
);
3092 mpz_setbit (result
->value
.integer
, i
- ashift
);
3096 convert_mpz_to_signed (result
->value
.integer
, bitsize
);
3104 gfc_simplify_ishft (gfc_expr
*e
, gfc_expr
*s
)
3106 return simplify_shift (e
, s
, "ISHFT", false, 0);
3111 gfc_simplify_lshift (gfc_expr
*e
, gfc_expr
*s
)
3113 return simplify_shift (e
, s
, "LSHIFT", false, 1);
3118 gfc_simplify_rshift (gfc_expr
*e
, gfc_expr
*s
)
3120 return simplify_shift (e
, s
, "RSHIFT", true, -1);
3125 gfc_simplify_shifta (gfc_expr
*e
, gfc_expr
*s
)
3127 return simplify_shift (e
, s
, "SHIFTA", true, -1);
3132 gfc_simplify_shiftl (gfc_expr
*e
, gfc_expr
*s
)
3134 return simplify_shift (e
, s
, "SHIFTL", false, 1);
3139 gfc_simplify_shiftr (gfc_expr
*e
, gfc_expr
*s
)
3141 return simplify_shift (e
, s
, "SHIFTR", false, -1);
3146 gfc_simplify_ishftc (gfc_expr
*e
, gfc_expr
*s
, gfc_expr
*sz
)
3149 int shift
, ashift
, isize
, ssize
, delta
, k
;
3152 if (e
->expr_type
!= EXPR_CONSTANT
|| s
->expr_type
!= EXPR_CONSTANT
)
3155 gfc_extract_int (s
, &shift
);
3157 k
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
3158 isize
= gfc_integer_kinds
[k
].bit_size
;
3162 if (sz
->expr_type
!= EXPR_CONSTANT
)
3165 gfc_extract_int (sz
, &ssize
);
3179 gfc_error ("Magnitude of second argument of ISHFTC exceeds "
3180 "BIT_SIZE of first argument at %L", &s
->where
);
3181 return &gfc_bad_expr
;
3184 result
= gfc_get_constant_expr (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
3186 mpz_set (result
->value
.integer
, e
->value
.integer
);
3191 convert_mpz_to_unsigned (result
->value
.integer
, isize
);
3193 bits
= XCNEWVEC (int, ssize
);
3195 for (i
= 0; i
< ssize
; i
++)
3196 bits
[i
] = mpz_tstbit (e
->value
.integer
, i
);
3198 delta
= ssize
- ashift
;
3202 for (i
= 0; i
< delta
; i
++)
3205 mpz_clrbit (result
->value
.integer
, i
+ shift
);
3207 mpz_setbit (result
->value
.integer
, i
+ shift
);
3210 for (i
= delta
; i
< ssize
; i
++)
3213 mpz_clrbit (result
->value
.integer
, i
- delta
);
3215 mpz_setbit (result
->value
.integer
, i
- delta
);
3220 for (i
= 0; i
< ashift
; i
++)
3223 mpz_clrbit (result
->value
.integer
, i
+ delta
);
3225 mpz_setbit (result
->value
.integer
, i
+ delta
);
3228 for (i
= ashift
; i
< ssize
; i
++)
3231 mpz_clrbit (result
->value
.integer
, i
+ shift
);
3233 mpz_setbit (result
->value
.integer
, i
+ shift
);
3237 convert_mpz_to_signed (result
->value
.integer
, isize
);
3245 gfc_simplify_kind (gfc_expr
*e
)
3247 return gfc_get_int_expr (gfc_default_integer_kind
, NULL
, e
->ts
.kind
);
3252 simplify_bound_dim (gfc_expr
*array
, gfc_expr
*kind
, int d
, int upper
,
3253 gfc_array_spec
*as
, gfc_ref
*ref
, bool coarray
)
3255 gfc_expr
*l
, *u
, *result
;
3258 k
= get_kind (BT_INTEGER
, kind
, upper
? "UBOUND" : "LBOUND",
3259 gfc_default_integer_kind
);
3261 return &gfc_bad_expr
;
3263 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &array
->where
);
3265 /* For non-variables, LBOUND(expr, DIM=n) = 1 and
3266 UBOUND(expr, DIM=n) = SIZE(expr, DIM=n). */
3267 if (!coarray
&& array
->expr_type
!= EXPR_VARIABLE
)
3271 gfc_expr
* dim
= result
;
3272 mpz_set_si (dim
->value
.integer
, d
);
3274 result
= simplify_size (array
, dim
, k
);
3275 gfc_free_expr (dim
);
3280 mpz_set_si (result
->value
.integer
, 1);
3285 /* Otherwise, we have a variable expression. */
3286 gcc_assert (array
->expr_type
== EXPR_VARIABLE
);
3289 if (!gfc_resolve_array_spec (as
, 0))
3292 /* The last dimension of an assumed-size array is special. */
3293 if ((!coarray
&& d
== as
->rank
&& as
->type
== AS_ASSUMED_SIZE
&& !upper
)
3294 || (coarray
&& d
== as
->rank
+ as
->corank
3295 && (!upper
|| gfc_option
.coarray
== GFC_FCOARRAY_SINGLE
)))
3297 if (as
->lower
[d
-1]->expr_type
== EXPR_CONSTANT
)
3299 gfc_free_expr (result
);
3300 return gfc_copy_expr (as
->lower
[d
-1]);
3306 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &array
->where
);
3308 /* Then, we need to know the extent of the given dimension. */
3309 if (coarray
|| ref
->u
.ar
.type
== AR_FULL
)
3314 if (l
->expr_type
!= EXPR_CONSTANT
|| u
== NULL
3315 || u
->expr_type
!= EXPR_CONSTANT
)
3318 if (mpz_cmp (l
->value
.integer
, u
->value
.integer
) > 0)
3322 mpz_set_si (result
->value
.integer
, 0);
3324 mpz_set_si (result
->value
.integer
, 1);
3328 /* Nonzero extent. */
3330 mpz_set (result
->value
.integer
, u
->value
.integer
);
3332 mpz_set (result
->value
.integer
, l
->value
.integer
);
3339 if (!gfc_ref_dimen_size (&ref
->u
.ar
, d
- 1, &result
->value
.integer
, NULL
))
3343 mpz_set_si (result
->value
.integer
, (long int) 1);
3347 return range_check (result
, upper
? "UBOUND" : "LBOUND");
3350 gfc_free_expr (result
);
3356 simplify_bound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
, int upper
)
3362 if (array
->ts
.type
== BT_CLASS
)
3365 if (array
->expr_type
!= EXPR_VARIABLE
)
3372 /* Follow any component references. */
3373 as
= array
->symtree
->n
.sym
->as
;
3374 for (ref
= array
->ref
; ref
; ref
= ref
->next
)
3379 switch (ref
->u
.ar
.type
)
3386 /* We're done because 'as' has already been set in the
3387 previous iteration. */
3404 as
= ref
->u
.c
.component
->as
;
3416 if (as
&& (as
->type
== AS_DEFERRED
|| as
->type
== AS_ASSUMED_SHAPE
3417 || as
->type
== AS_ASSUMED_RANK
))
3422 /* Multi-dimensional bounds. */
3423 gfc_expr
*bounds
[GFC_MAX_DIMENSIONS
];
3427 /* UBOUND(ARRAY) is not valid for an assumed-size array. */
3428 if (upper
&& as
&& as
->type
== AS_ASSUMED_SIZE
)
3430 /* An error message will be emitted in
3431 check_assumed_size_reference (resolve.c). */
3432 return &gfc_bad_expr
;
3435 /* Simplify the bounds for each dimension. */
3436 for (d
= 0; d
< array
->rank
; d
++)
3438 bounds
[d
] = simplify_bound_dim (array
, kind
, d
+ 1, upper
, as
, ref
,
3440 if (bounds
[d
] == NULL
|| bounds
[d
] == &gfc_bad_expr
)
3444 for (j
= 0; j
< d
; j
++)
3445 gfc_free_expr (bounds
[j
]);
3450 /* Allocate the result expression. */
3451 k
= get_kind (BT_INTEGER
, kind
, upper
? "UBOUND" : "LBOUND",
3452 gfc_default_integer_kind
);
3454 return &gfc_bad_expr
;
3456 e
= gfc_get_array_expr (BT_INTEGER
, k
, &array
->where
);
3458 /* The result is a rank 1 array; its size is the rank of the first
3459 argument to {L,U}BOUND. */
3461 e
->shape
= gfc_get_shape (1);
3462 mpz_init_set_ui (e
->shape
[0], array
->rank
);
3464 /* Create the constructor for this array. */
3465 for (d
= 0; d
< array
->rank
; d
++)
3466 gfc_constructor_append_expr (&e
->value
.constructor
,
3467 bounds
[d
], &e
->where
);
3473 /* A DIM argument is specified. */
3474 if (dim
->expr_type
!= EXPR_CONSTANT
)
3477 d
= mpz_get_si (dim
->value
.integer
);
3479 if ((d
< 1 || d
> array
->rank
)
3480 || (d
== array
->rank
&& as
&& as
->type
== AS_ASSUMED_SIZE
&& upper
))
3482 gfc_error ("DIM argument at %L is out of bounds", &dim
->where
);
3483 return &gfc_bad_expr
;
3486 if (as
&& as
->type
== AS_ASSUMED_RANK
)
3489 return simplify_bound_dim (array
, kind
, d
, upper
, as
, ref
, false);
3495 simplify_cobound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
, int upper
)
3501 if (array
->expr_type
!= EXPR_VARIABLE
)
3504 /* Follow any component references. */
3505 as
= (array
->ts
.type
== BT_CLASS
&& array
->ts
.u
.derived
->components
)
3506 ? array
->ts
.u
.derived
->components
->as
3507 : array
->symtree
->n
.sym
->as
;
3508 for (ref
= array
->ref
; ref
; ref
= ref
->next
)
3513 switch (ref
->u
.ar
.type
)
3516 if (ref
->u
.ar
.as
->corank
> 0)
3518 gcc_assert (as
== ref
->u
.ar
.as
);
3525 /* We're done because 'as' has already been set in the
3526 previous iteration. */
3543 as
= ref
->u
.c
.component
->as
;
3556 if (as
->cotype
== AS_DEFERRED
|| as
->cotype
== AS_ASSUMED_SHAPE
)
3561 /* Multi-dimensional cobounds. */
3562 gfc_expr
*bounds
[GFC_MAX_DIMENSIONS
];
3566 /* Simplify the cobounds for each dimension. */
3567 for (d
= 0; d
< as
->corank
; d
++)
3569 bounds
[d
] = simplify_bound_dim (array
, kind
, d
+ 1 + as
->rank
,
3570 upper
, as
, ref
, true);
3571 if (bounds
[d
] == NULL
|| bounds
[d
] == &gfc_bad_expr
)
3575 for (j
= 0; j
< d
; j
++)
3576 gfc_free_expr (bounds
[j
]);
3581 /* Allocate the result expression. */
3582 e
= gfc_get_expr ();
3583 e
->where
= array
->where
;
3584 e
->expr_type
= EXPR_ARRAY
;
3585 e
->ts
.type
= BT_INTEGER
;
3586 k
= get_kind (BT_INTEGER
, kind
, upper
? "UCOBOUND" : "LCOBOUND",
3587 gfc_default_integer_kind
);
3591 return &gfc_bad_expr
;
3595 /* The result is a rank 1 array; its size is the rank of the first
3596 argument to {L,U}COBOUND. */
3598 e
->shape
= gfc_get_shape (1);
3599 mpz_init_set_ui (e
->shape
[0], as
->corank
);
3601 /* Create the constructor for this array. */
3602 for (d
= 0; d
< as
->corank
; d
++)
3603 gfc_constructor_append_expr (&e
->value
.constructor
,
3604 bounds
[d
], &e
->where
);
3609 /* A DIM argument is specified. */
3610 if (dim
->expr_type
!= EXPR_CONSTANT
)
3613 d
= mpz_get_si (dim
->value
.integer
);
3615 if (d
< 1 || d
> as
->corank
)
3617 gfc_error ("DIM argument at %L is out of bounds", &dim
->where
);
3618 return &gfc_bad_expr
;
3621 return simplify_bound_dim (array
, kind
, d
+as
->rank
, upper
, as
, ref
, true);
3627 gfc_simplify_lbound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
)
3629 return simplify_bound (array
, dim
, kind
, 0);
3634 gfc_simplify_lcobound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
)
3636 return simplify_cobound (array
, dim
, kind
, 0);
3640 gfc_simplify_leadz (gfc_expr
*e
)
3642 unsigned long lz
, bs
;
3645 if (e
->expr_type
!= EXPR_CONSTANT
)
3648 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
3649 bs
= gfc_integer_kinds
[i
].bit_size
;
3650 if (mpz_cmp_si (e
->value
.integer
, 0) == 0)
3652 else if (mpz_cmp_si (e
->value
.integer
, 0) < 0)
3655 lz
= bs
- mpz_sizeinbase (e
->value
.integer
, 2);
3657 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, lz
);
3662 gfc_simplify_len (gfc_expr
*e
, gfc_expr
*kind
)
3665 int k
= get_kind (BT_INTEGER
, kind
, "LEN", gfc_default_integer_kind
);
3668 return &gfc_bad_expr
;
3670 if (e
->expr_type
== EXPR_CONSTANT
)
3672 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &e
->where
);
3673 mpz_set_si (result
->value
.integer
, e
->value
.character
.length
);
3674 return range_check (result
, "LEN");
3676 else if (e
->ts
.u
.cl
!= NULL
&& e
->ts
.u
.cl
->length
!= NULL
3677 && e
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
3678 && e
->ts
.u
.cl
->length
->ts
.type
== BT_INTEGER
)
3680 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &e
->where
);
3681 mpz_set (result
->value
.integer
, e
->ts
.u
.cl
->length
->value
.integer
);
3682 return range_check (result
, "LEN");
3690 gfc_simplify_len_trim (gfc_expr
*e
, gfc_expr
*kind
)
3694 int k
= get_kind (BT_INTEGER
, kind
, "LEN_TRIM", gfc_default_integer_kind
);
3697 return &gfc_bad_expr
;
3699 if (e
->expr_type
!= EXPR_CONSTANT
)
3702 len
= e
->value
.character
.length
;
3703 for (count
= 0, i
= 1; i
<= len
; i
++)
3704 if (e
->value
.character
.string
[len
- i
] == ' ')
3709 result
= gfc_get_int_expr (k
, &e
->where
, len
- count
);
3710 return range_check (result
, "LEN_TRIM");
3714 gfc_simplify_lgamma (gfc_expr
*x
)
3719 if (x
->expr_type
!= EXPR_CONSTANT
)
3722 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3723 mpfr_lgamma (result
->value
.real
, &sg
, x
->value
.real
, GFC_RND_MODE
);
3725 return range_check (result
, "LGAMMA");
3730 gfc_simplify_lge (gfc_expr
*a
, gfc_expr
*b
)
3732 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
3735 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
,
3736 gfc_compare_string (a
, b
) >= 0);
3741 gfc_simplify_lgt (gfc_expr
*a
, gfc_expr
*b
)
3743 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
3746 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
,
3747 gfc_compare_string (a
, b
) > 0);
3752 gfc_simplify_lle (gfc_expr
*a
, gfc_expr
*b
)
3754 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
3757 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
,
3758 gfc_compare_string (a
, b
) <= 0);
3763 gfc_simplify_llt (gfc_expr
*a
, gfc_expr
*b
)
3765 if (a
->expr_type
!= EXPR_CONSTANT
|| b
->expr_type
!= EXPR_CONSTANT
)
3768 return gfc_get_logical_expr (gfc_default_logical_kind
, &a
->where
,
3769 gfc_compare_string (a
, b
) < 0);
3774 gfc_simplify_log (gfc_expr
*x
)
3778 if (x
->expr_type
!= EXPR_CONSTANT
)
3781 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3786 if (mpfr_sgn (x
->value
.real
) <= 0)
3788 gfc_error ("Argument of LOG at %L cannot be less than or equal "
3789 "to zero", &x
->where
);
3790 gfc_free_expr (result
);
3791 return &gfc_bad_expr
;
3794 mpfr_log (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
3798 if ((mpfr_sgn (mpc_realref (x
->value
.complex)) == 0)
3799 && (mpfr_sgn (mpc_imagref (x
->value
.complex)) == 0))
3801 gfc_error ("Complex argument of LOG at %L cannot be zero",
3803 gfc_free_expr (result
);
3804 return &gfc_bad_expr
;
3807 gfc_set_model_kind (x
->ts
.kind
);
3808 mpc_log (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
3812 gfc_internal_error ("gfc_simplify_log: bad type");
3815 return range_check (result
, "LOG");
3820 gfc_simplify_log10 (gfc_expr
*x
)
3824 if (x
->expr_type
!= EXPR_CONSTANT
)
3827 if (mpfr_sgn (x
->value
.real
) <= 0)
3829 gfc_error ("Argument of LOG10 at %L cannot be less than or equal "
3830 "to zero", &x
->where
);
3831 return &gfc_bad_expr
;
3834 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
3835 mpfr_log10 (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
3837 return range_check (result
, "LOG10");
3842 gfc_simplify_logical (gfc_expr
*e
, gfc_expr
*k
)
3846 kind
= get_kind (BT_LOGICAL
, k
, "LOGICAL", gfc_default_logical_kind
);
3848 return &gfc_bad_expr
;
3850 if (e
->expr_type
!= EXPR_CONSTANT
)
3853 return gfc_get_logical_expr (kind
, &e
->where
, e
->value
.logical
);
3858 gfc_simplify_matmul (gfc_expr
*matrix_a
, gfc_expr
*matrix_b
)
3861 int row
, result_rows
, col
, result_columns
;
3862 int stride_a
, offset_a
, stride_b
, offset_b
;
3864 if (!is_constant_array_expr (matrix_a
)
3865 || !is_constant_array_expr (matrix_b
))
3868 gcc_assert (gfc_compare_types (&matrix_a
->ts
, &matrix_b
->ts
));
3869 result
= gfc_get_array_expr (matrix_a
->ts
.type
,
3873 if (matrix_a
->rank
== 1 && matrix_b
->rank
== 2)
3876 result_columns
= mpz_get_si (matrix_b
->shape
[1]);
3878 stride_b
= mpz_get_si (matrix_b
->shape
[0]);
3881 result
->shape
= gfc_get_shape (result
->rank
);
3882 mpz_init_set_si (result
->shape
[0], result_columns
);
3884 else if (matrix_a
->rank
== 2 && matrix_b
->rank
== 1)
3886 result_rows
= mpz_get_si (matrix_a
->shape
[0]);
3888 stride_a
= mpz_get_si (matrix_a
->shape
[0]);
3892 result
->shape
= gfc_get_shape (result
->rank
);
3893 mpz_init_set_si (result
->shape
[0], result_rows
);
3895 else if (matrix_a
->rank
== 2 && matrix_b
->rank
== 2)
3897 result_rows
= mpz_get_si (matrix_a
->shape
[0]);
3898 result_columns
= mpz_get_si (matrix_b
->shape
[1]);
3899 stride_a
= mpz_get_si (matrix_a
->shape
[0]);
3900 stride_b
= mpz_get_si (matrix_b
->shape
[0]);
3903 result
->shape
= gfc_get_shape (result
->rank
);
3904 mpz_init_set_si (result
->shape
[0], result_rows
);
3905 mpz_init_set_si (result
->shape
[1], result_columns
);
3910 offset_a
= offset_b
= 0;
3911 for (col
= 0; col
< result_columns
; ++col
)
3915 for (row
= 0; row
< result_rows
; ++row
)
3917 gfc_expr
*e
= compute_dot_product (matrix_a
, stride_a
, offset_a
,
3918 matrix_b
, 1, offset_b
, false);
3919 gfc_constructor_append_expr (&result
->value
.constructor
,
3925 offset_b
+= stride_b
;
3933 gfc_simplify_maskr (gfc_expr
*i
, gfc_expr
*kind_arg
)
3939 if (i
->expr_type
!= EXPR_CONSTANT
)
3942 kind
= get_kind (BT_INTEGER
, kind_arg
, "MASKR", gfc_default_integer_kind
);
3944 return &gfc_bad_expr
;
3945 k
= gfc_validate_kind (BT_INTEGER
, kind
, false);
3947 s
= gfc_extract_int (i
, &arg
);
3950 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &i
->where
);
3952 /* MASKR(n) = 2^n - 1 */
3953 mpz_set_ui (result
->value
.integer
, 1);
3954 mpz_mul_2exp (result
->value
.integer
, result
->value
.integer
, arg
);
3955 mpz_sub_ui (result
->value
.integer
, result
->value
.integer
, 1);
3957 convert_mpz_to_signed (result
->value
.integer
, gfc_integer_kinds
[k
].bit_size
);
3964 gfc_simplify_maskl (gfc_expr
*i
, gfc_expr
*kind_arg
)
3971 if (i
->expr_type
!= EXPR_CONSTANT
)
3974 kind
= get_kind (BT_INTEGER
, kind_arg
, "MASKL", gfc_default_integer_kind
);
3976 return &gfc_bad_expr
;
3977 k
= gfc_validate_kind (BT_INTEGER
, kind
, false);
3979 s
= gfc_extract_int (i
, &arg
);
3982 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &i
->where
);
3984 /* MASKL(n) = 2^bit_size - 2^(bit_size - n) */
3985 mpz_init_set_ui (z
, 1);
3986 mpz_mul_2exp (z
, z
, gfc_integer_kinds
[k
].bit_size
);
3987 mpz_set_ui (result
->value
.integer
, 1);
3988 mpz_mul_2exp (result
->value
.integer
, result
->value
.integer
,
3989 gfc_integer_kinds
[k
].bit_size
- arg
);
3990 mpz_sub (result
->value
.integer
, z
, result
->value
.integer
);
3993 convert_mpz_to_signed (result
->value
.integer
, gfc_integer_kinds
[k
].bit_size
);
4000 gfc_simplify_merge (gfc_expr
*tsource
, gfc_expr
*fsource
, gfc_expr
*mask
)
4003 gfc_constructor
*tsource_ctor
, *fsource_ctor
, *mask_ctor
;
4005 if (mask
->expr_type
== EXPR_CONSTANT
)
4006 return gfc_get_parentheses (gfc_copy_expr (mask
->value
.logical
4007 ? tsource
: fsource
));
4009 if (!mask
->rank
|| !is_constant_array_expr (mask
)
4010 || !is_constant_array_expr (tsource
) || !is_constant_array_expr (fsource
))
4013 result
= gfc_get_array_expr (tsource
->ts
.type
, tsource
->ts
.kind
,
4015 if (tsource
->ts
.type
== BT_DERIVED
)
4016 result
->ts
.u
.derived
= tsource
->ts
.u
.derived
;
4017 else if (tsource
->ts
.type
== BT_CHARACTER
)
4018 result
->ts
.u
.cl
= tsource
->ts
.u
.cl
;
4020 tsource_ctor
= gfc_constructor_first (tsource
->value
.constructor
);
4021 fsource_ctor
= gfc_constructor_first (fsource
->value
.constructor
);
4022 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
4026 if (mask_ctor
->expr
->value
.logical
)
4027 gfc_constructor_append_expr (&result
->value
.constructor
,
4028 gfc_copy_expr (tsource_ctor
->expr
),
4031 gfc_constructor_append_expr (&result
->value
.constructor
,
4032 gfc_copy_expr (fsource_ctor
->expr
),
4034 tsource_ctor
= gfc_constructor_next (tsource_ctor
);
4035 fsource_ctor
= gfc_constructor_next (fsource_ctor
);
4036 mask_ctor
= gfc_constructor_next (mask_ctor
);
4039 result
->shape
= gfc_get_shape (1);
4040 gfc_array_size (result
, &result
->shape
[0]);
4047 gfc_simplify_merge_bits (gfc_expr
*i
, gfc_expr
*j
, gfc_expr
*mask_expr
)
4049 mpz_t arg1
, arg2
, mask
;
4052 if (i
->expr_type
!= EXPR_CONSTANT
|| j
->expr_type
!= EXPR_CONSTANT
4053 || mask_expr
->expr_type
!= EXPR_CONSTANT
)
4056 result
= gfc_get_constant_expr (BT_INTEGER
, i
->ts
.kind
, &i
->where
);
4058 /* Convert all argument to unsigned. */
4059 mpz_init_set (arg1
, i
->value
.integer
);
4060 mpz_init_set (arg2
, j
->value
.integer
);
4061 mpz_init_set (mask
, mask_expr
->value
.integer
);
4063 /* MERGE_BITS(I,J,MASK) = IOR (IAND (I, MASK), IAND (J, NOT (MASK))). */
4064 mpz_and (arg1
, arg1
, mask
);
4065 mpz_com (mask
, mask
);
4066 mpz_and (arg2
, arg2
, mask
);
4067 mpz_ior (result
->value
.integer
, arg1
, arg2
);
4077 /* Selects between current value and extremum for simplify_min_max
4078 and simplify_minval_maxval. */
4080 min_max_choose (gfc_expr
*arg
, gfc_expr
*extremum
, int sign
)
4082 switch (arg
->ts
.type
)
4085 if (mpz_cmp (arg
->value
.integer
,
4086 extremum
->value
.integer
) * sign
> 0)
4087 mpz_set (extremum
->value
.integer
, arg
->value
.integer
);
4091 /* We need to use mpfr_min and mpfr_max to treat NaN properly. */
4093 mpfr_max (extremum
->value
.real
, extremum
->value
.real
,
4094 arg
->value
.real
, GFC_RND_MODE
);
4096 mpfr_min (extremum
->value
.real
, extremum
->value
.real
,
4097 arg
->value
.real
, GFC_RND_MODE
);
4101 #define LENGTH(x) ((x)->value.character.length)
4102 #define STRING(x) ((x)->value.character.string)
4103 if (LENGTH (extremum
) < LENGTH(arg
))
4105 gfc_char_t
*tmp
= STRING(extremum
);
4107 STRING(extremum
) = gfc_get_wide_string (LENGTH(arg
) + 1);
4108 memcpy (STRING(extremum
), tmp
,
4109 LENGTH(extremum
) * sizeof (gfc_char_t
));
4110 gfc_wide_memset (&STRING(extremum
)[LENGTH(extremum
)], ' ',
4111 LENGTH(arg
) - LENGTH(extremum
));
4112 STRING(extremum
)[LENGTH(arg
)] = '\0'; /* For debugger */
4113 LENGTH(extremum
) = LENGTH(arg
);
4117 if (gfc_compare_string (arg
, extremum
) * sign
> 0)
4119 free (STRING(extremum
));
4120 STRING(extremum
) = gfc_get_wide_string (LENGTH(extremum
) + 1);
4121 memcpy (STRING(extremum
), STRING(arg
),
4122 LENGTH(arg
) * sizeof (gfc_char_t
));
4123 gfc_wide_memset (&STRING(extremum
)[LENGTH(arg
)], ' ',
4124 LENGTH(extremum
) - LENGTH(arg
));
4125 STRING(extremum
)[LENGTH(extremum
)] = '\0'; /* For debugger */
4132 gfc_internal_error ("simplify_min_max(): Bad type in arglist");
4137 /* This function is special since MAX() can take any number of
4138 arguments. The simplified expression is a rewritten version of the
4139 argument list containing at most one constant element. Other
4140 constant elements are deleted. Because the argument list has
4141 already been checked, this function always succeeds. sign is 1 for
4142 MAX(), -1 for MIN(). */
4145 simplify_min_max (gfc_expr
*expr
, int sign
)
4147 gfc_actual_arglist
*arg
, *last
, *extremum
;
4148 gfc_intrinsic_sym
* specific
;
4152 specific
= expr
->value
.function
.isym
;
4154 arg
= expr
->value
.function
.actual
;
4156 for (; arg
; last
= arg
, arg
= arg
->next
)
4158 if (arg
->expr
->expr_type
!= EXPR_CONSTANT
)
4161 if (extremum
== NULL
)
4167 min_max_choose (arg
->expr
, extremum
->expr
, sign
);
4169 /* Delete the extra constant argument. */
4170 last
->next
= arg
->next
;
4173 gfc_free_actual_arglist (arg
);
4177 /* If there is one value left, replace the function call with the
4179 if (expr
->value
.function
.actual
->next
!= NULL
)
4182 /* Convert to the correct type and kind. */
4183 if (expr
->ts
.type
!= BT_UNKNOWN
)
4184 return gfc_convert_constant (expr
->value
.function
.actual
->expr
,
4185 expr
->ts
.type
, expr
->ts
.kind
);
4187 if (specific
->ts
.type
!= BT_UNKNOWN
)
4188 return gfc_convert_constant (expr
->value
.function
.actual
->expr
,
4189 specific
->ts
.type
, specific
->ts
.kind
);
4191 return gfc_copy_expr (expr
->value
.function
.actual
->expr
);
4196 gfc_simplify_min (gfc_expr
*e
)
4198 return simplify_min_max (e
, -1);
4203 gfc_simplify_max (gfc_expr
*e
)
4205 return simplify_min_max (e
, 1);
4209 /* This is a simplified version of simplify_min_max to provide
4210 simplification of minval and maxval for a vector. */
4213 simplify_minval_maxval (gfc_expr
*expr
, int sign
)
4215 gfc_constructor
*c
, *extremum
;
4216 gfc_intrinsic_sym
* specific
;
4219 specific
= expr
->value
.function
.isym
;
4221 for (c
= gfc_constructor_first (expr
->value
.constructor
);
4222 c
; c
= gfc_constructor_next (c
))
4224 if (c
->expr
->expr_type
!= EXPR_CONSTANT
)
4227 if (extremum
== NULL
)
4233 min_max_choose (c
->expr
, extremum
->expr
, sign
);
4236 if (extremum
== NULL
)
4239 /* Convert to the correct type and kind. */
4240 if (expr
->ts
.type
!= BT_UNKNOWN
)
4241 return gfc_convert_constant (extremum
->expr
,
4242 expr
->ts
.type
, expr
->ts
.kind
);
4244 if (specific
->ts
.type
!= BT_UNKNOWN
)
4245 return gfc_convert_constant (extremum
->expr
,
4246 specific
->ts
.type
, specific
->ts
.kind
);
4248 return gfc_copy_expr (extremum
->expr
);
4253 gfc_simplify_minval (gfc_expr
*array
, gfc_expr
* dim
, gfc_expr
*mask
)
4255 if (array
->expr_type
!= EXPR_ARRAY
|| array
->rank
!= 1 || dim
|| mask
)
4258 return simplify_minval_maxval (array
, -1);
4263 gfc_simplify_maxval (gfc_expr
*array
, gfc_expr
* dim
, gfc_expr
*mask
)
4265 if (array
->expr_type
!= EXPR_ARRAY
|| array
->rank
!= 1 || dim
|| mask
)
4268 return simplify_minval_maxval (array
, 1);
4273 gfc_simplify_maxexponent (gfc_expr
*x
)
4275 int i
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, false);
4276 return gfc_get_int_expr (gfc_default_integer_kind
, &x
->where
,
4277 gfc_real_kinds
[i
].max_exponent
);
4282 gfc_simplify_minexponent (gfc_expr
*x
)
4284 int i
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, false);
4285 return gfc_get_int_expr (gfc_default_integer_kind
, &x
->where
,
4286 gfc_real_kinds
[i
].min_exponent
);
4291 gfc_simplify_mod (gfc_expr
*a
, gfc_expr
*p
)
4296 if (a
->expr_type
!= EXPR_CONSTANT
|| p
->expr_type
!= EXPR_CONSTANT
)
4299 kind
= a
->ts
.kind
> p
->ts
.kind
? a
->ts
.kind
: p
->ts
.kind
;
4300 result
= gfc_get_constant_expr (a
->ts
.type
, kind
, &a
->where
);
4305 if (mpz_cmp_ui (p
->value
.integer
, 0) == 0)
4307 /* Result is processor-dependent. */
4308 gfc_error ("Second argument MOD at %L is zero", &a
->where
);
4309 gfc_free_expr (result
);
4310 return &gfc_bad_expr
;
4312 mpz_tdiv_r (result
->value
.integer
, a
->value
.integer
, p
->value
.integer
);
4316 if (mpfr_cmp_ui (p
->value
.real
, 0) == 0)
4318 /* Result is processor-dependent. */
4319 gfc_error ("Second argument of MOD at %L is zero", &p
->where
);
4320 gfc_free_expr (result
);
4321 return &gfc_bad_expr
;
4324 gfc_set_model_kind (kind
);
4325 mpfr_fmod (result
->value
.real
, a
->value
.real
, p
->value
.real
,
4330 gfc_internal_error ("gfc_simplify_mod(): Bad arguments");
4333 return range_check (result
, "MOD");
4338 gfc_simplify_modulo (gfc_expr
*a
, gfc_expr
*p
)
4343 if (a
->expr_type
!= EXPR_CONSTANT
|| p
->expr_type
!= EXPR_CONSTANT
)
4346 kind
= a
->ts
.kind
> p
->ts
.kind
? a
->ts
.kind
: p
->ts
.kind
;
4347 result
= gfc_get_constant_expr (a
->ts
.type
, kind
, &a
->where
);
4352 if (mpz_cmp_ui (p
->value
.integer
, 0) == 0)
4354 /* Result is processor-dependent. This processor just opts
4355 to not handle it at all. */
4356 gfc_error ("Second argument of MODULO at %L is zero", &a
->where
);
4357 gfc_free_expr (result
);
4358 return &gfc_bad_expr
;
4360 mpz_fdiv_r (result
->value
.integer
, a
->value
.integer
, p
->value
.integer
);
4365 if (mpfr_cmp_ui (p
->value
.real
, 0) == 0)
4367 /* Result is processor-dependent. */
4368 gfc_error ("Second argument of MODULO at %L is zero", &p
->where
);
4369 gfc_free_expr (result
);
4370 return &gfc_bad_expr
;
4373 gfc_set_model_kind (kind
);
4374 mpfr_fmod (result
->value
.real
, a
->value
.real
, p
->value
.real
,
4376 if (mpfr_cmp_ui (result
->value
.real
, 0) != 0)
4378 if (mpfr_signbit (a
->value
.real
) != mpfr_signbit (p
->value
.real
))
4379 mpfr_add (result
->value
.real
, result
->value
.real
, p
->value
.real
,
4383 mpfr_copysign (result
->value
.real
, result
->value
.real
,
4384 p
->value
.real
, GFC_RND_MODE
);
4388 gfc_internal_error ("gfc_simplify_modulo(): Bad arguments");
4391 return range_check (result
, "MODULO");
4395 /* Exists for the sole purpose of consistency with other intrinsics. */
4397 gfc_simplify_mvbits (gfc_expr
*f ATTRIBUTE_UNUSED
,
4398 gfc_expr
*fp ATTRIBUTE_UNUSED
,
4399 gfc_expr
*l ATTRIBUTE_UNUSED
,
4400 gfc_expr
*to ATTRIBUTE_UNUSED
,
4401 gfc_expr
*tp ATTRIBUTE_UNUSED
)
4408 gfc_simplify_nearest (gfc_expr
*x
, gfc_expr
*s
)
4411 mp_exp_t emin
, emax
;
4414 if (x
->expr_type
!= EXPR_CONSTANT
|| s
->expr_type
!= EXPR_CONSTANT
)
4417 result
= gfc_copy_expr (x
);
4419 /* Save current values of emin and emax. */
4420 emin
= mpfr_get_emin ();
4421 emax
= mpfr_get_emax ();
4423 /* Set emin and emax for the current model number. */
4424 kind
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, 0);
4425 mpfr_set_emin ((mp_exp_t
) gfc_real_kinds
[kind
].min_exponent
-
4426 mpfr_get_prec(result
->value
.real
) + 1);
4427 mpfr_set_emax ((mp_exp_t
) gfc_real_kinds
[kind
].max_exponent
- 1);
4428 mpfr_check_range (result
->value
.real
, 0, GMP_RNDU
);
4430 if (mpfr_sgn (s
->value
.real
) > 0)
4432 mpfr_nextabove (result
->value
.real
);
4433 mpfr_subnormalize (result
->value
.real
, 0, GMP_RNDU
);
4437 mpfr_nextbelow (result
->value
.real
);
4438 mpfr_subnormalize (result
->value
.real
, 0, GMP_RNDD
);
4441 mpfr_set_emin (emin
);
4442 mpfr_set_emax (emax
);
4444 /* Only NaN can occur. Do not use range check as it gives an
4445 error for denormal numbers. */
4446 if (mpfr_nan_p (result
->value
.real
) && gfc_option
.flag_range_check
)
4448 gfc_error ("Result of NEAREST is NaN at %L", &result
->where
);
4449 gfc_free_expr (result
);
4450 return &gfc_bad_expr
;
4458 simplify_nint (const char *name
, gfc_expr
*e
, gfc_expr
*k
)
4460 gfc_expr
*itrunc
, *result
;
4463 kind
= get_kind (BT_INTEGER
, k
, name
, gfc_default_integer_kind
);
4465 return &gfc_bad_expr
;
4467 if (e
->expr_type
!= EXPR_CONSTANT
)
4470 itrunc
= gfc_copy_expr (e
);
4471 mpfr_round (itrunc
->value
.real
, e
->value
.real
);
4473 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &e
->where
);
4474 gfc_mpfr_to_mpz (result
->value
.integer
, itrunc
->value
.real
, &e
->where
);
4476 gfc_free_expr (itrunc
);
4478 return range_check (result
, name
);
4483 gfc_simplify_new_line (gfc_expr
*e
)
4487 result
= gfc_get_character_expr (e
->ts
.kind
, &e
->where
, NULL
, 1);
4488 result
->value
.character
.string
[0] = '\n';
4495 gfc_simplify_nint (gfc_expr
*e
, gfc_expr
*k
)
4497 return simplify_nint ("NINT", e
, k
);
4502 gfc_simplify_idnint (gfc_expr
*e
)
4504 return simplify_nint ("IDNINT", e
, NULL
);
4509 add_squared (gfc_expr
*result
, gfc_expr
*e
)
4513 gcc_assert (e
->ts
.type
== BT_REAL
&& e
->expr_type
== EXPR_CONSTANT
);
4514 gcc_assert (result
->ts
.type
== BT_REAL
4515 && result
->expr_type
== EXPR_CONSTANT
);
4517 gfc_set_model_kind (result
->ts
.kind
);
4519 mpfr_pow_ui (tmp
, e
->value
.real
, 2, GFC_RND_MODE
);
4520 mpfr_add (result
->value
.real
, result
->value
.real
, tmp
,
4529 do_sqrt (gfc_expr
*result
, gfc_expr
*e
)
4531 gcc_assert (e
->ts
.type
== BT_REAL
&& e
->expr_type
== EXPR_CONSTANT
);
4532 gcc_assert (result
->ts
.type
== BT_REAL
4533 && result
->expr_type
== EXPR_CONSTANT
);
4535 mpfr_set (result
->value
.real
, e
->value
.real
, GFC_RND_MODE
);
4536 mpfr_sqrt (result
->value
.real
, result
->value
.real
, GFC_RND_MODE
);
4542 gfc_simplify_norm2 (gfc_expr
*e
, gfc_expr
*dim
)
4546 if (!is_constant_array_expr (e
)
4547 || (dim
!= NULL
&& !gfc_is_constant_expr (dim
)))
4550 result
= transformational_result (e
, dim
, e
->ts
.type
, e
->ts
.kind
, &e
->where
);
4551 init_result_expr (result
, 0, NULL
);
4553 if (!dim
|| e
->rank
== 1)
4555 result
= simplify_transformation_to_scalar (result
, e
, NULL
,
4557 mpfr_sqrt (result
->value
.real
, result
->value
.real
, GFC_RND_MODE
);
4560 result
= simplify_transformation_to_array (result
, e
, dim
, NULL
,
4561 add_squared
, &do_sqrt
);
4568 gfc_simplify_not (gfc_expr
*e
)
4572 if (e
->expr_type
!= EXPR_CONSTANT
)
4575 result
= gfc_get_constant_expr (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
4576 mpz_com (result
->value
.integer
, e
->value
.integer
);
4578 return range_check (result
, "NOT");
4583 gfc_simplify_null (gfc_expr
*mold
)
4589 result
= gfc_copy_expr (mold
);
4590 result
->expr_type
= EXPR_NULL
;
4593 result
= gfc_get_null_expr (NULL
);
4600 gfc_simplify_num_images (void)
4604 if (gfc_option
.coarray
== GFC_FCOARRAY_NONE
)
4606 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
4607 return &gfc_bad_expr
;
4610 if (gfc_option
.coarray
!= GFC_FCOARRAY_SINGLE
)
4613 /* FIXME: gfc_current_locus is wrong. */
4614 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
4615 &gfc_current_locus
);
4616 mpz_set_si (result
->value
.integer
, 1);
4622 gfc_simplify_or (gfc_expr
*x
, gfc_expr
*y
)
4627 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
4630 kind
= x
->ts
.kind
> y
->ts
.kind
? x
->ts
.kind
: y
->ts
.kind
;
4635 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &x
->where
);
4636 mpz_ior (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
4637 return range_check (result
, "OR");
4640 return gfc_get_logical_expr (kind
, &x
->where
,
4641 x
->value
.logical
|| y
->value
.logical
);
4649 gfc_simplify_pack (gfc_expr
*array
, gfc_expr
*mask
, gfc_expr
*vector
)
4652 gfc_constructor
*array_ctor
, *mask_ctor
, *vector_ctor
;
4654 if (!is_constant_array_expr (array
)
4655 || !is_constant_array_expr (vector
)
4656 || (!gfc_is_constant_expr (mask
)
4657 && !is_constant_array_expr (mask
)))
4660 result
= gfc_get_array_expr (array
->ts
.type
, array
->ts
.kind
, &array
->where
);
4661 if (array
->ts
.type
== BT_DERIVED
)
4662 result
->ts
.u
.derived
= array
->ts
.u
.derived
;
4664 array_ctor
= gfc_constructor_first (array
->value
.constructor
);
4665 vector_ctor
= vector
4666 ? gfc_constructor_first (vector
->value
.constructor
)
4669 if (mask
->expr_type
== EXPR_CONSTANT
4670 && mask
->value
.logical
)
4672 /* Copy all elements of ARRAY to RESULT. */
4675 gfc_constructor_append_expr (&result
->value
.constructor
,
4676 gfc_copy_expr (array_ctor
->expr
),
4679 array_ctor
= gfc_constructor_next (array_ctor
);
4680 vector_ctor
= gfc_constructor_next (vector_ctor
);
4683 else if (mask
->expr_type
== EXPR_ARRAY
)
4685 /* Copy only those elements of ARRAY to RESULT whose
4686 MASK equals .TRUE.. */
4687 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
4690 if (mask_ctor
->expr
->value
.logical
)
4692 gfc_constructor_append_expr (&result
->value
.constructor
,
4693 gfc_copy_expr (array_ctor
->expr
),
4695 vector_ctor
= gfc_constructor_next (vector_ctor
);
4698 array_ctor
= gfc_constructor_next (array_ctor
);
4699 mask_ctor
= gfc_constructor_next (mask_ctor
);
4703 /* Append any left-over elements from VECTOR to RESULT. */
4706 gfc_constructor_append_expr (&result
->value
.constructor
,
4707 gfc_copy_expr (vector_ctor
->expr
),
4709 vector_ctor
= gfc_constructor_next (vector_ctor
);
4712 result
->shape
= gfc_get_shape (1);
4713 gfc_array_size (result
, &result
->shape
[0]);
4715 if (array
->ts
.type
== BT_CHARACTER
)
4716 result
->ts
.u
.cl
= array
->ts
.u
.cl
;
4723 do_xor (gfc_expr
*result
, gfc_expr
*e
)
4725 gcc_assert (e
->ts
.type
== BT_LOGICAL
&& e
->expr_type
== EXPR_CONSTANT
);
4726 gcc_assert (result
->ts
.type
== BT_LOGICAL
4727 && result
->expr_type
== EXPR_CONSTANT
);
4729 result
->value
.logical
= result
->value
.logical
!= e
->value
.logical
;
4736 gfc_simplify_parity (gfc_expr
*e
, gfc_expr
*dim
)
4738 return simplify_transformation (e
, dim
, NULL
, 0, do_xor
);
4743 gfc_simplify_popcnt (gfc_expr
*e
)
4748 if (e
->expr_type
!= EXPR_CONSTANT
)
4751 k
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
4753 /* Convert argument to unsigned, then count the '1' bits. */
4754 mpz_init_set (x
, e
->value
.integer
);
4755 convert_mpz_to_unsigned (x
, gfc_integer_kinds
[k
].bit_size
);
4756 res
= mpz_popcount (x
);
4759 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, res
);
4764 gfc_simplify_poppar (gfc_expr
*e
)
4770 if (e
->expr_type
!= EXPR_CONSTANT
)
4773 popcnt
= gfc_simplify_popcnt (e
);
4774 gcc_assert (popcnt
);
4776 s
= gfc_extract_int (popcnt
, &i
);
4779 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, i
% 2);
4784 gfc_simplify_precision (gfc_expr
*e
)
4786 int i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
4787 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
,
4788 gfc_real_kinds
[i
].precision
);
4793 gfc_simplify_product (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
)
4795 return simplify_transformation (array
, dim
, mask
, 1, gfc_multiply
);
4800 gfc_simplify_radix (gfc_expr
*e
)
4803 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
4808 i
= gfc_integer_kinds
[i
].radix
;
4812 i
= gfc_real_kinds
[i
].radix
;
4819 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, i
);
4824 gfc_simplify_range (gfc_expr
*e
)
4827 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
4832 i
= gfc_integer_kinds
[i
].range
;
4837 i
= gfc_real_kinds
[i
].range
;
4844 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, i
);
4849 gfc_simplify_rank (gfc_expr
*e
)
4855 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, e
->rank
);
4860 gfc_simplify_real (gfc_expr
*e
, gfc_expr
*k
)
4862 gfc_expr
*result
= NULL
;
4865 if (e
->ts
.type
== BT_COMPLEX
)
4866 kind
= get_kind (BT_REAL
, k
, "REAL", e
->ts
.kind
);
4868 kind
= get_kind (BT_REAL
, k
, "REAL", gfc_default_real_kind
);
4871 return &gfc_bad_expr
;
4873 if (e
->expr_type
!= EXPR_CONSTANT
)
4876 if (convert_boz (e
, kind
) == &gfc_bad_expr
)
4877 return &gfc_bad_expr
;
4879 result
= gfc_convert_constant (e
, BT_REAL
, kind
);
4880 if (result
== &gfc_bad_expr
)
4881 return &gfc_bad_expr
;
4883 return range_check (result
, "REAL");
4888 gfc_simplify_realpart (gfc_expr
*e
)
4892 if (e
->expr_type
!= EXPR_CONSTANT
)
4895 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
4896 mpc_real (result
->value
.real
, e
->value
.complex, GFC_RND_MODE
);
4898 return range_check (result
, "REALPART");
4902 gfc_simplify_repeat (gfc_expr
*e
, gfc_expr
*n
)
4905 int i
, j
, len
, ncop
, nlen
;
4907 bool have_length
= false;
4909 /* If NCOPIES isn't a constant, there's nothing we can do. */
4910 if (n
->expr_type
!= EXPR_CONSTANT
)
4913 /* If NCOPIES is negative, it's an error. */
4914 if (mpz_sgn (n
->value
.integer
) < 0)
4916 gfc_error ("Argument NCOPIES of REPEAT intrinsic is negative at %L",
4918 return &gfc_bad_expr
;
4921 /* If we don't know the character length, we can do no more. */
4922 if (e
->ts
.u
.cl
&& e
->ts
.u
.cl
->length
4923 && e
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
4925 len
= mpz_get_si (e
->ts
.u
.cl
->length
->value
.integer
);
4928 else if (e
->expr_type
== EXPR_CONSTANT
4929 && (e
->ts
.u
.cl
== NULL
|| e
->ts
.u
.cl
->length
== NULL
))
4931 len
= e
->value
.character
.length
;
4936 /* If the source length is 0, any value of NCOPIES is valid
4937 and everything behaves as if NCOPIES == 0. */
4940 mpz_set_ui (ncopies
, 0);
4942 mpz_set (ncopies
, n
->value
.integer
);
4944 /* Check that NCOPIES isn't too large. */
4950 /* Compute the maximum value allowed for NCOPIES: huge(cl) / len. */
4952 i
= gfc_validate_kind (BT_INTEGER
, gfc_charlen_int_kind
, false);
4956 mpz_tdiv_q (max
, gfc_integer_kinds
[i
].huge
,
4957 e
->ts
.u
.cl
->length
->value
.integer
);
4961 mpz_init_set_si (mlen
, len
);
4962 mpz_tdiv_q (max
, gfc_integer_kinds
[i
].huge
, mlen
);
4966 /* The check itself. */
4967 if (mpz_cmp (ncopies
, max
) > 0)
4970 mpz_clear (ncopies
);
4971 gfc_error ("Argument NCOPIES of REPEAT intrinsic is too large at %L",
4973 return &gfc_bad_expr
;
4978 mpz_clear (ncopies
);
4980 /* For further simplification, we need the character string to be
4982 if (e
->expr_type
!= EXPR_CONSTANT
)
4986 (e
->ts
.u
.cl
->length
&&
4987 mpz_sgn (e
->ts
.u
.cl
->length
->value
.integer
)) != 0)
4989 const char *res
= gfc_extract_int (n
, &ncop
);
4990 gcc_assert (res
== NULL
);
4996 return gfc_get_character_expr (e
->ts
.kind
, &e
->where
, NULL
, 0);
4998 len
= e
->value
.character
.length
;
5001 result
= gfc_get_character_expr (e
->ts
.kind
, &e
->where
, NULL
, nlen
);
5002 for (i
= 0; i
< ncop
; i
++)
5003 for (j
= 0; j
< len
; j
++)
5004 result
->value
.character
.string
[j
+i
*len
]= e
->value
.character
.string
[j
];
5006 result
->value
.character
.string
[nlen
] = '\0'; /* For debugger */
5011 /* This one is a bear, but mainly has to do with shuffling elements. */
5014 gfc_simplify_reshape (gfc_expr
*source
, gfc_expr
*shape_exp
,
5015 gfc_expr
*pad
, gfc_expr
*order_exp
)
5017 int order
[GFC_MAX_DIMENSIONS
], shape
[GFC_MAX_DIMENSIONS
];
5018 int i
, rank
, npad
, x
[GFC_MAX_DIMENSIONS
];
5022 gfc_expr
*e
, *result
;
5024 /* Check that argument expression types are OK. */
5025 if (!is_constant_array_expr (source
)
5026 || !is_constant_array_expr (shape_exp
)
5027 || !is_constant_array_expr (pad
)
5028 || !is_constant_array_expr (order_exp
))
5031 /* Proceed with simplification, unpacking the array. */
5038 e
= gfc_constructor_lookup_expr (shape_exp
->value
.constructor
, rank
);
5042 gfc_extract_int (e
, &shape
[rank
]);
5044 gcc_assert (rank
>= 0 && rank
< GFC_MAX_DIMENSIONS
);
5045 gcc_assert (shape
[rank
] >= 0);
5050 gcc_assert (rank
> 0);
5052 /* Now unpack the order array if present. */
5053 if (order_exp
== NULL
)
5055 for (i
= 0; i
< rank
; i
++)
5060 for (i
= 0; i
< rank
; i
++)
5063 for (i
= 0; i
< rank
; i
++)
5065 e
= gfc_constructor_lookup_expr (order_exp
->value
.constructor
, i
);
5068 gfc_extract_int (e
, &order
[i
]);
5070 gcc_assert (order
[i
] >= 1 && order
[i
] <= rank
);
5072 gcc_assert (x
[order
[i
]] == 0);
5077 /* Count the elements in the source and padding arrays. */
5082 gfc_array_size (pad
, &size
);
5083 npad
= mpz_get_ui (size
);
5087 gfc_array_size (source
, &size
);
5088 nsource
= mpz_get_ui (size
);
5091 /* If it weren't for that pesky permutation we could just loop
5092 through the source and round out any shortage with pad elements.
5093 But no, someone just had to have the compiler do something the
5094 user should be doing. */
5096 for (i
= 0; i
< rank
; i
++)
5099 result
= gfc_get_array_expr (source
->ts
.type
, source
->ts
.kind
,
5101 if (source
->ts
.type
== BT_DERIVED
)
5102 result
->ts
.u
.derived
= source
->ts
.u
.derived
;
5103 result
->rank
= rank
;
5104 result
->shape
= gfc_get_shape (rank
);
5105 for (i
= 0; i
< rank
; i
++)
5106 mpz_init_set_ui (result
->shape
[i
], shape
[i
]);
5108 while (nsource
> 0 || npad
> 0)
5110 /* Figure out which element to extract. */
5111 mpz_set_ui (index
, 0);
5113 for (i
= rank
- 1; i
>= 0; i
--)
5115 mpz_add_ui (index
, index
, x
[order
[i
]]);
5117 mpz_mul_ui (index
, index
, shape
[order
[i
- 1]]);
5120 if (mpz_cmp_ui (index
, INT_MAX
) > 0)
5121 gfc_internal_error ("Reshaped array too large at %C");
5123 j
= mpz_get_ui (index
);
5126 e
= gfc_constructor_lookup_expr (source
->value
.constructor
, j
);
5129 gcc_assert (npad
> 0);
5133 e
= gfc_constructor_lookup_expr (pad
->value
.constructor
, j
);
5137 gfc_constructor_append_expr (&result
->value
.constructor
,
5138 gfc_copy_expr (e
), &e
->where
);
5140 /* Calculate the next element. */
5144 if (++x
[i
] < shape
[i
])
5160 gfc_simplify_rrspacing (gfc_expr
*x
)
5166 if (x
->expr_type
!= EXPR_CONSTANT
)
5169 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
5171 result
= gfc_get_constant_expr (BT_REAL
, x
->ts
.kind
, &x
->where
);
5172 mpfr_abs (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
5174 /* Special case x = -0 and 0. */
5175 if (mpfr_sgn (result
->value
.real
) == 0)
5177 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
5181 /* | x * 2**(-e) | * 2**p. */
5182 e
= - (long int) mpfr_get_exp (x
->value
.real
);
5183 mpfr_mul_2si (result
->value
.real
, result
->value
.real
, e
, GFC_RND_MODE
);
5185 p
= (long int) gfc_real_kinds
[i
].digits
;
5186 mpfr_mul_2si (result
->value
.real
, result
->value
.real
, p
, GFC_RND_MODE
);
5188 return range_check (result
, "RRSPACING");
5193 gfc_simplify_scale (gfc_expr
*x
, gfc_expr
*i
)
5195 int k
, neg_flag
, power
, exp_range
;
5196 mpfr_t scale
, radix
;
5199 if (x
->expr_type
!= EXPR_CONSTANT
|| i
->expr_type
!= EXPR_CONSTANT
)
5202 result
= gfc_get_constant_expr (BT_REAL
, x
->ts
.kind
, &x
->where
);
5204 if (mpfr_sgn (x
->value
.real
) == 0)
5206 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
5210 k
= gfc_validate_kind (BT_REAL
, x
->ts
.kind
, false);
5212 exp_range
= gfc_real_kinds
[k
].max_exponent
- gfc_real_kinds
[k
].min_exponent
;
5214 /* This check filters out values of i that would overflow an int. */
5215 if (mpz_cmp_si (i
->value
.integer
, exp_range
+ 2) > 0
5216 || mpz_cmp_si (i
->value
.integer
, -exp_range
- 2) < 0)
5218 gfc_error ("Result of SCALE overflows its kind at %L", &result
->where
);
5219 gfc_free_expr (result
);
5220 return &gfc_bad_expr
;
5223 /* Compute scale = radix ** power. */
5224 power
= mpz_get_si (i
->value
.integer
);
5234 gfc_set_model_kind (x
->ts
.kind
);
5237 mpfr_set_ui (radix
, gfc_real_kinds
[k
].radix
, GFC_RND_MODE
);
5238 mpfr_pow_ui (scale
, radix
, power
, GFC_RND_MODE
);
5241 mpfr_div (result
->value
.real
, x
->value
.real
, scale
, GFC_RND_MODE
);
5243 mpfr_mul (result
->value
.real
, x
->value
.real
, scale
, GFC_RND_MODE
);
5245 mpfr_clears (scale
, radix
, NULL
);
5247 return range_check (result
, "SCALE");
5251 /* Variants of strspn and strcspn that operate on wide characters. */
5254 wide_strspn (const gfc_char_t
*s1
, const gfc_char_t
*s2
)
5257 const gfc_char_t
*c
;
5261 for (c
= s2
; *c
; c
++)
5275 wide_strcspn (const gfc_char_t
*s1
, const gfc_char_t
*s2
)
5278 const gfc_char_t
*c
;
5282 for (c
= s2
; *c
; c
++)
5297 gfc_simplify_scan (gfc_expr
*e
, gfc_expr
*c
, gfc_expr
*b
, gfc_expr
*kind
)
5302 size_t indx
, len
, lenc
;
5303 int k
= get_kind (BT_INTEGER
, kind
, "SCAN", gfc_default_integer_kind
);
5306 return &gfc_bad_expr
;
5308 if (e
->expr_type
!= EXPR_CONSTANT
|| c
->expr_type
!= EXPR_CONSTANT
5309 || ( b
!= NULL
&& b
->expr_type
!= EXPR_CONSTANT
))
5312 if (b
!= NULL
&& b
->value
.logical
!= 0)
5317 len
= e
->value
.character
.length
;
5318 lenc
= c
->value
.character
.length
;
5320 if (len
== 0 || lenc
== 0)
5328 indx
= wide_strcspn (e
->value
.character
.string
,
5329 c
->value
.character
.string
) + 1;
5336 for (indx
= len
; indx
> 0; indx
--)
5338 for (i
= 0; i
< lenc
; i
++)
5340 if (c
->value
.character
.string
[i
]
5341 == e
->value
.character
.string
[indx
- 1])
5350 result
= gfc_get_int_expr (k
, &e
->where
, indx
);
5351 return range_check (result
, "SCAN");
5356 gfc_simplify_selected_char_kind (gfc_expr
*e
)
5360 if (e
->expr_type
!= EXPR_CONSTANT
)
5363 if (gfc_compare_with_Cstring (e
, "ascii", false) == 0
5364 || gfc_compare_with_Cstring (e
, "default", false) == 0)
5366 else if (gfc_compare_with_Cstring (e
, "iso_10646", false) == 0)
5371 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, kind
);
5376 gfc_simplify_selected_int_kind (gfc_expr
*e
)
5380 if (e
->expr_type
!= EXPR_CONSTANT
|| gfc_extract_int (e
, &range
) != NULL
)
5385 for (i
= 0; gfc_integer_kinds
[i
].kind
!= 0; i
++)
5386 if (gfc_integer_kinds
[i
].range
>= range
5387 && gfc_integer_kinds
[i
].kind
< kind
)
5388 kind
= gfc_integer_kinds
[i
].kind
;
5390 if (kind
== INT_MAX
)
5393 return gfc_get_int_expr (gfc_default_integer_kind
, &e
->where
, kind
);
5398 gfc_simplify_selected_real_kind (gfc_expr
*p
, gfc_expr
*q
, gfc_expr
*rdx
)
5400 int range
, precision
, radix
, i
, kind
, found_precision
, found_range
,
5402 locus
*loc
= &gfc_current_locus
;
5408 if (p
->expr_type
!= EXPR_CONSTANT
5409 || gfc_extract_int (p
, &precision
) != NULL
)
5418 if (q
->expr_type
!= EXPR_CONSTANT
5419 || gfc_extract_int (q
, &range
) != NULL
)
5430 if (rdx
->expr_type
!= EXPR_CONSTANT
5431 || gfc_extract_int (rdx
, &radix
) != NULL
)
5439 found_precision
= 0;
5443 for (i
= 0; gfc_real_kinds
[i
].kind
!= 0; i
++)
5445 if (gfc_real_kinds
[i
].precision
>= precision
)
5446 found_precision
= 1;
5448 if (gfc_real_kinds
[i
].range
>= range
)
5451 if (gfc_real_kinds
[i
].radix
>= radix
)
5454 if (gfc_real_kinds
[i
].precision
>= precision
5455 && gfc_real_kinds
[i
].range
>= range
5456 && gfc_real_kinds
[i
].radix
>= radix
&& gfc_real_kinds
[i
].kind
< kind
)
5457 kind
= gfc_real_kinds
[i
].kind
;
5460 if (kind
== INT_MAX
)
5462 if (found_radix
&& found_range
&& !found_precision
)
5464 else if (found_radix
&& found_precision
&& !found_range
)
5466 else if (found_radix
&& !found_precision
&& !found_range
)
5468 else if (found_radix
)
5474 return gfc_get_int_expr (gfc_default_integer_kind
, loc
, kind
);
5479 gfc_simplify_set_exponent (gfc_expr
*x
, gfc_expr
*i
)
5482 mpfr_t exp
, absv
, log2
, pow2
, frac
;
5485 if (x
->expr_type
!= EXPR_CONSTANT
|| i
->expr_type
!= EXPR_CONSTANT
)
5488 result
= gfc_get_constant_expr (BT_REAL
, x
->ts
.kind
, &x
->where
);
5490 if (mpfr_sgn (x
->value
.real
) == 0)
5492 mpfr_set_ui (result
->value
.real
, 0, GFC_RND_MODE
);
5496 gfc_set_model_kind (x
->ts
.kind
);
5503 mpfr_abs (absv
, x
->value
.real
, GFC_RND_MODE
);
5504 mpfr_log2 (log2
, absv
, GFC_RND_MODE
);
5506 mpfr_trunc (log2
, log2
);
5507 mpfr_add_ui (exp
, log2
, 1, GFC_RND_MODE
);
5509 /* Old exponent value, and fraction. */
5510 mpfr_ui_pow (pow2
, 2, exp
, GFC_RND_MODE
);
5512 mpfr_div (frac
, absv
, pow2
, GFC_RND_MODE
);
5515 exp2
= (unsigned long) mpz_get_d (i
->value
.integer
);
5516 mpfr_mul_2exp (result
->value
.real
, frac
, exp2
, GFC_RND_MODE
);
5518 mpfr_clears (absv
, log2
, pow2
, frac
, NULL
);
5520 return range_check (result
, "SET_EXPONENT");
5525 gfc_simplify_shape (gfc_expr
*source
, gfc_expr
*kind
)
5527 mpz_t shape
[GFC_MAX_DIMENSIONS
];
5528 gfc_expr
*result
, *e
, *f
;
5532 int k
= get_kind (BT_INTEGER
, kind
, "SHAPE", gfc_default_integer_kind
);
5534 if (source
->rank
== -1)
5537 result
= gfc_get_array_expr (BT_INTEGER
, k
, &source
->where
);
5539 if (source
->rank
== 0)
5542 if (source
->expr_type
== EXPR_VARIABLE
)
5544 ar
= gfc_find_array_ref (source
);
5545 t
= gfc_array_ref_shape (ar
, shape
);
5547 else if (source
->shape
)
5550 for (n
= 0; n
< source
->rank
; n
++)
5552 mpz_init (shape
[n
]);
5553 mpz_set (shape
[n
], source
->shape
[n
]);
5559 for (n
= 0; n
< source
->rank
; n
++)
5561 e
= gfc_get_constant_expr (BT_INTEGER
, k
, &source
->where
);
5564 mpz_set (e
->value
.integer
, shape
[n
]);
5567 mpz_set_ui (e
->value
.integer
, n
+ 1);
5569 f
= simplify_size (source
, e
, k
);
5573 gfc_free_expr (result
);
5580 if (e
== &gfc_bad_expr
|| range_check (e
, "SHAPE") == &gfc_bad_expr
)
5582 gfc_free_expr (result
);
5584 gfc_clear_shape (shape
, source
->rank
);
5585 return &gfc_bad_expr
;
5588 gfc_constructor_append_expr (&result
->value
.constructor
, e
, NULL
);
5592 gfc_clear_shape (shape
, source
->rank
);
5599 simplify_size (gfc_expr
*array
, gfc_expr
*dim
, int k
)
5602 gfc_expr
*return_value
;
5605 /* For unary operations, the size of the result is given by the size
5606 of the operand. For binary ones, it's the size of the first operand
5607 unless it is scalar, then it is the size of the second. */
5608 if (array
->expr_type
== EXPR_OP
&& !array
->value
.op
.uop
)
5610 gfc_expr
* replacement
;
5611 gfc_expr
* simplified
;
5613 switch (array
->value
.op
.op
)
5615 /* Unary operations. */
5617 case INTRINSIC_UPLUS
:
5618 case INTRINSIC_UMINUS
:
5619 case INTRINSIC_PARENTHESES
:
5620 replacement
= array
->value
.op
.op1
;
5623 /* Binary operations. If any one of the operands is scalar, take
5624 the other one's size. If both of them are arrays, it does not
5625 matter -- try to find one with known shape, if possible. */
5627 if (array
->value
.op
.op1
->rank
== 0)
5628 replacement
= array
->value
.op
.op2
;
5629 else if (array
->value
.op
.op2
->rank
== 0)
5630 replacement
= array
->value
.op
.op1
;
5633 simplified
= simplify_size (array
->value
.op
.op1
, dim
, k
);
5637 replacement
= array
->value
.op
.op2
;
5642 /* Try to reduce it directly if possible. */
5643 simplified
= simplify_size (replacement
, dim
, k
);
5645 /* Otherwise, we build a new SIZE call. This is hopefully at least
5646 simpler than the original one. */
5649 gfc_expr
*kind
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, k
);
5650 simplified
= gfc_build_intrinsic_call (gfc_current_ns
,
5651 GFC_ISYM_SIZE
, "size",
5653 gfc_copy_expr (replacement
),
5654 gfc_copy_expr (dim
),
5662 if (!gfc_array_size (array
, &size
))
5667 if (dim
->expr_type
!= EXPR_CONSTANT
)
5670 d
= mpz_get_ui (dim
->value
.integer
) - 1;
5671 if (!gfc_array_dimen_size (array
, d
, &size
))
5675 return_value
= gfc_get_constant_expr (BT_INTEGER
, k
, &array
->where
);
5676 mpz_set (return_value
->value
.integer
, size
);
5679 return return_value
;
5684 gfc_simplify_size (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
)
5687 int k
= get_kind (BT_INTEGER
, kind
, "SIZE", gfc_default_integer_kind
);
5690 return &gfc_bad_expr
;
5692 result
= simplify_size (array
, dim
, k
);
5693 if (result
== NULL
|| result
== &gfc_bad_expr
)
5696 return range_check (result
, "SIZE");
5700 /* SIZEOF and C_SIZEOF return the size in bytes of an array element
5701 multiplied by the array size. */
5704 gfc_simplify_sizeof (gfc_expr
*x
)
5706 gfc_expr
*result
= NULL
;
5709 if (x
->ts
.type
== BT_CLASS
|| x
->ts
.deferred
)
5712 if (x
->ts
.type
== BT_CHARACTER
5713 && (!x
->ts
.u
.cl
|| !x
->ts
.u
.cl
->length
5714 || x
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
))
5717 if (x
->rank
&& x
->expr_type
!= EXPR_ARRAY
5718 && !gfc_array_size (x
, &array_size
))
5721 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_index_integer_kind
,
5723 mpz_set_si (result
->value
.integer
, gfc_target_expr_size (x
));
5729 /* STORAGE_SIZE returns the size in bits of a single array element. */
5732 gfc_simplify_storage_size (gfc_expr
*x
,
5735 gfc_expr
*result
= NULL
;
5738 if (x
->ts
.type
== BT_CLASS
|| x
->ts
.deferred
)
5741 if (x
->ts
.type
== BT_CHARACTER
&& x
->expr_type
!= EXPR_CONSTANT
5742 && (!x
->ts
.u
.cl
|| !x
->ts
.u
.cl
->length
5743 || x
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
))
5746 k
= get_kind (BT_INTEGER
, kind
, "STORAGE_SIZE", gfc_default_integer_kind
);
5748 return &gfc_bad_expr
;
5750 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_index_integer_kind
,
5753 mpz_set_si (result
->value
.integer
, gfc_element_size (x
));
5755 mpz_mul_ui (result
->value
.integer
, result
->value
.integer
, BITS_PER_UNIT
);
5757 return range_check (result
, "STORAGE_SIZE");
5762 gfc_simplify_sign (gfc_expr
*x
, gfc_expr
*y
)
5766 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
5769 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
5774 mpz_abs (result
->value
.integer
, x
->value
.integer
);
5775 if (mpz_sgn (y
->value
.integer
) < 0)
5776 mpz_neg (result
->value
.integer
, result
->value
.integer
);
5780 if (gfc_option
.flag_sign_zero
)
5781 mpfr_copysign (result
->value
.real
, x
->value
.real
, y
->value
.real
,
5784 mpfr_setsign (result
->value
.real
, x
->value
.real
,
5785 mpfr_sgn (y
->value
.real
) < 0 ? 1 : 0, GFC_RND_MODE
);
5789 gfc_internal_error ("Bad type in gfc_simplify_sign");
5797 gfc_simplify_sin (gfc_expr
*x
)
5801 if (x
->expr_type
!= EXPR_CONSTANT
)
5804 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
5809 mpfr_sin (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
5813 gfc_set_model (x
->value
.real
);
5814 mpc_sin (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
5818 gfc_internal_error ("in gfc_simplify_sin(): Bad type");
5821 return range_check (result
, "SIN");
5826 gfc_simplify_sinh (gfc_expr
*x
)
5830 if (x
->expr_type
!= EXPR_CONSTANT
)
5833 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
5838 mpfr_sinh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
5842 mpc_sinh (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
5849 return range_check (result
, "SINH");
5853 /* The argument is always a double precision real that is converted to
5854 single precision. TODO: Rounding! */
5857 gfc_simplify_sngl (gfc_expr
*a
)
5861 if (a
->expr_type
!= EXPR_CONSTANT
)
5864 result
= gfc_real2real (a
, gfc_default_real_kind
);
5865 return range_check (result
, "SNGL");
5870 gfc_simplify_spacing (gfc_expr
*x
)
5876 if (x
->expr_type
!= EXPR_CONSTANT
)
5879 i
= gfc_validate_kind (x
->ts
.type
, x
->ts
.kind
, false);
5881 result
= gfc_get_constant_expr (BT_REAL
, x
->ts
.kind
, &x
->where
);
5883 /* Special case x = 0 and -0. */
5884 mpfr_abs (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
5885 if (mpfr_sgn (result
->value
.real
) == 0)
5887 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].tiny
, GFC_RND_MODE
);
5891 /* In the Fortran 95 standard, the result is b**(e - p) where b, e, and p
5892 are the radix, exponent of x, and precision. This excludes the
5893 possibility of subnormal numbers. Fortran 2003 states the result is
5894 b**max(e - p, emin - 1). */
5896 ep
= (long int) mpfr_get_exp (x
->value
.real
) - gfc_real_kinds
[i
].digits
;
5897 en
= (long int) gfc_real_kinds
[i
].min_exponent
- 1;
5898 en
= en
> ep
? en
: ep
;
5900 mpfr_set_ui (result
->value
.real
, 1, GFC_RND_MODE
);
5901 mpfr_mul_2si (result
->value
.real
, result
->value
.real
, en
, GFC_RND_MODE
);
5903 return range_check (result
, "SPACING");
5908 gfc_simplify_spread (gfc_expr
*source
, gfc_expr
*dim_expr
, gfc_expr
*ncopies_expr
)
5910 gfc_expr
*result
= 0L;
5911 int i
, j
, dim
, ncopies
;
5914 if ((!gfc_is_constant_expr (source
)
5915 && !is_constant_array_expr (source
))
5916 || !gfc_is_constant_expr (dim_expr
)
5917 || !gfc_is_constant_expr (ncopies_expr
))
5920 gcc_assert (dim_expr
->ts
.type
== BT_INTEGER
);
5921 gfc_extract_int (dim_expr
, &dim
);
5922 dim
-= 1; /* zero-base DIM */
5924 gcc_assert (ncopies_expr
->ts
.type
== BT_INTEGER
);
5925 gfc_extract_int (ncopies_expr
, &ncopies
);
5926 ncopies
= MAX (ncopies
, 0);
5928 /* Do not allow the array size to exceed the limit for an array
5930 if (source
->expr_type
== EXPR_ARRAY
)
5932 if (!gfc_array_size (source
, &size
))
5933 gfc_internal_error ("Failure getting length of a constant array.");
5936 mpz_init_set_ui (size
, 1);
5938 if (mpz_get_si (size
)*ncopies
> gfc_option
.flag_max_array_constructor
)
5941 if (source
->expr_type
== EXPR_CONSTANT
)
5943 gcc_assert (dim
== 0);
5945 result
= gfc_get_array_expr (source
->ts
.type
, source
->ts
.kind
,
5947 if (source
->ts
.type
== BT_DERIVED
)
5948 result
->ts
.u
.derived
= source
->ts
.u
.derived
;
5950 result
->shape
= gfc_get_shape (result
->rank
);
5951 mpz_init_set_si (result
->shape
[0], ncopies
);
5953 for (i
= 0; i
< ncopies
; ++i
)
5954 gfc_constructor_append_expr (&result
->value
.constructor
,
5955 gfc_copy_expr (source
), NULL
);
5957 else if (source
->expr_type
== EXPR_ARRAY
)
5959 int offset
, rstride
[GFC_MAX_DIMENSIONS
], extent
[GFC_MAX_DIMENSIONS
];
5960 gfc_constructor
*source_ctor
;
5962 gcc_assert (source
->rank
< GFC_MAX_DIMENSIONS
);
5963 gcc_assert (dim
>= 0 && dim
<= source
->rank
);
5965 result
= gfc_get_array_expr (source
->ts
.type
, source
->ts
.kind
,
5967 if (source
->ts
.type
== BT_DERIVED
)
5968 result
->ts
.u
.derived
= source
->ts
.u
.derived
;
5969 result
->rank
= source
->rank
+ 1;
5970 result
->shape
= gfc_get_shape (result
->rank
);
5972 for (i
= 0, j
= 0; i
< result
->rank
; ++i
)
5975 mpz_init_set (result
->shape
[i
], source
->shape
[j
++]);
5977 mpz_init_set_si (result
->shape
[i
], ncopies
);
5979 extent
[i
] = mpz_get_si (result
->shape
[i
]);
5980 rstride
[i
] = (i
== 0) ? 1 : rstride
[i
-1] * extent
[i
-1];
5984 for (source_ctor
= gfc_constructor_first (source
->value
.constructor
);
5985 source_ctor
; source_ctor
= gfc_constructor_next (source_ctor
))
5987 for (i
= 0; i
< ncopies
; ++i
)
5988 gfc_constructor_insert_expr (&result
->value
.constructor
,
5989 gfc_copy_expr (source_ctor
->expr
),
5990 NULL
, offset
+ i
* rstride
[dim
]);
5992 offset
+= (dim
== 0 ? ncopies
: 1);
5996 /* FIXME: Returning here avoids a regression in array_simplify_1.f90.
5997 Replace NULL with gcc_unreachable() after implementing
5998 gfc_simplify_cshift(). */
6001 if (source
->ts
.type
== BT_CHARACTER
)
6002 result
->ts
.u
.cl
= source
->ts
.u
.cl
;
6009 gfc_simplify_sqrt (gfc_expr
*e
)
6011 gfc_expr
*result
= NULL
;
6013 if (e
->expr_type
!= EXPR_CONSTANT
)
6019 if (mpfr_cmp_si (e
->value
.real
, 0) < 0)
6021 gfc_error ("Argument of SQRT at %L has a negative value",
6023 return &gfc_bad_expr
;
6025 result
= gfc_get_constant_expr (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
6026 mpfr_sqrt (result
->value
.real
, e
->value
.real
, GFC_RND_MODE
);
6030 gfc_set_model (e
->value
.real
);
6032 result
= gfc_get_constant_expr (e
->ts
.type
, e
->ts
.kind
, &e
->where
);
6033 mpc_sqrt (result
->value
.complex, e
->value
.complex, GFC_MPC_RND_MODE
);
6037 gfc_internal_error ("invalid argument of SQRT at %L", &e
->where
);
6040 return range_check (result
, "SQRT");
6045 gfc_simplify_sum (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*mask
)
6047 return simplify_transformation (array
, dim
, mask
, 0, gfc_add
);
6052 gfc_simplify_tan (gfc_expr
*x
)
6056 if (x
->expr_type
!= EXPR_CONSTANT
)
6059 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
6064 mpfr_tan (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
6068 mpc_tan (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
6075 return range_check (result
, "TAN");
6080 gfc_simplify_tanh (gfc_expr
*x
)
6084 if (x
->expr_type
!= EXPR_CONSTANT
)
6087 result
= gfc_get_constant_expr (x
->ts
.type
, x
->ts
.kind
, &x
->where
);
6092 mpfr_tanh (result
->value
.real
, x
->value
.real
, GFC_RND_MODE
);
6096 mpc_tanh (result
->value
.complex, x
->value
.complex, GFC_MPC_RND_MODE
);
6103 return range_check (result
, "TANH");
6108 gfc_simplify_tiny (gfc_expr
*e
)
6113 i
= gfc_validate_kind (BT_REAL
, e
->ts
.kind
, false);
6115 result
= gfc_get_constant_expr (BT_REAL
, e
->ts
.kind
, &e
->where
);
6116 mpfr_set (result
->value
.real
, gfc_real_kinds
[i
].tiny
, GFC_RND_MODE
);
6123 gfc_simplify_trailz (gfc_expr
*e
)
6125 unsigned long tz
, bs
;
6128 if (e
->expr_type
!= EXPR_CONSTANT
)
6131 i
= gfc_validate_kind (e
->ts
.type
, e
->ts
.kind
, false);
6132 bs
= gfc_integer_kinds
[i
].bit_size
;
6133 tz
= mpz_scan1 (e
->value
.integer
, 0);
6135 return gfc_get_int_expr (gfc_default_integer_kind
,
6136 &e
->where
, MIN (tz
, bs
));
6141 gfc_simplify_transfer (gfc_expr
*source
, gfc_expr
*mold
, gfc_expr
*size
)
6144 gfc_expr
*mold_element
;
6149 unsigned char *buffer
;
6150 size_t result_length
;
6153 if (!gfc_is_constant_expr (source
)
6154 || (gfc_init_expr_flag
&& !gfc_is_constant_expr (mold
))
6155 || !gfc_is_constant_expr (size
))
6158 if (!gfc_calculate_transfer_sizes (source
, mold
, size
, &source_size
,
6159 &result_size
, &result_length
))
6162 /* Calculate the size of the source. */
6163 if (source
->expr_type
== EXPR_ARRAY
6164 && !gfc_array_size (source
, &tmp
))
6165 gfc_internal_error ("Failure getting length of a constant array.");
6167 /* Create an empty new expression with the appropriate characteristics. */
6168 result
= gfc_get_constant_expr (mold
->ts
.type
, mold
->ts
.kind
,
6170 result
->ts
= mold
->ts
;
6172 mold_element
= mold
->expr_type
== EXPR_ARRAY
6173 ? gfc_constructor_first (mold
->value
.constructor
)->expr
6176 /* Set result character length, if needed. Note that this needs to be
6177 set even for array expressions, in order to pass this information into
6178 gfc_target_interpret_expr. */
6179 if (result
->ts
.type
== BT_CHARACTER
&& gfc_is_constant_expr (mold_element
))
6180 result
->value
.character
.length
= mold_element
->value
.character
.length
;
6182 /* Set the number of elements in the result, and determine its size. */
6184 if (mold
->expr_type
== EXPR_ARRAY
|| mold
->rank
|| size
)
6186 result
->expr_type
= EXPR_ARRAY
;
6188 result
->shape
= gfc_get_shape (1);
6189 mpz_init_set_ui (result
->shape
[0], result_length
);
6194 /* Allocate the buffer to store the binary version of the source. */
6195 buffer_size
= MAX (source_size
, result_size
);
6196 buffer
= (unsigned char*)alloca (buffer_size
);
6197 memset (buffer
, 0, buffer_size
);
6199 /* Now write source to the buffer. */
6200 gfc_target_encode_expr (source
, buffer
, buffer_size
);
6202 /* And read the buffer back into the new expression. */
6203 gfc_target_interpret_expr (buffer
, buffer_size
, result
, false);
6210 gfc_simplify_transpose (gfc_expr
*matrix
)
6212 int row
, matrix_rows
, col
, matrix_cols
;
6215 if (!is_constant_array_expr (matrix
))
6218 gcc_assert (matrix
->rank
== 2);
6220 result
= gfc_get_array_expr (matrix
->ts
.type
, matrix
->ts
.kind
,
6223 result
->shape
= gfc_get_shape (result
->rank
);
6224 mpz_set (result
->shape
[0], matrix
->shape
[1]);
6225 mpz_set (result
->shape
[1], matrix
->shape
[0]);
6227 if (matrix
->ts
.type
== BT_CHARACTER
)
6228 result
->ts
.u
.cl
= matrix
->ts
.u
.cl
;
6229 else if (matrix
->ts
.type
== BT_DERIVED
)
6230 result
->ts
.u
.derived
= matrix
->ts
.u
.derived
;
6232 matrix_rows
= mpz_get_si (matrix
->shape
[0]);
6233 matrix_cols
= mpz_get_si (matrix
->shape
[1]);
6234 for (row
= 0; row
< matrix_rows
; ++row
)
6235 for (col
= 0; col
< matrix_cols
; ++col
)
6237 gfc_expr
*e
= gfc_constructor_lookup_expr (matrix
->value
.constructor
,
6238 col
* matrix_rows
+ row
);
6239 gfc_constructor_insert_expr (&result
->value
.constructor
,
6240 gfc_copy_expr (e
), &matrix
->where
,
6241 row
* matrix_cols
+ col
);
6249 gfc_simplify_trim (gfc_expr
*e
)
6252 int count
, i
, len
, lentrim
;
6254 if (e
->expr_type
!= EXPR_CONSTANT
)
6257 len
= e
->value
.character
.length
;
6258 for (count
= 0, i
= 1; i
<= len
; ++i
)
6260 if (e
->value
.character
.string
[len
- i
] == ' ')
6266 lentrim
= len
- count
;
6268 result
= gfc_get_character_expr (e
->ts
.kind
, &e
->where
, NULL
, lentrim
);
6269 for (i
= 0; i
< lentrim
; i
++)
6270 result
->value
.character
.string
[i
] = e
->value
.character
.string
[i
];
6277 gfc_simplify_image_index (gfc_expr
*coarray
, gfc_expr
*sub
)
6282 gfc_constructor
*sub_cons
;
6286 if (!is_constant_array_expr (sub
))
6289 /* Follow any component references. */
6290 as
= coarray
->symtree
->n
.sym
->as
;
6291 for (ref
= coarray
->ref
; ref
; ref
= ref
->next
)
6292 if (ref
->type
== REF_COMPONENT
)
6295 if (as
->type
== AS_DEFERRED
)
6298 /* "valid sequence of cosubscripts" are required; thus, return 0 unless
6299 the cosubscript addresses the first image. */
6301 sub_cons
= gfc_constructor_first (sub
->value
.constructor
);
6304 for (d
= 1; d
<= as
->corank
; d
++)
6309 gcc_assert (sub_cons
!= NULL
);
6311 ca_bound
= simplify_bound_dim (coarray
, NULL
, d
+ as
->rank
, 0, as
,
6313 if (ca_bound
== NULL
)
6316 if (ca_bound
== &gfc_bad_expr
)
6319 cmp
= mpz_cmp (ca_bound
->value
.integer
, sub_cons
->expr
->value
.integer
);
6323 gfc_free_expr (ca_bound
);
6324 sub_cons
= gfc_constructor_next (sub_cons
);
6328 first_image
= false;
6332 gfc_error ("Out of bounds in IMAGE_INDEX at %L for dimension %d, "
6333 "SUB has %ld and COARRAY lower bound is %ld)",
6335 mpz_get_si (sub_cons
->expr
->value
.integer
),
6336 mpz_get_si (ca_bound
->value
.integer
));
6337 gfc_free_expr (ca_bound
);
6338 return &gfc_bad_expr
;
6341 gfc_free_expr (ca_bound
);
6343 /* Check whether upperbound is valid for the multi-images case. */
6346 ca_bound
= simplify_bound_dim (coarray
, NULL
, d
+ as
->rank
, 1, as
,
6348 if (ca_bound
== &gfc_bad_expr
)
6351 if (ca_bound
&& ca_bound
->expr_type
== EXPR_CONSTANT
6352 && mpz_cmp (ca_bound
->value
.integer
,
6353 sub_cons
->expr
->value
.integer
) < 0)
6355 gfc_error ("Out of bounds in IMAGE_INDEX at %L for dimension %d, "
6356 "SUB has %ld and COARRAY upper bound is %ld)",
6358 mpz_get_si (sub_cons
->expr
->value
.integer
),
6359 mpz_get_si (ca_bound
->value
.integer
));
6360 gfc_free_expr (ca_bound
);
6361 return &gfc_bad_expr
;
6365 gfc_free_expr (ca_bound
);
6368 sub_cons
= gfc_constructor_next (sub_cons
);
6371 gcc_assert (sub_cons
== NULL
);
6373 if (gfc_option
.coarray
!= GFC_FCOARRAY_SINGLE
&& !first_image
)
6376 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
6377 &gfc_current_locus
);
6379 mpz_set_si (result
->value
.integer
, 1);
6381 mpz_set_si (result
->value
.integer
, 0);
6388 gfc_simplify_this_image (gfc_expr
*coarray
, gfc_expr
*dim
)
6390 if (gfc_option
.coarray
!= GFC_FCOARRAY_SINGLE
)
6393 if (coarray
== NULL
)
6396 /* FIXME: gfc_current_locus is wrong. */
6397 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_default_integer_kind
,
6398 &gfc_current_locus
);
6399 mpz_set_si (result
->value
.integer
, 1);
6403 /* For -fcoarray=single, this_image(A) is the same as lcobound(A). */
6404 return simplify_cobound (coarray
, dim
, NULL
, 0);
6409 gfc_simplify_ubound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
)
6411 return simplify_bound (array
, dim
, kind
, 1);
6415 gfc_simplify_ucobound (gfc_expr
*array
, gfc_expr
*dim
, gfc_expr
*kind
)
6417 return simplify_cobound (array
, dim
, kind
, 1);
6422 gfc_simplify_unpack (gfc_expr
*vector
, gfc_expr
*mask
, gfc_expr
*field
)
6424 gfc_expr
*result
, *e
;
6425 gfc_constructor
*vector_ctor
, *mask_ctor
, *field_ctor
;
6427 if (!is_constant_array_expr (vector
)
6428 || !is_constant_array_expr (mask
)
6429 || (!gfc_is_constant_expr (field
)
6430 && !is_constant_array_expr (field
)))
6433 result
= gfc_get_array_expr (vector
->ts
.type
, vector
->ts
.kind
,
6435 if (vector
->ts
.type
== BT_DERIVED
)
6436 result
->ts
.u
.derived
= vector
->ts
.u
.derived
;
6437 result
->rank
= mask
->rank
;
6438 result
->shape
= gfc_copy_shape (mask
->shape
, mask
->rank
);
6440 if (vector
->ts
.type
== BT_CHARACTER
)
6441 result
->ts
.u
.cl
= vector
->ts
.u
.cl
;
6443 vector_ctor
= gfc_constructor_first (vector
->value
.constructor
);
6444 mask_ctor
= gfc_constructor_first (mask
->value
.constructor
);
6446 = field
->expr_type
== EXPR_ARRAY
6447 ? gfc_constructor_first (field
->value
.constructor
)
6452 if (mask_ctor
->expr
->value
.logical
)
6454 gcc_assert (vector_ctor
);
6455 e
= gfc_copy_expr (vector_ctor
->expr
);
6456 vector_ctor
= gfc_constructor_next (vector_ctor
);
6458 else if (field
->expr_type
== EXPR_ARRAY
)
6459 e
= gfc_copy_expr (field_ctor
->expr
);
6461 e
= gfc_copy_expr (field
);
6463 gfc_constructor_append_expr (&result
->value
.constructor
, e
, NULL
);
6465 mask_ctor
= gfc_constructor_next (mask_ctor
);
6466 field_ctor
= gfc_constructor_next (field_ctor
);
6474 gfc_simplify_verify (gfc_expr
*s
, gfc_expr
*set
, gfc_expr
*b
, gfc_expr
*kind
)
6478 size_t index
, len
, lenset
;
6480 int k
= get_kind (BT_INTEGER
, kind
, "VERIFY", gfc_default_integer_kind
);
6483 return &gfc_bad_expr
;
6485 if (s
->expr_type
!= EXPR_CONSTANT
|| set
->expr_type
!= EXPR_CONSTANT
6486 || ( b
!= NULL
&& b
->expr_type
!= EXPR_CONSTANT
))
6489 if (b
!= NULL
&& b
->value
.logical
!= 0)
6494 result
= gfc_get_constant_expr (BT_INTEGER
, k
, &s
->where
);
6496 len
= s
->value
.character
.length
;
6497 lenset
= set
->value
.character
.length
;
6501 mpz_set_ui (result
->value
.integer
, 0);
6509 mpz_set_ui (result
->value
.integer
, 1);
6513 index
= wide_strspn (s
->value
.character
.string
,
6514 set
->value
.character
.string
) + 1;
6523 mpz_set_ui (result
->value
.integer
, len
);
6526 for (index
= len
; index
> 0; index
--)
6528 for (i
= 0; i
< lenset
; i
++)
6530 if (s
->value
.character
.string
[index
- 1]
6531 == set
->value
.character
.string
[i
])
6539 mpz_set_ui (result
->value
.integer
, index
);
6545 gfc_simplify_xor (gfc_expr
*x
, gfc_expr
*y
)
6550 if (x
->expr_type
!= EXPR_CONSTANT
|| y
->expr_type
!= EXPR_CONSTANT
)
6553 kind
= x
->ts
.kind
> y
->ts
.kind
? x
->ts
.kind
: y
->ts
.kind
;
6558 result
= gfc_get_constant_expr (BT_INTEGER
, kind
, &x
->where
);
6559 mpz_xor (result
->value
.integer
, x
->value
.integer
, y
->value
.integer
);
6560 return range_check (result
, "XOR");
6563 return gfc_get_logical_expr (kind
, &x
->where
,
6564 (x
->value
.logical
&& !y
->value
.logical
)
6565 || (!x
->value
.logical
&& y
->value
.logical
));
6573 /****************** Constant simplification *****************/
6575 /* Master function to convert one constant to another. While this is
6576 used as a simplification function, it requires the destination type
6577 and kind information which is supplied by a special case in
6581 gfc_convert_constant (gfc_expr
*e
, bt type
, int kind
)
6583 gfc_expr
*g
, *result
, *(*f
) (gfc_expr
*, int);
6598 f
= gfc_int2complex
;
6618 f
= gfc_real2complex
;
6629 f
= gfc_complex2int
;
6632 f
= gfc_complex2real
;
6635 f
= gfc_complex2complex
;
6661 f
= gfc_hollerith2int
;
6665 f
= gfc_hollerith2real
;
6669 f
= gfc_hollerith2complex
;
6673 f
= gfc_hollerith2character
;
6677 f
= gfc_hollerith2logical
;
6687 gfc_internal_error ("gfc_convert_constant(): Unexpected type");
6692 switch (e
->expr_type
)
6695 result
= f (e
, kind
);
6697 return &gfc_bad_expr
;
6701 if (!gfc_is_constant_expr (e
))
6704 result
= gfc_get_array_expr (type
, kind
, &e
->where
);
6705 result
->shape
= gfc_copy_shape (e
->shape
, e
->rank
);
6706 result
->rank
= e
->rank
;
6708 for (c
= gfc_constructor_first (e
->value
.constructor
);
6709 c
; c
= gfc_constructor_next (c
))
6712 if (c
->iterator
== NULL
)
6713 tmp
= f (c
->expr
, kind
);
6716 g
= gfc_convert_constant (c
->expr
, type
, kind
);
6717 if (g
== &gfc_bad_expr
)
6719 gfc_free_expr (result
);
6727 gfc_free_expr (result
);
6731 gfc_constructor_append_expr (&result
->value
.constructor
,
6745 /* Function for converting character constants. */
6747 gfc_convert_char_constant (gfc_expr
*e
, bt type ATTRIBUTE_UNUSED
, int kind
)
6752 if (!gfc_is_constant_expr (e
))
6755 if (e
->expr_type
== EXPR_CONSTANT
)
6757 /* Simple case of a scalar. */
6758 result
= gfc_get_constant_expr (BT_CHARACTER
, kind
, &e
->where
);
6760 return &gfc_bad_expr
;
6762 result
->value
.character
.length
= e
->value
.character
.length
;
6763 result
->value
.character
.string
6764 = gfc_get_wide_string (e
->value
.character
.length
+ 1);
6765 memcpy (result
->value
.character
.string
, e
->value
.character
.string
,
6766 (e
->value
.character
.length
+ 1) * sizeof (gfc_char_t
));
6768 /* Check we only have values representable in the destination kind. */
6769 for (i
= 0; i
< result
->value
.character
.length
; i
++)
6770 if (!gfc_check_character_range (result
->value
.character
.string
[i
],
6773 gfc_error ("Character '%s' in string at %L cannot be converted "
6774 "into character kind %d",
6775 gfc_print_wide_char (result
->value
.character
.string
[i
]),
6777 return &gfc_bad_expr
;
6782 else if (e
->expr_type
== EXPR_ARRAY
)
6784 /* For an array constructor, we convert each constructor element. */
6787 result
= gfc_get_array_expr (type
, kind
, &e
->where
);
6788 result
->shape
= gfc_copy_shape (e
->shape
, e
->rank
);
6789 result
->rank
= e
->rank
;
6790 result
->ts
.u
.cl
= e
->ts
.u
.cl
;
6792 for (c
= gfc_constructor_first (e
->value
.constructor
);
6793 c
; c
= gfc_constructor_next (c
))
6795 gfc_expr
*tmp
= gfc_convert_char_constant (c
->expr
, type
, kind
);
6796 if (tmp
== &gfc_bad_expr
)
6798 gfc_free_expr (result
);
6799 return &gfc_bad_expr
;
6804 gfc_free_expr (result
);
6808 gfc_constructor_append_expr (&result
->value
.constructor
,
6820 gfc_simplify_compiler_options (void)
6825 str
= gfc_get_option_string ();
6826 result
= gfc_get_character_expr (gfc_default_character_kind
,
6827 &gfc_current_locus
, str
, strlen (str
));
6834 gfc_simplify_compiler_version (void)
6839 len
= strlen ("GCC version ") + strlen (version_string
);
6840 buffer
= XALLOCAVEC (char, len
+ 1);
6841 snprintf (buffer
, len
+ 1, "GCC version %s", version_string
);
6842 return gfc_get_character_expr (gfc_default_character_kind
,
6843 &gfc_current_locus
, buffer
, len
);