OSDN Git Service

PR java/17329:
[pf3gnuchains/gcc-fork.git] / gcc / java / java-gimplify.c
1 /* Java(TM) language-specific gimplification routines.
2    Copyright (C) 2003, 2004 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. 
20
21 Java and all Java-based marks are trademarks or registered trademarks
22 of Sun Microsystems, Inc. in the United States and other countries.
23 The Free Software Foundation is independent of Sun Microsystems, Inc.  */
24
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "tree.h"
30 #include "java-tree.h"
31 #include "tree-dump.h"
32 #include "tree-gimple.h"
33 #include "toplev.h"
34
35 static tree java_gimplify_case_expr (tree);
36 static tree java_gimplify_default_expr (tree);
37 static tree java_gimplify_block (tree);
38 static tree java_gimplify_new_array_init (tree);
39 static tree java_gimplify_try_expr (tree);
40 static tree java_gimplify_modify_expr (tree);
41
42 static void dump_java_tree (enum tree_dump_index, tree);
43
44 /* Convert a Java tree to GENERIC.  */
45
46 void
47 java_genericize (tree fndecl)
48 {
49   dump_java_tree (TDI_original, fndecl);
50
51   /* Genericize with the gimplifier.  */
52   gimplify_function_tree (fndecl);
53
54   dump_function (TDI_generic, fndecl);
55 }
56
57 /* Gimplify a Java tree.  */
58
59 int
60 java_gimplify_expr (tree *expr_p, tree *pre_p ATTRIBUTE_UNUSED,
61                     tree *post_p ATTRIBUTE_UNUSED)
62 {
63   enum tree_code code = TREE_CODE (*expr_p);
64
65   switch (code)
66     {
67     case BLOCK:
68       *expr_p = java_gimplify_block (*expr_p);
69       break;
70
71     case EXPR_WITH_FILE_LOCATION:
72       input_location.file = EXPR_WFL_FILENAME (*expr_p);
73       input_location.line = EXPR_WFL_LINENO (*expr_p);
74       *expr_p = EXPR_WFL_NODE (*expr_p);
75       annotate_with_locus (*expr_p, input_location);
76       break;
77
78     case CASE_EXPR:
79       *expr_p = java_gimplify_case_expr (*expr_p);
80       break;
81
82     case DEFAULT_EXPR:
83       *expr_p = java_gimplify_default_expr (*expr_p);
84       break;
85
86     case NEW_ARRAY_INIT:
87       *expr_p = java_gimplify_new_array_init (*expr_p);
88       break;
89
90     case TRY_EXPR:
91       *expr_p = java_gimplify_try_expr (*expr_p);
92       break;
93
94     case JAVA_CATCH_EXPR:
95       *expr_p = TREE_OPERAND (*expr_p, 0);
96       break;
97
98     case JAVA_EXC_OBJ_EXPR:
99       *expr_p = build_exception_object_ref (TREE_TYPE (*expr_p));
100       break;
101
102     case VAR_DECL:
103       *expr_p = java_replace_reference (*expr_p, /* want_lvalue */ false);
104       return GS_UNHANDLED;
105
106     case MODIFY_EXPR:
107       *expr_p = java_gimplify_modify_expr (*expr_p);
108       return GS_UNHANDLED;
109
110     case SAVE_EXPR:
111       /* Note that we can see <save_expr NULL> if the save_expr was
112          already handled by gimplify_save_expr.  */
113       if (TREE_OPERAND (*expr_p, 0) != NULL_TREE
114           && TREE_CODE (TREE_OPERAND (*expr_p, 0)) == VAR_DECL)
115         TREE_OPERAND (*expr_p, 0) 
116           = java_replace_reference (TREE_OPERAND (*expr_p, 0), 
117                                /* want_lvalue */ false);
118       return GS_UNHANDLED;
119
120     /* These should already be lowered before we get here.  */
121     case URSHIFT_EXPR:
122     case COMPARE_EXPR:
123     case COMPARE_L_EXPR:
124     case COMPARE_G_EXPR:
125     case UNARY_PLUS_EXPR:
126     case NEW_ARRAY_EXPR:
127     case NEW_ANONYMOUS_ARRAY_EXPR:
128     case NEW_CLASS_EXPR:
129     case THIS_EXPR:
130     case SYNCHRONIZED_EXPR:
131     case CONDITIONAL_EXPR:
132     case INSTANCEOF_EXPR:
133     case CLASS_LITERAL:
134       abort ();
135
136     default:
137       /* Java insists on strict left-to-right evaluation of expressions.
138          A problem may arise if a variable used in the LHS of a binary
139          operation is altered by an assignment to that value in the RHS
140          before we've performed the operation.  So, we always copy every
141          LHS to a temporary variable.  
142
143          FIXME: Are there any other cases where we should do this?
144          Parameter lists, maybe?  Or perhaps that's unnecessary because
145          the front end already generates SAVE_EXPRs.  */
146
147       if (TREE_CODE_CLASS (code) == tcc_binary
148           || TREE_CODE_CLASS (code) == tcc_comparison)
149         {
150           enum gimplify_status stat 
151             = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
152                              is_gimple_formal_tmp_var, fb_rvalue);
153           if (stat == GS_ERROR)
154             return stat;
155         }
156
157       return GS_UNHANDLED;
158     }
159
160   return GS_OK;
161 }
162
163 /* This is specific to the bytecode compiler.  If a variable has
164    LOCAL_SLOT_P set, replace an assignment to it with an assignment to
165    the corresponding variable that holds all its aliases.  */
166
167 static tree
168 java_gimplify_modify_expr (tree modify_expr)
169 {
170   tree lhs = TREE_OPERAND (modify_expr, 0);
171   tree rhs = TREE_OPERAND (modify_expr, 1);
172   tree lhs_type = TREE_TYPE (lhs);
173   
174   if (TREE_CODE (lhs) == VAR_DECL
175       && DECL_LANG_SPECIFIC (lhs)
176       && LOCAL_SLOT_P (lhs)
177       && TREE_CODE (lhs_type) == POINTER_TYPE)
178     {
179       tree new_lhs = java_replace_reference (lhs, /* want_lvalue */ true);
180       tree new_rhs = build1 (NOP_EXPR, TREE_TYPE (new_lhs), rhs);
181       modify_expr = build2 (MODIFY_EXPR, TREE_TYPE (new_lhs),
182                             new_lhs, new_rhs);
183       modify_expr = build1 (NOP_EXPR, lhs_type, modify_expr);
184     }
185   
186   return modify_expr;
187 }
188
189     
190 static tree
191 java_gimplify_case_expr (tree expr)
192 {
193   tree label = create_artificial_label ();
194   return build3 (CASE_LABEL_EXPR, void_type_node,
195                  TREE_OPERAND (expr, 0), NULL_TREE, label);
196 }
197
198 static tree
199 java_gimplify_default_expr (tree expr ATTRIBUTE_UNUSED)
200 {
201   tree label = create_artificial_label ();
202   return build3 (CASE_LABEL_EXPR, void_type_node, NULL_TREE, NULL_TREE, label);
203 }
204
205 /* Gimplify BLOCK into a BIND_EXPR.  */
206
207 static tree
208 java_gimplify_block (tree java_block)
209 {
210   tree decls = BLOCK_VARS (java_block);
211   tree body = BLOCK_EXPR_BODY (java_block);
212   tree outer = gimple_current_bind_expr ();
213   tree block;
214
215   /* Don't bother with empty blocks.  */
216   if (! body)
217     return build_empty_stmt ();
218
219   if (IS_EMPTY_STMT (body))
220     return body;
221
222   /* Make a proper block.  Java blocks are unsuitable for BIND_EXPR
223      because they use BLOCK_SUBBLOCKS for another purpose.  */
224   block = make_node (BLOCK);
225   BLOCK_VARS (block) = decls;
226
227   /* The TREE_USED flag on a block determines whether the debug ouput
228      routines generate info for the variables in that block.  */
229   TREE_USED (block) = 1;
230
231   if (outer != NULL_TREE)
232     {
233       outer = BIND_EXPR_BLOCK (outer);
234       BLOCK_SUBBLOCKS (outer) = chainon (BLOCK_SUBBLOCKS (outer), block);
235     }
236
237   return build3 (BIND_EXPR, TREE_TYPE (java_block), decls, body, block);
238 }
239
240 /* Gimplify a NEW_ARRAY_INIT node into array/element assignments.  */
241
242 static tree
243 java_gimplify_new_array_init (tree exp)
244 {
245   tree array_type = TREE_TYPE (TREE_TYPE (exp));
246   tree data_field = lookup_field (&array_type, get_identifier ("data"));
247   tree element_type = TYPE_ARRAY_ELEMENT (array_type);
248   HOST_WIDE_INT ilength = java_array_type_length (array_type);
249   tree length = build_int_cst (NULL_TREE, ilength);
250   tree init = TREE_OPERAND (exp, 0);
251   tree values = CONSTRUCTOR_ELTS (init);
252
253   tree array_ptr_type = build_pointer_type (array_type);
254   tree tmp = create_tmp_var (array_ptr_type, "array");
255   tree body = build2 (MODIFY_EXPR, array_ptr_type, tmp,
256                       build_new_array (element_type, length));
257
258   int index = 0;
259
260   /* FIXME: try to allocate array statically?  */
261   while (values != NULL_TREE)
262     {
263       /* FIXME: Should use build_java_arrayaccess here, but avoid
264          bounds checking.  */
265       tree lhs = build3 (COMPONENT_REF, TREE_TYPE (data_field),    
266                          build_java_indirect_ref (array_type, tmp, 0),
267                          data_field, NULL_TREE);
268       tree assignment = build2 (MODIFY_EXPR, element_type,
269                                 build4 (ARRAY_REF, element_type, lhs,
270                                         build_int_cst (NULL_TREE, index++),
271                                         NULL_TREE, NULL_TREE),
272                                 TREE_VALUE (values));
273       body = build2 (COMPOUND_EXPR, element_type, body, assignment);
274       values = TREE_CHAIN (values);
275     }
276
277   return build2 (COMPOUND_EXPR, array_ptr_type, body, tmp);
278 }
279
280 static tree
281 java_gimplify_try_expr (tree try_expr)
282 {
283   tree body = TREE_OPERAND (try_expr, 0);
284   tree handler = TREE_OPERAND (try_expr, 1);
285   tree catch = NULL_TREE;
286
287   /* Build a CATCH_EXPR for each handler.  */
288   while (handler)
289     {
290       tree java_catch = TREE_OPERAND (handler, 0);
291       tree catch_type = TREE_TYPE (TREE_TYPE (BLOCK_EXPR_DECLS (java_catch)));
292       tree expr = build2 (CATCH_EXPR, void_type_node,
293                           prepare_eh_table_type (catch_type),
294                           handler);
295       if (catch)
296         catch = build2 (COMPOUND_EXPR, void_type_node, catch, expr);
297       else
298         catch = expr;
299       handler = TREE_CHAIN (handler);
300     }
301   return build2 (TRY_CATCH_EXPR, void_type_node, body, catch);
302 }
303
304 /* Dump a tree of some kind.  This is a convenience wrapper for the
305    dump_* functions in tree-dump.c.  */
306 static void
307 dump_java_tree (enum tree_dump_index phase, tree t)
308 {
309   FILE *stream;
310   int flags;
311
312   stream = dump_begin (phase, &flags);
313   flags |= TDF_SLIM;
314   if (stream)
315     {
316       dump_node (t, flags, stream);
317       dump_end (phase, stream);
318     }
319 }