OSDN Git Service

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