* final.c (final_scan_insn): Run FINAL_PRESCAN_INSNS on asm insns
[official-gcc.git] / gcc / except.c
blobedbcda10cf48e432afece1449e82385076919065
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"
76 #include "cgraph.h"
78 /* Provide defaults for stuff that may not be defined when using
79 sjlj exceptions. */
80 #ifndef EH_RETURN_DATA_REGNO
81 #define EH_RETURN_DATA_REGNO(N) INVALID_REGNUM
82 #endif
85 /* Nonzero means enable synchronous exceptions for non-call instructions. */
86 int flag_non_call_exceptions;
88 /* Protect cleanup actions with must-not-throw regions, with a call
89 to the given failure handler. */
90 tree (*lang_protect_cleanup_actions) (void);
92 /* Return true if type A catches type B. */
93 int (*lang_eh_type_covers) (tree a, tree b);
95 /* Map a type to a runtime object to match type. */
96 tree (*lang_eh_runtime_type) (tree);
98 /* A hash table of label to region number. */
100 struct ehl_map_entry GTY(())
102 rtx label;
103 struct eh_region *region;
106 static GTY(()) int call_site_base;
107 static GTY ((param_is (union tree_node)))
108 htab_t type_to_runtime_map;
110 /* Describe the SjLj_Function_Context structure. */
111 static GTY(()) tree sjlj_fc_type_node;
112 static int sjlj_fc_call_site_ofs;
113 static int sjlj_fc_data_ofs;
114 static int sjlj_fc_personality_ofs;
115 static int sjlj_fc_lsda_ofs;
116 static int sjlj_fc_jbuf_ofs;
118 /* Describes one exception region. */
119 struct eh_region GTY(())
121 /* The immediately surrounding region. */
122 struct eh_region *outer;
124 /* The list of immediately contained regions. */
125 struct eh_region *inner;
126 struct eh_region *next_peer;
128 /* An identifier for this region. */
129 int region_number;
131 /* When a region is deleted, its parents inherit the REG_EH_REGION
132 numbers already assigned. */
133 bitmap aka;
135 /* Each region does exactly one thing. */
136 enum eh_region_type
138 ERT_UNKNOWN = 0,
139 ERT_CLEANUP,
140 ERT_TRY,
141 ERT_CATCH,
142 ERT_ALLOWED_EXCEPTIONS,
143 ERT_MUST_NOT_THROW,
144 ERT_THROW,
145 ERT_FIXUP
146 } type;
148 /* Holds the action to perform based on the preceding type. */
149 union eh_region_u {
150 /* A list of catch blocks, a surrounding try block,
151 and the label for continuing after a catch. */
152 struct eh_region_u_try {
153 struct eh_region *catch;
154 struct eh_region *last_catch;
155 struct eh_region *prev_try;
156 rtx continue_label;
157 } GTY ((tag ("ERT_TRY"))) try;
159 /* The list through the catch handlers, the list of type objects
160 matched, and the list of associated filters. */
161 struct eh_region_u_catch {
162 struct eh_region *next_catch;
163 struct eh_region *prev_catch;
164 tree type_list;
165 tree filter_list;
166 } GTY ((tag ("ERT_CATCH"))) catch;
168 /* A tree_list of allowed types. */
169 struct eh_region_u_allowed {
170 tree type_list;
171 int filter;
172 } GTY ((tag ("ERT_ALLOWED_EXCEPTIONS"))) allowed;
174 /* The type given by a call to "throw foo();", or discovered
175 for a throw. */
176 struct eh_region_u_throw {
177 tree type;
178 } GTY ((tag ("ERT_THROW"))) throw;
180 /* Retain the cleanup expression even after expansion so that
181 we can match up fixup regions. */
182 struct eh_region_u_cleanup {
183 tree exp;
184 struct eh_region *prev_try;
185 } GTY ((tag ("ERT_CLEANUP"))) cleanup;
187 /* The real region (by expression and by pointer) that fixup code
188 should live in. */
189 struct eh_region_u_fixup {
190 tree cleanup_exp;
191 struct eh_region *real_region;
192 } GTY ((tag ("ERT_FIXUP"))) fixup;
193 } GTY ((desc ("%0.type"))) u;
195 /* Entry point for this region's handler before landing pads are built. */
196 rtx label;
198 /* Entry point for this region's handler from the runtime eh library. */
199 rtx landing_pad;
201 /* Entry point for this region's handler from an inner region. */
202 rtx post_landing_pad;
204 /* The RESX insn for handing off control to the next outermost handler,
205 if appropriate. */
206 rtx resume;
208 /* True if something in this region may throw. */
209 unsigned may_contain_throw : 1;
212 struct call_site_record GTY(())
214 rtx landing_pad;
215 int action;
218 /* Used to save exception status for each function. */
219 struct eh_status GTY(())
221 /* The tree of all regions for this function. */
222 struct eh_region *region_tree;
224 /* The same information as an indexable array. */
225 struct eh_region ** GTY ((length ("%h.last_region_number"))) region_array;
227 /* The most recently open region. */
228 struct eh_region *cur_region;
230 /* This is the region for which we are processing catch blocks. */
231 struct eh_region *try_region;
233 rtx filter;
234 rtx exc_ptr;
236 int built_landing_pads;
237 int last_region_number;
239 varray_type ttype_data;
240 varray_type ehspec_data;
241 varray_type action_record_data;
243 htab_t GTY ((param_is (struct ehl_map_entry))) exception_handler_label_map;
245 struct call_site_record * GTY ((length ("%h.call_site_data_used")))
246 call_site_data;
247 int call_site_data_used;
248 int call_site_data_size;
250 rtx ehr_stackadj;
251 rtx ehr_handler;
252 rtx ehr_label;
254 rtx sjlj_fc;
255 rtx sjlj_exit_after;
259 static int t2r_eq (const void *, const void *);
260 static hashval_t t2r_hash (const void *);
261 static void add_type_for_runtime (tree);
262 static tree lookup_type_for_runtime (tree);
264 static struct eh_region *expand_eh_region_end (void);
266 static rtx get_exception_filter (struct function *);
268 static void collect_eh_region_array (void);
269 static void resolve_fixup_regions (void);
270 static void remove_fixup_regions (void);
271 static void remove_unreachable_regions (rtx);
272 static void convert_from_eh_region_ranges_1 (rtx *, int *, int);
274 static struct eh_region *duplicate_eh_region_1 (struct eh_region *,
275 struct inline_remap *);
276 static void duplicate_eh_region_2 (struct eh_region *, struct eh_region **);
277 static int ttypes_filter_eq (const void *, const void *);
278 static hashval_t ttypes_filter_hash (const void *);
279 static int ehspec_filter_eq (const void *, const void *);
280 static hashval_t ehspec_filter_hash (const void *);
281 static int add_ttypes_entry (htab_t, tree);
282 static int add_ehspec_entry (htab_t, htab_t, tree);
283 static void assign_filter_values (void);
284 static void build_post_landing_pads (void);
285 static void connect_post_landing_pads (void);
286 static void dw2_build_landing_pads (void);
288 struct sjlj_lp_info;
289 static bool sjlj_find_directly_reachable_regions (struct sjlj_lp_info *);
290 static void sjlj_assign_call_site_values (rtx, struct sjlj_lp_info *);
291 static void sjlj_mark_call_sites (struct sjlj_lp_info *);
292 static void sjlj_emit_function_enter (rtx);
293 static void sjlj_emit_function_exit (void);
294 static void sjlj_emit_dispatch_table (rtx, struct sjlj_lp_info *);
295 static void sjlj_build_landing_pads (void);
297 static hashval_t ehl_hash (const void *);
298 static int ehl_eq (const void *, const void *);
299 static void add_ehl_entry (rtx, struct eh_region *);
300 static void remove_exception_handler_label (rtx);
301 static void remove_eh_handler (struct eh_region *);
302 static int for_each_eh_label_1 (void **, void *);
304 struct reachable_info;
306 /* The return value of reachable_next_level. */
307 enum reachable_code
309 /* The given exception is not processed by the given region. */
310 RNL_NOT_CAUGHT,
311 /* The given exception may need processing by the given region. */
312 RNL_MAYBE_CAUGHT,
313 /* The given exception is completely processed by the given region. */
314 RNL_CAUGHT,
315 /* The given exception is completely processed by the runtime. */
316 RNL_BLOCKED
319 static int check_handled (tree, tree);
320 static void add_reachable_handler (struct reachable_info *,
321 struct eh_region *, struct eh_region *);
322 static enum reachable_code reachable_next_level (struct eh_region *, tree,
323 struct reachable_info *);
325 static int action_record_eq (const void *, const void *);
326 static hashval_t action_record_hash (const void *);
327 static int add_action_record (htab_t, int, int);
328 static int collect_one_action_chain (htab_t, struct eh_region *);
329 static int add_call_site (rtx, int);
331 static void push_uleb128 (varray_type *, unsigned int);
332 static void push_sleb128 (varray_type *, int);
333 #ifndef HAVE_AS_LEB128
334 static int dw2_size_of_call_site_table (void);
335 static int sjlj_size_of_call_site_table (void);
336 #endif
337 static void dw2_output_call_site_table (void);
338 static void sjlj_output_call_site_table (void);
341 /* Routine to see if exception handling is turned on.
342 DO_WARN is nonzero if we want to inform the user that exception
343 handling is turned off.
345 This is used to ensure that -fexceptions has been specified if the
346 compiler tries to use any exception-specific functions. */
349 doing_eh (int do_warn)
351 if (! flag_exceptions)
353 static int warned = 0;
354 if (! warned && do_warn)
356 error ("exception handling disabled, use -fexceptions to enable");
357 warned = 1;
359 return 0;
361 return 1;
365 void
366 init_eh (void)
368 if (! flag_exceptions)
369 return;
371 type_to_runtime_map = htab_create_ggc (31, t2r_hash, t2r_eq, NULL);
373 /* Create the SjLj_Function_Context structure. This should match
374 the definition in unwind-sjlj.c. */
375 if (USING_SJLJ_EXCEPTIONS)
377 tree f_jbuf, f_per, f_lsda, f_prev, f_cs, f_data, tmp;
379 sjlj_fc_type_node = (*lang_hooks.types.make_type) (RECORD_TYPE);
381 f_prev = build_decl (FIELD_DECL, get_identifier ("__prev"),
382 build_pointer_type (sjlj_fc_type_node));
383 DECL_FIELD_CONTEXT (f_prev) = sjlj_fc_type_node;
385 f_cs = build_decl (FIELD_DECL, get_identifier ("__call_site"),
386 integer_type_node);
387 DECL_FIELD_CONTEXT (f_cs) = sjlj_fc_type_node;
389 tmp = build_index_type (build_int_2 (4 - 1, 0));
390 tmp = build_array_type ((*lang_hooks.types.type_for_mode) (word_mode, 1),
391 tmp);
392 f_data = build_decl (FIELD_DECL, get_identifier ("__data"), tmp);
393 DECL_FIELD_CONTEXT (f_data) = sjlj_fc_type_node;
395 f_per = build_decl (FIELD_DECL, get_identifier ("__personality"),
396 ptr_type_node);
397 DECL_FIELD_CONTEXT (f_per) = sjlj_fc_type_node;
399 f_lsda = build_decl (FIELD_DECL, get_identifier ("__lsda"),
400 ptr_type_node);
401 DECL_FIELD_CONTEXT (f_lsda) = sjlj_fc_type_node;
403 #ifdef DONT_USE_BUILTIN_SETJMP
404 #ifdef JMP_BUF_SIZE
405 tmp = build_int_2 (JMP_BUF_SIZE - 1, 0);
406 #else
407 /* Should be large enough for most systems, if it is not,
408 JMP_BUF_SIZE should be defined with the proper value. It will
409 also tend to be larger than necessary for most systems, a more
410 optimal port will define JMP_BUF_SIZE. */
411 tmp = build_int_2 (FIRST_PSEUDO_REGISTER + 2 - 1, 0);
412 #endif
413 #else
414 /* This is 2 for builtin_setjmp, plus whatever the target requires
415 via STACK_SAVEAREA_MODE (SAVE_NONLOCAL). */
416 tmp = build_int_2 ((GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL))
417 / GET_MODE_SIZE (Pmode)) + 2 - 1, 0);
418 #endif
419 tmp = build_index_type (tmp);
420 tmp = build_array_type (ptr_type_node, tmp);
421 f_jbuf = build_decl (FIELD_DECL, get_identifier ("__jbuf"), tmp);
422 #ifdef DONT_USE_BUILTIN_SETJMP
423 /* We don't know what the alignment requirements of the
424 runtime's jmp_buf has. Overestimate. */
425 DECL_ALIGN (f_jbuf) = BIGGEST_ALIGNMENT;
426 DECL_USER_ALIGN (f_jbuf) = 1;
427 #endif
428 DECL_FIELD_CONTEXT (f_jbuf) = sjlj_fc_type_node;
430 TYPE_FIELDS (sjlj_fc_type_node) = f_prev;
431 TREE_CHAIN (f_prev) = f_cs;
432 TREE_CHAIN (f_cs) = f_data;
433 TREE_CHAIN (f_data) = f_per;
434 TREE_CHAIN (f_per) = f_lsda;
435 TREE_CHAIN (f_lsda) = f_jbuf;
437 layout_type (sjlj_fc_type_node);
439 /* Cache the interesting field offsets so that we have
440 easy access from rtl. */
441 sjlj_fc_call_site_ofs
442 = (tree_low_cst (DECL_FIELD_OFFSET (f_cs), 1)
443 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_cs), 1) / BITS_PER_UNIT);
444 sjlj_fc_data_ofs
445 = (tree_low_cst (DECL_FIELD_OFFSET (f_data), 1)
446 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_data), 1) / BITS_PER_UNIT);
447 sjlj_fc_personality_ofs
448 = (tree_low_cst (DECL_FIELD_OFFSET (f_per), 1)
449 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_per), 1) / BITS_PER_UNIT);
450 sjlj_fc_lsda_ofs
451 = (tree_low_cst (DECL_FIELD_OFFSET (f_lsda), 1)
452 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_lsda), 1) / BITS_PER_UNIT);
453 sjlj_fc_jbuf_ofs
454 = (tree_low_cst (DECL_FIELD_OFFSET (f_jbuf), 1)
455 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_jbuf), 1) / BITS_PER_UNIT);
459 void
460 init_eh_for_function (void)
462 cfun->eh = ggc_alloc_cleared (sizeof (struct eh_status));
465 /* Start an exception handling region. All instructions emitted
466 after this point are considered to be part of the region until
467 expand_eh_region_end is invoked. */
469 void
470 expand_eh_region_start (void)
472 struct eh_region *new_region;
473 struct eh_region *cur_region;
474 rtx note;
476 if (! doing_eh (0))
477 return;
479 /* Insert a new blank region as a leaf in the tree. */
480 new_region = ggc_alloc_cleared (sizeof (*new_region));
481 cur_region = cfun->eh->cur_region;
482 new_region->outer = cur_region;
483 if (cur_region)
485 new_region->next_peer = cur_region->inner;
486 cur_region->inner = new_region;
488 else
490 new_region->next_peer = cfun->eh->region_tree;
491 cfun->eh->region_tree = new_region;
493 cfun->eh->cur_region = new_region;
495 /* Create a note marking the start of this region. */
496 new_region->region_number = ++cfun->eh->last_region_number;
497 note = emit_note (NOTE_INSN_EH_REGION_BEG);
498 NOTE_EH_HANDLER (note) = new_region->region_number;
501 /* Common code to end a region. Returns the region just ended. */
503 static struct eh_region *
504 expand_eh_region_end (void)
506 struct eh_region *cur_region = cfun->eh->cur_region;
507 rtx note;
509 /* Create a note marking the end of this region. */
510 note = emit_note (NOTE_INSN_EH_REGION_END);
511 NOTE_EH_HANDLER (note) = cur_region->region_number;
513 /* Pop. */
514 cfun->eh->cur_region = cur_region->outer;
516 return cur_region;
519 /* End an exception handling region for a cleanup. HANDLER is an
520 expression to expand for the cleanup. */
522 void
523 expand_eh_region_end_cleanup (tree handler)
525 struct eh_region *region;
526 tree protect_cleanup_actions;
527 rtx around_label;
528 rtx data_save[2];
530 if (! doing_eh (0))
531 return;
533 region = expand_eh_region_end ();
534 region->type = ERT_CLEANUP;
535 region->label = gen_label_rtx ();
536 region->u.cleanup.exp = handler;
537 region->u.cleanup.prev_try = cfun->eh->try_region;
539 around_label = gen_label_rtx ();
540 emit_jump (around_label);
542 emit_label (region->label);
544 if (flag_non_call_exceptions || region->may_contain_throw)
546 /* Give the language a chance to specify an action to be taken if an
547 exception is thrown that would propagate out of the HANDLER. */
548 protect_cleanup_actions
549 = (lang_protect_cleanup_actions
550 ? (*lang_protect_cleanup_actions) ()
551 : NULL_TREE);
553 if (protect_cleanup_actions)
554 expand_eh_region_start ();
556 /* In case this cleanup involves an inline destructor with a try block in
557 it, we need to save the EH return data registers around it. */
558 data_save[0] = gen_reg_rtx (ptr_mode);
559 emit_move_insn (data_save[0], get_exception_pointer (cfun));
560 data_save[1] = gen_reg_rtx (word_mode);
561 emit_move_insn (data_save[1], get_exception_filter (cfun));
563 expand_expr (handler, const0_rtx, VOIDmode, 0);
565 emit_move_insn (cfun->eh->exc_ptr, data_save[0]);
566 emit_move_insn (cfun->eh->filter, data_save[1]);
568 if (protect_cleanup_actions)
569 expand_eh_region_end_must_not_throw (protect_cleanup_actions);
571 /* We need any stack adjustment complete before the around_label. */
572 do_pending_stack_adjust ();
575 /* We delay the generation of the _Unwind_Resume until we generate
576 landing pads. We emit a marker here so as to get good control
577 flow data in the meantime. */
578 region->resume
579 = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
580 emit_barrier ();
582 emit_label (around_label);
585 /* End an exception handling region for a try block, and prepares
586 for subsequent calls to expand_start_catch. */
588 void
589 expand_start_all_catch (void)
591 struct eh_region *region;
593 if (! doing_eh (1))
594 return;
596 region = expand_eh_region_end ();
597 region->type = ERT_TRY;
598 region->u.try.prev_try = cfun->eh->try_region;
599 region->u.try.continue_label = gen_label_rtx ();
601 cfun->eh->try_region = region;
603 emit_jump (region->u.try.continue_label);
606 /* Begin a catch clause. TYPE is the type caught, a list of such types, or
607 null if this is a catch-all clause. Providing a type list enables to
608 associate the catch region with potentially several exception types, which
609 is useful e.g. for Ada. */
611 void
612 expand_start_catch (tree type_or_list)
614 struct eh_region *t, *c, *l;
615 tree type_list;
617 if (! doing_eh (0))
618 return;
620 type_list = type_or_list;
622 if (type_or_list)
624 /* Ensure to always end up with a type list to normalize further
625 processing, then register each type against the runtime types
626 map. */
627 tree type_node;
629 if (TREE_CODE (type_or_list) != TREE_LIST)
630 type_list = tree_cons (NULL_TREE, type_or_list, NULL_TREE);
632 type_node = type_list;
633 for (; type_node; type_node = TREE_CHAIN (type_node))
634 add_type_for_runtime (TREE_VALUE (type_node));
637 expand_eh_region_start ();
639 t = cfun->eh->try_region;
640 c = cfun->eh->cur_region;
641 c->type = ERT_CATCH;
642 c->u.catch.type_list = type_list;
643 c->label = gen_label_rtx ();
645 l = t->u.try.last_catch;
646 c->u.catch.prev_catch = l;
647 if (l)
648 l->u.catch.next_catch = c;
649 else
650 t->u.try.catch = c;
651 t->u.try.last_catch = c;
653 emit_label (c->label);
656 /* End a catch clause. Control will resume after the try/catch block. */
658 void
659 expand_end_catch (void)
661 struct eh_region *try_region;
663 if (! doing_eh (0))
664 return;
666 expand_eh_region_end ();
667 try_region = cfun->eh->try_region;
669 emit_jump (try_region->u.try.continue_label);
672 /* End a sequence of catch handlers for a try block. */
674 void
675 expand_end_all_catch (void)
677 struct eh_region *try_region;
679 if (! doing_eh (0))
680 return;
682 try_region = cfun->eh->try_region;
683 cfun->eh->try_region = try_region->u.try.prev_try;
685 emit_label (try_region->u.try.continue_label);
688 /* End an exception region for an exception type filter. ALLOWED is a
689 TREE_LIST of types to be matched by the runtime. FAILURE is an
690 expression to invoke if a mismatch occurs.
692 ??? We could use these semantics for calls to rethrow, too; if we can
693 see the surrounding catch clause, we know that the exception we're
694 rethrowing satisfies the "filter" of the catch type. */
696 void
697 expand_eh_region_end_allowed (tree allowed, tree failure)
699 struct eh_region *region;
700 rtx around_label;
702 if (! doing_eh (0))
703 return;
705 region = expand_eh_region_end ();
706 region->type = ERT_ALLOWED_EXCEPTIONS;
707 region->u.allowed.type_list = allowed;
708 region->label = gen_label_rtx ();
710 for (; allowed ; allowed = TREE_CHAIN (allowed))
711 add_type_for_runtime (TREE_VALUE (allowed));
713 /* We must emit the call to FAILURE here, so that if this function
714 throws a different exception, that it will be processed by the
715 correct region. */
717 around_label = gen_label_rtx ();
718 emit_jump (around_label);
720 emit_label (region->label);
721 expand_expr (failure, const0_rtx, VOIDmode, EXPAND_NORMAL);
722 /* We must adjust the stack before we reach the AROUND_LABEL because
723 the call to FAILURE does not occur on all paths to the
724 AROUND_LABEL. */
725 do_pending_stack_adjust ();
727 emit_label (around_label);
730 /* End an exception region for a must-not-throw filter. FAILURE is an
731 expression invoke if an uncaught exception propagates this far.
733 This is conceptually identical to expand_eh_region_end_allowed with
734 an empty allowed list (if you passed "std::terminate" instead of
735 "__cxa_call_unexpected"), but they are represented differently in
736 the C++ LSDA. */
738 void
739 expand_eh_region_end_must_not_throw (tree failure)
741 struct eh_region *region;
742 rtx around_label;
744 if (! doing_eh (0))
745 return;
747 region = expand_eh_region_end ();
748 region->type = ERT_MUST_NOT_THROW;
749 region->label = gen_label_rtx ();
751 /* We must emit the call to FAILURE here, so that if this function
752 throws a different exception, that it will be processed by the
753 correct region. */
755 around_label = gen_label_rtx ();
756 emit_jump (around_label);
758 emit_label (region->label);
759 expand_expr (failure, const0_rtx, VOIDmode, EXPAND_NORMAL);
761 emit_label (around_label);
764 /* End an exception region for a throw. No handling goes on here,
765 but it's the easiest way for the front-end to indicate what type
766 is being thrown. */
768 void
769 expand_eh_region_end_throw (tree type)
771 struct eh_region *region;
773 if (! doing_eh (0))
774 return;
776 region = expand_eh_region_end ();
777 region->type = ERT_THROW;
778 region->u.throw.type = type;
781 /* End a fixup region. Within this region the cleanups for the immediately
782 enclosing region are _not_ run. This is used for goto cleanup to avoid
783 destroying an object twice.
785 This would be an extraordinarily simple prospect, were it not for the
786 fact that we don't actually know what the immediately enclosing region
787 is. This surprising fact is because expand_cleanups is currently
788 generating a sequence that it will insert somewhere else. We collect
789 the proper notion of "enclosing" in convert_from_eh_region_ranges. */
791 void
792 expand_eh_region_end_fixup (tree handler)
794 struct eh_region *fixup;
796 if (! doing_eh (0))
797 return;
799 fixup = expand_eh_region_end ();
800 fixup->type = ERT_FIXUP;
801 fixup->u.fixup.cleanup_exp = handler;
804 /* Note that the current EH region (if any) may contain a throw, or a
805 call to a function which itself may contain a throw. */
807 void
808 note_eh_region_may_contain_throw (void)
810 struct eh_region *region;
812 region = cfun->eh->cur_region;
813 while (region && !region->may_contain_throw)
815 region->may_contain_throw = 1;
816 region = region->outer;
820 /* Return an rtl expression for a pointer to the exception object
821 within a handler. */
824 get_exception_pointer (struct function *fun)
826 rtx exc_ptr = fun->eh->exc_ptr;
827 if (fun == cfun && ! exc_ptr)
829 exc_ptr = gen_reg_rtx (ptr_mode);
830 fun->eh->exc_ptr = exc_ptr;
832 return exc_ptr;
835 /* Return an rtl expression for the exception dispatch filter
836 within a handler. */
838 static rtx
839 get_exception_filter (struct function *fun)
841 rtx filter = fun->eh->filter;
842 if (fun == cfun && ! filter)
844 filter = gen_reg_rtx (word_mode);
845 fun->eh->filter = filter;
847 return filter;
850 /* This section is for the exception handling specific optimization pass. */
852 /* Random access the exception region tree. It's just as simple to
853 collect the regions this way as in expand_eh_region_start, but
854 without having to realloc memory. */
856 static void
857 collect_eh_region_array (void)
859 struct eh_region **array, *i;
861 i = cfun->eh->region_tree;
862 if (! i)
863 return;
865 array = ggc_alloc_cleared ((cfun->eh->last_region_number + 1)
866 * sizeof (*array));
867 cfun->eh->region_array = array;
869 while (1)
871 array[i->region_number] = i;
873 /* If there are sub-regions, process them. */
874 if (i->inner)
875 i = i->inner;
876 /* If there are peers, process them. */
877 else if (i->next_peer)
878 i = i->next_peer;
879 /* Otherwise, step back up the tree to the next peer. */
880 else
882 do {
883 i = i->outer;
884 if (i == NULL)
885 return;
886 } while (i->next_peer == NULL);
887 i = i->next_peer;
892 static void
893 resolve_fixup_regions (void)
895 int i, j, n = cfun->eh->last_region_number;
897 for (i = 1; i <= n; ++i)
899 struct eh_region *fixup = cfun->eh->region_array[i];
900 struct eh_region *cleanup = 0;
902 if (! fixup || fixup->type != ERT_FIXUP)
903 continue;
905 for (j = 1; j <= n; ++j)
907 cleanup = cfun->eh->region_array[j];
908 if (cleanup && cleanup->type == ERT_CLEANUP
909 && cleanup->u.cleanup.exp == fixup->u.fixup.cleanup_exp)
910 break;
912 if (j > n)
913 abort ();
915 fixup->u.fixup.real_region = cleanup->outer;
919 /* Now that we've discovered what region actually encloses a fixup,
920 we can shuffle pointers and remove them from the tree. */
922 static void
923 remove_fixup_regions (void)
925 int i;
926 rtx insn, note;
927 struct eh_region *fixup;
929 /* Walk the insn chain and adjust the REG_EH_REGION numbers
930 for instructions referencing fixup regions. This is only
931 strictly necessary for fixup regions with no parent, but
932 doesn't hurt to do it for all regions. */
933 for (insn = get_insns(); insn ; insn = NEXT_INSN (insn))
934 if (INSN_P (insn)
935 && (note = find_reg_note (insn, REG_EH_REGION, NULL))
936 && INTVAL (XEXP (note, 0)) > 0
937 && (fixup = cfun->eh->region_array[INTVAL (XEXP (note, 0))])
938 && fixup->type == ERT_FIXUP)
940 if (fixup->u.fixup.real_region)
941 XEXP (note, 0) = GEN_INT (fixup->u.fixup.real_region->region_number);
942 else
943 remove_note (insn, note);
946 /* Remove the fixup regions from the tree. */
947 for (i = cfun->eh->last_region_number; i > 0; --i)
949 fixup = cfun->eh->region_array[i];
950 if (! fixup)
951 continue;
953 /* Allow GC to maybe free some memory. */
954 if (fixup->type == ERT_CLEANUP)
955 fixup->u.cleanup.exp = NULL_TREE;
957 if (fixup->type != ERT_FIXUP)
958 continue;
960 if (fixup->inner)
962 struct eh_region *parent, *p, **pp;
964 parent = fixup->u.fixup.real_region;
966 /* Fix up the children's parent pointers; find the end of
967 the list. */
968 for (p = fixup->inner; ; p = p->next_peer)
970 p->outer = parent;
971 if (! p->next_peer)
972 break;
975 /* In the tree of cleanups, only outer-inner ordering matters.
976 So link the children back in anywhere at the correct level. */
977 if (parent)
978 pp = &parent->inner;
979 else
980 pp = &cfun->eh->region_tree;
981 p->next_peer = *pp;
982 *pp = fixup->inner;
983 fixup->inner = NULL;
986 remove_eh_handler (fixup);
990 /* Remove all regions whose labels are not reachable from insns. */
992 static void
993 remove_unreachable_regions (rtx insns)
995 int i, *uid_region_num;
996 bool *reachable;
997 struct eh_region *r;
998 rtx insn;
1000 uid_region_num = xcalloc (get_max_uid (), sizeof(int));
1001 reachable = xcalloc (cfun->eh->last_region_number + 1, sizeof(bool));
1003 for (i = cfun->eh->last_region_number; i > 0; --i)
1005 r = cfun->eh->region_array[i];
1006 if (!r || r->region_number != i)
1007 continue;
1009 if (r->resume)
1011 if (uid_region_num[INSN_UID (r->resume)])
1012 abort ();
1013 uid_region_num[INSN_UID (r->resume)] = i;
1015 if (r->label)
1017 if (uid_region_num[INSN_UID (r->label)])
1018 abort ();
1019 uid_region_num[INSN_UID (r->label)] = i;
1021 if (r->type == ERT_TRY && r->u.try.continue_label)
1023 if (uid_region_num[INSN_UID (r->u.try.continue_label)])
1024 abort ();
1025 uid_region_num[INSN_UID (r->u.try.continue_label)] = i;
1029 for (insn = insns; insn; insn = NEXT_INSN (insn))
1030 reachable[uid_region_num[INSN_UID (insn)]] = true;
1032 for (i = cfun->eh->last_region_number; i > 0; --i)
1034 r = cfun->eh->region_array[i];
1035 if (r && r->region_number == i && !reachable[i])
1037 /* Don't remove ERT_THROW regions if their outer region
1038 is reachable. */
1039 if (r->type == ERT_THROW
1040 && r->outer
1041 && reachable[r->outer->region_number])
1042 continue;
1044 remove_eh_handler (r);
1048 free (reachable);
1049 free (uid_region_num);
1052 /* Turn NOTE_INSN_EH_REGION notes into REG_EH_REGION notes for each
1053 can_throw instruction in the region. */
1055 static void
1056 convert_from_eh_region_ranges_1 (rtx *pinsns, int *orig_sp, int cur)
1058 int *sp = orig_sp;
1059 rtx insn, next;
1061 for (insn = *pinsns; insn ; insn = next)
1063 next = NEXT_INSN (insn);
1064 if (GET_CODE (insn) == NOTE)
1066 int kind = NOTE_LINE_NUMBER (insn);
1067 if (kind == NOTE_INSN_EH_REGION_BEG
1068 || kind == NOTE_INSN_EH_REGION_END)
1070 if (kind == NOTE_INSN_EH_REGION_BEG)
1072 struct eh_region *r;
1074 *sp++ = cur;
1075 cur = NOTE_EH_HANDLER (insn);
1077 r = cfun->eh->region_array[cur];
1078 if (r->type == ERT_FIXUP)
1080 r = r->u.fixup.real_region;
1081 cur = r ? r->region_number : 0;
1083 else if (r->type == ERT_CATCH)
1085 r = r->outer;
1086 cur = r ? r->region_number : 0;
1089 else
1090 cur = *--sp;
1092 /* Removing the first insn of a CALL_PLACEHOLDER sequence
1093 requires extra care to adjust sequence start. */
1094 if (insn == *pinsns)
1095 *pinsns = next;
1096 remove_insn (insn);
1097 continue;
1100 else if (INSN_P (insn))
1102 if (cur > 0
1103 && ! find_reg_note (insn, REG_EH_REGION, NULL_RTX)
1104 /* Calls can always potentially throw exceptions, unless
1105 they have a REG_EH_REGION note with a value of 0 or less.
1106 Which should be the only possible kind so far. */
1107 && (GET_CODE (insn) == CALL_INSN
1108 /* If we wanted exceptions for non-call insns, then
1109 any may_trap_p instruction could throw. */
1110 || (flag_non_call_exceptions
1111 && GET_CODE (PATTERN (insn)) != CLOBBER
1112 && GET_CODE (PATTERN (insn)) != USE
1113 && may_trap_p (PATTERN (insn)))))
1115 REG_NOTES (insn) = alloc_EXPR_LIST (REG_EH_REGION, GEN_INT (cur),
1116 REG_NOTES (insn));
1119 if (GET_CODE (insn) == CALL_INSN
1120 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
1122 convert_from_eh_region_ranges_1 (&XEXP (PATTERN (insn), 0),
1123 sp, cur);
1124 convert_from_eh_region_ranges_1 (&XEXP (PATTERN (insn), 1),
1125 sp, cur);
1126 convert_from_eh_region_ranges_1 (&XEXP (PATTERN (insn), 2),
1127 sp, cur);
1132 if (sp != orig_sp)
1133 abort ();
1136 void
1137 convert_from_eh_region_ranges (void)
1139 int *stack;
1140 rtx insns;
1142 collect_eh_region_array ();
1143 resolve_fixup_regions ();
1145 stack = xmalloc (sizeof (int) * (cfun->eh->last_region_number + 1));
1146 insns = get_insns ();
1147 convert_from_eh_region_ranges_1 (&insns, stack, 0);
1148 free (stack);
1150 remove_fixup_regions ();
1151 remove_unreachable_regions (insns);
1154 static void
1155 add_ehl_entry (rtx label, struct eh_region *region)
1157 struct ehl_map_entry **slot, *entry;
1159 LABEL_PRESERVE_P (label) = 1;
1161 entry = ggc_alloc (sizeof (*entry));
1162 entry->label = label;
1163 entry->region = region;
1165 slot = (struct ehl_map_entry **)
1166 htab_find_slot (cfun->eh->exception_handler_label_map, entry, INSERT);
1168 /* Before landing pad creation, each exception handler has its own
1169 label. After landing pad creation, the exception handlers may
1170 share landing pads. This is ok, since maybe_remove_eh_handler
1171 only requires the 1-1 mapping before landing pad creation. */
1172 if (*slot && !cfun->eh->built_landing_pads)
1173 abort ();
1175 *slot = entry;
1178 void
1179 find_exception_handler_labels (void)
1181 int i;
1183 if (cfun->eh->exception_handler_label_map)
1184 htab_empty (cfun->eh->exception_handler_label_map);
1185 else
1187 /* ??? The expansion factor here (3/2) must be greater than the htab
1188 occupancy factor (4/3) to avoid unnecessary resizing. */
1189 cfun->eh->exception_handler_label_map
1190 = htab_create_ggc (cfun->eh->last_region_number * 3 / 2,
1191 ehl_hash, ehl_eq, NULL);
1194 if (cfun->eh->region_tree == NULL)
1195 return;
1197 for (i = cfun->eh->last_region_number; i > 0; --i)
1199 struct eh_region *region = cfun->eh->region_array[i];
1200 rtx lab;
1202 if (! region || region->region_number != i)
1203 continue;
1204 if (cfun->eh->built_landing_pads)
1205 lab = region->landing_pad;
1206 else
1207 lab = region->label;
1209 if (lab)
1210 add_ehl_entry (lab, region);
1213 /* For sjlj exceptions, need the return label to remain live until
1214 after landing pad generation. */
1215 if (USING_SJLJ_EXCEPTIONS && ! cfun->eh->built_landing_pads)
1216 add_ehl_entry (return_label, NULL);
1219 bool
1220 current_function_has_exception_handlers (void)
1222 int i;
1224 for (i = cfun->eh->last_region_number; i > 0; --i)
1226 struct eh_region *region = cfun->eh->region_array[i];
1228 if (! region || region->region_number != i)
1229 continue;
1230 if (region->type != ERT_THROW)
1231 return true;
1234 return false;
1237 static struct eh_region *
1238 duplicate_eh_region_1 (struct eh_region *o, struct inline_remap *map)
1240 struct eh_region *n = ggc_alloc_cleared (sizeof (struct eh_region));
1242 n->region_number = o->region_number + cfun->eh->last_region_number;
1243 n->type = o->type;
1245 switch (n->type)
1247 case ERT_CLEANUP:
1248 case ERT_MUST_NOT_THROW:
1249 break;
1251 case ERT_TRY:
1252 if (o->u.try.continue_label)
1253 n->u.try.continue_label
1254 = get_label_from_map (map,
1255 CODE_LABEL_NUMBER (o->u.try.continue_label));
1256 break;
1258 case ERT_CATCH:
1259 n->u.catch.type_list = o->u.catch.type_list;
1260 break;
1262 case ERT_ALLOWED_EXCEPTIONS:
1263 n->u.allowed.type_list = o->u.allowed.type_list;
1264 break;
1266 case ERT_THROW:
1267 n->u.throw.type = o->u.throw.type;
1269 default:
1270 abort ();
1273 if (o->label)
1274 n->label = get_label_from_map (map, CODE_LABEL_NUMBER (o->label));
1275 if (o->resume)
1277 n->resume = map->insn_map[INSN_UID (o->resume)];
1278 if (n->resume == NULL)
1279 abort ();
1282 return n;
1285 static void
1286 duplicate_eh_region_2 (struct eh_region *o, struct eh_region **n_array)
1288 struct eh_region *n = n_array[o->region_number];
1290 switch (n->type)
1292 case ERT_TRY:
1293 n->u.try.catch = n_array[o->u.try.catch->region_number];
1294 n->u.try.last_catch = n_array[o->u.try.last_catch->region_number];
1295 break;
1297 case ERT_CATCH:
1298 if (o->u.catch.next_catch)
1299 n->u.catch.next_catch = n_array[o->u.catch.next_catch->region_number];
1300 if (o->u.catch.prev_catch)
1301 n->u.catch.prev_catch = n_array[o->u.catch.prev_catch->region_number];
1302 break;
1304 default:
1305 break;
1308 if (o->outer)
1309 n->outer = n_array[o->outer->region_number];
1310 if (o->inner)
1311 n->inner = n_array[o->inner->region_number];
1312 if (o->next_peer)
1313 n->next_peer = n_array[o->next_peer->region_number];
1317 duplicate_eh_regions (struct function *ifun, struct inline_remap *map)
1319 int ifun_last_region_number = ifun->eh->last_region_number;
1320 struct eh_region **n_array, *root, *cur;
1321 int i;
1323 if (ifun_last_region_number == 0)
1324 return 0;
1326 n_array = xcalloc (ifun_last_region_number + 1, sizeof (*n_array));
1328 for (i = 1; i <= ifun_last_region_number; ++i)
1330 cur = ifun->eh->region_array[i];
1331 if (!cur || cur->region_number != i)
1332 continue;
1333 n_array[i] = duplicate_eh_region_1 (cur, map);
1335 for (i = 1; i <= ifun_last_region_number; ++i)
1337 cur = ifun->eh->region_array[i];
1338 if (!cur || cur->region_number != i)
1339 continue;
1340 duplicate_eh_region_2 (cur, n_array);
1343 root = n_array[ifun->eh->region_tree->region_number];
1344 cur = cfun->eh->cur_region;
1345 if (cur)
1347 struct eh_region *p = cur->inner;
1348 if (p)
1350 while (p->next_peer)
1351 p = p->next_peer;
1352 p->next_peer = root;
1354 else
1355 cur->inner = root;
1357 for (i = 1; i <= ifun_last_region_number; ++i)
1358 if (n_array[i] && n_array[i]->outer == NULL)
1359 n_array[i]->outer = cur;
1361 else
1363 struct eh_region *p = cfun->eh->region_tree;
1364 if (p)
1366 while (p->next_peer)
1367 p = p->next_peer;
1368 p->next_peer = root;
1370 else
1371 cfun->eh->region_tree = root;
1374 free (n_array);
1376 i = cfun->eh->last_region_number;
1377 cfun->eh->last_region_number = i + ifun_last_region_number;
1378 return i;
1382 static int
1383 t2r_eq (const void *pentry, const void *pdata)
1385 tree entry = (tree) pentry;
1386 tree data = (tree) pdata;
1388 return TREE_PURPOSE (entry) == data;
1391 static hashval_t
1392 t2r_hash (const void *pentry)
1394 tree entry = (tree) pentry;
1395 return TYPE_HASH (TREE_PURPOSE (entry));
1398 static void
1399 add_type_for_runtime (tree type)
1401 tree *slot;
1403 slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
1404 TYPE_HASH (type), INSERT);
1405 if (*slot == NULL)
1407 tree runtime = (*lang_eh_runtime_type) (type);
1408 *slot = tree_cons (type, runtime, NULL_TREE);
1412 static tree
1413 lookup_type_for_runtime (tree type)
1415 tree *slot;
1417 slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
1418 TYPE_HASH (type), NO_INSERT);
1420 /* We should have always inserted the data earlier. */
1421 return TREE_VALUE (*slot);
1425 /* Represent an entry in @TTypes for either catch actions
1426 or exception filter actions. */
1427 struct ttypes_filter GTY(())
1429 tree t;
1430 int filter;
1433 /* Compare ENTRY (a ttypes_filter entry in the hash table) with DATA
1434 (a tree) for a @TTypes type node we are thinking about adding. */
1436 static int
1437 ttypes_filter_eq (const void *pentry, const void *pdata)
1439 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1440 tree data = (tree) pdata;
1442 return entry->t == data;
1445 static hashval_t
1446 ttypes_filter_hash (const void *pentry)
1448 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1449 return TYPE_HASH (entry->t);
1452 /* Compare ENTRY with DATA (both struct ttypes_filter) for a @TTypes
1453 exception specification list we are thinking about adding. */
1454 /* ??? Currently we use the type lists in the order given. Someone
1455 should put these in some canonical order. */
1457 static int
1458 ehspec_filter_eq (const void *pentry, const void *pdata)
1460 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1461 const struct ttypes_filter *data = (const struct ttypes_filter *) pdata;
1463 return type_list_equal (entry->t, data->t);
1466 /* Hash function for exception specification lists. */
1468 static hashval_t
1469 ehspec_filter_hash (const void *pentry)
1471 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1472 hashval_t h = 0;
1473 tree list;
1475 for (list = entry->t; list ; list = TREE_CHAIN (list))
1476 h = (h << 5) + (h >> 27) + TYPE_HASH (TREE_VALUE (list));
1477 return h;
1480 /* Add TYPE to cfun->eh->ttype_data, using TYPES_HASH to speed
1481 up the search. Return the filter value to be used. */
1483 static int
1484 add_ttypes_entry (htab_t ttypes_hash, tree type)
1486 struct ttypes_filter **slot, *n;
1488 slot = (struct ttypes_filter **)
1489 htab_find_slot_with_hash (ttypes_hash, type, TYPE_HASH (type), INSERT);
1491 if ((n = *slot) == NULL)
1493 /* Filter value is a 1 based table index. */
1495 n = xmalloc (sizeof (*n));
1496 n->t = type;
1497 n->filter = VARRAY_ACTIVE_SIZE (cfun->eh->ttype_data) + 1;
1498 *slot = n;
1500 VARRAY_PUSH_TREE (cfun->eh->ttype_data, type);
1503 return n->filter;
1506 /* Add LIST to cfun->eh->ehspec_data, using EHSPEC_HASH and TYPES_HASH
1507 to speed up the search. Return the filter value to be used. */
1509 static int
1510 add_ehspec_entry (htab_t ehspec_hash, htab_t ttypes_hash, tree list)
1512 struct ttypes_filter **slot, *n;
1513 struct ttypes_filter dummy;
1515 dummy.t = list;
1516 slot = (struct ttypes_filter **)
1517 htab_find_slot (ehspec_hash, &dummy, INSERT);
1519 if ((n = *slot) == NULL)
1521 /* Filter value is a -1 based byte index into a uleb128 buffer. */
1523 n = xmalloc (sizeof (*n));
1524 n->t = list;
1525 n->filter = -(VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data) + 1);
1526 *slot = n;
1528 /* Look up each type in the list and encode its filter
1529 value as a uleb128. Terminate the list with 0. */
1530 for (; list ; list = TREE_CHAIN (list))
1531 push_uleb128 (&cfun->eh->ehspec_data,
1532 add_ttypes_entry (ttypes_hash, TREE_VALUE (list)));
1533 VARRAY_PUSH_UCHAR (cfun->eh->ehspec_data, 0);
1536 return n->filter;
1539 /* Generate the action filter values to be used for CATCH and
1540 ALLOWED_EXCEPTIONS regions. When using dwarf2 exception regions,
1541 we use lots of landing pads, and so every type or list can share
1542 the same filter value, which saves table space. */
1544 static void
1545 assign_filter_values (void)
1547 int i;
1548 htab_t ttypes, ehspec;
1550 VARRAY_TREE_INIT (cfun->eh->ttype_data, 16, "ttype_data");
1551 VARRAY_UCHAR_INIT (cfun->eh->ehspec_data, 64, "ehspec_data");
1553 ttypes = htab_create (31, ttypes_filter_hash, ttypes_filter_eq, free);
1554 ehspec = htab_create (31, ehspec_filter_hash, ehspec_filter_eq, free);
1556 for (i = cfun->eh->last_region_number; i > 0; --i)
1558 struct eh_region *r = cfun->eh->region_array[i];
1560 /* Mind we don't process a region more than once. */
1561 if (!r || r->region_number != i)
1562 continue;
1564 switch (r->type)
1566 case ERT_CATCH:
1567 /* Whatever type_list is (NULL or true list), we build a list
1568 of filters for the region. */
1569 r->u.catch.filter_list = NULL_TREE;
1571 if (r->u.catch.type_list != NULL)
1573 /* Get a filter value for each of the types caught and store
1574 them in the region's dedicated list. */
1575 tree tp_node = r->u.catch.type_list;
1577 for (;tp_node; tp_node = TREE_CHAIN (tp_node))
1579 int flt = add_ttypes_entry (ttypes, TREE_VALUE (tp_node));
1580 tree flt_node = build_int_2 (flt, 0);
1582 r->u.catch.filter_list
1583 = tree_cons (NULL_TREE, flt_node, r->u.catch.filter_list);
1586 else
1588 /* Get a filter value for the NULL list also since it will need
1589 an action record anyway. */
1590 int flt = add_ttypes_entry (ttypes, NULL);
1591 tree flt_node = build_int_2 (flt, 0);
1593 r->u.catch.filter_list
1594 = tree_cons (NULL_TREE, flt_node, r->u.catch.filter_list);
1597 break;
1599 case ERT_ALLOWED_EXCEPTIONS:
1600 r->u.allowed.filter
1601 = add_ehspec_entry (ehspec, ttypes, r->u.allowed.type_list);
1602 break;
1604 default:
1605 break;
1609 htab_delete (ttypes);
1610 htab_delete (ehspec);
1613 /* Generate the code to actually handle exceptions, which will follow the
1614 landing pads. */
1616 static void
1617 build_post_landing_pads (void)
1619 int i;
1621 for (i = cfun->eh->last_region_number; i > 0; --i)
1623 struct eh_region *region = cfun->eh->region_array[i];
1624 rtx seq;
1626 /* Mind we don't process a region more than once. */
1627 if (!region || region->region_number != i)
1628 continue;
1630 switch (region->type)
1632 case ERT_TRY:
1633 /* ??? Collect the set of all non-overlapping catch handlers
1634 all the way up the chain until blocked by a cleanup. */
1635 /* ??? Outer try regions can share landing pads with inner
1636 try regions if the types are completely non-overlapping,
1637 and there are no intervening cleanups. */
1639 region->post_landing_pad = gen_label_rtx ();
1641 start_sequence ();
1643 emit_label (region->post_landing_pad);
1645 /* ??? It is mighty inconvenient to call back into the
1646 switch statement generation code in expand_end_case.
1647 Rapid prototyping sez a sequence of ifs. */
1649 struct eh_region *c;
1650 for (c = region->u.try.catch; c ; c = c->u.catch.next_catch)
1652 if (c->u.catch.type_list == NULL)
1653 emit_jump (c->label);
1654 else
1656 /* Need for one cmp/jump per type caught. Each type
1657 list entry has a matching entry in the filter list
1658 (see assign_filter_values). */
1659 tree tp_node = c->u.catch.type_list;
1660 tree flt_node = c->u.catch.filter_list;
1662 for (; tp_node; )
1664 emit_cmp_and_jump_insns
1665 (cfun->eh->filter,
1666 GEN_INT (tree_low_cst (TREE_VALUE (flt_node), 0)),
1667 EQ, NULL_RTX, word_mode, 0, c->label);
1669 tp_node = TREE_CHAIN (tp_node);
1670 flt_node = TREE_CHAIN (flt_node);
1676 /* We delay the generation of the _Unwind_Resume until we generate
1677 landing pads. We emit a marker here so as to get good control
1678 flow data in the meantime. */
1679 region->resume
1680 = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
1681 emit_barrier ();
1683 seq = get_insns ();
1684 end_sequence ();
1686 emit_insn_before (seq, region->u.try.catch->label);
1687 break;
1689 case ERT_ALLOWED_EXCEPTIONS:
1690 region->post_landing_pad = gen_label_rtx ();
1692 start_sequence ();
1694 emit_label (region->post_landing_pad);
1696 emit_cmp_and_jump_insns (cfun->eh->filter,
1697 GEN_INT (region->u.allowed.filter),
1698 EQ, NULL_RTX, word_mode, 0, region->label);
1700 /* We delay the generation of the _Unwind_Resume until we generate
1701 landing pads. We emit a marker here so as to get good control
1702 flow data in the meantime. */
1703 region->resume
1704 = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
1705 emit_barrier ();
1707 seq = get_insns ();
1708 end_sequence ();
1710 emit_insn_before (seq, region->label);
1711 break;
1713 case ERT_CLEANUP:
1714 case ERT_MUST_NOT_THROW:
1715 region->post_landing_pad = region->label;
1716 break;
1718 case ERT_CATCH:
1719 case ERT_THROW:
1720 /* Nothing to do. */
1721 break;
1723 default:
1724 abort ();
1729 /* Replace RESX patterns with jumps to the next handler if any, or calls to
1730 _Unwind_Resume otherwise. */
1732 static void
1733 connect_post_landing_pads (void)
1735 int i;
1737 for (i = cfun->eh->last_region_number; i > 0; --i)
1739 struct eh_region *region = cfun->eh->region_array[i];
1740 struct eh_region *outer;
1741 rtx seq;
1743 /* Mind we don't process a region more than once. */
1744 if (!region || region->region_number != i)
1745 continue;
1747 /* If there is no RESX, or it has been deleted by flow, there's
1748 nothing to fix up. */
1749 if (! region->resume || INSN_DELETED_P (region->resume))
1750 continue;
1752 /* Search for another landing pad in this function. */
1753 for (outer = region->outer; outer ; outer = outer->outer)
1754 if (outer->post_landing_pad)
1755 break;
1757 start_sequence ();
1759 if (outer)
1760 emit_jump (outer->post_landing_pad);
1761 else
1762 emit_library_call (unwind_resume_libfunc, LCT_THROW,
1763 VOIDmode, 1, cfun->eh->exc_ptr, ptr_mode);
1765 seq = get_insns ();
1766 end_sequence ();
1767 emit_insn_before (seq, region->resume);
1768 delete_insn (region->resume);
1773 static void
1774 dw2_build_landing_pads (void)
1776 int i;
1777 unsigned int j;
1779 for (i = cfun->eh->last_region_number; i > 0; --i)
1781 struct eh_region *region = cfun->eh->region_array[i];
1782 rtx seq;
1783 bool clobbers_hard_regs = false;
1785 /* Mind we don't process a region more than once. */
1786 if (!region || region->region_number != i)
1787 continue;
1789 if (region->type != ERT_CLEANUP
1790 && region->type != ERT_TRY
1791 && region->type != ERT_ALLOWED_EXCEPTIONS)
1792 continue;
1794 start_sequence ();
1796 region->landing_pad = gen_label_rtx ();
1797 emit_label (region->landing_pad);
1799 #ifdef HAVE_exception_receiver
1800 if (HAVE_exception_receiver)
1801 emit_insn (gen_exception_receiver ());
1802 else
1803 #endif
1804 #ifdef HAVE_nonlocal_goto_receiver
1805 if (HAVE_nonlocal_goto_receiver)
1806 emit_insn (gen_nonlocal_goto_receiver ());
1807 else
1808 #endif
1809 { /* Nothing */ }
1811 /* If the eh_return data registers are call-saved, then we
1812 won't have considered them clobbered from the call that
1813 threw. Kill them now. */
1814 for (j = 0; ; ++j)
1816 unsigned r = EH_RETURN_DATA_REGNO (j);
1817 if (r == INVALID_REGNUM)
1818 break;
1819 if (! call_used_regs[r])
1821 emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, r)));
1822 clobbers_hard_regs = true;
1826 if (clobbers_hard_regs)
1828 /* @@@ This is a kludge. Not all machine descriptions define a
1829 blockage insn, but we must not allow the code we just generated
1830 to be reordered by scheduling. So emit an ASM_INPUT to act as
1831 blockage insn. */
1832 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
1835 emit_move_insn (cfun->eh->exc_ptr,
1836 gen_rtx_REG (ptr_mode, EH_RETURN_DATA_REGNO (0)));
1837 emit_move_insn (cfun->eh->filter,
1838 gen_rtx_REG (word_mode, EH_RETURN_DATA_REGNO (1)));
1840 seq = get_insns ();
1841 end_sequence ();
1843 emit_insn_before (seq, region->post_landing_pad);
1848 struct sjlj_lp_info
1850 int directly_reachable;
1851 int action_index;
1852 int dispatch_index;
1853 int call_site_index;
1856 static bool
1857 sjlj_find_directly_reachable_regions (struct sjlj_lp_info *lp_info)
1859 rtx insn;
1860 bool found_one = false;
1862 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
1864 struct eh_region *region;
1865 enum reachable_code rc;
1866 tree type_thrown;
1867 rtx note;
1869 if (! INSN_P (insn))
1870 continue;
1872 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1873 if (!note || INTVAL (XEXP (note, 0)) <= 0)
1874 continue;
1876 region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
1878 type_thrown = NULL_TREE;
1879 if (region->type == ERT_THROW)
1881 type_thrown = region->u.throw.type;
1882 region = region->outer;
1885 /* Find the first containing region that might handle the exception.
1886 That's the landing pad to which we will transfer control. */
1887 rc = RNL_NOT_CAUGHT;
1888 for (; region; region = region->outer)
1890 rc = reachable_next_level (region, type_thrown, 0);
1891 if (rc != RNL_NOT_CAUGHT)
1892 break;
1894 if (rc == RNL_MAYBE_CAUGHT || rc == RNL_CAUGHT)
1896 lp_info[region->region_number].directly_reachable = 1;
1897 found_one = true;
1901 return found_one;
1904 static void
1905 sjlj_assign_call_site_values (rtx dispatch_label, struct sjlj_lp_info *lp_info)
1907 htab_t ar_hash;
1908 int i, index;
1910 /* First task: build the action table. */
1912 VARRAY_UCHAR_INIT (cfun->eh->action_record_data, 64, "action_record_data");
1913 ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
1915 for (i = cfun->eh->last_region_number; i > 0; --i)
1916 if (lp_info[i].directly_reachable)
1918 struct eh_region *r = cfun->eh->region_array[i];
1919 r->landing_pad = dispatch_label;
1920 lp_info[i].action_index = collect_one_action_chain (ar_hash, r);
1921 if (lp_info[i].action_index != -1)
1922 cfun->uses_eh_lsda = 1;
1925 htab_delete (ar_hash);
1927 /* Next: assign dispatch values. In dwarf2 terms, this would be the
1928 landing pad label for the region. For sjlj though, there is one
1929 common landing pad from which we dispatch to the post-landing pads.
1931 A region receives a dispatch index if it is directly reachable
1932 and requires in-function processing. Regions that share post-landing
1933 pads may share dispatch indices. */
1934 /* ??? Post-landing pad sharing doesn't actually happen at the moment
1935 (see build_post_landing_pads) so we don't bother checking for it. */
1937 index = 0;
1938 for (i = cfun->eh->last_region_number; i > 0; --i)
1939 if (lp_info[i].directly_reachable)
1940 lp_info[i].dispatch_index = index++;
1942 /* Finally: assign call-site values. If dwarf2 terms, this would be
1943 the region number assigned by convert_to_eh_region_ranges, but
1944 handles no-action and must-not-throw differently. */
1946 call_site_base = 1;
1947 for (i = cfun->eh->last_region_number; i > 0; --i)
1948 if (lp_info[i].directly_reachable)
1950 int action = lp_info[i].action_index;
1952 /* Map must-not-throw to otherwise unused call-site index 0. */
1953 if (action == -2)
1954 index = 0;
1955 /* Map no-action to otherwise unused call-site index -1. */
1956 else if (action == -1)
1957 index = -1;
1958 /* Otherwise, look it up in the table. */
1959 else
1960 index = add_call_site (GEN_INT (lp_info[i].dispatch_index), action);
1962 lp_info[i].call_site_index = index;
1966 static void
1967 sjlj_mark_call_sites (struct sjlj_lp_info *lp_info)
1969 int last_call_site = -2;
1970 rtx insn, mem;
1972 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
1974 struct eh_region *region;
1975 int this_call_site;
1976 rtx note, before, p;
1978 /* Reset value tracking at extended basic block boundaries. */
1979 if (GET_CODE (insn) == CODE_LABEL)
1980 last_call_site = -2;
1982 if (! INSN_P (insn))
1983 continue;
1985 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1986 if (!note)
1988 /* Calls (and trapping insns) without notes are outside any
1989 exception handling region in this function. Mark them as
1990 no action. */
1991 if (GET_CODE (insn) == CALL_INSN
1992 || (flag_non_call_exceptions
1993 && may_trap_p (PATTERN (insn))))
1994 this_call_site = -1;
1995 else
1996 continue;
1998 else
2000 /* Calls that are known to not throw need not be marked. */
2001 if (INTVAL (XEXP (note, 0)) <= 0)
2002 continue;
2004 region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
2005 this_call_site = lp_info[region->region_number].call_site_index;
2008 if (this_call_site == last_call_site)
2009 continue;
2011 /* Don't separate a call from it's argument loads. */
2012 before = insn;
2013 if (GET_CODE (insn) == CALL_INSN)
2014 before = find_first_parameter_load (insn, NULL_RTX);
2016 start_sequence ();
2017 mem = adjust_address (cfun->eh->sjlj_fc, TYPE_MODE (integer_type_node),
2018 sjlj_fc_call_site_ofs);
2019 emit_move_insn (mem, GEN_INT (this_call_site));
2020 p = get_insns ();
2021 end_sequence ();
2023 emit_insn_before (p, before);
2024 last_call_site = this_call_site;
2028 /* Construct the SjLj_Function_Context. */
2030 static void
2031 sjlj_emit_function_enter (rtx dispatch_label)
2033 rtx fn_begin, fc, mem, seq;
2035 fc = cfun->eh->sjlj_fc;
2037 start_sequence ();
2039 /* We're storing this libcall's address into memory instead of
2040 calling it directly. Thus, we must call assemble_external_libcall
2041 here, as we can not depend on emit_library_call to do it for us. */
2042 assemble_external_libcall (eh_personality_libfunc);
2043 mem = adjust_address (fc, Pmode, sjlj_fc_personality_ofs);
2044 emit_move_insn (mem, eh_personality_libfunc);
2046 mem = adjust_address (fc, Pmode, sjlj_fc_lsda_ofs);
2047 if (cfun->uses_eh_lsda)
2049 char buf[20];
2050 ASM_GENERATE_INTERNAL_LABEL (buf, "LLSDA", current_function_funcdef_no);
2051 emit_move_insn (mem, gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf)));
2053 else
2054 emit_move_insn (mem, const0_rtx);
2056 #ifdef DONT_USE_BUILTIN_SETJMP
2058 rtx x, note;
2059 x = emit_library_call_value (setjmp_libfunc, NULL_RTX, LCT_RETURNS_TWICE,
2060 TYPE_MODE (integer_type_node), 1,
2061 plus_constant (XEXP (fc, 0),
2062 sjlj_fc_jbuf_ofs), Pmode);
2064 note = emit_note (NOTE_INSN_EXPECTED_VALUE);
2065 NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, x, const0_rtx);
2067 emit_cmp_and_jump_insns (x, const0_rtx, NE, 0,
2068 TYPE_MODE (integer_type_node), 0, dispatch_label);
2070 #else
2071 expand_builtin_setjmp_setup (plus_constant (XEXP (fc, 0), sjlj_fc_jbuf_ofs),
2072 dispatch_label);
2073 #endif
2075 emit_library_call (unwind_sjlj_register_libfunc, LCT_NORMAL, VOIDmode,
2076 1, XEXP (fc, 0), Pmode);
2078 seq = get_insns ();
2079 end_sequence ();
2081 /* ??? Instead of doing this at the beginning of the function,
2082 do this in a block that is at loop level 0 and dominates all
2083 can_throw_internal instructions. */
2085 for (fn_begin = get_insns (); ; fn_begin = NEXT_INSN (fn_begin))
2086 if (GET_CODE (fn_begin) == NOTE
2087 && NOTE_LINE_NUMBER (fn_begin) == NOTE_INSN_FUNCTION_BEG)
2088 break;
2089 emit_insn_after (seq, fn_begin);
2092 /* Call back from expand_function_end to know where we should put
2093 the call to unwind_sjlj_unregister_libfunc if needed. */
2095 void
2096 sjlj_emit_function_exit_after (rtx after)
2098 cfun->eh->sjlj_exit_after = after;
2101 static void
2102 sjlj_emit_function_exit (void)
2104 rtx seq;
2106 start_sequence ();
2108 emit_library_call (unwind_sjlj_unregister_libfunc, LCT_NORMAL, VOIDmode,
2109 1, XEXP (cfun->eh->sjlj_fc, 0), Pmode);
2111 seq = get_insns ();
2112 end_sequence ();
2114 /* ??? Really this can be done in any block at loop level 0 that
2115 post-dominates all can_throw_internal instructions. This is
2116 the last possible moment. */
2118 emit_insn_after (seq, cfun->eh->sjlj_exit_after);
2121 static void
2122 sjlj_emit_dispatch_table (rtx dispatch_label, struct sjlj_lp_info *lp_info)
2124 int i, first_reachable;
2125 rtx mem, dispatch, seq, fc;
2127 fc = cfun->eh->sjlj_fc;
2129 start_sequence ();
2131 emit_label (dispatch_label);
2133 #ifndef DONT_USE_BUILTIN_SETJMP
2134 expand_builtin_setjmp_receiver (dispatch_label);
2135 #endif
2137 /* Load up dispatch index, exc_ptr and filter values from the
2138 function context. */
2139 mem = adjust_address (fc, TYPE_MODE (integer_type_node),
2140 sjlj_fc_call_site_ofs);
2141 dispatch = copy_to_reg (mem);
2143 mem = adjust_address (fc, word_mode, sjlj_fc_data_ofs);
2144 if (word_mode != ptr_mode)
2146 #ifdef POINTERS_EXTEND_UNSIGNED
2147 mem = convert_memory_address (ptr_mode, mem);
2148 #else
2149 mem = convert_to_mode (ptr_mode, mem, 0);
2150 #endif
2152 emit_move_insn (cfun->eh->exc_ptr, mem);
2154 mem = adjust_address (fc, word_mode, sjlj_fc_data_ofs + UNITS_PER_WORD);
2155 emit_move_insn (cfun->eh->filter, mem);
2157 /* Jump to one of the directly reachable regions. */
2158 /* ??? This really ought to be using a switch statement. */
2160 first_reachable = 0;
2161 for (i = cfun->eh->last_region_number; i > 0; --i)
2163 if (! lp_info[i].directly_reachable)
2164 continue;
2166 if (! first_reachable)
2168 first_reachable = i;
2169 continue;
2172 emit_cmp_and_jump_insns (dispatch, GEN_INT (lp_info[i].dispatch_index),
2173 EQ, NULL_RTX, TYPE_MODE (integer_type_node), 0,
2174 cfun->eh->region_array[i]->post_landing_pad);
2177 seq = get_insns ();
2178 end_sequence ();
2180 emit_insn_before (seq, (cfun->eh->region_array[first_reachable]
2181 ->post_landing_pad));
2184 static void
2185 sjlj_build_landing_pads (void)
2187 struct sjlj_lp_info *lp_info;
2189 lp_info = xcalloc (cfun->eh->last_region_number + 1,
2190 sizeof (struct sjlj_lp_info));
2192 if (sjlj_find_directly_reachable_regions (lp_info))
2194 rtx dispatch_label = gen_label_rtx ();
2196 cfun->eh->sjlj_fc
2197 = assign_stack_local (TYPE_MODE (sjlj_fc_type_node),
2198 int_size_in_bytes (sjlj_fc_type_node),
2199 TYPE_ALIGN (sjlj_fc_type_node));
2201 sjlj_assign_call_site_values (dispatch_label, lp_info);
2202 sjlj_mark_call_sites (lp_info);
2204 sjlj_emit_function_enter (dispatch_label);
2205 sjlj_emit_dispatch_table (dispatch_label, lp_info);
2206 sjlj_emit_function_exit ();
2209 free (lp_info);
2212 void
2213 finish_eh_generation (void)
2215 /* Nothing to do if no regions created. */
2216 if (cfun->eh->region_tree == NULL)
2217 return;
2219 /* The object here is to provide find_basic_blocks with detailed
2220 information (via reachable_handlers) on how exception control
2221 flows within the function. In this first pass, we can include
2222 type information garnered from ERT_THROW and ERT_ALLOWED_EXCEPTIONS
2223 regions, and hope that it will be useful in deleting unreachable
2224 handlers. Subsequently, we will generate landing pads which will
2225 connect many of the handlers, and then type information will not
2226 be effective. Still, this is a win over previous implementations. */
2228 cleanup_cfg (CLEANUP_PRE_LOOP | CLEANUP_NO_INSN_DEL);
2230 /* These registers are used by the landing pads. Make sure they
2231 have been generated. */
2232 get_exception_pointer (cfun);
2233 get_exception_filter (cfun);
2235 /* Construct the landing pads. */
2237 assign_filter_values ();
2238 build_post_landing_pads ();
2239 connect_post_landing_pads ();
2240 if (USING_SJLJ_EXCEPTIONS)
2241 sjlj_build_landing_pads ();
2242 else
2243 dw2_build_landing_pads ();
2245 cfun->eh->built_landing_pads = 1;
2247 /* We've totally changed the CFG. Start over. */
2248 find_exception_handler_labels ();
2249 rebuild_jump_labels (get_insns ());
2250 find_basic_blocks (get_insns (), max_reg_num (), 0);
2251 cleanup_cfg (CLEANUP_PRE_LOOP | CLEANUP_NO_INSN_DEL);
2254 static hashval_t
2255 ehl_hash (const void *pentry)
2257 struct ehl_map_entry *entry = (struct ehl_map_entry *) pentry;
2259 /* 2^32 * ((sqrt(5) - 1) / 2) */
2260 const hashval_t scaled_golden_ratio = 0x9e3779b9;
2261 return CODE_LABEL_NUMBER (entry->label) * scaled_golden_ratio;
2264 static int
2265 ehl_eq (const void *pentry, const void *pdata)
2267 struct ehl_map_entry *entry = (struct ehl_map_entry *) pentry;
2268 struct ehl_map_entry *data = (struct ehl_map_entry *) pdata;
2270 return entry->label == data->label;
2273 /* This section handles removing dead code for flow. */
2275 /* Remove LABEL from exception_handler_label_map. */
2277 static void
2278 remove_exception_handler_label (rtx label)
2280 struct ehl_map_entry **slot, tmp;
2282 /* If exception_handler_label_map was not built yet,
2283 there is nothing to do. */
2284 if (cfun->eh->exception_handler_label_map == NULL)
2285 return;
2287 tmp.label = label;
2288 slot = (struct ehl_map_entry **)
2289 htab_find_slot (cfun->eh->exception_handler_label_map, &tmp, NO_INSERT);
2290 if (! slot)
2291 abort ();
2293 htab_clear_slot (cfun->eh->exception_handler_label_map, (void **) slot);
2296 /* Splice REGION from the region tree etc. */
2298 static void
2299 remove_eh_handler (struct eh_region *region)
2301 struct eh_region **pp, **pp_start, *p, *outer, *inner;
2302 rtx lab;
2304 /* For the benefit of efficiently handling REG_EH_REGION notes,
2305 replace this region in the region array with its containing
2306 region. Note that previous region deletions may result in
2307 multiple copies of this region in the array, so we have a
2308 list of alternate numbers by which we are known. */
2310 outer = region->outer;
2311 cfun->eh->region_array[region->region_number] = outer;
2312 if (region->aka)
2314 int i;
2315 EXECUTE_IF_SET_IN_BITMAP (region->aka, 0, i,
2316 { cfun->eh->region_array[i] = outer; });
2319 if (outer)
2321 if (!outer->aka)
2322 outer->aka = BITMAP_GGC_ALLOC ();
2323 if (region->aka)
2324 bitmap_a_or_b (outer->aka, outer->aka, region->aka);
2325 bitmap_set_bit (outer->aka, region->region_number);
2328 if (cfun->eh->built_landing_pads)
2329 lab = region->landing_pad;
2330 else
2331 lab = region->label;
2332 if (lab)
2333 remove_exception_handler_label (lab);
2335 if (outer)
2336 pp_start = &outer->inner;
2337 else
2338 pp_start = &cfun->eh->region_tree;
2339 for (pp = pp_start, p = *pp; p != region; pp = &p->next_peer, p = *pp)
2340 continue;
2341 *pp = region->next_peer;
2343 inner = region->inner;
2344 if (inner)
2346 for (p = inner; p->next_peer ; p = p->next_peer)
2347 p->outer = outer;
2348 p->outer = outer;
2350 p->next_peer = *pp_start;
2351 *pp_start = inner;
2354 if (region->type == ERT_CATCH)
2356 struct eh_region *try, *next, *prev;
2358 for (try = region->next_peer;
2359 try->type == ERT_CATCH;
2360 try = try->next_peer)
2361 continue;
2362 if (try->type != ERT_TRY)
2363 abort ();
2365 next = region->u.catch.next_catch;
2366 prev = region->u.catch.prev_catch;
2368 if (next)
2369 next->u.catch.prev_catch = prev;
2370 else
2371 try->u.try.last_catch = prev;
2372 if (prev)
2373 prev->u.catch.next_catch = next;
2374 else
2376 try->u.try.catch = next;
2377 if (! next)
2378 remove_eh_handler (try);
2383 /* LABEL heads a basic block that is about to be deleted. If this
2384 label corresponds to an exception region, we may be able to
2385 delete the region. */
2387 void
2388 maybe_remove_eh_handler (rtx label)
2390 struct ehl_map_entry **slot, tmp;
2391 struct eh_region *region;
2393 /* ??? After generating landing pads, it's not so simple to determine
2394 if the region data is completely unused. One must examine the
2395 landing pad and the post landing pad, and whether an inner try block
2396 is referencing the catch handlers directly. */
2397 if (cfun->eh->built_landing_pads)
2398 return;
2400 tmp.label = label;
2401 slot = (struct ehl_map_entry **)
2402 htab_find_slot (cfun->eh->exception_handler_label_map, &tmp, NO_INSERT);
2403 if (! slot)
2404 return;
2405 region = (*slot)->region;
2406 if (! region)
2407 return;
2409 /* Flow will want to remove MUST_NOT_THROW regions as unreachable
2410 because there is no path to the fallback call to terminate.
2411 But the region continues to affect call-site data until there
2412 are no more contained calls, which we don't see here. */
2413 if (region->type == ERT_MUST_NOT_THROW)
2415 htab_clear_slot (cfun->eh->exception_handler_label_map, (void **) slot);
2416 region->label = NULL_RTX;
2418 else
2419 remove_eh_handler (region);
2422 /* Invokes CALLBACK for every exception handler label. Only used by old
2423 loop hackery; should not be used by new code. */
2425 void
2426 for_each_eh_label (void (*callback) (rtx))
2428 htab_traverse (cfun->eh->exception_handler_label_map, for_each_eh_label_1,
2429 (void *)callback);
2432 static int
2433 for_each_eh_label_1 (void **pentry, void *data)
2435 struct ehl_map_entry *entry = *(struct ehl_map_entry **)pentry;
2436 void (*callback) (rtx) = (void (*) (rtx)) data;
2438 (*callback) (entry->label);
2439 return 1;
2442 /* This section describes CFG exception edges for flow. */
2444 /* For communicating between calls to reachable_next_level. */
2445 struct reachable_info GTY(())
2447 tree types_caught;
2448 tree types_allowed;
2449 rtx handlers;
2452 /* A subroutine of reachable_next_level. Return true if TYPE, or a
2453 base class of TYPE, is in HANDLED. */
2455 static int
2456 check_handled (tree handled, tree type)
2458 tree t;
2460 /* We can check for exact matches without front-end help. */
2461 if (! lang_eh_type_covers)
2463 for (t = handled; t ; t = TREE_CHAIN (t))
2464 if (TREE_VALUE (t) == type)
2465 return 1;
2467 else
2469 for (t = handled; t ; t = TREE_CHAIN (t))
2470 if ((*lang_eh_type_covers) (TREE_VALUE (t), type))
2471 return 1;
2474 return 0;
2477 /* A subroutine of reachable_next_level. If we are collecting a list
2478 of handlers, add one. After landing pad generation, reference
2479 it instead of the handlers themselves. Further, the handlers are
2480 all wired together, so by referencing one, we've got them all.
2481 Before landing pad generation we reference each handler individually.
2483 LP_REGION contains the landing pad; REGION is the handler. */
2485 static void
2486 add_reachable_handler (struct reachable_info *info, struct eh_region *lp_region, struct eh_region *region)
2488 if (! info)
2489 return;
2491 if (cfun->eh->built_landing_pads)
2493 if (! info->handlers)
2494 info->handlers = alloc_INSN_LIST (lp_region->landing_pad, NULL_RTX);
2496 else
2497 info->handlers = alloc_INSN_LIST (region->label, info->handlers);
2500 /* Process one level of exception regions for reachability.
2501 If TYPE_THROWN is non-null, then it is the *exact* type being
2502 propagated. If INFO is non-null, then collect handler labels
2503 and caught/allowed type information between invocations. */
2505 static enum reachable_code
2506 reachable_next_level (struct eh_region *region, tree type_thrown,
2507 struct reachable_info *info)
2509 switch (region->type)
2511 case ERT_CLEANUP:
2512 /* Before landing-pad generation, we model control flow
2513 directly to the individual handlers. In this way we can
2514 see that catch handler types may shadow one another. */
2515 add_reachable_handler (info, region, region);
2516 return RNL_MAYBE_CAUGHT;
2518 case ERT_TRY:
2520 struct eh_region *c;
2521 enum reachable_code ret = RNL_NOT_CAUGHT;
2523 for (c = region->u.try.catch; c ; c = c->u.catch.next_catch)
2525 /* A catch-all handler ends the search. */
2526 if (c->u.catch.type_list == NULL)
2528 add_reachable_handler (info, region, c);
2529 return RNL_CAUGHT;
2532 if (type_thrown)
2534 /* If we have at least one type match, end the search. */
2535 tree tp_node = c->u.catch.type_list;
2537 for (; tp_node; tp_node = TREE_CHAIN (tp_node))
2539 tree type = TREE_VALUE (tp_node);
2541 if (type == type_thrown
2542 || (lang_eh_type_covers
2543 && (*lang_eh_type_covers) (type, type_thrown)))
2545 add_reachable_handler (info, region, c);
2546 return RNL_CAUGHT;
2550 /* If we have definitive information of a match failure,
2551 the catch won't trigger. */
2552 if (lang_eh_type_covers)
2553 return RNL_NOT_CAUGHT;
2556 /* At this point, we either don't know what type is thrown or
2557 don't have front-end assistance to help deciding if it is
2558 covered by one of the types in the list for this region.
2560 We'd then like to add this region to the list of reachable
2561 handlers since it is indeed potentially reachable based on the
2562 information we have.
2564 Actually, this handler is for sure not reachable if all the
2565 types it matches have already been caught. That is, it is only
2566 potentially reachable if at least one of the types it catches
2567 has not been previously caught. */
2569 if (! info)
2570 ret = RNL_MAYBE_CAUGHT;
2571 else
2573 tree tp_node = c->u.catch.type_list;
2574 bool maybe_reachable = false;
2576 /* Compute the potential reachability of this handler and
2577 update the list of types caught at the same time. */
2578 for (; tp_node; tp_node = TREE_CHAIN (tp_node))
2580 tree type = TREE_VALUE (tp_node);
2582 if (! check_handled (info->types_caught, type))
2584 info->types_caught
2585 = tree_cons (NULL, type, info->types_caught);
2587 maybe_reachable = true;
2591 if (maybe_reachable)
2593 add_reachable_handler (info, region, c);
2595 /* ??? If the catch type is a base class of every allowed
2596 type, then we know we can stop the search. */
2597 ret = RNL_MAYBE_CAUGHT;
2602 return ret;
2605 case ERT_ALLOWED_EXCEPTIONS:
2606 /* An empty list of types definitely ends the search. */
2607 if (region->u.allowed.type_list == NULL_TREE)
2609 add_reachable_handler (info, region, region);
2610 return RNL_CAUGHT;
2613 /* Collect a list of lists of allowed types for use in detecting
2614 when a catch may be transformed into a catch-all. */
2615 if (info)
2616 info->types_allowed = tree_cons (NULL_TREE,
2617 region->u.allowed.type_list,
2618 info->types_allowed);
2620 /* If we have definitive information about the type hierarchy,
2621 then we can tell if the thrown type will pass through the
2622 filter. */
2623 if (type_thrown && lang_eh_type_covers)
2625 if (check_handled (region->u.allowed.type_list, type_thrown))
2626 return RNL_NOT_CAUGHT;
2627 else
2629 add_reachable_handler (info, region, region);
2630 return RNL_CAUGHT;
2634 add_reachable_handler (info, region, region);
2635 return RNL_MAYBE_CAUGHT;
2637 case ERT_CATCH:
2638 /* Catch regions are handled by their controlling try region. */
2639 return RNL_NOT_CAUGHT;
2641 case ERT_MUST_NOT_THROW:
2642 /* Here we end our search, since no exceptions may propagate.
2643 If we've touched down at some landing pad previous, then the
2644 explicit function call we generated may be used. Otherwise
2645 the call is made by the runtime. */
2646 if (info && info->handlers)
2648 add_reachable_handler (info, region, region);
2649 return RNL_CAUGHT;
2651 else
2652 return RNL_BLOCKED;
2654 case ERT_THROW:
2655 case ERT_FIXUP:
2656 case ERT_UNKNOWN:
2657 /* Shouldn't see these here. */
2658 break;
2661 abort ();
2664 /* Retrieve a list of labels of exception handlers which can be
2665 reached by a given insn. */
2668 reachable_handlers (rtx insn)
2670 struct reachable_info info;
2671 struct eh_region *region;
2672 tree type_thrown;
2673 int region_number;
2675 if (GET_CODE (insn) == JUMP_INSN
2676 && GET_CODE (PATTERN (insn)) == RESX)
2677 region_number = XINT (PATTERN (insn), 0);
2678 else
2680 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2681 if (!note || INTVAL (XEXP (note, 0)) <= 0)
2682 return NULL;
2683 region_number = INTVAL (XEXP (note, 0));
2686 memset (&info, 0, sizeof (info));
2688 region = cfun->eh->region_array[region_number];
2690 type_thrown = NULL_TREE;
2691 if (GET_CODE (insn) == JUMP_INSN
2692 && GET_CODE (PATTERN (insn)) == RESX)
2694 /* A RESX leaves a region instead of entering it. Thus the
2695 region itself may have been deleted out from under us. */
2696 if (region == NULL)
2697 return NULL;
2698 region = region->outer;
2700 else if (region->type == ERT_THROW)
2702 type_thrown = region->u.throw.type;
2703 region = region->outer;
2706 while (region)
2708 if (reachable_next_level (region, type_thrown, &info) >= RNL_CAUGHT)
2709 break;
2710 /* If we have processed one cleanup, there is no point in
2711 processing any more of them. Each cleanup will have an edge
2712 to the next outer cleanup region, so the flow graph will be
2713 accurate. */
2714 if (region->type == ERT_CLEANUP)
2715 region = region->u.cleanup.prev_try;
2716 else
2717 region = region->outer;
2720 return info.handlers;
2723 /* Determine if the given INSN can throw an exception that is caught
2724 within the function. */
2726 bool
2727 can_throw_internal (rtx insn)
2729 struct eh_region *region;
2730 tree type_thrown;
2731 rtx note;
2733 if (! INSN_P (insn))
2734 return false;
2736 if (GET_CODE (insn) == INSN
2737 && GET_CODE (PATTERN (insn)) == SEQUENCE)
2738 insn = XVECEXP (PATTERN (insn), 0, 0);
2740 if (GET_CODE (insn) == CALL_INSN
2741 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
2743 int i;
2744 for (i = 0; i < 3; ++i)
2746 rtx sub = XEXP (PATTERN (insn), i);
2747 for (; sub ; sub = NEXT_INSN (sub))
2748 if (can_throw_internal (sub))
2749 return true;
2751 return false;
2754 /* Every insn that might throw has an EH_REGION note. */
2755 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2756 if (!note || INTVAL (XEXP (note, 0)) <= 0)
2757 return false;
2759 region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
2761 type_thrown = NULL_TREE;
2762 if (region->type == ERT_THROW)
2764 type_thrown = region->u.throw.type;
2765 region = region->outer;
2768 /* If this exception is ignored by each and every containing region,
2769 then control passes straight out. The runtime may handle some
2770 regions, which also do not require processing internally. */
2771 for (; region; region = region->outer)
2773 enum reachable_code how = reachable_next_level (region, type_thrown, 0);
2774 if (how == RNL_BLOCKED)
2775 return false;
2776 if (how != RNL_NOT_CAUGHT)
2777 return true;
2780 return false;
2783 /* Determine if the given INSN can throw an exception that is
2784 visible outside the function. */
2786 bool
2787 can_throw_external (rtx insn)
2789 struct eh_region *region;
2790 tree type_thrown;
2791 rtx note;
2793 if (! INSN_P (insn))
2794 return false;
2796 if (GET_CODE (insn) == INSN
2797 && GET_CODE (PATTERN (insn)) == SEQUENCE)
2798 insn = XVECEXP (PATTERN (insn), 0, 0);
2800 if (GET_CODE (insn) == CALL_INSN
2801 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
2803 int i;
2804 for (i = 0; i < 3; ++i)
2806 rtx sub = XEXP (PATTERN (insn), i);
2807 for (; sub ; sub = NEXT_INSN (sub))
2808 if (can_throw_external (sub))
2809 return true;
2811 return false;
2814 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2815 if (!note)
2817 /* Calls (and trapping insns) without notes are outside any
2818 exception handling region in this function. We have to
2819 assume it might throw. Given that the front end and middle
2820 ends mark known NOTHROW functions, this isn't so wildly
2821 inaccurate. */
2822 return (GET_CODE (insn) == CALL_INSN
2823 || (flag_non_call_exceptions
2824 && may_trap_p (PATTERN (insn))));
2826 if (INTVAL (XEXP (note, 0)) <= 0)
2827 return false;
2829 region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
2831 type_thrown = NULL_TREE;
2832 if (region->type == ERT_THROW)
2834 type_thrown = region->u.throw.type;
2835 region = region->outer;
2838 /* If the exception is caught or blocked by any containing region,
2839 then it is not seen by any calling function. */
2840 for (; region ; region = region->outer)
2841 if (reachable_next_level (region, type_thrown, NULL) >= RNL_CAUGHT)
2842 return false;
2844 return true;
2847 /* Set current_function_nothrow and cfun->all_throwers_are_sibcalls. */
2849 void
2850 set_nothrow_function_flags (void)
2852 rtx insn;
2854 current_function_nothrow = 1;
2856 /* Assume cfun->all_throwers_are_sibcalls until we encounter
2857 something that can throw an exception. We specifically exempt
2858 CALL_INSNs that are SIBLING_CALL_P, as these are really jumps,
2859 and can't throw. Most CALL_INSNs are not SIBLING_CALL_P, so this
2860 is optimistic. */
2862 cfun->all_throwers_are_sibcalls = 1;
2864 if (! flag_exceptions)
2865 return;
2867 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2868 if (can_throw_external (insn))
2870 current_function_nothrow = 0;
2872 if (GET_CODE (insn) != CALL_INSN || !SIBLING_CALL_P (insn))
2874 cfun->all_throwers_are_sibcalls = 0;
2875 return;
2879 for (insn = current_function_epilogue_delay_list; insn;
2880 insn = XEXP (insn, 1))
2881 if (can_throw_external (insn))
2883 current_function_nothrow = 0;
2885 if (GET_CODE (insn) != CALL_INSN || !SIBLING_CALL_P (insn))
2887 cfun->all_throwers_are_sibcalls = 0;
2888 return;
2894 /* Various hooks for unwind library. */
2896 /* Do any necessary initialization to access arbitrary stack frames.
2897 On the SPARC, this means flushing the register windows. */
2899 void
2900 expand_builtin_unwind_init (void)
2902 /* Set this so all the registers get saved in our frame; we need to be
2903 able to copy the saved values for any registers from frames we unwind. */
2904 current_function_has_nonlocal_label = 1;
2906 #ifdef SETUP_FRAME_ADDRESSES
2907 SETUP_FRAME_ADDRESSES ();
2908 #endif
2912 expand_builtin_eh_return_data_regno (tree arglist)
2914 tree which = TREE_VALUE (arglist);
2915 unsigned HOST_WIDE_INT iwhich;
2917 if (TREE_CODE (which) != INTEGER_CST)
2919 error ("argument of `__builtin_eh_return_regno' must be constant");
2920 return constm1_rtx;
2923 iwhich = tree_low_cst (which, 1);
2924 iwhich = EH_RETURN_DATA_REGNO (iwhich);
2925 if (iwhich == INVALID_REGNUM)
2926 return constm1_rtx;
2928 #ifdef DWARF_FRAME_REGNUM
2929 iwhich = DWARF_FRAME_REGNUM (iwhich);
2930 #else
2931 iwhich = DBX_REGISTER_NUMBER (iwhich);
2932 #endif
2934 return GEN_INT (iwhich);
2937 /* Given a value extracted from the return address register or stack slot,
2938 return the actual address encoded in that value. */
2941 expand_builtin_extract_return_addr (tree addr_tree)
2943 rtx addr = expand_expr (addr_tree, NULL_RTX, Pmode, 0);
2945 if (GET_MODE (addr) != Pmode
2946 && GET_MODE (addr) != VOIDmode)
2948 #ifdef POINTERS_EXTEND_UNSIGNED
2949 addr = convert_memory_address (Pmode, addr);
2950 #else
2951 addr = convert_to_mode (Pmode, addr, 0);
2952 #endif
2955 /* First mask out any unwanted bits. */
2956 #ifdef MASK_RETURN_ADDR
2957 expand_and (Pmode, addr, MASK_RETURN_ADDR, addr);
2958 #endif
2960 /* Then adjust to find the real return address. */
2961 #if defined (RETURN_ADDR_OFFSET)
2962 addr = plus_constant (addr, RETURN_ADDR_OFFSET);
2963 #endif
2965 return addr;
2968 /* Given an actual address in addr_tree, do any necessary encoding
2969 and return the value to be stored in the return address register or
2970 stack slot so the epilogue will return to that address. */
2973 expand_builtin_frob_return_addr (tree addr_tree)
2975 rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, 0);
2977 addr = convert_memory_address (Pmode, addr);
2979 #ifdef RETURN_ADDR_OFFSET
2980 addr = force_reg (Pmode, addr);
2981 addr = plus_constant (addr, -RETURN_ADDR_OFFSET);
2982 #endif
2984 return addr;
2987 /* Set up the epilogue with the magic bits we'll need to return to the
2988 exception handler. */
2990 void
2991 expand_builtin_eh_return (tree stackadj_tree ATTRIBUTE_UNUSED,
2992 tree handler_tree)
2994 rtx tmp;
2996 #ifdef EH_RETURN_STACKADJ_RTX
2997 tmp = expand_expr (stackadj_tree, cfun->eh->ehr_stackadj, VOIDmode, 0);
2998 tmp = convert_memory_address (Pmode, tmp);
2999 if (!cfun->eh->ehr_stackadj)
3000 cfun->eh->ehr_stackadj = copy_to_reg (tmp);
3001 else if (tmp != cfun->eh->ehr_stackadj)
3002 emit_move_insn (cfun->eh->ehr_stackadj, tmp);
3003 #endif
3005 tmp = expand_expr (handler_tree, cfun->eh->ehr_handler, VOIDmode, 0);
3006 tmp = convert_memory_address (Pmode, tmp);
3007 if (!cfun->eh->ehr_handler)
3008 cfun->eh->ehr_handler = copy_to_reg (tmp);
3009 else if (tmp != cfun->eh->ehr_handler)
3010 emit_move_insn (cfun->eh->ehr_handler, tmp);
3012 if (!cfun->eh->ehr_label)
3013 cfun->eh->ehr_label = gen_label_rtx ();
3014 emit_jump (cfun->eh->ehr_label);
3017 void
3018 expand_eh_return (void)
3020 rtx around_label;
3022 if (! cfun->eh->ehr_label)
3023 return;
3025 current_function_calls_eh_return = 1;
3027 #ifdef EH_RETURN_STACKADJ_RTX
3028 emit_move_insn (EH_RETURN_STACKADJ_RTX, const0_rtx);
3029 #endif
3031 around_label = gen_label_rtx ();
3032 emit_jump (around_label);
3034 emit_label (cfun->eh->ehr_label);
3035 clobber_return_register ();
3037 #ifdef EH_RETURN_STACKADJ_RTX
3038 emit_move_insn (EH_RETURN_STACKADJ_RTX, cfun->eh->ehr_stackadj);
3039 #endif
3041 #ifdef HAVE_eh_return
3042 if (HAVE_eh_return)
3043 emit_insn (gen_eh_return (cfun->eh->ehr_handler));
3044 else
3045 #endif
3047 #ifdef EH_RETURN_HANDLER_RTX
3048 emit_move_insn (EH_RETURN_HANDLER_RTX, cfun->eh->ehr_handler);
3049 #else
3050 error ("__builtin_eh_return not supported on this target");
3051 #endif
3054 emit_label (around_label);
3057 /* In the following functions, we represent entries in the action table
3058 as 1-based indices. Special cases are:
3060 0: null action record, non-null landing pad; implies cleanups
3061 -1: null action record, null landing pad; implies no action
3062 -2: no call-site entry; implies must_not_throw
3063 -3: we have yet to process outer regions
3065 Further, no special cases apply to the "next" field of the record.
3066 For next, 0 means end of list. */
3068 struct action_record
3070 int offset;
3071 int filter;
3072 int next;
3075 static int
3076 action_record_eq (const void *pentry, const void *pdata)
3078 const struct action_record *entry = (const struct action_record *) pentry;
3079 const struct action_record *data = (const struct action_record *) pdata;
3080 return entry->filter == data->filter && entry->next == data->next;
3083 static hashval_t
3084 action_record_hash (const void *pentry)
3086 const struct action_record *entry = (const struct action_record *) pentry;
3087 return entry->next * 1009 + entry->filter;
3090 static int
3091 add_action_record (htab_t ar_hash, int filter, int next)
3093 struct action_record **slot, *new, tmp;
3095 tmp.filter = filter;
3096 tmp.next = next;
3097 slot = (struct action_record **) htab_find_slot (ar_hash, &tmp, INSERT);
3099 if ((new = *slot) == NULL)
3101 new = xmalloc (sizeof (*new));
3102 new->offset = VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data) + 1;
3103 new->filter = filter;
3104 new->next = next;
3105 *slot = new;
3107 /* The filter value goes in untouched. The link to the next
3108 record is a "self-relative" byte offset, or zero to indicate
3109 that there is no next record. So convert the absolute 1 based
3110 indices we've been carrying around into a displacement. */
3112 push_sleb128 (&cfun->eh->action_record_data, filter);
3113 if (next)
3114 next -= VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data) + 1;
3115 push_sleb128 (&cfun->eh->action_record_data, next);
3118 return new->offset;
3121 static int
3122 collect_one_action_chain (htab_t ar_hash, struct eh_region *region)
3124 struct eh_region *c;
3125 int next;
3127 /* If we've reached the top of the region chain, then we have
3128 no actions, and require no landing pad. */
3129 if (region == NULL)
3130 return -1;
3132 switch (region->type)
3134 case ERT_CLEANUP:
3135 /* A cleanup adds a zero filter to the beginning of the chain, but
3136 there are special cases to look out for. If there are *only*
3137 cleanups along a path, then it compresses to a zero action.
3138 Further, if there are multiple cleanups along a path, we only
3139 need to represent one of them, as that is enough to trigger
3140 entry to the landing pad at runtime. */
3141 next = collect_one_action_chain (ar_hash, region->outer);
3142 if (next <= 0)
3143 return 0;
3144 for (c = region->outer; c ; c = c->outer)
3145 if (c->type == ERT_CLEANUP)
3146 return next;
3147 return add_action_record (ar_hash, 0, next);
3149 case ERT_TRY:
3150 /* Process the associated catch regions in reverse order.
3151 If there's a catch-all handler, then we don't need to
3152 search outer regions. Use a magic -3 value to record
3153 that we haven't done the outer search. */
3154 next = -3;
3155 for (c = region->u.try.last_catch; c ; c = c->u.catch.prev_catch)
3157 if (c->u.catch.type_list == NULL)
3159 /* Retrieve the filter from the head of the filter list
3160 where we have stored it (see assign_filter_values). */
3161 int filter
3162 = TREE_INT_CST_LOW (TREE_VALUE (c->u.catch.filter_list));
3164 next = add_action_record (ar_hash, filter, 0);
3166 else
3168 /* Once the outer search is done, trigger an action record for
3169 each filter we have. */
3170 tree flt_node;
3172 if (next == -3)
3174 next = collect_one_action_chain (ar_hash, region->outer);
3176 /* If there is no next action, terminate the chain. */
3177 if (next == -1)
3178 next = 0;
3179 /* If all outer actions are cleanups or must_not_throw,
3180 we'll have no action record for it, since we had wanted
3181 to encode these states in the call-site record directly.
3182 Add a cleanup action to the chain to catch these. */
3183 else if (next <= 0)
3184 next = add_action_record (ar_hash, 0, 0);
3187 flt_node = c->u.catch.filter_list;
3188 for (; flt_node; flt_node = TREE_CHAIN (flt_node))
3190 int filter = TREE_INT_CST_LOW (TREE_VALUE (flt_node));
3191 next = add_action_record (ar_hash, filter, next);
3195 return next;
3197 case ERT_ALLOWED_EXCEPTIONS:
3198 /* An exception specification adds its filter to the
3199 beginning of the chain. */
3200 next = collect_one_action_chain (ar_hash, region->outer);
3201 return add_action_record (ar_hash, region->u.allowed.filter,
3202 next < 0 ? 0 : next);
3204 case ERT_MUST_NOT_THROW:
3205 /* A must-not-throw region with no inner handlers or cleanups
3206 requires no call-site entry. Note that this differs from
3207 the no handler or cleanup case in that we do require an lsda
3208 to be generated. Return a magic -2 value to record this. */
3209 return -2;
3211 case ERT_CATCH:
3212 case ERT_THROW:
3213 /* CATCH regions are handled in TRY above. THROW regions are
3214 for optimization information only and produce no output. */
3215 return collect_one_action_chain (ar_hash, region->outer);
3217 default:
3218 abort ();
3222 static int
3223 add_call_site (rtx landing_pad, int action)
3225 struct call_site_record *data = cfun->eh->call_site_data;
3226 int used = cfun->eh->call_site_data_used;
3227 int size = cfun->eh->call_site_data_size;
3229 if (used >= size)
3231 size = (size ? size * 2 : 64);
3232 data = ggc_realloc (data, sizeof (*data) * size);
3233 cfun->eh->call_site_data = data;
3234 cfun->eh->call_site_data_size = size;
3237 data[used].landing_pad = landing_pad;
3238 data[used].action = action;
3240 cfun->eh->call_site_data_used = used + 1;
3242 return used + call_site_base;
3245 /* Turn REG_EH_REGION notes back into NOTE_INSN_EH_REGION notes.
3246 The new note numbers will not refer to region numbers, but
3247 instead to call site entries. */
3249 void
3250 convert_to_eh_region_ranges (void)
3252 rtx insn, iter, note;
3253 htab_t ar_hash;
3254 int last_action = -3;
3255 rtx last_action_insn = NULL_RTX;
3256 rtx last_landing_pad = NULL_RTX;
3257 rtx first_no_action_insn = NULL_RTX;
3258 int call_site = 0;
3260 if (USING_SJLJ_EXCEPTIONS || cfun->eh->region_tree == NULL)
3261 return;
3263 VARRAY_UCHAR_INIT (cfun->eh->action_record_data, 64, "action_record_data");
3265 ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
3267 for (iter = get_insns (); iter ; iter = NEXT_INSN (iter))
3268 if (INSN_P (iter))
3270 struct eh_region *region;
3271 int this_action;
3272 rtx this_landing_pad;
3274 insn = iter;
3275 if (GET_CODE (insn) == INSN
3276 && GET_CODE (PATTERN (insn)) == SEQUENCE)
3277 insn = XVECEXP (PATTERN (insn), 0, 0);
3279 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3280 if (!note)
3282 if (! (GET_CODE (insn) == CALL_INSN
3283 || (flag_non_call_exceptions
3284 && may_trap_p (PATTERN (insn)))))
3285 continue;
3286 this_action = -1;
3287 region = NULL;
3289 else
3291 if (INTVAL (XEXP (note, 0)) <= 0)
3292 continue;
3293 region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
3294 this_action = collect_one_action_chain (ar_hash, region);
3297 /* Existence of catch handlers, or must-not-throw regions
3298 implies that an lsda is needed (even if empty). */
3299 if (this_action != -1)
3300 cfun->uses_eh_lsda = 1;
3302 /* Delay creation of region notes for no-action regions
3303 until we're sure that an lsda will be required. */
3304 else if (last_action == -3)
3306 first_no_action_insn = iter;
3307 last_action = -1;
3310 /* Cleanups and handlers may share action chains but not
3311 landing pads. Collect the landing pad for this region. */
3312 if (this_action >= 0)
3314 struct eh_region *o;
3315 for (o = region; ! o->landing_pad ; o = o->outer)
3316 continue;
3317 this_landing_pad = o->landing_pad;
3319 else
3320 this_landing_pad = NULL_RTX;
3322 /* Differing actions or landing pads implies a change in call-site
3323 info, which implies some EH_REGION note should be emitted. */
3324 if (last_action != this_action
3325 || last_landing_pad != this_landing_pad)
3327 /* If we'd not seen a previous action (-3) or the previous
3328 action was must-not-throw (-2), then we do not need an
3329 end note. */
3330 if (last_action >= -1)
3332 /* If we delayed the creation of the begin, do it now. */
3333 if (first_no_action_insn)
3335 call_site = add_call_site (NULL_RTX, 0);
3336 note = emit_note_before (NOTE_INSN_EH_REGION_BEG,
3337 first_no_action_insn);
3338 NOTE_EH_HANDLER (note) = call_site;
3339 first_no_action_insn = NULL_RTX;
3342 note = emit_note_after (NOTE_INSN_EH_REGION_END,
3343 last_action_insn);
3344 NOTE_EH_HANDLER (note) = call_site;
3347 /* If the new action is must-not-throw, then no region notes
3348 are created. */
3349 if (this_action >= -1)
3351 call_site = add_call_site (this_landing_pad,
3352 this_action < 0 ? 0 : this_action);
3353 note = emit_note_before (NOTE_INSN_EH_REGION_BEG, iter);
3354 NOTE_EH_HANDLER (note) = call_site;
3357 last_action = this_action;
3358 last_landing_pad = this_landing_pad;
3360 last_action_insn = iter;
3363 if (last_action >= -1 && ! first_no_action_insn)
3365 note = emit_note_after (NOTE_INSN_EH_REGION_END, last_action_insn);
3366 NOTE_EH_HANDLER (note) = call_site;
3369 htab_delete (ar_hash);
3373 static void
3374 push_uleb128 (varray_type *data_area, unsigned int value)
3378 unsigned char byte = value & 0x7f;
3379 value >>= 7;
3380 if (value)
3381 byte |= 0x80;
3382 VARRAY_PUSH_UCHAR (*data_area, byte);
3384 while (value);
3387 static void
3388 push_sleb128 (varray_type *data_area, int value)
3390 unsigned char byte;
3391 int more;
3395 byte = value & 0x7f;
3396 value >>= 7;
3397 more = ! ((value == 0 && (byte & 0x40) == 0)
3398 || (value == -1 && (byte & 0x40) != 0));
3399 if (more)
3400 byte |= 0x80;
3401 VARRAY_PUSH_UCHAR (*data_area, byte);
3403 while (more);
3407 #ifndef HAVE_AS_LEB128
3408 static int
3409 dw2_size_of_call_site_table (void)
3411 int n = cfun->eh->call_site_data_used;
3412 int size = n * (4 + 4 + 4);
3413 int i;
3415 for (i = 0; i < n; ++i)
3417 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3418 size += size_of_uleb128 (cs->action);
3421 return size;
3424 static int
3425 sjlj_size_of_call_site_table (void)
3427 int n = cfun->eh->call_site_data_used;
3428 int size = 0;
3429 int i;
3431 for (i = 0; i < n; ++i)
3433 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3434 size += size_of_uleb128 (INTVAL (cs->landing_pad));
3435 size += size_of_uleb128 (cs->action);
3438 return size;
3440 #endif
3442 static void
3443 dw2_output_call_site_table (void)
3445 const char *const function_start_lab
3446 = IDENTIFIER_POINTER (current_function_func_begin_label);
3447 int n = cfun->eh->call_site_data_used;
3448 int i;
3450 for (i = 0; i < n; ++i)
3452 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3453 char reg_start_lab[32];
3454 char reg_end_lab[32];
3455 char landing_pad_lab[32];
3457 ASM_GENERATE_INTERNAL_LABEL (reg_start_lab, "LEHB", call_site_base + i);
3458 ASM_GENERATE_INTERNAL_LABEL (reg_end_lab, "LEHE", call_site_base + i);
3460 if (cs->landing_pad)
3461 ASM_GENERATE_INTERNAL_LABEL (landing_pad_lab, "L",
3462 CODE_LABEL_NUMBER (cs->landing_pad));
3464 /* ??? Perhaps use insn length scaling if the assembler supports
3465 generic arithmetic. */
3466 /* ??? Perhaps use attr_length to choose data1 or data2 instead of
3467 data4 if the function is small enough. */
3468 #ifdef HAVE_AS_LEB128
3469 dw2_asm_output_delta_uleb128 (reg_start_lab, function_start_lab,
3470 "region %d start", i);
3471 dw2_asm_output_delta_uleb128 (reg_end_lab, reg_start_lab,
3472 "length");
3473 if (cs->landing_pad)
3474 dw2_asm_output_delta_uleb128 (landing_pad_lab, function_start_lab,
3475 "landing pad");
3476 else
3477 dw2_asm_output_data_uleb128 (0, "landing pad");
3478 #else
3479 dw2_asm_output_delta (4, reg_start_lab, function_start_lab,
3480 "region %d start", i);
3481 dw2_asm_output_delta (4, reg_end_lab, reg_start_lab, "length");
3482 if (cs->landing_pad)
3483 dw2_asm_output_delta (4, landing_pad_lab, function_start_lab,
3484 "landing pad");
3485 else
3486 dw2_asm_output_data (4, 0, "landing pad");
3487 #endif
3488 dw2_asm_output_data_uleb128 (cs->action, "action");
3491 call_site_base += n;
3494 static void
3495 sjlj_output_call_site_table (void)
3497 int n = cfun->eh->call_site_data_used;
3498 int i;
3500 for (i = 0; i < n; ++i)
3502 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3504 dw2_asm_output_data_uleb128 (INTVAL (cs->landing_pad),
3505 "region %d landing pad", i);
3506 dw2_asm_output_data_uleb128 (cs->action, "action");
3509 call_site_base += n;
3512 /* Tell assembler to switch to the section for the exception handling
3513 table. */
3515 void
3516 default_exception_section (void)
3518 if (targetm.have_named_sections)
3520 int flags;
3521 #ifdef HAVE_LD_RO_RW_SECTION_MIXING
3522 int tt_format = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3524 flags = (! flag_pic
3525 || ((tt_format & 0x70) != DW_EH_PE_absptr
3526 && (tt_format & 0x70) != DW_EH_PE_aligned))
3527 ? 0 : SECTION_WRITE;
3528 #else
3529 flags = SECTION_WRITE;
3530 #endif
3531 named_section_flags (".gcc_except_table", flags);
3533 else if (flag_pic)
3534 data_section ();
3535 else
3536 readonly_data_section ();
3539 void
3540 output_function_exception_table (void)
3542 int tt_format, cs_format, lp_format, i, n;
3543 #ifdef HAVE_AS_LEB128
3544 char ttype_label[32];
3545 char cs_after_size_label[32];
3546 char cs_end_label[32];
3547 #else
3548 int call_site_len;
3549 #endif
3550 int have_tt_data;
3551 int tt_format_size = 0;
3553 /* Not all functions need anything. */
3554 if (! cfun->uses_eh_lsda)
3555 return;
3557 #ifdef IA64_UNWIND_INFO
3558 fputs ("\t.personality\t", asm_out_file);
3559 output_addr_const (asm_out_file, eh_personality_libfunc);
3560 fputs ("\n\t.handlerdata\n", asm_out_file);
3561 /* Note that varasm still thinks we're in the function's code section.
3562 The ".endp" directive that will immediately follow will take us back. */
3563 #else
3564 (*targetm.asm_out.exception_section) ();
3565 #endif
3567 have_tt_data = (VARRAY_ACTIVE_SIZE (cfun->eh->ttype_data) > 0
3568 || VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data) > 0);
3570 /* Indicate the format of the @TType entries. */
3571 if (! have_tt_data)
3572 tt_format = DW_EH_PE_omit;
3573 else
3575 tt_format = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3576 #ifdef HAVE_AS_LEB128
3577 ASM_GENERATE_INTERNAL_LABEL (ttype_label, "LLSDATT",
3578 current_function_funcdef_no);
3579 #endif
3580 tt_format_size = size_of_encoded_value (tt_format);
3582 assemble_align (tt_format_size * BITS_PER_UNIT);
3585 (*targetm.asm_out.internal_label) (asm_out_file, "LLSDA",
3586 current_function_funcdef_no);
3588 /* The LSDA header. */
3590 /* Indicate the format of the landing pad start pointer. An omitted
3591 field implies @LPStart == @Start. */
3592 /* Currently we always put @LPStart == @Start. This field would
3593 be most useful in moving the landing pads completely out of
3594 line to another section, but it could also be used to minimize
3595 the size of uleb128 landing pad offsets. */
3596 lp_format = DW_EH_PE_omit;
3597 dw2_asm_output_data (1, lp_format, "@LPStart format (%s)",
3598 eh_data_format_name (lp_format));
3600 /* @LPStart pointer would go here. */
3602 dw2_asm_output_data (1, tt_format, "@TType format (%s)",
3603 eh_data_format_name (tt_format));
3605 #ifndef HAVE_AS_LEB128
3606 if (USING_SJLJ_EXCEPTIONS)
3607 call_site_len = sjlj_size_of_call_site_table ();
3608 else
3609 call_site_len = dw2_size_of_call_site_table ();
3610 #endif
3612 /* A pc-relative 4-byte displacement to the @TType data. */
3613 if (have_tt_data)
3615 #ifdef HAVE_AS_LEB128
3616 char ttype_after_disp_label[32];
3617 ASM_GENERATE_INTERNAL_LABEL (ttype_after_disp_label, "LLSDATTD",
3618 current_function_funcdef_no);
3619 dw2_asm_output_delta_uleb128 (ttype_label, ttype_after_disp_label,
3620 "@TType base offset");
3621 ASM_OUTPUT_LABEL (asm_out_file, ttype_after_disp_label);
3622 #else
3623 /* Ug. Alignment queers things. */
3624 unsigned int before_disp, after_disp, last_disp, disp;
3626 before_disp = 1 + 1;
3627 after_disp = (1 + size_of_uleb128 (call_site_len)
3628 + call_site_len
3629 + VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data)
3630 + (VARRAY_ACTIVE_SIZE (cfun->eh->ttype_data)
3631 * tt_format_size));
3633 disp = after_disp;
3636 unsigned int disp_size, pad;
3638 last_disp = disp;
3639 disp_size = size_of_uleb128 (disp);
3640 pad = before_disp + disp_size + after_disp;
3641 if (pad % tt_format_size)
3642 pad = tt_format_size - (pad % tt_format_size);
3643 else
3644 pad = 0;
3645 disp = after_disp + pad;
3647 while (disp != last_disp);
3649 dw2_asm_output_data_uleb128 (disp, "@TType base offset");
3650 #endif
3653 /* Indicate the format of the call-site offsets. */
3654 #ifdef HAVE_AS_LEB128
3655 cs_format = DW_EH_PE_uleb128;
3656 #else
3657 cs_format = DW_EH_PE_udata4;
3658 #endif
3659 dw2_asm_output_data (1, cs_format, "call-site format (%s)",
3660 eh_data_format_name (cs_format));
3662 #ifdef HAVE_AS_LEB128
3663 ASM_GENERATE_INTERNAL_LABEL (cs_after_size_label, "LLSDACSB",
3664 current_function_funcdef_no);
3665 ASM_GENERATE_INTERNAL_LABEL (cs_end_label, "LLSDACSE",
3666 current_function_funcdef_no);
3667 dw2_asm_output_delta_uleb128 (cs_end_label, cs_after_size_label,
3668 "Call-site table length");
3669 ASM_OUTPUT_LABEL (asm_out_file, cs_after_size_label);
3670 if (USING_SJLJ_EXCEPTIONS)
3671 sjlj_output_call_site_table ();
3672 else
3673 dw2_output_call_site_table ();
3674 ASM_OUTPUT_LABEL (asm_out_file, cs_end_label);
3675 #else
3676 dw2_asm_output_data_uleb128 (call_site_len,"Call-site table length");
3677 if (USING_SJLJ_EXCEPTIONS)
3678 sjlj_output_call_site_table ();
3679 else
3680 dw2_output_call_site_table ();
3681 #endif
3683 /* ??? Decode and interpret the data for flag_debug_asm. */
3684 n = VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data);
3685 for (i = 0; i < n; ++i)
3686 dw2_asm_output_data (1, VARRAY_UCHAR (cfun->eh->action_record_data, i),
3687 (i ? NULL : "Action record table"));
3689 if (have_tt_data)
3690 assemble_align (tt_format_size * BITS_PER_UNIT);
3692 i = VARRAY_ACTIVE_SIZE (cfun->eh->ttype_data);
3693 while (i-- > 0)
3695 tree type = VARRAY_TREE (cfun->eh->ttype_data, i);
3696 rtx value;
3698 if (type == NULL_TREE)
3699 value = const0_rtx;
3700 else
3702 struct cgraph_varpool_node *node;
3704 type = lookup_type_for_runtime (type);
3705 value = expand_expr (type, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
3707 /* Let cgraph know that the rtti decl is used. Not all of the
3708 paths below go through assemble_integer, which would take
3709 care of this for us. */
3710 if (TREE_CODE (type) == ADDR_EXPR)
3712 type = TREE_OPERAND (type, 0);
3713 node = cgraph_varpool_node (type);
3714 if (node)
3715 cgraph_varpool_mark_needed_node (node);
3717 else if (TREE_CODE (type) != INTEGER_CST)
3718 abort ();
3721 if (tt_format == DW_EH_PE_absptr || tt_format == DW_EH_PE_aligned)
3722 assemble_integer (value, tt_format_size,
3723 tt_format_size * BITS_PER_UNIT, 1);
3724 else
3725 dw2_asm_output_encoded_addr_rtx (tt_format, value, NULL);
3728 #ifdef HAVE_AS_LEB128
3729 if (have_tt_data)
3730 ASM_OUTPUT_LABEL (asm_out_file, ttype_label);
3731 #endif
3733 /* ??? Decode and interpret the data for flag_debug_asm. */
3734 n = VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data);
3735 for (i = 0; i < n; ++i)
3736 dw2_asm_output_data (1, VARRAY_UCHAR (cfun->eh->ehspec_data, i),
3737 (i ? NULL : "Exception specification table"));
3739 function_section (current_function_decl);
3742 #include "gt-except.h"