Update ChangeLogs for wide-int work.
[gcc.git] / gcc / fortran / trans-const.c
1 /* Translation of constants
2 Copyright (C) 2002-2014 Free Software Foundation, Inc.
3 Contributed by Paul Brook
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 /* trans-const.c -- convert constant values */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tree.h"
27 #include "stor-layout.h"
28 #include "realmpfr.h"
29 #include "diagnostic-core.h" /* For fatal_error. */
30 #include "double-int.h"
31 #include "gfortran.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"
37
38 tree gfc_rank_cst[GFC_MAX_DIMENSIONS + 1];
39
40 /* Build a constant with given type from an int_cst. */
41
42 tree
43 gfc_build_const (tree type, tree intval)
44 {
45 tree val;
46 tree zero;
47
48 switch (TREE_CODE (type))
49 {
50 case INTEGER_TYPE:
51 val = convert (type, intval);
52 break;
53
54 case REAL_TYPE:
55 val = build_real_from_int_cst (type, intval);
56 break;
57
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;
63
64 default:
65 gcc_unreachable ();
66 }
67 return val;
68 }
69
70 /* Build a string constant with C char type. */
71
72 tree
73 gfc_build_string_const (int length, const char *s)
74 {
75 tree str;
76 tree len;
77
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 return str;
85 }
86
87
88 /* Build a string constant with a type given by its kind; take care of
89 non-default character kinds. */
90
91 tree
92 gfc_build_wide_string_const (int kind, int length, const gfc_char_t *string)
93 {
94 int i;
95 tree str, len;
96 size_t size;
97 char *s;
98
99 i = gfc_validate_kind (BT_CHARACTER, kind, false);
100 size = length * gfc_character_kinds[i].bit_size / 8;
101
102 s = XCNEWVAR (char, size);
103 gfc_encode_character (kind, length, string, (unsigned char *) s, size);
104
105 str = build_string (size, s);
106 free (s);
107
108 len = size_int (length);
109 TREE_TYPE (str) =
110 build_array_type (gfc_get_char_type (kind),
111 build_range_type (gfc_charlen_type_node,
112 size_one_node, len));
113 return str;
114 }
115
116
117 /* Build a Fortran character constant from a zero-terminated string.
118 There a two version of this function, one that translates the string
119 and one that doesn't. */
120 tree
121 gfc_build_cstring_const (const char *string)
122 {
123 return gfc_build_string_const (strlen (string) + 1, string);
124 }
125
126 tree
127 gfc_build_localized_cstring_const (const char *msgid)
128 {
129 const char *localized = _(msgid);
130 return gfc_build_string_const (strlen (localized) + 1, localized);
131 }
132
133
134 /* Return a string constant with the given length. Used for static
135 initializers. The constant will be padded or truncated to match
136 length. */
137
138 tree
139 gfc_conv_string_init (tree length, gfc_expr * expr)
140 {
141 gfc_char_t *s;
142 HOST_WIDE_INT len;
143 int slen;
144 tree str;
145 bool free_s = false;
146
147 gcc_assert (expr->expr_type == EXPR_CONSTANT);
148 gcc_assert (expr->ts.type == BT_CHARACTER);
149 gcc_assert (tree_fits_uhwi_p (length));
150
151 len = TREE_INT_CST_LOW (length);
152 slen = expr->value.character.length;
153
154 if (len > slen)
155 {
156 s = gfc_get_wide_string (len);
157 memcpy (s, expr->value.character.string, slen * sizeof (gfc_char_t));
158 gfc_wide_memset (&s[slen], ' ', len - slen);
159 free_s = true;
160 }
161 else
162 s = expr->value.character.string;
163
164 str = gfc_build_wide_string_const (expr->ts.kind, len, s);
165
166 if (free_s)
167 free (s);
168
169 return str;
170 }
171
172
173 /* Create a tree node for the string length if it is constant. */
174
175 void
176 gfc_conv_const_charlen (gfc_charlen * cl)
177 {
178 if (!cl || cl->backend_decl)
179 return;
180
181 if (cl->length && cl->length->expr_type == EXPR_CONSTANT)
182 {
183 cl->backend_decl = gfc_conv_mpz_to_tree (cl->length->value.integer,
184 cl->length->ts.kind);
185 cl->backend_decl = fold_convert (gfc_charlen_type_node,
186 cl->backend_decl);
187 }
188 }
189
190 void
191 gfc_init_constants (void)
192 {
193 int n;
194
195 for (n = 0; n <= GFC_MAX_DIMENSIONS; n++)
196 gfc_rank_cst[n] = build_int_cst (gfc_array_index_type, n);
197 }
198
199 /* Converts a GMP integer into a backend tree node. */
200
201 tree
202 gfc_conv_mpz_to_tree (mpz_t i, int kind)
203 {
204 wide_int val = wi::from_mpz (gfc_get_int_type (kind), i, true);
205 return wide_int_to_tree (gfc_get_int_type (kind), val);
206 }
207
208 /* Converts a backend tree into a GMP integer. */
209
210 void
211 gfc_conv_tree_to_mpz (mpz_t i, tree source)
212 {
213 wi::to_mpz (source, i, TYPE_SIGN (TREE_TYPE (source)));
214 }
215
216 /* Converts a real constant into backend form. */
217
218 tree
219 gfc_conv_mpfr_to_tree (mpfr_t f, int kind, int is_snan)
220 {
221 tree type;
222 int n;
223 REAL_VALUE_TYPE real;
224
225 n = gfc_validate_kind (BT_REAL, kind, false);
226 gcc_assert (gfc_real_kinds[n].radix == 2);
227
228 type = gfc_get_real_type (kind);
229 if (mpfr_nan_p (f) && is_snan)
230 real_from_string (&real, "SNaN");
231 else
232 real_from_mpfr (&real, f, type, GFC_RND_MODE);
233
234 return build_real (type, real);
235 }
236
237 /* Returns a real constant that is +Infinity if the target
238 supports infinities for this floating-point mode, and
239 +HUGE_VAL otherwise (the largest representable number). */
240
241 tree
242 gfc_build_inf_or_huge (tree type, int kind)
243 {
244 if (HONOR_INFINITIES (TYPE_MODE (type)))
245 {
246 REAL_VALUE_TYPE real;
247 real_inf (&real);
248 return build_real (type, real);
249 }
250 else
251 {
252 int k = gfc_validate_kind (BT_REAL, kind, false);
253 return gfc_conv_mpfr_to_tree (gfc_real_kinds[k].huge, kind, 0);
254 }
255 }
256
257 /* Converts a backend tree into a real constant. */
258
259 void
260 gfc_conv_tree_to_mpfr (mpfr_ptr f, tree source)
261 {
262 mpfr_from_real (f, TREE_REAL_CST_PTR (source), GFC_RND_MODE);
263 }
264
265 /* Translate any literal constant to a tree. Constants never have
266 pre or post chains. Character literal constants are special
267 special because they have a value and a length, so they cannot be
268 returned as a single tree. It is up to the caller to set the
269 length somewhere if necessary.
270
271 Returns the translated constant, or aborts if it gets a type it
272 can't handle. */
273
274 tree
275 gfc_conv_constant_to_tree (gfc_expr * expr)
276 {
277 tree res;
278
279 gcc_assert (expr->expr_type == EXPR_CONSTANT);
280
281 /* If it is has a prescribed memory representation, we build a string
282 constant and VIEW_CONVERT to its type. */
283
284 switch (expr->ts.type)
285 {
286 case BT_INTEGER:
287 if (expr->representation.string)
288 return fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
289 gfc_get_int_type (expr->ts.kind),
290 gfc_build_string_const (expr->representation.length,
291 expr->representation.string));
292 else
293 return gfc_conv_mpz_to_tree (expr->value.integer, expr->ts.kind);
294
295 case BT_REAL:
296 if (expr->representation.string)
297 return fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
298 gfc_get_real_type (expr->ts.kind),
299 gfc_build_string_const (expr->representation.length,
300 expr->representation.string));
301 else
302 return gfc_conv_mpfr_to_tree (expr->value.real, expr->ts.kind, expr->is_snan);
303
304 case BT_LOGICAL:
305 if (expr->representation.string)
306 {
307 tree tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
308 gfc_get_int_type (expr->ts.kind),
309 gfc_build_string_const (expr->representation.length,
310 expr->representation.string));
311 if (!integer_zerop (tmp) && !integer_onep (tmp))
312 gfc_warning ("Assigning value other than 0 or 1 to LOGICAL"
313 " has undefined result at %L", &expr->where);
314 return fold_convert (gfc_get_logical_type (expr->ts.kind), tmp);
315 }
316 else
317 return build_int_cst (gfc_get_logical_type (expr->ts.kind),
318 expr->value.logical);
319
320 case BT_COMPLEX:
321 if (expr->representation.string)
322 return fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
323 gfc_get_complex_type (expr->ts.kind),
324 gfc_build_string_const (expr->representation.length,
325 expr->representation.string));
326 else
327 {
328 tree real = gfc_conv_mpfr_to_tree (mpc_realref (expr->value.complex),
329 expr->ts.kind, expr->is_snan);
330 tree imag = gfc_conv_mpfr_to_tree (mpc_imagref (expr->value.complex),
331 expr->ts.kind, expr->is_snan);
332
333 return build_complex (gfc_typenode_for_spec (&expr->ts),
334 real, imag);
335 }
336
337 case BT_CHARACTER:
338 res = gfc_build_wide_string_const (expr->ts.kind,
339 expr->value.character.length,
340 expr->value.character.string);
341 return res;
342
343 case BT_HOLLERITH:
344 return gfc_build_string_const (expr->representation.length,
345 expr->representation.string);
346
347 default:
348 fatal_error ("gfc_conv_constant_to_tree(): invalid type: %s",
349 gfc_typename (&expr->ts));
350 }
351 }
352
353
354 /* Like gfc_conv_constant_to_tree, but for a simplified expression.
355 We can handle character literal constants here as well. */
356
357 void
358 gfc_conv_constant (gfc_se * se, gfc_expr * expr)
359 {
360 gfc_ss *ss;
361
362 /* We may be receiving an expression for C_NULL_PTR or C_NULL_FUNPTR. If
363 so, the expr_type will not yet be an EXPR_CONSTANT. We need to make
364 it so here. */
365 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
366 && expr->ts.u.derived->attr.is_iso_c)
367 {
368 if (expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_PTR
369 || expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_FUNPTR)
370 {
371 /* Create a new EXPR_CONSTANT expression for our local uses. */
372 expr = gfc_get_int_expr (gfc_default_integer_kind, NULL, 0);
373 }
374 }
375
376 if (expr->expr_type != EXPR_CONSTANT)
377 {
378 gfc_expr *e = gfc_get_int_expr (gfc_default_integer_kind, NULL, 0);
379 gfc_error ("non-constant initialization expression at %L", &expr->where);
380 se->expr = gfc_conv_constant_to_tree (e);
381 return;
382 }
383
384 ss = se->ss;
385 if (ss != NULL)
386 {
387 gfc_ss_info *ss_info;
388
389 ss_info = ss->info;
390 gcc_assert (ss != gfc_ss_terminator);
391 gcc_assert (ss_info->type == GFC_SS_SCALAR);
392 gcc_assert (ss_info->expr == expr);
393
394 se->expr = ss_info->data.scalar.value;
395 se->string_length = ss_info->string_length;
396 gfc_advance_se_ss_chain (se);
397 return;
398 }
399
400 /* Translate the constant and put it in the simplifier structure. */
401 se->expr = gfc_conv_constant_to_tree (expr);
402
403 /* If this is a CHARACTER string, set its length in the simplifier
404 structure, too. */
405 if (expr->ts.type == BT_CHARACTER)
406 se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));
407 }