1 /* Java(TM) language-specific gimplification routines.
2 Copyright (C) 2003, 2004, 2006, 2007, 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 3, 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 COPYING3. If not see
18 <http://www.gnu.org/licenses/>.
20 Java and all Java-based marks are trademarks or registered trademarks
21 of Sun Microsystems, Inc. in the United States and other countries.
22 The Free Software Foundation is independent of Sun Microsystems, Inc. */
26 #include "coretypes.h"
29 #include "java-tree.h"
30 #include "tree-dump.h"
34 static tree
java_gimplify_block (tree
);
35 static enum gimplify_status
java_gimplify_modify_expr (tree
*);
36 static enum gimplify_status
java_gimplify_self_mod_expr (tree
*, gimple_seq
*,
39 static void dump_java_tree (enum tree_dump_index
, tree
);
41 /* Convert a Java tree to GENERIC. */
44 java_genericize (tree fndecl
)
46 dump_java_tree (TDI_original
, fndecl
);
48 /* Genericize with the gimplifier. */
49 gimplify_function_tree (fndecl
);
51 dump_function (TDI_generic
, fndecl
);
54 /* Gimplify a Java tree. */
57 java_gimplify_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
)
59 enum tree_code code
= TREE_CODE (*expr_p
);
64 *expr_p
= java_gimplify_block (*expr_p
);
68 *expr_p
= java_replace_reference (*expr_p
, /* want_lvalue */ false);
72 return java_gimplify_modify_expr (expr_p
);
75 /* Note that we can see <save_expr NULL> if the save_expr was
76 already handled by gimplify_save_expr. */
77 if (TREE_OPERAND (*expr_p
, 0) != NULL_TREE
78 && TREE_CODE (TREE_OPERAND (*expr_p
, 0)) == VAR_DECL
)
79 TREE_OPERAND (*expr_p
, 0)
80 = java_replace_reference (TREE_OPERAND (*expr_p
, 0),
81 /* want_lvalue */ false);
84 case POSTINCREMENT_EXPR
:
85 case POSTDECREMENT_EXPR
:
86 case PREINCREMENT_EXPR
:
87 case PREDECREMENT_EXPR
:
88 return java_gimplify_self_mod_expr (expr_p
, pre_p
, post_p
);
90 /* These should already be lowered before we get here. */
98 /* Java insists on strict left-to-right evaluation of expressions.
99 A problem may arise if a variable used in the LHS of a binary
100 operation is altered by an assignment to that value in the RHS
101 before we've performed the operation. So, we always copy every
102 LHS to a temporary variable.
104 FIXME: Are there any other cases where we should do this?
105 Parameter lists, maybe? Or perhaps that's unnecessary because
106 the front end already generates SAVE_EXPRs. */
108 if (TREE_CODE_CLASS (code
) == tcc_binary
109 || TREE_CODE_CLASS (code
) == tcc_comparison
)
111 enum gimplify_status stat
112 = gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
, post_p
,
113 is_gimple_formal_tmp_var
, fb_rvalue
);
114 if (stat
== GS_ERROR
)
124 static enum gimplify_status
125 java_gimplify_modify_expr (tree
*modify_expr_p
)
127 tree modify_expr
= *modify_expr_p
;
128 tree lhs
= TREE_OPERAND (modify_expr
, 0);
129 tree rhs
= TREE_OPERAND (modify_expr
, 1);
130 tree lhs_type
= TREE_TYPE (lhs
);
132 /* This is specific to the bytecode compiler. If a variable has
133 LOCAL_SLOT_P set, replace an assignment to it with an assignment
134 to the corresponding variable that holds all its aliases. */
135 if (TREE_CODE (lhs
) == VAR_DECL
136 && DECL_LANG_SPECIFIC (lhs
)
137 && LOCAL_SLOT_P (lhs
)
138 && TREE_CODE (lhs_type
) == POINTER_TYPE
)
140 tree new_lhs
= java_replace_reference (lhs
, /* want_lvalue */ true);
141 tree new_rhs
= build1 (NOP_EXPR
, TREE_TYPE (new_lhs
), rhs
);
142 modify_expr
= build2 (MODIFY_EXPR
, TREE_TYPE (new_lhs
),
144 modify_expr
= build1 (NOP_EXPR
, lhs_type
, modify_expr
);
146 else if (lhs_type
!= TREE_TYPE (rhs
))
147 /* Fix up type mismatches to make legal GIMPLE. These are
148 generated in several places, in particular null pointer
149 assignment and subclass assignment. */
150 TREE_OPERAND (modify_expr
, 1) = convert (lhs_type
, rhs
);
152 *modify_expr_p
= modify_expr
;
156 /* Special case handling for volatiles: we need to generate a barrier
157 between the reading and the writing. */
159 static enum gimplify_status
160 java_gimplify_self_mod_expr (tree
*expr_p
, gimple_seq
*pre_p ATTRIBUTE_UNUSED
,
161 gimple_seq
*post_p ATTRIBUTE_UNUSED
)
163 tree lhs
= TREE_OPERAND (*expr_p
, 0);
165 if (TREE_CODE (lhs
) == COMPONENT_REF
166 && TREE_THIS_VOLATILE (TREE_OPERAND (lhs
, 1)))
167 TREE_THIS_VOLATILE (lhs
) = 1;
173 /* Gimplify BLOCK into a BIND_EXPR. */
176 java_gimplify_block (tree java_block
)
178 tree decls
= BLOCK_VARS (java_block
);
179 tree body
= BLOCK_EXPR_BODY (java_block
);
180 gimple outer
= gimple_current_bind_expr ();
183 /* Don't bother with empty blocks. */
185 return build_empty_stmt ();
187 if (IS_EMPTY_STMT (body
))
190 /* Make a proper block. Java blocks are unsuitable for BIND_EXPR
191 because they use BLOCK_SUBBLOCKS for another purpose. */
192 block
= make_node (BLOCK
);
193 BLOCK_VARS (block
) = decls
;
195 /* The TREE_USED flag on a block determines whether the debug output
196 routines generate info for the variables in that block. */
197 TREE_USED (block
) = 1;
201 tree b
= gimple_bind_block (outer
);
202 BLOCK_SUBBLOCKS (b
) = chainon (BLOCK_SUBBLOCKS (b
), block
);
204 BLOCK_EXPR_BODY (java_block
) = NULL_TREE
;
206 return build3 (BIND_EXPR
, TREE_TYPE (java_block
), decls
, body
, block
);
209 /* Dump a tree of some kind. This is a convenience wrapper for the
210 dump_* functions in tree-dump.c. */
212 dump_java_tree (enum tree_dump_index phase
, tree t
)
217 stream
= dump_begin (phase
, &flags
);
221 dump_node (t
, flags
, stream
);
222 dump_end (phase
, stream
);