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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
23 /* trans-const.c -- convert constant values */
27 #include "coretypes.h"
32 #include "double-int.h"
35 #include "trans-const.h"
36 #include "trans-types.h"
38 tree gfc_rank_cst[GFC_MAX_DIMENSIONS + 1];
40 /* Build a constant with given type from an int_cst. */
43 gfc_build_const (tree type, tree intval)
48 switch (TREE_CODE (type))
51 val = convert (type, intval);
55 val = build_real_from_int_cst (type, intval);
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);
71 gfc_build_string_const (int length, const char *s)
76 str = build_string (length, s);
77 len = build_int_cst (NULL_TREE, length);
79 build_array_type (gfc_character1_type_node,
80 build_range_type (gfc_charlen_type_node,
81 integer_one_node, len));
85 /* Build a Fortran character constant from a zero-terminated string.
86 Since this is mainly used for error messages, the string will get
89 gfc_build_cstring_const (const char *msgid)
91 return gfc_build_string_const (strlen (msgid) + 1, _(msgid));
94 /* Return a string constant with the given length. Used for static
95 initializers. The constant will be padded or truncated to match
99 gfc_conv_string_init (tree length, gfc_expr * expr)
106 gcc_assert (expr->expr_type == EXPR_CONSTANT);
107 gcc_assert (expr->ts.type == BT_CHARACTER && expr->ts.kind == 1);
108 gcc_assert (INTEGER_CST_P (length));
109 gcc_assert (TREE_INT_CST_HIGH (length) == 0);
111 len = TREE_INT_CST_LOW (length);
112 slen = expr->value.character.length;
116 s = gfc_getmem (len);
117 memcpy (s, expr->value.character.string, slen);
118 memset (&s[slen], ' ', len - slen);
119 str = gfc_build_string_const (len, s);
123 str = gfc_build_string_const (len, expr->value.character.string);
129 /* Create a tree node for the string length if it is constant. */
132 gfc_conv_const_charlen (gfc_charlen * cl)
134 if (cl->backend_decl)
137 if (cl->length && cl->length->expr_type == EXPR_CONSTANT)
139 cl->backend_decl = gfc_conv_mpz_to_tree (cl->length->value.integer,
140 cl->length->ts.kind);
141 cl->backend_decl = fold_convert (gfc_charlen_type_node,
147 gfc_init_constants (void)
151 for (n = 0; n <= GFC_MAX_DIMENSIONS; n++)
152 gfc_rank_cst[n] = build_int_cst (gfc_array_index_type, n);
155 /* Converts a GMP integer into a backend tree node. */
158 gfc_conv_mpz_to_tree (mpz_t i, int kind)
160 double_int val = mpz_get_double_int (gfc_get_int_type (kind), i, true);
161 return double_int_to_tree (gfc_get_int_type (kind), val);
164 /* Converts a backend tree into a GMP integer. */
167 gfc_conv_tree_to_mpz (mpz_t i, tree source)
169 double_int val = tree_to_double_int (source);
170 mpz_set_double_int (i, val, TYPE_UNSIGNED (TREE_TYPE (source)));
173 /* Converts a real constant into backend form. */
176 gfc_conv_mpfr_to_tree (mpfr_t f, int kind)
180 REAL_VALUE_TYPE real;
182 n = gfc_validate_kind (BT_REAL, kind, false);
183 gcc_assert (gfc_real_kinds[n].radix == 2);
185 type = gfc_get_real_type (kind);
186 real_from_mpfr (&real, f, type, GFC_RND_MODE);
187 return build_real (type, real);
190 /* Converts a backend tree into a real constant. */
193 gfc_conv_tree_to_mpfr (mpfr_ptr f, tree source)
195 mpfr_from_real (f, TREE_REAL_CST_PTR (source), GFC_RND_MODE);
198 /* Translate any literal constant to a tree. Constants never have
199 pre or post chains. Character literal constants are special
200 special because they have a value and a length, so they cannot be
201 returned as a single tree. It is up to the caller to set the
202 length somewhere if necessary.
204 Returns the translated constant, or aborts if it gets a type it
208 gfc_conv_constant_to_tree (gfc_expr * expr)
210 gcc_assert (expr->expr_type == EXPR_CONSTANT);
212 /* If it is converted from Hollerith constant, we build string constant
213 and VIEW_CONVERT to its type. */
215 switch (expr->ts.type)
219 return build1 (VIEW_CONVERT_EXPR,
220 gfc_get_int_type (expr->ts.kind),
221 gfc_build_string_const (expr->value.character.length,
222 expr->value.character.string));
224 return gfc_conv_mpz_to_tree (expr->value.integer, expr->ts.kind);
228 return build1 (VIEW_CONVERT_EXPR,
229 gfc_get_real_type (expr->ts.kind),
230 gfc_build_string_const (expr->value.character.length,
231 expr->value.character.string));
233 return gfc_conv_mpfr_to_tree (expr->value.real, expr->ts.kind);
237 return build1 (VIEW_CONVERT_EXPR,
238 gfc_get_logical_type (expr->ts.kind),
239 gfc_build_string_const (expr->value.character.length,
240 expr->value.character.string));
242 return build_int_cst (gfc_get_logical_type (expr->ts.kind),
243 expr->value.logical);
247 return build1 (VIEW_CONVERT_EXPR,
248 gfc_get_complex_type (expr->ts.kind),
249 gfc_build_string_const (expr->value.character.length,
250 expr->value.character.string));
253 tree real = gfc_conv_mpfr_to_tree (expr->value.complex.r,
255 tree imag = gfc_conv_mpfr_to_tree (expr->value.complex.i,
258 return build_complex (gfc_typenode_for_spec (&expr->ts),
264 return gfc_build_string_const (expr->value.character.length,
265 expr->value.character.string);
268 fatal_error ("gfc_conv_constant_to_tree(): invalid type: %s",
269 gfc_typename (&expr->ts));
274 /* Like gfc_conv_constant_to_tree, but for a simplified expression.
275 We can handle character literal constants here as well. */
278 gfc_conv_constant (gfc_se * se, gfc_expr * expr)
280 gcc_assert (expr->expr_type == EXPR_CONSTANT);
284 gcc_assert (se->ss != gfc_ss_terminator);
285 gcc_assert (se->ss->type == GFC_SS_SCALAR);
286 gcc_assert (se->ss->expr == expr);
288 se->expr = se->ss->data.scalar.expr;
289 se->string_length = se->ss->string_length;
290 gfc_advance_se_ss_chain (se);
294 /* Translate the constant and put it in the simplifier structure. */
295 se->expr = gfc_conv_constant_to_tree (expr);
297 /* If this is a CHARACTER string, set its length in the simplifier
299 if (expr->ts.type == BT_CHARACTER)
300 se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));