Fix DealII type problems.
[official-gcc/Ramakrishna.git] / gcc / except.c
blob572aad0f8422e6a7a35ac617ab17fe8c607308db
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
4 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 lang_protect_cleanup_actions hook 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 During pass_rtl_eh (except.c), 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 "integrate.h"
127 #include "hard-reg-set.h"
128 #include "basic-block.h"
129 #include "output.h"
130 #include "dwarf2asm.h"
131 #include "dwarf2out.h"
132 #include "dwarf2.h"
133 #include "toplev.h"
134 #include "hashtab.h"
135 #include "intl.h"
136 #include "ggc.h"
137 #include "tm_p.h"
138 #include "target.h"
139 #include "langhooks.h"
140 #include "cgraph.h"
141 #include "diagnostic.h"
142 #include "tree-pass.h"
143 #include "timevar.h"
144 #include "tree-flow.h"
146 /* Provide defaults for stuff that may not be defined when using
147 sjlj exceptions. */
148 #ifndef EH_RETURN_DATA_REGNO
149 #define EH_RETURN_DATA_REGNO(N) INVALID_REGNUM
150 #endif
152 /* Protect cleanup actions with must-not-throw regions, with a call
153 to the given failure handler. */
154 tree (*lang_protect_cleanup_actions) (void);
156 /* Return true if type A catches type B. */
157 int (*lang_eh_type_covers) (tree a, tree b);
159 static GTY(()) int call_site_base;
160 static GTY ((param_is (union tree_node)))
161 htab_t type_to_runtime_map;
163 /* Describe the SjLj_Function_Context structure. */
164 static GTY(()) tree sjlj_fc_type_node;
165 static int sjlj_fc_call_site_ofs;
166 static int sjlj_fc_data_ofs;
167 static int sjlj_fc_personality_ofs;
168 static int sjlj_fc_lsda_ofs;
169 static int sjlj_fc_jbuf_ofs;
172 struct GTY(()) call_site_record_d
174 rtx landing_pad;
175 int action;
178 static bool get_eh_region_and_lp_from_rtx (const_rtx, eh_region *,
179 eh_landing_pad *);
181 static int t2r_eq (const void *, const void *);
182 static hashval_t t2r_hash (const void *);
184 static int ttypes_filter_eq (const void *, const void *);
185 static hashval_t ttypes_filter_hash (const void *);
186 static int ehspec_filter_eq (const void *, const void *);
187 static hashval_t ehspec_filter_hash (const void *);
188 static int add_ttypes_entry (htab_t, tree);
189 static int add_ehspec_entry (htab_t, htab_t, tree);
190 static void dw2_build_landing_pads (void);
192 static int action_record_eq (const void *, const void *);
193 static hashval_t action_record_hash (const void *);
194 static int add_action_record (htab_t, int, int);
195 static int collect_one_action_chain (htab_t, eh_region);
196 static int add_call_site (rtx, int, int);
198 static void push_uleb128 (VEC (uchar, gc) **, unsigned int);
199 static void push_sleb128 (VEC (uchar, gc) **, int);
200 #ifndef HAVE_AS_LEB128
201 static int dw2_size_of_call_site_table (int);
202 static int sjlj_size_of_call_site_table (void);
203 #endif
204 static void dw2_output_call_site_table (int, int);
205 static void sjlj_output_call_site_table (void);
208 /* Routine to see if exception handling is turned on.
209 DO_WARN is nonzero if we want to inform the user that exception
210 handling is turned off.
212 This is used to ensure that -fexceptions has been specified if the
213 compiler tries to use any exception-specific functions. */
216 doing_eh (int do_warn)
218 if (! flag_exceptions)
220 static int warned = 0;
221 if (! warned && do_warn)
223 error ("exception handling disabled, use -fexceptions to enable");
224 warned = 1;
226 return 0;
228 return 1;
232 void
233 init_eh (void)
235 if (! flag_exceptions)
236 return;
238 type_to_runtime_map = htab_create_ggc (31, t2r_hash, t2r_eq, NULL);
240 /* Create the SjLj_Function_Context structure. This should match
241 the definition in unwind-sjlj.c. */
242 if (USING_SJLJ_EXCEPTIONS)
244 tree f_jbuf, f_per, f_lsda, f_prev, f_cs, f_data, tmp;
246 sjlj_fc_type_node = lang_hooks.types.make_type (RECORD_TYPE);
248 f_prev = build_decl (BUILTINS_LOCATION,
249 FIELD_DECL, get_identifier ("__prev"),
250 build_pointer_type (sjlj_fc_type_node));
251 DECL_FIELD_CONTEXT (f_prev) = sjlj_fc_type_node;
253 f_cs = build_decl (BUILTINS_LOCATION,
254 FIELD_DECL, get_identifier ("__call_site"),
255 integer_type_node);
256 DECL_FIELD_CONTEXT (f_cs) = sjlj_fc_type_node;
258 tmp = build_index_type (build_int_cst (NULL_TREE, 4 - 1));
259 tmp = build_array_type (lang_hooks.types.type_for_mode
260 (targetm.unwind_word_mode (), 1),
261 tmp);
262 f_data = build_decl (BUILTINS_LOCATION,
263 FIELD_DECL, get_identifier ("__data"), tmp);
264 DECL_FIELD_CONTEXT (f_data) = sjlj_fc_type_node;
266 f_per = build_decl (BUILTINS_LOCATION,
267 FIELD_DECL, get_identifier ("__personality"),
268 ptr_type_node);
269 DECL_FIELD_CONTEXT (f_per) = sjlj_fc_type_node;
271 f_lsda = build_decl (BUILTINS_LOCATION,
272 FIELD_DECL, get_identifier ("__lsda"),
273 ptr_type_node);
274 DECL_FIELD_CONTEXT (f_lsda) = sjlj_fc_type_node;
276 #ifdef DONT_USE_BUILTIN_SETJMP
277 #ifdef JMP_BUF_SIZE
278 tmp = build_int_cst (NULL_TREE, JMP_BUF_SIZE - 1);
279 #else
280 /* Should be large enough for most systems, if it is not,
281 JMP_BUF_SIZE should be defined with the proper value. It will
282 also tend to be larger than necessary for most systems, a more
283 optimal port will define JMP_BUF_SIZE. */
284 tmp = build_int_cst (NULL_TREE, FIRST_PSEUDO_REGISTER + 2 - 1);
285 #endif
286 #else
287 /* builtin_setjmp takes a pointer to 5 words. */
288 tmp = build_int_cst (NULL_TREE, 5 * BITS_PER_WORD / POINTER_SIZE - 1);
289 #endif
290 tmp = build_index_type (tmp);
291 tmp = build_array_type (ptr_type_node, tmp);
292 f_jbuf = build_decl (BUILTINS_LOCATION,
293 FIELD_DECL, get_identifier ("__jbuf"), tmp);
294 #ifdef DONT_USE_BUILTIN_SETJMP
295 /* We don't know what the alignment requirements of the
296 runtime's jmp_buf has. Overestimate. */
297 DECL_ALIGN (f_jbuf) = BIGGEST_ALIGNMENT;
298 DECL_USER_ALIGN (f_jbuf) = 1;
299 #endif
300 DECL_FIELD_CONTEXT (f_jbuf) = sjlj_fc_type_node;
302 TYPE_FIELDS (sjlj_fc_type_node) = f_prev;
303 TREE_CHAIN (f_prev) = f_cs;
304 TREE_CHAIN (f_cs) = f_data;
305 TREE_CHAIN (f_data) = f_per;
306 TREE_CHAIN (f_per) = f_lsda;
307 TREE_CHAIN (f_lsda) = f_jbuf;
309 layout_type (sjlj_fc_type_node);
311 /* Cache the interesting field offsets so that we have
312 easy access from rtl. */
313 sjlj_fc_call_site_ofs
314 = (tree_low_cst (DECL_FIELD_OFFSET (f_cs), 1)
315 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_cs), 1) / BITS_PER_UNIT);
316 sjlj_fc_data_ofs
317 = (tree_low_cst (DECL_FIELD_OFFSET (f_data), 1)
318 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_data), 1) / BITS_PER_UNIT);
319 sjlj_fc_personality_ofs
320 = (tree_low_cst (DECL_FIELD_OFFSET (f_per), 1)
321 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_per), 1) / BITS_PER_UNIT);
322 sjlj_fc_lsda_ofs
323 = (tree_low_cst (DECL_FIELD_OFFSET (f_lsda), 1)
324 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_lsda), 1) / BITS_PER_UNIT);
325 sjlj_fc_jbuf_ofs
326 = (tree_low_cst (DECL_FIELD_OFFSET (f_jbuf), 1)
327 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_jbuf), 1) / BITS_PER_UNIT);
331 void
332 init_eh_for_function (void)
334 cfun->eh = GGC_CNEW (struct eh_status);
336 /* Make sure zero'th entries are used. */
337 VEC_safe_push (eh_region, gc, cfun->eh->region_array, NULL);
338 VEC_safe_push (eh_landing_pad, gc, cfun->eh->lp_array, NULL);
341 /* Routines to generate the exception tree somewhat directly.
342 These are used from tree-eh.c when processing exception related
343 nodes during tree optimization. */
345 static eh_region
346 gen_eh_region (enum eh_region_type type, eh_region outer)
348 eh_region new_eh;
350 #ifdef ENABLE_CHECKING
351 gcc_assert (doing_eh (0));
352 #endif
354 /* Insert a new blank region as a leaf in the tree. */
355 new_eh = GGC_CNEW (struct eh_region_d);
356 new_eh->type = type;
357 new_eh->outer = outer;
358 if (outer)
360 new_eh->next_peer = outer->inner;
361 outer->inner = new_eh;
363 else
365 new_eh->next_peer = cfun->eh->region_tree;
366 cfun->eh->region_tree = new_eh;
369 new_eh->index = VEC_length (eh_region, cfun->eh->region_array);
370 VEC_safe_push (eh_region, gc, cfun->eh->region_array, new_eh);
372 /* Copy the language's notion of whether to use __cxa_end_cleanup. */
373 if (targetm.arm_eabi_unwinder && lang_hooks.eh_use_cxa_end_cleanup)
374 new_eh->use_cxa_end_cleanup = true;
376 return new_eh;
379 eh_region
380 gen_eh_region_cleanup (eh_region outer)
382 return gen_eh_region (ERT_CLEANUP, outer);
385 eh_region
386 gen_eh_region_try (eh_region outer)
388 return gen_eh_region (ERT_TRY, outer);
391 eh_catch
392 gen_eh_region_catch (eh_region t, tree type_or_list)
394 eh_catch c, l;
395 tree type_list, type_node;
397 gcc_assert (t->type == ERT_TRY);
399 /* Ensure to always end up with a type list to normalize further
400 processing, then register each type against the runtime types map. */
401 type_list = type_or_list;
402 if (type_or_list)
404 if (TREE_CODE (type_or_list) != TREE_LIST)
405 type_list = tree_cons (NULL_TREE, type_or_list, NULL_TREE);
407 type_node = type_list;
408 for (; type_node; type_node = TREE_CHAIN (type_node))
409 add_type_for_runtime (TREE_VALUE (type_node));
412 c = GGC_CNEW (struct eh_catch_d);
413 c->type_list = type_list;
414 l = t->u.eh_try.last_catch;
415 c->prev_catch = l;
416 if (l)
417 l->next_catch = c;
418 else
419 t->u.eh_try.first_catch = c;
420 t->u.eh_try.last_catch = c;
422 return c;
425 eh_region
426 gen_eh_region_allowed (eh_region outer, tree allowed)
428 eh_region region = gen_eh_region (ERT_ALLOWED_EXCEPTIONS, outer);
429 region->u.allowed.type_list = allowed;
431 for (; allowed ; allowed = TREE_CHAIN (allowed))
432 add_type_for_runtime (TREE_VALUE (allowed));
434 return region;
437 eh_region
438 gen_eh_region_must_not_throw (eh_region outer)
440 return gen_eh_region (ERT_MUST_NOT_THROW, outer);
443 eh_landing_pad
444 gen_eh_landing_pad (eh_region region)
446 eh_landing_pad lp = GGC_CNEW (struct eh_landing_pad_d);
448 lp->next_lp = region->landing_pads;
449 lp->region = region;
450 lp->index = VEC_length (eh_landing_pad, cfun->eh->lp_array);
451 region->landing_pads = lp;
453 VEC_safe_push (eh_landing_pad, gc, cfun->eh->lp_array, lp);
455 return lp;
458 eh_region
459 get_eh_region_from_number_fn (struct function *ifun, int i)
461 return VEC_index (eh_region, ifun->eh->region_array, i);
464 eh_region
465 get_eh_region_from_number (int i)
467 return get_eh_region_from_number_fn (cfun, i);
470 eh_landing_pad
471 get_eh_landing_pad_from_number_fn (struct function *ifun, int i)
473 return VEC_index (eh_landing_pad, ifun->eh->lp_array, i);
476 eh_landing_pad
477 get_eh_landing_pad_from_number (int i)
479 return get_eh_landing_pad_from_number_fn (cfun, i);
482 eh_region
483 get_eh_region_from_lp_number_fn (struct function *ifun, int i)
485 if (i < 0)
486 return VEC_index (eh_region, ifun->eh->region_array, -i);
487 else if (i == 0)
488 return NULL;
489 else
491 eh_landing_pad lp;
492 lp = VEC_index (eh_landing_pad, ifun->eh->lp_array, i);
493 return lp->region;
497 eh_region
498 get_eh_region_from_lp_number (int i)
500 return get_eh_region_from_lp_number_fn (cfun, i);
503 /* Returns true if the current function has exception handling regions. */
505 bool
506 current_function_has_exception_handlers (void)
508 return cfun->eh->region_tree != NULL;
511 /* A subroutine of duplicate_eh_regions. Copy the eh_region tree at OLD.
512 Root it at OUTER, and apply LP_OFFSET to the lp numbers. */
514 struct duplicate_eh_regions_data
516 duplicate_eh_regions_map label_map;
517 void *label_map_data;
518 struct pointer_map_t *eh_map;
521 static void
522 duplicate_eh_regions_1 (struct duplicate_eh_regions_data *data,
523 eh_region old_r, eh_region outer)
525 eh_landing_pad old_lp, new_lp;
526 eh_region new_r;
527 void **slot;
529 new_r = gen_eh_region (old_r->type, outer);
530 slot = pointer_map_insert (data->eh_map, (void *)old_r);
531 gcc_assert (*slot == NULL);
532 *slot = (void *)new_r;
534 switch (old_r->type)
536 case ERT_CLEANUP:
537 break;
539 case ERT_TRY:
541 eh_catch oc, nc;
542 for (oc = old_r->u.eh_try.first_catch; oc ; oc = oc->next_catch)
544 /* We should be doing all our region duplication before and
545 during inlining, which is before filter lists are created. */
546 gcc_assert (oc->filter_list == NULL);
547 nc = gen_eh_region_catch (new_r, oc->type_list);
548 nc->label = data->label_map (oc->label, data->label_map_data);
551 break;
553 case ERT_ALLOWED_EXCEPTIONS:
554 new_r->u.allowed.type_list = old_r->u.allowed.type_list;
555 if (old_r->u.allowed.label)
556 new_r->u.allowed.label
557 = data->label_map (old_r->u.allowed.label, data->label_map_data);
558 else
559 new_r->u.allowed.label = NULL_TREE;
560 break;
562 case ERT_MUST_NOT_THROW:
563 new_r->u.must_not_throw = old_r->u.must_not_throw;
564 break;
567 for (old_lp = old_r->landing_pads; old_lp ; old_lp = old_lp->next_lp)
569 /* Don't bother copying unused landing pads. */
570 if (old_lp->post_landing_pad == NULL)
571 continue;
573 new_lp = gen_eh_landing_pad (new_r);
574 slot = pointer_map_insert (data->eh_map, (void *)old_lp);
575 gcc_assert (*slot == NULL);
576 *slot = (void *)new_lp;
578 new_lp->post_landing_pad
579 = data->label_map (old_lp->post_landing_pad, data->label_map_data);
580 EH_LANDING_PAD_NR (new_lp->post_landing_pad) = new_lp->index;
583 /* Make sure to preserve the original use of __cxa_end_cleanup. */
584 new_r->use_cxa_end_cleanup = old_r->use_cxa_end_cleanup;
586 for (old_r = old_r->inner; old_r ; old_r = old_r->next_peer)
587 duplicate_eh_regions_1 (data, old_r, new_r);
590 /* Duplicate the EH regions from IFUN rooted at COPY_REGION into
591 the current function and root the tree below OUTER_REGION.
592 The special case of COPY_REGION of NULL means all regions.
593 Remap labels using MAP/MAP_DATA callback. Return a pointer map
594 that allows the caller to remap uses of both EH regions and
595 EH landing pads. */
597 struct pointer_map_t *
598 duplicate_eh_regions (struct function *ifun,
599 eh_region copy_region, int outer_lp,
600 duplicate_eh_regions_map map, void *map_data)
602 struct duplicate_eh_regions_data data;
603 eh_region outer_region;
605 #ifdef ENABLE_CHECKING
606 verify_eh_tree (ifun);
607 #endif
609 data.label_map = map;
610 data.label_map_data = map_data;
611 data.eh_map = pointer_map_create ();
613 outer_region = get_eh_region_from_lp_number (outer_lp);
615 /* Copy all the regions in the subtree. */
616 if (copy_region)
617 duplicate_eh_regions_1 (&data, copy_region, outer_region);
618 else
620 eh_region r;
621 for (r = ifun->eh->region_tree; r ; r = r->next_peer)
622 duplicate_eh_regions_1 (&data, r, outer_region);
625 #ifdef ENABLE_CHECKING
626 verify_eh_tree (cfun);
627 #endif
629 return data.eh_map;
632 /* Return the region that is outer to both REGION_A and REGION_B in IFUN. */
634 eh_region
635 eh_region_outermost (struct function *ifun, eh_region region_a,
636 eh_region region_b)
638 sbitmap b_outer;
640 gcc_assert (ifun->eh->region_array);
641 gcc_assert (ifun->eh->region_tree);
643 b_outer = sbitmap_alloc (VEC_length (eh_region, ifun->eh->region_array));
644 sbitmap_zero (b_outer);
648 SET_BIT (b_outer, region_b->index);
649 region_b = region_b->outer;
651 while (region_b);
655 if (TEST_BIT (b_outer, region_a->index))
656 break;
657 region_a = region_a->outer;
659 while (region_a);
661 sbitmap_free (b_outer);
662 return region_a;
665 static int
666 t2r_eq (const void *pentry, const void *pdata)
668 const_tree const entry = (const_tree) pentry;
669 const_tree const data = (const_tree) pdata;
671 return TREE_PURPOSE (entry) == data;
674 static hashval_t
675 t2r_hash (const void *pentry)
677 const_tree const entry = (const_tree) pentry;
678 return TREE_HASH (TREE_PURPOSE (entry));
681 void
682 add_type_for_runtime (tree type)
684 tree *slot;
686 /* If TYPE is NOP_EXPR, it means that it already is a runtime type. */
687 if (TREE_CODE (type) == NOP_EXPR)
688 return;
690 slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
691 TREE_HASH (type), INSERT);
692 if (*slot == NULL)
694 tree runtime = lang_hooks.eh_runtime_type (type);
695 *slot = tree_cons (type, runtime, NULL_TREE);
699 tree
700 lookup_type_for_runtime (tree type)
702 tree *slot;
704 /* If TYPE is NOP_EXPR, it means that it already is a runtime type. */
705 if (TREE_CODE (type) == NOP_EXPR)
706 return type;
708 slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
709 TREE_HASH (type), NO_INSERT);
711 /* We should have always inserted the data earlier. */
712 return TREE_VALUE (*slot);
716 /* Represent an entry in @TTypes for either catch actions
717 or exception filter actions. */
718 struct GTY(()) ttypes_filter {
719 tree t;
720 int filter;
723 /* Compare ENTRY (a ttypes_filter entry in the hash table) with DATA
724 (a tree) for a @TTypes type node we are thinking about adding. */
726 static int
727 ttypes_filter_eq (const void *pentry, const void *pdata)
729 const struct ttypes_filter *const entry
730 = (const struct ttypes_filter *) pentry;
731 const_tree const data = (const_tree) pdata;
733 return entry->t == data;
736 static hashval_t
737 ttypes_filter_hash (const void *pentry)
739 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
740 return TREE_HASH (entry->t);
743 /* Compare ENTRY with DATA (both struct ttypes_filter) for a @TTypes
744 exception specification list we are thinking about adding. */
745 /* ??? Currently we use the type lists in the order given. Someone
746 should put these in some canonical order. */
748 static int
749 ehspec_filter_eq (const void *pentry, const void *pdata)
751 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
752 const struct ttypes_filter *data = (const struct ttypes_filter *) pdata;
754 return type_list_equal (entry->t, data->t);
757 /* Hash function for exception specification lists. */
759 static hashval_t
760 ehspec_filter_hash (const void *pentry)
762 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
763 hashval_t h = 0;
764 tree list;
766 for (list = entry->t; list ; list = TREE_CHAIN (list))
767 h = (h << 5) + (h >> 27) + TREE_HASH (TREE_VALUE (list));
768 return h;
771 /* Add TYPE (which may be NULL) to cfun->eh->ttype_data, using TYPES_HASH
772 to speed up the search. Return the filter value to be used. */
774 static int
775 add_ttypes_entry (htab_t ttypes_hash, tree type)
777 struct ttypes_filter **slot, *n;
779 slot = (struct ttypes_filter **)
780 htab_find_slot_with_hash (ttypes_hash, type, TREE_HASH (type), INSERT);
782 if ((n = *slot) == NULL)
784 /* Filter value is a 1 based table index. */
786 n = XNEW (struct ttypes_filter);
787 n->t = type;
788 n->filter = VEC_length (tree, cfun->eh->ttype_data) + 1;
789 *slot = n;
791 VEC_safe_push (tree, gc, cfun->eh->ttype_data, type);
794 return n->filter;
797 /* Add LIST to cfun->eh->ehspec_data, using EHSPEC_HASH and TYPES_HASH
798 to speed up the search. Return the filter value to be used. */
800 static int
801 add_ehspec_entry (htab_t ehspec_hash, htab_t ttypes_hash, tree list)
803 struct ttypes_filter **slot, *n;
804 struct ttypes_filter dummy;
806 dummy.t = list;
807 slot = (struct ttypes_filter **)
808 htab_find_slot (ehspec_hash, &dummy, INSERT);
810 if ((n = *slot) == NULL)
812 int len;
814 if (targetm.arm_eabi_unwinder)
815 len = VEC_length (tree, cfun->eh->ehspec_data.arm_eabi);
816 else
817 len = VEC_length (uchar, cfun->eh->ehspec_data.other);
819 /* Filter value is a -1 based byte index into a uleb128 buffer. */
821 n = XNEW (struct ttypes_filter);
822 n->t = list;
823 n->filter = -(len + 1);
824 *slot = n;
826 /* Generate a 0 terminated list of filter values. */
827 for (; list ; list = TREE_CHAIN (list))
829 if (targetm.arm_eabi_unwinder)
830 VEC_safe_push (tree, gc, cfun->eh->ehspec_data.arm_eabi,
831 TREE_VALUE (list));
832 else
834 /* Look up each type in the list and encode its filter
835 value as a uleb128. */
836 push_uleb128 (&cfun->eh->ehspec_data.other,
837 add_ttypes_entry (ttypes_hash, TREE_VALUE (list)));
840 if (targetm.arm_eabi_unwinder)
841 VEC_safe_push (tree, gc, cfun->eh->ehspec_data.arm_eabi, NULL_TREE);
842 else
843 VEC_safe_push (uchar, gc, cfun->eh->ehspec_data.other, 0);
846 return n->filter;
849 /* Generate the action filter values to be used for CATCH and
850 ALLOWED_EXCEPTIONS regions. When using dwarf2 exception regions,
851 we use lots of landing pads, and so every type or list can share
852 the same filter value, which saves table space. */
854 void
855 assign_filter_values (void)
857 int i;
858 htab_t ttypes, ehspec;
859 eh_region r;
860 eh_catch c;
862 cfun->eh->ttype_data = VEC_alloc (tree, gc, 16);
863 if (targetm.arm_eabi_unwinder)
864 cfun->eh->ehspec_data.arm_eabi = VEC_alloc (tree, gc, 64);
865 else
866 cfun->eh->ehspec_data.other = VEC_alloc (uchar, gc, 64);
868 ttypes = htab_create (31, ttypes_filter_hash, ttypes_filter_eq, free);
869 ehspec = htab_create (31, ehspec_filter_hash, ehspec_filter_eq, free);
871 for (i = 1; VEC_iterate (eh_region, cfun->eh->region_array, i, r); ++i)
873 if (r == NULL)
874 continue;
876 switch (r->type)
878 case ERT_TRY:
879 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
881 /* Whatever type_list is (NULL or true list), we build a list
882 of filters for the region. */
883 c->filter_list = NULL_TREE;
885 if (c->type_list != NULL)
887 /* Get a filter value for each of the types caught and store
888 them in the region's dedicated list. */
889 tree tp_node = c->type_list;
891 for ( ; tp_node; tp_node = TREE_CHAIN (tp_node))
893 int flt = add_ttypes_entry (ttypes, TREE_VALUE (tp_node));
894 tree flt_node = build_int_cst (NULL_TREE, flt);
896 c->filter_list
897 = tree_cons (NULL_TREE, flt_node, c->filter_list);
900 else
902 /* Get a filter value for the NULL list also since it
903 will need an action record anyway. */
904 int flt = add_ttypes_entry (ttypes, NULL);
905 tree flt_node = build_int_cst (NULL_TREE, flt);
907 c->filter_list
908 = tree_cons (NULL_TREE, flt_node, NULL);
911 break;
913 case ERT_ALLOWED_EXCEPTIONS:
914 r->u.allowed.filter
915 = add_ehspec_entry (ehspec, ttypes, r->u.allowed.type_list);
916 break;
918 default:
919 break;
923 htab_delete (ttypes);
924 htab_delete (ehspec);
927 /* Emit SEQ into basic block just before INSN (that is assumed to be
928 first instruction of some existing BB and return the newly
929 produced block. */
930 static basic_block
931 emit_to_new_bb_before (rtx seq, rtx insn)
933 rtx last;
934 basic_block bb;
935 edge e;
936 edge_iterator ei;
938 /* If there happens to be a fallthru edge (possibly created by cleanup_cfg
939 call), we don't want it to go into newly created landing pad or other EH
940 construct. */
941 for (ei = ei_start (BLOCK_FOR_INSN (insn)->preds); (e = ei_safe_edge (ei)); )
942 if (e->flags & EDGE_FALLTHRU)
943 force_nonfallthru (e);
944 else
945 ei_next (&ei);
946 last = emit_insn_before (seq, insn);
947 if (BARRIER_P (last))
948 last = PREV_INSN (last);
949 bb = create_basic_block (seq, last, BLOCK_FOR_INSN (insn)->prev_bb);
950 update_bb_for_insn (bb);
951 bb->flags |= BB_SUPERBLOCK;
952 return bb;
955 /* Expand the extra code needed at landing pads for dwarf2 unwinding. */
957 static void
958 dw2_build_landing_pads (void)
960 int i;
961 eh_landing_pad lp;
963 for (i = 1; VEC_iterate (eh_landing_pad, cfun->eh->lp_array, i, lp); ++i)
965 eh_region region;
966 basic_block bb;
967 rtx seq;
968 edge e;
970 if (lp == NULL || lp->post_landing_pad == NULL)
971 continue;
973 start_sequence ();
975 lp->landing_pad = gen_label_rtx ();
976 emit_label (lp->landing_pad);
978 #ifdef HAVE_exception_receiver
979 if (HAVE_exception_receiver)
980 emit_insn (gen_exception_receiver ());
981 else
982 #endif
983 #ifdef HAVE_nonlocal_goto_receiver
984 if (HAVE_nonlocal_goto_receiver)
985 emit_insn (gen_nonlocal_goto_receiver ());
986 else
987 #endif
988 { /* Nothing */ }
990 region = lp->region;
991 if (region->exc_ptr_reg)
992 emit_move_insn (region->exc_ptr_reg,
993 gen_rtx_REG (ptr_mode, EH_RETURN_DATA_REGNO (0)));
994 if (region->filter_reg)
995 emit_move_insn (region->filter_reg,
996 gen_rtx_REG (targetm.eh_return_filter_mode (),
997 EH_RETURN_DATA_REGNO (1)));
999 seq = get_insns ();
1000 end_sequence ();
1002 bb = emit_to_new_bb_before (seq, label_rtx (lp->post_landing_pad));
1003 e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
1004 e->count = bb->count;
1005 e->probability = REG_BR_PROB_BASE;
1010 static VEC (int, heap) *sjlj_lp_call_site_index;
1012 /* Process all active landing pads. Assign each one a compact dispatch
1013 index, and a call-site index. */
1015 static int
1016 sjlj_assign_call_site_values (void)
1018 htab_t ar_hash;
1019 int i, disp_index;
1020 eh_landing_pad lp;
1022 crtl->eh.action_record_data = VEC_alloc (uchar, gc, 64);
1023 ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
1025 disp_index = 0;
1026 call_site_base = 1;
1027 for (i = 1; VEC_iterate (eh_landing_pad, cfun->eh->lp_array, i, lp); ++i)
1028 if (lp && lp->post_landing_pad)
1030 int action, call_site;
1032 /* First: build the action table. */
1033 action = collect_one_action_chain (ar_hash, lp->region);
1034 if (action != -1)
1035 crtl->uses_eh_lsda = 1;
1037 /* Next: assign call-site values. If dwarf2 terms, this would be
1038 the region number assigned by convert_to_eh_region_ranges, but
1039 handles no-action and must-not-throw differently. */
1040 /* Map must-not-throw to otherwise unused call-site index 0. */
1041 if (action == -2)
1042 call_site = 0;
1043 /* Map no-action to otherwise unused call-site index -1. */
1044 else if (action == -1)
1045 call_site = -1;
1046 /* Otherwise, look it up in the table. */
1047 else
1048 call_site = add_call_site (GEN_INT (disp_index), action, 0);
1049 VEC_replace (int, sjlj_lp_call_site_index, i, call_site);
1051 disp_index++;
1054 htab_delete (ar_hash);
1056 return disp_index;
1059 /* Emit code to record the current call-site index before every
1060 insn that can throw. */
1062 static void
1063 sjlj_mark_call_sites (void)
1065 int last_call_site = -2;
1066 rtx insn, mem;
1068 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
1070 eh_landing_pad lp;
1071 eh_region r;
1072 bool nothrow;
1073 int this_call_site;
1074 rtx before, p;
1076 /* Reset value tracking at extended basic block boundaries. */
1077 if (LABEL_P (insn))
1078 last_call_site = -2;
1080 if (! INSN_P (insn))
1081 continue;
1083 nothrow = get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1084 if (nothrow)
1085 continue;
1086 if (lp)
1087 this_call_site = VEC_index (int, sjlj_lp_call_site_index, lp->index);
1088 else if (r == NULL)
1090 /* Calls (and trapping insns) without notes are outside any
1091 exception handling region in this function. Mark them as
1092 no action. */
1093 this_call_site = -1;
1095 else
1097 gcc_assert (r->type == ERT_MUST_NOT_THROW);
1098 this_call_site = 0;
1101 if (this_call_site == last_call_site)
1102 continue;
1104 /* Don't separate a call from it's argument loads. */
1105 before = insn;
1106 if (CALL_P (insn))
1107 before = find_first_parameter_load (insn, NULL_RTX);
1109 start_sequence ();
1110 mem = adjust_address (crtl->eh.sjlj_fc, TYPE_MODE (integer_type_node),
1111 sjlj_fc_call_site_ofs);
1112 emit_move_insn (mem, GEN_INT (this_call_site));
1113 p = get_insns ();
1114 end_sequence ();
1116 emit_insn_before (p, before);
1117 last_call_site = this_call_site;
1121 /* Construct the SjLj_Function_Context. */
1123 static void
1124 sjlj_emit_function_enter (rtx dispatch_label)
1126 rtx fn_begin, fc, mem, seq;
1127 bool fn_begin_outside_block;
1128 rtx personality = get_personality_function (current_function_decl);
1130 fc = crtl->eh.sjlj_fc;
1132 start_sequence ();
1134 /* We're storing this libcall's address into memory instead of
1135 calling it directly. Thus, we must call assemble_external_libcall
1136 here, as we can not depend on emit_library_call to do it for us. */
1137 assemble_external_libcall (personality);
1138 mem = adjust_address (fc, Pmode, sjlj_fc_personality_ofs);
1139 emit_move_insn (mem, personality);
1141 mem = adjust_address (fc, Pmode, sjlj_fc_lsda_ofs);
1142 if (crtl->uses_eh_lsda)
1144 char buf[20];
1145 rtx sym;
1147 ASM_GENERATE_INTERNAL_LABEL (buf, "LLSDA", current_function_funcdef_no);
1148 sym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
1149 SYMBOL_REF_FLAGS (sym) = SYMBOL_FLAG_LOCAL;
1150 emit_move_insn (mem, sym);
1152 else
1153 emit_move_insn (mem, const0_rtx);
1155 #ifdef DONT_USE_BUILTIN_SETJMP
1157 rtx x;
1158 x = emit_library_call_value (setjmp_libfunc, NULL_RTX, LCT_RETURNS_TWICE,
1159 TYPE_MODE (integer_type_node), 1,
1160 plus_constant (XEXP (fc, 0),
1161 sjlj_fc_jbuf_ofs), Pmode);
1163 emit_cmp_and_jump_insns (x, const0_rtx, NE, 0,
1164 TYPE_MODE (integer_type_node), 0, dispatch_label);
1165 add_reg_br_prob_note (get_insns (), REG_BR_PROB_BASE/100);
1167 #else
1168 expand_builtin_setjmp_setup (plus_constant (XEXP (fc, 0), sjlj_fc_jbuf_ofs),
1169 dispatch_label);
1170 #endif
1172 emit_library_call (unwind_sjlj_register_libfunc, LCT_NORMAL, VOIDmode,
1173 1, XEXP (fc, 0), Pmode);
1175 seq = get_insns ();
1176 end_sequence ();
1178 /* ??? Instead of doing this at the beginning of the function,
1179 do this in a block that is at loop level 0 and dominates all
1180 can_throw_internal instructions. */
1182 fn_begin_outside_block = true;
1183 for (fn_begin = get_insns (); ; fn_begin = NEXT_INSN (fn_begin))
1184 if (NOTE_P (fn_begin))
1186 if (NOTE_KIND (fn_begin) == NOTE_INSN_FUNCTION_BEG)
1187 break;
1188 else if (NOTE_INSN_BASIC_BLOCK_P (fn_begin))
1189 fn_begin_outside_block = false;
1192 if (fn_begin_outside_block)
1193 insert_insn_on_edge (seq, single_succ_edge (ENTRY_BLOCK_PTR));
1194 else
1195 emit_insn_after (seq, fn_begin);
1198 /* Call back from expand_function_end to know where we should put
1199 the call to unwind_sjlj_unregister_libfunc if needed. */
1201 void
1202 sjlj_emit_function_exit_after (rtx after)
1204 crtl->eh.sjlj_exit_after = after;
1207 static void
1208 sjlj_emit_function_exit (void)
1210 rtx seq, insn;
1212 start_sequence ();
1214 emit_library_call (unwind_sjlj_unregister_libfunc, LCT_NORMAL, VOIDmode,
1215 1, XEXP (crtl->eh.sjlj_fc, 0), Pmode);
1217 seq = get_insns ();
1218 end_sequence ();
1220 /* ??? Really this can be done in any block at loop level 0 that
1221 post-dominates all can_throw_internal instructions. This is
1222 the last possible moment. */
1224 insn = crtl->eh.sjlj_exit_after;
1225 if (LABEL_P (insn))
1226 insn = NEXT_INSN (insn);
1228 emit_insn_after (seq, insn);
1231 static void
1232 sjlj_emit_dispatch_table (rtx dispatch_label, int num_dispatch)
1234 enum machine_mode unwind_word_mode = targetm.unwind_word_mode ();
1235 enum machine_mode filter_mode = targetm.eh_return_filter_mode ();
1236 eh_landing_pad lp;
1237 rtx mem, seq, fc, before, exc_ptr_reg, filter_reg;
1238 rtx first_reachable_label;
1239 basic_block bb;
1240 eh_region r;
1241 edge e;
1242 int i, disp_index;
1243 gimple switch_stmt;
1245 fc = crtl->eh.sjlj_fc;
1247 start_sequence ();
1249 emit_label (dispatch_label);
1251 #ifndef DONT_USE_BUILTIN_SETJMP
1252 expand_builtin_setjmp_receiver (dispatch_label);
1254 /* The caller of expand_builtin_setjmp_receiver is responsible for
1255 making sure that the label doesn't vanish. The only other caller
1256 is the expander for __builtin_setjmp_receiver, which places this
1257 label on the nonlocal_goto_label list. Since we're modeling these
1258 CFG edges more exactly, we can use the forced_labels list instead. */
1259 LABEL_PRESERVE_P (dispatch_label) = 1;
1260 forced_labels
1261 = gen_rtx_EXPR_LIST (VOIDmode, dispatch_label, forced_labels);
1262 #endif
1264 /* Load up exc_ptr and filter values from the function context. */
1265 mem = adjust_address (fc, unwind_word_mode, sjlj_fc_data_ofs);
1266 if (unwind_word_mode != ptr_mode)
1268 #ifdef POINTERS_EXTEND_UNSIGNED
1269 mem = convert_memory_address (ptr_mode, mem);
1270 #else
1271 mem = convert_to_mode (ptr_mode, mem, 0);
1272 #endif
1274 exc_ptr_reg = force_reg (ptr_mode, mem);
1276 mem = adjust_address (fc, unwind_word_mode,
1277 sjlj_fc_data_ofs + GET_MODE_SIZE (unwind_word_mode));
1278 if (unwind_word_mode != filter_mode)
1279 mem = convert_to_mode (filter_mode, mem, 0);
1280 filter_reg = force_reg (filter_mode, mem);
1282 /* Jump to one of the directly reachable regions. */
1284 disp_index = 0;
1285 first_reachable_label = NULL;
1287 /* If there's exactly one call site in the function, don't bother
1288 generating a switch statement. */
1289 switch_stmt = NULL;
1290 if (num_dispatch > 1)
1292 tree disp;
1294 mem = adjust_address (fc, TYPE_MODE (integer_type_node),
1295 sjlj_fc_call_site_ofs);
1296 disp = make_tree (integer_type_node, mem);
1298 switch_stmt = gimple_build_switch_nlabels (num_dispatch, disp, NULL);
1301 for (i = 1; VEC_iterate (eh_landing_pad, cfun->eh->lp_array, i, lp); ++i)
1302 if (lp && lp->post_landing_pad)
1304 rtx seq2, label;
1306 start_sequence ();
1308 lp->landing_pad = dispatch_label;
1310 if (num_dispatch > 1)
1312 tree t_label, case_elt;
1314 t_label = create_artificial_label (UNKNOWN_LOCATION);
1315 case_elt = build3 (CASE_LABEL_EXPR, void_type_node,
1316 build_int_cst (NULL, disp_index),
1317 NULL, t_label);
1318 gimple_switch_set_label (switch_stmt, disp_index, case_elt);
1320 label = label_rtx (t_label);
1322 else
1323 label = gen_label_rtx ();
1325 if (disp_index == 0)
1326 first_reachable_label = label;
1327 emit_label (label);
1329 r = lp->region;
1330 if (r->exc_ptr_reg)
1331 emit_move_insn (r->exc_ptr_reg, exc_ptr_reg);
1332 if (r->filter_reg)
1333 emit_move_insn (r->filter_reg, filter_reg);
1335 seq2 = get_insns ();
1336 end_sequence ();
1338 before = label_rtx (lp->post_landing_pad);
1339 bb = emit_to_new_bb_before (seq2, before);
1340 e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
1341 e->count = bb->count;
1342 e->probability = REG_BR_PROB_BASE;
1344 disp_index++;
1346 gcc_assert (disp_index == num_dispatch);
1348 if (num_dispatch > 1)
1350 expand_case (switch_stmt);
1351 expand_builtin_trap ();
1354 seq = get_insns ();
1355 end_sequence ();
1357 bb = emit_to_new_bb_before (seq, first_reachable_label);
1358 if (num_dispatch == 1)
1360 e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
1361 e->count = bb->count;
1362 e->probability = REG_BR_PROB_BASE;
1366 static void
1367 sjlj_build_landing_pads (void)
1369 int num_dispatch;
1371 num_dispatch = VEC_length (eh_landing_pad, cfun->eh->lp_array);
1372 if (num_dispatch == 0)
1373 return;
1374 VEC_safe_grow (int, heap, sjlj_lp_call_site_index, num_dispatch);
1376 num_dispatch = sjlj_assign_call_site_values ();
1377 if (num_dispatch > 0)
1379 rtx dispatch_label = gen_label_rtx ();
1380 int align = STACK_SLOT_ALIGNMENT (sjlj_fc_type_node,
1381 TYPE_MODE (sjlj_fc_type_node),
1382 TYPE_ALIGN (sjlj_fc_type_node));
1383 crtl->eh.sjlj_fc
1384 = assign_stack_local (TYPE_MODE (sjlj_fc_type_node),
1385 int_size_in_bytes (sjlj_fc_type_node),
1386 align);
1388 sjlj_mark_call_sites ();
1389 sjlj_emit_function_enter (dispatch_label);
1390 sjlj_emit_dispatch_table (dispatch_label, num_dispatch);
1391 sjlj_emit_function_exit ();
1394 VEC_free (int, heap, sjlj_lp_call_site_index);
1397 /* After initial rtl generation, call back to finish generating
1398 exception support code. */
1400 static void
1401 finish_eh_generation (void)
1403 basic_block bb;
1405 /* Construct the landing pads. */
1406 if (USING_SJLJ_EXCEPTIONS)
1407 sjlj_build_landing_pads ();
1408 else
1409 dw2_build_landing_pads ();
1410 break_superblocks ();
1412 if (USING_SJLJ_EXCEPTIONS
1413 /* Kludge for Alpha/Tru64 (see alpha_gp_save_rtx). */
1414 || single_succ_edge (ENTRY_BLOCK_PTR)->insns.r)
1415 commit_edge_insertions ();
1417 /* Redirect all EH edges from the post_landing_pad to the landing pad. */
1418 FOR_EACH_BB (bb)
1420 eh_landing_pad lp;
1421 edge_iterator ei;
1422 edge e;
1424 lp = get_eh_landing_pad_from_rtx (BB_END (bb));
1426 FOR_EACH_EDGE (e, ei, bb->succs)
1427 if (e->flags & EDGE_EH)
1428 break;
1430 /* We should not have generated any new throwing insns during this
1431 pass, and we should not have lost any EH edges, so we only need
1432 to handle two cases here:
1433 (1) reachable handler and an existing edge to post-landing-pad,
1434 (2) no reachable handler and no edge. */
1435 gcc_assert ((lp != NULL) == (e != NULL));
1436 if (lp != NULL)
1438 gcc_assert (BB_HEAD (e->dest) == label_rtx (lp->post_landing_pad));
1440 redirect_edge_succ (e, BLOCK_FOR_INSN (lp->landing_pad));
1441 e->flags |= (CALL_P (BB_END (bb))
1442 ? EDGE_ABNORMAL | EDGE_ABNORMAL_CALL
1443 : EDGE_ABNORMAL);
1448 static bool
1449 gate_handle_eh (void)
1451 /* Nothing to do if no regions created. */
1452 return cfun->eh->region_tree != NULL;
1455 /* Complete generation of exception handling code. */
1456 static unsigned int
1457 rest_of_handle_eh (void)
1459 finish_eh_generation ();
1460 cleanup_cfg (CLEANUP_NO_INSN_DEL);
1461 return 0;
1464 struct rtl_opt_pass pass_rtl_eh =
1467 RTL_PASS,
1468 "rtl eh", /* name */
1469 gate_handle_eh, /* gate */
1470 rest_of_handle_eh, /* execute */
1471 NULL, /* sub */
1472 NULL, /* next */
1473 0, /* static_pass_number */
1474 TV_JUMP, /* tv_id */
1475 0, /* properties_required */
1476 0, /* properties_provided */
1477 0, /* properties_destroyed */
1478 0, /* todo_flags_start */
1479 TODO_dump_func /* todo_flags_finish */
1483 /* This section handles removing dead code for flow. */
1485 void
1486 remove_eh_landing_pad (eh_landing_pad lp)
1488 eh_landing_pad *pp;
1490 for (pp = &lp->region->landing_pads; *pp != lp; pp = &(*pp)->next_lp)
1491 continue;
1492 *pp = lp->next_lp;
1494 if (lp->post_landing_pad)
1495 EH_LANDING_PAD_NR (lp->post_landing_pad) = 0;
1496 VEC_replace (eh_landing_pad, cfun->eh->lp_array, lp->index, NULL);
1499 /* Splice REGION from the region tree. */
1501 void
1502 remove_eh_handler (eh_region region)
1504 eh_region *pp, *pp_start, p, outer;
1505 eh_landing_pad lp;
1507 for (lp = region->landing_pads; lp ; lp = lp->next_lp)
1509 if (lp->post_landing_pad)
1510 EH_LANDING_PAD_NR (lp->post_landing_pad) = 0;
1511 VEC_replace (eh_landing_pad, cfun->eh->lp_array, lp->index, NULL);
1514 outer = region->outer;
1515 if (outer)
1516 pp_start = &outer->inner;
1517 else
1518 pp_start = &cfun->eh->region_tree;
1519 for (pp = pp_start, p = *pp; p != region; pp = &p->next_peer, p = *pp)
1520 continue;
1521 if (region->inner)
1523 *pp = p = region->inner;
1526 p->outer = outer;
1527 pp = &p->next_peer;
1528 p = *pp;
1530 while (p);
1532 *pp = region->next_peer;
1534 VEC_replace (eh_region, cfun->eh->region_array, region->index, NULL);
1537 /* Invokes CALLBACK for every exception handler landing pad label.
1538 Only used by reload hackery; should not be used by new code. */
1540 void
1541 for_each_eh_label (void (*callback) (rtx))
1543 eh_landing_pad lp;
1544 int i;
1546 for (i = 1; VEC_iterate (eh_landing_pad, cfun->eh->lp_array, i, lp); ++i)
1548 if (lp)
1550 rtx lab = lp->landing_pad;
1551 if (lab && LABEL_P (lab))
1552 (*callback) (lab);
1557 /* Create the REG_EH_REGION note for INSN, given its ECF_FLAGS for a
1558 call insn.
1560 At the gimple level, we use LP_NR
1561 > 0 : The statement transfers to landing pad LP_NR
1562 = 0 : The statement is outside any EH region
1563 < 0 : The statement is within MUST_NOT_THROW region -LP_NR.
1565 At the rtl level, we use LP_NR
1566 > 0 : The insn transfers to landing pad LP_NR
1567 = 0 : The insn cannot throw
1568 < 0 : The insn is within MUST_NOT_THROW region -LP_NR
1569 = INT_MIN : The insn cannot throw or execute a nonlocal-goto.
1570 missing note: The insn is outside any EH region.
1572 ??? This difference probably ought to be avoided. We could stand
1573 to record nothrow for arbitrary gimple statements, and so avoid
1574 some moderately complex lookups in stmt_could_throw_p. Perhaps
1575 NOTHROW should be mapped on both sides to INT_MIN. Perhaps the
1576 no-nonlocal-goto property should be recorded elsewhere as a bit
1577 on the call_insn directly. Perhaps we should make more use of
1578 attaching the trees to call_insns (reachable via symbol_ref in
1579 direct call cases) and just pull the data out of the trees. */
1581 void
1582 make_reg_eh_region_note (rtx insn, int ecf_flags, int lp_nr)
1584 rtx value;
1585 if (ecf_flags & ECF_NOTHROW)
1586 value = const0_rtx;
1587 else if (lp_nr != 0)
1588 value = GEN_INT (lp_nr);
1589 else
1590 return;
1591 add_reg_note (insn, REG_EH_REGION, value);
1594 /* Create a REG_EH_REGION note for a CALL_INSN that cannot throw
1595 nor perform a non-local goto. Replace the region note if it
1596 already exists. */
1598 void
1599 make_reg_eh_region_note_nothrow_nononlocal (rtx insn)
1601 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1602 rtx intmin = GEN_INT (INT_MIN);
1604 if (note != 0)
1605 XEXP (note, 0) = intmin;
1606 else
1607 add_reg_note (insn, REG_EH_REGION, intmin);
1610 /* Return true if INSN could throw, assuming no REG_EH_REGION note
1611 to the contrary. */
1613 bool
1614 insn_could_throw_p (const_rtx insn)
1616 if (CALL_P (insn))
1617 return true;
1618 if (INSN_P (insn) && flag_non_call_exceptions)
1619 return may_trap_p (PATTERN (insn));
1620 return false;
1623 /* Copy an REG_EH_REGION note to each insn that might throw beginning
1624 at FIRST and ending at LAST. NOTE_OR_INSN is either the source insn
1625 to look for a note, or the note itself. */
1627 void
1628 copy_reg_eh_region_note_forward (rtx note_or_insn, rtx first, rtx last)
1630 rtx insn, note = note_or_insn;
1632 if (INSN_P (note_or_insn))
1634 note = find_reg_note (note_or_insn, REG_EH_REGION, NULL_RTX);
1635 if (note == NULL)
1636 return;
1638 note = XEXP (note, 0);
1640 for (insn = first; insn != last ; insn = NEXT_INSN (insn))
1641 if (!find_reg_note (insn, REG_EH_REGION, NULL_RTX)
1642 && insn_could_throw_p (insn))
1643 add_reg_note (insn, REG_EH_REGION, note);
1646 /* Likewise, but iterate backward. */
1648 void
1649 copy_reg_eh_region_note_backward (rtx note_or_insn, rtx last, rtx first)
1651 rtx insn, note = note_or_insn;
1653 if (INSN_P (note_or_insn))
1655 note = find_reg_note (note_or_insn, REG_EH_REGION, NULL_RTX);
1656 if (note == NULL)
1657 return;
1659 note = XEXP (note, 0);
1661 for (insn = last; insn != first; insn = PREV_INSN (insn))
1662 if (insn_could_throw_p (insn))
1663 add_reg_note (insn, REG_EH_REGION, note);
1667 /* Extract all EH information from INSN. Return true if the insn
1668 was marked NOTHROW. */
1670 static bool
1671 get_eh_region_and_lp_from_rtx (const_rtx insn, eh_region *pr,
1672 eh_landing_pad *plp)
1674 eh_landing_pad lp = NULL;
1675 eh_region r = NULL;
1676 bool ret = false;
1677 rtx note;
1678 int lp_nr;
1680 if (! INSN_P (insn))
1681 goto egress;
1683 if (NONJUMP_INSN_P (insn)
1684 && GET_CODE (PATTERN (insn)) == SEQUENCE)
1685 insn = XVECEXP (PATTERN (insn), 0, 0);
1687 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1688 if (!note)
1690 ret = !insn_could_throw_p (insn);
1691 goto egress;
1694 lp_nr = INTVAL (XEXP (note, 0));
1695 if (lp_nr == 0 || lp_nr == INT_MIN)
1697 ret = true;
1698 goto egress;
1701 if (lp_nr < 0)
1702 r = VEC_index (eh_region, cfun->eh->region_array, -lp_nr);
1703 else
1705 lp = VEC_index (eh_landing_pad, cfun->eh->lp_array, lp_nr);
1706 r = lp->region;
1709 egress:
1710 *plp = lp;
1711 *pr = r;
1712 return ret;
1715 /* Return the landing pad to which INSN may go, or NULL if it does not
1716 have a reachable landing pad within this function. */
1718 eh_landing_pad
1719 get_eh_landing_pad_from_rtx (const_rtx insn)
1721 eh_landing_pad lp;
1722 eh_region r;
1724 get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1725 return lp;
1728 /* Return the region to which INSN may go, or NULL if it does not
1729 have a reachable region within this function. */
1731 eh_region
1732 get_eh_region_from_rtx (const_rtx insn)
1734 eh_landing_pad lp;
1735 eh_region r;
1737 get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1738 return r;
1741 /* Return true if INSN throws and is caught by something in this function. */
1743 bool
1744 can_throw_internal (const_rtx insn)
1746 return get_eh_landing_pad_from_rtx (insn) != NULL;
1749 /* Return true if INSN throws and escapes from the current function. */
1751 bool
1752 can_throw_external (const_rtx insn)
1754 eh_landing_pad lp;
1755 eh_region r;
1756 bool nothrow;
1758 if (! INSN_P (insn))
1759 return false;
1761 if (NONJUMP_INSN_P (insn)
1762 && GET_CODE (PATTERN (insn)) == SEQUENCE)
1764 rtx seq = PATTERN (insn);
1765 int i, n = XVECLEN (seq, 0);
1767 for (i = 0; i < n; i++)
1768 if (can_throw_external (XVECEXP (seq, 0, i)))
1769 return true;
1771 return false;
1774 nothrow = get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1776 /* If we can't throw, we obviously can't throw external. */
1777 if (nothrow)
1778 return false;
1780 /* If we have an internal landing pad, then we're not external. */
1781 if (lp != NULL)
1782 return false;
1784 /* If we're not within an EH region, then we are external. */
1785 if (r == NULL)
1786 return true;
1788 /* The only thing that ought to be left is MUST_NOT_THROW regions,
1789 which don't always have landing pads. */
1790 gcc_assert (r->type == ERT_MUST_NOT_THROW);
1791 return false;
1794 /* Return true if INSN cannot throw at all. */
1796 bool
1797 insn_nothrow_p (const_rtx insn)
1799 eh_landing_pad lp;
1800 eh_region r;
1802 if (! INSN_P (insn))
1803 return true;
1805 if (NONJUMP_INSN_P (insn)
1806 && GET_CODE (PATTERN (insn)) == SEQUENCE)
1808 rtx seq = PATTERN (insn);
1809 int i, n = XVECLEN (seq, 0);
1811 for (i = 0; i < n; i++)
1812 if (!insn_nothrow_p (XVECEXP (seq, 0, i)))
1813 return false;
1815 return true;
1818 return get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1821 /* Return true if INSN can perform a non-local goto. */
1822 /* ??? This test is here in this file because it (ab)uses REG_EH_REGION. */
1824 bool
1825 can_nonlocal_goto (const_rtx insn)
1827 if (nonlocal_goto_handler_labels && CALL_P (insn))
1829 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1830 if (!note || INTVAL (XEXP (note, 0)) != INT_MIN)
1831 return true;
1833 return false;
1836 /* Set TREE_NOTHROW and crtl->all_throwers_are_sibcalls. */
1838 unsigned int
1839 set_nothrow_function_flags (void)
1841 rtx insn;
1843 crtl->nothrow = 1;
1845 /* Assume crtl->all_throwers_are_sibcalls until we encounter
1846 something that can throw an exception. We specifically exempt
1847 CALL_INSNs that are SIBLING_CALL_P, as these are really jumps,
1848 and can't throw. Most CALL_INSNs are not SIBLING_CALL_P, so this
1849 is optimistic. */
1851 crtl->all_throwers_are_sibcalls = 1;
1853 /* If we don't know that this implementation of the function will
1854 actually be used, then we must not set TREE_NOTHROW, since
1855 callers must not assume that this function does not throw. */
1856 if (TREE_NOTHROW (current_function_decl))
1857 return 0;
1859 if (! flag_exceptions)
1860 return 0;
1862 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1863 if (can_throw_external (insn))
1865 crtl->nothrow = 0;
1867 if (!CALL_P (insn) || !SIBLING_CALL_P (insn))
1869 crtl->all_throwers_are_sibcalls = 0;
1870 return 0;
1874 for (insn = crtl->epilogue_delay_list; insn;
1875 insn = XEXP (insn, 1))
1876 if (can_throw_external (insn))
1878 crtl->nothrow = 0;
1880 if (!CALL_P (insn) || !SIBLING_CALL_P (insn))
1882 crtl->all_throwers_are_sibcalls = 0;
1883 return 0;
1886 if (crtl->nothrow
1887 && (cgraph_function_body_availability (cgraph_node
1888 (current_function_decl))
1889 >= AVAIL_AVAILABLE))
1891 struct cgraph_node *node = cgraph_node (current_function_decl);
1892 struct cgraph_edge *e;
1893 for (e = node->callers; e; e = e->next_caller)
1894 e->can_throw_external = false;
1895 TREE_NOTHROW (current_function_decl) = 1;
1897 if (dump_file)
1898 fprintf (dump_file, "Marking function nothrow: %s\n\n",
1899 current_function_name ());
1901 return 0;
1904 struct rtl_opt_pass pass_set_nothrow_function_flags =
1907 RTL_PASS,
1908 "nothrow", /* name */
1909 NULL, /* gate */
1910 set_nothrow_function_flags, /* execute */
1911 NULL, /* sub */
1912 NULL, /* next */
1913 0, /* static_pass_number */
1914 TV_NONE, /* tv_id */
1915 0, /* properties_required */
1916 0, /* properties_provided */
1917 0, /* properties_destroyed */
1918 0, /* todo_flags_start */
1919 TODO_dump_func, /* todo_flags_finish */
1924 /* Various hooks for unwind library. */
1926 /* Expand the EH support builtin functions:
1927 __builtin_eh_pointer and __builtin_eh_filter. */
1929 static eh_region
1930 expand_builtin_eh_common (tree region_nr_t)
1932 HOST_WIDE_INT region_nr;
1933 eh_region region;
1935 gcc_assert (host_integerp (region_nr_t, 0));
1936 region_nr = tree_low_cst (region_nr_t, 0);
1938 region = VEC_index (eh_region, cfun->eh->region_array, region_nr);
1940 /* ??? We shouldn't have been able to delete a eh region without
1941 deleting all the code that depended on it. */
1942 gcc_assert (region != NULL);
1944 return region;
1947 /* Expand to the exc_ptr value from the given eh region. */
1950 expand_builtin_eh_pointer (tree exp)
1952 eh_region region
1953 = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 0));
1954 if (region->exc_ptr_reg == NULL)
1955 region->exc_ptr_reg = gen_reg_rtx (ptr_mode);
1956 return region->exc_ptr_reg;
1959 /* Expand to the filter value from the given eh region. */
1962 expand_builtin_eh_filter (tree exp)
1964 eh_region region
1965 = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 0));
1966 if (region->filter_reg == NULL)
1967 region->filter_reg = gen_reg_rtx (targetm.eh_return_filter_mode ());
1968 return region->filter_reg;
1971 /* Copy the exc_ptr and filter values from one landing pad's registers
1972 to another. This is used to inline the resx statement. */
1975 expand_builtin_eh_copy_values (tree exp)
1977 eh_region dst
1978 = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 0));
1979 eh_region src
1980 = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 1));
1981 enum machine_mode fmode = targetm.eh_return_filter_mode ();
1983 if (dst->exc_ptr_reg == NULL)
1984 dst->exc_ptr_reg = gen_reg_rtx (ptr_mode);
1985 if (src->exc_ptr_reg == NULL)
1986 src->exc_ptr_reg = gen_reg_rtx (ptr_mode);
1988 if (dst->filter_reg == NULL)
1989 dst->filter_reg = gen_reg_rtx (fmode);
1990 if (src->filter_reg == NULL)
1991 src->filter_reg = gen_reg_rtx (fmode);
1993 emit_move_insn (dst->exc_ptr_reg, src->exc_ptr_reg);
1994 emit_move_insn (dst->filter_reg, src->filter_reg);
1996 return const0_rtx;
1999 /* Do any necessary initialization to access arbitrary stack frames.
2000 On the SPARC, this means flushing the register windows. */
2002 void
2003 expand_builtin_unwind_init (void)
2005 /* Set this so all the registers get saved in our frame; we need to be
2006 able to copy the saved values for any registers from frames we unwind. */
2007 crtl->saves_all_registers = 1;
2009 #ifdef SETUP_FRAME_ADDRESSES
2010 SETUP_FRAME_ADDRESSES ();
2011 #endif
2014 /* Map a non-negative number to an eh return data register number; expands
2015 to -1 if no return data register is associated with the input number.
2016 At least the inputs 0 and 1 must be mapped; the target may provide more. */
2019 expand_builtin_eh_return_data_regno (tree exp)
2021 tree which = CALL_EXPR_ARG (exp, 0);
2022 unsigned HOST_WIDE_INT iwhich;
2024 if (TREE_CODE (which) != INTEGER_CST)
2026 error ("argument of %<__builtin_eh_return_regno%> must be constant");
2027 return constm1_rtx;
2030 iwhich = tree_low_cst (which, 1);
2031 iwhich = EH_RETURN_DATA_REGNO (iwhich);
2032 if (iwhich == INVALID_REGNUM)
2033 return constm1_rtx;
2035 #ifdef DWARF_FRAME_REGNUM
2036 iwhich = DWARF_FRAME_REGNUM (iwhich);
2037 #else
2038 iwhich = DBX_REGISTER_NUMBER (iwhich);
2039 #endif
2041 return GEN_INT (iwhich);
2044 /* Given a value extracted from the return address register or stack slot,
2045 return the actual address encoded in that value. */
2048 expand_builtin_extract_return_addr (tree addr_tree)
2050 rtx addr = expand_expr (addr_tree, NULL_RTX, Pmode, EXPAND_NORMAL);
2052 if (GET_MODE (addr) != Pmode
2053 && GET_MODE (addr) != VOIDmode)
2055 #ifdef POINTERS_EXTEND_UNSIGNED
2056 addr = convert_memory_address (Pmode, addr);
2057 #else
2058 addr = convert_to_mode (Pmode, addr, 0);
2059 #endif
2062 /* First mask out any unwanted bits. */
2063 #ifdef MASK_RETURN_ADDR
2064 expand_and (Pmode, addr, MASK_RETURN_ADDR, addr);
2065 #endif
2067 /* Then adjust to find the real return address. */
2068 #if defined (RETURN_ADDR_OFFSET)
2069 addr = plus_constant (addr, RETURN_ADDR_OFFSET);
2070 #endif
2072 return addr;
2075 /* Given an actual address in addr_tree, do any necessary encoding
2076 and return the value to be stored in the return address register or
2077 stack slot so the epilogue will return to that address. */
2080 expand_builtin_frob_return_addr (tree addr_tree)
2082 rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, EXPAND_NORMAL);
2084 addr = convert_memory_address (Pmode, addr);
2086 #ifdef RETURN_ADDR_OFFSET
2087 addr = force_reg (Pmode, addr);
2088 addr = plus_constant (addr, -RETURN_ADDR_OFFSET);
2089 #endif
2091 return addr;
2094 /* Set up the epilogue with the magic bits we'll need to return to the
2095 exception handler. */
2097 void
2098 expand_builtin_eh_return (tree stackadj_tree ATTRIBUTE_UNUSED,
2099 tree handler_tree)
2101 rtx tmp;
2103 #ifdef EH_RETURN_STACKADJ_RTX
2104 tmp = expand_expr (stackadj_tree, crtl->eh.ehr_stackadj,
2105 VOIDmode, EXPAND_NORMAL);
2106 tmp = convert_memory_address (Pmode, tmp);
2107 if (!crtl->eh.ehr_stackadj)
2108 crtl->eh.ehr_stackadj = copy_to_reg (tmp);
2109 else if (tmp != crtl->eh.ehr_stackadj)
2110 emit_move_insn (crtl->eh.ehr_stackadj, tmp);
2111 #endif
2113 tmp = expand_expr (handler_tree, crtl->eh.ehr_handler,
2114 VOIDmode, EXPAND_NORMAL);
2115 tmp = convert_memory_address (Pmode, tmp);
2116 if (!crtl->eh.ehr_handler)
2117 crtl->eh.ehr_handler = copy_to_reg (tmp);
2118 else if (tmp != crtl->eh.ehr_handler)
2119 emit_move_insn (crtl->eh.ehr_handler, tmp);
2121 if (!crtl->eh.ehr_label)
2122 crtl->eh.ehr_label = gen_label_rtx ();
2123 emit_jump (crtl->eh.ehr_label);
2126 /* Expand __builtin_eh_return. This exit path from the function loads up
2127 the eh return data registers, adjusts the stack, and branches to a
2128 given PC other than the normal return address. */
2130 void
2131 expand_eh_return (void)
2133 rtx around_label;
2135 if (! crtl->eh.ehr_label)
2136 return;
2138 crtl->calls_eh_return = 1;
2140 #ifdef EH_RETURN_STACKADJ_RTX
2141 emit_move_insn (EH_RETURN_STACKADJ_RTX, const0_rtx);
2142 #endif
2144 around_label = gen_label_rtx ();
2145 emit_jump (around_label);
2147 emit_label (crtl->eh.ehr_label);
2148 clobber_return_register ();
2150 #ifdef EH_RETURN_STACKADJ_RTX
2151 emit_move_insn (EH_RETURN_STACKADJ_RTX, crtl->eh.ehr_stackadj);
2152 #endif
2154 #ifdef HAVE_eh_return
2155 if (HAVE_eh_return)
2156 emit_insn (gen_eh_return (crtl->eh.ehr_handler));
2157 else
2158 #endif
2160 #ifdef EH_RETURN_HANDLER_RTX
2161 emit_move_insn (EH_RETURN_HANDLER_RTX, crtl->eh.ehr_handler);
2162 #else
2163 error ("__builtin_eh_return not supported on this target");
2164 #endif
2167 emit_label (around_label);
2170 /* Convert a ptr_mode address ADDR_TREE to a Pmode address controlled by
2171 POINTERS_EXTEND_UNSIGNED and return it. */
2174 expand_builtin_extend_pointer (tree addr_tree)
2176 rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, EXPAND_NORMAL);
2177 int extend;
2179 #ifdef POINTERS_EXTEND_UNSIGNED
2180 extend = POINTERS_EXTEND_UNSIGNED;
2181 #else
2182 /* The previous EH code did an unsigned extend by default, so we do this also
2183 for consistency. */
2184 extend = 1;
2185 #endif
2187 return convert_modes (targetm.unwind_word_mode (), ptr_mode, addr, extend);
2190 /* In the following functions, we represent entries in the action table
2191 as 1-based indices. Special cases are:
2193 0: null action record, non-null landing pad; implies cleanups
2194 -1: null action record, null landing pad; implies no action
2195 -2: no call-site entry; implies must_not_throw
2196 -3: we have yet to process outer regions
2198 Further, no special cases apply to the "next" field of the record.
2199 For next, 0 means end of list. */
2201 struct action_record
2203 int offset;
2204 int filter;
2205 int next;
2208 static int
2209 action_record_eq (const void *pentry, const void *pdata)
2211 const struct action_record *entry = (const struct action_record *) pentry;
2212 const struct action_record *data = (const struct action_record *) pdata;
2213 return entry->filter == data->filter && entry->next == data->next;
2216 static hashval_t
2217 action_record_hash (const void *pentry)
2219 const struct action_record *entry = (const struct action_record *) pentry;
2220 return entry->next * 1009 + entry->filter;
2223 static int
2224 add_action_record (htab_t ar_hash, int filter, int next)
2226 struct action_record **slot, *new_ar, tmp;
2228 tmp.filter = filter;
2229 tmp.next = next;
2230 slot = (struct action_record **) htab_find_slot (ar_hash, &tmp, INSERT);
2232 if ((new_ar = *slot) == NULL)
2234 new_ar = XNEW (struct action_record);
2235 new_ar->offset = VEC_length (uchar, crtl->eh.action_record_data) + 1;
2236 new_ar->filter = filter;
2237 new_ar->next = next;
2238 *slot = new_ar;
2240 /* The filter value goes in untouched. The link to the next
2241 record is a "self-relative" byte offset, or zero to indicate
2242 that there is no next record. So convert the absolute 1 based
2243 indices we've been carrying around into a displacement. */
2245 push_sleb128 (&crtl->eh.action_record_data, filter);
2246 if (next)
2247 next -= VEC_length (uchar, crtl->eh.action_record_data) + 1;
2248 push_sleb128 (&crtl->eh.action_record_data, next);
2251 return new_ar->offset;
2254 static int
2255 collect_one_action_chain (htab_t ar_hash, eh_region region)
2257 int next;
2259 /* If we've reached the top of the region chain, then we have
2260 no actions, and require no landing pad. */
2261 if (region == NULL)
2262 return -1;
2264 switch (region->type)
2266 case ERT_CLEANUP:
2268 eh_region r;
2269 /* A cleanup adds a zero filter to the beginning of the chain, but
2270 there are special cases to look out for. If there are *only*
2271 cleanups along a path, then it compresses to a zero action.
2272 Further, if there are multiple cleanups along a path, we only
2273 need to represent one of them, as that is enough to trigger
2274 entry to the landing pad at runtime. */
2275 next = collect_one_action_chain (ar_hash, region->outer);
2276 if (next <= 0)
2277 return 0;
2278 for (r = region->outer; r ; r = r->outer)
2279 if (r->type == ERT_CLEANUP)
2280 return next;
2281 return add_action_record (ar_hash, 0, next);
2284 case ERT_TRY:
2286 eh_catch c;
2288 /* Process the associated catch regions in reverse order.
2289 If there's a catch-all handler, then we don't need to
2290 search outer regions. Use a magic -3 value to record
2291 that we haven't done the outer search. */
2292 next = -3;
2293 for (c = region->u.eh_try.last_catch; c ; c = c->prev_catch)
2295 if (c->type_list == NULL)
2297 /* Retrieve the filter from the head of the filter list
2298 where we have stored it (see assign_filter_values). */
2299 int filter = TREE_INT_CST_LOW (TREE_VALUE (c->filter_list));
2300 next = add_action_record (ar_hash, filter, 0);
2302 else
2304 /* Once the outer search is done, trigger an action record for
2305 each filter we have. */
2306 tree flt_node;
2308 if (next == -3)
2310 next = collect_one_action_chain (ar_hash, region->outer);
2312 /* If there is no next action, terminate the chain. */
2313 if (next == -1)
2314 next = 0;
2315 /* If all outer actions are cleanups or must_not_throw,
2316 we'll have no action record for it, since we had wanted
2317 to encode these states in the call-site record directly.
2318 Add a cleanup action to the chain to catch these. */
2319 else if (next <= 0)
2320 next = add_action_record (ar_hash, 0, 0);
2323 flt_node = c->filter_list;
2324 for (; flt_node; flt_node = TREE_CHAIN (flt_node))
2326 int filter = TREE_INT_CST_LOW (TREE_VALUE (flt_node));
2327 next = add_action_record (ar_hash, filter, next);
2331 return next;
2334 case ERT_ALLOWED_EXCEPTIONS:
2335 /* An exception specification adds its filter to the
2336 beginning of the chain. */
2337 next = collect_one_action_chain (ar_hash, region->outer);
2339 /* If there is no next action, terminate the chain. */
2340 if (next == -1)
2341 next = 0;
2342 /* If all outer actions are cleanups or must_not_throw,
2343 we'll have no action record for it, since we had wanted
2344 to encode these states in the call-site record directly.
2345 Add a cleanup action to the chain to catch these. */
2346 else if (next <= 0)
2347 next = add_action_record (ar_hash, 0, 0);
2349 return add_action_record (ar_hash, region->u.allowed.filter, next);
2351 case ERT_MUST_NOT_THROW:
2352 /* A must-not-throw region with no inner handlers or cleanups
2353 requires no call-site entry. Note that this differs from
2354 the no handler or cleanup case in that we do require an lsda
2355 to be generated. Return a magic -2 value to record this. */
2356 return -2;
2359 gcc_unreachable ();
2362 static int
2363 add_call_site (rtx landing_pad, int action, int section)
2365 call_site_record record;
2367 record = GGC_NEW (struct call_site_record_d);
2368 record->landing_pad = landing_pad;
2369 record->action = action;
2371 VEC_safe_push (call_site_record, gc,
2372 crtl->eh.call_site_record[section], record);
2374 return call_site_base + VEC_length (call_site_record,
2375 crtl->eh.call_site_record[section]) - 1;
2378 /* Turn REG_EH_REGION notes back into NOTE_INSN_EH_REGION notes.
2379 The new note numbers will not refer to region numbers, but
2380 instead to call site entries. */
2382 static unsigned int
2383 convert_to_eh_region_ranges (void)
2385 rtx insn, iter, note;
2386 htab_t ar_hash;
2387 int last_action = -3;
2388 rtx last_action_insn = NULL_RTX;
2389 rtx last_landing_pad = NULL_RTX;
2390 rtx first_no_action_insn = NULL_RTX;
2391 int call_site = 0;
2392 int cur_sec = 0;
2393 rtx section_switch_note = NULL_RTX;
2394 rtx first_no_action_insn_before_switch = NULL_RTX;
2395 rtx last_no_action_insn_before_switch = NULL_RTX;
2396 rtx *pad_map = NULL;
2397 sbitmap pad_loc = NULL;
2398 int min_labelno = 0, max_labelno = 0;
2399 int saved_call_site_base = call_site_base;
2401 crtl->eh.action_record_data = VEC_alloc (uchar, gc, 64);
2403 ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
2405 for (iter = get_insns (); iter ; iter = NEXT_INSN (iter))
2406 if (INSN_P (iter))
2408 eh_landing_pad lp;
2409 eh_region region;
2410 bool nothrow;
2411 int this_action;
2412 rtx this_landing_pad;
2414 insn = iter;
2415 if (NONJUMP_INSN_P (insn)
2416 && GET_CODE (PATTERN (insn)) == SEQUENCE)
2417 insn = XVECEXP (PATTERN (insn), 0, 0);
2419 nothrow = get_eh_region_and_lp_from_rtx (insn, &region, &lp);
2420 if (nothrow)
2421 continue;
2422 if (region)
2423 this_action = collect_one_action_chain (ar_hash, region);
2424 else
2425 this_action = -1;
2427 /* Existence of catch handlers, or must-not-throw regions
2428 implies that an lsda is needed (even if empty). */
2429 if (this_action != -1)
2430 crtl->uses_eh_lsda = 1;
2432 /* Delay creation of region notes for no-action regions
2433 until we're sure that an lsda will be required. */
2434 else if (last_action == -3)
2436 first_no_action_insn = iter;
2437 last_action = -1;
2440 if (this_action >= 0)
2441 this_landing_pad = lp->landing_pad;
2442 else
2443 this_landing_pad = NULL_RTX;
2445 /* Differing actions or landing pads implies a change in call-site
2446 info, which implies some EH_REGION note should be emitted. */
2447 if (last_action != this_action
2448 || last_landing_pad != this_landing_pad)
2450 /* If we'd not seen a previous action (-3) or the previous
2451 action was must-not-throw (-2), then we do not need an
2452 end note. */
2453 if (last_action >= -1)
2455 /* If we delayed the creation of the begin, do it now. */
2456 if (first_no_action_insn_before_switch)
2458 call_site = add_call_site (NULL_RTX, 0, 0);
2459 note
2460 = emit_note_before (NOTE_INSN_EH_REGION_BEG,
2461 first_no_action_insn_before_switch);
2462 NOTE_EH_HANDLER (note) = call_site;
2463 if (first_no_action_insn)
2465 note
2466 = emit_note_after (NOTE_INSN_EH_REGION_END,
2467 last_no_action_insn_before_switch);
2468 NOTE_EH_HANDLER (note) = call_site;
2470 else
2471 gcc_assert (last_action_insn
2472 == last_no_action_insn_before_switch);
2474 if (first_no_action_insn)
2476 call_site = add_call_site (NULL_RTX, 0, cur_sec);
2477 note = emit_note_before (NOTE_INSN_EH_REGION_BEG,
2478 first_no_action_insn);
2479 NOTE_EH_HANDLER (note) = call_site;
2480 first_no_action_insn = NULL_RTX;
2483 note = emit_note_after (NOTE_INSN_EH_REGION_END,
2484 last_action_insn);
2485 NOTE_EH_HANDLER (note) = call_site;
2488 /* If the new action is must-not-throw, then no region notes
2489 are created. */
2490 if (this_action >= -1)
2492 call_site = add_call_site (this_landing_pad,
2493 this_action < 0 ? 0 : this_action,
2494 cur_sec);
2495 note = emit_note_before (NOTE_INSN_EH_REGION_BEG, iter);
2496 NOTE_EH_HANDLER (note) = call_site;
2499 last_action = this_action;
2500 last_landing_pad = this_landing_pad;
2502 last_action_insn = iter;
2504 else if (NOTE_P (iter)
2505 && NOTE_KIND (iter) == NOTE_INSN_SWITCH_TEXT_SECTIONS)
2507 gcc_assert (section_switch_note == NULL_RTX);
2508 gcc_assert (flag_reorder_blocks_and_partition);
2509 section_switch_note = iter;
2510 if (first_no_action_insn)
2512 first_no_action_insn_before_switch = first_no_action_insn;
2513 last_no_action_insn_before_switch = last_action_insn;
2514 first_no_action_insn = NULL_RTX;
2515 gcc_assert (last_action == -1);
2516 last_action = -3;
2518 /* Force closing of current EH region before section switch and
2519 opening a new one afterwards. */
2520 else if (last_action != -3)
2521 last_landing_pad = pc_rtx;
2522 call_site_base += VEC_length (call_site_record,
2523 crtl->eh.call_site_record[cur_sec]);
2524 cur_sec++;
2525 gcc_assert (crtl->eh.call_site_record[cur_sec] == NULL);
2526 crtl->eh.call_site_record[cur_sec]
2527 = VEC_alloc (call_site_record, gc, 10);
2528 max_labelno = max_label_num ();
2529 min_labelno = get_first_label_num ();
2530 pad_map = XCNEWVEC (rtx, max_labelno - min_labelno + 1);
2531 pad_loc = sbitmap_alloc (max_labelno - min_labelno + 1);
2533 else if (LABEL_P (iter) && pad_map)
2534 SET_BIT (pad_loc, CODE_LABEL_NUMBER (iter) - min_labelno);
2536 if (last_action >= -1 && ! first_no_action_insn)
2538 note = emit_note_after (NOTE_INSN_EH_REGION_END, last_action_insn);
2539 NOTE_EH_HANDLER (note) = call_site;
2542 call_site_base = saved_call_site_base;
2544 if (pad_map)
2546 /* When doing hot/cold partitioning, ensure landing pads are
2547 always in the same section as the EH region, .gcc_except_table
2548 can't express it otherwise. */
2549 for (cur_sec = 0; cur_sec < 2; cur_sec++)
2551 int i, idx;
2552 int n = VEC_length (call_site_record,
2553 crtl->eh.call_site_record[cur_sec]);
2554 basic_block prev_bb = NULL, padbb;
2556 for (i = 0; i < n; ++i)
2558 struct call_site_record_d *cs =
2559 VEC_index (call_site_record,
2560 crtl->eh.call_site_record[cur_sec], i);
2561 rtx jump, note;
2563 if (cs->landing_pad == NULL_RTX)
2564 continue;
2565 idx = CODE_LABEL_NUMBER (cs->landing_pad) - min_labelno;
2566 /* If the landing pad is in the correct section, nothing
2567 is needed. */
2568 if (TEST_BIT (pad_loc, idx) ^ (cur_sec == 0))
2569 continue;
2570 /* Otherwise, if we haven't seen this pad yet, we need to
2571 add a new label and jump to the correct section. */
2572 if (pad_map[idx] == NULL_RTX)
2574 pad_map[idx] = gen_label_rtx ();
2575 if (prev_bb == NULL)
2576 for (iter = section_switch_note;
2577 iter; iter = PREV_INSN (iter))
2578 if (NOTE_INSN_BASIC_BLOCK_P (iter))
2580 prev_bb = NOTE_BASIC_BLOCK (iter);
2581 break;
2583 if (cur_sec == 0)
2585 note = emit_label_before (pad_map[idx],
2586 section_switch_note);
2587 jump = emit_jump_insn_before (gen_jump (cs->landing_pad),
2588 section_switch_note);
2590 else
2592 jump = emit_jump_insn_after (gen_jump (cs->landing_pad),
2593 section_switch_note);
2594 note = emit_label_after (pad_map[idx],
2595 section_switch_note);
2597 JUMP_LABEL (jump) = cs->landing_pad;
2598 add_reg_note (jump, REG_CROSSING_JUMP, NULL_RTX);
2599 iter = NEXT_INSN (cs->landing_pad);
2600 if (iter && NOTE_INSN_BASIC_BLOCK_P (iter))
2601 padbb = NOTE_BASIC_BLOCK (iter);
2602 else
2603 padbb = NULL;
2604 if (padbb && prev_bb
2605 && BB_PARTITION (padbb) != BB_UNPARTITIONED)
2607 basic_block bb;
2608 int part
2609 = BB_PARTITION (padbb) == BB_COLD_PARTITION
2610 ? BB_HOT_PARTITION : BB_COLD_PARTITION;
2611 edge_iterator ei;
2612 edge e;
2614 bb = create_basic_block (note, jump, prev_bb);
2615 make_single_succ_edge (bb, padbb, EDGE_CROSSING);
2616 BB_SET_PARTITION (bb, part);
2617 for (ei = ei_start (padbb->preds);
2618 (e = ei_safe_edge (ei)); )
2620 if ((e->flags & (EDGE_EH|EDGE_CROSSING))
2621 == (EDGE_EH|EDGE_CROSSING))
2623 redirect_edge_succ (e, bb);
2624 e->flags &= ~EDGE_CROSSING;
2626 else
2627 ei_next (&ei);
2629 if (cur_sec == 0)
2630 prev_bb = bb;
2633 cs->landing_pad = pad_map[idx];
2637 sbitmap_free (pad_loc);
2638 XDELETEVEC (pad_map);
2641 htab_delete (ar_hash);
2642 return 0;
2645 static bool
2646 gate_convert_to_eh_region_ranges (void)
2648 /* Nothing to do for SJLJ exceptions or if no regions created. */
2649 return !(USING_SJLJ_EXCEPTIONS || cfun->eh->region_tree == NULL);
2652 struct rtl_opt_pass pass_convert_to_eh_region_ranges =
2655 RTL_PASS,
2656 "eh_ranges", /* name */
2657 gate_convert_to_eh_region_ranges, /* gate */
2658 convert_to_eh_region_ranges, /* execute */
2659 NULL, /* sub */
2660 NULL, /* next */
2661 0, /* static_pass_number */
2662 TV_NONE, /* tv_id */
2663 0, /* properties_required */
2664 0, /* properties_provided */
2665 0, /* properties_destroyed */
2666 0, /* todo_flags_start */
2667 TODO_dump_func, /* todo_flags_finish */
2671 static void
2672 push_uleb128 (VEC (uchar, gc) **data_area, unsigned int value)
2676 unsigned char byte = value & 0x7f;
2677 value >>= 7;
2678 if (value)
2679 byte |= 0x80;
2680 VEC_safe_push (uchar, gc, *data_area, byte);
2682 while (value);
2685 static void
2686 push_sleb128 (VEC (uchar, gc) **data_area, int value)
2688 unsigned char byte;
2689 int more;
2693 byte = value & 0x7f;
2694 value >>= 7;
2695 more = ! ((value == 0 && (byte & 0x40) == 0)
2696 || (value == -1 && (byte & 0x40) != 0));
2697 if (more)
2698 byte |= 0x80;
2699 VEC_safe_push (uchar, gc, *data_area, byte);
2701 while (more);
2705 #ifndef HAVE_AS_LEB128
2706 static int
2707 dw2_size_of_call_site_table (int section)
2709 int n = VEC_length (call_site_record, crtl->eh.call_site_record[section]);
2710 int size = n * (4 + 4 + 4);
2711 int i;
2713 for (i = 0; i < n; ++i)
2715 struct call_site_record_d *cs =
2716 VEC_index (call_site_record, crtl->eh.call_site_record[section], i);
2717 size += size_of_uleb128 (cs->action);
2720 return size;
2723 static int
2724 sjlj_size_of_call_site_table (void)
2726 int n = VEC_length (call_site_record, crtl->eh.call_site_record[0]);
2727 int size = 0;
2728 int i;
2730 for (i = 0; i < n; ++i)
2732 struct call_site_record_d *cs =
2733 VEC_index (call_site_record, crtl->eh.call_site_record[0], i);
2734 size += size_of_uleb128 (INTVAL (cs->landing_pad));
2735 size += size_of_uleb128 (cs->action);
2738 return size;
2740 #endif
2742 static void
2743 dw2_output_call_site_table (int cs_format, int section)
2745 int n = VEC_length (call_site_record, crtl->eh.call_site_record[section]);
2746 int i;
2747 const char *begin;
2749 if (section == 0)
2750 begin = current_function_func_begin_label;
2751 else if (first_function_block_is_cold)
2752 begin = crtl->subsections.hot_section_label;
2753 else
2754 begin = crtl->subsections.cold_section_label;
2756 for (i = 0; i < n; ++i)
2758 struct call_site_record_d *cs =
2759 VEC_index (call_site_record, crtl->eh.call_site_record[section], i);
2760 char reg_start_lab[32];
2761 char reg_end_lab[32];
2762 char landing_pad_lab[32];
2764 ASM_GENERATE_INTERNAL_LABEL (reg_start_lab, "LEHB", call_site_base + i);
2765 ASM_GENERATE_INTERNAL_LABEL (reg_end_lab, "LEHE", call_site_base + i);
2767 if (cs->landing_pad)
2768 ASM_GENERATE_INTERNAL_LABEL (landing_pad_lab, "L",
2769 CODE_LABEL_NUMBER (cs->landing_pad));
2771 /* ??? Perhaps use insn length scaling if the assembler supports
2772 generic arithmetic. */
2773 /* ??? Perhaps use attr_length to choose data1 or data2 instead of
2774 data4 if the function is small enough. */
2775 if (cs_format == DW_EH_PE_uleb128)
2777 dw2_asm_output_delta_uleb128 (reg_start_lab, begin,
2778 "region %d start", i);
2779 dw2_asm_output_delta_uleb128 (reg_end_lab, reg_start_lab,
2780 "length");
2781 if (cs->landing_pad)
2782 dw2_asm_output_delta_uleb128 (landing_pad_lab, begin,
2783 "landing pad");
2784 else
2785 dw2_asm_output_data_uleb128 (0, "landing pad");
2787 else
2789 dw2_asm_output_delta (4, reg_start_lab, begin,
2790 "region %d start", i);
2791 dw2_asm_output_delta (4, reg_end_lab, reg_start_lab, "length");
2792 if (cs->landing_pad)
2793 dw2_asm_output_delta (4, landing_pad_lab, begin,
2794 "landing pad");
2795 else
2796 dw2_asm_output_data (4, 0, "landing pad");
2798 dw2_asm_output_data_uleb128 (cs->action, "action");
2801 call_site_base += n;
2804 static void
2805 sjlj_output_call_site_table (void)
2807 int n = VEC_length (call_site_record, crtl->eh.call_site_record[0]);
2808 int i;
2810 for (i = 0; i < n; ++i)
2812 struct call_site_record_d *cs =
2813 VEC_index (call_site_record, crtl->eh.call_site_record[0], i);
2815 dw2_asm_output_data_uleb128 (INTVAL (cs->landing_pad),
2816 "region %d landing pad", i);
2817 dw2_asm_output_data_uleb128 (cs->action, "action");
2820 call_site_base += n;
2823 #ifndef TARGET_UNWIND_INFO
2824 /* Switch to the section that should be used for exception tables. */
2826 static void
2827 switch_to_exception_section (const char * ARG_UNUSED (fnname))
2829 section *s;
2831 if (exception_section)
2832 s = exception_section;
2833 else
2835 /* Compute the section and cache it into exception_section,
2836 unless it depends on the function name. */
2837 if (targetm.have_named_sections)
2839 int flags;
2841 if (EH_TABLES_CAN_BE_READ_ONLY)
2843 int tt_format =
2844 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
2845 flags = ((! flag_pic
2846 || ((tt_format & 0x70) != DW_EH_PE_absptr
2847 && (tt_format & 0x70) != DW_EH_PE_aligned))
2848 ? 0 : SECTION_WRITE);
2850 else
2851 flags = SECTION_WRITE;
2853 #ifdef HAVE_LD_EH_GC_SECTIONS
2854 if (flag_function_sections)
2856 char *section_name = XNEWVEC (char, strlen (fnname) + 32);
2857 sprintf (section_name, ".gcc_except_table.%s", fnname);
2858 s = get_section (section_name, flags, NULL);
2859 free (section_name);
2861 else
2862 #endif
2863 exception_section
2864 = s = get_section (".gcc_except_table", flags, NULL);
2866 else
2867 exception_section
2868 = s = flag_pic ? data_section : readonly_data_section;
2871 switch_to_section (s);
2873 #endif
2876 /* Output a reference from an exception table to the type_info object TYPE.
2877 TT_FORMAT and TT_FORMAT_SIZE describe the DWARF encoding method used for
2878 the value. */
2880 static void
2881 output_ttype (tree type, int tt_format, int tt_format_size)
2883 rtx value;
2884 bool is_public = true;
2886 if (type == NULL_TREE)
2887 value = const0_rtx;
2888 else
2890 struct varpool_node *node;
2892 /* FIXME lto. pass_ipa_free_lang_data changes all types to
2893 runtime types so TYPE should already be a runtime type
2894 reference. When pass_ipa_free_lang data is made a default
2895 pass, we can then remove the call to lookup_type_for_runtime
2896 below. */
2897 if (TYPE_P (type))
2898 type = lookup_type_for_runtime (type);
2900 value = expand_expr (type, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
2902 /* Let cgraph know that the rtti decl is used. Not all of the
2903 paths below go through assemble_integer, which would take
2904 care of this for us. */
2905 STRIP_NOPS (type);
2906 if (TREE_CODE (type) == ADDR_EXPR)
2908 type = TREE_OPERAND (type, 0);
2909 if (TREE_CODE (type) == VAR_DECL)
2911 node = varpool_node (type);
2912 if (node)
2913 varpool_mark_needed_node (node);
2914 is_public = TREE_PUBLIC (type);
2917 else
2918 gcc_assert (TREE_CODE (type) == INTEGER_CST);
2921 /* Allow the target to override the type table entry format. */
2922 if (targetm.asm_out.ttype (value))
2923 return;
2925 if (tt_format == DW_EH_PE_absptr || tt_format == DW_EH_PE_aligned)
2926 assemble_integer (value, tt_format_size,
2927 tt_format_size * BITS_PER_UNIT, 1);
2928 else
2929 dw2_asm_output_encoded_addr_rtx (tt_format, value, is_public, NULL);
2932 static void
2933 output_one_function_exception_table (const char * ARG_UNUSED (fnname),
2934 int section, rtx ARG_UNUSED (personality))
2936 int tt_format, cs_format, lp_format, i;
2937 #ifdef HAVE_AS_LEB128
2938 char ttype_label[32];
2939 char cs_after_size_label[32];
2940 char cs_end_label[32];
2941 #else
2942 int call_site_len;
2943 #endif
2944 int have_tt_data;
2945 int tt_format_size = 0;
2947 #ifdef TARGET_UNWIND_INFO
2948 /* TODO: Move this into target file. */
2949 fputs ("\t.personality\t", asm_out_file);
2950 output_addr_const (asm_out_file, personality);
2951 fputs ("\n\t.handlerdata\n", asm_out_file);
2952 /* Note that varasm still thinks we're in the function's code section.
2953 The ".endp" directive that will immediately follow will take us back. */
2954 #else
2955 switch_to_exception_section (fnname);
2956 #endif
2958 /* If the target wants a label to begin the table, emit it here. */
2959 targetm.asm_out.except_table_label (asm_out_file);
2961 have_tt_data = (VEC_length (tree, cfun->eh->ttype_data)
2962 || (targetm.arm_eabi_unwinder
2963 ? VEC_length (tree, cfun->eh->ehspec_data.arm_eabi)
2964 : VEC_length (uchar, cfun->eh->ehspec_data.other)));
2966 /* Indicate the format of the @TType entries. */
2967 if (! have_tt_data)
2968 tt_format = DW_EH_PE_omit;
2969 else
2971 tt_format = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
2972 #ifdef HAVE_AS_LEB128
2973 ASM_GENERATE_INTERNAL_LABEL (ttype_label,
2974 section ? "LLSDATTC" : "LLSDATT",
2975 current_function_funcdef_no);
2976 #endif
2977 tt_format_size = size_of_encoded_value (tt_format);
2979 assemble_align (tt_format_size * BITS_PER_UNIT);
2982 targetm.asm_out.internal_label (asm_out_file, section ? "LLSDAC" : "LLSDA",
2983 current_function_funcdef_no);
2985 /* The LSDA header. */
2987 /* Indicate the format of the landing pad start pointer. An omitted
2988 field implies @LPStart == @Start. */
2989 /* Currently we always put @LPStart == @Start. This field would
2990 be most useful in moving the landing pads completely out of
2991 line to another section, but it could also be used to minimize
2992 the size of uleb128 landing pad offsets. */
2993 lp_format = DW_EH_PE_omit;
2994 dw2_asm_output_data (1, lp_format, "@LPStart format (%s)",
2995 eh_data_format_name (lp_format));
2997 /* @LPStart pointer would go here. */
2999 dw2_asm_output_data (1, tt_format, "@TType format (%s)",
3000 eh_data_format_name (tt_format));
3002 #ifndef HAVE_AS_LEB128
3003 if (USING_SJLJ_EXCEPTIONS)
3004 call_site_len = sjlj_size_of_call_site_table ();
3005 else
3006 call_site_len = dw2_size_of_call_site_table (section);
3007 #endif
3009 /* A pc-relative 4-byte displacement to the @TType data. */
3010 if (have_tt_data)
3012 #ifdef HAVE_AS_LEB128
3013 char ttype_after_disp_label[32];
3014 ASM_GENERATE_INTERNAL_LABEL (ttype_after_disp_label,
3015 section ? "LLSDATTDC" : "LLSDATTD",
3016 current_function_funcdef_no);
3017 dw2_asm_output_delta_uleb128 (ttype_label, ttype_after_disp_label,
3018 "@TType base offset");
3019 ASM_OUTPUT_LABEL (asm_out_file, ttype_after_disp_label);
3020 #else
3021 /* Ug. Alignment queers things. */
3022 unsigned int before_disp, after_disp, last_disp, disp;
3024 before_disp = 1 + 1;
3025 after_disp = (1 + size_of_uleb128 (call_site_len)
3026 + call_site_len
3027 + VEC_length (uchar, crtl->eh.action_record_data)
3028 + (VEC_length (tree, cfun->eh->ttype_data)
3029 * tt_format_size));
3031 disp = after_disp;
3034 unsigned int disp_size, pad;
3036 last_disp = disp;
3037 disp_size = size_of_uleb128 (disp);
3038 pad = before_disp + disp_size + after_disp;
3039 if (pad % tt_format_size)
3040 pad = tt_format_size - (pad % tt_format_size);
3041 else
3042 pad = 0;
3043 disp = after_disp + pad;
3045 while (disp != last_disp);
3047 dw2_asm_output_data_uleb128 (disp, "@TType base offset");
3048 #endif
3051 /* Indicate the format of the call-site offsets. */
3052 #ifdef HAVE_AS_LEB128
3053 cs_format = DW_EH_PE_uleb128;
3054 #else
3055 cs_format = DW_EH_PE_udata4;
3056 #endif
3057 dw2_asm_output_data (1, cs_format, "call-site format (%s)",
3058 eh_data_format_name (cs_format));
3060 #ifdef HAVE_AS_LEB128
3061 ASM_GENERATE_INTERNAL_LABEL (cs_after_size_label,
3062 section ? "LLSDACSBC" : "LLSDACSB",
3063 current_function_funcdef_no);
3064 ASM_GENERATE_INTERNAL_LABEL (cs_end_label,
3065 section ? "LLSDACSEC" : "LLSDACSE",
3066 current_function_funcdef_no);
3067 dw2_asm_output_delta_uleb128 (cs_end_label, cs_after_size_label,
3068 "Call-site table length");
3069 ASM_OUTPUT_LABEL (asm_out_file, cs_after_size_label);
3070 if (USING_SJLJ_EXCEPTIONS)
3071 sjlj_output_call_site_table ();
3072 else
3073 dw2_output_call_site_table (cs_format, section);
3074 ASM_OUTPUT_LABEL (asm_out_file, cs_end_label);
3075 #else
3076 dw2_asm_output_data_uleb128 (call_site_len, "Call-site table length");
3077 if (USING_SJLJ_EXCEPTIONS)
3078 sjlj_output_call_site_table ();
3079 else
3080 dw2_output_call_site_table (cs_format, section);
3081 #endif
3083 /* ??? Decode and interpret the data for flag_debug_asm. */
3085 uchar uc;
3086 for (i = 0; VEC_iterate (uchar, crtl->eh.action_record_data, i, uc); ++i)
3087 dw2_asm_output_data (1, uc, i ? NULL : "Action record table");
3090 if (have_tt_data)
3091 assemble_align (tt_format_size * BITS_PER_UNIT);
3093 i = VEC_length (tree, cfun->eh->ttype_data);
3094 while (i-- > 0)
3096 tree type = VEC_index (tree, cfun->eh->ttype_data, i);
3097 output_ttype (type, tt_format, tt_format_size);
3100 #ifdef HAVE_AS_LEB128
3101 if (have_tt_data)
3102 ASM_OUTPUT_LABEL (asm_out_file, ttype_label);
3103 #endif
3105 /* ??? Decode and interpret the data for flag_debug_asm. */
3106 if (targetm.arm_eabi_unwinder)
3108 tree type;
3109 for (i = 0;
3110 VEC_iterate (tree, cfun->eh->ehspec_data.arm_eabi, i, type); ++i)
3111 output_ttype (type, tt_format, tt_format_size);
3113 else
3115 uchar uc;
3116 for (i = 0;
3117 VEC_iterate (uchar, cfun->eh->ehspec_data.other, i, uc); ++i)
3118 dw2_asm_output_data (1, uc,
3119 i ? NULL : "Exception specification table");
3123 void
3124 output_function_exception_table (const char * ARG_UNUSED (fnname))
3126 rtx personality = get_personality_function (current_function_decl);
3128 /* Not all functions need anything. */
3129 if (! crtl->uses_eh_lsda)
3130 return;
3132 if (personality)
3133 assemble_external_libcall (personality);
3135 output_one_function_exception_table (fnname, 0, personality);
3136 if (crtl->eh.call_site_record[1] != NULL)
3137 output_one_function_exception_table (fnname, 1, personality);
3139 switch_to_section (current_function_section ());
3142 void
3143 set_eh_throw_stmt_table (struct function *fun, struct htab *table)
3145 fun->eh->throw_stmt_table = table;
3148 htab_t
3149 get_eh_throw_stmt_table (struct function *fun)
3151 return fun->eh->throw_stmt_table;
3154 /* Determine if the function needs an EH personality function. */
3156 enum eh_personality_kind
3157 function_needs_eh_personality (struct function *fn)
3159 enum eh_personality_kind kind = eh_personality_none;
3160 eh_region i;
3162 FOR_ALL_EH_REGION_FN (i, fn)
3164 switch (i->type)
3166 case ERT_CLEANUP:
3167 /* Can do with any personality including the generic C one. */
3168 kind = eh_personality_any;
3169 break;
3171 case ERT_TRY:
3172 case ERT_ALLOWED_EXCEPTIONS:
3173 /* Always needs a EH personality function. The generic C
3174 personality doesn't handle these even for empty type lists. */
3175 return eh_personality_lang;
3177 case ERT_MUST_NOT_THROW:
3178 /* Always needs a EH personality function. The language may specify
3179 what abort routine that must be used, e.g. std::terminate. */
3180 return eh_personality_lang;
3184 return kind;
3187 /* Dump EH information to OUT. */
3189 void
3190 dump_eh_tree (FILE * out, struct function *fun)
3192 eh_region i;
3193 int depth = 0;
3194 static const char *const type_name[] = {
3195 "cleanup", "try", "allowed_exceptions", "must_not_throw"
3198 i = fun->eh->region_tree;
3199 if (!i)
3200 return;
3202 fprintf (out, "Eh tree:\n");
3203 while (1)
3205 fprintf (out, " %*s %i %s", depth * 2, "",
3206 i->index, type_name[(int) i->type]);
3208 if (i->landing_pads)
3210 eh_landing_pad lp;
3212 fprintf (out, " land:");
3213 if (current_ir_type () == IR_GIMPLE)
3215 for (lp = i->landing_pads; lp ; lp = lp->next_lp)
3217 fprintf (out, "{%i,", lp->index);
3218 print_generic_expr (out, lp->post_landing_pad, 0);
3219 fputc ('}', out);
3220 if (lp->next_lp)
3221 fputc (',', out);
3224 else
3226 for (lp = i->landing_pads; lp ; lp = lp->next_lp);
3228 fprintf (out, "{%i,", lp->index);
3229 if (lp->landing_pad)
3230 fprintf (out, "%i%s,", INSN_UID (lp->landing_pad),
3231 NOTE_P (lp->landing_pad) ? "(del)" : "");
3232 else
3233 fprintf (out, "(nil),");
3234 if (lp->post_landing_pad)
3236 rtx lab = label_rtx (lp->post_landing_pad);
3237 fprintf (out, "%i%s}", INSN_UID (lab),
3238 NOTE_P (lab) ? "(del)" : "");
3240 else
3241 fprintf (out, "(nil)}");
3242 if (lp->next_lp)
3243 fputc (',', out);
3248 switch (i->type)
3250 case ERT_CLEANUP:
3251 case ERT_MUST_NOT_THROW:
3252 break;
3254 case ERT_TRY:
3256 eh_catch c;
3257 fprintf (out, " catch:");
3258 for (c = i->u.eh_try.first_catch; c; c = c->next_catch)
3260 fputc ('{', out);
3261 if (c->label)
3263 fprintf (out, "lab:");
3264 print_generic_expr (out, c->label, 0);
3265 fputc (';', out);
3267 print_generic_expr (out, c->type_list, 0);
3268 fputc ('}', out);
3269 if (c->next_catch)
3270 fputc (',', out);
3273 break;
3275 case ERT_ALLOWED_EXCEPTIONS:
3276 fprintf (out, " filter :%i types:", i->u.allowed.filter);
3277 print_generic_expr (out, i->u.allowed.type_list, 0);
3278 break;
3280 fputc ('\n', out);
3282 /* If there are sub-regions, process them. */
3283 if (i->inner)
3284 i = i->inner, depth++;
3285 /* If there are peers, process them. */
3286 else if (i->next_peer)
3287 i = i->next_peer;
3288 /* Otherwise, step back up the tree to the next peer. */
3289 else
3293 i = i->outer;
3294 depth--;
3295 if (i == NULL)
3296 return;
3298 while (i->next_peer == NULL);
3299 i = i->next_peer;
3304 /* Dump the EH tree for FN on stderr. */
3306 void
3307 debug_eh_tree (struct function *fn)
3309 dump_eh_tree (stderr, fn);
3312 /* Verify invariants on EH datastructures. */
3314 void
3315 verify_eh_tree (struct function *fun)
3317 eh_region r, outer;
3318 int nvisited_lp, nvisited_r;
3319 int count_lp, count_r, depth, i;
3320 eh_landing_pad lp;
3321 bool err = false;
3323 if (!fun->eh->region_tree)
3324 return;
3326 count_r = 0;
3327 for (i = 1; VEC_iterate (eh_region, fun->eh->region_array, i, r); ++i)
3328 if (r)
3330 if (r->index == i)
3331 count_r++;
3332 else
3334 error ("region_array is corrupted for region %i", r->index);
3335 err = true;
3339 count_lp = 0;
3340 for (i = 1; VEC_iterate (eh_landing_pad, fun->eh->lp_array, i, lp); ++i)
3341 if (lp)
3343 if (lp->index == i)
3344 count_lp++;
3345 else
3347 error ("lp_array is corrupted for lp %i", lp->index);
3348 err = true;
3352 depth = nvisited_lp = nvisited_r = 0;
3353 outer = NULL;
3354 r = fun->eh->region_tree;
3355 while (1)
3357 if (VEC_index (eh_region, fun->eh->region_array, r->index) != r)
3359 error ("region_array is corrupted for region %i", r->index);
3360 err = true;
3362 if (r->outer != outer)
3364 error ("outer block of region %i is wrong", r->index);
3365 err = true;
3367 if (depth < 0)
3369 error ("negative nesting depth of region %i", r->index);
3370 err = true;
3372 nvisited_r++;
3374 for (lp = r->landing_pads; lp ; lp = lp->next_lp)
3376 if (VEC_index (eh_landing_pad, fun->eh->lp_array, lp->index) != lp)
3378 error ("lp_array is corrupted for lp %i", lp->index);
3379 err = true;
3381 if (lp->region != r)
3383 error ("region of lp %i is wrong", lp->index);
3384 err = true;
3386 nvisited_lp++;
3389 if (r->inner)
3390 outer = r, r = r->inner, depth++;
3391 else if (r->next_peer)
3392 r = r->next_peer;
3393 else
3397 r = r->outer;
3398 if (r == NULL)
3399 goto region_done;
3400 depth--;
3401 outer = r->outer;
3403 while (r->next_peer == NULL);
3404 r = r->next_peer;
3407 region_done:
3408 if (depth != 0)
3410 error ("tree list ends on depth %i", depth);
3411 err = true;
3413 if (count_r != nvisited_r)
3415 error ("region_array does not match region_tree");
3416 err = true;
3418 if (count_lp != nvisited_lp)
3420 error ("lp_array does not match region_tree");
3421 err = true;
3424 if (err)
3426 dump_eh_tree (stderr, fun);
3427 internal_error ("verify_eh_tree failed");
3431 #include "gt-except.h"