* lang.c (dump_compound_expr) <EXPR_WITH_FILE_LOCATION>: Removed
[official-gcc.git] / gcc / java / java-gimplify.c
blob96e900934e337fe4537291395e0b618a4dca28c7
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)
9 any later version.
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. */
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"
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. */
47 void
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. */
60 int
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);
66 switch (code)
68 case BLOCK:
69 *expr_p = java_gimplify_block (*expr_p);
70 break;
72 case LABELED_BLOCK_EXPR:
73 *expr_p = java_gimplify_labeled_block_expr (*expr_p);
74 break;
76 case EXIT_BLOCK_EXPR:
77 *expr_p = java_gimplify_exit_block_expr (*expr_p);
78 break;
80 case TRY_EXPR:
81 *expr_p = java_gimplify_try_expr (*expr_p);
82 break;
84 case VAR_DECL:
85 *expr_p = java_replace_reference (*expr_p, /* want_lvalue */ false);
86 return GS_UNHANDLED;
88 /* We don't handle GIMPLE_MODIFY_STMT, as MODIFY_EXPRs with java
89 semantics should only be generated by the front-end, and never
90 by anything after gimplification. */
91 case MODIFY_EXPR:
92 return java_gimplify_modify_expr (expr_p, pre_p, post_p);
94 case SAVE_EXPR:
95 /* Note that we can see <save_expr NULL> if the save_expr was
96 already handled by gimplify_save_expr. */
97 if (TREE_OPERAND (*expr_p, 0) != NULL_TREE
98 && TREE_CODE (TREE_OPERAND (*expr_p, 0)) == VAR_DECL)
99 TREE_OPERAND (*expr_p, 0)
100 = java_replace_reference (TREE_OPERAND (*expr_p, 0),
101 /* want_lvalue */ false);
102 return GS_UNHANDLED;
104 case POSTINCREMENT_EXPR:
105 case POSTDECREMENT_EXPR:
106 case PREINCREMENT_EXPR:
107 case PREDECREMENT_EXPR:
108 return java_gimplify_self_mod_expr (expr_p, pre_p, post_p);
110 /* These should already be lowered before we get here. */
111 case URSHIFT_EXPR:
112 case COMPARE_EXPR:
113 case COMPARE_L_EXPR:
114 case COMPARE_G_EXPR:
115 gcc_unreachable ();
117 case COMPONENT_REF:
118 return java_gimplify_component_ref (expr_p, pre_p, post_p);
120 default:
121 /* Java insists on strict left-to-right evaluation of expressions.
122 A problem may arise if a variable used in the LHS of a binary
123 operation is altered by an assignment to that value in the RHS
124 before we've performed the operation. So, we always copy every
125 LHS to a temporary variable.
127 FIXME: Are there any other cases where we should do this?
128 Parameter lists, maybe? Or perhaps that's unnecessary because
129 the front end already generates SAVE_EXPRs. */
131 if (TREE_CODE_CLASS (code) == tcc_binary
132 || TREE_CODE_CLASS (code) == tcc_comparison)
134 enum gimplify_status stat
135 = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
136 is_gimple_formal_tmp_var, fb_rvalue);
137 if (stat == GS_ERROR)
138 return stat;
141 return GS_UNHANDLED;
144 return GS_OK;
147 /* Gimplify a LABELED_BLOCK_EXPR into a LABEL_EXPR following
148 a (possibly empty) body. */
150 static tree
151 java_gimplify_labeled_block_expr (tree expr)
153 tree body = LABELED_BLOCK_BODY (expr);
154 tree label = LABELED_BLOCK_LABEL (expr);
155 tree t;
157 DECL_CONTEXT (label) = current_function_decl;
158 t = build1 (LABEL_EXPR, void_type_node, label);
159 if (body != NULL_TREE)
160 t = build2 (COMPOUND_EXPR, void_type_node, body, t);
161 return t;
164 /* Gimplify a EXIT_BLOCK_EXPR into a GOTO_EXPR. */
166 static tree
167 java_gimplify_exit_block_expr (tree expr)
169 tree labeled_block = EXIT_BLOCK_LABELED_BLOCK (expr);
170 tree label;
172 /* First operand must be a LABELED_BLOCK_EXPR, which should
173 already be lowered (or partially lowered) when we get here. */
174 gcc_assert (TREE_CODE (labeled_block) == LABELED_BLOCK_EXPR);
176 label = LABELED_BLOCK_LABEL (labeled_block);
177 return build1 (GOTO_EXPR, void_type_node, label);
182 static enum gimplify_status
183 java_gimplify_component_ref (tree *expr_p, tree *pre_p, tree *post_p)
185 if (CLASS_FROM_SOURCE_P (output_class)
186 && TREE_THIS_VOLATILE (TREE_OPERAND (*expr_p, 1))
187 && ! TREE_THIS_VOLATILE (*expr_p))
189 enum gimplify_status stat;
190 tree sync_expr;
192 /* Special handling for volatile fields.
194 A load has "acquire" semantics, implying that you can't move up
195 later operations. A store has "release" semantics meaning that
196 earlier operations cannot be delayed past it.
198 This logic only handles loads: stores are handled in
199 java_gimplify_modify_expr().
201 We gimplify this COMPONENT_REF, put the result in a tmp_var, and then
202 return a COMPOUND_EXPR of the form {__sync_synchronize(); tmp_var}.
203 This forces __sync_synchronize() to be placed immediately after
204 loading from the volatile field.
208 TREE_THIS_VOLATILE (*expr_p) = 1;
209 *expr_p = java_modify_addr_for_volatile (*expr_p);
210 stat = gimplify_expr (expr_p, pre_p, post_p,
211 is_gimple_formal_tmp_var, fb_rvalue);
212 if (stat == GS_ERROR)
213 return stat;
215 sync_expr
216 = build3 (CALL_EXPR, void_type_node,
217 build_address_of (built_in_decls[BUILT_IN_SYNCHRONIZE]),
218 NULL_TREE, NULL_TREE);
219 TREE_SIDE_EFFECTS (sync_expr) = 1;
220 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
221 sync_expr, *expr_p);
222 TREE_SIDE_EFFECTS (*expr_p) = 1;
225 return GS_UNHANDLED;
229 static enum gimplify_status
230 java_gimplify_modify_expr (tree *modify_expr_p, tree *pre_p, tree *post_p)
232 tree modify_expr = *modify_expr_p;
233 tree lhs = TREE_OPERAND (modify_expr, 0);
234 tree rhs = TREE_OPERAND (modify_expr, 1);
235 tree lhs_type = TREE_TYPE (lhs);
237 if (CLASS_FROM_SOURCE_P (output_class)
238 && TREE_CODE (lhs) == COMPONENT_REF
239 && TREE_THIS_VOLATILE (TREE_OPERAND (lhs, 1)))
241 /* Special handling for volatile fields.
243 A load has "acquire" semantics, implying that you can't move up
244 later operations. A store has "release" semantics meaning that
245 earlier operations cannot be delayed past it.
247 This logic only handles stores; loads are handled in
248 java_gimplify_component_ref().
250 We gimplify the rhs, put the result in a tmp_var, and then return
251 a MODIFY_EXPR with an rhs of the form {__sync_synchronize(); tmp_var}.
252 This forces __sync_synchronize() to be placed after evaluating
253 the rhs and immediately before storing to the volatile field.
257 enum gimplify_status stat;
258 tree sync_expr
259 = build3 (CALL_EXPR, void_type_node,
260 build_address_of (built_in_decls[BUILT_IN_SYNCHRONIZE]),
261 NULL_TREE, NULL_TREE);
262 TREE_SIDE_EFFECTS (sync_expr) = 1;
264 stat = gimplify_expr (&rhs, pre_p, post_p,
265 is_gimple_formal_tmp_var, fb_rvalue);
266 if (stat == GS_ERROR)
267 return stat;
269 rhs = build2 (COMPOUND_EXPR, TREE_TYPE (rhs),
270 sync_expr, rhs);
271 TREE_SIDE_EFFECTS (rhs) = 1;
272 TREE_THIS_VOLATILE (lhs) = 1;
273 lhs = java_modify_addr_for_volatile (lhs);
274 TREE_OPERAND (modify_expr, 0) = lhs;
275 TREE_OPERAND (modify_expr, 1) = rhs;
278 /* This is specific to the bytecode compiler. If a variable has
279 LOCAL_SLOT_P set, replace an assignment to it with an assignment
280 to the corresponding variable that holds all its aliases. */
281 if (TREE_CODE (lhs) == VAR_DECL
282 && DECL_LANG_SPECIFIC (lhs)
283 && LOCAL_SLOT_P (lhs)
284 && TREE_CODE (lhs_type) == POINTER_TYPE)
286 tree new_lhs = java_replace_reference (lhs, /* want_lvalue */ true);
287 tree new_rhs = build1 (NOP_EXPR, TREE_TYPE (new_lhs), rhs);
288 modify_expr = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (new_lhs),
289 new_lhs, new_rhs);
290 modify_expr = build1 (NOP_EXPR, lhs_type, modify_expr);
292 else if (lhs_type != TREE_TYPE (rhs))
293 /* Fix up type mismatches to make legal GIMPLE. These are
294 generated in several places, in particular null pointer
295 assignment and subclass assignment. */
296 TREE_OPERAND (modify_expr, 1) = convert (lhs_type, rhs);
298 *modify_expr_p = modify_expr;
299 return GS_UNHANDLED;
302 /* Special case handling for volatiles: we need to generate a barrier
303 between the reading and the writing. */
305 static enum gimplify_status
306 java_gimplify_self_mod_expr (tree *expr_p, tree *pre_p ATTRIBUTE_UNUSED,
307 tree *post_p ATTRIBUTE_UNUSED)
309 tree lhs = TREE_OPERAND (*expr_p, 0);
311 if (TREE_CODE (lhs) == COMPONENT_REF
312 && TREE_THIS_VOLATILE (TREE_OPERAND (lhs, 1)))
313 TREE_THIS_VOLATILE (lhs) = 1;
315 return GS_UNHANDLED;
319 /* Gimplify BLOCK into a BIND_EXPR. */
321 static tree
322 java_gimplify_block (tree java_block)
324 tree decls = BLOCK_VARS (java_block);
325 tree body = BLOCK_EXPR_BODY (java_block);
326 tree outer = gimple_current_bind_expr ();
327 tree block;
329 /* Don't bother with empty blocks. */
330 if (! body)
331 return build_empty_stmt ();
333 if (IS_EMPTY_STMT (body))
334 return body;
336 /* Make a proper block. Java blocks are unsuitable for BIND_EXPR
337 because they use BLOCK_SUBBLOCKS for another purpose. */
338 block = make_node (BLOCK);
339 BLOCK_VARS (block) = decls;
341 /* The TREE_USED flag on a block determines whether the debug output
342 routines generate info for the variables in that block. */
343 TREE_USED (block) = 1;
345 if (outer != NULL_TREE)
347 outer = BIND_EXPR_BLOCK (outer);
348 BLOCK_SUBBLOCKS (outer) = chainon (BLOCK_SUBBLOCKS (outer), block);
350 BLOCK_EXPR_BODY (java_block) = NULL_TREE;
352 return build3 (BIND_EXPR, TREE_TYPE (java_block), decls, body, block);
355 static tree
356 java_gimplify_try_expr (tree try_expr)
358 tree body = TREE_OPERAND (try_expr, 0);
359 tree handler = TREE_OPERAND (try_expr, 1);
360 tree catch = NULL_TREE;
362 /* Build a CATCH_EXPR for each handler. */
363 while (handler)
365 tree java_catch = TREE_OPERAND (handler, 0);
366 tree catch_type = TREE_TYPE (TREE_TYPE (BLOCK_EXPR_DECLS (java_catch)));
367 tree expr = build2 (CATCH_EXPR, void_type_node,
368 prepare_eh_table_type (catch_type),
369 handler);
370 if (catch)
371 catch = build2 (COMPOUND_EXPR, void_type_node, catch, expr);
372 else
373 catch = expr;
374 handler = TREE_CHAIN (handler);
376 return build2 (TRY_CATCH_EXPR, void_type_node, body, catch);
379 /* Dump a tree of some kind. This is a convenience wrapper for the
380 dump_* functions in tree-dump.c. */
381 static void
382 dump_java_tree (enum tree_dump_index phase, tree t)
384 FILE *stream;
385 int flags;
387 stream = dump_begin (phase, &flags);
388 flags |= TDF_SLIM;
389 if (stream)
391 dump_node (t, flags, stream);
392 dump_end (phase, stream);