PR rtl-optimization/24762
[official-gcc.git] / gcc / except.c
blob0e9ca239284a2626bdaf8d53b5cff596730e8406
1 /* Implements exception handling.
2 Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005 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;
256 static int t2r_eq (const void *, const void *);
257 static hashval_t t2r_hash (const void *);
258 static void add_type_for_runtime (tree);
259 static tree lookup_type_for_runtime (tree);
261 static void remove_unreachable_regions (rtx);
263 static int ttypes_filter_eq (const void *, const void *);
264 static hashval_t ttypes_filter_hash (const void *);
265 static int ehspec_filter_eq (const void *, const void *);
266 static hashval_t ehspec_filter_hash (const void *);
267 static int add_ttypes_entry (htab_t, tree);
268 static int add_ehspec_entry (htab_t, htab_t, tree);
269 static void assign_filter_values (void);
270 static void build_post_landing_pads (void);
271 static void connect_post_landing_pads (void);
272 static void dw2_build_landing_pads (void);
274 struct sjlj_lp_info;
275 static bool sjlj_find_directly_reachable_regions (struct sjlj_lp_info *);
276 static void sjlj_assign_call_site_values (rtx, struct sjlj_lp_info *);
277 static void sjlj_mark_call_sites (struct sjlj_lp_info *);
278 static void sjlj_emit_function_enter (rtx);
279 static void sjlj_emit_function_exit (void);
280 static void sjlj_emit_dispatch_table (rtx, struct sjlj_lp_info *);
281 static void sjlj_build_landing_pads (void);
283 static hashval_t ehl_hash (const void *);
284 static int ehl_eq (const void *, const void *);
285 static void add_ehl_entry (rtx, struct eh_region *);
286 static void remove_exception_handler_label (rtx);
287 static void remove_eh_handler (struct eh_region *);
288 static int for_each_eh_label_1 (void **, void *);
290 /* The return value of reachable_next_level. */
291 enum reachable_code
293 /* The given exception is not processed by the given region. */
294 RNL_NOT_CAUGHT,
295 /* The given exception may need processing by the given region. */
296 RNL_MAYBE_CAUGHT,
297 /* The given exception is completely processed by the given region. */
298 RNL_CAUGHT,
299 /* The given exception is completely processed by the runtime. */
300 RNL_BLOCKED
303 struct reachable_info;
304 static enum reachable_code reachable_next_level (struct eh_region *, tree,
305 struct reachable_info *);
307 static int action_record_eq (const void *, const void *);
308 static hashval_t action_record_hash (const void *);
309 static int add_action_record (htab_t, int, int);
310 static int collect_one_action_chain (htab_t, struct eh_region *);
311 static int add_call_site (rtx, int);
313 static void push_uleb128 (varray_type *, unsigned int);
314 static void push_sleb128 (varray_type *, int);
315 #ifndef HAVE_AS_LEB128
316 static int dw2_size_of_call_site_table (void);
317 static int sjlj_size_of_call_site_table (void);
318 #endif
319 static void dw2_output_call_site_table (void);
320 static void sjlj_output_call_site_table (void);
323 /* Routine to see if exception handling is turned on.
324 DO_WARN is nonzero if we want to inform the user that exception
325 handling is turned off.
327 This is used to ensure that -fexceptions has been specified if the
328 compiler tries to use any exception-specific functions. */
331 doing_eh (int do_warn)
333 if (! flag_exceptions)
335 static int warned = 0;
336 if (! warned && do_warn)
338 error ("exception handling disabled, use -fexceptions to enable");
339 warned = 1;
341 return 0;
343 return 1;
347 void
348 init_eh (void)
350 if (! flag_exceptions)
351 return;
353 type_to_runtime_map = htab_create_ggc (31, t2r_hash, t2r_eq, NULL);
355 /* Create the SjLj_Function_Context structure. This should match
356 the definition in unwind-sjlj.c. */
357 if (USING_SJLJ_EXCEPTIONS)
359 tree f_jbuf, f_per, f_lsda, f_prev, f_cs, f_data, tmp;
361 sjlj_fc_type_node = lang_hooks.types.make_type (RECORD_TYPE);
363 f_prev = build_decl (FIELD_DECL, get_identifier ("__prev"),
364 build_pointer_type (sjlj_fc_type_node));
365 DECL_FIELD_CONTEXT (f_prev) = sjlj_fc_type_node;
367 f_cs = build_decl (FIELD_DECL, get_identifier ("__call_site"),
368 integer_type_node);
369 DECL_FIELD_CONTEXT (f_cs) = sjlj_fc_type_node;
371 tmp = build_index_type (build_int_cst (NULL_TREE, 4 - 1));
372 tmp = build_array_type (lang_hooks.types.type_for_mode (word_mode, 1),
373 tmp);
374 f_data = build_decl (FIELD_DECL, get_identifier ("__data"), tmp);
375 DECL_FIELD_CONTEXT (f_data) = sjlj_fc_type_node;
377 f_per = build_decl (FIELD_DECL, get_identifier ("__personality"),
378 ptr_type_node);
379 DECL_FIELD_CONTEXT (f_per) = sjlj_fc_type_node;
381 f_lsda = build_decl (FIELD_DECL, get_identifier ("__lsda"),
382 ptr_type_node);
383 DECL_FIELD_CONTEXT (f_lsda) = sjlj_fc_type_node;
385 #ifdef DONT_USE_BUILTIN_SETJMP
386 #ifdef JMP_BUF_SIZE
387 tmp = build_int_cst (NULL_TREE, JMP_BUF_SIZE - 1);
388 #else
389 /* Should be large enough for most systems, if it is not,
390 JMP_BUF_SIZE should be defined with the proper value. It will
391 also tend to be larger than necessary for most systems, a more
392 optimal port will define JMP_BUF_SIZE. */
393 tmp = build_int_cst (NULL_TREE, FIRST_PSEUDO_REGISTER + 2 - 1);
394 #endif
395 #else
396 /* builtin_setjmp takes a pointer to 5 words. */
397 tmp = build_int_cst (NULL_TREE, 5 * BITS_PER_WORD / POINTER_SIZE - 1);
398 #endif
399 tmp = build_index_type (tmp);
400 tmp = build_array_type (ptr_type_node, tmp);
401 f_jbuf = build_decl (FIELD_DECL, get_identifier ("__jbuf"), tmp);
402 #ifdef DONT_USE_BUILTIN_SETJMP
403 /* We don't know what the alignment requirements of the
404 runtime's jmp_buf has. Overestimate. */
405 DECL_ALIGN (f_jbuf) = BIGGEST_ALIGNMENT;
406 DECL_USER_ALIGN (f_jbuf) = 1;
407 #endif
408 DECL_FIELD_CONTEXT (f_jbuf) = sjlj_fc_type_node;
410 TYPE_FIELDS (sjlj_fc_type_node) = f_prev;
411 TREE_CHAIN (f_prev) = f_cs;
412 TREE_CHAIN (f_cs) = f_data;
413 TREE_CHAIN (f_data) = f_per;
414 TREE_CHAIN (f_per) = f_lsda;
415 TREE_CHAIN (f_lsda) = f_jbuf;
417 layout_type (sjlj_fc_type_node);
419 /* Cache the interesting field offsets so that we have
420 easy access from rtl. */
421 sjlj_fc_call_site_ofs
422 = (tree_low_cst (DECL_FIELD_OFFSET (f_cs), 1)
423 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_cs), 1) / BITS_PER_UNIT);
424 sjlj_fc_data_ofs
425 = (tree_low_cst (DECL_FIELD_OFFSET (f_data), 1)
426 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_data), 1) / BITS_PER_UNIT);
427 sjlj_fc_personality_ofs
428 = (tree_low_cst (DECL_FIELD_OFFSET (f_per), 1)
429 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_per), 1) / BITS_PER_UNIT);
430 sjlj_fc_lsda_ofs
431 = (tree_low_cst (DECL_FIELD_OFFSET (f_lsda), 1)
432 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_lsda), 1) / BITS_PER_UNIT);
433 sjlj_fc_jbuf_ofs
434 = (tree_low_cst (DECL_FIELD_OFFSET (f_jbuf), 1)
435 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_jbuf), 1) / BITS_PER_UNIT);
439 void
440 init_eh_for_function (void)
442 cfun->eh = ggc_alloc_cleared (sizeof (struct eh_status));
445 /* Routines to generate the exception tree somewhat directly.
446 These are used from tree-eh.c when processing exception related
447 nodes during tree optimization. */
449 static struct eh_region *
450 gen_eh_region (enum eh_region_type type, struct eh_region *outer)
452 struct eh_region *new;
454 #ifdef ENABLE_CHECKING
455 gcc_assert (doing_eh (0));
456 #endif
458 /* Insert a new blank region as a leaf in the tree. */
459 new = ggc_alloc_cleared (sizeof (*new));
460 new->type = type;
461 new->outer = outer;
462 if (outer)
464 new->next_peer = outer->inner;
465 outer->inner = new;
467 else
469 new->next_peer = cfun->eh->region_tree;
470 cfun->eh->region_tree = new;
473 new->region_number = ++cfun->eh->last_region_number;
475 return new;
478 struct eh_region *
479 gen_eh_region_cleanup (struct eh_region *outer, struct eh_region *prev_try)
481 struct eh_region *cleanup = gen_eh_region (ERT_CLEANUP, outer);
482 cleanup->u.cleanup.prev_try = prev_try;
483 return cleanup;
486 struct eh_region *
487 gen_eh_region_try (struct eh_region *outer)
489 return gen_eh_region (ERT_TRY, outer);
492 struct eh_region *
493 gen_eh_region_catch (struct eh_region *t, tree type_or_list)
495 struct eh_region *c, *l;
496 tree type_list, type_node;
498 /* Ensure to always end up with a type list to normalize further
499 processing, then register each type against the runtime types map. */
500 type_list = type_or_list;
501 if (type_or_list)
503 if (TREE_CODE (type_or_list) != TREE_LIST)
504 type_list = tree_cons (NULL_TREE, type_or_list, NULL_TREE);
506 type_node = type_list;
507 for (; type_node; type_node = TREE_CHAIN (type_node))
508 add_type_for_runtime (TREE_VALUE (type_node));
511 c = gen_eh_region (ERT_CATCH, t->outer);
512 c->u.catch.type_list = type_list;
513 l = t->u.try.last_catch;
514 c->u.catch.prev_catch = l;
515 if (l)
516 l->u.catch.next_catch = c;
517 else
518 t->u.try.catch = c;
519 t->u.try.last_catch = c;
521 return c;
524 struct eh_region *
525 gen_eh_region_allowed (struct eh_region *outer, tree allowed)
527 struct eh_region *region = gen_eh_region (ERT_ALLOWED_EXCEPTIONS, outer);
528 region->u.allowed.type_list = allowed;
530 for (; allowed ; allowed = TREE_CHAIN (allowed))
531 add_type_for_runtime (TREE_VALUE (allowed));
533 return region;
536 struct eh_region *
537 gen_eh_region_must_not_throw (struct eh_region *outer)
539 return gen_eh_region (ERT_MUST_NOT_THROW, outer);
543 get_eh_region_number (struct eh_region *region)
545 return region->region_number;
548 bool
549 get_eh_region_may_contain_throw (struct eh_region *region)
551 return region->may_contain_throw;
554 tree
555 get_eh_region_tree_label (struct eh_region *region)
557 return region->tree_label;
560 void
561 set_eh_region_tree_label (struct eh_region *region, tree lab)
563 region->tree_label = lab;
566 void
567 expand_resx_expr (tree exp)
569 int region_nr = TREE_INT_CST_LOW (TREE_OPERAND (exp, 0));
570 struct eh_region *reg = VEC_index (eh_region,
571 cfun->eh->region_array, region_nr);
573 gcc_assert (!reg->resume);
574 reg->resume = emit_jump_insn (gen_rtx_RESX (VOIDmode, region_nr));
575 emit_barrier ();
578 /* Note that the current EH region (if any) may contain a throw, or a
579 call to a function which itself may contain a throw. */
581 void
582 note_eh_region_may_contain_throw (struct eh_region *region)
584 while (region && !region->may_contain_throw)
586 region->may_contain_throw = 1;
587 region = region->outer;
591 void
592 note_current_region_may_contain_throw (void)
594 note_eh_region_may_contain_throw (cfun->eh->cur_region);
598 /* Return an rtl expression for a pointer to the exception object
599 within a handler. */
602 get_exception_pointer (struct function *fun)
604 rtx exc_ptr = fun->eh->exc_ptr;
605 if (fun == cfun && ! exc_ptr)
607 exc_ptr = gen_reg_rtx (ptr_mode);
608 fun->eh->exc_ptr = exc_ptr;
610 return exc_ptr;
613 /* Return an rtl expression for the exception dispatch filter
614 within a handler. */
617 get_exception_filter (struct function *fun)
619 rtx filter = fun->eh->filter;
620 if (fun == cfun && ! filter)
622 filter = gen_reg_rtx (targetm.eh_return_filter_mode ());
623 fun->eh->filter = filter;
625 return filter;
628 /* This section is for the exception handling specific optimization pass. */
630 /* Random access the exception region tree. */
632 void
633 collect_eh_region_array (void)
635 struct eh_region *i;
637 i = cfun->eh->region_tree;
638 if (! i)
639 return;
641 VEC_safe_grow (eh_region, gc, cfun->eh->region_array,
642 cfun->eh->last_region_number + 1);
643 VEC_replace (eh_region, cfun->eh->region_array, 0, 0);
645 while (1)
647 VEC_replace (eh_region, cfun->eh->region_array, i->region_number, i);
649 /* If there are sub-regions, process them. */
650 if (i->inner)
651 i = i->inner;
652 /* If there are peers, process them. */
653 else if (i->next_peer)
654 i = i->next_peer;
655 /* Otherwise, step back up the tree to the next peer. */
656 else
658 do {
659 i = i->outer;
660 if (i == NULL)
661 return;
662 } while (i->next_peer == NULL);
663 i = i->next_peer;
668 /* Remove all regions whose labels are not reachable from insns. */
670 static void
671 remove_unreachable_regions (rtx insns)
673 int i, *uid_region_num;
674 bool *reachable;
675 struct eh_region *r;
676 rtx insn;
678 uid_region_num = xcalloc (get_max_uid (), sizeof(int));
679 reachable = xcalloc (cfun->eh->last_region_number + 1, sizeof(bool));
681 for (i = cfun->eh->last_region_number; i > 0; --i)
683 r = VEC_index (eh_region, cfun->eh->region_array, i);
684 if (!r || r->region_number != i)
685 continue;
687 if (r->resume)
689 gcc_assert (!uid_region_num[INSN_UID (r->resume)]);
690 uid_region_num[INSN_UID (r->resume)] = i;
692 if (r->label)
694 gcc_assert (!uid_region_num[INSN_UID (r->label)]);
695 uid_region_num[INSN_UID (r->label)] = i;
699 for (insn = insns; insn; insn = NEXT_INSN (insn))
700 reachable[uid_region_num[INSN_UID (insn)]] = true;
702 for (i = cfun->eh->last_region_number; i > 0; --i)
704 r = VEC_index (eh_region, cfun->eh->region_array, i);
705 if (r && r->region_number == i && !reachable[i])
707 bool kill_it = true;
708 switch (r->type)
710 case ERT_THROW:
711 /* Don't remove ERT_THROW regions if their outer region
712 is reachable. */
713 if (r->outer && reachable[r->outer->region_number])
714 kill_it = false;
715 break;
717 case ERT_MUST_NOT_THROW:
718 /* MUST_NOT_THROW regions are implementable solely in the
719 runtime, but their existence continues to affect calls
720 within that region. Never delete them here. */
721 kill_it = false;
722 break;
724 case ERT_TRY:
726 /* TRY regions are reachable if any of its CATCH regions
727 are reachable. */
728 struct eh_region *c;
729 for (c = r->u.try.catch; c ; c = c->u.catch.next_catch)
730 if (reachable[c->region_number])
732 kill_it = false;
733 break;
735 break;
738 default:
739 break;
742 if (kill_it)
743 remove_eh_handler (r);
747 free (reachable);
748 free (uid_region_num);
751 /* Set up EH labels for RTL. */
753 void
754 convert_from_eh_region_ranges (void)
756 rtx insns = get_insns ();
757 int i, n = cfun->eh->last_region_number;
759 /* Most of the work is already done at the tree level. All we need to
760 do is collect the rtl labels that correspond to the tree labels that
761 collect the rtl labels that correspond to the tree labels
762 we allocated earlier. */
763 for (i = 1; i <= n; ++i)
765 struct eh_region *region;
767 region = VEC_index (eh_region, cfun->eh->region_array, i);
768 if (region && region->tree_label)
769 region->label = DECL_RTL_IF_SET (region->tree_label);
772 remove_unreachable_regions (insns);
775 static void
776 add_ehl_entry (rtx label, struct eh_region *region)
778 struct ehl_map_entry **slot, *entry;
780 LABEL_PRESERVE_P (label) = 1;
782 entry = ggc_alloc (sizeof (*entry));
783 entry->label = label;
784 entry->region = region;
786 slot = (struct ehl_map_entry **)
787 htab_find_slot (cfun->eh->exception_handler_label_map, entry, INSERT);
789 /* Before landing pad creation, each exception handler has its own
790 label. After landing pad creation, the exception handlers may
791 share landing pads. This is ok, since maybe_remove_eh_handler
792 only requires the 1-1 mapping before landing pad creation. */
793 gcc_assert (!*slot || cfun->eh->built_landing_pads);
795 *slot = entry;
798 void
799 find_exception_handler_labels (void)
801 int i;
803 if (cfun->eh->exception_handler_label_map)
804 htab_empty (cfun->eh->exception_handler_label_map);
805 else
807 /* ??? The expansion factor here (3/2) must be greater than the htab
808 occupancy factor (4/3) to avoid unnecessary resizing. */
809 cfun->eh->exception_handler_label_map
810 = htab_create_ggc (cfun->eh->last_region_number * 3 / 2,
811 ehl_hash, ehl_eq, NULL);
814 if (cfun->eh->region_tree == NULL)
815 return;
817 for (i = cfun->eh->last_region_number; i > 0; --i)
819 struct eh_region *region;
820 rtx lab;
822 region = VEC_index (eh_region, cfun->eh->region_array, i);
823 if (! region || region->region_number != i)
824 continue;
825 if (cfun->eh->built_landing_pads)
826 lab = region->landing_pad;
827 else
828 lab = region->label;
830 if (lab)
831 add_ehl_entry (lab, region);
834 /* For sjlj exceptions, need the return label to remain live until
835 after landing pad generation. */
836 if (USING_SJLJ_EXCEPTIONS && ! cfun->eh->built_landing_pads)
837 add_ehl_entry (return_label, NULL);
840 /* Returns true if the current function has exception handling regions. */
842 bool
843 current_function_has_exception_handlers (void)
845 int i;
847 for (i = cfun->eh->last_region_number; i > 0; --i)
849 struct eh_region *region;
851 region = VEC_index (eh_region, cfun->eh->region_array, i);
852 if (region
853 && region->region_number == i
854 && region->type != ERT_THROW)
855 return true;
858 return false;
861 static struct eh_region *
862 duplicate_eh_region_1 (struct eh_region *o)
864 struct eh_region *n = ggc_alloc_cleared (sizeof (struct eh_region));
866 *n = *o;
868 n->region_number = o->region_number + cfun->eh->last_region_number;
869 VEC_replace (eh_region, cfun->eh->region_array, n->region_number, n);
870 gcc_assert (!o->aka);
872 return n;
875 static void
876 duplicate_eh_region_2 (struct eh_region *o, struct eh_region **n_array,
877 struct eh_region *prev_try)
879 struct eh_region *n = n_array[o->region_number];
881 switch (n->type)
883 case ERT_TRY:
884 if (o->u.try.catch)
885 n->u.try.catch = n_array[o->u.try.catch->region_number];
886 if (o->u.try.last_catch)
887 n->u.try.last_catch = n_array[o->u.try.last_catch->region_number];
888 break;
890 case ERT_CATCH:
891 if (o->u.catch.next_catch)
892 n->u.catch.next_catch = n_array[o->u.catch.next_catch->region_number];
893 if (o->u.catch.prev_catch)
894 n->u.catch.prev_catch = n_array[o->u.catch.prev_catch->region_number];
895 break;
897 case ERT_CLEANUP:
898 if (o->u.cleanup.prev_try)
899 n->u.cleanup.prev_try = n_array[o->u.cleanup.prev_try->region_number];
900 else
901 n->u.cleanup.prev_try = prev_try;
902 break;
904 default:
905 break;
908 if (o->outer)
909 n->outer = n_array[o->outer->region_number];
910 if (o->inner)
911 n->inner = n_array[o->inner->region_number];
912 if (o->next_peer)
913 n->next_peer = n_array[o->next_peer->region_number];
916 /* Duplicate the EH regions of IFUN into current function, root the tree in
917 OUTER_REGION and remap labels using MAP callback. */
919 duplicate_eh_regions (struct function *ifun, duplicate_eh_regions_map map,
920 void *data, int outer_region)
922 int ifun_last_region_number = ifun->eh->last_region_number;
923 struct eh_region **n_array, *root, *cur, *prev_try;
924 int i;
926 if (ifun_last_region_number == 0 || !ifun->eh->region_tree)
927 return 0;
929 n_array = xcalloc (ifun_last_region_number + 1, sizeof (*n_array));
930 VEC_safe_grow (eh_region, gc, cfun->eh->region_array,
931 cfun->eh->last_region_number + 1 + ifun_last_region_number);
933 /* We might've created new cfun->eh->region_array so zero out nonexisting region 0. */
934 VEC_replace (eh_region, cfun->eh->region_array, 0, 0);
936 for (i = cfun->eh->last_region_number + 1;
937 i < cfun->eh->last_region_number + 1 + ifun_last_region_number; i++)
938 VEC_replace (eh_region, cfun->eh->region_array, i, 0);
940 /* Search for the containing ERT_TRY region to fix up
941 the prev_try short-cuts for ERT_CLEANUP regions. */
942 prev_try = NULL;
943 if (outer_region > 0)
944 for (prev_try = VEC_index (eh_region, cfun->eh->region_array, outer_region);
945 prev_try && prev_try->type != ERT_TRY;
946 prev_try = prev_try->outer)
949 for (i = 1; i <= ifun_last_region_number; ++i)
951 cur = VEC_index (eh_region, ifun->eh->region_array, i);
952 if (!cur || cur->region_number != i)
953 continue;
954 n_array[i] = duplicate_eh_region_1 (cur);
955 if (cur->tree_label)
957 tree newlabel = map (cur->tree_label, data);
958 n_array[i]->tree_label = newlabel;
960 else
961 n_array[i]->tree_label = NULL;
963 for (i = 1; i <= ifun_last_region_number; ++i)
965 cur = VEC_index (eh_region, ifun->eh->region_array, i);
966 if (!cur || cur->region_number != i)
967 continue;
968 duplicate_eh_region_2 (cur, n_array, prev_try);
971 root = n_array[ifun->eh->region_tree->region_number];
972 gcc_assert (root->outer == NULL);
973 if (outer_region > 0)
975 struct eh_region *cur
976 = VEC_index (eh_region, cfun->eh->region_array, outer_region);
977 struct eh_region *p = cur->inner;
979 if (p)
981 while (p->next_peer)
982 p = p->next_peer;
983 p->next_peer = root;
985 else
986 cur->inner = root;
987 for (i = 1; i <= ifun_last_region_number; ++i)
988 if (n_array[i] && n_array[i]->outer == NULL)
989 n_array[i]->outer = cur;
991 else
993 struct eh_region *p = cfun->eh->region_tree;
994 if (p)
996 while (p->next_peer)
997 p = p->next_peer;
998 p->next_peer = root;
1000 else
1001 cfun->eh->region_tree = root;
1004 free (n_array);
1006 i = cfun->eh->last_region_number;
1007 cfun->eh->last_region_number = i + ifun_last_region_number;
1009 return i;
1012 static int
1013 t2r_eq (const void *pentry, const void *pdata)
1015 tree entry = (tree) pentry;
1016 tree data = (tree) pdata;
1018 return TREE_PURPOSE (entry) == data;
1021 static hashval_t
1022 t2r_hash (const void *pentry)
1024 tree entry = (tree) pentry;
1025 return TREE_HASH (TREE_PURPOSE (entry));
1028 static void
1029 add_type_for_runtime (tree type)
1031 tree *slot;
1033 slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
1034 TREE_HASH (type), INSERT);
1035 if (*slot == NULL)
1037 tree runtime = (*lang_eh_runtime_type) (type);
1038 *slot = tree_cons (type, runtime, NULL_TREE);
1042 static tree
1043 lookup_type_for_runtime (tree type)
1045 tree *slot;
1047 slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
1048 TREE_HASH (type), NO_INSERT);
1050 /* We should have always inserted the data earlier. */
1051 return TREE_VALUE (*slot);
1055 /* Represent an entry in @TTypes for either catch actions
1056 or exception filter actions. */
1057 struct ttypes_filter GTY(())
1059 tree t;
1060 int filter;
1063 /* Compare ENTRY (a ttypes_filter entry in the hash table) with DATA
1064 (a tree) for a @TTypes type node we are thinking about adding. */
1066 static int
1067 ttypes_filter_eq (const void *pentry, const void *pdata)
1069 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1070 tree data = (tree) pdata;
1072 return entry->t == data;
1075 static hashval_t
1076 ttypes_filter_hash (const void *pentry)
1078 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1079 return TREE_HASH (entry->t);
1082 /* Compare ENTRY with DATA (both struct ttypes_filter) for a @TTypes
1083 exception specification list we are thinking about adding. */
1084 /* ??? Currently we use the type lists in the order given. Someone
1085 should put these in some canonical order. */
1087 static int
1088 ehspec_filter_eq (const void *pentry, const void *pdata)
1090 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1091 const struct ttypes_filter *data = (const struct ttypes_filter *) pdata;
1093 return type_list_equal (entry->t, data->t);
1096 /* Hash function for exception specification lists. */
1098 static hashval_t
1099 ehspec_filter_hash (const void *pentry)
1101 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1102 hashval_t h = 0;
1103 tree list;
1105 for (list = entry->t; list ; list = TREE_CHAIN (list))
1106 h = (h << 5) + (h >> 27) + TREE_HASH (TREE_VALUE (list));
1107 return h;
1110 /* Add TYPE (which may be NULL) to cfun->eh->ttype_data, using TYPES_HASH
1111 to speed up the search. Return the filter value to be used. */
1113 static int
1114 add_ttypes_entry (htab_t ttypes_hash, tree type)
1116 struct ttypes_filter **slot, *n;
1118 slot = (struct ttypes_filter **)
1119 htab_find_slot_with_hash (ttypes_hash, type, TREE_HASH (type), INSERT);
1121 if ((n = *slot) == NULL)
1123 /* Filter value is a 1 based table index. */
1125 n = xmalloc (sizeof (*n));
1126 n->t = type;
1127 n->filter = VEC_length (tree, cfun->eh->ttype_data) + 1;
1128 *slot = n;
1130 VEC_safe_push (tree, gc, cfun->eh->ttype_data, type);
1133 return n->filter;
1136 /* Add LIST to cfun->eh->ehspec_data, using EHSPEC_HASH and TYPES_HASH
1137 to speed up the search. Return the filter value to be used. */
1139 static int
1140 add_ehspec_entry (htab_t ehspec_hash, htab_t ttypes_hash, tree list)
1142 struct ttypes_filter **slot, *n;
1143 struct ttypes_filter dummy;
1145 dummy.t = list;
1146 slot = (struct ttypes_filter **)
1147 htab_find_slot (ehspec_hash, &dummy, INSERT);
1149 if ((n = *slot) == NULL)
1151 /* Filter value is a -1 based byte index into a uleb128 buffer. */
1153 n = xmalloc (sizeof (*n));
1154 n->t = list;
1155 n->filter = -(VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data) + 1);
1156 *slot = n;
1158 /* Generate a 0 terminated list of filter values. */
1159 for (; list ; list = TREE_CHAIN (list))
1161 if (targetm.arm_eabi_unwinder)
1162 VARRAY_PUSH_TREE (cfun->eh->ehspec_data, TREE_VALUE (list));
1163 else
1165 /* Look up each type in the list and encode its filter
1166 value as a uleb128. */
1167 push_uleb128 (&cfun->eh->ehspec_data,
1168 add_ttypes_entry (ttypes_hash, TREE_VALUE (list)));
1171 if (targetm.arm_eabi_unwinder)
1172 VARRAY_PUSH_TREE (cfun->eh->ehspec_data, NULL_TREE);
1173 else
1174 VARRAY_PUSH_UCHAR (cfun->eh->ehspec_data, 0);
1177 return n->filter;
1180 /* Generate the action filter values to be used for CATCH and
1181 ALLOWED_EXCEPTIONS regions. When using dwarf2 exception regions,
1182 we use lots of landing pads, and so every type or list can share
1183 the same filter value, which saves table space. */
1185 static void
1186 assign_filter_values (void)
1188 int i;
1189 htab_t ttypes, ehspec;
1191 cfun->eh->ttype_data = VEC_alloc (tree, gc, 16);
1192 if (targetm.arm_eabi_unwinder)
1193 VARRAY_TREE_INIT (cfun->eh->ehspec_data, 64, "ehspec_data");
1194 else
1195 VARRAY_UCHAR_INIT (cfun->eh->ehspec_data, 64, "ehspec_data");
1197 ttypes = htab_create (31, ttypes_filter_hash, ttypes_filter_eq, free);
1198 ehspec = htab_create (31, ehspec_filter_hash, ehspec_filter_eq, free);
1200 for (i = cfun->eh->last_region_number; i > 0; --i)
1202 struct eh_region *r;
1204 r = VEC_index (eh_region, cfun->eh->region_array, i);
1206 /* Mind we don't process a region more than once. */
1207 if (!r || r->region_number != i)
1208 continue;
1210 switch (r->type)
1212 case ERT_CATCH:
1213 /* Whatever type_list is (NULL or true list), we build a list
1214 of filters for the region. */
1215 r->u.catch.filter_list = NULL_TREE;
1217 if (r->u.catch.type_list != NULL)
1219 /* Get a filter value for each of the types caught and store
1220 them in the region's dedicated list. */
1221 tree tp_node = r->u.catch.type_list;
1223 for (;tp_node; tp_node = TREE_CHAIN (tp_node))
1225 int flt = add_ttypes_entry (ttypes, TREE_VALUE (tp_node));
1226 tree flt_node = build_int_cst (NULL_TREE, flt);
1228 r->u.catch.filter_list
1229 = tree_cons (NULL_TREE, flt_node, r->u.catch.filter_list);
1232 else
1234 /* Get a filter value for the NULL list also since it will need
1235 an action record anyway. */
1236 int flt = add_ttypes_entry (ttypes, NULL);
1237 tree flt_node = build_int_cst (NULL_TREE, flt);
1239 r->u.catch.filter_list
1240 = tree_cons (NULL_TREE, flt_node, r->u.catch.filter_list);
1243 break;
1245 case ERT_ALLOWED_EXCEPTIONS:
1246 r->u.allowed.filter
1247 = add_ehspec_entry (ehspec, ttypes, r->u.allowed.type_list);
1248 break;
1250 default:
1251 break;
1255 htab_delete (ttypes);
1256 htab_delete (ehspec);
1259 /* Emit SEQ into basic block just before INSN (that is assumed to be
1260 first instruction of some existing BB and return the newly
1261 produced block. */
1262 static basic_block
1263 emit_to_new_bb_before (rtx seq, rtx insn)
1265 rtx last;
1266 basic_block bb;
1267 edge e;
1268 edge_iterator ei;
1270 /* If there happens to be a fallthru edge (possibly created by cleanup_cfg
1271 call), we don't want it to go into newly created landing pad or other EH
1272 construct. */
1273 for (ei = ei_start (BLOCK_FOR_INSN (insn)->preds); (e = ei_safe_edge (ei)); )
1274 if (e->flags & EDGE_FALLTHRU)
1275 force_nonfallthru (e);
1276 else
1277 ei_next (&ei);
1278 last = emit_insn_before (seq, insn);
1279 if (BARRIER_P (last))
1280 last = PREV_INSN (last);
1281 bb = create_basic_block (seq, last, BLOCK_FOR_INSN (insn)->prev_bb);
1282 update_bb_for_insn (bb);
1283 bb->flags |= BB_SUPERBLOCK;
1284 return bb;
1287 /* Generate the code to actually handle exceptions, which will follow the
1288 landing pads. */
1290 static void
1291 build_post_landing_pads (void)
1293 int i;
1295 for (i = cfun->eh->last_region_number; i > 0; --i)
1297 struct eh_region *region;
1298 rtx seq;
1300 region = VEC_index (eh_region, cfun->eh->region_array, i);
1301 /* Mind we don't process a region more than once. */
1302 if (!region || region->region_number != i)
1303 continue;
1305 switch (region->type)
1307 case ERT_TRY:
1308 /* ??? Collect the set of all non-overlapping catch handlers
1309 all the way up the chain until blocked by a cleanup. */
1310 /* ??? Outer try regions can share landing pads with inner
1311 try regions if the types are completely non-overlapping,
1312 and there are no intervening cleanups. */
1314 region->post_landing_pad = gen_label_rtx ();
1316 start_sequence ();
1318 emit_label (region->post_landing_pad);
1320 /* ??? It is mighty inconvenient to call back into the
1321 switch statement generation code in expand_end_case.
1322 Rapid prototyping sez a sequence of ifs. */
1324 struct eh_region *c;
1325 for (c = region->u.try.catch; c ; c = c->u.catch.next_catch)
1327 if (c->u.catch.type_list == NULL)
1328 emit_jump (c->label);
1329 else
1331 /* Need for one cmp/jump per type caught. Each type
1332 list entry has a matching entry in the filter list
1333 (see assign_filter_values). */
1334 tree tp_node = c->u.catch.type_list;
1335 tree flt_node = c->u.catch.filter_list;
1337 for (; tp_node; )
1339 emit_cmp_and_jump_insns
1340 (cfun->eh->filter,
1341 GEN_INT (tree_low_cst (TREE_VALUE (flt_node), 0)),
1342 EQ, NULL_RTX,
1343 targetm.eh_return_filter_mode (), 0, c->label);
1345 tp_node = TREE_CHAIN (tp_node);
1346 flt_node = TREE_CHAIN (flt_node);
1352 /* We delay the generation of the _Unwind_Resume until we generate
1353 landing pads. We emit a marker here so as to get good control
1354 flow data in the meantime. */
1355 region->resume
1356 = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
1357 emit_barrier ();
1359 seq = get_insns ();
1360 end_sequence ();
1362 emit_to_new_bb_before (seq, region->u.try.catch->label);
1364 break;
1366 case ERT_ALLOWED_EXCEPTIONS:
1367 region->post_landing_pad = gen_label_rtx ();
1369 start_sequence ();
1371 emit_label (region->post_landing_pad);
1373 emit_cmp_and_jump_insns (cfun->eh->filter,
1374 GEN_INT (region->u.allowed.filter),
1375 EQ, NULL_RTX,
1376 targetm.eh_return_filter_mode (), 0, region->label);
1378 /* We delay the generation of the _Unwind_Resume until we generate
1379 landing pads. We emit a marker here so as to get good control
1380 flow data in the meantime. */
1381 region->resume
1382 = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
1383 emit_barrier ();
1385 seq = get_insns ();
1386 end_sequence ();
1388 emit_to_new_bb_before (seq, region->label);
1389 break;
1391 case ERT_CLEANUP:
1392 case ERT_MUST_NOT_THROW:
1393 region->post_landing_pad = region->label;
1394 break;
1396 case ERT_CATCH:
1397 case ERT_THROW:
1398 /* Nothing to do. */
1399 break;
1401 default:
1402 gcc_unreachable ();
1407 /* Replace RESX patterns with jumps to the next handler if any, or calls to
1408 _Unwind_Resume otherwise. */
1410 static void
1411 connect_post_landing_pads (void)
1413 int i;
1415 for (i = cfun->eh->last_region_number; i > 0; --i)
1417 struct eh_region *region;
1418 struct eh_region *outer;
1419 rtx seq;
1420 rtx barrier;
1422 region = VEC_index (eh_region, cfun->eh->region_array, i);
1423 /* Mind we don't process a region more than once. */
1424 if (!region || region->region_number != i)
1425 continue;
1427 /* If there is no RESX, or it has been deleted by flow, there's
1428 nothing to fix up. */
1429 if (! region->resume || INSN_DELETED_P (region->resume))
1430 continue;
1432 /* Search for another landing pad in this function. */
1433 for (outer = region->outer; outer ; outer = outer->outer)
1434 if (outer->post_landing_pad)
1435 break;
1437 start_sequence ();
1439 if (outer)
1441 edge e;
1442 basic_block src, dest;
1444 emit_jump (outer->post_landing_pad);
1445 src = BLOCK_FOR_INSN (region->resume);
1446 dest = BLOCK_FOR_INSN (outer->post_landing_pad);
1447 while (EDGE_COUNT (src->succs) > 0)
1448 remove_edge (EDGE_SUCC (src, 0));
1449 e = make_edge (src, dest, 0);
1450 e->probability = REG_BR_PROB_BASE;
1451 e->count = src->count;
1453 else
1455 emit_library_call (unwind_resume_libfunc, LCT_THROW,
1456 VOIDmode, 1, cfun->eh->exc_ptr, ptr_mode);
1458 /* What we just emitted was a throwing libcall, so it got a
1459 barrier automatically added after it. If the last insn in
1460 the libcall sequence isn't the barrier, it's because the
1461 target emits multiple insns for a call, and there are insns
1462 after the actual call insn (which are redundant and would be
1463 optimized away). The barrier is inserted exactly after the
1464 call insn, so let's go get that and delete the insns after
1465 it, because below we need the barrier to be the last insn in
1466 the sequence. */
1467 delete_insns_since (NEXT_INSN (last_call_insn ()));
1470 seq = get_insns ();
1471 end_sequence ();
1472 barrier = emit_insn_before (seq, region->resume);
1473 /* Avoid duplicate barrier. */
1474 gcc_assert (BARRIER_P (barrier));
1475 delete_insn (barrier);
1476 delete_insn (region->resume);
1478 /* ??? From tree-ssa we can wind up with catch regions whose
1479 label is not instantiated, but whose resx is present. Now
1480 that we've dealt with the resx, kill the region. */
1481 if (region->label == NULL && region->type == ERT_CLEANUP)
1482 remove_eh_handler (region);
1487 static void
1488 dw2_build_landing_pads (void)
1490 int i;
1491 unsigned int j;
1493 for (i = cfun->eh->last_region_number; i > 0; --i)
1495 struct eh_region *region;
1496 rtx seq;
1497 basic_block bb;
1498 bool clobbers_hard_regs = false;
1499 edge e;
1501 region = VEC_index (eh_region, cfun->eh->region_array, i);
1502 /* Mind we don't process a region more than once. */
1503 if (!region || region->region_number != i)
1504 continue;
1506 if (region->type != ERT_CLEANUP
1507 && region->type != ERT_TRY
1508 && region->type != ERT_ALLOWED_EXCEPTIONS)
1509 continue;
1511 start_sequence ();
1513 region->landing_pad = gen_label_rtx ();
1514 emit_label (region->landing_pad);
1516 #ifdef HAVE_exception_receiver
1517 if (HAVE_exception_receiver)
1518 emit_insn (gen_exception_receiver ());
1519 else
1520 #endif
1521 #ifdef HAVE_nonlocal_goto_receiver
1522 if (HAVE_nonlocal_goto_receiver)
1523 emit_insn (gen_nonlocal_goto_receiver ());
1524 else
1525 #endif
1526 { /* Nothing */ }
1528 /* If the eh_return data registers are call-saved, then we
1529 won't have considered them clobbered from the call that
1530 threw. But we need to clobber all hard registers, since
1531 df.c assumes that call clobbers are not definitions
1532 (normally it is invalid to use call-clobbered register
1533 after the call). */
1534 for (j = 0; ; ++j)
1536 unsigned r = EH_RETURN_DATA_REGNO (j);
1537 if (r == INVALID_REGNUM)
1538 break;
1540 emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, r)));
1541 clobbers_hard_regs = true;
1544 if (clobbers_hard_regs)
1546 /* @@@ This is a kludge. Not all machine descriptions define a
1547 blockage insn, but we must not allow the code we just generated
1548 to be reordered by scheduling. So emit an ASM_INPUT to act as
1549 blockage insn. */
1550 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
1553 emit_move_insn (cfun->eh->exc_ptr,
1554 gen_rtx_REG (ptr_mode, EH_RETURN_DATA_REGNO (0)));
1555 emit_move_insn (cfun->eh->filter,
1556 gen_rtx_REG (targetm.eh_return_filter_mode (),
1557 EH_RETURN_DATA_REGNO (1)));
1559 seq = get_insns ();
1560 end_sequence ();
1562 bb = emit_to_new_bb_before (seq, region->post_landing_pad);
1563 e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
1564 e->count = bb->count;
1565 e->probability = REG_BR_PROB_BASE;
1570 struct sjlj_lp_info
1572 int directly_reachable;
1573 int action_index;
1574 int dispatch_index;
1575 int call_site_index;
1578 static bool
1579 sjlj_find_directly_reachable_regions (struct sjlj_lp_info *lp_info)
1581 rtx insn;
1582 bool found_one = false;
1584 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
1586 struct eh_region *region;
1587 enum reachable_code rc;
1588 tree type_thrown;
1589 rtx note;
1591 if (! INSN_P (insn))
1592 continue;
1594 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1595 if (!note || INTVAL (XEXP (note, 0)) <= 0)
1596 continue;
1598 region = VEC_index (eh_region, cfun->eh->region_array, INTVAL (XEXP (note, 0)));
1600 type_thrown = NULL_TREE;
1601 if (region->type == ERT_THROW)
1603 type_thrown = region->u.throw.type;
1604 region = region->outer;
1607 /* Find the first containing region that might handle the exception.
1608 That's the landing pad to which we will transfer control. */
1609 rc = RNL_NOT_CAUGHT;
1610 for (; region; region = region->outer)
1612 rc = reachable_next_level (region, type_thrown, NULL);
1613 if (rc != RNL_NOT_CAUGHT)
1614 break;
1616 if (rc == RNL_MAYBE_CAUGHT || rc == RNL_CAUGHT)
1618 lp_info[region->region_number].directly_reachable = 1;
1619 found_one = true;
1623 return found_one;
1626 static void
1627 sjlj_assign_call_site_values (rtx dispatch_label, struct sjlj_lp_info *lp_info)
1629 htab_t ar_hash;
1630 int i, index;
1632 /* First task: build the action table. */
1634 VARRAY_UCHAR_INIT (cfun->eh->action_record_data, 64, "action_record_data");
1635 ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
1637 for (i = cfun->eh->last_region_number; i > 0; --i)
1638 if (lp_info[i].directly_reachable)
1640 struct eh_region *r = VEC_index (eh_region, cfun->eh->region_array, i);
1642 r->landing_pad = dispatch_label;
1643 lp_info[i].action_index = collect_one_action_chain (ar_hash, r);
1644 if (lp_info[i].action_index != -1)
1645 cfun->uses_eh_lsda = 1;
1648 htab_delete (ar_hash);
1650 /* Next: assign dispatch values. In dwarf2 terms, this would be the
1651 landing pad label for the region. For sjlj though, there is one
1652 common landing pad from which we dispatch to the post-landing pads.
1654 A region receives a dispatch index if it is directly reachable
1655 and requires in-function processing. Regions that share post-landing
1656 pads may share dispatch indices. */
1657 /* ??? Post-landing pad sharing doesn't actually happen at the moment
1658 (see build_post_landing_pads) so we don't bother checking for it. */
1660 index = 0;
1661 for (i = cfun->eh->last_region_number; i > 0; --i)
1662 if (lp_info[i].directly_reachable)
1663 lp_info[i].dispatch_index = index++;
1665 /* Finally: assign call-site values. If dwarf2 terms, this would be
1666 the region number assigned by convert_to_eh_region_ranges, but
1667 handles no-action and must-not-throw differently. */
1669 call_site_base = 1;
1670 for (i = cfun->eh->last_region_number; i > 0; --i)
1671 if (lp_info[i].directly_reachable)
1673 int action = lp_info[i].action_index;
1675 /* Map must-not-throw to otherwise unused call-site index 0. */
1676 if (action == -2)
1677 index = 0;
1678 /* Map no-action to otherwise unused call-site index -1. */
1679 else if (action == -1)
1680 index = -1;
1681 /* Otherwise, look it up in the table. */
1682 else
1683 index = add_call_site (GEN_INT (lp_info[i].dispatch_index), action);
1685 lp_info[i].call_site_index = index;
1689 static void
1690 sjlj_mark_call_sites (struct sjlj_lp_info *lp_info)
1692 int last_call_site = -2;
1693 rtx insn, mem;
1695 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
1697 struct eh_region *region;
1698 int this_call_site;
1699 rtx note, before, p;
1701 /* Reset value tracking at extended basic block boundaries. */
1702 if (LABEL_P (insn))
1703 last_call_site = -2;
1705 if (! INSN_P (insn))
1706 continue;
1708 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1709 if (!note)
1711 /* Calls (and trapping insns) without notes are outside any
1712 exception handling region in this function. Mark them as
1713 no action. */
1714 if (CALL_P (insn)
1715 || (flag_non_call_exceptions
1716 && may_trap_p (PATTERN (insn))))
1717 this_call_site = -1;
1718 else
1719 continue;
1721 else
1723 /* Calls that are known to not throw need not be marked. */
1724 if (INTVAL (XEXP (note, 0)) <= 0)
1725 continue;
1727 region = VEC_index (eh_region, cfun->eh->region_array, INTVAL (XEXP (note, 0)));
1728 this_call_site = lp_info[region->region_number].call_site_index;
1731 if (this_call_site == last_call_site)
1732 continue;
1734 /* Don't separate a call from it's argument loads. */
1735 before = insn;
1736 if (CALL_P (insn))
1737 before = find_first_parameter_load (insn, NULL_RTX);
1739 start_sequence ();
1740 mem = adjust_address (cfun->eh->sjlj_fc, TYPE_MODE (integer_type_node),
1741 sjlj_fc_call_site_ofs);
1742 emit_move_insn (mem, GEN_INT (this_call_site));
1743 p = get_insns ();
1744 end_sequence ();
1746 emit_insn_before (p, before);
1747 last_call_site = this_call_site;
1751 /* Construct the SjLj_Function_Context. */
1753 static void
1754 sjlj_emit_function_enter (rtx dispatch_label)
1756 rtx fn_begin, fc, mem, seq;
1757 bool fn_begin_outside_block;
1759 fc = cfun->eh->sjlj_fc;
1761 start_sequence ();
1763 /* We're storing this libcall's address into memory instead of
1764 calling it directly. Thus, we must call assemble_external_libcall
1765 here, as we can not depend on emit_library_call to do it for us. */
1766 assemble_external_libcall (eh_personality_libfunc);
1767 mem = adjust_address (fc, Pmode, sjlj_fc_personality_ofs);
1768 emit_move_insn (mem, eh_personality_libfunc);
1770 mem = adjust_address (fc, Pmode, sjlj_fc_lsda_ofs);
1771 if (cfun->uses_eh_lsda)
1773 char buf[20];
1774 rtx sym;
1776 ASM_GENERATE_INTERNAL_LABEL (buf, "LLSDA", current_function_funcdef_no);
1777 sym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
1778 SYMBOL_REF_FLAGS (sym) = SYMBOL_FLAG_LOCAL;
1779 emit_move_insn (mem, sym);
1781 else
1782 emit_move_insn (mem, const0_rtx);
1784 #ifdef DONT_USE_BUILTIN_SETJMP
1786 rtx x, note;
1787 x = emit_library_call_value (setjmp_libfunc, NULL_RTX, LCT_RETURNS_TWICE,
1788 TYPE_MODE (integer_type_node), 1,
1789 plus_constant (XEXP (fc, 0),
1790 sjlj_fc_jbuf_ofs), Pmode);
1792 note = emit_note (NOTE_INSN_EXPECTED_VALUE);
1793 NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, x, const0_rtx);
1795 emit_cmp_and_jump_insns (x, const0_rtx, NE, 0,
1796 TYPE_MODE (integer_type_node), 0, dispatch_label);
1798 #else
1799 expand_builtin_setjmp_setup (plus_constant (XEXP (fc, 0), sjlj_fc_jbuf_ofs),
1800 dispatch_label);
1801 #endif
1803 emit_library_call (unwind_sjlj_register_libfunc, LCT_NORMAL, VOIDmode,
1804 1, XEXP (fc, 0), Pmode);
1806 seq = get_insns ();
1807 end_sequence ();
1809 /* ??? Instead of doing this at the beginning of the function,
1810 do this in a block that is at loop level 0 and dominates all
1811 can_throw_internal instructions. */
1813 fn_begin_outside_block = true;
1814 for (fn_begin = get_insns (); ; fn_begin = NEXT_INSN (fn_begin))
1815 if (NOTE_P (fn_begin))
1817 if (NOTE_LINE_NUMBER (fn_begin) == NOTE_INSN_FUNCTION_BEG)
1818 break;
1819 else if (NOTE_LINE_NUMBER (fn_begin) == NOTE_INSN_BASIC_BLOCK)
1820 fn_begin_outside_block = false;
1823 if (fn_begin_outside_block)
1824 insert_insn_on_edge (seq, single_succ_edge (ENTRY_BLOCK_PTR));
1825 else
1826 emit_insn_after (seq, fn_begin);
1829 /* Call back from expand_function_end to know where we should put
1830 the call to unwind_sjlj_unregister_libfunc if needed. */
1832 void
1833 sjlj_emit_function_exit_after (rtx after)
1835 cfun->eh->sjlj_exit_after = after;
1838 static void
1839 sjlj_emit_function_exit (void)
1841 rtx seq;
1842 edge e;
1843 edge_iterator ei;
1845 start_sequence ();
1847 emit_library_call (unwind_sjlj_unregister_libfunc, LCT_NORMAL, VOIDmode,
1848 1, XEXP (cfun->eh->sjlj_fc, 0), Pmode);
1850 seq = get_insns ();
1851 end_sequence ();
1853 /* ??? Really this can be done in any block at loop level 0 that
1854 post-dominates all can_throw_internal instructions. This is
1855 the last possible moment. */
1857 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
1858 if (e->flags & EDGE_FALLTHRU)
1859 break;
1860 if (e)
1862 rtx insn;
1864 /* Figure out whether the place we are supposed to insert libcall
1865 is inside the last basic block or after it. In the other case
1866 we need to emit to edge. */
1867 gcc_assert (e->src->next_bb == EXIT_BLOCK_PTR);
1868 for (insn = BB_HEAD (e->src); ; insn = NEXT_INSN (insn))
1870 if (insn == cfun->eh->sjlj_exit_after)
1872 if (LABEL_P (insn))
1873 insn = NEXT_INSN (insn);
1874 emit_insn_after (seq, insn);
1875 return;
1877 if (insn == BB_END (e->src))
1878 break;
1880 insert_insn_on_edge (seq, e);
1884 static void
1885 sjlj_emit_dispatch_table (rtx dispatch_label, struct sjlj_lp_info *lp_info)
1887 int i, first_reachable;
1888 rtx mem, dispatch, seq, fc;
1889 rtx before;
1890 basic_block bb;
1891 edge e;
1893 fc = cfun->eh->sjlj_fc;
1895 start_sequence ();
1897 emit_label (dispatch_label);
1899 #ifndef DONT_USE_BUILTIN_SETJMP
1900 expand_builtin_setjmp_receiver (dispatch_label);
1901 #endif
1903 /* Load up dispatch index, exc_ptr and filter values from the
1904 function context. */
1905 mem = adjust_address (fc, TYPE_MODE (integer_type_node),
1906 sjlj_fc_call_site_ofs);
1907 dispatch = copy_to_reg (mem);
1909 mem = adjust_address (fc, word_mode, sjlj_fc_data_ofs);
1910 if (word_mode != ptr_mode)
1912 #ifdef POINTERS_EXTEND_UNSIGNED
1913 mem = convert_memory_address (ptr_mode, mem);
1914 #else
1915 mem = convert_to_mode (ptr_mode, mem, 0);
1916 #endif
1918 emit_move_insn (cfun->eh->exc_ptr, mem);
1920 mem = adjust_address (fc, word_mode, sjlj_fc_data_ofs + UNITS_PER_WORD);
1921 emit_move_insn (cfun->eh->filter, mem);
1923 /* Jump to one of the directly reachable regions. */
1924 /* ??? This really ought to be using a switch statement. */
1926 first_reachable = 0;
1927 for (i = cfun->eh->last_region_number; i > 0; --i)
1929 if (! lp_info[i].directly_reachable)
1930 continue;
1932 if (! first_reachable)
1934 first_reachable = i;
1935 continue;
1938 emit_cmp_and_jump_insns (dispatch, GEN_INT (lp_info[i].dispatch_index),
1939 EQ, NULL_RTX, TYPE_MODE (integer_type_node), 0,
1940 ((struct eh_region *)VEC_index (eh_region, cfun->eh->region_array, i))
1941 ->post_landing_pad);
1944 seq = get_insns ();
1945 end_sequence ();
1947 before = (((struct eh_region *)VEC_index (eh_region, cfun->eh->region_array, first_reachable))
1948 ->post_landing_pad);
1950 bb = emit_to_new_bb_before (seq, before);
1951 e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
1952 e->count = bb->count;
1953 e->probability = REG_BR_PROB_BASE;
1956 static void
1957 sjlj_build_landing_pads (void)
1959 struct sjlj_lp_info *lp_info;
1961 lp_info = xcalloc (cfun->eh->last_region_number + 1,
1962 sizeof (struct sjlj_lp_info));
1964 if (sjlj_find_directly_reachable_regions (lp_info))
1966 rtx dispatch_label = gen_label_rtx ();
1968 cfun->eh->sjlj_fc
1969 = assign_stack_local (TYPE_MODE (sjlj_fc_type_node),
1970 int_size_in_bytes (sjlj_fc_type_node),
1971 TYPE_ALIGN (sjlj_fc_type_node));
1973 sjlj_assign_call_site_values (dispatch_label, lp_info);
1974 sjlj_mark_call_sites (lp_info);
1976 sjlj_emit_function_enter (dispatch_label);
1977 sjlj_emit_dispatch_table (dispatch_label, lp_info);
1978 sjlj_emit_function_exit ();
1981 free (lp_info);
1984 void
1985 finish_eh_generation (void)
1987 basic_block bb;
1989 /* Nothing to do if no regions created. */
1990 if (cfun->eh->region_tree == NULL)
1991 return;
1993 /* The object here is to provide find_basic_blocks with detailed
1994 information (via reachable_handlers) on how exception control
1995 flows within the function. In this first pass, we can include
1996 type information garnered from ERT_THROW and ERT_ALLOWED_EXCEPTIONS
1997 regions, and hope that it will be useful in deleting unreachable
1998 handlers. Subsequently, we will generate landing pads which will
1999 connect many of the handlers, and then type information will not
2000 be effective. Still, this is a win over previous implementations. */
2002 /* These registers are used by the landing pads. Make sure they
2003 have been generated. */
2004 get_exception_pointer (cfun);
2005 get_exception_filter (cfun);
2007 /* Construct the landing pads. */
2009 assign_filter_values ();
2010 build_post_landing_pads ();
2011 connect_post_landing_pads ();
2012 if (USING_SJLJ_EXCEPTIONS)
2013 sjlj_build_landing_pads ();
2014 else
2015 dw2_build_landing_pads ();
2017 cfun->eh->built_landing_pads = 1;
2019 /* We've totally changed the CFG. Start over. */
2020 find_exception_handler_labels ();
2021 break_superblocks ();
2022 if (USING_SJLJ_EXCEPTIONS)
2023 commit_edge_insertions ();
2024 FOR_EACH_BB (bb)
2026 edge e;
2027 edge_iterator ei;
2028 bool eh = false;
2029 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2031 if (e->flags & EDGE_EH)
2033 remove_edge (e);
2034 eh = true;
2036 else
2037 ei_next (&ei);
2039 if (eh)
2040 rtl_make_eh_edge (NULL, bb, BB_END (bb));
2044 static hashval_t
2045 ehl_hash (const void *pentry)
2047 struct ehl_map_entry *entry = (struct ehl_map_entry *) pentry;
2049 /* 2^32 * ((sqrt(5) - 1) / 2) */
2050 const hashval_t scaled_golden_ratio = 0x9e3779b9;
2051 return CODE_LABEL_NUMBER (entry->label) * scaled_golden_ratio;
2054 static int
2055 ehl_eq (const void *pentry, const void *pdata)
2057 struct ehl_map_entry *entry = (struct ehl_map_entry *) pentry;
2058 struct ehl_map_entry *data = (struct ehl_map_entry *) pdata;
2060 return entry->label == data->label;
2063 /* This section handles removing dead code for flow. */
2065 /* Remove LABEL from exception_handler_label_map. */
2067 static void
2068 remove_exception_handler_label (rtx label)
2070 struct ehl_map_entry **slot, tmp;
2072 /* If exception_handler_label_map was not built yet,
2073 there is nothing to do. */
2074 if (cfun->eh->exception_handler_label_map == NULL)
2075 return;
2077 tmp.label = label;
2078 slot = (struct ehl_map_entry **)
2079 htab_find_slot (cfun->eh->exception_handler_label_map, &tmp, NO_INSERT);
2080 gcc_assert (slot);
2082 htab_clear_slot (cfun->eh->exception_handler_label_map, (void **) slot);
2085 /* Splice REGION from the region tree etc. */
2087 static void
2088 remove_eh_handler (struct eh_region *region)
2090 struct eh_region **pp, **pp_start, *p, *outer, *inner;
2091 rtx lab;
2093 /* For the benefit of efficiently handling REG_EH_REGION notes,
2094 replace this region in the region array with its containing
2095 region. Note that previous region deletions may result in
2096 multiple copies of this region in the array, so we have a
2097 list of alternate numbers by which we are known. */
2099 outer = region->outer;
2100 VEC_replace (eh_region, cfun->eh->region_array, region->region_number, outer);
2101 if (region->aka)
2103 unsigned i;
2104 bitmap_iterator bi;
2106 EXECUTE_IF_SET_IN_BITMAP (region->aka, 0, i, bi)
2108 VEC_replace (eh_region, cfun->eh->region_array, i, outer);
2112 if (outer)
2114 if (!outer->aka)
2115 outer->aka = BITMAP_GGC_ALLOC ();
2116 if (region->aka)
2117 bitmap_ior_into (outer->aka, region->aka);
2118 bitmap_set_bit (outer->aka, region->region_number);
2121 if (cfun->eh->built_landing_pads)
2122 lab = region->landing_pad;
2123 else
2124 lab = region->label;
2125 if (lab)
2126 remove_exception_handler_label (lab);
2128 if (outer)
2129 pp_start = &outer->inner;
2130 else
2131 pp_start = &cfun->eh->region_tree;
2132 for (pp = pp_start, p = *pp; p != region; pp = &p->next_peer, p = *pp)
2133 continue;
2134 *pp = region->next_peer;
2136 inner = region->inner;
2137 if (inner)
2139 for (p = inner; p->next_peer ; p = p->next_peer)
2140 p->outer = outer;
2141 p->outer = outer;
2143 p->next_peer = *pp_start;
2144 *pp_start = inner;
2147 if (region->type == ERT_CATCH)
2149 struct eh_region *try, *next, *prev;
2151 for (try = region->next_peer;
2152 try->type == ERT_CATCH;
2153 try = try->next_peer)
2154 continue;
2155 gcc_assert (try->type == ERT_TRY);
2157 next = region->u.catch.next_catch;
2158 prev = region->u.catch.prev_catch;
2160 if (next)
2161 next->u.catch.prev_catch = prev;
2162 else
2163 try->u.try.last_catch = prev;
2164 if (prev)
2165 prev->u.catch.next_catch = next;
2166 else
2168 try->u.try.catch = next;
2169 if (! next)
2170 remove_eh_handler (try);
2175 /* LABEL heads a basic block that is about to be deleted. If this
2176 label corresponds to an exception region, we may be able to
2177 delete the region. */
2179 void
2180 maybe_remove_eh_handler (rtx label)
2182 struct ehl_map_entry **slot, tmp;
2183 struct eh_region *region;
2185 /* ??? After generating landing pads, it's not so simple to determine
2186 if the region data is completely unused. One must examine the
2187 landing pad and the post landing pad, and whether an inner try block
2188 is referencing the catch handlers directly. */
2189 if (cfun->eh->built_landing_pads)
2190 return;
2192 tmp.label = label;
2193 slot = (struct ehl_map_entry **)
2194 htab_find_slot (cfun->eh->exception_handler_label_map, &tmp, NO_INSERT);
2195 if (! slot)
2196 return;
2197 region = (*slot)->region;
2198 if (! region)
2199 return;
2201 /* Flow will want to remove MUST_NOT_THROW regions as unreachable
2202 because there is no path to the fallback call to terminate.
2203 But the region continues to affect call-site data until there
2204 are no more contained calls, which we don't see here. */
2205 if (region->type == ERT_MUST_NOT_THROW)
2207 htab_clear_slot (cfun->eh->exception_handler_label_map, (void **) slot);
2208 region->label = NULL_RTX;
2210 else
2211 remove_eh_handler (region);
2214 /* Invokes CALLBACK for every exception handler label. Only used by old
2215 loop hackery; should not be used by new code. */
2217 void
2218 for_each_eh_label (void (*callback) (rtx))
2220 htab_traverse (cfun->eh->exception_handler_label_map, for_each_eh_label_1,
2221 (void *) &callback);
2224 static int
2225 for_each_eh_label_1 (void **pentry, void *data)
2227 struct ehl_map_entry *entry = *(struct ehl_map_entry **)pentry;
2228 void (*callback) (rtx) = *(void (**) (rtx)) data;
2230 (*callback) (entry->label);
2231 return 1;
2234 /* Invoke CALLBACK for every exception region in the current function. */
2236 void
2237 for_each_eh_region (void (*callback) (struct eh_region *))
2239 int i, n = cfun->eh->last_region_number;
2240 for (i = 1; i <= n; ++i)
2242 struct eh_region *region;
2244 region = VEC_index (eh_region, cfun->eh->region_array, i);
2245 if (region)
2246 (*callback) (region);
2250 /* This section describes CFG exception edges for flow. */
2252 /* For communicating between calls to reachable_next_level. */
2253 struct reachable_info
2255 tree types_caught;
2256 tree types_allowed;
2257 void (*callback) (struct eh_region *, void *);
2258 void *callback_data;
2259 bool saw_any_handlers;
2262 /* A subroutine of reachable_next_level. Return true if TYPE, or a
2263 base class of TYPE, is in HANDLED. */
2265 static int
2266 check_handled (tree handled, tree type)
2268 tree t;
2270 /* We can check for exact matches without front-end help. */
2271 if (! lang_eh_type_covers)
2273 for (t = handled; t ; t = TREE_CHAIN (t))
2274 if (TREE_VALUE (t) == type)
2275 return 1;
2277 else
2279 for (t = handled; t ; t = TREE_CHAIN (t))
2280 if ((*lang_eh_type_covers) (TREE_VALUE (t), type))
2281 return 1;
2284 return 0;
2287 /* A subroutine of reachable_next_level. If we are collecting a list
2288 of handlers, add one. After landing pad generation, reference
2289 it instead of the handlers themselves. Further, the handlers are
2290 all wired together, so by referencing one, we've got them all.
2291 Before landing pad generation we reference each handler individually.
2293 LP_REGION contains the landing pad; REGION is the handler. */
2295 static void
2296 add_reachable_handler (struct reachable_info *info,
2297 struct eh_region *lp_region, struct eh_region *region)
2299 if (! info)
2300 return;
2302 info->saw_any_handlers = true;
2304 if (cfun->eh->built_landing_pads)
2305 info->callback (lp_region, info->callback_data);
2306 else
2307 info->callback (region, info->callback_data);
2310 /* Process one level of exception regions for reachability.
2311 If TYPE_THROWN is non-null, then it is the *exact* type being
2312 propagated. If INFO is non-null, then collect handler labels
2313 and caught/allowed type information between invocations. */
2315 static enum reachable_code
2316 reachable_next_level (struct eh_region *region, tree type_thrown,
2317 struct reachable_info *info)
2319 switch (region->type)
2321 case ERT_CLEANUP:
2322 /* Before landing-pad generation, we model control flow
2323 directly to the individual handlers. In this way we can
2324 see that catch handler types may shadow one another. */
2325 add_reachable_handler (info, region, region);
2326 return RNL_MAYBE_CAUGHT;
2328 case ERT_TRY:
2330 struct eh_region *c;
2331 enum reachable_code ret = RNL_NOT_CAUGHT;
2333 for (c = region->u.try.catch; c ; c = c->u.catch.next_catch)
2335 /* A catch-all handler ends the search. */
2336 if (c->u.catch.type_list == NULL)
2338 add_reachable_handler (info, region, c);
2339 return RNL_CAUGHT;
2342 if (type_thrown)
2344 /* If we have at least one type match, end the search. */
2345 tree tp_node = c->u.catch.type_list;
2347 for (; tp_node; tp_node = TREE_CHAIN (tp_node))
2349 tree type = TREE_VALUE (tp_node);
2351 if (type == type_thrown
2352 || (lang_eh_type_covers
2353 && (*lang_eh_type_covers) (type, type_thrown)))
2355 add_reachable_handler (info, region, c);
2356 return RNL_CAUGHT;
2360 /* If we have definitive information of a match failure,
2361 the catch won't trigger. */
2362 if (lang_eh_type_covers)
2363 return RNL_NOT_CAUGHT;
2366 /* At this point, we either don't know what type is thrown or
2367 don't have front-end assistance to help deciding if it is
2368 covered by one of the types in the list for this region.
2370 We'd then like to add this region to the list of reachable
2371 handlers since it is indeed potentially reachable based on the
2372 information we have.
2374 Actually, this handler is for sure not reachable if all the
2375 types it matches have already been caught. That is, it is only
2376 potentially reachable if at least one of the types it catches
2377 has not been previously caught. */
2379 if (! info)
2380 ret = RNL_MAYBE_CAUGHT;
2381 else
2383 tree tp_node = c->u.catch.type_list;
2384 bool maybe_reachable = false;
2386 /* Compute the potential reachability of this handler and
2387 update the list of types caught at the same time. */
2388 for (; tp_node; tp_node = TREE_CHAIN (tp_node))
2390 tree type = TREE_VALUE (tp_node);
2392 if (! check_handled (info->types_caught, type))
2394 info->types_caught
2395 = tree_cons (NULL, type, info->types_caught);
2397 maybe_reachable = true;
2401 if (maybe_reachable)
2403 add_reachable_handler (info, region, c);
2405 /* ??? If the catch type is a base class of every allowed
2406 type, then we know we can stop the search. */
2407 ret = RNL_MAYBE_CAUGHT;
2412 return ret;
2415 case ERT_ALLOWED_EXCEPTIONS:
2416 /* An empty list of types definitely ends the search. */
2417 if (region->u.allowed.type_list == NULL_TREE)
2419 add_reachable_handler (info, region, region);
2420 return RNL_CAUGHT;
2423 /* Collect a list of lists of allowed types for use in detecting
2424 when a catch may be transformed into a catch-all. */
2425 if (info)
2426 info->types_allowed = tree_cons (NULL_TREE,
2427 region->u.allowed.type_list,
2428 info->types_allowed);
2430 /* If we have definitive information about the type hierarchy,
2431 then we can tell if the thrown type will pass through the
2432 filter. */
2433 if (type_thrown && lang_eh_type_covers)
2435 if (check_handled (region->u.allowed.type_list, type_thrown))
2436 return RNL_NOT_CAUGHT;
2437 else
2439 add_reachable_handler (info, region, region);
2440 return RNL_CAUGHT;
2444 add_reachable_handler (info, region, region);
2445 return RNL_MAYBE_CAUGHT;
2447 case ERT_CATCH:
2448 /* Catch regions are handled by their controlling try region. */
2449 return RNL_NOT_CAUGHT;
2451 case ERT_MUST_NOT_THROW:
2452 /* Here we end our search, since no exceptions may propagate.
2453 If we've touched down at some landing pad previous, then the
2454 explicit function call we generated may be used. Otherwise
2455 the call is made by the runtime.
2457 Before inlining, do not perform this optimization. We may
2458 inline a subroutine that contains handlers, and that will
2459 change the value of saw_any_handlers. */
2461 if ((info && info->saw_any_handlers) || !cfun->after_inlining)
2463 add_reachable_handler (info, region, region);
2464 return RNL_CAUGHT;
2466 else
2467 return RNL_BLOCKED;
2469 case ERT_THROW:
2470 case ERT_UNKNOWN:
2471 /* Shouldn't see these here. */
2472 gcc_unreachable ();
2473 break;
2474 default:
2475 gcc_unreachable ();
2479 /* Invoke CALLBACK on each region reachable from REGION_NUMBER. */
2481 void
2482 foreach_reachable_handler (int region_number, bool is_resx,
2483 void (*callback) (struct eh_region *, void *),
2484 void *callback_data)
2486 struct reachable_info info;
2487 struct eh_region *region;
2488 tree type_thrown;
2490 memset (&info, 0, sizeof (info));
2491 info.callback = callback;
2492 info.callback_data = callback_data;
2494 region = VEC_index (eh_region, cfun->eh->region_array, region_number);
2496 type_thrown = NULL_TREE;
2497 if (is_resx)
2499 /* A RESX leaves a region instead of entering it. Thus the
2500 region itself may have been deleted out from under us. */
2501 if (region == NULL)
2502 return;
2503 region = region->outer;
2505 else if (region->type == ERT_THROW)
2507 type_thrown = region->u.throw.type;
2508 region = region->outer;
2511 while (region)
2513 if (reachable_next_level (region, type_thrown, &info) >= RNL_CAUGHT)
2514 break;
2515 /* If we have processed one cleanup, there is no point in
2516 processing any more of them. Each cleanup will have an edge
2517 to the next outer cleanup region, so the flow graph will be
2518 accurate. */
2519 if (region->type == ERT_CLEANUP)
2520 region = region->u.cleanup.prev_try;
2521 else
2522 region = region->outer;
2526 /* Retrieve a list of labels of exception handlers which can be
2527 reached by a given insn. */
2529 static void
2530 arh_to_landing_pad (struct eh_region *region, void *data)
2532 rtx *p_handlers = data;
2533 if (! *p_handlers)
2534 *p_handlers = alloc_INSN_LIST (region->landing_pad, NULL_RTX);
2537 static void
2538 arh_to_label (struct eh_region *region, void *data)
2540 rtx *p_handlers = data;
2541 *p_handlers = alloc_INSN_LIST (region->label, *p_handlers);
2545 reachable_handlers (rtx insn)
2547 bool is_resx = false;
2548 rtx handlers = NULL;
2549 int region_number;
2551 if (JUMP_P (insn)
2552 && GET_CODE (PATTERN (insn)) == RESX)
2554 region_number = XINT (PATTERN (insn), 0);
2555 is_resx = true;
2557 else
2559 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2560 if (!note || INTVAL (XEXP (note, 0)) <= 0)
2561 return NULL;
2562 region_number = INTVAL (XEXP (note, 0));
2565 foreach_reachable_handler (region_number, is_resx,
2566 (cfun->eh->built_landing_pads
2567 ? arh_to_landing_pad
2568 : arh_to_label),
2569 &handlers);
2571 return handlers;
2574 /* Determine if the given INSN can throw an exception that is caught
2575 within the function. */
2577 bool
2578 can_throw_internal_1 (int region_number, bool is_resx)
2580 struct eh_region *region;
2581 tree type_thrown;
2583 region = VEC_index (eh_region, cfun->eh->region_array, region_number);
2585 type_thrown = NULL_TREE;
2586 if (is_resx)
2587 region = region->outer;
2588 else if (region->type == ERT_THROW)
2590 type_thrown = region->u.throw.type;
2591 region = region->outer;
2594 /* If this exception is ignored by each and every containing region,
2595 then control passes straight out. The runtime may handle some
2596 regions, which also do not require processing internally. */
2597 for (; region; region = region->outer)
2599 enum reachable_code how = reachable_next_level (region, type_thrown, 0);
2600 if (how == RNL_BLOCKED)
2601 return false;
2602 if (how != RNL_NOT_CAUGHT)
2603 return true;
2606 return false;
2609 bool
2610 can_throw_internal (rtx insn)
2612 rtx note;
2614 if (! INSN_P (insn))
2615 return false;
2617 if (JUMP_P (insn)
2618 && GET_CODE (PATTERN (insn)) == RESX
2619 && XINT (PATTERN (insn), 0) > 0)
2620 return can_throw_internal_1 (XINT (PATTERN (insn), 0), true);
2622 if (NONJUMP_INSN_P (insn)
2623 && GET_CODE (PATTERN (insn)) == SEQUENCE)
2624 insn = XVECEXP (PATTERN (insn), 0, 0);
2626 /* Every insn that might throw has an EH_REGION note. */
2627 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2628 if (!note || INTVAL (XEXP (note, 0)) <= 0)
2629 return false;
2631 return can_throw_internal_1 (INTVAL (XEXP (note, 0)), false);
2634 /* Determine if the given INSN can throw an exception that is
2635 visible outside the function. */
2637 bool
2638 can_throw_external_1 (int region_number, bool is_resx)
2640 struct eh_region *region;
2641 tree type_thrown;
2643 region = VEC_index (eh_region, cfun->eh->region_array, region_number);
2645 type_thrown = NULL_TREE;
2646 if (is_resx)
2647 region = region->outer;
2648 else if (region->type == ERT_THROW)
2650 type_thrown = region->u.throw.type;
2651 region = region->outer;
2654 /* If the exception is caught or blocked by any containing region,
2655 then it is not seen by any calling function. */
2656 for (; region ; region = region->outer)
2657 if (reachable_next_level (region, type_thrown, NULL) >= RNL_CAUGHT)
2658 return false;
2660 return true;
2663 bool
2664 can_throw_external (rtx insn)
2666 rtx note;
2668 if (! INSN_P (insn))
2669 return false;
2671 if (JUMP_P (insn)
2672 && GET_CODE (PATTERN (insn)) == RESX
2673 && XINT (PATTERN (insn), 0) > 0)
2674 return can_throw_external_1 (XINT (PATTERN (insn), 0), true);
2676 if (NONJUMP_INSN_P (insn)
2677 && GET_CODE (PATTERN (insn)) == SEQUENCE)
2678 insn = XVECEXP (PATTERN (insn), 0, 0);
2680 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2681 if (!note)
2683 /* Calls (and trapping insns) without notes are outside any
2684 exception handling region in this function. We have to
2685 assume it might throw. Given that the front end and middle
2686 ends mark known NOTHROW functions, this isn't so wildly
2687 inaccurate. */
2688 return (CALL_P (insn)
2689 || (flag_non_call_exceptions
2690 && may_trap_p (PATTERN (insn))));
2692 if (INTVAL (XEXP (note, 0)) <= 0)
2693 return false;
2695 return can_throw_external_1 (INTVAL (XEXP (note, 0)), false);
2698 /* Set TREE_NOTHROW and cfun->all_throwers_are_sibcalls. */
2700 void
2701 set_nothrow_function_flags (void)
2703 rtx insn;
2705 TREE_NOTHROW (current_function_decl) = 1;
2707 /* Assume cfun->all_throwers_are_sibcalls until we encounter
2708 something that can throw an exception. We specifically exempt
2709 CALL_INSNs that are SIBLING_CALL_P, as these are really jumps,
2710 and can't throw. Most CALL_INSNs are not SIBLING_CALL_P, so this
2711 is optimistic. */
2713 cfun->all_throwers_are_sibcalls = 1;
2715 if (! flag_exceptions)
2716 return;
2718 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2719 if (can_throw_external (insn))
2721 TREE_NOTHROW (current_function_decl) = 0;
2723 if (!CALL_P (insn) || !SIBLING_CALL_P (insn))
2725 cfun->all_throwers_are_sibcalls = 0;
2726 return;
2730 for (insn = current_function_epilogue_delay_list; insn;
2731 insn = XEXP (insn, 1))
2732 if (can_throw_external (insn))
2734 TREE_NOTHROW (current_function_decl) = 0;
2736 if (!CALL_P (insn) || !SIBLING_CALL_P (insn))
2738 cfun->all_throwers_are_sibcalls = 0;
2739 return;
2744 struct tree_opt_pass pass_set_nothrow_function_flags =
2746 NULL, /* name */
2747 NULL, /* gate */
2748 set_nothrow_function_flags, /* execute */
2749 NULL, /* sub */
2750 NULL, /* next */
2751 0, /* static_pass_number */
2752 0, /* tv_id */
2753 0, /* properties_required */
2754 0, /* properties_provided */
2755 0, /* properties_destroyed */
2756 0, /* todo_flags_start */
2757 0, /* todo_flags_finish */
2758 0 /* letter */
2762 /* Various hooks for unwind library. */
2764 /* Do any necessary initialization to access arbitrary stack frames.
2765 On the SPARC, this means flushing the register windows. */
2767 void
2768 expand_builtin_unwind_init (void)
2770 /* Set this so all the registers get saved in our frame; we need to be
2771 able to copy the saved values for any registers from frames we unwind. */
2772 current_function_has_nonlocal_label = 1;
2774 #ifdef SETUP_FRAME_ADDRESSES
2775 SETUP_FRAME_ADDRESSES ();
2776 #endif
2780 expand_builtin_eh_return_data_regno (tree arglist)
2782 tree which = TREE_VALUE (arglist);
2783 unsigned HOST_WIDE_INT iwhich;
2785 if (TREE_CODE (which) != INTEGER_CST)
2787 error ("argument of %<__builtin_eh_return_regno%> must be constant");
2788 return constm1_rtx;
2791 iwhich = tree_low_cst (which, 1);
2792 iwhich = EH_RETURN_DATA_REGNO (iwhich);
2793 if (iwhich == INVALID_REGNUM)
2794 return constm1_rtx;
2796 #ifdef DWARF_FRAME_REGNUM
2797 iwhich = DWARF_FRAME_REGNUM (iwhich);
2798 #else
2799 iwhich = DBX_REGISTER_NUMBER (iwhich);
2800 #endif
2802 return GEN_INT (iwhich);
2805 /* Given a value extracted from the return address register or stack slot,
2806 return the actual address encoded in that value. */
2809 expand_builtin_extract_return_addr (tree addr_tree)
2811 rtx addr = expand_expr (addr_tree, NULL_RTX, Pmode, 0);
2813 if (GET_MODE (addr) != Pmode
2814 && GET_MODE (addr) != VOIDmode)
2816 #ifdef POINTERS_EXTEND_UNSIGNED
2817 addr = convert_memory_address (Pmode, addr);
2818 #else
2819 addr = convert_to_mode (Pmode, addr, 0);
2820 #endif
2823 /* First mask out any unwanted bits. */
2824 #ifdef MASK_RETURN_ADDR
2825 expand_and (Pmode, addr, MASK_RETURN_ADDR, addr);
2826 #endif
2828 /* Then adjust to find the real return address. */
2829 #if defined (RETURN_ADDR_OFFSET)
2830 addr = plus_constant (addr, RETURN_ADDR_OFFSET);
2831 #endif
2833 return addr;
2836 /* Given an actual address in addr_tree, do any necessary encoding
2837 and return the value to be stored in the return address register or
2838 stack slot so the epilogue will return to that address. */
2841 expand_builtin_frob_return_addr (tree addr_tree)
2843 rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, 0);
2845 addr = convert_memory_address (Pmode, addr);
2847 #ifdef RETURN_ADDR_OFFSET
2848 addr = force_reg (Pmode, addr);
2849 addr = plus_constant (addr, -RETURN_ADDR_OFFSET);
2850 #endif
2852 return addr;
2855 /* Set up the epilogue with the magic bits we'll need to return to the
2856 exception handler. */
2858 void
2859 expand_builtin_eh_return (tree stackadj_tree ATTRIBUTE_UNUSED,
2860 tree handler_tree)
2862 rtx tmp;
2864 #ifdef EH_RETURN_STACKADJ_RTX
2865 tmp = expand_expr (stackadj_tree, cfun->eh->ehr_stackadj, VOIDmode, 0);
2866 tmp = convert_memory_address (Pmode, tmp);
2867 if (!cfun->eh->ehr_stackadj)
2868 cfun->eh->ehr_stackadj = copy_to_reg (tmp);
2869 else if (tmp != cfun->eh->ehr_stackadj)
2870 emit_move_insn (cfun->eh->ehr_stackadj, tmp);
2871 #endif
2873 tmp = expand_expr (handler_tree, cfun->eh->ehr_handler, VOIDmode, 0);
2874 tmp = convert_memory_address (Pmode, tmp);
2875 if (!cfun->eh->ehr_handler)
2876 cfun->eh->ehr_handler = copy_to_reg (tmp);
2877 else if (tmp != cfun->eh->ehr_handler)
2878 emit_move_insn (cfun->eh->ehr_handler, tmp);
2880 if (!cfun->eh->ehr_label)
2881 cfun->eh->ehr_label = gen_label_rtx ();
2882 emit_jump (cfun->eh->ehr_label);
2885 void
2886 expand_eh_return (void)
2888 rtx around_label;
2890 if (! cfun->eh->ehr_label)
2891 return;
2893 current_function_calls_eh_return = 1;
2895 #ifdef EH_RETURN_STACKADJ_RTX
2896 emit_move_insn (EH_RETURN_STACKADJ_RTX, const0_rtx);
2897 #endif
2899 around_label = gen_label_rtx ();
2900 emit_jump (around_label);
2902 emit_label (cfun->eh->ehr_label);
2903 clobber_return_register ();
2905 #ifdef EH_RETURN_STACKADJ_RTX
2906 emit_move_insn (EH_RETURN_STACKADJ_RTX, cfun->eh->ehr_stackadj);
2907 #endif
2909 #ifdef HAVE_eh_return
2910 if (HAVE_eh_return)
2911 emit_insn (gen_eh_return (cfun->eh->ehr_handler));
2912 else
2913 #endif
2915 #ifdef EH_RETURN_HANDLER_RTX
2916 emit_move_insn (EH_RETURN_HANDLER_RTX, cfun->eh->ehr_handler);
2917 #else
2918 error ("__builtin_eh_return not supported on this target");
2919 #endif
2922 emit_label (around_label);
2925 /* Convert a ptr_mode address ADDR_TREE to a Pmode address controlled by
2926 POINTERS_EXTEND_UNSIGNED and return it. */
2929 expand_builtin_extend_pointer (tree addr_tree)
2931 rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, 0);
2932 int extend;
2934 #ifdef POINTERS_EXTEND_UNSIGNED
2935 extend = POINTERS_EXTEND_UNSIGNED;
2936 #else
2937 /* The previous EH code did an unsigned extend by default, so we do this also
2938 for consistency. */
2939 extend = 1;
2940 #endif
2942 return convert_modes (word_mode, ptr_mode, addr, extend);
2945 /* In the following functions, we represent entries in the action table
2946 as 1-based indices. Special cases are:
2948 0: null action record, non-null landing pad; implies cleanups
2949 -1: null action record, null landing pad; implies no action
2950 -2: no call-site entry; implies must_not_throw
2951 -3: we have yet to process outer regions
2953 Further, no special cases apply to the "next" field of the record.
2954 For next, 0 means end of list. */
2956 struct action_record
2958 int offset;
2959 int filter;
2960 int next;
2963 static int
2964 action_record_eq (const void *pentry, const void *pdata)
2966 const struct action_record *entry = (const struct action_record *) pentry;
2967 const struct action_record *data = (const struct action_record *) pdata;
2968 return entry->filter == data->filter && entry->next == data->next;
2971 static hashval_t
2972 action_record_hash (const void *pentry)
2974 const struct action_record *entry = (const struct action_record *) pentry;
2975 return entry->next * 1009 + entry->filter;
2978 static int
2979 add_action_record (htab_t ar_hash, int filter, int next)
2981 struct action_record **slot, *new, tmp;
2983 tmp.filter = filter;
2984 tmp.next = next;
2985 slot = (struct action_record **) htab_find_slot (ar_hash, &tmp, INSERT);
2987 if ((new = *slot) == NULL)
2989 new = xmalloc (sizeof (*new));
2990 new->offset = VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data) + 1;
2991 new->filter = filter;
2992 new->next = next;
2993 *slot = new;
2995 /* The filter value goes in untouched. The link to the next
2996 record is a "self-relative" byte offset, or zero to indicate
2997 that there is no next record. So convert the absolute 1 based
2998 indices we've been carrying around into a displacement. */
3000 push_sleb128 (&cfun->eh->action_record_data, filter);
3001 if (next)
3002 next -= VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data) + 1;
3003 push_sleb128 (&cfun->eh->action_record_data, next);
3006 return new->offset;
3009 static int
3010 collect_one_action_chain (htab_t ar_hash, struct eh_region *region)
3012 struct eh_region *c;
3013 int next;
3015 /* If we've reached the top of the region chain, then we have
3016 no actions, and require no landing pad. */
3017 if (region == NULL)
3018 return -1;
3020 switch (region->type)
3022 case ERT_CLEANUP:
3023 /* A cleanup adds a zero filter to the beginning of the chain, but
3024 there are special cases to look out for. If there are *only*
3025 cleanups along a path, then it compresses to a zero action.
3026 Further, if there are multiple cleanups along a path, we only
3027 need to represent one of them, as that is enough to trigger
3028 entry to the landing pad at runtime. */
3029 next = collect_one_action_chain (ar_hash, region->outer);
3030 if (next <= 0)
3031 return 0;
3032 for (c = region->outer; c ; c = c->outer)
3033 if (c->type == ERT_CLEANUP)
3034 return next;
3035 return add_action_record (ar_hash, 0, next);
3037 case ERT_TRY:
3038 /* Process the associated catch regions in reverse order.
3039 If there's a catch-all handler, then we don't need to
3040 search outer regions. Use a magic -3 value to record
3041 that we haven't done the outer search. */
3042 next = -3;
3043 for (c = region->u.try.last_catch; c ; c = c->u.catch.prev_catch)
3045 if (c->u.catch.type_list == NULL)
3047 /* Retrieve the filter from the head of the filter list
3048 where we have stored it (see assign_filter_values). */
3049 int filter
3050 = TREE_INT_CST_LOW (TREE_VALUE (c->u.catch.filter_list));
3052 next = add_action_record (ar_hash, filter, 0);
3054 else
3056 /* Once the outer search is done, trigger an action record for
3057 each filter we have. */
3058 tree flt_node;
3060 if (next == -3)
3062 next = collect_one_action_chain (ar_hash, region->outer);
3064 /* If there is no next action, terminate the chain. */
3065 if (next == -1)
3066 next = 0;
3067 /* If all outer actions are cleanups or must_not_throw,
3068 we'll have no action record for it, since we had wanted
3069 to encode these states in the call-site record directly.
3070 Add a cleanup action to the chain to catch these. */
3071 else if (next <= 0)
3072 next = add_action_record (ar_hash, 0, 0);
3075 flt_node = c->u.catch.filter_list;
3076 for (; flt_node; flt_node = TREE_CHAIN (flt_node))
3078 int filter = TREE_INT_CST_LOW (TREE_VALUE (flt_node));
3079 next = add_action_record (ar_hash, filter, next);
3083 return next;
3085 case ERT_ALLOWED_EXCEPTIONS:
3086 /* An exception specification adds its filter to the
3087 beginning of the chain. */
3088 next = collect_one_action_chain (ar_hash, region->outer);
3090 /* If there is no next action, terminate the chain. */
3091 if (next == -1)
3092 next = 0;
3093 /* If all outer actions are cleanups or must_not_throw,
3094 we'll have no action record for it, since we had wanted
3095 to encode these states in the call-site record directly.
3096 Add a cleanup action to the chain to catch these. */
3097 else if (next <= 0)
3098 next = add_action_record (ar_hash, 0, 0);
3100 return add_action_record (ar_hash, region->u.allowed.filter, next);
3102 case ERT_MUST_NOT_THROW:
3103 /* A must-not-throw region with no inner handlers or cleanups
3104 requires no call-site entry. Note that this differs from
3105 the no handler or cleanup case in that we do require an lsda
3106 to be generated. Return a magic -2 value to record this. */
3107 return -2;
3109 case ERT_CATCH:
3110 case ERT_THROW:
3111 /* CATCH regions are handled in TRY above. THROW regions are
3112 for optimization information only and produce no output. */
3113 return collect_one_action_chain (ar_hash, region->outer);
3115 default:
3116 gcc_unreachable ();
3120 static int
3121 add_call_site (rtx landing_pad, int action)
3123 struct call_site_record *data = cfun->eh->call_site_data;
3124 int used = cfun->eh->call_site_data_used;
3125 int size = cfun->eh->call_site_data_size;
3127 if (used >= size)
3129 size = (size ? size * 2 : 64);
3130 data = ggc_realloc (data, sizeof (*data) * size);
3131 cfun->eh->call_site_data = data;
3132 cfun->eh->call_site_data_size = size;
3135 data[used].landing_pad = landing_pad;
3136 data[used].action = action;
3138 cfun->eh->call_site_data_used = used + 1;
3140 return used + call_site_base;
3143 /* Turn REG_EH_REGION notes back into NOTE_INSN_EH_REGION notes.
3144 The new note numbers will not refer to region numbers, but
3145 instead to call site entries. */
3147 void
3148 convert_to_eh_region_ranges (void)
3150 rtx insn, iter, note;
3151 htab_t ar_hash;
3152 int last_action = -3;
3153 rtx last_action_insn = NULL_RTX;
3154 rtx last_landing_pad = NULL_RTX;
3155 rtx first_no_action_insn = NULL_RTX;
3156 int call_site = 0;
3158 if (USING_SJLJ_EXCEPTIONS || cfun->eh->region_tree == NULL)
3159 return;
3161 VARRAY_UCHAR_INIT (cfun->eh->action_record_data, 64, "action_record_data");
3163 ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
3165 for (iter = get_insns (); iter ; iter = NEXT_INSN (iter))
3166 if (INSN_P (iter))
3168 struct eh_region *region;
3169 int this_action;
3170 rtx this_landing_pad;
3172 insn = iter;
3173 if (NONJUMP_INSN_P (insn)
3174 && GET_CODE (PATTERN (insn)) == SEQUENCE)
3175 insn = XVECEXP (PATTERN (insn), 0, 0);
3177 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3178 if (!note)
3180 if (! (CALL_P (insn)
3181 || (flag_non_call_exceptions
3182 && may_trap_p (PATTERN (insn)))))
3183 continue;
3184 this_action = -1;
3185 region = NULL;
3187 else
3189 if (INTVAL (XEXP (note, 0)) <= 0)
3190 continue;
3191 region = VEC_index (eh_region, cfun->eh->region_array, INTVAL (XEXP (note, 0)));
3192 this_action = collect_one_action_chain (ar_hash, region);
3195 /* Existence of catch handlers, or must-not-throw regions
3196 implies that an lsda is needed (even if empty). */
3197 if (this_action != -1)
3198 cfun->uses_eh_lsda = 1;
3200 /* Delay creation of region notes for no-action regions
3201 until we're sure that an lsda will be required. */
3202 else if (last_action == -3)
3204 first_no_action_insn = iter;
3205 last_action = -1;
3208 /* Cleanups and handlers may share action chains but not
3209 landing pads. Collect the landing pad for this region. */
3210 if (this_action >= 0)
3212 struct eh_region *o;
3213 for (o = region; ! o->landing_pad ; o = o->outer)
3214 continue;
3215 this_landing_pad = o->landing_pad;
3217 else
3218 this_landing_pad = NULL_RTX;
3220 /* Differing actions or landing pads implies a change in call-site
3221 info, which implies some EH_REGION note should be emitted. */
3222 if (last_action != this_action
3223 || last_landing_pad != this_landing_pad)
3225 /* If we'd not seen a previous action (-3) or the previous
3226 action was must-not-throw (-2), then we do not need an
3227 end note. */
3228 if (last_action >= -1)
3230 /* If we delayed the creation of the begin, do it now. */
3231 if (first_no_action_insn)
3233 call_site = add_call_site (NULL_RTX, 0);
3234 note = emit_note_before (NOTE_INSN_EH_REGION_BEG,
3235 first_no_action_insn);
3236 NOTE_EH_HANDLER (note) = call_site;
3237 first_no_action_insn = NULL_RTX;
3240 note = emit_note_after (NOTE_INSN_EH_REGION_END,
3241 last_action_insn);
3242 NOTE_EH_HANDLER (note) = call_site;
3245 /* If the new action is must-not-throw, then no region notes
3246 are created. */
3247 if (this_action >= -1)
3249 call_site = add_call_site (this_landing_pad,
3250 this_action < 0 ? 0 : this_action);
3251 note = emit_note_before (NOTE_INSN_EH_REGION_BEG, iter);
3252 NOTE_EH_HANDLER (note) = call_site;
3255 last_action = this_action;
3256 last_landing_pad = this_landing_pad;
3258 last_action_insn = iter;
3261 if (last_action >= -1 && ! first_no_action_insn)
3263 note = emit_note_after (NOTE_INSN_EH_REGION_END, last_action_insn);
3264 NOTE_EH_HANDLER (note) = call_site;
3267 htab_delete (ar_hash);
3270 struct tree_opt_pass pass_convert_to_eh_region_ranges =
3272 "eh-ranges", /* name */
3273 NULL, /* gate */
3274 convert_to_eh_region_ranges, /* execute */
3275 NULL, /* sub */
3276 NULL, /* next */
3277 0, /* static_pass_number */
3278 0, /* tv_id */
3279 0, /* properties_required */
3280 0, /* properties_provided */
3281 0, /* properties_destroyed */
3282 0, /* todo_flags_start */
3283 TODO_dump_func, /* todo_flags_finish */
3284 0 /* letter */
3288 static void
3289 push_uleb128 (varray_type *data_area, unsigned int value)
3293 unsigned char byte = value & 0x7f;
3294 value >>= 7;
3295 if (value)
3296 byte |= 0x80;
3297 VARRAY_PUSH_UCHAR (*data_area, byte);
3299 while (value);
3302 static void
3303 push_sleb128 (varray_type *data_area, int value)
3305 unsigned char byte;
3306 int more;
3310 byte = value & 0x7f;
3311 value >>= 7;
3312 more = ! ((value == 0 && (byte & 0x40) == 0)
3313 || (value == -1 && (byte & 0x40) != 0));
3314 if (more)
3315 byte |= 0x80;
3316 VARRAY_PUSH_UCHAR (*data_area, byte);
3318 while (more);
3322 #ifndef HAVE_AS_LEB128
3323 static int
3324 dw2_size_of_call_site_table (void)
3326 int n = cfun->eh->call_site_data_used;
3327 int size = n * (4 + 4 + 4);
3328 int i;
3330 for (i = 0; i < n; ++i)
3332 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3333 size += size_of_uleb128 (cs->action);
3336 return size;
3339 static int
3340 sjlj_size_of_call_site_table (void)
3342 int n = cfun->eh->call_site_data_used;
3343 int size = 0;
3344 int i;
3346 for (i = 0; i < n; ++i)
3348 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3349 size += size_of_uleb128 (INTVAL (cs->landing_pad));
3350 size += size_of_uleb128 (cs->action);
3353 return size;
3355 #endif
3357 static void
3358 dw2_output_call_site_table (void)
3360 int n = cfun->eh->call_site_data_used;
3361 int i;
3363 for (i = 0; i < n; ++i)
3365 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3366 char reg_start_lab[32];
3367 char reg_end_lab[32];
3368 char landing_pad_lab[32];
3370 ASM_GENERATE_INTERNAL_LABEL (reg_start_lab, "LEHB", call_site_base + i);
3371 ASM_GENERATE_INTERNAL_LABEL (reg_end_lab, "LEHE", call_site_base + i);
3373 if (cs->landing_pad)
3374 ASM_GENERATE_INTERNAL_LABEL (landing_pad_lab, "L",
3375 CODE_LABEL_NUMBER (cs->landing_pad));
3377 /* ??? Perhaps use insn length scaling if the assembler supports
3378 generic arithmetic. */
3379 /* ??? Perhaps use attr_length to choose data1 or data2 instead of
3380 data4 if the function is small enough. */
3381 #ifdef HAVE_AS_LEB128
3382 dw2_asm_output_delta_uleb128 (reg_start_lab,
3383 current_function_func_begin_label,
3384 "region %d start", i);
3385 dw2_asm_output_delta_uleb128 (reg_end_lab, reg_start_lab,
3386 "length");
3387 if (cs->landing_pad)
3388 dw2_asm_output_delta_uleb128 (landing_pad_lab,
3389 current_function_func_begin_label,
3390 "landing pad");
3391 else
3392 dw2_asm_output_data_uleb128 (0, "landing pad");
3393 #else
3394 dw2_asm_output_delta (4, reg_start_lab,
3395 current_function_func_begin_label,
3396 "region %d start", i);
3397 dw2_asm_output_delta (4, reg_end_lab, reg_start_lab, "length");
3398 if (cs->landing_pad)
3399 dw2_asm_output_delta (4, landing_pad_lab,
3400 current_function_func_begin_label,
3401 "landing pad");
3402 else
3403 dw2_asm_output_data (4, 0, "landing pad");
3404 #endif
3405 dw2_asm_output_data_uleb128 (cs->action, "action");
3408 call_site_base += n;
3411 static void
3412 sjlj_output_call_site_table (void)
3414 int n = cfun->eh->call_site_data_used;
3415 int i;
3417 for (i = 0; i < n; ++i)
3419 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3421 dw2_asm_output_data_uleb128 (INTVAL (cs->landing_pad),
3422 "region %d landing pad", i);
3423 dw2_asm_output_data_uleb128 (cs->action, "action");
3426 call_site_base += n;
3429 /* Tell assembler to switch to the section for the exception handling
3430 table. */
3432 void
3433 default_exception_section (void)
3435 if (targetm.have_named_sections)
3437 int flags;
3439 if (EH_TABLES_CAN_BE_READ_ONLY)
3441 int tt_format = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3443 flags = (! flag_pic
3444 || ((tt_format & 0x70) != DW_EH_PE_absptr
3445 && (tt_format & 0x70) != DW_EH_PE_aligned))
3446 ? 0 : SECTION_WRITE;
3448 else
3449 flags = SECTION_WRITE;
3450 named_section_flags (".gcc_except_table", flags);
3452 else if (flag_pic)
3453 data_section ();
3454 else
3455 readonly_data_section ();
3459 /* Output a reference from an exception table to the type_info object TYPE.
3460 TT_FORMAT and TT_FORMAT_SIZE descibe the DWARF encoding method used for
3461 the value. */
3463 static void
3464 output_ttype (tree type, int tt_format, int tt_format_size)
3466 rtx value;
3468 if (type == NULL_TREE)
3469 value = const0_rtx;
3470 else
3472 struct cgraph_varpool_node *node;
3474 type = lookup_type_for_runtime (type);
3475 value = expand_expr (type, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
3477 /* Let cgraph know that the rtti decl is used. Not all of the
3478 paths below go through assemble_integer, which would take
3479 care of this for us. */
3480 STRIP_NOPS (type);
3481 if (TREE_CODE (type) == ADDR_EXPR)
3483 type = TREE_OPERAND (type, 0);
3484 if (TREE_CODE (type) == VAR_DECL)
3486 node = cgraph_varpool_node (type);
3487 if (node)
3488 cgraph_varpool_mark_needed_node (node);
3491 else if (TREE_CODE (type) != INTEGER_CST)
3492 abort ();
3495 /* Allow the target to override the type table entry format. */
3496 if (targetm.asm_out.ttype (value))
3497 return;
3499 if (tt_format == DW_EH_PE_absptr || tt_format == DW_EH_PE_aligned)
3500 assemble_integer (value, tt_format_size,
3501 tt_format_size * BITS_PER_UNIT, 1);
3502 else
3503 dw2_asm_output_encoded_addr_rtx (tt_format, value, NULL);
3506 void
3507 output_function_exception_table (void)
3509 int tt_format, cs_format, lp_format, i, n;
3510 #ifdef HAVE_AS_LEB128
3511 char ttype_label[32];
3512 char cs_after_size_label[32];
3513 char cs_end_label[32];
3514 #else
3515 int call_site_len;
3516 #endif
3517 int have_tt_data;
3518 int tt_format_size = 0;
3520 /* Not all functions need anything. */
3521 if (! cfun->uses_eh_lsda)
3522 return;
3524 #ifdef TARGET_UNWIND_INFO
3525 /* TODO: Move this into target file. */
3526 assemble_external_libcall (eh_personality_libfunc);
3527 fputs ("\t.personality\t", asm_out_file);
3528 output_addr_const (asm_out_file, eh_personality_libfunc);
3529 fputs ("\n\t.handlerdata\n", asm_out_file);
3530 /* Note that varasm still thinks we're in the function's code section.
3531 The ".endp" directive that will immediately follow will take us back. */
3532 #else
3533 targetm.asm_out.exception_section ();
3534 #endif
3536 have_tt_data = (VEC_length (tree, cfun->eh->ttype_data) > 0
3537 || VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data) > 0);
3539 /* Indicate the format of the @TType entries. */
3540 if (! have_tt_data)
3541 tt_format = DW_EH_PE_omit;
3542 else
3544 tt_format = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3545 #ifdef HAVE_AS_LEB128
3546 ASM_GENERATE_INTERNAL_LABEL (ttype_label, "LLSDATT",
3547 current_function_funcdef_no);
3548 #endif
3549 tt_format_size = size_of_encoded_value (tt_format);
3551 assemble_align (tt_format_size * BITS_PER_UNIT);
3554 targetm.asm_out.internal_label (asm_out_file, "LLSDA",
3555 current_function_funcdef_no);
3557 /* The LSDA header. */
3559 /* Indicate the format of the landing pad start pointer. An omitted
3560 field implies @LPStart == @Start. */
3561 /* Currently we always put @LPStart == @Start. This field would
3562 be most useful in moving the landing pads completely out of
3563 line to another section, but it could also be used to minimize
3564 the size of uleb128 landing pad offsets. */
3565 lp_format = DW_EH_PE_omit;
3566 dw2_asm_output_data (1, lp_format, "@LPStart format (%s)",
3567 eh_data_format_name (lp_format));
3569 /* @LPStart pointer would go here. */
3571 dw2_asm_output_data (1, tt_format, "@TType format (%s)",
3572 eh_data_format_name (tt_format));
3574 #ifndef HAVE_AS_LEB128
3575 if (USING_SJLJ_EXCEPTIONS)
3576 call_site_len = sjlj_size_of_call_site_table ();
3577 else
3578 call_site_len = dw2_size_of_call_site_table ();
3579 #endif
3581 /* A pc-relative 4-byte displacement to the @TType data. */
3582 if (have_tt_data)
3584 #ifdef HAVE_AS_LEB128
3585 char ttype_after_disp_label[32];
3586 ASM_GENERATE_INTERNAL_LABEL (ttype_after_disp_label, "LLSDATTD",
3587 current_function_funcdef_no);
3588 dw2_asm_output_delta_uleb128 (ttype_label, ttype_after_disp_label,
3589 "@TType base offset");
3590 ASM_OUTPUT_LABEL (asm_out_file, ttype_after_disp_label);
3591 #else
3592 /* Ug. Alignment queers things. */
3593 unsigned int before_disp, after_disp, last_disp, disp;
3595 before_disp = 1 + 1;
3596 after_disp = (1 + size_of_uleb128 (call_site_len)
3597 + call_site_len
3598 + VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data)
3599 + (VEC_length (tree, cfun->eh->ttype_data)
3600 * tt_format_size));
3602 disp = after_disp;
3605 unsigned int disp_size, pad;
3607 last_disp = disp;
3608 disp_size = size_of_uleb128 (disp);
3609 pad = before_disp + disp_size + after_disp;
3610 if (pad % tt_format_size)
3611 pad = tt_format_size - (pad % tt_format_size);
3612 else
3613 pad = 0;
3614 disp = after_disp + pad;
3616 while (disp != last_disp);
3618 dw2_asm_output_data_uleb128 (disp, "@TType base offset");
3619 #endif
3622 /* Indicate the format of the call-site offsets. */
3623 #ifdef HAVE_AS_LEB128
3624 cs_format = DW_EH_PE_uleb128;
3625 #else
3626 cs_format = DW_EH_PE_udata4;
3627 #endif
3628 dw2_asm_output_data (1, cs_format, "call-site format (%s)",
3629 eh_data_format_name (cs_format));
3631 #ifdef HAVE_AS_LEB128
3632 ASM_GENERATE_INTERNAL_LABEL (cs_after_size_label, "LLSDACSB",
3633 current_function_funcdef_no);
3634 ASM_GENERATE_INTERNAL_LABEL (cs_end_label, "LLSDACSE",
3635 current_function_funcdef_no);
3636 dw2_asm_output_delta_uleb128 (cs_end_label, cs_after_size_label,
3637 "Call-site table length");
3638 ASM_OUTPUT_LABEL (asm_out_file, cs_after_size_label);
3639 if (USING_SJLJ_EXCEPTIONS)
3640 sjlj_output_call_site_table ();
3641 else
3642 dw2_output_call_site_table ();
3643 ASM_OUTPUT_LABEL (asm_out_file, cs_end_label);
3644 #else
3645 dw2_asm_output_data_uleb128 (call_site_len,"Call-site table length");
3646 if (USING_SJLJ_EXCEPTIONS)
3647 sjlj_output_call_site_table ();
3648 else
3649 dw2_output_call_site_table ();
3650 #endif
3652 /* ??? Decode and interpret the data for flag_debug_asm. */
3653 n = VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data);
3654 for (i = 0; i < n; ++i)
3655 dw2_asm_output_data (1, VARRAY_UCHAR (cfun->eh->action_record_data, i),
3656 (i ? NULL : "Action record table"));
3658 if (have_tt_data)
3659 assemble_align (tt_format_size * BITS_PER_UNIT);
3661 i = VEC_length (tree, cfun->eh->ttype_data);
3662 while (i-- > 0)
3664 tree type = VEC_index (tree, cfun->eh->ttype_data, i);
3665 output_ttype (type, tt_format, tt_format_size);
3668 #ifdef HAVE_AS_LEB128
3669 if (have_tt_data)
3670 ASM_OUTPUT_LABEL (asm_out_file, ttype_label);
3671 #endif
3673 /* ??? Decode and interpret the data for flag_debug_asm. */
3674 n = VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data);
3675 for (i = 0; i < n; ++i)
3677 if (targetm.arm_eabi_unwinder)
3679 tree type = VARRAY_TREE (cfun->eh->ehspec_data, i);
3680 output_ttype (type, tt_format, tt_format_size);
3682 else
3683 dw2_asm_output_data (1, VARRAY_UCHAR (cfun->eh->ehspec_data, i),
3684 (i ? NULL : "Exception specification table"));
3687 current_function_section (current_function_decl);
3690 void
3691 set_eh_throw_stmt_table (struct function *fun, struct htab *table)
3693 fun->eh->throw_stmt_table = table;
3696 htab_t
3697 get_eh_throw_stmt_table (struct function *fun)
3699 return fun->eh->throw_stmt_table;
3702 /* Dump EH information to OUT. */
3703 void
3704 dump_eh_tree (FILE *out, struct function *fun)
3706 struct eh_region *i;
3707 int depth = 0;
3708 static const char * const type_name[] = {"unknown", "cleanup", "try", "catch",
3709 "allowed_exceptions", "must_not_throw",
3710 "throw"};
3712 i = fun->eh->region_tree;
3713 if (! i)
3714 return;
3716 fprintf (out, "Eh tree:\n");
3717 while (1)
3719 fprintf (out, " %*s %i %s", depth * 2, "",
3720 i->region_number, type_name [(int)i->type]);
3721 if (i->tree_label)
3723 fprintf (out, " tree_label:");
3724 print_generic_expr (out, i->tree_label, 0);
3726 fprintf (out, "\n");
3727 /* If there are sub-regions, process them. */
3728 if (i->inner)
3729 i = i->inner, depth++;
3730 /* If there are peers, process them. */
3731 else if (i->next_peer)
3732 i = i->next_peer;
3733 /* Otherwise, step back up the tree to the next peer. */
3734 else
3736 do {
3737 i = i->outer;
3738 depth--;
3739 if (i == NULL)
3740 return;
3741 } while (i->next_peer == NULL);
3742 i = i->next_peer;
3747 /* Verify some basic invariants on EH datastructures. Could be extended to
3748 catch more. */
3749 void
3750 verify_eh_tree (struct function *fun)
3752 struct eh_region *i, *outer = NULL;
3753 bool err = false;
3754 int nvisited = 0;
3755 int count = 0;
3756 int j;
3757 int depth = 0;
3759 i = fun->eh->region_tree;
3760 if (! i)
3761 return;
3762 for (j = fun->eh->last_region_number; j > 0; --j)
3763 if ((i = VEC_index (eh_region, cfun->eh->region_array, j)))
3765 count++;
3766 if (i->region_number != j)
3768 error ("region_array is corrupted for region %i", i->region_number);
3769 err = true;
3773 while (1)
3775 if (VEC_index (eh_region, cfun->eh->region_array, i->region_number) != i)
3777 error ("region_array is corrupted for region %i", i->region_number);
3778 err = true;
3780 if (i->outer != outer)
3782 error ("outer block of region %i is wrong", i->region_number);
3783 err = true;
3785 if (i->may_contain_throw && outer && !outer->may_contain_throw)
3787 error ("region %i may contain throw and is contained in region that may not",
3788 i->region_number);
3789 err = true;
3791 if (depth < 0)
3793 error ("negative nesting depth of region %i", i->region_number);
3794 err = true;
3796 nvisited ++;
3797 /* If there are sub-regions, process them. */
3798 if (i->inner)
3799 outer = i, i = i->inner, depth++;
3800 /* If there are peers, process them. */
3801 else if (i->next_peer)
3802 i = i->next_peer;
3803 /* Otherwise, step back up the tree to the next peer. */
3804 else
3806 do {
3807 i = i->outer;
3808 depth--;
3809 if (i == NULL)
3811 if (depth != -1)
3813 error ("tree list ends on depth %i", depth + 1);
3814 err = true;
3816 if (count != nvisited)
3818 error ("array does not match the region tree");
3819 err = true;
3821 if (err)
3823 dump_eh_tree (stderr, fun);
3824 internal_error ("verify_eh_tree failed");
3826 return;
3828 outer = i->outer;
3829 } while (i->next_peer == NULL);
3830 i = i->next_peer;
3835 /* Initialize unwind_resume_libfunc. */
3837 void
3838 default_init_unwind_resume_libfunc (void)
3840 /* The default c++ routines aren't actually c++ specific, so use those. */
3841 unwind_resume_libfunc =
3842 init_one_libfunc ( USING_SJLJ_EXCEPTIONS ? "_Unwind_SjLj_Resume"
3843 : "_Unwind_Resume");
3847 static bool
3848 gate_handle_eh (void)
3850 return doing_eh (0);
3853 /* Complete generation of exception handling code. */
3854 static void
3855 rest_of_handle_eh (void)
3857 cleanup_cfg (CLEANUP_PRE_LOOP | CLEANUP_NO_INSN_DEL);
3858 finish_eh_generation ();
3859 cleanup_cfg (CLEANUP_PRE_LOOP | CLEANUP_NO_INSN_DEL);
3862 struct tree_opt_pass pass_rtl_eh =
3864 "eh", /* name */
3865 gate_handle_eh, /* gate */
3866 rest_of_handle_eh, /* execute */
3867 NULL, /* sub */
3868 NULL, /* next */
3869 0, /* static_pass_number */
3870 TV_JUMP, /* tv_id */
3871 0, /* properties_required */
3872 0, /* properties_provided */
3873 0, /* properties_destroyed */
3874 0, /* todo_flags_start */
3875 TODO_dump_func, /* todo_flags_finish */
3876 'h' /* letter */
3879 #include "gt-except.h"