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