OSDN Git Service

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