1 /* Java(TM) language-specific gimplification routines.
2 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
4 This file is part of GCC.
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)
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.
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.
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. */
27 #include "coretypes.h"
30 #include "java-tree.h"
31 #include "tree-dump.h"
32 #include "tree-gimple.h"
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 *);
46 static void dump_java_tree (enum tree_dump_index, tree);
48 /* Convert a Java tree to GENERIC. */
51 java_genericize (tree fndecl)
53 dump_java_tree (TDI_original, fndecl);
55 /* Genericize with the gimplifier. */
56 gimplify_function_tree (fndecl);
58 dump_function (TDI_generic, fndecl);
61 /* Gimplify a Java tree. */
64 java_gimplify_expr (tree *expr_p, tree *pre_p ATTRIBUTE_UNUSED,
65 tree *post_p ATTRIBUTE_UNUSED)
67 enum tree_code code = TREE_CODE (*expr_p);
72 *expr_p = java_gimplify_block (*expr_p);
75 case EXPR_WITH_FILE_LOCATION:
76 #ifdef USE_MAPPED_LOCATION
77 input_location = EXPR_LOCATION (*expr_p);
79 input_location.file = EXPR_WFL_FILENAME (*expr_p);
80 input_location.line = EXPR_WFL_LINENO (*expr_p);
82 *expr_p = EXPR_WFL_NODE (*expr_p);
84 SET_EXPR_LOCATION (*expr_p, input_location);
87 case LABELED_BLOCK_EXPR:
88 *expr_p = java_gimplify_labeled_block_expr (*expr_p);
92 *expr_p = java_gimplify_exit_block_expr (*expr_p);
96 *expr_p = java_gimplify_case_expr (*expr_p);
100 *expr_p = java_gimplify_default_expr (*expr_p);
104 *expr_p = java_gimplify_new_array_init (*expr_p);
108 *expr_p = java_gimplify_try_expr (*expr_p);
111 case JAVA_CATCH_EXPR:
112 *expr_p = TREE_OPERAND (*expr_p, 0);
115 case JAVA_EXC_OBJ_EXPR:
116 *expr_p = build_exception_object_ref (TREE_TYPE (*expr_p));
120 *expr_p = java_replace_reference (*expr_p, /* want_lvalue */ false);
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. */
127 return java_gimplify_modify_expr (expr_p, pre_p, post_p);
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);
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);
145 /* These should already be lowered before we get here. */
150 case UNARY_PLUS_EXPR:
152 case NEW_ANONYMOUS_ARRAY_EXPR:
155 case SYNCHRONIZED_EXPR:
156 case CONDITIONAL_EXPR:
157 case INSTANCEOF_EXPR:
162 return java_gimplify_component_ref (expr_p, pre_p, post_p);
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.
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. */
175 if (TREE_CODE_CLASS (code) == tcc_binary
176 || TREE_CODE_CLASS (code) == tcc_comparison)
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)
191 /* Gimplify a LABELED_BLOCK_EXPR into a LABEL_EXPR following
192 a (possibly empty) body. */
195 java_gimplify_labeled_block_expr (tree expr)
197 tree body = LABELED_BLOCK_BODY (expr);
198 tree label = LABELED_BLOCK_LABEL (expr);
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);
208 /* Gimplify a EXIT_BLOCK_EXPR into a GOTO_EXPR. */
211 java_gimplify_exit_block_expr (tree expr)
213 tree labeled_block = EXIT_BLOCK_LABELED_BLOCK (expr);
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);
220 label = LABELED_BLOCK_LABEL (labeled_block);
221 return build1 (GOTO_EXPR, void_type_node, label);
226 static enum gimplify_status
227 java_gimplify_component_ref (tree *expr_p, tree *pre_p, tree *post_p)
229 if (CLASS_FROM_SOURCE_P (output_class)
230 && TREE_THIS_VOLATILE (TREE_OPERAND (*expr_p, 1))
231 && ! TREE_THIS_VOLATILE (*expr_p))
233 enum gimplify_status stat;
236 /* Special handling for volatile fields.
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.
242 This logic only handles loads: stores are handled in
243 java_gimplify_modify_expr().
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.
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)
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),
266 TREE_SIDE_EFFECTS (*expr_p) = 1;
273 static enum gimplify_status
274 java_gimplify_modify_expr (tree *modify_expr_p, tree *pre_p, tree *post_p)
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);
281 if (CLASS_FROM_SOURCE_P (output_class)
282 && TREE_CODE (lhs) == COMPONENT_REF
283 && TREE_THIS_VOLATILE (TREE_OPERAND (lhs, 1)))
285 /* Special handling for volatile fields.
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.
291 This logic only handles stores; loads are handled in
292 java_gimplify_component_ref().
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.
301 enum gimplify_status stat;
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;
308 stat = gimplify_expr (&rhs, pre_p, post_p,
309 is_gimple_formal_tmp_var, fb_rvalue);
310 if (stat == GS_ERROR)
313 rhs = build2 (COMPOUND_EXPR, TREE_TYPE (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;
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)
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),
334 modify_expr = build1 (NOP_EXPR, lhs_type, modify_expr);
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);
342 *modify_expr_p = modify_expr;
346 /* Special case handling for volatiles: we need to generate a barrier
347 between the reading and the writing. */
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)
353 tree lhs = TREE_OPERAND (*expr_p, 0);
355 if (TREE_CODE (lhs) == COMPONENT_REF
356 && TREE_THIS_VOLATILE (TREE_OPERAND (lhs, 1)))
357 TREE_THIS_VOLATILE (lhs) = 1;
364 java_gimplify_case_expr (tree expr)
366 tree label = create_artificial_label ();
367 return build3 (CASE_LABEL_EXPR, void_type_node,
368 TREE_OPERAND (expr, 0), NULL_TREE, label);
372 java_gimplify_default_expr (tree expr ATTRIBUTE_UNUSED)
374 tree label = create_artificial_label ();
375 return build3 (CASE_LABEL_EXPR, void_type_node, NULL_TREE, NULL_TREE, label);
378 /* Gimplify BLOCK into a BIND_EXPR. */
381 java_gimplify_block (tree java_block)
383 tree decls = BLOCK_VARS (java_block);
384 tree body = BLOCK_EXPR_BODY (java_block);
385 tree outer = gimple_current_bind_expr ();
388 /* Don't bother with empty blocks. */
390 return build_empty_stmt ();
392 if (IS_EMPTY_STMT (body))
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;
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;
404 if (outer != NULL_TREE)
406 outer = BIND_EXPR_BLOCK (outer);
407 BLOCK_SUBBLOCKS (outer) = chainon (BLOCK_SUBBLOCKS (outer), block);
409 BLOCK_EXPR_BODY (java_block) = NULL_TREE;
411 return build3 (BIND_EXPR, TREE_TYPE (java_block), decls, body, block);
414 /* Gimplify a NEW_ARRAY_INIT node into array/element assignments. */
417 java_gimplify_new_array_init (tree exp)
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);
426 unsigned HOST_WIDE_INT cnt;
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));
435 /* FIXME: try to allocate array statically? */
436 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), cnt, value)
438 /* FIXME: Should use build_java_arrayaccess here, but avoid
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),
448 body = build2 (COMPOUND_EXPR, element_type, body, assignment);
451 return build2 (COMPOUND_EXPR, array_ptr_type, body, tmp);
455 java_gimplify_try_expr (tree try_expr)
457 tree body = TREE_OPERAND (try_expr, 0);
458 tree handler = TREE_OPERAND (try_expr, 1);
459 tree catch = NULL_TREE;
461 /* Build a CATCH_EXPR for each handler. */
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),
470 catch = build2 (COMPOUND_EXPR, void_type_node, catch, expr);
473 handler = TREE_CHAIN (handler);
475 return build2 (TRY_CATCH_EXPR, void_type_node, body, catch);
478 /* Dump a tree of some kind. This is a convenience wrapper for the
479 dump_* functions in tree-dump.c. */
481 dump_java_tree (enum tree_dump_index phase, tree t)
486 stream = dump_begin (phase, &flags);
490 dump_node (t, flags, stream);
491 dump_end (phase, stream);