OSDN Git Service

2008-04-06 Tobias Schlter <tobi@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / fortran / trans-const.c
1 /* Translation of constants
2    Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software
3    Foundation, Inc.
4    Contributed by Paul Brook
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>.  */
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"
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"
36
37 tree gfc_rank_cst[GFC_MAX_DIMENSIONS + 1];
38
39 /* Build a constant with given type from an int_cst.  */
40
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:
64       gcc_unreachable ();
65     }
66   return val;
67 }
68
69 tree
70 gfc_build_string_const (int length, const char *s)
71 {
72   tree str;
73   tree len;
74
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;
82 }
83
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)
89 {
90   return gfc_build_string_const (strlen (string) + 1, string);
91 }
92
93 tree
94 gfc_build_localized_cstring_const (const char *msgid)
95 {
96   const char *localized = _(msgid);
97   return gfc_build_string_const (strlen (localized) + 1, localized);
98 }
99
100
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.  */
104
105 tree
106 gfc_conv_string_init (tree length, gfc_expr * expr)
107 {
108   char *s;
109   HOST_WIDE_INT len;
110   int slen;
111   tree str;
112
113   gcc_assert (expr->expr_type == EXPR_CONSTANT);
114   gcc_assert (expr->ts.type == BT_CHARACTER && expr->ts.kind == 1);
115   gcc_assert (INTEGER_CST_P (length));
116   gcc_assert (TREE_INT_CST_HIGH (length) == 0);
117
118   len = TREE_INT_CST_LOW (length);
119   slen = expr->value.character.length;
120
121   if (len > slen)
122     {
123       s = gfc_getmem (len);
124       memcpy (s, expr->value.character.string, slen);
125       memset (&s[slen], ' ', len - slen);
126       str = gfc_build_string_const (len, s);
127       gfc_free (s);
128     }
129   else
130     str = gfc_build_string_const (len, expr->value.character.string);
131
132   return str;
133 }
134
135
136 /* Create a tree node for the string length if it is constant.  */
137
138 void
139 gfc_conv_const_charlen (gfc_charlen * cl)
140 {
141   if (cl->backend_decl)
142     return;
143
144   if (cl->length && cl->length->expr_type == EXPR_CONSTANT)
145     {
146       cl->backend_decl = gfc_conv_mpz_to_tree (cl->length->value.integer,
147                                                cl->length->ts.kind);
148       cl->backend_decl = fold_convert (gfc_charlen_type_node,
149                                         cl->backend_decl);
150     }
151 }
152
153 void
154 gfc_init_constants (void)
155 {
156   int n;
157
158   for (n = 0; n <= GFC_MAX_DIMENSIONS; n++)
159     gfc_rank_cst[n] = build_int_cst (gfc_array_index_type, n);
160 }
161
162 /* Converts a GMP integer into a backend tree node.  */
163
164 tree
165 gfc_conv_mpz_to_tree (mpz_t i, int kind)
166 {
167   double_int val = mpz_get_double_int (gfc_get_int_type (kind), i, true);
168   return double_int_to_tree (gfc_get_int_type (kind), val);
169 }
170
171 /* Converts a backend tree into a GMP integer.  */
172
173 void
174 gfc_conv_tree_to_mpz (mpz_t i, tree source)
175 {
176   double_int val = tree_to_double_int (source);
177   mpz_set_double_int (i, val, TYPE_UNSIGNED (TREE_TYPE (source)));
178 }
179
180 /* Converts a real constant into backend form.  */
181
182 tree
183 gfc_conv_mpfr_to_tree (mpfr_t f, int kind)
184 {
185   tree type;
186   int n;
187   REAL_VALUE_TYPE real;
188
189   n = gfc_validate_kind (BT_REAL, kind, false);
190   gcc_assert (gfc_real_kinds[n].radix == 2);
191
192   type = gfc_get_real_type (kind);
193   real_from_mpfr (&real, f, type, GFC_RND_MODE);
194   return build_real (type, real);
195 }
196
197 /* Converts a backend tree into a real constant.  */
198
199 void
200 gfc_conv_tree_to_mpfr (mpfr_ptr f, tree source)
201 {
202   mpfr_from_real (f, TREE_REAL_CST_PTR (source), GFC_RND_MODE);
203 }
204
205 /* Translate any literal constant to a tree.  Constants never have
206    pre or post chains.  Character literal constants are special
207    special because they have a value and a length, so they cannot be
208    returned as a single tree.  It is up to the caller to set the
209    length somewhere if necessary.
210
211    Returns the translated constant, or aborts if it gets a type it
212    can't handle.  */
213
214 tree
215 gfc_conv_constant_to_tree (gfc_expr * expr)
216 {
217   gcc_assert (expr->expr_type == EXPR_CONSTANT);
218
219   /* If it is has a prescribed memory representation, we build a string
220      constant and VIEW_CONVERT to its type.  */
221  
222   switch (expr->ts.type)
223     {
224     case BT_INTEGER:
225       if (expr->representation.string)
226         return fold_build1 (VIEW_CONVERT_EXPR,
227                             gfc_get_int_type (expr->ts.kind),
228                             gfc_build_string_const (expr->representation.length,
229                                                     expr->representation.string));
230       else
231         return gfc_conv_mpz_to_tree (expr->value.integer, expr->ts.kind);
232
233     case BT_REAL:
234       if (expr->representation.string)
235         return fold_build1 (VIEW_CONVERT_EXPR,
236                             gfc_get_real_type (expr->ts.kind),
237                             gfc_build_string_const (expr->representation.length,
238                                                     expr->representation.string));
239       else
240         return gfc_conv_mpfr_to_tree (expr->value.real, expr->ts.kind);
241
242     case BT_LOGICAL:
243       if (expr->representation.string)
244         return fold_build1 (VIEW_CONVERT_EXPR,
245                             gfc_get_logical_type (expr->ts.kind),
246                             gfc_build_string_const (expr->representation.length,
247                                                     expr->representation.string));
248       else
249         return build_int_cst (gfc_get_logical_type (expr->ts.kind),
250                             expr->value.logical);
251
252     case BT_COMPLEX:
253       if (expr->representation.string)
254         return fold_build1 (VIEW_CONVERT_EXPR,
255                             gfc_get_complex_type (expr->ts.kind),
256                             gfc_build_string_const (expr->representation.length,
257                                                     expr->representation.string));
258       else
259         {
260           tree real = gfc_conv_mpfr_to_tree (expr->value.complex.r,
261                                           expr->ts.kind);
262           tree imag = gfc_conv_mpfr_to_tree (expr->value.complex.i,
263                                           expr->ts.kind);
264
265           return build_complex (gfc_typenode_for_spec (&expr->ts),
266                                 real, imag);
267         }
268
269     case BT_CHARACTER:
270       return gfc_build_string_const (expr->value.character.length,
271                                      expr->value.character.string);
272
273     case BT_HOLLERITH:
274       return gfc_build_string_const (expr->representation.length,
275                                      expr->representation.string);
276
277     default:
278       fatal_error ("gfc_conv_constant_to_tree(): invalid type: %s",
279                    gfc_typename (&expr->ts));
280     }
281 }
282
283
284 /* Like gfc_conv_constant_to_tree, but for a simplified expression.
285    We can handle character literal constants here as well.  */
286
287 void
288 gfc_conv_constant (gfc_se * se, gfc_expr * expr)
289 {
290   /* We may be receiving an expression for C_NULL_PTR or C_NULL_FUNPTR.  If
291      so, they expr_type will not yet be an EXPR_CONSTANT.  We need to make
292      it so here.  */
293   if (expr->ts.type == BT_DERIVED && expr->ts.derived
294       && expr->ts.derived->attr.is_iso_c)
295     {
296       if (expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_PTR 
297           || expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_FUNPTR)
298         {
299           /* Create a new EXPR_CONSTANT expression for our local uses.  */
300           expr = gfc_int_expr (0);
301         }
302     }
303
304   gcc_assert (expr->expr_type == EXPR_CONSTANT);
305
306   if (se->ss != NULL)
307     {
308       gcc_assert (se->ss != gfc_ss_terminator);
309       gcc_assert (se->ss->type == GFC_SS_SCALAR);
310       gcc_assert (se->ss->expr == expr);
311
312       se->expr = se->ss->data.scalar.expr;
313       se->string_length = se->ss->string_length;
314       gfc_advance_se_ss_chain (se);
315       return;
316     }
317
318   /* Translate the constant and put it in the simplifier structure.  */
319   se->expr = gfc_conv_constant_to_tree (expr);
320
321   /* If this is a CHARACTER string, set its length in the simplifier
322      structure, too.  */
323   if (expr->ts.type == BT_CHARACTER)
324     se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));
325 }