OSDN Git Service

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