Imported Upstream version 4.8.1
[platform/upstream/gcc48.git] / gcc / java / java-gimplify.c
1 /* Java(TM) language-specific gimplification routines.
2    Copyright (C) 2003-2013 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 "tree.h"
28 #include "java-tree.h"
29 #include "dumpfile.h"
30 #include "gimple.h"
31
32 static tree java_gimplify_block (tree);
33 static enum gimplify_status java_gimplify_modify_expr (tree *);
34 static enum gimplify_status java_gimplify_self_mod_expr (tree *, gimple_seq *,
35                                                          gimple_seq *);
36
37 static void dump_java_tree (enum tree_dump_index, tree);
38
39 /* Convert a Java tree to GENERIC.  */
40
41 void
42 java_genericize (tree fndecl)
43 {
44   walk_tree (&DECL_SAVED_TREE (fndecl), java_replace_references, NULL, NULL);
45   dump_java_tree (TDI_original, fndecl);
46 }
47
48 /* Gimplify a Java tree.  */
49
50 int
51 java_gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p)
52 {
53   enum tree_code code = TREE_CODE (*expr_p);
54
55   switch (code)
56     {
57     case BLOCK:
58       *expr_p = java_gimplify_block (*expr_p);
59       break;
60
61     case MODIFY_EXPR:
62       return java_gimplify_modify_expr (expr_p);
63
64     case POSTINCREMENT_EXPR:
65     case POSTDECREMENT_EXPR:
66     case PREINCREMENT_EXPR:
67     case PREDECREMENT_EXPR:
68       return java_gimplify_self_mod_expr (expr_p, pre_p, post_p);
69       
70     /* These should already be lowered before we get here.  */
71     case URSHIFT_EXPR:
72     case COMPARE_EXPR:
73     case COMPARE_L_EXPR:
74     case COMPARE_G_EXPR:
75       gcc_unreachable ();
76
77     default:
78       return GS_UNHANDLED;
79     }
80
81   return GS_OK;
82 }
83
84 static enum gimplify_status
85 java_gimplify_modify_expr (tree *modify_expr_p)
86 {
87   tree modify_expr = *modify_expr_p;
88   tree lhs = TREE_OPERAND (modify_expr, 0);
89   tree rhs = TREE_OPERAND (modify_expr, 1);
90   tree lhs_type = TREE_TYPE (lhs);
91
92   if (lhs_type != TREE_TYPE (rhs))
93     /* Fix up type mismatches to make legal GIMPLE.  These are
94        generated in several places, in particular null pointer
95        assignment and subclass assignment.  */
96     TREE_OPERAND (modify_expr, 1) = convert (lhs_type, rhs);
97
98   return GS_UNHANDLED;
99 }
100
101 /*  Special case handling for volatiles: we need to generate a barrier
102     between the reading and the writing.  */
103
104 static enum gimplify_status
105 java_gimplify_self_mod_expr (tree *expr_p, gimple_seq *pre_p ATTRIBUTE_UNUSED, 
106                              gimple_seq *post_p ATTRIBUTE_UNUSED)
107 {
108   tree lhs = TREE_OPERAND (*expr_p, 0);
109
110   if (TREE_CODE (lhs) == COMPONENT_REF
111       && TREE_THIS_VOLATILE (TREE_OPERAND (lhs, 1)))
112     TREE_THIS_VOLATILE (lhs) = 1;
113
114   return GS_UNHANDLED;
115 }
116
117     
118 /* Gimplify BLOCK into a BIND_EXPR.  */
119
120 static tree
121 java_gimplify_block (tree java_block)
122 {
123   tree decls = BLOCK_VARS (java_block);
124   tree body = BLOCK_EXPR_BODY (java_block);
125   gimple outer = gimple_current_bind_expr ();
126   tree block;
127
128   /* Don't bother with empty blocks.  */
129   if (! body)
130     return build_empty_stmt (input_location);
131
132   if (IS_EMPTY_STMT (body))
133     return body;
134
135   /* Make a proper block.  Java blocks are unsuitable for BIND_EXPR
136      because they use BLOCK_SUBBLOCKS for another purpose.  */
137   block = make_node (BLOCK);
138   BLOCK_VARS (block) = decls;
139
140   /* The TREE_USED flag on a block determines whether the debug output
141      routines generate info for the variables in that block.  */
142   TREE_USED (block) = 1;
143
144   if (outer != NULL)
145     {
146       tree b = gimple_bind_block (outer);
147       BLOCK_SUBBLOCKS (b) = chainon (BLOCK_SUBBLOCKS (b), block);
148     }
149   BLOCK_EXPR_BODY (java_block) = NULL_TREE;
150
151   return build3 (BIND_EXPR, TREE_TYPE (java_block), decls, body, block);
152 }
153
154 /* Dump a tree of some kind.  This is a convenience wrapper for the
155    dump_* functions in tree-dump.c.  */
156 static void
157 dump_java_tree (enum tree_dump_index phase, tree t)
158 {
159   FILE *stream;
160   int flags;
161
162   stream = dump_begin (phase, &flags);
163   flags |= TDF_SLIM;
164   if (stream)
165     {
166       dump_node (t, flags, stream);
167       dump_end (phase, stream);
168     }
169 }