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"
31 #include "tree-gimple.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
*, tree
*, tree
*);
38 static void dump_java_tree (enum tree_dump_index
, tree
);
40 /* Convert a Java tree to GENERIC. */
43 java_genericize (tree fndecl
)
45 dump_java_tree (TDI_original
, fndecl
);
47 /* Genericize with the gimplifier. */
48 gimplify_function_tree (fndecl
);
50 dump_function (TDI_generic
, fndecl
);
53 /* Gimplify a Java tree. */
56 java_gimplify_expr (tree
*expr_p
, tree
*pre_p ATTRIBUTE_UNUSED
,
57 tree
*post_p ATTRIBUTE_UNUSED
)
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);
71 /* We don't handle GIMPLE_MODIFY_STMT, as MODIFY_EXPRs with java
72 semantics should only be generated by the front-end, and never
73 by anything after gimplification. */
75 return java_gimplify_modify_expr (expr_p
);
78 /* Note that we can see <save_expr NULL> if the save_expr was
79 already handled by gimplify_save_expr. */
80 if (TREE_OPERAND (*expr_p
, 0) != NULL_TREE
81 && TREE_CODE (TREE_OPERAND (*expr_p
, 0)) == VAR_DECL
)
82 TREE_OPERAND (*expr_p
, 0)
83 = java_replace_reference (TREE_OPERAND (*expr_p
, 0),
84 /* want_lvalue */ false);
87 case POSTINCREMENT_EXPR
:
88 case POSTDECREMENT_EXPR
:
89 case PREINCREMENT_EXPR
:
90 case PREDECREMENT_EXPR
:
91 return java_gimplify_self_mod_expr (expr_p
, pre_p
, post_p
);
93 /* These should already be lowered before we get here. */
101 /* Java insists on strict left-to-right evaluation of expressions.
102 A problem may arise if a variable used in the LHS of a binary
103 operation is altered by an assignment to that value in the RHS
104 before we've performed the operation. So, we always copy every
105 LHS to a temporary variable.
107 FIXME: Are there any other cases where we should do this?
108 Parameter lists, maybe? Or perhaps that's unnecessary because
109 the front end already generates SAVE_EXPRs. */
111 if (TREE_CODE_CLASS (code
) == tcc_binary
112 || TREE_CODE_CLASS (code
) == tcc_comparison
)
114 enum gimplify_status stat
115 = gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
, post_p
,
116 is_gimple_formal_tmp_var
, fb_rvalue
);
117 if (stat
== GS_ERROR
)
127 static enum gimplify_status
128 java_gimplify_modify_expr (tree
*modify_expr_p
)
130 tree modify_expr
= *modify_expr_p
;
131 tree lhs
= TREE_OPERAND (modify_expr
, 0);
132 tree rhs
= TREE_OPERAND (modify_expr
, 1);
133 tree lhs_type
= TREE_TYPE (lhs
);
135 /* This is specific to the bytecode compiler. If a variable has
136 LOCAL_SLOT_P set, replace an assignment to it with an assignment
137 to the corresponding variable that holds all its aliases. */
138 if (TREE_CODE (lhs
) == VAR_DECL
139 && DECL_LANG_SPECIFIC (lhs
)
140 && LOCAL_SLOT_P (lhs
)
141 && TREE_CODE (lhs_type
) == POINTER_TYPE
)
143 tree new_lhs
= java_replace_reference (lhs
, /* want_lvalue */ true);
144 tree new_rhs
= build1 (NOP_EXPR
, TREE_TYPE (new_lhs
), rhs
);
145 modify_expr
= build2 (GIMPLE_MODIFY_STMT
, TREE_TYPE (new_lhs
),
147 modify_expr
= build1 (NOP_EXPR
, lhs_type
, modify_expr
);
149 else if (lhs_type
!= TREE_TYPE (rhs
))
150 /* Fix up type mismatches to make legal GIMPLE. These are
151 generated in several places, in particular null pointer
152 assignment and subclass assignment. */
153 TREE_OPERAND (modify_expr
, 1) = convert (lhs_type
, rhs
);
155 *modify_expr_p
= modify_expr
;
159 /* Special case handling for volatiles: we need to generate a barrier
160 between the reading and the writing. */
162 static enum gimplify_status
163 java_gimplify_self_mod_expr (tree
*expr_p
, tree
*pre_p ATTRIBUTE_UNUSED
,
164 tree
*post_p ATTRIBUTE_UNUSED
)
166 tree lhs
= TREE_OPERAND (*expr_p
, 0);
168 if (TREE_CODE (lhs
) == COMPONENT_REF
169 && TREE_THIS_VOLATILE (TREE_OPERAND (lhs
, 1)))
170 TREE_THIS_VOLATILE (lhs
) = 1;
176 /* Gimplify BLOCK into a BIND_EXPR. */
179 java_gimplify_block (tree java_block
)
181 tree decls
= BLOCK_VARS (java_block
);
182 tree body
= BLOCK_EXPR_BODY (java_block
);
183 tree outer
= gimple_current_bind_expr ();
186 /* Don't bother with empty blocks. */
188 return build_empty_stmt ();
190 if (IS_EMPTY_STMT (body
))
193 /* Make a proper block. Java blocks are unsuitable for BIND_EXPR
194 because they use BLOCK_SUBBLOCKS for another purpose. */
195 block
= make_node (BLOCK
);
196 BLOCK_VARS (block
) = decls
;
198 /* The TREE_USED flag on a block determines whether the debug output
199 routines generate info for the variables in that block. */
200 TREE_USED (block
) = 1;
202 if (outer
!= NULL_TREE
)
204 outer
= BIND_EXPR_BLOCK (outer
);
205 BLOCK_SUBBLOCKS (outer
) = chainon (BLOCK_SUBBLOCKS (outer
), block
);
207 BLOCK_EXPR_BODY (java_block
) = NULL_TREE
;
209 return build3 (BIND_EXPR
, TREE_TYPE (java_block
), decls
, body
, block
);
212 /* Dump a tree of some kind. This is a convenience wrapper for the
213 dump_* functions in tree-dump.c. */
215 dump_java_tree (enum tree_dump_index phase
, tree t
)
220 stream
= dump_begin (phase
, &flags
);
224 dump_node (t
, flags
, stream
);
225 dump_end (phase
, stream
);