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