]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/except.c
* tree-vrp.c (debug_value_range, debug_all_value_ranges,
[thirdparty/gcc.git] / gcc / except.c
CommitLineData
97bb6c17 1/* Implements exception handling.
1ed5443b 2 Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
d78f9811 3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
8c4c00c1 4 Free Software Foundation, Inc.
97ecdf3e 5 Contributed by Mike Stump <mrs@cygnus.com>.
6
f12b58b3 7This file is part of GCC.
97ecdf3e 8
f12b58b3 9GCC is free software; you can redistribute it and/or modify it under
10the terms of the GNU General Public License as published by the Free
8c4c00c1 11Software Foundation; either version 3, or (at your option) any later
f12b58b3 12version.
97ecdf3e 13
f12b58b3 14GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15WARRANTY; without even the implied warranty of MERCHANTABILITY or
16FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17for more details.
97ecdf3e 18
19You should have received a copy of the GNU General Public License
8c4c00c1 20along with GCC; see the file COPYING3. If not see
21<http://www.gnu.org/licenses/>. */
97ecdf3e 22
23
e38def9c 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.
27
28 The representation of exceptions changes several times during
29 the compilation process:
30
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.
34
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.
40
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
48e1416a 51 throw does happen. We also create RESX statements that are
e38def9c 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.
57
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.
67
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).
48e1416a 78
e38def9c 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.
83
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.
94
95 During pass_convert_to_eh_region_ranges (except.c), we transform
48e1416a 96 the REG_EH_REGION notes attached to individual insns into
e38def9c 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.
104
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. */
97ecdf3e 112
113
114#include "config.h"
405711de 115#include "system.h"
805e22b2 116#include "coretypes.h"
117#include "tm.h"
97ecdf3e 118#include "rtl.h"
119#include "tree.h"
120#include "flags.h"
97ecdf3e 121#include "function.h"
97ecdf3e 122#include "expr.h"
d8fc4d0b 123#include "libfuncs.h"
97ecdf3e 124#include "insn-config.h"
df4b504c 125#include "except.h"
126#include "integrate.h"
127#include "hard-reg-set.h"
128#include "basic-block.h"
97ecdf3e 129#include "output.h"
df4b504c 130#include "dwarf2asm.h"
131#include "dwarf2out.h"
bde36f4a 132#include "dwarf2.h"
12874aaf 133#include "toplev.h"
df4b504c 134#include "hashtab.h"
723766b6 135#include "intl.h"
a7b0c170 136#include "ggc.h"
075136a2 137#include "tm_p.h"
02c8b767 138#include "target.h"
a1f71e15 139#include "langhooks.h"
3b919cde 140#include "cgraph.h"
b4ba5e9d 141#include "diagnostic.h"
ce084dfc 142#include "tree-pretty-print.h"
77fce4cd 143#include "tree-pass.h"
144#include "timevar.h"
927a6b6b 145#include "tree-flow.h"
df4b504c 146
147/* Provide defaults for stuff that may not be defined when using
148 sjlj exceptions. */
df4b504c 149#ifndef EH_RETURN_DATA_REGNO
150#define EH_RETURN_DATA_REGNO(N) INVALID_REGNUM
98ad18c5 151#endif
152
df4b504c 153/* Protect cleanup actions with must-not-throw regions, with a call
154 to the given failure handler. */
e38def9c 155tree (*lang_protect_cleanup_actions) (void);
8591d03a 156
252cc619 157static GTY(()) int call_site_base;
1f3233d1 158static GTY ((param_is (union tree_node)))
159 htab_t type_to_runtime_map;
df4b504c 160
161/* Describe the SjLj_Function_Context structure. */
1f3233d1 162static GTY(()) tree sjlj_fc_type_node;
df4b504c 163static int sjlj_fc_call_site_ofs;
164static int sjlj_fc_data_ofs;
165static int sjlj_fc_personality_ofs;
166static int sjlj_fc_lsda_ofs;
167static int sjlj_fc_jbuf_ofs;
168\f
318077fa 169
26dbec0a 170struct GTY(()) call_site_record_d
1f3233d1 171{
172 rtx landing_pad;
173 int action;
174};
df4b504c 175\f
e38def9c 176static bool get_eh_region_and_lp_from_rtx (const_rtx, eh_region *,
177 eh_landing_pad *);
178
35cb5232 179static int t2r_eq (const void *, const void *);
180static hashval_t t2r_hash (const void *);
35cb5232 181
35cb5232 182static int ttypes_filter_eq (const void *, const void *);
183static hashval_t ttypes_filter_hash (const void *);
184static int ehspec_filter_eq (const void *, const void *);
185static hashval_t ehspec_filter_hash (const void *);
186static int add_ttypes_entry (htab_t, tree);
187static int add_ehspec_entry (htab_t, htab_t, tree);
35cb5232 188static void dw2_build_landing_pads (void);
df4b504c 189
35cb5232 190static int action_record_eq (const void *, const void *);
191static hashval_t action_record_hash (const void *);
192static int add_action_record (htab_t, int, int);
e38def9c 193static int collect_one_action_chain (htab_t, eh_region);
33b1ed32 194static int add_call_site (rtx, int, int);
35cb5232 195
e38def9c 196static void push_uleb128 (VEC (uchar, gc) **, unsigned int);
197static void push_sleb128 (VEC (uchar, gc) **, int);
df4b504c 198#ifndef HAVE_AS_LEB128
2142a70c 199static int dw2_size_of_call_site_table (int);
35cb5232 200static int sjlj_size_of_call_site_table (void);
df4b504c 201#endif
33b1ed32 202static void dw2_output_call_site_table (int, int);
35cb5232 203static void sjlj_output_call_site_table (void);
d63ea2f2 204
df4b504c 205\f
206/* Routine to see if exception handling is turned on.
6ef828f9 207 DO_WARN is nonzero if we want to inform the user that exception
1ed5443b 208 handling is turned off.
97ecdf3e 209
df4b504c 210 This is used to ensure that -fexceptions has been specified if the
211 compiler tries to use any exception-specific functions. */
97ecdf3e 212
df4b504c 213int
35cb5232 214doing_eh (int do_warn)
df4b504c 215{
216 if (! flag_exceptions)
217 {
218 static int warned = 0;
219 if (! warned && do_warn)
220 {
221 error ("exception handling disabled, use -fexceptions to enable");
222 warned = 1;
223 }
224 return 0;
225 }
226 return 1;
97ecdf3e 227}
228
df4b504c 229\f
230void
35cb5232 231init_eh (void)
97ecdf3e 232{
df4b504c 233 if (! flag_exceptions)
234 return;
97ecdf3e 235
1f3233d1 236 type_to_runtime_map = htab_create_ggc (31, t2r_hash, t2r_eq, NULL);
97ecdf3e 237
df4b504c 238 /* Create the SjLj_Function_Context structure. This should match
239 the definition in unwind-sjlj.c. */
240 if (USING_SJLJ_EXCEPTIONS)
241 {
242 tree f_jbuf, f_per, f_lsda, f_prev, f_cs, f_data, tmp;
97ecdf3e 243
dc24ddbd 244 sjlj_fc_type_node = lang_hooks.types.make_type (RECORD_TYPE);
011a7f23 245
0b35068b 246 f_prev = build_decl (BUILTINS_LOCATION,
e60a6f7b 247 FIELD_DECL, get_identifier ("__prev"),
df4b504c 248 build_pointer_type (sjlj_fc_type_node));
249 DECL_FIELD_CONTEXT (f_prev) = sjlj_fc_type_node;
011a7f23 250
0b35068b 251 f_cs = build_decl (BUILTINS_LOCATION,
e60a6f7b 252 FIELD_DECL, get_identifier ("__call_site"),
df4b504c 253 integer_type_node);
254 DECL_FIELD_CONTEXT (f_cs) = sjlj_fc_type_node;
97ecdf3e 255
7016c612 256 tmp = build_index_type (build_int_cst (NULL_TREE, 4 - 1));
1bd43494 257 tmp = build_array_type (lang_hooks.types.type_for_mode
258 (targetm.unwind_word_mode (), 1),
771d21fa 259 tmp);
0b35068b 260 f_data = build_decl (BUILTINS_LOCATION,
e60a6f7b 261 FIELD_DECL, get_identifier ("__data"), tmp);
df4b504c 262 DECL_FIELD_CONTEXT (f_data) = sjlj_fc_type_node;
011a7f23 263
0b35068b 264 f_per = build_decl (BUILTINS_LOCATION,
e60a6f7b 265 FIELD_DECL, get_identifier ("__personality"),
df4b504c 266 ptr_type_node);
267 DECL_FIELD_CONTEXT (f_per) = sjlj_fc_type_node;
97ecdf3e 268
0b35068b 269 f_lsda = build_decl (BUILTINS_LOCATION,
e60a6f7b 270 FIELD_DECL, get_identifier ("__lsda"),
df4b504c 271 ptr_type_node);
272 DECL_FIELD_CONTEXT (f_lsda) = sjlj_fc_type_node;
ed74c60e 273
df4b504c 274#ifdef DONT_USE_BUILTIN_SETJMP
275#ifdef JMP_BUF_SIZE
7016c612 276 tmp = build_int_cst (NULL_TREE, JMP_BUF_SIZE - 1);
df4b504c 277#else
278 /* Should be large enough for most systems, if it is not,
279 JMP_BUF_SIZE should be defined with the proper value. It will
280 also tend to be larger than necessary for most systems, a more
281 optimal port will define JMP_BUF_SIZE. */
7016c612 282 tmp = build_int_cst (NULL_TREE, FIRST_PSEUDO_REGISTER + 2 - 1);
df4b504c 283#endif
284#else
5b8192c3 285 /* builtin_setjmp takes a pointer to 5 words. */
7016c612 286 tmp = build_int_cst (NULL_TREE, 5 * BITS_PER_WORD / POINTER_SIZE - 1);
df4b504c 287#endif
288 tmp = build_index_type (tmp);
289 tmp = build_array_type (ptr_type_node, tmp);
0b35068b 290 f_jbuf = build_decl (BUILTINS_LOCATION,
e60a6f7b 291 FIELD_DECL, get_identifier ("__jbuf"), tmp);
df4b504c 292#ifdef DONT_USE_BUILTIN_SETJMP
293 /* We don't know what the alignment requirements of the
294 runtime's jmp_buf has. Overestimate. */
295 DECL_ALIGN (f_jbuf) = BIGGEST_ALIGNMENT;
296 DECL_USER_ALIGN (f_jbuf) = 1;
297#endif
298 DECL_FIELD_CONTEXT (f_jbuf) = sjlj_fc_type_node;
299
300 TYPE_FIELDS (sjlj_fc_type_node) = f_prev;
301 TREE_CHAIN (f_prev) = f_cs;
302 TREE_CHAIN (f_cs) = f_data;
303 TREE_CHAIN (f_data) = f_per;
304 TREE_CHAIN (f_per) = f_lsda;
305 TREE_CHAIN (f_lsda) = f_jbuf;
306
307 layout_type (sjlj_fc_type_node);
308
309 /* Cache the interesting field offsets so that we have
310 easy access from rtl. */
311 sjlj_fc_call_site_ofs
312 = (tree_low_cst (DECL_FIELD_OFFSET (f_cs), 1)
313 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_cs), 1) / BITS_PER_UNIT);
314 sjlj_fc_data_ofs
315 = (tree_low_cst (DECL_FIELD_OFFSET (f_data), 1)
316 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_data), 1) / BITS_PER_UNIT);
317 sjlj_fc_personality_ofs
318 = (tree_low_cst (DECL_FIELD_OFFSET (f_per), 1)
319 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_per), 1) / BITS_PER_UNIT);
320 sjlj_fc_lsda_ofs
321 = (tree_low_cst (DECL_FIELD_OFFSET (f_lsda), 1)
322 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_lsda), 1) / BITS_PER_UNIT);
323 sjlj_fc_jbuf_ofs
324 = (tree_low_cst (DECL_FIELD_OFFSET (f_jbuf), 1)
325 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_jbuf), 1) / BITS_PER_UNIT);
326 }
97ecdf3e 327}
328
df4b504c 329void
35cb5232 330init_eh_for_function (void)
97ecdf3e 331{
2457c754 332 cfun->eh = GGC_CNEW (struct eh_status);
e38def9c 333
334 /* Make sure zero'th entries are used. */
335 VEC_safe_push (eh_region, gc, cfun->eh->region_array, NULL);
336 VEC_safe_push (eh_landing_pad, gc, cfun->eh->lp_array, NULL);
8f8dcce4 337}
df4b504c 338\f
b215c058 339/* Routines to generate the exception tree somewhat directly.
4ee9c684 340 These are used from tree-eh.c when processing exception related
341 nodes during tree optimization. */
342
e38def9c 343static eh_region
344gen_eh_region (enum eh_region_type type, eh_region outer)
4ee9c684 345{
e38def9c 346 eh_region new_eh;
4ee9c684 347
348#ifdef ENABLE_CHECKING
611234b4 349 gcc_assert (doing_eh (0));
4ee9c684 350#endif
351
352 /* Insert a new blank region as a leaf in the tree. */
26dbec0a 353 new_eh = GGC_CNEW (struct eh_region_d);
6659485c 354 new_eh->type = type;
355 new_eh->outer = outer;
4ee9c684 356 if (outer)
357 {
6659485c 358 new_eh->next_peer = outer->inner;
359 outer->inner = new_eh;
4ee9c684 360 }
361 else
362 {
6659485c 363 new_eh->next_peer = cfun->eh->region_tree;
364 cfun->eh->region_tree = new_eh;
4ee9c684 365 }
366
e38def9c 367 new_eh->index = VEC_length (eh_region, cfun->eh->region_array);
368 VEC_safe_push (eh_region, gc, cfun->eh->region_array, new_eh);
4ee9c684 369
471eff36 370 /* Copy the language's notion of whether to use __cxa_end_cleanup. */
371 if (targetm.arm_eabi_unwinder && lang_hooks.eh_use_cxa_end_cleanup)
372 new_eh->use_cxa_end_cleanup = true;
373
6659485c 374 return new_eh;
4ee9c684 375}
376
e38def9c 377eh_region
378gen_eh_region_cleanup (eh_region outer)
4ee9c684 379{
e38def9c 380 return gen_eh_region (ERT_CLEANUP, outer);
4ee9c684 381}
382
e38def9c 383eh_region
384gen_eh_region_try (eh_region outer)
4ee9c684 385{
386 return gen_eh_region (ERT_TRY, outer);
387}
388
e38def9c 389eh_catch
390gen_eh_region_catch (eh_region t, tree type_or_list)
4ee9c684 391{
e38def9c 392 eh_catch c, l;
4ee9c684 393 tree type_list, type_node;
394
e38def9c 395 gcc_assert (t->type == ERT_TRY);
396
4ee9c684 397 /* Ensure to always end up with a type list to normalize further
398 processing, then register each type against the runtime types map. */
399 type_list = type_or_list;
400 if (type_or_list)
401 {
402 if (TREE_CODE (type_or_list) != TREE_LIST)
403 type_list = tree_cons (NULL_TREE, type_or_list, NULL_TREE);
404
405 type_node = type_list;
406 for (; type_node; type_node = TREE_CHAIN (type_node))
407 add_type_for_runtime (TREE_VALUE (type_node));
408 }
409
e38def9c 410 c = GGC_CNEW (struct eh_catch_d);
411 c->type_list = type_list;
f4e36c33 412 l = t->u.eh_try.last_catch;
e38def9c 413 c->prev_catch = l;
4ee9c684 414 if (l)
e38def9c 415 l->next_catch = c;
4ee9c684 416 else
e38def9c 417 t->u.eh_try.first_catch = c;
f4e36c33 418 t->u.eh_try.last_catch = c;
4ee9c684 419
420 return c;
421}
422
e38def9c 423eh_region
424gen_eh_region_allowed (eh_region outer, tree allowed)
4ee9c684 425{
e38def9c 426 eh_region region = gen_eh_region (ERT_ALLOWED_EXCEPTIONS, outer);
4ee9c684 427 region->u.allowed.type_list = allowed;
428
429 for (; allowed ; allowed = TREE_CHAIN (allowed))
430 add_type_for_runtime (TREE_VALUE (allowed));
431
432 return region;
433}
434
e38def9c 435eh_region
436gen_eh_region_must_not_throw (eh_region outer)
4ee9c684 437{
438 return gen_eh_region (ERT_MUST_NOT_THROW, outer);
439}
440
e38def9c 441eh_landing_pad
442gen_eh_landing_pad (eh_region region)
4ee9c684 443{
e38def9c 444 eh_landing_pad lp = GGC_CNEW (struct eh_landing_pad_d);
4ee9c684 445
e38def9c 446 lp->next_lp = region->landing_pads;
447 lp->region = region;
448 lp->index = VEC_length (eh_landing_pad, cfun->eh->lp_array);
449 region->landing_pads = lp;
450
451 VEC_safe_push (eh_landing_pad, gc, cfun->eh->lp_array, lp);
452
453 return lp;
4ee9c684 454}
455
e38def9c 456eh_region
457get_eh_region_from_number_fn (struct function *ifun, int i)
4ee9c684 458{
e38def9c 459 return VEC_index (eh_region, ifun->eh->region_array, i);
4ee9c684 460}
461
e38def9c 462eh_region
463get_eh_region_from_number (int i)
3bd82487 464{
e38def9c 465 return get_eh_region_from_number_fn (cfun, i);
3bd82487 466}
467
e38def9c 468eh_landing_pad
469get_eh_landing_pad_from_number_fn (struct function *ifun, int i)
4ee9c684 470{
e38def9c 471 return VEC_index (eh_landing_pad, ifun->eh->lp_array, i);
4ee9c684 472}
4ee9c684 473
e38def9c 474eh_landing_pad
475get_eh_landing_pad_from_number (int i)
476{
477 return get_eh_landing_pad_from_number_fn (cfun, i);
4ee9c684 478}
479
e38def9c 480eh_region
481get_eh_region_from_lp_number_fn (struct function *ifun, int i)
95cedffb 482{
e38def9c 483 if (i < 0)
484 return VEC_index (eh_region, ifun->eh->region_array, -i);
485 else if (i == 0)
486 return NULL;
487 else
95cedffb 488 {
e38def9c 489 eh_landing_pad lp;
490 lp = VEC_index (eh_landing_pad, ifun->eh->lp_array, i);
491 return lp->region;
95cedffb 492 }
493}
494
e38def9c 495eh_region
496get_eh_region_from_lp_number (int i)
97ecdf3e 497{
e38def9c 498 return get_eh_region_from_lp_number_fn (cfun, i);
df4b504c 499}
e38def9c 500\f
501/* Returns true if the current function has exception handling regions. */
97ecdf3e 502
e38def9c 503bool
504current_function_has_exception_handlers (void)
1639c6db 505{
e38def9c 506 return cfun->eh->region_tree != NULL;
1639c6db 507}
df4b504c 508\f
e38def9c 509/* A subroutine of duplicate_eh_regions. Copy the eh_region tree at OLD.
510 Root it at OUTER, and apply LP_OFFSET to the lp numbers. */
732992fa 511
e38def9c 512struct duplicate_eh_regions_data
732992fa 513{
e38def9c 514 duplicate_eh_regions_map label_map;
515 void *label_map_data;
516 struct pointer_map_t *eh_map;
517};
732992fa 518
e38def9c 519static void
520duplicate_eh_regions_1 (struct duplicate_eh_regions_data *data,
521 eh_region old_r, eh_region outer)
522{
523 eh_landing_pad old_lp, new_lp;
524 eh_region new_r;
525 void **slot;
732992fa 526
e38def9c 527 new_r = gen_eh_region (old_r->type, outer);
528 slot = pointer_map_insert (data->eh_map, (void *)old_r);
529 gcc_assert (*slot == NULL);
530 *slot = (void *)new_r;
732992fa 531
e38def9c 532 switch (old_r->type)
df4b504c 533 {
e38def9c 534 case ERT_CLEANUP:
535 break;
df4b504c 536
e38def9c 537 case ERT_TRY:
538 {
539 eh_catch oc, nc;
540 for (oc = old_r->u.eh_try.first_catch; oc ; oc = oc->next_catch)
541 {
542 /* We should be doing all our region duplication before and
543 during inlining, which is before filter lists are created. */
544 gcc_assert (oc->filter_list == NULL);
545 nc = gen_eh_region_catch (new_r, oc->type_list);
546 nc->label = data->label_map (oc->label, data->label_map_data);
547 }
548 }
549 break;
8591d03a 550
e38def9c 551 case ERT_ALLOWED_EXCEPTIONS:
552 new_r->u.allowed.type_list = old_r->u.allowed.type_list;
c90b5d40 553 if (old_r->u.allowed.label)
554 new_r->u.allowed.label
555 = data->label_map (old_r->u.allowed.label, data->label_map_data);
556 else
557 new_r->u.allowed.label = NULL_TREE;
e38def9c 558 break;
f491db07 559
e38def9c 560 case ERT_MUST_NOT_THROW:
561 new_r->u.must_not_throw = old_r->u.must_not_throw;
562 break;
563 }
4c5fcca6 564
e38def9c 565 for (old_lp = old_r->landing_pads; old_lp ; old_lp = old_lp->next_lp)
f491db07 566 {
e38def9c 567 /* Don't bother copying unused landing pads. */
568 if (old_lp->post_landing_pad == NULL)
569 continue;
4c5fcca6 570
e38def9c 571 new_lp = gen_eh_landing_pad (new_r);
572 slot = pointer_map_insert (data->eh_map, (void *)old_lp);
573 gcc_assert (*slot == NULL);
574 *slot = (void *)new_lp;
575
576 new_lp->post_landing_pad
577 = data->label_map (old_lp->post_landing_pad, data->label_map_data);
578 EH_LANDING_PAD_NR (new_lp->post_landing_pad) = new_lp->index;
f491db07 579 }
e38def9c 580
471eff36 581 /* Make sure to preserve the original use of __cxa_end_cleanup. */
582 new_r->use_cxa_end_cleanup = old_r->use_cxa_end_cleanup;
583
e38def9c 584 for (old_r = old_r->inner; old_r ; old_r = old_r->next_peer)
585 duplicate_eh_regions_1 (data, old_r, new_r);
4c5fcca6 586}
f491db07 587
e38def9c 588/* Duplicate the EH regions from IFUN rooted at COPY_REGION into
589 the current function and root the tree below OUTER_REGION.
590 The special case of COPY_REGION of NULL means all regions.
591 Remap labels using MAP/MAP_DATA callback. Return a pointer map
592 that allows the caller to remap uses of both EH regions and
593 EH landing pads. */
686e094c 594
e38def9c 595struct pointer_map_t *
596duplicate_eh_regions (struct function *ifun,
597 eh_region copy_region, int outer_lp,
598 duplicate_eh_regions_map map, void *map_data)
686e094c 599{
e38def9c 600 struct duplicate_eh_regions_data data;
601 eh_region outer_region;
686e094c 602
e38def9c 603#ifdef ENABLE_CHECKING
604 verify_eh_tree (ifun);
605#endif
927a6b6b 606
e38def9c 607 data.label_map = map;
608 data.label_map_data = map_data;
609 data.eh_map = pointer_map_create ();
927a6b6b 610
e38def9c 611 outer_region = get_eh_region_from_lp_number (outer_lp);
48e1416a 612
e38def9c 613 /* Copy all the regions in the subtree. */
614 if (copy_region)
615 duplicate_eh_regions_1 (&data, copy_region, outer_region);
616 else
927a6b6b 617 {
e38def9c 618 eh_region r;
619 for (r = ifun->eh->region_tree; r ; r = r->next_peer)
620 duplicate_eh_regions_1 (&data, r, outer_region);
927a6b6b 621 }
927a6b6b 622
e38def9c 623#ifdef ENABLE_CHECKING
624 verify_eh_tree (cfun);
625#endif
927a6b6b 626
e38def9c 627 return data.eh_map;
927a6b6b 628}
629
e38def9c 630/* Return the region that is outer to both REGION_A and REGION_B in IFUN. */
927a6b6b 631
e38def9c 632eh_region
633eh_region_outermost (struct function *ifun, eh_region region_a,
634 eh_region region_b)
927a6b6b 635{
e38def9c 636 sbitmap b_outer;
637
638 gcc_assert (ifun->eh->region_array);
639 gcc_assert (ifun->eh->region_tree);
927a6b6b 640
e38def9c 641 b_outer = sbitmap_alloc (VEC_length (eh_region, ifun->eh->region_array));
642 sbitmap_zero (b_outer);
927a6b6b 643
e38def9c 644 do
645 {
646 SET_BIT (b_outer, region_b->index);
647 region_b = region_b->outer;
648 }
649 while (region_b);
927a6b6b 650
e38def9c 651 do
927a6b6b 652 {
e38def9c 653 if (TEST_BIT (b_outer, region_a->index))
927a6b6b 654 break;
e38def9c 655 region_a = region_a->outer;
927a6b6b 656 }
e38def9c 657 while (region_a);
927a6b6b 658
e38def9c 659 sbitmap_free (b_outer);
660 return region_a;
661}
662\f
927a6b6b 663static int
e38def9c 664t2r_eq (const void *pentry, const void *pdata)
927a6b6b 665{
e38def9c 666 const_tree const entry = (const_tree) pentry;
667 const_tree const data = (const_tree) pdata;
927a6b6b 668
e38def9c 669 return TREE_PURPOSE (entry) == data;
670}
927a6b6b 671
e38def9c 672static hashval_t
673t2r_hash (const void *pentry)
927a6b6b 674{
e38def9c 675 const_tree const entry = (const_tree) pentry;
676 return TREE_HASH (TREE_PURPOSE (entry));
677}
927a6b6b 678
e38def9c 679void
680add_type_for_runtime (tree type)
681{
682 tree *slot;
927a6b6b 683
e38def9c 684 /* If TYPE is NOP_EXPR, it means that it already is a runtime type. */
685 if (TREE_CODE (type) == NOP_EXPR)
686 return;
927a6b6b 687
e38def9c 688 slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
689 TREE_HASH (type), INSERT);
690 if (*slot == NULL)
927a6b6b 691 {
e38def9c 692 tree runtime = lang_hooks.eh_runtime_type (type);
693 *slot = tree_cons (type, runtime, NULL_TREE);
927a6b6b 694 }
927a6b6b 695}
696
e38def9c 697tree
698lookup_type_for_runtime (tree type)
4c5fcca6 699{
e38def9c 700 tree *slot;
f491db07 701
e38def9c 702 /* If TYPE is NOP_EXPR, it means that it already is a runtime type. */
703 if (TREE_CODE (type) == NOP_EXPR)
704 return type;
4c5fcca6 705
e38def9c 706 slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
707 TREE_HASH (type), NO_INSERT);
f491db07 708
e38def9c 709 /* We should have always inserted the data earlier. */
710 return TREE_VALUE (*slot);
711}
b215c058 712
e38def9c 713\f
714/* Represent an entry in @TTypes for either catch actions
715 or exception filter actions. */
716struct GTY(()) ttypes_filter {
717 tree t;
718 int filter;
719};
686e094c 720
e38def9c 721/* Compare ENTRY (a ttypes_filter entry in the hash table) with DATA
722 (a tree) for a @TTypes type node we are thinking about adding. */
686e094c 723
e38def9c 724static int
725ttypes_filter_eq (const void *pentry, const void *pdata)
726{
727 const struct ttypes_filter *const entry
728 = (const struct ttypes_filter *) pentry;
729 const_tree const data = (const_tree) pdata;
8cd32046 730
df4b504c 731 return entry->t == data;
506b6864 732}
733
df4b504c 734static hashval_t
35cb5232 735ttypes_filter_hash (const void *pentry)
df4b504c 736{
737 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
908e5f41 738 return TREE_HASH (entry->t);
df4b504c 739}
97ecdf3e 740
df4b504c 741/* Compare ENTRY with DATA (both struct ttypes_filter) for a @TTypes
742 exception specification list we are thinking about adding. */
743/* ??? Currently we use the type lists in the order given. Someone
744 should put these in some canonical order. */
745
746static int
35cb5232 747ehspec_filter_eq (const void *pentry, const void *pdata)
97ecdf3e 748{
df4b504c 749 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
750 const struct ttypes_filter *data = (const struct ttypes_filter *) pdata;
751
752 return type_list_equal (entry->t, data->t);
97ecdf3e 753}
754
df4b504c 755/* Hash function for exception specification lists. */
97ecdf3e 756
df4b504c 757static hashval_t
35cb5232 758ehspec_filter_hash (const void *pentry)
97ecdf3e 759{
df4b504c 760 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
761 hashval_t h = 0;
762 tree list;
763
764 for (list = entry->t; list ; list = TREE_CHAIN (list))
908e5f41 765 h = (h << 5) + (h >> 27) + TREE_HASH (TREE_VALUE (list));
df4b504c 766 return h;
97ecdf3e 767}
768
e38def9c 769/* Add TYPE (which may be NULL) to cfun->eh->ttype_data, using TYPES_HASH
908e5f41 770 to speed up the search. Return the filter value to be used. */
97ecdf3e 771
df4b504c 772static int
35cb5232 773add_ttypes_entry (htab_t ttypes_hash, tree type)
97ecdf3e 774{
df4b504c 775 struct ttypes_filter **slot, *n;
97ecdf3e 776
df4b504c 777 slot = (struct ttypes_filter **)
908e5f41 778 htab_find_slot_with_hash (ttypes_hash, type, TREE_HASH (type), INSERT);
df4b504c 779
780 if ((n = *slot) == NULL)
97ecdf3e 781 {
df4b504c 782 /* Filter value is a 1 based table index. */
97bb6c17 783
4c36ffe6 784 n = XNEW (struct ttypes_filter);
df4b504c 785 n->t = type;
e38def9c 786 n->filter = VEC_length (tree, cfun->eh->ttype_data) + 1;
df4b504c 787 *slot = n;
788
e38def9c 789 VEC_safe_push (tree, gc, cfun->eh->ttype_data, type);
97ecdf3e 790 }
df4b504c 791
792 return n->filter;
97ecdf3e 793}
794
e38def9c 795/* Add LIST to cfun->eh->ehspec_data, using EHSPEC_HASH and TYPES_HASH
df4b504c 796 to speed up the search. Return the filter value to be used. */
797
798static int
35cb5232 799add_ehspec_entry (htab_t ehspec_hash, htab_t ttypes_hash, tree list)
97bb6c17 800{
df4b504c 801 struct ttypes_filter **slot, *n;
802 struct ttypes_filter dummy;
97bb6c17 803
df4b504c 804 dummy.t = list;
805 slot = (struct ttypes_filter **)
806 htab_find_slot (ehspec_hash, &dummy, INSERT);
807
808 if ((n = *slot) == NULL)
809 {
e38def9c 810 int len;
811
812 if (targetm.arm_eabi_unwinder)
813 len = VEC_length (tree, cfun->eh->ehspec_data.arm_eabi);
814 else
815 len = VEC_length (uchar, cfun->eh->ehspec_data.other);
816
df4b504c 817 /* Filter value is a -1 based byte index into a uleb128 buffer. */
818
4c36ffe6 819 n = XNEW (struct ttypes_filter);
df4b504c 820 n->t = list;
e38def9c 821 n->filter = -(len + 1);
df4b504c 822 *slot = n;
823
1774763d 824 /* Generate a 0 terminated list of filter values. */
df4b504c 825 for (; list ; list = TREE_CHAIN (list))
1774763d 826 {
827 if (targetm.arm_eabi_unwinder)
e38def9c 828 VEC_safe_push (tree, gc, cfun->eh->ehspec_data.arm_eabi,
829 TREE_VALUE (list));
1774763d 830 else
831 {
832 /* Look up each type in the list and encode its filter
833 value as a uleb128. */
e38def9c 834 push_uleb128 (&cfun->eh->ehspec_data.other,
835 add_ttypes_entry (ttypes_hash, TREE_VALUE (list)));
5c15c916 836 }
df4b504c 837 }
e38def9c 838 if (targetm.arm_eabi_unwinder)
839 VEC_safe_push (tree, gc, cfun->eh->ehspec_data.arm_eabi, NULL_TREE);
840 else
841 VEC_safe_push (uchar, gc, cfun->eh->ehspec_data.other, 0);
df4b504c 842 }
e38def9c 843
844 return n->filter;
df4b504c 845}
6c74b671 846
e38def9c 847/* Generate the action filter values to be used for CATCH and
848 ALLOWED_EXCEPTIONS regions. When using dwarf2 exception regions,
849 we use lots of landing pads, and so every type or list can share
850 the same filter value, which saves table space. */
1639c6db 851
e38def9c 852void
853assign_filter_values (void)
6c74b671 854{
df4b504c 855 int i;
e38def9c 856 htab_t ttypes, ehspec;
857 eh_region r;
858 eh_catch c;
1228f0d8 859
e38def9c 860 cfun->eh->ttype_data = VEC_alloc (tree, gc, 16);
861 if (targetm.arm_eabi_unwinder)
862 cfun->eh->ehspec_data.arm_eabi = VEC_alloc (tree, gc, 64);
863 else
864 cfun->eh->ehspec_data.other = VEC_alloc (uchar, gc, 64);
6c74b671 865
e38def9c 866 ttypes = htab_create (31, ttypes_filter_hash, ttypes_filter_eq, free);
867 ehspec = htab_create (31, ehspec_filter_hash, ehspec_filter_eq, free);
6c74b671 868
e38def9c 869 for (i = 1; VEC_iterate (eh_region, cfun->eh->region_array, i, r); ++i)
870 {
871 if (r == NULL)
df4b504c 872 continue;
1228f0d8 873
e38def9c 874 switch (r->type)
54f7a985 875 {
e38def9c 876 case ERT_TRY:
877 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
504e084a 878 {
e38def9c 879 /* Whatever type_list is (NULL or true list), we build a list
880 of filters for the region. */
881 c->filter_list = NULL_TREE;
882
883 if (c->type_list != NULL)
884 {
885 /* Get a filter value for each of the types caught and store
886 them in the region's dedicated list. */
887 tree tp_node = c->type_list;
888
889 for ( ; tp_node; tp_node = TREE_CHAIN (tp_node))
890 {
891 int flt = add_ttypes_entry (ttypes, TREE_VALUE (tp_node));
892 tree flt_node = build_int_cst (NULL_TREE, flt);
893
894 c->filter_list
895 = tree_cons (NULL_TREE, flt_node, c->filter_list);
896 }
897 }
898 else
899 {
900 /* Get a filter value for the NULL list also since it
901 will need an action record anyway. */
902 int flt = add_ttypes_entry (ttypes, NULL);
903 tree flt_node = build_int_cst (NULL_TREE, flt);
904
905 c->filter_list
906 = tree_cons (NULL_TREE, flt_node, NULL);
907 }
504e084a 908 }
e38def9c 909 break;
504e084a 910
e38def9c 911 case ERT_ALLOWED_EXCEPTIONS:
912 r->u.allowed.filter
913 = add_ehspec_entry (ehspec, ttypes, r->u.allowed.type_list);
914 break;
4ee9c684 915
e38def9c 916 default:
917 break;
918 }
df4b504c 919 }
e38def9c 920
921 htab_delete (ttypes);
922 htab_delete (ehspec);
df4b504c 923}
924
e38def9c 925/* Emit SEQ into basic block just before INSN (that is assumed to be
926 first instruction of some existing BB and return the newly
927 produced block. */
928static basic_block
929emit_to_new_bb_before (rtx seq, rtx insn)
930{
931 rtx last;
932 basic_block bb;
933 edge e;
934 edge_iterator ei;
935
936 /* If there happens to be a fallthru edge (possibly created by cleanup_cfg
937 call), we don't want it to go into newly created landing pad or other EH
938 construct. */
939 for (ei = ei_start (BLOCK_FOR_INSN (insn)->preds); (e = ei_safe_edge (ei)); )
940 if (e->flags & EDGE_FALLTHRU)
941 force_nonfallthru (e);
942 else
943 ei_next (&ei);
944 last = emit_insn_before (seq, insn);
945 if (BARRIER_P (last))
946 last = PREV_INSN (last);
947 bb = create_basic_block (seq, last, BLOCK_FOR_INSN (insn)->prev_bb);
948 update_bb_for_insn (bb);
949 bb->flags |= BB_SUPERBLOCK;
950 return bb;
951}
df4b504c 952\f
e38def9c 953/* Expand the extra code needed at landing pads for dwarf2 unwinding. */
954
df4b504c 955static void
35cb5232 956dw2_build_landing_pads (void)
97ecdf3e 957{
97b330ca 958 int i;
e38def9c 959 eh_landing_pad lp;
97ecdf3e 960
e38def9c 961 for (i = 1; VEC_iterate (eh_landing_pad, cfun->eh->lp_array, i, lp); ++i)
df4b504c 962 {
e38def9c 963 eh_region region;
54f7a985 964 basic_block bb;
e38def9c 965 rtx seq;
54f7a985 966 edge e;
97ecdf3e 967
e38def9c 968 if (lp == NULL || lp->post_landing_pad == NULL)
927a6b6b 969 continue;
970
df4b504c 971 start_sequence ();
97ecdf3e 972
e38def9c 973 lp->landing_pad = gen_label_rtx ();
974 emit_label (lp->landing_pad);
c242f3f7 975 LABEL_PRESERVE_P (lp->landing_pad) = 1;
97ecdf3e 976
df4b504c 977#ifdef HAVE_exception_receiver
978 if (HAVE_exception_receiver)
979 emit_insn (gen_exception_receiver ());
980 else
981#endif
982#ifdef HAVE_nonlocal_goto_receiver
983 if (HAVE_nonlocal_goto_receiver)
984 emit_insn (gen_nonlocal_goto_receiver ());
985 else
986#endif
987 { /* Nothing */ }
97ecdf3e 988
e38def9c 989 region = lp->region;
990 if (region->exc_ptr_reg)
991 emit_move_insn (region->exc_ptr_reg,
992 gen_rtx_REG (ptr_mode, EH_RETURN_DATA_REGNO (0)));
993 if (region->filter_reg)
994 emit_move_insn (region->filter_reg,
995 gen_rtx_REG (targetm.eh_return_filter_mode (),
996 EH_RETURN_DATA_REGNO (1)));
011a7f23 997
df4b504c 998 seq = get_insns ();
999 end_sequence ();
64db8f62 1000
e38def9c 1001 bb = emit_to_new_bb_before (seq, label_rtx (lp->post_landing_pad));
54f7a985 1002 e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
1003 e->count = bb->count;
1004 e->probability = REG_BR_PROB_BASE;
df4b504c 1005 }
97ecdf3e 1006}
1007
df4b504c 1008\f
e38def9c 1009static VEC (int, heap) *sjlj_lp_call_site_index;
97bb6c17 1010
e38def9c 1011/* Process all active landing pads. Assign each one a compact dispatch
1012 index, and a call-site index. */
23ceb7b2 1013
e38def9c 1014static int
1015sjlj_assign_call_site_values (void)
23ceb7b2 1016{
df4b504c 1017 htab_t ar_hash;
e38def9c 1018 int i, disp_index;
1019 eh_landing_pad lp;
df4b504c 1020
e38def9c 1021 crtl->eh.action_record_data = VEC_alloc (uchar, gc, 64);
df4b504c 1022 ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
1023
e38def9c 1024 disp_index = 0;
1025 call_site_base = 1;
1026 for (i = 1; VEC_iterate (eh_landing_pad, cfun->eh->lp_array, i, lp); ++i)
1027 if (lp && lp->post_landing_pad)
d63ea2f2 1028 {
e38def9c 1029 int action, call_site;
318077fa 1030
e38def9c 1031 /* First: build the action table. */
1032 action = collect_one_action_chain (ar_hash, lp->region);
1033 if (action != -1)
18d50ae6 1034 crtl->uses_eh_lsda = 1;
df4b504c 1035
e38def9c 1036 /* Next: assign call-site values. If dwarf2 terms, this would be
1037 the region number assigned by convert_to_eh_region_ranges, but
1038 handles no-action and must-not-throw differently. */
df4b504c 1039 /* Map must-not-throw to otherwise unused call-site index 0. */
1040 if (action == -2)
e38def9c 1041 call_site = 0;
df4b504c 1042 /* Map no-action to otherwise unused call-site index -1. */
1043 else if (action == -1)
e38def9c 1044 call_site = -1;
df4b504c 1045 /* Otherwise, look it up in the table. */
1046 else
e38def9c 1047 call_site = add_call_site (GEN_INT (disp_index), action, 0);
1048 VEC_replace (int, sjlj_lp_call_site_index, i, call_site);
df4b504c 1049
e38def9c 1050 disp_index++;
df4b504c 1051 }
e38def9c 1052
1053 htab_delete (ar_hash);
1054
1055 return disp_index;
97ecdf3e 1056}
8591d03a 1057
e38def9c 1058/* Emit code to record the current call-site index before every
1059 insn that can throw. */
1060
df4b504c 1061static void
e38def9c 1062sjlj_mark_call_sites (void)
8591d03a 1063{
df4b504c 1064 int last_call_site = -2;
1065 rtx insn, mem;
1066
df4b504c 1067 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
8591d03a 1068 {
e38def9c 1069 eh_landing_pad lp;
1070 eh_region r;
1071 bool nothrow;
df4b504c 1072 int this_call_site;
e38def9c 1073 rtx before, p;
8591d03a 1074
df4b504c 1075 /* Reset value tracking at extended basic block boundaries. */
6d7dc5b9 1076 if (LABEL_P (insn))
df4b504c 1077 last_call_site = -2;
8591d03a 1078
df4b504c 1079 if (! INSN_P (insn))
1080 continue;
8591d03a 1081
e38def9c 1082 nothrow = get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1083 if (nothrow)
0f656a57 1084 continue;
e38def9c 1085 if (lp)
1086 this_call_site = VEC_index (int, sjlj_lp_call_site_index, lp->index);
1087 else if (r == NULL)
df4b504c 1088 {
1089 /* Calls (and trapping insns) without notes are outside any
1090 exception handling region in this function. Mark them as
1091 no action. */
e38def9c 1092 this_call_site = -1;
df4b504c 1093 }
1094 else
e38def9c 1095 {
1096 gcc_assert (r->type == ERT_MUST_NOT_THROW);
1097 this_call_site = 0;
1098 }
8591d03a 1099
df4b504c 1100 if (this_call_site == last_call_site)
1101 continue;
1102
1103 /* Don't separate a call from it's argument loads. */
1104 before = insn;
6d7dc5b9 1105 if (CALL_P (insn))
ff385626 1106 before = find_first_parameter_load (insn, NULL_RTX);
97ecdf3e 1107
df4b504c 1108 start_sequence ();
4dd3de56 1109 mem = adjust_address (crtl->eh.sjlj_fc, TYPE_MODE (integer_type_node),
82832d9a 1110 sjlj_fc_call_site_ofs);
df4b504c 1111 emit_move_insn (mem, GEN_INT (this_call_site));
1112 p = get_insns ();
1113 end_sequence ();
97bb6c17 1114
31d3e01c 1115 emit_insn_before (p, before);
df4b504c 1116 last_call_site = this_call_site;
1117 }
1118}
97ecdf3e 1119
df4b504c 1120/* Construct the SjLj_Function_Context. */
1121
1122static void
35cb5232 1123sjlj_emit_function_enter (rtx dispatch_label)
97ecdf3e 1124{
df4b504c 1125 rtx fn_begin, fc, mem, seq;
d2099aed 1126 bool fn_begin_outside_block;
58d82cd0 1127 rtx personality = get_personality_function (current_function_decl);
97ecdf3e 1128
4dd3de56 1129 fc = crtl->eh.sjlj_fc;
97ecdf3e 1130
df4b504c 1131 start_sequence ();
de46d199 1132
b52a90a7 1133 /* We're storing this libcall's address into memory instead of
1134 calling it directly. Thus, we must call assemble_external_libcall
1135 here, as we can not depend on emit_library_call to do it for us. */
58d82cd0 1136 assemble_external_libcall (personality);
e513d163 1137 mem = adjust_address (fc, Pmode, sjlj_fc_personality_ofs);
58d82cd0 1138 emit_move_insn (mem, personality);
df4b504c 1139
e513d163 1140 mem = adjust_address (fc, Pmode, sjlj_fc_lsda_ofs);
18d50ae6 1141 if (crtl->uses_eh_lsda)
df4b504c 1142 {
1143 char buf[20];
134c13b5 1144 rtx sym;
1145
4781f9b9 1146 ASM_GENERATE_INTERNAL_LABEL (buf, "LLSDA", current_function_funcdef_no);
134c13b5 1147 sym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
1148 SYMBOL_REF_FLAGS (sym) = SYMBOL_FLAG_LOCAL;
1149 emit_move_insn (mem, sym);
de46d199 1150 }
df4b504c 1151 else
1152 emit_move_insn (mem, const0_rtx);
1ed5443b 1153
df4b504c 1154#ifdef DONT_USE_BUILTIN_SETJMP
1155 {
d78f9811 1156 rtx x, last;
0ff18307 1157 x = emit_library_call_value (setjmp_libfunc, NULL_RTX, LCT_RETURNS_TWICE,
df4b504c 1158 TYPE_MODE (integer_type_node), 1,
1159 plus_constant (XEXP (fc, 0),
1160 sjlj_fc_jbuf_ofs), Pmode);
1161
df4b504c 1162 emit_cmp_and_jump_insns (x, const0_rtx, NE, 0,
2b96c5f6 1163 TYPE_MODE (integer_type_node), 0, dispatch_label);
d78f9811 1164 last = get_last_insn ();
1165 if (JUMP_P (last) && any_condjump_p (last))
1166 {
1167 gcc_assert (!find_reg_note (last, REG_BR_PROB, 0));
1168 add_reg_note (last, REG_BR_PROB, GEN_INT (REG_BR_PROB_BASE / 100));
1169 }
df4b504c 1170 }
1171#else
1172 expand_builtin_setjmp_setup (plus_constant (XEXP (fc, 0), sjlj_fc_jbuf_ofs),
1173 dispatch_label);
97ecdf3e 1174#endif
97ecdf3e 1175
df4b504c 1176 emit_library_call (unwind_sjlj_register_libfunc, LCT_NORMAL, VOIDmode,
1177 1, XEXP (fc, 0), Pmode);
97bb6c17 1178
df4b504c 1179 seq = get_insns ();
1180 end_sequence ();
97ecdf3e 1181
df4b504c 1182 /* ??? Instead of doing this at the beginning of the function,
1183 do this in a block that is at loop level 0 and dominates all
1184 can_throw_internal instructions. */
97ecdf3e 1185
d2099aed 1186 fn_begin_outside_block = true;
df4b504c 1187 for (fn_begin = get_insns (); ; fn_begin = NEXT_INSN (fn_begin))
d2099aed 1188 if (NOTE_P (fn_begin))
1189 {
ad4583d9 1190 if (NOTE_KIND (fn_begin) == NOTE_INSN_FUNCTION_BEG)
d2099aed 1191 break;
ad4583d9 1192 else if (NOTE_INSN_BASIC_BLOCK_P (fn_begin))
d2099aed 1193 fn_begin_outside_block = false;
1194 }
1195
1196 if (fn_begin_outside_block)
ea091dfd 1197 insert_insn_on_edge (seq, single_succ_edge (ENTRY_BLOCK_PTR));
54f7a985 1198 else
d2099aed 1199 emit_insn_after (seq, fn_begin);
97ecdf3e 1200}
1201
df4b504c 1202/* Call back from expand_function_end to know where we should put
1203 the call to unwind_sjlj_unregister_libfunc if needed. */
97bb6c17 1204
df4b504c 1205void
35cb5232 1206sjlj_emit_function_exit_after (rtx after)
df4b504c 1207{
4dd3de56 1208 crtl->eh.sjlj_exit_after = after;
df4b504c 1209}
97ecdf3e 1210
1211static void
35cb5232 1212sjlj_emit_function_exit (void)
df4b504c 1213{
b2ee26d5 1214 rtx seq, insn;
97ecdf3e 1215
df4b504c 1216 start_sequence ();
fbba5463 1217
df4b504c 1218 emit_library_call (unwind_sjlj_unregister_libfunc, LCT_NORMAL, VOIDmode,
4dd3de56 1219 1, XEXP (crtl->eh.sjlj_fc, 0), Pmode);
d63ea2f2 1220
df4b504c 1221 seq = get_insns ();
1222 end_sequence ();
97ecdf3e 1223
df4b504c 1224 /* ??? Really this can be done in any block at loop level 0 that
1225 post-dominates all can_throw_internal instructions. This is
1226 the last possible moment. */
011a7f23 1227
b2ee26d5 1228 insn = crtl->eh.sjlj_exit_after;
1229 if (LABEL_P (insn))
1230 insn = NEXT_INSN (insn);
54f7a985 1231
b2ee26d5 1232 emit_insn_after (seq, insn);
011a7f23 1233}
1234
df4b504c 1235static void
e38def9c 1236sjlj_emit_dispatch_table (rtx dispatch_label, int num_dispatch)
fbba5463 1237{
1bd43494 1238 enum machine_mode unwind_word_mode = targetm.unwind_word_mode ();
1239 enum machine_mode filter_mode = targetm.eh_return_filter_mode ();
e38def9c 1240 eh_landing_pad lp;
1241 rtx mem, seq, fc, before, exc_ptr_reg, filter_reg;
1242 rtx first_reachable_label;
54f7a985 1243 basic_block bb;
e38def9c 1244 eh_region r;
54f7a985 1245 edge e;
e38def9c 1246 int i, disp_index;
1247 gimple switch_stmt;
df4b504c 1248
4dd3de56 1249 fc = crtl->eh.sjlj_fc;
df4b504c 1250
1251 start_sequence ();
1252
1253 emit_label (dispatch_label);
1ed5443b 1254
df4b504c 1255#ifndef DONT_USE_BUILTIN_SETJMP
1256 expand_builtin_setjmp_receiver (dispatch_label);
df4b504c 1257
e38def9c 1258 /* The caller of expand_builtin_setjmp_receiver is responsible for
1259 making sure that the label doesn't vanish. The only other caller
1260 is the expander for __builtin_setjmp_receiver, which places this
1261 label on the nonlocal_goto_label list. Since we're modeling these
1262 CFG edges more exactly, we can use the forced_labels list instead. */
1263 LABEL_PRESERVE_P (dispatch_label) = 1;
1264 forced_labels
1265 = gen_rtx_EXPR_LIST (VOIDmode, dispatch_label, forced_labels);
1266#endif
df4b504c 1267
e38def9c 1268 /* Load up exc_ptr and filter values from the function context. */
1bd43494 1269 mem = adjust_address (fc, unwind_word_mode, sjlj_fc_data_ofs);
1270 if (unwind_word_mode != ptr_mode)
df4b504c 1271 {
1272#ifdef POINTERS_EXTEND_UNSIGNED
a64908cd 1273 mem = convert_memory_address (ptr_mode, mem);
df4b504c 1274#else
a64908cd 1275 mem = convert_to_mode (ptr_mode, mem, 0);
df4b504c 1276#endif
1277 }
e38def9c 1278 exc_ptr_reg = force_reg (ptr_mode, mem);
df4b504c 1279
1bd43494 1280 mem = adjust_address (fc, unwind_word_mode,
1281 sjlj_fc_data_ofs + GET_MODE_SIZE (unwind_word_mode));
1282 if (unwind_word_mode != filter_mode)
1283 mem = convert_to_mode (filter_mode, mem, 0);
e38def9c 1284 filter_reg = force_reg (filter_mode, mem);
97ecdf3e 1285
df4b504c 1286 /* Jump to one of the directly reachable regions. */
df4b504c 1287
e38def9c 1288 disp_index = 0;
1289 first_reachable_label = NULL;
1290
1291 /* If there's exactly one call site in the function, don't bother
1292 generating a switch statement. */
1293 switch_stmt = NULL;
1294 if (num_dispatch > 1)
173f0bec 1295 {
e38def9c 1296 tree disp;
173f0bec 1297
e38def9c 1298 mem = adjust_address (fc, TYPE_MODE (integer_type_node),
1299 sjlj_fc_call_site_ofs);
1300 disp = make_tree (integer_type_node, mem);
1301
1302 switch_stmt = gimple_build_switch_nlabels (num_dispatch, disp, NULL);
1303 }
1304
1305 for (i = 1; VEC_iterate (eh_landing_pad, cfun->eh->lp_array, i, lp); ++i)
1306 if (lp && lp->post_landing_pad)
1307 {
1308 rtx seq2, label;
1309
1310 start_sequence ();
1311
1312 lp->landing_pad = dispatch_label;
1313
1314 if (num_dispatch > 1)
1315 {
1316 tree t_label, case_elt;
1317
1318 t_label = create_artificial_label (UNKNOWN_LOCATION);
1319 case_elt = build3 (CASE_LABEL_EXPR, void_type_node,
1320 build_int_cst (NULL, disp_index),
1321 NULL, t_label);
1322 gimple_switch_set_label (switch_stmt, disp_index, case_elt);
1323
1324 label = label_rtx (t_label);
1325 }
1326 else
1327 label = gen_label_rtx ();
1328
1329 if (disp_index == 0)
1330 first_reachable_label = label;
1331 emit_label (label);
1332
1333 r = lp->region;
1334 if (r->exc_ptr_reg)
1335 emit_move_insn (r->exc_ptr_reg, exc_ptr_reg);
1336 if (r->filter_reg)
1337 emit_move_insn (r->filter_reg, filter_reg);
1338
1339 seq2 = get_insns ();
1340 end_sequence ();
1341
1342 before = label_rtx (lp->post_landing_pad);
1343 bb = emit_to_new_bb_before (seq2, before);
1344 e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
1345 e->count = bb->count;
1346 e->probability = REG_BR_PROB_BASE;
d63ea2f2 1347
e38def9c 1348 disp_index++;
1349 }
1350 gcc_assert (disp_index == num_dispatch);
1351
1352 if (num_dispatch > 1)
1353 {
1354 expand_case (switch_stmt);
1355 expand_builtin_trap ();
173f0bec 1356 }
011a7f23 1357
df4b504c 1358 seq = get_insns ();
1359 end_sequence ();
97ecdf3e 1360
e38def9c 1361 bb = emit_to_new_bb_before (seq, first_reachable_label);
1362 if (num_dispatch == 1)
1363 {
1364 e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
1365 e->count = bb->count;
1366 e->probability = REG_BR_PROB_BASE;
1367 }
fbba5463 1368}
1369
df4b504c 1370static void
35cb5232 1371sjlj_build_landing_pads (void)
fbba5463 1372{
e38def9c 1373 int num_dispatch;
fbba5463 1374
e38def9c 1375 num_dispatch = VEC_length (eh_landing_pad, cfun->eh->lp_array);
1376 if (num_dispatch == 0)
1377 return;
1378 VEC_safe_grow (int, heap, sjlj_lp_call_site_index, num_dispatch);
fbba5463 1379
e38def9c 1380 num_dispatch = sjlj_assign_call_site_values ();
1381 if (num_dispatch > 0)
df4b504c 1382 {
1383 rtx dispatch_label = gen_label_rtx ();
c9b50df7 1384 int align = STACK_SLOT_ALIGNMENT (sjlj_fc_type_node,
1385 TYPE_MODE (sjlj_fc_type_node),
1386 TYPE_ALIGN (sjlj_fc_type_node));
4dd3de56 1387 crtl->eh.sjlj_fc
df4b504c 1388 = assign_stack_local (TYPE_MODE (sjlj_fc_type_node),
1389 int_size_in_bytes (sjlj_fc_type_node),
c9b50df7 1390 align);
97ecdf3e 1391
e38def9c 1392 sjlj_mark_call_sites ();
df4b504c 1393 sjlj_emit_function_enter (dispatch_label);
e38def9c 1394 sjlj_emit_dispatch_table (dispatch_label, num_dispatch);
df4b504c 1395 sjlj_emit_function_exit ();
1396 }
173f0bec 1397
e38def9c 1398 VEC_free (int, heap, sjlj_lp_call_site_index);
97ecdf3e 1399}
fbba5463 1400
2ea77971 1401/* After initial rtl generation, call back to finish generating
1402 exception support code. */
1403
1404static void
35cb5232 1405finish_eh_generation (void)
fbba5463 1406{
54f7a985 1407 basic_block bb;
1408
df4b504c 1409 /* Construct the landing pads. */
df4b504c 1410 if (USING_SJLJ_EXCEPTIONS)
1411 sjlj_build_landing_pads ();
1412 else
1413 dw2_build_landing_pads ();
54f7a985 1414 break_superblocks ();
e38def9c 1415
8a1586ba 1416 if (USING_SJLJ_EXCEPTIONS
1417 /* Kludge for Alpha/Tru64 (see alpha_gp_save_rtx). */
1418 || single_succ_edge (ENTRY_BLOCK_PTR)->insns.r)
54f7a985 1419 commit_edge_insertions ();
e38def9c 1420
1421 /* Redirect all EH edges from the post_landing_pad to the landing pad. */
54f7a985 1422 FOR_EACH_BB (bb)
1423 {
e38def9c 1424 eh_landing_pad lp;
cd665a06 1425 edge_iterator ei;
e38def9c 1426 edge e;
3d1eacdb 1427
e38def9c 1428 lp = get_eh_landing_pad_from_rtx (BB_END (bb));
8f8dcce4 1429
e38def9c 1430 FOR_EACH_EDGE (e, ei, bb->succs)
1431 if (e->flags & EDGE_EH)
1432 break;
0cc4271a 1433
e38def9c 1434 /* We should not have generated any new throwing insns during this
1435 pass, and we should not have lost any EH edges, so we only need
1436 to handle two cases here:
1437 (1) reachable handler and an existing edge to post-landing-pad,
1438 (2) no reachable handler and no edge. */
1439 gcc_assert ((lp != NULL) == (e != NULL));
1440 if (lp != NULL)
0cc4271a 1441 {
e38def9c 1442 gcc_assert (BB_HEAD (e->dest) == label_rtx (lp->post_landing_pad));
98b4572e 1443
e38def9c 1444 redirect_edge_succ (e, BLOCK_FOR_INSN (lp->landing_pad));
1445 e->flags |= (CALL_P (BB_END (bb))
1446 ? EDGE_ABNORMAL | EDGE_ABNORMAL_CALL
1447 : EDGE_ABNORMAL);
df4b504c 1448 }
1449 }
97ecdf3e 1450}
1451
e38def9c 1452static bool
1453gate_handle_eh (void)
1454{
1455 /* Nothing to do if no regions created. */
1456 return cfun->eh->region_tree != NULL;
1457}
686e094c 1458
e38def9c 1459/* Complete generation of exception handling code. */
1460static unsigned int
1461rest_of_handle_eh (void)
686e094c 1462{
e38def9c 1463 finish_eh_generation ();
1464 cleanup_cfg (CLEANUP_NO_INSN_DEL);
1465 return 0;
686e094c 1466}
1467
e38def9c 1468struct rtl_opt_pass pass_rtl_eh =
1469{
1470 {
1471 RTL_PASS,
0c297edc 1472 "rtl eh", /* name */
e38def9c 1473 gate_handle_eh, /* gate */
1474 rest_of_handle_eh, /* execute */
1475 NULL, /* sub */
1476 NULL, /* next */
1477 0, /* static_pass_number */
1478 TV_JUMP, /* tv_id */
1479 0, /* properties_required */
1480 0, /* properties_provided */
1481 0, /* properties_destroyed */
1482 0, /* todo_flags_start */
1483 TODO_dump_func /* todo_flags_finish */
1484 }
1485};
1486\f
1487/* This section handles removing dead code for flow. */
4c5fcca6 1488
1489void
e38def9c 1490remove_eh_landing_pad (eh_landing_pad lp)
4c5fcca6 1491{
e38def9c 1492 eh_landing_pad *pp;
4c5fcca6 1493
e38def9c 1494 for (pp = &lp->region->landing_pads; *pp != lp; pp = &(*pp)->next_lp)
1495 continue;
1496 *pp = lp->next_lp;
48e1416a 1497
e38def9c 1498 if (lp->post_landing_pad)
1499 EH_LANDING_PAD_NR (lp->post_landing_pad) = 0;
1500 VEC_replace (eh_landing_pad, cfun->eh->lp_array, lp->index, NULL);
4c5fcca6 1501}
1502
e38def9c 1503/* Splice REGION from the region tree. */
3d1eacdb 1504
1505void
e38def9c 1506remove_eh_handler (eh_region region)
3d1eacdb 1507{
e38def9c 1508 eh_region *pp, *pp_start, p, outer;
1509 eh_landing_pad lp;
3d1eacdb 1510
e38def9c 1511 for (lp = region->landing_pads; lp ; lp = lp->next_lp)
48d5ef93 1512 {
e38def9c 1513 if (lp->post_landing_pad)
1514 EH_LANDING_PAD_NR (lp->post_landing_pad) = 0;
1515 VEC_replace (eh_landing_pad, cfun->eh->lp_array, lp->index, NULL);
48d5ef93 1516 }
bf6102ae 1517
e38def9c 1518 outer = region->outer;
1519 if (outer)
1520 pp_start = &outer->inner;
1521 else
1522 pp_start = &cfun->eh->region_tree;
1523 for (pp = pp_start, p = *pp; p != region; pp = &p->next_peer, p = *pp)
1524 continue;
1525 if (region->inner)
bf6102ae 1526 {
e38def9c 1527 *pp = p = region->inner;
1528 do
1529 {
1530 p->outer = outer;
1531 pp = &p->next_peer;
1532 p = *pp;
1533 }
1534 while (p);
bf6102ae 1535 }
e38def9c 1536 *pp = region->next_peer;
a7b0c170 1537
e38def9c 1538 VEC_replace (eh_region, cfun->eh->region_array, region->index, NULL);
1539}
a7b0c170 1540
e38def9c 1541/* Invokes CALLBACK for every exception handler landing pad label.
1542 Only used by reload hackery; should not be used by new code. */
a7b0c170 1543
e38def9c 1544void
1545for_each_eh_label (void (*callback) (rtx))
a7b0c170 1546{
e38def9c 1547 eh_landing_pad lp;
1548 int i;
df4b504c 1549
e38def9c 1550 for (i = 1; VEC_iterate (eh_landing_pad, cfun->eh->lp_array, i, lp); ++i)
d3a0267f 1551 {
e38def9c 1552 if (lp)
34e5cced 1553 {
e38def9c 1554 rtx lab = lp->landing_pad;
1555 if (lab && LABEL_P (lab))
1556 (*callback) (lab);
34e5cced 1557 }
df4b504c 1558 }
a7b0c170 1559}
e38def9c 1560\f
1561/* Create the REG_EH_REGION note for INSN, given its ECF_FLAGS for a
48e1416a 1562 call insn.
e38def9c 1563
1564 At the gimple level, we use LP_NR
1565 > 0 : The statement transfers to landing pad LP_NR
1566 = 0 : The statement is outside any EH region
1567 < 0 : The statement is within MUST_NOT_THROW region -LP_NR.
1568
1569 At the rtl level, we use LP_NR
1570 > 0 : The insn transfers to landing pad LP_NR
1571 = 0 : The insn cannot throw
1572 < 0 : The insn is within MUST_NOT_THROW region -LP_NR
1573 = INT_MIN : The insn cannot throw or execute a nonlocal-goto.
1574 missing note: The insn is outside any EH region.
1575
1576 ??? This difference probably ought to be avoided. We could stand
1577 to record nothrow for arbitrary gimple statements, and so avoid
1578 some moderately complex lookups in stmt_could_throw_p. Perhaps
1579 NOTHROW should be mapped on both sides to INT_MIN. Perhaps the
1580 no-nonlocal-goto property should be recorded elsewhere as a bit
1581 on the call_insn directly. Perhaps we should make more use of
1582 attaching the trees to call_insns (reachable via symbol_ref in
1583 direct call cases) and just pull the data out of the trees. */
a7b0c170 1584
e38def9c 1585void
1586make_reg_eh_region_note (rtx insn, int ecf_flags, int lp_nr)
a7b0c170 1587{
e38def9c 1588 rtx value;
1589 if (ecf_flags & ECF_NOTHROW)
1590 value = const0_rtx;
1591 else if (lp_nr != 0)
1592 value = GEN_INT (lp_nr);
df4b504c 1593 else
e38def9c 1594 return;
1595 add_reg_note (insn, REG_EH_REGION, value);
a7b0c170 1596}
1597
e38def9c 1598/* Create a REG_EH_REGION note for a CALL_INSN that cannot throw
1599 nor perform a non-local goto. Replace the region note if it
1600 already exists. */
a7b0c170 1601
e38def9c 1602void
1603make_reg_eh_region_note_nothrow_nononlocal (rtx insn)
a7b0c170 1604{
e38def9c 1605 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1606 rtx intmin = GEN_INT (INT_MIN);
df4b504c 1607
e38def9c 1608 if (note != 0)
1609 XEXP (note, 0) = intmin;
1610 else
1611 add_reg_note (insn, REG_EH_REGION, intmin);
1612}
c788feb1 1613
e38def9c 1614/* Return true if INSN could throw, assuming no REG_EH_REGION note
1615 to the contrary. */
c788feb1 1616
e38def9c 1617bool
1618insn_could_throw_p (const_rtx insn)
1619{
1620 if (CALL_P (insn))
1621 return true;
cbeb677e 1622 if (INSN_P (insn) && cfun->can_throw_non_call_exceptions)
e38def9c 1623 return may_trap_p (PATTERN (insn));
1624 return false;
1625}
0de999f1 1626
e38def9c 1627/* Copy an REG_EH_REGION note to each insn that might throw beginning
1628 at FIRST and ending at LAST. NOTE_OR_INSN is either the source insn
1629 to look for a note, or the note itself. */
0de999f1 1630
e38def9c 1631void
1632copy_reg_eh_region_note_forward (rtx note_or_insn, rtx first, rtx last)
1633{
1634 rtx insn, note = note_or_insn;
c788feb1 1635
e38def9c 1636 if (INSN_P (note_or_insn))
1637 {
1638 note = find_reg_note (note_or_insn, REG_EH_REGION, NULL_RTX);
1639 if (note == NULL)
1640 return;
c788feb1 1641 }
e38def9c 1642 note = XEXP (note, 0);
1643
1644 for (insn = first; insn != last ; insn = NEXT_INSN (insn))
1645 if (!find_reg_note (insn, REG_EH_REGION, NULL_RTX)
1646 && insn_could_throw_p (insn))
1647 add_reg_note (insn, REG_EH_REGION, note);
3c3bb268 1648}
97ecdf3e 1649
e38def9c 1650/* Likewise, but iterate backward. */
97ecdf3e 1651
4ee9c684 1652void
e38def9c 1653copy_reg_eh_region_note_backward (rtx note_or_insn, rtx last, rtx first)
97ecdf3e 1654{
e38def9c 1655 rtx insn, note = note_or_insn;
f929a98a 1656
e38def9c 1657 if (INSN_P (note_or_insn))
385df8c5 1658 {
e38def9c 1659 note = find_reg_note (note_or_insn, REG_EH_REGION, NULL_RTX);
1660 if (note == NULL)
4ee9c684 1661 return;
df4b504c 1662 }
e38def9c 1663 note = XEXP (note, 0);
8b4f3d64 1664
e38def9c 1665 for (insn = last; insn != first; insn = PREV_INSN (insn))
1666 if (insn_could_throw_p (insn))
1667 add_reg_note (insn, REG_EH_REGION, note);
4ee9c684 1668}
1669
4ee9c684 1670
e38def9c 1671/* Extract all EH information from INSN. Return true if the insn
1672 was marked NOTHROW. */
4ee9c684 1673
e38def9c 1674static bool
1675get_eh_region_and_lp_from_rtx (const_rtx insn, eh_region *pr,
1676 eh_landing_pad *plp)
4ee9c684 1677{
e38def9c 1678 eh_landing_pad lp = NULL;
1679 eh_region r = NULL;
1680 bool ret = false;
1681 rtx note;
1682 int lp_nr;
4ee9c684 1683
e38def9c 1684 if (! INSN_P (insn))
1685 goto egress;
1686
1687 if (NONJUMP_INSN_P (insn)
1688 && GET_CODE (PATTERN (insn)) == SEQUENCE)
1689 insn = XVECEXP (PATTERN (insn), 0, 0);
4ee9c684 1690
e38def9c 1691 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1692 if (!note)
4ee9c684 1693 {
e38def9c 1694 ret = !insn_could_throw_p (insn);
1695 goto egress;
4ee9c684 1696 }
e38def9c 1697
1698 lp_nr = INTVAL (XEXP (note, 0));
1699 if (lp_nr == 0 || lp_nr == INT_MIN)
4ee9c684 1700 {
e38def9c 1701 ret = true;
1702 goto egress;
4ee9c684 1703 }
35cb5232 1704
e38def9c 1705 if (lp_nr < 0)
1706 r = VEC_index (eh_region, cfun->eh->region_array, -lp_nr);
1707 else
1708 {
1709 lp = VEC_index (eh_landing_pad, cfun->eh->lp_array, lp_nr);
1710 r = lp->region;
1711 }
4ee9c684 1712
e38def9c 1713 egress:
1714 *plp = lp;
1715 *pr = r;
1716 return ret;
f929a98a 1717}
1718
e38def9c 1719/* Return the landing pad to which INSN may go, or NULL if it does not
1720 have a reachable landing pad within this function. */
97ecdf3e 1721
e38def9c 1722eh_landing_pad
1723get_eh_landing_pad_from_rtx (const_rtx insn)
97ecdf3e 1724{
e38def9c 1725 eh_landing_pad lp;
1726 eh_region r;
4ee9c684 1727
e38def9c 1728 get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1729 return lp;
1730}
4ee9c684 1731
e38def9c 1732/* Return the region to which INSN may go, or NULL if it does not
1733 have a reachable region within this function. */
4ee9c684 1734
e38def9c 1735eh_region
1736get_eh_region_from_rtx (const_rtx insn)
1737{
1738 eh_landing_pad lp;
1739 eh_region r;
4ee9c684 1740
e38def9c 1741 get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1742 return r;
4ee9c684 1743}
1744
e38def9c 1745/* Return true if INSN throws and is caught by something in this function. */
1746
4ee9c684 1747bool
7ecb5bb2 1748can_throw_internal (const_rtx insn)
4ee9c684 1749{
e38def9c 1750 return get_eh_landing_pad_from_rtx (insn) != NULL;
1751}
1752
1753/* Return true if INSN throws and escapes from the current function. */
1754
1755bool
1756can_throw_external (const_rtx insn)
1757{
1758 eh_landing_pad lp;
1759 eh_region r;
1760 bool nothrow;
d63ea2f2 1761
df4b504c 1762 if (! INSN_P (insn))
1763 return false;
97bb6c17 1764
6d7dc5b9 1765 if (NONJUMP_INSN_P (insn)
df4b504c 1766 && GET_CODE (PATTERN (insn)) == SEQUENCE)
e38def9c 1767 {
1768 rtx seq = PATTERN (insn);
1769 int i, n = XVECLEN (seq, 0);
97ecdf3e 1770
e38def9c 1771 for (i = 0; i < n; i++)
1772 if (can_throw_external (XVECEXP (seq, 0, i)))
1773 return true;
4ee9c684 1774
e38def9c 1775 return false;
1776 }
4ee9c684 1777
e38def9c 1778 nothrow = get_eh_region_and_lp_from_rtx (insn, &r, &lp);
4ee9c684 1779
e38def9c 1780 /* If we can't throw, we obviously can't throw external. */
1781 if (nothrow)
1782 return false;
97ecdf3e 1783
e38def9c 1784 /* If we have an internal landing pad, then we're not external. */
1785 if (lp != NULL)
1786 return false;
97ecdf3e 1787
e38def9c 1788 /* If we're not within an EH region, then we are external. */
1789 if (r == NULL)
1790 return true;
97ecdf3e 1791
e38def9c 1792 /* The only thing that ought to be left is MUST_NOT_THROW regions,
1793 which don't always have landing pads. */
1794 gcc_assert (r->type == ERT_MUST_NOT_THROW);
1795 return false;
df4b504c 1796}
97ecdf3e 1797
e38def9c 1798/* Return true if INSN cannot throw at all. */
1799
df4b504c 1800bool
e38def9c 1801insn_nothrow_p (const_rtx insn)
97ecdf3e 1802{
e38def9c 1803 eh_landing_pad lp;
1804 eh_region r;
97ecdf3e 1805
df4b504c 1806 if (! INSN_P (insn))
e38def9c 1807 return true;
e4bbf4c1 1808
6d7dc5b9 1809 if (NONJUMP_INSN_P (insn)
df4b504c 1810 && GET_CODE (PATTERN (insn)) == SEQUENCE)
80621b93 1811 {
1812 rtx seq = PATTERN (insn);
1813 int i, n = XVECLEN (seq, 0);
1814
1815 for (i = 0; i < n; i++)
e38def9c 1816 if (!insn_nothrow_p (XVECEXP (seq, 0, i)))
1817 return false;
80621b93 1818
e38def9c 1819 return true;
80621b93 1820 }
df4b504c 1821
e38def9c 1822 return get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1823}
1824
1825/* Return true if INSN can perform a non-local goto. */
1826/* ??? This test is here in this file because it (ab)uses REG_EH_REGION. */
1827
1828bool
1829can_nonlocal_goto (const_rtx insn)
1830{
1831 if (nonlocal_goto_handler_labels && CALL_P (insn))
df4b504c 1832 {
e38def9c 1833 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1834 if (!note || INTVAL (XEXP (note, 0)) != INT_MIN)
1835 return true;
df4b504c 1836 }
e38def9c 1837 return false;
97ecdf3e 1838}
e38def9c 1839\f
18d50ae6 1840/* Set TREE_NOTHROW and crtl->all_throwers_are_sibcalls. */
ed74c60e 1841
16fcf0f4 1842static unsigned int
35cb5232 1843set_nothrow_function_flags (void)
da5038a3 1844{
1845 rtx insn;
35cb5232 1846
b0d29d1c 1847 crtl->nothrow = 1;
da5038a3 1848
18d50ae6 1849 /* Assume crtl->all_throwers_are_sibcalls until we encounter
04396483 1850 something that can throw an exception. We specifically exempt
1851 CALL_INSNs that are SIBLING_CALL_P, as these are really jumps,
1852 and can't throw. Most CALL_INSNs are not SIBLING_CALL_P, so this
1853 is optimistic. */
da5038a3 1854
18d50ae6 1855 crtl->all_throwers_are_sibcalls = 1;
04396483 1856
b0d29d1c 1857 /* If we don't know that this implementation of the function will
1858 actually be used, then we must not set TREE_NOTHROW, since
1859 callers must not assume that this function does not throw. */
1860 if (TREE_NOTHROW (current_function_decl))
1861 return 0;
1862
04396483 1863 if (! flag_exceptions)
2a1990e9 1864 return 0;
35cb5232 1865
da5038a3 1866 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
df4b504c 1867 if (can_throw_external (insn))
04396483 1868 {
b0d29d1c 1869 crtl->nothrow = 0;
04396483 1870
6d7dc5b9 1871 if (!CALL_P (insn) || !SIBLING_CALL_P (insn))
04396483 1872 {
18d50ae6 1873 crtl->all_throwers_are_sibcalls = 0;
2a1990e9 1874 return 0;
04396483 1875 }
1876 }
1877
edb7afe8 1878 for (insn = crtl->epilogue_delay_list; insn;
df4b504c 1879 insn = XEXP (insn, 1))
04396483 1880 if (can_throw_external (insn))
1881 {
b0d29d1c 1882 crtl->nothrow = 0;
b9cf3f63 1883
6d7dc5b9 1884 if (!CALL_P (insn) || !SIBLING_CALL_P (insn))
04396483 1885 {
18d50ae6 1886 crtl->all_throwers_are_sibcalls = 0;
2a1990e9 1887 return 0;
04396483 1888 }
1889 }
b0d29d1c 1890 if (crtl->nothrow
a883441f 1891 && (cgraph_function_body_availability (cgraph_node
1892 (current_function_decl))
b0d29d1c 1893 >= AVAIL_AVAILABLE))
a883441f 1894 {
17b28e52 1895 struct cgraph_node *node = cgraph_node (current_function_decl);
1896 struct cgraph_edge *e;
1897 for (e = node->callers; e; e = e->next_caller)
1898 e->can_throw_external = false;
16fcf0f4 1899 cgraph_set_nothrow_flag (node, true);
a883441f 1900
1901 if (dump_file)
1902 fprintf (dump_file, "Marking function nothrow: %s\n\n",
1903 current_function_name ());
1904 }
2a1990e9 1905 return 0;
da5038a3 1906}
df4b504c 1907
20099e35 1908struct rtl_opt_pass pass_set_nothrow_function_flags =
77fce4cd 1909{
20099e35 1910 {
1911 RTL_PASS,
a883441f 1912 "nothrow", /* name */
77fce4cd 1913 NULL, /* gate */
1914 set_nothrow_function_flags, /* execute */
1915 NULL, /* sub */
1916 NULL, /* next */
1917 0, /* static_pass_number */
0b1615c1 1918 TV_NONE, /* tv_id */
77fce4cd 1919 0, /* properties_required */
1920 0, /* properties_provided */
1921 0, /* properties_destroyed */
1922 0, /* todo_flags_start */
a883441f 1923 TODO_dump_func, /* todo_flags_finish */
20099e35 1924 }
77fce4cd 1925};
1926
e38def9c 1927\f
1928/* Various hooks for unwind library. */
1929
1930/* Expand the EH support builtin functions:
1931 __builtin_eh_pointer and __builtin_eh_filter. */
1932
1933static eh_region
1934expand_builtin_eh_common (tree region_nr_t)
1935{
1936 HOST_WIDE_INT region_nr;
1937 eh_region region;
1938
1939 gcc_assert (host_integerp (region_nr_t, 0));
1940 region_nr = tree_low_cst (region_nr_t, 0);
1941
1942 region = VEC_index (eh_region, cfun->eh->region_array, region_nr);
1943
1944 /* ??? We shouldn't have been able to delete a eh region without
1945 deleting all the code that depended on it. */
1946 gcc_assert (region != NULL);
1947
1948 return region;
1949}
1950
1951/* Expand to the exc_ptr value from the given eh region. */
1952
1953rtx
1954expand_builtin_eh_pointer (tree exp)
1955{
1956 eh_region region
1957 = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 0));
1958 if (region->exc_ptr_reg == NULL)
1959 region->exc_ptr_reg = gen_reg_rtx (ptr_mode);
1960 return region->exc_ptr_reg;
1961}
1962
1963/* Expand to the filter value from the given eh region. */
1964
1965rtx
1966expand_builtin_eh_filter (tree exp)
1967{
1968 eh_region region
1969 = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 0));
1970 if (region->filter_reg == NULL)
1971 region->filter_reg = gen_reg_rtx (targetm.eh_return_filter_mode ());
1972 return region->filter_reg;
1973}
1974
1975/* Copy the exc_ptr and filter values from one landing pad's registers
1976 to another. This is used to inline the resx statement. */
1977
1978rtx
1979expand_builtin_eh_copy_values (tree exp)
1980{
1981 eh_region dst
1982 = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 0));
1983 eh_region src
1984 = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 1));
1985 enum machine_mode fmode = targetm.eh_return_filter_mode ();
1986
1987 if (dst->exc_ptr_reg == NULL)
1988 dst->exc_ptr_reg = gen_reg_rtx (ptr_mode);
1989 if (src->exc_ptr_reg == NULL)
1990 src->exc_ptr_reg = gen_reg_rtx (ptr_mode);
1991
1992 if (dst->filter_reg == NULL)
1993 dst->filter_reg = gen_reg_rtx (fmode);
1994 if (src->filter_reg == NULL)
1995 src->filter_reg = gen_reg_rtx (fmode);
1996
1997 emit_move_insn (dst->exc_ptr_reg, src->exc_ptr_reg);
1998 emit_move_insn (dst->filter_reg, src->filter_reg);
1999
2000 return const0_rtx;
2001}
447a9eb9 2002
2003/* Do any necessary initialization to access arbitrary stack frames.
2004 On the SPARC, this means flushing the register windows. */
2005
2006void
35cb5232 2007expand_builtin_unwind_init (void)
447a9eb9 2008{
2009 /* Set this so all the registers get saved in our frame; we need to be
6312a35e 2010 able to copy the saved values for any registers from frames we unwind. */
18d50ae6 2011 crtl->saves_all_registers = 1;
447a9eb9 2012
2013#ifdef SETUP_FRAME_ADDRESSES
2014 SETUP_FRAME_ADDRESSES ();
2015#endif
2016}
2017
e38def9c 2018/* Map a non-negative number to an eh return data register number; expands
2019 to -1 if no return data register is associated with the input number.
2020 At least the inputs 0 and 1 must be mapped; the target may provide more. */
2021
df4b504c 2022rtx
c2f47e15 2023expand_builtin_eh_return_data_regno (tree exp)
df4b504c 2024{
c2f47e15 2025 tree which = CALL_EXPR_ARG (exp, 0);
df4b504c 2026 unsigned HOST_WIDE_INT iwhich;
2027
2028 if (TREE_CODE (which) != INTEGER_CST)
2029 {
eb586f2c 2030 error ("argument of %<__builtin_eh_return_regno%> must be constant");
df4b504c 2031 return constm1_rtx;
2032 }
2033
2034 iwhich = tree_low_cst (which, 1);
2035 iwhich = EH_RETURN_DATA_REGNO (iwhich);
2036 if (iwhich == INVALID_REGNUM)
2037 return constm1_rtx;
2038
2039#ifdef DWARF_FRAME_REGNUM
2040 iwhich = DWARF_FRAME_REGNUM (iwhich);
2041#else
2042 iwhich = DBX_REGISTER_NUMBER (iwhich);
2043#endif
2044
1ed5443b 2045 return GEN_INT (iwhich);
df4b504c 2046}
2047
447a9eb9 2048/* Given a value extracted from the return address register or stack slot,
2049 return the actual address encoded in that value. */
2050
2051rtx
35cb5232 2052expand_builtin_extract_return_addr (tree addr_tree)
447a9eb9 2053{
1db6d067 2054 rtx addr = expand_expr (addr_tree, NULL_RTX, Pmode, EXPAND_NORMAL);
df4b504c 2055
52a14611 2056 if (GET_MODE (addr) != Pmode
2057 && GET_MODE (addr) != VOIDmode)
2058 {
2059#ifdef POINTERS_EXTEND_UNSIGNED
2060 addr = convert_memory_address (Pmode, addr);
2061#else
2062 addr = convert_to_mode (Pmode, addr, 0);
2063#endif
2064 }
2065
df4b504c 2066 /* First mask out any unwanted bits. */
2067#ifdef MASK_RETURN_ADDR
6de9716c 2068 expand_and (Pmode, addr, MASK_RETURN_ADDR, addr);
df4b504c 2069#endif
2070
2071 /* Then adjust to find the real return address. */
2072#if defined (RETURN_ADDR_OFFSET)
2073 addr = plus_constant (addr, RETURN_ADDR_OFFSET);
2074#endif
2075
2076 return addr;
447a9eb9 2077}
2078
2079/* Given an actual address in addr_tree, do any necessary encoding
2080 and return the value to be stored in the return address register or
2081 stack slot so the epilogue will return to that address. */
2082
2083rtx
35cb5232 2084expand_builtin_frob_return_addr (tree addr_tree)
447a9eb9 2085{
1db6d067 2086 rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, EXPAND_NORMAL);
df4b504c 2087
85d654dd 2088 addr = convert_memory_address (Pmode, addr);
be275a4a 2089
447a9eb9 2090#ifdef RETURN_ADDR_OFFSET
df4b504c 2091 addr = force_reg (Pmode, addr);
447a9eb9 2092 addr = plus_constant (addr, -RETURN_ADDR_OFFSET);
2093#endif
df4b504c 2094
447a9eb9 2095 return addr;
2096}
2097
df4b504c 2098/* Set up the epilogue with the magic bits we'll need to return to the
2099 exception handler. */
447a9eb9 2100
df4b504c 2101void
35cb5232 2102expand_builtin_eh_return (tree stackadj_tree ATTRIBUTE_UNUSED,
2103 tree handler_tree)
447a9eb9 2104{
cd4e2223 2105 rtx tmp;
447a9eb9 2106
cd4e2223 2107#ifdef EH_RETURN_STACKADJ_RTX
4dd3de56 2108 tmp = expand_expr (stackadj_tree, crtl->eh.ehr_stackadj,
1db6d067 2109 VOIDmode, EXPAND_NORMAL);
85d654dd 2110 tmp = convert_memory_address (Pmode, tmp);
4dd3de56 2111 if (!crtl->eh.ehr_stackadj)
2112 crtl->eh.ehr_stackadj = copy_to_reg (tmp);
2113 else if (tmp != crtl->eh.ehr_stackadj)
2114 emit_move_insn (crtl->eh.ehr_stackadj, tmp);
be275a4a 2115#endif
2116
4dd3de56 2117 tmp = expand_expr (handler_tree, crtl->eh.ehr_handler,
1db6d067 2118 VOIDmode, EXPAND_NORMAL);
85d654dd 2119 tmp = convert_memory_address (Pmode, tmp);
4dd3de56 2120 if (!crtl->eh.ehr_handler)
2121 crtl->eh.ehr_handler = copy_to_reg (tmp);
2122 else if (tmp != crtl->eh.ehr_handler)
2123 emit_move_insn (crtl->eh.ehr_handler, tmp);
447a9eb9 2124
4dd3de56 2125 if (!crtl->eh.ehr_label)
2126 crtl->eh.ehr_label = gen_label_rtx ();
2127 emit_jump (crtl->eh.ehr_label);
173f0bec 2128}
2129
e38def9c 2130/* Expand __builtin_eh_return. This exit path from the function loads up
2131 the eh return data registers, adjusts the stack, and branches to a
2132 given PC other than the normal return address. */
2133
ec37ccb4 2134void
35cb5232 2135expand_eh_return (void)
447a9eb9 2136{
cd4e2223 2137 rtx around_label;
447a9eb9 2138
4dd3de56 2139 if (! crtl->eh.ehr_label)
ec37ccb4 2140 return;
447a9eb9 2141
18d50ae6 2142 crtl->calls_eh_return = 1;
447a9eb9 2143
cd4e2223 2144#ifdef EH_RETURN_STACKADJ_RTX
2145 emit_move_insn (EH_RETURN_STACKADJ_RTX, const0_rtx);
2146#endif
2147
df4b504c 2148 around_label = gen_label_rtx ();
df4b504c 2149 emit_jump (around_label);
447a9eb9 2150
4dd3de56 2151 emit_label (crtl->eh.ehr_label);
df4b504c 2152 clobber_return_register ();
447a9eb9 2153
cd4e2223 2154#ifdef EH_RETURN_STACKADJ_RTX
4dd3de56 2155 emit_move_insn (EH_RETURN_STACKADJ_RTX, crtl->eh.ehr_stackadj);
cd4e2223 2156#endif
2157
df4b504c 2158#ifdef HAVE_eh_return
2159 if (HAVE_eh_return)
4dd3de56 2160 emit_insn (gen_eh_return (crtl->eh.ehr_handler));
df4b504c 2161 else
ec37ccb4 2162#endif
df4b504c 2163 {
cd4e2223 2164#ifdef EH_RETURN_HANDLER_RTX
4dd3de56 2165 emit_move_insn (EH_RETURN_HANDLER_RTX, crtl->eh.ehr_handler);
cd4e2223 2166#else
2167 error ("__builtin_eh_return not supported on this target");
2168#endif
df4b504c 2169 }
ec37ccb4 2170
df4b504c 2171 emit_label (around_label);
ec37ccb4 2172}
26093bf4 2173
2174/* Convert a ptr_mode address ADDR_TREE to a Pmode address controlled by
2175 POINTERS_EXTEND_UNSIGNED and return it. */
2176
2177rtx
2178expand_builtin_extend_pointer (tree addr_tree)
2179{
1db6d067 2180 rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, EXPAND_NORMAL);
26093bf4 2181 int extend;
2182
2183#ifdef POINTERS_EXTEND_UNSIGNED
2184 extend = POINTERS_EXTEND_UNSIGNED;
2185#else
2186 /* The previous EH code did an unsigned extend by default, so we do this also
2187 for consistency. */
2188 extend = 1;
2189#endif
2190
1bd43494 2191 return convert_modes (targetm.unwind_word_mode (), ptr_mode, addr, extend);
26093bf4 2192}
cac66fd5 2193\f
98668546 2194/* In the following functions, we represent entries in the action table
4a82352a 2195 as 1-based indices. Special cases are:
98668546 2196
2197 0: null action record, non-null landing pad; implies cleanups
2198 -1: null action record, null landing pad; implies no action
2199 -2: no call-site entry; implies must_not_throw
2200 -3: we have yet to process outer regions
2201
2202 Further, no special cases apply to the "next" field of the record.
2203 For next, 0 means end of list. */
2204
df4b504c 2205struct action_record
2206{
2207 int offset;
2208 int filter;
2209 int next;
2210};
cac66fd5 2211
df4b504c 2212static int
35cb5232 2213action_record_eq (const void *pentry, const void *pdata)
df4b504c 2214{
2215 const struct action_record *entry = (const struct action_record *) pentry;
2216 const struct action_record *data = (const struct action_record *) pdata;
2217 return entry->filter == data->filter && entry->next == data->next;
2218}
cac66fd5 2219
df4b504c 2220static hashval_t
35cb5232 2221action_record_hash (const void *pentry)
df4b504c 2222{
2223 const struct action_record *entry = (const struct action_record *) pentry;
2224 return entry->next * 1009 + entry->filter;
2225}
cac66fd5 2226
df4b504c 2227static int
35cb5232 2228add_action_record (htab_t ar_hash, int filter, int next)
cac66fd5 2229{
6659485c 2230 struct action_record **slot, *new_ar, tmp;
df4b504c 2231
2232 tmp.filter = filter;
2233 tmp.next = next;
2234 slot = (struct action_record **) htab_find_slot (ar_hash, &tmp, INSERT);
cac66fd5 2235
6659485c 2236 if ((new_ar = *slot) == NULL)
cac66fd5 2237 {
6659485c 2238 new_ar = XNEW (struct action_record);
e38def9c 2239 new_ar->offset = VEC_length (uchar, crtl->eh.action_record_data) + 1;
6659485c 2240 new_ar->filter = filter;
2241 new_ar->next = next;
2242 *slot = new_ar;
df4b504c 2243
2244 /* The filter value goes in untouched. The link to the next
2245 record is a "self-relative" byte offset, or zero to indicate
2246 that there is no next record. So convert the absolute 1 based
4a82352a 2247 indices we've been carrying around into a displacement. */
df4b504c 2248
4dd3de56 2249 push_sleb128 (&crtl->eh.action_record_data, filter);
df4b504c 2250 if (next)
e38def9c 2251 next -= VEC_length (uchar, crtl->eh.action_record_data) + 1;
4dd3de56 2252 push_sleb128 (&crtl->eh.action_record_data, next);
cac66fd5 2253 }
cac66fd5 2254
6659485c 2255 return new_ar->offset;
df4b504c 2256}
cac66fd5 2257
df4b504c 2258static int
e38def9c 2259collect_one_action_chain (htab_t ar_hash, eh_region region)
cac66fd5 2260{
df4b504c 2261 int next;
cac66fd5 2262
df4b504c 2263 /* If we've reached the top of the region chain, then we have
2264 no actions, and require no landing pad. */
2265 if (region == NULL)
2266 return -1;
2267
2268 switch (region->type)
cac66fd5 2269 {
df4b504c 2270 case ERT_CLEANUP:
e38def9c 2271 {
2272 eh_region r;
2273 /* A cleanup adds a zero filter to the beginning of the chain, but
2274 there are special cases to look out for. If there are *only*
2275 cleanups along a path, then it compresses to a zero action.
2276 Further, if there are multiple cleanups along a path, we only
2277 need to represent one of them, as that is enough to trigger
2278 entry to the landing pad at runtime. */
2279 next = collect_one_action_chain (ar_hash, region->outer);
2280 if (next <= 0)
2281 return 0;
2282 for (r = region->outer; r ; r = r->outer)
2283 if (r->type == ERT_CLEANUP)
2284 return next;
2285 return add_action_record (ar_hash, 0, next);
2286 }
df4b504c 2287
2288 case ERT_TRY:
e38def9c 2289 {
2290 eh_catch c;
2291
2292 /* Process the associated catch regions in reverse order.
2293 If there's a catch-all handler, then we don't need to
2294 search outer regions. Use a magic -3 value to record
2295 that we haven't done the outer search. */
2296 next = -3;
2297 for (c = region->u.eh_try.last_catch; c ; c = c->prev_catch)
2298 {
2299 if (c->type_list == NULL)
2300 {
2301 /* Retrieve the filter from the head of the filter list
2302 where we have stored it (see assign_filter_values). */
2303 int filter = TREE_INT_CST_LOW (TREE_VALUE (c->filter_list));
2304 next = add_action_record (ar_hash, filter, 0);
2305 }
2306 else
2307 {
2308 /* Once the outer search is done, trigger an action record for
2309 each filter we have. */
2310 tree flt_node;
5c15c916 2311
e38def9c 2312 if (next == -3)
2313 {
2314 next = collect_one_action_chain (ar_hash, region->outer);
2315
2316 /* If there is no next action, terminate the chain. */
2317 if (next == -1)
2318 next = 0;
2319 /* If all outer actions are cleanups or must_not_throw,
2320 we'll have no action record for it, since we had wanted
2321 to encode these states in the call-site record directly.
2322 Add a cleanup action to the chain to catch these. */
2323 else if (next <= 0)
2324 next = add_action_record (ar_hash, 0, 0);
2325 }
1ed5443b 2326
e38def9c 2327 flt_node = c->filter_list;
2328 for (; flt_node; flt_node = TREE_CHAIN (flt_node))
2329 {
2330 int filter = TREE_INT_CST_LOW (TREE_VALUE (flt_node));
2331 next = add_action_record (ar_hash, filter, next);
2332 }
2333 }
2334 }
2335 return next;
2336 }
df4b504c 2337
2338 case ERT_ALLOWED_EXCEPTIONS:
2339 /* An exception specification adds its filter to the
2340 beginning of the chain. */
2341 next = collect_one_action_chain (ar_hash, region->outer);
3ec8c2a1 2342
2343 /* If there is no next action, terminate the chain. */
2344 if (next == -1)
2345 next = 0;
2346 /* If all outer actions are cleanups or must_not_throw,
2347 we'll have no action record for it, since we had wanted
2348 to encode these states in the call-site record directly.
2349 Add a cleanup action to the chain to catch these. */
2350 else if (next <= 0)
2351 next = add_action_record (ar_hash, 0, 0);
b215c058 2352
3ec8c2a1 2353 return add_action_record (ar_hash, region->u.allowed.filter, next);
df4b504c 2354
2355 case ERT_MUST_NOT_THROW:
2356 /* A must-not-throw region with no inner handlers or cleanups
2357 requires no call-site entry. Note that this differs from
2358 the no handler or cleanup case in that we do require an lsda
2359 to be generated. Return a magic -2 value to record this. */
2360 return -2;
cac66fd5 2361 }
e38def9c 2362
2363 gcc_unreachable ();
cac66fd5 2364}
2365
df4b504c 2366static int
33b1ed32 2367add_call_site (rtx landing_pad, int action, int section)
cac66fd5 2368{
4dd3de56 2369 call_site_record record;
e38def9c 2370
26dbec0a 2371 record = GGC_NEW (struct call_site_record_d);
4dd3de56 2372 record->landing_pad = landing_pad;
2373 record->action = action;
cac66fd5 2374
33b1ed32 2375 VEC_safe_push (call_site_record, gc,
2376 crtl->eh.call_site_record[section], record);
cac66fd5 2377
33b1ed32 2378 return call_site_base + VEC_length (call_site_record,
2379 crtl->eh.call_site_record[section]) - 1;
cac66fd5 2380}
2381
df4b504c 2382/* Turn REG_EH_REGION notes back into NOTE_INSN_EH_REGION notes.
2383 The new note numbers will not refer to region numbers, but
2384 instead to call site entries. */
cac66fd5 2385
e38def9c 2386static unsigned int
35cb5232 2387convert_to_eh_region_ranges (void)
cac66fd5 2388{
df4b504c 2389 rtx insn, iter, note;
2390 htab_t ar_hash;
2391 int last_action = -3;
2392 rtx last_action_insn = NULL_RTX;
2393 rtx last_landing_pad = NULL_RTX;
2394 rtx first_no_action_insn = NULL_RTX;
97b330ca 2395 int call_site = 0;
33b1ed32 2396 int cur_sec = 0;
2397 rtx section_switch_note = NULL_RTX;
2398 rtx first_no_action_insn_before_switch = NULL_RTX;
2399 rtx last_no_action_insn_before_switch = NULL_RTX;
2400 rtx *pad_map = NULL;
2401 sbitmap pad_loc = NULL;
2402 int min_labelno = 0, max_labelno = 0;
2403 int saved_call_site_base = call_site_base;
cac66fd5 2404
e38def9c 2405 crtl->eh.action_record_data = VEC_alloc (uchar, gc, 64);
cac66fd5 2406
df4b504c 2407 ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
cac66fd5 2408
df4b504c 2409 for (iter = get_insns (); iter ; iter = NEXT_INSN (iter))
2410 if (INSN_P (iter))
2411 {
e38def9c 2412 eh_landing_pad lp;
2413 eh_region region;
2414 bool nothrow;
df4b504c 2415 int this_action;
2416 rtx this_landing_pad;
cac66fd5 2417
df4b504c 2418 insn = iter;
6d7dc5b9 2419 if (NONJUMP_INSN_P (insn)
df4b504c 2420 && GET_CODE (PATTERN (insn)) == SEQUENCE)
2421 insn = XVECEXP (PATTERN (insn), 0, 0);
da5038a3 2422
e38def9c 2423 nothrow = get_eh_region_and_lp_from_rtx (insn, &region, &lp);
2424 if (nothrow)
2425 continue;
2426 if (region)
2427 this_action = collect_one_action_chain (ar_hash, region);
df4b504c 2428 else
e38def9c 2429 this_action = -1;
df4b504c 2430
2431 /* Existence of catch handlers, or must-not-throw regions
2432 implies that an lsda is needed (even if empty). */
2433 if (this_action != -1)
18d50ae6 2434 crtl->uses_eh_lsda = 1;
df4b504c 2435
2436 /* Delay creation of region notes for no-action regions
2437 until we're sure that an lsda will be required. */
2438 else if (last_action == -3)
2439 {
2440 first_no_action_insn = iter;
2441 last_action = -1;
2442 }
da5038a3 2443
df4b504c 2444 if (this_action >= 0)
e38def9c 2445 this_landing_pad = lp->landing_pad;
df4b504c 2446 else
2447 this_landing_pad = NULL_RTX;
da5038a3 2448
df4b504c 2449 /* Differing actions or landing pads implies a change in call-site
2450 info, which implies some EH_REGION note should be emitted. */
2451 if (last_action != this_action
2452 || last_landing_pad != this_landing_pad)
2453 {
2454 /* If we'd not seen a previous action (-3) or the previous
2455 action was must-not-throw (-2), then we do not need an
2456 end note. */
2457 if (last_action >= -1)
2458 {
2459 /* If we delayed the creation of the begin, do it now. */
33b1ed32 2460 if (first_no_action_insn_before_switch)
2461 {
2462 call_site = add_call_site (NULL_RTX, 0, 0);
2463 note
2464 = emit_note_before (NOTE_INSN_EH_REGION_BEG,
2465 first_no_action_insn_before_switch);
2466 NOTE_EH_HANDLER (note) = call_site;
2467 if (first_no_action_insn)
2468 {
2469 note
2470 = emit_note_after (NOTE_INSN_EH_REGION_END,
2471 last_no_action_insn_before_switch);
2472 NOTE_EH_HANDLER (note) = call_site;
2473 }
2474 else
2475 gcc_assert (last_action_insn
2476 == last_no_action_insn_before_switch);
2477 }
df4b504c 2478 if (first_no_action_insn)
2479 {
33b1ed32 2480 call_site = add_call_site (NULL_RTX, 0, cur_sec);
df4b504c 2481 note = emit_note_before (NOTE_INSN_EH_REGION_BEG,
2482 first_no_action_insn);
2483 NOTE_EH_HANDLER (note) = call_site;
2484 first_no_action_insn = NULL_RTX;
2485 }
2486
2487 note = emit_note_after (NOTE_INSN_EH_REGION_END,
2488 last_action_insn);
2489 NOTE_EH_HANDLER (note) = call_site;
2490 }
2491
2492 /* If the new action is must-not-throw, then no region notes
2493 are created. */
2494 if (this_action >= -1)
2495 {
1ed5443b 2496 call_site = add_call_site (this_landing_pad,
33b1ed32 2497 this_action < 0 ? 0 : this_action,
2498 cur_sec);
df4b504c 2499 note = emit_note_before (NOTE_INSN_EH_REGION_BEG, iter);
2500 NOTE_EH_HANDLER (note) = call_site;
2501 }
2502
2503 last_action = this_action;
2504 last_landing_pad = this_landing_pad;
2505 }
2506 last_action_insn = iter;
2507 }
33b1ed32 2508 else if (NOTE_P (iter)
2509 && NOTE_KIND (iter) == NOTE_INSN_SWITCH_TEXT_SECTIONS)
2510 {
2511 gcc_assert (section_switch_note == NULL_RTX);
2512 gcc_assert (flag_reorder_blocks_and_partition);
2513 section_switch_note = iter;
2514 if (first_no_action_insn)
2515 {
2516 first_no_action_insn_before_switch = first_no_action_insn;
2517 last_no_action_insn_before_switch = last_action_insn;
2518 first_no_action_insn = NULL_RTX;
2519 gcc_assert (last_action == -1);
2520 last_action = -3;
2521 }
2522 /* Force closing of current EH region before section switch and
2523 opening a new one afterwards. */
2524 else if (last_action != -3)
2525 last_landing_pad = pc_rtx;
2526 call_site_base += VEC_length (call_site_record,
2527 crtl->eh.call_site_record[cur_sec]);
2528 cur_sec++;
2529 gcc_assert (crtl->eh.call_site_record[cur_sec] == NULL);
2530 crtl->eh.call_site_record[cur_sec]
2531 = VEC_alloc (call_site_record, gc, 10);
2532 max_labelno = max_label_num ();
2533 min_labelno = get_first_label_num ();
2534 pad_map = XCNEWVEC (rtx, max_labelno - min_labelno + 1);
2535 pad_loc = sbitmap_alloc (max_labelno - min_labelno + 1);
2536 }
2537 else if (LABEL_P (iter) && pad_map)
2538 SET_BIT (pad_loc, CODE_LABEL_NUMBER (iter) - min_labelno);
da5038a3 2539
df4b504c 2540 if (last_action >= -1 && ! first_no_action_insn)
da5038a3 2541 {
df4b504c 2542 note = emit_note_after (NOTE_INSN_EH_REGION_END, last_action_insn);
2543 NOTE_EH_HANDLER (note) = call_site;
da5038a3 2544 }
2545
33b1ed32 2546 call_site_base = saved_call_site_base;
2547
2548 if (pad_map)
2549 {
2550 /* When doing hot/cold partitioning, ensure landing pads are
2551 always in the same section as the EH region, .gcc_except_table
2552 can't express it otherwise. */
2553 for (cur_sec = 0; cur_sec < 2; cur_sec++)
2554 {
2555 int i, idx;
2556 int n = VEC_length (call_site_record,
2557 crtl->eh.call_site_record[cur_sec]);
2558 basic_block prev_bb = NULL, padbb;
2559
2560 for (i = 0; i < n; ++i)
2561 {
2562 struct call_site_record_d *cs =
2563 VEC_index (call_site_record,
2564 crtl->eh.call_site_record[cur_sec], i);
2565 rtx jump, note;
2566
2567 if (cs->landing_pad == NULL_RTX)
2568 continue;
2569 idx = CODE_LABEL_NUMBER (cs->landing_pad) - min_labelno;
2570 /* If the landing pad is in the correct section, nothing
2571 is needed. */
2572 if (TEST_BIT (pad_loc, idx) ^ (cur_sec == 0))
2573 continue;
2574 /* Otherwise, if we haven't seen this pad yet, we need to
2575 add a new label and jump to the correct section. */
2576 if (pad_map[idx] == NULL_RTX)
2577 {
2578 pad_map[idx] = gen_label_rtx ();
2579 if (prev_bb == NULL)
2580 for (iter = section_switch_note;
2581 iter; iter = PREV_INSN (iter))
2582 if (NOTE_INSN_BASIC_BLOCK_P (iter))
2583 {
2584 prev_bb = NOTE_BASIC_BLOCK (iter);
2585 break;
2586 }
2587 if (cur_sec == 0)
2588 {
2589 note = emit_label_before (pad_map[idx],
2590 section_switch_note);
2591 jump = emit_jump_insn_before (gen_jump (cs->landing_pad),
2592 section_switch_note);
2593 }
2594 else
2595 {
2596 jump = emit_jump_insn_after (gen_jump (cs->landing_pad),
2597 section_switch_note);
2598 note = emit_label_after (pad_map[idx],
2599 section_switch_note);
2600 }
2601 JUMP_LABEL (jump) = cs->landing_pad;
2602 add_reg_note (jump, REG_CROSSING_JUMP, NULL_RTX);
2603 iter = NEXT_INSN (cs->landing_pad);
2604 if (iter && NOTE_INSN_BASIC_BLOCK_P (iter))
2605 padbb = NOTE_BASIC_BLOCK (iter);
2606 else
2607 padbb = NULL;
2608 if (padbb && prev_bb
2609 && BB_PARTITION (padbb) != BB_UNPARTITIONED)
2610 {
2611 basic_block bb;
2612 int part
2613 = BB_PARTITION (padbb) == BB_COLD_PARTITION
2614 ? BB_HOT_PARTITION : BB_COLD_PARTITION;
2615 edge_iterator ei;
2616 edge e;
2617
2618 bb = create_basic_block (note, jump, prev_bb);
2619 make_single_succ_edge (bb, padbb, EDGE_CROSSING);
2620 BB_SET_PARTITION (bb, part);
2621 for (ei = ei_start (padbb->preds);
2622 (e = ei_safe_edge (ei)); )
2623 {
2624 if ((e->flags & (EDGE_EH|EDGE_CROSSING))
2625 == (EDGE_EH|EDGE_CROSSING))
2626 {
2627 redirect_edge_succ (e, bb);
2628 e->flags &= ~EDGE_CROSSING;
2629 }
2630 else
2631 ei_next (&ei);
2632 }
2633 if (cur_sec == 0)
2634 prev_bb = bb;
2635 }
2636 }
2637 cs->landing_pad = pad_map[idx];
2638 }
2639 }
2640
2641 sbitmap_free (pad_loc);
2642 XDELETEVEC (pad_map);
2643 }
2644
df4b504c 2645 htab_delete (ar_hash);
2a1990e9 2646 return 0;
df4b504c 2647}
da5038a3 2648
e38def9c 2649static bool
2650gate_convert_to_eh_region_ranges (void)
2651{
2652 /* Nothing to do for SJLJ exceptions or if no regions created. */
2653 return !(USING_SJLJ_EXCEPTIONS || cfun->eh->region_tree == NULL);
2654}
2655
20099e35 2656struct rtl_opt_pass pass_convert_to_eh_region_ranges =
77fce4cd 2657{
20099e35 2658 {
2659 RTL_PASS,
b85ccd2c 2660 "eh_ranges", /* name */
e38def9c 2661 gate_convert_to_eh_region_ranges, /* gate */
77fce4cd 2662 convert_to_eh_region_ranges, /* execute */
2663 NULL, /* sub */
2664 NULL, /* next */
2665 0, /* static_pass_number */
0b1615c1 2666 TV_NONE, /* tv_id */
77fce4cd 2667 0, /* properties_required */
2668 0, /* properties_provided */
2669 0, /* properties_destroyed */
2670 0, /* todo_flags_start */
228967a9 2671 TODO_dump_func, /* todo_flags_finish */
20099e35 2672 }
77fce4cd 2673};
df4b504c 2674\f
2675static void
e38def9c 2676push_uleb128 (VEC (uchar, gc) **data_area, unsigned int value)
df4b504c 2677{
2678 do
2679 {
2680 unsigned char byte = value & 0x7f;
2681 value >>= 7;
2682 if (value)
2683 byte |= 0x80;
e38def9c 2684 VEC_safe_push (uchar, gc, *data_area, byte);
df4b504c 2685 }
2686 while (value);
2687}
da5038a3 2688
df4b504c 2689static void
e38def9c 2690push_sleb128 (VEC (uchar, gc) **data_area, int value)
df4b504c 2691{
2692 unsigned char byte;
2693 int more;
da5038a3 2694
df4b504c 2695 do
da5038a3 2696 {
df4b504c 2697 byte = value & 0x7f;
2698 value >>= 7;
2699 more = ! ((value == 0 && (byte & 0x40) == 0)
2700 || (value == -1 && (byte & 0x40) != 0));
2701 if (more)
2702 byte |= 0x80;
e38def9c 2703 VEC_safe_push (uchar, gc, *data_area, byte);
da5038a3 2704 }
df4b504c 2705 while (more);
2706}
da5038a3 2707
df4b504c 2708\f
df4b504c 2709#ifndef HAVE_AS_LEB128
2710static int
33b1ed32 2711dw2_size_of_call_site_table (int section)
da5038a3 2712{
33b1ed32 2713 int n = VEC_length (call_site_record, crtl->eh.call_site_record[section]);
df4b504c 2714 int size = n * (4 + 4 + 4);
2715 int i;
da5038a3 2716
df4b504c 2717 for (i = 0; i < n; ++i)
2718 {
26dbec0a 2719 struct call_site_record_d *cs =
33b1ed32 2720 VEC_index (call_site_record, crtl->eh.call_site_record[section], i);
df4b504c 2721 size += size_of_uleb128 (cs->action);
2722 }
8b4f3d64 2723
df4b504c 2724 return size;
2725}
2726
2727static int
35cb5232 2728sjlj_size_of_call_site_table (void)
df4b504c 2729{
33b1ed32 2730 int n = VEC_length (call_site_record, crtl->eh.call_site_record[0]);
df4b504c 2731 int size = 0;
2732 int i;
cac66fd5 2733
df4b504c 2734 for (i = 0; i < n; ++i)
da5038a3 2735 {
26dbec0a 2736 struct call_site_record_d *cs =
33b1ed32 2737 VEC_index (call_site_record, crtl->eh.call_site_record[0], i);
df4b504c 2738 size += size_of_uleb128 (INTVAL (cs->landing_pad));
2739 size += size_of_uleb128 (cs->action);
da5038a3 2740 }
df4b504c 2741
2742 return size;
2743}
2744#endif
2745
2746static void
33b1ed32 2747dw2_output_call_site_table (int cs_format, int section)
df4b504c 2748{
33b1ed32 2749 int n = VEC_length (call_site_record, crtl->eh.call_site_record[section]);
df4b504c 2750 int i;
33b1ed32 2751 const char *begin;
2752
2753 if (section == 0)
2754 begin = current_function_func_begin_label;
2755 else if (first_function_block_is_cold)
2756 begin = crtl->subsections.hot_section_label;
2757 else
2758 begin = crtl->subsections.cold_section_label;
df4b504c 2759
2760 for (i = 0; i < n; ++i)
da5038a3 2761 {
26dbec0a 2762 struct call_site_record_d *cs =
33b1ed32 2763 VEC_index (call_site_record, crtl->eh.call_site_record[section], i);
df4b504c 2764 char reg_start_lab[32];
2765 char reg_end_lab[32];
2766 char landing_pad_lab[32];
2767
2768 ASM_GENERATE_INTERNAL_LABEL (reg_start_lab, "LEHB", call_site_base + i);
2769 ASM_GENERATE_INTERNAL_LABEL (reg_end_lab, "LEHE", call_site_base + i);
2770
2771 if (cs->landing_pad)
2772 ASM_GENERATE_INTERNAL_LABEL (landing_pad_lab, "L",
2773 CODE_LABEL_NUMBER (cs->landing_pad));
2774
2775 /* ??? Perhaps use insn length scaling if the assembler supports
2776 generic arithmetic. */
2777 /* ??? Perhaps use attr_length to choose data1 or data2 instead of
2778 data4 if the function is small enough. */
33b1ed32 2779 if (cs_format == DW_EH_PE_uleb128)
2780 {
2781 dw2_asm_output_delta_uleb128 (reg_start_lab, begin,
2782 "region %d start", i);
2783 dw2_asm_output_delta_uleb128 (reg_end_lab, reg_start_lab,
2784 "length");
2785 if (cs->landing_pad)
2786 dw2_asm_output_delta_uleb128 (landing_pad_lab, begin,
2787 "landing pad");
2788 else
2789 dw2_asm_output_data_uleb128 (0, "landing pad");
2790 }
df4b504c 2791 else
33b1ed32 2792 {
2793 dw2_asm_output_delta (4, reg_start_lab, begin,
2794 "region %d start", i);
2795 dw2_asm_output_delta (4, reg_end_lab, reg_start_lab, "length");
2796 if (cs->landing_pad)
2797 dw2_asm_output_delta (4, landing_pad_lab, begin,
2798 "landing pad");
2799 else
2800 dw2_asm_output_data (4, 0, "landing pad");
2801 }
df4b504c 2802 dw2_asm_output_data_uleb128 (cs->action, "action");
da5038a3 2803 }
2804
df4b504c 2805 call_site_base += n;
2806}
2807
2808static void
35cb5232 2809sjlj_output_call_site_table (void)
df4b504c 2810{
33b1ed32 2811 int n = VEC_length (call_site_record, crtl->eh.call_site_record[0]);
df4b504c 2812 int i;
da5038a3 2813
df4b504c 2814 for (i = 0; i < n; ++i)
da5038a3 2815 {
26dbec0a 2816 struct call_site_record_d *cs =
33b1ed32 2817 VEC_index (call_site_record, crtl->eh.call_site_record[0], i);
b9cf3f63 2818
df4b504c 2819 dw2_asm_output_data_uleb128 (INTVAL (cs->landing_pad),
2820 "region %d landing pad", i);
2821 dw2_asm_output_data_uleb128 (cs->action, "action");
2822 }
b9cf3f63 2823
df4b504c 2824 call_site_base += n;
da5038a3 2825}
2826
7753ca4b 2827#ifndef TARGET_UNWIND_INFO
2943ce06 2828/* Switch to the section that should be used for exception tables. */
d5720b0c 2829
2943ce06 2830static void
0a011112 2831switch_to_exception_section (const char * ARG_UNUSED (fnname))
d5720b0c 2832{
f1d76faf 2833 section *s;
2834
2835 if (exception_section)
2836 s = exception_section;
2837 else
d5720b0c 2838 {
f1d76faf 2839 /* Compute the section and cache it into exception_section,
2840 unless it depends on the function name. */
2943ce06 2841 if (targetm.have_named_sections)
51601538 2842 {
2943ce06 2843 int flags;
2844
2845 if (EH_TABLES_CAN_BE_READ_ONLY)
2846 {
2847 int tt_format =
2848 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
2849 flags = ((! flag_pic
2850 || ((tt_format & 0x70) != DW_EH_PE_absptr
2851 && (tt_format & 0x70) != DW_EH_PE_aligned))
2852 ? 0 : SECTION_WRITE);
2853 }
2854 else
2855 flags = SECTION_WRITE;
f1d76faf 2856
0a011112 2857#ifdef HAVE_LD_EH_GC_SECTIONS
2858 if (flag_function_sections)
2859 {
2457c754 2860 char *section_name = XNEWVEC (char, strlen (fnname) + 32);
0a011112 2861 sprintf (section_name, ".gcc_except_table.%s", fnname);
f1d76faf 2862 s = get_section (section_name, flags, NULL);
0a011112 2863 free (section_name);
2864 }
2865 else
2866#endif
f1d76faf 2867 exception_section
2868 = s = get_section (".gcc_except_table", flags, NULL);
51601538 2869 }
2870 else
f1d76faf 2871 exception_section
2872 = s = flag_pic ? data_section : readonly_data_section;
d5720b0c 2873 }
f1d76faf 2874
2875 switch_to_section (s);
d5720b0c 2876}
7753ca4b 2877#endif
d5720b0c 2878
1774763d 2879
2880/* Output a reference from an exception table to the type_info object TYPE.
554f2707 2881 TT_FORMAT and TT_FORMAT_SIZE describe the DWARF encoding method used for
1774763d 2882 the value. */
2883
2884static void
2885output_ttype (tree type, int tt_format, int tt_format_size)
2886{
2887 rtx value;
6659485c 2888 bool is_public = true;
1774763d 2889
2890 if (type == NULL_TREE)
2891 value = const0_rtx;
2892 else
2893 {
1d416bd7 2894 struct varpool_node *node;
1774763d 2895
7bfefa9d 2896 /* FIXME lto. pass_ipa_free_lang_data changes all types to
2897 runtime types so TYPE should already be a runtime type
2898 reference. When pass_ipa_free_lang data is made a default
2899 pass, we can then remove the call to lookup_type_for_runtime
2900 below. */
2901 if (TYPE_P (type))
2902 type = lookup_type_for_runtime (type);
2903
3669a8aa 2904 value = expand_expr (type, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
1774763d 2905
2906 /* Let cgraph know that the rtti decl is used. Not all of the
2907 paths below go through assemble_integer, which would take
2908 care of this for us. */
2909 STRIP_NOPS (type);
2910 if (TREE_CODE (type) == ADDR_EXPR)
2911 {
2912 type = TREE_OPERAND (type, 0);
2913 if (TREE_CODE (type) == VAR_DECL)
2914 {
1d416bd7 2915 node = varpool_node (type);
1774763d 2916 if (node)
1d416bd7 2917 varpool_mark_needed_node (node);
6659485c 2918 is_public = TREE_PUBLIC (type);
1774763d 2919 }
2920 }
6276113b 2921 else
2922 gcc_assert (TREE_CODE (type) == INTEGER_CST);
1774763d 2923 }
2924
2925 /* Allow the target to override the type table entry format. */
2926 if (targetm.asm_out.ttype (value))
2927 return;
2928
2929 if (tt_format == DW_EH_PE_absptr || tt_format == DW_EH_PE_aligned)
2930 assemble_integer (value, tt_format_size,
2931 tt_format_size * BITS_PER_UNIT, 1);
2932 else
6659485c 2933 dw2_asm_output_encoded_addr_rtx (tt_format, value, is_public, NULL);
1774763d 2934}
2935
33b1ed32 2936static void
2937output_one_function_exception_table (const char * ARG_UNUSED (fnname),
58d82cd0 2938 int section, rtx ARG_UNUSED (personality))
df4b504c 2939{
e38def9c 2940 int tt_format, cs_format, lp_format, i;
df4b504c 2941#ifdef HAVE_AS_LEB128
2942 char ttype_label[32];
2943 char cs_after_size_label[32];
2944 char cs_end_label[32];
2945#else
2946 int call_site_len;
2947#endif
2948 int have_tt_data;
97b330ca 2949 int tt_format_size = 0;
da5038a3 2950
8ec87476 2951#ifdef TARGET_UNWIND_INFO
2952 /* TODO: Move this into target file. */
ad5818ae 2953 fputs ("\t.personality\t", asm_out_file);
58d82cd0 2954 output_addr_const (asm_out_file, personality);
ad5818ae 2955 fputs ("\n\t.handlerdata\n", asm_out_file);
2956 /* Note that varasm still thinks we're in the function's code section.
2957 The ".endp" directive that will immediately follow will take us back. */
2958#else
0a011112 2959 switch_to_exception_section (fnname);
ad5818ae 2960#endif
df4b504c 2961
b215c058 2962 /* If the target wants a label to begin the table, emit it here. */
2963 targetm.asm_out.except_table_label (asm_out_file);
2964
e38def9c 2965 have_tt_data = (VEC_length (tree, cfun->eh->ttype_data)
2966 || (targetm.arm_eabi_unwinder
2967 ? VEC_length (tree, cfun->eh->ehspec_data.arm_eabi)
2968 : VEC_length (uchar, cfun->eh->ehspec_data.other)));
df4b504c 2969
631413ff 2970 /* Indicate the format of the @TType entries. */
2971 if (! have_tt_data)
2972 tt_format = DW_EH_PE_omit;
2973 else
2974 {
2975 tt_format = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
2976#ifdef HAVE_AS_LEB128
33b1ed32 2977 ASM_GENERATE_INTERNAL_LABEL (ttype_label,
2978 section ? "LLSDATTC" : "LLSDATT",
4781f9b9 2979 current_function_funcdef_no);
631413ff 2980#endif
2981 tt_format_size = size_of_encoded_value (tt_format);
2982
4aa1f91c 2983 assemble_align (tt_format_size * BITS_PER_UNIT);
631413ff 2984 }
df4b504c 2985
33b1ed32 2986 targetm.asm_out.internal_label (asm_out_file, section ? "LLSDAC" : "LLSDA",
2987 current_function_funcdef_no);
df4b504c 2988
2989 /* The LSDA header. */
2990
2991 /* Indicate the format of the landing pad start pointer. An omitted
2992 field implies @LPStart == @Start. */
2993 /* Currently we always put @LPStart == @Start. This field would
2994 be most useful in moving the landing pads completely out of
2995 line to another section, but it could also be used to minimize
2996 the size of uleb128 landing pad offsets. */
ad5818ae 2997 lp_format = DW_EH_PE_omit;
2998 dw2_asm_output_data (1, lp_format, "@LPStart format (%s)",
2999 eh_data_format_name (lp_format));
df4b504c 3000
3001 /* @LPStart pointer would go here. */
3002
ad5818ae 3003 dw2_asm_output_data (1, tt_format, "@TType format (%s)",
3004 eh_data_format_name (tt_format));
df4b504c 3005
3006#ifndef HAVE_AS_LEB128
3007 if (USING_SJLJ_EXCEPTIONS)
3008 call_site_len = sjlj_size_of_call_site_table ();
3009 else
33b1ed32 3010 call_site_len = dw2_size_of_call_site_table (section);
df4b504c 3011#endif
3012
3013 /* A pc-relative 4-byte displacement to the @TType data. */
3014 if (have_tt_data)
3015 {
3016#ifdef HAVE_AS_LEB128
3017 char ttype_after_disp_label[32];
33b1ed32 3018 ASM_GENERATE_INTERNAL_LABEL (ttype_after_disp_label,
3019 section ? "LLSDATTDC" : "LLSDATTD",
4781f9b9 3020 current_function_funcdef_no);
df4b504c 3021 dw2_asm_output_delta_uleb128 (ttype_label, ttype_after_disp_label,
3022 "@TType base offset");
3023 ASM_OUTPUT_LABEL (asm_out_file, ttype_after_disp_label);
3024#else
3025 /* Ug. Alignment queers things. */
631413ff 3026 unsigned int before_disp, after_disp, last_disp, disp;
df4b504c 3027
df4b504c 3028 before_disp = 1 + 1;
3029 after_disp = (1 + size_of_uleb128 (call_site_len)
3030 + call_site_len
e38def9c 3031 + VEC_length (uchar, crtl->eh.action_record_data)
3032 + (VEC_length (tree, cfun->eh->ttype_data)
631413ff 3033 * tt_format_size));
df4b504c 3034
3035 disp = after_disp;
3036 do
da5038a3 3037 {
df4b504c 3038 unsigned int disp_size, pad;
3039
3040 last_disp = disp;
3041 disp_size = size_of_uleb128 (disp);
3042 pad = before_disp + disp_size + after_disp;
631413ff 3043 if (pad % tt_format_size)
3044 pad = tt_format_size - (pad % tt_format_size);
df4b504c 3045 else
3046 pad = 0;
3047 disp = after_disp + pad;
da5038a3 3048 }
df4b504c 3049 while (disp != last_disp);
3050
3051 dw2_asm_output_data_uleb128 (disp, "@TType base offset");
3052#endif
da5038a3 3053 }
da5038a3 3054
df4b504c 3055 /* Indicate the format of the call-site offsets. */
3056#ifdef HAVE_AS_LEB128
ad5818ae 3057 cs_format = DW_EH_PE_uleb128;
df4b504c 3058#else
ad5818ae 3059 cs_format = DW_EH_PE_udata4;
df4b504c 3060#endif
ad5818ae 3061 dw2_asm_output_data (1, cs_format, "call-site format (%s)",
3062 eh_data_format_name (cs_format));
df4b504c 3063
3064#ifdef HAVE_AS_LEB128
33b1ed32 3065 ASM_GENERATE_INTERNAL_LABEL (cs_after_size_label,
3066 section ? "LLSDACSBC" : "LLSDACSB",
4781f9b9 3067 current_function_funcdef_no);
33b1ed32 3068 ASM_GENERATE_INTERNAL_LABEL (cs_end_label,
3069 section ? "LLSDACSEC" : "LLSDACSE",
4781f9b9 3070 current_function_funcdef_no);
df4b504c 3071 dw2_asm_output_delta_uleb128 (cs_end_label, cs_after_size_label,
3072 "Call-site table length");
3073 ASM_OUTPUT_LABEL (asm_out_file, cs_after_size_label);
3074 if (USING_SJLJ_EXCEPTIONS)
3075 sjlj_output_call_site_table ();
3076 else
33b1ed32 3077 dw2_output_call_site_table (cs_format, section);
df4b504c 3078 ASM_OUTPUT_LABEL (asm_out_file, cs_end_label);
3079#else
33b1ed32 3080 dw2_asm_output_data_uleb128 (call_site_len, "Call-site table length");
df4b504c 3081 if (USING_SJLJ_EXCEPTIONS)
3082 sjlj_output_call_site_table ();
3083 else
33b1ed32 3084 dw2_output_call_site_table (cs_format, section);
df4b504c 3085#endif
3086
3087 /* ??? Decode and interpret the data for flag_debug_asm. */
e38def9c 3088 {
3089 uchar uc;
3090 for (i = 0; VEC_iterate (uchar, crtl->eh.action_record_data, i, uc); ++i)
3091 dw2_asm_output_data (1, uc, i ? NULL : "Action record table");
3092 }
da5038a3 3093
df4b504c 3094 if (have_tt_data)
4aa1f91c 3095 assemble_align (tt_format_size * BITS_PER_UNIT);
da5038a3 3096
e38def9c 3097 i = VEC_length (tree, cfun->eh->ttype_data);
df4b504c 3098 while (i-- > 0)
da5038a3 3099 {
e38def9c 3100 tree type = VEC_index (tree, cfun->eh->ttype_data, i);
1774763d 3101 output_ttype (type, tt_format, tt_format_size);
da5038a3 3102 }
df4b504c 3103
3104#ifdef HAVE_AS_LEB128
3105 if (have_tt_data)
3106 ASM_OUTPUT_LABEL (asm_out_file, ttype_label);
3107#endif
3108
3109 /* ??? Decode and interpret the data for flag_debug_asm. */
e38def9c 3110 if (targetm.arm_eabi_unwinder)
1774763d 3111 {
e38def9c 3112 tree type;
3113 for (i = 0;
3114 VEC_iterate (tree, cfun->eh->ehspec_data.arm_eabi, i, type); ++i)
3115 output_ttype (type, tt_format, tt_format_size);
3116 }
3117 else
3118 {
3119 uchar uc;
3120 for (i = 0;
3121 VEC_iterate (uchar, cfun->eh->ehspec_data.other, i, uc); ++i)
3122 dw2_asm_output_data (1, uc,
3123 i ? NULL : "Exception specification table");
1774763d 3124 }
33b1ed32 3125}
3126
3127void
3128output_function_exception_table (const char * ARG_UNUSED (fnname))
3129{
58d82cd0 3130 rtx personality = get_personality_function (current_function_decl);
3131
33b1ed32 3132 /* Not all functions need anything. */
3133 if (! crtl->uses_eh_lsda)
3134 return;
3135
58d82cd0 3136 if (personality)
3137 assemble_external_libcall (personality);
33b1ed32 3138
58d82cd0 3139 output_one_function_exception_table (fnname, 0, personality);
33b1ed32 3140 if (crtl->eh.call_site_record[1] != NULL)
58d82cd0 3141 output_one_function_exception_table (fnname, 1, personality);
df4b504c 3142
2f14b1f9 3143 switch_to_section (current_function_section ());
da5038a3 3144}
1f3233d1 3145
b3f1469f 3146void
3147set_eh_throw_stmt_table (struct function *fun, struct htab *table)
3148{
3149 fun->eh->throw_stmt_table = table;
3150}
3151
3152htab_t
3153get_eh_throw_stmt_table (struct function *fun)
3154{
3155 return fun->eh->throw_stmt_table;
3156}
e38def9c 3157\f
3158/* Determine if the function needs an EH personality function. */
58d82cd0 3159
3160enum eh_personality_kind
3161function_needs_eh_personality (struct function *fn)
3162{
58d82cd0 3163 enum eh_personality_kind kind = eh_personality_none;
e38def9c 3164 eh_region i;
58d82cd0 3165
e38def9c 3166 FOR_ALL_EH_REGION_FN (i, fn)
58d82cd0 3167 {
3168 switch (i->type)
3169 {
58d82cd0 3170 case ERT_CLEANUP:
3171 /* Can do with any personality including the generic C one. */
3172 kind = eh_personality_any;
3173 break;
3174
e38def9c 3175 case ERT_TRY:
58d82cd0 3176 case ERT_ALLOWED_EXCEPTIONS:
3177 /* Always needs a EH personality function. The generic C
3178 personality doesn't handle these even for empty type lists. */
3179 return eh_personality_lang;
3180
e38def9c 3181 case ERT_MUST_NOT_THROW:
3182 /* Always needs a EH personality function. The language may specify
3183 what abort routine that must be used, e.g. std::terminate. */
58d82cd0 3184 return eh_personality_lang;
3185 }
58d82cd0 3186 }
3187
3188 return kind;
3189}
e38def9c 3190\f
b4ba5e9d 3191/* Dump EH information to OUT. */
bb22691b 3192
b215c058 3193void
bb22691b 3194dump_eh_tree (FILE * out, struct function *fun)
b4ba5e9d 3195{
e38def9c 3196 eh_region i;
b4ba5e9d 3197 int depth = 0;
e38def9c 3198 static const char *const type_name[] = {
3199 "cleanup", "try", "allowed_exceptions", "must_not_throw"
3200 };
b4ba5e9d 3201
3202 i = fun->eh->region_tree;
bb22691b 3203 if (!i)
b4ba5e9d 3204 return;
3205
3206 fprintf (out, "Eh tree:\n");
3207 while (1)
3208 {
3209 fprintf (out, " %*s %i %s", depth * 2, "",
e38def9c 3210 i->index, type_name[(int) i->type]);
3211
3212 if (i->landing_pads)
98b64106 3213 {
e38def9c 3214 eh_landing_pad lp;
3215
3216 fprintf (out, " land:");
3217 if (current_ir_type () == IR_GIMPLE)
3218 {
3219 for (lp = i->landing_pads; lp ; lp = lp->next_lp)
3220 {
3221 fprintf (out, "{%i,", lp->index);
3222 print_generic_expr (out, lp->post_landing_pad, 0);
3223 fputc ('}', out);
3224 if (lp->next_lp)
3225 fputc (',', out);
3226 }
3227 }
3228 else
504e084a 3229 {
e38def9c 3230 for (lp = i->landing_pads; lp ; lp = lp->next_lp);
3231 {
3232 fprintf (out, "{%i,", lp->index);
3233 if (lp->landing_pad)
3234 fprintf (out, "%i%s,", INSN_UID (lp->landing_pad),
3235 NOTE_P (lp->landing_pad) ? "(del)" : "");
3236 else
3237 fprintf (out, "(nil),");
3238 if (lp->post_landing_pad)
3239 {
3240 rtx lab = label_rtx (lp->post_landing_pad);
3241 fprintf (out, "%i%s}", INSN_UID (lab),
3242 NOTE_P (lab) ? "(del)" : "");
3243 }
3244 else
3245 fprintf (out, "(nil)}");
3246 if (lp->next_lp)
3247 fputc (',', out);
3248 }
504e084a 3249 }
98b64106 3250 }
e38def9c 3251
bb22691b 3252 switch (i->type)
3253 {
3254 case ERT_CLEANUP:
e38def9c 3255 case ERT_MUST_NOT_THROW:
bb22691b 3256 break;
3257
3258 case ERT_TRY:
3259 {
e38def9c 3260 eh_catch c;
3261 fprintf (out, " catch:");
3262 for (c = i->u.eh_try.first_catch; c; c = c->next_catch)
3263 {
3264 fputc ('{', out);
3265 if (c->label)
3266 {
3267 fprintf (out, "lab:");
3268 print_generic_expr (out, c->label, 0);
3269 fputc (';', out);
3270 }
3271 print_generic_expr (out, c->type_list, 0);
3272 fputc ('}', out);
3273 if (c->next_catch)
3274 fputc (',', out);
3275 }
bb22691b 3276 }
3277 break;
3278
bb22691b 3279 case ERT_ALLOWED_EXCEPTIONS:
98b64106 3280 fprintf (out, " filter :%i types:", i->u.allowed.filter);
bb22691b 3281 print_generic_expr (out, i->u.allowed.type_list, 0);
3282 break;
bb22691b 3283 }
e38def9c 3284 fputc ('\n', out);
3285
b4ba5e9d 3286 /* If there are sub-regions, process them. */
3287 if (i->inner)
3288 i = i->inner, depth++;
3289 /* If there are peers, process them. */
3290 else if (i->next_peer)
3291 i = i->next_peer;
3292 /* Otherwise, step back up the tree to the next peer. */
3293 else
3294 {
bb22691b 3295 do
3296 {
3297 i = i->outer;
3298 depth--;
3299 if (i == NULL)
3300 return;
3301 }
3302 while (i->next_peer == NULL);
b4ba5e9d 3303 i = i->next_peer;
3304 }
3305 }
3306}
3307
576cc2fe 3308/* Dump the EH tree for FN on stderr. */
3309
4b987fac 3310DEBUG_FUNCTION void
576cc2fe 3311debug_eh_tree (struct function *fn)
3312{
3313 dump_eh_tree (stderr, fn);
3314}
3315
98b64106 3316/* Verify invariants on EH datastructures. */
3317
4b987fac 3318DEBUG_FUNCTION void
b4ba5e9d 3319verify_eh_tree (struct function *fun)
3320{
e38def9c 3321 eh_region r, outer;
3322 int nvisited_lp, nvisited_r;
3323 int count_lp, count_r, depth, i;
3324 eh_landing_pad lp;
b4ba5e9d 3325 bool err = false;
b4ba5e9d 3326
b8c0c866 3327 if (!fun->eh->region_tree)
b4ba5e9d 3328 return;
e38def9c 3329
3330 count_r = 0;
3331 for (i = 1; VEC_iterate (eh_region, fun->eh->region_array, i, r); ++i)
3332 if (r)
b4ba5e9d 3333 {
e38def9c 3334 if (r->index == i)
3335 count_r++;
3336 else
b4ba5e9d 3337 {
e38def9c 3338 error ("region_array is corrupted for region %i", r->index);
b4ba5e9d 3339 err = true;
3340 }
3341 }
3342
e38def9c 3343 count_lp = 0;
3344 for (i = 1; VEC_iterate (eh_landing_pad, fun->eh->lp_array, i, lp); ++i)
3345 if (lp)
3346 {
3347 if (lp->index == i)
3348 count_lp++;
3349 else
3350 {
3351 error ("lp_array is corrupted for lp %i", lp->index);
3352 err = true;
3353 }
3354 }
3355
3356 depth = nvisited_lp = nvisited_r = 0;
3357 outer = NULL;
3358 r = fun->eh->region_tree;
b4ba5e9d 3359 while (1)
3360 {
e38def9c 3361 if (VEC_index (eh_region, fun->eh->region_array, r->index) != r)
b4ba5e9d 3362 {
e38def9c 3363 error ("region_array is corrupted for region %i", r->index);
b4ba5e9d 3364 err = true;
3365 }
e38def9c 3366 if (r->outer != outer)
b4ba5e9d 3367 {
e38def9c 3368 error ("outer block of region %i is wrong", r->index);
b4ba5e9d 3369 err = true;
3370 }
e38def9c 3371 if (depth < 0)
b4ba5e9d 3372 {
e38def9c 3373 error ("negative nesting depth of region %i", r->index);
b4ba5e9d 3374 err = true;
3375 }
e38def9c 3376 nvisited_r++;
3377
3378 for (lp = r->landing_pads; lp ; lp = lp->next_lp)
b4ba5e9d 3379 {
e38def9c 3380 if (VEC_index (eh_landing_pad, fun->eh->lp_array, lp->index) != lp)
3381 {
3382 error ("lp_array is corrupted for lp %i", lp->index);
3383 err = true;
3384 }
3385 if (lp->region != r)
3386 {
3387 error ("region of lp %i is wrong", lp->index);
3388 err = true;
3389 }
3390 nvisited_lp++;
b4ba5e9d 3391 }
e38def9c 3392
3393 if (r->inner)
3394 outer = r, r = r->inner, depth++;
3395 else if (r->next_peer)
3396 r = r->next_peer;
b4ba5e9d 3397 else
3398 {
b8c0c866 3399 do
3400 {
e38def9c 3401 r = r->outer;
3402 if (r == NULL)
3403 goto region_done;
b8c0c866 3404 depth--;
e38def9c 3405 outer = r->outer;
b8c0c866 3406 }
e38def9c 3407 while (r->next_peer == NULL);
3408 r = r->next_peer;
b4ba5e9d 3409 }
3410 }
e38def9c 3411 region_done:
3412 if (depth != 0)
3413 {
3414 error ("tree list ends on depth %i", depth);
3415 err = true;
3416 }
3417 if (count_r != nvisited_r)
3418 {
3419 error ("region_array does not match region_tree");
3420 err = true;
3421 }
3422 if (count_lp != nvisited_lp)
3423 {
3424 error ("lp_array does not match region_tree");
3425 err = true;
3426 }
1774763d 3427
e38def9c 3428 if (err)
3429 {
3430 dump_eh_tree (stderr, fun);
3431 internal_error ("verify_eh_tree failed");
3432 }
1774763d 3433}
77fce4cd 3434\f
1f3233d1 3435#include "gt-except.h"