1169689Skan/* Tree lowering pass. This pass gimplifies the tree representation built 2169689Skan by the C-based front ends. The structure of gimplified, or 3169689Skan language-independent, trees is dictated by the grammar described in this 4169689Skan file. 5169689Skan Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. 6169689Skan Lowering of expressions contributed by Sebastian Pop <s.pop@laposte.net> 7169689Skan Re-written to support lowering of whole function trees, documentation 8169689Skan and miscellaneous cleanups by Diego Novillo <dnovillo@redhat.com> 9169689Skan 10169689SkanThis file is part of GCC. 11169689Skan 12169689SkanGCC is free software; you can redistribute it and/or modify it under 13169689Skanthe terms of the GNU General Public License as published by the Free 14169689SkanSoftware Foundation; either version 2, or (at your option) any later 15169689Skanversion. 16169689Skan 17169689SkanGCC is distributed in the hope that it will be useful, but WITHOUT ANY 18169689SkanWARRANTY; without even the implied warranty of MERCHANTABILITY or 19169689SkanFITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 20169689Skanfor more details. 21169689Skan 22169689SkanYou should have received a copy of the GNU General Public License 23169689Skanalong with GCC; see the file COPYING. If not, write to the Free 24169689SkanSoftware Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 25169689Skan02110-1301, USA. */ 26169689Skan 27169689Skan#include "config.h" 28169689Skan#include "system.h" 29169689Skan#include "coretypes.h" 30169689Skan#include "tm.h" 31169689Skan#include "tree.h" 32169689Skan#include "varray.h" 33169689Skan#include "c-tree.h" 34169689Skan#include "c-common.h" 35169689Skan#include "tree-gimple.h" 36169689Skan#include "hard-reg-set.h" 37169689Skan#include "basic-block.h" 38169689Skan#include "tree-flow.h" 39169689Skan#include "tree-inline.h" 40169689Skan#include "diagnostic.h" 41169689Skan#include "langhooks.h" 42169689Skan#include "langhooks-def.h" 43169689Skan#include "flags.h" 44169689Skan#include "rtl.h" 45169689Skan#include "toplev.h" 46169689Skan#include "tree-dump.h" 47169689Skan#include "c-pretty-print.h" 48169689Skan#include "cgraph.h" 49169689Skan 50169689Skan 51169689Skan/* The gimplification pass converts the language-dependent trees 52169689Skan (ld-trees) emitted by the parser into language-independent trees 53169689Skan (li-trees) that are the target of SSA analysis and transformations. 54169689Skan 55169689Skan Language-independent trees are based on the SIMPLE intermediate 56169689Skan representation used in the McCAT compiler framework: 57169689Skan 58169689Skan "Designing the McCAT Compiler Based on a Family of Structured 59169689Skan Intermediate Representations," 60169689Skan L. Hendren, C. Donawa, M. Emami, G. Gao, Justiani, and B. Sridharan, 61169689Skan Proceedings of the 5th International Workshop on Languages and 62169689Skan Compilers for Parallel Computing, no. 757 in Lecture Notes in 63169689Skan Computer Science, New Haven, Connecticut, pp. 406-420, 64169689Skan Springer-Verlag, August 3-5, 1992. 65169689Skan 66169689Skan http://www-acaps.cs.mcgill.ca/info/McCAT/McCAT.html 67169689Skan 68169689Skan Basically, we walk down gimplifying the nodes that we encounter. As we 69169689Skan walk back up, we check that they fit our constraints, and copy them 70169689Skan into temporaries if not. */ 71169689Skan 72169689Skan/* Gimplification of statement trees. */ 73169689Skan 74169689Skan/* Convert the tree representation of FNDECL from C frontend trees to 75169689Skan GENERIC. */ 76169689Skan 77169689Skanvoid 78169689Skanc_genericize (tree fndecl) 79169689Skan{ 80169689Skan FILE *dump_orig; 81169689Skan int local_dump_flags; 82169689Skan struct cgraph_node *cgn; 83169689Skan 84169689Skan /* Dump the C-specific tree IR. */ 85169689Skan dump_orig = dump_begin (TDI_original, &local_dump_flags); 86169689Skan if (dump_orig) 87169689Skan { 88169689Skan fprintf (dump_orig, "\n;; Function %s", 89169689Skan lang_hooks.decl_printable_name (fndecl, 2)); 90169689Skan fprintf (dump_orig, " (%s)\n", 91169689Skan IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl))); 92169689Skan fprintf (dump_orig, ";; enabled by -%s\n", dump_flag_name (TDI_original)); 93169689Skan fprintf (dump_orig, "\n"); 94169689Skan 95169689Skan if (local_dump_flags & TDF_RAW) 96169689Skan dump_node (DECL_SAVED_TREE (fndecl), 97169689Skan TDF_SLIM | local_dump_flags, dump_orig); 98169689Skan else 99169689Skan print_c_tree (dump_orig, DECL_SAVED_TREE (fndecl)); 100169689Skan fprintf (dump_orig, "\n"); 101169689Skan 102169689Skan dump_end (TDI_original, dump_orig); 103169689Skan } 104169689Skan 105169689Skan /* Go ahead and gimplify for now. */ 106169689Skan gimplify_function_tree (fndecl); 107169689Skan 108169689Skan /* Dump the genericized tree IR. */ 109169689Skan dump_function (TDI_generic, fndecl); 110169689Skan 111169689Skan /* Genericize all nested functions now. We do things in this order so 112169689Skan that items like VLA sizes are expanded properly in the context of 113169689Skan the correct function. */ 114169689Skan cgn = cgraph_node (fndecl); 115169689Skan for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested) 116169689Skan c_genericize (cgn->decl); 117169689Skan} 118169689Skan 119169689Skanstatic void 120169689Skanadd_block_to_enclosing (tree block) 121169689Skan{ 122169689Skan tree enclosing; 123169689Skan 124169689Skan for (enclosing = gimple_current_bind_expr (); 125169689Skan enclosing; enclosing = TREE_CHAIN (enclosing)) 126169689Skan if (BIND_EXPR_BLOCK (enclosing)) 127169689Skan break; 128169689Skan 129169689Skan enclosing = BIND_EXPR_BLOCK (enclosing); 130169689Skan BLOCK_SUBBLOCKS (enclosing) = chainon (BLOCK_SUBBLOCKS (enclosing), block); 131169689Skan} 132169689Skan 133169689Skan/* Genericize a scope by creating a new BIND_EXPR. 134169689Skan BLOCK is either a BLOCK representing the scope or a chain of _DECLs. 135169689Skan In the latter case, we need to create a new BLOCK and add it to the 136169689Skan BLOCK_SUBBLOCKS of the enclosing block. 137169689Skan BODY is a chain of C _STMT nodes for the contents of the scope, to be 138169689Skan genericized. */ 139169689Skan 140169689Skantree 141169689Skanc_build_bind_expr (tree block, tree body) 142169689Skan{ 143169689Skan tree decls, bind; 144169689Skan 145169689Skan if (block == NULL_TREE) 146169689Skan decls = NULL_TREE; 147169689Skan else if (TREE_CODE (block) == BLOCK) 148169689Skan decls = BLOCK_VARS (block); 149169689Skan else 150169689Skan { 151169689Skan decls = block; 152169689Skan if (DECL_ARTIFICIAL (decls)) 153169689Skan block = NULL_TREE; 154169689Skan else 155169689Skan { 156169689Skan block = make_node (BLOCK); 157169689Skan BLOCK_VARS (block) = decls; 158169689Skan add_block_to_enclosing (block); 159169689Skan } 160169689Skan } 161169689Skan 162169689Skan if (!body) 163169689Skan body = build_empty_stmt (); 164169689Skan if (decls || block) 165169689Skan { 166169689Skan bind = build3 (BIND_EXPR, void_type_node, decls, body, block); 167169689Skan TREE_SIDE_EFFECTS (bind) = 1; 168169689Skan } 169169689Skan else 170169689Skan bind = body; 171169689Skan 172169689Skan return bind; 173169689Skan} 174169689Skan 175169689Skan/* Gimplification of expression trees. */ 176169689Skan 177169689Skan/* Gimplify a C99 compound literal expression. This just means adding 178169689Skan the DECL_EXPR before the current statement and using its anonymous 179169689Skan decl instead. */ 180169689Skan 181169689Skanstatic enum gimplify_status 182169689Skangimplify_compound_literal_expr (tree *expr_p, tree *pre_p) 183169689Skan{ 184169689Skan tree decl_s = COMPOUND_LITERAL_EXPR_DECL_STMT (*expr_p); 185169689Skan tree decl = DECL_EXPR_DECL (decl_s); 186169689Skan 187169689Skan /* This decl isn't mentioned in the enclosing block, so add it to the 188169689Skan list of temps. FIXME it seems a bit of a kludge to say that 189169689Skan anonymous artificial vars aren't pushed, but everything else is. */ 190169689Skan if (DECL_NAME (decl) == NULL_TREE && !DECL_SEEN_IN_BIND_EXPR_P (decl)) 191169689Skan gimple_add_tmp_var (decl); 192169689Skan 193169689Skan gimplify_and_add (decl_s, pre_p); 194169689Skan *expr_p = decl; 195169689Skan return GS_OK; 196169689Skan} 197169689Skan 198169689Skan/* Do C-specific gimplification. Args are as for gimplify_expr. */ 199169689Skan 200169689Skanint 201169689Skanc_gimplify_expr (tree *expr_p, tree *pre_p, tree *post_p ATTRIBUTE_UNUSED) 202169689Skan{ 203169689Skan enum tree_code code = TREE_CODE (*expr_p); 204169689Skan 205169689Skan switch (code) 206169689Skan { 207169689Skan case DECL_EXPR: 208169689Skan /* This is handled mostly by gimplify.c, but we have to deal with 209169689Skan not warning about int x = x; as it is a GCC extension to turn off 210169689Skan this warning but only if warn_init_self is zero. */ 211169689Skan if (TREE_CODE (DECL_EXPR_DECL (*expr_p)) == VAR_DECL 212169689Skan && !DECL_EXTERNAL (DECL_EXPR_DECL (*expr_p)) 213169689Skan && !TREE_STATIC (DECL_EXPR_DECL (*expr_p)) 214169689Skan && (DECL_INITIAL (DECL_EXPR_DECL (*expr_p)) 215169689Skan == DECL_EXPR_DECL (*expr_p)) 216169689Skan && !warn_init_self) 217169689Skan TREE_NO_WARNING (DECL_EXPR_DECL (*expr_p)) = 1; 218169689Skan return GS_UNHANDLED; 219169689Skan 220169689Skan case COMPOUND_LITERAL_EXPR: 221169689Skan return gimplify_compound_literal_expr (expr_p, pre_p); 222169689Skan 223169689Skan default: 224169689Skan return GS_UNHANDLED; 225169689Skan } 226169689Skan} 227