OSDN Git Service

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