OSDN Git Service

2006-09-07 H.J. Lu <hongjiu.lu@intel.com>
[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, 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, 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 enum gimplify_status java_gimplify_modify_expr (tree*, tree*, tree *);
43 static enum gimplify_status java_gimplify_component_ref (tree*, tree*, tree *);
44 static enum gimplify_status java_gimplify_self_mod_expr (tree*, tree*, tree *);
45
46 static void dump_java_tree (enum tree_dump_index, tree);
47
48 /* Convert a Java tree to GENERIC.  */
49
50 void
51 java_genericize (tree fndecl)
52 {
53   dump_java_tree (TDI_original, fndecl);
54
55   /* Genericize with the gimplifier.  */
56   gimplify_function_tree (fndecl);
57
58   dump_function (TDI_generic, fndecl);
59 }
60
61 /* Gimplify a Java tree.  */
62
63 int
64 java_gimplify_expr (tree *expr_p, tree *pre_p ATTRIBUTE_UNUSED,
65                     tree *post_p ATTRIBUTE_UNUSED)
66 {
67   enum tree_code code = TREE_CODE (*expr_p);
68
69   switch (code)
70     {
71     case BLOCK:
72       *expr_p = java_gimplify_block (*expr_p);
73       break;
74
75     case EXPR_WITH_FILE_LOCATION:
76 #ifdef USE_MAPPED_LOCATION
77       input_location = EXPR_LOCATION (*expr_p);
78 #else
79       input_location.file = EXPR_WFL_FILENAME (*expr_p);
80       input_location.line = EXPR_WFL_LINENO (*expr_p);
81 #endif
82       *expr_p = EXPR_WFL_NODE (*expr_p);
83       if (EXPR_P (*expr_p))
84         SET_EXPR_LOCATION (*expr_p, input_location);
85       break;
86
87     case LABELED_BLOCK_EXPR:
88       *expr_p = java_gimplify_labeled_block_expr (*expr_p);
89       break;
90
91     case EXIT_BLOCK_EXPR:
92       *expr_p = java_gimplify_exit_block_expr (*expr_p);
93       break;
94
95     case CASE_EXPR:
96       *expr_p = java_gimplify_case_expr (*expr_p);
97       break;
98
99     case DEFAULT_EXPR:
100       *expr_p = java_gimplify_default_expr (*expr_p);
101       break;
102
103     case NEW_ARRAY_INIT:
104       *expr_p = java_gimplify_new_array_init (*expr_p);
105       break;
106
107     case TRY_EXPR:
108       *expr_p = java_gimplify_try_expr (*expr_p);
109       break;
110
111     case JAVA_CATCH_EXPR:
112       *expr_p = TREE_OPERAND (*expr_p, 0);
113       break;
114
115     case JAVA_EXC_OBJ_EXPR:
116       *expr_p = build_exception_object_ref (TREE_TYPE (*expr_p));
117       break;
118
119     case VAR_DECL:
120       *expr_p = java_replace_reference (*expr_p, /* want_lvalue */ false);
121       return GS_UNHANDLED;
122
123     case MODIFY_EXPR:
124       return java_gimplify_modify_expr (expr_p, pre_p, post_p);
125
126     case SAVE_EXPR:
127       /* Note that we can see <save_expr NULL> if the save_expr was
128          already handled by gimplify_save_expr.  */
129       if (TREE_OPERAND (*expr_p, 0) != NULL_TREE
130           && TREE_CODE (TREE_OPERAND (*expr_p, 0)) == VAR_DECL)
131         TREE_OPERAND (*expr_p, 0) 
132           = java_replace_reference (TREE_OPERAND (*expr_p, 0), 
133                                /* want_lvalue */ false);
134       return GS_UNHANDLED;
135
136     case POSTINCREMENT_EXPR:
137     case POSTDECREMENT_EXPR:
138     case PREINCREMENT_EXPR:
139     case PREDECREMENT_EXPR:
140       return java_gimplify_self_mod_expr (expr_p, pre_p, post_p);
141       
142     /* These should already be lowered before we get here.  */
143     case URSHIFT_EXPR:
144     case COMPARE_EXPR:
145     case COMPARE_L_EXPR:
146     case COMPARE_G_EXPR:
147     case UNARY_PLUS_EXPR:
148     case NEW_ARRAY_EXPR:
149     case NEW_ANONYMOUS_ARRAY_EXPR:
150     case NEW_CLASS_EXPR:
151     case THIS_EXPR:
152     case SYNCHRONIZED_EXPR:
153     case CONDITIONAL_EXPR:
154     case INSTANCEOF_EXPR:
155     case CLASS_LITERAL:
156       gcc_unreachable ();
157
158     case COMPONENT_REF:
159       return java_gimplify_component_ref (expr_p, pre_p, post_p);
160
161     default:
162       /* Java insists on strict left-to-right evaluation of expressions.
163          A problem may arise if a variable used in the LHS of a binary
164          operation is altered by an assignment to that value in the RHS
165          before we've performed the operation.  So, we always copy every
166          LHS to a temporary variable.  
167
168          FIXME: Are there any other cases where we should do this?
169          Parameter lists, maybe?  Or perhaps that's unnecessary because
170          the front end already generates SAVE_EXPRs.  */
171
172       if (TREE_CODE_CLASS (code) == tcc_binary
173           || TREE_CODE_CLASS (code) == tcc_comparison)
174         {
175           enum gimplify_status stat 
176             = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
177                              is_gimple_formal_tmp_var, fb_rvalue);
178           if (stat == GS_ERROR)
179             return stat;
180         }
181
182       return GS_UNHANDLED;
183     }
184
185   return GS_OK;
186 }
187
188 /* Gimplify a LABELED_BLOCK_EXPR into a LABEL_EXPR following
189    a (possibly empty) body.  */
190
191 static tree
192 java_gimplify_labeled_block_expr (tree expr)
193 {
194   tree body = LABELED_BLOCK_BODY (expr);
195   tree label = LABELED_BLOCK_LABEL (expr);
196   tree t;
197
198   DECL_CONTEXT (label) = current_function_decl;
199   t = build1 (LABEL_EXPR, void_type_node, label);
200   if (body != NULL_TREE)
201     t = build2 (COMPOUND_EXPR, void_type_node, body, t);
202   return t;
203 }
204
205 /* Gimplify a EXIT_BLOCK_EXPR into a GOTO_EXPR.  */
206
207 static tree
208 java_gimplify_exit_block_expr (tree expr)
209 {
210   tree labeled_block = EXIT_BLOCK_LABELED_BLOCK (expr);
211   tree label;
212
213   /* First operand must be a LABELED_BLOCK_EXPR, which should
214      already be lowered (or partially lowered) when we get here.  */
215   gcc_assert (TREE_CODE (labeled_block) == LABELED_BLOCK_EXPR);
216
217   label = LABELED_BLOCK_LABEL (labeled_block);
218   return build1 (GOTO_EXPR, void_type_node, label);
219 }
220
221
222
223 static enum gimplify_status
224 java_gimplify_component_ref (tree *expr_p, tree *pre_p, tree *post_p)
225 {
226   if (CLASS_FROM_SOURCE_P (output_class)
227       && TREE_THIS_VOLATILE (TREE_OPERAND (*expr_p, 1))
228       && ! TREE_THIS_VOLATILE (*expr_p))
229   {
230     enum gimplify_status stat;
231     tree sync_expr;
232
233     /* Special handling for volatile fields.  
234
235     A load has "acquire" semantics, implying that you can't move up
236     later operations.  A store has "release" semantics meaning that
237     earlier operations cannot be delayed past it.  
238
239     This logic only handles loads: stores are handled in
240     java_gimplify_modify_expr().
241
242     We gimplify this COMPONENT_REF, put the result in a tmp_var, and then
243     return a COMPOUND_EXPR of the form {__sync_synchronize(); tmp_var}.  
244     This forces __sync_synchronize() to be placed immediately after
245     loading from the volatile field.
246
247     */
248   
249     TREE_THIS_VOLATILE (*expr_p) = 1;
250     *expr_p = java_modify_addr_for_volatile (*expr_p);
251     stat = gimplify_expr (expr_p, pre_p, post_p,
252                           is_gimple_formal_tmp_var, fb_rvalue);
253     if (stat == GS_ERROR)
254       return stat;
255
256     sync_expr 
257       = build3 (CALL_EXPR, void_type_node,
258                 build_address_of (built_in_decls[BUILT_IN_SYNCHRONIZE]),
259                 NULL_TREE, NULL_TREE);
260     TREE_SIDE_EFFECTS (sync_expr) = 1;
261     *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
262                       sync_expr, *expr_p);
263     TREE_SIDE_EFFECTS (*expr_p) = 1;
264   }
265
266   return GS_UNHANDLED;
267 }
268   
269
270 static enum gimplify_status
271 java_gimplify_modify_expr (tree *modify_expr_p, tree *pre_p, tree *post_p)
272 {
273   tree modify_expr = *modify_expr_p;
274   tree lhs = TREE_OPERAND (modify_expr, 0);
275   tree rhs = TREE_OPERAND (modify_expr, 1);
276   tree lhs_type = TREE_TYPE (lhs);
277
278   if (CLASS_FROM_SOURCE_P (output_class)
279       && TREE_CODE (lhs) == COMPONENT_REF
280       && TREE_THIS_VOLATILE (TREE_OPERAND (lhs, 1)))
281     {
282       /* Special handling for volatile fields.  
283
284       A load has "acquire" semantics, implying that you can't move up
285       later operations.  A store has "release" semantics meaning that
286       earlier operations cannot be delayed past it.  
287
288       This logic only handles stores; loads are handled in
289       java_gimplify_component_ref().
290
291       We gimplify the rhs, put the result in a tmp_var, and then return
292       a MODIFY_EXPR with an rhs of the form {__sync_synchronize(); tmp_var}.
293       This forces __sync_synchronize() to be placed after evaluating
294       the rhs and immediately before storing to the volatile field.
295
296       */
297   
298       enum gimplify_status stat;
299       tree sync_expr 
300         = build3 (CALL_EXPR, void_type_node,
301                   build_address_of (built_in_decls[BUILT_IN_SYNCHRONIZE]),
302                   NULL_TREE, NULL_TREE);
303       TREE_SIDE_EFFECTS (sync_expr) = 1;
304
305       stat = gimplify_expr (&rhs, pre_p, post_p,
306                             is_gimple_formal_tmp_var, fb_rvalue);
307       if (stat == GS_ERROR)
308         return stat;
309
310       rhs = build2 (COMPOUND_EXPR, TREE_TYPE (rhs),
311                     sync_expr, rhs);
312       TREE_SIDE_EFFECTS (rhs) = 1;
313       TREE_THIS_VOLATILE (lhs) = 1;
314       lhs = java_modify_addr_for_volatile (lhs);
315       TREE_OPERAND (modify_expr, 0) = lhs;
316       TREE_OPERAND (modify_expr, 1) = rhs;
317     }
318
319   /* This is specific to the bytecode compiler.  If a variable has
320      LOCAL_SLOT_P set, replace an assignment to it with an assignment
321      to the corresponding variable that holds all its aliases.  */
322   if (TREE_CODE (lhs) == VAR_DECL
323       && DECL_LANG_SPECIFIC (lhs)
324       && LOCAL_SLOT_P (lhs)
325       && TREE_CODE (lhs_type) == POINTER_TYPE)
326     {
327       tree new_lhs = java_replace_reference (lhs, /* want_lvalue */ true);
328       tree new_rhs = build1 (NOP_EXPR, TREE_TYPE (new_lhs), rhs);
329       modify_expr = build2 (MODIFY_EXPR, TREE_TYPE (new_lhs),
330                             new_lhs, new_rhs);
331       modify_expr = build1 (NOP_EXPR, lhs_type, modify_expr);
332     }
333   else if (lhs_type != TREE_TYPE (rhs))
334     /* Fix up type mismatches to make legal GIMPLE.  These are
335        generated in several places, in particular null pointer
336        assignment and subclass assignment.  */
337     TREE_OPERAND (modify_expr, 1) = convert (lhs_type, rhs);
338
339   *modify_expr_p = modify_expr;
340   return GS_UNHANDLED;
341 }
342
343 /*  Special case handling for volatiles: we need to generate a barrier
344     between the reading and the writing.  */
345
346 static enum gimplify_status
347 java_gimplify_self_mod_expr (tree *expr_p, tree *pre_p ATTRIBUTE_UNUSED, 
348                              tree *post_p ATTRIBUTE_UNUSED)
349 {
350   tree lhs = TREE_OPERAND (*expr_p, 0);
351
352   if (TREE_CODE (lhs) == COMPONENT_REF
353       && TREE_THIS_VOLATILE (TREE_OPERAND (lhs, 1)))
354     TREE_THIS_VOLATILE (lhs) = 1;
355
356   return GS_UNHANDLED;
357 }
358
359     
360 static tree
361 java_gimplify_case_expr (tree expr)
362 {
363   tree label = create_artificial_label ();
364   return build3 (CASE_LABEL_EXPR, void_type_node,
365                  TREE_OPERAND (expr, 0), NULL_TREE, label);
366 }
367
368 static tree
369 java_gimplify_default_expr (tree expr ATTRIBUTE_UNUSED)
370 {
371   tree label = create_artificial_label ();
372   return build3 (CASE_LABEL_EXPR, void_type_node, NULL_TREE, NULL_TREE, label);
373 }
374
375 /* Gimplify BLOCK into a BIND_EXPR.  */
376
377 static tree
378 java_gimplify_block (tree java_block)
379 {
380   tree decls = BLOCK_VARS (java_block);
381   tree body = BLOCK_EXPR_BODY (java_block);
382   tree outer = gimple_current_bind_expr ();
383   tree block;
384
385   /* Don't bother with empty blocks.  */
386   if (! body)
387     return build_empty_stmt ();
388
389   if (IS_EMPTY_STMT (body))
390     return body;
391
392   /* Make a proper block.  Java blocks are unsuitable for BIND_EXPR
393      because they use BLOCK_SUBBLOCKS for another purpose.  */
394   block = make_node (BLOCK);
395   BLOCK_VARS (block) = decls;
396
397   /* The TREE_USED flag on a block determines whether the debug output
398      routines generate info for the variables in that block.  */
399   TREE_USED (block) = 1;
400
401   if (outer != NULL_TREE)
402     {
403       outer = BIND_EXPR_BLOCK (outer);
404       BLOCK_SUBBLOCKS (outer) = chainon (BLOCK_SUBBLOCKS (outer), block);
405     }
406   BLOCK_EXPR_BODY (java_block) = NULL_TREE;
407
408   return build3 (BIND_EXPR, TREE_TYPE (java_block), decls, body, block);
409 }
410
411 /* Gimplify a NEW_ARRAY_INIT node into array/element assignments.  */
412
413 static tree
414 java_gimplify_new_array_init (tree exp)
415 {
416   tree array_type = TREE_TYPE (TREE_TYPE (exp));
417   tree data_field = lookup_field (&array_type, get_identifier ("data"));
418   tree element_type = TYPE_ARRAY_ELEMENT (array_type);
419   HOST_WIDE_INT ilength = java_array_type_length (array_type);
420   tree length = build_int_cst (NULL_TREE, ilength);
421   tree init = TREE_OPERAND (exp, 0);
422   tree value;
423   unsigned HOST_WIDE_INT cnt;
424
425   tree array_ptr_type = build_pointer_type (array_type);
426   tree tmp = create_tmp_var (array_ptr_type, "array");
427   tree body = build2 (MODIFY_EXPR, array_ptr_type, tmp,
428                       build_new_array (element_type, length));
429
430   int index = 0;
431
432   /* FIXME: try to allocate array statically?  */
433   FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), cnt, value)
434     {
435       /* FIXME: Should use build_java_arrayaccess here, but avoid
436          bounds checking.  */
437       tree lhs = build3 (COMPONENT_REF, TREE_TYPE (data_field),    
438                          build_java_indirect_ref (array_type, tmp, 0),
439                          data_field, NULL_TREE);
440       tree assignment = build2 (MODIFY_EXPR, element_type,
441                                 build4 (ARRAY_REF, element_type, lhs,
442                                         build_int_cst (NULL_TREE, index++),
443                                         NULL_TREE, NULL_TREE),
444                                 value);
445       body = build2 (COMPOUND_EXPR, element_type, body, assignment);
446     }
447
448   return build2 (COMPOUND_EXPR, array_ptr_type, body, tmp);
449 }
450
451 static tree
452 java_gimplify_try_expr (tree try_expr)
453 {
454   tree body = TREE_OPERAND (try_expr, 0);
455   tree handler = TREE_OPERAND (try_expr, 1);
456   tree catch = NULL_TREE;
457
458   /* Build a CATCH_EXPR for each handler.  */
459   while (handler)
460     {
461       tree java_catch = TREE_OPERAND (handler, 0);
462       tree catch_type = TREE_TYPE (TREE_TYPE (BLOCK_EXPR_DECLS (java_catch)));
463       tree expr = build2 (CATCH_EXPR, void_type_node,
464                           prepare_eh_table_type (catch_type),
465                           handler);
466       if (catch)
467         catch = build2 (COMPOUND_EXPR, void_type_node, catch, expr);
468       else
469         catch = expr;
470       handler = TREE_CHAIN (handler);
471     }
472   return build2 (TRY_CATCH_EXPR, void_type_node, body, catch);
473 }
474
475 /* Dump a tree of some kind.  This is a convenience wrapper for the
476    dump_* functions in tree-dump.c.  */
477 static void
478 dump_java_tree (enum tree_dump_index phase, tree t)
479 {
480   FILE *stream;
481   int flags;
482
483   stream = dump_begin (phase, &flags);
484   flags |= TDF_SLIM;
485   if (stream)
486     {
487       dump_node (t, flags, stream);
488       dump_end (phase, stream);
489     }
490 }