OSDN Git Service

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