OSDN Git Service

2005-04-27 Paolo Bonzini <bonzini@gnu.org>
[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 /* Gimplification of expression trees.  */
177
178 /* Gimplify a C99 compound literal expression.  This just means adding
179    the DECL_EXPR before the current statement and using its anonymous
180    decl instead.  */
181
182 static enum gimplify_status
183 gimplify_compound_literal_expr (tree *expr_p, tree *pre_p)
184 {
185   tree decl_s = COMPOUND_LITERAL_EXPR_DECL_STMT (*expr_p);
186   tree decl = DECL_EXPR_DECL (decl_s);
187
188   /* This decl isn't mentioned in the enclosing block, so add it to the
189      list of temps.  FIXME it seems a bit of a kludge to say that
190      anonymous artificial vars aren't pushed, but everything else is.  */
191   if (DECL_NAME (decl) == NULL_TREE)
192     gimple_add_tmp_var (decl);
193
194   gimplify_and_add (decl_s, pre_p);
195   *expr_p = decl;
196   return GS_OK;
197 }
198
199 /* Do C-specific gimplification.  Args are as for gimplify_expr.  */
200
201 int
202 c_gimplify_expr (tree *expr_p, tree *pre_p, tree *post_p ATTRIBUTE_UNUSED)
203 {
204   enum tree_code code = TREE_CODE (*expr_p);
205
206   switch (code)
207     {
208     case DECL_EXPR:
209       /* This is handled mostly by gimplify.c, but we have to deal with
210          not warning about int x = x; as it is a GCC extension to turn off
211          this warning but only if warn_init_self is zero.  */
212       if (TREE_CODE (DECL_EXPR_DECL (*expr_p)) == VAR_DECL
213           && !DECL_EXTERNAL (DECL_EXPR_DECL (*expr_p))
214           && !TREE_STATIC (DECL_EXPR_DECL (*expr_p))
215           && (DECL_INITIAL (DECL_EXPR_DECL (*expr_p))
216               == DECL_EXPR_DECL (*expr_p))
217           && !warn_init_self)
218         TREE_NO_WARNING (DECL_EXPR_DECL (*expr_p)) = 1;
219       return GS_UNHANDLED;
220       
221     case COMPOUND_LITERAL_EXPR:
222       return gimplify_compound_literal_expr (expr_p, pre_p);
223
224     default:
225       return GS_UNHANDLED;
226     }
227 }