]>
Commit | Line | Data |
---|---|---|
6de9cd9a | 1 | /* Translation of constants |
23a5b65a | 2 | Copyright (C) 2002-2014 Free Software Foundation, Inc. |
6de9cd9a DN |
3 | Contributed by Paul Brook |
4 | ||
9fc4d79b | 5 | This file is part of GCC. |
6de9cd9a | 6 | |
9fc4d79b TS |
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 | |
d234d788 | 9 | Software Foundation; either version 3, or (at your option) any later |
9fc4d79b | 10 | version. |
6de9cd9a | 11 | |
9fc4d79b TS |
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. | |
6de9cd9a DN |
16 | |
17 | You should have received a copy of the GNU General Public License | |
d234d788 NC |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
6de9cd9a DN |
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" | |
d8a2d370 | 27 | #include "stor-layout.h" |
d49b6e1e | 28 | #include "realmpfr.h" |
c829d016 | 29 | #include "diagnostic-core.h" /* For fatal_error. */ |
18452a7d | 30 | #include "double-int.h" |
6de9cd9a DN |
31 | #include "gfortran.h" |
32 | #include "trans.h" | |
33 | #include "trans-const.h" | |
34 | #include "trans-types.h" | |
d393bbd7 | 35 | #include "target-memory.h" |
6de9cd9a | 36 | |
6de9cd9a DN |
37 | tree gfc_rank_cst[GFC_MAX_DIMENSIONS + 1]; |
38 | ||
39 | /* Build a constant with given type from an int_cst. */ | |
f8d0aee5 | 40 | |
6de9cd9a DN |
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: | |
6e45f57b | 64 | gcc_unreachable (); |
6de9cd9a DN |
65 | } |
66 | return val; | |
67 | } | |
68 | ||
d393bbd7 FXC |
69 | /* Build a string constant with C char type. */ |
70 | ||
6de9cd9a DN |
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); | |
df09d1d5 | 78 | len = size_int (length); |
6de9cd9a DN |
79 | TREE_TYPE (str) = |
80 | build_array_type (gfc_character1_type_node, | |
d7177ab2 | 81 | build_range_type (gfc_charlen_type_node, |
df09d1d5 | 82 | size_one_node, len)); |
6de9cd9a DN |
83 | return str; |
84 | } | |
85 | ||
d393bbd7 FXC |
86 | |
87 | /* Build a string constant with a type given by its kind; take care of | |
88 | non-default character kinds. */ | |
89 | ||
90 | tree | |
91 | gfc_build_wide_string_const (int kind, int length, const gfc_char_t *string) | |
92 | { | |
93 | int i; | |
94 | tree str, len; | |
95 | size_t size; | |
96 | char *s; | |
97 | ||
98 | i = gfc_validate_kind (BT_CHARACTER, kind, false); | |
99 | size = length * gfc_character_kinds[i].bit_size / 8; | |
100 | ||
ece3f663 | 101 | s = XCNEWVAR (char, size); |
d393bbd7 FXC |
102 | gfc_encode_character (kind, length, string, (unsigned char *) s, size); |
103 | ||
104 | str = build_string (size, s); | |
cede9502 | 105 | free (s); |
d393bbd7 | 106 | |
df09d1d5 | 107 | len = size_int (length); |
d393bbd7 FXC |
108 | TREE_TYPE (str) = |
109 | build_array_type (gfc_get_char_type (kind), | |
110 | build_range_type (gfc_charlen_type_node, | |
df09d1d5 | 111 | size_one_node, len)); |
d393bbd7 FXC |
112 | return str; |
113 | } | |
114 | ||
115 | ||
31043f6c | 116 | /* Build a Fortran character constant from a zero-terminated string. |
ee37d2f5 FXC |
117 | There a two version of this function, one that translates the string |
118 | and one that doesn't. */ | |
95638988 | 119 | tree |
ee37d2f5 | 120 | gfc_build_cstring_const (const char *string) |
95638988 | 121 | { |
ee37d2f5 | 122 | return gfc_build_string_const (strlen (string) + 1, string); |
95638988 TS |
123 | } |
124 | ||
ee37d2f5 FXC |
125 | tree |
126 | gfc_build_localized_cstring_const (const char *msgid) | |
127 | { | |
128 | const char *localized = _(msgid); | |
129 | return gfc_build_string_const (strlen (localized) + 1, localized); | |
130 | } | |
131 | ||
132 | ||
6de9cd9a | 133 | /* Return a string constant with the given length. Used for static |
94716287 TS |
134 | initializers. The constant will be padded or truncated to match |
135 | length. */ | |
136 | ||
6de9cd9a DN |
137 | tree |
138 | gfc_conv_string_init (tree length, gfc_expr * expr) | |
139 | { | |
00660189 | 140 | gfc_char_t *s; |
6de9cd9a DN |
141 | HOST_WIDE_INT len; |
142 | int slen; | |
143 | tree str; | |
d393bbd7 | 144 | bool free_s = false; |
6de9cd9a | 145 | |
6e45f57b | 146 | gcc_assert (expr->expr_type == EXPR_CONSTANT); |
d393bbd7 | 147 | gcc_assert (expr->ts.type == BT_CHARACTER); |
6e45f57b PB |
148 | gcc_assert (INTEGER_CST_P (length)); |
149 | gcc_assert (TREE_INT_CST_HIGH (length) == 0); | |
6de9cd9a DN |
150 | |
151 | len = TREE_INT_CST_LOW (length); | |
152 | slen = expr->value.character.length; | |
94716287 TS |
153 | |
154 | if (len > slen) | |
6de9cd9a | 155 | { |
00660189 FXC |
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); | |
d393bbd7 | 159 | free_s = true; |
6de9cd9a DN |
160 | } |
161 | else | |
d393bbd7 | 162 | s = expr->value.character.string; |
00660189 | 163 | |
d393bbd7 FXC |
164 | str = gfc_build_wide_string_const (expr->ts.kind, len, s); |
165 | ||
166 | if (free_s) | |
cede9502 | 167 | free (s); |
6de9cd9a DN |
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 | { | |
c73b6478 | 178 | if (!cl || cl->backend_decl) |
6de9cd9a DN |
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); | |
1e7d0a64 FW |
185 | cl->backend_decl = fold_convert (gfc_charlen_type_node, |
186 | cl->backend_decl); | |
6de9cd9a DN |
187 | } |
188 | } | |
189 | ||
190 | void | |
191 | gfc_init_constants (void) | |
192 | { | |
193 | int n; | |
194 | ||
195 | for (n = 0; n <= GFC_MAX_DIMENSIONS; n++) | |
7d60be94 | 196 | gfc_rank_cst[n] = build_int_cst (gfc_array_index_type, n); |
6de9cd9a DN |
197 | } |
198 | ||
6de9cd9a | 199 | /* Converts a GMP integer into a backend tree node. */ |
18452a7d | 200 | |
6de9cd9a DN |
201 | tree |
202 | gfc_conv_mpz_to_tree (mpz_t i, int kind) | |
203 | { | |
18452a7d BM |
204 | double_int val = mpz_get_double_int (gfc_get_int_type (kind), i, true); |
205 | return double_int_to_tree (gfc_get_int_type (kind), val); | |
206 | } | |
6de9cd9a | 207 | |
18452a7d | 208 | /* Converts a backend tree into a GMP integer. */ |
6de9cd9a | 209 | |
18452a7d BM |
210 | void |
211 | gfc_conv_tree_to_mpz (mpz_t i, tree source) | |
212 | { | |
213 | double_int val = tree_to_double_int (source); | |
214 | mpz_set_double_int (i, val, TYPE_UNSIGNED (TREE_TYPE (source))); | |
6de9cd9a DN |
215 | } |
216 | ||
18452a7d | 217 | /* Converts a real constant into backend form. */ |
f8d0aee5 | 218 | |
6de9cd9a | 219 | tree |
346a77d1 | 220 | gfc_conv_mpfr_to_tree (mpfr_t f, int kind, int is_snan) |
6de9cd9a | 221 | { |
6de9cd9a | 222 | tree type; |
6de9cd9a | 223 | int n; |
54554825 | 224 | REAL_VALUE_TYPE real; |
6de9cd9a | 225 | |
855a145c | 226 | n = gfc_validate_kind (BT_REAL, kind, false); |
855a145c TS |
227 | gcc_assert (gfc_real_kinds[n].radix == 2); |
228 | ||
54554825 | 229 | type = gfc_get_real_type (kind); |
346a77d1 TB |
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 | ||
18452a7d BM |
235 | return build_real (type, real); |
236 | } | |
54554825 | 237 | |
a67189d4 FXC |
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 | ||
18452a7d | 258 | /* Converts a backend tree into a real constant. */ |
6de9cd9a | 259 | |
18452a7d BM |
260 | void |
261 | gfc_conv_tree_to_mpfr (mpfr_ptr f, tree source) | |
262 | { | |
263 | mpfr_from_real (f, TREE_REAL_CST_PTR (source), GFC_RND_MODE); | |
6de9cd9a DN |
264 | } |
265 | ||
6de9cd9a DN |
266 | /* Translate any literal constant to a tree. Constants never have |
267 | pre or post chains. Character literal constants are special | |
268 | special because they have a value and a length, so they cannot be | |
269 | returned as a single tree. It is up to the caller to set the | |
270 | length somewhere if necessary. | |
271 | ||
272 | Returns the translated constant, or aborts if it gets a type it | |
273 | can't handle. */ | |
274 | ||
275 | tree | |
276 | gfc_conv_constant_to_tree (gfc_expr * expr) | |
277 | { | |
00660189 | 278 | tree res; |
00660189 | 279 | |
6e45f57b | 280 | gcc_assert (expr->expr_type == EXPR_CONSTANT); |
6de9cd9a | 281 | |
20585ad6 BM |
282 | /* If it is has a prescribed memory representation, we build a string |
283 | constant and VIEW_CONVERT to its type. */ | |
d3642f89 | 284 | |
6de9cd9a DN |
285 | switch (expr->ts.type) |
286 | { | |
287 | case BT_INTEGER: | |
20585ad6 | 288 | if (expr->representation.string) |
bc98ed60 TB |
289 | return fold_build1_loc (input_location, VIEW_CONVERT_EXPR, |
290 | gfc_get_int_type (expr->ts.kind), | |
291 | gfc_build_string_const (expr->representation.length, | |
292 | expr->representation.string)); | |
d3642f89 FW |
293 | else |
294 | return gfc_conv_mpz_to_tree (expr->value.integer, expr->ts.kind); | |
6de9cd9a DN |
295 | |
296 | case BT_REAL: | |
20585ad6 | 297 | if (expr->representation.string) |
bc98ed60 TB |
298 | return fold_build1_loc (input_location, VIEW_CONVERT_EXPR, |
299 | gfc_get_real_type (expr->ts.kind), | |
300 | gfc_build_string_const (expr->representation.length, | |
301 | expr->representation.string)); | |
d3642f89 | 302 | else |
346a77d1 | 303 | return gfc_conv_mpfr_to_tree (expr->value.real, expr->ts.kind, expr->is_snan); |
6de9cd9a DN |
304 | |
305 | case BT_LOGICAL: | |
20585ad6 | 306 | if (expr->representation.string) |
27a4e072 | 307 | { |
bc98ed60 TB |
308 | tree tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR, |
309 | gfc_get_int_type (expr->ts.kind), | |
310 | gfc_build_string_const (expr->representation.length, | |
311 | expr->representation.string)); | |
27a4e072 JJ |
312 | if (!integer_zerop (tmp) && !integer_onep (tmp)) |
313 | gfc_warning ("Assigning value other than 0 or 1 to LOGICAL" | |
314 | " has undefined result at %L", &expr->where); | |
315 | return fold_convert (gfc_get_logical_type (expr->ts.kind), tmp); | |
316 | } | |
d3642f89 FW |
317 | else |
318 | return build_int_cst (gfc_get_logical_type (expr->ts.kind), | |
27a4e072 | 319 | expr->value.logical); |
6de9cd9a DN |
320 | |
321 | case BT_COMPLEX: | |
20585ad6 | 322 | if (expr->representation.string) |
bc98ed60 TB |
323 | return fold_build1_loc (input_location, VIEW_CONVERT_EXPR, |
324 | gfc_get_complex_type (expr->ts.kind), | |
325 | gfc_build_string_const (expr->representation.length, | |
326 | expr->representation.string)); | |
d3642f89 FW |
327 | else |
328 | { | |
eb6f9a86 | 329 | tree real = gfc_conv_mpfr_to_tree (mpc_realref (expr->value.complex), |
346a77d1 | 330 | expr->ts.kind, expr->is_snan); |
eb6f9a86 | 331 | tree imag = gfc_conv_mpfr_to_tree (mpc_imagref (expr->value.complex), |
346a77d1 | 332 | expr->ts.kind, expr->is_snan); |
6de9cd9a | 333 | |
d3642f89 FW |
334 | return build_complex (gfc_typenode_for_spec (&expr->ts), |
335 | real, imag); | |
336 | } | |
6de9cd9a DN |
337 | |
338 | case BT_CHARACTER: | |
d393bbd7 FXC |
339 | res = gfc_build_wide_string_const (expr->ts.kind, |
340 | expr->value.character.length, | |
341 | expr->value.character.string); | |
00660189 | 342 | return res; |
6de9cd9a | 343 | |
20585ad6 BM |
344 | case BT_HOLLERITH: |
345 | return gfc_build_string_const (expr->representation.length, | |
346 | expr->representation.string); | |
347 | ||
6de9cd9a DN |
348 | default: |
349 | fatal_error ("gfc_conv_constant_to_tree(): invalid type: %s", | |
350 | gfc_typename (&expr->ts)); | |
351 | } | |
352 | } | |
353 | ||
354 | ||
f8d0aee5 | 355 | /* Like gfc_conv_constant_to_tree, but for a simplified expression. |
6de9cd9a DN |
356 | We can handle character literal constants here as well. */ |
357 | ||
358 | void | |
359 | gfc_conv_constant (gfc_se * se, gfc_expr * expr) | |
360 | { | |
bcc4d4e0 MM |
361 | gfc_ss *ss; |
362 | ||
a8b3b0b6 | 363 | /* We may be receiving an expression for C_NULL_PTR or C_NULL_FUNPTR. If |
9d3d3f10 | 364 | so, the expr_type will not yet be an EXPR_CONSTANT. We need to make |
a8b3b0b6 | 365 | it so here. */ |
bc21d315 JW |
366 | if (expr->ts.type == BT_DERIVED && expr->ts.u.derived |
367 | && expr->ts.u.derived->attr.is_iso_c) | |
a8b3b0b6 CR |
368 | { |
369 | if (expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_PTR | |
370 | || expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_FUNPTR) | |
371 | { | |
372 | /* Create a new EXPR_CONSTANT expression for our local uses. */ | |
b7e75771 | 373 | expr = gfc_get_int_expr (gfc_default_integer_kind, NULL, 0); |
a8b3b0b6 CR |
374 | } |
375 | } | |
376 | ||
9d3d3f10 JD |
377 | if (expr->expr_type != EXPR_CONSTANT) |
378 | { | |
b7e75771 | 379 | gfc_expr *e = gfc_get_int_expr (gfc_default_integer_kind, NULL, 0); |
9d3d3f10 | 380 | gfc_error ("non-constant initialization expression at %L", &expr->where); |
b7e75771 | 381 | se->expr = gfc_conv_constant_to_tree (e); |
9d3d3f10 JD |
382 | return; |
383 | } | |
6de9cd9a | 384 | |
bcc4d4e0 MM |
385 | ss = se->ss; |
386 | if (ss != NULL) | |
6de9cd9a | 387 | { |
f98cfd3c MM |
388 | gfc_ss_info *ss_info; |
389 | ||
390 | ss_info = ss->info; | |
bcc4d4e0 | 391 | gcc_assert (ss != gfc_ss_terminator); |
f98cfd3c MM |
392 | gcc_assert (ss_info->type == GFC_SS_SCALAR); |
393 | gcc_assert (ss_info->expr == expr); | |
6de9cd9a | 394 | |
99dd5a29 | 395 | se->expr = ss_info->data.scalar.value; |
a0add3be | 396 | se->string_length = ss_info->string_length; |
6de9cd9a DN |
397 | gfc_advance_se_ss_chain (se); |
398 | return; | |
399 | } | |
400 | ||
401 | /* Translate the constant and put it in the simplifier structure. */ | |
402 | se->expr = gfc_conv_constant_to_tree (expr); | |
403 | ||
f8d0aee5 | 404 | /* If this is a CHARACTER string, set its length in the simplifier |
6de9cd9a DN |
405 | structure, too. */ |
406 | if (expr->ts.type == BT_CHARACTER) | |
407 | se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr))); | |
408 | } |