* config/m68k/m68k.md (bungt_rev): New pattern.
[official-gcc.git] / gcc / except.c
blob2e65340dceafe535f4d937d082d8dea204979979
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>.
6 This file is part of GCC.
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.
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.
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. */
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.
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.
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.
47 [ Add updated documentation on how to use this. ] */
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"
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
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);
92 /* Return true if type A catches type B. */
93 int (*lang_eh_type_covers) (tree a, tree b);
95 /* Map a type to a runtime object to match type. */
96 tree (*lang_eh_runtime_type) (tree);
98 /* A hash table of label to region number. */
100 struct ehl_map_entry GTY(())
102 rtx label;
103 struct eh_region *region;
106 static GTY(()) int call_site_base;
107 static GTY ((param_is (union tree_node)))
108 htab_t type_to_runtime_map;
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;
118 /* Describes one exception region. */
119 struct eh_region GTY(())
121 /* The immediately surrounding region. */
122 struct eh_region *outer;
124 /* The list of immediately contained regions. */
125 struct eh_region *inner;
126 struct eh_region *next_peer;
128 /* An identifier for this region. */
129 int region_number;
131 /* When a region is deleted, its parents inherit the REG_EH_REGION
132 numbers already assigned. */
133 bitmap aka;
135 /* Each region does exactly one thing. */
136 enum eh_region_type
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;
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;
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;
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;
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;
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;
184 /* Entry point for this region's handler before landing pads are built. */
185 rtx label;
186 tree tree_label;
188 /* Entry point for this region's handler from the runtime eh library. */
189 rtx landing_pad;
191 /* Entry point for this region's handler from an inner region. */
192 rtx post_landing_pad;
194 /* The RESX insn for handing off control to the next outermost handler,
195 if appropriate. */
196 rtx resume;
198 /* True if something in this region may throw. */
199 unsigned may_contain_throw : 1;
202 typedef struct eh_region *eh_region;
204 struct call_site_record GTY(())
206 rtx landing_pad;
207 int action;
210 DEF_VEC_P(eh_region);
211 DEF_VEC_ALLOC_P(eh_region, gc);
213 /* Used to save exception status for each function. */
214 struct eh_status GTY(())
216 /* The tree of all regions for this function. */
217 struct eh_region *region_tree;
219 /* The same information as an indexable array. */
220 VEC(eh_region,gc) *region_array;
222 /* The most recently open region. */
223 struct eh_region *cur_region;
225 /* This is the region for which we are processing catch blocks. */
226 struct eh_region *try_region;
228 rtx filter;
229 rtx exc_ptr;
231 int built_landing_pads;
232 int last_region_number;
234 VEC(tree,gc) *ttype_data;
235 varray_type ehspec_data;
236 varray_type action_record_data;
238 htab_t GTY ((param_is (struct ehl_map_entry))) exception_handler_label_map;
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;
245 rtx ehr_stackadj;
246 rtx ehr_handler;
247 rtx ehr_label;
249 rtx sjlj_fc;
250 rtx sjlj_exit_after;
252 htab_t GTY((param_is (struct throw_stmt_node))) throw_stmt_table;
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);
260 static void remove_unreachable_regions (rtx);
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);
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);
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 *);
289 /* The return value of reachable_next_level. */
290 enum reachable_code
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
302 struct reachable_info;
303 static enum reachable_code reachable_next_level (struct eh_region *, tree,
304 struct reachable_info *);
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);
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);
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.
326 This is used to ensure that -fexceptions has been specified if the
327 compiler tries to use any exception-specific functions. */
330 doing_eh (int do_warn)
332 if (! flag_exceptions)
334 static int warned = 0;
335 if (! warned && do_warn)
337 error ("exception handling disabled, use -fexceptions to enable");
338 warned = 1;
340 return 0;
342 return 1;
346 void
347 init_eh (void)
349 if (! flag_exceptions)
350 return;
352 type_to_runtime_map = htab_create_ggc (31, t2r_hash, t2r_eq, NULL);
354 /* Create the SjLj_Function_Context structure. This should match
355 the definition in unwind-sjlj.c. */
356 if (USING_SJLJ_EXCEPTIONS)
358 tree f_jbuf, f_per, f_lsda, f_prev, f_cs, f_data, tmp;
360 sjlj_fc_type_node = lang_hooks.types.make_type (RECORD_TYPE);
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;
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;
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;
376 f_per = build_decl (FIELD_DECL, get_identifier ("__personality"),
377 ptr_type_node);
378 DECL_FIELD_CONTEXT (f_per) = sjlj_fc_type_node;
380 f_lsda = build_decl (FIELD_DECL, get_identifier ("__lsda"),
381 ptr_type_node);
382 DECL_FIELD_CONTEXT (f_lsda) = sjlj_fc_type_node;
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;
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;
416 layout_type (sjlj_fc_type_node);
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);
438 void
439 init_eh_for_function (void)
441 cfun->eh = ggc_alloc_cleared (sizeof (struct eh_status));
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. */
448 static struct eh_region *
449 gen_eh_region (enum eh_region_type type, struct eh_region *outer)
451 struct eh_region *new;
453 #ifdef ENABLE_CHECKING
454 gcc_assert (doing_eh (0));
455 #endif
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)
463 new->next_peer = outer->inner;
464 outer->inner = new;
466 else
468 new->next_peer = cfun->eh->region_tree;
469 cfun->eh->region_tree = new;
472 new->region_number = ++cfun->eh->last_region_number;
474 return new;
477 struct eh_region *
478 gen_eh_region_cleanup (struct eh_region *outer, struct eh_region *prev_try)
480 struct eh_region *cleanup = gen_eh_region (ERT_CLEANUP, outer);
481 cleanup->u.cleanup.prev_try = prev_try;
482 return cleanup;
485 struct eh_region *
486 gen_eh_region_try (struct eh_region *outer)
488 return gen_eh_region (ERT_TRY, outer);
491 struct eh_region *
492 gen_eh_region_catch (struct eh_region *t, tree type_or_list)
494 struct eh_region *c, *l;
495 tree type_list, type_node;
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)
502 if (TREE_CODE (type_or_list) != TREE_LIST)
503 type_list = tree_cons (NULL_TREE, type_or_list, NULL_TREE);
505 type_node = type_list;
506 for (; type_node; type_node = TREE_CHAIN (type_node))
507 add_type_for_runtime (TREE_VALUE (type_node));
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;
520 return c;
523 struct eh_region *
524 gen_eh_region_allowed (struct eh_region *outer, tree allowed)
526 struct eh_region *region = gen_eh_region (ERT_ALLOWED_EXCEPTIONS, outer);
527 region->u.allowed.type_list = allowed;
529 for (; allowed ; allowed = TREE_CHAIN (allowed))
530 add_type_for_runtime (TREE_VALUE (allowed));
532 return region;
535 struct eh_region *
536 gen_eh_region_must_not_throw (struct eh_region *outer)
538 return gen_eh_region (ERT_MUST_NOT_THROW, outer);
542 get_eh_region_number (struct eh_region *region)
544 return region->region_number;
547 bool
548 get_eh_region_may_contain_throw (struct eh_region *region)
550 return region->may_contain_throw;
553 tree
554 get_eh_region_tree_label (struct eh_region *region)
556 return region->tree_label;
559 void
560 set_eh_region_tree_label (struct eh_region *region, tree lab)
562 region->tree_label = lab;
565 void
566 expand_resx_expr (tree exp)
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);
572 gcc_assert (!reg->resume);
573 reg->resume = emit_jump_insn (gen_rtx_RESX (VOIDmode, region_nr));
574 emit_barrier ();
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. */
580 void
581 note_eh_region_may_contain_throw (struct eh_region *region)
583 while (region && !region->may_contain_throw)
585 region->may_contain_throw = 1;
586 region = region->outer;
590 void
591 note_current_region_may_contain_throw (void)
593 note_eh_region_may_contain_throw (cfun->eh->cur_region);
597 /* Return an rtl expression for a pointer to the exception object
598 within a handler. */
601 get_exception_pointer (struct function *fun)
603 rtx exc_ptr = fun->eh->exc_ptr;
604 if (fun == cfun && ! exc_ptr)
606 exc_ptr = gen_reg_rtx (ptr_mode);
607 fun->eh->exc_ptr = exc_ptr;
609 return exc_ptr;
612 /* Return an rtl expression for the exception dispatch filter
613 within a handler. */
616 get_exception_filter (struct function *fun)
618 rtx filter = fun->eh->filter;
619 if (fun == cfun && ! filter)
621 filter = gen_reg_rtx (targetm.eh_return_filter_mode ());
622 fun->eh->filter = filter;
624 return filter;
627 /* This section is for the exception handling specific optimization pass. */
629 /* Random access the exception region tree. */
631 void
632 collect_eh_region_array (void)
634 struct eh_region *i;
636 i = cfun->eh->region_tree;
637 if (! i)
638 return;
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);
644 while (1)
646 VEC_replace (eh_region, cfun->eh->region_array, i->region_number, i);
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
657 do {
658 i = i->outer;
659 if (i == NULL)
660 return;
661 } while (i->next_peer == NULL);
662 i = i->next_peer;
667 /* Remove all regions whose labels are not reachable from insns. */
669 static void
670 remove_unreachable_regions (rtx insns)
672 int i, *uid_region_num;
673 bool *reachable;
674 struct eh_region *r;
675 rtx insn;
677 uid_region_num = xcalloc (get_max_uid (), sizeof(int));
678 reachable = xcalloc (cfun->eh->last_region_number + 1, sizeof(bool));
680 for (i = cfun->eh->last_region_number; i > 0; --i)
682 r = VEC_index (eh_region, cfun->eh->region_array, i);
683 if (!r || r->region_number != i)
684 continue;
686 if (r->resume)
688 gcc_assert (!uid_region_num[INSN_UID (r->resume)]);
689 uid_region_num[INSN_UID (r->resume)] = i;
691 if (r->label)
693 gcc_assert (!uid_region_num[INSN_UID (r->label)]);
694 uid_region_num[INSN_UID (r->label)] = i;
698 for (insn = insns; insn; insn = NEXT_INSN (insn))
699 reachable[uid_region_num[INSN_UID (insn)]] = true;
701 for (i = cfun->eh->last_region_number; i > 0; --i)
703 r = VEC_index (eh_region, cfun->eh->region_array, i);
704 if (r && r->region_number == i && !reachable[i])
706 bool kill_it = true;
707 switch (r->type)
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;
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;
723 case ERT_TRY:
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])
731 kill_it = false;
732 break;
734 break;
737 default:
738 break;
741 if (kill_it)
742 remove_eh_handler (r);
746 free (reachable);
747 free (uid_region_num);
750 /* Set up EH labels for RTL. */
752 void
753 convert_from_eh_region_ranges (void)
755 rtx insns = get_insns ();
756 int i, n = cfun->eh->last_region_number;
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)
764 struct eh_region *region;
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);
771 remove_unreachable_regions (insns);
774 static void
775 add_ehl_entry (rtx label, struct eh_region *region)
777 struct ehl_map_entry **slot, *entry;
779 LABEL_PRESERVE_P (label) = 1;
781 entry = ggc_alloc (sizeof (*entry));
782 entry->label = label;
783 entry->region = region;
785 slot = (struct ehl_map_entry **)
786 htab_find_slot (cfun->eh->exception_handler_label_map, entry, INSERT);
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);
794 *slot = entry;
797 void
798 find_exception_handler_labels (void)
800 int i;
802 if (cfun->eh->exception_handler_label_map)
803 htab_empty (cfun->eh->exception_handler_label_map);
804 else
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);
813 if (cfun->eh->region_tree == NULL)
814 return;
816 for (i = cfun->eh->last_region_number; i > 0; --i)
818 struct eh_region *region;
819 rtx lab;
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;
829 if (lab)
830 add_ehl_entry (lab, region);
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);
839 /* Returns true if the current function has exception handling regions. */
841 bool
842 current_function_has_exception_handlers (void)
844 int i;
846 for (i = cfun->eh->last_region_number; i > 0; --i)
848 struct eh_region *region;
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;
857 return false;
860 /* A subroutine of duplicate_eh_regions. Search the region tree under O
861 for the minimum and maximum region numbers. Update *MIN and *MAX. */
863 static void
864 duplicate_eh_regions_0 (eh_region o, int *min, int *max)
866 if (o->region_number < *min)
867 *min = o->region_number;
868 if (o->region_number > *max)
869 *max = o->region_number;
871 if (o->inner)
873 o = o->inner;
874 duplicate_eh_regions_0 (o, min, max);
875 while (o->next_peer)
877 o = o->next_peer;
878 duplicate_eh_regions_0 (o, min, max);
883 /* A subroutine of duplicate_eh_regions. Copy the region tree under OLD.
884 Root it at OUTER, and apply EH_OFFSET to the region number. Don't worry
885 about the other internal pointers just yet, just the tree-like pointers. */
887 static eh_region
888 duplicate_eh_regions_1 (eh_region old, eh_region outer, int eh_offset)
890 eh_region ret, n;
892 ret = n = ggc_alloc (sizeof (struct eh_region));
894 *n = *old;
895 n->outer = outer;
896 n->next_peer = NULL;
897 gcc_assert (!old->aka);
899 n->region_number += eh_offset;
900 VEC_replace (eh_region, cfun->eh->region_array, n->region_number, n);
902 if (old->inner)
904 old = old->inner;
905 n = n->inner = duplicate_eh_regions_1 (old, ret, eh_offset);
906 while (old->next_peer)
908 old = old->next_peer;
909 n = n->next_peer = duplicate_eh_regions_1 (old, ret, eh_offset);
913 return ret;
916 /* Duplicate the EH regions of IFUN, rooted at COPY_REGION, into current
917 function and root the tree below OUTER_REGION. Remap labels using MAP
918 callback. The special case of COPY_REGION of 0 means all regions. */
921 duplicate_eh_regions (struct function *ifun, duplicate_eh_regions_map map,
922 void *data, int copy_region, int outer_region)
924 eh_region cur, prev_try, outer, *splice;
925 int i, min_region, max_region, eh_offset, cfun_last_region_number;
926 int num_regions;
928 if (!ifun->eh->region_tree)
929 return 0;
931 /* Find the range of region numbers to be copied. The interface we
932 provide here mandates a single offset to find new number from old,
933 which means we must look at the numbers present, instead of the
934 count or something else. */
935 if (copy_region > 0)
937 min_region = INT_MAX;
938 max_region = 0;
940 cur = VEC_index (eh_region, ifun->eh->region_array, copy_region);
941 duplicate_eh_regions_0 (cur, &min_region, &max_region);
943 else
944 min_region = 1, max_region = ifun->eh->last_region_number;
945 num_regions = max_region - min_region + 1;
946 cfun_last_region_number = cfun->eh->last_region_number;
947 eh_offset = cfun_last_region_number + 1 - min_region;
949 /* If we've not yet created a region array, do so now. */
950 VEC_safe_grow (eh_region, gc, cfun->eh->region_array,
951 cfun_last_region_number + 1 + num_regions);
952 cfun->eh->last_region_number = max_region + eh_offset;
954 /* We may have just allocated the array for the first time.
955 Make sure that element zero is null. */
956 VEC_replace (eh_region, cfun->eh->region_array, 0, 0);
958 /* Zero all entries in the range allocated. */
959 memset (VEC_address (eh_region, cfun->eh->region_array)
960 + cfun_last_region_number + 1, 0, num_regions * sizeof (eh_region));
962 /* Locate the spot at which to insert the new tree. */
963 if (outer_region > 0)
965 outer = VEC_index (eh_region, cfun->eh->region_array, outer_region);
966 splice = &outer->inner;
968 else
970 outer = NULL;
971 splice = &cfun->eh->region_tree;
973 while (*splice)
974 splice = &(*splice)->next_peer;
976 /* Copy all the regions in the subtree. */
977 if (copy_region > 0)
979 cur = VEC_index (eh_region, ifun->eh->region_array, copy_region);
980 *splice = duplicate_eh_regions_1 (cur, outer, eh_offset);
982 else
984 eh_region n;
986 cur = ifun->eh->region_tree;
987 *splice = n = duplicate_eh_regions_1 (cur, outer, eh_offset);
988 while (cur->next_peer)
990 cur = cur->next_peer;
991 n = n->next_peer = duplicate_eh_regions_1 (cur, outer, eh_offset);
995 /* Remap all the labels in the new regions. */
996 for (i = cfun_last_region_number + 1;
997 VEC_iterate (eh_region, cfun->eh->region_array, i, cur); ++i)
998 if (cur && cur->tree_label)
999 cur->tree_label = map (cur->tree_label, data);
1001 /* Search for the containing ERT_TRY region to fix up
1002 the prev_try short-cuts for ERT_CLEANUP regions. */
1003 prev_try = NULL;
1004 if (outer_region > 0)
1005 for (prev_try = VEC_index (eh_region, cfun->eh->region_array, outer_region);
1006 prev_try && prev_try->type != ERT_TRY;
1007 prev_try = prev_try->outer)
1010 /* Remap all of the internal catch and cleanup linkages. Since we
1011 duplicate entire subtrees, all of the referenced regions will have
1012 been copied too. And since we renumbered them as a block, a simple
1013 bit of arithmetic finds us the index for the replacement region. */
1014 for (i = cfun_last_region_number + 1;
1015 VEC_iterate (eh_region, cfun->eh->region_array, i, cur); ++i)
1017 if (cur == NULL)
1018 continue;
1020 #define REMAP(REG) \
1021 (REG) = VEC_index (eh_region, cfun->eh->region_array, \
1022 (REG)->region_number + eh_offset)
1024 switch (cur->type)
1026 case ERT_TRY:
1027 if (cur->u.try.catch)
1028 REMAP (cur->u.try.catch);
1029 if (cur->u.try.last_catch)
1030 REMAP (cur->u.try.last_catch);
1031 break;
1033 case ERT_CATCH:
1034 if (cur->u.catch.next_catch)
1035 REMAP (cur->u.catch.next_catch);
1036 if (cur->u.catch.prev_catch)
1037 REMAP (cur->u.catch.prev_catch);
1038 break;
1040 case ERT_CLEANUP:
1041 if (cur->u.cleanup.prev_try)
1042 REMAP (cur->u.cleanup.prev_try);
1043 else
1044 cur->u.cleanup.prev_try = prev_try;
1045 break;
1047 default:
1048 break;
1051 #undef REMAP
1054 return eh_offset;
1057 /* Return true if REGION_A is outer to REGION_B in IFUN. */
1059 bool
1060 eh_region_outer_p (struct function *ifun, int region_a, int region_b)
1062 struct eh_region *rp_a, *rp_b;
1064 gcc_assert (ifun->eh->last_region_number > 0);
1065 gcc_assert (ifun->eh->region_tree);
1067 rp_a = VEC_index (eh_region, ifun->eh->region_array, region_a);
1068 rp_b = VEC_index (eh_region, ifun->eh->region_array, region_b);
1069 gcc_assert (rp_a != NULL);
1070 gcc_assert (rp_b != NULL);
1074 if (rp_a == rp_b)
1075 return true;
1076 rp_b = rp_b->outer;
1078 while (rp_b);
1080 return false;
1083 /* Return region number of region that is outer to both if REGION_A and
1084 REGION_B in IFUN. */
1087 eh_region_outermost (struct function *ifun, int region_a, int region_b)
1089 struct eh_region *rp_a, *rp_b;
1090 sbitmap b_outer;
1092 gcc_assert (ifun->eh->last_region_number > 0);
1093 gcc_assert (ifun->eh->region_tree);
1095 rp_a = VEC_index (eh_region, ifun->eh->region_array, region_a);
1096 rp_b = VEC_index (eh_region, ifun->eh->region_array, region_b);
1097 gcc_assert (rp_a != NULL);
1098 gcc_assert (rp_b != NULL);
1100 b_outer = sbitmap_alloc (ifun->eh->last_region_number + 1);
1101 sbitmap_zero (b_outer);
1105 SET_BIT (b_outer, rp_b->region_number);
1106 rp_b = rp_b->outer;
1108 while (rp_b);
1112 if (TEST_BIT (b_outer, rp_a->region_number))
1114 sbitmap_free (b_outer);
1115 return rp_a->region_number;
1117 rp_a = rp_a->outer;
1119 while (rp_a);
1121 sbitmap_free (b_outer);
1122 return -1;
1125 static int
1126 t2r_eq (const void *pentry, const void *pdata)
1128 tree entry = (tree) pentry;
1129 tree data = (tree) pdata;
1131 return TREE_PURPOSE (entry) == data;
1134 static hashval_t
1135 t2r_hash (const void *pentry)
1137 tree entry = (tree) pentry;
1138 return TREE_HASH (TREE_PURPOSE (entry));
1141 static void
1142 add_type_for_runtime (tree type)
1144 tree *slot;
1146 slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
1147 TREE_HASH (type), INSERT);
1148 if (*slot == NULL)
1150 tree runtime = (*lang_eh_runtime_type) (type);
1151 *slot = tree_cons (type, runtime, NULL_TREE);
1155 static tree
1156 lookup_type_for_runtime (tree type)
1158 tree *slot;
1160 slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
1161 TREE_HASH (type), NO_INSERT);
1163 /* We should have always inserted the data earlier. */
1164 return TREE_VALUE (*slot);
1168 /* Represent an entry in @TTypes for either catch actions
1169 or exception filter actions. */
1170 struct ttypes_filter GTY(())
1172 tree t;
1173 int filter;
1176 /* Compare ENTRY (a ttypes_filter entry in the hash table) with DATA
1177 (a tree) for a @TTypes type node we are thinking about adding. */
1179 static int
1180 ttypes_filter_eq (const void *pentry, const void *pdata)
1182 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1183 tree data = (tree) pdata;
1185 return entry->t == data;
1188 static hashval_t
1189 ttypes_filter_hash (const void *pentry)
1191 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1192 return TREE_HASH (entry->t);
1195 /* Compare ENTRY with DATA (both struct ttypes_filter) for a @TTypes
1196 exception specification list we are thinking about adding. */
1197 /* ??? Currently we use the type lists in the order given. Someone
1198 should put these in some canonical order. */
1200 static int
1201 ehspec_filter_eq (const void *pentry, const void *pdata)
1203 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1204 const struct ttypes_filter *data = (const struct ttypes_filter *) pdata;
1206 return type_list_equal (entry->t, data->t);
1209 /* Hash function for exception specification lists. */
1211 static hashval_t
1212 ehspec_filter_hash (const void *pentry)
1214 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1215 hashval_t h = 0;
1216 tree list;
1218 for (list = entry->t; list ; list = TREE_CHAIN (list))
1219 h = (h << 5) + (h >> 27) + TREE_HASH (TREE_VALUE (list));
1220 return h;
1223 /* Add TYPE (which may be NULL) to cfun->eh->ttype_data, using TYPES_HASH
1224 to speed up the search. Return the filter value to be used. */
1226 static int
1227 add_ttypes_entry (htab_t ttypes_hash, tree type)
1229 struct ttypes_filter **slot, *n;
1231 slot = (struct ttypes_filter **)
1232 htab_find_slot_with_hash (ttypes_hash, type, TREE_HASH (type), INSERT);
1234 if ((n = *slot) == NULL)
1236 /* Filter value is a 1 based table index. */
1238 n = XNEW (struct ttypes_filter);
1239 n->t = type;
1240 n->filter = VEC_length (tree, cfun->eh->ttype_data) + 1;
1241 *slot = n;
1243 VEC_safe_push (tree, gc, cfun->eh->ttype_data, type);
1246 return n->filter;
1249 /* Add LIST to cfun->eh->ehspec_data, using EHSPEC_HASH and TYPES_HASH
1250 to speed up the search. Return the filter value to be used. */
1252 static int
1253 add_ehspec_entry (htab_t ehspec_hash, htab_t ttypes_hash, tree list)
1255 struct ttypes_filter **slot, *n;
1256 struct ttypes_filter dummy;
1258 dummy.t = list;
1259 slot = (struct ttypes_filter **)
1260 htab_find_slot (ehspec_hash, &dummy, INSERT);
1262 if ((n = *slot) == NULL)
1264 /* Filter value is a -1 based byte index into a uleb128 buffer. */
1266 n = XNEW (struct ttypes_filter);
1267 n->t = list;
1268 n->filter = -(VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data) + 1);
1269 *slot = n;
1271 /* Generate a 0 terminated list of filter values. */
1272 for (; list ; list = TREE_CHAIN (list))
1274 if (targetm.arm_eabi_unwinder)
1275 VARRAY_PUSH_TREE (cfun->eh->ehspec_data, TREE_VALUE (list));
1276 else
1278 /* Look up each type in the list and encode its filter
1279 value as a uleb128. */
1280 push_uleb128 (&cfun->eh->ehspec_data,
1281 add_ttypes_entry (ttypes_hash, TREE_VALUE (list)));
1284 if (targetm.arm_eabi_unwinder)
1285 VARRAY_PUSH_TREE (cfun->eh->ehspec_data, NULL_TREE);
1286 else
1287 VARRAY_PUSH_UCHAR (cfun->eh->ehspec_data, 0);
1290 return n->filter;
1293 /* Generate the action filter values to be used for CATCH and
1294 ALLOWED_EXCEPTIONS regions. When using dwarf2 exception regions,
1295 we use lots of landing pads, and so every type or list can share
1296 the same filter value, which saves table space. */
1298 static void
1299 assign_filter_values (void)
1301 int i;
1302 htab_t ttypes, ehspec;
1304 cfun->eh->ttype_data = VEC_alloc (tree, gc, 16);
1305 if (targetm.arm_eabi_unwinder)
1306 VARRAY_TREE_INIT (cfun->eh->ehspec_data, 64, "ehspec_data");
1307 else
1308 VARRAY_UCHAR_INIT (cfun->eh->ehspec_data, 64, "ehspec_data");
1310 ttypes = htab_create (31, ttypes_filter_hash, ttypes_filter_eq, free);
1311 ehspec = htab_create (31, ehspec_filter_hash, ehspec_filter_eq, free);
1313 for (i = cfun->eh->last_region_number; i > 0; --i)
1315 struct eh_region *r;
1317 r = VEC_index (eh_region, cfun->eh->region_array, i);
1319 /* Mind we don't process a region more than once. */
1320 if (!r || r->region_number != i)
1321 continue;
1323 switch (r->type)
1325 case ERT_CATCH:
1326 /* Whatever type_list is (NULL or true list), we build a list
1327 of filters for the region. */
1328 r->u.catch.filter_list = NULL_TREE;
1330 if (r->u.catch.type_list != NULL)
1332 /* Get a filter value for each of the types caught and store
1333 them in the region's dedicated list. */
1334 tree tp_node = r->u.catch.type_list;
1336 for (;tp_node; tp_node = TREE_CHAIN (tp_node))
1338 int flt = add_ttypes_entry (ttypes, TREE_VALUE (tp_node));
1339 tree flt_node = build_int_cst (NULL_TREE, flt);
1341 r->u.catch.filter_list
1342 = tree_cons (NULL_TREE, flt_node, r->u.catch.filter_list);
1345 else
1347 /* Get a filter value for the NULL list also since it will need
1348 an action record anyway. */
1349 int flt = add_ttypes_entry (ttypes, NULL);
1350 tree flt_node = build_int_cst (NULL_TREE, flt);
1352 r->u.catch.filter_list
1353 = tree_cons (NULL_TREE, flt_node, r->u.catch.filter_list);
1356 break;
1358 case ERT_ALLOWED_EXCEPTIONS:
1359 r->u.allowed.filter
1360 = add_ehspec_entry (ehspec, ttypes, r->u.allowed.type_list);
1361 break;
1363 default:
1364 break;
1368 htab_delete (ttypes);
1369 htab_delete (ehspec);
1372 /* Emit SEQ into basic block just before INSN (that is assumed to be
1373 first instruction of some existing BB and return the newly
1374 produced block. */
1375 static basic_block
1376 emit_to_new_bb_before (rtx seq, rtx insn)
1378 rtx last;
1379 basic_block bb;
1380 edge e;
1381 edge_iterator ei;
1383 /* If there happens to be a fallthru edge (possibly created by cleanup_cfg
1384 call), we don't want it to go into newly created landing pad or other EH
1385 construct. */
1386 for (ei = ei_start (BLOCK_FOR_INSN (insn)->preds); (e = ei_safe_edge (ei)); )
1387 if (e->flags & EDGE_FALLTHRU)
1388 force_nonfallthru (e);
1389 else
1390 ei_next (&ei);
1391 last = emit_insn_before (seq, insn);
1392 if (BARRIER_P (last))
1393 last = PREV_INSN (last);
1394 bb = create_basic_block (seq, last, BLOCK_FOR_INSN (insn)->prev_bb);
1395 update_bb_for_insn (bb);
1396 bb->flags |= BB_SUPERBLOCK;
1397 return bb;
1400 /* Generate the code to actually handle exceptions, which will follow the
1401 landing pads. */
1403 static void
1404 build_post_landing_pads (void)
1406 int i;
1408 for (i = cfun->eh->last_region_number; i > 0; --i)
1410 struct eh_region *region;
1411 rtx seq;
1413 region = VEC_index (eh_region, cfun->eh->region_array, i);
1414 /* Mind we don't process a region more than once. */
1415 if (!region || region->region_number != i)
1416 continue;
1418 switch (region->type)
1420 case ERT_TRY:
1421 /* ??? Collect the set of all non-overlapping catch handlers
1422 all the way up the chain until blocked by a cleanup. */
1423 /* ??? Outer try regions can share landing pads with inner
1424 try regions if the types are completely non-overlapping,
1425 and there are no intervening cleanups. */
1427 region->post_landing_pad = gen_label_rtx ();
1429 start_sequence ();
1431 emit_label (region->post_landing_pad);
1433 /* ??? It is mighty inconvenient to call back into the
1434 switch statement generation code in expand_end_case.
1435 Rapid prototyping sez a sequence of ifs. */
1437 struct eh_region *c;
1438 for (c = region->u.try.catch; c ; c = c->u.catch.next_catch)
1440 if (c->u.catch.type_list == NULL)
1441 emit_jump (c->label);
1442 else
1444 /* Need for one cmp/jump per type caught. Each type
1445 list entry has a matching entry in the filter list
1446 (see assign_filter_values). */
1447 tree tp_node = c->u.catch.type_list;
1448 tree flt_node = c->u.catch.filter_list;
1450 for (; tp_node; )
1452 emit_cmp_and_jump_insns
1453 (cfun->eh->filter,
1454 GEN_INT (tree_low_cst (TREE_VALUE (flt_node), 0)),
1455 EQ, NULL_RTX,
1456 targetm.eh_return_filter_mode (), 0, c->label);
1458 tp_node = TREE_CHAIN (tp_node);
1459 flt_node = TREE_CHAIN (flt_node);
1465 /* We delay the generation of the _Unwind_Resume until we generate
1466 landing pads. We emit a marker here so as to get good control
1467 flow data in the meantime. */
1468 region->resume
1469 = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
1470 emit_barrier ();
1472 seq = get_insns ();
1473 end_sequence ();
1475 emit_to_new_bb_before (seq, region->u.try.catch->label);
1477 break;
1479 case ERT_ALLOWED_EXCEPTIONS:
1480 region->post_landing_pad = gen_label_rtx ();
1482 start_sequence ();
1484 emit_label (region->post_landing_pad);
1486 emit_cmp_and_jump_insns (cfun->eh->filter,
1487 GEN_INT (region->u.allowed.filter),
1488 EQ, NULL_RTX,
1489 targetm.eh_return_filter_mode (), 0, region->label);
1491 /* We delay the generation of the _Unwind_Resume until we generate
1492 landing pads. We emit a marker here so as to get good control
1493 flow data in the meantime. */
1494 region->resume
1495 = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
1496 emit_barrier ();
1498 seq = get_insns ();
1499 end_sequence ();
1501 emit_to_new_bb_before (seq, region->label);
1502 break;
1504 case ERT_CLEANUP:
1505 case ERT_MUST_NOT_THROW:
1506 region->post_landing_pad = region->label;
1507 break;
1509 case ERT_CATCH:
1510 case ERT_THROW:
1511 /* Nothing to do. */
1512 break;
1514 default:
1515 gcc_unreachable ();
1520 /* Replace RESX patterns with jumps to the next handler if any, or calls to
1521 _Unwind_Resume otherwise. */
1523 static void
1524 connect_post_landing_pads (void)
1526 int i;
1528 for (i = cfun->eh->last_region_number; i > 0; --i)
1530 struct eh_region *region;
1531 struct eh_region *outer;
1532 rtx seq;
1533 rtx barrier;
1535 region = VEC_index (eh_region, cfun->eh->region_array, i);
1536 /* Mind we don't process a region more than once. */
1537 if (!region || region->region_number != i)
1538 continue;
1540 /* If there is no RESX, or it has been deleted by flow, there's
1541 nothing to fix up. */
1542 if (! region->resume || INSN_DELETED_P (region->resume))
1543 continue;
1545 /* Search for another landing pad in this function. */
1546 for (outer = region->outer; outer ; outer = outer->outer)
1547 if (outer->post_landing_pad)
1548 break;
1550 start_sequence ();
1552 if (outer)
1554 edge e;
1555 basic_block src, dest;
1557 emit_jump (outer->post_landing_pad);
1558 src = BLOCK_FOR_INSN (region->resume);
1559 dest = BLOCK_FOR_INSN (outer->post_landing_pad);
1560 while (EDGE_COUNT (src->succs) > 0)
1561 remove_edge (EDGE_SUCC (src, 0));
1562 e = make_edge (src, dest, 0);
1563 e->probability = REG_BR_PROB_BASE;
1564 e->count = src->count;
1566 else
1568 emit_library_call (unwind_resume_libfunc, LCT_THROW,
1569 VOIDmode, 1, cfun->eh->exc_ptr, ptr_mode);
1571 /* What we just emitted was a throwing libcall, so it got a
1572 barrier automatically added after it. If the last insn in
1573 the libcall sequence isn't the barrier, it's because the
1574 target emits multiple insns for a call, and there are insns
1575 after the actual call insn (which are redundant and would be
1576 optimized away). The barrier is inserted exactly after the
1577 call insn, so let's go get that and delete the insns after
1578 it, because below we need the barrier to be the last insn in
1579 the sequence. */
1580 delete_insns_since (NEXT_INSN (last_call_insn ()));
1583 seq = get_insns ();
1584 end_sequence ();
1585 barrier = emit_insn_before (seq, region->resume);
1586 /* Avoid duplicate barrier. */
1587 gcc_assert (BARRIER_P (barrier));
1588 delete_insn (barrier);
1589 delete_insn (region->resume);
1591 /* ??? From tree-ssa we can wind up with catch regions whose
1592 label is not instantiated, but whose resx is present. Now
1593 that we've dealt with the resx, kill the region. */
1594 if (region->label == NULL && region->type == ERT_CLEANUP)
1595 remove_eh_handler (region);
1600 static void
1601 dw2_build_landing_pads (void)
1603 int i;
1604 unsigned int j;
1606 for (i = cfun->eh->last_region_number; i > 0; --i)
1608 struct eh_region *region;
1609 rtx seq;
1610 basic_block bb;
1611 bool clobbers_hard_regs = false;
1612 edge e;
1614 region = VEC_index (eh_region, cfun->eh->region_array, i);
1615 /* Mind we don't process a region more than once. */
1616 if (!region || region->region_number != i)
1617 continue;
1619 if (region->type != ERT_CLEANUP
1620 && region->type != ERT_TRY
1621 && region->type != ERT_ALLOWED_EXCEPTIONS)
1622 continue;
1624 start_sequence ();
1626 region->landing_pad = gen_label_rtx ();
1627 emit_label (region->landing_pad);
1629 #ifdef HAVE_exception_receiver
1630 if (HAVE_exception_receiver)
1631 emit_insn (gen_exception_receiver ());
1632 else
1633 #endif
1634 #ifdef HAVE_nonlocal_goto_receiver
1635 if (HAVE_nonlocal_goto_receiver)
1636 emit_insn (gen_nonlocal_goto_receiver ());
1637 else
1638 #endif
1639 { /* Nothing */ }
1641 /* If the eh_return data registers are call-saved, then we
1642 won't have considered them clobbered from the call that
1643 threw. Kill them now. */
1644 for (j = 0; ; ++j)
1646 unsigned r = EH_RETURN_DATA_REGNO (j);
1647 if (r == INVALID_REGNUM)
1648 break;
1649 if (! call_used_regs[r])
1651 emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, r)));
1652 clobbers_hard_regs = true;
1656 if (clobbers_hard_regs)
1658 /* @@@ This is a kludge. Not all machine descriptions define a
1659 blockage insn, but we must not allow the code we just generated
1660 to be reordered by scheduling. So emit an ASM_INPUT to act as
1661 blockage insn. */
1662 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
1665 emit_move_insn (cfun->eh->exc_ptr,
1666 gen_rtx_REG (ptr_mode, EH_RETURN_DATA_REGNO (0)));
1667 emit_move_insn (cfun->eh->filter,
1668 gen_rtx_REG (targetm.eh_return_filter_mode (),
1669 EH_RETURN_DATA_REGNO (1)));
1671 seq = get_insns ();
1672 end_sequence ();
1674 bb = emit_to_new_bb_before (seq, region->post_landing_pad);
1675 e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
1676 e->count = bb->count;
1677 e->probability = REG_BR_PROB_BASE;
1682 struct sjlj_lp_info
1684 int directly_reachable;
1685 int action_index;
1686 int dispatch_index;
1687 int call_site_index;
1690 static bool
1691 sjlj_find_directly_reachable_regions (struct sjlj_lp_info *lp_info)
1693 rtx insn;
1694 bool found_one = false;
1696 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
1698 struct eh_region *region;
1699 enum reachable_code rc;
1700 tree type_thrown;
1701 rtx note;
1703 if (! INSN_P (insn))
1704 continue;
1706 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1707 if (!note || INTVAL (XEXP (note, 0)) <= 0)
1708 continue;
1710 region = VEC_index (eh_region, cfun->eh->region_array, INTVAL (XEXP (note, 0)));
1712 type_thrown = NULL_TREE;
1713 if (region->type == ERT_THROW)
1715 type_thrown = region->u.throw.type;
1716 region = region->outer;
1719 /* Find the first containing region that might handle the exception.
1720 That's the landing pad to which we will transfer control. */
1721 rc = RNL_NOT_CAUGHT;
1722 for (; region; region = region->outer)
1724 rc = reachable_next_level (region, type_thrown, NULL);
1725 if (rc != RNL_NOT_CAUGHT)
1726 break;
1728 if (rc == RNL_MAYBE_CAUGHT || rc == RNL_CAUGHT)
1730 lp_info[region->region_number].directly_reachable = 1;
1731 found_one = true;
1735 return found_one;
1738 static void
1739 sjlj_assign_call_site_values (rtx dispatch_label, struct sjlj_lp_info *lp_info)
1741 htab_t ar_hash;
1742 int i, index;
1744 /* First task: build the action table. */
1746 VARRAY_UCHAR_INIT (cfun->eh->action_record_data, 64, "action_record_data");
1747 ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
1749 for (i = cfun->eh->last_region_number; i > 0; --i)
1750 if (lp_info[i].directly_reachable)
1752 struct eh_region *r = VEC_index (eh_region, cfun->eh->region_array, i);
1754 r->landing_pad = dispatch_label;
1755 lp_info[i].action_index = collect_one_action_chain (ar_hash, r);
1756 if (lp_info[i].action_index != -1)
1757 cfun->uses_eh_lsda = 1;
1760 htab_delete (ar_hash);
1762 /* Next: assign dispatch values. In dwarf2 terms, this would be the
1763 landing pad label for the region. For sjlj though, there is one
1764 common landing pad from which we dispatch to the post-landing pads.
1766 A region receives a dispatch index if it is directly reachable
1767 and requires in-function processing. Regions that share post-landing
1768 pads may share dispatch indices. */
1769 /* ??? Post-landing pad sharing doesn't actually happen at the moment
1770 (see build_post_landing_pads) so we don't bother checking for it. */
1772 index = 0;
1773 for (i = cfun->eh->last_region_number; i > 0; --i)
1774 if (lp_info[i].directly_reachable)
1775 lp_info[i].dispatch_index = index++;
1777 /* Finally: assign call-site values. If dwarf2 terms, this would be
1778 the region number assigned by convert_to_eh_region_ranges, but
1779 handles no-action and must-not-throw differently. */
1781 call_site_base = 1;
1782 for (i = cfun->eh->last_region_number; i > 0; --i)
1783 if (lp_info[i].directly_reachable)
1785 int action = lp_info[i].action_index;
1787 /* Map must-not-throw to otherwise unused call-site index 0. */
1788 if (action == -2)
1789 index = 0;
1790 /* Map no-action to otherwise unused call-site index -1. */
1791 else if (action == -1)
1792 index = -1;
1793 /* Otherwise, look it up in the table. */
1794 else
1795 index = add_call_site (GEN_INT (lp_info[i].dispatch_index), action);
1797 lp_info[i].call_site_index = index;
1801 static void
1802 sjlj_mark_call_sites (struct sjlj_lp_info *lp_info)
1804 int last_call_site = -2;
1805 rtx insn, mem;
1807 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
1809 struct eh_region *region;
1810 int this_call_site;
1811 rtx note, before, p;
1813 /* Reset value tracking at extended basic block boundaries. */
1814 if (LABEL_P (insn))
1815 last_call_site = -2;
1817 if (! INSN_P (insn))
1818 continue;
1820 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1821 if (!note)
1823 /* Calls (and trapping insns) without notes are outside any
1824 exception handling region in this function. Mark them as
1825 no action. */
1826 if (CALL_P (insn)
1827 || (flag_non_call_exceptions
1828 && may_trap_p (PATTERN (insn))))
1829 this_call_site = -1;
1830 else
1831 continue;
1833 else
1835 /* Calls that are known to not throw need not be marked. */
1836 if (INTVAL (XEXP (note, 0)) <= 0)
1837 continue;
1839 region = VEC_index (eh_region, cfun->eh->region_array, INTVAL (XEXP (note, 0)));
1840 this_call_site = lp_info[region->region_number].call_site_index;
1843 if (this_call_site == last_call_site)
1844 continue;
1846 /* Don't separate a call from it's argument loads. */
1847 before = insn;
1848 if (CALL_P (insn))
1849 before = find_first_parameter_load (insn, NULL_RTX);
1851 start_sequence ();
1852 mem = adjust_address (cfun->eh->sjlj_fc, TYPE_MODE (integer_type_node),
1853 sjlj_fc_call_site_ofs);
1854 emit_move_insn (mem, GEN_INT (this_call_site));
1855 p = get_insns ();
1856 end_sequence ();
1858 emit_insn_before (p, before);
1859 last_call_site = this_call_site;
1863 /* Construct the SjLj_Function_Context. */
1865 static void
1866 sjlj_emit_function_enter (rtx dispatch_label)
1868 rtx fn_begin, fc, mem, seq;
1869 bool fn_begin_outside_block;
1871 fc = cfun->eh->sjlj_fc;
1873 start_sequence ();
1875 /* We're storing this libcall's address into memory instead of
1876 calling it directly. Thus, we must call assemble_external_libcall
1877 here, as we can not depend on emit_library_call to do it for us. */
1878 assemble_external_libcall (eh_personality_libfunc);
1879 mem = adjust_address (fc, Pmode, sjlj_fc_personality_ofs);
1880 emit_move_insn (mem, eh_personality_libfunc);
1882 mem = adjust_address (fc, Pmode, sjlj_fc_lsda_ofs);
1883 if (cfun->uses_eh_lsda)
1885 char buf[20];
1886 rtx sym;
1888 ASM_GENERATE_INTERNAL_LABEL (buf, "LLSDA", current_function_funcdef_no);
1889 sym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
1890 SYMBOL_REF_FLAGS (sym) = SYMBOL_FLAG_LOCAL;
1891 emit_move_insn (mem, sym);
1893 else
1894 emit_move_insn (mem, const0_rtx);
1896 #ifdef DONT_USE_BUILTIN_SETJMP
1898 rtx x, note;
1899 x = emit_library_call_value (setjmp_libfunc, NULL_RTX, LCT_RETURNS_TWICE,
1900 TYPE_MODE (integer_type_node), 1,
1901 plus_constant (XEXP (fc, 0),
1902 sjlj_fc_jbuf_ofs), Pmode);
1904 note = emit_note (NOTE_INSN_EXPECTED_VALUE);
1905 NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, x, const0_rtx);
1907 emit_cmp_and_jump_insns (x, const0_rtx, NE, 0,
1908 TYPE_MODE (integer_type_node), 0, dispatch_label);
1910 #else
1911 expand_builtin_setjmp_setup (plus_constant (XEXP (fc, 0), sjlj_fc_jbuf_ofs),
1912 dispatch_label);
1913 #endif
1915 emit_library_call (unwind_sjlj_register_libfunc, LCT_NORMAL, VOIDmode,
1916 1, XEXP (fc, 0), Pmode);
1918 seq = get_insns ();
1919 end_sequence ();
1921 /* ??? Instead of doing this at the beginning of the function,
1922 do this in a block that is at loop level 0 and dominates all
1923 can_throw_internal instructions. */
1925 fn_begin_outside_block = true;
1926 for (fn_begin = get_insns (); ; fn_begin = NEXT_INSN (fn_begin))
1927 if (NOTE_P (fn_begin))
1929 if (NOTE_LINE_NUMBER (fn_begin) == NOTE_INSN_FUNCTION_BEG)
1930 break;
1931 else if (NOTE_LINE_NUMBER (fn_begin) == NOTE_INSN_BASIC_BLOCK)
1932 fn_begin_outside_block = false;
1935 if (fn_begin_outside_block)
1936 insert_insn_on_edge (seq, single_succ_edge (ENTRY_BLOCK_PTR));
1937 else
1938 emit_insn_after (seq, fn_begin);
1941 /* Call back from expand_function_end to know where we should put
1942 the call to unwind_sjlj_unregister_libfunc if needed. */
1944 void
1945 sjlj_emit_function_exit_after (rtx after)
1947 cfun->eh->sjlj_exit_after = after;
1950 static void
1951 sjlj_emit_function_exit (void)
1953 rtx seq;
1954 edge e;
1955 edge_iterator ei;
1957 start_sequence ();
1959 emit_library_call (unwind_sjlj_unregister_libfunc, LCT_NORMAL, VOIDmode,
1960 1, XEXP (cfun->eh->sjlj_fc, 0), Pmode);
1962 seq = get_insns ();
1963 end_sequence ();
1965 /* ??? Really this can be done in any block at loop level 0 that
1966 post-dominates all can_throw_internal instructions. This is
1967 the last possible moment. */
1969 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
1970 if (e->flags & EDGE_FALLTHRU)
1971 break;
1972 if (e)
1974 rtx insn;
1976 /* Figure out whether the place we are supposed to insert libcall
1977 is inside the last basic block or after it. In the other case
1978 we need to emit to edge. */
1979 gcc_assert (e->src->next_bb == EXIT_BLOCK_PTR);
1980 for (insn = BB_HEAD (e->src); ; insn = NEXT_INSN (insn))
1982 if (insn == cfun->eh->sjlj_exit_after)
1984 if (LABEL_P (insn))
1985 insn = NEXT_INSN (insn);
1986 emit_insn_after (seq, insn);
1987 return;
1989 if (insn == BB_END (e->src))
1990 break;
1992 insert_insn_on_edge (seq, e);
1996 static void
1997 sjlj_emit_dispatch_table (rtx dispatch_label, struct sjlj_lp_info *lp_info)
1999 int i, first_reachable;
2000 rtx mem, dispatch, seq, fc;
2001 rtx before;
2002 basic_block bb;
2003 edge e;
2005 fc = cfun->eh->sjlj_fc;
2007 start_sequence ();
2009 emit_label (dispatch_label);
2011 #ifndef DONT_USE_BUILTIN_SETJMP
2012 expand_builtin_setjmp_receiver (dispatch_label);
2013 #endif
2015 /* Load up dispatch index, exc_ptr and filter values from the
2016 function context. */
2017 mem = adjust_address (fc, TYPE_MODE (integer_type_node),
2018 sjlj_fc_call_site_ofs);
2019 dispatch = copy_to_reg (mem);
2021 mem = adjust_address (fc, word_mode, sjlj_fc_data_ofs);
2022 if (word_mode != ptr_mode)
2024 #ifdef POINTERS_EXTEND_UNSIGNED
2025 mem = convert_memory_address (ptr_mode, mem);
2026 #else
2027 mem = convert_to_mode (ptr_mode, mem, 0);
2028 #endif
2030 emit_move_insn (cfun->eh->exc_ptr, mem);
2032 mem = adjust_address (fc, word_mode, sjlj_fc_data_ofs + UNITS_PER_WORD);
2033 emit_move_insn (cfun->eh->filter, mem);
2035 /* Jump to one of the directly reachable regions. */
2036 /* ??? This really ought to be using a switch statement. */
2038 first_reachable = 0;
2039 for (i = cfun->eh->last_region_number; i > 0; --i)
2041 if (! lp_info[i].directly_reachable)
2042 continue;
2044 if (! first_reachable)
2046 first_reachable = i;
2047 continue;
2050 emit_cmp_and_jump_insns (dispatch, GEN_INT (lp_info[i].dispatch_index),
2051 EQ, NULL_RTX, TYPE_MODE (integer_type_node), 0,
2052 ((struct eh_region *)VEC_index (eh_region, cfun->eh->region_array, i))
2053 ->post_landing_pad);
2056 seq = get_insns ();
2057 end_sequence ();
2059 before = (((struct eh_region *)VEC_index (eh_region, cfun->eh->region_array, first_reachable))
2060 ->post_landing_pad);
2062 bb = emit_to_new_bb_before (seq, before);
2063 e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
2064 e->count = bb->count;
2065 e->probability = REG_BR_PROB_BASE;
2068 static void
2069 sjlj_build_landing_pads (void)
2071 struct sjlj_lp_info *lp_info;
2073 lp_info = XCNEWVEC (struct sjlj_lp_info, cfun->eh->last_region_number + 1);
2075 if (sjlj_find_directly_reachable_regions (lp_info))
2077 rtx dispatch_label = gen_label_rtx ();
2079 cfun->eh->sjlj_fc
2080 = assign_stack_local (TYPE_MODE (sjlj_fc_type_node),
2081 int_size_in_bytes (sjlj_fc_type_node),
2082 TYPE_ALIGN (sjlj_fc_type_node));
2084 sjlj_assign_call_site_values (dispatch_label, lp_info);
2085 sjlj_mark_call_sites (lp_info);
2087 sjlj_emit_function_enter (dispatch_label);
2088 sjlj_emit_dispatch_table (dispatch_label, lp_info);
2089 sjlj_emit_function_exit ();
2092 free (lp_info);
2095 void
2096 finish_eh_generation (void)
2098 basic_block bb;
2100 /* Nothing to do if no regions created. */
2101 if (cfun->eh->region_tree == NULL)
2102 return;
2104 /* The object here is to provide find_basic_blocks with detailed
2105 information (via reachable_handlers) on how exception control
2106 flows within the function. In this first pass, we can include
2107 type information garnered from ERT_THROW and ERT_ALLOWED_EXCEPTIONS
2108 regions, and hope that it will be useful in deleting unreachable
2109 handlers. Subsequently, we will generate landing pads which will
2110 connect many of the handlers, and then type information will not
2111 be effective. Still, this is a win over previous implementations. */
2113 /* These registers are used by the landing pads. Make sure they
2114 have been generated. */
2115 get_exception_pointer (cfun);
2116 get_exception_filter (cfun);
2118 /* Construct the landing pads. */
2120 assign_filter_values ();
2121 build_post_landing_pads ();
2122 connect_post_landing_pads ();
2123 if (USING_SJLJ_EXCEPTIONS)
2124 sjlj_build_landing_pads ();
2125 else
2126 dw2_build_landing_pads ();
2128 cfun->eh->built_landing_pads = 1;
2130 /* We've totally changed the CFG. Start over. */
2131 find_exception_handler_labels ();
2132 break_superblocks ();
2133 if (USING_SJLJ_EXCEPTIONS)
2134 commit_edge_insertions ();
2135 FOR_EACH_BB (bb)
2137 edge e;
2138 edge_iterator ei;
2139 bool eh = false;
2140 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2142 if (e->flags & EDGE_EH)
2144 remove_edge (e);
2145 eh = true;
2147 else
2148 ei_next (&ei);
2150 if (eh)
2151 rtl_make_eh_edge (NULL, bb, BB_END (bb));
2155 static hashval_t
2156 ehl_hash (const void *pentry)
2158 struct ehl_map_entry *entry = (struct ehl_map_entry *) pentry;
2160 /* 2^32 * ((sqrt(5) - 1) / 2) */
2161 const hashval_t scaled_golden_ratio = 0x9e3779b9;
2162 return CODE_LABEL_NUMBER (entry->label) * scaled_golden_ratio;
2165 static int
2166 ehl_eq (const void *pentry, const void *pdata)
2168 struct ehl_map_entry *entry = (struct ehl_map_entry *) pentry;
2169 struct ehl_map_entry *data = (struct ehl_map_entry *) pdata;
2171 return entry->label == data->label;
2174 /* This section handles removing dead code for flow. */
2176 /* Remove LABEL from exception_handler_label_map. */
2178 static void
2179 remove_exception_handler_label (rtx label)
2181 struct ehl_map_entry **slot, tmp;
2183 /* If exception_handler_label_map was not built yet,
2184 there is nothing to do. */
2185 if (cfun->eh->exception_handler_label_map == NULL)
2186 return;
2188 tmp.label = label;
2189 slot = (struct ehl_map_entry **)
2190 htab_find_slot (cfun->eh->exception_handler_label_map, &tmp, NO_INSERT);
2191 gcc_assert (slot);
2193 htab_clear_slot (cfun->eh->exception_handler_label_map, (void **) slot);
2196 /* Splice REGION from the region tree etc. */
2198 static void
2199 remove_eh_handler (struct eh_region *region)
2201 struct eh_region **pp, **pp_start, *p, *outer, *inner;
2202 rtx lab;
2204 /* For the benefit of efficiently handling REG_EH_REGION notes,
2205 replace this region in the region array with its containing
2206 region. Note that previous region deletions may result in
2207 multiple copies of this region in the array, so we have a
2208 list of alternate numbers by which we are known. */
2210 outer = region->outer;
2211 VEC_replace (eh_region, cfun->eh->region_array, region->region_number, outer);
2212 if (region->aka)
2214 unsigned i;
2215 bitmap_iterator bi;
2217 EXECUTE_IF_SET_IN_BITMAP (region->aka, 0, i, bi)
2219 VEC_replace (eh_region, cfun->eh->region_array, i, outer);
2223 if (outer)
2225 if (!outer->aka)
2226 outer->aka = BITMAP_GGC_ALLOC ();
2227 if (region->aka)
2228 bitmap_ior_into (outer->aka, region->aka);
2229 bitmap_set_bit (outer->aka, region->region_number);
2232 if (cfun->eh->built_landing_pads)
2233 lab = region->landing_pad;
2234 else
2235 lab = region->label;
2236 if (lab)
2237 remove_exception_handler_label (lab);
2239 if (outer)
2240 pp_start = &outer->inner;
2241 else
2242 pp_start = &cfun->eh->region_tree;
2243 for (pp = pp_start, p = *pp; p != region; pp = &p->next_peer, p = *pp)
2244 continue;
2245 *pp = region->next_peer;
2247 inner = region->inner;
2248 if (inner)
2250 for (p = inner; p->next_peer ; p = p->next_peer)
2251 p->outer = outer;
2252 p->outer = outer;
2254 p->next_peer = *pp_start;
2255 *pp_start = inner;
2258 if (region->type == ERT_CATCH)
2260 struct eh_region *try, *next, *prev;
2262 for (try = region->next_peer;
2263 try->type == ERT_CATCH;
2264 try = try->next_peer)
2265 continue;
2266 gcc_assert (try->type == ERT_TRY);
2268 next = region->u.catch.next_catch;
2269 prev = region->u.catch.prev_catch;
2271 if (next)
2272 next->u.catch.prev_catch = prev;
2273 else
2274 try->u.try.last_catch = prev;
2275 if (prev)
2276 prev->u.catch.next_catch = next;
2277 else
2279 try->u.try.catch = next;
2280 if (! next)
2281 remove_eh_handler (try);
2286 /* LABEL heads a basic block that is about to be deleted. If this
2287 label corresponds to an exception region, we may be able to
2288 delete the region. */
2290 void
2291 maybe_remove_eh_handler (rtx label)
2293 struct ehl_map_entry **slot, tmp;
2294 struct eh_region *region;
2296 /* ??? After generating landing pads, it's not so simple to determine
2297 if the region data is completely unused. One must examine the
2298 landing pad and the post landing pad, and whether an inner try block
2299 is referencing the catch handlers directly. */
2300 if (cfun->eh->built_landing_pads)
2301 return;
2303 tmp.label = label;
2304 slot = (struct ehl_map_entry **)
2305 htab_find_slot (cfun->eh->exception_handler_label_map, &tmp, NO_INSERT);
2306 if (! slot)
2307 return;
2308 region = (*slot)->region;
2309 if (! region)
2310 return;
2312 /* Flow will want to remove MUST_NOT_THROW regions as unreachable
2313 because there is no path to the fallback call to terminate.
2314 But the region continues to affect call-site data until there
2315 are no more contained calls, which we don't see here. */
2316 if (region->type == ERT_MUST_NOT_THROW)
2318 htab_clear_slot (cfun->eh->exception_handler_label_map, (void **) slot);
2319 region->label = NULL_RTX;
2321 else
2322 remove_eh_handler (region);
2325 /* Invokes CALLBACK for every exception handler label. Only used by old
2326 loop hackery; should not be used by new code. */
2328 void
2329 for_each_eh_label (void (*callback) (rtx))
2331 htab_traverse (cfun->eh->exception_handler_label_map, for_each_eh_label_1,
2332 (void *) &callback);
2335 static int
2336 for_each_eh_label_1 (void **pentry, void *data)
2338 struct ehl_map_entry *entry = *(struct ehl_map_entry **)pentry;
2339 void (*callback) (rtx) = *(void (**) (rtx)) data;
2341 (*callback) (entry->label);
2342 return 1;
2345 /* Invoke CALLBACK for every exception region in the current function. */
2347 void
2348 for_each_eh_region (void (*callback) (struct eh_region *))
2350 int i, n = cfun->eh->last_region_number;
2351 for (i = 1; i <= n; ++i)
2353 struct eh_region *region;
2355 region = VEC_index (eh_region, cfun->eh->region_array, i);
2356 if (region)
2357 (*callback) (region);
2361 /* This section describes CFG exception edges for flow. */
2363 /* For communicating between calls to reachable_next_level. */
2364 struct reachable_info
2366 tree types_caught;
2367 tree types_allowed;
2368 void (*callback) (struct eh_region *, void *);
2369 void *callback_data;
2370 bool saw_any_handlers;
2373 /* A subroutine of reachable_next_level. Return true if TYPE, or a
2374 base class of TYPE, is in HANDLED. */
2376 static int
2377 check_handled (tree handled, tree type)
2379 tree t;
2381 /* We can check for exact matches without front-end help. */
2382 if (! lang_eh_type_covers)
2384 for (t = handled; t ; t = TREE_CHAIN (t))
2385 if (TREE_VALUE (t) == type)
2386 return 1;
2388 else
2390 for (t = handled; t ; t = TREE_CHAIN (t))
2391 if ((*lang_eh_type_covers) (TREE_VALUE (t), type))
2392 return 1;
2395 return 0;
2398 /* A subroutine of reachable_next_level. If we are collecting a list
2399 of handlers, add one. After landing pad generation, reference
2400 it instead of the handlers themselves. Further, the handlers are
2401 all wired together, so by referencing one, we've got them all.
2402 Before landing pad generation we reference each handler individually.
2404 LP_REGION contains the landing pad; REGION is the handler. */
2406 static void
2407 add_reachable_handler (struct reachable_info *info,
2408 struct eh_region *lp_region, struct eh_region *region)
2410 if (! info)
2411 return;
2413 info->saw_any_handlers = true;
2415 if (cfun->eh->built_landing_pads)
2416 info->callback (lp_region, info->callback_data);
2417 else
2418 info->callback (region, info->callback_data);
2421 /* Process one level of exception regions for reachability.
2422 If TYPE_THROWN is non-null, then it is the *exact* type being
2423 propagated. If INFO is non-null, then collect handler labels
2424 and caught/allowed type information between invocations. */
2426 static enum reachable_code
2427 reachable_next_level (struct eh_region *region, tree type_thrown,
2428 struct reachable_info *info)
2430 switch (region->type)
2432 case ERT_CLEANUP:
2433 /* Before landing-pad generation, we model control flow
2434 directly to the individual handlers. In this way we can
2435 see that catch handler types may shadow one another. */
2436 add_reachable_handler (info, region, region);
2437 return RNL_MAYBE_CAUGHT;
2439 case ERT_TRY:
2441 struct eh_region *c;
2442 enum reachable_code ret = RNL_NOT_CAUGHT;
2444 for (c = region->u.try.catch; c ; c = c->u.catch.next_catch)
2446 /* A catch-all handler ends the search. */
2447 if (c->u.catch.type_list == NULL)
2449 add_reachable_handler (info, region, c);
2450 return RNL_CAUGHT;
2453 if (type_thrown)
2455 /* If we have at least one type match, end the search. */
2456 tree tp_node = c->u.catch.type_list;
2458 for (; tp_node; tp_node = TREE_CHAIN (tp_node))
2460 tree type = TREE_VALUE (tp_node);
2462 if (type == type_thrown
2463 || (lang_eh_type_covers
2464 && (*lang_eh_type_covers) (type, type_thrown)))
2466 add_reachable_handler (info, region, c);
2467 return RNL_CAUGHT;
2471 /* If we have definitive information of a match failure,
2472 the catch won't trigger. */
2473 if (lang_eh_type_covers)
2474 return RNL_NOT_CAUGHT;
2477 /* At this point, we either don't know what type is thrown or
2478 don't have front-end assistance to help deciding if it is
2479 covered by one of the types in the list for this region.
2481 We'd then like to add this region to the list of reachable
2482 handlers since it is indeed potentially reachable based on the
2483 information we have.
2485 Actually, this handler is for sure not reachable if all the
2486 types it matches have already been caught. That is, it is only
2487 potentially reachable if at least one of the types it catches
2488 has not been previously caught. */
2490 if (! info)
2491 ret = RNL_MAYBE_CAUGHT;
2492 else
2494 tree tp_node = c->u.catch.type_list;
2495 bool maybe_reachable = false;
2497 /* Compute the potential reachability of this handler and
2498 update the list of types caught at the same time. */
2499 for (; tp_node; tp_node = TREE_CHAIN (tp_node))
2501 tree type = TREE_VALUE (tp_node);
2503 if (! check_handled (info->types_caught, type))
2505 info->types_caught
2506 = tree_cons (NULL, type, info->types_caught);
2508 maybe_reachable = true;
2512 if (maybe_reachable)
2514 add_reachable_handler (info, region, c);
2516 /* ??? If the catch type is a base class of every allowed
2517 type, then we know we can stop the search. */
2518 ret = RNL_MAYBE_CAUGHT;
2523 return ret;
2526 case ERT_ALLOWED_EXCEPTIONS:
2527 /* An empty list of types definitely ends the search. */
2528 if (region->u.allowed.type_list == NULL_TREE)
2530 add_reachable_handler (info, region, region);
2531 return RNL_CAUGHT;
2534 /* Collect a list of lists of allowed types for use in detecting
2535 when a catch may be transformed into a catch-all. */
2536 if (info)
2537 info->types_allowed = tree_cons (NULL_TREE,
2538 region->u.allowed.type_list,
2539 info->types_allowed);
2541 /* If we have definitive information about the type hierarchy,
2542 then we can tell if the thrown type will pass through the
2543 filter. */
2544 if (type_thrown && lang_eh_type_covers)
2546 if (check_handled (region->u.allowed.type_list, type_thrown))
2547 return RNL_NOT_CAUGHT;
2548 else
2550 add_reachable_handler (info, region, region);
2551 return RNL_CAUGHT;
2555 add_reachable_handler (info, region, region);
2556 return RNL_MAYBE_CAUGHT;
2558 case ERT_CATCH:
2559 /* Catch regions are handled by their controlling try region. */
2560 return RNL_NOT_CAUGHT;
2562 case ERT_MUST_NOT_THROW:
2563 /* Here we end our search, since no exceptions may propagate.
2564 If we've touched down at some landing pad previous, then the
2565 explicit function call we generated may be used. Otherwise
2566 the call is made by the runtime.
2568 Before inlining, do not perform this optimization. We may
2569 inline a subroutine that contains handlers, and that will
2570 change the value of saw_any_handlers. */
2572 if ((info && info->saw_any_handlers) || !cfun->after_inlining)
2574 add_reachable_handler (info, region, region);
2575 return RNL_CAUGHT;
2577 else
2578 return RNL_BLOCKED;
2580 case ERT_THROW:
2581 case ERT_UNKNOWN:
2582 /* Shouldn't see these here. */
2583 gcc_unreachable ();
2584 break;
2585 default:
2586 gcc_unreachable ();
2590 /* Invoke CALLBACK on each region reachable from REGION_NUMBER. */
2592 void
2593 foreach_reachable_handler (int region_number, bool is_resx,
2594 void (*callback) (struct eh_region *, void *),
2595 void *callback_data)
2597 struct reachable_info info;
2598 struct eh_region *region;
2599 tree type_thrown;
2601 memset (&info, 0, sizeof (info));
2602 info.callback = callback;
2603 info.callback_data = callback_data;
2605 region = VEC_index (eh_region, cfun->eh->region_array, region_number);
2607 type_thrown = NULL_TREE;
2608 if (is_resx)
2610 /* A RESX leaves a region instead of entering it. Thus the
2611 region itself may have been deleted out from under us. */
2612 if (region == NULL)
2613 return;
2614 region = region->outer;
2616 else if (region->type == ERT_THROW)
2618 type_thrown = region->u.throw.type;
2619 region = region->outer;
2622 while (region)
2624 if (reachable_next_level (region, type_thrown, &info) >= RNL_CAUGHT)
2625 break;
2626 /* If we have processed one cleanup, there is no point in
2627 processing any more of them. Each cleanup will have an edge
2628 to the next outer cleanup region, so the flow graph will be
2629 accurate. */
2630 if (region->type == ERT_CLEANUP)
2631 region = region->u.cleanup.prev_try;
2632 else
2633 region = region->outer;
2637 /* Retrieve a list of labels of exception handlers which can be
2638 reached by a given insn. */
2640 static void
2641 arh_to_landing_pad (struct eh_region *region, void *data)
2643 rtx *p_handlers = data;
2644 if (! *p_handlers)
2645 *p_handlers = alloc_INSN_LIST (region->landing_pad, NULL_RTX);
2648 static void
2649 arh_to_label (struct eh_region *region, void *data)
2651 rtx *p_handlers = data;
2652 *p_handlers = alloc_INSN_LIST (region->label, *p_handlers);
2656 reachable_handlers (rtx insn)
2658 bool is_resx = false;
2659 rtx handlers = NULL;
2660 int region_number;
2662 if (JUMP_P (insn)
2663 && GET_CODE (PATTERN (insn)) == RESX)
2665 region_number = XINT (PATTERN (insn), 0);
2666 is_resx = true;
2668 else
2670 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2671 if (!note || INTVAL (XEXP (note, 0)) <= 0)
2672 return NULL;
2673 region_number = INTVAL (XEXP (note, 0));
2676 foreach_reachable_handler (region_number, is_resx,
2677 (cfun->eh->built_landing_pads
2678 ? arh_to_landing_pad
2679 : arh_to_label),
2680 &handlers);
2682 return handlers;
2685 /* Determine if the given INSN can throw an exception that is caught
2686 within the function. */
2688 bool
2689 can_throw_internal_1 (int region_number, bool is_resx)
2691 struct eh_region *region;
2692 tree type_thrown;
2694 region = VEC_index (eh_region, cfun->eh->region_array, region_number);
2696 type_thrown = NULL_TREE;
2697 if (is_resx)
2698 region = region->outer;
2699 else if (region->type == ERT_THROW)
2701 type_thrown = region->u.throw.type;
2702 region = region->outer;
2705 /* If this exception is ignored by each and every containing region,
2706 then control passes straight out. The runtime may handle some
2707 regions, which also do not require processing internally. */
2708 for (; region; region = region->outer)
2710 enum reachable_code how = reachable_next_level (region, type_thrown, 0);
2711 if (how == RNL_BLOCKED)
2712 return false;
2713 if (how != RNL_NOT_CAUGHT)
2714 return true;
2717 return false;
2720 bool
2721 can_throw_internal (rtx insn)
2723 rtx note;
2725 if (! INSN_P (insn))
2726 return false;
2728 if (JUMP_P (insn)
2729 && GET_CODE (PATTERN (insn)) == RESX
2730 && XINT (PATTERN (insn), 0) > 0)
2731 return can_throw_internal_1 (XINT (PATTERN (insn), 0), true);
2733 if (NONJUMP_INSN_P (insn)
2734 && GET_CODE (PATTERN (insn)) == SEQUENCE)
2735 insn = XVECEXP (PATTERN (insn), 0, 0);
2737 /* Every insn that might throw has an EH_REGION note. */
2738 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2739 if (!note || INTVAL (XEXP (note, 0)) <= 0)
2740 return false;
2742 return can_throw_internal_1 (INTVAL (XEXP (note, 0)), false);
2745 /* Determine if the given INSN can throw an exception that is
2746 visible outside the function. */
2748 bool
2749 can_throw_external_1 (int region_number, bool is_resx)
2751 struct eh_region *region;
2752 tree type_thrown;
2754 region = VEC_index (eh_region, cfun->eh->region_array, region_number);
2756 type_thrown = NULL_TREE;
2757 if (is_resx)
2758 region = region->outer;
2759 else if (region->type == ERT_THROW)
2761 type_thrown = region->u.throw.type;
2762 region = region->outer;
2765 /* If the exception is caught or blocked by any containing region,
2766 then it is not seen by any calling function. */
2767 for (; region ; region = region->outer)
2768 if (reachable_next_level (region, type_thrown, NULL) >= RNL_CAUGHT)
2769 return false;
2771 return true;
2774 bool
2775 can_throw_external (rtx insn)
2777 rtx note;
2779 if (! INSN_P (insn))
2780 return false;
2782 if (JUMP_P (insn)
2783 && GET_CODE (PATTERN (insn)) == RESX
2784 && XINT (PATTERN (insn), 0) > 0)
2785 return can_throw_external_1 (XINT (PATTERN (insn), 0), true);
2787 if (NONJUMP_INSN_P (insn)
2788 && GET_CODE (PATTERN (insn)) == SEQUENCE)
2789 insn = XVECEXP (PATTERN (insn), 0, 0);
2791 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2792 if (!note)
2794 /* Calls (and trapping insns) without notes are outside any
2795 exception handling region in this function. We have to
2796 assume it might throw. Given that the front end and middle
2797 ends mark known NOTHROW functions, this isn't so wildly
2798 inaccurate. */
2799 return (CALL_P (insn)
2800 || (flag_non_call_exceptions
2801 && may_trap_p (PATTERN (insn))));
2803 if (INTVAL (XEXP (note, 0)) <= 0)
2804 return false;
2806 return can_throw_external_1 (INTVAL (XEXP (note, 0)), false);
2809 /* Set TREE_NOTHROW and cfun->all_throwers_are_sibcalls. */
2811 unsigned int
2812 set_nothrow_function_flags (void)
2814 rtx insn;
2816 TREE_NOTHROW (current_function_decl) = 1;
2818 /* Assume cfun->all_throwers_are_sibcalls until we encounter
2819 something that can throw an exception. We specifically exempt
2820 CALL_INSNs that are SIBLING_CALL_P, as these are really jumps,
2821 and can't throw. Most CALL_INSNs are not SIBLING_CALL_P, so this
2822 is optimistic. */
2824 cfun->all_throwers_are_sibcalls = 1;
2826 if (! flag_exceptions)
2827 return 0;
2829 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2830 if (can_throw_external (insn))
2832 TREE_NOTHROW (current_function_decl) = 0;
2834 if (!CALL_P (insn) || !SIBLING_CALL_P (insn))
2836 cfun->all_throwers_are_sibcalls = 0;
2837 return 0;
2841 for (insn = current_function_epilogue_delay_list; insn;
2842 insn = XEXP (insn, 1))
2843 if (can_throw_external (insn))
2845 TREE_NOTHROW (current_function_decl) = 0;
2847 if (!CALL_P (insn) || !SIBLING_CALL_P (insn))
2849 cfun->all_throwers_are_sibcalls = 0;
2850 return 0;
2853 return 0;
2856 struct tree_opt_pass pass_set_nothrow_function_flags =
2858 NULL, /* name */
2859 NULL, /* gate */
2860 set_nothrow_function_flags, /* execute */
2861 NULL, /* sub */
2862 NULL, /* next */
2863 0, /* static_pass_number */
2864 0, /* tv_id */
2865 0, /* properties_required */
2866 0, /* properties_provided */
2867 0, /* properties_destroyed */
2868 0, /* todo_flags_start */
2869 0, /* todo_flags_finish */
2870 0 /* letter */
2874 /* Various hooks for unwind library. */
2876 /* Do any necessary initialization to access arbitrary stack frames.
2877 On the SPARC, this means flushing the register windows. */
2879 void
2880 expand_builtin_unwind_init (void)
2882 /* Set this so all the registers get saved in our frame; we need to be
2883 able to copy the saved values for any registers from frames we unwind. */
2884 current_function_has_nonlocal_label = 1;
2886 #ifdef SETUP_FRAME_ADDRESSES
2887 SETUP_FRAME_ADDRESSES ();
2888 #endif
2892 expand_builtin_eh_return_data_regno (tree arglist)
2894 tree which = TREE_VALUE (arglist);
2895 unsigned HOST_WIDE_INT iwhich;
2897 if (TREE_CODE (which) != INTEGER_CST)
2899 error ("argument of %<__builtin_eh_return_regno%> must be constant");
2900 return constm1_rtx;
2903 iwhich = tree_low_cst (which, 1);
2904 iwhich = EH_RETURN_DATA_REGNO (iwhich);
2905 if (iwhich == INVALID_REGNUM)
2906 return constm1_rtx;
2908 #ifdef DWARF_FRAME_REGNUM
2909 iwhich = DWARF_FRAME_REGNUM (iwhich);
2910 #else
2911 iwhich = DBX_REGISTER_NUMBER (iwhich);
2912 #endif
2914 return GEN_INT (iwhich);
2917 /* Given a value extracted from the return address register or stack slot,
2918 return the actual address encoded in that value. */
2921 expand_builtin_extract_return_addr (tree addr_tree)
2923 rtx addr = expand_expr (addr_tree, NULL_RTX, Pmode, 0);
2925 if (GET_MODE (addr) != Pmode
2926 && GET_MODE (addr) != VOIDmode)
2928 #ifdef POINTERS_EXTEND_UNSIGNED
2929 addr = convert_memory_address (Pmode, addr);
2930 #else
2931 addr = convert_to_mode (Pmode, addr, 0);
2932 #endif
2935 /* First mask out any unwanted bits. */
2936 #ifdef MASK_RETURN_ADDR
2937 expand_and (Pmode, addr, MASK_RETURN_ADDR, addr);
2938 #endif
2940 /* Then adjust to find the real return address. */
2941 #if defined (RETURN_ADDR_OFFSET)
2942 addr = plus_constant (addr, RETURN_ADDR_OFFSET);
2943 #endif
2945 return addr;
2948 /* Given an actual address in addr_tree, do any necessary encoding
2949 and return the value to be stored in the return address register or
2950 stack slot so the epilogue will return to that address. */
2953 expand_builtin_frob_return_addr (tree addr_tree)
2955 rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, 0);
2957 addr = convert_memory_address (Pmode, addr);
2959 #ifdef RETURN_ADDR_OFFSET
2960 addr = force_reg (Pmode, addr);
2961 addr = plus_constant (addr, -RETURN_ADDR_OFFSET);
2962 #endif
2964 return addr;
2967 /* Set up the epilogue with the magic bits we'll need to return to the
2968 exception handler. */
2970 void
2971 expand_builtin_eh_return (tree stackadj_tree ATTRIBUTE_UNUSED,
2972 tree handler_tree)
2974 rtx tmp;
2976 #ifdef EH_RETURN_STACKADJ_RTX
2977 tmp = expand_expr (stackadj_tree, cfun->eh->ehr_stackadj, VOIDmode, 0);
2978 tmp = convert_memory_address (Pmode, tmp);
2979 if (!cfun->eh->ehr_stackadj)
2980 cfun->eh->ehr_stackadj = copy_to_reg (tmp);
2981 else if (tmp != cfun->eh->ehr_stackadj)
2982 emit_move_insn (cfun->eh->ehr_stackadj, tmp);
2983 #endif
2985 tmp = expand_expr (handler_tree, cfun->eh->ehr_handler, VOIDmode, 0);
2986 tmp = convert_memory_address (Pmode, tmp);
2987 if (!cfun->eh->ehr_handler)
2988 cfun->eh->ehr_handler = copy_to_reg (tmp);
2989 else if (tmp != cfun->eh->ehr_handler)
2990 emit_move_insn (cfun->eh->ehr_handler, tmp);
2992 if (!cfun->eh->ehr_label)
2993 cfun->eh->ehr_label = gen_label_rtx ();
2994 emit_jump (cfun->eh->ehr_label);
2997 void
2998 expand_eh_return (void)
3000 rtx around_label;
3002 if (! cfun->eh->ehr_label)
3003 return;
3005 current_function_calls_eh_return = 1;
3007 #ifdef EH_RETURN_STACKADJ_RTX
3008 emit_move_insn (EH_RETURN_STACKADJ_RTX, const0_rtx);
3009 #endif
3011 around_label = gen_label_rtx ();
3012 emit_jump (around_label);
3014 emit_label (cfun->eh->ehr_label);
3015 clobber_return_register ();
3017 #ifdef EH_RETURN_STACKADJ_RTX
3018 emit_move_insn (EH_RETURN_STACKADJ_RTX, cfun->eh->ehr_stackadj);
3019 #endif
3021 #ifdef HAVE_eh_return
3022 if (HAVE_eh_return)
3023 emit_insn (gen_eh_return (cfun->eh->ehr_handler));
3024 else
3025 #endif
3027 #ifdef EH_RETURN_HANDLER_RTX
3028 emit_move_insn (EH_RETURN_HANDLER_RTX, cfun->eh->ehr_handler);
3029 #else
3030 error ("__builtin_eh_return not supported on this target");
3031 #endif
3034 emit_label (around_label);
3037 /* Convert a ptr_mode address ADDR_TREE to a Pmode address controlled by
3038 POINTERS_EXTEND_UNSIGNED and return it. */
3041 expand_builtin_extend_pointer (tree addr_tree)
3043 rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, 0);
3044 int extend;
3046 #ifdef POINTERS_EXTEND_UNSIGNED
3047 extend = POINTERS_EXTEND_UNSIGNED;
3048 #else
3049 /* The previous EH code did an unsigned extend by default, so we do this also
3050 for consistency. */
3051 extend = 1;
3052 #endif
3054 return convert_modes (word_mode, ptr_mode, addr, extend);
3057 /* In the following functions, we represent entries in the action table
3058 as 1-based indices. Special cases are:
3060 0: null action record, non-null landing pad; implies cleanups
3061 -1: null action record, null landing pad; implies no action
3062 -2: no call-site entry; implies must_not_throw
3063 -3: we have yet to process outer regions
3065 Further, no special cases apply to the "next" field of the record.
3066 For next, 0 means end of list. */
3068 struct action_record
3070 int offset;
3071 int filter;
3072 int next;
3075 static int
3076 action_record_eq (const void *pentry, const void *pdata)
3078 const struct action_record *entry = (const struct action_record *) pentry;
3079 const struct action_record *data = (const struct action_record *) pdata;
3080 return entry->filter == data->filter && entry->next == data->next;
3083 static hashval_t
3084 action_record_hash (const void *pentry)
3086 const struct action_record *entry = (const struct action_record *) pentry;
3087 return entry->next * 1009 + entry->filter;
3090 static int
3091 add_action_record (htab_t ar_hash, int filter, int next)
3093 struct action_record **slot, *new, tmp;
3095 tmp.filter = filter;
3096 tmp.next = next;
3097 slot = (struct action_record **) htab_find_slot (ar_hash, &tmp, INSERT);
3099 if ((new = *slot) == NULL)
3101 new = xmalloc (sizeof (*new));
3102 new->offset = VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data) + 1;
3103 new->filter = filter;
3104 new->next = next;
3105 *slot = new;
3107 /* The filter value goes in untouched. The link to the next
3108 record is a "self-relative" byte offset, or zero to indicate
3109 that there is no next record. So convert the absolute 1 based
3110 indices we've been carrying around into a displacement. */
3112 push_sleb128 (&cfun->eh->action_record_data, filter);
3113 if (next)
3114 next -= VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data) + 1;
3115 push_sleb128 (&cfun->eh->action_record_data, next);
3118 return new->offset;
3121 static int
3122 collect_one_action_chain (htab_t ar_hash, struct eh_region *region)
3124 struct eh_region *c;
3125 int next;
3127 /* If we've reached the top of the region chain, then we have
3128 no actions, and require no landing pad. */
3129 if (region == NULL)
3130 return -1;
3132 switch (region->type)
3134 case ERT_CLEANUP:
3135 /* A cleanup adds a zero filter to the beginning of the chain, but
3136 there are special cases to look out for. If there are *only*
3137 cleanups along a path, then it compresses to a zero action.
3138 Further, if there are multiple cleanups along a path, we only
3139 need to represent one of them, as that is enough to trigger
3140 entry to the landing pad at runtime. */
3141 next = collect_one_action_chain (ar_hash, region->outer);
3142 if (next <= 0)
3143 return 0;
3144 for (c = region->outer; c ; c = c->outer)
3145 if (c->type == ERT_CLEANUP)
3146 return next;
3147 return add_action_record (ar_hash, 0, next);
3149 case ERT_TRY:
3150 /* Process the associated catch regions in reverse order.
3151 If there's a catch-all handler, then we don't need to
3152 search outer regions. Use a magic -3 value to record
3153 that we haven't done the outer search. */
3154 next = -3;
3155 for (c = region->u.try.last_catch; c ; c = c->u.catch.prev_catch)
3157 if (c->u.catch.type_list == NULL)
3159 /* Retrieve the filter from the head of the filter list
3160 where we have stored it (see assign_filter_values). */
3161 int filter
3162 = TREE_INT_CST_LOW (TREE_VALUE (c->u.catch.filter_list));
3164 next = add_action_record (ar_hash, filter, 0);
3166 else
3168 /* Once the outer search is done, trigger an action record for
3169 each filter we have. */
3170 tree flt_node;
3172 if (next == -3)
3174 next = collect_one_action_chain (ar_hash, region->outer);
3176 /* If there is no next action, terminate the chain. */
3177 if (next == -1)
3178 next = 0;
3179 /* If all outer actions are cleanups or must_not_throw,
3180 we'll have no action record for it, since we had wanted
3181 to encode these states in the call-site record directly.
3182 Add a cleanup action to the chain to catch these. */
3183 else if (next <= 0)
3184 next = add_action_record (ar_hash, 0, 0);
3187 flt_node = c->u.catch.filter_list;
3188 for (; flt_node; flt_node = TREE_CHAIN (flt_node))
3190 int filter = TREE_INT_CST_LOW (TREE_VALUE (flt_node));
3191 next = add_action_record (ar_hash, filter, next);
3195 return next;
3197 case ERT_ALLOWED_EXCEPTIONS:
3198 /* An exception specification adds its filter to the
3199 beginning of the chain. */
3200 next = collect_one_action_chain (ar_hash, region->outer);
3202 /* If there is no next action, terminate the chain. */
3203 if (next == -1)
3204 next = 0;
3205 /* If all outer actions are cleanups or must_not_throw,
3206 we'll have no action record for it, since we had wanted
3207 to encode these states in the call-site record directly.
3208 Add a cleanup action to the chain to catch these. */
3209 else if (next <= 0)
3210 next = add_action_record (ar_hash, 0, 0);
3212 return add_action_record (ar_hash, region->u.allowed.filter, next);
3214 case ERT_MUST_NOT_THROW:
3215 /* A must-not-throw region with no inner handlers or cleanups
3216 requires no call-site entry. Note that this differs from
3217 the no handler or cleanup case in that we do require an lsda
3218 to be generated. Return a magic -2 value to record this. */
3219 return -2;
3221 case ERT_CATCH:
3222 case ERT_THROW:
3223 /* CATCH regions are handled in TRY above. THROW regions are
3224 for optimization information only and produce no output. */
3225 return collect_one_action_chain (ar_hash, region->outer);
3227 default:
3228 gcc_unreachable ();
3232 static int
3233 add_call_site (rtx landing_pad, int action)
3235 struct call_site_record *data = cfun->eh->call_site_data;
3236 int used = cfun->eh->call_site_data_used;
3237 int size = cfun->eh->call_site_data_size;
3239 if (used >= size)
3241 size = (size ? size * 2 : 64);
3242 data = ggc_realloc (data, sizeof (*data) * size);
3243 cfun->eh->call_site_data = data;
3244 cfun->eh->call_site_data_size = size;
3247 data[used].landing_pad = landing_pad;
3248 data[used].action = action;
3250 cfun->eh->call_site_data_used = used + 1;
3252 return used + call_site_base;
3255 /* Turn REG_EH_REGION notes back into NOTE_INSN_EH_REGION notes.
3256 The new note numbers will not refer to region numbers, but
3257 instead to call site entries. */
3259 unsigned int
3260 convert_to_eh_region_ranges (void)
3262 rtx insn, iter, note;
3263 htab_t ar_hash;
3264 int last_action = -3;
3265 rtx last_action_insn = NULL_RTX;
3266 rtx last_landing_pad = NULL_RTX;
3267 rtx first_no_action_insn = NULL_RTX;
3268 int call_site = 0;
3270 if (USING_SJLJ_EXCEPTIONS || cfun->eh->region_tree == NULL)
3271 return 0;
3273 VARRAY_UCHAR_INIT (cfun->eh->action_record_data, 64, "action_record_data");
3275 ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
3277 for (iter = get_insns (); iter ; iter = NEXT_INSN (iter))
3278 if (INSN_P (iter))
3280 struct eh_region *region;
3281 int this_action;
3282 rtx this_landing_pad;
3284 insn = iter;
3285 if (NONJUMP_INSN_P (insn)
3286 && GET_CODE (PATTERN (insn)) == SEQUENCE)
3287 insn = XVECEXP (PATTERN (insn), 0, 0);
3289 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3290 if (!note)
3292 if (! (CALL_P (insn)
3293 || (flag_non_call_exceptions
3294 && may_trap_p (PATTERN (insn)))))
3295 continue;
3296 this_action = -1;
3297 region = NULL;
3299 else
3301 if (INTVAL (XEXP (note, 0)) <= 0)
3302 continue;
3303 region = VEC_index (eh_region, cfun->eh->region_array, INTVAL (XEXP (note, 0)));
3304 this_action = collect_one_action_chain (ar_hash, region);
3307 /* Existence of catch handlers, or must-not-throw regions
3308 implies that an lsda is needed (even if empty). */
3309 if (this_action != -1)
3310 cfun->uses_eh_lsda = 1;
3312 /* Delay creation of region notes for no-action regions
3313 until we're sure that an lsda will be required. */
3314 else if (last_action == -3)
3316 first_no_action_insn = iter;
3317 last_action = -1;
3320 /* Cleanups and handlers may share action chains but not
3321 landing pads. Collect the landing pad for this region. */
3322 if (this_action >= 0)
3324 struct eh_region *o;
3325 for (o = region; ! o->landing_pad ; o = o->outer)
3326 continue;
3327 this_landing_pad = o->landing_pad;
3329 else
3330 this_landing_pad = NULL_RTX;
3332 /* Differing actions or landing pads implies a change in call-site
3333 info, which implies some EH_REGION note should be emitted. */
3334 if (last_action != this_action
3335 || last_landing_pad != this_landing_pad)
3337 /* If we'd not seen a previous action (-3) or the previous
3338 action was must-not-throw (-2), then we do not need an
3339 end note. */
3340 if (last_action >= -1)
3342 /* If we delayed the creation of the begin, do it now. */
3343 if (first_no_action_insn)
3345 call_site = add_call_site (NULL_RTX, 0);
3346 note = emit_note_before (NOTE_INSN_EH_REGION_BEG,
3347 first_no_action_insn);
3348 NOTE_EH_HANDLER (note) = call_site;
3349 first_no_action_insn = NULL_RTX;
3352 note = emit_note_after (NOTE_INSN_EH_REGION_END,
3353 last_action_insn);
3354 NOTE_EH_HANDLER (note) = call_site;
3357 /* If the new action is must-not-throw, then no region notes
3358 are created. */
3359 if (this_action >= -1)
3361 call_site = add_call_site (this_landing_pad,
3362 this_action < 0 ? 0 : this_action);
3363 note = emit_note_before (NOTE_INSN_EH_REGION_BEG, iter);
3364 NOTE_EH_HANDLER (note) = call_site;
3367 last_action = this_action;
3368 last_landing_pad = this_landing_pad;
3370 last_action_insn = iter;
3373 if (last_action >= -1 && ! first_no_action_insn)
3375 note = emit_note_after (NOTE_INSN_EH_REGION_END, last_action_insn);
3376 NOTE_EH_HANDLER (note) = call_site;
3379 htab_delete (ar_hash);
3380 return 0;
3383 struct tree_opt_pass pass_convert_to_eh_region_ranges =
3385 "eh-ranges", /* name */
3386 NULL, /* gate */
3387 convert_to_eh_region_ranges, /* execute */
3388 NULL, /* sub */
3389 NULL, /* next */
3390 0, /* static_pass_number */
3391 0, /* tv_id */
3392 0, /* properties_required */
3393 0, /* properties_provided */
3394 0, /* properties_destroyed */
3395 0, /* todo_flags_start */
3396 TODO_dump_func, /* todo_flags_finish */
3397 0 /* letter */
3401 static void
3402 push_uleb128 (varray_type *data_area, unsigned int value)
3406 unsigned char byte = value & 0x7f;
3407 value >>= 7;
3408 if (value)
3409 byte |= 0x80;
3410 VARRAY_PUSH_UCHAR (*data_area, byte);
3412 while (value);
3415 static void
3416 push_sleb128 (varray_type *data_area, int value)
3418 unsigned char byte;
3419 int more;
3423 byte = value & 0x7f;
3424 value >>= 7;
3425 more = ! ((value == 0 && (byte & 0x40) == 0)
3426 || (value == -1 && (byte & 0x40) != 0));
3427 if (more)
3428 byte |= 0x80;
3429 VARRAY_PUSH_UCHAR (*data_area, byte);
3431 while (more);
3435 #ifndef HAVE_AS_LEB128
3436 static int
3437 dw2_size_of_call_site_table (void)
3439 int n = cfun->eh->call_site_data_used;
3440 int size = n * (4 + 4 + 4);
3441 int i;
3443 for (i = 0; i < n; ++i)
3445 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3446 size += size_of_uleb128 (cs->action);
3449 return size;
3452 static int
3453 sjlj_size_of_call_site_table (void)
3455 int n = cfun->eh->call_site_data_used;
3456 int size = 0;
3457 int i;
3459 for (i = 0; i < n; ++i)
3461 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3462 size += size_of_uleb128 (INTVAL (cs->landing_pad));
3463 size += size_of_uleb128 (cs->action);
3466 return size;
3468 #endif
3470 static void
3471 dw2_output_call_site_table (void)
3473 int n = cfun->eh->call_site_data_used;
3474 int i;
3476 for (i = 0; i < n; ++i)
3478 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3479 char reg_start_lab[32];
3480 char reg_end_lab[32];
3481 char landing_pad_lab[32];
3483 ASM_GENERATE_INTERNAL_LABEL (reg_start_lab, "LEHB", call_site_base + i);
3484 ASM_GENERATE_INTERNAL_LABEL (reg_end_lab, "LEHE", call_site_base + i);
3486 if (cs->landing_pad)
3487 ASM_GENERATE_INTERNAL_LABEL (landing_pad_lab, "L",
3488 CODE_LABEL_NUMBER (cs->landing_pad));
3490 /* ??? Perhaps use insn length scaling if the assembler supports
3491 generic arithmetic. */
3492 /* ??? Perhaps use attr_length to choose data1 or data2 instead of
3493 data4 if the function is small enough. */
3494 #ifdef HAVE_AS_LEB128
3495 dw2_asm_output_delta_uleb128 (reg_start_lab,
3496 current_function_func_begin_label,
3497 "region %d start", i);
3498 dw2_asm_output_delta_uleb128 (reg_end_lab, reg_start_lab,
3499 "length");
3500 if (cs->landing_pad)
3501 dw2_asm_output_delta_uleb128 (landing_pad_lab,
3502 current_function_func_begin_label,
3503 "landing pad");
3504 else
3505 dw2_asm_output_data_uleb128 (0, "landing pad");
3506 #else
3507 dw2_asm_output_delta (4, reg_start_lab,
3508 current_function_func_begin_label,
3509 "region %d start", i);
3510 dw2_asm_output_delta (4, reg_end_lab, reg_start_lab, "length");
3511 if (cs->landing_pad)
3512 dw2_asm_output_delta (4, landing_pad_lab,
3513 current_function_func_begin_label,
3514 "landing pad");
3515 else
3516 dw2_asm_output_data (4, 0, "landing pad");
3517 #endif
3518 dw2_asm_output_data_uleb128 (cs->action, "action");
3521 call_site_base += n;
3524 static void
3525 sjlj_output_call_site_table (void)
3527 int n = cfun->eh->call_site_data_used;
3528 int i;
3530 for (i = 0; i < n; ++i)
3532 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3534 dw2_asm_output_data_uleb128 (INTVAL (cs->landing_pad),
3535 "region %d landing pad", i);
3536 dw2_asm_output_data_uleb128 (cs->action, "action");
3539 call_site_base += n;
3542 #ifndef TARGET_UNWIND_INFO
3543 /* Switch to the section that should be used for exception tables. */
3545 static void
3546 switch_to_exception_section (void)
3548 if (exception_section == 0)
3550 if (targetm.have_named_sections)
3552 int flags;
3554 if (EH_TABLES_CAN_BE_READ_ONLY)
3556 int tt_format =
3557 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3558 flags = ((! flag_pic
3559 || ((tt_format & 0x70) != DW_EH_PE_absptr
3560 && (tt_format & 0x70) != DW_EH_PE_aligned))
3561 ? 0 : SECTION_WRITE);
3563 else
3564 flags = SECTION_WRITE;
3565 exception_section = get_section (".gcc_except_table", flags, NULL);
3567 else
3568 exception_section = flag_pic ? data_section : readonly_data_section;
3570 switch_to_section (exception_section);
3572 #endif
3575 /* Output a reference from an exception table to the type_info object TYPE.
3576 TT_FORMAT and TT_FORMAT_SIZE describe the DWARF encoding method used for
3577 the value. */
3579 static void
3580 output_ttype (tree type, int tt_format, int tt_format_size)
3582 rtx value;
3583 bool public = true;
3585 if (type == NULL_TREE)
3586 value = const0_rtx;
3587 else
3589 struct cgraph_varpool_node *node;
3591 type = lookup_type_for_runtime (type);
3592 value = expand_expr (type, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
3594 /* Let cgraph know that the rtti decl is used. Not all of the
3595 paths below go through assemble_integer, which would take
3596 care of this for us. */
3597 STRIP_NOPS (type);
3598 if (TREE_CODE (type) == ADDR_EXPR)
3600 type = TREE_OPERAND (type, 0);
3601 if (TREE_CODE (type) == VAR_DECL)
3603 node = cgraph_varpool_node (type);
3604 if (node)
3605 cgraph_varpool_mark_needed_node (node);
3606 public = TREE_PUBLIC (type);
3609 else
3610 gcc_assert (TREE_CODE (type) == INTEGER_CST);
3613 /* Allow the target to override the type table entry format. */
3614 if (targetm.asm_out.ttype (value))
3615 return;
3617 if (tt_format == DW_EH_PE_absptr || tt_format == DW_EH_PE_aligned)
3618 assemble_integer (value, tt_format_size,
3619 tt_format_size * BITS_PER_UNIT, 1);
3620 else
3621 dw2_asm_output_encoded_addr_rtx (tt_format, value, public, NULL);
3624 void
3625 output_function_exception_table (void)
3627 int tt_format, cs_format, lp_format, i, n;
3628 #ifdef HAVE_AS_LEB128
3629 char ttype_label[32];
3630 char cs_after_size_label[32];
3631 char cs_end_label[32];
3632 #else
3633 int call_site_len;
3634 #endif
3635 int have_tt_data;
3636 int tt_format_size = 0;
3638 if (eh_personality_libfunc)
3639 assemble_external_libcall (eh_personality_libfunc);
3641 /* Not all functions need anything. */
3642 if (! cfun->uses_eh_lsda)
3643 return;
3645 #ifdef TARGET_UNWIND_INFO
3646 /* TODO: Move this into target file. */
3647 fputs ("\t.personality\t", asm_out_file);
3648 output_addr_const (asm_out_file, eh_personality_libfunc);
3649 fputs ("\n\t.handlerdata\n", asm_out_file);
3650 /* Note that varasm still thinks we're in the function's code section.
3651 The ".endp" directive that will immediately follow will take us back. */
3652 #else
3653 switch_to_exception_section ();
3654 #endif
3656 /* If the target wants a label to begin the table, emit it here. */
3657 targetm.asm_out.except_table_label (asm_out_file);
3659 have_tt_data = (VEC_length (tree, cfun->eh->ttype_data) > 0
3660 || VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data) > 0);
3662 /* Indicate the format of the @TType entries. */
3663 if (! have_tt_data)
3664 tt_format = DW_EH_PE_omit;
3665 else
3667 tt_format = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3668 #ifdef HAVE_AS_LEB128
3669 ASM_GENERATE_INTERNAL_LABEL (ttype_label, "LLSDATT",
3670 current_function_funcdef_no);
3671 #endif
3672 tt_format_size = size_of_encoded_value (tt_format);
3674 assemble_align (tt_format_size * BITS_PER_UNIT);
3677 targetm.asm_out.internal_label (asm_out_file, "LLSDA",
3678 current_function_funcdef_no);
3680 /* The LSDA header. */
3682 /* Indicate the format of the landing pad start pointer. An omitted
3683 field implies @LPStart == @Start. */
3684 /* Currently we always put @LPStart == @Start. This field would
3685 be most useful in moving the landing pads completely out of
3686 line to another section, but it could also be used to minimize
3687 the size of uleb128 landing pad offsets. */
3688 lp_format = DW_EH_PE_omit;
3689 dw2_asm_output_data (1, lp_format, "@LPStart format (%s)",
3690 eh_data_format_name (lp_format));
3692 /* @LPStart pointer would go here. */
3694 dw2_asm_output_data (1, tt_format, "@TType format (%s)",
3695 eh_data_format_name (tt_format));
3697 #ifndef HAVE_AS_LEB128
3698 if (USING_SJLJ_EXCEPTIONS)
3699 call_site_len = sjlj_size_of_call_site_table ();
3700 else
3701 call_site_len = dw2_size_of_call_site_table ();
3702 #endif
3704 /* A pc-relative 4-byte displacement to the @TType data. */
3705 if (have_tt_data)
3707 #ifdef HAVE_AS_LEB128
3708 char ttype_after_disp_label[32];
3709 ASM_GENERATE_INTERNAL_LABEL (ttype_after_disp_label, "LLSDATTD",
3710 current_function_funcdef_no);
3711 dw2_asm_output_delta_uleb128 (ttype_label, ttype_after_disp_label,
3712 "@TType base offset");
3713 ASM_OUTPUT_LABEL (asm_out_file, ttype_after_disp_label);
3714 #else
3715 /* Ug. Alignment queers things. */
3716 unsigned int before_disp, after_disp, last_disp, disp;
3718 before_disp = 1 + 1;
3719 after_disp = (1 + size_of_uleb128 (call_site_len)
3720 + call_site_len
3721 + VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data)
3722 + (VEC_length (tree, cfun->eh->ttype_data)
3723 * tt_format_size));
3725 disp = after_disp;
3728 unsigned int disp_size, pad;
3730 last_disp = disp;
3731 disp_size = size_of_uleb128 (disp);
3732 pad = before_disp + disp_size + after_disp;
3733 if (pad % tt_format_size)
3734 pad = tt_format_size - (pad % tt_format_size);
3735 else
3736 pad = 0;
3737 disp = after_disp + pad;
3739 while (disp != last_disp);
3741 dw2_asm_output_data_uleb128 (disp, "@TType base offset");
3742 #endif
3745 /* Indicate the format of the call-site offsets. */
3746 #ifdef HAVE_AS_LEB128
3747 cs_format = DW_EH_PE_uleb128;
3748 #else
3749 cs_format = DW_EH_PE_udata4;
3750 #endif
3751 dw2_asm_output_data (1, cs_format, "call-site format (%s)",
3752 eh_data_format_name (cs_format));
3754 #ifdef HAVE_AS_LEB128
3755 ASM_GENERATE_INTERNAL_LABEL (cs_after_size_label, "LLSDACSB",
3756 current_function_funcdef_no);
3757 ASM_GENERATE_INTERNAL_LABEL (cs_end_label, "LLSDACSE",
3758 current_function_funcdef_no);
3759 dw2_asm_output_delta_uleb128 (cs_end_label, cs_after_size_label,
3760 "Call-site table length");
3761 ASM_OUTPUT_LABEL (asm_out_file, cs_after_size_label);
3762 if (USING_SJLJ_EXCEPTIONS)
3763 sjlj_output_call_site_table ();
3764 else
3765 dw2_output_call_site_table ();
3766 ASM_OUTPUT_LABEL (asm_out_file, cs_end_label);
3767 #else
3768 dw2_asm_output_data_uleb128 (call_site_len,"Call-site table length");
3769 if (USING_SJLJ_EXCEPTIONS)
3770 sjlj_output_call_site_table ();
3771 else
3772 dw2_output_call_site_table ();
3773 #endif
3775 /* ??? Decode and interpret the data for flag_debug_asm. */
3776 n = VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data);
3777 for (i = 0; i < n; ++i)
3778 dw2_asm_output_data (1, VARRAY_UCHAR (cfun->eh->action_record_data, i),
3779 (i ? NULL : "Action record table"));
3781 if (have_tt_data)
3782 assemble_align (tt_format_size * BITS_PER_UNIT);
3784 i = VEC_length (tree, cfun->eh->ttype_data);
3785 while (i-- > 0)
3787 tree type = VEC_index (tree, cfun->eh->ttype_data, i);
3788 output_ttype (type, tt_format, tt_format_size);
3791 #ifdef HAVE_AS_LEB128
3792 if (have_tt_data)
3793 ASM_OUTPUT_LABEL (asm_out_file, ttype_label);
3794 #endif
3796 /* ??? Decode and interpret the data for flag_debug_asm. */
3797 n = VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data);
3798 for (i = 0; i < n; ++i)
3800 if (targetm.arm_eabi_unwinder)
3802 tree type = VARRAY_TREE (cfun->eh->ehspec_data, i);
3803 output_ttype (type, tt_format, tt_format_size);
3805 else
3806 dw2_asm_output_data (1, VARRAY_UCHAR (cfun->eh->ehspec_data, i),
3807 (i ? NULL : "Exception specification table"));
3810 switch_to_section (current_function_section ());
3813 void
3814 set_eh_throw_stmt_table (struct function *fun, struct htab *table)
3816 fun->eh->throw_stmt_table = table;
3819 htab_t
3820 get_eh_throw_stmt_table (struct function *fun)
3822 return fun->eh->throw_stmt_table;
3825 /* Dump EH information to OUT. */
3826 void
3827 dump_eh_tree (FILE *out, struct function *fun)
3829 struct eh_region *i;
3830 int depth = 0;
3831 static const char * const type_name[] = {"unknown", "cleanup", "try", "catch",
3832 "allowed_exceptions", "must_not_throw",
3833 "throw"};
3835 i = fun->eh->region_tree;
3836 if (! i)
3837 return;
3839 fprintf (out, "Eh tree:\n");
3840 while (1)
3842 fprintf (out, " %*s %i %s", depth * 2, "",
3843 i->region_number, type_name [(int)i->type]);
3844 if (i->tree_label)
3846 fprintf (out, " tree_label:");
3847 print_generic_expr (out, i->tree_label, 0);
3849 fprintf (out, "\n");
3850 /* If there are sub-regions, process them. */
3851 if (i->inner)
3852 i = i->inner, depth++;
3853 /* If there are peers, process them. */
3854 else if (i->next_peer)
3855 i = i->next_peer;
3856 /* Otherwise, step back up the tree to the next peer. */
3857 else
3859 do {
3860 i = i->outer;
3861 depth--;
3862 if (i == NULL)
3863 return;
3864 } while (i->next_peer == NULL);
3865 i = i->next_peer;
3870 /* Verify some basic invariants on EH datastructures. Could be extended to
3871 catch more. */
3872 void
3873 verify_eh_tree (struct function *fun)
3875 struct eh_region *i, *outer = NULL;
3876 bool err = false;
3877 int nvisited = 0;
3878 int count = 0;
3879 int j;
3880 int depth = 0;
3882 i = fun->eh->region_tree;
3883 if (! i)
3884 return;
3885 for (j = fun->eh->last_region_number; j > 0; --j)
3886 if ((i = VEC_index (eh_region, cfun->eh->region_array, j)))
3888 count++;
3889 if (i->region_number != j)
3891 error ("region_array is corrupted for region %i", i->region_number);
3892 err = true;
3896 while (1)
3898 if (VEC_index (eh_region, cfun->eh->region_array, i->region_number) != i)
3900 error ("region_array is corrupted for region %i", i->region_number);
3901 err = true;
3903 if (i->outer != outer)
3905 error ("outer block of region %i is wrong", i->region_number);
3906 err = true;
3908 if (i->may_contain_throw && outer && !outer->may_contain_throw)
3910 error ("region %i may contain throw and is contained in region that may not",
3911 i->region_number);
3912 err = true;
3914 if (depth < 0)
3916 error ("negative nesting depth of region %i", i->region_number);
3917 err = true;
3919 nvisited ++;
3920 /* If there are sub-regions, process them. */
3921 if (i->inner)
3922 outer = i, i = i->inner, depth++;
3923 /* If there are peers, process them. */
3924 else if (i->next_peer)
3925 i = i->next_peer;
3926 /* Otherwise, step back up the tree to the next peer. */
3927 else
3929 do {
3930 i = i->outer;
3931 depth--;
3932 if (i == NULL)
3934 if (depth != -1)
3936 error ("tree list ends on depth %i", depth + 1);
3937 err = true;
3939 if (count != nvisited)
3941 error ("array does not match the region tree");
3942 err = true;
3944 if (err)
3946 dump_eh_tree (stderr, fun);
3947 internal_error ("verify_eh_tree failed");
3949 return;
3951 outer = i->outer;
3952 } while (i->next_peer == NULL);
3953 i = i->next_peer;
3958 /* Initialize unwind_resume_libfunc. */
3960 void
3961 default_init_unwind_resume_libfunc (void)
3963 /* The default c++ routines aren't actually c++ specific, so use those. */
3964 unwind_resume_libfunc =
3965 init_one_libfunc ( USING_SJLJ_EXCEPTIONS ? "_Unwind_SjLj_Resume"
3966 : "_Unwind_Resume");
3970 static bool
3971 gate_handle_eh (void)
3973 return doing_eh (0);
3976 /* Complete generation of exception handling code. */
3977 static unsigned int
3978 rest_of_handle_eh (void)
3980 cleanup_cfg (CLEANUP_NO_INSN_DEL);
3981 finish_eh_generation ();
3982 cleanup_cfg (CLEANUP_NO_INSN_DEL);
3983 return 0;
3986 struct tree_opt_pass pass_rtl_eh =
3988 "eh", /* name */
3989 gate_handle_eh, /* gate */
3990 rest_of_handle_eh, /* execute */
3991 NULL, /* sub */
3992 NULL, /* next */
3993 0, /* static_pass_number */
3994 TV_JUMP, /* tv_id */
3995 0, /* properties_required */
3996 0, /* properties_provided */
3997 0, /* properties_destroyed */
3998 0, /* todo_flags_start */
3999 TODO_dump_func, /* todo_flags_finish */
4000 'h' /* letter */
4003 #include "gt-except.h"