OSDN Git Service

* cse.c (rest_of_handle_cse): Do not pass CLEANUP_PRE_LOOP to cfg
[pf3gnuchains/gcc-fork.git] / gcc / except.c
1 /* Implements exception handling.
2    Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4    Contributed by Mike Stump <mrs@cygnus.com>.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING.  If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA.  */
22
23
24 /* An exception is an event that can be signaled from within a
25    function. This event can then be "caught" or "trapped" by the
26    callers of this function. This potentially allows program flow to
27    be transferred to any arbitrary code associated with a function call
28    several levels up the stack.
29
30    The intended use for this mechanism is for signaling "exceptional
31    events" in an out-of-band fashion, hence its name. The C++ language
32    (and many other OO-styled or functional languages) practically
33    requires such a mechanism, as otherwise it becomes very difficult
34    or even impossible to signal failure conditions in complex
35    situations.  The traditional C++ example is when an error occurs in
36    the process of constructing an object; without such a mechanism, it
37    is impossible to signal that the error occurs without adding global
38    state variables and error checks around every object construction.
39
40    The act of causing this event to occur is referred to as "throwing
41    an exception". (Alternate terms include "raising an exception" or
42    "signaling an exception".) The term "throw" is used because control
43    is returned to the callers of the function that is signaling the
44    exception, and thus there is the concept of "throwing" the
45    exception up the call stack.
46
47    [ Add updated documentation on how to use this.  ]  */
48
49
50 #include "config.h"
51 #include "system.h"
52 #include "coretypes.h"
53 #include "tm.h"
54 #include "rtl.h"
55 #include "tree.h"
56 #include "flags.h"
57 #include "function.h"
58 #include "expr.h"
59 #include "libfuncs.h"
60 #include "insn-config.h"
61 #include "except.h"
62 #include "integrate.h"
63 #include "hard-reg-set.h"
64 #include "basic-block.h"
65 #include "output.h"
66 #include "dwarf2asm.h"
67 #include "dwarf2out.h"
68 #include "dwarf2.h"
69 #include "toplev.h"
70 #include "hashtab.h"
71 #include "intl.h"
72 #include "ggc.h"
73 #include "tm_p.h"
74 #include "target.h"
75 #include "langhooks.h"
76 #include "cgraph.h"
77 #include "diagnostic.h"
78 #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 static struct eh_region *
861 duplicate_eh_region_1 (struct eh_region *o)
862 {
863   struct eh_region *n = ggc_alloc_cleared (sizeof (struct eh_region));
864
865   *n = *o;
866
867   n->region_number = o->region_number + cfun->eh->last_region_number;
868   VEC_replace (eh_region, cfun->eh->region_array, n->region_number, n);
869   gcc_assert (!o->aka);
870
871   return n;
872 }
873
874 static void
875 duplicate_eh_region_2 (struct eh_region *o, struct eh_region **n_array,
876                        struct eh_region *prev_try)
877 {
878   struct eh_region *n = n_array[o->region_number];
879
880   switch (n->type)
881     {
882     case ERT_TRY:
883       if (o->u.try.catch)
884         n->u.try.catch = n_array[o->u.try.catch->region_number];
885       if (o->u.try.last_catch)
886         n->u.try.last_catch = n_array[o->u.try.last_catch->region_number];
887       break;
888
889     case ERT_CATCH:
890       if (o->u.catch.next_catch)
891         n->u.catch.next_catch = n_array[o->u.catch.next_catch->region_number];
892       if (o->u.catch.prev_catch)
893         n->u.catch.prev_catch = n_array[o->u.catch.prev_catch->region_number];
894       break;
895
896     case ERT_CLEANUP:
897       if (o->u.cleanup.prev_try)
898         n->u.cleanup.prev_try = n_array[o->u.cleanup.prev_try->region_number];
899       else
900         n->u.cleanup.prev_try = prev_try;
901       break;
902
903     default:
904       break;
905     }
906
907   if (o->outer)
908     n->outer = n_array[o->outer->region_number];
909   if (o->inner)
910     n->inner = n_array[o->inner->region_number];
911   if (o->next_peer)
912     n->next_peer = n_array[o->next_peer->region_number];
913 }
914
915 /* Duplicate the EH regions of IFUN into current function, root the tree in
916    OUTER_REGION and remap labels using MAP callback.  */
917 int
918 duplicate_eh_regions (struct function *ifun, duplicate_eh_regions_map map,
919                       void *data, int outer_region)
920 {
921   int ifun_last_region_number = ifun->eh->last_region_number;
922   struct eh_region **n_array, *root, *cur, *prev_try;
923   int i;
924
925   if (ifun_last_region_number == 0 || !ifun->eh->region_tree)
926     return 0;
927
928   n_array = xcalloc (ifun_last_region_number + 1, sizeof (*n_array));
929   VEC_safe_grow (eh_region, gc, cfun->eh->region_array,
930                  cfun->eh->last_region_number + 1 + ifun_last_region_number);
931
932   /* We might've created new cfun->eh->region_array so zero out nonexisting region 0.  */
933   VEC_replace (eh_region, cfun->eh->region_array, 0, 0);
934
935   for (i = cfun->eh->last_region_number + 1;
936        i < cfun->eh->last_region_number + 1 + ifun_last_region_number; i++)
937     VEC_replace (eh_region, cfun->eh->region_array, i, 0);
938
939   /* Search for the containing ERT_TRY region to fix up
940      the prev_try short-cuts for ERT_CLEANUP regions.  */
941   prev_try = NULL;
942   if (outer_region > 0)
943     for (prev_try = VEC_index (eh_region, cfun->eh->region_array, outer_region);
944          prev_try && prev_try->type != ERT_TRY;
945          prev_try = prev_try->outer)
946       ;
947
948   for (i = 1; i <= ifun_last_region_number; ++i)
949     {
950       cur = VEC_index (eh_region, ifun->eh->region_array, i);
951       if (!cur || cur->region_number != i)
952         continue;
953       n_array[i] = duplicate_eh_region_1 (cur);
954       if (cur->tree_label)
955         {
956           tree newlabel = map (cur->tree_label, data);
957           n_array[i]->tree_label = newlabel;
958         }
959       else
960         n_array[i]->tree_label = NULL;
961     }
962   for (i = 1; i <= ifun_last_region_number; ++i)
963     {
964       cur = VEC_index (eh_region, ifun->eh->region_array, i);
965       if (!cur || cur->region_number != i)
966         continue;
967       duplicate_eh_region_2 (cur, n_array, prev_try);
968     }
969
970   root = n_array[ifun->eh->region_tree->region_number];
971   gcc_assert (root->outer == NULL);
972   if (outer_region > 0)
973     {
974       struct eh_region *cur
975          = VEC_index (eh_region, cfun->eh->region_array, outer_region);
976       struct eh_region *p = cur->inner;
977
978       if (p)
979         {
980           while (p->next_peer)
981             p = p->next_peer;
982           p->next_peer = root;
983         }
984       else
985         cur->inner = root;
986       for (i = 1; i <= ifun_last_region_number; ++i)
987         if (n_array[i] && n_array[i]->outer == NULL)
988           n_array[i]->outer = cur;
989     }
990   else
991     {
992       struct eh_region *p = cfun->eh->region_tree;
993       if (p)
994         {
995           while (p->next_peer)
996             p = p->next_peer;
997           p->next_peer = root;
998         }
999       else
1000         cfun->eh->region_tree = root;
1001     }
1002
1003   free (n_array);
1004
1005   i = cfun->eh->last_region_number;
1006   cfun->eh->last_region_number = i + ifun_last_region_number;
1007
1008   return i;
1009 }
1010 \f
1011 static int
1012 t2r_eq (const void *pentry, const void *pdata)
1013 {
1014   tree entry = (tree) pentry;
1015   tree data = (tree) pdata;
1016
1017   return TREE_PURPOSE (entry) == data;
1018 }
1019
1020 static hashval_t
1021 t2r_hash (const void *pentry)
1022 {
1023   tree entry = (tree) pentry;
1024   return TREE_HASH (TREE_PURPOSE (entry));
1025 }
1026
1027 static void
1028 add_type_for_runtime (tree type)
1029 {
1030   tree *slot;
1031
1032   slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
1033                                             TREE_HASH (type), INSERT);
1034   if (*slot == NULL)
1035     {
1036       tree runtime = (*lang_eh_runtime_type) (type);
1037       *slot = tree_cons (type, runtime, NULL_TREE);
1038     }
1039 }
1040
1041 static tree
1042 lookup_type_for_runtime (tree type)
1043 {
1044   tree *slot;
1045
1046   slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
1047                                             TREE_HASH (type), NO_INSERT);
1048
1049   /* We should have always inserted the data earlier.  */
1050   return TREE_VALUE (*slot);
1051 }
1052
1053 \f
1054 /* Represent an entry in @TTypes for either catch actions
1055    or exception filter actions.  */
1056 struct ttypes_filter GTY(())
1057 {
1058   tree t;
1059   int filter;
1060 };
1061
1062 /* Compare ENTRY (a ttypes_filter entry in the hash table) with DATA
1063    (a tree) for a @TTypes type node we are thinking about adding.  */
1064
1065 static int
1066 ttypes_filter_eq (const void *pentry, const void *pdata)
1067 {
1068   const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1069   tree data = (tree) pdata;
1070
1071   return entry->t == data;
1072 }
1073
1074 static hashval_t
1075 ttypes_filter_hash (const void *pentry)
1076 {
1077   const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1078   return TREE_HASH (entry->t);
1079 }
1080
1081 /* Compare ENTRY with DATA (both struct ttypes_filter) for a @TTypes
1082    exception specification list we are thinking about adding.  */
1083 /* ??? Currently we use the type lists in the order given.  Someone
1084    should put these in some canonical order.  */
1085
1086 static int
1087 ehspec_filter_eq (const void *pentry, const void *pdata)
1088 {
1089   const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1090   const struct ttypes_filter *data = (const struct ttypes_filter *) pdata;
1091
1092   return type_list_equal (entry->t, data->t);
1093 }
1094
1095 /* Hash function for exception specification lists.  */
1096
1097 static hashval_t
1098 ehspec_filter_hash (const void *pentry)
1099 {
1100   const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1101   hashval_t h = 0;
1102   tree list;
1103
1104   for (list = entry->t; list ; list = TREE_CHAIN (list))
1105     h = (h << 5) + (h >> 27) + TREE_HASH (TREE_VALUE (list));
1106   return h;
1107 }
1108
1109 /* Add TYPE (which may be NULL) to cfun->eh->ttype_data, using TYPES_HASH
1110    to speed up the search.  Return the filter value to be used.  */
1111
1112 static int
1113 add_ttypes_entry (htab_t ttypes_hash, tree type)
1114 {
1115   struct ttypes_filter **slot, *n;
1116
1117   slot = (struct ttypes_filter **)
1118     htab_find_slot_with_hash (ttypes_hash, type, TREE_HASH (type), INSERT);
1119
1120   if ((n = *slot) == NULL)
1121     {
1122       /* Filter value is a 1 based table index.  */
1123
1124       n = XNEW (struct ttypes_filter);
1125       n->t = type;
1126       n->filter = VEC_length (tree, cfun->eh->ttype_data) + 1;
1127       *slot = n;
1128
1129       VEC_safe_push (tree, gc, cfun->eh->ttype_data, type);
1130     }
1131
1132   return n->filter;
1133 }
1134
1135 /* Add LIST to cfun->eh->ehspec_data, using EHSPEC_HASH and TYPES_HASH
1136    to speed up the search.  Return the filter value to be used.  */
1137
1138 static int
1139 add_ehspec_entry (htab_t ehspec_hash, htab_t ttypes_hash, tree list)
1140 {
1141   struct ttypes_filter **slot, *n;
1142   struct ttypes_filter dummy;
1143
1144   dummy.t = list;
1145   slot = (struct ttypes_filter **)
1146     htab_find_slot (ehspec_hash, &dummy, INSERT);
1147
1148   if ((n = *slot) == NULL)
1149     {
1150       /* Filter value is a -1 based byte index into a uleb128 buffer.  */
1151
1152       n = XNEW (struct ttypes_filter);
1153       n->t = list;
1154       n->filter = -(VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data) + 1);
1155       *slot = n;
1156
1157       /* Generate a 0 terminated list of filter values.  */
1158       for (; list ; list = TREE_CHAIN (list))
1159         {
1160           if (targetm.arm_eabi_unwinder)
1161             VARRAY_PUSH_TREE (cfun->eh->ehspec_data, TREE_VALUE (list));
1162           else
1163             {
1164               /* Look up each type in the list and encode its filter
1165                  value as a uleb128.  */
1166               push_uleb128 (&cfun->eh->ehspec_data,
1167                   add_ttypes_entry (ttypes_hash, TREE_VALUE (list)));
1168             }
1169         }
1170       if (targetm.arm_eabi_unwinder)
1171         VARRAY_PUSH_TREE (cfun->eh->ehspec_data, NULL_TREE);
1172       else
1173         VARRAY_PUSH_UCHAR (cfun->eh->ehspec_data, 0);
1174     }
1175
1176   return n->filter;
1177 }
1178
1179 /* Generate the action filter values to be used for CATCH and
1180    ALLOWED_EXCEPTIONS regions.  When using dwarf2 exception regions,
1181    we use lots of landing pads, and so every type or list can share
1182    the same filter value, which saves table space.  */
1183
1184 static void
1185 assign_filter_values (void)
1186 {
1187   int i;
1188   htab_t ttypes, ehspec;
1189
1190   cfun->eh->ttype_data = VEC_alloc (tree, gc, 16);
1191   if (targetm.arm_eabi_unwinder)
1192     VARRAY_TREE_INIT (cfun->eh->ehspec_data, 64, "ehspec_data");
1193   else
1194     VARRAY_UCHAR_INIT (cfun->eh->ehspec_data, 64, "ehspec_data");
1195
1196   ttypes = htab_create (31, ttypes_filter_hash, ttypes_filter_eq, free);
1197   ehspec = htab_create (31, ehspec_filter_hash, ehspec_filter_eq, free);
1198
1199   for (i = cfun->eh->last_region_number; i > 0; --i)
1200     {
1201       struct eh_region *r;
1202
1203       r = VEC_index (eh_region, cfun->eh->region_array, i);
1204
1205       /* Mind we don't process a region more than once.  */
1206       if (!r || r->region_number != i)
1207         continue;
1208
1209       switch (r->type)
1210         {
1211         case ERT_CATCH:
1212           /* Whatever type_list is (NULL or true list), we build a list
1213              of filters for the region.  */
1214           r->u.catch.filter_list = NULL_TREE;
1215
1216           if (r->u.catch.type_list != NULL)
1217             {
1218               /* Get a filter value for each of the types caught and store
1219                  them in the region's dedicated list.  */
1220               tree tp_node = r->u.catch.type_list;
1221
1222               for (;tp_node; tp_node = TREE_CHAIN (tp_node))
1223                 {
1224                   int flt = add_ttypes_entry (ttypes, TREE_VALUE (tp_node));
1225                   tree flt_node = build_int_cst (NULL_TREE, flt);
1226
1227                   r->u.catch.filter_list
1228                     = tree_cons (NULL_TREE, flt_node, r->u.catch.filter_list);
1229                 }
1230             }
1231           else
1232             {
1233               /* Get a filter value for the NULL list also since it will need
1234                  an action record anyway.  */
1235               int flt = add_ttypes_entry (ttypes, NULL);
1236               tree flt_node = build_int_cst (NULL_TREE, flt);
1237
1238               r->u.catch.filter_list
1239                 = tree_cons (NULL_TREE, flt_node, r->u.catch.filter_list);
1240             }
1241
1242           break;
1243
1244         case ERT_ALLOWED_EXCEPTIONS:
1245           r->u.allowed.filter
1246             = add_ehspec_entry (ehspec, ttypes, r->u.allowed.type_list);
1247           break;
1248
1249         default:
1250           break;
1251         }
1252     }
1253
1254   htab_delete (ttypes);
1255   htab_delete (ehspec);
1256 }
1257
1258 /* Emit SEQ into basic block just before INSN (that is assumed to be
1259    first instruction of some existing BB and return the newly
1260    produced block.  */
1261 static basic_block
1262 emit_to_new_bb_before (rtx seq, rtx insn)
1263 {
1264   rtx last;
1265   basic_block bb;
1266   edge e;
1267   edge_iterator ei;
1268
1269   /* If there happens to be a fallthru edge (possibly created by cleanup_cfg
1270      call), we don't want it to go into newly created landing pad or other EH
1271      construct.  */
1272   for (ei = ei_start (BLOCK_FOR_INSN (insn)->preds); (e = ei_safe_edge (ei)); )
1273     if (e->flags & EDGE_FALLTHRU)
1274       force_nonfallthru (e);
1275     else
1276       ei_next (&ei);
1277   last = emit_insn_before (seq, insn);
1278   if (BARRIER_P (last))
1279     last = PREV_INSN (last);
1280   bb = create_basic_block (seq, last, BLOCK_FOR_INSN (insn)->prev_bb);
1281   update_bb_for_insn (bb);
1282   bb->flags |= BB_SUPERBLOCK;
1283   return bb;
1284 }
1285
1286 /* Generate the code to actually handle exceptions, which will follow the
1287    landing pads.  */
1288
1289 static void
1290 build_post_landing_pads (void)
1291 {
1292   int i;
1293
1294   for (i = cfun->eh->last_region_number; i > 0; --i)
1295     {
1296       struct eh_region *region;
1297       rtx seq;
1298
1299       region = VEC_index (eh_region, cfun->eh->region_array, i);
1300       /* Mind we don't process a region more than once.  */
1301       if (!region || region->region_number != i)
1302         continue;
1303
1304       switch (region->type)
1305         {
1306         case ERT_TRY:
1307           /* ??? Collect the set of all non-overlapping catch handlers
1308                all the way up the chain until blocked by a cleanup.  */
1309           /* ??? Outer try regions can share landing pads with inner
1310              try regions if the types are completely non-overlapping,
1311              and there are no intervening cleanups.  */
1312
1313           region->post_landing_pad = gen_label_rtx ();
1314
1315           start_sequence ();
1316
1317           emit_label (region->post_landing_pad);
1318
1319           /* ??? It is mighty inconvenient to call back into the
1320              switch statement generation code in expand_end_case.
1321              Rapid prototyping sez a sequence of ifs.  */
1322           {
1323             struct eh_region *c;
1324             for (c = region->u.try.catch; c ; c = c->u.catch.next_catch)
1325               {
1326                 if (c->u.catch.type_list == NULL)
1327                   emit_jump (c->label);
1328                 else
1329                   {
1330                     /* Need for one cmp/jump per type caught. Each type
1331                        list entry has a matching entry in the filter list
1332                        (see assign_filter_values).  */
1333                     tree tp_node = c->u.catch.type_list;
1334                     tree flt_node = c->u.catch.filter_list;
1335
1336                     for (; tp_node; )
1337                       {
1338                         emit_cmp_and_jump_insns
1339                           (cfun->eh->filter,
1340                            GEN_INT (tree_low_cst (TREE_VALUE (flt_node), 0)),
1341                            EQ, NULL_RTX,
1342                            targetm.eh_return_filter_mode (), 0, c->label);
1343
1344                         tp_node = TREE_CHAIN (tp_node);
1345                         flt_node = TREE_CHAIN (flt_node);
1346                       }
1347                   }
1348               }
1349           }
1350
1351           /* We delay the generation of the _Unwind_Resume until we generate
1352              landing pads.  We emit a marker here so as to get good control
1353              flow data in the meantime.  */
1354           region->resume
1355             = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
1356           emit_barrier ();
1357
1358           seq = get_insns ();
1359           end_sequence ();
1360
1361           emit_to_new_bb_before (seq, region->u.try.catch->label);
1362
1363           break;
1364
1365         case ERT_ALLOWED_EXCEPTIONS:
1366           region->post_landing_pad = gen_label_rtx ();
1367
1368           start_sequence ();
1369
1370           emit_label (region->post_landing_pad);
1371
1372           emit_cmp_and_jump_insns (cfun->eh->filter,
1373                                    GEN_INT (region->u.allowed.filter),
1374                                    EQ, NULL_RTX,
1375                                    targetm.eh_return_filter_mode (), 0, region->label);
1376
1377           /* We delay the generation of the _Unwind_Resume until we generate
1378              landing pads.  We emit a marker here so as to get good control
1379              flow data in the meantime.  */
1380           region->resume
1381             = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
1382           emit_barrier ();
1383
1384           seq = get_insns ();
1385           end_sequence ();
1386
1387           emit_to_new_bb_before (seq, region->label);
1388           break;
1389
1390         case ERT_CLEANUP:
1391         case ERT_MUST_NOT_THROW:
1392           region->post_landing_pad = region->label;
1393           break;
1394
1395         case ERT_CATCH:
1396         case ERT_THROW:
1397           /* Nothing to do.  */
1398           break;
1399
1400         default:
1401           gcc_unreachable ();
1402         }
1403     }
1404 }
1405
1406 /* Replace RESX patterns with jumps to the next handler if any, or calls to
1407    _Unwind_Resume otherwise.  */
1408
1409 static void
1410 connect_post_landing_pads (void)
1411 {
1412   int i;
1413
1414   for (i = cfun->eh->last_region_number; i > 0; --i)
1415     {
1416       struct eh_region *region;
1417       struct eh_region *outer;
1418       rtx seq;
1419       rtx barrier;
1420
1421       region = VEC_index (eh_region, cfun->eh->region_array, i);
1422       /* Mind we don't process a region more than once.  */
1423       if (!region || region->region_number != i)
1424         continue;
1425
1426       /* If there is no RESX, or it has been deleted by flow, there's
1427          nothing to fix up.  */
1428       if (! region->resume || INSN_DELETED_P (region->resume))
1429         continue;
1430
1431       /* Search for another landing pad in this function.  */
1432       for (outer = region->outer; outer ; outer = outer->outer)
1433         if (outer->post_landing_pad)
1434           break;
1435
1436       start_sequence ();
1437
1438       if (outer)
1439         {
1440           edge e;
1441           basic_block src, dest;
1442
1443           emit_jump (outer->post_landing_pad);
1444           src = BLOCK_FOR_INSN (region->resume);
1445           dest = BLOCK_FOR_INSN (outer->post_landing_pad);
1446           while (EDGE_COUNT (src->succs) > 0)
1447             remove_edge (EDGE_SUCC (src, 0));
1448           e = make_edge (src, dest, 0);
1449           e->probability = REG_BR_PROB_BASE;
1450           e->count = src->count;
1451         }
1452       else
1453         {
1454           emit_library_call (unwind_resume_libfunc, LCT_THROW,
1455                              VOIDmode, 1, cfun->eh->exc_ptr, ptr_mode);
1456
1457           /* What we just emitted was a throwing libcall, so it got a
1458              barrier automatically added after it.  If the last insn in
1459              the libcall sequence isn't the barrier, it's because the
1460              target emits multiple insns for a call, and there are insns
1461              after the actual call insn (which are redundant and would be
1462              optimized away).  The barrier is inserted exactly after the
1463              call insn, so let's go get that and delete the insns after
1464              it, because below we need the barrier to be the last insn in
1465              the sequence.  */
1466           delete_insns_since (NEXT_INSN (last_call_insn ()));
1467         }
1468
1469       seq = get_insns ();
1470       end_sequence ();
1471       barrier = emit_insn_before (seq, region->resume);
1472       /* Avoid duplicate barrier.  */
1473       gcc_assert (BARRIER_P (barrier));
1474       delete_insn (barrier);
1475       delete_insn (region->resume);
1476
1477       /* ??? From tree-ssa we can wind up with catch regions whose
1478          label is not instantiated, but whose resx is present.  Now
1479          that we've dealt with the resx, kill the region.  */
1480       if (region->label == NULL && region->type == ERT_CLEANUP)
1481         remove_eh_handler (region);
1482     }
1483 }
1484
1485 \f
1486 static void
1487 dw2_build_landing_pads (void)
1488 {
1489   int i;
1490   unsigned int j;
1491
1492   for (i = cfun->eh->last_region_number; i > 0; --i)
1493     {
1494       struct eh_region *region;
1495       rtx seq;
1496       basic_block bb;
1497       bool clobbers_hard_regs = false;
1498       edge e;
1499
1500       region = VEC_index (eh_region, cfun->eh->region_array, i);
1501       /* Mind we don't process a region more than once.  */
1502       if (!region || region->region_number != i)
1503         continue;
1504
1505       if (region->type != ERT_CLEANUP
1506           && region->type != ERT_TRY
1507           && region->type != ERT_ALLOWED_EXCEPTIONS)
1508         continue;
1509
1510       start_sequence ();
1511
1512       region->landing_pad = gen_label_rtx ();
1513       emit_label (region->landing_pad);
1514
1515 #ifdef HAVE_exception_receiver
1516       if (HAVE_exception_receiver)
1517         emit_insn (gen_exception_receiver ());
1518       else
1519 #endif
1520 #ifdef HAVE_nonlocal_goto_receiver
1521         if (HAVE_nonlocal_goto_receiver)
1522           emit_insn (gen_nonlocal_goto_receiver ());
1523         else
1524 #endif
1525           { /* Nothing */ }
1526
1527       /* If the eh_return data registers are call-saved, then we
1528          won't have considered them clobbered from the call that
1529          threw.  Kill them now.  */
1530       for (j = 0; ; ++j)
1531         {
1532           unsigned r = EH_RETURN_DATA_REGNO (j);
1533           if (r == INVALID_REGNUM)
1534             break;
1535           if (! call_used_regs[r])
1536             {
1537               emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, r)));
1538               clobbers_hard_regs = true;
1539             }
1540         }
1541
1542       if (clobbers_hard_regs)
1543         {
1544           /* @@@ This is a kludge.  Not all machine descriptions define a
1545              blockage insn, but we must not allow the code we just generated
1546              to be reordered by scheduling.  So emit an ASM_INPUT to act as
1547              blockage insn.  */
1548           emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
1549         }
1550
1551       emit_move_insn (cfun->eh->exc_ptr,
1552                       gen_rtx_REG (ptr_mode, EH_RETURN_DATA_REGNO (0)));
1553       emit_move_insn (cfun->eh->filter,
1554                       gen_rtx_REG (targetm.eh_return_filter_mode (),
1555                                    EH_RETURN_DATA_REGNO (1)));
1556
1557       seq = get_insns ();
1558       end_sequence ();
1559
1560       bb = emit_to_new_bb_before (seq, region->post_landing_pad);
1561       e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
1562       e->count = bb->count;
1563       e->probability = REG_BR_PROB_BASE;
1564     }
1565 }
1566
1567 \f
1568 struct sjlj_lp_info
1569 {
1570   int directly_reachable;
1571   int action_index;
1572   int dispatch_index;
1573   int call_site_index;
1574 };
1575
1576 static bool
1577 sjlj_find_directly_reachable_regions (struct sjlj_lp_info *lp_info)
1578 {
1579   rtx insn;
1580   bool found_one = false;
1581
1582   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
1583     {
1584       struct eh_region *region;
1585       enum reachable_code rc;
1586       tree type_thrown;
1587       rtx note;
1588
1589       if (! INSN_P (insn))
1590         continue;
1591
1592       note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1593       if (!note || INTVAL (XEXP (note, 0)) <= 0)
1594         continue;
1595
1596       region = VEC_index (eh_region, cfun->eh->region_array, INTVAL (XEXP (note, 0)));
1597
1598       type_thrown = NULL_TREE;
1599       if (region->type == ERT_THROW)
1600         {
1601           type_thrown = region->u.throw.type;
1602           region = region->outer;
1603         }
1604
1605       /* Find the first containing region that might handle the exception.
1606          That's the landing pad to which we will transfer control.  */
1607       rc = RNL_NOT_CAUGHT;
1608       for (; region; region = region->outer)
1609         {
1610           rc = reachable_next_level (region, type_thrown, NULL);
1611           if (rc != RNL_NOT_CAUGHT)
1612             break;
1613         }
1614       if (rc == RNL_MAYBE_CAUGHT || rc == RNL_CAUGHT)
1615         {
1616           lp_info[region->region_number].directly_reachable = 1;
1617           found_one = true;
1618         }
1619     }
1620
1621   return found_one;
1622 }
1623
1624 static void
1625 sjlj_assign_call_site_values (rtx dispatch_label, struct sjlj_lp_info *lp_info)
1626 {
1627   htab_t ar_hash;
1628   int i, index;
1629
1630   /* First task: build the action table.  */
1631
1632   VARRAY_UCHAR_INIT (cfun->eh->action_record_data, 64, "action_record_data");
1633   ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
1634
1635   for (i = cfun->eh->last_region_number; i > 0; --i)
1636     if (lp_info[i].directly_reachable)
1637       {
1638         struct eh_region *r = VEC_index (eh_region, cfun->eh->region_array, i);
1639
1640         r->landing_pad = dispatch_label;
1641         lp_info[i].action_index = collect_one_action_chain (ar_hash, r);
1642         if (lp_info[i].action_index != -1)
1643           cfun->uses_eh_lsda = 1;
1644       }
1645
1646   htab_delete (ar_hash);
1647
1648   /* Next: assign dispatch values.  In dwarf2 terms, this would be the
1649      landing pad label for the region.  For sjlj though, there is one
1650      common landing pad from which we dispatch to the post-landing pads.
1651
1652      A region receives a dispatch index if it is directly reachable
1653      and requires in-function processing.  Regions that share post-landing
1654      pads may share dispatch indices.  */
1655   /* ??? Post-landing pad sharing doesn't actually happen at the moment
1656      (see build_post_landing_pads) so we don't bother checking for it.  */
1657
1658   index = 0;
1659   for (i = cfun->eh->last_region_number; i > 0; --i)
1660     if (lp_info[i].directly_reachable)
1661       lp_info[i].dispatch_index = index++;
1662
1663   /* Finally: assign call-site values.  If dwarf2 terms, this would be
1664      the region number assigned by convert_to_eh_region_ranges, but
1665      handles no-action and must-not-throw differently.  */
1666
1667   call_site_base = 1;
1668   for (i = cfun->eh->last_region_number; i > 0; --i)
1669     if (lp_info[i].directly_reachable)
1670       {
1671         int action = lp_info[i].action_index;
1672
1673         /* Map must-not-throw to otherwise unused call-site index 0.  */
1674         if (action == -2)
1675           index = 0;
1676         /* Map no-action to otherwise unused call-site index -1.  */
1677         else if (action == -1)
1678           index = -1;
1679         /* Otherwise, look it up in the table.  */
1680         else
1681           index = add_call_site (GEN_INT (lp_info[i].dispatch_index), action);
1682
1683         lp_info[i].call_site_index = index;
1684       }
1685 }
1686
1687 static void
1688 sjlj_mark_call_sites (struct sjlj_lp_info *lp_info)
1689 {
1690   int last_call_site = -2;
1691   rtx insn, mem;
1692
1693   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
1694     {
1695       struct eh_region *region;
1696       int this_call_site;
1697       rtx note, before, p;
1698
1699       /* Reset value tracking at extended basic block boundaries.  */
1700       if (LABEL_P (insn))
1701         last_call_site = -2;
1702
1703       if (! INSN_P (insn))
1704         continue;
1705
1706       note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1707       if (!note)
1708         {
1709           /* Calls (and trapping insns) without notes are outside any
1710              exception handling region in this function.  Mark them as
1711              no action.  */
1712           if (CALL_P (insn)
1713               || (flag_non_call_exceptions
1714                   && may_trap_p (PATTERN (insn))))
1715             this_call_site = -1;
1716           else
1717             continue;
1718         }
1719       else
1720         {
1721           /* Calls that are known to not throw need not be marked.  */
1722           if (INTVAL (XEXP (note, 0)) <= 0)
1723             continue;
1724
1725           region = VEC_index (eh_region, cfun->eh->region_array, INTVAL (XEXP (note, 0)));
1726           this_call_site = lp_info[region->region_number].call_site_index;
1727         }
1728
1729       if (this_call_site == last_call_site)
1730         continue;
1731
1732       /* Don't separate a call from it's argument loads.  */
1733       before = insn;
1734       if (CALL_P (insn))
1735         before = find_first_parameter_load (insn, NULL_RTX);
1736
1737       start_sequence ();
1738       mem = adjust_address (cfun->eh->sjlj_fc, TYPE_MODE (integer_type_node),
1739                             sjlj_fc_call_site_ofs);
1740       emit_move_insn (mem, GEN_INT (this_call_site));
1741       p = get_insns ();
1742       end_sequence ();
1743
1744       emit_insn_before (p, before);
1745       last_call_site = this_call_site;
1746     }
1747 }
1748
1749 /* Construct the SjLj_Function_Context.  */
1750
1751 static void
1752 sjlj_emit_function_enter (rtx dispatch_label)
1753 {
1754   rtx fn_begin, fc, mem, seq;
1755   bool fn_begin_outside_block;
1756
1757   fc = cfun->eh->sjlj_fc;
1758
1759   start_sequence ();
1760
1761   /* We're storing this libcall's address into memory instead of
1762      calling it directly.  Thus, we must call assemble_external_libcall
1763      here, as we can not depend on emit_library_call to do it for us.  */
1764   assemble_external_libcall (eh_personality_libfunc);
1765   mem = adjust_address (fc, Pmode, sjlj_fc_personality_ofs);
1766   emit_move_insn (mem, eh_personality_libfunc);
1767
1768   mem = adjust_address (fc, Pmode, sjlj_fc_lsda_ofs);
1769   if (cfun->uses_eh_lsda)
1770     {
1771       char buf[20];
1772       rtx sym;
1773
1774       ASM_GENERATE_INTERNAL_LABEL (buf, "LLSDA", current_function_funcdef_no);
1775       sym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
1776       SYMBOL_REF_FLAGS (sym) = SYMBOL_FLAG_LOCAL;
1777       emit_move_insn (mem, sym);
1778     }
1779   else
1780     emit_move_insn (mem, const0_rtx);
1781
1782 #ifdef DONT_USE_BUILTIN_SETJMP
1783   {
1784     rtx x, note;
1785     x = emit_library_call_value (setjmp_libfunc, NULL_RTX, LCT_RETURNS_TWICE,
1786                                  TYPE_MODE (integer_type_node), 1,
1787                                  plus_constant (XEXP (fc, 0),
1788                                                 sjlj_fc_jbuf_ofs), Pmode);
1789
1790     note = emit_note (NOTE_INSN_EXPECTED_VALUE);
1791     NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, x, const0_rtx);
1792
1793     emit_cmp_and_jump_insns (x, const0_rtx, NE, 0,
1794                              TYPE_MODE (integer_type_node), 0, dispatch_label);
1795   }
1796 #else
1797   expand_builtin_setjmp_setup (plus_constant (XEXP (fc, 0), sjlj_fc_jbuf_ofs),
1798                                dispatch_label);
1799 #endif
1800
1801   emit_library_call (unwind_sjlj_register_libfunc, LCT_NORMAL, VOIDmode,
1802                      1, XEXP (fc, 0), Pmode);
1803
1804   seq = get_insns ();
1805   end_sequence ();
1806
1807   /* ??? Instead of doing this at the beginning of the function,
1808      do this in a block that is at loop level 0 and dominates all
1809      can_throw_internal instructions.  */
1810
1811   fn_begin_outside_block = true;
1812   for (fn_begin = get_insns (); ; fn_begin = NEXT_INSN (fn_begin))
1813     if (NOTE_P (fn_begin))
1814       {
1815         if (NOTE_LINE_NUMBER (fn_begin) == NOTE_INSN_FUNCTION_BEG)
1816           break;
1817         else if (NOTE_LINE_NUMBER (fn_begin) == NOTE_INSN_BASIC_BLOCK)
1818           fn_begin_outside_block = false;
1819       }
1820
1821   if (fn_begin_outside_block)
1822     insert_insn_on_edge (seq, single_succ_edge (ENTRY_BLOCK_PTR));
1823   else
1824     emit_insn_after (seq, fn_begin);
1825 }
1826
1827 /* Call back from expand_function_end to know where we should put
1828    the call to unwind_sjlj_unregister_libfunc if needed.  */
1829
1830 void
1831 sjlj_emit_function_exit_after (rtx after)
1832 {
1833   cfun->eh->sjlj_exit_after = after;
1834 }
1835
1836 static void
1837 sjlj_emit_function_exit (void)
1838 {
1839   rtx seq;
1840   edge e;
1841   edge_iterator ei;
1842
1843   start_sequence ();
1844
1845   emit_library_call (unwind_sjlj_unregister_libfunc, LCT_NORMAL, VOIDmode,
1846                      1, XEXP (cfun->eh->sjlj_fc, 0), Pmode);
1847
1848   seq = get_insns ();
1849   end_sequence ();
1850
1851   /* ??? Really this can be done in any block at loop level 0 that
1852      post-dominates all can_throw_internal instructions.  This is
1853      the last possible moment.  */
1854
1855   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
1856     if (e->flags & EDGE_FALLTHRU)
1857       break;
1858   if (e)
1859     {
1860       rtx insn;
1861
1862       /* Figure out whether the place we are supposed to insert libcall
1863          is inside the last basic block or after it.  In the other case
1864          we need to emit to edge.  */
1865       gcc_assert (e->src->next_bb == EXIT_BLOCK_PTR);
1866       for (insn = BB_HEAD (e->src); ; insn = NEXT_INSN (insn))
1867         {
1868           if (insn == cfun->eh->sjlj_exit_after)
1869             {
1870               if (LABEL_P (insn))
1871                 insn = NEXT_INSN (insn);
1872               emit_insn_after (seq, insn);
1873               return;
1874             }
1875           if (insn == BB_END (e->src))
1876             break;
1877         }
1878       insert_insn_on_edge (seq, e);
1879     }
1880 }
1881
1882 static void
1883 sjlj_emit_dispatch_table (rtx dispatch_label, struct sjlj_lp_info *lp_info)
1884 {
1885   int i, first_reachable;
1886   rtx mem, dispatch, seq, fc;
1887   rtx before;
1888   basic_block bb;
1889   edge e;
1890
1891   fc = cfun->eh->sjlj_fc;
1892
1893   start_sequence ();
1894
1895   emit_label (dispatch_label);
1896
1897 #ifndef DONT_USE_BUILTIN_SETJMP
1898   expand_builtin_setjmp_receiver (dispatch_label);
1899 #endif
1900
1901   /* Load up dispatch index, exc_ptr and filter values from the
1902      function context.  */
1903   mem = adjust_address (fc, TYPE_MODE (integer_type_node),
1904                         sjlj_fc_call_site_ofs);
1905   dispatch = copy_to_reg (mem);
1906
1907   mem = adjust_address (fc, word_mode, sjlj_fc_data_ofs);
1908   if (word_mode != ptr_mode)
1909     {
1910 #ifdef POINTERS_EXTEND_UNSIGNED
1911       mem = convert_memory_address (ptr_mode, mem);
1912 #else
1913       mem = convert_to_mode (ptr_mode, mem, 0);
1914 #endif
1915     }
1916   emit_move_insn (cfun->eh->exc_ptr, mem);
1917
1918   mem = adjust_address (fc, word_mode, sjlj_fc_data_ofs + UNITS_PER_WORD);
1919   emit_move_insn (cfun->eh->filter, mem);
1920
1921   /* Jump to one of the directly reachable regions.  */
1922   /* ??? This really ought to be using a switch statement.  */
1923
1924   first_reachable = 0;
1925   for (i = cfun->eh->last_region_number; i > 0; --i)
1926     {
1927       if (! lp_info[i].directly_reachable)
1928         continue;
1929
1930       if (! first_reachable)
1931         {
1932           first_reachable = i;
1933           continue;
1934         }
1935
1936       emit_cmp_and_jump_insns (dispatch, GEN_INT (lp_info[i].dispatch_index),
1937                                EQ, NULL_RTX, TYPE_MODE (integer_type_node), 0,
1938                                ((struct eh_region *)VEC_index (eh_region, cfun->eh->region_array, i))
1939                                 ->post_landing_pad);
1940     }
1941
1942   seq = get_insns ();
1943   end_sequence ();
1944
1945   before = (((struct eh_region *)VEC_index (eh_region, cfun->eh->region_array, first_reachable))
1946             ->post_landing_pad);
1947
1948   bb = emit_to_new_bb_before (seq, before);
1949   e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
1950   e->count = bb->count;
1951   e->probability = REG_BR_PROB_BASE;
1952 }
1953
1954 static void
1955 sjlj_build_landing_pads (void)
1956 {
1957   struct sjlj_lp_info *lp_info;
1958
1959   lp_info = XCNEWVEC (struct sjlj_lp_info, cfun->eh->last_region_number + 1);
1960
1961   if (sjlj_find_directly_reachable_regions (lp_info))
1962     {
1963       rtx dispatch_label = gen_label_rtx ();
1964
1965       cfun->eh->sjlj_fc
1966         = assign_stack_local (TYPE_MODE (sjlj_fc_type_node),
1967                               int_size_in_bytes (sjlj_fc_type_node),
1968                               TYPE_ALIGN (sjlj_fc_type_node));
1969
1970       sjlj_assign_call_site_values (dispatch_label, lp_info);
1971       sjlj_mark_call_sites (lp_info);
1972
1973       sjlj_emit_function_enter (dispatch_label);
1974       sjlj_emit_dispatch_table (dispatch_label, lp_info);
1975       sjlj_emit_function_exit ();
1976     }
1977
1978   free (lp_info);
1979 }
1980
1981 void
1982 finish_eh_generation (void)
1983 {
1984   basic_block bb;
1985
1986   /* Nothing to do if no regions created.  */
1987   if (cfun->eh->region_tree == NULL)
1988     return;
1989
1990   /* The object here is to provide find_basic_blocks with detailed
1991      information (via reachable_handlers) on how exception control
1992      flows within the function.  In this first pass, we can include
1993      type information garnered from ERT_THROW and ERT_ALLOWED_EXCEPTIONS
1994      regions, and hope that it will be useful in deleting unreachable
1995      handlers.  Subsequently, we will generate landing pads which will
1996      connect many of the handlers, and then type information will not
1997      be effective.  Still, this is a win over previous implementations.  */
1998
1999   /* These registers are used by the landing pads.  Make sure they
2000      have been generated.  */
2001   get_exception_pointer (cfun);
2002   get_exception_filter (cfun);
2003
2004   /* Construct the landing pads.  */
2005
2006   assign_filter_values ();
2007   build_post_landing_pads ();
2008   connect_post_landing_pads ();
2009   if (USING_SJLJ_EXCEPTIONS)
2010     sjlj_build_landing_pads ();
2011   else
2012     dw2_build_landing_pads ();
2013
2014   cfun->eh->built_landing_pads = 1;
2015
2016   /* We've totally changed the CFG.  Start over.  */
2017   find_exception_handler_labels ();
2018   break_superblocks ();
2019   if (USING_SJLJ_EXCEPTIONS)
2020     commit_edge_insertions ();
2021   FOR_EACH_BB (bb)
2022     {
2023       edge e;
2024       edge_iterator ei;
2025       bool eh = false;
2026       for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2027         {
2028           if (e->flags & EDGE_EH)
2029             {
2030               remove_edge (e);
2031               eh = true;
2032             }
2033           else
2034             ei_next (&ei);
2035         }
2036       if (eh)
2037         rtl_make_eh_edge (NULL, bb, BB_END (bb));
2038     }
2039 }
2040 \f
2041 static hashval_t
2042 ehl_hash (const void *pentry)
2043 {
2044   struct ehl_map_entry *entry = (struct ehl_map_entry *) pentry;
2045
2046   /* 2^32 * ((sqrt(5) - 1) / 2) */
2047   const hashval_t scaled_golden_ratio = 0x9e3779b9;
2048   return CODE_LABEL_NUMBER (entry->label) * scaled_golden_ratio;
2049 }
2050
2051 static int
2052 ehl_eq (const void *pentry, const void *pdata)
2053 {
2054   struct ehl_map_entry *entry = (struct ehl_map_entry *) pentry;
2055   struct ehl_map_entry *data = (struct ehl_map_entry *) pdata;
2056
2057   return entry->label == data->label;
2058 }
2059
2060 /* This section handles removing dead code for flow.  */
2061
2062 /* Remove LABEL from exception_handler_label_map.  */
2063
2064 static void
2065 remove_exception_handler_label (rtx label)
2066 {
2067   struct ehl_map_entry **slot, tmp;
2068
2069   /* If exception_handler_label_map was not built yet,
2070      there is nothing to do.  */
2071   if (cfun->eh->exception_handler_label_map == NULL)
2072     return;
2073
2074   tmp.label = label;
2075   slot = (struct ehl_map_entry **)
2076     htab_find_slot (cfun->eh->exception_handler_label_map, &tmp, NO_INSERT);
2077   gcc_assert (slot);
2078
2079   htab_clear_slot (cfun->eh->exception_handler_label_map, (void **) slot);
2080 }
2081
2082 /* Splice REGION from the region tree etc.  */
2083
2084 static void
2085 remove_eh_handler (struct eh_region *region)
2086 {
2087   struct eh_region **pp, **pp_start, *p, *outer, *inner;
2088   rtx lab;
2089
2090   /* For the benefit of efficiently handling REG_EH_REGION notes,
2091      replace this region in the region array with its containing
2092      region.  Note that previous region deletions may result in
2093      multiple copies of this region in the array, so we have a
2094      list of alternate numbers by which we are known.  */
2095
2096   outer = region->outer;
2097   VEC_replace (eh_region, cfun->eh->region_array, region->region_number, outer);
2098   if (region->aka)
2099     {
2100       unsigned i;
2101       bitmap_iterator bi;
2102
2103       EXECUTE_IF_SET_IN_BITMAP (region->aka, 0, i, bi)
2104         {
2105           VEC_replace (eh_region, cfun->eh->region_array, i, outer);
2106         }
2107     }
2108
2109   if (outer)
2110     {
2111       if (!outer->aka)
2112         outer->aka = BITMAP_GGC_ALLOC ();
2113       if (region->aka)
2114         bitmap_ior_into (outer->aka, region->aka);
2115       bitmap_set_bit (outer->aka, region->region_number);
2116     }
2117
2118   if (cfun->eh->built_landing_pads)
2119     lab = region->landing_pad;
2120   else
2121     lab = region->label;
2122   if (lab)
2123     remove_exception_handler_label (lab);
2124
2125   if (outer)
2126     pp_start = &outer->inner;
2127   else
2128     pp_start = &cfun->eh->region_tree;
2129   for (pp = pp_start, p = *pp; p != region; pp = &p->next_peer, p = *pp)
2130     continue;
2131   *pp = region->next_peer;
2132
2133   inner = region->inner;
2134   if (inner)
2135     {
2136       for (p = inner; p->next_peer ; p = p->next_peer)
2137         p->outer = outer;
2138       p->outer = outer;
2139
2140       p->next_peer = *pp_start;
2141       *pp_start = inner;
2142     }
2143
2144   if (region->type == ERT_CATCH)
2145     {
2146       struct eh_region *try, *next, *prev;
2147
2148       for (try = region->next_peer;
2149            try->type == ERT_CATCH;
2150            try = try->next_peer)
2151         continue;
2152       gcc_assert (try->type == ERT_TRY);
2153
2154       next = region->u.catch.next_catch;
2155       prev = region->u.catch.prev_catch;
2156
2157       if (next)
2158         next->u.catch.prev_catch = prev;
2159       else
2160         try->u.try.last_catch = prev;
2161       if (prev)
2162         prev->u.catch.next_catch = next;
2163       else
2164         {
2165           try->u.try.catch = next;
2166           if (! next)
2167             remove_eh_handler (try);
2168         }
2169     }
2170 }
2171
2172 /* LABEL heads a basic block that is about to be deleted.  If this
2173    label corresponds to an exception region, we may be able to
2174    delete the region.  */
2175
2176 void
2177 maybe_remove_eh_handler (rtx label)
2178 {
2179   struct ehl_map_entry **slot, tmp;
2180   struct eh_region *region;
2181
2182   /* ??? After generating landing pads, it's not so simple to determine
2183      if the region data is completely unused.  One must examine the
2184      landing pad and the post landing pad, and whether an inner try block
2185      is referencing the catch handlers directly.  */
2186   if (cfun->eh->built_landing_pads)
2187     return;
2188
2189   tmp.label = label;
2190   slot = (struct ehl_map_entry **)
2191     htab_find_slot (cfun->eh->exception_handler_label_map, &tmp, NO_INSERT);
2192   if (! slot)
2193     return;
2194   region = (*slot)->region;
2195   if (! region)
2196     return;
2197
2198   /* Flow will want to remove MUST_NOT_THROW regions as unreachable
2199      because there is no path to the fallback call to terminate.
2200      But the region continues to affect call-site data until there
2201      are no more contained calls, which we don't see here.  */
2202   if (region->type == ERT_MUST_NOT_THROW)
2203     {
2204       htab_clear_slot (cfun->eh->exception_handler_label_map, (void **) slot);
2205       region->label = NULL_RTX;
2206     }
2207   else
2208     remove_eh_handler (region);
2209 }
2210
2211 /* Invokes CALLBACK for every exception handler label.  Only used by old
2212    loop hackery; should not be used by new code.  */
2213
2214 void
2215 for_each_eh_label (void (*callback) (rtx))
2216 {
2217   htab_traverse (cfun->eh->exception_handler_label_map, for_each_eh_label_1,
2218                  (void *) &callback);
2219 }
2220
2221 static int
2222 for_each_eh_label_1 (void **pentry, void *data)
2223 {
2224   struct ehl_map_entry *entry = *(struct ehl_map_entry **)pentry;
2225   void (*callback) (rtx) = *(void (**) (rtx)) data;
2226
2227   (*callback) (entry->label);
2228   return 1;
2229 }
2230
2231 /* Invoke CALLBACK for every exception region in the current function.  */
2232
2233 void
2234 for_each_eh_region (void (*callback) (struct eh_region *))
2235 {
2236   int i, n = cfun->eh->last_region_number;
2237   for (i = 1; i <= n; ++i)
2238     {
2239       struct eh_region *region;
2240
2241       region = VEC_index (eh_region, cfun->eh->region_array, i);
2242       if (region)
2243         (*callback) (region);
2244     }
2245 }
2246 \f
2247 /* This section describes CFG exception edges for flow.  */
2248
2249 /* For communicating between calls to reachable_next_level.  */
2250 struct reachable_info
2251 {
2252   tree types_caught;
2253   tree types_allowed;
2254   void (*callback) (struct eh_region *, void *);
2255   void *callback_data;
2256   bool saw_any_handlers;
2257 };
2258
2259 /* A subroutine of reachable_next_level.  Return true if TYPE, or a
2260    base class of TYPE, is in HANDLED.  */
2261
2262 static int
2263 check_handled (tree handled, tree type)
2264 {
2265   tree t;
2266
2267   /* We can check for exact matches without front-end help.  */
2268   if (! lang_eh_type_covers)
2269     {
2270       for (t = handled; t ; t = TREE_CHAIN (t))
2271         if (TREE_VALUE (t) == type)
2272           return 1;
2273     }
2274   else
2275     {
2276       for (t = handled; t ; t = TREE_CHAIN (t))
2277         if ((*lang_eh_type_covers) (TREE_VALUE (t), type))
2278           return 1;
2279     }
2280
2281   return 0;
2282 }
2283
2284 /* A subroutine of reachable_next_level.  If we are collecting a list
2285    of handlers, add one.  After landing pad generation, reference
2286    it instead of the handlers themselves.  Further, the handlers are
2287    all wired together, so by referencing one, we've got them all.
2288    Before landing pad generation we reference each handler individually.
2289
2290    LP_REGION contains the landing pad; REGION is the handler.  */
2291
2292 static void
2293 add_reachable_handler (struct reachable_info *info,
2294                        struct eh_region *lp_region, struct eh_region *region)
2295 {
2296   if (! info)
2297     return;
2298
2299   info->saw_any_handlers = true;
2300
2301   if (cfun->eh->built_landing_pads)
2302     info->callback (lp_region, info->callback_data);
2303   else
2304     info->callback (region, info->callback_data);
2305 }
2306
2307 /* Process one level of exception regions for reachability.
2308    If TYPE_THROWN is non-null, then it is the *exact* type being
2309    propagated.  If INFO is non-null, then collect handler labels
2310    and caught/allowed type information between invocations.  */
2311
2312 static enum reachable_code
2313 reachable_next_level (struct eh_region *region, tree type_thrown,
2314                       struct reachable_info *info)
2315 {
2316   switch (region->type)
2317     {
2318     case ERT_CLEANUP:
2319       /* Before landing-pad generation, we model control flow
2320          directly to the individual handlers.  In this way we can
2321          see that catch handler types may shadow one another.  */
2322       add_reachable_handler (info, region, region);
2323       return RNL_MAYBE_CAUGHT;
2324
2325     case ERT_TRY:
2326       {
2327         struct eh_region *c;
2328         enum reachable_code ret = RNL_NOT_CAUGHT;
2329
2330         for (c = region->u.try.catch; c ; c = c->u.catch.next_catch)
2331           {
2332             /* A catch-all handler ends the search.  */
2333             if (c->u.catch.type_list == NULL)
2334               {
2335                 add_reachable_handler (info, region, c);
2336                 return RNL_CAUGHT;
2337               }
2338
2339             if (type_thrown)
2340               {
2341                 /* If we have at least one type match, end the search.  */
2342                 tree tp_node = c->u.catch.type_list;
2343
2344                 for (; tp_node; tp_node = TREE_CHAIN (tp_node))
2345                   {
2346                     tree type = TREE_VALUE (tp_node);
2347
2348                     if (type == type_thrown
2349                         || (lang_eh_type_covers
2350                             && (*lang_eh_type_covers) (type, type_thrown)))
2351                       {
2352                         add_reachable_handler (info, region, c);
2353                         return RNL_CAUGHT;
2354                       }
2355                   }
2356
2357                 /* If we have definitive information of a match failure,
2358                    the catch won't trigger.  */
2359                 if (lang_eh_type_covers)
2360                   return RNL_NOT_CAUGHT;
2361               }
2362
2363             /* At this point, we either don't know what type is thrown or
2364                don't have front-end assistance to help deciding if it is
2365                covered by one of the types in the list for this region.
2366
2367                We'd then like to add this region to the list of reachable
2368                handlers since it is indeed potentially reachable based on the
2369                information we have.
2370
2371                Actually, this handler is for sure not reachable if all the
2372                types it matches have already been caught. That is, it is only
2373                potentially reachable if at least one of the types it catches
2374                has not been previously caught.  */
2375
2376             if (! info)
2377               ret = RNL_MAYBE_CAUGHT;
2378             else
2379               {
2380                 tree tp_node = c->u.catch.type_list;
2381                 bool maybe_reachable = false;
2382
2383                 /* Compute the potential reachability of this handler and
2384                    update the list of types caught at the same time.  */
2385                 for (; tp_node; tp_node = TREE_CHAIN (tp_node))
2386                   {
2387                     tree type = TREE_VALUE (tp_node);
2388
2389                     if (! check_handled (info->types_caught, type))
2390                       {
2391                         info->types_caught
2392                           = tree_cons (NULL, type, info->types_caught);
2393
2394                         maybe_reachable = true;
2395                       }
2396                   }
2397
2398                 if (maybe_reachable)
2399                   {
2400                     add_reachable_handler (info, region, c);
2401
2402                     /* ??? If the catch type is a base class of every allowed
2403                        type, then we know we can stop the search.  */
2404                     ret = RNL_MAYBE_CAUGHT;
2405                   }
2406               }
2407           }
2408
2409         return ret;
2410       }
2411
2412     case ERT_ALLOWED_EXCEPTIONS:
2413       /* An empty list of types definitely ends the search.  */
2414       if (region->u.allowed.type_list == NULL_TREE)
2415         {
2416           add_reachable_handler (info, region, region);
2417           return RNL_CAUGHT;
2418         }
2419
2420       /* Collect a list of lists of allowed types for use in detecting
2421          when a catch may be transformed into a catch-all.  */
2422       if (info)
2423         info->types_allowed = tree_cons (NULL_TREE,
2424                                          region->u.allowed.type_list,
2425                                          info->types_allowed);
2426
2427       /* If we have definitive information about the type hierarchy,
2428          then we can tell if the thrown type will pass through the
2429          filter.  */
2430       if (type_thrown && lang_eh_type_covers)
2431         {
2432           if (check_handled (region->u.allowed.type_list, type_thrown))
2433             return RNL_NOT_CAUGHT;
2434           else
2435             {
2436               add_reachable_handler (info, region, region);
2437               return RNL_CAUGHT;
2438             }
2439         }
2440
2441       add_reachable_handler (info, region, region);
2442       return RNL_MAYBE_CAUGHT;
2443
2444     case ERT_CATCH:
2445       /* Catch regions are handled by their controlling try region.  */
2446       return RNL_NOT_CAUGHT;
2447
2448     case ERT_MUST_NOT_THROW:
2449       /* Here we end our search, since no exceptions may propagate.
2450          If we've touched down at some landing pad previous, then the
2451          explicit function call we generated may be used.  Otherwise
2452          the call is made by the runtime.
2453
2454          Before inlining, do not perform this optimization.  We may
2455          inline a subroutine that contains handlers, and that will
2456          change the value of saw_any_handlers.  */
2457
2458       if ((info && info->saw_any_handlers) || !cfun->after_inlining)
2459         {
2460           add_reachable_handler (info, region, region);
2461           return RNL_CAUGHT;
2462         }
2463       else
2464         return RNL_BLOCKED;
2465
2466     case ERT_THROW:
2467     case ERT_UNKNOWN:
2468       /* Shouldn't see these here.  */
2469       gcc_unreachable ();
2470       break;
2471     default:
2472       gcc_unreachable ();
2473     }
2474 }
2475
2476 /* Invoke CALLBACK on each region reachable from REGION_NUMBER.  */
2477
2478 void
2479 foreach_reachable_handler (int region_number, bool is_resx,
2480                            void (*callback) (struct eh_region *, void *),
2481                            void *callback_data)
2482 {
2483   struct reachable_info info;
2484   struct eh_region *region;
2485   tree type_thrown;
2486
2487   memset (&info, 0, sizeof (info));
2488   info.callback = callback;
2489   info.callback_data = callback_data;
2490
2491   region = VEC_index (eh_region, cfun->eh->region_array, region_number);
2492
2493   type_thrown = NULL_TREE;
2494   if (is_resx)
2495     {
2496       /* A RESX leaves a region instead of entering it.  Thus the
2497          region itself may have been deleted out from under us.  */
2498       if (region == NULL)
2499         return;
2500       region = region->outer;
2501     }
2502   else if (region->type == ERT_THROW)
2503     {
2504       type_thrown = region->u.throw.type;
2505       region = region->outer;
2506     }
2507
2508   while (region)
2509     {
2510       if (reachable_next_level (region, type_thrown, &info) >= RNL_CAUGHT)
2511         break;
2512       /* If we have processed one cleanup, there is no point in
2513          processing any more of them.  Each cleanup will have an edge
2514          to the next outer cleanup region, so the flow graph will be
2515          accurate.  */
2516       if (region->type == ERT_CLEANUP)
2517         region = region->u.cleanup.prev_try;
2518       else
2519         region = region->outer;
2520     }
2521 }
2522
2523 /* Retrieve a list of labels of exception handlers which can be
2524    reached by a given insn.  */
2525
2526 static void
2527 arh_to_landing_pad (struct eh_region *region, void *data)
2528 {
2529   rtx *p_handlers = data;
2530   if (! *p_handlers)
2531     *p_handlers = alloc_INSN_LIST (region->landing_pad, NULL_RTX);
2532 }
2533
2534 static void
2535 arh_to_label (struct eh_region *region, void *data)
2536 {
2537   rtx *p_handlers = data;
2538   *p_handlers = alloc_INSN_LIST (region->label, *p_handlers);
2539 }
2540
2541 rtx
2542 reachable_handlers (rtx insn)
2543 {
2544   bool is_resx = false;
2545   rtx handlers = NULL;
2546   int region_number;
2547
2548   if (JUMP_P (insn)
2549       && GET_CODE (PATTERN (insn)) == RESX)
2550     {
2551       region_number = XINT (PATTERN (insn), 0);
2552       is_resx = true;
2553     }
2554   else
2555     {
2556       rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2557       if (!note || INTVAL (XEXP (note, 0)) <= 0)
2558         return NULL;
2559       region_number = INTVAL (XEXP (note, 0));
2560     }
2561
2562   foreach_reachable_handler (region_number, is_resx,
2563                              (cfun->eh->built_landing_pads
2564                               ? arh_to_landing_pad
2565                               : arh_to_label),
2566                              &handlers);
2567
2568   return handlers;
2569 }
2570
2571 /* Determine if the given INSN can throw an exception that is caught
2572    within the function.  */
2573
2574 bool
2575 can_throw_internal_1 (int region_number, bool is_resx)
2576 {
2577   struct eh_region *region;
2578   tree type_thrown;
2579
2580   region = VEC_index (eh_region, cfun->eh->region_array, region_number);
2581
2582   type_thrown = NULL_TREE;
2583   if (is_resx)
2584     region = region->outer;
2585   else if (region->type == ERT_THROW)
2586     {
2587       type_thrown = region->u.throw.type;
2588       region = region->outer;
2589     }
2590
2591   /* If this exception is ignored by each and every containing region,
2592      then control passes straight out.  The runtime may handle some
2593      regions, which also do not require processing internally.  */
2594   for (; region; region = region->outer)
2595     {
2596       enum reachable_code how = reachable_next_level (region, type_thrown, 0);
2597       if (how == RNL_BLOCKED)
2598         return false;
2599       if (how != RNL_NOT_CAUGHT)
2600         return true;
2601     }
2602
2603   return false;
2604 }
2605
2606 bool
2607 can_throw_internal (rtx insn)
2608 {
2609   rtx note;
2610
2611   if (! INSN_P (insn))
2612     return false;
2613
2614   if (JUMP_P (insn)
2615       && GET_CODE (PATTERN (insn)) == RESX
2616       && XINT (PATTERN (insn), 0) > 0)
2617     return can_throw_internal_1 (XINT (PATTERN (insn), 0), true);
2618
2619   if (NONJUMP_INSN_P (insn)
2620       && GET_CODE (PATTERN (insn)) == SEQUENCE)
2621     insn = XVECEXP (PATTERN (insn), 0, 0);
2622
2623   /* Every insn that might throw has an EH_REGION note.  */
2624   note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2625   if (!note || INTVAL (XEXP (note, 0)) <= 0)
2626     return false;
2627
2628   return can_throw_internal_1 (INTVAL (XEXP (note, 0)), false);
2629 }
2630
2631 /* Determine if the given INSN can throw an exception that is
2632    visible outside the function.  */
2633
2634 bool
2635 can_throw_external_1 (int region_number, bool is_resx)
2636 {
2637   struct eh_region *region;
2638   tree type_thrown;
2639
2640   region = VEC_index (eh_region, cfun->eh->region_array, region_number);
2641
2642   type_thrown = NULL_TREE;
2643   if (is_resx)
2644     region = region->outer;
2645   else if (region->type == ERT_THROW)
2646     {
2647       type_thrown = region->u.throw.type;
2648       region = region->outer;
2649     }
2650
2651   /* If the exception is caught or blocked by any containing region,
2652      then it is not seen by any calling function.  */
2653   for (; region ; region = region->outer)
2654     if (reachable_next_level (region, type_thrown, NULL) >= RNL_CAUGHT)
2655       return false;
2656
2657   return true;
2658 }
2659
2660 bool
2661 can_throw_external (rtx insn)
2662 {
2663   rtx note;
2664
2665   if (! INSN_P (insn))
2666     return false;
2667
2668   if (JUMP_P (insn)
2669       && GET_CODE (PATTERN (insn)) == RESX
2670       && XINT (PATTERN (insn), 0) > 0)
2671     return can_throw_external_1 (XINT (PATTERN (insn), 0), true);
2672
2673   if (NONJUMP_INSN_P (insn)
2674       && GET_CODE (PATTERN (insn)) == SEQUENCE)
2675     insn = XVECEXP (PATTERN (insn), 0, 0);
2676
2677   note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2678   if (!note)
2679     {
2680       /* Calls (and trapping insns) without notes are outside any
2681          exception handling region in this function.  We have to
2682          assume it might throw.  Given that the front end and middle
2683          ends mark known NOTHROW functions, this isn't so wildly
2684          inaccurate.  */
2685       return (CALL_P (insn)
2686               || (flag_non_call_exceptions
2687                   && may_trap_p (PATTERN (insn))));
2688     }
2689   if (INTVAL (XEXP (note, 0)) <= 0)
2690     return false;
2691
2692   return can_throw_external_1 (INTVAL (XEXP (note, 0)), false);
2693 }
2694
2695 /* Set TREE_NOTHROW and cfun->all_throwers_are_sibcalls.  */
2696
2697 unsigned int
2698 set_nothrow_function_flags (void)
2699 {
2700   rtx insn;
2701
2702   TREE_NOTHROW (current_function_decl) = 1;
2703
2704   /* Assume cfun->all_throwers_are_sibcalls until we encounter
2705      something that can throw an exception.  We specifically exempt
2706      CALL_INSNs that are SIBLING_CALL_P, as these are really jumps,
2707      and can't throw.  Most CALL_INSNs are not SIBLING_CALL_P, so this
2708      is optimistic.  */
2709
2710   cfun->all_throwers_are_sibcalls = 1;
2711
2712   if (! flag_exceptions)
2713     return 0;
2714
2715   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2716     if (can_throw_external (insn))
2717       {
2718         TREE_NOTHROW (current_function_decl) = 0;
2719
2720         if (!CALL_P (insn) || !SIBLING_CALL_P (insn))
2721           {
2722             cfun->all_throwers_are_sibcalls = 0;
2723             return 0;
2724           }
2725       }
2726
2727   for (insn = current_function_epilogue_delay_list; insn;
2728        insn = XEXP (insn, 1))
2729     if (can_throw_external (insn))
2730       {
2731         TREE_NOTHROW (current_function_decl) = 0;
2732
2733         if (!CALL_P (insn) || !SIBLING_CALL_P (insn))
2734           {
2735             cfun->all_throwers_are_sibcalls = 0;
2736             return 0;
2737           }
2738       }
2739   return 0;
2740 }
2741
2742 struct tree_opt_pass pass_set_nothrow_function_flags =
2743 {
2744   NULL,                                 /* name */
2745   NULL,                                 /* gate */
2746   set_nothrow_function_flags,           /* execute */
2747   NULL,                                 /* sub */
2748   NULL,                                 /* next */
2749   0,                                    /* static_pass_number */
2750   0,                                    /* tv_id */
2751   0,                                    /* properties_required */
2752   0,                                    /* properties_provided */
2753   0,                                    /* properties_destroyed */
2754   0,                                    /* todo_flags_start */
2755   0,                                    /* todo_flags_finish */
2756   0                                     /* letter */
2757 };
2758
2759 \f
2760 /* Various hooks for unwind library.  */
2761
2762 /* Do any necessary initialization to access arbitrary stack frames.
2763    On the SPARC, this means flushing the register windows.  */
2764
2765 void
2766 expand_builtin_unwind_init (void)
2767 {
2768   /* Set this so all the registers get saved in our frame; we need to be
2769      able to copy the saved values for any registers from frames we unwind.  */
2770   current_function_has_nonlocal_label = 1;
2771
2772 #ifdef SETUP_FRAME_ADDRESSES
2773   SETUP_FRAME_ADDRESSES ();
2774 #endif
2775 }
2776
2777 rtx
2778 expand_builtin_eh_return_data_regno (tree arglist)
2779 {
2780   tree which = TREE_VALUE (arglist);
2781   unsigned HOST_WIDE_INT iwhich;
2782
2783   if (TREE_CODE (which) != INTEGER_CST)
2784     {
2785       error ("argument of %<__builtin_eh_return_regno%> must be constant");
2786       return constm1_rtx;
2787     }
2788
2789   iwhich = tree_low_cst (which, 1);
2790   iwhich = EH_RETURN_DATA_REGNO (iwhich);
2791   if (iwhich == INVALID_REGNUM)
2792     return constm1_rtx;
2793
2794 #ifdef DWARF_FRAME_REGNUM
2795   iwhich = DWARF_FRAME_REGNUM (iwhich);
2796 #else
2797   iwhich = DBX_REGISTER_NUMBER (iwhich);
2798 #endif
2799
2800   return GEN_INT (iwhich);
2801 }
2802
2803 /* Given a value extracted from the return address register or stack slot,
2804    return the actual address encoded in that value.  */
2805
2806 rtx
2807 expand_builtin_extract_return_addr (tree addr_tree)
2808 {
2809   rtx addr = expand_expr (addr_tree, NULL_RTX, Pmode, 0);
2810
2811   if (GET_MODE (addr) != Pmode
2812       && GET_MODE (addr) != VOIDmode)
2813     {
2814 #ifdef POINTERS_EXTEND_UNSIGNED
2815       addr = convert_memory_address (Pmode, addr);
2816 #else
2817       addr = convert_to_mode (Pmode, addr, 0);
2818 #endif
2819     }
2820
2821   /* First mask out any unwanted bits.  */
2822 #ifdef MASK_RETURN_ADDR
2823   expand_and (Pmode, addr, MASK_RETURN_ADDR, addr);
2824 #endif
2825
2826   /* Then adjust to find the real return address.  */
2827 #if defined (RETURN_ADDR_OFFSET)
2828   addr = plus_constant (addr, RETURN_ADDR_OFFSET);
2829 #endif
2830
2831   return addr;
2832 }
2833
2834 /* Given an actual address in addr_tree, do any necessary encoding
2835    and return the value to be stored in the return address register or
2836    stack slot so the epilogue will return to that address.  */
2837
2838 rtx
2839 expand_builtin_frob_return_addr (tree addr_tree)
2840 {
2841   rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, 0);
2842
2843   addr = convert_memory_address (Pmode, addr);
2844
2845 #ifdef RETURN_ADDR_OFFSET
2846   addr = force_reg (Pmode, addr);
2847   addr = plus_constant (addr, -RETURN_ADDR_OFFSET);
2848 #endif
2849
2850   return addr;
2851 }
2852
2853 /* Set up the epilogue with the magic bits we'll need to return to the
2854    exception handler.  */
2855
2856 void
2857 expand_builtin_eh_return (tree stackadj_tree ATTRIBUTE_UNUSED,
2858                           tree handler_tree)
2859 {
2860   rtx tmp;
2861
2862 #ifdef EH_RETURN_STACKADJ_RTX
2863   tmp = expand_expr (stackadj_tree, cfun->eh->ehr_stackadj, VOIDmode, 0);
2864   tmp = convert_memory_address (Pmode, tmp);
2865   if (!cfun->eh->ehr_stackadj)
2866     cfun->eh->ehr_stackadj = copy_to_reg (tmp);
2867   else if (tmp != cfun->eh->ehr_stackadj)
2868     emit_move_insn (cfun->eh->ehr_stackadj, tmp);
2869 #endif
2870
2871   tmp = expand_expr (handler_tree, cfun->eh->ehr_handler, VOIDmode, 0);
2872   tmp = convert_memory_address (Pmode, tmp);
2873   if (!cfun->eh->ehr_handler)
2874     cfun->eh->ehr_handler = copy_to_reg (tmp);
2875   else if (tmp != cfun->eh->ehr_handler)
2876     emit_move_insn (cfun->eh->ehr_handler, tmp);
2877
2878   if (!cfun->eh->ehr_label)
2879     cfun->eh->ehr_label = gen_label_rtx ();
2880   emit_jump (cfun->eh->ehr_label);
2881 }
2882
2883 void
2884 expand_eh_return (void)
2885 {
2886   rtx around_label;
2887
2888   if (! cfun->eh->ehr_label)
2889     return;
2890
2891   current_function_calls_eh_return = 1;
2892
2893 #ifdef EH_RETURN_STACKADJ_RTX
2894   emit_move_insn (EH_RETURN_STACKADJ_RTX, const0_rtx);
2895 #endif
2896
2897   around_label = gen_label_rtx ();
2898   emit_jump (around_label);
2899
2900   emit_label (cfun->eh->ehr_label);
2901   clobber_return_register ();
2902
2903 #ifdef EH_RETURN_STACKADJ_RTX
2904   emit_move_insn (EH_RETURN_STACKADJ_RTX, cfun->eh->ehr_stackadj);
2905 #endif
2906
2907 #ifdef HAVE_eh_return
2908   if (HAVE_eh_return)
2909     emit_insn (gen_eh_return (cfun->eh->ehr_handler));
2910   else
2911 #endif
2912     {
2913 #ifdef EH_RETURN_HANDLER_RTX
2914       emit_move_insn (EH_RETURN_HANDLER_RTX, cfun->eh->ehr_handler);
2915 #else
2916       error ("__builtin_eh_return not supported on this target");
2917 #endif
2918     }
2919
2920   emit_label (around_label);
2921 }
2922
2923 /* Convert a ptr_mode address ADDR_TREE to a Pmode address controlled by
2924    POINTERS_EXTEND_UNSIGNED and return it.  */
2925
2926 rtx
2927 expand_builtin_extend_pointer (tree addr_tree)
2928 {
2929   rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, 0);
2930   int extend;
2931
2932 #ifdef POINTERS_EXTEND_UNSIGNED
2933   extend = POINTERS_EXTEND_UNSIGNED;
2934 #else
2935   /* The previous EH code did an unsigned extend by default, so we do this also
2936      for consistency.  */
2937   extend = 1;
2938 #endif
2939
2940   return convert_modes (word_mode, ptr_mode, addr, extend);
2941 }
2942 \f
2943 /* In the following functions, we represent entries in the action table
2944    as 1-based indices.  Special cases are:
2945
2946          0:     null action record, non-null landing pad; implies cleanups
2947         -1:     null action record, null landing pad; implies no action
2948         -2:     no call-site entry; implies must_not_throw
2949         -3:     we have yet to process outer regions
2950
2951    Further, no special cases apply to the "next" field of the record.
2952    For next, 0 means end of list.  */
2953
2954 struct action_record
2955 {
2956   int offset;
2957   int filter;
2958   int next;
2959 };
2960
2961 static int
2962 action_record_eq (const void *pentry, const void *pdata)
2963 {
2964   const struct action_record *entry = (const struct action_record *) pentry;
2965   const struct action_record *data = (const struct action_record *) pdata;
2966   return entry->filter == data->filter && entry->next == data->next;
2967 }
2968
2969 static hashval_t
2970 action_record_hash (const void *pentry)
2971 {
2972   const struct action_record *entry = (const struct action_record *) pentry;
2973   return entry->next * 1009 + entry->filter;
2974 }
2975
2976 static int
2977 add_action_record (htab_t ar_hash, int filter, int next)
2978 {
2979   struct action_record **slot, *new, tmp;
2980
2981   tmp.filter = filter;
2982   tmp.next = next;
2983   slot = (struct action_record **) htab_find_slot (ar_hash, &tmp, INSERT);
2984
2985   if ((new = *slot) == NULL)
2986     {
2987       new = xmalloc (sizeof (*new));
2988       new->offset = VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data) + 1;
2989       new->filter = filter;
2990       new->next = next;
2991       *slot = new;
2992
2993       /* The filter value goes in untouched.  The link to the next
2994          record is a "self-relative" byte offset, or zero to indicate
2995          that there is no next record.  So convert the absolute 1 based
2996          indices we've been carrying around into a displacement.  */
2997
2998       push_sleb128 (&cfun->eh->action_record_data, filter);
2999       if (next)
3000         next -= VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data) + 1;
3001       push_sleb128 (&cfun->eh->action_record_data, next);
3002     }
3003
3004   return new->offset;
3005 }
3006
3007 static int
3008 collect_one_action_chain (htab_t ar_hash, struct eh_region *region)
3009 {
3010   struct eh_region *c;
3011   int next;
3012
3013   /* If we've reached the top of the region chain, then we have
3014      no actions, and require no landing pad.  */
3015   if (region == NULL)
3016     return -1;
3017
3018   switch (region->type)
3019     {
3020     case ERT_CLEANUP:
3021       /* A cleanup adds a zero filter to the beginning of the chain, but
3022          there are special cases to look out for.  If there are *only*
3023          cleanups along a path, then it compresses to a zero action.
3024          Further, if there are multiple cleanups along a path, we only
3025          need to represent one of them, as that is enough to trigger
3026          entry to the landing pad at runtime.  */
3027       next = collect_one_action_chain (ar_hash, region->outer);
3028       if (next <= 0)
3029         return 0;
3030       for (c = region->outer; c ; c = c->outer)
3031         if (c->type == ERT_CLEANUP)
3032           return next;
3033       return add_action_record (ar_hash, 0, next);
3034
3035     case ERT_TRY:
3036       /* Process the associated catch regions in reverse order.
3037          If there's a catch-all handler, then we don't need to
3038          search outer regions.  Use a magic -3 value to record
3039          that we haven't done the outer search.  */
3040       next = -3;
3041       for (c = region->u.try.last_catch; c ; c = c->u.catch.prev_catch)
3042         {
3043           if (c->u.catch.type_list == NULL)
3044             {
3045               /* Retrieve the filter from the head of the filter list
3046                  where we have stored it (see assign_filter_values).  */
3047               int filter
3048                 = TREE_INT_CST_LOW (TREE_VALUE (c->u.catch.filter_list));
3049
3050               next = add_action_record (ar_hash, filter, 0);
3051             }
3052           else
3053             {
3054               /* Once the outer search is done, trigger an action record for
3055                  each filter we have.  */
3056               tree flt_node;
3057
3058               if (next == -3)
3059                 {
3060                   next = collect_one_action_chain (ar_hash, region->outer);
3061
3062                   /* If there is no next action, terminate the chain.  */
3063                   if (next == -1)
3064                     next = 0;
3065                   /* If all outer actions are cleanups or must_not_throw,
3066                      we'll have no action record for it, since we had wanted
3067                      to encode these states in the call-site record directly.
3068                      Add a cleanup action to the chain to catch these.  */
3069                   else if (next <= 0)
3070                     next = add_action_record (ar_hash, 0, 0);
3071                 }
3072
3073               flt_node = c->u.catch.filter_list;
3074               for (; flt_node; flt_node = TREE_CHAIN (flt_node))
3075                 {
3076                   int filter = TREE_INT_CST_LOW (TREE_VALUE (flt_node));
3077                   next = add_action_record (ar_hash, filter, next);
3078                 }
3079             }
3080         }
3081       return next;
3082
3083     case ERT_ALLOWED_EXCEPTIONS:
3084       /* An exception specification adds its filter to the
3085          beginning of the chain.  */
3086       next = collect_one_action_chain (ar_hash, region->outer);
3087
3088       /* If there is no next action, terminate the chain.  */
3089       if (next == -1)
3090         next = 0;
3091       /* If all outer actions are cleanups or must_not_throw,
3092          we'll have no action record for it, since we had wanted
3093          to encode these states in the call-site record directly.
3094          Add a cleanup action to the chain to catch these.  */
3095       else if (next <= 0)
3096         next = add_action_record (ar_hash, 0, 0);
3097
3098       return add_action_record (ar_hash, region->u.allowed.filter, next);
3099
3100     case ERT_MUST_NOT_THROW:
3101       /* A must-not-throw region with no inner handlers or cleanups
3102          requires no call-site entry.  Note that this differs from
3103          the no handler or cleanup case in that we do require an lsda
3104          to be generated.  Return a magic -2 value to record this.  */
3105       return -2;
3106
3107     case ERT_CATCH:
3108     case ERT_THROW:
3109       /* CATCH regions are handled in TRY above.  THROW regions are
3110          for optimization information only and produce no output.  */
3111       return collect_one_action_chain (ar_hash, region->outer);
3112
3113     default:
3114       gcc_unreachable ();
3115     }
3116 }
3117
3118 static int
3119 add_call_site (rtx landing_pad, int action)
3120 {
3121   struct call_site_record *data = cfun->eh->call_site_data;
3122   int used = cfun->eh->call_site_data_used;
3123   int size = cfun->eh->call_site_data_size;
3124
3125   if (used >= size)
3126     {
3127       size = (size ? size * 2 : 64);
3128       data = ggc_realloc (data, sizeof (*data) * size);
3129       cfun->eh->call_site_data = data;
3130       cfun->eh->call_site_data_size = size;
3131     }
3132
3133   data[used].landing_pad = landing_pad;
3134   data[used].action = action;
3135
3136   cfun->eh->call_site_data_used = used + 1;
3137
3138   return used + call_site_base;
3139 }
3140
3141 /* Turn REG_EH_REGION notes back into NOTE_INSN_EH_REGION notes.
3142    The new note numbers will not refer to region numbers, but
3143    instead to call site entries.  */
3144
3145 unsigned int
3146 convert_to_eh_region_ranges (void)
3147 {
3148   rtx insn, iter, note;
3149   htab_t ar_hash;
3150   int last_action = -3;
3151   rtx last_action_insn = NULL_RTX;
3152   rtx last_landing_pad = NULL_RTX;
3153   rtx first_no_action_insn = NULL_RTX;
3154   int call_site = 0;
3155
3156   if (USING_SJLJ_EXCEPTIONS || cfun->eh->region_tree == NULL)
3157     return 0;
3158
3159   VARRAY_UCHAR_INIT (cfun->eh->action_record_data, 64, "action_record_data");
3160
3161   ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
3162
3163   for (iter = get_insns (); iter ; iter = NEXT_INSN (iter))
3164     if (INSN_P (iter))
3165       {
3166         struct eh_region *region;
3167         int this_action;
3168         rtx this_landing_pad;
3169
3170         insn = iter;
3171         if (NONJUMP_INSN_P (insn)
3172             && GET_CODE (PATTERN (insn)) == SEQUENCE)
3173           insn = XVECEXP (PATTERN (insn), 0, 0);
3174
3175         note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3176         if (!note)
3177           {
3178             if (! (CALL_P (insn)
3179                    || (flag_non_call_exceptions
3180                        && may_trap_p (PATTERN (insn)))))
3181               continue;
3182             this_action = -1;
3183             region = NULL;
3184           }
3185         else
3186           {
3187             if (INTVAL (XEXP (note, 0)) <= 0)
3188               continue;
3189             region = VEC_index (eh_region, cfun->eh->region_array, INTVAL (XEXP (note, 0)));
3190             this_action = collect_one_action_chain (ar_hash, region);
3191           }
3192
3193         /* Existence of catch handlers, or must-not-throw regions
3194            implies that an lsda is needed (even if empty).  */
3195         if (this_action != -1)
3196           cfun->uses_eh_lsda = 1;
3197
3198         /* Delay creation of region notes for no-action regions
3199            until we're sure that an lsda will be required.  */
3200         else if (last_action == -3)
3201           {
3202             first_no_action_insn = iter;
3203             last_action = -1;
3204           }
3205
3206         /* Cleanups and handlers may share action chains but not
3207            landing pads.  Collect the landing pad for this region.  */
3208         if (this_action >= 0)
3209           {
3210             struct eh_region *o;
3211             for (o = region; ! o->landing_pad ; o = o->outer)
3212               continue;
3213             this_landing_pad = o->landing_pad;
3214           }
3215         else
3216           this_landing_pad = NULL_RTX;
3217
3218         /* Differing actions or landing pads implies a change in call-site
3219            info, which implies some EH_REGION note should be emitted.  */
3220         if (last_action != this_action
3221             || last_landing_pad != this_landing_pad)
3222           {
3223             /* If we'd not seen a previous action (-3) or the previous
3224                action was must-not-throw (-2), then we do not need an
3225                end note.  */
3226             if (last_action >= -1)
3227               {
3228                 /* If we delayed the creation of the begin, do it now.  */
3229                 if (first_no_action_insn)
3230                   {
3231                     call_site = add_call_site (NULL_RTX, 0);
3232                     note = emit_note_before (NOTE_INSN_EH_REGION_BEG,
3233                                              first_no_action_insn);
3234                     NOTE_EH_HANDLER (note) = call_site;
3235                     first_no_action_insn = NULL_RTX;
3236                   }
3237
3238                 note = emit_note_after (NOTE_INSN_EH_REGION_END,
3239                                         last_action_insn);
3240                 NOTE_EH_HANDLER (note) = call_site;
3241               }
3242
3243             /* If the new action is must-not-throw, then no region notes
3244                are created.  */
3245             if (this_action >= -1)
3246               {
3247                 call_site = add_call_site (this_landing_pad,
3248                                            this_action < 0 ? 0 : this_action);
3249                 note = emit_note_before (NOTE_INSN_EH_REGION_BEG, iter);
3250                 NOTE_EH_HANDLER (note) = call_site;
3251               }
3252
3253             last_action = this_action;
3254             last_landing_pad = this_landing_pad;
3255           }
3256         last_action_insn = iter;
3257       }
3258
3259   if (last_action >= -1 && ! first_no_action_insn)
3260     {
3261       note = emit_note_after (NOTE_INSN_EH_REGION_END, last_action_insn);
3262       NOTE_EH_HANDLER (note) = call_site;
3263     }
3264
3265   htab_delete (ar_hash);
3266   return 0;
3267 }
3268
3269 struct tree_opt_pass pass_convert_to_eh_region_ranges =
3270 {
3271   "eh-ranges",                          /* name */
3272   NULL,                                 /* gate */
3273   convert_to_eh_region_ranges,          /* execute */
3274   NULL,                                 /* sub */
3275   NULL,                                 /* next */
3276   0,                                    /* static_pass_number */
3277   0,                                    /* tv_id */
3278   0,                                    /* properties_required */
3279   0,                                    /* properties_provided */
3280   0,                                    /* properties_destroyed */
3281   0,                                    /* todo_flags_start */
3282   TODO_dump_func,                       /* todo_flags_finish */
3283   0                                     /* letter */
3284 };
3285
3286 \f
3287 static void
3288 push_uleb128 (varray_type *data_area, unsigned int value)
3289 {
3290   do
3291     {
3292       unsigned char byte = value & 0x7f;
3293       value >>= 7;
3294       if (value)
3295         byte |= 0x80;
3296       VARRAY_PUSH_UCHAR (*data_area, byte);
3297     }
3298   while (value);
3299 }
3300
3301 static void
3302 push_sleb128 (varray_type *data_area, int value)
3303 {
3304   unsigned char byte;
3305   int more;
3306
3307   do
3308     {
3309       byte = value & 0x7f;
3310       value >>= 7;
3311       more = ! ((value == 0 && (byte & 0x40) == 0)
3312                 || (value == -1 && (byte & 0x40) != 0));
3313       if (more)
3314         byte |= 0x80;
3315       VARRAY_PUSH_UCHAR (*data_area, byte);
3316     }
3317   while (more);
3318 }
3319
3320 \f
3321 #ifndef HAVE_AS_LEB128
3322 static int
3323 dw2_size_of_call_site_table (void)
3324 {
3325   int n = cfun->eh->call_site_data_used;
3326   int size = n * (4 + 4 + 4);
3327   int i;
3328
3329   for (i = 0; i < n; ++i)
3330     {
3331       struct call_site_record *cs = &cfun->eh->call_site_data[i];
3332       size += size_of_uleb128 (cs->action);
3333     }
3334
3335   return size;
3336 }
3337
3338 static int
3339 sjlj_size_of_call_site_table (void)
3340 {
3341   int n = cfun->eh->call_site_data_used;
3342   int size = 0;
3343   int i;
3344
3345   for (i = 0; i < n; ++i)
3346     {
3347       struct call_site_record *cs = &cfun->eh->call_site_data[i];
3348       size += size_of_uleb128 (INTVAL (cs->landing_pad));
3349       size += size_of_uleb128 (cs->action);
3350     }
3351
3352   return size;
3353 }
3354 #endif
3355
3356 static void
3357 dw2_output_call_site_table (void)
3358 {
3359   int n = cfun->eh->call_site_data_used;
3360   int i;
3361
3362   for (i = 0; i < n; ++i)
3363     {
3364       struct call_site_record *cs = &cfun->eh->call_site_data[i];
3365       char reg_start_lab[32];
3366       char reg_end_lab[32];
3367       char landing_pad_lab[32];
3368
3369       ASM_GENERATE_INTERNAL_LABEL (reg_start_lab, "LEHB", call_site_base + i);
3370       ASM_GENERATE_INTERNAL_LABEL (reg_end_lab, "LEHE", call_site_base + i);
3371
3372       if (cs->landing_pad)
3373         ASM_GENERATE_INTERNAL_LABEL (landing_pad_lab, "L",
3374                                      CODE_LABEL_NUMBER (cs->landing_pad));
3375
3376       /* ??? Perhaps use insn length scaling if the assembler supports
3377          generic arithmetic.  */
3378       /* ??? Perhaps use attr_length to choose data1 or data2 instead of
3379          data4 if the function is small enough.  */
3380 #ifdef HAVE_AS_LEB128
3381       dw2_asm_output_delta_uleb128 (reg_start_lab,
3382                                     current_function_func_begin_label,
3383                                     "region %d start", i);
3384       dw2_asm_output_delta_uleb128 (reg_end_lab, reg_start_lab,
3385                                     "length");
3386       if (cs->landing_pad)
3387         dw2_asm_output_delta_uleb128 (landing_pad_lab,
3388                                       current_function_func_begin_label,
3389                                       "landing pad");
3390       else
3391         dw2_asm_output_data_uleb128 (0, "landing pad");
3392 #else
3393       dw2_asm_output_delta (4, reg_start_lab,
3394                             current_function_func_begin_label,
3395                             "region %d start", i);
3396       dw2_asm_output_delta (4, reg_end_lab, reg_start_lab, "length");
3397       if (cs->landing_pad)
3398         dw2_asm_output_delta (4, landing_pad_lab,
3399                               current_function_func_begin_label,
3400                               "landing pad");
3401       else
3402         dw2_asm_output_data (4, 0, "landing pad");
3403 #endif
3404       dw2_asm_output_data_uleb128 (cs->action, "action");
3405     }
3406
3407   call_site_base += n;
3408 }
3409
3410 static void
3411 sjlj_output_call_site_table (void)
3412 {
3413   int n = cfun->eh->call_site_data_used;
3414   int i;
3415
3416   for (i = 0; i < n; ++i)
3417     {
3418       struct call_site_record *cs = &cfun->eh->call_site_data[i];
3419
3420       dw2_asm_output_data_uleb128 (INTVAL (cs->landing_pad),
3421                                    "region %d landing pad", i);
3422       dw2_asm_output_data_uleb128 (cs->action, "action");
3423     }
3424
3425   call_site_base += n;
3426 }
3427
3428 #ifndef TARGET_UNWIND_INFO
3429 /* Switch to the section that should be used for exception tables.  */
3430
3431 static void
3432 switch_to_exception_section (void)
3433 {
3434   if (exception_section == 0)
3435     {
3436       if (targetm.have_named_sections)
3437         {
3438           int flags;
3439
3440           if (EH_TABLES_CAN_BE_READ_ONLY)
3441             {
3442               int tt_format =
3443                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3444               flags = ((! flag_pic
3445                         || ((tt_format & 0x70) != DW_EH_PE_absptr
3446                             && (tt_format & 0x70) != DW_EH_PE_aligned))
3447                        ? 0 : SECTION_WRITE);
3448             }
3449           else
3450             flags = SECTION_WRITE;
3451           exception_section = get_section (".gcc_except_table", flags, NULL);
3452         }
3453       else
3454         exception_section = flag_pic ? data_section : readonly_data_section;
3455     }
3456   switch_to_section (exception_section);
3457 }
3458 #endif
3459
3460
3461 /* Output a reference from an exception table to the type_info object TYPE.
3462    TT_FORMAT and TT_FORMAT_SIZE descibe the DWARF encoding method used for
3463    the value.  */
3464
3465 static void
3466 output_ttype (tree type, int tt_format, int tt_format_size)
3467 {
3468   rtx value;
3469   bool public = true;
3470
3471   if (type == NULL_TREE)
3472     value = const0_rtx;
3473   else
3474     {
3475       struct cgraph_varpool_node *node;
3476
3477       type = lookup_type_for_runtime (type);
3478       value = expand_normal (type);
3479
3480       /* Let cgraph know that the rtti decl is used.  Not all of the
3481          paths below go through assemble_integer, which would take
3482          care of this for us.  */
3483       STRIP_NOPS (type);
3484       if (TREE_CODE (type) == ADDR_EXPR)
3485         {
3486           type = TREE_OPERAND (type, 0);
3487           if (TREE_CODE (type) == VAR_DECL)
3488             {
3489               node = cgraph_varpool_node (type);
3490               if (node)
3491                 cgraph_varpool_mark_needed_node (node);
3492               public = TREE_PUBLIC (type);
3493             }
3494         }
3495       else
3496         gcc_assert (TREE_CODE (type) == INTEGER_CST);
3497     }
3498
3499   /* Allow the target to override the type table entry format.  */
3500   if (targetm.asm_out.ttype (value))
3501     return;
3502
3503   if (tt_format == DW_EH_PE_absptr || tt_format == DW_EH_PE_aligned)
3504     assemble_integer (value, tt_format_size,
3505                       tt_format_size * BITS_PER_UNIT, 1);
3506   else
3507     dw2_asm_output_encoded_addr_rtx (tt_format, value, public, NULL);
3508 }
3509
3510 void
3511 output_function_exception_table (void)
3512 {
3513   int tt_format, cs_format, lp_format, i, n;
3514 #ifdef HAVE_AS_LEB128
3515   char ttype_label[32];
3516   char cs_after_size_label[32];
3517   char cs_end_label[32];
3518 #else
3519   int call_site_len;
3520 #endif
3521   int have_tt_data;
3522   int tt_format_size = 0;
3523
3524   if (eh_personality_libfunc)
3525     assemble_external_libcall (eh_personality_libfunc);
3526
3527   /* Not all functions need anything.  */
3528   if (! cfun->uses_eh_lsda)
3529     return;
3530
3531 #ifdef TARGET_UNWIND_INFO
3532   /* TODO: Move this into target file.  */
3533   fputs ("\t.personality\t", asm_out_file);
3534   output_addr_const (asm_out_file, eh_personality_libfunc);
3535   fputs ("\n\t.handlerdata\n", asm_out_file);
3536   /* Note that varasm still thinks we're in the function's code section.
3537      The ".endp" directive that will immediately follow will take us back.  */
3538 #else
3539   switch_to_exception_section ();
3540 #endif
3541
3542   /* If the target wants a label to begin the table, emit it here.  */
3543   targetm.asm_out.except_table_label (asm_out_file);
3544
3545   have_tt_data = (VEC_length (tree, cfun->eh->ttype_data) > 0
3546                   || VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data) > 0);
3547
3548   /* Indicate the format of the @TType entries.  */
3549   if (! have_tt_data)
3550     tt_format = DW_EH_PE_omit;
3551   else
3552     {
3553       tt_format = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3554 #ifdef HAVE_AS_LEB128
3555       ASM_GENERATE_INTERNAL_LABEL (ttype_label, "LLSDATT",
3556                                    current_function_funcdef_no);
3557 #endif
3558       tt_format_size = size_of_encoded_value (tt_format);
3559
3560       assemble_align (tt_format_size * BITS_PER_UNIT);
3561     }
3562
3563   targetm.asm_out.internal_label (asm_out_file, "LLSDA",
3564                              current_function_funcdef_no);
3565
3566   /* The LSDA header.  */
3567
3568   /* Indicate the format of the landing pad start pointer.  An omitted
3569      field implies @LPStart == @Start.  */
3570   /* Currently we always put @LPStart == @Start.  This field would
3571      be most useful in moving the landing pads completely out of
3572      line to another section, but it could also be used to minimize
3573      the size of uleb128 landing pad offsets.  */
3574   lp_format = DW_EH_PE_omit;
3575   dw2_asm_output_data (1, lp_format, "@LPStart format (%s)",
3576                        eh_data_format_name (lp_format));
3577
3578   /* @LPStart pointer would go here.  */
3579
3580   dw2_asm_output_data (1, tt_format, "@TType format (%s)",
3581                        eh_data_format_name (tt_format));
3582
3583 #ifndef HAVE_AS_LEB128
3584   if (USING_SJLJ_EXCEPTIONS)
3585     call_site_len = sjlj_size_of_call_site_table ();
3586   else
3587     call_site_len = dw2_size_of_call_site_table ();
3588 #endif
3589
3590   /* A pc-relative 4-byte displacement to the @TType data.  */
3591   if (have_tt_data)
3592     {
3593 #ifdef HAVE_AS_LEB128
3594       char ttype_after_disp_label[32];
3595       ASM_GENERATE_INTERNAL_LABEL (ttype_after_disp_label, "LLSDATTD",
3596                                    current_function_funcdef_no);
3597       dw2_asm_output_delta_uleb128 (ttype_label, ttype_after_disp_label,
3598                                     "@TType base offset");
3599       ASM_OUTPUT_LABEL (asm_out_file, ttype_after_disp_label);
3600 #else
3601       /* Ug.  Alignment queers things.  */
3602       unsigned int before_disp, after_disp, last_disp, disp;
3603
3604       before_disp = 1 + 1;
3605       after_disp = (1 + size_of_uleb128 (call_site_len)
3606                     + call_site_len
3607                     + VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data)
3608                     + (VEC_length (tree, cfun->eh->ttype_data)
3609                        * tt_format_size));
3610
3611       disp = after_disp;
3612       do
3613         {
3614           unsigned int disp_size, pad;
3615
3616           last_disp = disp;
3617           disp_size = size_of_uleb128 (disp);
3618           pad = before_disp + disp_size + after_disp;
3619           if (pad % tt_format_size)
3620             pad = tt_format_size - (pad % tt_format_size);
3621           else
3622             pad = 0;
3623           disp = after_disp + pad;
3624         }
3625       while (disp != last_disp);
3626
3627       dw2_asm_output_data_uleb128 (disp, "@TType base offset");
3628 #endif
3629     }
3630
3631   /* Indicate the format of the call-site offsets.  */
3632 #ifdef HAVE_AS_LEB128
3633   cs_format = DW_EH_PE_uleb128;
3634 #else
3635   cs_format = DW_EH_PE_udata4;
3636 #endif
3637   dw2_asm_output_data (1, cs_format, "call-site format (%s)",
3638                        eh_data_format_name (cs_format));
3639
3640 #ifdef HAVE_AS_LEB128
3641   ASM_GENERATE_INTERNAL_LABEL (cs_after_size_label, "LLSDACSB",
3642                                current_function_funcdef_no);
3643   ASM_GENERATE_INTERNAL_LABEL (cs_end_label, "LLSDACSE",
3644                                current_function_funcdef_no);
3645   dw2_asm_output_delta_uleb128 (cs_end_label, cs_after_size_label,
3646                                 "Call-site table length");
3647   ASM_OUTPUT_LABEL (asm_out_file, cs_after_size_label);
3648   if (USING_SJLJ_EXCEPTIONS)
3649     sjlj_output_call_site_table ();
3650   else
3651     dw2_output_call_site_table ();
3652   ASM_OUTPUT_LABEL (asm_out_file, cs_end_label);
3653 #else
3654   dw2_asm_output_data_uleb128 (call_site_len,"Call-site table length");
3655   if (USING_SJLJ_EXCEPTIONS)
3656     sjlj_output_call_site_table ();
3657   else
3658     dw2_output_call_site_table ();
3659 #endif
3660
3661   /* ??? Decode and interpret the data for flag_debug_asm.  */
3662   n = VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data);
3663   for (i = 0; i < n; ++i)
3664     dw2_asm_output_data (1, VARRAY_UCHAR (cfun->eh->action_record_data, i),
3665                          (i ? NULL : "Action record table"));
3666
3667   if (have_tt_data)
3668     assemble_align (tt_format_size * BITS_PER_UNIT);
3669
3670   i = VEC_length (tree, cfun->eh->ttype_data);
3671   while (i-- > 0)
3672     {
3673       tree type = VEC_index (tree, cfun->eh->ttype_data, i);
3674       output_ttype (type, tt_format, tt_format_size);
3675     }
3676
3677 #ifdef HAVE_AS_LEB128
3678   if (have_tt_data)
3679       ASM_OUTPUT_LABEL (asm_out_file, ttype_label);
3680 #endif
3681
3682   /* ??? Decode and interpret the data for flag_debug_asm.  */
3683   n = VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data);
3684   for (i = 0; i < n; ++i)
3685     {
3686       if (targetm.arm_eabi_unwinder)
3687         {
3688           tree type = VARRAY_TREE (cfun->eh->ehspec_data, i);
3689           output_ttype (type, tt_format, tt_format_size);
3690         }
3691       else
3692         dw2_asm_output_data (1, VARRAY_UCHAR (cfun->eh->ehspec_data, i),
3693                              (i ? NULL : "Exception specification table"));
3694     }
3695
3696   switch_to_section (current_function_section ());
3697 }
3698
3699 void
3700 set_eh_throw_stmt_table (struct function *fun, struct htab *table)
3701 {
3702   fun->eh->throw_stmt_table = table;
3703 }
3704
3705 htab_t
3706 get_eh_throw_stmt_table (struct function *fun)
3707 {
3708   return fun->eh->throw_stmt_table;
3709 }
3710
3711 /* Dump EH information to OUT.  */
3712 void
3713 dump_eh_tree (FILE *out, struct function *fun)
3714 {
3715   struct eh_region *i;
3716   int depth = 0;
3717   static const char * const type_name[] = {"unknown", "cleanup", "try", "catch",
3718                                            "allowed_exceptions", "must_not_throw",
3719                                            "throw"};
3720
3721   i = fun->eh->region_tree;
3722   if (! i)
3723     return;
3724
3725   fprintf (out, "Eh tree:\n");
3726   while (1)
3727     {
3728       fprintf (out, "  %*s %i %s", depth * 2, "",
3729                i->region_number, type_name [(int)i->type]);
3730       if (i->tree_label)
3731         {
3732           fprintf (out, " tree_label:");
3733           print_generic_expr (out, i->tree_label, 0);
3734         }
3735       fprintf (out, "\n");
3736       /* If there are sub-regions, process them.  */
3737       if (i->inner)
3738         i = i->inner, depth++;
3739       /* If there are peers, process them.  */
3740       else if (i->next_peer)
3741         i = i->next_peer;
3742       /* Otherwise, step back up the tree to the next peer.  */
3743       else
3744         {
3745           do {
3746             i = i->outer;
3747             depth--;
3748             if (i == NULL)
3749               return;
3750           } while (i->next_peer == NULL);
3751           i = i->next_peer;
3752         }
3753     }
3754 }
3755
3756 /* Verify some basic invariants on EH datastructures.  Could be extended to
3757    catch more.  */
3758 void
3759 verify_eh_tree (struct function *fun)
3760 {
3761   struct eh_region *i, *outer = NULL;
3762   bool err = false;
3763   int nvisited = 0;
3764   int count = 0;
3765   int j;
3766   int depth = 0;
3767
3768   i = fun->eh->region_tree;
3769   if (! i)
3770     return;
3771   for (j = fun->eh->last_region_number; j > 0; --j)
3772     if ((i = VEC_index (eh_region, cfun->eh->region_array, j)))
3773       {
3774         count++;
3775         if (i->region_number != j)
3776           {
3777             error ("region_array is corrupted for region %i", i->region_number);
3778             err = true;
3779           }
3780       }
3781
3782   while (1)
3783     {
3784       if (VEC_index (eh_region, cfun->eh->region_array, i->region_number) != i)
3785         {
3786           error ("region_array is corrupted for region %i", i->region_number);
3787           err = true;
3788         }
3789       if (i->outer != outer)
3790         {
3791           error ("outer block of region %i is wrong", i->region_number);
3792           err = true;
3793         }
3794       if (i->may_contain_throw && outer && !outer->may_contain_throw)
3795         {
3796           error ("region %i may contain throw and is contained in region that may not",
3797                  i->region_number);
3798           err = true;
3799         }
3800       if (depth < 0)
3801         {
3802           error ("negative nesting depth of region %i", i->region_number);
3803           err = true;
3804         }
3805       nvisited ++;
3806       /* If there are sub-regions, process them.  */
3807       if (i->inner)
3808         outer = i, i = i->inner, depth++;
3809       /* If there are peers, process them.  */
3810       else if (i->next_peer)
3811         i = i->next_peer;
3812       /* Otherwise, step back up the tree to the next peer.  */
3813       else
3814         {
3815           do {
3816             i = i->outer;
3817             depth--;
3818             if (i == NULL)
3819               {
3820                 if (depth != -1)
3821                   {
3822                     error ("tree list ends on depth %i", depth + 1);
3823                     err = true;
3824                   }
3825                 if (count != nvisited)
3826                   {
3827                     error ("array does not match the region tree");
3828                     err = true;
3829                   }
3830                 if (err)
3831                   {
3832                     dump_eh_tree (stderr, fun);
3833                     internal_error ("verify_eh_tree failed");
3834                   }
3835                 return;
3836               }
3837             outer = i->outer;
3838           } while (i->next_peer == NULL);
3839           i = i->next_peer;
3840         }
3841     }
3842 }
3843
3844 /* Initialize unwind_resume_libfunc.  */
3845
3846 void
3847 default_init_unwind_resume_libfunc (void)
3848 {
3849   /* The default c++ routines aren't actually c++ specific, so use those.  */
3850   unwind_resume_libfunc =
3851     init_one_libfunc ( USING_SJLJ_EXCEPTIONS ? "_Unwind_SjLj_Resume"
3852                                              : "_Unwind_Resume");
3853 }
3854
3855 \f
3856 static bool
3857 gate_handle_eh (void)
3858 {
3859   return doing_eh (0);
3860 }
3861
3862 /* Complete generation of exception handling code.  */
3863 static unsigned int
3864 rest_of_handle_eh (void)
3865 {
3866   cleanup_cfg (CLEANUP_NO_INSN_DEL);
3867   finish_eh_generation ();
3868   cleanup_cfg (CLEANUP_NO_INSN_DEL);
3869   return 0;
3870 }
3871
3872 struct tree_opt_pass pass_rtl_eh =
3873 {
3874   "eh",                                 /* name */
3875   gate_handle_eh,                       /* gate */
3876   rest_of_handle_eh,                    /* execute */
3877   NULL,                                 /* sub */
3878   NULL,                                 /* next */
3879   0,                                    /* static_pass_number */
3880   TV_JUMP,                              /* tv_id */
3881   0,                                    /* properties_required */
3882   0,                                    /* properties_provided */
3883   0,                                    /* properties_destroyed */
3884   0,                                    /* todo_flags_start */
3885   TODO_dump_func,                       /* todo_flags_finish */
3886   'h'                                   /* letter */
3887 };
3888
3889 #include "gt-except.h"