OSDN Git Service

PR tree-optimization/23434
[pf3gnuchains/gcc-fork.git] / gcc / tree-nested.c
1 /* Nested function decomposition for trees.
2    Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3
4    This file is part of GCC.
5
6    GCC is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    GCC is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with GCC; see the file COPYING.  If not, write to
18    the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19    Boston, MA 02110-1301, USA.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "function.h"
29 #include "tree-dump.h"
30 #include "tree-inline.h"
31 #include "tree-gimple.h"
32 #include "tree-iterator.h"
33 #include "tree-flow.h"
34 #include "cgraph.h"
35 #include "expr.h"
36 #include "langhooks.h"
37 #include "ggc.h"
38
39
40 /* The object of this pass is to lower the representation of a set of nested
41    functions in order to expose all of the gory details of the various
42    nonlocal references.  We want to do this sooner rather than later, in
43    order to give us more freedom in emitting all of the functions in question.
44
45    Back in olden times, when gcc was young, we developed an insanely 
46    complicated scheme whereby variables which were referenced nonlocally
47    were forced to live in the stack of the declaring function, and then
48    the nested functions magically discovered where these variables were
49    placed.  In order for this scheme to function properly, it required
50    that the outer function be partially expanded, then we switch to 
51    compiling the inner function, and once done with those we switch back
52    to compiling the outer function.  Such delicate ordering requirements
53    makes it difficult to do whole translation unit optimizations 
54    involving such functions.
55
56    The implementation here is much more direct.  Everything that can be
57    referenced by an inner function is a member of an explicitly created
58    structure herein called the "nonlocal frame struct".  The incoming
59    static chain for a nested function is a pointer to this struct in 
60    the parent.  In this way, we settle on known offsets from a known
61    base, and so are decoupled from the logic that places objects in the
62    function's stack frame.  More importantly, we don't have to wait for
63    that to happen -- since the compilation of the inner function is no
64    longer tied to a real stack frame, the nonlocal frame struct can be
65    allocated anywhere.  Which means that the outer function is now
66    inlinable.
67
68    Theory of operation here is very simple.  Iterate over all the 
69    statements in all the functions (depth first) several times, 
70    allocating structures and fields on demand.  In general we want to
71    examine inner functions first, so that we can avoid making changes
72    to outer functions which are unnecessary.
73
74    The order of the passes matters a bit, in that later passes will be
75    skipped if it is discovered that the functions don't actually interact
76    at all.  That is, they're nested in the lexical sense but could have
77    been written as independent functions without change.  */
78
79
80 struct var_map_elt
81 {
82   tree old;
83   tree new;
84 };
85
86 struct nesting_info
87 {
88   struct nesting_info *outer;
89   struct nesting_info *inner;
90   struct nesting_info *next;
91   
92   htab_t var_map;
93   tree context;
94   tree new_local_var_chain;
95   tree frame_type;
96   tree frame_decl;
97   tree chain_field;
98   tree chain_decl;
99   tree nl_goto_field;
100
101   bool any_parm_remapped;
102   bool any_tramp_created;
103 };
104
105
106 /* Hashing and equality functions for nesting_info->var_map.  */
107
108 static hashval_t
109 var_map_hash (const void *x)
110 {
111   const struct var_map_elt *a = x;
112   return htab_hash_pointer (a->old);
113 }
114
115 static int
116 var_map_eq (const void *x, const void *y)
117 {
118   const struct var_map_elt *a = x;
119   const struct var_map_elt *b = y;
120   return a->old == b->old;
121 }
122
123 /* We're working in so many different function contexts simultaneously,
124    that create_tmp_var is dangerous.  Prevent mishap.  */
125 #define create_tmp_var cant_use_create_tmp_var_here_dummy
126
127 /* Like create_tmp_var, except record the variable for registration at
128    the given nesting level.  */
129
130 static tree
131 create_tmp_var_for (struct nesting_info *info, tree type, const char *prefix)
132 {
133   tree tmp_var;
134
135   /* If the type is of variable size or a type which must be created by the
136      frontend, something is wrong.  Note that we explicitly allow
137      incomplete types here, since we create them ourselves here.  */
138   gcc_assert (!TREE_ADDRESSABLE (type));
139   gcc_assert (!TYPE_SIZE_UNIT (type)
140               || TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
141
142   tmp_var = create_tmp_var_raw (type, prefix);
143   DECL_CONTEXT (tmp_var) = info->context;
144   TREE_CHAIN (tmp_var) = info->new_local_var_chain;
145   DECL_SEEN_IN_BIND_EXPR_P (tmp_var) = 1;
146   info->new_local_var_chain = tmp_var;
147
148   return tmp_var;
149 }
150
151 /* Take the address of EXP to be used within function CONTEXT.
152    Mark it for addressability as necessary.  */
153
154 tree
155 build_addr (tree exp, tree context)
156 {
157   tree base = exp;
158   tree save_context;
159   tree retval;
160
161   while (handled_component_p (base))
162     base = TREE_OPERAND (base, 0);
163
164   if (DECL_P (base))
165     TREE_ADDRESSABLE (base) = 1;
166
167   /* Building the ADDR_EXPR will compute a set of properties for
168      that ADDR_EXPR.  Those properties are unfortunately context
169      specific.  ie, they are dependent on CURRENT_FUNCTION_DECL.
170
171      Temporarily set CURRENT_FUNCTION_DECL to the desired context,
172      build the ADDR_EXPR, then restore CURRENT_FUNCTION_DECL.  That
173      way the properties are for the ADDR_EXPR are computed properly.  */
174   save_context = current_function_decl;
175   current_function_decl = context;
176   retval = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
177   current_function_decl = save_context;;
178   return retval;
179 }
180
181 /* Insert FIELD into TYPE, sorted by alignment requirements.  */
182
183 static void
184 insert_field_into_struct (tree type, tree field)
185 {
186   tree *p;
187
188   DECL_CONTEXT (field) = type;
189
190   for (p = &TYPE_FIELDS (type); *p ; p = &TREE_CHAIN (*p))
191     if (DECL_ALIGN (field) >= DECL_ALIGN (*p))
192       break;
193
194   TREE_CHAIN (field) = *p;
195   *p = field;
196 }
197
198 /* Build or return the RECORD_TYPE that describes the frame state that is
199    shared between INFO->CONTEXT and its nested functions.  This record will
200    not be complete until finalize_nesting_tree; up until that point we'll
201    be adding fields as necessary.
202
203    We also build the DECL that represents this frame in the function.  */
204
205 static tree
206 get_frame_type (struct nesting_info *info)
207 {
208   tree type = info->frame_type;
209   if (!type)
210     {
211       char *name;
212
213       type = make_node (RECORD_TYPE);
214
215       name = concat ("FRAME.",
216                      IDENTIFIER_POINTER (DECL_NAME (info->context)),
217                      NULL);
218       TYPE_NAME (type) = get_identifier (name);
219       free (name);
220
221       info->frame_type = type;
222       info->frame_decl = create_tmp_var_for (info, type, "FRAME");
223     }
224   return type;
225 }
226
227 /* Return true if DECL should be referenced by pointer in the non-local
228    frame structure.  */
229
230 static bool
231 use_pointer_in_frame (tree decl)
232 {
233   if (TREE_CODE (decl) == PARM_DECL)
234     {
235       /* It's illegal to copy TREE_ADDRESSABLE, impossible to copy variable
236          sized decls, and inefficient to copy large aggregates.  Don't bother
237          moving anything but scalar variables.  */
238       return AGGREGATE_TYPE_P (TREE_TYPE (decl));
239     }
240   else
241     {
242       /* Variable sized types make things "interesting" in the frame.  */
243       return DECL_SIZE (decl) == NULL || !TREE_CONSTANT (DECL_SIZE (decl));
244     }
245 }
246
247 /* Given DECL, a non-locally accessed variable, find or create a field
248    in the non-local frame structure for the given nesting context.  */
249
250 static tree
251 lookup_field_for_decl (struct nesting_info *info, tree decl,
252                        enum insert_option insert)
253 {
254   struct var_map_elt *elt, dummy;
255   void **slot;
256   tree field;
257
258   dummy.old = decl;
259   slot = htab_find_slot (info->var_map, &dummy, insert);
260   if (!slot)
261     {
262       gcc_assert (insert != INSERT);
263       return NULL;
264     }
265   elt = *slot;
266
267   if (!elt && insert == INSERT)
268     {
269       field = make_node (FIELD_DECL);
270       DECL_NAME (field) = DECL_NAME (decl);
271
272       if (use_pointer_in_frame (decl))
273         {
274           TREE_TYPE (field) = build_pointer_type (TREE_TYPE (decl));
275           DECL_ALIGN (field) = TYPE_ALIGN (TREE_TYPE (field));
276           DECL_NONADDRESSABLE_P (field) = 1;
277         }
278       else
279         {
280           TREE_TYPE (field) = TREE_TYPE (decl);
281           DECL_SOURCE_LOCATION (field) = DECL_SOURCE_LOCATION (decl);
282           DECL_ALIGN (field) = DECL_ALIGN (decl);
283           DECL_USER_ALIGN (field) = DECL_USER_ALIGN (decl);
284           TREE_ADDRESSABLE (field) = TREE_ADDRESSABLE (decl);
285           DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (decl);
286           TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (decl);
287         }
288
289       insert_field_into_struct (get_frame_type (info), field);
290   
291       elt = xmalloc (sizeof (*elt));
292       elt->old = decl;
293       elt->new = field;
294       *slot = elt;
295
296       if (TREE_CODE (decl) == PARM_DECL)
297         info->any_parm_remapped = true;
298     }
299   else
300     field = elt ? elt->new : NULL;
301
302   return field;
303 }
304
305 /* Build or return the variable that holds the static chain within
306    INFO->CONTEXT.  This variable may only be used within INFO->CONTEXT.  */
307
308 static tree
309 get_chain_decl (struct nesting_info *info)
310 {
311   tree decl = info->chain_decl;
312   if (!decl)
313     {
314       tree type;
315
316       type = get_frame_type (info->outer);
317       type = build_pointer_type (type);
318
319       /* Note that this variable is *not* entered into any BIND_EXPR;
320          the construction of this variable is handled specially in
321          expand_function_start and initialize_inlined_parameters.
322          Note also that it's represented as a parameter.  This is more
323          close to the truth, since the initial value does come from 
324          the caller.  */
325       decl = build_decl (PARM_DECL, create_tmp_var_name ("CHAIN"), type);
326       DECL_ARTIFICIAL (decl) = 1;
327       DECL_IGNORED_P (decl) = 1;
328       TREE_USED (decl) = 1;
329       DECL_CONTEXT (decl) = info->context;
330       DECL_ARG_TYPE (decl) = type;
331
332       /* Tell tree-inline.c that we never write to this variable, so
333          it can copy-prop the replacement value immediately.  */
334       TREE_READONLY (decl) = 1;
335
336       info->chain_decl = decl;
337     }
338   return decl;
339 }
340
341 /* Build or return the field within the non-local frame state that holds
342    the static chain for INFO->CONTEXT.  This is the way to walk back up
343    multiple nesting levels.  */
344
345 static tree
346 get_chain_field (struct nesting_info *info)
347 {
348   tree field = info->chain_field;
349   if (!field)
350     {
351       tree type = build_pointer_type (get_frame_type (info->outer));
352
353       field = make_node (FIELD_DECL);
354       DECL_NAME (field) = get_identifier ("__chain");
355       TREE_TYPE (field) = type;
356       DECL_ALIGN (field) = TYPE_ALIGN (type);
357       DECL_NONADDRESSABLE_P (field) = 1;
358
359       insert_field_into_struct (get_frame_type (info), field);
360
361       info->chain_field = field;
362     }
363   return field;
364 }
365
366 /* Copy EXP into a temporary.  Allocate the temporary in the context of
367    INFO and insert the initialization statement before TSI.  */
368
369 static tree
370 init_tmp_var (struct nesting_info *info, tree exp, tree_stmt_iterator *tsi)
371 {
372   tree t, stmt;
373
374   t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
375   stmt = build (MODIFY_EXPR, TREE_TYPE (t), t, exp);
376   SET_EXPR_LOCUS (stmt, EXPR_LOCUS (tsi_stmt (*tsi)));
377   tsi_link_before (tsi, stmt, TSI_SAME_STMT);
378
379   return t;
380 }
381
382 /* Similarly, but only do so to force EXP to satisfy is_gimple_val.  */
383
384 static tree
385 tsi_gimplify_val (struct nesting_info *info, tree exp, tree_stmt_iterator *tsi)
386 {
387   if (is_gimple_val (exp))
388     return exp;
389   else
390     return init_tmp_var (info, exp, tsi);
391 }
392
393 /* Similarly, but copy from the temporary and insert the statement
394    after the iterator.  */
395
396 static tree
397 save_tmp_var (struct nesting_info *info, tree exp,
398               tree_stmt_iterator *tsi)
399 {
400   tree t, stmt;
401
402   t = create_tmp_var_for (info, TREE_TYPE (exp), NULL);
403   stmt = build (MODIFY_EXPR, TREE_TYPE (t), exp, t);
404   SET_EXPR_LOCUS (stmt, EXPR_LOCUS (tsi_stmt (*tsi)));
405   tsi_link_after (tsi, stmt, TSI_SAME_STMT);
406
407   return t;
408 }
409
410 /* Build or return the type used to represent a nested function trampoline.  */
411
412 static GTY(()) tree trampoline_type;
413
414 static tree
415 get_trampoline_type (void)
416 {
417   tree record, t;
418   unsigned align, size;
419
420   if (trampoline_type)
421     return trampoline_type;
422
423   align = TRAMPOLINE_ALIGNMENT;
424   size = TRAMPOLINE_SIZE;
425
426   /* If we won't be able to guarantee alignment simply via TYPE_ALIGN,
427      then allocate extra space so that we can do dynamic alignment.  */
428   if (align > STACK_BOUNDARY)
429     {
430       size += ((align/BITS_PER_UNIT) - 1) & -(STACK_BOUNDARY/BITS_PER_UNIT);
431       align = STACK_BOUNDARY;
432     }
433
434   t = build_index_type (build_int_cst (NULL_TREE, size - 1));
435   t = build_array_type (char_type_node, t);
436   t = build_decl (FIELD_DECL, get_identifier ("__data"), t);
437   DECL_ALIGN (t) = align;
438   DECL_USER_ALIGN (t) = 1;
439
440   record = make_node (RECORD_TYPE);
441   TYPE_NAME (record) = get_identifier ("__builtin_trampoline");
442   TYPE_FIELDS (record) = t;
443   layout_type (record);
444
445   return record;
446 }
447
448 /* Given DECL, a nested function, find or create a field in the non-local
449    frame structure for a trampoline for this function.  */
450
451 static tree
452 lookup_tramp_for_decl (struct nesting_info *info, tree decl,
453                        enum insert_option insert)
454 {
455   struct var_map_elt *elt, dummy;
456   void **slot;
457   tree field;
458
459   dummy.old = decl;
460   slot = htab_find_slot (info->var_map, &dummy, insert);
461   if (!slot)
462     {
463       gcc_assert (insert != INSERT);
464       return NULL;
465     }
466   elt = *slot;
467
468   if (!elt && insert == INSERT)
469     {
470       field = make_node (FIELD_DECL);
471       DECL_NAME (field) = DECL_NAME (decl);
472       TREE_TYPE (field) = get_trampoline_type ();
473       TREE_ADDRESSABLE (field) = 1;
474
475       insert_field_into_struct (get_frame_type (info), field);
476
477       elt = xmalloc (sizeof (*elt));
478       elt->old = decl;
479       elt->new = field;
480       *slot = elt;
481
482       info->any_tramp_created = true;
483     }
484   else
485     field = elt ? elt->new : NULL;
486
487   return field;
488
489
490 /* Build or return the field within the non-local frame state that holds
491    the non-local goto "jmp_buf".  The buffer itself is maintained by the
492    rtl middle-end as dynamic stack space is allocated.  */
493
494 static tree
495 get_nl_goto_field (struct nesting_info *info)
496 {
497   tree field = info->nl_goto_field;
498   if (!field)
499     {
500       unsigned size;
501       tree type;
502
503       /* For __builtin_nonlocal_goto, we need N words.  The first is the
504          frame pointer, the rest is for the target's stack pointer save
505          area.  The number of words is controlled by STACK_SAVEAREA_MODE;
506          not the best interface, but it'll do for now.  */
507       if (Pmode == ptr_mode)
508         type = ptr_type_node;
509       else
510         type = lang_hooks.types.type_for_mode (Pmode, 1);
511
512       size = GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
513       size = size / GET_MODE_SIZE (Pmode);
514       size = size + 1;
515
516       type = build_array_type
517         (type, build_index_type (build_int_cst (NULL_TREE, size)));
518
519       field = make_node (FIELD_DECL);
520       DECL_NAME (field) = get_identifier ("__nl_goto_buf");
521       TREE_TYPE (field) = type;
522       DECL_ALIGN (field) = TYPE_ALIGN (type);
523       TREE_ADDRESSABLE (field) = 1;
524
525       insert_field_into_struct (get_frame_type (info), field);
526
527       info->nl_goto_field = field;
528     }
529
530   return field;
531 }
532 \f
533 /* Convenience routines to walk all statements of a gimple function.
534
535    For each statement, we invoke CALLBACK via walk_tree.  The passed
536    data is a walk_stmt_info structure.  Of note here is a TSI that
537    points to the current statement being walked.  The VAL_ONLY flag
538    that indicates whether the *TP being examined may be replaced 
539    with something that matches is_gimple_val (if true) or something
540    slightly more complicated (if false).  "Something" technically 
541    means the common subset of is_gimple_lvalue and is_gimple_rhs, 
542    but we never try to form anything more complicated than that, so
543    we don't bother checking.  */
544
545 struct walk_stmt_info
546 {
547   walk_tree_fn callback;
548   tree_stmt_iterator tsi;
549   struct nesting_info *info;
550   bool val_only;
551   bool is_lhs;
552   bool changed;
553 };
554
555 /* A subroutine of walk_function.  Iterate over all sub-statements of *TP.  */
556
557 static void
558 walk_stmts (struct walk_stmt_info *wi, tree *tp)
559 {
560   tree t = *tp;
561   if (!t)
562     return;
563
564   switch (TREE_CODE (t))
565     {
566     case STATEMENT_LIST:
567       {
568         tree_stmt_iterator i;
569         for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
570           {
571             wi->tsi = i;
572             walk_stmts (wi, tsi_stmt_ptr (i));
573           }
574       }
575       break;
576
577     case COND_EXPR:
578       walk_tree (&COND_EXPR_COND (t), wi->callback, wi, NULL);
579       walk_stmts (wi, &COND_EXPR_THEN (t));
580       walk_stmts (wi, &COND_EXPR_ELSE (t));
581       break;
582     case CATCH_EXPR:
583       walk_stmts (wi, &CATCH_BODY (t));
584       break;
585     case EH_FILTER_EXPR:
586       walk_stmts (wi, &EH_FILTER_FAILURE (t));
587       break;
588     case TRY_CATCH_EXPR:
589     case TRY_FINALLY_EXPR:
590       walk_stmts (wi, &TREE_OPERAND (t, 0));
591       walk_stmts (wi, &TREE_OPERAND (t, 1));
592       break;
593     case BIND_EXPR:
594       walk_stmts (wi, &BIND_EXPR_BODY (t));
595       break;
596
597     case RETURN_EXPR:
598       walk_stmts (wi, &TREE_OPERAND (t, 0));
599       break;
600
601     case MODIFY_EXPR:
602       /* A formal temporary lhs may use a COMPONENT_REF rhs.  */
603       wi->val_only = !is_gimple_formal_tmp_var (TREE_OPERAND (t, 0));
604       walk_tree (&TREE_OPERAND (t, 1), wi->callback, wi, NULL);
605
606       /* If the rhs is appropriate for a memory, we may use a
607          COMPONENT_REF on the lhs.  */
608       wi->val_only = !is_gimple_mem_rhs (TREE_OPERAND (t, 1));
609       wi->is_lhs = true;
610       walk_tree (&TREE_OPERAND (t, 0), wi->callback, wi, NULL);
611
612       wi->val_only = true;
613       wi->is_lhs = false;
614       break;
615
616     default:
617       wi->val_only = true;
618       walk_tree (tp, wi->callback, wi, NULL);
619       break;
620     }
621 }
622
623 /* Invoke CALLBACK on all statements of INFO->CONTEXT.  */
624
625 static void
626 walk_function (walk_tree_fn callback, struct nesting_info *info)
627 {
628   struct walk_stmt_info wi;
629
630   memset (&wi, 0, sizeof (wi));
631   wi.callback = callback;
632   wi.info = info;
633   wi.val_only = true;
634
635   walk_stmts (&wi, &DECL_SAVED_TREE (info->context));
636 }
637
638 /* Similarly for ROOT and all functions nested underneath, depth first.  */
639     
640 static void
641 walk_all_functions (walk_tree_fn callback, struct nesting_info *root)
642 {
643   do
644     {
645       if (root->inner)
646         walk_all_functions (callback, root->inner);
647       walk_function (callback, root);
648       root = root->next;
649     }
650   while (root);
651 }
652 \f
653 /* We have to check for a fairly pathological case.  The operands of function
654    nested function are to be interpreted in the context of the enclosing
655    function.  So if any are variably-sized, they will get remapped when the
656    enclosing function is inlined.  But that remapping would also have to be
657    done in the types of the PARM_DECLs of the nested function, meaning the
658    argument types of that function will disagree with the arguments in the
659    calls to that function.  So we'd either have to make a copy of the nested
660    function corresponding to each time the enclosing function was inlined or
661    add a VIEW_CONVERT_EXPR to each such operand for each call to the nested
662    function.  The former is not practical.  The latter would still require
663    detecting this case to know when to add the conversions.  So, for now at
664    least, we don't inline such an enclosing function.
665
666    We have to do that check recursively, so here return indicating whether
667    FNDECL has such a nested function.  ORIG_FN is the function we were
668    trying to inline to use for checking whether any argument is variably
669    modified by anything in it.
670
671    It would be better to do this in tree-inline.c so that we could give
672    the appropriate warning for why a function can't be inlined, but that's
673    too late since the nesting structure has already been flattened and
674    adding a flag just to record this fact seems a waste of a flag.  */
675
676 static bool
677 check_for_nested_with_variably_modified (tree fndecl, tree orig_fndecl)
678 {
679   struct cgraph_node *cgn = cgraph_node (fndecl);
680   tree arg;
681
682   for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
683     {
684       for (arg = DECL_ARGUMENTS (cgn->decl); arg; arg = TREE_CHAIN (arg))
685         if (variably_modified_type_p (TREE_TYPE (arg), 0), orig_fndecl)
686           return true;
687
688       if (check_for_nested_with_variably_modified (cgn->decl, orig_fndecl))
689         return true;
690     }
691
692   return false;
693 }
694
695 /* Construct our local datastructure describing the function nesting
696    tree rooted by CGN.  */
697
698 static struct nesting_info *
699 create_nesting_tree (struct cgraph_node *cgn)
700 {
701   struct nesting_info *info = xcalloc (1, sizeof (*info));
702   info->var_map = htab_create (7, var_map_hash, var_map_eq, free);
703   info->context = cgn->decl;
704
705   for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
706     {
707       struct nesting_info *sub = create_nesting_tree (cgn);
708       sub->outer = info;
709       sub->next = info->inner;
710       info->inner = sub;
711     }
712
713   /* See discussion at check_for_nested_with_variably_modified for a
714      discussion of why this has to be here.  */
715   if (check_for_nested_with_variably_modified (info->context, info->context))
716     DECL_UNINLINABLE (info->context) = true;
717
718   return info;
719 }
720
721 /* Return an expression computing the static chain for TARGET_CONTEXT
722    from INFO->CONTEXT.  Insert any necessary computations before TSI.  */
723
724 static tree
725 get_static_chain (struct nesting_info *info, tree target_context,
726                   tree_stmt_iterator *tsi)
727 {
728   struct nesting_info *i;
729   tree x;
730
731   if (info->context == target_context)
732     {
733       x = build_addr (info->frame_decl, target_context);
734     }
735   else
736     {
737       x = get_chain_decl (info);
738
739       for (i = info->outer; i->context != target_context; i = i->outer)
740         {
741           tree field = get_chain_field (i);
742
743           x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
744           x = build (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
745           x = init_tmp_var (info, x, tsi);
746         }
747     }
748
749   return x;
750 }
751
752 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
753    frame as seen from INFO->CONTEXT.  Insert any necessary computations
754    before TSI.  */
755
756 static tree
757 get_frame_field (struct nesting_info *info, tree target_context,
758                  tree field, tree_stmt_iterator *tsi)
759 {
760   struct nesting_info *i;
761   tree x;
762
763   if (info->context == target_context)
764     {
765       /* Make sure frame_decl gets created.  */
766       (void) get_frame_type (info);
767       x = info->frame_decl;
768     }
769   else
770     {
771       x = get_chain_decl (info);
772
773       for (i = info->outer; i->context != target_context; i = i->outer)
774         {
775           tree field = get_chain_field (i);
776
777           x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
778           x = build (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
779           x = init_tmp_var (info, x, tsi);
780         }
781
782       x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
783     }
784
785   x = build (COMPONENT_REF, TREE_TYPE (field), x, field, NULL_TREE);
786   return x;
787 }
788
789 /* Called via walk_function+walk_tree, rewrite all references to VAR
790    and PARM_DECLs that belong to outer functions.
791
792    The rewrite will involve some number of structure accesses back up
793    the static chain.  E.g. for a variable FOO up one nesting level it'll
794    be CHAIN->FOO.  For two levels it'll be CHAIN->__chain->FOO.  Further
795    indirections apply to decls for which use_pointer_in_frame is true.  */
796
797 static tree
798 convert_nonlocal_reference (tree *tp, int *walk_subtrees, void *data)
799 {
800   struct walk_stmt_info *wi = data;
801   struct nesting_info *info = wi->info;
802   tree t = *tp;
803
804   *walk_subtrees = 0;
805   switch (TREE_CODE (t))
806     {
807     case VAR_DECL:
808       /* Non-automatic variables are never processed.  */
809       if (TREE_STATIC (t) || DECL_EXTERNAL (t))
810         break;
811       /* FALLTHRU */
812
813     case PARM_DECL:
814       if (decl_function_context (t) != info->context)
815         {
816           tree target_context = decl_function_context (t);
817           struct nesting_info *i;
818           tree x;
819           wi->changed = true;
820
821           for (i = info->outer; i->context != target_context; i = i->outer)
822             continue;
823           x = lookup_field_for_decl (i, t, INSERT);
824           x = get_frame_field (info, target_context, x, &wi->tsi);
825           if (use_pointer_in_frame (t))
826             {
827               x = init_tmp_var (info, x, &wi->tsi);
828               x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
829             }
830
831           if (wi->val_only)
832             {
833               if (wi->is_lhs)
834                 x = save_tmp_var (info, x, &wi->tsi);
835               else
836                 x = init_tmp_var (info, x, &wi->tsi);
837             }
838
839           *tp = x;
840         }
841       break;
842
843     case GOTO_EXPR:
844       /* Don't walk non-local gotos for now.  */
845       if (TREE_CODE (GOTO_DESTINATION (t)) != LABEL_DECL)
846         {
847           *walk_subtrees = 1;
848           wi->val_only = true;
849           wi->is_lhs = false;
850         }
851       break;
852
853     case LABEL_DECL:
854       /* We're taking the address of a label from a parent function, but
855          this is not itself a non-local goto.  Mark the label such that it
856          will not be deleted, much as we would with a label address in
857          static storage.  */
858       if (decl_function_context (t) != info->context)
859         FORCED_LABEL (t) = 1;
860       break;
861
862     case ADDR_EXPR:
863       {
864         bool save_val_only = wi->val_only;
865
866         wi->val_only = false;
867         wi->is_lhs = false;
868         wi->changed = false;
869         walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference, wi, NULL);
870         wi->val_only = true;
871
872         if (wi->changed)
873           {
874             tree save_context;
875
876             /* If we changed anything, then TREE_INVARIANT is be wrong,
877                since we're no longer directly referencing a decl.  */
878             save_context = current_function_decl;
879             current_function_decl = info->context;
880             recompute_tree_invarant_for_addr_expr (t);
881             current_function_decl = save_context;
882
883             /* If the callback converted the address argument in a context
884                where we only accept variables (and min_invariant, presumably),
885                then compute the address into a temporary.  */
886             if (save_val_only)
887               *tp = tsi_gimplify_val (wi->info, t, &wi->tsi);
888           }
889       }
890       break;
891
892     case REALPART_EXPR:
893     case IMAGPART_EXPR:
894     case COMPONENT_REF:
895     case ARRAY_REF:
896     case ARRAY_RANGE_REF:
897     case BIT_FIELD_REF:
898       /* Go down this entire nest and just look at the final prefix and
899          anything that describes the references.  Otherwise, we lose track
900          of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value.  */
901       wi->val_only = true;
902       wi->is_lhs = false;
903       for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
904         {
905           if (TREE_CODE (t) == COMPONENT_REF)
906             walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference, wi,
907                        NULL);
908           else if (TREE_CODE (t) == ARRAY_REF
909                    || TREE_CODE (t) == ARRAY_RANGE_REF)
910             {
911               walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference, wi,
912                          NULL);
913               walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference, wi,
914                          NULL);
915               walk_tree (&TREE_OPERAND (t, 3), convert_nonlocal_reference, wi,
916                          NULL);
917             }
918           else if (TREE_CODE (t) == BIT_FIELD_REF)
919             {
920               walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference, wi,
921                          NULL);
922               walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference, wi,
923                          NULL);
924             }
925         }
926       wi->val_only = false;
927       walk_tree (tp, convert_nonlocal_reference, wi, NULL);
928       break;
929
930     default:
931       if (!IS_TYPE_OR_DECL_P (t))
932         {
933           *walk_subtrees = 1;
934           wi->val_only = true;
935           wi->is_lhs = false;
936         }
937       break;
938     }
939
940   return NULL_TREE;
941 }
942
943 /* Called via walk_function+walk_tree, rewrite all references to VAR
944    and PARM_DECLs that were referenced by inner nested functions.
945    The rewrite will be a structure reference to the local frame variable.  */
946
947 static tree
948 convert_local_reference (tree *tp, int *walk_subtrees, void *data)
949 {
950   struct walk_stmt_info *wi = data;
951   struct nesting_info *info = wi->info;
952   tree t = *tp, field, x;
953   bool save_val_only;
954
955   *walk_subtrees = 0;
956   switch (TREE_CODE (t))
957     {
958     case VAR_DECL:
959       /* Non-automatic variables are never processed.  */
960       if (TREE_STATIC (t) || DECL_EXTERNAL (t))
961         break;
962       /* FALLTHRU */
963
964     case PARM_DECL:
965       if (decl_function_context (t) == info->context)
966         {
967           /* If we copied a pointer to the frame, then the original decl
968              is used unchanged in the parent function.  */
969           if (use_pointer_in_frame (t))
970             break;
971
972           /* No need to transform anything if no child references the
973              variable.  */
974           field = lookup_field_for_decl (info, t, NO_INSERT);
975           if (!field)
976             break;
977           wi->changed = true;
978
979           x = get_frame_field (info, info->context, field, &wi->tsi);
980
981           if (wi->val_only)
982             {
983               if (wi->is_lhs)
984                 x = save_tmp_var (info, x, &wi->tsi);
985               else
986                 x = init_tmp_var (info, x, &wi->tsi);
987             }
988
989           *tp = x;
990         }
991       break;
992
993     case ADDR_EXPR:
994       save_val_only = wi->val_only;
995       wi->val_only = false;
996       wi->is_lhs = false;
997       wi->changed = false;
998       walk_tree (&TREE_OPERAND (t, 0), convert_local_reference, wi, NULL);
999       wi->val_only = save_val_only;
1000
1001       /* If we converted anything ... */
1002       if (wi->changed)
1003         {
1004           tree save_context;
1005
1006           /* Then the frame decl is now addressable.  */
1007           TREE_ADDRESSABLE (info->frame_decl) = 1;
1008             
1009           save_context = current_function_decl;
1010           current_function_decl = info->context;
1011           recompute_tree_invarant_for_addr_expr (t);
1012           current_function_decl = save_context;
1013
1014           /* If we are in a context where we only accept values, then
1015              compute the address into a temporary.  */
1016           if (save_val_only)
1017             *tp = tsi_gimplify_val (wi->info, t, &wi->tsi);
1018         }
1019       break;
1020
1021     case REALPART_EXPR:
1022     case IMAGPART_EXPR:
1023     case COMPONENT_REF:
1024     case ARRAY_REF:
1025     case ARRAY_RANGE_REF:
1026     case BIT_FIELD_REF:
1027       /* Go down this entire nest and just look at the final prefix and
1028          anything that describes the references.  Otherwise, we lose track
1029          of whether a NOP_EXPR or VIEW_CONVERT_EXPR needs a simple value.  */
1030       save_val_only = wi->val_only;
1031       wi->val_only = true;
1032       wi->is_lhs = false;
1033       for (; handled_component_p (t); tp = &TREE_OPERAND (t, 0), t = *tp)
1034         {
1035           if (TREE_CODE (t) == COMPONENT_REF)
1036             walk_tree (&TREE_OPERAND (t, 2), convert_local_reference, wi,
1037                        NULL);
1038           else if (TREE_CODE (t) == ARRAY_REF
1039                    || TREE_CODE (t) == ARRAY_RANGE_REF)
1040             {
1041               walk_tree (&TREE_OPERAND (t, 1), convert_local_reference, wi,
1042                          NULL);
1043               walk_tree (&TREE_OPERAND (t, 2), convert_local_reference, wi,
1044                          NULL);
1045               walk_tree (&TREE_OPERAND (t, 3), convert_local_reference, wi,
1046                          NULL);
1047             }
1048           else if (TREE_CODE (t) == BIT_FIELD_REF)
1049             {
1050               walk_tree (&TREE_OPERAND (t, 1), convert_local_reference, wi,
1051                          NULL);
1052               walk_tree (&TREE_OPERAND (t, 2), convert_local_reference, wi,
1053                          NULL);
1054             }
1055         }
1056       wi->val_only = false;
1057       walk_tree (tp, convert_local_reference, wi, NULL);
1058       wi->val_only = save_val_only;
1059       break;
1060
1061     default:
1062       if (!IS_TYPE_OR_DECL_P (t))
1063         {
1064           *walk_subtrees = 1;
1065           wi->val_only = true;
1066           wi->is_lhs = false;
1067         }
1068       break;
1069     }
1070
1071   return NULL_TREE;
1072 }
1073
1074 /* Called via walk_function+walk_tree, rewrite all GOTO_EXPRs that 
1075    reference labels from outer functions.  The rewrite will be a 
1076    call to __builtin_nonlocal_goto.  */
1077
1078 static tree
1079 convert_nl_goto_reference (tree *tp, int *walk_subtrees, void *data)
1080 {
1081   struct walk_stmt_info *wi = data;
1082   struct nesting_info *info = wi->info, *i;
1083   tree t = *tp, label, new_label, target_context, x, arg, field;
1084   struct var_map_elt *elt;
1085   void **slot;
1086
1087   *walk_subtrees = 0;
1088   if (TREE_CODE (t) != GOTO_EXPR)
1089     return NULL_TREE;
1090   label = GOTO_DESTINATION (t);
1091   if (TREE_CODE (label) != LABEL_DECL)
1092     return NULL_TREE;
1093   target_context = decl_function_context (label);
1094   if (target_context == info->context)
1095     return NULL_TREE;
1096
1097   for (i = info->outer; target_context != i->context; i = i->outer)
1098     continue;
1099
1100   /* The original user label may also be use for a normal goto, therefore
1101      we must create a new label that will actually receive the abnormal
1102      control transfer.  This new label will be marked LABEL_NONLOCAL; this
1103      mark will trigger proper behavior in the cfg, as well as cause the
1104      (hairy target-specific) non-local goto receiver code to be generated
1105      when we expand rtl.  */
1106   new_label = create_artificial_label ();
1107   DECL_NONLOCAL (new_label) = 1;
1108
1109   /* Enter this association into var_map so that we can insert the new
1110      label into the IL during a second pass.  */
1111   elt = xmalloc (sizeof (*elt));
1112   elt->old = label;
1113   elt->new = new_label;
1114   slot = htab_find_slot (i->var_map, elt, INSERT);
1115   *slot = elt;
1116   
1117   /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field).  */
1118   field = get_nl_goto_field (i);
1119   x = get_frame_field (info, target_context, field, &wi->tsi);
1120   x = build_addr (x, target_context);
1121   x = tsi_gimplify_val (info, x, &wi->tsi);
1122   arg = tree_cons (NULL, x, NULL);
1123   x = build_addr (new_label, target_context);
1124   arg = tree_cons (NULL, x, arg);
1125   x = implicit_built_in_decls[BUILT_IN_NONLOCAL_GOTO];
1126   x = build_function_call_expr (x, arg);
1127
1128   SET_EXPR_LOCUS (x, EXPR_LOCUS (tsi_stmt (wi->tsi)));
1129   *tsi_stmt_ptr (wi->tsi) = x;
1130
1131   return NULL_TREE;
1132 }
1133
1134 /* Called via walk_function+walk_tree, rewrite all LABEL_EXPRs that 
1135    are referenced via nonlocal goto from a nested function.  The rewrite
1136    will involve installing a newly generated DECL_NONLOCAL label, and
1137    (potentially) a branch around the rtl gunk that is assumed to be 
1138    attached to such a label.  */
1139
1140 static tree
1141 convert_nl_goto_receiver (tree *tp, int *walk_subtrees, void *data)
1142 {
1143   struct walk_stmt_info *wi = data;
1144   struct nesting_info *info = wi->info;
1145   tree t = *tp, label, new_label, x;
1146   struct var_map_elt *elt, dummy;
1147   tree_stmt_iterator tmp_tsi;
1148
1149   *walk_subtrees = 0;
1150   if (TREE_CODE (t) != LABEL_EXPR)
1151     return NULL_TREE;
1152   label = LABEL_EXPR_LABEL (t);
1153
1154   dummy.old = label;
1155   elt = htab_find (info->var_map, &dummy);
1156   if (!elt)
1157     return NULL_TREE;
1158   new_label = elt->new;
1159
1160   /* If there's any possibility that the previous statement falls through,
1161      then we must branch around the new non-local label.  */
1162   tmp_tsi = wi->tsi;
1163   tsi_prev (&tmp_tsi);
1164   if (tsi_end_p (tmp_tsi) || block_may_fallthru (tsi_stmt (tmp_tsi)))
1165     {
1166       x = build1 (GOTO_EXPR, void_type_node, label);
1167       tsi_link_before (&wi->tsi, x, TSI_SAME_STMT);
1168     }
1169   x = build1 (LABEL_EXPR, void_type_node, new_label);
1170   tsi_link_before (&wi->tsi, x, TSI_SAME_STMT);
1171
1172   return NULL_TREE;
1173 }
1174
1175 /* Called via walk_function+walk_tree, rewrite all references to addresses
1176    of nested functions that require the use of trampolines.  The rewrite
1177    will involve a reference a trampoline generated for the occasion.  */
1178
1179 static tree
1180 convert_tramp_reference (tree *tp, int *walk_subtrees, void *data)
1181 {
1182   struct walk_stmt_info *wi = data;
1183   struct nesting_info *info = wi->info, *i;
1184   tree t = *tp, decl, target_context, x, arg;
1185
1186   *walk_subtrees = 0;
1187   switch (TREE_CODE (t))
1188     {
1189     case ADDR_EXPR:
1190       /* Build
1191            T.1 = &CHAIN->tramp;
1192            T.2 = __builtin_adjust_trampoline (T.1);
1193            T.3 = (func_type)T.2;
1194       */
1195
1196       decl = TREE_OPERAND (t, 0);
1197       if (TREE_CODE (decl) != FUNCTION_DECL)
1198         break;
1199
1200       /* Only need to process nested functions.  */
1201       target_context = decl_function_context (decl);
1202       if (!target_context)
1203         break;
1204
1205       /* If the nested function doesn't use a static chain, then
1206          it doesn't need a trampoline.  */
1207       if (DECL_NO_STATIC_CHAIN (decl))
1208         break;
1209
1210       /* Lookup the immediate parent of the callee, as that's where
1211          we need to insert the trampoline.  */
1212       for (i = info; i->context != target_context; i = i->outer)
1213         continue;
1214       x = lookup_tramp_for_decl (i, decl, INSERT);
1215
1216       /* Compute the address of the field holding the trampoline.  */
1217       x = get_frame_field (info, target_context, x, &wi->tsi);
1218       x = build_addr (x, target_context);
1219       x = tsi_gimplify_val (info, x, &wi->tsi);
1220       arg = tree_cons (NULL, x, NULL);
1221
1222       /* Do machine-specific ugliness.  Normally this will involve
1223          computing extra alignment, but it can really be anything.  */
1224       x = implicit_built_in_decls[BUILT_IN_ADJUST_TRAMPOLINE];
1225       x = build_function_call_expr (x, arg);
1226       x = init_tmp_var (info, x, &wi->tsi);
1227
1228       /* Cast back to the proper function type.  */
1229       x = build1 (NOP_EXPR, TREE_TYPE (t), x);
1230       x = init_tmp_var (info, x, &wi->tsi);
1231
1232       *tp = x;
1233       break;
1234
1235     case CALL_EXPR:
1236       /* Only walk call arguments, lest we generate trampolines for
1237          direct calls.  */
1238       walk_tree (&TREE_OPERAND (t, 1), convert_tramp_reference, wi, NULL);
1239       break;
1240
1241     default:
1242       if (!IS_TYPE_OR_DECL_P (t))
1243         *walk_subtrees = 1;
1244       break;
1245     }
1246
1247   return NULL_TREE;
1248 }
1249
1250 /* Called via walk_function+walk_tree, rewrite all CALL_EXPRs that 
1251    reference nested functions to make sure that the static chain is
1252    set up properly for the call.  */
1253
1254 static tree
1255 convert_call_expr (tree *tp, int *walk_subtrees, void *data)
1256 {
1257   struct walk_stmt_info *wi = data;
1258   struct nesting_info *info = wi->info;
1259   tree t = *tp, decl, target_context;
1260
1261   *walk_subtrees = 0;
1262   switch (TREE_CODE (t))
1263     {
1264     case CALL_EXPR:
1265       decl = get_callee_fndecl (t);
1266       if (!decl)
1267         break;
1268       target_context = decl_function_context (decl);
1269       if (target_context && !DECL_NO_STATIC_CHAIN (decl))
1270         TREE_OPERAND (t, 2)
1271           = get_static_chain (info, target_context, &wi->tsi);
1272       break;
1273
1274     case RETURN_EXPR:
1275     case MODIFY_EXPR:
1276     case WITH_SIZE_EXPR:
1277       /* Only return modify and with_size_expr may contain calls.  */
1278       *walk_subtrees = 1;
1279       break;
1280
1281     default:
1282       break;
1283     }
1284
1285   return NULL_TREE;
1286 }
1287
1288 /* Walk the nesting tree starting with ROOT, depth first.  Convert all
1289    trampolines and call expressions.  On the way back up, determine if
1290    a nested function actually uses its static chain; if not, remember that.  */
1291
1292 static void
1293 convert_all_function_calls (struct nesting_info *root)
1294 {
1295   do
1296     {
1297       if (root->inner)
1298         convert_all_function_calls (root->inner);
1299
1300       walk_function (convert_tramp_reference, root);
1301       walk_function (convert_call_expr, root);
1302
1303       /* If the function does not use a static chain, then remember that.  */
1304       if (root->outer && !root->chain_decl && !root->chain_field)
1305         DECL_NO_STATIC_CHAIN (root->context) = 1;
1306       else
1307         gcc_assert (!DECL_NO_STATIC_CHAIN (root->context));
1308
1309       root = root->next;
1310     }
1311   while (root);
1312 }
1313
1314 /* Do "everything else" to clean up or complete state collected by the
1315    various walking passes -- lay out the types and decls, generate code
1316    to initialize the frame decl, store critical expressions in the
1317    struct function for rtl to find.  */
1318
1319 static void
1320 finalize_nesting_tree_1 (struct nesting_info *root)
1321 {
1322   tree stmt_list = NULL;
1323   tree context = root->context;
1324   struct function *sf;
1325   struct cgraph_node *node;
1326
1327   /* If we created a non-local frame type or decl, we need to lay them
1328      out at this time.  */
1329   if (root->frame_type)
1330     {
1331       /* In some cases the frame type will trigger the -Wpadded warning.
1332          This is not helpful; suppress it. */
1333       int save_warn_padded = warn_padded;
1334       warn_padded = 0;
1335       layout_type (root->frame_type);
1336       warn_padded = save_warn_padded;
1337       layout_decl (root->frame_decl, 0);
1338     }
1339
1340   /* If any parameters were referenced non-locally, then we need to 
1341      insert a copy.  Likewise, if any variables were referenced by
1342      pointer, we need to initialize the address.  */
1343   if (root->any_parm_remapped)
1344     {
1345       tree p;
1346       for (p = DECL_ARGUMENTS (context); p ; p = TREE_CHAIN (p))
1347         {
1348           tree field, x, y;
1349
1350           field = lookup_field_for_decl (root, p, NO_INSERT);
1351           if (!field)
1352             continue;
1353
1354           if (use_pointer_in_frame (p))
1355             x = build_addr (p, context);
1356           else
1357             x = p;
1358
1359           y = build (COMPONENT_REF, TREE_TYPE (field),
1360                      root->frame_decl, field, NULL_TREE);
1361           x = build (MODIFY_EXPR, TREE_TYPE (field), y, x);
1362           append_to_statement_list (x, &stmt_list);
1363         }
1364     }
1365
1366   /* If a chain_field was created, then it needs to be initialized
1367      from chain_decl.  */
1368   if (root->chain_field)
1369     {
1370       tree x = build (COMPONENT_REF, TREE_TYPE (root->chain_field),
1371                       root->frame_decl, root->chain_field, NULL_TREE);
1372       x = build (MODIFY_EXPR, TREE_TYPE (x), x, get_chain_decl (root));
1373       append_to_statement_list (x, &stmt_list);
1374     }
1375
1376   /* If trampolines were created, then we need to initialize them.  */
1377   if (root->any_tramp_created)
1378     {
1379       struct nesting_info *i;
1380       for (i = root->inner; i ; i = i->next)
1381         {
1382           tree arg, x, field;
1383
1384           field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
1385           if (!field)
1386             continue;
1387
1388           if (DECL_NO_STATIC_CHAIN (i->context))
1389             x = null_pointer_node;
1390           else
1391             x = build_addr (root->frame_decl, context);
1392           arg = tree_cons (NULL, x, NULL);
1393
1394           x = build_addr (i->context, context);
1395           arg = tree_cons (NULL, x, arg);
1396
1397           x = build (COMPONENT_REF, TREE_TYPE (field),
1398                      root->frame_decl, field, NULL_TREE);
1399           x = build_addr (x, context);
1400           arg = tree_cons (NULL, x, arg);
1401
1402           x = implicit_built_in_decls[BUILT_IN_INIT_TRAMPOLINE];
1403           x = build_function_call_expr (x, arg);
1404
1405           append_to_statement_list (x, &stmt_list);
1406         }
1407     }
1408
1409   /* If we created initialization statements, insert them.  */
1410   if (stmt_list)
1411     {
1412       annotate_all_with_locus (&stmt_list,
1413                                DECL_SOURCE_LOCATION (context));
1414       append_to_statement_list (BIND_EXPR_BODY (DECL_SAVED_TREE (context)),
1415                                 &stmt_list);
1416       BIND_EXPR_BODY (DECL_SAVED_TREE (context)) = stmt_list;
1417     }
1418
1419   /* If a chain_decl was created, then it needs to be registered with
1420      struct function so that it gets initialized from the static chain
1421      register at the beginning of the function.  */
1422   sf = DECL_STRUCT_FUNCTION (root->context);
1423   sf->static_chain_decl = root->chain_decl;
1424
1425   /* Similarly for the non-local goto save area.  */
1426   if (root->nl_goto_field)
1427     {
1428       sf->nonlocal_goto_save_area
1429         = get_frame_field (root, context, root->nl_goto_field, NULL);
1430       sf->has_nonlocal_label = 1;
1431     }
1432
1433   /* Make sure all new local variables get inserted into the
1434      proper BIND_EXPR.  */
1435   if (root->new_local_var_chain)
1436     declare_tmp_vars (root->new_local_var_chain,
1437                       DECL_SAVED_TREE (root->context));
1438
1439   /* Dump the translated tree function.  */
1440   dump_function (TDI_nested, root->context);
1441   node = cgraph_node (root->context);
1442
1443   /* For nested functions update the cgraph to reflect unnesting.
1444      We also delay finalizing of these functions up to this point.  */
1445   if (node->origin)
1446     {
1447        cgraph_unnest_node (cgraph_node (root->context));
1448        cgraph_finalize_function (root->context, true);
1449     }
1450 }
1451
1452 static void
1453 finalize_nesting_tree (struct nesting_info *root)
1454 {
1455   do
1456     {
1457       if (root->inner)
1458         finalize_nesting_tree (root->inner);
1459       finalize_nesting_tree_1 (root);
1460       root = root->next;
1461     }
1462   while (root);
1463 }
1464
1465 /* Free the data structures allocated during this pass.  */
1466
1467 static void
1468 free_nesting_tree (struct nesting_info *root)
1469 {
1470   struct nesting_info *next;
1471   do
1472     {
1473       if (root->inner)
1474         free_nesting_tree (root->inner);
1475       htab_delete (root->var_map);
1476       next = root->next;
1477       free (root);
1478       root = next;
1479     }
1480   while (root);
1481 }
1482
1483 /* Main entry point for this pass.  Process FNDECL and all of its nested
1484    subroutines and turn them into something less tightly bound.  */
1485
1486 void
1487 lower_nested_functions (tree fndecl)
1488 {
1489   struct nesting_info *root;
1490   struct cgraph_node *cgn;
1491
1492   /* If there are no nested functions, there's nothing to do.  */
1493   cgn = cgraph_node (fndecl);
1494   if (!cgn->nested)
1495     return;
1496
1497   root = create_nesting_tree (cgn);
1498   walk_all_functions (convert_nonlocal_reference, root);
1499   walk_all_functions (convert_local_reference, root);
1500   walk_all_functions (convert_nl_goto_reference, root);
1501   walk_all_functions (convert_nl_goto_receiver, root);
1502   convert_all_function_calls (root);
1503   finalize_nesting_tree (root);
1504   free_nesting_tree (root);
1505 }
1506
1507 #include "gt-tree-nested.h"