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