OSDN Git Service

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