2012-10-06 Janus Weil <janus@gcc.gnu.org>
[official-gcc.git] / gcc / except.c
blob88cac856dad36ce8f2a4c325e79a01debc6242ad
1 /* Implements exception handling.
2 Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 2011, 2012 Free Software Foundation, Inc.
5 Contributed by Mike Stump <mrs@cygnus.com>.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
24 /* An exception is an event that can be "thrown" from within a
25 function. This event can then be "caught" by the callers of
26 the function.
28 The representation of exceptions changes several times during
29 the compilation process:
31 In the beginning, in the front end, we have the GENERIC trees
32 TRY_CATCH_EXPR, TRY_FINALLY_EXPR, WITH_CLEANUP_EXPR,
33 CLEANUP_POINT_EXPR, CATCH_EXPR, and EH_FILTER_EXPR.
35 During initial gimplification (gimplify.c) these are lowered
36 to the GIMPLE_TRY, GIMPLE_CATCH, and GIMPLE_EH_FILTER nodes.
37 The WITH_CLEANUP_EXPR and CLEANUP_POINT_EXPR nodes are converted
38 into GIMPLE_TRY_FINALLY nodes; the others are a more direct 1-1
39 conversion.
41 During pass_lower_eh (tree-eh.c) we record the nested structure
42 of the TRY nodes in EH_REGION nodes in CFUN->EH->REGION_TREE.
43 We expand the eh_protect_cleanup_actions langhook into MUST_NOT_THROW
44 regions at this time. We can then flatten the statements within
45 the TRY nodes to straight-line code. Statements that had been within
46 TRY nodes that can throw are recorded within CFUN->EH->THROW_STMT_TABLE,
47 so that we may remember what action is supposed to be taken if
48 a given statement does throw. During this lowering process,
49 we create an EH_LANDING_PAD node for each EH_REGION that has
50 some code within the function that needs to be executed if a
51 throw does happen. We also create RESX statements that are
52 used to transfer control from an inner EH_REGION to an outer
53 EH_REGION. We also create EH_DISPATCH statements as placeholders
54 for a runtime type comparison that should be made in order to
55 select the action to perform among different CATCH and EH_FILTER
56 regions.
58 During pass_lower_eh_dispatch (tree-eh.c), which is run after
59 all inlining is complete, we are able to run assign_filter_values,
60 which allows us to map the set of types manipulated by all of the
61 CATCH and EH_FILTER regions to a set of integers. This set of integers
62 will be how the exception runtime communicates with the code generated
63 within the function. We then expand the GIMPLE_EH_DISPATCH statements
64 to a switch or conditional branches that use the argument provided by
65 the runtime (__builtin_eh_filter) and the set of integers we computed
66 in assign_filter_values.
68 During pass_lower_resx (tree-eh.c), which is run near the end
69 of optimization, we expand RESX statements. If the eh region
70 that is outer to the RESX statement is a MUST_NOT_THROW, then
71 the RESX expands to some form of abort statement. If the eh
72 region that is outer to the RESX statement is within the current
73 function, then the RESX expands to a bookkeeping call
74 (__builtin_eh_copy_values) and a goto. Otherwise, the next
75 handler for the exception must be within a function somewhere
76 up the call chain, so we call back into the exception runtime
77 (__builtin_unwind_resume).
79 During pass_expand (cfgexpand.c), we generate REG_EH_REGION notes
80 that create an rtl to eh_region mapping that corresponds to the
81 gimple to eh_region mapping that had been recorded in the
82 THROW_STMT_TABLE.
84 Then, via finish_eh_generation, we generate the real landing pads
85 to which the runtime will actually transfer control. These new
86 landing pads perform whatever bookkeeping is needed by the target
87 backend in order to resume execution within the current function.
88 Each of these new landing pads falls through into the post_landing_pad
89 label which had been used within the CFG up to this point. All
90 exception edges within the CFG are redirected to the new landing pads.
91 If the target uses setjmp to implement exceptions, the various extra
92 calls into the runtime to register and unregister the current stack
93 frame are emitted at this time.
95 During pass_convert_to_eh_region_ranges (except.c), we transform
96 the REG_EH_REGION notes attached to individual insns into
97 non-overlapping ranges of insns bounded by NOTE_INSN_EH_REGION_BEG
98 and NOTE_INSN_EH_REGION_END. Each insn within such ranges has the
99 same associated action within the exception region tree, meaning
100 that (1) the exception is caught by the same landing pad within the
101 current function, (2) the exception is blocked by the runtime with
102 a MUST_NOT_THROW region, or (3) the exception is not handled at all
103 within the current function.
105 Finally, during assembly generation, we call
106 output_function_exception_table (except.c) to emit the tables with
107 which the exception runtime can determine if a given stack frame
108 handles a given exception, and if so what filter value to provide
109 to the function when the non-local control transfer is effected.
110 If the target uses dwarf2 unwinding to implement exceptions, then
111 output_call_frame_info (dwarf2out.c) emits the required unwind data. */
114 #include "config.h"
115 #include "system.h"
116 #include "coretypes.h"
117 #include "tm.h"
118 #include "rtl.h"
119 #include "tree.h"
120 #include "flags.h"
121 #include "function.h"
122 #include "expr.h"
123 #include "libfuncs.h"
124 #include "insn-config.h"
125 #include "except.h"
126 #include "hard-reg-set.h"
127 #include "basic-block.h"
128 #include "output.h"
129 #include "dwarf2asm.h"
130 #include "dwarf2out.h"
131 #include "dwarf2.h"
132 #include "toplev.h"
133 #include "hashtab.h"
134 #include "intl.h"
135 #include "ggc.h"
136 #include "tm_p.h"
137 #include "target.h"
138 #include "common/common-target.h"
139 #include "langhooks.h"
140 #include "cgraph.h"
141 #include "diagnostic.h"
142 #include "tree-pretty-print.h"
143 #include "tree-pass.h"
144 #include "tree-flow.h"
145 #include "cfgloop.h"
147 /* Provide defaults for stuff that may not be defined when using
148 sjlj exceptions. */
149 #ifndef EH_RETURN_DATA_REGNO
150 #define EH_RETURN_DATA_REGNO(N) INVALID_REGNUM
151 #endif
153 static GTY(()) int call_site_base;
154 static GTY ((param_is (union tree_node)))
155 htab_t type_to_runtime_map;
157 /* Describe the SjLj_Function_Context structure. */
158 static GTY(()) tree sjlj_fc_type_node;
159 static int sjlj_fc_call_site_ofs;
160 static int sjlj_fc_data_ofs;
161 static int sjlj_fc_personality_ofs;
162 static int sjlj_fc_lsda_ofs;
163 static int sjlj_fc_jbuf_ofs;
166 struct GTY(()) call_site_record_d
168 rtx landing_pad;
169 int action;
172 static bool get_eh_region_and_lp_from_rtx (const_rtx, eh_region *,
173 eh_landing_pad *);
175 static int t2r_eq (const void *, const void *);
176 static hashval_t t2r_hash (const void *);
178 static int ttypes_filter_eq (const void *, const void *);
179 static hashval_t ttypes_filter_hash (const void *);
180 static int ehspec_filter_eq (const void *, const void *);
181 static hashval_t ehspec_filter_hash (const void *);
182 static int add_ttypes_entry (htab_t, tree);
183 static int add_ehspec_entry (htab_t, htab_t, tree);
184 static void dw2_build_landing_pads (void);
186 static int action_record_eq (const void *, const void *);
187 static hashval_t action_record_hash (const void *);
188 static int add_action_record (htab_t, int, int);
189 static int collect_one_action_chain (htab_t, eh_region);
190 static int add_call_site (rtx, int, int);
192 static void push_uleb128 (VEC (uchar, gc) **, unsigned int);
193 static void push_sleb128 (VEC (uchar, gc) **, int);
194 #ifndef HAVE_AS_LEB128
195 static int dw2_size_of_call_site_table (int);
196 static int sjlj_size_of_call_site_table (void);
197 #endif
198 static void dw2_output_call_site_table (int, int);
199 static void sjlj_output_call_site_table (void);
202 void
203 init_eh (void)
205 if (! flag_exceptions)
206 return;
208 type_to_runtime_map = htab_create_ggc (31, t2r_hash, t2r_eq, NULL);
210 /* Create the SjLj_Function_Context structure. This should match
211 the definition in unwind-sjlj.c. */
212 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
214 tree f_jbuf, f_per, f_lsda, f_prev, f_cs, f_data, tmp;
216 sjlj_fc_type_node = lang_hooks.types.make_type (RECORD_TYPE);
218 f_prev = build_decl (BUILTINS_LOCATION,
219 FIELD_DECL, get_identifier ("__prev"),
220 build_pointer_type (sjlj_fc_type_node));
221 DECL_FIELD_CONTEXT (f_prev) = sjlj_fc_type_node;
223 f_cs = build_decl (BUILTINS_LOCATION,
224 FIELD_DECL, get_identifier ("__call_site"),
225 integer_type_node);
226 DECL_FIELD_CONTEXT (f_cs) = sjlj_fc_type_node;
228 tmp = build_index_type (size_int (4 - 1));
229 tmp = build_array_type (lang_hooks.types.type_for_mode
230 (targetm.unwind_word_mode (), 1),
231 tmp);
232 f_data = build_decl (BUILTINS_LOCATION,
233 FIELD_DECL, get_identifier ("__data"), tmp);
234 DECL_FIELD_CONTEXT (f_data) = sjlj_fc_type_node;
236 f_per = build_decl (BUILTINS_LOCATION,
237 FIELD_DECL, get_identifier ("__personality"),
238 ptr_type_node);
239 DECL_FIELD_CONTEXT (f_per) = sjlj_fc_type_node;
241 f_lsda = build_decl (BUILTINS_LOCATION,
242 FIELD_DECL, get_identifier ("__lsda"),
243 ptr_type_node);
244 DECL_FIELD_CONTEXT (f_lsda) = sjlj_fc_type_node;
246 #ifdef DONT_USE_BUILTIN_SETJMP
247 #ifdef JMP_BUF_SIZE
248 tmp = size_int (JMP_BUF_SIZE - 1);
249 #else
250 /* Should be large enough for most systems, if it is not,
251 JMP_BUF_SIZE should be defined with the proper value. It will
252 also tend to be larger than necessary for most systems, a more
253 optimal port will define JMP_BUF_SIZE. */
254 tmp = size_int (FIRST_PSEUDO_REGISTER + 2 - 1);
255 #endif
256 #else
257 /* builtin_setjmp takes a pointer to 5 words. */
258 tmp = size_int (5 * BITS_PER_WORD / POINTER_SIZE - 1);
259 #endif
260 tmp = build_index_type (tmp);
261 tmp = build_array_type (ptr_type_node, tmp);
262 f_jbuf = build_decl (BUILTINS_LOCATION,
263 FIELD_DECL, get_identifier ("__jbuf"), tmp);
264 #ifdef DONT_USE_BUILTIN_SETJMP
265 /* We don't know what the alignment requirements of the
266 runtime's jmp_buf has. Overestimate. */
267 DECL_ALIGN (f_jbuf) = BIGGEST_ALIGNMENT;
268 DECL_USER_ALIGN (f_jbuf) = 1;
269 #endif
270 DECL_FIELD_CONTEXT (f_jbuf) = sjlj_fc_type_node;
272 TYPE_FIELDS (sjlj_fc_type_node) = f_prev;
273 TREE_CHAIN (f_prev) = f_cs;
274 TREE_CHAIN (f_cs) = f_data;
275 TREE_CHAIN (f_data) = f_per;
276 TREE_CHAIN (f_per) = f_lsda;
277 TREE_CHAIN (f_lsda) = f_jbuf;
279 layout_type (sjlj_fc_type_node);
281 /* Cache the interesting field offsets so that we have
282 easy access from rtl. */
283 sjlj_fc_call_site_ofs
284 = (tree_low_cst (DECL_FIELD_OFFSET (f_cs), 1)
285 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_cs), 1) / BITS_PER_UNIT);
286 sjlj_fc_data_ofs
287 = (tree_low_cst (DECL_FIELD_OFFSET (f_data), 1)
288 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_data), 1) / BITS_PER_UNIT);
289 sjlj_fc_personality_ofs
290 = (tree_low_cst (DECL_FIELD_OFFSET (f_per), 1)
291 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_per), 1) / BITS_PER_UNIT);
292 sjlj_fc_lsda_ofs
293 = (tree_low_cst (DECL_FIELD_OFFSET (f_lsda), 1)
294 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_lsda), 1) / BITS_PER_UNIT);
295 sjlj_fc_jbuf_ofs
296 = (tree_low_cst (DECL_FIELD_OFFSET (f_jbuf), 1)
297 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_jbuf), 1) / BITS_PER_UNIT);
301 void
302 init_eh_for_function (void)
304 cfun->eh = ggc_alloc_cleared_eh_status ();
306 /* Make sure zero'th entries are used. */
307 VEC_safe_push (eh_region, gc, cfun->eh->region_array, NULL);
308 VEC_safe_push (eh_landing_pad, gc, cfun->eh->lp_array, NULL);
311 /* Routines to generate the exception tree somewhat directly.
312 These are used from tree-eh.c when processing exception related
313 nodes during tree optimization. */
315 static eh_region
316 gen_eh_region (enum eh_region_type type, eh_region outer)
318 eh_region new_eh;
320 /* Insert a new blank region as a leaf in the tree. */
321 new_eh = ggc_alloc_cleared_eh_region_d ();
322 new_eh->type = type;
323 new_eh->outer = outer;
324 if (outer)
326 new_eh->next_peer = outer->inner;
327 outer->inner = new_eh;
329 else
331 new_eh->next_peer = cfun->eh->region_tree;
332 cfun->eh->region_tree = new_eh;
335 new_eh->index = VEC_length (eh_region, cfun->eh->region_array);
336 VEC_safe_push (eh_region, gc, cfun->eh->region_array, new_eh);
338 /* Copy the language's notion of whether to use __cxa_end_cleanup. */
339 if (targetm.arm_eabi_unwinder && lang_hooks.eh_use_cxa_end_cleanup)
340 new_eh->use_cxa_end_cleanup = true;
342 return new_eh;
345 eh_region
346 gen_eh_region_cleanup (eh_region outer)
348 return gen_eh_region (ERT_CLEANUP, outer);
351 eh_region
352 gen_eh_region_try (eh_region outer)
354 return gen_eh_region (ERT_TRY, outer);
357 eh_catch
358 gen_eh_region_catch (eh_region t, tree type_or_list)
360 eh_catch c, l;
361 tree type_list, type_node;
363 gcc_assert (t->type == ERT_TRY);
365 /* Ensure to always end up with a type list to normalize further
366 processing, then register each type against the runtime types map. */
367 type_list = type_or_list;
368 if (type_or_list)
370 if (TREE_CODE (type_or_list) != TREE_LIST)
371 type_list = tree_cons (NULL_TREE, type_or_list, NULL_TREE);
373 type_node = type_list;
374 for (; type_node; type_node = TREE_CHAIN (type_node))
375 add_type_for_runtime (TREE_VALUE (type_node));
378 c = ggc_alloc_cleared_eh_catch_d ();
379 c->type_list = type_list;
380 l = t->u.eh_try.last_catch;
381 c->prev_catch = l;
382 if (l)
383 l->next_catch = c;
384 else
385 t->u.eh_try.first_catch = c;
386 t->u.eh_try.last_catch = c;
388 return c;
391 eh_region
392 gen_eh_region_allowed (eh_region outer, tree allowed)
394 eh_region region = gen_eh_region (ERT_ALLOWED_EXCEPTIONS, outer);
395 region->u.allowed.type_list = allowed;
397 for (; allowed ; allowed = TREE_CHAIN (allowed))
398 add_type_for_runtime (TREE_VALUE (allowed));
400 return region;
403 eh_region
404 gen_eh_region_must_not_throw (eh_region outer)
406 return gen_eh_region (ERT_MUST_NOT_THROW, outer);
409 eh_landing_pad
410 gen_eh_landing_pad (eh_region region)
412 eh_landing_pad lp = ggc_alloc_cleared_eh_landing_pad_d ();
414 lp->next_lp = region->landing_pads;
415 lp->region = region;
416 lp->index = VEC_length (eh_landing_pad, cfun->eh->lp_array);
417 region->landing_pads = lp;
419 VEC_safe_push (eh_landing_pad, gc, cfun->eh->lp_array, lp);
421 return lp;
424 eh_region
425 get_eh_region_from_number_fn (struct function *ifun, int i)
427 return VEC_index (eh_region, ifun->eh->region_array, i);
430 eh_region
431 get_eh_region_from_number (int i)
433 return get_eh_region_from_number_fn (cfun, i);
436 eh_landing_pad
437 get_eh_landing_pad_from_number_fn (struct function *ifun, int i)
439 return VEC_index (eh_landing_pad, ifun->eh->lp_array, i);
442 eh_landing_pad
443 get_eh_landing_pad_from_number (int i)
445 return get_eh_landing_pad_from_number_fn (cfun, i);
448 eh_region
449 get_eh_region_from_lp_number_fn (struct function *ifun, int i)
451 if (i < 0)
452 return VEC_index (eh_region, ifun->eh->region_array, -i);
453 else if (i == 0)
454 return NULL;
455 else
457 eh_landing_pad lp;
458 lp = VEC_index (eh_landing_pad, ifun->eh->lp_array, i);
459 return lp->region;
463 eh_region
464 get_eh_region_from_lp_number (int i)
466 return get_eh_region_from_lp_number_fn (cfun, i);
469 /* Returns true if the current function has exception handling regions. */
471 bool
472 current_function_has_exception_handlers (void)
474 return cfun->eh->region_tree != NULL;
477 /* A subroutine of duplicate_eh_regions. Copy the eh_region tree at OLD.
478 Root it at OUTER, and apply LP_OFFSET to the lp numbers. */
480 struct duplicate_eh_regions_data
482 duplicate_eh_regions_map label_map;
483 void *label_map_data;
484 struct pointer_map_t *eh_map;
487 static void
488 duplicate_eh_regions_1 (struct duplicate_eh_regions_data *data,
489 eh_region old_r, eh_region outer)
491 eh_landing_pad old_lp, new_lp;
492 eh_region new_r;
493 void **slot;
495 new_r = gen_eh_region (old_r->type, outer);
496 slot = pointer_map_insert (data->eh_map, (void *)old_r);
497 gcc_assert (*slot == NULL);
498 *slot = (void *)new_r;
500 switch (old_r->type)
502 case ERT_CLEANUP:
503 break;
505 case ERT_TRY:
507 eh_catch oc, nc;
508 for (oc = old_r->u.eh_try.first_catch; oc ; oc = oc->next_catch)
510 /* We should be doing all our region duplication before and
511 during inlining, which is before filter lists are created. */
512 gcc_assert (oc->filter_list == NULL);
513 nc = gen_eh_region_catch (new_r, oc->type_list);
514 nc->label = data->label_map (oc->label, data->label_map_data);
517 break;
519 case ERT_ALLOWED_EXCEPTIONS:
520 new_r->u.allowed.type_list = old_r->u.allowed.type_list;
521 if (old_r->u.allowed.label)
522 new_r->u.allowed.label
523 = data->label_map (old_r->u.allowed.label, data->label_map_data);
524 else
525 new_r->u.allowed.label = NULL_TREE;
526 break;
528 case ERT_MUST_NOT_THROW:
529 new_r->u.must_not_throw.failure_loc =
530 LOCATION_LOCUS (old_r->u.must_not_throw.failure_loc);
531 new_r->u.must_not_throw.failure_decl =
532 old_r->u.must_not_throw.failure_decl;
533 break;
536 for (old_lp = old_r->landing_pads; old_lp ; old_lp = old_lp->next_lp)
538 /* Don't bother copying unused landing pads. */
539 if (old_lp->post_landing_pad == NULL)
540 continue;
542 new_lp = gen_eh_landing_pad (new_r);
543 slot = pointer_map_insert (data->eh_map, (void *)old_lp);
544 gcc_assert (*slot == NULL);
545 *slot = (void *)new_lp;
547 new_lp->post_landing_pad
548 = data->label_map (old_lp->post_landing_pad, data->label_map_data);
549 EH_LANDING_PAD_NR (new_lp->post_landing_pad) = new_lp->index;
552 /* Make sure to preserve the original use of __cxa_end_cleanup. */
553 new_r->use_cxa_end_cleanup = old_r->use_cxa_end_cleanup;
555 for (old_r = old_r->inner; old_r ; old_r = old_r->next_peer)
556 duplicate_eh_regions_1 (data, old_r, new_r);
559 /* Duplicate the EH regions from IFUN rooted at COPY_REGION into
560 the current function and root the tree below OUTER_REGION.
561 The special case of COPY_REGION of NULL means all regions.
562 Remap labels using MAP/MAP_DATA callback. Return a pointer map
563 that allows the caller to remap uses of both EH regions and
564 EH landing pads. */
566 struct pointer_map_t *
567 duplicate_eh_regions (struct function *ifun,
568 eh_region copy_region, int outer_lp,
569 duplicate_eh_regions_map map, void *map_data)
571 struct duplicate_eh_regions_data data;
572 eh_region outer_region;
574 #ifdef ENABLE_CHECKING
575 verify_eh_tree (ifun);
576 #endif
578 data.label_map = map;
579 data.label_map_data = map_data;
580 data.eh_map = pointer_map_create ();
582 outer_region = get_eh_region_from_lp_number (outer_lp);
584 /* Copy all the regions in the subtree. */
585 if (copy_region)
586 duplicate_eh_regions_1 (&data, copy_region, outer_region);
587 else
589 eh_region r;
590 for (r = ifun->eh->region_tree; r ; r = r->next_peer)
591 duplicate_eh_regions_1 (&data, r, outer_region);
594 #ifdef ENABLE_CHECKING
595 verify_eh_tree (cfun);
596 #endif
598 return data.eh_map;
601 /* Return the region that is outer to both REGION_A and REGION_B in IFUN. */
603 eh_region
604 eh_region_outermost (struct function *ifun, eh_region region_a,
605 eh_region region_b)
607 sbitmap b_outer;
609 gcc_assert (ifun->eh->region_array);
610 gcc_assert (ifun->eh->region_tree);
612 b_outer = sbitmap_alloc (VEC_length (eh_region, ifun->eh->region_array));
613 sbitmap_zero (b_outer);
617 SET_BIT (b_outer, region_b->index);
618 region_b = region_b->outer;
620 while (region_b);
624 if (TEST_BIT (b_outer, region_a->index))
625 break;
626 region_a = region_a->outer;
628 while (region_a);
630 sbitmap_free (b_outer);
631 return region_a;
634 static int
635 t2r_eq (const void *pentry, const void *pdata)
637 const_tree const entry = (const_tree) pentry;
638 const_tree const data = (const_tree) pdata;
640 return TREE_PURPOSE (entry) == data;
643 static hashval_t
644 t2r_hash (const void *pentry)
646 const_tree const entry = (const_tree) pentry;
647 return TREE_HASH (TREE_PURPOSE (entry));
650 void
651 add_type_for_runtime (tree type)
653 tree *slot;
655 /* If TYPE is NOP_EXPR, it means that it already is a runtime type. */
656 if (TREE_CODE (type) == NOP_EXPR)
657 return;
659 slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
660 TREE_HASH (type), INSERT);
661 if (*slot == NULL)
663 tree runtime = lang_hooks.eh_runtime_type (type);
664 *slot = tree_cons (type, runtime, NULL_TREE);
668 tree
669 lookup_type_for_runtime (tree type)
671 tree *slot;
673 /* If TYPE is NOP_EXPR, it means that it already is a runtime type. */
674 if (TREE_CODE (type) == NOP_EXPR)
675 return type;
677 slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
678 TREE_HASH (type), NO_INSERT);
680 /* We should have always inserted the data earlier. */
681 return TREE_VALUE (*slot);
685 /* Represent an entry in @TTypes for either catch actions
686 or exception filter actions. */
687 struct ttypes_filter {
688 tree t;
689 int filter;
692 /* Compare ENTRY (a ttypes_filter entry in the hash table) with DATA
693 (a tree) for a @TTypes type node we are thinking about adding. */
695 static int
696 ttypes_filter_eq (const void *pentry, const void *pdata)
698 const struct ttypes_filter *const entry
699 = (const struct ttypes_filter *) pentry;
700 const_tree const data = (const_tree) pdata;
702 return entry->t == data;
705 static hashval_t
706 ttypes_filter_hash (const void *pentry)
708 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
709 return TREE_HASH (entry->t);
712 /* Compare ENTRY with DATA (both struct ttypes_filter) for a @TTypes
713 exception specification list we are thinking about adding. */
714 /* ??? Currently we use the type lists in the order given. Someone
715 should put these in some canonical order. */
717 static int
718 ehspec_filter_eq (const void *pentry, const void *pdata)
720 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
721 const struct ttypes_filter *data = (const struct ttypes_filter *) pdata;
723 return type_list_equal (entry->t, data->t);
726 /* Hash function for exception specification lists. */
728 static hashval_t
729 ehspec_filter_hash (const void *pentry)
731 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
732 hashval_t h = 0;
733 tree list;
735 for (list = entry->t; list ; list = TREE_CHAIN (list))
736 h = (h << 5) + (h >> 27) + TREE_HASH (TREE_VALUE (list));
737 return h;
740 /* Add TYPE (which may be NULL) to cfun->eh->ttype_data, using TYPES_HASH
741 to speed up the search. Return the filter value to be used. */
743 static int
744 add_ttypes_entry (htab_t ttypes_hash, tree type)
746 struct ttypes_filter **slot, *n;
748 slot = (struct ttypes_filter **)
749 htab_find_slot_with_hash (ttypes_hash, type, TREE_HASH (type), INSERT);
751 if ((n = *slot) == NULL)
753 /* Filter value is a 1 based table index. */
755 n = XNEW (struct ttypes_filter);
756 n->t = type;
757 n->filter = VEC_length (tree, cfun->eh->ttype_data) + 1;
758 *slot = n;
760 VEC_safe_push (tree, gc, cfun->eh->ttype_data, type);
763 return n->filter;
766 /* Add LIST to cfun->eh->ehspec_data, using EHSPEC_HASH and TYPES_HASH
767 to speed up the search. Return the filter value to be used. */
769 static int
770 add_ehspec_entry (htab_t ehspec_hash, htab_t ttypes_hash, tree list)
772 struct ttypes_filter **slot, *n;
773 struct ttypes_filter dummy;
775 dummy.t = list;
776 slot = (struct ttypes_filter **)
777 htab_find_slot (ehspec_hash, &dummy, INSERT);
779 if ((n = *slot) == NULL)
781 int len;
783 if (targetm.arm_eabi_unwinder)
784 len = VEC_length (tree, cfun->eh->ehspec_data.arm_eabi);
785 else
786 len = VEC_length (uchar, cfun->eh->ehspec_data.other);
788 /* Filter value is a -1 based byte index into a uleb128 buffer. */
790 n = XNEW (struct ttypes_filter);
791 n->t = list;
792 n->filter = -(len + 1);
793 *slot = n;
795 /* Generate a 0 terminated list of filter values. */
796 for (; list ; list = TREE_CHAIN (list))
798 if (targetm.arm_eabi_unwinder)
799 VEC_safe_push (tree, gc, cfun->eh->ehspec_data.arm_eabi,
800 TREE_VALUE (list));
801 else
803 /* Look up each type in the list and encode its filter
804 value as a uleb128. */
805 push_uleb128 (&cfun->eh->ehspec_data.other,
806 add_ttypes_entry (ttypes_hash, TREE_VALUE (list)));
809 if (targetm.arm_eabi_unwinder)
810 VEC_safe_push (tree, gc, cfun->eh->ehspec_data.arm_eabi, NULL_TREE);
811 else
812 VEC_safe_push (uchar, gc, cfun->eh->ehspec_data.other, 0);
815 return n->filter;
818 /* Generate the action filter values to be used for CATCH and
819 ALLOWED_EXCEPTIONS regions. When using dwarf2 exception regions,
820 we use lots of landing pads, and so every type or list can share
821 the same filter value, which saves table space. */
823 void
824 assign_filter_values (void)
826 int i;
827 htab_t ttypes, ehspec;
828 eh_region r;
829 eh_catch c;
831 cfun->eh->ttype_data = VEC_alloc (tree, gc, 16);
832 if (targetm.arm_eabi_unwinder)
833 cfun->eh->ehspec_data.arm_eabi = VEC_alloc (tree, gc, 64);
834 else
835 cfun->eh->ehspec_data.other = VEC_alloc (uchar, gc, 64);
837 ttypes = htab_create (31, ttypes_filter_hash, ttypes_filter_eq, free);
838 ehspec = htab_create (31, ehspec_filter_hash, ehspec_filter_eq, free);
840 for (i = 1; VEC_iterate (eh_region, cfun->eh->region_array, i, r); ++i)
842 if (r == NULL)
843 continue;
845 switch (r->type)
847 case ERT_TRY:
848 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
850 /* Whatever type_list is (NULL or true list), we build a list
851 of filters for the region. */
852 c->filter_list = NULL_TREE;
854 if (c->type_list != NULL)
856 /* Get a filter value for each of the types caught and store
857 them in the region's dedicated list. */
858 tree tp_node = c->type_list;
860 for ( ; tp_node; tp_node = TREE_CHAIN (tp_node))
862 int flt = add_ttypes_entry (ttypes, TREE_VALUE (tp_node));
863 tree flt_node = build_int_cst (integer_type_node, flt);
865 c->filter_list
866 = tree_cons (NULL_TREE, flt_node, c->filter_list);
869 else
871 /* Get a filter value for the NULL list also since it
872 will need an action record anyway. */
873 int flt = add_ttypes_entry (ttypes, NULL);
874 tree flt_node = build_int_cst (integer_type_node, flt);
876 c->filter_list
877 = tree_cons (NULL_TREE, flt_node, NULL);
880 break;
882 case ERT_ALLOWED_EXCEPTIONS:
883 r->u.allowed.filter
884 = add_ehspec_entry (ehspec, ttypes, r->u.allowed.type_list);
885 break;
887 default:
888 break;
892 htab_delete (ttypes);
893 htab_delete (ehspec);
896 /* Emit SEQ into basic block just before INSN (that is assumed to be
897 first instruction of some existing BB and return the newly
898 produced block. */
899 static basic_block
900 emit_to_new_bb_before (rtx seq, rtx insn)
902 rtx last;
903 basic_block bb, prev_bb;
904 edge e;
905 edge_iterator ei;
907 /* If there happens to be a fallthru edge (possibly created by cleanup_cfg
908 call), we don't want it to go into newly created landing pad or other EH
909 construct. */
910 for (ei = ei_start (BLOCK_FOR_INSN (insn)->preds); (e = ei_safe_edge (ei)); )
911 if (e->flags & EDGE_FALLTHRU)
912 force_nonfallthru (e);
913 else
914 ei_next (&ei);
915 last = emit_insn_before (seq, insn);
916 if (BARRIER_P (last))
917 last = PREV_INSN (last);
918 prev_bb = BLOCK_FOR_INSN (insn)->prev_bb;
919 bb = create_basic_block (seq, last, prev_bb);
920 update_bb_for_insn (bb);
921 bb->flags |= BB_SUPERBLOCK;
922 return bb;
925 /* A subroutine of dw2_build_landing_pads, also used for edge splitting
926 at the rtl level. Emit the code required by the target at a landing
927 pad for the given region. */
929 void
930 expand_dw2_landing_pad_for_region (eh_region region)
932 #ifdef HAVE_exception_receiver
933 if (HAVE_exception_receiver)
934 emit_insn (gen_exception_receiver ());
935 else
936 #endif
937 #ifdef HAVE_nonlocal_goto_receiver
938 if (HAVE_nonlocal_goto_receiver)
939 emit_insn (gen_nonlocal_goto_receiver ());
940 else
941 #endif
942 { /* Nothing */ }
944 if (region->exc_ptr_reg)
945 emit_move_insn (region->exc_ptr_reg,
946 gen_rtx_REG (ptr_mode, EH_RETURN_DATA_REGNO (0)));
947 if (region->filter_reg)
948 emit_move_insn (region->filter_reg,
949 gen_rtx_REG (targetm.eh_return_filter_mode (),
950 EH_RETURN_DATA_REGNO (1)));
953 /* Expand the extra code needed at landing pads for dwarf2 unwinding. */
955 static void
956 dw2_build_landing_pads (void)
958 int i;
959 eh_landing_pad lp;
960 int e_flags = EDGE_FALLTHRU;
962 /* If we're going to partition blocks, we need to be able to add
963 new landing pads later, which means that we need to hold on to
964 the post-landing-pad block. Prevent it from being merged away.
965 We'll remove this bit after partitioning. */
966 if (flag_reorder_blocks_and_partition)
967 e_flags |= EDGE_PRESERVE;
969 for (i = 1; VEC_iterate (eh_landing_pad, cfun->eh->lp_array, i, lp); ++i)
971 basic_block bb;
972 rtx seq;
973 edge e;
975 if (lp == NULL || lp->post_landing_pad == NULL)
976 continue;
978 start_sequence ();
980 lp->landing_pad = gen_label_rtx ();
981 emit_label (lp->landing_pad);
982 LABEL_PRESERVE_P (lp->landing_pad) = 1;
984 expand_dw2_landing_pad_for_region (lp->region);
986 seq = get_insns ();
987 end_sequence ();
989 bb = emit_to_new_bb_before (seq, label_rtx (lp->post_landing_pad));
990 e = make_edge (bb, bb->next_bb, e_flags);
991 e->count = bb->count;
992 e->probability = REG_BR_PROB_BASE;
993 if (current_loops)
995 struct loop *loop = bb->next_bb->loop_father;
996 /* If we created a pre-header block, add the new block to the
997 outer loop, otherwise to the loop itself. */
998 if (bb->next_bb == loop->header)
999 add_bb_to_loop (bb, loop_outer (loop));
1000 else
1001 add_bb_to_loop (bb, loop);
1007 static VEC (int, heap) *sjlj_lp_call_site_index;
1009 /* Process all active landing pads. Assign each one a compact dispatch
1010 index, and a call-site index. */
1012 static int
1013 sjlj_assign_call_site_values (void)
1015 htab_t ar_hash;
1016 int i, disp_index;
1017 eh_landing_pad lp;
1019 crtl->eh.action_record_data = VEC_alloc (uchar, gc, 64);
1020 ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
1022 disp_index = 0;
1023 call_site_base = 1;
1024 for (i = 1; VEC_iterate (eh_landing_pad, cfun->eh->lp_array, i, lp); ++i)
1025 if (lp && lp->post_landing_pad)
1027 int action, call_site;
1029 /* First: build the action table. */
1030 action = collect_one_action_chain (ar_hash, lp->region);
1032 /* Next: assign call-site values. If dwarf2 terms, this would be
1033 the region number assigned by convert_to_eh_region_ranges, but
1034 handles no-action and must-not-throw differently. */
1035 /* Map must-not-throw to otherwise unused call-site index 0. */
1036 if (action == -2)
1037 call_site = 0;
1038 /* Map no-action to otherwise unused call-site index -1. */
1039 else if (action == -1)
1040 call_site = -1;
1041 /* Otherwise, look it up in the table. */
1042 else
1043 call_site = add_call_site (GEN_INT (disp_index), action, 0);
1044 VEC_replace (int, sjlj_lp_call_site_index, i, call_site);
1046 disp_index++;
1049 htab_delete (ar_hash);
1051 return disp_index;
1054 /* Emit code to record the current call-site index before every
1055 insn that can throw. */
1057 static void
1058 sjlj_mark_call_sites (void)
1060 int last_call_site = -2;
1061 rtx insn, mem;
1063 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
1065 eh_landing_pad lp;
1066 eh_region r;
1067 bool nothrow;
1068 int this_call_site;
1069 rtx before, p;
1071 /* Reset value tracking at extended basic block boundaries. */
1072 if (LABEL_P (insn))
1073 last_call_site = -2;
1075 if (! INSN_P (insn))
1076 continue;
1078 nothrow = get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1079 if (nothrow)
1080 continue;
1081 if (lp)
1082 this_call_site = VEC_index (int, sjlj_lp_call_site_index, lp->index);
1083 else if (r == NULL)
1085 /* Calls (and trapping insns) without notes are outside any
1086 exception handling region in this function. Mark them as
1087 no action. */
1088 this_call_site = -1;
1090 else
1092 gcc_assert (r->type == ERT_MUST_NOT_THROW);
1093 this_call_site = 0;
1096 if (this_call_site != -1)
1097 crtl->uses_eh_lsda = 1;
1099 if (this_call_site == last_call_site)
1100 continue;
1102 /* Don't separate a call from it's argument loads. */
1103 before = insn;
1104 if (CALL_P (insn))
1105 before = find_first_parameter_load (insn, NULL_RTX);
1107 start_sequence ();
1108 mem = adjust_address (crtl->eh.sjlj_fc, TYPE_MODE (integer_type_node),
1109 sjlj_fc_call_site_ofs);
1110 emit_move_insn (mem, GEN_INT (this_call_site));
1111 p = get_insns ();
1112 end_sequence ();
1114 emit_insn_before (p, before);
1115 last_call_site = this_call_site;
1119 /* Construct the SjLj_Function_Context. */
1121 static void
1122 sjlj_emit_function_enter (rtx dispatch_label)
1124 rtx fn_begin, fc, mem, seq;
1125 bool fn_begin_outside_block;
1126 rtx personality = get_personality_function (current_function_decl);
1128 fc = crtl->eh.sjlj_fc;
1130 start_sequence ();
1132 /* We're storing this libcall's address into memory instead of
1133 calling it directly. Thus, we must call assemble_external_libcall
1134 here, as we can not depend on emit_library_call to do it for us. */
1135 assemble_external_libcall (personality);
1136 mem = adjust_address (fc, Pmode, sjlj_fc_personality_ofs);
1137 emit_move_insn (mem, personality);
1139 mem = adjust_address (fc, Pmode, sjlj_fc_lsda_ofs);
1140 if (crtl->uses_eh_lsda)
1142 char buf[20];
1143 rtx sym;
1145 ASM_GENERATE_INTERNAL_LABEL (buf, "LLSDA", current_function_funcdef_no);
1146 sym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
1147 SYMBOL_REF_FLAGS (sym) = SYMBOL_FLAG_LOCAL;
1148 emit_move_insn (mem, sym);
1150 else
1151 emit_move_insn (mem, const0_rtx);
1153 if (dispatch_label)
1155 #ifdef DONT_USE_BUILTIN_SETJMP
1156 rtx x, last;
1157 x = emit_library_call_value (setjmp_libfunc, NULL_RTX, LCT_RETURNS_TWICE,
1158 TYPE_MODE (integer_type_node), 1,
1159 plus_constant (Pmode, XEXP (fc, 0),
1160 sjlj_fc_jbuf_ofs), Pmode);
1162 emit_cmp_and_jump_insns (x, const0_rtx, NE, 0,
1163 TYPE_MODE (integer_type_node), 0,
1164 dispatch_label);
1165 last = get_last_insn ();
1166 if (JUMP_P (last) && any_condjump_p (last))
1168 gcc_assert (!find_reg_note (last, REG_BR_PROB, 0));
1169 add_reg_note (last, REG_BR_PROB, GEN_INT (REG_BR_PROB_BASE / 100));
1171 #else
1172 expand_builtin_setjmp_setup (plus_constant (Pmode, XEXP (fc, 0),
1173 sjlj_fc_jbuf_ofs),
1174 dispatch_label);
1175 #endif
1178 emit_library_call (unwind_sjlj_register_libfunc, LCT_NORMAL, VOIDmode,
1179 1, XEXP (fc, 0), Pmode);
1181 seq = get_insns ();
1182 end_sequence ();
1184 /* ??? Instead of doing this at the beginning of the function,
1185 do this in a block that is at loop level 0 and dominates all
1186 can_throw_internal instructions. */
1188 fn_begin_outside_block = true;
1189 for (fn_begin = get_insns (); ; fn_begin = NEXT_INSN (fn_begin))
1190 if (NOTE_P (fn_begin))
1192 if (NOTE_KIND (fn_begin) == NOTE_INSN_FUNCTION_BEG)
1193 break;
1194 else if (NOTE_INSN_BASIC_BLOCK_P (fn_begin))
1195 fn_begin_outside_block = false;
1198 if (fn_begin_outside_block)
1199 insert_insn_on_edge (seq, single_succ_edge (ENTRY_BLOCK_PTR));
1200 else
1201 emit_insn_after (seq, fn_begin);
1204 /* Call back from expand_function_end to know where we should put
1205 the call to unwind_sjlj_unregister_libfunc if needed. */
1207 void
1208 sjlj_emit_function_exit_after (rtx after)
1210 crtl->eh.sjlj_exit_after = after;
1213 static void
1214 sjlj_emit_function_exit (void)
1216 rtx seq, insn;
1218 start_sequence ();
1220 emit_library_call (unwind_sjlj_unregister_libfunc, LCT_NORMAL, VOIDmode,
1221 1, XEXP (crtl->eh.sjlj_fc, 0), Pmode);
1223 seq = get_insns ();
1224 end_sequence ();
1226 /* ??? Really this can be done in any block at loop level 0 that
1227 post-dominates all can_throw_internal instructions. This is
1228 the last possible moment. */
1230 insn = crtl->eh.sjlj_exit_after;
1231 if (LABEL_P (insn))
1232 insn = NEXT_INSN (insn);
1234 emit_insn_after (seq, insn);
1237 static void
1238 sjlj_emit_dispatch_table (rtx dispatch_label, int num_dispatch)
1240 enum machine_mode unwind_word_mode = targetm.unwind_word_mode ();
1241 enum machine_mode filter_mode = targetm.eh_return_filter_mode ();
1242 eh_landing_pad lp;
1243 rtx mem, seq, fc, before, exc_ptr_reg, filter_reg;
1244 rtx first_reachable_label;
1245 basic_block bb;
1246 eh_region r;
1247 edge e;
1248 int i, disp_index;
1249 VEC(tree, heap) *dispatch_labels = NULL;
1251 fc = crtl->eh.sjlj_fc;
1253 start_sequence ();
1255 emit_label (dispatch_label);
1257 #ifndef DONT_USE_BUILTIN_SETJMP
1258 expand_builtin_setjmp_receiver (dispatch_label);
1260 /* The caller of expand_builtin_setjmp_receiver is responsible for
1261 making sure that the label doesn't vanish. The only other caller
1262 is the expander for __builtin_setjmp_receiver, which places this
1263 label on the nonlocal_goto_label list. Since we're modeling these
1264 CFG edges more exactly, we can use the forced_labels list instead. */
1265 LABEL_PRESERVE_P (dispatch_label) = 1;
1266 forced_labels
1267 = gen_rtx_EXPR_LIST (VOIDmode, dispatch_label, forced_labels);
1268 #endif
1270 /* Load up exc_ptr and filter values from the function context. */
1271 mem = adjust_address (fc, unwind_word_mode, sjlj_fc_data_ofs);
1272 if (unwind_word_mode != ptr_mode)
1274 #ifdef POINTERS_EXTEND_UNSIGNED
1275 mem = convert_memory_address (ptr_mode, mem);
1276 #else
1277 mem = convert_to_mode (ptr_mode, mem, 0);
1278 #endif
1280 exc_ptr_reg = force_reg (ptr_mode, mem);
1282 mem = adjust_address (fc, unwind_word_mode,
1283 sjlj_fc_data_ofs + GET_MODE_SIZE (unwind_word_mode));
1284 if (unwind_word_mode != filter_mode)
1285 mem = convert_to_mode (filter_mode, mem, 0);
1286 filter_reg = force_reg (filter_mode, mem);
1288 /* Jump to one of the directly reachable regions. */
1290 disp_index = 0;
1291 first_reachable_label = NULL;
1293 /* If there's exactly one call site in the function, don't bother
1294 generating a switch statement. */
1295 if (num_dispatch > 1)
1296 dispatch_labels = VEC_alloc (tree, heap, num_dispatch);
1298 for (i = 1; VEC_iterate (eh_landing_pad, cfun->eh->lp_array, i, lp); ++i)
1299 if (lp && lp->post_landing_pad)
1301 rtx seq2, label;
1303 start_sequence ();
1305 lp->landing_pad = dispatch_label;
1307 if (num_dispatch > 1)
1309 tree t_label, case_elt, t;
1311 t_label = create_artificial_label (UNKNOWN_LOCATION);
1312 t = build_int_cst (integer_type_node, disp_index);
1313 case_elt = build_case_label (t, NULL, t_label);
1314 VEC_quick_push (tree, dispatch_labels, case_elt);
1315 label = label_rtx (t_label);
1317 else
1318 label = gen_label_rtx ();
1320 if (disp_index == 0)
1321 first_reachable_label = label;
1322 emit_label (label);
1324 r = lp->region;
1325 if (r->exc_ptr_reg)
1326 emit_move_insn (r->exc_ptr_reg, exc_ptr_reg);
1327 if (r->filter_reg)
1328 emit_move_insn (r->filter_reg, filter_reg);
1330 seq2 = get_insns ();
1331 end_sequence ();
1333 before = label_rtx (lp->post_landing_pad);
1334 bb = emit_to_new_bb_before (seq2, before);
1335 e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
1336 e->count = bb->count;
1337 e->probability = REG_BR_PROB_BASE;
1338 if (current_loops)
1340 struct loop *loop = bb->next_bb->loop_father;
1341 /* If we created a pre-header block, add the new block to the
1342 outer loop, otherwise to the loop itself. */
1343 if (bb->next_bb == loop->header)
1344 add_bb_to_loop (bb, loop_outer (loop));
1345 else
1346 add_bb_to_loop (bb, loop);
1347 /* ??? For multiple dispatches we will end up with edges
1348 from the loop tree root into this loop, making it a
1349 multiple-entry loop. Discard all affected loops. */
1350 if (num_dispatch > 1)
1352 for (loop = bb->loop_father;
1353 loop_outer (loop); loop = loop_outer (loop))
1355 loop->header = NULL;
1356 loop->latch = NULL;
1361 disp_index++;
1363 gcc_assert (disp_index == num_dispatch);
1365 if (num_dispatch > 1)
1367 rtx disp = adjust_address (fc, TYPE_MODE (integer_type_node),
1368 sjlj_fc_call_site_ofs);
1369 expand_sjlj_dispatch_table (disp, dispatch_labels);
1372 seq = get_insns ();
1373 end_sequence ();
1375 bb = emit_to_new_bb_before (seq, first_reachable_label);
1376 if (num_dispatch == 1)
1378 e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
1379 e->count = bb->count;
1380 e->probability = REG_BR_PROB_BASE;
1381 if (current_loops)
1383 struct loop *loop = bb->next_bb->loop_father;
1384 /* If we created a pre-header block, add the new block to the
1385 outer loop, otherwise to the loop itself. */
1386 if (bb->next_bb == loop->header)
1387 add_bb_to_loop (bb, loop_outer (loop));
1388 else
1389 add_bb_to_loop (bb, loop);
1392 else
1394 /* We are not wiring up edges here, but as the dispatcher call
1395 is at function begin simply associate the block with the
1396 outermost (non-)loop. */
1397 if (current_loops)
1398 add_bb_to_loop (bb, current_loops->tree_root);
1402 static void
1403 sjlj_build_landing_pads (void)
1405 int num_dispatch;
1407 num_dispatch = VEC_length (eh_landing_pad, cfun->eh->lp_array);
1408 if (num_dispatch == 0)
1409 return;
1410 VEC_safe_grow (int, heap, sjlj_lp_call_site_index, num_dispatch);
1412 num_dispatch = sjlj_assign_call_site_values ();
1413 if (num_dispatch > 0)
1415 rtx dispatch_label = gen_label_rtx ();
1416 int align = STACK_SLOT_ALIGNMENT (sjlj_fc_type_node,
1417 TYPE_MODE (sjlj_fc_type_node),
1418 TYPE_ALIGN (sjlj_fc_type_node));
1419 crtl->eh.sjlj_fc
1420 = assign_stack_local (TYPE_MODE (sjlj_fc_type_node),
1421 int_size_in_bytes (sjlj_fc_type_node),
1422 align);
1424 sjlj_mark_call_sites ();
1425 sjlj_emit_function_enter (dispatch_label);
1426 sjlj_emit_dispatch_table (dispatch_label, num_dispatch);
1427 sjlj_emit_function_exit ();
1430 /* If we do not have any landing pads, we may still need to register a
1431 personality routine and (empty) LSDA to handle must-not-throw regions. */
1432 else if (function_needs_eh_personality (cfun) != eh_personality_none)
1434 int align = STACK_SLOT_ALIGNMENT (sjlj_fc_type_node,
1435 TYPE_MODE (sjlj_fc_type_node),
1436 TYPE_ALIGN (sjlj_fc_type_node));
1437 crtl->eh.sjlj_fc
1438 = assign_stack_local (TYPE_MODE (sjlj_fc_type_node),
1439 int_size_in_bytes (sjlj_fc_type_node),
1440 align);
1442 sjlj_mark_call_sites ();
1443 sjlj_emit_function_enter (NULL_RTX);
1444 sjlj_emit_function_exit ();
1447 VEC_free (int, heap, sjlj_lp_call_site_index);
1450 /* After initial rtl generation, call back to finish generating
1451 exception support code. */
1453 void
1454 finish_eh_generation (void)
1456 basic_block bb;
1458 /* Construct the landing pads. */
1459 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
1460 sjlj_build_landing_pads ();
1461 else
1462 dw2_build_landing_pads ();
1463 break_superblocks ();
1465 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ
1466 /* Kludge for Alpha (see alpha_gp_save_rtx). */
1467 || single_succ_edge (ENTRY_BLOCK_PTR)->insns.r)
1468 commit_edge_insertions ();
1470 /* Redirect all EH edges from the post_landing_pad to the landing pad. */
1471 FOR_EACH_BB (bb)
1473 eh_landing_pad lp;
1474 edge_iterator ei;
1475 edge e;
1477 lp = get_eh_landing_pad_from_rtx (BB_END (bb));
1479 FOR_EACH_EDGE (e, ei, bb->succs)
1480 if (e->flags & EDGE_EH)
1481 break;
1483 /* We should not have generated any new throwing insns during this
1484 pass, and we should not have lost any EH edges, so we only need
1485 to handle two cases here:
1486 (1) reachable handler and an existing edge to post-landing-pad,
1487 (2) no reachable handler and no edge. */
1488 gcc_assert ((lp != NULL) == (e != NULL));
1489 if (lp != NULL)
1491 gcc_assert (BB_HEAD (e->dest) == label_rtx (lp->post_landing_pad));
1493 redirect_edge_succ (e, BLOCK_FOR_INSN (lp->landing_pad));
1494 e->flags |= (CALL_P (BB_END (bb))
1495 ? EDGE_ABNORMAL | EDGE_ABNORMAL_CALL
1496 : EDGE_ABNORMAL);
1501 /* This section handles removing dead code for flow. */
1503 void
1504 remove_eh_landing_pad (eh_landing_pad lp)
1506 eh_landing_pad *pp;
1508 for (pp = &lp->region->landing_pads; *pp != lp; pp = &(*pp)->next_lp)
1509 continue;
1510 *pp = lp->next_lp;
1512 if (lp->post_landing_pad)
1513 EH_LANDING_PAD_NR (lp->post_landing_pad) = 0;
1514 VEC_replace (eh_landing_pad, cfun->eh->lp_array, lp->index, NULL);
1517 /* Splice REGION from the region tree. */
1519 void
1520 remove_eh_handler (eh_region region)
1522 eh_region *pp, *pp_start, p, outer;
1523 eh_landing_pad lp;
1525 for (lp = region->landing_pads; lp ; lp = lp->next_lp)
1527 if (lp->post_landing_pad)
1528 EH_LANDING_PAD_NR (lp->post_landing_pad) = 0;
1529 VEC_replace (eh_landing_pad, cfun->eh->lp_array, lp->index, NULL);
1532 outer = region->outer;
1533 if (outer)
1534 pp_start = &outer->inner;
1535 else
1536 pp_start = &cfun->eh->region_tree;
1537 for (pp = pp_start, p = *pp; p != region; pp = &p->next_peer, p = *pp)
1538 continue;
1539 if (region->inner)
1541 *pp = p = region->inner;
1544 p->outer = outer;
1545 pp = &p->next_peer;
1546 p = *pp;
1548 while (p);
1550 *pp = region->next_peer;
1552 VEC_replace (eh_region, cfun->eh->region_array, region->index, NULL);
1555 /* Invokes CALLBACK for every exception handler landing pad label.
1556 Only used by reload hackery; should not be used by new code. */
1558 void
1559 for_each_eh_label (void (*callback) (rtx))
1561 eh_landing_pad lp;
1562 int i;
1564 for (i = 1; VEC_iterate (eh_landing_pad, cfun->eh->lp_array, i, lp); ++i)
1566 if (lp)
1568 rtx lab = lp->landing_pad;
1569 if (lab && LABEL_P (lab))
1570 (*callback) (lab);
1575 /* Create the REG_EH_REGION note for INSN, given its ECF_FLAGS for a
1576 call insn.
1578 At the gimple level, we use LP_NR
1579 > 0 : The statement transfers to landing pad LP_NR
1580 = 0 : The statement is outside any EH region
1581 < 0 : The statement is within MUST_NOT_THROW region -LP_NR.
1583 At the rtl level, we use LP_NR
1584 > 0 : The insn transfers to landing pad LP_NR
1585 = 0 : The insn cannot throw
1586 < 0 : The insn is within MUST_NOT_THROW region -LP_NR
1587 = INT_MIN : The insn cannot throw or execute a nonlocal-goto.
1588 missing note: The insn is outside any EH region.
1590 ??? This difference probably ought to be avoided. We could stand
1591 to record nothrow for arbitrary gimple statements, and so avoid
1592 some moderately complex lookups in stmt_could_throw_p. Perhaps
1593 NOTHROW should be mapped on both sides to INT_MIN. Perhaps the
1594 no-nonlocal-goto property should be recorded elsewhere as a bit
1595 on the call_insn directly. Perhaps we should make more use of
1596 attaching the trees to call_insns (reachable via symbol_ref in
1597 direct call cases) and just pull the data out of the trees. */
1599 void
1600 make_reg_eh_region_note (rtx insn, int ecf_flags, int lp_nr)
1602 rtx value;
1603 if (ecf_flags & ECF_NOTHROW)
1604 value = const0_rtx;
1605 else if (lp_nr != 0)
1606 value = GEN_INT (lp_nr);
1607 else
1608 return;
1609 add_reg_note (insn, REG_EH_REGION, value);
1612 /* Create a REG_EH_REGION note for a CALL_INSN that cannot throw
1613 nor perform a non-local goto. Replace the region note if it
1614 already exists. */
1616 void
1617 make_reg_eh_region_note_nothrow_nononlocal (rtx insn)
1619 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1620 rtx intmin = GEN_INT (INT_MIN);
1622 if (note != 0)
1623 XEXP (note, 0) = intmin;
1624 else
1625 add_reg_note (insn, REG_EH_REGION, intmin);
1628 /* Return true if INSN could throw, assuming no REG_EH_REGION note
1629 to the contrary. */
1631 bool
1632 insn_could_throw_p (const_rtx insn)
1634 if (!flag_exceptions)
1635 return false;
1636 if (CALL_P (insn))
1637 return true;
1638 if (INSN_P (insn) && cfun->can_throw_non_call_exceptions)
1639 return may_trap_p (PATTERN (insn));
1640 return false;
1643 /* Copy an REG_EH_REGION note to each insn that might throw beginning
1644 at FIRST and ending at LAST. NOTE_OR_INSN is either the source insn
1645 to look for a note, or the note itself. */
1647 void
1648 copy_reg_eh_region_note_forward (rtx note_or_insn, rtx first, rtx last)
1650 rtx insn, note = note_or_insn;
1652 if (INSN_P (note_or_insn))
1654 note = find_reg_note (note_or_insn, REG_EH_REGION, NULL_RTX);
1655 if (note == NULL)
1656 return;
1658 note = XEXP (note, 0);
1660 for (insn = first; insn != last ; insn = NEXT_INSN (insn))
1661 if (!find_reg_note (insn, REG_EH_REGION, NULL_RTX)
1662 && insn_could_throw_p (insn))
1663 add_reg_note (insn, REG_EH_REGION, note);
1666 /* Likewise, but iterate backward. */
1668 void
1669 copy_reg_eh_region_note_backward (rtx note_or_insn, rtx last, rtx first)
1671 rtx insn, note = note_or_insn;
1673 if (INSN_P (note_or_insn))
1675 note = find_reg_note (note_or_insn, REG_EH_REGION, NULL_RTX);
1676 if (note == NULL)
1677 return;
1679 note = XEXP (note, 0);
1681 for (insn = last; insn != first; insn = PREV_INSN (insn))
1682 if (insn_could_throw_p (insn))
1683 add_reg_note (insn, REG_EH_REGION, note);
1687 /* Extract all EH information from INSN. Return true if the insn
1688 was marked NOTHROW. */
1690 static bool
1691 get_eh_region_and_lp_from_rtx (const_rtx insn, eh_region *pr,
1692 eh_landing_pad *plp)
1694 eh_landing_pad lp = NULL;
1695 eh_region r = NULL;
1696 bool ret = false;
1697 rtx note;
1698 int lp_nr;
1700 if (! INSN_P (insn))
1701 goto egress;
1703 if (NONJUMP_INSN_P (insn)
1704 && GET_CODE (PATTERN (insn)) == SEQUENCE)
1705 insn = XVECEXP (PATTERN (insn), 0, 0);
1707 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1708 if (!note)
1710 ret = !insn_could_throw_p (insn);
1711 goto egress;
1714 lp_nr = INTVAL (XEXP (note, 0));
1715 if (lp_nr == 0 || lp_nr == INT_MIN)
1717 ret = true;
1718 goto egress;
1721 if (lp_nr < 0)
1722 r = VEC_index (eh_region, cfun->eh->region_array, -lp_nr);
1723 else
1725 lp = VEC_index (eh_landing_pad, cfun->eh->lp_array, lp_nr);
1726 r = lp->region;
1729 egress:
1730 *plp = lp;
1731 *pr = r;
1732 return ret;
1735 /* Return the landing pad to which INSN may go, or NULL if it does not
1736 have a reachable landing pad within this function. */
1738 eh_landing_pad
1739 get_eh_landing_pad_from_rtx (const_rtx insn)
1741 eh_landing_pad lp;
1742 eh_region r;
1744 get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1745 return lp;
1748 /* Return the region to which INSN may go, or NULL if it does not
1749 have a reachable region within this function. */
1751 eh_region
1752 get_eh_region_from_rtx (const_rtx insn)
1754 eh_landing_pad lp;
1755 eh_region r;
1757 get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1758 return r;
1761 /* Return true if INSN throws and is caught by something in this function. */
1763 bool
1764 can_throw_internal (const_rtx insn)
1766 return get_eh_landing_pad_from_rtx (insn) != NULL;
1769 /* Return true if INSN throws and escapes from the current function. */
1771 bool
1772 can_throw_external (const_rtx insn)
1774 eh_landing_pad lp;
1775 eh_region r;
1776 bool nothrow;
1778 if (! INSN_P (insn))
1779 return false;
1781 if (NONJUMP_INSN_P (insn)
1782 && GET_CODE (PATTERN (insn)) == SEQUENCE)
1784 rtx seq = PATTERN (insn);
1785 int i, n = XVECLEN (seq, 0);
1787 for (i = 0; i < n; i++)
1788 if (can_throw_external (XVECEXP (seq, 0, i)))
1789 return true;
1791 return false;
1794 nothrow = get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1796 /* If we can't throw, we obviously can't throw external. */
1797 if (nothrow)
1798 return false;
1800 /* If we have an internal landing pad, then we're not external. */
1801 if (lp != NULL)
1802 return false;
1804 /* If we're not within an EH region, then we are external. */
1805 if (r == NULL)
1806 return true;
1808 /* The only thing that ought to be left is MUST_NOT_THROW regions,
1809 which don't always have landing pads. */
1810 gcc_assert (r->type == ERT_MUST_NOT_THROW);
1811 return false;
1814 /* Return true if INSN cannot throw at all. */
1816 bool
1817 insn_nothrow_p (const_rtx insn)
1819 eh_landing_pad lp;
1820 eh_region r;
1822 if (! INSN_P (insn))
1823 return true;
1825 if (NONJUMP_INSN_P (insn)
1826 && GET_CODE (PATTERN (insn)) == SEQUENCE)
1828 rtx seq = PATTERN (insn);
1829 int i, n = XVECLEN (seq, 0);
1831 for (i = 0; i < n; i++)
1832 if (!insn_nothrow_p (XVECEXP (seq, 0, i)))
1833 return false;
1835 return true;
1838 return get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1841 /* Return true if INSN can perform a non-local goto. */
1842 /* ??? This test is here in this file because it (ab)uses REG_EH_REGION. */
1844 bool
1845 can_nonlocal_goto (const_rtx insn)
1847 if (nonlocal_goto_handler_labels && CALL_P (insn))
1849 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1850 if (!note || INTVAL (XEXP (note, 0)) != INT_MIN)
1851 return true;
1853 return false;
1856 /* Set TREE_NOTHROW and crtl->all_throwers_are_sibcalls. */
1858 static unsigned int
1859 set_nothrow_function_flags (void)
1861 rtx insn;
1863 crtl->nothrow = 1;
1865 /* Assume crtl->all_throwers_are_sibcalls until we encounter
1866 something that can throw an exception. We specifically exempt
1867 CALL_INSNs that are SIBLING_CALL_P, as these are really jumps,
1868 and can't throw. Most CALL_INSNs are not SIBLING_CALL_P, so this
1869 is optimistic. */
1871 crtl->all_throwers_are_sibcalls = 1;
1873 /* If we don't know that this implementation of the function will
1874 actually be used, then we must not set TREE_NOTHROW, since
1875 callers must not assume that this function does not throw. */
1876 if (TREE_NOTHROW (current_function_decl))
1877 return 0;
1879 if (! flag_exceptions)
1880 return 0;
1882 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1883 if (can_throw_external (insn))
1885 crtl->nothrow = 0;
1887 if (!CALL_P (insn) || !SIBLING_CALL_P (insn))
1889 crtl->all_throwers_are_sibcalls = 0;
1890 return 0;
1894 for (insn = crtl->epilogue_delay_list; insn;
1895 insn = XEXP (insn, 1))
1896 if (can_throw_external (insn))
1898 crtl->nothrow = 0;
1900 if (!CALL_P (insn) || !SIBLING_CALL_P (insn))
1902 crtl->all_throwers_are_sibcalls = 0;
1903 return 0;
1906 if (crtl->nothrow
1907 && (cgraph_function_body_availability (cgraph_get_node
1908 (current_function_decl))
1909 >= AVAIL_AVAILABLE))
1911 struct cgraph_node *node = cgraph_get_node (current_function_decl);
1912 struct cgraph_edge *e;
1913 for (e = node->callers; e; e = e->next_caller)
1914 e->can_throw_external = false;
1915 cgraph_set_nothrow_flag (node, true);
1917 if (dump_file)
1918 fprintf (dump_file, "Marking function nothrow: %s\n\n",
1919 current_function_name ());
1921 return 0;
1924 struct rtl_opt_pass pass_set_nothrow_function_flags =
1927 RTL_PASS,
1928 "nothrow", /* name */
1929 NULL, /* gate */
1930 set_nothrow_function_flags, /* execute */
1931 NULL, /* sub */
1932 NULL, /* next */
1933 0, /* static_pass_number */
1934 TV_NONE, /* tv_id */
1935 0, /* properties_required */
1936 0, /* properties_provided */
1937 0, /* properties_destroyed */
1938 0, /* todo_flags_start */
1939 0 /* todo_flags_finish */
1944 /* Various hooks for unwind library. */
1946 /* Expand the EH support builtin functions:
1947 __builtin_eh_pointer and __builtin_eh_filter. */
1949 static eh_region
1950 expand_builtin_eh_common (tree region_nr_t)
1952 HOST_WIDE_INT region_nr;
1953 eh_region region;
1955 gcc_assert (host_integerp (region_nr_t, 0));
1956 region_nr = tree_low_cst (region_nr_t, 0);
1958 region = VEC_index (eh_region, cfun->eh->region_array, region_nr);
1960 /* ??? We shouldn't have been able to delete a eh region without
1961 deleting all the code that depended on it. */
1962 gcc_assert (region != NULL);
1964 return region;
1967 /* Expand to the exc_ptr value from the given eh region. */
1970 expand_builtin_eh_pointer (tree exp)
1972 eh_region region
1973 = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 0));
1974 if (region->exc_ptr_reg == NULL)
1975 region->exc_ptr_reg = gen_reg_rtx (ptr_mode);
1976 return region->exc_ptr_reg;
1979 /* Expand to the filter value from the given eh region. */
1982 expand_builtin_eh_filter (tree exp)
1984 eh_region region
1985 = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 0));
1986 if (region->filter_reg == NULL)
1987 region->filter_reg = gen_reg_rtx (targetm.eh_return_filter_mode ());
1988 return region->filter_reg;
1991 /* Copy the exc_ptr and filter values from one landing pad's registers
1992 to another. This is used to inline the resx statement. */
1995 expand_builtin_eh_copy_values (tree exp)
1997 eh_region dst
1998 = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 0));
1999 eh_region src
2000 = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 1));
2001 enum machine_mode fmode = targetm.eh_return_filter_mode ();
2003 if (dst->exc_ptr_reg == NULL)
2004 dst->exc_ptr_reg = gen_reg_rtx (ptr_mode);
2005 if (src->exc_ptr_reg == NULL)
2006 src->exc_ptr_reg = gen_reg_rtx (ptr_mode);
2008 if (dst->filter_reg == NULL)
2009 dst->filter_reg = gen_reg_rtx (fmode);
2010 if (src->filter_reg == NULL)
2011 src->filter_reg = gen_reg_rtx (fmode);
2013 emit_move_insn (dst->exc_ptr_reg, src->exc_ptr_reg);
2014 emit_move_insn (dst->filter_reg, src->filter_reg);
2016 return const0_rtx;
2019 /* Do any necessary initialization to access arbitrary stack frames.
2020 On the SPARC, this means flushing the register windows. */
2022 void
2023 expand_builtin_unwind_init (void)
2025 /* Set this so all the registers get saved in our frame; we need to be
2026 able to copy the saved values for any registers from frames we unwind. */
2027 crtl->saves_all_registers = 1;
2029 #ifdef SETUP_FRAME_ADDRESSES
2030 SETUP_FRAME_ADDRESSES ();
2031 #endif
2034 /* Map a non-negative number to an eh return data register number; expands
2035 to -1 if no return data register is associated with the input number.
2036 At least the inputs 0 and 1 must be mapped; the target may provide more. */
2039 expand_builtin_eh_return_data_regno (tree exp)
2041 tree which = CALL_EXPR_ARG (exp, 0);
2042 unsigned HOST_WIDE_INT iwhich;
2044 if (TREE_CODE (which) != INTEGER_CST)
2046 error ("argument of %<__builtin_eh_return_regno%> must be constant");
2047 return constm1_rtx;
2050 iwhich = tree_low_cst (which, 1);
2051 iwhich = EH_RETURN_DATA_REGNO (iwhich);
2052 if (iwhich == INVALID_REGNUM)
2053 return constm1_rtx;
2055 #ifdef DWARF_FRAME_REGNUM
2056 iwhich = DWARF_FRAME_REGNUM (iwhich);
2057 #else
2058 iwhich = DBX_REGISTER_NUMBER (iwhich);
2059 #endif
2061 return GEN_INT (iwhich);
2064 /* Given a value extracted from the return address register or stack slot,
2065 return the actual address encoded in that value. */
2068 expand_builtin_extract_return_addr (tree addr_tree)
2070 rtx addr = expand_expr (addr_tree, NULL_RTX, Pmode, EXPAND_NORMAL);
2072 if (GET_MODE (addr) != Pmode
2073 && GET_MODE (addr) != VOIDmode)
2075 #ifdef POINTERS_EXTEND_UNSIGNED
2076 addr = convert_memory_address (Pmode, addr);
2077 #else
2078 addr = convert_to_mode (Pmode, addr, 0);
2079 #endif
2082 /* First mask out any unwanted bits. */
2083 #ifdef MASK_RETURN_ADDR
2084 expand_and (Pmode, addr, MASK_RETURN_ADDR, addr);
2085 #endif
2087 /* Then adjust to find the real return address. */
2088 #if defined (RETURN_ADDR_OFFSET)
2089 addr = plus_constant (Pmode, addr, RETURN_ADDR_OFFSET);
2090 #endif
2092 return addr;
2095 /* Given an actual address in addr_tree, do any necessary encoding
2096 and return the value to be stored in the return address register or
2097 stack slot so the epilogue will return to that address. */
2100 expand_builtin_frob_return_addr (tree addr_tree)
2102 rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, EXPAND_NORMAL);
2104 addr = convert_memory_address (Pmode, addr);
2106 #ifdef RETURN_ADDR_OFFSET
2107 addr = force_reg (Pmode, addr);
2108 addr = plus_constant (Pmode, addr, -RETURN_ADDR_OFFSET);
2109 #endif
2111 return addr;
2114 /* Set up the epilogue with the magic bits we'll need to return to the
2115 exception handler. */
2117 void
2118 expand_builtin_eh_return (tree stackadj_tree ATTRIBUTE_UNUSED,
2119 tree handler_tree)
2121 rtx tmp;
2123 #ifdef EH_RETURN_STACKADJ_RTX
2124 tmp = expand_expr (stackadj_tree, crtl->eh.ehr_stackadj,
2125 VOIDmode, EXPAND_NORMAL);
2126 tmp = convert_memory_address (Pmode, tmp);
2127 if (!crtl->eh.ehr_stackadj)
2128 crtl->eh.ehr_stackadj = copy_to_reg (tmp);
2129 else if (tmp != crtl->eh.ehr_stackadj)
2130 emit_move_insn (crtl->eh.ehr_stackadj, tmp);
2131 #endif
2133 tmp = expand_expr (handler_tree, crtl->eh.ehr_handler,
2134 VOIDmode, EXPAND_NORMAL);
2135 tmp = convert_memory_address (Pmode, tmp);
2136 if (!crtl->eh.ehr_handler)
2137 crtl->eh.ehr_handler = copy_to_reg (tmp);
2138 else if (tmp != crtl->eh.ehr_handler)
2139 emit_move_insn (crtl->eh.ehr_handler, tmp);
2141 if (!crtl->eh.ehr_label)
2142 crtl->eh.ehr_label = gen_label_rtx ();
2143 emit_jump (crtl->eh.ehr_label);
2146 /* Expand __builtin_eh_return. This exit path from the function loads up
2147 the eh return data registers, adjusts the stack, and branches to a
2148 given PC other than the normal return address. */
2150 void
2151 expand_eh_return (void)
2153 rtx around_label;
2155 if (! crtl->eh.ehr_label)
2156 return;
2158 crtl->calls_eh_return = 1;
2160 #ifdef EH_RETURN_STACKADJ_RTX
2161 emit_move_insn (EH_RETURN_STACKADJ_RTX, const0_rtx);
2162 #endif
2164 around_label = gen_label_rtx ();
2165 emit_jump (around_label);
2167 emit_label (crtl->eh.ehr_label);
2168 clobber_return_register ();
2170 #ifdef EH_RETURN_STACKADJ_RTX
2171 emit_move_insn (EH_RETURN_STACKADJ_RTX, crtl->eh.ehr_stackadj);
2172 #endif
2174 #ifdef HAVE_eh_return
2175 if (HAVE_eh_return)
2176 emit_insn (gen_eh_return (crtl->eh.ehr_handler));
2177 else
2178 #endif
2180 #ifdef EH_RETURN_HANDLER_RTX
2181 emit_move_insn (EH_RETURN_HANDLER_RTX, crtl->eh.ehr_handler);
2182 #else
2183 error ("__builtin_eh_return not supported on this target");
2184 #endif
2187 emit_label (around_label);
2190 /* Convert a ptr_mode address ADDR_TREE to a Pmode address controlled by
2191 POINTERS_EXTEND_UNSIGNED and return it. */
2194 expand_builtin_extend_pointer (tree addr_tree)
2196 rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, EXPAND_NORMAL);
2197 int extend;
2199 #ifdef POINTERS_EXTEND_UNSIGNED
2200 extend = POINTERS_EXTEND_UNSIGNED;
2201 #else
2202 /* The previous EH code did an unsigned extend by default, so we do this also
2203 for consistency. */
2204 extend = 1;
2205 #endif
2207 return convert_modes (targetm.unwind_word_mode (), ptr_mode, addr, extend);
2210 /* In the following functions, we represent entries in the action table
2211 as 1-based indices. Special cases are:
2213 0: null action record, non-null landing pad; implies cleanups
2214 -1: null action record, null landing pad; implies no action
2215 -2: no call-site entry; implies must_not_throw
2216 -3: we have yet to process outer regions
2218 Further, no special cases apply to the "next" field of the record.
2219 For next, 0 means end of list. */
2221 struct action_record
2223 int offset;
2224 int filter;
2225 int next;
2228 static int
2229 action_record_eq (const void *pentry, const void *pdata)
2231 const struct action_record *entry = (const struct action_record *) pentry;
2232 const struct action_record *data = (const struct action_record *) pdata;
2233 return entry->filter == data->filter && entry->next == data->next;
2236 static hashval_t
2237 action_record_hash (const void *pentry)
2239 const struct action_record *entry = (const struct action_record *) pentry;
2240 return entry->next * 1009 + entry->filter;
2243 static int
2244 add_action_record (htab_t ar_hash, int filter, int next)
2246 struct action_record **slot, *new_ar, tmp;
2248 tmp.filter = filter;
2249 tmp.next = next;
2250 slot = (struct action_record **) htab_find_slot (ar_hash, &tmp, INSERT);
2252 if ((new_ar = *slot) == NULL)
2254 new_ar = XNEW (struct action_record);
2255 new_ar->offset = VEC_length (uchar, crtl->eh.action_record_data) + 1;
2256 new_ar->filter = filter;
2257 new_ar->next = next;
2258 *slot = new_ar;
2260 /* The filter value goes in untouched. The link to the next
2261 record is a "self-relative" byte offset, or zero to indicate
2262 that there is no next record. So convert the absolute 1 based
2263 indices we've been carrying around into a displacement. */
2265 push_sleb128 (&crtl->eh.action_record_data, filter);
2266 if (next)
2267 next -= VEC_length (uchar, crtl->eh.action_record_data) + 1;
2268 push_sleb128 (&crtl->eh.action_record_data, next);
2271 return new_ar->offset;
2274 static int
2275 collect_one_action_chain (htab_t ar_hash, eh_region region)
2277 int next;
2279 /* If we've reached the top of the region chain, then we have
2280 no actions, and require no landing pad. */
2281 if (region == NULL)
2282 return -1;
2284 switch (region->type)
2286 case ERT_CLEANUP:
2288 eh_region r;
2289 /* A cleanup adds a zero filter to the beginning of the chain, but
2290 there are special cases to look out for. If there are *only*
2291 cleanups along a path, then it compresses to a zero action.
2292 Further, if there are multiple cleanups along a path, we only
2293 need to represent one of them, as that is enough to trigger
2294 entry to the landing pad at runtime. */
2295 next = collect_one_action_chain (ar_hash, region->outer);
2296 if (next <= 0)
2297 return 0;
2298 for (r = region->outer; r ; r = r->outer)
2299 if (r->type == ERT_CLEANUP)
2300 return next;
2301 return add_action_record (ar_hash, 0, next);
2304 case ERT_TRY:
2306 eh_catch c;
2308 /* Process the associated catch regions in reverse order.
2309 If there's a catch-all handler, then we don't need to
2310 search outer regions. Use a magic -3 value to record
2311 that we haven't done the outer search. */
2312 next = -3;
2313 for (c = region->u.eh_try.last_catch; c ; c = c->prev_catch)
2315 if (c->type_list == NULL)
2317 /* Retrieve the filter from the head of the filter list
2318 where we have stored it (see assign_filter_values). */
2319 int filter = TREE_INT_CST_LOW (TREE_VALUE (c->filter_list));
2320 next = add_action_record (ar_hash, filter, 0);
2322 else
2324 /* Once the outer search is done, trigger an action record for
2325 each filter we have. */
2326 tree flt_node;
2328 if (next == -3)
2330 next = collect_one_action_chain (ar_hash, region->outer);
2332 /* If there is no next action, terminate the chain. */
2333 if (next == -1)
2334 next = 0;
2335 /* If all outer actions are cleanups or must_not_throw,
2336 we'll have no action record for it, since we had wanted
2337 to encode these states in the call-site record directly.
2338 Add a cleanup action to the chain to catch these. */
2339 else if (next <= 0)
2340 next = add_action_record (ar_hash, 0, 0);
2343 flt_node = c->filter_list;
2344 for (; flt_node; flt_node = TREE_CHAIN (flt_node))
2346 int filter = TREE_INT_CST_LOW (TREE_VALUE (flt_node));
2347 next = add_action_record (ar_hash, filter, next);
2351 return next;
2354 case ERT_ALLOWED_EXCEPTIONS:
2355 /* An exception specification adds its filter to the
2356 beginning of the chain. */
2357 next = collect_one_action_chain (ar_hash, region->outer);
2359 /* If there is no next action, terminate the chain. */
2360 if (next == -1)
2361 next = 0;
2362 /* If all outer actions are cleanups or must_not_throw,
2363 we'll have no action record for it, since we had wanted
2364 to encode these states in the call-site record directly.
2365 Add a cleanup action to the chain to catch these. */
2366 else if (next <= 0)
2367 next = add_action_record (ar_hash, 0, 0);
2369 return add_action_record (ar_hash, region->u.allowed.filter, next);
2371 case ERT_MUST_NOT_THROW:
2372 /* A must-not-throw region with no inner handlers or cleanups
2373 requires no call-site entry. Note that this differs from
2374 the no handler or cleanup case in that we do require an lsda
2375 to be generated. Return a magic -2 value to record this. */
2376 return -2;
2379 gcc_unreachable ();
2382 static int
2383 add_call_site (rtx landing_pad, int action, int section)
2385 call_site_record record;
2387 record = ggc_alloc_call_site_record_d ();
2388 record->landing_pad = landing_pad;
2389 record->action = action;
2391 VEC_safe_push (call_site_record, gc,
2392 crtl->eh.call_site_record_v[section], record);
2394 return call_site_base + VEC_length (call_site_record,
2395 crtl->eh.call_site_record_v[section]) - 1;
2398 /* Turn REG_EH_REGION notes back into NOTE_INSN_EH_REGION notes.
2399 The new note numbers will not refer to region numbers, but
2400 instead to call site entries. */
2402 static unsigned int
2403 convert_to_eh_region_ranges (void)
2405 rtx insn, iter, note;
2406 htab_t ar_hash;
2407 int last_action = -3;
2408 rtx last_action_insn = NULL_RTX;
2409 rtx last_landing_pad = NULL_RTX;
2410 rtx first_no_action_insn = NULL_RTX;
2411 int call_site = 0;
2412 int cur_sec = 0;
2413 rtx section_switch_note = NULL_RTX;
2414 rtx first_no_action_insn_before_switch = NULL_RTX;
2415 rtx last_no_action_insn_before_switch = NULL_RTX;
2416 int saved_call_site_base = call_site_base;
2418 crtl->eh.action_record_data = VEC_alloc (uchar, gc, 64);
2420 ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
2422 for (iter = get_insns (); iter ; iter = NEXT_INSN (iter))
2423 if (INSN_P (iter))
2425 eh_landing_pad lp;
2426 eh_region region;
2427 bool nothrow;
2428 int this_action;
2429 rtx this_landing_pad;
2431 insn = iter;
2432 if (NONJUMP_INSN_P (insn)
2433 && GET_CODE (PATTERN (insn)) == SEQUENCE)
2434 insn = XVECEXP (PATTERN (insn), 0, 0);
2436 nothrow = get_eh_region_and_lp_from_rtx (insn, &region, &lp);
2437 if (nothrow)
2438 continue;
2439 if (region)
2440 this_action = collect_one_action_chain (ar_hash, region);
2441 else
2442 this_action = -1;
2444 /* Existence of catch handlers, or must-not-throw regions
2445 implies that an lsda is needed (even if empty). */
2446 if (this_action != -1)
2447 crtl->uses_eh_lsda = 1;
2449 /* Delay creation of region notes for no-action regions
2450 until we're sure that an lsda will be required. */
2451 else if (last_action == -3)
2453 first_no_action_insn = iter;
2454 last_action = -1;
2457 if (this_action >= 0)
2458 this_landing_pad = lp->landing_pad;
2459 else
2460 this_landing_pad = NULL_RTX;
2462 /* Differing actions or landing pads implies a change in call-site
2463 info, which implies some EH_REGION note should be emitted. */
2464 if (last_action != this_action
2465 || last_landing_pad != this_landing_pad)
2467 /* If there is a queued no-action region in the other section
2468 with hot/cold partitioning, emit it now. */
2469 if (first_no_action_insn_before_switch)
2471 gcc_assert (this_action != -1
2472 && last_action == (first_no_action_insn
2473 ? -1 : -3));
2474 call_site = add_call_site (NULL_RTX, 0, 0);
2475 note = emit_note_before (NOTE_INSN_EH_REGION_BEG,
2476 first_no_action_insn_before_switch);
2477 NOTE_EH_HANDLER (note) = call_site;
2478 note = emit_note_after (NOTE_INSN_EH_REGION_END,
2479 last_no_action_insn_before_switch);
2480 NOTE_EH_HANDLER (note) = call_site;
2481 gcc_assert (last_action != -3
2482 || (last_action_insn
2483 == last_no_action_insn_before_switch));
2484 first_no_action_insn_before_switch = NULL_RTX;
2485 last_no_action_insn_before_switch = NULL_RTX;
2486 call_site_base++;
2488 /* If we'd not seen a previous action (-3) or the previous
2489 action was must-not-throw (-2), then we do not need an
2490 end note. */
2491 if (last_action >= -1)
2493 /* If we delayed the creation of the begin, do it now. */
2494 if (first_no_action_insn)
2496 call_site = add_call_site (NULL_RTX, 0, cur_sec);
2497 note = emit_note_before (NOTE_INSN_EH_REGION_BEG,
2498 first_no_action_insn);
2499 NOTE_EH_HANDLER (note) = call_site;
2500 first_no_action_insn = NULL_RTX;
2503 note = emit_note_after (NOTE_INSN_EH_REGION_END,
2504 last_action_insn);
2505 NOTE_EH_HANDLER (note) = call_site;
2508 /* If the new action is must-not-throw, then no region notes
2509 are created. */
2510 if (this_action >= -1)
2512 call_site = add_call_site (this_landing_pad,
2513 this_action < 0 ? 0 : this_action,
2514 cur_sec);
2515 note = emit_note_before (NOTE_INSN_EH_REGION_BEG, iter);
2516 NOTE_EH_HANDLER (note) = call_site;
2519 last_action = this_action;
2520 last_landing_pad = this_landing_pad;
2522 last_action_insn = iter;
2524 else if (NOTE_P (iter)
2525 && NOTE_KIND (iter) == NOTE_INSN_SWITCH_TEXT_SECTIONS)
2527 gcc_assert (section_switch_note == NULL_RTX);
2528 gcc_assert (flag_reorder_blocks_and_partition);
2529 section_switch_note = iter;
2530 if (first_no_action_insn)
2532 first_no_action_insn_before_switch = first_no_action_insn;
2533 last_no_action_insn_before_switch = last_action_insn;
2534 first_no_action_insn = NULL_RTX;
2535 gcc_assert (last_action == -1);
2536 last_action = -3;
2538 /* Force closing of current EH region before section switch and
2539 opening a new one afterwards. */
2540 else if (last_action != -3)
2541 last_landing_pad = pc_rtx;
2542 call_site_base += VEC_length (call_site_record,
2543 crtl->eh.call_site_record_v[cur_sec]);
2544 cur_sec++;
2545 gcc_assert (crtl->eh.call_site_record_v[cur_sec] == NULL);
2546 crtl->eh.call_site_record_v[cur_sec]
2547 = VEC_alloc (call_site_record, gc, 10);
2550 if (last_action >= -1 && ! first_no_action_insn)
2552 note = emit_note_after (NOTE_INSN_EH_REGION_END, last_action_insn);
2553 NOTE_EH_HANDLER (note) = call_site;
2556 call_site_base = saved_call_site_base;
2558 htab_delete (ar_hash);
2559 return 0;
2562 static bool
2563 gate_convert_to_eh_region_ranges (void)
2565 /* Nothing to do for SJLJ exceptions or if no regions created. */
2566 if (cfun->eh->region_tree == NULL)
2567 return false;
2568 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
2569 return false;
2570 return true;
2573 struct rtl_opt_pass pass_convert_to_eh_region_ranges =
2576 RTL_PASS,
2577 "eh_ranges", /* name */
2578 gate_convert_to_eh_region_ranges, /* gate */
2579 convert_to_eh_region_ranges, /* execute */
2580 NULL, /* sub */
2581 NULL, /* next */
2582 0, /* static_pass_number */
2583 TV_NONE, /* tv_id */
2584 0, /* properties_required */
2585 0, /* properties_provided */
2586 0, /* properties_destroyed */
2587 0, /* todo_flags_start */
2588 0 /* todo_flags_finish */
2592 static void
2593 push_uleb128 (VEC (uchar, gc) **data_area, unsigned int value)
2597 unsigned char byte = value & 0x7f;
2598 value >>= 7;
2599 if (value)
2600 byte |= 0x80;
2601 VEC_safe_push (uchar, gc, *data_area, byte);
2603 while (value);
2606 static void
2607 push_sleb128 (VEC (uchar, gc) **data_area, int value)
2609 unsigned char byte;
2610 int more;
2614 byte = value & 0x7f;
2615 value >>= 7;
2616 more = ! ((value == 0 && (byte & 0x40) == 0)
2617 || (value == -1 && (byte & 0x40) != 0));
2618 if (more)
2619 byte |= 0x80;
2620 VEC_safe_push (uchar, gc, *data_area, byte);
2622 while (more);
2626 #ifndef HAVE_AS_LEB128
2627 static int
2628 dw2_size_of_call_site_table (int section)
2630 int n = VEC_length (call_site_record, crtl->eh.call_site_record_v[section]);
2631 int size = n * (4 + 4 + 4);
2632 int i;
2634 for (i = 0; i < n; ++i)
2636 struct call_site_record_d *cs =
2637 VEC_index (call_site_record, crtl->eh.call_site_record_v[section], i);
2638 size += size_of_uleb128 (cs->action);
2641 return size;
2644 static int
2645 sjlj_size_of_call_site_table (void)
2647 int n = VEC_length (call_site_record, crtl->eh.call_site_record_v[0]);
2648 int size = 0;
2649 int i;
2651 for (i = 0; i < n; ++i)
2653 struct call_site_record_d *cs =
2654 VEC_index (call_site_record, crtl->eh.call_site_record_v[0], i);
2655 size += size_of_uleb128 (INTVAL (cs->landing_pad));
2656 size += size_of_uleb128 (cs->action);
2659 return size;
2661 #endif
2663 static void
2664 dw2_output_call_site_table (int cs_format, int section)
2666 int n = VEC_length (call_site_record, crtl->eh.call_site_record_v[section]);
2667 int i;
2668 const char *begin;
2670 if (section == 0)
2671 begin = current_function_func_begin_label;
2672 else if (first_function_block_is_cold)
2673 begin = crtl->subsections.hot_section_label;
2674 else
2675 begin = crtl->subsections.cold_section_label;
2677 for (i = 0; i < n; ++i)
2679 struct call_site_record_d *cs =
2680 VEC_index (call_site_record, crtl->eh.call_site_record_v[section], i);
2681 char reg_start_lab[32];
2682 char reg_end_lab[32];
2683 char landing_pad_lab[32];
2685 ASM_GENERATE_INTERNAL_LABEL (reg_start_lab, "LEHB", call_site_base + i);
2686 ASM_GENERATE_INTERNAL_LABEL (reg_end_lab, "LEHE", call_site_base + i);
2688 if (cs->landing_pad)
2689 ASM_GENERATE_INTERNAL_LABEL (landing_pad_lab, "L",
2690 CODE_LABEL_NUMBER (cs->landing_pad));
2692 /* ??? Perhaps use insn length scaling if the assembler supports
2693 generic arithmetic. */
2694 /* ??? Perhaps use attr_length to choose data1 or data2 instead of
2695 data4 if the function is small enough. */
2696 if (cs_format == DW_EH_PE_uleb128)
2698 dw2_asm_output_delta_uleb128 (reg_start_lab, begin,
2699 "region %d start", i);
2700 dw2_asm_output_delta_uleb128 (reg_end_lab, reg_start_lab,
2701 "length");
2702 if (cs->landing_pad)
2703 dw2_asm_output_delta_uleb128 (landing_pad_lab, begin,
2704 "landing pad");
2705 else
2706 dw2_asm_output_data_uleb128 (0, "landing pad");
2708 else
2710 dw2_asm_output_delta (4, reg_start_lab, begin,
2711 "region %d start", i);
2712 dw2_asm_output_delta (4, reg_end_lab, reg_start_lab, "length");
2713 if (cs->landing_pad)
2714 dw2_asm_output_delta (4, landing_pad_lab, begin,
2715 "landing pad");
2716 else
2717 dw2_asm_output_data (4, 0, "landing pad");
2719 dw2_asm_output_data_uleb128 (cs->action, "action");
2722 call_site_base += n;
2725 static void
2726 sjlj_output_call_site_table (void)
2728 int n = VEC_length (call_site_record, crtl->eh.call_site_record_v[0]);
2729 int i;
2731 for (i = 0; i < n; ++i)
2733 struct call_site_record_d *cs =
2734 VEC_index (call_site_record, crtl->eh.call_site_record_v[0], i);
2736 dw2_asm_output_data_uleb128 (INTVAL (cs->landing_pad),
2737 "region %d landing pad", i);
2738 dw2_asm_output_data_uleb128 (cs->action, "action");
2741 call_site_base += n;
2744 /* Switch to the section that should be used for exception tables. */
2746 static void
2747 switch_to_exception_section (const char * ARG_UNUSED (fnname))
2749 section *s;
2751 if (exception_section)
2752 s = exception_section;
2753 else
2755 /* Compute the section and cache it into exception_section,
2756 unless it depends on the function name. */
2757 if (targetm_common.have_named_sections)
2759 int flags;
2761 if (EH_TABLES_CAN_BE_READ_ONLY)
2763 int tt_format =
2764 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
2765 flags = ((! flag_pic
2766 || ((tt_format & 0x70) != DW_EH_PE_absptr
2767 && (tt_format & 0x70) != DW_EH_PE_aligned))
2768 ? 0 : SECTION_WRITE);
2770 else
2771 flags = SECTION_WRITE;
2773 #ifdef HAVE_LD_EH_GC_SECTIONS
2774 if (flag_function_sections
2775 || (DECL_ONE_ONLY (current_function_decl) && HAVE_COMDAT_GROUP))
2777 char *section_name = XNEWVEC (char, strlen (fnname) + 32);
2778 /* The EH table must match the code section, so only mark
2779 it linkonce if we have COMDAT groups to tie them together. */
2780 if (DECL_ONE_ONLY (current_function_decl) && HAVE_COMDAT_GROUP)
2781 flags |= SECTION_LINKONCE;
2782 sprintf (section_name, ".gcc_except_table.%s", fnname);
2783 s = get_section (section_name, flags, current_function_decl);
2784 free (section_name);
2786 else
2787 #endif
2788 exception_section
2789 = s = get_section (".gcc_except_table", flags, NULL);
2791 else
2792 exception_section
2793 = s = flag_pic ? data_section : readonly_data_section;
2796 switch_to_section (s);
2800 /* Output a reference from an exception table to the type_info object TYPE.
2801 TT_FORMAT and TT_FORMAT_SIZE describe the DWARF encoding method used for
2802 the value. */
2804 static void
2805 output_ttype (tree type, int tt_format, int tt_format_size)
2807 rtx value;
2808 bool is_public = true;
2810 if (type == NULL_TREE)
2811 value = const0_rtx;
2812 else
2814 /* FIXME lto. pass_ipa_free_lang_data changes all types to
2815 runtime types so TYPE should already be a runtime type
2816 reference. When pass_ipa_free_lang data is made a default
2817 pass, we can then remove the call to lookup_type_for_runtime
2818 below. */
2819 if (TYPE_P (type))
2820 type = lookup_type_for_runtime (type);
2822 value = expand_expr (type, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
2824 /* Let cgraph know that the rtti decl is used. Not all of the
2825 paths below go through assemble_integer, which would take
2826 care of this for us. */
2827 STRIP_NOPS (type);
2828 if (TREE_CODE (type) == ADDR_EXPR)
2830 type = TREE_OPERAND (type, 0);
2831 if (TREE_CODE (type) == VAR_DECL)
2832 is_public = TREE_PUBLIC (type);
2834 else
2835 gcc_assert (TREE_CODE (type) == INTEGER_CST);
2838 /* Allow the target to override the type table entry format. */
2839 if (targetm.asm_out.ttype (value))
2840 return;
2842 if (tt_format == DW_EH_PE_absptr || tt_format == DW_EH_PE_aligned)
2843 assemble_integer (value, tt_format_size,
2844 tt_format_size * BITS_PER_UNIT, 1);
2845 else
2846 dw2_asm_output_encoded_addr_rtx (tt_format, value, is_public, NULL);
2849 static void
2850 output_one_function_exception_table (int section)
2852 int tt_format, cs_format, lp_format, i;
2853 #ifdef HAVE_AS_LEB128
2854 char ttype_label[32];
2855 char cs_after_size_label[32];
2856 char cs_end_label[32];
2857 #else
2858 int call_site_len;
2859 #endif
2860 int have_tt_data;
2861 int tt_format_size = 0;
2863 have_tt_data = (VEC_length (tree, cfun->eh->ttype_data)
2864 || (targetm.arm_eabi_unwinder
2865 ? VEC_length (tree, cfun->eh->ehspec_data.arm_eabi)
2866 : VEC_length (uchar, cfun->eh->ehspec_data.other)));
2868 /* Indicate the format of the @TType entries. */
2869 if (! have_tt_data)
2870 tt_format = DW_EH_PE_omit;
2871 else
2873 tt_format = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
2874 #ifdef HAVE_AS_LEB128
2875 ASM_GENERATE_INTERNAL_LABEL (ttype_label,
2876 section ? "LLSDATTC" : "LLSDATT",
2877 current_function_funcdef_no);
2878 #endif
2879 tt_format_size = size_of_encoded_value (tt_format);
2881 assemble_align (tt_format_size * BITS_PER_UNIT);
2884 targetm.asm_out.internal_label (asm_out_file, section ? "LLSDAC" : "LLSDA",
2885 current_function_funcdef_no);
2887 /* The LSDA header. */
2889 /* Indicate the format of the landing pad start pointer. An omitted
2890 field implies @LPStart == @Start. */
2891 /* Currently we always put @LPStart == @Start. This field would
2892 be most useful in moving the landing pads completely out of
2893 line to another section, but it could also be used to minimize
2894 the size of uleb128 landing pad offsets. */
2895 lp_format = DW_EH_PE_omit;
2896 dw2_asm_output_data (1, lp_format, "@LPStart format (%s)",
2897 eh_data_format_name (lp_format));
2899 /* @LPStart pointer would go here. */
2901 dw2_asm_output_data (1, tt_format, "@TType format (%s)",
2902 eh_data_format_name (tt_format));
2904 #ifndef HAVE_AS_LEB128
2905 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
2906 call_site_len = sjlj_size_of_call_site_table ();
2907 else
2908 call_site_len = dw2_size_of_call_site_table (section);
2909 #endif
2911 /* A pc-relative 4-byte displacement to the @TType data. */
2912 if (have_tt_data)
2914 #ifdef HAVE_AS_LEB128
2915 char ttype_after_disp_label[32];
2916 ASM_GENERATE_INTERNAL_LABEL (ttype_after_disp_label,
2917 section ? "LLSDATTDC" : "LLSDATTD",
2918 current_function_funcdef_no);
2919 dw2_asm_output_delta_uleb128 (ttype_label, ttype_after_disp_label,
2920 "@TType base offset");
2921 ASM_OUTPUT_LABEL (asm_out_file, ttype_after_disp_label);
2922 #else
2923 /* Ug. Alignment queers things. */
2924 unsigned int before_disp, after_disp, last_disp, disp;
2926 before_disp = 1 + 1;
2927 after_disp = (1 + size_of_uleb128 (call_site_len)
2928 + call_site_len
2929 + VEC_length (uchar, crtl->eh.action_record_data)
2930 + (VEC_length (tree, cfun->eh->ttype_data)
2931 * tt_format_size));
2933 disp = after_disp;
2936 unsigned int disp_size, pad;
2938 last_disp = disp;
2939 disp_size = size_of_uleb128 (disp);
2940 pad = before_disp + disp_size + after_disp;
2941 if (pad % tt_format_size)
2942 pad = tt_format_size - (pad % tt_format_size);
2943 else
2944 pad = 0;
2945 disp = after_disp + pad;
2947 while (disp != last_disp);
2949 dw2_asm_output_data_uleb128 (disp, "@TType base offset");
2950 #endif
2953 /* Indicate the format of the call-site offsets. */
2954 #ifdef HAVE_AS_LEB128
2955 cs_format = DW_EH_PE_uleb128;
2956 #else
2957 cs_format = DW_EH_PE_udata4;
2958 #endif
2959 dw2_asm_output_data (1, cs_format, "call-site format (%s)",
2960 eh_data_format_name (cs_format));
2962 #ifdef HAVE_AS_LEB128
2963 ASM_GENERATE_INTERNAL_LABEL (cs_after_size_label,
2964 section ? "LLSDACSBC" : "LLSDACSB",
2965 current_function_funcdef_no);
2966 ASM_GENERATE_INTERNAL_LABEL (cs_end_label,
2967 section ? "LLSDACSEC" : "LLSDACSE",
2968 current_function_funcdef_no);
2969 dw2_asm_output_delta_uleb128 (cs_end_label, cs_after_size_label,
2970 "Call-site table length");
2971 ASM_OUTPUT_LABEL (asm_out_file, cs_after_size_label);
2972 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
2973 sjlj_output_call_site_table ();
2974 else
2975 dw2_output_call_site_table (cs_format, section);
2976 ASM_OUTPUT_LABEL (asm_out_file, cs_end_label);
2977 #else
2978 dw2_asm_output_data_uleb128 (call_site_len, "Call-site table length");
2979 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
2980 sjlj_output_call_site_table ();
2981 else
2982 dw2_output_call_site_table (cs_format, section);
2983 #endif
2985 /* ??? Decode and interpret the data for flag_debug_asm. */
2987 uchar uc;
2988 FOR_EACH_VEC_ELT (uchar, crtl->eh.action_record_data, i, uc)
2989 dw2_asm_output_data (1, uc, i ? NULL : "Action record table");
2992 if (have_tt_data)
2993 assemble_align (tt_format_size * BITS_PER_UNIT);
2995 i = VEC_length (tree, cfun->eh->ttype_data);
2996 while (i-- > 0)
2998 tree type = VEC_index (tree, cfun->eh->ttype_data, i);
2999 output_ttype (type, tt_format, tt_format_size);
3002 #ifdef HAVE_AS_LEB128
3003 if (have_tt_data)
3004 ASM_OUTPUT_LABEL (asm_out_file, ttype_label);
3005 #endif
3007 /* ??? Decode and interpret the data for flag_debug_asm. */
3008 if (targetm.arm_eabi_unwinder)
3010 tree type;
3011 for (i = 0;
3012 VEC_iterate (tree, cfun->eh->ehspec_data.arm_eabi, i, type); ++i)
3013 output_ttype (type, tt_format, tt_format_size);
3015 else
3017 uchar uc;
3018 for (i = 0;
3019 VEC_iterate (uchar, cfun->eh->ehspec_data.other, i, uc); ++i)
3020 dw2_asm_output_data (1, uc,
3021 i ? NULL : "Exception specification table");
3025 void
3026 output_function_exception_table (const char *fnname)
3028 rtx personality = get_personality_function (current_function_decl);
3030 /* Not all functions need anything. */
3031 if (! crtl->uses_eh_lsda)
3032 return;
3034 if (personality)
3036 assemble_external_libcall (personality);
3038 if (targetm.asm_out.emit_except_personality)
3039 targetm.asm_out.emit_except_personality (personality);
3042 switch_to_exception_section (fnname);
3044 /* If the target wants a label to begin the table, emit it here. */
3045 targetm.asm_out.emit_except_table_label (asm_out_file);
3047 output_one_function_exception_table (0);
3048 if (crtl->eh.call_site_record_v[1] != NULL)
3049 output_one_function_exception_table (1);
3051 switch_to_section (current_function_section ());
3054 void
3055 set_eh_throw_stmt_table (struct function *fun, struct htab *table)
3057 fun->eh->throw_stmt_table = table;
3060 htab_t
3061 get_eh_throw_stmt_table (struct function *fun)
3063 return fun->eh->throw_stmt_table;
3066 /* Determine if the function needs an EH personality function. */
3068 enum eh_personality_kind
3069 function_needs_eh_personality (struct function *fn)
3071 enum eh_personality_kind kind = eh_personality_none;
3072 eh_region i;
3074 FOR_ALL_EH_REGION_FN (i, fn)
3076 switch (i->type)
3078 case ERT_CLEANUP:
3079 /* Can do with any personality including the generic C one. */
3080 kind = eh_personality_any;
3081 break;
3083 case ERT_TRY:
3084 case ERT_ALLOWED_EXCEPTIONS:
3085 /* Always needs a EH personality function. The generic C
3086 personality doesn't handle these even for empty type lists. */
3087 return eh_personality_lang;
3089 case ERT_MUST_NOT_THROW:
3090 /* Always needs a EH personality function. The language may specify
3091 what abort routine that must be used, e.g. std::terminate. */
3092 return eh_personality_lang;
3096 return kind;
3099 /* Dump EH information to OUT. */
3101 void
3102 dump_eh_tree (FILE * out, struct function *fun)
3104 eh_region i;
3105 int depth = 0;
3106 static const char *const type_name[] = {
3107 "cleanup", "try", "allowed_exceptions", "must_not_throw"
3110 i = fun->eh->region_tree;
3111 if (!i)
3112 return;
3114 fprintf (out, "Eh tree:\n");
3115 while (1)
3117 fprintf (out, " %*s %i %s", depth * 2, "",
3118 i->index, type_name[(int) i->type]);
3120 if (i->landing_pads)
3122 eh_landing_pad lp;
3124 fprintf (out, " land:");
3125 if (current_ir_type () == IR_GIMPLE)
3127 for (lp = i->landing_pads; lp ; lp = lp->next_lp)
3129 fprintf (out, "{%i,", lp->index);
3130 print_generic_expr (out, lp->post_landing_pad, 0);
3131 fputc ('}', out);
3132 if (lp->next_lp)
3133 fputc (',', out);
3136 else
3138 for (lp = i->landing_pads; lp ; lp = lp->next_lp)
3140 fprintf (out, "{%i,", lp->index);
3141 if (lp->landing_pad)
3142 fprintf (out, "%i%s,", INSN_UID (lp->landing_pad),
3143 NOTE_P (lp->landing_pad) ? "(del)" : "");
3144 else
3145 fprintf (out, "(nil),");
3146 if (lp->post_landing_pad)
3148 rtx lab = label_rtx (lp->post_landing_pad);
3149 fprintf (out, "%i%s}", INSN_UID (lab),
3150 NOTE_P (lab) ? "(del)" : "");
3152 else
3153 fprintf (out, "(nil)}");
3154 if (lp->next_lp)
3155 fputc (',', out);
3160 switch (i->type)
3162 case ERT_CLEANUP:
3163 case ERT_MUST_NOT_THROW:
3164 break;
3166 case ERT_TRY:
3168 eh_catch c;
3169 fprintf (out, " catch:");
3170 for (c = i->u.eh_try.first_catch; c; c = c->next_catch)
3172 fputc ('{', out);
3173 if (c->label)
3175 fprintf (out, "lab:");
3176 print_generic_expr (out, c->label, 0);
3177 fputc (';', out);
3179 print_generic_expr (out, c->type_list, 0);
3180 fputc ('}', out);
3181 if (c->next_catch)
3182 fputc (',', out);
3185 break;
3187 case ERT_ALLOWED_EXCEPTIONS:
3188 fprintf (out, " filter :%i types:", i->u.allowed.filter);
3189 print_generic_expr (out, i->u.allowed.type_list, 0);
3190 break;
3192 fputc ('\n', out);
3194 /* If there are sub-regions, process them. */
3195 if (i->inner)
3196 i = i->inner, depth++;
3197 /* If there are peers, process them. */
3198 else if (i->next_peer)
3199 i = i->next_peer;
3200 /* Otherwise, step back up the tree to the next peer. */
3201 else
3205 i = i->outer;
3206 depth--;
3207 if (i == NULL)
3208 return;
3210 while (i->next_peer == NULL);
3211 i = i->next_peer;
3216 /* Dump the EH tree for FN on stderr. */
3218 DEBUG_FUNCTION void
3219 debug_eh_tree (struct function *fn)
3221 dump_eh_tree (stderr, fn);
3224 /* Verify invariants on EH datastructures. */
3226 DEBUG_FUNCTION void
3227 verify_eh_tree (struct function *fun)
3229 eh_region r, outer;
3230 int nvisited_lp, nvisited_r;
3231 int count_lp, count_r, depth, i;
3232 eh_landing_pad lp;
3233 bool err = false;
3235 if (!fun->eh->region_tree)
3236 return;
3238 count_r = 0;
3239 for (i = 1; VEC_iterate (eh_region, fun->eh->region_array, i, r); ++i)
3240 if (r)
3242 if (r->index == i)
3243 count_r++;
3244 else
3246 error ("region_array is corrupted for region %i", r->index);
3247 err = true;
3251 count_lp = 0;
3252 for (i = 1; VEC_iterate (eh_landing_pad, fun->eh->lp_array, i, lp); ++i)
3253 if (lp)
3255 if (lp->index == i)
3256 count_lp++;
3257 else
3259 error ("lp_array is corrupted for lp %i", lp->index);
3260 err = true;
3264 depth = nvisited_lp = nvisited_r = 0;
3265 outer = NULL;
3266 r = fun->eh->region_tree;
3267 while (1)
3269 if (VEC_index (eh_region, fun->eh->region_array, r->index) != r)
3271 error ("region_array is corrupted for region %i", r->index);
3272 err = true;
3274 if (r->outer != outer)
3276 error ("outer block of region %i is wrong", r->index);
3277 err = true;
3279 if (depth < 0)
3281 error ("negative nesting depth of region %i", r->index);
3282 err = true;
3284 nvisited_r++;
3286 for (lp = r->landing_pads; lp ; lp = lp->next_lp)
3288 if (VEC_index (eh_landing_pad, fun->eh->lp_array, lp->index) != lp)
3290 error ("lp_array is corrupted for lp %i", lp->index);
3291 err = true;
3293 if (lp->region != r)
3295 error ("region of lp %i is wrong", lp->index);
3296 err = true;
3298 nvisited_lp++;
3301 if (r->inner)
3302 outer = r, r = r->inner, depth++;
3303 else if (r->next_peer)
3304 r = r->next_peer;
3305 else
3309 r = r->outer;
3310 if (r == NULL)
3311 goto region_done;
3312 depth--;
3313 outer = r->outer;
3315 while (r->next_peer == NULL);
3316 r = r->next_peer;
3319 region_done:
3320 if (depth != 0)
3322 error ("tree list ends on depth %i", depth);
3323 err = true;
3325 if (count_r != nvisited_r)
3327 error ("region_array does not match region_tree");
3328 err = true;
3330 if (count_lp != nvisited_lp)
3332 error ("lp_array does not match region_tree");
3333 err = true;
3336 if (err)
3338 dump_eh_tree (stderr, fun);
3339 internal_error ("verify_eh_tree failed");
3343 #include "gt-except.h"