simplify.c 154 KB
Newer Older
1
/* Simplify intrinsic functions at compile-time.
2 3
   Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
   2010 Free Software Foundation, Inc.
4 5
   Contributed by Andy Vaught & Katherine Holcomb

6
This file is part of GCC.
7

8 9
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
10
Software Foundation; either version 3, or (at your option) any later
11
version.
12

13 14 15 16
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.
17 18

You should have received a copy of the GNU General Public License
19 20
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */
21 22 23 24 25 26 27

#include "config.h"
#include "system.h"
#include "flags.h"
#include "gfortran.h"
#include "arith.h"
#include "intrinsic.h"
Paul Thomas committed
28
#include "target-memory.h"
Jerry DeLisle committed
29
#include "constructor.h"
30
#include "version.h"  /* For version_string.  */
31

32

33 34 35 36 37 38 39 40 41 42 43 44 45 46
gfc_expr gfc_bad_expr;


/* Note that 'simplification' is not just transforming expressions.
   For functions that are not simplified at compile time, range
   checking is done if possible.

   The return convention is that each simplification function returns:

     A new expression node corresponding to the simplified arguments.
     The original arguments are destroyed by the caller, and must not
     be a part of the new expression.

     NULL pointer indicating that no simplification was possible and
Jerry DeLisle committed
47
     the original expression should remain intact.
48 49

     An expression pointer to gfc_bad_expr (a static placeholder)
Jerry DeLisle committed
50 51 52
     indicating that some error has prevented simplification.  The
     error is generated within the function and should be propagated
     upwards
53 54 55 56 57 58 59 60

   By the time a simplification function gets control, it has been
   decided that the function call is really supposed to be the
   intrinsic.  No type checking is strictly necessary, since only
   valid types will be passed on.  On the other hand, a simplification
   subroutine may have to look at the type of an argument as part of
   its processing.

Jerry DeLisle committed
61 62
   Array arguments are only passed to these subroutines that implement
   the simplification of transformational intrinsics.
63 64 65 66 67 68 69 70 71

   The functions in this file don't have much comment with them, but
   everything is reasonably straight-forward.  The Standard, chapter 13
   is the best comment you'll find for this file anyway.  */

/* Range checks an expression node.  If all goes well, returns the
   node, otherwise returns &gfc_bad_expr and frees the node.  */

static gfc_expr *
72
range_check (gfc_expr *result, const char *name)
73
{
74 75 76
  if (result == NULL)
    return &gfc_bad_expr;

77 78 79
  if (result->expr_type != EXPR_CONSTANT)
    return result;

80 81 82 83 84 85
  switch (gfc_range_check (result))
    {
      case ARITH_OK:
	return result;
 
      case ARITH_OVERFLOW:
86 87
	gfc_error ("Result of %s overflows its kind at %L", name,
		   &result->where);
88 89 90
	break;

      case ARITH_UNDERFLOW:
91 92
	gfc_error ("Result of %s underflows its kind at %L", name,
		   &result->where);
93 94 95 96 97 98 99
	break;

      case ARITH_NAN:
	gfc_error ("Result of %s is NaN at %L", name, &result->where);
	break;

      default:
100 101
	gfc_error ("Result of %s gives range error for its kind at %L", name,
		   &result->where);
102 103 104
	break;
    }

105 106 107 108 109 110 111 112 113
  gfc_free_expr (result);
  return &gfc_bad_expr;
}


/* A helper function that gets an optional and possibly missing
   kind parameter.  Returns the kind, -1 if something went wrong.  */

static int
114
get_kind (bt type, gfc_expr *k, const char *name, int default_kind)
115 116 117 118 119 120 121 122 123 124 125 126 127 128
{
  int kind;

  if (k == NULL)
    return default_kind;

  if (k->expr_type != EXPR_CONSTANT)
    {
      gfc_error ("KIND parameter of %s at %L must be an initialization "
		 "expression", name, &k->where);
      return -1;
    }

  if (gfc_extract_int (k, &kind) != NULL
129
      || gfc_validate_kind (type, kind, true) < 0)
130 131 132 133 134 135 136 137 138
    {
      gfc_error ("Invalid KIND parameter of %s at %L", name, &k->where);
      return -1;
    }

  return kind;
}


139 140 141 142
/* Converts an mpz_t signed variable into an unsigned one, assuming
   two's complement representations and a binary width of bitsize.
   The conversion is a no-op unless x is negative; otherwise, it can
   be accomplished by masking out the high bits.  */
143 144

static void
145
convert_mpz_to_unsigned (mpz_t x, int bitsize)
146 147 148
{
  mpz_t mask;

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
  if (mpz_sgn (x) < 0)
    {
      /* Confirm that no bits above the signed range are unset.  */
      gcc_assert (mpz_scan0 (x, bitsize-1) == ULONG_MAX);

      mpz_init_set_ui (mask, 1);
      mpz_mul_2exp (mask, mask, bitsize);
      mpz_sub_ui (mask, mask, 1);

      mpz_and (x, x, mask);

      mpz_clear (mask);
    }
  else
    {
      /* Confirm that no bits above the signed range are set.  */
      gcc_assert (mpz_scan1 (x, bitsize-1) == ULONG_MAX);
    }
}


/* Converts an mpz_t unsigned variable into a signed one, assuming
   two's complement representations and a binary width of bitsize.
   If the bitsize-1 bit is set, this is taken as a sign bit and
   the number is converted to the corresponding negative number.  */

static void
convert_mpz_to_signed (mpz_t x, int bitsize)
{
  mpz_t mask;

  /* Confirm that no bits above the unsigned range are set.  */
  gcc_assert (mpz_scan1 (x, bitsize) == ULONG_MAX);

183 184
  if (mpz_tstbit (x, bitsize - 1) == 1)
    {
185 186 187
      mpz_init_set_ui (mask, 1);
      mpz_mul_2exp (mask, mask, bitsize);
      mpz_sub_ui (mask, mask, 1);
188 189

      /* We negate the number by hand, zeroing the high bits, that is
190 191 192
	 make it the corresponding positive number, and then have it
	 negated by GMP, giving the correct representation of the
	 negative number.  */
193 194 195 196 197 198 199 200 201 202
      mpz_com (x, x);
      mpz_add_ui (x, x, 1);
      mpz_and (x, x, mask);

      mpz_neg (x, x);

      mpz_clear (mask);
    }
}

Jerry DeLisle committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

/* In-place convert BOZ to REAL of the specified kind.  */

static gfc_expr *
convert_boz (gfc_expr *x, int kind)
{
  if (x && x->ts.type == BT_INTEGER && x->is_boz)
    {
      gfc_typespec ts;
      gfc_clear_ts (&ts);
      ts.type = BT_REAL;
      ts.kind = kind;

      if (!gfc_convert_boz (x, &ts))
	return &gfc_bad_expr;
    }

  return x;
}


224 225 226 227 228 229 230 231 232 233 234 235 236
/* Test that the expression is an constant array.  */

static bool
is_constant_array_expr (gfc_expr *e)
{
  gfc_constructor *c;

  if (e == NULL)
    return true;

  if (e->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (e))
    return false;

Jerry DeLisle committed
237 238
  for (c = gfc_constructor_first (e->value.constructor);
       c; c = gfc_constructor_next (c))
239 240
    if (c->expr->expr_type != EXPR_CONSTANT
	  && c->expr->expr_type != EXPR_STRUCTURE)
241 242 243 244 245 246
      return false;

  return true;
}


247 248 249 250 251 252 253
/* Initialize a transformational result expression with a given value.  */

