OSDN Git Service

035f40b125cc73867f24e60c9e0b178c9399ccbf
[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 #include "target-memory.h"
37
38 tree gfc_rank_cst[GFC_MAX_DIMENSIONS + 1];
39
40 /* Build a constant with given type from an int_cst.  */
41
42 tree
43 gfc_build_const (tree type, tree intval)
44 {
45   tree val;
46   tree zero;
47
48   switch (TREE_CODE (type))
49     {
50     case INTEGER_TYPE:
51       val = convert (type, intval);
52       break;
53
54     case REAL_TYPE:
55       val = build_real_from_int_cst (type, intval);
56       break;
57
58     case COMPLEX_TYPE:
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);
62       break;
63
64     default:
65       gcc_unreachable ();
66     }
67   return val;
68 }
69
70 /* Build a string constant with C char type.  */
71
72 tree
73 gfc_build_string_const (int length, const char *s)
74 {
75   tree str;
76   tree len;
77
78   str = build_string (length, s);
79   len = build_int_cst (NULL_TREE, length);
80   TREE_TYPE (str) =
81     build_array_type (gfc_character1_type_node,
82                       build_range_type (gfc_charlen_type_node,
83                                         integer_one_node, len));
84   return str;
85 }
86
87
88 /* Build a string constant with a type given by its kind; take care of
89    non-default character kinds.  */
90
91 tree
92 gfc_build_wide_string_const (int kind, int length, const gfc_char_t *string)
93 {
94   int i;
95   tree str, len;
96   size_t size;
97   char *s;
98
99   i = gfc_validate_kind (BT_CHARACTER, kind, false);
100   size = length * gfc_character_kinds[i].bit_size / 8;
101
102   s = XCNEWVAR (char, size);
103   gfc_encode_character (kind, length, string, (unsigned char *) s, size);
104
105   str = build_string (size, s);
106   gfc_free (s);
107
108   len = build_int_cst (NULL_TREE, length);
109   TREE_TYPE (str) =
110     build_array_type (gfc_get_char_type (kind),
111                       build_range_type (gfc_charlen_type_node,
112                                         integer_one_node, len));
113   return str;
114 }
115
116
117 /* Build a Fortran character constant from a zero-terminated string.
118    There a two version of this function, one that translates the string
119    and one that doesn't.  */
120 tree
121 gfc_build_cstring_const (const char *string)
122 {
123   return gfc_build_string_const (strlen (string) + 1, string);
124 }
125
126 tree
127 gfc_build_localized_cstring_const (const char *msgid)
128 {
129   const char *localized = _(msgid);
130   return gfc_build_string_const (strlen (localized) + 1, localized);
131 }
132
133
134 /* Return a string constant with the given length.  Used for static
135    initializers.  The constant will be padded or truncated to match 
136    length.  */
137
138 tree
139 gfc_conv_string_init (tree length, gfc_expr * expr)
140 {
141   gfc_char_t *s;
142   HOST_WIDE_INT len;
143   int slen;
144   tree str;
145   bool free_s = false;
146
147   gcc_assert (expr->expr_type == EXPR_CONSTANT);
148   gcc_assert (expr->ts.type == BT_CHARACTER);
149   gcc_assert (INTEGER_CST_P (length));
150   gcc_assert (TREE_INT_CST_HIGH (length) == 0);
151
152   len = TREE_INT_CST_LOW (length);
153   slen = expr->value.character.length;
154
155   if (len > slen)
156     {
157       s = gfc_get_wide_string (len);
158       memcpy (s, expr->value.character.string, slen * sizeof (gfc_char_t));
159       gfc_wide_memset (&s[slen], ' ', len - slen);
160       free_s = true;
161     }
162   else
163     s = expr->value.character.string;
164
165   str = gfc_build_wide_string_const (expr->ts.kind, len, s);
166
167   if (free_s)
168     gfc_free (s);
169
170   return str;
171 }
172
173
174 /* Create a tree node for the string length if it is constant.  */
175
176 void
177 gfc_conv_const_charlen (gfc_charlen * cl)
178 {
179   if (!cl || cl->backend_decl)
180     return;
181
182   if (cl->length && cl->length->expr_type == EXPR_CONSTANT)
183     {
184       cl->backend_decl = gfc_conv_mpz_to_tree (cl->length->value.integer,
185                                                cl->length->ts.kind);
186       cl->backend_decl = fold_convert (gfc_charlen_type_node,
187                                         cl->backend_decl);
188     }
189 }
190
191 void
192 gfc_init_constants (void)
193 {
194   int n;
195
196   for (n = 0; n <= GFC_MAX_DIMENSIONS; n++)
197     gfc_rank_cst[n] = build_int_cst (gfc_array_index_type, n);
198 }
199
200 /* Converts a GMP integer into a backend tree node.  */
201
202 tree
203 gfc_conv_mpz_to_tree (mpz_t i, int kind)
204 {
205   double_int val = mpz_get_double_int (gfc_get_int_type (kind), i, true);
206   return double_int_to_tree (gfc_get_int_type (kind), val);
207 }
208
209 /* Converts a backend tree into a GMP integer.  */
210
211 void
212 gfc_conv_tree_to_mpz (mpz_t i, tree source)
213 {
214   double_int val = tree_to_double_int (source);
215   mpz_set_double_int (i, val, TYPE_UNSIGNED (TREE_TYPE (source)));
216 }
217
218 /* Converts a real constant into backend form.  */
219
220 tree
221 gfc_conv_mpfr_to_tree (mpfr_t f, int kind, int is_snan)
222 {
223   tree type;
224   int n;
225   REAL_VALUE_TYPE real;
226
227   n = gfc_validate_kind (BT_REAL, kind, false);
228   gcc_assert (gfc_real_kinds[n].radix == 2);
229
230   type = gfc_get_real_type (kind);
231   if (mpfr_nan_p (f) && is_snan)
232      real_from_string (&real, "SNaN");
233   else
234     real_from_mpfr (&real, f, type, GFC_RND_MODE);
235
236   return build_real (type, real);
237 }
238
239 /* Converts a backend tree into a real constant.  */
240
241 void
242 gfc_conv_tree_to_mpfr (mpfr_ptr f, tree source)
243 {
244   mpfr_from_real (f, TREE_REAL_CST_PTR (source), GFC_RND_MODE);
245 }
246
247 /* Translate any literal constant to a tree.  Constants never have
248    pre or post chains.  Character literal constants are special
249    special because they have a value and a length, so they cannot be
250    returned as a single tree.  It is up to the caller to set the
251    length somewhere if necessary.
252
253    Returns the translated constant, or aborts if it gets a type it
254    can't handle.  */
255
256 tree
257 gfc_conv_constant_to_tree (gfc_expr * expr)
258 {
259   tree res;
260
261   gcc_assert (expr->expr_type == EXPR_CONSTANT);
262
263   /* If it is has a prescribed memory representation, we build a string
264      constant and VIEW_CONVERT to its type.  */
265  
266   switch (expr->ts.type)
267     {
268     case BT_INTEGER:
269       if (expr->representation.string)
270         return fold_build1 (VIEW_CONVERT_EXPR,
271                             gfc_get_int_type (expr->ts.kind),
272                             gfc_build_string_const (expr->representation.length,
273                                                     expr->representation.string));
274       else
275         return gfc_conv_mpz_to_tree (expr->value.integer, expr->ts.kind);
276
277     case BT_REAL:
278       if (expr->representation.string)
279         return fold_build1 (VIEW_CONVERT_EXPR,
280                             gfc_get_real_type (expr->ts.kind),
281                             gfc_build_string_const (expr->representation.length,
282                                                     expr->representation.string));
283       else
284         return gfc_conv_mpfr_to_tree (expr->value.real, expr->ts.kind, expr->is_snan);
285
286     case BT_LOGICAL:
287       if (expr->representation.string)
288         {
289           tree tmp = fold_build1 (VIEW_CONVERT_EXPR,
290                                   gfc_get_int_type (expr->ts.kind),
291                                   gfc_build_string_const (expr->representation.length,
292                                                           expr->representation.string));
293           if (!integer_zerop (tmp) && !integer_onep (tmp))
294             gfc_warning ("Assigning value other than 0 or 1 to LOGICAL"
295                          " has undefined result at %L", &expr->where);
296           return fold_convert (gfc_get_logical_type (expr->ts.kind), tmp);
297         }
298       else
299         return build_int_cst (gfc_get_logical_type (expr->ts.kind),
300                               expr->value.logical);
301
302     case BT_COMPLEX:
303       if (expr->representation.string)
304         return fold_build1 (VIEW_CONVERT_EXPR,
305                             gfc_get_complex_type (expr->ts.kind),
306                             gfc_build_string_const (expr->representation.length,
307                                                     expr->representation.string));
308       else
309         {
310           tree real = gfc_conv_mpfr_to_tree (mpc_realref (expr->value.complex),
311                                           expr->ts.kind, expr->is_snan);
312           tree imag = gfc_conv_mpfr_to_tree (mpc_imagref (expr->value.complex),
313                                           expr->ts.kind, expr->is_snan);
314
315           return build_complex (gfc_typenode_for_spec (&expr->ts),
316                                 real, imag);
317         }
318
319     case BT_CHARACTER:
320       res = gfc_build_wide_string_const (expr->ts.kind,
321                                          expr->value.character.length,
322                                          expr->value.character.string);
323       return res;
324
325     case BT_HOLLERITH:
326       return gfc_build_string_const (expr->representation.length,
327                                      expr->representation.string);
328
329     default:
330       fatal_error ("gfc_conv_constant_to_tree(): invalid type: %s",
331                    gfc_typename (&expr->ts));
332     }
333 }
334
335
336 /* Like gfc_conv_constant_to_tree, but for a simplified expression.
337    We can handle character literal constants here as well.  */
338
339 void
340 gfc_conv_constant (gfc_se * se, gfc_expr * expr)
341 {
342   /* We may be receiving an expression for C_NULL_PTR or C_NULL_FUNPTR.  If
343      so, they expr_type will not yet be an EXPR_CONSTANT.  We need to make
344      it so here.  */
345   if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
346       && expr->ts.u.derived->attr.is_iso_c)
347     {
348       if (expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_PTR 
349           || expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_FUNPTR)
350         {
351           /* Create a new EXPR_CONSTANT expression for our local uses.  */
352           expr = gfc_int_expr (0);
353         }
354     }
355
356   gcc_assert (expr->expr_type == EXPR_CONSTANT);
357
358   if (se->ss != NULL)
359     {
360       gcc_assert (se->ss != gfc_ss_terminator);
361       gcc_assert (se->ss->type == GFC_SS_SCALAR);
362       gcc_assert (se->ss->expr == expr);
363
364       se->expr = se->ss->data.scalar.expr;
365       se->string_length = se->ss->string_length;
366       gfc_advance_se_ss_chain (se);
367       return;
368     }
369
370   /* Translate the constant and put it in the simplifier structure.  */
371   se->expr = gfc_conv_constant_to_tree (expr);
372
373   /* If this is a CHARACTER string, set its length in the simplifier
374      structure, too.  */
375   if (expr->ts.type == BT_CHARACTER)
376     se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));
377 }