Daily bump.
[official-gcc.git] / gcc / except.c
blob3e424eb9d7f54878a2b417b4b3b69e3a06eea6a9
1 /* Implements exception handling.
2 Copyright (C) 1989-2013 Free Software Foundation, Inc.
3 Contributed by Mike Stump <mrs@cygnus.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* An exception is an event that can be "thrown" from within a
23 function. This event can then be "caught" by the callers of
24 the function.
26 The representation of exceptions changes several times during
27 the compilation process:
29 In the beginning, in the front end, we have the GENERIC trees
30 TRY_CATCH_EXPR, TRY_FINALLY_EXPR, WITH_CLEANUP_EXPR,
31 CLEANUP_POINT_EXPR, CATCH_EXPR, and EH_FILTER_EXPR.
33 During initial gimplification (gimplify.c) these are lowered
34 to the GIMPLE_TRY, GIMPLE_CATCH, and GIMPLE_EH_FILTER nodes.
35 The WITH_CLEANUP_EXPR and CLEANUP_POINT_EXPR nodes are converted
36 into GIMPLE_TRY_FINALLY nodes; the others are a more direct 1-1
37 conversion.
39 During pass_lower_eh (tree-eh.c) we record the nested structure
40 of the TRY nodes in EH_REGION nodes in CFUN->EH->REGION_TREE.
41 We expand the eh_protect_cleanup_actions langhook into MUST_NOT_THROW
42 regions at this time. We can then flatten the statements within
43 the TRY nodes to straight-line code. Statements that had been within
44 TRY nodes that can throw are recorded within CFUN->EH->THROW_STMT_TABLE,
45 so that we may remember what action is supposed to be taken if
46 a given statement does throw. During this lowering process,
47 we create an EH_LANDING_PAD node for each EH_REGION that has
48 some code within the function that needs to be executed if a
49 throw does happen. We also create RESX statements that are
50 used to transfer control from an inner EH_REGION to an outer
51 EH_REGION. We also create EH_DISPATCH statements as placeholders
52 for a runtime type comparison that should be made in order to
53 select the action to perform among different CATCH and EH_FILTER
54 regions.
56 During pass_lower_eh_dispatch (tree-eh.c), which is run after
57 all inlining is complete, we are able to run assign_filter_values,
58 which allows us to map the set of types manipulated by all of the
59 CATCH and EH_FILTER regions to a set of integers. This set of integers
60 will be how the exception runtime communicates with the code generated
61 within the function. We then expand the GIMPLE_EH_DISPATCH statements
62 to a switch or conditional branches that use the argument provided by
63 the runtime (__builtin_eh_filter) and the set of integers we computed
64 in assign_filter_values.
66 During pass_lower_resx (tree-eh.c), which is run near the end
67 of optimization, we expand RESX statements. If the eh region
68 that is outer to the RESX statement is a MUST_NOT_THROW, then
69 the RESX expands to some form of abort statement. If the eh
70 region that is outer to the RESX statement is within the current
71 function, then the RESX expands to a bookkeeping call
72 (__builtin_eh_copy_values) and a goto. Otherwise, the next
73 handler for the exception must be within a function somewhere
74 up the call chain, so we call back into the exception runtime
75 (__builtin_unwind_resume).
77 During pass_expand (cfgexpand.c), we generate REG_EH_REGION notes
78 that create an rtl to eh_region mapping that corresponds to the
79 gimple to eh_region mapping that had been recorded in the
80 THROW_STMT_TABLE.
82 Then, via finish_eh_generation, we generate the real landing pads
83 to which the runtime will actually transfer control. These new
84 landing pads perform whatever bookkeeping is needed by the target
85 backend in order to resume execution within the current function.
86 Each of these new landing pads falls through into the post_landing_pad
87 label which had been used within the CFG up to this point. All
88 exception edges within the CFG are redirected to the new landing pads.
89 If the target uses setjmp to implement exceptions, the various extra
90 calls into the runtime to register and unregister the current stack
91 frame are emitted at this time.
93 During pass_convert_to_eh_region_ranges (except.c), we transform
94 the REG_EH_REGION notes attached to individual insns into
95 non-overlapping ranges of insns bounded by NOTE_INSN_EH_REGION_BEG
96 and NOTE_INSN_EH_REGION_END. Each insn within such ranges has the
97 same associated action within the exception region tree, meaning
98 that (1) the exception is caught by the same landing pad within the
99 current function, (2) the exception is blocked by the runtime with
100 a MUST_NOT_THROW region, or (3) the exception is not handled at all
101 within the current function.
103 Finally, during assembly generation, we call
104 output_function_exception_table (except.c) to emit the tables with
105 which the exception runtime can determine if a given stack frame
106 handles a given exception, and if so what filter value to provide
107 to the function when the non-local control transfer is effected.
108 If the target uses dwarf2 unwinding to implement exceptions, then
109 output_call_frame_info (dwarf2out.c) emits the required unwind data. */
112 #include "config.h"
113 #include "system.h"
114 #include "coretypes.h"
115 #include "tm.h"
116 #include "rtl.h"
117 #include "tree.h"
118 #include "flags.h"
119 #include "function.h"
120 #include "expr.h"
121 #include "libfuncs.h"
122 #include "insn-config.h"
123 #include "except.h"
124 #include "hard-reg-set.h"
125 #include "basic-block.h"
126 #include "output.h"
127 #include "dwarf2asm.h"
128 #include "dwarf2out.h"
129 #include "dwarf2.h"
130 #include "toplev.h"
131 #include "hashtab.h"
132 #include "intl.h"
133 #include "ggc.h"
134 #include "tm_p.h"
135 #include "target.h"
136 #include "common/common-target.h"
137 #include "langhooks.h"
138 #include "cgraph.h"
139 #include "diagnostic.h"
140 #include "tree-pretty-print.h"
141 #include "tree-pass.h"
142 #include "tree-flow.h"
143 #include "cfgloop.h"
145 /* Provide defaults for stuff that may not be defined when using
146 sjlj exceptions. */
147 #ifndef EH_RETURN_DATA_REGNO
148 #define EH_RETURN_DATA_REGNO(N) INVALID_REGNUM
149 #endif
151 static GTY(()) int call_site_base;
152 static GTY ((param_is (union tree_node)))
153 htab_t type_to_runtime_map;
155 /* Describe the SjLj_Function_Context structure. */
156 static GTY(()) tree sjlj_fc_type_node;
157 static int sjlj_fc_call_site_ofs;
158 static int sjlj_fc_data_ofs;
159 static int sjlj_fc_personality_ofs;
160 static int sjlj_fc_lsda_ofs;
161 static int sjlj_fc_jbuf_ofs;
164 struct GTY(()) call_site_record_d
166 rtx landing_pad;
167 int action;
170 static bool get_eh_region_and_lp_from_rtx (const_rtx, eh_region *,
171 eh_landing_pad *);
173 static int t2r_eq (const void *, const void *);
174 static hashval_t t2r_hash (const void *);
176 static int ttypes_filter_eq (const void *, const void *);
177 static hashval_t ttypes_filter_hash (const void *);
178 static int ehspec_filter_eq (const void *, const void *);
179 static hashval_t ehspec_filter_hash (const void *);
180 static int add_ttypes_entry (htab_t, tree);
181 static int add_ehspec_entry (htab_t, htab_t, tree);
182 static void dw2_build_landing_pads (void);
184 static int action_record_eq (const void *, const void *);
185 static hashval_t action_record_hash (const void *);
186 static int add_action_record (htab_t, int, int);
187 static int collect_one_action_chain (htab_t, eh_region);
188 static int add_call_site (rtx, int, int);
190 static void push_uleb128 (vec<uchar, va_gc> **, unsigned int);
191 static void push_sleb128 (vec<uchar, va_gc> **, int);
192 #ifndef HAVE_AS_LEB128
193 static int dw2_size_of_call_site_table (int);
194 static int sjlj_size_of_call_site_table (void);
195 #endif
196 static void dw2_output_call_site_table (int, int);
197 static void sjlj_output_call_site_table (void);
200 void
201 init_eh (void)
203 if (! flag_exceptions)
204 return;
206 type_to_runtime_map = htab_create_ggc (31, t2r_hash, t2r_eq, NULL);
208 /* Create the SjLj_Function_Context structure. This should match
209 the definition in unwind-sjlj.c. */
210 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
212 tree f_jbuf, f_per, f_lsda, f_prev, f_cs, f_data, tmp;
214 sjlj_fc_type_node = lang_hooks.types.make_type (RECORD_TYPE);
216 f_prev = build_decl (BUILTINS_LOCATION,
217 FIELD_DECL, get_identifier ("__prev"),
218 build_pointer_type (sjlj_fc_type_node));
219 DECL_FIELD_CONTEXT (f_prev) = sjlj_fc_type_node;
221 f_cs = build_decl (BUILTINS_LOCATION,
222 FIELD_DECL, get_identifier ("__call_site"),
223 integer_type_node);
224 DECL_FIELD_CONTEXT (f_cs) = sjlj_fc_type_node;
226 tmp = build_index_type (size_int (4 - 1));
227 tmp = build_array_type (lang_hooks.types.type_for_mode
228 (targetm.unwind_word_mode (), 1),
229 tmp);
230 f_data = build_decl (BUILTINS_LOCATION,
231 FIELD_DECL, get_identifier ("__data"), tmp);
232 DECL_FIELD_CONTEXT (f_data) = sjlj_fc_type_node;
234 f_per = build_decl (BUILTINS_LOCATION,
235 FIELD_DECL, get_identifier ("__personality"),
236 ptr_type_node);
237 DECL_FIELD_CONTEXT (f_per) = sjlj_fc_type_node;
239 f_lsda = build_decl (BUILTINS_LOCATION,
240 FIELD_DECL, get_identifier ("__lsda"),
241 ptr_type_node);
242 DECL_FIELD_CONTEXT (f_lsda) = sjlj_fc_type_node;
244 #ifdef DONT_USE_BUILTIN_SETJMP
245 #ifdef JMP_BUF_SIZE
246 tmp = size_int (JMP_BUF_SIZE - 1);
247 #else
248 /* Should be large enough for most systems, if it is not,
249 JMP_BUF_SIZE should be defined with the proper value. It will
250 also tend to be larger than necessary for most systems, a more
251 optimal port will define JMP_BUF_SIZE. */
252 tmp = size_int (FIRST_PSEUDO_REGISTER + 2 - 1);
253 #endif
254 #else
255 /* builtin_setjmp takes a pointer to 5 words. */
256 tmp = size_int (5 * BITS_PER_WORD / POINTER_SIZE - 1);
257 #endif
258 tmp = build_index_type (tmp);
259 tmp = build_array_type (ptr_type_node, tmp);
260 f_jbuf = build_decl (BUILTINS_LOCATION,
261 FIELD_DECL, get_identifier ("__jbuf"), tmp);
262 #ifdef DONT_USE_BUILTIN_SETJMP
263 /* We don't know what the alignment requirements of the
264 runtime's jmp_buf has. Overestimate. */
265 DECL_ALIGN (f_jbuf) = BIGGEST_ALIGNMENT;
266 DECL_USER_ALIGN (f_jbuf) = 1;
267 #endif
268 DECL_FIELD_CONTEXT (f_jbuf) = sjlj_fc_type_node;
270 TYPE_FIELDS (sjlj_fc_type_node) = f_prev;
271 TREE_CHAIN (f_prev) = f_cs;
272 TREE_CHAIN (f_cs) = f_data;
273 TREE_CHAIN (f_data) = f_per;
274 TREE_CHAIN (f_per) = f_lsda;
275 TREE_CHAIN (f_lsda) = f_jbuf;
277 layout_type (sjlj_fc_type_node);
279 /* Cache the interesting field offsets so that we have
280 easy access from rtl. */
281 sjlj_fc_call_site_ofs
282 = (tree_low_cst (DECL_FIELD_OFFSET (f_cs), 1)
283 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_cs), 1) / BITS_PER_UNIT);
284 sjlj_fc_data_ofs
285 = (tree_low_cst (DECL_FIELD_OFFSET (f_data), 1)
286 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_data), 1) / BITS_PER_UNIT);
287 sjlj_fc_personality_ofs
288 = (tree_low_cst (DECL_FIELD_OFFSET (f_per), 1)
289 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_per), 1) / BITS_PER_UNIT);
290 sjlj_fc_lsda_ofs
291 = (tree_low_cst (DECL_FIELD_OFFSET (f_lsda), 1)
292 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_lsda), 1) / BITS_PER_UNIT);
293 sjlj_fc_jbuf_ofs
294 = (tree_low_cst (DECL_FIELD_OFFSET (f_jbuf), 1)
295 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_jbuf), 1) / BITS_PER_UNIT);
299 void
300 init_eh_for_function (void)
302 cfun->eh = ggc_alloc_cleared_eh_status ();
304 /* Make sure zero'th entries are used. */
305 vec_safe_push (cfun->eh->region_array, (eh_region)0);
306 vec_safe_push (cfun->eh->lp_array, (eh_landing_pad)0);
309 /* Routines to generate the exception tree somewhat directly.
310 These are used from tree-eh.c when processing exception related
311 nodes during tree optimization. */
313 static eh_region
314 gen_eh_region (enum eh_region_type type, eh_region outer)
316 eh_region new_eh;
318 /* Insert a new blank region as a leaf in the tree. */
319 new_eh = ggc_alloc_cleared_eh_region_d ();
320 new_eh->type = type;
321 new_eh->outer = outer;
322 if (outer)
324 new_eh->next_peer = outer->inner;
325 outer->inner = new_eh;
327 else
329 new_eh->next_peer = cfun->eh->region_tree;
330 cfun->eh->region_tree = new_eh;
333 new_eh->index = vec_safe_length (cfun->eh->region_array);
334 vec_safe_push (cfun->eh->region_array, new_eh);
336 /* Copy the language's notion of whether to use __cxa_end_cleanup. */
337 if (targetm.arm_eabi_unwinder && lang_hooks.eh_use_cxa_end_cleanup)
338 new_eh->use_cxa_end_cleanup = true;
340 return new_eh;
343 eh_region
344 gen_eh_region_cleanup (eh_region outer)
346 return gen_eh_region (ERT_CLEANUP, outer);
349 eh_region
350 gen_eh_region_try (eh_region outer)
352 return gen_eh_region (ERT_TRY, outer);
355 eh_catch
356 gen_eh_region_catch (eh_region t, tree type_or_list)
358 eh_catch c, l;
359 tree type_list, type_node;
361 gcc_assert (t->type == ERT_TRY);
363 /* Ensure to always end up with a type list to normalize further
364 processing, then register each type against the runtime types map. */
365 type_list = type_or_list;
366 if (type_or_list)
368 if (TREE_CODE (type_or_list) != TREE_LIST)
369 type_list = tree_cons (NULL_TREE, type_or_list, NULL_TREE);
371 type_node = type_list;
372 for (; type_node; type_node = TREE_CHAIN (type_node))
373 add_type_for_runtime (TREE_VALUE (type_node));
376 c = ggc_alloc_cleared_eh_catch_d ();
377 c->type_list = type_list;
378 l = t->u.eh_try.last_catch;
379 c->prev_catch = l;
380 if (l)
381 l->next_catch = c;
382 else
383 t->u.eh_try.first_catch = c;
384 t->u.eh_try.last_catch = c;
386 return c;
389 eh_region
390 gen_eh_region_allowed (eh_region outer, tree allowed)
392 eh_region region = gen_eh_region (ERT_ALLOWED_EXCEPTIONS, outer);
393 region->u.allowed.type_list = allowed;
395 for (; allowed ; allowed = TREE_CHAIN (allowed))
396 add_type_for_runtime (TREE_VALUE (allowed));
398 return region;
401 eh_region
402 gen_eh_region_must_not_throw (eh_region outer)
404 return gen_eh_region (ERT_MUST_NOT_THROW, outer);
407 eh_landing_pad
408 gen_eh_landing_pad (eh_region region)
410 eh_landing_pad lp = ggc_alloc_cleared_eh_landing_pad_d ();
412 lp->next_lp = region->landing_pads;
413 lp->region = region;
414 lp->index = vec_safe_length (cfun->eh->lp_array);
415 region->landing_pads = lp;
417 vec_safe_push (cfun->eh->lp_array, lp);
419 return lp;
422 eh_region
423 get_eh_region_from_number_fn (struct function *ifun, int i)
425 return (*ifun->eh->region_array)[i];
428 eh_region
429 get_eh_region_from_number (int i)
431 return get_eh_region_from_number_fn (cfun, i);
434 eh_landing_pad
435 get_eh_landing_pad_from_number_fn (struct function *ifun, int i)
437 return (*ifun->eh->lp_array)[i];
440 eh_landing_pad
441 get_eh_landing_pad_from_number (int i)
443 return get_eh_landing_pad_from_number_fn (cfun, i);
446 eh_region
447 get_eh_region_from_lp_number_fn (struct function *ifun, int i)
449 if (i < 0)
450 return (*ifun->eh->region_array)[-i];
451 else if (i == 0)
452 return NULL;
453 else
455 eh_landing_pad lp;
456 lp = (*ifun->eh->lp_array)[i];
457 return lp->region;
461 eh_region
462 get_eh_region_from_lp_number (int i)
464 return get_eh_region_from_lp_number_fn (cfun, i);
467 /* Returns true if the current function has exception handling regions. */
469 bool
470 current_function_has_exception_handlers (void)
472 return cfun->eh->region_tree != NULL;
475 /* A subroutine of duplicate_eh_regions. Copy the eh_region tree at OLD.
476 Root it at OUTER, and apply LP_OFFSET to the lp numbers. */
478 struct duplicate_eh_regions_data
480 duplicate_eh_regions_map label_map;
481 void *label_map_data;
482 struct pointer_map_t *eh_map;
485 static void
486 duplicate_eh_regions_1 (struct duplicate_eh_regions_data *data,
487 eh_region old_r, eh_region outer)
489 eh_landing_pad old_lp, new_lp;
490 eh_region new_r;
491 void **slot;
493 new_r = gen_eh_region (old_r->type, outer);
494 slot = pointer_map_insert (data->eh_map, (void *)old_r);
495 gcc_assert (*slot == NULL);
496 *slot = (void *)new_r;
498 switch (old_r->type)
500 case ERT_CLEANUP:
501 break;
503 case ERT_TRY:
505 eh_catch oc, nc;
506 for (oc = old_r->u.eh_try.first_catch; oc ; oc = oc->next_catch)
508 /* We should be doing all our region duplication before and
509 during inlining, which is before filter lists are created. */
510 gcc_assert (oc->filter_list == NULL);
511 nc = gen_eh_region_catch (new_r, oc->type_list);
512 nc->label = data->label_map (oc->label, data->label_map_data);
515 break;
517 case ERT_ALLOWED_EXCEPTIONS:
518 new_r->u.allowed.type_list = old_r->u.allowed.type_list;
519 if (old_r->u.allowed.label)
520 new_r->u.allowed.label
521 = data->label_map (old_r->u.allowed.label, data->label_map_data);
522 else
523 new_r->u.allowed.label = NULL_TREE;
524 break;
526 case ERT_MUST_NOT_THROW:
527 new_r->u.must_not_throw.failure_loc =
528 LOCATION_LOCUS (old_r->u.must_not_throw.failure_loc);
529 new_r->u.must_not_throw.failure_decl =
530 old_r->u.must_not_throw.failure_decl;
531 break;
534 for (old_lp = old_r->landing_pads; old_lp ; old_lp = old_lp->next_lp)
536 /* Don't bother copying unused landing pads. */
537 if (old_lp->post_landing_pad == NULL)
538 continue;
540 new_lp = gen_eh_landing_pad (new_r);
541 slot = pointer_map_insert (data->eh_map, (void *)old_lp);
542 gcc_assert (*slot == NULL);
543 *slot = (void *)new_lp;
545 new_lp->post_landing_pad
546 = data->label_map (old_lp->post_landing_pad, data->label_map_data);
547 EH_LANDING_PAD_NR (new_lp->post_landing_pad) = new_lp->index;
550 /* Make sure to preserve the original use of __cxa_end_cleanup. */
551 new_r->use_cxa_end_cleanup = old_r->use_cxa_end_cleanup;
553 for (old_r = old_r->inner; old_r ; old_r = old_r->next_peer)
554 duplicate_eh_regions_1 (data, old_r, new_r);
557 /* Duplicate the EH regions from IFUN rooted at COPY_REGION into
558 the current function and root the tree below OUTER_REGION.
559 The special case of COPY_REGION of NULL means all regions.
560 Remap labels using MAP/MAP_DATA callback. Return a pointer map
561 that allows the caller to remap uses of both EH regions and
562 EH landing pads. */
564 struct pointer_map_t *
565 duplicate_eh_regions (struct function *ifun,
566 eh_region copy_region, int outer_lp,
567 duplicate_eh_regions_map map, void *map_data)
569 struct duplicate_eh_regions_data data;
570 eh_region outer_region;
572 #ifdef ENABLE_CHECKING
573 verify_eh_tree (ifun);
574 #endif
576 data.label_map = map;
577 data.label_map_data = map_data;
578 data.eh_map = pointer_map_create ();
580 outer_region = get_eh_region_from_lp_number (outer_lp);
582 /* Copy all the regions in the subtree. */
583 if (copy_region)
584 duplicate_eh_regions_1 (&data, copy_region, outer_region);
585 else
587 eh_region r;
588 for (r = ifun->eh->region_tree; r ; r = r->next_peer)
589 duplicate_eh_regions_1 (&data, r, outer_region);
592 #ifdef ENABLE_CHECKING
593 verify_eh_tree (cfun);
594 #endif
596 return data.eh_map;
599 /* Return the region that is outer to both REGION_A and REGION_B in IFUN. */
601 eh_region
602 eh_region_outermost (struct function *ifun, eh_region region_a,
603 eh_region region_b)
605 sbitmap b_outer;
607 gcc_assert (ifun->eh->region_array);
608 gcc_assert (ifun->eh->region_tree);
610 b_outer = sbitmap_alloc (ifun->eh->region_array->length());
611 bitmap_clear (b_outer);
615 bitmap_set_bit (b_outer, region_b->index);
616 region_b = region_b->outer;
618 while (region_b);
622 if (bitmap_bit_p (b_outer, region_a->index))
623 break;
624 region_a = region_a->outer;
626 while (region_a);
628 sbitmap_free (b_outer);
629 return region_a;
632 static int
633 t2r_eq (const void *pentry, const void *pdata)
635 const_tree const entry = (const_tree) pentry;
636 const_tree const data = (const_tree) pdata;
638 return TREE_PURPOSE (entry) == data;
641 static hashval_t
642 t2r_hash (const void *pentry)
644 const_tree const entry = (const_tree) pentry;
645 return TREE_HASH (TREE_PURPOSE (entry));
648 void
649 add_type_for_runtime (tree type)
651 tree *slot;
653 /* If TYPE is NOP_EXPR, it means that it already is a runtime type. */
654 if (TREE_CODE (type) == NOP_EXPR)
655 return;
657 slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
658 TREE_HASH (type), INSERT);
659 if (*slot == NULL)
661 tree runtime = lang_hooks.eh_runtime_type (type);
662 *slot = tree_cons (type, runtime, NULL_TREE);
666 tree
667 lookup_type_for_runtime (tree type)
669 tree *slot;
671 /* If TYPE is NOP_EXPR, it means that it already is a runtime type. */
672 if (TREE_CODE (type) == NOP_EXPR)
673 return type;
675 slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
676 TREE_HASH (type), NO_INSERT);
678 /* We should have always inserted the data earlier. */
679 return TREE_VALUE (*slot);
683 /* Represent an entry in @TTypes for either catch actions
684 or exception filter actions. */
685 struct ttypes_filter {
686 tree t;
687 int filter;
690 /* Compare ENTRY (a ttypes_filter entry in the hash table) with DATA
691 (a tree) for a @TTypes type node we are thinking about adding. */
693 static int
694 ttypes_filter_eq (const void *pentry, const void *pdata)
696 const struct ttypes_filter *const entry
697 = (const struct ttypes_filter *) pentry;
698 const_tree const data = (const_tree) pdata;
700 return entry->t == data;
703 static hashval_t
704 ttypes_filter_hash (const void *pentry)
706 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
707 return TREE_HASH (entry->t);
710 /* Compare ENTRY with DATA (both struct ttypes_filter) for a @TTypes
711 exception specification list we are thinking about adding. */
712 /* ??? Currently we use the type lists in the order given. Someone
713 should put these in some canonical order. */
715 static int
716 ehspec_filter_eq (const void *pentry, const void *pdata)
718 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
719 const struct ttypes_filter *data = (const struct ttypes_filter *) pdata;
721 return type_list_equal (entry->t, data->t);
724 /* Hash function for exception specification lists. */
726 static hashval_t
727 ehspec_filter_hash (const void *pentry)
729 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
730 hashval_t h = 0;
731 tree list;
733 for (list = entry->t; list ; list = TREE_CHAIN (list))
734 h = (h << 5) + (h >> 27) + TREE_HASH (TREE_VALUE (list));
735 return h;
738 /* Add TYPE (which may be NULL) to cfun->eh->ttype_data, using TYPES_HASH
739 to speed up the search. Return the filter value to be used. */
741 static int
742 add_ttypes_entry (htab_t ttypes_hash, tree type)
744 struct ttypes_filter **slot, *n;
746 slot = (struct ttypes_filter **)
747 htab_find_slot_with_hash (ttypes_hash, type, TREE_HASH (type), INSERT);
749 if ((n = *slot) == NULL)
751 /* Filter value is a 1 based table index. */
753 n = XNEW (struct ttypes_filter);
754 n->t = type;
755 n->filter = vec_safe_length (cfun->eh->ttype_data) + 1;
756 *slot = n;
758 vec_safe_push (cfun->eh->ttype_data, type);
761 return n->filter;
764 /* Add LIST to cfun->eh->ehspec_data, using EHSPEC_HASH and TYPES_HASH
765 to speed up the search. Return the filter value to be used. */
767 static int
768 add_ehspec_entry (htab_t ehspec_hash, htab_t ttypes_hash, tree list)
770 struct ttypes_filter **slot, *n;
771 struct ttypes_filter dummy;
773 dummy.t = list;
774 slot = (struct ttypes_filter **)
775 htab_find_slot (ehspec_hash, &dummy, INSERT);
777 if ((n = *slot) == NULL)
779 int len;
781 if (targetm.arm_eabi_unwinder)
782 len = vec_safe_length (cfun->eh->ehspec_data.arm_eabi);
783 else
784 len = vec_safe_length (cfun->eh->ehspec_data.other);
786 /* Filter value is a -1 based byte index into a uleb128 buffer. */
788 n = XNEW (struct ttypes_filter);
789 n->t = list;
790 n->filter = -(len + 1);
791 *slot = n;
793 /* Generate a 0 terminated list of filter values. */
794 for (; list ; list = TREE_CHAIN (list))
796 if (targetm.arm_eabi_unwinder)
797 vec_safe_push (cfun->eh->ehspec_data.arm_eabi, TREE_VALUE (list));
798 else
800 /* Look up each type in the list and encode its filter
801 value as a uleb128. */
802 push_uleb128 (&cfun->eh->ehspec_data.other,
803 add_ttypes_entry (ttypes_hash, TREE_VALUE (list)));
806 if (targetm.arm_eabi_unwinder)
807 vec_safe_push (cfun->eh->ehspec_data.arm_eabi, NULL_TREE);
808 else
809 vec_safe_push (cfun->eh->ehspec_data.other, (uchar)0);
812 return n->filter;
815 /* Generate the action filter values to be used for CATCH and
816 ALLOWED_EXCEPTIONS regions. When using dwarf2 exception regions,
817 we use lots of landing pads, and so every type or list can share
818 the same filter value, which saves table space. */
820 void
821 assign_filter_values (void)
823 int i;
824 htab_t ttypes, ehspec;
825 eh_region r;
826 eh_catch c;
828 vec_alloc (cfun->eh->ttype_data, 16);
829 if (targetm.arm_eabi_unwinder)
830 vec_alloc (cfun->eh->ehspec_data.arm_eabi, 64);
831 else
832 vec_alloc (cfun->eh->ehspec_data.other, 64);
834 ttypes = htab_create (31, ttypes_filter_hash, ttypes_filter_eq, free);
835 ehspec = htab_create (31, ehspec_filter_hash, ehspec_filter_eq, free);
837 for (i = 1; vec_safe_iterate (cfun->eh->region_array, i, &r); ++i)
839 if (r == NULL)
840 continue;
842 switch (r->type)
844 case ERT_TRY:
845 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
847 /* Whatever type_list is (NULL or true list), we build a list
848 of filters for the region. */
849 c->filter_list = NULL_TREE;
851 if (c->type_list != NULL)
853 /* Get a filter value for each of the types caught and store
854 them in the region's dedicated list. */
855 tree tp_node = c->type_list;
857 for ( ; tp_node; tp_node = TREE_CHAIN (tp_node))
859 int flt = add_ttypes_entry (ttypes, TREE_VALUE (tp_node));
860 tree flt_node = build_int_cst (integer_type_node, flt);
862 c->filter_list
863 = tree_cons (NULL_TREE, flt_node, c->filter_list);
866 else
868 /* Get a filter value for the NULL list also since it
869 will need an action record anyway. */
870 int flt = add_ttypes_entry (ttypes, NULL);
871 tree flt_node = build_int_cst (integer_type_node, flt);
873 c->filter_list
874 = tree_cons (NULL_TREE, flt_node, NULL);
877 break;
879 case ERT_ALLOWED_EXCEPTIONS:
880 r->u.allowed.filter
881 = add_ehspec_entry (ehspec, ttypes, r->u.allowed.type_list);
882 break;
884 default:
885 break;
889 htab_delete (ttypes);
890 htab_delete (ehspec);
893 /* Emit SEQ into basic block just before INSN (that is assumed to be
894 first instruction of some existing BB and return the newly
895 produced block. */
896 static basic_block
897 emit_to_new_bb_before (rtx seq, rtx insn)
899 rtx last;
900 basic_block bb, prev_bb;
901 edge e;
902 edge_iterator ei;
904 /* If there happens to be a fallthru edge (possibly created by cleanup_cfg
905 call), we don't want it to go into newly created landing pad or other EH
906 construct. */
907 for (ei = ei_start (BLOCK_FOR_INSN (insn)->preds); (e = ei_safe_edge (ei)); )
908 if (e->flags & EDGE_FALLTHRU)
909 force_nonfallthru (e);
910 else
911 ei_next (&ei);
912 last = emit_insn_before (seq, insn);
913 if (BARRIER_P (last))
914 last = PREV_INSN (last);
915 prev_bb = BLOCK_FOR_INSN (insn)->prev_bb;
916 bb = create_basic_block (seq, last, prev_bb);
917 update_bb_for_insn (bb);
918 bb->flags |= BB_SUPERBLOCK;
919 return bb;
922 /* A subroutine of dw2_build_landing_pads, also used for edge splitting
923 at the rtl level. Emit the code required by the target at a landing
924 pad for the given region. */
926 void
927 expand_dw2_landing_pad_for_region (eh_region region)
929 #ifdef HAVE_exception_receiver
930 if (HAVE_exception_receiver)
931 emit_insn (gen_exception_receiver ());
932 else
933 #endif
934 #ifdef HAVE_nonlocal_goto_receiver
935 if (HAVE_nonlocal_goto_receiver)
936 emit_insn (gen_nonlocal_goto_receiver ());
937 else
938 #endif
939 { /* Nothing */ }
941 if (region->exc_ptr_reg)
942 emit_move_insn (region->exc_ptr_reg,
943 gen_rtx_REG (ptr_mode, EH_RETURN_DATA_REGNO (0)));
944 if (region->filter_reg)
945 emit_move_insn (region->filter_reg,
946 gen_rtx_REG (targetm.eh_return_filter_mode (),
947 EH_RETURN_DATA_REGNO (1)));
950 /* Expand the extra code needed at landing pads for dwarf2 unwinding. */
952 static void
953 dw2_build_landing_pads (void)
955 int i;
956 eh_landing_pad lp;
957 int e_flags = EDGE_FALLTHRU;
959 /* If we're going to partition blocks, we need to be able to add
960 new landing pads later, which means that we need to hold on to
961 the post-landing-pad block. Prevent it from being merged away.
962 We'll remove this bit after partitioning. */
963 if (flag_reorder_blocks_and_partition)
964 e_flags |= EDGE_PRESERVE;
966 for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
968 basic_block bb;
969 rtx seq;
970 edge e;
972 if (lp == NULL || lp->post_landing_pad == NULL)
973 continue;
975 start_sequence ();
977 lp->landing_pad = gen_label_rtx ();
978 emit_label (lp->landing_pad);
979 LABEL_PRESERVE_P (lp->landing_pad) = 1;
981 expand_dw2_landing_pad_for_region (lp->region);
983 seq = get_insns ();
984 end_sequence ();
986 bb = emit_to_new_bb_before (seq, label_rtx (lp->post_landing_pad));
987 e = make_edge (bb, bb->next_bb, e_flags);
988 e->count = bb->count;
989 e->probability = REG_BR_PROB_BASE;
990 if (current_loops)
992 struct loop *loop = bb->next_bb->loop_father;
993 /* If we created a pre-header block, add the new block to the
994 outer loop, otherwise to the loop itself. */
995 if (bb->next_bb == loop->header)
996 add_bb_to_loop (bb, loop_outer (loop));
997 else
998 add_bb_to_loop (bb, loop);
1004 static vec<int> sjlj_lp_call_site_index;
1006 /* Process all active landing pads. Assign each one a compact dispatch
1007 index, and a call-site index. */
1009 static int
1010 sjlj_assign_call_site_values (void)
1012 htab_t ar_hash;
1013 int i, disp_index;
1014 eh_landing_pad lp;
1016 vec_alloc (crtl->eh.action_record_data, 64);
1017 ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
1019 disp_index = 0;
1020 call_site_base = 1;
1021 for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
1022 if (lp && lp->post_landing_pad)
1024 int action, call_site;
1026 /* First: build the action table. */
1027 action = collect_one_action_chain (ar_hash, lp->region);
1029 /* Next: assign call-site values. If dwarf2 terms, this would be
1030 the region number assigned by convert_to_eh_region_ranges, but
1031 handles no-action and must-not-throw differently. */
1032 /* Map must-not-throw to otherwise unused call-site index 0. */
1033 if (action == -2)
1034 call_site = 0;
1035 /* Map no-action to otherwise unused call-site index -1. */
1036 else if (action == -1)
1037 call_site = -1;
1038 /* Otherwise, look it up in the table. */
1039 else
1040 call_site = add_call_site (GEN_INT (disp_index), action, 0);
1041 sjlj_lp_call_site_index[i] = call_site;
1043 disp_index++;
1046 htab_delete (ar_hash);
1048 return disp_index;
1051 /* Emit code to record the current call-site index before every
1052 insn that can throw. */
1054 static void
1055 sjlj_mark_call_sites (void)
1057 int last_call_site = -2;
1058 rtx insn, mem;
1060 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
1062 eh_landing_pad lp;
1063 eh_region r;
1064 bool nothrow;
1065 int this_call_site;
1066 rtx before, p;
1068 /* Reset value tracking at extended basic block boundaries. */
1069 if (LABEL_P (insn))
1070 last_call_site = -2;
1072 if (! INSN_P (insn))
1073 continue;
1075 nothrow = get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1076 if (nothrow)
1077 continue;
1078 if (lp)
1079 this_call_site = sjlj_lp_call_site_index[lp->index];
1080 else if (r == NULL)
1082 /* Calls (and trapping insns) without notes are outside any
1083 exception handling region in this function. Mark them as
1084 no action. */
1085 this_call_site = -1;
1087 else
1089 gcc_assert (r->type == ERT_MUST_NOT_THROW);
1090 this_call_site = 0;
1093 if (this_call_site != -1)
1094 crtl->uses_eh_lsda = 1;
1096 if (this_call_site == last_call_site)
1097 continue;
1099 /* Don't separate a call from it's argument loads. */
1100 before = insn;
1101 if (CALL_P (insn))
1102 before = find_first_parameter_load (insn, NULL_RTX);
1104 start_sequence ();
1105 mem = adjust_address (crtl->eh.sjlj_fc, TYPE_MODE (integer_type_node),
1106 sjlj_fc_call_site_ofs);
1107 emit_move_insn (mem, GEN_INT (this_call_site));
1108 p = get_insns ();
1109 end_sequence ();
1111 emit_insn_before (p, before);
1112 last_call_site = this_call_site;
1116 /* Construct the SjLj_Function_Context. */
1118 static void
1119 sjlj_emit_function_enter (rtx dispatch_label)
1121 rtx fn_begin, fc, mem, seq;
1122 bool fn_begin_outside_block;
1123 rtx personality = get_personality_function (current_function_decl);
1125 fc = crtl->eh.sjlj_fc;
1127 start_sequence ();
1129 /* We're storing this libcall's address into memory instead of
1130 calling it directly. Thus, we must call assemble_external_libcall
1131 here, as we can not depend on emit_library_call to do it for us. */
1132 assemble_external_libcall (personality);
1133 mem = adjust_address (fc, Pmode, sjlj_fc_personality_ofs);
1134 emit_move_insn (mem, personality);
1136 mem = adjust_address (fc, Pmode, sjlj_fc_lsda_ofs);
1137 if (crtl->uses_eh_lsda)
1139 char buf[20];
1140 rtx sym;
1142 ASM_GENERATE_INTERNAL_LABEL (buf, "LLSDA", current_function_funcdef_no);
1143 sym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
1144 SYMBOL_REF_FLAGS (sym) = SYMBOL_FLAG_LOCAL;
1145 emit_move_insn (mem, sym);
1147 else
1148 emit_move_insn (mem, const0_rtx);
1150 if (dispatch_label)
1152 #ifdef DONT_USE_BUILTIN_SETJMP
1153 rtx x;
1154 x = emit_library_call_value (setjmp_libfunc, NULL_RTX, LCT_RETURNS_TWICE,
1155 TYPE_MODE (integer_type_node), 1,
1156 plus_constant (Pmode, XEXP (fc, 0),
1157 sjlj_fc_jbuf_ofs), Pmode);
1159 emit_cmp_and_jump_insns (x, const0_rtx, NE, 0,
1160 TYPE_MODE (integer_type_node), 0,
1161 dispatch_label, REG_BR_PROB_BASE / 100);
1162 #else
1163 expand_builtin_setjmp_setup (plus_constant (Pmode, XEXP (fc, 0),
1164 sjlj_fc_jbuf_ofs),
1165 dispatch_label);
1166 #endif
1169 emit_library_call (unwind_sjlj_register_libfunc, LCT_NORMAL, VOIDmode,
1170 1, XEXP (fc, 0), Pmode);
1172 seq = get_insns ();
1173 end_sequence ();
1175 /* ??? Instead of doing this at the beginning of the function,
1176 do this in a block that is at loop level 0 and dominates all
1177 can_throw_internal instructions. */
1179 fn_begin_outside_block = true;
1180 for (fn_begin = get_insns (); ; fn_begin = NEXT_INSN (fn_begin))
1181 if (NOTE_P (fn_begin))
1183 if (NOTE_KIND (fn_begin) == NOTE_INSN_FUNCTION_BEG)
1184 break;
1185 else if (NOTE_INSN_BASIC_BLOCK_P (fn_begin))
1186 fn_begin_outside_block = false;
1189 if (fn_begin_outside_block)
1190 insert_insn_on_edge (seq, single_succ_edge (ENTRY_BLOCK_PTR));
1191 else
1192 emit_insn_after (seq, fn_begin);
1195 /* Call back from expand_function_end to know where we should put
1196 the call to unwind_sjlj_unregister_libfunc if needed. */
1198 void
1199 sjlj_emit_function_exit_after (rtx after)
1201 crtl->eh.sjlj_exit_after = after;
1204 static void
1205 sjlj_emit_function_exit (void)
1207 rtx seq, insn;
1209 start_sequence ();
1211 emit_library_call (unwind_sjlj_unregister_libfunc, LCT_NORMAL, VOIDmode,
1212 1, XEXP (crtl->eh.sjlj_fc, 0), Pmode);
1214 seq = get_insns ();
1215 end_sequence ();
1217 /* ??? Really this can be done in any block at loop level 0 that
1218 post-dominates all can_throw_internal instructions. This is
1219 the last possible moment. */
1221 insn = crtl->eh.sjlj_exit_after;
1222 if (LABEL_P (insn))
1223 insn = NEXT_INSN (insn);
1225 emit_insn_after (seq, insn);
1228 static void
1229 sjlj_emit_dispatch_table (rtx dispatch_label, int num_dispatch)
1231 enum machine_mode unwind_word_mode = targetm.unwind_word_mode ();
1232 enum machine_mode filter_mode = targetm.eh_return_filter_mode ();
1233 eh_landing_pad lp;
1234 rtx mem, seq, fc, before, exc_ptr_reg, filter_reg;
1235 rtx first_reachable_label;
1236 basic_block bb;
1237 eh_region r;
1238 edge e;
1239 int i, disp_index;
1240 vec<tree> dispatch_labels = vNULL;
1242 fc = crtl->eh.sjlj_fc;
1244 start_sequence ();
1246 emit_label (dispatch_label);
1248 #ifndef DONT_USE_BUILTIN_SETJMP
1249 expand_builtin_setjmp_receiver (dispatch_label);
1251 /* The caller of expand_builtin_setjmp_receiver is responsible for
1252 making sure that the label doesn't vanish. The only other caller
1253 is the expander for __builtin_setjmp_receiver, which places this
1254 label on the nonlocal_goto_label list. Since we're modeling these
1255 CFG edges more exactly, we can use the forced_labels list instead. */
1256 LABEL_PRESERVE_P (dispatch_label) = 1;
1257 forced_labels
1258 = gen_rtx_EXPR_LIST (VOIDmode, dispatch_label, forced_labels);
1259 #endif
1261 /* Load up exc_ptr and filter values from the function context. */
1262 mem = adjust_address (fc, unwind_word_mode, sjlj_fc_data_ofs);
1263 if (unwind_word_mode != ptr_mode)
1265 #ifdef POINTERS_EXTEND_UNSIGNED
1266 mem = convert_memory_address (ptr_mode, mem);
1267 #else
1268 mem = convert_to_mode (ptr_mode, mem, 0);
1269 #endif
1271 exc_ptr_reg = force_reg (ptr_mode, mem);
1273 mem = adjust_address (fc, unwind_word_mode,
1274 sjlj_fc_data_ofs + GET_MODE_SIZE (unwind_word_mode));
1275 if (unwind_word_mode != filter_mode)
1276 mem = convert_to_mode (filter_mode, mem, 0);
1277 filter_reg = force_reg (filter_mode, mem);
1279 /* Jump to one of the directly reachable regions. */
1281 disp_index = 0;
1282 first_reachable_label = NULL;
1284 /* If there's exactly one call site in the function, don't bother
1285 generating a switch statement. */
1286 if (num_dispatch > 1)
1287 dispatch_labels.create (num_dispatch);
1289 for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
1290 if (lp && lp->post_landing_pad)
1292 rtx seq2, label;
1294 start_sequence ();
1296 lp->landing_pad = dispatch_label;
1298 if (num_dispatch > 1)
1300 tree t_label, case_elt, t;
1302 t_label = create_artificial_label (UNKNOWN_LOCATION);
1303 t = build_int_cst (integer_type_node, disp_index);
1304 case_elt = build_case_label (t, NULL, t_label);
1305 dispatch_labels.quick_push (case_elt);
1306 label = label_rtx (t_label);
1308 else
1309 label = gen_label_rtx ();
1311 if (disp_index == 0)
1312 first_reachable_label = label;
1313 emit_label (label);
1315 r = lp->region;
1316 if (r->exc_ptr_reg)
1317 emit_move_insn (r->exc_ptr_reg, exc_ptr_reg);
1318 if (r->filter_reg)
1319 emit_move_insn (r->filter_reg, filter_reg);
1321 seq2 = get_insns ();
1322 end_sequence ();
1324 before = label_rtx (lp->post_landing_pad);
1325 bb = emit_to_new_bb_before (seq2, before);
1326 e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
1327 e->count = bb->count;
1328 e->probability = REG_BR_PROB_BASE;
1329 if (current_loops)
1331 struct loop *loop = bb->next_bb->loop_father;
1332 /* If we created a pre-header block, add the new block to the
1333 outer loop, otherwise to the loop itself. */
1334 if (bb->next_bb == loop->header)
1335 add_bb_to_loop (bb, loop_outer (loop));
1336 else
1337 add_bb_to_loop (bb, loop);
1338 /* ??? For multiple dispatches we will end up with edges
1339 from the loop tree root into this loop, making it a
1340 multiple-entry loop. Discard all affected loops. */
1341 if (num_dispatch > 1)
1343 for (loop = bb->loop_father;
1344 loop_outer (loop); loop = loop_outer (loop))
1346 loop->header = NULL;
1347 loop->latch = NULL;
1352 disp_index++;
1354 gcc_assert (disp_index == num_dispatch);
1356 if (num_dispatch > 1)
1358 rtx disp = adjust_address (fc, TYPE_MODE (integer_type_node),
1359 sjlj_fc_call_site_ofs);
1360 expand_sjlj_dispatch_table (disp, dispatch_labels);
1363 seq = get_insns ();
1364 end_sequence ();
1366 bb = emit_to_new_bb_before (seq, first_reachable_label);
1367 if (num_dispatch == 1)
1369 e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
1370 e->count = bb->count;
1371 e->probability = REG_BR_PROB_BASE;
1372 if (current_loops)
1374 struct loop *loop = bb->next_bb->loop_father;
1375 /* If we created a pre-header block, add the new block to the
1376 outer loop, otherwise to the loop itself. */
1377 if (bb->next_bb == loop->header)
1378 add_bb_to_loop (bb, loop_outer (loop));
1379 else
1380 add_bb_to_loop (bb, loop);
1383 else
1385 /* We are not wiring up edges here, but as the dispatcher call
1386 is at function begin simply associate the block with the
1387 outermost (non-)loop. */
1388 if (current_loops)
1389 add_bb_to_loop (bb, current_loops->tree_root);
1393 static void
1394 sjlj_build_landing_pads (void)
1396 int num_dispatch;
1398 num_dispatch = vec_safe_length (cfun->eh->lp_array);
1399 if (num_dispatch == 0)
1400 return;
1401 sjlj_lp_call_site_index.safe_grow_cleared (num_dispatch);
1403 num_dispatch = sjlj_assign_call_site_values ();
1404 if (num_dispatch > 0)
1406 rtx dispatch_label = gen_label_rtx ();
1407 int align = STACK_SLOT_ALIGNMENT (sjlj_fc_type_node,
1408 TYPE_MODE (sjlj_fc_type_node),
1409 TYPE_ALIGN (sjlj_fc_type_node));
1410 crtl->eh.sjlj_fc
1411 = assign_stack_local (TYPE_MODE (sjlj_fc_type_node),
1412 int_size_in_bytes (sjlj_fc_type_node),
1413 align);
1415 sjlj_mark_call_sites ();
1416 sjlj_emit_function_enter (dispatch_label);
1417 sjlj_emit_dispatch_table (dispatch_label, num_dispatch);
1418 sjlj_emit_function_exit ();
1421 /* If we do not have any landing pads, we may still need to register a
1422 personality routine and (empty) LSDA to handle must-not-throw regions. */
1423 else if (function_needs_eh_personality (cfun) != eh_personality_none)
1425 int align = STACK_SLOT_ALIGNMENT (sjlj_fc_type_node,
1426 TYPE_MODE (sjlj_fc_type_node),
1427 TYPE_ALIGN (sjlj_fc_type_node));
1428 crtl->eh.sjlj_fc
1429 = assign_stack_local (TYPE_MODE (sjlj_fc_type_node),
1430 int_size_in_bytes (sjlj_fc_type_node),
1431 align);
1433 sjlj_mark_call_sites ();
1434 sjlj_emit_function_enter (NULL_RTX);
1435 sjlj_emit_function_exit ();
1438 sjlj_lp_call_site_index.release ();
1441 /* After initial rtl generation, call back to finish generating
1442 exception support code. */
1444 void
1445 finish_eh_generation (void)
1447 basic_block bb;
1449 /* Construct the landing pads. */
1450 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
1451 sjlj_build_landing_pads ();
1452 else
1453 dw2_build_landing_pads ();
1454 break_superblocks ();
1456 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ
1457 /* Kludge for Alpha (see alpha_gp_save_rtx). */
1458 || single_succ_edge (ENTRY_BLOCK_PTR)->insns.r)
1459 commit_edge_insertions ();
1461 /* Redirect all EH edges from the post_landing_pad to the landing pad. */
1462 FOR_EACH_BB (bb)
1464 eh_landing_pad lp;
1465 edge_iterator ei;
1466 edge e;
1468 lp = get_eh_landing_pad_from_rtx (BB_END (bb));
1470 FOR_EACH_EDGE (e, ei, bb->succs)
1471 if (e->flags & EDGE_EH)
1472 break;
1474 /* We should not have generated any new throwing insns during this
1475 pass, and we should not have lost any EH edges, so we only need
1476 to handle two cases here:
1477 (1) reachable handler and an existing edge to post-landing-pad,
1478 (2) no reachable handler and no edge. */
1479 gcc_assert ((lp != NULL) == (e != NULL));
1480 if (lp != NULL)
1482 gcc_assert (BB_HEAD (e->dest) == label_rtx (lp->post_landing_pad));
1484 redirect_edge_succ (e, BLOCK_FOR_INSN (lp->landing_pad));
1485 e->flags |= (CALL_P (BB_END (bb))
1486 ? EDGE_ABNORMAL | EDGE_ABNORMAL_CALL
1487 : EDGE_ABNORMAL);
1492 /* This section handles removing dead code for flow. */
1494 void
1495 remove_eh_landing_pad (eh_landing_pad lp)
1497 eh_landing_pad *pp;
1499 for (pp = &lp->region->landing_pads; *pp != lp; pp = &(*pp)->next_lp)
1500 continue;
1501 *pp = lp->next_lp;
1503 if (lp->post_landing_pad)
1504 EH_LANDING_PAD_NR (lp->post_landing_pad) = 0;
1505 (*cfun->eh->lp_array)[lp->index] = NULL;
1508 /* Splice REGION from the region tree. */
1510 void
1511 remove_eh_handler (eh_region region)
1513 eh_region *pp, *pp_start, p, outer;
1514 eh_landing_pad lp;
1516 for (lp = region->landing_pads; lp ; lp = lp->next_lp)
1518 if (lp->post_landing_pad)
1519 EH_LANDING_PAD_NR (lp->post_landing_pad) = 0;
1520 (*cfun->eh->lp_array)[lp->index] = NULL;
1523 outer = region->outer;
1524 if (outer)
1525 pp_start = &outer->inner;
1526 else
1527 pp_start = &cfun->eh->region_tree;
1528 for (pp = pp_start, p = *pp; p != region; pp = &p->next_peer, p = *pp)
1529 continue;
1530 if (region->inner)
1532 *pp = p = region->inner;
1535 p->outer = outer;
1536 pp = &p->next_peer;
1537 p = *pp;
1539 while (p);
1541 *pp = region->next_peer;
1543 (*cfun->eh->region_array)[region->index] = NULL;
1546 /* Invokes CALLBACK for every exception handler landing pad label.
1547 Only used by reload hackery; should not be used by new code. */
1549 void
1550 for_each_eh_label (void (*callback) (rtx))
1552 eh_landing_pad lp;
1553 int i;
1555 for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
1557 if (lp)
1559 rtx lab = lp->landing_pad;
1560 if (lab && LABEL_P (lab))
1561 (*callback) (lab);
1566 /* Create the REG_EH_REGION note for INSN, given its ECF_FLAGS for a
1567 call insn.
1569 At the gimple level, we use LP_NR
1570 > 0 : The statement transfers to landing pad LP_NR
1571 = 0 : The statement is outside any EH region
1572 < 0 : The statement is within MUST_NOT_THROW region -LP_NR.
1574 At the rtl level, we use LP_NR
1575 > 0 : The insn transfers to landing pad LP_NR
1576 = 0 : The insn cannot throw
1577 < 0 : The insn is within MUST_NOT_THROW region -LP_NR
1578 = INT_MIN : The insn cannot throw or execute a nonlocal-goto.
1579 missing note: The insn is outside any EH region.
1581 ??? This difference probably ought to be avoided. We could stand
1582 to record nothrow for arbitrary gimple statements, and so avoid
1583 some moderately complex lookups in stmt_could_throw_p. Perhaps
1584 NOTHROW should be mapped on both sides to INT_MIN. Perhaps the
1585 no-nonlocal-goto property should be recorded elsewhere as a bit
1586 on the call_insn directly. Perhaps we should make more use of
1587 attaching the trees to call_insns (reachable via symbol_ref in
1588 direct call cases) and just pull the data out of the trees. */
1590 void
1591 make_reg_eh_region_note (rtx insn, int ecf_flags, int lp_nr)
1593 rtx value;
1594 if (ecf_flags & ECF_NOTHROW)
1595 value = const0_rtx;
1596 else if (lp_nr != 0)
1597 value = GEN_INT (lp_nr);
1598 else
1599 return;
1600 add_reg_note (insn, REG_EH_REGION, value);
1603 /* Create a REG_EH_REGION note for a CALL_INSN that cannot throw
1604 nor perform a non-local goto. Replace the region note if it
1605 already exists. */
1607 void
1608 make_reg_eh_region_note_nothrow_nononlocal (rtx insn)
1610 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1611 rtx intmin = GEN_INT (INT_MIN);
1613 if (note != 0)
1614 XEXP (note, 0) = intmin;
1615 else
1616 add_reg_note (insn, REG_EH_REGION, intmin);
1619 /* Return true if INSN could throw, assuming no REG_EH_REGION note
1620 to the contrary. */
1622 bool
1623 insn_could_throw_p (const_rtx insn)
1625 if (!flag_exceptions)
1626 return false;
1627 if (CALL_P (insn))
1628 return true;
1629 if (INSN_P (insn) && cfun->can_throw_non_call_exceptions)
1630 return may_trap_p (PATTERN (insn));
1631 return false;
1634 /* Copy an REG_EH_REGION note to each insn that might throw beginning
1635 at FIRST and ending at LAST. NOTE_OR_INSN is either the source insn
1636 to look for a note, or the note itself. */
1638 void
1639 copy_reg_eh_region_note_forward (rtx note_or_insn, rtx first, rtx last)
1641 rtx insn, note = note_or_insn;
1643 if (INSN_P (note_or_insn))
1645 note = find_reg_note (note_or_insn, REG_EH_REGION, NULL_RTX);
1646 if (note == NULL)
1647 return;
1649 note = XEXP (note, 0);
1651 for (insn = first; insn != last ; insn = NEXT_INSN (insn))
1652 if (!find_reg_note (insn, REG_EH_REGION, NULL_RTX)
1653 && insn_could_throw_p (insn))
1654 add_reg_note (insn, REG_EH_REGION, note);
1657 /* Likewise, but iterate backward. */
1659 void
1660 copy_reg_eh_region_note_backward (rtx note_or_insn, rtx last, rtx first)
1662 rtx insn, note = note_or_insn;
1664 if (INSN_P (note_or_insn))
1666 note = find_reg_note (note_or_insn, REG_EH_REGION, NULL_RTX);
1667 if (note == NULL)
1668 return;
1670 note = XEXP (note, 0);
1672 for (insn = last; insn != first; insn = PREV_INSN (insn))
1673 if (insn_could_throw_p (insn))
1674 add_reg_note (insn, REG_EH_REGION, note);
1678 /* Extract all EH information from INSN. Return true if the insn
1679 was marked NOTHROW. */
1681 static bool
1682 get_eh_region_and_lp_from_rtx (const_rtx insn, eh_region *pr,
1683 eh_landing_pad *plp)
1685 eh_landing_pad lp = NULL;
1686 eh_region r = NULL;
1687 bool ret = false;
1688 rtx note;
1689 int lp_nr;
1691 if (! INSN_P (insn))
1692 goto egress;
1694 if (NONJUMP_INSN_P (insn)
1695 && GET_CODE (PATTERN (insn)) == SEQUENCE)
1696 insn = XVECEXP (PATTERN (insn), 0, 0);
1698 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1699 if (!note)
1701 ret = !insn_could_throw_p (insn);
1702 goto egress;
1705 lp_nr = INTVAL (XEXP (note, 0));
1706 if (lp_nr == 0 || lp_nr == INT_MIN)
1708 ret = true;
1709 goto egress;
1712 if (lp_nr < 0)
1713 r = (*cfun->eh->region_array)[-lp_nr];
1714 else
1716 lp = (*cfun->eh->lp_array)[lp_nr];
1717 r = lp->region;
1720 egress:
1721 *plp = lp;
1722 *pr = r;
1723 return ret;
1726 /* Return the landing pad to which INSN may go, or NULL if it does not
1727 have a reachable landing pad within this function. */
1729 eh_landing_pad
1730 get_eh_landing_pad_from_rtx (const_rtx insn)
1732 eh_landing_pad lp;
1733 eh_region r;
1735 get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1736 return lp;
1739 /* Return the region to which INSN may go, or NULL if it does not
1740 have a reachable region within this function. */
1742 eh_region
1743 get_eh_region_from_rtx (const_rtx insn)
1745 eh_landing_pad lp;
1746 eh_region r;
1748 get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1749 return r;
1752 /* Return true if INSN throws and is caught by something in this function. */
1754 bool
1755 can_throw_internal (const_rtx insn)
1757 return get_eh_landing_pad_from_rtx (insn) != NULL;
1760 /* Return true if INSN throws and escapes from the current function. */
1762 bool
1763 can_throw_external (const_rtx insn)
1765 eh_landing_pad lp;
1766 eh_region r;
1767 bool nothrow;
1769 if (! INSN_P (insn))
1770 return false;
1772 if (NONJUMP_INSN_P (insn)
1773 && GET_CODE (PATTERN (insn)) == SEQUENCE)
1775 rtx seq = PATTERN (insn);
1776 int i, n = XVECLEN (seq, 0);
1778 for (i = 0; i < n; i++)
1779 if (can_throw_external (XVECEXP (seq, 0, i)))
1780 return true;
1782 return false;
1785 nothrow = get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1787 /* If we can't throw, we obviously can't throw external. */
1788 if (nothrow)
1789 return false;
1791 /* If we have an internal landing pad, then we're not external. */
1792 if (lp != NULL)
1793 return false;
1795 /* If we're not within an EH region, then we are external. */
1796 if (r == NULL)
1797 return true;
1799 /* The only thing that ought to be left is MUST_NOT_THROW regions,
1800 which don't always have landing pads. */
1801 gcc_assert (r->type == ERT_MUST_NOT_THROW);
1802 return false;
1805 /* Return true if INSN cannot throw at all. */
1807 bool
1808 insn_nothrow_p (const_rtx insn)
1810 eh_landing_pad lp;
1811 eh_region r;
1813 if (! INSN_P (insn))
1814 return true;
1816 if (NONJUMP_INSN_P (insn)
1817 && GET_CODE (PATTERN (insn)) == SEQUENCE)
1819 rtx seq = PATTERN (insn);
1820 int i, n = XVECLEN (seq, 0);
1822 for (i = 0; i < n; i++)
1823 if (!insn_nothrow_p (XVECEXP (seq, 0, i)))
1824 return false;
1826 return true;
1829 return get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1832 /* Return true if INSN can perform a non-local goto. */
1833 /* ??? This test is here in this file because it (ab)uses REG_EH_REGION. */
1835 bool
1836 can_nonlocal_goto (const_rtx insn)
1838 if (nonlocal_goto_handler_labels && CALL_P (insn))
1840 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1841 if (!note || INTVAL (XEXP (note, 0)) != INT_MIN)
1842 return true;
1844 return false;
1847 /* Set TREE_NOTHROW and crtl->all_throwers_are_sibcalls. */
1849 static unsigned int
1850 set_nothrow_function_flags (void)
1852 rtx insn;
1854 crtl->nothrow = 1;
1856 /* Assume crtl->all_throwers_are_sibcalls until we encounter
1857 something that can throw an exception. We specifically exempt
1858 CALL_INSNs that are SIBLING_CALL_P, as these are really jumps,
1859 and can't throw. Most CALL_INSNs are not SIBLING_CALL_P, so this
1860 is optimistic. */
1862 crtl->all_throwers_are_sibcalls = 1;
1864 /* If we don't know that this implementation of the function will
1865 actually be used, then we must not set TREE_NOTHROW, since
1866 callers must not assume that this function does not throw. */
1867 if (TREE_NOTHROW (current_function_decl))
1868 return 0;
1870 if (! flag_exceptions)
1871 return 0;
1873 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1874 if (can_throw_external (insn))
1876 crtl->nothrow = 0;
1878 if (!CALL_P (insn) || !SIBLING_CALL_P (insn))
1880 crtl->all_throwers_are_sibcalls = 0;
1881 return 0;
1885 if (crtl->nothrow
1886 && (cgraph_function_body_availability (cgraph_get_node
1887 (current_function_decl))
1888 >= AVAIL_AVAILABLE))
1890 struct cgraph_node *node = cgraph_get_node (current_function_decl);
1891 struct cgraph_edge *e;
1892 for (e = node->callers; e; e = e->next_caller)
1893 e->can_throw_external = false;
1894 cgraph_set_nothrow_flag (node, true);
1896 if (dump_file)
1897 fprintf (dump_file, "Marking function nothrow: %s\n\n",
1898 current_function_name ());
1900 return 0;
1903 struct rtl_opt_pass pass_set_nothrow_function_flags =
1906 RTL_PASS,
1907 "nothrow", /* name */
1908 OPTGROUP_NONE, /* optinfo_flags */
1909 NULL, /* gate */
1910 set_nothrow_function_flags, /* execute */
1911 NULL, /* sub */
1912 NULL, /* next */
1913 0, /* static_pass_number */
1914 TV_NONE, /* tv_id */
1915 0, /* properties_required */
1916 0, /* properties_provided */
1917 0, /* properties_destroyed */
1918 0, /* todo_flags_start */
1919 0 /* todo_flags_finish */
1924 /* Various hooks for unwind library. */
1926 /* Expand the EH support builtin functions:
1927 __builtin_eh_pointer and __builtin_eh_filter. */
1929 static eh_region
1930 expand_builtin_eh_common (tree region_nr_t)
1932 HOST_WIDE_INT region_nr;
1933 eh_region region;
1935 gcc_assert (host_integerp (region_nr_t, 0));
1936 region_nr = tree_low_cst (region_nr_t, 0);
1938 region = (*cfun->eh->region_array)[region_nr];
1940 /* ??? We shouldn't have been able to delete a eh region without
1941 deleting all the code that depended on it. */
1942 gcc_assert (region != NULL);
1944 return region;
1947 /* Expand to the exc_ptr value from the given eh region. */
1950 expand_builtin_eh_pointer (tree exp)
1952 eh_region region
1953 = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 0));
1954 if (region->exc_ptr_reg == NULL)
1955 region->exc_ptr_reg = gen_reg_rtx (ptr_mode);
1956 return region->exc_ptr_reg;
1959 /* Expand to the filter value from the given eh region. */
1962 expand_builtin_eh_filter (tree exp)
1964 eh_region region
1965 = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 0));
1966 if (region->filter_reg == NULL)
1967 region->filter_reg = gen_reg_rtx (targetm.eh_return_filter_mode ());
1968 return region->filter_reg;
1971 /* Copy the exc_ptr and filter values from one landing pad's registers
1972 to another. This is used to inline the resx statement. */
1975 expand_builtin_eh_copy_values (tree exp)
1977 eh_region dst
1978 = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 0));
1979 eh_region src
1980 = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 1));
1981 enum machine_mode fmode = targetm.eh_return_filter_mode ();
1983 if (dst->exc_ptr_reg == NULL)
1984 dst->exc_ptr_reg = gen_reg_rtx (ptr_mode);
1985 if (src->exc_ptr_reg == NULL)
1986 src->exc_ptr_reg = gen_reg_rtx (ptr_mode);
1988 if (dst->filter_reg == NULL)
1989 dst->filter_reg = gen_reg_rtx (fmode);
1990 if (src->filter_reg == NULL)
1991 src->filter_reg = gen_reg_rtx (fmode);
1993 emit_move_insn (dst->exc_ptr_reg, src->exc_ptr_reg);
1994 emit_move_insn (dst->filter_reg, src->filter_reg);
1996 return const0_rtx;
1999 /* Do any necessary initialization to access arbitrary stack frames.
2000 On the SPARC, this means flushing the register windows. */
2002 void
2003 expand_builtin_unwind_init (void)
2005 /* Set this so all the registers get saved in our frame; we need to be
2006 able to copy the saved values for any registers from frames we unwind. */
2007 crtl->saves_all_registers = 1;
2009 #ifdef SETUP_FRAME_ADDRESSES
2010 SETUP_FRAME_ADDRESSES ();
2011 #endif
2014 /* Map a non-negative number to an eh return data register number; expands
2015 to -1 if no return data register is associated with the input number.
2016 At least the inputs 0 and 1 must be mapped; the target may provide more. */
2019 expand_builtin_eh_return_data_regno (tree exp)
2021 tree which = CALL_EXPR_ARG (exp, 0);
2022 unsigned HOST_WIDE_INT iwhich;
2024 if (TREE_CODE (which) != INTEGER_CST)
2026 error ("argument of %<__builtin_eh_return_regno%> must be constant");
2027 return constm1_rtx;
2030 iwhich = tree_low_cst (which, 1);
2031 iwhich = EH_RETURN_DATA_REGNO (iwhich);
2032 if (iwhich == INVALID_REGNUM)
2033 return constm1_rtx;
2035 #ifdef DWARF_FRAME_REGNUM
2036 iwhich = DWARF_FRAME_REGNUM (iwhich);
2037 #else
2038 iwhich = DBX_REGISTER_NUMBER (iwhich);
2039 #endif
2041 return GEN_INT (iwhich);
2044 /* Given a value extracted from the return address register or stack slot,
2045 return the actual address encoded in that value. */
2048 expand_builtin_extract_return_addr (tree addr_tree)
2050 rtx addr = expand_expr (addr_tree, NULL_RTX, Pmode, EXPAND_NORMAL);
2052 if (GET_MODE (addr) != Pmode
2053 && GET_MODE (addr) != VOIDmode)
2055 #ifdef POINTERS_EXTEND_UNSIGNED
2056 addr = convert_memory_address (Pmode, addr);
2057 #else
2058 addr = convert_to_mode (Pmode, addr, 0);
2059 #endif
2062 /* First mask out any unwanted bits. */
2063 #ifdef MASK_RETURN_ADDR
2064 expand_and (Pmode, addr, MASK_RETURN_ADDR, addr);
2065 #endif
2067 /* Then adjust to find the real return address. */
2068 #if defined (RETURN_ADDR_OFFSET)
2069 addr = plus_constant (Pmode, addr, RETURN_ADDR_OFFSET);
2070 #endif
2072 return addr;
2075 /* Given an actual address in addr_tree, do any necessary encoding
2076 and return the value to be stored in the return address register or
2077 stack slot so the epilogue will return to that address. */
2080 expand_builtin_frob_return_addr (tree addr_tree)
2082 rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, EXPAND_NORMAL);
2084 addr = convert_memory_address (Pmode, addr);
2086 #ifdef RETURN_ADDR_OFFSET
2087 addr = force_reg (Pmode, addr);
2088 addr = plus_constant (Pmode, addr, -RETURN_ADDR_OFFSET);
2089 #endif
2091 return addr;
2094 /* Set up the epilogue with the magic bits we'll need to return to the
2095 exception handler. */
2097 void
2098 expand_builtin_eh_return (tree stackadj_tree ATTRIBUTE_UNUSED,
2099 tree handler_tree)
2101 rtx tmp;
2103 #ifdef EH_RETURN_STACKADJ_RTX
2104 tmp = expand_expr (stackadj_tree, crtl->eh.ehr_stackadj,
2105 VOIDmode, EXPAND_NORMAL);
2106 tmp = convert_memory_address (Pmode, tmp);
2107 if (!crtl->eh.ehr_stackadj)
2108 crtl->eh.ehr_stackadj = copy_to_reg (tmp);
2109 else if (tmp != crtl->eh.ehr_stackadj)
2110 emit_move_insn (crtl->eh.ehr_stackadj, tmp);
2111 #endif
2113 tmp = expand_expr (handler_tree, crtl->eh.ehr_handler,
2114 VOIDmode, EXPAND_NORMAL);
2115 tmp = convert_memory_address (Pmode, tmp);
2116 if (!crtl->eh.ehr_handler)
2117 crtl->eh.ehr_handler = copy_to_reg (tmp);
2118 else if (tmp != crtl->eh.ehr_handler)
2119 emit_move_insn (crtl->eh.ehr_handler, tmp);
2121 if (!crtl->eh.ehr_label)
2122 crtl->eh.ehr_label = gen_label_rtx ();
2123 emit_jump (crtl->eh.ehr_label);
2126 /* Expand __builtin_eh_return. This exit path from the function loads up
2127 the eh return data registers, adjusts the stack, and branches to a
2128 given PC other than the normal return address. */
2130 void
2131 expand_eh_return (void)
2133 rtx around_label;
2135 if (! crtl->eh.ehr_label)
2136 return;
2138 crtl->calls_eh_return = 1;
2140 #ifdef EH_RETURN_STACKADJ_RTX
2141 emit_move_insn (EH_RETURN_STACKADJ_RTX, const0_rtx);
2142 #endif
2144 around_label = gen_label_rtx ();
2145 emit_jump (around_label);
2147 emit_label (crtl->eh.ehr_label);
2148 clobber_return_register ();
2150 #ifdef EH_RETURN_STACKADJ_RTX
2151 emit_move_insn (EH_RETURN_STACKADJ_RTX, crtl->eh.ehr_stackadj);
2152 #endif
2154 #ifdef HAVE_eh_return
2155 if (HAVE_eh_return)
2156 emit_insn (gen_eh_return (crtl->eh.ehr_handler));
2157 else
2158 #endif
2160 #ifdef EH_RETURN_HANDLER_RTX
2161 emit_move_insn (EH_RETURN_HANDLER_RTX, crtl->eh.ehr_handler);
2162 #else
2163 error ("__builtin_eh_return not supported on this target");
2164 #endif
2167 emit_label (around_label);
2170 /* Convert a ptr_mode address ADDR_TREE to a Pmode address controlled by
2171 POINTERS_EXTEND_UNSIGNED and return it. */
2174 expand_builtin_extend_pointer (tree addr_tree)
2176 rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, EXPAND_NORMAL);
2177 int extend;
2179 #ifdef POINTERS_EXTEND_UNSIGNED
2180 extend = POINTERS_EXTEND_UNSIGNED;
2181 #else
2182 /* The previous EH code did an unsigned extend by default, so we do this also
2183 for consistency. */
2184 extend = 1;
2185 #endif
2187 return convert_modes (targetm.unwind_word_mode (), ptr_mode, addr, extend);
2190 /* In the following functions, we represent entries in the action table
2191 as 1-based indices. Special cases are:
2193 0: null action record, non-null landing pad; implies cleanups
2194 -1: null action record, null landing pad; implies no action
2195 -2: no call-site entry; implies must_not_throw
2196 -3: we have yet to process outer regions
2198 Further, no special cases apply to the "next" field of the record.
2199 For next, 0 means end of list. */
2201 struct action_record
2203 int offset;
2204 int filter;
2205 int next;
2208 static int
2209 action_record_eq (const void *pentry, const void *pdata)
2211 const struct action_record *entry = (const struct action_record *) pentry;
2212 const struct action_record *data = (const struct action_record *) pdata;
2213 return entry->filter == data->filter && entry->next == data->next;
2216 static hashval_t
2217 action_record_hash (const void *pentry)
2219 const struct action_record *entry = (const struct action_record *) pentry;
2220 return entry->next * 1009 + entry->filter;
2223 static int
2224 add_action_record (htab_t ar_hash, int filter, int next)
2226 struct action_record **slot, *new_ar, tmp;
2228 tmp.filter = filter;
2229 tmp.next = next;
2230 slot = (struct action_record **) htab_find_slot (ar_hash, &tmp, INSERT);
2232 if ((new_ar = *slot) == NULL)
2234 new_ar = XNEW (struct action_record);
2235 new_ar->offset = crtl->eh.action_record_data->length () + 1;
2236 new_ar->filter = filter;
2237 new_ar->next = next;
2238 *slot = new_ar;
2240 /* The filter value goes in untouched. The link to the next
2241 record is a "self-relative" byte offset, or zero to indicate
2242 that there is no next record. So convert the absolute 1 based
2243 indices we've been carrying around into a displacement. */
2245 push_sleb128 (&crtl->eh.action_record_data, filter);
2246 if (next)
2247 next -= crtl->eh.action_record_data->length () + 1;
2248 push_sleb128 (&crtl->eh.action_record_data, next);
2251 return new_ar->offset;
2254 static int
2255 collect_one_action_chain (htab_t ar_hash, eh_region region)
2257 int next;
2259 /* If we've reached the top of the region chain, then we have
2260 no actions, and require no landing pad. */
2261 if (region == NULL)
2262 return -1;
2264 switch (region->type)
2266 case ERT_CLEANUP:
2268 eh_region r;
2269 /* A cleanup adds a zero filter to the beginning of the chain, but
2270 there are special cases to look out for. If there are *only*
2271 cleanups along a path, then it compresses to a zero action.
2272 Further, if there are multiple cleanups along a path, we only
2273 need to represent one of them, as that is enough to trigger
2274 entry to the landing pad at runtime. */
2275 next = collect_one_action_chain (ar_hash, region->outer);
2276 if (next <= 0)
2277 return 0;
2278 for (r = region->outer; r ; r = r->outer)
2279 if (r->type == ERT_CLEANUP)
2280 return next;
2281 return add_action_record (ar_hash, 0, next);
2284 case ERT_TRY:
2286 eh_catch c;
2288 /* Process the associated catch regions in reverse order.
2289 If there's a catch-all handler, then we don't need to
2290 search outer regions. Use a magic -3 value to record
2291 that we haven't done the outer search. */
2292 next = -3;
2293 for (c = region->u.eh_try.last_catch; c ; c = c->prev_catch)
2295 if (c->type_list == NULL)
2297 /* Retrieve the filter from the head of the filter list
2298 where we have stored it (see assign_filter_values). */
2299 int filter = TREE_INT_CST_LOW (TREE_VALUE (c->filter_list));
2300 next = add_action_record (ar_hash, filter, 0);
2302 else
2304 /* Once the outer search is done, trigger an action record for
2305 each filter we have. */
2306 tree flt_node;
2308 if (next == -3)
2310 next = collect_one_action_chain (ar_hash, region->outer);
2312 /* If there is no next action, terminate the chain. */
2313 if (next == -1)
2314 next = 0;
2315 /* If all outer actions are cleanups or must_not_throw,
2316 we'll have no action record for it, since we had wanted
2317 to encode these states in the call-site record directly.
2318 Add a cleanup action to the chain to catch these. */
2319 else if (next <= 0)
2320 next = add_action_record (ar_hash, 0, 0);
2323 flt_node = c->filter_list;
2324 for (; flt_node; flt_node = TREE_CHAIN (flt_node))
2326 int filter = TREE_INT_CST_LOW (TREE_VALUE (flt_node));
2327 next = add_action_record (ar_hash, filter, next);
2331 return next;
2334 case ERT_ALLOWED_EXCEPTIONS:
2335 /* An exception specification adds its filter to the
2336 beginning of the chain. */
2337 next = collect_one_action_chain (ar_hash, region->outer);
2339 /* If there is no next action, terminate the chain. */
2340 if (next == -1)
2341 next = 0;
2342 /* If all outer actions are cleanups or must_not_throw,
2343 we'll have no action record for it, since we had wanted
2344 to encode these states in the call-site record directly.
2345 Add a cleanup action to the chain to catch these. */
2346 else if (next <= 0)
2347 next = add_action_record (ar_hash, 0, 0);
2349 return add_action_record (ar_hash, region->u.allowed.filter, next);
2351 case ERT_MUST_NOT_THROW:
2352 /* A must-not-throw region with no inner handlers or cleanups
2353 requires no call-site entry. Note that this differs from
2354 the no handler or cleanup case in that we do require an lsda
2355 to be generated. Return a magic -2 value to record this. */
2356 return -2;
2359 gcc_unreachable ();
2362 static int
2363 add_call_site (rtx landing_pad, int action, int section)
2365 call_site_record record;
2367 record = ggc_alloc_call_site_record_d ();
2368 record->landing_pad = landing_pad;
2369 record->action = action;
2371 vec_safe_push (crtl->eh.call_site_record_v[section], record);
2373 return call_site_base + crtl->eh.call_site_record_v[section]->length () - 1;
2376 /* Turn REG_EH_REGION notes back into NOTE_INSN_EH_REGION notes.
2377 The new note numbers will not refer to region numbers, but
2378 instead to call site entries. */
2380 static unsigned int
2381 convert_to_eh_region_ranges (void)
2383 rtx insn, iter, note;
2384 htab_t ar_hash;
2385 int last_action = -3;
2386 rtx last_action_insn = NULL_RTX;
2387 rtx last_landing_pad = NULL_RTX;
2388 rtx first_no_action_insn = NULL_RTX;
2389 int call_site = 0;
2390 int cur_sec = 0;
2391 rtx section_switch_note = NULL_RTX;
2392 rtx first_no_action_insn_before_switch = NULL_RTX;
2393 rtx last_no_action_insn_before_switch = NULL_RTX;
2394 int saved_call_site_base = call_site_base;
2396 vec_alloc (crtl->eh.action_record_data, 64);
2398 ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
2400 for (iter = get_insns (); iter ; iter = NEXT_INSN (iter))
2401 if (INSN_P (iter))
2403 eh_landing_pad lp;
2404 eh_region region;
2405 bool nothrow;
2406 int this_action;
2407 rtx this_landing_pad;
2409 insn = iter;
2410 if (NONJUMP_INSN_P (insn)
2411 && GET_CODE (PATTERN (insn)) == SEQUENCE)
2412 insn = XVECEXP (PATTERN (insn), 0, 0);
2414 nothrow = get_eh_region_and_lp_from_rtx (insn, &region, &lp);
2415 if (nothrow)
2416 continue;
2417 if (region)
2418 this_action = collect_one_action_chain (ar_hash, region);
2419 else
2420 this_action = -1;
2422 /* Existence of catch handlers, or must-not-throw regions
2423 implies that an lsda is needed (even if empty). */
2424 if (this_action != -1)
2425 crtl->uses_eh_lsda = 1;
2427 /* Delay creation of region notes for no-action regions
2428 until we're sure that an lsda will be required. */
2429 else if (last_action == -3)
2431 first_no_action_insn = iter;
2432 last_action = -1;
2435 if (this_action >= 0)
2436 this_landing_pad = lp->landing_pad;
2437 else
2438 this_landing_pad = NULL_RTX;
2440 /* Differing actions or landing pads implies a change in call-site
2441 info, which implies some EH_REGION note should be emitted. */
2442 if (last_action != this_action
2443 || last_landing_pad != this_landing_pad)
2445 /* If there is a queued no-action region in the other section
2446 with hot/cold partitioning, emit it now. */
2447 if (first_no_action_insn_before_switch)
2449 gcc_assert (this_action != -1
2450 && last_action == (first_no_action_insn
2451 ? -1 : -3));
2452 call_site = add_call_site (NULL_RTX, 0, 0);
2453 note = emit_note_before (NOTE_INSN_EH_REGION_BEG,
2454 first_no_action_insn_before_switch);
2455 NOTE_EH_HANDLER (note) = call_site;
2456 note = emit_note_after (NOTE_INSN_EH_REGION_END,
2457 last_no_action_insn_before_switch);
2458 NOTE_EH_HANDLER (note) = call_site;
2459 gcc_assert (last_action != -3
2460 || (last_action_insn
2461 == last_no_action_insn_before_switch));
2462 first_no_action_insn_before_switch = NULL_RTX;
2463 last_no_action_insn_before_switch = NULL_RTX;
2464 call_site_base++;
2466 /* If we'd not seen a previous action (-3) or the previous
2467 action was must-not-throw (-2), then we do not need an
2468 end note. */
2469 if (last_action >= -1)
2471 /* If we delayed the creation of the begin, do it now. */
2472 if (first_no_action_insn)
2474 call_site = add_call_site (NULL_RTX, 0, cur_sec);
2475 note = emit_note_before (NOTE_INSN_EH_REGION_BEG,
2476 first_no_action_insn);
2477 NOTE_EH_HANDLER (note) = call_site;
2478 first_no_action_insn = NULL_RTX;
2481 note = emit_note_after (NOTE_INSN_EH_REGION_END,
2482 last_action_insn);
2483 NOTE_EH_HANDLER (note) = call_site;
2486 /* If the new action is must-not-throw, then no region notes
2487 are created. */
2488 if (this_action >= -1)
2490 call_site = add_call_site (this_landing_pad,
2491 this_action < 0 ? 0 : this_action,
2492 cur_sec);
2493 note = emit_note_before (NOTE_INSN_EH_REGION_BEG, iter);
2494 NOTE_EH_HANDLER (note) = call_site;
2497 last_action = this_action;
2498 last_landing_pad = this_landing_pad;
2500 last_action_insn = iter;
2502 else if (NOTE_P (iter)
2503 && NOTE_KIND (iter) == NOTE_INSN_SWITCH_TEXT_SECTIONS)
2505 gcc_assert (section_switch_note == NULL_RTX);
2506 gcc_assert (flag_reorder_blocks_and_partition);
2507 section_switch_note = iter;
2508 if (first_no_action_insn)
2510 first_no_action_insn_before_switch = first_no_action_insn;
2511 last_no_action_insn_before_switch = last_action_insn;
2512 first_no_action_insn = NULL_RTX;
2513 gcc_assert (last_action == -1);
2514 last_action = -3;
2516 /* Force closing of current EH region before section switch and
2517 opening a new one afterwards. */
2518 else if (last_action != -3)
2519 last_landing_pad = pc_rtx;
2520 if (crtl->eh.call_site_record_v[cur_sec])
2521 call_site_base += crtl->eh.call_site_record_v[cur_sec]->length ();
2522 cur_sec++;
2523 gcc_assert (crtl->eh.call_site_record_v[cur_sec] == NULL);
2524 vec_alloc (crtl->eh.call_site_record_v[cur_sec], 10);
2527 if (last_action >= -1 && ! first_no_action_insn)
2529 note = emit_note_after (NOTE_INSN_EH_REGION_END, last_action_insn);
2530 NOTE_EH_HANDLER (note) = call_site;
2533 call_site_base = saved_call_site_base;
2535 htab_delete (ar_hash);
2536 return 0;
2539 static bool
2540 gate_convert_to_eh_region_ranges (void)
2542 /* Nothing to do for SJLJ exceptions or if no regions created. */
2543 if (cfun->eh->region_tree == NULL)
2544 return false;
2545 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
2546 return false;
2547 return true;
2550 struct rtl_opt_pass pass_convert_to_eh_region_ranges =
2553 RTL_PASS,
2554 "eh_ranges", /* name */
2555 OPTGROUP_NONE, /* optinfo_flags */
2556 gate_convert_to_eh_region_ranges, /* gate */
2557 convert_to_eh_region_ranges, /* execute */
2558 NULL, /* sub */
2559 NULL, /* next */
2560 0, /* static_pass_number */
2561 TV_NONE, /* tv_id */
2562 0, /* properties_required */
2563 0, /* properties_provided */
2564 0, /* properties_destroyed */
2565 0, /* todo_flags_start */
2566 0 /* todo_flags_finish */
2570 static void
2571 push_uleb128 (vec<uchar, va_gc> **data_area, unsigned int value)
2575 unsigned char byte = value & 0x7f;
2576 value >>= 7;
2577 if (value)
2578 byte |= 0x80;
2579 vec_safe_push (*data_area, byte);
2581 while (value);
2584 static void
2585 push_sleb128 (vec<uchar, va_gc> **data_area, int value)
2587 unsigned char byte;
2588 int more;
2592 byte = value & 0x7f;
2593 value >>= 7;
2594 more = ! ((value == 0 && (byte & 0x40) == 0)
2595 || (value == -1 && (byte & 0x40) != 0));
2596 if (more)
2597 byte |= 0x80;
2598 vec_safe_push (*data_area, byte);
2600 while (more);
2604 #ifndef HAVE_AS_LEB128
2605 static int
2606 dw2_size_of_call_site_table (int section)
2608 int n = vec_safe_length (crtl->eh.call_site_record_v[section]);
2609 int size = n * (4 + 4 + 4);
2610 int i;
2612 for (i = 0; i < n; ++i)
2614 struct call_site_record_d *cs =
2615 (*crtl->eh.call_site_record_v[section])[i];
2616 size += size_of_uleb128 (cs->action);
2619 return size;
2622 static int
2623 sjlj_size_of_call_site_table (void)
2625 int n = vec_safe_length (crtl->eh.call_site_record_v[0]);
2626 int size = 0;
2627 int i;
2629 for (i = 0; i < n; ++i)
2631 struct call_site_record_d *cs =
2632 (*crtl->eh.call_site_record_v[0])[i];
2633 size += size_of_uleb128 (INTVAL (cs->landing_pad));
2634 size += size_of_uleb128 (cs->action);
2637 return size;
2639 #endif
2641 static void
2642 dw2_output_call_site_table (int cs_format, int section)
2644 int n = vec_safe_length (crtl->eh.call_site_record_v[section]);
2645 int i;
2646 const char *begin;
2648 if (section == 0)
2649 begin = current_function_func_begin_label;
2650 else if (first_function_block_is_cold)
2651 begin = crtl->subsections.hot_section_label;
2652 else
2653 begin = crtl->subsections.cold_section_label;
2655 for (i = 0; i < n; ++i)
2657 struct call_site_record_d *cs = (*crtl->eh.call_site_record_v[section])[i];
2658 char reg_start_lab[32];
2659 char reg_end_lab[32];
2660 char landing_pad_lab[32];
2662 ASM_GENERATE_INTERNAL_LABEL (reg_start_lab, "LEHB", call_site_base + i);
2663 ASM_GENERATE_INTERNAL_LABEL (reg_end_lab, "LEHE", call_site_base + i);
2665 if (cs->landing_pad)
2666 ASM_GENERATE_INTERNAL_LABEL (landing_pad_lab, "L",
2667 CODE_LABEL_NUMBER (cs->landing_pad));
2669 /* ??? Perhaps use insn length scaling if the assembler supports
2670 generic arithmetic. */
2671 /* ??? Perhaps use attr_length to choose data1 or data2 instead of
2672 data4 if the function is small enough. */
2673 if (cs_format == DW_EH_PE_uleb128)
2675 dw2_asm_output_delta_uleb128 (reg_start_lab, begin,
2676 "region %d start", i);
2677 dw2_asm_output_delta_uleb128 (reg_end_lab, reg_start_lab,
2678 "length");
2679 if (cs->landing_pad)
2680 dw2_asm_output_delta_uleb128 (landing_pad_lab, begin,
2681 "landing pad");
2682 else
2683 dw2_asm_output_data_uleb128 (0, "landing pad");
2685 else
2687 dw2_asm_output_delta (4, reg_start_lab, begin,
2688 "region %d start", i);
2689 dw2_asm_output_delta (4, reg_end_lab, reg_start_lab, "length");
2690 if (cs->landing_pad)
2691 dw2_asm_output_delta (4, landing_pad_lab, begin,
2692 "landing pad");
2693 else
2694 dw2_asm_output_data (4, 0, "landing pad");
2696 dw2_asm_output_data_uleb128 (cs->action, "action");
2699 call_site_base += n;
2702 static void
2703 sjlj_output_call_site_table (void)
2705 int n = vec_safe_length (crtl->eh.call_site_record_v[0]);
2706 int i;
2708 for (i = 0; i < n; ++i)
2710 struct call_site_record_d *cs = (*crtl->eh.call_site_record_v[0])[i];
2712 dw2_asm_output_data_uleb128 (INTVAL (cs->landing_pad),
2713 "region %d landing pad", i);
2714 dw2_asm_output_data_uleb128 (cs->action, "action");
2717 call_site_base += n;
2720 /* Switch to the section that should be used for exception tables. */
2722 static void
2723 switch_to_exception_section (const char * ARG_UNUSED (fnname))
2725 section *s;
2727 if (exception_section)
2728 s = exception_section;
2729 else
2731 /* Compute the section and cache it into exception_section,
2732 unless it depends on the function name. */
2733 if (targetm_common.have_named_sections)
2735 int flags;
2737 if (EH_TABLES_CAN_BE_READ_ONLY)
2739 int tt_format =
2740 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
2741 flags = ((! flag_pic
2742 || ((tt_format & 0x70) != DW_EH_PE_absptr
2743 && (tt_format & 0x70) != DW_EH_PE_aligned))
2744 ? 0 : SECTION_WRITE);
2746 else
2747 flags = SECTION_WRITE;
2749 #ifdef HAVE_LD_EH_GC_SECTIONS
2750 if (flag_function_sections
2751 || (DECL_ONE_ONLY (current_function_decl) && HAVE_COMDAT_GROUP))
2753 char *section_name = XNEWVEC (char, strlen (fnname) + 32);
2754 /* The EH table must match the code section, so only mark
2755 it linkonce if we have COMDAT groups to tie them together. */
2756 if (DECL_ONE_ONLY (current_function_decl) && HAVE_COMDAT_GROUP)
2757 flags |= SECTION_LINKONCE;
2758 sprintf (section_name, ".gcc_except_table.%s", fnname);
2759 s = get_section (section_name, flags, current_function_decl);
2760 free (section_name);
2762 else
2763 #endif
2764 exception_section
2765 = s = get_section (".gcc_except_table", flags, NULL);
2767 else
2768 exception_section
2769 = s = flag_pic ? data_section : readonly_data_section;
2772 switch_to_section (s);
2776 /* Output a reference from an exception table to the type_info object TYPE.
2777 TT_FORMAT and TT_FORMAT_SIZE describe the DWARF encoding method used for
2778 the value. */
2780 static void
2781 output_ttype (tree type, int tt_format, int tt_format_size)
2783 rtx value;
2784 bool is_public = true;
2786 if (type == NULL_TREE)
2787 value = const0_rtx;
2788 else
2790 /* FIXME lto. pass_ipa_free_lang_data changes all types to
2791 runtime types so TYPE should already be a runtime type
2792 reference. When pass_ipa_free_lang data is made a default
2793 pass, we can then remove the call to lookup_type_for_runtime
2794 below. */
2795 if (TYPE_P (type))
2796 type = lookup_type_for_runtime (type);
2798 value = expand_expr (type, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
2800 /* Let cgraph know that the rtti decl is used. Not all of the
2801 paths below go through assemble_integer, which would take
2802 care of this for us. */
2803 STRIP_NOPS (type);
2804 if (TREE_CODE (type) == ADDR_EXPR)
2806 type = TREE_OPERAND (type, 0);
2807 if (TREE_CODE (type) == VAR_DECL)
2808 is_public = TREE_PUBLIC (type);
2810 else
2811 gcc_assert (TREE_CODE (type) == INTEGER_CST);
2814 /* Allow the target to override the type table entry format. */
2815 if (targetm.asm_out.ttype (value))
2816 return;
2818 if (tt_format == DW_EH_PE_absptr || tt_format == DW_EH_PE_aligned)
2819 assemble_integer (value, tt_format_size,
2820 tt_format_size * BITS_PER_UNIT, 1);
2821 else
2822 dw2_asm_output_encoded_addr_rtx (tt_format, value, is_public, NULL);
2825 static void
2826 output_one_function_exception_table (int section)
2828 int tt_format, cs_format, lp_format, i;
2829 #ifdef HAVE_AS_LEB128
2830 char ttype_label[32];
2831 char cs_after_size_label[32];
2832 char cs_end_label[32];
2833 #else
2834 int call_site_len;
2835 #endif
2836 int have_tt_data;
2837 int tt_format_size = 0;
2839 have_tt_data = (vec_safe_length (cfun->eh->ttype_data)
2840 || (targetm.arm_eabi_unwinder
2841 ? vec_safe_length (cfun->eh->ehspec_data.arm_eabi)
2842 : vec_safe_length (cfun->eh->ehspec_data.other)));
2844 /* Indicate the format of the @TType entries. */
2845 if (! have_tt_data)
2846 tt_format = DW_EH_PE_omit;
2847 else
2849 tt_format = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
2850 #ifdef HAVE_AS_LEB128
2851 ASM_GENERATE_INTERNAL_LABEL (ttype_label,
2852 section ? "LLSDATTC" : "LLSDATT",
2853 current_function_funcdef_no);
2854 #endif
2855 tt_format_size = size_of_encoded_value (tt_format);
2857 assemble_align (tt_format_size * BITS_PER_UNIT);
2860 targetm.asm_out.internal_label (asm_out_file, section ? "LLSDAC" : "LLSDA",
2861 current_function_funcdef_no);
2863 /* The LSDA header. */
2865 /* Indicate the format of the landing pad start pointer. An omitted
2866 field implies @LPStart == @Start. */
2867 /* Currently we always put @LPStart == @Start. This field would
2868 be most useful in moving the landing pads completely out of
2869 line to another section, but it could also be used to minimize
2870 the size of uleb128 landing pad offsets. */
2871 lp_format = DW_EH_PE_omit;
2872 dw2_asm_output_data (1, lp_format, "@LPStart format (%s)",
2873 eh_data_format_name (lp_format));
2875 /* @LPStart pointer would go here. */
2877 dw2_asm_output_data (1, tt_format, "@TType format (%s)",
2878 eh_data_format_name (tt_format));
2880 #ifndef HAVE_AS_LEB128
2881 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
2882 call_site_len = sjlj_size_of_call_site_table ();
2883 else
2884 call_site_len = dw2_size_of_call_site_table (section);
2885 #endif
2887 /* A pc-relative 4-byte displacement to the @TType data. */
2888 if (have_tt_data)
2890 #ifdef HAVE_AS_LEB128
2891 char ttype_after_disp_label[32];
2892 ASM_GENERATE_INTERNAL_LABEL (ttype_after_disp_label,
2893 section ? "LLSDATTDC" : "LLSDATTD",
2894 current_function_funcdef_no);
2895 dw2_asm_output_delta_uleb128 (ttype_label, ttype_after_disp_label,
2896 "@TType base offset");
2897 ASM_OUTPUT_LABEL (asm_out_file, ttype_after_disp_label);
2898 #else
2899 /* Ug. Alignment queers things. */
2900 unsigned int before_disp, after_disp, last_disp, disp;
2902 before_disp = 1 + 1;
2903 after_disp = (1 + size_of_uleb128 (call_site_len)
2904 + call_site_len
2905 + vec_safe_length (crtl->eh.action_record_data)
2906 + (vec_safe_length (cfun->eh->ttype_data)
2907 * tt_format_size));
2909 disp = after_disp;
2912 unsigned int disp_size, pad;
2914 last_disp = disp;
2915 disp_size = size_of_uleb128 (disp);
2916 pad = before_disp + disp_size + after_disp;
2917 if (pad % tt_format_size)
2918 pad = tt_format_size - (pad % tt_format_size);
2919 else
2920 pad = 0;
2921 disp = after_disp + pad;
2923 while (disp != last_disp);
2925 dw2_asm_output_data_uleb128 (disp, "@TType base offset");
2926 #endif
2929 /* Indicate the format of the call-site offsets. */
2930 #ifdef HAVE_AS_LEB128
2931 cs_format = DW_EH_PE_uleb128;
2932 #else
2933 cs_format = DW_EH_PE_udata4;
2934 #endif
2935 dw2_asm_output_data (1, cs_format, "call-site format (%s)",
2936 eh_data_format_name (cs_format));
2938 #ifdef HAVE_AS_LEB128
2939 ASM_GENERATE_INTERNAL_LABEL (cs_after_size_label,
2940 section ? "LLSDACSBC" : "LLSDACSB",
2941 current_function_funcdef_no);
2942 ASM_GENERATE_INTERNAL_LABEL (cs_end_label,
2943 section ? "LLSDACSEC" : "LLSDACSE",
2944 current_function_funcdef_no);
2945 dw2_asm_output_delta_uleb128 (cs_end_label, cs_after_size_label,
2946 "Call-site table length");
2947 ASM_OUTPUT_LABEL (asm_out_file, cs_after_size_label);
2948 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
2949 sjlj_output_call_site_table ();
2950 else
2951 dw2_output_call_site_table (cs_format, section);
2952 ASM_OUTPUT_LABEL (asm_out_file, cs_end_label);
2953 #else
2954 dw2_asm_output_data_uleb128 (call_site_len, "Call-site table length");
2955 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
2956 sjlj_output_call_site_table ();
2957 else
2958 dw2_output_call_site_table (cs_format, section);
2959 #endif
2961 /* ??? Decode and interpret the data for flag_debug_asm. */
2963 uchar uc;
2964 FOR_EACH_VEC_ELT (*crtl->eh.action_record_data, i, uc)
2965 dw2_asm_output_data (1, uc, i ? NULL : "Action record table");
2968 if (have_tt_data)
2969 assemble_align (tt_format_size * BITS_PER_UNIT);
2971 i = vec_safe_length (cfun->eh->ttype_data);
2972 while (i-- > 0)
2974 tree type = (*cfun->eh->ttype_data)[i];
2975 output_ttype (type, tt_format, tt_format_size);
2978 #ifdef HAVE_AS_LEB128
2979 if (have_tt_data)
2980 ASM_OUTPUT_LABEL (asm_out_file, ttype_label);
2981 #endif
2983 /* ??? Decode and interpret the data for flag_debug_asm. */
2984 if (targetm.arm_eabi_unwinder)
2986 tree type;
2987 for (i = 0;
2988 vec_safe_iterate (cfun->eh->ehspec_data.arm_eabi, i, &type); ++i)
2989 output_ttype (type, tt_format, tt_format_size);
2991 else
2993 uchar uc;
2994 for (i = 0;
2995 vec_safe_iterate (cfun->eh->ehspec_data.other, i, &uc); ++i)
2996 dw2_asm_output_data (1, uc,
2997 i ? NULL : "Exception specification table");
3001 void
3002 output_function_exception_table (const char *fnname)
3004 rtx personality = get_personality_function (current_function_decl);
3006 /* Not all functions need anything. */
3007 if (! crtl->uses_eh_lsda)
3008 return;
3010 if (personality)
3012 assemble_external_libcall (personality);
3014 if (targetm.asm_out.emit_except_personality)
3015 targetm.asm_out.emit_except_personality (personality);
3018 switch_to_exception_section (fnname);
3020 /* If the target wants a label to begin the table, emit it here. */
3021 targetm.asm_out.emit_except_table_label (asm_out_file);
3023 output_one_function_exception_table (0);
3024 if (crtl->eh.call_site_record_v[1])
3025 output_one_function_exception_table (1);
3027 switch_to_section (current_function_section ());
3030 void
3031 set_eh_throw_stmt_table (struct function *fun, struct htab *table)
3033 fun->eh->throw_stmt_table = table;
3036 htab_t
3037 get_eh_throw_stmt_table (struct function *fun)
3039 return fun->eh->throw_stmt_table;
3042 /* Determine if the function needs an EH personality function. */
3044 enum eh_personality_kind
3045 function_needs_eh_personality (struct function *fn)
3047 enum eh_personality_kind kind = eh_personality_none;
3048 eh_region i;
3050 FOR_ALL_EH_REGION_FN (i, fn)
3052 switch (i->type)
3054 case ERT_CLEANUP:
3055 /* Can do with any personality including the generic C one. */
3056 kind = eh_personality_any;
3057 break;
3059 case ERT_TRY:
3060 case ERT_ALLOWED_EXCEPTIONS:
3061 /* Always needs a EH personality function. The generic C
3062 personality doesn't handle these even for empty type lists. */
3063 return eh_personality_lang;
3065 case ERT_MUST_NOT_THROW:
3066 /* Always needs a EH personality function. The language may specify
3067 what abort routine that must be used, e.g. std::terminate. */
3068 return eh_personality_lang;
3072 return kind;
3075 /* Dump EH information to OUT. */
3077 void
3078 dump_eh_tree (FILE * out, struct function *fun)
3080 eh_region i;
3081 int depth = 0;
3082 static const char *const type_name[] = {
3083 "cleanup", "try", "allowed_exceptions", "must_not_throw"
3086 i = fun->eh->region_tree;
3087 if (!i)
3088 return;
3090 fprintf (out, "Eh tree:\n");
3091 while (1)
3093 fprintf (out, " %*s %i %s", depth * 2, "",
3094 i->index, type_name[(int) i->type]);
3096 if (i->landing_pads)
3098 eh_landing_pad lp;
3100 fprintf (out, " land:");
3101 if (current_ir_type () == IR_GIMPLE)
3103 for (lp = i->landing_pads; lp ; lp = lp->next_lp)
3105 fprintf (out, "{%i,", lp->index);
3106 print_generic_expr (out, lp->post_landing_pad, 0);
3107 fputc ('}', out);
3108 if (lp->next_lp)
3109 fputc (',', out);
3112 else
3114 for (lp = i->landing_pads; lp ; lp = lp->next_lp)
3116 fprintf (out, "{%i,", lp->index);
3117 if (lp->landing_pad)
3118 fprintf (out, "%i%s,", INSN_UID (lp->landing_pad),
3119 NOTE_P (lp->landing_pad) ? "(del)" : "");
3120 else
3121 fprintf (out, "(nil),");
3122 if (lp->post_landing_pad)
3124 rtx lab = label_rtx (lp->post_landing_pad);
3125 fprintf (out, "%i%s}", INSN_UID (lab),
3126 NOTE_P (lab) ? "(del)" : "");
3128 else
3129 fprintf (out, "(nil)}");
3130 if (lp->next_lp)
3131 fputc (',', out);
3136 switch (i->type)
3138 case ERT_CLEANUP:
3139 case ERT_MUST_NOT_THROW:
3140 break;
3142 case ERT_TRY:
3144 eh_catch c;
3145 fprintf (out, " catch:");
3146 for (c = i->u.eh_try.first_catch; c; c = c->next_catch)
3148 fputc ('{', out);
3149 if (c->label)
3151 fprintf (out, "lab:");
3152 print_generic_expr (out, c->label, 0);
3153 fputc (';', out);
3155 print_generic_expr (out, c->type_list, 0);
3156 fputc ('}', out);
3157 if (c->next_catch)
3158 fputc (',', out);
3161 break;
3163 case ERT_ALLOWED_EXCEPTIONS:
3164 fprintf (out, " filter :%i types:", i->u.allowed.filter);
3165 print_generic_expr (out, i->u.allowed.type_list, 0);
3166 break;
3168 fputc ('\n', out);
3170 /* If there are sub-regions, process them. */
3171 if (i->inner)
3172 i = i->inner, depth++;
3173 /* If there are peers, process them. */
3174 else if (i->next_peer)
3175 i = i->next_peer;
3176 /* Otherwise, step back up the tree to the next peer. */
3177 else
3181 i = i->outer;
3182 depth--;
3183 if (i == NULL)
3184 return;
3186 while (i->next_peer == NULL);
3187 i = i->next_peer;
3192 /* Dump the EH tree for FN on stderr. */
3194 DEBUG_FUNCTION void
3195 debug_eh_tree (struct function *fn)
3197 dump_eh_tree (stderr, fn);
3200 /* Verify invariants on EH datastructures. */
3202 DEBUG_FUNCTION void
3203 verify_eh_tree (struct function *fun)
3205 eh_region r, outer;
3206 int nvisited_lp, nvisited_r;
3207 int count_lp, count_r, depth, i;
3208 eh_landing_pad lp;
3209 bool err = false;
3211 if (!fun->eh->region_tree)
3212 return;
3214 count_r = 0;
3215 for (i = 1; vec_safe_iterate (fun->eh->region_array, i, &r); ++i)
3216 if (r)
3218 if (r->index == i)
3219 count_r++;
3220 else
3222 error ("region_array is corrupted for region %i", r->index);
3223 err = true;
3227 count_lp = 0;
3228 for (i = 1; vec_safe_iterate (fun->eh->lp_array, i, &lp); ++i)
3229 if (lp)
3231 if (lp->index == i)
3232 count_lp++;
3233 else
3235 error ("lp_array is corrupted for lp %i", lp->index);
3236 err = true;
3240 depth = nvisited_lp = nvisited_r = 0;
3241 outer = NULL;
3242 r = fun->eh->region_tree;
3243 while (1)
3245 if ((*fun->eh->region_array)[r->index] != r)
3247 error ("region_array is corrupted for region %i", r->index);
3248 err = true;
3250 if (r->outer != outer)
3252 error ("outer block of region %i is wrong", r->index);
3253 err = true;
3255 if (depth < 0)
3257 error ("negative nesting depth of region %i", r->index);
3258 err = true;
3260 nvisited_r++;
3262 for (lp = r->landing_pads; lp ; lp = lp->next_lp)
3264 if ((*fun->eh->lp_array)[lp->index] != lp)
3266 error ("lp_array is corrupted for lp %i", lp->index);
3267 err = true;
3269 if (lp->region != r)
3271 error ("region of lp %i is wrong", lp->index);
3272 err = true;
3274 nvisited_lp++;
3277 if (r->inner)
3278 outer = r, r = r->inner, depth++;
3279 else if (r->next_peer)
3280 r = r->next_peer;
3281 else
3285 r = r->outer;
3286 if (r == NULL)
3287 goto region_done;
3288 depth--;
3289 outer = r->outer;
3291 while (r->next_peer == NULL);
3292 r = r->next_peer;
3295 region_done:
3296 if (depth != 0)
3298 error ("tree list ends on depth %i", depth);
3299 err = true;
3301 if (count_r != nvisited_r)
3303 error ("region_array does not match region_tree");
3304 err = true;
3306 if (count_lp != nvisited_lp)
3308 error ("lp_array does not match region_tree");
3309 err = true;
3312 if (err)
3314 dump_eh_tree (stderr, fun);
3315 internal_error ("verify_eh_tree failed");
3319 #include "gt-except.h"