Debian Codegraph @

gcc/except.cc

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