1 /* Implements exception handling.
2 Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4 Contributed by Mike Stump <mrs@cygnus.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
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"
78 /* Provide defaults for stuff that may not be defined when using
80 #ifndef EH_RETURN_DATA_REGNO
81 #define EH_RETURN_DATA_REGNO(N) INVALID_REGNUM
85 /* Nonzero means enable synchronous exceptions for non-call instructions. */
86 int flag_non_call_exceptions;
88 /* Protect cleanup actions with must-not-throw regions, with a call
89 to the given failure handler. */
90 tree (*lang_protect_cleanup_actions) (void);
92 /* Return true if type A catches type B. */
93 int (*lang_eh_type_covers) (tree a, tree b);
95 /* Map a type to a runtime object to match type. */
96 tree (*lang_eh_runtime_type) (tree);
98 /* A hash table of label to region number. */
100 struct ehl_map_entry GTY(())
103 struct eh_region *region;
106 static GTY(()) int call_site_base;
107 static GTY ((param_is (union tree_node)))
108 htab_t type_to_runtime_map;
110 /* Describe the SjLj_Function_Context structure. */
111 static GTY(()) tree sjlj_fc_type_node;
112 static int sjlj_fc_call_site_ofs;
113 static int sjlj_fc_data_ofs;
114 static int sjlj_fc_personality_ofs;
115 static int sjlj_fc_lsda_ofs;
116 static int sjlj_fc_jbuf_ofs;
118 /* Describes one exception region. */
119 struct eh_region GTY(())
121 /* The immediately surrounding region. */
122 struct eh_region *outer;
124 /* The list of immediately contained regions. */
125 struct eh_region *inner;
126 struct eh_region *next_peer;
128 /* An identifier for this region. */
131 /* When a region is deleted, its parents inherit the REG_EH_REGION
132 numbers already assigned. */
135 /* Each region does exactly one thing. */
142 ERT_ALLOWED_EXCEPTIONS,
148 /* Holds the action to perform based on the preceding type. */
150 /* A list of catch blocks, a surrounding try block,
151 and the label for continuing after a catch. */
152 struct eh_region_u_try {
153 struct eh_region *catch;
154 struct eh_region *last_catch;
155 struct eh_region *prev_try;
157 } GTY ((tag ("ERT_TRY"))) try;
159 /* The list through the catch handlers, the list of type objects
160 matched, and the list of associated filters. */
161 struct eh_region_u_catch {
162 struct eh_region *next_catch;
163 struct eh_region *prev_catch;
166 } GTY ((tag ("ERT_CATCH"))) catch;
168 /* A tree_list of allowed types. */
169 struct eh_region_u_allowed {
172 } GTY ((tag ("ERT_ALLOWED_EXCEPTIONS"))) allowed;
174 /* The type given by a call to "throw foo();", or discovered
176 struct eh_region_u_throw {
178 } GTY ((tag ("ERT_THROW"))) throw;
180 /* Retain the cleanup expression even after expansion so that
181 we can match up fixup regions. */
182 struct eh_region_u_cleanup {
184 struct eh_region *prev_try;
185 } GTY ((tag ("ERT_CLEANUP"))) cleanup;
187 /* The real region (by expression and by pointer) that fixup code
189 struct eh_region_u_fixup {
191 struct eh_region *real_region;
192 } GTY ((tag ("ERT_FIXUP"))) fixup;
193 } GTY ((desc ("%0.type"))) u;
195 /* Entry point for this region's handler before landing pads are built. */
198 /* Entry point for this region's handler from the runtime eh library. */
201 /* Entry point for this region's handler from an inner region. */
202 rtx post_landing_pad;
204 /* The RESX insn for handing off control to the next outermost handler,
208 /* True if something in this region may throw. */
209 unsigned may_contain_throw : 1;
212 struct call_site_record GTY(())
218 /* Used to save exception status for each function. */
219 struct eh_status GTY(())
221 /* The tree of all regions for this function. */
222 struct eh_region *region_tree;
224 /* The same information as an indexable array. */
225 struct eh_region ** GTY ((length ("%h.last_region_number"))) region_array;
227 /* The most recently open region. */
228 struct eh_region *cur_region;
230 /* This is the region for which we are processing catch blocks. */
231 struct eh_region *try_region;
236 int built_landing_pads;
237 int last_region_number;
239 varray_type ttype_data;
240 varray_type ehspec_data;
241 varray_type action_record_data;
243 htab_t GTY ((param_is (struct ehl_map_entry))) exception_handler_label_map;
245 struct call_site_record * GTY ((length ("%h.call_site_data_used")))
247 int call_site_data_used;
248 int call_site_data_size;
259 static int t2r_eq (const void *, const void *);
260 static hashval_t t2r_hash (const void *);
261 static void add_type_for_runtime (tree);
262 static tree lookup_type_for_runtime (tree);
264 static struct eh_region *expand_eh_region_end (void);
266 static rtx get_exception_filter (struct function *);
268 static void collect_eh_region_array (void);
269 static void resolve_fixup_regions (void);
270 static void remove_fixup_regions (void);
271 static void remove_unreachable_regions (rtx);
272 static void convert_from_eh_region_ranges_1 (rtx *, int *, int);
274 static struct eh_region *duplicate_eh_region_1 (struct eh_region *,
275 struct inline_remap *);
276 static void duplicate_eh_region_2 (struct eh_region *, struct eh_region **);
277 static int ttypes_filter_eq (const void *, const void *);
278 static hashval_t ttypes_filter_hash (const void *);
279 static int ehspec_filter_eq (const void *, const void *);
280 static hashval_t ehspec_filter_hash (const void *);
281 static int add_ttypes_entry (htab_t, tree);
282 static int add_ehspec_entry (htab_t, htab_t, tree);
283 static void assign_filter_values (void);
284 static void build_post_landing_pads (void);
285 static void connect_post_landing_pads (void);
286 static void dw2_build_landing_pads (void);
289 static bool sjlj_find_directly_reachable_regions (struct sjlj_lp_info *);
290 static void sjlj_assign_call_site_values (rtx, struct sjlj_lp_info *);
291 static void sjlj_mark_call_sites (struct sjlj_lp_info *);
292 static void sjlj_emit_function_enter (rtx);
293 static void sjlj_emit_function_exit (void);
294 static void sjlj_emit_dispatch_table (rtx, struct sjlj_lp_info *);
295 static void sjlj_build_landing_pads (void);
297 static hashval_t ehl_hash (const void *);
298 static int ehl_eq (const void *, const void *);
299 static void add_ehl_entry (rtx, struct eh_region *);
300 static void remove_exception_handler_label (rtx);
301 static void remove_eh_handler (struct eh_region *);
302 static int for_each_eh_label_1 (void **, void *);
304 struct reachable_info;
306 /* The return value of reachable_next_level. */
309 /* The given exception is not processed by the given region. */
311 /* The given exception may need processing by the given region. */
313 /* The given exception is completely processed by the given region. */
315 /* The given exception is completely processed by the runtime. */
319 static int check_handled (tree, tree);
320 static void add_reachable_handler (struct reachable_info *,
321 struct eh_region *, struct eh_region *);
322 static enum reachable_code reachable_next_level (struct eh_region *, tree,
323 struct reachable_info *);
325 static int action_record_eq (const void *, const void *);
326 static hashval_t action_record_hash (const void *);
327 static int add_action_record (htab_t, int, int);
328 static int collect_one_action_chain (htab_t, struct eh_region *);
329 static int add_call_site (rtx, int);
331 static void push_uleb128 (varray_type *, unsigned int);
332 static void push_sleb128 (varray_type *, int);
333 #ifndef HAVE_AS_LEB128
334 static int dw2_size_of_call_site_table (void);
335 static int sjlj_size_of_call_site_table (void);
337 static void dw2_output_call_site_table (void);
338 static void sjlj_output_call_site_table (void);
341 /* Routine to see if exception handling is turned on.
342 DO_WARN is nonzero if we want to inform the user that exception
343 handling is turned off.
345 This is used to ensure that -fexceptions has been specified if the
346 compiler tries to use any exception-specific functions. */
349 doing_eh (int do_warn)
351 if (! flag_exceptions)
353 static int warned = 0;
354 if (! warned && do_warn)
356 error ("exception handling disabled, use -fexceptions to enable");
368 if (! flag_exceptions)
371 type_to_runtime_map = htab_create_ggc (31, t2r_hash, t2r_eq, NULL);
373 /* Create the SjLj_Function_Context structure. This should match
374 the definition in unwind-sjlj.c. */
375 if (USING_SJLJ_EXCEPTIONS)
377 tree f_jbuf, f_per, f_lsda, f_prev, f_cs, f_data, tmp;
379 sjlj_fc_type_node = (*lang_hooks.types.make_type) (RECORD_TYPE);
381 f_prev = build_decl (FIELD_DECL, get_identifier ("__prev"),
382 build_pointer_type (sjlj_fc_type_node));
383 DECL_FIELD_CONTEXT (f_prev) = sjlj_fc_type_node;
385 f_cs = build_decl (FIELD_DECL, get_identifier ("__call_site"),
387 DECL_FIELD_CONTEXT (f_cs) = sjlj_fc_type_node;
389 tmp = build_index_type (build_int_2 (4 - 1, 0));
390 tmp = build_array_type ((*lang_hooks.types.type_for_mode) (word_mode, 1),
392 f_data = build_decl (FIELD_DECL, get_identifier ("__data"), tmp);
393 DECL_FIELD_CONTEXT (f_data) = sjlj_fc_type_node;
395 f_per = build_decl (FIELD_DECL, get_identifier ("__personality"),
397 DECL_FIELD_CONTEXT (f_per) = sjlj_fc_type_node;
399 f_lsda = build_decl (FIELD_DECL, get_identifier ("__lsda"),
401 DECL_FIELD_CONTEXT (f_lsda) = sjlj_fc_type_node;
403 #ifdef DONT_USE_BUILTIN_SETJMP
405 tmp = build_int_2 (JMP_BUF_SIZE - 1, 0);
407 /* Should be large enough for most systems, if it is not,
408 JMP_BUF_SIZE should be defined with the proper value. It will
409 also tend to be larger than necessary for most systems, a more
410 optimal port will define JMP_BUF_SIZE. */
411 tmp = build_int_2 (FIRST_PSEUDO_REGISTER + 2 - 1, 0);
414 /* This is 2 for builtin_setjmp, plus whatever the target requires
415 via STACK_SAVEAREA_MODE (SAVE_NONLOCAL). */
416 tmp = build_int_2 ((GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL))
417 / GET_MODE_SIZE (Pmode)) + 2 - 1, 0);
419 tmp = build_index_type (tmp);
420 tmp = build_array_type (ptr_type_node, tmp);
421 f_jbuf = build_decl (FIELD_DECL, get_identifier ("__jbuf"), tmp);
422 #ifdef DONT_USE_BUILTIN_SETJMP
423 /* We don't know what the alignment requirements of the
424 runtime's jmp_buf has. Overestimate. */
425 DECL_ALIGN (f_jbuf) = BIGGEST_ALIGNMENT;
426 DECL_USER_ALIGN (f_jbuf) = 1;
428 DECL_FIELD_CONTEXT (f_jbuf) = sjlj_fc_type_node;
430 TYPE_FIELDS (sjlj_fc_type_node) = f_prev;
431 TREE_CHAIN (f_prev) = f_cs;
432 TREE_CHAIN (f_cs) = f_data;
433 TREE_CHAIN (f_data) = f_per;
434 TREE_CHAIN (f_per) = f_lsda;
435 TREE_CHAIN (f_lsda) = f_jbuf;
437 layout_type (sjlj_fc_type_node);
439 /* Cache the interesting field offsets so that we have
440 easy access from rtl. */
441 sjlj_fc_call_site_ofs
442 = (tree_low_cst (DECL_FIELD_OFFSET (f_cs), 1)
443 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_cs), 1) / BITS_PER_UNIT);
445 = (tree_low_cst (DECL_FIELD_OFFSET (f_data), 1)
446 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_data), 1) / BITS_PER_UNIT);
447 sjlj_fc_personality_ofs
448 = (tree_low_cst (DECL_FIELD_OFFSET (f_per), 1)
449 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_per), 1) / BITS_PER_UNIT);
451 = (tree_low_cst (DECL_FIELD_OFFSET (f_lsda), 1)
452 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_lsda), 1) / BITS_PER_UNIT);
454 = (tree_low_cst (DECL_FIELD_OFFSET (f_jbuf), 1)
455 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_jbuf), 1) / BITS_PER_UNIT);
460 init_eh_for_function (void)
462 cfun->eh = ggc_alloc_cleared (sizeof (struct eh_status));
465 /* Start an exception handling region. All instructions emitted
466 after this point are considered to be part of the region until
467 expand_eh_region_end is invoked. */
470 expand_eh_region_start (void)
472 struct eh_region *new_region;
473 struct eh_region *cur_region;
479 /* Insert a new blank region as a leaf in the tree. */
480 new_region = ggc_alloc_cleared (sizeof (*new_region));
481 cur_region = cfun->eh->cur_region;
482 new_region->outer = cur_region;
485 new_region->next_peer = cur_region->inner;
486 cur_region->inner = new_region;
490 new_region->next_peer = cfun->eh->region_tree;
491 cfun->eh->region_tree = new_region;
493 cfun->eh->cur_region = new_region;
495 /* Create a note marking the start of this region. */
496 new_region->region_number = ++cfun->eh->last_region_number;
497 note = emit_note (NOTE_INSN_EH_REGION_BEG);
498 NOTE_EH_HANDLER (note) = new_region->region_number;
501 /* Common code to end a region. Returns the region just ended. */
503 static struct eh_region *
504 expand_eh_region_end (void)
506 struct eh_region *cur_region = cfun->eh->cur_region;
509 /* Create a note marking the end of this region. */
510 note = emit_note (NOTE_INSN_EH_REGION_END);
511 NOTE_EH_HANDLER (note) = cur_region->region_number;
514 cfun->eh->cur_region = cur_region->outer;
519 /* End an exception handling region for a cleanup. HANDLER is an
520 expression to expand for the cleanup. */
523 expand_eh_region_end_cleanup (tree handler)
525 struct eh_region *region;
526 tree protect_cleanup_actions;
533 region = expand_eh_region_end ();
534 region->type = ERT_CLEANUP;
535 region->label = gen_label_rtx ();
536 region->u.cleanup.exp = handler;
537 region->u.cleanup.prev_try = cfun->eh->try_region;
539 around_label = gen_label_rtx ();
540 emit_jump (around_label);
542 emit_label (region->label);
544 if (flag_non_call_exceptions || region->may_contain_throw)
546 /* Give the language a chance to specify an action to be taken if an
547 exception is thrown that would propagate out of the HANDLER. */
548 protect_cleanup_actions
549 = (lang_protect_cleanup_actions
550 ? (*lang_protect_cleanup_actions) ()
553 if (protect_cleanup_actions)
554 expand_eh_region_start ();
556 /* In case this cleanup involves an inline destructor with a try block in
557 it, we need to save the EH return data registers around it. */
558 data_save[0] = gen_reg_rtx (ptr_mode);
559 emit_move_insn (data_save[0], get_exception_pointer (cfun));
560 data_save[1] = gen_reg_rtx (word_mode);
561 emit_move_insn (data_save[1], get_exception_filter (cfun));
563 expand_expr (handler, const0_rtx, VOIDmode, 0);
565 emit_move_insn (cfun->eh->exc_ptr, data_save[0]);
566 emit_move_insn (cfun->eh->filter, data_save[1]);
568 if (protect_cleanup_actions)
569 expand_eh_region_end_must_not_throw (protect_cleanup_actions);
571 /* We need any stack adjustment complete before the around_label. */
572 do_pending_stack_adjust ();
575 /* We delay the generation of the _Unwind_Resume until we generate
576 landing pads. We emit a marker here so as to get good control
577 flow data in the meantime. */
579 = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
582 emit_label (around_label);
585 /* End an exception handling region for a try block, and prepares
586 for subsequent calls to expand_start_catch. */
589 expand_start_all_catch (void)
591 struct eh_region *region;
596 region = expand_eh_region_end ();
597 region->type = ERT_TRY;
598 region->u.try.prev_try = cfun->eh->try_region;
599 region->u.try.continue_label = gen_label_rtx ();
601 cfun->eh->try_region = region;
603 emit_jump (region->u.try.continue_label);
606 /* Begin a catch clause. TYPE is the type caught, a list of such types, or
607 null if this is a catch-all clause. Providing a type list enables to
608 associate the catch region with potentially several exception types, which
609 is useful e.g. for Ada. */
612 expand_start_catch (tree type_or_list)
614 struct eh_region *t, *c, *l;
620 type_list = type_or_list;
624 /* Ensure to always end up with a type list to normalize further
625 processing, then register each type against the runtime types
629 if (TREE_CODE (type_or_list) != TREE_LIST)
630 type_list = tree_cons (NULL_TREE, type_or_list, NULL_TREE);
632 type_node = type_list;
633 for (; type_node; type_node = TREE_CHAIN (type_node))
634 add_type_for_runtime (TREE_VALUE (type_node));
637 expand_eh_region_start ();
639 t = cfun->eh->try_region;
640 c = cfun->eh->cur_region;
642 c->u.catch.type_list = type_list;
643 c->label = gen_label_rtx ();
645 l = t->u.try.last_catch;
646 c->u.catch.prev_catch = l;
648 l->u.catch.next_catch = c;
651 t->u.try.last_catch = c;
653 emit_label (c->label);
656 /* End a catch clause. Control will resume after the try/catch block. */
659 expand_end_catch (void)
661 struct eh_region *try_region;
666 expand_eh_region_end ();
667 try_region = cfun->eh->try_region;
669 emit_jump (try_region->u.try.continue_label);
672 /* End a sequence of catch handlers for a try block. */
675 expand_end_all_catch (void)
677 struct eh_region *try_region;
682 try_region = cfun->eh->try_region;
683 cfun->eh->try_region = try_region->u.try.prev_try;
685 emit_label (try_region->u.try.continue_label);
688 /* End an exception region for an exception type filter. ALLOWED is a
689 TREE_LIST of types to be matched by the runtime. FAILURE is an
690 expression to invoke if a mismatch occurs.
692 ??? We could use these semantics for calls to rethrow, too; if we can
693 see the surrounding catch clause, we know that the exception we're
694 rethrowing satisfies the "filter" of the catch type. */
697 expand_eh_region_end_allowed (tree allowed, tree failure)
699 struct eh_region *region;
705 region = expand_eh_region_end ();
706 region->type = ERT_ALLOWED_EXCEPTIONS;
707 region->u.allowed.type_list = allowed;
708 region->label = gen_label_rtx ();
710 for (; allowed ; allowed = TREE_CHAIN (allowed))
711 add_type_for_runtime (TREE_VALUE (allowed));
713 /* We must emit the call to FAILURE here, so that if this function
714 throws a different exception, that it will be processed by the
717 around_label = gen_label_rtx ();
718 emit_jump (around_label);
720 emit_label (region->label);
721 expand_expr (failure, const0_rtx, VOIDmode, EXPAND_NORMAL);
722 /* We must adjust the stack before we reach the AROUND_LABEL because
723 the call to FAILURE does not occur on all paths to the
725 do_pending_stack_adjust ();
727 emit_label (around_label);
730 /* End an exception region for a must-not-throw filter. FAILURE is an
731 expression invoke if an uncaught exception propagates this far.
733 This is conceptually identical to expand_eh_region_end_allowed with
734 an empty allowed list (if you passed "std::terminate" instead of
735 "__cxa_call_unexpected"), but they are represented differently in
739 expand_eh_region_end_must_not_throw (tree failure)
741 struct eh_region *region;
747 region = expand_eh_region_end ();
748 region->type = ERT_MUST_NOT_THROW;
749 region->label = gen_label_rtx ();
751 /* We must emit the call to FAILURE here, so that if this function
752 throws a different exception, that it will be processed by the
755 around_label = gen_label_rtx ();
756 emit_jump (around_label);
758 emit_label (region->label);
759 expand_expr (failure, const0_rtx, VOIDmode, EXPAND_NORMAL);
761 emit_label (around_label);
764 /* End an exception region for a throw. No handling goes on here,
765 but it's the easiest way for the front-end to indicate what type
769 expand_eh_region_end_throw (tree type)
771 struct eh_region *region;
776 region = expand_eh_region_end ();
777 region->type = ERT_THROW;
778 region->u.throw.type = type;
781 /* End a fixup region. Within this region the cleanups for the immediately
782 enclosing region are _not_ run. This is used for goto cleanup to avoid
783 destroying an object twice.
785 This would be an extraordinarily simple prospect, were it not for the
786 fact that we don't actually know what the immediately enclosing region
787 is. This surprising fact is because expand_cleanups is currently
788 generating a sequence that it will insert somewhere else. We collect
789 the proper notion of "enclosing" in convert_from_eh_region_ranges. */
792 expand_eh_region_end_fixup (tree handler)
794 struct eh_region *fixup;
799 fixup = expand_eh_region_end ();
800 fixup->type = ERT_FIXUP;
801 fixup->u.fixup.cleanup_exp = handler;
804 /* Note that the current EH region (if any) may contain a throw, or a
805 call to a function which itself may contain a throw. */
808 note_eh_region_may_contain_throw (void)
810 struct eh_region *region;
812 region = cfun->eh->cur_region;
813 while (region && !region->may_contain_throw)
815 region->may_contain_throw = 1;
816 region = region->outer;
820 /* Return an rtl expression for a pointer to the exception object
824 get_exception_pointer (struct function *fun)
826 rtx exc_ptr = fun->eh->exc_ptr;
827 if (fun == cfun && ! exc_ptr)
829 exc_ptr = gen_reg_rtx (ptr_mode);
830 fun->eh->exc_ptr = exc_ptr;
835 /* Return an rtl expression for the exception dispatch filter
839 get_exception_filter (struct function *fun)
841 rtx filter = fun->eh->filter;
842 if (fun == cfun && ! filter)
844 filter = gen_reg_rtx (word_mode);
845 fun->eh->filter = filter;
850 /* This section is for the exception handling specific optimization pass. */
852 /* Random access the exception region tree. It's just as simple to
853 collect the regions this way as in expand_eh_region_start, but
854 without having to realloc memory. */
857 collect_eh_region_array (void)
859 struct eh_region **array, *i;
861 i = cfun->eh->region_tree;
865 array = ggc_alloc_cleared ((cfun->eh->last_region_number + 1)
867 cfun->eh->region_array = array;
871 array[i->region_number] = i;
873 /* If there are sub-regions, process them. */
876 /* If there are peers, process them. */
877 else if (i->next_peer)
879 /* Otherwise, step back up the tree to the next peer. */
886 } while (i->next_peer == NULL);
893 resolve_fixup_regions (void)
895 int i, j, n = cfun->eh->last_region_number;
897 for (i = 1; i <= n; ++i)
899 struct eh_region *fixup = cfun->eh->region_array[i];
900 struct eh_region *cleanup = 0;
902 if (! fixup || fixup->type != ERT_FIXUP)
905 for (j = 1; j <= n; ++j)
907 cleanup = cfun->eh->region_array[j];
908 if (cleanup && cleanup->type == ERT_CLEANUP
909 && cleanup->u.cleanup.exp == fixup->u.fixup.cleanup_exp)
915 fixup->u.fixup.real_region = cleanup->outer;
919 /* Now that we've discovered what region actually encloses a fixup,
920 we can shuffle pointers and remove them from the tree. */
923 remove_fixup_regions (void)
927 struct eh_region *fixup;
929 /* Walk the insn chain and adjust the REG_EH_REGION numbers
930 for instructions referencing fixup regions. This is only
931 strictly necessary for fixup regions with no parent, but
932 doesn't hurt to do it for all regions. */
933 for (insn = get_insns(); insn ; insn = NEXT_INSN (insn))
935 && (note = find_reg_note (insn, REG_EH_REGION, NULL))
936 && INTVAL (XEXP (note, 0)) > 0
937 && (fixup = cfun->eh->region_array[INTVAL (XEXP (note, 0))])
938 && fixup->type == ERT_FIXUP)
940 if (fixup->u.fixup.real_region)
941 XEXP (note, 0) = GEN_INT (fixup->u.fixup.real_region->region_number);
943 remove_note (insn, note);
946 /* Remove the fixup regions from the tree. */
947 for (i = cfun->eh->last_region_number; i > 0; --i)
949 fixup = cfun->eh->region_array[i];
953 /* Allow GC to maybe free some memory. */
954 if (fixup->type == ERT_CLEANUP)
955 fixup->u.cleanup.exp = NULL_TREE;
957 if (fixup->type != ERT_FIXUP)
962 struct eh_region *parent, *p, **pp;
964 parent = fixup->u.fixup.real_region;
966 /* Fix up the children's parent pointers; find the end of
968 for (p = fixup->inner; ; p = p->next_peer)
975 /* In the tree of cleanups, only outer-inner ordering matters.
976 So link the children back in anywhere at the correct level. */
980 pp = &cfun->eh->region_tree;
986 remove_eh_handler (fixup);
990 /* Remove all regions whose labels are not reachable from insns. */
993 remove_unreachable_regions (rtx insns)
995 int i, *uid_region_num;
1000 uid_region_num = xcalloc (get_max_uid (), sizeof(int));
1001 reachable = xcalloc (cfun->eh->last_region_number + 1, sizeof(bool));
1003 for (i = cfun->eh->last_region_number; i > 0; --i)
1005 r = cfun->eh->region_array[i];
1006 if (!r || r->region_number != i)
1011 if (uid_region_num[INSN_UID (r->resume)])
1013 uid_region_num[INSN_UID (r->resume)] = i;
1017 if (uid_region_num[INSN_UID (r->label)])
1019 uid_region_num[INSN_UID (r->label)] = i;
1021 if (r->type == ERT_TRY && r->u.try.continue_label)
1023 if (uid_region_num[INSN_UID (r->u.try.continue_label)])
1025 uid_region_num[INSN_UID (r->u.try.continue_label)] = i;
1029 for (insn = insns; insn; insn = NEXT_INSN (insn))
1030 reachable[uid_region_num[INSN_UID (insn)]] = true;
1032 for (i = cfun->eh->last_region_number; i > 0; --i)
1034 r = cfun->eh->region_array[i];
1035 if (r && r->region_number == i && !reachable[i])
1037 /* Don't remove ERT_THROW regions if their outer region
1039 if (r->type == ERT_THROW
1041 && reachable[r->outer->region_number])
1044 remove_eh_handler (r);
1049 free (uid_region_num);
1052 /* Turn NOTE_INSN_EH_REGION notes into REG_EH_REGION notes for each
1053 can_throw instruction in the region. */
1056 convert_from_eh_region_ranges_1 (rtx *pinsns, int *orig_sp, int cur)
1061 for (insn = *pinsns; insn ; insn = next)
1063 next = NEXT_INSN (insn);
1064 if (GET_CODE (insn) == NOTE)
1066 int kind = NOTE_LINE_NUMBER (insn);
1067 if (kind == NOTE_INSN_EH_REGION_BEG
1068 || kind == NOTE_INSN_EH_REGION_END)
1070 if (kind == NOTE_INSN_EH_REGION_BEG)
1072 struct eh_region *r;
1075 cur = NOTE_EH_HANDLER (insn);
1077 r = cfun->eh->region_array[cur];
1078 if (r->type == ERT_FIXUP)
1080 r = r->u.fixup.real_region;
1081 cur = r ? r->region_number : 0;
1083 else if (r->type == ERT_CATCH)
1086 cur = r ? r->region_number : 0;
1092 /* Removing the first insn of a CALL_PLACEHOLDER sequence
1093 requires extra care to adjust sequence start. */
1094 if (insn == *pinsns)
1100 else if (INSN_P (insn))
1103 && ! find_reg_note (insn, REG_EH_REGION, NULL_RTX)
1104 /* Calls can always potentially throw exceptions, unless
1105 they have a REG_EH_REGION note with a value of 0 or less.
1106 Which should be the only possible kind so far. */
1107 && (GET_CODE (insn) == CALL_INSN
1108 /* If we wanted exceptions for non-call insns, then
1109 any may_trap_p instruction could throw. */
1110 || (flag_non_call_exceptions
1111 && GET_CODE (PATTERN (insn)) != CLOBBER
1112 && GET_CODE (PATTERN (insn)) != USE
1113 && may_trap_p (PATTERN (insn)))))
1115 REG_NOTES (insn) = alloc_EXPR_LIST (REG_EH_REGION, GEN_INT (cur),
1119 if (GET_CODE (insn) == CALL_INSN
1120 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
1122 convert_from_eh_region_ranges_1 (&XEXP (PATTERN (insn), 0),
1124 convert_from_eh_region_ranges_1 (&XEXP (PATTERN (insn), 1),
1126 convert_from_eh_region_ranges_1 (&XEXP (PATTERN (insn), 2),
1137 convert_from_eh_region_ranges (void)
1142 collect_eh_region_array ();
1143 resolve_fixup_regions ();
1145 stack = xmalloc (sizeof (int) * (cfun->eh->last_region_number + 1));
1146 insns = get_insns ();
1147 convert_from_eh_region_ranges_1 (&insns, stack, 0);
1150 remove_fixup_regions ();
1151 remove_unreachable_regions (insns);
1155 add_ehl_entry (rtx label, struct eh_region *region)
1157 struct ehl_map_entry **slot, *entry;
1159 LABEL_PRESERVE_P (label) = 1;
1161 entry = ggc_alloc (sizeof (*entry));
1162 entry->label = label;
1163 entry->region = region;
1165 slot = (struct ehl_map_entry **)
1166 htab_find_slot (cfun->eh->exception_handler_label_map, entry, INSERT);
1168 /* Before landing pad creation, each exception handler has its own
1169 label. After landing pad creation, the exception handlers may
1170 share landing pads. This is ok, since maybe_remove_eh_handler
1171 only requires the 1-1 mapping before landing pad creation. */
1172 if (*slot && !cfun->eh->built_landing_pads)
1179 find_exception_handler_labels (void)
1183 if (cfun->eh->exception_handler_label_map)
1184 htab_empty (cfun->eh->exception_handler_label_map);
1187 /* ??? The expansion factor here (3/2) must be greater than the htab
1188 occupancy factor (4/3) to avoid unnecessary resizing. */
1189 cfun->eh->exception_handler_label_map
1190 = htab_create_ggc (cfun->eh->last_region_number * 3 / 2,
1191 ehl_hash, ehl_eq, NULL);
1194 if (cfun->eh->region_tree == NULL)
1197 for (i = cfun->eh->last_region_number; i > 0; --i)
1199 struct eh_region *region = cfun->eh->region_array[i];
1202 if (! region || region->region_number != i)
1204 if (cfun->eh->built_landing_pads)
1205 lab = region->landing_pad;
1207 lab = region->label;
1210 add_ehl_entry (lab, region);
1213 /* For sjlj exceptions, need the return label to remain live until
1214 after landing pad generation. */
1215 if (USING_SJLJ_EXCEPTIONS && ! cfun->eh->built_landing_pads)
1216 add_ehl_entry (return_label, NULL);
1220 current_function_has_exception_handlers (void)
1224 for (i = cfun->eh->last_region_number; i > 0; --i)
1226 struct eh_region *region = cfun->eh->region_array[i];
1228 if (! region || region->region_number != i)
1230 if (region->type != ERT_THROW)
1237 static struct eh_region *
1238 duplicate_eh_region_1 (struct eh_region *o, struct inline_remap *map)
1240 struct eh_region *n = ggc_alloc_cleared (sizeof (struct eh_region));
1242 n->region_number = o->region_number + cfun->eh->last_region_number;
1248 case ERT_MUST_NOT_THROW:
1252 if (o->u.try.continue_label)
1253 n->u.try.continue_label
1254 = get_label_from_map (map,
1255 CODE_LABEL_NUMBER (o->u.try.continue_label));
1259 n->u.catch.type_list = o->u.catch.type_list;
1262 case ERT_ALLOWED_EXCEPTIONS:
1263 n->u.allowed.type_list = o->u.allowed.type_list;
1267 n->u.throw.type = o->u.throw.type;
1274 n->label = get_label_from_map (map, CODE_LABEL_NUMBER (o->label));
1277 n->resume = map->insn_map[INSN_UID (o->resume)];
1278 if (n->resume == NULL)
1286 duplicate_eh_region_2 (struct eh_region *o, struct eh_region **n_array)
1288 struct eh_region *n = n_array[o->region_number];
1293 n->u.try.catch = n_array[o->u.try.catch->region_number];
1294 n->u.try.last_catch = n_array[o->u.try.last_catch->region_number];
1298 if (o->u.catch.next_catch)
1299 n->u.catch.next_catch = n_array[o->u.catch.next_catch->region_number];
1300 if (o->u.catch.prev_catch)
1301 n->u.catch.prev_catch = n_array[o->u.catch.prev_catch->region_number];
1309 n->outer = n_array[o->outer->region_number];
1311 n->inner = n_array[o->inner->region_number];
1313 n->next_peer = n_array[o->next_peer->region_number];
1317 duplicate_eh_regions (struct function *ifun, struct inline_remap *map)
1319 int ifun_last_region_number = ifun->eh->last_region_number;
1320 struct eh_region **n_array, *root, *cur;
1323 if (ifun_last_region_number == 0)
1326 n_array = xcalloc (ifun_last_region_number + 1, sizeof (*n_array));
1328 for (i = 1; i <= ifun_last_region_number; ++i)
1330 cur = ifun->eh->region_array[i];
1331 if (!cur || cur->region_number != i)
1333 n_array[i] = duplicate_eh_region_1 (cur, map);
1335 for (i = 1; i <= ifun_last_region_number; ++i)
1337 cur = ifun->eh->region_array[i];
1338 if (!cur || cur->region_number != i)
1340 duplicate_eh_region_2 (cur, n_array);
1343 root = n_array[ifun->eh->region_tree->region_number];
1344 cur = cfun->eh->cur_region;
1347 struct eh_region *p = cur->inner;
1350 while (p->next_peer)
1352 p->next_peer = root;
1357 for (i = 1; i <= ifun_last_region_number; ++i)
1358 if (n_array[i] && n_array[i]->outer == NULL)
1359 n_array[i]->outer = cur;
1363 struct eh_region *p = cfun->eh->region_tree;
1366 while (p->next_peer)
1368 p->next_peer = root;
1371 cfun->eh->region_tree = root;
1376 i = cfun->eh->last_region_number;
1377 cfun->eh->last_region_number = i + ifun_last_region_number;
1383 t2r_eq (const void *pentry, const void *pdata)
1385 tree entry = (tree) pentry;
1386 tree data = (tree) pdata;
1388 return TREE_PURPOSE (entry) == data;
1392 t2r_hash (const void *pentry)
1394 tree entry = (tree) pentry;
1395 return TYPE_HASH (TREE_PURPOSE (entry));
1399 add_type_for_runtime (tree type)
1403 slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
1404 TYPE_HASH (type), INSERT);
1407 tree runtime = (*lang_eh_runtime_type) (type);
1408 *slot = tree_cons (type, runtime, NULL_TREE);
1413 lookup_type_for_runtime (tree type)
1417 slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
1418 TYPE_HASH (type), NO_INSERT);
1420 /* We should have always inserted the data earlier. */
1421 return TREE_VALUE (*slot);
1425 /* Represent an entry in @TTypes for either catch actions
1426 or exception filter actions. */
1427 struct ttypes_filter GTY(())
1433 /* Compare ENTRY (a ttypes_filter entry in the hash table) with DATA
1434 (a tree) for a @TTypes type node we are thinking about adding. */
1437 ttypes_filter_eq (const void *pentry, const void *pdata)
1439 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1440 tree data = (tree) pdata;
1442 return entry->t == data;
1446 ttypes_filter_hash (const void *pentry)
1448 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1449 return TYPE_HASH (entry->t);
1452 /* Compare ENTRY with DATA (both struct ttypes_filter) for a @TTypes
1453 exception specification list we are thinking about adding. */
1454 /* ??? Currently we use the type lists in the order given. Someone
1455 should put these in some canonical order. */
1458 ehspec_filter_eq (const void *pentry, const void *pdata)
1460 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1461 const struct ttypes_filter *data = (const struct ttypes_filter *) pdata;
1463 return type_list_equal (entry->t, data->t);
1466 /* Hash function for exception specification lists. */
1469 ehspec_filter_hash (const void *pentry)
1471 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1475 for (list = entry->t; list ; list = TREE_CHAIN (list))
1476 h = (h << 5) + (h >> 27) + TYPE_HASH (TREE_VALUE (list));
1480 /* Add TYPE to cfun->eh->ttype_data, using TYPES_HASH to speed
1481 up the search. Return the filter value to be used. */
1484 add_ttypes_entry (htab_t ttypes_hash, tree type)
1486 struct ttypes_filter **slot, *n;
1488 slot = (struct ttypes_filter **)
1489 htab_find_slot_with_hash (ttypes_hash, type, TYPE_HASH (type), INSERT);
1491 if ((n = *slot) == NULL)
1493 /* Filter value is a 1 based table index. */
1495 n = xmalloc (sizeof (*n));
1497 n->filter = VARRAY_ACTIVE_SIZE (cfun->eh->ttype_data) + 1;
1500 VARRAY_PUSH_TREE (cfun->eh->ttype_data, type);
1506 /* Add LIST to cfun->eh->ehspec_data, using EHSPEC_HASH and TYPES_HASH
1507 to speed up the search. Return the filter value to be used. */
1510 add_ehspec_entry (htab_t ehspec_hash, htab_t ttypes_hash, tree list)
1512 struct ttypes_filter **slot, *n;
1513 struct ttypes_filter dummy;
1516 slot = (struct ttypes_filter **)
1517 htab_find_slot (ehspec_hash, &dummy, INSERT);
1519 if ((n = *slot) == NULL)
1521 /* Filter value is a -1 based byte index into a uleb128 buffer. */
1523 n = xmalloc (sizeof (*n));
1525 n->filter = -(VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data) + 1);
1528 /* Look up each type in the list and encode its filter
1529 value as a uleb128. Terminate the list with 0. */
1530 for (; list ; list = TREE_CHAIN (list))
1531 push_uleb128 (&cfun->eh->ehspec_data,
1532 add_ttypes_entry (ttypes_hash, TREE_VALUE (list)));
1533 VARRAY_PUSH_UCHAR (cfun->eh->ehspec_data, 0);
1539 /* Generate the action filter values to be used for CATCH and
1540 ALLOWED_EXCEPTIONS regions. When using dwarf2 exception regions,
1541 we use lots of landing pads, and so every type or list can share
1542 the same filter value, which saves table space. */
1545 assign_filter_values (void)
1548 htab_t ttypes, ehspec;
1550 VARRAY_TREE_INIT (cfun->eh->ttype_data, 16, "ttype_data");
1551 VARRAY_UCHAR_INIT (cfun->eh->ehspec_data, 64, "ehspec_data");
1553 ttypes = htab_create (31, ttypes_filter_hash, ttypes_filter_eq, free);
1554 ehspec = htab_create (31, ehspec_filter_hash, ehspec_filter_eq, free);
1556 for (i = cfun->eh->last_region_number; i > 0; --i)
1558 struct eh_region *r = cfun->eh->region_array[i];
1560 /* Mind we don't process a region more than once. */
1561 if (!r || r->region_number != i)
1567 /* Whatever type_list is (NULL or true list), we build a list
1568 of filters for the region. */
1569 r->u.catch.filter_list = NULL_TREE;
1571 if (r->u.catch.type_list != NULL)
1573 /* Get a filter value for each of the types caught and store
1574 them in the region's dedicated list. */
1575 tree tp_node = r->u.catch.type_list;
1577 for (;tp_node; tp_node = TREE_CHAIN (tp_node))
1579 int flt = add_ttypes_entry (ttypes, TREE_VALUE (tp_node));
1580 tree flt_node = build_int_2 (flt, 0);
1582 r->u.catch.filter_list
1583 = tree_cons (NULL_TREE, flt_node, r->u.catch.filter_list);
1588 /* Get a filter value for the NULL list also since it will need
1589 an action record anyway. */
1590 int flt = add_ttypes_entry (ttypes, NULL);
1591 tree flt_node = build_int_2 (flt, 0);
1593 r->u.catch.filter_list
1594 = tree_cons (NULL_TREE, flt_node, r->u.catch.filter_list);
1599 case ERT_ALLOWED_EXCEPTIONS:
1601 = add_ehspec_entry (ehspec, ttypes, r->u.allowed.type_list);
1609 htab_delete (ttypes);
1610 htab_delete (ehspec);
1613 /* Generate the code to actually handle exceptions, which will follow the
1617 build_post_landing_pads (void)
1621 for (i = cfun->eh->last_region_number; i > 0; --i)
1623 struct eh_region *region = cfun->eh->region_array[i];
1626 /* Mind we don't process a region more than once. */
1627 if (!region || region->region_number != i)
1630 switch (region->type)
1633 /* ??? Collect the set of all non-overlapping catch handlers
1634 all the way up the chain until blocked by a cleanup. */
1635 /* ??? Outer try regions can share landing pads with inner
1636 try regions if the types are completely non-overlapping,
1637 and there are no intervening cleanups. */
1639 region->post_landing_pad = gen_label_rtx ();
1643 emit_label (region->post_landing_pad);
1645 /* ??? It is mighty inconvenient to call back into the
1646 switch statement generation code in expand_end_case.
1647 Rapid prototyping sez a sequence of ifs. */
1649 struct eh_region *c;
1650 for (c = region->u.try.catch; c ; c = c->u.catch.next_catch)
1652 if (c->u.catch.type_list == NULL)
1653 emit_jump (c->label);
1656 /* Need for one cmp/jump per type caught. Each type
1657 list entry has a matching entry in the filter list
1658 (see assign_filter_values). */
1659 tree tp_node = c->u.catch.type_list;
1660 tree flt_node = c->u.catch.filter_list;
1664 emit_cmp_and_jump_insns
1666 GEN_INT (tree_low_cst (TREE_VALUE (flt_node), 0)),
1667 EQ, NULL_RTX, word_mode, 0, c->label);
1669 tp_node = TREE_CHAIN (tp_node);
1670 flt_node = TREE_CHAIN (flt_node);
1676 /* We delay the generation of the _Unwind_Resume until we generate
1677 landing pads. We emit a marker here so as to get good control
1678 flow data in the meantime. */
1680 = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
1686 emit_insn_before (seq, region->u.try.catch->label);
1689 case ERT_ALLOWED_EXCEPTIONS:
1690 region->post_landing_pad = gen_label_rtx ();
1694 emit_label (region->post_landing_pad);
1696 emit_cmp_and_jump_insns (cfun->eh->filter,
1697 GEN_INT (region->u.allowed.filter),
1698 EQ, NULL_RTX, word_mode, 0, region->label);
1700 /* We delay the generation of the _Unwind_Resume until we generate
1701 landing pads. We emit a marker here so as to get good control
1702 flow data in the meantime. */
1704 = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
1710 emit_insn_before (seq, region->label);
1714 case ERT_MUST_NOT_THROW:
1715 region->post_landing_pad = region->label;
1720 /* Nothing to do. */
1729 /* Replace RESX patterns with jumps to the next handler if any, or calls to
1730 _Unwind_Resume otherwise. */
1733 connect_post_landing_pads (void)
1737 for (i = cfun->eh->last_region_number; i > 0; --i)
1739 struct eh_region *region = cfun->eh->region_array[i];
1740 struct eh_region *outer;
1743 /* Mind we don't process a region more than once. */
1744 if (!region || region->region_number != i)
1747 /* If there is no RESX, or it has been deleted by flow, there's
1748 nothing to fix up. */
1749 if (! region->resume || INSN_DELETED_P (region->resume))
1752 /* Search for another landing pad in this function. */
1753 for (outer = region->outer; outer ; outer = outer->outer)
1754 if (outer->post_landing_pad)
1760 emit_jump (outer->post_landing_pad);
1762 emit_library_call (unwind_resume_libfunc, LCT_THROW,
1763 VOIDmode, 1, cfun->eh->exc_ptr, ptr_mode);
1767 emit_insn_before (seq, region->resume);
1768 delete_insn (region->resume);
1774 dw2_build_landing_pads (void)
1779 for (i = cfun->eh->last_region_number; i > 0; --i)
1781 struct eh_region *region = cfun->eh->region_array[i];
1783 bool clobbers_hard_regs = false;
1785 /* Mind we don't process a region more than once. */
1786 if (!region || region->region_number != i)
1789 if (region->type != ERT_CLEANUP
1790 && region->type != ERT_TRY
1791 && region->type != ERT_ALLOWED_EXCEPTIONS)
1796 region->landing_pad = gen_label_rtx ();
1797 emit_label (region->landing_pad);
1799 #ifdef HAVE_exception_receiver
1800 if (HAVE_exception_receiver)
1801 emit_insn (gen_exception_receiver ());
1804 #ifdef HAVE_nonlocal_goto_receiver
1805 if (HAVE_nonlocal_goto_receiver)
1806 emit_insn (gen_nonlocal_goto_receiver ());
1811 /* If the eh_return data registers are call-saved, then we
1812 won't have considered them clobbered from the call that
1813 threw. Kill them now. */
1816 unsigned r = EH_RETURN_DATA_REGNO (j);
1817 if (r == INVALID_REGNUM)
1819 if (! call_used_regs[r])
1821 emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, r)));
1822 clobbers_hard_regs = true;
1826 if (clobbers_hard_regs)
1828 /* @@@ This is a kludge. Not all machine descriptions define a
1829 blockage insn, but we must not allow the code we just generated
1830 to be reordered by scheduling. So emit an ASM_INPUT to act as
1832 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
1835 emit_move_insn (cfun->eh->exc_ptr,
1836 gen_rtx_REG (ptr_mode, EH_RETURN_DATA_REGNO (0)));
1837 emit_move_insn (cfun->eh->filter,
1838 gen_rtx_REG (word_mode, EH_RETURN_DATA_REGNO (1)));
1843 emit_insn_before (seq, region->post_landing_pad);
1850 int directly_reachable;
1853 int call_site_index;
1857 sjlj_find_directly_reachable_regions (struct sjlj_lp_info *lp_info)
1860 bool found_one = false;
1862 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
1864 struct eh_region *region;
1865 enum reachable_code rc;
1869 if (! INSN_P (insn))
1872 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1873 if (!note || INTVAL (XEXP (note, 0)) <= 0)
1876 region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
1878 type_thrown = NULL_TREE;
1879 if (region->type == ERT_THROW)
1881 type_thrown = region->u.throw.type;
1882 region = region->outer;
1885 /* Find the first containing region that might handle the exception.
1886 That's the landing pad to which we will transfer control. */
1887 rc = RNL_NOT_CAUGHT;
1888 for (; region; region = region->outer)
1890 rc = reachable_next_level (region, type_thrown, 0);
1891 if (rc != RNL_NOT_CAUGHT)
1894 if (rc == RNL_MAYBE_CAUGHT || rc == RNL_CAUGHT)
1896 lp_info[region->region_number].directly_reachable = 1;
1905 sjlj_assign_call_site_values (rtx dispatch_label, struct sjlj_lp_info *lp_info)
1910 /* First task: build the action table. */
1912 VARRAY_UCHAR_INIT (cfun->eh->action_record_data, 64, "action_record_data");
1913 ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
1915 for (i = cfun->eh->last_region_number; i > 0; --i)
1916 if (lp_info[i].directly_reachable)
1918 struct eh_region *r = cfun->eh->region_array[i];
1919 r->landing_pad = dispatch_label;
1920 lp_info[i].action_index = collect_one_action_chain (ar_hash, r);
1921 if (lp_info[i].action_index != -1)
1922 cfun->uses_eh_lsda = 1;
1925 htab_delete (ar_hash);
1927 /* Next: assign dispatch values. In dwarf2 terms, this would be the
1928 landing pad label for the region. For sjlj though, there is one
1929 common landing pad from which we dispatch to the post-landing pads.
1931 A region receives a dispatch index if it is directly reachable
1932 and requires in-function processing. Regions that share post-landing
1933 pads may share dispatch indices. */
1934 /* ??? Post-landing pad sharing doesn't actually happen at the moment
1935 (see build_post_landing_pads) so we don't bother checking for it. */
1938 for (i = cfun->eh->last_region_number; i > 0; --i)
1939 if (lp_info[i].directly_reachable)
1940 lp_info[i].dispatch_index = index++;
1942 /* Finally: assign call-site values. If dwarf2 terms, this would be
1943 the region number assigned by convert_to_eh_region_ranges, but
1944 handles no-action and must-not-throw differently. */
1947 for (i = cfun->eh->last_region_number; i > 0; --i)
1948 if (lp_info[i].directly_reachable)
1950 int action = lp_info[i].action_index;
1952 /* Map must-not-throw to otherwise unused call-site index 0. */
1955 /* Map no-action to otherwise unused call-site index -1. */
1956 else if (action == -1)
1958 /* Otherwise, look it up in the table. */
1960 index = add_call_site (GEN_INT (lp_info[i].dispatch_index), action);
1962 lp_info[i].call_site_index = index;
1967 sjlj_mark_call_sites (struct sjlj_lp_info *lp_info)
1969 int last_call_site = -2;
1972 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
1974 struct eh_region *region;
1976 rtx note, before, p;
1978 /* Reset value tracking at extended basic block boundaries. */
1979 if (GET_CODE (insn) == CODE_LABEL)
1980 last_call_site = -2;
1982 if (! INSN_P (insn))
1985 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1988 /* Calls (and trapping insns) without notes are outside any
1989 exception handling region in this function. Mark them as
1991 if (GET_CODE (insn) == CALL_INSN
1992 || (flag_non_call_exceptions
1993 && may_trap_p (PATTERN (insn))))
1994 this_call_site = -1;
2000 /* Calls that are known to not throw need not be marked. */
2001 if (INTVAL (XEXP (note, 0)) <= 0)
2004 region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
2005 this_call_site = lp_info[region->region_number].call_site_index;
2008 if (this_call_site == last_call_site)
2011 /* Don't separate a call from it's argument loads. */
2013 if (GET_CODE (insn) == CALL_INSN)
2014 before = find_first_parameter_load (insn, NULL_RTX);
2017 mem = adjust_address (cfun->eh->sjlj_fc, TYPE_MODE (integer_type_node),
2018 sjlj_fc_call_site_ofs);
2019 emit_move_insn (mem, GEN_INT (this_call_site));
2023 emit_insn_before (p, before);
2024 last_call_site = this_call_site;
2028 /* Construct the SjLj_Function_Context. */
2031 sjlj_emit_function_enter (rtx dispatch_label)
2033 rtx fn_begin, fc, mem, seq;
2035 fc = cfun->eh->sjlj_fc;
2039 /* We're storing this libcall's address into memory instead of
2040 calling it directly. Thus, we must call assemble_external_libcall
2041 here, as we can not depend on emit_library_call to do it for us. */
2042 assemble_external_libcall (eh_personality_libfunc);
2043 mem = adjust_address (fc, Pmode, sjlj_fc_personality_ofs);
2044 emit_move_insn (mem, eh_personality_libfunc);
2046 mem = adjust_address (fc, Pmode, sjlj_fc_lsda_ofs);
2047 if (cfun->uses_eh_lsda)
2050 ASM_GENERATE_INTERNAL_LABEL (buf, "LLSDA", current_function_funcdef_no);
2051 emit_move_insn (mem, gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf)));
2054 emit_move_insn (mem, const0_rtx);
2056 #ifdef DONT_USE_BUILTIN_SETJMP
2059 x = emit_library_call_value (setjmp_libfunc, NULL_RTX, LCT_RETURNS_TWICE,
2060 TYPE_MODE (integer_type_node), 1,
2061 plus_constant (XEXP (fc, 0),
2062 sjlj_fc_jbuf_ofs), Pmode);
2064 note = emit_note (NOTE_INSN_EXPECTED_VALUE);
2065 NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, x, const0_rtx);
2067 emit_cmp_and_jump_insns (x, const0_rtx, NE, 0,
2068 TYPE_MODE (integer_type_node), 0, dispatch_label);
2071 expand_builtin_setjmp_setup (plus_constant (XEXP (fc, 0), sjlj_fc_jbuf_ofs),
2075 emit_library_call (unwind_sjlj_register_libfunc, LCT_NORMAL, VOIDmode,
2076 1, XEXP (fc, 0), Pmode);
2081 /* ??? Instead of doing this at the beginning of the function,
2082 do this in a block that is at loop level 0 and dominates all
2083 can_throw_internal instructions. */
2085 for (fn_begin = get_insns (); ; fn_begin = NEXT_INSN (fn_begin))
2086 if (GET_CODE (fn_begin) == NOTE
2087 && NOTE_LINE_NUMBER (fn_begin) == NOTE_INSN_FUNCTION_BEG)
2089 emit_insn_after (seq, fn_begin);
2092 /* Call back from expand_function_end to know where we should put
2093 the call to unwind_sjlj_unregister_libfunc if needed. */
2096 sjlj_emit_function_exit_after (rtx after)
2098 cfun->eh->sjlj_exit_after = after;
2102 sjlj_emit_function_exit (void)
2108 emit_library_call (unwind_sjlj_unregister_libfunc, LCT_NORMAL, VOIDmode,
2109 1, XEXP (cfun->eh->sjlj_fc, 0), Pmode);
2114 /* ??? Really this can be done in any block at loop level 0 that
2115 post-dominates all can_throw_internal instructions. This is
2116 the last possible moment. */
2118 emit_insn_after (seq, cfun->eh->sjlj_exit_after);
2122 sjlj_emit_dispatch_table (rtx dispatch_label, struct sjlj_lp_info *lp_info)
2124 int i, first_reachable;
2125 rtx mem, dispatch, seq, fc;
2127 fc = cfun->eh->sjlj_fc;
2131 emit_label (dispatch_label);
2133 #ifndef DONT_USE_BUILTIN_SETJMP
2134 expand_builtin_setjmp_receiver (dispatch_label);
2137 /* Load up dispatch index, exc_ptr and filter values from the
2138 function context. */
2139 mem = adjust_address (fc, TYPE_MODE (integer_type_node),
2140 sjlj_fc_call_site_ofs);
2141 dispatch = copy_to_reg (mem);
2143 mem = adjust_address (fc, word_mode, sjlj_fc_data_ofs);
2144 if (word_mode != ptr_mode)
2146 #ifdef POINTERS_EXTEND_UNSIGNED
2147 mem = convert_memory_address (ptr_mode, mem);
2149 mem = convert_to_mode (ptr_mode, mem, 0);
2152 emit_move_insn (cfun->eh->exc_ptr, mem);
2154 mem = adjust_address (fc, word_mode, sjlj_fc_data_ofs + UNITS_PER_WORD);
2155 emit_move_insn (cfun->eh->filter, mem);
2157 /* Jump to one of the directly reachable regions. */
2158 /* ??? This really ought to be using a switch statement. */
2160 first_reachable = 0;
2161 for (i = cfun->eh->last_region_number; i > 0; --i)
2163 if (! lp_info[i].directly_reachable)
2166 if (! first_reachable)
2168 first_reachable = i;
2172 emit_cmp_and_jump_insns (dispatch, GEN_INT (lp_info[i].dispatch_index),
2173 EQ, NULL_RTX, TYPE_MODE (integer_type_node), 0,
2174 cfun->eh->region_array[i]->post_landing_pad);
2180 emit_insn_before (seq, (cfun->eh->region_array[first_reachable]
2181 ->post_landing_pad));
2185 sjlj_build_landing_pads (void)
2187 struct sjlj_lp_info *lp_info;
2189 lp_info = xcalloc (cfun->eh->last_region_number + 1,
2190 sizeof (struct sjlj_lp_info));
2192 if (sjlj_find_directly_reachable_regions (lp_info))
2194 rtx dispatch_label = gen_label_rtx ();
2197 = assign_stack_local (TYPE_MODE (sjlj_fc_type_node),
2198 int_size_in_bytes (sjlj_fc_type_node),
2199 TYPE_ALIGN (sjlj_fc_type_node));
2201 sjlj_assign_call_site_values (dispatch_label, lp_info);
2202 sjlj_mark_call_sites (lp_info);
2204 sjlj_emit_function_enter (dispatch_label);
2205 sjlj_emit_dispatch_table (dispatch_label, lp_info);
2206 sjlj_emit_function_exit ();
2213 finish_eh_generation (void)
2215 /* Nothing to do if no regions created. */
2216 if (cfun->eh->region_tree == NULL)
2219 /* The object here is to provide find_basic_blocks with detailed
2220 information (via reachable_handlers) on how exception control
2221 flows within the function. In this first pass, we can include
2222 type information garnered from ERT_THROW and ERT_ALLOWED_EXCEPTIONS
2223 regions, and hope that it will be useful in deleting unreachable
2224 handlers. Subsequently, we will generate landing pads which will
2225 connect many of the handlers, and then type information will not
2226 be effective. Still, this is a win over previous implementations. */
2228 cleanup_cfg (CLEANUP_PRE_LOOP | CLEANUP_NO_INSN_DEL);
2230 /* These registers are used by the landing pads. Make sure they
2231 have been generated. */
2232 get_exception_pointer (cfun);
2233 get_exception_filter (cfun);
2235 /* Construct the landing pads. */
2237 assign_filter_values ();
2238 build_post_landing_pads ();
2239 connect_post_landing_pads ();
2240 if (USING_SJLJ_EXCEPTIONS)
2241 sjlj_build_landing_pads ();
2243 dw2_build_landing_pads ();
2245 cfun->eh->built_landing_pads = 1;
2247 /* We've totally changed the CFG. Start over. */
2248 find_exception_handler_labels ();
2249 rebuild_jump_labels (get_insns ());
2250 find_basic_blocks (get_insns (), max_reg_num (), 0);
2251 cleanup_cfg (CLEANUP_PRE_LOOP | CLEANUP_NO_INSN_DEL);
2255 ehl_hash (const void *pentry)
2257 struct ehl_map_entry *entry = (struct ehl_map_entry *) pentry;
2259 /* 2^32 * ((sqrt(5) - 1) / 2) */
2260 const hashval_t scaled_golden_ratio = 0x9e3779b9;
2261 return CODE_LABEL_NUMBER (entry->label) * scaled_golden_ratio;
2265 ehl_eq (const void *pentry, const void *pdata)
2267 struct ehl_map_entry *entry = (struct ehl_map_entry *) pentry;
2268 struct ehl_map_entry *data = (struct ehl_map_entry *) pdata;
2270 return entry->label == data->label;
2273 /* This section handles removing dead code for flow. */
2275 /* Remove LABEL from exception_handler_label_map. */
2278 remove_exception_handler_label (rtx label)
2280 struct ehl_map_entry **slot, tmp;
2282 /* If exception_handler_label_map was not built yet,
2283 there is nothing to do. */
2284 if (cfun->eh->exception_handler_label_map == NULL)
2288 slot = (struct ehl_map_entry **)
2289 htab_find_slot (cfun->eh->exception_handler_label_map, &tmp, NO_INSERT);
2293 htab_clear_slot (cfun->eh->exception_handler_label_map, (void **) slot);
2296 /* Splice REGION from the region tree etc. */
2299 remove_eh_handler (struct eh_region *region)
2301 struct eh_region **pp, **pp_start, *p, *outer, *inner;
2304 /* For the benefit of efficiently handling REG_EH_REGION notes,
2305 replace this region in the region array with its containing
2306 region. Note that previous region deletions may result in
2307 multiple copies of this region in the array, so we have a
2308 list of alternate numbers by which we are known. */
2310 outer = region->outer;
2311 cfun->eh->region_array[region->region_number] = outer;
2315 EXECUTE_IF_SET_IN_BITMAP (region->aka, 0, i,
2316 { cfun->eh->region_array[i] = outer; });
2322 outer->aka = BITMAP_GGC_ALLOC ();
2324 bitmap_a_or_b (outer->aka, outer->aka, region->aka);
2325 bitmap_set_bit (outer->aka, region->region_number);
2328 if (cfun->eh->built_landing_pads)
2329 lab = region->landing_pad;
2331 lab = region->label;
2333 remove_exception_handler_label (lab);
2336 pp_start = &outer->inner;
2338 pp_start = &cfun->eh->region_tree;
2339 for (pp = pp_start, p = *pp; p != region; pp = &p->next_peer, p = *pp)
2341 *pp = region->next_peer;
2343 inner = region->inner;
2346 for (p = inner; p->next_peer ; p = p->next_peer)
2350 p->next_peer = *pp_start;
2354 if (region->type == ERT_CATCH)
2356 struct eh_region *try, *next, *prev;
2358 for (try = region->next_peer;
2359 try->type == ERT_CATCH;
2360 try = try->next_peer)
2362 if (try->type != ERT_TRY)
2365 next = region->u.catch.next_catch;
2366 prev = region->u.catch.prev_catch;
2369 next->u.catch.prev_catch = prev;
2371 try->u.try.last_catch = prev;
2373 prev->u.catch.next_catch = next;
2376 try->u.try.catch = next;
2378 remove_eh_handler (try);
2383 /* LABEL heads a basic block that is about to be deleted. If this
2384 label corresponds to an exception region, we may be able to
2385 delete the region. */
2388 maybe_remove_eh_handler (rtx label)
2390 struct ehl_map_entry **slot, tmp;
2391 struct eh_region *region;
2393 /* ??? After generating landing pads, it's not so simple to determine
2394 if the region data is completely unused. One must examine the
2395 landing pad and the post landing pad, and whether an inner try block
2396 is referencing the catch handlers directly. */
2397 if (cfun->eh->built_landing_pads)
2401 slot = (struct ehl_map_entry **)
2402 htab_find_slot (cfun->eh->exception_handler_label_map, &tmp, NO_INSERT);
2405 region = (*slot)->region;
2409 /* Flow will want to remove MUST_NOT_THROW regions as unreachable
2410 because there is no path to the fallback call to terminate.
2411 But the region continues to affect call-site data until there
2412 are no more contained calls, which we don't see here. */
2413 if (region->type == ERT_MUST_NOT_THROW)
2415 htab_clear_slot (cfun->eh->exception_handler_label_map, (void **) slot);
2416 region->label = NULL_RTX;
2419 remove_eh_handler (region);
2422 /* Invokes CALLBACK for every exception handler label. Only used by old
2423 loop hackery; should not be used by new code. */
2426 for_each_eh_label (void (*callback) (rtx))
2428 htab_traverse (cfun->eh->exception_handler_label_map, for_each_eh_label_1,
2433 for_each_eh_label_1 (void **pentry, void *data)
2435 struct ehl_map_entry *entry = *(struct ehl_map_entry **)pentry;
2436 void (*callback) (rtx) = (void (*) (rtx)) data;
2438 (*callback) (entry->label);
2442 /* This section describes CFG exception edges for flow. */
2444 /* For communicating between calls to reachable_next_level. */
2445 struct reachable_info GTY(())
2452 /* A subroutine of reachable_next_level. Return true if TYPE, or a
2453 base class of TYPE, is in HANDLED. */
2456 check_handled (tree handled, tree type)
2460 /* We can check for exact matches without front-end help. */
2461 if (! lang_eh_type_covers)
2463 for (t = handled; t ; t = TREE_CHAIN (t))
2464 if (TREE_VALUE (t) == type)
2469 for (t = handled; t ; t = TREE_CHAIN (t))
2470 if ((*lang_eh_type_covers) (TREE_VALUE (t), type))
2477 /* A subroutine of reachable_next_level. If we are collecting a list
2478 of handlers, add one. After landing pad generation, reference
2479 it instead of the handlers themselves. Further, the handlers are
2480 all wired together, so by referencing one, we've got them all.
2481 Before landing pad generation we reference each handler individually.
2483 LP_REGION contains the landing pad; REGION is the handler. */
2486 add_reachable_handler (struct reachable_info *info, struct eh_region *lp_region, struct eh_region *region)
2491 if (cfun->eh->built_landing_pads)
2493 if (! info->handlers)
2494 info->handlers = alloc_INSN_LIST (lp_region->landing_pad, NULL_RTX);
2497 info->handlers = alloc_INSN_LIST (region->label, info->handlers);
2500 /* Process one level of exception regions for reachability.
2501 If TYPE_THROWN is non-null, then it is the *exact* type being
2502 propagated. If INFO is non-null, then collect handler labels
2503 and caught/allowed type information between invocations. */
2505 static enum reachable_code
2506 reachable_next_level (struct eh_region *region, tree type_thrown,
2507 struct reachable_info *info)
2509 switch (region->type)
2512 /* Before landing-pad generation, we model control flow
2513 directly to the individual handlers. In this way we can
2514 see that catch handler types may shadow one another. */
2515 add_reachable_handler (info, region, region);
2516 return RNL_MAYBE_CAUGHT;
2520 struct eh_region *c;
2521 enum reachable_code ret = RNL_NOT_CAUGHT;
2523 for (c = region->u.try.catch; c ; c = c->u.catch.next_catch)
2525 /* A catch-all handler ends the search. */
2526 if (c->u.catch.type_list == NULL)
2528 add_reachable_handler (info, region, c);
2534 /* If we have at least one type match, end the search. */
2535 tree tp_node = c->u.catch.type_list;
2537 for (; tp_node; tp_node = TREE_CHAIN (tp_node))
2539 tree type = TREE_VALUE (tp_node);
2541 if (type == type_thrown
2542 || (lang_eh_type_covers
2543 && (*lang_eh_type_covers) (type, type_thrown)))
2545 add_reachable_handler (info, region, c);
2550 /* If we have definitive information of a match failure,
2551 the catch won't trigger. */
2552 if (lang_eh_type_covers)
2553 return RNL_NOT_CAUGHT;
2556 /* At this point, we either don't know what type is thrown or
2557 don't have front-end assistance to help deciding if it is
2558 covered by one of the types in the list for this region.
2560 We'd then like to add this region to the list of reachable
2561 handlers since it is indeed potentially reachable based on the
2562 information we have.
2564 Actually, this handler is for sure not reachable if all the
2565 types it matches have already been caught. That is, it is only
2566 potentially reachable if at least one of the types it catches
2567 has not been previously caught. */
2570 ret = RNL_MAYBE_CAUGHT;
2573 tree tp_node = c->u.catch.type_list;
2574 bool maybe_reachable = false;
2576 /* Compute the potential reachability of this handler and
2577 update the list of types caught at the same time. */
2578 for (; tp_node; tp_node = TREE_CHAIN (tp_node))
2580 tree type = TREE_VALUE (tp_node);
2582 if (! check_handled (info->types_caught, type))
2585 = tree_cons (NULL, type, info->types_caught);
2587 maybe_reachable = true;
2591 if (maybe_reachable)
2593 add_reachable_handler (info, region, c);
2595 /* ??? If the catch type is a base class of every allowed
2596 type, then we know we can stop the search. */
2597 ret = RNL_MAYBE_CAUGHT;
2605 case ERT_ALLOWED_EXCEPTIONS:
2606 /* An empty list of types definitely ends the search. */
2607 if (region->u.allowed.type_list == NULL_TREE)
2609 add_reachable_handler (info, region, region);
2613 /* Collect a list of lists of allowed types for use in detecting
2614 when a catch may be transformed into a catch-all. */
2616 info->types_allowed = tree_cons (NULL_TREE,
2617 region->u.allowed.type_list,
2618 info->types_allowed);
2620 /* If we have definitive information about the type hierarchy,
2621 then we can tell if the thrown type will pass through the
2623 if (type_thrown && lang_eh_type_covers)
2625 if (check_handled (region->u.allowed.type_list, type_thrown))
2626 return RNL_NOT_CAUGHT;
2629 add_reachable_handler (info, region, region);
2634 add_reachable_handler (info, region, region);
2635 return RNL_MAYBE_CAUGHT;
2638 /* Catch regions are handled by their controlling try region. */
2639 return RNL_NOT_CAUGHT;
2641 case ERT_MUST_NOT_THROW:
2642 /* Here we end our search, since no exceptions may propagate.
2643 If we've touched down at some landing pad previous, then the
2644 explicit function call we generated may be used. Otherwise
2645 the call is made by the runtime. */
2646 if (info && info->handlers)
2648 add_reachable_handler (info, region, region);
2657 /* Shouldn't see these here. */
2664 /* Retrieve a list of labels of exception handlers which can be
2665 reached by a given insn. */
2668 reachable_handlers (rtx insn)
2670 struct reachable_info info;
2671 struct eh_region *region;
2675 if (GET_CODE (insn) == JUMP_INSN
2676 && GET_CODE (PATTERN (insn)) == RESX)
2677 region_number = XINT (PATTERN (insn), 0);
2680 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2681 if (!note || INTVAL (XEXP (note, 0)) <= 0)
2683 region_number = INTVAL (XEXP (note, 0));
2686 memset (&info, 0, sizeof (info));
2688 region = cfun->eh->region_array[region_number];
2690 type_thrown = NULL_TREE;
2691 if (GET_CODE (insn) == JUMP_INSN
2692 && GET_CODE (PATTERN (insn)) == RESX)
2694 /* A RESX leaves a region instead of entering it. Thus the
2695 region itself may have been deleted out from under us. */
2698 region = region->outer;
2700 else if (region->type == ERT_THROW)
2702 type_thrown = region->u.throw.type;
2703 region = region->outer;
2708 if (reachable_next_level (region, type_thrown, &info) >= RNL_CAUGHT)
2710 /* If we have processed one cleanup, there is no point in
2711 processing any more of them. Each cleanup will have an edge
2712 to the next outer cleanup region, so the flow graph will be
2714 if (region->type == ERT_CLEANUP)
2715 region = region->u.cleanup.prev_try;
2717 region = region->outer;
2720 return info.handlers;
2723 /* Determine if the given INSN can throw an exception that is caught
2724 within the function. */
2727 can_throw_internal (rtx insn)
2729 struct eh_region *region;
2733 if (! INSN_P (insn))
2736 if (GET_CODE (insn) == INSN
2737 && GET_CODE (PATTERN (insn)) == SEQUENCE)
2738 insn = XVECEXP (PATTERN (insn), 0, 0);
2740 if (GET_CODE (insn) == CALL_INSN
2741 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
2744 for (i = 0; i < 3; ++i)
2746 rtx sub = XEXP (PATTERN (insn), i);
2747 for (; sub ; sub = NEXT_INSN (sub))
2748 if (can_throw_internal (sub))
2754 /* Every insn that might throw has an EH_REGION note. */
2755 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2756 if (!note || INTVAL (XEXP (note, 0)) <= 0)
2759 region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
2761 type_thrown = NULL_TREE;
2762 if (region->type == ERT_THROW)
2764 type_thrown = region->u.throw.type;
2765 region = region->outer;
2768 /* If this exception is ignored by each and every containing region,
2769 then control passes straight out. The runtime may handle some
2770 regions, which also do not require processing internally. */
2771 for (; region; region = region->outer)
2773 enum reachable_code how = reachable_next_level (region, type_thrown, 0);
2774 if (how == RNL_BLOCKED)
2776 if (how != RNL_NOT_CAUGHT)
2783 /* Determine if the given INSN can throw an exception that is
2784 visible outside the function. */
2787 can_throw_external (rtx insn)
2789 struct eh_region *region;
2793 if (! INSN_P (insn))
2796 if (GET_CODE (insn) == INSN
2797 && GET_CODE (PATTERN (insn)) == SEQUENCE)
2798 insn = XVECEXP (PATTERN (insn), 0, 0);
2800 if (GET_CODE (insn) == CALL_INSN
2801 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
2804 for (i = 0; i < 3; ++i)
2806 rtx sub = XEXP (PATTERN (insn), i);
2807 for (; sub ; sub = NEXT_INSN (sub))
2808 if (can_throw_external (sub))
2814 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2817 /* Calls (and trapping insns) without notes are outside any
2818 exception handling region in this function. We have to
2819 assume it might throw. Given that the front end and middle
2820 ends mark known NOTHROW functions, this isn't so wildly
2822 return (GET_CODE (insn) == CALL_INSN
2823 || (flag_non_call_exceptions
2824 && may_trap_p (PATTERN (insn))));
2826 if (INTVAL (XEXP (note, 0)) <= 0)
2829 region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
2831 type_thrown = NULL_TREE;
2832 if (region->type == ERT_THROW)
2834 type_thrown = region->u.throw.type;
2835 region = region->outer;
2838 /* If the exception is caught or blocked by any containing region,
2839 then it is not seen by any calling function. */
2840 for (; region ; region = region->outer)
2841 if (reachable_next_level (region, type_thrown, NULL) >= RNL_CAUGHT)
2847 /* Set current_function_nothrow and cfun->all_throwers_are_sibcalls. */
2850 set_nothrow_function_flags (void)
2854 current_function_nothrow = 1;
2856 /* Assume cfun->all_throwers_are_sibcalls until we encounter
2857 something that can throw an exception. We specifically exempt
2858 CALL_INSNs that are SIBLING_CALL_P, as these are really jumps,
2859 and can't throw. Most CALL_INSNs are not SIBLING_CALL_P, so this
2862 cfun->all_throwers_are_sibcalls = 1;
2864 if (! flag_exceptions)
2867 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2868 if (can_throw_external (insn))
2870 current_function_nothrow = 0;
2872 if (GET_CODE (insn) != CALL_INSN || !SIBLING_CALL_P (insn))
2874 cfun->all_throwers_are_sibcalls = 0;
2879 for (insn = current_function_epilogue_delay_list; insn;
2880 insn = XEXP (insn, 1))
2881 if (can_throw_external (insn))
2883 current_function_nothrow = 0;
2885 if (GET_CODE (insn) != CALL_INSN || !SIBLING_CALL_P (insn))
2887 cfun->all_throwers_are_sibcalls = 0;
2894 /* Various hooks for unwind library. */
2896 /* Do any necessary initialization to access arbitrary stack frames.
2897 On the SPARC, this means flushing the register windows. */
2900 expand_builtin_unwind_init (void)
2902 /* Set this so all the registers get saved in our frame; we need to be
2903 able to copy the saved values for any registers from frames we unwind. */
2904 current_function_has_nonlocal_label = 1;
2906 #ifdef SETUP_FRAME_ADDRESSES
2907 SETUP_FRAME_ADDRESSES ();
2912 expand_builtin_eh_return_data_regno (tree arglist)
2914 tree which = TREE_VALUE (arglist);
2915 unsigned HOST_WIDE_INT iwhich;
2917 if (TREE_CODE (which) != INTEGER_CST)
2919 error ("argument of `__builtin_eh_return_regno' must be constant");
2923 iwhich = tree_low_cst (which, 1);
2924 iwhich = EH_RETURN_DATA_REGNO (iwhich);
2925 if (iwhich == INVALID_REGNUM)
2928 #ifdef DWARF_FRAME_REGNUM
2929 iwhich = DWARF_FRAME_REGNUM (iwhich);
2931 iwhich = DBX_REGISTER_NUMBER (iwhich);
2934 return GEN_INT (iwhich);
2937 /* Given a value extracted from the return address register or stack slot,
2938 return the actual address encoded in that value. */
2941 expand_builtin_extract_return_addr (tree addr_tree)
2943 rtx addr = expand_expr (addr_tree, NULL_RTX, Pmode, 0);
2945 if (GET_MODE (addr) != Pmode
2946 && GET_MODE (addr) != VOIDmode)
2948 #ifdef POINTERS_EXTEND_UNSIGNED
2949 addr = convert_memory_address (Pmode, addr);
2951 addr = convert_to_mode (Pmode, addr, 0);
2955 /* First mask out any unwanted bits. */
2956 #ifdef MASK_RETURN_ADDR
2957 expand_and (Pmode, addr, MASK_RETURN_ADDR, addr);
2960 /* Then adjust to find the real return address. */
2961 #if defined (RETURN_ADDR_OFFSET)
2962 addr = plus_constant (addr, RETURN_ADDR_OFFSET);
2968 /* Given an actual address in addr_tree, do any necessary encoding
2969 and return the value to be stored in the return address register or
2970 stack slot so the epilogue will return to that address. */
2973 expand_builtin_frob_return_addr (tree addr_tree)
2975 rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, 0);
2977 #ifdef POINTERS_EXTEND_UNSIGNED
2978 if (GET_MODE (addr) != Pmode)
2979 addr = convert_memory_address (Pmode, addr);
2982 #ifdef RETURN_ADDR_OFFSET
2983 addr = force_reg (Pmode, addr);
2984 addr = plus_constant (addr, -RETURN_ADDR_OFFSET);
2990 /* Set up the epilogue with the magic bits we'll need to return to the
2991 exception handler. */
2994 expand_builtin_eh_return (tree stackadj_tree ATTRIBUTE_UNUSED,
2999 #ifdef EH_RETURN_STACKADJ_RTX
3000 tmp = expand_expr (stackadj_tree, cfun->eh->ehr_stackadj, VOIDmode, 0);
3001 #ifdef POINTERS_EXTEND_UNSIGNED
3002 if (GET_MODE (tmp) != Pmode)
3003 tmp = convert_memory_address (Pmode, tmp);
3005 if (!cfun->eh->ehr_stackadj)
3006 cfun->eh->ehr_stackadj = copy_to_reg (tmp);
3007 else if (tmp != cfun->eh->ehr_stackadj)
3008 emit_move_insn (cfun->eh->ehr_stackadj, tmp);
3011 tmp = expand_expr (handler_tree, cfun->eh->ehr_handler, VOIDmode, 0);
3012 #ifdef POINTERS_EXTEND_UNSIGNED
3013 if (GET_MODE (tmp) != Pmode)
3014 tmp = convert_memory_address (Pmode, tmp);
3016 if (!cfun->eh->ehr_handler)
3017 cfun->eh->ehr_handler = copy_to_reg (tmp);
3018 else if (tmp != cfun->eh->ehr_handler)
3019 emit_move_insn (cfun->eh->ehr_handler, tmp);
3021 if (!cfun->eh->ehr_label)
3022 cfun->eh->ehr_label = gen_label_rtx ();
3023 emit_jump (cfun->eh->ehr_label);
3027 expand_eh_return (void)
3031 if (! cfun->eh->ehr_label)
3034 current_function_calls_eh_return = 1;
3036 #ifdef EH_RETURN_STACKADJ_RTX
3037 emit_move_insn (EH_RETURN_STACKADJ_RTX, const0_rtx);
3040 around_label = gen_label_rtx ();
3041 emit_jump (around_label);
3043 emit_label (cfun->eh->ehr_label);
3044 clobber_return_register ();
3046 #ifdef EH_RETURN_STACKADJ_RTX
3047 emit_move_insn (EH_RETURN_STACKADJ_RTX, cfun->eh->ehr_stackadj);
3050 #ifdef HAVE_eh_return
3052 emit_insn (gen_eh_return (cfun->eh->ehr_handler));
3056 #ifdef EH_RETURN_HANDLER_RTX
3057 emit_move_insn (EH_RETURN_HANDLER_RTX, cfun->eh->ehr_handler);
3059 error ("__builtin_eh_return not supported on this target");
3063 emit_label (around_label);
3066 /* In the following functions, we represent entries in the action table
3067 as 1-based indices. Special cases are:
3069 0: null action record, non-null landing pad; implies cleanups
3070 -1: null action record, null landing pad; implies no action
3071 -2: no call-site entry; implies must_not_throw
3072 -3: we have yet to process outer regions
3074 Further, no special cases apply to the "next" field of the record.
3075 For next, 0 means end of list. */
3077 struct action_record
3085 action_record_eq (const void *pentry, const void *pdata)
3087 const struct action_record *entry = (const struct action_record *) pentry;
3088 const struct action_record *data = (const struct action_record *) pdata;
3089 return entry->filter == data->filter && entry->next == data->next;
3093 action_record_hash (const void *pentry)
3095 const struct action_record *entry = (const struct action_record *) pentry;
3096 return entry->next * 1009 + entry->filter;
3100 add_action_record (htab_t ar_hash, int filter, int next)
3102 struct action_record **slot, *new, tmp;
3104 tmp.filter = filter;
3106 slot = (struct action_record **) htab_find_slot (ar_hash, &tmp, INSERT);
3108 if ((new = *slot) == NULL)
3110 new = xmalloc (sizeof (*new));
3111 new->offset = VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data) + 1;
3112 new->filter = filter;
3116 /* The filter value goes in untouched. The link to the next
3117 record is a "self-relative" byte offset, or zero to indicate
3118 that there is no next record. So convert the absolute 1 based
3119 indices we've been carrying around into a displacement. */
3121 push_sleb128 (&cfun->eh->action_record_data, filter);
3123 next -= VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data) + 1;
3124 push_sleb128 (&cfun->eh->action_record_data, next);
3131 collect_one_action_chain (htab_t ar_hash, struct eh_region *region)
3133 struct eh_region *c;
3136 /* If we've reached the top of the region chain, then we have
3137 no actions, and require no landing pad. */
3141 switch (region->type)
3144 /* A cleanup adds a zero filter to the beginning of the chain, but
3145 there are special cases to look out for. If there are *only*
3146 cleanups along a path, then it compresses to a zero action.
3147 Further, if there are multiple cleanups along a path, we only
3148 need to represent one of them, as that is enough to trigger
3149 entry to the landing pad at runtime. */
3150 next = collect_one_action_chain (ar_hash, region->outer);
3153 for (c = region->outer; c ; c = c->outer)
3154 if (c->type == ERT_CLEANUP)
3156 return add_action_record (ar_hash, 0, next);
3159 /* Process the associated catch regions in reverse order.
3160 If there's a catch-all handler, then we don't need to
3161 search outer regions. Use a magic -3 value to record
3162 that we haven't done the outer search. */
3164 for (c = region->u.try.last_catch; c ; c = c->u.catch.prev_catch)
3166 if (c->u.catch.type_list == NULL)
3168 /* Retrieve the filter from the head of the filter list
3169 where we have stored it (see assign_filter_values). */
3171 = TREE_INT_CST_LOW (TREE_VALUE (c->u.catch.filter_list));
3173 next = add_action_record (ar_hash, filter, 0);
3177 /* Once the outer search is done, trigger an action record for
3178 each filter we have. */
3183 next = collect_one_action_chain (ar_hash, region->outer);
3185 /* If there is no next action, terminate the chain. */
3188 /* If all outer actions are cleanups or must_not_throw,
3189 we'll have no action record for it, since we had wanted
3190 to encode these states in the call-site record directly.
3191 Add a cleanup action to the chain to catch these. */
3193 next = add_action_record (ar_hash, 0, 0);
3196 flt_node = c->u.catch.filter_list;
3197 for (; flt_node; flt_node = TREE_CHAIN (flt_node))
3199 int filter = TREE_INT_CST_LOW (TREE_VALUE (flt_node));
3200 next = add_action_record (ar_hash, filter, next);
3206 case ERT_ALLOWED_EXCEPTIONS:
3207 /* An exception specification adds its filter to the
3208 beginning of the chain. */
3209 next = collect_one_action_chain (ar_hash, region->outer);
3210 return add_action_record (ar_hash, region->u.allowed.filter,
3211 next < 0 ? 0 : next);
3213 case ERT_MUST_NOT_THROW:
3214 /* A must-not-throw region with no inner handlers or cleanups
3215 requires no call-site entry. Note that this differs from
3216 the no handler or cleanup case in that we do require an lsda
3217 to be generated. Return a magic -2 value to record this. */
3222 /* CATCH regions are handled in TRY above. THROW regions are
3223 for optimization information only and produce no output. */
3224 return collect_one_action_chain (ar_hash, region->outer);
3232 add_call_site (rtx landing_pad, int action)
3234 struct call_site_record *data = cfun->eh->call_site_data;
3235 int used = cfun->eh->call_site_data_used;
3236 int size = cfun->eh->call_site_data_size;
3240 size = (size ? size * 2 : 64);
3241 data = ggc_realloc (data, sizeof (*data) * size);
3242 cfun->eh->call_site_data = data;
3243 cfun->eh->call_site_data_size = size;
3246 data[used].landing_pad = landing_pad;
3247 data[used].action = action;
3249 cfun->eh->call_site_data_used = used + 1;
3251 return used + call_site_base;
3254 /* Turn REG_EH_REGION notes back into NOTE_INSN_EH_REGION notes.
3255 The new note numbers will not refer to region numbers, but
3256 instead to call site entries. */
3259 convert_to_eh_region_ranges (void)
3261 rtx insn, iter, note;
3263 int last_action = -3;
3264 rtx last_action_insn = NULL_RTX;
3265 rtx last_landing_pad = NULL_RTX;
3266 rtx first_no_action_insn = NULL_RTX;
3269 if (USING_SJLJ_EXCEPTIONS || cfun->eh->region_tree == NULL)
3272 VARRAY_UCHAR_INIT (cfun->eh->action_record_data, 64, "action_record_data");
3274 ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
3276 for (iter = get_insns (); iter ; iter = NEXT_INSN (iter))
3279 struct eh_region *region;
3281 rtx this_landing_pad;
3284 if (GET_CODE (insn) == INSN
3285 && GET_CODE (PATTERN (insn)) == SEQUENCE)
3286 insn = XVECEXP (PATTERN (insn), 0, 0);
3288 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3291 if (! (GET_CODE (insn) == CALL_INSN
3292 || (flag_non_call_exceptions
3293 && may_trap_p (PATTERN (insn)))))
3300 if (INTVAL (XEXP (note, 0)) <= 0)
3302 region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
3303 this_action = collect_one_action_chain (ar_hash, region);
3306 /* Existence of catch handlers, or must-not-throw regions
3307 implies that an lsda is needed (even if empty). */
3308 if (this_action != -1)
3309 cfun->uses_eh_lsda = 1;
3311 /* Delay creation of region notes for no-action regions
3312 until we're sure that an lsda will be required. */
3313 else if (last_action == -3)
3315 first_no_action_insn = iter;
3319 /* Cleanups and handlers may share action chains but not
3320 landing pads. Collect the landing pad for this region. */
3321 if (this_action >= 0)
3323 struct eh_region *o;
3324 for (o = region; ! o->landing_pad ; o = o->outer)
3326 this_landing_pad = o->landing_pad;
3329 this_landing_pad = NULL_RTX;
3331 /* Differing actions or landing pads implies a change in call-site
3332 info, which implies some EH_REGION note should be emitted. */
3333 if (last_action != this_action
3334 || last_landing_pad != this_landing_pad)
3336 /* If we'd not seen a previous action (-3) or the previous
3337 action was must-not-throw (-2), then we do not need an
3339 if (last_action >= -1)
3341 /* If we delayed the creation of the begin, do it now. */
3342 if (first_no_action_insn)
3344 call_site = add_call_site (NULL_RTX, 0);
3345 note = emit_note_before (NOTE_INSN_EH_REGION_BEG,
3346 first_no_action_insn);
3347 NOTE_EH_HANDLER (note) = call_site;
3348 first_no_action_insn = NULL_RTX;
3351 note = emit_note_after (NOTE_INSN_EH_REGION_END,
3353 NOTE_EH_HANDLER (note) = call_site;
3356 /* If the new action is must-not-throw, then no region notes
3358 if (this_action >= -1)
3360 call_site = add_call_site (this_landing_pad,
3361 this_action < 0 ? 0 : this_action);
3362 note = emit_note_before (NOTE_INSN_EH_REGION_BEG, iter);
3363 NOTE_EH_HANDLER (note) = call_site;
3366 last_action = this_action;
3367 last_landing_pad = this_landing_pad;
3369 last_action_insn = iter;
3372 if (last_action >= -1 && ! first_no_action_insn)
3374 note = emit_note_after (NOTE_INSN_EH_REGION_END, last_action_insn);
3375 NOTE_EH_HANDLER (note) = call_site;
3378 htab_delete (ar_hash);
3383 push_uleb128 (varray_type *data_area, unsigned int value)
3387 unsigned char byte = value & 0x7f;
3391 VARRAY_PUSH_UCHAR (*data_area, byte);
3397 push_sleb128 (varray_type *data_area, int value)
3404 byte = value & 0x7f;
3406 more = ! ((value == 0 && (byte & 0x40) == 0)
3407 || (value == -1 && (byte & 0x40) != 0));
3410 VARRAY_PUSH_UCHAR (*data_area, byte);
3416 #ifndef HAVE_AS_LEB128
3418 dw2_size_of_call_site_table (void)
3420 int n = cfun->eh->call_site_data_used;
3421 int size = n * (4 + 4 + 4);
3424 for (i = 0; i < n; ++i)
3426 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3427 size += size_of_uleb128 (cs->action);
3434 sjlj_size_of_call_site_table (void)
3436 int n = cfun->eh->call_site_data_used;
3440 for (i = 0; i < n; ++i)
3442 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3443 size += size_of_uleb128 (INTVAL (cs->landing_pad));
3444 size += size_of_uleb128 (cs->action);
3452 dw2_output_call_site_table (void)
3454 const char *const function_start_lab
3455 = IDENTIFIER_POINTER (current_function_func_begin_label);
3456 int n = cfun->eh->call_site_data_used;
3459 for (i = 0; i < n; ++i)
3461 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3462 char reg_start_lab[32];
3463 char reg_end_lab[32];
3464 char landing_pad_lab[32];
3466 ASM_GENERATE_INTERNAL_LABEL (reg_start_lab, "LEHB", call_site_base + i);
3467 ASM_GENERATE_INTERNAL_LABEL (reg_end_lab, "LEHE", call_site_base + i);
3469 if (cs->landing_pad)
3470 ASM_GENERATE_INTERNAL_LABEL (landing_pad_lab, "L",
3471 CODE_LABEL_NUMBER (cs->landing_pad));
3473 /* ??? Perhaps use insn length scaling if the assembler supports
3474 generic arithmetic. */
3475 /* ??? Perhaps use attr_length to choose data1 or data2 instead of
3476 data4 if the function is small enough. */
3477 #ifdef HAVE_AS_LEB128
3478 dw2_asm_output_delta_uleb128 (reg_start_lab, function_start_lab,
3479 "region %d start", i);
3480 dw2_asm_output_delta_uleb128 (reg_end_lab, reg_start_lab,
3482 if (cs->landing_pad)
3483 dw2_asm_output_delta_uleb128 (landing_pad_lab, function_start_lab,
3486 dw2_asm_output_data_uleb128 (0, "landing pad");
3488 dw2_asm_output_delta (4, reg_start_lab, function_start_lab,
3489 "region %d start", i);
3490 dw2_asm_output_delta (4, reg_end_lab, reg_start_lab, "length");
3491 if (cs->landing_pad)
3492 dw2_asm_output_delta (4, landing_pad_lab, function_start_lab,
3495 dw2_asm_output_data (4, 0, "landing pad");
3497 dw2_asm_output_data_uleb128 (cs->action, "action");
3500 call_site_base += n;
3504 sjlj_output_call_site_table (void)
3506 int n = cfun->eh->call_site_data_used;
3509 for (i = 0; i < n; ++i)
3511 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3513 dw2_asm_output_data_uleb128 (INTVAL (cs->landing_pad),
3514 "region %d landing pad", i);
3515 dw2_asm_output_data_uleb128 (cs->action, "action");
3518 call_site_base += n;
3521 /* Tell assembler to switch to the section for the exception handling
3525 default_exception_section (void)
3527 if (targetm.have_named_sections)
3530 #ifdef HAVE_LD_RO_RW_SECTION_MIXING
3531 int tt_format = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3534 || ((tt_format & 0x70) != DW_EH_PE_absptr
3535 && (tt_format & 0x70) != DW_EH_PE_aligned))
3536 ? 0 : SECTION_WRITE;
3538 flags = SECTION_WRITE;
3540 named_section_flags (".gcc_except_table", flags);
3545 readonly_data_section ();
3549 output_function_exception_table (void)
3551 int tt_format, cs_format, lp_format, i, n;
3552 #ifdef HAVE_AS_LEB128
3553 char ttype_label[32];
3554 char cs_after_size_label[32];
3555 char cs_end_label[32];
3560 int tt_format_size = 0;
3562 /* Not all functions need anything. */
3563 if (! cfun->uses_eh_lsda)
3566 #ifdef IA64_UNWIND_INFO
3567 fputs ("\t.personality\t", asm_out_file);
3568 output_addr_const (asm_out_file, eh_personality_libfunc);
3569 fputs ("\n\t.handlerdata\n", asm_out_file);
3570 /* Note that varasm still thinks we're in the function's code section.
3571 The ".endp" directive that will immediately follow will take us back. */
3573 (*targetm.asm_out.exception_section) ();
3576 have_tt_data = (VARRAY_ACTIVE_SIZE (cfun->eh->ttype_data) > 0
3577 || VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data) > 0);
3579 /* Indicate the format of the @TType entries. */
3581 tt_format = DW_EH_PE_omit;
3584 tt_format = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3585 #ifdef HAVE_AS_LEB128
3586 ASM_GENERATE_INTERNAL_LABEL (ttype_label, "LLSDATT",
3587 current_function_funcdef_no);
3589 tt_format_size = size_of_encoded_value (tt_format);
3591 assemble_align (tt_format_size * BITS_PER_UNIT);
3594 (*targetm.asm_out.internal_label) (asm_out_file, "LLSDA",
3595 current_function_funcdef_no);
3597 /* The LSDA header. */
3599 /* Indicate the format of the landing pad start pointer. An omitted
3600 field implies @LPStart == @Start. */
3601 /* Currently we always put @LPStart == @Start. This field would
3602 be most useful in moving the landing pads completely out of
3603 line to another section, but it could also be used to minimize
3604 the size of uleb128 landing pad offsets. */
3605 lp_format = DW_EH_PE_omit;
3606 dw2_asm_output_data (1, lp_format, "@LPStart format (%s)",
3607 eh_data_format_name (lp_format));
3609 /* @LPStart pointer would go here. */
3611 dw2_asm_output_data (1, tt_format, "@TType format (%s)",
3612 eh_data_format_name (tt_format));
3614 #ifndef HAVE_AS_LEB128
3615 if (USING_SJLJ_EXCEPTIONS)
3616 call_site_len = sjlj_size_of_call_site_table ();
3618 call_site_len = dw2_size_of_call_site_table ();
3621 /* A pc-relative 4-byte displacement to the @TType data. */
3624 #ifdef HAVE_AS_LEB128
3625 char ttype_after_disp_label[32];
3626 ASM_GENERATE_INTERNAL_LABEL (ttype_after_disp_label, "LLSDATTD",
3627 current_function_funcdef_no);
3628 dw2_asm_output_delta_uleb128 (ttype_label, ttype_after_disp_label,
3629 "@TType base offset");
3630 ASM_OUTPUT_LABEL (asm_out_file, ttype_after_disp_label);
3632 /* Ug. Alignment queers things. */
3633 unsigned int before_disp, after_disp, last_disp, disp;
3635 before_disp = 1 + 1;
3636 after_disp = (1 + size_of_uleb128 (call_site_len)
3638 + VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data)
3639 + (VARRAY_ACTIVE_SIZE (cfun->eh->ttype_data)
3645 unsigned int disp_size, pad;
3648 disp_size = size_of_uleb128 (disp);
3649 pad = before_disp + disp_size + after_disp;
3650 if (pad % tt_format_size)
3651 pad = tt_format_size - (pad % tt_format_size);
3654 disp = after_disp + pad;
3656 while (disp != last_disp);
3658 dw2_asm_output_data_uleb128 (disp, "@TType base offset");
3662 /* Indicate the format of the call-site offsets. */
3663 #ifdef HAVE_AS_LEB128
3664 cs_format = DW_EH_PE_uleb128;
3666 cs_format = DW_EH_PE_udata4;
3668 dw2_asm_output_data (1, cs_format, "call-site format (%s)",
3669 eh_data_format_name (cs_format));
3671 #ifdef HAVE_AS_LEB128
3672 ASM_GENERATE_INTERNAL_LABEL (cs_after_size_label, "LLSDACSB",
3673 current_function_funcdef_no);
3674 ASM_GENERATE_INTERNAL_LABEL (cs_end_label, "LLSDACSE",
3675 current_function_funcdef_no);
3676 dw2_asm_output_delta_uleb128 (cs_end_label, cs_after_size_label,
3677 "Call-site table length");
3678 ASM_OUTPUT_LABEL (asm_out_file, cs_after_size_label);
3679 if (USING_SJLJ_EXCEPTIONS)
3680 sjlj_output_call_site_table ();
3682 dw2_output_call_site_table ();
3683 ASM_OUTPUT_LABEL (asm_out_file, cs_end_label);
3685 dw2_asm_output_data_uleb128 (call_site_len,"Call-site table length");
3686 if (USING_SJLJ_EXCEPTIONS)
3687 sjlj_output_call_site_table ();
3689 dw2_output_call_site_table ();
3692 /* ??? Decode and interpret the data for flag_debug_asm. */
3693 n = VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data);
3694 for (i = 0; i < n; ++i)
3695 dw2_asm_output_data (1, VARRAY_UCHAR (cfun->eh->action_record_data, i),
3696 (i ? NULL : "Action record table"));
3699 assemble_align (tt_format_size * BITS_PER_UNIT);
3701 i = VARRAY_ACTIVE_SIZE (cfun->eh->ttype_data);
3704 tree type = VARRAY_TREE (cfun->eh->ttype_data, i);
3707 if (type == NULL_TREE)
3711 struct cgraph_varpool_node *node;
3713 type = lookup_type_for_runtime (type);
3714 value = expand_expr (type, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
3716 /* Let cgraph know that the rtti decl is used. Not all of the
3717 paths below go through assemble_integer, which would take
3718 care of this for us. */
3719 if (TREE_CODE (type) != ADDR_EXPR)
3721 type = TREE_OPERAND (type, 0);
3722 node = cgraph_varpool_node (type);
3724 cgraph_varpool_mark_needed_node (node);
3727 if (tt_format == DW_EH_PE_absptr || tt_format == DW_EH_PE_aligned)
3728 assemble_integer (value, tt_format_size,
3729 tt_format_size * BITS_PER_UNIT, 1);
3731 dw2_asm_output_encoded_addr_rtx (tt_format, value, NULL);
3734 #ifdef HAVE_AS_LEB128
3736 ASM_OUTPUT_LABEL (asm_out_file, ttype_label);
3739 /* ??? Decode and interpret the data for flag_debug_asm. */
3740 n = VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data);
3741 for (i = 0; i < n; ++i)
3742 dw2_asm_output_data (1, VARRAY_UCHAR (cfun->eh->ehspec_data, i),
3743 (i ? NULL : "Exception specification table"));
3745 function_section (current_function_decl);
3748 #include "gt-except.h"