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 void resolve_fixup_regions (void);
264 static void remove_fixup_regions (void);
265 static void remove_unreachable_regions (rtx
);
266 static void convert_from_eh_region_ranges_1 (rtx
*, int *, int);
268 static struct eh_region
*duplicate_eh_region_1 (struct eh_region
*,
269 struct inline_remap
*);
270 static void duplicate_eh_region_2 (struct eh_region
*, struct eh_region
**);
271 static int ttypes_filter_eq (const void *, const void *);
272 static hashval_t
ttypes_filter_hash (const void *);
273 static int ehspec_filter_eq (const void *, const void *);
274 static hashval_t
ehspec_filter_hash (const void *);
275 static int add_ttypes_entry (htab_t
, tree
);
276 static int add_ehspec_entry (htab_t
, htab_t
, tree
);
277 static void assign_filter_values (void);
278 static void build_post_landing_pads (void);
279 static void connect_post_landing_pads (void);
280 static void dw2_build_landing_pads (void);
283 static bool sjlj_find_directly_reachable_regions (struct sjlj_lp_info
*);
284 static void sjlj_assign_call_site_values (rtx
, struct sjlj_lp_info
*);
285 static void sjlj_mark_call_sites (struct sjlj_lp_info
*);
286 static void sjlj_emit_function_enter (rtx
);
287 static void sjlj_emit_function_exit (void);
288 static void sjlj_emit_dispatch_table (rtx
, struct sjlj_lp_info
*);
289 static void sjlj_build_landing_pads (void);
291 static hashval_t
ehl_hash (const void *);
292 static int ehl_eq (const void *, const void *);
293 static void add_ehl_entry (rtx
, struct eh_region
*);
294 static void remove_exception_handler_label (rtx
);
295 static void remove_eh_handler (struct eh_region
*);
296 static int for_each_eh_label_1 (void **, void *);
298 /* The return value of reachable_next_level. */
301 /* The given exception is not processed by the given region. */
303 /* The given exception may need processing by the given region. */
305 /* The given exception is completely processed by the given region. */
307 /* The given exception is completely processed by the runtime. */
311 struct reachable_info
;
312 static enum reachable_code
reachable_next_level (struct eh_region
*, tree
,
313 struct reachable_info
*);
315 static int action_record_eq (const void *, const void *);
316 static hashval_t
action_record_hash (const void *);
317 static int add_action_record (htab_t
, int, int);
318 static int collect_one_action_chain (htab_t
, struct eh_region
*);
319 static int add_call_site (rtx
, int);
321 static void push_uleb128 (varray_type
*, unsigned int);
322 static void push_sleb128 (varray_type
*, int);
323 #ifndef HAVE_AS_LEB128
324 static int dw2_size_of_call_site_table (void);
325 static int sjlj_size_of_call_site_table (void);
327 static void dw2_output_call_site_table (void);
328 static void sjlj_output_call_site_table (void);
331 /* Routine to see if exception handling is turned on.
332 DO_WARN is nonzero if we want to inform the user that exception
333 handling is turned off.
335 This is used to ensure that -fexceptions has been specified if the
336 compiler tries to use any exception-specific functions. */
339 doing_eh (int do_warn
)
341 if (! flag_exceptions
)
343 static int warned
= 0;
344 if (! warned
&& do_warn
)
346 error ("exception handling disabled, use -fexceptions to enable");
358 if (! flag_exceptions
)
361 type_to_runtime_map
= htab_create_ggc (31, t2r_hash
, t2r_eq
, NULL
);
363 /* Create the SjLj_Function_Context structure. This should match
364 the definition in unwind-sjlj.c. */
365 if (USING_SJLJ_EXCEPTIONS
)
367 tree f_jbuf
, f_per
, f_lsda
, f_prev
, f_cs
, f_data
, tmp
;
369 sjlj_fc_type_node
= lang_hooks
.types
.make_type (RECORD_TYPE
);
371 f_prev
= build_decl (FIELD_DECL
, get_identifier ("__prev"),
372 build_pointer_type (sjlj_fc_type_node
));
373 DECL_FIELD_CONTEXT (f_prev
) = sjlj_fc_type_node
;
375 f_cs
= build_decl (FIELD_DECL
, get_identifier ("__call_site"),
377 DECL_FIELD_CONTEXT (f_cs
) = sjlj_fc_type_node
;
379 tmp
= build_index_type (build_int_cst (NULL_TREE
, 4 - 1, 0));
380 tmp
= build_array_type (lang_hooks
.types
.type_for_mode (word_mode
, 1),
382 f_data
= build_decl (FIELD_DECL
, get_identifier ("__data"), tmp
);
383 DECL_FIELD_CONTEXT (f_data
) = sjlj_fc_type_node
;
385 f_per
= build_decl (FIELD_DECL
, get_identifier ("__personality"),
387 DECL_FIELD_CONTEXT (f_per
) = sjlj_fc_type_node
;
389 f_lsda
= build_decl (FIELD_DECL
, get_identifier ("__lsda"),
391 DECL_FIELD_CONTEXT (f_lsda
) = sjlj_fc_type_node
;
393 #ifdef DONT_USE_BUILTIN_SETJMP
395 tmp
= build_int_cst (NULL_TREE
, JMP_BUF_SIZE
- 1, 0);
397 /* Should be large enough for most systems, if it is not,
398 JMP_BUF_SIZE should be defined with the proper value. It will
399 also tend to be larger than necessary for most systems, a more
400 optimal port will define JMP_BUF_SIZE. */
401 tmp
= build_int_cst (NULL_TREE
,
402 FIRST_PSEUDO_REGISTER
+ 2 - 1, 0);
405 /* builtin_setjmp takes a pointer to 5 words. */
406 tmp
= build_int_cst (NULL_TREE
,
407 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
;
578 expand_resx_expr (tree exp
)
580 int region_nr
= TREE_INT_CST_LOW (TREE_OPERAND (exp
, 0));
581 struct eh_region
*reg
= cfun
->eh
->region_array
[region_nr
];
583 reg
->resume
= emit_jump_insn (gen_rtx_RESX (VOIDmode
, region_nr
));
587 /* Note that the current EH region (if any) may contain a throw, or a
588 call to a function which itself may contain a throw. */
591 note_eh_region_may_contain_throw (struct eh_region
*region
)
593 while (region
&& !region
->may_contain_throw
)
595 region
->may_contain_throw
= 1;
596 region
= region
->outer
;
601 note_current_region_may_contain_throw (void)
603 note_eh_region_may_contain_throw (cfun
->eh
->cur_region
);
607 /* Return an rtl expression for a pointer to the exception object
611 get_exception_pointer (struct function
*fun
)
613 rtx exc_ptr
= fun
->eh
->exc_ptr
;
614 if (fun
== cfun
&& ! exc_ptr
)
616 exc_ptr
= gen_reg_rtx (ptr_mode
);
617 fun
->eh
->exc_ptr
= exc_ptr
;
622 /* Return an rtl expression for the exception dispatch filter
626 get_exception_filter (struct function
*fun
)
628 rtx filter
= fun
->eh
->filter
;
629 if (fun
== cfun
&& ! filter
)
631 filter
= gen_reg_rtx (targetm
.eh_return_filter_mode ());
632 fun
->eh
->filter
= filter
;
637 /* This section is for the exception handling specific optimization pass. */
639 /* Random access the exception region tree. */
642 collect_eh_region_array (void)
644 struct eh_region
**array
, *i
;
646 i
= cfun
->eh
->region_tree
;
650 array
= ggc_alloc_cleared ((cfun
->eh
->last_region_number
+ 1)
652 cfun
->eh
->region_array
= array
;
656 array
[i
->region_number
] = i
;
658 /* If there are sub-regions, process them. */
661 /* If there are peers, process them. */
662 else if (i
->next_peer
)
664 /* Otherwise, step back up the tree to the next peer. */
671 } while (i
->next_peer
== NULL
);
678 resolve_one_fixup_region (struct eh_region
*fixup
)
680 struct eh_region
*cleanup
, *real
;
683 n
= cfun
->eh
->last_region_number
;
686 for (j
= 1; j
<= n
; ++j
)
688 cleanup
= cfun
->eh
->region_array
[j
];
689 if (cleanup
&& cleanup
->type
== ERT_CLEANUP
690 && cleanup
->u
.cleanup
.exp
== fixup
->u
.fixup
.cleanup_exp
)
696 real
= cleanup
->outer
;
697 if (real
&& real
->type
== ERT_FIXUP
)
699 if (!real
->u
.fixup
.resolved
)
700 resolve_one_fixup_region (real
);
701 real
= real
->u
.fixup
.real_region
;
704 fixup
->u
.fixup
.real_region
= real
;
705 fixup
->u
.fixup
.resolved
= true;
709 resolve_fixup_regions (void)
711 int i
, n
= cfun
->eh
->last_region_number
;
713 for (i
= 1; i
<= n
; ++i
)
715 struct eh_region
*fixup
= cfun
->eh
->region_array
[i
];
717 if (!fixup
|| fixup
->type
!= ERT_FIXUP
|| fixup
->u
.fixup
.resolved
)
720 resolve_one_fixup_region (fixup
);
724 /* Now that we've discovered what region actually encloses a fixup,
725 we can shuffle pointers and remove them from the tree. */
728 remove_fixup_regions (void)
732 struct eh_region
*fixup
;
734 /* Walk the insn chain and adjust the REG_EH_REGION numbers
735 for instructions referencing fixup regions. This is only
736 strictly necessary for fixup regions with no parent, but
737 doesn't hurt to do it for all regions. */
738 for (insn
= get_insns(); insn
; insn
= NEXT_INSN (insn
))
740 && (note
= find_reg_note (insn
, REG_EH_REGION
, NULL
))
741 && INTVAL (XEXP (note
, 0)) > 0
742 && (fixup
= cfun
->eh
->region_array
[INTVAL (XEXP (note
, 0))])
743 && fixup
->type
== ERT_FIXUP
)
745 if (fixup
->u
.fixup
.real_region
)
746 XEXP (note
, 0) = GEN_INT (fixup
->u
.fixup
.real_region
->region_number
);
748 remove_note (insn
, note
);
751 /* Remove the fixup regions from the tree. */
752 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
754 fixup
= cfun
->eh
->region_array
[i
];
758 /* Allow GC to maybe free some memory. */
759 if (fixup
->type
== ERT_CLEANUP
)
760 fixup
->u
.cleanup
.exp
= NULL_TREE
;
762 if (fixup
->type
!= ERT_FIXUP
)
767 struct eh_region
*parent
, *p
, **pp
;
769 parent
= fixup
->u
.fixup
.real_region
;
771 /* Fix up the children's parent pointers; find the end of
773 for (p
= fixup
->inner
; ; p
= p
->next_peer
)
780 /* In the tree of cleanups, only outer-inner ordering matters.
781 So link the children back in anywhere at the correct level. */
785 pp
= &cfun
->eh
->region_tree
;
791 remove_eh_handler (fixup
);
795 /* Remove all regions whose labels are not reachable from insns. */
798 remove_unreachable_regions (rtx insns
)
800 int i
, *uid_region_num
;
805 uid_region_num
= xcalloc (get_max_uid (), sizeof(int));
806 reachable
= xcalloc (cfun
->eh
->last_region_number
+ 1, sizeof(bool));
808 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
810 r
= cfun
->eh
->region_array
[i
];
811 if (!r
|| r
->region_number
!= i
)
816 if (uid_region_num
[INSN_UID (r
->resume
)])
818 uid_region_num
[INSN_UID (r
->resume
)] = i
;
822 if (uid_region_num
[INSN_UID (r
->label
)])
824 uid_region_num
[INSN_UID (r
->label
)] = i
;
828 for (insn
= insns
; insn
; insn
= NEXT_INSN (insn
))
829 reachable
[uid_region_num
[INSN_UID (insn
)]] = true;
831 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
833 r
= cfun
->eh
->region_array
[i
];
834 if (r
&& r
->region_number
== i
&& !reachable
[i
])
840 /* Don't remove ERT_THROW regions if their outer region
842 if (r
->outer
&& reachable
[r
->outer
->region_number
])
846 case ERT_MUST_NOT_THROW
:
847 /* MUST_NOT_THROW regions are implementable solely in the
848 runtime, but their existence continues to affect calls
849 within that region. Never delete them here. */
855 /* TRY regions are reachable if any of its CATCH regions
858 for (c
= r
->u
.try.catch; c
; c
= c
->u
.catch.next_catch
)
859 if (reachable
[c
->region_number
])
872 remove_eh_handler (r
);
877 free (uid_region_num
);
880 /* Turn NOTE_INSN_EH_REGION notes into REG_EH_REGION notes for each
881 can_throw instruction in the region. */
884 convert_from_eh_region_ranges_1 (rtx
*pinsns
, int *orig_sp
, int cur
)
889 for (insn
= *pinsns
; insn
; insn
= next
)
891 next
= NEXT_INSN (insn
);
894 int kind
= NOTE_LINE_NUMBER (insn
);
895 if (kind
== NOTE_INSN_EH_REGION_BEG
896 || kind
== NOTE_INSN_EH_REGION_END
)
898 if (kind
== NOTE_INSN_EH_REGION_BEG
)
903 cur
= NOTE_EH_HANDLER (insn
);
905 r
= cfun
->eh
->region_array
[cur
];
906 if (r
->type
== ERT_FIXUP
)
908 r
= r
->u
.fixup
.real_region
;
909 cur
= r
? r
->region_number
: 0;
911 else if (r
->type
== ERT_CATCH
)
914 cur
= r
? r
->region_number
: 0;
926 else if (INSN_P (insn
))
929 && ! find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
)
930 /* Calls can always potentially throw exceptions, unless
931 they have a REG_EH_REGION note with a value of 0 or less.
932 Which should be the only possible kind so far. */
934 /* If we wanted exceptions for non-call insns, then
935 any may_trap_p instruction could throw. */
936 || (flag_non_call_exceptions
937 && GET_CODE (PATTERN (insn
)) != CLOBBER
938 && GET_CODE (PATTERN (insn
)) != USE
939 && may_trap_p (PATTERN (insn
)))))
941 REG_NOTES (insn
) = alloc_EXPR_LIST (REG_EH_REGION
, GEN_INT (cur
),
952 collect_rtl_labels_from_trees (void)
954 int i
, n
= cfun
->eh
->last_region_number
;
955 for (i
= 1; i
<= n
; ++i
)
957 struct eh_region
*reg
= cfun
->eh
->region_array
[i
];
958 if (reg
&& reg
->tree_label
)
959 reg
->label
= DECL_RTL_IF_SET (reg
->tree_label
);
964 convert_from_eh_region_ranges (void)
966 rtx insns
= get_insns ();
968 if (cfun
->eh
->region_array
)
970 /* If the region array already exists, assume we're coming from
971 optimize_function_tree. In this case all we need to do is
972 collect the rtl labels that correspond to the tree labels
973 that we allocated earlier. */
974 collect_rtl_labels_from_trees ();
980 collect_eh_region_array ();
981 resolve_fixup_regions ();
983 stack
= xmalloc (sizeof (int) * (cfun
->eh
->last_region_number
+ 1));
984 convert_from_eh_region_ranges_1 (&insns
, stack
, 0);
987 remove_fixup_regions ();
990 remove_unreachable_regions (insns
);
994 add_ehl_entry (rtx label
, struct eh_region
*region
)
996 struct ehl_map_entry
**slot
, *entry
;
998 LABEL_PRESERVE_P (label
) = 1;
1000 entry
= ggc_alloc (sizeof (*entry
));
1001 entry
->label
= label
;
1002 entry
->region
= region
;
1004 slot
= (struct ehl_map_entry
**)
1005 htab_find_slot (cfun
->eh
->exception_handler_label_map
, entry
, INSERT
);
1007 /* Before landing pad creation, each exception handler has its own
1008 label. After landing pad creation, the exception handlers may
1009 share landing pads. This is ok, since maybe_remove_eh_handler
1010 only requires the 1-1 mapping before landing pad creation. */
1011 if (*slot
&& !cfun
->eh
->built_landing_pads
)
1018 find_exception_handler_labels (void)
1022 if (cfun
->eh
->exception_handler_label_map
)
1023 htab_empty (cfun
->eh
->exception_handler_label_map
);
1026 /* ??? The expansion factor here (3/2) must be greater than the htab
1027 occupancy factor (4/3) to avoid unnecessary resizing. */
1028 cfun
->eh
->exception_handler_label_map
1029 = htab_create_ggc (cfun
->eh
->last_region_number
* 3 / 2,
1030 ehl_hash
, ehl_eq
, NULL
);
1033 if (cfun
->eh
->region_tree
== NULL
)
1036 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1038 struct eh_region
*region
= cfun
->eh
->region_array
[i
];
1041 if (! region
|| region
->region_number
!= i
)
1043 if (cfun
->eh
->built_landing_pads
)
1044 lab
= region
->landing_pad
;
1046 lab
= region
->label
;
1049 add_ehl_entry (lab
, region
);
1052 /* For sjlj exceptions, need the return label to remain live until
1053 after landing pad generation. */
1054 if (USING_SJLJ_EXCEPTIONS
&& ! cfun
->eh
->built_landing_pads
)
1055 add_ehl_entry (return_label
, NULL
);
1059 current_function_has_exception_handlers (void)
1063 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1065 struct eh_region
*region
= cfun
->eh
->region_array
[i
];
1067 if (! region
|| region
->region_number
!= i
)
1069 if (region
->type
!= ERT_THROW
)
1076 static struct eh_region
*
1077 duplicate_eh_region_1 (struct eh_region
*o
, struct inline_remap
*map
)
1079 struct eh_region
*n
= ggc_alloc_cleared (sizeof (struct eh_region
));
1081 n
->region_number
= o
->region_number
+ cfun
->eh
->last_region_number
;
1087 case ERT_MUST_NOT_THROW
:
1091 if (o
->u
.try.continue_label
)
1092 n
->u
.try.continue_label
1093 = get_label_from_map (map
,
1094 CODE_LABEL_NUMBER (o
->u
.try.continue_label
));
1098 n
->u
.catch.type_list
= o
->u
.catch.type_list
;
1101 case ERT_ALLOWED_EXCEPTIONS
:
1102 n
->u
.allowed
.type_list
= o
->u
.allowed
.type_list
;
1106 n
->u
.throw.type
= o
->u
.throw.type
;
1113 n
->label
= get_label_from_map (map
, CODE_LABEL_NUMBER (o
->label
));
1116 n
->resume
= map
->insn_map
[INSN_UID (o
->resume
)];
1117 if (n
->resume
== NULL
)
1125 duplicate_eh_region_2 (struct eh_region
*o
, struct eh_region
**n_array
)
1127 struct eh_region
*n
= n_array
[o
->region_number
];
1132 n
->u
.try.catch = n_array
[o
->u
.try.catch->region_number
];
1133 n
->u
.try.last_catch
= n_array
[o
->u
.try.last_catch
->region_number
];
1137 if (o
->u
.catch.next_catch
)
1138 n
->u
.catch.next_catch
= n_array
[o
->u
.catch.next_catch
->region_number
];
1139 if (o
->u
.catch.prev_catch
)
1140 n
->u
.catch.prev_catch
= n_array
[o
->u
.catch.prev_catch
->region_number
];
1148 n
->outer
= n_array
[o
->outer
->region_number
];
1150 n
->inner
= n_array
[o
->inner
->region_number
];
1152 n
->next_peer
= n_array
[o
->next_peer
->region_number
];
1156 duplicate_eh_regions (struct function
*ifun
, struct inline_remap
*map
)
1158 int ifun_last_region_number
= ifun
->eh
->last_region_number
;
1159 struct eh_region
**n_array
, *root
, *cur
;
1162 if (ifun_last_region_number
== 0)
1165 n_array
= xcalloc (ifun_last_region_number
+ 1, sizeof (*n_array
));
1167 for (i
= 1; i
<= ifun_last_region_number
; ++i
)
1169 cur
= ifun
->eh
->region_array
[i
];
1170 if (!cur
|| cur
->region_number
!= i
)
1172 n_array
[i
] = duplicate_eh_region_1 (cur
, map
);
1174 for (i
= 1; i
<= ifun_last_region_number
; ++i
)
1176 cur
= ifun
->eh
->region_array
[i
];
1177 if (!cur
|| cur
->region_number
!= i
)
1179 duplicate_eh_region_2 (cur
, n_array
);
1182 root
= n_array
[ifun
->eh
->region_tree
->region_number
];
1183 cur
= cfun
->eh
->cur_region
;
1186 struct eh_region
*p
= cur
->inner
;
1189 while (p
->next_peer
)
1191 p
->next_peer
= root
;
1196 for (i
= 1; i
<= ifun_last_region_number
; ++i
)
1197 if (n_array
[i
] && n_array
[i
]->outer
== NULL
)
1198 n_array
[i
]->outer
= cur
;
1202 struct eh_region
*p
= cfun
->eh
->region_tree
;
1205 while (p
->next_peer
)
1207 p
->next_peer
= root
;
1210 cfun
->eh
->region_tree
= root
;
1215 i
= cfun
->eh
->last_region_number
;
1216 cfun
->eh
->last_region_number
= i
+ ifun_last_region_number
;
1222 t2r_eq (const void *pentry
, const void *pdata
)
1224 tree entry
= (tree
) pentry
;
1225 tree data
= (tree
) pdata
;
1227 return TREE_PURPOSE (entry
) == data
;
1231 t2r_hash (const void *pentry
)
1233 tree entry
= (tree
) pentry
;
1234 return TREE_HASH (TREE_PURPOSE (entry
));
1238 add_type_for_runtime (tree type
)
1242 slot
= (tree
*) htab_find_slot_with_hash (type_to_runtime_map
, type
,
1243 TREE_HASH (type
), INSERT
);
1246 tree runtime
= (*lang_eh_runtime_type
) (type
);
1247 *slot
= tree_cons (type
, runtime
, NULL_TREE
);
1252 lookup_type_for_runtime (tree type
)
1256 slot
= (tree
*) htab_find_slot_with_hash (type_to_runtime_map
, type
,
1257 TREE_HASH (type
), NO_INSERT
);
1259 /* We should have always inserted the data earlier. */
1260 return TREE_VALUE (*slot
);
1264 /* Represent an entry in @TTypes for either catch actions
1265 or exception filter actions. */
1266 struct ttypes_filter
GTY(())
1272 /* Compare ENTRY (a ttypes_filter entry in the hash table) with DATA
1273 (a tree) for a @TTypes type node we are thinking about adding. */
1276 ttypes_filter_eq (const void *pentry
, const void *pdata
)
1278 const struct ttypes_filter
*entry
= (const struct ttypes_filter
*) pentry
;
1279 tree data
= (tree
) pdata
;
1281 return entry
->t
== data
;
1285 ttypes_filter_hash (const void *pentry
)
1287 const struct ttypes_filter
*entry
= (const struct ttypes_filter
*) pentry
;
1288 return TREE_HASH (entry
->t
);
1291 /* Compare ENTRY with DATA (both struct ttypes_filter) for a @TTypes
1292 exception specification list we are thinking about adding. */
1293 /* ??? Currently we use the type lists in the order given. Someone
1294 should put these in some canonical order. */
1297 ehspec_filter_eq (const void *pentry
, const void *pdata
)
1299 const struct ttypes_filter
*entry
= (const struct ttypes_filter
*) pentry
;
1300 const struct ttypes_filter
*data
= (const struct ttypes_filter
*) pdata
;
1302 return type_list_equal (entry
->t
, data
->t
);
1305 /* Hash function for exception specification lists. */
1308 ehspec_filter_hash (const void *pentry
)
1310 const struct ttypes_filter
*entry
= (const struct ttypes_filter
*) pentry
;
1314 for (list
= entry
->t
; list
; list
= TREE_CHAIN (list
))
1315 h
= (h
<< 5) + (h
>> 27) + TREE_HASH (TREE_VALUE (list
));
1319 /* Add TYPE (which may be NULL) to cfun->eh->ttype_data, using TYPES_HASH
1320 to speed up the search. Return the filter value to be used. */
1323 add_ttypes_entry (htab_t ttypes_hash
, tree type
)
1325 struct ttypes_filter
**slot
, *n
;
1327 slot
= (struct ttypes_filter
**)
1328 htab_find_slot_with_hash (ttypes_hash
, type
, TREE_HASH (type
), INSERT
);
1330 if ((n
= *slot
) == NULL
)
1332 /* Filter value is a 1 based table index. */
1334 n
= xmalloc (sizeof (*n
));
1336 n
->filter
= VARRAY_ACTIVE_SIZE (cfun
->eh
->ttype_data
) + 1;
1339 VARRAY_PUSH_TREE (cfun
->eh
->ttype_data
, type
);
1345 /* Add LIST to cfun->eh->ehspec_data, using EHSPEC_HASH and TYPES_HASH
1346 to speed up the search. Return the filter value to be used. */
1349 add_ehspec_entry (htab_t ehspec_hash
, htab_t ttypes_hash
, tree list
)
1351 struct ttypes_filter
**slot
, *n
;
1352 struct ttypes_filter dummy
;
1355 slot
= (struct ttypes_filter
**)
1356 htab_find_slot (ehspec_hash
, &dummy
, INSERT
);
1358 if ((n
= *slot
) == NULL
)
1360 /* Filter value is a -1 based byte index into a uleb128 buffer. */
1362 n
= xmalloc (sizeof (*n
));
1364 n
->filter
= -(VARRAY_ACTIVE_SIZE (cfun
->eh
->ehspec_data
) + 1);
1367 /* Look up each type in the list and encode its filter
1368 value as a uleb128. Terminate the list with 0. */
1369 for (; list
; list
= TREE_CHAIN (list
))
1370 push_uleb128 (&cfun
->eh
->ehspec_data
,
1371 add_ttypes_entry (ttypes_hash
, TREE_VALUE (list
)));
1372 VARRAY_PUSH_UCHAR (cfun
->eh
->ehspec_data
, 0);
1378 /* Generate the action filter values to be used for CATCH and
1379 ALLOWED_EXCEPTIONS regions. When using dwarf2 exception regions,
1380 we use lots of landing pads, and so every type or list can share
1381 the same filter value, which saves table space. */
1384 assign_filter_values (void)
1387 htab_t ttypes
, ehspec
;
1389 VARRAY_TREE_INIT (cfun
->eh
->ttype_data
, 16, "ttype_data");
1390 VARRAY_UCHAR_INIT (cfun
->eh
->ehspec_data
, 64, "ehspec_data");
1392 ttypes
= htab_create (31, ttypes_filter_hash
, ttypes_filter_eq
, free
);
1393 ehspec
= htab_create (31, ehspec_filter_hash
, ehspec_filter_eq
, free
);
1395 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1397 struct eh_region
*r
= cfun
->eh
->region_array
[i
];
1399 /* Mind we don't process a region more than once. */
1400 if (!r
|| r
->region_number
!= i
)
1406 /* Whatever type_list is (NULL or true list), we build a list
1407 of filters for the region. */
1408 r
->u
.catch.filter_list
= NULL_TREE
;
1410 if (r
->u
.catch.type_list
!= NULL
)
1412 /* Get a filter value for each of the types caught and store
1413 them in the region's dedicated list. */
1414 tree tp_node
= r
->u
.catch.type_list
;
1416 for (;tp_node
; tp_node
= TREE_CHAIN (tp_node
))
1418 int flt
= add_ttypes_entry (ttypes
, TREE_VALUE (tp_node
));
1419 tree flt_node
= build_int_cst (NULL_TREE
, flt
, 0);
1421 r
->u
.catch.filter_list
1422 = tree_cons (NULL_TREE
, flt_node
, r
->u
.catch.filter_list
);
1427 /* Get a filter value for the NULL list also since it will need
1428 an action record anyway. */
1429 int flt
= add_ttypes_entry (ttypes
, NULL
);
1430 tree flt_node
= build_int_cst (NULL_TREE
, flt
, 0);
1432 r
->u
.catch.filter_list
1433 = tree_cons (NULL_TREE
, flt_node
, r
->u
.catch.filter_list
);
1438 case ERT_ALLOWED_EXCEPTIONS
:
1440 = add_ehspec_entry (ehspec
, ttypes
, r
->u
.allowed
.type_list
);
1448 htab_delete (ttypes
);
1449 htab_delete (ehspec
);
1452 /* Emit SEQ into basic block just before INSN (that is assumed to be
1453 first instruction of some existing BB and return the newly
1456 emit_to_new_bb_before (rtx seq
, rtx insn
)
1462 /* If there happens to be an fallthru edge (possibly created by cleanup_cfg
1463 call), we don't want it to go into newly created landing pad or other EH
1465 for (e
= BLOCK_FOR_INSN (insn
)->pred
; e
; e
= e
->pred_next
)
1466 if (e
->flags
& EDGE_FALLTHRU
)
1467 force_nonfallthru (e
);
1468 last
= emit_insn_before (seq
, insn
);
1469 if (BARRIER_P (last
))
1470 last
= PREV_INSN (last
);
1471 bb
= create_basic_block (seq
, last
, BLOCK_FOR_INSN (insn
)->prev_bb
);
1472 update_bb_for_insn (bb
);
1473 bb
->flags
|= BB_SUPERBLOCK
;
1477 /* Generate the code to actually handle exceptions, which will follow the
1481 build_post_landing_pads (void)
1485 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1487 struct eh_region
*region
= cfun
->eh
->region_array
[i
];
1490 /* Mind we don't process a region more than once. */
1491 if (!region
|| region
->region_number
!= i
)
1494 switch (region
->type
)
1497 /* ??? Collect the set of all non-overlapping catch handlers
1498 all the way up the chain until blocked by a cleanup. */
1499 /* ??? Outer try regions can share landing pads with inner
1500 try regions if the types are completely non-overlapping,
1501 and there are no intervening cleanups. */
1503 region
->post_landing_pad
= gen_label_rtx ();
1507 emit_label (region
->post_landing_pad
);
1509 /* ??? It is mighty inconvenient to call back into the
1510 switch statement generation code in expand_end_case.
1511 Rapid prototyping sez a sequence of ifs. */
1513 struct eh_region
*c
;
1514 for (c
= region
->u
.try.catch; c
; c
= c
->u
.catch.next_catch
)
1516 if (c
->u
.catch.type_list
== NULL
)
1517 emit_jump (c
->label
);
1520 /* Need for one cmp/jump per type caught. Each type
1521 list entry has a matching entry in the filter list
1522 (see assign_filter_values). */
1523 tree tp_node
= c
->u
.catch.type_list
;
1524 tree flt_node
= c
->u
.catch.filter_list
;
1528 emit_cmp_and_jump_insns
1530 GEN_INT (tree_low_cst (TREE_VALUE (flt_node
), 0)),
1532 targetm
.eh_return_filter_mode (), 0, c
->label
);
1534 tp_node
= TREE_CHAIN (tp_node
);
1535 flt_node
= TREE_CHAIN (flt_node
);
1541 /* We delay the generation of the _Unwind_Resume until we generate
1542 landing pads. We emit a marker here so as to get good control
1543 flow data in the meantime. */
1545 = emit_jump_insn (gen_rtx_RESX (VOIDmode
, region
->region_number
));
1551 emit_to_new_bb_before (seq
, region
->u
.try.catch->label
);
1555 case ERT_ALLOWED_EXCEPTIONS
:
1556 region
->post_landing_pad
= gen_label_rtx ();
1560 emit_label (region
->post_landing_pad
);
1562 emit_cmp_and_jump_insns (cfun
->eh
->filter
,
1563 GEN_INT (region
->u
.allowed
.filter
),
1565 targetm
.eh_return_filter_mode (), 0, region
->label
);
1567 /* We delay the generation of the _Unwind_Resume until we generate
1568 landing pads. We emit a marker here so as to get good control
1569 flow data in the meantime. */
1571 = emit_jump_insn (gen_rtx_RESX (VOIDmode
, region
->region_number
));
1577 emit_to_new_bb_before (seq
, region
->label
);
1581 case ERT_MUST_NOT_THROW
:
1582 region
->post_landing_pad
= region
->label
;
1587 /* Nothing to do. */
1596 /* Replace RESX patterns with jumps to the next handler if any, or calls to
1597 _Unwind_Resume otherwise. */
1600 connect_post_landing_pads (void)
1604 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1606 struct eh_region
*region
= cfun
->eh
->region_array
[i
];
1607 struct eh_region
*outer
;
1611 /* Mind we don't process a region more than once. */
1612 if (!region
|| region
->region_number
!= i
)
1615 /* If there is no RESX, or it has been deleted by flow, there's
1616 nothing to fix up. */
1617 if (! region
->resume
|| INSN_DELETED_P (region
->resume
))
1620 /* Search for another landing pad in this function. */
1621 for (outer
= region
->outer
; outer
; outer
= outer
->outer
)
1622 if (outer
->post_landing_pad
)
1630 basic_block src
, dest
;
1632 emit_jump (outer
->post_landing_pad
);
1633 src
= BLOCK_FOR_INSN (region
->resume
);
1634 dest
= BLOCK_FOR_INSN (outer
->post_landing_pad
);
1636 remove_edge (src
->succ
);
1637 e
= make_edge (src
, dest
, 0);
1638 e
->probability
= REG_BR_PROB_BASE
;
1639 e
->count
= src
->count
;
1643 emit_library_call (unwind_resume_libfunc
, LCT_THROW
,
1644 VOIDmode
, 1, cfun
->eh
->exc_ptr
, ptr_mode
);
1646 /* What we just emitted was a throwing libcall, so it got a
1647 barrier automatically added after it. If the last insn in
1648 the libcall sequence isn't the barrier, it's because the
1649 target emits multiple insns for a call, and there are insns
1650 after the actual call insn (which are redundant and would be
1651 optimized away). The barrier is inserted exactly after the
1652 call insn, so let's go get that and delete the insns after
1653 it, because below we need the barrier to be the last insn in
1655 delete_insns_since (NEXT_INSN (last_call_insn ()));
1660 barrier
= emit_insn_before (seq
, region
->resume
);
1661 /* Avoid duplicate barrier. */
1662 if (!BARRIER_P (barrier
))
1664 delete_insn (barrier
);
1665 delete_insn (region
->resume
);
1667 /* ??? From tree-ssa we can wind up with catch regions whose
1668 label is not instantiated, but whose resx is present. Now
1669 that we've dealt with the resx, kill the region. */
1670 if (region
->label
== NULL
&& region
->type
== ERT_CLEANUP
)
1671 remove_eh_handler (region
);
1677 dw2_build_landing_pads (void)
1682 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1684 struct eh_region
*region
= cfun
->eh
->region_array
[i
];
1687 bool clobbers_hard_regs
= false;
1690 /* Mind we don't process a region more than once. */
1691 if (!region
|| region
->region_number
!= i
)
1694 if (region
->type
!= ERT_CLEANUP
1695 && region
->type
!= ERT_TRY
1696 && region
->type
!= ERT_ALLOWED_EXCEPTIONS
)
1701 region
->landing_pad
= gen_label_rtx ();
1702 emit_label (region
->landing_pad
);
1704 #ifdef HAVE_exception_receiver
1705 if (HAVE_exception_receiver
)
1706 emit_insn (gen_exception_receiver ());
1709 #ifdef HAVE_nonlocal_goto_receiver
1710 if (HAVE_nonlocal_goto_receiver
)
1711 emit_insn (gen_nonlocal_goto_receiver ());
1716 /* If the eh_return data registers are call-saved, then we
1717 won't have considered them clobbered from the call that
1718 threw. Kill them now. */
1721 unsigned r
= EH_RETURN_DATA_REGNO (j
);
1722 if (r
== INVALID_REGNUM
)
1724 if (! call_used_regs
[r
])
1726 emit_insn (gen_rtx_CLOBBER (VOIDmode
, gen_rtx_REG (Pmode
, r
)));
1727 clobbers_hard_regs
= true;
1731 if (clobbers_hard_regs
)
1733 /* @@@ This is a kludge. Not all machine descriptions define a
1734 blockage insn, but we must not allow the code we just generated
1735 to be reordered by scheduling. So emit an ASM_INPUT to act as
1737 emit_insn (gen_rtx_ASM_INPUT (VOIDmode
, ""));
1740 emit_move_insn (cfun
->eh
->exc_ptr
,
1741 gen_rtx_REG (ptr_mode
, EH_RETURN_DATA_REGNO (0)));
1742 emit_move_insn (cfun
->eh
->filter
,
1743 gen_rtx_REG (targetm
.eh_return_filter_mode (),
1744 EH_RETURN_DATA_REGNO (1)));
1749 bb
= emit_to_new_bb_before (seq
, region
->post_landing_pad
);
1750 e
= make_edge (bb
, bb
->next_bb
, EDGE_FALLTHRU
);
1751 e
->count
= bb
->count
;
1752 e
->probability
= REG_BR_PROB_BASE
;
1759 int directly_reachable
;
1762 int call_site_index
;
1766 sjlj_find_directly_reachable_regions (struct sjlj_lp_info
*lp_info
)
1769 bool found_one
= false;
1771 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
1773 struct eh_region
*region
;
1774 enum reachable_code rc
;
1778 if (! INSN_P (insn
))
1781 note
= find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
);
1782 if (!note
|| INTVAL (XEXP (note
, 0)) <= 0)
1785 region
= cfun
->eh
->region_array
[INTVAL (XEXP (note
, 0))];
1787 type_thrown
= NULL_TREE
;
1788 if (region
->type
== ERT_THROW
)
1790 type_thrown
= region
->u
.throw.type
;
1791 region
= region
->outer
;
1794 /* Find the first containing region that might handle the exception.
1795 That's the landing pad to which we will transfer control. */
1796 rc
= RNL_NOT_CAUGHT
;
1797 for (; region
; region
= region
->outer
)
1799 rc
= reachable_next_level (region
, type_thrown
, NULL
);
1800 if (rc
!= RNL_NOT_CAUGHT
)
1803 if (rc
== RNL_MAYBE_CAUGHT
|| rc
== RNL_CAUGHT
)
1805 lp_info
[region
->region_number
].directly_reachable
= 1;
1814 sjlj_assign_call_site_values (rtx dispatch_label
, struct sjlj_lp_info
*lp_info
)
1819 /* First task: build the action table. */
1821 VARRAY_UCHAR_INIT (cfun
->eh
->action_record_data
, 64, "action_record_data");
1822 ar_hash
= htab_create (31, action_record_hash
, action_record_eq
, free
);
1824 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1825 if (lp_info
[i
].directly_reachable
)
1827 struct eh_region
*r
= cfun
->eh
->region_array
[i
];
1828 r
->landing_pad
= dispatch_label
;
1829 lp_info
[i
].action_index
= collect_one_action_chain (ar_hash
, r
);
1830 if (lp_info
[i
].action_index
!= -1)
1831 cfun
->uses_eh_lsda
= 1;
1834 htab_delete (ar_hash
);
1836 /* Next: assign dispatch values. In dwarf2 terms, this would be the
1837 landing pad label for the region. For sjlj though, there is one
1838 common landing pad from which we dispatch to the post-landing pads.
1840 A region receives a dispatch index if it is directly reachable
1841 and requires in-function processing. Regions that share post-landing
1842 pads may share dispatch indices. */
1843 /* ??? Post-landing pad sharing doesn't actually happen at the moment
1844 (see build_post_landing_pads) so we don't bother checking for it. */
1847 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1848 if (lp_info
[i
].directly_reachable
)
1849 lp_info
[i
].dispatch_index
= index
++;
1851 /* Finally: assign call-site values. If dwarf2 terms, this would be
1852 the region number assigned by convert_to_eh_region_ranges, but
1853 handles no-action and must-not-throw differently. */
1856 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
1857 if (lp_info
[i
].directly_reachable
)
1859 int action
= lp_info
[i
].action_index
;
1861 /* Map must-not-throw to otherwise unused call-site index 0. */
1864 /* Map no-action to otherwise unused call-site index -1. */
1865 else if (action
== -1)
1867 /* Otherwise, look it up in the table. */
1869 index
= add_call_site (GEN_INT (lp_info
[i
].dispatch_index
), action
);
1871 lp_info
[i
].call_site_index
= index
;
1876 sjlj_mark_call_sites (struct sjlj_lp_info
*lp_info
)
1878 int last_call_site
= -2;
1881 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
1883 struct eh_region
*region
;
1885 rtx note
, before
, p
;
1887 /* Reset value tracking at extended basic block boundaries. */
1889 last_call_site
= -2;
1891 if (! INSN_P (insn
))
1894 note
= find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
);
1897 /* Calls (and trapping insns) without notes are outside any
1898 exception handling region in this function. Mark them as
1901 || (flag_non_call_exceptions
1902 && may_trap_p (PATTERN (insn
))))
1903 this_call_site
= -1;
1909 /* Calls that are known to not throw need not be marked. */
1910 if (INTVAL (XEXP (note
, 0)) <= 0)
1913 region
= cfun
->eh
->region_array
[INTVAL (XEXP (note
, 0))];
1914 this_call_site
= lp_info
[region
->region_number
].call_site_index
;
1917 if (this_call_site
== last_call_site
)
1920 /* Don't separate a call from it's argument loads. */
1923 before
= find_first_parameter_load (insn
, NULL_RTX
);
1926 mem
= adjust_address (cfun
->eh
->sjlj_fc
, TYPE_MODE (integer_type_node
),
1927 sjlj_fc_call_site_ofs
);
1928 emit_move_insn (mem
, GEN_INT (this_call_site
));
1932 emit_insn_before (p
, before
);
1933 last_call_site
= this_call_site
;
1937 /* Construct the SjLj_Function_Context. */
1940 sjlj_emit_function_enter (rtx dispatch_label
)
1942 rtx fn_begin
, fc
, mem
, seq
;
1944 fc
= cfun
->eh
->sjlj_fc
;
1948 /* We're storing this libcall's address into memory instead of
1949 calling it directly. Thus, we must call assemble_external_libcall
1950 here, as we can not depend on emit_library_call to do it for us. */
1951 assemble_external_libcall (eh_personality_libfunc
);
1952 mem
= adjust_address (fc
, Pmode
, sjlj_fc_personality_ofs
);
1953 emit_move_insn (mem
, eh_personality_libfunc
);
1955 mem
= adjust_address (fc
, Pmode
, sjlj_fc_lsda_ofs
);
1956 if (cfun
->uses_eh_lsda
)
1961 ASM_GENERATE_INTERNAL_LABEL (buf
, "LLSDA", current_function_funcdef_no
);
1962 sym
= gen_rtx_SYMBOL_REF (Pmode
, ggc_strdup (buf
));
1963 SYMBOL_REF_FLAGS (sym
) = SYMBOL_FLAG_LOCAL
;
1964 emit_move_insn (mem
, sym
);
1967 emit_move_insn (mem
, const0_rtx
);
1969 #ifdef DONT_USE_BUILTIN_SETJMP
1972 x
= emit_library_call_value (setjmp_libfunc
, NULL_RTX
, LCT_RETURNS_TWICE
,
1973 TYPE_MODE (integer_type_node
), 1,
1974 plus_constant (XEXP (fc
, 0),
1975 sjlj_fc_jbuf_ofs
), Pmode
);
1977 note
= emit_note (NOTE_INSN_EXPECTED_VALUE
);
1978 NOTE_EXPECTED_VALUE (note
) = gen_rtx_EQ (VOIDmode
, x
, const0_rtx
);
1980 emit_cmp_and_jump_insns (x
, const0_rtx
, NE
, 0,
1981 TYPE_MODE (integer_type_node
), 0, dispatch_label
);
1984 expand_builtin_setjmp_setup (plus_constant (XEXP (fc
, 0), sjlj_fc_jbuf_ofs
),
1988 emit_library_call (unwind_sjlj_register_libfunc
, LCT_NORMAL
, VOIDmode
,
1989 1, XEXP (fc
, 0), Pmode
);
1994 /* ??? Instead of doing this at the beginning of the function,
1995 do this in a block that is at loop level 0 and dominates all
1996 can_throw_internal instructions. */
1998 for (fn_begin
= get_insns (); ; fn_begin
= NEXT_INSN (fn_begin
))
1999 if (NOTE_P (fn_begin
)
2000 && (NOTE_LINE_NUMBER (fn_begin
) == NOTE_INSN_FUNCTION_BEG
2001 || NOTE_LINE_NUMBER (fn_begin
) == NOTE_INSN_BASIC_BLOCK
))
2003 if (NOTE_LINE_NUMBER (fn_begin
) == NOTE_INSN_FUNCTION_BEG
)
2004 insert_insn_on_edge (seq
, ENTRY_BLOCK_PTR
->succ
);
2007 rtx last
= BB_END (ENTRY_BLOCK_PTR
->succ
->dest
);
2008 for (; ; fn_begin
= NEXT_INSN (fn_begin
))
2009 if ((NOTE_P (fn_begin
)
2010 && NOTE_LINE_NUMBER (fn_begin
) == NOTE_INSN_FUNCTION_BEG
)
2011 || fn_begin
== last
)
2013 emit_insn_after (seq
, fn_begin
);
2017 /* Call back from expand_function_end to know where we should put
2018 the call to unwind_sjlj_unregister_libfunc if needed. */
2021 sjlj_emit_function_exit_after (rtx after
)
2023 cfun
->eh
->sjlj_exit_after
= after
;
2027 sjlj_emit_function_exit (void)
2034 emit_library_call (unwind_sjlj_unregister_libfunc
, LCT_NORMAL
, VOIDmode
,
2035 1, XEXP (cfun
->eh
->sjlj_fc
, 0), Pmode
);
2040 /* ??? Really this can be done in any block at loop level 0 that
2041 post-dominates all can_throw_internal instructions. This is
2042 the last possible moment. */
2044 for (e
= EXIT_BLOCK_PTR
->pred
; e
; e
= e
->pred_next
)
2045 if (e
->flags
& EDGE_FALLTHRU
)
2051 /* Figure out whether the place we are supposed to insert libcall
2052 is inside the last basic block or after it. In the other case
2053 we need to emit to edge. */
2054 if (e
->src
->next_bb
!= EXIT_BLOCK_PTR
)
2056 for (insn
= NEXT_INSN (BB_END (e
->src
)); insn
; insn
= NEXT_INSN (insn
))
2057 if (insn
== cfun
->eh
->sjlj_exit_after
)
2060 insert_insn_on_edge (seq
, e
);
2063 insn
= cfun
->eh
->sjlj_exit_after
;
2065 insn
= NEXT_INSN (insn
);
2066 emit_insn_after (seq
, insn
);
2072 sjlj_emit_dispatch_table (rtx dispatch_label
, struct sjlj_lp_info
*lp_info
)
2074 int i
, first_reachable
;
2075 rtx mem
, dispatch
, seq
, fc
;
2080 fc
= cfun
->eh
->sjlj_fc
;
2084 emit_label (dispatch_label
);
2086 #ifndef DONT_USE_BUILTIN_SETJMP
2087 expand_builtin_setjmp_receiver (dispatch_label
);
2090 /* Load up dispatch index, exc_ptr and filter values from the
2091 function context. */
2092 mem
= adjust_address (fc
, TYPE_MODE (integer_type_node
),
2093 sjlj_fc_call_site_ofs
);
2094 dispatch
= copy_to_reg (mem
);
2096 mem
= adjust_address (fc
, word_mode
, sjlj_fc_data_ofs
);
2097 if (word_mode
!= ptr_mode
)
2099 #ifdef POINTERS_EXTEND_UNSIGNED
2100 mem
= convert_memory_address (ptr_mode
, mem
);
2102 mem
= convert_to_mode (ptr_mode
, mem
, 0);
2105 emit_move_insn (cfun
->eh
->exc_ptr
, mem
);
2107 mem
= adjust_address (fc
, word_mode
, sjlj_fc_data_ofs
+ UNITS_PER_WORD
);
2108 emit_move_insn (cfun
->eh
->filter
, mem
);
2110 /* Jump to one of the directly reachable regions. */
2111 /* ??? This really ought to be using a switch statement. */
2113 first_reachable
= 0;
2114 for (i
= cfun
->eh
->last_region_number
; i
> 0; --i
)
2116 if (! lp_info
[i
].directly_reachable
)
2119 if (! first_reachable
)
2121 first_reachable
= i
;
2125 emit_cmp_and_jump_insns (dispatch
, GEN_INT (lp_info
[i
].dispatch_index
),
2126 EQ
, NULL_RTX
, TYPE_MODE (integer_type_node
), 0,
2127 cfun
->eh
->region_array
[i
]->post_landing_pad
);
2133 before
= cfun
->eh
->region_array
[first_reachable
]->post_landing_pad
;
2135 bb
= emit_to_new_bb_before (seq
, before
);
2136 e
= make_edge (bb
, bb
->next_bb
, EDGE_FALLTHRU
);
2137 e
->count
= bb
->count
;
2138 e
->probability
= REG_BR_PROB_BASE
;
2142 sjlj_build_landing_pads (void)
2144 struct sjlj_lp_info
*lp_info
;
2146 lp_info
= xcalloc (cfun
->eh
->last_region_number
+ 1,
2147 sizeof (struct sjlj_lp_info
));
2149 if (sjlj_find_directly_reachable_regions (lp_info
))
2151 rtx dispatch_label
= gen_label_rtx ();
2154 = assign_stack_local (TYPE_MODE (sjlj_fc_type_node
),
2155 int_size_in_bytes (sjlj_fc_type_node
),
2156 TYPE_ALIGN (sjlj_fc_type_node
));
2158 sjlj_assign_call_site_values (dispatch_label
, lp_info
);
2159 sjlj_mark_call_sites (lp_info
);
2161 sjlj_emit_function_enter (dispatch_label
);
2162 sjlj_emit_dispatch_table (dispatch_label
, lp_info
);
2163 sjlj_emit_function_exit ();
2170 finish_eh_generation (void)
2174 /* Nothing to do if no regions created. */
2175 if (cfun
->eh
->region_tree
== NULL
)
2178 /* The object here is to provide find_basic_blocks with detailed
2179 information (via reachable_handlers) on how exception control
2180 flows within the function. In this first pass, we can include
2181 type information garnered from ERT_THROW and ERT_ALLOWED_EXCEPTIONS
2182 regions, and hope that it will be useful in deleting unreachable
2183 handlers. Subsequently, we will generate landing pads which will
2184 connect many of the handlers, and then type information will not
2185 be effective. Still, this is a win over previous implementations. */
2187 /* These registers are used by the landing pads. Make sure they
2188 have been generated. */
2189 get_exception_pointer (cfun
);
2190 get_exception_filter (cfun
);
2192 /* Construct the landing pads. */
2194 assign_filter_values ();
2195 build_post_landing_pads ();
2196 connect_post_landing_pads ();
2197 if (USING_SJLJ_EXCEPTIONS
)
2198 sjlj_build_landing_pads ();
2200 dw2_build_landing_pads ();
2202 cfun
->eh
->built_landing_pads
= 1;
2204 /* We've totally changed the CFG. Start over. */
2205 find_exception_handler_labels ();
2206 break_superblocks ();
2207 if (USING_SJLJ_EXCEPTIONS
)
2208 commit_edge_insertions ();
2213 for (e
= bb
->succ
; e
; e
= next
)
2215 next
= e
->succ_next
;
2216 if (e
->flags
& EDGE_EH
)
2223 rtl_make_eh_edge (NULL
, bb
, BB_END (bb
));
2228 ehl_hash (const void *pentry
)
2230 struct ehl_map_entry
*entry
= (struct ehl_map_entry
*) pentry
;
2232 /* 2^32 * ((sqrt(5) - 1) / 2) */
2233 const hashval_t scaled_golden_ratio
= 0x9e3779b9;
2234 return CODE_LABEL_NUMBER (entry
->label
) * scaled_golden_ratio
;
2238 ehl_eq (const void *pentry
, const void *pdata
)
2240 struct ehl_map_entry
*entry
= (struct ehl_map_entry
*) pentry
;
2241 struct ehl_map_entry
*data
= (struct ehl_map_entry
*) pdata
;
2243 return entry
->label
== data
->label
;
2246 /* This section handles removing dead code for flow. */
2248 /* Remove LABEL from exception_handler_label_map. */
2251 remove_exception_handler_label (rtx label
)
2253 struct ehl_map_entry
**slot
, tmp
;
2255 /* If exception_handler_label_map was not built yet,
2256 there is nothing to do. */
2257 if (cfun
->eh
->exception_handler_label_map
== NULL
)
2261 slot
= (struct ehl_map_entry
**)
2262 htab_find_slot (cfun
->eh
->exception_handler_label_map
, &tmp
, NO_INSERT
);
2266 htab_clear_slot (cfun
->eh
->exception_handler_label_map
, (void **) slot
);
2269 /* Splice REGION from the region tree etc. */
2272 remove_eh_handler (struct eh_region
*region
)
2274 struct eh_region
**pp
, **pp_start
, *p
, *outer
, *inner
;
2277 /* For the benefit of efficiently handling REG_EH_REGION notes,
2278 replace this region in the region array with its containing
2279 region. Note that previous region deletions may result in
2280 multiple copies of this region in the array, so we have a
2281 list of alternate numbers by which we are known. */
2283 outer
= region
->outer
;
2284 cfun
->eh
->region_array
[region
->region_number
] = outer
;
2288 EXECUTE_IF_SET_IN_BITMAP (region
->aka
, 0, i
,
2289 { cfun
->eh
->region_array
[i
] = outer
; });
2295 outer
->aka
= BITMAP_GGC_ALLOC ();
2297 bitmap_a_or_b (outer
->aka
, outer
->aka
, region
->aka
);
2298 bitmap_set_bit (outer
->aka
, region
->region_number
);
2301 if (cfun
->eh
->built_landing_pads
)
2302 lab
= region
->landing_pad
;
2304 lab
= region
->label
;
2306 remove_exception_handler_label (lab
);
2309 pp_start
= &outer
->inner
;
2311 pp_start
= &cfun
->eh
->region_tree
;
2312 for (pp
= pp_start
, p
= *pp
; p
!= region
; pp
= &p
->next_peer
, p
= *pp
)
2314 *pp
= region
->next_peer
;
2316 inner
= region
->inner
;
2319 for (p
= inner
; p
->next_peer
; p
= p
->next_peer
)
2323 p
->next_peer
= *pp_start
;
2327 if (region
->type
== ERT_CATCH
)
2329 struct eh_region
*try, *next
, *prev
;
2331 for (try = region
->next_peer
;
2332 try->type
== ERT_CATCH
;
2333 try = try->next_peer
)
2335 if (try->type
!= ERT_TRY
)
2338 next
= region
->u
.catch.next_catch
;
2339 prev
= region
->u
.catch.prev_catch
;
2342 next
->u
.catch.prev_catch
= prev
;
2344 try->u
.try.last_catch
= prev
;
2346 prev
->u
.catch.next_catch
= next
;
2349 try->u
.try.catch = next
;
2351 remove_eh_handler (try);
2356 /* LABEL heads a basic block that is about to be deleted. If this
2357 label corresponds to an exception region, we may be able to
2358 delete the region. */
2361 maybe_remove_eh_handler (rtx label
)
2363 struct ehl_map_entry
**slot
, tmp
;
2364 struct eh_region
*region
;
2366 /* ??? After generating landing pads, it's not so simple to determine
2367 if the region data is completely unused. One must examine the
2368 landing pad and the post landing pad, and whether an inner try block
2369 is referencing the catch handlers directly. */
2370 if (cfun
->eh
->built_landing_pads
)
2374 slot
= (struct ehl_map_entry
**)
2375 htab_find_slot (cfun
->eh
->exception_handler_label_map
, &tmp
, NO_INSERT
);
2378 region
= (*slot
)->region
;
2382 /* Flow will want to remove MUST_NOT_THROW regions as unreachable
2383 because there is no path to the fallback call to terminate.
2384 But the region continues to affect call-site data until there
2385 are no more contained calls, which we don't see here. */
2386 if (region
->type
== ERT_MUST_NOT_THROW
)
2388 htab_clear_slot (cfun
->eh
->exception_handler_label_map
, (void **) slot
);
2389 region
->label
= NULL_RTX
;
2392 remove_eh_handler (region
);
2395 /* Invokes CALLBACK for every exception handler label. Only used by old
2396 loop hackery; should not be used by new code. */
2399 for_each_eh_label (void (*callback
) (rtx
))
2401 htab_traverse (cfun
->eh
->exception_handler_label_map
, for_each_eh_label_1
,
2402 (void *) &callback
);
2406 for_each_eh_label_1 (void **pentry
, void *data
)
2408 struct ehl_map_entry
*entry
= *(struct ehl_map_entry
**)pentry
;
2409 void (*callback
) (rtx
) = *(void (**) (rtx
)) data
;
2411 (*callback
) (entry
->label
);
2415 /* Invoke CALLBACK for every exception region in the current function. */
2418 for_each_eh_region (void (*callback
) (struct eh_region
*))
2420 int i
, n
= cfun
->eh
->last_region_number
;
2421 for (i
= 1; i
<= n
; ++i
)
2423 struct eh_region
*region
= cfun
->eh
->region_array
[i
];
2425 (*callback
) (region
);
2429 /* This section describes CFG exception edges for flow. */
2431 /* For communicating between calls to reachable_next_level. */
2432 struct reachable_info
2436 void (*callback
) (struct eh_region
*, void *);
2437 void *callback_data
;
2438 bool saw_any_handlers
;
2441 /* A subroutine of reachable_next_level. Return true if TYPE, or a
2442 base class of TYPE, is in HANDLED. */
2445 check_handled (tree handled
, tree type
)
2449 /* We can check for exact matches without front-end help. */
2450 if (! lang_eh_type_covers
)
2452 for (t
= handled
; t
; t
= TREE_CHAIN (t
))
2453 if (TREE_VALUE (t
) == type
)
2458 for (t
= handled
; t
; t
= TREE_CHAIN (t
))
2459 if ((*lang_eh_type_covers
) (TREE_VALUE (t
), type
))
2466 /* A subroutine of reachable_next_level. If we are collecting a list
2467 of handlers, add one. After landing pad generation, reference
2468 it instead of the handlers themselves. Further, the handlers are
2469 all wired together, so by referencing one, we've got them all.
2470 Before landing pad generation we reference each handler individually.
2472 LP_REGION contains the landing pad; REGION is the handler. */
2475 add_reachable_handler (struct reachable_info
*info
,
2476 struct eh_region
*lp_region
, struct eh_region
*region
)
2481 info
->saw_any_handlers
= true;
2483 if (cfun
->eh
->built_landing_pads
)
2484 info
->callback (lp_region
, info
->callback_data
);
2486 info
->callback (region
, info
->callback_data
);
2489 /* Process one level of exception regions for reachability.
2490 If TYPE_THROWN is non-null, then it is the *exact* type being
2491 propagated. If INFO is non-null, then collect handler labels
2492 and caught/allowed type information between invocations. */
2494 static enum reachable_code
2495 reachable_next_level (struct eh_region
*region
, tree type_thrown
,
2496 struct reachable_info
*info
)
2498 switch (region
->type
)
2501 /* Before landing-pad generation, we model control flow
2502 directly to the individual handlers. In this way we can
2503 see that catch handler types may shadow one another. */
2504 add_reachable_handler (info
, region
, region
);
2505 return RNL_MAYBE_CAUGHT
;
2509 struct eh_region
*c
;
2510 enum reachable_code ret
= RNL_NOT_CAUGHT
;
2512 for (c
= region
->u
.try.catch; c
; c
= c
->u
.catch.next_catch
)
2514 /* A catch-all handler ends the search. */
2515 if (c
->u
.catch.type_list
== NULL
)
2517 add_reachable_handler (info
, region
, c
);
2523 /* If we have at least one type match, end the search. */
2524 tree tp_node
= c
->u
.catch.type_list
;
2526 for (; tp_node
; tp_node
= TREE_CHAIN (tp_node
))
2528 tree type
= TREE_VALUE (tp_node
);
2530 if (type
== type_thrown
2531 || (lang_eh_type_covers
2532 && (*lang_eh_type_covers
) (type
, type_thrown
)))
2534 add_reachable_handler (info
, region
, c
);
2539 /* If we have definitive information of a match failure,
2540 the catch won't trigger. */
2541 if (lang_eh_type_covers
)
2542 return RNL_NOT_CAUGHT
;
2545 /* At this point, we either don't know what type is thrown or
2546 don't have front-end assistance to help deciding if it is
2547 covered by one of the types in the list for this region.
2549 We'd then like to add this region to the list of reachable
2550 handlers since it is indeed potentially reachable based on the
2551 information we have.
2553 Actually, this handler is for sure not reachable if all the
2554 types it matches have already been caught. That is, it is only
2555 potentially reachable if at least one of the types it catches
2556 has not been previously caught. */
2559 ret
= RNL_MAYBE_CAUGHT
;
2562 tree tp_node
= c
->u
.catch.type_list
;
2563 bool maybe_reachable
= false;
2565 /* Compute the potential reachability of this handler and
2566 update the list of types caught at the same time. */
2567 for (; tp_node
; tp_node
= TREE_CHAIN (tp_node
))
2569 tree type
= TREE_VALUE (tp_node
);
2571 if (! check_handled (info
->types_caught
, type
))
2574 = tree_cons (NULL
, type
, info
->types_caught
);
2576 maybe_reachable
= true;
2580 if (maybe_reachable
)
2582 add_reachable_handler (info
, region
, c
);
2584 /* ??? If the catch type is a base class of every allowed
2585 type, then we know we can stop the search. */
2586 ret
= RNL_MAYBE_CAUGHT
;
2594 case ERT_ALLOWED_EXCEPTIONS
:
2595 /* An empty list of types definitely ends the search. */
2596 if (region
->u
.allowed
.type_list
== NULL_TREE
)
2598 add_reachable_handler (info
, region
, region
);
2602 /* Collect a list of lists of allowed types for use in detecting
2603 when a catch may be transformed into a catch-all. */
2605 info
->types_allowed
= tree_cons (NULL_TREE
,
2606 region
->u
.allowed
.type_list
,
2607 info
->types_allowed
);
2609 /* If we have definitive information about the type hierarchy,
2610 then we can tell if the thrown type will pass through the
2612 if (type_thrown
&& lang_eh_type_covers
)
2614 if (check_handled (region
->u
.allowed
.type_list
, type_thrown
))
2615 return RNL_NOT_CAUGHT
;
2618 add_reachable_handler (info
, region
, region
);
2623 add_reachable_handler (info
, region
, region
);
2624 return RNL_MAYBE_CAUGHT
;
2627 /* Catch regions are handled by their controlling try region. */
2628 return RNL_NOT_CAUGHT
;
2630 case ERT_MUST_NOT_THROW
:
2631 /* Here we end our search, since no exceptions may propagate.
2632 If we've touched down at some landing pad previous, then the
2633 explicit function call we generated may be used. Otherwise
2634 the call is made by the runtime. */
2635 if (info
&& info
->saw_any_handlers
)
2637 add_reachable_handler (info
, region
, region
);
2646 /* Shouldn't see these here. */
2653 /* Invoke CALLBACK on each region reachable from REGION_NUMBER. */
2656 foreach_reachable_handler (int region_number
, bool is_resx
,
2657 void (*callback
) (struct eh_region
*, void *),
2658 void *callback_data
)
2660 struct reachable_info info
;
2661 struct eh_region
*region
;
2664 memset (&info
, 0, sizeof (info
));
2665 info
.callback
= callback
;
2666 info
.callback_data
= callback_data
;
2668 region
= cfun
->eh
->region_array
[region_number
];
2670 type_thrown
= NULL_TREE
;
2673 /* A RESX leaves a region instead of entering it. Thus the
2674 region itself may have been deleted out from under us. */
2677 region
= region
->outer
;
2679 else if (region
->type
== ERT_THROW
)
2681 type_thrown
= region
->u
.throw.type
;
2682 region
= region
->outer
;
2687 if (reachable_next_level (region
, type_thrown
, &info
) >= RNL_CAUGHT
)
2689 /* If we have processed one cleanup, there is no point in
2690 processing any more of them. Each cleanup will have an edge
2691 to the next outer cleanup region, so the flow graph will be
2693 if (region
->type
== ERT_CLEANUP
)
2694 region
= region
->u
.cleanup
.prev_try
;
2696 region
= region
->outer
;
2700 /* Retrieve a list of labels of exception handlers which can be
2701 reached by a given insn. */
2704 arh_to_landing_pad (struct eh_region
*region
, void *data
)
2706 rtx
*p_handlers
= data
;
2708 *p_handlers
= alloc_INSN_LIST (region
->landing_pad
, NULL_RTX
);
2712 arh_to_label (struct eh_region
*region
, void *data
)
2714 rtx
*p_handlers
= data
;
2715 *p_handlers
= alloc_INSN_LIST (region
->label
, *p_handlers
);
2719 reachable_handlers (rtx insn
)
2721 bool is_resx
= false;
2722 rtx handlers
= NULL
;
2726 && GET_CODE (PATTERN (insn
)) == RESX
)
2728 region_number
= XINT (PATTERN (insn
), 0);
2733 rtx note
= find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
);
2734 if (!note
|| INTVAL (XEXP (note
, 0)) <= 0)
2736 region_number
= INTVAL (XEXP (note
, 0));
2739 foreach_reachable_handler (region_number
, is_resx
,
2740 (cfun
->eh
->built_landing_pads
2741 ? arh_to_landing_pad
2748 /* Determine if the given INSN can throw an exception that is caught
2749 within the function. */
2752 can_throw_internal_1 (int region_number
)
2754 struct eh_region
*region
;
2757 region
= cfun
->eh
->region_array
[region_number
];
2759 type_thrown
= NULL_TREE
;
2760 if (region
->type
== ERT_THROW
)
2762 type_thrown
= region
->u
.throw.type
;
2763 region
= region
->outer
;
2766 /* If this exception is ignored by each and every containing region,
2767 then control passes straight out. The runtime may handle some
2768 regions, which also do not require processing internally. */
2769 for (; region
; region
= region
->outer
)
2771 enum reachable_code how
= reachable_next_level (region
, type_thrown
, 0);
2772 if (how
== RNL_BLOCKED
)
2774 if (how
!= RNL_NOT_CAUGHT
)
2782 can_throw_internal (rtx insn
)
2786 if (! INSN_P (insn
))
2790 && GET_CODE (PATTERN (insn
)) == RESX
2791 && XINT (PATTERN (insn
), 0) > 0)
2792 return can_throw_internal_1 (XINT (PATTERN (insn
), 0));
2794 if (NONJUMP_INSN_P (insn
)
2795 && GET_CODE (PATTERN (insn
)) == SEQUENCE
)
2796 insn
= XVECEXP (PATTERN (insn
), 0, 0);
2798 /* Every insn that might throw has an EH_REGION note. */
2799 note
= find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
);
2800 if (!note
|| INTVAL (XEXP (note
, 0)) <= 0)
2803 return can_throw_internal_1 (INTVAL (XEXP (note
, 0)));
2806 /* Determine if the given INSN can throw an exception that is
2807 visible outside the function. */
2810 can_throw_external_1 (int region_number
)
2812 struct eh_region
*region
;
2815 region
= cfun
->eh
->region_array
[region_number
];
2817 type_thrown
= NULL_TREE
;
2818 if (region
->type
== ERT_THROW
)
2820 type_thrown
= region
->u
.throw.type
;
2821 region
= region
->outer
;
2824 /* If the exception is caught or blocked by any containing region,
2825 then it is not seen by any calling function. */
2826 for (; region
; region
= region
->outer
)
2827 if (reachable_next_level (region
, type_thrown
, NULL
) >= RNL_CAUGHT
)
2834 can_throw_external (rtx insn
)
2838 if (! INSN_P (insn
))
2841 if (NONJUMP_INSN_P (insn
)
2842 && GET_CODE (PATTERN (insn
)) == SEQUENCE
)
2843 insn
= XVECEXP (PATTERN (insn
), 0, 0);
2845 note
= find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
);
2848 /* Calls (and trapping insns) without notes are outside any
2849 exception handling region in this function. We have to
2850 assume it might throw. Given that the front end and middle
2851 ends mark known NOTHROW functions, this isn't so wildly
2853 return (CALL_P (insn
)
2854 || (flag_non_call_exceptions
2855 && may_trap_p (PATTERN (insn
))));
2857 if (INTVAL (XEXP (note
, 0)) <= 0)
2860 return can_throw_external_1 (INTVAL (XEXP (note
, 0)));
2863 /* Set TREE_NOTHROW and cfun->all_throwers_are_sibcalls. */
2866 set_nothrow_function_flags (void)
2870 TREE_NOTHROW (current_function_decl
) = 1;
2872 /* Assume cfun->all_throwers_are_sibcalls until we encounter
2873 something that can throw an exception. We specifically exempt
2874 CALL_INSNs that are SIBLING_CALL_P, as these are really jumps,
2875 and can't throw. Most CALL_INSNs are not SIBLING_CALL_P, so this
2878 cfun
->all_throwers_are_sibcalls
= 1;
2880 if (! flag_exceptions
)
2883 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
2884 if (can_throw_external (insn
))
2886 TREE_NOTHROW (current_function_decl
) = 0;
2888 if (!CALL_P (insn
) || !SIBLING_CALL_P (insn
))
2890 cfun
->all_throwers_are_sibcalls
= 0;
2895 for (insn
= current_function_epilogue_delay_list
; insn
;
2896 insn
= XEXP (insn
, 1))
2897 if (can_throw_external (insn
))
2899 TREE_NOTHROW (current_function_decl
) = 0;
2901 if (!CALL_P (insn
) || !SIBLING_CALL_P (insn
))
2903 cfun
->all_throwers_are_sibcalls
= 0;
2910 /* Various hooks for unwind library. */
2912 /* Do any necessary initialization to access arbitrary stack frames.
2913 On the SPARC, this means flushing the register windows. */
2916 expand_builtin_unwind_init (void)
2918 /* Set this so all the registers get saved in our frame; we need to be
2919 able to copy the saved values for any registers from frames we unwind. */
2920 current_function_has_nonlocal_label
= 1;
2922 #ifdef SETUP_FRAME_ADDRESSES
2923 SETUP_FRAME_ADDRESSES ();
2928 expand_builtin_eh_return_data_regno (tree arglist
)
2930 tree which
= TREE_VALUE (arglist
);
2931 unsigned HOST_WIDE_INT iwhich
;
2933 if (TREE_CODE (which
) != INTEGER_CST
)
2935 error ("argument of `__builtin_eh_return_regno' must be constant");
2939 iwhich
= tree_low_cst (which
, 1);
2940 iwhich
= EH_RETURN_DATA_REGNO (iwhich
);
2941 if (iwhich
== INVALID_REGNUM
)
2944 #ifdef DWARF_FRAME_REGNUM
2945 iwhich
= DWARF_FRAME_REGNUM (iwhich
);
2947 iwhich
= DBX_REGISTER_NUMBER (iwhich
);
2950 return GEN_INT (iwhich
);
2953 /* Given a value extracted from the return address register or stack slot,
2954 return the actual address encoded in that value. */
2957 expand_builtin_extract_return_addr (tree addr_tree
)
2959 rtx addr
= expand_expr (addr_tree
, NULL_RTX
, Pmode
, 0);
2961 if (GET_MODE (addr
) != Pmode
2962 && GET_MODE (addr
) != VOIDmode
)
2964 #ifdef POINTERS_EXTEND_UNSIGNED
2965 addr
= convert_memory_address (Pmode
, addr
);
2967 addr
= convert_to_mode (Pmode
, addr
, 0);
2971 /* First mask out any unwanted bits. */
2972 #ifdef MASK_RETURN_ADDR
2973 expand_and (Pmode
, addr
, MASK_RETURN_ADDR
, addr
);
2976 /* Then adjust to find the real return address. */
2977 #if defined (RETURN_ADDR_OFFSET)
2978 addr
= plus_constant (addr
, RETURN_ADDR_OFFSET
);
2984 /* Given an actual address in addr_tree, do any necessary encoding
2985 and return the value to be stored in the return address register or
2986 stack slot so the epilogue will return to that address. */
2989 expand_builtin_frob_return_addr (tree addr_tree
)
2991 rtx addr
= expand_expr (addr_tree
, NULL_RTX
, ptr_mode
, 0);
2993 addr
= convert_memory_address (Pmode
, addr
);
2995 #ifdef RETURN_ADDR_OFFSET
2996 addr
= force_reg (Pmode
, addr
);
2997 addr
= plus_constant (addr
, -RETURN_ADDR_OFFSET
);
3003 /* Set up the epilogue with the magic bits we'll need to return to the
3004 exception handler. */
3007 expand_builtin_eh_return (tree stackadj_tree ATTRIBUTE_UNUSED
,
3012 #ifdef EH_RETURN_STACKADJ_RTX
3013 tmp
= expand_expr (stackadj_tree
, cfun
->eh
->ehr_stackadj
, VOIDmode
, 0);
3014 tmp
= convert_memory_address (Pmode
, tmp
);
3015 if (!cfun
->eh
->ehr_stackadj
)
3016 cfun
->eh
->ehr_stackadj
= copy_to_reg (tmp
);
3017 else if (tmp
!= cfun
->eh
->ehr_stackadj
)
3018 emit_move_insn (cfun
->eh
->ehr_stackadj
, tmp
);
3021 tmp
= expand_expr (handler_tree
, cfun
->eh
->ehr_handler
, VOIDmode
, 0);
3022 tmp
= convert_memory_address (Pmode
, tmp
);
3023 if (!cfun
->eh
->ehr_handler
)
3024 cfun
->eh
->ehr_handler
= copy_to_reg (tmp
);
3025 else if (tmp
!= cfun
->eh
->ehr_handler
)
3026 emit_move_insn (cfun
->eh
->ehr_handler
, tmp
);
3028 if (!cfun
->eh
->ehr_label
)
3029 cfun
->eh
->ehr_label
= gen_label_rtx ();
3030 emit_jump (cfun
->eh
->ehr_label
);
3034 expand_eh_return (void)
3038 if (! cfun
->eh
->ehr_label
)
3041 current_function_calls_eh_return
= 1;
3043 #ifdef EH_RETURN_STACKADJ_RTX
3044 emit_move_insn (EH_RETURN_STACKADJ_RTX
, const0_rtx
);
3047 around_label
= gen_label_rtx ();
3048 emit_jump (around_label
);
3050 emit_label (cfun
->eh
->ehr_label
);
3051 clobber_return_register ();
3053 #ifdef EH_RETURN_STACKADJ_RTX
3054 emit_move_insn (EH_RETURN_STACKADJ_RTX
, cfun
->eh
->ehr_stackadj
);
3057 #ifdef HAVE_eh_return
3059 emit_insn (gen_eh_return (cfun
->eh
->ehr_handler
));
3063 #ifdef EH_RETURN_HANDLER_RTX
3064 emit_move_insn (EH_RETURN_HANDLER_RTX
, cfun
->eh
->ehr_handler
);
3066 error ("__builtin_eh_return not supported on this target");
3070 emit_label (around_label
);
3073 /* Convert a ptr_mode address ADDR_TREE to a Pmode address controlled by
3074 POINTERS_EXTEND_UNSIGNED and return it. */
3077 expand_builtin_extend_pointer (tree addr_tree
)
3079 rtx addr
= expand_expr (addr_tree
, NULL_RTX
, ptr_mode
, 0);
3082 #ifdef POINTERS_EXTEND_UNSIGNED
3083 extend
= POINTERS_EXTEND_UNSIGNED
;
3085 /* The previous EH code did an unsigned extend by default, so we do this also
3090 return convert_modes (word_mode
, ptr_mode
, addr
, extend
);
3093 /* In the following functions, we represent entries in the action table
3094 as 1-based indices. Special cases are:
3096 0: null action record, non-null landing pad; implies cleanups
3097 -1: null action record, null landing pad; implies no action
3098 -2: no call-site entry; implies must_not_throw
3099 -3: we have yet to process outer regions
3101 Further, no special cases apply to the "next" field of the record.
3102 For next, 0 means end of list. */
3104 struct action_record
3112 action_record_eq (const void *pentry
, const void *pdata
)
3114 const struct action_record
*entry
= (const struct action_record
*) pentry
;
3115 const struct action_record
*data
= (const struct action_record
*) pdata
;
3116 return entry
->filter
== data
->filter
&& entry
->next
== data
->next
;
3120 action_record_hash (const void *pentry
)
3122 const struct action_record
*entry
= (const struct action_record
*) pentry
;
3123 return entry
->next
* 1009 + entry
->filter
;
3127 add_action_record (htab_t ar_hash
, int filter
, int next
)
3129 struct action_record
**slot
, *new, tmp
;
3131 tmp
.filter
= filter
;
3133 slot
= (struct action_record
**) htab_find_slot (ar_hash
, &tmp
, INSERT
);
3135 if ((new = *slot
) == NULL
)
3137 new = xmalloc (sizeof (*new));
3138 new->offset
= VARRAY_ACTIVE_SIZE (cfun
->eh
->action_record_data
) + 1;
3139 new->filter
= filter
;
3143 /* The filter value goes in untouched. The link to the next
3144 record is a "self-relative" byte offset, or zero to indicate
3145 that there is no next record. So convert the absolute 1 based
3146 indices we've been carrying around into a displacement. */
3148 push_sleb128 (&cfun
->eh
->action_record_data
, filter
);
3150 next
-= VARRAY_ACTIVE_SIZE (cfun
->eh
->action_record_data
) + 1;
3151 push_sleb128 (&cfun
->eh
->action_record_data
, next
);
3158 collect_one_action_chain (htab_t ar_hash
, struct eh_region
*region
)
3160 struct eh_region
*c
;
3163 /* If we've reached the top of the region chain, then we have
3164 no actions, and require no landing pad. */
3168 switch (region
->type
)
3171 /* A cleanup adds a zero filter to the beginning of the chain, but
3172 there are special cases to look out for. If there are *only*
3173 cleanups along a path, then it compresses to a zero action.
3174 Further, if there are multiple cleanups along a path, we only
3175 need to represent one of them, as that is enough to trigger
3176 entry to the landing pad at runtime. */
3177 next
= collect_one_action_chain (ar_hash
, region
->outer
);
3180 for (c
= region
->outer
; c
; c
= c
->outer
)
3181 if (c
->type
== ERT_CLEANUP
)
3183 return add_action_record (ar_hash
, 0, next
);
3186 /* Process the associated catch regions in reverse order.
3187 If there's a catch-all handler, then we don't need to
3188 search outer regions. Use a magic -3 value to record
3189 that we haven't done the outer search. */
3191 for (c
= region
->u
.try.last_catch
; c
; c
= c
->u
.catch.prev_catch
)
3193 if (c
->u
.catch.type_list
== NULL
)
3195 /* Retrieve the filter from the head of the filter list
3196 where we have stored it (see assign_filter_values). */
3198 = TREE_INT_CST_LOW (TREE_VALUE (c
->u
.catch.filter_list
));
3200 next
= add_action_record (ar_hash
, filter
, 0);
3204 /* Once the outer search is done, trigger an action record for
3205 each filter we have. */
3210 next
= collect_one_action_chain (ar_hash
, region
->outer
);
3212 /* If there is no next action, terminate the chain. */
3215 /* If all outer actions are cleanups or must_not_throw,
3216 we'll have no action record for it, since we had wanted
3217 to encode these states in the call-site record directly.
3218 Add a cleanup action to the chain to catch these. */
3220 next
= add_action_record (ar_hash
, 0, 0);
3223 flt_node
= c
->u
.catch.filter_list
;
3224 for (; flt_node
; flt_node
= TREE_CHAIN (flt_node
))
3226 int filter
= TREE_INT_CST_LOW (TREE_VALUE (flt_node
));
3227 next
= add_action_record (ar_hash
, filter
, next
);
3233 case ERT_ALLOWED_EXCEPTIONS
:
3234 /* An exception specification adds its filter to the
3235 beginning of the chain. */
3236 next
= collect_one_action_chain (ar_hash
, region
->outer
);
3238 /* If there is no next action, terminate the chain. */
3241 /* If all outer actions are cleanups or must_not_throw,
3242 we'll have no action record for it, since we had wanted
3243 to encode these states in the call-site record directly.
3244 Add a cleanup action to the chain to catch these. */
3246 next
= add_action_record (ar_hash
, 0, 0);
3248 return add_action_record (ar_hash
, region
->u
.allowed
.filter
, next
);
3250 case ERT_MUST_NOT_THROW
:
3251 /* A must-not-throw region with no inner handlers or cleanups
3252 requires no call-site entry. Note that this differs from
3253 the no handler or cleanup case in that we do require an lsda
3254 to be generated. Return a magic -2 value to record this. */
3259 /* CATCH regions are handled in TRY above. THROW regions are
3260 for optimization information only and produce no output. */
3261 return collect_one_action_chain (ar_hash
, region
->outer
);
3269 add_call_site (rtx landing_pad
, int action
)
3271 struct call_site_record
*data
= cfun
->eh
->call_site_data
;
3272 int used
= cfun
->eh
->call_site_data_used
;
3273 int size
= cfun
->eh
->call_site_data_size
;
3277 size
= (size
? size
* 2 : 64);
3278 data
= ggc_realloc (data
, sizeof (*data
) * size
);
3279 cfun
->eh
->call_site_data
= data
;
3280 cfun
->eh
->call_site_data_size
= size
;
3283 data
[used
].landing_pad
= landing_pad
;
3284 data
[used
].action
= action
;
3286 cfun
->eh
->call_site_data_used
= used
+ 1;
3288 return used
+ call_site_base
;
3291 /* Turn REG_EH_REGION notes back into NOTE_INSN_EH_REGION notes.
3292 The new note numbers will not refer to region numbers, but
3293 instead to call site entries. */
3296 convert_to_eh_region_ranges (void)
3298 rtx insn
, iter
, note
;
3300 int last_action
= -3;
3301 rtx last_action_insn
= NULL_RTX
;
3302 rtx last_landing_pad
= NULL_RTX
;
3303 rtx first_no_action_insn
= NULL_RTX
;
3306 if (USING_SJLJ_EXCEPTIONS
|| cfun
->eh
->region_tree
== NULL
)
3309 VARRAY_UCHAR_INIT (cfun
->eh
->action_record_data
, 64, "action_record_data");
3311 ar_hash
= htab_create (31, action_record_hash
, action_record_eq
, free
);
3313 for (iter
= get_insns (); iter
; iter
= NEXT_INSN (iter
))
3316 struct eh_region
*region
;
3318 rtx this_landing_pad
;
3321 if (NONJUMP_INSN_P (insn
)
3322 && GET_CODE (PATTERN (insn
)) == SEQUENCE
)
3323 insn
= XVECEXP (PATTERN (insn
), 0, 0);
3325 note
= find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
);
3328 if (! (CALL_P (insn
)
3329 || (flag_non_call_exceptions
3330 && may_trap_p (PATTERN (insn
)))))
3337 if (INTVAL (XEXP (note
, 0)) <= 0)
3339 region
= cfun
->eh
->region_array
[INTVAL (XEXP (note
, 0))];
3340 this_action
= collect_one_action_chain (ar_hash
, region
);
3343 /* Existence of catch handlers, or must-not-throw regions
3344 implies that an lsda is needed (even if empty). */
3345 if (this_action
!= -1)
3346 cfun
->uses_eh_lsda
= 1;
3348 /* Delay creation of region notes for no-action regions
3349 until we're sure that an lsda will be required. */
3350 else if (last_action
== -3)
3352 first_no_action_insn
= iter
;
3356 /* Cleanups and handlers may share action chains but not
3357 landing pads. Collect the landing pad for this region. */
3358 if (this_action
>= 0)
3360 struct eh_region
*o
;
3361 for (o
= region
; ! o
->landing_pad
; o
= o
->outer
)
3363 this_landing_pad
= o
->landing_pad
;
3366 this_landing_pad
= NULL_RTX
;
3368 /* Differing actions or landing pads implies a change in call-site
3369 info, which implies some EH_REGION note should be emitted. */
3370 if (last_action
!= this_action
3371 || last_landing_pad
!= this_landing_pad
)
3373 /* If we'd not seen a previous action (-3) or the previous
3374 action was must-not-throw (-2), then we do not need an
3376 if (last_action
>= -1)
3378 /* If we delayed the creation of the begin, do it now. */
3379 if (first_no_action_insn
)
3381 call_site
= add_call_site (NULL_RTX
, 0);
3382 note
= emit_note_before (NOTE_INSN_EH_REGION_BEG
,
3383 first_no_action_insn
);
3384 NOTE_EH_HANDLER (note
) = call_site
;
3385 first_no_action_insn
= NULL_RTX
;
3388 note
= emit_note_after (NOTE_INSN_EH_REGION_END
,
3390 NOTE_EH_HANDLER (note
) = call_site
;
3393 /* If the new action is must-not-throw, then no region notes
3395 if (this_action
>= -1)
3397 call_site
= add_call_site (this_landing_pad
,
3398 this_action
< 0 ? 0 : this_action
);
3399 note
= emit_note_before (NOTE_INSN_EH_REGION_BEG
, iter
);
3400 NOTE_EH_HANDLER (note
) = call_site
;
3403 last_action
= this_action
;
3404 last_landing_pad
= this_landing_pad
;
3406 last_action_insn
= iter
;
3409 if (last_action
>= -1 && ! first_no_action_insn
)
3411 note
= emit_note_after (NOTE_INSN_EH_REGION_END
, last_action_insn
);
3412 NOTE_EH_HANDLER (note
) = call_site
;
3415 htab_delete (ar_hash
);
3420 push_uleb128 (varray_type
*data_area
, unsigned int value
)
3424 unsigned char byte
= value
& 0x7f;
3428 VARRAY_PUSH_UCHAR (*data_area
, byte
);
3434 push_sleb128 (varray_type
*data_area
, int value
)
3441 byte
= value
& 0x7f;
3443 more
= ! ((value
== 0 && (byte
& 0x40) == 0)
3444 || (value
== -1 && (byte
& 0x40) != 0));
3447 VARRAY_PUSH_UCHAR (*data_area
, byte
);
3453 #ifndef HAVE_AS_LEB128
3455 dw2_size_of_call_site_table (void)
3457 int n
= cfun
->eh
->call_site_data_used
;
3458 int size
= n
* (4 + 4 + 4);
3461 for (i
= 0; i
< n
; ++i
)
3463 struct call_site_record
*cs
= &cfun
->eh
->call_site_data
[i
];
3464 size
+= size_of_uleb128 (cs
->action
);
3471 sjlj_size_of_call_site_table (void)
3473 int n
= cfun
->eh
->call_site_data_used
;
3477 for (i
= 0; i
< n
; ++i
)
3479 struct call_site_record
*cs
= &cfun
->eh
->call_site_data
[i
];
3480 size
+= size_of_uleb128 (INTVAL (cs
->landing_pad
));
3481 size
+= size_of_uleb128 (cs
->action
);
3489 dw2_output_call_site_table (void)
3491 const char *const function_start_lab
3492 = IDENTIFIER_POINTER (current_function_func_begin_label
);
3493 int n
= cfun
->eh
->call_site_data_used
;
3496 for (i
= 0; i
< n
; ++i
)
3498 struct call_site_record
*cs
= &cfun
->eh
->call_site_data
[i
];
3499 char reg_start_lab
[32];
3500 char reg_end_lab
[32];
3501 char landing_pad_lab
[32];
3503 ASM_GENERATE_INTERNAL_LABEL (reg_start_lab
, "LEHB", call_site_base
+ i
);
3504 ASM_GENERATE_INTERNAL_LABEL (reg_end_lab
, "LEHE", call_site_base
+ i
);
3506 if (cs
->landing_pad
)
3507 ASM_GENERATE_INTERNAL_LABEL (landing_pad_lab
, "L",
3508 CODE_LABEL_NUMBER (cs
->landing_pad
));
3510 /* ??? Perhaps use insn length scaling if the assembler supports
3511 generic arithmetic. */
3512 /* ??? Perhaps use attr_length to choose data1 or data2 instead of
3513 data4 if the function is small enough. */
3514 #ifdef HAVE_AS_LEB128
3515 dw2_asm_output_delta_uleb128 (reg_start_lab
, function_start_lab
,
3516 "region %d start", i
);
3517 dw2_asm_output_delta_uleb128 (reg_end_lab
, reg_start_lab
,
3519 if (cs
->landing_pad
)
3520 dw2_asm_output_delta_uleb128 (landing_pad_lab
, function_start_lab
,
3523 dw2_asm_output_data_uleb128 (0, "landing pad");
3525 dw2_asm_output_delta (4, reg_start_lab
, function_start_lab
,
3526 "region %d start", i
);
3527 dw2_asm_output_delta (4, reg_end_lab
, reg_start_lab
, "length");
3528 if (cs
->landing_pad
)
3529 dw2_asm_output_delta (4, landing_pad_lab
, function_start_lab
,
3532 dw2_asm_output_data (4, 0, "landing pad");
3534 dw2_asm_output_data_uleb128 (cs
->action
, "action");
3537 call_site_base
+= n
;
3541 sjlj_output_call_site_table (void)
3543 int n
= cfun
->eh
->call_site_data_used
;
3546 for (i
= 0; i
< n
; ++i
)
3548 struct call_site_record
*cs
= &cfun
->eh
->call_site_data
[i
];
3550 dw2_asm_output_data_uleb128 (INTVAL (cs
->landing_pad
),
3551 "region %d landing pad", i
);
3552 dw2_asm_output_data_uleb128 (cs
->action
, "action");
3555 call_site_base
+= n
;
3558 /* Tell assembler to switch to the section for the exception handling
3562 default_exception_section (void)
3564 if (targetm
.have_named_sections
)
3567 #ifdef HAVE_LD_RO_RW_SECTION_MIXING
3568 int tt_format
= ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3571 || ((tt_format
& 0x70) != DW_EH_PE_absptr
3572 && (tt_format
& 0x70) != DW_EH_PE_aligned
))
3573 ? 0 : SECTION_WRITE
;
3575 flags
= SECTION_WRITE
;
3577 named_section_flags (".gcc_except_table", flags
);
3582 readonly_data_section ();
3586 output_function_exception_table (void)
3588 int tt_format
, cs_format
, lp_format
, i
, n
;
3589 #ifdef HAVE_AS_LEB128
3590 char ttype_label
[32];
3591 char cs_after_size_label
[32];
3592 char cs_end_label
[32];
3597 int tt_format_size
= 0;
3599 /* Not all functions need anything. */
3600 if (! cfun
->uses_eh_lsda
)
3603 #ifdef TARGET_UNWIND_INFO
3604 /* TODO: Move this into target file. */
3605 fputs ("\t.personality\t", asm_out_file
);
3606 output_addr_const (asm_out_file
, eh_personality_libfunc
);
3607 fputs ("\n\t.handlerdata\n", asm_out_file
);
3608 /* Note that varasm still thinks we're in the function's code section.
3609 The ".endp" directive that will immediately follow will take us back. */
3611 targetm
.asm_out
.exception_section ();
3614 have_tt_data
= (VARRAY_ACTIVE_SIZE (cfun
->eh
->ttype_data
) > 0
3615 || VARRAY_ACTIVE_SIZE (cfun
->eh
->ehspec_data
) > 0);
3617 /* Indicate the format of the @TType entries. */
3619 tt_format
= DW_EH_PE_omit
;
3622 tt_format
= ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3623 #ifdef HAVE_AS_LEB128
3624 ASM_GENERATE_INTERNAL_LABEL (ttype_label
, "LLSDATT",
3625 current_function_funcdef_no
);
3627 tt_format_size
= size_of_encoded_value (tt_format
);
3629 assemble_align (tt_format_size
* BITS_PER_UNIT
);
3632 targetm
.asm_out
.internal_label (asm_out_file
, "LLSDA",
3633 current_function_funcdef_no
);
3635 /* The LSDA header. */
3637 /* Indicate the format of the landing pad start pointer. An omitted
3638 field implies @LPStart == @Start. */
3639 /* Currently we always put @LPStart == @Start. This field would
3640 be most useful in moving the landing pads completely out of
3641 line to another section, but it could also be used to minimize
3642 the size of uleb128 landing pad offsets. */
3643 lp_format
= DW_EH_PE_omit
;
3644 dw2_asm_output_data (1, lp_format
, "@LPStart format (%s)",
3645 eh_data_format_name (lp_format
));
3647 /* @LPStart pointer would go here. */
3649 dw2_asm_output_data (1, tt_format
, "@TType format (%s)",
3650 eh_data_format_name (tt_format
));
3652 #ifndef HAVE_AS_LEB128
3653 if (USING_SJLJ_EXCEPTIONS
)
3654 call_site_len
= sjlj_size_of_call_site_table ();
3656 call_site_len
= dw2_size_of_call_site_table ();
3659 /* A pc-relative 4-byte displacement to the @TType data. */
3662 #ifdef HAVE_AS_LEB128
3663 char ttype_after_disp_label
[32];
3664 ASM_GENERATE_INTERNAL_LABEL (ttype_after_disp_label
, "LLSDATTD",
3665 current_function_funcdef_no
);
3666 dw2_asm_output_delta_uleb128 (ttype_label
, ttype_after_disp_label
,
3667 "@TType base offset");
3668 ASM_OUTPUT_LABEL (asm_out_file
, ttype_after_disp_label
);
3670 /* Ug. Alignment queers things. */
3671 unsigned int before_disp
, after_disp
, last_disp
, disp
;
3673 before_disp
= 1 + 1;
3674 after_disp
= (1 + size_of_uleb128 (call_site_len
)
3676 + VARRAY_ACTIVE_SIZE (cfun
->eh
->action_record_data
)
3677 + (VARRAY_ACTIVE_SIZE (cfun
->eh
->ttype_data
)
3683 unsigned int disp_size
, pad
;
3686 disp_size
= size_of_uleb128 (disp
);
3687 pad
= before_disp
+ disp_size
+ after_disp
;
3688 if (pad
% tt_format_size
)
3689 pad
= tt_format_size
- (pad
% tt_format_size
);
3692 disp
= after_disp
+ pad
;
3694 while (disp
!= last_disp
);
3696 dw2_asm_output_data_uleb128 (disp
, "@TType base offset");
3700 /* Indicate the format of the call-site offsets. */
3701 #ifdef HAVE_AS_LEB128
3702 cs_format
= DW_EH_PE_uleb128
;
3704 cs_format
= DW_EH_PE_udata4
;
3706 dw2_asm_output_data (1, cs_format
, "call-site format (%s)",
3707 eh_data_format_name (cs_format
));
3709 #ifdef HAVE_AS_LEB128
3710 ASM_GENERATE_INTERNAL_LABEL (cs_after_size_label
, "LLSDACSB",
3711 current_function_funcdef_no
);
3712 ASM_GENERATE_INTERNAL_LABEL (cs_end_label
, "LLSDACSE",
3713 current_function_funcdef_no
);
3714 dw2_asm_output_delta_uleb128 (cs_end_label
, cs_after_size_label
,
3715 "Call-site table length");
3716 ASM_OUTPUT_LABEL (asm_out_file
, cs_after_size_label
);
3717 if (USING_SJLJ_EXCEPTIONS
)
3718 sjlj_output_call_site_table ();
3720 dw2_output_call_site_table ();
3721 ASM_OUTPUT_LABEL (asm_out_file
, cs_end_label
);
3723 dw2_asm_output_data_uleb128 (call_site_len
,"Call-site table length");
3724 if (USING_SJLJ_EXCEPTIONS
)
3725 sjlj_output_call_site_table ();
3727 dw2_output_call_site_table ();
3730 /* ??? Decode and interpret the data for flag_debug_asm. */
3731 n
= VARRAY_ACTIVE_SIZE (cfun
->eh
->action_record_data
);
3732 for (i
= 0; i
< n
; ++i
)
3733 dw2_asm_output_data (1, VARRAY_UCHAR (cfun
->eh
->action_record_data
, i
),
3734 (i
? NULL
: "Action record table"));
3737 assemble_align (tt_format_size
* BITS_PER_UNIT
);
3739 i
= VARRAY_ACTIVE_SIZE (cfun
->eh
->ttype_data
);
3742 tree type
= VARRAY_TREE (cfun
->eh
->ttype_data
, i
);
3745 if (type
== NULL_TREE
)
3749 struct cgraph_varpool_node
*node
;
3751 type
= lookup_type_for_runtime (type
);
3752 value
= expand_expr (type
, NULL_RTX
, VOIDmode
, EXPAND_INITIALIZER
);
3754 /* Let cgraph know that the rtti decl is used. Not all of the
3755 paths below go through assemble_integer, which would take
3756 care of this for us. */
3758 if (TREE_CODE (type
) == ADDR_EXPR
)
3760 type
= TREE_OPERAND (type
, 0);
3761 if (TREE_CODE (type
) == VAR_DECL
)
3763 node
= cgraph_varpool_node (type
);
3765 cgraph_varpool_mark_needed_node (node
);
3768 else if (TREE_CODE (type
) != INTEGER_CST
)
3772 if (tt_format
== DW_EH_PE_absptr
|| tt_format
== DW_EH_PE_aligned
)
3773 assemble_integer (value
, tt_format_size
,
3774 tt_format_size
* BITS_PER_UNIT
, 1);
3776 dw2_asm_output_encoded_addr_rtx (tt_format
, value
, NULL
);
3779 #ifdef HAVE_AS_LEB128
3781 ASM_OUTPUT_LABEL (asm_out_file
, ttype_label
);
3784 /* ??? Decode and interpret the data for flag_debug_asm. */
3785 n
= VARRAY_ACTIVE_SIZE (cfun
->eh
->ehspec_data
);
3786 for (i
= 0; i
< n
; ++i
)
3787 dw2_asm_output_data (1, VARRAY_UCHAR (cfun
->eh
->ehspec_data
, i
),
3788 (i
? NULL
: "Exception specification table"));
3790 function_section (current_function_decl
);
3793 #include "gt-except.h"