]> gcc.gnu.org Git - gcc.git/blame - gcc/fortran/trans-const.c
Update FSF address.
[gcc.git] / gcc / fortran / trans-const.c
CommitLineData
6de9cd9a 1/* Translation of constants
92574caf 2 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
6de9cd9a
DN
3 Contributed by Paul Brook
4
9fc4d79b 5This file is part of GCC.
6de9cd9a 6
9fc4d79b
TS
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 2, or (at your option) any later
10version.
6de9cd9a 11
9fc4d79b
TS
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
6de9cd9a
DN
16
17You should have received a copy of the GNU General Public License
9fc4d79b 18along with GCC; see the file COPYING. If not, write to the Free
ab57747b
KC
19Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
2002110-1301, USA. */
6de9cd9a
DN
21
22/* trans-const.c -- convert constant values */
23
24#include "config.h"
25#include "system.h"
26#include "coretypes.h"
27#include "tree.h"
6de9cd9a
DN
28#include "ggc.h"
29#include "toplev.h"
30#include "real.h"
6de9cd9a
DN
31#include "gfortran.h"
32#include "trans.h"
33#include "trans-const.h"
34#include "trans-types.h"
35
36/* String constants. */
37tree gfc_strconst_bounds;
38tree gfc_strconst_fault;
39tree gfc_strconst_wrong_return;
40tree gfc_strconst_current_filename;
41
42tree gfc_rank_cst[GFC_MAX_DIMENSIONS + 1];
43
44/* Build a constant with given type from an int_cst. */
f8d0aee5 45
6de9cd9a
DN
46tree
47gfc_build_const (tree type, tree intval)
48{
49 tree val;
50 tree zero;
51
52 switch (TREE_CODE (type))
53 {
54 case INTEGER_TYPE:
55 val = convert (type, intval);
56 break;
57
58 case REAL_TYPE:
59 val = build_real_from_int_cst (type, intval);
60 break;
61
62 case COMPLEX_TYPE:
63 val = build_real_from_int_cst (TREE_TYPE (type), intval);
64 zero = build_real_from_int_cst (TREE_TYPE (type), integer_zero_node);
65 val = build_complex (type, val, zero);
66 break;
67
68 default:
6e45f57b 69 gcc_unreachable ();
6de9cd9a
DN
70 }
71 return val;
72}
73
74tree
75gfc_build_string_const (int length, const char *s)
76{
77 tree str;
78 tree len;
79
80 str = build_string (length, s);
7d60be94 81 len = build_int_cst (NULL_TREE, length);
6de9cd9a
DN
82 TREE_TYPE (str) =
83 build_array_type (gfc_character1_type_node,
d7177ab2 84 build_range_type (gfc_charlen_type_node,
6de9cd9a
DN
85 integer_one_node, len));
86 return str;
87}
88
95638988
TS
89/* Build a Fortran character constant from a zero-terminated string. */
90
91tree
92gfc_build_cstring_const (const char *s)
93{
94 return gfc_build_string_const (strlen (s) + 1, s);
95}
96
6de9cd9a 97/* Return a string constant with the given length. Used for static
94716287
TS
98 initializers. The constant will be padded or truncated to match
99 length. */
100
6de9cd9a
DN
101tree
102gfc_conv_string_init (tree length, gfc_expr * expr)
103{
104 char *s;
105 HOST_WIDE_INT len;
106 int slen;
107 tree str;
108
6e45f57b
PB
109 gcc_assert (expr->expr_type == EXPR_CONSTANT);
110 gcc_assert (expr->ts.type == BT_CHARACTER && expr->ts.kind == 1);
111 gcc_assert (INTEGER_CST_P (length));
112 gcc_assert (TREE_INT_CST_HIGH (length) == 0);
6de9cd9a
DN
113
114 len = TREE_INT_CST_LOW (length);
115 slen = expr->value.character.length;
94716287
TS
116
117 if (len > slen)
6de9cd9a
DN
118 {
119 s = gfc_getmem (len);
120 memcpy (s, expr->value.character.string, slen);
121 memset (&s[slen], ' ', len - slen);
122 str = gfc_build_string_const (len, s);
123 gfc_free (s);
124 }
125 else
126 str = gfc_build_string_const (len, expr->value.character.string);
127
128 return str;
129}
130
131
132/* Create a tree node for the string length if it is constant. */
133
134void
135gfc_conv_const_charlen (gfc_charlen * cl)
136{
137 if (cl->backend_decl)
138 return;
139
140 if (cl->length && cl->length->expr_type == EXPR_CONSTANT)
141 {
142 cl->backend_decl = gfc_conv_mpz_to_tree (cl->length->value.integer,
143 cl->length->ts.kind);
1e7d0a64
FW
144 cl->backend_decl = fold_convert (gfc_charlen_type_node,
145 cl->backend_decl);
6de9cd9a
DN
146 }
147}
148
149void
150gfc_init_constants (void)
151{
152 int n;
153
154 for (n = 0; n <= GFC_MAX_DIMENSIONS; n++)
7d60be94 155 gfc_rank_cst[n] = build_int_cst (gfc_array_index_type, n);
6de9cd9a 156
95638988 157 gfc_strconst_bounds = gfc_build_cstring_const ("Array bound mismatch");
6de9cd9a
DN
158
159 gfc_strconst_fault =
95638988 160 gfc_build_cstring_const ("Array reference out of bounds");
6de9cd9a
DN
161
162 gfc_strconst_wrong_return =
95638988 163 gfc_build_cstring_const ("Incorrect function return value");
6de9cd9a
DN
164
165 gfc_strconst_current_filename =
95638988 166 gfc_build_cstring_const (gfc_option.source);
6de9cd9a
DN
167}
168
6de9cd9a
DN
169/* Converts a GMP integer into a backend tree node. */
170tree
171gfc_conv_mpz_to_tree (mpz_t i, int kind)
172{
6de9cd9a
DN
173 HOST_WIDE_INT high;
174 unsigned HOST_WIDE_INT low;
6de9cd9a 175
6de9cd9a
DN
176 if (mpz_fits_slong_p (i))
177 {
5f0ae953
RH
178 /* Note that HOST_WIDE_INT is never smaller than long. */
179 low = mpz_get_si (i);
180 high = mpz_sgn (i) < 0 ? -1 : 0;
6de9cd9a 181 }
6de9cd9a 182 else
6de9cd9a 183 {
04204c2f
RH
184 unsigned HOST_WIDE_INT words[2];
185 size_t count;
5f0ae953 186
04204c2f 187 /* Since we know that the value is not zero (mpz_fits_slong_p),
6c08eb63 188 we know that at least one word will be written, but we don't know
04204c2f 189 about the second. It's quicker to zero the second word before
6c08eb63 190 than conditionally clear it later. */
04204c2f
RH
191 words[1] = 0;
192
193 /* Extract the absolute value into words. */
194 mpz_export (words, &count, -1, sizeof (HOST_WIDE_INT), 0, 0, i);
195
196 /* We assume that all numbers are in range for its type, and that
197 we never create a type larger than 2*HWI, which is the largest
198 that the middle-end can handle. */
6e45f57b 199 gcc_assert (count == 1 || count == 2);
04204c2f
RH
200
201 low = words[0];
202 high = words[1];
5f0ae953 203
04204c2f 204 /* Negate if necessary. */
5f0ae953
RH
205 if (mpz_sgn (i) < 0)
206 {
207 if (low == 0)
208 high = -high;
209 else
210 low = -low, high = ~high;
211 }
6de9cd9a 212 }
6de9cd9a 213
0cc4be67 214 return build_int_cst_wide (gfc_get_int_type (kind), low, high);
6de9cd9a
DN
215}
216
217/* Converts a real constant into backend form. Uses an intermediate string
218 representation. */
f8d0aee5 219
6de9cd9a 220tree
f8e566e5 221gfc_conv_mpfr_to_tree (mpfr_t f, int kind)
6de9cd9a
DN
222{
223 tree res;
224 tree type;
225 mp_exp_t exp;
c41993e8 226 char *p, *q;
6de9cd9a 227 int n;
6de9cd9a 228
855a145c 229 n = gfc_validate_kind (BT_REAL, kind, false);
6de9cd9a 230
855a145c
TS
231 gcc_assert (gfc_real_kinds[n].radix == 2);
232
233 /* mpfr chooses too small a number of hexadecimal digits if the
234 number of binary digits is not divisible by four, therefore we
235 have to explicitly request a sufficient number of digits here. */
236 p = mpfr_get_str (NULL, &exp, 16, gfc_real_kinds[n].digits / 4 + 1,
c41993e8 237 f, GFC_RND_MODE);
f8e566e5 238
beceb652 239 /* REAL_VALUE_ATOF expects the exponent for mantissa * 2**exp,
855a145c
TS
240 mpfr_get_str returns the exponent for mantissa * 16**exp, adjust
241 for that. */
242 exp *= 4;
243
244 /* The additional 12 characters add space for the sprintf below.
245 This leaves 6 digits for the exponent which is certainly enough. */
246 q = (char *) gfc_getmem (strlen (p) + 12);
6de9cd9a 247
c41993e8 248 if (p[0] == '-')
855a145c 249 sprintf (q, "-0x.%sp%d", &p[1], (int) exp);
6de9cd9a 250 else
855a145c 251 sprintf (q, "0x.%sp%d", p, (int) exp);
6de9cd9a
DN
252
253 type = gfc_get_real_type (kind);
254 res = build_real (type, REAL_VALUE_ATOF (q, TYPE_MODE (type)));
f8e566e5 255
6de9cd9a
DN
256 gfc_free (q);
257 gfc_free (p);
258
259 return res;
260}
261
262
263/* Translate any literal constant to a tree. Constants never have
264 pre or post chains. Character literal constants are special
265 special because they have a value and a length, so they cannot be
266 returned as a single tree. It is up to the caller to set the
267 length somewhere if necessary.
268
269 Returns the translated constant, or aborts if it gets a type it
270 can't handle. */
271
272tree
273gfc_conv_constant_to_tree (gfc_expr * expr)
274{
6e45f57b 275 gcc_assert (expr->expr_type == EXPR_CONSTANT);
6de9cd9a
DN
276
277 switch (expr->ts.type)
278 {
279 case BT_INTEGER:
280 return gfc_conv_mpz_to_tree (expr->value.integer, expr->ts.kind);
281
282 case BT_REAL:
f8e566e5 283 return gfc_conv_mpfr_to_tree (expr->value.real, expr->ts.kind);
6de9cd9a
DN
284
285 case BT_LOGICAL:
6ba692b4
TS
286 return build_int_cst (gfc_get_logical_type (expr->ts.kind),
287 expr->value.logical);
6de9cd9a
DN
288
289 case BT_COMPLEX:
290 {
f8e566e5 291 tree real = gfc_conv_mpfr_to_tree (expr->value.complex.r,
6de9cd9a 292 expr->ts.kind);
f8e566e5 293 tree imag = gfc_conv_mpfr_to_tree (expr->value.complex.i,
6de9cd9a
DN
294 expr->ts.kind);
295
19db01bb
TS
296 return build_complex (gfc_typenode_for_spec (&expr->ts),
297 real, imag);
6de9cd9a
DN
298 }
299
300 case BT_CHARACTER:
301 return gfc_build_string_const (expr->value.character.length,
302 expr->value.character.string);
303
304 default:
305 fatal_error ("gfc_conv_constant_to_tree(): invalid type: %s",
306 gfc_typename (&expr->ts));
307 }
308}
309
310
f8d0aee5 311/* Like gfc_conv_constant_to_tree, but for a simplified expression.
6de9cd9a
DN
312 We can handle character literal constants here as well. */
313
314void
315gfc_conv_constant (gfc_se * se, gfc_expr * expr)
316{
6e45f57b 317 gcc_assert (expr->expr_type == EXPR_CONSTANT);
6de9cd9a
DN
318
319 if (se->ss != NULL)
320 {
6e45f57b
PB
321 gcc_assert (se->ss != gfc_ss_terminator);
322 gcc_assert (se->ss->type == GFC_SS_SCALAR);
323 gcc_assert (se->ss->expr == expr);
6de9cd9a
DN
324
325 se->expr = se->ss->data.scalar.expr;
40f20186 326 se->string_length = se->ss->string_length;
6de9cd9a
DN
327 gfc_advance_se_ss_chain (se);
328 return;
329 }
330
331 /* Translate the constant and put it in the simplifier structure. */
332 se->expr = gfc_conv_constant_to_tree (expr);
333
f8d0aee5 334 /* If this is a CHARACTER string, set its length in the simplifier
6de9cd9a
DN
335 structure, too. */
336 if (expr->ts.type == BT_CHARACTER)
337 se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));
338}
This page took 0.518931 seconds and 5 git commands to generate.