libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / except.cc
blob5bb5edbd8065f768e8f01cf4baabf1a1daceda05
1 /* Implements exception handling.
2 Copyright (C) 1989-2024 Free Software Foundation, Inc.
3 Contributed by Mike Stump <mrs@cygnus.com>.
5 This file is part of GCC.
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.
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.
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/>. */
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.
26 The representation of exceptions changes several times during
27 the compilation process:
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.
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.
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.
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.
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).
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.
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.
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.
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. */
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"
149 static GTY(()) int call_site_base;
151 static GTY(()) hash_map<tree_hash, tree> *type_to_runtime_map;
153 static GTY(()) tree setjmp_fn;
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;
164 struct GTY(()) call_site_record_d
166 rtx landing_pad;
167 int action;
170 /* In the following structure and associated functions,
171 we represent entries in the action table as 1-based indices.
172 Special cases are:
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
179 Further, no special cases apply to the "next" field of the record.
180 For next, 0 means end of list. */
182 struct action_record
184 int offset;
185 int filter;
186 int next;
189 /* Hashtable helpers. */
191 struct action_record_hasher : free_ptr_hash <action_record>
193 static inline hashval_t hash (const action_record *);
194 static inline bool equal (const action_record *, const action_record *);
197 inline hashval_t
198 action_record_hasher::hash (const action_record *entry)
200 return entry->next * 1009 + entry->filter;
203 inline bool
204 action_record_hasher::equal (const action_record *entry,
205 const action_record *data)
207 return entry->filter == data->filter && entry->next == data->next;
210 typedef hash_table<action_record_hasher> action_hash_type;
212 static bool get_eh_region_and_lp_from_rtx (const_rtx, eh_region *,
213 eh_landing_pad *);
215 static void dw2_build_landing_pads (void);
217 static int collect_one_action_chain (action_hash_type *, eh_region);
218 static int add_call_site (rtx, int, int);
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);
228 void
229 init_eh (void)
231 if (! flag_exceptions)
232 return;
234 type_to_runtime_map = hash_map<tree_hash, tree>::create_ggc (31);
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)
240 tree f_jbuf, f_per, f_lsda, f_prev, f_cs, f_data, tmp;
242 sjlj_fc_type_node = lang_hooks.types.make_type (RECORD_TYPE);
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;
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;
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;
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;
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;
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.
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
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;
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;
318 layout_type (sjlj_fc_type_node);
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);
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
350 void
351 init_eh_for_function (void)
353 cfun->eh = ggc_cleared_alloc<eh_status> ();
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);
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. */
364 static eh_region
365 gen_eh_region (enum eh_region_type type, eh_region outer)
367 eh_region new_eh;
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)
375 new_eh->next_peer = outer->inner;
376 outer->inner = new_eh;
378 else
380 new_eh->next_peer = cfun->eh->region_tree;
381 cfun->eh->region_tree = new_eh;
384 new_eh->index = vec_safe_length (cfun->eh->region_array);
385 vec_safe_push (cfun->eh->region_array, new_eh);
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;
391 return new_eh;
394 eh_region
395 gen_eh_region_cleanup (eh_region outer)
397 return gen_eh_region (ERT_CLEANUP, outer);
400 eh_region
401 gen_eh_region_try (eh_region outer)
403 return gen_eh_region (ERT_TRY, outer);
406 eh_catch
407 gen_eh_region_catch (eh_region t, tree type_or_list)
409 eh_catch c, l;
410 tree type_list, type_node;
412 gcc_assert (t->type == ERT_TRY);
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)
419 if (TREE_CODE (type_or_list) != TREE_LIST)
420 type_list = tree_cons (NULL_TREE, type_or_list, NULL_TREE);
422 type_node = type_list;
423 for (; type_node; type_node = TREE_CHAIN (type_node))
424 add_type_for_runtime (TREE_VALUE (type_node));
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;
437 return c;
440 eh_region
441 gen_eh_region_allowed (eh_region outer, tree allowed)
443 eh_region region = gen_eh_region (ERT_ALLOWED_EXCEPTIONS, outer);
444 region->u.allowed.type_list = allowed;
446 for (; allowed ; allowed = TREE_CHAIN (allowed))
447 add_type_for_runtime (TREE_VALUE (allowed));
449 return region;
452 eh_region
453 gen_eh_region_must_not_throw (eh_region outer)
455 return gen_eh_region (ERT_MUST_NOT_THROW, outer);
458 eh_landing_pad
459 gen_eh_landing_pad (eh_region region)
461 eh_landing_pad lp = ggc_cleared_alloc<eh_landing_pad_d> ();
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;
468 vec_safe_push (cfun->eh->lp_array, lp);
470 return lp;
473 eh_region
474 get_eh_region_from_number_fn (struct function *ifun, int i)
476 return (*ifun->eh->region_array)[i];
479 eh_region
480 get_eh_region_from_number (int i)
482 return get_eh_region_from_number_fn (cfun, i);
485 eh_landing_pad
486 get_eh_landing_pad_from_number_fn (struct function *ifun, int i)
488 return (*ifun->eh->lp_array)[i];
491 eh_landing_pad
492 get_eh_landing_pad_from_number (int i)
494 return get_eh_landing_pad_from_number_fn (cfun, i);
497 eh_region
498 get_eh_region_from_lp_number_fn (struct function *ifun, int i)
500 if (i < 0)
501 return (*ifun->eh->region_array)[-i];
502 else if (i == 0)
503 return NULL;
504 else
506 eh_landing_pad lp;
507 lp = (*ifun->eh->lp_array)[i];
508 return lp->region;
512 eh_region
513 get_eh_region_from_lp_number (int i)
515 return get_eh_region_from_lp_number_fn (cfun, i);
518 /* Returns true if the current function has exception handling regions. */
520 bool
521 current_function_has_exception_handlers (void)
523 return cfun->eh->region_tree != NULL;
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. */
529 struct duplicate_eh_regions_data
531 duplicate_eh_regions_map label_map;
532 void *label_map_data;
533 hash_map<void *, void *> *eh_map;
536 static void
537 duplicate_eh_regions_1 (struct duplicate_eh_regions_data *data,
538 eh_region old_r, eh_region outer)
540 eh_landing_pad old_lp, new_lp;
541 eh_region new_r;
543 new_r = gen_eh_region (old_r->type, outer);
544 gcc_assert (!data->eh_map->put (old_r, new_r));
546 switch (old_r->type)
548 case ERT_CLEANUP:
549 break;
551 case ERT_TRY:
553 eh_catch oc, nc;
554 for (oc = old_r->u.eh_try.first_catch; oc ; oc = oc->next_catch)
556 /* We should be doing all our region duplication before and
557 during inlining, which is before filter lists are created. */
558 gcc_assert (oc->filter_list == NULL);
559 nc = gen_eh_region_catch (new_r, oc->type_list);
560 nc->label = data->label_map (oc->label, data->label_map_data);
563 break;
565 case ERT_ALLOWED_EXCEPTIONS:
566 new_r->u.allowed.type_list = old_r->u.allowed.type_list;
567 if (old_r->u.allowed.label)
568 new_r->u.allowed.label
569 = data->label_map (old_r->u.allowed.label, data->label_map_data);
570 else
571 new_r->u.allowed.label = NULL_TREE;
572 break;
574 case ERT_MUST_NOT_THROW:
575 new_r->u.must_not_throw.failure_loc =
576 LOCATION_LOCUS (old_r->u.must_not_throw.failure_loc);
577 new_r->u.must_not_throw.failure_decl =
578 old_r->u.must_not_throw.failure_decl;
579 break;
582 for (old_lp = old_r->landing_pads; old_lp ; old_lp = old_lp->next_lp)
584 /* Don't bother copying unused landing pads. */
585 if (old_lp->post_landing_pad == NULL)
586 continue;
588 new_lp = gen_eh_landing_pad (new_r);
589 gcc_assert (!data->eh_map->put (old_lp, new_lp));
591 new_lp->post_landing_pad
592 = data->label_map (old_lp->post_landing_pad, data->label_map_data);
593 EH_LANDING_PAD_NR (new_lp->post_landing_pad) = new_lp->index;
596 /* Make sure to preserve the original use of __cxa_end_cleanup. */
597 new_r->use_cxa_end_cleanup = old_r->use_cxa_end_cleanup;
599 for (old_r = old_r->inner; old_r ; old_r = old_r->next_peer)
600 duplicate_eh_regions_1 (data, old_r, new_r);
603 /* Duplicate the EH regions from IFUN rooted at COPY_REGION into
604 the current function and root the tree below OUTER_REGION.
605 The special case of COPY_REGION of NULL means all regions.
606 Remap labels using MAP/MAP_DATA callback. Return a pointer map
607 that allows the caller to remap uses of both EH regions and
608 EH landing pads. */
610 hash_map<void *, void *> *
611 duplicate_eh_regions (struct function *ifun,
612 eh_region copy_region, int outer_lp,
613 duplicate_eh_regions_map map, void *map_data)
615 struct duplicate_eh_regions_data data;
616 eh_region outer_region;
618 if (flag_checking)
619 verify_eh_tree (ifun);
621 data.label_map = map;
622 data.label_map_data = map_data;
623 data.eh_map = new hash_map<void *, void *>;
625 outer_region = get_eh_region_from_lp_number_fn (cfun, outer_lp);
627 /* Copy all the regions in the subtree. */
628 if (copy_region)
629 duplicate_eh_regions_1 (&data, copy_region, outer_region);
630 else
632 eh_region r;
633 for (r = ifun->eh->region_tree; r ; r = r->next_peer)
634 duplicate_eh_regions_1 (&data, r, outer_region);
637 if (flag_checking)
638 verify_eh_tree (cfun);
640 return data.eh_map;
643 /* Return the region that is outer to both REGION_A and REGION_B in IFUN. */
645 eh_region
646 eh_region_outermost (struct function *ifun, eh_region region_a,
647 eh_region region_b)
649 gcc_assert (ifun->eh->region_array);
650 gcc_assert (ifun->eh->region_tree);
652 auto_sbitmap b_outer (ifun->eh->region_array->length ());
653 bitmap_clear (b_outer);
657 bitmap_set_bit (b_outer, region_b->index);
658 region_b = region_b->outer;
660 while (region_b);
664 if (bitmap_bit_p (b_outer, region_a->index))
665 break;
666 region_a = region_a->outer;
668 while (region_a);
670 return region_a;
673 void
674 add_type_for_runtime (tree type)
676 /* If TYPE is NOP_EXPR, it means that it already is a runtime type. */
677 if (TREE_CODE (type) == NOP_EXPR)
678 return;
680 bool existed = false;
681 tree *slot = &type_to_runtime_map->get_or_insert (type, &existed);
682 if (!existed)
683 *slot = lang_hooks.eh_runtime_type (type);
686 tree
687 lookup_type_for_runtime (tree type)
689 /* If TYPE is NOP_EXPR, it means that it already is a runtime type. */
690 if (TREE_CODE (type) == NOP_EXPR)
691 return type;
693 /* We should have always inserted the data earlier. */
694 return *type_to_runtime_map->get (type);
698 /* Represent an entry in @TTypes for either catch actions
699 or exception filter actions. */
700 struct ttypes_filter {
701 tree t;
702 int filter;
705 /* Helper for ttypes_filter hashing. */
707 struct ttypes_filter_hasher : free_ptr_hash <ttypes_filter>
709 typedef tree_node *compare_type;
710 static inline hashval_t hash (const ttypes_filter *);
711 static inline bool equal (const ttypes_filter *, const tree_node *);
714 /* Compare ENTRY (a ttypes_filter entry in the hash table) with DATA
715 (a tree) for a @TTypes type node we are thinking about adding. */
717 inline bool
718 ttypes_filter_hasher::equal (const ttypes_filter *entry, const tree_node *data)
720 return entry->t == data;
723 inline hashval_t
724 ttypes_filter_hasher::hash (const ttypes_filter *entry)
726 return TREE_HASH (entry->t);
729 typedef hash_table<ttypes_filter_hasher> ttypes_hash_type;
732 /* Helper for ehspec hashing. */
734 struct ehspec_hasher : free_ptr_hash <ttypes_filter>
736 static inline hashval_t hash (const ttypes_filter *);
737 static inline bool equal (const ttypes_filter *, const ttypes_filter *);
740 /* Compare ENTRY with DATA (both struct ttypes_filter) for a @TTypes
741 exception specification list we are thinking about adding. */
742 /* ??? Currently we use the type lists in the order given. Someone
743 should put these in some canonical order. */
745 inline bool
746 ehspec_hasher::equal (const ttypes_filter *entry, const ttypes_filter *data)
748 return type_list_equal (entry->t, data->t);
751 /* Hash function for exception specification lists. */
753 inline hashval_t
754 ehspec_hasher::hash (const ttypes_filter *entry)
756 hashval_t h = 0;
757 tree list;
759 for (list = entry->t; list ; list = TREE_CHAIN (list))
760 h = (h << 5) + (h >> 27) + TREE_HASH (TREE_VALUE (list));
761 return h;
764 typedef hash_table<ehspec_hasher> ehspec_hash_type;
767 /* Add TYPE (which may be NULL) to cfun->eh->ttype_data, using TYPES_HASH
768 to speed up the search. Return the filter value to be used. */
770 static int
771 add_ttypes_entry (ttypes_hash_type *ttypes_hash, tree type)
773 struct ttypes_filter **slot, *n;
775 slot = ttypes_hash->find_slot_with_hash (type, (hashval_t) TREE_HASH (type),
776 INSERT);
778 if ((n = *slot) == NULL)
780 /* Filter value is a 1 based table index. */
782 n = XNEW (struct ttypes_filter);
783 n->t = type;
784 n->filter = vec_safe_length (cfun->eh->ttype_data) + 1;
785 *slot = n;
787 vec_safe_push (cfun->eh->ttype_data, type);
790 return n->filter;
793 /* Add LIST to cfun->eh->ehspec_data, using EHSPEC_HASH and TYPES_HASH
794 to speed up the search. Return the filter value to be used. */
796 static int
797 add_ehspec_entry (ehspec_hash_type *ehspec_hash, ttypes_hash_type *ttypes_hash,
798 tree list)
800 struct ttypes_filter **slot, *n;
801 struct ttypes_filter dummy;
803 dummy.t = list;
804 slot = ehspec_hash->find_slot (&dummy, INSERT);
806 if ((n = *slot) == NULL)
808 int len;
810 if (targetm.arm_eabi_unwinder)
811 len = vec_safe_length (cfun->eh->ehspec_data.arm_eabi);
812 else
813 len = vec_safe_length (cfun->eh->ehspec_data.other);
815 /* Filter value is a -1 based byte index into a uleb128 buffer. */
817 n = XNEW (struct ttypes_filter);
818 n->t = list;
819 n->filter = -(len + 1);
820 *slot = n;
822 /* Generate a 0 terminated list of filter values. */
823 for (; list ; list = TREE_CHAIN (list))
825 if (targetm.arm_eabi_unwinder)
826 vec_safe_push (cfun->eh->ehspec_data.arm_eabi, TREE_VALUE (list));
827 else
829 /* Look up each type in the list and encode its filter
830 value as a uleb128. */
831 push_uleb128 (&cfun->eh->ehspec_data.other,
832 add_ttypes_entry (ttypes_hash, TREE_VALUE (list)));
835 if (targetm.arm_eabi_unwinder)
836 vec_safe_push (cfun->eh->ehspec_data.arm_eabi, NULL_TREE);
837 else
838 vec_safe_push (cfun->eh->ehspec_data.other, (uchar)0);
841 return n->filter;
844 /* Generate the action filter values to be used for CATCH and
845 ALLOWED_EXCEPTIONS regions. When using dwarf2 exception regions,
846 we use lots of landing pads, and so every type or list can share
847 the same filter value, which saves table space. */
849 void
850 assign_filter_values (void)
852 int i;
853 eh_region r;
854 eh_catch c;
856 vec_alloc (cfun->eh->ttype_data, 16);
857 if (targetm.arm_eabi_unwinder)
858 vec_alloc (cfun->eh->ehspec_data.arm_eabi, 64);
859 else
860 vec_alloc (cfun->eh->ehspec_data.other, 64);
862 ehspec_hash_type ehspec (31);
863 ttypes_hash_type ttypes (31);
865 for (i = 1; vec_safe_iterate (cfun->eh->region_array, i, &r); ++i)
867 if (r == NULL)
868 continue;
870 switch (r->type)
872 case ERT_TRY:
873 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
875 /* Whatever type_list is (NULL or true list), we build a list
876 of filters for the region. */
877 c->filter_list = NULL_TREE;
879 if (c->type_list != NULL)
881 /* Get a filter value for each of the types caught and store
882 them in the region's dedicated list. */
883 tree tp_node = c->type_list;
885 for ( ; tp_node; tp_node = TREE_CHAIN (tp_node))
887 int flt
888 = add_ttypes_entry (&ttypes, TREE_VALUE (tp_node));
889 tree flt_node = build_int_cst (integer_type_node, flt);
891 c->filter_list
892 = tree_cons (NULL_TREE, flt_node, c->filter_list);
895 else
897 /* Get a filter value for the NULL list also since it
898 will need an action record anyway. */
899 int flt = add_ttypes_entry (&ttypes, NULL);
900 tree flt_node = build_int_cst (integer_type_node, flt);
902 c->filter_list
903 = tree_cons (NULL_TREE, flt_node, NULL);
906 break;
908 case ERT_ALLOWED_EXCEPTIONS:
909 r->u.allowed.filter
910 = add_ehspec_entry (&ehspec, &ttypes, r->u.allowed.type_list);
911 break;
913 default:
914 break;
919 /* Emit SEQ into basic block just before INSN (that is assumed to be
920 first instruction of some existing BB and return the newly
921 produced block. */
922 static basic_block
923 emit_to_new_bb_before (rtx_insn *seq, rtx_insn *insn)
925 rtx_insn *next, *last;
926 basic_block bb;
927 edge e;
928 edge_iterator ei;
930 /* If there happens to be a fallthru edge (possibly created by cleanup_cfg
931 call), we don't want it to go into newly created landing pad or other EH
932 construct. */
933 for (ei = ei_start (BLOCK_FOR_INSN (insn)->preds); (e = ei_safe_edge (ei)); )
934 if (e->flags & EDGE_FALLTHRU)
935 force_nonfallthru (e);
936 else
937 ei_next (&ei);
939 /* Make sure to put the location of INSN or a subsequent instruction on SEQ
940 to avoid inheriting the location of the previous instruction. */
941 next = insn;
942 while (next && !NONDEBUG_INSN_P (next))
943 next = NEXT_INSN (next);
944 if (next)
945 last = emit_insn_before_setloc (seq, insn, INSN_LOCATION (next));
946 else
947 last = emit_insn_before (seq, insn);
948 if (BARRIER_P (last))
949 last = PREV_INSN (last);
950 bb = create_basic_block (seq, last, BLOCK_FOR_INSN (insn)->prev_bb);
951 update_bb_for_insn (bb);
952 bb->flags |= BB_SUPERBLOCK;
953 return bb;
956 /* A subroutine of dw2_build_landing_pads, also used for edge splitting
957 at the rtl level. Emit the code required by the target at a landing
958 pad for the given region. */
960 static void
961 expand_dw2_landing_pad_for_region (eh_region region)
963 if (targetm.have_exception_receiver ())
964 emit_insn (targetm.gen_exception_receiver ());
965 else if (targetm.have_nonlocal_goto_receiver ())
966 emit_insn (targetm.gen_nonlocal_goto_receiver ());
967 else
968 { /* Nothing */ }
970 if (region->exc_ptr_reg)
971 emit_move_insn (region->exc_ptr_reg,
972 gen_rtx_REG (ptr_mode, EH_RETURN_DATA_REGNO (0)));
973 if (region->filter_reg)
974 emit_move_insn (region->filter_reg,
975 gen_rtx_REG (targetm.eh_return_filter_mode (),
976 EH_RETURN_DATA_REGNO (1)));
979 /* Expand the extra code needed at landing pads for dwarf2 unwinding. */
981 static void
982 dw2_build_landing_pads (void)
984 int i;
985 eh_landing_pad lp;
986 int e_flags = EDGE_FALLTHRU;
988 /* If we're going to partition blocks, we need to be able to add
989 new landing pads later, which means that we need to hold on to
990 the post-landing-pad block. Prevent it from being merged away.
991 We'll remove this bit after partitioning. */
992 if (flag_reorder_blocks_and_partition)
993 e_flags |= EDGE_PRESERVE;
995 for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
997 basic_block bb;
998 rtx_insn *seq;
1000 if (lp == NULL || lp->post_landing_pad == NULL)
1001 continue;
1003 start_sequence ();
1005 lp->landing_pad = gen_label_rtx ();
1006 emit_label (lp->landing_pad);
1007 LABEL_PRESERVE_P (lp->landing_pad) = 1;
1009 expand_dw2_landing_pad_for_region (lp->region);
1011 seq = get_insns ();
1012 end_sequence ();
1014 bb = emit_to_new_bb_before (seq, label_rtx (lp->post_landing_pad));
1015 bb->count = bb->next_bb->count;
1016 make_single_succ_edge (bb, bb->next_bb, e_flags);
1017 if (current_loops)
1019 class loop *loop = bb->next_bb->loop_father;
1020 /* If we created a pre-header block, add the new block to the
1021 outer loop, otherwise to the loop itself. */
1022 if (bb->next_bb == loop->header)
1023 add_bb_to_loop (bb, loop_outer (loop));
1024 else
1025 add_bb_to_loop (bb, loop);
1031 static vec<int> sjlj_lp_call_site_index;
1033 /* Process all active landing pads. Assign each one a compact dispatch
1034 index, and a call-site index. */
1036 static int
1037 sjlj_assign_call_site_values (void)
1039 action_hash_type ar_hash (31);
1040 int i, disp_index;
1041 eh_landing_pad lp;
1043 vec_alloc (crtl->eh.action_record_data, 64);
1045 disp_index = 0;
1046 call_site_base = 1;
1047 for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
1048 if (lp && lp->post_landing_pad)
1050 int action, call_site;
1052 /* First: build the action table. */
1053 action = collect_one_action_chain (&ar_hash, lp->region);
1055 /* Next: assign call-site values. If dwarf2 terms, this would be
1056 the region number assigned by convert_to_eh_region_ranges, but
1057 handles no-action and must-not-throw differently. */
1058 /* Map must-not-throw to otherwise unused call-site index 0. */
1059 if (action == -2)
1060 call_site = 0;
1061 /* Map no-action to otherwise unused call-site index -1. */
1062 else if (action == -1)
1063 call_site = -1;
1064 /* Otherwise, look it up in the table. */
1065 else
1066 call_site = add_call_site (GEN_INT (disp_index), action, 0);
1067 sjlj_lp_call_site_index[i] = call_site;
1069 disp_index++;
1072 return disp_index;
1075 /* Emit code to record the current call-site index before every
1076 insn that can throw. */
1078 static void
1079 sjlj_mark_call_sites (void)
1081 int last_call_site = -2;
1082 rtx_insn *insn;
1083 rtx mem;
1085 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
1087 eh_landing_pad lp;
1088 eh_region r;
1089 bool nothrow;
1090 int this_call_site;
1091 rtx_insn *before, *p;
1093 /* Reset value tracking at extended basic block boundaries. */
1094 if (LABEL_P (insn))
1095 last_call_site = -2;
1097 /* If the function allocates dynamic stack space, the context must
1098 be updated after every allocation/deallocation accordingly. */
1099 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_UPDATE_SJLJ_CONTEXT)
1101 rtx buf_addr;
1103 start_sequence ();
1104 buf_addr = plus_constant (Pmode, XEXP (crtl->eh.sjlj_fc, 0),
1105 sjlj_fc_jbuf_ofs);
1106 expand_builtin_update_setjmp_buf (buf_addr);
1107 p = get_insns ();
1108 end_sequence ();
1109 emit_insn_before (p, insn);
1112 if (! INSN_P (insn))
1113 continue;
1115 nothrow = get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1116 if (nothrow)
1117 continue;
1118 if (lp)
1119 this_call_site = sjlj_lp_call_site_index[lp->index];
1120 else if (r == NULL)
1122 /* Calls (and trapping insns) without notes are outside any
1123 exception handling region in this function. Mark them as
1124 no action. */
1125 this_call_site = -1;
1127 else
1129 gcc_assert (r->type == ERT_MUST_NOT_THROW);
1130 this_call_site = 0;
1133 if (this_call_site != -1)
1134 crtl->uses_eh_lsda = 1;
1136 if (this_call_site == last_call_site)
1137 continue;
1139 /* Don't separate a call from it's argument loads. */
1140 before = insn;
1141 if (CALL_P (insn))
1142 before = find_first_parameter_load (insn, NULL);
1144 start_sequence ();
1145 mem = adjust_address (crtl->eh.sjlj_fc, TYPE_MODE (integer_type_node),
1146 sjlj_fc_call_site_ofs);
1147 emit_move_insn (mem, gen_int_mode (this_call_site, GET_MODE (mem)));
1148 p = get_insns ();
1149 end_sequence ();
1151 emit_insn_before (p, before);
1152 last_call_site = this_call_site;
1156 /* Construct the SjLj_Function_Context. */
1158 static void
1159 sjlj_emit_function_enter (rtx_code_label *dispatch_label)
1161 rtx_insn *fn_begin, *seq;
1162 rtx fc, mem;
1163 bool fn_begin_outside_block;
1164 rtx personality = get_personality_function (current_function_decl);
1166 fc = crtl->eh.sjlj_fc;
1168 start_sequence ();
1170 /* We're storing this libcall's address into memory instead of
1171 calling it directly. Thus, we must call assemble_external_libcall
1172 here, as we cannot depend on emit_library_call to do it for us. */
1173 assemble_external_libcall (personality);
1174 mem = adjust_address (fc, Pmode, sjlj_fc_personality_ofs);
1175 emit_move_insn (mem, personality);
1177 mem = adjust_address (fc, Pmode, sjlj_fc_lsda_ofs);
1178 if (crtl->uses_eh_lsda)
1180 char buf[20];
1181 rtx sym;
1183 ASM_GENERATE_INTERNAL_LABEL (buf, "LLSDA", current_function_funcdef_no);
1184 sym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
1185 SYMBOL_REF_FLAGS (sym) = SYMBOL_FLAG_LOCAL;
1186 emit_move_insn (mem, sym);
1188 else
1189 emit_move_insn (mem, const0_rtx);
1191 if (dispatch_label)
1193 rtx addr = plus_constant (Pmode, XEXP (fc, 0), sjlj_fc_jbuf_ofs);
1195 #ifdef DONT_USE_BUILTIN_SETJMP
1196 addr = copy_addr_to_reg (addr);
1197 addr = convert_memory_address (ptr_mode, addr);
1198 tree addr_tree = make_tree (ptr_type_node, addr);
1200 tree call_expr = build_call_expr (setjmp_fn, 1, addr_tree);
1201 rtx x = expand_call (call_expr, NULL_RTX, false);
1203 emit_cmp_and_jump_insns (x, const0_rtx, NE, 0,
1204 TYPE_MODE (integer_type_node), 0,
1205 dispatch_label,
1206 profile_probability::unlikely ());
1207 #else
1208 expand_builtin_setjmp_setup (addr, dispatch_label);
1209 #endif
1212 emit_library_call (unwind_sjlj_register_libfunc, LCT_NORMAL, VOIDmode,
1213 XEXP (fc, 0), Pmode);
1215 seq = get_insns ();
1216 end_sequence ();
1218 /* ??? Instead of doing this at the beginning of the function,
1219 do this in a block that is at loop level 0 and dominates all
1220 can_throw_internal instructions. */
1222 fn_begin_outside_block = true;
1223 for (fn_begin = get_insns (); ; fn_begin = NEXT_INSN (fn_begin))
1224 if (NOTE_P (fn_begin))
1226 if (NOTE_KIND (fn_begin) == NOTE_INSN_FUNCTION_BEG)
1227 break;
1228 else if (NOTE_INSN_BASIC_BLOCK_P (fn_begin))
1229 fn_begin_outside_block = false;
1231 /* assign_params can indirectly call emit_block_move_via_loop, e.g.
1232 for g++.dg/torture/pr85627.C for 16-bit targets. */
1233 else if (JUMP_P (fn_begin))
1234 fn_begin_outside_block = true;
1236 #ifdef DONT_USE_BUILTIN_SETJMP
1237 if (dispatch_label)
1239 /* The sequence contains a branch in the middle so we need to force
1240 the creation of a new basic block by means of BB_SUPERBLOCK. */
1241 if (fn_begin_outside_block)
1243 basic_block bb
1244 = split_edge (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1245 if (JUMP_P (BB_END (bb)))
1246 emit_insn_before (seq, BB_END (bb));
1247 else
1248 emit_insn_after (seq, BB_END (bb));
1250 else
1251 emit_insn_after (seq, fn_begin);
1253 single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun))->flags |= BB_SUPERBLOCK;
1254 return;
1256 #endif
1258 if (fn_begin_outside_block)
1259 insert_insn_on_edge (seq, single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1260 else
1261 emit_insn_after (seq, fn_begin);
1264 /* Call back from expand_function_end to know where we should put
1265 the call to unwind_sjlj_unregister_libfunc if needed. */
1267 void
1268 sjlj_emit_function_exit_after (rtx_insn *after)
1270 crtl->eh.sjlj_exit_after = after;
1273 static void
1274 sjlj_emit_function_exit (void)
1276 rtx_insn *seq, *insn;
1278 start_sequence ();
1280 emit_library_call (unwind_sjlj_unregister_libfunc, LCT_NORMAL, VOIDmode,
1281 XEXP (crtl->eh.sjlj_fc, 0), Pmode);
1283 seq = get_insns ();
1284 end_sequence ();
1286 /* ??? Really this can be done in any block at loop level 0 that
1287 post-dominates all can_throw_internal instructions. This is
1288 the last possible moment. */
1290 insn = crtl->eh.sjlj_exit_after;
1291 if (LABEL_P (insn))
1292 insn = NEXT_INSN (insn);
1294 emit_insn_after (seq, insn);
1297 static void
1298 sjlj_emit_dispatch_table (rtx_code_label *dispatch_label, int num_dispatch)
1300 scalar_int_mode unwind_word_mode = targetm.unwind_word_mode ();
1301 scalar_int_mode filter_mode = targetm.eh_return_filter_mode ();
1302 eh_landing_pad lp;
1303 rtx mem, fc, exc_ptr_reg, filter_reg;
1304 rtx_insn *seq;
1305 basic_block bb;
1306 eh_region r;
1307 int i, disp_index;
1308 vec<tree> dispatch_labels = vNULL;
1310 fc = crtl->eh.sjlj_fc;
1312 start_sequence ();
1314 emit_label (dispatch_label);
1316 #ifndef DONT_USE_BUILTIN_SETJMP
1317 expand_builtin_setjmp_receiver (dispatch_label);
1319 /* The caller of expand_builtin_setjmp_receiver is responsible for
1320 making sure that the label doesn't vanish. The only other caller
1321 is the expander for __builtin_setjmp_receiver, which places this
1322 label on the nonlocal_goto_label list. Since we're modeling these
1323 CFG edges more exactly, we can use the forced_labels list instead. */
1324 LABEL_PRESERVE_P (dispatch_label) = 1;
1325 vec_safe_push<rtx_insn *> (forced_labels, dispatch_label);
1326 #endif
1328 /* Load up exc_ptr and filter values from the function context. */
1329 mem = adjust_address (fc, unwind_word_mode, sjlj_fc_data_ofs);
1330 if (unwind_word_mode != ptr_mode)
1332 #ifdef POINTERS_EXTEND_UNSIGNED
1333 mem = convert_memory_address (ptr_mode, mem);
1334 #else
1335 mem = convert_to_mode (ptr_mode, mem, 0);
1336 #endif
1338 exc_ptr_reg = force_reg (ptr_mode, mem);
1340 mem = adjust_address (fc, unwind_word_mode,
1341 sjlj_fc_data_ofs + GET_MODE_SIZE (unwind_word_mode));
1342 if (unwind_word_mode != filter_mode)
1343 mem = convert_to_mode (filter_mode, mem, 0);
1344 filter_reg = force_reg (filter_mode, mem);
1346 /* Jump to one of the directly reachable regions. */
1348 disp_index = 0;
1349 rtx_code_label *first_reachable_label = NULL;
1351 /* If there's exactly one call site in the function, don't bother
1352 generating a switch statement. */
1353 if (num_dispatch > 1)
1354 dispatch_labels.create (num_dispatch);
1356 for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
1357 if (lp && lp->post_landing_pad)
1359 rtx_insn *seq2;
1360 rtx_code_label *label;
1362 start_sequence ();
1364 lp->landing_pad = dispatch_label;
1366 if (num_dispatch > 1)
1368 tree t_label, case_elt, t;
1370 t_label = create_artificial_label (UNKNOWN_LOCATION);
1371 t = build_int_cst (integer_type_node, disp_index);
1372 case_elt = build_case_label (t, NULL, t_label);
1373 dispatch_labels.quick_push (case_elt);
1374 label = jump_target_rtx (t_label);
1376 else
1377 label = gen_label_rtx ();
1379 if (disp_index == 0)
1380 first_reachable_label = label;
1381 emit_label (label);
1383 r = lp->region;
1384 if (r->exc_ptr_reg)
1385 emit_move_insn (r->exc_ptr_reg, exc_ptr_reg);
1386 if (r->filter_reg)
1387 emit_move_insn (r->filter_reg, filter_reg);
1389 seq2 = get_insns ();
1390 end_sequence ();
1392 rtx_insn *before = label_rtx (lp->post_landing_pad);
1393 bb = emit_to_new_bb_before (seq2, before);
1394 make_single_succ_edge (bb, bb->next_bb, EDGE_FALLTHRU);
1395 if (current_loops)
1397 class loop *loop = bb->next_bb->loop_father;
1398 /* If we created a pre-header block, add the new block to the
1399 outer loop, otherwise to the loop itself. */
1400 if (bb->next_bb == loop->header)
1401 add_bb_to_loop (bb, loop_outer (loop));
1402 else
1403 add_bb_to_loop (bb, loop);
1404 /* ??? For multiple dispatches we will end up with edges
1405 from the loop tree root into this loop, making it a
1406 multiple-entry loop. Discard all affected loops. */
1407 if (num_dispatch > 1)
1409 for (loop = bb->loop_father;
1410 loop_outer (loop); loop = loop_outer (loop))
1411 mark_loop_for_removal (loop);
1415 disp_index++;
1417 gcc_assert (disp_index == num_dispatch);
1419 if (num_dispatch > 1)
1421 rtx disp = adjust_address (fc, TYPE_MODE (integer_type_node),
1422 sjlj_fc_call_site_ofs);
1423 expand_sjlj_dispatch_table (disp, dispatch_labels);
1426 seq = get_insns ();
1427 end_sequence ();
1429 bb = emit_to_new_bb_before (seq, first_reachable_label);
1430 if (num_dispatch == 1)
1432 make_single_succ_edge (bb, bb->next_bb, EDGE_FALLTHRU);
1433 if (current_loops)
1435 class loop *loop = bb->next_bb->loop_father;
1436 /* If we created a pre-header block, add the new block to the
1437 outer loop, otherwise to the loop itself. */
1438 if (bb->next_bb == loop->header)
1439 add_bb_to_loop (bb, loop_outer (loop));
1440 else
1441 add_bb_to_loop (bb, loop);
1444 else
1446 /* We are not wiring up edges here, but as the dispatcher call
1447 is at function begin simply associate the block with the
1448 outermost (non-)loop. */
1449 if (current_loops)
1450 add_bb_to_loop (bb, current_loops->tree_root);
1454 static void
1455 sjlj_build_landing_pads (void)
1457 int num_dispatch;
1459 num_dispatch = vec_safe_length (cfun->eh->lp_array);
1460 if (num_dispatch == 0)
1461 return;
1462 sjlj_lp_call_site_index.safe_grow_cleared (num_dispatch, true);
1464 num_dispatch = sjlj_assign_call_site_values ();
1465 if (num_dispatch > 0)
1467 rtx_code_label *dispatch_label = gen_label_rtx ();
1468 int align = STACK_SLOT_ALIGNMENT (sjlj_fc_type_node,
1469 TYPE_MODE (sjlj_fc_type_node),
1470 TYPE_ALIGN (sjlj_fc_type_node));
1471 crtl->eh.sjlj_fc
1472 = assign_stack_local (TYPE_MODE (sjlj_fc_type_node),
1473 int_size_in_bytes (sjlj_fc_type_node),
1474 align);
1476 sjlj_mark_call_sites ();
1477 sjlj_emit_function_enter (dispatch_label);
1478 sjlj_emit_dispatch_table (dispatch_label, num_dispatch);
1479 sjlj_emit_function_exit ();
1482 /* If we do not have any landing pads, we may still need to register a
1483 personality routine and (empty) LSDA to handle must-not-throw regions. */
1484 else if (function_needs_eh_personality (cfun) != eh_personality_none)
1486 int align = STACK_SLOT_ALIGNMENT (sjlj_fc_type_node,
1487 TYPE_MODE (sjlj_fc_type_node),
1488 TYPE_ALIGN (sjlj_fc_type_node));
1489 crtl->eh.sjlj_fc
1490 = assign_stack_local (TYPE_MODE (sjlj_fc_type_node),
1491 int_size_in_bytes (sjlj_fc_type_node),
1492 align);
1494 sjlj_mark_call_sites ();
1495 sjlj_emit_function_enter (NULL);
1496 sjlj_emit_function_exit ();
1499 sjlj_lp_call_site_index.release ();
1502 /* Update the sjlj function context. This function should be called
1503 whenever we allocate or deallocate dynamic stack space. */
1505 void
1506 update_sjlj_context (void)
1508 if (!flag_exceptions)
1509 return;
1511 emit_note (NOTE_INSN_UPDATE_SJLJ_CONTEXT);
1514 /* After initial rtl generation, call back to finish generating
1515 exception support code. */
1517 void
1518 finish_eh_generation (void)
1520 basic_block bb;
1522 /* Construct the landing pads. */
1523 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
1524 sjlj_build_landing_pads ();
1525 else
1526 dw2_build_landing_pads ();
1528 break_superblocks ();
1530 /* Redirect all EH edges from the post_landing_pad to the landing pad. */
1531 FOR_EACH_BB_FN (bb, cfun)
1533 eh_landing_pad lp;
1534 edge_iterator ei;
1535 edge e;
1537 lp = get_eh_landing_pad_from_rtx (BB_END (bb));
1539 FOR_EACH_EDGE (e, ei, bb->succs)
1540 if (e->flags & EDGE_EH)
1541 break;
1543 /* We should not have generated any new throwing insns during this
1544 pass, and we should not have lost any EH edges, so we only need
1545 to handle two cases here:
1546 (1) reachable handler and an existing edge to post-landing-pad,
1547 (2) no reachable handler and no edge. */
1548 gcc_assert ((lp != NULL) == (e != NULL));
1549 if (lp != NULL)
1551 gcc_assert (BB_HEAD (e->dest) == label_rtx (lp->post_landing_pad));
1553 redirect_edge_succ (e, BLOCK_FOR_INSN (lp->landing_pad));
1554 e->flags |= (CALL_P (BB_END (bb))
1555 ? EDGE_ABNORMAL | EDGE_ABNORMAL_CALL
1556 : EDGE_ABNORMAL);
1560 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ
1561 /* Kludge for Alpha (see alpha_gp_save_rtx). */
1562 || single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun))->insns.r)
1563 commit_edge_insertions ();
1566 /* This section handles removing dead code for flow. */
1568 void
1569 remove_eh_landing_pad (eh_landing_pad lp)
1571 eh_landing_pad *pp;
1573 for (pp = &lp->region->landing_pads; *pp != lp; pp = &(*pp)->next_lp)
1574 continue;
1575 *pp = lp->next_lp;
1577 if (lp->post_landing_pad)
1578 EH_LANDING_PAD_NR (lp->post_landing_pad) = 0;
1579 (*cfun->eh->lp_array)[lp->index] = NULL;
1582 /* Splice the EH region at PP from the region tree. */
1584 static void
1585 remove_eh_handler_splicer (eh_region *pp)
1587 eh_region region = *pp;
1588 eh_landing_pad lp;
1590 for (lp = region->landing_pads; lp ; lp = lp->next_lp)
1592 if (lp->post_landing_pad)
1593 EH_LANDING_PAD_NR (lp->post_landing_pad) = 0;
1594 (*cfun->eh->lp_array)[lp->index] = NULL;
1597 if (region->inner)
1599 eh_region p, outer;
1600 outer = region->outer;
1602 *pp = p = region->inner;
1605 p->outer = outer;
1606 pp = &p->next_peer;
1607 p = *pp;
1609 while (p);
1611 *pp = region->next_peer;
1613 (*cfun->eh->region_array)[region->index] = NULL;
1616 /* Splice a single EH region REGION from the region tree.
1618 To unlink REGION, we need to find the pointer to it with a relatively
1619 expensive search in REGION's outer region. If you are going to
1620 remove a number of handlers, using remove_unreachable_eh_regions may
1621 be a better option. */
1623 void
1624 remove_eh_handler (eh_region region)
1626 eh_region *pp, *pp_start, p, outer;
1628 outer = region->outer;
1629 if (outer)
1630 pp_start = &outer->inner;
1631 else
1632 pp_start = &cfun->eh->region_tree;
1633 for (pp = pp_start, p = *pp; p != region; pp = &p->next_peer, p = *pp)
1634 continue;
1636 remove_eh_handler_splicer (pp);
1639 /* Worker for remove_unreachable_eh_regions.
1640 PP is a pointer to the region to start a region tree depth-first
1641 search from. R_REACHABLE is the set of regions that have to be
1642 preserved. */
1644 static void
1645 remove_unreachable_eh_regions_worker (eh_region *pp, sbitmap r_reachable)
1647 while (*pp)
1649 eh_region region = *pp;
1650 remove_unreachable_eh_regions_worker (&region->inner, r_reachable);
1651 if (!bitmap_bit_p (r_reachable, region->index))
1652 remove_eh_handler_splicer (pp);
1653 else
1654 pp = &region->next_peer;
1658 /* Splice all EH regions *not* marked in R_REACHABLE from the region tree.
1659 Do this by traversing the EH tree top-down and splice out regions that
1660 are not marked. By removing regions from the leaves, we avoid costly
1661 searches in the region tree. */
1663 void
1664 remove_unreachable_eh_regions (sbitmap r_reachable)
1666 remove_unreachable_eh_regions_worker (&cfun->eh->region_tree, r_reachable);
1669 /* Invokes CALLBACK for every exception handler landing pad label.
1670 Only used by reload hackery; should not be used by new code. */
1672 void
1673 for_each_eh_label (void (*callback) (rtx))
1675 eh_landing_pad lp;
1676 int i;
1678 for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
1680 if (lp)
1682 rtx_code_label *lab = lp->landing_pad;
1683 if (lab && LABEL_P (lab))
1684 (*callback) (lab);
1689 /* Create the REG_EH_REGION note for INSN, given its ECF_FLAGS for a
1690 call insn.
1692 At the gimple level, we use LP_NR
1693 > 0 : The statement transfers to landing pad LP_NR
1694 = 0 : The statement is outside any EH region
1695 < 0 : The statement is within MUST_NOT_THROW region -LP_NR.
1697 At the rtl level, we use LP_NR
1698 > 0 : The insn transfers to landing pad LP_NR
1699 = 0 : The insn cannot throw
1700 < 0 : The insn is within MUST_NOT_THROW region -LP_NR
1701 = INT_MIN : The insn cannot throw or execute a nonlocal-goto.
1702 missing note: The insn is outside any EH region.
1704 ??? This difference probably ought to be avoided. We could stand
1705 to record nothrow for arbitrary gimple statements, and so avoid
1706 some moderately complex lookups in stmt_could_throw_p. Perhaps
1707 NOTHROW should be mapped on both sides to INT_MIN. Perhaps the
1708 no-nonlocal-goto property should be recorded elsewhere as a bit
1709 on the call_insn directly. Perhaps we should make more use of
1710 attaching the trees to call_insns (reachable via symbol_ref in
1711 direct call cases) and just pull the data out of the trees. */
1713 void
1714 make_reg_eh_region_note (rtx_insn *insn, int ecf_flags, int lp_nr)
1716 rtx value;
1717 if (ecf_flags & ECF_NOTHROW)
1718 value = const0_rtx;
1719 else if (lp_nr != 0)
1720 value = GEN_INT (lp_nr);
1721 else
1722 return;
1723 add_reg_note (insn, REG_EH_REGION, value);
1726 /* Create a REG_EH_REGION note for a CALL_INSN that cannot throw
1727 nor perform a non-local goto. Replace the region note if it
1728 already exists. */
1730 void
1731 make_reg_eh_region_note_nothrow_nononlocal (rtx_insn *insn)
1733 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1734 rtx intmin = GEN_INT (INT_MIN);
1736 if (note != 0)
1737 XEXP (note, 0) = intmin;
1738 else
1739 add_reg_note (insn, REG_EH_REGION, intmin);
1742 /* Return true if INSN could throw, assuming no REG_EH_REGION note
1743 to the contrary. */
1745 bool
1746 insn_could_throw_p (const_rtx insn)
1748 if (!flag_exceptions)
1749 return false;
1750 if (CALL_P (insn))
1751 return true;
1752 if (INSN_P (insn) && cfun->can_throw_non_call_exceptions)
1753 return may_trap_p (PATTERN (insn));
1754 return false;
1757 /* Copy an REG_EH_REGION note to each insn that might throw beginning
1758 at FIRST and ending at LAST. NOTE_OR_INSN is either the source insn
1759 to look for a note, or the note itself. */
1761 void
1762 copy_reg_eh_region_note_forward (rtx note_or_insn, rtx_insn *first, rtx last)
1764 rtx_insn *insn;
1765 rtx note = note_or_insn;
1767 if (INSN_P (note_or_insn))
1769 note = find_reg_note (note_or_insn, REG_EH_REGION, NULL_RTX);
1770 if (note == NULL)
1771 return;
1773 else if (is_a <rtx_insn *> (note_or_insn))
1774 return;
1775 note = XEXP (note, 0);
1777 for (insn = first; insn != last ; insn = NEXT_INSN (insn))
1778 if (!find_reg_note (insn, REG_EH_REGION, NULL_RTX)
1779 && insn_could_throw_p (insn))
1780 add_reg_note (insn, REG_EH_REGION, note);
1783 /* Likewise, but iterate backward. */
1785 void
1786 copy_reg_eh_region_note_backward (rtx note_or_insn, rtx_insn *last, rtx first)
1788 rtx_insn *insn;
1789 rtx note = note_or_insn;
1791 if (INSN_P (note_or_insn))
1793 note = find_reg_note (note_or_insn, REG_EH_REGION, NULL_RTX);
1794 if (note == NULL)
1795 return;
1797 else if (is_a <rtx_insn *> (note_or_insn))
1798 return;
1799 note = XEXP (note, 0);
1801 for (insn = last; insn != first; insn = PREV_INSN (insn))
1802 if (insn_could_throw_p (insn))
1803 add_reg_note (insn, REG_EH_REGION, note);
1807 /* Extract all EH information from INSN. Return true if the insn
1808 was marked NOTHROW. */
1810 static bool
1811 get_eh_region_and_lp_from_rtx (const_rtx insn, eh_region *pr,
1812 eh_landing_pad *plp)
1814 eh_landing_pad lp = NULL;
1815 eh_region r = NULL;
1816 bool ret = false;
1817 rtx note;
1818 int lp_nr;
1820 if (! INSN_P (insn))
1821 goto egress;
1823 if (NONJUMP_INSN_P (insn)
1824 && GET_CODE (PATTERN (insn)) == SEQUENCE)
1825 insn = XVECEXP (PATTERN (insn), 0, 0);
1827 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1828 if (!note)
1830 ret = !insn_could_throw_p (insn);
1831 goto egress;
1834 lp_nr = INTVAL (XEXP (note, 0));
1835 if (lp_nr == 0 || lp_nr == INT_MIN)
1837 ret = true;
1838 goto egress;
1841 if (lp_nr < 0)
1842 r = (*cfun->eh->region_array)[-lp_nr];
1843 else
1845 lp = (*cfun->eh->lp_array)[lp_nr];
1846 r = lp->region;
1849 egress:
1850 *plp = lp;
1851 *pr = r;
1852 return ret;
1855 /* Return the landing pad to which INSN may go, or NULL if it does not
1856 have a reachable landing pad within this function. */
1858 eh_landing_pad
1859 get_eh_landing_pad_from_rtx (const_rtx insn)
1861 eh_landing_pad lp;
1862 eh_region r;
1864 get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1865 return lp;
1868 /* Return the region to which INSN may go, or NULL if it does not
1869 have a reachable region within this function. */
1871 eh_region
1872 get_eh_region_from_rtx (const_rtx insn)
1874 eh_landing_pad lp;
1875 eh_region r;
1877 get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1878 return r;
1881 /* Return true if INSN throws and is caught by something in this function. */
1883 bool
1884 can_throw_internal (const_rtx insn)
1886 return get_eh_landing_pad_from_rtx (insn) != NULL;
1889 /* Return true if INSN throws and escapes from the current function. */
1891 bool
1892 can_throw_external (const_rtx insn)
1894 eh_landing_pad lp;
1895 eh_region r;
1896 bool nothrow;
1898 if (! INSN_P (insn))
1899 return false;
1901 if (NONJUMP_INSN_P (insn)
1902 && GET_CODE (PATTERN (insn)) == SEQUENCE)
1904 rtx_sequence *seq = as_a <rtx_sequence *> (PATTERN (insn));
1905 int i, n = seq->len ();
1907 for (i = 0; i < n; i++)
1908 if (can_throw_external (seq->element (i)))
1909 return true;
1911 return false;
1914 nothrow = get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1916 /* If we can't throw, we obviously can't throw external. */
1917 if (nothrow)
1918 return false;
1920 /* If we have an internal landing pad, then we're not external. */
1921 if (lp != NULL)
1922 return false;
1924 /* If we're not within an EH region, then we are external. */
1925 if (r == NULL)
1926 return true;
1928 /* The only thing that ought to be left is MUST_NOT_THROW regions,
1929 which don't always have landing pads. */
1930 gcc_assert (r->type == ERT_MUST_NOT_THROW);
1931 return false;
1934 /* Return true if INSN cannot throw at all. */
1936 bool
1937 insn_nothrow_p (const_rtx insn)
1939 eh_landing_pad lp;
1940 eh_region r;
1942 if (! INSN_P (insn))
1943 return true;
1945 if (NONJUMP_INSN_P (insn)
1946 && GET_CODE (PATTERN (insn)) == SEQUENCE)
1948 rtx_sequence *seq = as_a <rtx_sequence *> (PATTERN (insn));
1949 int i, n = seq->len ();
1951 for (i = 0; i < n; i++)
1952 if (!insn_nothrow_p (seq->element (i)))
1953 return false;
1955 return true;
1958 return get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1961 /* Return true if INSN can perform a non-local goto. */
1962 /* ??? This test is here in this file because it (ab)uses REG_EH_REGION. */
1964 bool
1965 can_nonlocal_goto (const rtx_insn *insn)
1967 if (nonlocal_goto_handler_labels && CALL_P (insn))
1969 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1970 if (!note || INTVAL (XEXP (note, 0)) != INT_MIN)
1971 return true;
1973 return false;
1976 /* Set TREE_NOTHROW and crtl->all_throwers_are_sibcalls. */
1978 static unsigned int
1979 set_nothrow_function_flags (void)
1981 rtx_insn *insn;
1983 crtl->nothrow = 1;
1985 /* Assume crtl->all_throwers_are_sibcalls until we encounter
1986 something that can throw an exception. We specifically exempt
1987 CALL_INSNs that are SIBLING_CALL_P, as these are really jumps,
1988 and can't throw. Most CALL_INSNs are not SIBLING_CALL_P, so this
1989 is optimistic. */
1991 crtl->all_throwers_are_sibcalls = 1;
1993 /* If we don't know that this implementation of the function will
1994 actually be used, then we must not set TREE_NOTHROW, since
1995 callers must not assume that this function does not throw. */
1996 if (TREE_NOTHROW (current_function_decl))
1997 return 0;
1999 if (! flag_exceptions)
2000 return 0;
2002 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2003 if (can_throw_external (insn))
2005 crtl->nothrow = 0;
2007 if (!CALL_P (insn) || !SIBLING_CALL_P (insn))
2009 crtl->all_throwers_are_sibcalls = 0;
2010 return 0;
2014 if (crtl->nothrow
2015 && (cgraph_node::get (current_function_decl)->get_availability ()
2016 >= AVAIL_AVAILABLE))
2018 struct cgraph_node *node = cgraph_node::get (current_function_decl);
2019 struct cgraph_edge *e;
2020 for (e = node->callers; e; e = e->next_caller)
2021 e->can_throw_external = false;
2022 node->set_nothrow_flag (true);
2024 if (dump_file)
2025 fprintf (dump_file, "Marking function nothrow: %s\n\n",
2026 current_function_name ());
2028 return 0;
2031 namespace {
2033 const pass_data pass_data_set_nothrow_function_flags =
2035 RTL_PASS, /* type */
2036 "nothrow", /* name */
2037 OPTGROUP_NONE, /* optinfo_flags */
2038 TV_NONE, /* tv_id */
2039 0, /* properties_required */
2040 0, /* properties_provided */
2041 0, /* properties_destroyed */
2042 0, /* todo_flags_start */
2043 0, /* todo_flags_finish */
2046 class pass_set_nothrow_function_flags : public rtl_opt_pass
2048 public:
2049 pass_set_nothrow_function_flags (gcc::context *ctxt)
2050 : rtl_opt_pass (pass_data_set_nothrow_function_flags, ctxt)
2053 /* opt_pass methods: */
2054 unsigned int execute (function *) final override
2056 return set_nothrow_function_flags ();
2059 }; // class pass_set_nothrow_function_flags
2061 } // anon namespace
2063 rtl_opt_pass *
2064 make_pass_set_nothrow_function_flags (gcc::context *ctxt)
2066 return new pass_set_nothrow_function_flags (ctxt);
2070 /* Various hooks for unwind library. */
2072 /* Expand the EH support builtin functions:
2073 __builtin_eh_pointer and __builtin_eh_filter. */
2075 static eh_region
2076 expand_builtin_eh_common (tree region_nr_t)
2078 HOST_WIDE_INT region_nr;
2079 eh_region region;
2081 gcc_assert (tree_fits_shwi_p (region_nr_t));
2082 region_nr = tree_to_shwi (region_nr_t);
2084 region = (*cfun->eh->region_array)[region_nr];
2086 /* ??? We shouldn't have been able to delete a eh region without
2087 deleting all the code that depended on it. */
2088 gcc_assert (region != NULL);
2090 return region;
2093 /* Expand to the exc_ptr value from the given eh region. */
2096 expand_builtin_eh_pointer (tree exp)
2098 eh_region region
2099 = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 0));
2100 if (region->exc_ptr_reg == NULL)
2101 region->exc_ptr_reg = gen_reg_rtx (ptr_mode);
2102 return region->exc_ptr_reg;
2105 /* Expand to the filter value from the given eh region. */
2108 expand_builtin_eh_filter (tree exp)
2110 eh_region region
2111 = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 0));
2112 if (region->filter_reg == NULL)
2113 region->filter_reg = gen_reg_rtx (targetm.eh_return_filter_mode ());
2114 return region->filter_reg;
2117 /* Copy the exc_ptr and filter values from one landing pad's registers
2118 to another. This is used to inline the resx statement. */
2121 expand_builtin_eh_copy_values (tree exp)
2123 eh_region dst
2124 = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 0));
2125 eh_region src
2126 = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 1));
2127 scalar_int_mode fmode = targetm.eh_return_filter_mode ();
2129 if (dst->exc_ptr_reg == NULL)
2130 dst->exc_ptr_reg = gen_reg_rtx (ptr_mode);
2131 if (src->exc_ptr_reg == NULL)
2132 src->exc_ptr_reg = gen_reg_rtx (ptr_mode);
2134 if (dst->filter_reg == NULL)
2135 dst->filter_reg = gen_reg_rtx (fmode);
2136 if (src->filter_reg == NULL)
2137 src->filter_reg = gen_reg_rtx (fmode);
2139 emit_move_insn (dst->exc_ptr_reg, src->exc_ptr_reg);
2140 emit_move_insn (dst->filter_reg, src->filter_reg);
2142 return const0_rtx;
2145 /* Do any necessary initialization to access arbitrary stack frames.
2146 On the SPARC, this means flushing the register windows. */
2148 void
2149 expand_builtin_unwind_init (void)
2151 /* Set this so all the registers get saved in our frame; we need to be
2152 able to copy the saved values for any registers from frames we unwind. */
2153 crtl->saves_all_registers = 1;
2155 SETUP_FRAME_ADDRESSES ();
2158 /* Map a non-negative number to an eh return data register number; expands
2159 to -1 if no return data register is associated with the input number.
2160 At least the inputs 0 and 1 must be mapped; the target may provide more. */
2163 expand_builtin_eh_return_data_regno (tree exp)
2165 tree which = CALL_EXPR_ARG (exp, 0);
2166 unsigned HOST_WIDE_INT iwhich;
2168 if (TREE_CODE (which) != INTEGER_CST)
2170 error ("argument of %<__builtin_eh_return_regno%> must be constant");
2171 return constm1_rtx;
2174 if (!tree_fits_uhwi_p (which))
2175 return constm1_rtx;
2177 iwhich = tree_to_uhwi (which);
2178 iwhich = EH_RETURN_DATA_REGNO (iwhich);
2179 if (iwhich == INVALID_REGNUM)
2180 return constm1_rtx;
2182 #ifdef DWARF_FRAME_REGNUM
2183 iwhich = DWARF_FRAME_REGNUM (iwhich);
2184 #else
2185 iwhich = DEBUGGER_REGNO (iwhich);
2186 #endif
2188 return GEN_INT (iwhich);
2191 /* Given a value extracted from the return address register or stack slot,
2192 return the actual address encoded in that value. */
2195 expand_builtin_extract_return_addr (tree addr_tree)
2197 rtx addr = expand_expr (addr_tree, NULL_RTX, Pmode, EXPAND_NORMAL);
2199 if (GET_MODE (addr) != Pmode
2200 && GET_MODE (addr) != VOIDmode)
2202 #ifdef POINTERS_EXTEND_UNSIGNED
2203 addr = convert_memory_address (Pmode, addr);
2204 #else
2205 addr = convert_to_mode (Pmode, addr, 0);
2206 #endif
2209 /* First mask out any unwanted bits. */
2210 rtx mask = MASK_RETURN_ADDR;
2211 if (mask)
2212 expand_and (Pmode, addr, mask, addr);
2214 /* Then adjust to find the real return address. */
2215 if (RETURN_ADDR_OFFSET)
2216 addr = plus_constant (Pmode, addr, RETURN_ADDR_OFFSET);
2218 return addr;
2221 /* Given an actual address in addr_tree, do any necessary encoding
2222 and return the value to be stored in the return address register or
2223 stack slot so the epilogue will return to that address. */
2226 expand_builtin_frob_return_addr (tree addr_tree)
2228 rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, EXPAND_NORMAL);
2230 addr = convert_memory_address (Pmode, addr);
2232 if (RETURN_ADDR_OFFSET)
2234 addr = force_reg (Pmode, addr);
2235 addr = plus_constant (Pmode, addr, -RETURN_ADDR_OFFSET);
2238 return addr;
2241 /* Set up the epilogue with the magic bits we'll need to return to the
2242 exception handler. */
2244 void
2245 expand_builtin_eh_return (tree stackadj_tree ATTRIBUTE_UNUSED,
2246 tree handler_tree)
2248 rtx tmp;
2250 #ifdef EH_RETURN_STACKADJ_RTX
2251 tmp = expand_expr (stackadj_tree, crtl->eh.ehr_stackadj,
2252 VOIDmode, EXPAND_NORMAL);
2253 tmp = convert_memory_address (Pmode, tmp);
2254 if (!crtl->eh.ehr_stackadj)
2255 crtl->eh.ehr_stackadj = copy_addr_to_reg (tmp);
2256 else if (tmp != crtl->eh.ehr_stackadj)
2257 emit_move_insn (crtl->eh.ehr_stackadj, tmp);
2258 #endif
2260 tmp = expand_expr (handler_tree, crtl->eh.ehr_handler,
2261 VOIDmode, EXPAND_NORMAL);
2262 tmp = convert_memory_address (Pmode, tmp);
2263 if (!crtl->eh.ehr_handler)
2264 crtl->eh.ehr_handler = copy_addr_to_reg (tmp);
2265 else if (tmp != crtl->eh.ehr_handler)
2266 emit_move_insn (crtl->eh.ehr_handler, tmp);
2268 if (!crtl->eh.ehr_label)
2269 crtl->eh.ehr_label = gen_label_rtx ();
2270 emit_jump (crtl->eh.ehr_label);
2273 /* Expand __builtin_eh_return. This exit path from the function loads up
2274 the eh return data registers, adjusts the stack, and branches to a
2275 given PC other than the normal return address. */
2277 void
2278 expand_eh_return (void)
2280 rtx_code_label *around_label;
2282 if (! crtl->eh.ehr_label)
2283 return;
2285 crtl->calls_eh_return = 1;
2287 #ifdef EH_RETURN_STACKADJ_RTX
2288 emit_move_insn (EH_RETURN_STACKADJ_RTX, const0_rtx);
2289 #endif
2291 #ifdef EH_RETURN_TAKEN_RTX
2292 emit_move_insn (EH_RETURN_TAKEN_RTX, const0_rtx);
2293 #endif
2295 around_label = gen_label_rtx ();
2296 emit_jump (around_label);
2298 emit_label (crtl->eh.ehr_label);
2299 clobber_return_register ();
2301 #ifdef EH_RETURN_STACKADJ_RTX
2302 emit_move_insn (EH_RETURN_STACKADJ_RTX, crtl->eh.ehr_stackadj);
2303 #endif
2305 #ifdef EH_RETURN_TAKEN_RTX
2306 emit_move_insn (EH_RETURN_TAKEN_RTX, const1_rtx);
2307 #endif
2309 if (targetm.have_eh_return ())
2310 emit_insn (targetm.gen_eh_return (crtl->eh.ehr_handler));
2311 else
2313 if (rtx handler = EH_RETURN_HANDLER_RTX)
2314 emit_move_insn (handler, crtl->eh.ehr_handler);
2315 else
2316 error ("%<__builtin_eh_return%> not supported on this target");
2319 #ifdef EH_RETURN_TAKEN_RTX
2320 rtx_code_label *eh_done_label = gen_label_rtx ();
2321 emit_jump (eh_done_label);
2322 #endif
2324 emit_label (around_label);
2326 #ifdef EH_RETURN_TAKEN_RTX
2327 for (rtx tmp : { EH_RETURN_STACKADJ_RTX, EH_RETURN_HANDLER_RTX })
2328 if (tmp && REG_P (tmp))
2329 emit_clobber (tmp);
2330 emit_label (eh_done_label);
2331 #endif
2334 /* Convert a ptr_mode address ADDR_TREE to a Pmode address controlled by
2335 POINTERS_EXTEND_UNSIGNED and return it. */
2338 expand_builtin_extend_pointer (tree addr_tree)
2340 rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, EXPAND_NORMAL);
2341 int extend;
2343 #ifdef POINTERS_EXTEND_UNSIGNED
2344 extend = POINTERS_EXTEND_UNSIGNED;
2345 #else
2346 /* The previous EH code did an unsigned extend by default, so we do this also
2347 for consistency. */
2348 extend = 1;
2349 #endif
2351 return convert_modes (targetm.unwind_word_mode (), ptr_mode, addr, extend);
2354 static int
2355 add_action_record (action_hash_type *ar_hash, int filter, int next)
2357 struct action_record **slot, *new_ar, tmp;
2359 tmp.filter = filter;
2360 tmp.next = next;
2361 slot = ar_hash->find_slot (&tmp, INSERT);
2363 if ((new_ar = *slot) == NULL)
2365 new_ar = XNEW (struct action_record);
2366 new_ar->offset = crtl->eh.action_record_data->length () + 1;
2367 new_ar->filter = filter;
2368 new_ar->next = next;
2369 *slot = new_ar;
2371 /* The filter value goes in untouched. The link to the next
2372 record is a "self-relative" byte offset, or zero to indicate
2373 that there is no next record. So convert the absolute 1 based
2374 indices we've been carrying around into a displacement. */
2376 push_sleb128 (&crtl->eh.action_record_data, filter);
2377 if (next)
2378 next -= crtl->eh.action_record_data->length () + 1;
2379 push_sleb128 (&crtl->eh.action_record_data, next);
2382 return new_ar->offset;
2385 static int
2386 collect_one_action_chain (action_hash_type *ar_hash, eh_region region)
2388 int next;
2390 /* If we've reached the top of the region chain, then we have
2391 no actions, and require no landing pad. */
2392 if (region == NULL)
2393 return -1;
2395 switch (region->type)
2397 case ERT_CLEANUP:
2399 eh_region r;
2400 /* A cleanup adds a zero filter to the beginning of the chain, but
2401 there are special cases to look out for. If there are *only*
2402 cleanups along a path, then it compresses to a zero action.
2403 Further, if there are multiple cleanups along a path, we only
2404 need to represent one of them, as that is enough to trigger
2405 entry to the landing pad at runtime. */
2406 next = collect_one_action_chain (ar_hash, region->outer);
2407 if (next <= 0)
2408 return 0;
2409 for (r = region->outer; r ; r = r->outer)
2410 if (r->type == ERT_CLEANUP)
2411 return next;
2412 return add_action_record (ar_hash, 0, next);
2415 case ERT_TRY:
2417 eh_catch c;
2419 /* Process the associated catch regions in reverse order.
2420 If there's a catch-all handler, then we don't need to
2421 search outer regions. Use a magic -3 value to record
2422 that we haven't done the outer search. */
2423 next = -3;
2424 for (c = region->u.eh_try.last_catch; c ; c = c->prev_catch)
2426 if (c->type_list == NULL)
2428 /* Retrieve the filter from the head of the filter list
2429 where we have stored it (see assign_filter_values). */
2430 int filter = TREE_INT_CST_LOW (TREE_VALUE (c->filter_list));
2431 next = add_action_record (ar_hash, filter, 0);
2433 else
2435 /* Once the outer search is done, trigger an action record for
2436 each filter we have. */
2437 tree flt_node;
2439 if (next == -3)
2441 next = collect_one_action_chain (ar_hash, region->outer);
2443 /* If there is no next action, terminate the chain. */
2444 if (next == -1)
2445 next = 0;
2446 /* If all outer actions are cleanups or must_not_throw,
2447 we'll have no action record for it, since we had wanted
2448 to encode these states in the call-site record directly.
2449 Add a cleanup action to the chain to catch these. */
2450 else if (next <= 0)
2451 next = add_action_record (ar_hash, 0, 0);
2454 flt_node = c->filter_list;
2455 for (; flt_node; flt_node = TREE_CHAIN (flt_node))
2457 int filter = TREE_INT_CST_LOW (TREE_VALUE (flt_node));
2458 next = add_action_record (ar_hash, filter, next);
2462 return next;
2465 case ERT_ALLOWED_EXCEPTIONS:
2466 /* An exception specification adds its filter to the
2467 beginning of the chain. */
2468 next = collect_one_action_chain (ar_hash, region->outer);
2470 /* If there is no next action, terminate the chain. */
2471 if (next == -1)
2472 next = 0;
2473 /* If all outer actions are cleanups or must_not_throw,
2474 we'll have no action record for it, since we had wanted
2475 to encode these states in the call-site record directly.
2476 Add a cleanup action to the chain to catch these. */
2477 else if (next <= 0)
2478 next = add_action_record (ar_hash, 0, 0);
2480 return add_action_record (ar_hash, region->u.allowed.filter, next);
2482 case ERT_MUST_NOT_THROW:
2483 /* A must-not-throw region with no inner handlers or cleanups
2484 requires no call-site entry. Note that this differs from
2485 the no handler or cleanup case in that we do require an lsda
2486 to be generated. Return a magic -2 value to record this. */
2487 return -2;
2490 gcc_unreachable ();
2493 static int
2494 add_call_site (rtx landing_pad, int action, int section)
2496 call_site_record record;
2498 record = ggc_alloc<call_site_record_d> ();
2499 record->landing_pad = landing_pad;
2500 record->action = action;
2502 vec_safe_push (crtl->eh.call_site_record_v[section], record);
2504 return call_site_base + crtl->eh.call_site_record_v[section]->length () - 1;
2507 static rtx_note *
2508 emit_note_eh_region_end (rtx_insn *insn)
2510 return emit_note_after (NOTE_INSN_EH_REGION_END, insn);
2513 /* Add NOP after NOTE_INSN_SWITCH_TEXT_SECTIONS when the cold section starts
2514 with landing pad.
2515 With landing pad being at offset 0 from the start label of the section
2516 we would miss EH delivery because 0 is special and means no landing pad. */
2518 static bool
2519 maybe_add_nop_after_section_switch (void)
2521 if (!crtl->uses_eh_lsda
2522 || !crtl->eh.call_site_record_v[1])
2523 return false;
2524 int n = vec_safe_length (crtl->eh.call_site_record_v[1]);
2525 hash_set<rtx_insn *> visited;
2527 for (int i = 0; i < n; ++i)
2529 struct call_site_record_d *cs
2530 = (*crtl->eh.call_site_record_v[1])[i];
2531 if (cs->landing_pad)
2533 rtx_insn *insn = as_a <rtx_insn *> (cs->landing_pad);
2534 while (true)
2536 /* Landing pads have LABEL_PRESERVE_P flag set. This check make
2537 sure that we do not walk past landing pad visited earlier
2538 which would result in possible quadratic behaviour. */
2539 if (LABEL_P (insn) && LABEL_PRESERVE_P (insn)
2540 && visited.add (insn))
2541 break;
2543 /* Conservatively assume that ASM insn may be empty. We have
2544 now way to tell what they contain. */
2545 if (active_insn_p (insn)
2546 && GET_CODE (PATTERN (insn)) != ASM_INPUT
2547 && GET_CODE (PATTERN (insn)) != ASM_OPERANDS)
2548 break;
2550 /* If we reached the start of hot section, then NOP will be
2551 needed. */
2552 if (GET_CODE (insn) == NOTE
2553 && NOTE_KIND (insn) == NOTE_INSN_SWITCH_TEXT_SECTIONS)
2555 emit_insn_after (gen_nop (), insn);
2556 break;
2559 /* We visit only labels from cold section. We should never hit
2560 begining of the insn stream here. */
2561 insn = PREV_INSN (insn);
2565 return false;
2568 /* Turn REG_EH_REGION notes back into NOTE_INSN_EH_REGION notes.
2569 The new note numbers will not refer to region numbers, but
2570 instead to call site entries. */
2572 static unsigned int
2573 convert_to_eh_region_ranges (void)
2575 rtx insn;
2576 rtx_insn *iter;
2577 rtx_note *note;
2578 action_hash_type ar_hash (31);
2579 int last_action = -3;
2580 rtx_insn *last_action_insn = NULL;
2581 rtx last_landing_pad = NULL_RTX;
2582 rtx_insn *first_no_action_insn = NULL;
2583 int call_site = 0;
2584 int cur_sec = 0;
2585 rtx_insn *section_switch_note = NULL;
2586 rtx_insn *first_no_action_insn_before_switch = NULL;
2587 rtx_insn *last_no_action_insn_before_switch = NULL;
2588 int saved_call_site_base = call_site_base;
2590 vec_alloc (crtl->eh.action_record_data, 64);
2592 for (iter = get_insns (); iter ; iter = NEXT_INSN (iter))
2593 if (INSN_P (iter))
2595 eh_landing_pad lp;
2596 eh_region region;
2597 bool nothrow;
2598 int this_action;
2599 rtx_code_label *this_landing_pad;
2601 insn = iter;
2602 if (NONJUMP_INSN_P (insn)
2603 && GET_CODE (PATTERN (insn)) == SEQUENCE)
2604 insn = XVECEXP (PATTERN (insn), 0, 0);
2606 nothrow = get_eh_region_and_lp_from_rtx (insn, &region, &lp);
2607 if (nothrow)
2608 continue;
2609 if (region)
2610 this_action = collect_one_action_chain (&ar_hash, region);
2611 else
2612 this_action = -1;
2614 /* Existence of catch handlers, or must-not-throw regions
2615 implies that an lsda is needed (even if empty). */
2616 if (this_action != -1)
2617 crtl->uses_eh_lsda = 1;
2619 /* Delay creation of region notes for no-action regions
2620 until we're sure that an lsda will be required. */
2621 else if (last_action == -3)
2623 first_no_action_insn = iter;
2624 last_action = -1;
2627 if (this_action >= 0)
2628 this_landing_pad = lp->landing_pad;
2629 else
2630 this_landing_pad = NULL;
2632 /* Differing actions or landing pads implies a change in call-site
2633 info, which implies some EH_REGION note should be emitted. */
2634 if (last_action != this_action
2635 || last_landing_pad != this_landing_pad)
2637 /* If there is a queued no-action region in the other section
2638 with hot/cold partitioning, emit it now. */
2639 if (first_no_action_insn_before_switch)
2641 gcc_assert (this_action != -1
2642 && last_action == (first_no_action_insn
2643 ? -1 : -3));
2644 call_site = add_call_site (NULL_RTX, 0, 0);
2645 note = emit_note_before (NOTE_INSN_EH_REGION_BEG,
2646 first_no_action_insn_before_switch);
2647 NOTE_EH_HANDLER (note) = call_site;
2648 note
2649 = emit_note_eh_region_end (last_no_action_insn_before_switch);
2650 NOTE_EH_HANDLER (note) = call_site;
2651 gcc_assert (last_action != -3
2652 || (last_action_insn
2653 == last_no_action_insn_before_switch));
2654 first_no_action_insn_before_switch = NULL;
2655 last_no_action_insn_before_switch = NULL;
2656 call_site_base++;
2658 /* If we'd not seen a previous action (-3) or the previous
2659 action was must-not-throw (-2), then we do not need an
2660 end note. */
2661 if (last_action >= -1)
2663 /* If we delayed the creation of the begin, do it now. */
2664 if (first_no_action_insn)
2666 call_site = add_call_site (NULL_RTX, 0, cur_sec);
2667 note = emit_note_before (NOTE_INSN_EH_REGION_BEG,
2668 first_no_action_insn);
2669 NOTE_EH_HANDLER (note) = call_site;
2670 first_no_action_insn = NULL;
2673 note = emit_note_eh_region_end (last_action_insn);
2674 NOTE_EH_HANDLER (note) = call_site;
2677 /* If the new action is must-not-throw, then no region notes
2678 are created. */
2679 if (this_action >= -1)
2681 call_site = add_call_site (this_landing_pad,
2682 this_action < 0 ? 0 : this_action,
2683 cur_sec);
2684 note = emit_note_before (NOTE_INSN_EH_REGION_BEG, iter);
2685 NOTE_EH_HANDLER (note) = call_site;
2688 last_action = this_action;
2689 last_landing_pad = this_landing_pad;
2691 last_action_insn = iter;
2693 else if (NOTE_P (iter)
2694 && NOTE_KIND (iter) == NOTE_INSN_SWITCH_TEXT_SECTIONS)
2696 gcc_assert (section_switch_note == NULL_RTX);
2697 gcc_assert (flag_reorder_blocks_and_partition);
2698 section_switch_note = iter;
2699 if (first_no_action_insn)
2701 first_no_action_insn_before_switch = first_no_action_insn;
2702 last_no_action_insn_before_switch = last_action_insn;
2703 first_no_action_insn = NULL;
2704 gcc_assert (last_action == -1);
2705 last_action = -3;
2707 /* Force closing of current EH region before section switch and
2708 opening a new one afterwards. */
2709 else if (last_action != -3)
2710 last_landing_pad = pc_rtx;
2711 if (crtl->eh.call_site_record_v[cur_sec])
2712 call_site_base += crtl->eh.call_site_record_v[cur_sec]->length ();
2713 cur_sec++;
2714 gcc_assert (crtl->eh.call_site_record_v[cur_sec] == NULL);
2715 vec_alloc (crtl->eh.call_site_record_v[cur_sec], 10);
2718 if (last_action >= -1 && ! first_no_action_insn)
2720 note = emit_note_eh_region_end (last_action_insn);
2721 NOTE_EH_HANDLER (note) = call_site;
2724 call_site_base = saved_call_site_base;
2726 return 0;
2729 namespace {
2731 const pass_data pass_data_convert_to_eh_region_ranges =
2733 RTL_PASS, /* type */
2734 "eh_ranges", /* name */
2735 OPTGROUP_NONE, /* optinfo_flags */
2736 TV_NONE, /* tv_id */
2737 0, /* properties_required */
2738 0, /* properties_provided */
2739 0, /* properties_destroyed */
2740 0, /* todo_flags_start */
2741 0, /* todo_flags_finish */
2744 class pass_convert_to_eh_region_ranges : public rtl_opt_pass
2746 public:
2747 pass_convert_to_eh_region_ranges (gcc::context *ctxt)
2748 : rtl_opt_pass (pass_data_convert_to_eh_region_ranges, ctxt)
2751 /* opt_pass methods: */
2752 bool gate (function *) final override;
2753 unsigned int execute (function *) final override
2755 int ret = convert_to_eh_region_ranges ();
2756 maybe_add_nop_after_section_switch ();
2757 return ret;
2760 }; // class pass_convert_to_eh_region_ranges
2762 bool
2763 pass_convert_to_eh_region_ranges::gate (function *)
2765 /* Nothing to do for SJLJ exceptions or if no regions created. */
2766 if (cfun->eh->region_tree == NULL)
2767 return false;
2768 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
2769 return false;
2770 return true;
2773 } // anon namespace
2775 rtl_opt_pass *
2776 make_pass_convert_to_eh_region_ranges (gcc::context *ctxt)
2778 return new pass_convert_to_eh_region_ranges (ctxt);
2781 static void
2782 push_uleb128 (vec<uchar, va_gc> **data_area, unsigned int value)
2786 unsigned char byte = value & 0x7f;
2787 value >>= 7;
2788 if (value)
2789 byte |= 0x80;
2790 vec_safe_push (*data_area, byte);
2792 while (value);
2795 static void
2796 push_sleb128 (vec<uchar, va_gc> **data_area, int value)
2798 unsigned char byte;
2799 int more;
2803 byte = value & 0x7f;
2804 value >>= 7;
2805 more = ! ((value == 0 && (byte & 0x40) == 0)
2806 || (value == -1 && (byte & 0x40) != 0));
2807 if (more)
2808 byte |= 0x80;
2809 vec_safe_push (*data_area, byte);
2811 while (more);
2815 static int
2816 dw2_size_of_call_site_table (int section)
2818 int n = vec_safe_length (crtl->eh.call_site_record_v[section]);
2819 int size = n * (4 + 4 + 4);
2820 int i;
2822 for (i = 0; i < n; ++i)
2824 struct call_site_record_d *cs =
2825 (*crtl->eh.call_site_record_v[section])[i];
2826 size += size_of_uleb128 (cs->action);
2829 return size;
2832 static int
2833 sjlj_size_of_call_site_table (void)
2835 int n = vec_safe_length (crtl->eh.call_site_record_v[0]);
2836 int size = 0;
2837 int i;
2839 for (i = 0; i < n; ++i)
2841 struct call_site_record_d *cs =
2842 (*crtl->eh.call_site_record_v[0])[i];
2843 size += size_of_uleb128 (INTVAL (cs->landing_pad));
2844 size += size_of_uleb128 (cs->action);
2847 return size;
2850 static void
2851 dw2_output_call_site_table (int cs_format, int section)
2853 int n = vec_safe_length (crtl->eh.call_site_record_v[section]);
2854 int i;
2855 const char *begin;
2857 if (section == 0)
2858 begin = current_function_func_begin_label;
2859 else if (first_function_block_is_cold)
2860 begin = crtl->subsections.hot_section_label;
2861 else
2862 begin = crtl->subsections.cold_section_label;
2864 for (i = 0; i < n; ++i)
2866 struct call_site_record_d *cs = (*crtl->eh.call_site_record_v[section])[i];
2867 char reg_start_lab[32];
2868 char reg_end_lab[32];
2869 char landing_pad_lab[32];
2871 ASM_GENERATE_INTERNAL_LABEL (reg_start_lab, "LEHB", call_site_base + i);
2872 ASM_GENERATE_INTERNAL_LABEL (reg_end_lab, "LEHE", call_site_base + i);
2874 if (cs->landing_pad)
2875 ASM_GENERATE_INTERNAL_LABEL (landing_pad_lab, "L",
2876 CODE_LABEL_NUMBER (cs->landing_pad));
2878 /* ??? Perhaps use insn length scaling if the assembler supports
2879 generic arithmetic. */
2880 /* ??? Perhaps use attr_length to choose data1 or data2 instead of
2881 data4 if the function is small enough. */
2882 if (cs_format == DW_EH_PE_uleb128)
2884 dw2_asm_output_delta_uleb128 (reg_start_lab, begin,
2885 "region %d start", i);
2886 dw2_asm_output_delta_uleb128 (reg_end_lab, reg_start_lab,
2887 "length");
2888 if (cs->landing_pad)
2889 dw2_asm_output_delta_uleb128 (landing_pad_lab, begin,
2890 "landing pad");
2891 else
2892 dw2_asm_output_data_uleb128 (0, "landing pad");
2894 else
2896 dw2_asm_output_delta (4, reg_start_lab, begin,
2897 "region %d start", i);
2898 dw2_asm_output_delta (4, reg_end_lab, reg_start_lab, "length");
2899 if (cs->landing_pad)
2900 dw2_asm_output_delta (4, landing_pad_lab, begin,
2901 "landing pad");
2902 else
2903 dw2_asm_output_data (4, 0, "landing pad");
2905 dw2_asm_output_data_uleb128 (cs->action, "action");
2908 call_site_base += n;
2911 static void
2912 sjlj_output_call_site_table (void)
2914 int n = vec_safe_length (crtl->eh.call_site_record_v[0]);
2915 int i;
2917 for (i = 0; i < n; ++i)
2919 struct call_site_record_d *cs = (*crtl->eh.call_site_record_v[0])[i];
2921 dw2_asm_output_data_uleb128 (INTVAL (cs->landing_pad),
2922 "region %d landing pad", i);
2923 dw2_asm_output_data_uleb128 (cs->action, "action");
2926 call_site_base += n;
2929 /* Switch to the section that should be used for exception tables. */
2931 static void
2932 switch_to_exception_section (const char * ARG_UNUSED (fnname))
2934 section *s;
2936 if (exception_section)
2937 s = exception_section;
2938 else
2940 int flags;
2942 if (EH_TABLES_CAN_BE_READ_ONLY)
2944 int tt_format =
2945 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
2946 flags = ((! flag_pic
2947 || ((tt_format & 0x70) != DW_EH_PE_absptr
2948 && (tt_format & 0x70) != DW_EH_PE_aligned))
2949 ? 0 : SECTION_WRITE);
2951 else
2952 flags = SECTION_WRITE;
2954 /* Compute the section and cache it into exception_section,
2955 unless it depends on the function name. */
2956 if (targetm_common.have_named_sections)
2958 #ifdef HAVE_LD_EH_GC_SECTIONS
2959 if (flag_function_sections
2960 || (DECL_COMDAT_GROUP (current_function_decl) && HAVE_COMDAT_GROUP))
2962 char *section_name = XNEWVEC (char, strlen (fnname) + 32);
2963 /* The EH table must match the code section, so only mark
2964 it linkonce if we have COMDAT groups to tie them together. */
2965 if (DECL_COMDAT_GROUP (current_function_decl) && HAVE_COMDAT_GROUP)
2966 flags |= SECTION_LINKONCE;
2967 sprintf (section_name, ".gcc_except_table.%s", fnname);
2968 s = get_section (section_name, flags, current_function_decl);
2969 free (section_name);
2971 else
2972 #endif
2973 exception_section
2974 = s = get_section (".gcc_except_table", flags, NULL);
2976 else
2977 exception_section
2978 = s = flags == SECTION_WRITE ? data_section : readonly_data_section;
2981 switch_to_section (s);
2984 /* Output a reference from an exception table to the type_info object TYPE.
2985 TT_FORMAT and TT_FORMAT_SIZE describe the DWARF encoding method used for
2986 the value. */
2988 static void
2989 output_ttype (tree type, int tt_format, int tt_format_size)
2991 rtx value;
2992 bool is_public = true;
2994 if (type == NULL_TREE)
2995 value = const0_rtx;
2996 else
2998 /* FIXME lto. pass_ipa_free_lang_data changes all types to
2999 runtime types so TYPE should already be a runtime type
3000 reference. When pass_ipa_free_lang data is made a default
3001 pass, we can then remove the call to lookup_type_for_runtime
3002 below. */
3003 if (TYPE_P (type))
3004 type = lookup_type_for_runtime (type);
3006 value = expand_expr (type, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
3008 /* Let cgraph know that the rtti decl is used. Not all of the
3009 paths below go through assemble_integer, which would take
3010 care of this for us. */
3011 STRIP_NOPS (type);
3012 if (TREE_CODE (type) == ADDR_EXPR)
3014 type = TREE_OPERAND (type, 0);
3015 if (VAR_P (type))
3016 is_public = TREE_PUBLIC (type);
3018 else
3019 gcc_assert (TREE_CODE (type) == INTEGER_CST);
3022 /* Allow the target to override the type table entry format. */
3023 if (targetm.asm_out.ttype (value))
3024 return;
3026 if (tt_format == DW_EH_PE_absptr || tt_format == DW_EH_PE_aligned)
3027 assemble_integer (value, tt_format_size,
3028 tt_format_size * BITS_PER_UNIT, 1);
3029 else
3030 dw2_asm_output_encoded_addr_rtx (tt_format, value, is_public, NULL);
3033 /* Output an exception table for the current function according to SECTION.
3035 If the function has been partitioned into hot and cold parts, value 0 for
3036 SECTION refers to the table associated with the hot part while value 1
3037 refers to the table associated with the cold part. If the function has
3038 not been partitioned, value 0 refers to the single exception table. */
3040 static void
3041 output_one_function_exception_table (int section)
3043 int tt_format, cs_format, lp_format, i;
3044 char ttype_label[32];
3045 char cs_after_size_label[32];
3046 char cs_end_label[32];
3047 int call_site_len;
3048 int have_tt_data;
3049 int tt_format_size = 0;
3051 have_tt_data = (vec_safe_length (cfun->eh->ttype_data)
3052 || (targetm.arm_eabi_unwinder
3053 ? vec_safe_length (cfun->eh->ehspec_data.arm_eabi)
3054 : vec_safe_length (cfun->eh->ehspec_data.other)));
3056 /* Indicate the format of the @TType entries. */
3057 if (! have_tt_data)
3058 tt_format = DW_EH_PE_omit;
3059 else
3061 tt_format = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3062 if (HAVE_AS_LEB128)
3063 ASM_GENERATE_INTERNAL_LABEL (ttype_label,
3064 section ? "LLSDATTC" : "LLSDATT",
3065 current_function_funcdef_no);
3067 tt_format_size = size_of_encoded_value (tt_format);
3069 assemble_align (tt_format_size * BITS_PER_UNIT);
3072 targetm.asm_out.internal_label (asm_out_file, section ? "LLSDAC" : "LLSDA",
3073 current_function_funcdef_no);
3075 /* The LSDA header. */
3077 /* Indicate the format of the landing pad start pointer. An omitted
3078 field implies @LPStart == @Start. */
3079 /* Currently we always put @LPStart == @Start. This field would
3080 be most useful in moving the landing pads completely out of
3081 line to another section, but it could also be used to minimize
3082 the size of uleb128 landing pad offsets. */
3083 lp_format = DW_EH_PE_omit;
3084 dw2_asm_output_data (1, lp_format, "@LPStart format (%s)",
3085 eh_data_format_name (lp_format));
3087 /* @LPStart pointer would go here. */
3089 dw2_asm_output_data (1, tt_format, "@TType format (%s)",
3090 eh_data_format_name (tt_format));
3092 if (!HAVE_AS_LEB128)
3094 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
3095 call_site_len = sjlj_size_of_call_site_table ();
3096 else
3097 call_site_len = dw2_size_of_call_site_table (section);
3100 /* A pc-relative 4-byte displacement to the @TType data. */
3101 if (have_tt_data)
3103 if (HAVE_AS_LEB128)
3105 char ttype_after_disp_label[32];
3106 ASM_GENERATE_INTERNAL_LABEL (ttype_after_disp_label,
3107 section ? "LLSDATTDC" : "LLSDATTD",
3108 current_function_funcdef_no);
3109 dw2_asm_output_delta_uleb128 (ttype_label, ttype_after_disp_label,
3110 "@TType base offset");
3111 ASM_OUTPUT_LABEL (asm_out_file, ttype_after_disp_label);
3113 else
3115 /* Ug. Alignment queers things. */
3116 unsigned int before_disp, after_disp, last_disp, disp;
3118 before_disp = 1 + 1;
3119 after_disp = (1 + size_of_uleb128 (call_site_len)
3120 + call_site_len
3121 + vec_safe_length (crtl->eh.action_record_data)
3122 + (vec_safe_length (cfun->eh->ttype_data)
3123 * tt_format_size));
3125 disp = after_disp;
3128 unsigned int disp_size, pad;
3130 last_disp = disp;
3131 disp_size = size_of_uleb128 (disp);
3132 pad = before_disp + disp_size + after_disp;
3133 if (pad % tt_format_size)
3134 pad = tt_format_size - (pad % tt_format_size);
3135 else
3136 pad = 0;
3137 disp = after_disp + pad;
3139 while (disp != last_disp);
3141 dw2_asm_output_data_uleb128 (disp, "@TType base offset");
3145 /* Indicate the format of the call-site offsets. */
3146 if (HAVE_AS_LEB128)
3147 cs_format = DW_EH_PE_uleb128;
3148 else
3149 cs_format = DW_EH_PE_udata4;
3151 dw2_asm_output_data (1, cs_format, "call-site format (%s)",
3152 eh_data_format_name (cs_format));
3154 if (HAVE_AS_LEB128)
3156 ASM_GENERATE_INTERNAL_LABEL (cs_after_size_label,
3157 section ? "LLSDACSBC" : "LLSDACSB",
3158 current_function_funcdef_no);
3159 ASM_GENERATE_INTERNAL_LABEL (cs_end_label,
3160 section ? "LLSDACSEC" : "LLSDACSE",
3161 current_function_funcdef_no);
3162 dw2_asm_output_delta_uleb128 (cs_end_label, cs_after_size_label,
3163 "Call-site table length");
3164 ASM_OUTPUT_LABEL (asm_out_file, cs_after_size_label);
3165 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
3166 sjlj_output_call_site_table ();
3167 else
3168 dw2_output_call_site_table (cs_format, section);
3169 ASM_OUTPUT_LABEL (asm_out_file, cs_end_label);
3171 else
3173 dw2_asm_output_data_uleb128 (call_site_len, "Call-site table length");
3174 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
3175 sjlj_output_call_site_table ();
3176 else
3177 dw2_output_call_site_table (cs_format, section);
3180 /* ??? Decode and interpret the data for flag_debug_asm. */
3182 uchar uc;
3183 FOR_EACH_VEC_ELT (*crtl->eh.action_record_data, i, uc)
3184 dw2_asm_output_data (1, uc, i ? NULL : "Action record table");
3187 if (have_tt_data)
3188 assemble_align (tt_format_size * BITS_PER_UNIT);
3190 i = vec_safe_length (cfun->eh->ttype_data);
3191 while (i-- > 0)
3193 tree type = (*cfun->eh->ttype_data)[i];
3194 output_ttype (type, tt_format, tt_format_size);
3197 if (HAVE_AS_LEB128 && have_tt_data)
3198 ASM_OUTPUT_LABEL (asm_out_file, ttype_label);
3200 /* ??? Decode and interpret the data for flag_debug_asm. */
3201 if (targetm.arm_eabi_unwinder)
3203 tree type;
3204 for (i = 0;
3205 vec_safe_iterate (cfun->eh->ehspec_data.arm_eabi, i, &type); ++i)
3206 output_ttype (type, tt_format, tt_format_size);
3208 else
3210 uchar uc;
3211 for (i = 0;
3212 vec_safe_iterate (cfun->eh->ehspec_data.other, i, &uc); ++i)
3213 dw2_asm_output_data (1, uc,
3214 i ? NULL : "Exception specification table");
3218 /* Output an exception table for the current function according to SECTION,
3219 switching back and forth from the function section appropriately.
3221 If the function has been partitioned into hot and cold parts, value 0 for
3222 SECTION refers to the table associated with the hot part while value 1
3223 refers to the table associated with the cold part. If the function has
3224 not been partitioned, value 0 refers to the single exception table. */
3226 void
3227 output_function_exception_table (int section)
3229 /* Not all functions need anything. */
3230 if (!crtl->uses_eh_lsda
3231 || targetm_common.except_unwind_info (&global_options) == UI_NONE)
3232 return;
3234 /* No need to emit any boilerplate stuff for the cold part. */
3235 if (section == 1 && !crtl->eh.call_site_record_v[1])
3236 return;
3238 const char *fnname = get_fnname_from_decl (current_function_decl);
3239 rtx personality = get_personality_function (current_function_decl);
3241 if (personality)
3243 assemble_external_libcall (personality);
3245 if (targetm.asm_out.emit_except_personality)
3246 targetm.asm_out.emit_except_personality (personality);
3249 switch_to_exception_section (fnname);
3251 /* If the target wants a label to begin the table, emit it here. */
3252 targetm.asm_out.emit_except_table_label (asm_out_file);
3254 /* Do the real work. */
3255 output_one_function_exception_table (section);
3257 switch_to_section (current_function_section ());
3260 void
3261 set_eh_throw_stmt_table (function *fun, hash_map<gimple *, int> *table)
3263 fun->eh->throw_stmt_table = table;
3266 hash_map<gimple *, int> *
3267 get_eh_throw_stmt_table (struct function *fun)
3269 return fun->eh->throw_stmt_table;
3272 /* Determine if the function needs an EH personality function. */
3274 enum eh_personality_kind
3275 function_needs_eh_personality (struct function *fn)
3277 enum eh_personality_kind kind = eh_personality_none;
3278 eh_region i;
3280 FOR_ALL_EH_REGION_FN (i, fn)
3282 switch (i->type)
3284 case ERT_CLEANUP:
3285 /* Can do with any personality including the generic C one. */
3286 kind = eh_personality_any;
3287 break;
3289 case ERT_TRY:
3290 case ERT_ALLOWED_EXCEPTIONS:
3291 /* Always needs a EH personality function. The generic C
3292 personality doesn't handle these even for empty type lists. */
3293 return eh_personality_lang;
3295 case ERT_MUST_NOT_THROW:
3296 /* Always needs a EH personality function. The language may specify
3297 what abort routine that must be used, e.g. std::terminate. */
3298 return eh_personality_lang;
3302 return kind;
3305 /* Dump EH information to OUT. */
3307 void
3308 dump_eh_tree (FILE * out, struct function *fun)
3310 eh_region i;
3311 int depth = 0;
3312 static const char *const type_name[] = {
3313 "cleanup", "try", "allowed_exceptions", "must_not_throw"
3316 i = fun->eh->region_tree;
3317 if (!i)
3318 return;
3320 fprintf (out, "Eh tree:\n");
3321 while (1)
3323 fprintf (out, " %*s %i %s", depth * 2, "",
3324 i->index, type_name[(int) i->type]);
3326 if (i->landing_pads)
3328 eh_landing_pad lp;
3330 fprintf (out, " land:");
3331 if (current_ir_type () == IR_GIMPLE)
3333 for (lp = i->landing_pads; lp ; lp = lp->next_lp)
3335 fprintf (out, "{%i,", lp->index);
3336 print_generic_expr (out, lp->post_landing_pad);
3337 fputc ('}', out);
3338 if (lp->next_lp)
3339 fputc (',', out);
3342 else
3344 for (lp = i->landing_pads; lp ; lp = lp->next_lp)
3346 fprintf (out, "{%i,", lp->index);
3347 if (lp->landing_pad)
3348 fprintf (out, "%i%s,", INSN_UID (lp->landing_pad),
3349 NOTE_P (lp->landing_pad) ? "(del)" : "");
3350 else
3351 fprintf (out, "(nil),");
3352 if (lp->post_landing_pad)
3354 rtx_insn *lab = label_rtx (lp->post_landing_pad);
3355 fprintf (out, "%i%s}", INSN_UID (lab),
3356 NOTE_P (lab) ? "(del)" : "");
3358 else
3359 fprintf (out, "(nil)}");
3360 if (lp->next_lp)
3361 fputc (',', out);
3366 switch (i->type)
3368 case ERT_CLEANUP:
3369 case ERT_MUST_NOT_THROW:
3370 break;
3372 case ERT_TRY:
3374 eh_catch c;
3375 fprintf (out, " catch:");
3376 for (c = i->u.eh_try.first_catch; c; c = c->next_catch)
3378 fputc ('{', out);
3379 if (c->label)
3381 fprintf (out, "lab:");
3382 print_generic_expr (out, c->label);
3383 fputc (';', out);
3385 print_generic_expr (out, c->type_list);
3386 fputc ('}', out);
3387 if (c->next_catch)
3388 fputc (',', out);
3391 break;
3393 case ERT_ALLOWED_EXCEPTIONS:
3394 fprintf (out, " filter :%i types:", i->u.allowed.filter);
3395 print_generic_expr (out, i->u.allowed.type_list);
3396 break;
3398 fputc ('\n', out);
3400 /* If there are sub-regions, process them. */
3401 if (i->inner)
3402 i = i->inner, depth++;
3403 /* If there are peers, process them. */
3404 else if (i->next_peer)
3405 i = i->next_peer;
3406 /* Otherwise, step back up the tree to the next peer. */
3407 else
3411 i = i->outer;
3412 depth--;
3413 if (i == NULL)
3414 return;
3416 while (i->next_peer == NULL);
3417 i = i->next_peer;
3422 /* Dump the EH tree for FN on stderr. */
3424 DEBUG_FUNCTION void
3425 debug_eh_tree (struct function *fn)
3427 dump_eh_tree (stderr, fn);
3430 /* Verify invariants on EH datastructures. */
3432 DEBUG_FUNCTION void
3433 verify_eh_tree (struct function *fun)
3435 eh_region r, outer;
3436 int nvisited_lp, nvisited_r;
3437 int count_lp, count_r, depth, i;
3438 eh_landing_pad lp;
3439 bool err = false;
3441 if (!fun->eh->region_tree)
3442 return;
3444 count_r = 0;
3445 for (i = 1; vec_safe_iterate (fun->eh->region_array, i, &r); ++i)
3446 if (r)
3448 if (r->index == i)
3449 count_r++;
3450 else
3452 error ("%<region_array%> is corrupted for region %i", r->index);
3453 err = true;
3457 count_lp = 0;
3458 for (i = 1; vec_safe_iterate (fun->eh->lp_array, i, &lp); ++i)
3459 if (lp)
3461 if (lp->index == i)
3462 count_lp++;
3463 else
3465 error ("%<lp_array%> is corrupted for lp %i", lp->index);
3466 err = true;
3470 depth = nvisited_lp = nvisited_r = 0;
3471 outer = NULL;
3472 r = fun->eh->region_tree;
3473 while (1)
3475 if ((*fun->eh->region_array)[r->index] != r)
3477 error ("%<region_array%> is corrupted for region %i", r->index);
3478 err = true;
3480 if (r->outer != outer)
3482 error ("outer block of region %i is wrong", r->index);
3483 err = true;
3485 if (depth < 0)
3487 error ("negative nesting depth of region %i", r->index);
3488 err = true;
3490 nvisited_r++;
3492 for (lp = r->landing_pads; lp ; lp = lp->next_lp)
3494 if ((*fun->eh->lp_array)[lp->index] != lp)
3496 error ("%<lp_array%> is corrupted for lp %i", lp->index);
3497 err = true;
3499 if (lp->region != r)
3501 error ("region of lp %i is wrong", lp->index);
3502 err = true;
3504 nvisited_lp++;
3507 if (r->inner)
3508 outer = r, r = r->inner, depth++;
3509 else if (r->next_peer)
3510 r = r->next_peer;
3511 else
3515 r = r->outer;
3516 if (r == NULL)
3517 goto region_done;
3518 depth--;
3519 outer = r->outer;
3521 while (r->next_peer == NULL);
3522 r = r->next_peer;
3525 region_done:
3526 if (depth != 0)
3528 error ("tree list ends on depth %i", depth);
3529 err = true;
3531 if (count_r != nvisited_r)
3533 error ("%<region_array%> does not match %<region_tree%>");
3534 err = true;
3536 if (count_lp != nvisited_lp)
3538 error ("%<lp_array%> does not match %<region_tree%>");
3539 err = true;
3542 if (err)
3544 dump_eh_tree (stderr, fun);
3545 internal_error ("%qs failed", __func__);
3549 #include "gt-except.h"