OSDN Git Service

PR java/19742:
[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_labeled_block_expr (tree);
36 static tree java_gimplify_exit_block_expr (tree);
37 static tree java_gimplify_case_expr (tree);
38 static tree java_gimplify_default_expr (tree);
39 static tree java_gimplify_block (tree);
40 static tree java_gimplify_new_array_init (tree);
41 static tree java_gimplify_try_expr (tree);
42 static tree java_gimplify_modify_expr (tree);
43
44 static void dump_java_tree (enum tree_dump_index, tree);
45
46 /* Convert a Java tree to GENERIC.  */
47
48 void
49 java_genericize (tree fndecl)
50 {
51   dump_java_tree (TDI_original, fndecl);
52
53   /* Genericize with the gimplifier.  */
54   gimplify_function_tree (fndecl);
55
56   dump_function (TDI_generic, fndecl);
57 }
58
59 /* Gimplify a Java tree.  */
60
61 int
62 java_gimplify_expr (tree *expr_p, tree *pre_p ATTRIBUTE_UNUSED,
63                     tree *post_p ATTRIBUTE_UNUSED)
64 {
65   enum tree_code code = TREE_CODE (*expr_p);
66
67   switch (code)
68     {
69     case BLOCK:
70       *expr_p = java_gimplify_block (*expr_p);
71       break;
72
73     case EXPR_WITH_FILE_LOCATION:
74 #ifdef USE_MAPPED_LOCATION
75       input_location = EXPR_LOCATION (*expr_p);
76 #else
77       input_location.file = EXPR_WFL_FILENAME (*expr_p);
78       input_location.line = EXPR_WFL_LINENO (*expr_p);
79 #endif
80       *expr_p = EXPR_WFL_NODE (*expr_p);
81       if (EXPR_P (*expr_p))
82         SET_EXPR_LOCATION (*expr_p, input_location);
83       break;
84
85     case LABELED_BLOCK_EXPR:
86       *expr_p = java_gimplify_labeled_block_expr (*expr_p);
87       break;
88
89     case EXIT_BLOCK_EXPR:
90       *expr_p = java_gimplify_exit_block_expr (*expr_p);
91       break;
92
93     case CASE_EXPR:
94       *expr_p = java_gimplify_case_expr (*expr_p);
95       break;
96
97     case DEFAULT_EXPR:
98       *expr_p = java_gimplify_default_expr (*expr_p);
99       break;
100
101     case NEW_ARRAY_INIT:
102       *expr_p = java_gimplify_new_array_init (*expr_p);
103       break;
104
105     case TRY_EXPR:
106       *expr_p = java_gimplify_try_expr (*expr_p);
107       break;
108
109     case JAVA_CATCH_EXPR:
110       *expr_p = TREE_OPERAND (*expr_p, 0);
111       break;
112
113     case JAVA_EXC_OBJ_EXPR:
114       *expr_p = build_exception_object_ref (TREE_TYPE (*expr_p));
115       break;
116
117     case VAR_DECL:
118       *expr_p = java_replace_reference (*expr_p, /* want_lvalue */ false);
119       return GS_UNHANDLED;
120
121     case MODIFY_EXPR:
122       *expr_p = java_gimplify_modify_expr (*expr_p);
123       return GS_UNHANDLED;
124
125     case SAVE_EXPR:
126       /* Note that we can see <save_expr NULL> if the save_expr was
127          already handled by gimplify_save_expr.  */
128       if (TREE_OPERAND (*expr_p, 0) != NULL_TREE
129           && TREE_CODE (TREE_OPERAND (*expr_p, 0)) == VAR_DECL)
130         TREE_OPERAND (*expr_p, 0) 
131           = java_replace_reference (TREE_OPERAND (*expr_p, 0), 
132                                /* want_lvalue */ false);
133       return GS_UNHANDLED;
134
135     /* These should already be lowered before we get here.  */
136     case URSHIFT_EXPR:
137     case COMPARE_EXPR:
138     case COMPARE_L_EXPR:
139     case COMPARE_G_EXPR:
140     case UNARY_PLUS_EXPR:
141     case NEW_ARRAY_EXPR:
142     case NEW_ANONYMOUS_ARRAY_EXPR:
143     case NEW_CLASS_EXPR:
144     case THIS_EXPR:
145     case SYNCHRONIZED_EXPR:
146     case CONDITIONAL_EXPR:
147     case INSTANCEOF_EXPR:
148     case CLASS_LITERAL:
149       abort ();
150
151     default:
152       /* Java insists on strict left-to-right evaluation of expressions.
153          A problem may arise if a variable used in the LHS of a binary
154          operation is altered by an assignment to that value in the RHS
155          before we've performed the operation.  So, we always copy every
156          LHS to a temporary variable.  
157
158          FIXME: Are there any other cases where we should do this?
159          Parameter lists, maybe?  Or perhaps that's unnecessary because
160          the front end already generates SAVE_EXPRs.  */
161
162       if (TREE_CODE_CLASS (code) == tcc_binary
163           || TREE_CODE_CLASS (code) == tcc_comparison)
164         {
165           enum gimplify_status stat 
166             = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
167                              is_gimple_formal_tmp_var, fb_rvalue);
168           if (stat == GS_ERROR)
169             return stat;
170         }
171
172       return GS_UNHANDLED;
173     }
174
175   return GS_OK;
176 }
177
178 /* Gimplify a LABELED_BLOCK_EXPR into a LABEL_EXPR following
179    a (possibly empty) body.  */
180
181 static tree
182 java_gimplify_labeled_block_expr (tree expr)
183 {
184   tree body = LABELED_BLOCK_BODY (expr);
185   tree label = LABELED_BLOCK_LABEL (expr);
186   tree t;
187
188   DECL_CONTEXT (label) = current_function_decl;
189   t = build (LABEL_EXPR, void_type_node, label);
190   if (body != NULL_TREE)
191     t = build (COMPOUND_EXPR, void_type_node, body, t);
192   return t;
193 }
194
195 /* Gimplify a EXIT_BLOCK_EXPR into a GOTO_EXPR.  */
196
197 static tree
198 java_gimplify_exit_block_expr (tree expr)
199 {
200   tree labeled_block = EXIT_BLOCK_LABELED_BLOCK (expr);
201   tree label;
202
203   /* First operand must be a LABELED_BLOCK_EXPR, which should
204      already be lowered (or partially lowered) when we get here.  */
205   gcc_assert (TREE_CODE (labeled_block) == LABELED_BLOCK_EXPR);
206
207   label = LABELED_BLOCK_LABEL (labeled_block);
208   return build1 (GOTO_EXPR, void_type_node, label);
209 }
210
211 /* This is specific to the bytecode compiler.  If a variable has
212    LOCAL_SLOT_P set, replace an assignment to it with an assignment to
213    the corresponding variable that holds all its aliases.  */
214
215 static tree
216 java_gimplify_modify_expr (tree modify_expr)
217 {
218   tree lhs = TREE_OPERAND (modify_expr, 0);
219   tree rhs = TREE_OPERAND (modify_expr, 1);
220   tree lhs_type = TREE_TYPE (lhs);
221   
222   if (TREE_CODE (lhs) == VAR_DECL
223       && DECL_LANG_SPECIFIC (lhs)
224       && LOCAL_SLOT_P (lhs)
225       && TREE_CODE (lhs_type) == POINTER_TYPE)
226     {
227       tree new_lhs = java_replace_reference (lhs, /* want_lvalue */ true);
228       tree new_rhs = build1 (NOP_EXPR, TREE_TYPE (new_lhs), rhs);
229       modify_expr = build2 (MODIFY_EXPR, TREE_TYPE (new_lhs),
230                             new_lhs, new_rhs);
231       modify_expr = build1 (NOP_EXPR, lhs_type, modify_expr);
232     }
233   
234   return modify_expr;
235 }
236
237     
238 static tree
239 java_gimplify_case_expr (tree expr)
240 {
241   tree label = create_artificial_label ();
242   return build3 (CASE_LABEL_EXPR, void_type_node,
243                  TREE_OPERAND (expr, 0), NULL_TREE, label);
244 }
245
246 static tree
247 java_gimplify_default_expr (tree expr ATTRIBUTE_UNUSED)
248 {
249   tree label = create_artificial_label ();
250   return build3 (CASE_LABEL_EXPR, void_type_node, NULL_TREE, NULL_TREE, label);
251 }
252
253 /* Gimplify BLOCK into a BIND_EXPR.  */
254
255 static tree
256 java_gimplify_block (tree java_block)
257 {
258   tree decls = BLOCK_VARS (java_block);
259   tree body = BLOCK_EXPR_BODY (java_block);
260   tree outer = gimple_current_bind_expr ();
261   tree block;
262
263   /* Don't bother with empty blocks.  */
264   if (! body)
265     return build_empty_stmt ();
266
267   if (IS_EMPTY_STMT (body))
268     return body;
269
270   /* Make a proper block.  Java blocks are unsuitable for BIND_EXPR
271      because they use BLOCK_SUBBLOCKS for another purpose.  */
272   block = make_node (BLOCK);
273   BLOCK_VARS (block) = decls;
274
275   /* The TREE_USED flag on a block determines whether the debug ouput
276      routines generate info for the variables in that block.  */
277   TREE_USED (block) = 1;
278
279   if (outer != NULL_TREE)
280     {
281       outer = BIND_EXPR_BLOCK (outer);
282       BLOCK_SUBBLOCKS (outer) = chainon (BLOCK_SUBBLOCKS (outer), block);
283     }
284
285   return build3 (BIND_EXPR, TREE_TYPE (java_block), decls, body, block);
286 }
287
288 /* Gimplify a NEW_ARRAY_INIT node into array/element assignments.  */
289
290 static tree
291 java_gimplify_new_array_init (tree exp)
292 {
293   tree array_type = TREE_TYPE (TREE_TYPE (exp));
294   tree data_field = lookup_field (&array_type, get_identifier ("data"));
295   tree element_type = TYPE_ARRAY_ELEMENT (array_type);
296   HOST_WIDE_INT ilength = java_array_type_length (array_type);
297   tree length = build_int_cst (NULL_TREE, ilength);
298   tree init = TREE_OPERAND (exp, 0);
299   tree values = CONSTRUCTOR_ELTS (init);
300
301   tree array_ptr_type = build_pointer_type (array_type);
302   tree tmp = create_tmp_var (array_ptr_type, "array");
303   tree body = build2 (MODIFY_EXPR, array_ptr_type, tmp,
304                       build_new_array (element_type, length));
305
306   int index = 0;
307
308   /* FIXME: try to allocate array statically?  */
309   while (values != NULL_TREE)
310     {
311       /* FIXME: Should use build_java_arrayaccess here, but avoid
312          bounds checking.  */
313       tree lhs = build3 (COMPONENT_REF, TREE_TYPE (data_field),    
314                          build_java_indirect_ref (array_type, tmp, 0),
315                          data_field, NULL_TREE);
316       tree assignment = build2 (MODIFY_EXPR, element_type,
317                                 build4 (ARRAY_REF, element_type, lhs,
318                                         build_int_cst (NULL_TREE, index++),
319                                         NULL_TREE, NULL_TREE),
320                                 TREE_VALUE (values));
321       body = build2 (COMPOUND_EXPR, element_type, body, assignment);
322       values = TREE_CHAIN (values);
323     }
324
325   return build2 (COMPOUND_EXPR, array_ptr_type, body, tmp);
326 }
327
328 static tree
329 java_gimplify_try_expr (tree try_expr)
330 {
331   tree body = TREE_OPERAND (try_expr, 0);
332   tree handler = TREE_OPERAND (try_expr, 1);
333   tree catch = NULL_TREE;
334
335   /* Build a CATCH_EXPR for each handler.  */
336   while (handler)
337     {
338       tree java_catch = TREE_OPERAND (handler, 0);
339       tree catch_type = TREE_TYPE (TREE_TYPE (BLOCK_EXPR_DECLS (java_catch)));
340       tree expr = build2 (CATCH_EXPR, void_type_node,
341                           prepare_eh_table_type (catch_type),
342                           handler);
343       if (catch)
344         catch = build2 (COMPOUND_EXPR, void_type_node, catch, expr);
345       else
346         catch = expr;
347       handler = TREE_CHAIN (handler);
348     }
349   return build2 (TRY_CATCH_EXPR, void_type_node, body, catch);
350 }
351
352 /* Dump a tree of some kind.  This is a convenience wrapper for the
353    dump_* functions in tree-dump.c.  */
354 static void
355 dump_java_tree (enum tree_dump_index phase, tree t)
356 {
357   FILE *stream;
358   int flags;
359
360   stream = dump_begin (phase, &flags);
361   flags |= TDF_SLIM;
362   if (stream)
363     {
364       dump_node (t, flags, stream);
365       dump_end (phase, stream);
366     }
367 }