Check in tree-dce enh to trunk
[official-gcc.git] / gcc / fortran / trans-const.c
blob6c9032f972a2eb79c7641620237298ba2aeb7711
1 /* Translation of constants
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software
3 Foundation, Inc.
4 Contributed by Paul Brook
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* trans-const.c -- convert constant values */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tree.h"
28 #include "ggc.h"
29 #include "toplev.h"
30 #include "real.h"
31 #include "double-int.h"
32 #include "gfortran.h"
33 #include "trans.h"
34 #include "trans-const.h"
35 #include "trans-types.h"
37 tree gfc_rank_cst[GFC_MAX_DIMENSIONS + 1];
39 /* Build a constant with given type from an int_cst. */
41 tree
42 gfc_build_const (tree type, tree intval)
44 tree val;
45 tree zero;
47 switch (TREE_CODE (type))
49 case INTEGER_TYPE:
50 val = convert (type, intval);
51 break;
53 case REAL_TYPE:
54 val = build_real_from_int_cst (type, intval);
55 break;
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;
63 default:
64 gcc_unreachable ();
66 return val;
69 tree
70 gfc_build_string_const (int length, const char *s)
72 tree str;
73 tree len;
75 str = build_string (length, s);
76 len = build_int_cst (NULL_TREE, length);
77 TREE_TYPE (str) =
78 build_array_type (gfc_character1_type_node,
79 build_range_type (gfc_charlen_type_node,
80 integer_one_node, len));
81 return str;
84 /* Build a Fortran character constant from a zero-terminated string.
85 There a two version of this function, one that translates the string
86 and one that doesn't. */
87 tree
88 gfc_build_cstring_const (const char *string)
90 return gfc_build_string_const (strlen (string) + 1, string);
93 tree
94 gfc_build_localized_cstring_const (const char *msgid)
96 const char *localized = _(msgid);
97 return gfc_build_string_const (strlen (localized) + 1, localized);
101 /* Return a string constant with the given length. Used for static
102 initializers. The constant will be padded or truncated to match
103 length. */
105 tree
106 gfc_conv_string_init (tree length, gfc_expr * expr)
108 gfc_char_t *s;
109 char *c;
110 HOST_WIDE_INT len;
111 int slen;
112 tree str;
114 gcc_assert (expr->expr_type == EXPR_CONSTANT);
115 gcc_assert (expr->ts.type == BT_CHARACTER && expr->ts.kind == 1);
116 gcc_assert (INTEGER_CST_P (length));
117 gcc_assert (TREE_INT_CST_HIGH (length) == 0);
119 len = TREE_INT_CST_LOW (length);
120 slen = expr->value.character.length;
122 if (len > slen)
124 s = gfc_get_wide_string (len);
125 memcpy (s, expr->value.character.string, slen * sizeof (gfc_char_t));
126 gfc_wide_memset (&s[slen], ' ', len - slen);
128 /* FIXME -- currently ignore wide character strings; see assert
129 above. */
130 c = gfc_widechar_to_char (s, len);
131 gfc_free (s);
133 else
134 c = gfc_widechar_to_char (expr->value.character.string,
135 expr->value.character.length);
137 str = gfc_build_string_const (len, c);
138 gfc_free (c);
140 return str;
144 /* Create a tree node for the string length if it is constant. */
146 void
147 gfc_conv_const_charlen (gfc_charlen * cl)
149 if (cl->backend_decl)
150 return;
152 if (cl->length && cl->length->expr_type == EXPR_CONSTANT)
154 cl->backend_decl = gfc_conv_mpz_to_tree (cl->length->value.integer,
155 cl->length->ts.kind);
156 cl->backend_decl = fold_convert (gfc_charlen_type_node,
157 cl->backend_decl);
161 void
162 gfc_init_constants (void)
164 int n;
166 for (n = 0; n <= GFC_MAX_DIMENSIONS; n++)
167 gfc_rank_cst[n] = build_int_cst (gfc_array_index_type, n);
170 /* Converts a GMP integer into a backend tree node. */
172 tree
173 gfc_conv_mpz_to_tree (mpz_t i, int kind)
175 double_int val = mpz_get_double_int (gfc_get_int_type (kind), i, true);
176 return double_int_to_tree (gfc_get_int_type (kind), val);
179 /* Converts a backend tree into a GMP integer. */
181 void
182 gfc_conv_tree_to_mpz (mpz_t i, tree source)
184 double_int val = tree_to_double_int (source);
185 mpz_set_double_int (i, val, TYPE_UNSIGNED (TREE_TYPE (source)));
188 /* Converts a real constant into backend form. */
190 tree
191 gfc_conv_mpfr_to_tree (mpfr_t f, int kind)
193 tree type;
194 int n;
195 REAL_VALUE_TYPE real;
197 n = gfc_validate_kind (BT_REAL, kind, false);
198 gcc_assert (gfc_real_kinds[n].radix == 2);
200 type = gfc_get_real_type (kind);
201 real_from_mpfr (&real, f, type, GFC_RND_MODE);
202 return build_real (type, real);
205 /* Converts a backend tree into a real constant. */
207 void
208 gfc_conv_tree_to_mpfr (mpfr_ptr f, tree source)
210 mpfr_from_real (f, TREE_REAL_CST_PTR (source), GFC_RND_MODE);
213 /* Translate any literal constant to a tree. Constants never have
214 pre or post chains. Character literal constants are special
215 special because they have a value and a length, so they cannot be
216 returned as a single tree. It is up to the caller to set the
217 length somewhere if necessary.
219 Returns the translated constant, or aborts if it gets a type it
220 can't handle. */
222 tree
223 gfc_conv_constant_to_tree (gfc_expr * expr)
225 tree res;
226 char *s;
228 gcc_assert (expr->expr_type == EXPR_CONSTANT);
230 /* If it is has a prescribed memory representation, we build a string
231 constant and VIEW_CONVERT to its type. */
233 switch (expr->ts.type)
235 case BT_INTEGER:
236 if (expr->representation.string)
237 return fold_build1 (VIEW_CONVERT_EXPR,
238 gfc_get_int_type (expr->ts.kind),
239 gfc_build_string_const (expr->representation.length,
240 expr->representation.string));
241 else
242 return gfc_conv_mpz_to_tree (expr->value.integer, expr->ts.kind);
244 case BT_REAL:
245 if (expr->representation.string)
246 return fold_build1 (VIEW_CONVERT_EXPR,
247 gfc_get_real_type (expr->ts.kind),
248 gfc_build_string_const (expr->representation.length,
249 expr->representation.string));
250 else
251 return gfc_conv_mpfr_to_tree (expr->value.real, expr->ts.kind);
253 case BT_LOGICAL:
254 if (expr->representation.string)
255 return fold_build1 (VIEW_CONVERT_EXPR,
256 gfc_get_logical_type (expr->ts.kind),
257 gfc_build_string_const (expr->representation.length,
258 expr->representation.string));
259 else
260 return build_int_cst (gfc_get_logical_type (expr->ts.kind),
261 expr->value.logical);
263 case BT_COMPLEX:
264 if (expr->representation.string)
265 return fold_build1 (VIEW_CONVERT_EXPR,
266 gfc_get_complex_type (expr->ts.kind),
267 gfc_build_string_const (expr->representation.length,
268 expr->representation.string));
269 else
271 tree real = gfc_conv_mpfr_to_tree (expr->value.complex.r,
272 expr->ts.kind);
273 tree imag = gfc_conv_mpfr_to_tree (expr->value.complex.i,
274 expr->ts.kind);
276 return build_complex (gfc_typenode_for_spec (&expr->ts),
277 real, imag);
280 case BT_CHARACTER:
281 gcc_assert (expr->ts.kind == 1);
282 s = gfc_widechar_to_char (expr->value.character.string,
283 expr->value.character.length);
284 res = gfc_build_string_const (expr->value.character.length, s);
285 gfc_free (s);
286 return res;
288 case BT_HOLLERITH:
289 return gfc_build_string_const (expr->representation.length,
290 expr->representation.string);
292 default:
293 fatal_error ("gfc_conv_constant_to_tree(): invalid type: %s",
294 gfc_typename (&expr->ts));
299 /* Like gfc_conv_constant_to_tree, but for a simplified expression.
300 We can handle character literal constants here as well. */
302 void
303 gfc_conv_constant (gfc_se * se, gfc_expr * expr)
305 /* We may be receiving an expression for C_NULL_PTR or C_NULL_FUNPTR. If
306 so, they expr_type will not yet be an EXPR_CONSTANT. We need to make
307 it so here. */
308 if (expr->ts.type == BT_DERIVED && expr->ts.derived
309 && expr->ts.derived->attr.is_iso_c)
311 if (expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_PTR
312 || expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_FUNPTR)
314 /* Create a new EXPR_CONSTANT expression for our local uses. */
315 expr = gfc_int_expr (0);
319 gcc_assert (expr->expr_type == EXPR_CONSTANT);
321 if (se->ss != NULL)
323 gcc_assert (se->ss != gfc_ss_terminator);
324 gcc_assert (se->ss->type == GFC_SS_SCALAR);
325 gcc_assert (se->ss->expr == expr);
327 se->expr = se->ss->data.scalar.expr;
328 se->string_length = se->ss->string_length;
329 gfc_advance_se_ss_chain (se);
330 return;
333 /* Translate the constant and put it in the simplifier structure. */
334 se->expr = gfc_conv_constant_to_tree (expr);
336 /* If this is a CHARACTER string, set its length in the simplifier
337 structure, too. */
338 if (expr->ts.type == BT_CHARACTER)
339 se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));