OSDN Git Service

3c446672255efec91c0cc73e58516d9a45210108
[pf3gnuchains/gcc-fork.git] / gcc / java / java-gimplify.c
1 /* Java(TM) language-specific gimplification routines.
2
3    Copyright (C) 2003, 2004 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. 
21
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc.  */
25
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "java-tree.h"
32 #include "tree-dump.h"
33 #include "tree-gimple.h"
34 #include "toplev.h"
35
36 static tree java_gimplify_case_expr (tree);
37 static tree java_gimplify_default_expr (tree);
38 static tree java_gimplify_block (tree);
39 static tree java_gimplify_new_array_init (tree);
40 static tree java_gimplify_try_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     /* These should already be lowered before we get here.  */
122     case URSHIFT_EXPR:
123     case COMPARE_EXPR:
124     case COMPARE_L_EXPR:
125     case COMPARE_G_EXPR:
126     case UNARY_PLUS_EXPR:
127     case NEW_ARRAY_EXPR:
128     case NEW_ANONYMOUS_ARRAY_EXPR:
129     case NEW_CLASS_EXPR:
130     case THIS_EXPR:
131     case SYNCHRONIZED_EXPR:
132     case CONDITIONAL_EXPR:
133     case INSTANCEOF_EXPR:
134     case CLASS_LITERAL:
135       abort ();
136
137     default:
138       return GS_UNHANDLED;
139     }
140
141   return GS_OK;
142 }
143
144 static tree
145 java_gimplify_case_expr (tree expr)
146 {
147   tree label = create_artificial_label ();
148   return build (CASE_LABEL_EXPR, void_type_node,
149                 TREE_OPERAND (expr, 0), NULL_TREE, label);
150 }
151
152 static tree
153 java_gimplify_default_expr (tree expr ATTRIBUTE_UNUSED)
154 {
155   tree label = create_artificial_label ();
156   return build (CASE_LABEL_EXPR, void_type_node, NULL_TREE, NULL_TREE, label);
157 }
158
159 /* Gimplify BLOCK into a BIND_EXPR.  */
160
161 static tree
162 java_gimplify_block (tree java_block)
163 {
164   tree decls = BLOCK_VARS (java_block);
165   tree body = BLOCK_EXPR_BODY (java_block);
166   tree outer = gimple_current_bind_expr ();
167   tree block;
168
169   /* Don't bother with empty blocks.  */
170   if (! body)
171     return build_empty_stmt ();
172
173   if (IS_EMPTY_STMT (body))
174     return body;
175
176   /* Make a proper block.  Java blocks are unsuitable for BIND_EXPR
177      because they use BLOCK_SUBBLOCKS for another purpose.  */
178   block = make_node (BLOCK);
179   BLOCK_VARS (block) = decls;
180
181   /* The TREE_USED flag on a block determines whether the debug ouput
182      routines generate info for the variables in that block.  */
183   TREE_USED (block) = 1;
184
185   if (outer != NULL_TREE)
186     {
187       outer = BIND_EXPR_BLOCK (outer);
188       BLOCK_SUBBLOCKS (outer) = chainon (BLOCK_SUBBLOCKS (outer), block);
189     }
190
191   return build (BIND_EXPR, TREE_TYPE (java_block), decls, body, block);
192 }
193
194 /* Gimplify a NEW_ARRAY_INIT node into array/element assignments.  */
195
196 static tree
197 java_gimplify_new_array_init (tree exp)
198 {
199   tree array_type = TREE_TYPE (TREE_TYPE (exp));
200   tree data_field = lookup_field (&array_type, get_identifier ("data"));
201   tree element_type = TYPE_ARRAY_ELEMENT (array_type);
202   HOST_WIDE_INT ilength = java_array_type_length (array_type);
203   tree length = build_int_2 (ilength, 0);
204   tree init = TREE_OPERAND (exp, 0);
205   tree values = CONSTRUCTOR_ELTS (init);
206
207   tree array_ptr_type = build_pointer_type (array_type);
208   tree block = build (BLOCK, array_ptr_type);
209   tree tmp = build_decl (VAR_DECL, get_identifier ("<tmp>"), array_ptr_type);
210   tree array = build_decl (VAR_DECL, get_identifier ("<array>"), array_ptr_type);
211   tree body = build (MODIFY_EXPR, array_ptr_type, tmp,
212                      build_new_array (element_type, length));
213
214   int index = 0;
215
216   DECL_CONTEXT (array) = current_function_decl;
217   DECL_CONTEXT (tmp) = current_function_decl;
218
219   /* FIXME: try to allocate array statically?  */
220   while (values != NULL_TREE)
221     {
222       /* FIXME: Should use build_java_arrayaccess here, but avoid
223          bounds checking.  */
224       tree lhs = build (COMPONENT_REF, TREE_TYPE (data_field),    
225                         build_java_indirect_ref (array_type, tmp, 0),
226                         data_field);
227       tree assignment = build (MODIFY_EXPR, element_type,
228                                build (ARRAY_REF, element_type, lhs,
229                                       build_int_2 (index++, 0)),
230                                TREE_VALUE (values));
231       body = build (COMPOUND_EXPR, element_type, body, assignment);
232       values = TREE_CHAIN (values);
233     }
234
235   body = build (COMPOUND_EXPR, array_ptr_type, body,
236                 build (MODIFY_EXPR, array_ptr_type, array, tmp));
237   TREE_CHAIN (tmp) = array;
238   BLOCK_VARS (block) = tmp;
239   BLOCK_EXPR_BODY (block) = body;
240   return java_gimplify_block (block);
241 }
242
243 static tree
244 java_gimplify_try_expr (tree try_expr)
245 {
246   tree body = TREE_OPERAND (try_expr, 0);
247   tree handler = TREE_OPERAND (try_expr, 1);
248   tree catch = NULL_TREE;
249
250   /* Build a CATCH_EXPR for each handler.  */
251   while (handler)
252     {
253       tree java_catch = TREE_OPERAND (handler, 0);
254       tree catch_type = TREE_TYPE (TREE_TYPE (BLOCK_EXPR_DECLS (java_catch)));
255       tree expr = build (CATCH_EXPR, void_type_node,
256                          prepare_eh_table_type (catch_type),
257                          handler);
258       if (catch)
259         catch = build (COMPOUND_EXPR, void_type_node, catch, expr);
260       else
261         catch = expr;
262       handler = TREE_CHAIN (handler);
263     }
264   return build (TRY_CATCH_EXPR, void_type_node, body, catch);
265 }
266
267 /* Dump a tree of some kind.  This is a convenience wrapper for the
268    dump_* functions in tree-dump.c.  */
269 static void
270 dump_java_tree (enum tree_dump_index phase, tree t)
271 {
272   FILE *stream;
273   int flags;
274
275   stream = dump_begin (phase, &flags);
276   flags |= TDF_SLIM;
277   if (stream)
278     {
279       dump_node (t, flags, stream);
280       dump_end (phase, stream);
281     }
282 }