OSDN Git Service

2008-07-28 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.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 "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 *, gimple_seq *,
37                                                          gimple_seq *);
38
39 static void dump_java_tree (enum tree_dump_index, tree);
40
41 /* Convert a Java tree to GENERIC.  */
42
43 void
44 java_genericize (tree fndecl)
45 {
46   dump_java_tree (TDI_original, fndecl);
47
48   /* Genericize with the gimplifier.  */
49   gimplify_function_tree (fndecl);
50
51   dump_function (TDI_generic, fndecl);
52 }
53
54 /* Gimplify a Java tree.  */
55
56 int
57 java_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
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     case MODIFY_EXPR:
72       return java_gimplify_modify_expr (expr_p);
73
74     case SAVE_EXPR:
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);
82       return GS_UNHANDLED;
83
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);
89       
90     /* These should already be lowered before we get here.  */
91     case URSHIFT_EXPR:
92     case COMPARE_EXPR:
93     case COMPARE_L_EXPR:
94     case COMPARE_G_EXPR:
95       gcc_unreachable ();
96
97     default:
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.  
103
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.  */
107
108       if (TREE_CODE_CLASS (code) == tcc_binary
109           || TREE_CODE_CLASS (code) == tcc_comparison)
110         {
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)
115             return stat;
116         }
117
118       return GS_UNHANDLED;
119     }
120
121   return GS_OK;
122 }
123
124 static enum gimplify_status
125 java_gimplify_modify_expr (tree *modify_expr_p)
126 {
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);
131
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)
139     {
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),
143                             new_lhs, new_rhs);
144       modify_expr = build1 (NOP_EXPR, lhs_type, modify_expr);
145     }
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);
151
152   *modify_expr_p = modify_expr;
153   return GS_UNHANDLED;
154 }
155
156 /*  Special case handling for volatiles: we need to generate a barrier
157     between the reading and the writing.  */
158
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)
162 {
163   tree lhs = TREE_OPERAND (*expr_p, 0);
164
165   if (TREE_CODE (lhs) == COMPONENT_REF
166       && TREE_THIS_VOLATILE (TREE_OPERAND (lhs, 1)))
167     TREE_THIS_VOLATILE (lhs) = 1;
168
169   return GS_UNHANDLED;
170 }
171
172     
173 /* Gimplify BLOCK into a BIND_EXPR.  */
174
175 static tree
176 java_gimplify_block (tree java_block)
177 {
178   tree decls = BLOCK_VARS (java_block);
179   tree body = BLOCK_EXPR_BODY (java_block);
180   gimple outer = gimple_current_bind_expr ();
181   tree block;
182
183   /* Don't bother with empty blocks.  */
184   if (! body)
185     return build_empty_stmt ();
186
187   if (IS_EMPTY_STMT (body))
188     return body;
189
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;
194
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;
198
199   if (outer != NULL)
200     {
201       tree b = gimple_bind_block (outer);
202       BLOCK_SUBBLOCKS (b) = chainon (BLOCK_SUBBLOCKS (b), block);
203     }
204   BLOCK_EXPR_BODY (java_block) = NULL_TREE;
205
206   return build3 (BIND_EXPR, TREE_TYPE (java_block), decls, body, block);
207 }
208
209 /* Dump a tree of some kind.  This is a convenience wrapper for the
210    dump_* functions in tree-dump.c.  */
211 static void
212 dump_java_tree (enum tree_dump_index phase, tree t)
213 {
214   FILE *stream;
215   int flags;
216
217   stream = dump_begin (phase, &flags);
218   flags |= TDF_SLIM;
219   if (stream)
220     {
221       dump_node (t, flags, stream);
222       dump_end (phase, stream);
223     }
224 }