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