boehm.c: Don't use bitmap as gc_descr if pointer is misaligned.
[gcc.git] / gcc / java / java-gimplify.c
1 /* Java(TM) language-specific gimplification routines.
2 Copyright (C) 2003, 2004, 2006, 2007, 2007 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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)
9 any later version.
10
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.
15
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/>.
19
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. */
23
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "tree.h"
29 #include "java-tree.h"
30 #include "tree-dump.h"
31 #include "tree-gimple.h"
32 #include "toplev.h"
33
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 *);
37
38 static void dump_java_tree (enum tree_dump_index, tree);
39
40 /* Convert a Java tree to GENERIC. */
41
42 void
43 java_genericize (tree fndecl)
44 {
45 dump_java_tree (TDI_original, fndecl);
46
47 /* Genericize with the gimplifier. */
48 gimplify_function_tree (fndecl);
49
50 dump_function (TDI_generic, fndecl);
51 }
52
53 /* Gimplify a Java tree. */
54
55 int
56 java_gimplify_expr (tree *expr_p, tree *pre_p ATTRIBUTE_UNUSED,
57 tree *post_p ATTRIBUTE_UNUSED)
58 {
59 enum tree_code code = TREE_CODE (*expr_p);
60
61 switch (code)
62 {
63 case BLOCK:
64 *expr_p = java_gimplify_block (*expr_p);
65 break;
66
67 case VAR_DECL:
68 *expr_p = java_replace_reference (*expr_p, /* want_lvalue */ false);
69 return GS_UNHANDLED;
70
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. */
74 case MODIFY_EXPR:
75 return java_gimplify_modify_expr (expr_p);
76
77 case SAVE_EXPR:
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);
85 return GS_UNHANDLED;
86
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);
92
93 /* These should already be lowered before we get here. */
94 case URSHIFT_EXPR:
95 case COMPARE_EXPR:
96 case COMPARE_L_EXPR:
97 case COMPARE_G_EXPR:
98 gcc_unreachable ();
99
100 default:
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.
106
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. */
110
111 if (TREE_CODE_CLASS (code) == tcc_binary
112 || TREE_CODE_CLASS (code) == tcc_comparison)
113 {
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)
118 return stat;
119 }
120
121 return GS_UNHANDLED;
122 }
123
124 return GS_OK;
125 }
126
127 static enum gimplify_status
128 java_gimplify_modify_expr (tree *modify_expr_p)
129 {
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);
134
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)
142 {
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),
146 new_lhs, new_rhs);
147 modify_expr = build1 (NOP_EXPR, lhs_type, modify_expr);
148 }
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);
154
155 *modify_expr_p = modify_expr;
156 return GS_UNHANDLED;
157 }
158
159 /* Special case handling for volatiles: we need to generate a barrier
160 between the reading and the writing. */
161
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)
165 {
166 tree lhs = TREE_OPERAND (*expr_p, 0);
167
168 if (TREE_CODE (lhs) == COMPONENT_REF
169 && TREE_THIS_VOLATILE (TREE_OPERAND (lhs, 1)))
170 TREE_THIS_VOLATILE (lhs) = 1;
171
172 return GS_UNHANDLED;
173 }
174
175
176 /* Gimplify BLOCK into a BIND_EXPR. */
177
178 static tree
179 java_gimplify_block (tree java_block)
180 {
181 tree decls = BLOCK_VARS (java_block);
182 tree body = BLOCK_EXPR_BODY (java_block);
183 tree outer = gimple_current_bind_expr ();
184 tree block;
185
186 /* Don't bother with empty blocks. */
187 if (! body)
188 return build_empty_stmt ();
189
190 if (IS_EMPTY_STMT (body))
191 return body;
192
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;
197
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;
201
202 if (outer != NULL_TREE)
203 {
204 outer = BIND_EXPR_BLOCK (outer);
205 BLOCK_SUBBLOCKS (outer) = chainon (BLOCK_SUBBLOCKS (outer), block);
206 }
207 BLOCK_EXPR_BODY (java_block) = NULL_TREE;
208
209 return build3 (BIND_EXPR, TREE_TYPE (java_block), decls, body, block);
210 }
211
212 /* Dump a tree of some kind. This is a convenience wrapper for the
213 dump_* functions in tree-dump.c. */
214 static void
215 dump_java_tree (enum tree_dump_index phase, tree t)
216 {
217 FILE *stream;
218 int flags;
219
220 stream = dump_begin (phase, &flags);
221 flags |= TDF_SLIM;
222 if (stream)
223 {
224 dump_node (t, flags, stream);
225 dump_end (phase, stream);
226 }
227 }