OSDN Git Service

* tree-gimple.c: Rename from tree-simple.c.
[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       decl = create_tmp_var_raw (type, "CHAIN");
311       DECL_CONTEXT (decl) = info->context;
312       decl->decl.seen_in_bind_expr = 1;
313
314       /* The initialization of CHAIN is not visible to the tree-ssa
315          analyzers and optimizers.  Thus we do not want to issue
316          warnings for CHAIN.  */
317       TREE_NO_WARNING (decl) = 1;
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 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_2 (size - 1, 0));
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       if (insert == INSERT)
434         abort ();
435       return NULL;
436     }
437   elt = *slot;
438
439   if (!elt && insert == INSERT)
440     {
441       field = make_node (FIELD_DECL);
442       DECL_NAME (field) = DECL_NAME (decl);
443       TREE_TYPE (field) = get_trampoline_type ();
444       TREE_ADDRESSABLE (field) = 1;
445
446       insert_field_into_struct (get_frame_type (info), field);
447
448       elt = xmalloc (sizeof (*elt));
449       elt->old = decl;
450       elt->new = field;
451       *slot = elt;
452
453       info->any_tramp_created = true;
454     }
455   else
456     field = elt ? elt->new : NULL;
457
458   return field;
459
460
461 /* Build or return the field within the non-local frame state that holds
462    the non-local goto "jmp_buf".  The buffer itself is maintained by the
463    rtl middle-end as dynamic stack space is allocated.  */
464
465 static tree
466 get_nl_goto_field (struct nesting_info *info)
467 {
468   tree field = info->nl_goto_field;
469   if (!field)
470     {
471       unsigned size;
472       tree type;
473
474       /* For __builtin_nonlocal_goto, we need N words.  The first is the
475          frame pointer, the rest is for the target's stack pointer save
476          area.  The number of words is controled by STACK_SAVEAREA_MODE;
477          not the best interface, but it'll do for now.  */
478       if (Pmode == ptr_mode)
479         type = ptr_type_node;
480       else
481         type = lang_hooks.types.type_for_mode (Pmode, 1);
482
483       size = GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL));
484       size = size / GET_MODE_SIZE (Pmode);
485       size = size + 1;
486
487       type = build_array_type (type, build_index_type (build_int_2 (size, 0)));
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 };
522
523 /* A subroutine of walk_function.  Iterate over all sub-statements of *TP.  */
524
525 static void
526 walk_stmts (struct walk_stmt_info *wi, tree *tp)
527 {
528   tree t = *tp;
529   if (!t)
530     return;
531
532   switch (TREE_CODE (t))
533     {
534     case STATEMENT_LIST:
535       {
536         tree_stmt_iterator i;
537         for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
538           {
539             wi->tsi = i;
540             walk_stmts (wi, tsi_stmt_ptr (i));
541           }
542       }
543       break;
544
545     case COND_EXPR:
546       walk_tree (&COND_EXPR_COND (t), wi->callback, wi, NULL);
547       walk_stmts (wi, &COND_EXPR_THEN (t));
548       walk_stmts (wi, &COND_EXPR_ELSE (t));
549       break;
550     case CATCH_EXPR:
551       walk_stmts (wi, &CATCH_BODY (t));
552     case EH_FILTER_EXPR:
553       walk_stmts (wi, &EH_FILTER_FAILURE (t));
554       break;
555     case TRY_CATCH_EXPR:
556     case TRY_FINALLY_EXPR:
557       walk_stmts (wi, &TREE_OPERAND (t, 0));
558       walk_stmts (wi, &TREE_OPERAND (t, 1));
559       break;
560     case BIND_EXPR:
561       walk_stmts (wi, &BIND_EXPR_BODY (t));
562       break;
563
564     case RETURN_EXPR:
565       walk_stmts (wi, &TREE_OPERAND (t, 0));
566       break;
567
568     case MODIFY_EXPR:
569       /* The immediate arguments of a MODIFY_EXPR may use COMPONENT_REF.  */
570       wi->val_only = false;
571       walk_tree (&TREE_OPERAND (t, 0), wi->callback, wi, NULL);
572       wi->val_only = false;
573       walk_tree (&TREE_OPERAND (t, 1), wi->callback, wi, NULL);
574       wi->val_only = true;
575       break;
576
577     default:
578       wi->val_only = true;
579       walk_tree (tp, wi->callback, wi, NULL);
580       break;
581     }
582 }
583
584 /* Invoke CALLBACK on all statements of INFO->CONTEXT.  */
585
586 static void
587 walk_function (walk_tree_fn callback, struct nesting_info *info)
588 {
589   struct walk_stmt_info wi;
590
591   memset (&wi, 0, sizeof (wi));
592   wi.callback = callback;
593   wi.info = info;
594   wi.val_only = true;
595
596   walk_stmts (&wi, &DECL_SAVED_TREE (info->context));
597 }
598
599 /* Similarly for ROOT and all functions nested underneath, depth first.  */
600     
601 static void
602 walk_all_functions (walk_tree_fn callback, struct nesting_info *root)
603 {
604   do
605     {
606       if (root->inner)
607         walk_all_functions (callback, root->inner);
608       walk_function (callback, root);
609       root = root->next;
610     }
611   while (root);
612 }
613
614 \f
615 /* Construct our local datastructure describing the function nesting
616    tree rooted by CGN.  */
617
618 static struct nesting_info *
619 create_nesting_tree (struct cgraph_node *cgn)
620 {
621   struct nesting_info *info = xcalloc (1, sizeof (*info));
622   info->var_map = htab_create (7, var_map_hash, var_map_eq, free);
623   info->context = cgn->decl;
624
625   for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
626     {
627       struct nesting_info *sub = create_nesting_tree (cgn);
628       sub->outer = info;
629       sub->next = info->inner;
630       info->inner = sub;
631     }
632
633   return info;
634 }
635
636 /* Return an expression computing the static chain for TARGET_CONTEXT
637    from INFO->CONTEXT.  Insert any necessary computations before TSI.  */
638
639 static tree
640 get_static_chain (struct nesting_info *info, tree target_context,
641                   tree_stmt_iterator *tsi)
642 {
643   struct nesting_info *i;
644   tree x;
645
646   if (info->context == target_context)
647     {
648       x = build_addr (info->frame_decl);
649     }
650   else
651     {
652       x = get_chain_decl (info);
653
654       for (i = info->outer; i->context != target_context; i = i->outer)
655         {
656           tree field = get_chain_field (i);
657
658           x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
659           x = build (COMPONENT_REF, TREE_TYPE (field), x, field);
660           x = init_tmp_var (info, x, tsi);
661         }
662     }
663
664   return x;
665 }
666
667 /* Return an expression referencing FIELD from TARGET_CONTEXT's non-local
668    frame as seen from INFO->CONTEXT.  Insert any necessary computations
669    before TSI.  */
670
671 static tree
672 get_frame_field (struct nesting_info *info, tree target_context,
673                  tree field, tree_stmt_iterator *tsi)
674 {
675   struct nesting_info *i;
676   tree x;
677
678   if (info->context == target_context)
679     {
680       /* Make sure frame_decl gets created.  */
681       (void) get_frame_type (info);
682       x = info->frame_decl;
683     }
684   else
685     {
686       x = get_chain_decl (info);
687
688       for (i = info->outer; i->context != target_context; i = i->outer)
689         {
690           tree field = get_chain_field (i);
691
692           x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
693           x = build (COMPONENT_REF, TREE_TYPE (field), x, field);
694           x = init_tmp_var (info, x, tsi);
695         }
696
697       x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
698     }
699
700   x = build (COMPONENT_REF, TREE_TYPE (field), x, field);
701   return x;
702 }
703
704 /* Called via walk_function+walk_tree, rewrite all references to VAR
705    and PARM_DECLs that belong to outer functions.
706
707    The rewrite will involve some number of structure accesses back up
708    the static chain.  E.g. for a variable FOO up one nesting level it'll
709    be CHAIN->FOO.  For two levels it'll be CHAIN->__chain->FOO.  Further
710    indirections apply to decls for which use_pointer_in_frame is true.  */
711
712 static tree
713 convert_nonlocal_reference (tree *tp, int *walk_subtrees, void *data)
714 {
715   struct walk_stmt_info *wi = data;
716   struct nesting_info *info = wi->info;
717   tree t = *tp;
718
719   *walk_subtrees = 0;
720   switch (TREE_CODE (t))
721     {
722     case VAR_DECL:
723       /* Non-automatic variables are never processed.  */
724       if (TREE_STATIC (t) || DECL_EXTERNAL (t))
725         break;
726       /* FALLTHRU */
727
728     case PARM_DECL:
729       if (decl_function_context (t) != info->context)
730         {
731           tree target_context = decl_function_context (t);
732           struct nesting_info *i;
733           tree x;
734
735           for (i = info->outer; i->context != target_context; i = i->outer)
736             continue;
737           x = lookup_field_for_decl (i, t, INSERT);
738           x = get_frame_field (info, target_context, x, &wi->tsi);
739           if (use_pointer_in_frame (t))
740             {
741               x = init_tmp_var (info, x, &wi->tsi);
742               x = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (x)), x);
743             }
744           if (wi->val_only)
745             x = init_tmp_var (info, x, &wi->tsi);
746
747           *tp = x;
748         }
749       break;
750
751     case GOTO_EXPR:
752       /* Don't walk non-local gotos for now.  */
753       if (TREE_CODE (GOTO_DESTINATION (t)) != LABEL_DECL)
754         {
755           *walk_subtrees = 1;
756           wi->val_only = true;
757         }
758       break;
759
760     case LABEL_DECL:
761       /* We're taking the address of a label from a parent function, but
762          this is not itself a non-local goto.  Mark the label such that it
763          will not be deleted, much as we would with a label address in
764          static storage.  */
765       if (decl_function_context (t) != info->context)
766         FORCED_LABEL (t) = 1;
767       break;
768
769     case ADDR_EXPR:
770       {
771         bool save_val_only = wi->val_only;
772         tree save_sub = TREE_OPERAND (t, 0);
773
774         wi->val_only = false;
775         walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference, wi, NULL);
776         wi->val_only = true;
777
778         if (save_sub != TREE_OPERAND (t, 0))
779           {
780             /* If we changed anything, then TREE_INVARIANT is be wrong,
781                since we're no longer directly referencing a decl.  */
782             TREE_INVARIANT (t) = 0;
783
784             /* If the callback converted the address argument in a context
785                where we only accept variables (and min_invariant, presumably),
786                then compute the address into a temporary.  */
787             if (save_val_only)
788               *tp = gimplify_val (wi->info, t, &wi->tsi);
789           }
790       }
791       break;
792
793     case COMPONENT_REF:
794     case REALPART_EXPR:
795     case IMAGPART_EXPR:
796       wi->val_only = false;
797       walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference, wi, NULL);
798       wi->val_only = true;
799       break;
800
801     case ARRAY_REF:
802       wi->val_only = false;
803       walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference, wi, NULL);
804       wi->val_only = true;
805       walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference, wi, NULL);
806       break;
807
808     case BIT_FIELD_REF:
809       wi->val_only = false;
810       walk_tree (&TREE_OPERAND (t, 0), convert_nonlocal_reference, wi, NULL);
811       wi->val_only = true;
812       walk_tree (&TREE_OPERAND (t, 1), convert_nonlocal_reference, wi, NULL);
813       walk_tree (&TREE_OPERAND (t, 2), convert_nonlocal_reference, wi, NULL);
814       break;
815
816     default:
817       if (!DECL_P (t) && !TYPE_P (t))
818         {
819           *walk_subtrees = 1;
820           wi->val_only = true;
821         }
822       break;
823     }
824
825   return NULL_TREE;
826 }
827
828 /* Called via walk_function+walk_tree, rewrite all references to VAR
829    and PARM_DECLs that were referenced by inner nested functions.
830    The rewrite will be a structure reference to the local frame variable.  */
831
832 static tree
833 convert_local_reference (tree *tp, int *walk_subtrees, void *data)
834 {
835   struct walk_stmt_info *wi = data;
836   struct nesting_info *info = wi->info;
837   tree t = *tp, field, x, y;
838
839   switch (TREE_CODE (t))
840     {
841     case VAR_DECL:
842       /* Non-automatic variables are never processed.  */
843       if (TREE_STATIC (t) || DECL_EXTERNAL (t))
844         break;
845       /* FALLTHRU */
846
847     case PARM_DECL:
848       if (decl_function_context (t) == info->context)
849         {
850           /* If we copied a pointer to the frame, then the original decl
851              is used unchanged in the parent function.  */
852           if (use_pointer_in_frame (t))
853             break;
854
855           /* No need to transform anything if no child references the
856              variable.  */
857           field = lookup_field_for_decl (info, t, NO_INSERT);
858           if (!field)
859             break;
860
861           x = get_frame_field (info, info->context, field, &wi->tsi);
862           if (wi->val_only)
863             x = init_tmp_var (info, x, &wi->tsi);
864           *tp = x;
865         }
866       break;
867
868     case ADDR_EXPR:
869       {
870         bool save_val_only = wi->val_only;
871         tree save_sub = TREE_OPERAND (t, 0);
872
873         wi->val_only = false;
874         walk_tree (&TREE_OPERAND (t, 0), convert_local_reference, wi, NULL);
875         wi->val_only = save_val_only;
876
877         /* If we converted anything ... */
878         if (TREE_OPERAND (t, 0) != save_sub)
879           {
880             /* Then the frame decl is now addressable.  */
881             TREE_ADDRESSABLE (info->frame_decl) = 1;
882
883             /* If we are in a context where we only accept values, then
884                compute the address into a temporary.  */
885             if (save_val_only)
886               *tp = gimplify_val (wi->info, t, &wi->tsi);
887           }
888       }
889       break;
890
891     case CALL_EXPR:
892       *walk_subtrees = 1;
893
894       /* Ready for some fun?  We need to recognize
895             __builtin_stack_alloc (&x, n)
896          and insert
897             FRAME.x = &x
898          after that.  X should have use_pointer_in_frame set.  We can't
899          do this any earlier, since we can't meaningfully evaluate &x.  */
900
901       x = get_callee_fndecl (t);
902       if (!x || DECL_BUILT_IN_CLASS (x) != BUILT_IN_NORMAL)
903         break;
904       if (DECL_FUNCTION_CODE (x) != BUILT_IN_STACK_ALLOC)
905         break;
906       t = TREE_VALUE (TREE_OPERAND (t, 1));
907       if (TREE_CODE (t) != ADDR_EXPR)
908         abort ();
909       t = TREE_OPERAND (t, 0);
910       if (TREE_CODE (t) != VAR_DECL)
911         abort ();
912       field = lookup_field_for_decl (info, t, NO_INSERT);
913       if (!field)
914         break;
915       if (!use_pointer_in_frame (t))
916         abort ();
917
918       x = build_addr (t);
919       y = get_frame_field (info, info->context, field, &wi->tsi);
920       x = build (MODIFY_EXPR, void_type_node, y, x);
921       SET_EXPR_LOCUS (x, EXPR_LOCUS (tsi_stmt (wi->tsi)));
922       tsi_link_after (&wi->tsi, x, TSI_SAME_STMT);
923       break;
924
925     case COMPONENT_REF:
926     case REALPART_EXPR:
927     case IMAGPART_EXPR:
928       wi->val_only = false;
929       walk_tree (&TREE_OPERAND (t, 0), convert_local_reference, wi, NULL);
930       wi->val_only = true;
931       break;
932
933     case ARRAY_REF:
934       wi->val_only = false;
935       walk_tree (&TREE_OPERAND (t, 0), convert_local_reference, wi, NULL);
936       wi->val_only = true;
937       walk_tree (&TREE_OPERAND (t, 1), convert_local_reference, wi, NULL);
938       break;
939
940     case BIT_FIELD_REF:
941       wi->val_only = false;
942       walk_tree (&TREE_OPERAND (t, 0), convert_local_reference, wi, NULL);
943       wi->val_only = true;
944       walk_tree (&TREE_OPERAND (t, 1), convert_local_reference, wi, NULL);
945       walk_tree (&TREE_OPERAND (t, 2), convert_local_reference, wi, NULL);
946       break;
947
948     default:
949       if (!DECL_P (t) && !TYPE_P (t))
950         {
951           *walk_subtrees = 1;
952           wi->val_only = true;
953         }
954       break;
955     }
956
957   return NULL_TREE;
958 }
959
960 /* Called via walk_function+walk_tree, rewrite all GOTO_EXPRs that 
961    reference labels from outer functions.  The rewrite will be a 
962    call to __builtin_nonlocal_goto.  */
963
964 static tree
965 convert_nl_goto_reference (tree *tp, int *walk_subtrees, void *data)
966 {
967   struct walk_stmt_info *wi = data;
968   struct nesting_info *info = wi->info, *i;
969   tree t = *tp, label, new_label, target_context, x, arg, field;
970   struct var_map_elt *elt;
971   void **slot;
972
973   *walk_subtrees = 0;
974   if (TREE_CODE (t) != GOTO_EXPR)
975     return NULL_TREE;
976   label = GOTO_DESTINATION (t);
977   if (TREE_CODE (label) != LABEL_DECL)
978     return NULL_TREE;
979   target_context = decl_function_context (label);
980   if (target_context == info->context)
981     return NULL_TREE;
982
983   for (i = info->outer; target_context != i->context; i = i->outer)
984     continue;
985
986   /* The original user label may also be use for a normal goto, therefore
987      we must create a new label that will actually receive the abnormal
988      control transfer.  This new label will be marked LABEL_NONLOCAL; this
989      mark will trigger proper behaviour in the cfg, as well as cause the
990      (hairy target-specific) non-local goto receiver code to be generated
991      when we expand rtl.  */
992   new_label = create_artificial_label ();
993   DECL_NONLOCAL (new_label) = 1;
994
995   /* Enter this association into var_map so that we can insert the new
996      label into the IL during a second pass.  */
997   elt = xmalloc (sizeof (*elt));
998   elt->old = label;
999   elt->new = new_label;
1000   slot = htab_find_slot (i->var_map, elt, INSERT);
1001   *slot = elt;
1002   
1003   /* Build: __builtin_nl_goto(new_label, &chain->nl_goto_field).  */
1004   field = get_nl_goto_field (i);
1005   x = get_frame_field (info, target_context, field, &wi->tsi);
1006   x = build_addr (x);
1007   x = gimplify_val (info, x, &wi->tsi);
1008   arg = tree_cons (NULL, x, NULL);
1009   x = build_addr (new_label);
1010   arg = tree_cons (NULL, x, arg);
1011   x = implicit_built_in_decls[BUILT_IN_NONLOCAL_GOTO];
1012   x = build_function_call_expr (x, arg);
1013
1014   SET_EXPR_LOCUS (x, EXPR_LOCUS (tsi_stmt (wi->tsi)));
1015   *tsi_stmt_ptr (wi->tsi) = x;
1016
1017   return NULL_TREE;
1018 }
1019
1020 /* Called via walk_function+walk_tree, rewrite all LABEL_EXPRs that 
1021    are referenced via nonlocal goto from a nested function.  The rewrite
1022    will involve installing a newly generated DECL_NONLOCAL label, and
1023    (potentially) a branch around the rtl gunk that is assumed to be 
1024    attached to such a label.  */
1025
1026 static tree
1027 convert_nl_goto_receiver (tree *tp, int *walk_subtrees, void *data)
1028 {
1029   struct walk_stmt_info *wi = data;
1030   struct nesting_info *info = wi->info;
1031   tree t = *tp, label, new_label, x;
1032   struct var_map_elt *elt, dummy;
1033   tree_stmt_iterator tmp_tsi;
1034
1035   *walk_subtrees = 0;
1036   if (TREE_CODE (t) != LABEL_EXPR)
1037     return NULL_TREE;
1038   label = LABEL_EXPR_LABEL (t);
1039
1040   dummy.old = label;
1041   elt = htab_find (info->var_map, &dummy);
1042   if (!elt)
1043     return NULL_TREE;
1044   new_label = elt->new;
1045
1046   /* If there's any possibility that the previous statement falls through,
1047      then we must branch around the new non-local label.  */
1048   tmp_tsi = wi->tsi;
1049   tsi_prev (&tmp_tsi);
1050   if (tsi_end_p (tmp_tsi) || block_may_fallthru (tsi_stmt (tmp_tsi)))
1051     {
1052       x = build1 (GOTO_EXPR, void_type_node, label);
1053       tsi_link_before (&wi->tsi, x, TSI_SAME_STMT);
1054     }
1055   x = build1 (LABEL_EXPR, void_type_node, new_label);
1056   tsi_link_before (&wi->tsi, x, TSI_SAME_STMT);
1057
1058   return NULL_TREE;
1059 }
1060
1061 /* Called via walk_function+walk_tree, rewrite all references to addresses
1062    of nested functions that require the use of trampolines.  The rewrite
1063    will involve a reference a trampoline generated for the occasion.  */
1064
1065 static tree
1066 convert_tramp_reference (tree *tp, int *walk_subtrees, void *data)
1067 {
1068   struct walk_stmt_info *wi = data;
1069   struct nesting_info *info = wi->info, *i;
1070   tree t = *tp, decl, target_context, x, arg;
1071
1072   *walk_subtrees = 0;
1073   switch (TREE_CODE (t))
1074     {
1075     case ADDR_EXPR:
1076       /* Build
1077            T.1 = &CHAIN->tramp;
1078            T.2 = __builtin_adjust_trampoline (T.1);
1079            T.3 = (func_type)T.2;
1080       */
1081
1082       decl = TREE_OPERAND (t, 0);
1083       if (TREE_CODE (decl) != FUNCTION_DECL)
1084         break;
1085
1086       /* Only need to process nested functions.  */
1087       target_context = decl_function_context (decl);
1088       if (!target_context)
1089         break;
1090
1091       /* If the nested function doesn't use a static chain, then
1092          it doesn't need a trampoline.  */
1093       if (DECL_NO_STATIC_CHAIN (decl))
1094         break;
1095
1096       /* Lookup the immediate parent of the callee, as that's where
1097          we need to insert the trampoline.  */
1098       for (i = info; i->context != target_context; i = i->outer)
1099         continue;
1100       x = lookup_tramp_for_decl (i, decl, INSERT);
1101
1102       /* Compute the address of the field holding the trampoline.  */
1103       x = get_frame_field (info, target_context, x, &wi->tsi);
1104       x = build_addr (x);
1105       x = gimplify_val (info, x, &wi->tsi);
1106       arg = tree_cons (NULL, x, NULL);
1107
1108       /* Do machine-specific ugliness.  Normally this will involve
1109          computing extra alignment, but it can really be anything.  */
1110       x = implicit_built_in_decls[BUILT_IN_ADJUST_TRAMPOLINE];
1111       x = build_function_call_expr (x, arg);
1112       x = init_tmp_var (info, x, &wi->tsi);
1113
1114       /* Cast back to the proper function type.  */
1115       x = build1 (NOP_EXPR, TREE_TYPE (t), x);
1116       x = init_tmp_var (info, x, &wi->tsi);
1117
1118       *tp = x;
1119       break;
1120
1121     case CALL_EXPR:
1122       /* Only walk call arguments, lest we generate trampolines for
1123          direct calls.  */
1124       walk_tree (&TREE_OPERAND (t, 1), convert_tramp_reference, wi, NULL);
1125       break;
1126
1127     default:
1128       if (!DECL_P (t) && !TYPE_P (t))
1129         *walk_subtrees = 1;
1130       break;
1131     }
1132
1133   return NULL_TREE;
1134 }
1135
1136 /* Called via walk_function+walk_tree, rewrite all CALL_EXPRs that 
1137    reference nested functions to make sure that the static chain is
1138    set up properly for the call.  */
1139
1140 static tree
1141 convert_call_expr (tree *tp, int *walk_subtrees, void *data)
1142 {
1143   struct walk_stmt_info *wi = data;
1144   struct nesting_info *info = wi->info;
1145   tree t = *tp, decl, target_context;
1146
1147   *walk_subtrees = 0;
1148   switch (TREE_CODE (t))
1149     {
1150     case CALL_EXPR:
1151       decl = get_callee_fndecl (t);
1152       if (!decl)
1153         break;
1154       target_context = decl_function_context (decl);
1155       if (target_context && !DECL_NO_STATIC_CHAIN (decl))
1156         TREE_OPERAND (t, 2)
1157           = get_static_chain (info, target_context, &wi->tsi);
1158       break;
1159
1160     case RETURN_EXPR:
1161     case MODIFY_EXPR:
1162       /* Only return and modify may contain calls.  */
1163       *walk_subtrees = 1;
1164       break;
1165
1166     default:
1167       break;
1168     }
1169
1170   return NULL_TREE;
1171 }
1172
1173 /* Walk the nesting tree starting with ROOT, depth first.  Convert all
1174    trampolines and call expressions.  On the way back up, determine if
1175    a nested function actually uses its static chain; if not, remember that.  */
1176
1177 static void
1178 convert_all_function_calls (struct nesting_info *root)
1179 {
1180   do
1181     {
1182       if (root->inner)
1183         convert_all_function_calls (root->inner);
1184
1185       walk_function (convert_tramp_reference, root);
1186       walk_function (convert_call_expr, root);
1187
1188       /* If the function does not use a static chain, then remember that.  */
1189       if (root->outer && !root->chain_decl && !root->chain_field)
1190         DECL_NO_STATIC_CHAIN (root->context) = 1;
1191       else
1192         {
1193 #ifdef ENABLE_CHECKING
1194           if (DECL_NO_STATIC_CHAIN (root->context))
1195             abort ();
1196 #endif
1197         }
1198
1199       root = root->next;
1200     }
1201   while (root);
1202 }
1203
1204 /* Do "everything else" to clean up or complete state collected by the
1205    various walking passes -- lay out the types and decls, generate code
1206    to initialize the frame decl, store critical expressions in the
1207    struct function for rtl to find.  */
1208
1209 static void
1210 finalize_nesting_tree_1 (struct nesting_info *root)
1211 {
1212   tree stmt_list = NULL;
1213   tree context = root->context;
1214   struct function *sf;
1215
1216   /* If we created a non-local frame type or decl, we need to lay them
1217      out at this time.  */
1218   if (root->frame_type)
1219     {
1220       layout_type (root->frame_type);
1221       layout_decl (root->frame_decl, 0);
1222     }
1223
1224   /* If any parameters were referenced non-locally, then we need to 
1225      insert a copy.  Likewise, if any variables were referenced by
1226      pointer, we need to initialize the address.  */
1227   if (root->any_parm_remapped)
1228     {
1229       tree p;
1230       for (p = DECL_ARGUMENTS (context); p ; p = TREE_CHAIN (p))
1231         {
1232           tree field, x, y;
1233
1234           field = lookup_field_for_decl (root, p, NO_INSERT);
1235           if (!field)
1236             continue;
1237
1238           if (use_pointer_in_frame (p))
1239             x = build_addr (p);
1240           else
1241             x = p;
1242
1243           y = build (COMPONENT_REF, TREE_TYPE (field),
1244                      root->frame_decl, field);
1245           x = build (MODIFY_EXPR, TREE_TYPE (field), y, x);
1246           append_to_statement_list (x, &stmt_list);
1247         }
1248     }
1249
1250   /* If a chain_field was created, then it needs to be initialized
1251      from chain_decl.  */
1252   if (root->chain_field)
1253     {
1254       tree x;
1255       x = build (COMPONENT_REF, TREE_TYPE (root->chain_field),
1256                  root->frame_decl, root->chain_field);
1257       x = build (MODIFY_EXPR, TREE_TYPE (x), x, get_chain_decl (root));
1258       append_to_statement_list (x, &stmt_list);
1259     }
1260
1261   /* If trampolines were created, then we need to initialize them.  */
1262   if (root->any_tramp_created)
1263     {
1264       struct nesting_info *i;
1265       for (i = root->inner; i ; i = i->next)
1266         {
1267           tree arg, x, field;
1268
1269           field = lookup_tramp_for_decl (root, i->context, NO_INSERT);
1270           if (!field)
1271             continue;
1272
1273           if (DECL_NO_STATIC_CHAIN (i->context))
1274             x = null_pointer_node;
1275           else
1276             x = build_addr (root->frame_decl);
1277           arg = tree_cons (NULL, x, NULL);
1278
1279           x = build_addr (i->context);
1280           arg = tree_cons (NULL, x, arg);
1281
1282           x = build (COMPONENT_REF, TREE_TYPE (field),
1283                      root->frame_decl, field);
1284           x = build_addr (x);
1285           arg = tree_cons (NULL, x, arg);
1286
1287           x = implicit_built_in_decls[BUILT_IN_INIT_TRAMPOLINE];
1288           x = build_function_call_expr (x, arg);
1289
1290           append_to_statement_list (x, &stmt_list);
1291         }
1292     }
1293
1294   /* If we created initialization statements, insert them.  */
1295   if (stmt_list)
1296     {
1297       annotate_all_with_locus (&stmt_list,
1298                                DECL_SOURCE_LOCATION (context));
1299       append_to_statement_list (BIND_EXPR_BODY (DECL_SAVED_TREE (context)),
1300                                 &stmt_list);
1301       BIND_EXPR_BODY (DECL_SAVED_TREE (context)) = stmt_list;
1302     }
1303
1304   /* If a chain_decl was created, then it needs to be registered with
1305      struct function so that it gets initialized from the static chain
1306      register at the beginning of the function.  */
1307   sf = DECL_STRUCT_FUNCTION (root->context);
1308   sf->static_chain_decl = root->chain_decl;
1309
1310   /* Similarly for the non-local goto save area.  */
1311   if (root->nl_goto_field)
1312     {
1313       sf->nonlocal_goto_save_area
1314         = get_frame_field (root, context, root->nl_goto_field, NULL);
1315       sf->has_nonlocal_label = 1;
1316     }
1317
1318   /* Make sure all new local variables get insertted into the
1319      proper BIND_EXPR.  */
1320   if (root->new_local_var_chain)
1321     declare_tmp_vars (root->new_local_var_chain,
1322                       DECL_SAVED_TREE (root->context));
1323
1324   /* Dump the translated tree function.  */
1325   dump_function (TDI_nested, root->context);
1326 }
1327
1328 static void
1329 finalize_nesting_tree (struct nesting_info *root)
1330 {
1331   do
1332     {
1333       if (root->inner)
1334         finalize_nesting_tree (root->inner);
1335       finalize_nesting_tree_1 (root);
1336       root = root->next;
1337     }
1338   while (root);
1339 }
1340
1341 /* Free the data structures allocated during this pass.  */
1342
1343 static void
1344 free_nesting_tree (struct nesting_info *root)
1345 {
1346   struct nesting_info *next;
1347   do
1348     {
1349       if (root->inner)
1350         free_nesting_tree (root->inner);
1351       htab_delete (root->var_map);
1352       next = root->next;
1353       free (root);
1354       root = next;
1355     }
1356   while (root);
1357 }
1358
1359 /* Main entry point for this pass.  Process FNDECL and all of its nested
1360    subroutines and turn them into something less tightly bound.  */
1361
1362 void
1363 lower_nested_functions (tree fndecl)
1364 {
1365   struct nesting_info *root;
1366   struct cgraph_node *cgn;
1367
1368   /* If there are no nested functions, there's nothing to do.  */
1369   cgn = cgraph_node (fndecl);
1370   if (!cgn->nested)
1371     return;
1372
1373   root = create_nesting_tree (cgn);
1374   walk_all_functions (convert_nonlocal_reference, root);
1375   walk_all_functions (convert_local_reference, root);
1376   walk_all_functions (convert_nl_goto_reference, root);
1377   walk_all_functions (convert_nl_goto_receiver, root);
1378   convert_all_function_calls (root);
1379   finalize_nesting_tree (root);
1380   free_nesting_tree (root);
1381 }
1382
1383 #include "gt-tree-nested.h"