* config/xtensa/crti.asm (_init, _fini): Increase frame size to 64.
[official-gcc.git] / gcc / except.c
blobc34dcd9b5214dc1ecc2cb652363d0a817c01c4e2
1 /* Implements exception handling.
2 Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003 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"
77 /* Provide defaults for stuff that may not be defined when using
78 sjlj exceptions. */
79 #ifndef EH_RETURN_DATA_REGNO
80 #define EH_RETURN_DATA_REGNO(N) INVALID_REGNUM
81 #endif
84 /* Nonzero means enable synchronous exceptions for non-call instructions. */
85 int flag_non_call_exceptions;
87 /* Protect cleanup actions with must-not-throw regions, with a call
88 to the given failure handler. */
89 tree (*lang_protect_cleanup_actions) (void);
91 /* Return true if type A catches type B. */
92 int (*lang_eh_type_covers) (tree a, tree b);
94 /* Map a type to a runtime object to match type. */
95 tree (*lang_eh_runtime_type) (tree);
97 /* A hash table of label to region number. */
99 struct ehl_map_entry GTY(())
101 rtx label;
102 struct eh_region *region;
105 static GTY(()) int call_site_base;
106 static GTY ((param_is (union tree_node)))
107 htab_t type_to_runtime_map;
109 /* Describe the SjLj_Function_Context structure. */
110 static GTY(()) tree sjlj_fc_type_node;
111 static int sjlj_fc_call_site_ofs;
112 static int sjlj_fc_data_ofs;
113 static int sjlj_fc_personality_ofs;
114 static int sjlj_fc_lsda_ofs;
115 static int sjlj_fc_jbuf_ofs;
117 /* Describes one exception region. */
118 struct eh_region GTY(())
120 /* The immediately surrounding region. */
121 struct eh_region *outer;
123 /* The list of immediately contained regions. */
124 struct eh_region *inner;
125 struct eh_region *next_peer;
127 /* An identifier for this region. */
128 int region_number;
130 /* When a region is deleted, its parents inherit the REG_EH_REGION
131 numbers already assigned. */
132 bitmap aka;
134 /* Each region does exactly one thing. */
135 enum eh_region_type
137 ERT_UNKNOWN = 0,
138 ERT_CLEANUP,
139 ERT_TRY,
140 ERT_CATCH,
141 ERT_ALLOWED_EXCEPTIONS,
142 ERT_MUST_NOT_THROW,
143 ERT_THROW,
144 ERT_FIXUP
145 } type;
147 /* Holds the action to perform based on the preceding type. */
148 union eh_region_u {
149 /* A list of catch blocks, a surrounding try block,
150 and the label for continuing after a catch. */
151 struct eh_region_u_try {
152 struct eh_region *catch;
153 struct eh_region *last_catch;
154 struct eh_region *prev_try;
155 rtx continue_label;
156 } GTY ((tag ("ERT_TRY"))) try;
158 /* The list through the catch handlers, the list of type objects
159 matched, and the list of associated filters. */
160 struct eh_region_u_catch {
161 struct eh_region *next_catch;
162 struct eh_region *prev_catch;
163 tree type_list;
164 tree filter_list;
165 } GTY ((tag ("ERT_CATCH"))) catch;
167 /* A tree_list of allowed types. */
168 struct eh_region_u_allowed {
169 tree type_list;
170 int filter;
171 } GTY ((tag ("ERT_ALLOWED_EXCEPTIONS"))) allowed;
173 /* The type given by a call to "throw foo();", or discovered
174 for a throw. */
175 struct eh_region_u_throw {
176 tree type;
177 } GTY ((tag ("ERT_THROW"))) throw;
179 /* Retain the cleanup expression even after expansion so that
180 we can match up fixup regions. */
181 struct eh_region_u_cleanup {
182 tree exp;
183 struct eh_region *prev_try;
184 } GTY ((tag ("ERT_CLEANUP"))) cleanup;
186 /* The real region (by expression and by pointer) that fixup code
187 should live in. */
188 struct eh_region_u_fixup {
189 tree cleanup_exp;
190 struct eh_region *real_region;
191 } GTY ((tag ("ERT_FIXUP"))) fixup;
192 } GTY ((desc ("%0.type"))) u;
194 /* Entry point for this region's handler before landing pads are built. */
195 rtx 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 struct eh_region *expand_eh_region_end (void);
265 static rtx get_exception_filter (struct function *);
267 static void collect_eh_region_array (void);
268 static void resolve_fixup_regions (void);
269 static void remove_fixup_regions (void);
270 static void remove_unreachable_regions (rtx);
271 static void convert_from_eh_region_ranges_1 (rtx *, int *, int);
273 static struct eh_region *duplicate_eh_region_1 (struct eh_region *,
274 struct inline_remap *);
275 static void duplicate_eh_region_2 (struct eh_region *, struct eh_region **);
276 static int ttypes_filter_eq (const void *, const void *);
277 static hashval_t ttypes_filter_hash (const void *);
278 static int ehspec_filter_eq (const void *, const void *);
279 static hashval_t ehspec_filter_hash (const void *);
280 static int add_ttypes_entry (htab_t, tree);
281 static int add_ehspec_entry (htab_t, htab_t, tree);
282 static void assign_filter_values (void);
283 static void build_post_landing_pads (void);
284 static void connect_post_landing_pads (void);
285 static void dw2_build_landing_pads (void);
287 struct sjlj_lp_info;
288 static bool sjlj_find_directly_reachable_regions (struct sjlj_lp_info *);
289 static void sjlj_assign_call_site_values (rtx, struct sjlj_lp_info *);
290 static void sjlj_mark_call_sites (struct sjlj_lp_info *);
291 static void sjlj_emit_function_enter (rtx);
292 static void sjlj_emit_function_exit (void);
293 static void sjlj_emit_dispatch_table (rtx, struct sjlj_lp_info *);
294 static void sjlj_build_landing_pads (void);
296 static hashval_t ehl_hash (const void *);
297 static int ehl_eq (const void *, const void *);
298 static void add_ehl_entry (rtx, struct eh_region *);
299 static void remove_exception_handler_label (rtx);
300 static void remove_eh_handler (struct eh_region *);
301 static int for_each_eh_label_1 (void **, void *);
303 struct reachable_info;
305 /* The return value of reachable_next_level. */
306 enum reachable_code
308 /* The given exception is not processed by the given region. */
309 RNL_NOT_CAUGHT,
310 /* The given exception may need processing by the given region. */
311 RNL_MAYBE_CAUGHT,
312 /* The given exception is completely processed by the given region. */
313 RNL_CAUGHT,
314 /* The given exception is completely processed by the runtime. */
315 RNL_BLOCKED
318 static int check_handled (tree, tree);
319 static void add_reachable_handler (struct reachable_info *,
320 struct eh_region *, struct eh_region *);
321 static enum reachable_code reachable_next_level (struct eh_region *, tree,
322 struct reachable_info *);
324 static int action_record_eq (const void *, const void *);
325 static hashval_t action_record_hash (const void *);
326 static int add_action_record (htab_t, int, int);
327 static int collect_one_action_chain (htab_t, struct eh_region *);
328 static int add_call_site (rtx, int);
330 static void push_uleb128 (varray_type *, unsigned int);
331 static void push_sleb128 (varray_type *, int);
332 #ifndef HAVE_AS_LEB128
333 static int dw2_size_of_call_site_table (void);
334 static int sjlj_size_of_call_site_table (void);
335 #endif
336 static void dw2_output_call_site_table (void);
337 static void sjlj_output_call_site_table (void);
340 /* Routine to see if exception handling is turned on.
341 DO_WARN is nonzero if we want to inform the user that exception
342 handling is turned off.
344 This is used to ensure that -fexceptions has been specified if the
345 compiler tries to use any exception-specific functions. */
348 doing_eh (int do_warn)
350 if (! flag_exceptions)
352 static int warned = 0;
353 if (! warned && do_warn)
355 error ("exception handling disabled, use -fexceptions to enable");
356 warned = 1;
358 return 0;
360 return 1;
364 void
365 init_eh (void)
367 if (! flag_exceptions)
368 return;
370 type_to_runtime_map = htab_create_ggc (31, t2r_hash, t2r_eq, NULL);
372 /* Create the SjLj_Function_Context structure. This should match
373 the definition in unwind-sjlj.c. */
374 if (USING_SJLJ_EXCEPTIONS)
376 tree f_jbuf, f_per, f_lsda, f_prev, f_cs, f_data, tmp;
378 sjlj_fc_type_node = (*lang_hooks.types.make_type) (RECORD_TYPE);
380 f_prev = build_decl (FIELD_DECL, get_identifier ("__prev"),
381 build_pointer_type (sjlj_fc_type_node));
382 DECL_FIELD_CONTEXT (f_prev) = sjlj_fc_type_node;
384 f_cs = build_decl (FIELD_DECL, get_identifier ("__call_site"),
385 integer_type_node);
386 DECL_FIELD_CONTEXT (f_cs) = sjlj_fc_type_node;
388 tmp = build_index_type (build_int_2 (4 - 1, 0));
389 tmp = build_array_type ((*lang_hooks.types.type_for_mode) (word_mode, 1),
390 tmp);
391 f_data = build_decl (FIELD_DECL, get_identifier ("__data"), tmp);
392 DECL_FIELD_CONTEXT (f_data) = sjlj_fc_type_node;
394 f_per = build_decl (FIELD_DECL, get_identifier ("__personality"),
395 ptr_type_node);
396 DECL_FIELD_CONTEXT (f_per) = sjlj_fc_type_node;
398 f_lsda = build_decl (FIELD_DECL, get_identifier ("__lsda"),
399 ptr_type_node);
400 DECL_FIELD_CONTEXT (f_lsda) = sjlj_fc_type_node;
402 #ifdef DONT_USE_BUILTIN_SETJMP
403 #ifdef JMP_BUF_SIZE
404 tmp = build_int_2 (JMP_BUF_SIZE - 1, 0);
405 #else
406 /* Should be large enough for most systems, if it is not,
407 JMP_BUF_SIZE should be defined with the proper value. It will
408 also tend to be larger than necessary for most systems, a more
409 optimal port will define JMP_BUF_SIZE. */
410 tmp = build_int_2 (FIRST_PSEUDO_REGISTER + 2 - 1, 0);
411 #endif
412 #else
413 /* This is 2 for builtin_setjmp, plus whatever the target requires
414 via STACK_SAVEAREA_MODE (SAVE_NONLOCAL). */
415 tmp = build_int_2 ((GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL))
416 / GET_MODE_SIZE (Pmode)) + 2 - 1, 0);
417 #endif
418 tmp = build_index_type (tmp);
419 tmp = build_array_type (ptr_type_node, tmp);
420 f_jbuf = build_decl (FIELD_DECL, get_identifier ("__jbuf"), tmp);
421 #ifdef DONT_USE_BUILTIN_SETJMP
422 /* We don't know what the alignment requirements of the
423 runtime's jmp_buf has. Overestimate. */
424 DECL_ALIGN (f_jbuf) = BIGGEST_ALIGNMENT;
425 DECL_USER_ALIGN (f_jbuf) = 1;
426 #endif
427 DECL_FIELD_CONTEXT (f_jbuf) = sjlj_fc_type_node;
429 TYPE_FIELDS (sjlj_fc_type_node) = f_prev;
430 TREE_CHAIN (f_prev) = f_cs;
431 TREE_CHAIN (f_cs) = f_data;
432 TREE_CHAIN (f_data) = f_per;
433 TREE_CHAIN (f_per) = f_lsda;
434 TREE_CHAIN (f_lsda) = f_jbuf;
436 layout_type (sjlj_fc_type_node);
438 /* Cache the interesting field offsets so that we have
439 easy access from rtl. */
440 sjlj_fc_call_site_ofs
441 = (tree_low_cst (DECL_FIELD_OFFSET (f_cs), 1)
442 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_cs), 1) / BITS_PER_UNIT);
443 sjlj_fc_data_ofs
444 = (tree_low_cst (DECL_FIELD_OFFSET (f_data), 1)
445 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_data), 1) / BITS_PER_UNIT);
446 sjlj_fc_personality_ofs
447 = (tree_low_cst (DECL_FIELD_OFFSET (f_per), 1)
448 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_per), 1) / BITS_PER_UNIT);
449 sjlj_fc_lsda_ofs
450 = (tree_low_cst (DECL_FIELD_OFFSET (f_lsda), 1)
451 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_lsda), 1) / BITS_PER_UNIT);
452 sjlj_fc_jbuf_ofs
453 = (tree_low_cst (DECL_FIELD_OFFSET (f_jbuf), 1)
454 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_jbuf), 1) / BITS_PER_UNIT);
458 void
459 init_eh_for_function (void)
461 cfun->eh = ggc_alloc_cleared (sizeof (struct eh_status));
464 /* Start an exception handling region. All instructions emitted
465 after this point are considered to be part of the region until
466 expand_eh_region_end is invoked. */
468 void
469 expand_eh_region_start (void)
471 struct eh_region *new_region;
472 struct eh_region *cur_region;
473 rtx note;
475 if (! doing_eh (0))
476 return;
478 /* Insert a new blank region as a leaf in the tree. */
479 new_region = ggc_alloc_cleared (sizeof (*new_region));
480 cur_region = cfun->eh->cur_region;
481 new_region->outer = cur_region;
482 if (cur_region)
484 new_region->next_peer = cur_region->inner;
485 cur_region->inner = new_region;
487 else
489 new_region->next_peer = cfun->eh->region_tree;
490 cfun->eh->region_tree = new_region;
492 cfun->eh->cur_region = new_region;
494 /* Create a note marking the start of this region. */
495 new_region->region_number = ++cfun->eh->last_region_number;
496 note = emit_note (NOTE_INSN_EH_REGION_BEG);
497 NOTE_EH_HANDLER (note) = new_region->region_number;
500 /* Common code to end a region. Returns the region just ended. */
502 static struct eh_region *
503 expand_eh_region_end (void)
505 struct eh_region *cur_region = cfun->eh->cur_region;
506 rtx note;
508 /* Create a note marking the end of this region. */
509 note = emit_note (NOTE_INSN_EH_REGION_END);
510 NOTE_EH_HANDLER (note) = cur_region->region_number;
512 /* Pop. */
513 cfun->eh->cur_region = cur_region->outer;
515 return cur_region;
518 /* End an exception handling region for a cleanup. HANDLER is an
519 expression to expand for the cleanup. */
521 void
522 expand_eh_region_end_cleanup (tree handler)
524 struct eh_region *region;
525 tree protect_cleanup_actions;
526 rtx around_label;
527 rtx data_save[2];
529 if (! doing_eh (0))
530 return;
532 region = expand_eh_region_end ();
533 region->type = ERT_CLEANUP;
534 region->label = gen_label_rtx ();
535 region->u.cleanup.exp = handler;
536 region->u.cleanup.prev_try = cfun->eh->try_region;
538 around_label = gen_label_rtx ();
539 emit_jump (around_label);
541 emit_label (region->label);
543 if (flag_non_call_exceptions || region->may_contain_throw)
545 /* Give the language a chance to specify an action to be taken if an
546 exception is thrown that would propagate out of the HANDLER. */
547 protect_cleanup_actions
548 = (lang_protect_cleanup_actions
549 ? (*lang_protect_cleanup_actions) ()
550 : NULL_TREE);
552 if (protect_cleanup_actions)
553 expand_eh_region_start ();
555 /* In case this cleanup involves an inline destructor with a try block in
556 it, we need to save the EH return data registers around it. */
557 data_save[0] = gen_reg_rtx (ptr_mode);
558 emit_move_insn (data_save[0], get_exception_pointer (cfun));
559 data_save[1] = gen_reg_rtx (word_mode);
560 emit_move_insn (data_save[1], get_exception_filter (cfun));
562 expand_expr (handler, const0_rtx, VOIDmode, 0);
564 emit_move_insn (cfun->eh->exc_ptr, data_save[0]);
565 emit_move_insn (cfun->eh->filter, data_save[1]);
567 if (protect_cleanup_actions)
568 expand_eh_region_end_must_not_throw (protect_cleanup_actions);
570 /* We need any stack adjustment complete before the around_label. */
571 do_pending_stack_adjust ();
574 /* We delay the generation of the _Unwind_Resume until we generate
575 landing pads. We emit a marker here so as to get good control
576 flow data in the meantime. */
577 region->resume
578 = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
579 emit_barrier ();
581 emit_label (around_label);
584 /* End an exception handling region for a try block, and prepares
585 for subsequent calls to expand_start_catch. */
587 void
588 expand_start_all_catch (void)
590 struct eh_region *region;
592 if (! doing_eh (1))
593 return;
595 region = expand_eh_region_end ();
596 region->type = ERT_TRY;
597 region->u.try.prev_try = cfun->eh->try_region;
598 region->u.try.continue_label = gen_label_rtx ();
600 cfun->eh->try_region = region;
602 emit_jump (region->u.try.continue_label);
605 /* Begin a catch clause. TYPE is the type caught, a list of such types, or
606 null if this is a catch-all clause. Providing a type list enables to
607 associate the catch region with potentially several exception types, which
608 is useful e.g. for Ada. */
610 void
611 expand_start_catch (tree type_or_list)
613 struct eh_region *t, *c, *l;
614 tree type_list;
616 if (! doing_eh (0))
617 return;
619 type_list = type_or_list;
621 if (type_or_list)
623 /* Ensure to always end up with a type list to normalize further
624 processing, then register each type against the runtime types
625 map. */
626 tree type_node;
628 if (TREE_CODE (type_or_list) != TREE_LIST)
629 type_list = tree_cons (NULL_TREE, type_or_list, NULL_TREE);
631 type_node = type_list;
632 for (; type_node; type_node = TREE_CHAIN (type_node))
633 add_type_for_runtime (TREE_VALUE (type_node));
636 expand_eh_region_start ();
638 t = cfun->eh->try_region;
639 c = cfun->eh->cur_region;
640 c->type = ERT_CATCH;
641 c->u.catch.type_list = type_list;
642 c->label = gen_label_rtx ();
644 l = t->u.try.last_catch;
645 c->u.catch.prev_catch = l;
646 if (l)
647 l->u.catch.next_catch = c;
648 else
649 t->u.try.catch = c;
650 t->u.try.last_catch = c;
652 emit_label (c->label);
655 /* End a catch clause. Control will resume after the try/catch block. */
657 void
658 expand_end_catch (void)
660 struct eh_region *try_region;
662 if (! doing_eh (0))
663 return;
665 expand_eh_region_end ();
666 try_region = cfun->eh->try_region;
668 emit_jump (try_region->u.try.continue_label);
671 /* End a sequence of catch handlers for a try block. */
673 void
674 expand_end_all_catch (void)
676 struct eh_region *try_region;
678 if (! doing_eh (0))
679 return;
681 try_region = cfun->eh->try_region;
682 cfun->eh->try_region = try_region->u.try.prev_try;
684 emit_label (try_region->u.try.continue_label);
687 /* End an exception region for an exception type filter. ALLOWED is a
688 TREE_LIST of types to be matched by the runtime. FAILURE is an
689 expression to invoke if a mismatch occurs.
691 ??? We could use these semantics for calls to rethrow, too; if we can
692 see the surrounding catch clause, we know that the exception we're
693 rethrowing satisfies the "filter" of the catch type. */
695 void
696 expand_eh_region_end_allowed (tree allowed, tree failure)
698 struct eh_region *region;
699 rtx around_label;
701 if (! doing_eh (0))
702 return;
704 region = expand_eh_region_end ();
705 region->type = ERT_ALLOWED_EXCEPTIONS;
706 region->u.allowed.type_list = allowed;
707 region->label = gen_label_rtx ();
709 for (; allowed ; allowed = TREE_CHAIN (allowed))
710 add_type_for_runtime (TREE_VALUE (allowed));
712 /* We must emit the call to FAILURE here, so that if this function
713 throws a different exception, that it will be processed by the
714 correct region. */
716 around_label = gen_label_rtx ();
717 emit_jump (around_label);
719 emit_label (region->label);
720 expand_expr (failure, const0_rtx, VOIDmode, EXPAND_NORMAL);
721 /* We must adjust the stack before we reach the AROUND_LABEL because
722 the call to FAILURE does not occur on all paths to the
723 AROUND_LABEL. */
724 do_pending_stack_adjust ();
726 emit_label (around_label);
729 /* End an exception region for a must-not-throw filter. FAILURE is an
730 expression invoke if an uncaught exception propagates this far.
732 This is conceptually identical to expand_eh_region_end_allowed with
733 an empty allowed list (if you passed "std::terminate" instead of
734 "__cxa_call_unexpected"), but they are represented differently in
735 the C++ LSDA. */
737 void
738 expand_eh_region_end_must_not_throw (tree failure)
740 struct eh_region *region;
741 rtx around_label;
743 if (! doing_eh (0))
744 return;
746 region = expand_eh_region_end ();
747 region->type = ERT_MUST_NOT_THROW;
748 region->label = gen_label_rtx ();
750 /* We must emit the call to FAILURE here, so that if this function
751 throws a different exception, that it will be processed by the
752 correct region. */
754 around_label = gen_label_rtx ();
755 emit_jump (around_label);
757 emit_label (region->label);
758 expand_expr (failure, const0_rtx, VOIDmode, EXPAND_NORMAL);
760 emit_label (around_label);
763 /* End an exception region for a throw. No handling goes on here,
764 but it's the easiest way for the front-end to indicate what type
765 is being thrown. */
767 void
768 expand_eh_region_end_throw (tree type)
770 struct eh_region *region;
772 if (! doing_eh (0))
773 return;
775 region = expand_eh_region_end ();
776 region->type = ERT_THROW;
777 region->u.throw.type = type;
780 /* End a fixup region. Within this region the cleanups for the immediately
781 enclosing region are _not_ run. This is used for goto cleanup to avoid
782 destroying an object twice.
784 This would be an extraordinarily simple prospect, were it not for the
785 fact that we don't actually know what the immediately enclosing region
786 is. This surprising fact is because expand_cleanups is currently
787 generating a sequence that it will insert somewhere else. We collect
788 the proper notion of "enclosing" in convert_from_eh_region_ranges. */
790 void
791 expand_eh_region_end_fixup (tree handler)
793 struct eh_region *fixup;
795 if (! doing_eh (0))
796 return;
798 fixup = expand_eh_region_end ();
799 fixup->type = ERT_FIXUP;
800 fixup->u.fixup.cleanup_exp = handler;
803 /* Note that the current EH region (if any) may contain a throw, or a
804 call to a function which itself may contain a throw. */
806 void
807 note_eh_region_may_contain_throw (void)
809 struct eh_region *region;
811 region = cfun->eh->cur_region;
812 while (region && !region->may_contain_throw)
814 region->may_contain_throw = 1;
815 region = region->outer;
819 /* Return an rtl expression for a pointer to the exception object
820 within a handler. */
823 get_exception_pointer (struct function *fun)
825 rtx exc_ptr = fun->eh->exc_ptr;
826 if (fun == cfun && ! exc_ptr)
828 exc_ptr = gen_reg_rtx (ptr_mode);
829 fun->eh->exc_ptr = exc_ptr;
831 return exc_ptr;
834 /* Return an rtl expression for the exception dispatch filter
835 within a handler. */
837 static rtx
838 get_exception_filter (struct function *fun)
840 rtx filter = fun->eh->filter;
841 if (fun == cfun && ! filter)
843 filter = gen_reg_rtx (word_mode);
844 fun->eh->filter = filter;
846 return filter;
849 /* This section is for the exception handling specific optimization pass. */
851 /* Random access the exception region tree. It's just as simple to
852 collect the regions this way as in expand_eh_region_start, but
853 without having to realloc memory. */
855 static void
856 collect_eh_region_array (void)
858 struct eh_region **array, *i;
860 i = cfun->eh->region_tree;
861 if (! i)
862 return;
864 array = ggc_alloc_cleared ((cfun->eh->last_region_number + 1)
865 * sizeof (*array));
866 cfun->eh->region_array = array;
868 while (1)
870 array[i->region_number] = i;
872 /* If there are sub-regions, process them. */
873 if (i->inner)
874 i = i->inner;
875 /* If there are peers, process them. */
876 else if (i->next_peer)
877 i = i->next_peer;
878 /* Otherwise, step back up the tree to the next peer. */
879 else
881 do {
882 i = i->outer;
883 if (i == NULL)
884 return;
885 } while (i->next_peer == NULL);
886 i = i->next_peer;
891 static void
892 resolve_fixup_regions (void)
894 int i, j, n = cfun->eh->last_region_number;
896 for (i = 1; i <= n; ++i)
898 struct eh_region *fixup = cfun->eh->region_array[i];
899 struct eh_region *cleanup = 0;
901 if (! fixup || fixup->type != ERT_FIXUP)
902 continue;
904 for (j = 1; j <= n; ++j)
906 cleanup = cfun->eh->region_array[j];
907 if (cleanup && cleanup->type == ERT_CLEANUP
908 && cleanup->u.cleanup.exp == fixup->u.fixup.cleanup_exp)
909 break;
911 if (j > n)
912 abort ();
914 fixup->u.fixup.real_region = cleanup->outer;
918 /* Now that we've discovered what region actually encloses a fixup,
919 we can shuffle pointers and remove them from the tree. */
921 static void
922 remove_fixup_regions (void)
924 int i;
925 rtx insn, note;
926 struct eh_region *fixup;
928 /* Walk the insn chain and adjust the REG_EH_REGION numbers
929 for instructions referencing fixup regions. This is only
930 strictly necessary for fixup regions with no parent, but
931 doesn't hurt to do it for all regions. */
932 for (insn = get_insns(); insn ; insn = NEXT_INSN (insn))
933 if (INSN_P (insn)
934 && (note = find_reg_note (insn, REG_EH_REGION, NULL))
935 && INTVAL (XEXP (note, 0)) > 0
936 && (fixup = cfun->eh->region_array[INTVAL (XEXP (note, 0))])
937 && fixup->type == ERT_FIXUP)
939 if (fixup->u.fixup.real_region)
940 XEXP (note, 0) = GEN_INT (fixup->u.fixup.real_region->region_number);
941 else
942 remove_note (insn, note);
945 /* Remove the fixup regions from the tree. */
946 for (i = cfun->eh->last_region_number; i > 0; --i)
948 fixup = cfun->eh->region_array[i];
949 if (! fixup)
950 continue;
952 /* Allow GC to maybe free some memory. */
953 if (fixup->type == ERT_CLEANUP)
954 fixup->u.cleanup.exp = NULL_TREE;
956 if (fixup->type != ERT_FIXUP)
957 continue;
959 if (fixup->inner)
961 struct eh_region *parent, *p, **pp;
963 parent = fixup->u.fixup.real_region;
965 /* Fix up the children's parent pointers; find the end of
966 the list. */
967 for (p = fixup->inner; ; p = p->next_peer)
969 p->outer = parent;
970 if (! p->next_peer)
971 break;
974 /* In the tree of cleanups, only outer-inner ordering matters.
975 So link the children back in anywhere at the correct level. */
976 if (parent)
977 pp = &parent->inner;
978 else
979 pp = &cfun->eh->region_tree;
980 p->next_peer = *pp;
981 *pp = fixup->inner;
982 fixup->inner = NULL;
985 remove_eh_handler (fixup);
989 /* Remove all regions whose labels are not reachable from insns. */
991 static void
992 remove_unreachable_regions (rtx insns)
994 int i, *uid_region_num;
995 bool *reachable;
996 struct eh_region *r;
997 rtx insn;
999 uid_region_num = xcalloc (get_max_uid (), sizeof(int));
1000 reachable = xcalloc (cfun->eh->last_region_number + 1, sizeof(bool));
1002 for (i = cfun->eh->last_region_number; i > 0; --i)
1004 r = cfun->eh->region_array[i];
1005 if (!r || r->region_number != i)
1006 continue;
1008 if (r->resume)
1010 if (uid_region_num[INSN_UID (r->resume)])
1011 abort ();
1012 uid_region_num[INSN_UID (r->resume)] = i;
1014 if (r->label)
1016 if (uid_region_num[INSN_UID (r->label)])
1017 abort ();
1018 uid_region_num[INSN_UID (r->label)] = i;
1020 if (r->type == ERT_TRY && r->u.try.continue_label)
1022 if (uid_region_num[INSN_UID (r->u.try.continue_label)])
1023 abort ();
1024 uid_region_num[INSN_UID (r->u.try.continue_label)] = i;
1028 for (insn = insns; insn; insn = NEXT_INSN (insn))
1029 reachable[uid_region_num[INSN_UID (insn)]] = true;
1031 for (i = cfun->eh->last_region_number; i > 0; --i)
1033 r = cfun->eh->region_array[i];
1034 if (r && r->region_number == i && !reachable[i])
1036 /* Don't remove ERT_THROW regions if their outer region
1037 is reachable. */
1038 if (r->type == ERT_THROW
1039 && r->outer
1040 && reachable[r->outer->region_number])
1041 continue;
1043 remove_eh_handler (r);
1047 free (reachable);
1048 free (uid_region_num);
1051 /* Turn NOTE_INSN_EH_REGION notes into REG_EH_REGION notes for each
1052 can_throw instruction in the region. */
1054 static void
1055 convert_from_eh_region_ranges_1 (rtx *pinsns, int *orig_sp, int cur)
1057 int *sp = orig_sp;
1058 rtx insn, next;
1060 for (insn = *pinsns; insn ; insn = next)
1062 next = NEXT_INSN (insn);
1063 if (GET_CODE (insn) == NOTE)
1065 int kind = NOTE_LINE_NUMBER (insn);
1066 if (kind == NOTE_INSN_EH_REGION_BEG
1067 || kind == NOTE_INSN_EH_REGION_END)
1069 if (kind == NOTE_INSN_EH_REGION_BEG)
1071 struct eh_region *r;
1073 *sp++ = cur;
1074 cur = NOTE_EH_HANDLER (insn);
1076 r = cfun->eh->region_array[cur];
1077 if (r->type == ERT_FIXUP)
1079 r = r->u.fixup.real_region;
1080 cur = r ? r->region_number : 0;
1082 else if (r->type == ERT_CATCH)
1084 r = r->outer;
1085 cur = r ? r->region_number : 0;
1088 else
1089 cur = *--sp;
1091 /* Removing the first insn of a CALL_PLACEHOLDER sequence
1092 requires extra care to adjust sequence start. */
1093 if (insn == *pinsns)
1094 *pinsns = next;
1095 remove_insn (insn);
1096 continue;
1099 else if (INSN_P (insn))
1101 if (cur > 0
1102 && ! find_reg_note (insn, REG_EH_REGION, NULL_RTX)
1103 /* Calls can always potentially throw exceptions, unless
1104 they have a REG_EH_REGION note with a value of 0 or less.
1105 Which should be the only possible kind so far. */
1106 && (GET_CODE (insn) == CALL_INSN
1107 /* If we wanted exceptions for non-call insns, then
1108 any may_trap_p instruction could throw. */
1109 || (flag_non_call_exceptions
1110 && GET_CODE (PATTERN (insn)) != CLOBBER
1111 && GET_CODE (PATTERN (insn)) != USE
1112 && may_trap_p (PATTERN (insn)))))
1114 REG_NOTES (insn) = alloc_EXPR_LIST (REG_EH_REGION, GEN_INT (cur),
1115 REG_NOTES (insn));
1118 if (GET_CODE (insn) == CALL_INSN
1119 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
1121 convert_from_eh_region_ranges_1 (&XEXP (PATTERN (insn), 0),
1122 sp, cur);
1123 convert_from_eh_region_ranges_1 (&XEXP (PATTERN (insn), 1),
1124 sp, cur);
1125 convert_from_eh_region_ranges_1 (&XEXP (PATTERN (insn), 2),
1126 sp, cur);
1131 if (sp != orig_sp)
1132 abort ();
1135 void
1136 convert_from_eh_region_ranges (void)
1138 int *stack;
1139 rtx insns;
1141 collect_eh_region_array ();
1142 resolve_fixup_regions ();
1144 stack = xmalloc (sizeof (int) * (cfun->eh->last_region_number + 1));
1145 insns = get_insns ();
1146 convert_from_eh_region_ranges_1 (&insns, stack, 0);
1147 free (stack);
1149 remove_fixup_regions ();
1150 remove_unreachable_regions (insns);
1153 static void
1154 add_ehl_entry (rtx label, struct eh_region *region)
1156 struct ehl_map_entry **slot, *entry;
1158 LABEL_PRESERVE_P (label) = 1;
1160 entry = ggc_alloc (sizeof (*entry));
1161 entry->label = label;
1162 entry->region = region;
1164 slot = (struct ehl_map_entry **)
1165 htab_find_slot (cfun->eh->exception_handler_label_map, entry, INSERT);
1167 /* Before landing pad creation, each exception handler has its own
1168 label. After landing pad creation, the exception handlers may
1169 share landing pads. This is ok, since maybe_remove_eh_handler
1170 only requires the 1-1 mapping before landing pad creation. */
1171 if (*slot && !cfun->eh->built_landing_pads)
1172 abort ();
1174 *slot = entry;
1177 void
1178 find_exception_handler_labels (void)
1180 int i;
1182 if (cfun->eh->exception_handler_label_map)
1183 htab_empty (cfun->eh->exception_handler_label_map);
1184 else
1186 /* ??? The expansion factor here (3/2) must be greater than the htab
1187 occupancy factor (4/3) to avoid unnecessary resizing. */
1188 cfun->eh->exception_handler_label_map
1189 = htab_create_ggc (cfun->eh->last_region_number * 3 / 2,
1190 ehl_hash, ehl_eq, NULL);
1193 if (cfun->eh->region_tree == NULL)
1194 return;
1196 for (i = cfun->eh->last_region_number; i > 0; --i)
1198 struct eh_region *region = cfun->eh->region_array[i];
1199 rtx lab;
1201 if (! region || region->region_number != i)
1202 continue;
1203 if (cfun->eh->built_landing_pads)
1204 lab = region->landing_pad;
1205 else
1206 lab = region->label;
1208 if (lab)
1209 add_ehl_entry (lab, region);
1212 /* For sjlj exceptions, need the return label to remain live until
1213 after landing pad generation. */
1214 if (USING_SJLJ_EXCEPTIONS && ! cfun->eh->built_landing_pads)
1215 add_ehl_entry (return_label, NULL);
1218 bool
1219 current_function_has_exception_handlers (void)
1221 int i;
1223 for (i = cfun->eh->last_region_number; i > 0; --i)
1225 struct eh_region *region = cfun->eh->region_array[i];
1227 if (! region || region->region_number != i)
1228 continue;
1229 if (region->type != ERT_THROW)
1230 return true;
1233 return false;
1236 static struct eh_region *
1237 duplicate_eh_region_1 (struct eh_region *o, struct inline_remap *map)
1239 struct eh_region *n = ggc_alloc_cleared (sizeof (struct eh_region));
1241 n->region_number = o->region_number + cfun->eh->last_region_number;
1242 n->type = o->type;
1244 switch (n->type)
1246 case ERT_CLEANUP:
1247 case ERT_MUST_NOT_THROW:
1248 break;
1250 case ERT_TRY:
1251 if (o->u.try.continue_label)
1252 n->u.try.continue_label
1253 = get_label_from_map (map,
1254 CODE_LABEL_NUMBER (o->u.try.continue_label));
1255 break;
1257 case ERT_CATCH:
1258 n->u.catch.type_list = o->u.catch.type_list;
1259 break;
1261 case ERT_ALLOWED_EXCEPTIONS:
1262 n->u.allowed.type_list = o->u.allowed.type_list;
1263 break;
1265 case ERT_THROW:
1266 n->u.throw.type = o->u.throw.type;
1268 default:
1269 abort ();
1272 if (o->label)
1273 n->label = get_label_from_map (map, CODE_LABEL_NUMBER (o->label));
1274 if (o->resume)
1276 n->resume = map->insn_map[INSN_UID (o->resume)];
1277 if (n->resume == NULL)
1278 abort ();
1281 return n;
1284 static void
1285 duplicate_eh_region_2 (struct eh_region *o, struct eh_region **n_array)
1287 struct eh_region *n = n_array[o->region_number];
1289 switch (n->type)
1291 case ERT_TRY:
1292 n->u.try.catch = n_array[o->u.try.catch->region_number];
1293 n->u.try.last_catch = n_array[o->u.try.last_catch->region_number];
1294 break;
1296 case ERT_CATCH:
1297 if (o->u.catch.next_catch)
1298 n->u.catch.next_catch = n_array[o->u.catch.next_catch->region_number];
1299 if (o->u.catch.prev_catch)
1300 n->u.catch.prev_catch = n_array[o->u.catch.prev_catch->region_number];
1301 break;
1303 default:
1304 break;
1307 if (o->outer)
1308 n->outer = n_array[o->outer->region_number];
1309 if (o->inner)
1310 n->inner = n_array[o->inner->region_number];
1311 if (o->next_peer)
1312 n->next_peer = n_array[o->next_peer->region_number];
1316 duplicate_eh_regions (struct function *ifun, struct inline_remap *map)
1318 int ifun_last_region_number = ifun->eh->last_region_number;
1319 struct eh_region **n_array, *root, *cur;
1320 int i;
1322 if (ifun_last_region_number == 0)
1323 return 0;
1325 n_array = xcalloc (ifun_last_region_number + 1, sizeof (*n_array));
1327 for (i = 1; i <= ifun_last_region_number; ++i)
1329 cur = ifun->eh->region_array[i];
1330 if (!cur || cur->region_number != i)
1331 continue;
1332 n_array[i] = duplicate_eh_region_1 (cur, map);
1334 for (i = 1; i <= ifun_last_region_number; ++i)
1336 cur = ifun->eh->region_array[i];
1337 if (!cur || cur->region_number != i)
1338 continue;
1339 duplicate_eh_region_2 (cur, n_array);
1342 root = n_array[ifun->eh->region_tree->region_number];
1343 cur = cfun->eh->cur_region;
1344 if (cur)
1346 struct eh_region *p = cur->inner;
1347 if (p)
1349 while (p->next_peer)
1350 p = p->next_peer;
1351 p->next_peer = root;
1353 else
1354 cur->inner = root;
1356 for (i = 1; i <= ifun_last_region_number; ++i)
1357 if (n_array[i] && n_array[i]->outer == NULL)
1358 n_array[i]->outer = cur;
1360 else
1362 struct eh_region *p = cfun->eh->region_tree;
1363 if (p)
1365 while (p->next_peer)
1366 p = p->next_peer;
1367 p->next_peer = root;
1369 else
1370 cfun->eh->region_tree = root;
1373 free (n_array);
1375 i = cfun->eh->last_region_number;
1376 cfun->eh->last_region_number = i + ifun_last_region_number;
1377 return i;
1381 static int
1382 t2r_eq (const void *pentry, const void *pdata)
1384 tree entry = (tree) pentry;
1385 tree data = (tree) pdata;
1387 return TREE_PURPOSE (entry) == data;
1390 static hashval_t
1391 t2r_hash (const void *pentry)
1393 tree entry = (tree) pentry;
1394 return TYPE_HASH (TREE_PURPOSE (entry));
1397 static void
1398 add_type_for_runtime (tree type)
1400 tree *slot;
1402 slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
1403 TYPE_HASH (type), INSERT);
1404 if (*slot == NULL)
1406 tree runtime = (*lang_eh_runtime_type) (type);
1407 *slot = tree_cons (type, runtime, NULL_TREE);
1411 static tree
1412 lookup_type_for_runtime (tree type)
1414 tree *slot;
1416 slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
1417 TYPE_HASH (type), NO_INSERT);
1419 /* We should have always inserted the data earlier. */
1420 return TREE_VALUE (*slot);
1424 /* Represent an entry in @TTypes for either catch actions
1425 or exception filter actions. */
1426 struct ttypes_filter GTY(())
1428 tree t;
1429 int filter;
1432 /* Compare ENTRY (a ttypes_filter entry in the hash table) with DATA
1433 (a tree) for a @TTypes type node we are thinking about adding. */
1435 static int
1436 ttypes_filter_eq (const void *pentry, const void *pdata)
1438 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1439 tree data = (tree) pdata;
1441 return entry->t == data;
1444 static hashval_t
1445 ttypes_filter_hash (const void *pentry)
1447 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1448 return TYPE_HASH (entry->t);
1451 /* Compare ENTRY with DATA (both struct ttypes_filter) for a @TTypes
1452 exception specification list we are thinking about adding. */
1453 /* ??? Currently we use the type lists in the order given. Someone
1454 should put these in some canonical order. */
1456 static int
1457 ehspec_filter_eq (const void *pentry, const void *pdata)
1459 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1460 const struct ttypes_filter *data = (const struct ttypes_filter *) pdata;
1462 return type_list_equal (entry->t, data->t);
1465 /* Hash function for exception specification lists. */
1467 static hashval_t
1468 ehspec_filter_hash (const void *pentry)
1470 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1471 hashval_t h = 0;
1472 tree list;
1474 for (list = entry->t; list ; list = TREE_CHAIN (list))
1475 h = (h << 5) + (h >> 27) + TYPE_HASH (TREE_VALUE (list));
1476 return h;
1479 /* Add TYPE to cfun->eh->ttype_data, using TYPES_HASH to speed
1480 up the search. Return the filter value to be used. */
1482 static int
1483 add_ttypes_entry (htab_t ttypes_hash, tree type)
1485 struct ttypes_filter **slot, *n;
1487 slot = (struct ttypes_filter **)
1488 htab_find_slot_with_hash (ttypes_hash, type, TYPE_HASH (type), INSERT);
1490 if ((n = *slot) == NULL)
1492 /* Filter value is a 1 based table index. */
1494 n = xmalloc (sizeof (*n));
1495 n->t = type;
1496 n->filter = VARRAY_ACTIVE_SIZE (cfun->eh->ttype_data) + 1;
1497 *slot = n;
1499 VARRAY_PUSH_TREE (cfun->eh->ttype_data, type);
1502 return n->filter;
1505 /* Add LIST to cfun->eh->ehspec_data, using EHSPEC_HASH and TYPES_HASH
1506 to speed up the search. Return the filter value to be used. */
1508 static int
1509 add_ehspec_entry (htab_t ehspec_hash, htab_t ttypes_hash, tree list)
1511 struct ttypes_filter **slot, *n;
1512 struct ttypes_filter dummy;
1514 dummy.t = list;
1515 slot = (struct ttypes_filter **)
1516 htab_find_slot (ehspec_hash, &dummy, INSERT);
1518 if ((n = *slot) == NULL)
1520 /* Filter value is a -1 based byte index into a uleb128 buffer. */
1522 n = xmalloc (sizeof (*n));
1523 n->t = list;
1524 n->filter = -(VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data) + 1);
1525 *slot = n;
1527 /* Look up each type in the list and encode its filter
1528 value as a uleb128. Terminate the list with 0. */
1529 for (; list ; list = TREE_CHAIN (list))
1530 push_uleb128 (&cfun->eh->ehspec_data,
1531 add_ttypes_entry (ttypes_hash, TREE_VALUE (list)));
1532 VARRAY_PUSH_UCHAR (cfun->eh->ehspec_data, 0);
1535 return n->filter;
1538 /* Generate the action filter values to be used for CATCH and
1539 ALLOWED_EXCEPTIONS regions. When using dwarf2 exception regions,
1540 we use lots of landing pads, and so every type or list can share
1541 the same filter value, which saves table space. */
1543 static void
1544 assign_filter_values (void)
1546 int i;
1547 htab_t ttypes, ehspec;
1549 VARRAY_TREE_INIT (cfun->eh->ttype_data, 16, "ttype_data");
1550 VARRAY_UCHAR_INIT (cfun->eh->ehspec_data, 64, "ehspec_data");
1552 ttypes = htab_create (31, ttypes_filter_hash, ttypes_filter_eq, free);
1553 ehspec = htab_create (31, ehspec_filter_hash, ehspec_filter_eq, free);
1555 for (i = cfun->eh->last_region_number; i > 0; --i)
1557 struct eh_region *r = cfun->eh->region_array[i];
1559 /* Mind we don't process a region more than once. */
1560 if (!r || r->region_number != i)
1561 continue;
1563 switch (r->type)
1565 case ERT_CATCH:
1566 /* Whatever type_list is (NULL or true list), we build a list
1567 of filters for the region. */
1568 r->u.catch.filter_list = NULL_TREE;
1570 if (r->u.catch.type_list != NULL)
1572 /* Get a filter value for each of the types caught and store
1573 them in the region's dedicated list. */
1574 tree tp_node = r->u.catch.type_list;
1576 for (;tp_node; tp_node = TREE_CHAIN (tp_node))
1578 int flt = add_ttypes_entry (ttypes, TREE_VALUE (tp_node));
1579 tree flt_node = build_int_2 (flt, 0);
1581 r->u.catch.filter_list
1582 = tree_cons (NULL_TREE, flt_node, r->u.catch.filter_list);
1585 else
1587 /* Get a filter value for the NULL list also since it will need
1588 an action record anyway. */
1589 int flt = add_ttypes_entry (ttypes, NULL);
1590 tree flt_node = build_int_2 (flt, 0);
1592 r->u.catch.filter_list
1593 = tree_cons (NULL_TREE, flt_node, r->u.catch.filter_list);
1596 break;
1598 case ERT_ALLOWED_EXCEPTIONS:
1599 r->u.allowed.filter
1600 = add_ehspec_entry (ehspec, ttypes, r->u.allowed.type_list);
1601 break;
1603 default:
1604 break;
1608 htab_delete (ttypes);
1609 htab_delete (ehspec);
1612 /* Generate the code to actually handle exceptions, which will follow the
1613 landing pads. */
1615 static void
1616 build_post_landing_pads (void)
1618 int i;
1620 for (i = cfun->eh->last_region_number; i > 0; --i)
1622 struct eh_region *region = cfun->eh->region_array[i];
1623 rtx seq;
1625 /* Mind we don't process a region more than once. */
1626 if (!region || region->region_number != i)
1627 continue;
1629 switch (region->type)
1631 case ERT_TRY:
1632 /* ??? Collect the set of all non-overlapping catch handlers
1633 all the way up the chain until blocked by a cleanup. */
1634 /* ??? Outer try regions can share landing pads with inner
1635 try regions if the types are completely non-overlapping,
1636 and there are no intervening cleanups. */
1638 region->post_landing_pad = gen_label_rtx ();
1640 start_sequence ();
1642 emit_label (region->post_landing_pad);
1644 /* ??? It is mighty inconvenient to call back into the
1645 switch statement generation code in expand_end_case.
1646 Rapid prototyping sez a sequence of ifs. */
1648 struct eh_region *c;
1649 for (c = region->u.try.catch; c ; c = c->u.catch.next_catch)
1651 if (c->u.catch.type_list == NULL)
1652 emit_jump (c->label);
1653 else
1655 /* Need for one cmp/jump per type caught. Each type
1656 list entry has a matching entry in the filter list
1657 (see assign_filter_values). */
1658 tree tp_node = c->u.catch.type_list;
1659 tree flt_node = c->u.catch.filter_list;
1661 for (; tp_node; )
1663 emit_cmp_and_jump_insns
1664 (cfun->eh->filter,
1665 GEN_INT (tree_low_cst (TREE_VALUE (flt_node), 0)),
1666 EQ, NULL_RTX, word_mode, 0, c->label);
1668 tp_node = TREE_CHAIN (tp_node);
1669 flt_node = TREE_CHAIN (flt_node);
1675 /* We delay the generation of the _Unwind_Resume until we generate
1676 landing pads. We emit a marker here so as to get good control
1677 flow data in the meantime. */
1678 region->resume
1679 = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
1680 emit_barrier ();
1682 seq = get_insns ();
1683 end_sequence ();
1685 emit_insn_before (seq, region->u.try.catch->label);
1686 break;
1688 case ERT_ALLOWED_EXCEPTIONS:
1689 region->post_landing_pad = gen_label_rtx ();
1691 start_sequence ();
1693 emit_label (region->post_landing_pad);
1695 emit_cmp_and_jump_insns (cfun->eh->filter,
1696 GEN_INT (region->u.allowed.filter),
1697 EQ, NULL_RTX, word_mode, 0, region->label);
1699 /* We delay the generation of the _Unwind_Resume until we generate
1700 landing pads. We emit a marker here so as to get good control
1701 flow data in the meantime. */
1702 region->resume
1703 = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
1704 emit_barrier ();
1706 seq = get_insns ();
1707 end_sequence ();
1709 emit_insn_before (seq, region->label);
1710 break;
1712 case ERT_CLEANUP:
1713 case ERT_MUST_NOT_THROW:
1714 region->post_landing_pad = region->label;
1715 break;
1717 case ERT_CATCH:
1718 case ERT_THROW:
1719 /* Nothing to do. */
1720 break;
1722 default:
1723 abort ();
1728 /* Replace RESX patterns with jumps to the next handler if any, or calls to
1729 _Unwind_Resume otherwise. */
1731 static void
1732 connect_post_landing_pads (void)
1734 int i;
1736 for (i = cfun->eh->last_region_number; i > 0; --i)
1738 struct eh_region *region = cfun->eh->region_array[i];
1739 struct eh_region *outer;
1740 rtx seq;
1742 /* Mind we don't process a region more than once. */
1743 if (!region || region->region_number != i)
1744 continue;
1746 /* If there is no RESX, or it has been deleted by flow, there's
1747 nothing to fix up. */
1748 if (! region->resume || INSN_DELETED_P (region->resume))
1749 continue;
1751 /* Search for another landing pad in this function. */
1752 for (outer = region->outer; outer ; outer = outer->outer)
1753 if (outer->post_landing_pad)
1754 break;
1756 start_sequence ();
1758 if (outer)
1759 emit_jump (outer->post_landing_pad);
1760 else
1761 emit_library_call (unwind_resume_libfunc, LCT_THROW,
1762 VOIDmode, 1, cfun->eh->exc_ptr, ptr_mode);
1764 seq = get_insns ();
1765 end_sequence ();
1766 emit_insn_before (seq, region->resume);
1767 delete_insn (region->resume);
1772 static void
1773 dw2_build_landing_pads (void)
1775 int i;
1776 unsigned int j;
1778 for (i = cfun->eh->last_region_number; i > 0; --i)
1780 struct eh_region *region = cfun->eh->region_array[i];
1781 rtx seq;
1782 bool clobbers_hard_regs = false;
1784 /* Mind we don't process a region more than once. */
1785 if (!region || region->region_number != i)
1786 continue;
1788 if (region->type != ERT_CLEANUP
1789 && region->type != ERT_TRY
1790 && region->type != ERT_ALLOWED_EXCEPTIONS)
1791 continue;
1793 start_sequence ();
1795 region->landing_pad = gen_label_rtx ();
1796 emit_label (region->landing_pad);
1798 #ifdef HAVE_exception_receiver
1799 if (HAVE_exception_receiver)
1800 emit_insn (gen_exception_receiver ());
1801 else
1802 #endif
1803 #ifdef HAVE_nonlocal_goto_receiver
1804 if (HAVE_nonlocal_goto_receiver)
1805 emit_insn (gen_nonlocal_goto_receiver ());
1806 else
1807 #endif
1808 { /* Nothing */ }
1810 /* If the eh_return data registers are call-saved, then we
1811 won't have considered them clobbered from the call that
1812 threw. Kill them now. */
1813 for (j = 0; ; ++j)
1815 unsigned r = EH_RETURN_DATA_REGNO (j);
1816 if (r == INVALID_REGNUM)
1817 break;
1818 if (! call_used_regs[r])
1820 emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, r)));
1821 clobbers_hard_regs = true;
1825 if (clobbers_hard_regs)
1827 /* @@@ This is a kludge. Not all machine descriptions define a
1828 blockage insn, but we must not allow the code we just generated
1829 to be reordered by scheduling. So emit an ASM_INPUT to act as
1830 blockage insn. */
1831 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
1834 emit_move_insn (cfun->eh->exc_ptr,
1835 gen_rtx_REG (ptr_mode, EH_RETURN_DATA_REGNO (0)));
1836 emit_move_insn (cfun->eh->filter,
1837 gen_rtx_REG (word_mode, EH_RETURN_DATA_REGNO (1)));
1839 seq = get_insns ();
1840 end_sequence ();
1842 emit_insn_before (seq, region->post_landing_pad);
1847 struct sjlj_lp_info
1849 int directly_reachable;
1850 int action_index;
1851 int dispatch_index;
1852 int call_site_index;
1855 static bool
1856 sjlj_find_directly_reachable_regions (struct sjlj_lp_info *lp_info)
1858 rtx insn;
1859 bool found_one = false;
1861 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
1863 struct eh_region *region;
1864 enum reachable_code rc;
1865 tree type_thrown;
1866 rtx note;
1868 if (! INSN_P (insn))
1869 continue;
1871 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1872 if (!note || INTVAL (XEXP (note, 0)) <= 0)
1873 continue;
1875 region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
1877 type_thrown = NULL_TREE;
1878 if (region->type == ERT_THROW)
1880 type_thrown = region->u.throw.type;
1881 region = region->outer;
1884 /* Find the first containing region that might handle the exception.
1885 That's the landing pad to which we will transfer control. */
1886 rc = RNL_NOT_CAUGHT;
1887 for (; region; region = region->outer)
1889 rc = reachable_next_level (region, type_thrown, 0);
1890 if (rc != RNL_NOT_CAUGHT)
1891 break;
1893 if (rc == RNL_MAYBE_CAUGHT || rc == RNL_CAUGHT)
1895 lp_info[region->region_number].directly_reachable = 1;
1896 found_one = true;
1900 return found_one;
1903 static void
1904 sjlj_assign_call_site_values (rtx dispatch_label, struct sjlj_lp_info *lp_info)
1906 htab_t ar_hash;
1907 int i, index;
1909 /* First task: build the action table. */
1911 VARRAY_UCHAR_INIT (cfun->eh->action_record_data, 64, "action_record_data");
1912 ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
1914 for (i = cfun->eh->last_region_number; i > 0; --i)
1915 if (lp_info[i].directly_reachable)
1917 struct eh_region *r = cfun->eh->region_array[i];
1918 r->landing_pad = dispatch_label;
1919 lp_info[i].action_index = collect_one_action_chain (ar_hash, r);
1920 if (lp_info[i].action_index != -1)
1921 cfun->uses_eh_lsda = 1;
1924 htab_delete (ar_hash);
1926 /* Next: assign dispatch values. In dwarf2 terms, this would be the
1927 landing pad label for the region. For sjlj though, there is one
1928 common landing pad from which we dispatch to the post-landing pads.
1930 A region receives a dispatch index if it is directly reachable
1931 and requires in-function processing. Regions that share post-landing
1932 pads may share dispatch indices. */
1933 /* ??? Post-landing pad sharing doesn't actually happen at the moment
1934 (see build_post_landing_pads) so we don't bother checking for it. */
1936 index = 0;
1937 for (i = cfun->eh->last_region_number; i > 0; --i)
1938 if (lp_info[i].directly_reachable)
1939 lp_info[i].dispatch_index = index++;
1941 /* Finally: assign call-site values. If dwarf2 terms, this would be
1942 the region number assigned by convert_to_eh_region_ranges, but
1943 handles no-action and must-not-throw differently. */
1945 call_site_base = 1;
1946 for (i = cfun->eh->last_region_number; i > 0; --i)
1947 if (lp_info[i].directly_reachable)
1949 int action = lp_info[i].action_index;
1951 /* Map must-not-throw to otherwise unused call-site index 0. */
1952 if (action == -2)
1953 index = 0;
1954 /* Map no-action to otherwise unused call-site index -1. */
1955 else if (action == -1)
1956 index = -1;
1957 /* Otherwise, look it up in the table. */
1958 else
1959 index = add_call_site (GEN_INT (lp_info[i].dispatch_index), action);
1961 lp_info[i].call_site_index = index;
1965 static void
1966 sjlj_mark_call_sites (struct sjlj_lp_info *lp_info)
1968 int last_call_site = -2;
1969 rtx insn, mem;
1971 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
1973 struct eh_region *region;
1974 int this_call_site;
1975 rtx note, before, p;
1977 /* Reset value tracking at extended basic block boundaries. */
1978 if (GET_CODE (insn) == CODE_LABEL)
1979 last_call_site = -2;
1981 if (! INSN_P (insn))
1982 continue;
1984 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1985 if (!note)
1987 /* Calls (and trapping insns) without notes are outside any
1988 exception handling region in this function. Mark them as
1989 no action. */
1990 if (GET_CODE (insn) == CALL_INSN
1991 || (flag_non_call_exceptions
1992 && may_trap_p (PATTERN (insn))))
1993 this_call_site = -1;
1994 else
1995 continue;
1997 else
1999 /* Calls that are known to not throw need not be marked. */
2000 if (INTVAL (XEXP (note, 0)) <= 0)
2001 continue;
2003 region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
2004 this_call_site = lp_info[region->region_number].call_site_index;
2007 if (this_call_site == last_call_site)
2008 continue;
2010 /* Don't separate a call from it's argument loads. */
2011 before = insn;
2012 if (GET_CODE (insn) == CALL_INSN)
2013 before = find_first_parameter_load (insn, NULL_RTX);
2015 start_sequence ();
2016 mem = adjust_address (cfun->eh->sjlj_fc, TYPE_MODE (integer_type_node),
2017 sjlj_fc_call_site_ofs);
2018 emit_move_insn (mem, GEN_INT (this_call_site));
2019 p = get_insns ();
2020 end_sequence ();
2022 emit_insn_before (p, before);
2023 last_call_site = this_call_site;
2027 /* Construct the SjLj_Function_Context. */
2029 static void
2030 sjlj_emit_function_enter (rtx dispatch_label)
2032 rtx fn_begin, fc, mem, seq;
2034 fc = cfun->eh->sjlj_fc;
2036 start_sequence ();
2038 /* We're storing this libcall's address into memory instead of
2039 calling it directly. Thus, we must call assemble_external_libcall
2040 here, as we can not depend on emit_library_call to do it for us. */
2041 assemble_external_libcall (eh_personality_libfunc);
2042 mem = adjust_address (fc, Pmode, sjlj_fc_personality_ofs);
2043 emit_move_insn (mem, eh_personality_libfunc);
2045 mem = adjust_address (fc, Pmode, sjlj_fc_lsda_ofs);
2046 if (cfun->uses_eh_lsda)
2048 char buf[20];
2049 ASM_GENERATE_INTERNAL_LABEL (buf, "LLSDA", current_function_funcdef_no);
2050 emit_move_insn (mem, gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf)));
2052 else
2053 emit_move_insn (mem, const0_rtx);
2055 #ifdef DONT_USE_BUILTIN_SETJMP
2057 rtx x, note;
2058 x = emit_library_call_value (setjmp_libfunc, NULL_RTX, LCT_RETURNS_TWICE,
2059 TYPE_MODE (integer_type_node), 1,
2060 plus_constant (XEXP (fc, 0),
2061 sjlj_fc_jbuf_ofs), Pmode);
2063 note = emit_note (NOTE_INSN_EXPECTED_VALUE);
2064 NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, x, const0_rtx);
2066 emit_cmp_and_jump_insns (x, const0_rtx, NE, 0,
2067 TYPE_MODE (integer_type_node), 0, dispatch_label);
2069 #else
2070 expand_builtin_setjmp_setup (plus_constant (XEXP (fc, 0), sjlj_fc_jbuf_ofs),
2071 dispatch_label);
2072 #endif
2074 emit_library_call (unwind_sjlj_register_libfunc, LCT_NORMAL, VOIDmode,
2075 1, XEXP (fc, 0), Pmode);
2077 seq = get_insns ();
2078 end_sequence ();
2080 /* ??? Instead of doing this at the beginning of the function,
2081 do this in a block that is at loop level 0 and dominates all
2082 can_throw_internal instructions. */
2084 for (fn_begin = get_insns (); ; fn_begin = NEXT_INSN (fn_begin))
2085 if (GET_CODE (fn_begin) == NOTE
2086 && NOTE_LINE_NUMBER (fn_begin) == NOTE_INSN_FUNCTION_BEG)
2087 break;
2088 emit_insn_after (seq, fn_begin);
2091 /* Call back from expand_function_end to know where we should put
2092 the call to unwind_sjlj_unregister_libfunc if needed. */
2094 void
2095 sjlj_emit_function_exit_after (rtx after)
2097 cfun->eh->sjlj_exit_after = after;
2100 static void
2101 sjlj_emit_function_exit (void)
2103 rtx seq;
2105 start_sequence ();
2107 emit_library_call (unwind_sjlj_unregister_libfunc, LCT_NORMAL, VOIDmode,
2108 1, XEXP (cfun->eh->sjlj_fc, 0), Pmode);
2110 seq = get_insns ();
2111 end_sequence ();
2113 /* ??? Really this can be done in any block at loop level 0 that
2114 post-dominates all can_throw_internal instructions. This is
2115 the last possible moment. */
2117 emit_insn_after (seq, cfun->eh->sjlj_exit_after);
2120 static void
2121 sjlj_emit_dispatch_table (rtx dispatch_label, struct sjlj_lp_info *lp_info)
2123 int i, first_reachable;
2124 rtx mem, dispatch, seq, fc;
2126 fc = cfun->eh->sjlj_fc;
2128 start_sequence ();
2130 emit_label (dispatch_label);
2132 #ifndef DONT_USE_BUILTIN_SETJMP
2133 expand_builtin_setjmp_receiver (dispatch_label);
2134 #endif
2136 /* Load up dispatch index, exc_ptr and filter values from the
2137 function context. */
2138 mem = adjust_address (fc, TYPE_MODE (integer_type_node),
2139 sjlj_fc_call_site_ofs);
2140 dispatch = copy_to_reg (mem);
2142 mem = adjust_address (fc, word_mode, sjlj_fc_data_ofs);
2143 if (word_mode != Pmode)
2145 #ifdef POINTERS_EXTEND_UNSIGNED
2146 mem = convert_memory_address (Pmode, mem);
2147 #else
2148 mem = convert_to_mode (Pmode, mem, 0);
2149 #endif
2151 emit_move_insn (cfun->eh->exc_ptr, mem);
2153 mem = adjust_address (fc, word_mode, sjlj_fc_data_ofs + UNITS_PER_WORD);
2154 emit_move_insn (cfun->eh->filter, mem);
2156 /* Jump to one of the directly reachable regions. */
2157 /* ??? This really ought to be using a switch statement. */
2159 first_reachable = 0;
2160 for (i = cfun->eh->last_region_number; i > 0; --i)
2162 if (! lp_info[i].directly_reachable)
2163 continue;
2165 if (! first_reachable)
2167 first_reachable = i;
2168 continue;
2171 emit_cmp_and_jump_insns (dispatch, GEN_INT (lp_info[i].dispatch_index),
2172 EQ, NULL_RTX, TYPE_MODE (integer_type_node), 0,
2173 cfun->eh->region_array[i]->post_landing_pad);
2176 seq = get_insns ();
2177 end_sequence ();
2179 emit_insn_before (seq, (cfun->eh->region_array[first_reachable]
2180 ->post_landing_pad));
2183 static void
2184 sjlj_build_landing_pads (void)
2186 struct sjlj_lp_info *lp_info;
2188 lp_info = xcalloc (cfun->eh->last_region_number + 1,
2189 sizeof (struct sjlj_lp_info));
2191 if (sjlj_find_directly_reachable_regions (lp_info))
2193 rtx dispatch_label = gen_label_rtx ();
2195 cfun->eh->sjlj_fc
2196 = assign_stack_local (TYPE_MODE (sjlj_fc_type_node),
2197 int_size_in_bytes (sjlj_fc_type_node),
2198 TYPE_ALIGN (sjlj_fc_type_node));
2200 sjlj_assign_call_site_values (dispatch_label, lp_info);
2201 sjlj_mark_call_sites (lp_info);
2203 sjlj_emit_function_enter (dispatch_label);
2204 sjlj_emit_dispatch_table (dispatch_label, lp_info);
2205 sjlj_emit_function_exit ();
2208 free (lp_info);
2211 void
2212 finish_eh_generation (void)
2214 /* Nothing to do if no regions created. */
2215 if (cfun->eh->region_tree == NULL)
2216 return;
2218 /* The object here is to provide find_basic_blocks with detailed
2219 information (via reachable_handlers) on how exception control
2220 flows within the function. In this first pass, we can include
2221 type information garnered from ERT_THROW and ERT_ALLOWED_EXCEPTIONS
2222 regions, and hope that it will be useful in deleting unreachable
2223 handlers. Subsequently, we will generate landing pads which will
2224 connect many of the handlers, and then type information will not
2225 be effective. Still, this is a win over previous implementations. */
2227 cleanup_cfg (CLEANUP_PRE_LOOP | CLEANUP_NO_INSN_DEL);
2229 /* These registers are used by the landing pads. Make sure they
2230 have been generated. */
2231 get_exception_pointer (cfun);
2232 get_exception_filter (cfun);
2234 /* Construct the landing pads. */
2236 assign_filter_values ();
2237 build_post_landing_pads ();
2238 connect_post_landing_pads ();
2239 if (USING_SJLJ_EXCEPTIONS)
2240 sjlj_build_landing_pads ();
2241 else
2242 dw2_build_landing_pads ();
2244 cfun->eh->built_landing_pads = 1;
2246 /* We've totally changed the CFG. Start over. */
2247 find_exception_handler_labels ();
2248 rebuild_jump_labels (get_insns ());
2249 find_basic_blocks (get_insns (), max_reg_num (), 0);
2250 cleanup_cfg (CLEANUP_PRE_LOOP | CLEANUP_NO_INSN_DEL);
2253 static hashval_t
2254 ehl_hash (const void *pentry)
2256 struct ehl_map_entry *entry = (struct ehl_map_entry *) pentry;
2258 /* 2^32 * ((sqrt(5) - 1) / 2) */
2259 const hashval_t scaled_golden_ratio = 0x9e3779b9;
2260 return CODE_LABEL_NUMBER (entry->label) * scaled_golden_ratio;
2263 static int
2264 ehl_eq (const void *pentry, const void *pdata)
2266 struct ehl_map_entry *entry = (struct ehl_map_entry *) pentry;
2267 struct ehl_map_entry *data = (struct ehl_map_entry *) pdata;
2269 return entry->label == data->label;
2272 /* This section handles removing dead code for flow. */
2274 /* Remove LABEL from exception_handler_label_map. */
2276 static void
2277 remove_exception_handler_label (rtx label)
2279 struct ehl_map_entry **slot, tmp;
2281 /* If exception_handler_label_map was not built yet,
2282 there is nothing to do. */
2283 if (cfun->eh->exception_handler_label_map == NULL)
2284 return;
2286 tmp.label = label;
2287 slot = (struct ehl_map_entry **)
2288 htab_find_slot (cfun->eh->exception_handler_label_map, &tmp, NO_INSERT);
2289 if (! slot)
2290 abort ();
2292 htab_clear_slot (cfun->eh->exception_handler_label_map, (void **) slot);
2295 /* Splice REGION from the region tree etc. */
2297 static void
2298 remove_eh_handler (struct eh_region *region)
2300 struct eh_region **pp, **pp_start, *p, *outer, *inner;
2301 rtx lab;
2303 /* For the benefit of efficiently handling REG_EH_REGION notes,
2304 replace this region in the region array with its containing
2305 region. Note that previous region deletions may result in
2306 multiple copies of this region in the array, so we have a
2307 list of alternate numbers by which we are known. */
2309 outer = region->outer;
2310 cfun->eh->region_array[region->region_number] = outer;
2311 if (region->aka)
2313 int i;
2314 EXECUTE_IF_SET_IN_BITMAP (region->aka, 0, i,
2315 { cfun->eh->region_array[i] = outer; });
2318 if (outer)
2320 if (!outer->aka)
2321 outer->aka = BITMAP_GGC_ALLOC ();
2322 if (region->aka)
2323 bitmap_a_or_b (outer->aka, outer->aka, region->aka);
2324 bitmap_set_bit (outer->aka, region->region_number);
2327 if (cfun->eh->built_landing_pads)
2328 lab = region->landing_pad;
2329 else
2330 lab = region->label;
2331 if (lab)
2332 remove_exception_handler_label (lab);
2334 if (outer)
2335 pp_start = &outer->inner;
2336 else
2337 pp_start = &cfun->eh->region_tree;
2338 for (pp = pp_start, p = *pp; p != region; pp = &p->next_peer, p = *pp)
2339 continue;
2340 *pp = region->next_peer;
2342 inner = region->inner;
2343 if (inner)
2345 for (p = inner; p->next_peer ; p = p->next_peer)
2346 p->outer = outer;
2347 p->outer = outer;
2349 p->next_peer = *pp_start;
2350 *pp_start = inner;
2353 if (region->type == ERT_CATCH)
2355 struct eh_region *try, *next, *prev;
2357 for (try = region->next_peer;
2358 try->type == ERT_CATCH;
2359 try = try->next_peer)
2360 continue;
2361 if (try->type != ERT_TRY)
2362 abort ();
2364 next = region->u.catch.next_catch;
2365 prev = region->u.catch.prev_catch;
2367 if (next)
2368 next->u.catch.prev_catch = prev;
2369 else
2370 try->u.try.last_catch = prev;
2371 if (prev)
2372 prev->u.catch.next_catch = next;
2373 else
2375 try->u.try.catch = next;
2376 if (! next)
2377 remove_eh_handler (try);
2382 /* LABEL heads a basic block that is about to be deleted. If this
2383 label corresponds to an exception region, we may be able to
2384 delete the region. */
2386 void
2387 maybe_remove_eh_handler (rtx label)
2389 struct ehl_map_entry **slot, tmp;
2390 struct eh_region *region;
2392 /* ??? After generating landing pads, it's not so simple to determine
2393 if the region data is completely unused. One must examine the
2394 landing pad and the post landing pad, and whether an inner try block
2395 is referencing the catch handlers directly. */
2396 if (cfun->eh->built_landing_pads)
2397 return;
2399 tmp.label = label;
2400 slot = (struct ehl_map_entry **)
2401 htab_find_slot (cfun->eh->exception_handler_label_map, &tmp, NO_INSERT);
2402 if (! slot)
2403 return;
2404 region = (*slot)->region;
2405 if (! region)
2406 return;
2408 /* Flow will want to remove MUST_NOT_THROW regions as unreachable
2409 because there is no path to the fallback call to terminate.
2410 But the region continues to affect call-site data until there
2411 are no more contained calls, which we don't see here. */
2412 if (region->type == ERT_MUST_NOT_THROW)
2414 htab_clear_slot (cfun->eh->exception_handler_label_map, (void **) slot);
2415 region->label = NULL_RTX;
2417 else
2418 remove_eh_handler (region);
2421 /* Invokes CALLBACK for every exception handler label. Only used by old
2422 loop hackery; should not be used by new code. */
2424 void
2425 for_each_eh_label (void (*callback) (rtx))
2427 htab_traverse (cfun->eh->exception_handler_label_map, for_each_eh_label_1,
2428 (void *)callback);
2431 static int
2432 for_each_eh_label_1 (void **pentry, void *data)
2434 struct ehl_map_entry *entry = *(struct ehl_map_entry **)pentry;
2435 void (*callback) (rtx) = (void (*) (rtx)) data;
2437 (*callback) (entry->label);
2438 return 1;
2441 /* This section describes CFG exception edges for flow. */
2443 /* For communicating between calls to reachable_next_level. */
2444 struct reachable_info GTY(())
2446 tree types_caught;
2447 tree types_allowed;
2448 rtx handlers;
2451 /* A subroutine of reachable_next_level. Return true if TYPE, or a
2452 base class of TYPE, is in HANDLED. */
2454 static int
2455 check_handled (tree handled, tree type)
2457 tree t;
2459 /* We can check for exact matches without front-end help. */
2460 if (! lang_eh_type_covers)
2462 for (t = handled; t ; t = TREE_CHAIN (t))
2463 if (TREE_VALUE (t) == type)
2464 return 1;
2466 else
2468 for (t = handled; t ; t = TREE_CHAIN (t))
2469 if ((*lang_eh_type_covers) (TREE_VALUE (t), type))
2470 return 1;
2473 return 0;
2476 /* A subroutine of reachable_next_level. If we are collecting a list
2477 of handlers, add one. After landing pad generation, reference
2478 it instead of the handlers themselves. Further, the handlers are
2479 all wired together, so by referencing one, we've got them all.
2480 Before landing pad generation we reference each handler individually.
2482 LP_REGION contains the landing pad; REGION is the handler. */
2484 static void
2485 add_reachable_handler (struct reachable_info *info, struct eh_region *lp_region, struct eh_region *region)
2487 if (! info)
2488 return;
2490 if (cfun->eh->built_landing_pads)
2492 if (! info->handlers)
2493 info->handlers = alloc_INSN_LIST (lp_region->landing_pad, NULL_RTX);
2495 else
2496 info->handlers = alloc_INSN_LIST (region->label, info->handlers);
2499 /* Process one level of exception regions for reachability.
2500 If TYPE_THROWN is non-null, then it is the *exact* type being
2501 propagated. If INFO is non-null, then collect handler labels
2502 and caught/allowed type information between invocations. */
2504 static enum reachable_code
2505 reachable_next_level (struct eh_region *region, tree type_thrown,
2506 struct reachable_info *info)
2508 switch (region->type)
2510 case ERT_CLEANUP:
2511 /* Before landing-pad generation, we model control flow
2512 directly to the individual handlers. In this way we can
2513 see that catch handler types may shadow one another. */
2514 add_reachable_handler (info, region, region);
2515 return RNL_MAYBE_CAUGHT;
2517 case ERT_TRY:
2519 struct eh_region *c;
2520 enum reachable_code ret = RNL_NOT_CAUGHT;
2522 for (c = region->u.try.catch; c ; c = c->u.catch.next_catch)
2524 /* A catch-all handler ends the search. */
2525 if (c->u.catch.type_list == NULL)
2527 add_reachable_handler (info, region, c);
2528 return RNL_CAUGHT;
2531 if (type_thrown)
2533 /* If we have at least one type match, end the search. */
2534 tree tp_node = c->u.catch.type_list;
2536 for (; tp_node; tp_node = TREE_CHAIN (tp_node))
2538 tree type = TREE_VALUE (tp_node);
2540 if (type == type_thrown
2541 || (lang_eh_type_covers
2542 && (*lang_eh_type_covers) (type, type_thrown)))
2544 add_reachable_handler (info, region, c);
2545 return RNL_CAUGHT;
2549 /* If we have definitive information of a match failure,
2550 the catch won't trigger. */
2551 if (lang_eh_type_covers)
2552 return RNL_NOT_CAUGHT;
2555 /* At this point, we either don't know what type is thrown or
2556 don't have front-end assistance to help deciding if it is
2557 covered by one of the types in the list for this region.
2559 We'd then like to add this region to the list of reachable
2560 handlers since it is indeed potentially reachable based on the
2561 information we have.
2563 Actually, this handler is for sure not reachable if all the
2564 types it matches have already been caught. That is, it is only
2565 potentially reachable if at least one of the types it catches
2566 has not been previously caught. */
2568 if (! info)
2569 ret = RNL_MAYBE_CAUGHT;
2570 else
2572 tree tp_node = c->u.catch.type_list;
2573 bool maybe_reachable = false;
2575 /* Compute the potential reachability of this handler and
2576 update the list of types caught at the same time. */
2577 for (; tp_node; tp_node = TREE_CHAIN (tp_node))
2579 tree type = TREE_VALUE (tp_node);
2581 if (! check_handled (info->types_caught, type))
2583 info->types_caught
2584 = tree_cons (NULL, type, info->types_caught);
2586 maybe_reachable = true;
2590 if (maybe_reachable)
2592 add_reachable_handler (info, region, c);
2594 /* ??? If the catch type is a base class of every allowed
2595 type, then we know we can stop the search. */
2596 ret = RNL_MAYBE_CAUGHT;
2601 return ret;
2604 case ERT_ALLOWED_EXCEPTIONS:
2605 /* An empty list of types definitely ends the search. */
2606 if (region->u.allowed.type_list == NULL_TREE)
2608 add_reachable_handler (info, region, region);
2609 return RNL_CAUGHT;
2612 /* Collect a list of lists of allowed types for use in detecting
2613 when a catch may be transformed into a catch-all. */
2614 if (info)
2615 info->types_allowed = tree_cons (NULL_TREE,
2616 region->u.allowed.type_list,
2617 info->types_allowed);
2619 /* If we have definitive information about the type hierarchy,
2620 then we can tell if the thrown type will pass through the
2621 filter. */
2622 if (type_thrown && lang_eh_type_covers)
2624 if (check_handled (region->u.allowed.type_list, type_thrown))
2625 return RNL_NOT_CAUGHT;
2626 else
2628 add_reachable_handler (info, region, region);
2629 return RNL_CAUGHT;
2633 add_reachable_handler (info, region, region);
2634 return RNL_MAYBE_CAUGHT;
2636 case ERT_CATCH:
2637 /* Catch regions are handled by their controlling try region. */
2638 return RNL_NOT_CAUGHT;
2640 case ERT_MUST_NOT_THROW:
2641 /* Here we end our search, since no exceptions may propagate.
2642 If we've touched down at some landing pad previous, then the
2643 explicit function call we generated may be used. Otherwise
2644 the call is made by the runtime. */
2645 if (info && info->handlers)
2647 add_reachable_handler (info, region, region);
2648 return RNL_CAUGHT;
2650 else
2651 return RNL_BLOCKED;
2653 case ERT_THROW:
2654 case ERT_FIXUP:
2655 case ERT_UNKNOWN:
2656 /* Shouldn't see these here. */
2657 break;
2660 abort ();
2663 /* Retrieve a list of labels of exception handlers which can be
2664 reached by a given insn. */
2667 reachable_handlers (rtx insn)
2669 struct reachable_info info;
2670 struct eh_region *region;
2671 tree type_thrown;
2672 int region_number;
2674 if (GET_CODE (insn) == JUMP_INSN
2675 && GET_CODE (PATTERN (insn)) == RESX)
2676 region_number = XINT (PATTERN (insn), 0);
2677 else
2679 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2680 if (!note || INTVAL (XEXP (note, 0)) <= 0)
2681 return NULL;
2682 region_number = INTVAL (XEXP (note, 0));
2685 memset (&info, 0, sizeof (info));
2687 region = cfun->eh->region_array[region_number];
2689 type_thrown = NULL_TREE;
2690 if (GET_CODE (insn) == JUMP_INSN
2691 && GET_CODE (PATTERN (insn)) == RESX)
2693 /* A RESX leaves a region instead of entering it. Thus the
2694 region itself may have been deleted out from under us. */
2695 if (region == NULL)
2696 return NULL;
2697 region = region->outer;
2699 else if (region->type == ERT_THROW)
2701 type_thrown = region->u.throw.type;
2702 region = region->outer;
2705 while (region)
2707 if (reachable_next_level (region, type_thrown, &info) >= RNL_CAUGHT)
2708 break;
2709 /* If we have processed one cleanup, there is no point in
2710 processing any more of them. Each cleanup will have an edge
2711 to the next outer cleanup region, so the flow graph will be
2712 accurate. */
2713 if (region->type == ERT_CLEANUP)
2714 region = region->u.cleanup.prev_try;
2715 else
2716 region = region->outer;
2719 return info.handlers;
2722 /* Determine if the given INSN can throw an exception that is caught
2723 within the function. */
2725 bool
2726 can_throw_internal (rtx insn)
2728 struct eh_region *region;
2729 tree type_thrown;
2730 rtx note;
2732 if (! INSN_P (insn))
2733 return false;
2735 if (GET_CODE (insn) == INSN
2736 && GET_CODE (PATTERN (insn)) == SEQUENCE)
2737 insn = XVECEXP (PATTERN (insn), 0, 0);
2739 if (GET_CODE (insn) == CALL_INSN
2740 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
2742 int i;
2743 for (i = 0; i < 3; ++i)
2745 rtx sub = XEXP (PATTERN (insn), i);
2746 for (; sub ; sub = NEXT_INSN (sub))
2747 if (can_throw_internal (sub))
2748 return true;
2750 return false;
2753 /* Every insn that might throw has an EH_REGION note. */
2754 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2755 if (!note || INTVAL (XEXP (note, 0)) <= 0)
2756 return false;
2758 region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
2760 type_thrown = NULL_TREE;
2761 if (region->type == ERT_THROW)
2763 type_thrown = region->u.throw.type;
2764 region = region->outer;
2767 /* If this exception is ignored by each and every containing region,
2768 then control passes straight out. The runtime may handle some
2769 regions, which also do not require processing internally. */
2770 for (; region; region = region->outer)
2772 enum reachable_code how = reachable_next_level (region, type_thrown, 0);
2773 if (how == RNL_BLOCKED)
2774 return false;
2775 if (how != RNL_NOT_CAUGHT)
2776 return true;
2779 return false;
2782 /* Determine if the given INSN can throw an exception that is
2783 visible outside the function. */
2785 bool
2786 can_throw_external (rtx insn)
2788 struct eh_region *region;
2789 tree type_thrown;
2790 rtx note;
2792 if (! INSN_P (insn))
2793 return false;
2795 if (GET_CODE (insn) == INSN
2796 && GET_CODE (PATTERN (insn)) == SEQUENCE)
2797 insn = XVECEXP (PATTERN (insn), 0, 0);
2799 if (GET_CODE (insn) == CALL_INSN
2800 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
2802 int i;
2803 for (i = 0; i < 3; ++i)
2805 rtx sub = XEXP (PATTERN (insn), i);
2806 for (; sub ; sub = NEXT_INSN (sub))
2807 if (can_throw_external (sub))
2808 return true;
2810 return false;
2813 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2814 if (!note)
2816 /* Calls (and trapping insns) without notes are outside any
2817 exception handling region in this function. We have to
2818 assume it might throw. Given that the front end and middle
2819 ends mark known NOTHROW functions, this isn't so wildly
2820 inaccurate. */
2821 return (GET_CODE (insn) == CALL_INSN
2822 || (flag_non_call_exceptions
2823 && may_trap_p (PATTERN (insn))));
2825 if (INTVAL (XEXP (note, 0)) <= 0)
2826 return false;
2828 region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
2830 type_thrown = NULL_TREE;
2831 if (region->type == ERT_THROW)
2833 type_thrown = region->u.throw.type;
2834 region = region->outer;
2837 /* If the exception is caught or blocked by any containing region,
2838 then it is not seen by any calling function. */
2839 for (; region ; region = region->outer)
2840 if (reachable_next_level (region, type_thrown, NULL) >= RNL_CAUGHT)
2841 return false;
2843 return true;
2846 /* Set current_function_nothrow and cfun->all_throwers_are_sibcalls. */
2848 void
2849 set_nothrow_function_flags (void)
2851 rtx insn;
2853 current_function_nothrow = 1;
2855 /* Assume cfun->all_throwers_are_sibcalls until we encounter
2856 something that can throw an exception. We specifically exempt
2857 CALL_INSNs that are SIBLING_CALL_P, as these are really jumps,
2858 and can't throw. Most CALL_INSNs are not SIBLING_CALL_P, so this
2859 is optimistic. */
2861 cfun->all_throwers_are_sibcalls = 1;
2863 if (! flag_exceptions)
2864 return;
2866 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2867 if (can_throw_external (insn))
2869 current_function_nothrow = 0;
2871 if (GET_CODE (insn) != CALL_INSN || !SIBLING_CALL_P (insn))
2873 cfun->all_throwers_are_sibcalls = 0;
2874 return;
2878 for (insn = current_function_epilogue_delay_list; insn;
2879 insn = XEXP (insn, 1))
2880 if (can_throw_external (insn))
2882 current_function_nothrow = 0;
2884 if (GET_CODE (insn) != CALL_INSN || !SIBLING_CALL_P (insn))
2886 cfun->all_throwers_are_sibcalls = 0;
2887 return;
2893 /* Various hooks for unwind library. */
2895 /* Do any necessary initialization to access arbitrary stack frames.
2896 On the SPARC, this means flushing the register windows. */
2898 void
2899 expand_builtin_unwind_init (void)
2901 /* Set this so all the registers get saved in our frame; we need to be
2902 able to copy the saved values for any registers from frames we unwind. */
2903 current_function_has_nonlocal_label = 1;
2905 #ifdef SETUP_FRAME_ADDRESSES
2906 SETUP_FRAME_ADDRESSES ();
2907 #endif
2911 expand_builtin_eh_return_data_regno (tree arglist)
2913 tree which = TREE_VALUE (arglist);
2914 unsigned HOST_WIDE_INT iwhich;
2916 if (TREE_CODE (which) != INTEGER_CST)
2918 error ("argument of `__builtin_eh_return_regno' must be constant");
2919 return constm1_rtx;
2922 iwhich = tree_low_cst (which, 1);
2923 iwhich = EH_RETURN_DATA_REGNO (iwhich);
2924 if (iwhich == INVALID_REGNUM)
2925 return constm1_rtx;
2927 #ifdef DWARF_FRAME_REGNUM
2928 iwhich = DWARF_FRAME_REGNUM (iwhich);
2929 #else
2930 iwhich = DBX_REGISTER_NUMBER (iwhich);
2931 #endif
2933 return GEN_INT (iwhich);
2936 /* Given a value extracted from the return address register or stack slot,
2937 return the actual address encoded in that value. */
2940 expand_builtin_extract_return_addr (tree addr_tree)
2942 rtx addr = expand_expr (addr_tree, NULL_RTX, Pmode, 0);
2944 if (GET_MODE (addr) != Pmode
2945 && GET_MODE (addr) != VOIDmode)
2947 #ifdef POINTERS_EXTEND_UNSIGNED
2948 addr = convert_memory_address (Pmode, addr);
2949 #else
2950 addr = convert_to_mode (Pmode, addr, 0);
2951 #endif
2954 /* First mask out any unwanted bits. */
2955 #ifdef MASK_RETURN_ADDR
2956 expand_and (Pmode, addr, MASK_RETURN_ADDR, addr);
2957 #endif
2959 /* Then adjust to find the real return address. */
2960 #if defined (RETURN_ADDR_OFFSET)
2961 addr = plus_constant (addr, RETURN_ADDR_OFFSET);
2962 #endif
2964 return addr;
2967 /* Given an actual address in addr_tree, do any necessary encoding
2968 and return the value to be stored in the return address register or
2969 stack slot so the epilogue will return to that address. */
2972 expand_builtin_frob_return_addr (tree addr_tree)
2974 rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, 0);
2976 #ifdef POINTERS_EXTEND_UNSIGNED
2977 if (GET_MODE (addr) != Pmode)
2978 addr = convert_memory_address (Pmode, addr);
2979 #endif
2981 #ifdef RETURN_ADDR_OFFSET
2982 addr = force_reg (Pmode, addr);
2983 addr = plus_constant (addr, -RETURN_ADDR_OFFSET);
2984 #endif
2986 return addr;
2989 /* Set up the epilogue with the magic bits we'll need to return to the
2990 exception handler. */
2992 void
2993 expand_builtin_eh_return (tree stackadj_tree ATTRIBUTE_UNUSED,
2994 tree handler_tree)
2996 rtx tmp;
2998 #ifdef EH_RETURN_STACKADJ_RTX
2999 tmp = expand_expr (stackadj_tree, cfun->eh->ehr_stackadj, VOIDmode, 0);
3000 #ifdef POINTERS_EXTEND_UNSIGNED
3001 if (GET_MODE (tmp) != Pmode)
3002 tmp = convert_memory_address (Pmode, tmp);
3003 #endif
3004 if (!cfun->eh->ehr_stackadj)
3005 cfun->eh->ehr_stackadj = copy_to_reg (tmp);
3006 else if (tmp != cfun->eh->ehr_stackadj)
3007 emit_move_insn (cfun->eh->ehr_stackadj, tmp);
3008 #endif
3010 tmp = expand_expr (handler_tree, cfun->eh->ehr_handler, VOIDmode, 0);
3011 #ifdef POINTERS_EXTEND_UNSIGNED
3012 if (GET_MODE (tmp) != Pmode)
3013 tmp = convert_memory_address (Pmode, tmp);
3014 #endif
3015 if (!cfun->eh->ehr_handler)
3016 cfun->eh->ehr_handler = copy_to_reg (tmp);
3017 else if (tmp != cfun->eh->ehr_handler)
3018 emit_move_insn (cfun->eh->ehr_handler, tmp);
3020 if (!cfun->eh->ehr_label)
3021 cfun->eh->ehr_label = gen_label_rtx ();
3022 emit_jump (cfun->eh->ehr_label);
3025 void
3026 expand_eh_return (void)
3028 rtx around_label;
3030 if (! cfun->eh->ehr_label)
3031 return;
3033 current_function_calls_eh_return = 1;
3035 #ifdef EH_RETURN_STACKADJ_RTX
3036 emit_move_insn (EH_RETURN_STACKADJ_RTX, const0_rtx);
3037 #endif
3039 around_label = gen_label_rtx ();
3040 emit_jump (around_label);
3042 emit_label (cfun->eh->ehr_label);
3043 clobber_return_register ();
3045 #ifdef EH_RETURN_STACKADJ_RTX
3046 emit_move_insn (EH_RETURN_STACKADJ_RTX, cfun->eh->ehr_stackadj);
3047 #endif
3049 #ifdef HAVE_eh_return
3050 if (HAVE_eh_return)
3051 emit_insn (gen_eh_return (cfun->eh->ehr_handler));
3052 else
3053 #endif
3055 #ifdef EH_RETURN_HANDLER_RTX
3056 emit_move_insn (EH_RETURN_HANDLER_RTX, cfun->eh->ehr_handler);
3057 #else
3058 error ("__builtin_eh_return not supported on this target");
3059 #endif
3062 emit_label (around_label);
3065 /* In the following functions, we represent entries in the action table
3066 as 1-based indices. Special cases are:
3068 0: null action record, non-null landing pad; implies cleanups
3069 -1: null action record, null landing pad; implies no action
3070 -2: no call-site entry; implies must_not_throw
3071 -3: we have yet to process outer regions
3073 Further, no special cases apply to the "next" field of the record.
3074 For next, 0 means end of list. */
3076 struct action_record
3078 int offset;
3079 int filter;
3080 int next;
3083 static int
3084 action_record_eq (const void *pentry, const void *pdata)
3086 const struct action_record *entry = (const struct action_record *) pentry;
3087 const struct action_record *data = (const struct action_record *) pdata;
3088 return entry->filter == data->filter && entry->next == data->next;
3091 static hashval_t
3092 action_record_hash (const void *pentry)
3094 const struct action_record *entry = (const struct action_record *) pentry;
3095 return entry->next * 1009 + entry->filter;
3098 static int
3099 add_action_record (htab_t ar_hash, int filter, int next)
3101 struct action_record **slot, *new, tmp;
3103 tmp.filter = filter;
3104 tmp.next = next;
3105 slot = (struct action_record **) htab_find_slot (ar_hash, &tmp, INSERT);
3107 if ((new = *slot) == NULL)
3109 new = xmalloc (sizeof (*new));
3110 new->offset = VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data) + 1;
3111 new->filter = filter;
3112 new->next = next;
3113 *slot = new;
3115 /* The filter value goes in untouched. The link to the next
3116 record is a "self-relative" byte offset, or zero to indicate
3117 that there is no next record. So convert the absolute 1 based
3118 indices we've been carrying around into a displacement. */
3120 push_sleb128 (&cfun->eh->action_record_data, filter);
3121 if (next)
3122 next -= VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data) + 1;
3123 push_sleb128 (&cfun->eh->action_record_data, next);
3126 return new->offset;
3129 static int
3130 collect_one_action_chain (htab_t ar_hash, struct eh_region *region)
3132 struct eh_region *c;
3133 int next;
3135 /* If we've reached the top of the region chain, then we have
3136 no actions, and require no landing pad. */
3137 if (region == NULL)
3138 return -1;
3140 switch (region->type)
3142 case ERT_CLEANUP:
3143 /* A cleanup adds a zero filter to the beginning of the chain, but
3144 there are special cases to look out for. If there are *only*
3145 cleanups along a path, then it compresses to a zero action.
3146 Further, if there are multiple cleanups along a path, we only
3147 need to represent one of them, as that is enough to trigger
3148 entry to the landing pad at runtime. */
3149 next = collect_one_action_chain (ar_hash, region->outer);
3150 if (next <= 0)
3151 return 0;
3152 for (c = region->outer; c ; c = c->outer)
3153 if (c->type == ERT_CLEANUP)
3154 return next;
3155 return add_action_record (ar_hash, 0, next);
3157 case ERT_TRY:
3158 /* Process the associated catch regions in reverse order.
3159 If there's a catch-all handler, then we don't need to
3160 search outer regions. Use a magic -3 value to record
3161 that we haven't done the outer search. */
3162 next = -3;
3163 for (c = region->u.try.last_catch; c ; c = c->u.catch.prev_catch)
3165 if (c->u.catch.type_list == NULL)
3167 /* Retrieve the filter from the head of the filter list
3168 where we have stored it (see assign_filter_values). */
3169 int filter
3170 = TREE_INT_CST_LOW (TREE_VALUE (c->u.catch.filter_list));
3172 next = add_action_record (ar_hash, filter, 0);
3174 else
3176 /* Once the outer search is done, trigger an action record for
3177 each filter we have. */
3178 tree flt_node;
3180 if (next == -3)
3182 next = collect_one_action_chain (ar_hash, region->outer);
3184 /* If there is no next action, terminate the chain. */
3185 if (next == -1)
3186 next = 0;
3187 /* If all outer actions are cleanups or must_not_throw,
3188 we'll have no action record for it, since we had wanted
3189 to encode these states in the call-site record directly.
3190 Add a cleanup action to the chain to catch these. */
3191 else if (next <= 0)
3192 next = add_action_record (ar_hash, 0, 0);
3195 flt_node = c->u.catch.filter_list;
3196 for (; flt_node; flt_node = TREE_CHAIN (flt_node))
3198 int filter = TREE_INT_CST_LOW (TREE_VALUE (flt_node));
3199 next = add_action_record (ar_hash, filter, next);
3203 return next;
3205 case ERT_ALLOWED_EXCEPTIONS:
3206 /* An exception specification adds its filter to the
3207 beginning of the chain. */
3208 next = collect_one_action_chain (ar_hash, region->outer);
3209 return add_action_record (ar_hash, region->u.allowed.filter,
3210 next < 0 ? 0 : next);
3212 case ERT_MUST_NOT_THROW:
3213 /* A must-not-throw region with no inner handlers or cleanups
3214 requires no call-site entry. Note that this differs from
3215 the no handler or cleanup case in that we do require an lsda
3216 to be generated. Return a magic -2 value to record this. */
3217 return -2;
3219 case ERT_CATCH:
3220 case ERT_THROW:
3221 /* CATCH regions are handled in TRY above. THROW regions are
3222 for optimization information only and produce no output. */
3223 return collect_one_action_chain (ar_hash, region->outer);
3225 default:
3226 abort ();
3230 static int
3231 add_call_site (rtx landing_pad, int action)
3233 struct call_site_record *data = cfun->eh->call_site_data;
3234 int used = cfun->eh->call_site_data_used;
3235 int size = cfun->eh->call_site_data_size;
3237 if (used >= size)
3239 size = (size ? size * 2 : 64);
3240 data = ggc_realloc (data, sizeof (*data) * size);
3241 cfun->eh->call_site_data = data;
3242 cfun->eh->call_site_data_size = size;
3245 data[used].landing_pad = landing_pad;
3246 data[used].action = action;
3248 cfun->eh->call_site_data_used = used + 1;
3250 return used + call_site_base;
3253 /* Turn REG_EH_REGION notes back into NOTE_INSN_EH_REGION notes.
3254 The new note numbers will not refer to region numbers, but
3255 instead to call site entries. */
3257 void
3258 convert_to_eh_region_ranges (void)
3260 rtx insn, iter, note;
3261 htab_t ar_hash;
3262 int last_action = -3;
3263 rtx last_action_insn = NULL_RTX;
3264 rtx last_landing_pad = NULL_RTX;
3265 rtx first_no_action_insn = NULL_RTX;
3266 int call_site = 0;
3268 if (USING_SJLJ_EXCEPTIONS || cfun->eh->region_tree == NULL)
3269 return;
3271 VARRAY_UCHAR_INIT (cfun->eh->action_record_data, 64, "action_record_data");
3273 ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
3275 for (iter = get_insns (); iter ; iter = NEXT_INSN (iter))
3276 if (INSN_P (iter))
3278 struct eh_region *region;
3279 int this_action;
3280 rtx this_landing_pad;
3282 insn = iter;
3283 if (GET_CODE (insn) == INSN
3284 && GET_CODE (PATTERN (insn)) == SEQUENCE)
3285 insn = XVECEXP (PATTERN (insn), 0, 0);
3287 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3288 if (!note)
3290 if (! (GET_CODE (insn) == CALL_INSN
3291 || (flag_non_call_exceptions
3292 && may_trap_p (PATTERN (insn)))))
3293 continue;
3294 this_action = -1;
3295 region = NULL;
3297 else
3299 if (INTVAL (XEXP (note, 0)) <= 0)
3300 continue;
3301 region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
3302 this_action = collect_one_action_chain (ar_hash, region);
3305 /* Existence of catch handlers, or must-not-throw regions
3306 implies that an lsda is needed (even if empty). */
3307 if (this_action != -1)
3308 cfun->uses_eh_lsda = 1;
3310 /* Delay creation of region notes for no-action regions
3311 until we're sure that an lsda will be required. */
3312 else if (last_action == -3)
3314 first_no_action_insn = iter;
3315 last_action = -1;
3318 /* Cleanups and handlers may share action chains but not
3319 landing pads. Collect the landing pad for this region. */
3320 if (this_action >= 0)
3322 struct eh_region *o;
3323 for (o = region; ! o->landing_pad ; o = o->outer)
3324 continue;
3325 this_landing_pad = o->landing_pad;
3327 else
3328 this_landing_pad = NULL_RTX;
3330 /* Differing actions or landing pads implies a change in call-site
3331 info, which implies some EH_REGION note should be emitted. */
3332 if (last_action != this_action
3333 || last_landing_pad != this_landing_pad)
3335 /* If we'd not seen a previous action (-3) or the previous
3336 action was must-not-throw (-2), then we do not need an
3337 end note. */
3338 if (last_action >= -1)
3340 /* If we delayed the creation of the begin, do it now. */
3341 if (first_no_action_insn)
3343 call_site = add_call_site (NULL_RTX, 0);
3344 note = emit_note_before (NOTE_INSN_EH_REGION_BEG,
3345 first_no_action_insn);
3346 NOTE_EH_HANDLER (note) = call_site;
3347 first_no_action_insn = NULL_RTX;
3350 note = emit_note_after (NOTE_INSN_EH_REGION_END,
3351 last_action_insn);
3352 NOTE_EH_HANDLER (note) = call_site;
3355 /* If the new action is must-not-throw, then no region notes
3356 are created. */
3357 if (this_action >= -1)
3359 call_site = add_call_site (this_landing_pad,
3360 this_action < 0 ? 0 : this_action);
3361 note = emit_note_before (NOTE_INSN_EH_REGION_BEG, iter);
3362 NOTE_EH_HANDLER (note) = call_site;
3365 last_action = this_action;
3366 last_landing_pad = this_landing_pad;
3368 last_action_insn = iter;
3371 if (last_action >= -1 && ! first_no_action_insn)
3373 note = emit_note_after (NOTE_INSN_EH_REGION_END, last_action_insn);
3374 NOTE_EH_HANDLER (note) = call_site;
3377 htab_delete (ar_hash);
3381 static void
3382 push_uleb128 (varray_type *data_area, unsigned int value)
3386 unsigned char byte = value & 0x7f;
3387 value >>= 7;
3388 if (value)
3389 byte |= 0x80;
3390 VARRAY_PUSH_UCHAR (*data_area, byte);
3392 while (value);
3395 static void
3396 push_sleb128 (varray_type *data_area, int value)
3398 unsigned char byte;
3399 int more;
3403 byte = value & 0x7f;
3404 value >>= 7;
3405 more = ! ((value == 0 && (byte & 0x40) == 0)
3406 || (value == -1 && (byte & 0x40) != 0));
3407 if (more)
3408 byte |= 0x80;
3409 VARRAY_PUSH_UCHAR (*data_area, byte);
3411 while (more);
3415 #ifndef HAVE_AS_LEB128
3416 static int
3417 dw2_size_of_call_site_table (void)
3419 int n = cfun->eh->call_site_data_used;
3420 int size = n * (4 + 4 + 4);
3421 int i;
3423 for (i = 0; i < n; ++i)
3425 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3426 size += size_of_uleb128 (cs->action);
3429 return size;
3432 static int
3433 sjlj_size_of_call_site_table (void)
3435 int n = cfun->eh->call_site_data_used;
3436 int size = 0;
3437 int i;
3439 for (i = 0; i < n; ++i)
3441 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3442 size += size_of_uleb128 (INTVAL (cs->landing_pad));
3443 size += size_of_uleb128 (cs->action);
3446 return size;
3448 #endif
3450 static void
3451 dw2_output_call_site_table (void)
3453 const char *const function_start_lab
3454 = IDENTIFIER_POINTER (current_function_func_begin_label);
3455 int n = cfun->eh->call_site_data_used;
3456 int i;
3458 for (i = 0; i < n; ++i)
3460 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3461 char reg_start_lab[32];
3462 char reg_end_lab[32];
3463 char landing_pad_lab[32];
3465 ASM_GENERATE_INTERNAL_LABEL (reg_start_lab, "LEHB", call_site_base + i);
3466 ASM_GENERATE_INTERNAL_LABEL (reg_end_lab, "LEHE", call_site_base + i);
3468 if (cs->landing_pad)
3469 ASM_GENERATE_INTERNAL_LABEL (landing_pad_lab, "L",
3470 CODE_LABEL_NUMBER (cs->landing_pad));
3472 /* ??? Perhaps use insn length scaling if the assembler supports
3473 generic arithmetic. */
3474 /* ??? Perhaps use attr_length to choose data1 or data2 instead of
3475 data4 if the function is small enough. */
3476 #ifdef HAVE_AS_LEB128
3477 dw2_asm_output_delta_uleb128 (reg_start_lab, function_start_lab,
3478 "region %d start", i);
3479 dw2_asm_output_delta_uleb128 (reg_end_lab, reg_start_lab,
3480 "length");
3481 if (cs->landing_pad)
3482 dw2_asm_output_delta_uleb128 (landing_pad_lab, function_start_lab,
3483 "landing pad");
3484 else
3485 dw2_asm_output_data_uleb128 (0, "landing pad");
3486 #else
3487 dw2_asm_output_delta (4, reg_start_lab, function_start_lab,
3488 "region %d start", i);
3489 dw2_asm_output_delta (4, reg_end_lab, reg_start_lab, "length");
3490 if (cs->landing_pad)
3491 dw2_asm_output_delta (4, landing_pad_lab, function_start_lab,
3492 "landing pad");
3493 else
3494 dw2_asm_output_data (4, 0, "landing pad");
3495 #endif
3496 dw2_asm_output_data_uleb128 (cs->action, "action");
3499 call_site_base += n;
3502 static void
3503 sjlj_output_call_site_table (void)
3505 int n = cfun->eh->call_site_data_used;
3506 int i;
3508 for (i = 0; i < n; ++i)
3510 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3512 dw2_asm_output_data_uleb128 (INTVAL (cs->landing_pad),
3513 "region %d landing pad", i);
3514 dw2_asm_output_data_uleb128 (cs->action, "action");
3517 call_site_base += n;
3520 /* Tell assembler to switch to the section for the exception handling
3521 table. */
3523 void
3524 default_exception_section (void)
3526 if (targetm.have_named_sections)
3528 int flags;
3529 #ifdef HAVE_LD_RO_RW_SECTION_MIXING
3530 int tt_format = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3532 flags = (! flag_pic
3533 || ((tt_format & 0x70) != DW_EH_PE_absptr
3534 && (tt_format & 0x70) != DW_EH_PE_aligned))
3535 ? 0 : SECTION_WRITE;
3536 #else
3537 flags = SECTION_WRITE;
3538 #endif
3539 named_section_flags (".gcc_except_table", flags);
3541 else if (flag_pic)
3542 data_section ();
3543 else
3544 readonly_data_section ();
3547 void
3548 output_function_exception_table (void)
3550 int tt_format, cs_format, lp_format, i, n;
3551 #ifdef HAVE_AS_LEB128
3552 char ttype_label[32];
3553 char cs_after_size_label[32];
3554 char cs_end_label[32];
3555 #else
3556 int call_site_len;
3557 #endif
3558 int have_tt_data;
3559 int tt_format_size = 0;
3561 /* Not all functions need anything. */
3562 if (! cfun->uses_eh_lsda)
3563 return;
3565 #ifdef IA64_UNWIND_INFO
3566 fputs ("\t.personality\t", asm_out_file);
3567 output_addr_const (asm_out_file, eh_personality_libfunc);
3568 fputs ("\n\t.handlerdata\n", asm_out_file);
3569 /* Note that varasm still thinks we're in the function's code section.
3570 The ".endp" directive that will immediately follow will take us back. */
3571 #else
3572 (*targetm.asm_out.exception_section) ();
3573 #endif
3575 have_tt_data = (VARRAY_ACTIVE_SIZE (cfun->eh->ttype_data) > 0
3576 || VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data) > 0);
3578 /* Indicate the format of the @TType entries. */
3579 if (! have_tt_data)
3580 tt_format = DW_EH_PE_omit;
3581 else
3583 tt_format = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3584 #ifdef HAVE_AS_LEB128
3585 ASM_GENERATE_INTERNAL_LABEL (ttype_label, "LLSDATT",
3586 current_function_funcdef_no);
3587 #endif
3588 tt_format_size = size_of_encoded_value (tt_format);
3590 assemble_align (tt_format_size * BITS_PER_UNIT);
3593 (*targetm.asm_out.internal_label) (asm_out_file, "LLSDA",
3594 current_function_funcdef_no);
3596 /* The LSDA header. */
3598 /* Indicate the format of the landing pad start pointer. An omitted
3599 field implies @LPStart == @Start. */
3600 /* Currently we always put @LPStart == @Start. This field would
3601 be most useful in moving the landing pads completely out of
3602 line to another section, but it could also be used to minimize
3603 the size of uleb128 landing pad offsets. */
3604 lp_format = DW_EH_PE_omit;
3605 dw2_asm_output_data (1, lp_format, "@LPStart format (%s)",
3606 eh_data_format_name (lp_format));
3608 /* @LPStart pointer would go here. */
3610 dw2_asm_output_data (1, tt_format, "@TType format (%s)",
3611 eh_data_format_name (tt_format));
3613 #ifndef HAVE_AS_LEB128
3614 if (USING_SJLJ_EXCEPTIONS)
3615 call_site_len = sjlj_size_of_call_site_table ();
3616 else
3617 call_site_len = dw2_size_of_call_site_table ();
3618 #endif
3620 /* A pc-relative 4-byte displacement to the @TType data. */
3621 if (have_tt_data)
3623 #ifdef HAVE_AS_LEB128
3624 char ttype_after_disp_label[32];
3625 ASM_GENERATE_INTERNAL_LABEL (ttype_after_disp_label, "LLSDATTD",
3626 current_function_funcdef_no);
3627 dw2_asm_output_delta_uleb128 (ttype_label, ttype_after_disp_label,
3628 "@TType base offset");
3629 ASM_OUTPUT_LABEL (asm_out_file, ttype_after_disp_label);
3630 #else
3631 /* Ug. Alignment queers things. */
3632 unsigned int before_disp, after_disp, last_disp, disp;
3634 before_disp = 1 + 1;
3635 after_disp = (1 + size_of_uleb128 (call_site_len)
3636 + call_site_len
3637 + VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data)
3638 + (VARRAY_ACTIVE_SIZE (cfun->eh->ttype_data)
3639 * tt_format_size));
3641 disp = after_disp;
3644 unsigned int disp_size, pad;
3646 last_disp = disp;
3647 disp_size = size_of_uleb128 (disp);
3648 pad = before_disp + disp_size + after_disp;
3649 if (pad % tt_format_size)
3650 pad = tt_format_size - (pad % tt_format_size);
3651 else
3652 pad = 0;
3653 disp = after_disp + pad;
3655 while (disp != last_disp);
3657 dw2_asm_output_data_uleb128 (disp, "@TType base offset");
3658 #endif
3661 /* Indicate the format of the call-site offsets. */
3662 #ifdef HAVE_AS_LEB128
3663 cs_format = DW_EH_PE_uleb128;
3664 #else
3665 cs_format = DW_EH_PE_udata4;
3666 #endif
3667 dw2_asm_output_data (1, cs_format, "call-site format (%s)",
3668 eh_data_format_name (cs_format));
3670 #ifdef HAVE_AS_LEB128
3671 ASM_GENERATE_INTERNAL_LABEL (cs_after_size_label, "LLSDACSB",
3672 current_function_funcdef_no);
3673 ASM_GENERATE_INTERNAL_LABEL (cs_end_label, "LLSDACSE",
3674 current_function_funcdef_no);
3675 dw2_asm_output_delta_uleb128 (cs_end_label, cs_after_size_label,
3676 "Call-site table length");
3677 ASM_OUTPUT_LABEL (asm_out_file, cs_after_size_label);
3678 if (USING_SJLJ_EXCEPTIONS)
3679 sjlj_output_call_site_table ();
3680 else
3681 dw2_output_call_site_table ();
3682 ASM_OUTPUT_LABEL (asm_out_file, cs_end_label);
3683 #else
3684 dw2_asm_output_data_uleb128 (call_site_len,"Call-site table length");
3685 if (USING_SJLJ_EXCEPTIONS)
3686 sjlj_output_call_site_table ();
3687 else
3688 dw2_output_call_site_table ();
3689 #endif
3691 /* ??? Decode and interpret the data for flag_debug_asm. */
3692 n = VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data);
3693 for (i = 0; i < n; ++i)
3694 dw2_asm_output_data (1, VARRAY_UCHAR (cfun->eh->action_record_data, i),
3695 (i ? NULL : "Action record table"));
3697 if (have_tt_data)
3698 assemble_align (tt_format_size * BITS_PER_UNIT);
3700 i = VARRAY_ACTIVE_SIZE (cfun->eh->ttype_data);
3701 while (i-- > 0)
3703 tree type = VARRAY_TREE (cfun->eh->ttype_data, i);
3704 rtx value;
3706 if (type == NULL_TREE)
3707 type = integer_zero_node;
3708 else
3709 type = lookup_type_for_runtime (type);
3711 value = expand_expr (type, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
3712 if (tt_format == DW_EH_PE_absptr || tt_format == DW_EH_PE_aligned)
3713 assemble_integer (value, tt_format_size,
3714 tt_format_size * BITS_PER_UNIT, 1);
3715 else
3716 dw2_asm_output_encoded_addr_rtx (tt_format, value, NULL);
3719 #ifdef HAVE_AS_LEB128
3720 if (have_tt_data)
3721 ASM_OUTPUT_LABEL (asm_out_file, ttype_label);
3722 #endif
3724 /* ??? Decode and interpret the data for flag_debug_asm. */
3725 n = VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data);
3726 for (i = 0; i < n; ++i)
3727 dw2_asm_output_data (1, VARRAY_UCHAR (cfun->eh->ehspec_data, i),
3728 (i ? NULL : "Exception specification table"));
3730 function_section (current_function_decl);
3733 #include "gt-except.h"