| 1 |
/* Simplify intrinsic functions at compile-time. |
| 2 |
Copyright (C) 2000-2025 Free Software Foundation, Inc. |
| 3 |
Contributed by Andy Vaught & Katherine Holcomb |
| 4 |
|
| 5 |
This file is part of GCC. |
| 6 |
|
| 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 |
| 10 |
version. |
| 11 |
|
| 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 |
| 15 |
for more details. |
| 16 |
|
| 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/>. */ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
#include "system.h" |
| 23 |
#include "coretypes.h" |
| 24 |
#include "tm.h" /* For BITS_PER_UNIT. */ |
| 25 |
#include "gfortran.h" |
| 26 |
#include "arith.h" |
| 27 |
#include "intrinsic.h" |
| 28 |
#include "match.h" |
| 29 |
#include "target-memory.h" |
| 30 |
#include "constructor.h" |
| 31 |
#include "version.h" /* For version_string. */ |
| 32 |
|
| 33 |
/* Prototypes. */ |
| 34 |
|
| 35 |
static int min_max_choose (gfc_expr *, gfc_expr *, int, bool back_val = false); |
| 36 |
|
| 37 |
gfc_expr gfc_bad_expr; |
| 38 |
|
| 39 |
static gfc_expr *simplify_size (gfc_expr *, gfc_expr *, int); |
| 40 |
|
| 41 |
|
| 42 |
/* Note that 'simplification' is not just transforming expressions. |
| 43 |
For functions that are not simplified at compile time, range |
| 44 |
checking is done if possible. |
| 45 |
|
| 46 |
The return convention is that each simplification function returns: |
| 47 |
|
| 48 |
A new expression node corresponding to the simplified arguments. |
| 49 |
The original arguments are destroyed by the caller, and must not |
| 50 |
be a part of the new expression. |
| 51 |
|
| 52 |
NULL pointer indicating that no simplification was possible and |
| 53 |
the original expression should remain intact. |
| 54 |
|
| 55 |
An expression pointer to gfc_bad_expr (a static placeholder) |
| 56 |
indicating that some error has prevented simplification. The |
| 57 |
error is generated within the function and should be propagated |
| 58 |
upwards |
| 59 |
|
| 60 |
By the time a simplification function gets control, it has been |
| 61 |
decided that the function call is really supposed to be the |
| 62 |
intrinsic. No type checking is strictly necessary, since only |
| 63 |
valid types will be passed on. On the other hand, a simplification |
| 64 |
subroutine may have to look at the type of an argument as part of |
| 65 |
its processing. |
| 66 |
|
| 67 |
Array arguments are only passed to these subroutines that implement |
| 68 |
the simplification of transformational intrinsics. |
| 69 |
|
| 70 |
The functions in this file don't have much comment with them, but |
| 71 |
everything is reasonably straight-forward. The Standard, chapter 13 |
| 72 |
is the best comment you'll find for this file anyway. */ |
| 73 |
|
| 74 |
/* Range checks an expression node. If all goes well, returns the |
| 75 |
node, otherwise returns &gfc_bad_expr and frees the node. */ |
| 76 |
|
| 77 |
static gfc_expr * |
| 78 |
range_check (gfc_expr *result, const char *name) |
| 79 |
{ |
| 80 |
if (result == NULL) |
| 81 |
return &gfc_bad_expr; |
| 82 |
|
| 83 |
if (result->expr_type != EXPR_CONSTANT) |
| 84 |
return result; |
| 85 |
|
| 86 |
switch (gfc_range_check (result)) |
| 87 |
{ |
| 88 |
case ARITH_OK: |
| 89 |
return result; |
| 90 |
|
| 91 |
case ARITH_OVERFLOW: |
| 92 |
gfc_error ("Result of %s overflows its kind at %L", name, |
| 93 |
&result->where); |
| 94 |
break; |
| 95 |
|
| 96 |
case ARITH_UNDERFLOW: |
| 97 |
gfc_error ("Result of %s underflows its kind at %L", name, |
| 98 |
&result->where); |
| 99 |
break; |
| 100 |
|
| 101 |
case ARITH_NAN: |
| 102 |
gfc_error ("Result of %s is NaN at %L", name, &result->where); |
| 103 |
break; |
| 104 |
|
| 105 |
default: |
| 106 |
gfc_error ("Result of %s gives range error for its kind at %L", name, |
| 107 |
&result->where); |
| 108 |
break; |
| 109 |
} |
| 110 |
|
| 111 |
gfc_free_expr (result); |
| 112 |
return &gfc_bad_expr; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
/* A helper function that gets an optional and possibly missing |
| 117 |
kind parameter. Returns the kind, -1 if something went wrong. */ |
| 118 |
|
| 119 |
static int |
| 120 |
get_kind (bt type, gfc_expr *k, const char *name, int default_kind) |
| 121 |
{ |
| 122 |
int kind; |
| 123 |
|
| 124 |
if (k == NULL) |
| 125 |
return default_kind; |
| 126 |
|
| 127 |
if (k->expr_type != EXPR_CONSTANT) |
| 128 |
{ |
| 129 |
gfc_error ("KIND parameter of %s at %L must be an initialization " |
| 130 |
"expression", name, &k->where); |
| 131 |
return -1; |
| 132 |
} |
| 133 |
|
| 134 |
if (gfc_extract_int (k, &kind) |
| 135 |
|| gfc_validate_kind (type, kind, true) < 0) |
| 136 |
{ |
| 137 |
gfc_error ("Invalid KIND parameter of %s at %L", name, &k->where); |
| 138 |
return -1; |
| 139 |
} |
| 140 |
|
| 141 |
return kind; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
/* Converts an mpz_t signed variable into an unsigned one, assuming |
| 146 |
two's complement representations and a binary width of bitsize. |
| 147 |
The conversion is a no-op unless x is negative; otherwise, it can |
| 148 |
be accomplished by masking out the high bits. */ |
| 149 |
|
| 150 |
void |
| 151 |
gfc_convert_mpz_to_unsigned (mpz_t x, int bitsize, bool sign) |
| 152 |
{ |
| 153 |
mpz_t mask; |
| 154 |
|
| 155 |
if (mpz_sgn (x) < 0) |
| 156 |
{ |
| 157 |
/* Confirm that no bits above the signed range are unset if we |
| 158 |
are doing range checking. */ |
| 159 |
if (sign && flag_range_check != 0) |
| 160 |
gcc_assert (mpz_scan0 (x, bitsize-1) == ULONG_MAX); |
| 161 |
|
| 162 |
mpz_init_set_ui (mask, 1); |
| 163 |
mpz_mul_2exp (mask, mask, bitsize); |
| 164 |
mpz_sub_ui (mask, mask, 1); |
| 165 |
|
| 166 |
mpz_and (x, x, mask); |
| 167 |
|
| 168 |
mpz_clear (mask); |
| 169 |
} |
| 170 |
else |
| 171 |
{ |
| 172 |
/* Confirm that no bits above the signed range are set if we |
| 173 |
are doing range checking. */ |
| 174 |
if (sign && flag_range_check != 0) |
| 175 |
gcc_assert (mpz_scan1 (x, bitsize-1) == ULONG_MAX); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
/* Converts an mpz_t unsigned variable into a signed one, assuming |
| 181 |
two's complement representations and a binary width of bitsize. |
| 182 |
If the bitsize-1 bit is set, this is taken as a sign bit and |
| 183 |
the number is converted to the corresponding negative number. */ |
| 184 |
|
| 185 |
void |
| 186 |
gfc_convert_mpz_to_signed (mpz_t x, int bitsize) |
| 187 |
{ |
| 188 |
mpz_t mask; |
| 189 |
|
| 190 |
/* Confirm that no bits above the unsigned range are set if we are |
| 191 |
doing range checking. */ |
| 192 |
if (flag_range_check != 0) |
| 193 |
gcc_assert (mpz_scan1 (x, bitsize) == ULONG_MAX); |
| 194 |
|
| 195 |
if (mpz_tstbit (x, bitsize - 1) == 1) |
| 196 |
{ |
| 197 |
mpz_init_set_ui (mask, 1); |
| 198 |
mpz_mul_2exp (mask, mask, bitsize); |
| 199 |
mpz_sub_ui (mask, mask, 1); |
| 200 |
|
| 201 |
/* We negate the number by hand, zeroing the high bits, that is |
| 202 |
make it the corresponding positive number, and then have it |
| 203 |
negated by GMP, giving the correct representation of the |
| 204 |
negative number. */ |
| 205 |
mpz_com (x, x); |
| 206 |
mpz_add_ui (x, x, 1); |
| 207 |
mpz_and (x, x, mask); |
| 208 |
|
| 209 |
mpz_neg (x, x); |
| 210 |
|
| 211 |
mpz_clear (mask); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
/* Test that the expression is a constant array, simplifying if |
| 217 |
we are dealing with a parameter array. */ |
| 218 |
|
| 219 |
static bool |
| 220 |
is_constant_array_expr (gfc_expr *e) |
| 221 |
{ |
| 222 |
gfc_constructor *c; |
| 223 |
bool array_OK = true; |
| 224 |
mpz_t size; |
| 225 |
|
| 226 |
if (e == NULL) |
| 227 |
return true; |
| 228 |
|
| 229 |
if (e->expr_type == EXPR_VARIABLE && e->rank > 0 |
| 230 |
&& e->symtree->n.sym->attr.flavor == FL_PARAMETER) |
| 231 |
gfc_simplify_expr (e, 1); |
| 232 |
|
| 233 |
if (e->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (e)) |
| 234 |
return false; |
| 235 |
|
| 236 |
/* A non-zero-sized constant array shall have a non-empty constructor. */ |
| 237 |
if (e->rank > 0 && e->shape != NULL && e->value.constructor == NULL) |
| 238 |
{ |
| 239 |
mpz_init_set_ui (size, 1); |
| 240 |
for (int j = 0; j < e->rank; j++) |
| 241 |
mpz_mul (size, size, e->shape[j]); |
| 242 |
bool not_size0 = (mpz_cmp_si (size, 0) != 0); |
| 243 |
mpz_clear (size); |
| 244 |
if (not_size0) |
| 245 |
return false; |
| 246 |
} |
| 247 |
|
| 248 |
for (c = gfc_constructor_first (e->value.constructor); |
| 249 |
c; c = gfc_constructor_next (c)) |
| 250 |
if (c->expr->expr_type != EXPR_CONSTANT |
| 251 |
&& c->expr->expr_type != EXPR_STRUCTURE) |
| 252 |
{ |
| 253 |
array_OK = false; |
| 254 |
break; |
| 255 |
} |
| 256 |
|
| 257 |
/* Check and expand the constructor. We do this when either |
| 258 |
gfc_init_expr_flag is set or for not too large array constructors. */ |
| 259 |
bool expand; |
| 260 |
expand = (e->rank == 1 |
| 261 |
&& e->shape |
| 262 |
&& (mpz_cmp_ui (e->shape[0], flag_max_array_constructor) < 0)); |
| 263 |
|
| 264 |
if (!array_OK && (gfc_init_expr_flag || expand) && e->rank == 1) |
| 265 |
{ |
| 266 |
bool saved_init_expr_flag = gfc_init_expr_flag; |
| 267 |
array_OK = gfc_reduce_init_expr (e); |
| 268 |
/* gfc_reduce_init_expr resets the flag. */ |
| 269 |
gfc_init_expr_flag = saved_init_expr_flag; |
| 270 |
} |
| 271 |
else |
| 272 |
return array_OK; |
| 273 |
|
| 274 |
/* Recheck to make sure that any EXPR_ARRAYs have gone. */ |
| 275 |
for (c = gfc_constructor_first (e->value.constructor); |
| 276 |
c; c = gfc_constructor_next (c)) |
| 277 |
if (c->expr->expr_type != EXPR_CONSTANT |
| 278 |
&& c->expr->expr_type != EXPR_STRUCTURE) |
| 279 |
return false; |
| 280 |
|
| 281 |
/* Make sure that the array has a valid shape. */ |
| 282 |
if (e->shape == NULL && e->rank == 1) |
| 283 |
{ |
| 284 |
if (!gfc_array_size(e, &size)) |
| 285 |
return false; |
| 286 |
e->shape = gfc_get_shape (1); |
| 287 |
mpz_init_set (e->shape[0], size); |
| 288 |
mpz_clear (size); |
| 289 |
} |
| 290 |
|
| 291 |
return array_OK; |
| 292 |
} |
| 293 |
|
| 294 |
bool |
| 295 |
gfc_is_constant_array_expr (gfc_expr *e) |
| 296 |
{ |
| 297 |
return is_constant_array_expr (e); |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
/* Test for a size zero array. */ |
| 302 |
bool |
| 303 |
gfc_is_size_zero_array (gfc_expr *array) |
| 304 |
{ |
| 305 |
|
| 306 |
if (array->rank == 0) |
| 307 |
return false; |
| 308 |
|
| 309 |
if (array->expr_type == EXPR_VARIABLE && array->rank > 0 |
| 310 |
&& array->symtree->n.sym->attr.flavor == FL_PARAMETER |
| 311 |
&& array->shape != NULL) |
| 312 |
{ |
| 313 |
for (int i = 0; i < array->rank; i++) |
| 314 |
if (mpz_cmp_si (array->shape[i], 0) <= 0) |
| 315 |
return true; |
| 316 |
|
| 317 |
return false; |
| 318 |
} |
| 319 |
|
| 320 |
if (array->expr_type == EXPR_ARRAY) |
| 321 |
return array->value.constructor == NULL; |
| 322 |
|
| 323 |
return false; |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
/* Initialize a transformational result expression with a given value. */ |
| 328 |
|
| 329 |
static void |
| 330 |
init_result_expr (gfc_expr *e, int init, gfc_expr *array) |
| 331 |
{ |
| 332 |
if (e && e->expr_type == EXPR_ARRAY) |
| 333 |
{ |
| 334 |
gfc_constructor *ctor = gfc_constructor_first (e->value.constructor); |
| 335 |
while (ctor) |
| 336 |
{ |
| 337 |
init_result_expr (ctor->expr, init, array); |
| 338 |
ctor = gfc_constructor_next (ctor); |
| 339 |
} |
| 340 |
} |
| 341 |
else if (e && e->expr_type == EXPR_CONSTANT) |
| 342 |
{ |
| 343 |
int i = gfc_validate_kind (e->ts.type, e->ts.kind, false); |
| 344 |
HOST_WIDE_INT length; |
| 345 |
gfc_char_t *string; |
| 346 |
|
| 347 |
switch (e->ts.type) |
| 348 |
{ |
| 349 |
case BT_LOGICAL: |
| 350 |
e->value.logical = (init ? 1 : 0); |
| 351 |
break; |
| 352 |
|
| 353 |
case BT_INTEGER: |
| 354 |
if (init == INT_MIN) |
| 355 |
mpz_set (e->value.integer, gfc_integer_kinds[i].min_int); |
| 356 |
else if (init == INT_MAX) |
| 357 |
mpz_set (e->value.integer, gfc_integer_kinds[i].huge); |
| 358 |
else |
| 359 |
mpz_set_si (e->value.integer, init); |
| 360 |
break; |
| 361 |
|
| 362 |
case BT_UNSIGNED: |
| 363 |
if (init == INT_MIN) |
| 364 |
mpz_set_ui (e->value.integer, 0); |
| 365 |
else if (init == INT_MAX) |
| 366 |
mpz_set (e->value.integer, gfc_unsigned_kinds[i].huge); |
| 367 |
else |
| 368 |
mpz_set_ui (e->value.integer, init); |
| 369 |
break; |
| 370 |
|
| 371 |
case BT_REAL: |
| 372 |
if (init == INT_MIN) |
| 373 |
{ |
| 374 |
mpfr_set (e->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE); |
| 375 |
mpfr_neg (e->value.real, e->value.real, GFC_RND_MODE); |
| 376 |
} |
| 377 |
else if (init == INT_MAX) |
| 378 |
mpfr_set (e->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE); |
| 379 |
else |
| 380 |
mpfr_set_si (e->value.real, init, GFC_RND_MODE); |
| 381 |
break; |
| 382 |
|
| 383 |
case BT_COMPLEX: |
| 384 |
mpc_set_si (e->value.complex, init, GFC_MPC_RND_MODE); |
| 385 |
break; |
| 386 |
|
| 387 |
case BT_CHARACTER: |
| 388 |
if (init == INT_MIN) |
| 389 |
{ |
| 390 |
gfc_expr *len = gfc_simplify_len (array, NULL); |
| 391 |
gfc_extract_hwi (len, &length); |
| 392 |
string = gfc_get_wide_string (length + 1); |
| 393 |
gfc_wide_memset (string, 0, length); |
| 394 |
} |
| 395 |
else if (init == INT_MAX) |
| 396 |
{ |
| 397 |
gfc_expr *len = gfc_simplify_len (array, NULL); |
| 398 |
gfc_extract_hwi (len, &length); |
| 399 |
string = gfc_get_wide_string (length + 1); |
| 400 |
gfc_wide_memset (string, 255, length); |
| 401 |
} |
| 402 |
else |
| 403 |
{ |
| 404 |
length = 0; |
| 405 |
string = gfc_get_wide_string (1); |
| 406 |
} |
| 407 |
|
| 408 |
string[length] = '\0'; |
| 409 |
e->value.character.length = length; |
| 410 |
e->value.character.string = string; |
| 411 |
break; |
| 412 |
|
| 413 |
default: |
| 414 |
gcc_unreachable(); |
| 415 |
} |
| 416 |
} |
| 417 |
else |
| 418 |
gcc_unreachable(); |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
/* Helper function for gfc_simplify_dot_product() and gfc_simplify_matmul; |
| 423 |
if conj_a is true, the matrix_a is complex conjugated. */ |
| 424 |
|
| 425 |
static gfc_expr * |
| 426 |
compute_dot_product (gfc_expr *matrix_a, int stride_a, int offset_a, |
| 427 |
gfc_expr *matrix_b, int stride_b, int offset_b, |
| 428 |
bool conj_a) |
| 429 |
{ |
| 430 |
gfc_expr *result, *a, *b, *c; |
| 431 |
|
| 432 |
/* Set result to an UNSIGNED of correct kind for unsigned, |
| 433 |
INTEGER(1) 0 for other numeric types, and .false. for |
| 434 |
LOGICAL. Mixed-mode math in the loop will promote result to the |
| 435 |
correct type and kind. */ |
| 436 |
if (matrix_a->ts.type == BT_LOGICAL) |
| 437 |
result = gfc_get_logical_expr (gfc_default_logical_kind, NULL, false); |
| 438 |
else if (matrix_a->ts.type == BT_UNSIGNED) |
| 439 |
{ |
| 440 |
int kind = MAX (matrix_a->ts.kind, matrix_b->ts.kind); |
| 441 |
result = gfc_get_unsigned_expr (kind, NULL, 0); |
| 442 |
} |
| 443 |
else |
| 444 |
result = gfc_get_int_expr (1, NULL, 0); |
| 445 |
|
| 446 |
result->where = matrix_a->where; |
| 447 |
|
| 448 |
a = gfc_constructor_lookup_expr (matrix_a->value.constructor, offset_a); |
| 449 |
b = gfc_constructor_lookup_expr (matrix_b->value.constructor, offset_b); |
| 450 |
while (a && b) |
| 451 |
{ |
| 452 |
/* Copying of expressions is required as operands are free'd |
| 453 |
by the gfc_arith routines. */ |
| 454 |
switch (result->ts.type) |
| 455 |
{ |
| 456 |
case BT_LOGICAL: |
| 457 |
result = gfc_or (result, |
| 458 |
gfc_and (gfc_copy_expr (a), |
| 459 |
gfc_copy_expr (b))); |
| 460 |
break; |
| 461 |
|
| 462 |
case BT_INTEGER: |
| 463 |
case BT_REAL: |
| 464 |
case BT_COMPLEX: |
| 465 |
case BT_UNSIGNED: |
| 466 |
if (conj_a && a->ts.type == BT_COMPLEX) |
| 467 |
c = gfc_simplify_conjg (a); |
| 468 |
else |
| 469 |
c = gfc_copy_expr (a); |
| 470 |
result = gfc_add (result, gfc_multiply (c, gfc_copy_expr (b))); |
| 471 |
break; |
| 472 |
|
| 473 |
default: |
| 474 |
gcc_unreachable(); |
| 475 |
} |
| 476 |
|
| 477 |
offset_a += stride_a; |
| 478 |
a = gfc_constructor_lookup_expr (matrix_a->value.constructor, offset_a); |
| 479 |
|
| 480 |
offset_b += stride_b; |
| 481 |
b = gfc_constructor_lookup_expr (matrix_b->value.constructor, offset_b); |
| 482 |
} |
| 483 |
|
| 484 |
return result; |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
/* Build a result expression for transformational intrinsics, |
| 489 |
depending on DIM. */ |
| 490 |
|
| 491 |
static gfc_expr * |
| 492 |
transformational_result (gfc_expr *array, gfc_expr *dim, bt type, |
| 493 |
int kind, locus* where) |
| 494 |
{ |
| 495 |
gfc_expr *result; |
| 496 |
int i, nelem; |
| 497 |
|
| 498 |
if (!dim || array->rank == 1) |
| 499 |
return gfc_get_constant_expr (type, kind, where); |
| 500 |
|
| 501 |
result = gfc_get_array_expr (type, kind, where); |
| 502 |
result->shape = gfc_copy_shape_excluding (array->shape, array->rank, dim); |
| 503 |
result->rank = array->rank - 1; |
| 504 |
|
| 505 |
/* gfc_array_size() would count the number of elements in the constructor, |
| 506 |
we have not built those yet. */ |
| 507 |
nelem = 1; |
| 508 |
for (i = 0; i < result->rank; ++i) |
| 509 |
nelem *= mpz_get_ui (result->shape[i]); |
| 510 |
|
| 511 |
for (i = 0; i < nelem; ++i) |
| 512 |
{ |
| 513 |
gfc_constructor_append_expr (&result->value.constructor, |
| 514 |
gfc_get_constant_expr (type, kind, where), |
| 515 |
NULL); |
| 516 |
} |
| 517 |
|
| 518 |
return result; |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
typedef gfc_expr* (*transformational_op)(gfc_expr*, gfc_expr*); |
| 523 |
|
| 524 |
/* Wrapper function, implements 'op1 += 1'. Only called if MASK |
| 525 |
of COUNT intrinsic is .TRUE.. |
| 526 |
|
| 527 |
Interface and implementation mimics arith functions as |
| 528 |
gfc_add, gfc_multiply, etc. */ |
| 529 |
|
| 530 |
static gfc_expr * |
| 531 |
gfc_count (gfc_expr *op1, gfc_expr *op2) |
| 532 |
{ |
| 533 |
gfc_expr *result; |
| 534 |
|
| 535 |
gcc_assert (op1->ts.type == BT_INTEGER); |
| 536 |
gcc_assert (op2->ts.type == BT_LOGICAL); |
| 537 |
gcc_assert (op2->value.logical); |
| 538 |
|
| 539 |
result = gfc_copy_expr (op1); |
| 540 |
mpz_add_ui (result->value.integer, result->value.integer, 1); |
| 541 |
|
| 542 |
gfc_free_expr (op1); |
| 543 |
gfc_free_expr (op2); |
| 544 |
return result; |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
/* Transforms an ARRAY with operation OP, according to MASK, to a |
| 549 |
scalar RESULT. E.g. called if |
| 550 |
|
| 551 |
REAL, PARAMETER :: array(n, m) = ... |
| 552 |
REAL, PARAMETER :: s = SUM(array) |
| 553 |
|
| 554 |
where OP == gfc_add(). */ |
| 555 |
|
| 556 |
static gfc_expr * |
| 557 |
simplify_transformation_to_scalar (gfc_expr *result, gfc_expr *array, gfc_expr *mask, |
| 558 |
transformational_op op) |
| 559 |
{ |
| 560 |
gfc_expr *a, *m; |
| 561 |
gfc_constructor *array_ctor, *mask_ctor; |
| 562 |
|
| 563 |
/* Shortcut for constant .FALSE. MASK. */ |
| 564 |
if (mask |
| 565 |
&& mask->expr_type == EXPR_CONSTANT |
| 566 |
&& !mask->value.logical) |
| 567 |
return result; |
| 568 |
|
| 569 |
array_ctor = gfc_constructor_first (array->value.constructor); |
| 570 |
mask_ctor = NULL; |
| 571 |
if (mask && mask->expr_type == EXPR_ARRAY) |
| 572 |
mask_ctor = gfc_constructor_first (mask->value.constructor); |
| 573 |
|
| 574 |
while (array_ctor) |
| 575 |
{ |
| 576 |
a = array_ctor->expr; |
| 577 |
array_ctor = gfc_constructor_next (array_ctor); |
| 578 |
|
| 579 |
/* A constant MASK equals .TRUE. here and can be ignored. */ |
| 580 |
if (mask_ctor) |
| 581 |
{ |
| 582 |
m = mask_ctor->expr; |
| 583 |
mask_ctor = gfc_constructor_next (mask_ctor); |
| 584 |
if (!m->value.logical) |
| 585 |
continue; |
| 586 |
} |
| 587 |
|
| 588 |
result = op (result, gfc_copy_expr (a)); |
| 589 |
if (!result) |
| 590 |
return result; |
| 591 |
} |
| 592 |
|
| 593 |
return result; |
| 594 |
} |
| 595 |
|
| 596 |
/* Transforms an ARRAY with operation OP, according to MASK, to an |
| 597 |
array RESULT. E.g. called if |
| 598 |
|
| 599 |
REAL, PARAMETER :: array(n, m) = ... |
| 600 |
REAL, PARAMETER :: s(n) = PROD(array, DIM=1) |
| 601 |
|
| 602 |
where OP == gfc_multiply(). |
| 603 |
The result might be post processed using post_op. */ |
| 604 |
|
| 605 |
static gfc_expr * |
| 606 |
simplify_transformation_to_array (gfc_expr *result, gfc_expr *array, gfc_expr *dim, |
| 607 |
gfc_expr *mask, transformational_op op, |
| 608 |
transformational_op post_op) |
| 609 |
{ |
| 610 |
mpz_t size; |
| 611 |
int done, i, n, arraysize, resultsize, dim_index, dim_extent, dim_stride; |
| 612 |
gfc_expr **arrayvec, **resultvec, **base, **src, **dest; |
| 613 |
gfc_constructor *array_ctor, *mask_ctor, *result_ctor; |
| 614 |
|
| 615 |
int count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS], |
| 616 |
sstride[GFC_MAX_DIMENSIONS], dstride[GFC_MAX_DIMENSIONS], |
| 617 |
tmpstride[GFC_MAX_DIMENSIONS]; |
| 618 |
|
| 619 |
/* Shortcut for constant .FALSE. MASK. */ |
| 620 |
if (mask |
| 621 |
&& mask->expr_type == EXPR_CONSTANT |
| 622 |
&& !mask->value.logical) |
| 623 |
return result; |
| 624 |
|
| 625 |
/* Build an indexed table for array element expressions to minimize |
| 626 |
linked-list traversal. Masked elements are set to NULL. */ |
| 627 |
gfc_array_size (array, &size); |
| 628 |
arraysize = mpz_get_ui (size); |
| 629 |
mpz_clear (size); |
| 630 |
|
| 631 |
arrayvec = XCNEWVEC (gfc_expr*, arraysize); |
| 632 |
|
| 633 |
array_ctor = gfc_constructor_first (array->value.constructor); |
| 634 |
mask_ctor = NULL; |
| 635 |
if (mask && mask->expr_type == EXPR_ARRAY) |
| 636 |
mask_ctor = gfc_constructor_first (mask->value.constructor); |
| 637 |
|
| 638 |
for (i = 0; i < arraysize; ++i) |
| 639 |
{ |
| 640 |
arrayvec[i] = array_ctor->expr; |
| 641 |
array_ctor = gfc_constructor_next (array_ctor); |
| 642 |
|
| 643 |
if (mask_ctor) |
| 644 |
{ |
| 645 |
if (!mask_ctor->expr->value.logical) |
| 646 |
arrayvec[i] = NULL; |
| 647 |
|
| 648 |
mask_ctor = gfc_constructor_next (mask_ctor); |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
/* Same for the result expression. */ |
| 653 |
gfc_array_size (result, &size); |
| 654 |
resultsize = mpz_get_ui (size); |
| 655 |
mpz_clear (size); |
| 656 |
|
| 657 |
resultvec = XCNEWVEC (gfc_expr*, resultsize); |
| 658 |
result_ctor = gfc_constructor_first (result->value.constructor); |
| 659 |
for (i = 0; i < resultsize; ++i) |
| 660 |
{ |
| 661 |
resultvec[i] = result_ctor->expr; |
| 662 |
result_ctor = gfc_constructor_next (result_ctor); |
| 663 |
} |
| 664 |
|
| 665 |
gfc_extract_int (dim, &dim_index); |
| 666 |
dim_index -= 1; /* zero-base index */ |
| 667 |
dim_extent = 0; |
| 668 |
dim_stride = 0; |
| 669 |
|
| 670 |
for (i = 0, n = 0; i < array->rank; ++i) |
| 671 |
{ |
| 672 |
count[i] = 0; |
| 673 |
tmpstride[i] = (i == 0) ? 1 : tmpstride[i-1] * mpz_get_si (array->shape[i-1]); |
| 674 |
if (i == dim_index) |
| 675 |
{ |
| 676 |
dim_extent = mpz_get_si (array->shape[i]); |
| 677 |
dim_stride = tmpstride[i]; |
| 678 |
continue; |
| 679 |
} |
| 680 |
|
| 681 |
extent[n] = mpz_get_si (array->shape[i]); |
| 682 |
sstride[n] = tmpstride[i]; |
| 683 |
dstride[n] = (n == 0) ? 1 : dstride[n-1] * extent[n-1]; |
| 684 |
n += 1; |
| 685 |
} |
| 686 |
|
| 687 |
done = resultsize <= 0; |
| 688 |
base = arrayvec; |
| 689 |
dest = resultvec; |
| 690 |
while (!done) |
| 691 |
{ |
| 692 |
for (src = base, n = 0; n < dim_extent; src += dim_stride, ++n) |
| 693 |
if (*src) |
| 694 |
*dest = op (*dest, gfc_copy_expr (*src)); |
| 695 |
|
| 696 |
if (post_op) |
| 697 |
*dest = post_op (*dest, *dest); |
| 698 |
|
| 699 |
count[0]++; |
| 700 |
base += sstride[0]; |
| 701 |
dest += dstride[0]; |
| 702 |
|
| 703 |
n = 0; |
| 704 |
while (!done && count[n] == extent[n]) |
| 705 |
{ |
| 706 |
count[n] = 0; |
| 707 |
base -= sstride[n] * extent[n]; |
| 708 |
dest -= dstride[n] * extent[n]; |
| 709 |
|
| 710 |
n++; |
| 711 |
if (n < result->rank) |
| 712 |
{ |
| 713 |
/* If the nested loop is unrolled GFC_MAX_DIMENSIONS |
| 714 |
times, we'd warn for the last iteration, because the |
| 715 |
array index will have already been incremented to the |
| 716 |
array sizes, and we can't tell that this must make |
| 717 |
the test against result->rank false, because ranks |
| 718 |
must not exceed GFC_MAX_DIMENSIONS. */ |
| 719 |
GCC_DIAGNOSTIC_PUSH_IGNORED (-Warray-bounds) |
| 720 |
count[n]++; |
| 721 |
base += sstride[n]; |
| 722 |
dest += dstride[n]; |
| 723 |
GCC_DIAGNOSTIC_POP |
| 724 |
} |
| 725 |
else |
| 726 |
done = true; |
| 727 |
} |
| 728 |
} |
| 729 |
|
| 730 |
/* Place updated expression in result constructor. */ |
| 731 |
result_ctor = gfc_constructor_first (result->value.constructor); |
| 732 |
for (i = 0; i < resultsize; ++i) |
| 733 |
{ |
| 734 |
result_ctor->expr = resultvec[i]; |
| 735 |
result_ctor = gfc_constructor_next (result_ctor); |
| 736 |
} |
| 737 |
|
| 738 |
free (arrayvec); |
| 739 |
free (resultvec); |
| 740 |
return result; |
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
static gfc_expr * |
| 745 |
simplify_transformation (gfc_expr *array, gfc_expr *dim, gfc_expr *mask, |
| 746 |
int init_val, transformational_op op) |
| 747 |
{ |
| 748 |
gfc_expr *result; |
| 749 |
bool size_zero; |
| 750 |
|
| 751 |
size_zero = gfc_is_size_zero_array (array); |
| 752 |
|
| 753 |
if (!(is_constant_array_expr (array) || size_zero) |
| 754 |
|| array->shape == NULL |
| 755 |
|| !gfc_is_constant_expr (dim)) |
| 756 |
return NULL; |
| 757 |
|
| 758 |
if (mask |
| 759 |
&& !is_constant_array_expr (mask) |
| 760 |
&& mask->expr_type != EXPR_CONSTANT) |
| 761 |
return NULL; |
| 762 |
|
| 763 |
result = transformational_result (array, dim, array->ts.type, |
| 764 |
array->ts.kind, &array->where); |
| 765 |
init_result_expr (result, init_val, array); |
| 766 |
|
| 767 |
if (size_zero) |
| 768 |
return result; |
| 769 |
|
| 770 |
return !dim || array->rank == 1 ? |
| 771 |
simplify_transformation_to_scalar (result, array, mask, op) : |
| 772 |
simplify_transformation_to_array (result, array, dim, mask, op, NULL); |
| 773 |
} |
| 774 |
|
| 775 |
|
| 776 |
/********************** Simplification functions *****************************/ |
| 777 |
|
| 778 |
gfc_expr * |
| 779 |
gfc_simplify_abs (gfc_expr *e) |
| 780 |
{ |
| 781 |
gfc_expr *result; |
| 782 |
|
| 783 |
if (e->expr_type != EXPR_CONSTANT) |
| 784 |
return NULL; |
| 785 |
|
| 786 |
switch (e->ts.type) |
| 787 |
{ |
| 788 |
case BT_INTEGER: |
| 789 |
result = gfc_get_constant_expr (BT_INTEGER, e->ts.kind, &e->where); |
| 790 |
mpz_abs (result->value.integer, e->value.integer); |
| 791 |
return range_check (result, "IABS"); |
| 792 |
|
| 793 |
case BT_REAL: |
| 794 |
result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where); |
| 795 |
mpfr_abs (result->value.real, e->value.real, GFC_RND_MODE); |
| 796 |
return range_check (result, "ABS"); |
| 797 |
|
| 798 |
case BT_COMPLEX: |
| 799 |
gfc_set_model_kind (e->ts.kind); |
| 800 |
result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where); |
| 801 |
mpc_abs (result->value.real, e->value.complex, GFC_RND_MODE); |
| 802 |
return range_check (result, "CABS"); |
| 803 |
|
| 804 |
default: |
| 805 |
gfc_internal_error ("gfc_simplify_abs(): Bad type"); |
| 806 |
} |
| 807 |
} |
| 808 |
|
| 809 |
|
| 810 |
static gfc_expr * |
| 811 |
simplify_achar_char (gfc_expr *e, gfc_expr *k, const char *name, bool ascii) |
| 812 |
{ |
| 813 |
gfc_expr *result; |
| 814 |
int kind; |
| 815 |
bool too_large = false; |
| 816 |
|
| 817 |
if (e->expr_type != EXPR_CONSTANT) |
| 818 |
return NULL; |
| 819 |
|
| 820 |
kind = get_kind (BT_CHARACTER, k, name, gfc_default_character_kind); |
| 821 |
if (kind == -1) |
| 822 |
return &gfc_bad_expr; |
| 823 |
|
| 824 |
if (mpz_cmp_si (e->value.integer, 0) < 0) |
| 825 |
{ |
| 826 |
gfc_error ("Argument of %s function at %L is negative", name, |
| 827 |
&e->where); |
| 828 |
return &gfc_bad_expr; |
| 829 |
} |
| 830 |
|
| 831 |
if (ascii && warn_surprising && mpz_cmp_si (e->value.integer, 127) > 0) |
| 832 |
gfc_warning (OPT_Wsurprising, |
| 833 |
"Argument of %s function at %L outside of range [0,127]", |
| 834 |
name, &e->where); |
| 835 |
|
| 836 |
if (kind == 1 && mpz_cmp_si (e->value.integer, 255) > 0) |
| 837 |
too_large = true; |
| 838 |
else if (kind == 4) |
| 839 |
{ |
| 840 |
mpz_t t; |
| 841 |
mpz_init_set_ui (t, 2); |
| 842 |
mpz_pow_ui (t, t, 32); |
| 843 |
mpz_sub_ui (t, t, 1); |
| 844 |
if (mpz_cmp (e->value.integer, t) > 0) |
| 845 |
too_large = true; |
| 846 |
mpz_clear (t); |
| 847 |
} |
| 848 |
|
| 849 |
if (too_large) |
| 850 |
{ |
| 851 |
gfc_error ("Argument of %s function at %L is too large for the " |
| 852 |
"collating sequence of kind %d", name, &e->where, kind); |
| 853 |
return &gfc_bad_expr; |
| 854 |
} |
| 855 |
|
| 856 |
result = gfc_get_character_expr (kind, &e->where, NULL, 1); |
| 857 |
result->value.character.string[0] = mpz_get_ui (e->value.integer); |
| 858 |
|
| 859 |
return result; |
| 860 |
} |
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
/* We use the processor's collating sequence, because all |
| 865 |
systems that gfortran currently works on are ASCII. */ |
| 866 |
|
| 867 |
gfc_expr * |
| 868 |
gfc_simplify_achar (gfc_expr *e, gfc_expr *k) |
| 869 |
{ |
| 870 |
return simplify_achar_char (e, k, "ACHAR", true); |
| 871 |
} |
| 872 |
|
| 873 |
|
| 874 |
gfc_expr * |
| 875 |
gfc_simplify_acos (gfc_expr *x) |
| 876 |
{ |
| 877 |
gfc_expr *result; |
| 878 |
|
| 879 |
if (x->expr_type != EXPR_CONSTANT) |
| 880 |
return NULL; |
| 881 |
|
| 882 |
switch (x->ts.type) |
| 883 |
{ |
| 884 |
case BT_REAL: |
| 885 |
if (mpfr_cmp_si (x->value.real, 1) > 0 |
| 886 |
|| mpfr_cmp_si (x->value.real, -1) < 0) |
| 887 |
{ |
| 888 |
gfc_error ("Argument of ACOS at %L must be between -1 and 1", |
| 889 |
&x->where); |
| 890 |
return &gfc_bad_expr; |
| 891 |
} |
| 892 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 893 |
mpfr_acos (result->value.real, x->value.real, GFC_RND_MODE); |
| 894 |
break; |
| 895 |
|
| 896 |
case BT_COMPLEX: |
| 897 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 898 |
mpc_acos (result->value.complex, x->value.complex, GFC_MPC_RND_MODE); |
| 899 |
break; |
| 900 |
|
| 901 |
default: |
| 902 |
gfc_internal_error ("in gfc_simplify_acos(): Bad type"); |
| 903 |
} |
| 904 |
|
| 905 |
return range_check (result, "ACOS"); |
| 906 |
} |
| 907 |
|
| 908 |
gfc_expr * |
| 909 |
gfc_simplify_acosh (gfc_expr *x) |
| 910 |
{ |
| 911 |
gfc_expr *result; |
| 912 |
|
| 913 |
if (x->expr_type != EXPR_CONSTANT) |
| 914 |
return NULL; |
| 915 |
|
| 916 |
switch (x->ts.type) |
| 917 |
{ |
| 918 |
case BT_REAL: |
| 919 |
if (mpfr_cmp_si (x->value.real, 1) < 0) |
| 920 |
{ |
| 921 |
gfc_error ("Argument of ACOSH at %L must not be less than 1", |
| 922 |
&x->where); |
| 923 |
return &gfc_bad_expr; |
| 924 |
} |
| 925 |
|
| 926 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 927 |
mpfr_acosh (result->value.real, x->value.real, GFC_RND_MODE); |
| 928 |
break; |
| 929 |
|
| 930 |
case BT_COMPLEX: |
| 931 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 932 |
mpc_acosh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE); |
| 933 |
break; |
| 934 |
|
| 935 |
default: |
| 936 |
gfc_internal_error ("in gfc_simplify_acosh(): Bad type"); |
| 937 |
} |
| 938 |
|
| 939 |
return range_check (result, "ACOSH"); |
| 940 |
} |
| 941 |
|
| 942 |
gfc_expr * |
| 943 |
gfc_simplify_adjustl (gfc_expr *e) |
| 944 |
{ |
| 945 |
gfc_expr *result; |
| 946 |
int count, i, len; |
| 947 |
gfc_char_t ch; |
| 948 |
|
| 949 |
if (e->expr_type != EXPR_CONSTANT) |
| 950 |
return NULL; |
| 951 |
|
| 952 |
len = e->value.character.length; |
| 953 |
|
| 954 |
for (count = 0, i = 0; i < len; ++i) |
| 955 |
{ |
| 956 |
ch = e->value.character.string[i]; |
| 957 |
if (ch != ' ') |
| 958 |
break; |
| 959 |
++count; |
| 960 |
} |
| 961 |
|
| 962 |
result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, len); |
| 963 |
for (i = 0; i < len - count; ++i) |
| 964 |
result->value.character.string[i] = e->value.character.string[count + i]; |
| 965 |
|
| 966 |
return result; |
| 967 |
} |
| 968 |
|
| 969 |
|
| 970 |
gfc_expr * |
| 971 |
gfc_simplify_adjustr (gfc_expr *e) |
| 972 |
{ |
| 973 |
gfc_expr *result; |
| 974 |
int count, i, len; |
| 975 |
gfc_char_t ch; |
| 976 |
|
| 977 |
if (e->expr_type != EXPR_CONSTANT) |
| 978 |
return NULL; |
| 979 |
|
| 980 |
len = e->value.character.length; |
| 981 |
|
| 982 |
for (count = 0, i = len - 1; i >= 0; --i) |
| 983 |
{ |
| 984 |
ch = e->value.character.string[i]; |
| 985 |
if (ch != ' ') |
| 986 |
break; |
| 987 |
++count; |
| 988 |
} |
| 989 |
|
| 990 |
result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, len); |
| 991 |
for (i = 0; i < count; ++i) |
| 992 |
result->value.character.string[i] = ' '; |
| 993 |
|
| 994 |
for (i = count; i < len; ++i) |
| 995 |
result->value.character.string[i] = e->value.character.string[i - count]; |
| 996 |
|
| 997 |
return result; |
| 998 |
} |
| 999 |
|
| 1000 |
|
| 1001 |
gfc_expr * |
| 1002 |
gfc_simplify_aimag (gfc_expr *e) |
| 1003 |
{ |
| 1004 |
gfc_expr *result; |
| 1005 |
|
| 1006 |
if (e->expr_type != EXPR_CONSTANT) |
| 1007 |
return NULL; |
| 1008 |
|
| 1009 |
result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where); |
| 1010 |
mpfr_set (result->value.real, mpc_imagref (e->value.complex), GFC_RND_MODE); |
| 1011 |
|
| 1012 |
return range_check (result, "AIMAG"); |
| 1013 |
} |
| 1014 |
|
| 1015 |
|
| 1016 |
gfc_expr * |
| 1017 |
gfc_simplify_aint (gfc_expr *e, gfc_expr *k) |
| 1018 |
{ |
| 1019 |
gfc_expr *rtrunc, *result; |
| 1020 |
int kind; |
| 1021 |
|
| 1022 |
kind = get_kind (BT_REAL, k, "AINT", e->ts.kind); |
| 1023 |
if (kind == -1) |
| 1024 |
return &gfc_bad_expr; |
| 1025 |
|
| 1026 |
if (e->expr_type != EXPR_CONSTANT) |
| 1027 |
return NULL; |
| 1028 |
|
| 1029 |
rtrunc = gfc_copy_expr (e); |
| 1030 |
mpfr_trunc (rtrunc->value.real, e->value.real); |
| 1031 |
|
| 1032 |
result = gfc_real2real (rtrunc, kind); |
| 1033 |
|
| 1034 |
gfc_free_expr (rtrunc); |
| 1035 |
|
| 1036 |
return range_check (result, "AINT"); |
| 1037 |
} |
| 1038 |
|
| 1039 |
|
| 1040 |
gfc_expr * |
| 1041 |
gfc_simplify_all (gfc_expr *mask, gfc_expr *dim) |
| 1042 |
{ |
| 1043 |
return simplify_transformation (mask, dim, NULL, true, gfc_and); |
| 1044 |
} |
| 1045 |
|
| 1046 |
|
| 1047 |
gfc_expr * |
| 1048 |
gfc_simplify_dint (gfc_expr *e) |
| 1049 |
{ |
| 1050 |
gfc_expr *rtrunc, *result; |
| 1051 |
|
| 1052 |
if (e->expr_type != EXPR_CONSTANT) |
| 1053 |
return NULL; |
| 1054 |
|
| 1055 |
rtrunc = gfc_copy_expr (e); |
| 1056 |
mpfr_trunc (rtrunc->value.real, e->value.real); |
| 1057 |
|
| 1058 |
result = gfc_real2real (rtrunc, gfc_default_double_kind); |
| 1059 |
|
| 1060 |
gfc_free_expr (rtrunc); |
| 1061 |
|
| 1062 |
return range_check (result, "DINT"); |
| 1063 |
} |
| 1064 |
|
| 1065 |
|
| 1066 |
gfc_expr * |
| 1067 |
gfc_simplify_dreal (gfc_expr *e) |
| 1068 |
{ |
| 1069 |
gfc_expr *result = NULL; |
| 1070 |
|
| 1071 |
if (e->expr_type != EXPR_CONSTANT) |
| 1072 |
return NULL; |
| 1073 |
|
| 1074 |
result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where); |
| 1075 |
mpc_real (result->value.real, e->value.complex, GFC_RND_MODE); |
| 1076 |
|
| 1077 |
return range_check (result, "DREAL"); |
| 1078 |
} |
| 1079 |
|
| 1080 |
|
| 1081 |
gfc_expr * |
| 1082 |
gfc_simplify_anint (gfc_expr *e, gfc_expr *k) |
| 1083 |
{ |
| 1084 |
gfc_expr *result; |
| 1085 |
int kind; |
| 1086 |
|
| 1087 |
kind = get_kind (BT_REAL, k, "ANINT", e->ts.kind); |
| 1088 |
if (kind == -1) |
| 1089 |
return &gfc_bad_expr; |
| 1090 |
|
| 1091 |
if (e->expr_type != EXPR_CONSTANT) |
| 1092 |
return NULL; |
| 1093 |
|
| 1094 |
result = gfc_get_constant_expr (e->ts.type, kind, &e->where); |
| 1095 |
mpfr_round (result->value.real, e->value.real); |
| 1096 |
|
| 1097 |
return range_check (result, "ANINT"); |
| 1098 |
} |
| 1099 |
|
| 1100 |
|
| 1101 |
gfc_expr * |
| 1102 |
gfc_simplify_and (gfc_expr *x, gfc_expr *y) |
| 1103 |
{ |
| 1104 |
gfc_expr *result; |
| 1105 |
int kind; |
| 1106 |
|
| 1107 |
if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT) |
| 1108 |
return NULL; |
| 1109 |
|
| 1110 |
kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind; |
| 1111 |
|
| 1112 |
switch (x->ts.type) |
| 1113 |
{ |
| 1114 |
case BT_INTEGER: |
| 1115 |
result = gfc_get_constant_expr (BT_INTEGER, kind, &x->where); |
| 1116 |
mpz_and (result->value.integer, x->value.integer, y->value.integer); |
| 1117 |
return range_check (result, "AND"); |
| 1118 |
|
| 1119 |
case BT_LOGICAL: |
| 1120 |
return gfc_get_logical_expr (kind, &x->where, |
| 1121 |
x->value.logical && y->value.logical); |
| 1122 |
|
| 1123 |
default: |
| 1124 |
gcc_unreachable (); |
| 1125 |
} |
| 1126 |
} |
| 1127 |
|
| 1128 |
|
| 1129 |
gfc_expr * |
| 1130 |
gfc_simplify_any (gfc_expr *mask, gfc_expr *dim) |
| 1131 |
{ |
| 1132 |
return simplify_transformation (mask, dim, NULL, false, gfc_or); |
| 1133 |
} |
| 1134 |
|
| 1135 |
|
| 1136 |
gfc_expr * |
| 1137 |
gfc_simplify_dnint (gfc_expr *e) |
| 1138 |
{ |
| 1139 |
gfc_expr *result; |
| 1140 |
|
| 1141 |
if (e->expr_type != EXPR_CONSTANT) |
| 1142 |
return NULL; |
| 1143 |
|
| 1144 |
result = gfc_get_constant_expr (BT_REAL, gfc_default_double_kind, &e->where); |
| 1145 |
mpfr_round (result->value.real, e->value.real); |
| 1146 |
|
| 1147 |
return range_check (result, "DNINT"); |
| 1148 |
} |
| 1149 |
|
| 1150 |
|
| 1151 |
gfc_expr * |
| 1152 |
gfc_simplify_asin (gfc_expr *x) |
| 1153 |
{ |
| 1154 |
gfc_expr *result; |
| 1155 |
|
| 1156 |
if (x->expr_type != EXPR_CONSTANT) |
| 1157 |
return NULL; |
| 1158 |
|
| 1159 |
switch (x->ts.type) |
| 1160 |
{ |
| 1161 |
case BT_REAL: |
| 1162 |
if (mpfr_cmp_si (x->value.real, 1) > 0 |
| 1163 |
|| mpfr_cmp_si (x->value.real, -1) < 0) |
| 1164 |
{ |
| 1165 |
gfc_error ("Argument of ASIN at %L must be between -1 and 1", |
| 1166 |
&x->where); |
| 1167 |
return &gfc_bad_expr; |
| 1168 |
} |
| 1169 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 1170 |
mpfr_asin (result->value.real, x->value.real, GFC_RND_MODE); |
| 1171 |
break; |
| 1172 |
|
| 1173 |
case BT_COMPLEX: |
| 1174 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 1175 |
mpc_asin (result->value.complex, x->value.complex, GFC_MPC_RND_MODE); |
| 1176 |
break; |
| 1177 |
|
| 1178 |
default: |
| 1179 |
gfc_internal_error ("in gfc_simplify_asin(): Bad type"); |
| 1180 |
} |
| 1181 |
|
| 1182 |
return range_check (result, "ASIN"); |
| 1183 |
} |
| 1184 |
|
| 1185 |
|
| 1186 |
/* Convert radians to degrees, i.e., x * 180 / pi. */ |
| 1187 |
|
| 1188 |
static void |
| 1189 |
rad2deg (mpfr_t x) |
| 1190 |
{ |
| 1191 |
mpfr_t tmp; |
| 1192 |
|
| 1193 |
mpfr_init (tmp); |
| 1194 |
mpfr_const_pi (tmp, GFC_RND_MODE); |
| 1195 |
mpfr_mul_ui (x, x, 180, GFC_RND_MODE); |
| 1196 |
mpfr_div (x, x, tmp, GFC_RND_MODE); |
| 1197 |
mpfr_clear (tmp); |
| 1198 |
} |
| 1199 |
|
| 1200 |
|
| 1201 |
/* Simplify ACOSD(X) where the returned value has units of degree. */ |
| 1202 |
|
| 1203 |
gfc_expr * |
| 1204 |
gfc_simplify_acosd (gfc_expr *x) |
| 1205 |
{ |
| 1206 |
gfc_expr *result; |
| 1207 |
|
| 1208 |
if (x->expr_type != EXPR_CONSTANT) |
| 1209 |
return NULL; |
| 1210 |
|
| 1211 |
if (mpfr_cmp_si (x->value.real, 1) > 0 |
| 1212 |
|| mpfr_cmp_si (x->value.real, -1) < 0) |
| 1213 |
{ |
| 1214 |
gfc_error ("Argument of ACOSD at %L must be between -1 and 1", |
| 1215 |
&x->where); |
| 1216 |
return &gfc_bad_expr; |
| 1217 |
} |
| 1218 |
|
| 1219 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 1220 |
mpfr_acos (result->value.real, x->value.real, GFC_RND_MODE); |
| 1221 |
rad2deg (result->value.real); |
| 1222 |
|
| 1223 |
return range_check (result, "ACOSD"); |
| 1224 |
} |
| 1225 |
|
| 1226 |
|
| 1227 |
/* Simplify asind (x) where the returned value has units of degree. */ |
| 1228 |
|
| 1229 |
gfc_expr * |
| 1230 |
gfc_simplify_asind (gfc_expr *x) |
| 1231 |
{ |
| 1232 |
gfc_expr *result; |
| 1233 |
|
| 1234 |
if (x->expr_type != EXPR_CONSTANT) |
| 1235 |
return NULL; |
| 1236 |
|
| 1237 |
if (mpfr_cmp_si (x->value.real, 1) > 0 |
| 1238 |
|| mpfr_cmp_si (x->value.real, -1) < 0) |
| 1239 |
{ |
| 1240 |
gfc_error ("Argument of ASIND at %L must be between -1 and 1", |
| 1241 |
&x->where); |
| 1242 |
return &gfc_bad_expr; |
| 1243 |
} |
| 1244 |
|
| 1245 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 1246 |
mpfr_asin (result->value.real, x->value.real, GFC_RND_MODE); |
| 1247 |
rad2deg (result->value.real); |
| 1248 |
|
| 1249 |
return range_check (result, "ASIND"); |
| 1250 |
} |
| 1251 |
|
| 1252 |
|
| 1253 |
/* Simplify atand (x) where the returned value has units of degree. */ |
| 1254 |
|
| 1255 |
gfc_expr * |
| 1256 |
gfc_simplify_atand (gfc_expr *x) |
| 1257 |
{ |
| 1258 |
gfc_expr *result; |
| 1259 |
|
| 1260 |
if (x->expr_type != EXPR_CONSTANT) |
| 1261 |
return NULL; |
| 1262 |
|
| 1263 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 1264 |
mpfr_atan (result->value.real, x->value.real, GFC_RND_MODE); |
| 1265 |
rad2deg (result->value.real); |
| 1266 |
|
| 1267 |
return range_check (result, "ATAND"); |
| 1268 |
} |
| 1269 |
|
| 1270 |
|
| 1271 |
gfc_expr * |
| 1272 |
gfc_simplify_asinh (gfc_expr *x) |
| 1273 |
{ |
| 1274 |
gfc_expr *result; |
| 1275 |
|
| 1276 |
if (x->expr_type != EXPR_CONSTANT) |
| 1277 |
return NULL; |
| 1278 |
|
| 1279 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 1280 |
|
| 1281 |
switch (x->ts.type) |
| 1282 |
{ |
| 1283 |
case BT_REAL: |
| 1284 |
mpfr_asinh (result->value.real, x->value.real, GFC_RND_MODE); |
| 1285 |
break; |
| 1286 |
|
| 1287 |
case BT_COMPLEX: |
| 1288 |
mpc_asinh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE); |
| 1289 |
break; |
| 1290 |
|
| 1291 |
default: |
| 1292 |
gfc_internal_error ("in gfc_simplify_asinh(): Bad type"); |
| 1293 |
} |
| 1294 |
|
| 1295 |
return range_check (result, "ASINH"); |
| 1296 |
} |
| 1297 |
|
| 1298 |
|
| 1299 |
gfc_expr * |
| 1300 |
gfc_simplify_atan (gfc_expr *x) |
| 1301 |
{ |
| 1302 |
gfc_expr *result; |
| 1303 |
|
| 1304 |
if (x->expr_type != EXPR_CONSTANT) |
| 1305 |
return NULL; |
| 1306 |
|
| 1307 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 1308 |
|
| 1309 |
switch (x->ts.type) |
| 1310 |
{ |
| 1311 |
case BT_REAL: |
| 1312 |
mpfr_atan (result->value.real, x->value.real, GFC_RND_MODE); |
| 1313 |
break; |
| 1314 |
|
| 1315 |
case BT_COMPLEX: |
| 1316 |
mpc_atan (result->value.complex, x->value.complex, GFC_MPC_RND_MODE); |
| 1317 |
break; |
| 1318 |
|
| 1319 |
default: |
| 1320 |
gfc_internal_error ("in gfc_simplify_atan(): Bad type"); |
| 1321 |
} |
| 1322 |
|
| 1323 |
return range_check (result, "ATAN"); |
| 1324 |
} |
| 1325 |
|
| 1326 |
|
| 1327 |
gfc_expr * |
| 1328 |
gfc_simplify_atanh (gfc_expr *x) |
| 1329 |
{ |
| 1330 |
gfc_expr *result; |
| 1331 |
|
| 1332 |
if (x->expr_type != EXPR_CONSTANT) |
| 1333 |
return NULL; |
| 1334 |
|
| 1335 |
switch (x->ts.type) |
| 1336 |
{ |
| 1337 |
case BT_REAL: |
| 1338 |
if (mpfr_cmp_si (x->value.real, 1) >= 0 |
| 1339 |
|| mpfr_cmp_si (x->value.real, -1) <= 0) |
| 1340 |
{ |
| 1341 |
gfc_error ("Argument of ATANH at %L must be inside the range -1 " |
| 1342 |
"to 1", &x->where); |
| 1343 |
return &gfc_bad_expr; |
| 1344 |
} |
| 1345 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 1346 |
mpfr_atanh (result->value.real, x->value.real, GFC_RND_MODE); |
| 1347 |
break; |
| 1348 |
|
| 1349 |
case BT_COMPLEX: |
| 1350 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 1351 |
mpc_atanh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE); |
| 1352 |
break; |
| 1353 |
|
| 1354 |
default: |
| 1355 |
gfc_internal_error ("in gfc_simplify_atanh(): Bad type"); |
| 1356 |
} |
| 1357 |
|
| 1358 |
return range_check (result, "ATANH"); |
| 1359 |
} |
| 1360 |
|
| 1361 |
|
| 1362 |
gfc_expr * |
| 1363 |
gfc_simplify_atan2 (gfc_expr *y, gfc_expr *x) |
| 1364 |
{ |
| 1365 |
gfc_expr *result; |
| 1366 |
|
| 1367 |
if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT) |
| 1368 |
return NULL; |
| 1369 |
|
| 1370 |
if (mpfr_zero_p (y->value.real) && mpfr_zero_p (x->value.real)) |
| 1371 |
{ |
| 1372 |
gfc_error ("If first argument of ATAN2 at %L is zero, then the " |
| 1373 |
"second argument must not be zero", &y->where); |
| 1374 |
return &gfc_bad_expr; |
| 1375 |
} |
| 1376 |
|
| 1377 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 1378 |
mpfr_atan2 (result->value.real, y->value.real, x->value.real, GFC_RND_MODE); |
| 1379 |
|
| 1380 |
return range_check (result, "ATAN2"); |
| 1381 |
} |
| 1382 |
|
| 1383 |
|
| 1384 |
gfc_expr * |
| 1385 |
gfc_simplify_bessel_j0 (gfc_expr *x) |
| 1386 |
{ |
| 1387 |
gfc_expr *result; |
| 1388 |
|
| 1389 |
if (x->expr_type != EXPR_CONSTANT) |
| 1390 |
return NULL; |
| 1391 |
|
| 1392 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 1393 |
mpfr_j0 (result->value.real, x->value.real, GFC_RND_MODE); |
| 1394 |
|
| 1395 |
return range_check (result, "BESSEL_J0"); |
| 1396 |
} |
| 1397 |
|
| 1398 |
|
| 1399 |
gfc_expr * |
| 1400 |
gfc_simplify_bessel_j1 (gfc_expr *x) |
| 1401 |
{ |
| 1402 |
gfc_expr *result; |
| 1403 |
|
| 1404 |
if (x->expr_type != EXPR_CONSTANT) |
| 1405 |
return NULL; |
| 1406 |
|
| 1407 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 1408 |
mpfr_j1 (result->value.real, x->value.real, GFC_RND_MODE); |
| 1409 |
|
| 1410 |
return range_check (result, "BESSEL_J1"); |
| 1411 |
} |
| 1412 |
|
| 1413 |
|
| 1414 |
gfc_expr * |
| 1415 |
gfc_simplify_bessel_jn (gfc_expr *order, gfc_expr *x) |
| 1416 |
{ |
| 1417 |
gfc_expr *result; |
| 1418 |
long n; |
| 1419 |
|
| 1420 |
if (x->expr_type != EXPR_CONSTANT || order->expr_type != EXPR_CONSTANT) |
| 1421 |
return NULL; |
| 1422 |
|
| 1423 |
n = mpz_get_si (order->value.integer); |
| 1424 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 1425 |
mpfr_jn (result->value.real, n, x->value.real, GFC_RND_MODE); |
| 1426 |
|
| 1427 |
return range_check (result, "BESSEL_JN"); |
| 1428 |
} |
| 1429 |
|
| 1430 |
|
| 1431 |
/* Simplify transformational form of JN and YN. */ |
| 1432 |
|
| 1433 |
static gfc_expr * |
| 1434 |
gfc_simplify_bessel_n2 (gfc_expr *order1, gfc_expr *order2, gfc_expr *x, |
| 1435 |
bool jn) |
| 1436 |
{ |
| 1437 |
gfc_expr *result; |
| 1438 |
gfc_expr *e; |
| 1439 |
long n1, n2; |
| 1440 |
int i; |
| 1441 |
mpfr_t x2rev, last1, last2; |
| 1442 |
|
| 1443 |
if (x->expr_type != EXPR_CONSTANT || order1->expr_type != EXPR_CONSTANT |
| 1444 |
|| order2->expr_type != EXPR_CONSTANT) |
| 1445 |
return NULL; |
| 1446 |
|
| 1447 |
n1 = mpz_get_si (order1->value.integer); |
| 1448 |
n2 = mpz_get_si (order2->value.integer); |
| 1449 |
result = gfc_get_array_expr (x->ts.type, x->ts.kind, &x->where); |
| 1450 |
result->rank = 1; |
| 1451 |
result->shape = gfc_get_shape (1); |
| 1452 |
mpz_init_set_ui (result->shape[0], MAX (n2-n1+1, 0)); |
| 1453 |
|
| 1454 |
if (n2 < n1) |
| 1455 |
return result; |
| 1456 |
|
| 1457 |
/* Special case: x == 0; it is J0(0.0) == 1, JN(N > 0, 0.0) == 0; and |
| 1458 |
YN(N, 0.0) = -Inf. */ |
| 1459 |
|
| 1460 |
if (mpfr_cmp_ui (x->value.real, 0.0) == 0) |
| 1461 |
{ |
| 1462 |
if (!jn && flag_range_check) |
| 1463 |
{ |
| 1464 |
gfc_error ("Result of BESSEL_YN is -INF at %L", &result->where); |
| 1465 |
gfc_free_expr (result); |
| 1466 |
return &gfc_bad_expr; |
| 1467 |
} |
| 1468 |
|
| 1469 |
if (jn && n1 == 0) |
| 1470 |
{ |
| 1471 |
e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 1472 |
mpfr_set_ui (e->value.real, 1, GFC_RND_MODE); |
| 1473 |
gfc_constructor_append_expr (&result->value.constructor, e, |
| 1474 |
&x->where); |
| 1475 |
n1++; |
| 1476 |
} |
| 1477 |
|
| 1478 |
for (i = n1; i <= n2; i++) |
| 1479 |
{ |
| 1480 |
e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 1481 |
if (jn) |
| 1482 |
mpfr_set_ui (e->value.real, 0, GFC_RND_MODE); |
| 1483 |
else |
| 1484 |
mpfr_set_inf (e->value.real, -1); |
| 1485 |
gfc_constructor_append_expr (&result->value.constructor, e, |
| 1486 |
&x->where); |
| 1487 |
} |
| 1488 |
|
| 1489 |
return result; |
| 1490 |
} |
| 1491 |
|
| 1492 |
/* Use the faster but more verbose recurrence algorithm. Bessel functions |
| 1493 |
are stable for downward recursion and Neumann functions are stable |
| 1494 |
for upward recursion. It is |
| 1495 |
x2rev = 2.0/x, |
| 1496 |
J(N-1, x) = x2rev * N * J(N, x) - J(N+1, x), |
| 1497 |
Y(N+1, x) = x2rev * N * Y(N, x) - Y(N-1, x). |
| 1498 |
Cf. http://dlmf.nist.gov/10.74#iv and http://dlmf.nist.gov/10.6#E1 */ |
| 1499 |
|
| 1500 |
gfc_set_model_kind (x->ts.kind); |
| 1501 |
|
| 1502 |
/* Get first recursion anchor. */ |
| 1503 |
|
| 1504 |
mpfr_init (last1); |
| 1505 |
if (jn) |
| 1506 |
mpfr_jn (last1, n2, x->value.real, GFC_RND_MODE); |
| 1507 |
else |
| 1508 |
mpfr_yn (last1, n1, x->value.real, GFC_RND_MODE); |
| 1509 |
|
| 1510 |
e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 1511 |
mpfr_set (e->value.real, last1, GFC_RND_MODE); |
| 1512 |
if (range_check (e, jn ? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr) |
| 1513 |
{ |
| 1514 |
mpfr_clear (last1); |
| 1515 |
gfc_free_expr (e); |
| 1516 |
gfc_free_expr (result); |
| 1517 |
return &gfc_bad_expr; |
| 1518 |
} |
| 1519 |
gfc_constructor_append_expr (&result->value.constructor, e, &x->where); |
| 1520 |
|
| 1521 |
if (n1 == n2) |
| 1522 |
{ |
| 1523 |
mpfr_clear (last1); |
| 1524 |
return result; |
| 1525 |
} |
| 1526 |
|
| 1527 |
/* Get second recursion anchor. */ |
| 1528 |
|
| 1529 |
mpfr_init (last2); |
| 1530 |
if (jn) |
| 1531 |
mpfr_jn (last2, n2-1, x->value.real, GFC_RND_MODE); |
| 1532 |
else |
| 1533 |
mpfr_yn (last2, n1+1, x->value.real, GFC_RND_MODE); |
| 1534 |
|
| 1535 |
e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 1536 |
mpfr_set (e->value.real, last2, GFC_RND_MODE); |
| 1537 |
if (range_check (e, jn ? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr) |
| 1538 |
{ |
| 1539 |
mpfr_clear (last1); |
| 1540 |
mpfr_clear (last2); |
| 1541 |
gfc_free_expr (e); |
| 1542 |
gfc_free_expr (result); |
| 1543 |
return &gfc_bad_expr; |
| 1544 |
} |
| 1545 |
if (jn) |
| 1546 |
gfc_constructor_insert_expr (&result->value.constructor, e, &x->where, -2); |
| 1547 |
else |
| 1548 |
gfc_constructor_append_expr (&result->value.constructor, e, &x->where); |
| 1549 |
|
| 1550 |
if (n1 + 1 == n2) |
| 1551 |
{ |
| 1552 |
mpfr_clear (last1); |
| 1553 |
mpfr_clear (last2); |
| 1554 |
return result; |
| 1555 |
} |
| 1556 |
|
| 1557 |
/* Start actual recursion. */ |
| 1558 |
|
| 1559 |
mpfr_init (x2rev); |
| 1560 |
mpfr_ui_div (x2rev, 2, x->value.real, GFC_RND_MODE); |
| 1561 |
|
| 1562 |
for (i = 2; i <= n2-n1; i++) |
| 1563 |
{ |
| 1564 |
e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 1565 |
|
| 1566 |
/* Special case: For YN, if the previous N gave -INF, set |
| 1567 |
also N+1 to -INF. */ |
| 1568 |
if (!jn && !flag_range_check && mpfr_inf_p (last2)) |
| 1569 |
{ |
| 1570 |
mpfr_set_inf (e->value.real, -1); |
| 1571 |
gfc_constructor_append_expr (&result->value.constructor, e, |
| 1572 |
&x->where); |
| 1573 |
continue; |
| 1574 |
} |
| 1575 |
|
| 1576 |
mpfr_mul_si (e->value.real, x2rev, jn ? (n2-i+1) : (n1+i-1), |
| 1577 |
GFC_RND_MODE); |
| 1578 |
mpfr_mul (e->value.real, e->value.real, last2, GFC_RND_MODE); |
| 1579 |
mpfr_sub (e->value.real, e->value.real, last1, GFC_RND_MODE); |
| 1580 |
|
| 1581 |
if (range_check (e, jn ? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr) |
| 1582 |
{ |
| 1583 |
/* Range_check frees "e" in that case. */ |
| 1584 |
e = NULL; |
| 1585 |
goto error; |
| 1586 |
} |
| 1587 |
|
| 1588 |
if (jn) |
| 1589 |
gfc_constructor_insert_expr (&result->value.constructor, e, &x->where, |
| 1590 |
-i-1); |
| 1591 |
else |
| 1592 |
gfc_constructor_append_expr (&result->value.constructor, e, &x->where); |
| 1593 |
|
| 1594 |
mpfr_set (last1, last2, GFC_RND_MODE); |
| 1595 |
mpfr_set (last2, e->value.real, GFC_RND_MODE); |
| 1596 |
} |
| 1597 |
|
| 1598 |
mpfr_clear (last1); |
| 1599 |
mpfr_clear (last2); |
| 1600 |
mpfr_clear (x2rev); |
| 1601 |
return result; |
| 1602 |
|
| 1603 |
error: |
| 1604 |
mpfr_clear (last1); |
| 1605 |
mpfr_clear (last2); |
| 1606 |
mpfr_clear (x2rev); |
| 1607 |
gfc_free_expr (e); |
| 1608 |
gfc_free_expr (result); |
| 1609 |
return &gfc_bad_expr; |
| 1610 |
} |
| 1611 |
|
| 1612 |
|
| 1613 |
gfc_expr * |
| 1614 |
gfc_simplify_bessel_jn2 (gfc_expr *order1, gfc_expr *order2, gfc_expr *x) |
| 1615 |
{ |
| 1616 |
return gfc_simplify_bessel_n2 (order1, order2, x, true); |
| 1617 |
} |
| 1618 |
|
| 1619 |
|
| 1620 |
gfc_expr * |
| 1621 |
gfc_simplify_bessel_y0 (gfc_expr *x) |
| 1622 |
{ |
| 1623 |
gfc_expr *result; |
| 1624 |
|
| 1625 |
if (x->expr_type != EXPR_CONSTANT) |
| 1626 |
return NULL; |
| 1627 |
|
| 1628 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 1629 |
mpfr_y0 (result->value.real, x->value.real, GFC_RND_MODE); |
| 1630 |
|
| 1631 |
return range_check (result, "BESSEL_Y0"); |
| 1632 |
} |
| 1633 |
|
| 1634 |
|
| 1635 |
gfc_expr * |
| 1636 |
gfc_simplify_bessel_y1 (gfc_expr *x) |
| 1637 |
{ |
| 1638 |
gfc_expr *result; |
| 1639 |
|
| 1640 |
if (x->expr_type != EXPR_CONSTANT) |
| 1641 |
return NULL; |
| 1642 |
|
| 1643 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 1644 |
mpfr_y1 (result->value.real, x->value.real, GFC_RND_MODE); |
| 1645 |
|
| 1646 |
return range_check (result, "BESSEL_Y1"); |
| 1647 |
} |
| 1648 |
|
| 1649 |
|
| 1650 |
gfc_expr * |
| 1651 |
gfc_simplify_bessel_yn (gfc_expr *order, gfc_expr *x) |
| 1652 |
{ |
| 1653 |
gfc_expr *result; |
| 1654 |
long n; |
| 1655 |
|
| 1656 |
if (x->expr_type != EXPR_CONSTANT || order->expr_type != EXPR_CONSTANT) |
| 1657 |
return NULL; |
| 1658 |
|
| 1659 |
n = mpz_get_si (order->value.integer); |
| 1660 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 1661 |
mpfr_yn (result->value.real, n, x->value.real, GFC_RND_MODE); |
| 1662 |
|
| 1663 |
return range_check (result, "BESSEL_YN"); |
| 1664 |
} |
| 1665 |
|
| 1666 |
|
| 1667 |
gfc_expr * |
| 1668 |
gfc_simplify_bessel_yn2 (gfc_expr *order1, gfc_expr *order2, gfc_expr *x) |
| 1669 |
{ |
| 1670 |
return gfc_simplify_bessel_n2 (order1, order2, x, false); |
| 1671 |
} |
| 1672 |
|
| 1673 |
|
| 1674 |
gfc_expr * |
| 1675 |
gfc_simplify_bit_size (gfc_expr *e) |
| 1676 |
{ |
| 1677 |
int i = gfc_validate_kind (e->ts.type, e->ts.kind, false); |
| 1678 |
int bit_size; |
| 1679 |
|
| 1680 |
if (flag_unsigned && e->ts.type == BT_UNSIGNED) |
| 1681 |
bit_size = gfc_unsigned_kinds[i].bit_size; |
| 1682 |
else |
| 1683 |
bit_size = gfc_integer_kinds[i].bit_size; |
| 1684 |
|
| 1685 |
return gfc_get_int_expr (e->ts.kind, &e->where, bit_size); |
| 1686 |
} |
| 1687 |
|
| 1688 |
|
| 1689 |
gfc_expr * |
| 1690 |
gfc_simplify_btest (gfc_expr *e, gfc_expr *bit) |
| 1691 |
{ |
| 1692 |
int b; |
| 1693 |
|
| 1694 |
if (e->expr_type != EXPR_CONSTANT || bit->expr_type != EXPR_CONSTANT) |
| 1695 |
return NULL; |
| 1696 |
|
| 1697 |
if (!gfc_check_bitfcn (e, bit)) |
| 1698 |
return &gfc_bad_expr; |
| 1699 |
|
| 1700 |
if (gfc_extract_int (bit, &b) || b < 0) |
| 1701 |
return gfc_get_logical_expr (gfc_default_logical_kind, &e->where, false); |
| 1702 |
|
| 1703 |
return gfc_get_logical_expr (gfc_default_logical_kind, &e->where, |
| 1704 |
mpz_tstbit (e->value.integer, b)); |
| 1705 |
} |
| 1706 |
|
| 1707 |
|
| 1708 |
static int |
| 1709 |
compare_bitwise (gfc_expr *i, gfc_expr *j) |
| 1710 |
{ |
| 1711 |
mpz_t x, y; |
| 1712 |
int k, res; |
| 1713 |
|
| 1714 |
gcc_assert (i->ts.type == BT_INTEGER); |
| 1715 |
gcc_assert (j->ts.type == BT_INTEGER); |
| 1716 |
|
| 1717 |
mpz_init_set (x, i->value.integer); |
| 1718 |
k = gfc_validate_kind (i->ts.type, i->ts.kind, false); |
| 1719 |
gfc_convert_mpz_to_unsigned (x, gfc_integer_kinds[k].bit_size); |
| 1720 |
|
| 1721 |
mpz_init_set (y, j->value.integer); |
| 1722 |
k = gfc_validate_kind (j->ts.type, j->ts.kind, false); |
| 1723 |
gfc_convert_mpz_to_unsigned (y, gfc_integer_kinds[k].bit_size); |
| 1724 |
|
| 1725 |
res = mpz_cmp (x, y); |
| 1726 |
mpz_clear (x); |
| 1727 |
mpz_clear (y); |
| 1728 |
return res; |
| 1729 |
} |
| 1730 |
|
| 1731 |
|
| 1732 |
gfc_expr * |
| 1733 |
gfc_simplify_bge (gfc_expr *i, gfc_expr *j) |
| 1734 |
{ |
| 1735 |
bool result; |
| 1736 |
|
| 1737 |
if (i->expr_type != EXPR_CONSTANT || j->expr_type != EXPR_CONSTANT) |
| 1738 |
return NULL; |
| 1739 |
|
| 1740 |
if (flag_unsigned && i->ts.type == BT_UNSIGNED) |
| 1741 |
result = mpz_cmp (i->value.integer, j->value.integer) >= 0; |
| 1742 |
else |
| 1743 |
result = compare_bitwise (i, j) >= 0; |
| 1744 |
|
| 1745 |
return gfc_get_logical_expr (gfc_default_logical_kind, &i->where, |
| 1746 |
result); |
| 1747 |
} |
| 1748 |
|
| 1749 |
|
| 1750 |
gfc_expr * |
| 1751 |
gfc_simplify_bgt (gfc_expr *i, gfc_expr *j) |
| 1752 |
{ |
| 1753 |
bool result; |
| 1754 |
|
| 1755 |
if (i->expr_type != EXPR_CONSTANT || j->expr_type != EXPR_CONSTANT) |
| 1756 |
return NULL; |
| 1757 |
|
| 1758 |
if (flag_unsigned && i->ts.type == BT_UNSIGNED) |
| 1759 |
result = mpz_cmp (i->value.integer, j->value.integer) > 0; |
| 1760 |
else |
| 1761 |
result = compare_bitwise (i, j) > 0; |
| 1762 |
|
| 1763 |
return gfc_get_logical_expr (gfc_default_logical_kind, &i->where, |
| 1764 |
result); |
| 1765 |
} |
| 1766 |
|
| 1767 |
|
| 1768 |
gfc_expr * |
| 1769 |
gfc_simplify_ble (gfc_expr *i, gfc_expr *j) |
| 1770 |
{ |
| 1771 |
bool result; |
| 1772 |
|
| 1773 |
if (i->expr_type != EXPR_CONSTANT || j->expr_type != EXPR_CONSTANT) |
| 1774 |
return NULL; |
| 1775 |
|
| 1776 |
if (flag_unsigned && i->ts.type == BT_UNSIGNED) |
| 1777 |
result = mpz_cmp (i->value.integer, j->value.integer) <= 0; |
| 1778 |
else |
| 1779 |
result = compare_bitwise (i, j) <= 0; |
| 1780 |
|
| 1781 |
return gfc_get_logical_expr (gfc_default_logical_kind, &i->where, |
| 1782 |
result); |
| 1783 |
} |
| 1784 |
|
| 1785 |
|
| 1786 |
gfc_expr * |
| 1787 |
gfc_simplify_blt (gfc_expr *i, gfc_expr *j) |
| 1788 |
{ |
| 1789 |
bool result; |
| 1790 |
|
| 1791 |
if (i->expr_type != EXPR_CONSTANT || j->expr_type != EXPR_CONSTANT) |
| 1792 |
return NULL; |
| 1793 |
|
| 1794 |
if (flag_unsigned && i->ts.type == BT_UNSIGNED) |
| 1795 |
result = mpz_cmp (i->value.integer, j->value.integer) < 0; |
| 1796 |
else |
| 1797 |
result = compare_bitwise (i, j) < 0; |
| 1798 |
|
| 1799 |
return gfc_get_logical_expr (gfc_default_logical_kind, &i->where, |
| 1800 |
result); |
| 1801 |
} |
| 1802 |
|
| 1803 |
gfc_expr * |
| 1804 |
gfc_simplify_ceiling (gfc_expr *e, gfc_expr *k) |
| 1805 |
{ |
| 1806 |
gfc_expr *ceil, *result; |
| 1807 |
int kind; |
| 1808 |
|
| 1809 |
kind = get_kind (BT_INTEGER, k, "CEILING", gfc_default_integer_kind); |
| 1810 |
if (kind == -1) |
| 1811 |
return &gfc_bad_expr; |
| 1812 |
|
| 1813 |
if (e->expr_type != EXPR_CONSTANT) |
| 1814 |
return NULL; |
| 1815 |
|
| 1816 |
ceil = gfc_copy_expr (e); |
| 1817 |
mpfr_ceil (ceil->value.real, e->value.real); |
| 1818 |
|
| 1819 |
result = gfc_get_constant_expr (BT_INTEGER, kind, &e->where); |
| 1820 |
gfc_mpfr_to_mpz (result->value.integer, ceil->value.real, &e->where); |
| 1821 |
|
| 1822 |
gfc_free_expr (ceil); |
| 1823 |
|
| 1824 |
return range_check (result, "CEILING"); |
| 1825 |
} |
| 1826 |
|
| 1827 |
|
| 1828 |
gfc_expr * |
| 1829 |
gfc_simplify_char (gfc_expr *e, gfc_expr *k) |
| 1830 |
{ |
| 1831 |
return simplify_achar_char (e, k, "CHAR", false); |
| 1832 |
} |
| 1833 |
|
| 1834 |
|
| 1835 |
/* Common subroutine for simplifying CMPLX, COMPLEX and DCMPLX. */ |
| 1836 |
|
| 1837 |
static gfc_expr * |
| 1838 |
simplify_cmplx (const char *name, gfc_expr *x, gfc_expr *y, int kind) |
| 1839 |
{ |
| 1840 |
gfc_expr *result; |
| 1841 |
|
| 1842 |
if (x->expr_type != EXPR_CONSTANT |
| 1843 |
|| (y != NULL && y->expr_type != EXPR_CONSTANT)) |
| 1844 |
return NULL; |
| 1845 |
|
| 1846 |
result = gfc_get_constant_expr (BT_COMPLEX, kind, &x->where); |
| 1847 |
|
| 1848 |
switch (x->ts.type) |
| 1849 |
{ |
| 1850 |
case BT_INTEGER: |
| 1851 |
case BT_UNSIGNED: |
| 1852 |
mpc_set_z (result->value.complex, x->value.integer, GFC_MPC_RND_MODE); |
| 1853 |
break; |
| 1854 |
|
| 1855 |
case BT_REAL: |
| 1856 |
mpc_set_fr (result->value.complex, x->value.real, GFC_RND_MODE); |
| 1857 |
break; |
| 1858 |
|
| 1859 |
case BT_COMPLEX: |
| 1860 |
mpc_set (result->value.complex, x->value.complex, GFC_MPC_RND_MODE); |
| 1861 |
break; |
| 1862 |
|
| 1863 |
default: |
| 1864 |
gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (x)"); |
| 1865 |
} |
| 1866 |
|
| 1867 |
if (!y) |
| 1868 |
return range_check (result, name); |
| 1869 |
|
| 1870 |
switch (y->ts.type) |
| 1871 |
{ |
| 1872 |
case BT_INTEGER: |
| 1873 |
case BT_UNSIGNED: |
| 1874 |
mpfr_set_z (mpc_imagref (result->value.complex), |
| 1875 |
y->value.integer, GFC_RND_MODE); |
| 1876 |
break; |
| 1877 |
|
| 1878 |
case BT_REAL: |
| 1879 |
mpfr_set (mpc_imagref (result->value.complex), |
| 1880 |
y->value.real, GFC_RND_MODE); |
| 1881 |
break; |
| 1882 |
|
| 1883 |
default: |
| 1884 |
gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (y)"); |
| 1885 |
} |
| 1886 |
|
| 1887 |
return range_check (result, name); |
| 1888 |
} |
| 1889 |
|
| 1890 |
|
| 1891 |
gfc_expr * |
| 1892 |
gfc_simplify_cmplx (gfc_expr *x, gfc_expr *y, gfc_expr *k) |
| 1893 |
{ |
| 1894 |
int kind; |
| 1895 |
|
| 1896 |
kind = get_kind (BT_REAL, k, "CMPLX", gfc_default_complex_kind); |
| 1897 |
if (kind == -1) |
| 1898 |
return &gfc_bad_expr; |
| 1899 |
|
| 1900 |
return simplify_cmplx ("CMPLX", x, y, kind); |
| 1901 |
} |
| 1902 |
|
| 1903 |
|
| 1904 |
gfc_expr * |
| 1905 |
gfc_simplify_complex (gfc_expr *x, gfc_expr *y) |
| 1906 |
{ |
| 1907 |
int kind; |
| 1908 |
|
| 1909 |
if (x->ts.type == BT_INTEGER && y->ts.type == BT_INTEGER) |
| 1910 |
kind = gfc_default_complex_kind; |
| 1911 |
else if (x->ts.type == BT_REAL || y->ts.type == BT_INTEGER) |
| 1912 |
kind = x->ts.kind; |
| 1913 |
else if (x->ts.type == BT_INTEGER || y->ts.type == BT_REAL) |
| 1914 |
kind = y->ts.kind; |
| 1915 |
else if (x->ts.type == BT_REAL && y->ts.type == BT_REAL) |
| 1916 |
kind = (x->ts.kind > y->ts.kind) ? x->ts.kind : y->ts.kind; |
| 1917 |
else |
| 1918 |
gcc_unreachable (); |
| 1919 |
|
| 1920 |
return simplify_cmplx ("COMPLEX", x, y, kind); |
| 1921 |
} |
| 1922 |
|
| 1923 |
|
| 1924 |
gfc_expr * |
| 1925 |
gfc_simplify_conjg (gfc_expr *e) |
| 1926 |
{ |
| 1927 |
gfc_expr *result; |
| 1928 |
|
| 1929 |
if (e->expr_type != EXPR_CONSTANT) |
| 1930 |
return NULL; |
| 1931 |
|
| 1932 |
result = gfc_copy_expr (e); |
| 1933 |
mpc_conj (result->value.complex, result->value.complex, GFC_MPC_RND_MODE); |
| 1934 |
|
| 1935 |
return range_check (result, "CONJG"); |
| 1936 |
} |
| 1937 |
|
| 1938 |
|
| 1939 |
/* Simplify atan2d (x) where the unit is degree. */ |
| 1940 |
|
| 1941 |
gfc_expr * |
| 1942 |
gfc_simplify_atan2d (gfc_expr *y, gfc_expr *x) |
| 1943 |
{ |
| 1944 |
gfc_expr *result; |
| 1945 |
|
| 1946 |
if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT) |
| 1947 |
return NULL; |
| 1948 |
|
| 1949 |
if (mpfr_zero_p (y->value.real) && mpfr_zero_p (x->value.real)) |
| 1950 |
{ |
| 1951 |
gfc_error ("If first argument of ATAN2D at %L is zero, then the " |
| 1952 |
"second argument must not be zero", &y->where); |
| 1953 |
return &gfc_bad_expr; |
| 1954 |
} |
| 1955 |
|
| 1956 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 1957 |
mpfr_atan2 (result->value.real, y->value.real, x->value.real, GFC_RND_MODE); |
| 1958 |
rad2deg (result->value.real); |
| 1959 |
|
| 1960 |
return range_check (result, "ATAN2D"); |
| 1961 |
} |
| 1962 |
|
| 1963 |
|
| 1964 |
gfc_expr * |
| 1965 |
gfc_simplify_cos (gfc_expr *x) |
| 1966 |
{ |
| 1967 |
gfc_expr *result; |
| 1968 |
|
| 1969 |
if (x->expr_type != EXPR_CONSTANT) |
| 1970 |
return NULL; |
| 1971 |
|
| 1972 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 1973 |
|
| 1974 |
switch (x->ts.type) |
| 1975 |
{ |
| 1976 |
case BT_REAL: |
| 1977 |
mpfr_cos (result->value.real, x->value.real, GFC_RND_MODE); |
| 1978 |
break; |
| 1979 |
|
| 1980 |
case BT_COMPLEX: |
| 1981 |
gfc_set_model_kind (x->ts.kind); |
| 1982 |
mpc_cos (result->value.complex, x->value.complex, GFC_MPC_RND_MODE); |
| 1983 |
break; |
| 1984 |
|
| 1985 |
default: |
| 1986 |
gfc_internal_error ("in gfc_simplify_cos(): Bad type"); |
| 1987 |
} |
| 1988 |
|
| 1989 |
return range_check (result, "COS"); |
| 1990 |
} |
| 1991 |
|
| 1992 |
|
| 1993 |
static void |
| 1994 |
deg2rad (mpfr_t x) |
| 1995 |
{ |
| 1996 |
mpfr_t d2r; |
| 1997 |
|
| 1998 |
mpfr_init (d2r); |
| 1999 |
mpfr_const_pi (d2r, GFC_RND_MODE); |
| 2000 |
mpfr_div_ui (d2r, d2r, 180, GFC_RND_MODE); |
| 2001 |
mpfr_mul (x, x, d2r, GFC_RND_MODE); |
| 2002 |
mpfr_clear (d2r); |
| 2003 |
} |
| 2004 |
|
| 2005 |
|
| 2006 |
/* Simplification routines for SIND, COSD, TAND. */ |
| 2007 |
#include "trigd_fe.inc" |
| 2008 |
|
| 2009 |
|
| 2010 |
/* Simplify COSD(X) where X has the unit of degree. */ |
| 2011 |
|
| 2012 |
gfc_expr * |
| 2013 |
gfc_simplify_cosd (gfc_expr *x) |
| 2014 |
{ |
| 2015 |
gfc_expr *result; |
| 2016 |
|
| 2017 |
if (x->expr_type != EXPR_CONSTANT) |
| 2018 |
return NULL; |
| 2019 |
|
| 2020 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 2021 |
mpfr_set (result->value.real, x->value.real, GFC_RND_MODE); |
| 2022 |
simplify_cosd (result->value.real); |
| 2023 |
|
| 2024 |
return range_check (result, "COSD"); |
| 2025 |
} |
| 2026 |
|
| 2027 |
|
| 2028 |
/* Simplify SIND(X) where X has the unit of degree. */ |
| 2029 |
|
| 2030 |
gfc_expr * |
| 2031 |
gfc_simplify_sind (gfc_expr *x) |
| 2032 |
{ |
| 2033 |
gfc_expr *result; |
| 2034 |
|
| 2035 |
if (x->expr_type != EXPR_CONSTANT) |
| 2036 |
return NULL; |
| 2037 |
|
| 2038 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 2039 |
mpfr_set (result->value.real, x->value.real, GFC_RND_MODE); |
| 2040 |
simplify_sind (result->value.real); |
| 2041 |
|
| 2042 |
return range_check (result, "SIND"); |
| 2043 |
} |
| 2044 |
|
| 2045 |
|
| 2046 |
/* Simplify TAND(X) where X has the unit of degree. */ |
| 2047 |
|
| 2048 |
gfc_expr * |
| 2049 |
gfc_simplify_tand (gfc_expr *x) |
| 2050 |
{ |
| 2051 |
gfc_expr *result; |
| 2052 |
|
| 2053 |
if (x->expr_type != EXPR_CONSTANT) |
| 2054 |
return NULL; |
| 2055 |
|
| 2056 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 2057 |
mpfr_set (result->value.real, x->value.real, GFC_RND_MODE); |
| 2058 |
simplify_tand (result->value.real); |
| 2059 |
|
| 2060 |
return range_check (result, "TAND"); |
| 2061 |
} |
| 2062 |
|
| 2063 |
|
| 2064 |
/* Simplify COTAND(X) where X has the unit of degree. */ |
| 2065 |
|
| 2066 |
gfc_expr * |
| 2067 |
gfc_simplify_cotand (gfc_expr *x) |
| 2068 |
{ |
| 2069 |
gfc_expr *result; |
| 2070 |
|
| 2071 |
if (x->expr_type != EXPR_CONSTANT) |
| 2072 |
return NULL; |
| 2073 |
|
| 2074 |
/* Implement COTAND = -TAND(x+90). |
| 2075 |
TAND offers correct exact values for multiples of 30 degrees. |
| 2076 |
This implementation is also compatible with the behavior of some legacy |
| 2077 |
compilers. Keep this consistent with gfc_conv_intrinsic_cotand. */ |
| 2078 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 2079 |
mpfr_set (result->value.real, x->value.real, GFC_RND_MODE); |
| 2080 |
mpfr_add_ui (result->value.real, result->value.real, 90, GFC_RND_MODE); |
| 2081 |
simplify_tand (result->value.real); |
| 2082 |
mpfr_neg (result->value.real, result->value.real, GFC_RND_MODE); |
| 2083 |
|
| 2084 |
return range_check (result, "COTAND"); |
| 2085 |
} |
| 2086 |
|
| 2087 |
|
| 2088 |
gfc_expr * |
| 2089 |
gfc_simplify_cosh (gfc_expr *x) |
| 2090 |
{ |
| 2091 |
gfc_expr *result; |
| 2092 |
|
| 2093 |
if (x->expr_type != EXPR_CONSTANT) |
| 2094 |
return NULL; |
| 2095 |
|
| 2096 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 2097 |
|
| 2098 |
switch (x->ts.type) |
| 2099 |
{ |
| 2100 |
case BT_REAL: |
| 2101 |
mpfr_cosh (result->value.real, x->value.real, GFC_RND_MODE); |
| 2102 |
break; |
| 2103 |
|
| 2104 |
case BT_COMPLEX: |
| 2105 |
mpc_cosh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE); |
| 2106 |
break; |
| 2107 |
|
| 2108 |
default: |
| 2109 |
gcc_unreachable (); |
| 2110 |
} |
| 2111 |
|
| 2112 |
return range_check (result, "COSH"); |
| 2113 |
} |
| 2114 |
|
| 2115 |
|
| 2116 |
gfc_expr * |
| 2117 |
gfc_simplify_count (gfc_expr *mask, gfc_expr *dim, gfc_expr *kind) |
| 2118 |
{ |
| 2119 |
gfc_expr *result; |
| 2120 |
bool size_zero; |
| 2121 |
|
| 2122 |
size_zero = gfc_is_size_zero_array (mask); |
| 2123 |
|
| 2124 |
if (!(is_constant_array_expr (mask) || size_zero) |
| 2125 |
|| !gfc_is_constant_expr (dim) |
| 2126 |
|| !gfc_is_constant_expr (kind)) |
| 2127 |
return NULL; |
| 2128 |
|
| 2129 |
result = transformational_result (mask, dim, |
| 2130 |
BT_INTEGER, |
| 2131 |
get_kind (BT_INTEGER, kind, "COUNT", |
| 2132 |
gfc_default_integer_kind), |
| 2133 |
&mask->where); |
| 2134 |
|
| 2135 |
init_result_expr (result, 0, NULL); |
| 2136 |
|
| 2137 |
if (size_zero) |
| 2138 |
return result; |
| 2139 |
|
| 2140 |
/* Passing MASK twice, once as data array, once as mask. |
| 2141 |
Whenever gfc_count is called, '1' is added to the result. */ |
| 2142 |
return !dim || mask->rank == 1 ? |
| 2143 |
simplify_transformation_to_scalar (result, mask, mask, gfc_count) : |
| 2144 |
simplify_transformation_to_array (result, mask, dim, mask, gfc_count, NULL); |
| 2145 |
} |
| 2146 |
|
| 2147 |
/* Simplification routine for cshift. This works by copying the array |
| 2148 |
expressions into a one-dimensional array, shuffling the values into another |
| 2149 |
one-dimensional array and creating the new array expression from this. The |
| 2150 |
shuffling part is basically taken from the library routine. */ |
| 2151 |
|
| 2152 |
gfc_expr * |
| 2153 |
gfc_simplify_cshift (gfc_expr *array, gfc_expr *shift, gfc_expr *dim) |
| 2154 |
{ |
| 2155 |
gfc_expr *result; |
| 2156 |
int which; |
| 2157 |
gfc_expr **arrayvec, **resultvec; |
| 2158 |
gfc_expr **rptr, **sptr; |
| 2159 |
mpz_t size; |
| 2160 |
size_t arraysize, shiftsize, i; |
| 2161 |
gfc_constructor *array_ctor, *shift_ctor; |
| 2162 |
ssize_t *shiftvec, *hptr; |
| 2163 |
ssize_t shift_val, len; |
| 2164 |
ssize_t count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS], |
| 2165 |
hs_ex[GFC_MAX_DIMENSIONS + 1], |
| 2166 |
hstride[GFC_MAX_DIMENSIONS], sstride[GFC_MAX_DIMENSIONS], |
| 2167 |
a_extent[GFC_MAX_DIMENSIONS], a_stride[GFC_MAX_DIMENSIONS], |
| 2168 |
h_extent[GFC_MAX_DIMENSIONS], |
| 2169 |
ss_ex[GFC_MAX_DIMENSIONS + 1]; |
| 2170 |
ssize_t rsoffset; |
| 2171 |
int d, n; |
| 2172 |
bool continue_loop; |
| 2173 |
gfc_expr **src, **dest; |
| 2174 |
|
| 2175 |
if (!is_constant_array_expr (array)) |
| 2176 |
return NULL; |
| 2177 |
|
| 2178 |
if (shift->rank > 0) |
| 2179 |
gfc_simplify_expr (shift, 1); |
| 2180 |
|
| 2181 |
if (!gfc_is_constant_expr (shift)) |
| 2182 |
return NULL; |
| 2183 |
|
| 2184 |
/* Make dim zero-based. */ |
| 2185 |
if (dim) |
| 2186 |
{ |
| 2187 |
if (!gfc_is_constant_expr (dim)) |
| 2188 |
return NULL; |
| 2189 |
which = mpz_get_si (dim->value.integer) - 1; |
| 2190 |
} |
| 2191 |
else |
| 2192 |
which = 0; |
| 2193 |
|
| 2194 |
if (array->shape == NULL) |
| 2195 |
return NULL; |
| 2196 |
|
| 2197 |
gfc_array_size (array, &size); |
| 2198 |
arraysize = mpz_get_ui (size); |
| 2199 |
mpz_clear (size); |
| 2200 |
|
| 2201 |
result = gfc_get_array_expr (array->ts.type, array->ts.kind, &array->where); |
| 2202 |
result->shape = gfc_copy_shape (array->shape, array->rank); |
| 2203 |
result->rank = array->rank; |
| 2204 |
result->ts.u.derived = array->ts.u.derived; |
| 2205 |
|
| 2206 |
if (arraysize == 0) |
| 2207 |
return result; |
| 2208 |
|
| 2209 |
arrayvec = XCNEWVEC (gfc_expr *, arraysize); |
| 2210 |
array_ctor = gfc_constructor_first (array->value.constructor); |
| 2211 |
for (i = 0; i < arraysize; i++) |
| 2212 |
{ |
| 2213 |
arrayvec[i] = array_ctor->expr; |
| 2214 |
array_ctor = gfc_constructor_next (array_ctor); |
| 2215 |
} |
| 2216 |
|
| 2217 |
resultvec = XCNEWVEC (gfc_expr *, arraysize); |
| 2218 |
|
| 2219 |
sstride[0] = 0; |
| 2220 |
extent[0] = 1; |
| 2221 |
count[0] = 0; |
| 2222 |
|
| 2223 |
for (d=0; d < array->rank; d++) |
| 2224 |
{ |
| 2225 |
a_extent[d] = mpz_get_si (array->shape[d]); |
| 2226 |
a_stride[d] = d == 0 ? 1 : a_stride[d-1] * a_extent[d-1]; |
| 2227 |
} |
| 2228 |
|
| 2229 |
if (shift->rank > 0) |
| 2230 |
{ |
| 2231 |
gfc_array_size (shift, &size); |
| 2232 |
shiftsize = mpz_get_ui (size); |
| 2233 |
mpz_clear (size); |
| 2234 |
shiftvec = XCNEWVEC (ssize_t, shiftsize); |
| 2235 |
shift_ctor = gfc_constructor_first (shift->value.constructor); |
| 2236 |
for (d = 0; d < shift->rank; d++) |
| 2237 |
{ |
| 2238 |
h_extent[d] = mpz_get_si (shift->shape[d]); |
| 2239 |
hstride[d] = d == 0 ? 1 : hstride[d-1] * h_extent[d-1]; |
| 2240 |
} |
| 2241 |
} |
| 2242 |
else |
| 2243 |
shiftvec = NULL; |
| 2244 |
|
| 2245 |
/* Shut up compiler */ |
| 2246 |
len = 1; |
| 2247 |
rsoffset = 1; |
| 2248 |
|
| 2249 |
n = 0; |
| 2250 |
for (d=0; d < array->rank; d++) |
| 2251 |
{ |
| 2252 |
if (d == which) |
| 2253 |
{ |
| 2254 |
rsoffset = a_stride[d]; |
| 2255 |
len = a_extent[d]; |
| 2256 |
} |
| 2257 |
else |
| 2258 |
{ |
| 2259 |
count[n] = 0; |
| 2260 |
extent[n] = a_extent[d]; |
| 2261 |
sstride[n] = a_stride[d]; |
| 2262 |
ss_ex[n] = sstride[n] * extent[n]; |
| 2263 |
if (shiftvec) |
| 2264 |
hs_ex[n] = hstride[n] * extent[n]; |
| 2265 |
n++; |
| 2266 |
} |
| 2267 |
} |
| 2268 |
ss_ex[n] = 0; |
| 2269 |
hs_ex[n] = 0; |
| 2270 |
|
| 2271 |
if (shiftvec) |
| 2272 |
{ |
| 2273 |
for (i = 0; i < shiftsize; i++) |
| 2274 |
{ |
| 2275 |
ssize_t val; |
| 2276 |
val = mpz_get_si (shift_ctor->expr->value.integer); |
| 2277 |
val = val % len; |
| 2278 |
if (val < 0) |
| 2279 |
val += len; |
| 2280 |
shiftvec[i] = val; |
| 2281 |
shift_ctor = gfc_constructor_next (shift_ctor); |
| 2282 |
} |
| 2283 |
shift_val = 0; |
| 2284 |
} |
| 2285 |
else |
| 2286 |
{ |
| 2287 |
shift_val = mpz_get_si (shift->value.integer); |
| 2288 |
shift_val = shift_val % len; |
| 2289 |
if (shift_val < 0) |
| 2290 |
shift_val += len; |
| 2291 |
} |
| 2292 |
|
| 2293 |
continue_loop = true; |
| 2294 |
d = array->rank; |
| 2295 |
rptr = resultvec; |
| 2296 |
sptr = arrayvec; |
| 2297 |
hptr = shiftvec; |
| 2298 |
|
| 2299 |
while (continue_loop) |
| 2300 |
{ |
| 2301 |
ssize_t sh; |
| 2302 |
if (shiftvec) |
| 2303 |
sh = *hptr; |
| 2304 |
else |
| 2305 |
sh = shift_val; |
| 2306 |
|
| 2307 |
src = &sptr[sh * rsoffset]; |
| 2308 |
dest = rptr; |
| 2309 |
for (n = 0; n < len - sh; n++) |
| 2310 |
{ |
| 2311 |
*dest = *src; |
| 2312 |
dest += rsoffset; |
| 2313 |
src += rsoffset; |
| 2314 |
} |
| 2315 |
src = sptr; |
| 2316 |
for ( n = 0; n < sh; n++) |
| 2317 |
{ |
| 2318 |
*dest = *src; |
| 2319 |
dest += rsoffset; |
| 2320 |
src += rsoffset; |
| 2321 |
} |
| 2322 |
rptr += sstride[0]; |
| 2323 |
sptr += sstride[0]; |
| 2324 |
if (shiftvec) |
| 2325 |
hptr += hstride[0]; |
| 2326 |
count[0]++; |
| 2327 |
n = 0; |
| 2328 |
while (count[n] == extent[n]) |
| 2329 |
{ |
| 2330 |
count[n] = 0; |
| 2331 |
rptr -= ss_ex[n]; |
| 2332 |
sptr -= ss_ex[n]; |
| 2333 |
if (shiftvec) |
| 2334 |
hptr -= hs_ex[n]; |
| 2335 |
n++; |
| 2336 |
if (n >= d - 1) |
| 2337 |
{ |
| 2338 |
continue_loop = false; |
| 2339 |
break; |
| 2340 |
} |
| 2341 |
else |
| 2342 |
{ |
| 2343 |
count[n]++; |
| 2344 |
rptr += sstride[n]; |
| 2345 |
sptr += sstride[n]; |
| 2346 |
if (shiftvec) |
| 2347 |
hptr += hstride[n]; |
| 2348 |
} |
| 2349 |
} |
| 2350 |
} |
| 2351 |
|
| 2352 |
for (i = 0; i < arraysize; i++) |
| 2353 |
{ |
| 2354 |
gfc_constructor_append_expr (&result->value.constructor, |
| 2355 |
gfc_copy_expr (resultvec[i]), |
| 2356 |
NULL); |
| 2357 |
} |
| 2358 |
return result; |
| 2359 |
} |
| 2360 |
|
| 2361 |
|
| 2362 |
gfc_expr * |
| 2363 |
gfc_simplify_dcmplx (gfc_expr *x, gfc_expr *y) |
| 2364 |
{ |
| 2365 |
return simplify_cmplx ("DCMPLX", x, y, gfc_default_double_kind); |
| 2366 |
} |
| 2367 |
|
| 2368 |
|
| 2369 |
gfc_expr * |
| 2370 |
gfc_simplify_dble (gfc_expr *e) |
| 2371 |
{ |
| 2372 |
gfc_expr *result = NULL; |
| 2373 |
int tmp1, tmp2; |
| 2374 |
|
| 2375 |
if (e->expr_type != EXPR_CONSTANT) |
| 2376 |
return NULL; |
| 2377 |
|
| 2378 |
/* For explicit conversion, turn off -Wconversion and -Wconversion-extra |
| 2379 |
warnings. */ |
| 2380 |
tmp1 = warn_conversion; |
| 2381 |
tmp2 = warn_conversion_extra; |
| 2382 |
warn_conversion = warn_conversion_extra = 0; |
| 2383 |
|
| 2384 |
result = gfc_convert_constant (e, BT_REAL, gfc_default_double_kind); |
| 2385 |
|
| 2386 |
warn_conversion = tmp1; |
| 2387 |
warn_conversion_extra = tmp2; |
| 2388 |
|
| 2389 |
if (result == &gfc_bad_expr) |
| 2390 |
return &gfc_bad_expr; |
| 2391 |
|
| 2392 |
return range_check (result, "DBLE"); |
| 2393 |
} |
| 2394 |
|
| 2395 |
|
| 2396 |
gfc_expr * |
| 2397 |
gfc_simplify_digits (gfc_expr *x) |
| 2398 |
{ |
| 2399 |
int i, digits; |
| 2400 |
|
| 2401 |
i = gfc_validate_kind (x->ts.type, x->ts.kind, false); |
| 2402 |
|
| 2403 |
switch (x->ts.type) |
| 2404 |
{ |
| 2405 |
case BT_INTEGER: |
| 2406 |
digits = gfc_integer_kinds[i].digits; |
| 2407 |
break; |
| 2408 |
|
| 2409 |
case BT_UNSIGNED: |
| 2410 |
digits = gfc_unsigned_kinds[i].digits; |
| 2411 |
break; |
| 2412 |
|
| 2413 |
case BT_REAL: |
| 2414 |
case BT_COMPLEX: |
| 2415 |
digits = gfc_real_kinds[i].digits; |
| 2416 |
break; |
| 2417 |
|
| 2418 |
default: |
| 2419 |
gcc_unreachable (); |
| 2420 |
} |
| 2421 |
|
| 2422 |
return gfc_get_int_expr (gfc_default_integer_kind, NULL, digits); |
| 2423 |
} |
| 2424 |
|
| 2425 |
|
| 2426 |
gfc_expr * |
| 2427 |
gfc_simplify_dim (gfc_expr *x, gfc_expr *y) |
| 2428 |
{ |
| 2429 |
gfc_expr *result; |
| 2430 |
int kind; |
| 2431 |
|
| 2432 |
if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT) |
| 2433 |
return NULL; |
| 2434 |
|
| 2435 |
kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind; |
| 2436 |
result = gfc_get_constant_expr (x->ts.type, kind, &x->where); |
| 2437 |
|
| 2438 |
switch (x->ts.type) |
| 2439 |
{ |
| 2440 |
case BT_INTEGER: |
| 2441 |
if (mpz_cmp (x->value.integer, y->value.integer) > 0) |
| 2442 |
mpz_sub (result->value.integer, x->value.integer, y->value.integer); |
| 2443 |
else |
| 2444 |
mpz_set_ui (result->value.integer, 0); |
| 2445 |
|
| 2446 |
break; |
| 2447 |
|
| 2448 |
case BT_REAL: |
| 2449 |
if (mpfr_cmp (x->value.real, y->value.real) > 0) |
| 2450 |
mpfr_sub (result->value.real, x->value.real, y->value.real, |
| 2451 |
GFC_RND_MODE); |
| 2452 |
else |
| 2453 |
mpfr_set_ui (result->value.real, 0, GFC_RND_MODE); |
| 2454 |
|
| 2455 |
break; |
| 2456 |
|
| 2457 |
default: |
| 2458 |
gfc_internal_error ("gfc_simplify_dim(): Bad type"); |
| 2459 |
} |
| 2460 |
|
| 2461 |
return range_check (result, "DIM"); |
| 2462 |
} |
| 2463 |
|
| 2464 |
|
| 2465 |
gfc_expr* |
| 2466 |
gfc_simplify_dot_product (gfc_expr *vector_a, gfc_expr *vector_b) |
| 2467 |
{ |
| 2468 |
/* If vector_a is a zero-sized array, the result is 0 for INTEGER, |
| 2469 |
REAL, and COMPLEX types and .false. for LOGICAL. */ |
| 2470 |
if (vector_a->shape && mpz_get_si (vector_a->shape[0]) == 0) |
| 2471 |
{ |
| 2472 |
if (vector_a->ts.type == BT_LOGICAL) |
| 2473 |
return gfc_get_logical_expr (gfc_default_logical_kind, NULL, false); |
| 2474 |
else |
| 2475 |
return gfc_get_int_expr (gfc_default_integer_kind, NULL, 0); |
| 2476 |
} |
| 2477 |
|
| 2478 |
if (!is_constant_array_expr (vector_a) |
| 2479 |
|| !is_constant_array_expr (vector_b)) |
| 2480 |
return NULL; |
| 2481 |
|
| 2482 |
return compute_dot_product (vector_a, 1, 0, vector_b, 1, 0, true); |
| 2483 |
} |
| 2484 |
|
| 2485 |
|
| 2486 |
gfc_expr * |
| 2487 |
gfc_simplify_dprod (gfc_expr *x, gfc_expr *y) |
| 2488 |
{ |
| 2489 |
gfc_expr *a1, *a2, *result; |
| 2490 |
|
| 2491 |
if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT) |
| 2492 |
return NULL; |
| 2493 |
|
| 2494 |
a1 = gfc_real2real (x, gfc_default_double_kind); |
| 2495 |
a2 = gfc_real2real (y, gfc_default_double_kind); |
| 2496 |
|
| 2497 |
result = gfc_get_constant_expr (BT_REAL, gfc_default_double_kind, &x->where); |
| 2498 |
mpfr_mul (result->value.real, a1->value.real, a2->value.real, GFC_RND_MODE); |
| 2499 |
|
| 2500 |
gfc_free_expr (a2); |
| 2501 |
gfc_free_expr (a1); |
| 2502 |
|
| 2503 |
return range_check (result, "DPROD"); |
| 2504 |
} |
| 2505 |
|
| 2506 |
|
| 2507 |
static gfc_expr * |
| 2508 |
simplify_dshift (gfc_expr *arg1, gfc_expr *arg2, gfc_expr *shiftarg, |
| 2509 |
bool right) |
| 2510 |
{ |
| 2511 |
gfc_expr *result; |
| 2512 |
int i, k, size, shift; |
| 2513 |
bt type = BT_INTEGER; |
| 2514 |
|
| 2515 |
if (arg1->expr_type != EXPR_CONSTANT || arg2->expr_type != EXPR_CONSTANT |
| 2516 |
|| shiftarg->expr_type != EXPR_CONSTANT) |
| 2517 |
return NULL; |
| 2518 |
|
| 2519 |
if (flag_unsigned && arg1->ts.type == BT_UNSIGNED) |
| 2520 |
{ |
| 2521 |
k = gfc_validate_kind (BT_UNSIGNED, arg1->ts.kind, false); |
| 2522 |
size = gfc_unsigned_kinds[k].bit_size; |
| 2523 |
type = BT_UNSIGNED; |
| 2524 |
} |
| 2525 |
else |
| 2526 |
{ |
| 2527 |
k = gfc_validate_kind (BT_INTEGER, arg1->ts.kind, false); |
| 2528 |
size = gfc_integer_kinds[k].bit_size; |
| 2529 |
} |
| 2530 |
|
| 2531 |
gfc_extract_int (shiftarg, &shift); |
| 2532 |
|
| 2533 |
/* DSHIFTR(I,J,SHIFT) = DSHIFTL(I,J,SIZE-SHIFT). */ |
| 2534 |
if (right) |
| 2535 |
shift = size - shift; |
| 2536 |
|
| 2537 |
result = gfc_get_constant_expr (type, arg1->ts.kind, &arg1->where); |
| 2538 |
mpz_set_ui (result->value.integer, 0); |
| 2539 |
|
| 2540 |
for (i = 0; i < shift; i++) |
| 2541 |
if (mpz_tstbit (arg2->value.integer, size - shift + i)) |
| 2542 |
mpz_setbit (result->value.integer, i); |
| 2543 |
|
| 2544 |
for (i = 0; i < size - shift; i++) |
| 2545 |
if (mpz_tstbit (arg1->value.integer, i)) |
| 2546 |
mpz_setbit (result->value.integer, shift + i); |
| 2547 |
|
| 2548 |
/* Convert to a signed value if needed. */ |
| 2549 |
if (type == BT_INTEGER) |
| 2550 |
gfc_convert_mpz_to_signed (result->value.integer, size); |
| 2551 |
else |
| 2552 |
gfc_reduce_unsigned (result); |
| 2553 |
|
| 2554 |
return result; |
| 2555 |
} |
| 2556 |
|
| 2557 |
|
| 2558 |
gfc_expr * |
| 2559 |
gfc_simplify_dshiftr (gfc_expr *arg1, gfc_expr *arg2, gfc_expr *shiftarg) |
| 2560 |
{ |
| 2561 |
return simplify_dshift (arg1, arg2, shiftarg, true); |
| 2562 |
} |
| 2563 |
|
| 2564 |
|
| 2565 |
gfc_expr * |
| 2566 |
gfc_simplify_dshiftl (gfc_expr *arg1, gfc_expr *arg2, gfc_expr *shiftarg) |
| 2567 |
{ |
| 2568 |
return simplify_dshift (arg1, arg2, shiftarg, false); |
| 2569 |
} |
| 2570 |
|
| 2571 |
|
| 2572 |
gfc_expr * |
| 2573 |
gfc_simplify_eoshift (gfc_expr *array, gfc_expr *shift, gfc_expr *boundary, |
| 2574 |
gfc_expr *dim) |
| 2575 |
{ |
| 2576 |
bool temp_boundary; |
| 2577 |
gfc_expr *bnd; |
| 2578 |
gfc_expr *result; |
| 2579 |
int which; |
| 2580 |
gfc_expr **arrayvec, **resultvec; |
| 2581 |
gfc_expr **rptr, **sptr; |
| 2582 |
mpz_t size; |
| 2583 |
size_t arraysize, i; |
| 2584 |
gfc_constructor *array_ctor, *shift_ctor, *bnd_ctor; |
| 2585 |
ssize_t shift_val, len; |
| 2586 |
ssize_t count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS], |
| 2587 |
sstride[GFC_MAX_DIMENSIONS], a_extent[GFC_MAX_DIMENSIONS], |
| 2588 |
a_stride[GFC_MAX_DIMENSIONS], ss_ex[GFC_MAX_DIMENSIONS + 1]; |
| 2589 |
ssize_t rsoffset; |
| 2590 |
int d, n; |
| 2591 |
bool continue_loop; |
| 2592 |
gfc_expr **src, **dest; |
| 2593 |
size_t s_len; |
| 2594 |
|
| 2595 |
if (!is_constant_array_expr (array)) |
| 2596 |
return NULL; |
| 2597 |
|
| 2598 |
if (shift->rank > 0) |
| 2599 |
gfc_simplify_expr (shift, 1); |
| 2600 |
|
| 2601 |
if (!gfc_is_constant_expr (shift)) |
| 2602 |
return NULL; |
| 2603 |
|
| 2604 |
if (boundary) |
| 2605 |
{ |
| 2606 |
if (boundary->rank > 0) |
| 2607 |
gfc_simplify_expr (boundary, 1); |
| 2608 |
|
| 2609 |
if (!gfc_is_constant_expr (boundary)) |
| 2610 |
return NULL; |
| 2611 |
} |
| 2612 |
|
| 2613 |
if (dim) |
| 2614 |
{ |
| 2615 |
if (!gfc_is_constant_expr (dim)) |
| 2616 |
return NULL; |
| 2617 |
which = mpz_get_si (dim->value.integer) - 1; |
| 2618 |
} |
| 2619 |
else |
| 2620 |
which = 0; |
| 2621 |
|
| 2622 |
s_len = 0; |
| 2623 |
if (boundary == NULL) |
| 2624 |
{ |
| 2625 |
temp_boundary = true; |
| 2626 |
switch (array->ts.type) |
| 2627 |
{ |
| 2628 |
|
| 2629 |
case BT_INTEGER: |
| 2630 |
bnd = gfc_get_int_expr (array->ts.kind, NULL, 0); |
| 2631 |
break; |
| 2632 |
|
| 2633 |
case BT_UNSIGNED: |
| 2634 |
bnd = gfc_get_unsigned_expr (array->ts.kind, NULL, 0); |
| 2635 |
break; |
| 2636 |
|
| 2637 |
case BT_LOGICAL: |
| 2638 |
bnd = gfc_get_logical_expr (array->ts.kind, NULL, 0); |
| 2639 |
break; |
| 2640 |
|
| 2641 |
case BT_REAL: |
| 2642 |
bnd = gfc_get_constant_expr (array->ts.type, array->ts.kind, &gfc_current_locus); |
| 2643 |
mpfr_set_ui (bnd->value.real, 0, GFC_RND_MODE); |
| 2644 |
break; |
| 2645 |
|
| 2646 |
case BT_COMPLEX: |
| 2647 |
bnd = gfc_get_constant_expr (array->ts.type, array->ts.kind, &gfc_current_locus); |
| 2648 |
mpc_set_ui (bnd->value.complex, 0, GFC_RND_MODE); |
| 2649 |
break; |
| 2650 |
|
| 2651 |
case BT_CHARACTER: |
| 2652 |
s_len = mpz_get_ui (array->ts.u.cl->length->value.integer); |
| 2653 |
bnd = gfc_get_character_expr (array->ts.kind, &gfc_current_locus, NULL, s_len); |
| 2654 |
break; |
| 2655 |
|
| 2656 |
default: |
| 2657 |
gcc_unreachable(); |
| 2658 |
|
| 2659 |
} |
| 2660 |
} |
| 2661 |
else |
| 2662 |
{ |
| 2663 |
temp_boundary = false; |
| 2664 |
bnd = boundary; |
| 2665 |
} |
| 2666 |
|
| 2667 |
gfc_array_size (array, &size); |
| 2668 |
arraysize = mpz_get_ui (size); |
| 2669 |
mpz_clear (size); |
| 2670 |
|
| 2671 |
result = gfc_get_array_expr (array->ts.type, array->ts.kind, &array->where); |
| 2672 |
result->shape = gfc_copy_shape (array->shape, array->rank); |
| 2673 |
result->rank = array->rank; |
| 2674 |
result->ts = array->ts; |
| 2675 |
|
| 2676 |
if (arraysize == 0) |
| 2677 |
goto final; |
| 2678 |
|
| 2679 |
if (array->shape == NULL) |
| 2680 |
goto final; |
| 2681 |
|
| 2682 |
arrayvec = XCNEWVEC (gfc_expr *, arraysize); |
| 2683 |
array_ctor = gfc_constructor_first (array->value.constructor); |
| 2684 |
for (i = 0; i < arraysize; i++) |
| 2685 |
{ |
| 2686 |
arrayvec[i] = array_ctor->expr; |
| 2687 |
array_ctor = gfc_constructor_next (array_ctor); |
| 2688 |
} |
| 2689 |
|
| 2690 |
resultvec = XCNEWVEC (gfc_expr *, arraysize); |
| 2691 |
|
| 2692 |
extent[0] = 1; |
| 2693 |
count[0] = 0; |
| 2694 |
|
| 2695 |
for (d=0; d < array->rank; d++) |
| 2696 |
{ |
| 2697 |
a_extent[d] = mpz_get_si (array->shape[d]); |
| 2698 |
a_stride[d] = d == 0 ? 1 : a_stride[d-1] * a_extent[d-1]; |
| 2699 |
} |
| 2700 |
|
| 2701 |
if (shift->rank > 0) |
| 2702 |
{ |
| 2703 |
shift_ctor = gfc_constructor_first (shift->value.constructor); |
| 2704 |
shift_val = 0; |
| 2705 |
} |
| 2706 |
else |
| 2707 |
{ |
| 2708 |
shift_ctor = NULL; |
| 2709 |
shift_val = mpz_get_si (shift->value.integer); |
| 2710 |
} |
| 2711 |
|
| 2712 |
if (bnd->rank > 0) |
| 2713 |
bnd_ctor = gfc_constructor_first (bnd->value.constructor); |
| 2714 |
else |
| 2715 |
bnd_ctor = NULL; |
| 2716 |
|
| 2717 |
/* Shut up compiler */ |
| 2718 |
len = 1; |
| 2719 |
rsoffset = 1; |
| 2720 |
sstride[0] = 0; |
| 2721 |
|
| 2722 |
n = 0; |
| 2723 |
for (d=0; d < array->rank; d++) |
| 2724 |
{ |
| 2725 |
if (d == which) |
| 2726 |
{ |
| 2727 |
rsoffset = a_stride[d]; |
| 2728 |
len = a_extent[d]; |
| 2729 |
} |
| 2730 |
else |
| 2731 |
{ |
| 2732 |
count[n] = 0; |
| 2733 |
extent[n] = a_extent[d]; |
| 2734 |
sstride[n] = a_stride[d]; |
| 2735 |
ss_ex[n] = sstride[n] * extent[n]; |
| 2736 |
n++; |
| 2737 |
} |
| 2738 |
} |
| 2739 |
ss_ex[n] = 0; |
| 2740 |
|
| 2741 |
continue_loop = true; |
| 2742 |
d = array->rank; |
| 2743 |
rptr = resultvec; |
| 2744 |
sptr = arrayvec; |
| 2745 |
|
| 2746 |
while (continue_loop) |
| 2747 |
{ |
| 2748 |
ssize_t sh, delta; |
| 2749 |
|
| 2750 |
if (shift_ctor) |
| 2751 |
sh = mpz_get_si (shift_ctor->expr->value.integer); |
| 2752 |
else |
| 2753 |
sh = shift_val; |
| 2754 |
|
| 2755 |
if (( sh >= 0 ? sh : -sh ) > len) |
| 2756 |
{ |
| 2757 |
delta = len; |
| 2758 |
sh = len; |
| 2759 |
} |
| 2760 |
else |
| 2761 |
delta = (sh >= 0) ? sh: -sh; |
| 2762 |
|
| 2763 |
if (sh > 0) |
| 2764 |
{ |
| 2765 |
src = &sptr[delta * rsoffset]; |
| 2766 |
dest = rptr; |
| 2767 |
} |
| 2768 |
else |
| 2769 |
{ |
| 2770 |
src = sptr; |
| 2771 |
dest = &rptr[delta * rsoffset]; |
| 2772 |
} |
| 2773 |
|
| 2774 |
for (n = 0; n < len - delta; n++) |
| 2775 |
{ |
| 2776 |
*dest = *src; |
| 2777 |
dest += rsoffset; |
| 2778 |
src += rsoffset; |
| 2779 |
} |
| 2780 |
|
| 2781 |
if (sh < 0) |
| 2782 |
dest = rptr; |
| 2783 |
|
| 2784 |
n = delta; |
| 2785 |
|
| 2786 |
if (bnd_ctor) |
| 2787 |
{ |
| 2788 |
while (n--) |
| 2789 |
{ |
| 2790 |
*dest = bnd_ctor->expr; |
| 2791 |
dest += rsoffset; |
| 2792 |
} |
| 2793 |
} |
| 2794 |
else |
| 2795 |
{ |
| 2796 |
while (n--) |
| 2797 |
{ |
| 2798 |
*dest = bnd; |
| 2799 |
dest += rsoffset; |
| 2800 |
} |
| 2801 |
} |
| 2802 |
rptr += sstride[0]; |
| 2803 |
sptr += sstride[0]; |
| 2804 |
if (shift_ctor) |
| 2805 |
shift_ctor = gfc_constructor_next (shift_ctor); |
| 2806 |
|
| 2807 |
if (bnd_ctor) |
| 2808 |
bnd_ctor = gfc_constructor_next (bnd_ctor); |
| 2809 |
|
| 2810 |
count[0]++; |
| 2811 |
n = 0; |
| 2812 |
while (count[n] == extent[n]) |
| 2813 |
{ |
| 2814 |
count[n] = 0; |
| 2815 |
rptr -= ss_ex[n]; |
| 2816 |
sptr -= ss_ex[n]; |
| 2817 |
n++; |
| 2818 |
if (n >= d - 1) |
| 2819 |
{ |
| 2820 |
continue_loop = false; |
| 2821 |
break; |
| 2822 |
} |
| 2823 |
else |
| 2824 |
{ |
| 2825 |
count[n]++; |
| 2826 |
rptr += sstride[n]; |
| 2827 |
sptr += sstride[n]; |
| 2828 |
} |
| 2829 |
} |
| 2830 |
} |
| 2831 |
|
| 2832 |
for (i = 0; i < arraysize; i++) |
| 2833 |
{ |
| 2834 |
gfc_constructor_append_expr (&result->value.constructor, |
| 2835 |
gfc_copy_expr (resultvec[i]), |
| 2836 |
NULL); |
| 2837 |
} |
| 2838 |
|
| 2839 |
free (arrayvec); |
| 2840 |
free (resultvec); |
| 2841 |
|
| 2842 |
final: |
| 2843 |
if (temp_boundary) |
| 2844 |
gfc_free_expr (bnd); |
| 2845 |
|
| 2846 |
return result; |
| 2847 |
} |
| 2848 |
|
| 2849 |
gfc_expr * |
| 2850 |
gfc_simplify_erf (gfc_expr *x) |
| 2851 |
{ |
| 2852 |
gfc_expr *result; |
| 2853 |
|
| 2854 |
if (x->expr_type != EXPR_CONSTANT) |
| 2855 |
return NULL; |
| 2856 |
|
| 2857 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 2858 |
mpfr_erf (result->value.real, x->value.real, GFC_RND_MODE); |
| 2859 |
|
| 2860 |
return range_check (result, "ERF"); |
| 2861 |
} |
| 2862 |
|
| 2863 |
|
| 2864 |
gfc_expr * |
| 2865 |
gfc_simplify_erfc (gfc_expr *x) |
| 2866 |
{ |
| 2867 |
gfc_expr *result; |
| 2868 |
|
| 2869 |
if (x->expr_type != EXPR_CONSTANT) |
| 2870 |
return NULL; |
| 2871 |
|
| 2872 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 2873 |
mpfr_erfc (result->value.real, x->value.real, GFC_RND_MODE); |
| 2874 |
|
| 2875 |
return range_check (result, "ERFC"); |
| 2876 |
} |
| 2877 |
|
| 2878 |
|
| 2879 |
/* Helper functions to simplify ERFC_SCALED(x) = ERFC(x) * EXP(X**2). */ |
| 2880 |
|
| 2881 |
#define MAX_ITER 200 |
| 2882 |
#define ARG_LIMIT 12 |
| 2883 |
|
| 2884 |
/* Calculate ERFC_SCALED directly by its definition: |
| 2885 |
|
| 2886 |
ERFC_SCALED(x) = ERFC(x) * EXP(X**2) |
| 2887 |
|
| 2888 |
using a large precision for intermediate results. This is used for all |
| 2889 |
but large values of the argument. */ |
| 2890 |
static void |
| 2891 |
fullprec_erfc_scaled (mpfr_t res, mpfr_t arg) |
| 2892 |
{ |
| 2893 |
mpfr_prec_t prec; |
| 2894 |
mpfr_t a, b; |
| 2895 |
|
| 2896 |
prec = mpfr_get_default_prec (); |
| 2897 |
mpfr_set_default_prec (10 * prec); |
| 2898 |
|
| 2899 |
mpfr_init (a); |
| 2900 |
mpfr_init (b); |
| 2901 |
|
| 2902 |
mpfr_set (a, arg, GFC_RND_MODE); |
| 2903 |
mpfr_sqr (b, a, GFC_RND_MODE); |
| 2904 |
mpfr_exp (b, b, GFC_RND_MODE); |
| 2905 |
mpfr_erfc (a, a, GFC_RND_MODE); |
| 2906 |
mpfr_mul (a, a, b, GFC_RND_MODE); |
| 2907 |
|
| 2908 |
mpfr_set (res, a, GFC_RND_MODE); |
| 2909 |
mpfr_set_default_prec (prec); |
| 2910 |
|
| 2911 |
mpfr_clear (a); |
| 2912 |
mpfr_clear (b); |
| 2913 |
} |
| 2914 |
|
| 2915 |
/* Calculate ERFC_SCALED using a power series expansion in 1/arg: |
| 2916 |
|
| 2917 |
ERFC_SCALED(x) = 1 / (x * sqrt(pi)) |
| 2918 |
* (1 + Sum_n (-1)**n * (1 * 3 * 5 * ... * (2n-1)) |
| 2919 |
/ (2 * x**2)**n) |
| 2920 |
|
| 2921 |
This is used for large values of the argument. Intermediate calculations |
| 2922 |
are performed with twice the precision. We don't do a fixed number of |
| 2923 |
iterations of the sum, but stop when it has converged to the required |
| 2924 |
precision. */ |
| 2925 |
static void |
| 2926 |
asympt_erfc_scaled (mpfr_t res, mpfr_t arg) |
| 2927 |
{ |
| 2928 |
mpfr_t sum, x, u, v, w, oldsum, sumtrunc; |
| 2929 |
mpz_t num; |
| 2930 |
mpfr_prec_t prec; |
| 2931 |
unsigned i; |
| 2932 |
|
| 2933 |
prec = mpfr_get_default_prec (); |
| 2934 |
mpfr_set_default_prec (2 * prec); |
| 2935 |
|
| 2936 |
mpfr_init (sum); |
| 2937 |
mpfr_init (x); |
| 2938 |
mpfr_init (u); |
| 2939 |
mpfr_init (v); |
| 2940 |
mpfr_init (w); |
| 2941 |
mpz_init (num); |
| 2942 |
|
| 2943 |
mpfr_init (oldsum); |
| 2944 |
mpfr_init (sumtrunc); |
| 2945 |
mpfr_set_prec (oldsum, prec); |
| 2946 |
mpfr_set_prec (sumtrunc, prec); |
| 2947 |
|
| 2948 |
mpfr_set (x, arg, GFC_RND_MODE); |
| 2949 |
mpfr_set_ui (sum, 1, GFC_RND_MODE); |
| 2950 |
mpz_set_ui (num, 1); |
| 2951 |
|
| 2952 |
mpfr_set (u, x, GFC_RND_MODE); |
| 2953 |
mpfr_sqr (u, u, GFC_RND_MODE); |
| 2954 |
mpfr_mul_ui (u, u, 2, GFC_RND_MODE); |
| 2955 |
mpfr_pow_si (u, u, -1, GFC_RND_MODE); |
| 2956 |
|
| 2957 |
for (i = 1; i < MAX_ITER; i++) |
| 2958 |
{ |
| 2959 |
mpfr_set (oldsum, sum, GFC_RND_MODE); |
| 2960 |
|
| 2961 |
mpz_mul_ui (num, num, 2 * i - 1); |
| 2962 |
mpz_neg (num, num); |
| 2963 |
|
| 2964 |
mpfr_set (w, u, GFC_RND_MODE); |
| 2965 |
mpfr_pow_ui (w, w, i, GFC_RND_MODE); |
| 2966 |
|
| 2967 |
mpfr_set_z (v, num, GFC_RND_MODE); |
| 2968 |
mpfr_mul (v, v, w, GFC_RND_MODE); |
| 2969 |
|
| 2970 |
mpfr_add (sum, sum, v, GFC_RND_MODE); |
| 2971 |
|
| 2972 |
mpfr_set (sumtrunc, sum, GFC_RND_MODE); |
| 2973 |
if (mpfr_cmp (sumtrunc, oldsum) == 0) |
| 2974 |
break; |
| 2975 |
} |
| 2976 |
|
| 2977 |
/* We should have converged by now; otherwise, ARG_LIMIT is probably |
| 2978 |
set too low. */ |
| 2979 |
gcc_assert (i < MAX_ITER); |
| 2980 |
|
| 2981 |
/* Divide by x * sqrt(Pi). */ |
| 2982 |
mpfr_const_pi (u, GFC_RND_MODE); |
| 2983 |
mpfr_sqrt (u, u, GFC_RND_MODE); |
| 2984 |
mpfr_mul (u, u, x, GFC_RND_MODE); |
| 2985 |
mpfr_div (sum, sum, u, GFC_RND_MODE); |
| 2986 |
|
| 2987 |
mpfr_set (res, sum, GFC_RND_MODE); |
| 2988 |
mpfr_set_default_prec (prec); |
| 2989 |
|
| 2990 |
mpfr_clears (sum, x, u, v, w, oldsum, sumtrunc, NULL); |
| 2991 |
mpz_clear (num); |
| 2992 |
} |
| 2993 |
|
| 2994 |
|
| 2995 |
gfc_expr * |
| 2996 |
gfc_simplify_erfc_scaled (gfc_expr *x) |
| 2997 |
{ |
| 2998 |
gfc_expr *result; |
| 2999 |
|
| 3000 |
if (x->expr_type != EXPR_CONSTANT) |
| 3001 |
return NULL; |
| 3002 |
|
| 3003 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 3004 |
if (mpfr_cmp_d (x->value.real, ARG_LIMIT) >= 0) |
| 3005 |
asympt_erfc_scaled (result->value.real, x->value.real); |
| 3006 |
else |
| 3007 |
fullprec_erfc_scaled (result->value.real, x->value.real); |
| 3008 |
|
| 3009 |
return range_check (result, "ERFC_SCALED"); |
| 3010 |
} |
| 3011 |
|
| 3012 |
#undef MAX_ITER |
| 3013 |
#undef ARG_LIMIT |
| 3014 |
|
| 3015 |
|
| 3016 |
gfc_expr * |
| 3017 |
gfc_simplify_epsilon (gfc_expr *e) |
| 3018 |
{ |
| 3019 |
gfc_expr *result; |
| 3020 |
int i; |
| 3021 |
|
| 3022 |
i = gfc_validate_kind (e->ts.type, e->ts.kind, false); |
| 3023 |
|
| 3024 |
result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where); |
| 3025 |
mpfr_set (result->value.real, gfc_real_kinds[i].epsilon, GFC_RND_MODE); |
| 3026 |
|
| 3027 |
return range_check (result, "EPSILON"); |
| 3028 |
} |
| 3029 |
|
| 3030 |
|
| 3031 |
gfc_expr * |
| 3032 |
gfc_simplify_exp (gfc_expr *x) |
| 3033 |
{ |
| 3034 |
gfc_expr *result; |
| 3035 |
|
| 3036 |
if (x->expr_type != EXPR_CONSTANT) |
| 3037 |
return NULL; |
| 3038 |
|
| 3039 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 3040 |
|
| 3041 |
switch (x->ts.type) |
| 3042 |
{ |
| 3043 |
case BT_REAL: |
| 3044 |
mpfr_exp (result->value.real, x->value.real, GFC_RND_MODE); |
| 3045 |
break; |
| 3046 |
|
| 3047 |
case BT_COMPLEX: |
| 3048 |
gfc_set_model_kind (x->ts.kind); |
| 3049 |
mpc_exp (result->value.complex, x->value.complex, GFC_MPC_RND_MODE); |
| 3050 |
break; |
| 3051 |
|
| 3052 |
default: |
| 3053 |
gfc_internal_error ("in gfc_simplify_exp(): Bad type"); |
| 3054 |
} |
| 3055 |
|
| 3056 |
return range_check (result, "EXP"); |
| 3057 |
} |
| 3058 |
|
| 3059 |
|
| 3060 |
gfc_expr * |
| 3061 |
gfc_simplify_exponent (gfc_expr *x) |
| 3062 |
{ |
| 3063 |
long int val; |
| 3064 |
gfc_expr *result; |
| 3065 |
|
| 3066 |
if (x->expr_type != EXPR_CONSTANT) |
| 3067 |
return NULL; |
| 3068 |
|
| 3069 |
result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind, |
| 3070 |
&x->where); |
| 3071 |
|
| 3072 |
/* EXPONENT(inf) = EXPONENT(nan) = HUGE(0) */ |
| 3073 |
if (mpfr_inf_p (x->value.real) || mpfr_nan_p (x->value.real)) |
| 3074 |
{ |
| 3075 |
int i = gfc_validate_kind (BT_INTEGER, gfc_default_integer_kind, false); |
| 3076 |
mpz_set (result->value.integer, gfc_integer_kinds[i].huge); |
| 3077 |
return result; |
| 3078 |
} |
| 3079 |
|
| 3080 |
/* EXPONENT(+/- 0.0) = 0 */ |
| 3081 |
if (mpfr_zero_p (x->value.real)) |
| 3082 |
{ |
| 3083 |
mpz_set_ui (result->value.integer, 0); |
| 3084 |
return result; |
| 3085 |
} |
| 3086 |
|
| 3087 |
gfc_set_model (x->value.real); |
| 3088 |
|
| 3089 |
val = (long int) mpfr_get_exp (x->value.real); |
| 3090 |
mpz_set_si (result->value.integer, val); |
| 3091 |
|
| 3092 |
return range_check (result, "EXPONENT"); |
| 3093 |
} |
| 3094 |
|
| 3095 |
|
| 3096 |
gfc_expr * |
| 3097 |
gfc_simplify_failed_or_stopped_images (gfc_expr *team ATTRIBUTE_UNUSED, |
| 3098 |
gfc_expr *kind) |
| 3099 |
{ |
| 3100 |
if (flag_coarray == GFC_FCOARRAY_NONE) |
| 3101 |
{ |
| 3102 |
gfc_current_locus = *gfc_current_intrinsic_where; |
| 3103 |
gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable"); |
| 3104 |
return &gfc_bad_expr; |
| 3105 |
} |
| 3106 |
|
| 3107 |
if (flag_coarray == GFC_FCOARRAY_SINGLE) |
| 3108 |
{ |
| 3109 |
gfc_expr *result; |
| 3110 |
int actual_kind; |
| 3111 |
if (kind) |
| 3112 |
gfc_extract_int (kind, &actual_kind); |
| 3113 |
else |
| 3114 |
actual_kind = gfc_default_integer_kind; |
| 3115 |
|
| 3116 |
result = gfc_get_array_expr (BT_INTEGER, actual_kind, &gfc_current_locus); |
| 3117 |
result->rank = 1; |
| 3118 |
return result; |
| 3119 |
} |
| 3120 |
|
| 3121 |
/* For fcoarray = lib no simplification is possible, because it is not known |
| 3122 |
what images failed or are stopped at compile time. */ |
| 3123 |
return NULL; |
| 3124 |
} |
| 3125 |
|
| 3126 |
|
| 3127 |
gfc_expr * |
| 3128 |
gfc_simplify_get_team (gfc_expr *level ATTRIBUTE_UNUSED) |
| 3129 |
{ |
| 3130 |
if (flag_coarray == GFC_FCOARRAY_NONE) |
| 3131 |
{ |
| 3132 |
gfc_current_locus = *gfc_current_intrinsic_where; |
| 3133 |
gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable"); |
| 3134 |
return &gfc_bad_expr; |
| 3135 |
} |
| 3136 |
|
| 3137 |
if (flag_coarray == GFC_FCOARRAY_SINGLE) |
| 3138 |
{ |
| 3139 |
gfc_expr *result; |
| 3140 |
result = gfc_get_array_expr (BT_INTEGER, gfc_default_integer_kind, &gfc_current_locus); |
| 3141 |
result->rank = 0; |
| 3142 |
return result; |
| 3143 |
} |
| 3144 |
|
| 3145 |
/* For fcoarray = lib no simplification is possible, because it is not known |
| 3146 |
what images failed or are stopped at compile time. */ |
| 3147 |
return NULL; |
| 3148 |
} |
| 3149 |
|
| 3150 |
|
| 3151 |
gfc_expr * |
| 3152 |
gfc_simplify_float (gfc_expr *a) |
| 3153 |
{ |
| 3154 |
gfc_expr *result; |
| 3155 |
|
| 3156 |
if (a->expr_type != EXPR_CONSTANT) |
| 3157 |
return NULL; |
| 3158 |
|
| 3159 |
result = gfc_int2real (a, gfc_default_real_kind); |
| 3160 |
|
| 3161 |
return range_check (result, "FLOAT"); |
| 3162 |
} |
| 3163 |
|
| 3164 |
|
| 3165 |
static bool |
| 3166 |
is_last_ref_vtab (gfc_expr *e) |
| 3167 |
{ |
| 3168 |
gfc_ref *ref; |
| 3169 |
gfc_component *comp = NULL; |
| 3170 |
|
| 3171 |
if (e->expr_type != EXPR_VARIABLE) |
| 3172 |
return false; |
| 3173 |
|
| 3174 |
for (ref = e->ref; ref; ref = ref->next) |
| 3175 |
if (ref->type == REF_COMPONENT) |
| 3176 |
comp = ref->u.c.component; |
| 3177 |
|
| 3178 |
if (!e->ref || !comp) |
| 3179 |
return e->symtree->n.sym->attr.vtab; |
| 3180 |
|
| 3181 |
if (comp->name[0] == '_' && strcmp (comp->name, "_vptr") == 0) |
| 3182 |
return true; |
| 3183 |
|
| 3184 |
return false; |
| 3185 |
} |
| 3186 |
|
| 3187 |
|
| 3188 |
gfc_expr * |
| 3189 |
gfc_simplify_extends_type_of (gfc_expr *a, gfc_expr *mold) |
| 3190 |
{ |
| 3191 |
/* Avoid simplification of resolved symbols. */ |
| 3192 |
if (is_last_ref_vtab (a) || is_last_ref_vtab (mold)) |
| 3193 |
return NULL; |
| 3194 |
|
| 3195 |
if (a->ts.type == BT_DERIVED && mold->ts.type == BT_DERIVED) |
| 3196 |
return gfc_get_logical_expr (gfc_default_logical_kind, &a->where, |
| 3197 |
gfc_type_is_extension_of (mold->ts.u.derived, |
| 3198 |
a->ts.u.derived)); |
| 3199 |
|
| 3200 |
if (UNLIMITED_POLY (a) || UNLIMITED_POLY (mold)) |
| 3201 |
return NULL; |
| 3202 |
|
| 3203 |
if ((a->ts.type == BT_CLASS && !gfc_expr_attr (a).class_ok) |
| 3204 |
|| (mold->ts.type == BT_CLASS && !gfc_expr_attr (mold).class_ok)) |
| 3205 |
return NULL; |
| 3206 |
|
| 3207 |
/* Return .false. if the dynamic type can never be an extension. */ |
| 3208 |
if ((a->ts.type == BT_CLASS && mold->ts.type == BT_CLASS |
| 3209 |
&& !gfc_type_is_extension_of |
| 3210 |
(CLASS_DATA (mold)->ts.u.derived, |
| 3211 |
CLASS_DATA (a)->ts.u.derived) |
| 3212 |
&& !gfc_type_is_extension_of |
| 3213 |
(CLASS_DATA (a)->ts.u.derived, |
| 3214 |
CLASS_DATA (mold)->ts.u.derived)) |
| 3215 |
|| (a->ts.type == BT_DERIVED && mold->ts.type == BT_CLASS |
| 3216 |
&& !gfc_type_is_extension_of |
| 3217 |
(CLASS_DATA (mold)->ts.u.derived, |
| 3218 |
a->ts.u.derived)) |
| 3219 |
|| (a->ts.type == BT_CLASS && mold->ts.type == BT_DERIVED |
| 3220 |
&& !gfc_type_is_extension_of |
| 3221 |
(mold->ts.u.derived, |
| 3222 |
CLASS_DATA (a)->ts.u.derived) |
| 3223 |
&& !gfc_type_is_extension_of |
| 3224 |
(CLASS_DATA (a)->ts.u.derived, |
| 3225 |
mold->ts.u.derived))) |
| 3226 |
return gfc_get_logical_expr (gfc_default_logical_kind, &a->where, false); |
| 3227 |
|
| 3228 |
/* Return .true. if the dynamic type is guaranteed to be an extension. */ |
| 3229 |
if (a->ts.type == BT_CLASS && mold->ts.type == BT_DERIVED |
| 3230 |
&& gfc_type_is_extension_of (mold->ts.u.derived, |
| 3231 |
CLASS_DATA (a)->ts.u.derived)) |
| 3232 |
return gfc_get_logical_expr (gfc_default_logical_kind, &a->where, true); |
| 3233 |
|
| 3234 |
return NULL; |
| 3235 |
} |
| 3236 |
|
| 3237 |
|
| 3238 |
gfc_expr * |
| 3239 |
gfc_simplify_same_type_as (gfc_expr *a, gfc_expr *b) |
| 3240 |
{ |
| 3241 |
/* Avoid simplification of resolved symbols. */ |
| 3242 |
if (is_last_ref_vtab (a) || is_last_ref_vtab (b)) |
| 3243 |
return NULL; |
| 3244 |
|
| 3245 |
/* Return .false. if the dynamic type can never be the |
| 3246 |
same. */ |
| 3247 |
if (((a->ts.type == BT_CLASS && gfc_expr_attr (a).class_ok) |
| 3248 |
|| (b->ts.type == BT_CLASS && gfc_expr_attr (b).class_ok)) |
| 3249 |
&& !gfc_type_compatible (&a->ts, &b->ts) |
| 3250 |
&& !gfc_type_compatible (&b->ts, &a->ts)) |
| 3251 |
return gfc_get_logical_expr (gfc_default_logical_kind, &a->where, false); |
| 3252 |
|
| 3253 |
if (a->ts.type != BT_DERIVED || b->ts.type != BT_DERIVED) |
| 3254 |
return NULL; |
| 3255 |
|
| 3256 |
return gfc_get_logical_expr (gfc_default_logical_kind, &a->where, |
| 3257 |
gfc_compare_derived_types (a->ts.u.derived, |
| 3258 |
b->ts.u.derived)); |
| 3259 |
} |
| 3260 |
|
| 3261 |
|
| 3262 |
gfc_expr * |
| 3263 |
gfc_simplify_floor (gfc_expr *e, gfc_expr *k) |
| 3264 |
{ |
| 3265 |
gfc_expr *result; |
| 3266 |
mpfr_t floor; |
| 3267 |
int kind; |
| 3268 |
|
| 3269 |
kind = get_kind (BT_INTEGER, k, "FLOOR", gfc_default_integer_kind); |
| 3270 |
if (kind == -1) |
| 3271 |
gfc_internal_error ("gfc_simplify_floor(): Bad kind"); |
| 3272 |
|
| 3273 |
if (e->expr_type != EXPR_CONSTANT) |
| 3274 |
return NULL; |
| 3275 |
|
| 3276 |
mpfr_init2 (floor, mpfr_get_prec (e->value.real)); |
| 3277 |
mpfr_floor (floor, e->value.real); |
| 3278 |
|
| 3279 |
result = gfc_get_constant_expr (BT_INTEGER, kind, &e->where); |
| 3280 |
gfc_mpfr_to_mpz (result->value.integer, floor, &e->where); |
| 3281 |
|
| 3282 |
mpfr_clear (floor); |
| 3283 |
|
| 3284 |
return range_check (result, "FLOOR"); |
| 3285 |
} |
| 3286 |
|
| 3287 |
|
| 3288 |
gfc_expr * |
| 3289 |
gfc_simplify_fraction (gfc_expr *x) |
| 3290 |
{ |
| 3291 |
gfc_expr *result; |
| 3292 |
mpfr_exp_t e; |
| 3293 |
|
| 3294 |
if (x->expr_type != EXPR_CONSTANT) |
| 3295 |
return NULL; |
| 3296 |
|
| 3297 |
result = gfc_get_constant_expr (BT_REAL, x->ts.kind, &x->where); |
| 3298 |
|
| 3299 |
/* FRACTION(inf) = NaN. */ |
| 3300 |
if (mpfr_inf_p (x->value.real)) |
| 3301 |
{ |
| 3302 |
mpfr_set_nan (result->value.real); |
| 3303 |
return result; |
| 3304 |
} |
| 3305 |
|
| 3306 |
/* mpfr_frexp() correctly handles zeros and NaNs. */ |
| 3307 |
mpfr_frexp (&e, result->value.real, x->value.real, GFC_RND_MODE); |
| 3308 |
|
| 3309 |
return range_check (result, "FRACTION"); |
| 3310 |
} |
| 3311 |
|
| 3312 |
|
| 3313 |
gfc_expr * |
| 3314 |
gfc_simplify_gamma (gfc_expr *x) |
| 3315 |
{ |
| 3316 |
gfc_expr *result; |
| 3317 |
|
| 3318 |
if (x->expr_type != EXPR_CONSTANT) |
| 3319 |
return NULL; |
| 3320 |
|
| 3321 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 3322 |
mpfr_gamma (result->value.real, x->value.real, GFC_RND_MODE); |
| 3323 |
|
| 3324 |
return range_check (result, "GAMMA"); |
| 3325 |
} |
| 3326 |
|
| 3327 |
|
| 3328 |
gfc_expr * |
| 3329 |
gfc_simplify_huge (gfc_expr *e) |
| 3330 |
{ |
| 3331 |
gfc_expr *result; |
| 3332 |
int i; |
| 3333 |
|
| 3334 |
i = gfc_validate_kind (e->ts.type, e->ts.kind, false); |
| 3335 |
result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where); |
| 3336 |
|
| 3337 |
switch (e->ts.type) |
| 3338 |
{ |
| 3339 |
case BT_INTEGER: |
| 3340 |
mpz_set (result->value.integer, gfc_integer_kinds[i].huge); |
| 3341 |
break; |
| 3342 |
|
| 3343 |
case BT_UNSIGNED: |
| 3344 |
mpz_set (result->value.integer, gfc_unsigned_kinds[i].huge); |
| 3345 |
break; |
| 3346 |
|
| 3347 |
case BT_REAL: |
| 3348 |
mpfr_set (result->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE); |
| 3349 |
break; |
| 3350 |
|
| 3351 |
default: |
| 3352 |
gcc_unreachable (); |
| 3353 |
} |
| 3354 |
|
| 3355 |
return result; |
| 3356 |
} |
| 3357 |
|
| 3358 |
|
| 3359 |
gfc_expr * |
| 3360 |
gfc_simplify_hypot (gfc_expr *x, gfc_expr *y) |
| 3361 |
{ |
| 3362 |
gfc_expr *result; |
| 3363 |
|
| 3364 |
if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT) |
| 3365 |
return NULL; |
| 3366 |
|
| 3367 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 3368 |
mpfr_hypot (result->value.real, x->value.real, y->value.real, GFC_RND_MODE); |
| 3369 |
return range_check (result, "HYPOT"); |
| 3370 |
} |
| 3371 |
|
| 3372 |
|
| 3373 |
/* We use the processor's collating sequence, because all |
| 3374 |
systems that gfortran currently works on are ASCII. */ |
| 3375 |
|
| 3376 |
gfc_expr * |
| 3377 |
gfc_simplify_iachar (gfc_expr *e, gfc_expr *kind) |
| 3378 |
{ |
| 3379 |
gfc_expr *result; |
| 3380 |
gfc_char_t index; |
| 3381 |
int k; |
| 3382 |
|
| 3383 |
if (e->expr_type != EXPR_CONSTANT) |
| 3384 |
return NULL; |
| 3385 |
|
| 3386 |
if (e->value.character.length != 1) |
| 3387 |
{ |
| 3388 |
gfc_error ("Argument of IACHAR at %L must be of length one", &e->where); |
| 3389 |
return &gfc_bad_expr; |
| 3390 |
} |
| 3391 |
|
| 3392 |
index = e->value.character.string[0]; |
| 3393 |
|
| 3394 |
if (warn_surprising && index > 127) |
| 3395 |
gfc_warning (OPT_Wsurprising, |
| 3396 |
"Argument of IACHAR function at %L outside of range 0..127", |
| 3397 |
&e->where); |
| 3398 |
|
| 3399 |
k = get_kind (BT_INTEGER, kind, "IACHAR", gfc_default_integer_kind); |
| 3400 |
if (k == -1) |
| 3401 |
return &gfc_bad_expr; |
| 3402 |
|
| 3403 |
result = gfc_get_int_expr (k, &e->where, index); |
| 3404 |
|
| 3405 |
return range_check (result, "IACHAR"); |
| 3406 |
} |
| 3407 |
|
| 3408 |
|
| 3409 |
static gfc_expr * |
| 3410 |
do_bit_and (gfc_expr *result, gfc_expr *e) |
| 3411 |
{ |
| 3412 |
if (flag_unsigned) |
| 3413 |
{ |
| 3414 |
gcc_assert ((e->ts.type == BT_INTEGER || e->ts.type == BT_UNSIGNED) |
| 3415 |
&& e->expr_type == EXPR_CONSTANT); |
| 3416 |
gcc_assert ((result->ts.type == BT_INTEGER |
| 3417 |
|| result->ts.type == BT_UNSIGNED) |
| 3418 |
&& result->expr_type == EXPR_CONSTANT); |
| 3419 |
} |
| 3420 |
else |
| 3421 |
{ |
| 3422 |
gcc_assert (e->ts.type == BT_INTEGER && e->expr_type == EXPR_CONSTANT); |
| 3423 |
gcc_assert (result->ts.type == BT_INTEGER |
| 3424 |
&& result->expr_type == EXPR_CONSTANT); |
| 3425 |
} |
| 3426 |
|
| 3427 |
mpz_and (result->value.integer, result->value.integer, e->value.integer); |
| 3428 |
return result; |
| 3429 |
} |
| 3430 |
|
| 3431 |
|
| 3432 |
gfc_expr * |
| 3433 |
gfc_simplify_iall (gfc_expr *array, gfc_expr *dim, gfc_expr *mask) |
| 3434 |
{ |
| 3435 |
return simplify_transformation (array, dim, mask, -1, do_bit_and); |
| 3436 |
} |
| 3437 |
|
| 3438 |
|
| 3439 |
static gfc_expr * |
| 3440 |
do_bit_ior (gfc_expr *result, gfc_expr *e) |
| 3441 |
{ |
| 3442 |
if (flag_unsigned) |
| 3443 |
{ |
| 3444 |
gcc_assert ((e->ts.type == BT_INTEGER || e->ts.type == BT_UNSIGNED) |
| 3445 |
&& e->expr_type == EXPR_CONSTANT); |
| 3446 |
gcc_assert ((result->ts.type == BT_INTEGER |
| 3447 |
|| result->ts.type == BT_UNSIGNED) |
| 3448 |
&& result->expr_type == EXPR_CONSTANT); |
| 3449 |
} |
| 3450 |
else |
| 3451 |
{ |
| 3452 |
gcc_assert (e->ts.type == BT_INTEGER && e->expr_type == EXPR_CONSTANT); |
| 3453 |
gcc_assert (result->ts.type == BT_INTEGER |
| 3454 |
&& result->expr_type == EXPR_CONSTANT); |
| 3455 |
} |
| 3456 |
|
| 3457 |
mpz_ior (result->value.integer, result->value.integer, e->value.integer); |
| 3458 |
return result; |
| 3459 |
} |
| 3460 |
|
| 3461 |
|
| 3462 |
gfc_expr * |
| 3463 |
gfc_simplify_iany (gfc_expr *array, gfc_expr *dim, gfc_expr *mask) |
| 3464 |
{ |
| 3465 |
return simplify_transformation (array, dim, mask, 0, do_bit_ior); |
| 3466 |
} |
| 3467 |
|
| 3468 |
|
| 3469 |
gfc_expr * |
| 3470 |
gfc_simplify_iand (gfc_expr *x, gfc_expr *y) |
| 3471 |
{ |
| 3472 |
gfc_expr *result; |
| 3473 |
bt type; |
| 3474 |
|
| 3475 |
if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT) |
| 3476 |
return NULL; |
| 3477 |
|
| 3478 |
type = x->ts.type == BT_UNSIGNED ? BT_UNSIGNED : BT_INTEGER; |
| 3479 |
result = gfc_get_constant_expr (type, x->ts.kind, &x->where); |
| 3480 |
mpz_and (result->value.integer, x->value.integer, y->value.integer); |
| 3481 |
|
| 3482 |
return range_check (result, "IAND"); |
| 3483 |
} |
| 3484 |
|
| 3485 |
|
| 3486 |
gfc_expr * |
| 3487 |
gfc_simplify_ibclr (gfc_expr *x, gfc_expr *y) |
| 3488 |
{ |
| 3489 |
gfc_expr *result; |
| 3490 |
int k, pos; |
| 3491 |
|
| 3492 |
if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT) |
| 3493 |
return NULL; |
| 3494 |
|
| 3495 |
if (!gfc_check_bitfcn (x, y)) |
| 3496 |
return &gfc_bad_expr; |
| 3497 |
|
| 3498 |
gfc_extract_int (y, &pos); |
| 3499 |
|
| 3500 |
k = gfc_validate_kind (x->ts.type, x->ts.kind, false); |
| 3501 |
|
| 3502 |
result = gfc_copy_expr (x); |
| 3503 |
/* Drop any separate memory representation of x to avoid potential |
| 3504 |
inconsistencies in result. */ |
| 3505 |
if (result->representation.string) |
| 3506 |
{ |
| 3507 |
free (result->representation.string); |
| 3508 |
result->representation.string = NULL; |
| 3509 |
} |
| 3510 |
|
| 3511 |
if (x->ts.type == BT_INTEGER) |
| 3512 |
{ |
| 3513 |
gfc_convert_mpz_to_unsigned (result->value.integer, |
| 3514 |
gfc_integer_kinds[k].bit_size); |
| 3515 |
|
| 3516 |
mpz_clrbit (result->value.integer, pos); |
| 3517 |
|
| 3518 |
gfc_convert_mpz_to_signed (result->value.integer, |
| 3519 |
gfc_integer_kinds[k].bit_size); |
| 3520 |
} |
| 3521 |
else |
| 3522 |
mpz_clrbit (result->value.integer, pos); |
| 3523 |
|
| 3524 |
return result; |
| 3525 |
} |
| 3526 |
|
| 3527 |
|
| 3528 |
gfc_expr * |
| 3529 |
gfc_simplify_ibits (gfc_expr *x, gfc_expr *y, gfc_expr *z) |
| 3530 |
{ |
| 3531 |
gfc_expr *result; |
| 3532 |
int pos, len; |
| 3533 |
int i, k, bitsize; |
| 3534 |
int *bits; |
| 3535 |
|
| 3536 |
if (x->expr_type != EXPR_CONSTANT |
| 3537 |
|| y->expr_type != EXPR_CONSTANT |
| 3538 |
|| z->expr_type != EXPR_CONSTANT) |
| 3539 |
return NULL; |
| 3540 |
|
| 3541 |
if (!gfc_check_ibits (x, y, z)) |
| 3542 |
return &gfc_bad_expr; |
| 3543 |
|
| 3544 |
gfc_extract_int (y, &pos); |
| 3545 |
gfc_extract_int (z, &len); |
| 3546 |
|
| 3547 |
k = gfc_validate_kind (x->ts.type, x->ts.kind, false); |
| 3548 |
|
| 3549 |
if (x->ts.type == BT_INTEGER) |
| 3550 |
bitsize = gfc_integer_kinds[k].bit_size; |
| 3551 |
else |
| 3552 |
bitsize = gfc_unsigned_kinds[k].bit_size; |
| 3553 |
|
| 3554 |
|
| 3555 |
if (pos + len > bitsize) |
| 3556 |
{ |
| 3557 |
gfc_error ("Sum of second and third arguments of IBITS exceeds " |
| 3558 |
"bit size at %L", &y->where); |
| 3559 |
return &gfc_bad_expr; |
| 3560 |
} |
| 3561 |
|
| 3562 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 3563 |
|
| 3564 |
if (x->ts.type == BT_INTEGER) |
| 3565 |
gfc_convert_mpz_to_unsigned (result->value.integer, |
| 3566 |
gfc_integer_kinds[k].bit_size); |
| 3567 |
|
| 3568 |
bits = XCNEWVEC (int, bitsize); |
| 3569 |
|
| 3570 |
for (i = 0; i < bitsize; i++) |
| 3571 |
bits[i] = 0; |
| 3572 |
|
| 3573 |
for (i = 0; i < len; i++) |
| 3574 |
bits[i] = mpz_tstbit (x->value.integer, i + pos); |
| 3575 |
|
| 3576 |
for (i = 0; i < bitsize; i++) |
| 3577 |
{ |
| 3578 |
if (bits[i] == 0) |
| 3579 |
mpz_clrbit (result->value.integer, i); |
| 3580 |
else if (bits[i] == 1) |
| 3581 |
mpz_setbit (result->value.integer, i); |
| 3582 |
else |
| 3583 |
gfc_internal_error ("IBITS: Bad bit"); |
| 3584 |
} |
| 3585 |
|
| 3586 |
free (bits); |
| 3587 |
|
| 3588 |
if (x->ts.type == BT_INTEGER) |
| 3589 |
gfc_convert_mpz_to_signed (result->value.integer, |
| 3590 |
gfc_integer_kinds[k].bit_size); |
| 3591 |
|
| 3592 |
return result; |
| 3593 |
} |
| 3594 |
|
| 3595 |
|
| 3596 |
gfc_expr * |
| 3597 |
gfc_simplify_ibset (gfc_expr *x, gfc_expr *y) |
| 3598 |
{ |
| 3599 |
gfc_expr *result; |
| 3600 |
int k, pos; |
| 3601 |
|
| 3602 |
if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT) |
| 3603 |
return NULL; |
| 3604 |
|
| 3605 |
if (!gfc_check_bitfcn (x, y)) |
| 3606 |
return &gfc_bad_expr; |
| 3607 |
|
| 3608 |
gfc_extract_int (y, &pos); |
| 3609 |
|
| 3610 |
k = gfc_validate_kind (x->ts.type, x->ts.kind, false); |
| 3611 |
|
| 3612 |
result = gfc_copy_expr (x); |
| 3613 |
/* Drop any separate memory representation of x to avoid potential |
| 3614 |
inconsistencies in result. */ |
| 3615 |
if (result->representation.string) |
| 3616 |
{ |
| 3617 |
free (result->representation.string); |
| 3618 |
result->representation.string = NULL; |
| 3619 |
} |
| 3620 |
|
| 3621 |
if (x->ts.type == BT_INTEGER) |
| 3622 |
{ |
| 3623 |
gfc_convert_mpz_to_unsigned (result->value.integer, |
| 3624 |
gfc_integer_kinds[k].bit_size); |
| 3625 |
|
| 3626 |
mpz_setbit (result->value.integer, pos); |
| 3627 |
|
| 3628 |
gfc_convert_mpz_to_signed (result->value.integer, |
| 3629 |
gfc_integer_kinds[k].bit_size); |
| 3630 |
} |
| 3631 |
else |
| 3632 |
mpz_setbit (result->value.integer, pos); |
| 3633 |
|
| 3634 |
return result; |
| 3635 |
} |
| 3636 |
|
| 3637 |
|
| 3638 |
gfc_expr * |
| 3639 |
gfc_simplify_ichar (gfc_expr *e, gfc_expr *kind) |
| 3640 |
{ |
| 3641 |
gfc_expr *result; |
| 3642 |
gfc_char_t index; |
| 3643 |
int k; |
| 3644 |
|
| 3645 |
if (e->expr_type != EXPR_CONSTANT) |
| 3646 |
return NULL; |
| 3647 |
|
| 3648 |
if (e->value.character.length != 1) |
| 3649 |
{ |
| 3650 |
gfc_error ("Argument of ICHAR at %L must be of length one", &e->where); |
| 3651 |
return &gfc_bad_expr; |
| 3652 |
} |
| 3653 |
|
| 3654 |
index = e->value.character.string[0]; |
| 3655 |
|
| 3656 |
k = get_kind (BT_INTEGER, kind, "ICHAR", gfc_default_integer_kind); |
| 3657 |
if (k == -1) |
| 3658 |
return &gfc_bad_expr; |
| 3659 |
|
| 3660 |
result = gfc_get_int_expr (k, &e->where, index); |
| 3661 |
|
| 3662 |
return range_check (result, "ICHAR"); |
| 3663 |
} |
| 3664 |
|
| 3665 |
|
| 3666 |
gfc_expr * |
| 3667 |
gfc_simplify_ieor (gfc_expr *x, gfc_expr *y) |
| 3668 |
{ |
| 3669 |
gfc_expr *result; |
| 3670 |
bt type; |
| 3671 |
|
| 3672 |
if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT) |
| 3673 |
return NULL; |
| 3674 |
|
| 3675 |
type = x->ts.type == BT_UNSIGNED ? BT_UNSIGNED : BT_INTEGER; |
| 3676 |
result = gfc_get_constant_expr (type, x->ts.kind, &x->where); |
| 3677 |
mpz_xor (result->value.integer, x->value.integer, y->value.integer); |
| 3678 |
|
| 3679 |
return range_check (result, "IEOR"); |
| 3680 |
} |
| 3681 |
|
| 3682 |
|
| 3683 |
gfc_expr * |
| 3684 |
gfc_simplify_index (gfc_expr *x, gfc_expr *y, gfc_expr *b, gfc_expr *kind) |
| 3685 |
{ |
| 3686 |
gfc_expr *result; |
| 3687 |
bool back; |
| 3688 |
HOST_WIDE_INT len, lensub, start, last, i, index = 0; |
| 3689 |
int k, delta; |
| 3690 |
|
| 3691 |
if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT |
| 3692 |
|| ( b != NULL && b->expr_type != EXPR_CONSTANT)) |
| 3693 |
return NULL; |
| 3694 |
|
| 3695 |
back = (b != NULL && b->value.logical != 0); |
| 3696 |
|
| 3697 |
k = get_kind (BT_INTEGER, kind, "INDEX", gfc_default_integer_kind); |
| 3698 |
if (k == -1) |
| 3699 |
return &gfc_bad_expr; |
| 3700 |
|
| 3701 |
result = gfc_get_constant_expr (BT_INTEGER, k, &x->where); |
| 3702 |
|
| 3703 |
len = x->value.character.length; |
| 3704 |
lensub = y->value.character.length; |
| 3705 |
|
| 3706 |
if (len < lensub) |
| 3707 |
{ |
| 3708 |
mpz_set_si (result->value.integer, 0); |
| 3709 |
return result; |
| 3710 |
} |
| 3711 |
|
| 3712 |
if (lensub == 0) |
| 3713 |
{ |
| 3714 |
if (back) |
| 3715 |
index = len + 1; |
| 3716 |
else |
| 3717 |
index = 1; |
| 3718 |
goto done; |
| 3719 |
} |
| 3720 |
|
| 3721 |
if (!back) |
| 3722 |
{ |
| 3723 |
last = len + 1 - lensub; |
| 3724 |
start = 0; |
| 3725 |
delta = 1; |
| 3726 |
} |
| 3727 |
else |
| 3728 |
{ |
| 3729 |
last = -1; |
| 3730 |
start = len - lensub; |
| 3731 |
delta = -1; |
| 3732 |
} |
| 3733 |
|
| 3734 |
for (; start != last; start += delta) |
| 3735 |
{ |
| 3736 |
for (i = 0; i < lensub; i++) |
| 3737 |
{ |
| 3738 |
if (x->value.character.string[start + i] |
| 3739 |
!= y->value.character.string[i]) |
| 3740 |
break; |
| 3741 |
} |
| 3742 |
if (i == lensub) |
| 3743 |
{ |
| 3744 |
index = start + 1; |
| 3745 |
goto done; |
| 3746 |
} |
| 3747 |
} |
| 3748 |
|
| 3749 |
done: |
| 3750 |
mpz_set_si (result->value.integer, index); |
| 3751 |
return range_check (result, "INDEX"); |
| 3752 |
} |
| 3753 |
|
| 3754 |
static gfc_expr * |
| 3755 |
simplify_intconv (gfc_expr *e, int kind, const char *name) |
| 3756 |
{ |
| 3757 |
gfc_expr *result = NULL; |
| 3758 |
int tmp1, tmp2; |
| 3759 |
|
| 3760 |
/* Convert BOZ to integer, and return without range checking. */ |
| 3761 |
if (e->ts.type == BT_BOZ) |
| 3762 |
{ |
| 3763 |
if (!gfc_boz2int (e, kind)) |
| 3764 |
return NULL; |
| 3765 |
result = gfc_copy_expr (e); |
| 3766 |
return result; |
| 3767 |
} |
| 3768 |
|
| 3769 |
if (e->expr_type != EXPR_CONSTANT) |
| 3770 |
return NULL; |
| 3771 |
|
| 3772 |
/* For explicit conversion, turn off -Wconversion and -Wconversion-extra |
| 3773 |
warnings. */ |
| 3774 |
tmp1 = warn_conversion; |
| 3775 |
tmp2 = warn_conversion_extra; |
| 3776 |
warn_conversion = warn_conversion_extra = 0; |
| 3777 |
|
| 3778 |
result = gfc_convert_constant (e, BT_INTEGER, kind); |
| 3779 |
|
| 3780 |
warn_conversion = tmp1; |
| 3781 |
warn_conversion_extra = tmp2; |
| 3782 |
|
| 3783 |
if (result == &gfc_bad_expr) |
| 3784 |
return &gfc_bad_expr; |
| 3785 |
|
| 3786 |
return range_check (result, name); |
| 3787 |
} |
| 3788 |
|
| 3789 |
|
| 3790 |
gfc_expr * |
| 3791 |
gfc_simplify_int (gfc_expr *e, gfc_expr *k) |
| 3792 |
{ |
| 3793 |
int kind; |
| 3794 |
|
| 3795 |
kind = get_kind (BT_INTEGER, k, "INT", gfc_default_integer_kind); |
| 3796 |
if (kind == -1) |
| 3797 |
return &gfc_bad_expr; |
| 3798 |
|
| 3799 |
return simplify_intconv (e, kind, "INT"); |
| 3800 |
} |
| 3801 |
|
| 3802 |
gfc_expr * |
| 3803 |
gfc_simplify_int2 (gfc_expr *e) |
| 3804 |
{ |
| 3805 |
return simplify_intconv (e, 2, "INT2"); |
| 3806 |
} |
| 3807 |
|
| 3808 |
|
| 3809 |
gfc_expr * |
| 3810 |
gfc_simplify_int8 (gfc_expr *e) |
| 3811 |
{ |
| 3812 |
return simplify_intconv (e, 8, "INT8"); |
| 3813 |
} |
| 3814 |
|
| 3815 |
|
| 3816 |
gfc_expr * |
| 3817 |
gfc_simplify_long (gfc_expr *e) |
| 3818 |
{ |
| 3819 |
return simplify_intconv (e, 4, "LONG"); |
| 3820 |
} |
| 3821 |
|
| 3822 |
|
| 3823 |
gfc_expr * |
| 3824 |
gfc_simplify_ifix (gfc_expr *e) |
| 3825 |
{ |
| 3826 |
gfc_expr *rtrunc, *result; |
| 3827 |
|
| 3828 |
if (e->expr_type != EXPR_CONSTANT) |
| 3829 |
return NULL; |
| 3830 |
|
| 3831 |
rtrunc = gfc_copy_expr (e); |
| 3832 |
mpfr_trunc (rtrunc->value.real, e->value.real); |
| 3833 |
|
| 3834 |
result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind, |
| 3835 |
&e->where); |
| 3836 |
gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real, &e->where); |
| 3837 |
|
| 3838 |
gfc_free_expr (rtrunc); |
| 3839 |
|
| 3840 |
return range_check (result, "IFIX"); |
| 3841 |
} |
| 3842 |
|
| 3843 |
|
| 3844 |
gfc_expr * |
| 3845 |
gfc_simplify_idint (gfc_expr *e) |
| 3846 |
{ |
| 3847 |
gfc_expr *rtrunc, *result; |
| 3848 |
|
| 3849 |
if (e->expr_type != EXPR_CONSTANT) |
| 3850 |
return NULL; |
| 3851 |
|
| 3852 |
rtrunc = gfc_copy_expr (e); |
| 3853 |
mpfr_trunc (rtrunc->value.real, e->value.real); |
| 3854 |
|
| 3855 |
result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind, |
| 3856 |
&e->where); |
| 3857 |
gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real, &e->where); |
| 3858 |
|
| 3859 |
gfc_free_expr (rtrunc); |
| 3860 |
|
| 3861 |
return range_check (result, "IDINT"); |
| 3862 |
} |
| 3863 |
|
| 3864 |
gfc_expr * |
| 3865 |
gfc_simplify_uint (gfc_expr *e, gfc_expr *k) |
| 3866 |
{ |
| 3867 |
gfc_expr *result = NULL; |
| 3868 |
int kind; |
| 3869 |
|
| 3870 |
/* KIND is always an integer. */ |
| 3871 |
|
| 3872 |
kind = get_kind (BT_INTEGER, k, "INT", gfc_default_integer_kind); |
| 3873 |
if (kind == -1) |
| 3874 |
return &gfc_bad_expr; |
| 3875 |
|
| 3876 |
/* Convert BOZ to integer, and return without range checking. */ |
| 3877 |
if (e->ts.type == BT_BOZ) |
| 3878 |
{ |
| 3879 |
if (!gfc_boz2uint (e, kind)) |
| 3880 |
return NULL; |
| 3881 |
result = gfc_copy_expr (e); |
| 3882 |
return result; |
| 3883 |
} |
| 3884 |
|
| 3885 |
if (e->expr_type != EXPR_CONSTANT) |
| 3886 |
return NULL; |
| 3887 |
|
| 3888 |
result = gfc_convert_constant (e, BT_UNSIGNED, kind); |
| 3889 |
|
| 3890 |
if (result == &gfc_bad_expr) |
| 3891 |
return &gfc_bad_expr; |
| 3892 |
|
| 3893 |
return range_check (result, "UINT"); |
| 3894 |
} |
| 3895 |
|
| 3896 |
|
| 3897 |
gfc_expr * |
| 3898 |
gfc_simplify_ior (gfc_expr *x, gfc_expr *y) |
| 3899 |
{ |
| 3900 |
gfc_expr *result; |
| 3901 |
bt type; |
| 3902 |
|
| 3903 |
if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT) |
| 3904 |
return NULL; |
| 3905 |
|
| 3906 |
type = x->ts.type == BT_UNSIGNED ? BT_UNSIGNED : BT_INTEGER; |
| 3907 |
result = gfc_get_constant_expr (type, x->ts.kind, &x->where); |
| 3908 |
mpz_ior (result->value.integer, x->value.integer, y->value.integer); |
| 3909 |
|
| 3910 |
return range_check (result, "IOR"); |
| 3911 |
} |
| 3912 |
|
| 3913 |
|
| 3914 |
static gfc_expr * |
| 3915 |
do_bit_xor (gfc_expr *result, gfc_expr *e) |
| 3916 |
{ |
| 3917 |
if (flag_unsigned) |
| 3918 |
{ |
| 3919 |
gcc_assert ((e->ts.type == BT_INTEGER || e->ts.type == BT_UNSIGNED) |
| 3920 |
&& e->expr_type == EXPR_CONSTANT); |
| 3921 |
gcc_assert ((result->ts.type == BT_INTEGER |
| 3922 |
|| result->ts.type == BT_UNSIGNED) |
| 3923 |
&& result->expr_type == EXPR_CONSTANT); |
| 3924 |
} |
| 3925 |
else |
| 3926 |
{ |
| 3927 |
gcc_assert (e->ts.type == BT_INTEGER && e->expr_type == EXPR_CONSTANT); |
| 3928 |
gcc_assert (result->ts.type == BT_INTEGER |
| 3929 |
&& result->expr_type == EXPR_CONSTANT); |
| 3930 |
} |
| 3931 |
|
| 3932 |
mpz_xor (result->value.integer, result->value.integer, e->value.integer); |
| 3933 |
return result; |
| 3934 |
} |
| 3935 |
|
| 3936 |
|
| 3937 |
gfc_expr * |
| 3938 |
gfc_simplify_iparity (gfc_expr *array, gfc_expr *dim, gfc_expr *mask) |
| 3939 |
{ |
| 3940 |
return simplify_transformation (array, dim, mask, 0, do_bit_xor); |
| 3941 |
} |
| 3942 |
|
| 3943 |
|
| 3944 |
gfc_expr * |
| 3945 |
gfc_simplify_is_iostat_end (gfc_expr *x) |
| 3946 |
{ |
| 3947 |
if (x->expr_type != EXPR_CONSTANT) |
| 3948 |
return NULL; |
| 3949 |
|
| 3950 |
return gfc_get_logical_expr (gfc_default_logical_kind, &x->where, |
| 3951 |
mpz_cmp_si (x->value.integer, |
| 3952 |
LIBERROR_END) == 0); |
| 3953 |
} |
| 3954 |
|
| 3955 |
|
| 3956 |
gfc_expr * |
| 3957 |
gfc_simplify_is_iostat_eor (gfc_expr *x) |
| 3958 |
{ |
| 3959 |
if (x->expr_type != EXPR_CONSTANT) |
| 3960 |
return NULL; |
| 3961 |
|
| 3962 |
return gfc_get_logical_expr (gfc_default_logical_kind, &x->where, |
| 3963 |
mpz_cmp_si (x->value.integer, |
| 3964 |
LIBERROR_EOR) == 0); |
| 3965 |
} |
| 3966 |
|
| 3967 |
|
| 3968 |
gfc_expr * |
| 3969 |
gfc_simplify_isnan (gfc_expr *x) |
| 3970 |
{ |
| 3971 |
if (x->expr_type != EXPR_CONSTANT) |
| 3972 |
return NULL; |
| 3973 |
|
| 3974 |
return gfc_get_logical_expr (gfc_default_logical_kind, &x->where, |
| 3975 |
mpfr_nan_p (x->value.real)); |
| 3976 |
} |
| 3977 |
|
| 3978 |
|
| 3979 |
/* Performs a shift on its first argument. Depending on the last |
| 3980 |
argument, the shift can be arithmetic, i.e. with filling from the |
| 3981 |
left like in the SHIFTA intrinsic. */ |
| 3982 |
static gfc_expr * |
| 3983 |
simplify_shift (gfc_expr *e, gfc_expr *s, const char *name, |
| 3984 |
bool arithmetic, int direction) |
| 3985 |
{ |
| 3986 |
gfc_expr *result; |
| 3987 |
int ashift, *bits, i, k, bitsize, shift; |
| 3988 |
|
| 3989 |
if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT) |
| 3990 |
return NULL; |
| 3991 |
|
| 3992 |
gfc_extract_int (s, &shift); |
| 3993 |
|
| 3994 |
k = gfc_validate_kind (e->ts.type, e->ts.kind, false); |
| 3995 |
if (e->ts.type == BT_INTEGER) |
| 3996 |
bitsize = gfc_integer_kinds[k].bit_size; |
| 3997 |
else |
| 3998 |
bitsize = gfc_unsigned_kinds[k].bit_size; |
| 3999 |
|
| 4000 |
result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where); |
| 4001 |
|
| 4002 |
if (shift == 0) |
| 4003 |
{ |
| 4004 |
mpz_set (result->value.integer, e->value.integer); |
| 4005 |
return result; |
| 4006 |
} |
| 4007 |
|
| 4008 |
if (direction > 0 && shift < 0) |
| 4009 |
{ |
| 4010 |
/* Left shift, as in SHIFTL. */ |
| 4011 |
gfc_error ("Second argument of %s is negative at %L", name, &e->where); |
| 4012 |
return &gfc_bad_expr; |
| 4013 |
} |
| 4014 |
else if (direction < 0) |
| 4015 |
{ |
| 4016 |
/* Right shift, as in SHIFTR or SHIFTA. */ |
| 4017 |
if (shift < 0) |
| 4018 |
{ |
| 4019 |
gfc_error ("Second argument of %s is negative at %L", |
| 4020 |
name, &e->where); |
| 4021 |
return &gfc_bad_expr; |
| 4022 |
} |
| 4023 |
|
| 4024 |
shift = -shift; |
| 4025 |
} |
| 4026 |
|
| 4027 |
ashift = (shift >= 0 ? shift : -shift); |
| 4028 |
|
| 4029 |
if (ashift > bitsize) |
| 4030 |
{ |
| 4031 |
gfc_error ("Magnitude of second argument of %s exceeds bit size " |
| 4032 |
"at %L", name, &e->where); |
| 4033 |
return &gfc_bad_expr; |
| 4034 |
} |
| 4035 |
|
| 4036 |
bits = XCNEWVEC (int, bitsize); |
| 4037 |
|
| 4038 |
for (i = 0; i < bitsize; i++) |
| 4039 |
bits[i] = mpz_tstbit (e->value.integer, i); |
| 4040 |
|
| 4041 |
if (shift > 0) |
| 4042 |
{ |
| 4043 |
/* Left shift. */ |
| 4044 |
for (i = 0; i < shift; i++) |
| 4045 |
mpz_clrbit (result->value.integer, i); |
| 4046 |
|
| 4047 |
for (i = 0; i < bitsize - shift; i++) |
| 4048 |
{ |
| 4049 |
if (bits[i] == 0) |
| 4050 |
mpz_clrbit (result->value.integer, i + shift); |
| 4051 |
else |
| 4052 |
mpz_setbit (result->value.integer, i + shift); |
| 4053 |
} |
| 4054 |
} |
| 4055 |
else |
| 4056 |
{ |
| 4057 |
/* Right shift. */ |
| 4058 |
if (arithmetic && bits[bitsize - 1]) |
| 4059 |
for (i = bitsize - 1; i >= bitsize - ashift; i--) |
| 4060 |
mpz_setbit (result->value.integer, i); |
| 4061 |
else |
| 4062 |
for (i = bitsize - 1; i >= bitsize - ashift; i--) |
| 4063 |
mpz_clrbit (result->value.integer, i); |
| 4064 |
|
| 4065 |
for (i = bitsize - 1; i >= ashift; i--) |
| 4066 |
{ |
| 4067 |
if (bits[i] == 0) |
| 4068 |
mpz_clrbit (result->value.integer, i - ashift); |
| 4069 |
else |
| 4070 |
mpz_setbit (result->value.integer, i - ashift); |
| 4071 |
} |
| 4072 |
} |
| 4073 |
|
| 4074 |
if (result->ts.type == BT_INTEGER) |
| 4075 |
gfc_convert_mpz_to_signed (result->value.integer, bitsize); |
| 4076 |
else |
| 4077 |
gfc_reduce_unsigned(result); |
| 4078 |
|
| 4079 |
free (bits); |
| 4080 |
|
| 4081 |
return result; |
| 4082 |
} |
| 4083 |
|
| 4084 |
|
| 4085 |
gfc_expr * |
| 4086 |
gfc_simplify_ishft (gfc_expr *e, gfc_expr *s) |
| 4087 |
{ |
| 4088 |
return simplify_shift (e, s, "ISHFT", false, 0); |
| 4089 |
} |
| 4090 |
|
| 4091 |
|
| 4092 |
gfc_expr * |
| 4093 |
gfc_simplify_lshift (gfc_expr *e, gfc_expr *s) |
| 4094 |
{ |
| 4095 |
return simplify_shift (e, s, "LSHIFT", false, 1); |
| 4096 |
} |
| 4097 |
|
| 4098 |
|
| 4099 |
gfc_expr * |
| 4100 |
gfc_simplify_rshift (gfc_expr *e, gfc_expr *s) |
| 4101 |
{ |
| 4102 |
return simplify_shift (e, s, "RSHIFT", true, -1); |
| 4103 |
} |
| 4104 |
|
| 4105 |
|
| 4106 |
gfc_expr * |
| 4107 |
gfc_simplify_shifta (gfc_expr *e, gfc_expr *s) |
| 4108 |
{ |
| 4109 |
return simplify_shift (e, s, "SHIFTA", true, -1); |
| 4110 |
} |
| 4111 |
|
| 4112 |
|
| 4113 |
gfc_expr * |
| 4114 |
gfc_simplify_shiftl (gfc_expr *e, gfc_expr *s) |
| 4115 |
{ |
| 4116 |
return simplify_shift (e, s, "SHIFTL", false, 1); |
| 4117 |
} |
| 4118 |
|
| 4119 |
|
| 4120 |
gfc_expr * |
| 4121 |
gfc_simplify_shiftr (gfc_expr *e, gfc_expr *s) |
| 4122 |
{ |
| 4123 |
return simplify_shift (e, s, "SHIFTR", false, -1); |
| 4124 |
} |
| 4125 |
|
| 4126 |
|
| 4127 |
gfc_expr * |
| 4128 |
gfc_simplify_ishftc (gfc_expr *e, gfc_expr *s, gfc_expr *sz) |
| 4129 |
{ |
| 4130 |
gfc_expr *result; |
| 4131 |
int shift, ashift, isize, ssize, delta, k; |
| 4132 |
int i, *bits; |
| 4133 |
|
| 4134 |
if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT) |
| 4135 |
return NULL; |
| 4136 |
|
| 4137 |
gfc_extract_int (s, &shift); |
| 4138 |
|
| 4139 |
k = gfc_validate_kind (e->ts.type, e->ts.kind, false); |
| 4140 |
isize = gfc_integer_kinds[k].bit_size; |
| 4141 |
|
| 4142 |
if (sz != NULL) |
| 4143 |
{ |
| 4144 |
if (sz->expr_type != EXPR_CONSTANT) |
| 4145 |
return NULL; |
| 4146 |
|
| 4147 |
gfc_extract_int (sz, &ssize); |
| 4148 |
|
| 4149 |
if (ssize > isize || ssize <= 0) |
| 4150 |
return &gfc_bad_expr; |
| 4151 |
} |
| 4152 |
else |
| 4153 |
ssize = isize; |
| 4154 |
|
| 4155 |
if (shift >= 0) |
| 4156 |
ashift = shift; |
| 4157 |
else |
| 4158 |
ashift = -shift; |
| 4159 |
|
| 4160 |
if (ashift > ssize) |
| 4161 |
{ |
| 4162 |
if (sz == NULL) |
| 4163 |
gfc_error ("Magnitude of second argument of ISHFTC exceeds " |
| 4164 |
"BIT_SIZE of first argument at %C"); |
| 4165 |
else |
| 4166 |
gfc_error ("Absolute value of SHIFT shall be less than or equal " |
| 4167 |
"to SIZE at %C"); |
| 4168 |
return &gfc_bad_expr; |
| 4169 |
} |
| 4170 |
|
| 4171 |
result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where); |
| 4172 |
|
| 4173 |
mpz_set (result->value.integer, e->value.integer); |
| 4174 |
|
| 4175 |
if (shift == 0) |
| 4176 |
return result; |
| 4177 |
|
| 4178 |
if (result->ts.type == BT_INTEGER) |
| 4179 |
gfc_convert_mpz_to_unsigned (result->value.integer, isize); |
| 4180 |
|
| 4181 |
bits = XCNEWVEC (int, ssize); |
| 4182 |
|
| 4183 |
for (i = 0; i < ssize; i++) |
| 4184 |
bits[i] = mpz_tstbit (e->value.integer, i); |
| 4185 |
|
| 4186 |
delta = ssize - ashift; |
| 4187 |
|
| 4188 |
if (shift > 0) |
| 4189 |
{ |
| 4190 |
for (i = 0; i < delta; i++) |
| 4191 |
{ |
| 4192 |
if (bits[i] == 0) |
| 4193 |
mpz_clrbit (result->value.integer, i + shift); |
| 4194 |
else |
| 4195 |
mpz_setbit (result->value.integer, i + shift); |
| 4196 |
} |
| 4197 |
|
| 4198 |
for (i = delta; i < ssize; i++) |
| 4199 |
{ |
| 4200 |
if (bits[i] == 0) |
| 4201 |
mpz_clrbit (result->value.integer, i - delta); |
| 4202 |
else |
| 4203 |
mpz_setbit (result->value.integer, i - delta); |
| 4204 |
} |
| 4205 |
} |
| 4206 |
else |
| 4207 |
{ |
| 4208 |
for (i = 0; i < ashift; i++) |
| 4209 |
{ |
| 4210 |
if (bits[i] == 0) |
| 4211 |
mpz_clrbit (result->value.integer, i + delta); |
| 4212 |
else |
| 4213 |
mpz_setbit (result->value.integer, i + delta); |
| 4214 |
} |
| 4215 |
|
| 4216 |
for (i = ashift; i < ssize; i++) |
| 4217 |
{ |
| 4218 |
if (bits[i] == 0) |
| 4219 |
mpz_clrbit (result->value.integer, i + shift); |
| 4220 |
else |
| 4221 |
mpz_setbit (result->value.integer, i + shift); |
| 4222 |
} |
| 4223 |
} |
| 4224 |
|
| 4225 |
if (result->ts.type == BT_INTEGER) |
| 4226 |
gfc_convert_mpz_to_signed (result->value.integer, isize); |
| 4227 |
|
| 4228 |
free (bits); |
| 4229 |
return result; |
| 4230 |
} |
| 4231 |
|
| 4232 |
|
| 4233 |
gfc_expr * |
| 4234 |
gfc_simplify_kind (gfc_expr *e) |
| 4235 |
{ |
| 4236 |
return gfc_get_int_expr (gfc_default_integer_kind, NULL, e->ts.kind); |
| 4237 |
} |
| 4238 |
|
| 4239 |
|
| 4240 |
static gfc_expr * |
| 4241 |
simplify_bound_dim (gfc_expr *array, gfc_expr *kind, int d, int upper, |
| 4242 |
gfc_array_spec *as, gfc_ref *ref, bool coarray) |
| 4243 |
{ |
| 4244 |
gfc_expr *l, *u, *result; |
| 4245 |
int k; |
| 4246 |
|
| 4247 |
k = get_kind (BT_INTEGER, kind, upper ? "UBOUND" : "LBOUND", |
| 4248 |
gfc_default_integer_kind); |
| 4249 |
if (k == -1) |
| 4250 |
return &gfc_bad_expr; |
| 4251 |
|
| 4252 |
result = gfc_get_constant_expr (BT_INTEGER, k, &array->where); |
| 4253 |
|
| 4254 |
/* For non-variables, LBOUND(expr, DIM=n) = 1 and |
| 4255 |
UBOUND(expr, DIM=n) = SIZE(expr, DIM=n). */ |
| 4256 |
if (!coarray && array->expr_type != EXPR_VARIABLE) |
| 4257 |
{ |
| 4258 |
if (upper) |
| 4259 |
{ |
| 4260 |
gfc_expr* dim = result; |
| 4261 |
mpz_set_si (dim->value.integer, d); |
| 4262 |
|
| 4263 |
result = simplify_size (array, dim, k); |
| 4264 |
gfc_free_expr (dim); |
| 4265 |
if (!result) |
| 4266 |
goto returnNull; |
| 4267 |
} |
| 4268 |
else |
| 4269 |
mpz_set_si (result->value.integer, 1); |
| 4270 |
|
| 4271 |
goto done; |
| 4272 |
} |
| 4273 |
|
| 4274 |
/* Otherwise, we have a variable expression. */ |
| 4275 |
gcc_assert (array->expr_type == EXPR_VARIABLE); |
| 4276 |
gcc_assert (as); |
| 4277 |
|
| 4278 |
if (!gfc_resolve_array_spec (as, 0)) |
| 4279 |
return NULL; |
| 4280 |
|
| 4281 |
/* The last dimension of an assumed-size array is special. */ |
| 4282 |
if ((!coarray && d == as->rank && as->type == AS_ASSUMED_SIZE && !upper) |
| 4283 |
|| (coarray && d == as->rank + as->corank |
| 4284 |
&& (!upper || flag_coarray == GFC_FCOARRAY_SINGLE))) |
| 4285 |
{ |
| 4286 |
if (as->lower[d-1] && as->lower[d-1]->expr_type == EXPR_CONSTANT) |
| 4287 |
{ |
| 4288 |
gfc_free_expr (result); |
| 4289 |
return gfc_copy_expr (as->lower[d-1]); |
| 4290 |
} |
| 4291 |
|
| 4292 |
goto returnNull; |
| 4293 |
} |
| 4294 |
|
| 4295 |
/* Then, we need to know the extent of the given dimension. */ |
| 4296 |
if (coarray || (ref->u.ar.type == AR_FULL && !ref->next)) |
| 4297 |
{ |
| 4298 |
gfc_expr *declared_bound; |
| 4299 |
int empty_bound; |
| 4300 |
bool constant_lbound, constant_ubound; |
| 4301 |
|
| 4302 |
l = as->lower[d-1]; |
| 4303 |
u = as->upper[d-1]; |
| 4304 |
|
| 4305 |
gcc_assert (l != NULL); |
| 4306 |
|
| 4307 |
constant_lbound = l->expr_type == EXPR_CONSTANT; |
| 4308 |
constant_ubound = u && u->expr_type == EXPR_CONSTANT; |
| 4309 |
|
| 4310 |
empty_bound = upper ? 0 : 1; |
| 4311 |
declared_bound = upper ? u : l; |
| 4312 |
|
| 4313 |
if ((!upper && !constant_lbound) |
| 4314 |
|| (upper && !constant_ubound)) |
| 4315 |
goto returnNull; |
| 4316 |
|
| 4317 |
if (!coarray) |
| 4318 |
{ |
| 4319 |
/* For {L,U}BOUND, the value depends on whether the array |
| 4320 |
is empty. We can nevertheless simplify if the declared bound |
| 4321 |
has the same value as that of an empty array, in which case |
| 4322 |
the result isn't dependent on the array emptiness. */ |
| 4323 |
if (mpz_cmp_si (declared_bound->value.integer, empty_bound) == 0) |
| 4324 |
mpz_set_si (result->value.integer, empty_bound); |
| 4325 |
else if (!constant_lbound || !constant_ubound) |
| 4326 |
/* Array emptiness can't be determined, we can't simplify. */ |
| 4327 |
goto returnNull; |
| 4328 |
else if (mpz_cmp (l->value.integer, u->value.integer) > 0) |
| 4329 |
mpz_set_si (result->value.integer, empty_bound); |
| 4330 |
else |
| 4331 |
mpz_set (result->value.integer, declared_bound->value.integer); |
| 4332 |
} |
| 4333 |
else |
| 4334 |
mpz_set (result->value.integer, declared_bound->value.integer); |
| 4335 |
} |
| 4336 |
else |
| 4337 |
{ |
| 4338 |
if (upper) |
| 4339 |
{ |
| 4340 |
int d2 = 0, cnt = 0; |
| 4341 |
for (int idx = 0; idx < ref->u.ar.dimen; ++idx) |
| 4342 |
{ |
| 4343 |
if (ref->u.ar.dimen_type[idx] == DIMEN_ELEMENT) |
| 4344 |
d2++; |
| 4345 |
else if (cnt < d - 1) |
| 4346 |
cnt++; |
| 4347 |
else |
| 4348 |
break; |
| 4349 |
} |
| 4350 |
if (!gfc_ref_dimen_size (&ref->u.ar, d2 + d - 1, &result->value.integer, NULL)) |
| 4351 |
goto returnNull; |
| 4352 |
} |
| 4353 |
else |
| 4354 |
mpz_set_si (result->value.integer, (long int) 1); |
| 4355 |
} |
| 4356 |
|
| 4357 |
done: |
| 4358 |
return range_check (result, upper ? "UBOUND" : "LBOUND"); |
| 4359 |
|
| 4360 |
returnNull: |
| 4361 |
gfc_free_expr (result); |
| 4362 |
return NULL; |
| 4363 |
} |
| 4364 |
|
| 4365 |
|
| 4366 |
static gfc_expr * |
| 4367 |
simplify_bound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind, int upper) |
| 4368 |
{ |
| 4369 |
gfc_ref *ref; |
| 4370 |
gfc_array_spec *as; |
| 4371 |
ar_type type = AR_UNKNOWN; |
| 4372 |
int d; |
| 4373 |
|
| 4374 |
if (array->ts.type == BT_CLASS) |
| 4375 |
return NULL; |
| 4376 |
|
| 4377 |
if (array->expr_type != EXPR_VARIABLE) |
| 4378 |
{ |
| 4379 |
as = NULL; |
| 4380 |
ref = NULL; |
| 4381 |
goto done; |
| 4382 |
} |
| 4383 |
|
| 4384 |
/* Do not attempt to resolve if error has already been issued. */ |
| 4385 |
if (array->symtree->n.sym->error) |
| 4386 |
return NULL; |
| 4387 |
|
| 4388 |
/* Follow any component references. */ |
| 4389 |
as = array->symtree->n.sym->as; |
| 4390 |
for (ref = array->ref; ref; ref = ref->next) |
| 4391 |
{ |
| 4392 |
switch (ref->type) |
| 4393 |
{ |
| 4394 |
case REF_ARRAY: |
| 4395 |
type = ref->u.ar.type; |
| 4396 |
switch (ref->u.ar.type) |
| 4397 |
{ |
| 4398 |
case AR_ELEMENT: |
| 4399 |
as = NULL; |
| 4400 |
continue; |
| 4401 |
|
| 4402 |
case AR_FULL: |
| 4403 |
/* We're done because 'as' has already been set in the |
| 4404 |
previous iteration. */ |
| 4405 |
goto done; |
| 4406 |
|
| 4407 |
case AR_UNKNOWN: |
| 4408 |
return NULL; |
| 4409 |
|
| 4410 |
case AR_SECTION: |
| 4411 |
as = ref->u.ar.as; |
| 4412 |
goto done; |
| 4413 |
} |
| 4414 |
|
| 4415 |
gcc_unreachable (); |
| 4416 |
|
| 4417 |
case REF_COMPONENT: |
| 4418 |
as = ref->u.c.component->as; |
| 4419 |
continue; |
| 4420 |
|
| 4421 |
case REF_SUBSTRING: |
| 4422 |
case REF_INQUIRY: |
| 4423 |
continue; |
| 4424 |
} |
| 4425 |
} |
| 4426 |
|
| 4427 |
gcc_unreachable (); |
| 4428 |
|
| 4429 |
done: |
| 4430 |
|
| 4431 |
if (as && (as->type == AS_DEFERRED || as->type == AS_ASSUMED_RANK |
| 4432 |
|| (as->type == AS_ASSUMED_SHAPE && upper))) |
| 4433 |
return NULL; |
| 4434 |
|
| 4435 |
/* 'array' shall not be an unallocated allocatable variable or a pointer that |
| 4436 |
is not associated. */ |
| 4437 |
if (array->expr_type == EXPR_VARIABLE |
| 4438 |
&& (gfc_expr_attr (array).allocatable || gfc_expr_attr (array).pointer)) |
| 4439 |
return NULL; |
| 4440 |
|
| 4441 |
gcc_assert (!as |
| 4442 |
|| (as->type != AS_DEFERRED |
| 4443 |
&& array->expr_type == EXPR_VARIABLE |
| 4444 |
&& !gfc_expr_attr (array).allocatable |
| 4445 |
&& !gfc_expr_attr (array).pointer)); |
| 4446 |
|
| 4447 |
if (dim == NULL) |
| 4448 |
{ |
| 4449 |
/* Multi-dimensional bounds. */ |
| 4450 |
gfc_expr *bounds[GFC_MAX_DIMENSIONS]; |
| 4451 |
gfc_expr *e; |
| 4452 |
int k; |
| 4453 |
|
| 4454 |
/* UBOUND(ARRAY) is not valid for an assumed-size array. */ |
| 4455 |
if (upper && type == AR_FULL && as && as->type == AS_ASSUMED_SIZE) |
| 4456 |
{ |
| 4457 |
/* An error message will be emitted in |
| 4458 |
check_assumed_size_reference (resolve.cc). */ |
| 4459 |
return &gfc_bad_expr; |
| 4460 |
} |
| 4461 |
|
| 4462 |
/* Simplify the bounds for each dimension. */ |
| 4463 |
for (d = 0; d < array->rank; d++) |
| 4464 |
{ |
| 4465 |
bounds[d] = simplify_bound_dim (array, kind, d + 1, upper, as, ref, |
| 4466 |
false); |
| 4467 |
if (bounds[d] == NULL || bounds[d] == &gfc_bad_expr) |
| 4468 |
{ |
| 4469 |
int j; |
| 4470 |
|
| 4471 |
for (j = 0; j < d; j++) |
| 4472 |
gfc_free_expr (bounds[j]); |
| 4473 |
|
| 4474 |
if (gfc_seen_div0) |
| 4475 |
return &gfc_bad_expr; |
| 4476 |
else |
| 4477 |
return bounds[d]; |
| 4478 |
} |
| 4479 |
} |
| 4480 |
|
| 4481 |
/* Allocate the result expression. */ |
| 4482 |
k = get_kind (BT_INTEGER, kind, upper ? "UBOUND" : "LBOUND", |
| 4483 |
gfc_default_integer_kind); |
| 4484 |
if (k == -1) |
| 4485 |
return &gfc_bad_expr; |
| 4486 |
|
| 4487 |
e = gfc_get_array_expr (BT_INTEGER, k, &array->where); |
| 4488 |
|
| 4489 |
/* The result is a rank 1 array; its size is the rank of the first |
| 4490 |
argument to {L,U}BOUND. */ |
| 4491 |
e->rank = 1; |
| 4492 |
e->shape = gfc_get_shape (1); |
| 4493 |
mpz_init_set_ui (e->shape[0], array->rank); |
| 4494 |
|
| 4495 |
/* Create the constructor for this array. */ |
| 4496 |
for (d = 0; d < array->rank; d++) |
| 4497 |
gfc_constructor_append_expr (&e->value.constructor, |
| 4498 |
bounds[d], &e->where); |
| 4499 |
|
| 4500 |
return e; |
| 4501 |
} |
| 4502 |
else |
| 4503 |
{ |
| 4504 |
/* A DIM argument is specified. */ |
| 4505 |
if (dim->expr_type != EXPR_CONSTANT) |
| 4506 |
return NULL; |
| 4507 |
|
| 4508 |
d = mpz_get_si (dim->value.integer); |
| 4509 |
|
| 4510 |
if ((d < 1 || d > array->rank) |
| 4511 |
|| (d == array->rank && as && as->type == AS_ASSUMED_SIZE && upper)) |
| 4512 |
{ |
| 4513 |
gfc_error ("DIM argument at %L is out of bounds", &dim->where); |
| 4514 |
return &gfc_bad_expr; |
| 4515 |
} |
| 4516 |
|
| 4517 |
if (as && as->type == AS_ASSUMED_RANK) |
| 4518 |
return NULL; |
| 4519 |
|
| 4520 |
return simplify_bound_dim (array, kind, d, upper, as, ref, false); |
| 4521 |
} |
| 4522 |
} |
| 4523 |
|
| 4524 |
|
| 4525 |
static gfc_expr * |
| 4526 |
simplify_cobound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind, int upper) |
| 4527 |
{ |
| 4528 |
gfc_ref *ref; |
| 4529 |
gfc_array_spec *as; |
| 4530 |
int d; |
| 4531 |
|
| 4532 |
if (array->expr_type != EXPR_VARIABLE) |
| 4533 |
return NULL; |
| 4534 |
|
| 4535 |
/* Follow any component references. */ |
| 4536 |
as = (array->ts.type == BT_CLASS && CLASS_DATA (array)) |
| 4537 |
? CLASS_DATA (array)->as |
| 4538 |
: array->symtree->n.sym->as; |
| 4539 |
for (ref = array->ref; ref; ref = ref->next) |
| 4540 |
{ |
| 4541 |
switch (ref->type) |
| 4542 |
{ |
| 4543 |
case REF_ARRAY: |
| 4544 |
switch (ref->u.ar.type) |
| 4545 |
{ |
| 4546 |
case AR_ELEMENT: |
| 4547 |
if (ref->u.ar.as->corank > 0) |
| 4548 |
{ |
| 4549 |
gcc_assert (as == ref->u.ar.as); |
| 4550 |
goto done; |
| 4551 |
} |
| 4552 |
as = NULL; |
| 4553 |
continue; |
| 4554 |
|
| 4555 |
case AR_FULL: |
| 4556 |
/* We're done because 'as' has already been set in the |
| 4557 |
previous iteration. */ |
| 4558 |
goto done; |
| 4559 |
|
| 4560 |
case AR_UNKNOWN: |
| 4561 |
return NULL; |
| 4562 |
|
| 4563 |
case AR_SECTION: |
| 4564 |
as = ref->u.ar.as; |
| 4565 |
goto done; |
| 4566 |
} |
| 4567 |
|
| 4568 |
gcc_unreachable (); |
| 4569 |
|
| 4570 |
case REF_COMPONENT: |
| 4571 |
as = ref->u.c.component->as; |
| 4572 |
continue; |
| 4573 |
|
| 4574 |
case REF_SUBSTRING: |
| 4575 |
case REF_INQUIRY: |
| 4576 |
continue; |
| 4577 |
} |
| 4578 |
} |
| 4579 |
|
| 4580 |
if (!as) |
| 4581 |
gcc_unreachable (); |
| 4582 |
|
| 4583 |
done: |
| 4584 |
|
| 4585 |
if (as->cotype == AS_DEFERRED || as->cotype == AS_ASSUMED_SHAPE) |
| 4586 |
return NULL; |
| 4587 |
|
| 4588 |
if (dim == NULL) |
| 4589 |
{ |
| 4590 |
/* Multi-dimensional cobounds. */ |
| 4591 |
gfc_expr *bounds[GFC_MAX_DIMENSIONS]; |
| 4592 |
gfc_expr *e; |
| 4593 |
int k; |
| 4594 |
|
| 4595 |
/* Simplify the cobounds for each dimension. */ |
| 4596 |
for (d = 0; d < as->corank; d++) |
| 4597 |
{ |
| 4598 |
bounds[d] = simplify_bound_dim (array, kind, d + 1 + as->rank, |
| 4599 |
upper, as, ref, true); |
| 4600 |
if (bounds[d] == NULL || bounds[d] == &gfc_bad_expr) |
| 4601 |
{ |
| 4602 |
int j; |
| 4603 |
|
| 4604 |
for (j = 0; j < d; j++) |
| 4605 |
gfc_free_expr (bounds[j]); |
| 4606 |
return bounds[d]; |
| 4607 |
} |
| 4608 |
} |
| 4609 |
|
| 4610 |
/* Allocate the result expression. */ |
| 4611 |
e = gfc_get_expr (); |
| 4612 |
e->where = array->where; |
| 4613 |
e->expr_type = EXPR_ARRAY; |
| 4614 |
e->ts.type = BT_INTEGER; |
| 4615 |
k = get_kind (BT_INTEGER, kind, upper ? "UCOBOUND" : "LCOBOUND", |
| 4616 |
gfc_default_integer_kind); |
| 4617 |
if (k == -1) |
| 4618 |
{ |
| 4619 |
gfc_free_expr (e); |
| 4620 |
return &gfc_bad_expr; |
| 4621 |
} |
| 4622 |
e->ts.kind = k; |
| 4623 |
|
| 4624 |
/* The result is a rank 1 array; its size is the rank of the first |
| 4625 |
argument to {L,U}COBOUND. */ |
| 4626 |
e->rank = 1; |
| 4627 |
e->shape = gfc_get_shape (1); |
| 4628 |
mpz_init_set_ui (e->shape[0], as->corank); |
| 4629 |
|
| 4630 |
/* Create the constructor for this array. */ |
| 4631 |
for (d = 0; d < as->corank; d++) |
| 4632 |
gfc_constructor_append_expr (&e->value.constructor, |
| 4633 |
bounds[d], &e->where); |
| 4634 |
return e; |
| 4635 |
} |
| 4636 |
else |
| 4637 |
{ |
| 4638 |
/* A DIM argument is specified. */ |
| 4639 |
if (dim->expr_type != EXPR_CONSTANT) |
| 4640 |
return NULL; |
| 4641 |
|
| 4642 |
d = mpz_get_si (dim->value.integer); |
| 4643 |
|
| 4644 |
if (d < 1 || d > as->corank) |
| 4645 |
{ |
| 4646 |
gfc_error ("DIM argument at %L is out of bounds", &dim->where); |
| 4647 |
return &gfc_bad_expr; |
| 4648 |
} |
| 4649 |
|
| 4650 |
return simplify_bound_dim (array, kind, d+as->rank, upper, as, ref, true); |
| 4651 |
} |
| 4652 |
} |
| 4653 |
|
| 4654 |
|
| 4655 |
gfc_expr * |
| 4656 |
gfc_simplify_lbound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind) |
| 4657 |
{ |
| 4658 |
return simplify_bound (array, dim, kind, 0); |
| 4659 |
} |
| 4660 |
|
| 4661 |
|
| 4662 |
gfc_expr * |
| 4663 |
gfc_simplify_lcobound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind) |
| 4664 |
{ |
| 4665 |
return simplify_cobound (array, dim, kind, 0); |
| 4666 |
} |
| 4667 |
|
| 4668 |
gfc_expr * |
| 4669 |
gfc_simplify_leadz (gfc_expr *e) |
| 4670 |
{ |
| 4671 |
unsigned long lz, bs; |
| 4672 |
int i; |
| 4673 |
|
| 4674 |
if (e->expr_type != EXPR_CONSTANT) |
| 4675 |
return NULL; |
| 4676 |
|
| 4677 |
i = gfc_validate_kind (e->ts.type, e->ts.kind, false); |
| 4678 |
bs = gfc_integer_kinds[i].bit_size; |
| 4679 |
if (mpz_cmp_si (e->value.integer, 0) == 0) |
| 4680 |
lz = bs; |
| 4681 |
else if (mpz_cmp_si (e->value.integer, 0) < 0) |
| 4682 |
lz = 0; |
| 4683 |
else |
| 4684 |
lz = bs - mpz_sizeinbase (e->value.integer, 2); |
| 4685 |
|
| 4686 |
return gfc_get_int_expr (gfc_default_integer_kind, &e->where, lz); |
| 4687 |
} |
| 4688 |
|
| 4689 |
|
| 4690 |
/* Check for constant length of a substring. */ |
| 4691 |
|
| 4692 |
static bool |
| 4693 |
substring_has_constant_len (gfc_expr *e) |
| 4694 |
{ |
| 4695 |
gfc_ref *ref; |
| 4696 |
HOST_WIDE_INT istart, iend, length; |
| 4697 |
bool equal_length = false; |
| 4698 |
|
| 4699 |
if (e->ts.type != BT_CHARACTER) |
| 4700 |
return false; |
| 4701 |
|
| 4702 |
for (ref = e->ref; ref; ref = ref->next) |
| 4703 |
if (ref->type != REF_COMPONENT && ref->type != REF_ARRAY) |
| 4704 |
break; |
| 4705 |
|
| 4706 |
if (!ref |
| 4707 |
|| ref->type != REF_SUBSTRING |
| 4708 |
|| !ref->u.ss.start |
| 4709 |
|| ref->u.ss.start->expr_type != EXPR_CONSTANT |
| 4710 |
|| !ref->u.ss.end |
| 4711 |
|| ref->u.ss.end->expr_type != EXPR_CONSTANT) |
| 4712 |
return false; |
| 4713 |
|
| 4714 |
/* Basic checks on substring starting and ending indices. */ |
| 4715 |
if (!gfc_resolve_substring (ref, &equal_length)) |
| 4716 |
return false; |
| 4717 |
|
| 4718 |
istart = gfc_mpz_get_hwi (ref->u.ss.start->value.integer); |
| 4719 |
iend = gfc_mpz_get_hwi (ref->u.ss.end->value.integer); |
| 4720 |
|
| 4721 |
if (istart <= iend) |
| 4722 |
length = iend - istart + 1; |
| 4723 |
else |
| 4724 |
length = 0; |
| 4725 |
|
| 4726 |
/* Fix substring length. */ |
| 4727 |
e->value.character.length = length; |
| 4728 |
|
| 4729 |
return true; |
| 4730 |
} |
| 4731 |
|
| 4732 |
|
| 4733 |
gfc_expr * |
| 4734 |
gfc_simplify_len (gfc_expr *e, gfc_expr *kind) |
| 4735 |
{ |
| 4736 |
gfc_expr *result; |
| 4737 |
int k = get_kind (BT_INTEGER, kind, "LEN", gfc_default_integer_kind); |
| 4738 |
|
| 4739 |
if (k == -1) |
| 4740 |
return &gfc_bad_expr; |
| 4741 |
|
| 4742 |
if (e->expr_type == EXPR_CONSTANT |
| 4743 |
|| substring_has_constant_len (e)) |
| 4744 |
{ |
| 4745 |
result = gfc_get_constant_expr (BT_INTEGER, k, &e->where); |
| 4746 |
mpz_set_si (result->value.integer, e->value.character.length); |
| 4747 |
return range_check (result, "LEN"); |
| 4748 |
} |
| 4749 |
else if (e->ts.u.cl != NULL && e->ts.u.cl->length != NULL |
| 4750 |
&& e->ts.u.cl->length->expr_type == EXPR_CONSTANT |
| 4751 |
&& e->ts.u.cl->length->ts.type == BT_INTEGER) |
| 4752 |
{ |
| 4753 |
result = gfc_get_constant_expr (BT_INTEGER, k, &e->where); |
| 4754 |
mpz_set (result->value.integer, e->ts.u.cl->length->value.integer); |
| 4755 |
return range_check (result, "LEN"); |
| 4756 |
} |
| 4757 |
else if (e->expr_type == EXPR_VARIABLE && e->ts.type == BT_CHARACTER |
| 4758 |
&& e->symtree->n.sym) |
| 4759 |
{ |
| 4760 |
if (e->symtree->n.sym->ts.type != BT_DERIVED |
| 4761 |
&& e->symtree->n.sym->assoc && e->symtree->n.sym->assoc->target |
| 4762 |
&& e->symtree->n.sym->assoc->target->ts.type == BT_DERIVED |
| 4763 |
&& e->symtree->n.sym->assoc->target->symtree->n.sym |
| 4764 |
&& UNLIMITED_POLY (e->symtree->n.sym->assoc->target->symtree->n.sym)) |
| 4765 |
/* The expression in assoc->target points to a ref to the _data |
| 4766 |
component of the unlimited polymorphic entity. To get the _len |
| 4767 |
component the last _data ref needs to be stripped and a ref to the |
| 4768 |
_len component added. */ |
| 4769 |
return gfc_get_len_component (e->symtree->n.sym->assoc->target, k); |
| 4770 |
else if (e->symtree->n.sym->ts.type == BT_DERIVED |
| 4771 |
&& e->ref && e->ref->type == REF_COMPONENT |
| 4772 |
&& e->ref->u.c.component->attr.pdt_string |
| 4773 |
&& e->ref->u.c.component->ts.type == BT_CHARACTER |
| 4774 |
&& e->ref->u.c.component->ts.u.cl->length) |
| 4775 |
{ |
| 4776 |
if (gfc_init_expr_flag) |
| 4777 |
{ |
| 4778 |
gfc_expr* tmp; |
| 4779 |
tmp = gfc_pdt_find_component_copy_initializer (e->symtree->n.sym, |
| 4780 |
e->ref->u.c |
| 4781 |
.component->ts.u.cl |
| 4782 |
->length->symtree |
| 4783 |
->name); |
| 4784 |
if (tmp) |
| 4785 |
return tmp; |
| 4786 |
} |
| 4787 |
else |
| 4788 |
{ |
| 4789 |
gfc_expr *len_expr = gfc_copy_expr (e); |
| 4790 |
gfc_free_ref_list (len_expr->ref); |
| 4791 |
len_expr->ref = NULL; |
| 4792 |
gfc_find_component (len_expr->symtree->n.sym->ts.u.derived, e->ref |
| 4793 |
->u.c.component->ts.u.cl->length->symtree |
| 4794 |
->name, |
| 4795 |
false, true, &len_expr->ref); |
| 4796 |
len_expr->ts = len_expr->ref->u.c.component->ts; |
| 4797 |
return len_expr; |
| 4798 |
} |
| 4799 |
} |
| 4800 |
} |
| 4801 |
return NULL; |
| 4802 |
} |
| 4803 |
|
| 4804 |
|
| 4805 |
gfc_expr * |
| 4806 |
gfc_simplify_len_trim (gfc_expr *e, gfc_expr *kind) |
| 4807 |
{ |
| 4808 |
gfc_expr *result; |
| 4809 |
size_t count, len, i; |
| 4810 |
int k = get_kind (BT_INTEGER, kind, "LEN_TRIM", gfc_default_integer_kind); |
| 4811 |
|
| 4812 |
if (k == -1) |
| 4813 |
return &gfc_bad_expr; |
| 4814 |
|
| 4815 |
/* If the expression is either an array element or section, an array |
| 4816 |
parameter must be built so that the reference can be applied. Constant |
| 4817 |
references should have already been simplified away. All other cases |
| 4818 |
can proceed to translation, where kind conversion will occur silently. */ |
| 4819 |
if (e->expr_type == EXPR_VARIABLE |
| 4820 |
&& e->ts.type == BT_CHARACTER |
| 4821 |
&& e->symtree->n.sym->attr.flavor == FL_PARAMETER |
| 4822 |
&& e->ref && e->ref->type == REF_ARRAY |
| 4823 |
&& e->ref->u.ar.type != AR_FULL |
| 4824 |
&& e->symtree->n.sym->value) |
| 4825 |
{ |
| 4826 |
char name[2*GFC_MAX_SYMBOL_LEN + 12]; |
| 4827 |
gfc_namespace *ns = e->symtree->n.sym->ns; |
| 4828 |
gfc_symtree *st; |
| 4829 |
gfc_expr *expr; |
| 4830 |
gfc_expr *p; |
| 4831 |
gfc_constructor *c; |
| 4832 |
int cnt = 0; |
| 4833 |
|
| 4834 |
sprintf (name, "_len_trim_%s_%s", e->symtree->n.sym->name, |
| 4835 |
ns->proc_name->name); |
| 4836 |
st = gfc_find_symtree (ns->sym_root, name); |
| 4837 |
if (st) |
| 4838 |
goto already_built; |
| 4839 |
|
| 4840 |
/* Recursively call this fcn to simplify the constructor elements. */ |
| 4841 |
expr = gfc_copy_expr (e->symtree->n.sym->value); |
| 4842 |
expr->ts.type = BT_INTEGER; |
| 4843 |
expr->ts.kind = k; |
| 4844 |
expr->ts.u.cl = NULL; |
| 4845 |
c = gfc_constructor_first (expr->value.constructor); |
| 4846 |
for (; c; c = gfc_constructor_next (c)) |
| 4847 |
{ |
| 4848 |
if (c->iterator) |
| 4849 |
continue; |
| 4850 |
|
| 4851 |
if (c->expr && c->expr->ts.type == BT_CHARACTER) |
| 4852 |
{ |
| 4853 |
p = gfc_simplify_len_trim (c->expr, kind); |
| 4854 |
if (p == NULL) |
| 4855 |
goto clean_up; |
| 4856 |
gfc_replace_expr (c->expr, p); |
| 4857 |
cnt++; |
| 4858 |
} |
| 4859 |
} |
| 4860 |
|
| 4861 |
if (cnt) |
| 4862 |
{ |
| 4863 |
/* Build a new parameter to take the result. */ |
| 4864 |
st = gfc_new_symtree (&ns->sym_root, name); |
| 4865 |
st->n.sym = gfc_new_symbol (st->name, ns); |
| 4866 |
st->n.sym->value = expr; |
| 4867 |
st->n.sym->ts = expr->ts; |
| 4868 |
st->n.sym->attr.dimension = 1; |
| 4869 |
st->n.sym->attr.save = SAVE_IMPLICIT; |
| 4870 |
st->n.sym->attr.flavor = FL_PARAMETER; |
| 4871 |
st->n.sym->as = gfc_copy_array_spec (e->symtree->n.sym->as); |
| 4872 |
gfc_set_sym_referenced (st->n.sym); |
| 4873 |
st->n.sym->refs++; |
| 4874 |
gfc_commit_symbol (st->n.sym); |
| 4875 |
|
| 4876 |
already_built: |
| 4877 |
/* Build a return expression. */ |
| 4878 |
expr = gfc_copy_expr (e); |
| 4879 |
expr->ts = st->n.sym->ts; |
| 4880 |
expr->symtree = st; |
| 4881 |
gfc_expression_rank (expr); |
| 4882 |
return expr; |
| 4883 |
} |
| 4884 |
|
| 4885 |
clean_up: |
| 4886 |
gfc_free_expr (expr); |
| 4887 |
return NULL; |
| 4888 |
} |
| 4889 |
|
| 4890 |
if (e->expr_type != EXPR_CONSTANT) |
| 4891 |
return NULL; |
| 4892 |
|
| 4893 |
len = e->value.character.length; |
| 4894 |
for (count = 0, i = 1; i <= len; i++) |
| 4895 |
if (e->value.character.string[len - i] == ' ') |
| 4896 |
count++; |
| 4897 |
else |
| 4898 |
break; |
| 4899 |
|
| 4900 |
result = gfc_get_int_expr (k, &e->where, len - count); |
| 4901 |
return range_check (result, "LEN_TRIM"); |
| 4902 |
} |
| 4903 |
|
| 4904 |
gfc_expr * |
| 4905 |
gfc_simplify_lgamma (gfc_expr *x) |
| 4906 |
{ |
| 4907 |
gfc_expr *result; |
| 4908 |
int sg; |
| 4909 |
|
| 4910 |
if (x->expr_type != EXPR_CONSTANT) |
| 4911 |
return NULL; |
| 4912 |
|
| 4913 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 4914 |
mpfr_lgamma (result->value.real, &sg, x->value.real, GFC_RND_MODE); |
| 4915 |
|
| 4916 |
return range_check (result, "LGAMMA"); |
| 4917 |
} |
| 4918 |
|
| 4919 |
|
| 4920 |
gfc_expr * |
| 4921 |
gfc_simplify_lge (gfc_expr *a, gfc_expr *b) |
| 4922 |
{ |
| 4923 |
if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT) |
| 4924 |
return NULL; |
| 4925 |
|
| 4926 |
return gfc_get_logical_expr (gfc_default_logical_kind, &a->where, |
| 4927 |
gfc_compare_string (a, b) >= 0); |
| 4928 |
} |
| 4929 |
|
| 4930 |
|
| 4931 |
gfc_expr * |
| 4932 |
gfc_simplify_lgt (gfc_expr *a, gfc_expr *b) |
| 4933 |
{ |
| 4934 |
if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT) |
| 4935 |
return NULL; |
| 4936 |
|
| 4937 |
return gfc_get_logical_expr (gfc_default_logical_kind, &a->where, |
| 4938 |
gfc_compare_string (a, b) > 0); |
| 4939 |
} |
| 4940 |
|
| 4941 |
|
| 4942 |
gfc_expr * |
| 4943 |
gfc_simplify_lle (gfc_expr *a, gfc_expr *b) |
| 4944 |
{ |
| 4945 |
if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT) |
| 4946 |
return NULL; |
| 4947 |
|
| 4948 |
return gfc_get_logical_expr (gfc_default_logical_kind, &a->where, |
| 4949 |
gfc_compare_string (a, b) <= 0); |
| 4950 |
} |
| 4951 |
|
| 4952 |
|
| 4953 |
gfc_expr * |
| 4954 |
gfc_simplify_llt (gfc_expr *a, gfc_expr *b) |
| 4955 |
{ |
| 4956 |
if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT) |
| 4957 |
return NULL; |
| 4958 |
|
| 4959 |
return gfc_get_logical_expr (gfc_default_logical_kind, &a->where, |
| 4960 |
gfc_compare_string (a, b) < 0); |
| 4961 |
} |
| 4962 |
|
| 4963 |
|
| 4964 |
gfc_expr * |
| 4965 |
gfc_simplify_log (gfc_expr *x) |
| 4966 |
{ |
| 4967 |
gfc_expr *result; |
| 4968 |
|
| 4969 |
if (x->expr_type != EXPR_CONSTANT) |
| 4970 |
return NULL; |
| 4971 |
|
| 4972 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 4973 |
|
| 4974 |
switch (x->ts.type) |
| 4975 |
{ |
| 4976 |
case BT_REAL: |
| 4977 |
if (mpfr_sgn (x->value.real) <= 0) |
| 4978 |
{ |
| 4979 |
gfc_error ("Argument of LOG at %L cannot be less than or equal " |
| 4980 |
"to zero", &x->where); |
| 4981 |
gfc_free_expr (result); |
| 4982 |
return &gfc_bad_expr; |
| 4983 |
} |
| 4984 |
|
| 4985 |
mpfr_log (result->value.real, x->value.real, GFC_RND_MODE); |
| 4986 |
break; |
| 4987 |
|
| 4988 |
case BT_COMPLEX: |
| 4989 |
if (mpfr_zero_p (mpc_realref (x->value.complex)) |
| 4990 |
&& mpfr_zero_p (mpc_imagref (x->value.complex))) |
| 4991 |
{ |
| 4992 |
gfc_error ("Complex argument of LOG at %L cannot be zero", |
| 4993 |
&x->where); |
| 4994 |
gfc_free_expr (result); |
| 4995 |
return &gfc_bad_expr; |
| 4996 |
} |
| 4997 |
|
| 4998 |
gfc_set_model_kind (x->ts.kind); |
| 4999 |
mpc_log (result->value.complex, x->value.complex, GFC_MPC_RND_MODE); |
| 5000 |
break; |
| 5001 |
|
| 5002 |
default: |
| 5003 |
gfc_internal_error ("gfc_simplify_log: bad type"); |
| 5004 |
} |
| 5005 |
|
| 5006 |
return range_check (result, "LOG"); |
| 5007 |
} |
| 5008 |
|
| 5009 |
|
| 5010 |
gfc_expr * |
| 5011 |
gfc_simplify_log10 (gfc_expr *x) |
| 5012 |
{ |
| 5013 |
gfc_expr *result; |
| 5014 |
|
| 5015 |
if (x->expr_type != EXPR_CONSTANT) |
| 5016 |
return NULL; |
| 5017 |
|
| 5018 |
if (mpfr_sgn (x->value.real) <= 0) |
| 5019 |
{ |
| 5020 |
gfc_error ("Argument of LOG10 at %L cannot be less than or equal " |
| 5021 |
"to zero", &x->where); |
| 5022 |
return &gfc_bad_expr; |
| 5023 |
} |
| 5024 |
|
| 5025 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 5026 |
mpfr_log10 (result->value.real, x->value.real, GFC_RND_MODE); |
| 5027 |
|
| 5028 |
return range_check (result, "LOG10"); |
| 5029 |
} |
| 5030 |
|
| 5031 |
|
| 5032 |
gfc_expr * |
| 5033 |
gfc_simplify_logical (gfc_expr *e, gfc_expr *k) |
| 5034 |
{ |
| 5035 |
int kind; |
| 5036 |
|
| 5037 |
kind = get_kind (BT_LOGICAL, k, "LOGICAL", gfc_default_logical_kind); |
| 5038 |
if (kind < 0) |
| 5039 |
return &gfc_bad_expr; |
| 5040 |
|
| 5041 |
if (e->expr_type != EXPR_CONSTANT) |
| 5042 |
return NULL; |
| 5043 |
|
| 5044 |
return gfc_get_logical_expr (kind, &e->where, e->value.logical); |
| 5045 |
} |
| 5046 |
|
| 5047 |
|
| 5048 |
gfc_expr* |
| 5049 |
gfc_simplify_matmul (gfc_expr *matrix_a, gfc_expr *matrix_b) |
| 5050 |
{ |
| 5051 |
gfc_expr *result; |
| 5052 |
int row, result_rows, col, result_columns; |
| 5053 |
int stride_a, offset_a, stride_b, offset_b; |
| 5054 |
|
| 5055 |
if (!is_constant_array_expr (matrix_a) |
| 5056 |
|| !is_constant_array_expr (matrix_b)) |
| 5057 |
return NULL; |
| 5058 |
|
| 5059 |
/* MATMUL should do mixed-mode arithmetic. Set the result type. */ |
| 5060 |
if (matrix_a->ts.type != matrix_b->ts.type) |
| 5061 |
{ |
| 5062 |
gfc_expr e; |
| 5063 |
e.expr_type = EXPR_OP; |
| 5064 |
gfc_clear_ts (&e.ts); |
| 5065 |
e.value.op.op = INTRINSIC_NONE; |
| 5066 |
e.value.op.op1 = matrix_a; |
| 5067 |
e.value.op.op2 = matrix_b; |
| 5068 |
gfc_type_convert_binary (&e, 1); |
| 5069 |
result = gfc_get_array_expr (e.ts.type, e.ts.kind, &matrix_a->where); |
| 5070 |
} |
| 5071 |
else |
| 5072 |
{ |
| 5073 |
result = gfc_get_array_expr (matrix_a->ts.type, matrix_a->ts.kind, |
| 5074 |
&matrix_a->where); |
| 5075 |
} |
| 5076 |
|
| 5077 |
if (matrix_a->rank == 1 && matrix_b->rank == 2) |
| 5078 |
{ |
| 5079 |
result_rows = 1; |
| 5080 |
result_columns = mpz_get_si (matrix_b->shape[1]); |
| 5081 |
stride_a = 1; |
| 5082 |
stride_b = mpz_get_si (matrix_b->shape[0]); |
| 5083 |
|
| 5084 |
result->rank = 1; |
| 5085 |
result->shape = gfc_get_shape (result->rank); |
| 5086 |
mpz_init_set_si (result->shape[0], result_columns); |
| 5087 |
} |
| 5088 |
else if (matrix_a->rank == 2 && matrix_b->rank == 1) |
| 5089 |
{ |
| 5090 |
result_rows = mpz_get_si (matrix_a->shape[0]); |
| 5091 |
result_columns = 1; |
| 5092 |
stride_a = mpz_get_si (matrix_a->shape[0]); |
| 5093 |
stride_b = 1; |
| 5094 |
|
| 5095 |
result->rank = 1; |
| 5096 |
result->shape = gfc_get_shape (result->rank); |
| 5097 |
mpz_init_set_si (result->shape[0], result_rows); |
| 5098 |
} |
| 5099 |
else if (matrix_a->rank == 2 && matrix_b->rank == 2) |
| 5100 |
{ |
| 5101 |
result_rows = mpz_get_si (matrix_a->shape[0]); |
| 5102 |
result_columns = mpz_get_si (matrix_b->shape[1]); |
| 5103 |
stride_a = mpz_get_si (matrix_a->shape[0]); |
| 5104 |
stride_b = mpz_get_si (matrix_b->shape[0]); |
| 5105 |
|
| 5106 |
result->rank = 2; |
| 5107 |
result->shape = gfc_get_shape (result->rank); |
| 5108 |
mpz_init_set_si (result->shape[0], result_rows); |
| 5109 |
mpz_init_set_si (result->shape[1], result_columns); |
| 5110 |
} |
| 5111 |
else |
| 5112 |
gcc_unreachable(); |
| 5113 |
|
| 5114 |
offset_b = 0; |
| 5115 |
for (col = 0; col < result_columns; ++col) |
| 5116 |
{ |
| 5117 |
offset_a = 0; |
| 5118 |
|
| 5119 |
for (row = 0; row < result_rows; ++row) |
| 5120 |
{ |
| 5121 |
gfc_expr *e = compute_dot_product (matrix_a, stride_a, offset_a, |
| 5122 |
matrix_b, 1, offset_b, false); |
| 5123 |
gfc_constructor_append_expr (&result->value.constructor, |
| 5124 |
e, NULL); |
| 5125 |
|
| 5126 |
offset_a += 1; |
| 5127 |
} |
| 5128 |
|
| 5129 |
offset_b += stride_b; |
| 5130 |
} |
| 5131 |
|
| 5132 |
return result; |
| 5133 |
} |
| 5134 |
|
| 5135 |
|
| 5136 |
gfc_expr * |
| 5137 |
gfc_simplify_maskr (gfc_expr *i, gfc_expr *kind_arg) |
| 5138 |
{ |
| 5139 |
gfc_expr *result; |
| 5140 |
int kind, arg, k; |
| 5141 |
|
| 5142 |
if (i->expr_type != EXPR_CONSTANT) |
| 5143 |
return NULL; |
| 5144 |
|
| 5145 |
kind = get_kind (BT_INTEGER, kind_arg, "MASKR", gfc_default_integer_kind); |
| 5146 |
if (kind == -1) |
| 5147 |
return &gfc_bad_expr; |
| 5148 |
k = gfc_validate_kind (BT_INTEGER, kind, false); |
| 5149 |
|
| 5150 |
bool fail = gfc_extract_int (i, &arg); |
| 5151 |
gcc_assert (!fail); |
| 5152 |
|
| 5153 |
if (!gfc_check_mask (i, kind_arg)) |
| 5154 |
return &gfc_bad_expr; |
| 5155 |
|
| 5156 |
result = gfc_get_constant_expr (BT_INTEGER, kind, &i->where); |
| 5157 |
|
| 5158 |
/* MASKR(n) = 2^n - 1 */ |
| 5159 |
mpz_set_ui (result->value.integer, 1); |
| 5160 |
mpz_mul_2exp (result->value.integer, result->value.integer, arg); |
| 5161 |
mpz_sub_ui (result->value.integer, result->value.integer, 1); |
| 5162 |
|
| 5163 |
gfc_convert_mpz_to_signed (result->value.integer, gfc_integer_kinds[k].bit_size); |
| 5164 |
|
| 5165 |
return result; |
| 5166 |
} |
| 5167 |
|
| 5168 |
|
| 5169 |
gfc_expr * |
| 5170 |
gfc_simplify_maskl (gfc_expr *i, gfc_expr *kind_arg) |
| 5171 |
{ |
| 5172 |
gfc_expr *result; |
| 5173 |
int kind, arg, k; |
| 5174 |
mpz_t z; |
| 5175 |
|
| 5176 |
if (i->expr_type != EXPR_CONSTANT) |
| 5177 |
return NULL; |
| 5178 |
|
| 5179 |
kind = get_kind (BT_INTEGER, kind_arg, "MASKL", gfc_default_integer_kind); |
| 5180 |
if (kind == -1) |
| 5181 |
return &gfc_bad_expr; |
| 5182 |
k = gfc_validate_kind (BT_INTEGER, kind, false); |
| 5183 |
|
| 5184 |
bool fail = gfc_extract_int (i, &arg); |
| 5185 |
gcc_assert (!fail); |
| 5186 |
|
| 5187 |
if (!gfc_check_mask (i, kind_arg)) |
| 5188 |
return &gfc_bad_expr; |
| 5189 |
|
| 5190 |
result = gfc_get_constant_expr (BT_INTEGER, kind, &i->where); |
| 5191 |
|
| 5192 |
/* MASKL(n) = 2^bit_size - 2^(bit_size - n) */ |
| 5193 |
mpz_init_set_ui (z, 1); |
| 5194 |
mpz_mul_2exp (z, z, gfc_integer_kinds[k].bit_size); |
| 5195 |
mpz_set_ui (result->value.integer, 1); |
| 5196 |
mpz_mul_2exp (result->value.integer, result->value.integer, |
| 5197 |
gfc_integer_kinds[k].bit_size - arg); |
| 5198 |
mpz_sub (result->value.integer, z, result->value.integer); |
| 5199 |
mpz_clear (z); |
| 5200 |
|
| 5201 |
gfc_convert_mpz_to_signed (result->value.integer, gfc_integer_kinds[k].bit_size); |
| 5202 |
|
| 5203 |
return result; |
| 5204 |
} |
| 5205 |
|
| 5206 |
/* Similar to gfc_simplify_maskr, but code paths are different enough to make |
| 5207 |
this into a separate function. */ |
| 5208 |
|
| 5209 |
gfc_expr * |
| 5210 |
gfc_simplify_umaskr (gfc_expr *i, gfc_expr *kind_arg) |
| 5211 |
{ |
| 5212 |
gfc_expr *result; |
| 5213 |
int kind, arg, k; |
| 5214 |
|
| 5215 |
if (i->expr_type != EXPR_CONSTANT) |
| 5216 |
return NULL; |
| 5217 |
|
| 5218 |
kind = get_kind (BT_UNSIGNED, kind_arg, "UMASKR", gfc_default_unsigned_kind); |
| 5219 |
if (kind == -1) |
| 5220 |
return &gfc_bad_expr; |
| 5221 |
k = gfc_validate_kind (BT_UNSIGNED, kind, false); |
| 5222 |
|
| 5223 |
bool fail = gfc_extract_int (i, &arg); |
| 5224 |
gcc_assert (!fail); |
| 5225 |
|
| 5226 |
if (!gfc_check_mask (i, kind_arg)) |
| 5227 |
return &gfc_bad_expr; |
| 5228 |
|
| 5229 |
result = gfc_get_constant_expr (BT_UNSIGNED, kind, &i->where); |
| 5230 |
|
| 5231 |
/* MASKR(n) = 2^n - 1 */ |
| 5232 |
mpz_set_ui (result->value.integer, 1); |
| 5233 |
mpz_mul_2exp (result->value.integer, result->value.integer, arg); |
| 5234 |
mpz_sub_ui (result->value.integer, result->value.integer, 1); |
| 5235 |
|
| 5236 |
gfc_convert_mpz_to_unsigned (result->value.integer, |
| 5237 |
gfc_unsigned_kinds[k].bit_size, |
| 5238 |
false); |
| 5239 |
|
| 5240 |
return result; |
| 5241 |
} |
| 5242 |
|
| 5243 |
/* Likewise, similar to gfc_simplify_maskl. */ |
| 5244 |
|
| 5245 |
gfc_expr * |
| 5246 |
gfc_simplify_umaskl (gfc_expr *i, gfc_expr *kind_arg) |
| 5247 |
{ |
| 5248 |
gfc_expr *result; |
| 5249 |
int kind, arg, k; |
| 5250 |
mpz_t z; |
| 5251 |
|
| 5252 |
if (i->expr_type != EXPR_CONSTANT) |
| 5253 |
return NULL; |
| 5254 |
|
| 5255 |
kind = get_kind (BT_UNSIGNED, kind_arg, "UMASKL", gfc_default_integer_kind); |
| 5256 |
if (kind == -1) |
| 5257 |
return &gfc_bad_expr; |
| 5258 |
k = gfc_validate_kind (BT_UNSIGNED, kind, false); |
| 5259 |
|
| 5260 |
bool fail = gfc_extract_int (i, &arg); |
| 5261 |
gcc_assert (!fail); |
| 5262 |
|
| 5263 |
if (!gfc_check_mask (i, kind_arg)) |
| 5264 |
return &gfc_bad_expr; |
| 5265 |
|
| 5266 |
result = gfc_get_constant_expr (BT_UNSIGNED, kind, &i->where); |
| 5267 |
|
| 5268 |
/* MASKL(n) = 2^bit_size - 2^(bit_size - n) */ |
| 5269 |
mpz_init_set_ui (z, 1); |
| 5270 |
mpz_mul_2exp (z, z, gfc_unsigned_kinds[k].bit_size); |
| 5271 |
mpz_set_ui (result->value.integer, 1); |
| 5272 |
mpz_mul_2exp (result->value.integer, result->value.integer, |
| 5273 |
gfc_integer_kinds[k].bit_size - arg); |
| 5274 |
mpz_sub (result->value.integer, z, result->value.integer); |
| 5275 |
mpz_clear (z); |
| 5276 |
|
| 5277 |
gfc_convert_mpz_to_unsigned (result->value.integer, |
| 5278 |
gfc_unsigned_kinds[k].bit_size, |
| 5279 |
false); |
| 5280 |
|
| 5281 |
return result; |
| 5282 |
} |
| 5283 |
|
| 5284 |
|
| 5285 |
gfc_expr * |
| 5286 |
gfc_simplify_merge (gfc_expr *tsource, gfc_expr *fsource, gfc_expr *mask) |
| 5287 |
{ |
| 5288 |
gfc_expr * result; |
| 5289 |
gfc_constructor *tsource_ctor, *fsource_ctor, *mask_ctor; |
| 5290 |
|
| 5291 |
if (mask->expr_type == EXPR_CONSTANT) |
| 5292 |
{ |
| 5293 |
/* The standard requires evaluation of all function arguments. |
| 5294 |
Simplify only when the other dropped argument (FSOURCE or TSOURCE) |
| 5295 |
is a constant expression. */ |
| 5296 |
if (mask->value.logical) |
| 5297 |
{ |
| 5298 |
if (!gfc_is_constant_expr (fsource)) |
| 5299 |
return NULL; |
| 5300 |
result = gfc_copy_expr (tsource); |
| 5301 |
} |
| 5302 |
else |
| 5303 |
{ |
| 5304 |
if (!gfc_is_constant_expr (tsource)) |
| 5305 |
return NULL; |
| 5306 |
result = gfc_copy_expr (fsource); |
| 5307 |
} |
| 5308 |
|
| 5309 |
/* Parenthesis is needed to get lower bounds of 1. */ |
| 5310 |
result = gfc_get_parentheses (result); |
| 5311 |
gfc_simplify_expr (result, 1); |
| 5312 |
return result; |
| 5313 |
} |
| 5314 |
|
| 5315 |
if (!mask->rank || !is_constant_array_expr (mask) |
| 5316 |
|| !is_constant_array_expr (tsource) || !is_constant_array_expr (fsource)) |
| 5317 |
return NULL; |
| 5318 |
|
| 5319 |
result = gfc_get_array_expr (tsource->ts.type, tsource->ts.kind, |
| 5320 |
&tsource->where); |
| 5321 |
if (tsource->ts.type == BT_DERIVED) |
| 5322 |
result->ts.u.derived = tsource->ts.u.derived; |
| 5323 |
else if (tsource->ts.type == BT_CHARACTER) |
| 5324 |
result->ts.u.cl = tsource->ts.u.cl; |
| 5325 |
|
| 5326 |
tsource_ctor = gfc_constructor_first (tsource->value.constructor); |
| 5327 |
fsource_ctor = gfc_constructor_first (fsource->value.constructor); |
| 5328 |
mask_ctor = gfc_constructor_first (mask->value.constructor); |
| 5329 |
|
| 5330 |
while (mask_ctor) |
| 5331 |
{ |
| 5332 |
if (mask_ctor->expr->value.logical) |
| 5333 |
gfc_constructor_append_expr (&result->value.constructor, |
| 5334 |
gfc_copy_expr (tsource_ctor->expr), |
| 5335 |
NULL); |
| 5336 |
else |
| 5337 |
gfc_constructor_append_expr (&result->value.constructor, |
| 5338 |
gfc_copy_expr (fsource_ctor->expr), |
| 5339 |
NULL); |
| 5340 |
tsource_ctor = gfc_constructor_next (tsource_ctor); |
| 5341 |
fsource_ctor = gfc_constructor_next (fsource_ctor); |
| 5342 |
mask_ctor = gfc_constructor_next (mask_ctor); |
| 5343 |
} |
| 5344 |
|
| 5345 |
result->shape = gfc_get_shape (1); |
| 5346 |
gfc_array_size (result, &result->shape[0]); |
| 5347 |
|
| 5348 |
return result; |
| 5349 |
} |
| 5350 |
|
| 5351 |
|
| 5352 |
gfc_expr * |
| 5353 |
gfc_simplify_merge_bits (gfc_expr *i, gfc_expr *j, gfc_expr *mask_expr) |
| 5354 |
{ |
| 5355 |
mpz_t arg1, arg2, mask; |
| 5356 |
gfc_expr *result; |
| 5357 |
|
| 5358 |
if (i->expr_type != EXPR_CONSTANT || j->expr_type != EXPR_CONSTANT |
| 5359 |
|| mask_expr->expr_type != EXPR_CONSTANT) |
| 5360 |
return NULL; |
| 5361 |
|
| 5362 |
result = gfc_get_constant_expr (i->ts.type, i->ts.kind, &i->where); |
| 5363 |
|
| 5364 |
/* Convert all argument to unsigned. */ |
| 5365 |
mpz_init_set (arg1, i->value.integer); |
| 5366 |
mpz_init_set (arg2, j->value.integer); |
| 5367 |
mpz_init_set (mask, mask_expr->value.integer); |
| 5368 |
|
| 5369 |
/* MERGE_BITS(I,J,MASK) = IOR (IAND (I, MASK), IAND (J, NOT (MASK))). */ |
| 5370 |
mpz_and (arg1, arg1, mask); |
| 5371 |
mpz_com (mask, mask); |
| 5372 |
mpz_and (arg2, arg2, mask); |
| 5373 |
mpz_ior (result->value.integer, arg1, arg2); |
| 5374 |
|
| 5375 |
mpz_clear (arg1); |
| 5376 |
mpz_clear (arg2); |
| 5377 |
mpz_clear (mask); |
| 5378 |
|
| 5379 |
return result; |
| 5380 |
} |
| 5381 |
|
| 5382 |
|
| 5383 |
/* Selects between current value and extremum for simplify_min_max |
| 5384 |
and simplify_minval_maxval. */ |
| 5385 |
static int |
| 5386 |
min_max_choose (gfc_expr *arg, gfc_expr *extremum, int sign, bool back_val) |
| 5387 |
{ |
| 5388 |
int ret; |
| 5389 |
|
| 5390 |
switch (arg->ts.type) |
| 5391 |
{ |
| 5392 |
case BT_INTEGER: |
| 5393 |
case BT_UNSIGNED: |
| 5394 |
if (extremum->ts.kind < arg->ts.kind) |
| 5395 |
extremum->ts.kind = arg->ts.kind; |
| 5396 |
ret = mpz_cmp (arg->value.integer, |
| 5397 |
extremum->value.integer) * sign; |
| 5398 |
if (ret > 0) |
| 5399 |
mpz_set (extremum->value.integer, arg->value.integer); |
| 5400 |
break; |
| 5401 |
|
| 5402 |
case BT_REAL: |
| 5403 |
if (extremum->ts.kind < arg->ts.kind) |
| 5404 |
extremum->ts.kind = arg->ts.kind; |
| 5405 |
if (mpfr_nan_p (extremum->value.real)) |
| 5406 |
{ |
| 5407 |
ret = 1; |
| 5408 |
mpfr_set (extremum->value.real, arg->value.real, GFC_RND_MODE); |
| 5409 |
} |
| 5410 |
else if (mpfr_nan_p (arg->value.real)) |
| 5411 |
ret = -1; |
| 5412 |
else |
| 5413 |
{ |
| 5414 |
ret = mpfr_cmp (arg->value.real, extremum->value.real) * sign; |
| 5415 |
if (ret > 0) |
| 5416 |
mpfr_set (extremum->value.real, arg->value.real, GFC_RND_MODE); |
| 5417 |
} |
| 5418 |
break; |
| 5419 |
|
| 5420 |
case BT_CHARACTER: |
| 5421 |
#define LENGTH(x) ((x)->value.character.length) |
| 5422 |
#define STRING(x) ((x)->value.character.string) |
| 5423 |
if (LENGTH (extremum) < LENGTH(arg)) |
| 5424 |
{ |
| 5425 |
gfc_char_t *tmp = STRING(extremum); |
| 5426 |
|
| 5427 |
STRING(extremum) = gfc_get_wide_string (LENGTH(arg) + 1); |
| 5428 |
memcpy (STRING(extremum), tmp, |
| 5429 |
LENGTH(extremum) * sizeof (gfc_char_t)); |
| 5430 |
gfc_wide_memset (&STRING(extremum)[LENGTH(extremum)], ' ', |
| 5431 |
LENGTH(arg) - LENGTH(extremum)); |
| 5432 |
STRING(extremum)[LENGTH(arg)] = '\0'; /* For debugger */ |
| 5433 |
LENGTH(extremum) = LENGTH(arg); |
| 5434 |
free (tmp); |
| 5435 |
} |
| 5436 |
ret = gfc_compare_string (arg, extremum) * sign; |
| 5437 |
if (ret > 0) |
| 5438 |
{ |
| 5439 |
free (STRING(extremum)); |
| 5440 |
STRING(extremum) = gfc_get_wide_string (LENGTH(extremum) + 1); |
| 5441 |
memcpy (STRING(extremum), STRING(arg), |
| 5442 |
LENGTH(arg) * sizeof (gfc_char_t)); |
| 5443 |
gfc_wide_memset (&STRING(extremum)[LENGTH(arg)], ' ', |
| 5444 |
LENGTH(extremum) - LENGTH(arg)); |
| 5445 |
STRING(extremum)[LENGTH(extremum)] = '\0'; /* For debugger */ |
| 5446 |
} |
| 5447 |
#undef LENGTH |
| 5448 |
#undef STRING |
| 5449 |
break; |
| 5450 |
|
| 5451 |
default: |
| 5452 |
gfc_internal_error ("simplify_min_max(): Bad type in arglist"); |
| 5453 |
} |
| 5454 |
if (back_val && ret == 0) |
| 5455 |
ret = 1; |
| 5456 |
|
| 5457 |
return ret; |
| 5458 |
} |
| 5459 |
|
| 5460 |
|
| 5461 |
/* This function is special since MAX() can take any number of |
| 5462 |
arguments. The simplified expression is a rewritten version of the |
| 5463 |
argument list containing at most one constant element. Other |
| 5464 |
constant elements are deleted. Because the argument list has |
| 5465 |
already been checked, this function always succeeds. sign is 1 for |
| 5466 |
MAX(), -1 for MIN(). */ |
| 5467 |
|
| 5468 |
static gfc_expr * |
| 5469 |
simplify_min_max (gfc_expr *expr, int sign) |
| 5470 |
{ |
| 5471 |
int tmp1, tmp2; |
| 5472 |
gfc_actual_arglist *arg, *last, *extremum; |
| 5473 |
gfc_expr *tmp, *ret; |
| 5474 |
const char *fname; |
| 5475 |
|
| 5476 |
last = NULL; |
| 5477 |
extremum = NULL; |
| 5478 |
|
| 5479 |
arg = expr->value.function.actual; |
| 5480 |
|
| 5481 |
for (; arg; last = arg, arg = arg->next) |
| 5482 |
{ |
| 5483 |
if (arg->expr->expr_type != EXPR_CONSTANT) |
| 5484 |
continue; |
| 5485 |
|
| 5486 |
if (extremum == NULL) |
| 5487 |
{ |
| 5488 |
extremum = arg; |
| 5489 |
continue; |
| 5490 |
} |
| 5491 |
|
| 5492 |
min_max_choose (arg->expr, extremum->expr, sign); |
| 5493 |
|
| 5494 |
/* Delete the extra constant argument. */ |
| 5495 |
last->next = arg->next; |
| 5496 |
|
| 5497 |
arg->next = NULL; |
| 5498 |
gfc_free_actual_arglist (arg); |
| 5499 |
arg = last; |
| 5500 |
} |
| 5501 |
|
| 5502 |
/* If there is one value left, replace the function call with the |
| 5503 |
expression. */ |
| 5504 |
if (expr->value.function.actual->next != NULL) |
| 5505 |
return NULL; |
| 5506 |
|
| 5507 |
/* Handle special cases of specific functions (min|max)1 and |
| 5508 |
a(min|max)0. */ |
| 5509 |
|
| 5510 |
tmp = expr->value.function.actual->expr; |
| 5511 |
fname = expr->value.function.isym->name; |
| 5512 |
|
| 5513 |
if ((tmp->ts.type != BT_INTEGER || tmp->ts.kind != gfc_integer_4_kind) |
| 5514 |
&& (strcmp (fname, "min1") == 0 || strcmp (fname, "max1") == 0)) |
| 5515 |
{ |
| 5516 |
/* Explicit conversion, turn off -Wconversion and -Wconversion-extra |
| 5517 |
warnings. */ |
| 5518 |
tmp1 = warn_conversion; |
| 5519 |
tmp2 = warn_conversion_extra; |
| 5520 |
warn_conversion = warn_conversion_extra = 0; |
| 5521 |
|
| 5522 |
ret = gfc_convert_constant (tmp, BT_INTEGER, gfc_integer_4_kind); |
| 5523 |
|
| 5524 |
warn_conversion = tmp1; |
| 5525 |
warn_conversion_extra = tmp2; |
| 5526 |
} |
| 5527 |
else if ((tmp->ts.type != BT_REAL || tmp->ts.kind != gfc_real_4_kind) |
| 5528 |
&& (strcmp (fname, "amin0") == 0 || strcmp (fname, "amax0") == 0)) |
| 5529 |
{ |
| 5530 |
ret = gfc_convert_constant (tmp, BT_REAL, gfc_real_4_kind); |
| 5531 |
} |
| 5532 |
else |
| 5533 |
ret = gfc_copy_expr (tmp); |
| 5534 |
|
| 5535 |
return ret; |
| 5536 |
|
| 5537 |
} |
| 5538 |
|
| 5539 |
|
| 5540 |
gfc_expr * |
| 5541 |
gfc_simplify_min (gfc_expr *e) |
| 5542 |
{ |
| 5543 |
return simplify_min_max (e, -1); |
| 5544 |
} |
| 5545 |
|
| 5546 |
|
| 5547 |
gfc_expr * |
| 5548 |
gfc_simplify_max (gfc_expr *e) |
| 5549 |
{ |
| 5550 |
return simplify_min_max (e, 1); |
| 5551 |
} |
| 5552 |
|
| 5553 |
/* Helper function for gfc_simplify_minval. */ |
| 5554 |
|
| 5555 |
static gfc_expr * |
| 5556 |
gfc_min (gfc_expr *op1, gfc_expr *op2) |
| 5557 |
{ |
| 5558 |
min_max_choose (op1, op2, -1); |
| 5559 |
gfc_free_expr (op1); |
| 5560 |
return op2; |
| 5561 |
} |
| 5562 |
|
| 5563 |
/* Simplify minval for constant arrays. */ |
| 5564 |
|
| 5565 |
gfc_expr * |
| 5566 |
gfc_simplify_minval (gfc_expr *array, gfc_expr* dim, gfc_expr *mask) |
| 5567 |
{ |
| 5568 |
return simplify_transformation (array, dim, mask, INT_MAX, gfc_min); |
| 5569 |
} |
| 5570 |
|
| 5571 |
/* Helper function for gfc_simplify_maxval. */ |
| 5572 |
|
| 5573 |
static gfc_expr * |
| 5574 |
gfc_max (gfc_expr *op1, gfc_expr *op2) |
| 5575 |
{ |
| 5576 |
min_max_choose (op1, op2, 1); |
| 5577 |
gfc_free_expr (op1); |
| 5578 |
return op2; |
| 5579 |
} |
| 5580 |
|
| 5581 |
|
| 5582 |
/* Simplify maxval for constant arrays. */ |
| 5583 |
|
| 5584 |
gfc_expr * |
| 5585 |
gfc_simplify_maxval (gfc_expr *array, gfc_expr* dim, gfc_expr *mask) |
| 5586 |
{ |
| 5587 |
return simplify_transformation (array, dim, mask, INT_MIN, gfc_max); |
| 5588 |
} |
| 5589 |
|
| 5590 |
|
| 5591 |
/* Transform minloc or maxloc of an array, according to MASK, |
| 5592 |
to the scalar result. This code is mostly identical to |
| 5593 |
simplify_transformation_to_scalar. */ |
| 5594 |
|
| 5595 |
static gfc_expr * |
| 5596 |
simplify_minmaxloc_to_scalar (gfc_expr *result, gfc_expr *array, gfc_expr *mask, |
| 5597 |
gfc_expr *extremum, int sign, bool back_val) |
| 5598 |
{ |
| 5599 |
gfc_expr *a, *m; |
| 5600 |
gfc_constructor *array_ctor, *mask_ctor; |
| 5601 |
mpz_t count; |
| 5602 |
|
| 5603 |
mpz_set_si (result->value.integer, 0); |
| 5604 |
|
| 5605 |
|
| 5606 |
/* Shortcut for constant .FALSE. MASK. */ |
| 5607 |
if (mask |
| 5608 |
&& mask->expr_type == EXPR_CONSTANT |
| 5609 |
&& !mask->value.logical) |
| 5610 |
return result; |
| 5611 |
|
| 5612 |
array_ctor = gfc_constructor_first (array->value.constructor); |
| 5613 |
if (mask && mask->expr_type == EXPR_ARRAY) |
| 5614 |
mask_ctor = gfc_constructor_first (mask->value.constructor); |
| 5615 |
else |
| 5616 |
mask_ctor = NULL; |
| 5617 |
|
| 5618 |
mpz_init_set_si (count, 0); |
| 5619 |
while (array_ctor) |
| 5620 |
{ |
| 5621 |
mpz_add_ui (count, count, 1); |
| 5622 |
a = array_ctor->expr; |
| 5623 |
array_ctor = gfc_constructor_next (array_ctor); |
| 5624 |
/* A constant MASK equals .TRUE. here and can be ignored. */ |
| 5625 |
if (mask_ctor) |
| 5626 |
{ |
| 5627 |
m = mask_ctor->expr; |
| 5628 |
mask_ctor = gfc_constructor_next (mask_ctor); |
| 5629 |
if (!m->value.logical) |
| 5630 |
continue; |
| 5631 |
} |
| 5632 |
if (min_max_choose (a, extremum, sign, back_val) > 0) |
| 5633 |
mpz_set (result->value.integer, count); |
| 5634 |
} |
| 5635 |
mpz_clear (count); |
| 5636 |
gfc_free_expr (extremum); |
| 5637 |
return result; |
| 5638 |
} |
| 5639 |
|
| 5640 |
/* Simplify minloc / maxloc in the absence of a dim argument. */ |
| 5641 |
|
| 5642 |
static gfc_expr * |
| 5643 |
simplify_minmaxloc_nodim (gfc_expr *result, gfc_expr *extremum, |
| 5644 |
gfc_expr *array, gfc_expr *mask, int sign, |
| 5645 |
bool back_val) |
| 5646 |
{ |
| 5647 |
ssize_t res[GFC_MAX_DIMENSIONS]; |
| 5648 |
int i, n; |
| 5649 |
gfc_constructor *result_ctor, *array_ctor, *mask_ctor; |
| 5650 |
ssize_t count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS], |
| 5651 |
sstride[GFC_MAX_DIMENSIONS]; |
| 5652 |
gfc_expr *a, *m; |
| 5653 |
bool continue_loop; |
| 5654 |
bool ma; |
| 5655 |
|
| 5656 |
for (i = 0; i<array->rank; i++) |
| 5657 |
res[i] = -1; |
| 5658 |
|
| 5659 |
/* Shortcut for constant .FALSE. MASK. */ |
| 5660 |
if (mask |
| 5661 |
&& mask->expr_type == EXPR_CONSTANT |
| 5662 |
&& !mask->value.logical) |
| 5663 |
goto finish; |
| 5664 |
|
| 5665 |
if (array->shape == NULL) |
| 5666 |
goto finish; |
| 5667 |
|
| 5668 |
for (i = 0; i < array->rank; i++) |
| 5669 |
{ |
| 5670 |
count[i] = 0; |
| 5671 |
sstride[i] = (i == 0) ? 1 : sstride[i-1] * mpz_get_si (array->shape[i-1]); |
| 5672 |
extent[i] = mpz_get_si (array->shape[i]); |
| 5673 |
if (extent[i] <= 0) |
| 5674 |
goto finish; |
| 5675 |
} |
| 5676 |
|
| 5677 |
continue_loop = true; |
| 5678 |
array_ctor = gfc_constructor_first (array->value.constructor); |
| 5679 |
if (mask && mask->rank > 0) |
| 5680 |
mask_ctor = gfc_constructor_first (mask->value.constructor); |
| 5681 |
else |
| 5682 |
mask_ctor = NULL; |
| 5683 |
|
| 5684 |
/* Loop over the array elements (and mask), keeping track of |
| 5685 |
the indices to return. */ |
| 5686 |
while (continue_loop) |
| 5687 |
{ |
| 5688 |
do |
| 5689 |
{ |
| 5690 |
a = array_ctor->expr; |
| 5691 |
if (mask_ctor) |
| 5692 |
{ |
| 5693 |
m = mask_ctor->expr; |
| 5694 |
ma = m->value.logical; |
| 5695 |
mask_ctor = gfc_constructor_next (mask_ctor); |
| 5696 |
} |
| 5697 |
else |
| 5698 |
ma = true; |
| 5699 |
|
| 5700 |
if (ma && min_max_choose (a, extremum, sign, back_val) > 0) |
| 5701 |
{ |
| 5702 |
for (i = 0; i<array->rank; i++) |
| 5703 |
res[i] = count[i]; |
| 5704 |
} |
| 5705 |
array_ctor = gfc_constructor_next (array_ctor); |
| 5706 |
count[0] ++; |
| 5707 |
} while (count[0] != extent[0]); |
| 5708 |
n = 0; |
| 5709 |
do |
| 5710 |
{ |
| 5711 |
/* When we get to the end of a dimension, reset it and increment |
| 5712 |
the next dimension. */ |
| 5713 |
count[n] = 0; |
| 5714 |
n++; |
| 5715 |
if (n >= array->rank) |
| 5716 |
{ |
| 5717 |
continue_loop = false; |
| 5718 |
break; |
| 5719 |
} |
| 5720 |
else |
| 5721 |
count[n] ++; |
| 5722 |
} while (count[n] == extent[n]); |
| 5723 |
} |
| 5724 |
|
| 5725 |
finish: |
| 5726 |
gfc_free_expr (extremum); |
| 5727 |
result_ctor = gfc_constructor_first (result->value.constructor); |
| 5728 |
for (i = 0; i<array->rank; i++) |
| 5729 |
{ |
| 5730 |
gfc_expr *r_expr; |
| 5731 |
r_expr = result_ctor->expr; |
| 5732 |
mpz_set_si (r_expr->value.integer, res[i] + 1); |
| 5733 |
result_ctor = gfc_constructor_next (result_ctor); |
| 5734 |
} |
| 5735 |
return result; |
| 5736 |
} |
| 5737 |
|
| 5738 |
/* Helper function for gfc_simplify_minmaxloc - build an array |
| 5739 |
expression with n elements. */ |
| 5740 |
|
| 5741 |
static gfc_expr * |
| 5742 |
new_array (bt type, int kind, int n, locus *where) |
| 5743 |
{ |
| 5744 |
gfc_expr *result; |
| 5745 |
int i; |
| 5746 |
|
| 5747 |
result = gfc_get_array_expr (type, kind, where); |
| 5748 |
result->rank = 1; |
| 5749 |
result->shape = gfc_get_shape(1); |
| 5750 |
mpz_init_set_si (result->shape[0], n); |
| 5751 |
for (i = 0; i < n; i++) |
| 5752 |
{ |
| 5753 |
gfc_constructor_append_expr (&result->value.constructor, |
| 5754 |
gfc_get_constant_expr (type, kind, where), |
| 5755 |
NULL); |
| 5756 |
} |
| 5757 |
|
| 5758 |
return result; |
| 5759 |
} |
| 5760 |
|
| 5761 |
/* Simplify minloc and maxloc. This code is mostly identical to |
| 5762 |
simplify_transformation_to_array. */ |
| 5763 |
|
| 5764 |
static gfc_expr * |
| 5765 |
simplify_minmaxloc_to_array (gfc_expr *result, gfc_expr *array, |
| 5766 |
gfc_expr *dim, gfc_expr *mask, |
| 5767 |
gfc_expr *extremum, int sign, bool back_val) |
| 5768 |
{ |
| 5769 |
mpz_t size; |
| 5770 |
int done, i, n, arraysize, resultsize, dim_index, dim_extent, dim_stride; |
| 5771 |
gfc_expr **arrayvec, **resultvec, **base, **src, **dest; |
| 5772 |
gfc_constructor *array_ctor, *mask_ctor, *result_ctor; |
| 5773 |
|
| 5774 |
int count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS], |
| 5775 |
sstride[GFC_MAX_DIMENSIONS], dstride[GFC_MAX_DIMENSIONS], |
| 5776 |
tmpstride[GFC_MAX_DIMENSIONS]; |
| 5777 |
|
| 5778 |
/* Shortcut for constant .FALSE. MASK. */ |
| 5779 |
if (mask |
| 5780 |
&& mask->expr_type == EXPR_CONSTANT |
| 5781 |
&& !mask->value.logical) |
| 5782 |
return result; |
| 5783 |
|
| 5784 |
/* Build an indexed table for array element expressions to minimize |
| 5785 |
linked-list traversal. Masked elements are set to NULL. */ |
| 5786 |
gfc_array_size (array, &size); |
| 5787 |
arraysize = mpz_get_ui (size); |
| 5788 |
mpz_clear (size); |
| 5789 |
|
| 5790 |
arrayvec = XCNEWVEC (gfc_expr*, arraysize); |
| 5791 |
|
| 5792 |
array_ctor = gfc_constructor_first (array->value.constructor); |
| 5793 |
mask_ctor = NULL; |
| 5794 |
if (mask && mask->expr_type == EXPR_ARRAY) |
| 5795 |
mask_ctor = gfc_constructor_first (mask->value.constructor); |
| 5796 |
|
| 5797 |
for (i = 0; i < arraysize; ++i) |
| 5798 |
{ |
| 5799 |
arrayvec[i] = array_ctor->expr; |
| 5800 |
array_ctor = gfc_constructor_next (array_ctor); |
| 5801 |
|
| 5802 |
if (mask_ctor) |
| 5803 |
{ |
| 5804 |
if (!mask_ctor->expr->value.logical) |
| 5805 |
arrayvec[i] = NULL; |
| 5806 |
|
| 5807 |
mask_ctor = gfc_constructor_next (mask_ctor); |
| 5808 |
} |
| 5809 |
} |
| 5810 |
|
| 5811 |
/* Same for the result expression. */ |
| 5812 |
gfc_array_size (result, &size); |
| 5813 |
resultsize = mpz_get_ui (size); |
| 5814 |
mpz_clear (size); |
| 5815 |
|
| 5816 |
resultvec = XCNEWVEC (gfc_expr*, resultsize); |
| 5817 |
result_ctor = gfc_constructor_first (result->value.constructor); |
| 5818 |
for (i = 0; i < resultsize; ++i) |
| 5819 |
{ |
| 5820 |
resultvec[i] = result_ctor->expr; |
| 5821 |
result_ctor = gfc_constructor_next (result_ctor); |
| 5822 |
} |
| 5823 |
|
| 5824 |
gfc_extract_int (dim, &dim_index); |
| 5825 |
dim_index -= 1; /* zero-base index */ |
| 5826 |
dim_extent = 0; |
| 5827 |
dim_stride = 0; |
| 5828 |
|
| 5829 |
for (i = 0, n = 0; i < array->rank; ++i) |
| 5830 |
{ |
| 5831 |
count[i] = 0; |
| 5832 |
tmpstride[i] = (i == 0) ? 1 : tmpstride[i-1] * mpz_get_si (array->shape[i-1]); |
| 5833 |
if (i == dim_index) |
| 5834 |
{ |
| 5835 |
dim_extent = mpz_get_si (array->shape[i]); |
| 5836 |
dim_stride = tmpstride[i]; |
| 5837 |
continue; |
| 5838 |
} |
| 5839 |
|
| 5840 |
extent[n] = mpz_get_si (array->shape[i]); |
| 5841 |
sstride[n] = tmpstride[i]; |
| 5842 |
dstride[n] = (n == 0) ? 1 : dstride[n-1] * extent[n-1]; |
| 5843 |
n += 1; |
| 5844 |
} |
| 5845 |
|
| 5846 |
done = resultsize <= 0; |
| 5847 |
base = arrayvec; |
| 5848 |
dest = resultvec; |
| 5849 |
while (!done) |
| 5850 |
{ |
| 5851 |
gfc_expr *ex; |
| 5852 |
ex = gfc_copy_expr (extremum); |
| 5853 |
for (src = base, n = 0; n < dim_extent; src += dim_stride, ++n) |
| 5854 |
{ |
| 5855 |
if (*src && min_max_choose (*src, ex, sign, back_val) > 0) |
| 5856 |
mpz_set_si ((*dest)->value.integer, n + 1); |
| 5857 |
} |
| 5858 |
|
| 5859 |
count[0]++; |
| 5860 |
base += sstride[0]; |
| 5861 |
dest += dstride[0]; |
| 5862 |
gfc_free_expr (ex); |
| 5863 |
|
| 5864 |
n = 0; |
| 5865 |
while (!done && count[n] == extent[n]) |
| 5866 |
{ |
| 5867 |
count[n] = 0; |
| 5868 |
base -= sstride[n] * extent[n]; |
| 5869 |
dest -= dstride[n] * extent[n]; |
| 5870 |
|
| 5871 |
n++; |
| 5872 |
if (n < result->rank) |
| 5873 |
{ |
| 5874 |
/* If the nested loop is unrolled GFC_MAX_DIMENSIONS |
| 5875 |
times, we'd warn for the last iteration, because the |
| 5876 |
array index will have already been incremented to the |
| 5877 |
array sizes, and we can't tell that this must make |
| 5878 |
the test against result->rank false, because ranks |
| 5879 |
must not exceed GFC_MAX_DIMENSIONS. */ |
| 5880 |
GCC_DIAGNOSTIC_PUSH_IGNORED (-Warray-bounds) |
| 5881 |
count[n]++; |
| 5882 |
base += sstride[n]; |
| 5883 |
dest += dstride[n]; |
| 5884 |
GCC_DIAGNOSTIC_POP |
| 5885 |
} |
| 5886 |
else |
| 5887 |
done = true; |
| 5888 |
} |
| 5889 |
} |
| 5890 |
|
| 5891 |
/* Place updated expression in result constructor. */ |
| 5892 |
result_ctor = gfc_constructor_first (result->value.constructor); |
| 5893 |
for (i = 0; i < resultsize; ++i) |
| 5894 |
{ |
| 5895 |
result_ctor->expr = resultvec[i]; |
| 5896 |
result_ctor = gfc_constructor_next (result_ctor); |
| 5897 |
} |
| 5898 |
|
| 5899 |
free (arrayvec); |
| 5900 |
free (resultvec); |
| 5901 |
free (extremum); |
| 5902 |
return result; |
| 5903 |
} |
| 5904 |
|
| 5905 |
/* Simplify minloc and maxloc for constant arrays. */ |
| 5906 |
|
| 5907 |
static gfc_expr * |
| 5908 |
gfc_simplify_minmaxloc (gfc_expr *array, gfc_expr *dim, gfc_expr *mask, |
| 5909 |
gfc_expr *kind, gfc_expr *back, int sign) |
| 5910 |
{ |
| 5911 |
gfc_expr *result; |
| 5912 |
gfc_expr *extremum; |
| 5913 |
int ikind; |
| 5914 |
int init_val; |
| 5915 |
bool back_val = false; |
| 5916 |
|
| 5917 |
if (!is_constant_array_expr (array) |
| 5918 |
|| !gfc_is_constant_expr (dim)) |
| 5919 |
return NULL; |
| 5920 |
|
| 5921 |
if (mask |
| 5922 |
&& !is_constant_array_expr (mask) |
| 5923 |
&& mask->expr_type != EXPR_CONSTANT) |
| 5924 |
return NULL; |
| 5925 |
|
| 5926 |
if (kind) |
| 5927 |
{ |
| 5928 |
if (gfc_extract_int (kind, &ikind, -1)) |
| 5929 |
return NULL; |
| 5930 |
} |
| 5931 |
else |
| 5932 |
ikind = gfc_default_integer_kind; |
| 5933 |
|
| 5934 |
if (back) |
| 5935 |
{ |
| 5936 |
if (back->expr_type != EXPR_CONSTANT) |
| 5937 |
return NULL; |
| 5938 |
|
| 5939 |
back_val = back->value.logical; |
| 5940 |
} |
| 5941 |
|
| 5942 |
if (sign < 0) |
| 5943 |
init_val = INT_MAX; |
| 5944 |
else if (sign > 0) |
| 5945 |
init_val = INT_MIN; |
| 5946 |
else |
| 5947 |
gcc_unreachable(); |
| 5948 |
|
| 5949 |
extremum = gfc_get_constant_expr (array->ts.type, array->ts.kind, &array->where); |
| 5950 |
init_result_expr (extremum, init_val, array); |
| 5951 |
|
| 5952 |
if (dim) |
| 5953 |
{ |
| 5954 |
result = transformational_result (array, dim, BT_INTEGER, |
| 5955 |
ikind, &array->where); |
| 5956 |
init_result_expr (result, 0, array); |
| 5957 |
|
| 5958 |
if (array->rank == 1) |
| 5959 |
return simplify_minmaxloc_to_scalar (result, array, mask, extremum, |
| 5960 |
sign, back_val); |
| 5961 |
else |
| 5962 |
return simplify_minmaxloc_to_array (result, array, dim, mask, extremum, |
| 5963 |
sign, back_val); |
| 5964 |
} |
| 5965 |
else |
| 5966 |
{ |
| 5967 |
result = new_array (BT_INTEGER, ikind, array->rank, &array->where); |
| 5968 |
return simplify_minmaxloc_nodim (result, extremum, array, mask, |
| 5969 |
sign, back_val); |
| 5970 |
} |
| 5971 |
} |
| 5972 |
|
| 5973 |
gfc_expr * |
| 5974 |
gfc_simplify_minloc (gfc_expr *array, gfc_expr *dim, gfc_expr *mask, gfc_expr *kind, |
| 5975 |
gfc_expr *back) |
| 5976 |
{ |
| 5977 |
return gfc_simplify_minmaxloc (array, dim, mask, kind, back, -1); |
| 5978 |
} |
| 5979 |
|
| 5980 |
gfc_expr * |
| 5981 |
gfc_simplify_maxloc (gfc_expr *array, gfc_expr *dim, gfc_expr *mask, gfc_expr *kind, |
| 5982 |
gfc_expr *back) |
| 5983 |
{ |
| 5984 |
return gfc_simplify_minmaxloc (array, dim, mask, kind, back, 1); |
| 5985 |
} |
| 5986 |
|
| 5987 |
/* Simplify findloc to scalar. Similar to |
| 5988 |
simplify_minmaxloc_to_scalar. */ |
| 5989 |
|
| 5990 |
static gfc_expr * |
| 5991 |
simplify_findloc_to_scalar (gfc_expr *result, gfc_expr *array, gfc_expr *value, |
| 5992 |
gfc_expr *mask, int back_val) |
| 5993 |
{ |
| 5994 |
gfc_expr *a, *m; |
| 5995 |
gfc_constructor *array_ctor, *mask_ctor; |
| 5996 |
mpz_t count; |
| 5997 |
|
| 5998 |
mpz_set_si (result->value.integer, 0); |
| 5999 |
|
| 6000 |
/* Shortcut for constant .FALSE. MASK. */ |
| 6001 |
if (mask |
| 6002 |
&& mask->expr_type == EXPR_CONSTANT |
| 6003 |
&& !mask->value.logical) |
| 6004 |
return result; |
| 6005 |
|
| 6006 |
array_ctor = gfc_constructor_first (array->value.constructor); |
| 6007 |
if (mask && mask->expr_type == EXPR_ARRAY) |
| 6008 |
mask_ctor = gfc_constructor_first (mask->value.constructor); |
| 6009 |
else |
| 6010 |
mask_ctor = NULL; |
| 6011 |
|
| 6012 |
mpz_init_set_si (count, 0); |
| 6013 |
while (array_ctor) |
| 6014 |
{ |
| 6015 |
mpz_add_ui (count, count, 1); |
| 6016 |
a = array_ctor->expr; |
| 6017 |
array_ctor = gfc_constructor_next (array_ctor); |
| 6018 |
/* A constant MASK equals .TRUE. here and can be ignored. */ |
| 6019 |
if (mask_ctor) |
| 6020 |
{ |
| 6021 |
m = mask_ctor->expr; |
| 6022 |
mask_ctor = gfc_constructor_next (mask_ctor); |
| 6023 |
if (!m->value.logical) |
| 6024 |
continue; |
| 6025 |
} |
| 6026 |
if (gfc_compare_expr (a, value, INTRINSIC_EQ) == 0) |
| 6027 |
{ |
| 6028 |
/* We have a match. If BACK is true, continue so we find |
| 6029 |
the last one. */ |
| 6030 |
mpz_set (result->value.integer, count); |
| 6031 |
if (!back_val) |
| 6032 |
break; |
| 6033 |
} |
| 6034 |
} |
| 6035 |
mpz_clear (count); |
| 6036 |
return result; |
| 6037 |
} |
| 6038 |
|
| 6039 |
/* Simplify findloc in the absence of a dim argument. Similar to |
| 6040 |
simplify_minmaxloc_nodim. */ |
| 6041 |
|
| 6042 |
static gfc_expr * |
| 6043 |
simplify_findloc_nodim (gfc_expr *result, gfc_expr *value, gfc_expr *array, |
| 6044 |
gfc_expr *mask, bool back_val) |
| 6045 |
{ |
| 6046 |
ssize_t res[GFC_MAX_DIMENSIONS]; |
| 6047 |
int i, n; |
| 6048 |
gfc_constructor *result_ctor, *array_ctor, *mask_ctor; |
| 6049 |
ssize_t count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS], |
| 6050 |
sstride[GFC_MAX_DIMENSIONS]; |
| 6051 |
gfc_expr *a, *m; |
| 6052 |
bool continue_loop; |
| 6053 |
bool ma; |
| 6054 |
|
| 6055 |
for (i = 0; i < array->rank; i++) |
| 6056 |
res[i] = -1; |
| 6057 |
|
| 6058 |
/* Shortcut for constant .FALSE. MASK. */ |
| 6059 |
if (mask |
| 6060 |
&& mask->expr_type == EXPR_CONSTANT |
| 6061 |
&& !mask->value.logical) |
| 6062 |
goto finish; |
| 6063 |
|
| 6064 |
for (i = 0; i < array->rank; i++) |
| 6065 |
{ |
| 6066 |
count[i] = 0; |
| 6067 |
sstride[i] = (i == 0) ? 1 : sstride[i-1] * mpz_get_si (array->shape[i-1]); |
| 6068 |
extent[i] = mpz_get_si (array->shape[i]); |
| 6069 |
if (extent[i] <= 0) |
| 6070 |
goto finish; |
| 6071 |
} |
| 6072 |
|
| 6073 |
continue_loop = true; |
| 6074 |
array_ctor = gfc_constructor_first (array->value.constructor); |
| 6075 |
if (mask && mask->rank > 0) |
| 6076 |
mask_ctor = gfc_constructor_first (mask->value.constructor); |
| 6077 |
else |
| 6078 |
mask_ctor = NULL; |
| 6079 |
|
| 6080 |
/* Loop over the array elements (and mask), keeping track of |
| 6081 |
the indices to return. */ |
| 6082 |
while (continue_loop) |
| 6083 |
{ |
| 6084 |
do |
| 6085 |
{ |
| 6086 |
a = array_ctor->expr; |
| 6087 |
if (mask_ctor) |
| 6088 |
{ |
| 6089 |
m = mask_ctor->expr; |
| 6090 |
ma = m->value.logical; |
| 6091 |
mask_ctor = gfc_constructor_next (mask_ctor); |
| 6092 |
} |
| 6093 |
else |
| 6094 |
ma = true; |
| 6095 |
|
| 6096 |
if (ma && gfc_compare_expr (a, value, INTRINSIC_EQ) == 0) |
| 6097 |
{ |
| 6098 |
for (i = 0; i < array->rank; i++) |
| 6099 |
res[i] = count[i]; |
| 6100 |
if (!back_val) |
| 6101 |
goto finish; |
| 6102 |
} |
| 6103 |
array_ctor = gfc_constructor_next (array_ctor); |
| 6104 |
count[0] ++; |
| 6105 |
} while (count[0] != extent[0]); |
| 6106 |
n = 0; |
| 6107 |
do |
| 6108 |
{ |
| 6109 |
/* When we get to the end of a dimension, reset it and increment |
| 6110 |
the next dimension. */ |
| 6111 |
count[n] = 0; |
| 6112 |
n++; |
| 6113 |
if (n >= array->rank) |
| 6114 |
{ |
| 6115 |
continue_loop = false; |
| 6116 |
break; |
| 6117 |
} |
| 6118 |
else |
| 6119 |
count[n] ++; |
| 6120 |
} while (count[n] == extent[n]); |
| 6121 |
} |
| 6122 |
|
| 6123 |
finish: |
| 6124 |
result_ctor = gfc_constructor_first (result->value.constructor); |
| 6125 |
for (i = 0; i < array->rank; i++) |
| 6126 |
{ |
| 6127 |
gfc_expr *r_expr; |
| 6128 |
r_expr = result_ctor->expr; |
| 6129 |
mpz_set_si (r_expr->value.integer, res[i] + 1); |
| 6130 |
result_ctor = gfc_constructor_next (result_ctor); |
| 6131 |
} |
| 6132 |
return result; |
| 6133 |
} |
| 6134 |
|
| 6135 |
|
| 6136 |
/* Simplify findloc to an array. Similar to |
| 6137 |
simplify_minmaxloc_to_array. */ |
| 6138 |
|
| 6139 |
static gfc_expr * |
| 6140 |
simplify_findloc_to_array (gfc_expr *result, gfc_expr *array, gfc_expr *value, |
| 6141 |
gfc_expr *dim, gfc_expr *mask, bool back_val) |
| 6142 |
{ |
| 6143 |
mpz_t size; |
| 6144 |
int done, i, n, arraysize, resultsize, dim_index, dim_extent, dim_stride; |
| 6145 |
gfc_expr **arrayvec, **resultvec, **base, **src, **dest; |
| 6146 |
gfc_constructor *array_ctor, *mask_ctor, *result_ctor; |
| 6147 |
|
| 6148 |
int count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS], |
| 6149 |
sstride[GFC_MAX_DIMENSIONS], dstride[GFC_MAX_DIMENSIONS], |
| 6150 |
tmpstride[GFC_MAX_DIMENSIONS]; |
| 6151 |
|
| 6152 |
/* Shortcut for constant .FALSE. MASK. */ |
| 6153 |
if (mask |
| 6154 |
&& mask->expr_type == EXPR_CONSTANT |
| 6155 |
&& !mask->value.logical) |
| 6156 |
return result; |
| 6157 |
|
| 6158 |
/* Build an indexed table for array element expressions to minimize |
| 6159 |
linked-list traversal. Masked elements are set to NULL. */ |
| 6160 |
gfc_array_size (array, &size); |
| 6161 |
arraysize = mpz_get_ui (size); |
| 6162 |
mpz_clear (size); |
| 6163 |
|
| 6164 |
arrayvec = XCNEWVEC (gfc_expr*, arraysize); |
| 6165 |
|
| 6166 |
array_ctor = gfc_constructor_first (array->value.constructor); |
| 6167 |
mask_ctor = NULL; |
| 6168 |
if (mask && mask->expr_type == EXPR_ARRAY) |
| 6169 |
mask_ctor = gfc_constructor_first (mask->value.constructor); |
| 6170 |
|
| 6171 |
for (i = 0; i < arraysize; ++i) |
| 6172 |
{ |
| 6173 |
arrayvec[i] = array_ctor->expr; |
| 6174 |
array_ctor = gfc_constructor_next (array_ctor); |
| 6175 |
|
| 6176 |
if (mask_ctor) |
| 6177 |
{ |
| 6178 |
if (!mask_ctor->expr->value.logical) |
| 6179 |
arrayvec[i] = NULL; |
| 6180 |
|
| 6181 |
mask_ctor = gfc_constructor_next (mask_ctor); |
| 6182 |
} |
| 6183 |
} |
| 6184 |
|
| 6185 |
/* Same for the result expression. */ |
| 6186 |
gfc_array_size (result, &size); |
| 6187 |
resultsize = mpz_get_ui (size); |
| 6188 |
mpz_clear (size); |
| 6189 |
|
| 6190 |
resultvec = XCNEWVEC (gfc_expr*, resultsize); |
| 6191 |
result_ctor = gfc_constructor_first (result->value.constructor); |
| 6192 |
for (i = 0; i < resultsize; ++i) |
| 6193 |
{ |
| 6194 |
resultvec[i] = result_ctor->expr; |
| 6195 |
result_ctor = gfc_constructor_next (result_ctor); |
| 6196 |
} |
| 6197 |
|
| 6198 |
gfc_extract_int (dim, &dim_index); |
| 6199 |
|
| 6200 |
dim_index -= 1; /* Zero-base index. */ |
| 6201 |
dim_extent = 0; |
| 6202 |
dim_stride = 0; |
| 6203 |
|
| 6204 |
for (i = 0, n = 0; i < array->rank; ++i) |
| 6205 |
{ |
| 6206 |
count[i] = 0; |
| 6207 |
tmpstride[i] = (i == 0) ? 1 : tmpstride[i-1] * mpz_get_si (array->shape[i-1]); |
| 6208 |
if (i == dim_index) |
| 6209 |
{ |
| 6210 |
dim_extent = mpz_get_si (array->shape[i]); |
| 6211 |
dim_stride = tmpstride[i]; |
| 6212 |
continue; |
| 6213 |
} |
| 6214 |
|
| 6215 |
extent[n] = mpz_get_si (array->shape[i]); |
| 6216 |
sstride[n] = tmpstride[i]; |
| 6217 |
dstride[n] = (n == 0) ? 1 : dstride[n-1] * extent[n-1]; |
| 6218 |
n += 1; |
| 6219 |
} |
| 6220 |
|
| 6221 |
done = resultsize <= 0; |
| 6222 |
base = arrayvec; |
| 6223 |
dest = resultvec; |
| 6224 |
while (!done) |
| 6225 |
{ |
| 6226 |
for (src = base, n = 0; n < dim_extent; src += dim_stride, ++n) |
| 6227 |
{ |
| 6228 |
if (*src && gfc_compare_expr (*src, value, INTRINSIC_EQ) == 0) |
| 6229 |
{ |
| 6230 |
mpz_set_si ((*dest)->value.integer, n + 1); |
| 6231 |
if (!back_val) |
| 6232 |
break; |
| 6233 |
} |
| 6234 |
} |
| 6235 |
|
| 6236 |
count[0]++; |
| 6237 |
base += sstride[0]; |
| 6238 |
dest += dstride[0]; |
| 6239 |
|
| 6240 |
n = 0; |
| 6241 |
while (!done && count[n] == extent[n]) |
| 6242 |
{ |
| 6243 |
count[n] = 0; |
| 6244 |
base -= sstride[n] * extent[n]; |
| 6245 |
dest -= dstride[n] * extent[n]; |
| 6246 |
|
| 6247 |
n++; |
| 6248 |
if (n < result->rank) |
| 6249 |
{ |
| 6250 |
/* If the nested loop is unrolled GFC_MAX_DIMENSIONS |
| 6251 |
times, we'd warn for the last iteration, because the |
| 6252 |
array index will have already been incremented to the |
| 6253 |
array sizes, and we can't tell that this must make |
| 6254 |
the test against result->rank false, because ranks |
| 6255 |
must not exceed GFC_MAX_DIMENSIONS. */ |
| 6256 |
GCC_DIAGNOSTIC_PUSH_IGNORED (-Warray-bounds) |
| 6257 |
count[n]++; |
| 6258 |
base += sstride[n]; |
| 6259 |
dest += dstride[n]; |
| 6260 |
GCC_DIAGNOSTIC_POP |
| 6261 |
} |
| 6262 |
else |
| 6263 |
done = true; |
| 6264 |
} |
| 6265 |
} |
| 6266 |
|
| 6267 |
/* Place updated expression in result constructor. */ |
| 6268 |
result_ctor = gfc_constructor_first (result->value.constructor); |
| 6269 |
for (i = 0; i < resultsize; ++i) |
| 6270 |
{ |
| 6271 |
result_ctor->expr = resultvec[i]; |
| 6272 |
result_ctor = gfc_constructor_next (result_ctor); |
| 6273 |
} |
| 6274 |
|
| 6275 |
free (arrayvec); |
| 6276 |
free (resultvec); |
| 6277 |
return result; |
| 6278 |
} |
| 6279 |
|
| 6280 |
/* Simplify findloc. */ |
| 6281 |
|
| 6282 |
gfc_expr * |
| 6283 |
gfc_simplify_findloc (gfc_expr *array, gfc_expr *value, gfc_expr *dim, |
| 6284 |
gfc_expr *mask, gfc_expr *kind, gfc_expr *back) |
| 6285 |
{ |
| 6286 |
gfc_expr *result; |
| 6287 |
int ikind; |
| 6288 |
bool back_val = false; |
| 6289 |
|
| 6290 |
if (!is_constant_array_expr (array) |
| 6291 |
|| array->shape == NULL |
| 6292 |
|| !gfc_is_constant_expr (dim)) |
| 6293 |
return NULL; |
| 6294 |
|
| 6295 |
if (! gfc_is_constant_expr (value)) |
| 6296 |
return 0; |
| 6297 |
|
| 6298 |
if (mask |
| 6299 |
&& !is_constant_array_expr (mask) |
| 6300 |
&& mask->expr_type != EXPR_CONSTANT) |
| 6301 |
return NULL; |
| 6302 |
|
| 6303 |
if (kind) |
| 6304 |
{ |
| 6305 |
if (gfc_extract_int (kind, &ikind, -1)) |
| 6306 |
return NULL; |
| 6307 |
} |
| 6308 |
else |
| 6309 |
ikind = gfc_default_integer_kind; |
| 6310 |
|
| 6311 |
if (back) |
| 6312 |
{ |
| 6313 |
if (back->expr_type != EXPR_CONSTANT) |
| 6314 |
return NULL; |
| 6315 |
|
| 6316 |
back_val = back->value.logical; |
| 6317 |
} |
| 6318 |
|
| 6319 |
if (dim) |
| 6320 |
{ |
| 6321 |
result = transformational_result (array, dim, BT_INTEGER, |
| 6322 |
ikind, &array->where); |
| 6323 |
init_result_expr (result, 0, array); |
| 6324 |
|
| 6325 |
if (array->rank == 1) |
| 6326 |
return simplify_findloc_to_scalar (result, array, value, mask, |
| 6327 |
back_val); |
| 6328 |
else |
| 6329 |
return simplify_findloc_to_array (result, array, value, dim, mask, |
| 6330 |
back_val); |
| 6331 |
} |
| 6332 |
else |
| 6333 |
{ |
| 6334 |
result = new_array (BT_INTEGER, ikind, array->rank, &array->where); |
| 6335 |
return simplify_findloc_nodim (result, value, array, mask, back_val); |
| 6336 |
} |
| 6337 |
return NULL; |
| 6338 |
} |
| 6339 |
|
| 6340 |
gfc_expr * |
| 6341 |
gfc_simplify_maxexponent (gfc_expr *x) |
| 6342 |
{ |
| 6343 |
int i = gfc_validate_kind (BT_REAL, x->ts.kind, false); |
| 6344 |
return gfc_get_int_expr (gfc_default_integer_kind, &x->where, |
| 6345 |
gfc_real_kinds[i].max_exponent); |
| 6346 |
} |
| 6347 |
|
| 6348 |
|
| 6349 |
gfc_expr * |
| 6350 |
gfc_simplify_minexponent (gfc_expr *x) |
| 6351 |
{ |
| 6352 |
int i = gfc_validate_kind (BT_REAL, x->ts.kind, false); |
| 6353 |
return gfc_get_int_expr (gfc_default_integer_kind, &x->where, |
| 6354 |
gfc_real_kinds[i].min_exponent); |
| 6355 |
} |
| 6356 |
|
| 6357 |
|
| 6358 |
gfc_expr * |
| 6359 |
gfc_simplify_mod (gfc_expr *a, gfc_expr *p) |
| 6360 |
{ |
| 6361 |
gfc_expr *result; |
| 6362 |
int kind; |
| 6363 |
|
| 6364 |
/* First check p. */ |
| 6365 |
if (p->expr_type != EXPR_CONSTANT) |
| 6366 |
return NULL; |
| 6367 |
|
| 6368 |
/* p shall not be 0. */ |
| 6369 |
switch (p->ts.type) |
| 6370 |
{ |
| 6371 |
case BT_INTEGER: |
| 6372 |
case BT_UNSIGNED: |
| 6373 |
if (mpz_cmp_ui (p->value.integer, 0) == 0) |
| 6374 |
{ |
| 6375 |
gfc_error ("Argument %qs of MOD at %L shall not be zero", |
| 6376 |
"P", &p->where); |
| 6377 |
return &gfc_bad_expr; |
| 6378 |
} |
| 6379 |
break; |
| 6380 |
case BT_REAL: |
| 6381 |
if (mpfr_cmp_ui (p->value.real, 0) == 0) |
| 6382 |
{ |
| 6383 |
gfc_error ("Argument %qs of MOD at %L shall not be zero", |
| 6384 |
"P", &p->where); |
| 6385 |
return &gfc_bad_expr; |
| 6386 |
} |
| 6387 |
break; |
| 6388 |
default: |
| 6389 |
gfc_internal_error ("gfc_simplify_mod(): Bad arguments"); |
| 6390 |
} |
| 6391 |
|
| 6392 |
if (a->expr_type != EXPR_CONSTANT) |
| 6393 |
return NULL; |
| 6394 |
|
| 6395 |
kind = a->ts.kind > p->ts.kind ? a->ts.kind : p->ts.kind; |
| 6396 |
result = gfc_get_constant_expr (a->ts.type, kind, &a->where); |
| 6397 |
|
| 6398 |
if (a->ts.type == BT_INTEGER || a->ts.type == BT_UNSIGNED) |
| 6399 |
mpz_tdiv_r (result->value.integer, a->value.integer, p->value.integer); |
| 6400 |
else |
| 6401 |
{ |
| 6402 |
gfc_set_model_kind (kind); |
| 6403 |
mpfr_fmod (result->value.real, a->value.real, p->value.real, |
| 6404 |
GFC_RND_MODE); |
| 6405 |
} |
| 6406 |
|
| 6407 |
return range_check (result, "MOD"); |
| 6408 |
} |
| 6409 |
|
| 6410 |
|
| 6411 |
gfc_expr * |
| 6412 |
gfc_simplify_modulo (gfc_expr *a, gfc_expr *p) |
| 6413 |
{ |
| 6414 |
gfc_expr *result; |
| 6415 |
int kind; |
| 6416 |
|
| 6417 |
/* First check p. */ |
| 6418 |
if (p->expr_type != EXPR_CONSTANT) |
| 6419 |
return NULL; |
| 6420 |
|
| 6421 |
/* p shall not be 0. */ |
| 6422 |
switch (p->ts.type) |
| 6423 |
{ |
| 6424 |
case BT_INTEGER: |
| 6425 |
case BT_UNSIGNED: |
| 6426 |
if (mpz_cmp_ui (p->value.integer, 0) == 0) |
| 6427 |
{ |
| 6428 |
gfc_error ("Argument %qs of MODULO at %L shall not be zero", |
| 6429 |
"P", &p->where); |
| 6430 |
return &gfc_bad_expr; |
| 6431 |
} |
| 6432 |
break; |
| 6433 |
case BT_REAL: |
| 6434 |
if (mpfr_cmp_ui (p->value.real, 0) == 0) |
| 6435 |
{ |
| 6436 |
gfc_error ("Argument %qs of MODULO at %L shall not be zero", |
| 6437 |
"P", &p->where); |
| 6438 |
return &gfc_bad_expr; |
| 6439 |
} |
| 6440 |
break; |
| 6441 |
default: |
| 6442 |
gfc_internal_error ("gfc_simplify_modulo(): Bad arguments"); |
| 6443 |
} |
| 6444 |
|
| 6445 |
if (a->expr_type != EXPR_CONSTANT) |
| 6446 |
return NULL; |
| 6447 |
|
| 6448 |
kind = a->ts.kind > p->ts.kind ? a->ts.kind : p->ts.kind; |
| 6449 |
result = gfc_get_constant_expr (a->ts.type, kind, &a->where); |
| 6450 |
|
| 6451 |
if (a->ts.type == BT_INTEGER || a->ts.type == BT_UNSIGNED) |
| 6452 |
mpz_fdiv_r (result->value.integer, a->value.integer, p->value.integer); |
| 6453 |
else |
| 6454 |
{ |
| 6455 |
gfc_set_model_kind (kind); |
| 6456 |
mpfr_fmod (result->value.real, a->value.real, p->value.real, |
| 6457 |
GFC_RND_MODE); |
| 6458 |
if (mpfr_cmp_ui (result->value.real, 0) != 0) |
| 6459 |
{ |
| 6460 |
if (mpfr_signbit (a->value.real) != mpfr_signbit (p->value.real)) |
| 6461 |
mpfr_add (result->value.real, result->value.real, p->value.real, |
| 6462 |
GFC_RND_MODE); |
| 6463 |
} |
| 6464 |
else |
| 6465 |
mpfr_copysign (result->value.real, result->value.real, |
| 6466 |
p->value.real, GFC_RND_MODE); |
| 6467 |
} |
| 6468 |
|
| 6469 |
return range_check (result, "MODULO"); |
| 6470 |
} |
| 6471 |
|
| 6472 |
|
| 6473 |
gfc_expr * |
| 6474 |
gfc_simplify_nearest (gfc_expr *x, gfc_expr *s) |
| 6475 |
{ |
| 6476 |
gfc_expr *result; |
| 6477 |
mpfr_exp_t emin, emax; |
| 6478 |
int kind; |
| 6479 |
|
| 6480 |
if (x->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT) |
| 6481 |
return NULL; |
| 6482 |
|
| 6483 |
result = gfc_copy_expr (x); |
| 6484 |
|
| 6485 |
/* Save current values of emin and emax. */ |
| 6486 |
emin = mpfr_get_emin (); |
| 6487 |
emax = mpfr_get_emax (); |
| 6488 |
|
| 6489 |
/* Set emin and emax for the current model number. */ |
| 6490 |
kind = gfc_validate_kind (BT_REAL, x->ts.kind, 0); |
| 6491 |
mpfr_set_emin ((mpfr_exp_t) gfc_real_kinds[kind].min_exponent - |
| 6492 |
mpfr_get_prec(result->value.real) + 1); |
| 6493 |
mpfr_set_emax ((mpfr_exp_t) gfc_real_kinds[kind].max_exponent); |
| 6494 |
mpfr_check_range (result->value.real, 0, MPFR_RNDU); |
| 6495 |
|
| 6496 |
if (mpfr_sgn (s->value.real) > 0) |
| 6497 |
{ |
| 6498 |
mpfr_nextabove (result->value.real); |
| 6499 |
mpfr_subnormalize (result->value.real, 0, MPFR_RNDU); |
| 6500 |
} |
| 6501 |
else |
| 6502 |
{ |
| 6503 |
mpfr_nextbelow (result->value.real); |
| 6504 |
mpfr_subnormalize (result->value.real, 0, MPFR_RNDD); |
| 6505 |
} |
| 6506 |
|
| 6507 |
mpfr_set_emin (emin); |
| 6508 |
mpfr_set_emax (emax); |
| 6509 |
|
| 6510 |
/* Only NaN can occur. Do not use range check as it gives an |
| 6511 |
error for denormal numbers. */ |
| 6512 |
if (mpfr_nan_p (result->value.real) && flag_range_check) |
| 6513 |
{ |
| 6514 |
gfc_error ("Result of NEAREST is NaN at %L", &result->where); |
| 6515 |
gfc_free_expr (result); |
| 6516 |
return &gfc_bad_expr; |
| 6517 |
} |
| 6518 |
|
| 6519 |
return result; |
| 6520 |
} |
| 6521 |
|
| 6522 |
|
| 6523 |
static gfc_expr * |
| 6524 |
simplify_nint (const char *name, gfc_expr *e, gfc_expr *k) |
| 6525 |
{ |
| 6526 |
gfc_expr *itrunc, *result; |
| 6527 |
int kind; |
| 6528 |
|
| 6529 |
kind = get_kind (BT_INTEGER, k, name, gfc_default_integer_kind); |
| 6530 |
if (kind == -1) |
| 6531 |
return &gfc_bad_expr; |
| 6532 |
|
| 6533 |
if (e->expr_type != EXPR_CONSTANT) |
| 6534 |
return NULL; |
| 6535 |
|
| 6536 |
itrunc = gfc_copy_expr (e); |
| 6537 |
mpfr_round (itrunc->value.real, e->value.real); |
| 6538 |
|
| 6539 |
result = gfc_get_constant_expr (BT_INTEGER, kind, &e->where); |
| 6540 |
gfc_mpfr_to_mpz (result->value.integer, itrunc->value.real, &e->where); |
| 6541 |
|
| 6542 |
gfc_free_expr (itrunc); |
| 6543 |
|
| 6544 |
return range_check (result, name); |
| 6545 |
} |
| 6546 |
|
| 6547 |
|
| 6548 |
gfc_expr * |
| 6549 |
gfc_simplify_new_line (gfc_expr *e) |
| 6550 |
{ |
| 6551 |
gfc_expr *result; |
| 6552 |
|
| 6553 |
result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, 1); |
| 6554 |
result->value.character.string[0] = '\n'; |
| 6555 |
|
| 6556 |
return result; |
| 6557 |
} |
| 6558 |
|
| 6559 |
|
| 6560 |
gfc_expr * |
| 6561 |
gfc_simplify_nint (gfc_expr *e, gfc_expr *k) |
| 6562 |
{ |
| 6563 |
return simplify_nint ("NINT", e, k); |
| 6564 |
} |
| 6565 |
|
| 6566 |
|
| 6567 |
gfc_expr * |
| 6568 |
gfc_simplify_idnint (gfc_expr *e) |
| 6569 |
{ |
| 6570 |
return simplify_nint ("IDNINT", e, NULL); |
| 6571 |
} |
| 6572 |
|
| 6573 |
static int norm2_scale; |
| 6574 |
|
| 6575 |
static gfc_expr * |
| 6576 |
norm2_add_squared (gfc_expr *result, gfc_expr *e) |
| 6577 |
{ |
| 6578 |
mpfr_t tmp; |
| 6579 |
|
| 6580 |
gcc_assert (e->ts.type == BT_REAL && e->expr_type == EXPR_CONSTANT); |
| 6581 |
gcc_assert (result->ts.type == BT_REAL |
| 6582 |
&& result->expr_type == EXPR_CONSTANT); |
| 6583 |
|
| 6584 |
gfc_set_model_kind (result->ts.kind); |
| 6585 |
int index = gfc_validate_kind (BT_REAL, result->ts.kind, false); |
| 6586 |
mpfr_exp_t exp; |
| 6587 |
if (mpfr_regular_p (result->value.real)) |
| 6588 |
{ |
| 6589 |
exp = mpfr_get_exp (result->value.real); |
| 6590 |
/* If result is getting close to overflowing, scale down. */ |
| 6591 |
if (exp >= gfc_real_kinds[index].max_exponent - 4 |
| 6592 |
&& norm2_scale <= gfc_real_kinds[index].max_exponent - 2) |
| 6593 |
{ |
| 6594 |
norm2_scale += 2; |
| 6595 |
mpfr_div_ui (result->value.real, result->value.real, 16, |
| 6596 |
GFC_RND_MODE); |
| 6597 |
} |
| 6598 |
} |
| 6599 |
|
| 6600 |
mpfr_init (tmp); |
| 6601 |
if (mpfr_regular_p (e->value.real)) |
| 6602 |
{ |
| 6603 |
exp = mpfr_get_exp (e->value.real); |
| 6604 |
/* If e**2 would overflow or close to overflowing, scale down. */ |
| 6605 |
if (exp - norm2_scale >= gfc_real_kinds[index].max_exponent / 2 - 2) |
| 6606 |
{ |
| 6607 |
int new_scale = gfc_real_kinds[index].max_exponent / 2 + 4; |
| 6608 |
mpfr_set_ui (tmp, 1, GFC_RND_MODE); |
| 6609 |
mpfr_set_exp (tmp, new_scale - norm2_scale); |
| 6610 |
mpfr_div (result->value.real, result->value.real, tmp, GFC_RND_MODE); |
| 6611 |
mpfr_div (result->value.real, result->value.real, tmp, GFC_RND_MODE); |
| 6612 |
norm2_scale = new_scale; |
| 6613 |
} |
| 6614 |
} |
| 6615 |
if (norm2_scale) |
| 6616 |
{ |
| 6617 |
mpfr_set_ui (tmp, 1, GFC_RND_MODE); |
| 6618 |
mpfr_set_exp (tmp, norm2_scale); |
| 6619 |
mpfr_div (tmp, e->value.real, tmp, GFC_RND_MODE); |
| 6620 |
} |
| 6621 |
else |
| 6622 |
mpfr_set (tmp, e->value.real, GFC_RND_MODE); |
| 6623 |
mpfr_pow_ui (tmp, tmp, 2, GFC_RND_MODE); |
| 6624 |
mpfr_add (result->value.real, result->value.real, tmp, |
| 6625 |
GFC_RND_MODE); |
| 6626 |
mpfr_clear (tmp); |
| 6627 |
|
| 6628 |
return result; |
| 6629 |
} |
| 6630 |
|
| 6631 |
|
| 6632 |
static gfc_expr * |
| 6633 |
norm2_do_sqrt (gfc_expr *result, gfc_expr *e) |
| 6634 |
{ |
| 6635 |
gcc_assert (e->ts.type == BT_REAL && e->expr_type == EXPR_CONSTANT); |
| 6636 |
gcc_assert (result->ts.type == BT_REAL |
| 6637 |
&& result->expr_type == EXPR_CONSTANT); |
| 6638 |
|
| 6639 |
if (result != e) |
| 6640 |
mpfr_set (result->value.real, e->value.real, GFC_RND_MODE); |
| 6641 |
mpfr_sqrt (result->value.real, result->value.real, GFC_RND_MODE); |
| 6642 |
if (norm2_scale && mpfr_regular_p (result->value.real)) |
| 6643 |
{ |
| 6644 |
mpfr_t tmp; |
| 6645 |
mpfr_init (tmp); |
| 6646 |
mpfr_set_ui (tmp, 1, GFC_RND_MODE); |
| 6647 |
mpfr_set_exp (tmp, norm2_scale); |
| 6648 |
mpfr_mul (result->value.real, result->value.real, tmp, GFC_RND_MODE); |
| 6649 |
mpfr_clear (tmp); |
| 6650 |
} |
| 6651 |
norm2_scale = 0; |
| 6652 |
|
| 6653 |
return result; |
| 6654 |
} |
| 6655 |
|
| 6656 |
|
| 6657 |
gfc_expr * |
| 6658 |
gfc_simplify_norm2 (gfc_expr *e, gfc_expr *dim) |
| 6659 |
{ |
| 6660 |
gfc_expr *result; |
| 6661 |
bool size_zero; |
| 6662 |
|
| 6663 |
size_zero = gfc_is_size_zero_array (e); |
| 6664 |
|
| 6665 |
if (!(is_constant_array_expr (e) || size_zero) |
| 6666 |
|| (dim != NULL && !gfc_is_constant_expr (dim))) |
| 6667 |
return NULL; |
| 6668 |
|
| 6669 |
result = transformational_result (e, dim, e->ts.type, e->ts.kind, &e->where); |
| 6670 |
init_result_expr (result, 0, NULL); |
| 6671 |
|
| 6672 |
if (size_zero) |
| 6673 |
return result; |
| 6674 |
|
| 6675 |
norm2_scale = 0; |
| 6676 |
if (!dim || e->rank == 1) |
| 6677 |
{ |
| 6678 |
result = simplify_transformation_to_scalar (result, e, NULL, |
| 6679 |
norm2_add_squared); |
| 6680 |
mpfr_sqrt (result->value.real, result->value.real, GFC_RND_MODE); |
| 6681 |
if (norm2_scale && mpfr_regular_p (result->value.real)) |
| 6682 |
{ |
| 6683 |
mpfr_t tmp; |
| 6684 |
mpfr_init (tmp); |
| 6685 |
mpfr_set_ui (tmp, 1, GFC_RND_MODE); |
| 6686 |
mpfr_set_exp (tmp, norm2_scale); |
| 6687 |
mpfr_mul (result->value.real, result->value.real, tmp, GFC_RND_MODE); |
| 6688 |
mpfr_clear (tmp); |
| 6689 |
} |
| 6690 |
norm2_scale = 0; |
| 6691 |
} |
| 6692 |
else |
| 6693 |
result = simplify_transformation_to_array (result, e, dim, NULL, |
| 6694 |
norm2_add_squared, |
| 6695 |
norm2_do_sqrt); |
| 6696 |
|
| 6697 |
return result; |
| 6698 |
} |
| 6699 |
|
| 6700 |
|
| 6701 |
gfc_expr * |
| 6702 |
gfc_simplify_not (gfc_expr *e) |
| 6703 |
{ |
| 6704 |
gfc_expr *result; |
| 6705 |
|
| 6706 |
if (e->expr_type != EXPR_CONSTANT) |
| 6707 |
return NULL; |
| 6708 |
|
| 6709 |
result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where); |
| 6710 |
mpz_com (result->value.integer, e->value.integer); |
| 6711 |
|
| 6712 |
return range_check (result, "NOT"); |
| 6713 |
} |
| 6714 |
|
| 6715 |
|
| 6716 |
gfc_expr * |
| 6717 |
gfc_simplify_null (gfc_expr *mold) |
| 6718 |
{ |
| 6719 |
gfc_expr *result; |
| 6720 |
|
| 6721 |
if (mold) |
| 6722 |
{ |
| 6723 |
result = gfc_copy_expr (mold); |
| 6724 |
result->expr_type = EXPR_NULL; |
| 6725 |
} |
| 6726 |
else |
| 6727 |
result = gfc_get_null_expr (NULL); |
| 6728 |
|
| 6729 |
return result; |
| 6730 |
} |
| 6731 |
|
| 6732 |
|
| 6733 |
gfc_expr * |
| 6734 |
gfc_simplify_num_images (gfc_expr *distance ATTRIBUTE_UNUSED, gfc_expr *failed) |
| 6735 |
{ |
| 6736 |
gfc_expr *result; |
| 6737 |
|
| 6738 |
if (flag_coarray == GFC_FCOARRAY_NONE) |
| 6739 |
{ |
| 6740 |
gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable"); |
| 6741 |
return &gfc_bad_expr; |
| 6742 |
} |
| 6743 |
|
| 6744 |
if (flag_coarray != GFC_FCOARRAY_SINGLE) |
| 6745 |
return NULL; |
| 6746 |
|
| 6747 |
if (failed && failed->expr_type != EXPR_CONSTANT) |
| 6748 |
return NULL; |
| 6749 |
|
| 6750 |
/* FIXME: gfc_current_locus is wrong. */ |
| 6751 |
result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind, |
| 6752 |
&gfc_current_locus); |
| 6753 |
|
| 6754 |
if (failed && failed->value.logical != 0) |
| 6755 |
mpz_set_si (result->value.integer, 0); |
| 6756 |
else |
| 6757 |
mpz_set_si (result->value.integer, 1); |
| 6758 |
|
| 6759 |
return result; |
| 6760 |
} |
| 6761 |
|
| 6762 |
|
| 6763 |
gfc_expr * |
| 6764 |
gfc_simplify_or (gfc_expr *x, gfc_expr *y) |
| 6765 |
{ |
| 6766 |
gfc_expr *result; |
| 6767 |
int kind; |
| 6768 |
|
| 6769 |
if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT) |
| 6770 |
return NULL; |
| 6771 |
|
| 6772 |
kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind; |
| 6773 |
|
| 6774 |
switch (x->ts.type) |
| 6775 |
{ |
| 6776 |
case BT_INTEGER: |
| 6777 |
result = gfc_get_constant_expr (BT_INTEGER, kind, &x->where); |
| 6778 |
mpz_ior (result->value.integer, x->value.integer, y->value.integer); |
| 6779 |
return range_check (result, "OR"); |
| 6780 |
|
| 6781 |
case BT_LOGICAL: |
| 6782 |
return gfc_get_logical_expr (kind, &x->where, |
| 6783 |
x->value.logical || y->value.logical); |
| 6784 |
default: |
| 6785 |
gcc_unreachable(); |
| 6786 |
} |
| 6787 |
} |
| 6788 |
|
| 6789 |
|
| 6790 |
gfc_expr * |
| 6791 |
gfc_simplify_out_of_range (gfc_expr *x, gfc_expr *mold, gfc_expr *round) |
| 6792 |
{ |
| 6793 |
gfc_expr *result; |
| 6794 |
mpfr_t a; |
| 6795 |
mpz_t b; |
| 6796 |
int i, k; |
| 6797 |
bool res = false; |
| 6798 |
bool rnd = false; |
| 6799 |
|
| 6800 |
i = gfc_validate_kind (x->ts.type, x->ts.kind, false); |
| 6801 |
k = gfc_validate_kind (mold->ts.type, mold->ts.kind, false); |
| 6802 |
|
| 6803 |
mpfr_init (a); |
| 6804 |
|
| 6805 |
switch (x->ts.type) |
| 6806 |
{ |
| 6807 |
case BT_REAL: |
| 6808 |
if (mold->ts.type == BT_REAL) |
| 6809 |
{ |
| 6810 |
if (mpfr_cmp (gfc_real_kinds[i].huge, |
| 6811 |
gfc_real_kinds[k].huge) <= 0) |
| 6812 |
{ |
| 6813 |
/* Range of MOLD is always sufficient. */ |
| 6814 |
res = false; |
| 6815 |
goto done; |
| 6816 |
} |
| 6817 |
else if (x->expr_type == EXPR_CONSTANT) |
| 6818 |
{ |
| 6819 |
mpfr_neg (a, gfc_real_kinds[k].huge, GFC_RND_MODE); |
| 6820 |
res = (mpfr_cmp (x->value.real, a) < 0 |
| 6821 |
|| mpfr_cmp (x->value.real, gfc_real_kinds[k].huge) > 0); |
| 6822 |
goto done; |
| 6823 |
} |
| 6824 |
} |
| 6825 |
else if (mold->ts.type == BT_INTEGER) |
| 6826 |
{ |
| 6827 |
if (x->expr_type == EXPR_CONSTANT) |
| 6828 |
{ |
| 6829 |
res = mpfr_inf_p (x->value.real) || mpfr_nan_p (x->value.real); |
| 6830 |
if (res) |
| 6831 |
goto done; |
| 6832 |
|
| 6833 |
if (round && round->expr_type != EXPR_CONSTANT) |
| 6834 |
break; |
| 6835 |
|
| 6836 |
if (round && round->expr_type == EXPR_CONSTANT) |
| 6837 |
rnd = round->value.logical; |
| 6838 |
|
| 6839 |
if (rnd) |
| 6840 |
mpfr_round (a, x->value.real); |
| 6841 |
else |
| 6842 |
mpfr_trunc (a, x->value.real); |
| 6843 |
|
| 6844 |
mpz_init (b); |
| 6845 |
mpfr_get_z (b, a, GFC_RND_MODE); |
| 6846 |
res = (mpz_cmp (b, gfc_integer_kinds[k].min_int) < 0 |
| 6847 |
|| mpz_cmp (b, gfc_integer_kinds[k].huge) > 0); |
| 6848 |
mpz_clear (b); |
| 6849 |
goto done; |
| 6850 |
} |
| 6851 |
} |
| 6852 |
else if (mold->ts.type == BT_UNSIGNED) |
| 6853 |
{ |
| 6854 |
if (x->expr_type == EXPR_CONSTANT) |
| 6855 |
{ |
| 6856 |
res = mpfr_inf_p (x->value.real) || mpfr_nan_p (x->value.real); |
| 6857 |
if (res) |
| 6858 |
goto done; |
| 6859 |
|
| 6860 |
if (round && round->expr_type != EXPR_CONSTANT) |
| 6861 |
break; |
| 6862 |
|
| 6863 |
if (round && round->expr_type == EXPR_CONSTANT) |
| 6864 |
rnd = round->value.logical; |
| 6865 |
|
| 6866 |
if (rnd) |
| 6867 |
mpfr_round (a, x->value.real); |
| 6868 |
else |
| 6869 |
mpfr_trunc (a, x->value.real); |
| 6870 |
|
| 6871 |
mpz_init (b); |
| 6872 |
mpfr_get_z (b, a, GFC_RND_MODE); |
| 6873 |
res = (mpz_cmp (b, gfc_unsigned_kinds[k].huge) > 0 |
| 6874 |
|| mpz_cmp_si (b, 0) < 0); |
| 6875 |
mpz_clear (b); |
| 6876 |
goto done; |
| 6877 |
} |
| 6878 |
} |
| 6879 |
break; |
| 6880 |
|
| 6881 |
case BT_INTEGER: |
| 6882 |
gcc_assert (round == NULL); |
| 6883 |
if (mold->ts.type == BT_INTEGER) |
| 6884 |
{ |
| 6885 |
if (mpz_cmp (gfc_integer_kinds[i].huge, |
| 6886 |
gfc_integer_kinds[k].huge) <= 0) |
| 6887 |
{ |
| 6888 |
/* Range of MOLD is always sufficient. */ |
| 6889 |
res = false; |
| 6890 |
goto done; |
| 6891 |
} |
| 6892 |
else if (x->expr_type == EXPR_CONSTANT) |
| 6893 |
{ |
| 6894 |
res = (mpz_cmp (x->value.integer, |
| 6895 |
gfc_integer_kinds[k].min_int) < 0 |
| 6896 |
|| mpz_cmp (x->value.integer, |
| 6897 |
gfc_integer_kinds[k].huge) > 0); |
| 6898 |
goto done; |
| 6899 |
} |
| 6900 |
} |
| 6901 |
else if (mold->ts.type == BT_UNSIGNED) |
| 6902 |
{ |
| 6903 |
if (x->expr_type == EXPR_CONSTANT) |
| 6904 |
{ |
| 6905 |
res = (mpz_cmp_si (x->value.integer, 0) < 0 |
| 6906 |
|| mpz_cmp (x->value.integer, |
| 6907 |
gfc_unsigned_kinds[k].huge) > 0); |
| 6908 |
goto done; |
| 6909 |
} |
| 6910 |
} |
| 6911 |
else if (mold->ts.type == BT_REAL) |
| 6912 |
{ |
| 6913 |
mpfr_set_z (a, gfc_integer_kinds[i].min_int, GFC_RND_MODE); |
| 6914 |
mpfr_neg (a, a, GFC_RND_MODE); |
| 6915 |
res = mpfr_cmp (a, gfc_real_kinds[k].huge) > 0; |
| 6916 |
/* When false, range of MOLD is always sufficient. */ |
| 6917 |
if (!res) |
| 6918 |
goto done; |
| 6919 |
|
| 6920 |
if (x->expr_type == EXPR_CONSTANT) |
| 6921 |
{ |
| 6922 |
mpfr_set_z (a, x->value.integer, GFC_RND_MODE); |
| 6923 |
mpfr_abs (a, a, GFC_RND_MODE); |
| 6924 |
res = mpfr_cmp (a, gfc_real_kinds[k].huge) > 0; |
| 6925 |
goto done; |
| 6926 |
} |
| 6927 |
} |
| 6928 |
break; |
| 6929 |
|
| 6930 |
case BT_UNSIGNED: |
| 6931 |
gcc_assert (round == NULL); |
| 6932 |
if (mold->ts.type == BT_UNSIGNED) |
| 6933 |
{ |
| 6934 |
if (mpz_cmp (gfc_unsigned_kinds[i].huge, |
| 6935 |
gfc_unsigned_kinds[k].huge) <= 0) |
| 6936 |
{ |
| 6937 |
/* Range of MOLD is always sufficient. */ |
| 6938 |
res = false; |
| 6939 |
goto done; |
| 6940 |
} |
| 6941 |
else if (x->expr_type == EXPR_CONSTANT) |
| 6942 |
{ |
| 6943 |
res = mpz_cmp (x->value.integer, |
| 6944 |
gfc_unsigned_kinds[k].huge) > 0; |
| 6945 |
goto done; |
| 6946 |
} |
| 6947 |
} |
| 6948 |
else if (mold->ts.type == BT_INTEGER) |
| 6949 |
{ |
| 6950 |
if (mpz_cmp (gfc_unsigned_kinds[i].huge, |
| 6951 |
gfc_integer_kinds[k].huge) <= 0) |
| 6952 |
{ |
| 6953 |
/* Range of MOLD is always sufficient. */ |
| 6954 |
res = false; |
| 6955 |
goto done; |
| 6956 |
} |
| 6957 |
else if (x->expr_type == EXPR_CONSTANT) |
| 6958 |
{ |
| 6959 |
res = mpz_cmp (x->value.integer, |
| 6960 |
gfc_integer_kinds[k].huge) > 0; |
| 6961 |
goto done; |
| 6962 |
} |
| 6963 |
} |
| 6964 |
else if (mold->ts.type == BT_REAL) |
| 6965 |
{ |
| 6966 |
mpfr_set_z (a, gfc_unsigned_kinds[i].huge, GFC_RND_MODE); |
| 6967 |
res = mpfr_cmp (a, gfc_real_kinds[k].huge) > 0; |
| 6968 |
/* When false, range of MOLD is always sufficient. */ |
| 6969 |
if (!res) |
| 6970 |
goto done; |
| 6971 |
|
| 6972 |
if (x->expr_type == EXPR_CONSTANT) |
| 6973 |
{ |
| 6974 |
mpfr_set_z (a, x->value.integer, GFC_RND_MODE); |
| 6975 |
res = mpfr_cmp (a, gfc_real_kinds[k].huge) > 0; |
| 6976 |
goto done; |
| 6977 |
} |
| 6978 |
} |
| 6979 |
break; |
| 6980 |
|
| 6981 |
default: |
| 6982 |
gcc_unreachable (); |
| 6983 |
} |
| 6984 |
|
| 6985 |
mpfr_clear (a); |
| 6986 |
|
| 6987 |
return NULL; |
| 6988 |
|
| 6989 |
done: |
| 6990 |
result = gfc_get_logical_expr (gfc_default_logical_kind, &x->where, res); |
| 6991 |
|
| 6992 |
mpfr_clear (a); |
| 6993 |
|
| 6994 |
return result; |
| 6995 |
} |
| 6996 |
|
| 6997 |
|
| 6998 |
gfc_expr * |
| 6999 |
gfc_simplify_pack (gfc_expr *array, gfc_expr *mask, gfc_expr *vector) |
| 7000 |
{ |
| 7001 |
gfc_expr *result; |
| 7002 |
gfc_constructor *array_ctor, *mask_ctor, *vector_ctor; |
| 7003 |
|
| 7004 |
if (!is_constant_array_expr (array) |
| 7005 |
|| !is_constant_array_expr (vector) |
| 7006 |
|| (!gfc_is_constant_expr (mask) |
| 7007 |
&& !is_constant_array_expr (mask))) |
| 7008 |
return NULL; |
| 7009 |
|
| 7010 |
result = gfc_get_array_expr (array->ts.type, array->ts.kind, &array->where); |
| 7011 |
if (array->ts.type == BT_DERIVED) |
| 7012 |
result->ts.u.derived = array->ts.u.derived; |
| 7013 |
|
| 7014 |
array_ctor = gfc_constructor_first (array->value.constructor); |
| 7015 |
vector_ctor = vector |
| 7016 |
? gfc_constructor_first (vector->value.constructor) |
| 7017 |
: NULL; |
| 7018 |
|
| 7019 |
if (mask->expr_type == EXPR_CONSTANT |
| 7020 |
&& mask->value.logical) |
| 7021 |
{ |
| 7022 |
/* Copy all elements of ARRAY to RESULT. */ |
| 7023 |
while (array_ctor) |
| 7024 |
{ |
| 7025 |
gfc_constructor_append_expr (&result->value.constructor, |
| 7026 |
gfc_copy_expr (array_ctor->expr), |
| 7027 |
NULL); |
| 7028 |
|
| 7029 |
array_ctor = gfc_constructor_next (array_ctor); |
| 7030 |
vector_ctor = gfc_constructor_next (vector_ctor); |
| 7031 |
} |
| 7032 |
} |
| 7033 |
else if (mask->expr_type == EXPR_ARRAY) |
| 7034 |
{ |
| 7035 |
/* Copy only those elements of ARRAY to RESULT whose |
| 7036 |
MASK equals .TRUE.. */ |
| 7037 |
mask_ctor = gfc_constructor_first (mask->value.constructor); |
| 7038 |
while (mask_ctor && array_ctor) |
| 7039 |
{ |
| 7040 |
if (mask_ctor->expr->value.logical) |
| 7041 |
{ |
| 7042 |
gfc_constructor_append_expr (&result->value.constructor, |
| 7043 |
gfc_copy_expr (array_ctor->expr), |
| 7044 |
NULL); |
| 7045 |
vector_ctor = gfc_constructor_next (vector_ctor); |
| 7046 |
} |
| 7047 |
|
| 7048 |
array_ctor = gfc_constructor_next (array_ctor); |
| 7049 |
mask_ctor = gfc_constructor_next (mask_ctor); |
| 7050 |
} |
| 7051 |
} |
| 7052 |
|
| 7053 |
/* Append any left-over elements from VECTOR to RESULT. */ |
| 7054 |
while (vector_ctor) |
| 7055 |
{ |
| 7056 |
gfc_constructor_append_expr (&result->value.constructor, |
| 7057 |
gfc_copy_expr (vector_ctor->expr), |
| 7058 |
NULL); |
| 7059 |
vector_ctor = gfc_constructor_next (vector_ctor); |
| 7060 |
} |
| 7061 |
|
| 7062 |
result->shape = gfc_get_shape (1); |
| 7063 |
gfc_array_size (result, &result->shape[0]); |
| 7064 |
|
| 7065 |
if (array->ts.type == BT_CHARACTER) |
| 7066 |
result->ts.u.cl = array->ts.u.cl; |
| 7067 |
|
| 7068 |
return result; |
| 7069 |
} |
| 7070 |
|
| 7071 |
|
| 7072 |
static gfc_expr * |
| 7073 |
do_xor (gfc_expr *result, gfc_expr *e) |
| 7074 |
{ |
| 7075 |
gcc_assert (e->ts.type == BT_LOGICAL && e->expr_type == EXPR_CONSTANT); |
| 7076 |
gcc_assert (result->ts.type == BT_LOGICAL |
| 7077 |
&& result->expr_type == EXPR_CONSTANT); |
| 7078 |
|
| 7079 |
result->value.logical = result->value.logical != e->value.logical; |
| 7080 |
return result; |
| 7081 |
} |
| 7082 |
|
| 7083 |
|
| 7084 |
gfc_expr * |
| 7085 |
gfc_simplify_is_contiguous (gfc_expr *array) |
| 7086 |
{ |
| 7087 |
if (gfc_is_simply_contiguous (array, false, true)) |
| 7088 |
return gfc_get_logical_expr (gfc_default_logical_kind, &array->where, 1); |
| 7089 |
|
| 7090 |
if (gfc_is_not_contiguous (array)) |
| 7091 |
return gfc_get_logical_expr (gfc_default_logical_kind, &array->where, 0); |
| 7092 |
|
| 7093 |
return NULL; |
| 7094 |
} |
| 7095 |
|
| 7096 |
|
| 7097 |
gfc_expr * |
| 7098 |
gfc_simplify_parity (gfc_expr *e, gfc_expr *dim) |
| 7099 |
{ |
| 7100 |
return simplify_transformation (e, dim, NULL, 0, do_xor); |
| 7101 |
} |
| 7102 |
|
| 7103 |
|
| 7104 |
gfc_expr * |
| 7105 |
gfc_simplify_popcnt (gfc_expr *e) |
| 7106 |
{ |
| 7107 |
int res, k; |
| 7108 |
mpz_t x; |
| 7109 |
|
| 7110 |
if (e->expr_type != EXPR_CONSTANT) |
| 7111 |
return NULL; |
| 7112 |
|
| 7113 |
k = gfc_validate_kind (e->ts.type, e->ts.kind, false); |
| 7114 |
|
| 7115 |
if (flag_unsigned && e->ts.type == BT_UNSIGNED) |
| 7116 |
res = mpz_popcount (e->value.integer); |
| 7117 |
else |
| 7118 |
{ |
| 7119 |
/* Convert argument to unsigned, then count the '1' bits. */ |
| 7120 |
mpz_init_set (x, e->value.integer); |
| 7121 |
gfc_convert_mpz_to_unsigned (x, gfc_integer_kinds[k].bit_size); |
| 7122 |
res = mpz_popcount (x); |
| 7123 |
mpz_clear (x); |
| 7124 |
} |
| 7125 |
|
| 7126 |
return gfc_get_int_expr (gfc_default_integer_kind, &e->where, res); |
| 7127 |
} |
| 7128 |
|
| 7129 |
|
| 7130 |
gfc_expr * |
| 7131 |
gfc_simplify_poppar (gfc_expr *e) |
| 7132 |
{ |
| 7133 |
gfc_expr *popcnt; |
| 7134 |
int i; |
| 7135 |
|
| 7136 |
if (e->expr_type != EXPR_CONSTANT) |
| 7137 |
return NULL; |
| 7138 |
|
| 7139 |
popcnt = gfc_simplify_popcnt (e); |
| 7140 |
gcc_assert (popcnt); |
| 7141 |
|
| 7142 |
bool fail = gfc_extract_int (popcnt, &i); |
| 7143 |
gcc_assert (!fail); |
| 7144 |
|
| 7145 |
return gfc_get_int_expr (gfc_default_integer_kind, &e->where, i % 2); |
| 7146 |
} |
| 7147 |
|
| 7148 |
|
| 7149 |
gfc_expr * |
| 7150 |
gfc_simplify_precision (gfc_expr *e) |
| 7151 |
{ |
| 7152 |
int i = gfc_validate_kind (e->ts.type, e->ts.kind, false); |
| 7153 |
return gfc_get_int_expr (gfc_default_integer_kind, &e->where, |
| 7154 |
gfc_real_kinds[i].precision); |
| 7155 |
} |
| 7156 |
|
| 7157 |
|
| 7158 |
gfc_expr * |
| 7159 |
gfc_simplify_product (gfc_expr *array, gfc_expr *dim, gfc_expr *mask) |
| 7160 |
{ |
| 7161 |
return simplify_transformation (array, dim, mask, 1, gfc_multiply); |
| 7162 |
} |
| 7163 |
|
| 7164 |
|
| 7165 |
gfc_expr * |
| 7166 |
gfc_simplify_radix (gfc_expr *e) |
| 7167 |
{ |
| 7168 |
int i; |
| 7169 |
i = gfc_validate_kind (e->ts.type, e->ts.kind, false); |
| 7170 |
|
| 7171 |
switch (e->ts.type) |
| 7172 |
{ |
| 7173 |
case BT_INTEGER: |
| 7174 |
i = gfc_integer_kinds[i].radix; |
| 7175 |
break; |
| 7176 |
|
| 7177 |
case BT_REAL: |
| 7178 |
i = gfc_real_kinds[i].radix; |
| 7179 |
break; |
| 7180 |
|
| 7181 |
default: |
| 7182 |
gcc_unreachable (); |
| 7183 |
} |
| 7184 |
|
| 7185 |
return gfc_get_int_expr (gfc_default_integer_kind, &e->where, i); |
| 7186 |
} |
| 7187 |
|
| 7188 |
|
| 7189 |
gfc_expr * |
| 7190 |
gfc_simplify_range (gfc_expr *e) |
| 7191 |
{ |
| 7192 |
int i; |
| 7193 |
i = gfc_validate_kind (e->ts.type, e->ts.kind, false); |
| 7194 |
|
| 7195 |
switch (e->ts.type) |
| 7196 |
{ |
| 7197 |
case BT_INTEGER: |
| 7198 |
i = gfc_integer_kinds[i].range; |
| 7199 |
break; |
| 7200 |
|
| 7201 |
case BT_UNSIGNED: |
| 7202 |
i = gfc_unsigned_kinds[i].range; |
| 7203 |
break; |
| 7204 |
|
| 7205 |
case BT_REAL: |
| 7206 |
case BT_COMPLEX: |
| 7207 |
i = gfc_real_kinds[i].range; |
| 7208 |
break; |
| 7209 |
|
| 7210 |
default: |
| 7211 |
gcc_unreachable (); |
| 7212 |
} |
| 7213 |
|
| 7214 |
return gfc_get_int_expr (gfc_default_integer_kind, &e->where, i); |
| 7215 |
} |
| 7216 |
|
| 7217 |
|
| 7218 |
gfc_expr * |
| 7219 |
gfc_simplify_rank (gfc_expr *e) |
| 7220 |
{ |
| 7221 |
/* Assumed rank. */ |
| 7222 |
if (e->rank == -1) |
| 7223 |
return NULL; |
| 7224 |
|
| 7225 |
return gfc_get_int_expr (gfc_default_integer_kind, &e->where, e->rank); |
| 7226 |
} |
| 7227 |
|
| 7228 |
|
| 7229 |
gfc_expr * |
| 7230 |
gfc_simplify_real (gfc_expr *e, gfc_expr *k) |
| 7231 |
{ |
| 7232 |
gfc_expr *result = NULL; |
| 7233 |
int kind, tmp1, tmp2; |
| 7234 |
|
| 7235 |
/* Convert BOZ to real, and return without range checking. */ |
| 7236 |
if (e->ts.type == BT_BOZ) |
| 7237 |
{ |
| 7238 |
/* Determine kind for conversion of the BOZ. */ |
| 7239 |
if (k) |
| 7240 |
gfc_extract_int (k, &kind); |
| 7241 |
else |
| 7242 |
kind = gfc_default_real_kind; |
| 7243 |
|
| 7244 |
if (!gfc_boz2real (e, kind)) |
| 7245 |
return NULL; |
| 7246 |
result = gfc_copy_expr (e); |
| 7247 |
return result; |
| 7248 |
} |
| 7249 |
|
| 7250 |
if (e->ts.type == BT_COMPLEX) |
| 7251 |
kind = get_kind (BT_REAL, k, "REAL", e->ts.kind); |
| 7252 |
else |
| 7253 |
kind = get_kind (BT_REAL, k, "REAL", gfc_default_real_kind); |
| 7254 |
|
| 7255 |
if (kind == -1) |
| 7256 |
return &gfc_bad_expr; |
| 7257 |
|
| 7258 |
if (e->expr_type != EXPR_CONSTANT) |
| 7259 |
return NULL; |
| 7260 |
|
| 7261 |
/* For explicit conversion, turn off -Wconversion and -Wconversion-extra |
| 7262 |
warnings. */ |
| 7263 |
tmp1 = warn_conversion; |
| 7264 |
tmp2 = warn_conversion_extra; |
| 7265 |
warn_conversion = warn_conversion_extra = 0; |
| 7266 |
|
| 7267 |
result = gfc_convert_constant (e, BT_REAL, kind); |
| 7268 |
|
| 7269 |
warn_conversion = tmp1; |
| 7270 |
warn_conversion_extra = tmp2; |
| 7271 |
|
| 7272 |
if (result == &gfc_bad_expr) |
| 7273 |
return &gfc_bad_expr; |
| 7274 |
|
| 7275 |
return range_check (result, "REAL"); |
| 7276 |
} |
| 7277 |
|
| 7278 |
|
| 7279 |
gfc_expr * |
| 7280 |
gfc_simplify_realpart (gfc_expr *e) |
| 7281 |
{ |
| 7282 |
gfc_expr *result; |
| 7283 |
|
| 7284 |
if (e->expr_type != EXPR_CONSTANT) |
| 7285 |
return NULL; |
| 7286 |
|
| 7287 |
result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where); |
| 7288 |
mpc_real (result->value.real, e->value.complex, GFC_RND_MODE); |
| 7289 |
|
| 7290 |
return range_check (result, "REALPART"); |
| 7291 |
} |
| 7292 |
|
| 7293 |
gfc_expr * |
| 7294 |
gfc_simplify_repeat (gfc_expr *e, gfc_expr *n) |
| 7295 |
{ |
| 7296 |
gfc_expr *result; |
| 7297 |
gfc_charlen_t len; |
| 7298 |
mpz_t ncopies; |
| 7299 |
bool have_length = false; |
| 7300 |
|
| 7301 |
/* If NCOPIES isn't a constant, there's nothing we can do. */ |
| 7302 |
if (n->expr_type != EXPR_CONSTANT) |
| 7303 |
return NULL; |
| 7304 |
|
| 7305 |
/* If NCOPIES is negative, it's an error. */ |
| 7306 |
if (mpz_sgn (n->value.integer) < 0) |
| 7307 |
{ |
| 7308 |
gfc_error ("Argument NCOPIES of REPEAT intrinsic is negative at %L", |
| 7309 |
&n->where); |
| 7310 |
return &gfc_bad_expr; |
| 7311 |
} |
| 7312 |
|
| 7313 |
/* If we don't know the character length, we can do no more. */ |
| 7314 |
if (e->ts.u.cl && e->ts.u.cl->length |
| 7315 |
&& e->ts.u.cl->length->expr_type == EXPR_CONSTANT) |
| 7316 |
{ |
| 7317 |
len = gfc_mpz_get_hwi (e->ts.u.cl->length->value.integer); |
| 7318 |
have_length = true; |
| 7319 |
} |
| 7320 |
else if (e->expr_type == EXPR_CONSTANT |
| 7321 |
&& (e->ts.u.cl == NULL || e->ts.u.cl->length == NULL)) |
| 7322 |
{ |
| 7323 |
len = e->value.character.length; |
| 7324 |
} |
| 7325 |
else |
| 7326 |
return NULL; |
| 7327 |
|
| 7328 |
/* If the source length is 0, any value of NCOPIES is valid |
| 7329 |
and everything behaves as if NCOPIES == 0. */ |
| 7330 |
mpz_init (ncopies); |
| 7331 |
if (len == 0) |
| 7332 |
mpz_set_ui (ncopies, 0); |
| 7333 |
else |
| 7334 |
mpz_set (ncopies, n->value.integer); |
| 7335 |
|
| 7336 |
/* Check that NCOPIES isn't too large. */ |
| 7337 |
if (len) |
| 7338 |
{ |
| 7339 |
mpz_t max, mlen; |
| 7340 |
int i; |
| 7341 |
|
| 7342 |
/* Compute the maximum value allowed for NCOPIES: huge(cl) / len. */ |
| 7343 |
mpz_init (max); |
| 7344 |
i = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false); |
| 7345 |
|
| 7346 |
if (have_length) |
| 7347 |
{ |
| 7348 |
mpz_tdiv_q (max, gfc_integer_kinds[i].huge, |
| 7349 |
e->ts.u.cl->length->value.integer); |
| 7350 |
} |
| 7351 |
else |
| 7352 |
{ |
| 7353 |
mpz_init (mlen); |
| 7354 |
gfc_mpz_set_hwi (mlen, len); |
| 7355 |
mpz_tdiv_q (max, gfc_integer_kinds[i].huge, mlen); |
| 7356 |
mpz_clear (mlen); |
| 7357 |
} |
| 7358 |
|
| 7359 |
/* The check itself. */ |
| 7360 |
if (mpz_cmp (ncopies, max) > 0) |
| 7361 |
{ |
| 7362 |
mpz_clear (max); |
| 7363 |
mpz_clear (ncopies); |
| 7364 |
gfc_error ("Argument NCOPIES of REPEAT intrinsic is too large at %L", |
| 7365 |
&n->where); |
| 7366 |
return &gfc_bad_expr; |
| 7367 |
} |
| 7368 |
|
| 7369 |
mpz_clear (max); |
| 7370 |
} |
| 7371 |
mpz_clear (ncopies); |
| 7372 |
|
| 7373 |
/* For further simplification, we need the character string to be |
| 7374 |
constant. */ |
| 7375 |
if (e->expr_type != EXPR_CONSTANT) |
| 7376 |
return NULL; |
| 7377 |
|
| 7378 |
HOST_WIDE_INT ncop; |
| 7379 |
if (len || |
| 7380 |
(e->ts.u.cl->length && |
| 7381 |
mpz_sgn (e->ts.u.cl->length->value.integer) != 0)) |
| 7382 |
{ |
| 7383 |
bool fail = gfc_extract_hwi (n, &ncop); |
| 7384 |
gcc_assert (!fail); |
| 7385 |
} |
| 7386 |
else |
| 7387 |
ncop = 0; |
| 7388 |
|
| 7389 |
if (ncop == 0) |
| 7390 |
return gfc_get_character_expr (e->ts.kind, &e->where, NULL, 0); |
| 7391 |
|
| 7392 |
len = e->value.character.length; |
| 7393 |
gfc_charlen_t nlen = ncop * len; |
| 7394 |
|
| 7395 |
/* Here's a semi-arbitrary limit. If the string is longer than 1 GB |
| 7396 |
(2**28 elements * 4 bytes (wide chars) per element) defer to |
| 7397 |
runtime instead of consuming (unbounded) memory and CPU at |
| 7398 |
compile time. */ |
| 7399 |
if (nlen > 268435456) |
| 7400 |
{ |
| 7401 |
gfc_warning_now (0, "Evaluation of string longer than 2**28 at %L" |
| 7402 |
" deferred to runtime, expect bugs", &e->where); |
| 7403 |
return NULL; |
| 7404 |
} |
| 7405 |
|
| 7406 |
result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, nlen); |
| 7407 |
for (size_t i = 0; i < (size_t) ncop; i++) |
| 7408 |
for (size_t j = 0; j < (size_t) len; j++) |
| 7409 |
result->value.character.string[j+i*len]= e->value.character.string[j]; |
| 7410 |
|
| 7411 |
result->value.character.string[nlen] = '\0'; /* For debugger */ |
| 7412 |
return result; |
| 7413 |
} |
| 7414 |
|
| 7415 |
|
| 7416 |
/* This one is a bear, but mainly has to do with shuffling elements. */ |
| 7417 |
|
| 7418 |
gfc_expr * |
| 7419 |
gfc_simplify_reshape (gfc_expr *source, gfc_expr *shape_exp, |
| 7420 |
gfc_expr *pad, gfc_expr *order_exp) |
| 7421 |
{ |
| 7422 |
int order[GFC_MAX_DIMENSIONS], shape[GFC_MAX_DIMENSIONS]; |
| 7423 |
int i, rank, npad, x[GFC_MAX_DIMENSIONS]; |
| 7424 |
mpz_t index, size; |
| 7425 |
unsigned long j; |
| 7426 |
size_t nsource; |
| 7427 |
gfc_expr *e, *result; |
| 7428 |
bool zerosize = false; |
| 7429 |
|
| 7430 |
/* Check that argument expression types are OK. */ |
| 7431 |
if (!is_constant_array_expr (source) |
| 7432 |
|| !is_constant_array_expr (shape_exp) |
| 7433 |
|| !is_constant_array_expr (pad) |
| 7434 |
|| !is_constant_array_expr (order_exp)) |
| 7435 |
return NULL; |
| 7436 |
|
| 7437 |
if (source->shape == NULL) |
| 7438 |
return NULL; |
| 7439 |
|
| 7440 |
/* Proceed with simplification, unpacking the array. */ |
| 7441 |
|
| 7442 |
mpz_init (index); |
| 7443 |
rank = 0; |
| 7444 |
|
| 7445 |
for (i = 0; i < GFC_MAX_DIMENSIONS; i++) |
| 7446 |
x[i] = 0; |
| 7447 |
|
| 7448 |
for (;;) |
| 7449 |
{ |
| 7450 |
e = gfc_constructor_lookup_expr (shape_exp->value.constructor, rank); |
| 7451 |
if (e == NULL) |
| 7452 |
break; |
| 7453 |
|
| 7454 |
gfc_extract_int (e, &shape[rank]); |
| 7455 |
|
| 7456 |
gcc_assert (rank >= 0 && rank < GFC_MAX_DIMENSIONS); |
| 7457 |
if (shape[rank] < 0) |
| 7458 |
{ |
| 7459 |
gfc_error ("The SHAPE array for the RESHAPE intrinsic at %L has a " |
| 7460 |
"negative value %d for dimension %d", |
| 7461 |
&shape_exp->where, shape[rank], rank+1); |
| 7462 |
mpz_clear (index); |
| 7463 |
return &gfc_bad_expr; |
| 7464 |
} |
| 7465 |
|
| 7466 |
rank++; |
| 7467 |
} |
| 7468 |
|
| 7469 |
gcc_assert (rank > 0); |
| 7470 |
|
| 7471 |
/* Now unpack the order array if present. */ |
| 7472 |
if (order_exp == NULL) |
| 7473 |
{ |
| 7474 |
for (i = 0; i < rank; i++) |
| 7475 |
order[i] = i; |
| 7476 |
} |
| 7477 |
else |
| 7478 |
{ |
| 7479 |
mpz_t size; |
| 7480 |
int order_size, shape_size; |
| 7481 |
|
| 7482 |
if (order_exp->rank != shape_exp->rank) |
| 7483 |
{ |
| 7484 |
gfc_error ("Shapes of ORDER at %L and SHAPE at %L are different", |
| 7485 |
&order_exp->where, &shape_exp->where); |
| 7486 |
mpz_clear (index); |
| 7487 |
return &gfc_bad_expr; |
| 7488 |
} |
| 7489 |
|
| 7490 |
gfc_array_size (shape_exp, &size); |
| 7491 |
shape_size = mpz_get_ui (size); |
| 7492 |
mpz_clear (size); |
| 7493 |
gfc_array_size (order_exp, &size); |
| 7494 |
order_size = mpz_get_ui (size); |
| 7495 |
mpz_clear (size); |
| 7496 |
if (order_size != shape_size) |
| 7497 |
{ |
| 7498 |
gfc_error ("Sizes of ORDER at %L and SHAPE at %L are different", |
| 7499 |
&order_exp->where, &shape_exp->where); |
| 7500 |
mpz_clear (index); |
| 7501 |
return &gfc_bad_expr; |
| 7502 |
} |
| 7503 |
|
| 7504 |
for (i = 0; i < rank; i++) |
| 7505 |
{ |
| 7506 |
e = gfc_constructor_lookup_expr (order_exp->value.constructor, i); |
| 7507 |
gcc_assert (e); |
| 7508 |
|
| 7509 |
gfc_extract_int (e, &order[i]); |
| 7510 |
|
| 7511 |
if (order[i] < 1 || order[i] > rank) |
| 7512 |
{ |
| 7513 |
gfc_error ("Element with a value of %d in ORDER at %L must be " |
| 7514 |
"in the range [1, ..., %d] for the RESHAPE intrinsic " |
| 7515 |
"near %L", order[i], &order_exp->where, rank, |
| 7516 |
&shape_exp->where); |
| 7517 |
mpz_clear (index); |
| 7518 |
return &gfc_bad_expr; |
| 7519 |
} |
| 7520 |
|
| 7521 |
order[i]--; |
| 7522 |
if (x[order[i]] != 0) |
| 7523 |
{ |
| 7524 |
gfc_error ("ORDER at %L is not a permutation of the size of " |
| 7525 |
"SHAPE at %L", &order_exp->where, &shape_exp->where); |
| 7526 |
mpz_clear (index); |
| 7527 |
return &gfc_bad_expr; |
| 7528 |
} |
| 7529 |
x[order[i]] = 1; |
| 7530 |
} |
| 7531 |
} |
| 7532 |
|
| 7533 |
/* Count the elements in the source and padding arrays. */ |
| 7534 |
|
| 7535 |
npad = 0; |
| 7536 |
if (pad != NULL) |
| 7537 |
{ |
| 7538 |
gfc_array_size (pad, &size); |
| 7539 |
npad = mpz_get_ui (size); |
| 7540 |
mpz_clear (size); |
| 7541 |
} |
| 7542 |
|
| 7543 |
gfc_array_size (source, &size); |
| 7544 |
nsource = mpz_get_ui (size); |
| 7545 |
mpz_clear (size); |
| 7546 |
|
| 7547 |
/* If it weren't for that pesky permutation we could just loop |
| 7548 |
through the source and round out any shortage with pad elements. |
| 7549 |
But no, someone just had to have the compiler do something the |
| 7550 |
user should be doing. */ |
| 7551 |
|
| 7552 |
for (i = 0; i < rank; i++) |
| 7553 |
x[i] = 0; |
| 7554 |
|
| 7555 |
result = gfc_get_array_expr (source->ts.type, source->ts.kind, |
| 7556 |
&source->where); |
| 7557 |
if (source->ts.type == BT_DERIVED) |
| 7558 |
result->ts.u.derived = source->ts.u.derived; |
| 7559 |
if (source->ts.type == BT_CHARACTER && result->ts.u.cl == NULL) |
| 7560 |
result->ts = source->ts; |
| 7561 |
result->rank = rank; |
| 7562 |
result->shape = gfc_get_shape (rank); |
| 7563 |
for (i = 0; i < rank; i++) |
| 7564 |
{ |
| 7565 |
mpz_init_set_ui (result->shape[i], shape[i]); |
| 7566 |
if (shape[i] == 0) |
| 7567 |
zerosize = true; |
| 7568 |
} |
| 7569 |
|
| 7570 |
if (zerosize) |
| 7571 |
goto sizezero; |
| 7572 |
|
| 7573 |
while (nsource > 0 || npad > 0) |
| 7574 |
{ |
| 7575 |
/* Figure out which element to extract. */ |
| 7576 |
mpz_set_ui (index, 0); |
| 7577 |
|
| 7578 |
for (i = rank - 1; i >= 0; i--) |
| 7579 |
{ |
| 7580 |
mpz_add_ui (index, index, x[order[i]]); |
| 7581 |
if (i != 0) |
| 7582 |
mpz_mul_ui (index, index, shape[order[i - 1]]); |
| 7583 |
} |
| 7584 |
|
| 7585 |
if (mpz_cmp_ui (index, INT_MAX) > 0) |
| 7586 |
gfc_internal_error ("Reshaped array too large at %C"); |
| 7587 |
|
| 7588 |
j = mpz_get_ui (index); |
| 7589 |
|
| 7590 |
if (j < nsource) |
| 7591 |
e = gfc_constructor_lookup_expr (source->value.constructor, j); |
| 7592 |
else |
| 7593 |
{ |
| 7594 |
if (npad <= 0) |
| 7595 |
{ |
| 7596 |
mpz_clear (index); |
| 7597 |
if (pad == NULL) |
| 7598 |
gfc_error ("Without padding, there are not enough elements " |
| 7599 |
"in the intrinsic RESHAPE source at %L to match " |
| 7600 |
"the shape", &source->where); |
| 7601 |
gfc_free_expr (result); |
| 7602 |
return NULL; |
| 7603 |
} |
| 7604 |
j = j - nsource; |
| 7605 |
j = j % npad; |
| 7606 |
e = gfc_constructor_lookup_expr (pad->value.constructor, j); |
| 7607 |
} |
| 7608 |
gcc_assert (e); |
| 7609 |
|
| 7610 |
gfc_constructor_append_expr (&result->value.constructor, |
| 7611 |
gfc_copy_expr (e), &e->where); |
| 7612 |
|
| 7613 |
/* Calculate the next element. */ |
| 7614 |
i = 0; |
| 7615 |
|
| 7616 |
inc: |
| 7617 |
if (++x[i] < shape[i]) |
| 7618 |
continue; |
| 7619 |
x[i++] = 0; |
| 7620 |
if (i < rank) |
| 7621 |
goto inc; |
| 7622 |
|
| 7623 |
break; |
| 7624 |
} |
| 7625 |
|
| 7626 |
sizezero: |
| 7627 |
|
| 7628 |
mpz_clear (index); |
| 7629 |
|
| 7630 |
return result; |
| 7631 |
} |
| 7632 |
|
| 7633 |
|
| 7634 |
gfc_expr * |
| 7635 |
gfc_simplify_rrspacing (gfc_expr *x) |
| 7636 |
{ |
| 7637 |
gfc_expr *result; |
| 7638 |
int i; |
| 7639 |
long int e, p; |
| 7640 |
|
| 7641 |
if (x->expr_type != EXPR_CONSTANT) |
| 7642 |
return NULL; |
| 7643 |
|
| 7644 |
i = gfc_validate_kind (x->ts.type, x->ts.kind, false); |
| 7645 |
|
| 7646 |
result = gfc_get_constant_expr (BT_REAL, x->ts.kind, &x->where); |
| 7647 |
|
| 7648 |
/* RRSPACING(+/- 0.0) = 0.0 */ |
| 7649 |
if (mpfr_zero_p (x->value.real)) |
| 7650 |
{ |
| 7651 |
mpfr_set_ui (result->value.real, 0, GFC_RND_MODE); |
| 7652 |
return result; |
| 7653 |
} |
| 7654 |
|
| 7655 |
/* RRSPACING(inf) = NaN */ |
| 7656 |
if (mpfr_inf_p (x->value.real)) |
| 7657 |
{ |
| 7658 |
mpfr_set_nan (result->value.real); |
| 7659 |
return result; |
| 7660 |
} |
| 7661 |
|
| 7662 |
/* RRSPACING(NaN) = same NaN */ |
| 7663 |
if (mpfr_nan_p (x->value.real)) |
| 7664 |
{ |
| 7665 |
mpfr_set (result->value.real, x->value.real, GFC_RND_MODE); |
| 7666 |
return result; |
| 7667 |
} |
| 7668 |
|
| 7669 |
/* | x * 2**(-e) | * 2**p. */ |
| 7670 |
mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE); |
| 7671 |
e = - (long int) mpfr_get_exp (x->value.real); |
| 7672 |
mpfr_mul_2si (result->value.real, result->value.real, e, GFC_RND_MODE); |
| 7673 |
|
| 7674 |
p = (long int) gfc_real_kinds[i].digits; |
| 7675 |
mpfr_mul_2si (result->value.real, result->value.real, p, GFC_RND_MODE); |
| 7676 |
|
| 7677 |
return range_check (result, "RRSPACING"); |
| 7678 |
} |
| 7679 |
|
| 7680 |
|
| 7681 |
gfc_expr * |
| 7682 |
gfc_simplify_scale (gfc_expr *x, gfc_expr *i) |
| 7683 |
{ |
| 7684 |
int k, neg_flag, power, exp_range; |
| 7685 |
mpfr_t scale, radix; |
| 7686 |
gfc_expr *result; |
| 7687 |
|
| 7688 |
if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT) |
| 7689 |
return NULL; |
| 7690 |
|
| 7691 |
result = gfc_get_constant_expr (BT_REAL, x->ts.kind, &x->where); |
| 7692 |
|
| 7693 |
if (mpfr_zero_p (x->value.real)) |
| 7694 |
{ |
| 7695 |
mpfr_set_ui (result->value.real, 0, GFC_RND_MODE); |
| 7696 |
return result; |
| 7697 |
} |
| 7698 |
|
| 7699 |
k = gfc_validate_kind (BT_REAL, x->ts.kind, false); |
| 7700 |
|
| 7701 |
exp_range = gfc_real_kinds[k].max_exponent - gfc_real_kinds[k].min_exponent; |
| 7702 |
|
| 7703 |
/* This check filters out values of i that would overflow an int. */ |
| 7704 |
if (mpz_cmp_si (i->value.integer, exp_range + 2) > 0 |
| 7705 |
|| mpz_cmp_si (i->value.integer, -exp_range - 2) < 0) |
| 7706 |
{ |
| 7707 |
gfc_error ("Result of SCALE overflows its kind at %L", &result->where); |
| 7708 |
gfc_free_expr (result); |
| 7709 |
return &gfc_bad_expr; |
| 7710 |
} |
| 7711 |
|
| 7712 |
/* Compute scale = radix ** power. */ |
| 7713 |
power = mpz_get_si (i->value.integer); |
| 7714 |
|
| 7715 |
if (power >= 0) |
| 7716 |
neg_flag = 0; |
| 7717 |
else |
| 7718 |
{ |
| 7719 |
neg_flag = 1; |
| 7720 |
power = -power; |
| 7721 |
} |
| 7722 |
|
| 7723 |
gfc_set_model_kind (x->ts.kind); |
| 7724 |
mpfr_init (scale); |
| 7725 |
mpfr_init (radix); |
| 7726 |
mpfr_set_ui (radix, gfc_real_kinds[k].radix, GFC_RND_MODE); |
| 7727 |
mpfr_pow_ui (scale, radix, power, GFC_RND_MODE); |
| 7728 |
|
| 7729 |
if (neg_flag) |
| 7730 |
mpfr_div (result->value.real, x->value.real, scale, GFC_RND_MODE); |
| 7731 |
else |
| 7732 |
mpfr_mul (result->value.real, x->value.real, scale, GFC_RND_MODE); |
| 7733 |
|
| 7734 |
mpfr_clears (scale, radix, NULL); |
| 7735 |
|
| 7736 |
return range_check (result, "SCALE"); |
| 7737 |
} |
| 7738 |
|
| 7739 |
|
| 7740 |
/* Variants of strspn and strcspn that operate on wide characters. */ |
| 7741 |
|
| 7742 |
static size_t |
| 7743 |
wide_strspn (const gfc_char_t *s1, const gfc_char_t *s2) |
| 7744 |
{ |
| 7745 |
size_t i = 0; |
| 7746 |
const gfc_char_t *c; |
| 7747 |
|
| 7748 |
while (s1[i]) |
| 7749 |
{ |
| 7750 |
for (c = s2; *c; c++) |
| 7751 |
{ |
| 7752 |
if (s1[i] == *c) |
| 7753 |
break; |
| 7754 |
} |
| 7755 |
if (*c == '\0') |
| 7756 |
break; |
| 7757 |
i++; |
| 7758 |
} |
| 7759 |
|
| 7760 |
return i; |
| 7761 |
} |
| 7762 |
|
| 7763 |
static size_t |
| 7764 |
wide_strcspn (const gfc_char_t *s1, const gfc_char_t *s2) |
| 7765 |
{ |
| 7766 |
size_t i = 0; |
| 7767 |
const gfc_char_t *c; |
| 7768 |
|
| 7769 |
while (s1[i]) |
| 7770 |
{ |
| 7771 |
for (c = s2; *c; c++) |
| 7772 |
{ |
| 7773 |
if (s1[i] == *c) |
| 7774 |
break; |
| 7775 |
} |
| 7776 |
if (*c) |
| 7777 |
break; |
| 7778 |
i++; |
| 7779 |
} |
| 7780 |
|
| 7781 |
return i; |
| 7782 |
} |
| 7783 |
|
| 7784 |
|
| 7785 |
gfc_expr * |
| 7786 |
gfc_simplify_scan (gfc_expr *e, gfc_expr *c, gfc_expr *b, gfc_expr *kind) |
| 7787 |
{ |
| 7788 |
gfc_expr *result; |
| 7789 |
int back; |
| 7790 |
size_t i; |
| 7791 |
size_t indx, len, lenc; |
| 7792 |
int k = get_kind (BT_INTEGER, kind, "SCAN", gfc_default_integer_kind); |
| 7793 |
|
| 7794 |
if (k == -1) |
| 7795 |
return &gfc_bad_expr; |
| 7796 |
|
| 7797 |
if (e->expr_type != EXPR_CONSTANT || c->expr_type != EXPR_CONSTANT |
| 7798 |
|| ( b != NULL && b->expr_type != EXPR_CONSTANT)) |
| 7799 |
return NULL; |
| 7800 |
|
| 7801 |
if (b != NULL && b->value.logical != 0) |
| 7802 |
back = 1; |
| 7803 |
else |
| 7804 |
back = 0; |
| 7805 |
|
| 7806 |
len = e->value.character.length; |
| 7807 |
lenc = c->value.character.length; |
| 7808 |
|
| 7809 |
if (len == 0 || lenc == 0) |
| 7810 |
{ |
| 7811 |
indx = 0; |
| 7812 |
} |
| 7813 |
else |
| 7814 |
{ |
| 7815 |
if (back == 0) |
| 7816 |
{ |
| 7817 |
indx = wide_strcspn (e->value.character.string, |
| 7818 |
c->value.character.string) + 1; |
| 7819 |
if (indx > len) |
| 7820 |
indx = 0; |
| 7821 |
} |
| 7822 |
else |
| 7823 |
for (indx = len; indx > 0; indx--) |
| 7824 |
{ |
| 7825 |
for (i = 0; i < lenc; i++) |
| 7826 |
{ |
| 7827 |
if (c->value.character.string[i] |
| 7828 |
== e->value.character.string[indx - 1]) |
| 7829 |
break; |
| 7830 |
} |
| 7831 |
if (i < lenc) |
| 7832 |
break; |
| 7833 |
} |
| 7834 |
} |
| 7835 |
|
| 7836 |
result = gfc_get_int_expr (k, &e->where, indx); |
| 7837 |
return range_check (result, "SCAN"); |
| 7838 |
} |
| 7839 |
|
| 7840 |
|
| 7841 |
gfc_expr * |
| 7842 |
gfc_simplify_selected_char_kind (gfc_expr *e) |
| 7843 |
{ |
| 7844 |
int kind; |
| 7845 |
|
| 7846 |
if (e->expr_type != EXPR_CONSTANT) |
| 7847 |
return NULL; |
| 7848 |
|
| 7849 |
if (gfc_compare_with_Cstring (e, "ascii", false) == 0 |
| 7850 |
|| gfc_compare_with_Cstring (e, "default", false) == 0) |
| 7851 |
kind = 1; |
| 7852 |
else if (gfc_compare_with_Cstring (e, "iso_10646", false) == 0) |
| 7853 |
kind = 4; |
| 7854 |
else |
| 7855 |
kind = -1; |
| 7856 |
|
| 7857 |
return gfc_get_int_expr (gfc_default_integer_kind, &e->where, kind); |
| 7858 |
} |
| 7859 |
|
| 7860 |
|
| 7861 |
gfc_expr * |
| 7862 |
gfc_simplify_selected_int_kind (gfc_expr *e) |
| 7863 |
{ |
| 7864 |
int i, kind, range; |
| 7865 |
|
| 7866 |
if (e->expr_type != EXPR_CONSTANT || gfc_extract_int (e, &range)) |
| 7867 |
return NULL; |
| 7868 |
|
| 7869 |
kind = INT_MAX; |
| 7870 |
|
| 7871 |
for (i = 0; gfc_integer_kinds[i].kind != 0; i++) |
| 7872 |
if (gfc_integer_kinds[i].range >= range |
| 7873 |
&& gfc_integer_kinds[i].kind < kind) |
| 7874 |
kind = gfc_integer_kinds[i].kind; |
| 7875 |
|
| 7876 |
if (kind == INT_MAX) |
| 7877 |
kind = -1; |
| 7878 |
|
| 7879 |
return gfc_get_int_expr (gfc_default_integer_kind, &e->where, kind); |
| 7880 |
} |
| 7881 |
|
| 7882 |
/* Same as above, but with unsigneds. */ |
| 7883 |
|
| 7884 |
gfc_expr * |
| 7885 |
gfc_simplify_selected_unsigned_kind (gfc_expr *e) |
| 7886 |
{ |
| 7887 |
int i, kind, range; |
| 7888 |
|
| 7889 |
if (e->expr_type != EXPR_CONSTANT || gfc_extract_int (e, &range)) |
| 7890 |
return NULL; |
| 7891 |
|
| 7892 |
kind = INT_MAX; |
| 7893 |
|
| 7894 |
for (i = 0; gfc_unsigned_kinds[i].kind != 0; i++) |
| 7895 |
if (gfc_unsigned_kinds[i].range >= range |
| 7896 |
&& gfc_unsigned_kinds[i].kind < kind) |
| 7897 |
kind = gfc_unsigned_kinds[i].kind; |
| 7898 |
|
| 7899 |
if (kind == INT_MAX) |
| 7900 |
kind = -1; |
| 7901 |
|
| 7902 |
return gfc_get_int_expr (gfc_default_integer_kind, &e->where, kind); |
| 7903 |
} |
| 7904 |
|
| 7905 |
|
| 7906 |
gfc_expr * |
| 7907 |
gfc_simplify_selected_logical_kind (gfc_expr *e) |
| 7908 |
{ |
| 7909 |
int i, kind, bits; |
| 7910 |
|
| 7911 |
if (e->expr_type != EXPR_CONSTANT || gfc_extract_int (e, &bits)) |
| 7912 |
return NULL; |
| 7913 |
|
| 7914 |
kind = INT_MAX; |
| 7915 |
|
| 7916 |
for (i = 0; gfc_logical_kinds[i].kind != 0; i++) |
| 7917 |
if (gfc_logical_kinds[i].bit_size >= bits |
| 7918 |
&& gfc_logical_kinds[i].kind < kind) |
| 7919 |
kind = gfc_logical_kinds[i].kind; |
| 7920 |
|
| 7921 |
if (kind == INT_MAX) |
| 7922 |
kind = -1; |
| 7923 |
|
| 7924 |
return gfc_get_int_expr (gfc_default_integer_kind, &e->where, kind); |
| 7925 |
} |
| 7926 |
|
| 7927 |
|
| 7928 |
gfc_expr * |
| 7929 |
gfc_simplify_selected_real_kind (gfc_expr *p, gfc_expr *q, gfc_expr *rdx) |
| 7930 |
{ |
| 7931 |
int range, precision, radix, i, kind, found_precision, found_range, |
| 7932 |
found_radix; |
| 7933 |
locus *loc = &gfc_current_locus; |
| 7934 |
|
| 7935 |
if (p == NULL) |
| 7936 |
precision = 0; |
| 7937 |
else |
| 7938 |
{ |
| 7939 |
if (p->expr_type != EXPR_CONSTANT |
| 7940 |
|| gfc_extract_int (p, &precision)) |
| 7941 |
return NULL; |
| 7942 |
loc = &p->where; |
| 7943 |
} |
| 7944 |
|
| 7945 |
if (q == NULL) |
| 7946 |
range = 0; |
| 7947 |
else |
| 7948 |
{ |
| 7949 |
if (q->expr_type != EXPR_CONSTANT |
| 7950 |
|| gfc_extract_int (q, &range)) |
| 7951 |
return NULL; |
| 7952 |
|
| 7953 |
if (!loc) |
| 7954 |
loc = &q->where; |
| 7955 |
} |
| 7956 |
|
| 7957 |
if (rdx == NULL) |
| 7958 |
radix = 0; |
| 7959 |
else |
| 7960 |
{ |
| 7961 |
if (rdx->expr_type != EXPR_CONSTANT |
| 7962 |
|| gfc_extract_int (rdx, &radix)) |
| 7963 |
return NULL; |
| 7964 |
|
| 7965 |
if (!loc) |
| 7966 |
loc = &rdx->where; |
| 7967 |
} |
| 7968 |
|
| 7969 |
kind = INT_MAX; |
| 7970 |
found_precision = 0; |
| 7971 |
found_range = 0; |
| 7972 |
found_radix = 0; |
| 7973 |
|
| 7974 |
for (i = 0; gfc_real_kinds[i].kind != 0; i++) |
| 7975 |
{ |
| 7976 |
if (gfc_real_kinds[i].precision >= precision) |
| 7977 |
found_precision = 1; |
| 7978 |
|
| 7979 |
if (gfc_real_kinds[i].range >= range) |
| 7980 |
found_range = 1; |
| 7981 |
|
| 7982 |
if (radix == 0 || gfc_real_kinds[i].radix == radix) |
| 7983 |
found_radix = 1; |
| 7984 |
|
| 7985 |
if (gfc_real_kinds[i].precision >= precision |
| 7986 |
&& gfc_real_kinds[i].range >= range |
| 7987 |
&& (radix == 0 || gfc_real_kinds[i].radix == radix) |
| 7988 |
&& gfc_real_kinds[i].kind < kind) |
| 7989 |
kind = gfc_real_kinds[i].kind; |
| 7990 |
} |
| 7991 |
|
| 7992 |
if (kind == INT_MAX) |
| 7993 |
{ |
| 7994 |
if (found_radix && found_range && !found_precision) |
| 7995 |
kind = -1; |
| 7996 |
else if (found_radix && found_precision && !found_range) |
| 7997 |
kind = -2; |
| 7998 |
else if (found_radix && !found_precision && !found_range) |
| 7999 |
kind = -3; |
| 8000 |
else if (found_radix) |
| 8001 |
kind = -4; |
| 8002 |
else |
| 8003 |
kind = -5; |
| 8004 |
} |
| 8005 |
|
| 8006 |
return gfc_get_int_expr (gfc_default_integer_kind, loc, kind); |
| 8007 |
} |
| 8008 |
|
| 8009 |
|
| 8010 |
gfc_expr * |
| 8011 |
gfc_simplify_set_exponent (gfc_expr *x, gfc_expr *i) |
| 8012 |
{ |
| 8013 |
gfc_expr *result; |
| 8014 |
mpfr_t exp, absv, log2, pow2, frac; |
| 8015 |
long exp2; |
| 8016 |
|
| 8017 |
if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT) |
| 8018 |
return NULL; |
| 8019 |
|
| 8020 |
result = gfc_get_constant_expr (BT_REAL, x->ts.kind, &x->where); |
| 8021 |
|
| 8022 |
/* SET_EXPONENT (+/-0.0, I) = +/- 0.0 |
| 8023 |
SET_EXPONENT (NaN) = same NaN */ |
| 8024 |
if (mpfr_zero_p (x->value.real) || mpfr_nan_p (x->value.real)) |
| 8025 |
{ |
| 8026 |
mpfr_set (result->value.real, x->value.real, GFC_RND_MODE); |
| 8027 |
return result; |
| 8028 |
} |
| 8029 |
|
| 8030 |
/* SET_EXPONENT (inf) = NaN */ |
| 8031 |
if (mpfr_inf_p (x->value.real)) |
| 8032 |
{ |
| 8033 |
mpfr_set_nan (result->value.real); |
| 8034 |
return result; |
| 8035 |
} |
| 8036 |
|
| 8037 |
gfc_set_model_kind (x->ts.kind); |
| 8038 |
mpfr_init (absv); |
| 8039 |
mpfr_init (log2); |
| 8040 |
mpfr_init (exp); |
| 8041 |
mpfr_init (pow2); |
| 8042 |
mpfr_init (frac); |
| 8043 |
|
| 8044 |
mpfr_abs (absv, x->value.real, GFC_RND_MODE); |
| 8045 |
mpfr_log2 (log2, absv, GFC_RND_MODE); |
| 8046 |
|
| 8047 |
mpfr_floor (log2, log2); |
| 8048 |
mpfr_add_ui (exp, log2, 1, GFC_RND_MODE); |
| 8049 |
|
| 8050 |
/* Old exponent value, and fraction. */ |
| 8051 |
mpfr_ui_pow (pow2, 2, exp, GFC_RND_MODE); |
| 8052 |
|
| 8053 |
mpfr_div (frac, x->value.real, pow2, GFC_RND_MODE); |
| 8054 |
|
| 8055 |
/* New exponent. */ |
| 8056 |
exp2 = mpz_get_si (i->value.integer); |
| 8057 |
mpfr_mul_2si (result->value.real, frac, exp2, GFC_RND_MODE); |
| 8058 |
|
| 8059 |
mpfr_clears (absv, log2, exp, pow2, frac, NULL); |
| 8060 |
|
| 8061 |
return range_check (result, "SET_EXPONENT"); |
| 8062 |
} |
| 8063 |
|
| 8064 |
|
| 8065 |
gfc_expr * |
| 8066 |
gfc_simplify_shape (gfc_expr *source, gfc_expr *kind) |
| 8067 |
{ |
| 8068 |
mpz_t shape[GFC_MAX_DIMENSIONS]; |
| 8069 |
gfc_expr *result, *e, *f; |
| 8070 |
gfc_array_ref *ar; |
| 8071 |
int n; |
| 8072 |
bool t; |
| 8073 |
int k = get_kind (BT_INTEGER, kind, "SHAPE", gfc_default_integer_kind); |
| 8074 |
|
| 8075 |
if (source->rank == -1) |
| 8076 |
return NULL; |
| 8077 |
|
| 8078 |
result = gfc_get_array_expr (BT_INTEGER, k, &source->where); |
| 8079 |
result->shape = gfc_get_shape (1); |
| 8080 |
mpz_init (result->shape[0]); |
| 8081 |
|
| 8082 |
if (source->rank == 0) |
| 8083 |
return result; |
| 8084 |
|
| 8085 |
if (source->expr_type == EXPR_VARIABLE) |
| 8086 |
{ |
| 8087 |
ar = gfc_find_array_ref (source); |
| 8088 |
t = gfc_array_ref_shape (ar, shape); |
| 8089 |
} |
| 8090 |
else if (source->shape) |
| 8091 |
{ |
| 8092 |
t = true; |
| 8093 |
for (n = 0; n < source->rank; n++) |
| 8094 |
{ |
| 8095 |
mpz_init (shape[n]); |
| 8096 |
mpz_set (shape[n], source->shape[n]); |
| 8097 |
} |
| 8098 |
} |
| 8099 |
else |
| 8100 |
t = false; |
| 8101 |
|
| 8102 |
for (n = 0; n < source->rank; n++) |
| 8103 |
{ |
| 8104 |
e = gfc_get_constant_expr (BT_INTEGER, k, &source->where); |
| 8105 |
|
| 8106 |
if (t) |
| 8107 |
mpz_set (e->value.integer, shape[n]); |
| 8108 |
else |
| 8109 |
{ |
| 8110 |
mpz_set_ui (e->value.integer, n + 1); |
| 8111 |
|
| 8112 |
f = simplify_size (source, e, k); |
| 8113 |
gfc_free_expr (e); |
| 8114 |
if (f == NULL) |
| 8115 |
{ |
| 8116 |
gfc_free_expr (result); |
| 8117 |
return NULL; |
| 8118 |
} |
| 8119 |
else |
| 8120 |
e = f; |
| 8121 |
} |
| 8122 |
|
| 8123 |
if (e == &gfc_bad_expr || range_check (e, "SHAPE") == &gfc_bad_expr) |
| 8124 |
{ |
| 8125 |
gfc_free_expr (result); |
| 8126 |
if (t) |
| 8127 |
gfc_clear_shape (shape, source->rank); |
| 8128 |
return &gfc_bad_expr; |
| 8129 |
} |
| 8130 |
|
| 8131 |
gfc_constructor_append_expr (&result->value.constructor, e, NULL); |
| 8132 |
} |
| 8133 |
|
| 8134 |
if (t) |
| 8135 |
gfc_clear_shape (shape, source->rank); |
| 8136 |
|
| 8137 |
mpz_set_si (result->shape[0], source->rank); |
| 8138 |
|
| 8139 |
return result; |
| 8140 |
} |
| 8141 |
|
| 8142 |
|
| 8143 |
static gfc_expr * |
| 8144 |
simplify_size (gfc_expr *array, gfc_expr *dim, int k) |
| 8145 |
{ |
| 8146 |
mpz_t size; |
| 8147 |
gfc_expr *return_value; |
| 8148 |
int d; |
| 8149 |
gfc_ref *ref; |
| 8150 |
|
| 8151 |
/* For unary operations, the size of the result is given by the size |
| 8152 |
of the operand. For binary ones, it's the size of the first operand |
| 8153 |
unless it is scalar, then it is the size of the second. */ |
| 8154 |
if (array->expr_type == EXPR_OP && !array->value.op.uop) |
| 8155 |
{ |
| 8156 |
gfc_expr* replacement; |
| 8157 |
gfc_expr* simplified; |
| 8158 |
|
| 8159 |
switch (array->value.op.op) |
| 8160 |
{ |
| 8161 |
/* Unary operations. */ |
| 8162 |
case INTRINSIC_NOT: |
| 8163 |
case INTRINSIC_UPLUS: |
| 8164 |
case INTRINSIC_UMINUS: |
| 8165 |
case INTRINSIC_PARENTHESES: |
| 8166 |
replacement = array->value.op.op1; |
| 8167 |
break; |
| 8168 |
|
| 8169 |
/* Binary operations. If any one of the operands is scalar, take |
| 8170 |
the other one's size. If both of them are arrays, it does not |
| 8171 |
matter -- try to find one with known shape, if possible. */ |
| 8172 |
default: |
| 8173 |
if (array->value.op.op1->rank == 0) |
| 8174 |
replacement = array->value.op.op2; |
| 8175 |
else if (array->value.op.op2->rank == 0) |
| 8176 |
replacement = array->value.op.op1; |
| 8177 |
else |
| 8178 |
{ |
| 8179 |
simplified = simplify_size (array->value.op.op1, dim, k); |
| 8180 |
if (simplified) |
| 8181 |
return simplified; |
| 8182 |
|
| 8183 |
replacement = array->value.op.op2; |
| 8184 |
} |
| 8185 |
break; |
| 8186 |
} |
| 8187 |
|
| 8188 |
/* Try to reduce it directly if possible. */ |
| 8189 |
simplified = simplify_size (replacement, dim, k); |
| 8190 |
|
| 8191 |
/* Otherwise, we build a new SIZE call. This is hopefully at least |
| 8192 |
simpler than the original one. */ |
| 8193 |
if (!simplified) |
| 8194 |
{ |
| 8195 |
gfc_expr *kind = gfc_get_int_expr (gfc_default_integer_kind, NULL, k); |
| 8196 |
simplified = gfc_build_intrinsic_call (gfc_current_ns, |
| 8197 |
GFC_ISYM_SIZE, "size", |
| 8198 |
array->where, 3, |
| 8199 |
gfc_copy_expr (replacement), |
| 8200 |
gfc_copy_expr (dim), |
| 8201 |
kind); |
| 8202 |
} |
| 8203 |
return simplified; |
| 8204 |
} |
| 8205 |
|
| 8206 |
for (ref = array->ref; ref; ref = ref->next) |
| 8207 |
if (ref->type == REF_ARRAY && ref->u.ar.as |
| 8208 |
&& !gfc_resolve_array_spec (ref->u.ar.as, 0)) |
| 8209 |
return NULL; |
| 8210 |
|
| 8211 |
if (dim == NULL) |
| 8212 |
{ |
| 8213 |
if (!gfc_array_size (array, &size)) |
| 8214 |
return NULL; |
| 8215 |
} |
| 8216 |
else |
| 8217 |
{ |
| 8218 |
if (dim->expr_type != EXPR_CONSTANT) |
| 8219 |
return NULL; |
| 8220 |
|
| 8221 |
if (array->rank == -1) |
| 8222 |
return NULL; |
| 8223 |
|
| 8224 |
d = mpz_get_si (dim->value.integer) - 1; |
| 8225 |
if (d < 0 || d > array->rank - 1) |
| 8226 |
{ |
| 8227 |
gfc_error ("DIM argument (%d) to intrinsic SIZE at %L out of range " |
| 8228 |
"(1:%d)", d+1, &array->where, array->rank); |
| 8229 |
return &gfc_bad_expr; |
| 8230 |
} |
| 8231 |
|
| 8232 |
if (!gfc_array_dimen_size (array, d, &size)) |
| 8233 |
return NULL; |
| 8234 |
} |
| 8235 |
|
| 8236 |
return_value = gfc_get_constant_expr (BT_INTEGER, k, &array->where); |
| 8237 |
mpz_set (return_value->value.integer, size); |
| 8238 |
mpz_clear (size); |
| 8239 |
|
| 8240 |
return return_value; |
| 8241 |
} |
| 8242 |
|
| 8243 |
|
| 8244 |
gfc_expr * |
| 8245 |
gfc_simplify_size (gfc_expr *array, gfc_expr *dim, gfc_expr *kind) |
| 8246 |
{ |
| 8247 |
gfc_expr *result; |
| 8248 |
int k = get_kind (BT_INTEGER, kind, "SIZE", gfc_default_integer_kind); |
| 8249 |
|
| 8250 |
if (k == -1) |
| 8251 |
return &gfc_bad_expr; |
| 8252 |
|
| 8253 |
result = simplify_size (array, dim, k); |
| 8254 |
if (result == NULL || result == &gfc_bad_expr) |
| 8255 |
return result; |
| 8256 |
|
| 8257 |
return range_check (result, "SIZE"); |
| 8258 |
} |
| 8259 |
|
| 8260 |
|
| 8261 |
/* SIZEOF and C_SIZEOF return the size in bytes of an array element |
| 8262 |
multiplied by the array size. */ |
| 8263 |
|
| 8264 |
gfc_expr * |
| 8265 |
gfc_simplify_sizeof (gfc_expr *x) |
| 8266 |
{ |
| 8267 |
gfc_expr *result = NULL; |
| 8268 |
mpz_t array_size; |
| 8269 |
size_t res_size; |
| 8270 |
|
| 8271 |
if (x->ts.type == BT_CLASS || x->ts.deferred) |
| 8272 |
return NULL; |
| 8273 |
|
| 8274 |
if (x->ts.type == BT_CHARACTER |
| 8275 |
&& (!x->ts.u.cl || !x->ts.u.cl->length |
| 8276 |
|| x->ts.u.cl->length->expr_type != EXPR_CONSTANT)) |
| 8277 |
return NULL; |
| 8278 |
|
| 8279 |
if (x->rank && x->expr_type != EXPR_ARRAY) |
| 8280 |
{ |
| 8281 |
if (!gfc_array_size (x, &array_size)) |
| 8282 |
return NULL; |
| 8283 |
|
| 8284 |
mpz_clear (array_size); |
| 8285 |
} |
| 8286 |
|
| 8287 |
result = gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind, |
| 8288 |
&x->where); |
| 8289 |
gfc_target_expr_size (x, &res_size); |
| 8290 |
mpz_set_si (result->value.integer, res_size); |
| 8291 |
|
| 8292 |
return result; |
| 8293 |
} |
| 8294 |
|
| 8295 |
|
| 8296 |
/* STORAGE_SIZE returns the size in bits of a single array element. */ |
| 8297 |
|
| 8298 |
gfc_expr * |
| 8299 |
gfc_simplify_storage_size (gfc_expr *x, |
| 8300 |
gfc_expr *kind) |
| 8301 |
{ |
| 8302 |
gfc_expr *result = NULL; |
| 8303 |
int k; |
| 8304 |
size_t siz; |
| 8305 |
|
| 8306 |
if (x->ts.type == BT_CLASS || x->ts.deferred) |
| 8307 |
return NULL; |
| 8308 |
|
| 8309 |
if (x->ts.type == BT_CHARACTER && x->expr_type != EXPR_CONSTANT |
| 8310 |
&& (!x->ts.u.cl || !x->ts.u.cl->length |
| 8311 |
|| x->ts.u.cl->length->expr_type != EXPR_CONSTANT)) |
| 8312 |
return NULL; |
| 8313 |
|
| 8314 |
k = get_kind (BT_INTEGER, kind, "STORAGE_SIZE", gfc_default_integer_kind); |
| 8315 |
if (k == -1) |
| 8316 |
return &gfc_bad_expr; |
| 8317 |
|
| 8318 |
result = gfc_get_constant_expr (BT_INTEGER, k, &x->where); |
| 8319 |
|
| 8320 |
gfc_element_size (x, &siz); |
| 8321 |
mpz_set_si (result->value.integer, siz); |
| 8322 |
mpz_mul_ui (result->value.integer, result->value.integer, BITS_PER_UNIT); |
| 8323 |
|
| 8324 |
return range_check (result, "STORAGE_SIZE"); |
| 8325 |
} |
| 8326 |
|
| 8327 |
|
| 8328 |
gfc_expr * |
| 8329 |
gfc_simplify_sign (gfc_expr *x, gfc_expr *y) |
| 8330 |
{ |
| 8331 |
gfc_expr *result; |
| 8332 |
|
| 8333 |
if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT) |
| 8334 |
return NULL; |
| 8335 |
|
| 8336 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 8337 |
|
| 8338 |
switch (x->ts.type) |
| 8339 |
{ |
| 8340 |
case BT_INTEGER: |
| 8341 |
mpz_abs (result->value.integer, x->value.integer); |
| 8342 |
if (mpz_sgn (y->value.integer) < 0) |
| 8343 |
mpz_neg (result->value.integer, result->value.integer); |
| 8344 |
break; |
| 8345 |
|
| 8346 |
case BT_REAL: |
| 8347 |
if (flag_sign_zero) |
| 8348 |
mpfr_copysign (result->value.real, x->value.real, y->value.real, |
| 8349 |
GFC_RND_MODE); |
| 8350 |
else |
| 8351 |
mpfr_setsign (result->value.real, x->value.real, |
| 8352 |
mpfr_sgn (y->value.real) < 0 ? 1 : 0, GFC_RND_MODE); |
| 8353 |
break; |
| 8354 |
|
| 8355 |
default: |
| 8356 |
gfc_internal_error ("Bad type in gfc_simplify_sign"); |
| 8357 |
} |
| 8358 |
|
| 8359 |
return result; |
| 8360 |
} |
| 8361 |
|
| 8362 |
|
| 8363 |
gfc_expr * |
| 8364 |
gfc_simplify_sin (gfc_expr *x) |
| 8365 |
{ |
| 8366 |
gfc_expr *result; |
| 8367 |
|
| 8368 |
if (x->expr_type != EXPR_CONSTANT) |
| 8369 |
return NULL; |
| 8370 |
|
| 8371 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 8372 |
|
| 8373 |
switch (x->ts.type) |
| 8374 |
{ |
| 8375 |
case BT_REAL: |
| 8376 |
mpfr_sin (result->value.real, x->value.real, GFC_RND_MODE); |
| 8377 |
break; |
| 8378 |
|
| 8379 |
case BT_COMPLEX: |
| 8380 |
gfc_set_model (x->value.real); |
| 8381 |
mpc_sin (result->value.complex, x->value.complex, GFC_MPC_RND_MODE); |
| 8382 |
break; |
| 8383 |
|
| 8384 |
default: |
| 8385 |
gfc_internal_error ("in gfc_simplify_sin(): Bad type"); |
| 8386 |
} |
| 8387 |
|
| 8388 |
return range_check (result, "SIN"); |
| 8389 |
} |
| 8390 |
|
| 8391 |
|
| 8392 |
gfc_expr * |
| 8393 |
gfc_simplify_sinh (gfc_expr *x) |
| 8394 |
{ |
| 8395 |
gfc_expr *result; |
| 8396 |
|
| 8397 |
if (x->expr_type != EXPR_CONSTANT) |
| 8398 |
return NULL; |
| 8399 |
|
| 8400 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 8401 |
|
| 8402 |
switch (x->ts.type) |
| 8403 |
{ |
| 8404 |
case BT_REAL: |
| 8405 |
mpfr_sinh (result->value.real, x->value.real, GFC_RND_MODE); |
| 8406 |
break; |
| 8407 |
|
| 8408 |
case BT_COMPLEX: |
| 8409 |
mpc_sinh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE); |
| 8410 |
break; |
| 8411 |
|
| 8412 |
default: |
| 8413 |
gcc_unreachable (); |
| 8414 |
} |
| 8415 |
|
| 8416 |
return range_check (result, "SINH"); |
| 8417 |
} |
| 8418 |
|
| 8419 |
|
| 8420 |
/* The argument is always a double precision real that is converted to |
| 8421 |
single precision. TODO: Rounding! */ |
| 8422 |
|
| 8423 |
gfc_expr * |
| 8424 |
gfc_simplify_sngl (gfc_expr *a) |
| 8425 |
{ |
| 8426 |
gfc_expr *result; |
| 8427 |
int tmp1, tmp2; |
| 8428 |
|
| 8429 |
if (a->expr_type != EXPR_CONSTANT) |
| 8430 |
return NULL; |
| 8431 |
|
| 8432 |
/* For explicit conversion, turn off -Wconversion and -Wconversion-extra |
| 8433 |
warnings. */ |
| 8434 |
tmp1 = warn_conversion; |
| 8435 |
tmp2 = warn_conversion_extra; |
| 8436 |
warn_conversion = warn_conversion_extra = 0; |
| 8437 |
|
| 8438 |
result = gfc_real2real (a, gfc_default_real_kind); |
| 8439 |
|
| 8440 |
warn_conversion = tmp1; |
| 8441 |
warn_conversion_extra = tmp2; |
| 8442 |
|
| 8443 |
return range_check (result, "SNGL"); |
| 8444 |
} |
| 8445 |
|
| 8446 |
|
| 8447 |
gfc_expr * |
| 8448 |
gfc_simplify_spacing (gfc_expr *x) |
| 8449 |
{ |
| 8450 |
gfc_expr *result; |
| 8451 |
int i; |
| 8452 |
long int en, ep; |
| 8453 |
|
| 8454 |
if (x->expr_type != EXPR_CONSTANT) |
| 8455 |
return NULL; |
| 8456 |
|
| 8457 |
i = gfc_validate_kind (x->ts.type, x->ts.kind, false); |
| 8458 |
result = gfc_get_constant_expr (BT_REAL, x->ts.kind, &x->where); |
| 8459 |
|
| 8460 |
/* SPACING(+/- 0.0) = SPACING(TINY(0.0)) = TINY(0.0) */ |
| 8461 |
if (mpfr_zero_p (x->value.real)) |
| 8462 |
{ |
| 8463 |
mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE); |
| 8464 |
return result; |
| 8465 |
} |
| 8466 |
|
| 8467 |
/* SPACING(inf) = NaN */ |
| 8468 |
if (mpfr_inf_p (x->value.real)) |
| 8469 |
{ |
| 8470 |
mpfr_set_nan (result->value.real); |
| 8471 |
return result; |
| 8472 |
} |
| 8473 |
|
| 8474 |
/* SPACING(NaN) = same NaN */ |
| 8475 |
if (mpfr_nan_p (x->value.real)) |
| 8476 |
{ |
| 8477 |
mpfr_set (result->value.real, x->value.real, GFC_RND_MODE); |
| 8478 |
return result; |
| 8479 |
} |
| 8480 |
|
| 8481 |
/* In the Fortran 95 standard, the result is b**(e - p) where b, e, and p |
| 8482 |
are the radix, exponent of x, and precision. This excludes the |
| 8483 |
possibility of subnormal numbers. Fortran 2003 states the result is |
| 8484 |
b**max(e - p, emin - 1). */ |
| 8485 |
|
| 8486 |
ep = (long int) mpfr_get_exp (x->value.real) - gfc_real_kinds[i].digits; |
| 8487 |
en = (long int) gfc_real_kinds[i].min_exponent - 1; |
| 8488 |
en = en > ep ? en : ep; |
| 8489 |
|
| 8490 |
mpfr_set_ui (result->value.real, 1, GFC_RND_MODE); |
| 8491 |
mpfr_mul_2si (result->value.real, result->value.real, en, GFC_RND_MODE); |
| 8492 |
|
| 8493 |
return range_check (result, "SPACING"); |
| 8494 |
} |
| 8495 |
|
| 8496 |
|
| 8497 |
gfc_expr * |
| 8498 |
gfc_simplify_spread (gfc_expr *source, gfc_expr *dim_expr, gfc_expr *ncopies_expr) |
| 8499 |
{ |
| 8500 |
gfc_expr *result = NULL; |
| 8501 |
int nelem, i, j, dim, ncopies; |
| 8502 |
mpz_t size; |
| 8503 |
|
| 8504 |
if ((!gfc_is_constant_expr (source) |
| 8505 |
&& !is_constant_array_expr (source)) |
| 8506 |
|| !gfc_is_constant_expr (dim_expr) |
| 8507 |
|| !gfc_is_constant_expr (ncopies_expr)) |
| 8508 |
return NULL; |
| 8509 |
|
| 8510 |
gcc_assert (dim_expr->ts.type == BT_INTEGER); |
| 8511 |
gfc_extract_int (dim_expr, &dim); |
| 8512 |
dim -= 1; /* zero-base DIM */ |
| 8513 |
|
| 8514 |
gcc_assert (ncopies_expr->ts.type == BT_INTEGER); |
| 8515 |
gfc_extract_int (ncopies_expr, &ncopies); |
| 8516 |
ncopies = MAX (ncopies, 0); |
| 8517 |
|
| 8518 |
/* Do not allow the array size to exceed the limit for an array |
| 8519 |
constructor. */ |
| 8520 |
if (source->expr_type == EXPR_ARRAY) |
| 8521 |
{ |
| 8522 |
if (!gfc_array_size (source, &size)) |
| 8523 |
gfc_internal_error ("Failure getting length of a constant array."); |
| 8524 |
} |
| 8525 |
else |
| 8526 |
mpz_init_set_ui (size, 1); |
| 8527 |
|
| 8528 |
nelem = mpz_get_si (size) * ncopies; |
| 8529 |
if (nelem > flag_max_array_constructor) |
| 8530 |
{ |
| 8531 |
if (gfc_init_expr_flag) |
| 8532 |
{ |
| 8533 |
gfc_error ("The number of elements (%d) in the array constructor " |
| 8534 |
"at %L requires an increase of the allowed %d upper " |
| 8535 |
"limit. See %<-fmax-array-constructor%> option.", |
| 8536 |
nelem, &source->where, flag_max_array_constructor); |
| 8537 |
return &gfc_bad_expr; |
| 8538 |
} |
| 8539 |
else |
| 8540 |
return NULL; |
| 8541 |
} |
| 8542 |
|
| 8543 |
if (source->expr_type == EXPR_CONSTANT |
| 8544 |
|| source->expr_type == EXPR_STRUCTURE) |
| 8545 |
{ |
| 8546 |
gcc_assert (dim == 0); |
| 8547 |
|
| 8548 |
result = gfc_get_array_expr (source->ts.type, source->ts.kind, |
| 8549 |
&source->where); |
| 8550 |
if (source->ts.type == BT_DERIVED) |
| 8551 |
result->ts.u.derived = source->ts.u.derived; |
| 8552 |
result->rank = 1; |
| 8553 |
result->shape = gfc_get_shape (result->rank); |
| 8554 |
mpz_init_set_si (result->shape[0], ncopies); |
| 8555 |
|
| 8556 |
for (i = 0; i < ncopies; ++i) |
| 8557 |
gfc_constructor_append_expr (&result->value.constructor, |
| 8558 |
gfc_copy_expr (source), NULL); |
| 8559 |
} |
| 8560 |
else if (source->expr_type == EXPR_ARRAY) |
| 8561 |
{ |
| 8562 |
int offset, rstride[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS]; |
| 8563 |
gfc_constructor *source_ctor; |
| 8564 |
|
| 8565 |
gcc_assert (source->rank < GFC_MAX_DIMENSIONS); |
| 8566 |
gcc_assert (dim >= 0 && dim <= source->rank); |
| 8567 |
|
| 8568 |
result = gfc_get_array_expr (source->ts.type, source->ts.kind, |
| 8569 |
&source->where); |
| 8570 |
if (source->ts.type == BT_DERIVED) |
| 8571 |
result->ts.u.derived = source->ts.u.derived; |
| 8572 |
result->rank = source->rank + 1; |
| 8573 |
result->shape = gfc_get_shape (result->rank); |
| 8574 |
|
| 8575 |
for (i = 0, j = 0; i < result->rank; ++i) |
| 8576 |
{ |
| 8577 |
if (i != dim) |
| 8578 |
mpz_init_set (result->shape[i], source->shape[j++]); |
| 8579 |
else |
| 8580 |
mpz_init_set_si (result->shape[i], ncopies); |
| 8581 |
|
| 8582 |
extent[i] = mpz_get_si (result->shape[i]); |
| 8583 |
rstride[i] = (i == 0) ? 1 : rstride[i-1] * extent[i-1]; |
| 8584 |
} |
| 8585 |
|
| 8586 |
offset = 0; |
| 8587 |
for (source_ctor = gfc_constructor_first (source->value.constructor); |
| 8588 |
source_ctor; source_ctor = gfc_constructor_next (source_ctor)) |
| 8589 |
{ |
| 8590 |
for (i = 0; i < ncopies; ++i) |
| 8591 |
gfc_constructor_insert_expr (&result->value.constructor, |
| 8592 |
gfc_copy_expr (source_ctor->expr), |
| 8593 |
NULL, offset + i * rstride[dim]); |
| 8594 |
|
| 8595 |
offset += (dim == 0 ? ncopies : 1); |
| 8596 |
} |
| 8597 |
} |
| 8598 |
else |
| 8599 |
{ |
| 8600 |
gfc_error ("Simplification of SPREAD at %C not yet implemented"); |
| 8601 |
return &gfc_bad_expr; |
| 8602 |
} |
| 8603 |
|
| 8604 |
if (source->ts.type == BT_CHARACTER) |
| 8605 |
result->ts.u.cl = source->ts.u.cl; |
| 8606 |
|
| 8607 |
return result; |
| 8608 |
} |
| 8609 |
|
| 8610 |
|
| 8611 |
gfc_expr * |
| 8612 |
gfc_simplify_sqrt (gfc_expr *e) |
| 8613 |
{ |
| 8614 |
gfc_expr *result = NULL; |
| 8615 |
|
| 8616 |
if (e->expr_type != EXPR_CONSTANT) |
| 8617 |
return NULL; |
| 8618 |
|
| 8619 |
switch (e->ts.type) |
| 8620 |
{ |
| 8621 |
case BT_REAL: |
| 8622 |
if (mpfr_cmp_si (e->value.real, 0) < 0) |
| 8623 |
{ |
| 8624 |
gfc_error ("Argument of SQRT at %L has a negative value", |
| 8625 |
&e->where); |
| 8626 |
return &gfc_bad_expr; |
| 8627 |
} |
| 8628 |
result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where); |
| 8629 |
mpfr_sqrt (result->value.real, e->value.real, GFC_RND_MODE); |
| 8630 |
break; |
| 8631 |
|
| 8632 |
case BT_COMPLEX: |
| 8633 |
gfc_set_model (e->value.real); |
| 8634 |
|
| 8635 |
result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where); |
| 8636 |
mpc_sqrt (result->value.complex, e->value.complex, GFC_MPC_RND_MODE); |
| 8637 |
break; |
| 8638 |
|
| 8639 |
default: |
| 8640 |
gfc_internal_error ("invalid argument of SQRT at %L", &e->where); |
| 8641 |
} |
| 8642 |
|
| 8643 |
return range_check (result, "SQRT"); |
| 8644 |
} |
| 8645 |
|
| 8646 |
|
| 8647 |
gfc_expr * |
| 8648 |
gfc_simplify_sum (gfc_expr *array, gfc_expr *dim, gfc_expr *mask) |
| 8649 |
{ |
| 8650 |
return simplify_transformation (array, dim, mask, 0, gfc_add); |
| 8651 |
} |
| 8652 |
|
| 8653 |
|
| 8654 |
/* Simplify COTAN(X) where X has the unit of radian. */ |
| 8655 |
|
| 8656 |
gfc_expr * |
| 8657 |
gfc_simplify_cotan (gfc_expr *x) |
| 8658 |
{ |
| 8659 |
gfc_expr *result; |
| 8660 |
mpc_t swp, *val; |
| 8661 |
|
| 8662 |
if (x->expr_type != EXPR_CONSTANT) |
| 8663 |
return NULL; |
| 8664 |
|
| 8665 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 8666 |
|
| 8667 |
switch (x->ts.type) |
| 8668 |
{ |
| 8669 |
case BT_REAL: |
| 8670 |
mpfr_cot (result->value.real, x->value.real, GFC_RND_MODE); |
| 8671 |
break; |
| 8672 |
|
| 8673 |
case BT_COMPLEX: |
| 8674 |
/* There is no builtin mpc_cot, so compute cot = cos / sin. */ |
| 8675 |
val = &result->value.complex; |
| 8676 |
mpc_init2 (swp, mpfr_get_default_prec ()); |
| 8677 |
mpc_sin_cos (*val, swp, x->value.complex, GFC_MPC_RND_MODE, |
| 8678 |
GFC_MPC_RND_MODE); |
| 8679 |
mpc_div (*val, swp, *val, GFC_MPC_RND_MODE); |
| 8680 |
mpc_clear (swp); |
| 8681 |
break; |
| 8682 |
|
| 8683 |
default: |
| 8684 |
gcc_unreachable (); |
| 8685 |
} |
| 8686 |
|
| 8687 |
return range_check (result, "COTAN"); |
| 8688 |
} |
| 8689 |
|
| 8690 |
|
| 8691 |
gfc_expr * |
| 8692 |
gfc_simplify_tan (gfc_expr *x) |
| 8693 |
{ |
| 8694 |
gfc_expr *result; |
| 8695 |
|
| 8696 |
if (x->expr_type != EXPR_CONSTANT) |
| 8697 |
return NULL; |
| 8698 |
|
| 8699 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 8700 |
|
| 8701 |
switch (x->ts.type) |
| 8702 |
{ |
| 8703 |
case BT_REAL: |
| 8704 |
mpfr_tan (result->value.real, x->value.real, GFC_RND_MODE); |
| 8705 |
break; |
| 8706 |
|
| 8707 |
case BT_COMPLEX: |
| 8708 |
mpc_tan (result->value.complex, x->value.complex, GFC_MPC_RND_MODE); |
| 8709 |
break; |
| 8710 |
|
| 8711 |
default: |
| 8712 |
gcc_unreachable (); |
| 8713 |
} |
| 8714 |
|
| 8715 |
return range_check (result, "TAN"); |
| 8716 |
} |
| 8717 |
|
| 8718 |
|
| 8719 |
gfc_expr * |
| 8720 |
gfc_simplify_tanh (gfc_expr *x) |
| 8721 |
{ |
| 8722 |
gfc_expr *result; |
| 8723 |
|
| 8724 |
if (x->expr_type != EXPR_CONSTANT) |
| 8725 |
return NULL; |
| 8726 |
|
| 8727 |
result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where); |
| 8728 |
|
| 8729 |
switch (x->ts.type) |
| 8730 |
{ |
| 8731 |
case BT_REAL: |
| 8732 |
mpfr_tanh (result->value.real, x->value.real, GFC_RND_MODE); |
| 8733 |
break; |
| 8734 |
|
| 8735 |
case BT_COMPLEX: |
| 8736 |
mpc_tanh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE); |
| 8737 |
break; |
| 8738 |
|
| 8739 |
default: |
| 8740 |
gcc_unreachable (); |
| 8741 |
} |
| 8742 |
|
| 8743 |
return range_check (result, "TANH"); |
| 8744 |
} |
| 8745 |
|
| 8746 |
|
| 8747 |
gfc_expr * |
| 8748 |
gfc_simplify_tiny (gfc_expr *e) |
| 8749 |
{ |
| 8750 |
gfc_expr *result; |
| 8751 |
int i; |
| 8752 |
|
| 8753 |
i = gfc_validate_kind (BT_REAL, e->ts.kind, false); |
| 8754 |
|
| 8755 |
result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where); |
| 8756 |
mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE); |
| 8757 |
|
| 8758 |
return result; |
| 8759 |
} |
| 8760 |
|
| 8761 |
|
| 8762 |
gfc_expr * |
| 8763 |
gfc_simplify_trailz (gfc_expr *e) |
| 8764 |
{ |
| 8765 |
unsigned long tz, bs; |
| 8766 |
int i; |
| 8767 |
|
| 8768 |
if (e->expr_type != EXPR_CONSTANT) |
| 8769 |
return NULL; |
| 8770 |
|
| 8771 |
i = gfc_validate_kind (e->ts.type, e->ts.kind, false); |
| 8772 |
bs = gfc_integer_kinds[i].bit_size; |
| 8773 |
tz = mpz_scan1 (e->value.integer, 0); |
| 8774 |
|
| 8775 |
return gfc_get_int_expr (gfc_default_integer_kind, |
| 8776 |
&e->where, MIN (tz, bs)); |
| 8777 |
} |
| 8778 |
|
| 8779 |
|
| 8780 |
gfc_expr * |
| 8781 |
gfc_simplify_transfer (gfc_expr *source, gfc_expr *mold, gfc_expr *size) |
| 8782 |
{ |
| 8783 |
gfc_expr *result; |
| 8784 |
gfc_expr *mold_element; |
| 8785 |
size_t source_size; |
| 8786 |
size_t result_size; |
| 8787 |
size_t buffer_size; |
| 8788 |
mpz_t tmp; |
| 8789 |
unsigned char *buffer; |
| 8790 |
size_t result_length; |
| 8791 |
|
| 8792 |
if (!gfc_is_constant_expr (source) || !gfc_is_constant_expr (size)) |
| 8793 |
return NULL; |
| 8794 |
|
| 8795 |
if (!gfc_resolve_expr (mold)) |
| 8796 |
return NULL; |
| 8797 |
if (gfc_init_expr_flag && !gfc_is_constant_expr (mold)) |
| 8798 |
return NULL; |
| 8799 |
|
| 8800 |
if (!gfc_calculate_transfer_sizes (source, mold, size, &source_size, |
| 8801 |
&result_size, &result_length)) |
| 8802 |
return NULL; |
| 8803 |
|
| 8804 |
/* Calculate the size of the source. */ |
| 8805 |
if (source->expr_type == EXPR_ARRAY && !gfc_array_size (source, &tmp)) |
| 8806 |
gfc_internal_error ("Failure getting length of a constant array."); |
| 8807 |
|
| 8808 |
/* Create an empty new expression with the appropriate characteristics. */ |
| 8809 |
result = gfc_get_constant_expr (mold->ts.type, mold->ts.kind, |
| 8810 |
&source->where); |
| 8811 |
result->ts = mold->ts; |
| 8812 |
|
| 8813 |
mold_element = (mold->expr_type == EXPR_ARRAY && mold->value.constructor) |
| 8814 |
? gfc_constructor_first (mold->value.constructor)->expr |
| 8815 |
: mold; |
| 8816 |
|
| 8817 |
/* Set result character length, if needed. Note that this needs to be |
| 8818 |
set even for array expressions, in order to pass this information into |
| 8819 |
gfc_target_interpret_expr. */ |
| 8820 |
if (result->ts.type == BT_CHARACTER && gfc_is_constant_expr (mold_element)) |
| 8821 |
{ |
| 8822 |
result->value.character.length = mold_element->value.character.length; |
| 8823 |
|
| 8824 |
/* Let the typespec of the result inherit the string length. |
| 8825 |
This is crucial if a resulting array has size zero. */ |
| 8826 |
if (mold_element->ts.u.cl->length) |
| 8827 |
result->ts.u.cl->length = gfc_copy_expr (mold_element->ts.u.cl->length); |
| 8828 |
else |
| 8829 |
result->ts.u.cl->length = |
| 8830 |
gfc_get_int_expr (gfc_charlen_int_kind, NULL, |
| 8831 |
mold_element->value.character.length); |
| 8832 |
} |
| 8833 |
|
| 8834 |
/* Set the number of elements in the result, and determine its size. */ |
| 8835 |
|
| 8836 |
if (mold->expr_type == EXPR_ARRAY || mold->rank || size) |
| 8837 |
{ |
| 8838 |
result->expr_type = EXPR_ARRAY; |
| 8839 |
result->rank = 1; |
| 8840 |
result->shape = gfc_get_shape (1); |
| 8841 |
mpz_init_set_ui (result->shape[0], result_length); |
| 8842 |
} |
| 8843 |
else |
| 8844 |
result->rank = 0; |
| 8845 |
|
| 8846 |
/* Allocate the buffer to store the binary version of the source. */ |
| 8847 |
buffer_size = MAX (source_size, result_size); |
| 8848 |
buffer = (unsigned char*)alloca (buffer_size); |
| 8849 |
memset (buffer, 0, buffer_size); |
| 8850 |
|
| 8851 |
/* Now write source to the buffer. */ |
| 8852 |
gfc_target_encode_expr (source, buffer, buffer_size); |
| 8853 |
|
| 8854 |
/* And read the buffer back into the new expression. */ |
| 8855 |
gfc_target_interpret_expr (buffer, buffer_size, result, false); |
| 8856 |
|
| 8857 |
return result; |
| 8858 |
} |
| 8859 |
|
| 8860 |
|
| 8861 |
gfc_expr * |
| 8862 |
gfc_simplify_transpose (gfc_expr *matrix) |
| 8863 |
{ |
| 8864 |
int row, matrix_rows, col, matrix_cols; |
| 8865 |
gfc_expr *result; |
| 8866 |
|
| 8867 |
if (!is_constant_array_expr (matrix)) |
| 8868 |
return NULL; |
| 8869 |
|
| 8870 |
gcc_assert (matrix->rank == 2); |
| 8871 |
|
| 8872 |
if (matrix->shape == NULL) |
| 8873 |
return NULL; |
| 8874 |
|
| 8875 |
result = gfc_get_array_expr (matrix->ts.type, matrix->ts.kind, |
| 8876 |
&matrix->where); |
| 8877 |
result->rank = 2; |
| 8878 |
result->shape = gfc_get_shape (result->rank); |
| 8879 |
mpz_init_set (result->shape[0], matrix->shape[1]); |
| 8880 |
mpz_init_set (result->shape[1], matrix->shape[0]); |
| 8881 |
|
| 8882 |
if (matrix->ts.type == BT_CHARACTER) |
| 8883 |
result->ts.u.cl = matrix->ts.u.cl; |
| 8884 |
else if (matrix->ts.type == BT_DERIVED) |
| 8885 |
result->ts.u.derived = matrix->ts.u.derived; |
| 8886 |
|
| 8887 |
matrix_rows = mpz_get_si (matrix->shape[0]); |
| 8888 |
matrix_cols = mpz_get_si (matrix->shape[1]); |
| 8889 |
for (row = 0; row < matrix_rows; ++row) |
| 8890 |
for (col = 0; col < matrix_cols; ++col) |
| 8891 |
{ |
| 8892 |
gfc_expr *e = gfc_constructor_lookup_expr (matrix->value.constructor, |
| 8893 |
col * matrix_rows + row); |
| 8894 |
gfc_constructor_insert_expr (&result->value.constructor, |
| 8895 |
gfc_copy_expr (e), &matrix->where, |
| 8896 |
row * matrix_cols + col); |
| 8897 |
} |
| 8898 |
|
| 8899 |
return result; |
| 8900 |
} |
| 8901 |
|
| 8902 |
|
| 8903 |
gfc_expr * |
| 8904 |
gfc_simplify_trim (gfc_expr *e) |
| 8905 |
{ |
| 8906 |
gfc_expr *result; |
| 8907 |
int count, i, len, lentrim; |
| 8908 |
|
| 8909 |
if (e->expr_type != EXPR_CONSTANT) |
| 8910 |
return NULL; |
| 8911 |
|
| 8912 |
len = e->value.character.length; |
| 8913 |
for (count = 0, i = 1; i <= len; ++i) |
| 8914 |
{ |
| 8915 |
if (e->value.character.string[len - i] == ' ') |
| 8916 |
count++; |
| 8917 |
else |
| 8918 |
break; |
| 8919 |
} |
| 8920 |
|
| 8921 |
lentrim = len - count; |
| 8922 |
|
| 8923 |
result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, lentrim); |
| 8924 |
for (i = 0; i < lentrim; i++) |
| 8925 |
result->value.character.string[i] = e->value.character.string[i]; |
| 8926 |
|
| 8927 |
return result; |
| 8928 |
} |
| 8929 |
|
| 8930 |
|
| 8931 |
gfc_expr * |
| 8932 |
gfc_simplify_image_index (gfc_expr *coarray, gfc_expr *sub) |
| 8933 |
{ |
| 8934 |
gfc_expr *result; |
| 8935 |
gfc_ref *ref; |
| 8936 |
gfc_array_spec *as; |
| 8937 |
gfc_constructor *sub_cons; |
| 8938 |
bool first_image; |
| 8939 |
int d; |
| 8940 |
|
| 8941 |
if (!is_constant_array_expr (sub)) |
| 8942 |
return NULL; |
| 8943 |
|
| 8944 |
/* Follow any component references. */ |
| 8945 |
as = coarray->symtree->n.sym->as; |
| 8946 |
for (ref = coarray->ref; ref; ref = ref->next) |
| 8947 |
if (ref->type == REF_COMPONENT) |
| 8948 |
as = ref->u.ar.as; |
| 8949 |
|
| 8950 |
if (!as || as->type == AS_DEFERRED) |
| 8951 |
return NULL; |
| 8952 |
|
| 8953 |
/* "valid sequence of cosubscripts" are required; thus, return 0 unless |
| 8954 |
the cosubscript addresses the first image. */ |
| 8955 |
|
| 8956 |
sub_cons = gfc_constructor_first (sub->value.constructor); |
| 8957 |
first_image = true; |
| 8958 |
|
| 8959 |
for (d = 1; d <= as->corank; d++) |
| 8960 |
{ |
| 8961 |
gfc_expr *ca_bound; |
| 8962 |
int cmp; |
| 8963 |
|
| 8964 |
gcc_assert (sub_cons != NULL); |
| 8965 |
|
| 8966 |
ca_bound = simplify_bound_dim (coarray, NULL, d + as->rank, 0, as, |
| 8967 |
NULL, true); |
| 8968 |
if (ca_bound == NULL) |
| 8969 |
return NULL; |
| 8970 |
|
| 8971 |
if (ca_bound == &gfc_bad_expr) |
| 8972 |
return ca_bound; |
| 8973 |
|
| 8974 |
cmp = mpz_cmp (ca_bound->value.integer, sub_cons->expr->value.integer); |
| 8975 |
|
| 8976 |
if (cmp == 0) |
| 8977 |
{ |
| 8978 |
gfc_free_expr (ca_bound); |
| 8979 |
sub_cons = gfc_constructor_next (sub_cons); |
| 8980 |
continue; |
| 8981 |
} |
| 8982 |
|
| 8983 |
first_image = false; |
| 8984 |
|
| 8985 |
if (cmp > 0) |
| 8986 |
{ |
| 8987 |
gfc_error ("Out of bounds in IMAGE_INDEX at %L for dimension %d, " |
| 8988 |
"SUB has %ld and COARRAY lower bound is %ld)", |
| 8989 |
&coarray->where, d, |
| 8990 |
mpz_get_si (sub_cons->expr->value.integer), |
| 8991 |
mpz_get_si (ca_bound->value.integer)); |
| 8992 |
gfc_free_expr (ca_bound); |
| 8993 |
return &gfc_bad_expr; |
| 8994 |
} |
| 8995 |
|
| 8996 |
gfc_free_expr (ca_bound); |
| 8997 |
|
| 8998 |
/* Check whether upperbound is valid for the multi-images case. */ |
| 8999 |
if (d < as->corank) |
| 9000 |
{ |
| 9001 |
ca_bound = simplify_bound_dim (coarray, NULL, d + as->rank, 1, as, |
| 9002 |
NULL, true); |
| 9003 |
if (ca_bound == &gfc_bad_expr) |
| 9004 |
return ca_bound; |
| 9005 |
|
| 9006 |
if (ca_bound && ca_bound->expr_type == EXPR_CONSTANT |
| 9007 |
&& mpz_cmp (ca_bound->value.integer, |
| 9008 |
sub_cons->expr->value.integer) < 0) |
| 9009 |
{ |
| 9010 |
gfc_error ("Out of bounds in IMAGE_INDEX at %L for dimension %d, " |
| 9011 |
"SUB has %ld and COARRAY upper bound is %ld)", |
| 9012 |
&coarray->where, d, |
| 9013 |
mpz_get_si (sub_cons->expr->value.integer), |
| 9014 |
mpz_get_si (ca_bound->value.integer)); |
| 9015 |
gfc_free_expr (ca_bound); |
| 9016 |
return &gfc_bad_expr; |
| 9017 |
} |
| 9018 |
|
| 9019 |
if (ca_bound) |
| 9020 |
gfc_free_expr (ca_bound); |
| 9021 |
} |
| 9022 |
|
| 9023 |
sub_cons = gfc_constructor_next (sub_cons); |
| 9024 |
} |
| 9025 |
|
| 9026 |
gcc_assert (sub_cons == NULL); |
| 9027 |
|
| 9028 |
if (flag_coarray != GFC_FCOARRAY_SINGLE && !first_image) |
| 9029 |
return NULL; |
| 9030 |
|
| 9031 |
result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind, |
| 9032 |
&gfc_current_locus); |
| 9033 |
if (first_image) |
| 9034 |
mpz_set_si (result->value.integer, 1); |
| 9035 |
else |
| 9036 |
mpz_set_si (result->value.integer, 0); |
| 9037 |
|
| 9038 |
return result; |
| 9039 |
} |
| 9040 |
|
| 9041 |
gfc_expr * |
| 9042 |
gfc_simplify_image_status (gfc_expr *image, gfc_expr *team ATTRIBUTE_UNUSED) |
| 9043 |
{ |
| 9044 |
if (flag_coarray == GFC_FCOARRAY_NONE) |
| 9045 |
{ |
| 9046 |
gfc_current_locus = *gfc_current_intrinsic_where; |
| 9047 |
gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable"); |
| 9048 |
return &gfc_bad_expr; |
| 9049 |
} |
| 9050 |
|
| 9051 |
/* Simplification is possible for fcoarray = single only. For all other modes |
| 9052 |
the result depends on runtime conditions. */ |
| 9053 |
if (flag_coarray != GFC_FCOARRAY_SINGLE) |
| 9054 |
return NULL; |
| 9055 |
|
| 9056 |
if (gfc_is_constant_expr (image)) |
| 9057 |
{ |
| 9058 |
gfc_expr *result; |
| 9059 |
result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind, |
| 9060 |
&image->where); |
| 9061 |
if (mpz_get_si (image->value.integer) == 1) |
| 9062 |
mpz_set_si (result->value.integer, 0); |
| 9063 |
else |
| 9064 |
mpz_set_si (result->value.integer, GFC_STAT_STOPPED_IMAGE); |
| 9065 |
return result; |
| 9066 |
} |
| 9067 |
else |
| 9068 |
return NULL; |
| 9069 |
} |
| 9070 |
|
| 9071 |
|
| 9072 |
gfc_expr * |
| 9073 |
gfc_simplify_this_image (gfc_expr *coarray, gfc_expr *dim, |
| 9074 |
gfc_expr *distance ATTRIBUTE_UNUSED) |
| 9075 |
{ |
| 9076 |
if (flag_coarray != GFC_FCOARRAY_SINGLE) |
| 9077 |
return NULL; |
| 9078 |
|
| 9079 |
/* If no coarray argument has been passed or when the first argument |
| 9080 |
is actually a distance argument. */ |
| 9081 |
if (coarray == NULL || !gfc_is_coarray (coarray)) |
| 9082 |
{ |
| 9083 |
gfc_expr *result; |
| 9084 |
/* FIXME: gfc_current_locus is wrong. */ |
| 9085 |
result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind, |
| 9086 |
&gfc_current_locus); |
| 9087 |
mpz_set_si (result->value.integer, 1); |
| 9088 |
return result; |
| 9089 |
} |
| 9090 |
|
| 9091 |
/* For -fcoarray=single, this_image(A) is the same as lcobound(A). */ |
| 9092 |
return simplify_cobound (coarray, dim, NULL, 0); |
| 9093 |
} |
| 9094 |
|
| 9095 |
|
| 9096 |
gfc_expr * |
| 9097 |
gfc_simplify_ubound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind) |
| 9098 |
{ |
| 9099 |
return simplify_bound (array, dim, kind, 1); |
| 9100 |
} |
| 9101 |
|
| 9102 |
gfc_expr * |
| 9103 |
gfc_simplify_ucobound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind) |
| 9104 |
{ |
| 9105 |
return simplify_cobound (array, dim, kind, 1); |
| 9106 |
} |
| 9107 |
|
| 9108 |
|
| 9109 |
gfc_expr * |
| 9110 |
gfc_simplify_unpack (gfc_expr *vector, gfc_expr *mask, gfc_expr *field) |
| 9111 |
{ |
| 9112 |
gfc_expr *result, *e; |
| 9113 |
gfc_constructor *vector_ctor, *mask_ctor, *field_ctor; |
| 9114 |
|
| 9115 |
if (!is_constant_array_expr (vector) |
| 9116 |
|| !is_constant_array_expr (mask) |
| 9117 |
|| (!gfc_is_constant_expr (field) |
| 9118 |
&& !is_constant_array_expr (field))) |
| 9119 |
return NULL; |
| 9120 |
|
| 9121 |
result = gfc_get_array_expr (vector->ts.type, vector->ts.kind, |
| 9122 |
&vector->where); |
| 9123 |
if (vector->ts.type == BT_DERIVED) |
| 9124 |
result->ts.u.derived = vector->ts.u.derived; |
| 9125 |
result->rank = mask->rank; |
| 9126 |
result->shape = gfc_copy_shape (mask->shape, mask->rank); |
| 9127 |
|
| 9128 |
if (vector->ts.type == BT_CHARACTER) |
| 9129 |
result->ts.u.cl = vector->ts.u.cl; |
| 9130 |
|
| 9131 |
vector_ctor = gfc_constructor_first (vector->value.constructor); |
| 9132 |
mask_ctor = gfc_constructor_first (mask->value.constructor); |
| 9133 |
field_ctor |
| 9134 |
= field->expr_type == EXPR_ARRAY |
| 9135 |
? gfc_constructor_first (field->value.constructor) |
| 9136 |
: NULL; |
| 9137 |
|
| 9138 |
while (mask_ctor) |
| 9139 |
{ |
| 9140 |
if (mask_ctor->expr->value.logical) |
| 9141 |
{ |
| 9142 |
if (vector_ctor) |
| 9143 |
{ |
| 9144 |
e = gfc_copy_expr (vector_ctor->expr); |
| 9145 |
vector_ctor = gfc_constructor_next (vector_ctor); |
| 9146 |
} |
| 9147 |
else |
| 9148 |
{ |
| 9149 |
gfc_free_expr (result); |
| 9150 |
return NULL; |
| 9151 |
} |
| 9152 |
} |
| 9153 |
else if (field->expr_type == EXPR_ARRAY) |
| 9154 |
{ |
| 9155 |
if (field_ctor) |
| 9156 |
e = gfc_copy_expr (field_ctor->expr); |
| 9157 |
else |
| 9158 |
{ |
| 9159 |
/* Not enough elements in array FIELD. */ |
| 9160 |
gfc_free_expr (result); |
| 9161 |
return &gfc_bad_expr; |
| 9162 |
} |
| 9163 |
} |
| 9164 |
else |
| 9165 |
e = gfc_copy_expr (field); |
| 9166 |
|
| 9167 |
gfc_constructor_append_expr (&result->value.constructor, e, NULL); |
| 9168 |
|
| 9169 |
mask_ctor = gfc_constructor_next (mask_ctor); |
| 9170 |
field_ctor = gfc_constructor_next (field_ctor); |
| 9171 |
} |
| 9172 |
|
| 9173 |
return result; |
| 9174 |
} |
| 9175 |
|
| 9176 |
|
| 9177 |
gfc_expr * |
| 9178 |
gfc_simplify_verify (gfc_expr *s, gfc_expr *set, gfc_expr *b, gfc_expr *kind) |
| 9179 |
{ |
| 9180 |
gfc_expr *result; |
| 9181 |
int back; |
| 9182 |
size_t index, len, lenset; |
| 9183 |
size_t i; |
| 9184 |
int k = get_kind (BT_INTEGER, kind, "VERIFY", gfc_default_integer_kind); |
| 9185 |
|
| 9186 |
if (k == -1) |
| 9187 |
return &gfc_bad_expr; |
| 9188 |
|
| 9189 |
if (s->expr_type != EXPR_CONSTANT || set->expr_type != EXPR_CONSTANT |
| 9190 |
|| ( b != NULL && b->expr_type != EXPR_CONSTANT)) |
| 9191 |
return NULL; |
| 9192 |
|
| 9193 |
if (b != NULL && b->value.logical != 0) |
| 9194 |
back = 1; |
| 9195 |
else |
| 9196 |
back = 0; |
| 9197 |
|
| 9198 |
result = gfc_get_constant_expr (BT_INTEGER, k, &s->where); |
| 9199 |
|
| 9200 |
len = s->value.character.length; |
| 9201 |
lenset = set->value.character.length; |
| 9202 |
|
| 9203 |
if (len == 0) |
| 9204 |
{ |
| 9205 |
mpz_set_ui (result->value.integer, 0); |
| 9206 |
return result; |
| 9207 |
} |
| 9208 |
|
| 9209 |
if (back == 0) |
| 9210 |
{ |
| 9211 |
if (lenset == 0) |
| 9212 |
{ |
| 9213 |
mpz_set_ui (result->value.integer, 1); |
| 9214 |
return result; |
| 9215 |
} |
| 9216 |
|
| 9217 |
index = wide_strspn (s->value.character.string, |
| 9218 |
set->value.character.string) + 1; |
| 9219 |
if (index > len) |
| 9220 |
index = 0; |
| 9221 |
|
| 9222 |
} |
| 9223 |
else |
| 9224 |
{ |
| 9225 |
if (lenset == 0) |
| 9226 |
{ |
| 9227 |
mpz_set_ui (result->value.integer, len); |
| 9228 |
return result; |
| 9229 |
} |
| 9230 |
for (index = len; index > 0; index --) |
| 9231 |
{ |
| 9232 |
for (i = 0; i < lenset; i++) |
| 9233 |
{ |
| 9234 |
if (s->value.character.string[index - 1] |
| 9235 |
== set->value.character.string[i]) |
| 9236 |
break; |
| 9237 |
} |
| 9238 |
if (i == lenset) |
| 9239 |
break; |
| 9240 |
} |
| 9241 |
} |
| 9242 |
|
| 9243 |
mpz_set_ui (result->value.integer, index); |
| 9244 |
return result; |
| 9245 |
} |
| 9246 |
|
| 9247 |
|
| 9248 |
gfc_expr * |
| 9249 |
gfc_simplify_xor (gfc_expr *x, gfc_expr *y) |
| 9250 |
{ |
| 9251 |
gfc_expr *result; |
| 9252 |
int kind; |
| 9253 |
|
| 9254 |
if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT) |
| 9255 |
return NULL; |
| 9256 |
|
| 9257 |
kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind; |
| 9258 |
|
| 9259 |
switch (x->ts.type) |
| 9260 |
{ |
| 9261 |
case BT_INTEGER: |
| 9262 |
result = gfc_get_constant_expr (BT_INTEGER, kind, &x->where); |
| 9263 |
mpz_xor (result->value.integer, x->value.integer, y->value.integer); |
| 9264 |
return range_check (result, "XOR"); |
| 9265 |
|
| 9266 |
case BT_LOGICAL: |
| 9267 |
return gfc_get_logical_expr (kind, &x->where, |
| 9268 |
(x->value.logical && !y->value.logical) |
| 9269 |
|| (!x->value.logical && y->value.logical)); |
| 9270 |
|
| 9271 |
default: |
| 9272 |
gcc_unreachable (); |
| 9273 |
} |
| 9274 |
} |
| 9275 |
|
| 9276 |
|
| 9277 |
/****************** Constant simplification *****************/ |
| 9278 |
|
| 9279 |
/* Master function to convert one constant to another. While this is |
| 9280 |
used as a simplification function, it requires the destination type |
| 9281 |
and kind information which is supplied by a special case in |
| 9282 |
do_simplify(). */ |
| 9283 |
|
| 9284 |
gfc_expr * |
| 9285 |
gfc_convert_constant (gfc_expr *e, bt type, int kind) |
| 9286 |
{ |
| 9287 |
gfc_expr *result, *(*f) (gfc_expr *, int); |
| 9288 |
gfc_constructor *c, *t; |
| 9289 |
|
| 9290 |
switch (e->ts.type) |
| 9291 |
{ |
| 9292 |
case BT_INTEGER: |
| 9293 |
switch (type) |
| 9294 |
{ |
| 9295 |
case BT_INTEGER: |
| 9296 |
f = gfc_int2int; |
| 9297 |
break; |
| 9298 |
case BT_UNSIGNED: |
| 9299 |
f = gfc_int2uint; |
| 9300 |
break; |
| 9301 |
case BT_REAL: |
| 9302 |
f = gfc_int2real; |
| 9303 |
break; |
| 9304 |
case BT_COMPLEX: |
| 9305 |
f = gfc_int2complex; |
| 9306 |
break; |
| 9307 |
case BT_LOGICAL: |
| 9308 |
f = gfc_int2log; |
| 9309 |
break; |
| 9310 |
default: |
| 9311 |
goto oops; |
| 9312 |
} |
| 9313 |
break; |
| 9314 |
|
| 9315 |
case BT_UNSIGNED: |
| 9316 |
switch (type) |
| 9317 |
{ |
| 9318 |
case BT_INTEGER: |
| 9319 |
f = gfc_uint2int; |
| 9320 |
break; |
| 9321 |
case BT_UNSIGNED: |
| 9322 |
f = gfc_uint2uint; |
| 9323 |
break; |
| 9324 |
case BT_REAL: |
| 9325 |
f = gfc_uint2real; |
| 9326 |
break; |
| 9327 |
case BT_COMPLEX: |
| 9328 |
f = gfc_uint2complex; |
| 9329 |
break; |
| 9330 |
case BT_LOGICAL: |
| 9331 |
f = gfc_uint2log; |
| 9332 |
break; |
| 9333 |
default: |
| 9334 |
goto oops; |
| 9335 |
} |
| 9336 |
break; |
| 9337 |
|
| 9338 |
case BT_REAL: |
| 9339 |
switch (type) |
| 9340 |
{ |
| 9341 |
case BT_INTEGER: |
| 9342 |
f = gfc_real2int; |
| 9343 |
break; |
| 9344 |
case BT_UNSIGNED: |
| 9345 |
f = gfc_real2uint; |
| 9346 |
break; |
| 9347 |
case BT_REAL: |
| 9348 |
f = gfc_real2real; |
| 9349 |
break; |
| 9350 |
case BT_COMPLEX: |
| 9351 |
f = gfc_real2complex; |
| 9352 |
break; |
| 9353 |
default: |
| 9354 |
goto oops; |
| 9355 |
} |
| 9356 |
break; |
| 9357 |
|
| 9358 |
case BT_COMPLEX: |
| 9359 |
switch (type) |
| 9360 |
{ |
| 9361 |
case BT_INTEGER: |
| 9362 |
f = gfc_complex2int; |
| 9363 |
break; |
| 9364 |
case BT_UNSIGNED: |
| 9365 |
f = gfc_complex2uint; |
| 9366 |
break; |
| 9367 |
case BT_REAL: |
| 9368 |
f = gfc_complex2real; |
| 9369 |
break; |
| 9370 |
case BT_COMPLEX: |
| 9371 |
f = gfc_complex2complex; |
| 9372 |
break; |
| 9373 |
|
| 9374 |
default: |
| 9375 |
goto oops; |
| 9376 |
} |
| 9377 |
break; |
| 9378 |
|
| 9379 |
case BT_LOGICAL: |
| 9380 |
switch (type) |
| 9381 |
{ |
| 9382 |
case BT_INTEGER: |
| 9383 |
f = gfc_log2int; |
| 9384 |
break; |
| 9385 |
case BT_UNSIGNED: |
| 9386 |
f = gfc_log2uint; |
| 9387 |
break; |
| 9388 |
case BT_LOGICAL: |
| 9389 |
f = gfc_log2log; |
| 9390 |
break; |
| 9391 |
default: |
| 9392 |
goto oops; |
| 9393 |
} |
| 9394 |
break; |
| 9395 |
|
| 9396 |
case BT_HOLLERITH: |
| 9397 |
switch (type) |
| 9398 |
{ |
| 9399 |
case BT_INTEGER: |
| 9400 |
f = gfc_hollerith2int; |
| 9401 |
break; |
| 9402 |
|
| 9403 |
/* Hollerith is for legacy code, we do not currently support |
| 9404 |
converting this to UNSIGNED. */ |
| 9405 |
case BT_UNSIGNED: |
| 9406 |
goto oops; |
| 9407 |
|
| 9408 |
case BT_REAL: |
| 9409 |
f = gfc_hollerith2real; |
| 9410 |
break; |
| 9411 |
|
| 9412 |
case BT_COMPLEX: |
| 9413 |
f = gfc_hollerith2complex; |
| 9414 |
break; |
| 9415 |
|
| 9416 |
case BT_CHARACTER: |
| 9417 |
f = gfc_hollerith2character; |
| 9418 |
break; |
| 9419 |
|
| 9420 |
case BT_LOGICAL: |
| 9421 |
f = gfc_hollerith2logical; |
| 9422 |
break; |
| 9423 |
|
| 9424 |
default: |
| 9425 |
goto oops; |
| 9426 |
} |
| 9427 |
break; |
| 9428 |
|
| 9429 |
case BT_CHARACTER: |
| 9430 |
switch (type) |
| 9431 |
{ |
| 9432 |
case BT_INTEGER: |
| 9433 |
f = gfc_character2int; |
| 9434 |
break; |
| 9435 |
|
| 9436 |
case BT_UNSIGNED: |
| 9437 |
goto oops; |
| 9438 |
|
| 9439 |
case BT_REAL: |
| 9440 |
f = gfc_character2real; |
| 9441 |
break; |
| 9442 |
|
| 9443 |
case BT_COMPLEX: |
| 9444 |
f = gfc_character2complex; |
| 9445 |
break; |
| 9446 |
|
| 9447 |
case BT_CHARACTER: |
| 9448 |
f = gfc_character2character; |
| 9449 |
break; |
| 9450 |
|
| 9451 |
case BT_LOGICAL: |
| 9452 |
f = gfc_character2logical; |
| 9453 |
break; |
| 9454 |
|
| 9455 |
default: |
| 9456 |
goto oops; |
| 9457 |
} |
| 9458 |
break; |
| 9459 |
|
| 9460 |
default: |
| 9461 |
oops: |
| 9462 |
return &gfc_bad_expr; |
| 9463 |
} |
| 9464 |
|
| 9465 |
result = NULL; |
| 9466 |
|
| 9467 |
switch (e->expr_type) |
| 9468 |
{ |
| 9469 |
case EXPR_CONSTANT: |
| 9470 |
result = f (e, kind); |
| 9471 |
if (result == NULL) |
| 9472 |
return &gfc_bad_expr; |
| 9473 |
break; |
| 9474 |
|
| 9475 |
case EXPR_ARRAY: |
| 9476 |
if (!gfc_is_constant_expr (e)) |
| 9477 |
break; |
| 9478 |
|
| 9479 |
result = gfc_get_array_expr (type, kind, &e->where); |
| 9480 |
result->shape = gfc_copy_shape (e->shape, e->rank); |
| 9481 |
result->rank = e->rank; |
| 9482 |
|
| 9483 |
for (c = gfc_constructor_first (e->value.constructor); |
| 9484 |
c; c = gfc_constructor_next (c)) |
| 9485 |
{ |
| 9486 |
gfc_expr *tmp; |
| 9487 |
if (c->iterator == NULL) |
| 9488 |
{ |
| 9489 |
if (c->expr->expr_type == EXPR_ARRAY) |
| 9490 |
tmp = gfc_convert_constant (c->expr, type, kind); |
| 9491 |
else if (c->expr->expr_type == EXPR_OP) |
| 9492 |
{ |
| 9493 |
if (!gfc_simplify_expr (c->expr, 1)) |
| 9494 |
return &gfc_bad_expr; |
| 9495 |
tmp = f (c->expr, kind); |
| 9496 |
} |
| 9497 |
else |
| 9498 |
tmp = f (c->expr, kind); |
| 9499 |
} |
| 9500 |
else |
| 9501 |
tmp = gfc_convert_constant (c->expr, type, kind); |
| 9502 |
|
| 9503 |
if (tmp == NULL || tmp == &gfc_bad_expr) |
| 9504 |
{ |
| 9505 |
gfc_free_expr (result); |
| 9506 |
return NULL; |
| 9507 |
} |
| 9508 |
|
| 9509 |
t = gfc_constructor_append_expr (&result->value.constructor, |
| 9510 |
tmp, &c->where); |
| 9511 |
if (c->iterator) |
| 9512 |
t->iterator = gfc_copy_iterator (c->iterator); |
| 9513 |
} |
| 9514 |
|
| 9515 |
break; |
| 9516 |
|
| 9517 |
default: |
| 9518 |
break; |
| 9519 |
} |
| 9520 |
|
| 9521 |
return result; |
| 9522 |
} |
| 9523 |
|
| 9524 |
|
| 9525 |
/* Function for converting character constants. */ |
| 9526 |
gfc_expr * |
| 9527 |
gfc_convert_char_constant (gfc_expr *e, bt type ATTRIBUTE_UNUSED, int kind) |
| 9528 |
{ |
| 9529 |
gfc_expr *result; |
| 9530 |
int i; |
| 9531 |
|
| 9532 |
if (!gfc_is_constant_expr (e)) |
| 9533 |
return NULL; |
| 9534 |
|
| 9535 |
if (e->expr_type == EXPR_CONSTANT) |
| 9536 |
{ |
| 9537 |
/* Simple case of a scalar. */ |
| 9538 |
result = gfc_get_constant_expr (BT_CHARACTER, kind, &e->where); |
| 9539 |
if (result == NULL) |
| 9540 |
return &gfc_bad_expr; |
| 9541 |
|
| 9542 |
result->value.character.length = e->value.character.length; |
| 9543 |
result->value.character.string |
| 9544 |
= gfc_get_wide_string (e->value.character.length + 1); |
| 9545 |
memcpy (result->value.character.string, e->value.character.string, |
| 9546 |
(e->value.character.length + 1) * sizeof (gfc_char_t)); |
| 9547 |
|
| 9548 |
/* Check we only have values representable in the destination kind. */ |
| 9549 |
for (i = 0; i < result->value.character.length; i++) |
| 9550 |
if (!gfc_check_character_range (result->value.character.string[i], |
| 9551 |
kind)) |
| 9552 |
{ |
| 9553 |
gfc_error ("Character %qs in string at %L cannot be converted " |
| 9554 |
"into character kind %d", |
| 9555 |
gfc_print_wide_char (result->value.character.string[i]), |
| 9556 |
&e->where, kind); |
| 9557 |
gfc_free_expr (result); |
| 9558 |
return &gfc_bad_expr; |
| 9559 |
} |
| 9560 |
|
| 9561 |
return result; |
| 9562 |
} |
| 9563 |
else if (e->expr_type == EXPR_ARRAY) |
| 9564 |
{ |
| 9565 |
/* For an array constructor, we convert each constructor element. */ |
| 9566 |
gfc_constructor *c; |
| 9567 |
|
| 9568 |
result = gfc_get_array_expr (type, kind, &e->where); |
| 9569 |
result->shape = gfc_copy_shape (e->shape, e->rank); |
| 9570 |
result->rank = e->rank; |
| 9571 |
result->ts.u.cl = e->ts.u.cl; |
| 9572 |
|
| 9573 |
for (c = gfc_constructor_first (e->value.constructor); |
| 9574 |
c; c = gfc_constructor_next (c)) |
| 9575 |
{ |
| 9576 |
gfc_expr *tmp = gfc_convert_char_constant (c->expr, type, kind); |
| 9577 |
if (tmp == &gfc_bad_expr) |
| 9578 |
{ |
| 9579 |
gfc_free_expr (result); |
| 9580 |
return &gfc_bad_expr; |
| 9581 |
} |
| 9582 |
|
| 9583 |
if (tmp == NULL) |
| 9584 |
{ |
| 9585 |
gfc_free_expr (result); |
| 9586 |
return NULL; |
| 9587 |
} |
| 9588 |
|
| 9589 |
gfc_constructor_append_expr (&result->value.constructor, |
| 9590 |
tmp, &c->where); |
| 9591 |
} |
| 9592 |
|
| 9593 |
return result; |
| 9594 |
} |
| 9595 |
else |
| 9596 |
return NULL; |
| 9597 |
} |
| 9598 |
|
| 9599 |
|
| 9600 |
gfc_expr * |
| 9601 |
gfc_simplify_compiler_options (void) |
| 9602 |
{ |
| 9603 |
char *str; |
| 9604 |
gfc_expr *result; |
| 9605 |
|
| 9606 |
str = gfc_get_option_string (); |
| 9607 |
result = gfc_get_character_expr (gfc_default_character_kind, |
| 9608 |
&gfc_current_locus, str, strlen (str)); |
| 9609 |
free (str); |
| 9610 |
return result; |
| 9611 |
} |
| 9612 |
|
| 9613 |
|
| 9614 |
gfc_expr * |
| 9615 |
gfc_simplify_compiler_version (void) |
| 9616 |
{ |
| 9617 |
char *buffer; |
| 9618 |
size_t len; |
| 9619 |
|
| 9620 |
len = strlen ("GCC version ") + strlen (version_string); |
| 9621 |
buffer = XALLOCAVEC (char, len + 1); |
| 9622 |
snprintf (buffer, len + 1, "GCC version %s", version_string); |
| 9623 |
return gfc_get_character_expr (gfc_default_character_kind, |
| 9624 |
&gfc_current_locus, buffer, len); |
| 9625 |
} |
| 9626 |
|
| 9627 |
/* Simplification routines for intrinsics of IEEE modules. */ |
| 9628 |
|
| 9629 |
gfc_expr * |
| 9630 |
simplify_ieee_selected_real_kind (gfc_expr *expr) |
| 9631 |
{ |
| 9632 |
gfc_actual_arglist *arg; |
| 9633 |
gfc_expr *p = NULL, *q = NULL, *rdx = NULL; |
| 9634 |
|
| 9635 |
arg = expr->value.function.actual; |
| 9636 |
p = arg->expr; |
| 9637 |
if (arg->next) |
| 9638 |
{ |
| 9639 |
q = arg->next->expr; |
| 9640 |
if (arg->next->next) |
| 9641 |
rdx = arg->next->next->expr; |
| 9642 |
} |
| 9643 |
|
| 9644 |
/* Currently, if IEEE is supported and this module is built, it means |
| 9645 |
all our floating-point types conform to IEEE. Hence, we simply handle |
| 9646 |
IEEE_SELECTED_REAL_KIND like SELECTED_REAL_KIND. */ |
| 9647 |
return gfc_simplify_selected_real_kind (p, q, rdx); |
| 9648 |
} |
| 9649 |
|
| 9650 |
gfc_expr * |
| 9651 |
simplify_ieee_support (gfc_expr *expr) |
| 9652 |
{ |
| 9653 |
/* We consider that if the IEEE modules are loaded, we have full support |
| 9654 |
for flags, halting and rounding, which are the three functions |
| 9655 |
(IEEE_SUPPORT_{FLAG,HALTING,ROUNDING}) allowed in constant |
| 9656 |
expressions. One day, we will need libgfortran to detect support and |
| 9657 |
communicate it back to us, allowing for partial support. */ |
| 9658 |
|
| 9659 |
return gfc_get_logical_expr (gfc_default_logical_kind, &expr->where, |
| 9660 |
true); |
| 9661 |
} |
| 9662 |
|
| 9663 |
bool |
| 9664 |
matches_ieee_function_name (gfc_symbol *sym, const char *name) |
| 9665 |
{ |
| 9666 |
int n = strlen(name); |
| 9667 |
|
| 9668 |
if (!strncmp(sym->name, name, n)) |
| 9669 |
return true; |
| 9670 |
|
| 9671 |
/* If a generic was used and renamed, we need more work to find out. |
| 9672 |
Compare the specific name. */ |
| 9673 |
if (sym->generic && !strncmp(sym->generic->sym->name, name, n)) |
| 9674 |
return true; |
| 9675 |
|
| 9676 |
return false; |
| 9677 |
} |
| 9678 |
|
| 9679 |
gfc_expr * |
| 9680 |
gfc_simplify_ieee_functions (gfc_expr *expr) |
| 9681 |
{ |
| 9682 |
gfc_symbol* sym = expr->symtree->n.sym; |
| 9683 |
|
| 9684 |
if (matches_ieee_function_name(sym, "ieee_selected_real_kind")) |
| 9685 |
return simplify_ieee_selected_real_kind (expr); |
| 9686 |
else if (matches_ieee_function_name(sym, "ieee_support_flag") |
| 9687 |
|| matches_ieee_function_name(sym, "ieee_support_halting") |
| 9688 |
|| matches_ieee_function_name(sym, "ieee_support_rounding")) |
| 9689 |
return simplify_ieee_support (expr); |
| 9690 |
else |
| 9691 |
return NULL; |
| 9692 |
} |