OSDN Git Service

./
[pf3gnuchains/gcc-fork.git] / gcc / c-gimplify.c
1 /* Tree lowering pass.  This pass gimplifies the tree representation built
2    by the C-based front ends.  The structure of gimplified, or
3    language-independent, trees is dictated by the grammar described in this
4    file.
5    Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
6    Lowering of expressions contributed by Sebastian Pop <s.pop@laposte.net>
7    Re-written to support lowering of whole function trees, documentation
8    and miscellaneous cleanups by Diego Novillo <dnovillo@redhat.com>
9
10 This file is part of GCC.
11
12 GCC is free software; you can redistribute it and/or modify it under
13 the terms of the GNU General Public License as published by the Free
14 Software Foundation; either version 2, or (at your option) any later
15 version.
16
17 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
18 WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20 for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING.  If not, write to the Free
24 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 02111-1307, USA.  */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "errors.h"
33 #include "varray.h"
34 #include "c-tree.h"
35 #include "c-common.h"
36 #include "tree-gimple.h"
37 #include "hard-reg-set.h"
38 #include "basic-block.h"
39 #include "tree-flow.h"
40 #include "tree-inline.h"
41 #include "diagnostic.h"
42 #include "langhooks.h"
43 #include "langhooks-def.h"
44 #include "flags.h"
45 #include "rtl.h"
46 #include "toplev.h"
47 #include "tree-dump.h"
48 #include "c-pretty-print.h"
49 #include "cgraph.h"
50
51
52 /*  The gimplification pass converts the language-dependent trees
53     (ld-trees) emitted by the parser into language-independent trees
54     (li-trees) that are the target of SSA analysis and transformations.
55
56     Language-independent trees are based on the SIMPLE intermediate
57     representation used in the McCAT compiler framework:
58
59     "Designing the McCAT Compiler Based on a Family of Structured
60     Intermediate Representations,"
61     L. Hendren, C. Donawa, M. Emami, G. Gao, Justiani, and B. Sridharan,
62     Proceedings of the 5th International Workshop on Languages and
63     Compilers for Parallel Computing, no. 757 in Lecture Notes in
64     Computer Science, New Haven, Connecticut, pp. 406-420,
65     Springer-Verlag, August 3-5, 1992.
66
67     http://www-acaps.cs.mcgill.ca/info/McCAT/McCAT.html
68
69     Basically, we walk down gimplifying the nodes that we encounter.  As we
70     walk back up, we check that they fit our constraints, and copy them
71     into temporaries if not.  */
72
73 /* Gimplification of statement trees.  */
74
75 /* Convert the tree representation of FNDECL from C frontend trees to
76    GENERIC.  */
77
78 void
79 c_genericize (tree fndecl)
80 {
81   FILE *dump_file;
82   int local_dump_flags;
83   struct cgraph_node *cgn;
84
85   /* Dump the C-specific tree IR.  */
86   dump_file = dump_begin (TDI_original, &local_dump_flags);
87   if (dump_file)
88     {
89       fprintf (dump_file, "\n;; Function %s",
90                lang_hooks.decl_printable_name (fndecl, 2));
91       fprintf (dump_file, " (%s)\n",
92                IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)));
93       fprintf (dump_file, ";; enabled by -%s\n", dump_flag_name (TDI_original));
94       fprintf (dump_file, "\n");
95
96       if (local_dump_flags & TDF_RAW)
97         dump_node (DECL_SAVED_TREE (fndecl),
98                    TDF_SLIM | local_dump_flags, dump_file);
99       else
100         print_c_tree (dump_file, DECL_SAVED_TREE (fndecl));
101       fprintf (dump_file, "\n");
102
103       dump_end (TDI_original, dump_file);
104     }
105
106   /* Go ahead and gimplify for now.  */
107   gimplify_function_tree (fndecl);
108
109   /* Dump the genericized tree IR.  */
110   dump_function (TDI_generic, fndecl);
111
112   /* Genericize all nested functions now.  We do things in this order so
113      that items like VLA sizes are expanded properly in the context of
114      the correct function.  */
115   cgn = cgraph_node (fndecl);
116   for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
117     c_genericize (cgn->decl);
118 }
119
120 static void
121 add_block_to_enclosing (tree block)
122 {
123   tree enclosing;
124
125   for (enclosing = gimple_current_bind_expr ();
126        enclosing; enclosing = TREE_CHAIN (enclosing))
127     if (BIND_EXPR_BLOCK (enclosing))
128       break;
129
130   enclosing = BIND_EXPR_BLOCK (enclosing);
131   BLOCK_SUBBLOCKS (enclosing) = chainon (BLOCK_SUBBLOCKS (enclosing), block);
132 }
133
134 /* Genericize a scope by creating a new BIND_EXPR.
135    BLOCK is either a BLOCK representing the scope or a chain of _DECLs.
136      In the latter case, we need to create a new BLOCK and add it to the
137      BLOCK_SUBBLOCKS of the enclosing block.
138    BODY is a chain of C _STMT nodes for the contents of the scope, to be
139      genericized.  */
140
141 tree
142 c_build_bind_expr (tree block, tree body)
143 {
144   tree decls, bind;
145
146   if (block == NULL_TREE)
147     decls = NULL_TREE;
148   else if (TREE_CODE (block) == BLOCK)
149     decls = BLOCK_VARS (block);
150   else
151     {
152       decls = block;
153       if (DECL_ARTIFICIAL (decls))
154         block = NULL_TREE;
155       else
156         {
157           block = make_node (BLOCK);
158           BLOCK_VARS (block) = decls;
159           add_block_to_enclosing (block);
160         }
161     }
162
163   if (!body)
164     body = build_empty_stmt ();
165   if (decls || block)
166     {
167       bind = build3 (BIND_EXPR, void_type_node, decls, body, block);
168       TREE_SIDE_EFFECTS (bind) = 1;
169     }
170   else
171     bind = body;
172
173   return bind;
174 }
175
176 /*  Gimplify an EXPR_STMT node.
177
178     STMT is the statement node.
179
180     PRE_P points to the list where side effects that must happen before
181         STMT should be stored.
182
183     POST_P points to the list where side effects that must happen after
184         STMT should be stored.  */
185
186 static enum gimplify_status
187 gimplify_expr_stmt (tree *stmt_p)
188 {
189   tree stmt = EXPR_STMT_EXPR (*stmt_p);
190
191   if (stmt == error_mark_node)
192     stmt = NULL;
193
194   /* Gimplification of a statement expression will nullify the
195      statement if all its side effects are moved to *PRE_P and *POST_P.
196
197      In this case we will not want to emit the gimplified statement.
198      However, we may still want to emit a warning, so we do that before
199      gimplification.  */
200   if (stmt && (extra_warnings || warn_unused_value))
201     {
202       if (!TREE_SIDE_EFFECTS (stmt))
203         {
204           if (!IS_EMPTY_STMT (stmt)
205               && !VOID_TYPE_P (TREE_TYPE (stmt))
206               && !TREE_NO_WARNING (stmt))
207             warning ("statement with no effect");
208         }
209       else if (warn_unused_value)
210         warn_if_unused_value (stmt, input_location);
211     }
212
213   if (stmt == NULL_TREE)
214     stmt = alloc_stmt_list ();
215
216   *stmt_p = stmt;
217
218   return GS_OK;
219 }
220
221 /* Gimplification of expression trees.  */
222
223 /* Gimplify a C99 compound literal expression.  This just means adding the
224    DECL_EXPR before the current EXPR_STMT and using its anonymous decl
225    instead.  */
226
227 static enum gimplify_status
228 gimplify_compound_literal_expr (tree *expr_p, tree *pre_p)
229 {
230   tree decl_s = COMPOUND_LITERAL_EXPR_DECL_STMT (*expr_p);
231   tree decl = DECL_EXPR_DECL (decl_s);
232
233   /* This decl isn't mentioned in the enclosing block, so add it to the
234      list of temps.  FIXME it seems a bit of a kludge to say that
235      anonymous artificial vars aren't pushed, but everything else is.  */
236   if (DECL_NAME (decl) == NULL_TREE)
237     gimple_add_tmp_var (decl);
238
239   gimplify_and_add (decl_s, pre_p);
240   *expr_p = decl;
241   return GS_OK;
242 }
243
244 /* Do C-specific gimplification.  Args are as for gimplify_expr.  */
245
246 int
247 c_gimplify_expr (tree *expr_p, tree *pre_p, tree *post_p ATTRIBUTE_UNUSED)
248 {
249   enum tree_code code = TREE_CODE (*expr_p);
250
251   switch (code)
252     {
253     case DECL_EXPR:
254       /* This is handled mostly by gimplify.c, but we have to deal with
255          not warning about int x = x; as it is a GCC extension to turn off
256          this warning but only if warn_init_self is zero.  */
257       if (TREE_CODE (DECL_EXPR_DECL (*expr_p)) == VAR_DECL
258           && !DECL_EXTERNAL (DECL_EXPR_DECL (*expr_p))
259           && !TREE_STATIC (DECL_EXPR_DECL (*expr_p))
260           && (DECL_INITIAL (DECL_EXPR_DECL (*expr_p))
261               == DECL_EXPR_DECL (*expr_p))
262           && !warn_init_self)
263         TREE_NO_WARNING (DECL_EXPR_DECL (*expr_p)) = 1;
264       return GS_UNHANDLED;
265       
266     case COMPOUND_LITERAL_EXPR:
267       return gimplify_compound_literal_expr (expr_p, pre_p);
268
269     case EXPR_STMT:
270       return gimplify_expr_stmt (expr_p);
271
272     default:
273       return GS_UNHANDLED;
274     }
275 }