OSDN Git Service

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