PR c++/6749
[official-gcc.git] / gcc / except.c
blobd45f1b2dcda69ee8a895fab3b76efb01ea1cfe4b
1 /* Implements exception handling.
2 Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4 Contributed by Mike Stump <mrs@cygnus.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
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, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, 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"
78 /* Provide defaults for stuff that may not be defined when using
79 sjlj exceptions. */
80 #ifndef EH_RETURN_DATA_REGNO
81 #define EH_RETURN_DATA_REGNO(N) INVALID_REGNUM
82 #endif
85 /* Protect cleanup actions with must-not-throw regions, with a call
86 to the given failure handler. */
87 tree (*lang_protect_cleanup_actions) (void);
89 /* Return true if type A catches type B. */
90 int (*lang_eh_type_covers) (tree a, tree b);
92 /* Map a type to a runtime object to match type. */
93 tree (*lang_eh_runtime_type) (tree);
95 /* A hash table of label to region number. */
97 struct ehl_map_entry GTY(())
99 rtx label;
100 struct eh_region *region;
103 static GTY(()) int call_site_base;
104 static GTY ((param_is (union tree_node)))
105 htab_t type_to_runtime_map;
107 /* Describe the SjLj_Function_Context structure. */
108 static GTY(()) tree sjlj_fc_type_node;
109 static int sjlj_fc_call_site_ofs;
110 static int sjlj_fc_data_ofs;
111 static int sjlj_fc_personality_ofs;
112 static int sjlj_fc_lsda_ofs;
113 static int sjlj_fc_jbuf_ofs;
115 /* Describes one exception region. */
116 struct eh_region GTY(())
118 /* The immediately surrounding region. */
119 struct eh_region *outer;
121 /* The list of immediately contained regions. */
122 struct eh_region *inner;
123 struct eh_region *next_peer;
125 /* An identifier for this region. */
126 int region_number;
128 /* When a region is deleted, its parents inherit the REG_EH_REGION
129 numbers already assigned. */
130 bitmap aka;
132 /* Each region does exactly one thing. */
133 enum eh_region_type
135 ERT_UNKNOWN = 0,
136 ERT_CLEANUP,
137 ERT_TRY,
138 ERT_CATCH,
139 ERT_ALLOWED_EXCEPTIONS,
140 ERT_MUST_NOT_THROW,
141 ERT_THROW,
142 ERT_FIXUP
143 } type;
145 /* Holds the action to perform based on the preceding type. */
146 union eh_region_u {
147 /* A list of catch blocks, a surrounding try block,
148 and the label for continuing after a catch. */
149 struct eh_region_u_try {
150 struct eh_region *catch;
151 struct eh_region *last_catch;
152 struct eh_region *prev_try;
153 rtx continue_label;
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 tree exp;
181 struct eh_region *prev_try;
182 } GTY ((tag ("ERT_CLEANUP"))) cleanup;
184 /* The real region (by expression and by pointer) that fixup code
185 should live in. */
186 struct eh_region_u_fixup {
187 tree cleanup_exp;
188 struct eh_region *real_region;
189 bool resolved;
190 } GTY ((tag ("ERT_FIXUP"))) fixup;
191 } GTY ((desc ("%0.type"))) u;
193 /* Entry point for this region's handler before landing pads are built. */
194 rtx label;
195 tree tree_label;
197 /* Entry point for this region's handler from the runtime eh library. */
198 rtx landing_pad;
200 /* Entry point for this region's handler from an inner region. */
201 rtx post_landing_pad;
203 /* The RESX insn for handing off control to the next outermost handler,
204 if appropriate. */
205 rtx resume;
207 /* True if something in this region may throw. */
208 unsigned may_contain_throw : 1;
211 struct call_site_record GTY(())
213 rtx landing_pad;
214 int action;
217 /* Used to save exception status for each function. */
218 struct eh_status GTY(())
220 /* The tree of all regions for this function. */
221 struct eh_region *region_tree;
223 /* The same information as an indexable array. */
224 struct eh_region ** GTY ((length ("%h.last_region_number"))) region_array;
226 /* The most recently open region. */
227 struct eh_region *cur_region;
229 /* This is the region for which we are processing catch blocks. */
230 struct eh_region *try_region;
232 rtx filter;
233 rtx exc_ptr;
235 int built_landing_pads;
236 int last_region_number;
238 varray_type ttype_data;
239 varray_type ehspec_data;
240 varray_type action_record_data;
242 htab_t GTY ((param_is (struct ehl_map_entry))) exception_handler_label_map;
244 struct call_site_record * GTY ((length ("%h.call_site_data_used")))
245 call_site_data;
246 int call_site_data_used;
247 int call_site_data_size;
249 rtx ehr_stackadj;
250 rtx ehr_handler;
251 rtx ehr_label;
253 rtx sjlj_fc;
254 rtx sjlj_exit_after;
258 static int t2r_eq (const void *, const void *);
259 static hashval_t t2r_hash (const void *);
260 static void add_type_for_runtime (tree);
261 static tree lookup_type_for_runtime (tree);
263 static void resolve_fixup_regions (void);
264 static void remove_fixup_regions (void);
265 static void remove_unreachable_regions (rtx);
266 static void convert_from_eh_region_ranges_1 (rtx *, int *, int);
268 static struct eh_region *duplicate_eh_region_1 (struct eh_region *,
269 struct inline_remap *);
270 static void duplicate_eh_region_2 (struct eh_region *, struct eh_region **);
271 static int ttypes_filter_eq (const void *, const void *);
272 static hashval_t ttypes_filter_hash (const void *);
273 static int ehspec_filter_eq (const void *, const void *);
274 static hashval_t ehspec_filter_hash (const void *);
275 static int add_ttypes_entry (htab_t, tree);
276 static int add_ehspec_entry (htab_t, htab_t, tree);
277 static void assign_filter_values (void);
278 static void build_post_landing_pads (void);
279 static void connect_post_landing_pads (void);
280 static void dw2_build_landing_pads (void);
282 struct sjlj_lp_info;
283 static bool sjlj_find_directly_reachable_regions (struct sjlj_lp_info *);
284 static void sjlj_assign_call_site_values (rtx, struct sjlj_lp_info *);
285 static void sjlj_mark_call_sites (struct sjlj_lp_info *);
286 static void sjlj_emit_function_enter (rtx);
287 static void sjlj_emit_function_exit (void);
288 static void sjlj_emit_dispatch_table (rtx, struct sjlj_lp_info *);
289 static void sjlj_build_landing_pads (void);
291 static hashval_t ehl_hash (const void *);
292 static int ehl_eq (const void *, const void *);
293 static void add_ehl_entry (rtx, struct eh_region *);
294 static void remove_exception_handler_label (rtx);
295 static void remove_eh_handler (struct eh_region *);
296 static int for_each_eh_label_1 (void **, void *);
298 /* The return value of reachable_next_level. */
299 enum reachable_code
301 /* The given exception is not processed by the given region. */
302 RNL_NOT_CAUGHT,
303 /* The given exception may need processing by the given region. */
304 RNL_MAYBE_CAUGHT,
305 /* The given exception is completely processed by the given region. */
306 RNL_CAUGHT,
307 /* The given exception is completely processed by the runtime. */
308 RNL_BLOCKED
311 struct reachable_info;
312 static enum reachable_code reachable_next_level (struct eh_region *, tree,
313 struct reachable_info *);
315 static int action_record_eq (const void *, const void *);
316 static hashval_t action_record_hash (const void *);
317 static int add_action_record (htab_t, int, int);
318 static int collect_one_action_chain (htab_t, struct eh_region *);
319 static int add_call_site (rtx, int);
321 static void push_uleb128 (varray_type *, unsigned int);
322 static void push_sleb128 (varray_type *, int);
323 #ifndef HAVE_AS_LEB128
324 static int dw2_size_of_call_site_table (void);
325 static int sjlj_size_of_call_site_table (void);
326 #endif
327 static void dw2_output_call_site_table (void);
328 static void sjlj_output_call_site_table (void);
331 /* Routine to see if exception handling is turned on.
332 DO_WARN is nonzero if we want to inform the user that exception
333 handling is turned off.
335 This is used to ensure that -fexceptions has been specified if the
336 compiler tries to use any exception-specific functions. */
339 doing_eh (int do_warn)
341 if (! flag_exceptions)
343 static int warned = 0;
344 if (! warned && do_warn)
346 error ("exception handling disabled, use -fexceptions to enable");
347 warned = 1;
349 return 0;
351 return 1;
355 void
356 init_eh (void)
358 if (! flag_exceptions)
359 return;
361 type_to_runtime_map = htab_create_ggc (31, t2r_hash, t2r_eq, NULL);
363 /* Create the SjLj_Function_Context structure. This should match
364 the definition in unwind-sjlj.c. */
365 if (USING_SJLJ_EXCEPTIONS)
367 tree f_jbuf, f_per, f_lsda, f_prev, f_cs, f_data, tmp;
369 sjlj_fc_type_node = lang_hooks.types.make_type (RECORD_TYPE);
371 f_prev = build_decl (FIELD_DECL, get_identifier ("__prev"),
372 build_pointer_type (sjlj_fc_type_node));
373 DECL_FIELD_CONTEXT (f_prev) = sjlj_fc_type_node;
375 f_cs = build_decl (FIELD_DECL, get_identifier ("__call_site"),
376 integer_type_node);
377 DECL_FIELD_CONTEXT (f_cs) = sjlj_fc_type_node;
379 tmp = build_index_type (build_int_cst (NULL_TREE, 4 - 1, 0));
380 tmp = build_array_type (lang_hooks.types.type_for_mode (word_mode, 1),
381 tmp);
382 f_data = build_decl (FIELD_DECL, get_identifier ("__data"), tmp);
383 DECL_FIELD_CONTEXT (f_data) = sjlj_fc_type_node;
385 f_per = build_decl (FIELD_DECL, get_identifier ("__personality"),
386 ptr_type_node);
387 DECL_FIELD_CONTEXT (f_per) = sjlj_fc_type_node;
389 f_lsda = build_decl (FIELD_DECL, get_identifier ("__lsda"),
390 ptr_type_node);
391 DECL_FIELD_CONTEXT (f_lsda) = sjlj_fc_type_node;
393 #ifdef DONT_USE_BUILTIN_SETJMP
394 #ifdef JMP_BUF_SIZE
395 tmp = build_int_cst (NULL_TREE, JMP_BUF_SIZE - 1, 0);
396 #else
397 /* Should be large enough for most systems, if it is not,
398 JMP_BUF_SIZE should be defined with the proper value. It will
399 also tend to be larger than necessary for most systems, a more
400 optimal port will define JMP_BUF_SIZE. */
401 tmp = build_int_cst (NULL_TREE,
402 FIRST_PSEUDO_REGISTER + 2 - 1, 0);
403 #endif
404 #else
405 /* builtin_setjmp takes a pointer to 5 words. */
406 tmp = build_int_cst (NULL_TREE,
407 5 * BITS_PER_WORD / POINTER_SIZE - 1, 0);
408 #endif
409 tmp = build_index_type (tmp);
410 tmp = build_array_type (ptr_type_node, tmp);
411 f_jbuf = build_decl (FIELD_DECL, get_identifier ("__jbuf"), tmp);
412 #ifdef DONT_USE_BUILTIN_SETJMP
413 /* We don't know what the alignment requirements of the
414 runtime's jmp_buf has. Overestimate. */
415 DECL_ALIGN (f_jbuf) = BIGGEST_ALIGNMENT;
416 DECL_USER_ALIGN (f_jbuf) = 1;
417 #endif
418 DECL_FIELD_CONTEXT (f_jbuf) = sjlj_fc_type_node;
420 TYPE_FIELDS (sjlj_fc_type_node) = f_prev;
421 TREE_CHAIN (f_prev) = f_cs;
422 TREE_CHAIN (f_cs) = f_data;
423 TREE_CHAIN (f_data) = f_per;
424 TREE_CHAIN (f_per) = f_lsda;
425 TREE_CHAIN (f_lsda) = f_jbuf;
427 layout_type (sjlj_fc_type_node);
429 /* Cache the interesting field offsets so that we have
430 easy access from rtl. */
431 sjlj_fc_call_site_ofs
432 = (tree_low_cst (DECL_FIELD_OFFSET (f_cs), 1)
433 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_cs), 1) / BITS_PER_UNIT);
434 sjlj_fc_data_ofs
435 = (tree_low_cst (DECL_FIELD_OFFSET (f_data), 1)
436 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_data), 1) / BITS_PER_UNIT);
437 sjlj_fc_personality_ofs
438 = (tree_low_cst (DECL_FIELD_OFFSET (f_per), 1)
439 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_per), 1) / BITS_PER_UNIT);
440 sjlj_fc_lsda_ofs
441 = (tree_low_cst (DECL_FIELD_OFFSET (f_lsda), 1)
442 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_lsda), 1) / BITS_PER_UNIT);
443 sjlj_fc_jbuf_ofs
444 = (tree_low_cst (DECL_FIELD_OFFSET (f_jbuf), 1)
445 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_jbuf), 1) / BITS_PER_UNIT);
449 void
450 init_eh_for_function (void)
452 cfun->eh = ggc_alloc_cleared (sizeof (struct eh_status));
455 /* Routines to generate the exception tree somewhat directly.
456 These are used from tree-eh.c when processing exception related
457 nodes during tree optimization. */
459 static struct eh_region *
460 gen_eh_region (enum eh_region_type type, struct eh_region *outer)
462 struct eh_region *new;
464 #ifdef ENABLE_CHECKING
465 if (! doing_eh (0))
466 abort ();
467 #endif
469 /* Insert a new blank region as a leaf in the tree. */
470 new = ggc_alloc_cleared (sizeof (*new));
471 new->type = type;
472 new->outer = outer;
473 if (outer)
475 new->next_peer = outer->inner;
476 outer->inner = new;
478 else
480 new->next_peer = cfun->eh->region_tree;
481 cfun->eh->region_tree = new;
484 new->region_number = ++cfun->eh->last_region_number;
486 return new;
489 struct eh_region *
490 gen_eh_region_cleanup (struct eh_region *outer, struct eh_region *prev_try)
492 struct eh_region *cleanup = gen_eh_region (ERT_CLEANUP, outer);
493 cleanup->u.cleanup.prev_try = prev_try;
494 return cleanup;
497 struct eh_region *
498 gen_eh_region_try (struct eh_region *outer)
500 return gen_eh_region (ERT_TRY, outer);
503 struct eh_region *
504 gen_eh_region_catch (struct eh_region *t, tree type_or_list)
506 struct eh_region *c, *l;
507 tree type_list, type_node;
509 /* Ensure to always end up with a type list to normalize further
510 processing, then register each type against the runtime types map. */
511 type_list = type_or_list;
512 if (type_or_list)
514 if (TREE_CODE (type_or_list) != TREE_LIST)
515 type_list = tree_cons (NULL_TREE, type_or_list, NULL_TREE);
517 type_node = type_list;
518 for (; type_node; type_node = TREE_CHAIN (type_node))
519 add_type_for_runtime (TREE_VALUE (type_node));
522 c = gen_eh_region (ERT_CATCH, t->outer);
523 c->u.catch.type_list = type_list;
524 l = t->u.try.last_catch;
525 c->u.catch.prev_catch = l;
526 if (l)
527 l->u.catch.next_catch = c;
528 else
529 t->u.try.catch = c;
530 t->u.try.last_catch = c;
532 return c;
535 struct eh_region *
536 gen_eh_region_allowed (struct eh_region *outer, tree allowed)
538 struct eh_region *region = gen_eh_region (ERT_ALLOWED_EXCEPTIONS, outer);
539 region->u.allowed.type_list = allowed;
541 for (; allowed ; allowed = TREE_CHAIN (allowed))
542 add_type_for_runtime (TREE_VALUE (allowed));
544 return region;
547 struct eh_region *
548 gen_eh_region_must_not_throw (struct eh_region *outer)
550 return gen_eh_region (ERT_MUST_NOT_THROW, outer);
554 get_eh_region_number (struct eh_region *region)
556 return region->region_number;
559 bool
560 get_eh_region_may_contain_throw (struct eh_region *region)
562 return region->may_contain_throw;
565 tree
566 get_eh_region_tree_label (struct eh_region *region)
568 return region->tree_label;
571 void
572 set_eh_region_tree_label (struct eh_region *region, tree lab)
574 region->tree_label = lab;
577 void
578 expand_resx_expr (tree exp)
580 int region_nr = TREE_INT_CST_LOW (TREE_OPERAND (exp, 0));
581 struct eh_region *reg = cfun->eh->region_array[region_nr];
583 reg->resume = emit_jump_insn (gen_rtx_RESX (VOIDmode, region_nr));
584 emit_barrier ();
587 /* Note that the current EH region (if any) may contain a throw, or a
588 call to a function which itself may contain a throw. */
590 void
591 note_eh_region_may_contain_throw (struct eh_region *region)
593 while (region && !region->may_contain_throw)
595 region->may_contain_throw = 1;
596 region = region->outer;
600 void
601 note_current_region_may_contain_throw (void)
603 note_eh_region_may_contain_throw (cfun->eh->cur_region);
607 /* Return an rtl expression for a pointer to the exception object
608 within a handler. */
611 get_exception_pointer (struct function *fun)
613 rtx exc_ptr = fun->eh->exc_ptr;
614 if (fun == cfun && ! exc_ptr)
616 exc_ptr = gen_reg_rtx (ptr_mode);
617 fun->eh->exc_ptr = exc_ptr;
619 return exc_ptr;
622 /* Return an rtl expression for the exception dispatch filter
623 within a handler. */
626 get_exception_filter (struct function *fun)
628 rtx filter = fun->eh->filter;
629 if (fun == cfun && ! filter)
631 filter = gen_reg_rtx (word_mode);
632 fun->eh->filter = filter;
634 return filter;
637 /* This section is for the exception handling specific optimization pass. */
639 /* Random access the exception region tree. */
641 void
642 collect_eh_region_array (void)
644 struct eh_region **array, *i;
646 i = cfun->eh->region_tree;
647 if (! i)
648 return;
650 array = ggc_alloc_cleared ((cfun->eh->last_region_number + 1)
651 * sizeof (*array));
652 cfun->eh->region_array = array;
654 while (1)
656 array[i->region_number] = i;
658 /* If there are sub-regions, process them. */
659 if (i->inner)
660 i = i->inner;
661 /* If there are peers, process them. */
662 else if (i->next_peer)
663 i = i->next_peer;
664 /* Otherwise, step back up the tree to the next peer. */
665 else
667 do {
668 i = i->outer;
669 if (i == NULL)
670 return;
671 } while (i->next_peer == NULL);
672 i = i->next_peer;
677 static void
678 resolve_one_fixup_region (struct eh_region *fixup)
680 struct eh_region *cleanup, *real;
681 int j, n;
683 n = cfun->eh->last_region_number;
684 cleanup = 0;
686 for (j = 1; j <= n; ++j)
688 cleanup = cfun->eh->region_array[j];
689 if (cleanup && cleanup->type == ERT_CLEANUP
690 && cleanup->u.cleanup.exp == fixup->u.fixup.cleanup_exp)
691 break;
693 if (j > n)
694 abort ();
696 real = cleanup->outer;
697 if (real && real->type == ERT_FIXUP)
699 if (!real->u.fixup.resolved)
700 resolve_one_fixup_region (real);
701 real = real->u.fixup.real_region;
704 fixup->u.fixup.real_region = real;
705 fixup->u.fixup.resolved = true;
708 static void
709 resolve_fixup_regions (void)
711 int i, n = cfun->eh->last_region_number;
713 for (i = 1; i <= n; ++i)
715 struct eh_region *fixup = cfun->eh->region_array[i];
717 if (!fixup || fixup->type != ERT_FIXUP || fixup->u.fixup.resolved)
718 continue;
720 resolve_one_fixup_region (fixup);
724 /* Now that we've discovered what region actually encloses a fixup,
725 we can shuffle pointers and remove them from the tree. */
727 static void
728 remove_fixup_regions (void)
730 int i;
731 rtx insn, note;
732 struct eh_region *fixup;
734 /* Walk the insn chain and adjust the REG_EH_REGION numbers
735 for instructions referencing fixup regions. This is only
736 strictly necessary for fixup regions with no parent, but
737 doesn't hurt to do it for all regions. */
738 for (insn = get_insns(); insn ; insn = NEXT_INSN (insn))
739 if (INSN_P (insn)
740 && (note = find_reg_note (insn, REG_EH_REGION, NULL))
741 && INTVAL (XEXP (note, 0)) > 0
742 && (fixup = cfun->eh->region_array[INTVAL (XEXP (note, 0))])
743 && fixup->type == ERT_FIXUP)
745 if (fixup->u.fixup.real_region)
746 XEXP (note, 0) = GEN_INT (fixup->u.fixup.real_region->region_number);
747 else
748 remove_note (insn, note);
751 /* Remove the fixup regions from the tree. */
752 for (i = cfun->eh->last_region_number; i > 0; --i)
754 fixup = cfun->eh->region_array[i];
755 if (! fixup)
756 continue;
758 /* Allow GC to maybe free some memory. */
759 if (fixup->type == ERT_CLEANUP)
760 fixup->u.cleanup.exp = NULL_TREE;
762 if (fixup->type != ERT_FIXUP)
763 continue;
765 if (fixup->inner)
767 struct eh_region *parent, *p, **pp;
769 parent = fixup->u.fixup.real_region;
771 /* Fix up the children's parent pointers; find the end of
772 the list. */
773 for (p = fixup->inner; ; p = p->next_peer)
775 p->outer = parent;
776 if (! p->next_peer)
777 break;
780 /* In the tree of cleanups, only outer-inner ordering matters.
781 So link the children back in anywhere at the correct level. */
782 if (parent)
783 pp = &parent->inner;
784 else
785 pp = &cfun->eh->region_tree;
786 p->next_peer = *pp;
787 *pp = fixup->inner;
788 fixup->inner = NULL;
791 remove_eh_handler (fixup);
795 /* Remove all regions whose labels are not reachable from insns. */
797 static void
798 remove_unreachable_regions (rtx insns)
800 int i, *uid_region_num;
801 bool *reachable;
802 struct eh_region *r;
803 rtx insn;
805 uid_region_num = xcalloc (get_max_uid (), sizeof(int));
806 reachable = xcalloc (cfun->eh->last_region_number + 1, sizeof(bool));
808 for (i = cfun->eh->last_region_number; i > 0; --i)
810 r = cfun->eh->region_array[i];
811 if (!r || r->region_number != i)
812 continue;
814 if (r->resume)
816 if (uid_region_num[INSN_UID (r->resume)])
817 abort ();
818 uid_region_num[INSN_UID (r->resume)] = i;
820 if (r->label)
822 if (uid_region_num[INSN_UID (r->label)])
823 abort ();
824 uid_region_num[INSN_UID (r->label)] = i;
828 for (insn = insns; insn; insn = NEXT_INSN (insn))
829 reachable[uid_region_num[INSN_UID (insn)]] = true;
831 for (i = cfun->eh->last_region_number; i > 0; --i)
833 r = cfun->eh->region_array[i];
834 if (r && r->region_number == i && !reachable[i])
836 bool kill_it = true;
837 switch (r->type)
839 case ERT_THROW:
840 /* Don't remove ERT_THROW regions if their outer region
841 is reachable. */
842 if (r->outer && reachable[r->outer->region_number])
843 kill_it = false;
844 break;
846 case ERT_MUST_NOT_THROW:
847 /* MUST_NOT_THROW regions are implementable solely in the
848 runtime, but their existence continues to affect calls
849 within that region. Never delete them here. */
850 kill_it = false;
851 break;
853 case ERT_TRY:
855 /* TRY regions are reachable if any of its CATCH regions
856 are reachable. */
857 struct eh_region *c;
858 for (c = r->u.try.catch; c ; c = c->u.catch.next_catch)
859 if (reachable[c->region_number])
861 kill_it = false;
862 break;
864 break;
867 default:
868 break;
871 if (kill_it)
872 remove_eh_handler (r);
876 free (reachable);
877 free (uid_region_num);
880 /* Turn NOTE_INSN_EH_REGION notes into REG_EH_REGION notes for each
881 can_throw instruction in the region. */
883 static void
884 convert_from_eh_region_ranges_1 (rtx *pinsns, int *orig_sp, int cur)
886 int *sp = orig_sp;
887 rtx insn, next;
889 for (insn = *pinsns; insn ; insn = next)
891 next = NEXT_INSN (insn);
892 if (NOTE_P (insn))
894 int kind = NOTE_LINE_NUMBER (insn);
895 if (kind == NOTE_INSN_EH_REGION_BEG
896 || kind == NOTE_INSN_EH_REGION_END)
898 if (kind == NOTE_INSN_EH_REGION_BEG)
900 struct eh_region *r;
902 *sp++ = cur;
903 cur = NOTE_EH_HANDLER (insn);
905 r = cfun->eh->region_array[cur];
906 if (r->type == ERT_FIXUP)
908 r = r->u.fixup.real_region;
909 cur = r ? r->region_number : 0;
911 else if (r->type == ERT_CATCH)
913 r = r->outer;
914 cur = r ? r->region_number : 0;
917 else
918 cur = *--sp;
920 if (insn == *pinsns)
921 *pinsns = next;
922 remove_insn (insn);
923 continue;
926 else if (INSN_P (insn))
928 if (cur > 0
929 && ! find_reg_note (insn, REG_EH_REGION, NULL_RTX)
930 /* Calls can always potentially throw exceptions, unless
931 they have a REG_EH_REGION note with a value of 0 or less.
932 Which should be the only possible kind so far. */
933 && (CALL_P (insn)
934 /* If we wanted exceptions for non-call insns, then
935 any may_trap_p instruction could throw. */
936 || (flag_non_call_exceptions
937 && GET_CODE (PATTERN (insn)) != CLOBBER
938 && GET_CODE (PATTERN (insn)) != USE
939 && may_trap_p (PATTERN (insn)))))
941 REG_NOTES (insn) = alloc_EXPR_LIST (REG_EH_REGION, GEN_INT (cur),
942 REG_NOTES (insn));
947 if (sp != orig_sp)
948 abort ();
951 static void
952 collect_rtl_labels_from_trees (void)
954 int i, n = cfun->eh->last_region_number;
955 for (i = 1; i <= n; ++i)
957 struct eh_region *reg = cfun->eh->region_array[i];
958 if (reg && reg->tree_label)
959 reg->label = DECL_RTL_IF_SET (reg->tree_label);
963 void
964 convert_from_eh_region_ranges (void)
966 rtx insns = get_insns ();
968 if (cfun->eh->region_array)
970 /* If the region array already exists, assume we're coming from
971 optimize_function_tree. In this case all we need to do is
972 collect the rtl labels that correspond to the tree labels
973 that we allocated earlier. */
974 collect_rtl_labels_from_trees ();
976 else
978 int *stack;
980 collect_eh_region_array ();
981 resolve_fixup_regions ();
983 stack = xmalloc (sizeof (int) * (cfun->eh->last_region_number + 1));
984 convert_from_eh_region_ranges_1 (&insns, stack, 0);
985 free (stack);
987 remove_fixup_regions ();
990 remove_unreachable_regions (insns);
993 static void
994 add_ehl_entry (rtx label, struct eh_region *region)
996 struct ehl_map_entry **slot, *entry;
998 LABEL_PRESERVE_P (label) = 1;
1000 entry = ggc_alloc (sizeof (*entry));
1001 entry->label = label;
1002 entry->region = region;
1004 slot = (struct ehl_map_entry **)
1005 htab_find_slot (cfun->eh->exception_handler_label_map, entry, INSERT);
1007 /* Before landing pad creation, each exception handler has its own
1008 label. After landing pad creation, the exception handlers may
1009 share landing pads. This is ok, since maybe_remove_eh_handler
1010 only requires the 1-1 mapping before landing pad creation. */
1011 if (*slot && !cfun->eh->built_landing_pads)
1012 abort ();
1014 *slot = entry;
1017 void
1018 find_exception_handler_labels (void)
1020 int i;
1022 if (cfun->eh->exception_handler_label_map)
1023 htab_empty (cfun->eh->exception_handler_label_map);
1024 else
1026 /* ??? The expansion factor here (3/2) must be greater than the htab
1027 occupancy factor (4/3) to avoid unnecessary resizing. */
1028 cfun->eh->exception_handler_label_map
1029 = htab_create_ggc (cfun->eh->last_region_number * 3 / 2,
1030 ehl_hash, ehl_eq, NULL);
1033 if (cfun->eh->region_tree == NULL)
1034 return;
1036 for (i = cfun->eh->last_region_number; i > 0; --i)
1038 struct eh_region *region = cfun->eh->region_array[i];
1039 rtx lab;
1041 if (! region || region->region_number != i)
1042 continue;
1043 if (cfun->eh->built_landing_pads)
1044 lab = region->landing_pad;
1045 else
1046 lab = region->label;
1048 if (lab)
1049 add_ehl_entry (lab, region);
1052 /* For sjlj exceptions, need the return label to remain live until
1053 after landing pad generation. */
1054 if (USING_SJLJ_EXCEPTIONS && ! cfun->eh->built_landing_pads)
1055 add_ehl_entry (return_label, NULL);
1058 bool
1059 current_function_has_exception_handlers (void)
1061 int i;
1063 for (i = cfun->eh->last_region_number; i > 0; --i)
1065 struct eh_region *region = cfun->eh->region_array[i];
1067 if (! region || region->region_number != i)
1068 continue;
1069 if (region->type != ERT_THROW)
1070 return true;
1073 return false;
1076 static struct eh_region *
1077 duplicate_eh_region_1 (struct eh_region *o, struct inline_remap *map)
1079 struct eh_region *n = ggc_alloc_cleared (sizeof (struct eh_region));
1081 n->region_number = o->region_number + cfun->eh->last_region_number;
1082 n->type = o->type;
1084 switch (n->type)
1086 case ERT_CLEANUP:
1087 case ERT_MUST_NOT_THROW:
1088 break;
1090 case ERT_TRY:
1091 if (o->u.try.continue_label)
1092 n->u.try.continue_label
1093 = get_label_from_map (map,
1094 CODE_LABEL_NUMBER (o->u.try.continue_label));
1095 break;
1097 case ERT_CATCH:
1098 n->u.catch.type_list = o->u.catch.type_list;
1099 break;
1101 case ERT_ALLOWED_EXCEPTIONS:
1102 n->u.allowed.type_list = o->u.allowed.type_list;
1103 break;
1105 case ERT_THROW:
1106 n->u.throw.type = o->u.throw.type;
1108 default:
1109 abort ();
1112 if (o->label)
1113 n->label = get_label_from_map (map, CODE_LABEL_NUMBER (o->label));
1114 if (o->resume)
1116 n->resume = map->insn_map[INSN_UID (o->resume)];
1117 if (n->resume == NULL)
1118 abort ();
1121 return n;
1124 static void
1125 duplicate_eh_region_2 (struct eh_region *o, struct eh_region **n_array)
1127 struct eh_region *n = n_array[o->region_number];
1129 switch (n->type)
1131 case ERT_TRY:
1132 n->u.try.catch = n_array[o->u.try.catch->region_number];
1133 n->u.try.last_catch = n_array[o->u.try.last_catch->region_number];
1134 break;
1136 case ERT_CATCH:
1137 if (o->u.catch.next_catch)
1138 n->u.catch.next_catch = n_array[o->u.catch.next_catch->region_number];
1139 if (o->u.catch.prev_catch)
1140 n->u.catch.prev_catch = n_array[o->u.catch.prev_catch->region_number];
1141 break;
1143 default:
1144 break;
1147 if (o->outer)
1148 n->outer = n_array[o->outer->region_number];
1149 if (o->inner)
1150 n->inner = n_array[o->inner->region_number];
1151 if (o->next_peer)
1152 n->next_peer = n_array[o->next_peer->region_number];
1156 duplicate_eh_regions (struct function *ifun, struct inline_remap *map)
1158 int ifun_last_region_number = ifun->eh->last_region_number;
1159 struct eh_region **n_array, *root, *cur;
1160 int i;
1162 if (ifun_last_region_number == 0)
1163 return 0;
1165 n_array = xcalloc (ifun_last_region_number + 1, sizeof (*n_array));
1167 for (i = 1; i <= ifun_last_region_number; ++i)
1169 cur = ifun->eh->region_array[i];
1170 if (!cur || cur->region_number != i)
1171 continue;
1172 n_array[i] = duplicate_eh_region_1 (cur, map);
1174 for (i = 1; i <= ifun_last_region_number; ++i)
1176 cur = ifun->eh->region_array[i];
1177 if (!cur || cur->region_number != i)
1178 continue;
1179 duplicate_eh_region_2 (cur, n_array);
1182 root = n_array[ifun->eh->region_tree->region_number];
1183 cur = cfun->eh->cur_region;
1184 if (cur)
1186 struct eh_region *p = cur->inner;
1187 if (p)
1189 while (p->next_peer)
1190 p = p->next_peer;
1191 p->next_peer = root;
1193 else
1194 cur->inner = root;
1196 for (i = 1; i <= ifun_last_region_number; ++i)
1197 if (n_array[i] && n_array[i]->outer == NULL)
1198 n_array[i]->outer = cur;
1200 else
1202 struct eh_region *p = cfun->eh->region_tree;
1203 if (p)
1205 while (p->next_peer)
1206 p = p->next_peer;
1207 p->next_peer = root;
1209 else
1210 cfun->eh->region_tree = root;
1213 free (n_array);
1215 i = cfun->eh->last_region_number;
1216 cfun->eh->last_region_number = i + ifun_last_region_number;
1217 return i;
1221 static int
1222 t2r_eq (const void *pentry, const void *pdata)
1224 tree entry = (tree) pentry;
1225 tree data = (tree) pdata;
1227 return TREE_PURPOSE (entry) == data;
1230 static hashval_t
1231 t2r_hash (const void *pentry)
1233 tree entry = (tree) pentry;
1234 return TREE_HASH (TREE_PURPOSE (entry));
1237 static void
1238 add_type_for_runtime (tree type)
1240 tree *slot;
1242 slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
1243 TREE_HASH (type), INSERT);
1244 if (*slot == NULL)
1246 tree runtime = (*lang_eh_runtime_type) (type);
1247 *slot = tree_cons (type, runtime, NULL_TREE);
1251 static tree
1252 lookup_type_for_runtime (tree type)
1254 tree *slot;
1256 slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
1257 TREE_HASH (type), NO_INSERT);
1259 /* We should have always inserted the data earlier. */
1260 return TREE_VALUE (*slot);
1264 /* Represent an entry in @TTypes for either catch actions
1265 or exception filter actions. */
1266 struct ttypes_filter GTY(())
1268 tree t;
1269 int filter;
1272 /* Compare ENTRY (a ttypes_filter entry in the hash table) with DATA
1273 (a tree) for a @TTypes type node we are thinking about adding. */
1275 static int
1276 ttypes_filter_eq (const void *pentry, const void *pdata)
1278 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1279 tree data = (tree) pdata;
1281 return entry->t == data;
1284 static hashval_t
1285 ttypes_filter_hash (const void *pentry)
1287 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1288 return TREE_HASH (entry->t);
1291 /* Compare ENTRY with DATA (both struct ttypes_filter) for a @TTypes
1292 exception specification list we are thinking about adding. */
1293 /* ??? Currently we use the type lists in the order given. Someone
1294 should put these in some canonical order. */
1296 static int
1297 ehspec_filter_eq (const void *pentry, const void *pdata)
1299 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1300 const struct ttypes_filter *data = (const struct ttypes_filter *) pdata;
1302 return type_list_equal (entry->t, data->t);
1305 /* Hash function for exception specification lists. */
1307 static hashval_t
1308 ehspec_filter_hash (const void *pentry)
1310 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1311 hashval_t h = 0;
1312 tree list;
1314 for (list = entry->t; list ; list = TREE_CHAIN (list))
1315 h = (h << 5) + (h >> 27) + TREE_HASH (TREE_VALUE (list));
1316 return h;
1319 /* Add TYPE (which may be NULL) to cfun->eh->ttype_data, using TYPES_HASH
1320 to speed up the search. Return the filter value to be used. */
1322 static int
1323 add_ttypes_entry (htab_t ttypes_hash, tree type)
1325 struct ttypes_filter **slot, *n;
1327 slot = (struct ttypes_filter **)
1328 htab_find_slot_with_hash (ttypes_hash, type, TREE_HASH (type), INSERT);
1330 if ((n = *slot) == NULL)
1332 /* Filter value is a 1 based table index. */
1334 n = xmalloc (sizeof (*n));
1335 n->t = type;
1336 n->filter = VARRAY_ACTIVE_SIZE (cfun->eh->ttype_data) + 1;
1337 *slot = n;
1339 VARRAY_PUSH_TREE (cfun->eh->ttype_data, type);
1342 return n->filter;
1345 /* Add LIST to cfun->eh->ehspec_data, using EHSPEC_HASH and TYPES_HASH
1346 to speed up the search. Return the filter value to be used. */
1348 static int
1349 add_ehspec_entry (htab_t ehspec_hash, htab_t ttypes_hash, tree list)
1351 struct ttypes_filter **slot, *n;
1352 struct ttypes_filter dummy;
1354 dummy.t = list;
1355 slot = (struct ttypes_filter **)
1356 htab_find_slot (ehspec_hash, &dummy, INSERT);
1358 if ((n = *slot) == NULL)
1360 /* Filter value is a -1 based byte index into a uleb128 buffer. */
1362 n = xmalloc (sizeof (*n));
1363 n->t = list;
1364 n->filter = -(VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data) + 1);
1365 *slot = n;
1367 /* Look up each type in the list and encode its filter
1368 value as a uleb128. Terminate the list with 0. */
1369 for (; list ; list = TREE_CHAIN (list))
1370 push_uleb128 (&cfun->eh->ehspec_data,
1371 add_ttypes_entry (ttypes_hash, TREE_VALUE (list)));
1372 VARRAY_PUSH_UCHAR (cfun->eh->ehspec_data, 0);
1375 return n->filter;
1378 /* Generate the action filter values to be used for CATCH and
1379 ALLOWED_EXCEPTIONS regions. When using dwarf2 exception regions,
1380 we use lots of landing pads, and so every type or list can share
1381 the same filter value, which saves table space. */
1383 static void
1384 assign_filter_values (void)
1386 int i;
1387 htab_t ttypes, ehspec;
1389 VARRAY_TREE_INIT (cfun->eh->ttype_data, 16, "ttype_data");
1390 VARRAY_UCHAR_INIT (cfun->eh->ehspec_data, 64, "ehspec_data");
1392 ttypes = htab_create (31, ttypes_filter_hash, ttypes_filter_eq, free);
1393 ehspec = htab_create (31, ehspec_filter_hash, ehspec_filter_eq, free);
1395 for (i = cfun->eh->last_region_number; i > 0; --i)
1397 struct eh_region *r = cfun->eh->region_array[i];
1399 /* Mind we don't process a region more than once. */
1400 if (!r || r->region_number != i)
1401 continue;
1403 switch (r->type)
1405 case ERT_CATCH:
1406 /* Whatever type_list is (NULL or true list), we build a list
1407 of filters for the region. */
1408 r->u.catch.filter_list = NULL_TREE;
1410 if (r->u.catch.type_list != NULL)
1412 /* Get a filter value for each of the types caught and store
1413 them in the region's dedicated list. */
1414 tree tp_node = r->u.catch.type_list;
1416 for (;tp_node; tp_node = TREE_CHAIN (tp_node))
1418 int flt = add_ttypes_entry (ttypes, TREE_VALUE (tp_node));
1419 tree flt_node = build_int_cst (NULL_TREE, flt, 0);
1421 r->u.catch.filter_list
1422 = tree_cons (NULL_TREE, flt_node, r->u.catch.filter_list);
1425 else
1427 /* Get a filter value for the NULL list also since it will need
1428 an action record anyway. */
1429 int flt = add_ttypes_entry (ttypes, NULL);
1430 tree flt_node = build_int_cst (NULL_TREE, flt, 0);
1432 r->u.catch.filter_list
1433 = tree_cons (NULL_TREE, flt_node, r->u.catch.filter_list);
1436 break;
1438 case ERT_ALLOWED_EXCEPTIONS:
1439 r->u.allowed.filter
1440 = add_ehspec_entry (ehspec, ttypes, r->u.allowed.type_list);
1441 break;
1443 default:
1444 break;
1448 htab_delete (ttypes);
1449 htab_delete (ehspec);
1452 /* Emit SEQ into basic block just before INSN (that is assumed to be
1453 first instruction of some existing BB and return the newly
1454 produced block. */
1455 static basic_block
1456 emit_to_new_bb_before (rtx seq, rtx insn)
1458 rtx last;
1459 basic_block bb;
1460 edge e;
1462 /* If there happens to be an fallthru edge (possibly created by cleanup_cfg
1463 call), we don't want it to go into newly created landing pad or other EH
1464 construct. */
1465 for (e = BLOCK_FOR_INSN (insn)->pred; e; e = e->pred_next)
1466 if (e->flags & EDGE_FALLTHRU)
1467 force_nonfallthru (e);
1468 last = emit_insn_before (seq, insn);
1469 if (BARRIER_P (last))
1470 last = PREV_INSN (last);
1471 bb = create_basic_block (seq, last, BLOCK_FOR_INSN (insn)->prev_bb);
1472 update_bb_for_insn (bb);
1473 bb->flags |= BB_SUPERBLOCK;
1474 return bb;
1477 /* Generate the code to actually handle exceptions, which will follow the
1478 landing pads. */
1480 static void
1481 build_post_landing_pads (void)
1483 int i;
1485 for (i = cfun->eh->last_region_number; i > 0; --i)
1487 struct eh_region *region = cfun->eh->region_array[i];
1488 rtx seq;
1490 /* Mind we don't process a region more than once. */
1491 if (!region || region->region_number != i)
1492 continue;
1494 switch (region->type)
1496 case ERT_TRY:
1497 /* ??? Collect the set of all non-overlapping catch handlers
1498 all the way up the chain until blocked by a cleanup. */
1499 /* ??? Outer try regions can share landing pads with inner
1500 try regions if the types are completely non-overlapping,
1501 and there are no intervening cleanups. */
1503 region->post_landing_pad = gen_label_rtx ();
1505 start_sequence ();
1507 emit_label (region->post_landing_pad);
1509 /* ??? It is mighty inconvenient to call back into the
1510 switch statement generation code in expand_end_case.
1511 Rapid prototyping sez a sequence of ifs. */
1513 struct eh_region *c;
1514 for (c = region->u.try.catch; c ; c = c->u.catch.next_catch)
1516 if (c->u.catch.type_list == NULL)
1517 emit_jump (c->label);
1518 else
1520 /* Need for one cmp/jump per type caught. Each type
1521 list entry has a matching entry in the filter list
1522 (see assign_filter_values). */
1523 tree tp_node = c->u.catch.type_list;
1524 tree flt_node = c->u.catch.filter_list;
1526 for (; tp_node; )
1528 emit_cmp_and_jump_insns
1529 (cfun->eh->filter,
1530 GEN_INT (tree_low_cst (TREE_VALUE (flt_node), 0)),
1531 EQ, NULL_RTX, word_mode, 0, c->label);
1533 tp_node = TREE_CHAIN (tp_node);
1534 flt_node = TREE_CHAIN (flt_node);
1540 /* We delay the generation of the _Unwind_Resume until we generate
1541 landing pads. We emit a marker here so as to get good control
1542 flow data in the meantime. */
1543 region->resume
1544 = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
1545 emit_barrier ();
1547 seq = get_insns ();
1548 end_sequence ();
1550 emit_to_new_bb_before (seq, region->u.try.catch->label);
1552 break;
1554 case ERT_ALLOWED_EXCEPTIONS:
1555 region->post_landing_pad = gen_label_rtx ();
1557 start_sequence ();
1559 emit_label (region->post_landing_pad);
1561 emit_cmp_and_jump_insns (cfun->eh->filter,
1562 GEN_INT (region->u.allowed.filter),
1563 EQ, NULL_RTX, word_mode, 0, region->label);
1565 /* We delay the generation of the _Unwind_Resume until we generate
1566 landing pads. We emit a marker here so as to get good control
1567 flow data in the meantime. */
1568 region->resume
1569 = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
1570 emit_barrier ();
1572 seq = get_insns ();
1573 end_sequence ();
1575 emit_to_new_bb_before (seq, region->label);
1576 break;
1578 case ERT_CLEANUP:
1579 case ERT_MUST_NOT_THROW:
1580 region->post_landing_pad = region->label;
1581 break;
1583 case ERT_CATCH:
1584 case ERT_THROW:
1585 /* Nothing to do. */
1586 break;
1588 default:
1589 abort ();
1594 /* Replace RESX patterns with jumps to the next handler if any, or calls to
1595 _Unwind_Resume otherwise. */
1597 static void
1598 connect_post_landing_pads (void)
1600 int i;
1602 for (i = cfun->eh->last_region_number; i > 0; --i)
1604 struct eh_region *region = cfun->eh->region_array[i];
1605 struct eh_region *outer;
1606 rtx seq;
1607 rtx barrier;
1609 /* Mind we don't process a region more than once. */
1610 if (!region || region->region_number != i)
1611 continue;
1613 /* If there is no RESX, or it has been deleted by flow, there's
1614 nothing to fix up. */
1615 if (! region->resume || INSN_DELETED_P (region->resume))
1616 continue;
1618 /* Search for another landing pad in this function. */
1619 for (outer = region->outer; outer ; outer = outer->outer)
1620 if (outer->post_landing_pad)
1621 break;
1623 start_sequence ();
1625 if (outer)
1627 edge e;
1628 basic_block src, dest;
1630 emit_jump (outer->post_landing_pad);
1631 src = BLOCK_FOR_INSN (region->resume);
1632 dest = BLOCK_FOR_INSN (outer->post_landing_pad);
1633 while (src->succ)
1634 remove_edge (src->succ);
1635 e = make_edge (src, dest, 0);
1636 e->probability = REG_BR_PROB_BASE;
1637 e->count = src->count;
1639 else
1641 emit_library_call (unwind_resume_libfunc, LCT_THROW,
1642 VOIDmode, 1, cfun->eh->exc_ptr, ptr_mode);
1644 /* What we just emitted was a throwing libcall, so it got a
1645 barrier automatically added after it. If the last insn in
1646 the libcall sequence isn't the barrier, it's because the
1647 target emits multiple insns for a call, and there are insns
1648 after the actual call insn (which are redundant and would be
1649 optimized away). The barrier is inserted exactly after the
1650 call insn, so let's go get that and delete the insns after
1651 it, because below we need the barrier to be the last insn in
1652 the sequence. */
1653 delete_insns_since (NEXT_INSN (last_call_insn ()));
1656 seq = get_insns ();
1657 end_sequence ();
1658 barrier = emit_insn_before (seq, region->resume);
1659 /* Avoid duplicate barrier. */
1660 if (!BARRIER_P (barrier))
1661 abort ();
1662 delete_insn (barrier);
1663 delete_insn (region->resume);
1665 /* ??? From tree-ssa we can wind up with catch regions whose
1666 label is not instantiated, but whose resx is present. Now
1667 that we've dealt with the resx, kill the region. */
1668 if (region->label == NULL && region->type == ERT_CLEANUP)
1669 remove_eh_handler (region);
1674 static void
1675 dw2_build_landing_pads (void)
1677 int i;
1678 unsigned int j;
1680 for (i = cfun->eh->last_region_number; i > 0; --i)
1682 struct eh_region *region = cfun->eh->region_array[i];
1683 rtx seq;
1684 basic_block bb;
1685 bool clobbers_hard_regs = false;
1686 edge e;
1688 /* Mind we don't process a region more than once. */
1689 if (!region || region->region_number != i)
1690 continue;
1692 if (region->type != ERT_CLEANUP
1693 && region->type != ERT_TRY
1694 && region->type != ERT_ALLOWED_EXCEPTIONS)
1695 continue;
1697 start_sequence ();
1699 region->landing_pad = gen_label_rtx ();
1700 emit_label (region->landing_pad);
1702 #ifdef HAVE_exception_receiver
1703 if (HAVE_exception_receiver)
1704 emit_insn (gen_exception_receiver ());
1705 else
1706 #endif
1707 #ifdef HAVE_nonlocal_goto_receiver
1708 if (HAVE_nonlocal_goto_receiver)
1709 emit_insn (gen_nonlocal_goto_receiver ());
1710 else
1711 #endif
1712 { /* Nothing */ }
1714 /* If the eh_return data registers are call-saved, then we
1715 won't have considered them clobbered from the call that
1716 threw. Kill them now. */
1717 for (j = 0; ; ++j)
1719 unsigned r = EH_RETURN_DATA_REGNO (j);
1720 if (r == INVALID_REGNUM)
1721 break;
1722 if (! call_used_regs[r])
1724 emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, r)));
1725 clobbers_hard_regs = true;
1729 if (clobbers_hard_regs)
1731 /* @@@ This is a kludge. Not all machine descriptions define a
1732 blockage insn, but we must not allow the code we just generated
1733 to be reordered by scheduling. So emit an ASM_INPUT to act as
1734 blockage insn. */
1735 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
1738 emit_move_insn (cfun->eh->exc_ptr,
1739 gen_rtx_REG (ptr_mode, EH_RETURN_DATA_REGNO (0)));
1740 emit_move_insn (cfun->eh->filter,
1741 gen_rtx_REG (word_mode, EH_RETURN_DATA_REGNO (1)));
1743 seq = get_insns ();
1744 end_sequence ();
1746 bb = emit_to_new_bb_before (seq, region->post_landing_pad);
1747 e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
1748 e->count = bb->count;
1749 e->probability = REG_BR_PROB_BASE;
1754 struct sjlj_lp_info
1756 int directly_reachable;
1757 int action_index;
1758 int dispatch_index;
1759 int call_site_index;
1762 static bool
1763 sjlj_find_directly_reachable_regions (struct sjlj_lp_info *lp_info)
1765 rtx insn;
1766 bool found_one = false;
1768 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
1770 struct eh_region *region;
1771 enum reachable_code rc;
1772 tree type_thrown;
1773 rtx note;
1775 if (! INSN_P (insn))
1776 continue;
1778 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1779 if (!note || INTVAL (XEXP (note, 0)) <= 0)
1780 continue;
1782 region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
1784 type_thrown = NULL_TREE;
1785 if (region->type == ERT_THROW)
1787 type_thrown = region->u.throw.type;
1788 region = region->outer;
1791 /* Find the first containing region that might handle the exception.
1792 That's the landing pad to which we will transfer control. */
1793 rc = RNL_NOT_CAUGHT;
1794 for (; region; region = region->outer)
1796 rc = reachable_next_level (region, type_thrown, NULL);
1797 if (rc != RNL_NOT_CAUGHT)
1798 break;
1800 if (rc == RNL_MAYBE_CAUGHT || rc == RNL_CAUGHT)
1802 lp_info[region->region_number].directly_reachable = 1;
1803 found_one = true;
1807 return found_one;
1810 static void
1811 sjlj_assign_call_site_values (rtx dispatch_label, struct sjlj_lp_info *lp_info)
1813 htab_t ar_hash;
1814 int i, index;
1816 /* First task: build the action table. */
1818 VARRAY_UCHAR_INIT (cfun->eh->action_record_data, 64, "action_record_data");
1819 ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
1821 for (i = cfun->eh->last_region_number; i > 0; --i)
1822 if (lp_info[i].directly_reachable)
1824 struct eh_region *r = cfun->eh->region_array[i];
1825 r->landing_pad = dispatch_label;
1826 lp_info[i].action_index = collect_one_action_chain (ar_hash, r);
1827 if (lp_info[i].action_index != -1)
1828 cfun->uses_eh_lsda = 1;
1831 htab_delete (ar_hash);
1833 /* Next: assign dispatch values. In dwarf2 terms, this would be the
1834 landing pad label for the region. For sjlj though, there is one
1835 common landing pad from which we dispatch to the post-landing pads.
1837 A region receives a dispatch index if it is directly reachable
1838 and requires in-function processing. Regions that share post-landing
1839 pads may share dispatch indices. */
1840 /* ??? Post-landing pad sharing doesn't actually happen at the moment
1841 (see build_post_landing_pads) so we don't bother checking for it. */
1843 index = 0;
1844 for (i = cfun->eh->last_region_number; i > 0; --i)
1845 if (lp_info[i].directly_reachable)
1846 lp_info[i].dispatch_index = index++;
1848 /* Finally: assign call-site values. If dwarf2 terms, this would be
1849 the region number assigned by convert_to_eh_region_ranges, but
1850 handles no-action and must-not-throw differently. */
1852 call_site_base = 1;
1853 for (i = cfun->eh->last_region_number; i > 0; --i)
1854 if (lp_info[i].directly_reachable)
1856 int action = lp_info[i].action_index;
1858 /* Map must-not-throw to otherwise unused call-site index 0. */
1859 if (action == -2)
1860 index = 0;
1861 /* Map no-action to otherwise unused call-site index -1. */
1862 else if (action == -1)
1863 index = -1;
1864 /* Otherwise, look it up in the table. */
1865 else
1866 index = add_call_site (GEN_INT (lp_info[i].dispatch_index), action);
1868 lp_info[i].call_site_index = index;
1872 static void
1873 sjlj_mark_call_sites (struct sjlj_lp_info *lp_info)
1875 int last_call_site = -2;
1876 rtx insn, mem;
1878 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
1880 struct eh_region *region;
1881 int this_call_site;
1882 rtx note, before, p;
1884 /* Reset value tracking at extended basic block boundaries. */
1885 if (LABEL_P (insn))
1886 last_call_site = -2;
1888 if (! INSN_P (insn))
1889 continue;
1891 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1892 if (!note)
1894 /* Calls (and trapping insns) without notes are outside any
1895 exception handling region in this function. Mark them as
1896 no action. */
1897 if (CALL_P (insn)
1898 || (flag_non_call_exceptions
1899 && may_trap_p (PATTERN (insn))))
1900 this_call_site = -1;
1901 else
1902 continue;
1904 else
1906 /* Calls that are known to not throw need not be marked. */
1907 if (INTVAL (XEXP (note, 0)) <= 0)
1908 continue;
1910 region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
1911 this_call_site = lp_info[region->region_number].call_site_index;
1914 if (this_call_site == last_call_site)
1915 continue;
1917 /* Don't separate a call from it's argument loads. */
1918 before = insn;
1919 if (CALL_P (insn))
1920 before = find_first_parameter_load (insn, NULL_RTX);
1922 start_sequence ();
1923 mem = adjust_address (cfun->eh->sjlj_fc, TYPE_MODE (integer_type_node),
1924 sjlj_fc_call_site_ofs);
1925 emit_move_insn (mem, GEN_INT (this_call_site));
1926 p = get_insns ();
1927 end_sequence ();
1929 emit_insn_before (p, before);
1930 last_call_site = this_call_site;
1934 /* Construct the SjLj_Function_Context. */
1936 static void
1937 sjlj_emit_function_enter (rtx dispatch_label)
1939 rtx fn_begin, fc, mem, seq;
1941 fc = cfun->eh->sjlj_fc;
1943 start_sequence ();
1945 /* We're storing this libcall's address into memory instead of
1946 calling it directly. Thus, we must call assemble_external_libcall
1947 here, as we can not depend on emit_library_call to do it for us. */
1948 assemble_external_libcall (eh_personality_libfunc);
1949 mem = adjust_address (fc, Pmode, sjlj_fc_personality_ofs);
1950 emit_move_insn (mem, eh_personality_libfunc);
1952 mem = adjust_address (fc, Pmode, sjlj_fc_lsda_ofs);
1953 if (cfun->uses_eh_lsda)
1955 char buf[20];
1956 rtx sym;
1958 ASM_GENERATE_INTERNAL_LABEL (buf, "LLSDA", current_function_funcdef_no);
1959 sym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
1960 SYMBOL_REF_FLAGS (sym) = SYMBOL_FLAG_LOCAL;
1961 emit_move_insn (mem, sym);
1963 else
1964 emit_move_insn (mem, const0_rtx);
1966 #ifdef DONT_USE_BUILTIN_SETJMP
1968 rtx x, note;
1969 x = emit_library_call_value (setjmp_libfunc, NULL_RTX, LCT_RETURNS_TWICE,
1970 TYPE_MODE (integer_type_node), 1,
1971 plus_constant (XEXP (fc, 0),
1972 sjlj_fc_jbuf_ofs), Pmode);
1974 note = emit_note (NOTE_INSN_EXPECTED_VALUE);
1975 NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, x, const0_rtx);
1977 emit_cmp_and_jump_insns (x, const0_rtx, NE, 0,
1978 TYPE_MODE (integer_type_node), 0, dispatch_label);
1980 #else
1981 expand_builtin_setjmp_setup (plus_constant (XEXP (fc, 0), sjlj_fc_jbuf_ofs),
1982 dispatch_label);
1983 #endif
1985 emit_library_call (unwind_sjlj_register_libfunc, LCT_NORMAL, VOIDmode,
1986 1, XEXP (fc, 0), Pmode);
1988 seq = get_insns ();
1989 end_sequence ();
1991 /* ??? Instead of doing this at the beginning of the function,
1992 do this in a block that is at loop level 0 and dominates all
1993 can_throw_internal instructions. */
1995 for (fn_begin = get_insns (); ; fn_begin = NEXT_INSN (fn_begin))
1996 if (NOTE_P (fn_begin)
1997 && (NOTE_LINE_NUMBER (fn_begin) == NOTE_INSN_FUNCTION_BEG
1998 || NOTE_LINE_NUMBER (fn_begin) == NOTE_INSN_BASIC_BLOCK))
1999 break;
2000 if (NOTE_LINE_NUMBER (fn_begin) == NOTE_INSN_FUNCTION_BEG)
2001 insert_insn_on_edge (seq, ENTRY_BLOCK_PTR->succ);
2002 else
2004 rtx last = BB_END (ENTRY_BLOCK_PTR->succ->dest);
2005 for (; ; fn_begin = NEXT_INSN (fn_begin))
2006 if ((NOTE_P (fn_begin)
2007 && NOTE_LINE_NUMBER (fn_begin) == NOTE_INSN_FUNCTION_BEG)
2008 || fn_begin == last)
2009 break;
2010 emit_insn_after (seq, fn_begin);
2014 /* Call back from expand_function_end to know where we should put
2015 the call to unwind_sjlj_unregister_libfunc if needed. */
2017 void
2018 sjlj_emit_function_exit_after (rtx after)
2020 cfun->eh->sjlj_exit_after = after;
2023 static void
2024 sjlj_emit_function_exit (void)
2026 rtx seq;
2027 edge e;
2029 start_sequence ();
2031 emit_library_call (unwind_sjlj_unregister_libfunc, LCT_NORMAL, VOIDmode,
2032 1, XEXP (cfun->eh->sjlj_fc, 0), Pmode);
2034 seq = get_insns ();
2035 end_sequence ();
2037 /* ??? Really this can be done in any block at loop level 0 that
2038 post-dominates all can_throw_internal instructions. This is
2039 the last possible moment. */
2041 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
2042 if (e->flags & EDGE_FALLTHRU)
2043 break;
2044 if (e)
2046 rtx insn;
2048 /* Figure out whether the place we are supposed to insert libcall
2049 is inside the last basic block or after it. In the other case
2050 we need to emit to edge. */
2051 if (e->src->next_bb != EXIT_BLOCK_PTR)
2052 abort ();
2053 for (insn = NEXT_INSN (BB_END (e->src)); insn; insn = NEXT_INSN (insn))
2054 if (insn == cfun->eh->sjlj_exit_after)
2055 break;
2056 if (insn)
2057 insert_insn_on_edge (seq, e);
2058 else
2060 insn = cfun->eh->sjlj_exit_after;
2061 if (LABEL_P (insn))
2062 insn = NEXT_INSN (insn);
2063 emit_insn_after (seq, insn);
2068 static void
2069 sjlj_emit_dispatch_table (rtx dispatch_label, struct sjlj_lp_info *lp_info)
2071 int i, first_reachable;
2072 rtx mem, dispatch, seq, fc;
2073 rtx before;
2074 basic_block bb;
2075 edge e;
2077 fc = cfun->eh->sjlj_fc;
2079 start_sequence ();
2081 emit_label (dispatch_label);
2083 #ifndef DONT_USE_BUILTIN_SETJMP
2084 expand_builtin_setjmp_receiver (dispatch_label);
2085 #endif
2087 /* Load up dispatch index, exc_ptr and filter values from the
2088 function context. */
2089 mem = adjust_address (fc, TYPE_MODE (integer_type_node),
2090 sjlj_fc_call_site_ofs);
2091 dispatch = copy_to_reg (mem);
2093 mem = adjust_address (fc, word_mode, sjlj_fc_data_ofs);
2094 if (word_mode != ptr_mode)
2096 #ifdef POINTERS_EXTEND_UNSIGNED
2097 mem = convert_memory_address (ptr_mode, mem);
2098 #else
2099 mem = convert_to_mode (ptr_mode, mem, 0);
2100 #endif
2102 emit_move_insn (cfun->eh->exc_ptr, mem);
2104 mem = adjust_address (fc, word_mode, sjlj_fc_data_ofs + UNITS_PER_WORD);
2105 emit_move_insn (cfun->eh->filter, mem);
2107 /* Jump to one of the directly reachable regions. */
2108 /* ??? This really ought to be using a switch statement. */
2110 first_reachable = 0;
2111 for (i = cfun->eh->last_region_number; i > 0; --i)
2113 if (! lp_info[i].directly_reachable)
2114 continue;
2116 if (! first_reachable)
2118 first_reachable = i;
2119 continue;
2122 emit_cmp_and_jump_insns (dispatch, GEN_INT (lp_info[i].dispatch_index),
2123 EQ, NULL_RTX, TYPE_MODE (integer_type_node), 0,
2124 cfun->eh->region_array[i]->post_landing_pad);
2127 seq = get_insns ();
2128 end_sequence ();
2130 before = cfun->eh->region_array[first_reachable]->post_landing_pad;
2132 bb = emit_to_new_bb_before (seq, before);
2133 e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
2134 e->count = bb->count;
2135 e->probability = REG_BR_PROB_BASE;
2138 static void
2139 sjlj_build_landing_pads (void)
2141 struct sjlj_lp_info *lp_info;
2143 lp_info = xcalloc (cfun->eh->last_region_number + 1,
2144 sizeof (struct sjlj_lp_info));
2146 if (sjlj_find_directly_reachable_regions (lp_info))
2148 rtx dispatch_label = gen_label_rtx ();
2150 cfun->eh->sjlj_fc
2151 = assign_stack_local (TYPE_MODE (sjlj_fc_type_node),
2152 int_size_in_bytes (sjlj_fc_type_node),
2153 TYPE_ALIGN (sjlj_fc_type_node));
2155 sjlj_assign_call_site_values (dispatch_label, lp_info);
2156 sjlj_mark_call_sites (lp_info);
2158 sjlj_emit_function_enter (dispatch_label);
2159 sjlj_emit_dispatch_table (dispatch_label, lp_info);
2160 sjlj_emit_function_exit ();
2163 free (lp_info);
2166 void
2167 finish_eh_generation (void)
2169 basic_block bb;
2171 /* Nothing to do if no regions created. */
2172 if (cfun->eh->region_tree == NULL)
2173 return;
2175 /* The object here is to provide find_basic_blocks with detailed
2176 information (via reachable_handlers) on how exception control
2177 flows within the function. In this first pass, we can include
2178 type information garnered from ERT_THROW and ERT_ALLOWED_EXCEPTIONS
2179 regions, and hope that it will be useful in deleting unreachable
2180 handlers. Subsequently, we will generate landing pads which will
2181 connect many of the handlers, and then type information will not
2182 be effective. Still, this is a win over previous implementations. */
2184 /* These registers are used by the landing pads. Make sure they
2185 have been generated. */
2186 get_exception_pointer (cfun);
2187 get_exception_filter (cfun);
2189 /* Construct the landing pads. */
2191 assign_filter_values ();
2192 build_post_landing_pads ();
2193 connect_post_landing_pads ();
2194 if (USING_SJLJ_EXCEPTIONS)
2195 sjlj_build_landing_pads ();
2196 else
2197 dw2_build_landing_pads ();
2199 cfun->eh->built_landing_pads = 1;
2201 /* We've totally changed the CFG. Start over. */
2202 find_exception_handler_labels ();
2203 break_superblocks ();
2204 if (USING_SJLJ_EXCEPTIONS)
2205 commit_edge_insertions ();
2206 FOR_EACH_BB (bb)
2208 edge e, next;
2209 bool eh = false;
2210 for (e = bb->succ; e; e = next)
2212 next = e->succ_next;
2213 if (e->flags & EDGE_EH)
2215 remove_edge (e);
2216 eh = true;
2219 if (eh)
2220 rtl_make_eh_edge (NULL, bb, BB_END (bb));
2224 static hashval_t
2225 ehl_hash (const void *pentry)
2227 struct ehl_map_entry *entry = (struct ehl_map_entry *) pentry;
2229 /* 2^32 * ((sqrt(5) - 1) / 2) */
2230 const hashval_t scaled_golden_ratio = 0x9e3779b9;
2231 return CODE_LABEL_NUMBER (entry->label) * scaled_golden_ratio;
2234 static int
2235 ehl_eq (const void *pentry, const void *pdata)
2237 struct ehl_map_entry *entry = (struct ehl_map_entry *) pentry;
2238 struct ehl_map_entry *data = (struct ehl_map_entry *) pdata;
2240 return entry->label == data->label;
2243 /* This section handles removing dead code for flow. */
2245 /* Remove LABEL from exception_handler_label_map. */
2247 static void
2248 remove_exception_handler_label (rtx label)
2250 struct ehl_map_entry **slot, tmp;
2252 /* If exception_handler_label_map was not built yet,
2253 there is nothing to do. */
2254 if (cfun->eh->exception_handler_label_map == NULL)
2255 return;
2257 tmp.label = label;
2258 slot = (struct ehl_map_entry **)
2259 htab_find_slot (cfun->eh->exception_handler_label_map, &tmp, NO_INSERT);
2260 if (! slot)
2261 abort ();
2263 htab_clear_slot (cfun->eh->exception_handler_label_map, (void **) slot);
2266 /* Splice REGION from the region tree etc. */
2268 static void
2269 remove_eh_handler (struct eh_region *region)
2271 struct eh_region **pp, **pp_start, *p, *outer, *inner;
2272 rtx lab;
2274 /* For the benefit of efficiently handling REG_EH_REGION notes,
2275 replace this region in the region array with its containing
2276 region. Note that previous region deletions may result in
2277 multiple copies of this region in the array, so we have a
2278 list of alternate numbers by which we are known. */
2280 outer = region->outer;
2281 cfun->eh->region_array[region->region_number] = outer;
2282 if (region->aka)
2284 int i;
2285 EXECUTE_IF_SET_IN_BITMAP (region->aka, 0, i,
2286 { cfun->eh->region_array[i] = outer; });
2289 if (outer)
2291 if (!outer->aka)
2292 outer->aka = BITMAP_GGC_ALLOC ();
2293 if (region->aka)
2294 bitmap_a_or_b (outer->aka, outer->aka, region->aka);
2295 bitmap_set_bit (outer->aka, region->region_number);
2298 if (cfun->eh->built_landing_pads)
2299 lab = region->landing_pad;
2300 else
2301 lab = region->label;
2302 if (lab)
2303 remove_exception_handler_label (lab);
2305 if (outer)
2306 pp_start = &outer->inner;
2307 else
2308 pp_start = &cfun->eh->region_tree;
2309 for (pp = pp_start, p = *pp; p != region; pp = &p->next_peer, p = *pp)
2310 continue;
2311 *pp = region->next_peer;
2313 inner = region->inner;
2314 if (inner)
2316 for (p = inner; p->next_peer ; p = p->next_peer)
2317 p->outer = outer;
2318 p->outer = outer;
2320 p->next_peer = *pp_start;
2321 *pp_start = inner;
2324 if (region->type == ERT_CATCH)
2326 struct eh_region *try, *next, *prev;
2328 for (try = region->next_peer;
2329 try->type == ERT_CATCH;
2330 try = try->next_peer)
2331 continue;
2332 if (try->type != ERT_TRY)
2333 abort ();
2335 next = region->u.catch.next_catch;
2336 prev = region->u.catch.prev_catch;
2338 if (next)
2339 next->u.catch.prev_catch = prev;
2340 else
2341 try->u.try.last_catch = prev;
2342 if (prev)
2343 prev->u.catch.next_catch = next;
2344 else
2346 try->u.try.catch = next;
2347 if (! next)
2348 remove_eh_handler (try);
2353 /* LABEL heads a basic block that is about to be deleted. If this
2354 label corresponds to an exception region, we may be able to
2355 delete the region. */
2357 void
2358 maybe_remove_eh_handler (rtx label)
2360 struct ehl_map_entry **slot, tmp;
2361 struct eh_region *region;
2363 /* ??? After generating landing pads, it's not so simple to determine
2364 if the region data is completely unused. One must examine the
2365 landing pad and the post landing pad, and whether an inner try block
2366 is referencing the catch handlers directly. */
2367 if (cfun->eh->built_landing_pads)
2368 return;
2370 tmp.label = label;
2371 slot = (struct ehl_map_entry **)
2372 htab_find_slot (cfun->eh->exception_handler_label_map, &tmp, NO_INSERT);
2373 if (! slot)
2374 return;
2375 region = (*slot)->region;
2376 if (! region)
2377 return;
2379 /* Flow will want to remove MUST_NOT_THROW regions as unreachable
2380 because there is no path to the fallback call to terminate.
2381 But the region continues to affect call-site data until there
2382 are no more contained calls, which we don't see here. */
2383 if (region->type == ERT_MUST_NOT_THROW)
2385 htab_clear_slot (cfun->eh->exception_handler_label_map, (void **) slot);
2386 region->label = NULL_RTX;
2388 else
2389 remove_eh_handler (region);
2392 /* Invokes CALLBACK for every exception handler label. Only used by old
2393 loop hackery; should not be used by new code. */
2395 void
2396 for_each_eh_label (void (*callback) (rtx))
2398 htab_traverse (cfun->eh->exception_handler_label_map, for_each_eh_label_1,
2399 (void *) &callback);
2402 static int
2403 for_each_eh_label_1 (void **pentry, void *data)
2405 struct ehl_map_entry *entry = *(struct ehl_map_entry **)pentry;
2406 void (*callback) (rtx) = *(void (**) (rtx)) data;
2408 (*callback) (entry->label);
2409 return 1;
2412 /* Invoke CALLBACK for every exception region in the current function. */
2414 void
2415 for_each_eh_region (void (*callback) (struct eh_region *))
2417 int i, n = cfun->eh->last_region_number;
2418 for (i = 1; i <= n; ++i)
2420 struct eh_region *region = cfun->eh->region_array[i];
2421 if (region)
2422 (*callback) (region);
2426 /* This section describes CFG exception edges for flow. */
2428 /* For communicating between calls to reachable_next_level. */
2429 struct reachable_info
2431 tree types_caught;
2432 tree types_allowed;
2433 void (*callback) (struct eh_region *, void *);
2434 void *callback_data;
2435 bool saw_any_handlers;
2438 /* A subroutine of reachable_next_level. Return true if TYPE, or a
2439 base class of TYPE, is in HANDLED. */
2442 check_handled (tree handled, tree type)
2444 tree t;
2446 /* We can check for exact matches without front-end help. */
2447 if (! lang_eh_type_covers)
2449 for (t = handled; t ; t = TREE_CHAIN (t))
2450 if (TREE_VALUE (t) == type)
2451 return 1;
2453 else
2455 for (t = handled; t ; t = TREE_CHAIN (t))
2456 if ((*lang_eh_type_covers) (TREE_VALUE (t), type))
2457 return 1;
2460 return 0;
2463 /* A subroutine of reachable_next_level. If we are collecting a list
2464 of handlers, add one. After landing pad generation, reference
2465 it instead of the handlers themselves. Further, the handlers are
2466 all wired together, so by referencing one, we've got them all.
2467 Before landing pad generation we reference each handler individually.
2469 LP_REGION contains the landing pad; REGION is the handler. */
2471 static void
2472 add_reachable_handler (struct reachable_info *info,
2473 struct eh_region *lp_region, struct eh_region *region)
2475 if (! info)
2476 return;
2478 info->saw_any_handlers = true;
2480 if (cfun->eh->built_landing_pads)
2481 info->callback (lp_region, info->callback_data);
2482 else
2483 info->callback (region, info->callback_data);
2486 /* Process one level of exception regions for reachability.
2487 If TYPE_THROWN is non-null, then it is the *exact* type being
2488 propagated. If INFO is non-null, then collect handler labels
2489 and caught/allowed type information between invocations. */
2491 static enum reachable_code
2492 reachable_next_level (struct eh_region *region, tree type_thrown,
2493 struct reachable_info *info)
2495 switch (region->type)
2497 case ERT_CLEANUP:
2498 /* Before landing-pad generation, we model control flow
2499 directly to the individual handlers. In this way we can
2500 see that catch handler types may shadow one another. */
2501 add_reachable_handler (info, region, region);
2502 return RNL_MAYBE_CAUGHT;
2504 case ERT_TRY:
2506 struct eh_region *c;
2507 enum reachable_code ret = RNL_NOT_CAUGHT;
2509 for (c = region->u.try.catch; c ; c = c->u.catch.next_catch)
2511 /* A catch-all handler ends the search. */
2512 if (c->u.catch.type_list == NULL)
2514 add_reachable_handler (info, region, c);
2515 return RNL_CAUGHT;
2518 if (type_thrown)
2520 /* If we have at least one type match, end the search. */
2521 tree tp_node = c->u.catch.type_list;
2523 for (; tp_node; tp_node = TREE_CHAIN (tp_node))
2525 tree type = TREE_VALUE (tp_node);
2527 if (type == type_thrown
2528 || (lang_eh_type_covers
2529 && (*lang_eh_type_covers) (type, type_thrown)))
2531 add_reachable_handler (info, region, c);
2532 return RNL_CAUGHT;
2536 /* If we have definitive information of a match failure,
2537 the catch won't trigger. */
2538 if (lang_eh_type_covers)
2539 return RNL_NOT_CAUGHT;
2542 /* At this point, we either don't know what type is thrown or
2543 don't have front-end assistance to help deciding if it is
2544 covered by one of the types in the list for this region.
2546 We'd then like to add this region to the list of reachable
2547 handlers since it is indeed potentially reachable based on the
2548 information we have.
2550 Actually, this handler is for sure not reachable if all the
2551 types it matches have already been caught. That is, it is only
2552 potentially reachable if at least one of the types it catches
2553 has not been previously caught. */
2555 if (! info)
2556 ret = RNL_MAYBE_CAUGHT;
2557 else
2559 tree tp_node = c->u.catch.type_list;
2560 bool maybe_reachable = false;
2562 /* Compute the potential reachability of this handler and
2563 update the list of types caught at the same time. */
2564 for (; tp_node; tp_node = TREE_CHAIN (tp_node))
2566 tree type = TREE_VALUE (tp_node);
2568 if (! check_handled (info->types_caught, type))
2570 info->types_caught
2571 = tree_cons (NULL, type, info->types_caught);
2573 maybe_reachable = true;
2577 if (maybe_reachable)
2579 add_reachable_handler (info, region, c);
2581 /* ??? If the catch type is a base class of every allowed
2582 type, then we know we can stop the search. */
2583 ret = RNL_MAYBE_CAUGHT;
2588 return ret;
2591 case ERT_ALLOWED_EXCEPTIONS:
2592 /* An empty list of types definitely ends the search. */
2593 if (region->u.allowed.type_list == NULL_TREE)
2595 add_reachable_handler (info, region, region);
2596 return RNL_CAUGHT;
2599 /* Collect a list of lists of allowed types for use in detecting
2600 when a catch may be transformed into a catch-all. */
2601 if (info)
2602 info->types_allowed = tree_cons (NULL_TREE,
2603 region->u.allowed.type_list,
2604 info->types_allowed);
2606 /* If we have definitive information about the type hierarchy,
2607 then we can tell if the thrown type will pass through the
2608 filter. */
2609 if (type_thrown && lang_eh_type_covers)
2611 if (check_handled (region->u.allowed.type_list, type_thrown))
2612 return RNL_NOT_CAUGHT;
2613 else
2615 add_reachable_handler (info, region, region);
2616 return RNL_CAUGHT;
2620 add_reachable_handler (info, region, region);
2621 return RNL_MAYBE_CAUGHT;
2623 case ERT_CATCH:
2624 /* Catch regions are handled by their controlling try region. */
2625 return RNL_NOT_CAUGHT;
2627 case ERT_MUST_NOT_THROW:
2628 /* Here we end our search, since no exceptions may propagate.
2629 If we've touched down at some landing pad previous, then the
2630 explicit function call we generated may be used. Otherwise
2631 the call is made by the runtime. */
2632 if (info && info->saw_any_handlers)
2634 add_reachable_handler (info, region, region);
2635 return RNL_CAUGHT;
2637 else
2638 return RNL_BLOCKED;
2640 case ERT_THROW:
2641 case ERT_FIXUP:
2642 case ERT_UNKNOWN:
2643 /* Shouldn't see these here. */
2644 break;
2647 abort ();
2650 /* Invoke CALLBACK on each region reachable from REGION_NUMBER. */
2652 void
2653 foreach_reachable_handler (int region_number, bool is_resx,
2654 void (*callback) (struct eh_region *, void *),
2655 void *callback_data)
2657 struct reachable_info info;
2658 struct eh_region *region;
2659 tree type_thrown;
2661 memset (&info, 0, sizeof (info));
2662 info.callback = callback;
2663 info.callback_data = callback_data;
2665 region = cfun->eh->region_array[region_number];
2667 type_thrown = NULL_TREE;
2668 if (is_resx)
2670 /* A RESX leaves a region instead of entering it. Thus the
2671 region itself may have been deleted out from under us. */
2672 if (region == NULL)
2673 return;
2674 region = region->outer;
2676 else if (region->type == ERT_THROW)
2678 type_thrown = region->u.throw.type;
2679 region = region->outer;
2682 while (region)
2684 if (reachable_next_level (region, type_thrown, &info) >= RNL_CAUGHT)
2685 break;
2686 /* If we have processed one cleanup, there is no point in
2687 processing any more of them. Each cleanup will have an edge
2688 to the next outer cleanup region, so the flow graph will be
2689 accurate. */
2690 if (region->type == ERT_CLEANUP)
2691 region = region->u.cleanup.prev_try;
2692 else
2693 region = region->outer;
2697 /* Retrieve a list of labels of exception handlers which can be
2698 reached by a given insn. */
2700 static void
2701 arh_to_landing_pad (struct eh_region *region, void *data)
2703 rtx *p_handlers = data;
2704 if (! *p_handlers)
2705 *p_handlers = alloc_INSN_LIST (region->landing_pad, NULL_RTX);
2708 static void
2709 arh_to_label (struct eh_region *region, void *data)
2711 rtx *p_handlers = data;
2712 *p_handlers = alloc_INSN_LIST (region->label, *p_handlers);
2716 reachable_handlers (rtx insn)
2718 bool is_resx = false;
2719 rtx handlers = NULL;
2720 int region_number;
2722 if (JUMP_P (insn)
2723 && GET_CODE (PATTERN (insn)) == RESX)
2725 region_number = XINT (PATTERN (insn), 0);
2726 is_resx = true;
2728 else
2730 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2731 if (!note || INTVAL (XEXP (note, 0)) <= 0)
2732 return NULL;
2733 region_number = INTVAL (XEXP (note, 0));
2736 foreach_reachable_handler (region_number, is_resx,
2737 (cfun->eh->built_landing_pads
2738 ? arh_to_landing_pad
2739 : arh_to_label),
2740 &handlers);
2742 return handlers;
2745 /* Determine if the given INSN can throw an exception that is caught
2746 within the function. */
2748 bool
2749 can_throw_internal_1 (int region_number)
2751 struct eh_region *region;
2752 tree type_thrown;
2754 region = cfun->eh->region_array[region_number];
2756 type_thrown = NULL_TREE;
2757 if (region->type == ERT_THROW)
2759 type_thrown = region->u.throw.type;
2760 region = region->outer;
2763 /* If this exception is ignored by each and every containing region,
2764 then control passes straight out. The runtime may handle some
2765 regions, which also do not require processing internally. */
2766 for (; region; region = region->outer)
2768 enum reachable_code how = reachable_next_level (region, type_thrown, 0);
2769 if (how == RNL_BLOCKED)
2770 return false;
2771 if (how != RNL_NOT_CAUGHT)
2772 return true;
2775 return false;
2778 bool
2779 can_throw_internal (rtx insn)
2781 rtx note;
2783 if (! INSN_P (insn))
2784 return false;
2786 if (JUMP_P (insn)
2787 && GET_CODE (PATTERN (insn)) == RESX
2788 && XINT (PATTERN (insn), 0) > 0)
2789 return can_throw_internal_1 (XINT (PATTERN (insn), 0));
2791 if (NONJUMP_INSN_P (insn)
2792 && GET_CODE (PATTERN (insn)) == SEQUENCE)
2793 insn = XVECEXP (PATTERN (insn), 0, 0);
2795 /* Every insn that might throw has an EH_REGION note. */
2796 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2797 if (!note || INTVAL (XEXP (note, 0)) <= 0)
2798 return false;
2800 return can_throw_internal_1 (INTVAL (XEXP (note, 0)));
2803 /* Determine if the given INSN can throw an exception that is
2804 visible outside the function. */
2806 bool
2807 can_throw_external_1 (int region_number)
2809 struct eh_region *region;
2810 tree type_thrown;
2812 region = cfun->eh->region_array[region_number];
2814 type_thrown = NULL_TREE;
2815 if (region->type == ERT_THROW)
2817 type_thrown = region->u.throw.type;
2818 region = region->outer;
2821 /* If the exception is caught or blocked by any containing region,
2822 then it is not seen by any calling function. */
2823 for (; region ; region = region->outer)
2824 if (reachable_next_level (region, type_thrown, NULL) >= RNL_CAUGHT)
2825 return false;
2827 return true;
2830 bool
2831 can_throw_external (rtx insn)
2833 rtx note;
2835 if (! INSN_P (insn))
2836 return false;
2838 if (NONJUMP_INSN_P (insn)
2839 && GET_CODE (PATTERN (insn)) == SEQUENCE)
2840 insn = XVECEXP (PATTERN (insn), 0, 0);
2842 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2843 if (!note)
2845 /* Calls (and trapping insns) without notes are outside any
2846 exception handling region in this function. We have to
2847 assume it might throw. Given that the front end and middle
2848 ends mark known NOTHROW functions, this isn't so wildly
2849 inaccurate. */
2850 return (CALL_P (insn)
2851 || (flag_non_call_exceptions
2852 && may_trap_p (PATTERN (insn))));
2854 if (INTVAL (XEXP (note, 0)) <= 0)
2855 return false;
2857 return can_throw_external_1 (INTVAL (XEXP (note, 0)));
2860 /* Set TREE_NOTHROW and cfun->all_throwers_are_sibcalls. */
2862 void
2863 set_nothrow_function_flags (void)
2865 rtx insn;
2867 TREE_NOTHROW (current_function_decl) = 1;
2869 /* Assume cfun->all_throwers_are_sibcalls until we encounter
2870 something that can throw an exception. We specifically exempt
2871 CALL_INSNs that are SIBLING_CALL_P, as these are really jumps,
2872 and can't throw. Most CALL_INSNs are not SIBLING_CALL_P, so this
2873 is optimistic. */
2875 cfun->all_throwers_are_sibcalls = 1;
2877 if (! flag_exceptions)
2878 return;
2880 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2881 if (can_throw_external (insn))
2883 TREE_NOTHROW (current_function_decl) = 0;
2885 if (!CALL_P (insn) || !SIBLING_CALL_P (insn))
2887 cfun->all_throwers_are_sibcalls = 0;
2888 return;
2892 for (insn = current_function_epilogue_delay_list; insn;
2893 insn = XEXP (insn, 1))
2894 if (can_throw_external (insn))
2896 TREE_NOTHROW (current_function_decl) = 0;
2898 if (!CALL_P (insn) || !SIBLING_CALL_P (insn))
2900 cfun->all_throwers_are_sibcalls = 0;
2901 return;
2907 /* Various hooks for unwind library. */
2909 /* Do any necessary initialization to access arbitrary stack frames.
2910 On the SPARC, this means flushing the register windows. */
2912 void
2913 expand_builtin_unwind_init (void)
2915 /* Set this so all the registers get saved in our frame; we need to be
2916 able to copy the saved values for any registers from frames we unwind. */
2917 current_function_has_nonlocal_label = 1;
2919 #ifdef SETUP_FRAME_ADDRESSES
2920 SETUP_FRAME_ADDRESSES ();
2921 #endif
2925 expand_builtin_eh_return_data_regno (tree arglist)
2927 tree which = TREE_VALUE (arglist);
2928 unsigned HOST_WIDE_INT iwhich;
2930 if (TREE_CODE (which) != INTEGER_CST)
2932 error ("argument of `__builtin_eh_return_regno' must be constant");
2933 return constm1_rtx;
2936 iwhich = tree_low_cst (which, 1);
2937 iwhich = EH_RETURN_DATA_REGNO (iwhich);
2938 if (iwhich == INVALID_REGNUM)
2939 return constm1_rtx;
2941 #ifdef DWARF_FRAME_REGNUM
2942 iwhich = DWARF_FRAME_REGNUM (iwhich);
2943 #else
2944 iwhich = DBX_REGISTER_NUMBER (iwhich);
2945 #endif
2947 return GEN_INT (iwhich);
2950 /* Given a value extracted from the return address register or stack slot,
2951 return the actual address encoded in that value. */
2954 expand_builtin_extract_return_addr (tree addr_tree)
2956 rtx addr = expand_expr (addr_tree, NULL_RTX, Pmode, 0);
2958 if (GET_MODE (addr) != Pmode
2959 && GET_MODE (addr) != VOIDmode)
2961 #ifdef POINTERS_EXTEND_UNSIGNED
2962 addr = convert_memory_address (Pmode, addr);
2963 #else
2964 addr = convert_to_mode (Pmode, addr, 0);
2965 #endif
2968 /* First mask out any unwanted bits. */
2969 #ifdef MASK_RETURN_ADDR
2970 expand_and (Pmode, addr, MASK_RETURN_ADDR, addr);
2971 #endif
2973 /* Then adjust to find the real return address. */
2974 #if defined (RETURN_ADDR_OFFSET)
2975 addr = plus_constant (addr, RETURN_ADDR_OFFSET);
2976 #endif
2978 return addr;
2981 /* Given an actual address in addr_tree, do any necessary encoding
2982 and return the value to be stored in the return address register or
2983 stack slot so the epilogue will return to that address. */
2986 expand_builtin_frob_return_addr (tree addr_tree)
2988 rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, 0);
2990 addr = convert_memory_address (Pmode, addr);
2992 #ifdef RETURN_ADDR_OFFSET
2993 addr = force_reg (Pmode, addr);
2994 addr = plus_constant (addr, -RETURN_ADDR_OFFSET);
2995 #endif
2997 return addr;
3000 /* Set up the epilogue with the magic bits we'll need to return to the
3001 exception handler. */
3003 void
3004 expand_builtin_eh_return (tree stackadj_tree ATTRIBUTE_UNUSED,
3005 tree handler_tree)
3007 rtx tmp;
3009 #ifdef EH_RETURN_STACKADJ_RTX
3010 tmp = expand_expr (stackadj_tree, cfun->eh->ehr_stackadj, VOIDmode, 0);
3011 tmp = convert_memory_address (Pmode, tmp);
3012 if (!cfun->eh->ehr_stackadj)
3013 cfun->eh->ehr_stackadj = copy_to_reg (tmp);
3014 else if (tmp != cfun->eh->ehr_stackadj)
3015 emit_move_insn (cfun->eh->ehr_stackadj, tmp);
3016 #endif
3018 tmp = expand_expr (handler_tree, cfun->eh->ehr_handler, VOIDmode, 0);
3019 tmp = convert_memory_address (Pmode, tmp);
3020 if (!cfun->eh->ehr_handler)
3021 cfun->eh->ehr_handler = copy_to_reg (tmp);
3022 else if (tmp != cfun->eh->ehr_handler)
3023 emit_move_insn (cfun->eh->ehr_handler, tmp);
3025 if (!cfun->eh->ehr_label)
3026 cfun->eh->ehr_label = gen_label_rtx ();
3027 emit_jump (cfun->eh->ehr_label);
3030 void
3031 expand_eh_return (void)
3033 rtx around_label;
3035 if (! cfun->eh->ehr_label)
3036 return;
3038 current_function_calls_eh_return = 1;
3040 #ifdef EH_RETURN_STACKADJ_RTX
3041 emit_move_insn (EH_RETURN_STACKADJ_RTX, const0_rtx);
3042 #endif
3044 around_label = gen_label_rtx ();
3045 emit_jump (around_label);
3047 emit_label (cfun->eh->ehr_label);
3048 clobber_return_register ();
3050 #ifdef EH_RETURN_STACKADJ_RTX
3051 emit_move_insn (EH_RETURN_STACKADJ_RTX, cfun->eh->ehr_stackadj);
3052 #endif
3054 #ifdef HAVE_eh_return
3055 if (HAVE_eh_return)
3056 emit_insn (gen_eh_return (cfun->eh->ehr_handler));
3057 else
3058 #endif
3060 #ifdef EH_RETURN_HANDLER_RTX
3061 emit_move_insn (EH_RETURN_HANDLER_RTX, cfun->eh->ehr_handler);
3062 #else
3063 error ("__builtin_eh_return not supported on this target");
3064 #endif
3067 emit_label (around_label);
3070 /* Convert a ptr_mode address ADDR_TREE to a Pmode address controlled by
3071 POINTERS_EXTEND_UNSIGNED and return it. */
3074 expand_builtin_extend_pointer (tree addr_tree)
3076 rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, 0);
3077 int extend;
3079 #ifdef POINTERS_EXTEND_UNSIGNED
3080 extend = POINTERS_EXTEND_UNSIGNED;
3081 #else
3082 /* The previous EH code did an unsigned extend by default, so we do this also
3083 for consistency. */
3084 extend = 1;
3085 #endif
3087 return convert_modes (word_mode, ptr_mode, addr, extend);
3090 /* In the following functions, we represent entries in the action table
3091 as 1-based indices. Special cases are:
3093 0: null action record, non-null landing pad; implies cleanups
3094 -1: null action record, null landing pad; implies no action
3095 -2: no call-site entry; implies must_not_throw
3096 -3: we have yet to process outer regions
3098 Further, no special cases apply to the "next" field of the record.
3099 For next, 0 means end of list. */
3101 struct action_record
3103 int offset;
3104 int filter;
3105 int next;
3108 static int
3109 action_record_eq (const void *pentry, const void *pdata)
3111 const struct action_record *entry = (const struct action_record *) pentry;
3112 const struct action_record *data = (const struct action_record *) pdata;
3113 return entry->filter == data->filter && entry->next == data->next;
3116 static hashval_t
3117 action_record_hash (const void *pentry)
3119 const struct action_record *entry = (const struct action_record *) pentry;
3120 return entry->next * 1009 + entry->filter;
3123 static int
3124 add_action_record (htab_t ar_hash, int filter, int next)
3126 struct action_record **slot, *new, tmp;
3128 tmp.filter = filter;
3129 tmp.next = next;
3130 slot = (struct action_record **) htab_find_slot (ar_hash, &tmp, INSERT);
3132 if ((new = *slot) == NULL)
3134 new = xmalloc (sizeof (*new));
3135 new->offset = VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data) + 1;
3136 new->filter = filter;
3137 new->next = next;
3138 *slot = new;
3140 /* The filter value goes in untouched. The link to the next
3141 record is a "self-relative" byte offset, or zero to indicate
3142 that there is no next record. So convert the absolute 1 based
3143 indices we've been carrying around into a displacement. */
3145 push_sleb128 (&cfun->eh->action_record_data, filter);
3146 if (next)
3147 next -= VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data) + 1;
3148 push_sleb128 (&cfun->eh->action_record_data, next);
3151 return new->offset;
3154 static int
3155 collect_one_action_chain (htab_t ar_hash, struct eh_region *region)
3157 struct eh_region *c;
3158 int next;
3160 /* If we've reached the top of the region chain, then we have
3161 no actions, and require no landing pad. */
3162 if (region == NULL)
3163 return -1;
3165 switch (region->type)
3167 case ERT_CLEANUP:
3168 /* A cleanup adds a zero filter to the beginning of the chain, but
3169 there are special cases to look out for. If there are *only*
3170 cleanups along a path, then it compresses to a zero action.
3171 Further, if there are multiple cleanups along a path, we only
3172 need to represent one of them, as that is enough to trigger
3173 entry to the landing pad at runtime. */
3174 next = collect_one_action_chain (ar_hash, region->outer);
3175 if (next <= 0)
3176 return 0;
3177 for (c = region->outer; c ; c = c->outer)
3178 if (c->type == ERT_CLEANUP)
3179 return next;
3180 return add_action_record (ar_hash, 0, next);
3182 case ERT_TRY:
3183 /* Process the associated catch regions in reverse order.
3184 If there's a catch-all handler, then we don't need to
3185 search outer regions. Use a magic -3 value to record
3186 that we haven't done the outer search. */
3187 next = -3;
3188 for (c = region->u.try.last_catch; c ; c = c->u.catch.prev_catch)
3190 if (c->u.catch.type_list == NULL)
3192 /* Retrieve the filter from the head of the filter list
3193 where we have stored it (see assign_filter_values). */
3194 int filter
3195 = TREE_INT_CST_LOW (TREE_VALUE (c->u.catch.filter_list));
3197 next = add_action_record (ar_hash, filter, 0);
3199 else
3201 /* Once the outer search is done, trigger an action record for
3202 each filter we have. */
3203 tree flt_node;
3205 if (next == -3)
3207 next = collect_one_action_chain (ar_hash, region->outer);
3209 /* If there is no next action, terminate the chain. */
3210 if (next == -1)
3211 next = 0;
3212 /* If all outer actions are cleanups or must_not_throw,
3213 we'll have no action record for it, since we had wanted
3214 to encode these states in the call-site record directly.
3215 Add a cleanup action to the chain to catch these. */
3216 else if (next <= 0)
3217 next = add_action_record (ar_hash, 0, 0);
3220 flt_node = c->u.catch.filter_list;
3221 for (; flt_node; flt_node = TREE_CHAIN (flt_node))
3223 int filter = TREE_INT_CST_LOW (TREE_VALUE (flt_node));
3224 next = add_action_record (ar_hash, filter, next);
3228 return next;
3230 case ERT_ALLOWED_EXCEPTIONS:
3231 /* An exception specification adds its filter to the
3232 beginning of the chain. */
3233 next = collect_one_action_chain (ar_hash, region->outer);
3235 /* If there is no next action, terminate the chain. */
3236 if (next == -1)
3237 next = 0;
3238 /* If all outer actions are cleanups or must_not_throw,
3239 we'll have no action record for it, since we had wanted
3240 to encode these states in the call-site record directly.
3241 Add a cleanup action to the chain to catch these. */
3242 else if (next <= 0)
3243 next = add_action_record (ar_hash, 0, 0);
3245 return add_action_record (ar_hash, region->u.allowed.filter, next);
3247 case ERT_MUST_NOT_THROW:
3248 /* A must-not-throw region with no inner handlers or cleanups
3249 requires no call-site entry. Note that this differs from
3250 the no handler or cleanup case in that we do require an lsda
3251 to be generated. Return a magic -2 value to record this. */
3252 return -2;
3254 case ERT_CATCH:
3255 case ERT_THROW:
3256 /* CATCH regions are handled in TRY above. THROW regions are
3257 for optimization information only and produce no output. */
3258 return collect_one_action_chain (ar_hash, region->outer);
3260 default:
3261 abort ();
3265 static int
3266 add_call_site (rtx landing_pad, int action)
3268 struct call_site_record *data = cfun->eh->call_site_data;
3269 int used = cfun->eh->call_site_data_used;
3270 int size = cfun->eh->call_site_data_size;
3272 if (used >= size)
3274 size = (size ? size * 2 : 64);
3275 data = ggc_realloc (data, sizeof (*data) * size);
3276 cfun->eh->call_site_data = data;
3277 cfun->eh->call_site_data_size = size;
3280 data[used].landing_pad = landing_pad;
3281 data[used].action = action;
3283 cfun->eh->call_site_data_used = used + 1;
3285 return used + call_site_base;
3288 /* Turn REG_EH_REGION notes back into NOTE_INSN_EH_REGION notes.
3289 The new note numbers will not refer to region numbers, but
3290 instead to call site entries. */
3292 void
3293 convert_to_eh_region_ranges (void)
3295 rtx insn, iter, note;
3296 htab_t ar_hash;
3297 int last_action = -3;
3298 rtx last_action_insn = NULL_RTX;
3299 rtx last_landing_pad = NULL_RTX;
3300 rtx first_no_action_insn = NULL_RTX;
3301 int call_site = 0;
3303 if (USING_SJLJ_EXCEPTIONS || cfun->eh->region_tree == NULL)
3304 return;
3306 VARRAY_UCHAR_INIT (cfun->eh->action_record_data, 64, "action_record_data");
3308 ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
3310 for (iter = get_insns (); iter ; iter = NEXT_INSN (iter))
3311 if (INSN_P (iter))
3313 struct eh_region *region;
3314 int this_action;
3315 rtx this_landing_pad;
3317 insn = iter;
3318 if (NONJUMP_INSN_P (insn)
3319 && GET_CODE (PATTERN (insn)) == SEQUENCE)
3320 insn = XVECEXP (PATTERN (insn), 0, 0);
3322 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3323 if (!note)
3325 if (! (CALL_P (insn)
3326 || (flag_non_call_exceptions
3327 && may_trap_p (PATTERN (insn)))))
3328 continue;
3329 this_action = -1;
3330 region = NULL;
3332 else
3334 if (INTVAL (XEXP (note, 0)) <= 0)
3335 continue;
3336 region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
3337 this_action = collect_one_action_chain (ar_hash, region);
3340 /* Existence of catch handlers, or must-not-throw regions
3341 implies that an lsda is needed (even if empty). */
3342 if (this_action != -1)
3343 cfun->uses_eh_lsda = 1;
3345 /* Delay creation of region notes for no-action regions
3346 until we're sure that an lsda will be required. */
3347 else if (last_action == -3)
3349 first_no_action_insn = iter;
3350 last_action = -1;
3353 /* Cleanups and handlers may share action chains but not
3354 landing pads. Collect the landing pad for this region. */
3355 if (this_action >= 0)
3357 struct eh_region *o;
3358 for (o = region; ! o->landing_pad ; o = o->outer)
3359 continue;
3360 this_landing_pad = o->landing_pad;
3362 else
3363 this_landing_pad = NULL_RTX;
3365 /* Differing actions or landing pads implies a change in call-site
3366 info, which implies some EH_REGION note should be emitted. */
3367 if (last_action != this_action
3368 || last_landing_pad != this_landing_pad)
3370 /* If we'd not seen a previous action (-3) or the previous
3371 action was must-not-throw (-2), then we do not need an
3372 end note. */
3373 if (last_action >= -1)
3375 /* If we delayed the creation of the begin, do it now. */
3376 if (first_no_action_insn)
3378 call_site = add_call_site (NULL_RTX, 0);
3379 note = emit_note_before (NOTE_INSN_EH_REGION_BEG,
3380 first_no_action_insn);
3381 NOTE_EH_HANDLER (note) = call_site;
3382 first_no_action_insn = NULL_RTX;
3385 note = emit_note_after (NOTE_INSN_EH_REGION_END,
3386 last_action_insn);
3387 NOTE_EH_HANDLER (note) = call_site;
3390 /* If the new action is must-not-throw, then no region notes
3391 are created. */
3392 if (this_action >= -1)
3394 call_site = add_call_site (this_landing_pad,
3395 this_action < 0 ? 0 : this_action);
3396 note = emit_note_before (NOTE_INSN_EH_REGION_BEG, iter);
3397 NOTE_EH_HANDLER (note) = call_site;
3400 last_action = this_action;
3401 last_landing_pad = this_landing_pad;
3403 last_action_insn = iter;
3406 if (last_action >= -1 && ! first_no_action_insn)
3408 note = emit_note_after (NOTE_INSN_EH_REGION_END, last_action_insn);
3409 NOTE_EH_HANDLER (note) = call_site;
3412 htab_delete (ar_hash);
3416 static void
3417 push_uleb128 (varray_type *data_area, unsigned int value)
3421 unsigned char byte = value & 0x7f;
3422 value >>= 7;
3423 if (value)
3424 byte |= 0x80;
3425 VARRAY_PUSH_UCHAR (*data_area, byte);
3427 while (value);
3430 static void
3431 push_sleb128 (varray_type *data_area, int value)
3433 unsigned char byte;
3434 int more;
3438 byte = value & 0x7f;
3439 value >>= 7;
3440 more = ! ((value == 0 && (byte & 0x40) == 0)
3441 || (value == -1 && (byte & 0x40) != 0));
3442 if (more)
3443 byte |= 0x80;
3444 VARRAY_PUSH_UCHAR (*data_area, byte);
3446 while (more);
3450 #ifndef HAVE_AS_LEB128
3451 static int
3452 dw2_size_of_call_site_table (void)
3454 int n = cfun->eh->call_site_data_used;
3455 int size = n * (4 + 4 + 4);
3456 int i;
3458 for (i = 0; i < n; ++i)
3460 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3461 size += size_of_uleb128 (cs->action);
3464 return size;
3467 static int
3468 sjlj_size_of_call_site_table (void)
3470 int n = cfun->eh->call_site_data_used;
3471 int size = 0;
3472 int i;
3474 for (i = 0; i < n; ++i)
3476 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3477 size += size_of_uleb128 (INTVAL (cs->landing_pad));
3478 size += size_of_uleb128 (cs->action);
3481 return size;
3483 #endif
3485 static void
3486 dw2_output_call_site_table (void)
3488 const char *const function_start_lab
3489 = IDENTIFIER_POINTER (current_function_func_begin_label);
3490 int n = cfun->eh->call_site_data_used;
3491 int i;
3493 for (i = 0; i < n; ++i)
3495 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3496 char reg_start_lab[32];
3497 char reg_end_lab[32];
3498 char landing_pad_lab[32];
3500 ASM_GENERATE_INTERNAL_LABEL (reg_start_lab, "LEHB", call_site_base + i);
3501 ASM_GENERATE_INTERNAL_LABEL (reg_end_lab, "LEHE", call_site_base + i);
3503 if (cs->landing_pad)
3504 ASM_GENERATE_INTERNAL_LABEL (landing_pad_lab, "L",
3505 CODE_LABEL_NUMBER (cs->landing_pad));
3507 /* ??? Perhaps use insn length scaling if the assembler supports
3508 generic arithmetic. */
3509 /* ??? Perhaps use attr_length to choose data1 or data2 instead of
3510 data4 if the function is small enough. */
3511 #ifdef HAVE_AS_LEB128
3512 dw2_asm_output_delta_uleb128 (reg_start_lab, function_start_lab,
3513 "region %d start", i);
3514 dw2_asm_output_delta_uleb128 (reg_end_lab, reg_start_lab,
3515 "length");
3516 if (cs->landing_pad)
3517 dw2_asm_output_delta_uleb128 (landing_pad_lab, function_start_lab,
3518 "landing pad");
3519 else
3520 dw2_asm_output_data_uleb128 (0, "landing pad");
3521 #else
3522 dw2_asm_output_delta (4, reg_start_lab, function_start_lab,
3523 "region %d start", i);
3524 dw2_asm_output_delta (4, reg_end_lab, reg_start_lab, "length");
3525 if (cs->landing_pad)
3526 dw2_asm_output_delta (4, landing_pad_lab, function_start_lab,
3527 "landing pad");
3528 else
3529 dw2_asm_output_data (4, 0, "landing pad");
3530 #endif
3531 dw2_asm_output_data_uleb128 (cs->action, "action");
3534 call_site_base += n;
3537 static void
3538 sjlj_output_call_site_table (void)
3540 int n = cfun->eh->call_site_data_used;
3541 int i;
3543 for (i = 0; i < n; ++i)
3545 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3547 dw2_asm_output_data_uleb128 (INTVAL (cs->landing_pad),
3548 "region %d landing pad", i);
3549 dw2_asm_output_data_uleb128 (cs->action, "action");
3552 call_site_base += n;
3555 /* Tell assembler to switch to the section for the exception handling
3556 table. */
3558 void
3559 default_exception_section (void)
3561 if (targetm.have_named_sections)
3563 int flags;
3564 #ifdef HAVE_LD_RO_RW_SECTION_MIXING
3565 int tt_format = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3567 flags = (! flag_pic
3568 || ((tt_format & 0x70) != DW_EH_PE_absptr
3569 && (tt_format & 0x70) != DW_EH_PE_aligned))
3570 ? 0 : SECTION_WRITE;
3571 #else
3572 flags = SECTION_WRITE;
3573 #endif
3574 named_section_flags (".gcc_except_table", flags);
3576 else if (flag_pic)
3577 data_section ();
3578 else
3579 readonly_data_section ();
3582 void
3583 output_function_exception_table (void)
3585 int tt_format, cs_format, lp_format, i, n;
3586 #ifdef HAVE_AS_LEB128
3587 char ttype_label[32];
3588 char cs_after_size_label[32];
3589 char cs_end_label[32];
3590 #else
3591 int call_site_len;
3592 #endif
3593 int have_tt_data;
3594 int tt_format_size = 0;
3596 /* Not all functions need anything. */
3597 if (! cfun->uses_eh_lsda)
3598 return;
3600 #ifdef TARGET_UNWIND_INFO
3601 /* TODO: Move this into target file. */
3602 fputs ("\t.personality\t", asm_out_file);
3603 output_addr_const (asm_out_file, eh_personality_libfunc);
3604 fputs ("\n\t.handlerdata\n", asm_out_file);
3605 /* Note that varasm still thinks we're in the function's code section.
3606 The ".endp" directive that will immediately follow will take us back. */
3607 #else
3608 targetm.asm_out.exception_section ();
3609 #endif
3611 have_tt_data = (VARRAY_ACTIVE_SIZE (cfun->eh->ttype_data) > 0
3612 || VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data) > 0);
3614 /* Indicate the format of the @TType entries. */
3615 if (! have_tt_data)
3616 tt_format = DW_EH_PE_omit;
3617 else
3619 tt_format = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3620 #ifdef HAVE_AS_LEB128
3621 ASM_GENERATE_INTERNAL_LABEL (ttype_label, "LLSDATT",
3622 current_function_funcdef_no);
3623 #endif
3624 tt_format_size = size_of_encoded_value (tt_format);
3626 assemble_align (tt_format_size * BITS_PER_UNIT);
3629 targetm.asm_out.internal_label (asm_out_file, "LLSDA",
3630 current_function_funcdef_no);
3632 /* The LSDA header. */
3634 /* Indicate the format of the landing pad start pointer. An omitted
3635 field implies @LPStart == @Start. */
3636 /* Currently we always put @LPStart == @Start. This field would
3637 be most useful in moving the landing pads completely out of
3638 line to another section, but it could also be used to minimize
3639 the size of uleb128 landing pad offsets. */
3640 lp_format = DW_EH_PE_omit;
3641 dw2_asm_output_data (1, lp_format, "@LPStart format (%s)",
3642 eh_data_format_name (lp_format));
3644 /* @LPStart pointer would go here. */
3646 dw2_asm_output_data (1, tt_format, "@TType format (%s)",
3647 eh_data_format_name (tt_format));
3649 #ifndef HAVE_AS_LEB128
3650 if (USING_SJLJ_EXCEPTIONS)
3651 call_site_len = sjlj_size_of_call_site_table ();
3652 else
3653 call_site_len = dw2_size_of_call_site_table ();
3654 #endif
3656 /* A pc-relative 4-byte displacement to the @TType data. */
3657 if (have_tt_data)
3659 #ifdef HAVE_AS_LEB128
3660 char ttype_after_disp_label[32];
3661 ASM_GENERATE_INTERNAL_LABEL (ttype_after_disp_label, "LLSDATTD",
3662 current_function_funcdef_no);
3663 dw2_asm_output_delta_uleb128 (ttype_label, ttype_after_disp_label,
3664 "@TType base offset");
3665 ASM_OUTPUT_LABEL (asm_out_file, ttype_after_disp_label);
3666 #else
3667 /* Ug. Alignment queers things. */
3668 unsigned int before_disp, after_disp, last_disp, disp;
3670 before_disp = 1 + 1;
3671 after_disp = (1 + size_of_uleb128 (call_site_len)
3672 + call_site_len
3673 + VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data)
3674 + (VARRAY_ACTIVE_SIZE (cfun->eh->ttype_data)
3675 * tt_format_size));
3677 disp = after_disp;
3680 unsigned int disp_size, pad;
3682 last_disp = disp;
3683 disp_size = size_of_uleb128 (disp);
3684 pad = before_disp + disp_size + after_disp;
3685 if (pad % tt_format_size)
3686 pad = tt_format_size - (pad % tt_format_size);
3687 else
3688 pad = 0;
3689 disp = after_disp + pad;
3691 while (disp != last_disp);
3693 dw2_asm_output_data_uleb128 (disp, "@TType base offset");
3694 #endif
3697 /* Indicate the format of the call-site offsets. */
3698 #ifdef HAVE_AS_LEB128
3699 cs_format = DW_EH_PE_uleb128;
3700 #else
3701 cs_format = DW_EH_PE_udata4;
3702 #endif
3703 dw2_asm_output_data (1, cs_format, "call-site format (%s)",
3704 eh_data_format_name (cs_format));
3706 #ifdef HAVE_AS_LEB128
3707 ASM_GENERATE_INTERNAL_LABEL (cs_after_size_label, "LLSDACSB",
3708 current_function_funcdef_no);
3709 ASM_GENERATE_INTERNAL_LABEL (cs_end_label, "LLSDACSE",
3710 current_function_funcdef_no);
3711 dw2_asm_output_delta_uleb128 (cs_end_label, cs_after_size_label,
3712 "Call-site table length");
3713 ASM_OUTPUT_LABEL (asm_out_file, cs_after_size_label);
3714 if (USING_SJLJ_EXCEPTIONS)
3715 sjlj_output_call_site_table ();
3716 else
3717 dw2_output_call_site_table ();
3718 ASM_OUTPUT_LABEL (asm_out_file, cs_end_label);
3719 #else
3720 dw2_asm_output_data_uleb128 (call_site_len,"Call-site table length");
3721 if (USING_SJLJ_EXCEPTIONS)
3722 sjlj_output_call_site_table ();
3723 else
3724 dw2_output_call_site_table ();
3725 #endif
3727 /* ??? Decode and interpret the data for flag_debug_asm. */
3728 n = VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data);
3729 for (i = 0; i < n; ++i)
3730 dw2_asm_output_data (1, VARRAY_UCHAR (cfun->eh->action_record_data, i),
3731 (i ? NULL : "Action record table"));
3733 if (have_tt_data)
3734 assemble_align (tt_format_size * BITS_PER_UNIT);
3736 i = VARRAY_ACTIVE_SIZE (cfun->eh->ttype_data);
3737 while (i-- > 0)
3739 tree type = VARRAY_TREE (cfun->eh->ttype_data, i);
3740 rtx value;
3742 if (type == NULL_TREE)
3743 value = const0_rtx;
3744 else
3746 struct cgraph_varpool_node *node;
3748 type = lookup_type_for_runtime (type);
3749 value = expand_expr (type, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
3751 /* Let cgraph know that the rtti decl is used. Not all of the
3752 paths below go through assemble_integer, which would take
3753 care of this for us. */
3754 STRIP_NOPS (type);
3755 if (TREE_CODE (type) == ADDR_EXPR)
3757 type = TREE_OPERAND (type, 0);
3758 if (TREE_CODE (type) == VAR_DECL)
3760 node = cgraph_varpool_node (type);
3761 if (node)
3762 cgraph_varpool_mark_needed_node (node);
3765 else if (TREE_CODE (type) != INTEGER_CST)
3766 abort ();
3769 if (tt_format == DW_EH_PE_absptr || tt_format == DW_EH_PE_aligned)
3770 assemble_integer (value, tt_format_size,
3771 tt_format_size * BITS_PER_UNIT, 1);
3772 else
3773 dw2_asm_output_encoded_addr_rtx (tt_format, value, NULL);
3776 #ifdef HAVE_AS_LEB128
3777 if (have_tt_data)
3778 ASM_OUTPUT_LABEL (asm_out_file, ttype_label);
3779 #endif
3781 /* ??? Decode and interpret the data for flag_debug_asm. */
3782 n = VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data);
3783 for (i = 0; i < n; ++i)
3784 dw2_asm_output_data (1, VARRAY_UCHAR (cfun->eh->ehspec_data, i),
3785 (i ? NULL : "Exception specification table"));
3787 function_section (current_function_decl);
3790 #include "gt-except.h"