1 /* Implements exception handling.
2 Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4 Contributed by Mike Stump <mrs@cygnus.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 /* An exception is an event that can be signaled from within a
25 function. This event can then be "caught" or "trapped" by the
26 callers of this function. This potentially allows program flow to
27 be transferred to any arbitrary code associated with a function call
28 several levels up the stack.
30 The intended use for this mechanism is for signaling "exceptional
31 events" in an out-of-band fashion, hence its name. The C++ language
32 (and many other OO-styled or functional languages) practically
33 requires such a mechanism, as otherwise it becomes very difficult
34 or even impossible to signal failure conditions in complex
35 situations. The traditional C++ example is when an error occurs in
36 the process of constructing an object; without such a mechanism, it
37 is impossible to signal that the error occurs without adding global
38 state variables and error checks around every object construction.
40 The act of causing this event to occur is referred to as "throwing
41 an exception". (Alternate terms include "raising an exception" or
42 "signaling an exception".) The term "throw" is used because control
43 is returned to the callers of the function that is signaling the
44 exception, and thus there is the concept of "throwing" the
45 exception up the call stack.
47 [ Add updated documentation on how to use this. ] */
52 #include "coretypes.h"
60 #include "insn-config.h"
62 #include "integrate.h"
63 #include "hard-reg-set.h"
64 #include "basic-block.h"
66 #include "dwarf2asm.h"
67 #include "dwarf2out.h"
75 #include "langhooks.h"
78 /* Provide defaults for stuff that may not be defined when using
80 #ifndef EH_RETURN_DATA_REGNO
81 #define EH_RETURN_DATA_REGNO(N) INVALID_REGNUM
85 /* Nonzero means enable synchronous exceptions for non-call instructions. */
86 int flag_non_call_exceptions
;
88 /* Protect cleanup actions with must-not-throw regions, with a call
89 to the given failure handler. */
90 tree (*lang_protect_cleanup_actions
) (void);
92 /* Return true if type A catches type B. */
93 int (*lang_eh_type_covers
) (tree a
, tree b
);
95 /* Map a type to a runtime object to match type. */
96 tree (*lang_eh_runtime_type
) (tree
);
98 /* A hash table of label to region number. */
100 struct ehl_map_entry
GTY(())
103 struct eh_region
*region
;
106 static GTY(()) int call_site_base
;
107 static GTY ((param_is (union tree_node
)))
108 htab_t type_to_runtime_map
;
110 /* Describe the SjLj_Function_Context structure. */
111 static GTY(()) tree sjlj_fc_type_node
;
112 static int sjlj_fc_call_site_ofs
;
113 static int sjlj_fc_data_ofs
;
114 static int sjlj_fc_personality_ofs
;
115 static int sjlj_fc_lsda_ofs
;
116 static int sjlj_fc_jbuf_ofs
;
118 /* Describes one exception region. */
119 struct eh_region
GTY(())
121 /* The immediately surrounding region. */
122 struct eh_region
*outer
;
124 /* The list of immediately contained regions. */
125 struct eh_region
*inner
;
126 struct eh_region
*next_peer
;
128 /* An identifier for this region. */
131 /* When a region is deleted, its parents inherit the REG_EH_REGION
132 numbers already assigned. */
135 /* Each region does exactly one thing. */
142 ERT_ALLOWED_EXCEPTIONS
,
148 /* Holds the action to perform based on the preceding type. */
150 /* A list of catch blocks, a surrounding try block,
151 and the label for continuing after a catch. */
152 struct eh_region_u_try
{
153 struct eh_region
*catch;
154 struct eh_region
*last_catch
;
155 struct eh_region
*prev_try
;
157 } GTY ((tag ("ERT_TRY"))) try;
159 /* The list through the catch handlers, the list of type objects
160 matched, and the list of associated filters. */
161 struct eh_region_u_catch
{
162 struct eh_region
*next_catch
;
163 struct eh_region
*prev_catch
;
166 } GTY ((tag ("ERT_CATCH"))) catch;
168 /* A tree_list of allowed types. */
169 struct eh_region_u_allowed
{
172 } GTY ((tag ("ERT_ALLOWED_EXCEPTIONS"))) allowed
;
174 /* The type given by a call to "throw foo();", or discovered
176 struct eh_region_u_throw
{
178 } GTY ((tag ("ERT_THROW"))) throw;
180 /* Retain the cleanup expression even after expansion so that
181 we can match up fixup regions. */
182 struct eh_region_u_cleanup
{
184 struct eh_region
*prev_try
;
185 } GTY ((tag ("ERT_CLEANUP"))) cleanup
;
187 /* The real region (by expression and by pointer) that fixup code
189 struct eh_region_u_fixup
{
191 struct eh_region
*real_region
;
192 } GTY ((tag ("ERT_FIXUP"))) fixup
;
193 } GTY ((desc ("%0.type"))) u
;
195 /* Entry point for this region's handler before landing pads are built. */
198 /* Entry point for this region's handler from the runtime eh library. */
201 /* Entry point for this region's handler from an inner region. */
202 rtx post_landing_pad
;
204 /* The RESX insn for handing off control to the next outermost handler,
208 /* True if something in this region may throw. */
209 unsigned may_contain_throw
: 1;
212 struct call_site_record
GTY(())
218 /* Used to save exception status for each function. */
219 struct eh_status
GTY(())
221 /* The tree of all regions for this function. */
222 struct eh_region
*region_tree
;
224 /* The same information as an indexable array. */
225 struct eh_region
** GTY ((length ("%h.last_region_number"))) region_array
;
227 /* The most recently open region. */
228 struct eh_region
*cur_region
;
230 /* This is the region for which we are processing catch blocks. */
231 struct eh_region
*try_region
;
236 int built_landing_pads
;
237 int last_region_number
;
239 varray_type ttype_data
;
240 varray_type ehspec_data
;
241 varray_type action_record_data
;
243 htab_t
GTY ((param_is (struct ehl_map_entry
))) exception_handler_label_map
;
245 struct call_site_record
* GTY ((length ("%h.call_site_data_used")))
247 int call_site_data_used
;
248 int call_site_data_size
;
259 static int t2r_eq (const void *, const void *);
260 static hashval_t
t2r_hash (const void *);
261 static void add_type_for_runtime (tree
);
262 static tree
lookup_type_for_runtime (tree
);
264 static struct eh_region
*expand_eh_region_end (void);
266 static rtx
get_exception_filter (struct function
*);
268 static void collect_eh_region_array (void);
269 static void resolve_fixup_regions (void);
270 static void remove_fixup_regions (void);
271 static void remove_unreachable_regions (rtx
);
272 static void convert_from_eh_region_ranges_1 (rtx
*, int *, int);
274 static struct eh_region
*duplicate_eh_region_1 (struct eh_region
*,
275 struct inline_remap
*);
276 static void duplicate_eh_region_2 (struct eh_region
*, struct eh_region
**);
277 static int ttypes_filter_eq (const void *, const void *);
278 static hashval_t
ttypes_filter_hash (const void *);
279 static int ehspec_filter_eq (const void *, const void *);
280 static hashval_t
ehspec_filter_hash (const void *);
281 static int add_ttypes_entry (htab_t
, tree
);
282 static int add_ehspec_entry (htab_t
, htab_t
, tree
);
283 static void assign_filter_values (void);
284 static void build_post_landing_pads (void);
285 static void connect_post_landing_pads (void);
286 static void dw2_build_landing_pads (void);
289 static bool sjlj_find_directly_reachable_regions (struct sjlj_lp_info
*);
290 static void sjlj_assign_call_site_values (rtx
, struct sjlj_lp_info
*);
291 static void sjlj_mark_call_sites (struct sjlj_lp_info
*);
292 static void sjlj_emit_function_enter (rtx
);
293 static void sjlj_emit_function_exit (void);
294 static void sjlj_emit_dispatch_table (rtx
, struct sjlj_lp_info
*);
295 static void sjlj_build_landing_pads (void);
297 static hashval_t
ehl_hash (const void *);
298 static int ehl_eq (const void *, const void *);
299 static void add_ehl_entry (rtx
, struct eh_region
*);
300 static void remove_exception_handler_label (rtx
);
301 static void remove_eh_handler (struct eh_region
*);
302 static int for_each_eh_label_1 (void **, void *);
304 struct reachable_info
;
306 /* The return value of reachable_next_level. */
309 /* The given exception is not processed by the given region. */
311 /* The given exception may need processing by the given region. */
313 /* The given exception is completely processed by the given region. */
315 /* The given exception is completely processed by the runtime. */
319 static int check_handled (tree
, tree
);
320 static void add_reachable_handler (struct reachable_info
*,
321 struct eh_region
*, struct eh_region
*);
322 static enum reachable_code
reachable_next_level (struct eh_region
*, tree
,
323 struct reachable_info
*);
325 static int action_record_eq (const void *, const void *);
326 static hashval_t
action_record_hash (const void *);
327 static int add_action_record (htab_t
, int, int);
328 static int collect_one_action_chain (htab_t
, struct eh_region
*);
329 static int add_call_site (rtx
, int);
331 static void push_uleb128 (varray_type
*, unsigned int);
332 static void push_sleb128 (varray_type
*, int);
333 #ifndef HAVE_AS_LEB128
334 static int dw2_size_of_call_site_table (void);
335 static int sjlj_size_of_call_site_table (void);
337 static void dw2_output_call_site_table (void);
338 static void sjlj_output_call_site_table (void);
341 /* Routine to see if exception handling is turned on.
342 DO_WARN is nonzero if we want to inform the user that exception
343 handling is turned off.
345 This is used to ensure that -fexceptions has been specified if the
346 compiler tries to use any exception-specific functions. */
349 doing_eh (int do_warn
)
351 if (! flag_exceptions
)
353 static int warned
= 0;
354 if (! warned
&& do_warn
)
356 error ("exception handling disabled, use -fexceptions to enable");
368 if (! flag_exceptions
)
371 type_to_runtime_map
= htab_create_ggc (31, t2r_hash
, t2r_eq
, NULL
);
373 /* Create the SjLj_Function_Context structure. This should match
374 the definition in unwind-sjlj.c. */
375 if (USING_SJLJ_EXCEPTIONS
)
377 tree f_jbuf
, f_per
, f_lsda
, f_prev
, f_cs
, f_data
, tmp
;
379 sjlj_fc_type_node
= (*lang_hooks
.types
.make_type
) (RECORD_TYPE
);
381 f_prev
= build_decl (FIELD_DECL
, get_identifier ("__prev"),
382 build_pointer_type (sjlj_fc_type_node
));
383 DECL_FIELD_CONTEXT (f_prev
) = sjlj_fc_type_node
;
385 f_cs
= build_decl (FIELD_DECL
, get_identifier ("__call_site"),
387 DECL_FIELD_CONTEXT (f_cs
) = sjlj_fc_type_node
;
389 tmp
= build_index_type (build_int_2 (4 - 1, 0));
390 tmp
= build_array_type ((*lang_hooks
.types
.type_for_mode
) (word_mode
, 1),
392 f_data
= build_decl (FIELD_DECL
, get_identifier ("__data"), tmp
);
393 DECL_FIELD_CONTEXT (f_data
) = sjlj_fc_type_node
;
395 f_per
= build_decl (FIELD_DECL
, get_identifier ("__personality"),
397 DECL_FIELD_CONTEXT (f_per
) = sjlj_fc_type_node
;
399 f_lsda
= build_decl (FIELD_DECL
, get_identifier ("__lsda"),
401 DECL_FIELD_CONTEXT (f_lsda
) = sjlj_fc_type_node
;
403 #ifdef DONT_USE_BUILTIN_SETJMP
405 tmp
= build_int_2 (JMP_BUF_SIZE
- 1, 0);
407 /* Should be large enough for most systems, if it is not,
408 JMP_BUF_SIZE should be defined with the proper value. It will
409 also tend to be larger than necessary for most systems, a more
410 optimal port will define JMP_BUF_SIZE. */
411 tmp
= build_int_2 (FIRST_PSEUDO_REGISTER
+ 2 - 1, 0);
414 /* This is 2 for builtin_setjmp, plus whatever the target requires
415 via STACK_SAVEAREA_MODE (SAVE_NONLOCAL). */
416 tmp
= build_int_2 ((GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL
))
417 / GET_MODE_SIZE (Pmode
)) + 2 - 1, 0);
419 tmp
= build_index_type (tmp
);
420 tmp
= build_array_type (ptr_type_node
, tmp
);
421 f_jbuf
= build_decl (FIELD_DECL
, get_identifier ("__jbuf"), tmp
);
422 #ifdef DONT_USE_BUILTIN_SETJMP
423 /* We don't know what the alignment requirements of the
424 runtime's jmp_buf has. Overestimate. */
425 DECL_ALIGN (f_jbuf
) = BIGGEST_ALIGNMENT
;
426 DECL_USER_ALIGN (f_jbuf
) = 1;
428 DECL_FIELD_CONTEXT (f_jbuf
) = sjlj_fc_type_node
;
430 TYPE_FIELDS (sjlj_fc_type_node
) = f_prev
;
431 TREE_CHAIN (f_prev
) = f_cs
;
432 TREE_CHAIN (f_cs
) = f_data
;
433 TREE_CHAIN (f_data
) = f_per
;
434 TREE_CHAIN (f_per
) = f_lsda
;
435 TREE_CHAIN (f_lsda
) = f_jbuf
;
437 layout_type (sjlj_fc_type_node
);
439 /* Cache the interesting field offsets so that we have
440 easy access from rtl. */
441 sjlj_fc_call_site_ofs
442 = (tree_low_cst (DECL_FIELD_OFFSET (f_cs
), 1)
443 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_cs
), 1) / BITS_PER_UNIT
);
445 = (tree_low_cst (DECL_FIELD_OFFSET (f_data
), 1)
446 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_data
), 1) / BITS_PER_UNIT
);
447 sjlj_fc_personality_ofs
448 = (tree_low_cst (DECL_FIELD_OFFSET (f_per
), 1)
449 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_per
), 1) / BITS_PER_UNIT
);
451 = (tree_low_cst (DECL_FIELD_OFFSET (f_lsda
), 1)
452 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_lsda
), 1) / BITS_PER_UNIT
);
454 = (tree_low_cst (DECL_FIELD_OFFSET (f_jbuf
), 1)
455 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_jbuf
), 1) / BITS_PER_UNIT
);
460 init_eh_for_function (void)
462 cfun
->eh
= ggc_alloc_cleared (sizeof (struct eh_status
));
465 /* Start an exception handling region. All instructions emitted
466 after this point are considered to be part of the region until
467 expand_eh_region_end is invoked. */
470 expand_eh_region_start (void)
472 struct eh_region
*new_region
;
473 struct eh_region
*cur_region
;
479 /* Insert a new blank region as a leaf in the tree. */
480 new_region
= ggc_alloc_cleared (sizeof (*new_region
));
481 cur_region
= cfun
->eh
->cur_region
;
482 new_region
->outer
= cur_region
;
485 new_region
->next_peer
= cur_region
->inner
;
486 cur_region
->inner
= new_region
;
490 new_region
->next_peer
= cfun
->eh
->region_tree
;
491 cfun
->eh
->region_tree
= new_region
;
493 cfun
->eh
->cur_region
= new_region
;
495 /* Create a note marking the start of this region. */
496 new_region
->region_number
= ++cfun
->eh
->last_region_number
;
497 note
= emit_note (NOTE_INSN_EH_REGION_BEG
);
498 NOTE_EH_HANDLER (note
) = new_region
->region_number
;
501 /* Common code to end a region. Returns the region just ended. */
503 static struct eh_region
*
504 expand_eh_region_end (void)
506 struct eh_region
*cur_region
= cfun
->eh
->cur_region
;
509 /* Create a note marking the end of this region. */
510 note
= emit_note (NOTE_INSN_EH_REGION_END
);
511 NOTE_EH_HANDLER (note
) = cur_region
->region_number
;
514 cfun
->eh
->cur_region
= cur_region
->outer
;
519 /* End an exception handling region for a cleanup. HANDLER is an
520 expression to expand for the cleanup. */
523 expand_eh_region_end_cleanup (tree handler
)
525 struct eh_region
*region
;
526 tree protect_cleanup_actions
;
533 region
= expand_eh_region_end ();
534 region
->type
= ERT_CLEANUP
;
535 region
->label
= gen_label_rtx ();
536 region
->u
.cleanup
.exp
= handler
;
537 region
->u
.cleanup
.prev_try
= cfun
->eh
->try_region
;
539 around_label
= gen_label_rtx ();
540 emit_jump (around_label
);
542 emit_label (region
->label
);
544 if (flag_non_call_exceptions
|| region
->may_contain_throw
)
546 /* Give the language a chance to specify an action to be taken if an
547 exception is thrown that would propagate out of the HANDLER. */
548 protect_cleanup_actions
549 = (lang_protect_cleanup_actions
550 ? (*lang_protect_cleanup_actions
) ()
553 if (protect_cleanup_actions
)
554 expand_eh_region_start ();
556 /* In case this cleanup involves an inline destructor with a try block in
557 it, we need to save the EH return data registers around it. */
558 data_save
[0] = gen_reg_rtx (ptr_mode
);
559 emit_move_insn (data_save
[0], get_exception_pointer (cfun
));
560 data_save
[1] = gen_reg_rtx (word_mode
);
561 emit_move_insn (data_save
[1], get_exception_filter (cfun
));
563 expand_expr (handler
, const0_rtx
, VOIDmode
, 0);
565 emit_move_insn (cfun
->eh
->exc_ptr
, data_save
[0]);
566 emit_move_insn (cfun
->eh
->filter
, data_save
[1]);
568 if (protect_cleanup_actions
)
569 expand_eh_region_end_must_not_throw (protect_cleanup_actions
);
571 /* We need any stack adjustment complete before the around_label. */
572 do_pending_stack_adjust ();
575 /* We delay the generation of the _Unwind_Resume until we generate
576 landing pads. We emit a marker here so as to get good control
577 flow data in the meantime. */
579 = emit_jump_insn (gen_rtx_RESX (VOIDmode
, region
->region_number
));
582 emit_label (around_label
);
585 /* End an exception handling region for a try block, and prepares
586 for subsequent calls to expand_start_catch. */
589 expand_start_all_catch (void)
591 struct eh_region
*region
;
596 region
= expand_eh_region_end ();
597 region
->type
= ERT_TRY
;
598 region
->u
.try.prev_try
= cfun
->eh
->try_region
;
599 region
->u
.try.continue_label
= gen_label_rtx ();
601 cfun
->eh
->try_region
= region
;
603 emit_jump (region
->u
.try.continue_label
);
606 /* Begin a catch clause. TYPE is the type caught, a list of such types, or
607 null if this is a catch-all clause. Providing a type list enables to
608 associate the catch region with potentially several exception types, which
609 is useful e.g. for Ada. */
612 expand_start_catch (tree type_or_list
)
614 struct eh_region
*t
, *c
, *l
;
620 type_list
= type_or_list
;
624 /* Ensure to always end up with a type list to normalize further
625 processing, then register each type against the runtime types
629 if (TREE_CODE (type_or_list
) != TREE_LIST
)
630 type_list
= tree_cons (NULL_TREE
, type_or_list
, NULL_TREE
);
632 type_node
= type_list
;
633 for (; type_node
; type_node
= TREE_CHAIN (type_node
))
634 add_type_for_runtime (TREE_VALUE (type_node
));
637 expand_eh_region_start ();
639 t
= cfun
->eh
->try_region
;
640 c
= cfun
->eh
->cur_region
;
642 c
->u
.catch.type_list
= type_list
;
643 c
->label
= gen_label_rtx ();
645 l
= t
->u
.try.last_catch
;
646 c
->u
.catch.prev_catch
= l
;
648 l
->u
.catch.next_catch
= c
;
651 t
->u
.try.last_catch
= c
;
653 emit_label (c
->label
);
656 /* End a catch clause. Control will resume after the try/catch block. */
659 expand_end_catch (void)
661 struct eh_region
*try_region
;
666 expand_eh_region_end ();
667 try_region
= cfun
->eh
->try_region
;
669 emit_jump (try_region
->u
.try.continue_label
);
672 /* End a sequence of catch handlers for a try block. */
675 expand_end_all_catch (void)
677 struct eh_region
*try_region
;
682 try_region
= cfun
->eh
->try_region
;
683 cfun
->eh
->try_region
= try_region
->u
.try.prev_try
;
685 emit_label (try_region
->u
.try.continue_label
);
688 /* End an exception region for an exception type filter. ALLOWED is a
689 TREE_LIST of types to be matched by the runtime. FAILURE is an
690 expression to invoke if a mismatch occurs.
692 ??? We could use these semantics for calls to rethrow, too; if we can
693 see the surrounding catch clause, we know that the exception we're
694 rethrowing satisfies the "filter" of the catch type. */
697 expand_eh_region_end_allowed (tree allowed
, tree failure
)
699 struct eh_region
*region
;
705 region
= expand_eh_region_end ();
706 region
->type
= ERT_ALLOWED_EXCEPTIONS
;
707 region
->u
.allowed
.type_list
= allowed
;
708 region
->label
= gen_label_rtx ();
710 for (; allowed
; allowed
= TREE_CHAIN (allowed
))
711 add_type_for_runtime (TREE_VALUE (allowed
));
713 /* We must emit the call to FAILURE here, so that if this function
714 throws a different exception, that it will be processed by the
717 around_label
= gen_label_rtx ();
718 emit_jump (around_label
);
720 emit_label (region
->label
);
721 expand_expr (failure
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
722 /* We must adjust the stack before we reach the AROUND_LABEL because
723 the call to FAILURE does not occur on all paths to the
725 do_pending_stack_adjust ();
727 emit_label (around_label
);
730 /* End an exception region for a must-not-throw filter. FAILURE is an
731 expression invoke if an uncaught exception propagates this far.
733 This is conceptually identical to expand_eh_region_end_allowed with
734 an empty allowed list (if you passed "std::terminate" instead of
735 "__cxa_call_unexpected"), but they are represented differently in
739 expand_eh_region_end_must_not_throw (tree failure
)
741 struct eh_region
*region
;
747 region
= expand_eh_region_end ();
748 region
->type
= ERT_MUST_NOT_THROW
;
749 region
->label
= gen_label_rtx ();
751 /* We must emit the call to FAILURE here, so that if this function
752 throws a different exception, that it will be processed by the
755 around_label
= gen_label_rtx ();
756 emit_jump (around_label
);
758 emit_label (region
->label
);
759 expand_expr (failure
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
761 emit_label (around_label
);
764 /* End an exception region for a throw. No handling goes on here,
765 but it's the easiest way for the front-end to indicate what type
769 expand_eh_region_end_throw (tree type
)
771 struct eh_region
*region
;
776 region
= expand_eh_region_end ();
777 region
->type
= ERT_THROW
;
778 region
->u
.throw.type
= type
;
781 /* End a fixup region. Within this region the cleanups for the immediately
782 enclosing region are _not_ run. This is used for goto cleanup to avoid
783 destroying an object twice.
785 This would be an extraordinarily simple prospect, were it not for the
786 fact that we don't actually know what the immediately enclosing region
787 is. This surprising fact is because expand_cleanups is currently
788 generating a sequence that it will insert somewhere else. We collect
789 the proper notion of "enclosing" in convert_from_eh_region_ranges. */
792 expand_eh_region_end_fixup (tree handler
)
794 struct eh_region
*fixup
;
799 fixup
= expand_eh_region_end ();
800 fixup
->type
= ERT_FIXUP
;
801 fixup
->u
.fixup
.cleanup_exp
= handler
;
804 /* Note that the current EH region (if any) may contain a throw, or a
805 call to a function which itself may contain a throw. */
808 note_eh_region_may_contain_throw (void)
810 struct eh_region
*region
;
812 region
= cfun
->eh
->cur_region
;
813 while (region
&& !region
->may_contain_throw
)
815 region
->may_contain_throw
= 1;
816 region
= region
->outer
;
820 /* Return an rtl expression for a pointer to the exception object
824 get_exception_pointer (struct function
*fun
)
826 rtx exc_ptr
= fun
->eh
->exc_ptr
;
827 if (fun
== cfun
&& ! exc_ptr
)
829 exc_ptr
= gen_reg_rtx (ptr_mode
);
830 fun
->eh
->exc_ptr
= exc_ptr
;
835 /* Return an rtl expression for the exception dispatch filter
839 get_exception_filter (struct function
*fun
)
841 rtx filter
= fun
->eh
->filter
;
842 if (fun
== cfun
&& ! filter
)
844 filter
= gen_reg_rtx (word_mode
);
845 fun
->eh
->filter
= filter
;
850 /* This section is for the exception handling specific optimization pass. */
852 /* Random access the exception region tree. It's just as simple to
853 collect the regions this way as in expand_eh_region_start, but
854 without having to realloc memory. */
857 collect_eh_region_array (void)
859 struct eh_region
**array
, *i
;
861 i
= cfun
->eh
->region_tree
;
865 array
= ggc_alloc_cleared ((cfun
->eh
->last_region_number
+ 1)
867 cfun
->eh
->region_array
= array
;
871 array
[i
->region_number
] = i
;
873 /* If there are sub-regions, process them. */
876 /* If there are peers, process them. */
877 else if (i
->next_peer
)
879 /* Otherwise, step back up the tree to the next peer. */
886 } while (i
->next_peer
== NULL
);
893 resolve_fixup_regions (void)
895 int i
, j
, n
= cfun
->eh
->last_region_number
;
897 for (i
= 1; i
<= n
; ++i
)
899 struct eh_region
*fixup
= cfun
->eh
->region_array
[i
];
900 struct eh_region
*cleanup
= 0;
902 if (! fixup
|| fixup
->type
!= ERT_FIXUP
)
905 for (j
= 1; j
<= n
; ++j
)
907 cleanup
= cfun
->eh
->region_array
[j
];
908 if (cleanup
&& cleanup
->type
== ERT_CLEANUP
909 && cleanup
->u
.cleanup
.exp
== fixup
->u
.fixup
.cleanup_exp
)
915 fixup
->u
.fixup
.real_region
= cleanup
->outer
;
919 /* Now that we've discovered what region actually encloses a fixup,
920 we can shuffle pointers and remove them from the tree. */
923 remove_fixup_regions (void)
927 struct eh_region
*fixup
;
929 /* Walk the insn chain and adjust the REG_EH_REGION numbers
930 for instructions referencing fixup regions. This is only
931 strictly necessary for fixup regions with no parent, but
932 doesn't hurt to do it for all regions. */
933 for (insn
= get_insns(); insn
; insn
= NEXT_INSN (insn
))
935 && (note
= find_reg_note (insn
, REG_EH_REGION
, NULL
))
936 && INTVAL (XEXP (note
, 0)) > 0
937 && (fixup
= cfun
->eh
->region_array
[INTVAL (XEXP (note
, 0))])
938 && fixup
->type
== ERT_FIXUP
)
940 if (fixup
->u
.fixup
.real_region
)
941 XEXP (note
, 0) = GEN_INT (fixup
->u
.fixup
.real_region
->region_number
);
943 remove_note (insn
, note
);
946 /* Remove the fixup regions from the tree. */
947 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
949 fixup
= cfun
->eh
->region_array
[i
];
953 /* Allow GC to maybe free some memory. */
954 if (fixup
->type
== ERT_CLEANUP
)
955 fixup
->u
.cleanup
.exp
= NULL_TREE
;
957 if (fixup
->type
!= ERT_FIXUP
)
962 struct eh_region
*parent
, *p
, **pp
;
964 parent
= fixup
->u
.fixup
.real_region
;
966 /* Fix up the children's parent pointers; find the end of
968 for (p
= fixup
->inner
; ; p
= p
->next_peer
)
975 /* In the tree of cleanups, only outer-inner ordering matters.
976 So link the children back in anywhere at the correct level. */
980 pp
= &cfun
->eh
->region_tree
;
986 remove_eh_handler (fixup
);
990 /* Remove all regions whose labels are not reachable from insns. */
993 remove_unreachable_regions (rtx insns
)
995 int i
, *uid_region_num
;
1000 uid_region_num
= xcalloc (get_max_uid (), sizeof(int));
1001 reachable
= xcalloc (cfun
->eh
->last_region_number
+ 1, sizeof(bool));
1003 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1005 r
= cfun
->eh
->region_array
[i
];
1006 if (!r
|| r
->region_number
!= i
)
1011 if (uid_region_num
[INSN_UID (r
->resume
)])
1013 uid_region_num
[INSN_UID (r
->resume
)] = i
;
1017 if (uid_region_num
[INSN_UID (r
->label
)])
1019 uid_region_num
[INSN_UID (r
->label
)] = i
;
1021 if (r
->type
== ERT_TRY
&& r
->u
.try.continue_label
)
1023 if (uid_region_num
[INSN_UID (r
->u
.try.continue_label
)])
1025 uid_region_num
[INSN_UID (r
->u
.try.continue_label
)] = i
;
1029 for (insn
= insns
; insn
; insn
= NEXT_INSN (insn
))
1030 reachable
[uid_region_num
[INSN_UID (insn
)]] = true;
1032 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1034 r
= cfun
->eh
->region_array
[i
];
1035 if (r
&& r
->region_number
== i
&& !reachable
[i
])
1037 /* Don't remove ERT_THROW regions if their outer region
1039 if (r
->type
== ERT_THROW
1041 && reachable
[r
->outer
->region_number
])
1044 remove_eh_handler (r
);
1049 free (uid_region_num
);
1052 /* Turn NOTE_INSN_EH_REGION notes into REG_EH_REGION notes for each
1053 can_throw instruction in the region. */
1056 convert_from_eh_region_ranges_1 (rtx
*pinsns
, int *orig_sp
, int cur
)
1061 for (insn
= *pinsns
; insn
; insn
= next
)
1063 next
= NEXT_INSN (insn
);
1064 if (GET_CODE (insn
) == NOTE
)
1066 int kind
= NOTE_LINE_NUMBER (insn
);
1067 if (kind
== NOTE_INSN_EH_REGION_BEG
1068 || kind
== NOTE_INSN_EH_REGION_END
)
1070 if (kind
== NOTE_INSN_EH_REGION_BEG
)
1072 struct eh_region
*r
;
1075 cur
= NOTE_EH_HANDLER (insn
);
1077 r
= cfun
->eh
->region_array
[cur
];
1078 if (r
->type
== ERT_FIXUP
)
1080 r
= r
->u
.fixup
.real_region
;
1081 cur
= r
? r
->region_number
: 0;
1083 else if (r
->type
== ERT_CATCH
)
1086 cur
= r
? r
->region_number
: 0;
1092 /* Removing the first insn of a CALL_PLACEHOLDER sequence
1093 requires extra care to adjust sequence start. */
1094 if (insn
== *pinsns
)
1100 else if (INSN_P (insn
))
1103 && ! find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
)
1104 /* Calls can always potentially throw exceptions, unless
1105 they have a REG_EH_REGION note with a value of 0 or less.
1106 Which should be the only possible kind so far. */
1107 && (GET_CODE (insn
) == CALL_INSN
1108 /* If we wanted exceptions for non-call insns, then
1109 any may_trap_p instruction could throw. */
1110 || (flag_non_call_exceptions
1111 && GET_CODE (PATTERN (insn
)) != CLOBBER
1112 && GET_CODE (PATTERN (insn
)) != USE
1113 && may_trap_p (PATTERN (insn
)))))
1115 REG_NOTES (insn
) = alloc_EXPR_LIST (REG_EH_REGION
, GEN_INT (cur
),
1119 if (GET_CODE (insn
) == CALL_INSN
1120 && GET_CODE (PATTERN (insn
)) == CALL_PLACEHOLDER
)
1122 convert_from_eh_region_ranges_1 (&XEXP (PATTERN (insn
), 0),
1124 convert_from_eh_region_ranges_1 (&XEXP (PATTERN (insn
), 1),
1126 convert_from_eh_region_ranges_1 (&XEXP (PATTERN (insn
), 2),
1137 convert_from_eh_region_ranges (void)
1142 collect_eh_region_array ();
1143 resolve_fixup_regions ();
1145 stack
= xmalloc (sizeof (int) * (cfun
->eh
->last_region_number
+ 1));
1146 insns
= get_insns ();
1147 convert_from_eh_region_ranges_1 (&insns
, stack
, 0);
1150 remove_fixup_regions ();
1151 remove_unreachable_regions (insns
);
1155 add_ehl_entry (rtx label
, struct eh_region
*region
)
1157 struct ehl_map_entry
**slot
, *entry
;
1159 LABEL_PRESERVE_P (label
) = 1;
1161 entry
= ggc_alloc (sizeof (*entry
));
1162 entry
->label
= label
;
1163 entry
->region
= region
;
1165 slot
= (struct ehl_map_entry
**)
1166 htab_find_slot (cfun
->eh
->exception_handler_label_map
, entry
, INSERT
);
1168 /* Before landing pad creation, each exception handler has its own
1169 label. After landing pad creation, the exception handlers may
1170 share landing pads. This is ok, since maybe_remove_eh_handler
1171 only requires the 1-1 mapping before landing pad creation. */
1172 if (*slot
&& !cfun
->eh
->built_landing_pads
)
1179 find_exception_handler_labels (void)
1183 if (cfun
->eh
->exception_handler_label_map
)
1184 htab_empty (cfun
->eh
->exception_handler_label_map
);
1187 /* ??? The expansion factor here (3/2) must be greater than the htab
1188 occupancy factor (4/3) to avoid unnecessary resizing. */
1189 cfun
->eh
->exception_handler_label_map
1190 = htab_create_ggc (cfun
->eh
->last_region_number
* 3 / 2,
1191 ehl_hash
, ehl_eq
, NULL
);
1194 if (cfun
->eh
->region_tree
== NULL
)
1197 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1199 struct eh_region
*region
= cfun
->eh
->region_array
[i
];
1202 if (! region
|| region
->region_number
!= i
)
1204 if (cfun
->eh
->built_landing_pads
)
1205 lab
= region
->landing_pad
;
1207 lab
= region
->label
;
1210 add_ehl_entry (lab
, region
);
1213 /* For sjlj exceptions, need the return label to remain live until
1214 after landing pad generation. */
1215 if (USING_SJLJ_EXCEPTIONS
&& ! cfun
->eh
->built_landing_pads
)
1216 add_ehl_entry (return_label
, NULL
);
1220 current_function_has_exception_handlers (void)
1224 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1226 struct eh_region
*region
= cfun
->eh
->region_array
[i
];
1228 if (! region
|| region
->region_number
!= i
)
1230 if (region
->type
!= ERT_THROW
)
1237 static struct eh_region
*
1238 duplicate_eh_region_1 (struct eh_region
*o
, struct inline_remap
*map
)
1240 struct eh_region
*n
= ggc_alloc_cleared (sizeof (struct eh_region
));
1242 n
->region_number
= o
->region_number
+ cfun
->eh
->last_region_number
;
1248 case ERT_MUST_NOT_THROW
:
1252 if (o
->u
.try.continue_label
)
1253 n
->u
.try.continue_label
1254 = get_label_from_map (map
,
1255 CODE_LABEL_NUMBER (o
->u
.try.continue_label
));
1259 n
->u
.catch.type_list
= o
->u
.catch.type_list
;
1262 case ERT_ALLOWED_EXCEPTIONS
:
1263 n
->u
.allowed
.type_list
= o
->u
.allowed
.type_list
;
1267 n
->u
.throw.type
= o
->u
.throw.type
;
1274 n
->label
= get_label_from_map (map
, CODE_LABEL_NUMBER (o
->label
));
1277 n
->resume
= map
->insn_map
[INSN_UID (o
->resume
)];
1278 if (n
->resume
== NULL
)
1286 duplicate_eh_region_2 (struct eh_region
*o
, struct eh_region
**n_array
)
1288 struct eh_region
*n
= n_array
[o
->region_number
];
1293 n
->u
.try.catch = n_array
[o
->u
.try.catch->region_number
];
1294 n
->u
.try.last_catch
= n_array
[o
->u
.try.last_catch
->region_number
];
1298 if (o
->u
.catch.next_catch
)
1299 n
->u
.catch.next_catch
= n_array
[o
->u
.catch.next_catch
->region_number
];
1300 if (o
->u
.catch.prev_catch
)
1301 n
->u
.catch.prev_catch
= n_array
[o
->u
.catch.prev_catch
->region_number
];
1309 n
->outer
= n_array
[o
->outer
->region_number
];
1311 n
->inner
= n_array
[o
->inner
->region_number
];
1313 n
->next_peer
= n_array
[o
->next_peer
->region_number
];
1317 duplicate_eh_regions (struct function
*ifun
, struct inline_remap
*map
)
1319 int ifun_last_region_number
= ifun
->eh
->last_region_number
;
1320 struct eh_region
**n_array
, *root
, *cur
;
1323 if (ifun_last_region_number
== 0)
1326 n_array
= xcalloc (ifun_last_region_number
+ 1, sizeof (*n_array
));
1328 for (i
= 1; i
<= ifun_last_region_number
; ++i
)
1330 cur
= ifun
->eh
->region_array
[i
];
1331 if (!cur
|| cur
->region_number
!= i
)
1333 n_array
[i
] = duplicate_eh_region_1 (cur
, map
);
1335 for (i
= 1; i
<= ifun_last_region_number
; ++i
)
1337 cur
= ifun
->eh
->region_array
[i
];
1338 if (!cur
|| cur
->region_number
!= i
)
1340 duplicate_eh_region_2 (cur
, n_array
);
1343 root
= n_array
[ifun
->eh
->region_tree
->region_number
];
1344 cur
= cfun
->eh
->cur_region
;
1347 struct eh_region
*p
= cur
->inner
;
1350 while (p
->next_peer
)
1352 p
->next_peer
= root
;
1357 for (i
= 1; i
<= ifun_last_region_number
; ++i
)
1358 if (n_array
[i
] && n_array
[i
]->outer
== NULL
)
1359 n_array
[i
]->outer
= cur
;
1363 struct eh_region
*p
= cfun
->eh
->region_tree
;
1366 while (p
->next_peer
)
1368 p
->next_peer
= root
;
1371 cfun
->eh
->region_tree
= root
;
1376 i
= cfun
->eh
->last_region_number
;
1377 cfun
->eh
->last_region_number
= i
+ ifun_last_region_number
;
1383 t2r_eq (const void *pentry
, const void *pdata
)
1385 tree entry
= (tree
) pentry
;
1386 tree data
= (tree
) pdata
;
1388 return TREE_PURPOSE (entry
) == data
;
1392 t2r_hash (const void *pentry
)
1394 tree entry
= (tree
) pentry
;
1395 return TYPE_HASH (TREE_PURPOSE (entry
));
1399 add_type_for_runtime (tree type
)
1403 slot
= (tree
*) htab_find_slot_with_hash (type_to_runtime_map
, type
,
1404 TYPE_HASH (type
), INSERT
);
1407 tree runtime
= (*lang_eh_runtime_type
) (type
);
1408 *slot
= tree_cons (type
, runtime
, NULL_TREE
);
1413 lookup_type_for_runtime (tree type
)
1417 slot
= (tree
*) htab_find_slot_with_hash (type_to_runtime_map
, type
,
1418 TYPE_HASH (type
), NO_INSERT
);
1420 /* We should have always inserted the data earlier. */
1421 return TREE_VALUE (*slot
);
1425 /* Represent an entry in @TTypes for either catch actions
1426 or exception filter actions. */
1427 struct ttypes_filter
GTY(())
1433 /* Compare ENTRY (a ttypes_filter entry in the hash table) with DATA
1434 (a tree) for a @TTypes type node we are thinking about adding. */
1437 ttypes_filter_eq (const void *pentry
, const void *pdata
)
1439 const struct ttypes_filter
*entry
= (const struct ttypes_filter
*) pentry
;
1440 tree data
= (tree
) pdata
;
1442 return entry
->t
== data
;
1446 ttypes_filter_hash (const void *pentry
)
1448 const struct ttypes_filter
*entry
= (const struct ttypes_filter
*) pentry
;
1449 return TYPE_HASH (entry
->t
);
1452 /* Compare ENTRY with DATA (both struct ttypes_filter) for a @TTypes
1453 exception specification list we are thinking about adding. */
1454 /* ??? Currently we use the type lists in the order given. Someone
1455 should put these in some canonical order. */
1458 ehspec_filter_eq (const void *pentry
, const void *pdata
)
1460 const struct ttypes_filter
*entry
= (const struct ttypes_filter
*) pentry
;
1461 const struct ttypes_filter
*data
= (const struct ttypes_filter
*) pdata
;
1463 return type_list_equal (entry
->t
, data
->t
);
1466 /* Hash function for exception specification lists. */
1469 ehspec_filter_hash (const void *pentry
)
1471 const struct ttypes_filter
*entry
= (const struct ttypes_filter
*) pentry
;
1475 for (list
= entry
->t
; list
; list
= TREE_CHAIN (list
))
1476 h
= (h
<< 5) + (h
>> 27) + TYPE_HASH (TREE_VALUE (list
));
1480 /* Add TYPE to cfun->eh->ttype_data, using TYPES_HASH to speed
1481 up the search. Return the filter value to be used. */
1484 add_ttypes_entry (htab_t ttypes_hash
, tree type
)
1486 struct ttypes_filter
**slot
, *n
;
1488 slot
= (struct ttypes_filter
**)
1489 htab_find_slot_with_hash (ttypes_hash
, type
, TYPE_HASH (type
), INSERT
);
1491 if ((n
= *slot
) == NULL
)
1493 /* Filter value is a 1 based table index. */
1495 n
= xmalloc (sizeof (*n
));
1497 n
->filter
= VARRAY_ACTIVE_SIZE (cfun
->eh
->ttype_data
) + 1;
1500 VARRAY_PUSH_TREE (cfun
->eh
->ttype_data
, type
);
1506 /* Add LIST to cfun->eh->ehspec_data, using EHSPEC_HASH and TYPES_HASH
1507 to speed up the search. Return the filter value to be used. */
1510 add_ehspec_entry (htab_t ehspec_hash
, htab_t ttypes_hash
, tree list
)
1512 struct ttypes_filter
**slot
, *n
;
1513 struct ttypes_filter dummy
;
1516 slot
= (struct ttypes_filter
**)
1517 htab_find_slot (ehspec_hash
, &dummy
, INSERT
);
1519 if ((n
= *slot
) == NULL
)
1521 /* Filter value is a -1 based byte index into a uleb128 buffer. */
1523 n
= xmalloc (sizeof (*n
));
1525 n
->filter
= -(VARRAY_ACTIVE_SIZE (cfun
->eh
->ehspec_data
) + 1);
1528 /* Look up each type in the list and encode its filter
1529 value as a uleb128. Terminate the list with 0. */
1530 for (; list
; list
= TREE_CHAIN (list
))
1531 push_uleb128 (&cfun
->eh
->ehspec_data
,
1532 add_ttypes_entry (ttypes_hash
, TREE_VALUE (list
)));
1533 VARRAY_PUSH_UCHAR (cfun
->eh
->ehspec_data
, 0);
1539 /* Generate the action filter values to be used for CATCH and
1540 ALLOWED_EXCEPTIONS regions. When using dwarf2 exception regions,
1541 we use lots of landing pads, and so every type or list can share
1542 the same filter value, which saves table space. */
1545 assign_filter_values (void)
1548 htab_t ttypes
, ehspec
;
1550 VARRAY_TREE_INIT (cfun
->eh
->ttype_data
, 16, "ttype_data");
1551 VARRAY_UCHAR_INIT (cfun
->eh
->ehspec_data
, 64, "ehspec_data");
1553 ttypes
= htab_create (31, ttypes_filter_hash
, ttypes_filter_eq
, free
);
1554 ehspec
= htab_create (31, ehspec_filter_hash
, ehspec_filter_eq
, free
);
1556 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1558 struct eh_region
*r
= cfun
->eh
->region_array
[i
];
1560 /* Mind we don't process a region more than once. */
1561 if (!r
|| r
->region_number
!= i
)
1567 /* Whatever type_list is (NULL or true list), we build a list
1568 of filters for the region. */
1569 r
->u
.catch.filter_list
= NULL_TREE
;
1571 if (r
->u
.catch.type_list
!= NULL
)
1573 /* Get a filter value for each of the types caught and store
1574 them in the region's dedicated list. */
1575 tree tp_node
= r
->u
.catch.type_list
;
1577 for (;tp_node
; tp_node
= TREE_CHAIN (tp_node
))
1579 int flt
= add_ttypes_entry (ttypes
, TREE_VALUE (tp_node
));
1580 tree flt_node
= build_int_2 (flt
, 0);
1582 r
->u
.catch.filter_list
1583 = tree_cons (NULL_TREE
, flt_node
, r
->u
.catch.filter_list
);
1588 /* Get a filter value for the NULL list also since it will need
1589 an action record anyway. */
1590 int flt
= add_ttypes_entry (ttypes
, NULL
);
1591 tree flt_node
= build_int_2 (flt
, 0);
1593 r
->u
.catch.filter_list
1594 = tree_cons (NULL_TREE
, flt_node
, r
->u
.catch.filter_list
);
1599 case ERT_ALLOWED_EXCEPTIONS
:
1601 = add_ehspec_entry (ehspec
, ttypes
, r
->u
.allowed
.type_list
);
1609 htab_delete (ttypes
);
1610 htab_delete (ehspec
);
1613 /* Generate the code to actually handle exceptions, which will follow the
1617 build_post_landing_pads (void)
1621 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1623 struct eh_region
*region
= cfun
->eh
->region_array
[i
];
1626 /* Mind we don't process a region more than once. */
1627 if (!region
|| region
->region_number
!= i
)
1630 switch (region
->type
)
1633 /* ??? Collect the set of all non-overlapping catch handlers
1634 all the way up the chain until blocked by a cleanup. */
1635 /* ??? Outer try regions can share landing pads with inner
1636 try regions if the types are completely non-overlapping,
1637 and there are no intervening cleanups. */
1639 region
->post_landing_pad
= gen_label_rtx ();
1643 emit_label (region
->post_landing_pad
);
1645 /* ??? It is mighty inconvenient to call back into the
1646 switch statement generation code in expand_end_case.
1647 Rapid prototyping sez a sequence of ifs. */
1649 struct eh_region
*c
;
1650 for (c
= region
->u
.try.catch; c
; c
= c
->u
.catch.next_catch
)
1652 if (c
->u
.catch.type_list
== NULL
)
1653 emit_jump (c
->label
);
1656 /* Need for one cmp/jump per type caught. Each type
1657 list entry has a matching entry in the filter list
1658 (see assign_filter_values). */
1659 tree tp_node
= c
->u
.catch.type_list
;
1660 tree flt_node
= c
->u
.catch.filter_list
;
1664 emit_cmp_and_jump_insns
1666 GEN_INT (tree_low_cst (TREE_VALUE (flt_node
), 0)),
1667 EQ
, NULL_RTX
, word_mode
, 0, c
->label
);
1669 tp_node
= TREE_CHAIN (tp_node
);
1670 flt_node
= TREE_CHAIN (flt_node
);
1676 /* We delay the generation of the _Unwind_Resume until we generate
1677 landing pads. We emit a marker here so as to get good control
1678 flow data in the meantime. */
1680 = emit_jump_insn (gen_rtx_RESX (VOIDmode
, region
->region_number
));
1686 emit_insn_before (seq
, region
->u
.try.catch->label
);
1689 case ERT_ALLOWED_EXCEPTIONS
:
1690 region
->post_landing_pad
= gen_label_rtx ();
1694 emit_label (region
->post_landing_pad
);
1696 emit_cmp_and_jump_insns (cfun
->eh
->filter
,
1697 GEN_INT (region
->u
.allowed
.filter
),
1698 EQ
, NULL_RTX
, word_mode
, 0, region
->label
);
1700 /* We delay the generation of the _Unwind_Resume until we generate
1701 landing pads. We emit a marker here so as to get good control
1702 flow data in the meantime. */
1704 = emit_jump_insn (gen_rtx_RESX (VOIDmode
, region
->region_number
));
1710 emit_insn_before (seq
, region
->label
);
1714 case ERT_MUST_NOT_THROW
:
1715 region
->post_landing_pad
= region
->label
;
1720 /* Nothing to do. */
1729 /* Replace RESX patterns with jumps to the next handler if any, or calls to
1730 _Unwind_Resume otherwise. */
1733 connect_post_landing_pads (void)
1737 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1739 struct eh_region
*region
= cfun
->eh
->region_array
[i
];
1740 struct eh_region
*outer
;
1743 /* Mind we don't process a region more than once. */
1744 if (!region
|| region
->region_number
!= i
)
1747 /* If there is no RESX, or it has been deleted by flow, there's
1748 nothing to fix up. */
1749 if (! region
->resume
|| INSN_DELETED_P (region
->resume
))
1752 /* Search for another landing pad in this function. */
1753 for (outer
= region
->outer
; outer
; outer
= outer
->outer
)
1754 if (outer
->post_landing_pad
)
1760 emit_jump (outer
->post_landing_pad
);
1762 emit_library_call (unwind_resume_libfunc
, LCT_THROW
,
1763 VOIDmode
, 1, cfun
->eh
->exc_ptr
, ptr_mode
);
1767 emit_insn_before (seq
, region
->resume
);
1768 delete_insn (region
->resume
);
1774 dw2_build_landing_pads (void)
1779 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1781 struct eh_region
*region
= cfun
->eh
->region_array
[i
];
1783 bool clobbers_hard_regs
= false;
1785 /* Mind we don't process a region more than once. */
1786 if (!region
|| region
->region_number
!= i
)
1789 if (region
->type
!= ERT_CLEANUP
1790 && region
->type
!= ERT_TRY
1791 && region
->type
!= ERT_ALLOWED_EXCEPTIONS
)
1796 region
->landing_pad
= gen_label_rtx ();
1797 emit_label (region
->landing_pad
);
1799 #ifdef HAVE_exception_receiver
1800 if (HAVE_exception_receiver
)
1801 emit_insn (gen_exception_receiver ());
1804 #ifdef HAVE_nonlocal_goto_receiver
1805 if (HAVE_nonlocal_goto_receiver
)
1806 emit_insn (gen_nonlocal_goto_receiver ());
1811 /* If the eh_return data registers are call-saved, then we
1812 won't have considered them clobbered from the call that
1813 threw. Kill them now. */
1816 unsigned r
= EH_RETURN_DATA_REGNO (j
);
1817 if (r
== INVALID_REGNUM
)
1819 if (! call_used_regs
[r
])
1821 emit_insn (gen_rtx_CLOBBER (VOIDmode
, gen_rtx_REG (Pmode
, r
)));
1822 clobbers_hard_regs
= true;
1826 if (clobbers_hard_regs
)
1828 /* @@@ This is a kludge. Not all machine descriptions define a
1829 blockage insn, but we must not allow the code we just generated
1830 to be reordered by scheduling. So emit an ASM_INPUT to act as
1832 emit_insn (gen_rtx_ASM_INPUT (VOIDmode
, ""));
1835 emit_move_insn (cfun
->eh
->exc_ptr
,
1836 gen_rtx_REG (ptr_mode
, EH_RETURN_DATA_REGNO (0)));
1837 emit_move_insn (cfun
->eh
->filter
,
1838 gen_rtx_REG (word_mode
, EH_RETURN_DATA_REGNO (1)));
1843 emit_insn_before (seq
, region
->post_landing_pad
);
1850 int directly_reachable
;
1853 int call_site_index
;
1857 sjlj_find_directly_reachable_regions (struct sjlj_lp_info
*lp_info
)
1860 bool found_one
= false;
1862 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
1864 struct eh_region
*region
;
1865 enum reachable_code rc
;
1869 if (! INSN_P (insn
))
1872 note
= find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
);
1873 if (!note
|| INTVAL (XEXP (note
, 0)) <= 0)
1876 region
= cfun
->eh
->region_array
[INTVAL (XEXP (note
, 0))];
1878 type_thrown
= NULL_TREE
;
1879 if (region
->type
== ERT_THROW
)
1881 type_thrown
= region
->u
.throw.type
;
1882 region
= region
->outer
;
1885 /* Find the first containing region that might handle the exception.
1886 That's the landing pad to which we will transfer control. */
1887 rc
= RNL_NOT_CAUGHT
;
1888 for (; region
; region
= region
->outer
)
1890 rc
= reachable_next_level (region
, type_thrown
, 0);
1891 if (rc
!= RNL_NOT_CAUGHT
)
1894 if (rc
== RNL_MAYBE_CAUGHT
|| rc
== RNL_CAUGHT
)
1896 lp_info
[region
->region_number
].directly_reachable
= 1;
1905 sjlj_assign_call_site_values (rtx dispatch_label
, struct sjlj_lp_info
*lp_info
)
1910 /* First task: build the action table. */
1912 VARRAY_UCHAR_INIT (cfun
->eh
->action_record_data
, 64, "action_record_data");
1913 ar_hash
= htab_create (31, action_record_hash
, action_record_eq
, free
);
1915 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1916 if (lp_info
[i
].directly_reachable
)
1918 struct eh_region
*r
= cfun
->eh
->region_array
[i
];
1919 r
->landing_pad
= dispatch_label
;
1920 lp_info
[i
].action_index
= collect_one_action_chain (ar_hash
, r
);
1921 if (lp_info
[i
].action_index
!= -1)
1922 cfun
->uses_eh_lsda
= 1;
1925 htab_delete (ar_hash
);
1927 /* Next: assign dispatch values. In dwarf2 terms, this would be the
1928 landing pad label for the region. For sjlj though, there is one
1929 common landing pad from which we dispatch to the post-landing pads.
1931 A region receives a dispatch index if it is directly reachable
1932 and requires in-function processing. Regions that share post-landing
1933 pads may share dispatch indices. */
1934 /* ??? Post-landing pad sharing doesn't actually happen at the moment
1935 (see build_post_landing_pads) so we don't bother checking for it. */
1938 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1939 if (lp_info
[i
].directly_reachable
)
1940 lp_info
[i
].dispatch_index
= index
++;
1942 /* Finally: assign call-site values. If dwarf2 terms, this would be
1943 the region number assigned by convert_to_eh_region_ranges, but
1944 handles no-action and must-not-throw differently. */
1947 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1948 if (lp_info
[i
].directly_reachable
)
1950 int action
= lp_info
[i
].action_index
;
1952 /* Map must-not-throw to otherwise unused call-site index 0. */
1955 /* Map no-action to otherwise unused call-site index -1. */
1956 else if (action
== -1)
1958 /* Otherwise, look it up in the table. */
1960 index
= add_call_site (GEN_INT (lp_info
[i
].dispatch_index
), action
);
1962 lp_info
[i
].call_site_index
= index
;
1967 sjlj_mark_call_sites (struct sjlj_lp_info
*lp_info
)
1969 int last_call_site
= -2;
1972 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
1974 struct eh_region
*region
;
1976 rtx note
, before
, p
;
1978 /* Reset value tracking at extended basic block boundaries. */
1979 if (GET_CODE (insn
) == CODE_LABEL
)
1980 last_call_site
= -2;
1982 if (! INSN_P (insn
))
1985 note
= find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
);
1988 /* Calls (and trapping insns) without notes are outside any
1989 exception handling region in this function. Mark them as
1991 if (GET_CODE (insn
) == CALL_INSN
1992 || (flag_non_call_exceptions
1993 && may_trap_p (PATTERN (insn
))))
1994 this_call_site
= -1;
2000 /* Calls that are known to not throw need not be marked. */
2001 if (INTVAL (XEXP (note
, 0)) <= 0)
2004 region
= cfun
->eh
->region_array
[INTVAL (XEXP (note
, 0))];
2005 this_call_site
= lp_info
[region
->region_number
].call_site_index
;
2008 if (this_call_site
== last_call_site
)
2011 /* Don't separate a call from it's argument loads. */
2013 if (GET_CODE (insn
) == CALL_INSN
)
2014 before
= find_first_parameter_load (insn
, NULL_RTX
);
2017 mem
= adjust_address (cfun
->eh
->sjlj_fc
, TYPE_MODE (integer_type_node
),
2018 sjlj_fc_call_site_ofs
);
2019 emit_move_insn (mem
, GEN_INT (this_call_site
));
2023 emit_insn_before (p
, before
);
2024 last_call_site
= this_call_site
;
2028 /* Construct the SjLj_Function_Context. */
2031 sjlj_emit_function_enter (rtx dispatch_label
)
2033 rtx fn_begin
, fc
, mem
, seq
;
2035 fc
= cfun
->eh
->sjlj_fc
;
2039 /* We're storing this libcall's address into memory instead of
2040 calling it directly. Thus, we must call assemble_external_libcall
2041 here, as we can not depend on emit_library_call to do it for us. */
2042 assemble_external_libcall (eh_personality_libfunc
);
2043 mem
= adjust_address (fc
, Pmode
, sjlj_fc_personality_ofs
);
2044 emit_move_insn (mem
, eh_personality_libfunc
);
2046 mem
= adjust_address (fc
, Pmode
, sjlj_fc_lsda_ofs
);
2047 if (cfun
->uses_eh_lsda
)
2052 ASM_GENERATE_INTERNAL_LABEL (buf
, "LLSDA", current_function_funcdef_no
);
2053 sym
= gen_rtx_SYMBOL_REF (Pmode
, ggc_strdup (buf
));
2054 SYMBOL_REF_FLAGS (sym
) = SYMBOL_FLAG_LOCAL
;
2055 emit_move_insn (mem
, sym
);
2058 emit_move_insn (mem
, const0_rtx
);
2060 #ifdef DONT_USE_BUILTIN_SETJMP
2063 x
= emit_library_call_value (setjmp_libfunc
, NULL_RTX
, LCT_RETURNS_TWICE
,
2064 TYPE_MODE (integer_type_node
), 1,
2065 plus_constant (XEXP (fc
, 0),
2066 sjlj_fc_jbuf_ofs
), Pmode
);
2068 note
= emit_note (NOTE_INSN_EXPECTED_VALUE
);
2069 NOTE_EXPECTED_VALUE (note
) = gen_rtx_EQ (VOIDmode
, x
, const0_rtx
);
2071 emit_cmp_and_jump_insns (x
, const0_rtx
, NE
, 0,
2072 TYPE_MODE (integer_type_node
), 0, dispatch_label
);
2075 expand_builtin_setjmp_setup (plus_constant (XEXP (fc
, 0), sjlj_fc_jbuf_ofs
),
2079 emit_library_call (unwind_sjlj_register_libfunc
, LCT_NORMAL
, VOIDmode
,
2080 1, XEXP (fc
, 0), Pmode
);
2085 /* ??? Instead of doing this at the beginning of the function,
2086 do this in a block that is at loop level 0 and dominates all
2087 can_throw_internal instructions. */
2089 for (fn_begin
= get_insns (); ; fn_begin
= NEXT_INSN (fn_begin
))
2090 if (GET_CODE (fn_begin
) == NOTE
2091 && NOTE_LINE_NUMBER (fn_begin
) == NOTE_INSN_FUNCTION_BEG
)
2093 emit_insn_after (seq
, fn_begin
);
2096 /* Call back from expand_function_end to know where we should put
2097 the call to unwind_sjlj_unregister_libfunc if needed. */
2100 sjlj_emit_function_exit_after (rtx after
)
2102 cfun
->eh
->sjlj_exit_after
= after
;
2106 sjlj_emit_function_exit (void)
2112 emit_library_call (unwind_sjlj_unregister_libfunc
, LCT_NORMAL
, VOIDmode
,
2113 1, XEXP (cfun
->eh
->sjlj_fc
, 0), Pmode
);
2118 /* ??? Really this can be done in any block at loop level 0 that
2119 post-dominates all can_throw_internal instructions. This is
2120 the last possible moment. */
2122 emit_insn_after (seq
, cfun
->eh
->sjlj_exit_after
);
2126 sjlj_emit_dispatch_table (rtx dispatch_label
, struct sjlj_lp_info
*lp_info
)
2128 int i
, first_reachable
;
2129 rtx mem
, dispatch
, seq
, fc
;
2131 fc
= cfun
->eh
->sjlj_fc
;
2135 emit_label (dispatch_label
);
2137 #ifndef DONT_USE_BUILTIN_SETJMP
2138 expand_builtin_setjmp_receiver (dispatch_label
);
2141 /* Load up dispatch index, exc_ptr and filter values from the
2142 function context. */
2143 mem
= adjust_address (fc
, TYPE_MODE (integer_type_node
),
2144 sjlj_fc_call_site_ofs
);
2145 dispatch
= copy_to_reg (mem
);
2147 mem
= adjust_address (fc
, word_mode
, sjlj_fc_data_ofs
);
2148 if (word_mode
!= ptr_mode
)
2150 #ifdef POINTERS_EXTEND_UNSIGNED
2151 mem
= convert_memory_address (ptr_mode
, mem
);
2153 mem
= convert_to_mode (ptr_mode
, mem
, 0);
2156 emit_move_insn (cfun
->eh
->exc_ptr
, mem
);
2158 mem
= adjust_address (fc
, word_mode
, sjlj_fc_data_ofs
+ UNITS_PER_WORD
);
2159 emit_move_insn (cfun
->eh
->filter
, mem
);
2161 /* Jump to one of the directly reachable regions. */
2162 /* ??? This really ought to be using a switch statement. */
2164 first_reachable
= 0;
2165 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
2167 if (! lp_info
[i
].directly_reachable
)
2170 if (! first_reachable
)
2172 first_reachable
= i
;
2176 emit_cmp_and_jump_insns (dispatch
, GEN_INT (lp_info
[i
].dispatch_index
),
2177 EQ
, NULL_RTX
, TYPE_MODE (integer_type_node
), 0,
2178 cfun
->eh
->region_array
[i
]->post_landing_pad
);
2184 emit_insn_before (seq
, (cfun
->eh
->region_array
[first_reachable
]
2185 ->post_landing_pad
));
2189 sjlj_build_landing_pads (void)
2191 struct sjlj_lp_info
*lp_info
;
2193 lp_info
= xcalloc (cfun
->eh
->last_region_number
+ 1,
2194 sizeof (struct sjlj_lp_info
));
2196 if (sjlj_find_directly_reachable_regions (lp_info
))
2198 rtx dispatch_label
= gen_label_rtx ();
2201 = assign_stack_local (TYPE_MODE (sjlj_fc_type_node
),
2202 int_size_in_bytes (sjlj_fc_type_node
),
2203 TYPE_ALIGN (sjlj_fc_type_node
));
2205 sjlj_assign_call_site_values (dispatch_label
, lp_info
);
2206 sjlj_mark_call_sites (lp_info
);
2208 sjlj_emit_function_enter (dispatch_label
);
2209 sjlj_emit_dispatch_table (dispatch_label
, lp_info
);
2210 sjlj_emit_function_exit ();
2217 finish_eh_generation (void)
2219 /* Nothing to do if no regions created. */
2220 if (cfun
->eh
->region_tree
== NULL
)
2223 /* The object here is to provide find_basic_blocks with detailed
2224 information (via reachable_handlers) on how exception control
2225 flows within the function. In this first pass, we can include
2226 type information garnered from ERT_THROW and ERT_ALLOWED_EXCEPTIONS
2227 regions, and hope that it will be useful in deleting unreachable
2228 handlers. Subsequently, we will generate landing pads which will
2229 connect many of the handlers, and then type information will not
2230 be effective. Still, this is a win over previous implementations. */
2232 cleanup_cfg (CLEANUP_PRE_LOOP
| CLEANUP_NO_INSN_DEL
);
2234 /* These registers are used by the landing pads. Make sure they
2235 have been generated. */
2236 get_exception_pointer (cfun
);
2237 get_exception_filter (cfun
);
2239 /* Construct the landing pads. */
2241 assign_filter_values ();
2242 build_post_landing_pads ();
2243 connect_post_landing_pads ();
2244 if (USING_SJLJ_EXCEPTIONS
)
2245 sjlj_build_landing_pads ();
2247 dw2_build_landing_pads ();
2249 cfun
->eh
->built_landing_pads
= 1;
2251 /* We've totally changed the CFG. Start over. */
2252 find_exception_handler_labels ();
2253 rebuild_jump_labels (get_insns ());
2254 find_basic_blocks (get_insns (), max_reg_num (), 0);
2255 cleanup_cfg (CLEANUP_PRE_LOOP
| CLEANUP_NO_INSN_DEL
);
2259 ehl_hash (const void *pentry
)
2261 struct ehl_map_entry
*entry
= (struct ehl_map_entry
*) pentry
;
2263 /* 2^32 * ((sqrt(5) - 1) / 2) */
2264 const hashval_t scaled_golden_ratio
= 0x9e3779b9;
2265 return CODE_LABEL_NUMBER (entry
->label
) * scaled_golden_ratio
;
2269 ehl_eq (const void *pentry
, const void *pdata
)
2271 struct ehl_map_entry
*entry
= (struct ehl_map_entry
*) pentry
;
2272 struct ehl_map_entry
*data
= (struct ehl_map_entry
*) pdata
;
2274 return entry
->label
== data
->label
;
2277 /* This section handles removing dead code for flow. */
2279 /* Remove LABEL from exception_handler_label_map. */
2282 remove_exception_handler_label (rtx label
)
2284 struct ehl_map_entry
**slot
, tmp
;
2286 /* If exception_handler_label_map was not built yet,
2287 there is nothing to do. */
2288 if (cfun
->eh
->exception_handler_label_map
== NULL
)
2292 slot
= (struct ehl_map_entry
**)
2293 htab_find_slot (cfun
->eh
->exception_handler_label_map
, &tmp
, NO_INSERT
);
2297 htab_clear_slot (cfun
->eh
->exception_handler_label_map
, (void **) slot
);
2300 /* Splice REGION from the region tree etc. */
2303 remove_eh_handler (struct eh_region
*region
)
2305 struct eh_region
**pp
, **pp_start
, *p
, *outer
, *inner
;
2308 /* For the benefit of efficiently handling REG_EH_REGION notes,
2309 replace this region in the region array with its containing
2310 region. Note that previous region deletions may result in
2311 multiple copies of this region in the array, so we have a
2312 list of alternate numbers by which we are known. */
2314 outer
= region
->outer
;
2315 cfun
->eh
->region_array
[region
->region_number
] = outer
;
2319 EXECUTE_IF_SET_IN_BITMAP (region
->aka
, 0, i
,
2320 { cfun
->eh
->region_array
[i
] = outer
; });
2326 outer
->aka
= BITMAP_GGC_ALLOC ();
2328 bitmap_a_or_b (outer
->aka
, outer
->aka
, region
->aka
);
2329 bitmap_set_bit (outer
->aka
, region
->region_number
);
2332 if (cfun
->eh
->built_landing_pads
)
2333 lab
= region
->landing_pad
;
2335 lab
= region
->label
;
2337 remove_exception_handler_label (lab
);
2340 pp_start
= &outer
->inner
;
2342 pp_start
= &cfun
->eh
->region_tree
;
2343 for (pp
= pp_start
, p
= *pp
; p
!= region
; pp
= &p
->next_peer
, p
= *pp
)
2345 *pp
= region
->next_peer
;
2347 inner
= region
->inner
;
2350 for (p
= inner
; p
->next_peer
; p
= p
->next_peer
)
2354 p
->next_peer
= *pp_start
;
2358 if (region
->type
== ERT_CATCH
)
2360 struct eh_region
*try, *next
, *prev
;
2362 for (try = region
->next_peer
;
2363 try->type
== ERT_CATCH
;
2364 try = try->next_peer
)
2366 if (try->type
!= ERT_TRY
)
2369 next
= region
->u
.catch.next_catch
;
2370 prev
= region
->u
.catch.prev_catch
;
2373 next
->u
.catch.prev_catch
= prev
;
2375 try->u
.try.last_catch
= prev
;
2377 prev
->u
.catch.next_catch
= next
;
2380 try->u
.try.catch = next
;
2382 remove_eh_handler (try);
2387 /* LABEL heads a basic block that is about to be deleted. If this
2388 label corresponds to an exception region, we may be able to
2389 delete the region. */
2392 maybe_remove_eh_handler (rtx label
)
2394 struct ehl_map_entry
**slot
, tmp
;
2395 struct eh_region
*region
;
2397 /* ??? After generating landing pads, it's not so simple to determine
2398 if the region data is completely unused. One must examine the
2399 landing pad and the post landing pad, and whether an inner try block
2400 is referencing the catch handlers directly. */
2401 if (cfun
->eh
->built_landing_pads
)
2405 slot
= (struct ehl_map_entry
**)
2406 htab_find_slot (cfun
->eh
->exception_handler_label_map
, &tmp
, NO_INSERT
);
2409 region
= (*slot
)->region
;
2413 /* Flow will want to remove MUST_NOT_THROW regions as unreachable
2414 because there is no path to the fallback call to terminate.
2415 But the region continues to affect call-site data until there
2416 are no more contained calls, which we don't see here. */
2417 if (region
->type
== ERT_MUST_NOT_THROW
)
2419 htab_clear_slot (cfun
->eh
->exception_handler_label_map
, (void **) slot
);
2420 region
->label
= NULL_RTX
;
2423 remove_eh_handler (region
);
2426 /* Invokes CALLBACK for every exception handler label. Only used by old
2427 loop hackery; should not be used by new code. */
2430 for_each_eh_label (void (*callback
) (rtx
))
2432 htab_traverse (cfun
->eh
->exception_handler_label_map
, for_each_eh_label_1
,
2433 (void *) &callback
);
2437 for_each_eh_label_1 (void **pentry
, void *data
)
2439 struct ehl_map_entry
*entry
= *(struct ehl_map_entry
**)pentry
;
2440 void (*callback
) (rtx
) = *(void (**) (rtx
)) data
;
2442 (*callback
) (entry
->label
);
2446 /* This section describes CFG exception edges for flow. */
2448 /* For communicating between calls to reachable_next_level. */
2449 struct reachable_info
GTY(())
2456 /* A subroutine of reachable_next_level. Return true if TYPE, or a
2457 base class of TYPE, is in HANDLED. */
2460 check_handled (tree handled
, tree type
)
2464 /* We can check for exact matches without front-end help. */
2465 if (! lang_eh_type_covers
)
2467 for (t
= handled
; t
; t
= TREE_CHAIN (t
))
2468 if (TREE_VALUE (t
) == type
)
2473 for (t
= handled
; t
; t
= TREE_CHAIN (t
))
2474 if ((*lang_eh_type_covers
) (TREE_VALUE (t
), type
))
2481 /* A subroutine of reachable_next_level. If we are collecting a list
2482 of handlers, add one. After landing pad generation, reference
2483 it instead of the handlers themselves. Further, the handlers are
2484 all wired together, so by referencing one, we've got them all.
2485 Before landing pad generation we reference each handler individually.
2487 LP_REGION contains the landing pad; REGION is the handler. */
2490 add_reachable_handler (struct reachable_info
*info
, struct eh_region
*lp_region
, struct eh_region
*region
)
2495 if (cfun
->eh
->built_landing_pads
)
2497 if (! info
->handlers
)
2498 info
->handlers
= alloc_INSN_LIST (lp_region
->landing_pad
, NULL_RTX
);
2501 info
->handlers
= alloc_INSN_LIST (region
->label
, info
->handlers
);
2504 /* Process one level of exception regions for reachability.
2505 If TYPE_THROWN is non-null, then it is the *exact* type being
2506 propagated. If INFO is non-null, then collect handler labels
2507 and caught/allowed type information between invocations. */
2509 static enum reachable_code
2510 reachable_next_level (struct eh_region
*region
, tree type_thrown
,
2511 struct reachable_info
*info
)
2513 switch (region
->type
)
2516 /* Before landing-pad generation, we model control flow
2517 directly to the individual handlers. In this way we can
2518 see that catch handler types may shadow one another. */
2519 add_reachable_handler (info
, region
, region
);
2520 return RNL_MAYBE_CAUGHT
;
2524 struct eh_region
*c
;
2525 enum reachable_code ret
= RNL_NOT_CAUGHT
;
2527 for (c
= region
->u
.try.catch; c
; c
= c
->u
.catch.next_catch
)
2529 /* A catch-all handler ends the search. */
2530 if (c
->u
.catch.type_list
== NULL
)
2532 add_reachable_handler (info
, region
, c
);
2538 /* If we have at least one type match, end the search. */
2539 tree tp_node
= c
->u
.catch.type_list
;
2541 for (; tp_node
; tp_node
= TREE_CHAIN (tp_node
))
2543 tree type
= TREE_VALUE (tp_node
);
2545 if (type
== type_thrown
2546 || (lang_eh_type_covers
2547 && (*lang_eh_type_covers
) (type
, type_thrown
)))
2549 add_reachable_handler (info
, region
, c
);
2554 /* If we have definitive information of a match failure,
2555 the catch won't trigger. */
2556 if (lang_eh_type_covers
)
2557 return RNL_NOT_CAUGHT
;
2560 /* At this point, we either don't know what type is thrown or
2561 don't have front-end assistance to help deciding if it is
2562 covered by one of the types in the list for this region.
2564 We'd then like to add this region to the list of reachable
2565 handlers since it is indeed potentially reachable based on the
2566 information we have.
2568 Actually, this handler is for sure not reachable if all the
2569 types it matches have already been caught. That is, it is only
2570 potentially reachable if at least one of the types it catches
2571 has not been previously caught. */
2574 ret
= RNL_MAYBE_CAUGHT
;
2577 tree tp_node
= c
->u
.catch.type_list
;
2578 bool maybe_reachable
= false;
2580 /* Compute the potential reachability of this handler and
2581 update the list of types caught at the same time. */
2582 for (; tp_node
; tp_node
= TREE_CHAIN (tp_node
))
2584 tree type
= TREE_VALUE (tp_node
);
2586 if (! check_handled (info
->types_caught
, type
))
2589 = tree_cons (NULL
, type
, info
->types_caught
);
2591 maybe_reachable
= true;
2595 if (maybe_reachable
)
2597 add_reachable_handler (info
, region
, c
);
2599 /* ??? If the catch type is a base class of every allowed
2600 type, then we know we can stop the search. */
2601 ret
= RNL_MAYBE_CAUGHT
;
2609 case ERT_ALLOWED_EXCEPTIONS
:
2610 /* An empty list of types definitely ends the search. */
2611 if (region
->u
.allowed
.type_list
== NULL_TREE
)
2613 add_reachable_handler (info
, region
, region
);
2617 /* Collect a list of lists of allowed types for use in detecting
2618 when a catch may be transformed into a catch-all. */
2620 info
->types_allowed
= tree_cons (NULL_TREE
,
2621 region
->u
.allowed
.type_list
,
2622 info
->types_allowed
);
2624 /* If we have definitive information about the type hierarchy,
2625 then we can tell if the thrown type will pass through the
2627 if (type_thrown
&& lang_eh_type_covers
)
2629 if (check_handled (region
->u
.allowed
.type_list
, type_thrown
))
2630 return RNL_NOT_CAUGHT
;
2633 add_reachable_handler (info
, region
, region
);
2638 add_reachable_handler (info
, region
, region
);
2639 return RNL_MAYBE_CAUGHT
;
2642 /* Catch regions are handled by their controlling try region. */
2643 return RNL_NOT_CAUGHT
;
2645 case ERT_MUST_NOT_THROW
:
2646 /* Here we end our search, since no exceptions may propagate.
2647 If we've touched down at some landing pad previous, then the
2648 explicit function call we generated may be used. Otherwise
2649 the call is made by the runtime. */
2650 if (info
&& info
->handlers
)
2652 add_reachable_handler (info
, region
, region
);
2661 /* Shouldn't see these here. */
2668 /* Retrieve a list of labels of exception handlers which can be
2669 reached by a given insn. */
2672 reachable_handlers (rtx insn
)
2674 struct reachable_info info
;
2675 struct eh_region
*region
;
2679 if (GET_CODE (insn
) == JUMP_INSN
2680 && GET_CODE (PATTERN (insn
)) == RESX
)
2681 region_number
= XINT (PATTERN (insn
), 0);
2684 rtx note
= find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
);
2685 if (!note
|| INTVAL (XEXP (note
, 0)) <= 0)
2687 region_number
= INTVAL (XEXP (note
, 0));
2690 memset (&info
, 0, sizeof (info
));
2692 region
= cfun
->eh
->region_array
[region_number
];
2694 type_thrown
= NULL_TREE
;
2695 if (GET_CODE (insn
) == JUMP_INSN
2696 && GET_CODE (PATTERN (insn
)) == RESX
)
2698 /* A RESX leaves a region instead of entering it. Thus the
2699 region itself may have been deleted out from under us. */
2702 region
= region
->outer
;
2704 else if (region
->type
== ERT_THROW
)
2706 type_thrown
= region
->u
.throw.type
;
2707 region
= region
->outer
;
2712 if (reachable_next_level (region
, type_thrown
, &info
) >= RNL_CAUGHT
)
2714 /* If we have processed one cleanup, there is no point in
2715 processing any more of them. Each cleanup will have an edge
2716 to the next outer cleanup region, so the flow graph will be
2718 if (region
->type
== ERT_CLEANUP
)
2719 region
= region
->u
.cleanup
.prev_try
;
2721 region
= region
->outer
;
2724 return info
.handlers
;
2727 /* Determine if the given INSN can throw an exception that is caught
2728 within the function. */
2731 can_throw_internal (rtx insn
)
2733 struct eh_region
*region
;
2737 if (! INSN_P (insn
))
2740 if (GET_CODE (insn
) == INSN
2741 && GET_CODE (PATTERN (insn
)) == SEQUENCE
)
2742 insn
= XVECEXP (PATTERN (insn
), 0, 0);
2744 if (GET_CODE (insn
) == CALL_INSN
2745 && GET_CODE (PATTERN (insn
)) == CALL_PLACEHOLDER
)
2748 for (i
= 0; i
< 3; ++i
)
2750 rtx sub
= XEXP (PATTERN (insn
), i
);
2751 for (; sub
; sub
= NEXT_INSN (sub
))
2752 if (can_throw_internal (sub
))
2758 /* Every insn that might throw has an EH_REGION note. */
2759 note
= find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
);
2760 if (!note
|| INTVAL (XEXP (note
, 0)) <= 0)
2763 region
= cfun
->eh
->region_array
[INTVAL (XEXP (note
, 0))];
2765 type_thrown
= NULL_TREE
;
2766 if (region
->type
== ERT_THROW
)
2768 type_thrown
= region
->u
.throw.type
;
2769 region
= region
->outer
;
2772 /* If this exception is ignored by each and every containing region,
2773 then control passes straight out. The runtime may handle some
2774 regions, which also do not require processing internally. */
2775 for (; region
; region
= region
->outer
)
2777 enum reachable_code how
= reachable_next_level (region
, type_thrown
, 0);
2778 if (how
== RNL_BLOCKED
)
2780 if (how
!= RNL_NOT_CAUGHT
)
2787 /* Determine if the given INSN can throw an exception that is
2788 visible outside the function. */
2791 can_throw_external (rtx insn
)
2793 struct eh_region
*region
;
2797 if (! INSN_P (insn
))
2800 if (GET_CODE (insn
) == INSN
2801 && GET_CODE (PATTERN (insn
)) == SEQUENCE
)
2802 insn
= XVECEXP (PATTERN (insn
), 0, 0);
2804 if (GET_CODE (insn
) == CALL_INSN
2805 && GET_CODE (PATTERN (insn
)) == CALL_PLACEHOLDER
)
2808 for (i
= 0; i
< 3; ++i
)
2810 rtx sub
= XEXP (PATTERN (insn
), i
);
2811 for (; sub
; sub
= NEXT_INSN (sub
))
2812 if (can_throw_external (sub
))
2818 note
= find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
);
2821 /* Calls (and trapping insns) without notes are outside any
2822 exception handling region in this function. We have to
2823 assume it might throw. Given that the front end and middle
2824 ends mark known NOTHROW functions, this isn't so wildly
2826 return (GET_CODE (insn
) == CALL_INSN
2827 || (flag_non_call_exceptions
2828 && may_trap_p (PATTERN (insn
))));
2830 if (INTVAL (XEXP (note
, 0)) <= 0)
2833 region
= cfun
->eh
->region_array
[INTVAL (XEXP (note
, 0))];
2835 type_thrown
= NULL_TREE
;
2836 if (region
->type
== ERT_THROW
)
2838 type_thrown
= region
->u
.throw.type
;
2839 region
= region
->outer
;
2842 /* If the exception is caught or blocked by any containing region,
2843 then it is not seen by any calling function. */
2844 for (; region
; region
= region
->outer
)
2845 if (reachable_next_level (region
, type_thrown
, NULL
) >= RNL_CAUGHT
)
2851 /* Set current_function_nothrow and cfun->all_throwers_are_sibcalls. */
2854 set_nothrow_function_flags (void)
2858 current_function_nothrow
= 1;
2860 /* Assume cfun->all_throwers_are_sibcalls until we encounter
2861 something that can throw an exception. We specifically exempt
2862 CALL_INSNs that are SIBLING_CALL_P, as these are really jumps,
2863 and can't throw. Most CALL_INSNs are not SIBLING_CALL_P, so this
2866 cfun
->all_throwers_are_sibcalls
= 1;
2868 if (! flag_exceptions
)
2871 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
2872 if (can_throw_external (insn
))
2874 current_function_nothrow
= 0;
2876 if (GET_CODE (insn
) != CALL_INSN
|| !SIBLING_CALL_P (insn
))
2878 cfun
->all_throwers_are_sibcalls
= 0;
2883 for (insn
= current_function_epilogue_delay_list
; insn
;
2884 insn
= XEXP (insn
, 1))
2885 if (can_throw_external (insn
))
2887 current_function_nothrow
= 0;
2889 if (GET_CODE (insn
) != CALL_INSN
|| !SIBLING_CALL_P (insn
))
2891 cfun
->all_throwers_are_sibcalls
= 0;
2898 /* Various hooks for unwind library. */
2900 /* Do any necessary initialization to access arbitrary stack frames.
2901 On the SPARC, this means flushing the register windows. */
2904 expand_builtin_unwind_init (void)
2906 /* Set this so all the registers get saved in our frame; we need to be
2907 able to copy the saved values for any registers from frames we unwind. */
2908 current_function_has_nonlocal_label
= 1;
2910 #ifdef SETUP_FRAME_ADDRESSES
2911 SETUP_FRAME_ADDRESSES ();
2916 expand_builtin_eh_return_data_regno (tree arglist
)
2918 tree which
= TREE_VALUE (arglist
);
2919 unsigned HOST_WIDE_INT iwhich
;
2921 if (TREE_CODE (which
) != INTEGER_CST
)
2923 error ("argument of `__builtin_eh_return_regno' must be constant");
2927 iwhich
= tree_low_cst (which
, 1);
2928 iwhich
= EH_RETURN_DATA_REGNO (iwhich
);
2929 if (iwhich
== INVALID_REGNUM
)
2932 #ifdef DWARF_FRAME_REGNUM
2933 iwhich
= DWARF_FRAME_REGNUM (iwhich
);
2935 iwhich
= DBX_REGISTER_NUMBER (iwhich
);
2938 return GEN_INT (iwhich
);
2941 /* Given a value extracted from the return address register or stack slot,
2942 return the actual address encoded in that value. */
2945 expand_builtin_extract_return_addr (tree addr_tree
)
2947 rtx addr
= expand_expr (addr_tree
, NULL_RTX
, Pmode
, 0);
2949 if (GET_MODE (addr
) != Pmode
2950 && GET_MODE (addr
) != VOIDmode
)
2952 #ifdef POINTERS_EXTEND_UNSIGNED
2953 addr
= convert_memory_address (Pmode
, addr
);
2955 addr
= convert_to_mode (Pmode
, addr
, 0);
2959 /* First mask out any unwanted bits. */
2960 #ifdef MASK_RETURN_ADDR
2961 expand_and (Pmode
, addr
, MASK_RETURN_ADDR
, addr
);
2964 /* Then adjust to find the real return address. */
2965 #if defined (RETURN_ADDR_OFFSET)
2966 addr
= plus_constant (addr
, RETURN_ADDR_OFFSET
);
2972 /* Given an actual address in addr_tree, do any necessary encoding
2973 and return the value to be stored in the return address register or
2974 stack slot so the epilogue will return to that address. */
2977 expand_builtin_frob_return_addr (tree addr_tree
)
2979 rtx addr
= expand_expr (addr_tree
, NULL_RTX
, ptr_mode
, 0);
2981 addr
= convert_memory_address (Pmode
, addr
);
2983 #ifdef RETURN_ADDR_OFFSET
2984 addr
= force_reg (Pmode
, addr
);
2985 addr
= plus_constant (addr
, -RETURN_ADDR_OFFSET
);
2991 /* Set up the epilogue with the magic bits we'll need to return to the
2992 exception handler. */
2995 expand_builtin_eh_return (tree stackadj_tree ATTRIBUTE_UNUSED
,
3000 #ifdef EH_RETURN_STACKADJ_RTX
3001 tmp
= expand_expr (stackadj_tree
, cfun
->eh
->ehr_stackadj
, VOIDmode
, 0);
3002 tmp
= convert_memory_address (Pmode
, tmp
);
3003 if (!cfun
->eh
->ehr_stackadj
)
3004 cfun
->eh
->ehr_stackadj
= copy_to_reg (tmp
);
3005 else if (tmp
!= cfun
->eh
->ehr_stackadj
)
3006 emit_move_insn (cfun
->eh
->ehr_stackadj
, tmp
);
3009 tmp
= expand_expr (handler_tree
, cfun
->eh
->ehr_handler
, VOIDmode
, 0);
3010 tmp
= convert_memory_address (Pmode
, tmp
);
3011 if (!cfun
->eh
->ehr_handler
)
3012 cfun
->eh
->ehr_handler
= copy_to_reg (tmp
);
3013 else if (tmp
!= cfun
->eh
->ehr_handler
)
3014 emit_move_insn (cfun
->eh
->ehr_handler
, tmp
);
3016 if (!cfun
->eh
->ehr_label
)
3017 cfun
->eh
->ehr_label
= gen_label_rtx ();
3018 emit_jump (cfun
->eh
->ehr_label
);
3022 expand_eh_return (void)
3026 if (! cfun
->eh
->ehr_label
)
3029 current_function_calls_eh_return
= 1;
3031 #ifdef EH_RETURN_STACKADJ_RTX
3032 emit_move_insn (EH_RETURN_STACKADJ_RTX
, const0_rtx
);
3035 around_label
= gen_label_rtx ();
3036 emit_jump (around_label
);
3038 emit_label (cfun
->eh
->ehr_label
);
3039 clobber_return_register ();
3041 #ifdef EH_RETURN_STACKADJ_RTX
3042 emit_move_insn (EH_RETURN_STACKADJ_RTX
, cfun
->eh
->ehr_stackadj
);
3045 #ifdef HAVE_eh_return
3047 emit_insn (gen_eh_return (cfun
->eh
->ehr_handler
));
3051 #ifdef EH_RETURN_HANDLER_RTX
3052 emit_move_insn (EH_RETURN_HANDLER_RTX
, cfun
->eh
->ehr_handler
);
3054 error ("__builtin_eh_return not supported on this target");
3058 emit_label (around_label
);
3061 /* In the following functions, we represent entries in the action table
3062 as 1-based indices. Special cases are:
3064 0: null action record, non-null landing pad; implies cleanups
3065 -1: null action record, null landing pad; implies no action
3066 -2: no call-site entry; implies must_not_throw
3067 -3: we have yet to process outer regions
3069 Further, no special cases apply to the "next" field of the record.
3070 For next, 0 means end of list. */
3072 struct action_record
3080 action_record_eq (const void *pentry
, const void *pdata
)
3082 const struct action_record
*entry
= (const struct action_record
*) pentry
;
3083 const struct action_record
*data
= (const struct action_record
*) pdata
;
3084 return entry
->filter
== data
->filter
&& entry
->next
== data
->next
;
3088 action_record_hash (const void *pentry
)
3090 const struct action_record
*entry
= (const struct action_record
*) pentry
;
3091 return entry
->next
* 1009 + entry
->filter
;
3095 add_action_record (htab_t ar_hash
, int filter
, int next
)
3097 struct action_record
**slot
, *new, tmp
;
3099 tmp
.filter
= filter
;
3101 slot
= (struct action_record
**) htab_find_slot (ar_hash
, &tmp
, INSERT
);
3103 if ((new = *slot
) == NULL
)
3105 new = xmalloc (sizeof (*new));
3106 new->offset
= VARRAY_ACTIVE_SIZE (cfun
->eh
->action_record_data
) + 1;
3107 new->filter
= filter
;
3111 /* The filter value goes in untouched. The link to the next
3112 record is a "self-relative" byte offset, or zero to indicate
3113 that there is no next record. So convert the absolute 1 based
3114 indices we've been carrying around into a displacement. */
3116 push_sleb128 (&cfun
->eh
->action_record_data
, filter
);
3118 next
-= VARRAY_ACTIVE_SIZE (cfun
->eh
->action_record_data
) + 1;
3119 push_sleb128 (&cfun
->eh
->action_record_data
, next
);
3126 collect_one_action_chain (htab_t ar_hash
, struct eh_region
*region
)
3128 struct eh_region
*c
;
3131 /* If we've reached the top of the region chain, then we have
3132 no actions, and require no landing pad. */
3136 switch (region
->type
)
3139 /* A cleanup adds a zero filter to the beginning of the chain, but
3140 there are special cases to look out for. If there are *only*
3141 cleanups along a path, then it compresses to a zero action.
3142 Further, if there are multiple cleanups along a path, we only
3143 need to represent one of them, as that is enough to trigger
3144 entry to the landing pad at runtime. */
3145 next
= collect_one_action_chain (ar_hash
, region
->outer
);
3148 for (c
= region
->outer
; c
; c
= c
->outer
)
3149 if (c
->type
== ERT_CLEANUP
)
3151 return add_action_record (ar_hash
, 0, next
);
3154 /* Process the associated catch regions in reverse order.
3155 If there's a catch-all handler, then we don't need to
3156 search outer regions. Use a magic -3 value to record
3157 that we haven't done the outer search. */
3159 for (c
= region
->u
.try.last_catch
; c
; c
= c
->u
.catch.prev_catch
)
3161 if (c
->u
.catch.type_list
== NULL
)
3163 /* Retrieve the filter from the head of the filter list
3164 where we have stored it (see assign_filter_values). */
3166 = TREE_INT_CST_LOW (TREE_VALUE (c
->u
.catch.filter_list
));
3168 next
= add_action_record (ar_hash
, filter
, 0);
3172 /* Once the outer search is done, trigger an action record for
3173 each filter we have. */
3178 next
= collect_one_action_chain (ar_hash
, region
->outer
);
3180 /* If there is no next action, terminate the chain. */
3183 /* If all outer actions are cleanups or must_not_throw,
3184 we'll have no action record for it, since we had wanted
3185 to encode these states in the call-site record directly.
3186 Add a cleanup action to the chain to catch these. */
3188 next
= add_action_record (ar_hash
, 0, 0);
3191 flt_node
= c
->u
.catch.filter_list
;
3192 for (; flt_node
; flt_node
= TREE_CHAIN (flt_node
))
3194 int filter
= TREE_INT_CST_LOW (TREE_VALUE (flt_node
));
3195 next
= add_action_record (ar_hash
, filter
, next
);
3201 case ERT_ALLOWED_EXCEPTIONS
:
3202 /* An exception specification adds its filter to the
3203 beginning of the chain. */
3204 next
= collect_one_action_chain (ar_hash
, region
->outer
);
3205 return add_action_record (ar_hash
, region
->u
.allowed
.filter
,
3206 next
< 0 ? 0 : next
);
3208 case ERT_MUST_NOT_THROW
:
3209 /* A must-not-throw region with no inner handlers or cleanups
3210 requires no call-site entry. Note that this differs from
3211 the no handler or cleanup case in that we do require an lsda
3212 to be generated. Return a magic -2 value to record this. */
3217 /* CATCH regions are handled in TRY above. THROW regions are
3218 for optimization information only and produce no output. */
3219 return collect_one_action_chain (ar_hash
, region
->outer
);
3227 add_call_site (rtx landing_pad
, int action
)
3229 struct call_site_record
*data
= cfun
->eh
->call_site_data
;
3230 int used
= cfun
->eh
->call_site_data_used
;
3231 int size
= cfun
->eh
->call_site_data_size
;
3235 size
= (size
? size
* 2 : 64);
3236 data
= ggc_realloc (data
, sizeof (*data
) * size
);
3237 cfun
->eh
->call_site_data
= data
;
3238 cfun
->eh
->call_site_data_size
= size
;
3241 data
[used
].landing_pad
= landing_pad
;
3242 data
[used
].action
= action
;
3244 cfun
->eh
->call_site_data_used
= used
+ 1;
3246 return used
+ call_site_base
;
3249 /* Turn REG_EH_REGION notes back into NOTE_INSN_EH_REGION notes.
3250 The new note numbers will not refer to region numbers, but
3251 instead to call site entries. */
3254 convert_to_eh_region_ranges (void)
3256 rtx insn
, iter
, note
;
3258 int last_action
= -3;
3259 rtx last_action_insn
= NULL_RTX
;
3260 rtx last_landing_pad
= NULL_RTX
;
3261 rtx first_no_action_insn
= NULL_RTX
;
3264 if (USING_SJLJ_EXCEPTIONS
|| cfun
->eh
->region_tree
== NULL
)
3267 VARRAY_UCHAR_INIT (cfun
->eh
->action_record_data
, 64, "action_record_data");
3269 ar_hash
= htab_create (31, action_record_hash
, action_record_eq
, free
);
3271 for (iter
= get_insns (); iter
; iter
= NEXT_INSN (iter
))
3274 struct eh_region
*region
;
3276 rtx this_landing_pad
;
3279 if (GET_CODE (insn
) == INSN
3280 && GET_CODE (PATTERN (insn
)) == SEQUENCE
)
3281 insn
= XVECEXP (PATTERN (insn
), 0, 0);
3283 note
= find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
);
3286 if (! (GET_CODE (insn
) == CALL_INSN
3287 || (flag_non_call_exceptions
3288 && may_trap_p (PATTERN (insn
)))))
3295 if (INTVAL (XEXP (note
, 0)) <= 0)
3297 region
= cfun
->eh
->region_array
[INTVAL (XEXP (note
, 0))];
3298 this_action
= collect_one_action_chain (ar_hash
, region
);
3301 /* Existence of catch handlers, or must-not-throw regions
3302 implies that an lsda is needed (even if empty). */
3303 if (this_action
!= -1)
3304 cfun
->uses_eh_lsda
= 1;
3306 /* Delay creation of region notes for no-action regions
3307 until we're sure that an lsda will be required. */
3308 else if (last_action
== -3)
3310 first_no_action_insn
= iter
;
3314 /* Cleanups and handlers may share action chains but not
3315 landing pads. Collect the landing pad for this region. */
3316 if (this_action
>= 0)
3318 struct eh_region
*o
;
3319 for (o
= region
; ! o
->landing_pad
; o
= o
->outer
)
3321 this_landing_pad
= o
->landing_pad
;
3324 this_landing_pad
= NULL_RTX
;
3326 /* Differing actions or landing pads implies a change in call-site
3327 info, which implies some EH_REGION note should be emitted. */
3328 if (last_action
!= this_action
3329 || last_landing_pad
!= this_landing_pad
)
3331 /* If we'd not seen a previous action (-3) or the previous
3332 action was must-not-throw (-2), then we do not need an
3334 if (last_action
>= -1)
3336 /* If we delayed the creation of the begin, do it now. */
3337 if (first_no_action_insn
)
3339 call_site
= add_call_site (NULL_RTX
, 0);
3340 note
= emit_note_before (NOTE_INSN_EH_REGION_BEG
,
3341 first_no_action_insn
);
3342 NOTE_EH_HANDLER (note
) = call_site
;
3343 first_no_action_insn
= NULL_RTX
;
3346 note
= emit_note_after (NOTE_INSN_EH_REGION_END
,
3348 NOTE_EH_HANDLER (note
) = call_site
;
3351 /* If the new action is must-not-throw, then no region notes
3353 if (this_action
>= -1)
3355 call_site
= add_call_site (this_landing_pad
,
3356 this_action
< 0 ? 0 : this_action
);
3357 note
= emit_note_before (NOTE_INSN_EH_REGION_BEG
, iter
);
3358 NOTE_EH_HANDLER (note
) = call_site
;
3361 last_action
= this_action
;
3362 last_landing_pad
= this_landing_pad
;
3364 last_action_insn
= iter
;
3367 if (last_action
>= -1 && ! first_no_action_insn
)
3369 note
= emit_note_after (NOTE_INSN_EH_REGION_END
, last_action_insn
);
3370 NOTE_EH_HANDLER (note
) = call_site
;
3373 htab_delete (ar_hash
);
3378 push_uleb128 (varray_type
*data_area
, unsigned int value
)
3382 unsigned char byte
= value
& 0x7f;
3386 VARRAY_PUSH_UCHAR (*data_area
, byte
);
3392 push_sleb128 (varray_type
*data_area
, int value
)
3399 byte
= value
& 0x7f;
3401 more
= ! ((value
== 0 && (byte
& 0x40) == 0)
3402 || (value
== -1 && (byte
& 0x40) != 0));
3405 VARRAY_PUSH_UCHAR (*data_area
, byte
);
3411 #ifndef HAVE_AS_LEB128
3413 dw2_size_of_call_site_table (void)
3415 int n
= cfun
->eh
->call_site_data_used
;
3416 int size
= n
* (4 + 4 + 4);
3419 for (i
= 0; i
< n
; ++i
)
3421 struct call_site_record
*cs
= &cfun
->eh
->call_site_data
[i
];
3422 size
+= size_of_uleb128 (cs
->action
);
3429 sjlj_size_of_call_site_table (void)
3431 int n
= cfun
->eh
->call_site_data_used
;
3435 for (i
= 0; i
< n
; ++i
)
3437 struct call_site_record
*cs
= &cfun
->eh
->call_site_data
[i
];
3438 size
+= size_of_uleb128 (INTVAL (cs
->landing_pad
));
3439 size
+= size_of_uleb128 (cs
->action
);
3447 dw2_output_call_site_table (void)
3449 const char *const function_start_lab
3450 = IDENTIFIER_POINTER (current_function_func_begin_label
);
3451 int n
= cfun
->eh
->call_site_data_used
;
3454 for (i
= 0; i
< n
; ++i
)
3456 struct call_site_record
*cs
= &cfun
->eh
->call_site_data
[i
];
3457 char reg_start_lab
[32];
3458 char reg_end_lab
[32];
3459 char landing_pad_lab
[32];
3461 ASM_GENERATE_INTERNAL_LABEL (reg_start_lab
, "LEHB", call_site_base
+ i
);
3462 ASM_GENERATE_INTERNAL_LABEL (reg_end_lab
, "LEHE", call_site_base
+ i
);
3464 if (cs
->landing_pad
)
3465 ASM_GENERATE_INTERNAL_LABEL (landing_pad_lab
, "L",
3466 CODE_LABEL_NUMBER (cs
->landing_pad
));
3468 /* ??? Perhaps use insn length scaling if the assembler supports
3469 generic arithmetic. */
3470 /* ??? Perhaps use attr_length to choose data1 or data2 instead of
3471 data4 if the function is small enough. */
3472 #ifdef HAVE_AS_LEB128
3473 dw2_asm_output_delta_uleb128 (reg_start_lab
, function_start_lab
,
3474 "region %d start", i
);
3475 dw2_asm_output_delta_uleb128 (reg_end_lab
, reg_start_lab
,
3477 if (cs
->landing_pad
)
3478 dw2_asm_output_delta_uleb128 (landing_pad_lab
, function_start_lab
,
3481 dw2_asm_output_data_uleb128 (0, "landing pad");
3483 dw2_asm_output_delta (4, reg_start_lab
, function_start_lab
,
3484 "region %d start", i
);
3485 dw2_asm_output_delta (4, reg_end_lab
, reg_start_lab
, "length");
3486 if (cs
->landing_pad
)
3487 dw2_asm_output_delta (4, landing_pad_lab
, function_start_lab
,
3490 dw2_asm_output_data (4, 0, "landing pad");
3492 dw2_asm_output_data_uleb128 (cs
->action
, "action");
3495 call_site_base
+= n
;
3499 sjlj_output_call_site_table (void)
3501 int n
= cfun
->eh
->call_site_data_used
;
3504 for (i
= 0; i
< n
; ++i
)
3506 struct call_site_record
*cs
= &cfun
->eh
->call_site_data
[i
];
3508 dw2_asm_output_data_uleb128 (INTVAL (cs
->landing_pad
),
3509 "region %d landing pad", i
);
3510 dw2_asm_output_data_uleb128 (cs
->action
, "action");
3513 call_site_base
+= n
;
3516 /* Tell assembler to switch to the section for the exception handling
3520 default_exception_section (void)
3522 if (targetm
.have_named_sections
)
3525 #ifdef HAVE_LD_RO_RW_SECTION_MIXING
3526 int tt_format
= ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3529 || ((tt_format
& 0x70) != DW_EH_PE_absptr
3530 && (tt_format
& 0x70) != DW_EH_PE_aligned
))
3531 ? 0 : SECTION_WRITE
;
3533 flags
= SECTION_WRITE
;
3535 named_section_flags (".gcc_except_table", flags
);
3540 readonly_data_section ();
3544 output_function_exception_table (void)
3546 int tt_format
, cs_format
, lp_format
, i
, n
;
3547 #ifdef HAVE_AS_LEB128
3548 char ttype_label
[32];
3549 char cs_after_size_label
[32];
3550 char cs_end_label
[32];
3555 int tt_format_size
= 0;
3557 /* Not all functions need anything. */
3558 if (! cfun
->uses_eh_lsda
)
3561 #ifdef IA64_UNWIND_INFO
3562 fputs ("\t.personality\t", asm_out_file
);
3563 output_addr_const (asm_out_file
, eh_personality_libfunc
);
3564 fputs ("\n\t.handlerdata\n", asm_out_file
);
3565 /* Note that varasm still thinks we're in the function's code section.
3566 The ".endp" directive that will immediately follow will take us back. */
3568 (*targetm
.asm_out
.exception_section
) ();
3571 have_tt_data
= (VARRAY_ACTIVE_SIZE (cfun
->eh
->ttype_data
) > 0
3572 || VARRAY_ACTIVE_SIZE (cfun
->eh
->ehspec_data
) > 0);
3574 /* Indicate the format of the @TType entries. */
3576 tt_format
= DW_EH_PE_omit
;
3579 tt_format
= ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3580 #ifdef HAVE_AS_LEB128
3581 ASM_GENERATE_INTERNAL_LABEL (ttype_label
, "LLSDATT",
3582 current_function_funcdef_no
);
3584 tt_format_size
= size_of_encoded_value (tt_format
);
3586 assemble_align (tt_format_size
* BITS_PER_UNIT
);
3589 (*targetm
.asm_out
.internal_label
) (asm_out_file
, "LLSDA",
3590 current_function_funcdef_no
);
3592 /* The LSDA header. */
3594 /* Indicate the format of the landing pad start pointer. An omitted
3595 field implies @LPStart == @Start. */
3596 /* Currently we always put @LPStart == @Start. This field would
3597 be most useful in moving the landing pads completely out of
3598 line to another section, but it could also be used to minimize
3599 the size of uleb128 landing pad offsets. */
3600 lp_format
= DW_EH_PE_omit
;
3601 dw2_asm_output_data (1, lp_format
, "@LPStart format (%s)",
3602 eh_data_format_name (lp_format
));
3604 /* @LPStart pointer would go here. */
3606 dw2_asm_output_data (1, tt_format
, "@TType format (%s)",
3607 eh_data_format_name (tt_format
));
3609 #ifndef HAVE_AS_LEB128
3610 if (USING_SJLJ_EXCEPTIONS
)
3611 call_site_len
= sjlj_size_of_call_site_table ();
3613 call_site_len
= dw2_size_of_call_site_table ();
3616 /* A pc-relative 4-byte displacement to the @TType data. */
3619 #ifdef HAVE_AS_LEB128
3620 char ttype_after_disp_label
[32];
3621 ASM_GENERATE_INTERNAL_LABEL (ttype_after_disp_label
, "LLSDATTD",
3622 current_function_funcdef_no
);
3623 dw2_asm_output_delta_uleb128 (ttype_label
, ttype_after_disp_label
,
3624 "@TType base offset");
3625 ASM_OUTPUT_LABEL (asm_out_file
, ttype_after_disp_label
);
3627 /* Ug. Alignment queers things. */
3628 unsigned int before_disp
, after_disp
, last_disp
, disp
;
3630 before_disp
= 1 + 1;
3631 after_disp
= (1 + size_of_uleb128 (call_site_len
)
3633 + VARRAY_ACTIVE_SIZE (cfun
->eh
->action_record_data
)
3634 + (VARRAY_ACTIVE_SIZE (cfun
->eh
->ttype_data
)
3640 unsigned int disp_size
, pad
;
3643 disp_size
= size_of_uleb128 (disp
);
3644 pad
= before_disp
+ disp_size
+ after_disp
;
3645 if (pad
% tt_format_size
)
3646 pad
= tt_format_size
- (pad
% tt_format_size
);
3649 disp
= after_disp
+ pad
;
3651 while (disp
!= last_disp
);
3653 dw2_asm_output_data_uleb128 (disp
, "@TType base offset");
3657 /* Indicate the format of the call-site offsets. */
3658 #ifdef HAVE_AS_LEB128
3659 cs_format
= DW_EH_PE_uleb128
;
3661 cs_format
= DW_EH_PE_udata4
;
3663 dw2_asm_output_data (1, cs_format
, "call-site format (%s)",
3664 eh_data_format_name (cs_format
));
3666 #ifdef HAVE_AS_LEB128
3667 ASM_GENERATE_INTERNAL_LABEL (cs_after_size_label
, "LLSDACSB",
3668 current_function_funcdef_no
);
3669 ASM_GENERATE_INTERNAL_LABEL (cs_end_label
, "LLSDACSE",
3670 current_function_funcdef_no
);
3671 dw2_asm_output_delta_uleb128 (cs_end_label
, cs_after_size_label
,
3672 "Call-site table length");
3673 ASM_OUTPUT_LABEL (asm_out_file
, cs_after_size_label
);
3674 if (USING_SJLJ_EXCEPTIONS
)
3675 sjlj_output_call_site_table ();
3677 dw2_output_call_site_table ();
3678 ASM_OUTPUT_LABEL (asm_out_file
, cs_end_label
);
3680 dw2_asm_output_data_uleb128 (call_site_len
,"Call-site table length");
3681 if (USING_SJLJ_EXCEPTIONS
)
3682 sjlj_output_call_site_table ();
3684 dw2_output_call_site_table ();
3687 /* ??? Decode and interpret the data for flag_debug_asm. */
3688 n
= VARRAY_ACTIVE_SIZE (cfun
->eh
->action_record_data
);
3689 for (i
= 0; i
< n
; ++i
)
3690 dw2_asm_output_data (1, VARRAY_UCHAR (cfun
->eh
->action_record_data
, i
),
3691 (i
? NULL
: "Action record table"));
3694 assemble_align (tt_format_size
* BITS_PER_UNIT
);
3696 i
= VARRAY_ACTIVE_SIZE (cfun
->eh
->ttype_data
);
3699 tree type
= VARRAY_TREE (cfun
->eh
->ttype_data
, i
);
3702 if (type
== NULL_TREE
)
3706 struct cgraph_varpool_node
*node
;
3708 type
= lookup_type_for_runtime (type
);
3709 value
= expand_expr (type
, NULL_RTX
, VOIDmode
, EXPAND_INITIALIZER
);
3711 /* Let cgraph know that the rtti decl is used. Not all of the
3712 paths below go through assemble_integer, which would take
3713 care of this for us. */
3714 if (TREE_CODE (type
) == ADDR_EXPR
)
3716 type
= TREE_OPERAND (type
, 0);
3717 node
= cgraph_varpool_node (type
);
3719 cgraph_varpool_mark_needed_node (node
);
3721 else if (TREE_CODE (type
) != INTEGER_CST
)
3725 if (tt_format
== DW_EH_PE_absptr
|| tt_format
== DW_EH_PE_aligned
)
3726 assemble_integer (value
, tt_format_size
,
3727 tt_format_size
* BITS_PER_UNIT
, 1);
3729 dw2_asm_output_encoded_addr_rtx (tt_format
, value
, NULL
);
3732 #ifdef HAVE_AS_LEB128
3734 ASM_OUTPUT_LABEL (asm_out_file
, ttype_label
);
3737 /* ??? Decode and interpret the data for flag_debug_asm. */
3738 n
= VARRAY_ACTIVE_SIZE (cfun
->eh
->ehspec_data
);
3739 for (i
= 0; i
< n
; ++i
)
3740 dw2_asm_output_data (1, VARRAY_UCHAR (cfun
->eh
->ehspec_data
, i
),
3741 (i
? NULL
: "Exception specification table"));
3743 function_section (current_function_decl
);
3746 #include "gt-except.h"