1 /* Translation of constants
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Free Software
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
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
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 */
26 #include "coretypes.h"
31 #include "double-int.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. */
42 gfc_build_const (tree type, tree intval)
47 switch (TREE_CODE (type))
50 val = convert (type, intval);
54 val = build_real_from_int_cst (type, intval);
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);
70 gfc_build_string_const (int length, const char *s)
75 str = build_string (length, s);
76 len = build_int_cst (NULL_TREE, length);
78 build_array_type (gfc_character1_type_node,
79 build_range_type (gfc_charlen_type_node,
80 integer_one_node, len));
84 /* Build a Fortran character constant from a zero-terminated string.
85 Since this is mainly used for error messages, the string will get
88 gfc_build_cstring_const (const char *msgid)
90 return gfc_build_string_const (strlen (msgid) + 1, _(msgid));
93 /* Return a string constant with the given length. Used for static
94 initializers. The constant will be padded or truncated to match
98 gfc_conv_string_init (tree length, gfc_expr * expr)
105 gcc_assert (expr->expr_type == EXPR_CONSTANT);
106 gcc_assert (expr->ts.type == BT_CHARACTER && expr->ts.kind == 1);
107 gcc_assert (INTEGER_CST_P (length));
108 gcc_assert (TREE_INT_CST_HIGH (length) == 0);
110 len = TREE_INT_CST_LOW (length);
111 slen = expr->value.character.length;
115 s = gfc_getmem (len);
116 memcpy (s, expr->value.character.string, slen);
117 memset (&s[slen], ' ', len - slen);
118 str = gfc_build_string_const (len, s);
122 str = gfc_build_string_const (len, expr->value.character.string);
128 /* Create a tree node for the string length if it is constant. */
131 gfc_conv_const_charlen (gfc_charlen * cl)
133 if (cl->backend_decl)
136 if (cl->length && cl->length->expr_type == EXPR_CONSTANT)
138 cl->backend_decl = gfc_conv_mpz_to_tree (cl->length->value.integer,
139 cl->length->ts.kind);
140 cl->backend_decl = fold_convert (gfc_charlen_type_node,
146 gfc_init_constants (void)
150 for (n = 0; n <= GFC_MAX_DIMENSIONS; n++)
151 gfc_rank_cst[n] = build_int_cst (gfc_array_index_type, n);
154 /* Converts a GMP integer into a backend tree node. */
157 gfc_conv_mpz_to_tree (mpz_t i, int kind)
159 double_int val = mpz_get_double_int (gfc_get_int_type (kind), i, true);
160 return double_int_to_tree (gfc_get_int_type (kind), val);
163 /* Converts a backend tree into a GMP integer. */
166 gfc_conv_tree_to_mpz (mpz_t i, tree source)
168 double_int val = tree_to_double_int (source);
169 mpz_set_double_int (i, val, TYPE_UNSIGNED (TREE_TYPE (source)));
172 /* Converts a real constant into backend form. */
175 gfc_conv_mpfr_to_tree (mpfr_t f, int kind)
179 REAL_VALUE_TYPE real;
181 n = gfc_validate_kind (BT_REAL, kind, false);
182 gcc_assert (gfc_real_kinds[n].radix == 2);
184 type = gfc_get_real_type (kind);
185 real_from_mpfr (&real, f, type, GFC_RND_MODE);
186 return build_real (type, real);
189 /* Converts a backend tree into a real constant. */
192 gfc_conv_tree_to_mpfr (mpfr_ptr f, tree source)
194 mpfr_from_real (f, TREE_REAL_CST_PTR (source), GFC_RND_MODE);
197 /* Translate any literal constant to a tree. Constants never have
198 pre or post chains. Character literal constants are special
199 special because they have a value and a length, so they cannot be
200 returned as a single tree. It is up to the caller to set the
201 length somewhere if necessary.
203 Returns the translated constant, or aborts if it gets a type it
207 gfc_conv_constant_to_tree (gfc_expr * expr)
209 gcc_assert (expr->expr_type == EXPR_CONSTANT);
211 /* If it is has a prescribed memory representation, we build a string
212 constant and VIEW_CONVERT to its type. */
214 switch (expr->ts.type)
217 if (expr->representation.string)
218 return build1 (VIEW_CONVERT_EXPR,
219 gfc_get_int_type (expr->ts.kind),
220 gfc_build_string_const (expr->representation.length,
221 expr->representation.string));
223 return gfc_conv_mpz_to_tree (expr->value.integer, expr->ts.kind);
226 if (expr->representation.string)
227 return build1 (VIEW_CONVERT_EXPR,
228 gfc_get_real_type (expr->ts.kind),
229 gfc_build_string_const (expr->representation.length,
230 expr->representation.string));
232 return gfc_conv_mpfr_to_tree (expr->value.real, expr->ts.kind);
235 if (expr->representation.string)
236 return build1 (VIEW_CONVERT_EXPR,
237 gfc_get_logical_type (expr->ts.kind),
238 gfc_build_string_const (expr->representation.length,
239 expr->representation.string));
241 return build_int_cst (gfc_get_logical_type (expr->ts.kind),
242 expr->value.logical);
245 if (expr->representation.string)
246 return build1 (VIEW_CONVERT_EXPR,
247 gfc_get_complex_type (expr->ts.kind),
248 gfc_build_string_const (expr->representation.length,
249 expr->representation.string));
252 tree real = gfc_conv_mpfr_to_tree (expr->value.complex.r,
254 tree imag = gfc_conv_mpfr_to_tree (expr->value.complex.i,
257 return build_complex (gfc_typenode_for_spec (&expr->ts),
262 return gfc_build_string_const (expr->value.character.length,
263 expr->value.character.string);
266 return gfc_build_string_const (expr->representation.length,
267 expr->representation.string);
270 fatal_error ("gfc_conv_constant_to_tree(): invalid type: %s",
271 gfc_typename (&expr->ts));
276 /* Like gfc_conv_constant_to_tree, but for a simplified expression.
277 We can handle character literal constants here as well. */
280 gfc_conv_constant (gfc_se * se, gfc_expr * expr)
282 /* We may be receiving an expression for C_NULL_PTR or C_NULL_FUNPTR. If
283 so, they expr_type will not yet be an EXPR_CONSTANT. We need to make
285 if (expr->ts.type == BT_DERIVED && expr->ts.derived
286 && expr->ts.derived->attr.is_iso_c)
288 if (expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_PTR
289 || expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_FUNPTR)
291 /* Create a new EXPR_CONSTANT expression for our local uses. */
292 expr = gfc_int_expr (0);
296 gcc_assert (expr->expr_type == EXPR_CONSTANT);
300 gcc_assert (se->ss != gfc_ss_terminator);
301 gcc_assert (se->ss->type == GFC_SS_SCALAR);
302 gcc_assert (se->ss->expr == expr);
304 se->expr = se->ss->data.scalar.expr;
305 se->string_length = se->ss->string_length;
306 gfc_advance_se_ss_chain (se);
310 /* Translate the constant and put it in the simplifier structure. */
311 se->expr = gfc_conv_constant_to_tree (expr);
313 /* If this is a CHARACTER string, set its length in the simplifier
315 if (expr->ts.type == BT_CHARACTER)
316 se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));