OSDN Git Service

* lang.c (dump_compound_expr) <EXPR_WITH_FILE_LOCATION>: Removed
[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 LABELED_BLOCK_EXPR:
73       *expr_p = java_gimplify_labeled_block_expr (*expr_p);
74       break;
75
76     case EXIT_BLOCK_EXPR:
77       *expr_p = java_gimplify_exit_block_expr (*expr_p);
78       break;
79
80     case TRY_EXPR:
81       *expr_p = java_gimplify_try_expr (*expr_p);
82       break;
83
84     case VAR_DECL:
85       *expr_p = java_replace_reference (*expr_p, /* want_lvalue */ false);
86       return GS_UNHANDLED;
87
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);
93
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;
103
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);
109       
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 ();
116
117     case COMPONENT_REF:
118       return java_gimplify_component_ref (expr_p, pre_p, post_p);
119
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.  
126
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.  */
130
131       if (TREE_CODE_CLASS (code) == tcc_binary
132           || TREE_CODE_CLASS (code) == tcc_comparison)
133         {
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;
139         }
140
141       return GS_UNHANDLED;
142     }
143
144   return GS_OK;
145 }
146
147 /* Gimplify a LABELED_BLOCK_EXPR into a LABEL_EXPR following
148    a (possibly empty) body.  */
149
150 static tree
151 java_gimplify_labeled_block_expr (tree expr)
152 {
153   tree body = LABELED_BLOCK_BODY (expr);
154   tree label = LABELED_BLOCK_LABEL (expr);
155   tree t;
156
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;
162 }
163
164 /* Gimplify a EXIT_BLOCK_EXPR into a GOTO_EXPR.  */
165
166 static tree
167 java_gimplify_exit_block_expr (tree expr)
168 {
169   tree labeled_block = EXIT_BLOCK_LABELED_BLOCK (expr);
170   tree label;
171
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);
175
176   label = LABELED_BLOCK_LABEL (labeled_block);
177   return build1 (GOTO_EXPR, void_type_node, label);
178 }
179
180
181
182 static enum gimplify_status
183 java_gimplify_component_ref (tree *expr_p, tree *pre_p, tree *post_p)
184 {
185   if (CLASS_FROM_SOURCE_P (output_class)
186       && TREE_THIS_VOLATILE (TREE_OPERAND (*expr_p, 1))
187       && ! TREE_THIS_VOLATILE (*expr_p))
188   {
189     enum gimplify_status stat;
190     tree sync_expr;
191
192     /* Special handling for volatile fields.  
193
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.  
197
198     This logic only handles loads: stores are handled in
199     java_gimplify_modify_expr().
200
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.
205
206     */
207   
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;
214
215     sync_expr 
216       = build3 (CALL_EXPR, void_type_node,
217                 build_address_of (built_in_decls[BUILT_IN_SYNCHRONIZE]),
218                 NULL_TREE, NULL_TREE);
219     TREE_SIDE_EFFECTS (sync_expr) = 1;
220     *expr_p = build2 (COMPOUND_EXPR, TREE_TYPE (*expr_p),
221                       sync_expr, *expr_p);
222     TREE_SIDE_EFFECTS (*expr_p) = 1;
223   }
224
225   return GS_UNHANDLED;
226 }
227   
228
229 static enum gimplify_status
230 java_gimplify_modify_expr (tree *modify_expr_p, tree *pre_p, tree *post_p)
231 {
232   tree modify_expr = *modify_expr_p;
233   tree lhs = TREE_OPERAND (modify_expr, 0);
234   tree rhs = TREE_OPERAND (modify_expr, 1);
235   tree lhs_type = TREE_TYPE (lhs);
236
237   if (CLASS_FROM_SOURCE_P (output_class)
238       && TREE_CODE (lhs) == COMPONENT_REF
239       && TREE_THIS_VOLATILE (TREE_OPERAND (lhs, 1)))
240     {
241       /* Special handling for volatile fields.  
242
243       A load has "acquire" semantics, implying that you can't move up
244       later operations.  A store has "release" semantics meaning that
245       earlier operations cannot be delayed past it.  
246
247       This logic only handles stores; loads are handled in
248       java_gimplify_component_ref().
249
250       We gimplify the rhs, put the result in a tmp_var, and then return
251       a MODIFY_EXPR with an rhs of the form {__sync_synchronize(); tmp_var}.
252       This forces __sync_synchronize() to be placed after evaluating
253       the rhs and immediately before storing to the volatile field.
254
255       */
256   
257       enum gimplify_status stat;
258       tree sync_expr 
259         = build3 (CALL_EXPR, void_type_node,
260                   build_address_of (built_in_decls[BUILT_IN_SYNCHRONIZE]),
261                   NULL_TREE, NULL_TREE);
262       TREE_SIDE_EFFECTS (sync_expr) = 1;
263
264       stat = gimplify_expr (&rhs, pre_p, post_p,
265                             is_gimple_formal_tmp_var, fb_rvalue);
266       if (stat == GS_ERROR)
267         return stat;
268
269       rhs = build2 (COMPOUND_EXPR, TREE_TYPE (rhs),
270                     sync_expr, rhs);
271       TREE_SIDE_EFFECTS (rhs) = 1;
272       TREE_THIS_VOLATILE (lhs) = 1;
273       lhs = java_modify_addr_for_volatile (lhs);
274       TREE_OPERAND (modify_expr, 0) = lhs;
275       TREE_OPERAND (modify_expr, 1) = rhs;
276     }
277
278   /* This is specific to the bytecode compiler.  If a variable has
279      LOCAL_SLOT_P set, replace an assignment to it with an assignment
280      to the corresponding variable that holds all its aliases.  */
281   if (TREE_CODE (lhs) == VAR_DECL
282       && DECL_LANG_SPECIFIC (lhs)
283       && LOCAL_SLOT_P (lhs)
284       && TREE_CODE (lhs_type) == POINTER_TYPE)
285     {
286       tree new_lhs = java_replace_reference (lhs, /* want_lvalue */ true);
287       tree new_rhs = build1 (NOP_EXPR, TREE_TYPE (new_lhs), rhs);
288       modify_expr = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (new_lhs),
289                             new_lhs, new_rhs);
290       modify_expr = build1 (NOP_EXPR, lhs_type, modify_expr);
291     }
292   else if (lhs_type != TREE_TYPE (rhs))
293     /* Fix up type mismatches to make legal GIMPLE.  These are
294        generated in several places, in particular null pointer
295        assignment and subclass assignment.  */
296     TREE_OPERAND (modify_expr, 1) = convert (lhs_type, rhs);
297
298   *modify_expr_p = modify_expr;
299   return GS_UNHANDLED;
300 }
301
302 /*  Special case handling for volatiles: we need to generate a barrier
303     between the reading and the writing.  */
304
305 static enum gimplify_status
306 java_gimplify_self_mod_expr (tree *expr_p, tree *pre_p ATTRIBUTE_UNUSED, 
307                              tree *post_p ATTRIBUTE_UNUSED)
308 {
309   tree lhs = TREE_OPERAND (*expr_p, 0);
310
311   if (TREE_CODE (lhs) == COMPONENT_REF
312       && TREE_THIS_VOLATILE (TREE_OPERAND (lhs, 1)))
313     TREE_THIS_VOLATILE (lhs) = 1;
314
315   return GS_UNHANDLED;
316 }
317
318     
319 /* Gimplify BLOCK into a BIND_EXPR.  */
320
321 static tree
322 java_gimplify_block (tree java_block)
323 {
324   tree decls = BLOCK_VARS (java_block);
325   tree body = BLOCK_EXPR_BODY (java_block);
326   tree outer = gimple_current_bind_expr ();
327   tree block;
328
329   /* Don't bother with empty blocks.  */
330   if (! body)
331     return build_empty_stmt ();
332
333   if (IS_EMPTY_STMT (body))
334     return body;
335
336   /* Make a proper block.  Java blocks are unsuitable for BIND_EXPR
337      because they use BLOCK_SUBBLOCKS for another purpose.  */
338   block = make_node (BLOCK);
339   BLOCK_VARS (block) = decls;
340
341   /* The TREE_USED flag on a block determines whether the debug output
342      routines generate info for the variables in that block.  */
343   TREE_USED (block) = 1;
344
345   if (outer != NULL_TREE)
346     {
347       outer = BIND_EXPR_BLOCK (outer);
348       BLOCK_SUBBLOCKS (outer) = chainon (BLOCK_SUBBLOCKS (outer), block);
349     }
350   BLOCK_EXPR_BODY (java_block) = NULL_TREE;
351
352   return build3 (BIND_EXPR, TREE_TYPE (java_block), decls, body, block);
353 }
354
355 static tree
356 java_gimplify_try_expr (tree try_expr)
357 {
358   tree body = TREE_OPERAND (try_expr, 0);
359   tree handler = TREE_OPERAND (try_expr, 1);
360   tree catch = NULL_TREE;
361
362   /* Build a CATCH_EXPR for each handler.  */
363   while (handler)
364     {
365       tree java_catch = TREE_OPERAND (handler, 0);
366       tree catch_type = TREE_TYPE (TREE_TYPE (BLOCK_EXPR_DECLS (java_catch)));
367       tree expr = build2 (CATCH_EXPR, void_type_node,
368                           prepare_eh_table_type (catch_type),
369                           handler);
370       if (catch)
371         catch = build2 (COMPOUND_EXPR, void_type_node, catch, expr);
372       else
373         catch = expr;
374       handler = TREE_CHAIN (handler);
375     }
376   return build2 (TRY_CATCH_EXPR, void_type_node, body, catch);
377 }
378
379 /* Dump a tree of some kind.  This is a convenience wrapper for the
380    dump_* functions in tree-dump.c.  */
381 static void
382 dump_java_tree (enum tree_dump_index phase, tree t)
383 {
384   FILE *stream;
385   int flags;
386
387   stream = dump_begin (phase, &flags);
388   flags |= TDF_SLIM;
389   if (stream)
390     {
391       dump_node (t, flags, stream);
392       dump_end (phase, stream);
393     }
394 }