static void
init_result_expr (gfc_expr *e, int init, gfc_expr *array)
{
  if (e && e->expr_type == EXPR_ARRAY)
    {
Jerry DeLisle committed
254
      gfc_constructor *ctor = gfc_constructor_first (e->value.constructor);
255 256 257
      while (ctor)
	{
	  init_result_expr (ctor->expr, init, array);
Jerry DeLisle committed
258
	  ctor = gfc_constructor_next (ctor);
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
	}
    }
  else if (e && e->expr_type == EXPR_CONSTANT)
    {
      int i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
      int length;
      gfc_char_t *string;

      switch (e->ts.type)
	{
	  case BT_LOGICAL:
	    e->value.logical = (init ? 1 : 0);
	    break;

	  case BT_INTEGER:
	    if (init == INT_MIN)
	      mpz_set (e->value.integer, gfc_integer_kinds[i].min_int);
	    else if (init == INT_MAX)
	      mpz_set (e->value.integer, gfc_integer_kinds[i].huge);
	    else
	      mpz_set_si (e->value.integer, init);
	    break;

	  case BT_REAL:
	    if (init == INT_MIN)
	      {
		mpfr_set (e->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
		mpfr_neg (e->value.real, e->value.real, GFC_RND_MODE);
	      }
	    else if (init == INT_MAX)
	      mpfr_set (e->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
	    else
	      mpfr_set_si (e->value.real, init, GFC_RND_MODE);
	    break;

	  case BT_COMPLEX:
295
	    mpc_set_si (e->value.complex, init, GFC_MPC_RND_MODE);
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
	    break;

	  case BT_CHARACTER:
	    if (init == INT_MIN)
	      {
		gfc_expr *len = gfc_simplify_len (array, NULL);
		gfc_extract_int (len, &length);
		string = gfc_get_wide_string (length + 1);
		gfc_wide_memset (string, 0, length);
	      }
	    else if (init == INT_MAX)
	      {
		gfc_expr *len = gfc_simplify_len (array, NULL);
		gfc_extract_int (len, &length);
		string = gfc_get_wide_string (length + 1);
		gfc_wide_memset (string, 255, length);
	      }
	    else
	      {
		length = 0;
		string = gfc_get_wide_string (1);
	      }

	    string[length] = '\0';
	    e->value.character.length = length;
	    e->value.character.string = string;
	    break;

	  default:
	    gcc_unreachable();
	}
    }
  else
    gcc_unreachable();
}


/* Helper function for gfc_simplify_dot_product() and gfc_simplify_matmul.  */

static gfc_expr *
Jerry DeLisle committed
336 337
compute_dot_product (gfc_expr *matrix_a, int stride_a, int offset_a,
		     gfc_expr *matrix_b, int stride_b, int offset_b)
338
{
Jerry DeLisle committed
339
  gfc_expr *result, *a, *b;
340

Jerry DeLisle committed
341 342
  result = gfc_get_constant_expr (matrix_a->ts.type, matrix_a->ts.kind,
				  &matrix_a->where);
343 344
  init_result_expr (result, 0, NULL);

Jerry DeLisle committed
345 346 347
  a = gfc_constructor_lookup_expr (matrix_a->value.constructor, offset_a);
  b = gfc_constructor_lookup_expr (matrix_b->value.constructor, offset_b);
  while (a && b)
348 349 350 351 352 353 354
    {
      /* Copying of expressions is required as operands are free'd
	 by the gfc_arith routines.  */
      switch (result->ts.type)
	{
	  case BT_LOGICAL:
	    result = gfc_or (result,
Jerry DeLisle committed
355 356
			     gfc_and (gfc_copy_expr (a),
				      gfc_copy_expr (b)));
357 358 359 360 361 362
	    break;

	  case BT_INTEGER:
	  case BT_REAL:
	  case BT_COMPLEX:
	    result = gfc_add (result,
Jerry DeLisle committed
363 364
			      gfc_multiply (gfc_copy_expr (a),
					    gfc_copy_expr (b)));
365 366 367 368 369 370
	    break;

	  default:
	    gcc_unreachable();
	}

Jerry DeLisle committed
371 372 373 374 375
      offset_a += stride_a;
      a = gfc_constructor_lookup_expr (matrix_a->value.constructor, offset_a);

      offset_b += stride_b;
      b = gfc_constructor_lookup_expr (matrix_b->value.constructor, offset_b);
376 377 378 379 380
    }

  return result;
}

381 382 383 384 385 386 387 388 389 390 391 392

/* Build a result expression for transformational intrinsics, 
   depending on DIM. */

static gfc_expr *
transformational_result (gfc_expr *array, gfc_expr *dim, bt type,
			 int kind, locus* where)
{
  gfc_expr *result;
  int i, nelem;

  if (!dim || array->rank == 1)
Jerry DeLisle committed
393
    return gfc_get_constant_expr (type, kind, where);
394

Jerry DeLisle committed
395
  result = gfc_get_array_expr (type, kind, where);
396 397 398 399 400 401 402 403 404 405 406
  result->shape = gfc_copy_shape_excluding (array->shape, array->rank, dim);
  result->rank = array->rank - 1;

  /* gfc_array_size() would count the number of elements in the constructor,
     we have not built those yet.  */
  nelem = 1;
  for  (i = 0; i < result->rank; ++i)
    nelem *= mpz_get_ui (result->shape[i]);

  for (i = 0; i < nelem; ++i)
    {
Jerry DeLisle committed
407 408 409
      gfc_constructor_append_expr (&result->value.constructor,
				   gfc_get_constant_expr (type, kind, where),
				   NULL);
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
    }

  return result;
}


typedef gfc_expr* (*transformational_op)(gfc_expr*, gfc_expr*);

/* Wrapper function, implements 'op1 += 1'. Only called if MASK
   of COUNT intrinsic is .TRUE..

   Interface and implimentation mimics arith functions as
   gfc_add, gfc_multiply, etc.  */

static gfc_expr* gfc_count (gfc_expr *op1, gfc_expr *op2)
{
  gfc_expr *result;

  gcc_assert (op1->ts.type == BT_INTEGER);
  gcc_assert (op2->ts.type == BT_LOGICAL);
  gcc_assert (op2->value.logical);

  result = gfc_copy_expr (op1);
  mpz_add_ui (result->value.integer, result->value.integer, 1);

  gfc_free_expr (op1);
  gfc_free_expr (op2);
  return result;
}


/* Transforms an ARRAY with operation OP, according to MASK, to a
   scalar RESULT. E.g. called if

     REAL, PARAMETER :: array(n, m) = ...
     REAL, PARAMETER :: s = SUM(array)

  where OP == gfc_add().  */

static gfc_expr *
simplify_transformation_to_scalar (gfc_expr *result, gfc_expr *array, gfc_expr *mask,
				   transformational_op op)
{
  gfc_expr *a, *m;
  gfc_constructor *array_ctor, *mask_ctor;

  /* Shortcut for constant .FALSE. MASK.  */
  if (mask
      && mask->expr_type == EXPR_CONSTANT
      && !mask->value.logical)
    return result;

Jerry DeLisle committed
462
  array_ctor = gfc_constructor_first (array->value.constructor);
463 464
  mask_ctor = NULL;
  if (mask && mask->expr_type == EXPR_ARRAY)
Jerry DeLisle committed
465
    mask_ctor = gfc_constructor_first (mask->value.constructor);
466 467 468 469

  while (array_ctor)
    {
      a = array_ctor->expr;
Jerry DeLisle committed
470
      array_ctor = gfc_constructor_next (array_ctor);
471 472 473 474 475

      /* A constant MASK equals .TRUE. here and can be ignored.  */
      if (mask_ctor)
	{
	  m = mask_ctor->expr;
Jerry DeLisle committed
476
	  mask_ctor = gfc_constructor_next (mask_ctor);
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
	  if (!m->value.logical)
	    continue;
	}

      result = op (result, gfc_copy_expr (a));
    }

  return result;
}

/* Transforms an ARRAY with operation OP, according to MASK, to an
   array RESULT. E.g. called if

     REAL, PARAMETER :: array(n, m) = ...
     REAL, PARAMETER :: s(n) = PROD(array, DIM=1)

493
  where OP == gfc_multiply(). The result might be post processed using post_op. */ 
494 495 496

static gfc_expr *
simplify_transformation_to_array (gfc_expr *result, gfc_expr *array, gfc_expr *dim,
497 498
				  gfc_expr *mask, transformational_op op,
				  transformational_op post_op)
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
{
  mpz_t size;
  int done, i, n, arraysize, resultsize, dim_index, dim_extent, dim_stride;
  gfc_expr **arrayvec, **resultvec, **base, **src, **dest;
  gfc_constructor *array_ctor, *mask_ctor, *result_ctor;

  int count[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS],
      sstride[GFC_MAX_DIMENSIONS], dstride[GFC_MAX_DIMENSIONS],
      tmpstride[GFC_MAX_DIMENSIONS];

  /* Shortcut for constant .FALSE. MASK.  */
  if (mask
      && mask->expr_type == EXPR_CONSTANT
      && !mask->value.logical)
    return result;

  /* Build an indexed table for array element expressions to minimize
     linked-list traversal. Masked elements are set to NULL.  */
  gfc_array_size (array, &size);
  arraysize = mpz_get_ui (size);

  arrayvec = (gfc_expr**) gfc_getmem (sizeof (gfc_expr*) * arraysize);

Jerry DeLisle committed
522
  array_ctor = gfc_constructor_first (array->value.constructor);
523 524
  mask_ctor = NULL;
  if (mask && mask->expr_type == EXPR_ARRAY)
Jerry DeLisle committed
525
    mask_ctor = gfc_constructor_first (mask->value.constructor);
526 527 528 529

  for (i = 0; i < arraysize; ++i)
    {
      arrayvec[i] = array_ctor->expr;
Jerry DeLisle committed
530
      array_ctor = gfc_constructor_next (array_ctor);
531 532 533 534 535 536

      if (mask_ctor)
	{
	  if (!mask_ctor->expr->value.logical)
	    arrayvec[i] = NULL;

Jerry DeLisle committed
537
	  mask_ctor = gfc_constructor_next (mask_ctor);
538 539 540 541 542 543 544 545 546
	}
    }

  /* Same for the result expression.  */
  gfc_array_size (result, &size);
  resultsize = mpz_get_ui (size);
  mpz_clear (size);

  resultvec = (gfc_expr**) gfc_getmem (sizeof (gfc_expr*) * resultsize);
Jerry DeLisle committed
547
  result_ctor = gfc_constructor_first (result->value.constructor);
548 549 550
  for (i = 0; i < resultsize; ++i)
    {
      resultvec[i] = result_ctor->expr;
Jerry DeLisle committed
551
      result_ctor = gfc_constructor_next (result_ctor);
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
    }

  gfc_extract_int (dim, &dim_index);
  dim_index -= 1;               /* zero-base index */
  dim_extent = 0;
  dim_stride = 0;

  for (i = 0, n = 0; i < array->rank; ++i)
    {
      count[i] = 0;
      tmpstride[i] = (i == 0) ? 1 : tmpstride[i-1] * mpz_get_si (array->shape[i-1]);
      if (i == dim_index)
	{
	  dim_extent = mpz_get_si (array->shape[i]);
	  dim_stride = tmpstride[i];
	  continue;
	}

      extent[n] = mpz_get_si (array->shape[i]);
      sstride[n] = tmpstride[i];
      dstride[n] = (n == 0) ? 1 : dstride[n-1] * extent[n-1];
      n += 1;
    }

  done = false;
  base = arrayvec;
  dest = resultvec;
  while (!done)
    {
      for (src = base, n = 0; n < dim_extent; src += dim_stride, ++n)
	if (*src)
	  *dest = op (*dest, gfc_copy_expr (*src));

      count[0]++;
      base += sstride[0];
      dest += dstride[0];

      n = 0;
      while (!done && count[n] == extent[n])
	{
	  count[n] = 0;
	  base -= sstride[n] * extent[n];
	  dest -= dstride[n] * extent[n];

	  n++;
	  if (n < result->rank)
	    {
	      count [n]++;
	      base += sstride[n];
	      dest += dstride[n];
	    }
	  else
	    done = true;
       }
    }

  /* Place updated expression in result constructor.  */
Jerry DeLisle committed
609
  result_ctor = gfc_constructor_first (result->value.constructor);
610 611
  for (i = 0; i < resultsize; ++i)
    {
612 613 614 615
      if (post_op)
	result_ctor->expr = post_op (result_ctor->expr, resultvec[i]);
      else
	result_ctor->expr = resultvec[i];
Jerry DeLisle committed
616
      result_ctor = gfc_constructor_next (result_ctor);
617 618 619 620 621 622 623 624
    }

  gfc_free (arrayvec);
  gfc_free (resultvec);
  return result;
}


625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
static gfc_expr *
simplify_transformation (gfc_expr *array, gfc_expr *dim, gfc_expr *mask,
			 int init_val, transformational_op op)
{
  gfc_expr *result;

  if (!is_constant_array_expr (array)
      || !gfc_is_constant_expr (dim))
    return NULL;

  if (mask
      && !is_constant_array_expr (mask)
      && mask->expr_type != EXPR_CONSTANT)
    return NULL;

  result = transformational_result (array, dim, array->ts.type,
				    array->ts.kind, &array->where);
  init_result_expr (result, init_val, NULL);

  return !dim || array->rank == 1 ?
    simplify_transformation_to_scalar (result, array, mask, op) :
    simplify_transformation_to_array (result, array, dim, mask, op, NULL);
}

649

650 651 652
/********************** Simplification functions *****************************/

gfc_expr *
653
gfc_simplify_abs (gfc_expr *e)
654 655 656 657 658 659 660 661
{
  gfc_expr *result;

  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

  switch (e->ts.type)
    {
Jerry DeLisle committed
662 663 664 665
      case BT_INTEGER:
	result = gfc_get_constant_expr (BT_INTEGER, e->ts.kind, &e->where);
	mpz_abs (result->value.integer, e->value.integer);
	return range_check (result, "IABS");
666

Jerry DeLisle committed
667 668 669 670
      case BT_REAL:
	result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
	mpfr_abs (result->value.real, e->value.real, GFC_RND_MODE);
	return range_check (result, "ABS");
671

Jerry DeLisle committed
672 673 674 675 676
      case BT_COMPLEX:
	gfc_set_model_kind (e->ts.kind);
	result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
	mpc_abs (result->value.real, e->value.complex, GFC_RND_MODE);
	return range_check (result, "CABS");
677

Jerry DeLisle committed
678 679
      default:
	gfc_internal_error ("gfc_simplify_abs(): Bad type");
680 681 682 683
    }
}


684 685
static gfc_expr *
simplify_achar_char (gfc_expr *e, gfc_expr *k, const char *name, bool ascii)
686 687
{
  gfc_expr *result;
688 689
  int kind;
  bool too_large = false;
690 691 692 693

  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

694
  kind = get_kind (BT_CHARACTER, k, name, gfc_default_character_kind);
695 696 697
  if (kind == -1)
    return &gfc_bad_expr;

698 699 700 701 702 703
  if (mpz_cmp_si (e->value.integer, 0) < 0)
    {
      gfc_error ("Argument of %s function at %L is negative", name,
		 &e->where);
      return &gfc_bad_expr;
    }
704

705 706 707 708
  if (ascii && gfc_option.warn_surprising
      && mpz_cmp_si (e->value.integer, 127) > 0)
    gfc_warning ("Argument of %s function at %L outside of range [0,127]",
		 name, &e->where);
709

710 711 712 713 714 715 716 717 718 719 720 721
  if (kind == 1 && mpz_cmp_si (e->value.integer, 255) > 0)
    too_large = true;
  else if (kind == 4)
    {
      mpz_t t;
      mpz_init_set_ui (t, 2);
      mpz_pow_ui (t, t, 32);
      mpz_sub_ui (t, t, 1);
      if (mpz_cmp (e->value.integer, t) > 0)
	too_large = true;
      mpz_clear (t);
    }
722

723 724 725 726 727 728
  if (too_large)
    {
      gfc_error ("Argument of %s function at %L is too large for the "
		 "collating sequence of kind %d", name, &e->where, kind);
      return &gfc_bad_expr;
    }
729

Jerry DeLisle committed
730
  result = gfc_get_character_expr (kind, &e->where, NULL, 1);
731
  result->value.character.string[0] = mpz_get_ui (e->value.integer);
Jerry DeLisle committed
732

733 734 735 736
  return result;
}


737 738 739 740 741 742 743 744 745 746 747

/* We use the processor's collating sequence, because all
   systems that gfortran currently works on are ASCII.  */

gfc_expr *
gfc_simplify_achar (gfc_expr *e, gfc_expr *k)
{
  return simplify_achar_char (e, k, "ACHAR", true);
}


748
gfc_expr *
749
gfc_simplify_acos (gfc_expr *x)
750 751 752 753 754 755
{
  gfc_expr *result;

  if (x->expr_type != EXPR_CONSTANT)
    return NULL;

756
  switch (x->ts.type)
757
    {
758 759 760 761 762 763 764 765
      case BT_REAL:
	if (mpfr_cmp_si (x->value.real, 1) > 0
	    || mpfr_cmp_si (x->value.real, -1) < 0)
	  {
	    gfc_error ("Argument of ACOS at %L must be between -1 and 1",
		       &x->where);
	    return &gfc_bad_expr;
	  }
Jerry DeLisle committed
766
	result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
767
	mpfr_acos (result->value.real, x->value.real, GFC_RND_MODE);
768
	break;
Jerry DeLisle committed
769

770
      case BT_COMPLEX:
Jerry DeLisle committed
771
	result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
772 773
	mpc_acos (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
	break;
Jerry DeLisle committed
774

775
      default:
776
	gfc_internal_error ("in gfc_simplify_acos(): Bad type");
777 778 779 780 781
    }

  return range_check (result, "ACOS");
}

782
gfc_expr *
783
gfc_simplify_acosh (gfc_expr *x)
784 785 786 787 788 789
{
  gfc_expr *result;

  if (x->expr_type != EXPR_CONSTANT)
    return NULL;

790
  switch (x->ts.type)
791
    {
792 793 794 795 796 797 798
      case BT_REAL:
	if (mpfr_cmp_si (x->value.real, 1) < 0)
	  {
	    gfc_error ("Argument of ACOSH at %L must not be less than 1",
		       &x->where);
	    return &gfc_bad_expr;
	  }
799

Jerry DeLisle committed
800
	result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
801 802
	mpfr_acosh (result->value.real, x->value.real, GFC_RND_MODE);
	break;
Jerry DeLisle committed
803

804
      case BT_COMPLEX:
Jerry DeLisle committed
805
	result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
806 807
	mpc_acosh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
	break;
Jerry DeLisle committed
808

809
      default:
810
	gfc_internal_error ("in gfc_simplify_acosh(): Bad type");
811
    }
812 813 814

  return range_check (result, "ACOSH");
}
815 816

gfc_expr *
817
gfc_simplify_adjustl (gfc_expr *e)
818 819 820
{
  gfc_expr *result;
  int count, i, len;
821
  gfc_char_t ch;
822 823 824 825 826 827 828 829 830 831 832 833 834 835

  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

  len = e->value.character.length;

  for (count = 0, i = 0; i < len; ++i)
    {
      ch = e->value.character.string[i];
      if (ch != ' ')
	break;
      ++count;
    }

Jerry DeLisle committed
836
  result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, len);
837
  for (i = 0; i < len - count; ++i)
838
    result->value.character.string[i] = e->value.character.string[count + i];
839 840 841 842 843 844

  return result;
}


gfc_expr *
845
gfc_simplify_adjustr (gfc_expr *e)
846 847 848
{
  gfc_expr *result;
  int count, i, len;
849
  gfc_char_t ch;
850 851 852 853 854 855 856 857 858 859 860 861 862 863

  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

  len = e->value.character.length;

  for (count = 0, i = len - 1; i >= 0; --i)
    {
      ch = e->value.character.string[i];
      if (ch != ' ')
	break;
      ++count;
    }

Jerry DeLisle committed
864
  result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, len);
865
  for (i = 0; i < count; ++i)
866
    result->value.character.string[i] = ' ';
867 868

  for (i = count; i < len; ++i)
869
    result->value.character.string[i] = e->value.character.string[i - count];
870 871 872 873 874 875

  return result;
}


gfc_expr *
876
gfc_simplify_aimag (gfc_expr *e)
877 878 879 880 881 882
{
  gfc_expr *result;

  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
883
  result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
884
  mpfr_set (result->value.real, mpc_imagref (e->value.complex), GFC_RND_MODE);
885 886 887 888 889 890

  return range_check (result, "AIMAG");
}


gfc_expr *
891
gfc_simplify_aint (gfc_expr *e, gfc_expr *k)
892 893 894 895 896 897 898 899 900 901 902 903
{
  gfc_expr *rtrunc, *result;
  int kind;

  kind = get_kind (BT_REAL, k, "AINT", e->ts.kind);
  if (kind == -1)
    return &gfc_bad_expr;

  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

  rtrunc = gfc_copy_expr (e);
904
  mpfr_trunc (rtrunc->value.real, e->value.real);
905 906

  result = gfc_real2real (rtrunc, kind);
Jerry DeLisle committed
907

908 909 910 911 912 913 914
  gfc_free_expr (rtrunc);

  return range_check (result, "AINT");
}


gfc_expr *
915 916
gfc_simplify_all (gfc_expr *mask, gfc_expr *dim)
{
917
  return simplify_transformation (mask, dim, NULL, true, gfc_and);
918 919 920 921
}


gfc_expr *
922
gfc_simplify_dint (gfc_expr *e)
923 924 925 926 927 928 929
{
  gfc_expr *rtrunc, *result;

  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

  rtrunc = gfc_copy_expr (e);
930
  mpfr_trunc (rtrunc->value.real, e->value.real);
931

932
  result = gfc_real2real (rtrunc, gfc_default_double_kind);
Jerry DeLisle committed
933

934 935 936 937 938 939 940
  gfc_free_expr (rtrunc);

  return range_check (result, "DINT");
}


gfc_expr *
941
gfc_simplify_anint (gfc_expr *e, gfc_expr *k)
942
{
943 944
  gfc_expr *result;
  int kind;
945 946 947 948 949 950 951 952

  kind = get_kind (BT_REAL, k, "ANINT", e->ts.kind);
  if (kind == -1)
    return &gfc_bad_expr;

  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
953
  result = gfc_get_constant_expr (e->ts.type, kind, &e->where);
954
  mpfr_round (result->value.real, e->value.real);
955 956 957 958 959 960

  return range_check (result, "ANINT");
}


gfc_expr *
961
gfc_simplify_and (gfc_expr *x, gfc_expr *y)
962 963 964 965 966 967 968 969
{
  gfc_expr *result;
  int kind;

  if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
    return NULL;

  kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
Jerry DeLisle committed
970 971

  switch (x->ts.type)
972
    {
Jerry DeLisle committed
973 974 975 976 977 978 979 980 981 982 983
      case BT_INTEGER:
	result = gfc_get_constant_expr (BT_INTEGER, kind, &x->where);
	mpz_and (result->value.integer, x->value.integer, y->value.integer);
	return range_check (result, "AND");

      case BT_LOGICAL:
	return gfc_get_logical_expr (kind, &x->where,
				     x->value.logical && y->value.logical);

      default:
	gcc_unreachable ();
984 985 986 987 988
    }
}


gfc_expr *
989 990
gfc_simplify_any (gfc_expr *mask, gfc_expr *dim)
{
991
  return simplify_transformation (mask, dim, NULL, false, gfc_or);
992 993 994 995
}


gfc_expr *
996
gfc_simplify_dnint (gfc_expr *e)
997
{
998
  gfc_expr *result;
999 1000 1001 1002

  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
1003
  result = gfc_get_constant_expr (BT_REAL, gfc_default_double_kind, &e->where);
1004
  mpfr_round (result->value.real, e->value.real);
1005 1006 1007 1008 1009 1010

  return range_check (result, "DNINT");
}


gfc_expr *
1011
gfc_simplify_asin (gfc_expr *x)
1012 1013 1014 1015 1016 1017
{
  gfc_expr *result;

  if (x->expr_type != EXPR_CONSTANT)
    return NULL;

1018
  switch (x->ts.type)
1019
    {
1020 1021 1022 1023 1024 1025 1026 1027
      case BT_REAL:
	if (mpfr_cmp_si (x->value.real, 1) > 0
	    || mpfr_cmp_si (x->value.real, -1) < 0)
	  {
	    gfc_error ("Argument of ASIN at %L must be between -1 and 1",
		       &x->where);
	    return &gfc_bad_expr;
	  }
Jerry DeLisle committed
1028
	result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1029 1030
	mpfr_asin (result->value.real, x->value.real, GFC_RND_MODE);
	break;
Jerry DeLisle committed
1031

1032
      case BT_COMPLEX:
Jerry DeLisle committed
1033
	result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1034 1035
	mpc_asin (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
	break;
Jerry DeLisle committed
1036

1037
      default:
1038
	gfc_internal_error ("in gfc_simplify_asin(): Bad type");
1039 1040 1041 1042 1043 1044 1045
    }

  return range_check (result, "ASIN");
}


gfc_expr *
1046
gfc_simplify_asinh (gfc_expr *x)
1047 1048 1049 1050 1051 1052
{
  gfc_expr *result;

  if (x->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
1053 1054
  result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);

1055 1056 1057 1058 1059
  switch (x->ts.type)
    {
      case BT_REAL:
	mpfr_asinh (result->value.real, x->value.real, GFC_RND_MODE);
	break;
Jerry DeLisle committed
1060

1061
      case BT_COMPLEX:
1062 1063
	mpc_asinh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
	break;
Jerry DeLisle committed
1064

1065
      default:
1066
	gfc_internal_error ("in gfc_simplify_asinh(): Bad type");
1067
    }
1068 1069 1070 1071 1072 1073

  return range_check (result, "ASINH");
}


gfc_expr *
1074
gfc_simplify_atan (gfc_expr *x)
1075 1076 1077 1078 1079
{
  gfc_expr *result;

  if (x->expr_type != EXPR_CONSTANT)
    return NULL;
Jerry DeLisle committed
1080 1081 1082

  result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);

1083 1084 1085 1086 1087
  switch (x->ts.type)
    {
      case BT_REAL:
	mpfr_atan (result->value.real, x->value.real, GFC_RND_MODE);
	break;
Jerry DeLisle committed
1088

1089
      case BT_COMPLEX:
1090 1091
	mpc_atan (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
	break;
Jerry DeLisle committed
1092

1093
      default:
1094
	gfc_internal_error ("in gfc_simplify_atan(): Bad type");
1095
    }
1096 1097

  return range_check (result, "ATAN");
1098 1099 1100 1101
}


gfc_expr *
1102
gfc_simplify_atanh (gfc_expr *x)
1103 1104
{
  gfc_expr *result;
1105

1106 1107 1108
  if (x->expr_type != EXPR_CONSTANT)
    return NULL;

1109
  switch (x->ts.type)
1110
    {
1111 1112 1113 1114 1115 1116 1117 1118
      case BT_REAL:
	if (mpfr_cmp_si (x->value.real, 1) >= 0
	    || mpfr_cmp_si (x->value.real, -1) <= 0)
	  {
	    gfc_error ("Argument of ATANH at %L must be inside the range -1 "
		       "to 1", &x->where);
	    return &gfc_bad_expr;
	  }
Jerry DeLisle committed
1119
	result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1120 1121
	mpfr_atanh (result->value.real, x->value.real, GFC_RND_MODE);
	break;
Jerry DeLisle committed
1122

1123
      case BT_COMPLEX:
Jerry DeLisle committed
1124
	result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1125 1126
	mpc_atanh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
	break;
Jerry DeLisle committed
1127

1128
      default:
1129
	gfc_internal_error ("in gfc_simplify_atanh(): Bad type");
1130
    }
1131 1132

  return range_check (result, "ATANH");
1133 1134 1135 1136
}


gfc_expr *
1137
gfc_simplify_atan2 (gfc_expr *y, gfc_expr *x)
1138 1139 1140 1141 1142 1143
{
  gfc_expr *result;

  if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
    return NULL;

1144
  if (mpfr_sgn (y->value.real) == 0 && mpfr_sgn (x->value.real) == 0)
1145
    {
1146 1147
      gfc_error ("If first argument of ATAN2 %L is zero, then the "
		 "second argument must not be zero", &x->where);
1148 1149 1150
      return &gfc_bad_expr;
    }

Jerry DeLisle committed
1151
  result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1152
  mpfr_atan2 (result->value.real, y->value.real, x->value.real, GFC_RND_MODE);
1153 1154 1155 1156 1157 1158

  return range_check (result, "ATAN2");
}


gfc_expr *
Jerry DeLisle committed
1159
gfc_simplify_bessel_j0 (gfc_expr *x)
1160 1161 1162 1163 1164 1165
{
  gfc_expr *result;

  if (x->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
1166
  result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1167 1168 1169 1170 1171 1172 1173
  mpfr_j0 (result->value.real, x->value.real, GFC_RND_MODE);

  return range_check (result, "BESSEL_J0");
}


gfc_expr *
Jerry DeLisle committed
1174
gfc_simplify_bessel_j1 (gfc_expr *x)
1175 1176 1177 1178 1179 1180
{
  gfc_expr *result;

  if (x->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
1181
  result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1182 1183 1184 1185 1186 1187 1188
  mpfr_j1 (result->value.real, x->value.real, GFC_RND_MODE);

  return range_check (result, "BESSEL_J1");
}


gfc_expr *
Jerry DeLisle committed
1189
gfc_simplify_bessel_jn (gfc_expr *order, gfc_expr *x)
1190 1191 1192 1193 1194 1195 1196 1197
{
  gfc_expr *result;
  long n;

  if (x->expr_type != EXPR_CONSTANT || order->expr_type != EXPR_CONSTANT)
    return NULL;

  n = mpz_get_si (order->value.integer);
Jerry DeLisle committed
1198
  result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1199 1200 1201 1202 1203 1204
  mpfr_jn (result->value.real, n, x->value.real, GFC_RND_MODE);

  return range_check (result, "BESSEL_JN");
}


1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
/* Simplify transformational form of JN and YN.  */

static gfc_expr *
gfc_simplify_bessel_n2 (gfc_expr *order1, gfc_expr *order2, gfc_expr *x,
			bool jn)
{
  gfc_expr *result;
  gfc_expr *e;
  long n1, n2;
  int i;
  mpfr_t x2rev, last1, last2;

  if (x->expr_type != EXPR_CONSTANT || order1->expr_type != EXPR_CONSTANT
      || order2->expr_type != EXPR_CONSTANT)
1219
    return NULL;
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245

  n1 = mpz_get_si (order1->value.integer);
  n2 = mpz_get_si (order2->value.integer);
  result = gfc_get_array_expr (x->ts.type, x->ts.kind, &x->where);
  result->rank = 1;
  result->shape = gfc_get_shape (1);
  mpz_init_set_ui (result->shape[0], MAX (n2-n1+1, 0));

  if (n2 < n1)
    return result;

  /* Special case: x == 0; it is J0(0.0) == 1, JN(N > 0, 0.0) == 0; and
     YN(N, 0.0) = -Inf.  */

  if (mpfr_cmp_ui (x->value.real, 0.0) == 0)
    {
      if (!jn && gfc_option.flag_range_check)
	{
	  gfc_error ("Result of BESSEL_YN is -INF at %L", &result->where);
 	  gfc_free_expr (result);
	  return &gfc_bad_expr;
	}

      if (jn && n1 == 0)
	{
	  e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1246
	  mpfr_set_ui (e->value.real, 1, GFC_RND_MODE);
1247 1248 1249 1250 1251 1252 1253 1254 1255
	  gfc_constructor_append_expr (&result->value.constructor, e,
				       &x->where);
	  n1++;
	}

      for (i = n1; i <= n2; i++)
	{
	  e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
	  if (jn)
1256
	    mpfr_set_ui (e->value.real, 0, GFC_RND_MODE);
1257
	  else
1258
	    mpfr_set_inf (e->value.real, -1);
1259 1260 1261 1262 1263 1264 1265
	  gfc_constructor_append_expr (&result->value.constructor, e,
				       &x->where);
	}

      return result;
    }

1266
  /* Use the faster but more verbose recurrence algorithm. Bessel functions
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
     are stable for downward recursion and Neumann functions are stable
     for upward recursion. It is
       x2rev = 2.0/x,
       J(N-1, x) = x2rev * N * J(N, x) - J(N+1, x),
       Y(N+1, x) = x2rev * N * Y(N, x) - Y(N-1, x).
     Cf. http://dlmf.nist.gov/10.74#iv and http://dlmf.nist.gov/10.6#E1  */

  gfc_set_model_kind (x->ts.kind);

  /* Get first recursion anchor.  */

  mpfr_init (last1);
  if (jn)
    mpfr_jn (last1, n2, x->value.real, GFC_RND_MODE);
  else
    mpfr_yn (last1, n1, x->value.real, GFC_RND_MODE);

  e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
  mpfr_set (e->value.real, last1, GFC_RND_MODE);
  if (range_check (e, jn ? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr)
    {
      mpfr_clear (last1);
      gfc_free_expr (e);
      gfc_free_expr (result);
      return &gfc_bad_expr;
    }
  gfc_constructor_append_expr (&result->value.constructor, e, &x->where);

  if (n1 == n2)
    {
      mpfr_clear (last1);
      return result;
    }
 
  /* Get second recursion anchor.  */

  mpfr_init (last2);
  if (jn)
    mpfr_jn (last2, n2-1, x->value.real, GFC_RND_MODE);
  else
    mpfr_yn (last2, n1+1, x->value.real, GFC_RND_MODE);

  e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
  mpfr_set (e->value.real, last2, GFC_RND_MODE);
  if (range_check (e, jn ? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr)
    {
      mpfr_clear (last1);
      mpfr_clear (last2);
      gfc_free_expr (e);
      gfc_free_expr (result);
      return &gfc_bad_expr;
    }
  if (jn)
1320
    gfc_constructor_insert_expr (&result->value.constructor, e, &x->where, -2);
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
  else 
    gfc_constructor_append_expr (&result->value.constructor, e, &x->where);

  if (n1 + 1 == n2)
    {
      mpfr_clear (last1);
      mpfr_clear (last2);
      return result;
    }

  /* Start actual recursion.  */

  mpfr_init (x2rev);
  mpfr_ui_div (x2rev, 2, x->value.real, GFC_RND_MODE);
 
  for (i = 2; i <= n2-n1; i++)
    {
      e = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349

      /* Special case: For YN, if the previous N gave -INF, set
	 also N+1 to -INF.  */
      if (!jn && !gfc_option.flag_range_check && mpfr_inf_p (last2))
	{
	  mpfr_set_inf (e->value.real, -1);
	  gfc_constructor_append_expr (&result->value.constructor, e,
				       &x->where);
	  continue;
	}

1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
      mpfr_mul_si (e->value.real, x2rev, jn ? (n2-i+1) : (n1+i-1),
		   GFC_RND_MODE);
      mpfr_mul (e->value.real, e->value.real, last2, GFC_RND_MODE);
      mpfr_sub (e->value.real, e->value.real, last1, GFC_RND_MODE);

      if (range_check (e, jn ? "BESSEL_JN" : "BESSEL_YN") == &gfc_bad_expr)
	goto error;

      if (jn)
	gfc_constructor_insert_expr (&result->value.constructor, e, &x->where,
				     -i-1);
      else
	gfc_constructor_append_expr (&result->value.constructor, e, &x->where);

      mpfr_set (last1, last2, GFC_RND_MODE);
      mpfr_set (last2, e->value.real, GFC_RND_MODE);
    }

  mpfr_clear (last1);
  mpfr_clear (last2);
  mpfr_clear (x2rev);
  return result;

error:
  mpfr_clear (last1);
  mpfr_clear (last2);
  mpfr_clear (x2rev);
  gfc_free_expr (e);
  gfc_free_expr (result);
  return &gfc_bad_expr;
}


gfc_expr *
gfc_simplify_bessel_jn2 (gfc_expr *order1, gfc_expr *order2, gfc_expr *x)
{
  return gfc_simplify_bessel_n2 (order1, order2, x, true);
}


1390
gfc_expr *
Jerry DeLisle committed
1391
gfc_simplify_bessel_y0 (gfc_expr *x)
1392 1393 1394 1395 1396 1397
{
  gfc_expr *result;

  if (x->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
1398
  result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1399 1400 1401 1402 1403 1404 1405
  mpfr_y0 (result->value.real, x->value.real, GFC_RND_MODE);

  return range_check (result, "BESSEL_Y0");
}


gfc_expr *
Jerry DeLisle committed
1406
gfc_simplify_bessel_y1 (gfc_expr *x)
1407 1408 1409 1410 1411 1412
{
  gfc_expr *result;

  if (x->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
1413
  result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1414 1415 1416 1417 1418 1419 1420
  mpfr_y1 (result->value.real, x->value.real, GFC_RND_MODE);

  return range_check (result, "BESSEL_Y1");
}


gfc_expr *
Jerry DeLisle committed
1421
gfc_simplify_bessel_yn (gfc_expr *order, gfc_expr *x)
1422 1423 1424 1425 1426 1427 1428 1429
{
  gfc_expr *result;
  long n;

  if (x->expr_type != EXPR_CONSTANT || order->expr_type != EXPR_CONSTANT)
    return NULL;

  n = mpz_get_si (order->value.integer);
Jerry DeLisle committed
1430
  result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1431 1432 1433 1434 1435 1436 1437
  mpfr_yn (result->value.real, n, x->value.real, GFC_RND_MODE);

  return range_check (result, "BESSEL_YN");
}


gfc_expr *
1438 1439 1440 1441 1442 1443 1444
gfc_simplify_bessel_yn2 (gfc_expr *order1, gfc_expr *order2, gfc_expr *x)
{
  return gfc_simplify_bessel_n2 (order1, order2, x, false);
}


gfc_expr *
1445
gfc_simplify_bit_size (gfc_expr *e)
1446
{
Jerry DeLisle committed
1447 1448 1449
  int i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
  return gfc_get_int_expr (e->ts.kind, &e->where,
			   gfc_integer_kinds[i].bit_size);
1450 1451 1452 1453
}


gfc_expr *
1454
gfc_simplify_btest (gfc_expr *e, gfc_expr *bit)
1455 1456 1457 1458 1459 1460 1461
{
  int b;

  if (e->expr_type != EXPR_CONSTANT || bit->expr_type != EXPR_CONSTANT)
    return NULL;

  if (gfc_extract_int (bit, &b) != NULL || b < 0)
Jerry DeLisle committed
1462
    return gfc_get_logical_expr (gfc_default_logical_kind, &e->where, false);
1463

Jerry DeLisle committed
1464 1465
  return gfc_get_logical_expr (gfc_default_logical_kind, &e->where,
			       mpz_tstbit (e->value.integer, b));
1466 1467 1468
}


1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
static int
compare_bitwise (gfc_expr *i, gfc_expr *j)
{
  mpz_t x, y;
  int k, res;

  gcc_assert (i->ts.type == BT_INTEGER);
  gcc_assert (j->ts.type == BT_INTEGER);

  mpz_init_set (x, i->value.integer);
  k = gfc_validate_kind (i->ts.type, i->ts.kind, false);
  convert_mpz_to_unsigned (x, gfc_integer_kinds[k].bit_size);

  mpz_init_set (y, j->value.integer);
  k = gfc_validate_kind (j->ts.type, j->ts.kind, false);
  convert_mpz_to_unsigned (y, gfc_integer_kinds[k].bit_size);

  res = mpz_cmp (x, y);
  mpz_clear (x);
  mpz_clear (y);
  return res;
}


gfc_expr *
gfc_simplify_bge (gfc_expr *i, gfc_expr *j)
{
  if (i->expr_type != EXPR_CONSTANT || j->expr_type != EXPR_CONSTANT)
    return NULL;

  return gfc_get_logical_expr (gfc_default_logical_kind, &i->where,
			       compare_bitwise (i, j) >= 0);
}


gfc_expr *
gfc_simplify_bgt (gfc_expr *i, gfc_expr *j)
{
  if (i->expr_type != EXPR_CONSTANT || j->expr_type != EXPR_CONSTANT)
    return NULL;

  return gfc_get_logical_expr (gfc_default_logical_kind, &i->where,
			       compare_bitwise (i, j) > 0);
}


gfc_expr *
gfc_simplify_ble (gfc_expr *i, gfc_expr *j)
{
  if (i->expr_type != EXPR_CONSTANT || j->expr_type != EXPR_CONSTANT)
    return NULL;

  return gfc_get_logical_expr (gfc_default_logical_kind, &i->where,
			       compare_bitwise (i, j) <= 0);
}


gfc_expr *
gfc_simplify_blt (gfc_expr *i, gfc_expr *j)
{
  if (i->expr_type != EXPR_CONSTANT || j->expr_type != EXPR_CONSTANT)
    return NULL;

  return gfc_get_logical_expr (gfc_default_logical_kind, &i->where,
			       compare_bitwise (i, j) < 0);
}


1537
gfc_expr *
1538
gfc_simplify_ceiling (gfc_expr *e, gfc_expr *k)
1539 1540 1541 1542
{
  gfc_expr *ceil, *result;
  int kind;

1543
  kind = get_kind (BT_INTEGER, k, "CEILING", gfc_default_integer_kind);
1544 1545 1546 1547 1548 1549 1550
  if (kind == -1)
    return &gfc_bad_expr;

  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

  ceil = gfc_copy_expr (e);
1551
  mpfr_ceil (ceil->value.real, e->value.real);
Jerry DeLisle committed
1552 1553

  result = gfc_get_constant_expr (BT_INTEGER, kind, &e->where);
1554
  gfc_mpfr_to_mpz (result->value.integer, ceil->value.real, &e->where);
1555 1556 1557 1558 1559 1560 1561 1562

  gfc_free_expr (ceil);

  return range_check (result, "CEILING");
}


gfc_expr *
1563
gfc_simplify_char (gfc_expr *e, gfc_expr *k)
1564
{
1565
  return simplify_achar_char (e, k, "CHAR", false);
1566 1567 1568
}


Jerry DeLisle committed
1569
/* Common subroutine for simplifying CMPLX, COMPLEX and DCMPLX.  */
1570 1571

static gfc_expr *
1572
simplify_cmplx (const char *name, gfc_expr *x, gfc_expr *y, int kind)
1573 1574 1575
{
  gfc_expr *result;

Jerry DeLisle committed
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
  if (convert_boz (x, kind) == &gfc_bad_expr)
    return &gfc_bad_expr;

  if (convert_boz (y, kind) == &gfc_bad_expr)
    return &gfc_bad_expr;

  if (x->expr_type != EXPR_CONSTANT
      || (y != NULL && y->expr_type != EXPR_CONSTANT))
    return NULL;

  result = gfc_get_constant_expr (BT_COMPLEX, kind, &x->where);
1587 1588 1589

  switch (x->ts.type)
    {
Jerry DeLisle committed
1590
      case BT_INTEGER:
1591
	mpc_set_z (result->value.complex, x->value.integer, GFC_MPC_RND_MODE);
Jerry DeLisle committed
1592
	break;
1593

Jerry DeLisle committed
1594 1595 1596
      case BT_REAL:
	mpc_set_fr (result->value.complex, x->value.real, GFC_RND_MODE);
	break;
1597

Jerry DeLisle committed
1598 1599 1600
      case BT_COMPLEX:
	mpc_set (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
	break;
1601

Jerry DeLisle committed
1602 1603
      default:
	gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (x)");
1604 1605
    }

Jerry DeLisle committed
1606 1607
  if (!y)
    return range_check (result, name);
1608

Jerry DeLisle committed
1609
  switch (y->ts.type)
1610
    {
Jerry DeLisle committed
1611 1612 1613 1614
      case BT_INTEGER:
	mpfr_set_z (mpc_imagref (result->value.complex),
		    y->value.integer, GFC_RND_MODE);
	break;
1615

Jerry DeLisle committed
1616 1617 1618 1619 1620 1621 1622
      case BT_REAL:
	mpfr_set (mpc_imagref (result->value.complex),
		  y->value.real, GFC_RND_MODE);
	break;

      default:
	gfc_internal_error ("gfc_simplify_dcmplx(): Bad type (y)");
1623 1624
    }

1625 1626 1627 1628 1629
  return range_check (result, name);
}


gfc_expr *
1630
gfc_simplify_cmplx (gfc_expr *x, gfc_expr *y, gfc_expr *k)
1631 1632 1633
{
  int kind;

Jerry DeLisle committed
1634
  kind = get_kind (BT_REAL, k, "CMPLX", gfc_default_complex_kind);
1635 1636 1637 1638 1639 1640 1641 1642
  if (kind == -1)
    return &gfc_bad_expr;

  return simplify_cmplx ("CMPLX", x, y, kind);
}


gfc_expr *
1643
gfc_simplify_complex (gfc_expr *x, gfc_expr *y)
1644 1645 1646
{
  int kind;

Jerry DeLisle committed
1647 1648 1649 1650 1651 1652 1653 1654
  if (x->ts.type == BT_INTEGER && y->ts.type == BT_INTEGER)
    kind = gfc_default_complex_kind;
  else if (x->ts.type == BT_REAL || y->ts.type == BT_INTEGER)
    kind = x->ts.kind;
  else if (x->ts.type == BT_INTEGER || y->ts.type == BT_REAL)
    kind = y->ts.kind;
  else if (x->ts.type == BT_REAL && y->ts.type == BT_REAL)
    kind = (x->ts.kind > y->ts.kind) ? x->ts.kind : y->ts.kind;
1655
  else
Jerry DeLisle committed
1656
    gcc_unreachable ();
1657

1658 1659 1660 1661 1662
  return simplify_cmplx ("COMPLEX", x, y, kind);
}


gfc_expr *
1663
gfc_simplify_conjg (gfc_expr *e)
1664 1665 1666 1667 1668 1669 1670
{
  gfc_expr *result;

  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

  result = gfc_copy_expr (e);
1671
  mpc_conj (result->value.complex, result->value.complex, GFC_MPC_RND_MODE);
Jerry DeLisle committed
1672

1673 1674 1675 1676 1677
  return range_check (result, "CONJG");
}


gfc_expr *
1678
gfc_simplify_cos (gfc_expr *x)
1679 1680 1681 1682 1683 1684
{
  gfc_expr *result;

  if (x->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
1685
  result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1686 1687 1688

  switch (x->ts.type)
    {
Jerry DeLisle committed
1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699
      case BT_REAL:
	mpfr_cos (result->value.real, x->value.real, GFC_RND_MODE);
	break;

      case BT_COMPLEX:
	gfc_set_model_kind (x->ts.kind);
	mpc_cos (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
	break;

      default:
	gfc_internal_error ("in gfc_simplify_cos(): Bad type");
1700 1701 1702 1703 1704 1705 1706
    }

  return range_check (result, "COS");
}


gfc_expr *
1707
gfc_simplify_cosh (gfc_expr *x)
1708 1709 1710 1711 1712 1713
{
  gfc_expr *result;

  if (x->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
1714
  result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1715

Jerry DeLisle committed
1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728
  switch (x->ts.type)
    {
      case BT_REAL:
	mpfr_cosh (result->value.real, x->value.real, GFC_RND_MODE);
	break;

      case BT_COMPLEX:
	mpc_cosh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
	break;
	
      default:
	gcc_unreachable ();
    }
1729 1730 1731 1732 1733 1734

  return range_check (result, "COSH");
}


gfc_expr *
1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755
gfc_simplify_count (gfc_expr *mask, gfc_expr *dim, gfc_expr *kind)
{
  gfc_expr *result;

  if (!is_constant_array_expr (mask)
      || !gfc_is_constant_expr (dim)
      || !gfc_is_constant_expr (kind))
    return NULL;

  result = transformational_result (mask, dim,
				    BT_INTEGER,
				    get_kind (BT_INTEGER, kind, "COUNT",
					      gfc_default_integer_kind),
				    &mask->where);

  init_result_expr (result, 0, NULL);

  /* Passing MASK twice, once as data array, once as mask.
     Whenever gfc_count is called, '1' is added to the result.  */
  return !dim || mask->rank == 1 ?
    simplify_transformation_to_scalar (result, mask, mask, gfc_count) :
1756
    simplify_transformation_to_array (result, mask, dim, mask, gfc_count, NULL);
1757 1758 1759 1760
}


gfc_expr *
1761
gfc_simplify_dcmplx (gfc_expr *x, gfc_expr *y)
1762
{
1763
  return simplify_cmplx ("DCMPLX", x, y, gfc_default_double_kind);
1764 1765 1766 1767
}


gfc_expr *
1768
gfc_simplify_dble (gfc_expr *e)
1769
{
1770
  gfc_expr *result = NULL;
1771 1772 1773 1774

  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
1775 1776
  if (convert_boz (e, gfc_default_double_kind) == &gfc_bad_expr)
    return &gfc_bad_expr;
1777

Jerry DeLisle committed
1778 1779 1780
  result = gfc_convert_constant (e, BT_REAL, gfc_default_double_kind);
  if (result == &gfc_bad_expr)
    return &gfc_bad_expr;
1781

1782 1783 1784 1785 1786
  return range_check (result, "DBLE");
}


gfc_expr *
1787
gfc_simplify_digits (gfc_expr *x)
1788 1789 1790
{
  int i, digits;

1791
  i = gfc_validate_kind (x->ts.type, x->ts.kind, false);
Jerry DeLisle committed
1792

1793 1794
  switch (x->ts.type)
    {
Jerry DeLisle committed
1795 1796 1797
      case BT_INTEGER:
	digits = gfc_integer_kinds[i].digits;
	break;
1798

Jerry DeLisle committed
1799 1800 1801 1802
      case BT_REAL:
      case BT_COMPLEX:
	digits = gfc_real_kinds[i].digits;
	break;
1803

Jerry DeLisle committed
1804 1805
      default:
	gcc_unreachable ();
1806 1807
    }

Jerry DeLisle committed
1808
  return gfc_get_int_expr (gfc_default_integer_kind, NULL, digits);
1809 1810 1811 1812
}


gfc_expr *
1813
gfc_simplify_dim (gfc_expr *x, gfc_expr *y)
1814 1815
{
  gfc_expr *result;
1816
  int kind;
1817 1818 1819 1820

  if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
    return NULL;

1821
  kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
Jerry DeLisle committed
1822
  result = gfc_get_constant_expr (x->ts.type, kind, &x->where);
1823 1824 1825

  switch (x->ts.type)
    {
Jerry DeLisle committed
1826 1827 1828 1829 1830
      case BT_INTEGER:
	if (mpz_cmp (x->value.integer, y->value.integer) > 0)
	  mpz_sub (result->value.integer, x->value.integer, y->value.integer);
	else
	  mpz_set_ui (result->value.integer, 0);
1831

Jerry DeLisle committed
1832
	break;
1833

Jerry DeLisle committed
1834 1835 1836 1837 1838 1839
      case BT_REAL:
	if (mpfr_cmp (x->value.real, y->value.real) > 0)
	  mpfr_sub (result->value.real, x->value.real, y->value.real,
		    GFC_RND_MODE);
	else
	  mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
1840

Jerry DeLisle committed
1841
	break;
1842

Jerry DeLisle committed
1843 1844
      default:
	gfc_internal_error ("gfc_simplify_dim(): Bad type");
1845 1846 1847 1848 1849 1850
    }

  return range_check (result, "DIM");
}


1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861
gfc_expr*
gfc_simplify_dot_product (gfc_expr *vector_a, gfc_expr *vector_b)
{
  if (!is_constant_array_expr (vector_a)
      || !is_constant_array_expr (vector_b))
    return NULL;

  gcc_assert (vector_a->rank == 1);
  gcc_assert (vector_b->rank == 1);
  gcc_assert (gfc_compare_types (&vector_a->ts, &vector_b->ts));

Jerry DeLisle committed
1862
  return compute_dot_product (vector_a, 1, 0, vector_b, 1, 0);
1863 1864 1865
}


1866
gfc_expr *
1867
gfc_simplify_dprod (gfc_expr *x, gfc_expr *y)
1868
{
1869
  gfc_expr *a1, *a2, *result;
1870 1871 1872 1873

  if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
    return NULL;

1874 1875
  a1 = gfc_real2real (x, gfc_default_double_kind);
  a2 = gfc_real2real (y, gfc_default_double_kind);
1876

Jerry DeLisle committed
1877
  result = gfc_get_constant_expr (BT_REAL, gfc_default_double_kind, &x->where);
1878
  mpfr_mul (result->value.real, a1->value.real, a2->value.real, GFC_RND_MODE);
1879

1880
  gfc_free_expr (a2);
Jerry DeLisle committed
1881
  gfc_free_expr (a1);
1882 1883 1884 1885 1886

  return range_check (result, "DPROD");
}


1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944
static gfc_expr *
simplify_dshift (gfc_expr *arg1, gfc_expr *arg2, gfc_expr *shiftarg,
		      bool right)
{
  gfc_expr *result;
  int i, k, size, shift;

  if (arg1->expr_type != EXPR_CONSTANT || arg2->expr_type != EXPR_CONSTANT
      || shiftarg->expr_type != EXPR_CONSTANT)
    return NULL;

  k = gfc_validate_kind (BT_INTEGER, arg1->ts.kind, false);
  size = gfc_integer_kinds[k].bit_size;

  if (gfc_extract_int (shiftarg, &shift) != NULL)
    {
      gfc_error ("Invalid SHIFT argument of DSHIFTL at %L", &shiftarg->where);
      return &gfc_bad_expr;
    }

  gcc_assert (shift >= 0 && shift <= size);

  /* DSHIFTR(I,J,SHIFT) = DSHIFTL(I,J,SIZE-SHIFT).  */
  if (right)
    shift = size - shift;

  result = gfc_get_constant_expr (BT_INTEGER, arg1->ts.kind, &arg1->where);
  mpz_set_ui (result->value.integer, 0);

  for (i = 0; i < shift; i++)
    if (mpz_tstbit (arg2->value.integer, size - shift + i))
      mpz_setbit (result->value.integer, i);

  for (i = 0; i < size - shift; i++)
    if (mpz_tstbit (arg1->value.integer, i))
      mpz_setbit (result->value.integer, shift + i);

  /* Convert to a signed value.  */
  convert_mpz_to_signed (result->value.integer, size);

  return result;
}


gfc_expr *
gfc_simplify_dshiftr (gfc_expr *arg1, gfc_expr *arg2, gfc_expr *shiftarg)
{
  return simplify_dshift (arg1, arg2, shiftarg, true);
}


gfc_expr *
gfc_simplify_dshiftl (gfc_expr *arg1, gfc_expr *arg2, gfc_expr *shiftarg)
{
  return simplify_dshift (arg1, arg2, shiftarg, false);
}


1945
gfc_expr *
1946 1947 1948 1949 1950 1951 1952
gfc_simplify_erf (gfc_expr *x)
{
  gfc_expr *result;

  if (x->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
1953
  result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
  mpfr_erf (result->value.real, x->value.real, GFC_RND_MODE);

  return range_check (result, "ERF");
}


gfc_expr *
gfc_simplify_erfc (gfc_expr *x)
{
  gfc_expr *result;

  if (x->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
1968
  result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
1969 1970 1971 1972 1973 1974
  mpfr_erfc (result->value.real, x->value.real, GFC_RND_MODE);

  return range_check (result, "ERFC");
}


1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098
/* Helper functions to simplify ERFC_SCALED(x) = ERFC(x) * EXP(X**2).  */

#define MAX_ITER 200
#define ARG_LIMIT 12

/* Calculate ERFC_SCALED directly by its definition:

     ERFC_SCALED(x) = ERFC(x) * EXP(X**2)

   using a large precision for intermediate results.  This is used for all
   but large values of the argument.  */
static void
fullprec_erfc_scaled (mpfr_t res, mpfr_t arg)
{
  mp_prec_t prec;
  mpfr_t a, b;

  prec = mpfr_get_default_prec ();
  mpfr_set_default_prec (10 * prec);

  mpfr_init (a);
  mpfr_init (b);

  mpfr_set (a, arg, GFC_RND_MODE);
  mpfr_sqr (b, a, GFC_RND_MODE);
  mpfr_exp (b, b, GFC_RND_MODE);
  mpfr_erfc (a, a, GFC_RND_MODE);
  mpfr_mul (a, a, b, GFC_RND_MODE);

  mpfr_set (res, a, GFC_RND_MODE);
  mpfr_set_default_prec (prec);

  mpfr_clear (a);
  mpfr_clear (b);
}

/* Calculate ERFC_SCALED using a power series expansion in 1/arg:

    ERFC_SCALED(x) = 1 / (x * sqrt(pi))
                     * (1 + Sum_n (-1)**n * (1 * 3 * 5 * ... * (2n-1))
                                          / (2 * x**2)**n)

  This is used for large values of the argument.  Intermediate calculations
  are performed with twice the precision.  We don't do a fixed number of
  iterations of the sum, but stop when it has converged to the required
  precision.  */
static void
asympt_erfc_scaled (mpfr_t res, mpfr_t arg)
{
  mpfr_t sum, x, u, v, w, oldsum, sumtrunc;
  mpz_t num;
  mp_prec_t prec;
  unsigned i;

  prec = mpfr_get_default_prec ();
  mpfr_set_default_prec (2 * prec);

  mpfr_init (sum);
  mpfr_init (x);
  mpfr_init (u);
  mpfr_init (v);
  mpfr_init (w);
  mpz_init (num);

  mpfr_init (oldsum);
  mpfr_init (sumtrunc);
  mpfr_set_prec (oldsum, prec);
  mpfr_set_prec (sumtrunc, prec);

  mpfr_set (x, arg, GFC_RND_MODE);
  mpfr_set_ui (sum, 1, GFC_RND_MODE);
  mpz_set_ui (num, 1);

  mpfr_set (u, x, GFC_RND_MODE);
  mpfr_sqr (u, u, GFC_RND_MODE);
  mpfr_mul_ui (u, u, 2, GFC_RND_MODE);
  mpfr_pow_si (u, u, -1, GFC_RND_MODE);

  for (i = 1; i < MAX_ITER; i++)
  {
    mpfr_set (oldsum, sum, GFC_RND_MODE);

    mpz_mul_ui (num, num, 2 * i - 1);
    mpz_neg (num, num);

    mpfr_set (w, u, GFC_RND_MODE);
    mpfr_pow_ui (w, w, i, GFC_RND_MODE);

    mpfr_set_z (v, num, GFC_RND_MODE);
    mpfr_mul (v, v, w, GFC_RND_MODE);

    mpfr_add (sum, sum, v, GFC_RND_MODE);

    mpfr_set (sumtrunc, sum, GFC_RND_MODE);
    if (mpfr_cmp (sumtrunc, oldsum) == 0)
      break;
  }

  /* We should have converged by now; otherwise, ARG_LIMIT is probably
     set too low.  */
  gcc_assert (i < MAX_ITER);

  /* Divide by x * sqrt(Pi).  */
  mpfr_const_pi (u, GFC_RND_MODE);
  mpfr_sqrt (u, u, GFC_RND_MODE);
  mpfr_mul (u, u, x, GFC_RND_MODE);
  mpfr_div (sum, sum, u, GFC_RND_MODE);

  mpfr_set (res, sum, GFC_RND_MODE);
  mpfr_set_default_prec (prec);

  mpfr_clears (sum, x, u, v, w, oldsum, sumtrunc, NULL);
  mpz_clear (num);
}


gfc_expr *
gfc_simplify_erfc_scaled (gfc_expr *x)
{
  gfc_expr *result;

  if (x->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
2099
  result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111
  if (mpfr_cmp_d (x->value.real, ARG_LIMIT) >= 0)
    asympt_erfc_scaled (result->value.real, x->value.real);
  else
    fullprec_erfc_scaled (result->value.real, x->value.real);

  return range_check (result, "ERFC_SCALED");
}

#undef MAX_ITER
#undef ARG_LIMIT


2112
gfc_expr *
2113
gfc_simplify_epsilon (gfc_expr *e)
2114 2115 2116 2117
{
  gfc_expr *result;
  int i;

2118
  i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
2119

Jerry DeLisle committed
2120
  result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
2121
  mpfr_set (result->value.real, gfc_real_kinds[i].epsilon, GFC_RND_MODE);
2122 2123 2124 2125 2126 2127

  return range_check (result, "EPSILON");
}


gfc_expr *
2128
gfc_simplify_exp (gfc_expr *x)
2129 2130 2131 2132 2133 2134
{
  gfc_expr *result;

  if (x->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
2135
  result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2136 2137 2138

  switch (x->ts.type)
    {
Jerry DeLisle committed
2139 2140 2141
      case BT_REAL:
	mpfr_exp (result->value.real, x->value.real, GFC_RND_MODE);
	break;
2142

Jerry DeLisle committed
2143 2144 2145 2146
      case BT_COMPLEX:
	gfc_set_model_kind (x->ts.kind);
	mpc_exp (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
	break;
2147

Jerry DeLisle committed
2148 2149
      default:
	gfc_internal_error ("in gfc_simplify_exp(): Bad type");
2150 2151 2152 2153 2154
    }

  return range_check (result, "EXP");
}

2155

2156
gfc_expr *
2157
gfc_simplify_exponent (gfc_expr *x)
2158
{
2159
  int i;
2160 2161 2162 2163 2164
  gfc_expr *result;

  if (x->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
2165 2166
  result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
				  &x->where);
2167

2168 2169
  gfc_set_model (x->value.real);

2170
  if (mpfr_sgn (x->value.real) == 0)
2171 2172 2173 2174 2175
    {
      mpz_set_ui (result->value.integer, 0);
      return result;
    }

2176 2177
  i = (int) mpfr_get_exp (x->value.real);
  mpz_set_si (result->value.integer, i);
2178 2179 2180 2181 2182 2183

  return range_check (result, "EXPONENT");
}


gfc_expr *
2184
gfc_simplify_float (gfc_expr *a)
2185 2186 2187 2188 2189 2190
{
  gfc_expr *result;

  if (a->expr_type != EXPR_CONSTANT)
    return NULL;

2191 2192
  if (a->is_boz)
    {
Jerry DeLisle committed
2193 2194
      if (convert_boz (a, gfc_default_real_kind) == &gfc_bad_expr)
	return &gfc_bad_expr;
2195 2196 2197 2198 2199

      result = gfc_copy_expr (a);
    }
  else
    result = gfc_int2real (a, gfc_default_real_kind);
Jerry DeLisle committed
2200

2201 2202 2203 2204 2205
  return range_check (result, "FLOAT");
}


gfc_expr *
2206
gfc_simplify_floor (gfc_expr *e, gfc_expr *k)
2207 2208
{
  gfc_expr *result;
2209
  mpfr_t floor;
2210 2211
  int kind;

2212
  kind = get_kind (BT_INTEGER, k, "FLOOR", gfc_default_integer_kind);
2213 2214 2215 2216 2217 2218
  if (kind == -1)
    gfc_internal_error ("gfc_simplify_floor(): Bad kind");

  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

2219
  gfc_set_model_kind (kind);
Jerry DeLisle committed
2220

2221 2222 2223
  mpfr_init (floor);
  mpfr_floor (floor, e->value.real);

Jerry DeLisle committed
2224
  result = gfc_get_constant_expr (BT_INTEGER, kind, &e->where);
2225
  gfc_mpfr_to_mpz (result->value.integer, floor, &e->where);
2226 2227

  mpfr_clear (floor);
2228 2229 2230 2231 2232 2233

  return range_check (result, "FLOOR");
}


gfc_expr *
2234
gfc_simplify_fraction (gfc_expr *x)
2235 2236
{
  gfc_expr *result;
2237
  mpfr_t absv, exp, pow2;
2238 2239 2240 2241

  if (x->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
2242
  result = gfc_get_constant_expr (BT_REAL, x->ts.kind, &x->where);
2243

2244
  if (mpfr_sgn (x->value.real) == 0)
2245
    {
2246
      mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
2247 2248 2249
      return result;
    }

2250
  gfc_set_model_kind (x->ts.kind);
2251
  mpfr_init (exp);
2252 2253
  mpfr_init (absv);
  mpfr_init (pow2);
2254

2255
  mpfr_abs (absv, x->value.real, GFC_RND_MODE);
2256
  mpfr_log2 (exp, absv, GFC_RND_MODE);
2257

2258 2259
  mpfr_trunc (exp, exp);
  mpfr_add_ui (exp, exp, 1, GFC_RND_MODE);
2260

2261
  mpfr_ui_pow (pow2, 2, exp, GFC_RND_MODE);
2262

2263
  mpfr_div (result->value.real, absv, pow2, GFC_RND_MODE);
2264

2265
  mpfr_clears (exp, absv, pow2, NULL);
2266 2267 2268 2269 2270 2271

  return range_check (result, "FRACTION");
}


gfc_expr *
2272 2273 2274 2275 2276 2277 2278
gfc_simplify_gamma (gfc_expr *x)
{
  gfc_expr *result;

  if (x->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
2279
  result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2280 2281 2282 2283 2284 2285 2286
  mpfr_gamma (result->value.real, x->value.real, GFC_RND_MODE);

  return range_check (result, "GAMMA");
}


gfc_expr *
2287
gfc_simplify_huge (gfc_expr *e)
2288 2289 2290 2291
{
  gfc_expr *result;
  int i;

2292
  i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
Jerry DeLisle committed
2293
  result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
2294 2295 2296

  switch (e->ts.type)
    {
Jerry DeLisle committed
2297 2298 2299
      case BT_INTEGER:
	mpz_set (result->value.integer, gfc_integer_kinds[i].huge);
	break;
2300

Jerry DeLisle committed
2301 2302 2303
      case BT_REAL:
	mpfr_set (result->value.real, gfc_real_kinds[i].huge, GFC_RND_MODE);
	break;
2304

Jerry DeLisle committed
2305 2306
      default:
	gcc_unreachable ();
2307 2308 2309 2310 2311
    }

  return result;
}

2312 2313 2314 2315 2316 2317 2318 2319 2320

gfc_expr *
gfc_simplify_hypot (gfc_expr *x, gfc_expr *y)
{
  gfc_expr *result;

  if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
2321
  result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2322 2323 2324 2325 2326
  mpfr_hypot (result->value.real, x->value.real, y->value.real, GFC_RND_MODE);
  return range_check (result, "HYPOT");
}


2327
/* We use the processor's collating sequence, because all
2328
   systems that gfortran currently works on are ASCII.  */
2329 2330

gfc_expr *
2331
gfc_simplify_iachar (gfc_expr *e, gfc_expr *kind)
2332 2333
{
  gfc_expr *result;
2334
  gfc_char_t index;
Jerry DeLisle committed
2335
  int k;
2336 2337 2338 2339 2340 2341 2342 2343 2344 2345

  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

  if (e->value.character.length != 1)
    {
      gfc_error ("Argument of IACHAR at %L must be of length one", &e->where);
      return &gfc_bad_expr;
    }

2346
  index = e->value.character.string[0];
2347 2348 2349 2350

  if (gfc_option.warn_surprising && index > 127)
    gfc_warning ("Argument of IACHAR function at %L outside of range 0..127",
		 &e->where);
2351

Jerry DeLisle committed
2352 2353
  k = get_kind (BT_INTEGER, kind, "IACHAR", gfc_default_integer_kind);
  if (k == -1)
2354 2355
    return &gfc_bad_expr;

Jerry DeLisle committed
2356
  result = gfc_get_int_expr (k, &e->where, index);
2357 2358 2359 2360 2361

  return range_check (result, "IACHAR");
}


2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399
static gfc_expr *
do_bit_and (gfc_expr *result, gfc_expr *e)
{
  gcc_assert (e->ts.type == BT_INTEGER && e->expr_type == EXPR_CONSTANT);
  gcc_assert (result->ts.type == BT_INTEGER
	      && result->expr_type == EXPR_CONSTANT);

  mpz_and (result->value.integer, result->value.integer, e->value.integer);
  return result;
}


gfc_expr *
gfc_simplify_iall (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
{
  return simplify_transformation (array, dim, mask, -1, do_bit_and);
}


static gfc_expr *
do_bit_ior (gfc_expr *result, gfc_expr *e)
{
  gcc_assert (e->ts.type == BT_INTEGER && e->expr_type == EXPR_CONSTANT);
  gcc_assert (result->ts.type == BT_INTEGER
	      && result->expr_type == EXPR_CONSTANT);

  mpz_ior (result->value.integer, result->value.integer, e->value.integer);
  return result;
}


gfc_expr *
gfc_simplify_iany (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
{
  return simplify_transformation (array, dim, mask, 0, do_bit_ior);
}


2400
gfc_expr *
2401
gfc_simplify_iand (gfc_expr *x, gfc_expr *y)
2402 2403 2404 2405 2406 2407
{
  gfc_expr *result;

  if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
2408
  result = gfc_get_constant_expr (BT_INTEGER, x->ts.kind, &x->where);
2409 2410 2411 2412 2413 2414 2415
  mpz_and (result->value.integer, x->value.integer, y->value.integer);

  return range_check (result, "IAND");
}


gfc_expr *
2416
gfc_simplify_ibclr (gfc_expr *x, gfc_expr *y)
2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429
{
  gfc_expr *result;
  int k, pos;

  if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
    return NULL;

  if (gfc_extract_int (y, &pos) != NULL || pos < 0)
    {
      gfc_error ("Invalid second argument of IBCLR at %L", &y->where);
      return &gfc_bad_expr;
    }

2430
  k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
2431

2432
  if (pos >= gfc_integer_kinds[k].bit_size)
2433 2434 2435 2436 2437 2438 2439 2440
    {
      gfc_error ("Second argument of IBCLR exceeds bit size at %L",
		 &y->where);
      return &gfc_bad_expr;
    }

  result = gfc_copy_expr (x);

2441 2442 2443
  convert_mpz_to_unsigned (result->value.integer,
			   gfc_integer_kinds[k].bit_size);

2444
  mpz_clrbit (result->value.integer, pos);
2445 2446 2447 2448

  convert_mpz_to_signed (result->value.integer,
			 gfc_integer_kinds[k].bit_size);

2449
  return result;
2450 2451 2452 2453
}


gfc_expr *
2454
gfc_simplify_ibits (gfc_expr *x, gfc_expr *y, gfc_expr *z)
2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477
{
  gfc_expr *result;
  int pos, len;
  int i, k, bitsize;
  int *bits;

  if (x->expr_type != EXPR_CONSTANT
      || y->expr_type != EXPR_CONSTANT
      || z->expr_type != EXPR_CONSTANT)
    return NULL;

  if (gfc_extract_int (y, &pos) != NULL || pos < 0)
    {
      gfc_error ("Invalid second argument of IBITS at %L", &y->where);
      return &gfc_bad_expr;
    }

  if (gfc_extract_int (z, &len) != NULL || len < 0)
    {
      gfc_error ("Invalid third argument of IBITS at %L", &z->where);
      return &gfc_bad_expr;
    }

2478
  k = gfc_validate_kind (BT_INTEGER, x->ts.kind, false);
2479 2480 2481 2482 2483

  bitsize = gfc_integer_kinds[k].bit_size;

  if (pos + len > bitsize)
    {
2484 2485
      gfc_error ("Sum of second and third arguments of IBITS exceeds "
		 "bit size at %L", &y->where);
2486 2487 2488
      return &gfc_bad_expr;
    }

Jerry DeLisle committed
2489
  result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
2490 2491
  convert_mpz_to_unsigned (result->value.integer,
			   gfc_integer_kinds[k].bit_size);
2492

2493
  bits = XCNEWVEC (int, bitsize);
2494 2495 2496 2497 2498 2499 2500 2501 2502 2503

  for (i = 0; i < bitsize; i++)
    bits[i] = 0;

  for (i = 0; i < len; i++)
    bits[i] = mpz_tstbit (x->value.integer, i + pos);

  for (i = 0; i < bitsize; i++)
    {
      if (bits[i] == 0)
2504
	mpz_clrbit (result->value.integer, i);
2505
      else if (bits[i] == 1)
2506
	mpz_setbit (result->value.integer, i);
2507
      else
2508
	gfc_internal_error ("IBITS: Bad bit");
2509 2510 2511 2512
    }

  gfc_free (bits);

2513 2514 2515 2516
  convert_mpz_to_signed (result->value.integer,
			 gfc_integer_kinds[k].bit_size);

  return result;
2517 2518 2519 2520
}


gfc_expr *
2521
gfc_simplify_ibset (gfc_expr *x, gfc_expr *y)
2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534
{
  gfc_expr *result;
  int k, pos;

  if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
    return NULL;

  if (gfc_extract_int (y, &pos) != NULL || pos < 0)
    {
      gfc_error ("Invalid second argument of IBSET at %L", &y->where);
      return &gfc_bad_expr;
    }

2535
  k = gfc_validate_kind (x->ts.type, x->ts.kind, false);
2536

2537
  if (pos >= gfc_integer_kinds[k].bit_size)
2538 2539 2540 2541 2542 2543 2544 2545
    {
      gfc_error ("Second argument of IBSET exceeds bit size at %L",
		 &y->where);
      return &gfc_bad_expr;
    }

  result = gfc_copy_expr (x);

2546 2547 2548
  convert_mpz_to_unsigned (result->value.integer,
			   gfc_integer_kinds[k].bit_size);

2549
  mpz_setbit (result->value.integer, pos);
2550

2551 2552
  convert_mpz_to_signed (result->value.integer,
			 gfc_integer_kinds[k].bit_size);
2553

2554
  return result;
2555 2556 2557 2558
}


gfc_expr *
2559
gfc_simplify_ichar (gfc_expr *e, gfc_expr *kind)
2560 2561
{
  gfc_expr *result;
2562
  gfc_char_t index;
Jerry DeLisle committed
2563
  int k;
2564 2565 2566 2567 2568 2569 2570 2571 2572 2573

  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

  if (e->value.character.length != 1)
    {
      gfc_error ("Argument of ICHAR at %L must be of length one", &e->where);
      return &gfc_bad_expr;
    }

2574
  index = e->value.character.string[0];
2575

Jerry DeLisle committed
2576 2577
  k = get_kind (BT_INTEGER, kind, "ICHAR", gfc_default_integer_kind);
  if (k == -1)
2578 2579
    return &gfc_bad_expr;

Jerry DeLisle committed
2580 2581
  result = gfc_get_int_expr (k, &e->where, index);

2582 2583 2584 2585 2586
  return range_check (result, "ICHAR");
}


gfc_expr *
2587
gfc_simplify_ieor (gfc_expr *x, gfc_expr *y)
2588 2589 2590 2591 2592 2593
{
  gfc_expr *result;

  if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
2594
  result = gfc_get_constant_expr (BT_INTEGER, x->ts.kind, &x->where);
2595 2596 2597 2598 2599 2600 2601
  mpz_xor (result->value.integer, x->value.integer, y->value.integer);

  return range_check (result, "IEOR");
}


gfc_expr *
2602
gfc_simplify_index (gfc_expr *x, gfc_expr *y, gfc_expr *b, gfc_expr *kind)
2603 2604 2605 2606 2607
{
  gfc_expr *result;
  int back, len, lensub;
  int i, j, k, count, index = 0, start;

Bud Davis committed
2608 2609
  if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT 
      || ( b != NULL && b->expr_type !=  EXPR_CONSTANT))
2610 2611 2612 2613 2614 2615 2616
    return NULL;

  if (b != NULL && b->value.logical != 0)
    back = 1;
  else
    back = 0;

2617 2618 2619 2620
  k = get_kind (BT_INTEGER, kind, "INDEX", gfc_default_integer_kind); 
  if (k == -1)
    return &gfc_bad_expr;

Jerry DeLisle committed
2621
  result = gfc_get_constant_expr (BT_INTEGER, k, &x->where);
2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644

  len = x->value.character.length;
  lensub = y->value.character.length;

  if (len < lensub)
    {
      mpz_set_si (result->value.integer, 0);
      return result;
    }

  if (back == 0)
    {
      if (lensub == 0)
	{
	  mpz_set_si (result->value.integer, 1);
	  return result;
	}
      else if (lensub == 1)
	{
	  for (i = 0; i < len; i++)
	    {
	      for (j = 0; j < lensub; j++)
		{
2645 2646
		  if (y->value.character.string[j]
		      == x->value.character.string[i])
2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659
		    {
		      index = i + 1;
		      goto done;
		    }
		}
	    }
	}
      else
	{
	  for (i = 0; i < len; i++)
	    {
	      for (j = 0; j < lensub; j++)
		{
2660 2661
		  if (y->value.character.string[j]
		      == x->value.character.string[i])
2662 2663 2664 2665 2666 2667
		    {
		      start = i;
		      count = 0;

		      for (k = 0; k < lensub; k++)
			{
2668 2669
			  if (y->value.character.string[k]
			      == x->value.character.string[k + start])
2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696
			    count++;
			}

		      if (count == lensub)
			{
			  index = start + 1;
			  goto done;
			}
		    }
		}
	    }
	}

    }
  else
    {
      if (lensub == 0)
	{
	  mpz_set_si (result->value.integer, len + 1);
	  return result;
	}
      else if (lensub == 1)
	{
	  for (i = 0; i < len; i++)
	    {
	      for (j = 0; j < lensub; j++)
		{
2697 2698
		  if (y->value.character.string[j]
		      == x->value.character.string[len - i])
2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711
		    {
		      index = len - i + 1;
		      goto done;
		    }
		}
	    }
	}
      else
	{
	  for (i = 0; i < len; i++)
	    {
	      for (j = 0; j < lensub; j++)
		{
2712 2713
		  if (y->value.character.string[j]
		      == x->value.character.string[len - i])
2714 2715 2716 2717 2718 2719
		    {
		      start = len - i;
		      if (start <= len - lensub)
			{
			  count = 0;
			  for (k = 0; k < lensub; k++)
2720 2721
			    if (y->value.character.string[k]
			        == x->value.character.string[k + start])
2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745
			      count++;

			  if (count == lensub)
			    {
			      index = start + 1;
			      goto done;
			    }
			}
		      else
			{
			  continue;
			}
		    }
		}
	    }
	}
    }

done:
  mpz_set_si (result->value.integer, index);
  return range_check (result, "INDEX");
}


Jerry DeLisle committed
2746 2747
static gfc_expr *
simplify_intconv (gfc_expr *e, int kind, const char *name)
2748
{
Steven G. Kargl committed
2749
  gfc_expr *result = NULL;
2750 2751 2752 2753

  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
2754 2755 2756
  result = gfc_convert_constant (e, BT_INTEGER, kind);
  if (result == &gfc_bad_expr)
    return &gfc_bad_expr;
2757

Jerry DeLisle committed
2758
  return range_check (result, name);
2759 2760 2761
}


Jerry DeLisle committed
2762 2763
gfc_expr *
gfc_simplify_int (gfc_expr *e, gfc_expr *k)
2764
{
Jerry DeLisle committed
2765
  int kind;
2766

Jerry DeLisle committed
2767 2768 2769
  kind = get_kind (BT_INTEGER, k, "INT", gfc_default_integer_kind);
  if (kind == -1)
    return &gfc_bad_expr;
2770

Jerry DeLisle committed
2771
  return simplify_intconv (e, kind, "INT");
2772 2773 2774
}

gfc_expr *
2775
gfc_simplify_int2 (gfc_expr *e)
2776
{
Steven G. Kargl committed
2777
  return simplify_intconv (e, 2, "INT2");
2778 2779
}

2780

2781
gfc_expr *
2782
gfc_simplify_int8 (gfc_expr *e)
2783
{
Steven G. Kargl committed
2784
  return simplify_intconv (e, 8, "INT8");
2785 2786
}

2787

2788
gfc_expr *
2789
gfc_simplify_long (gfc_expr *e)
2790
{
Steven G. Kargl committed
2791
  return simplify_intconv (e, 4, "LONG");
2792 2793 2794
}


2795
gfc_expr *
2796
gfc_simplify_ifix (gfc_expr *e)
2797 2798 2799 2800 2801 2802 2803
{
  gfc_expr *rtrunc, *result;

  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

  rtrunc = gfc_copy_expr (e);
2804
  mpfr_trunc (rtrunc->value.real, e->value.real);
Jerry DeLisle committed
2805 2806 2807

  result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
				  &e->where);
2808
  gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real, &e->where);
2809 2810

  gfc_free_expr (rtrunc);
Jerry DeLisle committed
2811

2812 2813 2814 2815 2816
  return range_check (result, "IFIX");
}


gfc_expr *
2817
gfc_simplify_idint (gfc_expr *e)
2818 2819 2820 2821 2822 2823 2824
{
  gfc_expr *rtrunc, *result;

  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

  rtrunc = gfc_copy_expr (e);
2825
  mpfr_trunc (rtrunc->value.real, e->value.real);
Jerry DeLisle committed
2826 2827 2828

  result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
				  &e->where);
2829
  gfc_mpfr_to_mpz (result->value.integer, rtrunc->value.real, &e->where);
2830 2831

  gfc_free_expr (rtrunc);
Jerry DeLisle committed
2832

2833 2834 2835 2836 2837
  return range_check (result, "IDINT");
}


gfc_expr *
2838
gfc_simplify_ior (gfc_expr *x, gfc_expr *y)
2839 2840 2841 2842 2843 2844
{
  gfc_expr *result;

  if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
2845
  result = gfc_get_constant_expr (BT_INTEGER, x->ts.kind, &x->where);
2846
  mpz_ior (result->value.integer, x->value.integer, y->value.integer);
Jerry DeLisle committed
2847

2848 2849 2850 2851
  return range_check (result, "IOR");
}


2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871
static gfc_expr *
do_bit_xor (gfc_expr *result, gfc_expr *e)
{
  gcc_assert (e->ts.type == BT_INTEGER && e->expr_type == EXPR_CONSTANT);
  gcc_assert (result->ts.type == BT_INTEGER
	      && result->expr_type == EXPR_CONSTANT);

  mpz_xor (result->value.integer, result->value.integer, e->value.integer);
  return result;
}


gfc_expr *
gfc_simplify_iparity (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
{
  return simplify_transformation (array, dim, mask, 0, do_bit_xor);
}



2872
gfc_expr *
2873 2874 2875 2876 2877
gfc_simplify_is_iostat_end (gfc_expr *x)
{
  if (x->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
2878 2879 2880
  return gfc_get_logical_expr (gfc_default_logical_kind, &x->where,
			       mpz_cmp_si (x->value.integer,
					   LIBERROR_END) == 0);
2881 2882 2883 2884 2885 2886 2887 2888 2889
}


gfc_expr *
gfc_simplify_is_iostat_eor (gfc_expr *x)
{
  if (x->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
2890 2891 2892
  return gfc_get_logical_expr (gfc_default_logical_kind, &x->where,
			       mpz_cmp_si (x->value.integer,
					   LIBERROR_EOR) == 0);
2893 2894 2895 2896 2897 2898 2899 2900 2901
}


gfc_expr *
gfc_simplify_isnan (gfc_expr *x)
{
  if (x->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
2902 2903
  return gfc_get_logical_expr (gfc_default_logical_kind, &x->where,
			       mpfr_nan_p (x->value.real));
2904 2905 2906
}


2907 2908 2909 2910 2911 2912
/* Performs a shift on its first argument.  Depending on the last
   argument, the shift can be arithmetic, i.e. with filling from the
   left like in the SHIFTA intrinsic.  */
static gfc_expr *
simplify_shift (gfc_expr *e, gfc_expr *s, const char *name,
		bool arithmetic, int direction)
2913 2914
{
  gfc_expr *result;
2915
  int ashift, *bits, i, k, bitsize, shift;
2916 2917 2918 2919 2920

  if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
    return NULL;
  if (gfc_extract_int (s, &shift) != NULL)
    {
2921
      gfc_error ("Invalid second argument of %s at %L", name, &s->where);
2922 2923 2924
      return &gfc_bad_expr;
    }

2925
  k = gfc_validate_kind (BT_INTEGER, e->ts.kind, false);
2926
  bitsize = gfc_integer_kinds[k].bit_size;
2927

2928
  result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
2929

2930 2931 2932 2933 2934
  if (shift == 0)
    {
      mpz_set (result->value.integer, e->value.integer);
      return result;
    }
2935

2936
  if (direction > 0 && shift < 0)
2937
    {
2938 2939
      /* Left shift, as in SHIFTL.  */
      gfc_error ("Second argument of %s is negative at %L", name, &e->where);
2940 2941
      return &gfc_bad_expr;
    }
2942 2943 2944 2945 2946 2947 2948 2949 2950
  else if (direction < 0)
    {
      /* Right shift, as in SHIFTR or SHIFTA.  */
      if (shift < 0)
	{
	  gfc_error ("Second argument of %s is negative at %L",
		     name, &e->where);
	  return &gfc_bad_expr;
	}
2951

2952 2953
      shift = -shift;
    }
2954

2955 2956 2957
  ashift = (shift >= 0 ? shift : -shift);

  if (ashift > bitsize)
2958
    {
2959 2960 2961
      gfc_error ("Magnitude of second argument of %s exceeds bit size "
		 "at %L", name, &e->where);
      return &gfc_bad_expr;
2962
    }
2963

2964 2965 2966
  bits = XCNEWVEC (int, bitsize);

  for (i = 0; i < bitsize; i++)
2967
    bits[i] = mpz_tstbit (e->value.integer, i);
2968 2969

  if (shift > 0)
2970
    {
2971
      /* Left shift.  */
2972 2973 2974
      for (i = 0; i < shift; i++)
	mpz_clrbit (result->value.integer, i);

2975
      for (i = 0; i < bitsize - shift; i++)
2976 2977 2978 2979 2980 2981 2982
	{
	  if (bits[i] == 0)
	    mpz_clrbit (result->value.integer, i + shift);
	  else
	    mpz_setbit (result->value.integer, i + shift);
	}
    }
2983
  else
2984
    {
2985 2986 2987 2988 2989 2990 2991
      /* Right shift.  */
      if (arithmetic && bits[bitsize - 1])
	for (i = bitsize - 1; i >= bitsize - ashift; i--)
	  mpz_setbit (result->value.integer, i);
      else
	for (i = bitsize - 1; i >= bitsize - ashift; i--)
	  mpz_clrbit (result->value.integer, i);
2992

2993
      for (i = bitsize - 1; i >= ashift; i--)
2994 2995 2996 2997 2998 2999 3000
	{
	  if (bits[i] == 0)
	    mpz_clrbit (result->value.integer, i - ashift);
	  else
	    mpz_setbit (result->value.integer, i - ashift);
	}
    }
3001

3002
  convert_mpz_to_signed (result->value.integer, bitsize);
3003
  gfc_free (bits);
3004

3005
  return result;
3006 3007 3008 3009
}


gfc_expr *
3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051
gfc_simplify_ishft (gfc_expr *e, gfc_expr *s)
{
  return simplify_shift (e, s, "ISHFT", false, 0);
}


gfc_expr *
gfc_simplify_lshift (gfc_expr *e, gfc_expr *s)
{
  return simplify_shift (e, s, "LSHIFT", false, 1);
}


gfc_expr *
gfc_simplify_rshift (gfc_expr *e, gfc_expr *s)
{
  return simplify_shift (e, s, "RSHIFT", true, -1);
}


gfc_expr *
gfc_simplify_shifta (gfc_expr *e, gfc_expr *s)
{
  return simplify_shift (e, s, "SHIFTA", true, -1);
}


gfc_expr *
gfc_simplify_shiftl (gfc_expr *e, gfc_expr *s)
{
  return simplify_shift (e, s, "SHIFTL", false, 1);
}


gfc_expr *
gfc_simplify_shiftr (gfc_expr *e, gfc_expr *s)
{
  return simplify_shift (e, s, "SHIFTR", false, -1);
}


gfc_expr *
3052
gfc_simplify_ishftc (gfc_expr *e, gfc_expr *s, gfc_expr *sz)
3053 3054
{
  gfc_expr *result;
3055
  int shift, ashift, isize, ssize, delta, k;
3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066
  int i, *bits;

  if (e->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
    return NULL;

  if (gfc_extract_int (s, &shift) != NULL)
    {
      gfc_error ("Invalid second argument of ISHFTC at %L", &s->where);
      return &gfc_bad_expr;
    }

3067
  k = gfc_validate_kind (e->ts.type, e->ts.kind, false);
3068
  isize = gfc_integer_kinds[k].bit_size;
3069 3070 3071

  if (sz != NULL)
    {
3072
      if (sz->expr_type != EXPR_CONSTANT)
3073
	return NULL;
3074 3075

      if (gfc_extract_int (sz, &ssize) != NULL || ssize <= 0)
3076 3077 3078 3079
	{
	  gfc_error ("Invalid third argument of ISHFTC at %L", &sz->where);
	  return &gfc_bad_expr;
	}
3080 3081 3082 3083 3084 3085 3086

      if (ssize > isize)
	{
	  gfc_error ("Magnitude of third argument of ISHFTC exceeds "
		     "BIT_SIZE of first argument at %L", &s->where);
	  return &gfc_bad_expr;
	}
3087 3088
    }
  else
3089
    ssize = isize;
3090 3091 3092 3093 3094 3095

  if (shift >= 0)
    ashift = shift;
  else
    ashift = -shift;

3096
  if (ashift > ssize)
3097
    {
3098 3099 3100 3101 3102 3103
      if (sz != NULL)
	gfc_error ("Magnitude of second argument of ISHFTC exceeds "
		   "third argument at %L", &s->where);
      else
	gfc_error ("Magnitude of second argument of ISHFTC exceeds "
		   "BIT_SIZE of first argument at %L", &s->where);
3104 3105 3106
      return &gfc_bad_expr;
    }

Jerry DeLisle committed
3107
  result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
3108

3109 3110
  mpz_set (result->value.integer, e->value.integer);

3111
  if (shift == 0)
3112
    return result;
3113

3114
  convert_mpz_to_unsigned (result->value.integer, isize);
3115

3116
  bits = XCNEWVEC (int, ssize);
3117 3118

  for (i = 0; i < ssize; i++)
3119 3120
    bits[i] = mpz_tstbit (e->value.integer, i);

3121
  delta = ssize - ashift;
3122

3123
  if (shift > 0)
3124 3125 3126 3127 3128
    {
      for (i = 0; i < delta; i++)
	{
	  if (bits[i] == 0)
	    mpz_clrbit (result->value.integer, i + shift);
3129
	  else
3130 3131 3132
	    mpz_setbit (result->value.integer, i + shift);
	}

3133
      for (i = delta; i < ssize; i++)
3134 3135 3136
	{
	  if (bits[i] == 0)
	    mpz_clrbit (result->value.integer, i - delta);
3137
	  else
3138 3139 3140 3141 3142 3143 3144 3145 3146
	    mpz_setbit (result->value.integer, i - delta);
	}
    }
  else
    {
      for (i = 0; i < ashift; i++)
	{
	  if (bits[i] == 0)
	    mpz_clrbit (result->value.integer, i + delta);
3147
	  else
3148 3149 3150
	    mpz_setbit (result->value.integer, i + delta);
	}

3151
      for (i = ashift; i < ssize; i++)
3152 3153 3154
	{
	  if (bits[i] == 0)
	    mpz_clrbit (result->value.integer, i + shift);
3155
	  else
3156 3157 3158
	    mpz_setbit (result->value.integer, i + shift);
	}
    }
3159

3160
  convert_mpz_to_signed (result->value.integer, isize);
3161 3162 3163

  gfc_free (bits);
  return result;
3164 3165 3166 3167
}


gfc_expr *
3168
gfc_simplify_kind (gfc_expr *e)
3169
{
Jerry DeLisle committed
3170
  return gfc_get_int_expr (gfc_default_integer_kind, NULL, e->ts.kind);
3171 3172 3173 3174
}


static gfc_expr *
3175
simplify_bound_dim (gfc_expr *array, gfc_expr *kind, int d, int upper,
3176
		    gfc_array_spec *as, gfc_ref *ref, bool coarray)
3177
{
3178
  gfc_expr *l, *u, *result;
3179
  int k;
3180

3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211
  k = get_kind (BT_INTEGER, kind, upper ? "UBOUND" : "LBOUND",
		gfc_default_integer_kind); 
  if (k == -1)
    return &gfc_bad_expr;

  result = gfc_get_constant_expr (BT_INTEGER, k, &array->where);

  /* For non-variables, LBOUND(expr, DIM=n) = 1 and
     UBOUND(expr, DIM=n) = SIZE(expr, DIM=n).  */
  if (!coarray && array->expr_type != EXPR_VARIABLE)
    {
      if (upper)
	{
	  gfc_expr* dim = result;
	  mpz_set_si (dim->value.integer, d);

	  result = gfc_simplify_size (array, dim, kind);
	  gfc_free_expr (dim);
	  if (!result)
	    goto returnNull;
	}
      else
	mpz_set_si (result->value.integer, 1);

      goto done;
    }

  /* Otherwise, we have a variable expression.  */
  gcc_assert (array->expr_type == EXPR_VARIABLE);
  gcc_assert (as);

3212
  /* The last dimension of an assumed-size array is special.  */
3213 3214
  if ((!coarray && d == as->rank && as->type == AS_ASSUMED_SIZE && !upper)
      || (coarray && d == as->rank + as->corank))
3215 3216
    {
      if (as->lower[d-1]->expr_type == EXPR_CONSTANT)
3217 3218 3219 3220
	{
	  gfc_free_expr (result);
	  return gfc_copy_expr (as->lower[d-1]);
	}
3221

3222 3223
      goto returnNull;
    }
3224

Jerry DeLisle committed
3225
  result = gfc_get_constant_expr (BT_INTEGER, k, &array->where);
3226

3227
  /* Then, we need to know the extent of the given dimension.  */
3228
  if (coarray || ref->u.ar.type == AR_FULL)
3229
    {
3230 3231 3232
      l = as->lower[d-1];
      u = as->upper[d-1];

3233 3234
      if (l->expr_type != EXPR_CONSTANT || u == NULL
	  || u->expr_type != EXPR_CONSTANT)
3235
	goto returnNull;
3236 3237 3238 3239 3240 3241 3242 3243 3244

      if (mpz_cmp (l->value.integer, u->value.integer) > 0)
	{
	  /* Zero extent.  */
	  if (upper)
	    mpz_set_si (result->value.integer, 0);
	  else
	    mpz_set_si (result->value.integer, 1);
	}
3245
      else
3246 3247 3248 3249 3250 3251 3252
	{
	  /* Nonzero extent.  */
	  if (upper)
	    mpz_set (result->value.integer, u->value.integer);
	  else
	    mpz_set (result->value.integer, l->value.integer);
	}
3253 3254 3255 3256
    }
  else
    {
      if (upper)
3257
	{
3258
	  if (gfc_ref_dimen_size (&ref->u.ar, d-1, &result->value.integer, NULL)
3259
	      != SUCCESS)
3260
	    goto returnNull;
3261
	}
3262
      else
3263
	mpz_set_si (result->value.integer, (long int) 1);
3264 3265
    }

3266
done:
3267
  return range_check (result, upper ? "UBOUND" : "LBOUND");
3268 3269 3270 3271

returnNull:
  gfc_free_expr (result);
  return NULL;
3272 3273 3274 3275
}


static gfc_expr *
3276
simplify_bound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind, int upper)
3277 3278 3279 3280 3281
{
  gfc_ref *ref;
  gfc_array_spec *as;
  int d;

3282
  if (array->expr_type != EXPR_VARIABLE)
3283 3284 3285 3286 3287
    {
      as = NULL;
      ref = NULL;
      goto done;
    }
3288

3289 3290
  /* Follow any component references.  */
  as = array->symtree->n.sym->as;
3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304
  for (ref = array->ref; ref; ref = ref->next)
    {
      switch (ref->type)
	{
	case REF_ARRAY:
	  switch (ref->u.ar.type)
	    {
	    case AR_ELEMENT:
	      as = NULL;
	      continue;

	    case AR_FULL:
	      /* We're done because 'as' has already been set in the
		 previous iteration.  */
3305
	      if (!ref->next)
3306
		goto done;
3307 3308

	    /* Fall through.  */
3309 3310 3311

	    case AR_UNKNOWN:
	      return NULL;
3312 3313 3314 3315

	    case AR_SECTION:
	      as = ref->u.ar.as;
	      goto done;
3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331
	    }

	  gcc_unreachable ();

	case REF_COMPONENT:
	  as = ref->u.c.component->as;
	  continue;

	case REF_SUBSTRING:
	  continue;
	}
    }

  gcc_unreachable ();

 done:
3332

3333
  if (as && (as->type == AS_DEFERRED || as->type == AS_ASSUMED_SHAPE))
3334 3335
    return NULL;

3336
  if (dim == NULL)
3337
    {
3338 3339 3340
      /* Multi-dimensional bounds.  */
      gfc_expr *bounds[GFC_MAX_DIMENSIONS];
      gfc_expr *e;
3341
      int k;
3342

3343
      /* UBOUND(ARRAY) is not valid for an assumed-size array.  */
3344
      if (upper && as && as->type == AS_ASSUMED_SIZE)
3345 3346 3347 3348 3349
	{
	  /* An error message will be emitted in
	     check_assumed_size_reference (resolve.c).  */
	  return &gfc_bad_expr;
	}
3350

3351 3352 3353
      /* Simplify the bounds for each dimension.  */
      for (d = 0; d < array->rank; d++)
	{
3354 3355
	  bounds[d] = simplify_bound_dim (array, kind, d + 1, upper, as, ref,
					  false);
3356 3357 3358
	  if (bounds[d] == NULL || bounds[d] == &gfc_bad_expr)
	    {
	      int j;
3359

3360 3361 3362 3363 3364
	      for (j = 0; j < d; j++)
		gfc_free_expr (bounds[j]);
	      return bounds[d];
	    }
	}
3365

3366
      /* Allocate the result expression.  */
3367
      k = get_kind (BT_INTEGER, kind, upper ? "UBOUND" : "LBOUND",
Jerry DeLisle committed
3368
		    gfc_default_integer_kind);
3369
      if (k == -1)
Jerry DeLisle committed
3370 3371 3372
	return &gfc_bad_expr;

      e = gfc_get_array_expr (BT_INTEGER, k, &array->where);
3373 3374 3375 3376 3377 3378 3379 3380 3381

      /* The result is a rank 1 array; its size is the rank of the first
	 argument to {L,U}BOUND.  */
      e->rank = 1;
      e->shape = gfc_get_shape (1);
      mpz_init_set_ui (e->shape[0], array->rank);

      /* Create the constructor for this array.  */
      for (d = 0; d < array->rank; d++)
Jerry DeLisle committed
3382 3383
	gfc_constructor_append_expr (&e->value.constructor,
				     bounds[d], &e->where);
3384 3385

      return e;
3386 3387 3388
    }
  else
    {
3389 3390 3391
      /* A DIM argument is specified.  */
      if (dim->expr_type != EXPR_CONSTANT)
	return NULL;
3392

3393 3394
      d = mpz_get_si (dim->value.integer);

3395 3396
      if (d < 1 || d > array->rank
	  || (d == array->rank && as && as->type == AS_ASSUMED_SIZE && upper))
3397 3398 3399 3400 3401
	{
	  gfc_error ("DIM argument at %L is out of bounds", &dim->where);
	  return &gfc_bad_expr;
	}

3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426
      return simplify_bound_dim (array, kind, d, upper, as, ref, false);
    }
}


static gfc_expr *
simplify_cobound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind, int upper)
{
  gfc_ref *ref;
  gfc_array_spec *as;
  int d;

  if (array->expr_type != EXPR_VARIABLE)
    return NULL;

  /* Follow any component references.  */
  as = array->symtree->n.sym->as;
  for (ref = array->ref; ref; ref = ref->next)
    {
      switch (ref->type)
	{
	case REF_ARRAY:
	  switch (ref->u.ar.type)
	    {
	    case AR_ELEMENT:
3427 3428 3429 3430 3431 3432 3433
	      if (ref->next == NULL)
		{
		  gcc_assert (ref->u.ar.as->corank > 0
			      && ref->u.ar.as->rank == 0);
		  as = ref->u.ar.as;
		  goto done;
		}
3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533
	      as = NULL;
	      continue;

	    case AR_FULL:
	      /* We're done because 'as' has already been set in the
		 previous iteration.  */
	      if (!ref->next)
	        goto done;

	    /* Fall through.  */

	    case AR_UNKNOWN:
	      return NULL;

	    case AR_SECTION:
	      as = ref->u.ar.as;
	      goto done;
	    }

	  gcc_unreachable ();

	case REF_COMPONENT:
	  as = ref->u.c.component->as;
	  continue;

	case REF_SUBSTRING:
	  continue;
	}
    }

  gcc_unreachable ();

 done:

  if (as->type == AS_DEFERRED || as->type == AS_ASSUMED_SHAPE)
    return NULL;

  if (dim == NULL)
    {
      /* Multi-dimensional cobounds.  */
      gfc_expr *bounds[GFC_MAX_DIMENSIONS];
      gfc_expr *e;
      int k;

      /* Simplify the cobounds for each dimension.  */
      for (d = 0; d < as->corank; d++)
	{
	  bounds[d] = simplify_bound_dim (array, kind, d + 1 + array->rank,
					  upper, as, ref, true);
	  if (bounds[d] == NULL || bounds[d] == &gfc_bad_expr)
	    {
	      int j;

	      for (j = 0; j < d; j++)
		gfc_free_expr (bounds[j]);
	      return bounds[d];
	    }
	}

      /* Allocate the result expression.  */
      e = gfc_get_expr ();
      e->where = array->where;
      e->expr_type = EXPR_ARRAY;
      e->ts.type = BT_INTEGER;
      k = get_kind (BT_INTEGER, kind, upper ? "UCOBOUND" : "LCOBOUND",
		    gfc_default_integer_kind); 
      if (k == -1)
	{
	  gfc_free_expr (e);
	  return &gfc_bad_expr;
	}
      e->ts.kind = k;

      /* The result is a rank 1 array; its size is the rank of the first
	 argument to {L,U}COBOUND.  */
      e->rank = 1;
      e->shape = gfc_get_shape (1);
      mpz_init_set_ui (e->shape[0], as->corank);

      /* Create the constructor for this array.  */
      for (d = 0; d < as->corank; d++)
	gfc_constructor_append_expr (&e->value.constructor,
				     bounds[d], &e->where);
      return e;
    }
  else
    {
      /* A DIM argument is specified.  */
      if (dim->expr_type != EXPR_CONSTANT)
	return NULL;

      d = mpz_get_si (dim->value.integer);

      if (d < 1 || d > as->corank)
	{
	  gfc_error ("DIM argument at %L is out of bounds", &dim->where);
	  return &gfc_bad_expr;
	}

      return simplify_bound_dim (array, kind, d+array->rank, upper, as, ref, true);
3534
    }
3535 3536 3537 3538
}


gfc_expr *
3539
gfc_simplify_lbound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
3540
{
3541
  return simplify_bound (array, dim, kind, 0);
3542 3543 3544 3545
}


gfc_expr *
3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560
gfc_simplify_lcobound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
{
  gfc_expr *e;
  /* return simplify_cobound (array, dim, kind, 0);*/

  e = simplify_cobound (array, dim, kind, 0);
  if (e != NULL)
    return e;

  gfc_error ("Not yet implemented: LCOBOUND for coarray with non-constant "
	     "cobounds at %L", &array->where);
  return &gfc_bad_expr;
}

gfc_expr *
3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572
gfc_simplify_leadz (gfc_expr *e)
{
  unsigned long lz, bs;
  int i;

  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

  i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
  bs = gfc_integer_kinds[i].bit_size;
  if (mpz_cmp_si (e->value.integer, 0) == 0)
    lz = bs;
3573 3574
  else if (mpz_cmp_si (e->value.integer, 0) < 0)
    lz = 0;
3575 3576 3577
  else
    lz = bs - mpz_sizeinbase (e->value.integer, 2);

Jerry DeLisle committed
3578
  return gfc_get_int_expr (gfc_default_integer_kind, &e->where, lz);
3579 3580 3581 3582
}


gfc_expr *
3583
gfc_simplify_len (gfc_expr *e, gfc_expr *kind)
3584 3585
{
  gfc_expr *result;
3586 3587 3588 3589
  int k = get_kind (BT_INTEGER, kind, "LEN", gfc_default_integer_kind);

  if (k == -1)
    return &gfc_bad_expr;
3590

3591 3592
  if (e->expr_type == EXPR_CONSTANT)
    {
Jerry DeLisle committed
3593
      result = gfc_get_constant_expr (BT_INTEGER, k, &e->where);
3594
      mpz_set_si (result->value.integer, e->value.character.length);
Jerry DeLisle committed
3595
      return range_check (result, "LEN");
3596
    }
Jerry DeLisle committed
3597 3598 3599
  else if (e->ts.u.cl != NULL && e->ts.u.cl->length != NULL
	   && e->ts.u.cl->length->expr_type == EXPR_CONSTANT
	   && e->ts.u.cl->length->ts.type == BT_INTEGER)
3600
    {
Jerry DeLisle committed
3601
      result = gfc_get_constant_expr (BT_INTEGER, k, &e->where);
3602
      mpz_set (result->value.integer, e->ts.u.cl->length->value.integer);
Jerry DeLisle committed
3603
      return range_check (result, "LEN");
3604
    }
Jerry DeLisle committed
3605 3606
  else
    return NULL;
3607 3608 3609 3610
}


gfc_expr *
3611
gfc_simplify_len_trim (gfc_expr *e, gfc_expr *kind)
3612 3613
{
  gfc_expr *result;
Jerry DeLisle committed
3614
  int count, len, i;
3615 3616 3617 3618
  int k = get_kind (BT_INTEGER, kind, "LEN_TRIM", gfc_default_integer_kind);

  if (k == -1)
    return &gfc_bad_expr;
3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629

  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

  len = e->value.character.length;
  for (count = 0, i = 1; i <= len; i++)
    if (e->value.character.string[len - i] == ' ')
      count++;
    else
      break;

Jerry DeLisle committed
3630
  result = gfc_get_int_expr (k, &e->where, len - count);
3631 3632 3633
  return range_check (result, "LEN_TRIM");
}

3634
gfc_expr *
Jerry DeLisle committed
3635
gfc_simplify_lgamma (gfc_expr *x)
3636 3637
{
  gfc_expr *result;
3638
  int sg;
3639 3640 3641 3642

  if (x->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
3643
  result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
3644
  mpfr_lgamma (result->value.real, &sg, x->value.real, GFC_RND_MODE);
3645 3646 3647 3648

  return range_check (result, "LGAMMA");
}

3649 3650

gfc_expr *
3651
gfc_simplify_lge (gfc_expr *a, gfc_expr *b)
3652 3653 3654 3655
{
  if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
3656 3657
  return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
			       gfc_compare_string (a, b) >= 0);
3658 3659 3660 3661
}


gfc_expr *
3662
gfc_simplify_lgt (gfc_expr *a, gfc_expr *b)
3663 3664 3665 3666
{
  if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
3667 3668
  return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
			       gfc_compare_string (a, b) > 0);
3669 3670 3671 3672
}


gfc_expr *
3673
gfc_simplify_lle (gfc_expr *a, gfc_expr *b)
3674 3675 3676 3677
{
  if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
3678 3679
  return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
			       gfc_compare_string (a, b) <= 0);
3680 3681 3682 3683
}


gfc_expr *
3684
gfc_simplify_llt (gfc_expr *a, gfc_expr *b)
3685 3686 3687 3688
{
  if (a->expr_type != EXPR_CONSTANT || b->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
3689 3690
  return gfc_get_logical_expr (gfc_default_logical_kind, &a->where,
			       gfc_compare_string (a, b) < 0);
3691 3692 3693 3694
}


gfc_expr *
3695
gfc_simplify_log (gfc_expr *x)
3696 3697 3698 3699 3700 3701
{
  gfc_expr *result;

  if (x->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
3702
  result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
3703

3704 3705 3706
  switch (x->ts.type)
    {
    case BT_REAL:
3707
      if (mpfr_sgn (x->value.real) <= 0)
3708
	{
3709 3710
	  gfc_error ("Argument of LOG at %L cannot be less than or equal "
		     "to zero", &x->where);
3711 3712 3713 3714
	  gfc_free_expr (result);
	  return &gfc_bad_expr;
	}

3715
      mpfr_log (result->value.real, x->value.real, GFC_RND_MODE);
3716 3717 3718
      break;

    case BT_COMPLEX:
3719 3720
      if ((mpfr_sgn (mpc_realref (x->value.complex)) == 0)
	  && (mpfr_sgn (mpc_imagref (x->value.complex)) == 0))
3721 3722 3723 3724 3725 3726 3727
	{
	  gfc_error ("Complex argument of LOG at %L cannot be zero",
		     &x->where);
	  gfc_free_expr (result);
	  return &gfc_bad_expr;
	}

3728
      gfc_set_model_kind (x->ts.kind);
3729
      mpc_log (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740
      break;

    default:
      gfc_internal_error ("gfc_simplify_log: bad type");
    }

  return range_check (result, "LOG");
}


gfc_expr *
3741
gfc_simplify_log10 (gfc_expr *x)
3742 3743 3744 3745 3746 3747
{
  gfc_expr *result;

  if (x->expr_type != EXPR_CONSTANT)
    return NULL;

3748
  if (mpfr_sgn (x->value.real) <= 0)
3749
    {
3750 3751
      gfc_error ("Argument of LOG10 at %L cannot be less than or equal "
		 "to zero", &x->where);
3752 3753 3754
      return &gfc_bad_expr;
    }

Jerry DeLisle committed
3755
  result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
3756
  mpfr_log10 (result->value.real, x->value.real, GFC_RND_MODE);
3757 3758 3759 3760 3761 3762

  return range_check (result, "LOG10");
}


gfc_expr *
3763
gfc_simplify_logical (gfc_expr *e, gfc_expr *k)
3764 3765 3766
{
  int kind;

3767
  kind = get_kind (BT_LOGICAL, k, "LOGICAL", gfc_default_logical_kind);
3768 3769 3770 3771 3772 3773
  if (kind < 0)
    return &gfc_bad_expr;

  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
3774
  return gfc_get_logical_expr (kind, &e->where, e->value.logical);
3775 3776 3777
}


3778 3779 3780 3781
gfc_expr*
gfc_simplify_matmul (gfc_expr *matrix_a, gfc_expr *matrix_b)
{
  gfc_expr *result;
Jerry DeLisle committed
3782 3783
  int row, result_rows, col, result_columns;
  int stride_a, offset_a, stride_b, offset_b;
3784 3785 3786 3787 3788 3789

  if (!is_constant_array_expr (matrix_a)
      || !is_constant_array_expr (matrix_b))
    return NULL;

  gcc_assert (gfc_compare_types (&matrix_a->ts, &matrix_b->ts));
Jerry DeLisle committed
3790 3791 3792
  result = gfc_get_array_expr (matrix_a->ts.type,
			       matrix_a->ts.kind,
			       &matrix_a->where);
3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830

  if (matrix_a->rank == 1 && matrix_b->rank == 2)
    {
      result_rows = 1;
      result_columns = mpz_get_si (matrix_b->shape[0]);
      stride_a = 1;
      stride_b = mpz_get_si (matrix_b->shape[0]);

      result->rank = 1;
      result->shape = gfc_get_shape (result->rank);
      mpz_init_set_si (result->shape[0], result_columns);
    }
  else if (matrix_a->rank == 2 && matrix_b->rank == 1)
    {
      result_rows = mpz_get_si (matrix_b->shape[0]);
      result_columns = 1;
      stride_a = mpz_get_si (matrix_a->shape[0]);
      stride_b = 1;

      result->rank = 1;
      result->shape = gfc_get_shape (result->rank);
      mpz_init_set_si (result->shape[0], result_rows);
    }
  else if (matrix_a->rank == 2 && matrix_b->rank == 2)
    {
      result_rows = mpz_get_si (matrix_a->shape[0]);
      result_columns = mpz_get_si (matrix_b->shape[1]);
      stride_a = mpz_get_si (matrix_a->shape[1]);
      stride_b = mpz_get_si (matrix_b->shape[0]);

      result->rank = 2;
      result->shape = gfc_get_shape (result->rank);
      mpz_init_set_si (result->shape[0], result_rows);
      mpz_init_set_si (result->shape[1], result_columns);
    }
  else
    gcc_unreachable();

Jerry DeLisle committed
3831
  offset_a = offset_b = 0;
3832 3833
  for (col = 0; col < result_columns; ++col)
    {
Jerry DeLisle committed
3834
      offset_a = 0;
3835 3836 3837

      for (row = 0; row < result_rows; ++row)
	{
Jerry DeLisle committed
3838 3839 3840 3841
	  gfc_expr *e = compute_dot_product (matrix_a, stride_a, offset_a,
					     matrix_b, 1, offset_b);
	  gfc_constructor_append_expr (&result->value.constructor,
				       e, NULL);
3842

Jerry DeLisle committed
3843 3844
	  offset_a += 1;
        }
3845

Jerry DeLisle committed
3846
      offset_b += stride_b;
3847 3848 3849 3850 3851 3852
    }

  return result;
}


3853
gfc_expr *
3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920
gfc_simplify_maskr (gfc_expr *i, gfc_expr *kind_arg)
{
  gfc_expr *result;
  int kind, arg, k;
  const char *s;

  if (i->expr_type != EXPR_CONSTANT)
    return NULL;
 
  kind = get_kind (BT_INTEGER, kind_arg, "MASKR", gfc_default_integer_kind);
  if (kind == -1)
    return &gfc_bad_expr;
  k = gfc_validate_kind (BT_INTEGER, kind, false);

  s = gfc_extract_int (i, &arg);
  gcc_assert (!s);

  result = gfc_get_constant_expr (BT_INTEGER, kind, &i->where);

  /* MASKR(n) = 2^n - 1 */
  mpz_set_ui (result->value.integer, 1);
  mpz_mul_2exp (result->value.integer, result->value.integer, arg);
  mpz_sub_ui (result->value.integer, result->value.integer, 1);

  convert_mpz_to_signed (result->value.integer, gfc_integer_kinds[k].bit_size);

  return result;
}


gfc_expr *
gfc_simplify_maskl (gfc_expr *i, gfc_expr *kind_arg)
{
  gfc_expr *result;
  int kind, arg, k;
  const char *s;
  mpz_t z;

  if (i->expr_type != EXPR_CONSTANT)
    return NULL;
 
  kind = get_kind (BT_INTEGER, kind_arg, "MASKL", gfc_default_integer_kind);
  if (kind == -1)
    return &gfc_bad_expr;
  k = gfc_validate_kind (BT_INTEGER, kind, false);

  s = gfc_extract_int (i, &arg);
  gcc_assert (!s);

  result = gfc_get_constant_expr (BT_INTEGER, kind, &i->where);

  /* MASKL(n) = 2^bit_size - 2^(bit_size - n) */
  mpz_init_set_ui (z, 1);
  mpz_mul_2exp (z, z, gfc_integer_kinds[k].bit_size);
  mpz_set_ui (result->value.integer, 1);
  mpz_mul_2exp (result->value.integer, result->value.integer,
		gfc_integer_kinds[k].bit_size - arg);
  mpz_sub (result->value.integer, z, result->value.integer);
  mpz_clear (z);

  convert_mpz_to_signed (result->value.integer, gfc_integer_kinds[k].bit_size);

  return result;
}


gfc_expr *
3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931
gfc_simplify_merge (gfc_expr *tsource, gfc_expr *fsource, gfc_expr *mask)
{
  if (tsource->expr_type != EXPR_CONSTANT
      || fsource->expr_type != EXPR_CONSTANT
      || mask->expr_type != EXPR_CONSTANT)
    return NULL;

  return gfc_copy_expr (mask->value.logical ? tsource : fsource);
}


3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963
gfc_expr *
gfc_simplify_merge_bits (gfc_expr *i, gfc_expr *j, gfc_expr *mask_expr)
{
  mpz_t arg1, arg2, mask;
  gfc_expr *result;

  if (i->expr_type != EXPR_CONSTANT || j->expr_type != EXPR_CONSTANT
      || mask_expr->expr_type != EXPR_CONSTANT)
    return NULL;

  result = gfc_get_constant_expr (BT_INTEGER, i->ts.kind, &i->where);

  /* Convert all argument to unsigned.  */
  mpz_init_set (arg1, i->value.integer);
  mpz_init_set (arg2, j->value.integer);
  mpz_init_set (mask, mask_expr->value.integer);

  /* MERGE_BITS(I,J,MASK) = IOR (IAND (I, MASK), IAND (J, NOT (MASK))).  */
  mpz_and (arg1, arg1, mask);
  mpz_com (mask, mask);
  mpz_and (arg2, arg2, mask);
  mpz_ior (result->value.integer, arg1, arg2);

  mpz_clear (arg1);
  mpz_clear (arg2);
  mpz_clear (mask);

  return result;
}


/* Selects between current value and extremum for simplify_min_max
3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022
   and simplify_minval_maxval.  */
static void
min_max_choose (gfc_expr *arg, gfc_expr *extremum, int sign)
{
  switch (arg->ts.type)
    {
      case BT_INTEGER:
	if (mpz_cmp (arg->value.integer,
			extremum->value.integer) * sign > 0)
	mpz_set (extremum->value.integer, arg->value.integer);
	break;

      case BT_REAL:
	/* We need to use mpfr_min and mpfr_max to treat NaN properly.  */
	if (sign > 0)
	  mpfr_max (extremum->value.real, extremum->value.real,
		      arg->value.real, GFC_RND_MODE);
	else
	  mpfr_min (extremum->value.real, extremum->value.real,
		      arg->value.real, GFC_RND_MODE);
	break;

      case BT_CHARACTER:
#define LENGTH(x) ((x)->value.character.length)
#define STRING(x) ((x)->value.character.string)
	if (LENGTH(extremum) < LENGTH(arg))
	  {
	    gfc_char_t *tmp = STRING(extremum);

	    STRING(extremum) = gfc_get_wide_string (LENGTH(arg) + 1);
	    memcpy (STRING(extremum), tmp,
		      LENGTH(extremum) * sizeof (gfc_char_t));
	    gfc_wide_memset (&STRING(extremum)[LENGTH(extremum)], ' ',
			       LENGTH(arg) - LENGTH(extremum));
	    STRING(extremum)[LENGTH(arg)] = '\0';  /* For debugger  */
	    LENGTH(extremum) = LENGTH(arg);
	    gfc_free (tmp);
	  }

	if (gfc_compare_string (arg, extremum) * sign > 0)
	  {
	    gfc_free (STRING(extremum));
	    STRING(extremum) = gfc_get_wide_string (LENGTH(extremum) + 1);
	    memcpy (STRING(extremum), STRING(arg),
		      LENGTH(arg) * sizeof (gfc_char_t));
	    gfc_wide_memset (&STRING(extremum)[LENGTH(arg)], ' ',
			       LENGTH(extremum) - LENGTH(arg));
	    STRING(extremum)[LENGTH(extremum)] = '\0';  /* For debugger  */
	  }
#undef LENGTH
#undef STRING
	break;
	      
      default:
	gfc_internal_error ("simplify_min_max(): Bad type in arglist");
    }
}


4023 4024 4025 4026 4027 4028 4029 4030
/* This function is special since MAX() can take any number of
   arguments.  The simplified expression is a rewritten version of the
   argument list containing at most one constant element.  Other
   constant elements are deleted.  Because the argument list has
   already been checked, this function always succeeds.  sign is 1 for
   MAX(), -1 for MIN().  */

static gfc_expr *
4031
simplify_min_max (gfc_expr *expr, int sign)
4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052
{
  gfc_actual_arglist *arg, *last, *extremum;
  gfc_intrinsic_sym * specific;

  last = NULL;
  extremum = NULL;
  specific = expr->value.function.isym;

  arg = expr->value.function.actual;

  for (; arg; last = arg, arg = arg->next)
    {
      if (arg->expr->expr_type != EXPR_CONSTANT)
	continue;

      if (extremum == NULL)
	{
	  extremum = arg;
	  continue;
	}

4053
      min_max_choose (arg->expr, extremum->expr, sign);
4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084

      /* Delete the extra constant argument.  */
      if (last == NULL)
	expr->value.function.actual = arg->next;
      else
	last->next = arg->next;

      arg->next = NULL;
      gfc_free_actual_arglist (arg);
      arg = last;
    }

  /* If there is one value left, replace the function call with the
     expression.  */
  if (expr->value.function.actual->next != NULL)
    return NULL;

  /* Convert to the correct type and kind.  */
  if (expr->ts.type != BT_UNKNOWN) 
    return gfc_convert_constant (expr->value.function.actual->expr,
	expr->ts.type, expr->ts.kind);

  if (specific->ts.type != BT_UNKNOWN) 
    return gfc_convert_constant (expr->value.function.actual->expr,
	specific->ts.type, specific->ts.kind); 
 
  return gfc_copy_expr (expr->value.function.actual->expr);
}


gfc_expr *
4085
gfc_simplify_min (gfc_expr *e)
4086 4087 4088 4089 4090 4091
{
  return simplify_min_max (e, -1);
}


gfc_expr *
4092
gfc_simplify_max (gfc_expr *e)
4093 4094 4095 4096 4097
{
  return simplify_min_max (e, 1);
}


4098 4099 4100 4101 4102 4103
/* This is a simplified version of simplify_min_max to provide
   simplification of minval and maxval for a vector.  */

static gfc_expr *
simplify_minval_maxval (gfc_expr *expr, int sign)
{
Jerry DeLisle committed
4104
  gfc_constructor *c, *extremum;
4105 4106 4107 4108 4109
  gfc_intrinsic_sym * specific;

  extremum = NULL;
  specific = expr->value.function.isym;

Jerry DeLisle committed
4110 4111
  for (c = gfc_constructor_first (expr->value.constructor);
       c; c = gfc_constructor_next (c))
4112
    {
Jerry DeLisle committed
4113
      if (c->expr->expr_type != EXPR_CONSTANT)
4114 4115 4116 4117
	return NULL;

      if (extremum == NULL)
	{
Jerry DeLisle committed
4118
	  extremum = c;
4119 4120 4121
	  continue;
	}

Jerry DeLisle committed
4122
      min_max_choose (c->expr, extremum->expr, sign);
4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145
     }

  if (extremum == NULL)
    return NULL;

  /* Convert to the correct type and kind.  */
  if (expr->ts.type != BT_UNKNOWN) 
    return gfc_convert_constant (extremum->expr,
	expr->ts.type, expr->ts.kind);

  if (specific->ts.type != BT_UNKNOWN) 
    return gfc_convert_constant (extremum->expr,
	specific->ts.type, specific->ts.kind); 
 
  return gfc_copy_expr (extremum->expr);
}


gfc_expr *
gfc_simplify_minval (gfc_expr *array, gfc_expr* dim, gfc_expr *mask)
{
  if (array->expr_type != EXPR_ARRAY || array->rank != 1 || dim || mask)
    return NULL;
Jerry DeLisle committed
4146

4147 4148 4149 4150 4151 4152 4153 4154 4155
  return simplify_minval_maxval (array, -1);
}


gfc_expr *
gfc_simplify_maxval (gfc_expr *array, gfc_expr* dim, gfc_expr *mask)
{
  if (array->expr_type != EXPR_ARRAY || array->rank != 1 || dim || mask)
    return NULL;
Jerry DeLisle committed
4156

4157 4158 4159 4160
  return simplify_minval_maxval (array, 1);
}


4161
gfc_expr *
4162
gfc_simplify_maxexponent (gfc_expr *x)
4163
{
Jerry DeLisle committed
4164 4165 4166
  int i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
  return gfc_get_int_expr (gfc_default_integer_kind, &x->where,
			   gfc_real_kinds[i].max_exponent);
4167 4168 4169 4170
}


gfc_expr *
4171
gfc_simplify_minexponent (gfc_expr *x)
4172
{
Jerry DeLisle committed
4173 4174 4175
  int i = gfc_validate_kind (BT_REAL, x->ts.kind, false);
  return gfc_get_int_expr (gfc_default_integer_kind, &x->where,
			   gfc_real_kinds[i].min_exponent);
4176 4177 4178 4179
}


gfc_expr *
4180
gfc_simplify_mod (gfc_expr *a, gfc_expr *p)
4181 4182
{
  gfc_expr *result;
4183
  mpfr_t tmp;
4184
  int kind;
4185 4186 4187 4188

  if (a->expr_type != EXPR_CONSTANT || p->expr_type != EXPR_CONSTANT)
    return NULL;

4189
  kind = a->ts.kind > p->ts.kind ? a->ts.kind : p->ts.kind;
Jerry DeLisle committed
4190
  result = gfc_get_constant_expr (a->ts.type, kind, &a->where);
4191 4192 4193

  switch (a->ts.type)
    {
Jerry DeLisle committed
4194 4195 4196 4197 4198 4199 4200 4201 4202 4203
      case BT_INTEGER:
	if (mpz_cmp_ui (p->value.integer, 0) == 0)
	  {
	    /* Result is processor-dependent.  */
	    gfc_error ("Second argument MOD at %L is zero", &a->where);
	    gfc_free_expr (result);
	    return &gfc_bad_expr;
	  }
	mpz_tdiv_r (result->value.integer, a->value.integer, p->value.integer);
	break;
4204

Jerry DeLisle committed
4205 4206 4207 4208 4209 4210 4211 4212
      case BT_REAL:
	if (mpfr_cmp_ui (p->value.real, 0) == 0)
	  {
	    /* Result is processor-dependent.  */
	    gfc_error ("Second argument of MOD at %L is zero", &p->where);
	    gfc_free_expr (result);
	    return &gfc_bad_expr;
	  }
4213

Jerry DeLisle committed
4214 4215 4216 4217 4218 4219 4220 4221
	gfc_set_model_kind (kind);
	mpfr_init (tmp);
	mpfr_div (tmp, a->value.real, p->value.real, GFC_RND_MODE);
	mpfr_trunc (tmp, tmp);
	mpfr_mul (tmp, tmp, p->value.real, GFC_RND_MODE);
	mpfr_sub (result->value.real, a->value.real, tmp, GFC_RND_MODE);
	mpfr_clear (tmp);
	break;
4222

Jerry DeLisle committed
4223 4224
      default:
	gfc_internal_error ("gfc_simplify_mod(): Bad arguments");
4225 4226 4227 4228 4229 4230 4231
    }

  return range_check (result, "MOD");
}


gfc_expr *
4232
gfc_simplify_modulo (gfc_expr *a, gfc_expr *p)
4233 4234
{
  gfc_expr *result;
4235
  mpfr_t tmp;
4236
  int kind;
4237 4238 4239 4240

  if (a->expr_type != EXPR_CONSTANT || p->expr_type != EXPR_CONSTANT)
    return NULL;

4241
  kind = a->ts.kind > p->ts.kind ? a->ts.kind : p->ts.kind;
Jerry DeLisle committed
4242
  result = gfc_get_constant_expr (a->ts.type, kind, &a->where);
4243 4244 4245

  switch (a->ts.type)
    {
Jerry DeLisle committed
4246 4247 4248 4249 4250 4251 4252 4253 4254 4255
      case BT_INTEGER:
	if (mpz_cmp_ui (p->value.integer, 0) == 0)
	  {
	    /* Result is processor-dependent. This processor just opts
	      to not handle it at all.  */
	    gfc_error ("Second argument of MODULO at %L is zero", &a->where);
	    gfc_free_expr (result);
	    return &gfc_bad_expr;
	  }
	mpz_fdiv_r (result->value.integer, a->value.integer, p->value.integer);
4256

Jerry DeLisle committed
4257
	break;
4258

Jerry DeLisle committed
4259 4260 4261 4262 4263 4264 4265 4266
      case BT_REAL:
	if (mpfr_cmp_ui (p->value.real, 0) == 0)
	  {
	    /* Result is processor-dependent.  */
	    gfc_error ("Second argument of MODULO at %L is zero", &p->where);
	    gfc_free_expr (result);
	    return &gfc_bad_expr;
	  }
4267

Jerry DeLisle committed
4268 4269 4270 4271 4272 4273 4274 4275
	gfc_set_model_kind (kind);
	mpfr_init (tmp);
	mpfr_div (tmp, a->value.real, p->value.real, GFC_RND_MODE);
	mpfr_floor (tmp, tmp);
	mpfr_mul (tmp, tmp, p->value.real, GFC_RND_MODE);
	mpfr_sub (result->value.real, a->value.real, tmp, GFC_RND_MODE);
	mpfr_clear (tmp);
	break;
4276

Jerry DeLisle committed
4277 4278
      default:
	gfc_internal_error ("gfc_simplify_modulo(): Bad arguments");
4279 4280 4281 4282 4283 4284 4285 4286
    }

  return range_check (result, "MODULO");
}


/* Exists for the sole purpose of consistency with other intrinsics.  */
gfc_expr *
4287 4288 4289 4290 4291
gfc_simplify_mvbits (gfc_expr *f  ATTRIBUTE_UNUSED,
		     gfc_expr *fp ATTRIBUTE_UNUSED,
		     gfc_expr *l  ATTRIBUTE_UNUSED,
		     gfc_expr *to ATTRIBUTE_UNUSED,
		     gfc_expr *tp ATTRIBUTE_UNUSED)
4292 4293 4294 4295 4296 4297
{
  return NULL;
}


gfc_expr *
4298
gfc_simplify_nearest (gfc_expr *x, gfc_expr *s)
4299 4300
{
  gfc_expr *result;
4301 4302
  mp_exp_t emin, emax;
  int kind;
4303

4304
  if (x->expr_type != EXPR_CONSTANT || s->expr_type != EXPR_CONSTANT)
4305 4306
    return NULL;

4307
  if (mpfr_sgn (s->value.real) == 0)
4308
    {
4309 4310
      gfc_error ("Second argument of NEAREST at %L shall not be zero",
		 &s->where);
4311
      return &gfc_bad_expr;
4312 4313
    }

4314 4315
  result = gfc_copy_expr (x);

4316 4317 4318 4319 4320 4321 4322 4323 4324
  /* Save current values of emin and emax.  */
  emin = mpfr_get_emin ();
  emax = mpfr_get_emax ();

  /* Set emin and emax for the current model number.  */
  kind = gfc_validate_kind (BT_REAL, x->ts.kind, 0);
  mpfr_set_emin ((mp_exp_t) gfc_real_kinds[kind].min_exponent -
		mpfr_get_prec(result->value.real) + 1);
  mpfr_set_emax ((mp_exp_t) gfc_real_kinds[kind].max_exponent - 1);
4325
  mpfr_check_range (result->value.real, 0, GMP_RNDU);
4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339

  if (mpfr_sgn (s->value.real) > 0)
    {
      mpfr_nextabove (result->value.real);
      mpfr_subnormalize (result->value.real, 0, GMP_RNDU);
    }
  else
    {
      mpfr_nextbelow (result->value.real);
      mpfr_subnormalize (result->value.real, 0, GMP_RNDD);
    }

  mpfr_set_emin (emin);
  mpfr_set_emax (emax);
4340

4341 4342 4343 4344 4345
  /* Only NaN can occur. Do not use range check as it gives an
     error for denormal numbers.  */
  if (mpfr_nan_p (result->value.real) && gfc_option.flag_range_check)
    {
      gfc_error ("Result of NEAREST is NaN at %L", &result->where);
Steven G. Kargl committed
4346
      gfc_free_expr (result);
4347 4348 4349 4350
      return &gfc_bad_expr;
    }

  return result;
4351 4352 4353 4354
}


static gfc_expr *
4355
simplify_nint (const char *name, gfc_expr *e, gfc_expr *k)
4356
{
4357 4358
  gfc_expr *itrunc, *result;
  int kind;
4359

4360
  kind = get_kind (BT_INTEGER, k, name, gfc_default_integer_kind);
4361 4362 4363 4364 4365 4366 4367
  if (kind == -1)
    return &gfc_bad_expr;

  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

  itrunc = gfc_copy_expr (e);
4368
  mpfr_round (itrunc->value.real, e->value.real);
4369

Jerry DeLisle committed
4370
  result = gfc_get_constant_expr (BT_INTEGER, kind, &e->where);
4371
  gfc_mpfr_to_mpz (result->value.integer, itrunc->value.real, &e->where);
4372 4373 4374 4375 4376 4377 4378 4379

  gfc_free_expr (itrunc);

  return range_check (result, name);
}


gfc_expr *
4380
gfc_simplify_new_line (gfc_expr *e)
4381 4382 4383
{
  gfc_expr *result;

Jerry DeLisle committed
4384
  result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, 1);
4385
  result->value.character.string[0] = '\n';
Jerry DeLisle committed
4386

4387 4388 4389 4390 4391
  return result;
}


gfc_expr *
4392
gfc_simplify_nint (gfc_expr *e, gfc_expr *k)
4393 4394 4395 4396 4397 4398
{
  return simplify_nint ("NINT", e, k);
}


gfc_expr *
4399
gfc_simplify_idnint (gfc_expr *e)
4400 4401 4402 4403 4404
{
  return simplify_nint ("IDNINT", e, NULL);
}


4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463
static gfc_expr *
add_squared (gfc_expr *result, gfc_expr *e)
{
  mpfr_t tmp;

  gcc_assert (e->ts.type == BT_REAL && e->expr_type == EXPR_CONSTANT);
  gcc_assert (result->ts.type == BT_REAL
	      && result->expr_type == EXPR_CONSTANT);

  gfc_set_model_kind (result->ts.kind);
  mpfr_init (tmp);
  mpfr_pow_ui (tmp, e->value.real, 2, GFC_RND_MODE);
  mpfr_add (result->value.real, result->value.real, tmp,
	    GFC_RND_MODE);
  mpfr_clear (tmp);

  return result;
}


static gfc_expr *
do_sqrt (gfc_expr *result, gfc_expr *e)
{
  gcc_assert (e->ts.type == BT_REAL && e->expr_type == EXPR_CONSTANT);
  gcc_assert (result->ts.type == BT_REAL
	      && result->expr_type == EXPR_CONSTANT);

  mpfr_set (result->value.real, e->value.real, GFC_RND_MODE);
  mpfr_sqrt (result->value.real, result->value.real, GFC_RND_MODE);
  return result;
}


gfc_expr *
gfc_simplify_norm2 (gfc_expr *e, gfc_expr *dim)
{
  gfc_expr *result;

  if (!is_constant_array_expr (e)
      || (dim != NULL && !gfc_is_constant_expr (dim)))
    return NULL;

  result = transformational_result (e, dim, e->ts.type, e->ts.kind, &e->where);
  init_result_expr (result, 0, NULL);

  if (!dim || e->rank == 1)
    {
      result = simplify_transformation_to_scalar (result, e, NULL,
						  add_squared);
      mpfr_sqrt (result->value.real, result->value.real, GFC_RND_MODE);
    }
  else
    result = simplify_transformation_to_array (result, e, dim, NULL,
					       add_squared, &do_sqrt);

  return result;
}


4464
gfc_expr *
4465
gfc_simplify_not (gfc_expr *e)
4466 4467 4468 4469 4470 4471
{
  gfc_expr *result;

  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
4472
  result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
4473 4474 4475 4476 4477 4478 4479
  mpz_com (result->value.integer, e->value.integer);

  return range_check (result, "NOT");
}


gfc_expr *
4480
gfc_simplify_null (gfc_expr *mold)
4481 4482 4483
{
  gfc_expr *result;

Jerry DeLisle committed
4484
  if (mold)
4485
    {
Jerry DeLisle committed
4486 4487
      result = gfc_copy_expr (mold);
      result->expr_type = EXPR_NULL;
4488
    }
4489
  else
Jerry DeLisle committed
4490
    result = gfc_get_null_expr (NULL);
4491 4492 4493 4494 4495 4496

  return result;
}


gfc_expr *
4497 4498 4499
gfc_simplify_num_images (void)
{
  gfc_expr *result;
4500 4501 4502 4503 4504 4505 4506

  if (gfc_option.coarray == GFC_FCOARRAY_NONE)
    {
      gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
      return &gfc_bad_expr;
    }

4507
  /* FIXME: gfc_current_locus is wrong.  */
Jerry DeLisle committed
4508 4509
  result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
				  &gfc_current_locus);
4510 4511 4512 4513 4514 4515
  mpz_set_si (result->value.integer, 1);
  return result;
}


gfc_expr *
4516
gfc_simplify_or (gfc_expr *x, gfc_expr *y)
4517 4518 4519 4520 4521 4522 4523 4524
{
  gfc_expr *result;
  int kind;

  if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
    return NULL;

  kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
Jerry DeLisle committed
4525 4526

  switch (x->ts.type)
4527
    {
Jerry DeLisle committed
4528 4529 4530 4531 4532 4533 4534 4535 4536 4537
      case BT_INTEGER:
	result = gfc_get_constant_expr (BT_INTEGER, kind, &x->where);
	mpz_ior (result->value.integer, x->value.integer, y->value.integer);
	return range_check (result, "OR");

      case BT_LOGICAL:
	return gfc_get_logical_expr (kind, &x->where,
				     x->value.logical || y->value.logical);
      default:
	gcc_unreachable();
4538 4539 4540 4541 4542
    }
}


gfc_expr *
4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553
gfc_simplify_pack (gfc_expr *array, gfc_expr *mask, gfc_expr *vector)
{
  gfc_expr *result;
  gfc_constructor *array_ctor, *mask_ctor, *vector_ctor;

  if (!is_constant_array_expr(array)
      || !is_constant_array_expr(vector)
      || (!gfc_is_constant_expr (mask)
          && !is_constant_array_expr(mask)))
    return NULL;

Jerry DeLisle committed
4554
  result = gfc_get_array_expr (array->ts.type, array->ts.kind, &array->where);
4555 4556
  if (array->ts.type == BT_DERIVED)
    result->ts.u.derived = array->ts.u.derived;
4557

Jerry DeLisle committed
4558 4559 4560 4561
  array_ctor = gfc_constructor_first (array->value.constructor);
  vector_ctor = vector
		  ? gfc_constructor_first (vector->value.constructor)
		  : NULL;
4562 4563 4564 4565 4566 4567 4568

  if (mask->expr_type == EXPR_CONSTANT
      && mask->value.logical)
    {
      /* Copy all elements of ARRAY to RESULT.  */
      while (array_ctor)
	{
Jerry DeLisle committed
4569 4570 4571
	  gfc_constructor_append_expr (&result->value.constructor,
				       gfc_copy_expr (array_ctor->expr),
				       NULL);
4572

Jerry DeLisle committed
4573 4574
	  array_ctor = gfc_constructor_next (array_ctor);
	  vector_ctor = gfc_constructor_next (vector_ctor);
4575 4576 4577 4578 4579 4580
	}
    }
  else if (mask->expr_type == EXPR_ARRAY)
    {
      /* Copy only those elements of ARRAY to RESULT whose 
	 MASK equals .TRUE..  */
Jerry DeLisle committed
4581
      mask_ctor = gfc_constructor_first (mask->value.constructor);
4582 4583 4584 4585
      while (mask_ctor)
	{
	  if (mask_ctor->expr->value.logical)
	    {
Jerry DeLisle committed
4586 4587 4588 4589
	      gfc_constructor_append_expr (&result->value.constructor,
					   gfc_copy_expr (array_ctor->expr),
					   NULL);
	      vector_ctor = gfc_constructor_next (vector_ctor);
4590 4591
	    }

Jerry DeLisle committed
4592 4593
	  array_ctor = gfc_constructor_next (array_ctor);
	  mask_ctor = gfc_constructor_next (mask_ctor);
4594 4595 4596 4597 4598 4599
	}
    }

  /* Append any left-over elements from VECTOR to RESULT.  */
  while (vector_ctor)
    {
Jerry DeLisle committed
4600 4601 4602 4603
      gfc_constructor_append_expr (&result->value.constructor,
				   gfc_copy_expr (vector_ctor->expr),
				   NULL);
      vector_ctor = gfc_constructor_next (vector_ctor);
4604 4605 4606 4607 4608 4609
    }

  result->shape = gfc_get_shape (1);
  gfc_array_size (result, &result->shape[0]);

  if (array->ts.type == BT_CHARACTER)
4610
    result->ts.u.cl = array->ts.u.cl;
4611 4612 4613 4614 4615

  return result;
}


4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631
static gfc_expr *
do_xor (gfc_expr *result, gfc_expr *e)
{
  gcc_assert (e->ts.type == BT_LOGICAL && e->expr_type == EXPR_CONSTANT);
  gcc_assert (result->ts.type == BT_LOGICAL
	      && result->expr_type == EXPR_CONSTANT);

  result->value.logical = result->value.logical != e->value.logical;
  return result;
}



gfc_expr *
gfc_simplify_parity (gfc_expr *e, gfc_expr *dim)
{
4632
  return simplify_transformation (e, dim, NULL, 0, do_xor);
4633 4634 4635
}


4636
gfc_expr *
4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677
gfc_simplify_popcnt (gfc_expr *e)
{
  int res, k;
  mpz_t x;

  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

  k = gfc_validate_kind (e->ts.type, e->ts.kind, false);

  /* Convert argument to unsigned, then count the '1' bits.  */
  mpz_init_set (x, e->value.integer);
  convert_mpz_to_unsigned (x, gfc_integer_kinds[k].bit_size);
  res = mpz_popcount (x);
  mpz_clear (x);

  return gfc_get_int_expr (gfc_default_integer_kind, &e->where, res);
}


gfc_expr *
gfc_simplify_poppar (gfc_expr *e)
{
  gfc_expr *popcnt;
  const char *s;
  int i;

  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

  popcnt = gfc_simplify_popcnt (e);
  gcc_assert (popcnt);

  s = gfc_extract_int (popcnt, &i);
  gcc_assert (!s);

  return gfc_get_int_expr (gfc_default_integer_kind, &e->where, i % 2);
}


gfc_expr *
4678
gfc_simplify_precision (gfc_expr *e)
4679
{
Jerry DeLisle committed
4680 4681 4682
  int i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
  return gfc_get_int_expr (gfc_default_integer_kind, &e->where,
			   gfc_real_kinds[i].precision);
4683 4684 4685 4686
}


gfc_expr *
4687 4688
gfc_simplify_product (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
{
4689
  return simplify_transformation (array, dim, mask, 1, gfc_multiply);
4690 4691 4692 4693
}


gfc_expr *
4694
gfc_simplify_radix (gfc_expr *e)
4695 4696
{
  int i;
4697
  i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
Jerry DeLisle committed
4698

4699 4700
  switch (e->ts.type)
    {
Jerry DeLisle committed
4701 4702 4703
      case BT_INTEGER:
	i = gfc_integer_kinds[i].radix;
	break;
4704

Jerry DeLisle committed
4705 4706 4707
      case BT_REAL:
	i = gfc_real_kinds[i].radix;
	break;
4708

Jerry DeLisle committed
4709 4710
      default:
	gcc_unreachable ();
4711 4712
    }

Jerry DeLisle committed
4713
  return gfc_get_int_expr (gfc_default_integer_kind, &e->where, i);
4714 4715 4716 4717
}


gfc_expr *
4718
gfc_simplify_range (gfc_expr *e)
4719 4720
{
  int i;
4721
  i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
4722 4723 4724

  switch (e->ts.type)
    {
Jerry DeLisle committed
4725 4726 4727
      case BT_INTEGER:
	i = gfc_integer_kinds[i].range;
	break;
4728

Jerry DeLisle committed
4729 4730 4731 4732
      case BT_REAL:
      case BT_COMPLEX:
	i = gfc_real_kinds[i].range;
	break;
4733

Jerry DeLisle committed
4734 4735
      default:
	gcc_unreachable ();
4736 4737
    }

Jerry DeLisle committed
4738
  return gfc_get_int_expr (gfc_default_integer_kind, &e->where, i);
4739 4740 4741 4742
}


gfc_expr *
4743
gfc_simplify_real (gfc_expr *e, gfc_expr *k)
4744
{
4745
  gfc_expr *result = NULL;
4746 4747 4748 4749 4750
  int kind;

  if (e->ts.type == BT_COMPLEX)
    kind = get_kind (BT_REAL, k, "REAL", e->ts.kind);
  else
4751
    kind = get_kind (BT_REAL, k, "REAL", gfc_default_real_kind);
4752 4753 4754 4755 4756 4757 4758

  if (kind == -1)
    return &gfc_bad_expr;

  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
4759 4760
  if (convert_boz (e, kind) == &gfc_bad_expr)
    return &gfc_bad_expr;
4761

Jerry DeLisle committed
4762 4763 4764
  result = gfc_convert_constant (e, BT_REAL, kind);
  if (result == &gfc_bad_expr)
    return &gfc_bad_expr;
Steven G. Kargl committed
4765

4766 4767 4768
  return range_check (result, "REAL");
}

4769 4770

gfc_expr *
4771
gfc_simplify_realpart (gfc_expr *e)
4772 4773 4774 4775 4776 4777
{
  gfc_expr *result;

  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
4778
  result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
4779
  mpc_real (result->value.real, e->value.complex, GFC_RND_MODE);
Jerry DeLisle committed
4780

4781 4782 4783
  return range_check (result, "REALPART");
}

4784
gfc_expr *
4785
gfc_simplify_repeat (gfc_expr *e, gfc_expr *n)
4786 4787
{
  gfc_expr *result;
4788 4789
  int i, j, len, ncop, nlen;
  mpz_t ncopies;
4790
  bool have_length = false;
4791

4792 4793
  /* If NCOPIES isn't a constant, there's nothing we can do.  */
  if (n->expr_type != EXPR_CONSTANT)
4794 4795
    return NULL;

4796 4797
  /* If NCOPIES is negative, it's an error.  */
  if (mpz_sgn (n->value.integer) < 0)
4798
    {
4799 4800
      gfc_error ("Argument NCOPIES of REPEAT intrinsic is negative at %L",
		 &n->where);
4801 4802 4803
      return &gfc_bad_expr;
    }

4804
  /* If we don't know the character length, we can do no more.  */
4805 4806
  if (e->ts.u.cl && e->ts.u.cl->length
	&& e->ts.u.cl->length->expr_type == EXPR_CONSTANT)
4807
    {
4808
      len = mpz_get_si (e->ts.u.cl->length->value.integer);
4809 4810 4811
      have_length = true;
    }
  else if (e->expr_type == EXPR_CONSTANT
4812
	     && (e->ts.u.cl == NULL || e->ts.u.cl->length == NULL))
4813 4814 4815 4816
    {
      len = e->value.character.length;
    }
  else
4817 4818 4819 4820 4821
    return NULL;

  /* If the source length is 0, any value of NCOPIES is valid
     and everything behaves as if NCOPIES == 0.  */
  mpz_init (ncopies);
4822
  if (len == 0)
4823 4824 4825 4826 4827
    mpz_set_ui (ncopies, 0);
  else
    mpz_set (ncopies, n->value.integer);

  /* Check that NCOPIES isn't too large.  */
4828
  if (len)
4829
    {
4830
      mpz_t max, mlen;
4831 4832 4833 4834 4835
      int i;

      /* Compute the maximum value allowed for NCOPIES: huge(cl) / len.  */
      mpz_init (max);
      i = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4836 4837 4838 4839

      if (have_length)
	{
	  mpz_tdiv_q (max, gfc_integer_kinds[i].huge,
4840
		      e->ts.u.cl->length->value.integer);
4841 4842 4843 4844 4845 4846 4847
	}
      else
	{
	  mpz_init_set_si (mlen, len);
	  mpz_tdiv_q (max, gfc_integer_kinds[i].huge, mlen);
	  mpz_clear (mlen);
	}
4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862

      /* The check itself.  */
      if (mpz_cmp (ncopies, max) > 0)
	{
	  mpz_clear (max);
	  mpz_clear (ncopies);
	  gfc_error ("Argument NCOPIES of REPEAT intrinsic is too large at %L",
		     &n->where);
	  return &gfc_bad_expr;
	}

      mpz_clear (max);
    }
  mpz_clear (ncopies);

4863
  /* For further simplification, we need the character string to be
4864 4865 4866 4867
     constant.  */
  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

4868
  if (len || 
4869 4870
      (e->ts.u.cl->length && 
       mpz_sgn (e->ts.u.cl->length->value.integer)) != 0)
4871 4872 4873 4874
    {
      const char *res = gfc_extract_int (n, &ncop);
      gcc_assert (res == NULL);
    }
4875 4876 4877
  else
    ncop = 0;

4878
  len = e->value.character.length;
4879
  nlen = ncop * len;
4880

Jerry DeLisle committed
4881
  result = gfc_get_constant_expr (BT_CHARACTER, e->ts.kind, &e->where);
4882

4883
  if (ncop == 0)
Jerry DeLisle committed
4884
    return gfc_get_character_expr (e->ts.kind, &e->where, NULL, 0);
4885

Jerry DeLisle committed
4886 4887
  len = e->value.character.length;
  nlen = ncop * len;
4888

Jerry DeLisle committed
4889
  result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, nlen);
4890
  for (i = 0; i < ncop; i++)
4891
    for (j = 0; j < len; j++)
4892
      result->value.character.string[j+i*len]= e->value.character.string[j];
4893 4894 4895 4896 4897 4898 4899 4900 4901

  result->value.character.string[nlen] = '\0';	/* For debugger */
  return result;
}


/* This one is a bear, but mainly has to do with shuffling elements.  */

gfc_expr *
4902 4903
gfc_simplify_reshape (gfc_expr *source, gfc_expr *shape_exp,
		      gfc_expr *pad, gfc_expr *order_exp)
4904 4905 4906 4907 4908 4909
{
  int order[GFC_MAX_DIMENSIONS], shape[GFC_MAX_DIMENSIONS];
  int i, rank, npad, x[GFC_MAX_DIMENSIONS];
  mpz_t index, size;
  unsigned long j;
  size_t nsource;
Jerry DeLisle committed
4910
  gfc_expr *e, *result;
4911

4912
  /* Check that argument expression types are OK.  */
4913 4914 4915 4916
  if (!is_constant_array_expr (source)
      || !is_constant_array_expr (shape_exp)
      || !is_constant_array_expr (pad)
      || !is_constant_array_expr (order_exp))
4917 4918
    return NULL;

4919 4920
  /* Proceed with simplification, unpacking the array.  */

4921 4922 4923 4924 4925
  mpz_init (index);
  rank = 0;

  for (;;)
    {
Jerry DeLisle committed
4926
      e = gfc_constructor_lookup_expr (shape_exp->value.constructor, rank);
4927 4928 4929
      if (e == NULL)
	break;

4930
      gfc_extract_int (e, &shape[rank]);
4931

4932 4933
      gcc_assert (rank >= 0 && rank < GFC_MAX_DIMENSIONS);
      gcc_assert (shape[rank] >= 0);
4934 4935 4936 4937

      rank++;
    }

4938
  gcc_assert (rank > 0);
4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952

  /* Now unpack the order array if present.  */
  if (order_exp == NULL)
    {
      for (i = 0; i < rank; i++)
	order[i] = i;
    }
  else
    {
      for (i = 0; i < rank; i++)
	x[i] = 0;

      for (i = 0; i < rank; i++)
	{
Jerry DeLisle committed
4953
	  e = gfc_constructor_lookup_expr (order_exp->value.constructor, i);
4954
	  gcc_assert (e);
4955

4956
	  gfc_extract_int (e, &order[i]);
Steven G. Kargl committed
4957

4958 4959 4960
	  gcc_assert (order[i] >= 1 && order[i] <= rank);
	  order[i]--;
	  gcc_assert (x[order[i]] == 0);
4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986
	  x[order[i]] = 1;
	}
    }

  /* Count the elements in the source and padding arrays.  */

  npad = 0;
  if (pad != NULL)
    {
      gfc_array_size (pad, &size);
      npad = mpz_get_ui (size);
      mpz_clear (size);
    }

  gfc_array_size (source, &size);
  nsource = mpz_get_ui (size);
  mpz_clear (size);

  /* If it weren't for that pesky permutation we could just loop
     through the source and round out any shortage with pad elements.
     But no, someone just had to have the compiler do something the
     user should be doing.  */

  for (i = 0; i < rank; i++)
    x[i] = 0;

Jerry DeLisle committed
4987 4988
  result = gfc_get_array_expr (source->ts.type, source->ts.kind,
			       &source->where);
4989 4990
  if (source->ts.type == BT_DERIVED)
    result->ts.u.derived = source->ts.u.derived;
Jerry DeLisle committed
4991 4992 4993 4994 4995
  result->rank = rank;
  result->shape = gfc_get_shape (rank);
  for (i = 0; i < rank; i++)
    mpz_init_set_ui (result->shape[i], shape[i]);

4996
  while (nsource > 0 || npad > 0)
4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008
    {
      /* Figure out which element to extract.  */
      mpz_set_ui (index, 0);

      for (i = rank - 1; i >= 0; i--)
	{
	  mpz_add_ui (index, index, x[order[i]]);
	  if (i != 0)
	    mpz_mul_ui (index, index, shape[order[i - 1]]);
	}

      if (mpz_cmp_ui (index, INT_MAX) > 0)
Steven G. Kargl committed
5009
	gfc_internal_error ("Reshaped array too large at %C");
5010 5011 5012 5013

      j = mpz_get_ui (index);

      if (j < nsource)
Jerry DeLisle committed
5014
	e = gfc_constructor_lookup_expr (source->value.constructor, j);
5015 5016
      else
	{
5017
	  gcc_assert (npad > 0);
5018

5019
	  j = j - nsource;
5020
	  j = j % npad;
Jerry DeLisle committed
5021
	  e = gfc_constructor_lookup_expr (pad->value.constructor, j);
5022
	}
5023
      gcc_assert (e);
5024

Jerry DeLisle committed
5025 5026
      gfc_constructor_append_expr (&result->value.constructor,
				   gfc_copy_expr (e), &e->where);
5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042

      /* Calculate the next element.  */
      i = 0;

inc:
      if (++x[i] < shape[i])
	continue;
      x[i++] = 0;
      if (i < rank)
	goto inc;

      break;
    }

  mpz_clear (index);

Jerry DeLisle committed
5043
  return result;
5044 5045 5046
}


5047
gfc_expr *
5048
gfc_simplify_rrspacing (gfc_expr *x)
5049 5050 5051 5052 5053 5054 5055
{
  gfc_expr *result;
  int i;
  long int e, p;

  if (x->expr_type != EXPR_CONSTANT)
    return NULL;
5056

5057 5058
  i = gfc_validate_kind (x->ts.type, x->ts.kind, false);

Jerry DeLisle committed
5059
  result = gfc_get_constant_expr (BT_REAL, x->ts.kind, &x->where);
5060 5061
  mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE);

5062
  /* Special case x = -0 and 0.  */
5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077
  if (mpfr_sgn (result->value.real) == 0)
    {
      mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
      return result;
    }

  /* | x * 2**(-e) | * 2**p.  */
  e = - (long int) mpfr_get_exp (x->value.real);
  mpfr_mul_2si (result->value.real, result->value.real, e, GFC_RND_MODE);

  p = (long int) gfc_real_kinds[i].digits;
  mpfr_mul_2si (result->value.real, result->value.real, p, GFC_RND_MODE);

  return range_check (result, "RRSPACING");
}
5078

5079 5080

gfc_expr *
5081
gfc_simplify_scale (gfc_expr *x, gfc_expr *i)
5082 5083
{
  int k, neg_flag, power, exp_range;
5084
  mpfr_t scale, radix;
5085 5086 5087 5088 5089
  gfc_expr *result;

  if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
5090
  result = gfc_get_constant_expr (BT_REAL, x->ts.kind, &x->where);
5091

5092
  if (mpfr_sgn (x->value.real) == 0)
5093
    {
5094
      mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
5095 5096 5097
      return result;
    }

5098
  k = gfc_validate_kind (BT_REAL, x->ts.kind, false);
5099 5100 5101 5102 5103 5104 5105 5106

  exp_range = gfc_real_kinds[k].max_exponent - gfc_real_kinds[k].min_exponent;

  /* This check filters out values of i that would overflow an int.  */
  if (mpz_cmp_si (i->value.integer, exp_range + 2) > 0
      || mpz_cmp_si (i->value.integer, -exp_range - 2) < 0)
    {
      gfc_error ("Result of SCALE overflows its kind at %L", &result->where);
Steven G. Kargl committed
5107
      gfc_free_expr (result);
5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121
      return &gfc_bad_expr;
    }

  /* Compute scale = radix ** power.  */
  power = mpz_get_si (i->value.integer);

  if (power >= 0)
    neg_flag = 0;
  else
    {
      neg_flag = 1;
      power = -power;
    }

5122 5123 5124 5125 5126
  gfc_set_model_kind (x->ts.kind);
  mpfr_init (scale);
  mpfr_init (radix);
  mpfr_set_ui (radix, gfc_real_kinds[k].radix, GFC_RND_MODE);
  mpfr_pow_ui (scale, radix, power, GFC_RND_MODE);
5127 5128

  if (neg_flag)
5129
    mpfr_div (result->value.real, x->value.real, scale, GFC_RND_MODE);
5130
  else
5131
    mpfr_mul (result->value.real, x->value.real, scale, GFC_RND_MODE);
5132

5133
  mpfr_clears (scale, radix, NULL);
5134 5135 5136 5137 5138

  return range_check (result, "SCALE");
}


5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183
/* Variants of strspn and strcspn that operate on wide characters.  */

static size_t
wide_strspn (const gfc_char_t *s1, const gfc_char_t *s2)
{
  size_t i = 0;
  const gfc_char_t *c;

  while (s1[i])
    {
      for (c = s2; *c; c++)
	{
	  if (s1[i] == *c)
	    break;
	}
      if (*c == '\0')
	break;
      i++;
    }

  return i;
}

static size_t
wide_strcspn (const gfc_char_t *s1, const gfc_char_t *s2)
{
  size_t i = 0;
  const gfc_char_t *c;

  while (s1[i])
    {
      for (c = s2; *c; c++)
	{
	  if (s1[i] == *c)
	    break;
	}
      if (*c)
	break;
      i++;
    }

  return i;
}


5184
gfc_expr *
5185
gfc_simplify_scan (gfc_expr *e, gfc_expr *c, gfc_expr *b, gfc_expr *kind)
5186 5187 5188 5189 5190
{
  gfc_expr *result;
  int back;
  size_t i;
  size_t indx, len, lenc;
5191 5192 5193 5194
  int k = get_kind (BT_INTEGER, kind, "SCAN", gfc_default_integer_kind);

  if (k == -1)
    return &gfc_bad_expr;
5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213

  if (e->expr_type != EXPR_CONSTANT || c->expr_type != EXPR_CONSTANT)
    return NULL;

  if (b != NULL && b->value.logical != 0)
    back = 1;
  else
    back = 0;

  len = e->value.character.length;
  lenc = c->value.character.length;

  if (len == 0 || lenc == 0)
    {
      indx = 0;
    }
  else
    {
      if (back == 0)
5214
	{
5215 5216
	  indx = wide_strcspn (e->value.character.string,
			       c->value.character.string) + 1;
5217 5218 5219
	  if (indx > len)
	    indx = 0;
	}
5220
      else
5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234
	{
	  i = 0;
	  for (indx = len; indx > 0; indx--)
	    {
	      for (i = 0; i < lenc; i++)
		{
		  if (c->value.character.string[i]
		      == e->value.character.string[indx - 1])
		    break;
		}
	      if (i < lenc)
		break;
	    }
	}
5235
    }
Jerry DeLisle committed
5236 5237

  result = gfc_get_int_expr (k, &e->where, indx);
5238 5239 5240 5241 5242
  return range_check (result, "SCAN");
}


gfc_expr *
5243 5244 5245 5246 5247 5248 5249 5250 5251 5252
gfc_simplify_selected_char_kind (gfc_expr *e)
{
  int kind;

  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

  if (gfc_compare_with_Cstring (e, "ascii", false) == 0
      || gfc_compare_with_Cstring (e, "default", false) == 0)
    kind = 1;
5253 5254
  else if (gfc_compare_with_Cstring (e, "iso_10646", false) == 0)
    kind = 4;
5255 5256 5257
  else
    kind = -1;

Jerry DeLisle committed
5258
  return gfc_get_int_expr (gfc_default_integer_kind, &e->where, kind);
5259 5260 5261 5262
}


gfc_expr *
5263
gfc_simplify_selected_int_kind (gfc_expr *e)
5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279
{
  int i, kind, range;

  if (e->expr_type != EXPR_CONSTANT || gfc_extract_int (e, &range) != NULL)
    return NULL;

  kind = INT_MAX;

  for (i = 0; gfc_integer_kinds[i].kind != 0; i++)
    if (gfc_integer_kinds[i].range >= range
	&& gfc_integer_kinds[i].kind < kind)
      kind = gfc_integer_kinds[i].kind;

  if (kind == INT_MAX)
    kind = -1;

Jerry DeLisle committed
5280
  return gfc_get_int_expr (gfc_default_integer_kind, &e->where, kind);
5281 5282 5283 5284
}


gfc_expr *
5285
gfc_simplify_selected_real_kind (gfc_expr *p, gfc_expr *q, gfc_expr *rdx)
5286
{
5287 5288 5289
  int range, precision, radix, i, kind, found_precision, found_range,
      found_radix;
  locus *loc = &gfc_current_locus;
5290 5291 5292 5293 5294 5295 5296 5297

  if (p == NULL)
    precision = 0;
  else
    {
      if (p->expr_type != EXPR_CONSTANT
	  || gfc_extract_int (p, &precision) != NULL)
	return NULL;
5298
      loc = &p->where;
5299 5300 5301 5302 5303 5304 5305 5306 5307
    }

  if (q == NULL)
    range = 0;
  else
    {
      if (q->expr_type != EXPR_CONSTANT
	  || gfc_extract_int (q, &range) != NULL)
	return NULL;
5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322

      if (!loc)
	loc = &q->where;
    }

  if (rdx == NULL)
    radix = 0;
  else
    {
      if (rdx->expr_type != EXPR_CONSTANT
	  || gfc_extract_int (rdx, &radix) != NULL)
	return NULL;

      if (!loc)
	loc = &rdx->where;
5323 5324 5325 5326 5327
    }

  kind = INT_MAX;
  found_precision = 0;
  found_range = 0;
5328
  found_radix = 0;
5329 5330 5331 5332 5333 5334 5335 5336 5337

  for (i = 0; gfc_real_kinds[i].kind != 0; i++)
    {
      if (gfc_real_kinds[i].precision >= precision)
	found_precision = 1;

      if (gfc_real_kinds[i].range >= range)
	found_range = 1;

5338 5339 5340
      if (gfc_real_kinds[i].radix >= radix)
	found_radix = 1;

5341
      if (gfc_real_kinds[i].precision >= precision
5342 5343
	  && gfc_real_kinds[i].range >= range
	  && gfc_real_kinds[i].radix >= radix && gfc_real_kinds[i].kind < kind)
5344 5345 5346 5347 5348
	kind = gfc_real_kinds[i].kind;
    }

  if (kind == INT_MAX)
    {
5349
      if (found_radix && found_range && !found_precision)
5350
	kind = -1;
5351 5352 5353 5354 5355 5356 5357 5358
      else if (found_radix && found_precision && !found_range)
	kind = -2;
      else if (found_radix && !found_precision && !found_range)
	kind = -3;
      else if (found_radix)
	kind = -4;
      else
	kind = -5;
5359 5360
    }

5361
  return gfc_get_int_expr (gfc_default_integer_kind, loc, kind);
5362 5363 5364 5365
}


gfc_expr *
5366
gfc_simplify_set_exponent (gfc_expr *x, gfc_expr *i)
5367 5368
{
  gfc_expr *result;
5369
  mpfr_t exp, absv, log2, pow2, frac;
5370 5371 5372 5373 5374
  unsigned long exp2;

  if (x->expr_type != EXPR_CONSTANT || i->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
5375
  result = gfc_get_constant_expr (BT_REAL, x->ts.kind, &x->where);
5376

5377
  if (mpfr_sgn (x->value.real) == 0)
5378
    {
5379
      mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
5380 5381 5382
      return result;
    }

5383
  gfc_set_model_kind (x->ts.kind);
5384
  mpfr_init (absv);
5385 5386
  mpfr_init (log2);
  mpfr_init (exp);
5387 5388
  mpfr_init (pow2);
  mpfr_init (frac);
5389

5390
  mpfr_abs (absv, x->value.real, GFC_RND_MODE);
5391
  mpfr_log2 (log2, absv, GFC_RND_MODE);
5392

5393 5394
  mpfr_trunc (log2, log2);
  mpfr_add_ui (exp, log2, 1, GFC_RND_MODE);
5395 5396

  /* Old exponent value, and fraction.  */
5397
  mpfr_ui_pow (pow2, 2, exp, GFC_RND_MODE);
5398

5399
  mpfr_div (frac, absv, pow2, GFC_RND_MODE);
5400 5401 5402

  /* New exponent.  */
  exp2 = (unsigned long) mpz_get_d (i->value.integer);
5403
  mpfr_mul_2exp (result->value.real, frac, exp2, GFC_RND_MODE);
5404

5405
  mpfr_clears (absv, log2, pow2, frac, NULL);
5406 5407 5408 5409 5410 5411

  return range_check (result, "SET_EXPONENT");
}


gfc_expr *
5412
gfc_simplify_shape (gfc_expr *source)
5413 5414 5415 5416 5417
{
  mpz_t shape[GFC_MAX_DIMENSIONS];
  gfc_expr *result, *e, *f;
  gfc_array_ref *ar;
  int n;
5418
  gfc_try t;
5419

Tobias Burnus committed
5420
  if (source->rank == 0)
Jerry DeLisle committed
5421 5422
    return gfc_get_array_expr (BT_INTEGER, gfc_default_integer_kind,
			       &source->where);
Tobias Burnus committed
5423

Jerry DeLisle committed
5424 5425
  result = gfc_get_array_expr (BT_INTEGER, gfc_default_integer_kind,
			       &source->where);
5426

5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442
  if (source->expr_type == EXPR_VARIABLE)
    {
      ar = gfc_find_array_ref (source);
      t = gfc_array_ref_shape (ar, shape);
    }
  else if (source->shape)
    {
      t = SUCCESS;
      for (n = 0; n < source->rank; n++)
	{
	  mpz_init (shape[n]);
	  mpz_set (shape[n], source->shape[n]);
	}
    }
  else
    t = FAILURE;
5443 5444 5445

  for (n = 0; n < source->rank; n++)
    {
Jerry DeLisle committed
5446 5447
      e = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
				 &source->where);
5448 5449 5450 5451 5452 5453 5454 5455 5456 5457

      if (t == SUCCESS)
	{
	  mpz_set (e->value.integer, shape[n]);
	  mpz_clear (shape[n]);
	}
      else
	{
	  mpz_set_ui (e->value.integer, n + 1);

5458
	  f = gfc_simplify_size (source, e, NULL);
5459 5460 5461 5462 5463 5464 5465
	  gfc_free_expr (e);
	  if (f == NULL)
	    {
	      gfc_free_expr (result);
	      return NULL;
	    }
	  else
5466
	    e = f;
5467 5468
	}

Jerry DeLisle committed
5469
      gfc_constructor_append_expr (&result->value.constructor, e, NULL);
5470 5471 5472 5473 5474 5475 5476
    }

  return result;
}


gfc_expr *
5477
gfc_simplify_size (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
5478 5479
{
  mpz_t size;
5480
  gfc_expr *return_value;
5481
  int d;
5482
  int k = get_kind (BT_INTEGER, kind, "SIZE", gfc_default_integer_kind);
5483 5484 5485

  if (k == -1)
    return &gfc_bad_expr;
5486

5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536
  /* For unary operations, the size of the result is given by the size
     of the operand.  For binary ones, it's the size of the first operand
     unless it is scalar, then it is the size of the second.  */
  if (array->expr_type == EXPR_OP && !array->value.op.uop)
    {
      gfc_expr* replacement;
      gfc_expr* simplified;

      switch (array->value.op.op)
	{
	  /* Unary operations.  */
	  case INTRINSIC_NOT:
	  case INTRINSIC_UPLUS:
	  case INTRINSIC_UMINUS:
	    replacement = array->value.op.op1;
	    break;

	  /* Binary operations.  If any one of the operands is scalar, take
	     the other one's size.  If both of them are arrays, it does not
	     matter -- try to find one with known shape, if possible.  */
	  default:
	    if (array->value.op.op1->rank == 0)
	      replacement = array->value.op.op2;
	    else if (array->value.op.op2->rank == 0)
	      replacement = array->value.op.op1;
	    else
	      {
		simplified = gfc_simplify_size (array->value.op.op1, dim, kind);
		if (simplified)
		  return simplified;

		replacement = array->value.op.op2;
	      }
	    break;
	}

      /* Try to reduce it directly if possible.  */
      simplified = gfc_simplify_size (replacement, dim, kind);

      /* Otherwise, we build a new SIZE call.  This is hopefully at least
	 simpler than the original one.  */
      if (!simplified)
	simplified = gfc_build_intrinsic_call ("size", array->where, 3,
					       gfc_copy_expr (replacement),
					       gfc_copy_expr (dim),
					       gfc_copy_expr (kind));

      return simplified;
    }

5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551
  if (dim == NULL)
    {
      if (gfc_array_size (array, &size) == FAILURE)
	return NULL;
    }
  else
    {
      if (dim->expr_type != EXPR_CONSTANT)
	return NULL;

      d = mpz_get_ui (dim->value.integer) - 1;
      if (gfc_array_dimen_size (array, d, &size) == FAILURE)
	return NULL;
    }

5552 5553 5554
  return_value = gfc_get_int_expr (k, &array->where, mpz_get_si (size));
  mpz_clear (size);
  return return_value;
5555 5556 5557 5558
}


gfc_expr *
5559
gfc_simplify_sign (gfc_expr *x, gfc_expr *y)
5560 5561 5562 5563 5564 5565
{
  gfc_expr *result;

  if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
5566
  result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
5567 5568 5569

  switch (x->ts.type)
    {
Jerry DeLisle committed
5570 5571 5572 5573 5574
      case BT_INTEGER:
	mpz_abs (result->value.integer, x->value.integer);
	if (mpz_sgn (y->value.integer) < 0)
	  mpz_neg (result->value.integer, result->value.integer);
	break;
5575

Jerry DeLisle committed
5576 5577 5578 5579 5580 5581 5582 5583
      case BT_REAL:
	if (gfc_option.flag_sign_zero)
	  mpfr_copysign (result->value.real, x->value.real, y->value.real,
			GFC_RND_MODE);
	else
	  mpfr_setsign (result->value.real, x->value.real,
			mpfr_sgn (y->value.real) < 0 ? 1 : 0, GFC_RND_MODE);
	break;
5584

Jerry DeLisle committed
5585 5586
      default:
	gfc_internal_error ("Bad type in gfc_simplify_sign");
5587 5588 5589 5590 5591 5592 5593
    }

  return result;
}


gfc_expr *
5594
gfc_simplify_sin (gfc_expr *x)
5595 5596 5597 5598 5599 5600
{
  gfc_expr *result;

  if (x->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
5601
  result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
5602 5603 5604

  switch (x->ts.type)
    {
Jerry DeLisle committed
5605 5606 5607
      case BT_REAL:
	mpfr_sin (result->value.real, x->value.real, GFC_RND_MODE);
	break;
5608

Jerry DeLisle committed
5609 5610 5611 5612
      case BT_COMPLEX:
	gfc_set_model (x->value.real);
	mpc_sin (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
	break;
5613

Jerry DeLisle committed
5614 5615
      default:
	gfc_internal_error ("in gfc_simplify_sin(): Bad type");
5616 5617 5618 5619 5620 5621 5622
    }

  return range_check (result, "SIN");
}


gfc_expr *
5623
gfc_simplify_sinh (gfc_expr *x)
5624 5625 5626 5627 5628 5629
{
  gfc_expr *result;

  if (x->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
5630
  result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
5631

Jerry DeLisle committed
5632 5633 5634 5635 5636 5637 5638 5639 5640
  switch (x->ts.type)
    {
      case BT_REAL:
	mpfr_sinh (result->value.real, x->value.real, GFC_RND_MODE);
	break;

      case BT_COMPLEX:
	mpc_sinh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
	break;
5641

Jerry DeLisle committed
5642 5643 5644
      default:
	gcc_unreachable ();
    }
5645 5646 5647 5648 5649 5650 5651 5652 5653

  return range_check (result, "SINH");
}


/* The argument is always a double precision real that is converted to
   single precision.  TODO: Rounding!  */

gfc_expr *
5654
gfc_simplify_sngl (gfc_expr *a)
5655 5656 5657 5658 5659 5660
{
  gfc_expr *result;

  if (a->expr_type != EXPR_CONSTANT)
    return NULL;

5661
  result = gfc_real2real (a, gfc_default_real_kind);
5662 5663 5664 5665
  return range_check (result, "SNGL");
}


5666
gfc_expr *
5667
gfc_simplify_spacing (gfc_expr *x)
5668 5669 5670 5671
{
  gfc_expr *result;
  int i;
  long int en, ep;
5672

5673 5674 5675 5676 5677
  if (x->expr_type != EXPR_CONSTANT)
    return NULL;

  i = gfc_validate_kind (x->ts.type, x->ts.kind, false);

Jerry DeLisle committed
5678
  result = gfc_get_constant_expr (BT_REAL, x->ts.kind, &x->where);
5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701

  /* Special case x = 0 and -0.  */
  mpfr_abs (result->value.real, x->value.real, GFC_RND_MODE);
  if (mpfr_sgn (result->value.real) == 0)
    {
      mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
      return result;
    }

  /* In the Fortran 95 standard, the result is b**(e - p) where b, e, and p
     are the radix, exponent of x, and precision.  This excludes the 
     possibility of subnormal numbers.  Fortran 2003 states the result is
     b**max(e - p, emin - 1).  */

  ep = (long int) mpfr_get_exp (x->value.real) - gfc_real_kinds[i].digits;
  en = (long int) gfc_real_kinds[i].min_exponent - 1;
  en = en > ep ? en : ep;

  mpfr_set_ui (result->value.real, 1, GFC_RND_MODE);
  mpfr_mul_2si (result->value.real, result->value.real, en, GFC_RND_MODE);

  return range_check (result, "SPACING");
}
5702

5703 5704

gfc_expr *
5705 5706 5707 5708
gfc_simplify_spread (gfc_expr *source, gfc_expr *dim_expr, gfc_expr *ncopies_expr)
{
  gfc_expr *result = 0L;
  int i, j, dim, ncopies;
5709
  mpz_t size;
5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724

  if ((!gfc_is_constant_expr (source)
       && !is_constant_array_expr (source))
      || !gfc_is_constant_expr (dim_expr)
      || !gfc_is_constant_expr (ncopies_expr))
    return NULL;

  gcc_assert (dim_expr->ts.type == BT_INTEGER);
  gfc_extract_int (dim_expr, &dim);
  dim -= 1;   /* zero-base DIM */

  gcc_assert (ncopies_expr->ts.type == BT_INTEGER);
  gfc_extract_int (ncopies_expr, &ncopies);
  ncopies = MAX (ncopies, 0);

5725 5726
  /* Do not allow the array size to exceed the limit for an array
     constructor.  */
5727 5728 5729 5730 5731 5732 5733 5734
  if (source->expr_type == EXPR_ARRAY)
    {
      if (gfc_array_size (source, &size) == FAILURE)
	gfc_internal_error ("Failure getting length of a constant array.");
    }
  else
    mpz_init_set_ui (size, 1);

5735 5736 5737
  if (mpz_get_si (size)*ncopies > gfc_option.flag_max_array_constructor)
    return NULL;

5738 5739 5740 5741
  if (source->expr_type == EXPR_CONSTANT)
    {
      gcc_assert (dim == 0);

Jerry DeLisle committed
5742 5743
      result = gfc_get_array_expr (source->ts.type, source->ts.kind,
				   &source->where);
5744 5745
      if (source->ts.type == BT_DERIVED)
	result->ts.u.derived = source->ts.u.derived;
5746 5747 5748 5749 5750
      result->rank = 1;
      result->shape = gfc_get_shape (result->rank);
      mpz_init_set_si (result->shape[0], ncopies);

      for (i = 0; i < ncopies; ++i)
Jerry DeLisle committed
5751 5752
        gfc_constructor_append_expr (&result->value.constructor,
				     gfc_copy_expr (source), NULL);
5753 5754 5755
    }
  else if (source->expr_type == EXPR_ARRAY)
    {
Jerry DeLisle committed
5756 5757
      int offset, rstride[GFC_MAX_DIMENSIONS], extent[GFC_MAX_DIMENSIONS];
      gfc_constructor *source_ctor;
5758 5759 5760 5761

      gcc_assert (source->rank < GFC_MAX_DIMENSIONS);
      gcc_assert (dim >= 0 && dim <= source->rank);

Jerry DeLisle committed
5762 5763
      result = gfc_get_array_expr (source->ts.type, source->ts.kind,
				   &source->where);
5764 5765
      if (source->ts.type == BT_DERIVED)
	result->ts.u.derived = source->ts.u.derived;
5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779
      result->rank = source->rank + 1;
      result->shape = gfc_get_shape (result->rank);

      for (i = 0, j = 0; i < result->rank; ++i)
	{
	  if (i != dim)
	    mpz_init_set (result->shape[i], source->shape[j++]);
	  else
	    mpz_init_set_si (result->shape[i], ncopies);

	  extent[i] = mpz_get_si (result->shape[i]);
	  rstride[i] = (i == 0) ? 1 : rstride[i-1] * extent[i-1];
	}

Jerry DeLisle committed
5780 5781 5782
      offset = 0;
      for (source_ctor = gfc_constructor_first (source->value.constructor);
           source_ctor; source_ctor = gfc_constructor_next (source_ctor))
5783 5784
	{
	  for (i = 0; i < ncopies; ++i)
Jerry DeLisle committed
5785 5786 5787
	    gfc_constructor_insert_expr (&result->value.constructor,
					 gfc_copy_expr (source_ctor->expr),
					 NULL, offset + i * rstride[dim]);
5788

Jerry DeLisle committed
5789
	  offset += (dim == 0 ? ncopies : 1);
5790 5791 5792 5793 5794 5795 5796 5797 5798
	}
    }
  else
    /* FIXME: Returning here avoids a regression in array_simplify_1.f90.
       Replace NULL with gcc_unreachable() after implementing
       gfc_simplify_cshift(). */
    return NULL;

  if (source->ts.type == BT_CHARACTER)
5799
    result->ts.u.cl = source->ts.u.cl;
5800 5801 5802 5803 5804 5805

  return result;
}


gfc_expr *
5806
gfc_simplify_sqrt (gfc_expr *e)
5807
{
Jerry DeLisle committed
5808
  gfc_expr *result = NULL;
5809 5810 5811 5812 5813 5814

  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

  switch (e->ts.type)
    {
Jerry DeLisle committed
5815 5816 5817 5818 5819 5820 5821 5822 5823 5824
      case BT_REAL:
	if (mpfr_cmp_si (e->value.real, 0) < 0)
	  {
	    gfc_error ("Argument of SQRT at %L has a negative value",
		       &e->where);
	    return &gfc_bad_expr;
	  }
	result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
	mpfr_sqrt (result->value.real, e->value.real, GFC_RND_MODE);
	break;
5825

Jerry DeLisle committed
5826 5827
      case BT_COMPLEX:
	gfc_set_model (e->value.real);
5828

Jerry DeLisle committed
5829 5830 5831
	result = gfc_get_constant_expr (e->ts.type, e->ts.kind, &e->where);
	mpc_sqrt (result->value.complex, e->value.complex, GFC_MPC_RND_MODE);
	break;
5832

Jerry DeLisle committed
5833 5834
      default:
	gfc_internal_error ("invalid argument of SQRT at %L", &e->where);
5835 5836 5837 5838 5839 5840 5841
    }

  return range_check (result, "SQRT");
}


gfc_expr *
5842 5843
gfc_simplify_sum (gfc_expr *array, gfc_expr *dim, gfc_expr *mask)
{
5844
  return simplify_transformation (array, dim, mask, 0, gfc_add);
5845 5846 5847 5848
}


gfc_expr *
5849
gfc_simplify_tan (gfc_expr *x)
5850
{
5851
  gfc_expr *result;
5852 5853 5854 5855

  if (x->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
5856
  result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
5857

Jerry DeLisle committed
5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870
  switch (x->ts.type)
    {
      case BT_REAL:
	mpfr_tan (result->value.real, x->value.real, GFC_RND_MODE);
	break;

      case BT_COMPLEX:
	mpc_tan (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
	break;

      default:
	gcc_unreachable ();
    }
5871 5872 5873 5874 5875 5876

  return range_check (result, "TAN");
}


gfc_expr *
5877
gfc_simplify_tanh (gfc_expr *x)
5878 5879 5880 5881 5882 5883
{
  gfc_expr *result;

  if (x->expr_type != EXPR_CONSTANT)
    return NULL;

Jerry DeLisle committed
5884
  result = gfc_get_constant_expr (x->ts.type, x->ts.kind, &x->where);
5885

Jerry DeLisle committed
5886 5887 5888 5889 5890
  switch (x->ts.type)
    {
      case BT_REAL:
	mpfr_tanh (result->value.real, x->value.real, GFC_RND_MODE);
	break;
5891

Jerry DeLisle committed
5892 5893 5894 5895 5896 5897 5898
      case BT_COMPLEX:
	mpc_tanh (result->value.complex, x->value.complex, GFC_MPC_RND_MODE);
	break;

      default:
	gcc_unreachable ();
    }
5899

Jerry DeLisle committed
5900
  return range_check (result, "TANH");
5901 5902 5903 5904
}


gfc_expr *
5905
gfc_simplify_tiny (gfc_expr *e)
5906 5907 5908 5909
{
  gfc_expr *result;
  int i;

5910
  i = gfc_validate_kind (BT_REAL, e->ts.kind, false);
5911

Jerry DeLisle committed
5912
  result = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
5913
  mpfr_set (result->value.real, gfc_real_kinds[i].tiny, GFC_RND_MODE);
5914 5915 5916 5917 5918 5919

  return result;
}


gfc_expr *
5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931
gfc_simplify_trailz (gfc_expr *e)
{
  unsigned long tz, bs;
  int i;

  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

  i = gfc_validate_kind (e->ts.type, e->ts.kind, false);
  bs = gfc_integer_kinds[i].bit_size;
  tz = mpz_scan1 (e->value.integer, 0);

Jerry DeLisle committed
5932 5933
  return gfc_get_int_expr (gfc_default_integer_kind,
			   &e->where, MIN (tz, bs));
5934 5935 5936 5937
}


gfc_expr *
5938
gfc_simplify_transfer (gfc_expr *source, gfc_expr *mold, gfc_expr *size)
5939
{
Paul Thomas committed
5940 5941 5942 5943 5944 5945 5946 5947
  gfc_expr *result;
  gfc_expr *mold_element;
  size_t source_size;
  size_t result_size;
  size_t result_elt_size;
  size_t buffer_size;
  mpz_t tmp;
  unsigned char *buffer;
5948

Paul Thomas committed
5949
  if (!gfc_is_constant_expr (source)
5950
	|| (gfc_init_expr_flag && !gfc_is_constant_expr (mold))
Paul Thomas committed
5951 5952 5953
	|| !gfc_is_constant_expr (size))
    return NULL;

5954 5955 5956
  if (source->expr_type == EXPR_FUNCTION)
    return NULL;

Paul Thomas committed
5957 5958 5959 5960 5961 5962 5963 5964
  /* Calculate the size of the source.  */
  if (source->expr_type == EXPR_ARRAY
      && gfc_array_size (source, &tmp) == FAILURE)
    gfc_internal_error ("Failure getting length of a constant array.");

  source_size = gfc_target_expr_size (source);

  /* Create an empty new expression with the appropriate characteristics.  */
Jerry DeLisle committed
5965 5966
  result = gfc_get_constant_expr (mold->ts.type, mold->ts.kind,
				  &source->where);
Paul Thomas committed
5967 5968 5969
  result->ts = mold->ts;

  mold_element = mold->expr_type == EXPR_ARRAY
Jerry DeLisle committed
5970
		 ? gfc_constructor_first (mold->value.constructor)->expr
Paul Thomas committed
5971 5972 5973 5974 5975
		 : mold;

  /* Set result character length, if needed.  Note that this needs to be
     set even for array expressions, in order to pass this information into 
     gfc_target_interpret_expr.  */
5976
  if (result->ts.type == BT_CHARACTER && gfc_is_constant_expr (mold_element))
Paul Thomas committed
5977 5978 5979 5980
    result->value.character.length = mold_element->value.character.length;
  
  /* Set the number of elements in the result, and determine its size.  */
  result_elt_size = gfc_target_expr_size (mold_element);
5981 5982 5983 5984 5985 5986
  if (result_elt_size == 0)
    {
      gfc_free_expr (result);
      return NULL;
    }

5987
  if (mold->expr_type == EXPR_ARRAY || mold->rank || size)
Paul Thomas committed
5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013
    {
      int result_length;

      result->expr_type = EXPR_ARRAY;
      result->rank = 1;

      if (size)
	result_length = (size_t)mpz_get_ui (size->value.integer);
      else
	{
	  result_length = source_size / result_elt_size;
	  if (result_length * result_elt_size < source_size)
	    result_length += 1;
	}

      result->shape = gfc_get_shape (1);
      mpz_init_set_ui (result->shape[0], result_length);

      result_size = result_length * result_elt_size;
    }
  else
    {
      result->rank = 0;
      result_size = result_elt_size;
    }

6014
  if (gfc_option.warn_surprising && source_size < result_size)
6015 6016 6017 6018
    gfc_warning("Intrinsic TRANSFER at %L has partly undefined result: "
		"source size %ld < result size %ld", &source->where,
		(long) source_size, (long) result_size);

Paul Thomas committed
6019 6020 6021
  /* Allocate the buffer to store the binary version of the source.  */
  buffer_size = MAX (source_size, result_size);
  buffer = (unsigned char*)alloca (buffer_size);
6022
  memset (buffer, 0, buffer_size);
Paul Thomas committed
6023 6024 6025 6026 6027 6028 6029 6030

  /* Now write source to the buffer.  */
  gfc_target_encode_expr (source, buffer, buffer_size);

  /* And read the buffer back into the new expression.  */
  gfc_target_interpret_expr (buffer, buffer_size, result);

  return result;
6031 6032 6033 6034
}


gfc_expr *
6035 6036
gfc_simplify_transpose (gfc_expr *matrix)
{
Jerry DeLisle committed
6037
  int row, matrix_rows, col, matrix_cols;
6038 6039 6040 6041 6042 6043 6044
  gfc_expr *result;

  if (!is_constant_array_expr (matrix))
    return NULL;

  gcc_assert (matrix->rank == 2);

Jerry DeLisle committed
6045 6046
  result = gfc_get_array_expr (matrix->ts.type, matrix->ts.kind,
			       &matrix->where);
6047 6048 6049 6050 6051 6052
  result->rank = 2;
  result->shape = gfc_get_shape (result->rank);
  mpz_set (result->shape[0], matrix->shape[1]);
  mpz_set (result->shape[1], matrix->shape[0]);

  if (matrix->ts.type == BT_CHARACTER)
6053
    result->ts.u.cl = matrix->ts.u.cl;
6054 6055
  else if (matrix->ts.type == BT_DERIVED)
    result->ts.u.derived = matrix->ts.u.derived;
6056 6057

  matrix_rows = mpz_get_si (matrix->shape[0]);
Jerry DeLisle committed
6058 6059 6060 6061 6062 6063 6064 6065 6066 6067
  matrix_cols = mpz_get_si (matrix->shape[1]);
  for (row = 0; row < matrix_rows; ++row)
    for (col = 0; col < matrix_cols; ++col)
      {
	gfc_expr *e = gfc_constructor_lookup_expr (matrix->value.constructor,
						   col * matrix_rows + row);
	gfc_constructor_insert_expr (&result->value.constructor, 
				     gfc_copy_expr (e), &matrix->where,
				     row * matrix_cols + col);
      }
6068 6069 6070 6071 6072 6073

  return result;
}


gfc_expr *
6074
gfc_simplify_trim (gfc_expr *e)
6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092
{
  gfc_expr *result;
  int count, i, len, lentrim;

  if (e->expr_type != EXPR_CONSTANT)
    return NULL;

  len = e->value.character.length;
  for (count = 0, i = 1; i <= len; ++i)
    {
      if (e->value.character.string[len - i] == ' ')
	count++;
      else
	break;
    }

  lentrim = len - count;

Jerry DeLisle committed
6093
  result = gfc_get_character_expr (e->ts.kind, &e->where, NULL, lentrim);
6094 6095 6096 6097 6098 6099 6100 6101
  for (i = 0; i < lentrim; i++)
    result->value.character.string[i] = e->value.character.string[i];

  return result;
}


gfc_expr *
6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323
gfc_simplify_image_index (gfc_expr *coarray, gfc_expr *sub)
{
  gfc_expr *result;
  gfc_ref *ref;
  gfc_array_spec *as;
  gfc_constructor *sub_cons;
  bool first_image;
  int d;

  if (!is_constant_array_expr (sub))
    goto not_implemented; /* return NULL;*/

  /* Follow any component references.  */
  as = coarray->symtree->n.sym->as;
  for (ref = coarray->ref; ref; ref = ref->next)
    if (ref->type == REF_COMPONENT)
      as = ref->u.ar.as;

  if (as->type == AS_DEFERRED)
    goto not_implemented; /* return NULL;*/

  /* "valid sequence of cosubscripts" are required; thus, return 0 unless
     the cosubscript addresses the first image.  */

  sub_cons = gfc_constructor_first (sub->value.constructor);
  first_image = true;

  for (d = 1; d <= as->corank; d++)
    {
      gfc_expr *ca_bound;
      int cmp;

      if (sub_cons == NULL)
	{
	  gfc_error ("Too few elements in expression for SUB= argument at %L",
		     &sub->where);
	  return &gfc_bad_expr;
	}

      ca_bound = simplify_bound_dim (coarray, NULL, d + as->rank, 0, as,
				     NULL, true);
      if (ca_bound == NULL)
	goto not_implemented; /* return NULL */

      if (ca_bound == &gfc_bad_expr)
	return ca_bound;

      cmp = mpz_cmp (ca_bound->value.integer, sub_cons->expr->value.integer);

      if (cmp == 0)
	{
          gfc_free_expr (ca_bound);
	  sub_cons = gfc_constructor_next (sub_cons);
	  continue;
	}

      first_image = false;

      if (cmp > 0)
	{
	  gfc_error ("Out of bounds in IMAGE_INDEX at %L for dimension %d, "
		     "SUB has %ld and COARRAY lower bound is %ld)",
		     &coarray->where, d,
		     mpz_get_si (sub_cons->expr->value.integer),
		     mpz_get_si (ca_bound->value.integer));
	  gfc_free_expr (ca_bound);
	  return &gfc_bad_expr;
	}

      gfc_free_expr (ca_bound);

      /* Check whether upperbound is valid for the multi-images case.  */
      if (d < as->corank)
	{
	  ca_bound = simplify_bound_dim (coarray, NULL, d + as->rank, 1, as,
					 NULL, true);
	  if (ca_bound == &gfc_bad_expr)
	    return ca_bound;

	  if (ca_bound && ca_bound->expr_type == EXPR_CONSTANT
	      && mpz_cmp (ca_bound->value.integer,
			  sub_cons->expr->value.integer) < 0)
	  {
	    gfc_error ("Out of bounds in IMAGE_INDEX at %L for dimension %d, "
		       "SUB has %ld and COARRAY upper bound is %ld)",
		       &coarray->where, d,
		       mpz_get_si (sub_cons->expr->value.integer),
		       mpz_get_si (ca_bound->value.integer));
	    gfc_free_expr (ca_bound);
	    return &gfc_bad_expr;
	  }

	  if (ca_bound)
	    gfc_free_expr (ca_bound);
	}

      sub_cons = gfc_constructor_next (sub_cons);
    }

  if (sub_cons != NULL)
    {
      gfc_error ("Too many elements in expression for SUB= argument at %L",
		 &sub->where);
      return &gfc_bad_expr;
    }

  result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
				  &gfc_current_locus);
  if (first_image)
    mpz_set_si (result->value.integer, 1);
  else
    mpz_set_si (result->value.integer, 0);

  return result;

not_implemented:
  gfc_error ("Not yet implemented: IMAGE_INDEX for coarray with non-constant "
	     "cobounds at %L", &coarray->where);
  return &gfc_bad_expr;
}


gfc_expr *
gfc_simplify_this_image (gfc_expr *coarray, gfc_expr *dim)
{
  gfc_ref *ref;
  gfc_array_spec *as;
  int d;

  if (coarray == NULL)
    {
      gfc_expr *result;
      /* FIXME: gfc_current_locus is wrong.  */
      result = gfc_get_constant_expr (BT_INTEGER, gfc_default_integer_kind,
				      &gfc_current_locus);
      mpz_set_si (result->value.integer, 1);
      return result;
    }

  gcc_assert (coarray->expr_type == EXPR_VARIABLE);

  /* Follow any component references.  */
  as = coarray->symtree->n.sym->as;
  for (ref = coarray->ref; ref; ref = ref->next)
    if (ref->type == REF_COMPONENT)
      as = ref->u.ar.as;

  if (as->type == AS_DEFERRED)
    goto not_implemented; /* return NULL;*/

  if (dim == NULL)
    {
      /* Multi-dimensional bounds.  */
      gfc_expr *bounds[GFC_MAX_DIMENSIONS];
      gfc_expr *e;

      /* Simplify the bounds for each dimension.  */
      for (d = 0; d < as->corank; d++)
	{
	  bounds[d] = simplify_bound_dim (coarray, NULL, d + as->rank + 1, 0,
					  as, NULL, true);
	  if (bounds[d] == NULL || bounds[d] == &gfc_bad_expr)
	    {
	      int j;

	      for (j = 0; j < d; j++)
		gfc_free_expr (bounds[j]);
	      if (bounds[d] == NULL)
		goto not_implemented;
	      return bounds[d];
	    }
	}

      /* Allocate the result expression.  */
      e = gfc_get_expr ();
      e->where = coarray->where;
      e->expr_type = EXPR_ARRAY;
      e->ts.type = BT_INTEGER;
      e->ts.kind = gfc_default_integer_kind;

      e->rank = 1;
      e->shape = gfc_get_shape (1);
      mpz_init_set_ui (e->shape[0], as->corank);

      /* Create the constructor for this array.  */
      for (d = 0; d < as->corank; d++)
        gfc_constructor_append_expr (&e->value.constructor,
                                     bounds[d], &e->where);

      return e;
    }
  else
    {
      gfc_expr *e;
      /* A DIM argument is specified.  */
      if (dim->expr_type != EXPR_CONSTANT)
	goto not_implemented; /*return NULL;*/

      d = mpz_get_si (dim->value.integer);

      if (d < 1 || d > as->corank)
	{
	  gfc_error ("DIM argument at %L is out of bounds", &dim->where);
	  return &gfc_bad_expr;
	}

      /*return simplify_bound_dim (coarray, NULL, d + as->rank, 0, as, NULL, true);*/
      e = simplify_bound_dim (coarray, NULL, d + as->rank, 0, as, NULL, true);
      if (e != NULL)
	return e;
      else
	goto not_implemented;
   }

not_implemented:
  gfc_error ("Not yet implemented: THIS_IMAGE for coarray with non-constant "
	     "cobounds at %L", &coarray->where);
  return &gfc_bad_expr;
}


gfc_expr *
6324
gfc_simplify_ubound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
6325
{
6326
  return simplify_bound (array, dim, kind, 1);
6327 6328
}

6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343
gfc_expr *
gfc_simplify_ucobound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
{
  gfc_expr *e;
  /* return simplify_cobound (array, dim, kind, 1);*/

  e = simplify_cobound (array, dim, kind, 1);
  if (e != NULL)
    return e;

  gfc_error ("Not yet implemented: UCOBOUND for coarray with non-constant "
	     "cobounds at %L", &array->where);
  return &gfc_bad_expr;
}

6344 6345

gfc_expr *
6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356
gfc_simplify_unpack (gfc_expr *vector, gfc_expr *mask, gfc_expr *field)
{
  gfc_expr *result, *e;
  gfc_constructor *vector_ctor, *mask_ctor, *field_ctor;

  if (!is_constant_array_expr (vector)
      || !is_constant_array_expr (mask)
      || (!gfc_is_constant_expr (field)
	  && !is_constant_array_expr(field)))
    return NULL;

Jerry DeLisle committed
6357 6358
  result = gfc_get_array_expr (vector->ts.type, vector->ts.kind,
			       &vector->where);
6359 6360
  if (vector->ts.type == BT_DERIVED)
    result->ts.u.derived = vector->ts.u.derived;
6361 6362 6363 6364
  result->rank = mask->rank;
  result->shape = gfc_copy_shape (mask->shape, mask->rank);

  if (vector->ts.type == BT_CHARACTER)
6365
    result->ts.u.cl = vector->ts.u.cl;
6366

Jerry DeLisle committed
6367 6368 6369 6370 6371 6372
  vector_ctor = gfc_constructor_first (vector->value.constructor);
  mask_ctor = gfc_constructor_first (mask->value.constructor);
  field_ctor
    = field->expr_type == EXPR_ARRAY
			    ? gfc_constructor_first (field->value.constructor)
			    : NULL;
6373 6374 6375 6376 6377 6378 6379

  while (mask_ctor)
    {
      if (mask_ctor->expr->value.logical)
	{
	  gcc_assert (vector_ctor);
	  e = gfc_copy_expr (vector_ctor->expr);
Jerry DeLisle committed
6380
	  vector_ctor = gfc_constructor_next (vector_ctor);
6381 6382 6383 6384 6385 6386
	}
      else if (field->expr_type == EXPR_ARRAY)
	e = gfc_copy_expr (field_ctor->expr);
      else
	e = gfc_copy_expr (field);

Jerry DeLisle committed
6387
      gfc_constructor_append_expr (&result->value.constructor, e, NULL);
6388

Jerry DeLisle committed
6389 6390
      mask_ctor = gfc_constructor_next (mask_ctor);
      field_ctor = gfc_constructor_next (field_ctor);
6391 6392 6393 6394 6395 6396 6397
    }

  return result;
}


gfc_expr *
6398
gfc_simplify_verify (gfc_expr *s, gfc_expr *set, gfc_expr *b, gfc_expr *kind)
6399 6400 6401 6402 6403
{
  gfc_expr *result;
  int back;
  size_t index, len, lenset;
  size_t i;
6404 6405 6406 6407
  int k = get_kind (BT_INTEGER, kind, "VERIFY", gfc_default_integer_kind);

  if (k == -1)
    return &gfc_bad_expr;
6408 6409 6410 6411 6412 6413 6414 6415 6416

  if (s->expr_type != EXPR_CONSTANT || set->expr_type != EXPR_CONSTANT)
    return NULL;

  if (b != NULL && b->value.logical != 0)
    back = 1;
  else
    back = 0;

Jerry DeLisle committed
6417
  result = gfc_get_constant_expr (BT_INTEGER, k, &s->where);
6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431

  len = s->value.character.length;
  lenset = set->value.character.length;

  if (len == 0)
    {
      mpz_set_ui (result->value.integer, 0);
      return result;
    }

  if (back == 0)
    {
      if (lenset == 0)
	{
6432
	  mpz_set_ui (result->value.integer, 1);
6433 6434 6435
	  return result;
	}

6436 6437
      index = wide_strspn (s->value.character.string,
			   set->value.character.string) + 1;
6438 6439 6440 6441 6442 6443 6444 6445
      if (index > len)
	index = 0;

    }
  else
    {
      if (lenset == 0)
	{
6446
	  mpz_set_ui (result->value.integer, len);
6447 6448 6449
	  return result;
	}
      for (index = len; index > 0; index --)
6450 6451 6452 6453 6454 6455 6456 6457 6458 6459
	{
	  for (i = 0; i < lenset; i++)
	    {
	      if (s->value.character.string[index - 1]
		  == set->value.character.string[i])
		break;
	    }
	  if (i == lenset)
	    break;
	}
6460 6461 6462 6463 6464 6465
    }

  mpz_set_ui (result->value.integer, index);
  return result;
}

6466 6467

gfc_expr *
6468
gfc_simplify_xor (gfc_expr *x, gfc_expr *y)
6469 6470 6471 6472 6473 6474 6475 6476
{
  gfc_expr *result;
  int kind;

  if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
    return NULL;

  kind = x->ts.kind > y->ts.kind ? x->ts.kind : y->ts.kind;
Jerry DeLisle committed
6477 6478

  switch (x->ts.type)
6479
    {
Jerry DeLisle committed
6480 6481 6482 6483 6484 6485 6486 6487 6488
      case BT_INTEGER:
	result = gfc_get_constant_expr (BT_INTEGER, kind, &x->where);
	mpz_xor (result->value.integer, x->value.integer, y->value.integer);
	return range_check (result, "XOR");

      case BT_LOGICAL:
	return gfc_get_logical_expr (kind, &x->where,
				     (x->value.logical && !y->value.logical)
				     || (!x->value.logical && y->value.logical));
6489

Jerry DeLisle committed
6490 6491 6492
      default:
	gcc_unreachable ();
    }
6493 6494 6495
}


6496 6497 6498 6499 6500 6501 6502 6503
/****************** Constant simplification *****************/

/* Master function to convert one constant to another.  While this is
   used as a simplification function, it requires the destination type
   and kind information which is supplied by a special case in
   do_simplify().  */

gfc_expr *
6504
gfc_convert_constant (gfc_expr *e, bt type, int kind)
6505 6506
{
  gfc_expr *g, *result, *(*f) (gfc_expr *, int);
Jerry DeLisle committed
6507
  gfc_constructor *c;
6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522

  switch (e->ts.type)
    {
    case BT_INTEGER:
      switch (type)
	{
	case BT_INTEGER:
	  f = gfc_int2int;
	  break;
	case BT_REAL:
	  f = gfc_int2real;
	  break;
	case BT_COMPLEX:
	  f = gfc_int2complex;
	  break;
6523 6524 6525
	case BT_LOGICAL:
	  f = gfc_int2log;
	  break;
6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566
	default:
	  goto oops;
	}
      break;

    case BT_REAL:
      switch (type)
	{
	case BT_INTEGER:
	  f = gfc_real2int;
	  break;
	case BT_REAL:
	  f = gfc_real2real;
	  break;
	case BT_COMPLEX:
	  f = gfc_real2complex;
	  break;
	default:
	  goto oops;
	}
      break;

    case BT_COMPLEX:
      switch (type)
	{
	case BT_INTEGER:
	  f = gfc_complex2int;
	  break;
	case BT_REAL:
	  f = gfc_complex2real;
	  break;
	case BT_COMPLEX:
	  f = gfc_complex2complex;
	  break;

	default:
	  goto oops;
	}
      break;

    case BT_LOGICAL:
6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577
      switch (type)
	{
	case BT_INTEGER:
	  f = gfc_log2int;
	  break;
	case BT_LOGICAL:
	  f = gfc_log2log;
	  break;
	default:
	  goto oops;
	}
6578 6579
      break;

6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607
    case BT_HOLLERITH:
      switch (type)
	{
	case BT_INTEGER:
	  f = gfc_hollerith2int;
	  break;

	case BT_REAL:
	  f = gfc_hollerith2real;
	  break;

	case BT_COMPLEX:
	  f = gfc_hollerith2complex;
	  break;

	case BT_CHARACTER:
	  f = gfc_hollerith2character;
	  break;

	case BT_LOGICAL:
	  f = gfc_hollerith2logical;
	  break;

	default:
	  goto oops;
	}
      break;

6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626
    default:
    oops:
      gfc_internal_error ("gfc_convert_constant(): Unexpected type");
    }

  result = NULL;

  switch (e->expr_type)
    {
    case EXPR_CONSTANT:
      result = f (e, kind);
      if (result == NULL)
	return &gfc_bad_expr;
      break;

    case EXPR_ARRAY:
      if (!gfc_is_constant_expr (e))
	break;

Jerry DeLisle committed
6627 6628 6629
      result = gfc_get_array_expr (type, kind, &e->where);
      result->shape = gfc_copy_shape (e->shape, e->rank);
      result->rank = e->rank;
6630

Jerry DeLisle committed
6631 6632
      for (c = gfc_constructor_first (e->value.constructor);
	   c; c = gfc_constructor_next (c))
6633
	{
Jerry DeLisle committed
6634
	  gfc_expr *tmp;
6635
	  if (c->iterator == NULL)
Jerry DeLisle committed
6636
	    tmp = f (c->expr, kind);
6637 6638 6639 6640
	  else
	    {
	      g = gfc_convert_constant (c->expr, type, kind);
	      if (g == &gfc_bad_expr)
Jerry DeLisle committed
6641 6642 6643 6644 6645
	        {
		  gfc_free_expr (result);
		  return g;
		}
	      tmp = g;
6646 6647
	    }

Jerry DeLisle committed
6648
	  if (tmp == NULL)
6649
	    {
Jerry DeLisle committed
6650
	      gfc_free_expr (result);
6651 6652
	      return NULL;
	    }
Jerry DeLisle committed
6653 6654 6655

	  gfc_constructor_append_expr (&result->value.constructor,
				       tmp, &c->where);
6656 6657 6658 6659 6660 6661 6662 6663 6664 6665
	}

      break;

    default:
      break;
    }

  return result;
}
6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677


/* Function for converting character constants.  */
gfc_expr *
gfc_convert_char_constant (gfc_expr *e, bt type ATTRIBUTE_UNUSED, int kind)
{
  gfc_expr *result;
  int i;

  if (!gfc_is_constant_expr (e))
    return NULL;

6678 6679 6680
  if (e->expr_type == EXPR_CONSTANT)
    {
      /* Simple case of a scalar.  */
Jerry DeLisle committed
6681
      result = gfc_get_constant_expr (BT_CHARACTER, kind, &e->where);
6682
      if (result == NULL)
6683 6684
	return &gfc_bad_expr;

6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707
      result->value.character.length = e->value.character.length;
      result->value.character.string
	= gfc_get_wide_string (e->value.character.length + 1);
      memcpy (result->value.character.string, e->value.character.string,
	      (e->value.character.length + 1) * sizeof (gfc_char_t));

      /* Check we only have values representable in the destination kind.  */
      for (i = 0; i < result->value.character.length; i++)
	if (!gfc_check_character_range (result->value.character.string[i],
					kind))
	  {
	    gfc_error ("Character '%s' in string at %L cannot be converted "
		       "into character kind %d",
		       gfc_print_wide_char (result->value.character.string[i]),
		       &e->where, kind);
	    return &gfc_bad_expr;
	  }

      return result;
    }
  else if (e->expr_type == EXPR_ARRAY)
    {
      /* For an array constructor, we convert each constructor element.  */
Jerry DeLisle committed
6708
      gfc_constructor *c;
6709

Jerry DeLisle committed
6710 6711 6712 6713
      result = gfc_get_array_expr (type, kind, &e->where);
      result->shape = gfc_copy_shape (e->shape, e->rank);
      result->rank = e->rank;
      result->ts.u.cl = e->ts.u.cl;
6714

Jerry DeLisle committed
6715 6716 6717 6718 6719
      for (c = gfc_constructor_first (e->value.constructor);
	   c; c = gfc_constructor_next (c))
	{
	  gfc_expr *tmp = gfc_convert_char_constant (c->expr, type, kind);
	  if (tmp == &gfc_bad_expr)
6720
	    {
Jerry DeLisle committed
6721
	      gfc_free_expr (result);
6722 6723 6724
	      return &gfc_bad_expr;
	    }

Jerry DeLisle committed
6725
	  if (tmp == NULL)
6726
	    {
Jerry DeLisle committed
6727
	      gfc_free_expr (result);
6728 6729 6730
	      return NULL;
	    }

Jerry DeLisle committed
6731 6732 6733
	  gfc_constructor_append_expr (&result->value.constructor,
				       tmp, &c->where);
	}
6734 6735 6736 6737 6738

      return result;
    }
  else
    return NULL;
6739
}
6740 6741 6742 6743 6744


gfc_expr *
gfc_simplify_compiler_options (void)
{
6745 6746 6747 6748 6749 6750 6751 6752
  char *str;
  gfc_expr *result;

  str = gfc_get_option_string ();
  result = gfc_get_character_expr (gfc_default_character_kind,
				   &gfc_current_locus, str, strlen (str));
  gfc_free (str);
  return result;
6753 6754 6755 6756 6757 6758
}


gfc_expr *
gfc_simplify_compiler_version (void)
{
6759 6760 6761 6762 6763 6764
  char *buffer;
  size_t len;

  len = strlen ("GCC version ") + strlen (version_string) + 1;
  buffer = (char*) alloca (len);
  snprintf (buffer, len, "GCC version %s", version_string);
6765
  return gfc_get_character_expr (gfc_default_character_kind,
6766
                                &gfc_current_locus, buffer, len);
6767
}