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_block (tree);
36 static enum gimplify_status java_gimplify_modify_expr (tree*, tree*, tree *);
37 static enum gimplify_status java_gimplify_component_ref (tree*, tree*, tree *);
38 static enum gimplify_status java_gimplify_self_mod_expr (tree*, tree*, tree *);
40 static void dump_java_tree (enum tree_dump_index, tree);
42 /* Convert a Java tree to GENERIC. */
45 java_genericize (tree fndecl)
47 dump_java_tree (TDI_original, fndecl);
49 /* Genericize with the gimplifier. */
50 gimplify_function_tree (fndecl);
52 dump_function (TDI_generic, fndecl);
55 /* Gimplify a Java tree. */
58 java_gimplify_expr (tree *expr_p, tree *pre_p ATTRIBUTE_UNUSED,
59 tree *post_p ATTRIBUTE_UNUSED)
61 enum tree_code code = TREE_CODE (*expr_p);
66 *expr_p = java_gimplify_block (*expr_p);
70 *expr_p = java_replace_reference (*expr_p, /* want_lvalue */ false);
73 /* We don't handle GIMPLE_MODIFY_STMT, as MODIFY_EXPRs with java
74 semantics should only be generated by the front-end, and never
75 by anything after gimplification. */
77 return java_gimplify_modify_expr (expr_p, pre_p, post_p);
80 /* Note that we can see <save_expr NULL> if the save_expr was
81 already handled by gimplify_save_expr. */
82 if (TREE_OPERAND (*expr_p, 0) != NULL_TREE
83 && TREE_CODE (TREE_OPERAND (*expr_p, 0)) == VAR_DECL)
84 TREE_OPERAND (*expr_p, 0)
85 = java_replace_reference (TREE_OPERAND (*expr_p, 0),
86 /* want_lvalue */ false);
89 case POSTINCREMENT_EXPR:
90 case POSTDECREMENT_EXPR:
91 case PREINCREMENT_EXPR:
92 case PREDECREMENT_EXPR:
93 return java_gimplify_self_mod_expr (expr_p, pre_p, post_p);
95 /* These should already be lowered before we get here. */
103 return java_gimplify_component_ref (expr_p, pre_p, post_p);
106 /* Java insists on strict left-to-right evaluation of expressions.
107 A problem may arise if a variable used in the LHS of a binary
108 operation is altered by an assignment to that value in the RHS
109 before we've performed the operation. So, we always copy every
110 LHS to a temporary variable.
112 FIXME: Are there any other cases where we should do this?
113 Parameter lists, maybe? Or perhaps that's unnecessary because
114 the front end already generates SAVE_EXPRs. */
116 if (TREE_CODE_CLASS (code) == tcc_binary
117 || TREE_CODE_CLASS (code) == tcc_comparison)
119 enum gimplify_status stat
120 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
121 is_gimple_formal_tmp_var, fb_rvalue);
122 if (stat == GS_ERROR)
132 static enum gimplify_status
133 java_gimplify_component_ref (tree *expr_p, tree *pre_p, tree *post_p)
135 if (CLASS_FROM_SOURCE_P (output_class)
136 && TREE_THIS_VOLATILE (TREE_OPERAND (*expr_p, 1))
137 && ! TREE_THIS_VOLATILE (*expr_p))
139 enum gimplify_status stat;
142 /* Special handling for volatile fields.
144 A load has "acquire" semantics, implying that you can't move up
145 later operations. A store has "release" semantics meaning that
146 earlier operations cannot be delayed past it.
148 This logic only handles loads: stores are handled in
149 java_gimplify_modify_expr().
151 We gimplify this COMPONENT_REF, put the result in a tmp_var, and then
152 return a COMPOUND_EXPR of the form {__sync_synchronize(); tmp_var}.
153 This forces __sync_synchronize() to be placed immediately after
154 loading from the volatile field.
158 TREE_THIS_VOLATILE (*expr_p) = 1;
159 *expr_p = java_modify_addr_for_volatile (*expr_p);
160 stat = gimplify_expr (expr_p, pre_p, post_p,
161 is_gimple_formal_tmp_var, fb_rvalue);
162 if (stat == GS_ERROR)
165 sync_expr = build_call_expr (built_in_decls[BUILT_IN_SYNCHRONIZE], 0);
166 TREE_SIDE_EFFECTS (sync_expr) = 1;
167 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
169 TREE_SIDE_EFFECTS (*expr_p) = 1;
176 static enum gimplify_status
177 java_gimplify_modify_expr (tree *modify_expr_p, tree *pre_p, tree *post_p)
179 tree modify_expr = *modify_expr_p;
180 tree lhs = TREE_OPERAND (modify_expr, 0);
181 tree rhs = TREE_OPERAND (modify_expr, 1);
182 tree lhs_type = TREE_TYPE (lhs);
184 if (CLASS_FROM_SOURCE_P (output_class)
185 && TREE_CODE (lhs) == COMPONENT_REF
186 && TREE_THIS_VOLATILE (TREE_OPERAND (lhs, 1)))
188 /* Special handling for volatile fields.
190 A load has "acquire" semantics, implying that you can't move up
191 later operations. A store has "release" semantics meaning that
192 earlier operations cannot be delayed past it.
194 This logic only handles stores; loads are handled in
195 java_gimplify_component_ref().
197 We gimplify the rhs, put the result in a tmp_var, and then return
198 a MODIFY_EXPR with an rhs of the form {__sync_synchronize(); tmp_var}.
199 This forces __sync_synchronize() to be placed after evaluating
200 the rhs and immediately before storing to the volatile field.
204 enum gimplify_status stat;
206 build_call_expr (built_in_decls[BUILT_IN_SYNCHRONIZE], 0);
207 TREE_SIDE_EFFECTS (sync_expr) = 1;
209 stat = gimplify_expr (&rhs, pre_p, post_p,
210 is_gimple_formal_tmp_var, fb_rvalue);
211 if (stat == GS_ERROR)
214 rhs = build2 (COMPOUND_EXPR, TREE_TYPE (rhs),
216 TREE_SIDE_EFFECTS (rhs) = 1;
217 TREE_THIS_VOLATILE (lhs) = 1;
218 lhs = java_modify_addr_for_volatile (lhs);
219 TREE_OPERAND (modify_expr, 0) = lhs;
220 TREE_OPERAND (modify_expr, 1) = rhs;
223 /* This is specific to the bytecode compiler. If a variable has
224 LOCAL_SLOT_P set, replace an assignment to it with an assignment
225 to the corresponding variable that holds all its aliases. */
226 if (TREE_CODE (lhs) == VAR_DECL
227 && DECL_LANG_SPECIFIC (lhs)
228 && LOCAL_SLOT_P (lhs)
229 && TREE_CODE (lhs_type) == POINTER_TYPE)
231 tree new_lhs = java_replace_reference (lhs, /* want_lvalue */ true);
232 tree new_rhs = build1 (NOP_EXPR, TREE_TYPE (new_lhs), rhs);
233 modify_expr = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (new_lhs),
235 modify_expr = build1 (NOP_EXPR, lhs_type, modify_expr);
237 else if (lhs_type != TREE_TYPE (rhs))
238 /* Fix up type mismatches to make legal GIMPLE. These are
239 generated in several places, in particular null pointer
240 assignment and subclass assignment. */
241 TREE_OPERAND (modify_expr, 1) = convert (lhs_type, rhs);
243 *modify_expr_p = modify_expr;
247 /* Special case handling for volatiles: we need to generate a barrier
248 between the reading and the writing. */
250 static enum gimplify_status
251 java_gimplify_self_mod_expr (tree *expr_p, tree *pre_p ATTRIBUTE_UNUSED,
252 tree *post_p ATTRIBUTE_UNUSED)
254 tree lhs = TREE_OPERAND (*expr_p, 0);
256 if (TREE_CODE (lhs) == COMPONENT_REF
257 && TREE_THIS_VOLATILE (TREE_OPERAND (lhs, 1)))
258 TREE_THIS_VOLATILE (lhs) = 1;
264 /* Gimplify BLOCK into a BIND_EXPR. */
267 java_gimplify_block (tree java_block)
269 tree decls = BLOCK_VARS (java_block);
270 tree body = BLOCK_EXPR_BODY (java_block);
271 tree outer = gimple_current_bind_expr ();
274 /* Don't bother with empty blocks. */
276 return build_empty_stmt ();
278 if (IS_EMPTY_STMT (body))
281 /* Make a proper block. Java blocks are unsuitable for BIND_EXPR
282 because they use BLOCK_SUBBLOCKS for another purpose. */
283 block = make_node (BLOCK);
284 BLOCK_VARS (block) = decls;
286 /* The TREE_USED flag on a block determines whether the debug output
287 routines generate info for the variables in that block. */
288 TREE_USED (block) = 1;
290 if (outer != NULL_TREE)
292 outer = BIND_EXPR_BLOCK (outer);
293 BLOCK_SUBBLOCKS (outer) = chainon (BLOCK_SUBBLOCKS (outer), block);
295 BLOCK_EXPR_BODY (java_block) = NULL_TREE;
297 return build3 (BIND_EXPR, TREE_TYPE (java_block), decls, body, block);
300 /* Dump a tree of some kind. This is a convenience wrapper for the
301 dump_* functions in tree-dump.c. */
303 dump_java_tree (enum tree_dump_index phase, tree t)
308 stream = dump_begin (phase, &flags);
312 dump_node (t, flags, stream);
313 dump_end (phase, stream);