OSDN Git Service

* config/rs6000/darwin.h (SUBTARGET_OVERRIDE_OPTIONS): Use %qs
[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_labeled_block_expr (tree);
36 static tree java_gimplify_exit_block_expr (tree);
37 static tree java_gimplify_case_expr (tree);
38 static tree java_gimplify_default_expr (tree);
39 static tree java_gimplify_block (tree);
40 static tree java_gimplify_new_array_init (tree);
41 static tree java_gimplify_try_expr (tree);
42 static tree java_gimplify_modify_expr (tree);
43
44 static void dump_java_tree (enum tree_dump_index, tree);
45
46 /* Convert a Java tree to GENERIC.  */
47
48 void
49 java_genericize (tree fndecl)
50 {
51   dump_java_tree (TDI_original, fndecl);
52
53   /* Genericize with the gimplifier.  */
54   gimplify_function_tree (fndecl);
55
56   dump_function (TDI_generic, fndecl);
57 }
58
59 /* Gimplify a Java tree.  */
60
61 int
62 java_gimplify_expr (tree *expr_p, tree *pre_p ATTRIBUTE_UNUSED,
63                     tree *post_p ATTRIBUTE_UNUSED)
64 {
65   enum tree_code code = TREE_CODE (*expr_p);
66
67   switch (code)
68     {
69     case BLOCK:
70       *expr_p = java_gimplify_block (*expr_p);
71       break;
72
73     case EXPR_WITH_FILE_LOCATION:
74 #ifdef USE_MAPPED_LOCATION
75       input_location = EXPR_LOCATION (*expr_p);
76 #else
77       input_location.file = EXPR_WFL_FILENAME (*expr_p);
78       input_location.line = EXPR_WFL_LINENO (*expr_p);
79 #endif
80       *expr_p = EXPR_WFL_NODE (*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 CASE_EXPR:
93       *expr_p = java_gimplify_case_expr (*expr_p);
94       break;
95
96     case DEFAULT_EXPR:
97       *expr_p = java_gimplify_default_expr (*expr_p);
98       break;
99
100     case NEW_ARRAY_INIT:
101       *expr_p = java_gimplify_new_array_init (*expr_p);
102       break;
103
104     case TRY_EXPR:
105       *expr_p = java_gimplify_try_expr (*expr_p);
106       break;
107
108     case JAVA_CATCH_EXPR:
109       *expr_p = TREE_OPERAND (*expr_p, 0);
110       break;
111
112     case JAVA_EXC_OBJ_EXPR:
113       *expr_p = build_exception_object_ref (TREE_TYPE (*expr_p));
114       break;
115
116     case VAR_DECL:
117       *expr_p = java_replace_reference (*expr_p, /* want_lvalue */ false);
118       return GS_UNHANDLED;
119
120     case MODIFY_EXPR:
121       *expr_p = java_gimplify_modify_expr (*expr_p);
122       return GS_UNHANDLED;
123
124     case SAVE_EXPR:
125       /* Note that we can see <save_expr NULL> if the save_expr was
126          already handled by gimplify_save_expr.  */
127       if (TREE_OPERAND (*expr_p, 0) != NULL_TREE
128           && TREE_CODE (TREE_OPERAND (*expr_p, 0)) == VAR_DECL)
129         TREE_OPERAND (*expr_p, 0) 
130           = java_replace_reference (TREE_OPERAND (*expr_p, 0), 
131                                /* want_lvalue */ false);
132       return GS_UNHANDLED;
133
134     /* These should already be lowered before we get here.  */
135     case URSHIFT_EXPR:
136     case COMPARE_EXPR:
137     case COMPARE_L_EXPR:
138     case COMPARE_G_EXPR:
139     case UNARY_PLUS_EXPR:
140     case NEW_ARRAY_EXPR:
141     case NEW_ANONYMOUS_ARRAY_EXPR:
142     case NEW_CLASS_EXPR:
143     case THIS_EXPR:
144     case SYNCHRONIZED_EXPR:
145     case CONDITIONAL_EXPR:
146     case INSTANCEOF_EXPR:
147     case CLASS_LITERAL:
148       abort ();
149
150     default:
151       /* Java insists on strict left-to-right evaluation of expressions.
152          A problem may arise if a variable used in the LHS of a binary
153          operation is altered by an assignment to that value in the RHS
154          before we've performed the operation.  So, we always copy every
155          LHS to a temporary variable.  
156
157          FIXME: Are there any other cases where we should do this?
158          Parameter lists, maybe?  Or perhaps that's unnecessary because
159          the front end already generates SAVE_EXPRs.  */
160
161       if (TREE_CODE_CLASS (code) == tcc_binary
162           || TREE_CODE_CLASS (code) == tcc_comparison)
163         {
164           enum gimplify_status stat 
165             = gimplify_expr (&TREE_OPERAND (*expr_p, 0), pre_p, post_p,
166                              is_gimple_formal_tmp_var, fb_rvalue);
167           if (stat == GS_ERROR)
168             return stat;
169         }
170
171       return GS_UNHANDLED;
172     }
173
174   return GS_OK;
175 }
176
177 /* Gimplify a LABELED_BLOCK_EXPR into a LABEL_EXPR following
178    a (possibly empty) body.  */
179
180 static tree
181 java_gimplify_labeled_block_expr (tree expr)
182 {
183   tree body = LABELED_BLOCK_BODY (expr);
184   tree label = LABELED_BLOCK_LABEL (expr);
185   tree t;
186
187   DECL_CONTEXT (label) = current_function_decl;
188   t = build (LABEL_EXPR, void_type_node, label);
189   if (body != NULL_TREE)
190     t = build (COMPOUND_EXPR, void_type_node, body, t);
191   return t;
192 }
193
194 /* Gimplify a EXIT_BLOCK_EXPR into a GOTO_EXPR.  */
195
196 static tree
197 java_gimplify_exit_block_expr (tree expr)
198 {
199   tree labeled_block = EXIT_BLOCK_LABELED_BLOCK (expr);
200   tree label;
201
202   /* First operand must be a LABELED_BLOCK_EXPR, which should
203      already be lowered (or partially lowered) when we get here.  */
204   gcc_assert (TREE_CODE (labeled_block) == LABELED_BLOCK_EXPR);
205
206   label = LABELED_BLOCK_LABEL (labeled_block);
207   return build1 (GOTO_EXPR, void_type_node, label);
208 }
209
210 /* This is specific to the bytecode compiler.  If a variable has
211    LOCAL_SLOT_P set, replace an assignment to it with an assignment to
212    the corresponding variable that holds all its aliases.  */
213
214 static tree
215 java_gimplify_modify_expr (tree modify_expr)
216 {
217   tree lhs = TREE_OPERAND (modify_expr, 0);
218   tree rhs = TREE_OPERAND (modify_expr, 1);
219   tree lhs_type = TREE_TYPE (lhs);
220   
221   if (TREE_CODE (lhs) == VAR_DECL
222       && DECL_LANG_SPECIFIC (lhs)
223       && LOCAL_SLOT_P (lhs)
224       && TREE_CODE (lhs_type) == POINTER_TYPE)
225     {
226       tree new_lhs = java_replace_reference (lhs, /* want_lvalue */ true);
227       tree new_rhs = build1 (NOP_EXPR, TREE_TYPE (new_lhs), rhs);
228       modify_expr = build2 (MODIFY_EXPR, TREE_TYPE (new_lhs),
229                             new_lhs, new_rhs);
230       modify_expr = build1 (NOP_EXPR, lhs_type, modify_expr);
231     }
232   
233   return modify_expr;
234 }
235
236     
237 static tree
238 java_gimplify_case_expr (tree expr)
239 {
240   tree label = create_artificial_label ();
241   return build3 (CASE_LABEL_EXPR, void_type_node,
242                  TREE_OPERAND (expr, 0), NULL_TREE, label);
243 }
244
245 static tree
246 java_gimplify_default_expr (tree expr ATTRIBUTE_UNUSED)
247 {
248   tree label = create_artificial_label ();
249   return build3 (CASE_LABEL_EXPR, void_type_node, NULL_TREE, NULL_TREE, label);
250 }
251
252 /* Gimplify BLOCK into a BIND_EXPR.  */
253
254 static tree
255 java_gimplify_block (tree java_block)
256 {
257   tree decls = BLOCK_VARS (java_block);
258   tree body = BLOCK_EXPR_BODY (java_block);
259   tree outer = gimple_current_bind_expr ();
260   tree block;
261
262   /* Don't bother with empty blocks.  */
263   if (! body)
264     return build_empty_stmt ();
265
266   if (IS_EMPTY_STMT (body))
267     return body;
268
269   /* Make a proper block.  Java blocks are unsuitable for BIND_EXPR
270      because they use BLOCK_SUBBLOCKS for another purpose.  */
271   block = make_node (BLOCK);
272   BLOCK_VARS (block) = decls;
273
274   /* The TREE_USED flag on a block determines whether the debug ouput
275      routines generate info for the variables in that block.  */
276   TREE_USED (block) = 1;
277
278   if (outer != NULL_TREE)
279     {
280       outer = BIND_EXPR_BLOCK (outer);
281       BLOCK_SUBBLOCKS (outer) = chainon (BLOCK_SUBBLOCKS (outer), block);
282     }
283
284   return build3 (BIND_EXPR, TREE_TYPE (java_block), decls, body, block);
285 }
286
287 /* Gimplify a NEW_ARRAY_INIT node into array/element assignments.  */
288
289 static tree
290 java_gimplify_new_array_init (tree exp)
291 {
292   tree array_type = TREE_TYPE (TREE_TYPE (exp));
293   tree data_field = lookup_field (&array_type, get_identifier ("data"));
294   tree element_type = TYPE_ARRAY_ELEMENT (array_type);
295   HOST_WIDE_INT ilength = java_array_type_length (array_type);
296   tree length = build_int_cst (NULL_TREE, ilength);
297   tree init = TREE_OPERAND (exp, 0);
298   tree values = CONSTRUCTOR_ELTS (init);
299
300   tree array_ptr_type = build_pointer_type (array_type);
301   tree tmp = create_tmp_var (array_ptr_type, "array");
302   tree body = build2 (MODIFY_EXPR, array_ptr_type, tmp,
303                       build_new_array (element_type, length));
304
305   int index = 0;
306
307   /* FIXME: try to allocate array statically?  */
308   while (values != NULL_TREE)
309     {
310       /* FIXME: Should use build_java_arrayaccess here, but avoid
311          bounds checking.  */
312       tree lhs = build3 (COMPONENT_REF, TREE_TYPE (data_field),    
313                          build_java_indirect_ref (array_type, tmp, 0),
314                          data_field, NULL_TREE);
315       tree assignment = build2 (MODIFY_EXPR, element_type,
316                                 build4 (ARRAY_REF, element_type, lhs,
317                                         build_int_cst (NULL_TREE, index++),
318                                         NULL_TREE, NULL_TREE),
319                                 TREE_VALUE (values));
320       body = build2 (COMPOUND_EXPR, element_type, body, assignment);
321       values = TREE_CHAIN (values);
322     }
323
324   return build2 (COMPOUND_EXPR, array_ptr_type, body, tmp);
325 }
326
327 static tree
328 java_gimplify_try_expr (tree try_expr)
329 {
330   tree body = TREE_OPERAND (try_expr, 0);
331   tree handler = TREE_OPERAND (try_expr, 1);
332   tree catch = NULL_TREE;
333
334   /* Build a CATCH_EXPR for each handler.  */
335   while (handler)
336     {
337       tree java_catch = TREE_OPERAND (handler, 0);
338       tree catch_type = TREE_TYPE (TREE_TYPE (BLOCK_EXPR_DECLS (java_catch)));
339       tree expr = build2 (CATCH_EXPR, void_type_node,
340                           prepare_eh_table_type (catch_type),
341                           handler);
342       if (catch)
343         catch = build2 (COMPOUND_EXPR, void_type_node, catch, expr);
344       else
345         catch = expr;
346       handler = TREE_CHAIN (handler);
347     }
348   return build2 (TRY_CATCH_EXPR, void_type_node, body, catch);
349 }
350
351 /* Dump a tree of some kind.  This is a convenience wrapper for the
352    dump_* functions in tree-dump.c.  */
353 static void
354 dump_java_tree (enum tree_dump_index phase, tree t)
355 {
356   FILE *stream;
357   int flags;
358
359   stream = dump_begin (phase, &flags);
360   flags |= TDF_SLIM;
361   if (stream)
362     {
363       dump_node (t, flags, stream);
364       dump_end (phase, stream);
365     }
366 }