]> gcc.gnu.org Git - gcc.git/blob - gcc/fortran/arith.c
b473fdb15eeee4f95e6819fd397d9ee8a645793d
[gcc.git] / gcc / fortran / arith.c
1 /* Compiler arithmetic
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
22
23 /* Since target arithmetic must be done on the host, there has to
24 be some way of evaluating arithmetic expressions as the host
25 would evaluate them. We use the GNU MP library and the MPFR
26 library to do arithmetic, and this file provides the interface. */
27
28 #include "config.h"
29 #include "system.h"
30 #include "flags.h"
31 #include "gfortran.h"
32 #include "arith.h"
33
34 /* MPFR does not have a direct replacement for mpz_set_f() from GMP.
35 It's easily implemented with a few calls though. */
36
37 void
38 gfc_mpfr_to_mpz (mpz_t z, mpfr_t x)
39 {
40 mp_exp_t e;
41
42 e = mpfr_get_z_exp (z, x);
43 /* MPFR 2.0.1 (included with GMP 4.1) has a bug whereby mpfr_get_z_exp
44 may set the sign of z incorrectly. Work around that here. */
45 if (mpfr_sgn (x) != mpz_sgn (z))
46 mpz_neg (z, z);
47
48 if (e > 0)
49 mpz_mul_2exp (z, z, e);
50 else
51 mpz_tdiv_q_2exp (z, z, -e);
52 }
53
54
55 /* Set the model number precision by the requested KIND. */
56
57 void
58 gfc_set_model_kind (int kind)
59 {
60 int index = gfc_validate_kind (BT_REAL, kind, false);
61 int base2prec;
62
63 base2prec = gfc_real_kinds[index].digits;
64 if (gfc_real_kinds[index].radix != 2)
65 base2prec *= gfc_real_kinds[index].radix / 2;
66 mpfr_set_default_prec (base2prec);
67 }
68
69
70 /* Set the model number precision from mpfr_t x. */
71
72 void
73 gfc_set_model (mpfr_t x)
74 {
75 mpfr_set_default_prec (mpfr_get_prec (x));
76 }
77
78 #if MPFR_VERSION_MAJOR < 2 || (MPFR_VERSION_MAJOR == 2 && MPFR_VERSION_MINOR < 2)
79 /* Calculate atan2 (y, x)
80
81 atan2(y, x) = atan(y/x) if x > 0,
82 sign(y)*(pi - atan(|y/x|)) if x < 0,
83 0 if x = 0 && y == 0,
84 sign(y)*pi/2 if x = 0 && y != 0.
85 */
86
87 void
88 arctangent2 (mpfr_t y, mpfr_t x, mpfr_t result)
89 {
90 int i;
91 mpfr_t t;
92
93 gfc_set_model (y);
94 mpfr_init (t);
95
96 i = mpfr_sgn (x);
97
98 if (i > 0)
99 {
100 mpfr_div (t, y, x, GFC_RND_MODE);
101 mpfr_atan (result, t, GFC_RND_MODE);
102 }
103 else if (i < 0)
104 {
105 mpfr_const_pi (result, GFC_RND_MODE);
106 mpfr_div (t, y, x, GFC_RND_MODE);
107 mpfr_abs (t, t, GFC_RND_MODE);
108 mpfr_atan (t, t, GFC_RND_MODE);
109 mpfr_sub (result, result, t, GFC_RND_MODE);
110 if (mpfr_sgn (y) < 0)
111 mpfr_neg (result, result, GFC_RND_MODE);
112 }
113 else
114 {
115 if (mpfr_sgn (y) == 0)
116 mpfr_set_ui (result, 0, GFC_RND_MODE);
117 else
118 {
119 mpfr_const_pi (result, GFC_RND_MODE);
120 mpfr_div_ui (result, result, 2, GFC_RND_MODE);
121 if (mpfr_sgn (y) < 0)
122 mpfr_neg (result, result, GFC_RND_MODE);
123 }
124 }
125
126 mpfr_clear (t);
127 }
128 #endif
129
130 /* Given an arithmetic error code, return a pointer to a string that
131 explains the error. */
132
133 static const char *
134 gfc_arith_error (arith code)
135 {
136 const char *p;
137
138 switch (code)
139 {
140 case ARITH_OK:
141 p = _("Arithmetic OK at %L");
142 break;
143 case ARITH_OVERFLOW:
144 p = _("Arithmetic overflow at %L");
145 break;
146 case ARITH_UNDERFLOW:
147 p = _("Arithmetic underflow at %L");
148 break;
149 case ARITH_NAN:
150 p = _("Arithmetic NaN at %L");
151 break;
152 case ARITH_DIV0:
153 p = _("Division by zero at %L");
154 break;
155 case ARITH_INCOMMENSURATE:
156 p = _("Array operands are incommensurate at %L");
157 break;
158 case ARITH_ASYMMETRIC:
159 p =
160 _("Integer outside symmetric range implied by Standard Fortran at %L");
161 break;
162 default:
163 gfc_internal_error ("gfc_arith_error(): Bad error code");
164 }
165
166 return p;
167 }
168
169
170 /* Get things ready to do math. */
171
172 void
173 gfc_arith_init_1 (void)
174 {
175 gfc_integer_info *int_info;
176 gfc_real_info *real_info;
177 mpfr_t a, b, c;
178 mpz_t r;
179 int i;
180
181 mpfr_set_default_prec (128);
182 mpfr_init (a);
183 mpz_init (r);
184
185 /* Convert the minimum and maximum values for each kind into their
186 GNU MP representation. */
187 for (int_info = gfc_integer_kinds; int_info->kind != 0; int_info++)
188 {
189 /* Huge */
190 mpz_set_ui (r, int_info->radix);
191 mpz_pow_ui (r, r, int_info->digits);
192
193 mpz_init (int_info->huge);
194 mpz_sub_ui (int_info->huge, r, 1);
195
196 /* These are the numbers that are actually representable by the
197 target. For bases other than two, this needs to be changed. */
198 if (int_info->radix != 2)
199 gfc_internal_error ("Fix min_int calculation");
200
201 /* See PRs 13490 and 17912, related to integer ranges.
202 The pedantic_min_int exists for range checking when a program
203 is compiled with -pedantic, and reflects the belief that
204 Standard Fortran requires integers to be symmetrical, i.e.
205 every negative integer must have a representable positive
206 absolute value, and vice versa. */
207
208 mpz_init (int_info->pedantic_min_int);
209 mpz_neg (int_info->pedantic_min_int, int_info->huge);
210
211 mpz_init (int_info->min_int);
212 mpz_sub_ui (int_info->min_int, int_info->pedantic_min_int, 1);
213
214 /* Range */
215 mpfr_set_z (a, int_info->huge, GFC_RND_MODE);
216 mpfr_log10 (a, a, GFC_RND_MODE);
217 mpfr_trunc (a, a);
218 gfc_mpfr_to_mpz (r, a);
219 int_info->range = mpz_get_si (r);
220 }
221
222 mpfr_clear (a);
223
224 for (real_info = gfc_real_kinds; real_info->kind != 0; real_info++)
225 {
226 gfc_set_model_kind (real_info->kind);
227
228 mpfr_init (a);
229 mpfr_init (b);
230 mpfr_init (c);
231
232 /* huge(x) = (1 - b**(-p)) * b**(emax-1) * b */
233 /* a = 1 - b**(-p) */
234 mpfr_set_ui (a, 1, GFC_RND_MODE);
235 mpfr_set_ui (b, real_info->radix, GFC_RND_MODE);
236 mpfr_pow_si (b, b, -real_info->digits, GFC_RND_MODE);
237 mpfr_sub (a, a, b, GFC_RND_MODE);
238
239 /* c = b**(emax-1) */
240 mpfr_set_ui (b, real_info->radix, GFC_RND_MODE);
241 mpfr_pow_ui (c, b, real_info->max_exponent - 1, GFC_RND_MODE);
242
243 /* a = a * c = (1 - b**(-p)) * b**(emax-1) */
244 mpfr_mul (a, a, c, GFC_RND_MODE);
245
246 /* a = (1 - b**(-p)) * b**(emax-1) * b */
247 mpfr_mul_ui (a, a, real_info->radix, GFC_RND_MODE);
248
249 mpfr_init (real_info->huge);
250 mpfr_set (real_info->huge, a, GFC_RND_MODE);
251
252 /* tiny(x) = b**(emin-1) */
253 mpfr_set_ui (b, real_info->radix, GFC_RND_MODE);
254 mpfr_pow_si (b, b, real_info->min_exponent - 1, GFC_RND_MODE);
255
256 mpfr_init (real_info->tiny);
257 mpfr_set (real_info->tiny, b, GFC_RND_MODE);
258
259 /* subnormal (x) = b**(emin - digit) */
260 mpfr_set_ui (b, real_info->radix, GFC_RND_MODE);
261 mpfr_pow_si (b, b, real_info->min_exponent - real_info->digits,
262 GFC_RND_MODE);
263
264 mpfr_init (real_info->subnormal);
265 mpfr_set (real_info->subnormal, b, GFC_RND_MODE);
266
267 /* epsilon(x) = b**(1-p) */
268 mpfr_set_ui (b, real_info->radix, GFC_RND_MODE);
269 mpfr_pow_si (b, b, 1 - real_info->digits, GFC_RND_MODE);
270
271 mpfr_init (real_info->epsilon);
272 mpfr_set (real_info->epsilon, b, GFC_RND_MODE);
273
274 /* range(x) = int(min(log10(huge(x)), -log10(tiny)) */
275 mpfr_log10 (a, real_info->huge, GFC_RND_MODE);
276 mpfr_log10 (b, real_info->tiny, GFC_RND_MODE);
277 mpfr_neg (b, b, GFC_RND_MODE);
278
279 /* a = min(a, b) */
280 if (mpfr_cmp (a, b) > 0)
281 mpfr_set (a, b, GFC_RND_MODE);
282
283 mpfr_trunc (a, a);
284 gfc_mpfr_to_mpz (r, a);
285 real_info->range = mpz_get_si (r);
286
287 /* precision(x) = int((p - 1) * log10(b)) + k */
288 mpfr_set_ui (a, real_info->radix, GFC_RND_MODE);
289 mpfr_log10 (a, a, GFC_RND_MODE);
290
291 mpfr_mul_ui (a, a, real_info->digits - 1, GFC_RND_MODE);
292 mpfr_trunc (a, a);
293 gfc_mpfr_to_mpz (r, a);
294 real_info->precision = mpz_get_si (r);
295
296 /* If the radix is an integral power of 10, add one to the precision. */
297 for (i = 10; i <= real_info->radix; i *= 10)
298 if (i == real_info->radix)
299 real_info->precision++;
300
301 mpfr_clear (a);
302 mpfr_clear (b);
303 mpfr_clear (c);
304 }
305
306 mpz_clear (r);
307 }
308
309
310 /* Clean up, get rid of numeric constants. */
311
312 void
313 gfc_arith_done_1 (void)
314 {
315 gfc_integer_info *ip;
316 gfc_real_info *rp;
317
318 for (ip = gfc_integer_kinds; ip->kind; ip++)
319 {
320 mpz_clear (ip->min_int);
321 mpz_clear (ip->pedantic_min_int);
322 mpz_clear (ip->huge);
323 }
324
325 for (rp = gfc_real_kinds; rp->kind; rp++)
326 {
327 mpfr_clear (rp->epsilon);
328 mpfr_clear (rp->huge);
329 mpfr_clear (rp->tiny);
330 mpfr_clear (rp->subnormal);
331 }
332 }
333
334
335 /* Given an integer and a kind, make sure that the integer lies within
336 the range of the kind. Returns ARITH_OK, ARITH_ASYMMETRIC or
337 ARITH_OVERFLOW. */
338
339 arith
340 gfc_check_integer_range (mpz_t p, int kind)
341 {
342 arith result;
343 int i;
344
345 i = gfc_validate_kind (BT_INTEGER, kind, false);
346 result = ARITH_OK;
347
348 if (pedantic)
349 {
350 if (mpz_cmp (p, gfc_integer_kinds[i].pedantic_min_int) < 0)
351 result = ARITH_ASYMMETRIC;
352 }
353
354
355 if (gfc_option.flag_range_check == 0)
356 return result;
357
358 if (mpz_cmp (p, gfc_integer_kinds[i].min_int) < 0
359 || mpz_cmp (p, gfc_integer_kinds[i].huge) > 0)
360 result = ARITH_OVERFLOW;
361
362 return result;
363 }
364
365
366 /* Given a real and a kind, make sure that the real lies within the
367 range of the kind. Returns ARITH_OK, ARITH_OVERFLOW or
368 ARITH_UNDERFLOW. */
369
370 static arith
371 gfc_check_real_range (mpfr_t p, int kind)
372 {
373 arith retval;
374 mpfr_t q;
375 int i;
376
377 i = gfc_validate_kind (BT_REAL, kind, false);
378
379 gfc_set_model (p);
380 mpfr_init (q);
381 mpfr_abs (q, p, GFC_RND_MODE);
382
383 if (mpfr_inf_p (p))
384 {
385 if (gfc_option.flag_range_check == 0)
386 retval = ARITH_OK;
387 else
388 retval = ARITH_OVERFLOW;
389 }
390 else if (mpfr_nan_p (p))
391 {
392 if (gfc_option.flag_range_check == 0)
393 retval = ARITH_OK;
394 else
395 retval = ARITH_NAN;
396 }
397 else if (mpfr_sgn (q) == 0)
398 retval = ARITH_OK;
399 else if (mpfr_cmp (q, gfc_real_kinds[i].huge) > 0)
400 {
401 if (gfc_option.flag_range_check == 0)
402 retval = ARITH_OK;
403 else
404 retval = ARITH_OVERFLOW;
405 }
406 else if (mpfr_cmp (q, gfc_real_kinds[i].subnormal) < 0)
407 {
408 if (gfc_option.flag_range_check == 0)
409 retval = ARITH_OK;
410 else
411 retval = ARITH_UNDERFLOW;
412 }
413 else if (mpfr_cmp (q, gfc_real_kinds[i].tiny) < 0)
414 {
415 #if MPFR_VERSION_MAJOR < 2 || (MPFR_VERSION_MAJOR == 2 && MPFR_VERSION_MINOR < 2)
416 /* MPFR operates on a number with a given precision and enormous
417 exponential range. To represent subnormal numbers, the exponent is
418 allowed to become smaller than emin, but always retains the full
419 precision. This code resets unused bits to 0 to alleviate
420 rounding problems. Note, a future version of MPFR will have a
421 mpfr_subnormalize() function, which handles this truncation in a
422 more efficient and robust way. */
423
424 int j, k;
425 char *bin, *s;
426 mp_exp_t e;
427
428 bin = mpfr_get_str (NULL, &e, gfc_real_kinds[i].radix, 0, q, GMP_RNDN);
429 k = gfc_real_kinds[i].digits - (gfc_real_kinds[i].min_exponent - e);
430 for (j = k; j < gfc_real_kinds[i].digits; j++)
431 bin[j] = '0';
432 /* Need space for '0.', bin, 'E', and e */
433 s = (char *) gfc_getmem (strlen(bin) + 10);
434 sprintf (s, "0.%sE%d", bin, (int) e);
435 mpfr_set_str (q, s, gfc_real_kinds[i].radix, GMP_RNDN);
436
437 gfc_free (s);
438 gfc_free (bin);
439 #else
440 mp_exp_t emin, emax;
441
442 /* Save current values of emin and emax. */
443 emin = mpfr_get_emin ();
444 emax = mpfr_get_emax ();
445
446 /* Set emin and emax for the current model number. */
447 mpfr_set_emin ((mp_exp_t) gfc_real_kinds[i].min_exponent - 1);
448 mpfr_set_emax ((mp_exp_t) gfc_real_kinds[i].max_exponent - 1);
449 mpfr_subnormalize (q, 0, GFC_RND_MODE);
450
451 /* Reset emin and emax. */
452 mpfr_set_emin (emin);
453 mpfr_set_emax (emax);
454 #endif
455
456 /* Copy sign if needed. */
457 if (mpfr_sgn (p) < 0)
458 mpfr_neg (p, q, GMP_RNDN);
459 else
460 mpfr_set (p, q, GMP_RNDN);
461
462 retval = ARITH_OK;
463 }
464 else
465 retval = ARITH_OK;
466
467 mpfr_clear (q);
468
469 return retval;
470 }
471
472
473 /* Function to return a constant expression node of a given type and kind. */
474
475 gfc_expr *
476 gfc_constant_result (bt type, int kind, locus * where)
477 {
478 gfc_expr *result;
479
480 if (!where)
481 gfc_internal_error
482 ("gfc_constant_result(): locus 'where' cannot be NULL");
483
484 result = gfc_get_expr ();
485
486 result->expr_type = EXPR_CONSTANT;
487 result->ts.type = type;
488 result->ts.kind = kind;
489 result->where = *where;
490
491 switch (type)
492 {
493 case BT_INTEGER:
494 mpz_init (result->value.integer);
495 break;
496
497 case BT_REAL:
498 gfc_set_model_kind (kind);
499 mpfr_init (result->value.real);
500 break;
501
502 case BT_COMPLEX:
503 gfc_set_model_kind (kind);
504 mpfr_init (result->value.complex.r);
505 mpfr_init (result->value.complex.i);
506 break;
507
508 default:
509 break;
510 }
511
512 return result;
513 }
514
515
516 /* Low-level arithmetic functions. All of these subroutines assume
517 that all operands are of the same type and return an operand of the
518 same type. The other thing about these subroutines is that they
519 can fail in various ways -- overflow, underflow, division by zero,
520 zero raised to the zero, etc. */
521
522 static arith
523 gfc_arith_not (gfc_expr * op1, gfc_expr ** resultp)
524 {
525 gfc_expr *result;
526
527 result = gfc_constant_result (BT_LOGICAL, op1->ts.kind, &op1->where);
528 result->value.logical = !op1->value.logical;
529 *resultp = result;
530
531 return ARITH_OK;
532 }
533
534
535 static arith
536 gfc_arith_and (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
537 {
538 gfc_expr *result;
539
540 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
541 &op1->where);
542 result->value.logical = op1->value.logical && op2->value.logical;
543 *resultp = result;
544
545 return ARITH_OK;
546 }
547
548
549 static arith
550 gfc_arith_or (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
551 {
552 gfc_expr *result;
553
554 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
555 &op1->where);
556 result->value.logical = op1->value.logical || op2->value.logical;
557 *resultp = result;
558
559 return ARITH_OK;
560 }
561
562
563 static arith
564 gfc_arith_eqv (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
565 {
566 gfc_expr *result;
567
568 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
569 &op1->where);
570 result->value.logical = op1->value.logical == op2->value.logical;
571 *resultp = result;
572
573 return ARITH_OK;
574 }
575
576
577 static arith
578 gfc_arith_neqv (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
579 {
580 gfc_expr *result;
581
582 result = gfc_constant_result (BT_LOGICAL, gfc_kind_max (op1, op2),
583 &op1->where);
584 result->value.logical = op1->value.logical != op2->value.logical;
585 *resultp = result;
586
587 return ARITH_OK;
588 }
589
590
591 /* Make sure a constant numeric expression is within the range for
592 its type and kind. Note that there's also a gfc_check_range(),
593 but that one deals with the intrinsic RANGE function. */
594
595 arith
596 gfc_range_check (gfc_expr * e)
597 {
598 arith rc;
599
600 switch (e->ts.type)
601 {
602 case BT_INTEGER:
603 rc = gfc_check_integer_range (e->value.integer, e->ts.kind);
604 break;
605
606 case BT_REAL:
607 rc = gfc_check_real_range (e->value.real, e->ts.kind);
608 if (rc == ARITH_UNDERFLOW)
609 mpfr_set_ui (e->value.real, 0, GFC_RND_MODE);
610 if (rc == ARITH_OVERFLOW)
611 mpfr_set_inf (e->value.real, mpfr_sgn (e->value.real));
612 if (rc == ARITH_NAN)
613 mpfr_set_nan (e->value.real);
614 break;
615
616 case BT_COMPLEX:
617 rc = gfc_check_real_range (e->value.complex.r, e->ts.kind);
618 if (rc == ARITH_UNDERFLOW)
619 mpfr_set_ui (e->value.complex.r, 0, GFC_RND_MODE);
620 if (rc == ARITH_OVERFLOW)
621 mpfr_set_inf (e->value.complex.r, mpfr_sgn (e->value.complex.r));
622 if (rc == ARITH_NAN)
623 mpfr_set_nan (e->value.complex.r);
624
625 rc = gfc_check_real_range (e->value.complex.i, e->ts.kind);
626 if (rc == ARITH_UNDERFLOW)
627 mpfr_set_ui (e->value.complex.i, 0, GFC_RND_MODE);
628 if (rc == ARITH_OVERFLOW)
629 mpfr_set_inf (e->value.complex.i, mpfr_sgn (e->value.complex.i));
630 if (rc == ARITH_NAN)
631 mpfr_set_nan (e->value.complex.i);
632 break;
633
634 default:
635 gfc_internal_error ("gfc_range_check(): Bad type");
636 }
637
638 return rc;
639 }
640
641
642 /* Several of the following routines use the same set of statements to
643 check the validity of the result. Encapsulate the checking here. */
644
645 static arith
646 check_result (arith rc, gfc_expr * x, gfc_expr * r, gfc_expr ** rp)
647 {
648 arith val = rc;
649
650 if (val == ARITH_UNDERFLOW)
651 {
652 if (gfc_option.warn_underflow)
653 gfc_warning (gfc_arith_error (val), &x->where);
654 val = ARITH_OK;
655 }
656
657 if (val == ARITH_ASYMMETRIC)
658 {
659 gfc_warning (gfc_arith_error (val), &x->where);
660 val = ARITH_OK;
661 }
662
663 if (val != ARITH_OK)
664 gfc_free_expr (r);
665 else
666 *rp = r;
667
668 return val;
669 }
670
671
672 /* It may seem silly to have a subroutine that actually computes the
673 unary plus of a constant, but it prevents us from making exceptions
674 in the code elsewhere. */
675
676 static arith
677 gfc_arith_uplus (gfc_expr * op1, gfc_expr ** resultp)
678 {
679 *resultp = gfc_copy_expr (op1);
680 return ARITH_OK;
681 }
682
683
684 static arith
685 gfc_arith_uminus (gfc_expr * op1, gfc_expr ** resultp)
686 {
687 gfc_expr *result;
688 arith rc;
689
690 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
691
692 switch (op1->ts.type)
693 {
694 case BT_INTEGER:
695 mpz_neg (result->value.integer, op1->value.integer);
696 break;
697
698 case BT_REAL:
699 mpfr_neg (result->value.real, op1->value.real, GFC_RND_MODE);
700 break;
701
702 case BT_COMPLEX:
703 mpfr_neg (result->value.complex.r, op1->value.complex.r, GFC_RND_MODE);
704 mpfr_neg (result->value.complex.i, op1->value.complex.i, GFC_RND_MODE);
705 break;
706
707 default:
708 gfc_internal_error ("gfc_arith_uminus(): Bad basic type");
709 }
710
711 rc = gfc_range_check (result);
712
713 return check_result (rc, op1, result, resultp);
714 }
715
716
717 static arith
718 gfc_arith_plus (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
719 {
720 gfc_expr *result;
721 arith rc;
722
723 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
724
725 switch (op1->ts.type)
726 {
727 case BT_INTEGER:
728 mpz_add (result->value.integer, op1->value.integer, op2->value.integer);
729 break;
730
731 case BT_REAL:
732 mpfr_add (result->value.real, op1->value.real, op2->value.real,
733 GFC_RND_MODE);
734 break;
735
736 case BT_COMPLEX:
737 mpfr_add (result->value.complex.r, op1->value.complex.r,
738 op2->value.complex.r, GFC_RND_MODE);
739
740 mpfr_add (result->value.complex.i, op1->value.complex.i,
741 op2->value.complex.i, GFC_RND_MODE);
742 break;
743
744 default:
745 gfc_internal_error ("gfc_arith_plus(): Bad basic type");
746 }
747
748 rc = gfc_range_check (result);
749
750 return check_result (rc, op1, result, resultp);
751 }
752
753
754 static arith
755 gfc_arith_minus (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
756 {
757 gfc_expr *result;
758 arith rc;
759
760 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
761
762 switch (op1->ts.type)
763 {
764 case BT_INTEGER:
765 mpz_sub (result->value.integer, op1->value.integer, op2->value.integer);
766 break;
767
768 case BT_REAL:
769 mpfr_sub (result->value.real, op1->value.real, op2->value.real,
770 GFC_RND_MODE);
771 break;
772
773 case BT_COMPLEX:
774 mpfr_sub (result->value.complex.r, op1->value.complex.r,
775 op2->value.complex.r, GFC_RND_MODE);
776
777 mpfr_sub (result->value.complex.i, op1->value.complex.i,
778 op2->value.complex.i, GFC_RND_MODE);
779 break;
780
781 default:
782 gfc_internal_error ("gfc_arith_minus(): Bad basic type");
783 }
784
785 rc = gfc_range_check (result);
786
787 return check_result (rc, op1, result, resultp);
788 }
789
790
791 static arith
792 gfc_arith_times (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
793 {
794 gfc_expr *result;
795 mpfr_t x, y;
796 arith rc;
797
798 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
799
800 switch (op1->ts.type)
801 {
802 case BT_INTEGER:
803 mpz_mul (result->value.integer, op1->value.integer, op2->value.integer);
804 break;
805
806 case BT_REAL:
807 mpfr_mul (result->value.real, op1->value.real, op2->value.real,
808 GFC_RND_MODE);
809 break;
810
811 case BT_COMPLEX:
812 gfc_set_model (op1->value.complex.r);
813 mpfr_init (x);
814 mpfr_init (y);
815
816 mpfr_mul (x, op1->value.complex.r, op2->value.complex.r, GFC_RND_MODE);
817 mpfr_mul (y, op1->value.complex.i, op2->value.complex.i, GFC_RND_MODE);
818 mpfr_sub (result->value.complex.r, x, y, GFC_RND_MODE);
819
820 mpfr_mul (x, op1->value.complex.r, op2->value.complex.i, GFC_RND_MODE);
821 mpfr_mul (y, op1->value.complex.i, op2->value.complex.r, GFC_RND_MODE);
822 mpfr_add (result->value.complex.i, x, y, GFC_RND_MODE);
823
824 mpfr_clear (x);
825 mpfr_clear (y);
826 break;
827
828 default:
829 gfc_internal_error ("gfc_arith_times(): Bad basic type");
830 }
831
832 rc = gfc_range_check (result);
833
834 return check_result (rc, op1, result, resultp);
835 }
836
837
838 static arith
839 gfc_arith_divide (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
840 {
841 gfc_expr *result;
842 mpfr_t x, y, div;
843 arith rc;
844
845 rc = ARITH_OK;
846
847 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
848
849 switch (op1->ts.type)
850 {
851 case BT_INTEGER:
852 if (mpz_sgn (op2->value.integer) == 0)
853 {
854 rc = ARITH_DIV0;
855 break;
856 }
857
858 mpz_tdiv_q (result->value.integer, op1->value.integer,
859 op2->value.integer);
860 break;
861
862 case BT_REAL:
863 if (mpfr_sgn (op2->value.real) == 0
864 && gfc_option.flag_range_check == 1)
865 {
866 rc = ARITH_DIV0;
867 break;
868 }
869
870 mpfr_div (result->value.real, op1->value.real, op2->value.real,
871 GFC_RND_MODE);
872 break;
873
874 case BT_COMPLEX:
875 if (mpfr_sgn (op2->value.complex.r) == 0
876 && mpfr_sgn (op2->value.complex.i) == 0
877 && gfc_option.flag_range_check == 1)
878 {
879 rc = ARITH_DIV0;
880 break;
881 }
882
883 gfc_set_model (op1->value.complex.r);
884 mpfr_init (x);
885 mpfr_init (y);
886 mpfr_init (div);
887
888 mpfr_mul (x, op2->value.complex.r, op2->value.complex.r, GFC_RND_MODE);
889 mpfr_mul (y, op2->value.complex.i, op2->value.complex.i, GFC_RND_MODE);
890 mpfr_add (div, x, y, GFC_RND_MODE);
891
892 mpfr_mul (x, op1->value.complex.r, op2->value.complex.r, GFC_RND_MODE);
893 mpfr_mul (y, op1->value.complex.i, op2->value.complex.i, GFC_RND_MODE);
894 mpfr_add (result->value.complex.r, x, y, GFC_RND_MODE);
895 mpfr_div (result->value.complex.r, result->value.complex.r, div,
896 GFC_RND_MODE);
897
898 mpfr_mul (x, op1->value.complex.i, op2->value.complex.r, GFC_RND_MODE);
899 mpfr_mul (y, op1->value.complex.r, op2->value.complex.i, GFC_RND_MODE);
900 mpfr_sub (result->value.complex.i, x, y, GFC_RND_MODE);
901 mpfr_div (result->value.complex.i, result->value.complex.i, div,
902 GFC_RND_MODE);
903
904 mpfr_clear (x);
905 mpfr_clear (y);
906 mpfr_clear (div);
907 break;
908
909 default:
910 gfc_internal_error ("gfc_arith_divide(): Bad basic type");
911 }
912
913 if (rc == ARITH_OK)
914 rc = gfc_range_check (result);
915
916 return check_result (rc, op1, result, resultp);
917 }
918
919
920 /* Compute the reciprocal of a complex number (guaranteed nonzero). */
921
922 static void
923 complex_reciprocal (gfc_expr * op)
924 {
925 mpfr_t mod, a, re, im;
926
927 gfc_set_model (op->value.complex.r);
928 mpfr_init (mod);
929 mpfr_init (a);
930 mpfr_init (re);
931 mpfr_init (im);
932
933 mpfr_mul (mod, op->value.complex.r, op->value.complex.r, GFC_RND_MODE);
934 mpfr_mul (a, op->value.complex.i, op->value.complex.i, GFC_RND_MODE);
935 mpfr_add (mod, mod, a, GFC_RND_MODE);
936
937 mpfr_div (re, op->value.complex.r, mod, GFC_RND_MODE);
938
939 mpfr_neg (im, op->value.complex.i, GFC_RND_MODE);
940 mpfr_div (im, im, mod, GFC_RND_MODE);
941
942 mpfr_set (op->value.complex.r, re, GFC_RND_MODE);
943 mpfr_set (op->value.complex.i, im, GFC_RND_MODE);
944
945 mpfr_clear (re);
946 mpfr_clear (im);
947 mpfr_clear (mod);
948 mpfr_clear (a);
949 }
950
951
952 /* Raise a complex number to positive power. */
953
954 static void
955 complex_pow_ui (gfc_expr * base, int power, gfc_expr * result)
956 {
957 mpfr_t re, im, a;
958
959 gfc_set_model (base->value.complex.r);
960 mpfr_init (re);
961 mpfr_init (im);
962 mpfr_init (a);
963
964 mpfr_set_ui (result->value.complex.r, 1, GFC_RND_MODE);
965 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
966
967 for (; power > 0; power--)
968 {
969 mpfr_mul (re, base->value.complex.r, result->value.complex.r,
970 GFC_RND_MODE);
971 mpfr_mul (a, base->value.complex.i, result->value.complex.i,
972 GFC_RND_MODE);
973 mpfr_sub (re, re, a, GFC_RND_MODE);
974
975 mpfr_mul (im, base->value.complex.r, result->value.complex.i,
976 GFC_RND_MODE);
977 mpfr_mul (a, base->value.complex.i, result->value.complex.r,
978 GFC_RND_MODE);
979 mpfr_add (im, im, a, GFC_RND_MODE);
980
981 mpfr_set (result->value.complex.r, re, GFC_RND_MODE);
982 mpfr_set (result->value.complex.i, im, GFC_RND_MODE);
983 }
984
985 mpfr_clear (re);
986 mpfr_clear (im);
987 mpfr_clear (a);
988 }
989
990
991 /* Raise a number to an integer power. */
992
993 static arith
994 gfc_arith_power (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
995 {
996 int power, apower;
997 gfc_expr *result;
998 mpz_t unity_z;
999 mpfr_t unity_f;
1000 arith rc;
1001
1002 rc = ARITH_OK;
1003
1004 if (gfc_extract_int (op2, &power) != NULL)
1005 gfc_internal_error ("gfc_arith_power(): Bad exponent");
1006
1007 result = gfc_constant_result (op1->ts.type, op1->ts.kind, &op1->where);
1008
1009 if (power == 0)
1010 {
1011 /* Handle something to the zeroth power. Since we're dealing
1012 with integral exponents, there is no ambiguity in the
1013 limiting procedure used to determine the value of 0**0. */
1014 switch (op1->ts.type)
1015 {
1016 case BT_INTEGER:
1017 mpz_set_ui (result->value.integer, 1);
1018 break;
1019
1020 case BT_REAL:
1021 mpfr_set_ui (result->value.real, 1, GFC_RND_MODE);
1022 break;
1023
1024 case BT_COMPLEX:
1025 mpfr_set_ui (result->value.complex.r, 1, GFC_RND_MODE);
1026 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
1027 break;
1028
1029 default:
1030 gfc_internal_error ("gfc_arith_power(): Bad base");
1031 }
1032 }
1033 else
1034 {
1035 apower = power;
1036 if (power < 0)
1037 apower = -power;
1038
1039 switch (op1->ts.type)
1040 {
1041 case BT_INTEGER:
1042 mpz_pow_ui (result->value.integer, op1->value.integer, apower);
1043
1044 if (power < 0)
1045 {
1046 mpz_init_set_ui (unity_z, 1);
1047 mpz_tdiv_q (result->value.integer, unity_z,
1048 result->value.integer);
1049 mpz_clear (unity_z);
1050 }
1051 break;
1052
1053 case BT_REAL:
1054 mpfr_pow_ui (result->value.real, op1->value.real, apower,
1055 GFC_RND_MODE);
1056
1057 if (power < 0)
1058 {
1059 gfc_set_model (op1->value.real);
1060 mpfr_init (unity_f);
1061 mpfr_set_ui (unity_f, 1, GFC_RND_MODE);
1062 mpfr_div (result->value.real, unity_f, result->value.real,
1063 GFC_RND_MODE);
1064 mpfr_clear (unity_f);
1065 }
1066 break;
1067
1068 case BT_COMPLEX:
1069 complex_pow_ui (op1, apower, result);
1070 if (power < 0)
1071 complex_reciprocal (result);
1072 break;
1073
1074 default:
1075 break;
1076 }
1077 }
1078
1079 if (rc == ARITH_OK)
1080 rc = gfc_range_check (result);
1081
1082 return check_result (rc, op1, result, resultp);
1083 }
1084
1085
1086 /* Concatenate two string constants. */
1087
1088 static arith
1089 gfc_arith_concat (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1090 {
1091 gfc_expr *result;
1092 int len;
1093
1094 result = gfc_constant_result (BT_CHARACTER, gfc_default_character_kind,
1095 &op1->where);
1096
1097 len = op1->value.character.length + op2->value.character.length;
1098
1099 result->value.character.string = gfc_getmem (len + 1);
1100 result->value.character.length = len;
1101
1102 memcpy (result->value.character.string, op1->value.character.string,
1103 op1->value.character.length);
1104
1105 memcpy (result->value.character.string + op1->value.character.length,
1106 op2->value.character.string, op2->value.character.length);
1107
1108 result->value.character.string[len] = '\0';
1109
1110 *resultp = result;
1111
1112 return ARITH_OK;
1113 }
1114
1115
1116 /* Comparison operators. Assumes that the two expression nodes
1117 contain two constants of the same type. */
1118
1119 int
1120 gfc_compare_expr (gfc_expr * op1, gfc_expr * op2)
1121 {
1122 int rc;
1123
1124 switch (op1->ts.type)
1125 {
1126 case BT_INTEGER:
1127 rc = mpz_cmp (op1->value.integer, op2->value.integer);
1128 break;
1129
1130 case BT_REAL:
1131 rc = mpfr_cmp (op1->value.real, op2->value.real);
1132 break;
1133
1134 case BT_CHARACTER:
1135 rc = gfc_compare_string (op1, op2, NULL);
1136 break;
1137
1138 case BT_LOGICAL:
1139 rc = ((!op1->value.logical && op2->value.logical)
1140 || (op1->value.logical && !op2->value.logical));
1141 break;
1142
1143 default:
1144 gfc_internal_error ("gfc_compare_expr(): Bad basic type");
1145 }
1146
1147 return rc;
1148 }
1149
1150
1151 /* Compare a pair of complex numbers. Naturally, this is only for
1152 equality and nonequality. */
1153
1154 static int
1155 compare_complex (gfc_expr * op1, gfc_expr * op2)
1156 {
1157 return (mpfr_cmp (op1->value.complex.r, op2->value.complex.r) == 0
1158 && mpfr_cmp (op1->value.complex.i, op2->value.complex.i) == 0);
1159 }
1160
1161
1162 /* Given two constant strings and the inverse collating sequence, compare the
1163 strings. We return -1 for a < b, 0 for a == b and 1 for a > b. If the
1164 xcoll_table is NULL, we use the processor's default collating sequence. */
1165
1166 int
1167 gfc_compare_string (gfc_expr * a, gfc_expr * b, const int * xcoll_table)
1168 {
1169 int len, alen, blen, i, ac, bc;
1170
1171 alen = a->value.character.length;
1172 blen = b->value.character.length;
1173
1174 len = (alen > blen) ? alen : blen;
1175
1176 for (i = 0; i < len; i++)
1177 {
1178 /* We cast to unsigned char because default char, if it is signed,
1179 would lead to ac < 0 for string[i] > 127. */
1180 ac = (unsigned char) ((i < alen) ? a->value.character.string[i] : ' ');
1181 bc = (unsigned char) ((i < blen) ? b->value.character.string[i] : ' ');
1182
1183 if (xcoll_table != NULL)
1184 {
1185 ac = xcoll_table[ac];
1186 bc = xcoll_table[bc];
1187 }
1188
1189 if (ac < bc)
1190 return -1;
1191 if (ac > bc)
1192 return 1;
1193 }
1194
1195 /* Strings are equal */
1196
1197 return 0;
1198 }
1199
1200
1201 /* Specific comparison subroutines. */
1202
1203 static arith
1204 gfc_arith_eq (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1205 {
1206 gfc_expr *result;
1207
1208 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
1209 &op1->where);
1210 result->value.logical = (op1->ts.type == BT_COMPLEX) ?
1211 compare_complex (op1, op2) : (gfc_compare_expr (op1, op2) == 0);
1212
1213 *resultp = result;
1214 return ARITH_OK;
1215 }
1216
1217
1218 static arith
1219 gfc_arith_ne (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1220 {
1221 gfc_expr *result;
1222
1223 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
1224 &op1->where);
1225 result->value.logical = (op1->ts.type == BT_COMPLEX) ?
1226 !compare_complex (op1, op2) : (gfc_compare_expr (op1, op2) != 0);
1227
1228 *resultp = result;
1229 return ARITH_OK;
1230 }
1231
1232
1233 static arith
1234 gfc_arith_gt (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1235 {
1236 gfc_expr *result;
1237
1238 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
1239 &op1->where);
1240 result->value.logical = (gfc_compare_expr (op1, op2) > 0);
1241 *resultp = result;
1242
1243 return ARITH_OK;
1244 }
1245
1246
1247 static arith
1248 gfc_arith_ge (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1249 {
1250 gfc_expr *result;
1251
1252 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
1253 &op1->where);
1254 result->value.logical = (gfc_compare_expr (op1, op2) >= 0);
1255 *resultp = result;
1256
1257 return ARITH_OK;
1258 }
1259
1260
1261 static arith
1262 gfc_arith_lt (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1263 {
1264 gfc_expr *result;
1265
1266 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
1267 &op1->where);
1268 result->value.logical = (gfc_compare_expr (op1, op2) < 0);
1269 *resultp = result;
1270
1271 return ARITH_OK;
1272 }
1273
1274
1275 static arith
1276 gfc_arith_le (gfc_expr * op1, gfc_expr * op2, gfc_expr ** resultp)
1277 {
1278 gfc_expr *result;
1279
1280 result = gfc_constant_result (BT_LOGICAL, gfc_default_logical_kind,
1281 &op1->where);
1282 result->value.logical = (gfc_compare_expr (op1, op2) <= 0);
1283 *resultp = result;
1284
1285 return ARITH_OK;
1286 }
1287
1288
1289 static arith
1290 reduce_unary (arith (*eval) (gfc_expr *, gfc_expr **), gfc_expr * op,
1291 gfc_expr ** result)
1292 {
1293 gfc_constructor *c, *head;
1294 gfc_expr *r;
1295 arith rc;
1296
1297 if (op->expr_type == EXPR_CONSTANT)
1298 return eval (op, result);
1299
1300 rc = ARITH_OK;
1301 head = gfc_copy_constructor (op->value.constructor);
1302
1303 for (c = head; c; c = c->next)
1304 {
1305 rc = eval (c->expr, &r);
1306 if (rc != ARITH_OK)
1307 break;
1308
1309 gfc_replace_expr (c->expr, r);
1310 }
1311
1312 if (rc != ARITH_OK)
1313 gfc_free_constructor (head);
1314 else
1315 {
1316 r = gfc_get_expr ();
1317 r->expr_type = EXPR_ARRAY;
1318 r->value.constructor = head;
1319 r->shape = gfc_copy_shape (op->shape, op->rank);
1320
1321 r->ts = head->expr->ts;
1322 r->where = op->where;
1323 r->rank = op->rank;
1324
1325 *result = r;
1326 }
1327
1328 return rc;
1329 }
1330
1331
1332 static arith
1333 reduce_binary_ac (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1334 gfc_expr * op1, gfc_expr * op2,
1335 gfc_expr ** result)
1336 {
1337 gfc_constructor *c, *head;
1338 gfc_expr *r;
1339 arith rc;
1340
1341 head = gfc_copy_constructor (op1->value.constructor);
1342 rc = ARITH_OK;
1343
1344 for (c = head; c; c = c->next)
1345 {
1346 rc = eval (c->expr, op2, &r);
1347 if (rc != ARITH_OK)
1348 break;
1349
1350 gfc_replace_expr (c->expr, r);
1351 }
1352
1353 if (rc != ARITH_OK)
1354 gfc_free_constructor (head);
1355 else
1356 {
1357 r = gfc_get_expr ();
1358 r->expr_type = EXPR_ARRAY;
1359 r->value.constructor = head;
1360 r->shape = gfc_copy_shape (op1->shape, op1->rank);
1361
1362 r->ts = head->expr->ts;
1363 r->where = op1->where;
1364 r->rank = op1->rank;
1365
1366 *result = r;
1367 }
1368
1369 return rc;
1370 }
1371
1372
1373 static arith
1374 reduce_binary_ca (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1375 gfc_expr * op1, gfc_expr * op2,
1376 gfc_expr ** result)
1377 {
1378 gfc_constructor *c, *head;
1379 gfc_expr *r;
1380 arith rc;
1381
1382 head = gfc_copy_constructor (op2->value.constructor);
1383 rc = ARITH_OK;
1384
1385 for (c = head; c; c = c->next)
1386 {
1387 rc = eval (op1, c->expr, &r);
1388 if (rc != ARITH_OK)
1389 break;
1390
1391 gfc_replace_expr (c->expr, r);
1392 }
1393
1394 if (rc != ARITH_OK)
1395 gfc_free_constructor (head);
1396 else
1397 {
1398 r = gfc_get_expr ();
1399 r->expr_type = EXPR_ARRAY;
1400 r->value.constructor = head;
1401 r->shape = gfc_copy_shape (op2->shape, op2->rank);
1402
1403 r->ts = head->expr->ts;
1404 r->where = op2->where;
1405 r->rank = op2->rank;
1406
1407 *result = r;
1408 }
1409
1410 return rc;
1411 }
1412
1413
1414 static arith
1415 reduce_binary_aa (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1416 gfc_expr * op1, gfc_expr * op2,
1417 gfc_expr ** result)
1418 {
1419 gfc_constructor *c, *d, *head;
1420 gfc_expr *r;
1421 arith rc;
1422
1423 head = gfc_copy_constructor (op1->value.constructor);
1424
1425 rc = ARITH_OK;
1426 d = op2->value.constructor;
1427
1428 if (gfc_check_conformance ("Elemental binary operation", op1, op2)
1429 != SUCCESS)
1430 rc = ARITH_INCOMMENSURATE;
1431 else
1432 {
1433
1434 for (c = head; c; c = c->next, d = d->next)
1435 {
1436 if (d == NULL)
1437 {
1438 rc = ARITH_INCOMMENSURATE;
1439 break;
1440 }
1441
1442 rc = eval (c->expr, d->expr, &r);
1443 if (rc != ARITH_OK)
1444 break;
1445
1446 gfc_replace_expr (c->expr, r);
1447 }
1448
1449 if (d != NULL)
1450 rc = ARITH_INCOMMENSURATE;
1451 }
1452
1453 if (rc != ARITH_OK)
1454 gfc_free_constructor (head);
1455 else
1456 {
1457 r = gfc_get_expr ();
1458 r->expr_type = EXPR_ARRAY;
1459 r->value.constructor = head;
1460 r->shape = gfc_copy_shape (op1->shape, op1->rank);
1461
1462 r->ts = head->expr->ts;
1463 r->where = op1->where;
1464 r->rank = op1->rank;
1465
1466 *result = r;
1467 }
1468
1469 return rc;
1470 }
1471
1472
1473 static arith
1474 reduce_binary (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1475 gfc_expr * op1, gfc_expr * op2,
1476 gfc_expr ** result)
1477 {
1478 if (op1->expr_type == EXPR_CONSTANT && op2->expr_type == EXPR_CONSTANT)
1479 return eval (op1, op2, result);
1480
1481 if (op1->expr_type == EXPR_CONSTANT && op2->expr_type == EXPR_ARRAY)
1482 return reduce_binary_ca (eval, op1, op2, result);
1483
1484 if (op1->expr_type == EXPR_ARRAY && op2->expr_type == EXPR_CONSTANT)
1485 return reduce_binary_ac (eval, op1, op2, result);
1486
1487 return reduce_binary_aa (eval, op1, op2, result);
1488 }
1489
1490
1491 typedef union
1492 {
1493 arith (*f2)(gfc_expr *, gfc_expr **);
1494 arith (*f3)(gfc_expr *, gfc_expr *, gfc_expr **);
1495 }
1496 eval_f;
1497
1498 /* High level arithmetic subroutines. These subroutines go into
1499 eval_intrinsic(), which can do one of several things to its
1500 operands. If the operands are incompatible with the intrinsic
1501 operation, we return a node pointing to the operands and hope that
1502 an operator interface is found during resolution.
1503
1504 If the operands are compatible and are constants, then we try doing
1505 the arithmetic. We also handle the cases where either or both
1506 operands are array constructors. */
1507
1508 static gfc_expr *
1509 eval_intrinsic (gfc_intrinsic_op operator,
1510 eval_f eval, gfc_expr * op1, gfc_expr * op2)
1511 {
1512 gfc_expr temp, *result;
1513 int unary;
1514 arith rc;
1515
1516 gfc_clear_ts (&temp.ts);
1517
1518 switch (operator)
1519 {
1520 /* Logical unary */
1521 case INTRINSIC_NOT:
1522 if (op1->ts.type != BT_LOGICAL)
1523 goto runtime;
1524
1525 temp.ts.type = BT_LOGICAL;
1526 temp.ts.kind = gfc_default_logical_kind;
1527
1528 unary = 1;
1529 break;
1530
1531 /* Logical binary operators */
1532 case INTRINSIC_OR:
1533 case INTRINSIC_AND:
1534 case INTRINSIC_NEQV:
1535 case INTRINSIC_EQV:
1536 if (op1->ts.type != BT_LOGICAL || op2->ts.type != BT_LOGICAL)
1537 goto runtime;
1538
1539 temp.ts.type = BT_LOGICAL;
1540 temp.ts.kind = gfc_default_logical_kind;
1541
1542 unary = 0;
1543 break;
1544
1545 /* Numeric unary */
1546 case INTRINSIC_UPLUS:
1547 case INTRINSIC_UMINUS:
1548 if (!gfc_numeric_ts (&op1->ts))
1549 goto runtime;
1550
1551 temp.ts = op1->ts;
1552
1553 unary = 1;
1554 break;
1555
1556 case INTRINSIC_PARENTHESES:
1557 temp.ts = op1->ts;
1558
1559 unary = 1;
1560 break;
1561
1562 /* Additional restrictions for ordering relations. */
1563 case INTRINSIC_GE:
1564 case INTRINSIC_LT:
1565 case INTRINSIC_LE:
1566 case INTRINSIC_GT:
1567 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
1568 {
1569 temp.ts.type = BT_LOGICAL;
1570 temp.ts.kind = gfc_default_logical_kind;
1571 goto runtime;
1572 }
1573
1574 /* Fall through */
1575 case INTRINSIC_EQ:
1576 case INTRINSIC_NE:
1577 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER)
1578 {
1579 unary = 0;
1580 temp.ts.type = BT_LOGICAL;
1581 temp.ts.kind = gfc_default_logical_kind;
1582 break;
1583 }
1584
1585 /* Fall through */
1586 /* Numeric binary */
1587 case INTRINSIC_PLUS:
1588 case INTRINSIC_MINUS:
1589 case INTRINSIC_TIMES:
1590 case INTRINSIC_DIVIDE:
1591 case INTRINSIC_POWER:
1592 if (!gfc_numeric_ts (&op1->ts) || !gfc_numeric_ts (&op2->ts))
1593 goto runtime;
1594
1595 /* Insert any necessary type conversions to make the operands
1596 compatible. */
1597
1598 temp.expr_type = EXPR_OP;
1599 gfc_clear_ts (&temp.ts);
1600 temp.value.op.operator = operator;
1601
1602 temp.value.op.op1 = op1;
1603 temp.value.op.op2 = op2;
1604
1605 gfc_type_convert_binary (&temp);
1606
1607 if (operator == INTRINSIC_EQ || operator == INTRINSIC_NE
1608 || operator == INTRINSIC_GE || operator == INTRINSIC_GT
1609 || operator == INTRINSIC_LE || operator == INTRINSIC_LT)
1610 {
1611 temp.ts.type = BT_LOGICAL;
1612 temp.ts.kind = gfc_default_logical_kind;
1613 }
1614
1615 unary = 0;
1616 break;
1617
1618 /* Character binary */
1619 case INTRINSIC_CONCAT:
1620 if (op1->ts.type != BT_CHARACTER || op2->ts.type != BT_CHARACTER)
1621 goto runtime;
1622
1623 temp.ts.type = BT_CHARACTER;
1624 temp.ts.kind = gfc_default_character_kind;
1625
1626 unary = 0;
1627 break;
1628
1629 case INTRINSIC_USER:
1630 goto runtime;
1631
1632 default:
1633 gfc_internal_error ("eval_intrinsic(): Bad operator");
1634 }
1635
1636 /* Try to combine the operators. */
1637 if (operator == INTRINSIC_POWER && op2->ts.type != BT_INTEGER)
1638 goto runtime;
1639
1640 if (op1->from_H
1641 || (op1->expr_type != EXPR_CONSTANT
1642 && (op1->expr_type != EXPR_ARRAY
1643 || !gfc_is_constant_expr (op1)
1644 || !gfc_expanded_ac (op1))))
1645 goto runtime;
1646
1647 if (op2 != NULL
1648 && (op2->from_H
1649 || (op2->expr_type != EXPR_CONSTANT
1650 && (op2->expr_type != EXPR_ARRAY
1651 || !gfc_is_constant_expr (op2)
1652 || !gfc_expanded_ac (op2)))))
1653 goto runtime;
1654
1655 if (unary)
1656 rc = reduce_unary (eval.f2, op1, &result);
1657 else
1658 rc = reduce_binary (eval.f3, op1, op2, &result);
1659
1660 if (rc != ARITH_OK)
1661 { /* Something went wrong. */
1662 gfc_error (gfc_arith_error (rc), &op1->where);
1663 return NULL;
1664 }
1665
1666 gfc_free_expr (op1);
1667 gfc_free_expr (op2);
1668 return result;
1669
1670 runtime:
1671 /* Create a run-time expression. */
1672 result = gfc_get_expr ();
1673 result->ts = temp.ts;
1674
1675 result->expr_type = EXPR_OP;
1676 result->value.op.operator = operator;
1677
1678 result->value.op.op1 = op1;
1679 result->value.op.op2 = op2;
1680
1681 result->where = op1->where;
1682
1683 return result;
1684 }
1685
1686
1687 /* Modify type of expression for zero size array. */
1688
1689 static gfc_expr *
1690 eval_type_intrinsic0 (gfc_intrinsic_op operator, gfc_expr * op)
1691 {
1692 if (op == NULL)
1693 gfc_internal_error ("eval_type_intrinsic0(): op NULL");
1694
1695 switch (operator)
1696 {
1697 case INTRINSIC_GE:
1698 case INTRINSIC_LT:
1699 case INTRINSIC_LE:
1700 case INTRINSIC_GT:
1701 case INTRINSIC_EQ:
1702 case INTRINSIC_NE:
1703 op->ts.type = BT_LOGICAL;
1704 op->ts.kind = gfc_default_logical_kind;
1705 break;
1706
1707 default:
1708 break;
1709 }
1710
1711 return op;
1712 }
1713
1714
1715 /* Return nonzero if the expression is a zero size array. */
1716
1717 static int
1718 gfc_zero_size_array (gfc_expr * e)
1719 {
1720 if (e->expr_type != EXPR_ARRAY)
1721 return 0;
1722
1723 return e->value.constructor == NULL;
1724 }
1725
1726
1727 /* Reduce a binary expression where at least one of the operands
1728 involves a zero-length array. Returns NULL if neither of the
1729 operands is a zero-length array. */
1730
1731 static gfc_expr *
1732 reduce_binary0 (gfc_expr * op1, gfc_expr * op2)
1733 {
1734 if (gfc_zero_size_array (op1))
1735 {
1736 gfc_free_expr (op2);
1737 return op1;
1738 }
1739
1740 if (gfc_zero_size_array (op2))
1741 {
1742 gfc_free_expr (op1);
1743 return op2;
1744 }
1745
1746 return NULL;
1747 }
1748
1749
1750 static gfc_expr *
1751 eval_intrinsic_f2 (gfc_intrinsic_op operator,
1752 arith (*eval) (gfc_expr *, gfc_expr **),
1753 gfc_expr * op1, gfc_expr * op2)
1754 {
1755 gfc_expr *result;
1756 eval_f f;
1757
1758 if (op2 == NULL)
1759 {
1760 if (gfc_zero_size_array (op1))
1761 return eval_type_intrinsic0 (operator, op1);
1762 }
1763 else
1764 {
1765 result = reduce_binary0 (op1, op2);
1766 if (result != NULL)
1767 return eval_type_intrinsic0 (operator, result);
1768 }
1769
1770 f.f2 = eval;
1771 return eval_intrinsic (operator, f, op1, op2);
1772 }
1773
1774
1775 static gfc_expr *
1776 eval_intrinsic_f3 (gfc_intrinsic_op operator,
1777 arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
1778 gfc_expr * op1, gfc_expr * op2)
1779 {
1780 gfc_expr *result;
1781 eval_f f;
1782
1783 result = reduce_binary0 (op1, op2);
1784 if (result != NULL)
1785 return eval_type_intrinsic0(operator, result);
1786
1787 f.f3 = eval;
1788 return eval_intrinsic (operator, f, op1, op2);
1789 }
1790
1791
1792 gfc_expr *
1793 gfc_uplus (gfc_expr * op)
1794 {
1795 return eval_intrinsic_f2 (INTRINSIC_UPLUS, gfc_arith_uplus, op, NULL);
1796 }
1797
1798
1799 gfc_expr *
1800 gfc_uminus (gfc_expr * op)
1801 {
1802 return eval_intrinsic_f2 (INTRINSIC_UMINUS, gfc_arith_uminus, op, NULL);
1803 }
1804
1805
1806 gfc_expr *
1807 gfc_add (gfc_expr * op1, gfc_expr * op2)
1808 {
1809 return eval_intrinsic_f3 (INTRINSIC_PLUS, gfc_arith_plus, op1, op2);
1810 }
1811
1812
1813 gfc_expr *
1814 gfc_subtract (gfc_expr * op1, gfc_expr * op2)
1815 {
1816 return eval_intrinsic_f3 (INTRINSIC_MINUS, gfc_arith_minus, op1, op2);
1817 }
1818
1819
1820 gfc_expr *
1821 gfc_multiply (gfc_expr * op1, gfc_expr * op2)
1822 {
1823 return eval_intrinsic_f3 (INTRINSIC_TIMES, gfc_arith_times, op1, op2);
1824 }
1825
1826
1827 gfc_expr *
1828 gfc_divide (gfc_expr * op1, gfc_expr * op2)
1829 {
1830 return eval_intrinsic_f3 (INTRINSIC_DIVIDE, gfc_arith_divide, op1, op2);
1831 }
1832
1833
1834 gfc_expr *
1835 gfc_power (gfc_expr * op1, gfc_expr * op2)
1836 {
1837 return eval_intrinsic_f3 (INTRINSIC_POWER, gfc_arith_power, op1, op2);
1838 }
1839
1840
1841 gfc_expr *
1842 gfc_concat (gfc_expr * op1, gfc_expr * op2)
1843 {
1844 return eval_intrinsic_f3 (INTRINSIC_CONCAT, gfc_arith_concat, op1, op2);
1845 }
1846
1847
1848 gfc_expr *
1849 gfc_and (gfc_expr * op1, gfc_expr * op2)
1850 {
1851 return eval_intrinsic_f3 (INTRINSIC_AND, gfc_arith_and, op1, op2);
1852 }
1853
1854
1855 gfc_expr *
1856 gfc_or (gfc_expr * op1, gfc_expr * op2)
1857 {
1858 return eval_intrinsic_f3 (INTRINSIC_OR, gfc_arith_or, op1, op2);
1859 }
1860
1861
1862 gfc_expr *
1863 gfc_not (gfc_expr * op1)
1864 {
1865 return eval_intrinsic_f2 (INTRINSIC_NOT, gfc_arith_not, op1, NULL);
1866 }
1867
1868
1869 gfc_expr *
1870 gfc_eqv (gfc_expr * op1, gfc_expr * op2)
1871 {
1872 return eval_intrinsic_f3 (INTRINSIC_EQV, gfc_arith_eqv, op1, op2);
1873 }
1874
1875
1876 gfc_expr *
1877 gfc_neqv (gfc_expr * op1, gfc_expr * op2)
1878 {
1879 return eval_intrinsic_f3 (INTRINSIC_NEQV, gfc_arith_neqv, op1, op2);
1880 }
1881
1882
1883 gfc_expr *
1884 gfc_eq (gfc_expr * op1, gfc_expr * op2)
1885 {
1886 return eval_intrinsic_f3 (INTRINSIC_EQ, gfc_arith_eq, op1, op2);
1887 }
1888
1889
1890 gfc_expr *
1891 gfc_ne (gfc_expr * op1, gfc_expr * op2)
1892 {
1893 return eval_intrinsic_f3 (INTRINSIC_NE, gfc_arith_ne, op1, op2);
1894 }
1895
1896
1897 gfc_expr *
1898 gfc_gt (gfc_expr * op1, gfc_expr * op2)
1899 {
1900 return eval_intrinsic_f3 (INTRINSIC_GT, gfc_arith_gt, op1, op2);
1901 }
1902
1903
1904 gfc_expr *
1905 gfc_ge (gfc_expr * op1, gfc_expr * op2)
1906 {
1907 return eval_intrinsic_f3 (INTRINSIC_GE, gfc_arith_ge, op1, op2);
1908 }
1909
1910
1911 gfc_expr *
1912 gfc_lt (gfc_expr * op1, gfc_expr * op2)
1913 {
1914 return eval_intrinsic_f3 (INTRINSIC_LT, gfc_arith_lt, op1, op2);
1915 }
1916
1917
1918 gfc_expr *
1919 gfc_le (gfc_expr * op1, gfc_expr * op2)
1920 {
1921 return eval_intrinsic_f3 (INTRINSIC_LE, gfc_arith_le, op1, op2);
1922 }
1923
1924
1925 /* Convert an integer string to an expression node. */
1926
1927 gfc_expr *
1928 gfc_convert_integer (const char * buffer, int kind, int radix, locus * where)
1929 {
1930 gfc_expr *e;
1931 const char *t;
1932
1933 e = gfc_constant_result (BT_INTEGER, kind, where);
1934 /* A leading plus is allowed, but not by mpz_set_str. */
1935 if (buffer[0] == '+')
1936 t = buffer + 1;
1937 else
1938 t = buffer;
1939 mpz_set_str (e->value.integer, t, radix);
1940
1941 return e;
1942 }
1943
1944
1945 /* Convert a real string to an expression node. */
1946
1947 gfc_expr *
1948 gfc_convert_real (const char * buffer, int kind, locus * where)
1949 {
1950 gfc_expr *e;
1951
1952 e = gfc_constant_result (BT_REAL, kind, where);
1953 mpfr_set_str (e->value.real, buffer, 10, GFC_RND_MODE);
1954
1955 return e;
1956 }
1957
1958
1959 /* Convert a pair of real, constant expression nodes to a single
1960 complex expression node. */
1961
1962 gfc_expr *
1963 gfc_convert_complex (gfc_expr * real, gfc_expr * imag, int kind)
1964 {
1965 gfc_expr *e;
1966
1967 e = gfc_constant_result (BT_COMPLEX, kind, &real->where);
1968 mpfr_set (e->value.complex.r, real->value.real, GFC_RND_MODE);
1969 mpfr_set (e->value.complex.i, imag->value.real, GFC_RND_MODE);
1970
1971 return e;
1972 }
1973
1974
1975 /******* Simplification of intrinsic functions with constant arguments *****/
1976
1977
1978 /* Deal with an arithmetic error. */
1979
1980 static void
1981 arith_error (arith rc, gfc_typespec * from, gfc_typespec * to, locus * where)
1982 {
1983 switch (rc)
1984 {
1985 case ARITH_OK:
1986 gfc_error ("Arithmetic OK converting %s to %s at %L",
1987 gfc_typename (from), gfc_typename (to), where);
1988 break;
1989 case ARITH_OVERFLOW:
1990 gfc_error ("Arithmetic overflow converting %s to %s at %L",
1991 gfc_typename (from), gfc_typename (to), where);
1992 break;
1993 case ARITH_UNDERFLOW:
1994 gfc_error ("Arithmetic underflow converting %s to %s at %L",
1995 gfc_typename (from), gfc_typename (to), where);
1996 break;
1997 case ARITH_NAN:
1998 gfc_error ("Arithmetic NaN converting %s to %s at %L",
1999 gfc_typename (from), gfc_typename (to), where);
2000 break;
2001 case ARITH_DIV0:
2002 gfc_error ("Division by zero converting %s to %s at %L",
2003 gfc_typename (from), gfc_typename (to), where);
2004 break;
2005 case ARITH_INCOMMENSURATE:
2006 gfc_error ("Array operands are incommensurate converting %s to %s at %L",
2007 gfc_typename (from), gfc_typename (to), where);
2008 break;
2009 case ARITH_ASYMMETRIC:
2010 gfc_error ("Integer outside symmetric range implied by Standard Fortran"
2011 " converting %s to %s at %L",
2012 gfc_typename (from), gfc_typename (to), where);
2013 break;
2014 default:
2015 gfc_internal_error ("gfc_arith_error(): Bad error code");
2016 }
2017
2018 /* TODO: Do something about the error, ie, throw exception, return
2019 NaN, etc. */
2020 }
2021
2022
2023 /* Convert integers to integers. */
2024
2025 gfc_expr *
2026 gfc_int2int (gfc_expr * src, int kind)
2027 {
2028 gfc_expr *result;
2029 arith rc;
2030
2031 result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2032
2033 mpz_set (result->value.integer, src->value.integer);
2034
2035 if ((rc = gfc_check_integer_range (result->value.integer, kind))
2036 != ARITH_OK)
2037 {
2038 if (rc == ARITH_ASYMMETRIC)
2039 {
2040 gfc_warning (gfc_arith_error (rc), &src->where);
2041 }
2042 else
2043 {
2044 arith_error (rc, &src->ts, &result->ts, &src->where);
2045 gfc_free_expr (result);
2046 return NULL;
2047 }
2048 }
2049
2050 return result;
2051 }
2052
2053
2054 /* Convert integers to reals. */
2055
2056 gfc_expr *
2057 gfc_int2real (gfc_expr * src, int kind)
2058 {
2059 gfc_expr *result;
2060 arith rc;
2061
2062 result = gfc_constant_result (BT_REAL, kind, &src->where);
2063
2064 mpfr_set_z (result->value.real, src->value.integer, GFC_RND_MODE);
2065
2066 if ((rc = gfc_check_real_range (result->value.real, kind)) != ARITH_OK)
2067 {
2068 arith_error (rc, &src->ts, &result->ts, &src->where);
2069 gfc_free_expr (result);
2070 return NULL;
2071 }
2072
2073 return result;
2074 }
2075
2076
2077 /* Convert default integer to default complex. */
2078
2079 gfc_expr *
2080 gfc_int2complex (gfc_expr * src, int kind)
2081 {
2082 gfc_expr *result;
2083 arith rc;
2084
2085 result = gfc_constant_result (BT_COMPLEX, kind, &src->where);
2086
2087 mpfr_set_z (result->value.complex.r, src->value.integer, GFC_RND_MODE);
2088 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
2089
2090 if ((rc = gfc_check_real_range (result->value.complex.r, kind)) != ARITH_OK)
2091 {
2092 arith_error (rc, &src->ts, &result->ts, &src->where);
2093 gfc_free_expr (result);
2094 return NULL;
2095 }
2096
2097 return result;
2098 }
2099
2100
2101 /* Convert default real to default integer. */
2102
2103 gfc_expr *
2104 gfc_real2int (gfc_expr * src, int kind)
2105 {
2106 gfc_expr *result;
2107 arith rc;
2108
2109 result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2110
2111 gfc_mpfr_to_mpz (result->value.integer, src->value.real);
2112
2113 if ((rc = gfc_check_integer_range (result->value.integer, kind))
2114 != ARITH_OK)
2115 {
2116 arith_error (rc, &src->ts, &result->ts, &src->where);
2117 gfc_free_expr (result);
2118 return NULL;
2119 }
2120
2121 return result;
2122 }
2123
2124
2125 /* Convert real to real. */
2126
2127 gfc_expr *
2128 gfc_real2real (gfc_expr * src, int kind)
2129 {
2130 gfc_expr *result;
2131 arith rc;
2132
2133 result = gfc_constant_result (BT_REAL, kind, &src->where);
2134
2135 mpfr_set (result->value.real, src->value.real, GFC_RND_MODE);
2136
2137 rc = gfc_check_real_range (result->value.real, kind);
2138
2139 if (rc == ARITH_UNDERFLOW)
2140 {
2141 if (gfc_option.warn_underflow)
2142 gfc_warning (gfc_arith_error (rc), &src->where);
2143 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
2144 }
2145 else if (rc != ARITH_OK)
2146 {
2147 arith_error (rc, &src->ts, &result->ts, &src->where);
2148 gfc_free_expr (result);
2149 return NULL;
2150 }
2151
2152 return result;
2153 }
2154
2155
2156 /* Convert real to complex. */
2157
2158 gfc_expr *
2159 gfc_real2complex (gfc_expr * src, int kind)
2160 {
2161 gfc_expr *result;
2162 arith rc;
2163
2164 result = gfc_constant_result (BT_COMPLEX, kind, &src->where);
2165
2166 mpfr_set (result->value.complex.r, src->value.real, GFC_RND_MODE);
2167 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
2168
2169 rc = gfc_check_real_range (result->value.complex.r, kind);
2170
2171 if (rc == ARITH_UNDERFLOW)
2172 {
2173 if (gfc_option.warn_underflow)
2174 gfc_warning (gfc_arith_error (rc), &src->where);
2175 mpfr_set_ui (result->value.complex.r, 0, GFC_RND_MODE);
2176 }
2177 else if (rc != ARITH_OK)
2178 {
2179 arith_error (rc, &src->ts, &result->ts, &src->where);
2180 gfc_free_expr (result);
2181 return NULL;
2182 }
2183
2184 return result;
2185 }
2186
2187
2188 /* Convert complex to integer. */
2189
2190 gfc_expr *
2191 gfc_complex2int (gfc_expr * src, int kind)
2192 {
2193 gfc_expr *result;
2194 arith rc;
2195
2196 result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2197
2198 gfc_mpfr_to_mpz (result->value.integer, src->value.complex.r);
2199
2200 if ((rc = gfc_check_integer_range (result->value.integer, kind))
2201 != ARITH_OK)
2202 {
2203 arith_error (rc, &src->ts, &result->ts, &src->where);
2204 gfc_free_expr (result);
2205 return NULL;
2206 }
2207
2208 return result;
2209 }
2210
2211
2212 /* Convert complex to real. */
2213
2214 gfc_expr *
2215 gfc_complex2real (gfc_expr * src, int kind)
2216 {
2217 gfc_expr *result;
2218 arith rc;
2219
2220 result = gfc_constant_result (BT_REAL, kind, &src->where);
2221
2222 mpfr_set (result->value.real, src->value.complex.r, GFC_RND_MODE);
2223
2224 rc = gfc_check_real_range (result->value.real, kind);
2225
2226 if (rc == ARITH_UNDERFLOW)
2227 {
2228 if (gfc_option.warn_underflow)
2229 gfc_warning (gfc_arith_error (rc), &src->where);
2230 mpfr_set_ui (result->value.real, 0, GFC_RND_MODE);
2231 }
2232 if (rc != ARITH_OK)
2233 {
2234 arith_error (rc, &src->ts, &result->ts, &src->where);
2235 gfc_free_expr (result);
2236 return NULL;
2237 }
2238
2239 return result;
2240 }
2241
2242
2243 /* Convert complex to complex. */
2244
2245 gfc_expr *
2246 gfc_complex2complex (gfc_expr * src, int kind)
2247 {
2248 gfc_expr *result;
2249 arith rc;
2250
2251 result = gfc_constant_result (BT_COMPLEX, kind, &src->where);
2252
2253 mpfr_set (result->value.complex.r, src->value.complex.r, GFC_RND_MODE);
2254 mpfr_set (result->value.complex.i, src->value.complex.i, GFC_RND_MODE);
2255
2256 rc = gfc_check_real_range (result->value.complex.r, kind);
2257
2258 if (rc == ARITH_UNDERFLOW)
2259 {
2260 if (gfc_option.warn_underflow)
2261 gfc_warning (gfc_arith_error (rc), &src->where);
2262 mpfr_set_ui (result->value.complex.r, 0, GFC_RND_MODE);
2263 }
2264 else if (rc != ARITH_OK)
2265 {
2266 arith_error (rc, &src->ts, &result->ts, &src->where);
2267 gfc_free_expr (result);
2268 return NULL;
2269 }
2270
2271 rc = gfc_check_real_range (result->value.complex.i, kind);
2272
2273 if (rc == ARITH_UNDERFLOW)
2274 {
2275 if (gfc_option.warn_underflow)
2276 gfc_warning (gfc_arith_error (rc), &src->where);
2277 mpfr_set_ui (result->value.complex.i, 0, GFC_RND_MODE);
2278 }
2279 else if (rc != ARITH_OK)
2280 {
2281 arith_error (rc, &src->ts, &result->ts, &src->where);
2282 gfc_free_expr (result);
2283 return NULL;
2284 }
2285
2286 return result;
2287 }
2288
2289
2290 /* Logical kind conversion. */
2291
2292 gfc_expr *
2293 gfc_log2log (gfc_expr * src, int kind)
2294 {
2295 gfc_expr *result;
2296
2297 result = gfc_constant_result (BT_LOGICAL, kind, &src->where);
2298 result->value.logical = src->value.logical;
2299
2300 return result;
2301 }
2302
2303
2304 /* Convert logical to integer. */
2305
2306 gfc_expr *
2307 gfc_log2int (gfc_expr *src, int kind)
2308 {
2309 gfc_expr *result;
2310
2311 result = gfc_constant_result (BT_INTEGER, kind, &src->where);
2312 mpz_set_si (result->value.integer, src->value.logical);
2313
2314 return result;
2315 }
2316
2317
2318 /* Convert integer to logical. */
2319
2320 gfc_expr *
2321 gfc_int2log (gfc_expr *src, int kind)
2322 {
2323 gfc_expr *result;
2324
2325 result = gfc_constant_result (BT_LOGICAL, kind, &src->where);
2326 result->value.logical = (mpz_cmp_si (src->value.integer, 0) != 0);
2327
2328 return result;
2329 }
2330
2331
2332 /* Convert Hollerith to integer. The constant will be padded or truncated. */
2333
2334 gfc_expr *
2335 gfc_hollerith2int (gfc_expr * src, int kind)
2336 {
2337 gfc_expr *result;
2338 int len;
2339
2340 len = src->value.character.length;
2341
2342 result = gfc_get_expr ();
2343 result->expr_type = EXPR_CONSTANT;
2344 result->ts.type = BT_INTEGER;
2345 result->ts.kind = kind;
2346 result->where = src->where;
2347 result->from_H = 1;
2348
2349 if (len > kind)
2350 {
2351 gfc_warning ("The Hollerith constant at %L is too long to convert to %s",
2352 &src->where, gfc_typename(&result->ts));
2353 }
2354 result->value.character.string = gfc_getmem (kind + 1);
2355 memcpy (result->value.character.string, src->value.character.string,
2356 MIN (kind, len));
2357
2358 if (len < kind)
2359 memset (&result->value.character.string[len], ' ', kind - len);
2360
2361 result->value.character.string[kind] = '\0'; /* For debugger */
2362 result->value.character.length = kind;
2363
2364 return result;
2365 }
2366
2367
2368 /* Convert Hollerith to real. The constant will be padded or truncated. */
2369
2370 gfc_expr *
2371 gfc_hollerith2real (gfc_expr * src, int kind)
2372 {
2373 gfc_expr *result;
2374 int len;
2375
2376 len = src->value.character.length;
2377
2378 result = gfc_get_expr ();
2379 result->expr_type = EXPR_CONSTANT;
2380 result->ts.type = BT_REAL;
2381 result->ts.kind = kind;
2382 result->where = src->where;
2383 result->from_H = 1;
2384
2385 if (len > kind)
2386 {
2387 gfc_warning ("The Hollerith constant at %L is too long to convert to %s",
2388 &src->where, gfc_typename(&result->ts));
2389 }
2390 result->value.character.string = gfc_getmem (kind + 1);
2391 memcpy (result->value.character.string, src->value.character.string,
2392 MIN (kind, len));
2393
2394 if (len < kind)
2395 memset (&result->value.character.string[len], ' ', kind - len);
2396
2397 result->value.character.string[kind] = '\0'; /* For debugger. */
2398 result->value.character.length = kind;
2399
2400 return result;
2401 }
2402
2403
2404 /* Convert Hollerith to complex. The constant will be padded or truncated. */
2405
2406 gfc_expr *
2407 gfc_hollerith2complex (gfc_expr * src, int kind)
2408 {
2409 gfc_expr *result;
2410 int len;
2411
2412 len = src->value.character.length;
2413
2414 result = gfc_get_expr ();
2415 result->expr_type = EXPR_CONSTANT;
2416 result->ts.type = BT_COMPLEX;
2417 result->ts.kind = kind;
2418 result->where = src->where;
2419 result->from_H = 1;
2420
2421 kind = kind * 2;
2422
2423 if (len > kind)
2424 {
2425 gfc_warning ("The Hollerith constant at %L is too long to convert to %s",
2426 &src->where, gfc_typename(&result->ts));
2427 }
2428 result->value.character.string = gfc_getmem (kind + 1);
2429 memcpy (result->value.character.string, src->value.character.string,
2430 MIN (kind, len));
2431
2432 if (len < kind)
2433 memset (&result->value.character.string[len], ' ', kind - len);
2434
2435 result->value.character.string[kind] = '\0'; /* For debugger */
2436 result->value.character.length = kind;
2437
2438 return result;
2439 }
2440
2441
2442 /* Convert Hollerith to character. */
2443
2444 gfc_expr *
2445 gfc_hollerith2character (gfc_expr * src, int kind)
2446 {
2447 gfc_expr *result;
2448
2449 result = gfc_copy_expr (src);
2450 result->ts.type = BT_CHARACTER;
2451 result->ts.kind = kind;
2452 result->from_H = 1;
2453
2454 return result;
2455 }
2456
2457
2458 /* Convert Hollerith to logical. The constant will be padded or truncated. */
2459
2460 gfc_expr *
2461 gfc_hollerith2logical (gfc_expr * src, int kind)
2462 {
2463 gfc_expr *result;
2464 int len;
2465
2466 len = src->value.character.length;
2467
2468 result = gfc_get_expr ();
2469 result->expr_type = EXPR_CONSTANT;
2470 result->ts.type = BT_LOGICAL;
2471 result->ts.kind = kind;
2472 result->where = src->where;
2473 result->from_H = 1;
2474
2475 if (len > kind)
2476 {
2477 gfc_warning ("The Hollerith constant at %L is too long to convert to %s",
2478 &src->where, gfc_typename(&result->ts));
2479 }
2480 result->value.character.string = gfc_getmem (kind + 1);
2481 memcpy (result->value.character.string, src->value.character.string,
2482 MIN (kind, len));
2483
2484 if (len < kind)
2485 memset (&result->value.character.string[len], ' ', kind - len);
2486
2487 result->value.character.string[kind] = '\0'; /* For debugger */
2488 result->value.character.length = kind;
2489
2490 return result;
2491 }
2492
2493
2494 /* Returns an initializer whose value is one higher than the value of the
2495 LAST_INITIALIZER argument. If the argument is NULL, the
2496 initializers value will be set to zero. The initializer's kind
2497 will be set to gfc_c_int_kind.
2498
2499 If -fshort-enums is given, the appropriate kind will be selected
2500 later after all enumerators have been parsed. A warning is issued
2501 here if an initializer exceeds gfc_c_int_kind. */
2502
2503 gfc_expr *
2504 gfc_enum_initializer (gfc_expr * last_initializer, locus where)
2505 {
2506 gfc_expr *result;
2507
2508 result = gfc_get_expr ();
2509 result->expr_type = EXPR_CONSTANT;
2510 result->ts.type = BT_INTEGER;
2511 result->ts.kind = gfc_c_int_kind;
2512 result->where = where;
2513
2514 mpz_init (result->value.integer);
2515
2516 if (last_initializer != NULL)
2517 {
2518 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
2519 result->where = last_initializer->where;
2520
2521 if (gfc_check_integer_range (result->value.integer,
2522 gfc_c_int_kind) != ARITH_OK)
2523 {
2524 gfc_error ("Enumerator exceeds the C integer type at %C");
2525 return NULL;
2526 }
2527 }
2528 else
2529 {
2530 /* Control comes here, if it's the very first enumerator and no
2531 initializer has been given. It will be initialized to zero. */
2532 mpz_set_si (result->value.integer, 0);
2533 }
2534
2535 return result;
2536 }
This page took 0.144979 seconds and 4 git commands to generate.