tree.h (enum tree_code_class): Add tcc_vl_exp.
[official-gcc.git] / gcc / java / java-gimplify.c
blobeeda04c3363b0d6051aae25103968949d3f6e081
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 = build_call_expr (built_in_decls[BUILT_IN_SYNCHRONIZE], 0);
216 TREE_SIDE_EFFECTS (sync_expr) = 1;
217 *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
218 sync_expr, *expr_p);
219 TREE_SIDE_EFFECTS (*expr_p) = 1;
222 return GS_UNHANDLED;
226 static enum gimplify_status
227 java_gimplify_modify_expr (tree *modify_expr_p, tree *pre_p, tree *post_p)
229 tree modify_expr = *modify_expr_p;
230 tree lhs = TREE_OPERAND (modify_expr, 0);
231 tree rhs = TREE_OPERAND (modify_expr, 1);
232 tree lhs_type = TREE_TYPE (lhs);
234 if (CLASS_FROM_SOURCE_P (output_class)
235 && TREE_CODE (lhs) == COMPONENT_REF
236 && TREE_THIS_VOLATILE (TREE_OPERAND (lhs, 1)))
238 /* Special handling for volatile fields.
240 A load has "acquire" semantics, implying that you can't move up
241 later operations. A store has "release" semantics meaning that
242 earlier operations cannot be delayed past it.
244 This logic only handles stores; loads are handled in
245 java_gimplify_component_ref().
247 We gimplify the rhs, put the result in a tmp_var, and then return
248 a MODIFY_EXPR with an rhs of the form {__sync_synchronize(); tmp_var}.
249 This forces __sync_synchronize() to be placed after evaluating
250 the rhs and immediately before storing to the volatile field.
254 enum gimplify_status stat;
255 tree sync_expr =
256 build_call_expr (built_in_decls[BUILT_IN_SYNCHRONIZE], 0);
257 TREE_SIDE_EFFECTS (sync_expr) = 1;
259 stat = gimplify_expr (&rhs, pre_p, post_p,
260 is_gimple_formal_tmp_var, fb_rvalue);
261 if (stat == GS_ERROR)
262 return stat;
264 rhs = build2 (COMPOUND_EXPR, TREE_TYPE (rhs),
265 sync_expr, rhs);
266 TREE_SIDE_EFFECTS (rhs) = 1;
267 TREE_THIS_VOLATILE (lhs) = 1;
268 lhs = java_modify_addr_for_volatile (lhs);
269 TREE_OPERAND (modify_expr, 0) = lhs;
270 TREE_OPERAND (modify_expr, 1) = rhs;
273 /* This is specific to the bytecode compiler. If a variable has
274 LOCAL_SLOT_P set, replace an assignment to it with an assignment
275 to the corresponding variable that holds all its aliases. */
276 if (TREE_CODE (lhs) == VAR_DECL
277 && DECL_LANG_SPECIFIC (lhs)
278 && LOCAL_SLOT_P (lhs)
279 && TREE_CODE (lhs_type) == POINTER_TYPE)
281 tree new_lhs = java_replace_reference (lhs, /* want_lvalue */ true);
282 tree new_rhs = build1 (NOP_EXPR, TREE_TYPE (new_lhs), rhs);
283 modify_expr = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (new_lhs),
284 new_lhs, new_rhs);
285 modify_expr = build1 (NOP_EXPR, lhs_type, modify_expr);
287 else if (lhs_type != TREE_TYPE (rhs))
288 /* Fix up type mismatches to make legal GIMPLE. These are
289 generated in several places, in particular null pointer
290 assignment and subclass assignment. */
291 TREE_OPERAND (modify_expr, 1) = convert (lhs_type, rhs);
293 *modify_expr_p = modify_expr;
294 return GS_UNHANDLED;
297 /* Special case handling for volatiles: we need to generate a barrier
298 between the reading and the writing. */
300 static enum gimplify_status
301 java_gimplify_self_mod_expr (tree *expr_p, tree *pre_p ATTRIBUTE_UNUSED,
302 tree *post_p ATTRIBUTE_UNUSED)
304 tree lhs = TREE_OPERAND (*expr_p, 0);
306 if (TREE_CODE (lhs) == COMPONENT_REF
307 && TREE_THIS_VOLATILE (TREE_OPERAND (lhs, 1)))
308 TREE_THIS_VOLATILE (lhs) = 1;
310 return GS_UNHANDLED;
314 /* Gimplify BLOCK into a BIND_EXPR. */
316 static tree
317 java_gimplify_block (tree java_block)
319 tree decls = BLOCK_VARS (java_block);
320 tree body = BLOCK_EXPR_BODY (java_block);
321 tree outer = gimple_current_bind_expr ();
322 tree block;
324 /* Don't bother with empty blocks. */
325 if (! body)
326 return build_empty_stmt ();
328 if (IS_EMPTY_STMT (body))
329 return body;
331 /* Make a proper block. Java blocks are unsuitable for BIND_EXPR
332 because they use BLOCK_SUBBLOCKS for another purpose. */
333 block = make_node (BLOCK);
334 BLOCK_VARS (block) = decls;
336 /* The TREE_USED flag on a block determines whether the debug output
337 routines generate info for the variables in that block. */
338 TREE_USED (block) = 1;
340 if (outer != NULL_TREE)
342 outer = BIND_EXPR_BLOCK (outer);
343 BLOCK_SUBBLOCKS (outer) = chainon (BLOCK_SUBBLOCKS (outer), block);
345 BLOCK_EXPR_BODY (java_block) = NULL_TREE;
347 return build3 (BIND_EXPR, TREE_TYPE (java_block), decls, body, block);
350 static tree
351 java_gimplify_try_expr (tree try_expr)
353 tree body = TREE_OPERAND (try_expr, 0);
354 tree handler = TREE_OPERAND (try_expr, 1);
355 tree catch = NULL_TREE;
357 /* Build a CATCH_EXPR for each handler. */
358 while (handler)
360 tree java_catch = TREE_OPERAND (handler, 0);
361 tree catch_type = TREE_TYPE (TREE_TYPE (BLOCK_EXPR_DECLS (java_catch)));
362 tree expr = build2 (CATCH_EXPR, void_type_node,
363 prepare_eh_table_type (catch_type),
364 handler);
365 if (catch)
366 catch = build2 (COMPOUND_EXPR, void_type_node, catch, expr);
367 else
368 catch = expr;
369 handler = TREE_CHAIN (handler);
371 return build2 (TRY_CATCH_EXPR, void_type_node, body, catch);
374 /* Dump a tree of some kind. This is a convenience wrapper for the
375 dump_* functions in tree-dump.c. */
376 static void
377 dump_java_tree (enum tree_dump_index phase, tree t)
379 FILE *stream;
380 int flags;
382 stream = dump_begin (phase, &flags);
383 flags |= TDF_SLIM;
384 if (stream)
386 dump_node (t, flags, stream);
387 dump_end (phase, stream);