1 /* Implements exception handling.
2 Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 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"
77 /* Provide defaults for stuff that may not be defined when using
79 #ifndef EH_RETURN_STACKADJ_RTX
80 #define EH_RETURN_STACKADJ_RTX 0
82 #ifndef EH_RETURN_HANDLER_RTX
83 #define EH_RETURN_HANDLER_RTX 0
85 #ifndef EH_RETURN_DATA_REGNO
86 #define EH_RETURN_DATA_REGNO(N) INVALID_REGNUM
90 /* Nonzero means enable synchronous exceptions for non-call instructions. */
91 int flag_non_call_exceptions;
93 /* Protect cleanup actions with must-not-throw regions, with a call
94 to the given failure handler. */
95 tree (*lang_protect_cleanup_actions) PARAMS ((void));
97 /* Return true if type A catches type B. */
98 int (*lang_eh_type_covers) PARAMS ((tree a, tree b));
100 /* Map a type to a runtime object to match type. */
101 tree (*lang_eh_runtime_type) PARAMS ((tree));
103 /* A hash table of label to region number. */
105 struct ehl_map_entry GTY(())
108 struct eh_region *region;
111 static int call_site_base;
112 static GTY ((param_is (union tree_node)))
113 htab_t type_to_runtime_map;
115 /* Describe the SjLj_Function_Context structure. */
116 static GTY(()) tree sjlj_fc_type_node;
117 static int sjlj_fc_call_site_ofs;
118 static int sjlj_fc_data_ofs;
119 static int sjlj_fc_personality_ofs;
120 static int sjlj_fc_lsda_ofs;
121 static int sjlj_fc_jbuf_ofs;
123 /* Describes one exception region. */
124 struct eh_region GTY(())
126 /* The immediately surrounding region. */
127 struct eh_region *outer;
129 /* The list of immediately contained regions. */
130 struct eh_region *inner;
131 struct eh_region *next_peer;
133 /* An identifier for this region. */
136 /* When a region is deleted, its parents inherit the REG_EH_REGION
137 numbers already assigned. */
140 /* Each region does exactly one thing. */
147 ERT_ALLOWED_EXCEPTIONS,
153 /* Holds the action to perform based on the preceding type. */
155 /* A list of catch blocks, a surrounding try block,
156 and the label for continuing after a catch. */
157 struct eh_region_u_try {
158 struct eh_region *catch;
159 struct eh_region *last_catch;
160 struct eh_region *prev_try;
162 } GTY ((tag ("ERT_TRY"))) try;
164 /* The list through the catch handlers, the list of type objects
165 matched, and the list of associated filters. */
166 struct eh_region_u_catch {
167 struct eh_region *next_catch;
168 struct eh_region *prev_catch;
171 } GTY ((tag ("ERT_CATCH"))) catch;
173 /* A tree_list of allowed types. */
174 struct eh_region_u_allowed {
177 } GTY ((tag ("ERT_ALLOWED_EXCEPTIONS"))) allowed;
179 /* The type given by a call to "throw foo();", or discovered
181 struct eh_region_u_throw {
183 } GTY ((tag ("ERT_THROW"))) throw;
185 /* Retain the cleanup expression even after expansion so that
186 we can match up fixup regions. */
187 struct eh_region_u_cleanup {
189 struct eh_region *prev_try;
190 } GTY ((tag ("ERT_CLEANUP"))) cleanup;
192 /* The real region (by expression and by pointer) that fixup code
194 struct eh_region_u_fixup {
196 struct eh_region *real_region;
197 } GTY ((tag ("ERT_FIXUP"))) fixup;
198 } GTY ((desc ("%0.type"))) u;
200 /* Entry point for this region's handler before landing pads are built. */
203 /* Entry point for this region's handler from the runtime eh library. */
206 /* Entry point for this region's handler from an inner region. */
207 rtx post_landing_pad;
209 /* The RESX insn for handing off control to the next outermost handler,
214 struct call_site_record GTY(())
220 /* Used to save exception status for each function. */
221 struct eh_status GTY(())
223 /* The tree of all regions for this function. */
224 struct eh_region *region_tree;
226 /* The same information as an indexable array. */
227 struct eh_region ** GTY ((length ("%h.last_region_number"))) region_array;
229 /* The most recently open region. */
230 struct eh_region *cur_region;
232 /* This is the region for which we are processing catch blocks. */
233 struct eh_region *try_region;
238 int built_landing_pads;
239 int last_region_number;
241 varray_type ttype_data;
242 varray_type ehspec_data;
243 varray_type action_record_data;
245 htab_t GTY ((param_is (struct ehl_map_entry))) exception_handler_label_map;
247 struct call_site_record * GTY ((length ("%h.call_site_data_used")))
249 int call_site_data_used;
250 int call_site_data_size;
261 static int t2r_eq PARAMS ((const PTR,
263 static hashval_t t2r_hash PARAMS ((const PTR));
264 static void add_type_for_runtime PARAMS ((tree));
265 static tree lookup_type_for_runtime PARAMS ((tree));
267 static struct eh_region *expand_eh_region_end PARAMS ((void));
269 static rtx get_exception_filter PARAMS ((struct function *));
271 static void collect_eh_region_array PARAMS ((void));
272 static void resolve_fixup_regions PARAMS ((void));
273 static void remove_fixup_regions PARAMS ((void));
274 static void remove_unreachable_regions PARAMS ((rtx));
275 static void convert_from_eh_region_ranges_1 PARAMS ((rtx *, int *, int));
277 static struct eh_region *duplicate_eh_region_1 PARAMS ((struct eh_region *,
278 struct inline_remap *));
279 static void duplicate_eh_region_2 PARAMS ((struct eh_region *,
280 struct eh_region **));
281 static int ttypes_filter_eq PARAMS ((const PTR,
283 static hashval_t ttypes_filter_hash PARAMS ((const PTR));
284 static int ehspec_filter_eq PARAMS ((const PTR,
286 static hashval_t ehspec_filter_hash PARAMS ((const PTR));
287 static int add_ttypes_entry PARAMS ((htab_t, tree));
288 static int add_ehspec_entry PARAMS ((htab_t, htab_t,
290 static void assign_filter_values PARAMS ((void));
291 static void build_post_landing_pads PARAMS ((void));
292 static void connect_post_landing_pads PARAMS ((void));
293 static void dw2_build_landing_pads PARAMS ((void));
296 static bool sjlj_find_directly_reachable_regions
297 PARAMS ((struct sjlj_lp_info *));
298 static void sjlj_assign_call_site_values
299 PARAMS ((rtx, struct sjlj_lp_info *));
300 static void sjlj_mark_call_sites
301 PARAMS ((struct sjlj_lp_info *));
302 static void sjlj_emit_function_enter PARAMS ((rtx));
303 static void sjlj_emit_function_exit PARAMS ((void));
304 static void sjlj_emit_dispatch_table
305 PARAMS ((rtx, struct sjlj_lp_info *));
306 static void sjlj_build_landing_pads PARAMS ((void));
308 static hashval_t ehl_hash PARAMS ((const PTR));
309 static int ehl_eq PARAMS ((const PTR,
311 static void add_ehl_entry PARAMS ((rtx,
312 struct eh_region *));
313 static void remove_exception_handler_label PARAMS ((rtx));
314 static void remove_eh_handler PARAMS ((struct eh_region *));
315 static int for_each_eh_label_1 PARAMS ((PTR *, PTR));
317 struct reachable_info;
319 /* The return value of reachable_next_level. */
322 /* The given exception is not processed by the given region. */
324 /* The given exception may need processing by the given region. */
326 /* The given exception is completely processed by the given region. */
328 /* The given exception is completely processed by the runtime. */
332 static int check_handled PARAMS ((tree, tree));
333 static void add_reachable_handler
334 PARAMS ((struct reachable_info *, struct eh_region *,
335 struct eh_region *));
336 static enum reachable_code reachable_next_level
337 PARAMS ((struct eh_region *, tree, struct reachable_info *));
339 static int action_record_eq PARAMS ((const PTR,
341 static hashval_t action_record_hash PARAMS ((const PTR));
342 static int add_action_record PARAMS ((htab_t, int, int));
343 static int collect_one_action_chain PARAMS ((htab_t,
344 struct eh_region *));
345 static int add_call_site PARAMS ((rtx, int));
347 static void push_uleb128 PARAMS ((varray_type *,
349 static void push_sleb128 PARAMS ((varray_type *, int));
350 #ifndef HAVE_AS_LEB128
351 static int dw2_size_of_call_site_table PARAMS ((void));
352 static int sjlj_size_of_call_site_table PARAMS ((void));
354 static void dw2_output_call_site_table PARAMS ((void));
355 static void sjlj_output_call_site_table PARAMS ((void));
358 /* Routine to see if exception handling is turned on.
359 DO_WARN is nonzero if we want to inform the user that exception
360 handling is turned off.
362 This is used to ensure that -fexceptions has been specified if the
363 compiler tries to use any exception-specific functions. */
369 if (! flag_exceptions)
371 static int warned = 0;
372 if (! warned && do_warn)
374 error ("exception handling disabled, use -fexceptions to enable");
386 if (! flag_exceptions)
389 type_to_runtime_map = htab_create_ggc (31, t2r_hash, t2r_eq, NULL);
391 /* Create the SjLj_Function_Context structure. This should match
392 the definition in unwind-sjlj.c. */
393 if (USING_SJLJ_EXCEPTIONS)
395 tree f_jbuf, f_per, f_lsda, f_prev, f_cs, f_data, tmp;
397 sjlj_fc_type_node = (*lang_hooks.types.make_type) (RECORD_TYPE);
399 f_prev = build_decl (FIELD_DECL, get_identifier ("__prev"),
400 build_pointer_type (sjlj_fc_type_node));
401 DECL_FIELD_CONTEXT (f_prev) = sjlj_fc_type_node;
403 f_cs = build_decl (FIELD_DECL, get_identifier ("__call_site"),
405 DECL_FIELD_CONTEXT (f_cs) = sjlj_fc_type_node;
407 tmp = build_index_type (build_int_2 (4 - 1, 0));
408 tmp = build_array_type ((*lang_hooks.types.type_for_mode) (word_mode, 1),
410 f_data = build_decl (FIELD_DECL, get_identifier ("__data"), tmp);
411 DECL_FIELD_CONTEXT (f_data) = sjlj_fc_type_node;
413 f_per = build_decl (FIELD_DECL, get_identifier ("__personality"),
415 DECL_FIELD_CONTEXT (f_per) = sjlj_fc_type_node;
417 f_lsda = build_decl (FIELD_DECL, get_identifier ("__lsda"),
419 DECL_FIELD_CONTEXT (f_lsda) = sjlj_fc_type_node;
421 #ifdef DONT_USE_BUILTIN_SETJMP
423 tmp = build_int_2 (JMP_BUF_SIZE - 1, 0);
425 /* Should be large enough for most systems, if it is not,
426 JMP_BUF_SIZE should be defined with the proper value. It will
427 also tend to be larger than necessary for most systems, a more
428 optimal port will define JMP_BUF_SIZE. */
429 tmp = build_int_2 (FIRST_PSEUDO_REGISTER + 2 - 1, 0);
432 /* This is 2 for builtin_setjmp, plus whatever the target requires
433 via STACK_SAVEAREA_MODE (SAVE_NONLOCAL). */
434 tmp = build_int_2 ((GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL))
435 / GET_MODE_SIZE (Pmode)) + 2 - 1, 0);
437 tmp = build_index_type (tmp);
438 tmp = build_array_type (ptr_type_node, tmp);
439 f_jbuf = build_decl (FIELD_DECL, get_identifier ("__jbuf"), tmp);
440 #ifdef DONT_USE_BUILTIN_SETJMP
441 /* We don't know what the alignment requirements of the
442 runtime's jmp_buf has. Overestimate. */
443 DECL_ALIGN (f_jbuf) = BIGGEST_ALIGNMENT;
444 DECL_USER_ALIGN (f_jbuf) = 1;
446 DECL_FIELD_CONTEXT (f_jbuf) = sjlj_fc_type_node;
448 TYPE_FIELDS (sjlj_fc_type_node) = f_prev;
449 TREE_CHAIN (f_prev) = f_cs;
450 TREE_CHAIN (f_cs) = f_data;
451 TREE_CHAIN (f_data) = f_per;
452 TREE_CHAIN (f_per) = f_lsda;
453 TREE_CHAIN (f_lsda) = f_jbuf;
455 layout_type (sjlj_fc_type_node);
457 /* Cache the interesting field offsets so that we have
458 easy access from rtl. */
459 sjlj_fc_call_site_ofs
460 = (tree_low_cst (DECL_FIELD_OFFSET (f_cs), 1)
461 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_cs), 1) / BITS_PER_UNIT);
463 = (tree_low_cst (DECL_FIELD_OFFSET (f_data), 1)
464 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_data), 1) / BITS_PER_UNIT);
465 sjlj_fc_personality_ofs
466 = (tree_low_cst (DECL_FIELD_OFFSET (f_per), 1)
467 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_per), 1) / BITS_PER_UNIT);
469 = (tree_low_cst (DECL_FIELD_OFFSET (f_lsda), 1)
470 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_lsda), 1) / BITS_PER_UNIT);
472 = (tree_low_cst (DECL_FIELD_OFFSET (f_jbuf), 1)
473 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_jbuf), 1) / BITS_PER_UNIT);
478 init_eh_for_function ()
480 cfun->eh = (struct eh_status *)
481 ggc_alloc_cleared (sizeof (struct eh_status));
484 /* Start an exception handling region. All instructions emitted
485 after this point are considered to be part of the region until
486 expand_eh_region_end is invoked. */
489 expand_eh_region_start ()
491 struct eh_region *new_region;
492 struct eh_region *cur_region;
498 /* Insert a new blank region as a leaf in the tree. */
499 new_region = (struct eh_region *) ggc_alloc_cleared (sizeof (*new_region));
500 cur_region = cfun->eh->cur_region;
501 new_region->outer = cur_region;
504 new_region->next_peer = cur_region->inner;
505 cur_region->inner = new_region;
509 new_region->next_peer = cfun->eh->region_tree;
510 cfun->eh->region_tree = new_region;
512 cfun->eh->cur_region = new_region;
514 /* Create a note marking the start of this region. */
515 new_region->region_number = ++cfun->eh->last_region_number;
516 note = emit_note (NULL, NOTE_INSN_EH_REGION_BEG);
517 NOTE_EH_HANDLER (note) = new_region->region_number;
520 /* Common code to end a region. Returns the region just ended. */
522 static struct eh_region *
523 expand_eh_region_end ()
525 struct eh_region *cur_region = cfun->eh->cur_region;
528 /* Create a note marking the end of this region. */
529 note = emit_note (NULL, NOTE_INSN_EH_REGION_END);
530 NOTE_EH_HANDLER (note) = cur_region->region_number;
533 cfun->eh->cur_region = cur_region->outer;
538 /* End an exception handling region for a cleanup. HANDLER is an
539 expression to expand for the cleanup. */
542 expand_eh_region_end_cleanup (handler)
545 struct eh_region *region;
546 tree protect_cleanup_actions;
553 region = expand_eh_region_end ();
554 region->type = ERT_CLEANUP;
555 region->label = gen_label_rtx ();
556 region->u.cleanup.exp = handler;
557 region->u.cleanup.prev_try = cfun->eh->try_region;
559 around_label = gen_label_rtx ();
560 emit_jump (around_label);
562 emit_label (region->label);
564 /* Give the language a chance to specify an action to be taken if an
565 exception is thrown that would propagate out of the HANDLER. */
566 protect_cleanup_actions
567 = (lang_protect_cleanup_actions
568 ? (*lang_protect_cleanup_actions) ()
571 if (protect_cleanup_actions)
572 expand_eh_region_start ();
574 /* In case this cleanup involves an inline destructor with a try block in
575 it, we need to save the EH return data registers around it. */
576 data_save[0] = gen_reg_rtx (ptr_mode);
577 emit_move_insn (data_save[0], get_exception_pointer (cfun));
578 data_save[1] = gen_reg_rtx (word_mode);
579 emit_move_insn (data_save[1], get_exception_filter (cfun));
581 expand_expr (handler, const0_rtx, VOIDmode, 0);
583 emit_move_insn (cfun->eh->exc_ptr, data_save[0]);
584 emit_move_insn (cfun->eh->filter, data_save[1]);
586 if (protect_cleanup_actions)
587 expand_eh_region_end_must_not_throw (protect_cleanup_actions);
589 /* We need any stack adjustment complete before the around_label. */
590 do_pending_stack_adjust ();
592 /* We delay the generation of the _Unwind_Resume until we generate
593 landing pads. We emit a marker here so as to get good control
594 flow data in the meantime. */
596 = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
599 emit_label (around_label);
602 /* End an exception handling region for a try block, and prepares
603 for subsequent calls to expand_start_catch. */
606 expand_start_all_catch ()
608 struct eh_region *region;
613 region = expand_eh_region_end ();
614 region->type = ERT_TRY;
615 region->u.try.prev_try = cfun->eh->try_region;
616 region->u.try.continue_label = gen_label_rtx ();
618 cfun->eh->try_region = region;
620 emit_jump (region->u.try.continue_label);
623 /* Begin a catch clause. TYPE is the type caught, a list of such types, or
624 null if this is a catch-all clause. Providing a type list enables to
625 associate the catch region with potentially several exception types, which
626 is useful e.g. for Ada. */
629 expand_start_catch (type_or_list)
632 struct eh_region *t, *c, *l;
638 type_list = type_or_list;
642 /* Ensure to always end up with a type list to normalize further
643 processing, then register each type against the runtime types
647 if (TREE_CODE (type_or_list) != TREE_LIST)
648 type_list = tree_cons (NULL_TREE, type_or_list, NULL_TREE);
650 type_node = type_list;
651 for (; type_node; type_node = TREE_CHAIN (type_node))
652 add_type_for_runtime (TREE_VALUE (type_node));
655 expand_eh_region_start ();
657 t = cfun->eh->try_region;
658 c = cfun->eh->cur_region;
660 c->u.catch.type_list = type_list;
661 c->label = gen_label_rtx ();
663 l = t->u.try.last_catch;
664 c->u.catch.prev_catch = l;
666 l->u.catch.next_catch = c;
669 t->u.try.last_catch = c;
671 emit_label (c->label);
674 /* End a catch clause. Control will resume after the try/catch block. */
679 struct eh_region *try_region;
684 expand_eh_region_end ();
685 try_region = cfun->eh->try_region;
687 emit_jump (try_region->u.try.continue_label);
690 /* End a sequence of catch handlers for a try block. */
693 expand_end_all_catch ()
695 struct eh_region *try_region;
700 try_region = cfun->eh->try_region;
701 cfun->eh->try_region = try_region->u.try.prev_try;
703 emit_label (try_region->u.try.continue_label);
706 /* End an exception region for an exception type filter. ALLOWED is a
707 TREE_LIST of types to be matched by the runtime. FAILURE is an
708 expression to invoke if a mismatch occurs.
710 ??? We could use these semantics for calls to rethrow, too; if we can
711 see the surrounding catch clause, we know that the exception we're
712 rethrowing satisfies the "filter" of the catch type. */
715 expand_eh_region_end_allowed (allowed, failure)
716 tree allowed, failure;
718 struct eh_region *region;
724 region = expand_eh_region_end ();
725 region->type = ERT_ALLOWED_EXCEPTIONS;
726 region->u.allowed.type_list = allowed;
727 region->label = gen_label_rtx ();
729 for (; allowed ; allowed = TREE_CHAIN (allowed))
730 add_type_for_runtime (TREE_VALUE (allowed));
732 /* We must emit the call to FAILURE here, so that if this function
733 throws a different exception, that it will be processed by the
736 around_label = gen_label_rtx ();
737 emit_jump (around_label);
739 emit_label (region->label);
740 expand_expr (failure, const0_rtx, VOIDmode, EXPAND_NORMAL);
741 /* We must adjust the stack before we reach the AROUND_LABEL because
742 the call to FAILURE does not occur on all paths to the
744 do_pending_stack_adjust ();
746 emit_label (around_label);
749 /* End an exception region for a must-not-throw filter. FAILURE is an
750 expression invoke if an uncaught exception propagates this far.
752 This is conceptually identical to expand_eh_region_end_allowed with
753 an empty allowed list (if you passed "std::terminate" instead of
754 "__cxa_call_unexpected"), but they are represented differently in
758 expand_eh_region_end_must_not_throw (failure)
761 struct eh_region *region;
767 region = expand_eh_region_end ();
768 region->type = ERT_MUST_NOT_THROW;
769 region->label = gen_label_rtx ();
771 /* We must emit the call to FAILURE here, so that if this function
772 throws a different exception, that it will be processed by the
775 around_label = gen_label_rtx ();
776 emit_jump (around_label);
778 emit_label (region->label);
779 expand_expr (failure, const0_rtx, VOIDmode, EXPAND_NORMAL);
781 emit_label (around_label);
784 /* End an exception region for a throw. No handling goes on here,
785 but it's the easiest way for the front-end to indicate what type
789 expand_eh_region_end_throw (type)
792 struct eh_region *region;
797 region = expand_eh_region_end ();
798 region->type = ERT_THROW;
799 region->u.throw.type = type;
802 /* End a fixup region. Within this region the cleanups for the immediately
803 enclosing region are _not_ run. This is used for goto cleanup to avoid
804 destroying an object twice.
806 This would be an extraordinarily simple prospect, were it not for the
807 fact that we don't actually know what the immediately enclosing region
808 is. This surprising fact is because expand_cleanups is currently
809 generating a sequence that it will insert somewhere else. We collect
810 the proper notion of "enclosing" in convert_from_eh_region_ranges. */
813 expand_eh_region_end_fixup (handler)
816 struct eh_region *fixup;
821 fixup = expand_eh_region_end ();
822 fixup->type = ERT_FIXUP;
823 fixup->u.fixup.cleanup_exp = handler;
826 /* Return an rtl expression for a pointer to the exception object
830 get_exception_pointer (fun)
831 struct function *fun;
833 rtx exc_ptr = fun->eh->exc_ptr;
834 if (fun == cfun && ! exc_ptr)
836 exc_ptr = gen_reg_rtx (ptr_mode);
837 fun->eh->exc_ptr = exc_ptr;
842 /* Return an rtl expression for the exception dispatch filter
846 get_exception_filter (fun)
847 struct function *fun;
849 rtx filter = fun->eh->filter;
850 if (fun == cfun && ! filter)
852 filter = gen_reg_rtx (word_mode);
853 fun->eh->filter = filter;
858 /* This section is for the exception handling specific optimization pass. */
860 /* Random access the exception region tree. It's just as simple to
861 collect the regions this way as in expand_eh_region_start, but
862 without having to realloc memory. */
865 collect_eh_region_array ()
867 struct eh_region **array, *i;
869 i = cfun->eh->region_tree;
873 array = ggc_alloc_cleared ((cfun->eh->last_region_number + 1)
875 cfun->eh->region_array = array;
879 array[i->region_number] = i;
881 /* If there are sub-regions, process them. */
884 /* If there are peers, process them. */
885 else if (i->next_peer)
887 /* Otherwise, step back up the tree to the next peer. */
894 } while (i->next_peer == NULL);
901 resolve_fixup_regions ()
903 int i, j, n = cfun->eh->last_region_number;
905 for (i = 1; i <= n; ++i)
907 struct eh_region *fixup = cfun->eh->region_array[i];
908 struct eh_region *cleanup = 0;
910 if (! fixup || fixup->type != ERT_FIXUP)
913 for (j = 1; j <= n; ++j)
915 cleanup = cfun->eh->region_array[j];
916 if (cleanup && cleanup->type == ERT_CLEANUP
917 && cleanup->u.cleanup.exp == fixup->u.fixup.cleanup_exp)
923 fixup->u.fixup.real_region = cleanup->outer;
927 /* Now that we've discovered what region actually encloses a fixup,
928 we can shuffle pointers and remove them from the tree. */
931 remove_fixup_regions ()
935 struct eh_region *fixup;
937 /* Walk the insn chain and adjust the REG_EH_REGION numbers
938 for instructions referencing fixup regions. This is only
939 strictly necessary for fixup regions with no parent, but
940 doesn't hurt to do it for all regions. */
941 for (insn = get_insns(); insn ; insn = NEXT_INSN (insn))
943 && (note = find_reg_note (insn, REG_EH_REGION, NULL))
944 && INTVAL (XEXP (note, 0)) > 0
945 && (fixup = cfun->eh->region_array[INTVAL (XEXP (note, 0))])
946 && fixup->type == ERT_FIXUP)
948 if (fixup->u.fixup.real_region)
949 XEXP (note, 0) = GEN_INT (fixup->u.fixup.real_region->region_number);
951 remove_note (insn, note);
954 /* Remove the fixup regions from the tree. */
955 for (i = cfun->eh->last_region_number; i > 0; --i)
957 fixup = cfun->eh->region_array[i];
961 /* Allow GC to maybe free some memory. */
962 if (fixup->type == ERT_CLEANUP)
963 fixup->u.cleanup.exp = NULL_TREE;
965 if (fixup->type != ERT_FIXUP)
970 struct eh_region *parent, *p, **pp;
972 parent = fixup->u.fixup.real_region;
974 /* Fix up the children's parent pointers; find the end of
976 for (p = fixup->inner; ; p = p->next_peer)
983 /* In the tree of cleanups, only outer-inner ordering matters.
984 So link the children back in anywhere at the correct level. */
988 pp = &cfun->eh->region_tree;
994 remove_eh_handler (fixup);
998 /* Remove all regions whose labels are not reachable from insns. */
1001 remove_unreachable_regions (insns)
1004 int i, *uid_region_num;
1006 struct eh_region *r;
1009 uid_region_num = xcalloc (get_max_uid (), sizeof(int));
1010 reachable = xcalloc (cfun->eh->last_region_number + 1, sizeof(bool));
1012 for (i = cfun->eh->last_region_number; i > 0; --i)
1014 r = cfun->eh->region_array[i];
1015 if (!r || r->region_number != i)
1020 if (uid_region_num[INSN_UID (r->resume)])
1022 uid_region_num[INSN_UID (r->resume)] = i;
1026 if (uid_region_num[INSN_UID (r->label)])
1028 uid_region_num[INSN_UID (r->label)] = i;
1030 if (r->type == ERT_TRY && r->u.try.continue_label)
1032 if (uid_region_num[INSN_UID (r->u.try.continue_label)])
1034 uid_region_num[INSN_UID (r->u.try.continue_label)] = i;
1038 for (insn = insns; insn; insn = NEXT_INSN (insn))
1039 reachable[uid_region_num[INSN_UID (insn)]] = true;
1041 for (i = cfun->eh->last_region_number; i > 0; --i)
1043 r = cfun->eh->region_array[i];
1044 if (r && r->region_number == i && !reachable[i])
1046 /* Don't remove ERT_THROW regions if their outer region
1048 if (r->type == ERT_THROW
1050 && reachable[r->outer->region_number])
1053 remove_eh_handler (r);
1058 free (uid_region_num);
1061 /* Turn NOTE_INSN_EH_REGION notes into REG_EH_REGION notes for each
1062 can_throw instruction in the region. */
1065 convert_from_eh_region_ranges_1 (pinsns, orig_sp, cur)
1073 for (insn = *pinsns; insn ; insn = next)
1075 next = NEXT_INSN (insn);
1076 if (GET_CODE (insn) == NOTE)
1078 int kind = NOTE_LINE_NUMBER (insn);
1079 if (kind == NOTE_INSN_EH_REGION_BEG
1080 || kind == NOTE_INSN_EH_REGION_END)
1082 if (kind == NOTE_INSN_EH_REGION_BEG)
1084 struct eh_region *r;
1087 cur = NOTE_EH_HANDLER (insn);
1089 r = cfun->eh->region_array[cur];
1090 if (r->type == ERT_FIXUP)
1092 r = r->u.fixup.real_region;
1093 cur = r ? r->region_number : 0;
1095 else if (r->type == ERT_CATCH)
1098 cur = r ? r->region_number : 0;
1104 /* Removing the first insn of a CALL_PLACEHOLDER sequence
1105 requires extra care to adjust sequence start. */
1106 if (insn == *pinsns)
1112 else if (INSN_P (insn))
1118 /* An existing region note may be present to suppress
1119 exception handling. Anything with a note value of -1
1120 cannot throw an exception of any kind. A note value
1121 of 0 means that "normal" exceptions are suppressed,
1122 but not necessarily "forced unwind" exceptions. */
1123 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1126 if (flag_forced_unwind_exceptions
1127 && INTVAL (XEXP (note, 0)) >= 0)
1128 XEXP (note, 0) = GEN_INT (cur);
1132 /* Calls can always potentially throw exceptions; if we wanted
1133 exceptions for non-call insns, then any may_trap_p
1134 instruction can throw. */
1135 if (GET_CODE (insn) != CALL_INSN
1136 && (!flag_non_call_exceptions
1137 || GET_CODE (PATTERN (insn)) == CLOBBER
1138 || GET_CODE (PATTERN (insn)) == USE
1139 || !may_trap_p (PATTERN (insn))))
1142 REG_NOTES (insn) = alloc_EXPR_LIST (REG_EH_REGION,
1150 if (GET_CODE (insn) == CALL_INSN
1151 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
1153 convert_from_eh_region_ranges_1 (&XEXP (PATTERN (insn), 0),
1155 convert_from_eh_region_ranges_1 (&XEXP (PATTERN (insn), 1),
1157 convert_from_eh_region_ranges_1 (&XEXP (PATTERN (insn), 2),
1168 convert_from_eh_region_ranges ()
1173 collect_eh_region_array ();
1174 resolve_fixup_regions ();
1176 stack = xmalloc (sizeof (int) * (cfun->eh->last_region_number + 1));
1177 insns = get_insns ();
1178 convert_from_eh_region_ranges_1 (&insns, stack, 0);
1181 remove_fixup_regions ();
1182 remove_unreachable_regions (insns);
1186 add_ehl_entry (label, region)
1188 struct eh_region *region;
1190 struct ehl_map_entry **slot, *entry;
1192 LABEL_PRESERVE_P (label) = 1;
1194 entry = (struct ehl_map_entry *) ggc_alloc (sizeof (*entry));
1195 entry->label = label;
1196 entry->region = region;
1198 slot = (struct ehl_map_entry **)
1199 htab_find_slot (cfun->eh->exception_handler_label_map, entry, INSERT);
1201 /* Before landing pad creation, each exception handler has its own
1202 label. After landing pad creation, the exception handlers may
1203 share landing pads. This is ok, since maybe_remove_eh_handler
1204 only requires the 1-1 mapping before landing pad creation. */
1205 if (*slot && !cfun->eh->built_landing_pads)
1212 find_exception_handler_labels ()
1216 if (cfun->eh->exception_handler_label_map)
1217 htab_empty (cfun->eh->exception_handler_label_map);
1220 /* ??? The expansion factor here (3/2) must be greater than the htab
1221 occupancy factor (4/3) to avoid unnecessary resizing. */
1222 cfun->eh->exception_handler_label_map
1223 = htab_create_ggc (cfun->eh->last_region_number * 3 / 2,
1224 ehl_hash, ehl_eq, NULL);
1227 if (cfun->eh->region_tree == NULL)
1230 for (i = cfun->eh->last_region_number; i > 0; --i)
1232 struct eh_region *region = cfun->eh->region_array[i];
1235 if (! region || region->region_number != i)
1237 if (cfun->eh->built_landing_pads)
1238 lab = region->landing_pad;
1240 lab = region->label;
1243 add_ehl_entry (lab, region);
1246 /* For sjlj exceptions, need the return label to remain live until
1247 after landing pad generation. */
1248 if (USING_SJLJ_EXCEPTIONS && ! cfun->eh->built_landing_pads)
1249 add_ehl_entry (return_label, NULL);
1253 current_function_has_exception_handlers ()
1257 for (i = cfun->eh->last_region_number; i > 0; --i)
1259 struct eh_region *region = cfun->eh->region_array[i];
1261 if (! region || region->region_number != i)
1263 if (region->type != ERT_THROW)
1270 static struct eh_region *
1271 duplicate_eh_region_1 (o, map)
1272 struct eh_region *o;
1273 struct inline_remap *map;
1276 = (struct eh_region *) ggc_alloc_cleared (sizeof (struct eh_region));
1278 n->region_number = o->region_number + cfun->eh->last_region_number;
1284 case ERT_MUST_NOT_THROW:
1288 if (o->u.try.continue_label)
1289 n->u.try.continue_label
1290 = get_label_from_map (map,
1291 CODE_LABEL_NUMBER (o->u.try.continue_label));
1295 n->u.catch.type_list = o->u.catch.type_list;
1298 case ERT_ALLOWED_EXCEPTIONS:
1299 n->u.allowed.type_list = o->u.allowed.type_list;
1303 n->u.throw.type = o->u.throw.type;
1310 n->label = get_label_from_map (map, CODE_LABEL_NUMBER (o->label));
1313 n->resume = map->insn_map[INSN_UID (o->resume)];
1314 if (n->resume == NULL)
1322 duplicate_eh_region_2 (o, n_array)
1323 struct eh_region *o;
1324 struct eh_region **n_array;
1326 struct eh_region *n = n_array[o->region_number];
1331 n->u.try.catch = n_array[o->u.try.catch->region_number];
1332 n->u.try.last_catch = n_array[o->u.try.last_catch->region_number];
1336 if (o->u.catch.next_catch)
1337 n->u.catch.next_catch = n_array[o->u.catch.next_catch->region_number];
1338 if (o->u.catch.prev_catch)
1339 n->u.catch.prev_catch = n_array[o->u.catch.prev_catch->region_number];
1347 n->outer = n_array[o->outer->region_number];
1349 n->inner = n_array[o->inner->region_number];
1351 n->next_peer = n_array[o->next_peer->region_number];
1355 duplicate_eh_regions (ifun, map)
1356 struct function *ifun;
1357 struct inline_remap *map;
1359 int ifun_last_region_number = ifun->eh->last_region_number;
1360 struct eh_region **n_array, *root, *cur;
1363 if (ifun_last_region_number == 0)
1366 n_array = xcalloc (ifun_last_region_number + 1, sizeof (*n_array));
1368 for (i = 1; i <= ifun_last_region_number; ++i)
1370 cur = ifun->eh->region_array[i];
1371 if (!cur || cur->region_number != i)
1373 n_array[i] = duplicate_eh_region_1 (cur, map);
1375 for (i = 1; i <= ifun_last_region_number; ++i)
1377 cur = ifun->eh->region_array[i];
1378 if (!cur || cur->region_number != i)
1380 duplicate_eh_region_2 (cur, n_array);
1383 root = n_array[ifun->eh->region_tree->region_number];
1384 cur = cfun->eh->cur_region;
1387 struct eh_region *p = cur->inner;
1390 while (p->next_peer)
1392 p->next_peer = root;
1397 for (i = 1; i <= ifun_last_region_number; ++i)
1398 if (n_array[i] && n_array[i]->outer == NULL)
1399 n_array[i]->outer = cur;
1403 struct eh_region *p = cfun->eh->region_tree;
1406 while (p->next_peer)
1408 p->next_peer = root;
1411 cfun->eh->region_tree = root;
1416 i = cfun->eh->last_region_number;
1417 cfun->eh->last_region_number = i + ifun_last_region_number;
1423 t2r_eq (pentry, pdata)
1427 tree entry = (tree) pentry;
1428 tree data = (tree) pdata;
1430 return TREE_PURPOSE (entry) == data;
1437 tree entry = (tree) pentry;
1438 return TYPE_HASH (TREE_PURPOSE (entry));
1442 add_type_for_runtime (type)
1447 slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
1448 TYPE_HASH (type), INSERT);
1451 tree runtime = (*lang_eh_runtime_type) (type);
1452 *slot = tree_cons (type, runtime, NULL_TREE);
1457 lookup_type_for_runtime (type)
1462 slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
1463 TYPE_HASH (type), NO_INSERT);
1465 /* We should have always inserted the data earlier. */
1466 return TREE_VALUE (*slot);
1470 /* Represent an entry in @TTypes for either catch actions
1471 or exception filter actions. */
1472 struct ttypes_filter GTY(())
1478 /* Compare ENTRY (a ttypes_filter entry in the hash table) with DATA
1479 (a tree) for a @TTypes type node we are thinking about adding. */
1482 ttypes_filter_eq (pentry, pdata)
1486 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1487 tree data = (tree) pdata;
1489 return entry->t == data;
1493 ttypes_filter_hash (pentry)
1496 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1497 return TYPE_HASH (entry->t);
1500 /* Compare ENTRY with DATA (both struct ttypes_filter) for a @TTypes
1501 exception specification list we are thinking about adding. */
1502 /* ??? Currently we use the type lists in the order given. Someone
1503 should put these in some canonical order. */
1506 ehspec_filter_eq (pentry, pdata)
1510 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1511 const struct ttypes_filter *data = (const struct ttypes_filter *) pdata;
1513 return type_list_equal (entry->t, data->t);
1516 /* Hash function for exception specification lists. */
1519 ehspec_filter_hash (pentry)
1522 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1526 for (list = entry->t; list ; list = TREE_CHAIN (list))
1527 h = (h << 5) + (h >> 27) + TYPE_HASH (TREE_VALUE (list));
1531 /* Add TYPE to cfun->eh->ttype_data, using TYPES_HASH to speed
1532 up the search. Return the filter value to be used. */
1535 add_ttypes_entry (ttypes_hash, type)
1539 struct ttypes_filter **slot, *n;
1541 slot = (struct ttypes_filter **)
1542 htab_find_slot_with_hash (ttypes_hash, type, TYPE_HASH (type), INSERT);
1544 if ((n = *slot) == NULL)
1546 /* Filter value is a 1 based table index. */
1548 n = (struct ttypes_filter *) xmalloc (sizeof (*n));
1550 n->filter = VARRAY_ACTIVE_SIZE (cfun->eh->ttype_data) + 1;
1553 VARRAY_PUSH_TREE (cfun->eh->ttype_data, type);
1559 /* Add LIST to cfun->eh->ehspec_data, using EHSPEC_HASH and TYPES_HASH
1560 to speed up the search. Return the filter value to be used. */
1563 add_ehspec_entry (ehspec_hash, ttypes_hash, list)
1568 struct ttypes_filter **slot, *n;
1569 struct ttypes_filter dummy;
1572 slot = (struct ttypes_filter **)
1573 htab_find_slot (ehspec_hash, &dummy, INSERT);
1575 if ((n = *slot) == NULL)
1577 /* Filter value is a -1 based byte index into a uleb128 buffer. */
1579 n = (struct ttypes_filter *) xmalloc (sizeof (*n));
1581 n->filter = -(VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data) + 1);
1584 /* Look up each type in the list and encode its filter
1585 value as a uleb128. Terminate the list with 0. */
1586 for (; list ; list = TREE_CHAIN (list))
1587 push_uleb128 (&cfun->eh->ehspec_data,
1588 add_ttypes_entry (ttypes_hash, TREE_VALUE (list)));
1589 VARRAY_PUSH_UCHAR (cfun->eh->ehspec_data, 0);
1595 /* Generate the action filter values to be used for CATCH and
1596 ALLOWED_EXCEPTIONS regions. When using dwarf2 exception regions,
1597 we use lots of landing pads, and so every type or list can share
1598 the same filter value, which saves table space. */
1601 assign_filter_values ()
1604 htab_t ttypes, ehspec;
1606 VARRAY_TREE_INIT (cfun->eh->ttype_data, 16, "ttype_data");
1607 VARRAY_UCHAR_INIT (cfun->eh->ehspec_data, 64, "ehspec_data");
1609 ttypes = htab_create (31, ttypes_filter_hash, ttypes_filter_eq, free);
1610 ehspec = htab_create (31, ehspec_filter_hash, ehspec_filter_eq, free);
1612 for (i = cfun->eh->last_region_number; i > 0; --i)
1614 struct eh_region *r = cfun->eh->region_array[i];
1616 /* Mind we don't process a region more than once. */
1617 if (!r || r->region_number != i)
1623 /* Whatever type_list is (NULL or true list), we build a list
1624 of filters for the region. */
1625 r->u.catch.filter_list = NULL_TREE;
1627 if (r->u.catch.type_list != NULL)
1629 /* Get a filter value for each of the types caught and store
1630 them in the region's dedicated list. */
1631 tree tp_node = r->u.catch.type_list;
1633 for (;tp_node; tp_node = TREE_CHAIN (tp_node))
1635 int flt = add_ttypes_entry (ttypes, TREE_VALUE (tp_node));
1636 tree flt_node = build_int_2 (flt, 0);
1638 r->u.catch.filter_list
1639 = tree_cons (NULL_TREE, flt_node, r->u.catch.filter_list);
1644 /* Get a filter value for the NULL list also since it will need
1645 an action record anyway. */
1646 int flt = add_ttypes_entry (ttypes, NULL);
1647 tree flt_node = build_int_2 (flt, 0);
1649 r->u.catch.filter_list
1650 = tree_cons (NULL_TREE, flt_node, r->u.catch.filter_list);
1655 case ERT_ALLOWED_EXCEPTIONS:
1657 = add_ehspec_entry (ehspec, ttypes, r->u.allowed.type_list);
1665 htab_delete (ttypes);
1666 htab_delete (ehspec);
1670 build_post_landing_pads ()
1674 for (i = cfun->eh->last_region_number; i > 0; --i)
1676 struct eh_region *region = cfun->eh->region_array[i];
1679 /* Mind we don't process a region more than once. */
1680 if (!region || region->region_number != i)
1683 switch (region->type)
1686 /* ??? Collect the set of all non-overlapping catch handlers
1687 all the way up the chain until blocked by a cleanup. */
1688 /* ??? Outer try regions can share landing pads with inner
1689 try regions if the types are completely non-overlapping,
1690 and there are no intervening cleanups. */
1692 region->post_landing_pad = gen_label_rtx ();
1696 emit_label (region->post_landing_pad);
1698 /* ??? It is mighty inconvenient to call back into the
1699 switch statement generation code in expand_end_case.
1700 Rapid prototyping sez a sequence of ifs. */
1702 struct eh_region *c;
1703 for (c = region->u.try.catch; c ; c = c->u.catch.next_catch)
1705 if (c->u.catch.type_list == NULL)
1707 if (flag_forced_unwind_exceptions)
1708 emit_cmp_and_jump_insns
1709 (cfun->eh->filter, const0_rtx, GT, NULL_RTX,
1710 word_mode, 0, c->label);
1712 emit_jump (c->label);
1716 /* Need for one cmp/jump per type caught. Each type
1717 list entry has a matching entry in the filter list
1718 (see assign_filter_values). */
1719 tree tp_node = c->u.catch.type_list;
1720 tree flt_node = c->u.catch.filter_list;
1724 emit_cmp_and_jump_insns
1726 GEN_INT (tree_low_cst (TREE_VALUE (flt_node), 0)),
1727 EQ, NULL_RTX, word_mode, 0, c->label);
1729 tp_node = TREE_CHAIN (tp_node);
1730 flt_node = TREE_CHAIN (flt_node);
1736 /* We delay the generation of the _Unwind_Resume until we generate
1737 landing pads. We emit a marker here so as to get good control
1738 flow data in the meantime. */
1740 = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
1746 emit_insn_before (seq, region->u.try.catch->label);
1749 case ERT_ALLOWED_EXCEPTIONS:
1750 region->post_landing_pad = gen_label_rtx ();
1754 emit_label (region->post_landing_pad);
1756 emit_cmp_and_jump_insns (cfun->eh->filter,
1757 GEN_INT (region->u.allowed.filter),
1758 EQ, NULL_RTX, word_mode, 0, region->label);
1760 /* We delay the generation of the _Unwind_Resume until we generate
1761 landing pads. We emit a marker here so as to get good control
1762 flow data in the meantime. */
1764 = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
1770 emit_insn_before (seq, region->label);
1774 region->post_landing_pad = region->label;
1777 case ERT_MUST_NOT_THROW:
1778 /* See maybe_remove_eh_handler about removing region->label. */
1779 if (flag_forced_unwind_exceptions && region->label)
1781 region->post_landing_pad = gen_label_rtx ();
1785 emit_label (region->post_landing_pad);
1786 emit_cmp_and_jump_insns (cfun->eh->filter, const0_rtx, GT,
1787 NULL_RTX, word_mode, 0, region->label);
1790 = emit_jump_insn (gen_rtx_RESX (VOIDmode,
1791 region->region_number));
1797 emit_insn_before (seq, region->label);
1800 region->post_landing_pad = region->label;
1805 /* Nothing to do. */
1814 /* Replace RESX patterns with jumps to the next handler if any, or calls to
1815 _Unwind_Resume otherwise. */
1818 connect_post_landing_pads ()
1822 for (i = cfun->eh->last_region_number; i > 0; --i)
1824 struct eh_region *region = cfun->eh->region_array[i];
1825 struct eh_region *outer;
1828 /* Mind we don't process a region more than once. */
1829 if (!region || region->region_number != i)
1832 /* If there is no RESX, or it has been deleted by flow, there's
1833 nothing to fix up. */
1834 if (! region->resume || INSN_DELETED_P (region->resume))
1837 /* Search for another landing pad in this function. */
1838 for (outer = region->outer; outer ; outer = outer->outer)
1839 if (outer->post_landing_pad)
1845 emit_jump (outer->post_landing_pad);
1847 emit_library_call (unwind_resume_libfunc, LCT_THROW,
1848 VOIDmode, 1, cfun->eh->exc_ptr, ptr_mode);
1852 emit_insn_before (seq, region->resume);
1853 delete_insn (region->resume);
1859 dw2_build_landing_pads ()
1864 for (i = cfun->eh->last_region_number; i > 0; --i)
1866 struct eh_region *region = cfun->eh->region_array[i];
1868 bool clobbers_hard_regs = false;
1870 /* Mind we don't process a region more than once. */
1871 if (!region || region->region_number != i)
1874 if (region->type != ERT_CLEANUP
1875 && region->type != ERT_TRY
1876 && region->type != ERT_ALLOWED_EXCEPTIONS)
1881 region->landing_pad = gen_label_rtx ();
1882 emit_label (region->landing_pad);
1884 #ifdef HAVE_exception_receiver
1885 if (HAVE_exception_receiver)
1886 emit_insn (gen_exception_receiver ());
1889 #ifdef HAVE_nonlocal_goto_receiver
1890 if (HAVE_nonlocal_goto_receiver)
1891 emit_insn (gen_nonlocal_goto_receiver ());
1896 /* If the eh_return data registers are call-saved, then we
1897 won't have considered them clobbered from the call that
1898 threw. Kill them now. */
1901 unsigned r = EH_RETURN_DATA_REGNO (j);
1902 if (r == INVALID_REGNUM)
1904 if (! call_used_regs[r])
1906 emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, r)));
1907 clobbers_hard_regs = true;
1911 if (clobbers_hard_regs)
1913 /* @@@ This is a kludge. Not all machine descriptions define a
1914 blockage insn, but we must not allow the code we just generated
1915 to be reordered by scheduling. So emit an ASM_INPUT to act as
1917 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
1920 emit_move_insn (cfun->eh->exc_ptr,
1921 gen_rtx_REG (ptr_mode, EH_RETURN_DATA_REGNO (0)));
1922 emit_move_insn (cfun->eh->filter,
1923 gen_rtx_REG (word_mode, EH_RETURN_DATA_REGNO (1)));
1928 emit_insn_before (seq, region->post_landing_pad);
1935 int directly_reachable;
1938 int call_site_index;
1942 sjlj_find_directly_reachable_regions (lp_info)
1943 struct sjlj_lp_info *lp_info;
1946 bool found_one = false;
1948 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
1950 struct eh_region *region;
1951 enum reachable_code rc;
1955 if (! INSN_P (insn))
1958 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1959 if (!note || INTVAL (XEXP (note, 0)) <= 0)
1962 region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
1964 type_thrown = NULL_TREE;
1965 if (region->type == ERT_THROW)
1967 type_thrown = region->u.throw.type;
1968 region = region->outer;
1971 /* Find the first containing region that might handle the exception.
1972 That's the landing pad to which we will transfer control. */
1973 rc = RNL_NOT_CAUGHT;
1974 for (; region; region = region->outer)
1976 rc = reachable_next_level (region, type_thrown, 0);
1977 if (rc != RNL_NOT_CAUGHT)
1981 /* Forced unwind exceptions aren't blocked. */
1982 if (flag_forced_unwind_exceptions && rc == RNL_BLOCKED)
1984 struct eh_region *r;
1985 for (r = region->outer; r ; r = r->outer)
1986 if (r->type == ERT_CLEANUP)
1988 rc = RNL_MAYBE_CAUGHT;
1989 if (! region->label)
1995 if (rc == RNL_MAYBE_CAUGHT || rc == RNL_CAUGHT)
1997 lp_info[region->region_number].directly_reachable = 1;
2006 sjlj_assign_call_site_values (dispatch_label, lp_info)
2008 struct sjlj_lp_info *lp_info;
2013 /* First task: build the action table. */
2015 VARRAY_UCHAR_INIT (cfun->eh->action_record_data, 64, "action_record_data");
2016 ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
2018 for (i = cfun->eh->last_region_number; i > 0; --i)
2019 if (lp_info[i].directly_reachable)
2021 struct eh_region *r = cfun->eh->region_array[i];
2022 r->landing_pad = dispatch_label;
2023 lp_info[i].action_index = collect_one_action_chain (ar_hash, r);
2024 if (lp_info[i].action_index != -1)
2025 cfun->uses_eh_lsda = 1;
2028 htab_delete (ar_hash);
2030 /* Next: assign dispatch values. In dwarf2 terms, this would be the
2031 landing pad label for the region. For sjlj though, there is one
2032 common landing pad from which we dispatch to the post-landing pads.
2034 A region receives a dispatch index if it is directly reachable
2035 and requires in-function processing. Regions that share post-landing
2036 pads may share dispatch indices. */
2037 /* ??? Post-landing pad sharing doesn't actually happen at the moment
2038 (see build_post_landing_pads) so we don't bother checking for it. */
2041 for (i = cfun->eh->last_region_number; i > 0; --i)
2042 if (lp_info[i].directly_reachable)
2043 lp_info[i].dispatch_index = index++;
2045 /* Finally: assign call-site values. If dwarf2 terms, this would be
2046 the region number assigned by convert_to_eh_region_ranges, but
2047 handles no-action and must-not-throw differently. */
2050 for (i = cfun->eh->last_region_number; i > 0; --i)
2051 if (lp_info[i].directly_reachable)
2053 int action = lp_info[i].action_index;
2055 /* Map must-not-throw to otherwise unused call-site index 0. */
2058 /* Map no-action to otherwise unused call-site index -1. */
2059 else if (action == -1)
2061 /* Otherwise, look it up in the table. */
2063 index = add_call_site (GEN_INT (lp_info[i].dispatch_index), action);
2065 lp_info[i].call_site_index = index;
2070 sjlj_mark_call_sites (lp_info)
2071 struct sjlj_lp_info *lp_info;
2073 int last_call_site = -2;
2076 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
2078 struct eh_region *region;
2080 rtx note, before, p;
2082 /* Reset value tracking at extended basic block boundaries. */
2083 if (GET_CODE (insn) == CODE_LABEL)
2084 last_call_site = -2;
2086 if (! INSN_P (insn))
2089 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2092 /* Calls (and trapping insns) without notes are outside any
2093 exception handling region in this function. Mark them as
2095 if (GET_CODE (insn) == CALL_INSN
2096 || (flag_non_call_exceptions
2097 && may_trap_p (PATTERN (insn))))
2098 this_call_site = -1;
2104 /* Calls that are known to not throw need not be marked. */
2105 if (INTVAL (XEXP (note, 0)) <= 0)
2108 region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
2109 this_call_site = lp_info[region->region_number].call_site_index;
2112 if (this_call_site == last_call_site)
2115 /* Don't separate a call from it's argument loads. */
2117 if (GET_CODE (insn) == CALL_INSN)
2118 before = find_first_parameter_load (insn, NULL_RTX);
2121 mem = adjust_address (cfun->eh->sjlj_fc, TYPE_MODE (integer_type_node),
2122 sjlj_fc_call_site_ofs);
2123 emit_move_insn (mem, GEN_INT (this_call_site));
2127 emit_insn_before (p, before);
2128 last_call_site = this_call_site;
2132 /* Construct the SjLj_Function_Context. */
2135 sjlj_emit_function_enter (dispatch_label)
2138 rtx fn_begin, fc, mem, seq;
2140 fc = cfun->eh->sjlj_fc;
2144 /* We're storing this libcall's address into memory instead of
2145 calling it directly. Thus, we must call assemble_external_libcall
2146 here, as we can not depend on emit_library_call to do it for us. */
2147 assemble_external_libcall (eh_personality_libfunc);
2148 mem = adjust_address (fc, Pmode, sjlj_fc_personality_ofs);
2149 emit_move_insn (mem, eh_personality_libfunc);
2151 mem = adjust_address (fc, Pmode, sjlj_fc_lsda_ofs);
2152 if (cfun->uses_eh_lsda)
2155 ASM_GENERATE_INTERNAL_LABEL (buf, "LLSDA", current_function_funcdef_no);
2156 emit_move_insn (mem, gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf)));
2159 emit_move_insn (mem, const0_rtx);
2161 #ifdef DONT_USE_BUILTIN_SETJMP
2164 x = emit_library_call_value (setjmp_libfunc, NULL_RTX, LCT_RETURNS_TWICE,
2165 TYPE_MODE (integer_type_node), 1,
2166 plus_constant (XEXP (fc, 0),
2167 sjlj_fc_jbuf_ofs), Pmode);
2169 note = emit_note (NULL, NOTE_INSN_EXPECTED_VALUE);
2170 NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, x, const0_rtx);
2172 emit_cmp_and_jump_insns (x, const0_rtx, NE, 0,
2173 TYPE_MODE (integer_type_node), 0, dispatch_label);
2176 expand_builtin_setjmp_setup (plus_constant (XEXP (fc, 0), sjlj_fc_jbuf_ofs),
2180 emit_library_call (unwind_sjlj_register_libfunc, LCT_NORMAL, VOIDmode,
2181 1, XEXP (fc, 0), Pmode);
2186 /* ??? Instead of doing this at the beginning of the function,
2187 do this in a block that is at loop level 0 and dominates all
2188 can_throw_internal instructions. */
2190 for (fn_begin = get_insns (); ; fn_begin = NEXT_INSN (fn_begin))
2191 if (GET_CODE (fn_begin) == NOTE
2192 && NOTE_LINE_NUMBER (fn_begin) == NOTE_INSN_FUNCTION_BEG)
2194 emit_insn_after (seq, fn_begin);
2197 /* Call back from expand_function_end to know where we should put
2198 the call to unwind_sjlj_unregister_libfunc if needed. */
2201 sjlj_emit_function_exit_after (after)
2204 cfun->eh->sjlj_exit_after = after;
2208 sjlj_emit_function_exit ()
2214 emit_library_call (unwind_sjlj_unregister_libfunc, LCT_NORMAL, VOIDmode,
2215 1, XEXP (cfun->eh->sjlj_fc, 0), Pmode);
2220 /* ??? Really this can be done in any block at loop level 0 that
2221 post-dominates all can_throw_internal instructions. This is
2222 the last possible moment. */
2224 emit_insn_after (seq, cfun->eh->sjlj_exit_after);
2228 sjlj_emit_dispatch_table (dispatch_label, lp_info)
2230 struct sjlj_lp_info *lp_info;
2232 int i, first_reachable;
2233 rtx mem, dispatch, seq, fc;
2235 fc = cfun->eh->sjlj_fc;
2239 emit_label (dispatch_label);
2241 #ifndef DONT_USE_BUILTIN_SETJMP
2242 expand_builtin_setjmp_receiver (dispatch_label);
2245 /* Load up dispatch index, exc_ptr and filter values from the
2246 function context. */
2247 mem = adjust_address (fc, TYPE_MODE (integer_type_node),
2248 sjlj_fc_call_site_ofs);
2249 dispatch = copy_to_reg (mem);
2251 mem = adjust_address (fc, word_mode, sjlj_fc_data_ofs);
2252 if (word_mode != Pmode)
2254 #ifdef POINTERS_EXTEND_UNSIGNED
2255 mem = convert_memory_address (Pmode, mem);
2257 mem = convert_to_mode (Pmode, mem, 0);
2260 emit_move_insn (cfun->eh->exc_ptr, mem);
2262 mem = adjust_address (fc, word_mode, sjlj_fc_data_ofs + UNITS_PER_WORD);
2263 emit_move_insn (cfun->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 cfun->eh->region_array[i]->post_landing_pad);
2288 emit_insn_before (seq, (cfun->eh->region_array[first_reachable]
2289 ->post_landing_pad));
2293 sjlj_build_landing_pads ()
2295 struct sjlj_lp_info *lp_info;
2297 lp_info = (struct sjlj_lp_info *) xcalloc (cfun->eh->last_region_number + 1,
2298 sizeof (struct sjlj_lp_info));
2300 if (sjlj_find_directly_reachable_regions (lp_info))
2302 rtx dispatch_label = gen_label_rtx ();
2305 = assign_stack_local (TYPE_MODE (sjlj_fc_type_node),
2306 int_size_in_bytes (sjlj_fc_type_node),
2307 TYPE_ALIGN (sjlj_fc_type_node));
2309 sjlj_assign_call_site_values (dispatch_label, lp_info);
2310 sjlj_mark_call_sites (lp_info);
2312 sjlj_emit_function_enter (dispatch_label);
2313 sjlj_emit_dispatch_table (dispatch_label, lp_info);
2314 sjlj_emit_function_exit ();
2321 finish_eh_generation ()
2323 /* Nothing to do if no regions created. */
2324 if (cfun->eh->region_tree == NULL)
2327 /* The object here is to provide find_basic_blocks with detailed
2328 information (via reachable_handlers) on how exception control
2329 flows within the function. In this first pass, we can include
2330 type information garnered from ERT_THROW and ERT_ALLOWED_EXCEPTIONS
2331 regions, and hope that it will be useful in deleting unreachable
2332 handlers. Subsequently, we will generate landing pads which will
2333 connect many of the handlers, and then type information will not
2334 be effective. Still, this is a win over previous implementations. */
2336 cleanup_cfg (CLEANUP_PRE_LOOP | CLEANUP_NO_INSN_DEL);
2338 /* These registers are used by the landing pads. Make sure they
2339 have been generated. */
2340 get_exception_pointer (cfun);
2341 get_exception_filter (cfun);
2343 /* Construct the landing pads. */
2345 assign_filter_values ();
2346 build_post_landing_pads ();
2347 connect_post_landing_pads ();
2348 if (USING_SJLJ_EXCEPTIONS)
2349 sjlj_build_landing_pads ();
2351 dw2_build_landing_pads ();
2353 cfun->eh->built_landing_pads = 1;
2355 /* We've totally changed the CFG. Start over. */
2356 find_exception_handler_labels ();
2357 rebuild_jump_labels (get_insns ());
2358 find_basic_blocks (get_insns (), max_reg_num (), 0);
2359 cleanup_cfg (CLEANUP_PRE_LOOP | CLEANUP_NO_INSN_DEL);
2366 struct ehl_map_entry *entry = (struct ehl_map_entry *) pentry;
2368 /* 2^32 * ((sqrt(5) - 1) / 2) */
2369 const hashval_t scaled_golden_ratio = 0x9e3779b9;
2370 return CODE_LABEL_NUMBER (entry->label) * scaled_golden_ratio;
2374 ehl_eq (pentry, pdata)
2378 struct ehl_map_entry *entry = (struct ehl_map_entry *) pentry;
2379 struct ehl_map_entry *data = (struct ehl_map_entry *) pdata;
2381 return entry->label == data->label;
2384 /* This section handles removing dead code for flow. */
2386 /* Remove LABEL from exception_handler_label_map. */
2389 remove_exception_handler_label (label)
2392 struct ehl_map_entry **slot, tmp;
2394 /* If exception_handler_label_map was not built yet,
2395 there is nothing to do. */
2396 if (cfun->eh->exception_handler_label_map == NULL)
2400 slot = (struct ehl_map_entry **)
2401 htab_find_slot (cfun->eh->exception_handler_label_map, &tmp, NO_INSERT);
2405 htab_clear_slot (cfun->eh->exception_handler_label_map, (void **) slot);
2408 /* Splice REGION from the region tree etc. */
2411 remove_eh_handler (region)
2412 struct eh_region *region;
2414 struct eh_region **pp, **pp_start, *p, *outer, *inner;
2417 /* For the benefit of efficiently handling REG_EH_REGION notes,
2418 replace this region in the region array with its containing
2419 region. Note that previous region deletions may result in
2420 multiple copies of this region in the array, so we have a
2421 list of alternate numbers by which we are known. */
2423 outer = region->outer;
2424 cfun->eh->region_array[region->region_number] = outer;
2428 EXECUTE_IF_SET_IN_BITMAP (region->aka, 0, i,
2429 { cfun->eh->region_array[i] = outer; });
2435 outer->aka = BITMAP_GGC_ALLOC ();
2437 bitmap_a_or_b (outer->aka, outer->aka, region->aka);
2438 bitmap_set_bit (outer->aka, region->region_number);
2441 if (cfun->eh->built_landing_pads)
2442 lab = region->landing_pad;
2444 lab = region->label;
2446 remove_exception_handler_label (lab);
2449 pp_start = &outer->inner;
2451 pp_start = &cfun->eh->region_tree;
2452 for (pp = pp_start, p = *pp; p != region; pp = &p->next_peer, p = *pp)
2454 *pp = region->next_peer;
2456 inner = region->inner;
2459 for (p = inner; p->next_peer ; p = p->next_peer)
2463 p->next_peer = *pp_start;
2467 if (region->type == ERT_CATCH)
2469 struct eh_region *try, *next, *prev;
2471 for (try = region->next_peer;
2472 try->type == ERT_CATCH;
2473 try = try->next_peer)
2475 if (try->type != ERT_TRY)
2478 next = region->u.catch.next_catch;
2479 prev = region->u.catch.prev_catch;
2482 next->u.catch.prev_catch = prev;
2484 try->u.try.last_catch = prev;
2486 prev->u.catch.next_catch = next;
2489 try->u.try.catch = next;
2491 remove_eh_handler (try);
2496 /* LABEL heads a basic block that is about to be deleted. If this
2497 label corresponds to an exception region, we may be able to
2498 delete the region. */
2501 maybe_remove_eh_handler (label)
2504 struct ehl_map_entry **slot, tmp;
2505 struct eh_region *region;
2507 /* ??? After generating landing pads, it's not so simple to determine
2508 if the region data is completely unused. One must examine the
2509 landing pad and the post landing pad, and whether an inner try block
2510 is referencing the catch handlers directly. */
2511 if (cfun->eh->built_landing_pads)
2515 slot = (struct ehl_map_entry **)
2516 htab_find_slot (cfun->eh->exception_handler_label_map, &tmp, NO_INSERT);
2519 region = (*slot)->region;
2523 /* Flow will want to remove MUST_NOT_THROW regions as unreachable
2524 because there is no path to the fallback call to terminate.
2525 But the region continues to affect call-site data until there
2526 are no more contained calls, which we don't see here. */
2527 if (region->type == ERT_MUST_NOT_THROW)
2529 htab_clear_slot (cfun->eh->exception_handler_label_map, (void **) slot);
2530 region->label = NULL_RTX;
2533 remove_eh_handler (region);
2536 /* Invokes CALLBACK for every exception handler label. Only used by old
2537 loop hackery; should not be used by new code. */
2540 for_each_eh_label (callback)
2541 void (*callback) PARAMS ((rtx));
2543 htab_traverse (cfun->eh->exception_handler_label_map, for_each_eh_label_1,
2548 for_each_eh_label_1 (pentry, data)
2552 struct ehl_map_entry *entry = *(struct ehl_map_entry **)pentry;
2553 void (*callback) PARAMS ((rtx)) = (void (*) PARAMS ((rtx))) data;
2555 (*callback) (entry->label);
2559 /* This section describes CFG exception edges for flow. */
2561 /* For communicating between calls to reachable_next_level. */
2562 struct reachable_info GTY(())
2569 /* A subroutine of reachable_next_level. Return true if TYPE, or a
2570 base class of TYPE, is in HANDLED. */
2573 check_handled (handled, type)
2578 /* We can check for exact matches without front-end help. */
2579 if (! lang_eh_type_covers)
2581 for (t = handled; t ; t = TREE_CHAIN (t))
2582 if (TREE_VALUE (t) == type)
2587 for (t = handled; t ; t = TREE_CHAIN (t))
2588 if ((*lang_eh_type_covers) (TREE_VALUE (t), type))
2595 /* A subroutine of reachable_next_level. If we are collecting a list
2596 of handlers, add one. After landing pad generation, reference
2597 it instead of the handlers themselves. Further, the handlers are
2598 all wired together, so by referencing one, we've got them all.
2599 Before landing pad generation we reference each handler individually.
2601 LP_REGION contains the landing pad; REGION is the handler. */
2604 add_reachable_handler (info, lp_region, region)
2605 struct reachable_info *info;
2606 struct eh_region *lp_region;
2607 struct eh_region *region;
2612 if (cfun->eh->built_landing_pads)
2614 if (! info->handlers)
2615 info->handlers = alloc_INSN_LIST (lp_region->landing_pad, NULL_RTX);
2618 info->handlers = alloc_INSN_LIST (region->label, info->handlers);
2621 /* Process one level of exception regions for reachability.
2622 If TYPE_THROWN is non-null, then it is the *exact* type being
2623 propagated. If INFO is non-null, then collect handler labels
2624 and caught/allowed type information between invocations. */
2626 static enum reachable_code
2627 reachable_next_level (region, type_thrown, info)
2628 struct eh_region *region;
2630 struct reachable_info *info;
2632 switch (region->type)
2635 /* Before landing-pad generation, we model control flow
2636 directly to the individual handlers. In this way we can
2637 see that catch handler types may shadow one another. */
2638 add_reachable_handler (info, region, region);
2639 return RNL_MAYBE_CAUGHT;
2643 struct eh_region *c;
2644 enum reachable_code ret = RNL_NOT_CAUGHT;
2646 for (c = region->u.try.catch; c ; c = c->u.catch.next_catch)
2648 /* A catch-all handler ends the search. */
2649 if (c->u.catch.type_list == NULL)
2651 add_reachable_handler (info, region, c);
2657 /* If we have at least one type match, end the search. */
2658 tree tp_node = c->u.catch.type_list;
2660 for (; tp_node; tp_node = TREE_CHAIN (tp_node))
2662 tree type = TREE_VALUE (tp_node);
2664 if (type == type_thrown
2665 || (lang_eh_type_covers
2666 && (*lang_eh_type_covers) (type, type_thrown)))
2668 add_reachable_handler (info, region, c);
2673 /* If we have definitive information of a match failure,
2674 the catch won't trigger. */
2675 if (lang_eh_type_covers)
2676 return RNL_NOT_CAUGHT;
2679 /* At this point, we either don't know what type is thrown or
2680 don't have front-end assistance to help deciding if it is
2681 covered by one of the types in the list for this region.
2683 We'd then like to add this region to the list of reachable
2684 handlers since it is indeed potentially reachable based on the
2685 information we have.
2687 Actually, this handler is for sure not reachable if all the
2688 types it matches have already been caught. That is, it is only
2689 potentially reachable if at least one of the types it catches
2690 has not been previously caught. */
2693 ret = RNL_MAYBE_CAUGHT;
2696 tree tp_node = c->u.catch.type_list;
2697 bool maybe_reachable = false;
2699 /* Compute the potential reachability of this handler and
2700 update the list of types caught at the same time. */
2701 for (; tp_node; tp_node = TREE_CHAIN (tp_node))
2703 tree type = TREE_VALUE (tp_node);
2705 if (! check_handled (info->types_caught, type))
2708 = tree_cons (NULL, type, info->types_caught);
2710 maybe_reachable = true;
2714 if (maybe_reachable)
2716 add_reachable_handler (info, region, c);
2718 /* ??? If the catch type is a base class of every allowed
2719 type, then we know we can stop the search. */
2720 ret = RNL_MAYBE_CAUGHT;
2728 case ERT_ALLOWED_EXCEPTIONS:
2729 /* An empty list of types definitely ends the search. */
2730 if (region->u.allowed.type_list == NULL_TREE)
2732 add_reachable_handler (info, region, region);
2736 /* Collect a list of lists of allowed types for use in detecting
2737 when a catch may be transformed into a catch-all. */
2739 info->types_allowed = tree_cons (NULL_TREE,
2740 region->u.allowed.type_list,
2741 info->types_allowed);
2743 /* If we have definitive information about the type hierarchy,
2744 then we can tell if the thrown type will pass through the
2746 if (type_thrown && lang_eh_type_covers)
2748 if (check_handled (region->u.allowed.type_list, type_thrown))
2749 return RNL_NOT_CAUGHT;
2752 add_reachable_handler (info, region, region);
2757 add_reachable_handler (info, region, region);
2758 return RNL_MAYBE_CAUGHT;
2761 /* Catch regions are handled by their controlling try region. */
2762 return RNL_NOT_CAUGHT;
2764 case ERT_MUST_NOT_THROW:
2765 /* Here we end our search, since no exceptions may propagate.
2766 If we've touched down at some landing pad previous, then the
2767 explicit function call we generated may be used. Otherwise
2768 the call is made by the runtime. */
2769 if (info && info->handlers)
2771 add_reachable_handler (info, region, region);
2780 /* Shouldn't see these here. */
2787 /* Retrieve a list of labels of exception handlers which can be
2788 reached by a given insn. */
2791 reachable_handlers (insn)
2794 struct reachable_info info;
2795 struct eh_region *region;
2799 if (GET_CODE (insn) == JUMP_INSN
2800 && GET_CODE (PATTERN (insn)) == RESX)
2801 region_number = XINT (PATTERN (insn), 0);
2804 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2805 if (!note || INTVAL (XEXP (note, 0)) <= 0)
2807 region_number = INTVAL (XEXP (note, 0));
2810 memset (&info, 0, sizeof (info));
2812 region = cfun->eh->region_array[region_number];
2814 type_thrown = NULL_TREE;
2815 if (GET_CODE (insn) == JUMP_INSN
2816 && GET_CODE (PATTERN (insn)) == RESX)
2818 /* A RESX leaves a region instead of entering it. Thus the
2819 region itself may have been deleted out from under us. */
2822 region = region->outer;
2824 else if (region->type == ERT_THROW)
2826 type_thrown = region->u.throw.type;
2827 region = region->outer;
2832 if (reachable_next_level (region, type_thrown, &info) >= RNL_CAUGHT)
2834 /* Forced unwind exceptions are neither BLOCKED nor CAUGHT.
2835 Make sure the cleanup regions are reachable. */
2836 if (flag_forced_unwind_exceptions)
2838 while ((region = region->outer) != NULL)
2839 if (region->type == ERT_CLEANUP)
2841 add_reachable_handler (&info, region, region);
2848 /* If we have processed one cleanup, there is no point in
2849 processing any more of them. Each cleanup will have an edge
2850 to the next outer cleanup region, so the flow graph will be
2852 if (region->type == ERT_CLEANUP)
2853 region = region->u.cleanup.prev_try;
2855 region = region->outer;
2858 return info.handlers;
2861 /* Determine if the given INSN can throw an exception that is caught
2862 within the function. */
2865 can_throw_internal (insn)
2868 struct eh_region *region;
2872 if (! INSN_P (insn))
2875 if (GET_CODE (insn) == INSN
2876 && GET_CODE (PATTERN (insn)) == SEQUENCE)
2877 insn = XVECEXP (PATTERN (insn), 0, 0);
2879 if (GET_CODE (insn) == CALL_INSN
2880 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
2883 for (i = 0; i < 3; ++i)
2885 rtx sub = XEXP (PATTERN (insn), i);
2886 for (; sub ; sub = NEXT_INSN (sub))
2887 if (can_throw_internal (sub))
2893 /* Every insn that might throw has an EH_REGION note. */
2894 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2895 if (!note || INTVAL (XEXP (note, 0)) <= 0)
2898 region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
2900 type_thrown = NULL_TREE;
2901 if (region->type == ERT_THROW)
2903 type_thrown = region->u.throw.type;
2904 region = region->outer;
2907 /* If this exception is ignored by each and every containing region,
2908 then control passes straight out. The runtime may handle some
2909 regions, which also do not require processing internally. */
2910 for (; region; region = region->outer)
2912 enum reachable_code how = reachable_next_level (region, type_thrown, 0);
2913 if (how == RNL_BLOCKED)
2915 if (how != RNL_NOT_CAUGHT)
2922 /* Determine if the given INSN can throw an exception that is
2923 visible outside the function. */
2926 can_throw_external (insn)
2929 struct eh_region *region;
2933 if (! INSN_P (insn))
2936 if (GET_CODE (insn) == INSN
2937 && GET_CODE (PATTERN (insn)) == SEQUENCE)
2938 insn = XVECEXP (PATTERN (insn), 0, 0);
2940 if (GET_CODE (insn) == CALL_INSN
2941 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
2944 for (i = 0; i < 3; ++i)
2946 rtx sub = XEXP (PATTERN (insn), i);
2947 for (; sub ; sub = NEXT_INSN (sub))
2948 if (can_throw_external (sub))
2954 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2957 /* Calls (and trapping insns) without notes are outside any
2958 exception handling region in this function. We have to
2959 assume it might throw. Given that the front end and middle
2960 ends mark known NOTHROW functions, this isn't so wildly
2962 return (GET_CODE (insn) == CALL_INSN
2963 || (flag_non_call_exceptions
2964 && may_trap_p (PATTERN (insn))));
2966 if (INTVAL (XEXP (note, 0)) <= 0)
2969 /* Forced unwind excptions are not catchable. */
2970 if (flag_forced_unwind_exceptions && GET_CODE (insn) == CALL_INSN)
2973 region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
2975 type_thrown = NULL_TREE;
2976 if (region->type == ERT_THROW)
2978 type_thrown = region->u.throw.type;
2979 region = region->outer;
2982 /* If the exception is caught or blocked by any containing region,
2983 then it is not seen by any calling function. */
2984 for (; region ; region = region->outer)
2985 if (reachable_next_level (region, type_thrown, NULL) >= RNL_CAUGHT)
2991 /* Set current_function_nothrow and cfun->all_throwers_are_sibcalls. */
2994 set_nothrow_function_flags ()
2998 current_function_nothrow = 1;
3000 /* Assume cfun->all_throwers_are_sibcalls until we encounter
3001 something that can throw an exception. We specifically exempt
3002 CALL_INSNs that are SIBLING_CALL_P, as these are really jumps,
3003 and can't throw. Most CALL_INSNs are not SIBLING_CALL_P, so this
3006 cfun->all_throwers_are_sibcalls = 1;
3008 if (! flag_exceptions)
3011 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3012 if (can_throw_external (insn))
3014 current_function_nothrow = 0;
3016 if (GET_CODE (insn) != CALL_INSN || !SIBLING_CALL_P (insn))
3018 cfun->all_throwers_are_sibcalls = 0;
3023 for (insn = current_function_epilogue_delay_list; insn;
3024 insn = XEXP (insn, 1))
3025 if (can_throw_external (insn))
3027 current_function_nothrow = 0;
3029 if (GET_CODE (insn) != CALL_INSN || !SIBLING_CALL_P (insn))
3031 cfun->all_throwers_are_sibcalls = 0;
3038 /* Various hooks for unwind library. */
3040 /* Do any necessary initialization to access arbitrary stack frames.
3041 On the SPARC, this means flushing the register windows. */
3044 expand_builtin_unwind_init ()
3046 /* Set this so all the registers get saved in our frame; we need to be
3047 able to copy the saved values for any registers from frames we unwind. */
3048 current_function_has_nonlocal_label = 1;
3050 #ifdef SETUP_FRAME_ADDRESSES
3051 SETUP_FRAME_ADDRESSES ();
3056 expand_builtin_eh_return_data_regno (arglist)
3059 tree which = TREE_VALUE (arglist);
3060 unsigned HOST_WIDE_INT iwhich;
3062 if (TREE_CODE (which) != INTEGER_CST)
3064 error ("argument of `__builtin_eh_return_regno' must be constant");
3068 iwhich = tree_low_cst (which, 1);
3069 iwhich = EH_RETURN_DATA_REGNO (iwhich);
3070 if (iwhich == INVALID_REGNUM)
3073 #ifdef DWARF_FRAME_REGNUM
3074 iwhich = DWARF_FRAME_REGNUM (iwhich);
3076 iwhich = DBX_REGISTER_NUMBER (iwhich);
3079 return GEN_INT (iwhich);
3082 /* Given a value extracted from the return address register or stack slot,
3083 return the actual address encoded in that value. */
3086 expand_builtin_extract_return_addr (addr_tree)
3089 rtx addr = expand_expr (addr_tree, NULL_RTX, Pmode, 0);
3091 if (GET_MODE (addr) != Pmode
3092 && GET_MODE (addr) != VOIDmode)
3094 #ifdef POINTERS_EXTEND_UNSIGNED
3095 addr = convert_memory_address (Pmode, addr);
3097 addr = convert_to_mode (Pmode, addr, 0);
3101 /* First mask out any unwanted bits. */
3102 #ifdef MASK_RETURN_ADDR
3103 expand_and (Pmode, addr, MASK_RETURN_ADDR, addr);
3106 /* Then adjust to find the real return address. */
3107 #if defined (RETURN_ADDR_OFFSET)
3108 addr = plus_constant (addr, RETURN_ADDR_OFFSET);
3114 /* Given an actual address in addr_tree, do any necessary encoding
3115 and return the value to be stored in the return address register or
3116 stack slot so the epilogue will return to that address. */
3119 expand_builtin_frob_return_addr (addr_tree)
3122 rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, 0);
3124 #ifdef POINTERS_EXTEND_UNSIGNED
3125 if (GET_MODE (addr) != Pmode)
3126 addr = convert_memory_address (Pmode, addr);
3129 #ifdef RETURN_ADDR_OFFSET
3130 addr = force_reg (Pmode, addr);
3131 addr = plus_constant (addr, -RETURN_ADDR_OFFSET);
3137 /* Set up the epilogue with the magic bits we'll need to return to the
3138 exception handler. */
3141 expand_builtin_eh_return (stackadj_tree, handler_tree)
3142 tree stackadj_tree, handler_tree;
3144 rtx stackadj, handler;
3146 stackadj = expand_expr (stackadj_tree, cfun->eh->ehr_stackadj, VOIDmode, 0);
3147 handler = expand_expr (handler_tree, cfun->eh->ehr_handler, VOIDmode, 0);
3149 #ifdef POINTERS_EXTEND_UNSIGNED
3150 if (GET_MODE (stackadj) != Pmode)
3151 stackadj = convert_memory_address (Pmode, stackadj);
3153 if (GET_MODE (handler) != Pmode)
3154 handler = convert_memory_address (Pmode, handler);
3157 if (! cfun->eh->ehr_label)
3159 cfun->eh->ehr_stackadj = copy_to_reg (stackadj);
3160 cfun->eh->ehr_handler = copy_to_reg (handler);
3161 cfun->eh->ehr_label = gen_label_rtx ();
3165 if (stackadj != cfun->eh->ehr_stackadj)
3166 emit_move_insn (cfun->eh->ehr_stackadj, stackadj);
3167 if (handler != cfun->eh->ehr_handler)
3168 emit_move_insn (cfun->eh->ehr_handler, handler);
3171 emit_jump (cfun->eh->ehr_label);
3177 rtx sa, ra, around_label;
3179 if (! cfun->eh->ehr_label)
3182 sa = EH_RETURN_STACKADJ_RTX;
3185 error ("__builtin_eh_return not supported on this target");
3189 current_function_calls_eh_return = 1;
3191 around_label = gen_label_rtx ();
3192 emit_move_insn (sa, const0_rtx);
3193 emit_jump (around_label);
3195 emit_label (cfun->eh->ehr_label);
3196 clobber_return_register ();
3198 #ifdef HAVE_eh_return
3200 emit_insn (gen_eh_return (cfun->eh->ehr_stackadj, cfun->eh->ehr_handler));
3204 ra = EH_RETURN_HANDLER_RTX;
3207 error ("__builtin_eh_return not supported on this target");
3208 ra = gen_reg_rtx (Pmode);
3211 emit_move_insn (sa, cfun->eh->ehr_stackadj);
3212 emit_move_insn (ra, cfun->eh->ehr_handler);
3215 emit_label (around_label);
3218 /* In the following functions, we represent entries in the action table
3219 as 1-based indices. Special cases are:
3221 0: null action record, non-null landing pad; implies cleanups
3222 -1: null action record, null landing pad; implies no action
3223 -2: no call-site entry; implies must_not_throw
3224 -3: we have yet to process outer regions
3226 Further, no special cases apply to the "next" field of the record.
3227 For next, 0 means end of list. */
3229 struct action_record
3237 action_record_eq (pentry, pdata)
3241 const struct action_record *entry = (const struct action_record *) pentry;
3242 const struct action_record *data = (const struct action_record *) pdata;
3243 return entry->filter == data->filter && entry->next == data->next;
3247 action_record_hash (pentry)
3250 const struct action_record *entry = (const struct action_record *) pentry;
3251 return entry->next * 1009 + entry->filter;
3255 add_action_record (ar_hash, filter, next)
3259 struct action_record **slot, *new, tmp;
3261 tmp.filter = filter;
3263 slot = (struct action_record **) htab_find_slot (ar_hash, &tmp, INSERT);
3265 if ((new = *slot) == NULL)
3267 new = (struct action_record *) xmalloc (sizeof (*new));
3268 new->offset = VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data) + 1;
3269 new->filter = filter;
3273 /* The filter value goes in untouched. The link to the next
3274 record is a "self-relative" byte offset, or zero to indicate
3275 that there is no next record. So convert the absolute 1 based
3276 indices we've been carrying around into a displacement. */
3278 push_sleb128 (&cfun->eh->action_record_data, filter);
3280 next -= VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data) + 1;
3281 push_sleb128 (&cfun->eh->action_record_data, next);
3288 collect_one_action_chain (ar_hash, region)
3290 struct eh_region *region;
3292 struct eh_region *c;
3295 /* If we've reached the top of the region chain, then we have
3296 no actions, and require no landing pad. */
3300 switch (region->type)
3303 /* A cleanup adds a zero filter to the beginning of the chain, but
3304 there are special cases to look out for. If there are *only*
3305 cleanups along a path, then it compresses to a zero action.
3306 Further, if there are multiple cleanups along a path, we only
3307 need to represent one of them, as that is enough to trigger
3308 entry to the landing pad at runtime. */
3309 next = collect_one_action_chain (ar_hash, region->outer);
3312 for (c = region->outer; c ; c = c->outer)
3313 if (c->type == ERT_CLEANUP)
3315 return add_action_record (ar_hash, 0, next);
3318 /* Process the associated catch regions in reverse order.
3319 If there's a catch-all handler, then we don't need to
3320 search outer regions. Use a magic -3 value to record
3321 that we haven't done the outer search. */
3323 for (c = region->u.try.last_catch; c ; c = c->u.catch.prev_catch)
3325 if (c->u.catch.type_list == NULL)
3329 /* Forced exceptions run cleanups, always. Record them if
3332 if (flag_forced_unwind_exceptions)
3334 struct eh_region *r;
3335 for (r = c->outer; r ; r = r->outer)
3336 if (r->type == ERT_CLEANUP)
3338 next = add_action_record (ar_hash, 0, 0);
3343 /* Retrieve the filter from the head of the filter list
3344 where we have stored it (see assign_filter_values). */
3345 filter = TREE_INT_CST_LOW (TREE_VALUE (c->u.catch.filter_list));
3346 next = add_action_record (ar_hash, filter, next);
3350 /* Once the outer search is done, trigger an action record for
3351 each filter we have. */
3356 next = collect_one_action_chain (ar_hash, region->outer);
3358 /* If there is no next action, terminate the chain. */
3361 /* If all outer actions are cleanups or must_not_throw,
3362 we'll have no action record for it, since we had wanted
3363 to encode these states in the call-site record directly.
3364 Add a cleanup action to the chain to catch these. */
3366 next = add_action_record (ar_hash, 0, 0);
3369 flt_node = c->u.catch.filter_list;
3370 for (; flt_node; flt_node = TREE_CHAIN (flt_node))
3372 int filter = TREE_INT_CST_LOW (TREE_VALUE (flt_node));
3373 next = add_action_record (ar_hash, filter, next);
3379 case ERT_ALLOWED_EXCEPTIONS:
3380 /* An exception specification adds its filter to the
3381 beginning of the chain. */
3382 next = collect_one_action_chain (ar_hash, region->outer);
3383 return add_action_record (ar_hash, region->u.allowed.filter,
3384 next < 0 ? 0 : next);
3386 case ERT_MUST_NOT_THROW:
3387 /* A must-not-throw region with no inner handlers or cleanups
3388 requires no call-site entry. Note that this differs from
3389 the no handler or cleanup case in that we do require an lsda
3390 to be generated. Return a magic -2 value to record this. */
3391 if (flag_forced_unwind_exceptions)
3393 struct eh_region *r;
3394 for (r = region->outer; r ; r = r->outer)
3395 if (r->type == ERT_CLEANUP)
3402 /* CATCH regions are handled in TRY above. THROW regions are
3403 for optimization information only and produce no output. */
3404 return collect_one_action_chain (ar_hash, region->outer);
3412 add_call_site (landing_pad, action)
3416 struct call_site_record *data = cfun->eh->call_site_data;
3417 int used = cfun->eh->call_site_data_used;
3418 int size = cfun->eh->call_site_data_size;
3422 size = (size ? size * 2 : 64);
3423 data = (struct call_site_record *)
3424 ggc_realloc (data, sizeof (*data) * size);
3425 cfun->eh->call_site_data = data;
3426 cfun->eh->call_site_data_size = size;
3429 data[used].landing_pad = landing_pad;
3430 data[used].action = action;
3432 cfun->eh->call_site_data_used = used + 1;
3434 return used + call_site_base;
3437 /* Turn REG_EH_REGION notes back into NOTE_INSN_EH_REGION notes.
3438 The new note numbers will not refer to region numbers, but
3439 instead to call site entries. */
3442 convert_to_eh_region_ranges ()
3444 rtx insn, iter, note;
3446 int last_action = -3;
3447 rtx last_action_insn = NULL_RTX;
3448 rtx last_landing_pad = NULL_RTX;
3449 rtx first_no_action_insn = NULL_RTX;
3452 if (USING_SJLJ_EXCEPTIONS || cfun->eh->region_tree == NULL)
3455 VARRAY_UCHAR_INIT (cfun->eh->action_record_data, 64, "action_record_data");
3457 ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
3459 for (iter = get_insns (); iter ; iter = NEXT_INSN (iter))
3462 struct eh_region *region;
3464 rtx this_landing_pad;
3467 if (GET_CODE (insn) == INSN
3468 && GET_CODE (PATTERN (insn)) == SEQUENCE)
3469 insn = XVECEXP (PATTERN (insn), 0, 0);
3471 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3474 if (! (GET_CODE (insn) == CALL_INSN
3475 || (flag_non_call_exceptions
3476 && may_trap_p (PATTERN (insn)))))
3483 if (INTVAL (XEXP (note, 0)) <= 0)
3485 region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
3486 this_action = collect_one_action_chain (ar_hash, region);
3489 /* Existence of catch handlers, or must-not-throw regions
3490 implies that an lsda is needed (even if empty). */
3491 if (this_action != -1)
3492 cfun->uses_eh_lsda = 1;
3494 /* Delay creation of region notes for no-action regions
3495 until we're sure that an lsda will be required. */
3496 else if (last_action == -3)
3498 first_no_action_insn = iter;
3502 /* Cleanups and handlers may share action chains but not
3503 landing pads. Collect the landing pad for this region. */
3504 if (this_action >= 0)
3506 struct eh_region *o;
3507 for (o = region; ! o->landing_pad ; o = o->outer)
3509 this_landing_pad = o->landing_pad;
3512 this_landing_pad = NULL_RTX;
3514 /* Differing actions or landing pads implies a change in call-site
3515 info, which implies some EH_REGION note should be emitted. */
3516 if (last_action != this_action
3517 || last_landing_pad != this_landing_pad)
3519 /* If we'd not seen a previous action (-3) or the previous
3520 action was must-not-throw (-2), then we do not need an
3522 if (last_action >= -1)
3524 /* If we delayed the creation of the begin, do it now. */
3525 if (first_no_action_insn)
3527 call_site = add_call_site (NULL_RTX, 0);
3528 note = emit_note_before (NOTE_INSN_EH_REGION_BEG,
3529 first_no_action_insn);
3530 NOTE_EH_HANDLER (note) = call_site;
3531 first_no_action_insn = NULL_RTX;
3534 note = emit_note_after (NOTE_INSN_EH_REGION_END,
3536 NOTE_EH_HANDLER (note) = call_site;
3539 /* If the new action is must-not-throw, then no region notes
3541 if (this_action >= -1)
3543 call_site = add_call_site (this_landing_pad,
3544 this_action < 0 ? 0 : this_action);
3545 note = emit_note_before (NOTE_INSN_EH_REGION_BEG, iter);
3546 NOTE_EH_HANDLER (note) = call_site;
3549 last_action = this_action;
3550 last_landing_pad = this_landing_pad;
3552 last_action_insn = iter;
3555 if (last_action >= -1 && ! first_no_action_insn)
3557 note = emit_note_after (NOTE_INSN_EH_REGION_END, last_action_insn);
3558 NOTE_EH_HANDLER (note) = call_site;
3561 htab_delete (ar_hash);
3566 push_uleb128 (data_area, value)
3567 varray_type *data_area;
3572 unsigned char byte = value & 0x7f;
3576 VARRAY_PUSH_UCHAR (*data_area, byte);
3582 push_sleb128 (data_area, value)
3583 varray_type *data_area;
3591 byte = value & 0x7f;
3593 more = ! ((value == 0 && (byte & 0x40) == 0)
3594 || (value == -1 && (byte & 0x40) != 0));
3597 VARRAY_PUSH_UCHAR (*data_area, byte);
3603 #ifndef HAVE_AS_LEB128
3605 dw2_size_of_call_site_table ()
3607 int n = cfun->eh->call_site_data_used;
3608 int size = n * (4 + 4 + 4);
3611 for (i = 0; i < n; ++i)
3613 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3614 size += size_of_uleb128 (cs->action);
3621 sjlj_size_of_call_site_table ()
3623 int n = cfun->eh->call_site_data_used;
3627 for (i = 0; i < n; ++i)
3629 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3630 size += size_of_uleb128 (INTVAL (cs->landing_pad));
3631 size += size_of_uleb128 (cs->action);
3639 dw2_output_call_site_table ()
3641 const char *const function_start_lab
3642 = IDENTIFIER_POINTER (current_function_func_begin_label);
3643 int n = cfun->eh->call_site_data_used;
3646 for (i = 0; i < n; ++i)
3648 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3649 char reg_start_lab[32];
3650 char reg_end_lab[32];
3651 char landing_pad_lab[32];
3653 ASM_GENERATE_INTERNAL_LABEL (reg_start_lab, "LEHB", call_site_base + i);
3654 ASM_GENERATE_INTERNAL_LABEL (reg_end_lab, "LEHE", call_site_base + i);
3656 if (cs->landing_pad)
3657 ASM_GENERATE_INTERNAL_LABEL (landing_pad_lab, "L",
3658 CODE_LABEL_NUMBER (cs->landing_pad));
3660 /* ??? Perhaps use insn length scaling if the assembler supports
3661 generic arithmetic. */
3662 /* ??? Perhaps use attr_length to choose data1 or data2 instead of
3663 data4 if the function is small enough. */
3664 #ifdef HAVE_AS_LEB128
3665 dw2_asm_output_delta_uleb128 (reg_start_lab, function_start_lab,
3666 "region %d start", i);
3667 dw2_asm_output_delta_uleb128 (reg_end_lab, reg_start_lab,
3669 if (cs->landing_pad)
3670 dw2_asm_output_delta_uleb128 (landing_pad_lab, function_start_lab,
3673 dw2_asm_output_data_uleb128 (0, "landing pad");
3675 dw2_asm_output_delta (4, reg_start_lab, function_start_lab,
3676 "region %d start", i);
3677 dw2_asm_output_delta (4, reg_end_lab, reg_start_lab, "length");
3678 if (cs->landing_pad)
3679 dw2_asm_output_delta (4, landing_pad_lab, function_start_lab,
3682 dw2_asm_output_data (4, 0, "landing pad");
3684 dw2_asm_output_data_uleb128 (cs->action, "action");
3687 call_site_base += n;
3691 sjlj_output_call_site_table ()
3693 int n = cfun->eh->call_site_data_used;
3696 for (i = 0; i < n; ++i)
3698 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3700 dw2_asm_output_data_uleb128 (INTVAL (cs->landing_pad),
3701 "region %d landing pad", i);
3702 dw2_asm_output_data_uleb128 (cs->action, "action");
3705 call_site_base += n;
3708 /* Tell assembler to switch to the section for the exception handling
3712 default_exception_section ()
3714 if (targetm.have_named_sections)
3717 #ifdef HAVE_LD_RO_RW_SECTION_MIXING
3718 int tt_format = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3721 || ((tt_format & 0x70) != DW_EH_PE_absptr
3722 && (tt_format & 0x70) != DW_EH_PE_aligned))
3723 ? 0 : SECTION_WRITE;
3725 flags = SECTION_WRITE;
3727 named_section_flags (".gcc_except_table", flags);
3732 readonly_data_section ();
3736 output_function_exception_table ()
3738 int tt_format, cs_format, lp_format, i, n;
3739 #ifdef HAVE_AS_LEB128
3740 char ttype_label[32];
3741 char cs_after_size_label[32];
3742 char cs_end_label[32];
3747 int tt_format_size = 0;
3749 /* Not all functions need anything. */
3750 if (! cfun->uses_eh_lsda)
3753 #ifdef IA64_UNWIND_INFO
3754 fputs ("\t.personality\t", asm_out_file);
3755 output_addr_const (asm_out_file, eh_personality_libfunc);
3756 fputs ("\n\t.handlerdata\n", asm_out_file);
3757 /* Note that varasm still thinks we're in the function's code section.
3758 The ".endp" directive that will immediately follow will take us back. */
3760 (*targetm.asm_out.exception_section) ();
3763 have_tt_data = (VARRAY_ACTIVE_SIZE (cfun->eh->ttype_data) > 0
3764 || VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data) > 0);
3766 /* Indicate the format of the @TType entries. */
3768 tt_format = DW_EH_PE_omit;
3771 tt_format = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3772 #ifdef HAVE_AS_LEB128
3773 ASM_GENERATE_INTERNAL_LABEL (ttype_label, "LLSDATT",
3774 current_function_funcdef_no);
3776 tt_format_size = size_of_encoded_value (tt_format);
3778 assemble_align (tt_format_size * BITS_PER_UNIT);
3781 (*targetm.asm_out.internal_label) (asm_out_file, "LLSDA",
3782 current_function_funcdef_no);
3784 /* The LSDA header. */
3786 /* Indicate the format of the landing pad start pointer. An omitted
3787 field implies @LPStart == @Start. */
3788 /* Currently we always put @LPStart == @Start. This field would
3789 be most useful in moving the landing pads completely out of
3790 line to another section, but it could also be used to minimize
3791 the size of uleb128 landing pad offsets. */
3792 lp_format = DW_EH_PE_omit;
3793 dw2_asm_output_data (1, lp_format, "@LPStart format (%s)",
3794 eh_data_format_name (lp_format));
3796 /* @LPStart pointer would go here. */
3798 dw2_asm_output_data (1, tt_format, "@TType format (%s)",
3799 eh_data_format_name (tt_format));
3801 #ifndef HAVE_AS_LEB128
3802 if (USING_SJLJ_EXCEPTIONS)
3803 call_site_len = sjlj_size_of_call_site_table ();
3805 call_site_len = dw2_size_of_call_site_table ();
3808 /* A pc-relative 4-byte displacement to the @TType data. */
3811 #ifdef HAVE_AS_LEB128
3812 char ttype_after_disp_label[32];
3813 ASM_GENERATE_INTERNAL_LABEL (ttype_after_disp_label, "LLSDATTD",
3814 current_function_funcdef_no);
3815 dw2_asm_output_delta_uleb128 (ttype_label, ttype_after_disp_label,
3816 "@TType base offset");
3817 ASM_OUTPUT_LABEL (asm_out_file, ttype_after_disp_label);
3819 /* Ug. Alignment queers things. */
3820 unsigned int before_disp, after_disp, last_disp, disp;
3822 before_disp = 1 + 1;
3823 after_disp = (1 + size_of_uleb128 (call_site_len)
3825 + VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data)
3826 + (VARRAY_ACTIVE_SIZE (cfun->eh->ttype_data)
3832 unsigned int disp_size, pad;
3835 disp_size = size_of_uleb128 (disp);
3836 pad = before_disp + disp_size + after_disp;
3837 if (pad % tt_format_size)
3838 pad = tt_format_size - (pad % tt_format_size);
3841 disp = after_disp + pad;
3843 while (disp != last_disp);
3845 dw2_asm_output_data_uleb128 (disp, "@TType base offset");
3849 /* Indicate the format of the call-site offsets. */
3850 #ifdef HAVE_AS_LEB128
3851 cs_format = DW_EH_PE_uleb128;
3853 cs_format = DW_EH_PE_udata4;
3855 dw2_asm_output_data (1, cs_format, "call-site format (%s)",
3856 eh_data_format_name (cs_format));
3858 #ifdef HAVE_AS_LEB128
3859 ASM_GENERATE_INTERNAL_LABEL (cs_after_size_label, "LLSDACSB",
3860 current_function_funcdef_no);
3861 ASM_GENERATE_INTERNAL_LABEL (cs_end_label, "LLSDACSE",
3862 current_function_funcdef_no);
3863 dw2_asm_output_delta_uleb128 (cs_end_label, cs_after_size_label,
3864 "Call-site table length");
3865 ASM_OUTPUT_LABEL (asm_out_file, cs_after_size_label);
3866 if (USING_SJLJ_EXCEPTIONS)
3867 sjlj_output_call_site_table ();
3869 dw2_output_call_site_table ();
3870 ASM_OUTPUT_LABEL (asm_out_file, cs_end_label);
3872 dw2_asm_output_data_uleb128 (call_site_len,"Call-site table length");
3873 if (USING_SJLJ_EXCEPTIONS)
3874 sjlj_output_call_site_table ();
3876 dw2_output_call_site_table ();
3879 /* ??? Decode and interpret the data for flag_debug_asm. */
3880 n = VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data);
3881 for (i = 0; i < n; ++i)
3882 dw2_asm_output_data (1, VARRAY_UCHAR (cfun->eh->action_record_data, i),
3883 (i ? NULL : "Action record table"));
3886 assemble_align (tt_format_size * BITS_PER_UNIT);
3888 i = VARRAY_ACTIVE_SIZE (cfun->eh->ttype_data);
3891 tree type = VARRAY_TREE (cfun->eh->ttype_data, i);
3894 if (type == NULL_TREE)
3895 type = integer_zero_node;
3897 type = lookup_type_for_runtime (type);
3899 value = expand_expr (type, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
3900 if (tt_format == DW_EH_PE_absptr || tt_format == DW_EH_PE_aligned)
3901 assemble_integer (value, tt_format_size,
3902 tt_format_size * BITS_PER_UNIT, 1);
3904 dw2_asm_output_encoded_addr_rtx (tt_format, value, NULL);
3907 #ifdef HAVE_AS_LEB128
3909 ASM_OUTPUT_LABEL (asm_out_file, ttype_label);
3912 /* ??? Decode and interpret the data for flag_debug_asm. */
3913 n = VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data);
3914 for (i = 0; i < n; ++i)
3915 dw2_asm_output_data (1, VARRAY_UCHAR (cfun->eh->ehspec_data, i),
3916 (i ? NULL : "Exception specification table"));
3918 function_section (current_function_decl);
3921 #include "gt-except.h"