OSDN Git Service

PR fortran/36260
[pf3gnuchains/gcc-fork.git] / gcc / except.c
1 /* Implements exception handling.
2    Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4    Free Software Foundation, Inc.
5    Contributed by Mike Stump <mrs@cygnus.com>.
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
22
23
24 /* An exception is an event that can be signaled from within a
25    function. This event can then be "caught" or "trapped" by the
26    callers of this function. This potentially allows program flow to
27    be transferred to any arbitrary code associated with a function call
28    several levels up the stack.
29
30    The intended use for this mechanism is for signaling "exceptional
31    events" in an out-of-band fashion, hence its name. The C++ language
32    (and many other OO-styled or functional languages) practically
33    requires such a mechanism, as otherwise it becomes very difficult
34    or even impossible to signal failure conditions in complex
35    situations.  The traditional C++ example is when an error occurs in
36    the process of constructing an object; without such a mechanism, it
37    is impossible to signal that the error occurs without adding global
38    state variables and error checks around every object construction.
39
40    The act of causing this event to occur is referred to as "throwing
41    an exception". (Alternate terms include "raising an exception" or
42    "signaling an exception".) The term "throw" is used because control
43    is returned to the callers of the function that is signaling the
44    exception, and thus there is the concept of "throwing" the
45    exception up the call stack.
46
47    [ Add updated documentation on how to use this.  ]  */
48
49
50 #include "config.h"
51 #include "system.h"
52 #include "coretypes.h"
53 #include "tm.h"
54 #include "rtl.h"
55 #include "tree.h"
56 #include "flags.h"
57 #include "function.h"
58 #include "expr.h"
59 #include "libfuncs.h"
60 #include "insn-config.h"
61 #include "except.h"
62 #include "integrate.h"
63 #include "hard-reg-set.h"
64 #include "basic-block.h"
65 #include "output.h"
66 #include "dwarf2asm.h"
67 #include "dwarf2out.h"
68 #include "dwarf2.h"
69 #include "toplev.h"
70 #include "hashtab.h"
71 #include "intl.h"
72 #include "ggc.h"
73 #include "tm_p.h"
74 #include "target.h"
75 #include "langhooks.h"
76 #include "cgraph.h"
77 #include "diagnostic.h"
78 #include "tree-pass.h"
79 #include "timevar.h"
80 #include "tree-flow.h"
81
82 /* Provide defaults for stuff that may not be defined when using
83    sjlj exceptions.  */
84 #ifndef EH_RETURN_DATA_REGNO
85 #define EH_RETURN_DATA_REGNO(N) INVALID_REGNUM
86 #endif
87
88 /* Protect cleanup actions with must-not-throw regions, with a call
89    to the given failure handler.  */
90 gimple (*lang_protect_cleanup_actions) (void);
91
92 /* Return true if type A catches type B.  */
93 int (*lang_eh_type_covers) (tree a, tree b);
94
95 /* Map a type to a runtime object to match type.  */
96 tree (*lang_eh_runtime_type) (tree);
97
98 /* A hash table of label to region number.  */
99
100 struct GTY(()) ehl_map_entry {
101   rtx label;
102   struct eh_region_d *region;
103 };
104
105 static GTY(()) int call_site_base;
106 static GTY ((param_is (union tree_node)))
107   htab_t type_to_runtime_map;
108
109 /* Describe the SjLj_Function_Context structure.  */
110 static GTY(()) tree sjlj_fc_type_node;
111 static int sjlj_fc_call_site_ofs;
112 static int sjlj_fc_data_ofs;
113 static int sjlj_fc_personality_ofs;
114 static int sjlj_fc_lsda_ofs;
115 static int sjlj_fc_jbuf_ofs;
116 \f
117
118 struct GTY(()) call_site_record_d
119 {
120   rtx landing_pad;
121   int action;
122 };
123 \f
124 static int t2r_eq (const void *, const void *);
125 static hashval_t t2r_hash (const void *);
126
127 static int ttypes_filter_eq (const void *, const void *);
128 static hashval_t ttypes_filter_hash (const void *);
129 static int ehspec_filter_eq (const void *, const void *);
130 static hashval_t ehspec_filter_hash (const void *);
131 static int add_ttypes_entry (htab_t, tree);
132 static int add_ehspec_entry (htab_t, htab_t, tree);
133 static void assign_filter_values (void);
134 static void build_post_landing_pads (void);
135 static void connect_post_landing_pads (void);
136 static void dw2_build_landing_pads (void);
137
138 struct sjlj_lp_info;
139 static bool sjlj_find_directly_reachable_regions (struct sjlj_lp_info *);
140 static void sjlj_assign_call_site_values (rtx, struct sjlj_lp_info *);
141 static void sjlj_mark_call_sites (struct sjlj_lp_info *);
142 static void sjlj_emit_function_enter (rtx);
143 static void sjlj_emit_function_exit (void);
144 static void sjlj_emit_dispatch_table (rtx, struct sjlj_lp_info *);
145 static void sjlj_build_landing_pads (void);
146
147 static void remove_eh_handler (struct eh_region_d *);
148 static void remove_eh_handler_and_replace (struct eh_region_d *,
149                                            struct eh_region_d *, bool);
150
151 /* The return value of reachable_next_level.  */
152 enum reachable_code
153 {
154   /* The given exception is not processed by the given region.  */
155   RNL_NOT_CAUGHT,
156   /* The given exception may need processing by the given region.  */
157   RNL_MAYBE_CAUGHT,
158   /* The given exception is completely processed by the given region.  */
159   RNL_CAUGHT,
160   /* The given exception is completely processed by the runtime.  */
161   RNL_BLOCKED
162 };
163
164 struct reachable_info;
165 static enum reachable_code reachable_next_level (struct eh_region_d *, tree,
166                                                  struct reachable_info *, bool);
167
168 static int action_record_eq (const void *, const void *);
169 static hashval_t action_record_hash (const void *);
170 static int add_action_record (htab_t, int, int);
171 static int collect_one_action_chain (htab_t, struct eh_region_d *);
172 static int add_call_site (rtx, int);
173
174 static void push_uleb128 (varray_type *, unsigned int);
175 static void push_sleb128 (varray_type *, int);
176 #ifndef HAVE_AS_LEB128
177 static int dw2_size_of_call_site_table (void);
178 static int sjlj_size_of_call_site_table (void);
179 #endif
180 static void dw2_output_call_site_table (void);
181 static void sjlj_output_call_site_table (void);
182
183 \f
184 /* Routine to see if exception handling is turned on.
185    DO_WARN is nonzero if we want to inform the user that exception
186    handling is turned off.
187
188    This is used to ensure that -fexceptions has been specified if the
189    compiler tries to use any exception-specific functions.  */
190
191 int
192 doing_eh (int do_warn)
193 {
194   if (! flag_exceptions)
195     {
196       static int warned = 0;
197       if (! warned && do_warn)
198         {
199           error ("exception handling disabled, use -fexceptions to enable");
200           warned = 1;
201         }
202       return 0;
203     }
204   return 1;
205 }
206
207 \f
208 void
209 init_eh (void)
210 {
211   if (! flag_exceptions)
212     return;
213
214   type_to_runtime_map = htab_create_ggc (31, t2r_hash, t2r_eq, NULL);
215
216   /* Create the SjLj_Function_Context structure.  This should match
217      the definition in unwind-sjlj.c.  */
218   if (USING_SJLJ_EXCEPTIONS)
219     {
220       tree f_jbuf, f_per, f_lsda, f_prev, f_cs, f_data, tmp;
221
222       sjlj_fc_type_node = lang_hooks.types.make_type (RECORD_TYPE);
223
224       f_prev = build_decl (FIELD_DECL, get_identifier ("__prev"),
225                            build_pointer_type (sjlj_fc_type_node));
226       DECL_FIELD_CONTEXT (f_prev) = sjlj_fc_type_node;
227
228       f_cs = build_decl (FIELD_DECL, get_identifier ("__call_site"),
229                          integer_type_node);
230       DECL_FIELD_CONTEXT (f_cs) = sjlj_fc_type_node;
231
232       tmp = build_index_type (build_int_cst (NULL_TREE, 4 - 1));
233       tmp = build_array_type (lang_hooks.types.type_for_mode
234                                 (targetm.unwind_word_mode (), 1),
235                               tmp);
236       f_data = build_decl (FIELD_DECL, get_identifier ("__data"), tmp);
237       DECL_FIELD_CONTEXT (f_data) = sjlj_fc_type_node;
238
239       f_per = build_decl (FIELD_DECL, get_identifier ("__personality"),
240                           ptr_type_node);
241       DECL_FIELD_CONTEXT (f_per) = sjlj_fc_type_node;
242
243       f_lsda = build_decl (FIELD_DECL, get_identifier ("__lsda"),
244                            ptr_type_node);
245       DECL_FIELD_CONTEXT (f_lsda) = sjlj_fc_type_node;
246
247 #ifdef DONT_USE_BUILTIN_SETJMP
248 #ifdef JMP_BUF_SIZE
249       tmp = build_int_cst (NULL_TREE, JMP_BUF_SIZE - 1);
250 #else
251       /* Should be large enough for most systems, if it is not,
252          JMP_BUF_SIZE should be defined with the proper value.  It will
253          also tend to be larger than necessary for most systems, a more
254          optimal port will define JMP_BUF_SIZE.  */
255       tmp = build_int_cst (NULL_TREE, FIRST_PSEUDO_REGISTER + 2 - 1);
256 #endif
257 #else
258       /* builtin_setjmp takes a pointer to 5 words.  */
259       tmp = build_int_cst (NULL_TREE, 5 * BITS_PER_WORD / POINTER_SIZE - 1);
260 #endif
261       tmp = build_index_type (tmp);
262       tmp = build_array_type (ptr_type_node, tmp);
263       f_jbuf = build_decl (FIELD_DECL, get_identifier ("__jbuf"), tmp);
264 #ifdef DONT_USE_BUILTIN_SETJMP
265       /* We don't know what the alignment requirements of the
266          runtime's jmp_buf has.  Overestimate.  */
267       DECL_ALIGN (f_jbuf) = BIGGEST_ALIGNMENT;
268       DECL_USER_ALIGN (f_jbuf) = 1;
269 #endif
270       DECL_FIELD_CONTEXT (f_jbuf) = sjlj_fc_type_node;
271
272       TYPE_FIELDS (sjlj_fc_type_node) = f_prev;
273       TREE_CHAIN (f_prev) = f_cs;
274       TREE_CHAIN (f_cs) = f_data;
275       TREE_CHAIN (f_data) = f_per;
276       TREE_CHAIN (f_per) = f_lsda;
277       TREE_CHAIN (f_lsda) = f_jbuf;
278
279       layout_type (sjlj_fc_type_node);
280
281       /* Cache the interesting field offsets so that we have
282          easy access from rtl.  */
283       sjlj_fc_call_site_ofs
284         = (tree_low_cst (DECL_FIELD_OFFSET (f_cs), 1)
285            + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_cs), 1) / BITS_PER_UNIT);
286       sjlj_fc_data_ofs
287         = (tree_low_cst (DECL_FIELD_OFFSET (f_data), 1)
288            + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_data), 1) / BITS_PER_UNIT);
289       sjlj_fc_personality_ofs
290         = (tree_low_cst (DECL_FIELD_OFFSET (f_per), 1)
291            + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_per), 1) / BITS_PER_UNIT);
292       sjlj_fc_lsda_ofs
293         = (tree_low_cst (DECL_FIELD_OFFSET (f_lsda), 1)
294            + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_lsda), 1) / BITS_PER_UNIT);
295       sjlj_fc_jbuf_ofs
296         = (tree_low_cst (DECL_FIELD_OFFSET (f_jbuf), 1)
297            + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_jbuf), 1) / BITS_PER_UNIT);
298     }
299 }
300
301 void
302 init_eh_for_function (void)
303 {
304   cfun->eh = GGC_CNEW (struct eh_status);
305 }
306 \f
307 /* Routines to generate the exception tree somewhat directly.
308    These are used from tree-eh.c when processing exception related
309    nodes during tree optimization.  */
310
311 static struct eh_region_d *
312 gen_eh_region (enum eh_region_type type, struct eh_region_d *outer)
313 {
314   struct eh_region_d *new_eh;
315
316 #ifdef ENABLE_CHECKING
317   gcc_assert (doing_eh (0));
318 #endif
319
320   /* Insert a new blank region as a leaf in the tree.  */
321   new_eh = GGC_CNEW (struct eh_region_d);
322   new_eh->type = type;
323   new_eh->outer = outer;
324   if (outer)
325     {
326       new_eh->next_peer = outer->inner;
327       outer->inner = new_eh;
328     }
329   else
330     {
331       new_eh->next_peer = cfun->eh->region_tree;
332       cfun->eh->region_tree = new_eh;
333     }
334
335   new_eh->region_number = ++cfun->eh->last_region_number;
336
337   return new_eh;
338 }
339
340 struct eh_region_d *
341 gen_eh_region_cleanup (struct eh_region_d *outer)
342 {
343   struct eh_region_d *cleanup = gen_eh_region (ERT_CLEANUP, outer);
344   return cleanup;
345 }
346
347 struct eh_region_d *
348 gen_eh_region_try (struct eh_region_d *outer)
349 {
350   return gen_eh_region (ERT_TRY, outer);
351 }
352
353 struct eh_region_d *
354 gen_eh_region_catch (struct eh_region_d *t, tree type_or_list)
355 {
356   struct eh_region_d *c, *l;
357   tree type_list, type_node;
358
359   /* Ensure to always end up with a type list to normalize further
360      processing, then register each type against the runtime types map.  */
361   type_list = type_or_list;
362   if (type_or_list)
363     {
364       if (TREE_CODE (type_or_list) != TREE_LIST)
365         type_list = tree_cons (NULL_TREE, type_or_list, NULL_TREE);
366
367       type_node = type_list;
368       for (; type_node; type_node = TREE_CHAIN (type_node))
369         add_type_for_runtime (TREE_VALUE (type_node));
370     }
371
372   c = gen_eh_region (ERT_CATCH, t->outer);
373   c->u.eh_catch.type_list = type_list;
374   l = t->u.eh_try.last_catch;
375   c->u.eh_catch.prev_catch = l;
376   if (l)
377     l->u.eh_catch.next_catch = c;
378   else
379     t->u.eh_try.eh_catch = c;
380   t->u.eh_try.last_catch = c;
381
382   return c;
383 }
384
385 struct eh_region_d *
386 gen_eh_region_allowed (struct eh_region_d *outer, tree allowed)
387 {
388   struct eh_region_d *region = gen_eh_region (ERT_ALLOWED_EXCEPTIONS, outer);
389   region->u.allowed.type_list = allowed;
390
391   for (; allowed ; allowed = TREE_CHAIN (allowed))
392     add_type_for_runtime (TREE_VALUE (allowed));
393
394   return region;
395 }
396
397 struct eh_region_d *
398 gen_eh_region_must_not_throw (struct eh_region_d *outer)
399 {
400   return gen_eh_region (ERT_MUST_NOT_THROW, outer);
401 }
402
403 int
404 get_eh_region_number (struct eh_region_d *region)
405 {
406   return region->region_number;
407 }
408
409 bool
410 get_eh_region_may_contain_throw (struct eh_region_d *region)
411 {
412   return region->may_contain_throw;
413 }
414
415 tree
416 get_eh_region_tree_label (struct eh_region_d *region)
417 {
418   return region->tree_label;
419 }
420
421 tree
422 get_eh_region_no_tree_label (int region)
423 {
424   return VEC_index (eh_region, cfun->eh->region_array, region)->tree_label;
425 }
426
427 void
428 set_eh_region_tree_label (struct eh_region_d *region, tree lab)
429 {
430   region->tree_label = lab;
431 }
432 \f
433 void
434 expand_resx_expr (tree exp)
435 {
436   int region_nr = TREE_INT_CST_LOW (TREE_OPERAND (exp, 0));
437   struct eh_region_d *reg = VEC_index (eh_region,
438                                        cfun->eh->region_array, region_nr);
439
440   gcc_assert (!reg->resume);
441   do_pending_stack_adjust ();
442   reg->resume = emit_jump_insn (gen_rtx_RESX (VOIDmode, region_nr));
443   emit_barrier ();
444 }
445
446 /* Note that the current EH region (if any) may contain a throw, or a
447    call to a function which itself may contain a throw.  */
448
449 void
450 note_eh_region_may_contain_throw (struct eh_region_d *region)
451 {
452   while (region && !region->may_contain_throw)
453     {
454       region->may_contain_throw = 1;
455       region = region->outer;
456     }
457 }
458
459
460 /* Return an rtl expression for a pointer to the exception object
461    within a handler.  */
462
463 rtx
464 get_exception_pointer (void)
465 {
466   if (! crtl->eh.exc_ptr)
467     crtl->eh.exc_ptr = gen_reg_rtx (ptr_mode);
468   return crtl->eh.exc_ptr;
469 }
470
471 /* Return an rtl expression for the exception dispatch filter
472    within a handler.  */
473
474 rtx
475 get_exception_filter (void)
476 {
477   if (! crtl->eh.filter)
478     crtl->eh.filter = gen_reg_rtx (targetm.eh_return_filter_mode ());
479   return crtl->eh.filter;
480 }
481 \f
482 /* This section is for the exception handling specific optimization pass.  */
483
484 /* Random access the exception region tree.  */
485
486 void
487 collect_eh_region_array (void)
488 {
489   struct eh_region_d *i;
490
491   i = cfun->eh->region_tree;
492   if (! i)
493     return;
494
495   VEC_safe_grow (eh_region, gc, cfun->eh->region_array,
496                  cfun->eh->last_region_number + 1);
497   VEC_replace (eh_region, cfun->eh->region_array, 0, 0);
498
499   while (1)
500     {
501       VEC_replace (eh_region, cfun->eh->region_array, i->region_number, i);
502
503       /* If there are sub-regions, process them.  */
504       if (i->inner)
505         i = i->inner;
506       /* If there are peers, process them.  */
507       else if (i->next_peer)
508         i = i->next_peer;
509       /* Otherwise, step back up the tree to the next peer.  */
510       else
511         {
512           do {
513             i = i->outer;
514             if (i == NULL)
515               return;
516           } while (i->next_peer == NULL);
517           i = i->next_peer;
518         }
519     }
520 }
521
522 /* R is MUST_NOT_THROW region that is not reachable via local
523    RESX instructions.  It still must be kept in the tree in case runtime
524    can unwind through it, or we will eliminate out terminate call
525    runtime would do otherwise.  Return TRUE if R contains throwing statements
526    or some of the exceptions in inner regions can be unwound up to R. 
527    
528    CONTAINS_STMT is bitmap of all regions that contains some throwing
529    statements.  
530    
531    Function looks O(^3) at first sight.  In fact the function is called at most
532    once for every MUST_NOT_THROW in EH tree from remove_unreachable_regions
533    Because the outer loop walking subregions does not dive in MUST_NOT_THROW,
534    the outer loop examines every region at most once.  The inner loop
535    is doing unwinding from the throwing statement same way as we do during
536    CFG construction, so it is O(^2) in size of EH tree, but O(n) in size
537    of CFG.  In practice Eh trees are wide, not deep, so this is not
538    a problem.  */
539
540 static bool
541 can_be_reached_by_runtime (sbitmap contains_stmt, struct eh_region_d *r)
542 {
543   struct eh_region_d *i = r->inner;
544   unsigned n;
545   bitmap_iterator bi;
546
547   if (TEST_BIT (contains_stmt, r->region_number))
548     return true;
549   if (r->aka)
550     EXECUTE_IF_SET_IN_BITMAP (r->aka, 0, n, bi)
551       if (TEST_BIT (contains_stmt, n))
552       return true;
553   if (!i)
554     return false;
555   while (1)
556     {
557       /* It is pointless to look into MUST_NOT_THROW
558          or dive into subregions.  They never unwind up.  */
559       if (i->type != ERT_MUST_NOT_THROW)
560         {
561           bool found = TEST_BIT (contains_stmt, i->region_number);
562           if (!found)
563             EXECUTE_IF_SET_IN_BITMAP (i->aka, 0, n, bi)
564               if (TEST_BIT (contains_stmt, n))
565               {
566                 found = true;
567                 break;
568               }
569           /* We have nested region that contains throwing statement.
570              See if resuming might lead up to the resx or we get locally
571              caught sooner.  If we get locally caught sooner, we either
572              know region R is not reachable or it would have direct edge
573              from the EH resx and thus consider region reachable at
574              firest place.  */
575           if (found)
576             {
577               struct eh_region_d *i1 = i;
578               tree type_thrown = NULL_TREE;
579
580               if (i1->type == ERT_THROW)
581                 {
582                   type_thrown = i1->u.eh_throw.type;
583                   i1 = i1->outer;
584                 }
585               for (; i1 != r; i1 = i1->outer)
586                 if (reachable_next_level (i1, type_thrown, NULL,
587                                           false) >= RNL_CAUGHT)
588                   break;
589               if (i1 == r)
590                 return true;
591             }
592         }
593       /* If there are sub-regions, process them.  */
594       if (i->type != ERT_MUST_NOT_THROW && i->inner)
595         i = i->inner;
596       /* If there are peers, process them.  */
597       else if (i->next_peer)
598         i = i->next_peer;
599       /* Otherwise, step back up the tree to the next peer.  */
600       else
601         {
602           do
603             {
604               i = i->outer;
605               if (i == r)
606                 return false;
607             }
608           while (i->next_peer == NULL);
609           i = i->next_peer;
610         }
611     }
612 }
613
614 /* Bring region R to the root of tree.  */
615
616 static void
617 bring_to_root (struct eh_region_d *r)
618 {
619   struct eh_region_d **pp;
620   struct eh_region_d *outer = r->outer;
621   if (!r->outer)
622     return;
623   for (pp = &outer->inner; *pp != r; pp = &(*pp)->next_peer)
624     continue;
625   *pp = r->next_peer;
626   r->outer = NULL;
627   r->next_peer = cfun->eh->region_tree;
628   cfun->eh->region_tree = r;
629 }
630
631 /* Return true if region R2 can be replaced by R1.  */
632
633 static bool
634 eh_region_replaceable_by_p (const struct eh_region_d *r1,
635                             const struct eh_region_d *r2)
636 {
637   /* Regions are semantically same if they are of same type,
638      have same label and type.  */
639   if (r1->type != r2->type)
640     return false;
641   if (r1->tree_label != r2->tree_label)
642     return false;
643
644   /* Verify that also region type dependent data are the same.  */
645   switch (r1->type)
646     {
647       case ERT_MUST_NOT_THROW:
648       case ERT_CLEANUP:
649         break;
650       case ERT_TRY:
651         {
652           struct eh_region_d *c1, *c2;
653           for (c1 = r1->u.eh_try.eh_catch,
654                c2 = r2->u.eh_try.eh_catch;
655                c1 && c2;
656                c1 = c1->u.eh_catch.next_catch,
657                c2 = c2->u.eh_catch.next_catch)
658             if (!eh_region_replaceable_by_p (c1, c2))
659               return false;
660           if (c1 || c2)
661             return false;
662         }
663         break;
664       case ERT_CATCH:
665         if (!list_equal_p (r1->u.eh_catch.type_list, r2->u.eh_catch.type_list))
666           return false;
667         if (!list_equal_p (r1->u.eh_catch.filter_list,
668                            r2->u.eh_catch.filter_list))
669           return false;
670         break;
671       case ERT_ALLOWED_EXCEPTIONS:
672         if (!list_equal_p (r1->u.allowed.type_list, r2->u.allowed.type_list))
673           return false;
674         if (r1->u.allowed.filter != r2->u.allowed.filter)
675           return false;
676         break;
677       case ERT_THROW:
678         if (r1->u.eh_throw.type != r2->u.eh_throw.type)
679           return false;
680         break;
681       default:
682         gcc_unreachable ();
683     }
684   if (dump_file && (dump_flags & TDF_DETAILS))
685     fprintf (dump_file, "Regions %i and %i match\n", r1->region_number,
686                                                      r2->region_number);
687   return true;
688 }
689
690 /* Replace region R2 by R1.  */
691
692 static void
693 replace_region (struct eh_region_d *r1, struct eh_region_d *r2)
694 {
695   struct eh_region_d *next1 = r1->u.eh_try.eh_catch;
696   struct eh_region_d *next2 = r2->u.eh_try.eh_catch;
697   bool is_try = r1->type == ERT_TRY;
698
699   gcc_assert (r1->type != ERT_CATCH);
700   remove_eh_handler_and_replace (r2, r1, false);
701   if (is_try)
702     {
703       while (next1)
704         {
705           r1 = next1;
706           r2 = next2;
707           gcc_assert (next1->type == ERT_CATCH);
708           gcc_assert (next2->type == ERT_CATCH);
709           next1 = next1->u.eh_catch.next_catch;
710           next2 = next2->u.eh_catch.next_catch;
711           remove_eh_handler_and_replace (r2, r1, false);
712         }
713     }
714 }
715
716 /* Return hash value of type list T.  */
717
718 static hashval_t
719 hash_type_list (tree t)
720 {
721   hashval_t val = 0;
722   for (; t; t = TREE_CHAIN (t))
723     val = iterative_hash_hashval_t (TREE_HASH (TREE_VALUE (t)), val);
724   return val;
725 }
726
727 /* Hash EH regions so semantically same regions get same hash value.  */
728
729 static hashval_t
730 hash_eh_region (const void *r)
731 {
732   const struct eh_region_d *region = (const struct eh_region_d *) r;
733   hashval_t val = region->type;
734
735   if (region->tree_label)
736     val = iterative_hash_hashval_t (LABEL_DECL_UID (region->tree_label), val);
737   switch (region->type)
738     {
739       case ERT_MUST_NOT_THROW:
740       case ERT_CLEANUP:
741         break;
742       case ERT_TRY:
743         {
744           struct eh_region_d *c;
745           for (c = region->u.eh_try.eh_catch;
746                c; c = c->u.eh_catch.next_catch)
747             val = iterative_hash_hashval_t (hash_eh_region (c), val);
748         }
749         break;
750       case ERT_CATCH:
751         val = iterative_hash_hashval_t (hash_type_list
752                                           (region->u.eh_catch.type_list), val);
753         break;
754       case ERT_ALLOWED_EXCEPTIONS:
755         val = iterative_hash_hashval_t
756                 (hash_type_list (region->u.allowed.type_list), val);
757         val = iterative_hash_hashval_t (region->u.allowed.filter, val);
758         break;
759       case ERT_THROW:
760         val |= iterative_hash_hashval_t (TYPE_UID (region->u.eh_throw.type), val);
761         break;
762       default:
763         gcc_unreachable ();
764     }
765   return val;
766 }
767
768 /* Return true if regions R1 and R2 are equal.  */
769
770 static int
771 eh_regions_equal_p (const void *r1, const void *r2)
772 {
773   return eh_region_replaceable_by_p ((const struct eh_region_d *) r1,
774                                      (const struct eh_region_d *) r2);
775 }
776
777 /* Walk all peers of REGION and try to merge those regions
778    that are semantically equivalent.  Look into subregions
779    recursively too.  */
780
781 static bool
782 merge_peers (struct eh_region_d *region)
783 {
784   struct eh_region_d *r1, *r2, *outer = NULL, *next;
785   bool merged = false;
786   int num_regions = 0;
787   if (region)
788     outer = region->outer;
789   else
790     return false;
791
792   /* First see if there is inner region equivalent to region
793      in question.  EH control flow is acyclic so we know we
794      can merge them.  */
795   if (outer)
796     for (r1 = region; r1; r1 = next)
797       {
798         next = r1->next_peer;
799         if (r1->type == ERT_CATCH)
800           continue;
801         if (eh_region_replaceable_by_p (r1->outer, r1))
802           {
803             replace_region (r1->outer, r1);
804             merged = true;
805           }
806         else
807           num_regions ++;
808       }
809
810   /* Get new first region and try to match the peers
811      for equivalence.  */
812   if (outer)
813     region = outer->inner;
814   else
815     region = cfun->eh->region_tree;
816
817   /* There are few regions to inspect:
818      N^2 loop matching each region with each region
819      will do the job well.  */
820   if (num_regions < 10)
821     {
822       for (r1 = region; r1; r1 = r1->next_peer)
823         {
824           if (r1->type == ERT_CATCH)
825             continue;
826           for (r2 = r1->next_peer; r2; r2 = next)
827             {
828               next = r2->next_peer;
829               if (eh_region_replaceable_by_p (r1, r2))
830                 {
831                   replace_region (r1, r2);
832                   merged = true;
833                 }
834             }
835         }
836     }
837   /* Or use hashtable to avoid N^2 behaviour.  */
838   else
839     {
840       htab_t hash;
841       hash = htab_create (num_regions, hash_eh_region,
842                           eh_regions_equal_p, NULL);
843       for (r1 = region; r1; r1 = next)
844         {
845           void **slot;
846
847           next = r1->next_peer;
848           if (r1->type == ERT_CATCH)
849             continue;
850           slot = htab_find_slot (hash, r1, INSERT);
851           if (!*slot)
852             *slot = r1;
853           else
854             replace_region ((struct eh_region_d *) *slot, r1);
855         }
856       htab_delete (hash);
857     }
858   for (r1 = region; r1; r1 = r1->next_peer)
859     merged |= merge_peers (r1->inner);
860   return merged;
861 }
862
863 /* Remove all regions whose labels are not reachable.
864    REACHABLE is bitmap of all regions that are used by the function
865    CONTAINS_STMT is bitmap of all regions that contains stmt (or NULL). */
866
867 void
868 remove_unreachable_regions (sbitmap reachable, sbitmap contains_stmt)
869 {
870   int i;
871   struct eh_region_d *r;
872   VEC(eh_region,heap) *must_not_throws = VEC_alloc (eh_region, heap, 16);
873   struct eh_region_d *local_must_not_throw = NULL;
874   struct eh_region_d *first_must_not_throw = NULL;
875
876   for (i = cfun->eh->last_region_number; i > 0; --i)
877     {
878       r = VEC_index (eh_region, cfun->eh->region_array, i);
879       if (!r || r->region_number != i)
880         continue;
881       if (!TEST_BIT (reachable, i) && !r->resume)
882         {
883           bool kill_it = true;
884
885           r->tree_label = NULL;
886           switch (r->type)
887             {
888             case ERT_THROW:
889               /* Don't remove ERT_THROW regions if their outer region
890                  is reachable.  */
891               if (r->outer && TEST_BIT (reachable, r->outer->region_number))
892                 kill_it = false;
893               break;
894             case ERT_MUST_NOT_THROW:
895               /* MUST_NOT_THROW regions are implementable solely in the
896                  runtime, but we need them when inlining function.
897
898                  Keep them if outer region is not MUST_NOT_THROW a well
899                  and if they contain some statement that might unwind through
900                  them.  */
901               if ((!r->outer || r->outer->type != ERT_MUST_NOT_THROW)
902                   && (!contains_stmt
903                       || can_be_reached_by_runtime (contains_stmt, r)))
904                 kill_it = false;
905               break;
906             case ERT_TRY:
907               {
908                 /* TRY regions are reachable if any of its CATCH regions
909                    are reachable.  */
910                 struct eh_region_d *c;
911                 for (c = r->u.eh_try.eh_catch; c;
912                      c = c->u.eh_catch.next_catch)
913                   if (TEST_BIT (reachable, c->region_number))
914                     {
915                       kill_it = false;
916                       break;
917                     }
918                 break;
919               }
920
921             default:
922               break;
923             }
924
925           if (kill_it)
926             {
927               if (dump_file)
928                 fprintf (dump_file, "Removing unreachable eh region %i\n",
929                          r->region_number);
930               remove_eh_handler (r);
931             }
932           else if (r->type == ERT_MUST_NOT_THROW)
933             {
934               if (!first_must_not_throw)
935                 first_must_not_throw = r;
936               VEC_safe_push (eh_region, heap, must_not_throws, r);
937             }
938         }
939       else
940         if (r->type == ERT_MUST_NOT_THROW)
941           {
942             if (!local_must_not_throw)
943               local_must_not_throw = r;
944             if (r->outer)
945               VEC_safe_push (eh_region, heap, must_not_throws, r);
946           }
947     }
948
949   /* MUST_NOT_THROW regions without local handler are all the same; they
950      trigger terminate call in runtime.
951      MUST_NOT_THROW handled locally can differ in debug info associated
952      to std::terminate () call or if one is coming from Java and other
953      from C++ whether they call terminate or abort.  
954
955      We merge all MUST_NOT_THROW regions handled by the run-time into one.
956      We alsobring all local MUST_NOT_THROW regions to the roots of EH tree
957      (since unwinding never continues to the outer region anyway).
958      If MUST_NOT_THROW with local handler is present in the tree, we use
959      that region to merge into, since it will remain in tree anyway;
960      otherwise we use first MUST_NOT_THROW.
961
962      Merging of locally handled regions needs changes to the CFG.  Crossjumping
963      should take care of this, by looking at the actual code and
964      ensuring that the cleanup actions are really the same.  */
965
966   if (local_must_not_throw)
967     first_must_not_throw = local_must_not_throw;
968
969   for (i = 0; VEC_iterate (eh_region, must_not_throws, i, r); i++)
970     {
971       if (!r->label && !r->tree_label && r != first_must_not_throw)
972         {
973           if (dump_file)
974             fprintf (dump_file, "Replacing MUST_NOT_THROW region %i by %i\n",
975                      r->region_number,
976                      first_must_not_throw->region_number);
977           remove_eh_handler_and_replace (r, first_must_not_throw, false);
978           first_must_not_throw->may_contain_throw |= r->may_contain_throw;
979         }
980       else
981         bring_to_root (r);
982     }
983   merge_peers (cfun->eh->region_tree);
984 #ifdef ENABLE_CHECKING
985   verify_eh_tree (cfun);
986 #endif
987   VEC_free (eh_region, heap, must_not_throws);
988 }
989
990 /* Return array mapping LABEL_DECL_UID to region such that region's tree_label
991    is identical to label.  */
992
993 VEC (int, heap) *
994 label_to_region_map (void)
995 {
996   VEC (int, heap) * label_to_region = NULL;
997   int i;
998   int idx;
999
1000   VEC_safe_grow_cleared (int, heap, label_to_region,
1001                          cfun->cfg->last_label_uid + 1);
1002   for (i = cfun->eh->last_region_number; i > 0; --i)
1003     {
1004       struct eh_region_d *r = VEC_index (eh_region, cfun->eh->region_array, i);
1005       if (r && r->region_number == i
1006           && r->tree_label && LABEL_DECL_UID (r->tree_label) >= 0)
1007         {
1008           if ((idx = VEC_index (int, label_to_region,
1009                                 LABEL_DECL_UID (r->tree_label))) != 0)
1010               r->next_region_sharing_label =
1011               VEC_index (eh_region, cfun->eh->region_array, idx);
1012           else
1013             r->next_region_sharing_label = NULL;
1014           VEC_replace (int, label_to_region, LABEL_DECL_UID (r->tree_label),
1015                        i);
1016         }
1017     }
1018   return label_to_region;
1019 }
1020
1021 /* Return number of EH regions.  */
1022 int
1023 num_eh_regions (void)
1024 {
1025   return cfun->eh->last_region_number + 1;
1026 }
1027
1028 /* Return next region sharing same label as REGION.  */
1029
1030 int
1031 get_next_region_sharing_label (int region)
1032 {
1033   struct eh_region_d *r;
1034   if (!region)
1035     return 0;
1036   r = VEC_index (eh_region, cfun->eh->region_array, region);
1037   if (!r || !r->next_region_sharing_label)
1038     return 0;
1039   return r->next_region_sharing_label->region_number;
1040 }
1041
1042 /* Set up EH labels for RTL.  */
1043
1044 void
1045 convert_from_eh_region_ranges (void)
1046 {
1047   int i, n = cfun->eh->last_region_number;
1048
1049   /* Most of the work is already done at the tree level.  All we need to
1050      do is collect the rtl labels that correspond to the tree labels that
1051      collect the rtl labels that correspond to the tree labels
1052      we allocated earlier.  */
1053   for (i = 1; i <= n; ++i)
1054     {
1055       struct eh_region_d *region;
1056
1057       region = VEC_index (eh_region, cfun->eh->region_array, i);
1058       if (region && region->tree_label)
1059         region->label = DECL_RTL_IF_SET (region->tree_label);
1060     }
1061 }
1062
1063 void
1064 find_exception_handler_labels (void)
1065 {
1066   int i;
1067
1068   if (cfun->eh->region_tree == NULL)
1069     return;
1070
1071   for (i = cfun->eh->last_region_number; i > 0; --i)
1072     {
1073       struct eh_region_d *region;
1074       rtx lab;
1075
1076       region = VEC_index (eh_region, cfun->eh->region_array, i);
1077       if (! region || region->region_number != i)
1078         continue;
1079       if (crtl->eh.built_landing_pads)
1080         lab = region->landing_pad;
1081       else
1082         lab = region->label;
1083     }
1084 }
1085
1086 /* Returns true if the current function has exception handling regions.  */
1087
1088 bool
1089 current_function_has_exception_handlers (void)
1090 {
1091   int i;
1092
1093   for (i = cfun->eh->last_region_number; i > 0; --i)
1094     {
1095       struct eh_region_d *region;
1096
1097       region = VEC_index (eh_region, cfun->eh->region_array, i);
1098       if (region
1099           && region->region_number == i
1100           && region->type != ERT_THROW)
1101         return true;
1102     }
1103
1104   return false;
1105 }
1106 \f
1107 /* A subroutine of duplicate_eh_regions.  Search the region tree under O
1108    for the minimum and maximum region numbers.  Update *MIN and *MAX.  */
1109
1110 static void
1111 duplicate_eh_regions_0 (eh_region o, int *min, int *max)
1112 {
1113   int i;
1114
1115   if (o->aka)
1116     {
1117       i = bitmap_first_set_bit (o->aka);
1118       if (i < *min)
1119         *min = i;
1120       i = bitmap_last_set_bit (o->aka);
1121       if (i > *max)
1122         *max = i;
1123     }
1124   if (o->region_number < *min)
1125     *min = o->region_number;
1126   if (o->region_number > *max)
1127     *max = o->region_number;
1128
1129   if (o->inner)
1130     {
1131       o = o->inner;
1132       duplicate_eh_regions_0 (o, min, max);
1133       while (o->next_peer)
1134         {
1135           o = o->next_peer;
1136           duplicate_eh_regions_0 (o, min, max);
1137         }
1138     }
1139 }
1140
1141 /* A subroutine of duplicate_eh_regions.  Copy the region tree under OLD.
1142    Root it at OUTER, and apply EH_OFFSET to the region number.  Don't worry
1143    about the other internal pointers just yet, just the tree-like pointers.  */
1144
1145 static eh_region
1146 duplicate_eh_regions_1 (eh_region old, eh_region outer, int eh_offset)
1147 {
1148   eh_region ret, n;
1149
1150   ret = n = GGC_NEW (struct eh_region_d);
1151
1152   *n = *old;
1153   n->outer = outer;
1154   n->next_peer = NULL;
1155   if (old->aka)
1156     {
1157       unsigned i;
1158       bitmap_iterator bi;
1159       n->aka = BITMAP_GGC_ALLOC ();
1160
1161       EXECUTE_IF_SET_IN_BITMAP (old->aka, 0, i, bi)
1162       {
1163         bitmap_set_bit (n->aka, i + eh_offset);
1164         VEC_replace (eh_region, cfun->eh->region_array, i + eh_offset, n);
1165       }
1166     }
1167
1168   n->region_number += eh_offset;
1169   VEC_replace (eh_region, cfun->eh->region_array, n->region_number, n);
1170
1171   if (old->inner)
1172     {
1173       old = old->inner;
1174       n = n->inner = duplicate_eh_regions_1 (old, ret, eh_offset);
1175       while (old->next_peer)
1176         {
1177           old = old->next_peer;
1178           n = n->next_peer = duplicate_eh_regions_1 (old, ret, eh_offset);
1179         }
1180     }
1181
1182   return ret;
1183 }
1184
1185 /* Look for first outer region of R (or R itself) that is
1186    TRY region. Return NULL if none.  */
1187
1188 static struct eh_region_d *
1189 find_prev_try (struct eh_region_d * r)
1190 {
1191   for (; r && r->type != ERT_TRY; r = r->outer)
1192     if (r->type == ERT_MUST_NOT_THROW
1193         || (r->type == ERT_ALLOWED_EXCEPTIONS
1194             && !r->u.allowed.type_list))
1195       {
1196         r = NULL;
1197         break;
1198       }
1199   return r;
1200 }
1201
1202 /* Duplicate the EH regions of IFUN, rooted at COPY_REGION, into current
1203    function and root the tree below OUTER_REGION.  Remap labels using MAP
1204    callback.  The special case of COPY_REGION of 0 means all regions.  */
1205
1206 int
1207 duplicate_eh_regions (struct function *ifun, duplicate_eh_regions_map map,
1208                       void *data, int copy_region, int outer_region)
1209 {
1210   eh_region cur, outer, *splice;
1211   int i, min_region, max_region, eh_offset, cfun_last_region_number;
1212   int num_regions;
1213
1214   if (!ifun->eh)
1215     return 0;
1216 #ifdef ENABLE_CHECKING
1217   verify_eh_tree (ifun);
1218 #endif
1219
1220   /* Find the range of region numbers to be copied.  The interface we 
1221      provide here mandates a single offset to find new number from old,
1222      which means we must look at the numbers present, instead of the
1223      count or something else.  */
1224   if (copy_region > 0)
1225     {
1226       min_region = INT_MAX;
1227       max_region = 0;
1228
1229       cur = VEC_index (eh_region, ifun->eh->region_array, copy_region);
1230       duplicate_eh_regions_0 (cur, &min_region, &max_region);
1231     }
1232   else
1233     {
1234       min_region = 1;
1235       max_region = ifun->eh->last_region_number;
1236     }
1237   num_regions = max_region - min_region + 1;
1238   cfun_last_region_number = cfun->eh->last_region_number;
1239   eh_offset = cfun_last_region_number + 1 - min_region;
1240
1241   /* If we've not yet created a region array, do so now.  */
1242   cfun->eh->last_region_number = cfun_last_region_number + num_regions;
1243   VEC_safe_grow_cleared (eh_region, gc, cfun->eh->region_array,
1244                          cfun->eh->last_region_number + 1);
1245
1246   /* Locate the spot at which to insert the new tree.  */
1247   if (outer_region > 0)
1248     {
1249       outer = VEC_index (eh_region, cfun->eh->region_array, outer_region);
1250       if (outer)
1251         splice = &outer->inner;
1252       else
1253         splice = &cfun->eh->region_tree;
1254     }
1255   else
1256     {
1257       outer = NULL;
1258       splice = &cfun->eh->region_tree;
1259     }
1260   while (*splice)
1261     splice = &(*splice)->next_peer;
1262
1263   if (!ifun->eh->region_tree)
1264     {
1265       if (outer)
1266         for (i = cfun_last_region_number + 1;
1267              i <= cfun->eh->last_region_number; i++)
1268           {
1269             VEC_replace (eh_region, cfun->eh->region_array, i, outer);
1270             if (outer->aka == NULL)
1271               outer->aka = BITMAP_GGC_ALLOC ();
1272             bitmap_set_bit (outer->aka, i);
1273           }
1274       return eh_offset;
1275     }
1276
1277   /* Copy all the regions in the subtree.  */
1278   if (copy_region > 0)
1279     {
1280       cur = VEC_index (eh_region, ifun->eh->region_array, copy_region);
1281       *splice = duplicate_eh_regions_1 (cur, outer, eh_offset);
1282     }
1283   else
1284     {
1285       eh_region n;
1286
1287       cur = ifun->eh->region_tree;
1288       *splice = n = duplicate_eh_regions_1 (cur, outer, eh_offset);
1289       while (cur->next_peer)
1290         {
1291           cur = cur->next_peer;
1292           n = n->next_peer = duplicate_eh_regions_1 (cur, outer, eh_offset);
1293         }
1294     }
1295
1296   /* Remap all the labels in the new regions.  */
1297   for (i = cfun_last_region_number + 1;
1298        VEC_iterate (eh_region, cfun->eh->region_array, i, cur); ++i)
1299     if (cur && cur->tree_label)
1300       cur->tree_label = map (cur->tree_label, data);
1301
1302   /* Remap all of the internal catch and cleanup linkages.  Since we 
1303      duplicate entire subtrees, all of the referenced regions will have
1304      been copied too.  And since we renumbered them as a block, a simple
1305      bit of arithmetic finds us the index for the replacement region.  */
1306   for (i = cfun_last_region_number + 1;
1307        VEC_iterate (eh_region, cfun->eh->region_array, i, cur); ++i)
1308     {
1309       /* All removed EH that is toplevel in input function is now
1310          in outer EH of output function.  */
1311       if (cur == NULL)
1312         {
1313           gcc_assert (VEC_index
1314                       (eh_region, ifun->eh->region_array,
1315                        i - eh_offset) == NULL);
1316           if (outer)
1317             {
1318               VEC_replace (eh_region, cfun->eh->region_array, i, outer);
1319               if (outer->aka == NULL)
1320                 outer->aka = BITMAP_GGC_ALLOC ();
1321               bitmap_set_bit (outer->aka, i);
1322             }
1323           continue;
1324         }
1325       if (i != cur->region_number)
1326         continue;
1327
1328 #define REMAP(REG) \
1329         (REG) = VEC_index (eh_region, cfun->eh->region_array, \
1330                            (REG)->region_number + eh_offset)
1331
1332       switch (cur->type)
1333         {
1334         case ERT_TRY:
1335           if (cur->u.eh_try.eh_catch)
1336             REMAP (cur->u.eh_try.eh_catch);
1337           if (cur->u.eh_try.last_catch)
1338             REMAP (cur->u.eh_try.last_catch);
1339           break;
1340
1341         case ERT_CATCH:
1342           if (cur->u.eh_catch.next_catch)
1343             REMAP (cur->u.eh_catch.next_catch);
1344           if (cur->u.eh_catch.prev_catch)
1345             REMAP (cur->u.eh_catch.prev_catch);
1346           break;
1347
1348         default:
1349           break;
1350         }
1351
1352 #undef REMAP
1353     }
1354 #ifdef ENABLE_CHECKING
1355   verify_eh_tree (cfun);
1356 #endif
1357
1358   return eh_offset;
1359 }
1360
1361 /* Return new copy of eh region OLD inside region NEW_OUTER.
1362    Do not care about updating the tree otherwise.  */
1363
1364 static struct eh_region_d *
1365 copy_eh_region_1 (struct eh_region_d *old, struct eh_region_d *new_outer)
1366 {
1367   struct eh_region_d *new_eh = gen_eh_region (old->type, new_outer);
1368   new_eh->u = old->u;
1369   new_eh->tree_label = old->tree_label;
1370   new_eh->may_contain_throw = old->may_contain_throw;
1371   VEC_safe_grow (eh_region, gc, cfun->eh->region_array,
1372                  cfun->eh->last_region_number + 1);
1373   VEC_replace (eh_region, cfun->eh->region_array, new_eh->region_number, new_eh);
1374   if (dump_file && (dump_flags & TDF_DETAILS))
1375     fprintf (dump_file, "Copying region %i to %i\n", old->region_number, new_eh->region_number);
1376   return new_eh;
1377 }
1378
1379 /* Return new copy of eh region OLD inside region NEW_OUTER.  
1380   
1381    Copy whole catch-try chain if neccesary.  */
1382
1383 static struct eh_region_d *
1384 copy_eh_region (struct eh_region_d *old, struct eh_region_d *new_outer)
1385 {
1386   struct eh_region_d *r, *n, *old_try, *new_try, *ret = NULL;
1387   VEC(eh_region,heap) *catch_list = NULL;
1388
1389   if (old->type != ERT_CATCH)
1390     {
1391       gcc_assert (old->type != ERT_TRY);
1392       r = copy_eh_region_1 (old, new_outer);
1393       return r;
1394     }
1395
1396   /* Locate and copy corresponding TRY.  */
1397   for (old_try = old->next_peer; old_try->type == ERT_CATCH; old_try = old_try->next_peer)
1398     continue;
1399   gcc_assert (old_try->type == ERT_TRY);
1400   new_try = gen_eh_region_try (new_outer);
1401   new_try->tree_label = old_try->tree_label;
1402   new_try->may_contain_throw = old_try->may_contain_throw;
1403   if (dump_file && (dump_flags & TDF_DETAILS))
1404     fprintf (dump_file, "Copying try-catch regions. Try: %i to %i\n",
1405              old_try->region_number, new_try->region_number);
1406   VEC_safe_grow (eh_region, gc, cfun->eh->region_array,
1407                  cfun->eh->last_region_number + 1);
1408   VEC_replace (eh_region, cfun->eh->region_array, new_try->region_number, new_try);
1409
1410   /* In order to keep CATCH list in order, we need to copy in reverse order.  */
1411   for (r = old_try->u.eh_try.last_catch; r->type == ERT_CATCH; r = r->next_peer)
1412     VEC_safe_push (eh_region, heap, catch_list, r);
1413
1414   while (VEC_length (eh_region, catch_list))
1415     {
1416       r = VEC_pop (eh_region, catch_list);
1417
1418       /* Duplicate CATCH.  */
1419       n = gen_eh_region_catch (new_try, r->u.eh_catch.type_list);
1420       n->tree_label = r->tree_label;
1421       n->may_contain_throw = r->may_contain_throw;
1422       VEC_safe_grow (eh_region, gc, cfun->eh->region_array,
1423                      cfun->eh->last_region_number + 1);
1424       VEC_replace (eh_region, cfun->eh->region_array, n->region_number, n);
1425       n->tree_label = r->tree_label;
1426
1427       if (dump_file && (dump_flags & TDF_DETAILS))
1428         fprintf (dump_file, "Copying try-catch regions. Catch: %i to %i\n",
1429                  r->region_number, n->region_number);
1430       if (r == old)
1431         ret = n;
1432     }
1433   VEC_free (eh_region, heap, catch_list);
1434   gcc_assert (ret);
1435   return ret;
1436 }
1437
1438 /* Callback for forach_reachable_handler that push REGION into single VECtor DATA.  */
1439
1440 static void
1441 push_reachable_handler (struct eh_region_d *region, void *data)
1442 {
1443   VEC(eh_region,heap) **trace = (VEC(eh_region,heap) **) data;
1444   VEC_safe_push (eh_region, heap, *trace, region);
1445 }
1446
1447 /* Redirect EH edge E that to NEW_DEST_LABEL.
1448    IS_RESX, INLINABLE_CALL and REGION_NMUBER match the parameter of
1449    foreach_reachable_handler.  */
1450
1451 struct eh_region_d *
1452 redirect_eh_edge_to_label (edge e, tree new_dest_label, bool is_resx,
1453                            bool inlinable_call, int region_number)
1454 {
1455   struct eh_region_d *outer;
1456   struct eh_region_d *region;
1457   VEC (eh_region, heap) * trace = NULL;
1458   int i;
1459   int start_here = -1;
1460   basic_block old_bb = e->dest;
1461   struct eh_region_d *old, *r = NULL;
1462   bool update_inplace = true;
1463   edge_iterator ei;
1464   edge e2;
1465
1466   /* If there is only one EH edge, we don't need to duplicate;
1467      just update labels in the tree.  */
1468   FOR_EACH_EDGE (e2, ei, old_bb->preds)
1469     if ((e2->flags & EDGE_EH) && e2 != e)
1470       {
1471         update_inplace = false;
1472         break;
1473       }
1474
1475   region = VEC_index (eh_region, cfun->eh->region_array, region_number);
1476   gcc_assert (region);
1477
1478   foreach_reachable_handler (region_number, is_resx, inlinable_call,
1479                              push_reachable_handler, &trace);
1480   if (dump_file && (dump_flags & TDF_DETAILS))
1481     {
1482       dump_eh_tree (dump_file, cfun);
1483       fprintf (dump_file, "Trace: ");
1484       for (i = 0; i < (int) VEC_length (eh_region, trace); i++)
1485         fprintf (dump_file, " %i", VEC_index (eh_region, trace, i)->region_number);
1486       fprintf (dump_file, " inplace: %i\n", update_inplace);
1487     }
1488
1489   if (update_inplace)
1490     {
1491       /* In easy route just walk trace and update all occurences of the label.  */
1492       for (i = 0; i < (int) VEC_length (eh_region, trace); i++)
1493         {
1494           r = VEC_index (eh_region, trace, i);
1495           if (r->tree_label && label_to_block (r->tree_label) == old_bb)
1496             {
1497               r->tree_label = new_dest_label;
1498               if (dump_file && (dump_flags & TDF_DETAILS))
1499                 fprintf (dump_file, "Updating label for region %i\n",
1500                          r->region_number);
1501             }
1502         }
1503       r = region;
1504     }
1505   else
1506     {
1507       /* Now look for outermost handler that reffers to the basic block in question.
1508          We start our duplication there.  */
1509       for (i = 0; i < (int) VEC_length (eh_region, trace); i++)
1510         {
1511           r = VEC_index (eh_region, trace, i);
1512           if (r->tree_label && label_to_block (r->tree_label) == old_bb)
1513             start_here = i;
1514         }
1515       outer = VEC_index (eh_region, trace, start_here)->outer;
1516       gcc_assert (start_here >= 0);
1517
1518       /* And now do the dirty job!  */
1519       for (i = start_here; i >= 0; i--)
1520         {
1521           old = VEC_index (eh_region, trace, i);
1522           gcc_assert (!outer || old->outer != outer->outer);
1523
1524           /* Copy region and update label.  */
1525           r = copy_eh_region (old, outer);
1526           VEC_replace (eh_region, trace, i, r);
1527           if (r->tree_label && label_to_block (r->tree_label) == old_bb)
1528             {
1529               r->tree_label = new_dest_label;
1530               if (dump_file && (dump_flags & TDF_DETAILS))
1531                 fprintf (dump_file, "Updating label for region %i\n",
1532                          r->region_number);
1533             }
1534
1535           /* We got into copying CATCH.  copy_eh_region already did job
1536              of copying all catch blocks corresponding to the try.  Now
1537              we need to update labels in all of them and see trace.
1538
1539              We continue nesting into TRY region corresponding to CATCH:
1540              When duplicating EH tree contaiing subregions of CATCH,
1541              the CATCH region itself is never inserted to trace so we
1542              never get here anyway.  */
1543           if (r->type == ERT_CATCH)
1544             {
1545               /* Walk other catch regions we copied and update labels as needed.  */
1546               for (r = r->next_peer; r->type == ERT_CATCH; r = r->next_peer)
1547                 if (r->tree_label && label_to_block (r->tree_label) == old_bb)
1548                   {
1549                     r->tree_label = new_dest_label;
1550                     if (dump_file && (dump_flags & TDF_DETAILS))
1551                       fprintf (dump_file, "Updating label for region %i\n",
1552                                r->region_number);
1553                   }
1554                gcc_assert (r->type == ERT_TRY);
1555
1556                /* Skip sibling catch regions from the trace.
1557                   They are already updated.  */
1558                while (i > 0 && VEC_index (eh_region, trace, i - 1)->outer == old->outer)
1559                  {
1560                    gcc_assert (VEC_index (eh_region, trace, i - 1)->type == ERT_CATCH);
1561                    i--;
1562                  }
1563              }
1564
1565           outer = r;
1566         }
1567         
1568       if (is_resx || region->type == ERT_THROW)
1569         r = copy_eh_region (region, outer);
1570     }
1571
1572   VEC_free (eh_region, heap, trace);
1573   if (dump_file && (dump_flags & TDF_DETAILS))
1574     {
1575       dump_eh_tree (dump_file, cfun);
1576       fprintf (dump_file, "New region: %i\n", r->region_number);
1577     }
1578   return r;
1579 }
1580
1581 /* Return region number of region that is outer to both if REGION_A and
1582    REGION_B in IFUN.  */
1583
1584 int
1585 eh_region_outermost (struct function *ifun, int region_a, int region_b)
1586 {
1587   struct eh_region_d *rp_a, *rp_b;
1588   sbitmap b_outer;
1589
1590   gcc_assert (ifun->eh->last_region_number > 0);
1591   gcc_assert (ifun->eh->region_tree);
1592
1593   rp_a = VEC_index (eh_region, ifun->eh->region_array, region_a);
1594   rp_b = VEC_index (eh_region, ifun->eh->region_array, region_b);
1595   gcc_assert (rp_a != NULL);
1596   gcc_assert (rp_b != NULL);
1597
1598   b_outer = sbitmap_alloc (ifun->eh->last_region_number + 1);
1599   sbitmap_zero (b_outer);
1600
1601   do
1602     {
1603       SET_BIT (b_outer, rp_b->region_number);
1604       rp_b = rp_b->outer;
1605     }
1606   while (rp_b);
1607
1608   do
1609     {
1610       if (TEST_BIT (b_outer, rp_a->region_number))
1611         {
1612           sbitmap_free (b_outer);
1613           return rp_a->region_number;
1614         }
1615       rp_a = rp_a->outer;
1616     }
1617   while (rp_a);
1618
1619   sbitmap_free (b_outer);
1620   return -1;
1621 }
1622 \f
1623 static int
1624 t2r_eq (const void *pentry, const void *pdata)
1625 {
1626   const_tree const entry = (const_tree) pentry;
1627   const_tree const data = (const_tree) pdata;
1628
1629   return TREE_PURPOSE (entry) == data;
1630 }
1631
1632 static hashval_t
1633 t2r_hash (const void *pentry)
1634 {
1635   const_tree const entry = (const_tree) pentry;
1636   return TREE_HASH (TREE_PURPOSE (entry));
1637 }
1638
1639 void
1640 add_type_for_runtime (tree type)
1641 {
1642   tree *slot;
1643
1644   slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
1645                                             TREE_HASH (type), INSERT);
1646   if (*slot == NULL)
1647     {
1648       tree runtime = (*lang_eh_runtime_type) (type);
1649       *slot = tree_cons (type, runtime, NULL_TREE);
1650     }
1651 }
1652
1653 tree
1654 lookup_type_for_runtime (tree type)
1655 {
1656   tree *slot;
1657
1658   slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
1659                                             TREE_HASH (type), NO_INSERT);
1660
1661   /* We should have always inserted the data earlier.  */
1662   return TREE_VALUE (*slot);
1663 }
1664
1665 \f
1666 /* Represent an entry in @TTypes for either catch actions
1667    or exception filter actions.  */
1668 struct GTY(()) ttypes_filter {
1669   tree t;
1670   int filter;
1671 };
1672
1673 /* Compare ENTRY (a ttypes_filter entry in the hash table) with DATA
1674    (a tree) for a @TTypes type node we are thinking about adding.  */
1675
1676 static int
1677 ttypes_filter_eq (const void *pentry, const void *pdata)
1678 {
1679   const struct ttypes_filter *const entry
1680     = (const struct ttypes_filter *) pentry;
1681   const_tree const data = (const_tree) pdata;
1682
1683   return entry->t == data;
1684 }
1685
1686 static hashval_t
1687 ttypes_filter_hash (const void *pentry)
1688 {
1689   const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1690   return TREE_HASH (entry->t);
1691 }
1692
1693 /* Compare ENTRY with DATA (both struct ttypes_filter) for a @TTypes
1694    exception specification list we are thinking about adding.  */
1695 /* ??? Currently we use the type lists in the order given.  Someone
1696    should put these in some canonical order.  */
1697
1698 static int
1699 ehspec_filter_eq (const void *pentry, const void *pdata)
1700 {
1701   const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1702   const struct ttypes_filter *data = (const struct ttypes_filter *) pdata;
1703
1704   return type_list_equal (entry->t, data->t);
1705 }
1706
1707 /* Hash function for exception specification lists.  */
1708
1709 static hashval_t
1710 ehspec_filter_hash (const void *pentry)
1711 {
1712   const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1713   hashval_t h = 0;
1714   tree list;
1715
1716   for (list = entry->t; list ; list = TREE_CHAIN (list))
1717     h = (h << 5) + (h >> 27) + TREE_HASH (TREE_VALUE (list));
1718   return h;
1719 }
1720
1721 /* Add TYPE (which may be NULL) to crtl->eh.ttype_data, using TYPES_HASH
1722    to speed up the search.  Return the filter value to be used.  */
1723
1724 static int
1725 add_ttypes_entry (htab_t ttypes_hash, tree type)
1726 {
1727   struct ttypes_filter **slot, *n;
1728
1729   slot = (struct ttypes_filter **)
1730     htab_find_slot_with_hash (ttypes_hash, type, TREE_HASH (type), INSERT);
1731
1732   if ((n = *slot) == NULL)
1733     {
1734       /* Filter value is a 1 based table index.  */
1735
1736       n = XNEW (struct ttypes_filter);
1737       n->t = type;
1738       n->filter = VEC_length (tree, crtl->eh.ttype_data) + 1;
1739       *slot = n;
1740
1741       VEC_safe_push (tree, gc, crtl->eh.ttype_data, type);
1742     }
1743
1744   return n->filter;
1745 }
1746
1747 /* Add LIST to crtl->eh.ehspec_data, using EHSPEC_HASH and TYPES_HASH
1748    to speed up the search.  Return the filter value to be used.  */
1749
1750 static int
1751 add_ehspec_entry (htab_t ehspec_hash, htab_t ttypes_hash, tree list)
1752 {
1753   struct ttypes_filter **slot, *n;
1754   struct ttypes_filter dummy;
1755
1756   dummy.t = list;
1757   slot = (struct ttypes_filter **)
1758     htab_find_slot (ehspec_hash, &dummy, INSERT);
1759
1760   if ((n = *slot) == NULL)
1761     {
1762       /* Filter value is a -1 based byte index into a uleb128 buffer.  */
1763
1764       n = XNEW (struct ttypes_filter);
1765       n->t = list;
1766       n->filter = -(VARRAY_ACTIVE_SIZE (crtl->eh.ehspec_data) + 1);
1767       *slot = n;
1768
1769       /* Generate a 0 terminated list of filter values.  */
1770       for (; list ; list = TREE_CHAIN (list))
1771         {
1772           if (targetm.arm_eabi_unwinder)
1773             VARRAY_PUSH_TREE (crtl->eh.ehspec_data, TREE_VALUE (list));
1774           else
1775             {
1776               /* Look up each type in the list and encode its filter
1777                  value as a uleb128.  */
1778               push_uleb128 (&crtl->eh.ehspec_data,
1779                   add_ttypes_entry (ttypes_hash, TREE_VALUE (list)));
1780             }
1781         }
1782       if (targetm.arm_eabi_unwinder)
1783         VARRAY_PUSH_TREE (crtl->eh.ehspec_data, NULL_TREE);
1784       else
1785         VARRAY_PUSH_UCHAR (crtl->eh.ehspec_data, 0);
1786     }
1787
1788   return n->filter;
1789 }
1790
1791 /* Generate the action filter values to be used for CATCH and
1792    ALLOWED_EXCEPTIONS regions.  When using dwarf2 exception regions,
1793    we use lots of landing pads, and so every type or list can share
1794    the same filter value, which saves table space.  */
1795
1796 static void
1797 assign_filter_values (void)
1798 {
1799   int i;
1800   htab_t ttypes, ehspec;
1801
1802   crtl->eh.ttype_data = VEC_alloc (tree, gc, 16);
1803   if (targetm.arm_eabi_unwinder)
1804     VARRAY_TREE_INIT (crtl->eh.ehspec_data, 64, "ehspec_data");
1805   else
1806     VARRAY_UCHAR_INIT (crtl->eh.ehspec_data, 64, "ehspec_data");
1807
1808   ttypes = htab_create (31, ttypes_filter_hash, ttypes_filter_eq, free);
1809   ehspec = htab_create (31, ehspec_filter_hash, ehspec_filter_eq, free);
1810
1811   for (i = cfun->eh->last_region_number; i > 0; --i)
1812     {
1813       struct eh_region_d *r;
1814
1815       r = VEC_index (eh_region, cfun->eh->region_array, i);
1816
1817       /* Mind we don't process a region more than once.  */
1818       if (!r || r->region_number != i)
1819         continue;
1820
1821       switch (r->type)
1822         {
1823         case ERT_CATCH:
1824           /* Whatever type_list is (NULL or true list), we build a list
1825              of filters for the region.  */
1826           r->u.eh_catch.filter_list = NULL_TREE;
1827
1828           if (r->u.eh_catch.type_list != NULL)
1829             {
1830               /* Get a filter value for each of the types caught and store
1831                  them in the region's dedicated list.  */
1832               tree tp_node = r->u.eh_catch.type_list;
1833
1834               for (;tp_node; tp_node = TREE_CHAIN (tp_node))
1835                 {
1836                   int flt = add_ttypes_entry (ttypes, TREE_VALUE (tp_node));
1837                   tree flt_node = build_int_cst (NULL_TREE, flt);
1838
1839                   r->u.eh_catch.filter_list
1840                     = tree_cons (NULL_TREE, flt_node, r->u.eh_catch.filter_list);
1841                 }
1842             }
1843           else
1844             {
1845               /* Get a filter value for the NULL list also since it will need
1846                  an action record anyway.  */
1847               int flt = add_ttypes_entry (ttypes, NULL);
1848               tree flt_node = build_int_cst (NULL_TREE, flt);
1849
1850               r->u.eh_catch.filter_list
1851                 = tree_cons (NULL_TREE, flt_node, r->u.eh_catch.filter_list);
1852             }
1853
1854           break;
1855
1856         case ERT_ALLOWED_EXCEPTIONS:
1857           r->u.allowed.filter
1858             = add_ehspec_entry (ehspec, ttypes, r->u.allowed.type_list);
1859           break;
1860
1861         default:
1862           break;
1863         }
1864     }
1865
1866   htab_delete (ttypes);
1867   htab_delete (ehspec);
1868 }
1869
1870 /* Emit SEQ into basic block just before INSN (that is assumed to be
1871    first instruction of some existing BB and return the newly
1872    produced block.  */
1873 static basic_block
1874 emit_to_new_bb_before (rtx seq, rtx insn)
1875 {
1876   rtx last;
1877   basic_block bb;
1878   edge e;
1879   edge_iterator ei;
1880
1881   /* If there happens to be a fallthru edge (possibly created by cleanup_cfg
1882      call), we don't want it to go into newly created landing pad or other EH
1883      construct.  */
1884   for (ei = ei_start (BLOCK_FOR_INSN (insn)->preds); (e = ei_safe_edge (ei)); )
1885     if (e->flags & EDGE_FALLTHRU)
1886       force_nonfallthru (e);
1887     else
1888       ei_next (&ei);
1889   last = emit_insn_before (seq, insn);
1890   if (BARRIER_P (last))
1891     last = PREV_INSN (last);
1892   bb = create_basic_block (seq, last, BLOCK_FOR_INSN (insn)->prev_bb);
1893   update_bb_for_insn (bb);
1894   bb->flags |= BB_SUPERBLOCK;
1895   return bb;
1896 }
1897
1898 /* Generate the code to actually handle exceptions, which will follow the
1899    landing pads.  */
1900
1901 static void
1902 build_post_landing_pads (void)
1903 {
1904   int i;
1905
1906   for (i = cfun->eh->last_region_number; i > 0; --i)
1907     {
1908       struct eh_region_d *region;
1909       rtx seq;
1910
1911       region = VEC_index (eh_region, cfun->eh->region_array, i);
1912       /* Mind we don't process a region more than once.  */
1913       if (!region || region->region_number != i)
1914         continue;
1915
1916       switch (region->type)
1917         {
1918         case ERT_TRY:
1919           /* It is possible that TRY region is kept alive only because some of
1920              contained catch region still have RESX instruction but they are
1921              reached via their copies.  In this case we need to do nothing.  */
1922           if (!region->u.eh_try.eh_catch->label)
1923             break;
1924
1925           /* ??? Collect the set of all non-overlapping catch handlers
1926                all the way up the chain until blocked by a cleanup.  */
1927           /* ??? Outer try regions can share landing pads with inner
1928              try regions if the types are completely non-overlapping,
1929              and there are no intervening cleanups.  */
1930
1931           region->post_landing_pad = gen_label_rtx ();
1932
1933           start_sequence ();
1934
1935           emit_label (region->post_landing_pad);
1936
1937           /* ??? It is mighty inconvenient to call back into the
1938              switch statement generation code in expand_end_case.
1939              Rapid prototyping sez a sequence of ifs.  */
1940           {
1941             struct eh_region_d *c;
1942             for (c = region->u.eh_try.eh_catch; c ; c = c->u.eh_catch.next_catch)
1943               {
1944                 if (c->u.eh_catch.type_list == NULL)
1945                   emit_jump (c->label);
1946                 else
1947                   {
1948                     /* Need for one cmp/jump per type caught. Each type
1949                        list entry has a matching entry in the filter list
1950                        (see assign_filter_values).  */
1951                     tree tp_node = c->u.eh_catch.type_list;
1952                     tree flt_node = c->u.eh_catch.filter_list;
1953
1954                     for (; tp_node; )
1955                       {
1956                         emit_cmp_and_jump_insns
1957                           (crtl->eh.filter,
1958                            GEN_INT (tree_low_cst (TREE_VALUE (flt_node), 0)),
1959                            EQ, NULL_RTX,
1960                            targetm.eh_return_filter_mode (), 0, c->label);
1961
1962                         tp_node = TREE_CHAIN (tp_node);
1963                         flt_node = TREE_CHAIN (flt_node);
1964                       }
1965                   }
1966               }
1967           }
1968
1969           /* We delay the generation of the _Unwind_Resume until we generate
1970              landing pads.  We emit a marker here so as to get good control
1971              flow data in the meantime.  */
1972           region->resume
1973             = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
1974           emit_barrier ();
1975
1976           seq = get_insns ();
1977           end_sequence ();
1978
1979           emit_to_new_bb_before (seq, region->u.eh_try.eh_catch->label);
1980
1981           break;
1982
1983         case ERT_ALLOWED_EXCEPTIONS:
1984           if (!region->label)
1985             break;
1986           region->post_landing_pad = gen_label_rtx ();
1987
1988           start_sequence ();
1989
1990           emit_label (region->post_landing_pad);
1991
1992           emit_cmp_and_jump_insns (crtl->eh.filter,
1993                                    GEN_INT (region->u.allowed.filter),
1994                                    EQ, NULL_RTX,
1995                                    targetm.eh_return_filter_mode (), 0, region->label);
1996
1997           /* We delay the generation of the _Unwind_Resume until we generate
1998              landing pads.  We emit a marker here so as to get good control
1999              flow data in the meantime.  */
2000           region->resume
2001             = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
2002           emit_barrier ();
2003
2004           seq = get_insns ();
2005           end_sequence ();
2006
2007           emit_to_new_bb_before (seq, region->label);
2008           break;
2009
2010         case ERT_CLEANUP:
2011         case ERT_MUST_NOT_THROW:
2012           region->post_landing_pad = region->label;
2013           break;
2014
2015         case ERT_CATCH:
2016         case ERT_THROW:
2017           /* Nothing to do.  */
2018           break;
2019
2020         default:
2021           gcc_unreachable ();
2022         }
2023     }
2024 }
2025
2026 /* Replace RESX patterns with jumps to the next handler if any, or calls to
2027    _Unwind_Resume otherwise.  */
2028
2029 static void
2030 connect_post_landing_pads (void)
2031 {
2032   int i;
2033
2034   for (i = cfun->eh->last_region_number; i > 0; --i)
2035     {
2036       struct eh_region_d *region;
2037       struct eh_region_d *outer;
2038       rtx seq;
2039       rtx barrier;
2040
2041       region = VEC_index (eh_region, cfun->eh->region_array, i);
2042       /* Mind we don't process a region more than once.  */
2043       if (!region || region->region_number != i)
2044         continue;
2045
2046       /* If there is no RESX, or it has been deleted by flow, there's
2047          nothing to fix up.  */
2048       if (! region->resume || INSN_DELETED_P (region->resume))
2049         continue;
2050
2051       /* Search for another landing pad in this function.  */
2052       for (outer = region->outer; outer ; outer = outer->outer)
2053         if (outer->post_landing_pad)
2054           break;
2055
2056       start_sequence ();
2057
2058       if (outer)
2059         {
2060           edge e;
2061           basic_block src, dest;
2062
2063           emit_jump (outer->post_landing_pad);
2064           src = BLOCK_FOR_INSN (region->resume);
2065           dest = BLOCK_FOR_INSN (outer->post_landing_pad);
2066           while (EDGE_COUNT (src->succs) > 0)
2067             remove_edge (EDGE_SUCC (src, 0));
2068           e = make_edge (src, dest, 0);
2069           e->probability = REG_BR_PROB_BASE;
2070           e->count = src->count;
2071         }
2072       else
2073         {
2074           emit_library_call (unwind_resume_libfunc, LCT_THROW,
2075                              VOIDmode, 1, crtl->eh.exc_ptr, ptr_mode);
2076
2077           /* What we just emitted was a throwing libcall, so it got a
2078              barrier automatically added after it.  If the last insn in
2079              the libcall sequence isn't the barrier, it's because the
2080              target emits multiple insns for a call, and there are insns
2081              after the actual call insn (which are redundant and would be
2082              optimized away).  The barrier is inserted exactly after the
2083              call insn, so let's go get that and delete the insns after
2084              it, because below we need the barrier to be the last insn in
2085              the sequence.  */
2086           delete_insns_since (NEXT_INSN (last_call_insn ()));
2087         }
2088
2089       seq = get_insns ();
2090       end_sequence ();
2091       barrier = emit_insn_before (seq, region->resume);
2092       /* Avoid duplicate barrier.  */
2093       gcc_assert (BARRIER_P (barrier));
2094       delete_insn (barrier);
2095       delete_insn (region->resume);
2096
2097       /* ??? From tree-ssa we can wind up with catch regions whose
2098          label is not instantiated, but whose resx is present.  Now
2099          that we've dealt with the resx, kill the region.  */
2100       if (region->label == NULL && region->type == ERT_CLEANUP)
2101         remove_eh_handler (region);
2102     }
2103 }
2104
2105 \f
2106 static void
2107 dw2_build_landing_pads (void)
2108 {
2109   int i;
2110
2111   for (i = cfun->eh->last_region_number; i > 0; --i)
2112     {
2113       struct eh_region_d *region;
2114       rtx seq;
2115       basic_block bb;
2116       edge e;
2117
2118       region = VEC_index (eh_region, cfun->eh->region_array, i);
2119       /* Mind we don't process a region more than once.  */
2120       if (!region || region->region_number != i)
2121         continue;
2122
2123       if (region->type != ERT_CLEANUP
2124           && region->type != ERT_TRY
2125           && region->type != ERT_ALLOWED_EXCEPTIONS)
2126         continue;
2127
2128       if (!region->post_landing_pad)
2129         continue;
2130
2131       start_sequence ();
2132
2133       region->landing_pad = gen_label_rtx ();
2134       emit_label (region->landing_pad);
2135
2136 #ifdef HAVE_exception_receiver
2137       if (HAVE_exception_receiver)
2138         emit_insn (gen_exception_receiver ());
2139       else
2140 #endif
2141 #ifdef HAVE_nonlocal_goto_receiver
2142         if (HAVE_nonlocal_goto_receiver)
2143           emit_insn (gen_nonlocal_goto_receiver ());
2144         else
2145 #endif
2146           { /* Nothing */ }
2147
2148       emit_move_insn (crtl->eh.exc_ptr,
2149                       gen_rtx_REG (ptr_mode, EH_RETURN_DATA_REGNO (0)));
2150       emit_move_insn (crtl->eh.filter,
2151                       gen_rtx_REG (targetm.eh_return_filter_mode (),
2152                                    EH_RETURN_DATA_REGNO (1)));
2153
2154       seq = get_insns ();
2155       end_sequence ();
2156
2157       bb = emit_to_new_bb_before (seq, region->post_landing_pad);
2158       e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
2159       e->count = bb->count;
2160       e->probability = REG_BR_PROB_BASE;
2161     }
2162 }
2163
2164 \f
2165 struct sjlj_lp_info
2166 {
2167   int directly_reachable;
2168   int action_index;
2169   int dispatch_index;
2170   int call_site_index;
2171 };
2172
2173 static bool
2174 sjlj_find_directly_reachable_regions (struct sjlj_lp_info *lp_info)
2175 {
2176   rtx insn;
2177   bool found_one = false;
2178
2179   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
2180     {
2181       struct eh_region_d *region;
2182       enum reachable_code rc;
2183       tree type_thrown;
2184       rtx note;
2185
2186       if (! INSN_P (insn))
2187         continue;
2188
2189       note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2190       if (!note || INTVAL (XEXP (note, 0)) <= 0)
2191         continue;
2192
2193       region = VEC_index (eh_region, cfun->eh->region_array, INTVAL (XEXP (note, 0)));
2194       if (!region)
2195         continue;
2196
2197       type_thrown = NULL_TREE;
2198       if (region->type == ERT_THROW)
2199         {
2200           type_thrown = region->u.eh_throw.type;
2201           region = region->outer;
2202         }
2203
2204       /* Find the first containing region that might handle the exception.
2205          That's the landing pad to which we will transfer control.  */
2206       rc = RNL_NOT_CAUGHT;
2207       for (; region; region = region->outer)
2208         {
2209           rc = reachable_next_level (region, type_thrown, NULL, false);
2210           if (rc != RNL_NOT_CAUGHT)
2211             break;
2212         }
2213       if (rc == RNL_MAYBE_CAUGHT || rc == RNL_CAUGHT)
2214         {
2215           lp_info[region->region_number].directly_reachable = 1;
2216           found_one = true;
2217         }
2218     }
2219
2220   return found_one;
2221 }
2222
2223 static void
2224 sjlj_assign_call_site_values (rtx dispatch_label, struct sjlj_lp_info *lp_info)
2225 {
2226   htab_t ar_hash;
2227   int i, index;
2228
2229   /* First task: build the action table.  */
2230
2231   VARRAY_UCHAR_INIT (crtl->eh.action_record_data, 64, "action_record_data");
2232   ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
2233
2234   for (i = cfun->eh->last_region_number; i > 0; --i)
2235     if (lp_info[i].directly_reachable)
2236       {
2237         struct eh_region_d *r =
2238           VEC_index (eh_region, cfun->eh->region_array, i);
2239
2240         r->landing_pad = dispatch_label;
2241         lp_info[i].action_index = collect_one_action_chain (ar_hash, r);
2242         if (lp_info[i].action_index != -1)
2243           crtl->uses_eh_lsda = 1;
2244       }
2245
2246   htab_delete (ar_hash);
2247
2248   /* Next: assign dispatch values.  In dwarf2 terms, this would be the
2249      landing pad label for the region.  For sjlj though, there is one
2250      common landing pad from which we dispatch to the post-landing pads.
2251
2252      A region receives a dispatch index if it is directly reachable
2253      and requires in-function processing.  Regions that share post-landing
2254      pads may share dispatch indices.  */
2255   /* ??? Post-landing pad sharing doesn't actually happen at the moment
2256      (see build_post_landing_pads) so we don't bother checking for it.  */
2257
2258   index = 0;
2259   for (i = cfun->eh->last_region_number; i > 0; --i)
2260     if (lp_info[i].directly_reachable)
2261       lp_info[i].dispatch_index = index++;
2262
2263   /* Finally: assign call-site values.  If dwarf2 terms, this would be
2264      the region number assigned by convert_to_eh_region_ranges, but
2265      handles no-action and must-not-throw differently.  */
2266
2267   call_site_base = 1;
2268   for (i = cfun->eh->last_region_number; i > 0; --i)
2269     if (lp_info[i].directly_reachable)
2270       {
2271         int action = lp_info[i].action_index;
2272
2273         /* Map must-not-throw to otherwise unused call-site index 0.  */
2274         if (action == -2)
2275           index = 0;
2276         /* Map no-action to otherwise unused call-site index -1.  */
2277         else if (action == -1)
2278           index = -1;
2279         /* Otherwise, look it up in the table.  */
2280         else
2281           index = add_call_site (GEN_INT (lp_info[i].dispatch_index), action);
2282
2283         lp_info[i].call_site_index = index;
2284       }
2285 }
2286
2287 static void
2288 sjlj_mark_call_sites (struct sjlj_lp_info *lp_info)
2289 {
2290   int last_call_site = -2;
2291   rtx insn, mem;
2292
2293   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
2294     {
2295       struct eh_region_d *region;
2296       int this_call_site;
2297       rtx note, before, p;
2298
2299       /* Reset value tracking at extended basic block boundaries.  */
2300       if (LABEL_P (insn))
2301         last_call_site = -2;
2302
2303       if (! INSN_P (insn))
2304         continue;
2305
2306       note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2307
2308       /* Calls that are known to not throw need not be marked.  */
2309       if (note && INTVAL (XEXP (note, 0)) <= 0)
2310         continue;
2311
2312       if (note)
2313         region = VEC_index (eh_region, cfun->eh->region_array, INTVAL (XEXP (note, 0)));
2314       else
2315         region = NULL;
2316
2317       if (!region)
2318         {
2319           /* Calls (and trapping insns) without notes are outside any
2320              exception handling region in this function.  Mark them as
2321              no action.  */
2322           if (CALL_P (insn)
2323               || (flag_non_call_exceptions
2324                   && may_trap_p (PATTERN (insn))))
2325             this_call_site = -1;
2326           else
2327             continue;
2328         }
2329       else
2330         this_call_site = lp_info[region->region_number].call_site_index;
2331
2332       if (this_call_site == last_call_site)
2333         continue;
2334
2335       /* Don't separate a call from it's argument loads.  */
2336       before = insn;
2337       if (CALL_P (insn))
2338         before = find_first_parameter_load (insn, NULL_RTX);
2339
2340       start_sequence ();
2341       mem = adjust_address (crtl->eh.sjlj_fc, TYPE_MODE (integer_type_node),
2342                             sjlj_fc_call_site_ofs);
2343       emit_move_insn (mem, GEN_INT (this_call_site));
2344       p = get_insns ();
2345       end_sequence ();
2346
2347       emit_insn_before (p, before);
2348       last_call_site = this_call_site;
2349     }
2350 }
2351
2352 /* Construct the SjLj_Function_Context.  */
2353
2354 static void
2355 sjlj_emit_function_enter (rtx dispatch_label)
2356 {
2357   rtx fn_begin, fc, mem, seq;
2358   bool fn_begin_outside_block;
2359
2360   fc = crtl->eh.sjlj_fc;
2361
2362   start_sequence ();
2363
2364   /* We're storing this libcall's address into memory instead of
2365      calling it directly.  Thus, we must call assemble_external_libcall
2366      here, as we can not depend on emit_library_call to do it for us.  */
2367   assemble_external_libcall (eh_personality_libfunc);
2368   mem = adjust_address (fc, Pmode, sjlj_fc_personality_ofs);
2369   emit_move_insn (mem, eh_personality_libfunc);
2370
2371   mem = adjust_address (fc, Pmode, sjlj_fc_lsda_ofs);
2372   if (crtl->uses_eh_lsda)
2373     {
2374       char buf[20];
2375       rtx sym;
2376
2377       ASM_GENERATE_INTERNAL_LABEL (buf, "LLSDA", current_function_funcdef_no);
2378       sym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
2379       SYMBOL_REF_FLAGS (sym) = SYMBOL_FLAG_LOCAL;
2380       emit_move_insn (mem, sym);
2381     }
2382   else
2383     emit_move_insn (mem, const0_rtx);
2384
2385 #ifdef DONT_USE_BUILTIN_SETJMP
2386   {
2387     rtx x;
2388     x = emit_library_call_value (setjmp_libfunc, NULL_RTX, LCT_RETURNS_TWICE,
2389                                  TYPE_MODE (integer_type_node), 1,
2390                                  plus_constant (XEXP (fc, 0),
2391                                                 sjlj_fc_jbuf_ofs), Pmode);
2392
2393     emit_cmp_and_jump_insns (x, const0_rtx, NE, 0,
2394                              TYPE_MODE (integer_type_node), 0, dispatch_label);
2395     add_reg_br_prob_note (get_insns (), REG_BR_PROB_BASE/100);
2396   }
2397 #else
2398   expand_builtin_setjmp_setup (plus_constant (XEXP (fc, 0), sjlj_fc_jbuf_ofs),
2399                                dispatch_label);
2400 #endif
2401
2402   emit_library_call (unwind_sjlj_register_libfunc, LCT_NORMAL, VOIDmode,
2403                      1, XEXP (fc, 0), Pmode);
2404
2405   seq = get_insns ();
2406   end_sequence ();
2407
2408   /* ??? Instead of doing this at the beginning of the function,
2409      do this in a block that is at loop level 0 and dominates all
2410      can_throw_internal instructions.  */
2411
2412   fn_begin_outside_block = true;
2413   for (fn_begin = get_insns (); ; fn_begin = NEXT_INSN (fn_begin))
2414     if (NOTE_P (fn_begin))
2415       {
2416         if (NOTE_KIND (fn_begin) == NOTE_INSN_FUNCTION_BEG)
2417           break;
2418         else if (NOTE_INSN_BASIC_BLOCK_P (fn_begin))
2419           fn_begin_outside_block = false;
2420       }
2421
2422   if (fn_begin_outside_block)
2423     insert_insn_on_edge (seq, single_succ_edge (ENTRY_BLOCK_PTR));
2424   else
2425     emit_insn_after (seq, fn_begin);
2426 }
2427
2428 /* Call back from expand_function_end to know where we should put
2429    the call to unwind_sjlj_unregister_libfunc if needed.  */
2430
2431 void
2432 sjlj_emit_function_exit_after (rtx after)
2433 {
2434   crtl->eh.sjlj_exit_after = after;
2435 }
2436
2437 static void
2438 sjlj_emit_function_exit (void)
2439 {
2440   rtx seq, insn;
2441
2442   start_sequence ();
2443
2444   emit_library_call (unwind_sjlj_unregister_libfunc, LCT_NORMAL, VOIDmode,
2445                      1, XEXP (crtl->eh.sjlj_fc, 0), Pmode);
2446
2447   seq = get_insns ();
2448   end_sequence ();
2449
2450   /* ??? Really this can be done in any block at loop level 0 that
2451      post-dominates all can_throw_internal instructions.  This is
2452      the last possible moment.  */
2453
2454   insn = crtl->eh.sjlj_exit_after;
2455   if (LABEL_P (insn))
2456     insn = NEXT_INSN (insn);
2457
2458   emit_insn_after (seq, insn);
2459 }
2460
2461 static void
2462 sjlj_emit_dispatch_table (rtx dispatch_label, struct sjlj_lp_info *lp_info)
2463 {
2464   enum machine_mode unwind_word_mode = targetm.unwind_word_mode ();
2465   enum machine_mode filter_mode = targetm.eh_return_filter_mode ();
2466   int i, first_reachable;
2467   rtx mem, dispatch, seq, fc;
2468   rtx before;
2469   basic_block bb;
2470   edge e;
2471
2472   fc = crtl->eh.sjlj_fc;
2473
2474   start_sequence ();
2475
2476   emit_label (dispatch_label);
2477
2478 #ifndef DONT_USE_BUILTIN_SETJMP
2479   expand_builtin_setjmp_receiver (dispatch_label);
2480 #endif
2481
2482   /* Load up dispatch index, exc_ptr and filter values from the
2483      function context.  */
2484   mem = adjust_address (fc, TYPE_MODE (integer_type_node),
2485                         sjlj_fc_call_site_ofs);
2486   dispatch = copy_to_reg (mem);
2487
2488   mem = adjust_address (fc, unwind_word_mode, sjlj_fc_data_ofs);
2489   if (unwind_word_mode != ptr_mode)
2490     {
2491 #ifdef POINTERS_EXTEND_UNSIGNED
2492       mem = convert_memory_address (ptr_mode, mem);
2493 #else
2494       mem = convert_to_mode (ptr_mode, mem, 0);
2495 #endif
2496     }
2497   emit_move_insn (crtl->eh.exc_ptr, mem);
2498
2499   mem = adjust_address (fc, unwind_word_mode,
2500                         sjlj_fc_data_ofs + GET_MODE_SIZE (unwind_word_mode));
2501   if (unwind_word_mode != filter_mode)
2502     mem = convert_to_mode (filter_mode, mem, 0);
2503   emit_move_insn (crtl->eh.filter, mem);
2504
2505   /* Jump to one of the directly reachable regions.  */
2506   /* ??? This really ought to be using a switch statement.  */
2507
2508   first_reachable = 0;
2509   for (i = cfun->eh->last_region_number; i > 0; --i)
2510     {
2511       if (! lp_info[i].directly_reachable)
2512         continue;
2513
2514       if (! first_reachable)
2515         {
2516           first_reachable = i;
2517           continue;
2518         }
2519
2520       emit_cmp_and_jump_insns (dispatch, GEN_INT (lp_info[i].dispatch_index),
2521                                EQ, NULL_RTX, TYPE_MODE (integer_type_node), 0,
2522                                (((struct eh_region_d *)
2523                                  VEC_index (eh_region,
2524                                             cfun->eh->region_array, i))
2525                                 ->post_landing_pad));
2526     }
2527
2528   seq = get_insns ();
2529   end_sequence ();
2530
2531   before = (((struct eh_region_d *)
2532              VEC_index (eh_region, cfun->eh->region_array, first_reachable))
2533             ->post_landing_pad);
2534
2535   bb = emit_to_new_bb_before (seq, before);
2536   e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
2537   e->count = bb->count;
2538   e->probability = REG_BR_PROB_BASE;
2539 }
2540
2541 static void
2542 sjlj_build_landing_pads (void)
2543 {
2544   struct sjlj_lp_info *lp_info;
2545
2546   lp_info = XCNEWVEC (struct sjlj_lp_info, cfun->eh->last_region_number + 1);
2547
2548   if (sjlj_find_directly_reachable_regions (lp_info))
2549     {
2550       rtx dispatch_label = gen_label_rtx ();
2551       int align = STACK_SLOT_ALIGNMENT (sjlj_fc_type_node,
2552                                         TYPE_MODE (sjlj_fc_type_node),
2553                                         TYPE_ALIGN (sjlj_fc_type_node));
2554       crtl->eh.sjlj_fc
2555         = assign_stack_local (TYPE_MODE (sjlj_fc_type_node),
2556                               int_size_in_bytes (sjlj_fc_type_node),
2557                               align);
2558
2559       sjlj_assign_call_site_values (dispatch_label, lp_info);
2560       sjlj_mark_call_sites (lp_info);
2561
2562       sjlj_emit_function_enter (dispatch_label);
2563       sjlj_emit_dispatch_table (dispatch_label, lp_info);
2564       sjlj_emit_function_exit ();
2565     }
2566
2567   free (lp_info);
2568 }
2569
2570 /* After initial rtl generation, call back to finish generating
2571    exception support code.  */
2572
2573 static void
2574 finish_eh_generation (void)
2575 {
2576   basic_block bb;
2577
2578   /* Nothing to do if no regions created.  */
2579   if (cfun->eh->region_tree == NULL)
2580     return;
2581
2582   /* The object here is to provide detailed information (via
2583      reachable_handlers) on how exception control flows within the
2584      function for the CFG construction.  In this first pass, we can
2585      include type information garnered from ERT_THROW and
2586      ERT_ALLOWED_EXCEPTIONS regions, and hope that it will be useful
2587      in deleting unreachable handlers.  Subsequently, we will generate
2588      landing pads which will connect many of the handlers, and then
2589      type information will not be effective.  Still, this is a win
2590      over previous implementations.  */
2591
2592   /* These registers are used by the landing pads.  Make sure they
2593      have been generated.  */
2594   get_exception_pointer ();
2595   get_exception_filter ();
2596
2597   /* Construct the landing pads.  */
2598
2599   assign_filter_values ();
2600   build_post_landing_pads ();
2601   connect_post_landing_pads ();
2602   if (USING_SJLJ_EXCEPTIONS)
2603     sjlj_build_landing_pads ();
2604   else
2605     dw2_build_landing_pads ();
2606
2607   crtl->eh.built_landing_pads = 1;
2608
2609   /* We've totally changed the CFG.  Start over.  */
2610   find_exception_handler_labels ();
2611   break_superblocks ();
2612   if (USING_SJLJ_EXCEPTIONS
2613       /* Kludge for Alpha/Tru64 (see alpha_gp_save_rtx).  */
2614       || single_succ_edge (ENTRY_BLOCK_PTR)->insns.r)
2615     commit_edge_insertions ();
2616   FOR_EACH_BB (bb)
2617     {
2618       edge e;
2619       edge_iterator ei;
2620       bool eh = false;
2621       for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2622         {
2623           if (e->flags & EDGE_EH)
2624             {
2625               remove_edge (e);
2626               eh = true;
2627             }
2628           else
2629             ei_next (&ei);
2630         }
2631       if (eh)
2632         rtl_make_eh_edge (NULL, bb, BB_END (bb));
2633     }
2634 }
2635 \f
2636 /* This section handles removing dead code for flow.  */
2637
2638 /* Splice REGION from the region tree and replace it by REPLACE etc.
2639    When UPDATE_CATCH_TRY is true mind updating links from catch to try
2640    region.*/
2641
2642 static void
2643 remove_eh_handler_and_replace (struct eh_region_d *region,
2644                                struct eh_region_d *replace,
2645                                bool update_catch_try)
2646 {
2647   struct eh_region_d **pp, **pp_start, *p, *outer, *inner;
2648   rtx lab;
2649
2650   outer = region->outer;
2651
2652   /* For the benefit of efficiently handling REG_EH_REGION notes,
2653      replace this region in the region array with its containing
2654      region.  Note that previous region deletions may result in
2655      multiple copies of this region in the array, so we have a
2656      list of alternate numbers by which we are known.  */
2657
2658   VEC_replace (eh_region, cfun->eh->region_array, region->region_number,
2659                replace);
2660   if (region->aka)
2661     {
2662       unsigned i;
2663       bitmap_iterator bi;
2664
2665       EXECUTE_IF_SET_IN_BITMAP (region->aka, 0, i, bi)
2666         {
2667           VEC_replace (eh_region, cfun->eh->region_array, i, replace);
2668         }
2669     }
2670
2671   if (replace)
2672     {
2673       if (!replace->aka)
2674         replace->aka = BITMAP_GGC_ALLOC ();
2675       if (region->aka)
2676         bitmap_ior_into (replace->aka, region->aka);
2677       bitmap_set_bit (replace->aka, region->region_number);
2678     }
2679
2680   if (crtl->eh.built_landing_pads)
2681     lab = region->landing_pad;
2682   else
2683     lab = region->label;
2684   if (outer)
2685     pp_start = &outer->inner;
2686   else
2687     pp_start = &cfun->eh->region_tree;
2688   for (pp = pp_start, p = *pp; p != region; pp = &p->next_peer, p = *pp)
2689     continue;
2690   *pp = region->next_peer;
2691
2692   if (replace)
2693     pp_start = &replace->inner;
2694   else
2695     pp_start = &cfun->eh->region_tree;
2696   inner = region->inner;
2697   if (inner)
2698     {
2699       for (p = inner; p->next_peer ; p = p->next_peer)
2700         p->outer = replace;
2701       p->outer = replace;
2702
2703       p->next_peer = *pp_start;
2704       *pp_start = inner;
2705     }
2706
2707   if (region->type == ERT_CATCH
2708       && update_catch_try)
2709     {
2710       struct eh_region_d *eh_try, *next, *prev;
2711
2712       for (eh_try = region->next_peer;
2713            eh_try->type == ERT_CATCH;
2714            eh_try = eh_try->next_peer)
2715         continue;
2716       gcc_assert (eh_try->type == ERT_TRY);
2717
2718       next = region->u.eh_catch.next_catch;
2719       prev = region->u.eh_catch.prev_catch;
2720
2721       if (next)
2722         next->u.eh_catch.prev_catch = prev;
2723       else
2724         eh_try->u.eh_try.last_catch = prev;
2725       if (prev)
2726         prev->u.eh_catch.next_catch = next;
2727       else
2728         {
2729           eh_try->u.eh_try.eh_catch = next;
2730           if (! next)
2731             remove_eh_handler (eh_try);
2732         }
2733     }
2734 }
2735
2736 /* Splice REGION from the region tree and replace it by the outer region
2737    etc.  */
2738
2739 static void
2740 remove_eh_handler (struct eh_region_d *region)
2741 {
2742   remove_eh_handler_and_replace (region, region->outer, true);
2743 }
2744
2745 /* Remove Eh region R that has turned out to have no code in its handler.  */
2746
2747 void
2748 remove_eh_region (int r)
2749 {
2750   struct eh_region_d *region;
2751
2752   region = VEC_index (eh_region, cfun->eh->region_array, r);
2753   remove_eh_handler (region);
2754 }
2755
2756 /* Remove Eh region R that has turned out to have no code in its handler
2757    and replace in by R2.  */
2758
2759 void
2760 remove_eh_region_and_replace_by_outer_of (int r, int r2)
2761 {
2762   struct eh_region_d *region, *region2;
2763
2764   region = VEC_index (eh_region, cfun->eh->region_array, r);
2765   region2 = VEC_index (eh_region, cfun->eh->region_array, r2);
2766   remove_eh_handler_and_replace (region, region2->outer, true);
2767 }
2768
2769 /* Invokes CALLBACK for every exception handler label.  Only used by old
2770    loop hackery; should not be used by new code.  */
2771
2772 void
2773 for_each_eh_label (void (*callback) (rtx))
2774 {
2775   int i;
2776   for (i = 0; i < cfun->eh->last_region_number; i++)
2777     {
2778       struct eh_region_d *r = VEC_index (eh_region, cfun->eh->region_array, i);
2779       if (r && r->region_number == i && r->label
2780           && GET_CODE (r->label) == CODE_LABEL)
2781         (*callback) (r->label);
2782     }
2783 }
2784
2785 /* Invoke CALLBACK for every exception region in the current function.  */
2786
2787 void
2788 for_each_eh_region (void (*callback) (struct eh_region_d *))
2789 {
2790   int i, n = cfun->eh->last_region_number;
2791   for (i = 1; i <= n; ++i)
2792     {
2793       struct eh_region_d *region;
2794
2795       region = VEC_index (eh_region, cfun->eh->region_array, i);
2796       if (region)
2797         (*callback) (region);
2798     }
2799 }
2800 \f
2801 /* This section describes CFG exception edges for flow.  */
2802
2803 /* For communicating between calls to reachable_next_level.  */
2804 struct reachable_info
2805 {
2806   tree types_caught;
2807   tree types_allowed;
2808   void (*callback) (struct eh_region_d *, void *);
2809   void *callback_data;
2810 };
2811
2812 /* A subroutine of reachable_next_level.  Return true if TYPE, or a
2813    base class of TYPE, is in HANDLED.  */
2814
2815 static int
2816 check_handled (tree handled, tree type)
2817 {
2818   tree t;
2819
2820   /* We can check for exact matches without front-end help.  */
2821   if (! lang_eh_type_covers)
2822     {
2823       for (t = handled; t ; t = TREE_CHAIN (t))
2824         if (TREE_VALUE (t) == type)
2825           return 1;
2826     }
2827   else
2828     {
2829       for (t = handled; t ; t = TREE_CHAIN (t))
2830         if ((*lang_eh_type_covers) (TREE_VALUE (t), type))
2831           return 1;
2832     }
2833
2834   return 0;
2835 }
2836
2837 /* A subroutine of reachable_next_level.  If we are collecting a list
2838    of handlers, add one.  After landing pad generation, reference
2839    it instead of the handlers themselves.  Further, the handlers are
2840    all wired together, so by referencing one, we've got them all.
2841    Before landing pad generation we reference each handler individually.
2842
2843    LP_REGION contains the landing pad; REGION is the handler.  */
2844
2845 static void
2846 add_reachable_handler (struct reachable_info *info,
2847                        struct eh_region_d *lp_region,
2848                        struct eh_region_d *region)
2849 {
2850   if (! info)
2851     return;
2852
2853   if (crtl->eh.built_landing_pads)
2854     info->callback (lp_region, info->callback_data);
2855   else
2856     info->callback (region, info->callback_data);
2857 }
2858
2859 /* Process one level of exception regions for reachability.
2860    If TYPE_THROWN is non-null, then it is the *exact* type being
2861    propagated.  If INFO is non-null, then collect handler labels
2862    and caught/allowed type information between invocations.  */
2863
2864 static enum reachable_code
2865 reachable_next_level (struct eh_region_d *region, tree type_thrown,
2866                       struct reachable_info *info,
2867                       bool maybe_resx)
2868 {
2869   switch (region->type)
2870     {
2871     case ERT_CLEANUP:
2872       /* Before landing-pad generation, we model control flow
2873          directly to the individual handlers.  In this way we can
2874          see that catch handler types may shadow one another.  */
2875       add_reachable_handler (info, region, region);
2876       return RNL_MAYBE_CAUGHT;
2877
2878     case ERT_TRY:
2879       {
2880         struct eh_region_d *c;
2881         enum reachable_code ret = RNL_NOT_CAUGHT;
2882
2883         for (c = region->u.eh_try.eh_catch; c ; c = c->u.eh_catch.next_catch)
2884           {
2885             /* A catch-all handler ends the search.  */
2886             if (c->u.eh_catch.type_list == NULL)
2887               {
2888                 add_reachable_handler (info, region, c);
2889                 return RNL_CAUGHT;
2890               }
2891
2892             if (type_thrown)
2893               {
2894                 /* If we have at least one type match, end the search.  */
2895                 tree tp_node = c->u.eh_catch.type_list;
2896
2897                 for (; tp_node; tp_node = TREE_CHAIN (tp_node))
2898                   {
2899                     tree type = TREE_VALUE (tp_node);
2900
2901                     if (type == type_thrown
2902                         || (lang_eh_type_covers
2903                             && (*lang_eh_type_covers) (type, type_thrown)))
2904                       {
2905                         add_reachable_handler (info, region, c);
2906                         return RNL_CAUGHT;
2907                       }
2908                   }
2909
2910                 /* If we have definitive information of a match failure,
2911                    the catch won't trigger.  */
2912                 if (lang_eh_type_covers)
2913                   return RNL_NOT_CAUGHT;
2914               }
2915
2916             /* At this point, we either don't know what type is thrown or
2917                don't have front-end assistance to help deciding if it is
2918                covered by one of the types in the list for this region.
2919
2920                We'd then like to add this region to the list of reachable
2921                handlers since it is indeed potentially reachable based on the
2922                information we have.
2923
2924                Actually, this handler is for sure not reachable if all the
2925                types it matches have already been caught. That is, it is only
2926                potentially reachable if at least one of the types it catches
2927                has not been previously caught.  */
2928
2929             if (! info)
2930               ret = RNL_MAYBE_CAUGHT;
2931             else
2932               {
2933                 tree tp_node = c->u.eh_catch.type_list;
2934                 bool maybe_reachable = false;
2935
2936                 /* Compute the potential reachability of this handler and
2937                    update the list of types caught at the same time.  */
2938                 for (; tp_node; tp_node = TREE_CHAIN (tp_node))
2939                   {
2940                     tree type = TREE_VALUE (tp_node);
2941
2942                     if (! check_handled (info->types_caught, type))
2943                       {
2944                         info->types_caught
2945                           = tree_cons (NULL, type, info->types_caught);
2946
2947                         maybe_reachable = true;
2948                       }
2949                   }
2950
2951                 if (maybe_reachable)
2952                   {
2953                     add_reachable_handler (info, region, c);
2954
2955                     /* ??? If the catch type is a base class of every allowed
2956                        type, then we know we can stop the search.  */
2957                     ret = RNL_MAYBE_CAUGHT;
2958                   }
2959               }
2960           }
2961
2962         return ret;
2963       }
2964
2965     case ERT_ALLOWED_EXCEPTIONS:
2966       /* An empty list of types definitely ends the search.  */
2967       if (region->u.allowed.type_list == NULL_TREE)
2968         {
2969           add_reachable_handler (info, region, region);
2970           return RNL_CAUGHT;
2971         }
2972
2973       /* Collect a list of lists of allowed types for use in detecting
2974          when a catch may be transformed into a catch-all.  */
2975       if (info)
2976         info->types_allowed = tree_cons (NULL_TREE,
2977                                          region->u.allowed.type_list,
2978                                          info->types_allowed);
2979
2980       /* If we have definitive information about the type hierarchy,
2981          then we can tell if the thrown type will pass through the
2982          filter.  */
2983       if (type_thrown && lang_eh_type_covers)
2984         {
2985           if (check_handled (region->u.allowed.type_list, type_thrown))
2986             return RNL_NOT_CAUGHT;
2987           else
2988             {
2989               add_reachable_handler (info, region, region);
2990               return RNL_CAUGHT;
2991             }
2992         }
2993
2994       add_reachable_handler (info, region, region);
2995       return RNL_MAYBE_CAUGHT;
2996
2997     case ERT_CATCH:
2998       /* Catch regions are handled by their controlling try region.  */
2999       return RNL_NOT_CAUGHT;
3000
3001     case ERT_MUST_NOT_THROW:
3002       /* Here we end our search, since no exceptions may propagate.
3003         
3004          Local landing pads of ERT_MUST_NOT_THROW instructions are reachable
3005          only via locally handled RESX instructions.  
3006
3007          When we inline a function call, we can bring in new handlers.  In order
3008          to avoid ERT_MUST_NOT_THROW landing pads from being deleted as unreachable
3009          assume that such handlers exists prior for any inlinable call prior
3010          inlining decisions are fixed.  */
3011
3012       if (maybe_resx)
3013         {
3014           add_reachable_handler (info, region, region);
3015           return RNL_CAUGHT;
3016         }
3017       else
3018         return RNL_BLOCKED;
3019
3020     case ERT_THROW:
3021     case ERT_UNKNOWN:
3022       /* Shouldn't see these here.  */
3023       gcc_unreachable ();
3024       break;
3025     default:
3026       gcc_unreachable ();
3027     }
3028 }
3029
3030 /* Invoke CALLBACK on each region reachable from REGION_NUMBER.  */
3031
3032 void
3033 foreach_reachable_handler (int region_number, bool is_resx, bool inlinable_call,
3034                            void (*callback) (struct eh_region_d *, void *),
3035                            void *callback_data)
3036 {
3037   struct reachable_info info;
3038   struct eh_region_d *region;
3039   tree type_thrown;
3040
3041   memset (&info, 0, sizeof (info));
3042   info.callback = callback;
3043   info.callback_data = callback_data;
3044
3045   region = VEC_index (eh_region, cfun->eh->region_array, region_number);
3046   if (!region)
3047     return;
3048
3049   type_thrown = NULL_TREE;
3050   if (is_resx)
3051     {
3052       /* A RESX leaves a region instead of entering it.  Thus the
3053          region itself may have been deleted out from under us.  */
3054       if (region == NULL)
3055         return;
3056       region = region->outer;
3057     }
3058   else if (region->type == ERT_THROW)
3059     {
3060       type_thrown = region->u.eh_throw.type;
3061       region = region->outer;
3062     }
3063
3064   while (region)
3065     {
3066       if (reachable_next_level (region, type_thrown, &info,
3067                                 inlinable_call || is_resx) >= RNL_CAUGHT)
3068         break;
3069       /* If we have processed one cleanup, there is no point in
3070          processing any more of them.  Each cleanup will have an edge
3071          to the next outer cleanup region, so the flow graph will be
3072          accurate.  */
3073       if (region->type == ERT_CLEANUP)
3074         {
3075           enum reachable_code code = RNL_NOT_CAUGHT;
3076           region = find_prev_try (region->outer);
3077           /* Continue looking for outer TRY region until we find one
3078              that might cath something.  */
3079           while (region
3080                  && (code = reachable_next_level (region, type_thrown, &info,
3081                                                   inlinable_call || is_resx))
3082                      == RNL_NOT_CAUGHT)
3083             region = find_prev_try (region->outer);
3084           if (code >= RNL_CAUGHT)
3085             break;
3086         }
3087       if (region)
3088         region = region->outer;
3089     }
3090 }
3091
3092 /* Retrieve a list of labels of exception handlers which can be
3093    reached by a given insn.  */
3094
3095 static void
3096 arh_to_landing_pad (struct eh_region_d *region, void *data)
3097 {
3098   rtx *p_handlers = (rtx *) data;
3099   if (! *p_handlers)
3100     *p_handlers = alloc_INSN_LIST (region->landing_pad, NULL_RTX);
3101 }
3102
3103 static void
3104 arh_to_label (struct eh_region_d *region, void *data)
3105 {
3106   rtx *p_handlers = (rtx *) data;
3107   *p_handlers = alloc_INSN_LIST (region->label, *p_handlers);
3108 }
3109
3110 rtx
3111 reachable_handlers (rtx insn)
3112 {
3113   bool is_resx = false;
3114   rtx handlers = NULL;
3115   int region_number;
3116
3117   if (JUMP_P (insn)
3118       && GET_CODE (PATTERN (insn)) == RESX)
3119     {
3120       region_number = XINT (PATTERN (insn), 0);
3121       is_resx = true;
3122     }
3123   else
3124     {
3125       rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3126       if (!note || INTVAL (XEXP (note, 0)) <= 0)
3127         return NULL;
3128       region_number = INTVAL (XEXP (note, 0));
3129     }
3130
3131   foreach_reachable_handler (region_number, is_resx, false,
3132                              (crtl->eh.built_landing_pads
3133                               ? arh_to_landing_pad
3134                               : arh_to_label),
3135                              &handlers);
3136
3137   return handlers;
3138 }
3139
3140 /* Determine if the given INSN can throw an exception that is caught
3141    within the function.  */
3142
3143 bool
3144 can_throw_internal_1 (int region_number, bool is_resx, bool inlinable_call)
3145 {
3146   struct eh_region_d *region;
3147   tree type_thrown;
3148
3149   region = VEC_index (eh_region, cfun->eh->region_array, region_number);
3150   if (!region)
3151     return false;
3152
3153   type_thrown = NULL_TREE;
3154   if (is_resx)
3155     region = region->outer;
3156   else if (region->type == ERT_THROW)
3157     {
3158       type_thrown = region->u.eh_throw.type;
3159       region = region->outer;
3160     }
3161
3162   /* If this exception is ignored by each and every containing region,
3163      then control passes straight out.  The runtime may handle some
3164      regions, which also do not require processing internally.  */
3165   for (; region; region = region->outer)
3166     {
3167       enum reachable_code how = reachable_next_level (region, type_thrown, 0,
3168                                                       inlinable_call || is_resx);
3169       if (how == RNL_BLOCKED)
3170         return false;
3171       if (how != RNL_NOT_CAUGHT)
3172         return true;
3173     }
3174
3175   return false;
3176 }
3177
3178 bool
3179 can_throw_internal (const_rtx insn)
3180 {
3181   rtx note;
3182
3183   if (! INSN_P (insn))
3184     return false;
3185
3186   if (JUMP_P (insn)
3187       && GET_CODE (PATTERN (insn)) == RESX
3188       && XINT (PATTERN (insn), 0) > 0)
3189     return can_throw_internal_1 (XINT (PATTERN (insn), 0), true, false);
3190
3191   if (NONJUMP_INSN_P (insn)
3192       && GET_CODE (PATTERN (insn)) == SEQUENCE)
3193     insn = XVECEXP (PATTERN (insn), 0, 0);
3194
3195   /* Every insn that might throw has an EH_REGION note.  */
3196   note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3197   if (!note || INTVAL (XEXP (note, 0)) <= 0)
3198     return false;
3199
3200   return can_throw_internal_1 (INTVAL (XEXP (note, 0)), false, false);
3201 }
3202
3203 /* Determine if the given INSN can throw an exception that is
3204    visible outside the function.  */
3205
3206 bool
3207 can_throw_external_1 (int region_number, bool is_resx, bool inlinable_call)
3208 {
3209   struct eh_region_d *region;
3210   tree type_thrown;
3211
3212   region = VEC_index (eh_region, cfun->eh->region_array, region_number);
3213   if (!region)
3214     return true;
3215
3216   type_thrown = NULL_TREE;
3217   if (is_resx)
3218     region = region->outer;
3219   else if (region->type == ERT_THROW)
3220     {
3221       type_thrown = region->u.eh_throw.type;
3222       region = region->outer;
3223     }
3224
3225   /* If the exception is caught or blocked by any containing region,
3226      then it is not seen by any calling function.  */
3227   for (; region ; region = region->outer)
3228     if (reachable_next_level (region, type_thrown, NULL,
3229         inlinable_call || is_resx) >= RNL_CAUGHT)
3230       return false;
3231
3232   return true;
3233 }
3234
3235 bool
3236 can_throw_external (const_rtx insn)
3237 {
3238   rtx note;
3239
3240   if (! INSN_P (insn))
3241     return false;
3242
3243   if (JUMP_P (insn)
3244       && GET_CODE (PATTERN (insn)) == RESX
3245       && XINT (PATTERN (insn), 0) > 0)
3246     return can_throw_external_1 (XINT (PATTERN (insn), 0), true, false);
3247
3248   if (NONJUMP_INSN_P (insn)
3249       && GET_CODE (PATTERN (insn)) == SEQUENCE)
3250     {
3251       rtx seq = PATTERN (insn);
3252       int i, n = XVECLEN (seq, 0);
3253
3254       for (i = 0; i < n; i++)
3255         if (can_throw_external (XVECEXP (seq, 0, i)))
3256           return true;
3257
3258       return false;
3259     }
3260
3261   note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3262   if (!note)
3263     {
3264       /* Calls (and trapping insns) without notes are outside any
3265          exception handling region in this function.  We have to
3266          assume it might throw.  Given that the front end and middle
3267          ends mark known NOTHROW functions, this isn't so wildly
3268          inaccurate.  */
3269       return (CALL_P (insn)
3270               || (flag_non_call_exceptions
3271                   && may_trap_p (PATTERN (insn))));
3272     }
3273   if (INTVAL (XEXP (note, 0)) <= 0)
3274     return false;
3275
3276   return can_throw_external_1 (INTVAL (XEXP (note, 0)), false, false);
3277 }
3278
3279 /* Set TREE_NOTHROW and crtl->all_throwers_are_sibcalls.  */
3280
3281 unsigned int
3282 set_nothrow_function_flags (void)
3283 {
3284   rtx insn;
3285
3286   crtl->nothrow = 1;
3287
3288   /* Assume crtl->all_throwers_are_sibcalls until we encounter
3289      something that can throw an exception.  We specifically exempt
3290      CALL_INSNs that are SIBLING_CALL_P, as these are really jumps,
3291      and can't throw.  Most CALL_INSNs are not SIBLING_CALL_P, so this
3292      is optimistic.  */
3293
3294   crtl->all_throwers_are_sibcalls = 1;
3295
3296   /* If we don't know that this implementation of the function will
3297      actually be used, then we must not set TREE_NOTHROW, since
3298      callers must not assume that this function does not throw.  */
3299   if (TREE_NOTHROW (current_function_decl))
3300     return 0;
3301
3302   if (! flag_exceptions)
3303     return 0;
3304
3305   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3306     if (can_throw_external (insn))
3307       {
3308         crtl->nothrow = 0;
3309
3310         if (!CALL_P (insn) || !SIBLING_CALL_P (insn))
3311           {
3312             crtl->all_throwers_are_sibcalls = 0;
3313             return 0;
3314           }
3315       }
3316
3317   for (insn = crtl->epilogue_delay_list; insn;
3318        insn = XEXP (insn, 1))
3319     if (can_throw_external (insn))
3320       {
3321         crtl->nothrow = 0;
3322
3323         if (!CALL_P (insn) || !SIBLING_CALL_P (insn))
3324           {
3325             crtl->all_throwers_are_sibcalls = 0;
3326             return 0;
3327           }
3328       }
3329   if (crtl->nothrow
3330       && (cgraph_function_body_availability (cgraph_node
3331                                              (current_function_decl))
3332           >= AVAIL_AVAILABLE))
3333     {
3334       struct cgraph_node *node = cgraph_node (current_function_decl);
3335       struct cgraph_edge *e;
3336       for (e = node->callers; e; e = e->next_caller)
3337         e->can_throw_external = false;
3338       TREE_NOTHROW (current_function_decl) = 1;
3339
3340       if (dump_file)
3341         fprintf (dump_file, "Marking function nothrow: %s\n\n",
3342                  current_function_name ());
3343     }
3344   return 0;
3345 }
3346
3347 struct rtl_opt_pass pass_set_nothrow_function_flags =
3348 {
3349  {
3350   RTL_PASS,
3351   "nothrow",                            /* name */
3352   NULL,                                 /* gate */
3353   set_nothrow_function_flags,           /* execute */
3354   NULL,                                 /* sub */
3355   NULL,                                 /* next */
3356   0,                                    /* static_pass_number */
3357   TV_NONE,                              /* tv_id */
3358   0,                                    /* properties_required */
3359   0,                                    /* properties_provided */
3360   0,                                    /* properties_destroyed */
3361   0,                                    /* todo_flags_start */
3362   TODO_dump_func,                       /* todo_flags_finish */
3363  }
3364 };
3365
3366 \f
3367 /* Various hooks for unwind library.  */
3368
3369 /* Do any necessary initialization to access arbitrary stack frames.
3370    On the SPARC, this means flushing the register windows.  */
3371
3372 void
3373 expand_builtin_unwind_init (void)
3374 {
3375   /* Set this so all the registers get saved in our frame; we need to be
3376      able to copy the saved values for any registers from frames we unwind.  */
3377   crtl->saves_all_registers = 1;
3378
3379 #ifdef SETUP_FRAME_ADDRESSES
3380   SETUP_FRAME_ADDRESSES ();
3381 #endif
3382 }
3383
3384 rtx
3385 expand_builtin_eh_return_data_regno (tree exp)
3386 {
3387   tree which = CALL_EXPR_ARG (exp, 0);
3388   unsigned HOST_WIDE_INT iwhich;
3389
3390   if (TREE_CODE (which) != INTEGER_CST)
3391     {
3392       error ("argument of %<__builtin_eh_return_regno%> must be constant");
3393       return constm1_rtx;
3394     }
3395
3396   iwhich = tree_low_cst (which, 1);
3397   iwhich = EH_RETURN_DATA_REGNO (iwhich);
3398   if (iwhich == INVALID_REGNUM)
3399     return constm1_rtx;
3400
3401 #ifdef DWARF_FRAME_REGNUM
3402   iwhich = DWARF_FRAME_REGNUM (iwhich);
3403 #else
3404   iwhich = DBX_REGISTER_NUMBER (iwhich);
3405 #endif
3406
3407   return GEN_INT (iwhich);
3408 }
3409
3410 /* Given a value extracted from the return address register or stack slot,
3411    return the actual address encoded in that value.  */
3412
3413 rtx
3414 expand_builtin_extract_return_addr (tree addr_tree)
3415 {
3416   rtx addr = expand_expr (addr_tree, NULL_RTX, Pmode, EXPAND_NORMAL);
3417
3418   if (GET_MODE (addr) != Pmode
3419       && GET_MODE (addr) != VOIDmode)
3420     {
3421 #ifdef POINTERS_EXTEND_UNSIGNED
3422       addr = convert_memory_address (Pmode, addr);
3423 #else
3424       addr = convert_to_mode (Pmode, addr, 0);
3425 #endif
3426     }
3427
3428   /* First mask out any unwanted bits.  */
3429 #ifdef MASK_RETURN_ADDR
3430   expand_and (Pmode, addr, MASK_RETURN_ADDR, addr);
3431 #endif
3432
3433   /* Then adjust to find the real return address.  */
3434 #if defined (RETURN_ADDR_OFFSET)
3435   addr = plus_constant (addr, RETURN_ADDR_OFFSET);
3436 #endif
3437
3438   return addr;
3439 }
3440
3441 /* Given an actual address in addr_tree, do any necessary encoding
3442    and return the value to be stored in the return address register or
3443    stack slot so the epilogue will return to that address.  */
3444
3445 rtx
3446 expand_builtin_frob_return_addr (tree addr_tree)
3447 {
3448   rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, EXPAND_NORMAL);
3449
3450   addr = convert_memory_address (Pmode, addr);
3451
3452 #ifdef RETURN_ADDR_OFFSET
3453   addr = force_reg (Pmode, addr);
3454   addr = plus_constant (addr, -RETURN_ADDR_OFFSET);
3455 #endif
3456
3457   return addr;
3458 }
3459
3460 /* Set up the epilogue with the magic bits we'll need to return to the
3461    exception handler.  */
3462
3463 void
3464 expand_builtin_eh_return (tree stackadj_tree ATTRIBUTE_UNUSED,
3465                           tree handler_tree)
3466 {
3467   rtx tmp;
3468
3469 #ifdef EH_RETURN_STACKADJ_RTX
3470   tmp = expand_expr (stackadj_tree, crtl->eh.ehr_stackadj,
3471                      VOIDmode, EXPAND_NORMAL);
3472   tmp = convert_memory_address (Pmode, tmp);
3473   if (!crtl->eh.ehr_stackadj)
3474     crtl->eh.ehr_stackadj = copy_to_reg (tmp);
3475   else if (tmp != crtl->eh.ehr_stackadj)
3476     emit_move_insn (crtl->eh.ehr_stackadj, tmp);
3477 #endif
3478
3479   tmp = expand_expr (handler_tree, crtl->eh.ehr_handler,
3480                      VOIDmode, EXPAND_NORMAL);
3481   tmp = convert_memory_address (Pmode, tmp);
3482   if (!crtl->eh.ehr_handler)
3483     crtl->eh.ehr_handler = copy_to_reg (tmp);
3484   else if (tmp != crtl->eh.ehr_handler)
3485     emit_move_insn (crtl->eh.ehr_handler, tmp);
3486
3487   if (!crtl->eh.ehr_label)
3488     crtl->eh.ehr_label = gen_label_rtx ();
3489   emit_jump (crtl->eh.ehr_label);
3490 }
3491
3492 void
3493 expand_eh_return (void)
3494 {
3495   rtx around_label;
3496
3497   if (! crtl->eh.ehr_label)
3498     return;
3499
3500   crtl->calls_eh_return = 1;
3501
3502 #ifdef EH_RETURN_STACKADJ_RTX
3503   emit_move_insn (EH_RETURN_STACKADJ_RTX, const0_rtx);
3504 #endif
3505
3506   around_label = gen_label_rtx ();
3507   emit_jump (around_label);
3508
3509   emit_label (crtl->eh.ehr_label);
3510   clobber_return_register ();
3511
3512 #ifdef EH_RETURN_STACKADJ_RTX
3513   emit_move_insn (EH_RETURN_STACKADJ_RTX, crtl->eh.ehr_stackadj);
3514 #endif
3515
3516 #ifdef HAVE_eh_return
3517   if (HAVE_eh_return)
3518     emit_insn (gen_eh_return (crtl->eh.ehr_handler));
3519   else
3520 #endif
3521     {
3522 #ifdef EH_RETURN_HANDLER_RTX
3523       emit_move_insn (EH_RETURN_HANDLER_RTX, crtl->eh.ehr_handler);
3524 #else
3525       error ("__builtin_eh_return not supported on this target");
3526 #endif
3527     }
3528
3529   emit_label (around_label);
3530 }
3531
3532 /* Convert a ptr_mode address ADDR_TREE to a Pmode address controlled by
3533    POINTERS_EXTEND_UNSIGNED and return it.  */
3534
3535 rtx
3536 expand_builtin_extend_pointer (tree addr_tree)
3537 {
3538   rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, EXPAND_NORMAL);
3539   int extend;
3540
3541 #ifdef POINTERS_EXTEND_UNSIGNED
3542   extend = POINTERS_EXTEND_UNSIGNED;
3543 #else
3544   /* The previous EH code did an unsigned extend by default, so we do this also
3545      for consistency.  */
3546   extend = 1;
3547 #endif
3548
3549   return convert_modes (targetm.unwind_word_mode (), ptr_mode, addr, extend);
3550 }
3551 \f
3552 /* In the following functions, we represent entries in the action table
3553    as 1-based indices.  Special cases are:
3554
3555          0:     null action record, non-null landing pad; implies cleanups
3556         -1:     null action record, null landing pad; implies no action
3557         -2:     no call-site entry; implies must_not_throw
3558         -3:     we have yet to process outer regions
3559
3560    Further, no special cases apply to the "next" field of the record.
3561    For next, 0 means end of list.  */
3562
3563 struct action_record
3564 {
3565   int offset;
3566   int filter;
3567   int next;
3568 };
3569
3570 static int
3571 action_record_eq (const void *pentry, const void *pdata)
3572 {
3573   const struct action_record *entry = (const struct action_record *) pentry;
3574   const struct action_record *data = (const struct action_record *) pdata;
3575   return entry->filter == data->filter && entry->next == data->next;
3576 }
3577
3578 static hashval_t
3579 action_record_hash (const void *pentry)
3580 {
3581   const struct action_record *entry = (const struct action_record *) pentry;
3582   return entry->next * 1009 + entry->filter;
3583 }
3584
3585 static int
3586 add_action_record (htab_t ar_hash, int filter, int next)
3587 {
3588   struct action_record **slot, *new_ar, tmp;
3589
3590   tmp.filter = filter;
3591   tmp.next = next;
3592   slot = (struct action_record **) htab_find_slot (ar_hash, &tmp, INSERT);
3593
3594   if ((new_ar = *slot) == NULL)
3595     {
3596       new_ar = XNEW (struct action_record);
3597       new_ar->offset = VARRAY_ACTIVE_SIZE (crtl->eh.action_record_data) + 1;
3598       new_ar->filter = filter;
3599       new_ar->next = next;
3600       *slot = new_ar;
3601
3602       /* The filter value goes in untouched.  The link to the next
3603          record is a "self-relative" byte offset, or zero to indicate
3604          that there is no next record.  So convert the absolute 1 based
3605          indices we've been carrying around into a displacement.  */
3606
3607       push_sleb128 (&crtl->eh.action_record_data, filter);
3608       if (next)
3609         next -= VARRAY_ACTIVE_SIZE (crtl->eh.action_record_data) + 1;
3610       push_sleb128 (&crtl->eh.action_record_data, next);
3611     }
3612
3613   return new_ar->offset;
3614 }
3615
3616 static int
3617 collect_one_action_chain (htab_t ar_hash, struct eh_region_d *region)
3618 {
3619   struct eh_region_d *c;
3620   int next;
3621
3622   /* If we've reached the top of the region chain, then we have
3623      no actions, and require no landing pad.  */
3624   if (region == NULL)
3625     return -1;
3626
3627   switch (region->type)
3628     {
3629     case ERT_CLEANUP:
3630       /* A cleanup adds a zero filter to the beginning of the chain, but
3631          there are special cases to look out for.  If there are *only*
3632          cleanups along a path, then it compresses to a zero action.
3633          Further, if there are multiple cleanups along a path, we only
3634          need to represent one of them, as that is enough to trigger
3635          entry to the landing pad at runtime.  */
3636       next = collect_one_action_chain (ar_hash, region->outer);
3637       if (next <= 0)
3638         return 0;
3639       for (c = region->outer; c ; c = c->outer)
3640         if (c->type == ERT_CLEANUP)
3641           return next;
3642       return add_action_record (ar_hash, 0, next);
3643
3644     case ERT_TRY:
3645       /* Process the associated catch regions in reverse order.
3646          If there's a catch-all handler, then we don't need to
3647          search outer regions.  Use a magic -3 value to record
3648          that we haven't done the outer search.  */
3649       next = -3;
3650       for (c = region->u.eh_try.last_catch; c ; c = c->u.eh_catch.prev_catch)
3651         {
3652           if (c->u.eh_catch.type_list == NULL)
3653             {
3654               /* Retrieve the filter from the head of the filter list
3655                  where we have stored it (see assign_filter_values).  */
3656               int filter
3657                 = TREE_INT_CST_LOW (TREE_VALUE (c->u.eh_catch.filter_list));
3658
3659               next = add_action_record (ar_hash, filter, 0);
3660             }
3661           else
3662             {
3663               /* Once the outer search is done, trigger an action record for
3664                  each filter we have.  */
3665               tree flt_node;
3666
3667               if (next == -3)
3668                 {
3669                   next = collect_one_action_chain (ar_hash, region->outer);
3670
3671                   /* If there is no next action, terminate the chain.  */
3672                   if (next == -1)
3673                     next = 0;
3674                   /* If all outer actions are cleanups or must_not_throw,
3675                      we'll have no action record for it, since we had wanted
3676                      to encode these states in the call-site record directly.
3677                      Add a cleanup action to the chain to catch these.  */
3678                   else if (next <= 0)
3679                     next = add_action_record (ar_hash, 0, 0);
3680                 }
3681
3682               flt_node = c->u.eh_catch.filter_list;
3683               for (; flt_node; flt_node = TREE_CHAIN (flt_node))
3684                 {
3685                   int filter = TREE_INT_CST_LOW (TREE_VALUE (flt_node));
3686                   next = add_action_record (ar_hash, filter, next);
3687                 }
3688             }
3689         }
3690       return next;
3691
3692     case ERT_ALLOWED_EXCEPTIONS:
3693       /* An exception specification adds its filter to the
3694          beginning of the chain.  */
3695       next = collect_one_action_chain (ar_hash, region->outer);
3696
3697       /* If there is no next action, terminate the chain.  */
3698       if (next == -1)
3699         next = 0;
3700       /* If all outer actions are cleanups or must_not_throw,
3701          we'll have no action record for it, since we had wanted
3702          to encode these states in the call-site record directly.
3703          Add a cleanup action to the chain to catch these.  */
3704       else if (next <= 0)
3705         next = add_action_record (ar_hash, 0, 0);
3706
3707       return add_action_record (ar_hash, region->u.allowed.filter, next);
3708
3709     case ERT_MUST_NOT_THROW:
3710       /* A must-not-throw region with no inner handlers or cleanups
3711          requires no call-site entry.  Note that this differs from
3712          the no handler or cleanup case in that we do require an lsda
3713          to be generated.  Return a magic -2 value to record this.  */
3714       return -2;
3715
3716     case ERT_CATCH:
3717     case ERT_THROW:
3718       /* CATCH regions are handled in TRY above.  THROW regions are
3719          for optimization information only and produce no output.  */
3720       return collect_one_action_chain (ar_hash, region->outer);
3721
3722     default:
3723       gcc_unreachable ();
3724     }
3725 }
3726
3727 static int
3728 add_call_site (rtx landing_pad, int action)
3729 {
3730   call_site_record record;
3731   
3732   record = GGC_NEW (struct call_site_record_d);
3733   record->landing_pad = landing_pad;
3734   record->action = action;
3735
3736   VEC_safe_push (call_site_record, gc, crtl->eh.call_site_record, record);
3737
3738   return call_site_base + VEC_length (call_site_record, crtl->eh.call_site_record) - 1;
3739 }
3740
3741 /* Turn REG_EH_REGION notes back into NOTE_INSN_EH_REGION notes.
3742    The new note numbers will not refer to region numbers, but
3743    instead to call site entries.  */
3744
3745 unsigned int
3746 convert_to_eh_region_ranges (void)
3747 {
3748   rtx insn, iter, note;
3749   htab_t ar_hash;
3750   int last_action = -3;
3751   rtx last_action_insn = NULL_RTX;
3752   rtx last_landing_pad = NULL_RTX;
3753   rtx first_no_action_insn = NULL_RTX;
3754   int call_site = 0;
3755
3756   if (USING_SJLJ_EXCEPTIONS || cfun->eh->region_tree == NULL)
3757     return 0;
3758
3759   VARRAY_UCHAR_INIT (crtl->eh.action_record_data, 64, "action_record_data");
3760
3761   ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
3762
3763   for (iter = get_insns (); iter ; iter = NEXT_INSN (iter))
3764     if (INSN_P (iter))
3765       {
3766         struct eh_region_d *region;
3767         int this_action;
3768         rtx this_landing_pad;
3769
3770         insn = iter;
3771         if (NONJUMP_INSN_P (insn)
3772             && GET_CODE (PATTERN (insn)) == SEQUENCE)
3773           insn = XVECEXP (PATTERN (insn), 0, 0);
3774
3775         note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3776         if (!note)
3777           {
3778             if (! (CALL_P (insn)
3779                    || (flag_non_call_exceptions
3780                        && may_trap_p (PATTERN (insn)))))
3781               continue;
3782             this_action = -1;
3783             region = NULL;
3784           }
3785         else
3786           {
3787             if (INTVAL (XEXP (note, 0)) <= 0)
3788               continue;
3789             region = VEC_index (eh_region, cfun->eh->region_array, INTVAL (XEXP (note, 0)));
3790             this_action = collect_one_action_chain (ar_hash, region);
3791           }
3792
3793         /* Existence of catch handlers, or must-not-throw regions
3794            implies that an lsda is needed (even if empty).  */
3795         if (this_action != -1)
3796           crtl->uses_eh_lsda = 1;
3797
3798         /* Delay creation of region notes for no-action regions
3799            until we're sure that an lsda will be required.  */
3800         else if (last_action == -3)
3801           {
3802             first_no_action_insn = iter;
3803             last_action = -1;
3804           }
3805
3806         /* Cleanups and handlers may share action chains but not
3807            landing pads.  Collect the landing pad for this region.  */
3808         if (this_action >= 0)
3809           {
3810             struct eh_region_d *o;
3811             for (o = region; ! o->landing_pad ; o = o->outer)
3812               continue;
3813             this_landing_pad = o->landing_pad;
3814           }
3815         else
3816           this_landing_pad = NULL_RTX;
3817
3818         /* Differing actions or landing pads implies a change in call-site
3819            info, which implies some EH_REGION note should be emitted.  */
3820         if (last_action != this_action
3821             || last_landing_pad != this_landing_pad)
3822           {
3823             /* If we'd not seen a previous action (-3) or the previous
3824                action was must-not-throw (-2), then we do not need an
3825                end note.  */
3826             if (last_action >= -1)
3827               {
3828                 /* If we delayed the creation of the begin, do it now.  */
3829                 if (first_no_action_insn)
3830                   {
3831                     call_site = add_call_site (NULL_RTX, 0);
3832                     note = emit_note_before (NOTE_INSN_EH_REGION_BEG,
3833                                              first_no_action_insn);
3834                     NOTE_EH_HANDLER (note) = call_site;
3835                     first_no_action_insn = NULL_RTX;
3836                   }
3837
3838                 note = emit_note_after (NOTE_INSN_EH_REGION_END,
3839                                         last_action_insn);
3840                 NOTE_EH_HANDLER (note) = call_site;
3841               }
3842
3843             /* If the new action is must-not-throw, then no region notes
3844                are created.  */
3845             if (this_action >= -1)
3846               {
3847                 call_site = add_call_site (this_landing_pad,
3848                                            this_action < 0 ? 0 : this_action);
3849                 note = emit_note_before (NOTE_INSN_EH_REGION_BEG, iter);
3850                 NOTE_EH_HANDLER (note) = call_site;
3851               }
3852
3853             last_action = this_action;
3854             last_landing_pad = this_landing_pad;
3855           }
3856         last_action_insn = iter;
3857       }
3858
3859   if (last_action >= -1 && ! first_no_action_insn)
3860     {
3861       note = emit_note_after (NOTE_INSN_EH_REGION_END, last_action_insn);
3862       NOTE_EH_HANDLER (note) = call_site;
3863     }
3864
3865   htab_delete (ar_hash);
3866   return 0;
3867 }
3868
3869 struct rtl_opt_pass pass_convert_to_eh_region_ranges =
3870 {
3871  {
3872   RTL_PASS,
3873   "eh_ranges",                          /* name */
3874   NULL,                                 /* gate */
3875   convert_to_eh_region_ranges,          /* execute */
3876   NULL,                                 /* sub */
3877   NULL,                                 /* next */
3878   0,                                    /* static_pass_number */
3879   TV_NONE,                              /* tv_id */
3880   0,                                    /* properties_required */
3881   0,                                    /* properties_provided */
3882   0,                                    /* properties_destroyed */
3883   0,                                    /* todo_flags_start */
3884   TODO_dump_func,                       /* todo_flags_finish */
3885  }
3886 };
3887
3888 \f
3889 static void
3890 push_uleb128 (varray_type *data_area, unsigned int value)
3891 {
3892   do
3893     {
3894       unsigned char byte = value & 0x7f;
3895       value >>= 7;
3896       if (value)
3897         byte |= 0x80;
3898       VARRAY_PUSH_UCHAR (*data_area, byte);
3899     }
3900   while (value);
3901 }
3902
3903 static void
3904 push_sleb128 (varray_type *data_area, int value)
3905 {
3906   unsigned char byte;
3907   int more;
3908
3909   do
3910     {
3911       byte = value & 0x7f;
3912       value >>= 7;
3913       more = ! ((value == 0 && (byte & 0x40) == 0)
3914                 || (value == -1 && (byte & 0x40) != 0));
3915       if (more)
3916         byte |= 0x80;
3917       VARRAY_PUSH_UCHAR (*data_area, byte);
3918     }
3919   while (more);
3920 }
3921
3922 \f
3923 #ifndef HAVE_AS_LEB128
3924 static int
3925 dw2_size_of_call_site_table (void)
3926 {
3927   int n = VEC_length (call_site_record, crtl->eh.call_site_record);
3928   int size = n * (4 + 4 + 4);
3929   int i;
3930
3931   for (i = 0; i < n; ++i)
3932     {
3933       struct call_site_record_d *cs =
3934         VEC_index (call_site_record, crtl->eh.call_site_record, i);
3935       size += size_of_uleb128 (cs->action);
3936     }
3937
3938   return size;
3939 }
3940
3941 static int
3942 sjlj_size_of_call_site_table (void)
3943 {
3944   int n = VEC_length (call_site_record, crtl->eh.call_site_record);
3945   int size = 0;
3946   int i;
3947
3948   for (i = 0; i < n; ++i)
3949     {
3950       struct call_site_record_d *cs =
3951         VEC_index (call_site_record, crtl->eh.call_site_record, i);
3952       size += size_of_uleb128 (INTVAL (cs->landing_pad));
3953       size += size_of_uleb128 (cs->action);
3954     }
3955
3956   return size;
3957 }
3958 #endif
3959
3960 static void
3961 dw2_output_call_site_table (void)
3962 {
3963   int n = VEC_length (call_site_record, crtl->eh.call_site_record);
3964   int i;
3965
3966   for (i = 0; i < n; ++i)
3967     {
3968       struct call_site_record_d *cs =
3969         VEC_index (call_site_record, crtl->eh.call_site_record, i);
3970       char reg_start_lab[32];
3971       char reg_end_lab[32];
3972       char landing_pad_lab[32];
3973
3974       ASM_GENERATE_INTERNAL_LABEL (reg_start_lab, "LEHB", call_site_base + i);
3975       ASM_GENERATE_INTERNAL_LABEL (reg_end_lab, "LEHE", call_site_base + i);
3976
3977       if (cs->landing_pad)
3978         ASM_GENERATE_INTERNAL_LABEL (landing_pad_lab, "L",
3979                                      CODE_LABEL_NUMBER (cs->landing_pad));
3980
3981       /* ??? Perhaps use insn length scaling if the assembler supports
3982          generic arithmetic.  */
3983       /* ??? Perhaps use attr_length to choose data1 or data2 instead of
3984          data4 if the function is small enough.  */
3985 #ifdef HAVE_AS_LEB128
3986       dw2_asm_output_delta_uleb128 (reg_start_lab,
3987                                     current_function_func_begin_label,
3988                                     "region %d start", i);
3989       dw2_asm_output_delta_uleb128 (reg_end_lab, reg_start_lab,
3990                                     "length");
3991       if (cs->landing_pad)
3992         dw2_asm_output_delta_uleb128 (landing_pad_lab,
3993                                       current_function_func_begin_label,
3994                                       "landing pad");
3995       else
3996         dw2_asm_output_data_uleb128 (0, "landing pad");
3997 #else
3998       dw2_asm_output_delta (4, reg_start_lab,
3999                             current_function_func_begin_label,
4000                             "region %d start", i);
4001       dw2_asm_output_delta (4, reg_end_lab, reg_start_lab, "length");
4002       if (cs->landing_pad)
4003         dw2_asm_output_delta (4, landing_pad_lab,
4004                               current_function_func_begin_label,
4005                               "landing pad");
4006       else
4007         dw2_asm_output_data (4, 0, "landing pad");
4008 #endif
4009       dw2_asm_output_data_uleb128 (cs->action, "action");
4010     }
4011
4012   call_site_base += n;
4013 }
4014
4015 static void
4016 sjlj_output_call_site_table (void)
4017 {
4018   int n = VEC_length (call_site_record, crtl->eh.call_site_record);
4019   int i;
4020
4021   for (i = 0; i < n; ++i)
4022     {
4023       struct call_site_record_d *cs =
4024         VEC_index (call_site_record, crtl->eh.call_site_record, i);
4025
4026       dw2_asm_output_data_uleb128 (INTVAL (cs->landing_pad),
4027                                    "region %d landing pad", i);
4028       dw2_asm_output_data_uleb128 (cs->action, "action");
4029     }
4030
4031   call_site_base += n;
4032 }
4033
4034 #ifndef TARGET_UNWIND_INFO
4035 /* Switch to the section that should be used for exception tables.  */
4036
4037 static void
4038 switch_to_exception_section (const char * ARG_UNUSED (fnname))
4039 {
4040   section *s;
4041
4042   if (exception_section)
4043     s = exception_section;
4044   else
4045     {
4046       /* Compute the section and cache it into exception_section,
4047          unless it depends on the function name.  */
4048       if (targetm.have_named_sections)
4049         {
4050           int flags;
4051
4052           if (EH_TABLES_CAN_BE_READ_ONLY)
4053             {
4054               int tt_format =
4055                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
4056               flags = ((! flag_pic
4057                         || ((tt_format & 0x70) != DW_EH_PE_absptr
4058                             && (tt_format & 0x70) != DW_EH_PE_aligned))
4059                        ? 0 : SECTION_WRITE);
4060             }
4061           else
4062             flags = SECTION_WRITE;
4063
4064 #ifdef HAVE_LD_EH_GC_SECTIONS
4065           if (flag_function_sections)
4066             {
4067               char *section_name = XNEWVEC (char, strlen (fnname) + 32);
4068               sprintf (section_name, ".gcc_except_table.%s", fnname);
4069               s = get_section (section_name, flags, NULL);
4070               free (section_name);
4071             }
4072           else
4073 #endif
4074             exception_section
4075               = s = get_section (".gcc_except_table", flags, NULL);
4076         }
4077       else
4078         exception_section
4079           = s = flag_pic ? data_section : readonly_data_section;
4080     }
4081
4082   switch_to_section (s);
4083 }
4084 #endif
4085
4086
4087 /* Output a reference from an exception table to the type_info object TYPE.
4088    TT_FORMAT and TT_FORMAT_SIZE describe the DWARF encoding method used for
4089    the value.  */
4090
4091 static void
4092 output_ttype (tree type, int tt_format, int tt_format_size)
4093 {
4094   rtx value;
4095   bool is_public = true;
4096
4097   if (type == NULL_TREE)
4098     value = const0_rtx;
4099   else
4100     {
4101       struct varpool_node *node;
4102
4103       type = lookup_type_for_runtime (type);
4104       value = expand_expr (type, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
4105
4106       /* Let cgraph know that the rtti decl is used.  Not all of the
4107          paths below go through assemble_integer, which would take
4108          care of this for us.  */
4109       STRIP_NOPS (type);
4110       if (TREE_CODE (type) == ADDR_EXPR)
4111         {
4112           type = TREE_OPERAND (type, 0);
4113           if (TREE_CODE (type) == VAR_DECL)
4114             {
4115               node = varpool_node (type);
4116               if (node)
4117                 varpool_mark_needed_node (node);
4118               is_public = TREE_PUBLIC (type);
4119             }
4120         }
4121       else
4122         gcc_assert (TREE_CODE (type) == INTEGER_CST);
4123     }
4124
4125   /* Allow the target to override the type table entry format.  */
4126   if (targetm.asm_out.ttype (value))
4127     return;
4128
4129   if (tt_format == DW_EH_PE_absptr || tt_format == DW_EH_PE_aligned)
4130     assemble_integer (value, tt_format_size,
4131                       tt_format_size * BITS_PER_UNIT, 1);
4132   else
4133     dw2_asm_output_encoded_addr_rtx (tt_format, value, is_public, NULL);
4134 }
4135
4136 void
4137 output_function_exception_table (const char * ARG_UNUSED (fnname))
4138 {
4139   int tt_format, cs_format, lp_format, i, n;
4140 #ifdef HAVE_AS_LEB128
4141   char ttype_label[32];
4142   char cs_after_size_label[32];
4143   char cs_end_label[32];
4144 #else
4145   int call_site_len;
4146 #endif
4147   int have_tt_data;
4148   int tt_format_size = 0;
4149
4150   /* Not all functions need anything.  */
4151   if (! crtl->uses_eh_lsda)
4152     return;
4153
4154   if (eh_personality_libfunc)
4155     assemble_external_libcall (eh_personality_libfunc);
4156
4157 #ifdef TARGET_UNWIND_INFO
4158   /* TODO: Move this into target file.  */
4159   fputs ("\t.personality\t", asm_out_file);
4160   output_addr_const (asm_out_file, eh_personality_libfunc);
4161   fputs ("\n\t.handlerdata\n", asm_out_file);
4162   /* Note that varasm still thinks we're in the function's code section.
4163      The ".endp" directive that will immediately follow will take us back.  */
4164 #else
4165   switch_to_exception_section (fnname);
4166 #endif
4167
4168   /* If the target wants a label to begin the table, emit it here.  */
4169   targetm.asm_out.except_table_label (asm_out_file);
4170
4171   have_tt_data = (VEC_length (tree, crtl->eh.ttype_data) > 0
4172                   || VARRAY_ACTIVE_SIZE (crtl->eh.ehspec_data) > 0);
4173
4174   /* Indicate the format of the @TType entries.  */
4175   if (! have_tt_data)
4176     tt_format = DW_EH_PE_omit;
4177   else
4178     {
4179       tt_format = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
4180 #ifdef HAVE_AS_LEB128
4181       ASM_GENERATE_INTERNAL_LABEL (ttype_label, "LLSDATT",
4182                                    current_function_funcdef_no);
4183 #endif
4184       tt_format_size = size_of_encoded_value (tt_format);
4185
4186       assemble_align (tt_format_size * BITS_PER_UNIT);
4187     }
4188
4189   targetm.asm_out.internal_label (asm_out_file, "LLSDA",
4190                              current_function_funcdef_no);
4191
4192   /* The LSDA header.  */
4193
4194   /* Indicate the format of the landing pad start pointer.  An omitted
4195      field implies @LPStart == @Start.  */
4196   /* Currently we always put @LPStart == @Start.  This field would
4197      be most useful in moving the landing pads completely out of
4198      line to another section, but it could also be used to minimize
4199      the size of uleb128 landing pad offsets.  */
4200   lp_format = DW_EH_PE_omit;
4201   dw2_asm_output_data (1, lp_format, "@LPStart format (%s)",
4202                        eh_data_format_name (lp_format));
4203
4204   /* @LPStart pointer would go here.  */
4205
4206   dw2_asm_output_data (1, tt_format, "@TType format (%s)",
4207                        eh_data_format_name (tt_format));
4208
4209 #ifndef HAVE_AS_LEB128
4210   if (USING_SJLJ_EXCEPTIONS)
4211     call_site_len = sjlj_size_of_call_site_table ();
4212   else
4213     call_site_len = dw2_size_of_call_site_table ();
4214 #endif
4215
4216   /* A pc-relative 4-byte displacement to the @TType data.  */
4217   if (have_tt_data)
4218     {
4219 #ifdef HAVE_AS_LEB128
4220       char ttype_after_disp_label[32];
4221       ASM_GENERATE_INTERNAL_LABEL (ttype_after_disp_label, "LLSDATTD",
4222                                    current_function_funcdef_no);
4223       dw2_asm_output_delta_uleb128 (ttype_label, ttype_after_disp_label,
4224                                     "@TType base offset");
4225       ASM_OUTPUT_LABEL (asm_out_file, ttype_after_disp_label);
4226 #else
4227       /* Ug.  Alignment queers things.  */
4228       unsigned int before_disp, after_disp, last_disp, disp;
4229
4230       before_disp = 1 + 1;
4231       after_disp = (1 + size_of_uleb128 (call_site_len)
4232                     + call_site_len
4233                     + VARRAY_ACTIVE_SIZE (crtl->eh.action_record_data)
4234                     + (VEC_length (tree, crtl->eh.ttype_data)
4235                        * tt_format_size));
4236
4237       disp = after_disp;
4238       do
4239         {
4240           unsigned int disp_size, pad;
4241
4242           last_disp = disp;
4243           disp_size = size_of_uleb128 (disp);
4244           pad = before_disp + disp_size + after_disp;
4245           if (pad % tt_format_size)
4246             pad = tt_format_size - (pad % tt_format_size);
4247           else
4248             pad = 0;
4249           disp = after_disp + pad;
4250         }
4251       while (disp != last_disp);
4252
4253       dw2_asm_output_data_uleb128 (disp, "@TType base offset");
4254 #endif
4255     }
4256
4257   /* Indicate the format of the call-site offsets.  */
4258 #ifdef HAVE_AS_LEB128
4259   cs_format = DW_EH_PE_uleb128;
4260 #else
4261   cs_format = DW_EH_PE_udata4;
4262 #endif
4263   dw2_asm_output_data (1, cs_format, "call-site format (%s)",
4264                        eh_data_format_name (cs_format));
4265
4266 #ifdef HAVE_AS_LEB128
4267   ASM_GENERATE_INTERNAL_LABEL (cs_after_size_label, "LLSDACSB",
4268                                current_function_funcdef_no);
4269   ASM_GENERATE_INTERNAL_LABEL (cs_end_label, "LLSDACSE",
4270                                current_function_funcdef_no);
4271   dw2_asm_output_delta_uleb128 (cs_end_label, cs_after_size_label,
4272                                 "Call-site table length");
4273   ASM_OUTPUT_LABEL (asm_out_file, cs_after_size_label);
4274   if (USING_SJLJ_EXCEPTIONS)
4275     sjlj_output_call_site_table ();
4276   else
4277     dw2_output_call_site_table ();
4278   ASM_OUTPUT_LABEL (asm_out_file, cs_end_label);
4279 #else
4280   dw2_asm_output_data_uleb128 (call_site_len,"Call-site table length");
4281   if (USING_SJLJ_EXCEPTIONS)
4282     sjlj_output_call_site_table ();
4283   else
4284     dw2_output_call_site_table ();
4285 #endif
4286
4287   /* ??? Decode and interpret the data for flag_debug_asm.  */
4288   n = VARRAY_ACTIVE_SIZE (crtl->eh.action_record_data);
4289   for (i = 0; i < n; ++i)
4290     dw2_asm_output_data (1, VARRAY_UCHAR (crtl->eh.action_record_data, i),
4291                          (i ? NULL : "Action record table"));
4292
4293   if (have_tt_data)
4294     assemble_align (tt_format_size * BITS_PER_UNIT);
4295
4296   i = VEC_length (tree, crtl->eh.ttype_data);
4297   while (i-- > 0)
4298     {
4299       tree type = VEC_index (tree, crtl->eh.ttype_data, i);
4300       output_ttype (type, tt_format, tt_format_size);
4301     }
4302
4303 #ifdef HAVE_AS_LEB128
4304   if (have_tt_data)
4305       ASM_OUTPUT_LABEL (asm_out_file, ttype_label);
4306 #endif
4307
4308   /* ??? Decode and interpret the data for flag_debug_asm.  */
4309   n = VARRAY_ACTIVE_SIZE (crtl->eh.ehspec_data);
4310   for (i = 0; i < n; ++i)
4311     {
4312       if (targetm.arm_eabi_unwinder)
4313         {
4314           tree type = VARRAY_TREE (crtl->eh.ehspec_data, i);
4315           output_ttype (type, tt_format, tt_format_size);
4316         }
4317       else
4318         dw2_asm_output_data (1, VARRAY_UCHAR (crtl->eh.ehspec_data, i),
4319                              (i ? NULL : "Exception specification table"));
4320     }
4321
4322   switch_to_section (current_function_section ());
4323 }
4324
4325 void
4326 set_eh_throw_stmt_table (struct function *fun, struct htab *table)
4327 {
4328   fun->eh->throw_stmt_table = table;
4329 }
4330
4331 htab_t
4332 get_eh_throw_stmt_table (struct function *fun)
4333 {
4334   return fun->eh->throw_stmt_table;
4335 }
4336
4337 /* Dump EH information to OUT.  */
4338
4339 void
4340 dump_eh_tree (FILE * out, struct function *fun)
4341 {
4342   struct eh_region_d *i;
4343   int depth = 0;
4344   static const char *const type_name[] = { "unknown", "cleanup", "try", "catch",
4345                                            "allowed_exceptions", "must_not_throw",
4346                                            "throw"
4347                                          };
4348
4349   i = fun->eh->region_tree;
4350   if (!i)
4351     return;
4352
4353   fprintf (out, "Eh tree:\n");
4354   while (1)
4355     {
4356       fprintf (out, "  %*s %i %s", depth * 2, "",
4357                i->region_number, type_name[(int) i->type]);
4358       if (i->tree_label)
4359         {
4360           fprintf (out, " tree_label:");
4361           print_generic_expr (out, i->tree_label, 0);
4362         }
4363       if (i->label)
4364         fprintf (out, " label:%i", INSN_UID (i->label));
4365       if (i->landing_pad)
4366         {
4367           fprintf (out, " landing_pad:%i", INSN_UID (i->landing_pad));
4368           if (GET_CODE (i->landing_pad) == NOTE)
4369             fprintf (out, " (deleted)");
4370         }
4371       if (i->post_landing_pad)
4372         {
4373           fprintf (out, " post_landing_pad:%i", INSN_UID (i->post_landing_pad));
4374           if (GET_CODE (i->post_landing_pad) == NOTE)
4375             fprintf (out, " (deleted)");
4376         }
4377       if (i->resume)
4378         {
4379           fprintf (out, " resume:%i", INSN_UID (i->resume));
4380           if (GET_CODE (i->resume) == NOTE)
4381             fprintf (out, " (deleted)");
4382         }
4383       if (i->may_contain_throw)
4384         fprintf (out, " may_contain_throw");
4385       switch (i->type)
4386         {
4387         case ERT_CLEANUP:
4388           break;
4389
4390         case ERT_TRY:
4391           {
4392             struct eh_region_d *c;
4393             fprintf (out, " catch regions:");
4394             for (c = i->u.eh_try.eh_catch; c; c = c->u.eh_catch.next_catch)
4395               fprintf (out, " %i", c->region_number);
4396           }
4397           break;
4398
4399         case ERT_CATCH:
4400           if (i->u.eh_catch.prev_catch)
4401             fprintf (out, " prev: %i",
4402                      i->u.eh_catch.prev_catch->region_number);
4403           if (i->u.eh_catch.next_catch)
4404             fprintf (out, " next %i",
4405                      i->u.eh_catch.next_catch->region_number);
4406           fprintf (out, " type:");
4407           print_generic_expr (out, i->u.eh_catch.type_list, 0);
4408           break;
4409
4410         case ERT_ALLOWED_EXCEPTIONS:
4411           fprintf (out, " filter :%i types:", i->u.allowed.filter);
4412           print_generic_expr (out, i->u.allowed.type_list, 0);
4413           break;
4414
4415         case ERT_THROW:
4416           fprintf (out, " type:");
4417           print_generic_expr (out, i->u.eh_throw.type, 0);
4418           break;
4419
4420         case ERT_MUST_NOT_THROW:
4421           break;
4422
4423         case ERT_UNKNOWN:
4424           break;
4425         }
4426       if (i->aka)
4427         {
4428           fprintf (out, " also known as:");
4429           dump_bitmap (out, i->aka);
4430         }
4431       else
4432         fprintf (out, "\n");
4433       /* If there are sub-regions, process them.  */
4434       if (i->inner)
4435         i = i->inner, depth++;
4436       /* If there are peers, process them.  */
4437       else if (i->next_peer)
4438         i = i->next_peer;
4439       /* Otherwise, step back up the tree to the next peer.  */
4440       else
4441         {
4442           do
4443             {
4444               i = i->outer;
4445               depth--;
4446               if (i == NULL)
4447                 return;
4448             }
4449           while (i->next_peer == NULL);
4450           i = i->next_peer;
4451         }
4452     }
4453 }
4454
4455 /* Dump the EH tree for FN on stderr.  */
4456
4457 void
4458 debug_eh_tree (struct function *fn)
4459 {
4460   dump_eh_tree (stderr, fn);
4461 }
4462
4463
4464 /* Verify EH region invariants.  */
4465
4466 static bool
4467 verify_eh_region (struct eh_region_d *region)
4468 {
4469   bool found = false;
4470   if (!region)
4471     return false;
4472   switch (region->type)
4473     {
4474     case ERT_TRY:
4475       {
4476         struct eh_region_d *c, *prev = NULL;
4477         if (region->u.eh_try.eh_catch->u.eh_catch.prev_catch)
4478           {
4479             error ("Try region %i has wrong rh_catch pointer to %i",
4480                    region->region_number,
4481                    region->u.eh_try.eh_catch->region_number);
4482             found = true;
4483           }
4484         for (c = region->u.eh_try.eh_catch; c; c = c->u.eh_catch.next_catch)
4485           {
4486             if (c->outer != region->outer)
4487               {
4488                 error
4489                   ("Catch region %i has different outer region than try region %i",
4490                    c->region_number, region->region_number);
4491                 found = true;
4492               }
4493             if (c->u.eh_catch.prev_catch != prev)
4494               {
4495                 error ("Catch region %i has corrupted catchlist",
4496                        c->region_number);
4497                 found = true;
4498               }
4499             prev = c;
4500           }
4501         if (prev != region->u.eh_try.last_catch)
4502           {
4503             error
4504               ("Try region %i has wrong last_catch pointer to %i instead of %i",
4505                region->region_number,
4506                region->u.eh_try.last_catch->region_number,
4507                prev->region_number);
4508             found = true;
4509           }
4510       }
4511       break;
4512     case ERT_CATCH:
4513       if (!region->u.eh_catch.prev_catch
4514           && (!region->next_peer || region->next_peer->type != ERT_TRY))
4515         {
4516           error ("Catch region %i should be followed by try", region->region_number);
4517           found = true;
4518         }
4519       break;
4520     case ERT_CLEANUP:
4521     case ERT_ALLOWED_EXCEPTIONS:
4522     case ERT_MUST_NOT_THROW:
4523     case ERT_THROW:
4524       break;
4525     case ERT_UNKNOWN:
4526       gcc_unreachable ();
4527     }
4528   for (region = region->inner; region; region = region->next_peer)
4529     found |= verify_eh_region (region);
4530   return found;
4531 }
4532
4533 /* Verify invariants on EH datastructures.  */
4534
4535 void
4536 verify_eh_tree (struct function *fun)
4537 {
4538   struct eh_region_d *i, *outer = NULL;
4539   bool err = false;
4540   int nvisited = 0;
4541   int count = 0;
4542   int j;
4543   int depth = 0;
4544
4545   if (!fun->eh->region_tree)
4546     return;
4547   for (j = fun->eh->last_region_number; j > 0; --j)
4548     if ((i = VEC_index (eh_region, fun->eh->region_array, j)))
4549       {
4550         if (i->region_number == j)
4551           count++;
4552         if (i->region_number != j && (!i->aka || !bitmap_bit_p (i->aka, j)))
4553           {
4554             error ("region_array is corrupted for region %i",
4555                    i->region_number);
4556             err = true;
4557           }
4558       }
4559   i = fun->eh->region_tree;
4560
4561   while (1)
4562     {
4563       if (VEC_index (eh_region, fun->eh->region_array, i->region_number) != i)
4564         {
4565           error ("region_array is corrupted for region %i", i->region_number);
4566           err = true;
4567         }
4568       if (i->outer != outer)
4569         {
4570           error ("outer block of region %i is wrong", i->region_number);
4571           err = true;
4572         }
4573       if (i->may_contain_throw && outer && !outer->may_contain_throw)
4574         {
4575           error
4576             ("region %i may contain throw and is contained in region that may not",
4577              i->region_number);
4578           err = true;
4579         }
4580       if (depth < 0)
4581         {
4582           error ("negative nesting depth of region %i", i->region_number);
4583           err = true;
4584         }
4585       nvisited++;
4586       /* If there are sub-regions, process them.  */
4587       if (i->inner)
4588         outer = i, i = i->inner, depth++;
4589       /* If there are peers, process them.  */
4590       else if (i->next_peer)
4591         i = i->next_peer;
4592       /* Otherwise, step back up the tree to the next peer.  */
4593       else
4594         {
4595           do
4596             {
4597               i = i->outer;
4598               depth--;
4599               if (i == NULL)
4600                 {
4601                   if (depth != -1)
4602                     {
4603                       error ("tree list ends on depth %i", depth + 1);
4604                       err = true;
4605                     }
4606                   if (count != nvisited)
4607                     {
4608                       error ("array does not match the region tree");
4609                       err = true;
4610                     }
4611                   if (!err)
4612                     for (i = fun->eh->region_tree; i; i = i->next_peer)
4613                       err |= verify_eh_region (i);
4614                   
4615                   if (err)
4616                     {
4617                       dump_eh_tree (stderr, fun);
4618                       internal_error ("verify_eh_tree failed");
4619                     }
4620                   return;
4621                 }
4622               outer = i->outer;
4623             }
4624           while (i->next_peer == NULL);
4625           i = i->next_peer;
4626         }
4627     }
4628 }
4629
4630 /* Initialize unwind_resume_libfunc.  */
4631
4632 void
4633 default_init_unwind_resume_libfunc (void)
4634 {
4635   /* The default c++ routines aren't actually c++ specific, so use those.  */
4636   unwind_resume_libfunc =
4637     init_one_libfunc ( USING_SJLJ_EXCEPTIONS ? "_Unwind_SjLj_Resume"
4638                                              : "_Unwind_Resume");
4639 }
4640
4641 \f
4642 static bool
4643 gate_handle_eh (void)
4644 {
4645   return doing_eh (0);
4646 }
4647
4648 /* Complete generation of exception handling code.  */
4649 static unsigned int
4650 rest_of_handle_eh (void)
4651 {
4652   finish_eh_generation ();
4653   cleanup_cfg (CLEANUP_NO_INSN_DEL);
4654   return 0;
4655 }
4656
4657 struct rtl_opt_pass pass_rtl_eh =
4658 {
4659  {
4660   RTL_PASS,
4661   "eh",                                 /* name */
4662   gate_handle_eh,                       /* gate */
4663   rest_of_handle_eh,                    /* execute */
4664   NULL,                                 /* sub */
4665   NULL,                                 /* next */
4666   0,                                    /* static_pass_number */
4667   TV_JUMP,                              /* tv_id */
4668   0,                                    /* properties_required */
4669   0,                                    /* properties_provided */
4670   0,                                    /* properties_destroyed */
4671   0,                                    /* todo_flags_start */
4672   TODO_dump_func                        /* todo_flags_finish */
4673  }
4674 };
4675
4676 #include "gt-except.h"