1 /* Implements exception handling.
2 Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4 Contributed by Mike Stump <mrs@cygnus.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 /* An exception is an event that can be signaled from within a
25 function. This event can then be "caught" or "trapped" by the
26 callers of this function. This potentially allows program flow to
27 be transferred to any arbitrary code associated with a function call
28 several levels up the stack.
30 The intended use for this mechanism is for signaling "exceptional
31 events" in an out-of-band fashion, hence its name. The C++ language
32 (and many other OO-styled or functional languages) practically
33 requires such a mechanism, as otherwise it becomes very difficult
34 or even impossible to signal failure conditions in complex
35 situations. The traditional C++ example is when an error occurs in
36 the process of constructing an object; without such a mechanism, it
37 is impossible to signal that the error occurs without adding global
38 state variables and error checks around every object construction.
40 The act of causing this event to occur is referred to as "throwing
41 an exception". (Alternate terms include "raising an exception" or
42 "signaling an exception".) The term "throw" is used because control
43 is returned to the callers of the function that is signaling the
44 exception, and thus there is the concept of "throwing" the
45 exception up the call stack.
47 [ Add updated documentation on how to use this. ] */
52 #include "coretypes.h"
60 #include "insn-config.h"
62 #include "integrate.h"
63 #include "hard-reg-set.h"
64 #include "basic-block.h"
66 #include "dwarf2asm.h"
67 #include "dwarf2out.h"
75 #include "langhooks.h"
78 /* Provide defaults for stuff that may not be defined when using
80 #ifndef EH_RETURN_DATA_REGNO
81 #define EH_RETURN_DATA_REGNO(N) INVALID_REGNUM
85 /* Protect cleanup actions with must-not-throw regions, with a call
86 to the given failure handler. */
87 tree (*lang_protect_cleanup_actions
) (void);
89 /* Return true if type A catches type B. */
90 int (*lang_eh_type_covers
) (tree a
, tree b
);
92 /* Map a type to a runtime object to match type. */
93 tree (*lang_eh_runtime_type
) (tree
);
95 /* A hash table of label to region number. */
97 struct ehl_map_entry
GTY(())
100 struct eh_region
*region
;
103 static GTY(()) int call_site_base
;
104 static GTY ((param_is (union tree_node
)))
105 htab_t type_to_runtime_map
;
107 /* Describe the SjLj_Function_Context structure. */
108 static GTY(()) tree sjlj_fc_type_node
;
109 static int sjlj_fc_call_site_ofs
;
110 static int sjlj_fc_data_ofs
;
111 static int sjlj_fc_personality_ofs
;
112 static int sjlj_fc_lsda_ofs
;
113 static int sjlj_fc_jbuf_ofs
;
115 /* Describes one exception region. */
116 struct eh_region
GTY(())
118 /* The immediately surrounding region. */
119 struct eh_region
*outer
;
121 /* The list of immediately contained regions. */
122 struct eh_region
*inner
;
123 struct eh_region
*next_peer
;
125 /* An identifier for this region. */
128 /* When a region is deleted, its parents inherit the REG_EH_REGION
129 numbers already assigned. */
132 /* Each region does exactly one thing. */
139 ERT_ALLOWED_EXCEPTIONS
,
145 /* Holds the action to perform based on the preceding type. */
147 /* A list of catch blocks, a surrounding try block,
148 and the label for continuing after a catch. */
149 struct eh_region_u_try
{
150 struct eh_region
*catch;
151 struct eh_region
*last_catch
;
152 struct eh_region
*prev_try
;
154 } GTY ((tag ("ERT_TRY"))) try;
156 /* The list through the catch handlers, the list of type objects
157 matched, and the list of associated filters. */
158 struct eh_region_u_catch
{
159 struct eh_region
*next_catch
;
160 struct eh_region
*prev_catch
;
163 } GTY ((tag ("ERT_CATCH"))) catch;
165 /* A tree_list of allowed types. */
166 struct eh_region_u_allowed
{
169 } GTY ((tag ("ERT_ALLOWED_EXCEPTIONS"))) allowed
;
171 /* The type given by a call to "throw foo();", or discovered
173 struct eh_region_u_throw
{
175 } GTY ((tag ("ERT_THROW"))) throw;
177 /* Retain the cleanup expression even after expansion so that
178 we can match up fixup regions. */
179 struct eh_region_u_cleanup
{
181 struct eh_region
*prev_try
;
182 } GTY ((tag ("ERT_CLEANUP"))) cleanup
;
184 /* The real region (by expression and by pointer) that fixup code
186 struct eh_region_u_fixup
{
188 struct eh_region
*real_region
;
190 } GTY ((tag ("ERT_FIXUP"))) fixup
;
191 } GTY ((desc ("%0.type"))) u
;
193 /* 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 (const void *, const void *);
259 static hashval_t
t2r_hash (const void *);
260 static void add_type_for_runtime (tree
);
261 static tree
lookup_type_for_runtime (tree
);
263 static struct eh_region
*expand_eh_region_end (void);
265 static void resolve_fixup_regions (void);
266 static void remove_fixup_regions (void);
267 static void remove_unreachable_regions (rtx
);
268 static void convert_from_eh_region_ranges_1 (rtx
*, int *, int);
270 static struct eh_region
*duplicate_eh_region_1 (struct eh_region
*,
271 struct inline_remap
*);
272 static void duplicate_eh_region_2 (struct eh_region
*, struct eh_region
**);
273 static int ttypes_filter_eq (const void *, const void *);
274 static hashval_t
ttypes_filter_hash (const void *);
275 static int ehspec_filter_eq (const void *, const void *);
276 static hashval_t
ehspec_filter_hash (const void *);
277 static int add_ttypes_entry (htab_t
, tree
);
278 static int add_ehspec_entry (htab_t
, htab_t
, tree
);
279 static void assign_filter_values (void);
280 static void build_post_landing_pads (void);
281 static void connect_post_landing_pads (void);
282 static void dw2_build_landing_pads (void);
285 static bool sjlj_find_directly_reachable_regions (struct sjlj_lp_info
*);
286 static void sjlj_assign_call_site_values (rtx
, struct sjlj_lp_info
*);
287 static void sjlj_mark_call_sites (struct sjlj_lp_info
*);
288 static void sjlj_emit_function_enter (rtx
);
289 static void sjlj_emit_function_exit (void);
290 static void sjlj_emit_dispatch_table (rtx
, struct sjlj_lp_info
*);
291 static void sjlj_build_landing_pads (void);
293 static hashval_t
ehl_hash (const void *);
294 static int ehl_eq (const void *, const void *);
295 static void add_ehl_entry (rtx
, struct eh_region
*);
296 static void remove_exception_handler_label (rtx
);
297 static void remove_eh_handler (struct eh_region
*);
298 static int for_each_eh_label_1 (void **, void *);
300 /* The return value of reachable_next_level. */
303 /* The given exception is not processed by the given region. */
305 /* The given exception may need processing by the given region. */
307 /* The given exception is completely processed by the given region. */
309 /* The given exception is completely processed by the runtime. */
313 struct reachable_info
;
314 static enum reachable_code
reachable_next_level (struct eh_region
*, tree
,
315 struct reachable_info
*);
317 static int action_record_eq (const void *, const void *);
318 static hashval_t
action_record_hash (const void *);
319 static int add_action_record (htab_t
, int, int);
320 static int collect_one_action_chain (htab_t
, struct eh_region
*);
321 static int add_call_site (rtx
, int);
323 static void push_uleb128 (varray_type
*, unsigned int);
324 static void push_sleb128 (varray_type
*, int);
325 #ifndef HAVE_AS_LEB128
326 static int dw2_size_of_call_site_table (void);
327 static int sjlj_size_of_call_site_table (void);
329 static void dw2_output_call_site_table (void);
330 static void sjlj_output_call_site_table (void);
333 /* Routine to see if exception handling is turned on.
334 DO_WARN is nonzero if we want to inform the user that exception
335 handling is turned off.
337 This is used to ensure that -fexceptions has been specified if the
338 compiler tries to use any exception-specific functions. */
341 doing_eh (int do_warn
)
343 if (! flag_exceptions
)
345 static int warned
= 0;
346 if (! warned
&& do_warn
)
348 error ("exception handling disabled, use -fexceptions to enable");
360 if (! flag_exceptions
)
363 type_to_runtime_map
= htab_create_ggc (31, t2r_hash
, t2r_eq
, NULL
);
365 /* Create the SjLj_Function_Context structure. This should match
366 the definition in unwind-sjlj.c. */
367 if (USING_SJLJ_EXCEPTIONS
)
369 tree f_jbuf
, f_per
, f_lsda
, f_prev
, f_cs
, f_data
, tmp
;
371 sjlj_fc_type_node
= lang_hooks
.types
.make_type (RECORD_TYPE
);
373 f_prev
= build_decl (FIELD_DECL
, get_identifier ("__prev"),
374 build_pointer_type (sjlj_fc_type_node
));
375 DECL_FIELD_CONTEXT (f_prev
) = sjlj_fc_type_node
;
377 f_cs
= build_decl (FIELD_DECL
, get_identifier ("__call_site"),
379 DECL_FIELD_CONTEXT (f_cs
) = sjlj_fc_type_node
;
381 tmp
= build_index_type (build_int_2 (4 - 1, 0));
382 tmp
= build_array_type (lang_hooks
.types
.type_for_mode (word_mode
, 1),
384 f_data
= build_decl (FIELD_DECL
, get_identifier ("__data"), tmp
);
385 DECL_FIELD_CONTEXT (f_data
) = sjlj_fc_type_node
;
387 f_per
= build_decl (FIELD_DECL
, get_identifier ("__personality"),
389 DECL_FIELD_CONTEXT (f_per
) = sjlj_fc_type_node
;
391 f_lsda
= build_decl (FIELD_DECL
, get_identifier ("__lsda"),
393 DECL_FIELD_CONTEXT (f_lsda
) = sjlj_fc_type_node
;
395 #ifdef DONT_USE_BUILTIN_SETJMP
397 tmp
= build_int_2 (JMP_BUF_SIZE
- 1, 0);
399 /* Should be large enough for most systems, if it is not,
400 JMP_BUF_SIZE should be defined with the proper value. It will
401 also tend to be larger than necessary for most systems, a more
402 optimal port will define JMP_BUF_SIZE. */
403 tmp
= build_int_2 (FIRST_PSEUDO_REGISTER
+ 2 - 1, 0);
406 /* builtin_setjmp takes a pointer to 5 words. */
407 tmp
= build_int_2 (5 * BITS_PER_WORD
/ POINTER_SIZE
- 1, 0);
409 tmp
= build_index_type (tmp
);
410 tmp
= build_array_type (ptr_type_node
, tmp
);
411 f_jbuf
= build_decl (FIELD_DECL
, get_identifier ("__jbuf"), tmp
);
412 #ifdef DONT_USE_BUILTIN_SETJMP
413 /* We don't know what the alignment requirements of the
414 runtime's jmp_buf has. Overestimate. */
415 DECL_ALIGN (f_jbuf
) = BIGGEST_ALIGNMENT
;
416 DECL_USER_ALIGN (f_jbuf
) = 1;
418 DECL_FIELD_CONTEXT (f_jbuf
) = sjlj_fc_type_node
;
420 TYPE_FIELDS (sjlj_fc_type_node
) = f_prev
;
421 TREE_CHAIN (f_prev
) = f_cs
;
422 TREE_CHAIN (f_cs
) = f_data
;
423 TREE_CHAIN (f_data
) = f_per
;
424 TREE_CHAIN (f_per
) = f_lsda
;
425 TREE_CHAIN (f_lsda
) = f_jbuf
;
427 layout_type (sjlj_fc_type_node
);
429 /* Cache the interesting field offsets so that we have
430 easy access from rtl. */
431 sjlj_fc_call_site_ofs
432 = (tree_low_cst (DECL_FIELD_OFFSET (f_cs
), 1)
433 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_cs
), 1) / BITS_PER_UNIT
);
435 = (tree_low_cst (DECL_FIELD_OFFSET (f_data
), 1)
436 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_data
), 1) / BITS_PER_UNIT
);
437 sjlj_fc_personality_ofs
438 = (tree_low_cst (DECL_FIELD_OFFSET (f_per
), 1)
439 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_per
), 1) / BITS_PER_UNIT
);
441 = (tree_low_cst (DECL_FIELD_OFFSET (f_lsda
), 1)
442 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_lsda
), 1) / BITS_PER_UNIT
);
444 = (tree_low_cst (DECL_FIELD_OFFSET (f_jbuf
), 1)
445 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_jbuf
), 1) / BITS_PER_UNIT
);
450 init_eh_for_function (void)
452 cfun
->eh
= ggc_alloc_cleared (sizeof (struct eh_status
));
455 /* Routines to generate the exception tree somewhat directly.
456 These are used from tree-eh.c when processing exception related
457 nodes during tree optimization. */
459 static struct eh_region
*
460 gen_eh_region (enum eh_region_type type
, struct eh_region
*outer
)
462 struct eh_region
*new;
464 #ifdef ENABLE_CHECKING
469 /* Insert a new blank region as a leaf in the tree. */
470 new = ggc_alloc_cleared (sizeof (*new));
475 new->next_peer
= outer
->inner
;
480 new->next_peer
= cfun
->eh
->region_tree
;
481 cfun
->eh
->region_tree
= new;
484 new->region_number
= ++cfun
->eh
->last_region_number
;
490 gen_eh_region_cleanup (struct eh_region
*outer
, struct eh_region
*prev_try
)
492 struct eh_region
*cleanup
= gen_eh_region (ERT_CLEANUP
, outer
);
493 cleanup
->u
.cleanup
.prev_try
= prev_try
;
498 gen_eh_region_try (struct eh_region
*outer
)
500 return gen_eh_region (ERT_TRY
, outer
);
504 gen_eh_region_catch (struct eh_region
*t
, tree type_or_list
)
506 struct eh_region
*c
, *l
;
507 tree type_list
, type_node
;
509 /* Ensure to always end up with a type list to normalize further
510 processing, then register each type against the runtime types map. */
511 type_list
= type_or_list
;
514 if (TREE_CODE (type_or_list
) != TREE_LIST
)
515 type_list
= tree_cons (NULL_TREE
, type_or_list
, NULL_TREE
);
517 type_node
= type_list
;
518 for (; type_node
; type_node
= TREE_CHAIN (type_node
))
519 add_type_for_runtime (TREE_VALUE (type_node
));
522 c
= gen_eh_region (ERT_CATCH
, t
->outer
);
523 c
->u
.catch.type_list
= type_list
;
524 l
= t
->u
.try.last_catch
;
525 c
->u
.catch.prev_catch
= l
;
527 l
->u
.catch.next_catch
= c
;
530 t
->u
.try.last_catch
= c
;
536 gen_eh_region_allowed (struct eh_region
*outer
, tree allowed
)
538 struct eh_region
*region
= gen_eh_region (ERT_ALLOWED_EXCEPTIONS
, outer
);
539 region
->u
.allowed
.type_list
= allowed
;
541 for (; allowed
; allowed
= TREE_CHAIN (allowed
))
542 add_type_for_runtime (TREE_VALUE (allowed
));
548 gen_eh_region_must_not_throw (struct eh_region
*outer
)
550 return gen_eh_region (ERT_MUST_NOT_THROW
, outer
);
554 get_eh_region_number (struct eh_region
*region
)
556 return region
->region_number
;
560 get_eh_region_may_contain_throw (struct eh_region
*region
)
562 return region
->may_contain_throw
;
566 get_eh_region_tree_label (struct eh_region
*region
)
568 return region
->tree_label
;
572 set_eh_region_tree_label (struct eh_region
*region
, tree lab
)
574 region
->tree_label
= lab
;
577 /* Start an exception handling region. All instructions emitted
578 after this point are considered to be part of the region until
579 expand_eh_region_end is invoked. */
582 expand_eh_region_start (void)
584 struct eh_region
*new;
590 new = gen_eh_region (ERT_UNKNOWN
, cfun
->eh
->cur_region
);
591 cfun
->eh
->cur_region
= new;
593 /* Create a note marking the start of this region. */
594 note
= emit_note (NOTE_INSN_EH_REGION_BEG
);
595 NOTE_EH_HANDLER (note
) = new->region_number
;
598 /* Common code to end a region. Returns the region just ended. */
600 static struct eh_region
*
601 expand_eh_region_end (void)
603 struct eh_region
*cur_region
= cfun
->eh
->cur_region
;
606 /* Create a note marking the end of this region. */
607 note
= emit_note (NOTE_INSN_EH_REGION_END
);
608 NOTE_EH_HANDLER (note
) = cur_region
->region_number
;
611 cfun
->eh
->cur_region
= cur_region
->outer
;
616 /* Expand HANDLER, which is the operand 1 of a TRY_CATCH_EXPR. Catch
617 blocks and C++ exception-specifications are handled specially. */
620 expand_eh_handler (tree handler
)
622 tree inner
= expr_first (handler
);
624 switch (TREE_CODE (inner
))
627 expand_start_all_catch ();
628 expand_expr (handler
, const0_rtx
, VOIDmode
, 0);
629 expand_end_all_catch ();
633 if (EH_FILTER_MUST_NOT_THROW (handler
))
634 expand_eh_region_end_must_not_throw (EH_FILTER_FAILURE (handler
));
636 expand_eh_region_end_allowed (EH_FILTER_TYPES (handler
),
637 EH_FILTER_FAILURE (handler
));
641 expand_eh_region_end_cleanup (handler
);
646 /* End an exception handling region for a cleanup. HANDLER is an
647 expression to expand for the cleanup. */
650 expand_eh_region_end_cleanup (tree handler
)
652 struct eh_region
*region
;
653 tree protect_cleanup_actions
;
660 region
= expand_eh_region_end ();
661 region
->type
= ERT_CLEANUP
;
662 region
->label
= gen_label_rtx ();
663 region
->u
.cleanup
.exp
= handler
;
664 region
->u
.cleanup
.prev_try
= cfun
->eh
->try_region
;
666 around_label
= gen_label_rtx ();
667 emit_jump (around_label
);
669 emit_label (region
->label
);
671 if (flag_non_call_exceptions
|| region
->may_contain_throw
)
673 /* Give the language a chance to specify an action to be taken if an
674 exception is thrown that would propagate out of the HANDLER. */
675 protect_cleanup_actions
676 = (lang_protect_cleanup_actions
677 ? (*lang_protect_cleanup_actions
) ()
680 if (protect_cleanup_actions
)
681 expand_eh_region_start ();
683 /* In case this cleanup involves an inline destructor with a try block in
684 it, we need to save the EH return data registers around it. */
685 data_save
[0] = gen_reg_rtx (ptr_mode
);
686 emit_move_insn (data_save
[0], get_exception_pointer (cfun
));
687 data_save
[1] = gen_reg_rtx (word_mode
);
688 emit_move_insn (data_save
[1], get_exception_filter (cfun
));
690 expand_expr (handler
, const0_rtx
, VOIDmode
, 0);
692 emit_move_insn (cfun
->eh
->exc_ptr
, data_save
[0]);
693 emit_move_insn (cfun
->eh
->filter
, data_save
[1]);
695 if (protect_cleanup_actions
)
696 expand_eh_region_end_must_not_throw (protect_cleanup_actions
);
698 /* We need any stack adjustment complete before the around_label. */
699 do_pending_stack_adjust ();
702 /* We delay the generation of the _Unwind_Resume until we generate
703 landing pads. We emit a marker here so as to get good control
704 flow data in the meantime. */
706 = emit_jump_insn (gen_rtx_RESX (VOIDmode
, region
->region_number
));
709 emit_label (around_label
);
713 expand_resx_expr (tree exp
)
715 int region_nr
= TREE_INT_CST_LOW (TREE_OPERAND (exp
, 0));
716 struct eh_region
*reg
= cfun
->eh
->region_array
[region_nr
];
718 reg
->resume
= emit_jump_insn (gen_rtx_RESX (VOIDmode
, region_nr
));
722 /* End an exception handling region for a try block, and prepares
723 for subsequent calls to expand_start_catch. */
726 expand_start_all_catch (void)
728 struct eh_region
*region
;
733 region
= expand_eh_region_end ();
734 region
->type
= ERT_TRY
;
735 region
->u
.try.prev_try
= cfun
->eh
->try_region
;
736 region
->u
.try.continue_label
= gen_label_rtx ();
738 cfun
->eh
->try_region
= region
;
740 emit_jump (region
->u
.try.continue_label
);
743 /* Begin a catch clause. TYPE is the type caught, a list of such
744 types, (in the case of Java) an ADDR_EXPR which points to the
745 runtime type to match, or null if this is a catch-all
746 clause. Providing a type list enables to associate the catch region
747 with potentially several exception types, which is useful e.g. for
751 expand_start_catch (tree type_or_list
)
759 c
= gen_eh_region_catch (cfun
->eh
->try_region
, type_or_list
);
760 cfun
->eh
->cur_region
= c
;
762 c
->label
= gen_label_rtx ();
763 emit_label (c
->label
);
765 note
= emit_note (NOTE_INSN_EH_REGION_BEG
);
766 NOTE_EH_HANDLER (note
) = c
->region_number
;
769 /* End a catch clause. Control will resume after the try/catch block. */
772 expand_end_catch (void)
777 expand_eh_region_end ();
778 emit_jump (cfun
->eh
->try_region
->u
.try.continue_label
);
781 /* End a sequence of catch handlers for a try block. */
784 expand_end_all_catch (void)
786 struct eh_region
*try_region
;
791 try_region
= cfun
->eh
->try_region
;
792 cfun
->eh
->try_region
= try_region
->u
.try.prev_try
;
794 emit_label (try_region
->u
.try.continue_label
);
797 /* End an exception region for an exception type filter. ALLOWED is a
798 TREE_LIST of types to be matched by the runtime. FAILURE is an
799 expression to invoke if a mismatch occurs.
801 ??? We could use these semantics for calls to rethrow, too; if we can
802 see the surrounding catch clause, we know that the exception we're
803 rethrowing satisfies the "filter" of the catch type. */
806 expand_eh_region_end_allowed (tree allowed
, tree failure
)
808 struct eh_region
*region
;
814 region
= expand_eh_region_end ();
815 region
->type
= ERT_ALLOWED_EXCEPTIONS
;
816 region
->u
.allowed
.type_list
= allowed
;
817 region
->label
= gen_label_rtx ();
819 for (; allowed
; allowed
= TREE_CHAIN (allowed
))
820 add_type_for_runtime (TREE_VALUE (allowed
));
822 /* We must emit the call to FAILURE here, so that if this function
823 throws a different exception, that it will be processed by the
826 around_label
= gen_label_rtx ();
827 emit_jump (around_label
);
829 emit_label (region
->label
);
830 expand_expr (failure
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
831 /* We must adjust the stack before we reach the AROUND_LABEL because
832 the call to FAILURE does not occur on all paths to the
834 do_pending_stack_adjust ();
836 emit_label (around_label
);
839 /* End an exception region for a must-not-throw filter. FAILURE is an
840 expression invoke if an uncaught exception propagates this far.
842 This is conceptually identical to expand_eh_region_end_allowed with
843 an empty allowed list (if you passed "std::terminate" instead of
844 "__cxa_call_unexpected"), but they are represented differently in
848 expand_eh_region_end_must_not_throw (tree failure
)
850 struct eh_region
*region
;
856 region
= expand_eh_region_end ();
857 region
->type
= ERT_MUST_NOT_THROW
;
858 region
->label
= gen_label_rtx ();
860 /* We must emit the call to FAILURE here, so that if this function
861 throws a different exception, that it will be processed by the
864 around_label
= gen_label_rtx ();
865 emit_jump (around_label
);
867 emit_label (region
->label
);
868 expand_expr (failure
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
870 emit_label (around_label
);
873 /* End an exception region for a throw. No handling goes on here,
874 but it's the easiest way for the front-end to indicate what type
878 expand_eh_region_end_throw (tree type
)
880 struct eh_region
*region
;
885 region
= expand_eh_region_end ();
886 region
->type
= ERT_THROW
;
887 region
->u
.throw.type
= type
;
890 /* End a fixup region. Within this region the cleanups for the immediately
891 enclosing region are _not_ run. This is used for goto cleanup to avoid
892 destroying an object twice.
894 This would be an extraordinarily simple prospect, were it not for the
895 fact that we don't actually know what the immediately enclosing region
896 is. This surprising fact is because expand_cleanups is currently
897 generating a sequence that it will insert somewhere else. We collect
898 the proper notion of "enclosing" in convert_from_eh_region_ranges. */
901 expand_eh_region_end_fixup (tree handler
)
903 struct eh_region
*fixup
;
908 fixup
= expand_eh_region_end ();
909 fixup
->type
= ERT_FIXUP
;
910 fixup
->u
.fixup
.cleanup_exp
= handler
;
913 /* Note that the current EH region (if any) may contain a throw, or a
914 call to a function which itself may contain a throw. */
917 note_eh_region_may_contain_throw (struct eh_region
*region
)
919 while (region
&& !region
->may_contain_throw
)
921 region
->may_contain_throw
= 1;
922 region
= region
->outer
;
927 note_current_region_may_contain_throw (void)
929 note_eh_region_may_contain_throw (cfun
->eh
->cur_region
);
933 /* Return an rtl expression for a pointer to the exception object
937 get_exception_pointer (struct function
*fun
)
939 rtx exc_ptr
= fun
->eh
->exc_ptr
;
940 if (fun
== cfun
&& ! exc_ptr
)
942 exc_ptr
= gen_reg_rtx (ptr_mode
);
943 fun
->eh
->exc_ptr
= exc_ptr
;
948 /* Return an rtl expression for the exception dispatch filter
952 get_exception_filter (struct function
*fun
)
954 rtx filter
= fun
->eh
->filter
;
955 if (fun
== cfun
&& ! filter
)
957 filter
= gen_reg_rtx (word_mode
);
958 fun
->eh
->filter
= filter
;
963 /* This section is for the exception handling specific optimization pass. */
965 /* Random access the exception region tree. It's just as simple to
966 collect the regions this way as in expand_eh_region_start, but
967 without having to realloc memory. */
970 collect_eh_region_array (void)
972 struct eh_region
**array
, *i
;
974 i
= cfun
->eh
->region_tree
;
978 array
= ggc_alloc_cleared ((cfun
->eh
->last_region_number
+ 1)
980 cfun
->eh
->region_array
= array
;
984 array
[i
->region_number
] = i
;
986 /* If there are sub-regions, process them. */
989 /* If there are peers, process them. */
990 else if (i
->next_peer
)
992 /* Otherwise, step back up the tree to the next peer. */
999 } while (i
->next_peer
== NULL
);
1006 resolve_one_fixup_region (struct eh_region
*fixup
)
1008 struct eh_region
*cleanup
, *real
;
1011 n
= cfun
->eh
->last_region_number
;
1014 for (j
= 1; j
<= n
; ++j
)
1016 cleanup
= cfun
->eh
->region_array
[j
];
1017 if (cleanup
&& cleanup
->type
== ERT_CLEANUP
1018 && cleanup
->u
.cleanup
.exp
== fixup
->u
.fixup
.cleanup_exp
)
1024 real
= cleanup
->outer
;
1025 if (real
&& real
->type
== ERT_FIXUP
)
1027 if (!real
->u
.fixup
.resolved
)
1028 resolve_one_fixup_region (real
);
1029 real
= real
->u
.fixup
.real_region
;
1032 fixup
->u
.fixup
.real_region
= real
;
1033 fixup
->u
.fixup
.resolved
= true;
1037 resolve_fixup_regions (void)
1039 int i
, n
= cfun
->eh
->last_region_number
;
1041 for (i
= 1; i
<= n
; ++i
)
1043 struct eh_region
*fixup
= cfun
->eh
->region_array
[i
];
1045 if (!fixup
|| fixup
->type
!= ERT_FIXUP
|| fixup
->u
.fixup
.resolved
)
1048 resolve_one_fixup_region (fixup
);
1052 /* Now that we've discovered what region actually encloses a fixup,
1053 we can shuffle pointers and remove them from the tree. */
1056 remove_fixup_regions (void)
1060 struct eh_region
*fixup
;
1062 /* Walk the insn chain and adjust the REG_EH_REGION numbers
1063 for instructions referencing fixup regions. This is only
1064 strictly necessary for fixup regions with no parent, but
1065 doesn't hurt to do it for all regions. */
1066 for (insn
= get_insns(); insn
; insn
= NEXT_INSN (insn
))
1068 && (note
= find_reg_note (insn
, REG_EH_REGION
, NULL
))
1069 && INTVAL (XEXP (note
, 0)) > 0
1070 && (fixup
= cfun
->eh
->region_array
[INTVAL (XEXP (note
, 0))])
1071 && fixup
->type
== ERT_FIXUP
)
1073 if (fixup
->u
.fixup
.real_region
)
1074 XEXP (note
, 0) = GEN_INT (fixup
->u
.fixup
.real_region
->region_number
);
1076 remove_note (insn
, note
);
1079 /* Remove the fixup regions from the tree. */
1080 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1082 fixup
= cfun
->eh
->region_array
[i
];
1086 /* Allow GC to maybe free some memory. */
1087 if (fixup
->type
== ERT_CLEANUP
)
1088 fixup
->u
.cleanup
.exp
= NULL_TREE
;
1090 if (fixup
->type
!= ERT_FIXUP
)
1095 struct eh_region
*parent
, *p
, **pp
;
1097 parent
= fixup
->u
.fixup
.real_region
;
1099 /* Fix up the children's parent pointers; find the end of
1101 for (p
= fixup
->inner
; ; p
= p
->next_peer
)
1108 /* In the tree of cleanups, only outer-inner ordering matters.
1109 So link the children back in anywhere at the correct level. */
1111 pp
= &parent
->inner
;
1113 pp
= &cfun
->eh
->region_tree
;
1116 fixup
->inner
= NULL
;
1119 remove_eh_handler (fixup
);
1123 /* Remove all regions whose labels are not reachable from insns. */
1126 remove_unreachable_regions (rtx insns
)
1128 int i
, *uid_region_num
;
1130 struct eh_region
*r
;
1133 uid_region_num
= xcalloc (get_max_uid (), sizeof(int));
1134 reachable
= xcalloc (cfun
->eh
->last_region_number
+ 1, sizeof(bool));
1136 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1138 r
= cfun
->eh
->region_array
[i
];
1139 if (!r
|| r
->region_number
!= i
)
1144 if (uid_region_num
[INSN_UID (r
->resume
)])
1146 uid_region_num
[INSN_UID (r
->resume
)] = i
;
1150 if (uid_region_num
[INSN_UID (r
->label
)])
1152 uid_region_num
[INSN_UID (r
->label
)] = i
;
1156 for (insn
= insns
; insn
; insn
= NEXT_INSN (insn
))
1157 reachable
[uid_region_num
[INSN_UID (insn
)]] = true;
1159 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1161 r
= cfun
->eh
->region_array
[i
];
1162 if (r
&& r
->region_number
== i
&& !reachable
[i
])
1164 bool kill_it
= true;
1168 /* Don't remove ERT_THROW regions if their outer region
1170 if (r
->outer
&& reachable
[r
->outer
->region_number
])
1174 case ERT_MUST_NOT_THROW
:
1175 /* MUST_NOT_THROW regions are implementable solely in the
1176 runtime, but their existence continues to affect calls
1177 within that region. Never delete them here. */
1183 /* TRY regions are reachable if any of its CATCH regions
1185 struct eh_region
*c
;
1186 for (c
= r
->u
.try.catch; c
; c
= c
->u
.catch.next_catch
)
1187 if (reachable
[c
->region_number
])
1200 remove_eh_handler (r
);
1205 free (uid_region_num
);
1208 /* Turn NOTE_INSN_EH_REGION notes into REG_EH_REGION notes for each
1209 can_throw instruction in the region. */
1212 convert_from_eh_region_ranges_1 (rtx
*pinsns
, int *orig_sp
, int cur
)
1217 for (insn
= *pinsns
; insn
; insn
= next
)
1219 next
= NEXT_INSN (insn
);
1220 if (GET_CODE (insn
) == NOTE
)
1222 int kind
= NOTE_LINE_NUMBER (insn
);
1223 if (kind
== NOTE_INSN_EH_REGION_BEG
1224 || kind
== NOTE_INSN_EH_REGION_END
)
1226 if (kind
== NOTE_INSN_EH_REGION_BEG
)
1228 struct eh_region
*r
;
1231 cur
= NOTE_EH_HANDLER (insn
);
1233 r
= cfun
->eh
->region_array
[cur
];
1234 if (r
->type
== ERT_FIXUP
)
1236 r
= r
->u
.fixup
.real_region
;
1237 cur
= r
? r
->region_number
: 0;
1239 else if (r
->type
== ERT_CATCH
)
1242 cur
= r
? r
->region_number
: 0;
1248 if (insn
== *pinsns
)
1254 else if (INSN_P (insn
))
1257 && ! find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
)
1258 /* Calls can always potentially throw exceptions, unless
1259 they have a REG_EH_REGION note with a value of 0 or less.
1260 Which should be the only possible kind so far. */
1261 && (GET_CODE (insn
) == CALL_INSN
1262 /* If we wanted exceptions for non-call insns, then
1263 any may_trap_p instruction could throw. */
1264 || (flag_non_call_exceptions
1265 && GET_CODE (PATTERN (insn
)) != CLOBBER
1266 && GET_CODE (PATTERN (insn
)) != USE
1267 && may_trap_p (PATTERN (insn
)))))
1269 REG_NOTES (insn
) = alloc_EXPR_LIST (REG_EH_REGION
, GEN_INT (cur
),
1280 collect_rtl_labels_from_trees (void)
1282 int i
, n
= cfun
->eh
->last_region_number
;
1283 for (i
= 1; i
<= n
; ++i
)
1285 struct eh_region
*reg
= cfun
->eh
->region_array
[i
];
1286 if (reg
&& reg
->tree_label
)
1287 reg
->label
= DECL_RTL_IF_SET (reg
->tree_label
);
1292 convert_from_eh_region_ranges (void)
1294 rtx insns
= get_insns ();
1296 if (cfun
->eh
->region_array
)
1298 /* If the region array already exists, assume we're coming from
1299 optimize_function_tree. In this case all we need to do is
1300 collect the rtl labels that correspond to the tree labels
1301 that we allocated earlier. */
1302 collect_rtl_labels_from_trees ();
1308 collect_eh_region_array ();
1309 resolve_fixup_regions ();
1311 stack
= xmalloc (sizeof (int) * (cfun
->eh
->last_region_number
+ 1));
1312 convert_from_eh_region_ranges_1 (&insns
, stack
, 0);
1315 remove_fixup_regions ();
1318 remove_unreachable_regions (insns
);
1322 add_ehl_entry (rtx label
, struct eh_region
*region
)
1324 struct ehl_map_entry
**slot
, *entry
;
1326 LABEL_PRESERVE_P (label
) = 1;
1328 entry
= ggc_alloc (sizeof (*entry
));
1329 entry
->label
= label
;
1330 entry
->region
= region
;
1332 slot
= (struct ehl_map_entry
**)
1333 htab_find_slot (cfun
->eh
->exception_handler_label_map
, entry
, INSERT
);
1335 /* Before landing pad creation, each exception handler has its own
1336 label. After landing pad creation, the exception handlers may
1337 share landing pads. This is ok, since maybe_remove_eh_handler
1338 only requires the 1-1 mapping before landing pad creation. */
1339 if (*slot
&& !cfun
->eh
->built_landing_pads
)
1346 find_exception_handler_labels (void)
1350 if (cfun
->eh
->exception_handler_label_map
)
1351 htab_empty (cfun
->eh
->exception_handler_label_map
);
1354 /* ??? The expansion factor here (3/2) must be greater than the htab
1355 occupancy factor (4/3) to avoid unnecessary resizing. */
1356 cfun
->eh
->exception_handler_label_map
1357 = htab_create_ggc (cfun
->eh
->last_region_number
* 3 / 2,
1358 ehl_hash
, ehl_eq
, NULL
);
1361 if (cfun
->eh
->region_tree
== NULL
)
1364 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1366 struct eh_region
*region
= cfun
->eh
->region_array
[i
];
1369 if (! region
|| region
->region_number
!= i
)
1371 if (cfun
->eh
->built_landing_pads
)
1372 lab
= region
->landing_pad
;
1374 lab
= region
->label
;
1377 add_ehl_entry (lab
, region
);
1380 /* For sjlj exceptions, need the return label to remain live until
1381 after landing pad generation. */
1382 if (USING_SJLJ_EXCEPTIONS
&& ! cfun
->eh
->built_landing_pads
)
1383 add_ehl_entry (return_label
, NULL
);
1387 current_function_has_exception_handlers (void)
1391 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1393 struct eh_region
*region
= cfun
->eh
->region_array
[i
];
1395 if (! region
|| region
->region_number
!= i
)
1397 if (region
->type
!= ERT_THROW
)
1404 static struct eh_region
*
1405 duplicate_eh_region_1 (struct eh_region
*o
, struct inline_remap
*map
)
1407 struct eh_region
*n
= ggc_alloc_cleared (sizeof (struct eh_region
));
1409 n
->region_number
= o
->region_number
+ cfun
->eh
->last_region_number
;
1415 case ERT_MUST_NOT_THROW
:
1419 if (o
->u
.try.continue_label
)
1420 n
->u
.try.continue_label
1421 = get_label_from_map (map
,
1422 CODE_LABEL_NUMBER (o
->u
.try.continue_label
));
1426 n
->u
.catch.type_list
= o
->u
.catch.type_list
;
1429 case ERT_ALLOWED_EXCEPTIONS
:
1430 n
->u
.allowed
.type_list
= o
->u
.allowed
.type_list
;
1434 n
->u
.throw.type
= o
->u
.throw.type
;
1441 n
->label
= get_label_from_map (map
, CODE_LABEL_NUMBER (o
->label
));
1444 n
->resume
= map
->insn_map
[INSN_UID (o
->resume
)];
1445 if (n
->resume
== NULL
)
1453 duplicate_eh_region_2 (struct eh_region
*o
, struct eh_region
**n_array
)
1455 struct eh_region
*n
= n_array
[o
->region_number
];
1460 n
->u
.try.catch = n_array
[o
->u
.try.catch->region_number
];
1461 n
->u
.try.last_catch
= n_array
[o
->u
.try.last_catch
->region_number
];
1465 if (o
->u
.catch.next_catch
)
1466 n
->u
.catch.next_catch
= n_array
[o
->u
.catch.next_catch
->region_number
];
1467 if (o
->u
.catch.prev_catch
)
1468 n
->u
.catch.prev_catch
= n_array
[o
->u
.catch.prev_catch
->region_number
];
1476 n
->outer
= n_array
[o
->outer
->region_number
];
1478 n
->inner
= n_array
[o
->inner
->region_number
];
1480 n
->next_peer
= n_array
[o
->next_peer
->region_number
];
1484 duplicate_eh_regions (struct function
*ifun
, struct inline_remap
*map
)
1486 int ifun_last_region_number
= ifun
->eh
->last_region_number
;
1487 struct eh_region
**n_array
, *root
, *cur
;
1490 if (ifun_last_region_number
== 0)
1493 n_array
= xcalloc (ifun_last_region_number
+ 1, sizeof (*n_array
));
1495 for (i
= 1; i
<= ifun_last_region_number
; ++i
)
1497 cur
= ifun
->eh
->region_array
[i
];
1498 if (!cur
|| cur
->region_number
!= i
)
1500 n_array
[i
] = duplicate_eh_region_1 (cur
, map
);
1502 for (i
= 1; i
<= ifun_last_region_number
; ++i
)
1504 cur
= ifun
->eh
->region_array
[i
];
1505 if (!cur
|| cur
->region_number
!= i
)
1507 duplicate_eh_region_2 (cur
, n_array
);
1510 root
= n_array
[ifun
->eh
->region_tree
->region_number
];
1511 cur
= cfun
->eh
->cur_region
;
1514 struct eh_region
*p
= cur
->inner
;
1517 while (p
->next_peer
)
1519 p
->next_peer
= root
;
1524 for (i
= 1; i
<= ifun_last_region_number
; ++i
)
1525 if (n_array
[i
] && n_array
[i
]->outer
== NULL
)
1526 n_array
[i
]->outer
= cur
;
1530 struct eh_region
*p
= cfun
->eh
->region_tree
;
1533 while (p
->next_peer
)
1535 p
->next_peer
= root
;
1538 cfun
->eh
->region_tree
= root
;
1543 i
= cfun
->eh
->last_region_number
;
1544 cfun
->eh
->last_region_number
= i
+ ifun_last_region_number
;
1550 t2r_eq (const void *pentry
, const void *pdata
)
1552 tree entry
= (tree
) pentry
;
1553 tree data
= (tree
) pdata
;
1555 return TREE_PURPOSE (entry
) == data
;
1559 t2r_hash (const void *pentry
)
1561 tree entry
= (tree
) pentry
;
1562 return TREE_HASH (TREE_PURPOSE (entry
));
1566 add_type_for_runtime (tree type
)
1570 slot
= (tree
*) htab_find_slot_with_hash (type_to_runtime_map
, type
,
1571 TREE_HASH (type
), INSERT
);
1574 tree runtime
= (*lang_eh_runtime_type
) (type
);
1575 *slot
= tree_cons (type
, runtime
, NULL_TREE
);
1580 lookup_type_for_runtime (tree type
)
1584 slot
= (tree
*) htab_find_slot_with_hash (type_to_runtime_map
, type
,
1585 TREE_HASH (type
), NO_INSERT
);
1587 /* We should have always inserted the data earlier. */
1588 return TREE_VALUE (*slot
);
1592 /* Represent an entry in @TTypes for either catch actions
1593 or exception filter actions. */
1594 struct ttypes_filter
GTY(())
1600 /* Compare ENTRY (a ttypes_filter entry in the hash table) with DATA
1601 (a tree) for a @TTypes type node we are thinking about adding. */
1604 ttypes_filter_eq (const void *pentry
, const void *pdata
)
1606 const struct ttypes_filter
*entry
= (const struct ttypes_filter
*) pentry
;
1607 tree data
= (tree
) pdata
;
1609 return entry
->t
== data
;
1613 ttypes_filter_hash (const void *pentry
)
1615 const struct ttypes_filter
*entry
= (const struct ttypes_filter
*) pentry
;
1616 return TREE_HASH (entry
->t
);
1619 /* Compare ENTRY with DATA (both struct ttypes_filter) for a @TTypes
1620 exception specification list we are thinking about adding. */
1621 /* ??? Currently we use the type lists in the order given. Someone
1622 should put these in some canonical order. */
1625 ehspec_filter_eq (const void *pentry
, const void *pdata
)
1627 const struct ttypes_filter
*entry
= (const struct ttypes_filter
*) pentry
;
1628 const struct ttypes_filter
*data
= (const struct ttypes_filter
*) pdata
;
1630 return type_list_equal (entry
->t
, data
->t
);
1633 /* Hash function for exception specification lists. */
1636 ehspec_filter_hash (const void *pentry
)
1638 const struct ttypes_filter
*entry
= (const struct ttypes_filter
*) pentry
;
1642 for (list
= entry
->t
; list
; list
= TREE_CHAIN (list
))
1643 h
= (h
<< 5) + (h
>> 27) + TREE_HASH (TREE_VALUE (list
));
1647 /* Add TYPE (which may be NULL) to cfun->eh->ttype_data, using TYPES_HASH
1648 to speed up the search. Return the filter value to be used. */
1651 add_ttypes_entry (htab_t ttypes_hash
, tree type
)
1653 struct ttypes_filter
**slot
, *n
;
1655 slot
= (struct ttypes_filter
**)
1656 htab_find_slot_with_hash (ttypes_hash
, type
, TREE_HASH (type
), INSERT
);
1658 if ((n
= *slot
) == NULL
)
1660 /* Filter value is a 1 based table index. */
1662 n
= xmalloc (sizeof (*n
));
1664 n
->filter
= VARRAY_ACTIVE_SIZE (cfun
->eh
->ttype_data
) + 1;
1667 VARRAY_PUSH_TREE (cfun
->eh
->ttype_data
, type
);
1673 /* Add LIST to cfun->eh->ehspec_data, using EHSPEC_HASH and TYPES_HASH
1674 to speed up the search. Return the filter value to be used. */
1677 add_ehspec_entry (htab_t ehspec_hash
, htab_t ttypes_hash
, tree list
)
1679 struct ttypes_filter
**slot
, *n
;
1680 struct ttypes_filter dummy
;
1683 slot
= (struct ttypes_filter
**)
1684 htab_find_slot (ehspec_hash
, &dummy
, INSERT
);
1686 if ((n
= *slot
) == NULL
)
1688 /* Filter value is a -1 based byte index into a uleb128 buffer. */
1690 n
= xmalloc (sizeof (*n
));
1692 n
->filter
= -(VARRAY_ACTIVE_SIZE (cfun
->eh
->ehspec_data
) + 1);
1695 /* Look up each type in the list and encode its filter
1696 value as a uleb128. Terminate the list with 0. */
1697 for (; list
; list
= TREE_CHAIN (list
))
1698 push_uleb128 (&cfun
->eh
->ehspec_data
,
1699 add_ttypes_entry (ttypes_hash
, TREE_VALUE (list
)));
1700 VARRAY_PUSH_UCHAR (cfun
->eh
->ehspec_data
, 0);
1706 /* Generate the action filter values to be used for CATCH and
1707 ALLOWED_EXCEPTIONS regions. When using dwarf2 exception regions,
1708 we use lots of landing pads, and so every type or list can share
1709 the same filter value, which saves table space. */
1712 assign_filter_values (void)
1715 htab_t ttypes
, ehspec
;
1717 VARRAY_TREE_INIT (cfun
->eh
->ttype_data
, 16, "ttype_data");
1718 VARRAY_UCHAR_INIT (cfun
->eh
->ehspec_data
, 64, "ehspec_data");
1720 ttypes
= htab_create (31, ttypes_filter_hash
, ttypes_filter_eq
, free
);
1721 ehspec
= htab_create (31, ehspec_filter_hash
, ehspec_filter_eq
, free
);
1723 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1725 struct eh_region
*r
= cfun
->eh
->region_array
[i
];
1727 /* Mind we don't process a region more than once. */
1728 if (!r
|| r
->region_number
!= i
)
1734 /* Whatever type_list is (NULL or true list), we build a list
1735 of filters for the region. */
1736 r
->u
.catch.filter_list
= NULL_TREE
;
1738 if (r
->u
.catch.type_list
!= NULL
)
1740 /* Get a filter value for each of the types caught and store
1741 them in the region's dedicated list. */
1742 tree tp_node
= r
->u
.catch.type_list
;
1744 for (;tp_node
; tp_node
= TREE_CHAIN (tp_node
))
1746 int flt
= add_ttypes_entry (ttypes
, TREE_VALUE (tp_node
));
1747 tree flt_node
= build_int_2 (flt
, 0);
1749 r
->u
.catch.filter_list
1750 = tree_cons (NULL_TREE
, flt_node
, r
->u
.catch.filter_list
);
1755 /* Get a filter value for the NULL list also since it will need
1756 an action record anyway. */
1757 int flt
= add_ttypes_entry (ttypes
, NULL
);
1758 tree flt_node
= build_int_2 (flt
, 0);
1760 r
->u
.catch.filter_list
1761 = tree_cons (NULL_TREE
, flt_node
, r
->u
.catch.filter_list
);
1766 case ERT_ALLOWED_EXCEPTIONS
:
1768 = add_ehspec_entry (ehspec
, ttypes
, r
->u
.allowed
.type_list
);
1776 htab_delete (ttypes
);
1777 htab_delete (ehspec
);
1780 /* Emit SEQ into basic block just before INSN (that is assumed to be
1781 first instruction of some existing BB and return the newly
1784 emit_to_new_bb_before (rtx seq
, rtx insn
)
1790 /* If there happens to be an fallthru edge (possibly created by cleanup_cfg
1791 call), we don't want it to go into newly created landing pad or other EH
1793 for (e
= BLOCK_FOR_INSN (insn
)->pred
; e
; e
= e
->pred_next
)
1794 if (e
->flags
& EDGE_FALLTHRU
)
1795 force_nonfallthru (e
);
1796 last
= emit_insn_before (seq
, insn
);
1797 if (GET_CODE (last
) == BARRIER
)
1798 last
= PREV_INSN (last
);
1799 bb
= create_basic_block (seq
, last
, BLOCK_FOR_INSN (insn
)->prev_bb
);
1800 update_bb_for_insn (bb
);
1801 bb
->flags
|= BB_SUPERBLOCK
;
1805 /* Generate the code to actually handle exceptions, which will follow the
1809 build_post_landing_pads (void)
1813 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1815 struct eh_region
*region
= cfun
->eh
->region_array
[i
];
1818 /* Mind we don't process a region more than once. */
1819 if (!region
|| region
->region_number
!= i
)
1822 switch (region
->type
)
1825 /* ??? Collect the set of all non-overlapping catch handlers
1826 all the way up the chain until blocked by a cleanup. */
1827 /* ??? Outer try regions can share landing pads with inner
1828 try regions if the types are completely non-overlapping,
1829 and there are no intervening cleanups. */
1831 region
->post_landing_pad
= gen_label_rtx ();
1835 emit_label (region
->post_landing_pad
);
1837 /* ??? It is mighty inconvenient to call back into the
1838 switch statement generation code in expand_end_case.
1839 Rapid prototyping sez a sequence of ifs. */
1841 struct eh_region
*c
;
1842 for (c
= region
->u
.try.catch; c
; c
= c
->u
.catch.next_catch
)
1844 if (c
->u
.catch.type_list
== NULL
)
1845 emit_jump (c
->label
);
1848 /* Need for one cmp/jump per type caught. Each type
1849 list entry has a matching entry in the filter list
1850 (see assign_filter_values). */
1851 tree tp_node
= c
->u
.catch.type_list
;
1852 tree flt_node
= c
->u
.catch.filter_list
;
1856 emit_cmp_and_jump_insns
1858 GEN_INT (tree_low_cst (TREE_VALUE (flt_node
), 0)),
1859 EQ
, NULL_RTX
, word_mode
, 0, c
->label
);
1861 tp_node
= TREE_CHAIN (tp_node
);
1862 flt_node
= TREE_CHAIN (flt_node
);
1868 /* We delay the generation of the _Unwind_Resume until we generate
1869 landing pads. We emit a marker here so as to get good control
1870 flow data in the meantime. */
1872 = emit_jump_insn (gen_rtx_RESX (VOIDmode
, region
->region_number
));
1878 emit_to_new_bb_before (seq
, region
->u
.try.catch->label
);
1882 case ERT_ALLOWED_EXCEPTIONS
:
1883 region
->post_landing_pad
= gen_label_rtx ();
1887 emit_label (region
->post_landing_pad
);
1889 emit_cmp_and_jump_insns (cfun
->eh
->filter
,
1890 GEN_INT (region
->u
.allowed
.filter
),
1891 EQ
, NULL_RTX
, word_mode
, 0, region
->label
);
1893 /* We delay the generation of the _Unwind_Resume until we generate
1894 landing pads. We emit a marker here so as to get good control
1895 flow data in the meantime. */
1897 = emit_jump_insn (gen_rtx_RESX (VOIDmode
, region
->region_number
));
1903 emit_to_new_bb_before (seq
, region
->label
);
1907 case ERT_MUST_NOT_THROW
:
1908 region
->post_landing_pad
= region
->label
;
1913 /* Nothing to do. */
1922 /* Replace RESX patterns with jumps to the next handler if any, or calls to
1923 _Unwind_Resume otherwise. */
1926 connect_post_landing_pads (void)
1930 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1932 struct eh_region
*region
= cfun
->eh
->region_array
[i
];
1933 struct eh_region
*outer
;
1937 /* Mind we don't process a region more than once. */
1938 if (!region
|| region
->region_number
!= i
)
1941 /* If there is no RESX, or it has been deleted by flow, there's
1942 nothing to fix up. */
1943 if (! region
->resume
|| INSN_DELETED_P (region
->resume
))
1946 /* Search for another landing pad in this function. */
1947 for (outer
= region
->outer
; outer
; outer
= outer
->outer
)
1948 if (outer
->post_landing_pad
)
1956 basic_block src
, dest
;
1958 emit_jump (outer
->post_landing_pad
);
1959 src
= BLOCK_FOR_INSN (region
->resume
);
1960 dest
= BLOCK_FOR_INSN (outer
->post_landing_pad
);
1962 remove_edge (src
->succ
);
1963 e
= make_edge (src
, dest
, 0);
1964 e
->probability
= REG_BR_PROB_BASE
;
1965 e
->count
= src
->count
;
1969 emit_library_call (unwind_resume_libfunc
, LCT_THROW
,
1970 VOIDmode
, 1, cfun
->eh
->exc_ptr
, ptr_mode
);
1972 /* What we just emitted was a throwing libcall, so it got a
1973 barrier automatically added after it. If the last insn in
1974 the libcall sequence isn't the barrier, it's because the
1975 target emits multiple insns for a call, and there are insns
1976 after the actual call insn (which are redundant and would be
1977 optimized away). The barrier is inserted exactly after the
1978 call insn, so let's go get that and delete the insns after
1979 it, because below we need the barrier to be the last insn in
1981 delete_insns_since (NEXT_INSN (last_call_insn ()));
1986 barrier
= emit_insn_before (seq
, region
->resume
);
1987 /* Avoid duplicate barrier. */
1988 if (GET_CODE (barrier
) != BARRIER
)
1990 delete_insn (barrier
);
1991 delete_insn (region
->resume
);
1993 /* ??? From tree-ssa we can wind up with catch regions whose
1994 label is not instantiated, but whose resx is present. Now
1995 that we've dealt with the resx, kill the region. */
1996 if (region
->label
== NULL
&& region
->type
== ERT_CLEANUP
)
1997 remove_eh_handler (region
);
2003 dw2_build_landing_pads (void)
2008 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
2010 struct eh_region
*region
= cfun
->eh
->region_array
[i
];
2013 bool clobbers_hard_regs
= false;
2016 /* Mind we don't process a region more than once. */
2017 if (!region
|| region
->region_number
!= i
)
2020 if (region
->type
!= ERT_CLEANUP
2021 && region
->type
!= ERT_TRY
2022 && region
->type
!= ERT_ALLOWED_EXCEPTIONS
)
2027 region
->landing_pad
= gen_label_rtx ();
2028 emit_label (region
->landing_pad
);
2030 #ifdef HAVE_exception_receiver
2031 if (HAVE_exception_receiver
)
2032 emit_insn (gen_exception_receiver ());
2035 #ifdef HAVE_nonlocal_goto_receiver
2036 if (HAVE_nonlocal_goto_receiver
)
2037 emit_insn (gen_nonlocal_goto_receiver ());
2042 /* If the eh_return data registers are call-saved, then we
2043 won't have considered them clobbered from the call that
2044 threw. Kill them now. */
2047 unsigned r
= EH_RETURN_DATA_REGNO (j
);
2048 if (r
== INVALID_REGNUM
)
2050 if (! call_used_regs
[r
])
2052 emit_insn (gen_rtx_CLOBBER (VOIDmode
, gen_rtx_REG (Pmode
, r
)));
2053 clobbers_hard_regs
= true;
2057 if (clobbers_hard_regs
)
2059 /* @@@ This is a kludge. Not all machine descriptions define a
2060 blockage insn, but we must not allow the code we just generated
2061 to be reordered by scheduling. So emit an ASM_INPUT to act as
2063 emit_insn (gen_rtx_ASM_INPUT (VOIDmode
, ""));
2066 emit_move_insn (cfun
->eh
->exc_ptr
,
2067 gen_rtx_REG (ptr_mode
, EH_RETURN_DATA_REGNO (0)));
2068 emit_move_insn (cfun
->eh
->filter
,
2069 gen_rtx_REG (word_mode
, EH_RETURN_DATA_REGNO (1)));
2074 bb
= emit_to_new_bb_before (seq
, region
->post_landing_pad
);
2075 e
= make_edge (bb
, bb
->next_bb
, EDGE_FALLTHRU
);
2076 e
->count
= bb
->count
;
2077 e
->probability
= REG_BR_PROB_BASE
;
2084 int directly_reachable
;
2087 int call_site_index
;
2091 sjlj_find_directly_reachable_regions (struct sjlj_lp_info
*lp_info
)
2094 bool found_one
= false;
2096 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
2098 struct eh_region
*region
;
2099 enum reachable_code rc
;
2103 if (! INSN_P (insn
))
2106 note
= find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
);
2107 if (!note
|| INTVAL (XEXP (note
, 0)) <= 0)
2110 region
= cfun
->eh
->region_array
[INTVAL (XEXP (note
, 0))];
2112 type_thrown
= NULL_TREE
;
2113 if (region
->type
== ERT_THROW
)
2115 type_thrown
= region
->u
.throw.type
;
2116 region
= region
->outer
;
2119 /* Find the first containing region that might handle the exception.
2120 That's the landing pad to which we will transfer control. */
2121 rc
= RNL_NOT_CAUGHT
;
2122 for (; region
; region
= region
->outer
)
2124 rc
= reachable_next_level (region
, type_thrown
, NULL
);
2125 if (rc
!= RNL_NOT_CAUGHT
)
2128 if (rc
== RNL_MAYBE_CAUGHT
|| rc
== RNL_CAUGHT
)
2130 lp_info
[region
->region_number
].directly_reachable
= 1;
2139 sjlj_assign_call_site_values (rtx dispatch_label
, struct sjlj_lp_info
*lp_info
)
2144 /* First task: build the action table. */
2146 VARRAY_UCHAR_INIT (cfun
->eh
->action_record_data
, 64, "action_record_data");
2147 ar_hash
= htab_create (31, action_record_hash
, action_record_eq
, free
);
2149 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
2150 if (lp_info
[i
].directly_reachable
)
2152 struct eh_region
*r
= cfun
->eh
->region_array
[i
];
2153 r
->landing_pad
= dispatch_label
;
2154 lp_info
[i
].action_index
= collect_one_action_chain (ar_hash
, r
);
2155 if (lp_info
[i
].action_index
!= -1)
2156 cfun
->uses_eh_lsda
= 1;
2159 htab_delete (ar_hash
);
2161 /* Next: assign dispatch values. In dwarf2 terms, this would be the
2162 landing pad label for the region. For sjlj though, there is one
2163 common landing pad from which we dispatch to the post-landing pads.
2165 A region receives a dispatch index if it is directly reachable
2166 and requires in-function processing. Regions that share post-landing
2167 pads may share dispatch indices. */
2168 /* ??? Post-landing pad sharing doesn't actually happen at the moment
2169 (see build_post_landing_pads) so we don't bother checking for it. */
2172 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
2173 if (lp_info
[i
].directly_reachable
)
2174 lp_info
[i
].dispatch_index
= index
++;
2176 /* Finally: assign call-site values. If dwarf2 terms, this would be
2177 the region number assigned by convert_to_eh_region_ranges, but
2178 handles no-action and must-not-throw differently. */
2181 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
2182 if (lp_info
[i
].directly_reachable
)
2184 int action
= lp_info
[i
].action_index
;
2186 /* Map must-not-throw to otherwise unused call-site index 0. */
2189 /* Map no-action to otherwise unused call-site index -1. */
2190 else if (action
== -1)
2192 /* Otherwise, look it up in the table. */
2194 index
= add_call_site (GEN_INT (lp_info
[i
].dispatch_index
), action
);
2196 lp_info
[i
].call_site_index
= index
;
2201 sjlj_mark_call_sites (struct sjlj_lp_info
*lp_info
)
2203 int last_call_site
= -2;
2206 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
2208 struct eh_region
*region
;
2210 rtx note
, before
, p
;
2212 /* Reset value tracking at extended basic block boundaries. */
2213 if (GET_CODE (insn
) == CODE_LABEL
)
2214 last_call_site
= -2;
2216 if (! INSN_P (insn
))
2219 note
= find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
);
2222 /* Calls (and trapping insns) without notes are outside any
2223 exception handling region in this function. Mark them as
2225 if (GET_CODE (insn
) == CALL_INSN
2226 || (flag_non_call_exceptions
2227 && may_trap_p (PATTERN (insn
))))
2228 this_call_site
= -1;
2234 /* Calls that are known to not throw need not be marked. */
2235 if (INTVAL (XEXP (note
, 0)) <= 0)
2238 region
= cfun
->eh
->region_array
[INTVAL (XEXP (note
, 0))];
2239 this_call_site
= lp_info
[region
->region_number
].call_site_index
;
2242 if (this_call_site
== last_call_site
)
2245 /* Don't separate a call from it's argument loads. */
2247 if (GET_CODE (insn
) == CALL_INSN
)
2248 before
= find_first_parameter_load (insn
, NULL_RTX
);
2251 mem
= adjust_address (cfun
->eh
->sjlj_fc
, TYPE_MODE (integer_type_node
),
2252 sjlj_fc_call_site_ofs
);
2253 emit_move_insn (mem
, GEN_INT (this_call_site
));
2257 emit_insn_before (p
, before
);
2258 last_call_site
= this_call_site
;
2262 /* Construct the SjLj_Function_Context. */
2265 sjlj_emit_function_enter (rtx dispatch_label
)
2267 rtx fn_begin
, fc
, mem
, seq
;
2269 fc
= cfun
->eh
->sjlj_fc
;
2273 /* We're storing this libcall's address into memory instead of
2274 calling it directly. Thus, we must call assemble_external_libcall
2275 here, as we can not depend on emit_library_call to do it for us. */
2276 assemble_external_libcall (eh_personality_libfunc
);
2277 mem
= adjust_address (fc
, Pmode
, sjlj_fc_personality_ofs
);
2278 emit_move_insn (mem
, eh_personality_libfunc
);
2280 mem
= adjust_address (fc
, Pmode
, sjlj_fc_lsda_ofs
);
2281 if (cfun
->uses_eh_lsda
)
2286 ASM_GENERATE_INTERNAL_LABEL (buf
, "LLSDA", current_function_funcdef_no
);
2287 sym
= gen_rtx_SYMBOL_REF (Pmode
, ggc_strdup (buf
));
2288 SYMBOL_REF_FLAGS (sym
) = SYMBOL_FLAG_LOCAL
;
2289 emit_move_insn (mem
, sym
);
2292 emit_move_insn (mem
, const0_rtx
);
2294 #ifdef DONT_USE_BUILTIN_SETJMP
2297 x
= emit_library_call_value (setjmp_libfunc
, NULL_RTX
, LCT_RETURNS_TWICE
,
2298 TYPE_MODE (integer_type_node
), 1,
2299 plus_constant (XEXP (fc
, 0),
2300 sjlj_fc_jbuf_ofs
), Pmode
);
2302 note
= emit_note (NOTE_INSN_EXPECTED_VALUE
);
2303 NOTE_EXPECTED_VALUE (note
) = gen_rtx_EQ (VOIDmode
, x
, const0_rtx
);
2305 emit_cmp_and_jump_insns (x
, const0_rtx
, NE
, 0,
2306 TYPE_MODE (integer_type_node
), 0, dispatch_label
);
2309 expand_builtin_setjmp_setup (plus_constant (XEXP (fc
, 0), sjlj_fc_jbuf_ofs
),
2313 emit_library_call (unwind_sjlj_register_libfunc
, LCT_NORMAL
, VOIDmode
,
2314 1, XEXP (fc
, 0), Pmode
);
2319 /* ??? Instead of doing this at the beginning of the function,
2320 do this in a block that is at loop level 0 and dominates all
2321 can_throw_internal instructions. */
2323 for (fn_begin
= get_insns (); ; fn_begin
= NEXT_INSN (fn_begin
))
2324 if (GET_CODE (fn_begin
) == NOTE
2325 && (NOTE_LINE_NUMBER (fn_begin
) == NOTE_INSN_FUNCTION_BEG
2326 || NOTE_LINE_NUMBER (fn_begin
) == NOTE_INSN_BASIC_BLOCK
))
2328 if (NOTE_LINE_NUMBER (fn_begin
) == NOTE_INSN_FUNCTION_BEG
)
2329 insert_insn_on_edge (seq
, ENTRY_BLOCK_PTR
->succ
);
2332 rtx last
= BB_END (ENTRY_BLOCK_PTR
->succ
->dest
);
2333 for (; ; fn_begin
= NEXT_INSN (fn_begin
))
2334 if ((GET_CODE (fn_begin
) == NOTE
2335 && NOTE_LINE_NUMBER (fn_begin
) == NOTE_INSN_FUNCTION_BEG
)
2336 || fn_begin
== last
)
2338 emit_insn_after (seq
, fn_begin
);
2342 /* Call back from expand_function_end to know where we should put
2343 the call to unwind_sjlj_unregister_libfunc if needed. */
2346 sjlj_emit_function_exit_after (rtx after
)
2348 cfun
->eh
->sjlj_exit_after
= after
;
2352 sjlj_emit_function_exit (void)
2359 emit_library_call (unwind_sjlj_unregister_libfunc
, LCT_NORMAL
, VOIDmode
,
2360 1, XEXP (cfun
->eh
->sjlj_fc
, 0), Pmode
);
2365 /* ??? Really this can be done in any block at loop level 0 that
2366 post-dominates all can_throw_internal instructions. This is
2367 the last possible moment. */
2369 for (e
= EXIT_BLOCK_PTR
->pred
; e
; e
= e
->pred_next
)
2370 if (e
->flags
& EDGE_FALLTHRU
)
2376 /* Figure out whether the place we are supposed to insert libcall
2377 is inside the last basic block or after it. In the other case
2378 we need to emit to edge. */
2379 if (e
->src
->next_bb
!= EXIT_BLOCK_PTR
)
2381 for (insn
= NEXT_INSN (BB_END (e
->src
)); insn
; insn
= NEXT_INSN (insn
))
2382 if (insn
== cfun
->eh
->sjlj_exit_after
)
2385 insert_insn_on_edge (seq
, e
);
2388 insn
= cfun
->eh
->sjlj_exit_after
;
2389 if (GET_CODE (insn
) == CODE_LABEL
)
2390 insn
= NEXT_INSN (insn
);
2391 emit_insn_after (seq
, insn
);
2397 sjlj_emit_dispatch_table (rtx dispatch_label
, struct sjlj_lp_info
*lp_info
)
2399 int i
, first_reachable
;
2400 rtx mem
, dispatch
, seq
, fc
;
2405 fc
= cfun
->eh
->sjlj_fc
;
2409 emit_label (dispatch_label
);
2411 #ifndef DONT_USE_BUILTIN_SETJMP
2412 expand_builtin_setjmp_receiver (dispatch_label
);
2415 /* Load up dispatch index, exc_ptr and filter values from the
2416 function context. */
2417 mem
= adjust_address (fc
, TYPE_MODE (integer_type_node
),
2418 sjlj_fc_call_site_ofs
);
2419 dispatch
= copy_to_reg (mem
);
2421 mem
= adjust_address (fc
, word_mode
, sjlj_fc_data_ofs
);
2422 if (word_mode
!= ptr_mode
)
2424 #ifdef POINTERS_EXTEND_UNSIGNED
2425 mem
= convert_memory_address (ptr_mode
, mem
);
2427 mem
= convert_to_mode (ptr_mode
, mem
, 0);
2430 emit_move_insn (cfun
->eh
->exc_ptr
, mem
);
2432 mem
= adjust_address (fc
, word_mode
, sjlj_fc_data_ofs
+ UNITS_PER_WORD
);
2433 emit_move_insn (cfun
->eh
->filter
, mem
);
2435 /* Jump to one of the directly reachable regions. */
2436 /* ??? This really ought to be using a switch statement. */
2438 first_reachable
= 0;
2439 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
2441 if (! lp_info
[i
].directly_reachable
)
2444 if (! first_reachable
)
2446 first_reachable
= i
;
2450 emit_cmp_and_jump_insns (dispatch
, GEN_INT (lp_info
[i
].dispatch_index
),
2451 EQ
, NULL_RTX
, TYPE_MODE (integer_type_node
), 0,
2452 cfun
->eh
->region_array
[i
]->post_landing_pad
);
2458 before
= cfun
->eh
->region_array
[first_reachable
]->post_landing_pad
;
2460 bb
= emit_to_new_bb_before (seq
, before
);
2461 e
= make_edge (bb
, bb
->next_bb
, EDGE_FALLTHRU
);
2462 e
->count
= bb
->count
;
2463 e
->probability
= REG_BR_PROB_BASE
;
2467 sjlj_build_landing_pads (void)
2469 struct sjlj_lp_info
*lp_info
;
2471 lp_info
= xcalloc (cfun
->eh
->last_region_number
+ 1,
2472 sizeof (struct sjlj_lp_info
));
2474 if (sjlj_find_directly_reachable_regions (lp_info
))
2476 rtx dispatch_label
= gen_label_rtx ();
2479 = assign_stack_local (TYPE_MODE (sjlj_fc_type_node
),
2480 int_size_in_bytes (sjlj_fc_type_node
),
2481 TYPE_ALIGN (sjlj_fc_type_node
));
2483 sjlj_assign_call_site_values (dispatch_label
, lp_info
);
2484 sjlj_mark_call_sites (lp_info
);
2486 sjlj_emit_function_enter (dispatch_label
);
2487 sjlj_emit_dispatch_table (dispatch_label
, lp_info
);
2488 sjlj_emit_function_exit ();
2495 finish_eh_generation (void)
2499 /* Nothing to do if no regions created. */
2500 if (cfun
->eh
->region_tree
== NULL
)
2503 /* The object here is to provide find_basic_blocks with detailed
2504 information (via reachable_handlers) on how exception control
2505 flows within the function. In this first pass, we can include
2506 type information garnered from ERT_THROW and ERT_ALLOWED_EXCEPTIONS
2507 regions, and hope that it will be useful in deleting unreachable
2508 handlers. Subsequently, we will generate landing pads which will
2509 connect many of the handlers, and then type information will not
2510 be effective. Still, this is a win over previous implementations. */
2512 cleanup_cfg (CLEANUP_PRE_LOOP
| CLEANUP_NO_INSN_DEL
);
2514 /* These registers are used by the landing pads. Make sure they
2515 have been generated. */
2516 get_exception_pointer (cfun
);
2517 get_exception_filter (cfun
);
2519 /* Construct the landing pads. */
2521 assign_filter_values ();
2522 build_post_landing_pads ();
2523 connect_post_landing_pads ();
2524 if (USING_SJLJ_EXCEPTIONS
)
2525 sjlj_build_landing_pads ();
2527 dw2_build_landing_pads ();
2529 cfun
->eh
->built_landing_pads
= 1;
2531 /* We've totally changed the CFG. Start over. */
2532 find_exception_handler_labels ();
2533 break_superblocks ();
2534 if (USING_SJLJ_EXCEPTIONS
)
2535 commit_edge_insertions ();
2540 for (e
= bb
->succ
; e
; e
= next
)
2542 next
= e
->succ_next
;
2543 if (e
->flags
& EDGE_EH
)
2550 rtl_make_eh_edge (NULL
, bb
, BB_END (bb
));
2552 cleanup_cfg (CLEANUP_PRE_LOOP
| CLEANUP_NO_INSN_DEL
);
2556 ehl_hash (const void *pentry
)
2558 struct ehl_map_entry
*entry
= (struct ehl_map_entry
*) pentry
;
2560 /* 2^32 * ((sqrt(5) - 1) / 2) */
2561 const hashval_t scaled_golden_ratio
= 0x9e3779b9;
2562 return CODE_LABEL_NUMBER (entry
->label
) * scaled_golden_ratio
;
2566 ehl_eq (const void *pentry
, const void *pdata
)
2568 struct ehl_map_entry
*entry
= (struct ehl_map_entry
*) pentry
;
2569 struct ehl_map_entry
*data
= (struct ehl_map_entry
*) pdata
;
2571 return entry
->label
== data
->label
;
2574 /* This section handles removing dead code for flow. */
2576 /* Remove LABEL from exception_handler_label_map. */
2579 remove_exception_handler_label (rtx label
)
2581 struct ehl_map_entry
**slot
, tmp
;
2583 /* If exception_handler_label_map was not built yet,
2584 there is nothing to do. */
2585 if (cfun
->eh
->exception_handler_label_map
== NULL
)
2589 slot
= (struct ehl_map_entry
**)
2590 htab_find_slot (cfun
->eh
->exception_handler_label_map
, &tmp
, NO_INSERT
);
2594 htab_clear_slot (cfun
->eh
->exception_handler_label_map
, (void **) slot
);
2597 /* Splice REGION from the region tree etc. */
2600 remove_eh_handler (struct eh_region
*region
)
2602 struct eh_region
**pp
, **pp_start
, *p
, *outer
, *inner
;
2605 /* For the benefit of efficiently handling REG_EH_REGION notes,
2606 replace this region in the region array with its containing
2607 region. Note that previous region deletions may result in
2608 multiple copies of this region in the array, so we have a
2609 list of alternate numbers by which we are known. */
2611 outer
= region
->outer
;
2612 cfun
->eh
->region_array
[region
->region_number
] = outer
;
2616 EXECUTE_IF_SET_IN_BITMAP (region
->aka
, 0, i
,
2617 { cfun
->eh
->region_array
[i
] = outer
; });
2623 outer
->aka
= BITMAP_GGC_ALLOC ();
2625 bitmap_a_or_b (outer
->aka
, outer
->aka
, region
->aka
);
2626 bitmap_set_bit (outer
->aka
, region
->region_number
);
2629 if (cfun
->eh
->built_landing_pads
)
2630 lab
= region
->landing_pad
;
2632 lab
= region
->label
;
2634 remove_exception_handler_label (lab
);
2637 pp_start
= &outer
->inner
;
2639 pp_start
= &cfun
->eh
->region_tree
;
2640 for (pp
= pp_start
, p
= *pp
; p
!= region
; pp
= &p
->next_peer
, p
= *pp
)
2642 *pp
= region
->next_peer
;
2644 inner
= region
->inner
;
2647 for (p
= inner
; p
->next_peer
; p
= p
->next_peer
)
2651 p
->next_peer
= *pp_start
;
2655 if (region
->type
== ERT_CATCH
)
2657 struct eh_region
*try, *next
, *prev
;
2659 for (try = region
->next_peer
;
2660 try->type
== ERT_CATCH
;
2661 try = try->next_peer
)
2663 if (try->type
!= ERT_TRY
)
2666 next
= region
->u
.catch.next_catch
;
2667 prev
= region
->u
.catch.prev_catch
;
2670 next
->u
.catch.prev_catch
= prev
;
2672 try->u
.try.last_catch
= prev
;
2674 prev
->u
.catch.next_catch
= next
;
2677 try->u
.try.catch = next
;
2679 remove_eh_handler (try);
2684 /* LABEL heads a basic block that is about to be deleted. If this
2685 label corresponds to an exception region, we may be able to
2686 delete the region. */
2689 maybe_remove_eh_handler (rtx label
)
2691 struct ehl_map_entry
**slot
, tmp
;
2692 struct eh_region
*region
;
2694 /* ??? After generating landing pads, it's not so simple to determine
2695 if the region data is completely unused. One must examine the
2696 landing pad and the post landing pad, and whether an inner try block
2697 is referencing the catch handlers directly. */
2698 if (cfun
->eh
->built_landing_pads
)
2702 slot
= (struct ehl_map_entry
**)
2703 htab_find_slot (cfun
->eh
->exception_handler_label_map
, &tmp
, NO_INSERT
);
2706 region
= (*slot
)->region
;
2710 /* Flow will want to remove MUST_NOT_THROW regions as unreachable
2711 because there is no path to the fallback call to terminate.
2712 But the region continues to affect call-site data until there
2713 are no more contained calls, which we don't see here. */
2714 if (region
->type
== ERT_MUST_NOT_THROW
)
2716 htab_clear_slot (cfun
->eh
->exception_handler_label_map
, (void **) slot
);
2717 region
->label
= NULL_RTX
;
2720 remove_eh_handler (region
);
2723 /* Invokes CALLBACK for every exception handler label. Only used by old
2724 loop hackery; should not be used by new code. */
2727 for_each_eh_label (void (*callback
) (rtx
))
2729 htab_traverse (cfun
->eh
->exception_handler_label_map
, for_each_eh_label_1
,
2730 (void *) &callback
);
2734 for_each_eh_label_1 (void **pentry
, void *data
)
2736 struct ehl_map_entry
*entry
= *(struct ehl_map_entry
**)pentry
;
2737 void (*callback
) (rtx
) = *(void (**) (rtx
)) data
;
2739 (*callback
) (entry
->label
);
2743 /* Invoke CALLBACK for every exception region in the current function. */
2746 for_each_eh_region (void (*callback
) (struct eh_region
*))
2748 int i
, n
= cfun
->eh
->last_region_number
;
2749 for (i
= 1; i
<= n
; ++i
)
2751 struct eh_region
*region
= cfun
->eh
->region_array
[i
];
2753 (*callback
) (region
);
2757 /* This section describes CFG exception edges for flow. */
2759 /* For communicating between calls to reachable_next_level. */
2760 struct reachable_info
2764 void (*callback
) (struct eh_region
*, void *);
2765 void *callback_data
;
2766 bool saw_any_handlers
;
2769 /* A subroutine of reachable_next_level. Return true if TYPE, or a
2770 base class of TYPE, is in HANDLED. */
2773 check_handled (tree handled
, tree type
)
2777 /* We can check for exact matches without front-end help. */
2778 if (! lang_eh_type_covers
)
2780 for (t
= handled
; t
; t
= TREE_CHAIN (t
))
2781 if (TREE_VALUE (t
) == type
)
2786 for (t
= handled
; t
; t
= TREE_CHAIN (t
))
2787 if ((*lang_eh_type_covers
) (TREE_VALUE (t
), type
))
2794 /* A subroutine of reachable_next_level. If we are collecting a list
2795 of handlers, add one. After landing pad generation, reference
2796 it instead of the handlers themselves. Further, the handlers are
2797 all wired together, so by referencing one, we've got them all.
2798 Before landing pad generation we reference each handler individually.
2800 LP_REGION contains the landing pad; REGION is the handler. */
2803 add_reachable_handler (struct reachable_info
*info
,
2804 struct eh_region
*lp_region
, struct eh_region
*region
)
2809 info
->saw_any_handlers
= true;
2811 if (cfun
->eh
->built_landing_pads
)
2812 info
->callback (lp_region
, info
->callback_data
);
2814 info
->callback (region
, info
->callback_data
);
2817 /* Process one level of exception regions for reachability.
2818 If TYPE_THROWN is non-null, then it is the *exact* type being
2819 propagated. If INFO is non-null, then collect handler labels
2820 and caught/allowed type information between invocations. */
2822 static enum reachable_code
2823 reachable_next_level (struct eh_region
*region
, tree type_thrown
,
2824 struct reachable_info
*info
)
2826 switch (region
->type
)
2829 /* Before landing-pad generation, we model control flow
2830 directly to the individual handlers. In this way we can
2831 see that catch handler types may shadow one another. */
2832 add_reachable_handler (info
, region
, region
);
2833 return RNL_MAYBE_CAUGHT
;
2837 struct eh_region
*c
;
2838 enum reachable_code ret
= RNL_NOT_CAUGHT
;
2840 for (c
= region
->u
.try.catch; c
; c
= c
->u
.catch.next_catch
)
2842 /* A catch-all handler ends the search. */
2843 if (c
->u
.catch.type_list
== NULL
)
2845 add_reachable_handler (info
, region
, c
);
2851 /* If we have at least one type match, end the search. */
2852 tree tp_node
= c
->u
.catch.type_list
;
2854 for (; tp_node
; tp_node
= TREE_CHAIN (tp_node
))
2856 tree type
= TREE_VALUE (tp_node
);
2858 if (type
== type_thrown
2859 || (lang_eh_type_covers
2860 && (*lang_eh_type_covers
) (type
, type_thrown
)))
2862 add_reachable_handler (info
, region
, c
);
2867 /* If we have definitive information of a match failure,
2868 the catch won't trigger. */
2869 if (lang_eh_type_covers
)
2870 return RNL_NOT_CAUGHT
;
2873 /* At this point, we either don't know what type is thrown or
2874 don't have front-end assistance to help deciding if it is
2875 covered by one of the types in the list for this region.
2877 We'd then like to add this region to the list of reachable
2878 handlers since it is indeed potentially reachable based on the
2879 information we have.
2881 Actually, this handler is for sure not reachable if all the
2882 types it matches have already been caught. That is, it is only
2883 potentially reachable if at least one of the types it catches
2884 has not been previously caught. */
2887 ret
= RNL_MAYBE_CAUGHT
;
2890 tree tp_node
= c
->u
.catch.type_list
;
2891 bool maybe_reachable
= false;
2893 /* Compute the potential reachability of this handler and
2894 update the list of types caught at the same time. */
2895 for (; tp_node
; tp_node
= TREE_CHAIN (tp_node
))
2897 tree type
= TREE_VALUE (tp_node
);
2899 if (! check_handled (info
->types_caught
, type
))
2902 = tree_cons (NULL
, type
, info
->types_caught
);
2904 maybe_reachable
= true;
2908 if (maybe_reachable
)
2910 add_reachable_handler (info
, region
, c
);
2912 /* ??? If the catch type is a base class of every allowed
2913 type, then we know we can stop the search. */
2914 ret
= RNL_MAYBE_CAUGHT
;
2922 case ERT_ALLOWED_EXCEPTIONS
:
2923 /* An empty list of types definitely ends the search. */
2924 if (region
->u
.allowed
.type_list
== NULL_TREE
)
2926 add_reachable_handler (info
, region
, region
);
2930 /* Collect a list of lists of allowed types for use in detecting
2931 when a catch may be transformed into a catch-all. */
2933 info
->types_allowed
= tree_cons (NULL_TREE
,
2934 region
->u
.allowed
.type_list
,
2935 info
->types_allowed
);
2937 /* If we have definitive information about the type hierarchy,
2938 then we can tell if the thrown type will pass through the
2940 if (type_thrown
&& lang_eh_type_covers
)
2942 if (check_handled (region
->u
.allowed
.type_list
, type_thrown
))
2943 return RNL_NOT_CAUGHT
;
2946 add_reachable_handler (info
, region
, region
);
2951 add_reachable_handler (info
, region
, region
);
2952 return RNL_MAYBE_CAUGHT
;
2955 /* Catch regions are handled by their controlling try region. */
2956 return RNL_NOT_CAUGHT
;
2958 case ERT_MUST_NOT_THROW
:
2959 /* Here we end our search, since no exceptions may propagate.
2960 If we've touched down at some landing pad previous, then the
2961 explicit function call we generated may be used. Otherwise
2962 the call is made by the runtime. */
2963 if (info
&& info
->saw_any_handlers
)
2965 add_reachable_handler (info
, region
, region
);
2974 /* Shouldn't see these here. */
2981 /* Invoke CALLBACK on each region reachable from REGION_NUMBER. */
2984 foreach_reachable_handler (int region_number
, bool is_resx
,
2985 void (*callback
) (struct eh_region
*, void *),
2986 void *callback_data
)
2988 struct reachable_info info
;
2989 struct eh_region
*region
;
2992 memset (&info
, 0, sizeof (info
));
2993 info
.callback
= callback
;
2994 info
.callback_data
= callback_data
;
2996 region
= cfun
->eh
->region_array
[region_number
];
2998 type_thrown
= NULL_TREE
;
3001 /* A RESX leaves a region instead of entering it. Thus the
3002 region itself may have been deleted out from under us. */
3005 region
= region
->outer
;
3007 else if (region
->type
== ERT_THROW
)
3009 type_thrown
= region
->u
.throw.type
;
3010 region
= region
->outer
;
3015 if (reachable_next_level (region
, type_thrown
, &info
) >= RNL_CAUGHT
)
3017 /* If we have processed one cleanup, there is no point in
3018 processing any more of them. Each cleanup will have an edge
3019 to the next outer cleanup region, so the flow graph will be
3021 if (region
->type
== ERT_CLEANUP
)
3022 region
= region
->u
.cleanup
.prev_try
;
3024 region
= region
->outer
;
3028 /* Retrieve a list of labels of exception handlers which can be
3029 reached by a given insn. */
3032 arh_to_landing_pad (struct eh_region
*region
, void *data
)
3034 rtx
*p_handlers
= data
;
3036 *p_handlers
= alloc_INSN_LIST (region
->landing_pad
, NULL_RTX
);
3040 arh_to_label (struct eh_region
*region
, void *data
)
3042 rtx
*p_handlers
= data
;
3043 *p_handlers
= alloc_INSN_LIST (region
->label
, *p_handlers
);
3047 reachable_handlers (rtx insn
)
3049 bool is_resx
= false;
3050 rtx handlers
= NULL
;
3053 if (GET_CODE (insn
) == JUMP_INSN
3054 && GET_CODE (PATTERN (insn
)) == RESX
)
3056 region_number
= XINT (PATTERN (insn
), 0);
3061 rtx note
= find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
);
3062 if (!note
|| INTVAL (XEXP (note
, 0)) <= 0)
3064 region_number
= INTVAL (XEXP (note
, 0));
3067 foreach_reachable_handler (region_number
, is_resx
,
3068 (cfun
->eh
->built_landing_pads
3069 ? arh_to_landing_pad
3076 /* Determine if the given INSN can throw an exception that is caught
3077 within the function. */
3080 can_throw_internal_1 (int region_number
)
3082 struct eh_region
*region
;
3085 region
= cfun
->eh
->region_array
[region_number
];
3087 type_thrown
= NULL_TREE
;
3088 if (region
->type
== ERT_THROW
)
3090 type_thrown
= region
->u
.throw.type
;
3091 region
= region
->outer
;
3094 /* If this exception is ignored by each and every containing region,
3095 then control passes straight out. The runtime may handle some
3096 regions, which also do not require processing internally. */
3097 for (; region
; region
= region
->outer
)
3099 enum reachable_code how
= reachable_next_level (region
, type_thrown
, 0);
3100 if (how
== RNL_BLOCKED
)
3102 if (how
!= RNL_NOT_CAUGHT
)
3110 can_throw_internal (rtx insn
)
3114 if (! INSN_P (insn
))
3117 if (GET_CODE (insn
) == JUMP_INSN
3118 && GET_CODE (PATTERN (insn
)) == RESX
3119 && XINT (PATTERN (insn
), 0) > 0)
3120 return can_throw_internal_1 (XINT (PATTERN (insn
), 0));
3122 if (GET_CODE (insn
) == INSN
3123 && GET_CODE (PATTERN (insn
)) == SEQUENCE
)
3124 insn
= XVECEXP (PATTERN (insn
), 0, 0);
3126 /* Every insn that might throw has an EH_REGION note. */
3127 note
= find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
);
3128 if (!note
|| INTVAL (XEXP (note
, 0)) <= 0)
3131 return can_throw_internal_1 (INTVAL (XEXP (note
, 0)));
3134 /* Determine if the given INSN can throw an exception that is
3135 visible outside the function. */
3138 can_throw_external_1 (int region_number
)
3140 struct eh_region
*region
;
3143 region
= cfun
->eh
->region_array
[region_number
];
3145 type_thrown
= NULL_TREE
;
3146 if (region
->type
== ERT_THROW
)
3148 type_thrown
= region
->u
.throw.type
;
3149 region
= region
->outer
;
3152 /* If the exception is caught or blocked by any containing region,
3153 then it is not seen by any calling function. */
3154 for (; region
; region
= region
->outer
)
3155 if (reachable_next_level (region
, type_thrown
, NULL
) >= RNL_CAUGHT
)
3162 can_throw_external (rtx insn
)
3166 if (! INSN_P (insn
))
3169 if (GET_CODE (insn
) == INSN
3170 && GET_CODE (PATTERN (insn
)) == SEQUENCE
)
3171 insn
= XVECEXP (PATTERN (insn
), 0, 0);
3173 note
= find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
);
3176 /* Calls (and trapping insns) without notes are outside any
3177 exception handling region in this function. We have to
3178 assume it might throw. Given that the front end and middle
3179 ends mark known NOTHROW functions, this isn't so wildly
3181 return (GET_CODE (insn
) == CALL_INSN
3182 || (flag_non_call_exceptions
3183 && may_trap_p (PATTERN (insn
))));
3185 if (INTVAL (XEXP (note
, 0)) <= 0)
3188 return can_throw_external_1 (INTVAL (XEXP (note
, 0)));
3191 /* Set current_function_nothrow and cfun->all_throwers_are_sibcalls. */
3194 set_nothrow_function_flags (void)
3198 current_function_nothrow
= 1;
3200 /* Assume cfun->all_throwers_are_sibcalls until we encounter
3201 something that can throw an exception. We specifically exempt
3202 CALL_INSNs that are SIBLING_CALL_P, as these are really jumps,
3203 and can't throw. Most CALL_INSNs are not SIBLING_CALL_P, so this
3206 cfun
->all_throwers_are_sibcalls
= 1;
3208 if (! flag_exceptions
)
3211 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
3212 if (can_throw_external (insn
))
3214 current_function_nothrow
= 0;
3216 if (GET_CODE (insn
) != CALL_INSN
|| !SIBLING_CALL_P (insn
))
3218 cfun
->all_throwers_are_sibcalls
= 0;
3223 for (insn
= current_function_epilogue_delay_list
; insn
;
3224 insn
= XEXP (insn
, 1))
3225 if (can_throw_external (insn
))
3227 current_function_nothrow
= 0;
3229 if (GET_CODE (insn
) != CALL_INSN
|| !SIBLING_CALL_P (insn
))
3231 cfun
->all_throwers_are_sibcalls
= 0;
3238 /* Various hooks for unwind library. */
3240 /* Do any necessary initialization to access arbitrary stack frames.
3241 On the SPARC, this means flushing the register windows. */
3244 expand_builtin_unwind_init (void)
3246 /* Set this so all the registers get saved in our frame; we need to be
3247 able to copy the saved values for any registers from frames we unwind. */
3248 current_function_has_nonlocal_label
= 1;
3250 #ifdef SETUP_FRAME_ADDRESSES
3251 SETUP_FRAME_ADDRESSES ();
3256 expand_builtin_eh_return_data_regno (tree arglist
)
3258 tree which
= TREE_VALUE (arglist
);
3259 unsigned HOST_WIDE_INT iwhich
;
3261 if (TREE_CODE (which
) != INTEGER_CST
)
3263 error ("argument of `__builtin_eh_return_regno' must be constant");
3267 iwhich
= tree_low_cst (which
, 1);
3268 iwhich
= EH_RETURN_DATA_REGNO (iwhich
);
3269 if (iwhich
== INVALID_REGNUM
)
3272 #ifdef DWARF_FRAME_REGNUM
3273 iwhich
= DWARF_FRAME_REGNUM (iwhich
);
3275 iwhich
= DBX_REGISTER_NUMBER (iwhich
);
3278 return GEN_INT (iwhich
);
3281 /* Given a value extracted from the return address register or stack slot,
3282 return the actual address encoded in that value. */
3285 expand_builtin_extract_return_addr (tree addr_tree
)
3287 rtx addr
= expand_expr (addr_tree
, NULL_RTX
, Pmode
, 0);
3289 if (GET_MODE (addr
) != Pmode
3290 && GET_MODE (addr
) != VOIDmode
)
3292 #ifdef POINTERS_EXTEND_UNSIGNED
3293 addr
= convert_memory_address (Pmode
, addr
);
3295 addr
= convert_to_mode (Pmode
, addr
, 0);
3299 /* First mask out any unwanted bits. */
3300 #ifdef MASK_RETURN_ADDR
3301 expand_and (Pmode
, addr
, MASK_RETURN_ADDR
, addr
);
3304 /* Then adjust to find the real return address. */
3305 #if defined (RETURN_ADDR_OFFSET)
3306 addr
= plus_constant (addr
, RETURN_ADDR_OFFSET
);
3312 /* Given an actual address in addr_tree, do any necessary encoding
3313 and return the value to be stored in the return address register or
3314 stack slot so the epilogue will return to that address. */
3317 expand_builtin_frob_return_addr (tree addr_tree
)
3319 rtx addr
= expand_expr (addr_tree
, NULL_RTX
, ptr_mode
, 0);
3321 addr
= convert_memory_address (Pmode
, addr
);
3323 #ifdef RETURN_ADDR_OFFSET
3324 addr
= force_reg (Pmode
, addr
);
3325 addr
= plus_constant (addr
, -RETURN_ADDR_OFFSET
);
3331 /* Set up the epilogue with the magic bits we'll need to return to the
3332 exception handler. */
3335 expand_builtin_eh_return (tree stackadj_tree ATTRIBUTE_UNUSED
,
3340 #ifdef EH_RETURN_STACKADJ_RTX
3341 tmp
= expand_expr (stackadj_tree
, cfun
->eh
->ehr_stackadj
, VOIDmode
, 0);
3342 tmp
= convert_memory_address (Pmode
, tmp
);
3343 if (!cfun
->eh
->ehr_stackadj
)
3344 cfun
->eh
->ehr_stackadj
= copy_to_reg (tmp
);
3345 else if (tmp
!= cfun
->eh
->ehr_stackadj
)
3346 emit_move_insn (cfun
->eh
->ehr_stackadj
, tmp
);
3349 tmp
= expand_expr (handler_tree
, cfun
->eh
->ehr_handler
, VOIDmode
, 0);
3350 tmp
= convert_memory_address (Pmode
, tmp
);
3351 if (!cfun
->eh
->ehr_handler
)
3352 cfun
->eh
->ehr_handler
= copy_to_reg (tmp
);
3353 else if (tmp
!= cfun
->eh
->ehr_handler
)
3354 emit_move_insn (cfun
->eh
->ehr_handler
, tmp
);
3356 if (!cfun
->eh
->ehr_label
)
3357 cfun
->eh
->ehr_label
= gen_label_rtx ();
3358 emit_jump (cfun
->eh
->ehr_label
);
3362 expand_eh_return (void)
3366 if (! cfun
->eh
->ehr_label
)
3369 current_function_calls_eh_return
= 1;
3371 #ifdef EH_RETURN_STACKADJ_RTX
3372 emit_move_insn (EH_RETURN_STACKADJ_RTX
, const0_rtx
);
3375 around_label
= gen_label_rtx ();
3376 emit_jump (around_label
);
3378 emit_label (cfun
->eh
->ehr_label
);
3379 clobber_return_register ();
3381 #ifdef EH_RETURN_STACKADJ_RTX
3382 emit_move_insn (EH_RETURN_STACKADJ_RTX
, cfun
->eh
->ehr_stackadj
);
3385 #ifdef HAVE_eh_return
3387 emit_insn (gen_eh_return (cfun
->eh
->ehr_handler
));
3391 #ifdef EH_RETURN_HANDLER_RTX
3392 emit_move_insn (EH_RETURN_HANDLER_RTX
, cfun
->eh
->ehr_handler
);
3394 error ("__builtin_eh_return not supported on this target");
3398 emit_label (around_label
);
3401 /* Convert a ptr_mode address ADDR_TREE to a Pmode address controlled by
3402 POINTERS_EXTEND_UNSIGNED and return it. */
3405 expand_builtin_extend_pointer (tree addr_tree
)
3407 rtx addr
= expand_expr (addr_tree
, NULL_RTX
, ptr_mode
, 0);
3410 #ifdef POINTERS_EXTEND_UNSIGNED
3411 extend
= POINTERS_EXTEND_UNSIGNED
;
3413 /* The previous EH code did an unsigned extend by default, so we do this also
3418 return convert_modes (word_mode
, ptr_mode
, addr
, extend
);
3421 /* In the following functions, we represent entries in the action table
3422 as 1-based indices. Special cases are:
3424 0: null action record, non-null landing pad; implies cleanups
3425 -1: null action record, null landing pad; implies no action
3426 -2: no call-site entry; implies must_not_throw
3427 -3: we have yet to process outer regions
3429 Further, no special cases apply to the "next" field of the record.
3430 For next, 0 means end of list. */
3432 struct action_record
3440 action_record_eq (const void *pentry
, const void *pdata
)
3442 const struct action_record
*entry
= (const struct action_record
*) pentry
;
3443 const struct action_record
*data
= (const struct action_record
*) pdata
;
3444 return entry
->filter
== data
->filter
&& entry
->next
== data
->next
;
3448 action_record_hash (const void *pentry
)
3450 const struct action_record
*entry
= (const struct action_record
*) pentry
;
3451 return entry
->next
* 1009 + entry
->filter
;
3455 add_action_record (htab_t ar_hash
, int filter
, int next
)
3457 struct action_record
**slot
, *new, tmp
;
3459 tmp
.filter
= filter
;
3461 slot
= (struct action_record
**) htab_find_slot (ar_hash
, &tmp
, INSERT
);
3463 if ((new = *slot
) == NULL
)
3465 new = xmalloc (sizeof (*new));
3466 new->offset
= VARRAY_ACTIVE_SIZE (cfun
->eh
->action_record_data
) + 1;
3467 new->filter
= filter
;
3471 /* The filter value goes in untouched. The link to the next
3472 record is a "self-relative" byte offset, or zero to indicate
3473 that there is no next record. So convert the absolute 1 based
3474 indices we've been carrying around into a displacement. */
3476 push_sleb128 (&cfun
->eh
->action_record_data
, filter
);
3478 next
-= VARRAY_ACTIVE_SIZE (cfun
->eh
->action_record_data
) + 1;
3479 push_sleb128 (&cfun
->eh
->action_record_data
, next
);
3486 collect_one_action_chain (htab_t ar_hash
, struct eh_region
*region
)
3488 struct eh_region
*c
;
3491 /* If we've reached the top of the region chain, then we have
3492 no actions, and require no landing pad. */
3496 switch (region
->type
)
3499 /* A cleanup adds a zero filter to the beginning of the chain, but
3500 there are special cases to look out for. If there are *only*
3501 cleanups along a path, then it compresses to a zero action.
3502 Further, if there are multiple cleanups along a path, we only
3503 need to represent one of them, as that is enough to trigger
3504 entry to the landing pad at runtime. */
3505 next
= collect_one_action_chain (ar_hash
, region
->outer
);
3508 for (c
= region
->outer
; c
; c
= c
->outer
)
3509 if (c
->type
== ERT_CLEANUP
)
3511 return add_action_record (ar_hash
, 0, next
);
3514 /* Process the associated catch regions in reverse order.
3515 If there's a catch-all handler, then we don't need to
3516 search outer regions. Use a magic -3 value to record
3517 that we haven't done the outer search. */
3519 for (c
= region
->u
.try.last_catch
; c
; c
= c
->u
.catch.prev_catch
)
3521 if (c
->u
.catch.type_list
== NULL
)
3523 /* Retrieve the filter from the head of the filter list
3524 where we have stored it (see assign_filter_values). */
3526 = TREE_INT_CST_LOW (TREE_VALUE (c
->u
.catch.filter_list
));
3528 next
= add_action_record (ar_hash
, filter
, 0);
3532 /* Once the outer search is done, trigger an action record for
3533 each filter we have. */
3538 next
= collect_one_action_chain (ar_hash
, region
->outer
);
3540 /* If there is no next action, terminate the chain. */
3543 /* If all outer actions are cleanups or must_not_throw,
3544 we'll have no action record for it, since we had wanted
3545 to encode these states in the call-site record directly.
3546 Add a cleanup action to the chain to catch these. */
3548 next
= add_action_record (ar_hash
, 0, 0);
3551 flt_node
= c
->u
.catch.filter_list
;
3552 for (; flt_node
; flt_node
= TREE_CHAIN (flt_node
))
3554 int filter
= TREE_INT_CST_LOW (TREE_VALUE (flt_node
));
3555 next
= add_action_record (ar_hash
, filter
, next
);
3561 case ERT_ALLOWED_EXCEPTIONS
:
3562 /* An exception specification adds its filter to the
3563 beginning of the chain. */
3564 next
= collect_one_action_chain (ar_hash
, region
->outer
);
3566 /* If there is no next action, terminate the chain. */
3569 /* If all outer actions are cleanups or must_not_throw,
3570 we'll have no action record for it, since we had wanted
3571 to encode these states in the call-site record directly.
3572 Add a cleanup action to the chain to catch these. */
3574 next
= add_action_record (ar_hash
, 0, 0);
3576 return add_action_record (ar_hash
, region
->u
.allowed
.filter
, next
);
3578 case ERT_MUST_NOT_THROW
:
3579 /* A must-not-throw region with no inner handlers or cleanups
3580 requires no call-site entry. Note that this differs from
3581 the no handler or cleanup case in that we do require an lsda
3582 to be generated. Return a magic -2 value to record this. */
3587 /* CATCH regions are handled in TRY above. THROW regions are
3588 for optimization information only and produce no output. */
3589 return collect_one_action_chain (ar_hash
, region
->outer
);
3597 add_call_site (rtx landing_pad
, int action
)
3599 struct call_site_record
*data
= cfun
->eh
->call_site_data
;
3600 int used
= cfun
->eh
->call_site_data_used
;
3601 int size
= cfun
->eh
->call_site_data_size
;
3605 size
= (size
? size
* 2 : 64);
3606 data
= ggc_realloc (data
, sizeof (*data
) * size
);
3607 cfun
->eh
->call_site_data
= data
;
3608 cfun
->eh
->call_site_data_size
= size
;
3611 data
[used
].landing_pad
= landing_pad
;
3612 data
[used
].action
= action
;
3614 cfun
->eh
->call_site_data_used
= used
+ 1;
3616 return used
+ call_site_base
;
3619 /* Turn REG_EH_REGION notes back into NOTE_INSN_EH_REGION notes.
3620 The new note numbers will not refer to region numbers, but
3621 instead to call site entries. */
3624 convert_to_eh_region_ranges (void)
3626 rtx insn
, iter
, note
;
3628 int last_action
= -3;
3629 rtx last_action_insn
= NULL_RTX
;
3630 rtx last_landing_pad
= NULL_RTX
;
3631 rtx first_no_action_insn
= NULL_RTX
;
3634 if (USING_SJLJ_EXCEPTIONS
|| cfun
->eh
->region_tree
== NULL
)
3637 VARRAY_UCHAR_INIT (cfun
->eh
->action_record_data
, 64, "action_record_data");
3639 ar_hash
= htab_create (31, action_record_hash
, action_record_eq
, free
);
3641 for (iter
= get_insns (); iter
; iter
= NEXT_INSN (iter
))
3644 struct eh_region
*region
;
3646 rtx this_landing_pad
;
3649 if (GET_CODE (insn
) == INSN
3650 && GET_CODE (PATTERN (insn
)) == SEQUENCE
)
3651 insn
= XVECEXP (PATTERN (insn
), 0, 0);
3653 note
= find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
);
3656 if (! (GET_CODE (insn
) == CALL_INSN
3657 || (flag_non_call_exceptions
3658 && may_trap_p (PATTERN (insn
)))))
3665 if (INTVAL (XEXP (note
, 0)) <= 0)
3667 region
= cfun
->eh
->region_array
[INTVAL (XEXP (note
, 0))];
3668 this_action
= collect_one_action_chain (ar_hash
, region
);
3671 /* Existence of catch handlers, or must-not-throw regions
3672 implies that an lsda is needed (even if empty). */
3673 if (this_action
!= -1)
3674 cfun
->uses_eh_lsda
= 1;
3676 /* Delay creation of region notes for no-action regions
3677 until we're sure that an lsda will be required. */
3678 else if (last_action
== -3)
3680 first_no_action_insn
= iter
;
3684 /* Cleanups and handlers may share action chains but not
3685 landing pads. Collect the landing pad for this region. */
3686 if (this_action
>= 0)
3688 struct eh_region
*o
;
3689 for (o
= region
; ! o
->landing_pad
; o
= o
->outer
)
3691 this_landing_pad
= o
->landing_pad
;
3694 this_landing_pad
= NULL_RTX
;
3696 /* Differing actions or landing pads implies a change in call-site
3697 info, which implies some EH_REGION note should be emitted. */
3698 if (last_action
!= this_action
3699 || last_landing_pad
!= this_landing_pad
)
3701 /* If we'd not seen a previous action (-3) or the previous
3702 action was must-not-throw (-2), then we do not need an
3704 if (last_action
>= -1)
3706 /* If we delayed the creation of the begin, do it now. */
3707 if (first_no_action_insn
)
3709 call_site
= add_call_site (NULL_RTX
, 0);
3710 note
= emit_note_before (NOTE_INSN_EH_REGION_BEG
,
3711 first_no_action_insn
);
3712 NOTE_EH_HANDLER (note
) = call_site
;
3713 first_no_action_insn
= NULL_RTX
;
3716 note
= emit_note_after (NOTE_INSN_EH_REGION_END
,
3718 NOTE_EH_HANDLER (note
) = call_site
;
3721 /* If the new action is must-not-throw, then no region notes
3723 if (this_action
>= -1)
3725 call_site
= add_call_site (this_landing_pad
,
3726 this_action
< 0 ? 0 : this_action
);
3727 note
= emit_note_before (NOTE_INSN_EH_REGION_BEG
, iter
);
3728 NOTE_EH_HANDLER (note
) = call_site
;
3731 last_action
= this_action
;
3732 last_landing_pad
= this_landing_pad
;
3734 last_action_insn
= iter
;
3737 if (last_action
>= -1 && ! first_no_action_insn
)
3739 note
= emit_note_after (NOTE_INSN_EH_REGION_END
, last_action_insn
);
3740 NOTE_EH_HANDLER (note
) = call_site
;
3743 htab_delete (ar_hash
);
3748 push_uleb128 (varray_type
*data_area
, unsigned int value
)
3752 unsigned char byte
= value
& 0x7f;
3756 VARRAY_PUSH_UCHAR (*data_area
, byte
);
3762 push_sleb128 (varray_type
*data_area
, int value
)
3769 byte
= value
& 0x7f;
3771 more
= ! ((value
== 0 && (byte
& 0x40) == 0)
3772 || (value
== -1 && (byte
& 0x40) != 0));
3775 VARRAY_PUSH_UCHAR (*data_area
, byte
);
3781 #ifndef HAVE_AS_LEB128
3783 dw2_size_of_call_site_table (void)
3785 int n
= cfun
->eh
->call_site_data_used
;
3786 int size
= n
* (4 + 4 + 4);
3789 for (i
= 0; i
< n
; ++i
)
3791 struct call_site_record
*cs
= &cfun
->eh
->call_site_data
[i
];
3792 size
+= size_of_uleb128 (cs
->action
);
3799 sjlj_size_of_call_site_table (void)
3801 int n
= cfun
->eh
->call_site_data_used
;
3805 for (i
= 0; i
< n
; ++i
)
3807 struct call_site_record
*cs
= &cfun
->eh
->call_site_data
[i
];
3808 size
+= size_of_uleb128 (INTVAL (cs
->landing_pad
));
3809 size
+= size_of_uleb128 (cs
->action
);
3817 dw2_output_call_site_table (void)
3819 const char *const function_start_lab
3820 = IDENTIFIER_POINTER (current_function_func_begin_label
);
3821 int n
= cfun
->eh
->call_site_data_used
;
3824 for (i
= 0; i
< n
; ++i
)
3826 struct call_site_record
*cs
= &cfun
->eh
->call_site_data
[i
];
3827 char reg_start_lab
[32];
3828 char reg_end_lab
[32];
3829 char landing_pad_lab
[32];
3831 ASM_GENERATE_INTERNAL_LABEL (reg_start_lab
, "LEHB", call_site_base
+ i
);
3832 ASM_GENERATE_INTERNAL_LABEL (reg_end_lab
, "LEHE", call_site_base
+ i
);
3834 if (cs
->landing_pad
)
3835 ASM_GENERATE_INTERNAL_LABEL (landing_pad_lab
, "L",
3836 CODE_LABEL_NUMBER (cs
->landing_pad
));
3838 /* ??? Perhaps use insn length scaling if the assembler supports
3839 generic arithmetic. */
3840 /* ??? Perhaps use attr_length to choose data1 or data2 instead of
3841 data4 if the function is small enough. */
3842 #ifdef HAVE_AS_LEB128
3843 dw2_asm_output_delta_uleb128 (reg_start_lab
, function_start_lab
,
3844 "region %d start", i
);
3845 dw2_asm_output_delta_uleb128 (reg_end_lab
, reg_start_lab
,
3847 if (cs
->landing_pad
)
3848 dw2_asm_output_delta_uleb128 (landing_pad_lab
, function_start_lab
,
3851 dw2_asm_output_data_uleb128 (0, "landing pad");
3853 dw2_asm_output_delta (4, reg_start_lab
, function_start_lab
,
3854 "region %d start", i
);
3855 dw2_asm_output_delta (4, reg_end_lab
, reg_start_lab
, "length");
3856 if (cs
->landing_pad
)
3857 dw2_asm_output_delta (4, landing_pad_lab
, function_start_lab
,
3860 dw2_asm_output_data (4, 0, "landing pad");
3862 dw2_asm_output_data_uleb128 (cs
->action
, "action");
3865 call_site_base
+= n
;
3869 sjlj_output_call_site_table (void)
3871 int n
= cfun
->eh
->call_site_data_used
;
3874 for (i
= 0; i
< n
; ++i
)
3876 struct call_site_record
*cs
= &cfun
->eh
->call_site_data
[i
];
3878 dw2_asm_output_data_uleb128 (INTVAL (cs
->landing_pad
),
3879 "region %d landing pad", i
);
3880 dw2_asm_output_data_uleb128 (cs
->action
, "action");
3883 call_site_base
+= n
;
3886 /* Tell assembler to switch to the section for the exception handling
3890 default_exception_section (void)
3892 if (targetm
.have_named_sections
)
3895 #ifdef HAVE_LD_RO_RW_SECTION_MIXING
3896 int tt_format
= ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3899 || ((tt_format
& 0x70) != DW_EH_PE_absptr
3900 && (tt_format
& 0x70) != DW_EH_PE_aligned
))
3901 ? 0 : SECTION_WRITE
;
3903 flags
= SECTION_WRITE
;
3905 named_section_flags (".gcc_except_table", flags
);
3910 readonly_data_section ();
3914 output_function_exception_table (void)
3916 int tt_format
, cs_format
, lp_format
, i
, n
;
3917 #ifdef HAVE_AS_LEB128
3918 char ttype_label
[32];
3919 char cs_after_size_label
[32];
3920 char cs_end_label
[32];
3925 int tt_format_size
= 0;
3927 /* Not all functions need anything. */
3928 if (! cfun
->uses_eh_lsda
)
3931 #ifdef IA64_UNWIND_INFO
3932 fputs ("\t.personality\t", asm_out_file
);
3933 output_addr_const (asm_out_file
, eh_personality_libfunc
);
3934 fputs ("\n\t.handlerdata\n", asm_out_file
);
3935 /* Note that varasm still thinks we're in the function's code section.
3936 The ".endp" directive that will immediately follow will take us back. */
3938 targetm
.asm_out
.exception_section ();
3941 have_tt_data
= (VARRAY_ACTIVE_SIZE (cfun
->eh
->ttype_data
) > 0
3942 || VARRAY_ACTIVE_SIZE (cfun
->eh
->ehspec_data
) > 0);
3944 /* Indicate the format of the @TType entries. */
3946 tt_format
= DW_EH_PE_omit
;
3949 tt_format
= ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3950 #ifdef HAVE_AS_LEB128
3951 ASM_GENERATE_INTERNAL_LABEL (ttype_label
, "LLSDATT",
3952 current_function_funcdef_no
);
3954 tt_format_size
= size_of_encoded_value (tt_format
);
3956 assemble_align (tt_format_size
* BITS_PER_UNIT
);
3959 targetm
.asm_out
.internal_label (asm_out_file
, "LLSDA",
3960 current_function_funcdef_no
);
3962 /* The LSDA header. */
3964 /* Indicate the format of the landing pad start pointer. An omitted
3965 field implies @LPStart == @Start. */
3966 /* Currently we always put @LPStart == @Start. This field would
3967 be most useful in moving the landing pads completely out of
3968 line to another section, but it could also be used to minimize
3969 the size of uleb128 landing pad offsets. */
3970 lp_format
= DW_EH_PE_omit
;
3971 dw2_asm_output_data (1, lp_format
, "@LPStart format (%s)",
3972 eh_data_format_name (lp_format
));
3974 /* @LPStart pointer would go here. */
3976 dw2_asm_output_data (1, tt_format
, "@TType format (%s)",
3977 eh_data_format_name (tt_format
));
3979 #ifndef HAVE_AS_LEB128
3980 if (USING_SJLJ_EXCEPTIONS
)
3981 call_site_len
= sjlj_size_of_call_site_table ();
3983 call_site_len
= dw2_size_of_call_site_table ();
3986 /* A pc-relative 4-byte displacement to the @TType data. */
3989 #ifdef HAVE_AS_LEB128
3990 char ttype_after_disp_label
[32];
3991 ASM_GENERATE_INTERNAL_LABEL (ttype_after_disp_label
, "LLSDATTD",
3992 current_function_funcdef_no
);
3993 dw2_asm_output_delta_uleb128 (ttype_label
, ttype_after_disp_label
,
3994 "@TType base offset");
3995 ASM_OUTPUT_LABEL (asm_out_file
, ttype_after_disp_label
);
3997 /* Ug. Alignment queers things. */
3998 unsigned int before_disp
, after_disp
, last_disp
, disp
;
4000 before_disp
= 1 + 1;
4001 after_disp
= (1 + size_of_uleb128 (call_site_len
)
4003 + VARRAY_ACTIVE_SIZE (cfun
->eh
->action_record_data
)
4004 + (VARRAY_ACTIVE_SIZE (cfun
->eh
->ttype_data
)
4010 unsigned int disp_size
, pad
;
4013 disp_size
= size_of_uleb128 (disp
);
4014 pad
= before_disp
+ disp_size
+ after_disp
;
4015 if (pad
% tt_format_size
)
4016 pad
= tt_format_size
- (pad
% tt_format_size
);
4019 disp
= after_disp
+ pad
;
4021 while (disp
!= last_disp
);
4023 dw2_asm_output_data_uleb128 (disp
, "@TType base offset");
4027 /* Indicate the format of the call-site offsets. */
4028 #ifdef HAVE_AS_LEB128
4029 cs_format
= DW_EH_PE_uleb128
;
4031 cs_format
= DW_EH_PE_udata4
;
4033 dw2_asm_output_data (1, cs_format
, "call-site format (%s)",
4034 eh_data_format_name (cs_format
));
4036 #ifdef HAVE_AS_LEB128
4037 ASM_GENERATE_INTERNAL_LABEL (cs_after_size_label
, "LLSDACSB",
4038 current_function_funcdef_no
);
4039 ASM_GENERATE_INTERNAL_LABEL (cs_end_label
, "LLSDACSE",
4040 current_function_funcdef_no
);
4041 dw2_asm_output_delta_uleb128 (cs_end_label
, cs_after_size_label
,
4042 "Call-site table length");
4043 ASM_OUTPUT_LABEL (asm_out_file
, cs_after_size_label
);
4044 if (USING_SJLJ_EXCEPTIONS
)
4045 sjlj_output_call_site_table ();
4047 dw2_output_call_site_table ();
4048 ASM_OUTPUT_LABEL (asm_out_file
, cs_end_label
);
4050 dw2_asm_output_data_uleb128 (call_site_len
,"Call-site table length");
4051 if (USING_SJLJ_EXCEPTIONS
)
4052 sjlj_output_call_site_table ();
4054 dw2_output_call_site_table ();
4057 /* ??? Decode and interpret the data for flag_debug_asm. */
4058 n
= VARRAY_ACTIVE_SIZE (cfun
->eh
->action_record_data
);
4059 for (i
= 0; i
< n
; ++i
)
4060 dw2_asm_output_data (1, VARRAY_UCHAR (cfun
->eh
->action_record_data
, i
),
4061 (i
? NULL
: "Action record table"));
4064 assemble_align (tt_format_size
* BITS_PER_UNIT
);
4066 i
= VARRAY_ACTIVE_SIZE (cfun
->eh
->ttype_data
);
4069 tree type
= VARRAY_TREE (cfun
->eh
->ttype_data
, i
);
4072 if (type
== NULL_TREE
)
4076 struct cgraph_varpool_node
*node
;
4078 type
= lookup_type_for_runtime (type
);
4079 value
= expand_expr (type
, NULL_RTX
, VOIDmode
, EXPAND_INITIALIZER
);
4081 /* Let cgraph know that the rtti decl is used. Not all of the
4082 paths below go through assemble_integer, which would take
4083 care of this for us. */
4085 if (TREE_CODE (type
) == ADDR_EXPR
)
4087 type
= TREE_OPERAND (type
, 0);
4088 if (TREE_CODE (type
) == VAR_DECL
)
4090 node
= cgraph_varpool_node (type
);
4092 cgraph_varpool_mark_needed_node (node
);
4095 else if (TREE_CODE (type
) != INTEGER_CST
)
4099 if (tt_format
== DW_EH_PE_absptr
|| tt_format
== DW_EH_PE_aligned
)
4100 assemble_integer (value
, tt_format_size
,
4101 tt_format_size
* BITS_PER_UNIT
, 1);
4103 dw2_asm_output_encoded_addr_rtx (tt_format
, value
, NULL
);
4106 #ifdef HAVE_AS_LEB128
4108 ASM_OUTPUT_LABEL (asm_out_file
, ttype_label
);
4111 /* ??? Decode and interpret the data for flag_debug_asm. */
4112 n
= VARRAY_ACTIVE_SIZE (cfun
->eh
->ehspec_data
);
4113 for (i
= 0; i
< n
; ++i
)
4114 dw2_asm_output_data (1, VARRAY_UCHAR (cfun
->eh
->ehspec_data
, i
),
4115 (i
? NULL
: "Exception specification table"));
4117 function_section (current_function_decl
);
4120 #include "gt-except.h"