2014-11-20 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / fortran / trans-const.c
blob99a18328be13210839523a5503f12e48da7f40ae
1 /* Translation of constants
2 Copyright (C) 2002-2014 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 "tree.h"
28 #include "stor-layout.h"
29 #include "realmpfr.h"
30 #include "diagnostic-core.h" /* For fatal_error. */
31 #include "double-int.h"
32 #include "trans.h"
33 #include "trans-const.h"
34 #include "trans-types.h"
35 #include "target-memory.h"
36 #include "wide-int.h"
38 tree gfc_rank_cst[GFC_MAX_DIMENSIONS + 1];
40 /* Build a constant with given type from an int_cst. */
42 tree
43 gfc_build_const (tree type, tree intval)
45 tree val;
46 tree zero;
48 switch (TREE_CODE (type))
50 case INTEGER_TYPE:
51 val = convert (type, intval);
52 break;
54 case REAL_TYPE:
55 val = build_real_from_int_cst (type, intval);
56 break;
58 case COMPLEX_TYPE:
59 val = build_real_from_int_cst (TREE_TYPE (type), intval);
60 zero = build_real_from_int_cst (TREE_TYPE (type), integer_zero_node);
61 val = build_complex (type, val, zero);
62 break;
64 default:
65 gcc_unreachable ();
67 return val;
70 /* Build a string constant with C char type. */
72 tree
73 gfc_build_string_const (int length, const char *s)
75 tree str;
76 tree len;
78 str = build_string (length, s);
79 len = size_int (length);
80 TREE_TYPE (str) =
81 build_array_type (gfc_character1_type_node,
82 build_range_type (gfc_charlen_type_node,
83 size_one_node, len));
84 TYPE_STRING_FLAG (TREE_TYPE (str)) = 1;
85 return str;
89 /* Build a string constant with a type given by its kind; take care of
90 non-default character kinds. */
92 tree
93 gfc_build_wide_string_const (int kind, int length, const gfc_char_t *string)
95 int i;
96 tree str, len;
97 size_t size;
98 char *s;
100 i = gfc_validate_kind (BT_CHARACTER, kind, false);
101 size = length * gfc_character_kinds[i].bit_size / 8;
103 s = XCNEWVAR (char, size);
104 gfc_encode_character (kind, length, string, (unsigned char *) s, size);
106 str = build_string (size, s);
107 free (s);
109 len = size_int (length);
110 TREE_TYPE (str) =
111 build_array_type (gfc_get_char_type (kind),
112 build_range_type (gfc_charlen_type_node,
113 size_one_node, len));
114 TYPE_STRING_FLAG (TREE_TYPE (str)) = 1;
115 return str;
119 /* Build a Fortran character constant from a zero-terminated string.
120 There a two version of this function, one that translates the string
121 and one that doesn't. */
122 tree
123 gfc_build_cstring_const (const char *string)
125 return gfc_build_string_const (strlen (string) + 1, string);
128 tree
129 gfc_build_localized_cstring_const (const char *msgid)
131 const char *localized = _(msgid);
132 return gfc_build_string_const (strlen (localized) + 1, localized);
136 /* Return a string constant with the given length. Used for static
137 initializers. The constant will be padded or truncated to match
138 length. */
140 tree
141 gfc_conv_string_init (tree length, gfc_expr * expr)
143 gfc_char_t *s;
144 HOST_WIDE_INT len;
145 int slen;
146 tree str;
147 bool free_s = false;
149 gcc_assert (expr->expr_type == EXPR_CONSTANT);
150 gcc_assert (expr->ts.type == BT_CHARACTER);
151 gcc_assert (tree_fits_uhwi_p (length));
153 len = TREE_INT_CST_LOW (length);
154 slen = expr->value.character.length;
156 if (len > slen)
158 s = gfc_get_wide_string (len);
159 memcpy (s, expr->value.character.string, slen * sizeof (gfc_char_t));
160 gfc_wide_memset (&s[slen], ' ', len - slen);
161 free_s = true;
163 else
164 s = expr->value.character.string;
166 str = gfc_build_wide_string_const (expr->ts.kind, len, s);
168 if (free_s)
169 free (s);
171 return str;
175 /* Create a tree node for the string length if it is constant. */
177 void
178 gfc_conv_const_charlen (gfc_charlen * cl)
180 if (!cl || cl->backend_decl)
181 return;
183 if (cl->length && cl->length->expr_type == EXPR_CONSTANT)
185 cl->backend_decl = gfc_conv_mpz_to_tree (cl->length->value.integer,
186 cl->length->ts.kind);
187 cl->backend_decl = fold_convert (gfc_charlen_type_node,
188 cl->backend_decl);
192 void
193 gfc_init_constants (void)
195 int n;
197 for (n = 0; n <= GFC_MAX_DIMENSIONS; n++)
198 gfc_rank_cst[n] = build_int_cst (gfc_array_index_type, n);
201 /* Converts a GMP integer into a backend tree node. */
203 tree
204 gfc_conv_mpz_to_tree (mpz_t i, int kind)
206 wide_int val = wi::from_mpz (gfc_get_int_type (kind), i, true);
207 return wide_int_to_tree (gfc_get_int_type (kind), val);
210 /* Converts a backend tree into a GMP integer. */
212 void
213 gfc_conv_tree_to_mpz (mpz_t i, tree source)
215 wi::to_mpz (source, i, TYPE_SIGN (TREE_TYPE (source)));
218 /* Converts a real constant into backend form. */
220 tree
221 gfc_conv_mpfr_to_tree (mpfr_t f, int kind, int is_snan)
223 tree type;
224 int n;
225 REAL_VALUE_TYPE real;
227 n = gfc_validate_kind (BT_REAL, kind, false);
228 gcc_assert (gfc_real_kinds[n].radix == 2);
230 type = gfc_get_real_type (kind);
231 if (mpfr_nan_p (f) && is_snan)
232 real_from_string (&real, "SNaN");
233 else
234 real_from_mpfr (&real, f, type, GFC_RND_MODE);
236 return build_real (type, real);
239 /* Returns a real constant that is +Infinity if the target
240 supports infinities for this floating-point mode, and
241 +HUGE_VAL otherwise (the largest representable number). */
243 tree
244 gfc_build_inf_or_huge (tree type, int kind)
246 if (HONOR_INFINITIES (TYPE_MODE (type)))
248 REAL_VALUE_TYPE real;
249 real_inf (&real);
250 return build_real (type, real);
252 else
254 int k = gfc_validate_kind (BT_REAL, kind, false);
255 return gfc_conv_mpfr_to_tree (gfc_real_kinds[k].huge, kind, 0);
259 /* Returns a floating-point NaN of a given type. */
261 tree
262 gfc_build_nan (tree type, const char *str)
264 REAL_VALUE_TYPE real;
265 real_nan (&real, str, 1, TYPE_MODE (type));
266 return build_real (type, real);
269 /* Converts a backend tree into a real constant. */
271 void
272 gfc_conv_tree_to_mpfr (mpfr_ptr f, tree source)
274 mpfr_from_real (f, TREE_REAL_CST_PTR (source), GFC_RND_MODE);
277 /* Translate any literal constant to a tree. Constants never have
278 pre or post chains. Character literal constants are special
279 special because they have a value and a length, so they cannot be
280 returned as a single tree. It is up to the caller to set the
281 length somewhere if necessary.
283 Returns the translated constant, or aborts if it gets a type it
284 can't handle. */
286 tree
287 gfc_conv_constant_to_tree (gfc_expr * expr)
289 tree res;
291 gcc_assert (expr->expr_type == EXPR_CONSTANT);
293 /* If it is has a prescribed memory representation, we build a string
294 constant and VIEW_CONVERT to its type. */
296 switch (expr->ts.type)
298 case BT_INTEGER:
299 if (expr->representation.string)
300 return fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
301 gfc_get_int_type (expr->ts.kind),
302 gfc_build_string_const (expr->representation.length,
303 expr->representation.string));
304 else
305 return gfc_conv_mpz_to_tree (expr->value.integer, expr->ts.kind);
307 case BT_REAL:
308 if (expr->representation.string)
309 return fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
310 gfc_get_real_type (expr->ts.kind),
311 gfc_build_string_const (expr->representation.length,
312 expr->representation.string));
313 else
314 return gfc_conv_mpfr_to_tree (expr->value.real, expr->ts.kind, expr->is_snan);
316 case BT_LOGICAL:
317 if (expr->representation.string)
319 tree tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
320 gfc_get_int_type (expr->ts.kind),
321 gfc_build_string_const (expr->representation.length,
322 expr->representation.string));
323 if (!integer_zerop (tmp) && !integer_onep (tmp))
324 gfc_warning ("Assigning value other than 0 or 1 to LOGICAL"
325 " has undefined result at %L", &expr->where);
326 return fold_convert (gfc_get_logical_type (expr->ts.kind), tmp);
328 else
329 return build_int_cst (gfc_get_logical_type (expr->ts.kind),
330 expr->value.logical);
332 case BT_COMPLEX:
333 if (expr->representation.string)
334 return fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
335 gfc_get_complex_type (expr->ts.kind),
336 gfc_build_string_const (expr->representation.length,
337 expr->representation.string));
338 else
340 tree real = gfc_conv_mpfr_to_tree (mpc_realref (expr->value.complex),
341 expr->ts.kind, expr->is_snan);
342 tree imag = gfc_conv_mpfr_to_tree (mpc_imagref (expr->value.complex),
343 expr->ts.kind, expr->is_snan);
345 return build_complex (gfc_typenode_for_spec (&expr->ts),
346 real, imag);
349 case BT_CHARACTER:
350 res = gfc_build_wide_string_const (expr->ts.kind,
351 expr->value.character.length,
352 expr->value.character.string);
353 return res;
355 case BT_HOLLERITH:
356 return gfc_build_string_const (expr->representation.length,
357 expr->representation.string);
359 default:
360 fatal_error ("gfc_conv_constant_to_tree(): invalid type: %s",
361 gfc_typename (&expr->ts));
366 /* Like gfc_conv_constant_to_tree, but for a simplified expression.
367 We can handle character literal constants here as well. */
369 void
370 gfc_conv_constant (gfc_se * se, gfc_expr * expr)
372 gfc_ss *ss;
374 /* We may be receiving an expression for C_NULL_PTR or C_NULL_FUNPTR. If
375 so, the expr_type will not yet be an EXPR_CONSTANT. We need to make
376 it so here. */
377 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
378 && expr->ts.u.derived->attr.is_iso_c)
380 if (expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_PTR
381 || expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_FUNPTR)
383 /* Create a new EXPR_CONSTANT expression for our local uses. */
384 expr = gfc_get_int_expr (gfc_default_integer_kind, NULL, 0);
388 if (expr->expr_type != EXPR_CONSTANT)
390 gfc_expr *e = gfc_get_int_expr (gfc_default_integer_kind, NULL, 0);
391 gfc_error ("non-constant initialization expression at %L", &expr->where);
392 se->expr = gfc_conv_constant_to_tree (e);
393 return;
396 ss = se->ss;
397 if (ss != NULL)
399 gfc_ss_info *ss_info;
401 ss_info = ss->info;
402 gcc_assert (ss != gfc_ss_terminator);
403 gcc_assert (ss_info->type == GFC_SS_SCALAR);
404 gcc_assert (ss_info->expr == expr);
406 se->expr = ss_info->data.scalar.value;
407 se->string_length = ss_info->string_length;
408 gfc_advance_se_ss_chain (se);
409 return;
412 /* Translate the constant and put it in the simplifier structure. */
413 se->expr = gfc_conv_constant_to_tree (expr);
415 /* If this is a CHARACTER string, set its length in the simplifier
416 structure, too. */
417 if (expr->ts.type == BT_CHARACTER)
418 se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));