1 /* Java(TM) language-specific gimplification routines.
2 Copyright (C) 2003, 2004, 2006, 2007 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_block (tree);
38 static tree java_gimplify_try_expr (tree);
39 static enum gimplify_status java_gimplify_modify_expr (tree*, tree*, tree *);
40 static enum gimplify_status java_gimplify_component_ref (tree*, tree*, tree *);
41 static enum gimplify_status java_gimplify_self_mod_expr (tree*, tree*, tree *);
43 static void dump_java_tree (enum tree_dump_index, tree);
45 /* Convert a Java tree to GENERIC. */
48 java_genericize (tree fndecl)
50 dump_java_tree (TDI_original, fndecl);
52 /* Genericize with the gimplifier. */
53 gimplify_function_tree (fndecl);
55 dump_function (TDI_generic, fndecl);
58 /* Gimplify a Java tree. */
61 java_gimplify_expr (tree *expr_p, tree *pre_p ATTRIBUTE_UNUSED,
62 tree *post_p ATTRIBUTE_UNUSED)
64 enum tree_code code = TREE_CODE (*expr_p);
69 *expr_p = java_gimplify_block (*expr_p);
72 case EXPR_WITH_FILE_LOCATION:
73 #ifdef USE_MAPPED_LOCATION
74 input_location = EXPR_LOCATION (*expr_p);
76 input_location.file = EXPR_WFL_FILENAME (*expr_p);
77 input_location.line = EXPR_WFL_LINENO (*expr_p);
79 *expr_p = EXPR_WFL_NODE (*expr_p);
81 SET_EXPR_LOCATION (*expr_p, input_location);
84 case LABELED_BLOCK_EXPR:
85 *expr_p = java_gimplify_labeled_block_expr (*expr_p);
89 *expr_p = java_gimplify_exit_block_expr (*expr_p);
93 *expr_p = java_gimplify_try_expr (*expr_p);
97 *expr_p = java_replace_reference (*expr_p, /* want_lvalue */ false);
100 /* We don't handle GIMPLE_MODIFY_STMT, as MODIFY_EXPRs with java
101 semantics should only be generated by the front-end, and never
102 by anything after gimplification. */
104 return java_gimplify_modify_expr (expr_p, pre_p, post_p);
107 /* Note that we can see <save_expr NULL> if the save_expr was
108 already handled by gimplify_save_expr. */
109 if (TREE_OPERAND (*expr_p, 0) != NULL_TREE
110 && TREE_CODE (TREE_OPERAND (*expr_p, 0)) == VAR_DECL)
111 TREE_OPERAND (*expr_p, 0)
112 = java_replace_reference (TREE_OPERAND (*expr_p, 0),
113 /* want_lvalue */ false);
116 case POSTINCREMENT_EXPR:
117 case POSTDECREMENT_EXPR:
118 case PREINCREMENT_EXPR:
119 case PREDECREMENT_EXPR:
120 return java_gimplify_self_mod_expr (expr_p, pre_p, post_p);
122 /* These should already be lowered before we get here. */
130 return java_gimplify_component_ref (expr_p, pre_p, post_p);
133 /* Java insists on strict left-to-right evaluation of expressions.
134 A problem may arise if a variable used in the LHS of a binary
135 operation is altered by an assignment to that value in the RHS
136 before we've performed the operation. So, we always copy every
137 LHS to a temporary variable.
139 FIXME: Are there any other cases where we should do this?
140 Parameter lists, maybe? Or perhaps that's unnecessary because
141 the front end already generates SAVE_EXPRs. */
143 if (TREE_CODE_CLASS (code) == tcc_binary
144 || TREE_CODE_CLASS (code) == tcc_comparison)
146 enum gimplify_status stat
147 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
148 is_gimple_formal_tmp_var, fb_rvalue);
149 if (stat == GS_ERROR)
159 /* Gimplify a LABELED_BLOCK_EXPR into a LABEL_EXPR following
160 a (possibly empty) body. */
163 java_gimplify_labeled_block_expr (tree expr)
165 tree body = LABELED_BLOCK_BODY (expr);
166 tree label = LABELED_BLOCK_LABEL (expr);
169 DECL_CONTEXT (label) = current_function_decl;
170 t = build1 (LABEL_EXPR, void_type_node, label);
171 if (body != NULL_TREE)
172 t = build2 (COMPOUND_EXPR, void_type_node, body, t);
176 /* Gimplify a EXIT_BLOCK_EXPR into a GOTO_EXPR. */
179 java_gimplify_exit_block_expr (tree expr)
181 tree labeled_block = EXIT_BLOCK_LABELED_BLOCK (expr);
184 /* First operand must be a LABELED_BLOCK_EXPR, which should
185 already be lowered (or partially lowered) when we get here. */
186 gcc_assert (TREE_CODE (labeled_block) == LABELED_BLOCK_EXPR);
188 label = LABELED_BLOCK_LABEL (labeled_block);
189 return build1 (GOTO_EXPR, void_type_node, label);
194 static enum gimplify_status
195 java_gimplify_component_ref (tree *expr_p, tree *pre_p, tree *post_p)
197 if (CLASS_FROM_SOURCE_P (output_class)
198 && TREE_THIS_VOLATILE (TREE_OPERAND (*expr_p, 1))
199 && ! TREE_THIS_VOLATILE (*expr_p))
201 enum gimplify_status stat;
204 /* Special handling for volatile fields.
206 A load has "acquire" semantics, implying that you can't move up
207 later operations. A store has "release" semantics meaning that
208 earlier operations cannot be delayed past it.
210 This logic only handles loads: stores are handled in
211 java_gimplify_modify_expr().
213 We gimplify this COMPONENT_REF, put the result in a tmp_var, and then
214 return a COMPOUND_EXPR of the form {__sync_synchronize(); tmp_var}.
215 This forces __sync_synchronize() to be placed immediately after
216 loading from the volatile field.
220 TREE_THIS_VOLATILE (*expr_p) = 1;
221 *expr_p = java_modify_addr_for_volatile (*expr_p);
222 stat = gimplify_expr (expr_p, pre_p, post_p,
223 is_gimple_formal_tmp_var, fb_rvalue);
224 if (stat == GS_ERROR)
228 = build3 (CALL_EXPR, void_type_node,
229 build_address_of (built_in_decls[BUILT_IN_SYNCHRONIZE]),
230 NULL_TREE, NULL_TREE);
231 TREE_SIDE_EFFECTS (sync_expr) = 1;
232 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
234 TREE_SIDE_EFFECTS (*expr_p) = 1;
241 static enum gimplify_status
242 java_gimplify_modify_expr (tree *modify_expr_p, tree *pre_p, tree *post_p)
244 tree modify_expr = *modify_expr_p;
245 tree lhs = TREE_OPERAND (modify_expr, 0);
246 tree rhs = TREE_OPERAND (modify_expr, 1);
247 tree lhs_type = TREE_TYPE (lhs);
249 if (CLASS_FROM_SOURCE_P (output_class)
250 && TREE_CODE (lhs) == COMPONENT_REF
251 && TREE_THIS_VOLATILE (TREE_OPERAND (lhs, 1)))
253 /* Special handling for volatile fields.
255 A load has "acquire" semantics, implying that you can't move up
256 later operations. A store has "release" semantics meaning that
257 earlier operations cannot be delayed past it.
259 This logic only handles stores; loads are handled in
260 java_gimplify_component_ref().
262 We gimplify the rhs, put the result in a tmp_var, and then return
263 a MODIFY_EXPR with an rhs of the form {__sync_synchronize(); tmp_var}.
264 This forces __sync_synchronize() to be placed after evaluating
265 the rhs and immediately before storing to the volatile field.
269 enum gimplify_status stat;
271 = build3 (CALL_EXPR, void_type_node,
272 build_address_of (built_in_decls[BUILT_IN_SYNCHRONIZE]),
273 NULL_TREE, NULL_TREE);
274 TREE_SIDE_EFFECTS (sync_expr) = 1;
276 stat = gimplify_expr (&rhs, pre_p, post_p,
277 is_gimple_formal_tmp_var, fb_rvalue);
278 if (stat == GS_ERROR)
281 rhs = build2 (COMPOUND_EXPR, TREE_TYPE (rhs),
283 TREE_SIDE_EFFECTS (rhs) = 1;
284 TREE_THIS_VOLATILE (lhs) = 1;
285 lhs = java_modify_addr_for_volatile (lhs);
286 TREE_OPERAND (modify_expr, 0) = lhs;
287 TREE_OPERAND (modify_expr, 1) = rhs;
290 /* This is specific to the bytecode compiler. If a variable has
291 LOCAL_SLOT_P set, replace an assignment to it with an assignment
292 to the corresponding variable that holds all its aliases. */
293 if (TREE_CODE (lhs) == VAR_DECL
294 && DECL_LANG_SPECIFIC (lhs)
295 && LOCAL_SLOT_P (lhs)
296 && TREE_CODE (lhs_type) == POINTER_TYPE)
298 tree new_lhs = java_replace_reference (lhs, /* want_lvalue */ true);
299 tree new_rhs = build1 (NOP_EXPR, TREE_TYPE (new_lhs), rhs);
300 modify_expr = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (new_lhs),
302 modify_expr = build1 (NOP_EXPR, lhs_type, modify_expr);
304 else if (lhs_type != TREE_TYPE (rhs))
305 /* Fix up type mismatches to make legal GIMPLE. These are
306 generated in several places, in particular null pointer
307 assignment and subclass assignment. */
308 TREE_OPERAND (modify_expr, 1) = convert (lhs_type, rhs);
310 *modify_expr_p = modify_expr;
314 /* Special case handling for volatiles: we need to generate a barrier
315 between the reading and the writing. */
317 static enum gimplify_status
318 java_gimplify_self_mod_expr (tree *expr_p, tree *pre_p ATTRIBUTE_UNUSED,
319 tree *post_p ATTRIBUTE_UNUSED)
321 tree lhs = TREE_OPERAND (*expr_p, 0);
323 if (TREE_CODE (lhs) == COMPONENT_REF
324 && TREE_THIS_VOLATILE (TREE_OPERAND (lhs, 1)))
325 TREE_THIS_VOLATILE (lhs) = 1;
331 /* Gimplify BLOCK into a BIND_EXPR. */
334 java_gimplify_block (tree java_block)
336 tree decls = BLOCK_VARS (java_block);
337 tree body = BLOCK_EXPR_BODY (java_block);
338 tree outer = gimple_current_bind_expr ();
341 /* Don't bother with empty blocks. */
343 return build_empty_stmt ();
345 if (IS_EMPTY_STMT (body))
348 /* Make a proper block. Java blocks are unsuitable for BIND_EXPR
349 because they use BLOCK_SUBBLOCKS for another purpose. */
350 block = make_node (BLOCK);
351 BLOCK_VARS (block) = decls;
353 /* The TREE_USED flag on a block determines whether the debug output
354 routines generate info for the variables in that block. */
355 TREE_USED (block) = 1;
357 if (outer != NULL_TREE)
359 outer = BIND_EXPR_BLOCK (outer);
360 BLOCK_SUBBLOCKS (outer) = chainon (BLOCK_SUBBLOCKS (outer), block);
362 BLOCK_EXPR_BODY (java_block) = NULL_TREE;
364 return build3 (BIND_EXPR, TREE_TYPE (java_block), decls, body, block);
368 java_gimplify_try_expr (tree try_expr)
370 tree body = TREE_OPERAND (try_expr, 0);
371 tree handler = TREE_OPERAND (try_expr, 1);
372 tree catch = NULL_TREE;
374 /* Build a CATCH_EXPR for each handler. */
377 tree java_catch = TREE_OPERAND (handler, 0);
378 tree catch_type = TREE_TYPE (TREE_TYPE (BLOCK_EXPR_DECLS (java_catch)));
379 tree expr = build2 (CATCH_EXPR, void_type_node,
380 prepare_eh_table_type (catch_type),
383 catch = build2 (COMPOUND_EXPR, void_type_node, catch, expr);
386 handler = TREE_CHAIN (handler);
388 return build2 (TRY_CATCH_EXPR, void_type_node, body, catch);
391 /* Dump a tree of some kind. This is a convenience wrapper for the
392 dump_* functions in tree-dump.c. */
394 dump_java_tree (enum tree_dump_index phase, tree t)
399 stream = dump_begin (phase, &flags);
403 dump_node (t, flags, stream);
404 dump_end (phase, stream);