OSDN Git Service

2010-06-22 Ed Schonberg <schonberg@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / c-family / 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, 2007, 2008, 2009, 2010
6    Free Software Foundation, Inc.
7    Lowering of expressions contributed by Sebastian Pop <s.pop@laposte.net>
8    Re-written to support lowering of whole function trees, documentation
9    and miscellaneous cleanups by Diego Novillo <dnovillo@redhat.com>
10
11 This file is part of GCC.
12
13 GCC is free software; you can redistribute it and/or modify it under
14 the terms of the GNU General Public License as published by the Free
15 Software Foundation; either version 3, or (at your option) any later
16 version.
17
18 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
21 for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with GCC; see the file COPYING3.  If not see
25 <http://www.gnu.org/licenses/>.  */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "c-common.h"
33 #include "gimple.h"
34 #include "basic-block.h"
35 #include "tree-flow.h"
36 #include "tree-inline.h"
37 #include "diagnostic-core.h"
38 #include "langhooks.h"
39 #include "langhooks-def.h"
40 #include "flags.h"
41 #include "toplev.h"
42 #include "tree-dump.h"
43 #include "c-pretty-print.h"
44 #include "cgraph.h"
45
46
47 /*  The gimplification pass converts the language-dependent trees
48     (ld-trees) emitted by the parser into language-independent trees
49     (li-trees) that are the target of SSA analysis and transformations.
50
51     Language-independent trees are based on the SIMPLE intermediate
52     representation used in the McCAT compiler framework:
53
54     "Designing the McCAT Compiler Based on a Family of Structured
55     Intermediate Representations,"
56     L. Hendren, C. Donawa, M. Emami, G. Gao, Justiani, and B. Sridharan,
57     Proceedings of the 5th International Workshop on Languages and
58     Compilers for Parallel Computing, no. 757 in Lecture Notes in
59     Computer Science, New Haven, Connecticut, pp. 406-420,
60     Springer-Verlag, August 3-5, 1992.
61
62     http://www-acaps.cs.mcgill.ca/info/McCAT/McCAT.html
63
64     Basically, we walk down gimplifying the nodes that we encounter.  As we
65     walk back up, we check that they fit our constraints, and copy them
66     into temporaries if not.  */
67
68 /* Gimplification of statement trees.  */
69
70 /* Convert the tree representation of FNDECL from C frontend trees to
71    GENERIC.  */
72
73 void
74 c_genericize (tree fndecl)
75 {
76   FILE *dump_orig;
77   int local_dump_flags;
78   struct cgraph_node *cgn;
79
80   /* Dump the C-specific tree IR.  */
81   dump_orig = dump_begin (TDI_original, &local_dump_flags);
82   if (dump_orig)
83     {
84       fprintf (dump_orig, "\n;; Function %s",
85                lang_hooks.decl_printable_name (fndecl, 2));
86       fprintf (dump_orig, " (%s)\n",
87                (!DECL_ASSEMBLER_NAME_SET_P (fndecl) ? "null"
88                 : IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl))));
89       fprintf (dump_orig, ";; enabled by -%s\n", dump_flag_name (TDI_original));
90       fprintf (dump_orig, "\n");
91
92       if (local_dump_flags & TDF_RAW)
93         dump_node (DECL_SAVED_TREE (fndecl),
94                    TDF_SLIM | local_dump_flags, dump_orig);
95       else
96         print_c_tree (dump_orig, DECL_SAVED_TREE (fndecl));
97       fprintf (dump_orig, "\n");
98
99       dump_end (TDI_original, dump_orig);
100     }
101
102   /* Dump all nested functions now.  */
103   cgn = cgraph_node (fndecl);
104   for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
105     c_genericize (cgn->decl);
106 }
107
108 static void
109 add_block_to_enclosing (tree block)
110 {
111   unsigned i;
112   tree enclosing;
113   gimple bind;
114   VEC(gimple, heap) *stack = gimple_bind_expr_stack ();
115
116   for (i = 0; VEC_iterate (gimple, stack, i, bind); i++)
117     if (gimple_bind_block (bind))
118       break;
119
120   enclosing = gimple_bind_block (bind);
121   BLOCK_SUBBLOCKS (enclosing) = chainon (BLOCK_SUBBLOCKS (enclosing), block);
122 }
123
124 /* Genericize a scope by creating a new BIND_EXPR.
125    BLOCK is either a BLOCK representing the scope or a chain of _DECLs.
126      In the latter case, we need to create a new BLOCK and add it to the
127      BLOCK_SUBBLOCKS of the enclosing block.
128    BODY is a chain of C _STMT nodes for the contents of the scope, to be
129      genericized.  */
130
131 tree
132 c_build_bind_expr (location_t loc, tree block, tree body)
133 {
134   tree decls, bind;
135
136   if (block == NULL_TREE)
137     decls = NULL_TREE;
138   else if (TREE_CODE (block) == BLOCK)
139     decls = BLOCK_VARS (block);
140   else
141     {
142       decls = block;
143       if (DECL_ARTIFICIAL (decls))
144         block = NULL_TREE;
145       else
146         {
147           block = make_node (BLOCK);
148           BLOCK_VARS (block) = decls;
149           add_block_to_enclosing (block);
150         }
151     }
152
153   if (!body)
154     body = build_empty_stmt (loc);
155   if (decls || block)
156     {
157       bind = build3 (BIND_EXPR, void_type_node, decls, body, block);
158       TREE_SIDE_EFFECTS (bind) = 1;
159       SET_EXPR_LOCATION (bind, loc);
160     }
161   else
162     bind = body;
163
164   return bind;
165 }
166
167 /* Gimplification of expression trees.  */
168
169 /* Do C-specific gimplification on *EXPR_P.  PRE_P and POST_P are as in
170    gimplify_expr.  */
171
172 int
173 c_gimplify_expr (tree *expr_p, gimple_seq *pre_p ATTRIBUTE_UNUSED,
174                  gimple_seq *post_p ATTRIBUTE_UNUSED)
175 {
176   enum tree_code code = TREE_CODE (*expr_p);
177
178   /* This is handled mostly by gimplify.c, but we have to deal with
179      not warning about int x = x; as it is a GCC extension to turn off
180      this warning but only if warn_init_self is zero.  */
181   if (code == DECL_EXPR
182       && TREE_CODE (DECL_EXPR_DECL (*expr_p)) == VAR_DECL
183       && !DECL_EXTERNAL (DECL_EXPR_DECL (*expr_p))
184       && !TREE_STATIC (DECL_EXPR_DECL (*expr_p))
185       && (DECL_INITIAL (DECL_EXPR_DECL (*expr_p)) == DECL_EXPR_DECL (*expr_p))
186       && !warn_init_self)
187     TREE_NO_WARNING (DECL_EXPR_DECL (*expr_p)) = 1;
188
189   return GS_UNHANDLED;
190 }