1 /* Implements exception handling.
2 Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
5 Contributed by Mike Stump <mrs@cygnus.com>.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
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. ] */
52 #include "coretypes.h"
60 #include "insn-config.h"
62 #include "integrate.h"
63 #include "hard-reg-set.h"
64 #include "basic-block.h"
66 #include "dwarf2asm.h"
67 #include "dwarf2out.h"
75 #include "langhooks.h"
77 #include "diagnostic.h"
78 #include "tree-pass.h"
81 /* Provide defaults for stuff that may not be defined when using
83 #ifndef EH_RETURN_DATA_REGNO
84 #define EH_RETURN_DATA_REGNO(N) INVALID_REGNUM
87 /* Protect cleanup actions with must-not-throw regions, with a call
88 to the given failure handler. */
89 gimple (*lang_protect_cleanup_actions) (void);
91 /* Return true if type A catches type B. */
92 int (*lang_eh_type_covers) (tree a, tree b);
94 /* Map a type to a runtime object to match type. */
95 tree (*lang_eh_runtime_type) (tree);
97 /* A hash table of label to region number. */
99 struct ehl_map_entry GTY(())
102 struct eh_region *region;
105 static GTY(()) int call_site_base;
106 static GTY ((param_is (union tree_node)))
107 htab_t type_to_runtime_map;
109 /* Describe the SjLj_Function_Context structure. */
110 static GTY(()) tree sjlj_fc_type_node;
111 static int sjlj_fc_call_site_ofs;
112 static int sjlj_fc_data_ofs;
113 static int sjlj_fc_personality_ofs;
114 static int sjlj_fc_lsda_ofs;
115 static int sjlj_fc_jbuf_ofs;
117 /* Describes one exception region. */
118 struct eh_region GTY(())
120 /* The immediately surrounding region. */
121 struct eh_region *outer;
123 /* The list of immediately contained regions. */
124 struct eh_region *inner;
125 struct eh_region *next_peer;
127 /* An identifier for this region. */
130 /* When a region is deleted, its parents inherit the REG_EH_REGION
131 numbers already assigned. */
134 /* Each region does exactly one thing. */
141 ERT_ALLOWED_EXCEPTIONS,
146 /* Holds the action to perform based on the preceding type. */
148 /* A list of catch blocks, a surrounding try block,
149 and the label for continuing after a catch. */
150 struct eh_region_u_try {
151 struct eh_region *eh_catch;
152 struct eh_region *last_catch;
153 } GTY ((tag ("ERT_TRY"))) eh_try;
155 /* The list through the catch handlers, the list of type objects
156 matched, and the list of associated filters. */
157 struct eh_region_u_catch {
158 struct eh_region *next_catch;
159 struct eh_region *prev_catch;
162 } GTY ((tag ("ERT_CATCH"))) eh_catch;
164 /* A tree_list of allowed types. */
165 struct eh_region_u_allowed {
168 } GTY ((tag ("ERT_ALLOWED_EXCEPTIONS"))) allowed;
170 /* The type given by a call to "throw foo();", or discovered
172 struct eh_region_u_throw {
174 } GTY ((tag ("ERT_THROW"))) eh_throw;
176 /* Retain the cleanup expression even after expansion so that
177 we can match up fixup regions. */
178 struct eh_region_u_cleanup {
179 struct eh_region *prev_try;
180 } GTY ((tag ("ERT_CLEANUP"))) cleanup;
181 } GTY ((desc ("%0.type"))) u;
183 /* Entry point for this region's handler before landing pads are built. */
187 /* Entry point for this region's handler from the runtime eh library. */
190 /* Entry point for this region's handler from an inner region. */
191 rtx post_landing_pad;
193 /* The RESX insn for handing off control to the next outermost handler,
197 /* True if something in this region may throw. */
198 unsigned may_contain_throw : 1;
201 typedef struct eh_region *eh_region;
203 struct call_site_record GTY(())
209 DEF_VEC_P(eh_region);
210 DEF_VEC_ALLOC_P(eh_region, gc);
211 DEF_VEC_ALLOC_P(eh_region, heap);
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;
221 int last_region_number;
223 htab_t GTY((param_is (struct throw_stmt_node))) throw_stmt_table;
226 static int t2r_eq (const void *, const void *);
227 static hashval_t t2r_hash (const void *);
228 static void add_type_for_runtime (tree);
229 static tree lookup_type_for_runtime (tree);
231 static int ttypes_filter_eq (const void *, const void *);
232 static hashval_t ttypes_filter_hash (const void *);
233 static int ehspec_filter_eq (const void *, const void *);
234 static hashval_t ehspec_filter_hash (const void *);
235 static int add_ttypes_entry (htab_t, tree);
236 static int add_ehspec_entry (htab_t, htab_t, tree);
237 static void assign_filter_values (void);
238 static void build_post_landing_pads (void);
239 static void connect_post_landing_pads (void);
240 static void dw2_build_landing_pads (void);
243 static bool sjlj_find_directly_reachable_regions (struct sjlj_lp_info *);
244 static void sjlj_assign_call_site_values (rtx, struct sjlj_lp_info *);
245 static void sjlj_mark_call_sites (struct sjlj_lp_info *);
246 static void sjlj_emit_function_enter (rtx);
247 static void sjlj_emit_function_exit (void);
248 static void sjlj_emit_dispatch_table (rtx, struct sjlj_lp_info *);
249 static void sjlj_build_landing_pads (void);
251 static hashval_t ehl_hash (const void *);
252 static int ehl_eq (const void *, const void *);
253 static void add_ehl_entry (rtx, struct eh_region *);
254 static void remove_exception_handler_label (rtx);
255 static void remove_eh_handler (struct eh_region *);
256 static void remove_eh_handler_and_replace (struct eh_region *,
258 static int for_each_eh_label_1 (void **, void *);
260 /* The return value of reachable_next_level. */
263 /* The given exception is not processed by the given region. */
265 /* The given exception may need processing by the given region. */
267 /* The given exception is completely processed by the given region. */
269 /* The given exception is completely processed by the runtime. */
273 struct reachable_info;
274 static enum reachable_code reachable_next_level (struct eh_region *, tree,
275 struct reachable_info *, bool);
277 static int action_record_eq (const void *, const void *);
278 static hashval_t action_record_hash (const void *);
279 static int add_action_record (htab_t, int, int);
280 static int collect_one_action_chain (htab_t, struct eh_region *);
281 static int add_call_site (rtx, int);
283 static void push_uleb128 (varray_type *, unsigned int);
284 static void push_sleb128 (varray_type *, int);
285 #ifndef HAVE_AS_LEB128
286 static int dw2_size_of_call_site_table (void);
287 static int sjlj_size_of_call_site_table (void);
289 static void dw2_output_call_site_table (void);
290 static void sjlj_output_call_site_table (void);
293 /* Routine to see if exception handling is turned on.
294 DO_WARN is nonzero if we want to inform the user that exception
295 handling is turned off.
297 This is used to ensure that -fexceptions has been specified if the
298 compiler tries to use any exception-specific functions. */
301 doing_eh (int do_warn)
303 if (! flag_exceptions)
305 static int warned = 0;
306 if (! warned && do_warn)
308 error ("exception handling disabled, use -fexceptions to enable");
320 if (! flag_exceptions)
323 type_to_runtime_map = htab_create_ggc (31, t2r_hash, t2r_eq, NULL);
325 /* Create the SjLj_Function_Context structure. This should match
326 the definition in unwind-sjlj.c. */
327 if (USING_SJLJ_EXCEPTIONS)
329 tree f_jbuf, f_per, f_lsda, f_prev, f_cs, f_data, tmp;
331 sjlj_fc_type_node = lang_hooks.types.make_type (RECORD_TYPE);
333 f_prev = build_decl (FIELD_DECL, get_identifier ("__prev"),
334 build_pointer_type (sjlj_fc_type_node));
335 DECL_FIELD_CONTEXT (f_prev) = sjlj_fc_type_node;
337 f_cs = build_decl (FIELD_DECL, get_identifier ("__call_site"),
339 DECL_FIELD_CONTEXT (f_cs) = sjlj_fc_type_node;
341 tmp = build_index_type (build_int_cst (NULL_TREE, 4 - 1));
342 tmp = build_array_type (lang_hooks.types.type_for_mode
343 (targetm.unwind_word_mode (), 1),
345 f_data = build_decl (FIELD_DECL, get_identifier ("__data"), tmp);
346 DECL_FIELD_CONTEXT (f_data) = sjlj_fc_type_node;
348 f_per = build_decl (FIELD_DECL, get_identifier ("__personality"),
350 DECL_FIELD_CONTEXT (f_per) = sjlj_fc_type_node;
352 f_lsda = build_decl (FIELD_DECL, get_identifier ("__lsda"),
354 DECL_FIELD_CONTEXT (f_lsda) = sjlj_fc_type_node;
356 #ifdef DONT_USE_BUILTIN_SETJMP
358 tmp = build_int_cst (NULL_TREE, JMP_BUF_SIZE - 1);
360 /* Should be large enough for most systems, if it is not,
361 JMP_BUF_SIZE should be defined with the proper value. It will
362 also tend to be larger than necessary for most systems, a more
363 optimal port will define JMP_BUF_SIZE. */
364 tmp = build_int_cst (NULL_TREE, FIRST_PSEUDO_REGISTER + 2 - 1);
367 /* builtin_setjmp takes a pointer to 5 words. */
368 tmp = build_int_cst (NULL_TREE, 5 * BITS_PER_WORD / POINTER_SIZE - 1);
370 tmp = build_index_type (tmp);
371 tmp = build_array_type (ptr_type_node, tmp);
372 f_jbuf = build_decl (FIELD_DECL, get_identifier ("__jbuf"), tmp);
373 #ifdef DONT_USE_BUILTIN_SETJMP
374 /* We don't know what the alignment requirements of the
375 runtime's jmp_buf has. Overestimate. */
376 DECL_ALIGN (f_jbuf) = BIGGEST_ALIGNMENT;
377 DECL_USER_ALIGN (f_jbuf) = 1;
379 DECL_FIELD_CONTEXT (f_jbuf) = sjlj_fc_type_node;
381 TYPE_FIELDS (sjlj_fc_type_node) = f_prev;
382 TREE_CHAIN (f_prev) = f_cs;
383 TREE_CHAIN (f_cs) = f_data;
384 TREE_CHAIN (f_data) = f_per;
385 TREE_CHAIN (f_per) = f_lsda;
386 TREE_CHAIN (f_lsda) = f_jbuf;
388 layout_type (sjlj_fc_type_node);
390 /* Cache the interesting field offsets so that we have
391 easy access from rtl. */
392 sjlj_fc_call_site_ofs
393 = (tree_low_cst (DECL_FIELD_OFFSET (f_cs), 1)
394 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_cs), 1) / BITS_PER_UNIT);
396 = (tree_low_cst (DECL_FIELD_OFFSET (f_data), 1)
397 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_data), 1) / BITS_PER_UNIT);
398 sjlj_fc_personality_ofs
399 = (tree_low_cst (DECL_FIELD_OFFSET (f_per), 1)
400 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_per), 1) / BITS_PER_UNIT);
402 = (tree_low_cst (DECL_FIELD_OFFSET (f_lsda), 1)
403 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_lsda), 1) / BITS_PER_UNIT);
405 = (tree_low_cst (DECL_FIELD_OFFSET (f_jbuf), 1)
406 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_jbuf), 1) / BITS_PER_UNIT);
411 init_eh_for_function (void)
413 cfun->eh = GGC_CNEW (struct eh_status);
416 /* Routines to generate the exception tree somewhat directly.
417 These are used from tree-eh.c when processing exception related
418 nodes during tree optimization. */
420 static struct eh_region *
421 gen_eh_region (enum eh_region_type type, struct eh_region *outer)
423 struct eh_region *new_eh;
425 #ifdef ENABLE_CHECKING
426 gcc_assert (doing_eh (0));
429 /* Insert a new blank region as a leaf in the tree. */
430 new_eh = GGC_CNEW (struct eh_region);
432 new_eh->outer = outer;
435 new_eh->next_peer = outer->inner;
436 outer->inner = new_eh;
440 new_eh->next_peer = cfun->eh->region_tree;
441 cfun->eh->region_tree = new_eh;
444 new_eh->region_number = ++cfun->eh->last_region_number;
450 gen_eh_region_cleanup (struct eh_region *outer, struct eh_region *prev_try)
452 struct eh_region *cleanup = gen_eh_region (ERT_CLEANUP, outer);
453 cleanup->u.cleanup.prev_try = prev_try;
458 gen_eh_region_try (struct eh_region *outer)
460 return gen_eh_region (ERT_TRY, outer);
464 gen_eh_region_catch (struct eh_region *t, tree type_or_list)
466 struct eh_region *c, *l;
467 tree type_list, type_node;
469 /* Ensure to always end up with a type list to normalize further
470 processing, then register each type against the runtime types map. */
471 type_list = type_or_list;
474 if (TREE_CODE (type_or_list) != TREE_LIST)
475 type_list = tree_cons (NULL_TREE, type_or_list, NULL_TREE);
477 type_node = type_list;
478 for (; type_node; type_node = TREE_CHAIN (type_node))
479 add_type_for_runtime (TREE_VALUE (type_node));
482 c = gen_eh_region (ERT_CATCH, t->outer);
483 c->u.eh_catch.type_list = type_list;
484 l = t->u.eh_try.last_catch;
485 c->u.eh_catch.prev_catch = l;
487 l->u.eh_catch.next_catch = c;
489 t->u.eh_try.eh_catch = c;
490 t->u.eh_try.last_catch = c;
496 gen_eh_region_allowed (struct eh_region *outer, tree allowed)
498 struct eh_region *region = gen_eh_region (ERT_ALLOWED_EXCEPTIONS, outer);
499 region->u.allowed.type_list = allowed;
501 for (; allowed ; allowed = TREE_CHAIN (allowed))
502 add_type_for_runtime (TREE_VALUE (allowed));
508 gen_eh_region_must_not_throw (struct eh_region *outer)
510 return gen_eh_region (ERT_MUST_NOT_THROW, outer);
514 get_eh_region_number (struct eh_region *region)
516 return region->region_number;
520 get_eh_region_may_contain_throw (struct eh_region *region)
522 return region->may_contain_throw;
526 get_eh_region_tree_label (struct eh_region *region)
528 return region->tree_label;
532 get_eh_region_no_tree_label (int region)
534 return VEC_index (eh_region, cfun->eh->region_array, region)->tree_label;
538 set_eh_region_tree_label (struct eh_region *region, tree lab)
540 region->tree_label = lab;
544 expand_resx_expr (tree exp)
546 int region_nr = TREE_INT_CST_LOW (TREE_OPERAND (exp, 0));
547 struct eh_region *reg = VEC_index (eh_region,
548 cfun->eh->region_array, region_nr);
550 gcc_assert (!reg->resume);
551 do_pending_stack_adjust ();
552 reg->resume = emit_jump_insn (gen_rtx_RESX (VOIDmode, region_nr));
556 /* Note that the current EH region (if any) may contain a throw, or a
557 call to a function which itself may contain a throw. */
560 note_eh_region_may_contain_throw (struct eh_region *region)
562 while (region && !region->may_contain_throw)
564 region->may_contain_throw = 1;
565 region = region->outer;
570 /* Return an rtl expression for a pointer to the exception object
574 get_exception_pointer (void)
576 if (! crtl->eh.exc_ptr)
577 crtl->eh.exc_ptr = gen_reg_rtx (ptr_mode);
578 return crtl->eh.exc_ptr;
581 /* Return an rtl expression for the exception dispatch filter
585 get_exception_filter (void)
587 if (! crtl->eh.filter)
588 crtl->eh.filter = gen_reg_rtx (targetm.eh_return_filter_mode ());
589 return crtl->eh.filter;
592 /* This section is for the exception handling specific optimization pass. */
594 /* Random access the exception region tree. */
597 collect_eh_region_array (void)
601 i = cfun->eh->region_tree;
605 VEC_safe_grow (eh_region, gc, cfun->eh->region_array,
606 cfun->eh->last_region_number + 1);
607 VEC_replace (eh_region, cfun->eh->region_array, 0, 0);
611 VEC_replace (eh_region, cfun->eh->region_array, i->region_number, i);
613 /* If there are sub-regions, process them. */
616 /* If there are peers, process them. */
617 else if (i->next_peer)
619 /* Otherwise, step back up the tree to the next peer. */
626 } while (i->next_peer == NULL);
632 /* R is MUST_NOT_THROW region that is not reachable via local
633 RESX instructions. It still must be kept in the tree in case runtime
634 can unwind through it, or we will eliminate out terminate call
635 runtime would do otherwise. Return TRUE if R contains throwing statements
636 or some of the exceptions in inner regions can be unwound up to R.
638 CONTAINS_STMT is bitmap of all regions that contains some throwing
641 Function looks O(^3) at first sight. In fact the function is called at most
642 once for every MUST_NOT_THROW in EH tree from remove_unreachable_regions
643 Because the outer loop walking subregions does not dive in MUST_NOT_THROW,
644 the outer loop examines every region at most once. The inner loop
645 is doing unwinding from the throwing statement same way as we do during
646 CFG construction, so it is O(^2) in size of EH tree, but O(n) in size
647 of CFG. In practice Eh trees are wide, not deep, so this is not
651 can_be_reached_by_runtime (sbitmap contains_stmt, struct eh_region *r)
653 struct eh_region *i = r->inner;
657 if (TEST_BIT (contains_stmt, r->region_number))
660 EXECUTE_IF_SET_IN_BITMAP (r->aka, 0, n, bi)
661 if (TEST_BIT (contains_stmt, n))
667 /* It is pointless to look into MUST_NOT_THROW
668 or dive into subregions. They never unwind up. */
669 if (i->type != ERT_MUST_NOT_THROW)
671 bool found = TEST_BIT (contains_stmt, i->region_number);
673 EXECUTE_IF_SET_IN_BITMAP (i->aka, 0, n, bi)
674 if (TEST_BIT (contains_stmt, n))
679 /* We have nested region that contains throwing statement.
680 See if resuming might lead up to the resx or we get locally
681 caught sooner. If we get locally caught sooner, we either
682 know region R is not reachable or it would have direct edge
683 from the EH resx and thus consider region reachable at
687 struct eh_region *i1 = i;
688 tree type_thrown = NULL_TREE;
690 if (i1->type == ERT_THROW)
692 type_thrown = i1->u.eh_throw.type;
695 for (; i1 != r; i1 = i1->outer)
696 if (reachable_next_level (i1, type_thrown, NULL,
697 false) >= RNL_CAUGHT)
703 /* If there are sub-regions, process them. */
704 if (i->type != ERT_MUST_NOT_THROW && i->inner)
706 /* If there are peers, process them. */
707 else if (i->next_peer)
709 /* Otherwise, step back up the tree to the next peer. */
718 while (i->next_peer == NULL);
724 /* Bring region R to the root of tree. */
727 bring_to_root (struct eh_region *r)
729 struct eh_region **pp;
730 struct eh_region *outer = r->outer;
733 for (pp = &outer->inner; *pp != r; pp = &(*pp)->next_peer)
737 r->next_peer = cfun->eh->region_tree;
738 cfun->eh->region_tree = r;
741 /* Remove all regions whose labels are not reachable.
742 REACHABLE is bitmap of all regions that are used by the function
743 CONTAINS_STMT is bitmap of all regions that contains stmt (or NULL). */
746 remove_unreachable_regions (sbitmap reachable, sbitmap contains_stmt)
750 VEC(eh_region,heap) *must_not_throws = VEC_alloc (eh_region, heap, 16);
751 struct eh_region *local_must_not_throw = NULL;
752 struct eh_region *first_must_not_throw = NULL;
754 for (i = cfun->eh->last_region_number; i > 0; --i)
756 r = VEC_index (eh_region, cfun->eh->region_array, i);
757 if (!r || r->region_number != i)
759 if (!TEST_BIT (reachable, i) && !r->resume)
763 r->tree_label = NULL;
767 /* Don't remove ERT_THROW regions if their outer region
769 if (r->outer && TEST_BIT (reachable, r->outer->region_number))
772 case ERT_MUST_NOT_THROW:
773 /* MUST_NOT_THROW regions are implementable solely in the
774 runtime, but we need them when inlining function.
776 Keep them if outer region is not MUST_NOT_THROW a well
777 and if they contain some statement that might unwind through
779 if ((!r->outer || r->outer->type != ERT_MUST_NOT_THROW)
781 || can_be_reached_by_runtime (contains_stmt, r)))
786 /* TRY regions are reachable if any of its CATCH regions
789 for (c = r->u.eh_try.eh_catch; c;
790 c = c->u.eh_catch.next_catch)
791 if (TEST_BIT (reachable, c->region_number))
806 fprintf (dump_file, "Removing unreachable eh region %i\n",
808 remove_eh_handler (r);
810 else if (r->type == ERT_MUST_NOT_THROW)
812 if (!first_must_not_throw)
813 first_must_not_throw = r;
814 VEC_safe_push (eh_region, heap, must_not_throws, r);
818 if (r->type == ERT_MUST_NOT_THROW)
820 if (!local_must_not_throw)
821 local_must_not_throw = r;
823 VEC_safe_push (eh_region, heap, must_not_throws, r);
827 /* MUST_NOT_THROW regions without local handler are all the same; they
828 trigger terminate call in runtime.
829 MUST_NOT_THROW handled locally can differ in debug info associated
830 to std::terminate () call or if one is coming from Java and other
831 from C++ whether they call terminate or abort.
833 We merge all MUST_NOT_THROW regions handled by the run-time into one.
834 We alsobring all local MUST_NOT_THROW regions to the roots of EH tree
835 (since unwinding never continues to the outer region anyway).
836 If MUST_NOT_THROW with local handler is present in the tree, we use
837 that region to merge into, since it will remain in tree anyway;
838 otherwise we use first MUST_NOT_THROW.
840 Merging of locally handled regions needs changes to the CFG. Crossjumping
841 should take care of this, by looking at the actual code and
842 ensuring that the cleanup actions are really the same. */
844 if (local_must_not_throw)
845 first_must_not_throw = local_must_not_throw;
847 for (i = 0; VEC_iterate (eh_region, must_not_throws, i, r); i++)
849 if (!r->label && !r->tree_label && r != first_must_not_throw)
852 fprintf (dump_file, "Replacing MUST_NOT_THROW region %i by %i\n",
854 first_must_not_throw->region_number);
855 remove_eh_handler_and_replace (r, first_must_not_throw);
860 #ifdef ENABLE_CHECKING
861 verify_eh_tree (cfun);
863 VEC_free (eh_region, heap, must_not_throws);
866 /* Return array mapping LABEL_DECL_UID to region such that region's tree_label
867 is identical to label. */
870 label_to_region_map (void)
872 VEC(int,heap) * label_to_region = NULL;
875 VEC_safe_grow_cleared (int, heap, label_to_region,
876 cfun->cfg->last_label_uid + 1);
877 for (i = cfun->eh->last_region_number; i > 0; --i)
879 struct eh_region *r = VEC_index (eh_region, cfun->eh->region_array, i);
880 if (r && r->region_number == i
881 && r->tree_label && LABEL_DECL_UID (r->tree_label) >= 0)
883 VEC_replace (int, label_to_region, LABEL_DECL_UID (r->tree_label),
887 return label_to_region;
890 /* Return number of EH regions. */
892 num_eh_regions (void)
894 return cfun->eh->last_region_number + 1;
897 /* Remove all regions whose labels are not reachable from insns. */
900 rtl_remove_unreachable_regions (rtx insns)
902 int i, *uid_region_num;
907 uid_region_num = XCNEWVEC (int, get_max_uid ());
908 reachable = sbitmap_alloc (cfun->eh->last_region_number + 1);
909 sbitmap_zero (reachable);
911 for (i = cfun->eh->last_region_number; i > 0; --i)
913 r = VEC_index (eh_region, cfun->eh->region_array, i);
914 if (!r || r->region_number != i)
919 gcc_assert (!uid_region_num[INSN_UID (r->resume)]);
920 uid_region_num[INSN_UID (r->resume)] = i;
924 gcc_assert (!uid_region_num[INSN_UID (r->label)]);
925 uid_region_num[INSN_UID (r->label)] = i;
929 for (insn = insns; insn; insn = NEXT_INSN (insn))
930 SET_BIT (reachable, uid_region_num[INSN_UID (insn)]);
932 remove_unreachable_regions (reachable, NULL);
934 sbitmap_free (reachable);
935 free (uid_region_num);
938 /* Set up EH labels for RTL. */
941 convert_from_eh_region_ranges (void)
943 rtx insns = get_insns ();
944 int i, n = cfun->eh->last_region_number;
946 /* Most of the work is already done at the tree level. All we need to
947 do is collect the rtl labels that correspond to the tree labels that
948 collect the rtl labels that correspond to the tree labels
949 we allocated earlier. */
950 for (i = 1; i <= n; ++i)
952 struct eh_region *region;
954 region = VEC_index (eh_region, cfun->eh->region_array, i);
955 if (region && region->tree_label)
956 region->label = DECL_RTL_IF_SET (region->tree_label);
959 rtl_remove_unreachable_regions (insns);
963 add_ehl_entry (rtx label, struct eh_region *region)
965 struct ehl_map_entry **slot, *entry;
967 LABEL_PRESERVE_P (label) = 1;
969 entry = GGC_NEW (struct ehl_map_entry);
970 entry->label = label;
971 entry->region = region;
973 slot = (struct ehl_map_entry **)
974 htab_find_slot (crtl->eh.exception_handler_label_map, entry, INSERT);
976 /* Before landing pad creation, each exception handler has its own
977 label. After landing pad creation, the exception handlers may
978 share landing pads. This is ok, since maybe_remove_eh_handler
979 only requires the 1-1 mapping before landing pad creation. */
980 gcc_assert (!*slot || crtl->eh.built_landing_pads);
986 find_exception_handler_labels (void)
990 if (crtl->eh.exception_handler_label_map)
991 htab_empty (crtl->eh.exception_handler_label_map);
994 /* ??? The expansion factor here (3/2) must be greater than the htab
995 occupancy factor (4/3) to avoid unnecessary resizing. */
996 crtl->eh.exception_handler_label_map
997 = htab_create_ggc (cfun->eh->last_region_number * 3 / 2,
998 ehl_hash, ehl_eq, NULL);
1001 if (cfun->eh->region_tree == NULL)
1004 for (i = cfun->eh->last_region_number; i > 0; --i)
1006 struct eh_region *region;
1009 region = VEC_index (eh_region, cfun->eh->region_array, i);
1010 if (! region || region->region_number != i)
1012 if (crtl->eh.built_landing_pads)
1013 lab = region->landing_pad;
1015 lab = region->label;
1018 add_ehl_entry (lab, region);
1021 /* For sjlj exceptions, need the return label to remain live until
1022 after landing pad generation. */
1023 if (USING_SJLJ_EXCEPTIONS && ! crtl->eh.built_landing_pads)
1024 add_ehl_entry (return_label, NULL);
1027 /* Returns true if the current function has exception handling regions. */
1030 current_function_has_exception_handlers (void)
1034 for (i = cfun->eh->last_region_number; i > 0; --i)
1036 struct eh_region *region;
1038 region = VEC_index (eh_region, cfun->eh->region_array, i);
1040 && region->region_number == i
1041 && region->type != ERT_THROW)
1048 /* A subroutine of duplicate_eh_regions. Search the region tree under O
1049 for the minimum and maximum region numbers. Update *MIN and *MAX. */
1052 duplicate_eh_regions_0 (eh_region o, int *min, int *max)
1058 i = bitmap_first_set_bit (o->aka);
1061 i = bitmap_last_set_bit (o->aka);
1065 if (o->region_number < *min)
1066 *min = o->region_number;
1067 if (o->region_number > *max)
1068 *max = o->region_number;
1073 duplicate_eh_regions_0 (o, min, max);
1074 while (o->next_peer)
1077 duplicate_eh_regions_0 (o, min, max);
1082 /* A subroutine of duplicate_eh_regions. Copy the region tree under OLD.
1083 Root it at OUTER, and apply EH_OFFSET to the region number. Don't worry
1084 about the other internal pointers just yet, just the tree-like pointers. */
1087 duplicate_eh_regions_1 (eh_region old, eh_region outer, int eh_offset)
1091 ret = n = GGC_NEW (struct eh_region);
1095 n->next_peer = NULL;
1100 n->aka = BITMAP_GGC_ALLOC ();
1102 EXECUTE_IF_SET_IN_BITMAP (old->aka, 0, i, bi)
1104 bitmap_set_bit (n->aka, i + eh_offset);
1105 VEC_replace (eh_region, cfun->eh->region_array, i + eh_offset, n);
1109 n->region_number += eh_offset;
1110 VEC_replace (eh_region, cfun->eh->region_array, n->region_number, n);
1115 n = n->inner = duplicate_eh_regions_1 (old, ret, eh_offset);
1116 while (old->next_peer)
1118 old = old->next_peer;
1119 n = n->next_peer = duplicate_eh_regions_1 (old, ret, eh_offset);
1126 /* Duplicate the EH regions of IFUN, rooted at COPY_REGION, into current
1127 function and root the tree below OUTER_REGION. Remap labels using MAP
1128 callback. The special case of COPY_REGION of 0 means all regions. */
1131 duplicate_eh_regions (struct function *ifun, duplicate_eh_regions_map map,
1132 void *data, int copy_region, int outer_region)
1134 eh_region cur, prev_try, outer, *splice;
1135 int i, min_region, max_region, eh_offset, cfun_last_region_number;
1140 #ifdef ENABLE_CHECKING
1141 verify_eh_tree (ifun);
1144 /* Find the range of region numbers to be copied. The interface we
1145 provide here mandates a single offset to find new number from old,
1146 which means we must look at the numbers present, instead of the
1147 count or something else. */
1148 if (copy_region > 0)
1150 min_region = INT_MAX;
1153 cur = VEC_index (eh_region, ifun->eh->region_array, copy_region);
1154 duplicate_eh_regions_0 (cur, &min_region, &max_region);
1157 min_region = 1, max_region = ifun->eh->last_region_number;
1158 num_regions = max_region - min_region + 1;
1159 cfun_last_region_number = cfun->eh->last_region_number;
1160 eh_offset = cfun_last_region_number + 1 - min_region;
1162 /* If we've not yet created a region array, do so now. */
1163 cfun->eh->last_region_number = cfun_last_region_number + num_regions;
1164 VEC_safe_grow_cleared (eh_region, gc, cfun->eh->region_array,
1165 cfun->eh->last_region_number + 1);
1167 /* Locate the spot at which to insert the new tree. */
1168 if (outer_region > 0)
1170 outer = VEC_index (eh_region, cfun->eh->region_array, outer_region);
1172 splice = &outer->inner;
1174 splice = &cfun->eh->region_tree;
1179 splice = &cfun->eh->region_tree;
1182 splice = &(*splice)->next_peer;
1184 if (!ifun->eh->region_tree)
1187 for (i = cfun_last_region_number + 1;
1188 i <= cfun->eh->last_region_number; i++)
1190 VEC_replace (eh_region, cfun->eh->region_array, i, outer);
1191 if (outer->aka == NULL)
1192 outer->aka = BITMAP_GGC_ALLOC ();
1193 bitmap_set_bit (outer->aka, i);
1198 /* Copy all the regions in the subtree. */
1199 if (copy_region > 0)
1201 cur = VEC_index (eh_region, ifun->eh->region_array, copy_region);
1202 *splice = duplicate_eh_regions_1 (cur, outer, eh_offset);
1208 cur = ifun->eh->region_tree;
1209 *splice = n = duplicate_eh_regions_1 (cur, outer, eh_offset);
1210 while (cur->next_peer)
1212 cur = cur->next_peer;
1213 n = n->next_peer = duplicate_eh_regions_1 (cur, outer, eh_offset);
1217 /* Remap all the labels in the new regions. */
1218 for (i = cfun_last_region_number + 1;
1219 VEC_iterate (eh_region, cfun->eh->region_array, i, cur); ++i)
1220 if (cur && cur->tree_label)
1221 cur->tree_label = map (cur->tree_label, data);
1223 /* Search for the containing ERT_TRY region to fix up
1224 the prev_try short-cuts for ERT_CLEANUP regions. */
1226 if (outer_region > 0)
1228 VEC_index (eh_region, cfun->eh->region_array, outer_region);
1229 prev_try && prev_try->type != ERT_TRY; prev_try = prev_try->outer)
1230 if (prev_try->type == ERT_MUST_NOT_THROW
1231 || (prev_try->type == ERT_ALLOWED_EXCEPTIONS
1232 && !prev_try->u.allowed.type_list))
1238 /* Remap all of the internal catch and cleanup linkages. Since we
1239 duplicate entire subtrees, all of the referenced regions will have
1240 been copied too. And since we renumbered them as a block, a simple
1241 bit of arithmetic finds us the index for the replacement region. */
1242 for (i = cfun_last_region_number + 1;
1243 VEC_iterate (eh_region, cfun->eh->region_array, i, cur); ++i)
1245 /* All removed EH that is toplevel in input function is now
1246 in outer EH of output function. */
1249 gcc_assert (VEC_index
1250 (eh_region, ifun->eh->region_array,
1251 i - eh_offset) == NULL);
1254 VEC_replace (eh_region, cfun->eh->region_array, i, outer);
1255 if (outer->aka == NULL)
1256 outer->aka = BITMAP_GGC_ALLOC ();
1257 bitmap_set_bit (outer->aka, i);
1261 if (i != cur->region_number)
1264 #define REMAP(REG) \
1265 (REG) = VEC_index (eh_region, cfun->eh->region_array, \
1266 (REG)->region_number + eh_offset)
1271 if (cur->u.eh_try.eh_catch)
1272 REMAP (cur->u.eh_try.eh_catch);
1273 if (cur->u.eh_try.last_catch)
1274 REMAP (cur->u.eh_try.last_catch);
1278 if (cur->u.eh_catch.next_catch)
1279 REMAP (cur->u.eh_catch.next_catch);
1280 if (cur->u.eh_catch.prev_catch)
1281 REMAP (cur->u.eh_catch.prev_catch);
1285 if (cur->u.cleanup.prev_try)
1286 REMAP (cur->u.cleanup.prev_try);
1288 cur->u.cleanup.prev_try = prev_try;
1297 #ifdef ENABLE_CHECKING
1298 verify_eh_tree (cfun);
1304 /* Return true if REGION_A is outer to REGION_B in IFUN. */
1307 eh_region_outer_p (struct function *ifun, int region_a, int region_b)
1309 struct eh_region *rp_a, *rp_b;
1311 gcc_assert (ifun->eh->last_region_number > 0);
1312 gcc_assert (ifun->eh->region_tree);
1314 rp_a = VEC_index (eh_region, ifun->eh->region_array, region_a);
1315 rp_b = VEC_index (eh_region, ifun->eh->region_array, region_b);
1316 gcc_assert (rp_a != NULL);
1317 gcc_assert (rp_b != NULL);
1330 /* Return region number of region that is outer to both if REGION_A and
1331 REGION_B in IFUN. */
1334 eh_region_outermost (struct function *ifun, int region_a, int region_b)
1336 struct eh_region *rp_a, *rp_b;
1339 gcc_assert (ifun->eh->last_region_number > 0);
1340 gcc_assert (ifun->eh->region_tree);
1342 rp_a = VEC_index (eh_region, ifun->eh->region_array, region_a);
1343 rp_b = VEC_index (eh_region, ifun->eh->region_array, region_b);
1344 gcc_assert (rp_a != NULL);
1345 gcc_assert (rp_b != NULL);
1347 b_outer = sbitmap_alloc (ifun->eh->last_region_number + 1);
1348 sbitmap_zero (b_outer);
1352 SET_BIT (b_outer, rp_b->region_number);
1359 if (TEST_BIT (b_outer, rp_a->region_number))
1361 sbitmap_free (b_outer);
1362 return rp_a->region_number;
1368 sbitmap_free (b_outer);
1373 t2r_eq (const void *pentry, const void *pdata)
1375 const_tree const entry = (const_tree) pentry;
1376 const_tree const data = (const_tree) pdata;
1378 return TREE_PURPOSE (entry) == data;
1382 t2r_hash (const void *pentry)
1384 const_tree const entry = (const_tree) pentry;
1385 return TREE_HASH (TREE_PURPOSE (entry));
1389 add_type_for_runtime (tree type)
1393 slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
1394 TREE_HASH (type), INSERT);
1397 tree runtime = (*lang_eh_runtime_type) (type);
1398 *slot = tree_cons (type, runtime, NULL_TREE);
1403 lookup_type_for_runtime (tree type)
1407 slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
1408 TREE_HASH (type), NO_INSERT);
1410 /* We should have always inserted the data earlier. */
1411 return TREE_VALUE (*slot);
1415 /* Represent an entry in @TTypes for either catch actions
1416 or exception filter actions. */
1417 struct ttypes_filter GTY(())
1423 /* Compare ENTRY (a ttypes_filter entry in the hash table) with DATA
1424 (a tree) for a @TTypes type node we are thinking about adding. */
1427 ttypes_filter_eq (const void *pentry, const void *pdata)
1429 const struct ttypes_filter *const entry
1430 = (const struct ttypes_filter *) pentry;
1431 const_tree const data = (const_tree) pdata;
1433 return entry->t == data;
1437 ttypes_filter_hash (const void *pentry)
1439 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1440 return TREE_HASH (entry->t);
1443 /* Compare ENTRY with DATA (both struct ttypes_filter) for a @TTypes
1444 exception specification list we are thinking about adding. */
1445 /* ??? Currently we use the type lists in the order given. Someone
1446 should put these in some canonical order. */
1449 ehspec_filter_eq (const void *pentry, const void *pdata)
1451 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1452 const struct ttypes_filter *data = (const struct ttypes_filter *) pdata;
1454 return type_list_equal (entry->t, data->t);
1457 /* Hash function for exception specification lists. */
1460 ehspec_filter_hash (const void *pentry)
1462 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1466 for (list = entry->t; list ; list = TREE_CHAIN (list))
1467 h = (h << 5) + (h >> 27) + TREE_HASH (TREE_VALUE (list));
1471 /* Add TYPE (which may be NULL) to crtl->eh.ttype_data, using TYPES_HASH
1472 to speed up the search. Return the filter value to be used. */
1475 add_ttypes_entry (htab_t ttypes_hash, tree type)
1477 struct ttypes_filter **slot, *n;
1479 slot = (struct ttypes_filter **)
1480 htab_find_slot_with_hash (ttypes_hash, type, TREE_HASH (type), INSERT);
1482 if ((n = *slot) == NULL)
1484 /* Filter value is a 1 based table index. */
1486 n = XNEW (struct ttypes_filter);
1488 n->filter = VEC_length (tree, crtl->eh.ttype_data) + 1;
1491 VEC_safe_push (tree, gc, crtl->eh.ttype_data, type);
1497 /* Add LIST to crtl->eh.ehspec_data, using EHSPEC_HASH and TYPES_HASH
1498 to speed up the search. Return the filter value to be used. */
1501 add_ehspec_entry (htab_t ehspec_hash, htab_t ttypes_hash, tree list)
1503 struct ttypes_filter **slot, *n;
1504 struct ttypes_filter dummy;
1507 slot = (struct ttypes_filter **)
1508 htab_find_slot (ehspec_hash, &dummy, INSERT);
1510 if ((n = *slot) == NULL)
1512 /* Filter value is a -1 based byte index into a uleb128 buffer. */
1514 n = XNEW (struct ttypes_filter);
1516 n->filter = -(VARRAY_ACTIVE_SIZE (crtl->eh.ehspec_data) + 1);
1519 /* Generate a 0 terminated list of filter values. */
1520 for (; list ; list = TREE_CHAIN (list))
1522 if (targetm.arm_eabi_unwinder)
1523 VARRAY_PUSH_TREE (crtl->eh.ehspec_data, TREE_VALUE (list));
1526 /* Look up each type in the list and encode its filter
1527 value as a uleb128. */
1528 push_uleb128 (&crtl->eh.ehspec_data,
1529 add_ttypes_entry (ttypes_hash, TREE_VALUE (list)));
1532 if (targetm.arm_eabi_unwinder)
1533 VARRAY_PUSH_TREE (crtl->eh.ehspec_data, NULL_TREE);
1535 VARRAY_PUSH_UCHAR (crtl->eh.ehspec_data, 0);
1541 /* Generate the action filter values to be used for CATCH and
1542 ALLOWED_EXCEPTIONS regions. When using dwarf2 exception regions,
1543 we use lots of landing pads, and so every type or list can share
1544 the same filter value, which saves table space. */
1547 assign_filter_values (void)
1550 htab_t ttypes, ehspec;
1552 crtl->eh.ttype_data = VEC_alloc (tree, gc, 16);
1553 if (targetm.arm_eabi_unwinder)
1554 VARRAY_TREE_INIT (crtl->eh.ehspec_data, 64, "ehspec_data");
1556 VARRAY_UCHAR_INIT (crtl->eh.ehspec_data, 64, "ehspec_data");
1558 ttypes = htab_create (31, ttypes_filter_hash, ttypes_filter_eq, free);
1559 ehspec = htab_create (31, ehspec_filter_hash, ehspec_filter_eq, free);
1561 for (i = cfun->eh->last_region_number; i > 0; --i)
1563 struct eh_region *r;
1565 r = VEC_index (eh_region, cfun->eh->region_array, i);
1567 /* Mind we don't process a region more than once. */
1568 if (!r || r->region_number != i)
1574 /* Whatever type_list is (NULL or true list), we build a list
1575 of filters for the region. */
1576 r->u.eh_catch.filter_list = NULL_TREE;
1578 if (r->u.eh_catch.type_list != NULL)
1580 /* Get a filter value for each of the types caught and store
1581 them in the region's dedicated list. */
1582 tree tp_node = r->u.eh_catch.type_list;
1584 for (;tp_node; tp_node = TREE_CHAIN (tp_node))
1586 int flt = add_ttypes_entry (ttypes, TREE_VALUE (tp_node));
1587 tree flt_node = build_int_cst (NULL_TREE, flt);
1589 r->u.eh_catch.filter_list
1590 = tree_cons (NULL_TREE, flt_node, r->u.eh_catch.filter_list);
1595 /* Get a filter value for the NULL list also since it will need
1596 an action record anyway. */
1597 int flt = add_ttypes_entry (ttypes, NULL);
1598 tree flt_node = build_int_cst (NULL_TREE, flt);
1600 r->u.eh_catch.filter_list
1601 = tree_cons (NULL_TREE, flt_node, r->u.eh_catch.filter_list);
1606 case ERT_ALLOWED_EXCEPTIONS:
1608 = add_ehspec_entry (ehspec, ttypes, r->u.allowed.type_list);
1616 htab_delete (ttypes);
1617 htab_delete (ehspec);
1620 /* Emit SEQ into basic block just before INSN (that is assumed to be
1621 first instruction of some existing BB and return the newly
1624 emit_to_new_bb_before (rtx seq, rtx insn)
1631 /* If there happens to be a fallthru edge (possibly created by cleanup_cfg
1632 call), we don't want it to go into newly created landing pad or other EH
1634 for (ei = ei_start (BLOCK_FOR_INSN (insn)->preds); (e = ei_safe_edge (ei)); )
1635 if (e->flags & EDGE_FALLTHRU)
1636 force_nonfallthru (e);
1639 last = emit_insn_before (seq, insn);
1640 if (BARRIER_P (last))
1641 last = PREV_INSN (last);
1642 bb = create_basic_block (seq, last, BLOCK_FOR_INSN (insn)->prev_bb);
1643 update_bb_for_insn (bb);
1644 bb->flags |= BB_SUPERBLOCK;
1648 /* Generate the code to actually handle exceptions, which will follow the
1652 build_post_landing_pads (void)
1656 for (i = cfun->eh->last_region_number; i > 0; --i)
1658 struct eh_region *region;
1661 region = VEC_index (eh_region, cfun->eh->region_array, i);
1662 /* Mind we don't process a region more than once. */
1663 if (!region || region->region_number != i)
1666 switch (region->type)
1669 /* ??? Collect the set of all non-overlapping catch handlers
1670 all the way up the chain until blocked by a cleanup. */
1671 /* ??? Outer try regions can share landing pads with inner
1672 try regions if the types are completely non-overlapping,
1673 and there are no intervening cleanups. */
1675 region->post_landing_pad = gen_label_rtx ();
1679 emit_label (region->post_landing_pad);
1681 /* ??? It is mighty inconvenient to call back into the
1682 switch statement generation code in expand_end_case.
1683 Rapid prototyping sez a sequence of ifs. */
1685 struct eh_region *c;
1686 for (c = region->u.eh_try.eh_catch; c ; c = c->u.eh_catch.next_catch)
1688 if (c->u.eh_catch.type_list == NULL)
1689 emit_jump (c->label);
1692 /* Need for one cmp/jump per type caught. Each type
1693 list entry has a matching entry in the filter list
1694 (see assign_filter_values). */
1695 tree tp_node = c->u.eh_catch.type_list;
1696 tree flt_node = c->u.eh_catch.filter_list;
1700 emit_cmp_and_jump_insns
1702 GEN_INT (tree_low_cst (TREE_VALUE (flt_node), 0)),
1704 targetm.eh_return_filter_mode (), 0, c->label);
1706 tp_node = TREE_CHAIN (tp_node);
1707 flt_node = TREE_CHAIN (flt_node);
1713 /* We delay the generation of the _Unwind_Resume until we generate
1714 landing pads. We emit a marker here so as to get good control
1715 flow data in the meantime. */
1717 = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
1723 emit_to_new_bb_before (seq, region->u.eh_try.eh_catch->label);
1727 case ERT_ALLOWED_EXCEPTIONS:
1728 region->post_landing_pad = gen_label_rtx ();
1732 emit_label (region->post_landing_pad);
1734 emit_cmp_and_jump_insns (crtl->eh.filter,
1735 GEN_INT (region->u.allowed.filter),
1737 targetm.eh_return_filter_mode (), 0, region->label);
1739 /* We delay the generation of the _Unwind_Resume until we generate
1740 landing pads. We emit a marker here so as to get good control
1741 flow data in the meantime. */
1743 = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
1749 emit_to_new_bb_before (seq, region->label);
1753 case ERT_MUST_NOT_THROW:
1754 region->post_landing_pad = region->label;
1759 /* Nothing to do. */
1768 /* Replace RESX patterns with jumps to the next handler if any, or calls to
1769 _Unwind_Resume otherwise. */
1772 connect_post_landing_pads (void)
1776 for (i = cfun->eh->last_region_number; i > 0; --i)
1778 struct eh_region *region;
1779 struct eh_region *outer;
1783 region = VEC_index (eh_region, cfun->eh->region_array, i);
1784 /* Mind we don't process a region more than once. */
1785 if (!region || region->region_number != i)
1788 /* If there is no RESX, or it has been deleted by flow, there's
1789 nothing to fix up. */
1790 if (! region->resume || INSN_DELETED_P (region->resume))
1793 /* Search for another landing pad in this function. */
1794 for (outer = region->outer; outer ; outer = outer->outer)
1795 if (outer->post_landing_pad)
1803 basic_block src, dest;
1805 emit_jump (outer->post_landing_pad);
1806 src = BLOCK_FOR_INSN (region->resume);
1807 dest = BLOCK_FOR_INSN (outer->post_landing_pad);
1808 while (EDGE_COUNT (src->succs) > 0)
1809 remove_edge (EDGE_SUCC (src, 0));
1810 e = make_edge (src, dest, 0);
1811 e->probability = REG_BR_PROB_BASE;
1812 e->count = src->count;
1816 emit_library_call (unwind_resume_libfunc, LCT_THROW,
1817 VOIDmode, 1, crtl->eh.exc_ptr, ptr_mode);
1819 /* What we just emitted was a throwing libcall, so it got a
1820 barrier automatically added after it. If the last insn in
1821 the libcall sequence isn't the barrier, it's because the
1822 target emits multiple insns for a call, and there are insns
1823 after the actual call insn (which are redundant and would be
1824 optimized away). The barrier is inserted exactly after the
1825 call insn, so let's go get that and delete the insns after
1826 it, because below we need the barrier to be the last insn in
1828 delete_insns_since (NEXT_INSN (last_call_insn ()));
1833 barrier = emit_insn_before (seq, region->resume);
1834 /* Avoid duplicate barrier. */
1835 gcc_assert (BARRIER_P (barrier));
1836 delete_insn (barrier);
1837 delete_insn (region->resume);
1839 /* ??? From tree-ssa we can wind up with catch regions whose
1840 label is not instantiated, but whose resx is present. Now
1841 that we've dealt with the resx, kill the region. */
1842 if (region->label == NULL && region->type == ERT_CLEANUP)
1843 remove_eh_handler (region);
1849 dw2_build_landing_pads (void)
1853 for (i = cfun->eh->last_region_number; i > 0; --i)
1855 struct eh_region *region;
1860 region = VEC_index (eh_region, cfun->eh->region_array, i);
1861 /* Mind we don't process a region more than once. */
1862 if (!region || region->region_number != i)
1865 if (region->type != ERT_CLEANUP
1866 && region->type != ERT_TRY
1867 && region->type != ERT_ALLOWED_EXCEPTIONS)
1872 region->landing_pad = gen_label_rtx ();
1873 emit_label (region->landing_pad);
1875 #ifdef HAVE_exception_receiver
1876 if (HAVE_exception_receiver)
1877 emit_insn (gen_exception_receiver ());
1880 #ifdef HAVE_nonlocal_goto_receiver
1881 if (HAVE_nonlocal_goto_receiver)
1882 emit_insn (gen_nonlocal_goto_receiver ());
1887 emit_move_insn (crtl->eh.exc_ptr,
1888 gen_rtx_REG (ptr_mode, EH_RETURN_DATA_REGNO (0)));
1889 emit_move_insn (crtl->eh.filter,
1890 gen_rtx_REG (targetm.eh_return_filter_mode (),
1891 EH_RETURN_DATA_REGNO (1)));
1896 bb = emit_to_new_bb_before (seq, region->post_landing_pad);
1897 e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
1898 e->count = bb->count;
1899 e->probability = REG_BR_PROB_BASE;
1906 int directly_reachable;
1909 int call_site_index;
1913 sjlj_find_directly_reachable_regions (struct sjlj_lp_info *lp_info)
1916 bool found_one = false;
1918 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
1920 struct eh_region *region;
1921 enum reachable_code rc;
1925 if (! INSN_P (insn))
1928 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1929 if (!note || INTVAL (XEXP (note, 0)) <= 0)
1932 region = VEC_index (eh_region, cfun->eh->region_array, INTVAL (XEXP (note, 0)));
1936 type_thrown = NULL_TREE;
1937 if (region->type == ERT_THROW)
1939 type_thrown = region->u.eh_throw.type;
1940 region = region->outer;
1943 /* Find the first containing region that might handle the exception.
1944 That's the landing pad to which we will transfer control. */
1945 rc = RNL_NOT_CAUGHT;
1946 for (; region; region = region->outer)
1948 rc = reachable_next_level (region, type_thrown, NULL, false);
1949 if (rc != RNL_NOT_CAUGHT)
1952 if (rc == RNL_MAYBE_CAUGHT || rc == RNL_CAUGHT)
1954 lp_info[region->region_number].directly_reachable = 1;
1963 sjlj_assign_call_site_values (rtx dispatch_label, struct sjlj_lp_info *lp_info)
1968 /* First task: build the action table. */
1970 VARRAY_UCHAR_INIT (crtl->eh.action_record_data, 64, "action_record_data");
1971 ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
1973 for (i = cfun->eh->last_region_number; i > 0; --i)
1974 if (lp_info[i].directly_reachable)
1976 struct eh_region *r = VEC_index (eh_region, cfun->eh->region_array, i);
1978 r->landing_pad = dispatch_label;
1979 lp_info[i].action_index = collect_one_action_chain (ar_hash, r);
1980 if (lp_info[i].action_index != -1)
1981 crtl->uses_eh_lsda = 1;
1984 htab_delete (ar_hash);
1986 /* Next: assign dispatch values. In dwarf2 terms, this would be the
1987 landing pad label for the region. For sjlj though, there is one
1988 common landing pad from which we dispatch to the post-landing pads.
1990 A region receives a dispatch index if it is directly reachable
1991 and requires in-function processing. Regions that share post-landing
1992 pads may share dispatch indices. */
1993 /* ??? Post-landing pad sharing doesn't actually happen at the moment
1994 (see build_post_landing_pads) so we don't bother checking for it. */
1997 for (i = cfun->eh->last_region_number; i > 0; --i)
1998 if (lp_info[i].directly_reachable)
1999 lp_info[i].dispatch_index = index++;
2001 /* Finally: assign call-site values. If dwarf2 terms, this would be
2002 the region number assigned by convert_to_eh_region_ranges, but
2003 handles no-action and must-not-throw differently. */
2006 for (i = cfun->eh->last_region_number; i > 0; --i)
2007 if (lp_info[i].directly_reachable)
2009 int action = lp_info[i].action_index;
2011 /* Map must-not-throw to otherwise unused call-site index 0. */
2014 /* Map no-action to otherwise unused call-site index -1. */
2015 else if (action == -1)
2017 /* Otherwise, look it up in the table. */
2019 index = add_call_site (GEN_INT (lp_info[i].dispatch_index), action);
2021 lp_info[i].call_site_index = index;
2026 sjlj_mark_call_sites (struct sjlj_lp_info *lp_info)
2028 int last_call_site = -2;
2031 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
2033 struct eh_region *region;
2035 rtx note, before, p;
2037 /* Reset value tracking at extended basic block boundaries. */
2039 last_call_site = -2;
2041 if (! INSN_P (insn))
2044 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2046 /* Calls that are known to not throw need not be marked. */
2047 if (note && INTVAL (XEXP (note, 0)) <= 0)
2051 region = VEC_index (eh_region, cfun->eh->region_array, INTVAL (XEXP (note, 0)));
2057 /* Calls (and trapping insns) without notes are outside any
2058 exception handling region in this function. Mark them as
2061 || (flag_non_call_exceptions
2062 && may_trap_p (PATTERN (insn))))
2063 this_call_site = -1;
2068 this_call_site = lp_info[region->region_number].call_site_index;
2070 if (this_call_site == last_call_site)
2073 /* Don't separate a call from it's argument loads. */
2076 before = find_first_parameter_load (insn, NULL_RTX);
2079 mem = adjust_address (crtl->eh.sjlj_fc, TYPE_MODE (integer_type_node),
2080 sjlj_fc_call_site_ofs);
2081 emit_move_insn (mem, GEN_INT (this_call_site));
2085 emit_insn_before (p, before);
2086 last_call_site = this_call_site;
2090 /* Construct the SjLj_Function_Context. */
2093 sjlj_emit_function_enter (rtx dispatch_label)
2095 rtx fn_begin, fc, mem, seq;
2096 bool fn_begin_outside_block;
2098 fc = crtl->eh.sjlj_fc;
2102 /* We're storing this libcall's address into memory instead of
2103 calling it directly. Thus, we must call assemble_external_libcall
2104 here, as we can not depend on emit_library_call to do it for us. */
2105 assemble_external_libcall (eh_personality_libfunc);
2106 mem = adjust_address (fc, Pmode, sjlj_fc_personality_ofs);
2107 emit_move_insn (mem, eh_personality_libfunc);
2109 mem = adjust_address (fc, Pmode, sjlj_fc_lsda_ofs);
2110 if (crtl->uses_eh_lsda)
2115 ASM_GENERATE_INTERNAL_LABEL (buf, "LLSDA", current_function_funcdef_no);
2116 sym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
2117 SYMBOL_REF_FLAGS (sym) = SYMBOL_FLAG_LOCAL;
2118 emit_move_insn (mem, sym);
2121 emit_move_insn (mem, const0_rtx);
2123 #ifdef DONT_USE_BUILTIN_SETJMP
2126 x = emit_library_call_value (setjmp_libfunc, NULL_RTX, LCT_RETURNS_TWICE,
2127 TYPE_MODE (integer_type_node), 1,
2128 plus_constant (XEXP (fc, 0),
2129 sjlj_fc_jbuf_ofs), Pmode);
2131 emit_cmp_and_jump_insns (x, const0_rtx, NE, 0,
2132 TYPE_MODE (integer_type_node), 0, dispatch_label);
2133 add_reg_br_prob_note (get_insns (), REG_BR_PROB_BASE/100);
2136 expand_builtin_setjmp_setup (plus_constant (XEXP (fc, 0), sjlj_fc_jbuf_ofs),
2140 emit_library_call (unwind_sjlj_register_libfunc, LCT_NORMAL, VOIDmode,
2141 1, XEXP (fc, 0), Pmode);
2146 /* ??? Instead of doing this at the beginning of the function,
2147 do this in a block that is at loop level 0 and dominates all
2148 can_throw_internal instructions. */
2150 fn_begin_outside_block = true;
2151 for (fn_begin = get_insns (); ; fn_begin = NEXT_INSN (fn_begin))
2152 if (NOTE_P (fn_begin))
2154 if (NOTE_KIND (fn_begin) == NOTE_INSN_FUNCTION_BEG)
2156 else if (NOTE_INSN_BASIC_BLOCK_P (fn_begin))
2157 fn_begin_outside_block = false;
2160 if (fn_begin_outside_block)
2161 insert_insn_on_edge (seq, single_succ_edge (ENTRY_BLOCK_PTR));
2163 emit_insn_after (seq, fn_begin);
2166 /* Call back from expand_function_end to know where we should put
2167 the call to unwind_sjlj_unregister_libfunc if needed. */
2170 sjlj_emit_function_exit_after (rtx after)
2172 crtl->eh.sjlj_exit_after = after;
2176 sjlj_emit_function_exit (void)
2184 emit_library_call (unwind_sjlj_unregister_libfunc, LCT_NORMAL, VOIDmode,
2185 1, XEXP (crtl->eh.sjlj_fc, 0), Pmode);
2190 /* ??? Really this can be done in any block at loop level 0 that
2191 post-dominates all can_throw_internal instructions. This is
2192 the last possible moment. */
2194 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
2195 if (e->flags & EDGE_FALLTHRU)
2201 /* Figure out whether the place we are supposed to insert libcall
2202 is inside the last basic block or after it. In the other case
2203 we need to emit to edge. */
2204 gcc_assert (e->src->next_bb == EXIT_BLOCK_PTR);
2205 for (insn = BB_HEAD (e->src); ; insn = NEXT_INSN (insn))
2207 if (insn == crtl->eh.sjlj_exit_after)
2210 insn = NEXT_INSN (insn);
2211 emit_insn_after (seq, insn);
2214 if (insn == BB_END (e->src))
2217 insert_insn_on_edge (seq, e);
2222 sjlj_emit_dispatch_table (rtx dispatch_label, struct sjlj_lp_info *lp_info)
2224 enum machine_mode unwind_word_mode = targetm.unwind_word_mode ();
2225 enum machine_mode filter_mode = targetm.eh_return_filter_mode ();
2226 int i, first_reachable;
2227 rtx mem, dispatch, seq, fc;
2232 fc = crtl->eh.sjlj_fc;
2236 emit_label (dispatch_label);
2238 #ifndef DONT_USE_BUILTIN_SETJMP
2239 expand_builtin_setjmp_receiver (dispatch_label);
2242 /* Load up dispatch index, exc_ptr and filter values from the
2243 function context. */
2244 mem = adjust_address (fc, TYPE_MODE (integer_type_node),
2245 sjlj_fc_call_site_ofs);
2246 dispatch = copy_to_reg (mem);
2248 mem = adjust_address (fc, unwind_word_mode, sjlj_fc_data_ofs);
2249 if (unwind_word_mode != ptr_mode)
2251 #ifdef POINTERS_EXTEND_UNSIGNED
2252 mem = convert_memory_address (ptr_mode, mem);
2254 mem = convert_to_mode (ptr_mode, mem, 0);
2257 emit_move_insn (crtl->eh.exc_ptr, mem);
2259 mem = adjust_address (fc, unwind_word_mode,
2260 sjlj_fc_data_ofs + GET_MODE_SIZE (unwind_word_mode));
2261 if (unwind_word_mode != filter_mode)
2262 mem = convert_to_mode (filter_mode, mem, 0);
2263 emit_move_insn (crtl->eh.filter, mem);
2265 /* Jump to one of the directly reachable regions. */
2266 /* ??? This really ought to be using a switch statement. */
2268 first_reachable = 0;
2269 for (i = cfun->eh->last_region_number; i > 0; --i)
2271 if (! lp_info[i].directly_reachable)
2274 if (! first_reachable)
2276 first_reachable = i;
2280 emit_cmp_and_jump_insns (dispatch, GEN_INT (lp_info[i].dispatch_index),
2281 EQ, NULL_RTX, TYPE_MODE (integer_type_node), 0,
2282 ((struct eh_region *)VEC_index (eh_region, cfun->eh->region_array, i))
2283 ->post_landing_pad);
2289 before = (((struct eh_region *)VEC_index (eh_region, cfun->eh->region_array, first_reachable))
2290 ->post_landing_pad);
2292 bb = emit_to_new_bb_before (seq, before);
2293 e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
2294 e->count = bb->count;
2295 e->probability = REG_BR_PROB_BASE;
2299 sjlj_build_landing_pads (void)
2301 struct sjlj_lp_info *lp_info;
2303 lp_info = XCNEWVEC (struct sjlj_lp_info, cfun->eh->last_region_number + 1);
2305 if (sjlj_find_directly_reachable_regions (lp_info))
2307 rtx dispatch_label = gen_label_rtx ();
2308 int align = STACK_SLOT_ALIGNMENT (sjlj_fc_type_node,
2309 TYPE_MODE (sjlj_fc_type_node),
2310 TYPE_ALIGN (sjlj_fc_type_node));
2312 = assign_stack_local (TYPE_MODE (sjlj_fc_type_node),
2313 int_size_in_bytes (sjlj_fc_type_node),
2316 sjlj_assign_call_site_values (dispatch_label, lp_info);
2317 sjlj_mark_call_sites (lp_info);
2319 sjlj_emit_function_enter (dispatch_label);
2320 sjlj_emit_dispatch_table (dispatch_label, lp_info);
2321 sjlj_emit_function_exit ();
2328 finish_eh_generation (void)
2332 /* Nothing to do if no regions created. */
2333 if (cfun->eh->region_tree == NULL)
2336 /* The object here is to provide find_basic_blocks with detailed
2337 information (via reachable_handlers) on how exception control
2338 flows within the function. In this first pass, we can include
2339 type information garnered from ERT_THROW and ERT_ALLOWED_EXCEPTIONS
2340 regions, and hope that it will be useful in deleting unreachable
2341 handlers. Subsequently, we will generate landing pads which will
2342 connect many of the handlers, and then type information will not
2343 be effective. Still, this is a win over previous implementations. */
2345 /* These registers are used by the landing pads. Make sure they
2346 have been generated. */
2347 get_exception_pointer ();
2348 get_exception_filter ();
2350 /* Construct the landing pads. */
2352 assign_filter_values ();
2353 build_post_landing_pads ();
2354 connect_post_landing_pads ();
2355 if (USING_SJLJ_EXCEPTIONS)
2356 sjlj_build_landing_pads ();
2358 dw2_build_landing_pads ();
2360 crtl->eh.built_landing_pads = 1;
2362 /* We've totally changed the CFG. Start over. */
2363 find_exception_handler_labels ();
2364 break_superblocks ();
2365 if (USING_SJLJ_EXCEPTIONS
2366 /* Kludge for Alpha/Tru64 (see alpha_gp_save_rtx). */
2367 || single_succ_edge (ENTRY_BLOCK_PTR)->insns.r)
2368 commit_edge_insertions ();
2374 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2376 if (e->flags & EDGE_EH)
2385 rtl_make_eh_edge (NULL, bb, BB_END (bb));
2390 ehl_hash (const void *pentry)
2392 const struct ehl_map_entry *const entry
2393 = (const struct ehl_map_entry *) pentry;
2395 /* 2^32 * ((sqrt(5) - 1) / 2) */
2396 const hashval_t scaled_golden_ratio = 0x9e3779b9;
2397 return CODE_LABEL_NUMBER (entry->label) * scaled_golden_ratio;
2401 ehl_eq (const void *pentry, const void *pdata)
2403 const struct ehl_map_entry *const entry
2404 = (const struct ehl_map_entry *) pentry;
2405 const struct ehl_map_entry *const data
2406 = (const struct ehl_map_entry *) pdata;
2408 return entry->label == data->label;
2411 /* This section handles removing dead code for flow. */
2413 /* Remove LABEL from exception_handler_label_map. */
2416 remove_exception_handler_label (rtx label)
2418 struct ehl_map_entry **slot, tmp;
2420 /* If exception_handler_label_map was not built yet,
2421 there is nothing to do. */
2422 if (crtl->eh.exception_handler_label_map == NULL)
2426 slot = (struct ehl_map_entry **)
2427 htab_find_slot (crtl->eh.exception_handler_label_map, &tmp, NO_INSERT);
2430 htab_clear_slot (crtl->eh.exception_handler_label_map, (void **) slot);
2433 /* Splice REGION from the region tree and replace it by REPLACE etc. */
2436 remove_eh_handler_and_replace (struct eh_region *region,
2437 struct eh_region *replace)
2439 struct eh_region **pp, **pp_start, *p, *outer, *inner;
2442 outer = region->outer;
2443 /* For the benefit of efficiently handling REG_EH_REGION notes,
2444 replace this region in the region array with its containing
2445 region. Note that previous region deletions may result in
2446 multiple copies of this region in the array, so we have a
2447 list of alternate numbers by which we are known. */
2449 VEC_replace (eh_region, cfun->eh->region_array, region->region_number,
2456 EXECUTE_IF_SET_IN_BITMAP (region->aka, 0, i, bi)
2458 VEC_replace (eh_region, cfun->eh->region_array, i, replace);
2465 replace->aka = BITMAP_GGC_ALLOC ();
2467 bitmap_ior_into (replace->aka, region->aka);
2468 bitmap_set_bit (replace->aka, region->region_number);
2471 if (crtl->eh.built_landing_pads)
2472 lab = region->landing_pad;
2474 lab = region->label;
2476 remove_exception_handler_label (lab);
2479 pp_start = &outer->inner;
2481 pp_start = &cfun->eh->region_tree;
2482 for (pp = pp_start, p = *pp; p != region; pp = &p->next_peer, p = *pp)
2484 *pp = region->next_peer;
2487 pp_start = &replace->inner;
2489 pp_start = &cfun->eh->region_tree;
2490 inner = region->inner;
2493 for (p = inner; p->next_peer ; p = p->next_peer)
2497 p->next_peer = *pp_start;
2501 if (region->type == ERT_CATCH)
2503 struct eh_region *eh_try, *next, *prev;
2505 for (eh_try = region->next_peer;
2506 eh_try->type == ERT_CATCH;
2507 eh_try = eh_try->next_peer)
2509 gcc_assert (eh_try->type == ERT_TRY);
2511 next = region->u.eh_catch.next_catch;
2512 prev = region->u.eh_catch.prev_catch;
2515 next->u.eh_catch.prev_catch = prev;
2517 eh_try->u.eh_try.last_catch = prev;
2519 prev->u.eh_catch.next_catch = next;
2522 eh_try->u.eh_try.eh_catch = next;
2524 remove_eh_handler (eh_try);
2529 /* Splice REGION from the region tree and replace it by the outer region
2533 remove_eh_handler (struct eh_region *region)
2535 remove_eh_handler_and_replace (region, region->outer);
2538 /* LABEL heads a basic block that is about to be deleted. If this
2539 label corresponds to an exception region, we may be able to
2540 delete the region. */
2543 maybe_remove_eh_handler (rtx label)
2545 struct ehl_map_entry **slot, tmp;
2546 struct eh_region *region;
2548 /* ??? After generating landing pads, it's not so simple to determine
2549 if the region data is completely unused. One must examine the
2550 landing pad and the post landing pad, and whether an inner try block
2551 is referencing the catch handlers directly. */
2552 if (crtl->eh.built_landing_pads)
2556 slot = (struct ehl_map_entry **)
2557 htab_find_slot (crtl->eh.exception_handler_label_map, &tmp, NO_INSERT);
2560 region = (*slot)->region;
2564 /* Flow will want to remove MUST_NOT_THROW regions as unreachable
2565 because there is no path to the fallback call to terminate.
2566 But the region continues to affect call-site data until there
2567 are no more contained calls, which we don't see here. */
2568 if (region->type == ERT_MUST_NOT_THROW)
2570 htab_clear_slot (crtl->eh.exception_handler_label_map, (void **) slot);
2571 region->label = NULL_RTX;
2574 remove_eh_handler (region);
2577 /* Remove Eh region R that has turned out to have no code in its handler. */
2580 remove_eh_region (int r)
2582 struct eh_region *region;
2584 region = VEC_index (eh_region, cfun->eh->region_array, r);
2585 remove_eh_handler (region);
2588 /* Invokes CALLBACK for every exception handler label. Only used by old
2589 loop hackery; should not be used by new code. */
2592 for_each_eh_label (void (*callback) (rtx))
2594 htab_traverse (crtl->eh.exception_handler_label_map, for_each_eh_label_1,
2595 (void *) &callback);
2599 for_each_eh_label_1 (void **pentry, void *data)
2601 struct ehl_map_entry *entry = *(struct ehl_map_entry **)pentry;
2602 void (*callback) (rtx) = *(void (**) (rtx)) data;
2604 (*callback) (entry->label);
2608 /* Invoke CALLBACK for every exception region in the current function. */
2611 for_each_eh_region (void (*callback) (struct eh_region *))
2613 int i, n = cfun->eh->last_region_number;
2614 for (i = 1; i <= n; ++i)
2616 struct eh_region *region;
2618 region = VEC_index (eh_region, cfun->eh->region_array, i);
2620 (*callback) (region);
2624 /* This section describes CFG exception edges for flow. */
2626 /* For communicating between calls to reachable_next_level. */
2627 struct reachable_info
2631 void (*callback) (struct eh_region *, void *);
2632 void *callback_data;
2635 /* A subroutine of reachable_next_level. Return true if TYPE, or a
2636 base class of TYPE, is in HANDLED. */
2639 check_handled (tree handled, tree type)
2643 /* We can check for exact matches without front-end help. */
2644 if (! lang_eh_type_covers)
2646 for (t = handled; t ; t = TREE_CHAIN (t))
2647 if (TREE_VALUE (t) == type)
2652 for (t = handled; t ; t = TREE_CHAIN (t))
2653 if ((*lang_eh_type_covers) (TREE_VALUE (t), type))
2660 /* A subroutine of reachable_next_level. If we are collecting a list
2661 of handlers, add one. After landing pad generation, reference
2662 it instead of the handlers themselves. Further, the handlers are
2663 all wired together, so by referencing one, we've got them all.
2664 Before landing pad generation we reference each handler individually.
2666 LP_REGION contains the landing pad; REGION is the handler. */
2669 add_reachable_handler (struct reachable_info *info,
2670 struct eh_region *lp_region, struct eh_region *region)
2675 if (crtl->eh.built_landing_pads)
2676 info->callback (lp_region, info->callback_data);
2678 info->callback (region, info->callback_data);
2681 /* Process one level of exception regions for reachability.
2682 If TYPE_THROWN is non-null, then it is the *exact* type being
2683 propagated. If INFO is non-null, then collect handler labels
2684 and caught/allowed type information between invocations. */
2686 static enum reachable_code
2687 reachable_next_level (struct eh_region *region, tree type_thrown,
2688 struct reachable_info *info,
2691 switch (region->type)
2694 /* Before landing-pad generation, we model control flow
2695 directly to the individual handlers. In this way we can
2696 see that catch handler types may shadow one another. */
2697 add_reachable_handler (info, region, region);
2698 return RNL_MAYBE_CAUGHT;
2702 struct eh_region *c;
2703 enum reachable_code ret = RNL_NOT_CAUGHT;
2705 for (c = region->u.eh_try.eh_catch; c ; c = c->u.eh_catch.next_catch)
2707 /* A catch-all handler ends the search. */
2708 if (c->u.eh_catch.type_list == NULL)
2710 add_reachable_handler (info, region, c);
2716 /* If we have at least one type match, end the search. */
2717 tree tp_node = c->u.eh_catch.type_list;
2719 for (; tp_node; tp_node = TREE_CHAIN (tp_node))
2721 tree type = TREE_VALUE (tp_node);
2723 if (type == type_thrown
2724 || (lang_eh_type_covers
2725 && (*lang_eh_type_covers) (type, type_thrown)))
2727 add_reachable_handler (info, region, c);
2732 /* If we have definitive information of a match failure,
2733 the catch won't trigger. */
2734 if (lang_eh_type_covers)
2735 return RNL_NOT_CAUGHT;
2738 /* At this point, we either don't know what type is thrown or
2739 don't have front-end assistance to help deciding if it is
2740 covered by one of the types in the list for this region.
2742 We'd then like to add this region to the list of reachable
2743 handlers since it is indeed potentially reachable based on the
2744 information we have.
2746 Actually, this handler is for sure not reachable if all the
2747 types it matches have already been caught. That is, it is only
2748 potentially reachable if at least one of the types it catches
2749 has not been previously caught. */
2752 ret = RNL_MAYBE_CAUGHT;
2755 tree tp_node = c->u.eh_catch.type_list;
2756 bool maybe_reachable = false;
2758 /* Compute the potential reachability of this handler and
2759 update the list of types caught at the same time. */
2760 for (; tp_node; tp_node = TREE_CHAIN (tp_node))
2762 tree type = TREE_VALUE (tp_node);
2764 if (! check_handled (info->types_caught, type))
2767 = tree_cons (NULL, type, info->types_caught);
2769 maybe_reachable = true;
2773 if (maybe_reachable)
2775 add_reachable_handler (info, region, c);
2777 /* ??? If the catch type is a base class of every allowed
2778 type, then we know we can stop the search. */
2779 ret = RNL_MAYBE_CAUGHT;
2787 case ERT_ALLOWED_EXCEPTIONS:
2788 /* An empty list of types definitely ends the search. */
2789 if (region->u.allowed.type_list == NULL_TREE)
2791 add_reachable_handler (info, region, region);
2795 /* Collect a list of lists of allowed types for use in detecting
2796 when a catch may be transformed into a catch-all. */
2798 info->types_allowed = tree_cons (NULL_TREE,
2799 region->u.allowed.type_list,
2800 info->types_allowed);
2802 /* If we have definitive information about the type hierarchy,
2803 then we can tell if the thrown type will pass through the
2805 if (type_thrown && lang_eh_type_covers)
2807 if (check_handled (region->u.allowed.type_list, type_thrown))
2808 return RNL_NOT_CAUGHT;
2811 add_reachable_handler (info, region, region);
2816 add_reachable_handler (info, region, region);
2817 return RNL_MAYBE_CAUGHT;
2820 /* Catch regions are handled by their controlling try region. */
2821 return RNL_NOT_CAUGHT;
2823 case ERT_MUST_NOT_THROW:
2824 /* Here we end our search, since no exceptions may propagate.
2826 Local landing pads of ERT_MUST_NOT_THROW instructions are reachable
2827 only via locally handled RESX instructions.
2829 When we inline a function call, we can bring in new handlers. In order
2830 to avoid ERT_MUST_NOT_THROW landing pads from being deleted as unreachable
2831 assume that such handlers exists prior for any inlinable call prior
2832 inlining decisions are fixed. */
2836 add_reachable_handler (info, region, region);
2844 /* Shouldn't see these here. */
2852 /* Invoke CALLBACK on each region reachable from REGION_NUMBER. */
2855 foreach_reachable_handler (int region_number, bool is_resx, bool inlinable_call,
2856 void (*callback) (struct eh_region *, void *),
2857 void *callback_data)
2859 struct reachable_info info;
2860 struct eh_region *region;
2863 memset (&info, 0, sizeof (info));
2864 info.callback = callback;
2865 info.callback_data = callback_data;
2867 region = VEC_index (eh_region, cfun->eh->region_array, region_number);
2871 type_thrown = NULL_TREE;
2874 /* A RESX leaves a region instead of entering it. Thus the
2875 region itself may have been deleted out from under us. */
2878 region = region->outer;
2880 else if (region->type == ERT_THROW)
2882 type_thrown = region->u.eh_throw.type;
2883 region = region->outer;
2888 if (reachable_next_level (region, type_thrown, &info,
2889 inlinable_call || is_resx) >= RNL_CAUGHT)
2891 /* If we have processed one cleanup, there is no point in
2892 processing any more of them. Each cleanup will have an edge
2893 to the next outer cleanup region, so the flow graph will be
2895 if (region->type == ERT_CLEANUP)
2896 region = region->u.cleanup.prev_try;
2898 region = region->outer;
2902 /* Retrieve a list of labels of exception handlers which can be
2903 reached by a given insn. */
2906 arh_to_landing_pad (struct eh_region *region, void *data)
2908 rtx *p_handlers = (rtx *) data;
2910 *p_handlers = alloc_INSN_LIST (region->landing_pad, NULL_RTX);
2914 arh_to_label (struct eh_region *region, void *data)
2916 rtx *p_handlers = (rtx *) data;
2917 *p_handlers = alloc_INSN_LIST (region->label, *p_handlers);
2921 reachable_handlers (rtx insn)
2923 bool is_resx = false;
2924 rtx handlers = NULL;
2928 && GET_CODE (PATTERN (insn)) == RESX)
2930 region_number = XINT (PATTERN (insn), 0);
2935 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2936 if (!note || INTVAL (XEXP (note, 0)) <= 0)
2938 region_number = INTVAL (XEXP (note, 0));
2941 foreach_reachable_handler (region_number, is_resx, false,
2942 (crtl->eh.built_landing_pads
2943 ? arh_to_landing_pad
2950 /* Determine if the given INSN can throw an exception that is caught
2951 within the function. */
2954 can_throw_internal_1 (int region_number, bool is_resx, bool inlinable_call)
2956 struct eh_region *region;
2959 region = VEC_index (eh_region, cfun->eh->region_array, region_number);
2963 type_thrown = NULL_TREE;
2965 region = region->outer;
2966 else if (region->type == ERT_THROW)
2968 type_thrown = region->u.eh_throw.type;
2969 region = region->outer;
2972 /* If this exception is ignored by each and every containing region,
2973 then control passes straight out. The runtime may handle some
2974 regions, which also do not require processing internally. */
2975 for (; region; region = region->outer)
2977 enum reachable_code how = reachable_next_level (region, type_thrown, 0,
2978 inlinable_call || is_resx);
2979 if (how == RNL_BLOCKED)
2981 if (how != RNL_NOT_CAUGHT)
2989 can_throw_internal (const_rtx insn)
2993 if (! INSN_P (insn))
2997 && GET_CODE (PATTERN (insn)) == RESX
2998 && XINT (PATTERN (insn), 0) > 0)
2999 return can_throw_internal_1 (XINT (PATTERN (insn), 0), true, false);
3001 if (NONJUMP_INSN_P (insn)
3002 && GET_CODE (PATTERN (insn)) == SEQUENCE)
3003 insn = XVECEXP (PATTERN (insn), 0, 0);
3005 /* Every insn that might throw has an EH_REGION note. */
3006 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3007 if (!note || INTVAL (XEXP (note, 0)) <= 0)
3010 return can_throw_internal_1 (INTVAL (XEXP (note, 0)), false, false);
3013 /* Determine if the given INSN can throw an exception that is
3014 visible outside the function. */
3017 can_throw_external_1 (int region_number, bool is_resx, bool inlinable_call)
3019 struct eh_region *region;
3022 region = VEC_index (eh_region, cfun->eh->region_array, region_number);
3026 type_thrown = NULL_TREE;
3028 region = region->outer;
3029 else if (region->type == ERT_THROW)
3031 type_thrown = region->u.eh_throw.type;
3032 region = region->outer;
3035 /* If the exception is caught or blocked by any containing region,
3036 then it is not seen by any calling function. */
3037 for (; region ; region = region->outer)
3038 if (reachable_next_level (region, type_thrown, NULL,
3039 inlinable_call || is_resx) >= RNL_CAUGHT)
3046 can_throw_external (const_rtx insn)
3050 if (! INSN_P (insn))
3054 && GET_CODE (PATTERN (insn)) == RESX
3055 && XINT (PATTERN (insn), 0) > 0)
3056 return can_throw_external_1 (XINT (PATTERN (insn), 0), true, false);
3058 if (NONJUMP_INSN_P (insn)
3059 && GET_CODE (PATTERN (insn)) == SEQUENCE)
3060 insn = XVECEXP (PATTERN (insn), 0, 0);
3062 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3065 /* Calls (and trapping insns) without notes are outside any
3066 exception handling region in this function. We have to
3067 assume it might throw. Given that the front end and middle
3068 ends mark known NOTHROW functions, this isn't so wildly
3070 return (CALL_P (insn)
3071 || (flag_non_call_exceptions
3072 && may_trap_p (PATTERN (insn))));
3074 if (INTVAL (XEXP (note, 0)) <= 0)
3077 return can_throw_external_1 (INTVAL (XEXP (note, 0)), false, false);
3080 /* Set TREE_NOTHROW and crtl->all_throwers_are_sibcalls. */
3083 set_nothrow_function_flags (void)
3089 /* Assume crtl->all_throwers_are_sibcalls until we encounter
3090 something that can throw an exception. We specifically exempt
3091 CALL_INSNs that are SIBLING_CALL_P, as these are really jumps,
3092 and can't throw. Most CALL_INSNs are not SIBLING_CALL_P, so this
3095 crtl->all_throwers_are_sibcalls = 1;
3097 /* If we don't know that this implementation of the function will
3098 actually be used, then we must not set TREE_NOTHROW, since
3099 callers must not assume that this function does not throw. */
3100 if (TREE_NOTHROW (current_function_decl))
3103 if (! flag_exceptions)
3106 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3107 if (can_throw_external (insn))
3111 if (!CALL_P (insn) || !SIBLING_CALL_P (insn))
3113 crtl->all_throwers_are_sibcalls = 0;
3118 for (insn = crtl->epilogue_delay_list; insn;
3119 insn = XEXP (insn, 1))
3120 if (can_throw_external (insn))
3124 if (!CALL_P (insn) || !SIBLING_CALL_P (insn))
3126 crtl->all_throwers_are_sibcalls = 0;
3131 && (cgraph_function_body_availability (cgraph_node (current_function_decl))
3132 >= AVAIL_AVAILABLE))
3133 TREE_NOTHROW (current_function_decl) = 1;
3137 struct rtl_opt_pass pass_set_nothrow_function_flags =
3143 set_nothrow_function_flags, /* execute */
3146 0, /* static_pass_number */
3148 0, /* properties_required */
3149 0, /* properties_provided */
3150 0, /* properties_destroyed */
3151 0, /* todo_flags_start */
3152 0, /* todo_flags_finish */
3157 /* Various hooks for unwind library. */
3159 /* Do any necessary initialization to access arbitrary stack frames.
3160 On the SPARC, this means flushing the register windows. */
3163 expand_builtin_unwind_init (void)
3165 /* Set this so all the registers get saved in our frame; we need to be
3166 able to copy the saved values for any registers from frames we unwind. */
3167 crtl->saves_all_registers = 1;
3169 #ifdef SETUP_FRAME_ADDRESSES
3170 SETUP_FRAME_ADDRESSES ();
3175 expand_builtin_eh_return_data_regno (tree exp)
3177 tree which = CALL_EXPR_ARG (exp, 0);
3178 unsigned HOST_WIDE_INT iwhich;
3180 if (TREE_CODE (which) != INTEGER_CST)
3182 error ("argument of %<__builtin_eh_return_regno%> must be constant");
3186 iwhich = tree_low_cst (which, 1);
3187 iwhich = EH_RETURN_DATA_REGNO (iwhich);
3188 if (iwhich == INVALID_REGNUM)
3191 #ifdef DWARF_FRAME_REGNUM
3192 iwhich = DWARF_FRAME_REGNUM (iwhich);
3194 iwhich = DBX_REGISTER_NUMBER (iwhich);
3197 return GEN_INT (iwhich);
3200 /* Given a value extracted from the return address register or stack slot,
3201 return the actual address encoded in that value. */
3204 expand_builtin_extract_return_addr (tree addr_tree)
3206 rtx addr = expand_expr (addr_tree, NULL_RTX, Pmode, EXPAND_NORMAL);
3208 if (GET_MODE (addr) != Pmode
3209 && GET_MODE (addr) != VOIDmode)
3211 #ifdef POINTERS_EXTEND_UNSIGNED
3212 addr = convert_memory_address (Pmode, addr);
3214 addr = convert_to_mode (Pmode, addr, 0);
3218 /* First mask out any unwanted bits. */
3219 #ifdef MASK_RETURN_ADDR
3220 expand_and (Pmode, addr, MASK_RETURN_ADDR, addr);
3223 /* Then adjust to find the real return address. */
3224 #if defined (RETURN_ADDR_OFFSET)
3225 addr = plus_constant (addr, RETURN_ADDR_OFFSET);
3231 /* Given an actual address in addr_tree, do any necessary encoding
3232 and return the value to be stored in the return address register or
3233 stack slot so the epilogue will return to that address. */
3236 expand_builtin_frob_return_addr (tree addr_tree)
3238 rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, EXPAND_NORMAL);
3240 addr = convert_memory_address (Pmode, addr);
3242 #ifdef RETURN_ADDR_OFFSET
3243 addr = force_reg (Pmode, addr);
3244 addr = plus_constant (addr, -RETURN_ADDR_OFFSET);
3250 /* Set up the epilogue with the magic bits we'll need to return to the
3251 exception handler. */
3254 expand_builtin_eh_return (tree stackadj_tree ATTRIBUTE_UNUSED,
3259 #ifdef EH_RETURN_STACKADJ_RTX
3260 tmp = expand_expr (stackadj_tree, crtl->eh.ehr_stackadj,
3261 VOIDmode, EXPAND_NORMAL);
3262 tmp = convert_memory_address (Pmode, tmp);
3263 if (!crtl->eh.ehr_stackadj)
3264 crtl->eh.ehr_stackadj = copy_to_reg (tmp);
3265 else if (tmp != crtl->eh.ehr_stackadj)
3266 emit_move_insn (crtl->eh.ehr_stackadj, tmp);
3269 tmp = expand_expr (handler_tree, crtl->eh.ehr_handler,
3270 VOIDmode, EXPAND_NORMAL);
3271 tmp = convert_memory_address (Pmode, tmp);
3272 if (!crtl->eh.ehr_handler)
3273 crtl->eh.ehr_handler = copy_to_reg (tmp);
3274 else if (tmp != crtl->eh.ehr_handler)
3275 emit_move_insn (crtl->eh.ehr_handler, tmp);
3277 if (!crtl->eh.ehr_label)
3278 crtl->eh.ehr_label = gen_label_rtx ();
3279 emit_jump (crtl->eh.ehr_label);
3283 expand_eh_return (void)
3287 if (! crtl->eh.ehr_label)
3290 crtl->calls_eh_return = 1;
3292 #ifdef EH_RETURN_STACKADJ_RTX
3293 emit_move_insn (EH_RETURN_STACKADJ_RTX, const0_rtx);
3296 around_label = gen_label_rtx ();
3297 emit_jump (around_label);
3299 emit_label (crtl->eh.ehr_label);
3300 clobber_return_register ();
3302 #ifdef EH_RETURN_STACKADJ_RTX
3303 emit_move_insn (EH_RETURN_STACKADJ_RTX, crtl->eh.ehr_stackadj);
3306 #ifdef HAVE_eh_return
3308 emit_insn (gen_eh_return (crtl->eh.ehr_handler));
3312 #ifdef EH_RETURN_HANDLER_RTX
3313 emit_move_insn (EH_RETURN_HANDLER_RTX, crtl->eh.ehr_handler);
3315 error ("__builtin_eh_return not supported on this target");
3319 emit_label (around_label);
3322 /* Convert a ptr_mode address ADDR_TREE to a Pmode address controlled by
3323 POINTERS_EXTEND_UNSIGNED and return it. */
3326 expand_builtin_extend_pointer (tree addr_tree)
3328 rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, EXPAND_NORMAL);
3331 #ifdef POINTERS_EXTEND_UNSIGNED
3332 extend = POINTERS_EXTEND_UNSIGNED;
3334 /* The previous EH code did an unsigned extend by default, so we do this also
3339 return convert_modes (targetm.unwind_word_mode (), ptr_mode, addr, extend);
3342 /* In the following functions, we represent entries in the action table
3343 as 1-based indices. Special cases are:
3345 0: null action record, non-null landing pad; implies cleanups
3346 -1: null action record, null landing pad; implies no action
3347 -2: no call-site entry; implies must_not_throw
3348 -3: we have yet to process outer regions
3350 Further, no special cases apply to the "next" field of the record.
3351 For next, 0 means end of list. */
3353 struct action_record
3361 action_record_eq (const void *pentry, const void *pdata)
3363 const struct action_record *entry = (const struct action_record *) pentry;
3364 const struct action_record *data = (const struct action_record *) pdata;
3365 return entry->filter == data->filter && entry->next == data->next;
3369 action_record_hash (const void *pentry)
3371 const struct action_record *entry = (const struct action_record *) pentry;
3372 return entry->next * 1009 + entry->filter;
3376 add_action_record (htab_t ar_hash, int filter, int next)
3378 struct action_record **slot, *new_ar, tmp;
3380 tmp.filter = filter;
3382 slot = (struct action_record **) htab_find_slot (ar_hash, &tmp, INSERT);
3384 if ((new_ar = *slot) == NULL)
3386 new_ar = XNEW (struct action_record);
3387 new_ar->offset = VARRAY_ACTIVE_SIZE (crtl->eh.action_record_data) + 1;
3388 new_ar->filter = filter;
3389 new_ar->next = next;
3392 /* The filter value goes in untouched. The link to the next
3393 record is a "self-relative" byte offset, or zero to indicate
3394 that there is no next record. So convert the absolute 1 based
3395 indices we've been carrying around into a displacement. */
3397 push_sleb128 (&crtl->eh.action_record_data, filter);
3399 next -= VARRAY_ACTIVE_SIZE (crtl->eh.action_record_data) + 1;
3400 push_sleb128 (&crtl->eh.action_record_data, next);
3403 return new_ar->offset;
3407 collect_one_action_chain (htab_t ar_hash, struct eh_region *region)
3409 struct eh_region *c;
3412 /* If we've reached the top of the region chain, then we have
3413 no actions, and require no landing pad. */
3417 switch (region->type)
3420 /* A cleanup adds a zero filter to the beginning of the chain, but
3421 there are special cases to look out for. If there are *only*
3422 cleanups along a path, then it compresses to a zero action.
3423 Further, if there are multiple cleanups along a path, we only
3424 need to represent one of them, as that is enough to trigger
3425 entry to the landing pad at runtime. */
3426 next = collect_one_action_chain (ar_hash, region->outer);
3429 for (c = region->outer; c ; c = c->outer)
3430 if (c->type == ERT_CLEANUP)
3432 return add_action_record (ar_hash, 0, next);
3435 /* Process the associated catch regions in reverse order.
3436 If there's a catch-all handler, then we don't need to
3437 search outer regions. Use a magic -3 value to record
3438 that we haven't done the outer search. */
3440 for (c = region->u.eh_try.last_catch; c ; c = c->u.eh_catch.prev_catch)
3442 if (c->u.eh_catch.type_list == NULL)
3444 /* Retrieve the filter from the head of the filter list
3445 where we have stored it (see assign_filter_values). */
3447 = TREE_INT_CST_LOW (TREE_VALUE (c->u.eh_catch.filter_list));
3449 next = add_action_record (ar_hash, filter, 0);
3453 /* Once the outer search is done, trigger an action record for
3454 each filter we have. */
3459 next = collect_one_action_chain (ar_hash, region->outer);
3461 /* If there is no next action, terminate the chain. */
3464 /* If all outer actions are cleanups or must_not_throw,
3465 we'll have no action record for it, since we had wanted
3466 to encode these states in the call-site record directly.
3467 Add a cleanup action to the chain to catch these. */
3469 next = add_action_record (ar_hash, 0, 0);
3472 flt_node = c->u.eh_catch.filter_list;
3473 for (; flt_node; flt_node = TREE_CHAIN (flt_node))
3475 int filter = TREE_INT_CST_LOW (TREE_VALUE (flt_node));
3476 next = add_action_record (ar_hash, filter, next);
3482 case ERT_ALLOWED_EXCEPTIONS:
3483 /* An exception specification adds its filter to the
3484 beginning of the chain. */
3485 next = collect_one_action_chain (ar_hash, region->outer);
3487 /* If there is no next action, terminate the chain. */
3490 /* If all outer actions are cleanups or must_not_throw,
3491 we'll have no action record for it, since we had wanted
3492 to encode these states in the call-site record directly.
3493 Add a cleanup action to the chain to catch these. */
3495 next = add_action_record (ar_hash, 0, 0);
3497 return add_action_record (ar_hash, region->u.allowed.filter, next);
3499 case ERT_MUST_NOT_THROW:
3500 /* A must-not-throw region with no inner handlers or cleanups
3501 requires no call-site entry. Note that this differs from
3502 the no handler or cleanup case in that we do require an lsda
3503 to be generated. Return a magic -2 value to record this. */
3508 /* CATCH regions are handled in TRY above. THROW regions are
3509 for optimization information only and produce no output. */
3510 return collect_one_action_chain (ar_hash, region->outer);
3518 add_call_site (rtx landing_pad, int action)
3520 call_site_record record;
3522 record = GGC_NEW (struct call_site_record);
3523 record->landing_pad = landing_pad;
3524 record->action = action;
3526 VEC_safe_push (call_site_record, gc, crtl->eh.call_site_record, record);
3528 return call_site_base + VEC_length (call_site_record, crtl->eh.call_site_record) - 1;
3531 /* Turn REG_EH_REGION notes back into NOTE_INSN_EH_REGION notes.
3532 The new note numbers will not refer to region numbers, but
3533 instead to call site entries. */
3536 convert_to_eh_region_ranges (void)
3538 rtx insn, iter, note;
3540 int last_action = -3;
3541 rtx last_action_insn = NULL_RTX;
3542 rtx last_landing_pad = NULL_RTX;
3543 rtx first_no_action_insn = NULL_RTX;
3546 if (USING_SJLJ_EXCEPTIONS || cfun->eh->region_tree == NULL)
3549 VARRAY_UCHAR_INIT (crtl->eh.action_record_data, 64, "action_record_data");
3551 ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
3553 for (iter = get_insns (); iter ; iter = NEXT_INSN (iter))
3556 struct eh_region *region;
3558 rtx this_landing_pad;
3561 if (NONJUMP_INSN_P (insn)
3562 && GET_CODE (PATTERN (insn)) == SEQUENCE)
3563 insn = XVECEXP (PATTERN (insn), 0, 0);
3565 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3568 if (! (CALL_P (insn)
3569 || (flag_non_call_exceptions
3570 && may_trap_p (PATTERN (insn)))))
3577 if (INTVAL (XEXP (note, 0)) <= 0)
3579 region = VEC_index (eh_region, cfun->eh->region_array, INTVAL (XEXP (note, 0)));
3580 this_action = collect_one_action_chain (ar_hash, region);
3583 /* Existence of catch handlers, or must-not-throw regions
3584 implies that an lsda is needed (even if empty). */
3585 if (this_action != -1)
3586 crtl->uses_eh_lsda = 1;
3588 /* Delay creation of region notes for no-action regions
3589 until we're sure that an lsda will be required. */
3590 else if (last_action == -3)
3592 first_no_action_insn = iter;
3596 /* Cleanups and handlers may share action chains but not
3597 landing pads. Collect the landing pad for this region. */
3598 if (this_action >= 0)
3600 struct eh_region *o;
3601 for (o = region; ! o->landing_pad ; o = o->outer)
3603 this_landing_pad = o->landing_pad;
3606 this_landing_pad = NULL_RTX;
3608 /* Differing actions or landing pads implies a change in call-site
3609 info, which implies some EH_REGION note should be emitted. */
3610 if (last_action != this_action
3611 || last_landing_pad != this_landing_pad)
3613 /* If we'd not seen a previous action (-3) or the previous
3614 action was must-not-throw (-2), then we do not need an
3616 if (last_action >= -1)
3618 /* If we delayed the creation of the begin, do it now. */
3619 if (first_no_action_insn)
3621 call_site = add_call_site (NULL_RTX, 0);
3622 note = emit_note_before (NOTE_INSN_EH_REGION_BEG,
3623 first_no_action_insn);
3624 NOTE_EH_HANDLER (note) = call_site;
3625 first_no_action_insn = NULL_RTX;
3628 note = emit_note_after (NOTE_INSN_EH_REGION_END,
3630 NOTE_EH_HANDLER (note) = call_site;
3633 /* If the new action is must-not-throw, then no region notes
3635 if (this_action >= -1)
3637 call_site = add_call_site (this_landing_pad,
3638 this_action < 0 ? 0 : this_action);
3639 note = emit_note_before (NOTE_INSN_EH_REGION_BEG, iter);
3640 NOTE_EH_HANDLER (note) = call_site;
3643 last_action = this_action;
3644 last_landing_pad = this_landing_pad;
3646 last_action_insn = iter;
3649 if (last_action >= -1 && ! first_no_action_insn)
3651 note = emit_note_after (NOTE_INSN_EH_REGION_END, last_action_insn);
3652 NOTE_EH_HANDLER (note) = call_site;
3655 htab_delete (ar_hash);
3659 struct rtl_opt_pass pass_convert_to_eh_region_ranges =
3663 "eh_ranges", /* name */
3665 convert_to_eh_region_ranges, /* execute */
3668 0, /* static_pass_number */
3670 0, /* properties_required */
3671 0, /* properties_provided */
3672 0, /* properties_destroyed */
3673 0, /* todo_flags_start */
3674 TODO_dump_func, /* todo_flags_finish */
3680 push_uleb128 (varray_type *data_area, unsigned int value)
3684 unsigned char byte = value & 0x7f;
3688 VARRAY_PUSH_UCHAR (*data_area, byte);
3694 push_sleb128 (varray_type *data_area, int value)
3701 byte = value & 0x7f;
3703 more = ! ((value == 0 && (byte & 0x40) == 0)
3704 || (value == -1 && (byte & 0x40) != 0));
3707 VARRAY_PUSH_UCHAR (*data_area, byte);
3713 #ifndef HAVE_AS_LEB128
3715 dw2_size_of_call_site_table (void)
3717 int n = VEC_length (call_site_record, crtl->eh.call_site_record);
3718 int size = n * (4 + 4 + 4);
3721 for (i = 0; i < n; ++i)
3723 struct call_site_record *cs = VEC_index (call_site_record, crtl->eh.call_site_record, i);
3724 size += size_of_uleb128 (cs->action);
3731 sjlj_size_of_call_site_table (void)
3733 int n = VEC_length (call_site_record, crtl->eh.call_site_record);
3737 for (i = 0; i < n; ++i)
3739 struct call_site_record *cs = VEC_index (call_site_record, crtl->eh.call_site_record, i);
3740 size += size_of_uleb128 (INTVAL (cs->landing_pad));
3741 size += size_of_uleb128 (cs->action);
3749 dw2_output_call_site_table (void)
3751 int n = VEC_length (call_site_record, crtl->eh.call_site_record);
3754 for (i = 0; i < n; ++i)
3756 struct call_site_record *cs = VEC_index (call_site_record, crtl->eh.call_site_record, i);
3757 char reg_start_lab[32];
3758 char reg_end_lab[32];
3759 char landing_pad_lab[32];
3761 ASM_GENERATE_INTERNAL_LABEL (reg_start_lab, "LEHB", call_site_base + i);
3762 ASM_GENERATE_INTERNAL_LABEL (reg_end_lab, "LEHE", call_site_base + i);
3764 if (cs->landing_pad)
3765 ASM_GENERATE_INTERNAL_LABEL (landing_pad_lab, "L",
3766 CODE_LABEL_NUMBER (cs->landing_pad));
3768 /* ??? Perhaps use insn length scaling if the assembler supports
3769 generic arithmetic. */
3770 /* ??? Perhaps use attr_length to choose data1 or data2 instead of
3771 data4 if the function is small enough. */
3772 #ifdef HAVE_AS_LEB128
3773 dw2_asm_output_delta_uleb128 (reg_start_lab,
3774 current_function_func_begin_label,
3775 "region %d start", i);
3776 dw2_asm_output_delta_uleb128 (reg_end_lab, reg_start_lab,
3778 if (cs->landing_pad)
3779 dw2_asm_output_delta_uleb128 (landing_pad_lab,
3780 current_function_func_begin_label,
3783 dw2_asm_output_data_uleb128 (0, "landing pad");
3785 dw2_asm_output_delta (4, reg_start_lab,
3786 current_function_func_begin_label,
3787 "region %d start", i);
3788 dw2_asm_output_delta (4, reg_end_lab, reg_start_lab, "length");
3789 if (cs->landing_pad)
3790 dw2_asm_output_delta (4, landing_pad_lab,
3791 current_function_func_begin_label,
3794 dw2_asm_output_data (4, 0, "landing pad");
3796 dw2_asm_output_data_uleb128 (cs->action, "action");
3799 call_site_base += n;
3803 sjlj_output_call_site_table (void)
3805 int n = VEC_length (call_site_record, crtl->eh.call_site_record);
3808 for (i = 0; i < n; ++i)
3810 struct call_site_record *cs = VEC_index (call_site_record, crtl->eh.call_site_record, i);
3812 dw2_asm_output_data_uleb128 (INTVAL (cs->landing_pad),
3813 "region %d landing pad", i);
3814 dw2_asm_output_data_uleb128 (cs->action, "action");
3817 call_site_base += n;
3820 #ifndef TARGET_UNWIND_INFO
3821 /* Switch to the section that should be used for exception tables. */
3824 switch_to_exception_section (const char * ARG_UNUSED (fnname))
3828 if (exception_section)
3829 s = exception_section;
3832 /* Compute the section and cache it into exception_section,
3833 unless it depends on the function name. */
3834 if (targetm.have_named_sections)
3838 if (EH_TABLES_CAN_BE_READ_ONLY)
3841 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3842 flags = ((! flag_pic
3843 || ((tt_format & 0x70) != DW_EH_PE_absptr
3844 && (tt_format & 0x70) != DW_EH_PE_aligned))
3845 ? 0 : SECTION_WRITE);
3848 flags = SECTION_WRITE;
3850 #ifdef HAVE_LD_EH_GC_SECTIONS
3851 if (flag_function_sections)
3853 char *section_name = XNEWVEC (char, strlen (fnname) + 32);
3854 sprintf (section_name, ".gcc_except_table.%s", fnname);
3855 s = get_section (section_name, flags, NULL);
3856 free (section_name);
3861 = s = get_section (".gcc_except_table", flags, NULL);
3865 = s = flag_pic ? data_section : readonly_data_section;
3868 switch_to_section (s);
3873 /* Output a reference from an exception table to the type_info object TYPE.
3874 TT_FORMAT and TT_FORMAT_SIZE describe the DWARF encoding method used for
3878 output_ttype (tree type, int tt_format, int tt_format_size)
3881 bool is_public = true;
3883 if (type == NULL_TREE)
3887 struct varpool_node *node;
3889 type = lookup_type_for_runtime (type);
3890 value = expand_expr (type, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
3892 /* Let cgraph know that the rtti decl is used. Not all of the
3893 paths below go through assemble_integer, which would take
3894 care of this for us. */
3896 if (TREE_CODE (type) == ADDR_EXPR)
3898 type = TREE_OPERAND (type, 0);
3899 if (TREE_CODE (type) == VAR_DECL)
3901 node = varpool_node (type);
3903 varpool_mark_needed_node (node);
3904 is_public = TREE_PUBLIC (type);
3908 gcc_assert (TREE_CODE (type) == INTEGER_CST);
3911 /* Allow the target to override the type table entry format. */
3912 if (targetm.asm_out.ttype (value))
3915 if (tt_format == DW_EH_PE_absptr || tt_format == DW_EH_PE_aligned)
3916 assemble_integer (value, tt_format_size,
3917 tt_format_size * BITS_PER_UNIT, 1);
3919 dw2_asm_output_encoded_addr_rtx (tt_format, value, is_public, NULL);
3923 output_function_exception_table (const char * ARG_UNUSED (fnname))
3925 int tt_format, cs_format, lp_format, i, n;
3926 #ifdef HAVE_AS_LEB128
3927 char ttype_label[32];
3928 char cs_after_size_label[32];
3929 char cs_end_label[32];
3934 int tt_format_size = 0;
3936 /* Not all functions need anything. */
3937 if (! crtl->uses_eh_lsda)
3940 if (eh_personality_libfunc)
3941 assemble_external_libcall (eh_personality_libfunc);
3943 #ifdef TARGET_UNWIND_INFO
3944 /* TODO: Move this into target file. */
3945 fputs ("\t.personality\t", asm_out_file);
3946 output_addr_const (asm_out_file, eh_personality_libfunc);
3947 fputs ("\n\t.handlerdata\n", asm_out_file);
3948 /* Note that varasm still thinks we're in the function's code section.
3949 The ".endp" directive that will immediately follow will take us back. */
3951 switch_to_exception_section (fnname);
3954 /* If the target wants a label to begin the table, emit it here. */
3955 targetm.asm_out.except_table_label (asm_out_file);
3957 have_tt_data = (VEC_length (tree, crtl->eh.ttype_data) > 0
3958 || VARRAY_ACTIVE_SIZE (crtl->eh.ehspec_data) > 0);
3960 /* Indicate the format of the @TType entries. */
3962 tt_format = DW_EH_PE_omit;
3965 tt_format = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3966 #ifdef HAVE_AS_LEB128
3967 ASM_GENERATE_INTERNAL_LABEL (ttype_label, "LLSDATT",
3968 current_function_funcdef_no);
3970 tt_format_size = size_of_encoded_value (tt_format);
3972 assemble_align (tt_format_size * BITS_PER_UNIT);
3975 targetm.asm_out.internal_label (asm_out_file, "LLSDA",
3976 current_function_funcdef_no);
3978 /* The LSDA header. */
3980 /* Indicate the format of the landing pad start pointer. An omitted
3981 field implies @LPStart == @Start. */
3982 /* Currently we always put @LPStart == @Start. This field would
3983 be most useful in moving the landing pads completely out of
3984 line to another section, but it could also be used to minimize
3985 the size of uleb128 landing pad offsets. */
3986 lp_format = DW_EH_PE_omit;
3987 dw2_asm_output_data (1, lp_format, "@LPStart format (%s)",
3988 eh_data_format_name (lp_format));
3990 /* @LPStart pointer would go here. */
3992 dw2_asm_output_data (1, tt_format, "@TType format (%s)",
3993 eh_data_format_name (tt_format));
3995 #ifndef HAVE_AS_LEB128
3996 if (USING_SJLJ_EXCEPTIONS)
3997 call_site_len = sjlj_size_of_call_site_table ();
3999 call_site_len = dw2_size_of_call_site_table ();
4002 /* A pc-relative 4-byte displacement to the @TType data. */
4005 #ifdef HAVE_AS_LEB128
4006 char ttype_after_disp_label[32];
4007 ASM_GENERATE_INTERNAL_LABEL (ttype_after_disp_label, "LLSDATTD",
4008 current_function_funcdef_no);
4009 dw2_asm_output_delta_uleb128 (ttype_label, ttype_after_disp_label,
4010 "@TType base offset");
4011 ASM_OUTPUT_LABEL (asm_out_file, ttype_after_disp_label);
4013 /* Ug. Alignment queers things. */
4014 unsigned int before_disp, after_disp, last_disp, disp;
4016 before_disp = 1 + 1;
4017 after_disp = (1 + size_of_uleb128 (call_site_len)
4019 + VARRAY_ACTIVE_SIZE (crtl->eh.action_record_data)
4020 + (VEC_length (tree, crtl->eh.ttype_data)
4026 unsigned int disp_size, pad;
4029 disp_size = size_of_uleb128 (disp);
4030 pad = before_disp + disp_size + after_disp;
4031 if (pad % tt_format_size)
4032 pad = tt_format_size - (pad % tt_format_size);
4035 disp = after_disp + pad;
4037 while (disp != last_disp);
4039 dw2_asm_output_data_uleb128 (disp, "@TType base offset");
4043 /* Indicate the format of the call-site offsets. */
4044 #ifdef HAVE_AS_LEB128
4045 cs_format = DW_EH_PE_uleb128;
4047 cs_format = DW_EH_PE_udata4;
4049 dw2_asm_output_data (1, cs_format, "call-site format (%s)",
4050 eh_data_format_name (cs_format));
4052 #ifdef HAVE_AS_LEB128
4053 ASM_GENERATE_INTERNAL_LABEL (cs_after_size_label, "LLSDACSB",
4054 current_function_funcdef_no);
4055 ASM_GENERATE_INTERNAL_LABEL (cs_end_label, "LLSDACSE",
4056 current_function_funcdef_no);
4057 dw2_asm_output_delta_uleb128 (cs_end_label, cs_after_size_label,
4058 "Call-site table length");
4059 ASM_OUTPUT_LABEL (asm_out_file, cs_after_size_label);
4060 if (USING_SJLJ_EXCEPTIONS)
4061 sjlj_output_call_site_table ();
4063 dw2_output_call_site_table ();
4064 ASM_OUTPUT_LABEL (asm_out_file, cs_end_label);
4066 dw2_asm_output_data_uleb128 (call_site_len,"Call-site table length");
4067 if (USING_SJLJ_EXCEPTIONS)
4068 sjlj_output_call_site_table ();
4070 dw2_output_call_site_table ();
4073 /* ??? Decode and interpret the data for flag_debug_asm. */
4074 n = VARRAY_ACTIVE_SIZE (crtl->eh.action_record_data);
4075 for (i = 0; i < n; ++i)
4076 dw2_asm_output_data (1, VARRAY_UCHAR (crtl->eh.action_record_data, i),
4077 (i ? NULL : "Action record table"));
4080 assemble_align (tt_format_size * BITS_PER_UNIT);
4082 i = VEC_length (tree, crtl->eh.ttype_data);
4085 tree type = VEC_index (tree, crtl->eh.ttype_data, i);
4086 output_ttype (type, tt_format, tt_format_size);
4089 #ifdef HAVE_AS_LEB128
4091 ASM_OUTPUT_LABEL (asm_out_file, ttype_label);
4094 /* ??? Decode and interpret the data for flag_debug_asm. */
4095 n = VARRAY_ACTIVE_SIZE (crtl->eh.ehspec_data);
4096 for (i = 0; i < n; ++i)
4098 if (targetm.arm_eabi_unwinder)
4100 tree type = VARRAY_TREE (crtl->eh.ehspec_data, i);
4101 output_ttype (type, tt_format, tt_format_size);
4104 dw2_asm_output_data (1, VARRAY_UCHAR (crtl->eh.ehspec_data, i),
4105 (i ? NULL : "Exception specification table"));
4108 switch_to_section (current_function_section ());
4112 set_eh_throw_stmt_table (struct function *fun, struct htab *table)
4114 fun->eh->throw_stmt_table = table;
4118 get_eh_throw_stmt_table (struct function *fun)
4120 return fun->eh->throw_stmt_table;
4123 /* Dump EH information to OUT. */
4126 dump_eh_tree (FILE * out, struct function *fun)
4128 struct eh_region *i;
4130 static const char *const type_name[] = { "unknown", "cleanup", "try", "catch",
4131 "allowed_exceptions", "must_not_throw",
4135 i = fun->eh->region_tree;
4139 fprintf (out, "Eh tree:\n");
4142 fprintf (out, " %*s %i %s", depth * 2, "",
4143 i->region_number, type_name[(int) i->type]);
4146 fprintf (out, " tree_label:");
4147 print_generic_expr (out, i->tree_label, 0);
4152 if (i->u.cleanup.prev_try)
4153 fprintf (out, " prev try:%i",
4154 i->u.cleanup.prev_try->region_number);
4159 struct eh_region *c;
4160 fprintf (out, " catch regions:");
4161 for (c = i->u.eh_try.eh_catch; c; c = c->u.eh_catch.next_catch)
4162 fprintf (out, " %i", c->region_number);
4167 if (i->u.eh_catch.prev_catch)
4168 fprintf (out, " prev: %i",
4169 i->u.eh_catch.prev_catch->region_number);
4170 if (i->u.eh_catch.next_catch)
4171 fprintf (out, " next %i",
4172 i->u.eh_catch.next_catch->region_number);
4175 case ERT_ALLOWED_EXCEPTIONS:
4176 fprintf (out, "filter :%i types:", i->u.allowed.filter);
4177 print_generic_expr (out, i->u.allowed.type_list, 0);
4181 fprintf (out, "type:");
4182 print_generic_expr (out, i->u.eh_throw.type, 0);
4185 case ERT_MUST_NOT_THROW:
4193 fprintf (out, " also known as:");
4194 dump_bitmap (out, i->aka);
4197 fprintf (out, "\n");
4198 /* If there are sub-regions, process them. */
4200 i = i->inner, depth++;
4201 /* If there are peers, process them. */
4202 else if (i->next_peer)
4204 /* Otherwise, step back up the tree to the next peer. */
4214 while (i->next_peer == NULL);
4220 /* Verify some basic invariants on EH datastructures. Could be extended to
4223 verify_eh_tree (struct function *fun)
4225 struct eh_region *i, *outer = NULL;
4232 if (!fun->eh->region_tree)
4234 for (j = fun->eh->last_region_number; j > 0; --j)
4235 if ((i = VEC_index (eh_region, fun->eh->region_array, j)))
4237 if (i->region_number == j)
4239 if (i->region_number != j && (!i->aka || !bitmap_bit_p (i->aka, j)))
4241 error ("region_array is corrupted for region %i",
4246 i = fun->eh->region_tree;
4250 if (VEC_index (eh_region, fun->eh->region_array, i->region_number) != i)
4252 error ("region_array is corrupted for region %i", i->region_number);
4255 if (i->outer != outer)
4257 error ("outer block of region %i is wrong", i->region_number);
4260 if (i->may_contain_throw && outer && !outer->may_contain_throw)
4263 ("region %i may contain throw and is contained in region that may not",
4269 error ("negative nesting depth of region %i", i->region_number);
4273 /* If there are sub-regions, process them. */
4275 outer = i, i = i->inner, depth++;
4276 /* If there are peers, process them. */
4277 else if (i->next_peer)
4279 /* Otherwise, step back up the tree to the next peer. */
4290 error ("tree list ends on depth %i", depth + 1);
4293 if (count != nvisited)
4295 error ("array does not match the region tree");
4300 dump_eh_tree (stderr, fun);
4301 internal_error ("verify_eh_tree failed");
4307 while (i->next_peer == NULL);
4313 /* Initialize unwind_resume_libfunc. */
4316 default_init_unwind_resume_libfunc (void)
4318 /* The default c++ routines aren't actually c++ specific, so use those. */
4319 unwind_resume_libfunc =
4320 init_one_libfunc ( USING_SJLJ_EXCEPTIONS ? "_Unwind_SjLj_Resume"
4321 : "_Unwind_Resume");
4326 gate_handle_eh (void)
4328 return doing_eh (0);
4331 /* Complete generation of exception handling code. */
4333 rest_of_handle_eh (void)
4335 cleanup_cfg (CLEANUP_NO_INSN_DEL);
4336 finish_eh_generation ();
4337 cleanup_cfg (CLEANUP_NO_INSN_DEL);
4341 struct rtl_opt_pass pass_rtl_eh =
4346 gate_handle_eh, /* gate */
4347 rest_of_handle_eh, /* execute */
4350 0, /* static_pass_number */
4351 TV_JUMP, /* tv_id */
4352 0, /* properties_required */
4353 0, /* properties_provided */
4354 0, /* properties_destroyed */
4355 0, /* todo_flags_start */
4356 TODO_dump_func /* todo_flags_finish */
4360 #include "gt-except.h"