1 /* Implements exception handling.
2 Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 Contributed by Mike Stump <mrs@cygnus.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 /* An exception is an event that can be signaled from within a
25 function. This event can then be "caught" or "trapped" by the
26 callers of this function. This potentially allows program flow to
27 be transferred to any arbitrary code associated with a function call
28 several levels up the stack.
30 The intended use for this mechanism is for signaling "exceptional
31 events" in an out-of-band fashion, hence its name. The C++ language
32 (and many other OO-styled or functional languages) practically
33 requires such a mechanism, as otherwise it becomes very difficult
34 or even impossible to signal failure conditions in complex
35 situations. The traditional C++ example is when an error occurs in
36 the process of constructing an object; without such a mechanism, it
37 is impossible to signal that the error occurs without adding global
38 state variables and error checks around every object construction.
40 The act of causing this event to occur is referred to as "throwing
41 an exception". (Alternate terms include "raising an exception" or
42 "signaling an exception".) The term "throw" is used because control
43 is returned to the callers of the function that is signaling the
44 exception, and thus there is the concept of "throwing" the
45 exception up the call stack.
47 [ Add updated documentation on how to use this. ] */
52 #include "coretypes.h"
60 #include "insn-config.h"
62 #include "integrate.h"
63 #include "hard-reg-set.h"
64 #include "basic-block.h"
66 #include "dwarf2asm.h"
67 #include "dwarf2out.h"
75 #include "langhooks.h"
77 /* Provide defaults for stuff that may not be defined when using
79 #ifndef EH_RETURN_DATA_REGNO
80 #define EH_RETURN_DATA_REGNO(N) INVALID_REGNUM
84 /* Nonzero means enable synchronous exceptions for non-call instructions. */
85 int flag_non_call_exceptions
;
87 /* Protect cleanup actions with must-not-throw regions, with a call
88 to the given failure handler. */
89 tree (*lang_protect_cleanup_actions
) PARAMS ((void));
91 /* Return true if type A catches type B. */
92 int (*lang_eh_type_covers
) PARAMS ((tree a
, tree b
));
94 /* Map a type to a runtime object to match type. */
95 tree (*lang_eh_runtime_type
) PARAMS ((tree
));
97 /* A hash table of label to region number. */
99 struct ehl_map_entry
GTY(())
102 struct eh_region
*region
;
105 static GTY(()) int call_site_base
;
106 static GTY ((param_is (union tree_node
)))
107 htab_t type_to_runtime_map
;
109 /* Describe the SjLj_Function_Context structure. */
110 static GTY(()) tree sjlj_fc_type_node
;
111 static int sjlj_fc_call_site_ofs
;
112 static int sjlj_fc_data_ofs
;
113 static int sjlj_fc_personality_ofs
;
114 static int sjlj_fc_lsda_ofs
;
115 static int sjlj_fc_jbuf_ofs
;
117 /* Describes one exception region. */
118 struct eh_region
GTY(())
120 /* The immediately surrounding region. */
121 struct eh_region
*outer
;
123 /* The list of immediately contained regions. */
124 struct eh_region
*inner
;
125 struct eh_region
*next_peer
;
127 /* An identifier for this region. */
130 /* When a region is deleted, its parents inherit the REG_EH_REGION
131 numbers already assigned. */
134 /* Each region does exactly one thing. */
141 ERT_ALLOWED_EXCEPTIONS
,
147 /* Holds the action to perform based on the preceding type. */
149 /* A list of catch blocks, a surrounding try block,
150 and the label for continuing after a catch. */
151 struct eh_region_u_try
{
152 struct eh_region
*catch;
153 struct eh_region
*last_catch
;
154 struct eh_region
*prev_try
;
156 } GTY ((tag ("ERT_TRY"))) try;
158 /* The list through the catch handlers, the list of type objects
159 matched, and the list of associated filters. */
160 struct eh_region_u_catch
{
161 struct eh_region
*next_catch
;
162 struct eh_region
*prev_catch
;
165 } GTY ((tag ("ERT_CATCH"))) catch;
167 /* A tree_list of allowed types. */
168 struct eh_region_u_allowed
{
171 } GTY ((tag ("ERT_ALLOWED_EXCEPTIONS"))) allowed
;
173 /* The type given by a call to "throw foo();", or discovered
175 struct eh_region_u_throw
{
177 } GTY ((tag ("ERT_THROW"))) throw;
179 /* Retain the cleanup expression even after expansion so that
180 we can match up fixup regions. */
181 struct eh_region_u_cleanup
{
183 struct eh_region
*prev_try
;
184 } GTY ((tag ("ERT_CLEANUP"))) cleanup
;
186 /* The real region (by expression and by pointer) that fixup code
188 struct eh_region_u_fixup
{
190 struct eh_region
*real_region
;
191 } GTY ((tag ("ERT_FIXUP"))) fixup
;
192 } GTY ((desc ("%0.type"))) u
;
194 /* Entry point for this region's handler before landing pads are built. */
197 /* Entry point for this region's handler from the runtime eh library. */
200 /* Entry point for this region's handler from an inner region. */
201 rtx post_landing_pad
;
203 /* The RESX insn for handing off control to the next outermost handler,
207 /* True if something in this region may throw. */
208 unsigned may_contain_throw
: 1;
211 struct call_site_record
GTY(())
217 /* Used to save exception status for each function. */
218 struct eh_status
GTY(())
220 /* The tree of all regions for this function. */
221 struct eh_region
*region_tree
;
223 /* The same information as an indexable array. */
224 struct eh_region
** GTY ((length ("%h.last_region_number"))) region_array
;
226 /* The most recently open region. */
227 struct eh_region
*cur_region
;
229 /* This is the region for which we are processing catch blocks. */
230 struct eh_region
*try_region
;
235 int built_landing_pads
;
236 int last_region_number
;
238 varray_type ttype_data
;
239 varray_type ehspec_data
;
240 varray_type action_record_data
;
242 htab_t
GTY ((param_is (struct ehl_map_entry
))) exception_handler_label_map
;
244 struct call_site_record
* GTY ((length ("%h.call_site_data_used")))
246 int call_site_data_used
;
247 int call_site_data_size
;
258 static int t2r_eq
PARAMS ((const void *,
260 static hashval_t t2r_hash
PARAMS ((const void *));
261 static void add_type_for_runtime
PARAMS ((tree
));
262 static tree lookup_type_for_runtime
PARAMS ((tree
));
264 static struct eh_region
*expand_eh_region_end
PARAMS ((void));
266 static rtx get_exception_filter
PARAMS ((struct function
*));
268 static void collect_eh_region_array
PARAMS ((void));
269 static void resolve_fixup_regions
PARAMS ((void));
270 static void remove_fixup_regions
PARAMS ((void));
271 static void remove_unreachable_regions
PARAMS ((rtx
));
272 static void convert_from_eh_region_ranges_1
PARAMS ((rtx
*, int *, int));
274 static struct eh_region
*duplicate_eh_region_1
PARAMS ((struct eh_region
*,
275 struct inline_remap
*));
276 static void duplicate_eh_region_2
PARAMS ((struct eh_region
*,
277 struct eh_region
**));
278 static int ttypes_filter_eq
PARAMS ((const void *,
280 static hashval_t ttypes_filter_hash
PARAMS ((const void *));
281 static int ehspec_filter_eq
PARAMS ((const void *,
283 static hashval_t ehspec_filter_hash
PARAMS ((const void *));
284 static int add_ttypes_entry
PARAMS ((htab_t
, tree
));
285 static int add_ehspec_entry
PARAMS ((htab_t
, htab_t
,
287 static void assign_filter_values
PARAMS ((void));
288 static void build_post_landing_pads
PARAMS ((void));
289 static void connect_post_landing_pads
PARAMS ((void));
290 static void dw2_build_landing_pads
PARAMS ((void));
293 static bool sjlj_find_directly_reachable_regions
294 PARAMS ((struct sjlj_lp_info
*));
295 static void sjlj_assign_call_site_values
296 PARAMS ((rtx
, struct sjlj_lp_info
*));
297 static void sjlj_mark_call_sites
298 PARAMS ((struct sjlj_lp_info
*));
299 static void sjlj_emit_function_enter
PARAMS ((rtx
));
300 static void sjlj_emit_function_exit
PARAMS ((void));
301 static void sjlj_emit_dispatch_table
302 PARAMS ((rtx
, struct sjlj_lp_info
*));
303 static void sjlj_build_landing_pads
PARAMS ((void));
305 static hashval_t ehl_hash
PARAMS ((const void *));
306 static int ehl_eq
PARAMS ((const void *,
308 static void add_ehl_entry
PARAMS ((rtx
,
309 struct eh_region
*));
310 static void remove_exception_handler_label
PARAMS ((rtx
));
311 static void remove_eh_handler
PARAMS ((struct eh_region
*));
312 static int for_each_eh_label_1
PARAMS ((void **, void *));
314 struct reachable_info
;
316 /* The return value of reachable_next_level. */
319 /* The given exception is not processed by the given region. */
321 /* The given exception may need processing by the given region. */
323 /* The given exception is completely processed by the given region. */
325 /* The given exception is completely processed by the runtime. */
329 static int check_handled
PARAMS ((tree
, tree
));
330 static void add_reachable_handler
331 PARAMS ((struct reachable_info
*, struct eh_region
*,
332 struct eh_region
*));
333 static enum reachable_code reachable_next_level
334 PARAMS ((struct eh_region
*, tree
, struct reachable_info
*));
336 static int action_record_eq
PARAMS ((const void *,
338 static hashval_t action_record_hash
PARAMS ((const void *));
339 static int add_action_record
PARAMS ((htab_t
, int, int));
340 static int collect_one_action_chain
PARAMS ((htab_t
,
341 struct eh_region
*));
342 static int add_call_site
PARAMS ((rtx
, int));
344 static void push_uleb128
PARAMS ((varray_type
*,
346 static void push_sleb128
PARAMS ((varray_type
*, int));
347 #ifndef HAVE_AS_LEB128
348 static int dw2_size_of_call_site_table
PARAMS ((void));
349 static int sjlj_size_of_call_site_table
PARAMS ((void));
351 static void dw2_output_call_site_table
PARAMS ((void));
352 static void sjlj_output_call_site_table
PARAMS ((void));
355 /* Routine to see if exception handling is turned on.
356 DO_WARN is nonzero if we want to inform the user that exception
357 handling is turned off.
359 This is used to ensure that -fexceptions has been specified if the
360 compiler tries to use any exception-specific functions. */
366 if (! flag_exceptions
)
368 static int warned
= 0;
369 if (! warned
&& do_warn
)
371 error ("exception handling disabled, use -fexceptions to enable");
383 if (! flag_exceptions
)
386 type_to_runtime_map
= htab_create_ggc (31, t2r_hash
, t2r_eq
, NULL
);
388 /* Create the SjLj_Function_Context structure. This should match
389 the definition in unwind-sjlj.c. */
390 if (USING_SJLJ_EXCEPTIONS
)
392 tree f_jbuf
, f_per
, f_lsda
, f_prev
, f_cs
, f_data
, tmp
;
394 sjlj_fc_type_node
= (*lang_hooks
.types
.make_type
) (RECORD_TYPE
);
396 f_prev
= build_decl (FIELD_DECL
, get_identifier ("__prev"),
397 build_pointer_type (sjlj_fc_type_node
));
398 DECL_FIELD_CONTEXT (f_prev
) = sjlj_fc_type_node
;
400 f_cs
= build_decl (FIELD_DECL
, get_identifier ("__call_site"),
402 DECL_FIELD_CONTEXT (f_cs
) = sjlj_fc_type_node
;
404 tmp
= build_index_type (build_int_2 (4 - 1, 0));
405 tmp
= build_array_type ((*lang_hooks
.types
.type_for_mode
) (word_mode
, 1),
407 f_data
= build_decl (FIELD_DECL
, get_identifier ("__data"), tmp
);
408 DECL_FIELD_CONTEXT (f_data
) = sjlj_fc_type_node
;
410 f_per
= build_decl (FIELD_DECL
, get_identifier ("__personality"),
412 DECL_FIELD_CONTEXT (f_per
) = sjlj_fc_type_node
;
414 f_lsda
= build_decl (FIELD_DECL
, get_identifier ("__lsda"),
416 DECL_FIELD_CONTEXT (f_lsda
) = sjlj_fc_type_node
;
418 #ifdef DONT_USE_BUILTIN_SETJMP
420 tmp
= build_int_2 (JMP_BUF_SIZE
- 1, 0);
422 /* Should be large enough for most systems, if it is not,
423 JMP_BUF_SIZE should be defined with the proper value. It will
424 also tend to be larger than necessary for most systems, a more
425 optimal port will define JMP_BUF_SIZE. */
426 tmp
= build_int_2 (FIRST_PSEUDO_REGISTER
+ 2 - 1, 0);
429 /* This is 2 for builtin_setjmp, plus whatever the target requires
430 via STACK_SAVEAREA_MODE (SAVE_NONLOCAL). */
431 tmp
= build_int_2 ((GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL
))
432 / GET_MODE_SIZE (Pmode
)) + 2 - 1, 0);
434 tmp
= build_index_type (tmp
);
435 tmp
= build_array_type (ptr_type_node
, tmp
);
436 f_jbuf
= build_decl (FIELD_DECL
, get_identifier ("__jbuf"), tmp
);
437 #ifdef DONT_USE_BUILTIN_SETJMP
438 /* We don't know what the alignment requirements of the
439 runtime's jmp_buf has. Overestimate. */
440 DECL_ALIGN (f_jbuf
) = BIGGEST_ALIGNMENT
;
441 DECL_USER_ALIGN (f_jbuf
) = 1;
443 DECL_FIELD_CONTEXT (f_jbuf
) = sjlj_fc_type_node
;
445 TYPE_FIELDS (sjlj_fc_type_node
) = f_prev
;
446 TREE_CHAIN (f_prev
) = f_cs
;
447 TREE_CHAIN (f_cs
) = f_data
;
448 TREE_CHAIN (f_data
) = f_per
;
449 TREE_CHAIN (f_per
) = f_lsda
;
450 TREE_CHAIN (f_lsda
) = f_jbuf
;
452 layout_type (sjlj_fc_type_node
);
454 /* Cache the interesting field offsets so that we have
455 easy access from rtl. */
456 sjlj_fc_call_site_ofs
457 = (tree_low_cst (DECL_FIELD_OFFSET (f_cs
), 1)
458 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_cs
), 1) / BITS_PER_UNIT
);
460 = (tree_low_cst (DECL_FIELD_OFFSET (f_data
), 1)
461 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_data
), 1) / BITS_PER_UNIT
);
462 sjlj_fc_personality_ofs
463 = (tree_low_cst (DECL_FIELD_OFFSET (f_per
), 1)
464 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_per
), 1) / BITS_PER_UNIT
);
466 = (tree_low_cst (DECL_FIELD_OFFSET (f_lsda
), 1)
467 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_lsda
), 1) / BITS_PER_UNIT
);
469 = (tree_low_cst (DECL_FIELD_OFFSET (f_jbuf
), 1)
470 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_jbuf
), 1) / BITS_PER_UNIT
);
475 init_eh_for_function ()
477 cfun
->eh
= (struct eh_status
*)
478 ggc_alloc_cleared (sizeof (struct eh_status
));
481 /* Start an exception handling region. All instructions emitted
482 after this point are considered to be part of the region until
483 expand_eh_region_end is invoked. */
486 expand_eh_region_start ()
488 struct eh_region
*new_region
;
489 struct eh_region
*cur_region
;
495 /* Insert a new blank region as a leaf in the tree. */
496 new_region
= (struct eh_region
*) ggc_alloc_cleared (sizeof (*new_region
));
497 cur_region
= cfun
->eh
->cur_region
;
498 new_region
->outer
= cur_region
;
501 new_region
->next_peer
= cur_region
->inner
;
502 cur_region
->inner
= new_region
;
506 new_region
->next_peer
= cfun
->eh
->region_tree
;
507 cfun
->eh
->region_tree
= new_region
;
509 cfun
->eh
->cur_region
= new_region
;
511 /* Create a note marking the start of this region. */
512 new_region
->region_number
= ++cfun
->eh
->last_region_number
;
513 note
= emit_note (NOTE_INSN_EH_REGION_BEG
);
514 NOTE_EH_HANDLER (note
) = new_region
->region_number
;
517 /* Common code to end a region. Returns the region just ended. */
519 static struct eh_region
*
520 expand_eh_region_end ()
522 struct eh_region
*cur_region
= cfun
->eh
->cur_region
;
525 /* Create a note marking the end of this region. */
526 note
= emit_note (NOTE_INSN_EH_REGION_END
);
527 NOTE_EH_HANDLER (note
) = cur_region
->region_number
;
530 cfun
->eh
->cur_region
= cur_region
->outer
;
535 /* End an exception handling region for a cleanup. HANDLER is an
536 expression to expand for the cleanup. */
539 expand_eh_region_end_cleanup (handler
)
542 struct eh_region
*region
;
543 tree protect_cleanup_actions
;
550 region
= expand_eh_region_end ();
551 region
->type
= ERT_CLEANUP
;
552 region
->label
= gen_label_rtx ();
553 region
->u
.cleanup
.exp
= handler
;
554 region
->u
.cleanup
.prev_try
= cfun
->eh
->try_region
;
556 around_label
= gen_label_rtx ();
557 emit_jump (around_label
);
559 emit_label (region
->label
);
561 if (flag_non_call_exceptions
|| region
->may_contain_throw
)
563 /* Give the language a chance to specify an action to be taken if an
564 exception is thrown that would propagate out of the HANDLER. */
565 protect_cleanup_actions
566 = (lang_protect_cleanup_actions
567 ? (*lang_protect_cleanup_actions
) ()
570 if (protect_cleanup_actions
)
571 expand_eh_region_start ();
573 /* In case this cleanup involves an inline destructor with a try block in
574 it, we need to save the EH return data registers around it. */
575 data_save
[0] = gen_reg_rtx (ptr_mode
);
576 emit_move_insn (data_save
[0], get_exception_pointer (cfun
));
577 data_save
[1] = gen_reg_rtx (word_mode
);
578 emit_move_insn (data_save
[1], get_exception_filter (cfun
));
580 expand_expr (handler
, const0_rtx
, VOIDmode
, 0);
582 emit_move_insn (cfun
->eh
->exc_ptr
, data_save
[0]);
583 emit_move_insn (cfun
->eh
->filter
, data_save
[1]);
585 if (protect_cleanup_actions
)
586 expand_eh_region_end_must_not_throw (protect_cleanup_actions
);
588 /* We need any stack adjustment complete before the around_label. */
589 do_pending_stack_adjust ();
592 /* We delay the generation of the _Unwind_Resume until we generate
593 landing pads. We emit a marker here so as to get good control
594 flow data in the meantime. */
596 = emit_jump_insn (gen_rtx_RESX (VOIDmode
, region
->region_number
));
599 emit_label (around_label
);
602 /* End an exception handling region for a try block, and prepares
603 for subsequent calls to expand_start_catch. */
606 expand_start_all_catch ()
608 struct eh_region
*region
;
613 region
= expand_eh_region_end ();
614 region
->type
= ERT_TRY
;
615 region
->u
.try.prev_try
= cfun
->eh
->try_region
;
616 region
->u
.try.continue_label
= gen_label_rtx ();
618 cfun
->eh
->try_region
= region
;
620 emit_jump (region
->u
.try.continue_label
);
623 /* Begin a catch clause. TYPE is the type caught, a list of such types, or
624 null if this is a catch-all clause. Providing a type list enables to
625 associate the catch region with potentially several exception types, which
626 is useful e.g. for Ada. */
629 expand_start_catch (type_or_list
)
632 struct eh_region
*t
, *c
, *l
;
638 type_list
= type_or_list
;
642 /* Ensure to always end up with a type list to normalize further
643 processing, then register each type against the runtime types
647 if (TREE_CODE (type_or_list
) != TREE_LIST
)
648 type_list
= tree_cons (NULL_TREE
, type_or_list
, NULL_TREE
);
650 type_node
= type_list
;
651 for (; type_node
; type_node
= TREE_CHAIN (type_node
))
652 add_type_for_runtime (TREE_VALUE (type_node
));
655 expand_eh_region_start ();
657 t
= cfun
->eh
->try_region
;
658 c
= cfun
->eh
->cur_region
;
660 c
->u
.catch.type_list
= type_list
;
661 c
->label
= gen_label_rtx ();
663 l
= t
->u
.try.last_catch
;
664 c
->u
.catch.prev_catch
= l
;
666 l
->u
.catch.next_catch
= c
;
669 t
->u
.try.last_catch
= c
;
671 emit_label (c
->label
);
674 /* End a catch clause. Control will resume after the try/catch block. */
679 struct eh_region
*try_region
;
684 expand_eh_region_end ();
685 try_region
= cfun
->eh
->try_region
;
687 emit_jump (try_region
->u
.try.continue_label
);
690 /* End a sequence of catch handlers for a try block. */
693 expand_end_all_catch ()
695 struct eh_region
*try_region
;
700 try_region
= cfun
->eh
->try_region
;
701 cfun
->eh
->try_region
= try_region
->u
.try.prev_try
;
703 emit_label (try_region
->u
.try.continue_label
);
706 /* End an exception region for an exception type filter. ALLOWED is a
707 TREE_LIST of types to be matched by the runtime. FAILURE is an
708 expression to invoke if a mismatch occurs.
710 ??? We could use these semantics for calls to rethrow, too; if we can
711 see the surrounding catch clause, we know that the exception we're
712 rethrowing satisfies the "filter" of the catch type. */
715 expand_eh_region_end_allowed (allowed
, failure
)
716 tree allowed
, failure
;
718 struct eh_region
*region
;
724 region
= expand_eh_region_end ();
725 region
->type
= ERT_ALLOWED_EXCEPTIONS
;
726 region
->u
.allowed
.type_list
= allowed
;
727 region
->label
= gen_label_rtx ();
729 for (; allowed
; allowed
= TREE_CHAIN (allowed
))
730 add_type_for_runtime (TREE_VALUE (allowed
));
732 /* We must emit the call to FAILURE here, so that if this function
733 throws a different exception, that it will be processed by the
736 around_label
= gen_label_rtx ();
737 emit_jump (around_label
);
739 emit_label (region
->label
);
740 expand_expr (failure
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
741 /* We must adjust the stack before we reach the AROUND_LABEL because
742 the call to FAILURE does not occur on all paths to the
744 do_pending_stack_adjust ();
746 emit_label (around_label
);
749 /* End an exception region for a must-not-throw filter. FAILURE is an
750 expression invoke if an uncaught exception propagates this far.
752 This is conceptually identical to expand_eh_region_end_allowed with
753 an empty allowed list (if you passed "std::terminate" instead of
754 "__cxa_call_unexpected"), but they are represented differently in
758 expand_eh_region_end_must_not_throw (failure
)
761 struct eh_region
*region
;
767 region
= expand_eh_region_end ();
768 region
->type
= ERT_MUST_NOT_THROW
;
769 region
->label
= gen_label_rtx ();
771 /* We must emit the call to FAILURE here, so that if this function
772 throws a different exception, that it will be processed by the
775 around_label
= gen_label_rtx ();
776 emit_jump (around_label
);
778 emit_label (region
->label
);
779 expand_expr (failure
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
781 emit_label (around_label
);
784 /* End an exception region for a throw. No handling goes on here,
785 but it's the easiest way for the front-end to indicate what type
789 expand_eh_region_end_throw (type
)
792 struct eh_region
*region
;
797 region
= expand_eh_region_end ();
798 region
->type
= ERT_THROW
;
799 region
->u
.throw.type
= type
;
802 /* End a fixup region. Within this region the cleanups for the immediately
803 enclosing region are _not_ run. This is used for goto cleanup to avoid
804 destroying an object twice.
806 This would be an extraordinarily simple prospect, were it not for the
807 fact that we don't actually know what the immediately enclosing region
808 is. This surprising fact is because expand_cleanups is currently
809 generating a sequence that it will insert somewhere else. We collect
810 the proper notion of "enclosing" in convert_from_eh_region_ranges. */
813 expand_eh_region_end_fixup (handler
)
816 struct eh_region
*fixup
;
821 fixup
= expand_eh_region_end ();
822 fixup
->type
= ERT_FIXUP
;
823 fixup
->u
.fixup
.cleanup_exp
= handler
;
826 /* Note that the current EH region (if any) may contain a throw, or a
827 call to a function which itself may contain a throw. */
830 note_eh_region_may_contain_throw ()
832 struct eh_region
*region
;
834 region
= cfun
->eh
->cur_region
;
835 while (region
&& !region
->may_contain_throw
)
837 region
->may_contain_throw
= 1;
838 region
= region
->outer
;
842 /* Return an rtl expression for a pointer to the exception object
846 get_exception_pointer (fun
)
847 struct function
*fun
;
849 rtx exc_ptr
= fun
->eh
->exc_ptr
;
850 if (fun
== cfun
&& ! exc_ptr
)
852 exc_ptr
= gen_reg_rtx (ptr_mode
);
853 fun
->eh
->exc_ptr
= exc_ptr
;
858 /* Return an rtl expression for the exception dispatch filter
862 get_exception_filter (fun
)
863 struct function
*fun
;
865 rtx filter
= fun
->eh
->filter
;
866 if (fun
== cfun
&& ! filter
)
868 filter
= gen_reg_rtx (word_mode
);
869 fun
->eh
->filter
= filter
;
874 /* This section is for the exception handling specific optimization pass. */
876 /* Random access the exception region tree. It's just as simple to
877 collect the regions this way as in expand_eh_region_start, but
878 without having to realloc memory. */
881 collect_eh_region_array ()
883 struct eh_region
**array
, *i
;
885 i
= cfun
->eh
->region_tree
;
889 array
= ggc_alloc_cleared ((cfun
->eh
->last_region_number
+ 1)
891 cfun
->eh
->region_array
= array
;
895 array
[i
->region_number
] = i
;
897 /* If there are sub-regions, process them. */
900 /* If there are peers, process them. */
901 else if (i
->next_peer
)
903 /* Otherwise, step back up the tree to the next peer. */
910 } while (i
->next_peer
== NULL
);
917 resolve_fixup_regions ()
919 int i
, j
, n
= cfun
->eh
->last_region_number
;
921 for (i
= 1; i
<= n
; ++i
)
923 struct eh_region
*fixup
= cfun
->eh
->region_array
[i
];
924 struct eh_region
*cleanup
= 0;
926 if (! fixup
|| fixup
->type
!= ERT_FIXUP
)
929 for (j
= 1; j
<= n
; ++j
)
931 cleanup
= cfun
->eh
->region_array
[j
];
932 if (cleanup
&& cleanup
->type
== ERT_CLEANUP
933 && cleanup
->u
.cleanup
.exp
== fixup
->u
.fixup
.cleanup_exp
)
939 fixup
->u
.fixup
.real_region
= cleanup
->outer
;
943 /* Now that we've discovered what region actually encloses a fixup,
944 we can shuffle pointers and remove them from the tree. */
947 remove_fixup_regions ()
951 struct eh_region
*fixup
;
953 /* Walk the insn chain and adjust the REG_EH_REGION numbers
954 for instructions referencing fixup regions. This is only
955 strictly necessary for fixup regions with no parent, but
956 doesn't hurt to do it for all regions. */
957 for (insn
= get_insns(); insn
; insn
= NEXT_INSN (insn
))
959 && (note
= find_reg_note (insn
, REG_EH_REGION
, NULL
))
960 && INTVAL (XEXP (note
, 0)) > 0
961 && (fixup
= cfun
->eh
->region_array
[INTVAL (XEXP (note
, 0))])
962 && fixup
->type
== ERT_FIXUP
)
964 if (fixup
->u
.fixup
.real_region
)
965 XEXP (note
, 0) = GEN_INT (fixup
->u
.fixup
.real_region
->region_number
);
967 remove_note (insn
, note
);
970 /* Remove the fixup regions from the tree. */
971 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
973 fixup
= cfun
->eh
->region_array
[i
];
977 /* Allow GC to maybe free some memory. */
978 if (fixup
->type
== ERT_CLEANUP
)
979 fixup
->u
.cleanup
.exp
= NULL_TREE
;
981 if (fixup
->type
!= ERT_FIXUP
)
986 struct eh_region
*parent
, *p
, **pp
;
988 parent
= fixup
->u
.fixup
.real_region
;
990 /* Fix up the children's parent pointers; find the end of
992 for (p
= fixup
->inner
; ; p
= p
->next_peer
)
999 /* In the tree of cleanups, only outer-inner ordering matters.
1000 So link the children back in anywhere at the correct level. */
1002 pp
= &parent
->inner
;
1004 pp
= &cfun
->eh
->region_tree
;
1007 fixup
->inner
= NULL
;
1010 remove_eh_handler (fixup
);
1014 /* Remove all regions whose labels are not reachable from insns. */
1017 remove_unreachable_regions (insns
)
1020 int i
, *uid_region_num
;
1022 struct eh_region
*r
;
1025 uid_region_num
= xcalloc (get_max_uid (), sizeof(int));
1026 reachable
= xcalloc (cfun
->eh
->last_region_number
+ 1, sizeof(bool));
1028 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1030 r
= cfun
->eh
->region_array
[i
];
1031 if (!r
|| r
->region_number
!= i
)
1036 if (uid_region_num
[INSN_UID (r
->resume
)])
1038 uid_region_num
[INSN_UID (r
->resume
)] = i
;
1042 if (uid_region_num
[INSN_UID (r
->label
)])
1044 uid_region_num
[INSN_UID (r
->label
)] = i
;
1046 if (r
->type
== ERT_TRY
&& r
->u
.try.continue_label
)
1048 if (uid_region_num
[INSN_UID (r
->u
.try.continue_label
)])
1050 uid_region_num
[INSN_UID (r
->u
.try.continue_label
)] = i
;
1054 for (insn
= insns
; insn
; insn
= NEXT_INSN (insn
))
1055 reachable
[uid_region_num
[INSN_UID (insn
)]] = true;
1057 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1059 r
= cfun
->eh
->region_array
[i
];
1060 if (r
&& r
->region_number
== i
&& !reachable
[i
])
1062 /* Don't remove ERT_THROW regions if their outer region
1064 if (r
->type
== ERT_THROW
1066 && reachable
[r
->outer
->region_number
])
1069 remove_eh_handler (r
);
1074 free (uid_region_num
);
1077 /* Turn NOTE_INSN_EH_REGION notes into REG_EH_REGION notes for each
1078 can_throw instruction in the region. */
1081 convert_from_eh_region_ranges_1 (pinsns
, orig_sp
, cur
)
1089 for (insn
= *pinsns
; insn
; insn
= next
)
1091 next
= NEXT_INSN (insn
);
1092 if (GET_CODE (insn
) == NOTE
)
1094 int kind
= NOTE_LINE_NUMBER (insn
);
1095 if (kind
== NOTE_INSN_EH_REGION_BEG
1096 || kind
== NOTE_INSN_EH_REGION_END
)
1098 if (kind
== NOTE_INSN_EH_REGION_BEG
)
1100 struct eh_region
*r
;
1103 cur
= NOTE_EH_HANDLER (insn
);
1105 r
= cfun
->eh
->region_array
[cur
];
1106 if (r
->type
== ERT_FIXUP
)
1108 r
= r
->u
.fixup
.real_region
;
1109 cur
= r
? r
->region_number
: 0;
1111 else if (r
->type
== ERT_CATCH
)
1114 cur
= r
? r
->region_number
: 0;
1120 /* Removing the first insn of a CALL_PLACEHOLDER sequence
1121 requires extra care to adjust sequence start. */
1122 if (insn
== *pinsns
)
1128 else if (INSN_P (insn
))
1131 && ! find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
)
1132 /* Calls can always potentially throw exceptions, unless
1133 they have a REG_EH_REGION note with a value of 0 or less.
1134 Which should be the only possible kind so far. */
1135 && (GET_CODE (insn
) == CALL_INSN
1136 /* If we wanted exceptions for non-call insns, then
1137 any may_trap_p instruction could throw. */
1138 || (flag_non_call_exceptions
1139 && GET_CODE (PATTERN (insn
)) != CLOBBER
1140 && GET_CODE (PATTERN (insn
)) != USE
1141 && may_trap_p (PATTERN (insn
)))))
1143 REG_NOTES (insn
) = alloc_EXPR_LIST (REG_EH_REGION
, GEN_INT (cur
),
1147 if (GET_CODE (insn
) == CALL_INSN
1148 && GET_CODE (PATTERN (insn
)) == CALL_PLACEHOLDER
)
1150 convert_from_eh_region_ranges_1 (&XEXP (PATTERN (insn
), 0),
1152 convert_from_eh_region_ranges_1 (&XEXP (PATTERN (insn
), 1),
1154 convert_from_eh_region_ranges_1 (&XEXP (PATTERN (insn
), 2),
1165 convert_from_eh_region_ranges ()
1170 collect_eh_region_array ();
1171 resolve_fixup_regions ();
1173 stack
= xmalloc (sizeof (int) * (cfun
->eh
->last_region_number
+ 1));
1174 insns
= get_insns ();
1175 convert_from_eh_region_ranges_1 (&insns
, stack
, 0);
1178 remove_fixup_regions ();
1179 remove_unreachable_regions (insns
);
1183 add_ehl_entry (label
, region
)
1185 struct eh_region
*region
;
1187 struct ehl_map_entry
**slot
, *entry
;
1189 LABEL_PRESERVE_P (label
) = 1;
1191 entry
= (struct ehl_map_entry
*) ggc_alloc (sizeof (*entry
));
1192 entry
->label
= label
;
1193 entry
->region
= region
;
1195 slot
= (struct ehl_map_entry
**)
1196 htab_find_slot (cfun
->eh
->exception_handler_label_map
, entry
, INSERT
);
1198 /* Before landing pad creation, each exception handler has its own
1199 label. After landing pad creation, the exception handlers may
1200 share landing pads. This is ok, since maybe_remove_eh_handler
1201 only requires the 1-1 mapping before landing pad creation. */
1202 if (*slot
&& !cfun
->eh
->built_landing_pads
)
1209 find_exception_handler_labels ()
1213 if (cfun
->eh
->exception_handler_label_map
)
1214 htab_empty (cfun
->eh
->exception_handler_label_map
);
1217 /* ??? The expansion factor here (3/2) must be greater than the htab
1218 occupancy factor (4/3) to avoid unnecessary resizing. */
1219 cfun
->eh
->exception_handler_label_map
1220 = htab_create_ggc (cfun
->eh
->last_region_number
* 3 / 2,
1221 ehl_hash
, ehl_eq
, NULL
);
1224 if (cfun
->eh
->region_tree
== NULL
)
1227 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1229 struct eh_region
*region
= cfun
->eh
->region_array
[i
];
1232 if (! region
|| region
->region_number
!= i
)
1234 if (cfun
->eh
->built_landing_pads
)
1235 lab
= region
->landing_pad
;
1237 lab
= region
->label
;
1240 add_ehl_entry (lab
, region
);
1243 /* For sjlj exceptions, need the return label to remain live until
1244 after landing pad generation. */
1245 if (USING_SJLJ_EXCEPTIONS
&& ! cfun
->eh
->built_landing_pads
)
1246 add_ehl_entry (return_label
, NULL
);
1250 current_function_has_exception_handlers ()
1254 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1256 struct eh_region
*region
= cfun
->eh
->region_array
[i
];
1258 if (! region
|| region
->region_number
!= i
)
1260 if (region
->type
!= ERT_THROW
)
1267 static struct eh_region
*
1268 duplicate_eh_region_1 (o
, map
)
1269 struct eh_region
*o
;
1270 struct inline_remap
*map
;
1273 = (struct eh_region
*) ggc_alloc_cleared (sizeof (struct eh_region
));
1275 n
->region_number
= o
->region_number
+ cfun
->eh
->last_region_number
;
1281 case ERT_MUST_NOT_THROW
:
1285 if (o
->u
.try.continue_label
)
1286 n
->u
.try.continue_label
1287 = get_label_from_map (map
,
1288 CODE_LABEL_NUMBER (o
->u
.try.continue_label
));
1292 n
->u
.catch.type_list
= o
->u
.catch.type_list
;
1295 case ERT_ALLOWED_EXCEPTIONS
:
1296 n
->u
.allowed
.type_list
= o
->u
.allowed
.type_list
;
1300 n
->u
.throw.type
= o
->u
.throw.type
;
1307 n
->label
= get_label_from_map (map
, CODE_LABEL_NUMBER (o
->label
));
1310 n
->resume
= map
->insn_map
[INSN_UID (o
->resume
)];
1311 if (n
->resume
== NULL
)
1319 duplicate_eh_region_2 (o
, n_array
)
1320 struct eh_region
*o
;
1321 struct eh_region
**n_array
;
1323 struct eh_region
*n
= n_array
[o
->region_number
];
1328 n
->u
.try.catch = n_array
[o
->u
.try.catch->region_number
];
1329 n
->u
.try.last_catch
= n_array
[o
->u
.try.last_catch
->region_number
];
1333 if (o
->u
.catch.next_catch
)
1334 n
->u
.catch.next_catch
= n_array
[o
->u
.catch.next_catch
->region_number
];
1335 if (o
->u
.catch.prev_catch
)
1336 n
->u
.catch.prev_catch
= n_array
[o
->u
.catch.prev_catch
->region_number
];
1344 n
->outer
= n_array
[o
->outer
->region_number
];
1346 n
->inner
= n_array
[o
->inner
->region_number
];
1348 n
->next_peer
= n_array
[o
->next_peer
->region_number
];
1352 duplicate_eh_regions (ifun
, map
)
1353 struct function
*ifun
;
1354 struct inline_remap
*map
;
1356 int ifun_last_region_number
= ifun
->eh
->last_region_number
;
1357 struct eh_region
**n_array
, *root
, *cur
;
1360 if (ifun_last_region_number
== 0)
1363 n_array
= xcalloc (ifun_last_region_number
+ 1, sizeof (*n_array
));
1365 for (i
= 1; i
<= ifun_last_region_number
; ++i
)
1367 cur
= ifun
->eh
->region_array
[i
];
1368 if (!cur
|| cur
->region_number
!= i
)
1370 n_array
[i
] = duplicate_eh_region_1 (cur
, map
);
1372 for (i
= 1; i
<= ifun_last_region_number
; ++i
)
1374 cur
= ifun
->eh
->region_array
[i
];
1375 if (!cur
|| cur
->region_number
!= i
)
1377 duplicate_eh_region_2 (cur
, n_array
);
1380 root
= n_array
[ifun
->eh
->region_tree
->region_number
];
1381 cur
= cfun
->eh
->cur_region
;
1384 struct eh_region
*p
= cur
->inner
;
1387 while (p
->next_peer
)
1389 p
->next_peer
= root
;
1394 for (i
= 1; i
<= ifun_last_region_number
; ++i
)
1395 if (n_array
[i
] && n_array
[i
]->outer
== NULL
)
1396 n_array
[i
]->outer
= cur
;
1400 struct eh_region
*p
= cfun
->eh
->region_tree
;
1403 while (p
->next_peer
)
1405 p
->next_peer
= root
;
1408 cfun
->eh
->region_tree
= root
;
1413 i
= cfun
->eh
->last_region_number
;
1414 cfun
->eh
->last_region_number
= i
+ ifun_last_region_number
;
1420 t2r_eq (pentry
, pdata
)
1424 tree entry
= (tree
) pentry
;
1425 tree data
= (tree
) pdata
;
1427 return TREE_PURPOSE (entry
) == data
;
1434 tree entry
= (tree
) pentry
;
1435 return TYPE_HASH (TREE_PURPOSE (entry
));
1439 add_type_for_runtime (type
)
1444 slot
= (tree
*) htab_find_slot_with_hash (type_to_runtime_map
, type
,
1445 TYPE_HASH (type
), INSERT
);
1448 tree runtime
= (*lang_eh_runtime_type
) (type
);
1449 *slot
= tree_cons (type
, runtime
, NULL_TREE
);
1454 lookup_type_for_runtime (type
)
1459 slot
= (tree
*) htab_find_slot_with_hash (type_to_runtime_map
, type
,
1460 TYPE_HASH (type
), NO_INSERT
);
1462 /* We should have always inserted the data earlier. */
1463 return TREE_VALUE (*slot
);
1467 /* Represent an entry in @TTypes for either catch actions
1468 or exception filter actions. */
1469 struct ttypes_filter
GTY(())
1475 /* Compare ENTRY (a ttypes_filter entry in the hash table) with DATA
1476 (a tree) for a @TTypes type node we are thinking about adding. */
1479 ttypes_filter_eq (pentry
, pdata
)
1483 const struct ttypes_filter
*entry
= (const struct ttypes_filter
*) pentry
;
1484 tree data
= (tree
) pdata
;
1486 return entry
->t
== data
;
1490 ttypes_filter_hash (pentry
)
1493 const struct ttypes_filter
*entry
= (const struct ttypes_filter
*) pentry
;
1494 return TYPE_HASH (entry
->t
);
1497 /* Compare ENTRY with DATA (both struct ttypes_filter) for a @TTypes
1498 exception specification list we are thinking about adding. */
1499 /* ??? Currently we use the type lists in the order given. Someone
1500 should put these in some canonical order. */
1503 ehspec_filter_eq (pentry
, pdata
)
1507 const struct ttypes_filter
*entry
= (const struct ttypes_filter
*) pentry
;
1508 const struct ttypes_filter
*data
= (const struct ttypes_filter
*) pdata
;
1510 return type_list_equal (entry
->t
, data
->t
);
1513 /* Hash function for exception specification lists. */
1516 ehspec_filter_hash (pentry
)
1519 const struct ttypes_filter
*entry
= (const struct ttypes_filter
*) pentry
;
1523 for (list
= entry
->t
; list
; list
= TREE_CHAIN (list
))
1524 h
= (h
<< 5) + (h
>> 27) + TYPE_HASH (TREE_VALUE (list
));
1528 /* Add TYPE to cfun->eh->ttype_data, using TYPES_HASH to speed
1529 up the search. Return the filter value to be used. */
1532 add_ttypes_entry (ttypes_hash
, type
)
1536 struct ttypes_filter
**slot
, *n
;
1538 slot
= (struct ttypes_filter
**)
1539 htab_find_slot_with_hash (ttypes_hash
, type
, TYPE_HASH (type
), INSERT
);
1541 if ((n
= *slot
) == NULL
)
1543 /* Filter value is a 1 based table index. */
1545 n
= (struct ttypes_filter
*) xmalloc (sizeof (*n
));
1547 n
->filter
= VARRAY_ACTIVE_SIZE (cfun
->eh
->ttype_data
) + 1;
1550 VARRAY_PUSH_TREE (cfun
->eh
->ttype_data
, type
);
1556 /* Add LIST to cfun->eh->ehspec_data, using EHSPEC_HASH and TYPES_HASH
1557 to speed up the search. Return the filter value to be used. */
1560 add_ehspec_entry (ehspec_hash
, ttypes_hash
, list
)
1565 struct ttypes_filter
**slot
, *n
;
1566 struct ttypes_filter dummy
;
1569 slot
= (struct ttypes_filter
**)
1570 htab_find_slot (ehspec_hash
, &dummy
, INSERT
);
1572 if ((n
= *slot
) == NULL
)
1574 /* Filter value is a -1 based byte index into a uleb128 buffer. */
1576 n
= (struct ttypes_filter
*) xmalloc (sizeof (*n
));
1578 n
->filter
= -(VARRAY_ACTIVE_SIZE (cfun
->eh
->ehspec_data
) + 1);
1581 /* Look up each type in the list and encode its filter
1582 value as a uleb128. Terminate the list with 0. */
1583 for (; list
; list
= TREE_CHAIN (list
))
1584 push_uleb128 (&cfun
->eh
->ehspec_data
,
1585 add_ttypes_entry (ttypes_hash
, TREE_VALUE (list
)));
1586 VARRAY_PUSH_UCHAR (cfun
->eh
->ehspec_data
, 0);
1592 /* Generate the action filter values to be used for CATCH and
1593 ALLOWED_EXCEPTIONS regions. When using dwarf2 exception regions,
1594 we use lots of landing pads, and so every type or list can share
1595 the same filter value, which saves table space. */
1598 assign_filter_values ()
1601 htab_t ttypes
, ehspec
;
1603 VARRAY_TREE_INIT (cfun
->eh
->ttype_data
, 16, "ttype_data");
1604 VARRAY_UCHAR_INIT (cfun
->eh
->ehspec_data
, 64, "ehspec_data");
1606 ttypes
= htab_create (31, ttypes_filter_hash
, ttypes_filter_eq
, free
);
1607 ehspec
= htab_create (31, ehspec_filter_hash
, ehspec_filter_eq
, free
);
1609 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1611 struct eh_region
*r
= cfun
->eh
->region_array
[i
];
1613 /* Mind we don't process a region more than once. */
1614 if (!r
|| r
->region_number
!= i
)
1620 /* Whatever type_list is (NULL or true list), we build a list
1621 of filters for the region. */
1622 r
->u
.catch.filter_list
= NULL_TREE
;
1624 if (r
->u
.catch.type_list
!= NULL
)
1626 /* Get a filter value for each of the types caught and store
1627 them in the region's dedicated list. */
1628 tree tp_node
= r
->u
.catch.type_list
;
1630 for (;tp_node
; tp_node
= TREE_CHAIN (tp_node
))
1632 int flt
= add_ttypes_entry (ttypes
, TREE_VALUE (tp_node
));
1633 tree flt_node
= build_int_2 (flt
, 0);
1635 r
->u
.catch.filter_list
1636 = tree_cons (NULL_TREE
, flt_node
, r
->u
.catch.filter_list
);
1641 /* Get a filter value for the NULL list also since it will need
1642 an action record anyway. */
1643 int flt
= add_ttypes_entry (ttypes
, NULL
);
1644 tree flt_node
= build_int_2 (flt
, 0);
1646 r
->u
.catch.filter_list
1647 = tree_cons (NULL_TREE
, flt_node
, r
->u
.catch.filter_list
);
1652 case ERT_ALLOWED_EXCEPTIONS
:
1654 = add_ehspec_entry (ehspec
, ttypes
, r
->u
.allowed
.type_list
);
1662 htab_delete (ttypes
);
1663 htab_delete (ehspec
);
1666 /* Generate the code to actually handle exceptions, which will follow the
1670 build_post_landing_pads ()
1674 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1676 struct eh_region
*region
= cfun
->eh
->region_array
[i
];
1679 /* Mind we don't process a region more than once. */
1680 if (!region
|| region
->region_number
!= i
)
1683 switch (region
->type
)
1686 /* ??? Collect the set of all non-overlapping catch handlers
1687 all the way up the chain until blocked by a cleanup. */
1688 /* ??? Outer try regions can share landing pads with inner
1689 try regions if the types are completely non-overlapping,
1690 and there are no intervening cleanups. */
1692 region
->post_landing_pad
= gen_label_rtx ();
1696 emit_label (region
->post_landing_pad
);
1698 /* ??? It is mighty inconvenient to call back into the
1699 switch statement generation code in expand_end_case.
1700 Rapid prototyping sez a sequence of ifs. */
1702 struct eh_region
*c
;
1703 for (c
= region
->u
.try.catch; c
; c
= c
->u
.catch.next_catch
)
1705 if (c
->u
.catch.type_list
== NULL
)
1706 emit_jump (c
->label
);
1709 /* Need for one cmp/jump per type caught. Each type
1710 list entry has a matching entry in the filter list
1711 (see assign_filter_values). */
1712 tree tp_node
= c
->u
.catch.type_list
;
1713 tree flt_node
= c
->u
.catch.filter_list
;
1717 emit_cmp_and_jump_insns
1719 GEN_INT (tree_low_cst (TREE_VALUE (flt_node
), 0)),
1720 EQ
, NULL_RTX
, word_mode
, 0, c
->label
);
1722 tp_node
= TREE_CHAIN (tp_node
);
1723 flt_node
= TREE_CHAIN (flt_node
);
1729 /* We delay the generation of the _Unwind_Resume until we generate
1730 landing pads. We emit a marker here so as to get good control
1731 flow data in the meantime. */
1733 = emit_jump_insn (gen_rtx_RESX (VOIDmode
, region
->region_number
));
1739 emit_insn_before (seq
, region
->u
.try.catch->label
);
1742 case ERT_ALLOWED_EXCEPTIONS
:
1743 region
->post_landing_pad
= gen_label_rtx ();
1747 emit_label (region
->post_landing_pad
);
1749 emit_cmp_and_jump_insns (cfun
->eh
->filter
,
1750 GEN_INT (region
->u
.allowed
.filter
),
1751 EQ
, NULL_RTX
, word_mode
, 0, region
->label
);
1753 /* We delay the generation of the _Unwind_Resume until we generate
1754 landing pads. We emit a marker here so as to get good control
1755 flow data in the meantime. */
1757 = emit_jump_insn (gen_rtx_RESX (VOIDmode
, region
->region_number
));
1763 emit_insn_before (seq
, region
->label
);
1767 case ERT_MUST_NOT_THROW
:
1768 region
->post_landing_pad
= region
->label
;
1773 /* Nothing to do. */
1782 /* Replace RESX patterns with jumps to the next handler if any, or calls to
1783 _Unwind_Resume otherwise. */
1786 connect_post_landing_pads ()
1790 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1792 struct eh_region
*region
= cfun
->eh
->region_array
[i
];
1793 struct eh_region
*outer
;
1796 /* Mind we don't process a region more than once. */
1797 if (!region
|| region
->region_number
!= i
)
1800 /* If there is no RESX, or it has been deleted by flow, there's
1801 nothing to fix up. */
1802 if (! region
->resume
|| INSN_DELETED_P (region
->resume
))
1805 /* Search for another landing pad in this function. */
1806 for (outer
= region
->outer
; outer
; outer
= outer
->outer
)
1807 if (outer
->post_landing_pad
)
1813 emit_jump (outer
->post_landing_pad
);
1815 emit_library_call (unwind_resume_libfunc
, LCT_THROW
,
1816 VOIDmode
, 1, cfun
->eh
->exc_ptr
, ptr_mode
);
1820 emit_insn_before (seq
, region
->resume
);
1821 delete_insn (region
->resume
);
1827 dw2_build_landing_pads ()
1832 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1834 struct eh_region
*region
= cfun
->eh
->region_array
[i
];
1836 bool clobbers_hard_regs
= false;
1838 /* Mind we don't process a region more than once. */
1839 if (!region
|| region
->region_number
!= i
)
1842 if (region
->type
!= ERT_CLEANUP
1843 && region
->type
!= ERT_TRY
1844 && region
->type
!= ERT_ALLOWED_EXCEPTIONS
)
1849 region
->landing_pad
= gen_label_rtx ();
1850 emit_label (region
->landing_pad
);
1852 #ifdef HAVE_exception_receiver
1853 if (HAVE_exception_receiver
)
1854 emit_insn (gen_exception_receiver ());
1857 #ifdef HAVE_nonlocal_goto_receiver
1858 if (HAVE_nonlocal_goto_receiver
)
1859 emit_insn (gen_nonlocal_goto_receiver ());
1864 /* If the eh_return data registers are call-saved, then we
1865 won't have considered them clobbered from the call that
1866 threw. Kill them now. */
1869 unsigned r
= EH_RETURN_DATA_REGNO (j
);
1870 if (r
== INVALID_REGNUM
)
1872 if (! call_used_regs
[r
])
1874 emit_insn (gen_rtx_CLOBBER (VOIDmode
, gen_rtx_REG (Pmode
, r
)));
1875 clobbers_hard_regs
= true;
1879 if (clobbers_hard_regs
)
1881 /* @@@ This is a kludge. Not all machine descriptions define a
1882 blockage insn, but we must not allow the code we just generated
1883 to be reordered by scheduling. So emit an ASM_INPUT to act as
1885 emit_insn (gen_rtx_ASM_INPUT (VOIDmode
, ""));
1888 emit_move_insn (cfun
->eh
->exc_ptr
,
1889 gen_rtx_REG (ptr_mode
, EH_RETURN_DATA_REGNO (0)));
1890 emit_move_insn (cfun
->eh
->filter
,
1891 gen_rtx_REG (word_mode
, EH_RETURN_DATA_REGNO (1)));
1896 emit_insn_before (seq
, region
->post_landing_pad
);
1903 int directly_reachable
;
1906 int call_site_index
;
1910 sjlj_find_directly_reachable_regions (lp_info
)
1911 struct sjlj_lp_info
*lp_info
;
1914 bool found_one
= false;
1916 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
1918 struct eh_region
*region
;
1919 enum reachable_code rc
;
1923 if (! INSN_P (insn
))
1926 note
= find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
);
1927 if (!note
|| INTVAL (XEXP (note
, 0)) <= 0)
1930 region
= cfun
->eh
->region_array
[INTVAL (XEXP (note
, 0))];
1932 type_thrown
= NULL_TREE
;
1933 if (region
->type
== ERT_THROW
)
1935 type_thrown
= region
->u
.throw.type
;
1936 region
= region
->outer
;
1939 /* Find the first containing region that might handle the exception.
1940 That's the landing pad to which we will transfer control. */
1941 rc
= RNL_NOT_CAUGHT
;
1942 for (; region
; region
= region
->outer
)
1944 rc
= reachable_next_level (region
, type_thrown
, 0);
1945 if (rc
!= RNL_NOT_CAUGHT
)
1948 if (rc
== RNL_MAYBE_CAUGHT
|| rc
== RNL_CAUGHT
)
1950 lp_info
[region
->region_number
].directly_reachable
= 1;
1959 sjlj_assign_call_site_values (dispatch_label
, lp_info
)
1961 struct sjlj_lp_info
*lp_info
;
1966 /* First task: build the action table. */
1968 VARRAY_UCHAR_INIT (cfun
->eh
->action_record_data
, 64, "action_record_data");
1969 ar_hash
= htab_create (31, action_record_hash
, action_record_eq
, free
);
1971 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1972 if (lp_info
[i
].directly_reachable
)
1974 struct eh_region
*r
= cfun
->eh
->region_array
[i
];
1975 r
->landing_pad
= dispatch_label
;
1976 lp_info
[i
].action_index
= collect_one_action_chain (ar_hash
, r
);
1977 if (lp_info
[i
].action_index
!= -1)
1978 cfun
->uses_eh_lsda
= 1;
1981 htab_delete (ar_hash
);
1983 /* Next: assign dispatch values. In dwarf2 terms, this would be the
1984 landing pad label for the region. For sjlj though, there is one
1985 common landing pad from which we dispatch to the post-landing pads.
1987 A region receives a dispatch index if it is directly reachable
1988 and requires in-function processing. Regions that share post-landing
1989 pads may share dispatch indices. */
1990 /* ??? Post-landing pad sharing doesn't actually happen at the moment
1991 (see build_post_landing_pads) so we don't bother checking for it. */
1994 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1995 if (lp_info
[i
].directly_reachable
)
1996 lp_info
[i
].dispatch_index
= index
++;
1998 /* Finally: assign call-site values. If dwarf2 terms, this would be
1999 the region number assigned by convert_to_eh_region_ranges, but
2000 handles no-action and must-not-throw differently. */
2003 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
2004 if (lp_info
[i
].directly_reachable
)
2006 int action
= lp_info
[i
].action_index
;
2008 /* Map must-not-throw to otherwise unused call-site index 0. */
2011 /* Map no-action to otherwise unused call-site index -1. */
2012 else if (action
== -1)
2014 /* Otherwise, look it up in the table. */
2016 index
= add_call_site (GEN_INT (lp_info
[i
].dispatch_index
), action
);
2018 lp_info
[i
].call_site_index
= index
;
2023 sjlj_mark_call_sites (lp_info
)
2024 struct sjlj_lp_info
*lp_info
;
2026 int last_call_site
= -2;
2029 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
2031 struct eh_region
*region
;
2033 rtx note
, before
, p
;
2035 /* Reset value tracking at extended basic block boundaries. */
2036 if (GET_CODE (insn
) == CODE_LABEL
)
2037 last_call_site
= -2;
2039 if (! INSN_P (insn
))
2042 note
= find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
);
2045 /* Calls (and trapping insns) without notes are outside any
2046 exception handling region in this function. Mark them as
2048 if (GET_CODE (insn
) == CALL_INSN
2049 || (flag_non_call_exceptions
2050 && may_trap_p (PATTERN (insn
))))
2051 this_call_site
= -1;
2057 /* Calls that are known to not throw need not be marked. */
2058 if (INTVAL (XEXP (note
, 0)) <= 0)
2061 region
= cfun
->eh
->region_array
[INTVAL (XEXP (note
, 0))];
2062 this_call_site
= lp_info
[region
->region_number
].call_site_index
;
2065 if (this_call_site
== last_call_site
)
2068 /* Don't separate a call from it's argument loads. */
2070 if (GET_CODE (insn
) == CALL_INSN
)
2071 before
= find_first_parameter_load (insn
, NULL_RTX
);
2074 mem
= adjust_address (cfun
->eh
->sjlj_fc
, TYPE_MODE (integer_type_node
),
2075 sjlj_fc_call_site_ofs
);
2076 emit_move_insn (mem
, GEN_INT (this_call_site
));
2080 emit_insn_before (p
, before
);
2081 last_call_site
= this_call_site
;
2085 /* Construct the SjLj_Function_Context. */
2088 sjlj_emit_function_enter (dispatch_label
)
2091 rtx fn_begin
, fc
, mem
, seq
;
2093 fc
= cfun
->eh
->sjlj_fc
;
2097 /* We're storing this libcall's address into memory instead of
2098 calling it directly. Thus, we must call assemble_external_libcall
2099 here, as we can not depend on emit_library_call to do it for us. */
2100 assemble_external_libcall (eh_personality_libfunc
);
2101 mem
= adjust_address (fc
, Pmode
, sjlj_fc_personality_ofs
);
2102 emit_move_insn (mem
, eh_personality_libfunc
);
2104 mem
= adjust_address (fc
, Pmode
, sjlj_fc_lsda_ofs
);
2105 if (cfun
->uses_eh_lsda
)
2108 ASM_GENERATE_INTERNAL_LABEL (buf
, "LLSDA", current_function_funcdef_no
);
2109 emit_move_insn (mem
, gen_rtx_SYMBOL_REF (Pmode
, ggc_strdup (buf
)));
2112 emit_move_insn (mem
, const0_rtx
);
2114 #ifdef DONT_USE_BUILTIN_SETJMP
2117 x
= emit_library_call_value (setjmp_libfunc
, NULL_RTX
, LCT_RETURNS_TWICE
,
2118 TYPE_MODE (integer_type_node
), 1,
2119 plus_constant (XEXP (fc
, 0),
2120 sjlj_fc_jbuf_ofs
), Pmode
);
2122 note
= emit_note (NOTE_INSN_EXPECTED_VALUE
);
2123 NOTE_EXPECTED_VALUE (note
) = gen_rtx_EQ (VOIDmode
, x
, const0_rtx
);
2125 emit_cmp_and_jump_insns (x
, const0_rtx
, NE
, 0,
2126 TYPE_MODE (integer_type_node
), 0, dispatch_label
);
2129 expand_builtin_setjmp_setup (plus_constant (XEXP (fc
, 0), sjlj_fc_jbuf_ofs
),
2133 emit_library_call (unwind_sjlj_register_libfunc
, LCT_NORMAL
, VOIDmode
,
2134 1, XEXP (fc
, 0), Pmode
);
2139 /* ??? Instead of doing this at the beginning of the function,
2140 do this in a block that is at loop level 0 and dominates all
2141 can_throw_internal instructions. */
2143 for (fn_begin
= get_insns (); ; fn_begin
= NEXT_INSN (fn_begin
))
2144 if (GET_CODE (fn_begin
) == NOTE
2145 && NOTE_LINE_NUMBER (fn_begin
) == NOTE_INSN_FUNCTION_BEG
)
2147 emit_insn_after (seq
, fn_begin
);
2150 /* Call back from expand_function_end to know where we should put
2151 the call to unwind_sjlj_unregister_libfunc if needed. */
2154 sjlj_emit_function_exit_after (after
)
2157 cfun
->eh
->sjlj_exit_after
= after
;
2161 sjlj_emit_function_exit ()
2167 emit_library_call (unwind_sjlj_unregister_libfunc
, LCT_NORMAL
, VOIDmode
,
2168 1, XEXP (cfun
->eh
->sjlj_fc
, 0), Pmode
);
2173 /* ??? Really this can be done in any block at loop level 0 that
2174 post-dominates all can_throw_internal instructions. This is
2175 the last possible moment. */
2177 emit_insn_after (seq
, cfun
->eh
->sjlj_exit_after
);
2181 sjlj_emit_dispatch_table (dispatch_label
, lp_info
)
2183 struct sjlj_lp_info
*lp_info
;
2185 int i
, first_reachable
;
2186 rtx mem
, dispatch
, seq
, fc
;
2188 fc
= cfun
->eh
->sjlj_fc
;
2192 emit_label (dispatch_label
);
2194 #ifndef DONT_USE_BUILTIN_SETJMP
2195 expand_builtin_setjmp_receiver (dispatch_label
);
2198 /* Load up dispatch index, exc_ptr and filter values from the
2199 function context. */
2200 mem
= adjust_address (fc
, TYPE_MODE (integer_type_node
),
2201 sjlj_fc_call_site_ofs
);
2202 dispatch
= copy_to_reg (mem
);
2204 mem
= adjust_address (fc
, word_mode
, sjlj_fc_data_ofs
);
2205 if (word_mode
!= Pmode
)
2207 #ifdef POINTERS_EXTEND_UNSIGNED
2208 mem
= convert_memory_address (Pmode
, mem
);
2210 mem
= convert_to_mode (Pmode
, mem
, 0);
2213 emit_move_insn (cfun
->eh
->exc_ptr
, mem
);
2215 mem
= adjust_address (fc
, word_mode
, sjlj_fc_data_ofs
+ UNITS_PER_WORD
);
2216 emit_move_insn (cfun
->eh
->filter
, mem
);
2218 /* Jump to one of the directly reachable regions. */
2219 /* ??? This really ought to be using a switch statement. */
2221 first_reachable
= 0;
2222 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
2224 if (! lp_info
[i
].directly_reachable
)
2227 if (! first_reachable
)
2229 first_reachable
= i
;
2233 emit_cmp_and_jump_insns (dispatch
, GEN_INT (lp_info
[i
].dispatch_index
),
2234 EQ
, NULL_RTX
, TYPE_MODE (integer_type_node
), 0,
2235 cfun
->eh
->region_array
[i
]->post_landing_pad
);
2241 emit_insn_before (seq
, (cfun
->eh
->region_array
[first_reachable
]
2242 ->post_landing_pad
));
2246 sjlj_build_landing_pads ()
2248 struct sjlj_lp_info
*lp_info
;
2250 lp_info
= (struct sjlj_lp_info
*) xcalloc (cfun
->eh
->last_region_number
+ 1,
2251 sizeof (struct sjlj_lp_info
));
2253 if (sjlj_find_directly_reachable_regions (lp_info
))
2255 rtx dispatch_label
= gen_label_rtx ();
2258 = assign_stack_local (TYPE_MODE (sjlj_fc_type_node
),
2259 int_size_in_bytes (sjlj_fc_type_node
),
2260 TYPE_ALIGN (sjlj_fc_type_node
));
2262 sjlj_assign_call_site_values (dispatch_label
, lp_info
);
2263 sjlj_mark_call_sites (lp_info
);
2265 sjlj_emit_function_enter (dispatch_label
);
2266 sjlj_emit_dispatch_table (dispatch_label
, lp_info
);
2267 sjlj_emit_function_exit ();
2274 finish_eh_generation ()
2276 /* Nothing to do if no regions created. */
2277 if (cfun
->eh
->region_tree
== NULL
)
2280 /* The object here is to provide find_basic_blocks with detailed
2281 information (via reachable_handlers) on how exception control
2282 flows within the function. In this first pass, we can include
2283 type information garnered from ERT_THROW and ERT_ALLOWED_EXCEPTIONS
2284 regions, and hope that it will be useful in deleting unreachable
2285 handlers. Subsequently, we will generate landing pads which will
2286 connect many of the handlers, and then type information will not
2287 be effective. Still, this is a win over previous implementations. */
2289 cleanup_cfg (CLEANUP_PRE_LOOP
| CLEANUP_NO_INSN_DEL
);
2291 /* These registers are used by the landing pads. Make sure they
2292 have been generated. */
2293 get_exception_pointer (cfun
);
2294 get_exception_filter (cfun
);
2296 /* Construct the landing pads. */
2298 assign_filter_values ();
2299 build_post_landing_pads ();
2300 connect_post_landing_pads ();
2301 if (USING_SJLJ_EXCEPTIONS
)
2302 sjlj_build_landing_pads ();
2304 dw2_build_landing_pads ();
2306 cfun
->eh
->built_landing_pads
= 1;
2308 /* We've totally changed the CFG. Start over. */
2309 find_exception_handler_labels ();
2310 rebuild_jump_labels (get_insns ());
2311 find_basic_blocks (get_insns (), max_reg_num (), 0);
2312 cleanup_cfg (CLEANUP_PRE_LOOP
| CLEANUP_NO_INSN_DEL
);
2319 struct ehl_map_entry
*entry
= (struct ehl_map_entry
*) pentry
;
2321 /* 2^32 * ((sqrt(5) - 1) / 2) */
2322 const hashval_t scaled_golden_ratio
= 0x9e3779b9;
2323 return CODE_LABEL_NUMBER (entry
->label
) * scaled_golden_ratio
;
2327 ehl_eq (pentry
, pdata
)
2331 struct ehl_map_entry
*entry
= (struct ehl_map_entry
*) pentry
;
2332 struct ehl_map_entry
*data
= (struct ehl_map_entry
*) pdata
;
2334 return entry
->label
== data
->label
;
2337 /* This section handles removing dead code for flow. */
2339 /* Remove LABEL from exception_handler_label_map. */
2342 remove_exception_handler_label (label
)
2345 struct ehl_map_entry
**slot
, tmp
;
2347 /* If exception_handler_label_map was not built yet,
2348 there is nothing to do. */
2349 if (cfun
->eh
->exception_handler_label_map
== NULL
)
2353 slot
= (struct ehl_map_entry
**)
2354 htab_find_slot (cfun
->eh
->exception_handler_label_map
, &tmp
, NO_INSERT
);
2358 htab_clear_slot (cfun
->eh
->exception_handler_label_map
, (void **) slot
);
2361 /* Splice REGION from the region tree etc. */
2364 remove_eh_handler (region
)
2365 struct eh_region
*region
;
2367 struct eh_region
**pp
, **pp_start
, *p
, *outer
, *inner
;
2370 /* For the benefit of efficiently handling REG_EH_REGION notes,
2371 replace this region in the region array with its containing
2372 region. Note that previous region deletions may result in
2373 multiple copies of this region in the array, so we have a
2374 list of alternate numbers by which we are known. */
2376 outer
= region
->outer
;
2377 cfun
->eh
->region_array
[region
->region_number
] = outer
;
2381 EXECUTE_IF_SET_IN_BITMAP (region
->aka
, 0, i
,
2382 { cfun
->eh
->region_array
[i
] = outer
; });
2388 outer
->aka
= BITMAP_GGC_ALLOC ();
2390 bitmap_a_or_b (outer
->aka
, outer
->aka
, region
->aka
);
2391 bitmap_set_bit (outer
->aka
, region
->region_number
);
2394 if (cfun
->eh
->built_landing_pads
)
2395 lab
= region
->landing_pad
;
2397 lab
= region
->label
;
2399 remove_exception_handler_label (lab
);
2402 pp_start
= &outer
->inner
;
2404 pp_start
= &cfun
->eh
->region_tree
;
2405 for (pp
= pp_start
, p
= *pp
; p
!= region
; pp
= &p
->next_peer
, p
= *pp
)
2407 *pp
= region
->next_peer
;
2409 inner
= region
->inner
;
2412 for (p
= inner
; p
->next_peer
; p
= p
->next_peer
)
2416 p
->next_peer
= *pp_start
;
2420 if (region
->type
== ERT_CATCH
)
2422 struct eh_region
*try, *next
, *prev
;
2424 for (try = region
->next_peer
;
2425 try->type
== ERT_CATCH
;
2426 try = try->next_peer
)
2428 if (try->type
!= ERT_TRY
)
2431 next
= region
->u
.catch.next_catch
;
2432 prev
= region
->u
.catch.prev_catch
;
2435 next
->u
.catch.prev_catch
= prev
;
2437 try->u
.try.last_catch
= prev
;
2439 prev
->u
.catch.next_catch
= next
;
2442 try->u
.try.catch = next
;
2444 remove_eh_handler (try);
2449 /* LABEL heads a basic block that is about to be deleted. If this
2450 label corresponds to an exception region, we may be able to
2451 delete the region. */
2454 maybe_remove_eh_handler (label
)
2457 struct ehl_map_entry
**slot
, tmp
;
2458 struct eh_region
*region
;
2460 /* ??? After generating landing pads, it's not so simple to determine
2461 if the region data is completely unused. One must examine the
2462 landing pad and the post landing pad, and whether an inner try block
2463 is referencing the catch handlers directly. */
2464 if (cfun
->eh
->built_landing_pads
)
2468 slot
= (struct ehl_map_entry
**)
2469 htab_find_slot (cfun
->eh
->exception_handler_label_map
, &tmp
, NO_INSERT
);
2472 region
= (*slot
)->region
;
2476 /* Flow will want to remove MUST_NOT_THROW regions as unreachable
2477 because there is no path to the fallback call to terminate.
2478 But the region continues to affect call-site data until there
2479 are no more contained calls, which we don't see here. */
2480 if (region
->type
== ERT_MUST_NOT_THROW
)
2482 htab_clear_slot (cfun
->eh
->exception_handler_label_map
, (void **) slot
);
2483 region
->label
= NULL_RTX
;
2486 remove_eh_handler (region
);
2489 /* Invokes CALLBACK for every exception handler label. Only used by old
2490 loop hackery; should not be used by new code. */
2493 for_each_eh_label (callback
)
2494 void (*callback
) PARAMS ((rtx
));
2496 htab_traverse (cfun
->eh
->exception_handler_label_map
, for_each_eh_label_1
,
2501 for_each_eh_label_1 (pentry
, data
)
2505 struct ehl_map_entry
*entry
= *(struct ehl_map_entry
**)pentry
;
2506 void (*callback
) PARAMS ((rtx
)) = (void (*) PARAMS ((rtx
))) data
;
2508 (*callback
) (entry
->label
);
2512 /* This section describes CFG exception edges for flow. */
2514 /* For communicating between calls to reachable_next_level. */
2515 struct reachable_info
GTY(())
2522 /* A subroutine of reachable_next_level. Return true if TYPE, or a
2523 base class of TYPE, is in HANDLED. */
2526 check_handled (handled
, type
)
2531 /* We can check for exact matches without front-end help. */
2532 if (! lang_eh_type_covers
)
2534 for (t
= handled
; t
; t
= TREE_CHAIN (t
))
2535 if (TREE_VALUE (t
) == type
)
2540 for (t
= handled
; t
; t
= TREE_CHAIN (t
))
2541 if ((*lang_eh_type_covers
) (TREE_VALUE (t
), type
))
2548 /* A subroutine of reachable_next_level. If we are collecting a list
2549 of handlers, add one. After landing pad generation, reference
2550 it instead of the handlers themselves. Further, the handlers are
2551 all wired together, so by referencing one, we've got them all.
2552 Before landing pad generation we reference each handler individually.
2554 LP_REGION contains the landing pad; REGION is the handler. */
2557 add_reachable_handler (info
, lp_region
, region
)
2558 struct reachable_info
*info
;
2559 struct eh_region
*lp_region
;
2560 struct eh_region
*region
;
2565 if (cfun
->eh
->built_landing_pads
)
2567 if (! info
->handlers
)
2568 info
->handlers
= alloc_INSN_LIST (lp_region
->landing_pad
, NULL_RTX
);
2571 info
->handlers
= alloc_INSN_LIST (region
->label
, info
->handlers
);
2574 /* Process one level of exception regions for reachability.
2575 If TYPE_THROWN is non-null, then it is the *exact* type being
2576 propagated. If INFO is non-null, then collect handler labels
2577 and caught/allowed type information between invocations. */
2579 static enum reachable_code
2580 reachable_next_level (region
, type_thrown
, info
)
2581 struct eh_region
*region
;
2583 struct reachable_info
*info
;
2585 switch (region
->type
)
2588 /* Before landing-pad generation, we model control flow
2589 directly to the individual handlers. In this way we can
2590 see that catch handler types may shadow one another. */
2591 add_reachable_handler (info
, region
, region
);
2592 return RNL_MAYBE_CAUGHT
;
2596 struct eh_region
*c
;
2597 enum reachable_code ret
= RNL_NOT_CAUGHT
;
2599 for (c
= region
->u
.try.catch; c
; c
= c
->u
.catch.next_catch
)
2601 /* A catch-all handler ends the search. */
2602 if (c
->u
.catch.type_list
== NULL
)
2604 add_reachable_handler (info
, region
, c
);
2610 /* If we have at least one type match, end the search. */
2611 tree tp_node
= c
->u
.catch.type_list
;
2613 for (; tp_node
; tp_node
= TREE_CHAIN (tp_node
))
2615 tree type
= TREE_VALUE (tp_node
);
2617 if (type
== type_thrown
2618 || (lang_eh_type_covers
2619 && (*lang_eh_type_covers
) (type
, type_thrown
)))
2621 add_reachable_handler (info
, region
, c
);
2626 /* If we have definitive information of a match failure,
2627 the catch won't trigger. */
2628 if (lang_eh_type_covers
)
2629 return RNL_NOT_CAUGHT
;
2632 /* At this point, we either don't know what type is thrown or
2633 don't have front-end assistance to help deciding if it is
2634 covered by one of the types in the list for this region.
2636 We'd then like to add this region to the list of reachable
2637 handlers since it is indeed potentially reachable based on the
2638 information we have.
2640 Actually, this handler is for sure not reachable if all the
2641 types it matches have already been caught. That is, it is only
2642 potentially reachable if at least one of the types it catches
2643 has not been previously caught. */
2646 ret
= RNL_MAYBE_CAUGHT
;
2649 tree tp_node
= c
->u
.catch.type_list
;
2650 bool maybe_reachable
= false;
2652 /* Compute the potential reachability of this handler and
2653 update the list of types caught at the same time. */
2654 for (; tp_node
; tp_node
= TREE_CHAIN (tp_node
))
2656 tree type
= TREE_VALUE (tp_node
);
2658 if (! check_handled (info
->types_caught
, type
))
2661 = tree_cons (NULL
, type
, info
->types_caught
);
2663 maybe_reachable
= true;
2667 if (maybe_reachable
)
2669 add_reachable_handler (info
, region
, c
);
2671 /* ??? If the catch type is a base class of every allowed
2672 type, then we know we can stop the search. */
2673 ret
= RNL_MAYBE_CAUGHT
;
2681 case ERT_ALLOWED_EXCEPTIONS
:
2682 /* An empty list of types definitely ends the search. */
2683 if (region
->u
.allowed
.type_list
== NULL_TREE
)
2685 add_reachable_handler (info
, region
, region
);
2689 /* Collect a list of lists of allowed types for use in detecting
2690 when a catch may be transformed into a catch-all. */
2692 info
->types_allowed
= tree_cons (NULL_TREE
,
2693 region
->u
.allowed
.type_list
,
2694 info
->types_allowed
);
2696 /* If we have definitive information about the type hierarchy,
2697 then we can tell if the thrown type will pass through the
2699 if (type_thrown
&& lang_eh_type_covers
)
2701 if (check_handled (region
->u
.allowed
.type_list
, type_thrown
))
2702 return RNL_NOT_CAUGHT
;
2705 add_reachable_handler (info
, region
, region
);
2710 add_reachable_handler (info
, region
, region
);
2711 return RNL_MAYBE_CAUGHT
;
2714 /* Catch regions are handled by their controlling try region. */
2715 return RNL_NOT_CAUGHT
;
2717 case ERT_MUST_NOT_THROW
:
2718 /* Here we end our search, since no exceptions may propagate.
2719 If we've touched down at some landing pad previous, then the
2720 explicit function call we generated may be used. Otherwise
2721 the call is made by the runtime. */
2722 if (info
&& info
->handlers
)
2724 add_reachable_handler (info
, region
, region
);
2733 /* Shouldn't see these here. */
2740 /* Retrieve a list of labels of exception handlers which can be
2741 reached by a given insn. */
2744 reachable_handlers (insn
)
2747 struct reachable_info info
;
2748 struct eh_region
*region
;
2752 if (GET_CODE (insn
) == JUMP_INSN
2753 && GET_CODE (PATTERN (insn
)) == RESX
)
2754 region_number
= XINT (PATTERN (insn
), 0);
2757 rtx note
= find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
);
2758 if (!note
|| INTVAL (XEXP (note
, 0)) <= 0)
2760 region_number
= INTVAL (XEXP (note
, 0));
2763 memset (&info
, 0, sizeof (info
));
2765 region
= cfun
->eh
->region_array
[region_number
];
2767 type_thrown
= NULL_TREE
;
2768 if (GET_CODE (insn
) == JUMP_INSN
2769 && GET_CODE (PATTERN (insn
)) == RESX
)
2771 /* A RESX leaves a region instead of entering it. Thus the
2772 region itself may have been deleted out from under us. */
2775 region
= region
->outer
;
2777 else if (region
->type
== ERT_THROW
)
2779 type_thrown
= region
->u
.throw.type
;
2780 region
= region
->outer
;
2785 if (reachable_next_level (region
, type_thrown
, &info
) >= RNL_CAUGHT
)
2787 /* If we have processed one cleanup, there is no point in
2788 processing any more of them. Each cleanup will have an edge
2789 to the next outer cleanup region, so the flow graph will be
2791 if (region
->type
== ERT_CLEANUP
)
2792 region
= region
->u
.cleanup
.prev_try
;
2794 region
= region
->outer
;
2797 return info
.handlers
;
2800 /* Determine if the given INSN can throw an exception that is caught
2801 within the function. */
2804 can_throw_internal (insn
)
2807 struct eh_region
*region
;
2811 if (! INSN_P (insn
))
2814 if (GET_CODE (insn
) == INSN
2815 && GET_CODE (PATTERN (insn
)) == SEQUENCE
)
2816 insn
= XVECEXP (PATTERN (insn
), 0, 0);
2818 if (GET_CODE (insn
) == CALL_INSN
2819 && GET_CODE (PATTERN (insn
)) == CALL_PLACEHOLDER
)
2822 for (i
= 0; i
< 3; ++i
)
2824 rtx sub
= XEXP (PATTERN (insn
), i
);
2825 for (; sub
; sub
= NEXT_INSN (sub
))
2826 if (can_throw_internal (sub
))
2832 /* Every insn that might throw has an EH_REGION note. */
2833 note
= find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
);
2834 if (!note
|| INTVAL (XEXP (note
, 0)) <= 0)
2837 region
= cfun
->eh
->region_array
[INTVAL (XEXP (note
, 0))];
2839 type_thrown
= NULL_TREE
;
2840 if (region
->type
== ERT_THROW
)
2842 type_thrown
= region
->u
.throw.type
;
2843 region
= region
->outer
;
2846 /* If this exception is ignored by each and every containing region,
2847 then control passes straight out. The runtime may handle some
2848 regions, which also do not require processing internally. */
2849 for (; region
; region
= region
->outer
)
2851 enum reachable_code how
= reachable_next_level (region
, type_thrown
, 0);
2852 if (how
== RNL_BLOCKED
)
2854 if (how
!= RNL_NOT_CAUGHT
)
2861 /* Determine if the given INSN can throw an exception that is
2862 visible outside the function. */
2865 can_throw_external (insn
)
2868 struct eh_region
*region
;
2872 if (! INSN_P (insn
))
2875 if (GET_CODE (insn
) == INSN
2876 && GET_CODE (PATTERN (insn
)) == SEQUENCE
)
2877 insn
= XVECEXP (PATTERN (insn
), 0, 0);
2879 if (GET_CODE (insn
) == CALL_INSN
2880 && GET_CODE (PATTERN (insn
)) == CALL_PLACEHOLDER
)
2883 for (i
= 0; i
< 3; ++i
)
2885 rtx sub
= XEXP (PATTERN (insn
), i
);
2886 for (; sub
; sub
= NEXT_INSN (sub
))
2887 if (can_throw_external (sub
))
2893 note
= find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
);
2896 /* Calls (and trapping insns) without notes are outside any
2897 exception handling region in this function. We have to
2898 assume it might throw. Given that the front end and middle
2899 ends mark known NOTHROW functions, this isn't so wildly
2901 return (GET_CODE (insn
) == CALL_INSN
2902 || (flag_non_call_exceptions
2903 && may_trap_p (PATTERN (insn
))));
2905 if (INTVAL (XEXP (note
, 0)) <= 0)
2908 region
= cfun
->eh
->region_array
[INTVAL (XEXP (note
, 0))];
2910 type_thrown
= NULL_TREE
;
2911 if (region
->type
== ERT_THROW
)
2913 type_thrown
= region
->u
.throw.type
;
2914 region
= region
->outer
;
2917 /* If the exception is caught or blocked by any containing region,
2918 then it is not seen by any calling function. */
2919 for (; region
; region
= region
->outer
)
2920 if (reachable_next_level (region
, type_thrown
, NULL
) >= RNL_CAUGHT
)
2926 /* Set current_function_nothrow and cfun->all_throwers_are_sibcalls. */
2929 set_nothrow_function_flags ()
2933 current_function_nothrow
= 1;
2935 /* Assume cfun->all_throwers_are_sibcalls until we encounter
2936 something that can throw an exception. We specifically exempt
2937 CALL_INSNs that are SIBLING_CALL_P, as these are really jumps,
2938 and can't throw. Most CALL_INSNs are not SIBLING_CALL_P, so this
2941 cfun
->all_throwers_are_sibcalls
= 1;
2943 if (! flag_exceptions
)
2946 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
2947 if (can_throw_external (insn
))
2949 current_function_nothrow
= 0;
2951 if (GET_CODE (insn
) != CALL_INSN
|| !SIBLING_CALL_P (insn
))
2953 cfun
->all_throwers_are_sibcalls
= 0;
2958 for (insn
= current_function_epilogue_delay_list
; insn
;
2959 insn
= XEXP (insn
, 1))
2960 if (can_throw_external (insn
))
2962 current_function_nothrow
= 0;
2964 if (GET_CODE (insn
) != CALL_INSN
|| !SIBLING_CALL_P (insn
))
2966 cfun
->all_throwers_are_sibcalls
= 0;
2973 /* Various hooks for unwind library. */
2975 /* Do any necessary initialization to access arbitrary stack frames.
2976 On the SPARC, this means flushing the register windows. */
2979 expand_builtin_unwind_init ()
2981 /* Set this so all the registers get saved in our frame; we need to be
2982 able to copy the saved values for any registers from frames we unwind. */
2983 current_function_has_nonlocal_label
= 1;
2985 #ifdef SETUP_FRAME_ADDRESSES
2986 SETUP_FRAME_ADDRESSES ();
2991 expand_builtin_eh_return_data_regno (arglist
)
2994 tree which
= TREE_VALUE (arglist
);
2995 unsigned HOST_WIDE_INT iwhich
;
2997 if (TREE_CODE (which
) != INTEGER_CST
)
2999 error ("argument of `__builtin_eh_return_regno' must be constant");
3003 iwhich
= tree_low_cst (which
, 1);
3004 iwhich
= EH_RETURN_DATA_REGNO (iwhich
);
3005 if (iwhich
== INVALID_REGNUM
)
3008 #ifdef DWARF_FRAME_REGNUM
3009 iwhich
= DWARF_FRAME_REGNUM (iwhich
);
3011 iwhich
= DBX_REGISTER_NUMBER (iwhich
);
3014 return GEN_INT (iwhich
);
3017 /* Given a value extracted from the return address register or stack slot,
3018 return the actual address encoded in that value. */
3021 expand_builtin_extract_return_addr (addr_tree
)
3024 rtx addr
= expand_expr (addr_tree
, NULL_RTX
, Pmode
, 0);
3026 if (GET_MODE (addr
) != Pmode
3027 && GET_MODE (addr
) != VOIDmode
)
3029 #ifdef POINTERS_EXTEND_UNSIGNED
3030 addr
= convert_memory_address (Pmode
, addr
);
3032 addr
= convert_to_mode (Pmode
, addr
, 0);
3036 /* First mask out any unwanted bits. */
3037 #ifdef MASK_RETURN_ADDR
3038 expand_and (Pmode
, addr
, MASK_RETURN_ADDR
, addr
);
3041 /* Then adjust to find the real return address. */
3042 #if defined (RETURN_ADDR_OFFSET)
3043 addr
= plus_constant (addr
, RETURN_ADDR_OFFSET
);
3049 /* Given an actual address in addr_tree, do any necessary encoding
3050 and return the value to be stored in the return address register or
3051 stack slot so the epilogue will return to that address. */
3054 expand_builtin_frob_return_addr (addr_tree
)
3057 rtx addr
= expand_expr (addr_tree
, NULL_RTX
, ptr_mode
, 0);
3059 #ifdef POINTERS_EXTEND_UNSIGNED
3060 if (GET_MODE (addr
) != Pmode
)
3061 addr
= convert_memory_address (Pmode
, addr
);
3064 #ifdef RETURN_ADDR_OFFSET
3065 addr
= force_reg (Pmode
, addr
);
3066 addr
= plus_constant (addr
, -RETURN_ADDR_OFFSET
);
3072 /* Set up the epilogue with the magic bits we'll need to return to the
3073 exception handler. */
3076 expand_builtin_eh_return (stackadj_tree
, handler_tree
)
3077 tree stackadj_tree ATTRIBUTE_UNUSED
;
3082 #ifdef EH_RETURN_STACKADJ_RTX
3083 tmp
= expand_expr (stackadj_tree
, cfun
->eh
->ehr_stackadj
, VOIDmode
, 0);
3084 #ifdef POINTERS_EXTEND_UNSIGNED
3085 if (GET_MODE (tmp
) != Pmode
)
3086 tmp
= convert_memory_address (Pmode
, tmp
);
3088 if (!cfun
->eh
->ehr_stackadj
)
3089 cfun
->eh
->ehr_stackadj
= copy_to_reg (tmp
);
3090 else if (tmp
!= cfun
->eh
->ehr_stackadj
)
3091 emit_move_insn (cfun
->eh
->ehr_stackadj
, tmp
);
3094 tmp
= expand_expr (handler_tree
, cfun
->eh
->ehr_handler
, VOIDmode
, 0);
3095 #ifdef POINTERS_EXTEND_UNSIGNED
3096 if (GET_MODE (tmp
) != Pmode
)
3097 tmp
= convert_memory_address (Pmode
, tmp
);
3099 if (!cfun
->eh
->ehr_handler
)
3100 cfun
->eh
->ehr_handler
= copy_to_reg (tmp
);
3101 else if (tmp
!= cfun
->eh
->ehr_handler
)
3102 emit_move_insn (cfun
->eh
->ehr_handler
, tmp
);
3104 if (!cfun
->eh
->ehr_label
)
3105 cfun
->eh
->ehr_label
= gen_label_rtx ();
3106 emit_jump (cfun
->eh
->ehr_label
);
3114 if (! cfun
->eh
->ehr_label
)
3117 current_function_calls_eh_return
= 1;
3119 #ifdef EH_RETURN_STACKADJ_RTX
3120 emit_move_insn (EH_RETURN_STACKADJ_RTX
, const0_rtx
);
3123 around_label
= gen_label_rtx ();
3124 emit_jump (around_label
);
3126 emit_label (cfun
->eh
->ehr_label
);
3127 clobber_return_register ();
3129 #ifdef EH_RETURN_STACKADJ_RTX
3130 emit_move_insn (EH_RETURN_STACKADJ_RTX
, cfun
->eh
->ehr_stackadj
);
3133 #ifdef HAVE_eh_return
3135 emit_insn (gen_eh_return (cfun
->eh
->ehr_handler
));
3139 #ifdef EH_RETURN_HANDLER_RTX
3140 emit_move_insn (EH_RETURN_HANDLER_RTX
, cfun
->eh
->ehr_handler
);
3142 error ("__builtin_eh_return not supported on this target");
3146 emit_label (around_label
);
3149 /* In the following functions, we represent entries in the action table
3150 as 1-based indices. Special cases are:
3152 0: null action record, non-null landing pad; implies cleanups
3153 -1: null action record, null landing pad; implies no action
3154 -2: no call-site entry; implies must_not_throw
3155 -3: we have yet to process outer regions
3157 Further, no special cases apply to the "next" field of the record.
3158 For next, 0 means end of list. */
3160 struct action_record
3168 action_record_eq (pentry
, pdata
)
3172 const struct action_record
*entry
= (const struct action_record
*) pentry
;
3173 const struct action_record
*data
= (const struct action_record
*) pdata
;
3174 return entry
->filter
== data
->filter
&& entry
->next
== data
->next
;
3178 action_record_hash (pentry
)
3181 const struct action_record
*entry
= (const struct action_record
*) pentry
;
3182 return entry
->next
* 1009 + entry
->filter
;
3186 add_action_record (ar_hash
, filter
, next
)
3190 struct action_record
**slot
, *new, tmp
;
3192 tmp
.filter
= filter
;
3194 slot
= (struct action_record
**) htab_find_slot (ar_hash
, &tmp
, INSERT
);
3196 if ((new = *slot
) == NULL
)
3198 new = (struct action_record
*) xmalloc (sizeof (*new));
3199 new->offset
= VARRAY_ACTIVE_SIZE (cfun
->eh
->action_record_data
) + 1;
3200 new->filter
= filter
;
3204 /* The filter value goes in untouched. The link to the next
3205 record is a "self-relative" byte offset, or zero to indicate
3206 that there is no next record. So convert the absolute 1 based
3207 indices we've been carrying around into a displacement. */
3209 push_sleb128 (&cfun
->eh
->action_record_data
, filter
);
3211 next
-= VARRAY_ACTIVE_SIZE (cfun
->eh
->action_record_data
) + 1;
3212 push_sleb128 (&cfun
->eh
->action_record_data
, next
);
3219 collect_one_action_chain (ar_hash
, region
)
3221 struct eh_region
*region
;
3223 struct eh_region
*c
;
3226 /* If we've reached the top of the region chain, then we have
3227 no actions, and require no landing pad. */
3231 switch (region
->type
)
3234 /* A cleanup adds a zero filter to the beginning of the chain, but
3235 there are special cases to look out for. If there are *only*
3236 cleanups along a path, then it compresses to a zero action.
3237 Further, if there are multiple cleanups along a path, we only
3238 need to represent one of them, as that is enough to trigger
3239 entry to the landing pad at runtime. */
3240 next
= collect_one_action_chain (ar_hash
, region
->outer
);
3243 for (c
= region
->outer
; c
; c
= c
->outer
)
3244 if (c
->type
== ERT_CLEANUP
)
3246 return add_action_record (ar_hash
, 0, next
);
3249 /* Process the associated catch regions in reverse order.
3250 If there's a catch-all handler, then we don't need to
3251 search outer regions. Use a magic -3 value to record
3252 that we haven't done the outer search. */
3254 for (c
= region
->u
.try.last_catch
; c
; c
= c
->u
.catch.prev_catch
)
3256 if (c
->u
.catch.type_list
== NULL
)
3258 /* Retrieve the filter from the head of the filter list
3259 where we have stored it (see assign_filter_values). */
3261 = TREE_INT_CST_LOW (TREE_VALUE (c
->u
.catch.filter_list
));
3263 next
= add_action_record (ar_hash
, filter
, 0);
3267 /* Once the outer search is done, trigger an action record for
3268 each filter we have. */
3273 next
= collect_one_action_chain (ar_hash
, region
->outer
);
3275 /* If there is no next action, terminate the chain. */
3278 /* If all outer actions are cleanups or must_not_throw,
3279 we'll have no action record for it, since we had wanted
3280 to encode these states in the call-site record directly.
3281 Add a cleanup action to the chain to catch these. */
3283 next
= add_action_record (ar_hash
, 0, 0);
3286 flt_node
= c
->u
.catch.filter_list
;
3287 for (; flt_node
; flt_node
= TREE_CHAIN (flt_node
))
3289 int filter
= TREE_INT_CST_LOW (TREE_VALUE (flt_node
));
3290 next
= add_action_record (ar_hash
, filter
, next
);
3296 case ERT_ALLOWED_EXCEPTIONS
:
3297 /* An exception specification adds its filter to the
3298 beginning of the chain. */
3299 next
= collect_one_action_chain (ar_hash
, region
->outer
);
3300 return add_action_record (ar_hash
, region
->u
.allowed
.filter
,
3301 next
< 0 ? 0 : next
);
3303 case ERT_MUST_NOT_THROW
:
3304 /* A must-not-throw region with no inner handlers or cleanups
3305 requires no call-site entry. Note that this differs from
3306 the no handler or cleanup case in that we do require an lsda
3307 to be generated. Return a magic -2 value to record this. */
3312 /* CATCH regions are handled in TRY above. THROW regions are
3313 for optimization information only and produce no output. */
3314 return collect_one_action_chain (ar_hash
, region
->outer
);
3322 add_call_site (landing_pad
, action
)
3326 struct call_site_record
*data
= cfun
->eh
->call_site_data
;
3327 int used
= cfun
->eh
->call_site_data_used
;
3328 int size
= cfun
->eh
->call_site_data_size
;
3332 size
= (size
? size
* 2 : 64);
3333 data
= (struct call_site_record
*)
3334 ggc_realloc (data
, sizeof (*data
) * size
);
3335 cfun
->eh
->call_site_data
= data
;
3336 cfun
->eh
->call_site_data_size
= size
;
3339 data
[used
].landing_pad
= landing_pad
;
3340 data
[used
].action
= action
;
3342 cfun
->eh
->call_site_data_used
= used
+ 1;
3344 return used
+ call_site_base
;
3347 /* Turn REG_EH_REGION notes back into NOTE_INSN_EH_REGION notes.
3348 The new note numbers will not refer to region numbers, but
3349 instead to call site entries. */
3352 convert_to_eh_region_ranges ()
3354 rtx insn
, iter
, note
;
3356 int last_action
= -3;
3357 rtx last_action_insn
= NULL_RTX
;
3358 rtx last_landing_pad
= NULL_RTX
;
3359 rtx first_no_action_insn
= NULL_RTX
;
3362 if (USING_SJLJ_EXCEPTIONS
|| cfun
->eh
->region_tree
== NULL
)
3365 VARRAY_UCHAR_INIT (cfun
->eh
->action_record_data
, 64, "action_record_data");
3367 ar_hash
= htab_create (31, action_record_hash
, action_record_eq
, free
);
3369 for (iter
= get_insns (); iter
; iter
= NEXT_INSN (iter
))
3372 struct eh_region
*region
;
3374 rtx this_landing_pad
;
3377 if (GET_CODE (insn
) == INSN
3378 && GET_CODE (PATTERN (insn
)) == SEQUENCE
)
3379 insn
= XVECEXP (PATTERN (insn
), 0, 0);
3381 note
= find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
);
3384 if (! (GET_CODE (insn
) == CALL_INSN
3385 || (flag_non_call_exceptions
3386 && may_trap_p (PATTERN (insn
)))))
3393 if (INTVAL (XEXP (note
, 0)) <= 0)
3395 region
= cfun
->eh
->region_array
[INTVAL (XEXP (note
, 0))];
3396 this_action
= collect_one_action_chain (ar_hash
, region
);
3399 /* Existence of catch handlers, or must-not-throw regions
3400 implies that an lsda is needed (even if empty). */
3401 if (this_action
!= -1)
3402 cfun
->uses_eh_lsda
= 1;
3404 /* Delay creation of region notes for no-action regions
3405 until we're sure that an lsda will be required. */
3406 else if (last_action
== -3)
3408 first_no_action_insn
= iter
;
3412 /* Cleanups and handlers may share action chains but not
3413 landing pads. Collect the landing pad for this region. */
3414 if (this_action
>= 0)
3416 struct eh_region
*o
;
3417 for (o
= region
; ! o
->landing_pad
; o
= o
->outer
)
3419 this_landing_pad
= o
->landing_pad
;
3422 this_landing_pad
= NULL_RTX
;
3424 /* Differing actions or landing pads implies a change in call-site
3425 info, which implies some EH_REGION note should be emitted. */
3426 if (last_action
!= this_action
3427 || last_landing_pad
!= this_landing_pad
)
3429 /* If we'd not seen a previous action (-3) or the previous
3430 action was must-not-throw (-2), then we do not need an
3432 if (last_action
>= -1)
3434 /* If we delayed the creation of the begin, do it now. */
3435 if (first_no_action_insn
)
3437 call_site
= add_call_site (NULL_RTX
, 0);
3438 note
= emit_note_before (NOTE_INSN_EH_REGION_BEG
,
3439 first_no_action_insn
);
3440 NOTE_EH_HANDLER (note
) = call_site
;
3441 first_no_action_insn
= NULL_RTX
;
3444 note
= emit_note_after (NOTE_INSN_EH_REGION_END
,
3446 NOTE_EH_HANDLER (note
) = call_site
;
3449 /* If the new action is must-not-throw, then no region notes
3451 if (this_action
>= -1)
3453 call_site
= add_call_site (this_landing_pad
,
3454 this_action
< 0 ? 0 : this_action
);
3455 note
= emit_note_before (NOTE_INSN_EH_REGION_BEG
, iter
);
3456 NOTE_EH_HANDLER (note
) = call_site
;
3459 last_action
= this_action
;
3460 last_landing_pad
= this_landing_pad
;
3462 last_action_insn
= iter
;
3465 if (last_action
>= -1 && ! first_no_action_insn
)
3467 note
= emit_note_after (NOTE_INSN_EH_REGION_END
, last_action_insn
);
3468 NOTE_EH_HANDLER (note
) = call_site
;
3471 htab_delete (ar_hash
);
3476 push_uleb128 (data_area
, value
)
3477 varray_type
*data_area
;
3482 unsigned char byte
= value
& 0x7f;
3486 VARRAY_PUSH_UCHAR (*data_area
, byte
);
3492 push_sleb128 (data_area
, value
)
3493 varray_type
*data_area
;
3501 byte
= value
& 0x7f;
3503 more
= ! ((value
== 0 && (byte
& 0x40) == 0)
3504 || (value
== -1 && (byte
& 0x40) != 0));
3507 VARRAY_PUSH_UCHAR (*data_area
, byte
);
3513 #ifndef HAVE_AS_LEB128
3515 dw2_size_of_call_site_table ()
3517 int n
= cfun
->eh
->call_site_data_used
;
3518 int size
= n
* (4 + 4 + 4);
3521 for (i
= 0; i
< n
; ++i
)
3523 struct call_site_record
*cs
= &cfun
->eh
->call_site_data
[i
];
3524 size
+= size_of_uleb128 (cs
->action
);
3531 sjlj_size_of_call_site_table ()
3533 int n
= cfun
->eh
->call_site_data_used
;
3537 for (i
= 0; i
< n
; ++i
)
3539 struct call_site_record
*cs
= &cfun
->eh
->call_site_data
[i
];
3540 size
+= size_of_uleb128 (INTVAL (cs
->landing_pad
));
3541 size
+= size_of_uleb128 (cs
->action
);
3549 dw2_output_call_site_table ()
3551 const char *const function_start_lab
3552 = IDENTIFIER_POINTER (current_function_func_begin_label
);
3553 int n
= cfun
->eh
->call_site_data_used
;
3556 for (i
= 0; i
< n
; ++i
)
3558 struct call_site_record
*cs
= &cfun
->eh
->call_site_data
[i
];
3559 char reg_start_lab
[32];
3560 char reg_end_lab
[32];
3561 char landing_pad_lab
[32];
3563 ASM_GENERATE_INTERNAL_LABEL (reg_start_lab
, "LEHB", call_site_base
+ i
);
3564 ASM_GENERATE_INTERNAL_LABEL (reg_end_lab
, "LEHE", call_site_base
+ i
);
3566 if (cs
->landing_pad
)
3567 ASM_GENERATE_INTERNAL_LABEL (landing_pad_lab
, "L",
3568 CODE_LABEL_NUMBER (cs
->landing_pad
));
3570 /* ??? Perhaps use insn length scaling if the assembler supports
3571 generic arithmetic. */
3572 /* ??? Perhaps use attr_length to choose data1 or data2 instead of
3573 data4 if the function is small enough. */
3574 #ifdef HAVE_AS_LEB128
3575 dw2_asm_output_delta_uleb128 (reg_start_lab
, function_start_lab
,
3576 "region %d start", i
);
3577 dw2_asm_output_delta_uleb128 (reg_end_lab
, reg_start_lab
,
3579 if (cs
->landing_pad
)
3580 dw2_asm_output_delta_uleb128 (landing_pad_lab
, function_start_lab
,
3583 dw2_asm_output_data_uleb128 (0, "landing pad");
3585 dw2_asm_output_delta (4, reg_start_lab
, function_start_lab
,
3586 "region %d start", i
);
3587 dw2_asm_output_delta (4, reg_end_lab
, reg_start_lab
, "length");
3588 if (cs
->landing_pad
)
3589 dw2_asm_output_delta (4, landing_pad_lab
, function_start_lab
,
3592 dw2_asm_output_data (4, 0, "landing pad");
3594 dw2_asm_output_data_uleb128 (cs
->action
, "action");
3597 call_site_base
+= n
;
3601 sjlj_output_call_site_table ()
3603 int n
= cfun
->eh
->call_site_data_used
;
3606 for (i
= 0; i
< n
; ++i
)
3608 struct call_site_record
*cs
= &cfun
->eh
->call_site_data
[i
];
3610 dw2_asm_output_data_uleb128 (INTVAL (cs
->landing_pad
),
3611 "region %d landing pad", i
);
3612 dw2_asm_output_data_uleb128 (cs
->action
, "action");
3615 call_site_base
+= n
;
3618 /* Tell assembler to switch to the section for the exception handling
3622 default_exception_section ()
3624 if (targetm
.have_named_sections
)
3627 #ifdef HAVE_LD_RO_RW_SECTION_MIXING
3628 int tt_format
= ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3631 || ((tt_format
& 0x70) != DW_EH_PE_absptr
3632 && (tt_format
& 0x70) != DW_EH_PE_aligned
))
3633 ? 0 : SECTION_WRITE
;
3635 flags
= SECTION_WRITE
;
3637 named_section_flags (".gcc_except_table", flags
);
3642 readonly_data_section ();
3646 output_function_exception_table ()
3648 int tt_format
, cs_format
, lp_format
, i
, n
;
3649 #ifdef HAVE_AS_LEB128
3650 char ttype_label
[32];
3651 char cs_after_size_label
[32];
3652 char cs_end_label
[32];
3657 int tt_format_size
= 0;
3659 /* Not all functions need anything. */
3660 if (! cfun
->uses_eh_lsda
)
3663 #ifdef IA64_UNWIND_INFO
3664 fputs ("\t.personality\t", asm_out_file
);
3665 output_addr_const (asm_out_file
, eh_personality_libfunc
);
3666 fputs ("\n\t.handlerdata\n", asm_out_file
);
3667 /* Note that varasm still thinks we're in the function's code section.
3668 The ".endp" directive that will immediately follow will take us back. */
3670 (*targetm
.asm_out
.exception_section
) ();
3673 have_tt_data
= (VARRAY_ACTIVE_SIZE (cfun
->eh
->ttype_data
) > 0
3674 || VARRAY_ACTIVE_SIZE (cfun
->eh
->ehspec_data
) > 0);
3676 /* Indicate the format of the @TType entries. */
3678 tt_format
= DW_EH_PE_omit
;
3681 tt_format
= ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3682 #ifdef HAVE_AS_LEB128
3683 ASM_GENERATE_INTERNAL_LABEL (ttype_label
, "LLSDATT",
3684 current_function_funcdef_no
);
3686 tt_format_size
= size_of_encoded_value (tt_format
);
3688 assemble_align (tt_format_size
* BITS_PER_UNIT
);
3691 (*targetm
.asm_out
.internal_label
) (asm_out_file
, "LLSDA",
3692 current_function_funcdef_no
);
3694 /* The LSDA header. */
3696 /* Indicate the format of the landing pad start pointer. An omitted
3697 field implies @LPStart == @Start. */
3698 /* Currently we always put @LPStart == @Start. This field would
3699 be most useful in moving the landing pads completely out of
3700 line to another section, but it could also be used to minimize
3701 the size of uleb128 landing pad offsets. */
3702 lp_format
= DW_EH_PE_omit
;
3703 dw2_asm_output_data (1, lp_format
, "@LPStart format (%s)",
3704 eh_data_format_name (lp_format
));
3706 /* @LPStart pointer would go here. */
3708 dw2_asm_output_data (1, tt_format
, "@TType format (%s)",
3709 eh_data_format_name (tt_format
));
3711 #ifndef HAVE_AS_LEB128
3712 if (USING_SJLJ_EXCEPTIONS
)
3713 call_site_len
= sjlj_size_of_call_site_table ();
3715 call_site_len
= dw2_size_of_call_site_table ();
3718 /* A pc-relative 4-byte displacement to the @TType data. */
3721 #ifdef HAVE_AS_LEB128
3722 char ttype_after_disp_label
[32];
3723 ASM_GENERATE_INTERNAL_LABEL (ttype_after_disp_label
, "LLSDATTD",
3724 current_function_funcdef_no
);
3725 dw2_asm_output_delta_uleb128 (ttype_label
, ttype_after_disp_label
,
3726 "@TType base offset");
3727 ASM_OUTPUT_LABEL (asm_out_file
, ttype_after_disp_label
);
3729 /* Ug. Alignment queers things. */
3730 unsigned int before_disp
, after_disp
, last_disp
, disp
;
3732 before_disp
= 1 + 1;
3733 after_disp
= (1 + size_of_uleb128 (call_site_len
)
3735 + VARRAY_ACTIVE_SIZE (cfun
->eh
->action_record_data
)
3736 + (VARRAY_ACTIVE_SIZE (cfun
->eh
->ttype_data
)
3742 unsigned int disp_size
, pad
;
3745 disp_size
= size_of_uleb128 (disp
);
3746 pad
= before_disp
+ disp_size
+ after_disp
;
3747 if (pad
% tt_format_size
)
3748 pad
= tt_format_size
- (pad
% tt_format_size
);
3751 disp
= after_disp
+ pad
;
3753 while (disp
!= last_disp
);
3755 dw2_asm_output_data_uleb128 (disp
, "@TType base offset");
3759 /* Indicate the format of the call-site offsets. */
3760 #ifdef HAVE_AS_LEB128
3761 cs_format
= DW_EH_PE_uleb128
;
3763 cs_format
= DW_EH_PE_udata4
;
3765 dw2_asm_output_data (1, cs_format
, "call-site format (%s)",
3766 eh_data_format_name (cs_format
));
3768 #ifdef HAVE_AS_LEB128
3769 ASM_GENERATE_INTERNAL_LABEL (cs_after_size_label
, "LLSDACSB",
3770 current_function_funcdef_no
);
3771 ASM_GENERATE_INTERNAL_LABEL (cs_end_label
, "LLSDACSE",
3772 current_function_funcdef_no
);
3773 dw2_asm_output_delta_uleb128 (cs_end_label
, cs_after_size_label
,
3774 "Call-site table length");
3775 ASM_OUTPUT_LABEL (asm_out_file
, cs_after_size_label
);
3776 if (USING_SJLJ_EXCEPTIONS
)
3777 sjlj_output_call_site_table ();
3779 dw2_output_call_site_table ();
3780 ASM_OUTPUT_LABEL (asm_out_file
, cs_end_label
);
3782 dw2_asm_output_data_uleb128 (call_site_len
,"Call-site table length");
3783 if (USING_SJLJ_EXCEPTIONS
)
3784 sjlj_output_call_site_table ();
3786 dw2_output_call_site_table ();
3789 /* ??? Decode and interpret the data for flag_debug_asm. */
3790 n
= VARRAY_ACTIVE_SIZE (cfun
->eh
->action_record_data
);
3791 for (i
= 0; i
< n
; ++i
)
3792 dw2_asm_output_data (1, VARRAY_UCHAR (cfun
->eh
->action_record_data
, i
),
3793 (i
? NULL
: "Action record table"));
3796 assemble_align (tt_format_size
* BITS_PER_UNIT
);
3798 i
= VARRAY_ACTIVE_SIZE (cfun
->eh
->ttype_data
);
3801 tree type
= VARRAY_TREE (cfun
->eh
->ttype_data
, i
);
3804 if (type
== NULL_TREE
)
3805 type
= integer_zero_node
;
3807 type
= lookup_type_for_runtime (type
);
3809 value
= expand_expr (type
, NULL_RTX
, VOIDmode
, EXPAND_INITIALIZER
);
3810 if (tt_format
== DW_EH_PE_absptr
|| tt_format
== DW_EH_PE_aligned
)
3811 assemble_integer (value
, tt_format_size
,
3812 tt_format_size
* BITS_PER_UNIT
, 1);
3814 dw2_asm_output_encoded_addr_rtx (tt_format
, value
, NULL
);
3817 #ifdef HAVE_AS_LEB128
3819 ASM_OUTPUT_LABEL (asm_out_file
, ttype_label
);
3822 /* ??? Decode and interpret the data for flag_debug_asm. */
3823 n
= VARRAY_ACTIVE_SIZE (cfun
->eh
->ehspec_data
);
3824 for (i
= 0; i
< n
; ++i
)
3825 dw2_asm_output_data (1, VARRAY_UCHAR (cfun
->eh
->ehspec_data
, i
),
3826 (i
? NULL
: "Exception specification table"));
3828 function_section (current_function_decl
);
3831 #include "gt-except.h"