2015-05-22 Pascal Obry <obry@adacore.com>
[official-gcc.git] / gcc / fortran / trans-const.c
blob764634495220d082c8b9f1355537c5f9b959ba20
1 /* Translation of constants
2 Copyright (C) 2002-2015 Free Software Foundation, Inc.
3 Contributed by Paul Brook
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* trans-const.c -- convert constant values */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "gfortran.h"
27 #include "hash-set.h"
28 #include "machmode.h"
29 #include "vec.h"
30 #include "double-int.h"
31 #include "input.h"
32 #include "alias.h"
33 #include "symtab.h"
34 #include "options.h"
35 #include "real.h"
36 #include "wide-int.h"
37 #include "inchash.h"
38 #include "tree.h"
39 #include "fold-const.h"
40 #include "stor-layout.h"
41 #include "realmpfr.h"
42 #include "diagnostic-core.h" /* For fatal_error. */
43 #include "double-int.h"
44 #include "trans.h"
45 #include "trans-const.h"
46 #include "trans-types.h"
47 #include "target-memory.h"
49 tree gfc_rank_cst[GFC_MAX_DIMENSIONS + 1];
51 /* Build a constant with given type from an int_cst. */
53 tree
54 gfc_build_const (tree type, tree intval)
56 tree val;
57 tree zero;
59 switch (TREE_CODE (type))
61 case INTEGER_TYPE:
62 val = convert (type, intval);
63 break;
65 case REAL_TYPE:
66 val = build_real_from_int_cst (type, intval);
67 break;
69 case COMPLEX_TYPE:
70 val = build_real_from_int_cst (TREE_TYPE (type), intval);
71 zero = build_real_from_int_cst (TREE_TYPE (type), integer_zero_node);
72 val = build_complex (type, val, zero);
73 break;
75 default:
76 gcc_unreachable ();
78 return val;
81 /* Build a string constant with C char type. */
83 tree
84 gfc_build_string_const (int length, const char *s)
86 tree str;
87 tree len;
89 str = build_string (length, s);
90 len = size_int (length);
91 TREE_TYPE (str) =
92 build_array_type (gfc_character1_type_node,
93 build_range_type (gfc_charlen_type_node,
94 size_one_node, len));
95 TYPE_STRING_FLAG (TREE_TYPE (str)) = 1;
96 return str;
100 /* Build a string constant with a type given by its kind; take care of
101 non-default character kinds. */
103 tree
104 gfc_build_wide_string_const (int kind, int length, const gfc_char_t *string)
106 int i;
107 tree str, len;
108 size_t size;
109 char *s;
111 i = gfc_validate_kind (BT_CHARACTER, kind, false);
112 size = length * gfc_character_kinds[i].bit_size / 8;
114 s = XCNEWVAR (char, size);
115 gfc_encode_character (kind, length, string, (unsigned char *) s, size);
117 str = build_string (size, s);
118 free (s);
120 len = size_int (length);
121 TREE_TYPE (str) =
122 build_array_type (gfc_get_char_type (kind),
123 build_range_type (gfc_charlen_type_node,
124 size_one_node, len));
125 TYPE_STRING_FLAG (TREE_TYPE (str)) = 1;
126 return str;
130 /* Build a Fortran character constant from a zero-terminated string.
131 There a two version of this function, one that translates the string
132 and one that doesn't. */
133 tree
134 gfc_build_cstring_const (const char *string)
136 return gfc_build_string_const (strlen (string) + 1, string);
139 tree
140 gfc_build_localized_cstring_const (const char *msgid)
142 const char *localized = _(msgid);
143 return gfc_build_string_const (strlen (localized) + 1, localized);
147 /* Return a string constant with the given length. Used for static
148 initializers. The constant will be padded or truncated to match
149 length. */
151 tree
152 gfc_conv_string_init (tree length, gfc_expr * expr)
154 gfc_char_t *s;
155 HOST_WIDE_INT len;
156 int slen;
157 tree str;
158 bool free_s = false;
160 gcc_assert (expr->expr_type == EXPR_CONSTANT);
161 gcc_assert (expr->ts.type == BT_CHARACTER);
162 gcc_assert (tree_fits_uhwi_p (length));
164 len = TREE_INT_CST_LOW (length);
165 slen = expr->value.character.length;
167 if (len > slen)
169 s = gfc_get_wide_string (len);
170 memcpy (s, expr->value.character.string, slen * sizeof (gfc_char_t));
171 gfc_wide_memset (&s[slen], ' ', len - slen);
172 free_s = true;
174 else
175 s = expr->value.character.string;
177 str = gfc_build_wide_string_const (expr->ts.kind, len, s);
179 if (free_s)
180 free (s);
182 return str;
186 /* Create a tree node for the string length if it is constant. */
188 void
189 gfc_conv_const_charlen (gfc_charlen * cl)
191 if (!cl || cl->backend_decl)
192 return;
194 if (cl->length && cl->length->expr_type == EXPR_CONSTANT)
196 cl->backend_decl = gfc_conv_mpz_to_tree (cl->length->value.integer,
197 cl->length->ts.kind);
198 cl->backend_decl = fold_convert (gfc_charlen_type_node,
199 cl->backend_decl);
203 void
204 gfc_init_constants (void)
206 int n;
208 for (n = 0; n <= GFC_MAX_DIMENSIONS; n++)
209 gfc_rank_cst[n] = build_int_cst (gfc_array_index_type, n);
212 /* Converts a GMP integer into a backend tree node. */
214 tree
215 gfc_conv_mpz_to_tree (mpz_t i, int kind)
217 wide_int val = wi::from_mpz (gfc_get_int_type (kind), i, true);
218 return wide_int_to_tree (gfc_get_int_type (kind), val);
221 /* Converts a backend tree into a GMP integer. */
223 void
224 gfc_conv_tree_to_mpz (mpz_t i, tree source)
226 wi::to_mpz (source, i, TYPE_SIGN (TREE_TYPE (source)));
229 /* Converts a real constant into backend form. */
231 tree
232 gfc_conv_mpfr_to_tree (mpfr_t f, int kind, int is_snan)
234 tree type;
235 int n;
236 REAL_VALUE_TYPE real;
238 n = gfc_validate_kind (BT_REAL, kind, false);
239 gcc_assert (gfc_real_kinds[n].radix == 2);
241 type = gfc_get_real_type (kind);
242 if (mpfr_nan_p (f) && is_snan)
243 real_from_string (&real, "SNaN");
244 else
245 real_from_mpfr (&real, f, type, GFC_RND_MODE);
247 return build_real (type, real);
250 /* Returns a real constant that is +Infinity if the target
251 supports infinities for this floating-point mode, and
252 +HUGE_VAL otherwise (the largest representable number). */
254 tree
255 gfc_build_inf_or_huge (tree type, int kind)
257 if (HONOR_INFINITIES (TYPE_MODE (type)))
259 REAL_VALUE_TYPE real;
260 real_inf (&real);
261 return build_real (type, real);
263 else
265 int k = gfc_validate_kind (BT_REAL, kind, false);
266 return gfc_conv_mpfr_to_tree (gfc_real_kinds[k].huge, kind, 0);
270 /* Returns a floating-point NaN of a given type. */
272 tree
273 gfc_build_nan (tree type, const char *str)
275 REAL_VALUE_TYPE real;
276 real_nan (&real, str, 1, TYPE_MODE (type));
277 return build_real (type, real);
280 /* Converts a backend tree into a real constant. */
282 void
283 gfc_conv_tree_to_mpfr (mpfr_ptr f, tree source)
285 mpfr_from_real (f, TREE_REAL_CST_PTR (source), GFC_RND_MODE);
288 /* Translate any literal constant to a tree. Constants never have
289 pre or post chains. Character literal constants are special
290 special because they have a value and a length, so they cannot be
291 returned as a single tree. It is up to the caller to set the
292 length somewhere if necessary.
294 Returns the translated constant, or aborts if it gets a type it
295 can't handle. */
297 tree
298 gfc_conv_constant_to_tree (gfc_expr * expr)
300 tree res;
302 gcc_assert (expr->expr_type == EXPR_CONSTANT);
304 /* If it is has a prescribed memory representation, we build a string
305 constant and VIEW_CONVERT to its type. */
307 switch (expr->ts.type)
309 case BT_INTEGER:
310 if (expr->representation.string)
311 return fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
312 gfc_get_int_type (expr->ts.kind),
313 gfc_build_string_const (expr->representation.length,
314 expr->representation.string));
315 else
316 return gfc_conv_mpz_to_tree (expr->value.integer, expr->ts.kind);
318 case BT_REAL:
319 if (expr->representation.string)
320 return fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
321 gfc_get_real_type (expr->ts.kind),
322 gfc_build_string_const (expr->representation.length,
323 expr->representation.string));
324 else
325 return gfc_conv_mpfr_to_tree (expr->value.real, expr->ts.kind, expr->is_snan);
327 case BT_LOGICAL:
328 if (expr->representation.string)
330 tree tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
331 gfc_get_int_type (expr->ts.kind),
332 gfc_build_string_const (expr->representation.length,
333 expr->representation.string));
334 if (!integer_zerop (tmp) && !integer_onep (tmp))
335 gfc_warning (0, "Assigning value other than 0 or 1 to LOGICAL"
336 " has undefined result at %L", &expr->where);
337 return fold_convert (gfc_get_logical_type (expr->ts.kind), tmp);
339 else
340 return build_int_cst (gfc_get_logical_type (expr->ts.kind),
341 expr->value.logical);
343 case BT_COMPLEX:
344 if (expr->representation.string)
345 return fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
346 gfc_get_complex_type (expr->ts.kind),
347 gfc_build_string_const (expr->representation.length,
348 expr->representation.string));
349 else
351 tree real = gfc_conv_mpfr_to_tree (mpc_realref (expr->value.complex),
352 expr->ts.kind, expr->is_snan);
353 tree imag = gfc_conv_mpfr_to_tree (mpc_imagref (expr->value.complex),
354 expr->ts.kind, expr->is_snan);
356 return build_complex (gfc_typenode_for_spec (&expr->ts),
357 real, imag);
360 case BT_CHARACTER:
361 res = gfc_build_wide_string_const (expr->ts.kind,
362 expr->value.character.length,
363 expr->value.character.string);
364 return res;
366 case BT_HOLLERITH:
367 return gfc_build_string_const (expr->representation.length,
368 expr->representation.string);
370 default:
371 fatal_error (input_location,
372 "gfc_conv_constant_to_tree(): invalid type: %s",
373 gfc_typename (&expr->ts));
378 /* Like gfc_conv_constant_to_tree, but for a simplified expression.
379 We can handle character literal constants here as well. */
381 void
382 gfc_conv_constant (gfc_se * se, gfc_expr * expr)
384 gfc_ss *ss;
386 /* We may be receiving an expression for C_NULL_PTR or C_NULL_FUNPTR. If
387 so, the expr_type will not yet be an EXPR_CONSTANT. We need to make
388 it so here. */
389 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
390 && expr->ts.u.derived->attr.is_iso_c)
392 if (expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_PTR
393 || expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_FUNPTR)
395 /* Create a new EXPR_CONSTANT expression for our local uses. */
396 expr = gfc_get_int_expr (gfc_default_integer_kind, NULL, 0);
400 if (expr->expr_type != EXPR_CONSTANT)
402 gfc_expr *e = gfc_get_int_expr (gfc_default_integer_kind, NULL, 0);
403 gfc_error ("non-constant initialization expression at %L", &expr->where);
404 se->expr = gfc_conv_constant_to_tree (e);
405 return;
408 ss = se->ss;
409 if (ss != NULL)
411 gfc_ss_info *ss_info;
413 ss_info = ss->info;
414 gcc_assert (ss != gfc_ss_terminator);
415 gcc_assert (ss_info->type == GFC_SS_SCALAR);
416 gcc_assert (ss_info->expr == expr);
418 se->expr = ss_info->data.scalar.value;
419 se->string_length = ss_info->string_length;
420 gfc_advance_se_ss_chain (se);
421 return;
424 /* Translate the constant and put it in the simplifier structure. */
425 se->expr = gfc_conv_constant_to_tree (expr);
427 /* If this is a CHARACTER string, set its length in the simplifier
428 structure, too. */
429 if (expr->ts.type == BT_CHARACTER)
430 se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));