OSDN Git Service

2004-08-10 Bryce McKinlay <mckinlay@redhat.com>
[pf3gnuchains/gcc-fork.git] / gcc / java / java-gimplify.c
1 /* Java(TM) language-specific gimplification routines.
2    Copyright (C) 2003, 2004 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, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, 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_case_expr (tree);
36 static tree java_gimplify_default_expr (tree);
37 static tree java_gimplify_block (tree);
38 static tree java_gimplify_new_array_init (tree);
39 static tree java_gimplify_try_expr (tree);
40 static tree java_gimplify_modify_expr (tree);
41
42 static void dump_java_tree (enum tree_dump_index, tree);
43
44 /* Convert a Java tree to GENERIC.  */
45
46 void
47 java_genericize (tree fndecl)
48 {
49   dump_java_tree (TDI_original, fndecl);
50
51   /* Genericize with the gimplifier.  */
52   gimplify_function_tree (fndecl);
53
54   dump_function (TDI_generic, fndecl);
55 }
56
57 /* Gimplify a Java tree.  */
58
59 int
60 java_gimplify_expr (tree *expr_p, tree *pre_p ATTRIBUTE_UNUSED,
61                     tree *post_p ATTRIBUTE_UNUSED)
62 {
63   char code_class = TREE_CODE_CLASS(TREE_CODE (*expr_p));
64
65   /* Java insists on strict left-to-right evaluation of expressions.
66      A problem may arise if a variable used in the LHS of a binary
67      operation is altered by an assignment to that value in the RHS
68      before we've performed the operation.  So, we always copy every
69      LHS to a temporary variable.  
70
71      FIXME: Are there any other cases where we should do this?
72      Parameter lists, maybe?  Or perhaps that's unnecessary because
73      the front end already generates SAVE_EXPRs.  */
74   if (code_class == '2')
75     {
76       tree lhs = TREE_OPERAND (*expr_p, 0);
77       enum gimplify_status stat 
78         = gimplify_expr (&lhs, pre_p, post_p, is_gimple_tmp_var, fb_rvalue);
79       if (stat == GS_ERROR)
80         return stat;
81       TREE_OPERAND (*expr_p, 0) = lhs;
82     }
83
84   switch (TREE_CODE (*expr_p))
85     {
86     case BLOCK:
87       *expr_p = java_gimplify_block (*expr_p);
88       break;
89
90     case EXPR_WITH_FILE_LOCATION:
91       input_location.file = EXPR_WFL_FILENAME (*expr_p);
92       input_location.line = EXPR_WFL_LINENO (*expr_p);
93       *expr_p = EXPR_WFL_NODE (*expr_p);
94       annotate_with_locus (*expr_p, input_location);
95       break;
96
97     case CASE_EXPR:
98       *expr_p = java_gimplify_case_expr (*expr_p);
99       break;
100
101     case DEFAULT_EXPR:
102       *expr_p = java_gimplify_default_expr (*expr_p);
103       break;
104
105     case NEW_ARRAY_INIT:
106       *expr_p = java_gimplify_new_array_init (*expr_p);
107       break;
108
109     case TRY_EXPR:
110       *expr_p = java_gimplify_try_expr (*expr_p);
111       break;
112
113     case JAVA_CATCH_EXPR:
114       *expr_p = TREE_OPERAND (*expr_p, 0);
115       break;
116
117     case JAVA_EXC_OBJ_EXPR:
118       *expr_p = build_exception_object_ref (TREE_TYPE (*expr_p));
119       break;
120
121     case VAR_DECL:
122       *expr_p = java_replace_reference (*expr_p, /* want_lvalue */ false);
123       return GS_UNHANDLED;
124
125     case MODIFY_EXPR:
126       *expr_p = java_gimplify_modify_expr (*expr_p);
127       return GS_UNHANDLED;
128
129     case SAVE_EXPR:
130       if (TREE_CODE (TREE_OPERAND (*expr_p, 0)) == VAR_DECL)
131         TREE_OPERAND (*expr_p, 0) 
132           = java_replace_reference (TREE_OPERAND (*expr_p, 0), 
133                                /* want_lvalue */ false);
134       return GS_UNHANDLED;
135
136     /* These should already be lowered before we get here.  */
137     case URSHIFT_EXPR:
138     case COMPARE_EXPR:
139     case COMPARE_L_EXPR:
140     case COMPARE_G_EXPR:
141     case UNARY_PLUS_EXPR:
142     case NEW_ARRAY_EXPR:
143     case NEW_ANONYMOUS_ARRAY_EXPR:
144     case NEW_CLASS_EXPR:
145     case THIS_EXPR:
146     case SYNCHRONIZED_EXPR:
147     case CONDITIONAL_EXPR:
148     case INSTANCEOF_EXPR:
149     case CLASS_LITERAL:
150       abort ();
151
152     default:
153       return GS_UNHANDLED;
154     }
155
156   return GS_OK;
157 }
158
159 /* This is specific to the bytecode compiler.  If a variable has
160    LOCAL_SLOT_P set, replace an assignment to it with an assignment to
161    the corresponding variable that holds all its aliases.  */
162
163 static tree
164 java_gimplify_modify_expr (tree modify_expr)
165 {
166   tree lhs = TREE_OPERAND (modify_expr, 0);
167   tree rhs = TREE_OPERAND (modify_expr, 1);
168   tree lhs_type = TREE_TYPE (lhs);
169   
170   if (TREE_CODE (lhs) == VAR_DECL
171       && DECL_LANG_SPECIFIC (lhs)
172       && LOCAL_SLOT_P (lhs)
173       && TREE_CODE (lhs_type) == POINTER_TYPE)
174     {
175       tree new_lhs = java_replace_reference (lhs, /* want_lvalue */ true);
176       tree new_rhs = build1 (NOP_EXPR, TREE_TYPE (new_lhs), rhs);
177       modify_expr = build2 (MODIFY_EXPR, TREE_TYPE (new_lhs),
178                             new_lhs, new_rhs);
179       modify_expr = build1 (NOP_EXPR, lhs_type, modify_expr);
180     }
181   
182   return modify_expr;
183 }
184
185     
186 static tree
187 java_gimplify_case_expr (tree expr)
188 {
189   tree label = create_artificial_label ();
190   return build3 (CASE_LABEL_EXPR, void_type_node,
191                  TREE_OPERAND (expr, 0), NULL_TREE, label);
192 }
193
194 static tree
195 java_gimplify_default_expr (tree expr ATTRIBUTE_UNUSED)
196 {
197   tree label = create_artificial_label ();
198   return build3 (CASE_LABEL_EXPR, void_type_node, NULL_TREE, NULL_TREE, label);
199 }
200
201 /* Gimplify BLOCK into a BIND_EXPR.  */
202
203 static tree
204 java_gimplify_block (tree java_block)
205 {
206   tree decls = BLOCK_VARS (java_block);
207   tree body = BLOCK_EXPR_BODY (java_block);
208   tree outer = gimple_current_bind_expr ();
209   tree block;
210
211   /* Don't bother with empty blocks.  */
212   if (! body)
213     return build_empty_stmt ();
214
215   if (IS_EMPTY_STMT (body))
216     return body;
217
218   /* Make a proper block.  Java blocks are unsuitable for BIND_EXPR
219      because they use BLOCK_SUBBLOCKS for another purpose.  */
220   block = make_node (BLOCK);
221   BLOCK_VARS (block) = decls;
222
223   /* The TREE_USED flag on a block determines whether the debug ouput
224      routines generate info for the variables in that block.  */
225   TREE_USED (block) = 1;
226
227   if (outer != NULL_TREE)
228     {
229       outer = BIND_EXPR_BLOCK (outer);
230       BLOCK_SUBBLOCKS (outer) = chainon (BLOCK_SUBBLOCKS (outer), block);
231     }
232
233   return build3 (BIND_EXPR, TREE_TYPE (java_block), decls, body, block);
234 }
235
236 /* Gimplify a NEW_ARRAY_INIT node into array/element assignments.  */
237
238 static tree
239 java_gimplify_new_array_init (tree exp)
240 {
241   tree array_type = TREE_TYPE (TREE_TYPE (exp));
242   tree data_field = lookup_field (&array_type, get_identifier ("data"));
243   tree element_type = TYPE_ARRAY_ELEMENT (array_type);
244   HOST_WIDE_INT ilength = java_array_type_length (array_type);
245   tree length = build_int_2 (ilength, 0);
246   tree init = TREE_OPERAND (exp, 0);
247   tree values = CONSTRUCTOR_ELTS (init);
248
249   tree array_ptr_type = build_pointer_type (array_type);
250   tree tmp = create_tmp_var (array_ptr_type, "array");
251   tree body = build2 (MODIFY_EXPR, array_ptr_type, tmp,
252                       build_new_array (element_type, length));
253
254   int index = 0;
255
256   /* FIXME: try to allocate array statically?  */
257   while (values != NULL_TREE)
258     {
259       /* FIXME: Should use build_java_arrayaccess here, but avoid
260          bounds checking.  */
261       tree lhs = build3 (COMPONENT_REF, TREE_TYPE (data_field),    
262                          build_java_indirect_ref (array_type, tmp, 0),
263                          data_field, NULL_TREE);
264       tree assignment = build2 (MODIFY_EXPR, element_type,
265                                 build4 (ARRAY_REF, element_type, lhs,
266                                         build_int_2 (index++, 0),
267                                         NULL_TREE, NULL_TREE),
268                                 TREE_VALUE (values));
269       body = build2 (COMPOUND_EXPR, element_type, body, assignment);
270       values = TREE_CHAIN (values);
271     }
272
273   return build2 (COMPOUND_EXPR, array_ptr_type, body, tmp);
274 }
275
276 static tree
277 java_gimplify_try_expr (tree try_expr)
278 {
279   tree body = TREE_OPERAND (try_expr, 0);
280   tree handler = TREE_OPERAND (try_expr, 1);
281   tree catch = NULL_TREE;
282
283   /* Build a CATCH_EXPR for each handler.  */
284   while (handler)
285     {
286       tree java_catch = TREE_OPERAND (handler, 0);
287       tree catch_type = TREE_TYPE (TREE_TYPE (BLOCK_EXPR_DECLS (java_catch)));
288       tree expr = build2 (CATCH_EXPR, void_type_node,
289                           prepare_eh_table_type (catch_type),
290                           handler);
291       if (catch)
292         catch = build2 (COMPOUND_EXPR, void_type_node, catch, expr);
293       else
294         catch = expr;
295       handler = TREE_CHAIN (handler);
296     }
297   return build2 (TRY_CATCH_EXPR, void_type_node, body, catch);
298 }
299
300 /* Dump a tree of some kind.  This is a convenience wrapper for the
301    dump_* functions in tree-dump.c.  */
302 static void
303 dump_java_tree (enum tree_dump_index phase, tree t)
304 {
305   FILE *stream;
306   int flags;
307
308   stream = dump_begin (phase, &flags);
309   flags |= TDF_SLIM;
310   if (stream)
311     {
312       dump_node (t, flags, stream);
313       dump_end (phase, stream);
314     }
315 }