2007-06-05 H.J. Lu <hongjiu.lu@intel.com>
[official-gcc.git] / gcc / except.c
blobb360ae4ad1da7f63ed76ac99b5a76a3efe1dc6fd
1 /* Implements exception handling.
2 Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 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
11 version.
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
16 for more details.
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, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
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. ] */
50 #include "config.h"
51 #include "system.h"
52 #include "coretypes.h"
53 #include "tm.h"
54 #include "rtl.h"
55 #include "tree.h"
56 #include "flags.h"
57 #include "function.h"
58 #include "expr.h"
59 #include "libfuncs.h"
60 #include "insn-config.h"
61 #include "except.h"
62 #include "integrate.h"
63 #include "hard-reg-set.h"
64 #include "basic-block.h"
65 #include "output.h"
66 #include "dwarf2asm.h"
67 #include "dwarf2out.h"
68 #include "dwarf2.h"
69 #include "toplev.h"
70 #include "hashtab.h"
71 #include "intl.h"
72 #include "ggc.h"
73 #include "tm_p.h"
74 #include "target.h"
75 #include "langhooks.h"
76 #include "cgraph.h"
77 #include "diagnostic.h"
78 #include "tree-pass.h"
79 #include "timevar.h"
81 /* Provide defaults for stuff that may not be defined when using
82 sjlj exceptions. */
83 #ifndef EH_RETURN_DATA_REGNO
84 #define EH_RETURN_DATA_REGNO(N) INVALID_REGNUM
85 #endif
88 /* Protect cleanup actions with must-not-throw regions, with a call
89 to the given failure handler. */
90 tree (*lang_protect_cleanup_actions) (void);
92 /* Return true if type A catches type B. */
93 int (*lang_eh_type_covers) (tree a, tree b);
95 /* Map a type to a runtime object to match type. */
96 tree (*lang_eh_runtime_type) (tree);
98 /* A hash table of label to region number. */
100 struct ehl_map_entry GTY(())
102 rtx label;
103 struct eh_region *region;
106 static GTY(()) int call_site_base;
107 static GTY ((param_is (union tree_node)))
108 htab_t type_to_runtime_map;
110 /* Describe the SjLj_Function_Context structure. */
111 static GTY(()) tree sjlj_fc_type_node;
112 static int sjlj_fc_call_site_ofs;
113 static int sjlj_fc_data_ofs;
114 static int sjlj_fc_personality_ofs;
115 static int sjlj_fc_lsda_ofs;
116 static int sjlj_fc_jbuf_ofs;
118 /* Describes one exception region. */
119 struct eh_region GTY(())
121 /* The immediately surrounding region. */
122 struct eh_region *outer;
124 /* The list of immediately contained regions. */
125 struct eh_region *inner;
126 struct eh_region *next_peer;
128 /* An identifier for this region. */
129 int region_number;
131 /* When a region is deleted, its parents inherit the REG_EH_REGION
132 numbers already assigned. */
133 bitmap aka;
135 /* Each region does exactly one thing. */
136 enum eh_region_type
138 ERT_UNKNOWN = 0,
139 ERT_CLEANUP,
140 ERT_TRY,
141 ERT_CATCH,
142 ERT_ALLOWED_EXCEPTIONS,
143 ERT_MUST_NOT_THROW,
144 ERT_THROW
145 } type;
147 /* Holds the action to perform based on the preceding type. */
148 union eh_region_u {
149 /* A list of catch blocks, a surrounding try block,
150 and the label for continuing after a catch. */
151 struct eh_region_u_try {
152 struct eh_region *catch;
153 struct eh_region *last_catch;
154 } 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;
161 tree type_list;
162 tree filter_list;
163 } GTY ((tag ("ERT_CATCH"))) catch;
165 /* A tree_list of allowed types. */
166 struct eh_region_u_allowed {
167 tree type_list;
168 int filter;
169 } GTY ((tag ("ERT_ALLOWED_EXCEPTIONS"))) allowed;
171 /* The type given by a call to "throw foo();", or discovered
172 for a throw. */
173 struct eh_region_u_throw {
174 tree type;
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 {
180 struct eh_region *prev_try;
181 } GTY ((tag ("ERT_CLEANUP"))) cleanup;
182 } GTY ((desc ("%0.type"))) u;
184 /* Entry point for this region's handler before landing pads are built. */
185 rtx label;
186 tree tree_label;
188 /* Entry point for this region's handler from the runtime eh library. */
189 rtx landing_pad;
191 /* Entry point for this region's handler from an inner region. */
192 rtx post_landing_pad;
194 /* The RESX insn for handing off control to the next outermost handler,
195 if appropriate. */
196 rtx resume;
198 /* True if something in this region may throw. */
199 unsigned may_contain_throw : 1;
202 typedef struct eh_region *eh_region;
204 struct call_site_record GTY(())
206 rtx landing_pad;
207 int action;
210 DEF_VEC_P(eh_region);
211 DEF_VEC_ALLOC_P(eh_region, gc);
213 /* Used to save exception status for each function. */
214 struct eh_status GTY(())
216 /* The tree of all regions for this function. */
217 struct eh_region *region_tree;
219 /* The same information as an indexable array. */
220 VEC(eh_region,gc) *region_array;
222 /* The most recently open region. */
223 struct eh_region *cur_region;
225 /* This is the region for which we are processing catch blocks. */
226 struct eh_region *try_region;
228 rtx filter;
229 rtx exc_ptr;
231 int built_landing_pads;
232 int last_region_number;
234 VEC(tree,gc) *ttype_data;
235 varray_type ehspec_data;
236 varray_type action_record_data;
238 htab_t GTY ((param_is (struct ehl_map_entry))) exception_handler_label_map;
240 struct call_site_record * GTY ((length ("%h.call_site_data_used")))
241 call_site_data;
242 int call_site_data_used;
243 int call_site_data_size;
245 rtx ehr_stackadj;
246 rtx ehr_handler;
247 rtx ehr_label;
249 rtx sjlj_fc;
250 rtx sjlj_exit_after;
252 htab_t GTY((param_is (struct throw_stmt_node))) throw_stmt_table;
255 static int t2r_eq (const void *, const void *);
256 static hashval_t t2r_hash (const void *);
257 static void add_type_for_runtime (tree);
258 static tree lookup_type_for_runtime (tree);
260 static void remove_unreachable_regions (rtx);
262 static int ttypes_filter_eq (const void *, const void *);
263 static hashval_t ttypes_filter_hash (const void *);
264 static int ehspec_filter_eq (const void *, const void *);
265 static hashval_t ehspec_filter_hash (const void *);
266 static int add_ttypes_entry (htab_t, tree);
267 static int add_ehspec_entry (htab_t, htab_t, tree);
268 static void assign_filter_values (void);
269 static void build_post_landing_pads (void);
270 static void connect_post_landing_pads (void);
271 static void dw2_build_landing_pads (void);
273 struct sjlj_lp_info;
274 static bool sjlj_find_directly_reachable_regions (struct sjlj_lp_info *);
275 static void sjlj_assign_call_site_values (rtx, struct sjlj_lp_info *);
276 static void sjlj_mark_call_sites (struct sjlj_lp_info *);
277 static void sjlj_emit_function_enter (rtx);
278 static void sjlj_emit_function_exit (void);
279 static void sjlj_emit_dispatch_table (rtx, struct sjlj_lp_info *);
280 static void sjlj_build_landing_pads (void);
282 static hashval_t ehl_hash (const void *);
283 static int ehl_eq (const void *, const void *);
284 static void add_ehl_entry (rtx, struct eh_region *);
285 static void remove_exception_handler_label (rtx);
286 static void remove_eh_handler (struct eh_region *);
287 static int for_each_eh_label_1 (void **, void *);
289 /* The return value of reachable_next_level. */
290 enum reachable_code
292 /* The given exception is not processed by the given region. */
293 RNL_NOT_CAUGHT,
294 /* The given exception may need processing by the given region. */
295 RNL_MAYBE_CAUGHT,
296 /* The given exception is completely processed by the given region. */
297 RNL_CAUGHT,
298 /* The given exception is completely processed by the runtime. */
299 RNL_BLOCKED
302 struct reachable_info;
303 static enum reachable_code reachable_next_level (struct eh_region *, tree,
304 struct reachable_info *);
306 static int action_record_eq (const void *, const void *);
307 static hashval_t action_record_hash (const void *);
308 static int add_action_record (htab_t, int, int);
309 static int collect_one_action_chain (htab_t, struct eh_region *);
310 static int add_call_site (rtx, int);
312 static void push_uleb128 (varray_type *, unsigned int);
313 static void push_sleb128 (varray_type *, int);
314 #ifndef HAVE_AS_LEB128
315 static int dw2_size_of_call_site_table (void);
316 static int sjlj_size_of_call_site_table (void);
317 #endif
318 static void dw2_output_call_site_table (void);
319 static void sjlj_output_call_site_table (void);
322 /* Routine to see if exception handling is turned on.
323 DO_WARN is nonzero if we want to inform the user that exception
324 handling is turned off.
326 This is used to ensure that -fexceptions has been specified if the
327 compiler tries to use any exception-specific functions. */
330 doing_eh (int do_warn)
332 if (! flag_exceptions)
334 static int warned = 0;
335 if (! warned && do_warn)
337 error ("exception handling disabled, use -fexceptions to enable");
338 warned = 1;
340 return 0;
342 return 1;
346 void
347 init_eh (void)
349 if (! flag_exceptions)
350 return;
352 type_to_runtime_map = htab_create_ggc (31, t2r_hash, t2r_eq, NULL);
354 /* Create the SjLj_Function_Context structure. This should match
355 the definition in unwind-sjlj.c. */
356 if (USING_SJLJ_EXCEPTIONS)
358 tree f_jbuf, f_per, f_lsda, f_prev, f_cs, f_data, tmp;
360 sjlj_fc_type_node = lang_hooks.types.make_type (RECORD_TYPE);
362 f_prev = build_decl (FIELD_DECL, get_identifier ("__prev"),
363 build_pointer_type (sjlj_fc_type_node));
364 DECL_FIELD_CONTEXT (f_prev) = sjlj_fc_type_node;
366 f_cs = build_decl (FIELD_DECL, get_identifier ("__call_site"),
367 integer_type_node);
368 DECL_FIELD_CONTEXT (f_cs) = sjlj_fc_type_node;
370 tmp = build_index_type (build_int_cst (NULL_TREE, 4 - 1));
371 tmp = build_array_type (lang_hooks.types.type_for_mode (word_mode, 1),
372 tmp);
373 f_data = build_decl (FIELD_DECL, get_identifier ("__data"), tmp);
374 DECL_FIELD_CONTEXT (f_data) = sjlj_fc_type_node;
376 f_per = build_decl (FIELD_DECL, get_identifier ("__personality"),
377 ptr_type_node);
378 DECL_FIELD_CONTEXT (f_per) = sjlj_fc_type_node;
380 f_lsda = build_decl (FIELD_DECL, get_identifier ("__lsda"),
381 ptr_type_node);
382 DECL_FIELD_CONTEXT (f_lsda) = sjlj_fc_type_node;
384 #ifdef DONT_USE_BUILTIN_SETJMP
385 #ifdef JMP_BUF_SIZE
386 tmp = build_int_cst (NULL_TREE, JMP_BUF_SIZE - 1);
387 #else
388 /* Should be large enough for most systems, if it is not,
389 JMP_BUF_SIZE should be defined with the proper value. It will
390 also tend to be larger than necessary for most systems, a more
391 optimal port will define JMP_BUF_SIZE. */
392 tmp = build_int_cst (NULL_TREE, FIRST_PSEUDO_REGISTER + 2 - 1);
393 #endif
394 #else
395 /* builtin_setjmp takes a pointer to 5 words. */
396 tmp = build_int_cst (NULL_TREE, 5 * BITS_PER_WORD / POINTER_SIZE - 1);
397 #endif
398 tmp = build_index_type (tmp);
399 tmp = build_array_type (ptr_type_node, tmp);
400 f_jbuf = build_decl (FIELD_DECL, get_identifier ("__jbuf"), tmp);
401 #ifdef DONT_USE_BUILTIN_SETJMP
402 /* We don't know what the alignment requirements of the
403 runtime's jmp_buf has. Overestimate. */
404 DECL_ALIGN (f_jbuf) = BIGGEST_ALIGNMENT;
405 DECL_USER_ALIGN (f_jbuf) = 1;
406 #endif
407 DECL_FIELD_CONTEXT (f_jbuf) = sjlj_fc_type_node;
409 TYPE_FIELDS (sjlj_fc_type_node) = f_prev;
410 TREE_CHAIN (f_prev) = f_cs;
411 TREE_CHAIN (f_cs) = f_data;
412 TREE_CHAIN (f_data) = f_per;
413 TREE_CHAIN (f_per) = f_lsda;
414 TREE_CHAIN (f_lsda) = f_jbuf;
416 layout_type (sjlj_fc_type_node);
418 /* Cache the interesting field offsets so that we have
419 easy access from rtl. */
420 sjlj_fc_call_site_ofs
421 = (tree_low_cst (DECL_FIELD_OFFSET (f_cs), 1)
422 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_cs), 1) / BITS_PER_UNIT);
423 sjlj_fc_data_ofs
424 = (tree_low_cst (DECL_FIELD_OFFSET (f_data), 1)
425 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_data), 1) / BITS_PER_UNIT);
426 sjlj_fc_personality_ofs
427 = (tree_low_cst (DECL_FIELD_OFFSET (f_per), 1)
428 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_per), 1) / BITS_PER_UNIT);
429 sjlj_fc_lsda_ofs
430 = (tree_low_cst (DECL_FIELD_OFFSET (f_lsda), 1)
431 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_lsda), 1) / BITS_PER_UNIT);
432 sjlj_fc_jbuf_ofs
433 = (tree_low_cst (DECL_FIELD_OFFSET (f_jbuf), 1)
434 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_jbuf), 1) / BITS_PER_UNIT);
438 void
439 init_eh_for_function (void)
441 cfun->eh = ggc_alloc_cleared (sizeof (struct eh_status));
444 /* Routines to generate the exception tree somewhat directly.
445 These are used from tree-eh.c when processing exception related
446 nodes during tree optimization. */
448 static struct eh_region *
449 gen_eh_region (enum eh_region_type type, struct eh_region *outer)
451 struct eh_region *new;
453 #ifdef ENABLE_CHECKING
454 gcc_assert (doing_eh (0));
455 #endif
457 /* Insert a new blank region as a leaf in the tree. */
458 new = ggc_alloc_cleared (sizeof (*new));
459 new->type = type;
460 new->outer = outer;
461 if (outer)
463 new->next_peer = outer->inner;
464 outer->inner = new;
466 else
468 new->next_peer = cfun->eh->region_tree;
469 cfun->eh->region_tree = new;
472 new->region_number = ++cfun->eh->last_region_number;
474 return new;
477 struct eh_region *
478 gen_eh_region_cleanup (struct eh_region *outer, struct eh_region *prev_try)
480 struct eh_region *cleanup = gen_eh_region (ERT_CLEANUP, outer);
481 cleanup->u.cleanup.prev_try = prev_try;
482 return cleanup;
485 struct eh_region *
486 gen_eh_region_try (struct eh_region *outer)
488 return gen_eh_region (ERT_TRY, outer);
491 struct eh_region *
492 gen_eh_region_catch (struct eh_region *t, tree type_or_list)
494 struct eh_region *c, *l;
495 tree type_list, type_node;
497 /* Ensure to always end up with a type list to normalize further
498 processing, then register each type against the runtime types map. */
499 type_list = type_or_list;
500 if (type_or_list)
502 if (TREE_CODE (type_or_list) != TREE_LIST)
503 type_list = tree_cons (NULL_TREE, type_or_list, NULL_TREE);
505 type_node = type_list;
506 for (; type_node; type_node = TREE_CHAIN (type_node))
507 add_type_for_runtime (TREE_VALUE (type_node));
510 c = gen_eh_region (ERT_CATCH, t->outer);
511 c->u.catch.type_list = type_list;
512 l = t->u.try.last_catch;
513 c->u.catch.prev_catch = l;
514 if (l)
515 l->u.catch.next_catch = c;
516 else
517 t->u.try.catch = c;
518 t->u.try.last_catch = c;
520 return c;
523 struct eh_region *
524 gen_eh_region_allowed (struct eh_region *outer, tree allowed)
526 struct eh_region *region = gen_eh_region (ERT_ALLOWED_EXCEPTIONS, outer);
527 region->u.allowed.type_list = allowed;
529 for (; allowed ; allowed = TREE_CHAIN (allowed))
530 add_type_for_runtime (TREE_VALUE (allowed));
532 return region;
535 struct eh_region *
536 gen_eh_region_must_not_throw (struct eh_region *outer)
538 return gen_eh_region (ERT_MUST_NOT_THROW, outer);
542 get_eh_region_number (struct eh_region *region)
544 return region->region_number;
547 bool
548 get_eh_region_may_contain_throw (struct eh_region *region)
550 return region->may_contain_throw;
553 tree
554 get_eh_region_tree_label (struct eh_region *region)
556 return region->tree_label;
559 void
560 set_eh_region_tree_label (struct eh_region *region, tree lab)
562 region->tree_label = lab;
565 void
566 expand_resx_expr (tree exp)
568 int region_nr = TREE_INT_CST_LOW (TREE_OPERAND (exp, 0));
569 struct eh_region *reg = VEC_index (eh_region,
570 cfun->eh->region_array, region_nr);
572 gcc_assert (!reg->resume);
573 reg->resume = emit_jump_insn (gen_rtx_RESX (VOIDmode, region_nr));
574 emit_barrier ();
577 /* Note that the current EH region (if any) may contain a throw, or a
578 call to a function which itself may contain a throw. */
580 void
581 note_eh_region_may_contain_throw (struct eh_region *region)
583 while (region && !region->may_contain_throw)
585 region->may_contain_throw = 1;
586 region = region->outer;
590 void
591 note_current_region_may_contain_throw (void)
593 note_eh_region_may_contain_throw (cfun->eh->cur_region);
597 /* Return an rtl expression for a pointer to the exception object
598 within a handler. */
601 get_exception_pointer (struct function *fun)
603 rtx exc_ptr = fun->eh->exc_ptr;
604 if (fun == cfun && ! exc_ptr)
606 exc_ptr = gen_reg_rtx (ptr_mode);
607 fun->eh->exc_ptr = exc_ptr;
609 return exc_ptr;
612 /* Return an rtl expression for the exception dispatch filter
613 within a handler. */
616 get_exception_filter (struct function *fun)
618 rtx filter = fun->eh->filter;
619 if (fun == cfun && ! filter)
621 filter = gen_reg_rtx (targetm.eh_return_filter_mode ());
622 fun->eh->filter = filter;
624 return filter;
627 /* This section is for the exception handling specific optimization pass. */
629 /* Random access the exception region tree. */
631 void
632 collect_eh_region_array (void)
634 struct eh_region *i;
636 i = cfun->eh->region_tree;
637 if (! i)
638 return;
640 VEC_safe_grow (eh_region, gc, cfun->eh->region_array,
641 cfun->eh->last_region_number + 1);
642 VEC_replace (eh_region, cfun->eh->region_array, 0, 0);
644 while (1)
646 VEC_replace (eh_region, cfun->eh->region_array, i->region_number, i);
648 /* If there are sub-regions, process them. */
649 if (i->inner)
650 i = i->inner;
651 /* If there are peers, process them. */
652 else if (i->next_peer)
653 i = i->next_peer;
654 /* Otherwise, step back up the tree to the next peer. */
655 else
657 do {
658 i = i->outer;
659 if (i == NULL)
660 return;
661 } while (i->next_peer == NULL);
662 i = i->next_peer;
667 /* Remove all regions whose labels are not reachable from insns. */
669 static void
670 remove_unreachable_regions (rtx insns)
672 int i, *uid_region_num;
673 bool *reachable;
674 struct eh_region *r;
675 rtx insn;
677 uid_region_num = xcalloc (get_max_uid (), sizeof(int));
678 reachable = xcalloc (cfun->eh->last_region_number + 1, sizeof(bool));
680 for (i = cfun->eh->last_region_number; i > 0; --i)
682 r = VEC_index (eh_region, cfun->eh->region_array, i);
683 if (!r || r->region_number != i)
684 continue;
686 if (r->resume)
688 gcc_assert (!uid_region_num[INSN_UID (r->resume)]);
689 uid_region_num[INSN_UID (r->resume)] = i;
691 if (r->label)
693 gcc_assert (!uid_region_num[INSN_UID (r->label)]);
694 uid_region_num[INSN_UID (r->label)] = i;
698 for (insn = insns; insn; insn = NEXT_INSN (insn))
699 reachable[uid_region_num[INSN_UID (insn)]] = true;
701 for (i = cfun->eh->last_region_number; i > 0; --i)
703 r = VEC_index (eh_region, cfun->eh->region_array, i);
704 if (r && r->region_number == i && !reachable[i])
706 bool kill_it = true;
707 switch (r->type)
709 case ERT_THROW:
710 /* Don't remove ERT_THROW regions if their outer region
711 is reachable. */
712 if (r->outer && reachable[r->outer->region_number])
713 kill_it = false;
714 break;
716 case ERT_MUST_NOT_THROW:
717 /* MUST_NOT_THROW regions are implementable solely in the
718 runtime, but their existence continues to affect calls
719 within that region. Never delete them here. */
720 kill_it = false;
721 break;
723 case ERT_TRY:
725 /* TRY regions are reachable if any of its CATCH regions
726 are reachable. */
727 struct eh_region *c;
728 for (c = r->u.try.catch; c ; c = c->u.catch.next_catch)
729 if (reachable[c->region_number])
731 kill_it = false;
732 break;
734 break;
737 default:
738 break;
741 if (kill_it)
742 remove_eh_handler (r);
746 free (reachable);
747 free (uid_region_num);
750 /* Set up EH labels for RTL. */
752 void
753 convert_from_eh_region_ranges (void)
755 rtx insns = get_insns ();
756 int i, n = cfun->eh->last_region_number;
758 /* Most of the work is already done at the tree level. All we need to
759 do is collect the rtl labels that correspond to the tree labels that
760 collect the rtl labels that correspond to the tree labels
761 we allocated earlier. */
762 for (i = 1; i <= n; ++i)
764 struct eh_region *region;
766 region = VEC_index (eh_region, cfun->eh->region_array, i);
767 if (region && region->tree_label)
768 region->label = DECL_RTL_IF_SET (region->tree_label);
771 remove_unreachable_regions (insns);
774 static void
775 add_ehl_entry (rtx label, struct eh_region *region)
777 struct ehl_map_entry **slot, *entry;
779 LABEL_PRESERVE_P (label) = 1;
781 entry = ggc_alloc (sizeof (*entry));
782 entry->label = label;
783 entry->region = region;
785 slot = (struct ehl_map_entry **)
786 htab_find_slot (cfun->eh->exception_handler_label_map, entry, INSERT);
788 /* Before landing pad creation, each exception handler has its own
789 label. After landing pad creation, the exception handlers may
790 share landing pads. This is ok, since maybe_remove_eh_handler
791 only requires the 1-1 mapping before landing pad creation. */
792 gcc_assert (!*slot || cfun->eh->built_landing_pads);
794 *slot = entry;
797 void
798 find_exception_handler_labels (void)
800 int i;
802 if (cfun->eh->exception_handler_label_map)
803 htab_empty (cfun->eh->exception_handler_label_map);
804 else
806 /* ??? The expansion factor here (3/2) must be greater than the htab
807 occupancy factor (4/3) to avoid unnecessary resizing. */
808 cfun->eh->exception_handler_label_map
809 = htab_create_ggc (cfun->eh->last_region_number * 3 / 2,
810 ehl_hash, ehl_eq, NULL);
813 if (cfun->eh->region_tree == NULL)
814 return;
816 for (i = cfun->eh->last_region_number; i > 0; --i)
818 struct eh_region *region;
819 rtx lab;
821 region = VEC_index (eh_region, cfun->eh->region_array, i);
822 if (! region || region->region_number != i)
823 continue;
824 if (cfun->eh->built_landing_pads)
825 lab = region->landing_pad;
826 else
827 lab = region->label;
829 if (lab)
830 add_ehl_entry (lab, region);
833 /* For sjlj exceptions, need the return label to remain live until
834 after landing pad generation. */
835 if (USING_SJLJ_EXCEPTIONS && ! cfun->eh->built_landing_pads)
836 add_ehl_entry (return_label, NULL);
839 /* Returns true if the current function has exception handling regions. */
841 bool
842 current_function_has_exception_handlers (void)
844 int i;
846 for (i = cfun->eh->last_region_number; i > 0; --i)
848 struct eh_region *region;
850 region = VEC_index (eh_region, cfun->eh->region_array, i);
851 if (region
852 && region->region_number == i
853 && region->type != ERT_THROW)
854 return true;
857 return false;
860 /* A subroutine of duplicate_eh_regions. Search the region tree under O
861 for the minimum and maximum region numbers. Update *MIN and *MAX. */
863 static void
864 duplicate_eh_regions_0 (eh_region o, int *min, int *max)
866 if (o->region_number < *min)
867 *min = o->region_number;
868 if (o->region_number > *max)
869 *max = o->region_number;
871 if (o->inner)
873 o = o->inner;
874 duplicate_eh_regions_0 (o, min, max);
875 while (o->next_peer)
877 o = o->next_peer;
878 duplicate_eh_regions_0 (o, min, max);
883 /* A subroutine of duplicate_eh_regions. Copy the region tree under OLD.
884 Root it at OUTER, and apply EH_OFFSET to the region number. Don't worry
885 about the other internal pointers just yet, just the tree-like pointers. */
887 static eh_region
888 duplicate_eh_regions_1 (eh_region old, eh_region outer, int eh_offset)
890 eh_region ret, n;
892 ret = n = ggc_alloc (sizeof (struct eh_region));
894 *n = *old;
895 n->outer = outer;
896 n->next_peer = NULL;
897 gcc_assert (!old->aka);
899 n->region_number += eh_offset;
900 VEC_replace (eh_region, cfun->eh->region_array, n->region_number, n);
902 if (old->inner)
904 old = old->inner;
905 n = n->inner = duplicate_eh_regions_1 (old, ret, eh_offset);
906 while (old->next_peer)
908 old = old->next_peer;
909 n = n->next_peer = duplicate_eh_regions_1 (old, ret, eh_offset);
913 return ret;
916 /* Duplicate the EH regions of IFUN, rooted at COPY_REGION, into current
917 function and root the tree below OUTER_REGION. Remap labels using MAP
918 callback. The special case of COPY_REGION of 0 means all regions. */
921 duplicate_eh_regions (struct function *ifun, duplicate_eh_regions_map map,
922 void *data, int copy_region, int outer_region)
924 eh_region cur, prev_try, outer, *splice;
925 int i, min_region, max_region, eh_offset, cfun_last_region_number;
926 int num_regions;
928 if (!ifun->eh->region_tree)
929 return 0;
931 /* Find the range of region numbers to be copied. The interface we
932 provide here mandates a single offset to find new number from old,
933 which means we must look at the numbers present, instead of the
934 count or something else. */
935 if (copy_region > 0)
937 min_region = INT_MAX;
938 max_region = 0;
940 cur = VEC_index (eh_region, ifun->eh->region_array, copy_region);
941 duplicate_eh_regions_0 (cur, &min_region, &max_region);
943 else
944 min_region = 1, max_region = ifun->eh->last_region_number;
945 num_regions = max_region - min_region + 1;
946 cfun_last_region_number = cfun->eh->last_region_number;
947 eh_offset = cfun_last_region_number + 1 - min_region;
949 /* If we've not yet created a region array, do so now. */
950 VEC_safe_grow (eh_region, gc, cfun->eh->region_array,
951 cfun_last_region_number + 1 + num_regions);
952 cfun->eh->last_region_number = max_region + eh_offset;
954 /* We may have just allocated the array for the first time.
955 Make sure that element zero is null. */
956 VEC_replace (eh_region, cfun->eh->region_array, 0, 0);
958 /* Zero all entries in the range allocated. */
959 memset (VEC_address (eh_region, cfun->eh->region_array)
960 + cfun_last_region_number + 1, 0, num_regions * sizeof (eh_region));
962 /* Locate the spot at which to insert the new tree. */
963 if (outer_region > 0)
965 outer = VEC_index (eh_region, cfun->eh->region_array, outer_region);
966 splice = &outer->inner;
968 else
970 outer = NULL;
971 splice = &cfun->eh->region_tree;
973 while (*splice)
974 splice = &(*splice)->next_peer;
976 /* Copy all the regions in the subtree. */
977 if (copy_region > 0)
979 cur = VEC_index (eh_region, ifun->eh->region_array, copy_region);
980 *splice = duplicate_eh_regions_1 (cur, outer, eh_offset);
982 else
984 eh_region n;
986 cur = ifun->eh->region_tree;
987 *splice = n = duplicate_eh_regions_1 (cur, outer, eh_offset);
988 while (cur->next_peer)
990 cur = cur->next_peer;
991 n = n->next_peer = duplicate_eh_regions_1 (cur, outer, eh_offset);
995 /* Remap all the labels in the new regions. */
996 for (i = cfun_last_region_number + 1;
997 VEC_iterate (eh_region, cfun->eh->region_array, i, cur); ++i)
998 if (cur && cur->tree_label)
999 cur->tree_label = map (cur->tree_label, data);
1001 /* Search for the containing ERT_TRY region to fix up
1002 the prev_try short-cuts for ERT_CLEANUP regions. */
1003 prev_try = NULL;
1004 if (outer_region > 0)
1005 for (prev_try = VEC_index (eh_region, cfun->eh->region_array, outer_region);
1006 prev_try && prev_try->type != ERT_TRY;
1007 prev_try = prev_try->outer)
1008 if (prev_try->type == ERT_MUST_NOT_THROW)
1010 prev_try = NULL;
1011 break;
1014 /* Remap all of the internal catch and cleanup linkages. Since we
1015 duplicate entire subtrees, all of the referenced regions will have
1016 been copied too. And since we renumbered them as a block, a simple
1017 bit of arithmetic finds us the index for the replacement region. */
1018 for (i = cfun_last_region_number + 1;
1019 VEC_iterate (eh_region, cfun->eh->region_array, i, cur); ++i)
1021 if (cur == NULL)
1022 continue;
1024 #define REMAP(REG) \
1025 (REG) = VEC_index (eh_region, cfun->eh->region_array, \
1026 (REG)->region_number + eh_offset)
1028 switch (cur->type)
1030 case ERT_TRY:
1031 if (cur->u.try.catch)
1032 REMAP (cur->u.try.catch);
1033 if (cur->u.try.last_catch)
1034 REMAP (cur->u.try.last_catch);
1035 break;
1037 case ERT_CATCH:
1038 if (cur->u.catch.next_catch)
1039 REMAP (cur->u.catch.next_catch);
1040 if (cur->u.catch.prev_catch)
1041 REMAP (cur->u.catch.prev_catch);
1042 break;
1044 case ERT_CLEANUP:
1045 if (cur->u.cleanup.prev_try)
1046 REMAP (cur->u.cleanup.prev_try);
1047 else
1048 cur->u.cleanup.prev_try = prev_try;
1049 break;
1051 default:
1052 break;
1055 #undef REMAP
1058 return eh_offset;
1061 /* Return true if REGION_A is outer to REGION_B in IFUN. */
1063 bool
1064 eh_region_outer_p (struct function *ifun, int region_a, int region_b)
1066 struct eh_region *rp_a, *rp_b;
1068 gcc_assert (ifun->eh->last_region_number > 0);
1069 gcc_assert (ifun->eh->region_tree);
1071 rp_a = VEC_index (eh_region, ifun->eh->region_array, region_a);
1072 rp_b = VEC_index (eh_region, ifun->eh->region_array, region_b);
1073 gcc_assert (rp_a != NULL);
1074 gcc_assert (rp_b != NULL);
1078 if (rp_a == rp_b)
1079 return true;
1080 rp_b = rp_b->outer;
1082 while (rp_b);
1084 return false;
1087 /* Return region number of region that is outer to both if REGION_A and
1088 REGION_B in IFUN. */
1091 eh_region_outermost (struct function *ifun, int region_a, int region_b)
1093 struct eh_region *rp_a, *rp_b;
1094 sbitmap b_outer;
1096 gcc_assert (ifun->eh->last_region_number > 0);
1097 gcc_assert (ifun->eh->region_tree);
1099 rp_a = VEC_index (eh_region, ifun->eh->region_array, region_a);
1100 rp_b = VEC_index (eh_region, ifun->eh->region_array, region_b);
1101 gcc_assert (rp_a != NULL);
1102 gcc_assert (rp_b != NULL);
1104 b_outer = sbitmap_alloc (ifun->eh->last_region_number + 1);
1105 sbitmap_zero (b_outer);
1109 SET_BIT (b_outer, rp_b->region_number);
1110 rp_b = rp_b->outer;
1112 while (rp_b);
1116 if (TEST_BIT (b_outer, rp_a->region_number))
1118 sbitmap_free (b_outer);
1119 return rp_a->region_number;
1121 rp_a = rp_a->outer;
1123 while (rp_a);
1125 sbitmap_free (b_outer);
1126 return -1;
1129 static int
1130 t2r_eq (const void *pentry, const void *pdata)
1132 tree entry = (tree) pentry;
1133 tree data = (tree) pdata;
1135 return TREE_PURPOSE (entry) == data;
1138 static hashval_t
1139 t2r_hash (const void *pentry)
1141 tree entry = (tree) pentry;
1142 return TREE_HASH (TREE_PURPOSE (entry));
1145 static void
1146 add_type_for_runtime (tree type)
1148 tree *slot;
1150 slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
1151 TREE_HASH (type), INSERT);
1152 if (*slot == NULL)
1154 tree runtime = (*lang_eh_runtime_type) (type);
1155 *slot = tree_cons (type, runtime, NULL_TREE);
1159 static tree
1160 lookup_type_for_runtime (tree type)
1162 tree *slot;
1164 slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
1165 TREE_HASH (type), NO_INSERT);
1167 /* We should have always inserted the data earlier. */
1168 return TREE_VALUE (*slot);
1172 /* Represent an entry in @TTypes for either catch actions
1173 or exception filter actions. */
1174 struct ttypes_filter GTY(())
1176 tree t;
1177 int filter;
1180 /* Compare ENTRY (a ttypes_filter entry in the hash table) with DATA
1181 (a tree) for a @TTypes type node we are thinking about adding. */
1183 static int
1184 ttypes_filter_eq (const void *pentry, const void *pdata)
1186 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1187 tree data = (tree) pdata;
1189 return entry->t == data;
1192 static hashval_t
1193 ttypes_filter_hash (const void *pentry)
1195 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1196 return TREE_HASH (entry->t);
1199 /* Compare ENTRY with DATA (both struct ttypes_filter) for a @TTypes
1200 exception specification list we are thinking about adding. */
1201 /* ??? Currently we use the type lists in the order given. Someone
1202 should put these in some canonical order. */
1204 static int
1205 ehspec_filter_eq (const void *pentry, const void *pdata)
1207 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1208 const struct ttypes_filter *data = (const struct ttypes_filter *) pdata;
1210 return type_list_equal (entry->t, data->t);
1213 /* Hash function for exception specification lists. */
1215 static hashval_t
1216 ehspec_filter_hash (const void *pentry)
1218 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1219 hashval_t h = 0;
1220 tree list;
1222 for (list = entry->t; list ; list = TREE_CHAIN (list))
1223 h = (h << 5) + (h >> 27) + TREE_HASH (TREE_VALUE (list));
1224 return h;
1227 /* Add TYPE (which may be NULL) to cfun->eh->ttype_data, using TYPES_HASH
1228 to speed up the search. Return the filter value to be used. */
1230 static int
1231 add_ttypes_entry (htab_t ttypes_hash, tree type)
1233 struct ttypes_filter **slot, *n;
1235 slot = (struct ttypes_filter **)
1236 htab_find_slot_with_hash (ttypes_hash, type, TREE_HASH (type), INSERT);
1238 if ((n = *slot) == NULL)
1240 /* Filter value is a 1 based table index. */
1242 n = XNEW (struct ttypes_filter);
1243 n->t = type;
1244 n->filter = VEC_length (tree, cfun->eh->ttype_data) + 1;
1245 *slot = n;
1247 VEC_safe_push (tree, gc, cfun->eh->ttype_data, type);
1250 return n->filter;
1253 /* Add LIST to cfun->eh->ehspec_data, using EHSPEC_HASH and TYPES_HASH
1254 to speed up the search. Return the filter value to be used. */
1256 static int
1257 add_ehspec_entry (htab_t ehspec_hash, htab_t ttypes_hash, tree list)
1259 struct ttypes_filter **slot, *n;
1260 struct ttypes_filter dummy;
1262 dummy.t = list;
1263 slot = (struct ttypes_filter **)
1264 htab_find_slot (ehspec_hash, &dummy, INSERT);
1266 if ((n = *slot) == NULL)
1268 /* Filter value is a -1 based byte index into a uleb128 buffer. */
1270 n = XNEW (struct ttypes_filter);
1271 n->t = list;
1272 n->filter = -(VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data) + 1);
1273 *slot = n;
1275 /* Generate a 0 terminated list of filter values. */
1276 for (; list ; list = TREE_CHAIN (list))
1278 if (targetm.arm_eabi_unwinder)
1279 VARRAY_PUSH_TREE (cfun->eh->ehspec_data, TREE_VALUE (list));
1280 else
1282 /* Look up each type in the list and encode its filter
1283 value as a uleb128. */
1284 push_uleb128 (&cfun->eh->ehspec_data,
1285 add_ttypes_entry (ttypes_hash, TREE_VALUE (list)));
1288 if (targetm.arm_eabi_unwinder)
1289 VARRAY_PUSH_TREE (cfun->eh->ehspec_data, NULL_TREE);
1290 else
1291 VARRAY_PUSH_UCHAR (cfun->eh->ehspec_data, 0);
1294 return n->filter;
1297 /* Generate the action filter values to be used for CATCH and
1298 ALLOWED_EXCEPTIONS regions. When using dwarf2 exception regions,
1299 we use lots of landing pads, and so every type or list can share
1300 the same filter value, which saves table space. */
1302 static void
1303 assign_filter_values (void)
1305 int i;
1306 htab_t ttypes, ehspec;
1308 cfun->eh->ttype_data = VEC_alloc (tree, gc, 16);
1309 if (targetm.arm_eabi_unwinder)
1310 VARRAY_TREE_INIT (cfun->eh->ehspec_data, 64, "ehspec_data");
1311 else
1312 VARRAY_UCHAR_INIT (cfun->eh->ehspec_data, 64, "ehspec_data");
1314 ttypes = htab_create (31, ttypes_filter_hash, ttypes_filter_eq, free);
1315 ehspec = htab_create (31, ehspec_filter_hash, ehspec_filter_eq, free);
1317 for (i = cfun->eh->last_region_number; i > 0; --i)
1319 struct eh_region *r;
1321 r = VEC_index (eh_region, cfun->eh->region_array, i);
1323 /* Mind we don't process a region more than once. */
1324 if (!r || r->region_number != i)
1325 continue;
1327 switch (r->type)
1329 case ERT_CATCH:
1330 /* Whatever type_list is (NULL or true list), we build a list
1331 of filters for the region. */
1332 r->u.catch.filter_list = NULL_TREE;
1334 if (r->u.catch.type_list != NULL)
1336 /* Get a filter value for each of the types caught and store
1337 them in the region's dedicated list. */
1338 tree tp_node = r->u.catch.type_list;
1340 for (;tp_node; tp_node = TREE_CHAIN (tp_node))
1342 int flt = add_ttypes_entry (ttypes, TREE_VALUE (tp_node));
1343 tree flt_node = build_int_cst (NULL_TREE, flt);
1345 r->u.catch.filter_list
1346 = tree_cons (NULL_TREE, flt_node, r->u.catch.filter_list);
1349 else
1351 /* Get a filter value for the NULL list also since it will need
1352 an action record anyway. */
1353 int flt = add_ttypes_entry (ttypes, NULL);
1354 tree flt_node = build_int_cst (NULL_TREE, flt);
1356 r->u.catch.filter_list
1357 = tree_cons (NULL_TREE, flt_node, r->u.catch.filter_list);
1360 break;
1362 case ERT_ALLOWED_EXCEPTIONS:
1363 r->u.allowed.filter
1364 = add_ehspec_entry (ehspec, ttypes, r->u.allowed.type_list);
1365 break;
1367 default:
1368 break;
1372 htab_delete (ttypes);
1373 htab_delete (ehspec);
1376 /* Emit SEQ into basic block just before INSN (that is assumed to be
1377 first instruction of some existing BB and return the newly
1378 produced block. */
1379 static basic_block
1380 emit_to_new_bb_before (rtx seq, rtx insn)
1382 rtx last;
1383 basic_block bb;
1384 edge e;
1385 edge_iterator ei;
1387 /* If there happens to be a fallthru edge (possibly created by cleanup_cfg
1388 call), we don't want it to go into newly created landing pad or other EH
1389 construct. */
1390 for (ei = ei_start (BLOCK_FOR_INSN (insn)->preds); (e = ei_safe_edge (ei)); )
1391 if (e->flags & EDGE_FALLTHRU)
1392 force_nonfallthru (e);
1393 else
1394 ei_next (&ei);
1395 last = emit_insn_before (seq, insn);
1396 if (BARRIER_P (last))
1397 last = PREV_INSN (last);
1398 bb = create_basic_block (seq, last, BLOCK_FOR_INSN (insn)->prev_bb);
1399 update_bb_for_insn (bb);
1400 bb->flags |= BB_SUPERBLOCK;
1401 return bb;
1404 /* Generate the code to actually handle exceptions, which will follow the
1405 landing pads. */
1407 static void
1408 build_post_landing_pads (void)
1410 int i;
1412 for (i = cfun->eh->last_region_number; i > 0; --i)
1414 struct eh_region *region;
1415 rtx seq;
1417 region = VEC_index (eh_region, cfun->eh->region_array, i);
1418 /* Mind we don't process a region more than once. */
1419 if (!region || region->region_number != i)
1420 continue;
1422 switch (region->type)
1424 case ERT_TRY:
1425 /* ??? Collect the set of all non-overlapping catch handlers
1426 all the way up the chain until blocked by a cleanup. */
1427 /* ??? Outer try regions can share landing pads with inner
1428 try regions if the types are completely non-overlapping,
1429 and there are no intervening cleanups. */
1431 region->post_landing_pad = gen_label_rtx ();
1433 start_sequence ();
1435 emit_label (region->post_landing_pad);
1437 /* ??? It is mighty inconvenient to call back into the
1438 switch statement generation code in expand_end_case.
1439 Rapid prototyping sez a sequence of ifs. */
1441 struct eh_region *c;
1442 for (c = region->u.try.catch; c ; c = c->u.catch.next_catch)
1444 if (c->u.catch.type_list == NULL)
1445 emit_jump (c->label);
1446 else
1448 /* Need for one cmp/jump per type caught. Each type
1449 list entry has a matching entry in the filter list
1450 (see assign_filter_values). */
1451 tree tp_node = c->u.catch.type_list;
1452 tree flt_node = c->u.catch.filter_list;
1454 for (; tp_node; )
1456 emit_cmp_and_jump_insns
1457 (cfun->eh->filter,
1458 GEN_INT (tree_low_cst (TREE_VALUE (flt_node), 0)),
1459 EQ, NULL_RTX,
1460 targetm.eh_return_filter_mode (), 0, c->label);
1462 tp_node = TREE_CHAIN (tp_node);
1463 flt_node = TREE_CHAIN (flt_node);
1469 /* We delay the generation of the _Unwind_Resume until we generate
1470 landing pads. We emit a marker here so as to get good control
1471 flow data in the meantime. */
1472 region->resume
1473 = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
1474 emit_barrier ();
1476 seq = get_insns ();
1477 end_sequence ();
1479 emit_to_new_bb_before (seq, region->u.try.catch->label);
1481 break;
1483 case ERT_ALLOWED_EXCEPTIONS:
1484 region->post_landing_pad = gen_label_rtx ();
1486 start_sequence ();
1488 emit_label (region->post_landing_pad);
1490 emit_cmp_and_jump_insns (cfun->eh->filter,
1491 GEN_INT (region->u.allowed.filter),
1492 EQ, NULL_RTX,
1493 targetm.eh_return_filter_mode (), 0, region->label);
1495 /* We delay the generation of the _Unwind_Resume until we generate
1496 landing pads. We emit a marker here so as to get good control
1497 flow data in the meantime. */
1498 region->resume
1499 = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
1500 emit_barrier ();
1502 seq = get_insns ();
1503 end_sequence ();
1505 emit_to_new_bb_before (seq, region->label);
1506 break;
1508 case ERT_CLEANUP:
1509 case ERT_MUST_NOT_THROW:
1510 region->post_landing_pad = region->label;
1511 break;
1513 case ERT_CATCH:
1514 case ERT_THROW:
1515 /* Nothing to do. */
1516 break;
1518 default:
1519 gcc_unreachable ();
1524 /* Replace RESX patterns with jumps to the next handler if any, or calls to
1525 _Unwind_Resume otherwise. */
1527 static void
1528 connect_post_landing_pads (void)
1530 int i;
1532 for (i = cfun->eh->last_region_number; i > 0; --i)
1534 struct eh_region *region;
1535 struct eh_region *outer;
1536 rtx seq;
1537 rtx barrier;
1539 region = VEC_index (eh_region, cfun->eh->region_array, i);
1540 /* Mind we don't process a region more than once. */
1541 if (!region || region->region_number != i)
1542 continue;
1544 /* If there is no RESX, or it has been deleted by flow, there's
1545 nothing to fix up. */
1546 if (! region->resume || INSN_DELETED_P (region->resume))
1547 continue;
1549 /* Search for another landing pad in this function. */
1550 for (outer = region->outer; outer ; outer = outer->outer)
1551 if (outer->post_landing_pad)
1552 break;
1554 start_sequence ();
1556 if (outer)
1558 edge e;
1559 basic_block src, dest;
1561 emit_jump (outer->post_landing_pad);
1562 src = BLOCK_FOR_INSN (region->resume);
1563 dest = BLOCK_FOR_INSN (outer->post_landing_pad);
1564 while (EDGE_COUNT (src->succs) > 0)
1565 remove_edge (EDGE_SUCC (src, 0));
1566 e = make_edge (src, dest, 0);
1567 e->probability = REG_BR_PROB_BASE;
1568 e->count = src->count;
1570 else
1572 emit_library_call (unwind_resume_libfunc, LCT_THROW,
1573 VOIDmode, 1, cfun->eh->exc_ptr, ptr_mode);
1575 /* What we just emitted was a throwing libcall, so it got a
1576 barrier automatically added after it. If the last insn in
1577 the libcall sequence isn't the barrier, it's because the
1578 target emits multiple insns for a call, and there are insns
1579 after the actual call insn (which are redundant and would be
1580 optimized away). The barrier is inserted exactly after the
1581 call insn, so let's go get that and delete the insns after
1582 it, because below we need the barrier to be the last insn in
1583 the sequence. */
1584 delete_insns_since (NEXT_INSN (last_call_insn ()));
1587 seq = get_insns ();
1588 end_sequence ();
1589 barrier = emit_insn_before (seq, region->resume);
1590 /* Avoid duplicate barrier. */
1591 gcc_assert (BARRIER_P (barrier));
1592 delete_insn (barrier);
1593 delete_insn (region->resume);
1595 /* ??? From tree-ssa we can wind up with catch regions whose
1596 label is not instantiated, but whose resx is present. Now
1597 that we've dealt with the resx, kill the region. */
1598 if (region->label == NULL && region->type == ERT_CLEANUP)
1599 remove_eh_handler (region);
1604 static void
1605 dw2_build_landing_pads (void)
1607 int i;
1609 for (i = cfun->eh->last_region_number; i > 0; --i)
1611 struct eh_region *region;
1612 rtx seq;
1613 basic_block bb;
1614 edge e;
1616 region = VEC_index (eh_region, cfun->eh->region_array, i);
1617 /* Mind we don't process a region more than once. */
1618 if (!region || region->region_number != i)
1619 continue;
1621 if (region->type != ERT_CLEANUP
1622 && region->type != ERT_TRY
1623 && region->type != ERT_ALLOWED_EXCEPTIONS)
1624 continue;
1626 start_sequence ();
1628 region->landing_pad = gen_label_rtx ();
1629 emit_label (region->landing_pad);
1631 #ifdef HAVE_exception_receiver
1632 if (HAVE_exception_receiver)
1633 emit_insn (gen_exception_receiver ());
1634 else
1635 #endif
1636 #ifdef HAVE_nonlocal_goto_receiver
1637 if (HAVE_nonlocal_goto_receiver)
1638 emit_insn (gen_nonlocal_goto_receiver ());
1639 else
1640 #endif
1641 { /* Nothing */ }
1643 emit_move_insn (cfun->eh->exc_ptr,
1644 gen_rtx_REG (ptr_mode, EH_RETURN_DATA_REGNO (0)));
1645 emit_move_insn (cfun->eh->filter,
1646 gen_rtx_REG (targetm.eh_return_filter_mode (),
1647 EH_RETURN_DATA_REGNO (1)));
1649 seq = get_insns ();
1650 end_sequence ();
1652 bb = emit_to_new_bb_before (seq, region->post_landing_pad);
1653 e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
1654 e->count = bb->count;
1655 e->probability = REG_BR_PROB_BASE;
1660 struct sjlj_lp_info
1662 int directly_reachable;
1663 int action_index;
1664 int dispatch_index;
1665 int call_site_index;
1668 static bool
1669 sjlj_find_directly_reachable_regions (struct sjlj_lp_info *lp_info)
1671 rtx insn;
1672 bool found_one = false;
1674 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
1676 struct eh_region *region;
1677 enum reachable_code rc;
1678 tree type_thrown;
1679 rtx note;
1681 if (! INSN_P (insn))
1682 continue;
1684 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1685 if (!note || INTVAL (XEXP (note, 0)) <= 0)
1686 continue;
1688 region = VEC_index (eh_region, cfun->eh->region_array, INTVAL (XEXP (note, 0)));
1690 type_thrown = NULL_TREE;
1691 if (region->type == ERT_THROW)
1693 type_thrown = region->u.throw.type;
1694 region = region->outer;
1697 /* Find the first containing region that might handle the exception.
1698 That's the landing pad to which we will transfer control. */
1699 rc = RNL_NOT_CAUGHT;
1700 for (; region; region = region->outer)
1702 rc = reachable_next_level (region, type_thrown, NULL);
1703 if (rc != RNL_NOT_CAUGHT)
1704 break;
1706 if (rc == RNL_MAYBE_CAUGHT || rc == RNL_CAUGHT)
1708 lp_info[region->region_number].directly_reachable = 1;
1709 found_one = true;
1713 return found_one;
1716 static void
1717 sjlj_assign_call_site_values (rtx dispatch_label, struct sjlj_lp_info *lp_info)
1719 htab_t ar_hash;
1720 int i, index;
1722 /* First task: build the action table. */
1724 VARRAY_UCHAR_INIT (cfun->eh->action_record_data, 64, "action_record_data");
1725 ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
1727 for (i = cfun->eh->last_region_number; i > 0; --i)
1728 if (lp_info[i].directly_reachable)
1730 struct eh_region *r = VEC_index (eh_region, cfun->eh->region_array, i);
1732 r->landing_pad = dispatch_label;
1733 lp_info[i].action_index = collect_one_action_chain (ar_hash, r);
1734 if (lp_info[i].action_index != -1)
1735 cfun->uses_eh_lsda = 1;
1738 htab_delete (ar_hash);
1740 /* Next: assign dispatch values. In dwarf2 terms, this would be the
1741 landing pad label for the region. For sjlj though, there is one
1742 common landing pad from which we dispatch to the post-landing pads.
1744 A region receives a dispatch index if it is directly reachable
1745 and requires in-function processing. Regions that share post-landing
1746 pads may share dispatch indices. */
1747 /* ??? Post-landing pad sharing doesn't actually happen at the moment
1748 (see build_post_landing_pads) so we don't bother checking for it. */
1750 index = 0;
1751 for (i = cfun->eh->last_region_number; i > 0; --i)
1752 if (lp_info[i].directly_reachable)
1753 lp_info[i].dispatch_index = index++;
1755 /* Finally: assign call-site values. If dwarf2 terms, this would be
1756 the region number assigned by convert_to_eh_region_ranges, but
1757 handles no-action and must-not-throw differently. */
1759 call_site_base = 1;
1760 for (i = cfun->eh->last_region_number; i > 0; --i)
1761 if (lp_info[i].directly_reachable)
1763 int action = lp_info[i].action_index;
1765 /* Map must-not-throw to otherwise unused call-site index 0. */
1766 if (action == -2)
1767 index = 0;
1768 /* Map no-action to otherwise unused call-site index -1. */
1769 else if (action == -1)
1770 index = -1;
1771 /* Otherwise, look it up in the table. */
1772 else
1773 index = add_call_site (GEN_INT (lp_info[i].dispatch_index), action);
1775 lp_info[i].call_site_index = index;
1779 static void
1780 sjlj_mark_call_sites (struct sjlj_lp_info *lp_info)
1782 int last_call_site = -2;
1783 rtx insn, mem;
1785 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
1787 struct eh_region *region;
1788 int this_call_site;
1789 rtx note, before, p;
1791 /* Reset value tracking at extended basic block boundaries. */
1792 if (LABEL_P (insn))
1793 last_call_site = -2;
1795 if (! INSN_P (insn))
1796 continue;
1798 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1799 if (!note)
1801 /* Calls (and trapping insns) without notes are outside any
1802 exception handling region in this function. Mark them as
1803 no action. */
1804 if (CALL_P (insn)
1805 || (flag_non_call_exceptions
1806 && may_trap_p (PATTERN (insn))))
1807 this_call_site = -1;
1808 else
1809 continue;
1811 else
1813 /* Calls that are known to not throw need not be marked. */
1814 if (INTVAL (XEXP (note, 0)) <= 0)
1815 continue;
1817 region = VEC_index (eh_region, cfun->eh->region_array, INTVAL (XEXP (note, 0)));
1818 this_call_site = lp_info[region->region_number].call_site_index;
1821 if (this_call_site == last_call_site)
1822 continue;
1824 /* Don't separate a call from it's argument loads. */
1825 before = insn;
1826 if (CALL_P (insn))
1827 before = find_first_parameter_load (insn, NULL_RTX);
1829 start_sequence ();
1830 mem = adjust_address (cfun->eh->sjlj_fc, TYPE_MODE (integer_type_node),
1831 sjlj_fc_call_site_ofs);
1832 emit_move_insn (mem, GEN_INT (this_call_site));
1833 p = get_insns ();
1834 end_sequence ();
1836 emit_insn_before (p, before);
1837 last_call_site = this_call_site;
1841 /* Construct the SjLj_Function_Context. */
1843 static void
1844 sjlj_emit_function_enter (rtx dispatch_label)
1846 rtx fn_begin, fc, mem, seq;
1847 bool fn_begin_outside_block;
1849 fc = cfun->eh->sjlj_fc;
1851 start_sequence ();
1853 /* We're storing this libcall's address into memory instead of
1854 calling it directly. Thus, we must call assemble_external_libcall
1855 here, as we can not depend on emit_library_call to do it for us. */
1856 assemble_external_libcall (eh_personality_libfunc);
1857 mem = adjust_address (fc, Pmode, sjlj_fc_personality_ofs);
1858 emit_move_insn (mem, eh_personality_libfunc);
1860 mem = adjust_address (fc, Pmode, sjlj_fc_lsda_ofs);
1861 if (cfun->uses_eh_lsda)
1863 char buf[20];
1864 rtx sym;
1866 ASM_GENERATE_INTERNAL_LABEL (buf, "LLSDA", current_function_funcdef_no);
1867 sym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
1868 SYMBOL_REF_FLAGS (sym) = SYMBOL_FLAG_LOCAL;
1869 emit_move_insn (mem, sym);
1871 else
1872 emit_move_insn (mem, const0_rtx);
1874 #ifdef DONT_USE_BUILTIN_SETJMP
1876 rtx x;
1877 x = emit_library_call_value (setjmp_libfunc, NULL_RTX, LCT_RETURNS_TWICE,
1878 TYPE_MODE (integer_type_node), 1,
1879 plus_constant (XEXP (fc, 0),
1880 sjlj_fc_jbuf_ofs), Pmode);
1882 emit_cmp_and_jump_insns (x, const0_rtx, NE, 0,
1883 TYPE_MODE (integer_type_node), 0, dispatch_label);
1884 add_reg_br_prob_note (get_insns (), REG_BR_PROB_BASE/100);
1886 #else
1887 expand_builtin_setjmp_setup (plus_constant (XEXP (fc, 0), sjlj_fc_jbuf_ofs),
1888 dispatch_label);
1889 #endif
1891 emit_library_call (unwind_sjlj_register_libfunc, LCT_NORMAL, VOIDmode,
1892 1, XEXP (fc, 0), Pmode);
1894 seq = get_insns ();
1895 end_sequence ();
1897 /* ??? Instead of doing this at the beginning of the function,
1898 do this in a block that is at loop level 0 and dominates all
1899 can_throw_internal instructions. */
1901 fn_begin_outside_block = true;
1902 for (fn_begin = get_insns (); ; fn_begin = NEXT_INSN (fn_begin))
1903 if (NOTE_P (fn_begin))
1905 if (NOTE_KIND (fn_begin) == NOTE_INSN_FUNCTION_BEG)
1906 break;
1907 else if (NOTE_INSN_BASIC_BLOCK_P (fn_begin))
1908 fn_begin_outside_block = false;
1911 if (fn_begin_outside_block)
1912 insert_insn_on_edge (seq, single_succ_edge (ENTRY_BLOCK_PTR));
1913 else
1914 emit_insn_after (seq, fn_begin);
1917 /* Call back from expand_function_end to know where we should put
1918 the call to unwind_sjlj_unregister_libfunc if needed. */
1920 void
1921 sjlj_emit_function_exit_after (rtx after)
1923 cfun->eh->sjlj_exit_after = after;
1926 static void
1927 sjlj_emit_function_exit (void)
1929 rtx seq;
1930 edge e;
1931 edge_iterator ei;
1933 start_sequence ();
1935 emit_library_call (unwind_sjlj_unregister_libfunc, LCT_NORMAL, VOIDmode,
1936 1, XEXP (cfun->eh->sjlj_fc, 0), Pmode);
1938 seq = get_insns ();
1939 end_sequence ();
1941 /* ??? Really this can be done in any block at loop level 0 that
1942 post-dominates all can_throw_internal instructions. This is
1943 the last possible moment. */
1945 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
1946 if (e->flags & EDGE_FALLTHRU)
1947 break;
1948 if (e)
1950 rtx insn;
1952 /* Figure out whether the place we are supposed to insert libcall
1953 is inside the last basic block or after it. In the other case
1954 we need to emit to edge. */
1955 gcc_assert (e->src->next_bb == EXIT_BLOCK_PTR);
1956 for (insn = BB_HEAD (e->src); ; insn = NEXT_INSN (insn))
1958 if (insn == cfun->eh->sjlj_exit_after)
1960 if (LABEL_P (insn))
1961 insn = NEXT_INSN (insn);
1962 emit_insn_after (seq, insn);
1963 return;
1965 if (insn == BB_END (e->src))
1966 break;
1968 insert_insn_on_edge (seq, e);
1972 static void
1973 sjlj_emit_dispatch_table (rtx dispatch_label, struct sjlj_lp_info *lp_info)
1975 int i, first_reachable;
1976 rtx mem, dispatch, seq, fc;
1977 rtx before;
1978 basic_block bb;
1979 edge e;
1981 fc = cfun->eh->sjlj_fc;
1983 start_sequence ();
1985 emit_label (dispatch_label);
1987 #ifndef DONT_USE_BUILTIN_SETJMP
1988 expand_builtin_setjmp_receiver (dispatch_label);
1989 #endif
1991 /* Load up dispatch index, exc_ptr and filter values from the
1992 function context. */
1993 mem = adjust_address (fc, TYPE_MODE (integer_type_node),
1994 sjlj_fc_call_site_ofs);
1995 dispatch = copy_to_reg (mem);
1997 mem = adjust_address (fc, word_mode, sjlj_fc_data_ofs);
1998 if (word_mode != ptr_mode)
2000 #ifdef POINTERS_EXTEND_UNSIGNED
2001 mem = convert_memory_address (ptr_mode, mem);
2002 #else
2003 mem = convert_to_mode (ptr_mode, mem, 0);
2004 #endif
2006 emit_move_insn (cfun->eh->exc_ptr, mem);
2008 mem = adjust_address (fc, word_mode, sjlj_fc_data_ofs + UNITS_PER_WORD);
2009 emit_move_insn (cfun->eh->filter, mem);
2011 /* Jump to one of the directly reachable regions. */
2012 /* ??? This really ought to be using a switch statement. */
2014 first_reachable = 0;
2015 for (i = cfun->eh->last_region_number; i > 0; --i)
2017 if (! lp_info[i].directly_reachable)
2018 continue;
2020 if (! first_reachable)
2022 first_reachable = i;
2023 continue;
2026 emit_cmp_and_jump_insns (dispatch, GEN_INT (lp_info[i].dispatch_index),
2027 EQ, NULL_RTX, TYPE_MODE (integer_type_node), 0,
2028 ((struct eh_region *)VEC_index (eh_region, cfun->eh->region_array, i))
2029 ->post_landing_pad);
2032 seq = get_insns ();
2033 end_sequence ();
2035 before = (((struct eh_region *)VEC_index (eh_region, cfun->eh->region_array, first_reachable))
2036 ->post_landing_pad);
2038 bb = emit_to_new_bb_before (seq, before);
2039 e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
2040 e->count = bb->count;
2041 e->probability = REG_BR_PROB_BASE;
2044 static void
2045 sjlj_build_landing_pads (void)
2047 struct sjlj_lp_info *lp_info;
2049 lp_info = XCNEWVEC (struct sjlj_lp_info, cfun->eh->last_region_number + 1);
2051 if (sjlj_find_directly_reachable_regions (lp_info))
2053 rtx dispatch_label = gen_label_rtx ();
2055 cfun->eh->sjlj_fc
2056 = assign_stack_local (TYPE_MODE (sjlj_fc_type_node),
2057 int_size_in_bytes (sjlj_fc_type_node),
2058 TYPE_ALIGN (sjlj_fc_type_node));
2060 sjlj_assign_call_site_values (dispatch_label, lp_info);
2061 sjlj_mark_call_sites (lp_info);
2063 sjlj_emit_function_enter (dispatch_label);
2064 sjlj_emit_dispatch_table (dispatch_label, lp_info);
2065 sjlj_emit_function_exit ();
2068 free (lp_info);
2071 void
2072 finish_eh_generation (void)
2074 basic_block bb;
2076 /* Nothing to do if no regions created. */
2077 if (cfun->eh->region_tree == NULL)
2078 return;
2080 /* The object here is to provide find_basic_blocks with detailed
2081 information (via reachable_handlers) on how exception control
2082 flows within the function. In this first pass, we can include
2083 type information garnered from ERT_THROW and ERT_ALLOWED_EXCEPTIONS
2084 regions, and hope that it will be useful in deleting unreachable
2085 handlers. Subsequently, we will generate landing pads which will
2086 connect many of the handlers, and then type information will not
2087 be effective. Still, this is a win over previous implementations. */
2089 /* These registers are used by the landing pads. Make sure they
2090 have been generated. */
2091 get_exception_pointer (cfun);
2092 get_exception_filter (cfun);
2094 /* Construct the landing pads. */
2096 assign_filter_values ();
2097 build_post_landing_pads ();
2098 connect_post_landing_pads ();
2099 if (USING_SJLJ_EXCEPTIONS)
2100 sjlj_build_landing_pads ();
2101 else
2102 dw2_build_landing_pads ();
2104 cfun->eh->built_landing_pads = 1;
2106 /* We've totally changed the CFG. Start over. */
2107 find_exception_handler_labels ();
2108 break_superblocks ();
2109 if (USING_SJLJ_EXCEPTIONS)
2110 commit_edge_insertions ();
2111 FOR_EACH_BB (bb)
2113 edge e;
2114 edge_iterator ei;
2115 bool eh = false;
2116 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2118 if (e->flags & EDGE_EH)
2120 remove_edge (e);
2121 eh = true;
2123 else
2124 ei_next (&ei);
2126 if (eh)
2127 rtl_make_eh_edge (NULL, bb, BB_END (bb));
2131 static hashval_t
2132 ehl_hash (const void *pentry)
2134 struct ehl_map_entry *entry = (struct ehl_map_entry *) pentry;
2136 /* 2^32 * ((sqrt(5) - 1) / 2) */
2137 const hashval_t scaled_golden_ratio = 0x9e3779b9;
2138 return CODE_LABEL_NUMBER (entry->label) * scaled_golden_ratio;
2141 static int
2142 ehl_eq (const void *pentry, const void *pdata)
2144 struct ehl_map_entry *entry = (struct ehl_map_entry *) pentry;
2145 struct ehl_map_entry *data = (struct ehl_map_entry *) pdata;
2147 return entry->label == data->label;
2150 /* This section handles removing dead code for flow. */
2152 /* Remove LABEL from exception_handler_label_map. */
2154 static void
2155 remove_exception_handler_label (rtx label)
2157 struct ehl_map_entry **slot, tmp;
2159 /* If exception_handler_label_map was not built yet,
2160 there is nothing to do. */
2161 if (cfun->eh->exception_handler_label_map == NULL)
2162 return;
2164 tmp.label = label;
2165 slot = (struct ehl_map_entry **)
2166 htab_find_slot (cfun->eh->exception_handler_label_map, &tmp, NO_INSERT);
2167 gcc_assert (slot);
2169 htab_clear_slot (cfun->eh->exception_handler_label_map, (void **) slot);
2172 /* Splice REGION from the region tree etc. */
2174 static void
2175 remove_eh_handler (struct eh_region *region)
2177 struct eh_region **pp, **pp_start, *p, *outer, *inner;
2178 rtx lab;
2180 /* For the benefit of efficiently handling REG_EH_REGION notes,
2181 replace this region in the region array with its containing
2182 region. Note that previous region deletions may result in
2183 multiple copies of this region in the array, so we have a
2184 list of alternate numbers by which we are known. */
2186 outer = region->outer;
2187 VEC_replace (eh_region, cfun->eh->region_array, region->region_number, outer);
2188 if (region->aka)
2190 unsigned i;
2191 bitmap_iterator bi;
2193 EXECUTE_IF_SET_IN_BITMAP (region->aka, 0, i, bi)
2195 VEC_replace (eh_region, cfun->eh->region_array, i, outer);
2199 if (outer)
2201 if (!outer->aka)
2202 outer->aka = BITMAP_GGC_ALLOC ();
2203 if (region->aka)
2204 bitmap_ior_into (outer->aka, region->aka);
2205 bitmap_set_bit (outer->aka, region->region_number);
2208 if (cfun->eh->built_landing_pads)
2209 lab = region->landing_pad;
2210 else
2211 lab = region->label;
2212 if (lab)
2213 remove_exception_handler_label (lab);
2215 if (outer)
2216 pp_start = &outer->inner;
2217 else
2218 pp_start = &cfun->eh->region_tree;
2219 for (pp = pp_start, p = *pp; p != region; pp = &p->next_peer, p = *pp)
2220 continue;
2221 *pp = region->next_peer;
2223 inner = region->inner;
2224 if (inner)
2226 for (p = inner; p->next_peer ; p = p->next_peer)
2227 p->outer = outer;
2228 p->outer = outer;
2230 p->next_peer = *pp_start;
2231 *pp_start = inner;
2234 if (region->type == ERT_CATCH)
2236 struct eh_region *try, *next, *prev;
2238 for (try = region->next_peer;
2239 try->type == ERT_CATCH;
2240 try = try->next_peer)
2241 continue;
2242 gcc_assert (try->type == ERT_TRY);
2244 next = region->u.catch.next_catch;
2245 prev = region->u.catch.prev_catch;
2247 if (next)
2248 next->u.catch.prev_catch = prev;
2249 else
2250 try->u.try.last_catch = prev;
2251 if (prev)
2252 prev->u.catch.next_catch = next;
2253 else
2255 try->u.try.catch = next;
2256 if (! next)
2257 remove_eh_handler (try);
2262 /* LABEL heads a basic block that is about to be deleted. If this
2263 label corresponds to an exception region, we may be able to
2264 delete the region. */
2266 void
2267 maybe_remove_eh_handler (rtx label)
2269 struct ehl_map_entry **slot, tmp;
2270 struct eh_region *region;
2272 /* ??? After generating landing pads, it's not so simple to determine
2273 if the region data is completely unused. One must examine the
2274 landing pad and the post landing pad, and whether an inner try block
2275 is referencing the catch handlers directly. */
2276 if (cfun->eh->built_landing_pads)
2277 return;
2279 tmp.label = label;
2280 slot = (struct ehl_map_entry **)
2281 htab_find_slot (cfun->eh->exception_handler_label_map, &tmp, NO_INSERT);
2282 if (! slot)
2283 return;
2284 region = (*slot)->region;
2285 if (! region)
2286 return;
2288 /* Flow will want to remove MUST_NOT_THROW regions as unreachable
2289 because there is no path to the fallback call to terminate.
2290 But the region continues to affect call-site data until there
2291 are no more contained calls, which we don't see here. */
2292 if (region->type == ERT_MUST_NOT_THROW)
2294 htab_clear_slot (cfun->eh->exception_handler_label_map, (void **) slot);
2295 region->label = NULL_RTX;
2297 else
2298 remove_eh_handler (region);
2301 /* Invokes CALLBACK for every exception handler label. Only used by old
2302 loop hackery; should not be used by new code. */
2304 void
2305 for_each_eh_label (void (*callback) (rtx))
2307 htab_traverse (cfun->eh->exception_handler_label_map, for_each_eh_label_1,
2308 (void *) &callback);
2311 static int
2312 for_each_eh_label_1 (void **pentry, void *data)
2314 struct ehl_map_entry *entry = *(struct ehl_map_entry **)pentry;
2315 void (*callback) (rtx) = *(void (**) (rtx)) data;
2317 (*callback) (entry->label);
2318 return 1;
2321 /* Invoke CALLBACK for every exception region in the current function. */
2323 void
2324 for_each_eh_region (void (*callback) (struct eh_region *))
2326 int i, n = cfun->eh->last_region_number;
2327 for (i = 1; i <= n; ++i)
2329 struct eh_region *region;
2331 region = VEC_index (eh_region, cfun->eh->region_array, i);
2332 if (region)
2333 (*callback) (region);
2337 /* This section describes CFG exception edges for flow. */
2339 /* For communicating between calls to reachable_next_level. */
2340 struct reachable_info
2342 tree types_caught;
2343 tree types_allowed;
2344 void (*callback) (struct eh_region *, void *);
2345 void *callback_data;
2346 bool saw_any_handlers;
2349 /* A subroutine of reachable_next_level. Return true if TYPE, or a
2350 base class of TYPE, is in HANDLED. */
2352 static int
2353 check_handled (tree handled, tree type)
2355 tree t;
2357 /* We can check for exact matches without front-end help. */
2358 if (! lang_eh_type_covers)
2360 for (t = handled; t ; t = TREE_CHAIN (t))
2361 if (TREE_VALUE (t) == type)
2362 return 1;
2364 else
2366 for (t = handled; t ; t = TREE_CHAIN (t))
2367 if ((*lang_eh_type_covers) (TREE_VALUE (t), type))
2368 return 1;
2371 return 0;
2374 /* A subroutine of reachable_next_level. If we are collecting a list
2375 of handlers, add one. After landing pad generation, reference
2376 it instead of the handlers themselves. Further, the handlers are
2377 all wired together, so by referencing one, we've got them all.
2378 Before landing pad generation we reference each handler individually.
2380 LP_REGION contains the landing pad; REGION is the handler. */
2382 static void
2383 add_reachable_handler (struct reachable_info *info,
2384 struct eh_region *lp_region, struct eh_region *region)
2386 if (! info)
2387 return;
2389 info->saw_any_handlers = true;
2391 if (cfun->eh->built_landing_pads)
2392 info->callback (lp_region, info->callback_data);
2393 else
2394 info->callback (region, info->callback_data);
2397 /* Process one level of exception regions for reachability.
2398 If TYPE_THROWN is non-null, then it is the *exact* type being
2399 propagated. If INFO is non-null, then collect handler labels
2400 and caught/allowed type information between invocations. */
2402 static enum reachable_code
2403 reachable_next_level (struct eh_region *region, tree type_thrown,
2404 struct reachable_info *info)
2406 switch (region->type)
2408 case ERT_CLEANUP:
2409 /* Before landing-pad generation, we model control flow
2410 directly to the individual handlers. In this way we can
2411 see that catch handler types may shadow one another. */
2412 add_reachable_handler (info, region, region);
2413 return RNL_MAYBE_CAUGHT;
2415 case ERT_TRY:
2417 struct eh_region *c;
2418 enum reachable_code ret = RNL_NOT_CAUGHT;
2420 for (c = region->u.try.catch; c ; c = c->u.catch.next_catch)
2422 /* A catch-all handler ends the search. */
2423 if (c->u.catch.type_list == NULL)
2425 add_reachable_handler (info, region, c);
2426 return RNL_CAUGHT;
2429 if (type_thrown)
2431 /* If we have at least one type match, end the search. */
2432 tree tp_node = c->u.catch.type_list;
2434 for (; tp_node; tp_node = TREE_CHAIN (tp_node))
2436 tree type = TREE_VALUE (tp_node);
2438 if (type == type_thrown
2439 || (lang_eh_type_covers
2440 && (*lang_eh_type_covers) (type, type_thrown)))
2442 add_reachable_handler (info, region, c);
2443 return RNL_CAUGHT;
2447 /* If we have definitive information of a match failure,
2448 the catch won't trigger. */
2449 if (lang_eh_type_covers)
2450 return RNL_NOT_CAUGHT;
2453 /* At this point, we either don't know what type is thrown or
2454 don't have front-end assistance to help deciding if it is
2455 covered by one of the types in the list for this region.
2457 We'd then like to add this region to the list of reachable
2458 handlers since it is indeed potentially reachable based on the
2459 information we have.
2461 Actually, this handler is for sure not reachable if all the
2462 types it matches have already been caught. That is, it is only
2463 potentially reachable if at least one of the types it catches
2464 has not been previously caught. */
2466 if (! info)
2467 ret = RNL_MAYBE_CAUGHT;
2468 else
2470 tree tp_node = c->u.catch.type_list;
2471 bool maybe_reachable = false;
2473 /* Compute the potential reachability of this handler and
2474 update the list of types caught at the same time. */
2475 for (; tp_node; tp_node = TREE_CHAIN (tp_node))
2477 tree type = TREE_VALUE (tp_node);
2479 if (! check_handled (info->types_caught, type))
2481 info->types_caught
2482 = tree_cons (NULL, type, info->types_caught);
2484 maybe_reachable = true;
2488 if (maybe_reachable)
2490 add_reachable_handler (info, region, c);
2492 /* ??? If the catch type is a base class of every allowed
2493 type, then we know we can stop the search. */
2494 ret = RNL_MAYBE_CAUGHT;
2499 return ret;
2502 case ERT_ALLOWED_EXCEPTIONS:
2503 /* An empty list of types definitely ends the search. */
2504 if (region->u.allowed.type_list == NULL_TREE)
2506 add_reachable_handler (info, region, region);
2507 return RNL_CAUGHT;
2510 /* Collect a list of lists of allowed types for use in detecting
2511 when a catch may be transformed into a catch-all. */
2512 if (info)
2513 info->types_allowed = tree_cons (NULL_TREE,
2514 region->u.allowed.type_list,
2515 info->types_allowed);
2517 /* If we have definitive information about the type hierarchy,
2518 then we can tell if the thrown type will pass through the
2519 filter. */
2520 if (type_thrown && lang_eh_type_covers)
2522 if (check_handled (region->u.allowed.type_list, type_thrown))
2523 return RNL_NOT_CAUGHT;
2524 else
2526 add_reachable_handler (info, region, region);
2527 return RNL_CAUGHT;
2531 add_reachable_handler (info, region, region);
2532 return RNL_MAYBE_CAUGHT;
2534 case ERT_CATCH:
2535 /* Catch regions are handled by their controlling try region. */
2536 return RNL_NOT_CAUGHT;
2538 case ERT_MUST_NOT_THROW:
2539 /* Here we end our search, since no exceptions may propagate.
2540 If we've touched down at some landing pad previous, then the
2541 explicit function call we generated may be used. Otherwise
2542 the call is made by the runtime.
2544 Before inlining, do not perform this optimization. We may
2545 inline a subroutine that contains handlers, and that will
2546 change the value of saw_any_handlers. */
2548 if ((info && info->saw_any_handlers) || !cfun->after_inlining)
2550 add_reachable_handler (info, region, region);
2551 return RNL_CAUGHT;
2553 else
2554 return RNL_BLOCKED;
2556 case ERT_THROW:
2557 case ERT_UNKNOWN:
2558 /* Shouldn't see these here. */
2559 gcc_unreachable ();
2560 break;
2561 default:
2562 gcc_unreachable ();
2566 /* Invoke CALLBACK on each region reachable from REGION_NUMBER. */
2568 void
2569 foreach_reachable_handler (int region_number, bool is_resx,
2570 void (*callback) (struct eh_region *, void *),
2571 void *callback_data)
2573 struct reachable_info info;
2574 struct eh_region *region;
2575 tree type_thrown;
2577 memset (&info, 0, sizeof (info));
2578 info.callback = callback;
2579 info.callback_data = callback_data;
2581 region = VEC_index (eh_region, cfun->eh->region_array, region_number);
2583 type_thrown = NULL_TREE;
2584 if (is_resx)
2586 /* A RESX leaves a region instead of entering it. Thus the
2587 region itself may have been deleted out from under us. */
2588 if (region == NULL)
2589 return;
2590 region = region->outer;
2592 else if (region->type == ERT_THROW)
2594 type_thrown = region->u.throw.type;
2595 region = region->outer;
2598 while (region)
2600 if (reachable_next_level (region, type_thrown, &info) >= RNL_CAUGHT)
2601 break;
2602 /* If we have processed one cleanup, there is no point in
2603 processing any more of them. Each cleanup will have an edge
2604 to the next outer cleanup region, so the flow graph will be
2605 accurate. */
2606 if (region->type == ERT_CLEANUP)
2607 region = region->u.cleanup.prev_try;
2608 else
2609 region = region->outer;
2613 /* Retrieve a list of labels of exception handlers which can be
2614 reached by a given insn. */
2616 static void
2617 arh_to_landing_pad (struct eh_region *region, void *data)
2619 rtx *p_handlers = data;
2620 if (! *p_handlers)
2621 *p_handlers = alloc_INSN_LIST (region->landing_pad, NULL_RTX);
2624 static void
2625 arh_to_label (struct eh_region *region, void *data)
2627 rtx *p_handlers = data;
2628 *p_handlers = alloc_INSN_LIST (region->label, *p_handlers);
2632 reachable_handlers (rtx insn)
2634 bool is_resx = false;
2635 rtx handlers = NULL;
2636 int region_number;
2638 if (JUMP_P (insn)
2639 && GET_CODE (PATTERN (insn)) == RESX)
2641 region_number = XINT (PATTERN (insn), 0);
2642 is_resx = true;
2644 else
2646 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2647 if (!note || INTVAL (XEXP (note, 0)) <= 0)
2648 return NULL;
2649 region_number = INTVAL (XEXP (note, 0));
2652 foreach_reachable_handler (region_number, is_resx,
2653 (cfun->eh->built_landing_pads
2654 ? arh_to_landing_pad
2655 : arh_to_label),
2656 &handlers);
2658 return handlers;
2661 /* Determine if the given INSN can throw an exception that is caught
2662 within the function. */
2664 bool
2665 can_throw_internal_1 (int region_number, bool is_resx)
2667 struct eh_region *region;
2668 tree type_thrown;
2670 region = VEC_index (eh_region, cfun->eh->region_array, region_number);
2672 type_thrown = NULL_TREE;
2673 if (is_resx)
2674 region = region->outer;
2675 else if (region->type == ERT_THROW)
2677 type_thrown = region->u.throw.type;
2678 region = region->outer;
2681 /* If this exception is ignored by each and every containing region,
2682 then control passes straight out. The runtime may handle some
2683 regions, which also do not require processing internally. */
2684 for (; region; region = region->outer)
2686 enum reachable_code how = reachable_next_level (region, type_thrown, 0);
2687 if (how == RNL_BLOCKED)
2688 return false;
2689 if (how != RNL_NOT_CAUGHT)
2690 return true;
2693 return false;
2696 bool
2697 can_throw_internal (rtx insn)
2699 rtx note;
2701 if (! INSN_P (insn))
2702 return false;
2704 if (JUMP_P (insn)
2705 && GET_CODE (PATTERN (insn)) == RESX
2706 && XINT (PATTERN (insn), 0) > 0)
2707 return can_throw_internal_1 (XINT (PATTERN (insn), 0), true);
2709 if (NONJUMP_INSN_P (insn)
2710 && GET_CODE (PATTERN (insn)) == SEQUENCE)
2711 insn = XVECEXP (PATTERN (insn), 0, 0);
2713 /* Every insn that might throw has an EH_REGION note. */
2714 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2715 if (!note || INTVAL (XEXP (note, 0)) <= 0)
2716 return false;
2718 return can_throw_internal_1 (INTVAL (XEXP (note, 0)), false);
2721 /* Determine if the given INSN can throw an exception that is
2722 visible outside the function. */
2724 bool
2725 can_throw_external_1 (int region_number, bool is_resx)
2727 struct eh_region *region;
2728 tree type_thrown;
2730 region = VEC_index (eh_region, cfun->eh->region_array, region_number);
2732 type_thrown = NULL_TREE;
2733 if (is_resx)
2734 region = region->outer;
2735 else if (region->type == ERT_THROW)
2737 type_thrown = region->u.throw.type;
2738 region = region->outer;
2741 /* If the exception is caught or blocked by any containing region,
2742 then it is not seen by any calling function. */
2743 for (; region ; region = region->outer)
2744 if (reachable_next_level (region, type_thrown, NULL) >= RNL_CAUGHT)
2745 return false;
2747 return true;
2750 bool
2751 can_throw_external (rtx insn)
2753 rtx note;
2755 if (! INSN_P (insn))
2756 return false;
2758 if (JUMP_P (insn)
2759 && GET_CODE (PATTERN (insn)) == RESX
2760 && XINT (PATTERN (insn), 0) > 0)
2761 return can_throw_external_1 (XINT (PATTERN (insn), 0), true);
2763 if (NONJUMP_INSN_P (insn)
2764 && GET_CODE (PATTERN (insn)) == SEQUENCE)
2765 insn = XVECEXP (PATTERN (insn), 0, 0);
2767 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2768 if (!note)
2770 /* Calls (and trapping insns) without notes are outside any
2771 exception handling region in this function. We have to
2772 assume it might throw. Given that the front end and middle
2773 ends mark known NOTHROW functions, this isn't so wildly
2774 inaccurate. */
2775 return (CALL_P (insn)
2776 || (flag_non_call_exceptions
2777 && may_trap_p (PATTERN (insn))));
2779 if (INTVAL (XEXP (note, 0)) <= 0)
2780 return false;
2782 return can_throw_external_1 (INTVAL (XEXP (note, 0)), false);
2785 /* Set TREE_NOTHROW and cfun->all_throwers_are_sibcalls. */
2787 unsigned int
2788 set_nothrow_function_flags (void)
2790 rtx insn;
2792 /* If we don't know that this implementation of the function will
2793 actually be used, then we must not set TREE_NOTHROW, since
2794 callers must not assume that this function does not throw. */
2795 if (DECL_REPLACEABLE_P (current_function_decl))
2796 return 0;
2798 TREE_NOTHROW (current_function_decl) = 1;
2800 /* Assume cfun->all_throwers_are_sibcalls until we encounter
2801 something that can throw an exception. We specifically exempt
2802 CALL_INSNs that are SIBLING_CALL_P, as these are really jumps,
2803 and can't throw. Most CALL_INSNs are not SIBLING_CALL_P, so this
2804 is optimistic. */
2806 cfun->all_throwers_are_sibcalls = 1;
2808 if (! flag_exceptions)
2809 return 0;
2811 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2812 if (can_throw_external (insn))
2814 TREE_NOTHROW (current_function_decl) = 0;
2816 if (!CALL_P (insn) || !SIBLING_CALL_P (insn))
2818 cfun->all_throwers_are_sibcalls = 0;
2819 return 0;
2823 for (insn = current_function_epilogue_delay_list; insn;
2824 insn = XEXP (insn, 1))
2825 if (can_throw_external (insn))
2827 TREE_NOTHROW (current_function_decl) = 0;
2829 if (!CALL_P (insn) || !SIBLING_CALL_P (insn))
2831 cfun->all_throwers_are_sibcalls = 0;
2832 return 0;
2835 return 0;
2838 struct tree_opt_pass pass_set_nothrow_function_flags =
2840 NULL, /* name */
2841 NULL, /* gate */
2842 set_nothrow_function_flags, /* execute */
2843 NULL, /* sub */
2844 NULL, /* next */
2845 0, /* static_pass_number */
2846 0, /* tv_id */
2847 0, /* properties_required */
2848 0, /* properties_provided */
2849 0, /* properties_destroyed */
2850 0, /* todo_flags_start */
2851 0, /* todo_flags_finish */
2852 0 /* letter */
2856 /* Various hooks for unwind library. */
2858 /* Do any necessary initialization to access arbitrary stack frames.
2859 On the SPARC, this means flushing the register windows. */
2861 void
2862 expand_builtin_unwind_init (void)
2864 /* Set this so all the registers get saved in our frame; we need to be
2865 able to copy the saved values for any registers from frames we unwind. */
2866 current_function_calls_unwind_init = 1;
2868 #ifdef SETUP_FRAME_ADDRESSES
2869 SETUP_FRAME_ADDRESSES ();
2870 #endif
2874 expand_builtin_eh_return_data_regno (tree exp)
2876 tree which = CALL_EXPR_ARG (exp, 0);
2877 unsigned HOST_WIDE_INT iwhich;
2879 if (TREE_CODE (which) != INTEGER_CST)
2881 error ("argument of %<__builtin_eh_return_regno%> must be constant");
2882 return constm1_rtx;
2885 iwhich = tree_low_cst (which, 1);
2886 iwhich = EH_RETURN_DATA_REGNO (iwhich);
2887 if (iwhich == INVALID_REGNUM)
2888 return constm1_rtx;
2890 #ifdef DWARF_FRAME_REGNUM
2891 iwhich = DWARF_FRAME_REGNUM (iwhich);
2892 #else
2893 iwhich = DBX_REGISTER_NUMBER (iwhich);
2894 #endif
2896 return GEN_INT (iwhich);
2899 /* Given a value extracted from the return address register or stack slot,
2900 return the actual address encoded in that value. */
2903 expand_builtin_extract_return_addr (tree addr_tree)
2905 rtx addr = expand_expr (addr_tree, NULL_RTX, Pmode, EXPAND_NORMAL);
2907 if (GET_MODE (addr) != Pmode
2908 && GET_MODE (addr) != VOIDmode)
2910 #ifdef POINTERS_EXTEND_UNSIGNED
2911 addr = convert_memory_address (Pmode, addr);
2912 #else
2913 addr = convert_to_mode (Pmode, addr, 0);
2914 #endif
2917 /* First mask out any unwanted bits. */
2918 #ifdef MASK_RETURN_ADDR
2919 expand_and (Pmode, addr, MASK_RETURN_ADDR, addr);
2920 #endif
2922 /* Then adjust to find the real return address. */
2923 #if defined (RETURN_ADDR_OFFSET)
2924 addr = plus_constant (addr, RETURN_ADDR_OFFSET);
2925 #endif
2927 return addr;
2930 /* Given an actual address in addr_tree, do any necessary encoding
2931 and return the value to be stored in the return address register or
2932 stack slot so the epilogue will return to that address. */
2935 expand_builtin_frob_return_addr (tree addr_tree)
2937 rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, EXPAND_NORMAL);
2939 addr = convert_memory_address (Pmode, addr);
2941 #ifdef RETURN_ADDR_OFFSET
2942 addr = force_reg (Pmode, addr);
2943 addr = plus_constant (addr, -RETURN_ADDR_OFFSET);
2944 #endif
2946 return addr;
2949 /* Set up the epilogue with the magic bits we'll need to return to the
2950 exception handler. */
2952 void
2953 expand_builtin_eh_return (tree stackadj_tree ATTRIBUTE_UNUSED,
2954 tree handler_tree)
2956 rtx tmp;
2958 #ifdef EH_RETURN_STACKADJ_RTX
2959 tmp = expand_expr (stackadj_tree, cfun->eh->ehr_stackadj,
2960 VOIDmode, EXPAND_NORMAL);
2961 tmp = convert_memory_address (Pmode, tmp);
2962 if (!cfun->eh->ehr_stackadj)
2963 cfun->eh->ehr_stackadj = copy_to_reg (tmp);
2964 else if (tmp != cfun->eh->ehr_stackadj)
2965 emit_move_insn (cfun->eh->ehr_stackadj, tmp);
2966 #endif
2968 tmp = expand_expr (handler_tree, cfun->eh->ehr_handler,
2969 VOIDmode, EXPAND_NORMAL);
2970 tmp = convert_memory_address (Pmode, tmp);
2971 if (!cfun->eh->ehr_handler)
2972 cfun->eh->ehr_handler = copy_to_reg (tmp);
2973 else if (tmp != cfun->eh->ehr_handler)
2974 emit_move_insn (cfun->eh->ehr_handler, tmp);
2976 if (!cfun->eh->ehr_label)
2977 cfun->eh->ehr_label = gen_label_rtx ();
2978 emit_jump (cfun->eh->ehr_label);
2981 void
2982 expand_eh_return (void)
2984 rtx around_label;
2986 if (! cfun->eh->ehr_label)
2987 return;
2989 current_function_calls_eh_return = 1;
2991 #ifdef EH_RETURN_STACKADJ_RTX
2992 emit_move_insn (EH_RETURN_STACKADJ_RTX, const0_rtx);
2993 #endif
2995 around_label = gen_label_rtx ();
2996 emit_jump (around_label);
2998 emit_label (cfun->eh->ehr_label);
2999 clobber_return_register ();
3001 #ifdef EH_RETURN_STACKADJ_RTX
3002 emit_move_insn (EH_RETURN_STACKADJ_RTX, cfun->eh->ehr_stackadj);
3003 #endif
3005 #ifdef HAVE_eh_return
3006 if (HAVE_eh_return)
3007 emit_insn (gen_eh_return (cfun->eh->ehr_handler));
3008 else
3009 #endif
3011 #ifdef EH_RETURN_HANDLER_RTX
3012 emit_move_insn (EH_RETURN_HANDLER_RTX, cfun->eh->ehr_handler);
3013 #else
3014 error ("__builtin_eh_return not supported on this target");
3015 #endif
3018 emit_label (around_label);
3021 /* Convert a ptr_mode address ADDR_TREE to a Pmode address controlled by
3022 POINTERS_EXTEND_UNSIGNED and return it. */
3025 expand_builtin_extend_pointer (tree addr_tree)
3027 rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, EXPAND_NORMAL);
3028 int extend;
3030 #ifdef POINTERS_EXTEND_UNSIGNED
3031 extend = POINTERS_EXTEND_UNSIGNED;
3032 #else
3033 /* The previous EH code did an unsigned extend by default, so we do this also
3034 for consistency. */
3035 extend = 1;
3036 #endif
3038 return convert_modes (word_mode, ptr_mode, addr, extend);
3041 /* In the following functions, we represent entries in the action table
3042 as 1-based indices. Special cases are:
3044 0: null action record, non-null landing pad; implies cleanups
3045 -1: null action record, null landing pad; implies no action
3046 -2: no call-site entry; implies must_not_throw
3047 -3: we have yet to process outer regions
3049 Further, no special cases apply to the "next" field of the record.
3050 For next, 0 means end of list. */
3052 struct action_record
3054 int offset;
3055 int filter;
3056 int next;
3059 static int
3060 action_record_eq (const void *pentry, const void *pdata)
3062 const struct action_record *entry = (const struct action_record *) pentry;
3063 const struct action_record *data = (const struct action_record *) pdata;
3064 return entry->filter == data->filter && entry->next == data->next;
3067 static hashval_t
3068 action_record_hash (const void *pentry)
3070 const struct action_record *entry = (const struct action_record *) pentry;
3071 return entry->next * 1009 + entry->filter;
3074 static int
3075 add_action_record (htab_t ar_hash, int filter, int next)
3077 struct action_record **slot, *new, tmp;
3079 tmp.filter = filter;
3080 tmp.next = next;
3081 slot = (struct action_record **) htab_find_slot (ar_hash, &tmp, INSERT);
3083 if ((new = *slot) == NULL)
3085 new = xmalloc (sizeof (*new));
3086 new->offset = VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data) + 1;
3087 new->filter = filter;
3088 new->next = next;
3089 *slot = new;
3091 /* The filter value goes in untouched. The link to the next
3092 record is a "self-relative" byte offset, or zero to indicate
3093 that there is no next record. So convert the absolute 1 based
3094 indices we've been carrying around into a displacement. */
3096 push_sleb128 (&cfun->eh->action_record_data, filter);
3097 if (next)
3098 next -= VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data) + 1;
3099 push_sleb128 (&cfun->eh->action_record_data, next);
3102 return new->offset;
3105 static int
3106 collect_one_action_chain (htab_t ar_hash, struct eh_region *region)
3108 struct eh_region *c;
3109 int next;
3111 /* If we've reached the top of the region chain, then we have
3112 no actions, and require no landing pad. */
3113 if (region == NULL)
3114 return -1;
3116 switch (region->type)
3118 case ERT_CLEANUP:
3119 /* A cleanup adds a zero filter to the beginning of the chain, but
3120 there are special cases to look out for. If there are *only*
3121 cleanups along a path, then it compresses to a zero action.
3122 Further, if there are multiple cleanups along a path, we only
3123 need to represent one of them, as that is enough to trigger
3124 entry to the landing pad at runtime. */
3125 next = collect_one_action_chain (ar_hash, region->outer);
3126 if (next <= 0)
3127 return 0;
3128 for (c = region->outer; c ; c = c->outer)
3129 if (c->type == ERT_CLEANUP)
3130 return next;
3131 return add_action_record (ar_hash, 0, next);
3133 case ERT_TRY:
3134 /* Process the associated catch regions in reverse order.
3135 If there's a catch-all handler, then we don't need to
3136 search outer regions. Use a magic -3 value to record
3137 that we haven't done the outer search. */
3138 next = -3;
3139 for (c = region->u.try.last_catch; c ; c = c->u.catch.prev_catch)
3141 if (c->u.catch.type_list == NULL)
3143 /* Retrieve the filter from the head of the filter list
3144 where we have stored it (see assign_filter_values). */
3145 int filter
3146 = TREE_INT_CST_LOW (TREE_VALUE (c->u.catch.filter_list));
3148 next = add_action_record (ar_hash, filter, 0);
3150 else
3152 /* Once the outer search is done, trigger an action record for
3153 each filter we have. */
3154 tree flt_node;
3156 if (next == -3)
3158 next = collect_one_action_chain (ar_hash, region->outer);
3160 /* If there is no next action, terminate the chain. */
3161 if (next == -1)
3162 next = 0;
3163 /* If all outer actions are cleanups or must_not_throw,
3164 we'll have no action record for it, since we had wanted
3165 to encode these states in the call-site record directly.
3166 Add a cleanup action to the chain to catch these. */
3167 else if (next <= 0)
3168 next = add_action_record (ar_hash, 0, 0);
3171 flt_node = c->u.catch.filter_list;
3172 for (; flt_node; flt_node = TREE_CHAIN (flt_node))
3174 int filter = TREE_INT_CST_LOW (TREE_VALUE (flt_node));
3175 next = add_action_record (ar_hash, filter, next);
3179 return next;
3181 case ERT_ALLOWED_EXCEPTIONS:
3182 /* An exception specification adds its filter to the
3183 beginning of the chain. */
3184 next = collect_one_action_chain (ar_hash, region->outer);
3186 /* If there is no next action, terminate the chain. */
3187 if (next == -1)
3188 next = 0;
3189 /* If all outer actions are cleanups or must_not_throw,
3190 we'll have no action record for it, since we had wanted
3191 to encode these states in the call-site record directly.
3192 Add a cleanup action to the chain to catch these. */
3193 else if (next <= 0)
3194 next = add_action_record (ar_hash, 0, 0);
3196 return add_action_record (ar_hash, region->u.allowed.filter, next);
3198 case ERT_MUST_NOT_THROW:
3199 /* A must-not-throw region with no inner handlers or cleanups
3200 requires no call-site entry. Note that this differs from
3201 the no handler or cleanup case in that we do require an lsda
3202 to be generated. Return a magic -2 value to record this. */
3203 return -2;
3205 case ERT_CATCH:
3206 case ERT_THROW:
3207 /* CATCH regions are handled in TRY above. THROW regions are
3208 for optimization information only and produce no output. */
3209 return collect_one_action_chain (ar_hash, region->outer);
3211 default:
3212 gcc_unreachable ();
3216 static int
3217 add_call_site (rtx landing_pad, int action)
3219 struct call_site_record *data = cfun->eh->call_site_data;
3220 int used = cfun->eh->call_site_data_used;
3221 int size = cfun->eh->call_site_data_size;
3223 if (used >= size)
3225 size = (size ? size * 2 : 64);
3226 data = ggc_realloc (data, sizeof (*data) * size);
3227 cfun->eh->call_site_data = data;
3228 cfun->eh->call_site_data_size = size;
3231 data[used].landing_pad = landing_pad;
3232 data[used].action = action;
3234 cfun->eh->call_site_data_used = used + 1;
3236 return used + call_site_base;
3239 /* Turn REG_EH_REGION notes back into NOTE_INSN_EH_REGION notes.
3240 The new note numbers will not refer to region numbers, but
3241 instead to call site entries. */
3243 unsigned int
3244 convert_to_eh_region_ranges (void)
3246 rtx insn, iter, note;
3247 htab_t ar_hash;
3248 int last_action = -3;
3249 rtx last_action_insn = NULL_RTX;
3250 rtx last_landing_pad = NULL_RTX;
3251 rtx first_no_action_insn = NULL_RTX;
3252 int call_site = 0;
3254 if (USING_SJLJ_EXCEPTIONS || cfun->eh->region_tree == NULL)
3255 return 0;
3257 VARRAY_UCHAR_INIT (cfun->eh->action_record_data, 64, "action_record_data");
3259 ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
3261 for (iter = get_insns (); iter ; iter = NEXT_INSN (iter))
3262 if (INSN_P (iter))
3264 struct eh_region *region;
3265 int this_action;
3266 rtx this_landing_pad;
3268 insn = iter;
3269 if (NONJUMP_INSN_P (insn)
3270 && GET_CODE (PATTERN (insn)) == SEQUENCE)
3271 insn = XVECEXP (PATTERN (insn), 0, 0);
3273 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3274 if (!note)
3276 if (! (CALL_P (insn)
3277 || (flag_non_call_exceptions
3278 && may_trap_p (PATTERN (insn)))))
3279 continue;
3280 this_action = -1;
3281 region = NULL;
3283 else
3285 if (INTVAL (XEXP (note, 0)) <= 0)
3286 continue;
3287 region = VEC_index (eh_region, cfun->eh->region_array, INTVAL (XEXP (note, 0)));
3288 this_action = collect_one_action_chain (ar_hash, region);
3291 /* Existence of catch handlers, or must-not-throw regions
3292 implies that an lsda is needed (even if empty). */
3293 if (this_action != -1)
3294 cfun->uses_eh_lsda = 1;
3296 /* Delay creation of region notes for no-action regions
3297 until we're sure that an lsda will be required. */
3298 else if (last_action == -3)
3300 first_no_action_insn = iter;
3301 last_action = -1;
3304 /* Cleanups and handlers may share action chains but not
3305 landing pads. Collect the landing pad for this region. */
3306 if (this_action >= 0)
3308 struct eh_region *o;
3309 for (o = region; ! o->landing_pad ; o = o->outer)
3310 continue;
3311 this_landing_pad = o->landing_pad;
3313 else
3314 this_landing_pad = NULL_RTX;
3316 /* Differing actions or landing pads implies a change in call-site
3317 info, which implies some EH_REGION note should be emitted. */
3318 if (last_action != this_action
3319 || last_landing_pad != this_landing_pad)
3321 /* If we'd not seen a previous action (-3) or the previous
3322 action was must-not-throw (-2), then we do not need an
3323 end note. */
3324 if (last_action >= -1)
3326 /* If we delayed the creation of the begin, do it now. */
3327 if (first_no_action_insn)
3329 call_site = add_call_site (NULL_RTX, 0);
3330 note = emit_note_before (NOTE_INSN_EH_REGION_BEG,
3331 first_no_action_insn);
3332 NOTE_EH_HANDLER (note) = call_site;
3333 first_no_action_insn = NULL_RTX;
3336 note = emit_note_after (NOTE_INSN_EH_REGION_END,
3337 last_action_insn);
3338 NOTE_EH_HANDLER (note) = call_site;
3341 /* If the new action is must-not-throw, then no region notes
3342 are created. */
3343 if (this_action >= -1)
3345 call_site = add_call_site (this_landing_pad,
3346 this_action < 0 ? 0 : this_action);
3347 note = emit_note_before (NOTE_INSN_EH_REGION_BEG, iter);
3348 NOTE_EH_HANDLER (note) = call_site;
3351 last_action = this_action;
3352 last_landing_pad = this_landing_pad;
3354 last_action_insn = iter;
3357 if (last_action >= -1 && ! first_no_action_insn)
3359 note = emit_note_after (NOTE_INSN_EH_REGION_END, last_action_insn);
3360 NOTE_EH_HANDLER (note) = call_site;
3363 htab_delete (ar_hash);
3364 return 0;
3367 struct tree_opt_pass pass_convert_to_eh_region_ranges =
3369 "eh-ranges", /* name */
3370 NULL, /* gate */
3371 convert_to_eh_region_ranges, /* execute */
3372 NULL, /* sub */
3373 NULL, /* next */
3374 0, /* static_pass_number */
3375 0, /* tv_id */
3376 0, /* properties_required */
3377 0, /* properties_provided */
3378 0, /* properties_destroyed */
3379 0, /* todo_flags_start */
3380 TODO_dump_func, /* todo_flags_finish */
3381 0 /* letter */
3385 static void
3386 push_uleb128 (varray_type *data_area, unsigned int value)
3390 unsigned char byte = value & 0x7f;
3391 value >>= 7;
3392 if (value)
3393 byte |= 0x80;
3394 VARRAY_PUSH_UCHAR (*data_area, byte);
3396 while (value);
3399 static void
3400 push_sleb128 (varray_type *data_area, int value)
3402 unsigned char byte;
3403 int more;
3407 byte = value & 0x7f;
3408 value >>= 7;
3409 more = ! ((value == 0 && (byte & 0x40) == 0)
3410 || (value == -1 && (byte & 0x40) != 0));
3411 if (more)
3412 byte |= 0x80;
3413 VARRAY_PUSH_UCHAR (*data_area, byte);
3415 while (more);
3419 #ifndef HAVE_AS_LEB128
3420 static int
3421 dw2_size_of_call_site_table (void)
3423 int n = cfun->eh->call_site_data_used;
3424 int size = n * (4 + 4 + 4);
3425 int i;
3427 for (i = 0; i < n; ++i)
3429 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3430 size += size_of_uleb128 (cs->action);
3433 return size;
3436 static int
3437 sjlj_size_of_call_site_table (void)
3439 int n = cfun->eh->call_site_data_used;
3440 int size = 0;
3441 int i;
3443 for (i = 0; i < n; ++i)
3445 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3446 size += size_of_uleb128 (INTVAL (cs->landing_pad));
3447 size += size_of_uleb128 (cs->action);
3450 return size;
3452 #endif
3454 static void
3455 dw2_output_call_site_table (void)
3457 int n = cfun->eh->call_site_data_used;
3458 int i;
3460 for (i = 0; i < n; ++i)
3462 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3463 char reg_start_lab[32];
3464 char reg_end_lab[32];
3465 char landing_pad_lab[32];
3467 ASM_GENERATE_INTERNAL_LABEL (reg_start_lab, "LEHB", call_site_base + i);
3468 ASM_GENERATE_INTERNAL_LABEL (reg_end_lab, "LEHE", call_site_base + i);
3470 if (cs->landing_pad)
3471 ASM_GENERATE_INTERNAL_LABEL (landing_pad_lab, "L",
3472 CODE_LABEL_NUMBER (cs->landing_pad));
3474 /* ??? Perhaps use insn length scaling if the assembler supports
3475 generic arithmetic. */
3476 /* ??? Perhaps use attr_length to choose data1 or data2 instead of
3477 data4 if the function is small enough. */
3478 #ifdef HAVE_AS_LEB128
3479 dw2_asm_output_delta_uleb128 (reg_start_lab,
3480 current_function_func_begin_label,
3481 "region %d start", i);
3482 dw2_asm_output_delta_uleb128 (reg_end_lab, reg_start_lab,
3483 "length");
3484 if (cs->landing_pad)
3485 dw2_asm_output_delta_uleb128 (landing_pad_lab,
3486 current_function_func_begin_label,
3487 "landing pad");
3488 else
3489 dw2_asm_output_data_uleb128 (0, "landing pad");
3490 #else
3491 dw2_asm_output_delta (4, reg_start_lab,
3492 current_function_func_begin_label,
3493 "region %d start", i);
3494 dw2_asm_output_delta (4, reg_end_lab, reg_start_lab, "length");
3495 if (cs->landing_pad)
3496 dw2_asm_output_delta (4, landing_pad_lab,
3497 current_function_func_begin_label,
3498 "landing pad");
3499 else
3500 dw2_asm_output_data (4, 0, "landing pad");
3501 #endif
3502 dw2_asm_output_data_uleb128 (cs->action, "action");
3505 call_site_base += n;
3508 static void
3509 sjlj_output_call_site_table (void)
3511 int n = cfun->eh->call_site_data_used;
3512 int i;
3514 for (i = 0; i < n; ++i)
3516 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3518 dw2_asm_output_data_uleb128 (INTVAL (cs->landing_pad),
3519 "region %d landing pad", i);
3520 dw2_asm_output_data_uleb128 (cs->action, "action");
3523 call_site_base += n;
3526 #ifndef TARGET_UNWIND_INFO
3527 /* Switch to the section that should be used for exception tables. */
3529 static void
3530 switch_to_exception_section (const char * ARG_UNUSED (fnname))
3532 section *s;
3534 if (exception_section)
3535 s = exception_section;
3536 else
3538 /* Compute the section and cache it into exception_section,
3539 unless it depends on the function name. */
3540 if (targetm.have_named_sections)
3542 int flags;
3544 if (EH_TABLES_CAN_BE_READ_ONLY)
3546 int tt_format =
3547 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3548 flags = ((! flag_pic
3549 || ((tt_format & 0x70) != DW_EH_PE_absptr
3550 && (tt_format & 0x70) != DW_EH_PE_aligned))
3551 ? 0 : SECTION_WRITE);
3553 else
3554 flags = SECTION_WRITE;
3556 #ifdef HAVE_LD_EH_GC_SECTIONS
3557 if (flag_function_sections)
3559 char *section_name = xmalloc (strlen (fnname) + 32);
3560 sprintf (section_name, ".gcc_except_table.%s", fnname);
3561 s = get_section (section_name, flags, NULL);
3562 free (section_name);
3564 else
3565 #endif
3566 exception_section
3567 = s = get_section (".gcc_except_table", flags, NULL);
3569 else
3570 exception_section
3571 = s = flag_pic ? data_section : readonly_data_section;
3574 switch_to_section (s);
3576 #endif
3579 /* Output a reference from an exception table to the type_info object TYPE.
3580 TT_FORMAT and TT_FORMAT_SIZE describe the DWARF encoding method used for
3581 the value. */
3583 static void
3584 output_ttype (tree type, int tt_format, int tt_format_size)
3586 rtx value;
3587 bool public = true;
3589 if (type == NULL_TREE)
3590 value = const0_rtx;
3591 else
3593 struct varpool_node *node;
3595 type = lookup_type_for_runtime (type);
3596 value = expand_expr (type, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
3598 /* Let cgraph know that the rtti decl is used. Not all of the
3599 paths below go through assemble_integer, which would take
3600 care of this for us. */
3601 STRIP_NOPS (type);
3602 if (TREE_CODE (type) == ADDR_EXPR)
3604 type = TREE_OPERAND (type, 0);
3605 if (TREE_CODE (type) == VAR_DECL)
3607 node = varpool_node (type);
3608 if (node)
3609 varpool_mark_needed_node (node);
3610 public = TREE_PUBLIC (type);
3613 else
3614 gcc_assert (TREE_CODE (type) == INTEGER_CST);
3617 /* Allow the target to override the type table entry format. */
3618 if (targetm.asm_out.ttype (value))
3619 return;
3621 if (tt_format == DW_EH_PE_absptr || tt_format == DW_EH_PE_aligned)
3622 assemble_integer (value, tt_format_size,
3623 tt_format_size * BITS_PER_UNIT, 1);
3624 else
3625 dw2_asm_output_encoded_addr_rtx (tt_format, value, public, NULL);
3628 void
3629 output_function_exception_table (const char * ARG_UNUSED (fnname))
3631 int tt_format, cs_format, lp_format, i, n;
3632 #ifdef HAVE_AS_LEB128
3633 char ttype_label[32];
3634 char cs_after_size_label[32];
3635 char cs_end_label[32];
3636 #else
3637 int call_site_len;
3638 #endif
3639 int have_tt_data;
3640 int tt_format_size = 0;
3642 /* Not all functions need anything. */
3643 if (! cfun->uses_eh_lsda)
3644 return;
3646 if (eh_personality_libfunc)
3647 assemble_external_libcall (eh_personality_libfunc);
3649 #ifdef TARGET_UNWIND_INFO
3650 /* TODO: Move this into target file. */
3651 fputs ("\t.personality\t", asm_out_file);
3652 output_addr_const (asm_out_file, eh_personality_libfunc);
3653 fputs ("\n\t.handlerdata\n", asm_out_file);
3654 /* Note that varasm still thinks we're in the function's code section.
3655 The ".endp" directive that will immediately follow will take us back. */
3656 #else
3657 switch_to_exception_section (fnname);
3658 #endif
3660 /* If the target wants a label to begin the table, emit it here. */
3661 targetm.asm_out.except_table_label (asm_out_file);
3663 have_tt_data = (VEC_length (tree, cfun->eh->ttype_data) > 0
3664 || VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data) > 0);
3666 /* Indicate the format of the @TType entries. */
3667 if (! have_tt_data)
3668 tt_format = DW_EH_PE_omit;
3669 else
3671 tt_format = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3672 #ifdef HAVE_AS_LEB128
3673 ASM_GENERATE_INTERNAL_LABEL (ttype_label, "LLSDATT",
3674 current_function_funcdef_no);
3675 #endif
3676 tt_format_size = size_of_encoded_value (tt_format);
3678 assemble_align (tt_format_size * BITS_PER_UNIT);
3681 targetm.asm_out.internal_label (asm_out_file, "LLSDA",
3682 current_function_funcdef_no);
3684 /* The LSDA header. */
3686 /* Indicate the format of the landing pad start pointer. An omitted
3687 field implies @LPStart == @Start. */
3688 /* Currently we always put @LPStart == @Start. This field would
3689 be most useful in moving the landing pads completely out of
3690 line to another section, but it could also be used to minimize
3691 the size of uleb128 landing pad offsets. */
3692 lp_format = DW_EH_PE_omit;
3693 dw2_asm_output_data (1, lp_format, "@LPStart format (%s)",
3694 eh_data_format_name (lp_format));
3696 /* @LPStart pointer would go here. */
3698 dw2_asm_output_data (1, tt_format, "@TType format (%s)",
3699 eh_data_format_name (tt_format));
3701 #ifndef HAVE_AS_LEB128
3702 if (USING_SJLJ_EXCEPTIONS)
3703 call_site_len = sjlj_size_of_call_site_table ();
3704 else
3705 call_site_len = dw2_size_of_call_site_table ();
3706 #endif
3708 /* A pc-relative 4-byte displacement to the @TType data. */
3709 if (have_tt_data)
3711 #ifdef HAVE_AS_LEB128
3712 char ttype_after_disp_label[32];
3713 ASM_GENERATE_INTERNAL_LABEL (ttype_after_disp_label, "LLSDATTD",
3714 current_function_funcdef_no);
3715 dw2_asm_output_delta_uleb128 (ttype_label, ttype_after_disp_label,
3716 "@TType base offset");
3717 ASM_OUTPUT_LABEL (asm_out_file, ttype_after_disp_label);
3718 #else
3719 /* Ug. Alignment queers things. */
3720 unsigned int before_disp, after_disp, last_disp, disp;
3722 before_disp = 1 + 1;
3723 after_disp = (1 + size_of_uleb128 (call_site_len)
3724 + call_site_len
3725 + VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data)
3726 + (VEC_length (tree, cfun->eh->ttype_data)
3727 * tt_format_size));
3729 disp = after_disp;
3732 unsigned int disp_size, pad;
3734 last_disp = disp;
3735 disp_size = size_of_uleb128 (disp);
3736 pad = before_disp + disp_size + after_disp;
3737 if (pad % tt_format_size)
3738 pad = tt_format_size - (pad % tt_format_size);
3739 else
3740 pad = 0;
3741 disp = after_disp + pad;
3743 while (disp != last_disp);
3745 dw2_asm_output_data_uleb128 (disp, "@TType base offset");
3746 #endif
3749 /* Indicate the format of the call-site offsets. */
3750 #ifdef HAVE_AS_LEB128
3751 cs_format = DW_EH_PE_uleb128;
3752 #else
3753 cs_format = DW_EH_PE_udata4;
3754 #endif
3755 dw2_asm_output_data (1, cs_format, "call-site format (%s)",
3756 eh_data_format_name (cs_format));
3758 #ifdef HAVE_AS_LEB128
3759 ASM_GENERATE_INTERNAL_LABEL (cs_after_size_label, "LLSDACSB",
3760 current_function_funcdef_no);
3761 ASM_GENERATE_INTERNAL_LABEL (cs_end_label, "LLSDACSE",
3762 current_function_funcdef_no);
3763 dw2_asm_output_delta_uleb128 (cs_end_label, cs_after_size_label,
3764 "Call-site table length");
3765 ASM_OUTPUT_LABEL (asm_out_file, cs_after_size_label);
3766 if (USING_SJLJ_EXCEPTIONS)
3767 sjlj_output_call_site_table ();
3768 else
3769 dw2_output_call_site_table ();
3770 ASM_OUTPUT_LABEL (asm_out_file, cs_end_label);
3771 #else
3772 dw2_asm_output_data_uleb128 (call_site_len,"Call-site table length");
3773 if (USING_SJLJ_EXCEPTIONS)
3774 sjlj_output_call_site_table ();
3775 else
3776 dw2_output_call_site_table ();
3777 #endif
3779 /* ??? Decode and interpret the data for flag_debug_asm. */
3780 n = VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data);
3781 for (i = 0; i < n; ++i)
3782 dw2_asm_output_data (1, VARRAY_UCHAR (cfun->eh->action_record_data, i),
3783 (i ? NULL : "Action record table"));
3785 if (have_tt_data)
3786 assemble_align (tt_format_size * BITS_PER_UNIT);
3788 i = VEC_length (tree, cfun->eh->ttype_data);
3789 while (i-- > 0)
3791 tree type = VEC_index (tree, cfun->eh->ttype_data, i);
3792 output_ttype (type, tt_format, tt_format_size);
3795 #ifdef HAVE_AS_LEB128
3796 if (have_tt_data)
3797 ASM_OUTPUT_LABEL (asm_out_file, ttype_label);
3798 #endif
3800 /* ??? Decode and interpret the data for flag_debug_asm. */
3801 n = VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data);
3802 for (i = 0; i < n; ++i)
3804 if (targetm.arm_eabi_unwinder)
3806 tree type = VARRAY_TREE (cfun->eh->ehspec_data, i);
3807 output_ttype (type, tt_format, tt_format_size);
3809 else
3810 dw2_asm_output_data (1, VARRAY_UCHAR (cfun->eh->ehspec_data, i),
3811 (i ? NULL : "Exception specification table"));
3814 switch_to_section (current_function_section ());
3817 void
3818 set_eh_throw_stmt_table (struct function *fun, struct htab *table)
3820 fun->eh->throw_stmt_table = table;
3823 htab_t
3824 get_eh_throw_stmt_table (struct function *fun)
3826 return fun->eh->throw_stmt_table;
3829 /* Dump EH information to OUT. */
3830 void
3831 dump_eh_tree (FILE *out, struct function *fun)
3833 struct eh_region *i;
3834 int depth = 0;
3835 static const char * const type_name[] = {"unknown", "cleanup", "try", "catch",
3836 "allowed_exceptions", "must_not_throw",
3837 "throw"};
3839 i = fun->eh->region_tree;
3840 if (! i)
3841 return;
3843 fprintf (out, "Eh tree:\n");
3844 while (1)
3846 fprintf (out, " %*s %i %s", depth * 2, "",
3847 i->region_number, type_name [(int)i->type]);
3848 if (i->tree_label)
3850 fprintf (out, " tree_label:");
3851 print_generic_expr (out, i->tree_label, 0);
3853 fprintf (out, "\n");
3854 /* If there are sub-regions, process them. */
3855 if (i->inner)
3856 i = i->inner, depth++;
3857 /* If there are peers, process them. */
3858 else if (i->next_peer)
3859 i = i->next_peer;
3860 /* Otherwise, step back up the tree to the next peer. */
3861 else
3863 do {
3864 i = i->outer;
3865 depth--;
3866 if (i == NULL)
3867 return;
3868 } while (i->next_peer == NULL);
3869 i = i->next_peer;
3874 /* Verify some basic invariants on EH datastructures. Could be extended to
3875 catch more. */
3876 void
3877 verify_eh_tree (struct function *fun)
3879 struct eh_region *i, *outer = NULL;
3880 bool err = false;
3881 int nvisited = 0;
3882 int count = 0;
3883 int j;
3884 int depth = 0;
3886 i = fun->eh->region_tree;
3887 if (! i)
3888 return;
3889 for (j = fun->eh->last_region_number; j > 0; --j)
3890 if ((i = VEC_index (eh_region, cfun->eh->region_array, j)))
3892 count++;
3893 if (i->region_number != j)
3895 error ("region_array is corrupted for region %i", i->region_number);
3896 err = true;
3900 while (1)
3902 if (VEC_index (eh_region, cfun->eh->region_array, i->region_number) != i)
3904 error ("region_array is corrupted for region %i", i->region_number);
3905 err = true;
3907 if (i->outer != outer)
3909 error ("outer block of region %i is wrong", i->region_number);
3910 err = true;
3912 if (i->may_contain_throw && outer && !outer->may_contain_throw)
3914 error ("region %i may contain throw and is contained in region that may not",
3915 i->region_number);
3916 err = true;
3918 if (depth < 0)
3920 error ("negative nesting depth of region %i", i->region_number);
3921 err = true;
3923 nvisited ++;
3924 /* If there are sub-regions, process them. */
3925 if (i->inner)
3926 outer = i, i = i->inner, depth++;
3927 /* If there are peers, process them. */
3928 else if (i->next_peer)
3929 i = i->next_peer;
3930 /* Otherwise, step back up the tree to the next peer. */
3931 else
3933 do {
3934 i = i->outer;
3935 depth--;
3936 if (i == NULL)
3938 if (depth != -1)
3940 error ("tree list ends on depth %i", depth + 1);
3941 err = true;
3943 if (count != nvisited)
3945 error ("array does not match the region tree");
3946 err = true;
3948 if (err)
3950 dump_eh_tree (stderr, fun);
3951 internal_error ("verify_eh_tree failed");
3953 return;
3955 outer = i->outer;
3956 } while (i->next_peer == NULL);
3957 i = i->next_peer;
3962 /* Initialize unwind_resume_libfunc. */
3964 void
3965 default_init_unwind_resume_libfunc (void)
3967 /* The default c++ routines aren't actually c++ specific, so use those. */
3968 unwind_resume_libfunc =
3969 init_one_libfunc ( USING_SJLJ_EXCEPTIONS ? "_Unwind_SjLj_Resume"
3970 : "_Unwind_Resume");
3974 static bool
3975 gate_handle_eh (void)
3977 return doing_eh (0);
3980 /* Complete generation of exception handling code. */
3981 static unsigned int
3982 rest_of_handle_eh (void)
3984 cleanup_cfg (CLEANUP_NO_INSN_DEL);
3985 finish_eh_generation ();
3986 cleanup_cfg (CLEANUP_NO_INSN_DEL);
3987 return 0;
3990 struct tree_opt_pass pass_rtl_eh =
3992 "eh", /* name */
3993 gate_handle_eh, /* gate */
3994 rest_of_handle_eh, /* execute */
3995 NULL, /* sub */
3996 NULL, /* next */
3997 0, /* static_pass_number */
3998 TV_JUMP, /* tv_id */
3999 0, /* properties_required */
4000 0, /* properties_provided */
4001 0, /* properties_destroyed */
4002 0, /* todo_flags_start */
4003 TODO_dump_func, /* todo_flags_finish */
4004 'h' /* letter */
4007 #include "gt-except.h"