cfglayout.c (fixup_fallthru_exit_predecesor): Make void, rename to ....
[official-gcc.git] / gcc / except.c
blob7ddb80734e89f17ef86be59649a593d02179af32
1 /* Implements exception handling.
2 Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 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 "rtl.h"
53 #include "tree.h"
54 #include "flags.h"
55 #include "function.h"
56 #include "expr.h"
57 #include "libfuncs.h"
58 #include "insn-config.h"
59 #include "except.h"
60 #include "integrate.h"
61 #include "hard-reg-set.h"
62 #include "basic-block.h"
63 #include "output.h"
64 #include "dwarf2asm.h"
65 #include "dwarf2out.h"
66 #include "dwarf2.h"
67 #include "toplev.h"
68 #include "hashtab.h"
69 #include "intl.h"
70 #include "ggc.h"
71 #include "tm_p.h"
72 #include "target.h"
74 /* Provide defaults for stuff that may not be defined when using
75 sjlj exceptions. */
76 #ifndef EH_RETURN_STACKADJ_RTX
77 #define EH_RETURN_STACKADJ_RTX 0
78 #endif
79 #ifndef EH_RETURN_HANDLER_RTX
80 #define EH_RETURN_HANDLER_RTX 0
81 #endif
82 #ifndef EH_RETURN_DATA_REGNO
83 #define EH_RETURN_DATA_REGNO(N) INVALID_REGNUM
84 #endif
87 /* Nonzero means enable synchronous exceptions for non-call instructions. */
88 int flag_non_call_exceptions;
90 /* Protect cleanup actions with must-not-throw regions, with a call
91 to the given failure handler. */
92 tree (*lang_protect_cleanup_actions) PARAMS ((void));
94 /* Return true if type A catches type B. */
95 int (*lang_eh_type_covers) PARAMS ((tree a, tree b));
97 /* Map a type to a runtime object to match type. */
98 tree (*lang_eh_runtime_type) PARAMS ((tree));
100 /* A list of labels used for exception handlers. */
101 rtx exception_handler_labels;
103 static int call_site_base;
104 static unsigned int sjlj_funcdef_number;
105 static htab_t type_to_runtime_map;
107 /* Describe the SjLj_Function_Context structure. */
108 static tree sjlj_fc_type_node;
109 static int sjlj_fc_call_site_ofs;
110 static int sjlj_fc_data_ofs;
111 static int sjlj_fc_personality_ofs;
112 static int sjlj_fc_lsda_ofs;
113 static int sjlj_fc_jbuf_ofs;
115 /* Describes one exception region. */
116 struct eh_region
118 /* The immediately surrounding region. */
119 struct eh_region *outer;
121 /* The list of immediately contained regions. */
122 struct eh_region *inner;
123 struct eh_region *next_peer;
125 /* An identifier for this region. */
126 int region_number;
128 /* Each region does exactly one thing. */
129 enum eh_region_type
131 ERT_CLEANUP = 1,
132 ERT_TRY,
133 ERT_CATCH,
134 ERT_ALLOWED_EXCEPTIONS,
135 ERT_MUST_NOT_THROW,
136 ERT_THROW,
137 ERT_FIXUP
138 } type;
140 /* Holds the action to perform based on the preceding type. */
141 union {
142 /* A list of catch blocks, a surrounding try block,
143 and the label for continuing after a catch. */
144 struct {
145 struct eh_region *catch;
146 struct eh_region *last_catch;
147 struct eh_region *prev_try;
148 rtx continue_label;
149 } try;
151 /* The list through the catch handlers, the list of type objects
152 matched, and the list of associated filters. */
153 struct {
154 struct eh_region *next_catch;
155 struct eh_region *prev_catch;
156 tree type_list;
157 tree filter_list;
158 } catch;
160 /* A tree_list of allowed types. */
161 struct {
162 tree type_list;
163 int filter;
164 } allowed;
166 /* The type given by a call to "throw foo();", or discovered
167 for a throw. */
168 struct {
169 tree type;
170 } throw;
172 /* Retain the cleanup expression even after expansion so that
173 we can match up fixup regions. */
174 struct {
175 tree exp;
176 } cleanup;
178 /* The real region (by expression and by pointer) that fixup code
179 should live in. */
180 struct {
181 tree cleanup_exp;
182 struct eh_region *real_region;
183 } fixup;
184 } u;
186 /* Entry point for this region's handler before landing pads are built. */
187 rtx label;
189 /* Entry point for this region's handler from the runtime eh library. */
190 rtx landing_pad;
192 /* Entry point for this region's handler from an inner region. */
193 rtx post_landing_pad;
195 /* The RESX insn for handing off control to the next outermost handler,
196 if appropriate. */
197 rtx resume;
200 /* Used to save exception status for each function. */
201 struct eh_status
203 /* The tree of all regions for this function. */
204 struct eh_region *region_tree;
206 /* The same information as an indexable array. */
207 struct eh_region **region_array;
209 /* The most recently open region. */
210 struct eh_region *cur_region;
212 /* This is the region for which we are processing catch blocks. */
213 struct eh_region *try_region;
215 /* A stack (TREE_LIST) of lists of handlers. The TREE_VALUE of each
216 node is itself a TREE_CHAINed list of handlers for regions that
217 are not yet closed. The TREE_VALUE of each entry contains the
218 handler for the corresponding entry on the ehstack. */
219 tree protect_list;
221 rtx filter;
222 rtx exc_ptr;
224 int built_landing_pads;
225 int last_region_number;
227 varray_type ttype_data;
228 varray_type ehspec_data;
229 varray_type action_record_data;
231 struct call_site_record
233 rtx landing_pad;
234 int action;
235 } *call_site_data;
236 int call_site_data_used;
237 int call_site_data_size;
239 rtx ehr_stackadj;
240 rtx ehr_handler;
241 rtx ehr_label;
243 rtx sjlj_fc;
244 rtx sjlj_exit_after;
248 static void mark_eh_region PARAMS ((struct eh_region *));
250 static int t2r_eq PARAMS ((const PTR,
251 const PTR));
252 static hashval_t t2r_hash PARAMS ((const PTR));
253 static int t2r_mark_1 PARAMS ((PTR *, PTR));
254 static void t2r_mark PARAMS ((PTR));
255 static void add_type_for_runtime PARAMS ((tree));
256 static tree lookup_type_for_runtime PARAMS ((tree));
258 static struct eh_region *expand_eh_region_end PARAMS ((void));
260 static rtx get_exception_filter PARAMS ((struct function *));
262 static void collect_eh_region_array PARAMS ((void));
263 static void resolve_fixup_regions PARAMS ((void));
264 static void remove_fixup_regions PARAMS ((void));
265 static void convert_from_eh_region_ranges_1 PARAMS ((rtx *, int *, int));
267 static struct eh_region *duplicate_eh_region_1 PARAMS ((struct eh_region *,
268 struct inline_remap *));
269 static void duplicate_eh_region_2 PARAMS ((struct eh_region *,
270 struct eh_region **));
271 static int ttypes_filter_eq PARAMS ((const PTR,
272 const PTR));
273 static hashval_t ttypes_filter_hash PARAMS ((const PTR));
274 static int ehspec_filter_eq PARAMS ((const PTR,
275 const PTR));
276 static hashval_t ehspec_filter_hash PARAMS ((const PTR));
277 static int add_ttypes_entry PARAMS ((htab_t, tree));
278 static int add_ehspec_entry PARAMS ((htab_t, htab_t,
279 tree));
280 static void assign_filter_values PARAMS ((void));
281 static void build_post_landing_pads PARAMS ((void));
282 static void connect_post_landing_pads PARAMS ((void));
283 static void dw2_build_landing_pads PARAMS ((void));
285 struct sjlj_lp_info;
286 static bool sjlj_find_directly_reachable_regions
287 PARAMS ((struct sjlj_lp_info *));
288 static void sjlj_assign_call_site_values
289 PARAMS ((rtx, struct sjlj_lp_info *));
290 static void sjlj_mark_call_sites
291 PARAMS ((struct sjlj_lp_info *));
292 static void sjlj_emit_function_enter PARAMS ((rtx));
293 static void sjlj_emit_function_exit PARAMS ((void));
294 static void sjlj_emit_dispatch_table
295 PARAMS ((rtx, struct sjlj_lp_info *));
296 static void sjlj_build_landing_pads PARAMS ((void));
298 static void remove_exception_handler_label PARAMS ((rtx));
299 static void remove_eh_handler PARAMS ((struct eh_region *));
301 struct reachable_info;
303 /* The return value of reachable_next_level. */
304 enum reachable_code
306 /* The given exception is not processed by the given region. */
307 RNL_NOT_CAUGHT,
308 /* The given exception may need processing by the given region. */
309 RNL_MAYBE_CAUGHT,
310 /* The given exception is completely processed by the given region. */
311 RNL_CAUGHT,
312 /* The given exception is completely processed by the runtime. */
313 RNL_BLOCKED
316 static int check_handled PARAMS ((tree, tree));
317 static void add_reachable_handler
318 PARAMS ((struct reachable_info *, struct eh_region *,
319 struct eh_region *));
320 static enum reachable_code reachable_next_level
321 PARAMS ((struct eh_region *, tree, struct reachable_info *));
323 static int action_record_eq PARAMS ((const PTR,
324 const PTR));
325 static hashval_t action_record_hash PARAMS ((const PTR));
326 static int add_action_record PARAMS ((htab_t, int, int));
327 static int collect_one_action_chain PARAMS ((htab_t,
328 struct eh_region *));
329 static int add_call_site PARAMS ((rtx, int));
331 static void push_uleb128 PARAMS ((varray_type *,
332 unsigned int));
333 static void push_sleb128 PARAMS ((varray_type *, int));
334 #ifndef HAVE_AS_LEB128
335 static int dw2_size_of_call_site_table PARAMS ((void));
336 static int sjlj_size_of_call_site_table PARAMS ((void));
337 #endif
338 static void dw2_output_call_site_table PARAMS ((void));
339 static void sjlj_output_call_site_table PARAMS ((void));
342 /* Routine to see if exception handling is turned on.
343 DO_WARN is non-zero if we want to inform the user that exception
344 handling is turned off.
346 This is used to ensure that -fexceptions has been specified if the
347 compiler tries to use any exception-specific functions. */
350 doing_eh (do_warn)
351 int do_warn;
353 if (! flag_exceptions)
355 static int warned = 0;
356 if (! warned && do_warn)
358 error ("exception handling disabled, use -fexceptions to enable");
359 warned = 1;
361 return 0;
363 return 1;
367 void
368 init_eh ()
370 ggc_add_rtx_root (&exception_handler_labels, 1);
372 if (! flag_exceptions)
373 return;
375 type_to_runtime_map = htab_create (31, t2r_hash, t2r_eq, NULL);
376 ggc_add_root (&type_to_runtime_map, 1, sizeof (htab_t), t2r_mark);
378 /* Create the SjLj_Function_Context structure. This should match
379 the definition in unwind-sjlj.c. */
380 if (USING_SJLJ_EXCEPTIONS)
382 tree f_jbuf, f_per, f_lsda, f_prev, f_cs, f_data, tmp;
384 sjlj_fc_type_node = make_lang_type (RECORD_TYPE);
385 ggc_add_tree_root (&sjlj_fc_type_node, 1);
387 f_prev = build_decl (FIELD_DECL, get_identifier ("__prev"),
388 build_pointer_type (sjlj_fc_type_node));
389 DECL_FIELD_CONTEXT (f_prev) = sjlj_fc_type_node;
391 f_cs = build_decl (FIELD_DECL, get_identifier ("__call_site"),
392 integer_type_node);
393 DECL_FIELD_CONTEXT (f_cs) = sjlj_fc_type_node;
395 tmp = build_index_type (build_int_2 (4 - 1, 0));
396 tmp = build_array_type (type_for_mode (word_mode, 1), tmp);
397 f_data = build_decl (FIELD_DECL, get_identifier ("__data"), tmp);
398 DECL_FIELD_CONTEXT (f_data) = sjlj_fc_type_node;
400 f_per = build_decl (FIELD_DECL, get_identifier ("__personality"),
401 ptr_type_node);
402 DECL_FIELD_CONTEXT (f_per) = sjlj_fc_type_node;
404 f_lsda = build_decl (FIELD_DECL, get_identifier ("__lsda"),
405 ptr_type_node);
406 DECL_FIELD_CONTEXT (f_lsda) = sjlj_fc_type_node;
408 #ifdef DONT_USE_BUILTIN_SETJMP
409 #ifdef JMP_BUF_SIZE
410 tmp = build_int_2 (JMP_BUF_SIZE - 1, 0);
411 #else
412 /* Should be large enough for most systems, if it is not,
413 JMP_BUF_SIZE should be defined with the proper value. It will
414 also tend to be larger than necessary for most systems, a more
415 optimal port will define JMP_BUF_SIZE. */
416 tmp = build_int_2 (FIRST_PSEUDO_REGISTER + 2 - 1, 0);
417 #endif
418 #else
419 /* This is 2 for builtin_setjmp, plus whatever the target requires
420 via STACK_SAVEAREA_MODE (SAVE_NONLOCAL). */
421 tmp = build_int_2 ((GET_MODE_SIZE (STACK_SAVEAREA_MODE (SAVE_NONLOCAL))
422 / GET_MODE_SIZE (Pmode)) + 2 - 1, 0);
423 #endif
424 tmp = build_index_type (tmp);
425 tmp = build_array_type (ptr_type_node, tmp);
426 f_jbuf = build_decl (FIELD_DECL, get_identifier ("__jbuf"), tmp);
427 #ifdef DONT_USE_BUILTIN_SETJMP
428 /* We don't know what the alignment requirements of the
429 runtime's jmp_buf has. Overestimate. */
430 DECL_ALIGN (f_jbuf) = BIGGEST_ALIGNMENT;
431 DECL_USER_ALIGN (f_jbuf) = 1;
432 #endif
433 DECL_FIELD_CONTEXT (f_jbuf) = sjlj_fc_type_node;
435 TYPE_FIELDS (sjlj_fc_type_node) = f_prev;
436 TREE_CHAIN (f_prev) = f_cs;
437 TREE_CHAIN (f_cs) = f_data;
438 TREE_CHAIN (f_data) = f_per;
439 TREE_CHAIN (f_per) = f_lsda;
440 TREE_CHAIN (f_lsda) = f_jbuf;
442 layout_type (sjlj_fc_type_node);
444 /* Cache the interesting field offsets so that we have
445 easy access from rtl. */
446 sjlj_fc_call_site_ofs
447 = (tree_low_cst (DECL_FIELD_OFFSET (f_cs), 1)
448 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_cs), 1) / BITS_PER_UNIT);
449 sjlj_fc_data_ofs
450 = (tree_low_cst (DECL_FIELD_OFFSET (f_data), 1)
451 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_data), 1) / BITS_PER_UNIT);
452 sjlj_fc_personality_ofs
453 = (tree_low_cst (DECL_FIELD_OFFSET (f_per), 1)
454 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_per), 1) / BITS_PER_UNIT);
455 sjlj_fc_lsda_ofs
456 = (tree_low_cst (DECL_FIELD_OFFSET (f_lsda), 1)
457 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_lsda), 1) / BITS_PER_UNIT);
458 sjlj_fc_jbuf_ofs
459 = (tree_low_cst (DECL_FIELD_OFFSET (f_jbuf), 1)
460 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_jbuf), 1) / BITS_PER_UNIT);
464 void
465 init_eh_for_function ()
467 cfun->eh = (struct eh_status *) xcalloc (1, sizeof (struct eh_status));
470 /* Mark EH for GC. */
472 static void
473 mark_eh_region (region)
474 struct eh_region *region;
476 if (! region)
477 return;
479 switch (region->type)
481 case ERT_CLEANUP:
482 ggc_mark_tree (region->u.cleanup.exp);
483 break;
484 case ERT_TRY:
485 ggc_mark_rtx (region->u.try.continue_label);
486 break;
487 case ERT_CATCH:
488 ggc_mark_tree (region->u.catch.type_list);
489 ggc_mark_tree (region->u.catch.filter_list);
490 break;
491 case ERT_ALLOWED_EXCEPTIONS:
492 ggc_mark_tree (region->u.allowed.type_list);
493 break;
494 case ERT_MUST_NOT_THROW:
495 break;
496 case ERT_THROW:
497 ggc_mark_tree (region->u.throw.type);
498 break;
499 case ERT_FIXUP:
500 ggc_mark_tree (region->u.fixup.cleanup_exp);
501 break;
502 default:
503 abort ();
506 ggc_mark_rtx (region->label);
507 ggc_mark_rtx (region->resume);
508 ggc_mark_rtx (region->landing_pad);
509 ggc_mark_rtx (region->post_landing_pad);
512 void
513 mark_eh_status (eh)
514 struct eh_status *eh;
516 int i;
518 if (eh == 0)
519 return;
521 /* If we've called collect_eh_region_array, use it. Otherwise walk
522 the tree non-recursively. */
523 if (eh->region_array)
525 for (i = eh->last_region_number; i > 0; --i)
527 struct eh_region *r = eh->region_array[i];
528 if (r && r->region_number == i)
529 mark_eh_region (r);
532 else if (eh->region_tree)
534 struct eh_region *r = eh->region_tree;
535 while (1)
537 mark_eh_region (r);
538 if (r->inner)
539 r = r->inner;
540 else if (r->next_peer)
541 r = r->next_peer;
542 else
544 do {
545 r = r->outer;
546 if (r == NULL)
547 goto tree_done;
548 } while (r->next_peer == NULL);
549 r = r->next_peer;
552 tree_done:;
555 ggc_mark_tree (eh->protect_list);
556 ggc_mark_rtx (eh->filter);
557 ggc_mark_rtx (eh->exc_ptr);
558 ggc_mark_tree_varray (eh->ttype_data);
560 if (eh->call_site_data)
562 for (i = eh->call_site_data_used - 1; i >= 0; --i)
563 ggc_mark_rtx (eh->call_site_data[i].landing_pad);
566 ggc_mark_rtx (eh->ehr_stackadj);
567 ggc_mark_rtx (eh->ehr_handler);
568 ggc_mark_rtx (eh->ehr_label);
570 ggc_mark_rtx (eh->sjlj_fc);
571 ggc_mark_rtx (eh->sjlj_exit_after);
574 void
575 free_eh_status (f)
576 struct function *f;
578 struct eh_status *eh = f->eh;
580 if (eh->region_array)
582 int i;
583 for (i = eh->last_region_number; i > 0; --i)
585 struct eh_region *r = eh->region_array[i];
586 /* Mind we don't free a region struct more than once. */
587 if (r && r->region_number == i)
588 free (r);
590 free (eh->region_array);
592 else if (eh->region_tree)
594 struct eh_region *next, *r = eh->region_tree;
595 while (1)
597 if (r->inner)
598 r = r->inner;
599 else if (r->next_peer)
601 next = r->next_peer;
602 free (r);
603 r = next;
605 else
607 do {
608 next = r->outer;
609 free (r);
610 r = next;
611 if (r == NULL)
612 goto tree_done;
613 } while (r->next_peer == NULL);
614 next = r->next_peer;
615 free (r);
616 r = next;
619 tree_done:;
622 VARRAY_FREE (eh->ttype_data);
623 VARRAY_FREE (eh->ehspec_data);
624 VARRAY_FREE (eh->action_record_data);
625 if (eh->call_site_data)
626 free (eh->call_site_data);
628 free (eh);
629 f->eh = NULL;
633 /* Start an exception handling region. All instructions emitted
634 after this point are considered to be part of the region until
635 expand_eh_region_end is invoked. */
637 void
638 expand_eh_region_start ()
640 struct eh_region *new_region;
641 struct eh_region *cur_region;
642 rtx note;
644 if (! doing_eh (0))
645 return;
647 /* Insert a new blank region as a leaf in the tree. */
648 new_region = (struct eh_region *) xcalloc (1, sizeof (*new_region));
649 cur_region = cfun->eh->cur_region;
650 new_region->outer = cur_region;
651 if (cur_region)
653 new_region->next_peer = cur_region->inner;
654 cur_region->inner = new_region;
656 else
658 new_region->next_peer = cfun->eh->region_tree;
659 cfun->eh->region_tree = new_region;
661 cfun->eh->cur_region = new_region;
663 /* Create a note marking the start of this region. */
664 new_region->region_number = ++cfun->eh->last_region_number;
665 note = emit_note (NULL, NOTE_INSN_EH_REGION_BEG);
666 NOTE_EH_HANDLER (note) = new_region->region_number;
669 /* Common code to end a region. Returns the region just ended. */
671 static struct eh_region *
672 expand_eh_region_end ()
674 struct eh_region *cur_region = cfun->eh->cur_region;
675 rtx note;
677 /* Create a nute marking the end of this region. */
678 note = emit_note (NULL, NOTE_INSN_EH_REGION_END);
679 NOTE_EH_HANDLER (note) = cur_region->region_number;
681 /* Pop. */
682 cfun->eh->cur_region = cur_region->outer;
684 return cur_region;
687 /* End an exception handling region for a cleanup. HANDLER is an
688 expression to expand for the cleanup. */
690 void
691 expand_eh_region_end_cleanup (handler)
692 tree handler;
694 struct eh_region *region;
695 tree protect_cleanup_actions;
696 rtx around_label;
697 rtx data_save[2];
699 if (! doing_eh (0))
700 return;
702 region = expand_eh_region_end ();
703 region->type = ERT_CLEANUP;
704 region->label = gen_label_rtx ();
705 region->u.cleanup.exp = handler;
707 around_label = gen_label_rtx ();
708 emit_jump (around_label);
710 emit_label (region->label);
712 /* Give the language a chance to specify an action to be taken if an
713 exception is thrown that would propogate out of the HANDLER. */
714 protect_cleanup_actions
715 = (lang_protect_cleanup_actions
716 ? (*lang_protect_cleanup_actions) ()
717 : NULL_TREE);
719 if (protect_cleanup_actions)
720 expand_eh_region_start ();
722 /* In case this cleanup involves an inline destructor with a try block in
723 it, we need to save the EH return data registers around it. */
724 data_save[0] = gen_reg_rtx (Pmode);
725 emit_move_insn (data_save[0], get_exception_pointer (cfun));
726 data_save[1] = gen_reg_rtx (word_mode);
727 emit_move_insn (data_save[1], get_exception_filter (cfun));
729 expand_expr (handler, const0_rtx, VOIDmode, 0);
731 emit_move_insn (cfun->eh->exc_ptr, data_save[0]);
732 emit_move_insn (cfun->eh->filter, data_save[1]);
734 if (protect_cleanup_actions)
735 expand_eh_region_end_must_not_throw (protect_cleanup_actions);
737 /* We need any stack adjustment complete before the around_label. */
738 do_pending_stack_adjust ();
740 /* We delay the generation of the _Unwind_Resume until we generate
741 landing pads. We emit a marker here so as to get good control
742 flow data in the meantime. */
743 region->resume
744 = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
745 emit_barrier ();
747 emit_label (around_label);
750 /* End an exception handling region for a try block, and prepares
751 for subsequent calls to expand_start_catch. */
753 void
754 expand_start_all_catch ()
756 struct eh_region *region;
758 if (! doing_eh (1))
759 return;
761 region = expand_eh_region_end ();
762 region->type = ERT_TRY;
763 region->u.try.prev_try = cfun->eh->try_region;
764 region->u.try.continue_label = gen_label_rtx ();
766 cfun->eh->try_region = region;
768 emit_jump (region->u.try.continue_label);
771 /* Begin a catch clause. TYPE is the type caught, a list of such types, or
772 null if this is a catch-all clause. Providing a type list enables to
773 associate the catch region with potentially several exception types, which
774 is useful e.g. for Ada. */
776 void
777 expand_start_catch (type_or_list)
778 tree type_or_list;
780 struct eh_region *t, *c, *l;
781 tree type_list;
783 if (! doing_eh (0))
784 return;
786 type_list = type_or_list;
788 if (type_or_list)
790 /* Ensure to always end up with a type list to normalize further
791 processing, then register each type against the runtime types
792 map. */
793 tree type_node;
795 if (TREE_CODE (type_or_list) != TREE_LIST)
796 type_list = tree_cons (NULL_TREE, type_or_list, NULL_TREE);
798 type_node = type_list;
799 for (; type_node; type_node = TREE_CHAIN (type_node))
800 add_type_for_runtime (TREE_VALUE (type_node));
803 expand_eh_region_start ();
805 t = cfun->eh->try_region;
806 c = cfun->eh->cur_region;
807 c->type = ERT_CATCH;
808 c->u.catch.type_list = type_list;
809 c->label = gen_label_rtx ();
811 l = t->u.try.last_catch;
812 c->u.catch.prev_catch = l;
813 if (l)
814 l->u.catch.next_catch = c;
815 else
816 t->u.try.catch = c;
817 t->u.try.last_catch = c;
819 emit_label (c->label);
822 /* End a catch clause. Control will resume after the try/catch block. */
824 void
825 expand_end_catch ()
827 struct eh_region *try_region, *catch_region;
829 if (! doing_eh (0))
830 return;
832 catch_region = expand_eh_region_end ();
833 try_region = cfun->eh->try_region;
835 emit_jump (try_region->u.try.continue_label);
838 /* End a sequence of catch handlers for a try block. */
840 void
841 expand_end_all_catch ()
843 struct eh_region *try_region;
845 if (! doing_eh (0))
846 return;
848 try_region = cfun->eh->try_region;
849 cfun->eh->try_region = try_region->u.try.prev_try;
851 emit_label (try_region->u.try.continue_label);
854 /* End an exception region for an exception type filter. ALLOWED is a
855 TREE_LIST of types to be matched by the runtime. FAILURE is an
856 expression to invoke if a mismatch occurs.
858 ??? We could use these semantics for calls to rethrow, too; if we can
859 see the surrounding catch clause, we know that the exception we're
860 rethrowing satisfies the "filter" of the catch type. */
862 void
863 expand_eh_region_end_allowed (allowed, failure)
864 tree allowed, failure;
866 struct eh_region *region;
867 rtx around_label;
869 if (! doing_eh (0))
870 return;
872 region = expand_eh_region_end ();
873 region->type = ERT_ALLOWED_EXCEPTIONS;
874 region->u.allowed.type_list = allowed;
875 region->label = gen_label_rtx ();
877 for (; allowed ; allowed = TREE_CHAIN (allowed))
878 add_type_for_runtime (TREE_VALUE (allowed));
880 /* We must emit the call to FAILURE here, so that if this function
881 throws a different exception, that it will be processed by the
882 correct region. */
884 around_label = gen_label_rtx ();
885 emit_jump (around_label);
887 emit_label (region->label);
888 expand_expr (failure, const0_rtx, VOIDmode, EXPAND_NORMAL);
889 /* We must adjust the stack before we reach the AROUND_LABEL because
890 the call to FAILURE does not occur on all paths to the
891 AROUND_LABEL. */
892 do_pending_stack_adjust ();
894 emit_label (around_label);
897 /* End an exception region for a must-not-throw filter. FAILURE is an
898 expression invoke if an uncaught exception propagates this far.
900 This is conceptually identical to expand_eh_region_end_allowed with
901 an empty allowed list (if you passed "std::terminate" instead of
902 "__cxa_call_unexpected"), but they are represented differently in
903 the C++ LSDA. */
905 void
906 expand_eh_region_end_must_not_throw (failure)
907 tree failure;
909 struct eh_region *region;
910 rtx around_label;
912 if (! doing_eh (0))
913 return;
915 region = expand_eh_region_end ();
916 region->type = ERT_MUST_NOT_THROW;
917 region->label = gen_label_rtx ();
919 /* We must emit the call to FAILURE here, so that if this function
920 throws a different exception, that it will be processed by the
921 correct region. */
923 around_label = gen_label_rtx ();
924 emit_jump (around_label);
926 emit_label (region->label);
927 expand_expr (failure, const0_rtx, VOIDmode, EXPAND_NORMAL);
929 emit_label (around_label);
932 /* End an exception region for a throw. No handling goes on here,
933 but it's the easiest way for the front-end to indicate what type
934 is being thrown. */
936 void
937 expand_eh_region_end_throw (type)
938 tree type;
940 struct eh_region *region;
942 if (! doing_eh (0))
943 return;
945 region = expand_eh_region_end ();
946 region->type = ERT_THROW;
947 region->u.throw.type = type;
950 /* End a fixup region. Within this region the cleanups for the immediately
951 enclosing region are _not_ run. This is used for goto cleanup to avoid
952 destroying an object twice.
954 This would be an extraordinarily simple prospect, were it not for the
955 fact that we don't actually know what the immediately enclosing region
956 is. This surprising fact is because expand_cleanups is currently
957 generating a sequence that it will insert somewhere else. We collect
958 the proper notion of "enclosing" in convert_from_eh_region_ranges. */
960 void
961 expand_eh_region_end_fixup (handler)
962 tree handler;
964 struct eh_region *fixup;
966 if (! doing_eh (0))
967 return;
969 fixup = expand_eh_region_end ();
970 fixup->type = ERT_FIXUP;
971 fixup->u.fixup.cleanup_exp = handler;
974 /* Return an rtl expression for a pointer to the exception object
975 within a handler. */
978 get_exception_pointer (fun)
979 struct function *fun;
981 rtx exc_ptr = fun->eh->exc_ptr;
982 if (fun == cfun && ! exc_ptr)
984 exc_ptr = gen_reg_rtx (Pmode);
985 fun->eh->exc_ptr = exc_ptr;
987 return exc_ptr;
990 /* Return an rtl expression for the exception dispatch filter
991 within a handler. */
993 static rtx
994 get_exception_filter (fun)
995 struct function *fun;
997 rtx filter = fun->eh->filter;
998 if (fun == cfun && ! filter)
1000 filter = gen_reg_rtx (word_mode);
1001 fun->eh->filter = filter;
1003 return filter;
1006 /* Begin a region that will contain entries created with
1007 add_partial_entry. */
1009 void
1010 begin_protect_partials ()
1012 /* Push room for a new list. */
1013 cfun->eh->protect_list
1014 = tree_cons (NULL_TREE, NULL_TREE, cfun->eh->protect_list);
1017 /* Start a new exception region for a region of code that has a
1018 cleanup action and push the HANDLER for the region onto
1019 protect_list. All of the regions created with add_partial_entry
1020 will be ended when end_protect_partials is invoked. */
1022 void
1023 add_partial_entry (handler)
1024 tree handler;
1026 expand_eh_region_start ();
1028 /* ??? This comment was old before the most recent rewrite. We
1029 really ought to fix the callers at some point. */
1030 /* For backwards compatibility, we allow callers to omit calls to
1031 begin_protect_partials for the outermost region. So, we must
1032 explicitly do so here. */
1033 if (!cfun->eh->protect_list)
1034 begin_protect_partials ();
1036 /* Add this entry to the front of the list. */
1037 TREE_VALUE (cfun->eh->protect_list)
1038 = tree_cons (NULL_TREE, handler, TREE_VALUE (cfun->eh->protect_list));
1041 /* End all the pending exception regions on protect_list. */
1043 void
1044 end_protect_partials ()
1046 tree t;
1048 /* ??? This comment was old before the most recent rewrite. We
1049 really ought to fix the callers at some point. */
1050 /* For backwards compatibility, we allow callers to omit the call to
1051 begin_protect_partials for the outermost region. So,
1052 PROTECT_LIST may be NULL. */
1053 if (!cfun->eh->protect_list)
1054 return;
1056 /* Pop the topmost entry. */
1057 t = TREE_VALUE (cfun->eh->protect_list);
1058 cfun->eh->protect_list = TREE_CHAIN (cfun->eh->protect_list);
1060 /* End all the exception regions. */
1061 for (; t; t = TREE_CHAIN (t))
1062 expand_eh_region_end_cleanup (TREE_VALUE (t));
1066 /* This section is for the exception handling specific optimization pass. */
1068 /* Random access the exception region tree. It's just as simple to
1069 collect the regions this way as in expand_eh_region_start, but
1070 without having to realloc memory. */
1072 static void
1073 collect_eh_region_array ()
1075 struct eh_region **array, *i;
1077 i = cfun->eh->region_tree;
1078 if (! i)
1079 return;
1081 array = xcalloc (cfun->eh->last_region_number + 1, sizeof (*array));
1082 cfun->eh->region_array = array;
1084 while (1)
1086 array[i->region_number] = i;
1088 /* If there are sub-regions, process them. */
1089 if (i->inner)
1090 i = i->inner;
1091 /* If there are peers, process them. */
1092 else if (i->next_peer)
1093 i = i->next_peer;
1094 /* Otherwise, step back up the tree to the next peer. */
1095 else
1097 do {
1098 i = i->outer;
1099 if (i == NULL)
1100 return;
1101 } while (i->next_peer == NULL);
1102 i = i->next_peer;
1107 static void
1108 resolve_fixup_regions ()
1110 int i, j, n = cfun->eh->last_region_number;
1112 for (i = 1; i <= n; ++i)
1114 struct eh_region *fixup = cfun->eh->region_array[i];
1115 struct eh_region *cleanup = 0;
1117 if (! fixup || fixup->type != ERT_FIXUP)
1118 continue;
1120 for (j = 1; j <= n; ++j)
1122 cleanup = cfun->eh->region_array[j];
1123 if (cleanup->type == ERT_CLEANUP
1124 && cleanup->u.cleanup.exp == fixup->u.fixup.cleanup_exp)
1125 break;
1127 if (j > n)
1128 abort ();
1130 fixup->u.fixup.real_region = cleanup->outer;
1134 /* Now that we've discovered what region actually encloses a fixup,
1135 we can shuffle pointers and remove them from the tree. */
1137 static void
1138 remove_fixup_regions ()
1140 int i;
1141 rtx insn, note;
1142 struct eh_region *fixup;
1144 /* Walk the insn chain and adjust the REG_EH_REGION numbers
1145 for instructions referencing fixup regions. This is only
1146 strictly necessary for fixup regions with no parent, but
1147 doesn't hurt to do it for all regions. */
1148 for (insn = get_insns(); insn ; insn = NEXT_INSN (insn))
1149 if (INSN_P (insn)
1150 && (note = find_reg_note (insn, REG_EH_REGION, NULL))
1151 && INTVAL (XEXP (note, 0)) > 0
1152 && (fixup = cfun->eh->region_array[INTVAL (XEXP (note, 0))])
1153 && fixup->type == ERT_FIXUP)
1155 if (fixup->u.fixup.real_region)
1156 XEXP (note, 0) = GEN_INT (fixup->u.fixup.real_region->region_number);
1157 else
1158 remove_note (insn, note);
1161 /* Remove the fixup regions from the tree. */
1162 for (i = cfun->eh->last_region_number; i > 0; --i)
1164 fixup = cfun->eh->region_array[i];
1165 if (! fixup)
1166 continue;
1168 /* Allow GC to maybe free some memory. */
1169 if (fixup->type == ERT_CLEANUP)
1170 fixup->u.cleanup.exp = NULL_TREE;
1172 if (fixup->type != ERT_FIXUP)
1173 continue;
1175 if (fixup->inner)
1177 struct eh_region *parent, *p, **pp;
1179 parent = fixup->u.fixup.real_region;
1181 /* Fix up the children's parent pointers; find the end of
1182 the list. */
1183 for (p = fixup->inner; ; p = p->next_peer)
1185 p->outer = parent;
1186 if (! p->next_peer)
1187 break;
1190 /* In the tree of cleanups, only outer-inner ordering matters.
1191 So link the children back in anywhere at the correct level. */
1192 if (parent)
1193 pp = &parent->inner;
1194 else
1195 pp = &cfun->eh->region_tree;
1196 p->next_peer = *pp;
1197 *pp = fixup->inner;
1198 fixup->inner = NULL;
1201 remove_eh_handler (fixup);
1205 /* Turn NOTE_INSN_EH_REGION notes into REG_EH_REGION notes for each
1206 can_throw instruction in the region. */
1208 static void
1209 convert_from_eh_region_ranges_1 (pinsns, orig_sp, cur)
1210 rtx *pinsns;
1211 int *orig_sp;
1212 int cur;
1214 int *sp = orig_sp;
1215 rtx insn, next;
1217 for (insn = *pinsns; insn ; insn = next)
1219 next = NEXT_INSN (insn);
1220 if (GET_CODE (insn) == NOTE)
1222 int kind = NOTE_LINE_NUMBER (insn);
1223 if (kind == NOTE_INSN_EH_REGION_BEG
1224 || kind == NOTE_INSN_EH_REGION_END)
1226 if (kind == NOTE_INSN_EH_REGION_BEG)
1228 struct eh_region *r;
1230 *sp++ = cur;
1231 cur = NOTE_EH_HANDLER (insn);
1233 r = cfun->eh->region_array[cur];
1234 if (r->type == ERT_FIXUP)
1236 r = r->u.fixup.real_region;
1237 cur = r ? r->region_number : 0;
1239 else if (r->type == ERT_CATCH)
1241 r = r->outer;
1242 cur = r ? r->region_number : 0;
1245 else
1246 cur = *--sp;
1248 /* Removing the first insn of a CALL_PLACEHOLDER sequence
1249 requires extra care to adjust sequence start. */
1250 if (insn == *pinsns)
1251 *pinsns = next;
1252 remove_insn (insn);
1253 continue;
1256 else if (INSN_P (insn))
1258 if (cur > 0
1259 && ! find_reg_note (insn, REG_EH_REGION, NULL_RTX)
1260 /* Calls can always potentially throw exceptions, unless
1261 they have a REG_EH_REGION note with a value of 0 or less.
1262 Which should be the only possible kind so far. */
1263 && (GET_CODE (insn) == CALL_INSN
1264 /* If we wanted exceptions for non-call insns, then
1265 any may_trap_p instruction could throw. */
1266 || (flag_non_call_exceptions
1267 && GET_CODE (PATTERN (insn)) != CLOBBER
1268 && GET_CODE (PATTERN (insn)) != USE
1269 && may_trap_p (PATTERN (insn)))))
1271 REG_NOTES (insn) = alloc_EXPR_LIST (REG_EH_REGION, GEN_INT (cur),
1272 REG_NOTES (insn));
1275 if (GET_CODE (insn) == CALL_INSN
1276 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
1278 convert_from_eh_region_ranges_1 (&XEXP (PATTERN (insn), 0),
1279 sp, cur);
1280 convert_from_eh_region_ranges_1 (&XEXP (PATTERN (insn), 1),
1281 sp, cur);
1282 convert_from_eh_region_ranges_1 (&XEXP (PATTERN (insn), 2),
1283 sp, cur);
1288 if (sp != orig_sp)
1289 abort ();
1292 void
1293 convert_from_eh_region_ranges ()
1295 int *stack;
1296 rtx insns;
1298 collect_eh_region_array ();
1299 resolve_fixup_regions ();
1301 stack = xmalloc (sizeof (int) * (cfun->eh->last_region_number + 1));
1302 insns = get_insns ();
1303 convert_from_eh_region_ranges_1 (&insns, stack, 0);
1304 free (stack);
1306 remove_fixup_regions ();
1309 void
1310 find_exception_handler_labels ()
1312 rtx list = NULL_RTX;
1313 int i;
1315 free_EXPR_LIST_list (&exception_handler_labels);
1317 if (cfun->eh->region_tree == NULL)
1318 return;
1320 for (i = cfun->eh->last_region_number; i > 0; --i)
1322 struct eh_region *region = cfun->eh->region_array[i];
1323 rtx lab;
1325 if (! region)
1326 continue;
1327 if (cfun->eh->built_landing_pads)
1328 lab = region->landing_pad;
1329 else
1330 lab = region->label;
1332 if (lab)
1333 list = alloc_EXPR_LIST (0, lab, list);
1336 /* For sjlj exceptions, need the return label to remain live until
1337 after landing pad generation. */
1338 if (USING_SJLJ_EXCEPTIONS && ! cfun->eh->built_landing_pads)
1339 list = alloc_EXPR_LIST (0, return_label, list);
1341 exception_handler_labels = list;
1345 static struct eh_region *
1346 duplicate_eh_region_1 (o, map)
1347 struct eh_region *o;
1348 struct inline_remap *map;
1350 struct eh_region *n
1351 = (struct eh_region *) xcalloc (1, sizeof (struct eh_region));
1353 n->region_number = o->region_number + cfun->eh->last_region_number;
1354 n->type = o->type;
1356 switch (n->type)
1358 case ERT_CLEANUP:
1359 case ERT_MUST_NOT_THROW:
1360 break;
1362 case ERT_TRY:
1363 if (o->u.try.continue_label)
1364 n->u.try.continue_label
1365 = get_label_from_map (map,
1366 CODE_LABEL_NUMBER (o->u.try.continue_label));
1367 break;
1369 case ERT_CATCH:
1370 n->u.catch.type_list = o->u.catch.type_list;
1371 break;
1373 case ERT_ALLOWED_EXCEPTIONS:
1374 n->u.allowed.type_list = o->u.allowed.type_list;
1375 break;
1377 case ERT_THROW:
1378 n->u.throw.type = o->u.throw.type;
1380 default:
1381 abort ();
1384 if (o->label)
1385 n->label = get_label_from_map (map, CODE_LABEL_NUMBER (o->label));
1386 if (o->resume)
1388 n->resume = map->insn_map[INSN_UID (o->resume)];
1389 if (n->resume == NULL)
1390 abort ();
1393 return n;
1396 static void
1397 duplicate_eh_region_2 (o, n_array)
1398 struct eh_region *o;
1399 struct eh_region **n_array;
1401 struct eh_region *n = n_array[o->region_number];
1403 switch (n->type)
1405 case ERT_TRY:
1406 n->u.try.catch = n_array[o->u.try.catch->region_number];
1407 n->u.try.last_catch = n_array[o->u.try.last_catch->region_number];
1408 break;
1410 case ERT_CATCH:
1411 if (o->u.catch.next_catch)
1412 n->u.catch.next_catch = n_array[o->u.catch.next_catch->region_number];
1413 if (o->u.catch.prev_catch)
1414 n->u.catch.prev_catch = n_array[o->u.catch.prev_catch->region_number];
1415 break;
1417 default:
1418 break;
1421 if (o->outer)
1422 n->outer = n_array[o->outer->region_number];
1423 if (o->inner)
1424 n->inner = n_array[o->inner->region_number];
1425 if (o->next_peer)
1426 n->next_peer = n_array[o->next_peer->region_number];
1430 duplicate_eh_regions (ifun, map)
1431 struct function *ifun;
1432 struct inline_remap *map;
1434 int ifun_last_region_number = ifun->eh->last_region_number;
1435 struct eh_region **n_array, *root, *cur;
1436 int i;
1438 if (ifun_last_region_number == 0)
1439 return 0;
1441 n_array = xcalloc (ifun_last_region_number + 1, sizeof (*n_array));
1443 for (i = 1; i <= ifun_last_region_number; ++i)
1445 cur = ifun->eh->region_array[i];
1446 if (!cur || cur->region_number != i)
1447 continue;
1448 n_array[i] = duplicate_eh_region_1 (cur, map);
1450 for (i = 1; i <= ifun_last_region_number; ++i)
1452 cur = ifun->eh->region_array[i];
1453 if (!cur || cur->region_number != i)
1454 continue;
1455 duplicate_eh_region_2 (cur, n_array);
1458 root = n_array[ifun->eh->region_tree->region_number];
1459 cur = cfun->eh->cur_region;
1460 if (cur)
1462 struct eh_region *p = cur->inner;
1463 if (p)
1465 while (p->next_peer)
1466 p = p->next_peer;
1467 p->next_peer = root;
1469 else
1470 cur->inner = root;
1472 for (i = 1; i <= ifun_last_region_number; ++i)
1473 if (n_array[i] && n_array[i]->outer == NULL)
1474 n_array[i]->outer = cur;
1476 else
1478 struct eh_region *p = cfun->eh->region_tree;
1479 if (p)
1481 while (p->next_peer)
1482 p = p->next_peer;
1483 p->next_peer = root;
1485 else
1486 cfun->eh->region_tree = root;
1489 free (n_array);
1491 i = cfun->eh->last_region_number;
1492 cfun->eh->last_region_number = i + ifun_last_region_number;
1493 return i;
1497 static int
1498 t2r_eq (pentry, pdata)
1499 const PTR pentry;
1500 const PTR pdata;
1502 tree entry = (tree) pentry;
1503 tree data = (tree) pdata;
1505 return TREE_PURPOSE (entry) == data;
1508 static hashval_t
1509 t2r_hash (pentry)
1510 const PTR pentry;
1512 tree entry = (tree) pentry;
1513 return TYPE_HASH (TREE_PURPOSE (entry));
1516 static int
1517 t2r_mark_1 (slot, data)
1518 PTR *slot;
1519 PTR data ATTRIBUTE_UNUSED;
1521 tree contents = (tree) *slot;
1522 ggc_mark_tree (contents);
1523 return 1;
1526 static void
1527 t2r_mark (addr)
1528 PTR addr;
1530 htab_traverse (*(htab_t *)addr, t2r_mark_1, NULL);
1533 static void
1534 add_type_for_runtime (type)
1535 tree type;
1537 tree *slot;
1539 slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
1540 TYPE_HASH (type), INSERT);
1541 if (*slot == NULL)
1543 tree runtime = (*lang_eh_runtime_type) (type);
1544 *slot = tree_cons (type, runtime, NULL_TREE);
1548 static tree
1549 lookup_type_for_runtime (type)
1550 tree type;
1552 tree *slot;
1554 slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
1555 TYPE_HASH (type), NO_INSERT);
1557 /* We should have always inserrted the data earlier. */
1558 return TREE_VALUE (*slot);
1562 /* Represent an entry in @TTypes for either catch actions
1563 or exception filter actions. */
1564 struct ttypes_filter
1566 tree t;
1567 int filter;
1570 /* Compare ENTRY (a ttypes_filter entry in the hash table) with DATA
1571 (a tree) for a @TTypes type node we are thinking about adding. */
1573 static int
1574 ttypes_filter_eq (pentry, pdata)
1575 const PTR pentry;
1576 const PTR pdata;
1578 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1579 tree data = (tree) pdata;
1581 return entry->t == data;
1584 static hashval_t
1585 ttypes_filter_hash (pentry)
1586 const PTR pentry;
1588 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1589 return TYPE_HASH (entry->t);
1592 /* Compare ENTRY with DATA (both struct ttypes_filter) for a @TTypes
1593 exception specification list we are thinking about adding. */
1594 /* ??? Currently we use the type lists in the order given. Someone
1595 should put these in some canonical order. */
1597 static int
1598 ehspec_filter_eq (pentry, pdata)
1599 const PTR pentry;
1600 const PTR pdata;
1602 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1603 const struct ttypes_filter *data = (const struct ttypes_filter *) pdata;
1605 return type_list_equal (entry->t, data->t);
1608 /* Hash function for exception specification lists. */
1610 static hashval_t
1611 ehspec_filter_hash (pentry)
1612 const PTR pentry;
1614 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1615 hashval_t h = 0;
1616 tree list;
1618 for (list = entry->t; list ; list = TREE_CHAIN (list))
1619 h = (h << 5) + (h >> 27) + TYPE_HASH (TREE_VALUE (list));
1620 return h;
1623 /* Add TYPE to cfun->eh->ttype_data, using TYPES_HASH to speed
1624 up the search. Return the filter value to be used. */
1626 static int
1627 add_ttypes_entry (ttypes_hash, type)
1628 htab_t ttypes_hash;
1629 tree type;
1631 struct ttypes_filter **slot, *n;
1633 slot = (struct ttypes_filter **)
1634 htab_find_slot_with_hash (ttypes_hash, type, TYPE_HASH (type), INSERT);
1636 if ((n = *slot) == NULL)
1638 /* Filter value is a 1 based table index. */
1640 n = (struct ttypes_filter *) xmalloc (sizeof (*n));
1641 n->t = type;
1642 n->filter = VARRAY_ACTIVE_SIZE (cfun->eh->ttype_data) + 1;
1643 *slot = n;
1645 VARRAY_PUSH_TREE (cfun->eh->ttype_data, type);
1648 return n->filter;
1651 /* Add LIST to cfun->eh->ehspec_data, using EHSPEC_HASH and TYPES_HASH
1652 to speed up the search. Return the filter value to be used. */
1654 static int
1655 add_ehspec_entry (ehspec_hash, ttypes_hash, list)
1656 htab_t ehspec_hash;
1657 htab_t ttypes_hash;
1658 tree list;
1660 struct ttypes_filter **slot, *n;
1661 struct ttypes_filter dummy;
1663 dummy.t = list;
1664 slot = (struct ttypes_filter **)
1665 htab_find_slot (ehspec_hash, &dummy, INSERT);
1667 if ((n = *slot) == NULL)
1669 /* Filter value is a -1 based byte index into a uleb128 buffer. */
1671 n = (struct ttypes_filter *) xmalloc (sizeof (*n));
1672 n->t = list;
1673 n->filter = -(VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data) + 1);
1674 *slot = n;
1676 /* Look up each type in the list and encode its filter
1677 value as a uleb128. Terminate the list with 0. */
1678 for (; list ; list = TREE_CHAIN (list))
1679 push_uleb128 (&cfun->eh->ehspec_data,
1680 add_ttypes_entry (ttypes_hash, TREE_VALUE (list)));
1681 VARRAY_PUSH_UCHAR (cfun->eh->ehspec_data, 0);
1684 return n->filter;
1687 /* Generate the action filter values to be used for CATCH and
1688 ALLOWED_EXCEPTIONS regions. When using dwarf2 exception regions,
1689 we use lots of landing pads, and so every type or list can share
1690 the same filter value, which saves table space. */
1692 static void
1693 assign_filter_values ()
1695 int i;
1696 htab_t ttypes, ehspec;
1698 VARRAY_TREE_INIT (cfun->eh->ttype_data, 16, "ttype_data");
1699 VARRAY_UCHAR_INIT (cfun->eh->ehspec_data, 64, "ehspec_data");
1701 ttypes = htab_create (31, ttypes_filter_hash, ttypes_filter_eq, free);
1702 ehspec = htab_create (31, ehspec_filter_hash, ehspec_filter_eq, free);
1704 for (i = cfun->eh->last_region_number; i > 0; --i)
1706 struct eh_region *r = cfun->eh->region_array[i];
1708 /* Mind we don't process a region more than once. */
1709 if (!r || r->region_number != i)
1710 continue;
1712 switch (r->type)
1714 case ERT_CATCH:
1715 /* Whatever type_list is (NULL or true list), we build a list
1716 of filters for the region. */
1717 r->u.catch.filter_list = NULL_TREE;
1719 if (r->u.catch.type_list != NULL)
1721 /* Get a filter value for each of the types caught and store
1722 them in the region's dedicated list. */
1723 tree tp_node = r->u.catch.type_list;
1725 for (;tp_node; tp_node = TREE_CHAIN (tp_node))
1727 int flt = add_ttypes_entry (ttypes, TREE_VALUE (tp_node));
1728 tree flt_node = build_int_2 (flt, 0);
1730 r->u.catch.filter_list
1731 = tree_cons (NULL_TREE, flt_node, r->u.catch.filter_list);
1734 else
1736 /* Get a filter value for the NULL list also since it will need
1737 an action record anyway. */
1738 int flt = add_ttypes_entry (ttypes, NULL);
1739 tree flt_node = build_int_2 (flt, 0);
1741 r->u.catch.filter_list
1742 = tree_cons (NULL_TREE, flt_node, r->u.catch.filter_list);
1745 break;
1747 case ERT_ALLOWED_EXCEPTIONS:
1748 r->u.allowed.filter
1749 = add_ehspec_entry (ehspec, ttypes, r->u.allowed.type_list);
1750 break;
1752 default:
1753 break;
1757 htab_delete (ttypes);
1758 htab_delete (ehspec);
1761 static void
1762 build_post_landing_pads ()
1764 int i;
1766 for (i = cfun->eh->last_region_number; i > 0; --i)
1768 struct eh_region *region = cfun->eh->region_array[i];
1769 rtx seq;
1771 /* Mind we don't process a region more than once. */
1772 if (!region || region->region_number != i)
1773 continue;
1775 switch (region->type)
1777 case ERT_TRY:
1778 /* ??? Collect the set of all non-overlapping catch handlers
1779 all the way up the chain until blocked by a cleanup. */
1780 /* ??? Outer try regions can share landing pads with inner
1781 try regions if the types are completely non-overlapping,
1782 and there are no interveaning cleanups. */
1784 region->post_landing_pad = gen_label_rtx ();
1786 start_sequence ();
1788 emit_label (region->post_landing_pad);
1790 /* ??? It is mighty inconvenient to call back into the
1791 switch statement generation code in expand_end_case.
1792 Rapid prototyping sez a sequence of ifs. */
1794 struct eh_region *c;
1795 for (c = region->u.try.catch; c ; c = c->u.catch.next_catch)
1797 /* ??? _Unwind_ForcedUnwind wants no match here. */
1798 if (c->u.catch.type_list == NULL)
1799 emit_jump (c->label);
1800 else
1802 /* Need for one cmp/jump per type caught. Each type
1803 list entry has a matching entry in the filter list
1804 (see assign_filter_values). */
1805 tree tp_node = c->u.catch.type_list;
1806 tree flt_node = c->u.catch.filter_list;
1808 for (; tp_node; )
1810 emit_cmp_and_jump_insns
1811 (cfun->eh->filter,
1812 GEN_INT (tree_low_cst (TREE_VALUE (flt_node), 0)),
1813 EQ, NULL_RTX, word_mode, 0, c->label);
1815 tp_node = TREE_CHAIN (tp_node);
1816 flt_node = TREE_CHAIN (flt_node);
1822 /* We delay the generation of the _Unwind_Resume until we generate
1823 landing pads. We emit a marker here so as to get good control
1824 flow data in the meantime. */
1825 region->resume
1826 = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
1827 emit_barrier ();
1829 seq = get_insns ();
1830 end_sequence ();
1832 emit_insns_before (seq, region->u.try.catch->label);
1833 break;
1835 case ERT_ALLOWED_EXCEPTIONS:
1836 region->post_landing_pad = gen_label_rtx ();
1838 start_sequence ();
1840 emit_label (region->post_landing_pad);
1842 emit_cmp_and_jump_insns (cfun->eh->filter,
1843 GEN_INT (region->u.allowed.filter),
1844 EQ, NULL_RTX, word_mode, 0, region->label);
1846 /* We delay the generation of the _Unwind_Resume until we generate
1847 landing pads. We emit a marker here so as to get good control
1848 flow data in the meantime. */
1849 region->resume
1850 = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
1851 emit_barrier ();
1853 seq = get_insns ();
1854 end_sequence ();
1856 emit_insns_before (seq, region->label);
1857 break;
1859 case ERT_CLEANUP:
1860 case ERT_MUST_NOT_THROW:
1861 region->post_landing_pad = region->label;
1862 break;
1864 case ERT_CATCH:
1865 case ERT_THROW:
1866 /* Nothing to do. */
1867 break;
1869 default:
1870 abort ();
1875 /* Replace RESX patterns with jumps to the next handler if any, or calls to
1876 _Unwind_Resume otherwise. */
1878 static void
1879 connect_post_landing_pads ()
1881 int i;
1883 for (i = cfun->eh->last_region_number; i > 0; --i)
1885 struct eh_region *region = cfun->eh->region_array[i];
1886 struct eh_region *outer;
1887 rtx seq;
1889 /* Mind we don't process a region more than once. */
1890 if (!region || region->region_number != i)
1891 continue;
1893 /* If there is no RESX, or it has been deleted by flow, there's
1894 nothing to fix up. */
1895 if (! region->resume || INSN_DELETED_P (region->resume))
1896 continue;
1898 /* Search for another landing pad in this function. */
1899 for (outer = region->outer; outer ; outer = outer->outer)
1900 if (outer->post_landing_pad)
1901 break;
1903 start_sequence ();
1905 if (outer)
1906 emit_jump (outer->post_landing_pad);
1907 else
1908 emit_library_call (unwind_resume_libfunc, LCT_THROW,
1909 VOIDmode, 1, cfun->eh->exc_ptr, Pmode);
1911 seq = get_insns ();
1912 end_sequence ();
1913 emit_insns_before (seq, region->resume);
1914 delete_insn (region->resume);
1919 static void
1920 dw2_build_landing_pads ()
1922 int i;
1923 unsigned int j;
1925 for (i = cfun->eh->last_region_number; i > 0; --i)
1927 struct eh_region *region = cfun->eh->region_array[i];
1928 rtx seq;
1929 bool clobbers_hard_regs = false;
1931 /* Mind we don't process a region more than once. */
1932 if (!region || region->region_number != i)
1933 continue;
1935 if (region->type != ERT_CLEANUP
1936 && region->type != ERT_TRY
1937 && region->type != ERT_ALLOWED_EXCEPTIONS)
1938 continue;
1940 start_sequence ();
1942 region->landing_pad = gen_label_rtx ();
1943 emit_label (region->landing_pad);
1945 #ifdef HAVE_exception_receiver
1946 if (HAVE_exception_receiver)
1947 emit_insn (gen_exception_receiver ());
1948 else
1949 #endif
1950 #ifdef HAVE_nonlocal_goto_receiver
1951 if (HAVE_nonlocal_goto_receiver)
1952 emit_insn (gen_nonlocal_goto_receiver ());
1953 else
1954 #endif
1955 { /* Nothing */ }
1957 /* If the eh_return data registers are call-saved, then we
1958 won't have considered them clobbered from the call that
1959 threw. Kill them now. */
1960 for (j = 0; ; ++j)
1962 unsigned r = EH_RETURN_DATA_REGNO (j);
1963 if (r == INVALID_REGNUM)
1964 break;
1965 if (! call_used_regs[r])
1967 emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, r)));
1968 clobbers_hard_regs = true;
1972 if (clobbers_hard_regs)
1974 /* @@@ This is a kludge. Not all machine descriptions define a
1975 blockage insn, but we must not allow the code we just generated
1976 to be reordered by scheduling. So emit an ASM_INPUT to act as
1977 blockage insn. */
1978 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
1981 emit_move_insn (cfun->eh->exc_ptr,
1982 gen_rtx_REG (Pmode, EH_RETURN_DATA_REGNO (0)));
1983 emit_move_insn (cfun->eh->filter,
1984 gen_rtx_REG (word_mode, EH_RETURN_DATA_REGNO (1)));
1986 seq = get_insns ();
1987 end_sequence ();
1989 emit_insns_before (seq, region->post_landing_pad);
1994 struct sjlj_lp_info
1996 int directly_reachable;
1997 int action_index;
1998 int dispatch_index;
1999 int call_site_index;
2002 static bool
2003 sjlj_find_directly_reachable_regions (lp_info)
2004 struct sjlj_lp_info *lp_info;
2006 rtx insn;
2007 bool found_one = false;
2009 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
2011 struct eh_region *region;
2012 tree type_thrown;
2013 rtx note;
2015 if (! INSN_P (insn))
2016 continue;
2018 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2019 if (!note || INTVAL (XEXP (note, 0)) <= 0)
2020 continue;
2022 region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
2024 type_thrown = NULL_TREE;
2025 if (region->type == ERT_THROW)
2027 type_thrown = region->u.throw.type;
2028 region = region->outer;
2031 /* Find the first containing region that might handle the exception.
2032 That's the landing pad to which we will transfer control. */
2033 for (; region; region = region->outer)
2034 if (reachable_next_level (region, type_thrown, 0) != RNL_NOT_CAUGHT)
2035 break;
2037 if (region)
2039 lp_info[region->region_number].directly_reachable = 1;
2040 found_one = true;
2044 return found_one;
2047 static void
2048 sjlj_assign_call_site_values (dispatch_label, lp_info)
2049 rtx dispatch_label;
2050 struct sjlj_lp_info *lp_info;
2052 htab_t ar_hash;
2053 int i, index;
2055 /* First task: build the action table. */
2057 VARRAY_UCHAR_INIT (cfun->eh->action_record_data, 64, "action_record_data");
2058 ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
2060 for (i = cfun->eh->last_region_number; i > 0; --i)
2061 if (lp_info[i].directly_reachable)
2063 struct eh_region *r = cfun->eh->region_array[i];
2064 r->landing_pad = dispatch_label;
2065 lp_info[i].action_index = collect_one_action_chain (ar_hash, r);
2066 if (lp_info[i].action_index != -1)
2067 cfun->uses_eh_lsda = 1;
2070 htab_delete (ar_hash);
2072 /* Next: assign dispatch values. In dwarf2 terms, this would be the
2073 landing pad label for the region. For sjlj though, there is one
2074 common landing pad from which we dispatch to the post-landing pads.
2076 A region receives a dispatch index if it is directly reachable
2077 and requires in-function processing. Regions that share post-landing
2078 pads may share dispatch indices. */
2079 /* ??? Post-landing pad sharing doesn't actually happen at the moment
2080 (see build_post_landing_pads) so we don't bother checking for it. */
2082 index = 0;
2083 for (i = cfun->eh->last_region_number; i > 0; --i)
2084 if (lp_info[i].directly_reachable
2085 && lp_info[i].action_index >= 0)
2086 lp_info[i].dispatch_index = index++;
2088 /* Finally: assign call-site values. If dwarf2 terms, this would be
2089 the region number assigned by convert_to_eh_region_ranges, but
2090 handles no-action and must-not-throw differently. */
2092 call_site_base = 1;
2093 for (i = cfun->eh->last_region_number; i > 0; --i)
2094 if (lp_info[i].directly_reachable)
2096 int action = lp_info[i].action_index;
2098 /* Map must-not-throw to otherwise unused call-site index 0. */
2099 if (action == -2)
2100 index = 0;
2101 /* Map no-action to otherwise unused call-site index -1. */
2102 else if (action == -1)
2103 index = -1;
2104 /* Otherwise, look it up in the table. */
2105 else
2106 index = add_call_site (GEN_INT (lp_info[i].dispatch_index), action);
2108 lp_info[i].call_site_index = index;
2112 static void
2113 sjlj_mark_call_sites (lp_info)
2114 struct sjlj_lp_info *lp_info;
2116 int last_call_site = -2;
2117 rtx insn, mem;
2119 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
2121 struct eh_region *region;
2122 int this_call_site;
2123 rtx note, before, p;
2125 /* Reset value tracking at extended basic block boundaries. */
2126 if (GET_CODE (insn) == CODE_LABEL)
2127 last_call_site = -2;
2129 if (! INSN_P (insn))
2130 continue;
2132 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2133 if (!note)
2135 /* Calls (and trapping insns) without notes are outside any
2136 exception handling region in this function. Mark them as
2137 no action. */
2138 if (GET_CODE (insn) == CALL_INSN
2139 || (flag_non_call_exceptions
2140 && may_trap_p (PATTERN (insn))))
2141 this_call_site = -1;
2142 else
2143 continue;
2145 else
2147 /* Calls that are known to not throw need not be marked. */
2148 if (INTVAL (XEXP (note, 0)) <= 0)
2149 continue;
2151 region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
2152 this_call_site = lp_info[region->region_number].call_site_index;
2155 if (this_call_site == last_call_site)
2156 continue;
2158 /* Don't separate a call from it's argument loads. */
2159 before = insn;
2160 if (GET_CODE (insn) == CALL_INSN)
2161 before = find_first_parameter_load (insn, NULL_RTX);
2163 start_sequence ();
2164 mem = adjust_address (cfun->eh->sjlj_fc, TYPE_MODE (integer_type_node),
2165 sjlj_fc_call_site_ofs);
2166 emit_move_insn (mem, GEN_INT (this_call_site));
2167 p = get_insns ();
2168 end_sequence ();
2170 emit_insns_before (p, before);
2171 last_call_site = this_call_site;
2175 /* Construct the SjLj_Function_Context. */
2177 static void
2178 sjlj_emit_function_enter (dispatch_label)
2179 rtx dispatch_label;
2181 rtx fn_begin, fc, mem, seq;
2183 fc = cfun->eh->sjlj_fc;
2185 start_sequence ();
2187 /* We're storing this libcall's address into memory instead of
2188 calling it directly. Thus, we must call assemble_external_libcall
2189 here, as we can not depend on emit_library_call to do it for us. */
2190 assemble_external_libcall (eh_personality_libfunc);
2191 mem = adjust_address (fc, Pmode, sjlj_fc_personality_ofs);
2192 emit_move_insn (mem, eh_personality_libfunc);
2194 mem = adjust_address (fc, Pmode, sjlj_fc_lsda_ofs);
2195 if (cfun->uses_eh_lsda)
2197 char buf[20];
2198 ASM_GENERATE_INTERNAL_LABEL (buf, "LLSDA", sjlj_funcdef_number);
2199 emit_move_insn (mem, gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf)));
2201 else
2202 emit_move_insn (mem, const0_rtx);
2204 #ifdef DONT_USE_BUILTIN_SETJMP
2206 rtx x, note;
2207 x = emit_library_call_value (setjmp_libfunc, NULL_RTX, LCT_RETURNS_TWICE,
2208 TYPE_MODE (integer_type_node), 1,
2209 plus_constant (XEXP (fc, 0),
2210 sjlj_fc_jbuf_ofs), Pmode);
2212 note = emit_note (NULL, NOTE_INSN_EXPECTED_VALUE);
2213 NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, x, const0_rtx);
2215 emit_cmp_and_jump_insns (x, const0_rtx, NE, 0,
2216 TYPE_MODE (integer_type_node), 0, dispatch_label);
2218 #else
2219 expand_builtin_setjmp_setup (plus_constant (XEXP (fc, 0), sjlj_fc_jbuf_ofs),
2220 dispatch_label);
2221 #endif
2223 emit_library_call (unwind_sjlj_register_libfunc, LCT_NORMAL, VOIDmode,
2224 1, XEXP (fc, 0), Pmode);
2226 seq = get_insns ();
2227 end_sequence ();
2229 /* ??? Instead of doing this at the beginning of the function,
2230 do this in a block that is at loop level 0 and dominates all
2231 can_throw_internal instructions. */
2233 for (fn_begin = get_insns (); ; fn_begin = NEXT_INSN (fn_begin))
2234 if (GET_CODE (fn_begin) == NOTE
2235 && NOTE_LINE_NUMBER (fn_begin) == NOTE_INSN_FUNCTION_BEG)
2236 break;
2237 emit_insns_after (seq, fn_begin);
2240 /* Call back from expand_function_end to know where we should put
2241 the call to unwind_sjlj_unregister_libfunc if needed. */
2243 void
2244 sjlj_emit_function_exit_after (after)
2245 rtx after;
2247 cfun->eh->sjlj_exit_after = after;
2250 static void
2251 sjlj_emit_function_exit ()
2253 rtx seq;
2255 start_sequence ();
2257 emit_library_call (unwind_sjlj_unregister_libfunc, LCT_NORMAL, VOIDmode,
2258 1, XEXP (cfun->eh->sjlj_fc, 0), Pmode);
2260 seq = get_insns ();
2261 end_sequence ();
2263 /* ??? Really this can be done in any block at loop level 0 that
2264 post-dominates all can_throw_internal instructions. This is
2265 the last possible moment. */
2267 emit_insns_after (seq, cfun->eh->sjlj_exit_after);
2270 static void
2271 sjlj_emit_dispatch_table (dispatch_label, lp_info)
2272 rtx dispatch_label;
2273 struct sjlj_lp_info *lp_info;
2275 int i, first_reachable;
2276 rtx mem, dispatch, seq, fc;
2278 fc = cfun->eh->sjlj_fc;
2280 start_sequence ();
2282 emit_label (dispatch_label);
2284 #ifndef DONT_USE_BUILTIN_SETJMP
2285 expand_builtin_setjmp_receiver (dispatch_label);
2286 #endif
2288 /* Load up dispatch index, exc_ptr and filter values from the
2289 function context. */
2290 mem = adjust_address (fc, TYPE_MODE (integer_type_node),
2291 sjlj_fc_call_site_ofs);
2292 dispatch = copy_to_reg (mem);
2294 mem = adjust_address (fc, word_mode, sjlj_fc_data_ofs);
2295 if (word_mode != Pmode)
2297 #ifdef POINTERS_EXTEND_UNSIGNED
2298 mem = convert_memory_address (Pmode, mem);
2299 #else
2300 mem = convert_to_mode (Pmode, mem, 0);
2301 #endif
2303 emit_move_insn (cfun->eh->exc_ptr, mem);
2305 mem = adjust_address (fc, word_mode, sjlj_fc_data_ofs + UNITS_PER_WORD);
2306 emit_move_insn (cfun->eh->filter, mem);
2308 /* Jump to one of the directly reachable regions. */
2309 /* ??? This really ought to be using a switch statement. */
2311 first_reachable = 0;
2312 for (i = cfun->eh->last_region_number; i > 0; --i)
2314 if (! lp_info[i].directly_reachable
2315 || lp_info[i].action_index < 0)
2316 continue;
2318 if (! first_reachable)
2320 first_reachable = i;
2321 continue;
2324 emit_cmp_and_jump_insns (dispatch, GEN_INT (lp_info[i].dispatch_index),
2325 EQ, NULL_RTX, TYPE_MODE (integer_type_node), 0,
2326 cfun->eh->region_array[i]->post_landing_pad);
2329 seq = get_insns ();
2330 end_sequence ();
2332 emit_insns_before (seq, (cfun->eh->region_array[first_reachable]
2333 ->post_landing_pad));
2336 static void
2337 sjlj_build_landing_pads ()
2339 struct sjlj_lp_info *lp_info;
2341 lp_info = (struct sjlj_lp_info *) xcalloc (cfun->eh->last_region_number + 1,
2342 sizeof (struct sjlj_lp_info));
2344 if (sjlj_find_directly_reachable_regions (lp_info))
2346 rtx dispatch_label = gen_label_rtx ();
2348 cfun->eh->sjlj_fc
2349 = assign_stack_local (TYPE_MODE (sjlj_fc_type_node),
2350 int_size_in_bytes (sjlj_fc_type_node),
2351 TYPE_ALIGN (sjlj_fc_type_node));
2353 sjlj_assign_call_site_values (dispatch_label, lp_info);
2354 sjlj_mark_call_sites (lp_info);
2356 sjlj_emit_function_enter (dispatch_label);
2357 sjlj_emit_dispatch_table (dispatch_label, lp_info);
2358 sjlj_emit_function_exit ();
2361 free (lp_info);
2364 void
2365 finish_eh_generation ()
2367 /* Nothing to do if no regions created. */
2368 if (cfun->eh->region_tree == NULL)
2369 return;
2371 /* The object here is to provide find_basic_blocks with detailed
2372 information (via reachable_handlers) on how exception control
2373 flows within the function. In this first pass, we can include
2374 type information garnered from ERT_THROW and ERT_ALLOWED_EXCEPTIONS
2375 regions, and hope that it will be useful in deleting unreachable
2376 handlers. Subsequently, we will generate landing pads which will
2377 connect many of the handlers, and then type information will not
2378 be effective. Still, this is a win over previous implementations. */
2380 rebuild_jump_labels (get_insns ());
2381 find_basic_blocks (get_insns (), max_reg_num (), 0);
2382 cleanup_cfg (CLEANUP_PRE_LOOP);
2384 /* These registers are used by the landing pads. Make sure they
2385 have been generated. */
2386 get_exception_pointer (cfun);
2387 get_exception_filter (cfun);
2389 /* Construct the landing pads. */
2391 assign_filter_values ();
2392 build_post_landing_pads ();
2393 connect_post_landing_pads ();
2394 if (USING_SJLJ_EXCEPTIONS)
2395 sjlj_build_landing_pads ();
2396 else
2397 dw2_build_landing_pads ();
2399 cfun->eh->built_landing_pads = 1;
2401 /* We've totally changed the CFG. Start over. */
2402 find_exception_handler_labels ();
2403 rebuild_jump_labels (get_insns ());
2404 find_basic_blocks (get_insns (), max_reg_num (), 0);
2405 cleanup_cfg (CLEANUP_PRE_LOOP);
2408 /* This section handles removing dead code for flow. */
2410 /* Remove LABEL from the exception_handler_labels list. */
2412 static void
2413 remove_exception_handler_label (label)
2414 rtx label;
2416 rtx *pl, l;
2418 for (pl = &exception_handler_labels, l = *pl;
2419 XEXP (l, 0) != label;
2420 pl = &XEXP (l, 1), l = *pl)
2421 continue;
2423 *pl = XEXP (l, 1);
2424 free_EXPR_LIST_node (l);
2427 /* Splice REGION from the region tree etc. */
2429 static void
2430 remove_eh_handler (region)
2431 struct eh_region *region;
2433 struct eh_region **pp, *p;
2434 rtx lab;
2435 int i;
2437 /* For the benefit of efficiently handling REG_EH_REGION notes,
2438 replace this region in the region array with its containing
2439 region. Note that previous region deletions may result in
2440 multiple copies of this region in the array, so we have to
2441 search the whole thing. */
2442 for (i = cfun->eh->last_region_number; i > 0; --i)
2443 if (cfun->eh->region_array[i] == region)
2444 cfun->eh->region_array[i] = region->outer;
2446 if (cfun->eh->built_landing_pads)
2447 lab = region->landing_pad;
2448 else
2449 lab = region->label;
2450 if (lab)
2451 remove_exception_handler_label (lab);
2453 if (region->outer)
2454 pp = &region->outer->inner;
2455 else
2456 pp = &cfun->eh->region_tree;
2457 for (p = *pp; p != region; pp = &p->next_peer, p = *pp)
2458 continue;
2460 if (region->inner)
2462 for (p = region->inner; p->next_peer ; p = p->next_peer)
2463 p->outer = region->outer;
2464 p->next_peer = region->next_peer;
2465 p->outer = region->outer;
2466 *pp = region->inner;
2468 else
2469 *pp = region->next_peer;
2471 if (region->type == ERT_CATCH)
2473 struct eh_region *try, *next, *prev;
2475 for (try = region->next_peer;
2476 try->type == ERT_CATCH;
2477 try = try->next_peer)
2478 continue;
2479 if (try->type != ERT_TRY)
2480 abort ();
2482 next = region->u.catch.next_catch;
2483 prev = region->u.catch.prev_catch;
2485 if (next)
2486 next->u.catch.prev_catch = prev;
2487 else
2488 try->u.try.last_catch = prev;
2489 if (prev)
2490 prev->u.catch.next_catch = next;
2491 else
2493 try->u.try.catch = next;
2494 if (! next)
2495 remove_eh_handler (try);
2499 free (region);
2502 /* LABEL heads a basic block that is about to be deleted. If this
2503 label corresponds to an exception region, we may be able to
2504 delete the region. */
2506 void
2507 maybe_remove_eh_handler (label)
2508 rtx label;
2510 int i;
2512 /* ??? After generating landing pads, it's not so simple to determine
2513 if the region data is completely unused. One must examine the
2514 landing pad and the post landing pad, and whether an inner try block
2515 is referencing the catch handlers directly. */
2516 if (cfun->eh->built_landing_pads)
2517 return;
2519 for (i = cfun->eh->last_region_number; i > 0; --i)
2521 struct eh_region *region = cfun->eh->region_array[i];
2522 if (region && region->label == label)
2524 /* Flow will want to remove MUST_NOT_THROW regions as unreachable
2525 because there is no path to the fallback call to terminate.
2526 But the region continues to affect call-site data until there
2527 are no more contained calls, which we don't see here. */
2528 if (region->type == ERT_MUST_NOT_THROW)
2530 remove_exception_handler_label (region->label);
2531 region->label = NULL_RTX;
2533 else
2534 remove_eh_handler (region);
2535 break;
2541 /* This section describes CFG exception edges for flow. */
2543 /* For communicating between calls to reachable_next_level. */
2544 struct reachable_info
2546 tree types_caught;
2547 tree types_allowed;
2548 rtx handlers;
2551 /* A subroutine of reachable_next_level. Return true if TYPE, or a
2552 base class of TYPE, is in HANDLED. */
2554 static int
2555 check_handled (handled, type)
2556 tree handled, type;
2558 tree t;
2560 /* We can check for exact matches without front-end help. */
2561 if (! lang_eh_type_covers)
2563 for (t = handled; t ; t = TREE_CHAIN (t))
2564 if (TREE_VALUE (t) == type)
2565 return 1;
2567 else
2569 for (t = handled; t ; t = TREE_CHAIN (t))
2570 if ((*lang_eh_type_covers) (TREE_VALUE (t), type))
2571 return 1;
2574 return 0;
2577 /* A subroutine of reachable_next_level. If we are collecting a list
2578 of handlers, add one. After landing pad generation, reference
2579 it instead of the handlers themselves. Further, the handlers are
2580 all wired together, so by referencing one, we've got them all.
2581 Before landing pad generation we reference each handler individually.
2583 LP_REGION contains the landing pad; REGION is the handler. */
2585 static void
2586 add_reachable_handler (info, lp_region, region)
2587 struct reachable_info *info;
2588 struct eh_region *lp_region;
2589 struct eh_region *region;
2591 if (! info)
2592 return;
2594 if (cfun->eh->built_landing_pads)
2596 if (! info->handlers)
2597 info->handlers = alloc_INSN_LIST (lp_region->landing_pad, NULL_RTX);
2599 else
2600 info->handlers = alloc_INSN_LIST (region->label, info->handlers);
2603 /* Process one level of exception regions for reachability.
2604 If TYPE_THROWN is non-null, then it is the *exact* type being
2605 propagated. If INFO is non-null, then collect handler labels
2606 and caught/allowed type information between invocations. */
2608 static enum reachable_code
2609 reachable_next_level (region, type_thrown, info)
2610 struct eh_region *region;
2611 tree type_thrown;
2612 struct reachable_info *info;
2614 switch (region->type)
2616 case ERT_CLEANUP:
2617 /* Before landing-pad generation, we model control flow
2618 directly to the individual handlers. In this way we can
2619 see that catch handler types may shadow one another. */
2620 add_reachable_handler (info, region, region);
2621 return RNL_MAYBE_CAUGHT;
2623 case ERT_TRY:
2625 struct eh_region *c;
2626 enum reachable_code ret = RNL_NOT_CAUGHT;
2628 for (c = region->u.try.catch; c ; c = c->u.catch.next_catch)
2630 /* A catch-all handler ends the search. */
2631 /* ??? _Unwind_ForcedUnwind will want outer cleanups
2632 to be run as well. */
2633 if (c->u.catch.type_list == NULL)
2635 add_reachable_handler (info, region, c);
2636 return RNL_CAUGHT;
2639 if (type_thrown)
2641 /* If we have a at least one type match, end the search. */
2642 tree tp_node = c->u.catch.type_list;
2644 for (; tp_node; tp_node = TREE_CHAIN (tp_node))
2646 tree type = TREE_VALUE (tp_node);
2648 if (type == type_thrown
2649 || (lang_eh_type_covers
2650 && (*lang_eh_type_covers) (type, type_thrown)))
2652 add_reachable_handler (info, region, c);
2653 return RNL_CAUGHT;
2657 /* If we have definitive information of a match failure,
2658 the catch won't trigger. */
2659 if (lang_eh_type_covers)
2660 return RNL_NOT_CAUGHT;
2663 /* At this point, we either don't know what type is thrown or
2664 don't have front-end assistance to help deciding if it is
2665 covered by one of the types in the list for this region.
2667 We'd then like to add this region to the list of reachable
2668 handlers since it is indeed potentially reachable based on the
2669 information we have.
2671 Actually, this handler is for sure not reachable if all the
2672 types it matches have already been caught. That is, it is only
2673 potentially reachable if at least one of the types it catches
2674 has not been previously caught. */
2676 if (! info)
2677 ret = RNL_MAYBE_CAUGHT;
2678 else
2680 tree tp_node = c->u.catch.type_list;
2681 bool maybe_reachable = false;
2683 /* Compute the potential reachability of this handler and
2684 update the list of types caught at the same time. */
2685 for (; tp_node; tp_node = TREE_CHAIN (tp_node))
2687 tree type = TREE_VALUE (tp_node);
2689 if (! check_handled (info->types_caught, type))
2691 info->types_caught
2692 = tree_cons (NULL, type, info->types_caught);
2694 maybe_reachable = true;
2698 if (maybe_reachable)
2700 add_reachable_handler (info, region, c);
2702 /* ??? If the catch type is a base class of every allowed
2703 type, then we know we can stop the search. */
2704 ret = RNL_MAYBE_CAUGHT;
2709 return ret;
2712 case ERT_ALLOWED_EXCEPTIONS:
2713 /* An empty list of types definitely ends the search. */
2714 if (region->u.allowed.type_list == NULL_TREE)
2716 add_reachable_handler (info, region, region);
2717 return RNL_CAUGHT;
2720 /* Collect a list of lists of allowed types for use in detecting
2721 when a catch may be transformed into a catch-all. */
2722 if (info)
2723 info->types_allowed = tree_cons (NULL_TREE,
2724 region->u.allowed.type_list,
2725 info->types_allowed);
2727 /* If we have definitive information about the type hierarchy,
2728 then we can tell if the thrown type will pass through the
2729 filter. */
2730 if (type_thrown && lang_eh_type_covers)
2732 if (check_handled (region->u.allowed.type_list, type_thrown))
2733 return RNL_NOT_CAUGHT;
2734 else
2736 add_reachable_handler (info, region, region);
2737 return RNL_CAUGHT;
2741 add_reachable_handler (info, region, region);
2742 return RNL_MAYBE_CAUGHT;
2744 case ERT_CATCH:
2745 /* Catch regions are handled by their controling try region. */
2746 return RNL_NOT_CAUGHT;
2748 case ERT_MUST_NOT_THROW:
2749 /* Here we end our search, since no exceptions may propagate.
2750 If we've touched down at some landing pad previous, then the
2751 explicit function call we generated may be used. Otherwise
2752 the call is made by the runtime. */
2753 if (info && info->handlers)
2755 add_reachable_handler (info, region, region);
2756 return RNL_CAUGHT;
2758 else
2759 return RNL_BLOCKED;
2761 case ERT_THROW:
2762 case ERT_FIXUP:
2763 /* Shouldn't see these here. */
2764 break;
2767 abort ();
2770 /* Retrieve a list of labels of exception handlers which can be
2771 reached by a given insn. */
2774 reachable_handlers (insn)
2775 rtx insn;
2777 struct reachable_info info;
2778 struct eh_region *region;
2779 tree type_thrown;
2780 int region_number;
2782 if (GET_CODE (insn) == JUMP_INSN
2783 && GET_CODE (PATTERN (insn)) == RESX)
2784 region_number = XINT (PATTERN (insn), 0);
2785 else
2787 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2788 if (!note || INTVAL (XEXP (note, 0)) <= 0)
2789 return NULL;
2790 region_number = INTVAL (XEXP (note, 0));
2793 memset (&info, 0, sizeof (info));
2795 region = cfun->eh->region_array[region_number];
2797 type_thrown = NULL_TREE;
2798 if (GET_CODE (insn) == JUMP_INSN
2799 && GET_CODE (PATTERN (insn)) == RESX)
2801 /* A RESX leaves a region instead of entering it. Thus the
2802 region itself may have been deleted out from under us. */
2803 if (region == NULL)
2804 return NULL;
2805 region = region->outer;
2807 else if (region->type == ERT_THROW)
2809 type_thrown = region->u.throw.type;
2810 region = region->outer;
2813 for (; region; region = region->outer)
2814 if (reachable_next_level (region, type_thrown, &info) >= RNL_CAUGHT)
2815 break;
2817 return info.handlers;
2820 /* Determine if the given INSN can throw an exception that is caught
2821 within the function. */
2823 bool
2824 can_throw_internal (insn)
2825 rtx insn;
2827 struct eh_region *region;
2828 tree type_thrown;
2829 rtx note;
2831 if (! INSN_P (insn))
2832 return false;
2834 if (GET_CODE (insn) == INSN
2835 && GET_CODE (PATTERN (insn)) == SEQUENCE)
2836 insn = XVECEXP (PATTERN (insn), 0, 0);
2838 if (GET_CODE (insn) == CALL_INSN
2839 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
2841 int i;
2842 for (i = 0; i < 3; ++i)
2844 rtx sub = XEXP (PATTERN (insn), i);
2845 for (; sub ; sub = NEXT_INSN (sub))
2846 if (can_throw_internal (sub))
2847 return true;
2849 return false;
2852 /* Every insn that might throw has an EH_REGION note. */
2853 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2854 if (!note || INTVAL (XEXP (note, 0)) <= 0)
2855 return false;
2857 region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
2859 type_thrown = NULL_TREE;
2860 if (region->type == ERT_THROW)
2862 type_thrown = region->u.throw.type;
2863 region = region->outer;
2866 /* If this exception is ignored by each and every containing region,
2867 then control passes straight out. The runtime may handle some
2868 regions, which also do not require processing internally. */
2869 for (; region; region = region->outer)
2871 enum reachable_code how = reachable_next_level (region, type_thrown, 0);
2872 if (how == RNL_BLOCKED)
2873 return false;
2874 if (how != RNL_NOT_CAUGHT)
2875 return true;
2878 return false;
2881 /* Determine if the given INSN can throw an exception that is
2882 visible outside the function. */
2884 bool
2885 can_throw_external (insn)
2886 rtx insn;
2888 struct eh_region *region;
2889 tree type_thrown;
2890 rtx note;
2892 if (! INSN_P (insn))
2893 return false;
2895 if (GET_CODE (insn) == INSN
2896 && GET_CODE (PATTERN (insn)) == SEQUENCE)
2897 insn = XVECEXP (PATTERN (insn), 0, 0);
2899 if (GET_CODE (insn) == CALL_INSN
2900 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
2902 int i;
2903 for (i = 0; i < 3; ++i)
2905 rtx sub = XEXP (PATTERN (insn), i);
2906 for (; sub ; sub = NEXT_INSN (sub))
2907 if (can_throw_external (sub))
2908 return true;
2910 return false;
2913 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2914 if (!note)
2916 /* Calls (and trapping insns) without notes are outside any
2917 exception handling region in this function. We have to
2918 assume it might throw. Given that the front end and middle
2919 ends mark known NOTHROW functions, this isn't so wildly
2920 inaccurate. */
2921 return (GET_CODE (insn) == CALL_INSN
2922 || (flag_non_call_exceptions
2923 && may_trap_p (PATTERN (insn))));
2925 if (INTVAL (XEXP (note, 0)) <= 0)
2926 return false;
2928 region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
2930 type_thrown = NULL_TREE;
2931 if (region->type == ERT_THROW)
2933 type_thrown = region->u.throw.type;
2934 region = region->outer;
2937 /* If the exception is caught or blocked by any containing region,
2938 then it is not seen by any calling function. */
2939 for (; region ; region = region->outer)
2940 if (reachable_next_level (region, type_thrown, NULL) >= RNL_CAUGHT)
2941 return false;
2943 return true;
2946 /* True if nothing in this function can throw outside this function. */
2948 bool
2949 nothrow_function_p ()
2951 rtx insn;
2953 if (! flag_exceptions)
2954 return true;
2956 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2957 if (can_throw_external (insn))
2958 return false;
2959 for (insn = current_function_epilogue_delay_list; insn;
2960 insn = XEXP (insn, 1))
2961 if (can_throw_external (insn))
2962 return false;
2964 return true;
2968 /* Various hooks for unwind library. */
2970 /* Do any necessary initialization to access arbitrary stack frames.
2971 On the SPARC, this means flushing the register windows. */
2973 void
2974 expand_builtin_unwind_init ()
2976 /* Set this so all the registers get saved in our frame; we need to be
2977 able to copy the saved values for any registers from frames we unwind. */
2978 current_function_has_nonlocal_label = 1;
2980 #ifdef SETUP_FRAME_ADDRESSES
2981 SETUP_FRAME_ADDRESSES ();
2982 #endif
2986 expand_builtin_eh_return_data_regno (arglist)
2987 tree arglist;
2989 tree which = TREE_VALUE (arglist);
2990 unsigned HOST_WIDE_INT iwhich;
2992 if (TREE_CODE (which) != INTEGER_CST)
2994 error ("argument of `__builtin_eh_return_regno' must be constant");
2995 return constm1_rtx;
2998 iwhich = tree_low_cst (which, 1);
2999 iwhich = EH_RETURN_DATA_REGNO (iwhich);
3000 if (iwhich == INVALID_REGNUM)
3001 return constm1_rtx;
3003 #ifdef DWARF_FRAME_REGNUM
3004 iwhich = DWARF_FRAME_REGNUM (iwhich);
3005 #else
3006 iwhich = DBX_REGISTER_NUMBER (iwhich);
3007 #endif
3009 return GEN_INT (iwhich);
3012 /* Given a value extracted from the return address register or stack slot,
3013 return the actual address encoded in that value. */
3016 expand_builtin_extract_return_addr (addr_tree)
3017 tree addr_tree;
3019 rtx addr = expand_expr (addr_tree, NULL_RTX, Pmode, 0);
3021 /* First mask out any unwanted bits. */
3022 #ifdef MASK_RETURN_ADDR
3023 expand_and (addr, MASK_RETURN_ADDR, addr);
3024 #endif
3026 /* Then adjust to find the real return address. */
3027 #if defined (RETURN_ADDR_OFFSET)
3028 addr = plus_constant (addr, RETURN_ADDR_OFFSET);
3029 #endif
3031 return addr;
3034 /* Given an actual address in addr_tree, do any necessary encoding
3035 and return the value to be stored in the return address register or
3036 stack slot so the epilogue will return to that address. */
3039 expand_builtin_frob_return_addr (addr_tree)
3040 tree addr_tree;
3042 rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, 0);
3044 #ifdef POINTERS_EXTEND_UNSIGNED
3045 if (GET_MODE (addr) != Pmode)
3046 addr = convert_memory_address (Pmode, addr);
3047 #endif
3049 #ifdef RETURN_ADDR_OFFSET
3050 addr = force_reg (Pmode, addr);
3051 addr = plus_constant (addr, -RETURN_ADDR_OFFSET);
3052 #endif
3054 return addr;
3057 /* Set up the epilogue with the magic bits we'll need to return to the
3058 exception handler. */
3060 void
3061 expand_builtin_eh_return (stackadj_tree, handler_tree)
3062 tree stackadj_tree, handler_tree;
3064 rtx stackadj, handler;
3066 stackadj = expand_expr (stackadj_tree, cfun->eh->ehr_stackadj, VOIDmode, 0);
3067 handler = expand_expr (handler_tree, cfun->eh->ehr_handler, VOIDmode, 0);
3069 #ifdef POINTERS_EXTEND_UNSIGNED
3070 if (GET_MODE (stackadj) != Pmode)
3071 stackadj = convert_memory_address (Pmode, stackadj);
3073 if (GET_MODE (handler) != Pmode)
3074 handler = convert_memory_address (Pmode, handler);
3075 #endif
3077 if (! cfun->eh->ehr_label)
3079 cfun->eh->ehr_stackadj = copy_to_reg (stackadj);
3080 cfun->eh->ehr_handler = copy_to_reg (handler);
3081 cfun->eh->ehr_label = gen_label_rtx ();
3083 else
3085 if (stackadj != cfun->eh->ehr_stackadj)
3086 emit_move_insn (cfun->eh->ehr_stackadj, stackadj);
3087 if (handler != cfun->eh->ehr_handler)
3088 emit_move_insn (cfun->eh->ehr_handler, handler);
3091 emit_jump (cfun->eh->ehr_label);
3094 void
3095 expand_eh_return ()
3097 rtx sa, ra, around_label;
3099 if (! cfun->eh->ehr_label)
3100 return;
3102 sa = EH_RETURN_STACKADJ_RTX;
3103 if (! sa)
3105 error ("__builtin_eh_return not supported on this target");
3106 return;
3109 current_function_calls_eh_return = 1;
3111 around_label = gen_label_rtx ();
3112 emit_move_insn (sa, const0_rtx);
3113 emit_jump (around_label);
3115 emit_label (cfun->eh->ehr_label);
3116 clobber_return_register ();
3118 #ifdef HAVE_eh_return
3119 if (HAVE_eh_return)
3120 emit_insn (gen_eh_return (cfun->eh->ehr_stackadj, cfun->eh->ehr_handler));
3121 else
3122 #endif
3124 ra = EH_RETURN_HANDLER_RTX;
3125 if (! ra)
3127 error ("__builtin_eh_return not supported on this target");
3128 ra = gen_reg_rtx (Pmode);
3131 emit_move_insn (sa, cfun->eh->ehr_stackadj);
3132 emit_move_insn (ra, cfun->eh->ehr_handler);
3135 emit_label (around_label);
3138 /* In the following functions, we represent entries in the action table
3139 as 1-based indices. Special cases are:
3141 0: null action record, non-null landing pad; implies cleanups
3142 -1: null action record, null landing pad; implies no action
3143 -2: no call-site entry; implies must_not_throw
3144 -3: we have yet to process outer regions
3146 Further, no special cases apply to the "next" field of the record.
3147 For next, 0 means end of list. */
3149 struct action_record
3151 int offset;
3152 int filter;
3153 int next;
3156 static int
3157 action_record_eq (pentry, pdata)
3158 const PTR pentry;
3159 const PTR pdata;
3161 const struct action_record *entry = (const struct action_record *) pentry;
3162 const struct action_record *data = (const struct action_record *) pdata;
3163 return entry->filter == data->filter && entry->next == data->next;
3166 static hashval_t
3167 action_record_hash (pentry)
3168 const PTR pentry;
3170 const struct action_record *entry = (const struct action_record *) pentry;
3171 return entry->next * 1009 + entry->filter;
3174 static int
3175 add_action_record (ar_hash, filter, next)
3176 htab_t ar_hash;
3177 int filter, next;
3179 struct action_record **slot, *new, tmp;
3181 tmp.filter = filter;
3182 tmp.next = next;
3183 slot = (struct action_record **) htab_find_slot (ar_hash, &tmp, INSERT);
3185 if ((new = *slot) == NULL)
3187 new = (struct action_record *) xmalloc (sizeof (*new));
3188 new->offset = VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data) + 1;
3189 new->filter = filter;
3190 new->next = next;
3191 *slot = new;
3193 /* The filter value goes in untouched. The link to the next
3194 record is a "self-relative" byte offset, or zero to indicate
3195 that there is no next record. So convert the absolute 1 based
3196 indices we've been carrying around into a displacement. */
3198 push_sleb128 (&cfun->eh->action_record_data, filter);
3199 if (next)
3200 next -= VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data) + 1;
3201 push_sleb128 (&cfun->eh->action_record_data, next);
3204 return new->offset;
3207 static int
3208 collect_one_action_chain (ar_hash, region)
3209 htab_t ar_hash;
3210 struct eh_region *region;
3212 struct eh_region *c;
3213 int next;
3215 /* If we've reached the top of the region chain, then we have
3216 no actions, and require no landing pad. */
3217 if (region == NULL)
3218 return -1;
3220 switch (region->type)
3222 case ERT_CLEANUP:
3223 /* A cleanup adds a zero filter to the beginning of the chain, but
3224 there are special cases to look out for. If there are *only*
3225 cleanups along a path, then it compresses to a zero action.
3226 Further, if there are multiple cleanups along a path, we only
3227 need to represent one of them, as that is enough to trigger
3228 entry to the landing pad at runtime. */
3229 next = collect_one_action_chain (ar_hash, region->outer);
3230 if (next <= 0)
3231 return 0;
3232 for (c = region->outer; c ; c = c->outer)
3233 if (c->type == ERT_CLEANUP)
3234 return next;
3235 return add_action_record (ar_hash, 0, next);
3237 case ERT_TRY:
3238 /* Process the associated catch regions in reverse order.
3239 If there's a catch-all handler, then we don't need to
3240 search outer regions. Use a magic -3 value to record
3241 that we havn't done the outer search. */
3242 next = -3;
3243 for (c = region->u.try.last_catch; c ; c = c->u.catch.prev_catch)
3245 if (c->u.catch.type_list == NULL)
3247 /* Retrieve the filter from the head of the filter list
3248 where we have stored it (see assign_filter_values). */
3249 int filter
3250 = TREE_INT_CST_LOW (TREE_VALUE (c->u.catch.filter_list));
3252 next = add_action_record (ar_hash, filter, 0);
3254 else
3256 /* Once the outer search is done, trigger an action record for
3257 each filter we have. */
3258 tree flt_node;
3260 if (next == -3)
3262 next = collect_one_action_chain (ar_hash, region->outer);
3264 /* If there is no next action, terminate the chain. */
3265 if (next == -1)
3266 next = 0;
3267 /* If all outer actions are cleanups or must_not_throw,
3268 we'll have no action record for it, since we had wanted
3269 to encode these states in the call-site record directly.
3270 Add a cleanup action to the chain to catch these. */
3271 else if (next <= 0)
3272 next = add_action_record (ar_hash, 0, 0);
3275 flt_node = c->u.catch.filter_list;
3276 for (; flt_node; flt_node = TREE_CHAIN (flt_node))
3278 int filter = TREE_INT_CST_LOW (TREE_VALUE (flt_node));
3279 next = add_action_record (ar_hash, filter, next);
3283 return next;
3285 case ERT_ALLOWED_EXCEPTIONS:
3286 /* An exception specification adds its filter to the
3287 beginning of the chain. */
3288 next = collect_one_action_chain (ar_hash, region->outer);
3289 return add_action_record (ar_hash, region->u.allowed.filter,
3290 next < 0 ? 0 : next);
3292 case ERT_MUST_NOT_THROW:
3293 /* A must-not-throw region with no inner handlers or cleanups
3294 requires no call-site entry. Note that this differs from
3295 the no handler or cleanup case in that we do require an lsda
3296 to be generated. Return a magic -2 value to record this. */
3297 return -2;
3299 case ERT_CATCH:
3300 case ERT_THROW:
3301 /* CATCH regions are handled in TRY above. THROW regions are
3302 for optimization information only and produce no output. */
3303 return collect_one_action_chain (ar_hash, region->outer);
3305 default:
3306 abort ();
3310 static int
3311 add_call_site (landing_pad, action)
3312 rtx landing_pad;
3313 int action;
3315 struct call_site_record *data = cfun->eh->call_site_data;
3316 int used = cfun->eh->call_site_data_used;
3317 int size = cfun->eh->call_site_data_size;
3319 if (used >= size)
3321 size = (size ? size * 2 : 64);
3322 data = (struct call_site_record *)
3323 xrealloc (data, sizeof (*data) * size);
3324 cfun->eh->call_site_data = data;
3325 cfun->eh->call_site_data_size = size;
3328 data[used].landing_pad = landing_pad;
3329 data[used].action = action;
3331 cfun->eh->call_site_data_used = used + 1;
3333 return used + call_site_base;
3336 /* Turn REG_EH_REGION notes back into NOTE_INSN_EH_REGION notes.
3337 The new note numbers will not refer to region numbers, but
3338 instead to call site entries. */
3340 void
3341 convert_to_eh_region_ranges ()
3343 rtx insn, iter, note;
3344 htab_t ar_hash;
3345 int last_action = -3;
3346 rtx last_action_insn = NULL_RTX;
3347 rtx last_landing_pad = NULL_RTX;
3348 rtx first_no_action_insn = NULL_RTX;
3349 int call_site = 0;
3351 if (USING_SJLJ_EXCEPTIONS || cfun->eh->region_tree == NULL)
3352 return;
3354 VARRAY_UCHAR_INIT (cfun->eh->action_record_data, 64, "action_record_data");
3356 ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
3358 for (iter = get_insns (); iter ; iter = NEXT_INSN (iter))
3359 if (INSN_P (iter))
3361 struct eh_region *region;
3362 int this_action;
3363 rtx this_landing_pad;
3365 insn = iter;
3366 if (GET_CODE (insn) == INSN
3367 && GET_CODE (PATTERN (insn)) == SEQUENCE)
3368 insn = XVECEXP (PATTERN (insn), 0, 0);
3370 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3371 if (!note)
3373 if (! (GET_CODE (insn) == CALL_INSN
3374 || (flag_non_call_exceptions
3375 && may_trap_p (PATTERN (insn)))))
3376 continue;
3377 this_action = -1;
3378 region = NULL;
3380 else
3382 if (INTVAL (XEXP (note, 0)) <= 0)
3383 continue;
3384 region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
3385 this_action = collect_one_action_chain (ar_hash, region);
3388 /* Existence of catch handlers, or must-not-throw regions
3389 implies that an lsda is needed (even if empty). */
3390 if (this_action != -1)
3391 cfun->uses_eh_lsda = 1;
3393 /* Delay creation of region notes for no-action regions
3394 until we're sure that an lsda will be required. */
3395 else if (last_action == -3)
3397 first_no_action_insn = iter;
3398 last_action = -1;
3401 /* Cleanups and handlers may share action chains but not
3402 landing pads. Collect the landing pad for this region. */
3403 if (this_action >= 0)
3405 struct eh_region *o;
3406 for (o = region; ! o->landing_pad ; o = o->outer)
3407 continue;
3408 this_landing_pad = o->landing_pad;
3410 else
3411 this_landing_pad = NULL_RTX;
3413 /* Differing actions or landing pads implies a change in call-site
3414 info, which implies some EH_REGION note should be emitted. */
3415 if (last_action != this_action
3416 || last_landing_pad != this_landing_pad)
3418 /* If we'd not seen a previous action (-3) or the previous
3419 action was must-not-throw (-2), then we do not need an
3420 end note. */
3421 if (last_action >= -1)
3423 /* If we delayed the creation of the begin, do it now. */
3424 if (first_no_action_insn)
3426 call_site = add_call_site (NULL_RTX, 0);
3427 note = emit_note_before (NOTE_INSN_EH_REGION_BEG,
3428 first_no_action_insn);
3429 NOTE_EH_HANDLER (note) = call_site;
3430 first_no_action_insn = NULL_RTX;
3433 note = emit_note_after (NOTE_INSN_EH_REGION_END,
3434 last_action_insn);
3435 NOTE_EH_HANDLER (note) = call_site;
3438 /* If the new action is must-not-throw, then no region notes
3439 are created. */
3440 if (this_action >= -1)
3442 call_site = add_call_site (this_landing_pad,
3443 this_action < 0 ? 0 : this_action);
3444 note = emit_note_before (NOTE_INSN_EH_REGION_BEG, iter);
3445 NOTE_EH_HANDLER (note) = call_site;
3448 last_action = this_action;
3449 last_landing_pad = this_landing_pad;
3451 last_action_insn = iter;
3454 if (last_action >= -1 && ! first_no_action_insn)
3456 note = emit_note_after (NOTE_INSN_EH_REGION_END, last_action_insn);
3457 NOTE_EH_HANDLER (note) = call_site;
3460 htab_delete (ar_hash);
3464 static void
3465 push_uleb128 (data_area, value)
3466 varray_type *data_area;
3467 unsigned int value;
3471 unsigned char byte = value & 0x7f;
3472 value >>= 7;
3473 if (value)
3474 byte |= 0x80;
3475 VARRAY_PUSH_UCHAR (*data_area, byte);
3477 while (value);
3480 static void
3481 push_sleb128 (data_area, value)
3482 varray_type *data_area;
3483 int value;
3485 unsigned char byte;
3486 int more;
3490 byte = value & 0x7f;
3491 value >>= 7;
3492 more = ! ((value == 0 && (byte & 0x40) == 0)
3493 || (value == -1 && (byte & 0x40) != 0));
3494 if (more)
3495 byte |= 0x80;
3496 VARRAY_PUSH_UCHAR (*data_area, byte);
3498 while (more);
3502 #ifndef HAVE_AS_LEB128
3503 static int
3504 dw2_size_of_call_site_table ()
3506 int n = cfun->eh->call_site_data_used;
3507 int size = n * (4 + 4 + 4);
3508 int i;
3510 for (i = 0; i < n; ++i)
3512 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3513 size += size_of_uleb128 (cs->action);
3516 return size;
3519 static int
3520 sjlj_size_of_call_site_table ()
3522 int n = cfun->eh->call_site_data_used;
3523 int size = 0;
3524 int i;
3526 for (i = 0; i < n; ++i)
3528 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3529 size += size_of_uleb128 (INTVAL (cs->landing_pad));
3530 size += size_of_uleb128 (cs->action);
3533 return size;
3535 #endif
3537 static void
3538 dw2_output_call_site_table ()
3540 const char *const function_start_lab
3541 = IDENTIFIER_POINTER (current_function_func_begin_label);
3542 int n = cfun->eh->call_site_data_used;
3543 int i;
3545 for (i = 0; i < n; ++i)
3547 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3548 char reg_start_lab[32];
3549 char reg_end_lab[32];
3550 char landing_pad_lab[32];
3552 ASM_GENERATE_INTERNAL_LABEL (reg_start_lab, "LEHB", call_site_base + i);
3553 ASM_GENERATE_INTERNAL_LABEL (reg_end_lab, "LEHE", call_site_base + i);
3555 if (cs->landing_pad)
3556 ASM_GENERATE_INTERNAL_LABEL (landing_pad_lab, "L",
3557 CODE_LABEL_NUMBER (cs->landing_pad));
3559 /* ??? Perhaps use insn length scaling if the assembler supports
3560 generic arithmetic. */
3561 /* ??? Perhaps use attr_length to choose data1 or data2 instead of
3562 data4 if the function is small enough. */
3563 #ifdef HAVE_AS_LEB128
3564 dw2_asm_output_delta_uleb128 (reg_start_lab, function_start_lab,
3565 "region %d start", i);
3566 dw2_asm_output_delta_uleb128 (reg_end_lab, reg_start_lab,
3567 "length");
3568 if (cs->landing_pad)
3569 dw2_asm_output_delta_uleb128 (landing_pad_lab, function_start_lab,
3570 "landing pad");
3571 else
3572 dw2_asm_output_data_uleb128 (0, "landing pad");
3573 #else
3574 dw2_asm_output_delta (4, reg_start_lab, function_start_lab,
3575 "region %d start", i);
3576 dw2_asm_output_delta (4, reg_end_lab, reg_start_lab, "length");
3577 if (cs->landing_pad)
3578 dw2_asm_output_delta (4, landing_pad_lab, function_start_lab,
3579 "landing pad");
3580 else
3581 dw2_asm_output_data (4, 0, "landing pad");
3582 #endif
3583 dw2_asm_output_data_uleb128 (cs->action, "action");
3586 call_site_base += n;
3589 static void
3590 sjlj_output_call_site_table ()
3592 int n = cfun->eh->call_site_data_used;
3593 int i;
3595 for (i = 0; i < n; ++i)
3597 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3599 dw2_asm_output_data_uleb128 (INTVAL (cs->landing_pad),
3600 "region %d landing pad", i);
3601 dw2_asm_output_data_uleb128 (cs->action, "action");
3604 call_site_base += n;
3607 void
3608 output_function_exception_table ()
3610 int tt_format, cs_format, lp_format, i, n;
3611 #ifdef HAVE_AS_LEB128
3612 char ttype_label[32];
3613 char cs_after_size_label[32];
3614 char cs_end_label[32];
3615 #else
3616 int call_site_len;
3617 #endif
3618 int have_tt_data;
3619 int funcdef_number;
3620 int tt_format_size = 0;
3622 /* Not all functions need anything. */
3623 if (! cfun->uses_eh_lsda)
3624 return;
3626 funcdef_number = (USING_SJLJ_EXCEPTIONS
3627 ? sjlj_funcdef_number
3628 : current_funcdef_number);
3630 #ifdef IA64_UNWIND_INFO
3631 fputs ("\t.personality\t", asm_out_file);
3632 output_addr_const (asm_out_file, eh_personality_libfunc);
3633 fputs ("\n\t.handlerdata\n", asm_out_file);
3634 /* Note that varasm still thinks we're in the function's code section.
3635 The ".endp" directive that will immediately follow will take us back. */
3636 #else
3637 (*targetm.asm_out.exception_section) ();
3638 #endif
3640 have_tt_data = (VARRAY_ACTIVE_SIZE (cfun->eh->ttype_data) > 0
3641 || VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data) > 0);
3643 /* Indicate the format of the @TType entries. */
3644 if (! have_tt_data)
3645 tt_format = DW_EH_PE_omit;
3646 else
3648 tt_format = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3649 #ifdef HAVE_AS_LEB128
3650 ASM_GENERATE_INTERNAL_LABEL (ttype_label, "LLSDATT", funcdef_number);
3651 #endif
3652 tt_format_size = size_of_encoded_value (tt_format);
3654 assemble_align (tt_format_size * BITS_PER_UNIT);
3657 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "LLSDA", funcdef_number);
3659 /* The LSDA header. */
3661 /* Indicate the format of the landing pad start pointer. An omitted
3662 field implies @LPStart == @Start. */
3663 /* Currently we always put @LPStart == @Start. This field would
3664 be most useful in moving the landing pads completely out of
3665 line to another section, but it could also be used to minimize
3666 the size of uleb128 landing pad offsets. */
3667 lp_format = DW_EH_PE_omit;
3668 dw2_asm_output_data (1, lp_format, "@LPStart format (%s)",
3669 eh_data_format_name (lp_format));
3671 /* @LPStart pointer would go here. */
3673 dw2_asm_output_data (1, tt_format, "@TType format (%s)",
3674 eh_data_format_name (tt_format));
3676 #ifndef HAVE_AS_LEB128
3677 if (USING_SJLJ_EXCEPTIONS)
3678 call_site_len = sjlj_size_of_call_site_table ();
3679 else
3680 call_site_len = dw2_size_of_call_site_table ();
3681 #endif
3683 /* A pc-relative 4-byte displacement to the @TType data. */
3684 if (have_tt_data)
3686 #ifdef HAVE_AS_LEB128
3687 char ttype_after_disp_label[32];
3688 ASM_GENERATE_INTERNAL_LABEL (ttype_after_disp_label, "LLSDATTD",
3689 funcdef_number);
3690 dw2_asm_output_delta_uleb128 (ttype_label, ttype_after_disp_label,
3691 "@TType base offset");
3692 ASM_OUTPUT_LABEL (asm_out_file, ttype_after_disp_label);
3693 #else
3694 /* Ug. Alignment queers things. */
3695 unsigned int before_disp, after_disp, last_disp, disp;
3697 before_disp = 1 + 1;
3698 after_disp = (1 + size_of_uleb128 (call_site_len)
3699 + call_site_len
3700 + VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data)
3701 + (VARRAY_ACTIVE_SIZE (cfun->eh->ttype_data)
3702 * tt_format_size));
3704 disp = after_disp;
3707 unsigned int disp_size, pad;
3709 last_disp = disp;
3710 disp_size = size_of_uleb128 (disp);
3711 pad = before_disp + disp_size + after_disp;
3712 if (pad % tt_format_size)
3713 pad = tt_format_size - (pad % tt_format_size);
3714 else
3715 pad = 0;
3716 disp = after_disp + pad;
3718 while (disp != last_disp);
3720 dw2_asm_output_data_uleb128 (disp, "@TType base offset");
3721 #endif
3724 /* Indicate the format of the call-site offsets. */
3725 #ifdef HAVE_AS_LEB128
3726 cs_format = DW_EH_PE_uleb128;
3727 #else
3728 cs_format = DW_EH_PE_udata4;
3729 #endif
3730 dw2_asm_output_data (1, cs_format, "call-site format (%s)",
3731 eh_data_format_name (cs_format));
3733 #ifdef HAVE_AS_LEB128
3734 ASM_GENERATE_INTERNAL_LABEL (cs_after_size_label, "LLSDACSB",
3735 funcdef_number);
3736 ASM_GENERATE_INTERNAL_LABEL (cs_end_label, "LLSDACSE",
3737 funcdef_number);
3738 dw2_asm_output_delta_uleb128 (cs_end_label, cs_after_size_label,
3739 "Call-site table length");
3740 ASM_OUTPUT_LABEL (asm_out_file, cs_after_size_label);
3741 if (USING_SJLJ_EXCEPTIONS)
3742 sjlj_output_call_site_table ();
3743 else
3744 dw2_output_call_site_table ();
3745 ASM_OUTPUT_LABEL (asm_out_file, cs_end_label);
3746 #else
3747 dw2_asm_output_data_uleb128 (call_site_len,"Call-site table length");
3748 if (USING_SJLJ_EXCEPTIONS)
3749 sjlj_output_call_site_table ();
3750 else
3751 dw2_output_call_site_table ();
3752 #endif
3754 /* ??? Decode and interpret the data for flag_debug_asm. */
3755 n = VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data);
3756 for (i = 0; i < n; ++i)
3757 dw2_asm_output_data (1, VARRAY_UCHAR (cfun->eh->action_record_data, i),
3758 (i ? NULL : "Action record table"));
3760 if (have_tt_data)
3761 assemble_align (tt_format_size * BITS_PER_UNIT);
3763 i = VARRAY_ACTIVE_SIZE (cfun->eh->ttype_data);
3764 while (i-- > 0)
3766 tree type = VARRAY_TREE (cfun->eh->ttype_data, i);
3767 rtx value;
3769 if (type == NULL_TREE)
3770 type = integer_zero_node;
3771 else
3772 type = lookup_type_for_runtime (type);
3774 value = expand_expr (type, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
3775 if (tt_format == DW_EH_PE_absptr || tt_format == DW_EH_PE_aligned)
3776 assemble_integer (value, tt_format_size,
3777 tt_format_size * BITS_PER_UNIT, 1);
3778 else
3779 dw2_asm_output_encoded_addr_rtx (tt_format, value, NULL);
3782 #ifdef HAVE_AS_LEB128
3783 if (have_tt_data)
3784 ASM_OUTPUT_LABEL (asm_out_file, ttype_label);
3785 #endif
3787 /* ??? Decode and interpret the data for flag_debug_asm. */
3788 n = VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data);
3789 for (i = 0; i < n; ++i)
3790 dw2_asm_output_data (1, VARRAY_UCHAR (cfun->eh->ehspec_data, i),
3791 (i ? NULL : "Exception specification table"));
3793 function_section (current_function_decl);
3795 if (USING_SJLJ_EXCEPTIONS)
3796 sjlj_funcdef_number += 1;