OSDN Git Service

2007-05-24 H.J. Lu <hongjiu.lu@intel.com>
[pf3gnuchains/gcc-fork.git] / gcc / fortran / trans-const.c
1 /* Translation of constants
2    Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 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 2, 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 COPYING.  If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA.  */
22
23 /* trans-const.c -- convert constant values */
24
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tree.h"
29 #include "ggc.h"
30 #include "toplev.h"
31 #include "real.h"
32 #include "double-int.h"
33 #include "gfortran.h"
34 #include "trans.h"
35 #include "trans-const.h"
36 #include "trans-types.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 tree
71 gfc_build_string_const (int length, const char *s)
72 {
73   tree str;
74   tree len;
75
76   str = build_string (length, s);
77   len = build_int_cst (NULL_TREE, length);
78   TREE_TYPE (str) =
79     build_array_type (gfc_character1_type_node,
80                       build_range_type (gfc_charlen_type_node,
81                                         integer_one_node, len));
82   return str;
83 }
84
85 /* Build a Fortran character constant from a zero-terminated string.
86    Since this is mainly used for error messages, the string will get
87    translated.  */
88 tree
89 gfc_build_cstring_const (const char *msgid)
90 {
91   return gfc_build_string_const (strlen (msgid) + 1, _(msgid));
92 }
93
94 /* Return a string constant with the given length.  Used for static
95    initializers.  The constant will be padded or truncated to match 
96    length.  */
97
98 tree
99 gfc_conv_string_init (tree length, gfc_expr * expr)
100 {
101   char *s;
102   HOST_WIDE_INT len;
103   int slen;
104   tree str;
105
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);
110
111   len = TREE_INT_CST_LOW (length);
112   slen = expr->value.character.length;
113
114   if (len > slen)
115     {
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);
120       gfc_free (s);
121     }
122   else
123     str = gfc_build_string_const (len, expr->value.character.string);
124
125   return str;
126 }
127
128
129 /* Create a tree node for the string length if it is constant.  */
130
131 void
132 gfc_conv_const_charlen (gfc_charlen * cl)
133 {
134   if (cl->backend_decl)
135     return;
136
137   if (cl->length && cl->length->expr_type == EXPR_CONSTANT)
138     {
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,
142                                         cl->backend_decl);
143     }
144 }
145
146 void
147 gfc_init_constants (void)
148 {
149   int n;
150
151   for (n = 0; n <= GFC_MAX_DIMENSIONS; n++)
152     gfc_rank_cst[n] = build_int_cst (gfc_array_index_type, n);
153 }
154
155 /* Converts a GMP integer into a backend tree node.  */
156
157 tree
158 gfc_conv_mpz_to_tree (mpz_t i, int kind)
159 {
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);
162 }
163
164 /* Converts a backend tree into a GMP integer.  */
165
166 void
167 gfc_conv_tree_to_mpz (mpz_t i, tree source)
168 {
169   double_int val = tree_to_double_int (source);
170   mpz_set_double_int (i, val, TYPE_UNSIGNED (TREE_TYPE (source)));
171 }
172
173 /* Converts a real constant into backend form.  */
174
175 tree
176 gfc_conv_mpfr_to_tree (mpfr_t f, int kind)
177 {
178   tree type;
179   int n;
180   REAL_VALUE_TYPE real;
181
182   n = gfc_validate_kind (BT_REAL, kind, false);
183   gcc_assert (gfc_real_kinds[n].radix == 2);
184
185   type = gfc_get_real_type (kind);
186   real_from_mpfr (&real, f, type, GFC_RND_MODE);
187   return build_real (type, real);
188 }
189
190 /* Converts a backend tree into a real constant.  */
191
192 void
193 gfc_conv_tree_to_mpfr (mpfr_ptr f, tree source)
194 {
195   mpfr_from_real (f, TREE_REAL_CST_PTR (source), GFC_RND_MODE);
196 }
197
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.
203
204    Returns the translated constant, or aborts if it gets a type it
205    can't handle.  */
206
207 tree
208 gfc_conv_constant_to_tree (gfc_expr * expr)
209 {
210   gcc_assert (expr->expr_type == EXPR_CONSTANT);
211
212   /* If it is converted from Hollerith constant, we build string constant
213      and VIEW_CONVERT to its type.  */
214  
215   switch (expr->ts.type)
216     {
217     case BT_INTEGER:
218       if (expr->from_H)
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));
223       else
224         return gfc_conv_mpz_to_tree (expr->value.integer, expr->ts.kind);
225
226     case BT_REAL:
227       if (expr->from_H)
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));
232       else
233         return gfc_conv_mpfr_to_tree (expr->value.real, expr->ts.kind);
234
235     case BT_LOGICAL:
236       if (expr->from_H)
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));
241       else
242         return build_int_cst (gfc_get_logical_type (expr->ts.kind),
243                             expr->value.logical);
244
245     case BT_COMPLEX:
246       if (expr->from_H)
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));
251       else
252         {
253           tree real = gfc_conv_mpfr_to_tree (expr->value.complex.r,
254                                           expr->ts.kind);
255           tree imag = gfc_conv_mpfr_to_tree (expr->value.complex.i,
256                                           expr->ts.kind);
257
258           return build_complex (gfc_typenode_for_spec (&expr->ts),
259                                 real, imag);
260         }
261
262     case BT_CHARACTER:
263     case BT_HOLLERITH:
264       return gfc_build_string_const (expr->value.character.length,
265                                      expr->value.character.string);
266
267     default:
268       fatal_error ("gfc_conv_constant_to_tree(): invalid type: %s",
269                    gfc_typename (&expr->ts));
270     }
271 }
272
273
274 /* Like gfc_conv_constant_to_tree, but for a simplified expression.
275    We can handle character literal constants here as well.  */
276
277 void
278 gfc_conv_constant (gfc_se * se, gfc_expr * expr)
279 {
280   gcc_assert (expr->expr_type == EXPR_CONSTANT);
281
282   if (se->ss != NULL)
283     {
284       gcc_assert (se->ss != gfc_ss_terminator);
285       gcc_assert (se->ss->type == GFC_SS_SCALAR);
286       gcc_assert (se->ss->expr == expr);
287
288       se->expr = se->ss->data.scalar.expr;
289       se->string_length = se->ss->string_length;
290       gfc_advance_se_ss_chain (se);
291       return;
292     }
293
294   /* Translate the constant and put it in the simplifier structure.  */
295   se->expr = gfc_conv_constant_to_tree (expr);
296
297   /* If this is a CHARACTER string, set its length in the simplifier
298      structure, too.  */
299   if (expr->ts.type == BT_CHARACTER)
300     se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));
301 }