OSDN Git Service

Merged gcj-eclipse branch to trunk.
[pf3gnuchains/gcc-fork.git] / gcc / java / java-gimplify.c
1 /* Java(TM) language-specific gimplification routines.
2    Copyright (C) 2003, 2004, 2006, 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 2, 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 COPYING.  If not, write to
18 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. 
20
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.  */
24
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"
34
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 *);
42
43 static void dump_java_tree (enum tree_dump_index, tree);
44
45 /* Convert a Java tree to GENERIC.  */
46
47 void
48 java_genericize (tree fndecl)
49 {
50   dump_java_tree (TDI_original, fndecl);
51
52   /* Genericize with the gimplifier.  */
53   gimplify_function_tree (fndecl);
54
55   dump_function (TDI_generic, fndecl);
56 }
57
58 /* Gimplify a Java tree.  */
59
60 int
61 java_gimplify_expr (tree *expr_p, tree *pre_p ATTRIBUTE_UNUSED,
62                     tree *post_p ATTRIBUTE_UNUSED)
63 {
64   enum tree_code code = TREE_CODE (*expr_p);
65
66   switch (code)
67     {
68     case BLOCK:
69       *expr_p = java_gimplify_block (*expr_p);
70       break;
71
72     case EXPR_WITH_FILE_LOCATION:
73 #ifdef USE_MAPPED_LOCATION
74       input_location = EXPR_LOCATION (*expr_p);
75 #else
76       input_location.file = EXPR_WFL_FILENAME (*expr_p);
77       input_location.line = EXPR_WFL_LINENO (*expr_p);
78 #endif
79       *expr_p = EXPR_WFL_NODE (*expr_p);
80       if (EXPR_P (*expr_p))
81         SET_EXPR_LOCATION (*expr_p, input_location);
82       break;
83
84     case LABELED_BLOCK_EXPR:
85       *expr_p = java_gimplify_labeled_block_expr (*expr_p);
86       break;
87
88     case EXIT_BLOCK_EXPR:
89       *expr_p = java_gimplify_exit_block_expr (*expr_p);
90       break;
91
92     case TRY_EXPR:
93       *expr_p = java_gimplify_try_expr (*expr_p);
94       break;
95
96     case VAR_DECL:
97       *expr_p = java_replace_reference (*expr_p, /* want_lvalue */ false);
98       return GS_UNHANDLED;
99
100       /* We don't handle GIMPLE_MODIFY_STMT, as MODIFY_EXPRs with java
101          semantics should only be generated by the front-end, and never
102          by anything after gimplification.  */
103     case MODIFY_EXPR:
104       return java_gimplify_modify_expr (expr_p, pre_p, post_p);
105
106     case SAVE_EXPR:
107       /* Note that we can see <save_expr NULL> if the save_expr was
108          already handled by gimplify_save_expr.  */
109       if (TREE_OPERAND (*expr_p, 0) != NULL_TREE
110           && TREE_CODE (TREE_OPERAND (*expr_p, 0)) == VAR_DECL)
111         TREE_OPERAND (*expr_p, 0) 
112           = java_replace_reference (TREE_OPERAND (*expr_p, 0), 
113                                /* want_lvalue */ false);
114       return GS_UNHANDLED;
115
116     case POSTINCREMENT_EXPR:
117     case POSTDECREMENT_EXPR:
118     case PREINCREMENT_EXPR:
119     case PREDECREMENT_EXPR:
120       return java_gimplify_self_mod_expr (expr_p, pre_p, post_p);
121       
122     /* These should already be lowered before we get here.  */
123     case URSHIFT_EXPR:
124     case COMPARE_EXPR:
125     case COMPARE_L_EXPR:
126     case COMPARE_G_EXPR:
127       gcc_unreachable ();
128
129     case COMPONENT_REF:
130       return java_gimplify_component_ref (expr_p, pre_p, post_p);
131
132     default:
133       /* Java insists on strict left-to-right evaluation of expressions.
134          A problem may arise if a variable used in the LHS of a binary
135          operation is altered by an assignment to that value in the RHS
136          before we've performed the operation.  So, we always copy every
137          LHS to a temporary variable.  
138
139          FIXME: Are there any other cases where we should do this?
140          Parameter lists, maybe?  Or perhaps that's unnecessary because
141          the front end already generates SAVE_EXPRs.  */
142
143       if (TREE_CODE_CLASS (code) == tcc_binary
144           || TREE_CODE_CLASS (code) == tcc_comparison)
145         {
146           enum gimplify_status stat 
147             = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
148                              is_gimple_formal_tmp_var, fb_rvalue);
149           if (stat == GS_ERROR)
150             return stat;
151         }
152
153       return GS_UNHANDLED;
154     }
155
156   return GS_OK;
157 }
158
159 /* Gimplify a LABELED_BLOCK_EXPR into a LABEL_EXPR following
160    a (possibly empty) body.  */
161
162 static tree
163 java_gimplify_labeled_block_expr (tree expr)
164 {
165   tree body = LABELED_BLOCK_BODY (expr);
166   tree label = LABELED_BLOCK_LABEL (expr);
167   tree t;
168
169   DECL_CONTEXT (label) = current_function_decl;
170   t = build1 (LABEL_EXPR, void_type_node, label);
171   if (body != NULL_TREE)
172     t = build2 (COMPOUND_EXPR, void_type_node, body, t);
173   return t;
174 }
175
176 /* Gimplify a EXIT_BLOCK_EXPR into a GOTO_EXPR.  */
177
178 static tree
179 java_gimplify_exit_block_expr (tree expr)
180 {
181   tree labeled_block = EXIT_BLOCK_LABELED_BLOCK (expr);
182   tree label;
183
184   /* First operand must be a LABELED_BLOCK_EXPR, which should
185      already be lowered (or partially lowered) when we get here.  */
186   gcc_assert (TREE_CODE (labeled_block) == LABELED_BLOCK_EXPR);
187
188   label = LABELED_BLOCK_LABEL (labeled_block);
189   return build1 (GOTO_EXPR, void_type_node, label);
190 }
191
192
193
194 static enum gimplify_status
195 java_gimplify_component_ref (tree *expr_p, tree *pre_p, tree *post_p)
196 {
197   if (CLASS_FROM_SOURCE_P (output_class)
198       && TREE_THIS_VOLATILE (TREE_OPERAND (*expr_p, 1))
199       && ! TREE_THIS_VOLATILE (*expr_p))
200   {
201     enum gimplify_status stat;
202     tree sync_expr;
203
204     /* Special handling for volatile fields.  
205
206     A load has "acquire" semantics, implying that you can't move up
207     later operations.  A store has "release" semantics meaning that
208     earlier operations cannot be delayed past it.  
209
210     This logic only handles loads: stores are handled in
211     java_gimplify_modify_expr().
212
213     We gimplify this COMPONENT_REF, put the result in a tmp_var, and then
214     return a COMPOUND_EXPR of the form {__sync_synchronize(); tmp_var}.  
215     This forces __sync_synchronize() to be placed immediately after
216     loading from the volatile field.
217
218     */
219   
220     TREE_THIS_VOLATILE (*expr_p) = 1;
221     *expr_p = java_modify_addr_for_volatile (*expr_p);
222     stat = gimplify_expr (expr_p, pre_p, post_p,
223                           is_gimple_formal_tmp_var, fb_rvalue);
224     if (stat == GS_ERROR)
225       return stat;
226
227     sync_expr 
228       = build3 (CALL_EXPR, void_type_node,
229                 build_address_of (built_in_decls[BUILT_IN_SYNCHRONIZE]),
230                 NULL_TREE, NULL_TREE);
231     TREE_SIDE_EFFECTS (sync_expr) = 1;
232     *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
233                       sync_expr, *expr_p);
234     TREE_SIDE_EFFECTS (*expr_p) = 1;
235   }
236
237   return GS_UNHANDLED;
238 }
239   
240
241 static enum gimplify_status
242 java_gimplify_modify_expr (tree *modify_expr_p, tree *pre_p, tree *post_p)
243 {
244   tree modify_expr = *modify_expr_p;
245   tree lhs = TREE_OPERAND (modify_expr, 0);
246   tree rhs = TREE_OPERAND (modify_expr, 1);
247   tree lhs_type = TREE_TYPE (lhs);
248
249   if (CLASS_FROM_SOURCE_P (output_class)
250       && TREE_CODE (lhs) == COMPONENT_REF
251       && TREE_THIS_VOLATILE (TREE_OPERAND (lhs, 1)))
252     {
253       /* Special handling for volatile fields.  
254
255       A load has "acquire" semantics, implying that you can't move up
256       later operations.  A store has "release" semantics meaning that
257       earlier operations cannot be delayed past it.  
258
259       This logic only handles stores; loads are handled in
260       java_gimplify_component_ref().
261
262       We gimplify the rhs, put the result in a tmp_var, and then return
263       a MODIFY_EXPR with an rhs of the form {__sync_synchronize(); tmp_var}.
264       This forces __sync_synchronize() to be placed after evaluating
265       the rhs and immediately before storing to the volatile field.
266
267       */
268   
269       enum gimplify_status stat;
270       tree sync_expr 
271         = build3 (CALL_EXPR, void_type_node,
272                   build_address_of (built_in_decls[BUILT_IN_SYNCHRONIZE]),
273                   NULL_TREE, NULL_TREE);
274       TREE_SIDE_EFFECTS (sync_expr) = 1;
275
276       stat = gimplify_expr (&rhs, pre_p, post_p,
277                             is_gimple_formal_tmp_var, fb_rvalue);
278       if (stat == GS_ERROR)
279         return stat;
280
281       rhs = build2 (COMPOUND_EXPR, TREE_TYPE (rhs),
282                     sync_expr, rhs);
283       TREE_SIDE_EFFECTS (rhs) = 1;
284       TREE_THIS_VOLATILE (lhs) = 1;
285       lhs = java_modify_addr_for_volatile (lhs);
286       TREE_OPERAND (modify_expr, 0) = lhs;
287       TREE_OPERAND (modify_expr, 1) = rhs;
288     }
289
290   /* This is specific to the bytecode compiler.  If a variable has
291      LOCAL_SLOT_P set, replace an assignment to it with an assignment
292      to the corresponding variable that holds all its aliases.  */
293   if (TREE_CODE (lhs) == VAR_DECL
294       && DECL_LANG_SPECIFIC (lhs)
295       && LOCAL_SLOT_P (lhs)
296       && TREE_CODE (lhs_type) == POINTER_TYPE)
297     {
298       tree new_lhs = java_replace_reference (lhs, /* want_lvalue */ true);
299       tree new_rhs = build1 (NOP_EXPR, TREE_TYPE (new_lhs), rhs);
300       modify_expr = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (new_lhs),
301                             new_lhs, new_rhs);
302       modify_expr = build1 (NOP_EXPR, lhs_type, modify_expr);
303     }
304   else if (lhs_type != TREE_TYPE (rhs))
305     /* Fix up type mismatches to make legal GIMPLE.  These are
306        generated in several places, in particular null pointer
307        assignment and subclass assignment.  */
308     TREE_OPERAND (modify_expr, 1) = convert (lhs_type, rhs);
309
310   *modify_expr_p = modify_expr;
311   return GS_UNHANDLED;
312 }
313
314 /*  Special case handling for volatiles: we need to generate a barrier
315     between the reading and the writing.  */
316
317 static enum gimplify_status
318 java_gimplify_self_mod_expr (tree *expr_p, tree *pre_p ATTRIBUTE_UNUSED, 
319                              tree *post_p ATTRIBUTE_UNUSED)
320 {
321   tree lhs = TREE_OPERAND (*expr_p, 0);
322
323   if (TREE_CODE (lhs) == COMPONENT_REF
324       && TREE_THIS_VOLATILE (TREE_OPERAND (lhs, 1)))
325     TREE_THIS_VOLATILE (lhs) = 1;
326
327   return GS_UNHANDLED;
328 }
329
330     
331 /* Gimplify BLOCK into a BIND_EXPR.  */
332
333 static tree
334 java_gimplify_block (tree java_block)
335 {
336   tree decls = BLOCK_VARS (java_block);
337   tree body = BLOCK_EXPR_BODY (java_block);
338   tree outer = gimple_current_bind_expr ();
339   tree block;
340
341   /* Don't bother with empty blocks.  */
342   if (! body)
343     return build_empty_stmt ();
344
345   if (IS_EMPTY_STMT (body))
346     return body;
347
348   /* Make a proper block.  Java blocks are unsuitable for BIND_EXPR
349      because they use BLOCK_SUBBLOCKS for another purpose.  */
350   block = make_node (BLOCK);
351   BLOCK_VARS (block) = decls;
352
353   /* The TREE_USED flag on a block determines whether the debug output
354      routines generate info for the variables in that block.  */
355   TREE_USED (block) = 1;
356
357   if (outer != NULL_TREE)
358     {
359       outer = BIND_EXPR_BLOCK (outer);
360       BLOCK_SUBBLOCKS (outer) = chainon (BLOCK_SUBBLOCKS (outer), block);
361     }
362   BLOCK_EXPR_BODY (java_block) = NULL_TREE;
363
364   return build3 (BIND_EXPR, TREE_TYPE (java_block), decls, body, block);
365 }
366
367 static tree
368 java_gimplify_try_expr (tree try_expr)
369 {
370   tree body = TREE_OPERAND (try_expr, 0);
371   tree handler = TREE_OPERAND (try_expr, 1);
372   tree catch = NULL_TREE;
373
374   /* Build a CATCH_EXPR for each handler.  */
375   while (handler)
376     {
377       tree java_catch = TREE_OPERAND (handler, 0);
378       tree catch_type = TREE_TYPE (TREE_TYPE (BLOCK_EXPR_DECLS (java_catch)));
379       tree expr = build2 (CATCH_EXPR, void_type_node,
380                           prepare_eh_table_type (catch_type),
381                           handler);
382       if (catch)
383         catch = build2 (COMPOUND_EXPR, void_type_node, catch, expr);
384       else
385         catch = expr;
386       handler = TREE_CHAIN (handler);
387     }
388   return build2 (TRY_CATCH_EXPR, void_type_node, body, catch);
389 }
390
391 /* Dump a tree of some kind.  This is a convenience wrapper for the
392    dump_* functions in tree-dump.c.  */
393 static void
394 dump_java_tree (enum tree_dump_index phase, tree t)
395 {
396   FILE *stream;
397   int flags;
398
399   stream = dump_begin (phase, &flags);
400   flags |= TDF_SLIM;
401   if (stream)
402     {
403       dump_node (t, flags, stream);
404       dump_end (phase, stream);
405     }
406 }