OSDN Git Service

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