1 |
38 |
julius |
/* 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 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 3, 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 COPYING3. If not see
|
24 |
|
|
<http://www.gnu.org/licenses/>. */
|
25 |
|
|
|
26 |
|
|
#include "config.h"
|
27 |
|
|
#include "system.h"
|
28 |
|
|
#include "coretypes.h"
|
29 |
|
|
#include "tm.h"
|
30 |
|
|
#include "tree.h"
|
31 |
|
|
#include "varray.h"
|
32 |
|
|
#include "c-tree.h"
|
33 |
|
|
#include "c-common.h"
|
34 |
|
|
#include "tree-gimple.h"
|
35 |
|
|
#include "hard-reg-set.h"
|
36 |
|
|
#include "basic-block.h"
|
37 |
|
|
#include "tree-flow.h"
|
38 |
|
|
#include "tree-inline.h"
|
39 |
|
|
#include "diagnostic.h"
|
40 |
|
|
#include "langhooks.h"
|
41 |
|
|
#include "langhooks-def.h"
|
42 |
|
|
#include "flags.h"
|
43 |
|
|
#include "rtl.h"
|
44 |
|
|
#include "toplev.h"
|
45 |
|
|
#include "tree-dump.h"
|
46 |
|
|
#include "c-pretty-print.h"
|
47 |
|
|
#include "cgraph.h"
|
48 |
|
|
|
49 |
|
|
|
50 |
|
|
/* The gimplification pass converts the language-dependent trees
|
51 |
|
|
(ld-trees) emitted by the parser into language-independent trees
|
52 |
|
|
(li-trees) that are the target of SSA analysis and transformations.
|
53 |
|
|
|
54 |
|
|
Language-independent trees are based on the SIMPLE intermediate
|
55 |
|
|
representation used in the McCAT compiler framework:
|
56 |
|
|
|
57 |
|
|
"Designing the McCAT Compiler Based on a Family of Structured
|
58 |
|
|
Intermediate Representations,"
|
59 |
|
|
L. Hendren, C. Donawa, M. Emami, G. Gao, Justiani, and B. Sridharan,
|
60 |
|
|
Proceedings of the 5th International Workshop on Languages and
|
61 |
|
|
Compilers for Parallel Computing, no. 757 in Lecture Notes in
|
62 |
|
|
Computer Science, New Haven, Connecticut, pp. 406-420,
|
63 |
|
|
Springer-Verlag, August 3-5, 1992.
|
64 |
|
|
|
65 |
|
|
http://www-acaps.cs.mcgill.ca/info/McCAT/McCAT.html
|
66 |
|
|
|
67 |
|
|
Basically, we walk down gimplifying the nodes that we encounter. As we
|
68 |
|
|
walk back up, we check that they fit our constraints, and copy them
|
69 |
|
|
into temporaries if not. */
|
70 |
|
|
|
71 |
|
|
/* Gimplification of statement trees. */
|
72 |
|
|
|
73 |
|
|
/* Convert the tree representation of FNDECL from C frontend trees to
|
74 |
|
|
GENERIC. */
|
75 |
|
|
|
76 |
|
|
void
|
77 |
|
|
c_genericize (tree fndecl)
|
78 |
|
|
{
|
79 |
|
|
FILE *dump_orig;
|
80 |
|
|
int local_dump_flags;
|
81 |
|
|
struct cgraph_node *cgn;
|
82 |
|
|
|
83 |
|
|
/* Dump the C-specific tree IR. */
|
84 |
|
|
dump_orig = dump_begin (TDI_original, &local_dump_flags);
|
85 |
|
|
if (dump_orig)
|
86 |
|
|
{
|
87 |
|
|
fprintf (dump_orig, "\n;; Function %s",
|
88 |
|
|
lang_hooks.decl_printable_name (fndecl, 2));
|
89 |
|
|
fprintf (dump_orig, " (%s)\n",
|
90 |
|
|
IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)));
|
91 |
|
|
fprintf (dump_orig, ";; enabled by -%s\n", dump_flag_name (TDI_original));
|
92 |
|
|
fprintf (dump_orig, "\n");
|
93 |
|
|
|
94 |
|
|
if (local_dump_flags & TDF_RAW)
|
95 |
|
|
dump_node (DECL_SAVED_TREE (fndecl),
|
96 |
|
|
TDF_SLIM | local_dump_flags, dump_orig);
|
97 |
|
|
else
|
98 |
|
|
print_c_tree (dump_orig, DECL_SAVED_TREE (fndecl));
|
99 |
|
|
fprintf (dump_orig, "\n");
|
100 |
|
|
|
101 |
|
|
dump_end (TDI_original, dump_orig);
|
102 |
|
|
}
|
103 |
|
|
|
104 |
|
|
/* Go ahead and gimplify for now. */
|
105 |
|
|
gimplify_function_tree (fndecl);
|
106 |
|
|
|
107 |
|
|
/* Dump the genericized tree IR. */
|
108 |
|
|
dump_function (TDI_generic, fndecl);
|
109 |
|
|
|
110 |
|
|
/* Genericize all nested functions now. We do things in this order so
|
111 |
|
|
that items like VLA sizes are expanded properly in the context of
|
112 |
|
|
the correct function. */
|
113 |
|
|
cgn = cgraph_node (fndecl);
|
114 |
|
|
for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
|
115 |
|
|
c_genericize (cgn->decl);
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
static void
|
119 |
|
|
add_block_to_enclosing (tree block)
|
120 |
|
|
{
|
121 |
|
|
tree enclosing;
|
122 |
|
|
|
123 |
|
|
for (enclosing = gimple_current_bind_expr ();
|
124 |
|
|
enclosing; enclosing = TREE_CHAIN (enclosing))
|
125 |
|
|
if (BIND_EXPR_BLOCK (enclosing))
|
126 |
|
|
break;
|
127 |
|
|
|
128 |
|
|
enclosing = BIND_EXPR_BLOCK (enclosing);
|
129 |
|
|
BLOCK_SUBBLOCKS (enclosing) = chainon (BLOCK_SUBBLOCKS (enclosing), block);
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
/* Genericize a scope by creating a new BIND_EXPR.
|
133 |
|
|
BLOCK is either a BLOCK representing the scope or a chain of _DECLs.
|
134 |
|
|
In the latter case, we need to create a new BLOCK and add it to the
|
135 |
|
|
BLOCK_SUBBLOCKS of the enclosing block.
|
136 |
|
|
BODY is a chain of C _STMT nodes for the contents of the scope, to be
|
137 |
|
|
genericized. */
|
138 |
|
|
|
139 |
|
|
tree
|
140 |
|
|
c_build_bind_expr (tree block, tree body)
|
141 |
|
|
{
|
142 |
|
|
tree decls, bind;
|
143 |
|
|
|
144 |
|
|
if (block == NULL_TREE)
|
145 |
|
|
decls = NULL_TREE;
|
146 |
|
|
else if (TREE_CODE (block) == BLOCK)
|
147 |
|
|
decls = BLOCK_VARS (block);
|
148 |
|
|
else
|
149 |
|
|
{
|
150 |
|
|
decls = block;
|
151 |
|
|
if (DECL_ARTIFICIAL (decls))
|
152 |
|
|
block = NULL_TREE;
|
153 |
|
|
else
|
154 |
|
|
{
|
155 |
|
|
block = make_node (BLOCK);
|
156 |
|
|
BLOCK_VARS (block) = decls;
|
157 |
|
|
add_block_to_enclosing (block);
|
158 |
|
|
}
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
if (!body)
|
162 |
|
|
body = build_empty_stmt ();
|
163 |
|
|
if (decls || block)
|
164 |
|
|
{
|
165 |
|
|
bind = build3 (BIND_EXPR, void_type_node, decls, body, block);
|
166 |
|
|
TREE_SIDE_EFFECTS (bind) = 1;
|
167 |
|
|
}
|
168 |
|
|
else
|
169 |
|
|
bind = body;
|
170 |
|
|
|
171 |
|
|
return bind;
|
172 |
|
|
}
|
173 |
|
|
|
174 |
|
|
/* Gimplification of expression trees. */
|
175 |
|
|
|
176 |
|
|
/* Gimplify a C99 compound literal expression. This just means adding
|
177 |
|
|
the DECL_EXPR before the current statement and using its anonymous
|
178 |
|
|
decl instead. */
|
179 |
|
|
|
180 |
|
|
static enum gimplify_status
|
181 |
|
|
gimplify_compound_literal_expr (tree *expr_p, tree *pre_p)
|
182 |
|
|
{
|
183 |
|
|
tree decl_s = COMPOUND_LITERAL_EXPR_DECL_STMT (*expr_p);
|
184 |
|
|
tree decl = DECL_EXPR_DECL (decl_s);
|
185 |
|
|
|
186 |
|
|
/* This decl isn't mentioned in the enclosing block, so add it to the
|
187 |
|
|
list of temps. FIXME it seems a bit of a kludge to say that
|
188 |
|
|
anonymous artificial vars aren't pushed, but everything else is. */
|
189 |
|
|
if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl))
|
190 |
|
|
gimple_add_tmp_var (decl);
|
191 |
|
|
|
192 |
|
|
gimplify_and_add (decl_s, pre_p);
|
193 |
|
|
*expr_p = decl;
|
194 |
|
|
return GS_OK;
|
195 |
|
|
}
|
196 |
|
|
|
197 |
|
|
/* Do C-specific gimplification. Args are as for gimplify_expr. */
|
198 |
|
|
|
199 |
|
|
int
|
200 |
|
|
c_gimplify_expr (tree *expr_p, tree *pre_p, tree *post_p ATTRIBUTE_UNUSED)
|
201 |
|
|
{
|
202 |
|
|
enum tree_code code = TREE_CODE (*expr_p);
|
203 |
|
|
|
204 |
|
|
switch (code)
|
205 |
|
|
{
|
206 |
|
|
case DECL_EXPR:
|
207 |
|
|
/* This is handled mostly by gimplify.c, but we have to deal with
|
208 |
|
|
not warning about int x = x; as it is a GCC extension to turn off
|
209 |
|
|
this warning but only if warn_init_self is zero. */
|
210 |
|
|
if (TREE_CODE (DECL_EXPR_DECL (*expr_p)) == VAR_DECL
|
211 |
|
|
&& !DECL_EXTERNAL (DECL_EXPR_DECL (*expr_p))
|
212 |
|
|
&& !TREE_STATIC (DECL_EXPR_DECL (*expr_p))
|
213 |
|
|
&& (DECL_INITIAL (DECL_EXPR_DECL (*expr_p))
|
214 |
|
|
== DECL_EXPR_DECL (*expr_p))
|
215 |
|
|
&& !warn_init_self)
|
216 |
|
|
TREE_NO_WARNING (DECL_EXPR_DECL (*expr_p)) = 1;
|
217 |
|
|
return GS_UNHANDLED;
|
218 |
|
|
|
219 |
|
|
case COMPOUND_LITERAL_EXPR:
|
220 |
|
|
return gimplify_compound_literal_expr (expr_p, pre_p);
|
221 |
|
|
|
222 |
|
|
default:
|
223 |
|
|
return GS_UNHANDLED;
|
224 |
|
|
}
|
225 |
|
|
}
|