PR target/82524
[official-gcc.git] / gcc / except.c
blob041f89a55e5bbce5b4c12f63d4a9db0962ff5b0a
1 /* Implements exception handling.
2 Copyright (C) 1989-2017 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, WITH_CLEANUP_EXPR,
31 CLEANUP_POINT_EXPR, CATCH_EXPR, and EH_FILTER_EXPR.
33 During initial gimplification (gimplify.c) these are lowered
34 to the GIMPLE_TRY, GIMPLE_CATCH, and GIMPLE_EH_FILTER nodes.
35 The WITH_CLEANUP_EXPR and CLEANUP_POINT_EXPR nodes are converted
36 into GIMPLE_TRY_FINALLY nodes; the others are a more direct 1-1
37 conversion.
39 During pass_lower_eh (tree-eh.c) 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.c), 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.c), 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.c), 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.c), 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.c) 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.c) 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 "dwarf2out.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"
148 static GTY(()) int call_site_base;
150 static GTY(()) hash_map<tree_hash, tree> *type_to_runtime_map;
152 static GTY(()) tree setjmp_fn;
154 /* Describe the SjLj_Function_Context structure. */
155 static GTY(()) tree sjlj_fc_type_node;
156 static int sjlj_fc_call_site_ofs;
157 static int sjlj_fc_data_ofs;
158 static int sjlj_fc_personality_ofs;
159 static int sjlj_fc_lsda_ofs;
160 static int sjlj_fc_jbuf_ofs;
163 struct GTY(()) call_site_record_d
165 rtx landing_pad;
166 int action;
169 /* In the following structure and associated functions,
170 we represent entries in the action table as 1-based indices.
171 Special cases are:
173 0: null action record, non-null landing pad; implies cleanups
174 -1: null action record, null landing pad; implies no action
175 -2: no call-site entry; implies must_not_throw
176 -3: we have yet to process outer regions
178 Further, no special cases apply to the "next" field of the record.
179 For next, 0 means end of list. */
181 struct action_record
183 int offset;
184 int filter;
185 int next;
188 /* Hashtable helpers. */
190 struct action_record_hasher : free_ptr_hash <action_record>
192 static inline hashval_t hash (const action_record *);
193 static inline bool equal (const action_record *, const action_record *);
196 inline hashval_t
197 action_record_hasher::hash (const action_record *entry)
199 return entry->next * 1009 + entry->filter;
202 inline bool
203 action_record_hasher::equal (const action_record *entry,
204 const action_record *data)
206 return entry->filter == data->filter && entry->next == data->next;
209 typedef hash_table<action_record_hasher> action_hash_type;
211 static bool get_eh_region_and_lp_from_rtx (const_rtx, eh_region *,
212 eh_landing_pad *);
214 static void dw2_build_landing_pads (void);
216 static int collect_one_action_chain (action_hash_type *, eh_region);
217 static int add_call_site (rtx, int, int);
219 static void push_uleb128 (vec<uchar, va_gc> **, unsigned int);
220 static void push_sleb128 (vec<uchar, va_gc> **, int);
221 static int dw2_size_of_call_site_table (int);
222 static int sjlj_size_of_call_site_table (void);
223 static void dw2_output_call_site_table (int, int);
224 static void sjlj_output_call_site_table (void);
227 void
228 init_eh (void)
230 if (! flag_exceptions)
231 return;
233 type_to_runtime_map = hash_map<tree_hash, tree>::create_ggc (31);
235 /* Create the SjLj_Function_Context structure. This should match
236 the definition in unwind-sjlj.c. */
237 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
239 tree f_jbuf, f_per, f_lsda, f_prev, f_cs, f_data, tmp;
241 sjlj_fc_type_node = lang_hooks.types.make_type (RECORD_TYPE);
243 f_prev = build_decl (BUILTINS_LOCATION,
244 FIELD_DECL, get_identifier ("__prev"),
245 build_pointer_type (sjlj_fc_type_node));
246 DECL_FIELD_CONTEXT (f_prev) = sjlj_fc_type_node;
248 f_cs = build_decl (BUILTINS_LOCATION,
249 FIELD_DECL, get_identifier ("__call_site"),
250 integer_type_node);
251 DECL_FIELD_CONTEXT (f_cs) = sjlj_fc_type_node;
253 tmp = build_index_type (size_int (4 - 1));
254 tmp = build_array_type (lang_hooks.types.type_for_mode
255 (targetm.unwind_word_mode (), 1),
256 tmp);
257 f_data = build_decl (BUILTINS_LOCATION,
258 FIELD_DECL, get_identifier ("__data"), tmp);
259 DECL_FIELD_CONTEXT (f_data) = sjlj_fc_type_node;
261 f_per = build_decl (BUILTINS_LOCATION,
262 FIELD_DECL, get_identifier ("__personality"),
263 ptr_type_node);
264 DECL_FIELD_CONTEXT (f_per) = sjlj_fc_type_node;
266 f_lsda = build_decl (BUILTINS_LOCATION,
267 FIELD_DECL, get_identifier ("__lsda"),
268 ptr_type_node);
269 DECL_FIELD_CONTEXT (f_lsda) = sjlj_fc_type_node;
271 #ifdef DONT_USE_BUILTIN_SETJMP
272 #ifdef JMP_BUF_SIZE
273 tmp = size_int (JMP_BUF_SIZE - 1);
274 #else
275 /* Should be large enough for most systems, if it is not,
276 JMP_BUF_SIZE should be defined with the proper value. It will
277 also tend to be larger than necessary for most systems, a more
278 optimal port will define JMP_BUF_SIZE. */
279 tmp = size_int (FIRST_PSEUDO_REGISTER + 2 - 1);
280 #endif
281 #else
282 /* Compute a minimally sized jump buffer. We need room to store at
283 least 3 pointers - stack pointer, frame pointer and return address.
284 Plus for some targets we need room for an extra pointer - in the
285 case of MIPS this is the global pointer. This makes a total of four
286 pointers, but to be safe we actually allocate room for 5.
288 If pointers are smaller than words then we allocate enough room for
289 5 words, just in case the backend needs this much room. For more
290 discussion on this issue see:
291 http://gcc.gnu.org/ml/gcc-patches/2014-05/msg00313.html. */
292 if (POINTER_SIZE > BITS_PER_WORD)
293 tmp = size_int (5 - 1);
294 else
295 tmp = size_int ((5 * BITS_PER_WORD / POINTER_SIZE) - 1);
296 #endif
298 tmp = build_index_type (tmp);
299 tmp = build_array_type (ptr_type_node, tmp);
300 f_jbuf = build_decl (BUILTINS_LOCATION,
301 FIELD_DECL, get_identifier ("__jbuf"), tmp);
302 #ifdef DONT_USE_BUILTIN_SETJMP
303 /* We don't know what the alignment requirements of the
304 runtime's jmp_buf has. Overestimate. */
305 SET_DECL_ALIGN (f_jbuf, BIGGEST_ALIGNMENT);
306 DECL_USER_ALIGN (f_jbuf) = 1;
307 #endif
308 DECL_FIELD_CONTEXT (f_jbuf) = sjlj_fc_type_node;
310 TYPE_FIELDS (sjlj_fc_type_node) = f_prev;
311 TREE_CHAIN (f_prev) = f_cs;
312 TREE_CHAIN (f_cs) = f_data;
313 TREE_CHAIN (f_data) = f_per;
314 TREE_CHAIN (f_per) = f_lsda;
315 TREE_CHAIN (f_lsda) = f_jbuf;
317 layout_type (sjlj_fc_type_node);
319 /* Cache the interesting field offsets so that we have
320 easy access from rtl. */
321 sjlj_fc_call_site_ofs
322 = (tree_to_uhwi (DECL_FIELD_OFFSET (f_cs))
323 + tree_to_uhwi (DECL_FIELD_BIT_OFFSET (f_cs)) / BITS_PER_UNIT);
324 sjlj_fc_data_ofs
325 = (tree_to_uhwi (DECL_FIELD_OFFSET (f_data))
326 + tree_to_uhwi (DECL_FIELD_BIT_OFFSET (f_data)) / BITS_PER_UNIT);
327 sjlj_fc_personality_ofs
328 = (tree_to_uhwi (DECL_FIELD_OFFSET (f_per))
329 + tree_to_uhwi (DECL_FIELD_BIT_OFFSET (f_per)) / BITS_PER_UNIT);
330 sjlj_fc_lsda_ofs
331 = (tree_to_uhwi (DECL_FIELD_OFFSET (f_lsda))
332 + tree_to_uhwi (DECL_FIELD_BIT_OFFSET (f_lsda)) / BITS_PER_UNIT);
333 sjlj_fc_jbuf_ofs
334 = (tree_to_uhwi (DECL_FIELD_OFFSET (f_jbuf))
335 + tree_to_uhwi (DECL_FIELD_BIT_OFFSET (f_jbuf)) / BITS_PER_UNIT);
337 #ifdef DONT_USE_BUILTIN_SETJMP
338 tmp = build_function_type_list (integer_type_node, TREE_TYPE (f_jbuf),
339 NULL);
340 setjmp_fn = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
341 get_identifier ("setjmp"), tmp);
342 TREE_PUBLIC (setjmp_fn) = 1;
343 DECL_EXTERNAL (setjmp_fn) = 1;
344 DECL_ASSEMBLER_NAME (setjmp_fn);
345 #endif
349 void
350 init_eh_for_function (void)
352 cfun->eh = ggc_cleared_alloc<eh_status> ();
354 /* Make sure zero'th entries are used. */
355 vec_safe_push (cfun->eh->region_array, (eh_region)0);
356 vec_safe_push (cfun->eh->lp_array, (eh_landing_pad)0);
359 /* Routines to generate the exception tree somewhat directly.
360 These are used from tree-eh.c when processing exception related
361 nodes during tree optimization. */
363 static eh_region
364 gen_eh_region (enum eh_region_type type, eh_region outer)
366 eh_region new_eh;
368 /* Insert a new blank region as a leaf in the tree. */
369 new_eh = ggc_cleared_alloc<eh_region_d> ();
370 new_eh->type = type;
371 new_eh->outer = outer;
372 if (outer)
374 new_eh->next_peer = outer->inner;
375 outer->inner = new_eh;
377 else
379 new_eh->next_peer = cfun->eh->region_tree;
380 cfun->eh->region_tree = new_eh;
383 new_eh->index = vec_safe_length (cfun->eh->region_array);
384 vec_safe_push (cfun->eh->region_array, new_eh);
386 /* Copy the language's notion of whether to use __cxa_end_cleanup. */
387 if (targetm.arm_eabi_unwinder && lang_hooks.eh_use_cxa_end_cleanup)
388 new_eh->use_cxa_end_cleanup = true;
390 return new_eh;
393 eh_region
394 gen_eh_region_cleanup (eh_region outer)
396 return gen_eh_region (ERT_CLEANUP, outer);
399 eh_region
400 gen_eh_region_try (eh_region outer)
402 return gen_eh_region (ERT_TRY, outer);
405 eh_catch
406 gen_eh_region_catch (eh_region t, tree type_or_list)
408 eh_catch c, l;
409 tree type_list, type_node;
411 gcc_assert (t->type == ERT_TRY);
413 /* Ensure to always end up with a type list to normalize further
414 processing, then register each type against the runtime types map. */
415 type_list = type_or_list;
416 if (type_or_list)
418 if (TREE_CODE (type_or_list) != TREE_LIST)
419 type_list = tree_cons (NULL_TREE, type_or_list, NULL_TREE);
421 type_node = type_list;
422 for (; type_node; type_node = TREE_CHAIN (type_node))
423 add_type_for_runtime (TREE_VALUE (type_node));
426 c = ggc_cleared_alloc<eh_catch_d> ();
427 c->type_list = type_list;
428 l = t->u.eh_try.last_catch;
429 c->prev_catch = l;
430 if (l)
431 l->next_catch = c;
432 else
433 t->u.eh_try.first_catch = c;
434 t->u.eh_try.last_catch = c;
436 return c;
439 eh_region
440 gen_eh_region_allowed (eh_region outer, tree allowed)
442 eh_region region = gen_eh_region (ERT_ALLOWED_EXCEPTIONS, outer);
443 region->u.allowed.type_list = allowed;
445 for (; allowed ; allowed = TREE_CHAIN (allowed))
446 add_type_for_runtime (TREE_VALUE (allowed));
448 return region;
451 eh_region
452 gen_eh_region_must_not_throw (eh_region outer)
454 return gen_eh_region (ERT_MUST_NOT_THROW, outer);
457 eh_landing_pad
458 gen_eh_landing_pad (eh_region region)
460 eh_landing_pad lp = ggc_cleared_alloc<eh_landing_pad_d> ();
462 lp->next_lp = region->landing_pads;
463 lp->region = region;
464 lp->index = vec_safe_length (cfun->eh->lp_array);
465 region->landing_pads = lp;
467 vec_safe_push (cfun->eh->lp_array, lp);
469 return lp;
472 eh_region
473 get_eh_region_from_number_fn (struct function *ifun, int i)
475 return (*ifun->eh->region_array)[i];
478 eh_region
479 get_eh_region_from_number (int i)
481 return get_eh_region_from_number_fn (cfun, i);
484 eh_landing_pad
485 get_eh_landing_pad_from_number_fn (struct function *ifun, int i)
487 return (*ifun->eh->lp_array)[i];
490 eh_landing_pad
491 get_eh_landing_pad_from_number (int i)
493 return get_eh_landing_pad_from_number_fn (cfun, i);
496 eh_region
497 get_eh_region_from_lp_number_fn (struct function *ifun, int i)
499 if (i < 0)
500 return (*ifun->eh->region_array)[-i];
501 else if (i == 0)
502 return NULL;
503 else
505 eh_landing_pad lp;
506 lp = (*ifun->eh->lp_array)[i];
507 return lp->region;
511 eh_region
512 get_eh_region_from_lp_number (int i)
514 return get_eh_region_from_lp_number_fn (cfun, i);
517 /* Returns true if the current function has exception handling regions. */
519 bool
520 current_function_has_exception_handlers (void)
522 return cfun->eh->region_tree != NULL;
525 /* A subroutine of duplicate_eh_regions. Copy the eh_region tree at OLD.
526 Root it at OUTER, and apply LP_OFFSET to the lp numbers. */
528 struct duplicate_eh_regions_data
530 duplicate_eh_regions_map label_map;
531 void *label_map_data;
532 hash_map<void *, void *> *eh_map;
535 static void
536 duplicate_eh_regions_1 (struct duplicate_eh_regions_data *data,
537 eh_region old_r, eh_region outer)
539 eh_landing_pad old_lp, new_lp;
540 eh_region new_r;
542 new_r = gen_eh_region (old_r->type, outer);
543 gcc_assert (!data->eh_map->put (old_r, new_r));
545 switch (old_r->type)
547 case ERT_CLEANUP:
548 break;
550 case ERT_TRY:
552 eh_catch oc, nc;
553 for (oc = old_r->u.eh_try.first_catch; oc ; oc = oc->next_catch)
555 /* We should be doing all our region duplication before and
556 during inlining, which is before filter lists are created. */
557 gcc_assert (oc->filter_list == NULL);
558 nc = gen_eh_region_catch (new_r, oc->type_list);
559 nc->label = data->label_map (oc->label, data->label_map_data);
562 break;
564 case ERT_ALLOWED_EXCEPTIONS:
565 new_r->u.allowed.type_list = old_r->u.allowed.type_list;
566 if (old_r->u.allowed.label)
567 new_r->u.allowed.label
568 = data->label_map (old_r->u.allowed.label, data->label_map_data);
569 else
570 new_r->u.allowed.label = NULL_TREE;
571 break;
573 case ERT_MUST_NOT_THROW:
574 new_r->u.must_not_throw.failure_loc =
575 LOCATION_LOCUS (old_r->u.must_not_throw.failure_loc);
576 new_r->u.must_not_throw.failure_decl =
577 old_r->u.must_not_throw.failure_decl;
578 break;
581 for (old_lp = old_r->landing_pads; old_lp ; old_lp = old_lp->next_lp)
583 /* Don't bother copying unused landing pads. */
584 if (old_lp->post_landing_pad == NULL)
585 continue;
587 new_lp = gen_eh_landing_pad (new_r);
588 gcc_assert (!data->eh_map->put (old_lp, new_lp));
590 new_lp->post_landing_pad
591 = data->label_map (old_lp->post_landing_pad, data->label_map_data);
592 EH_LANDING_PAD_NR (new_lp->post_landing_pad) = new_lp->index;
595 /* Make sure to preserve the original use of __cxa_end_cleanup. */
596 new_r->use_cxa_end_cleanup = old_r->use_cxa_end_cleanup;
598 for (old_r = old_r->inner; old_r ; old_r = old_r->next_peer)
599 duplicate_eh_regions_1 (data, old_r, new_r);
602 /* Duplicate the EH regions from IFUN rooted at COPY_REGION into
603 the current function and root the tree below OUTER_REGION.
604 The special case of COPY_REGION of NULL means all regions.
605 Remap labels using MAP/MAP_DATA callback. Return a pointer map
606 that allows the caller to remap uses of both EH regions and
607 EH landing pads. */
609 hash_map<void *, void *> *
610 duplicate_eh_regions (struct function *ifun,
611 eh_region copy_region, int outer_lp,
612 duplicate_eh_regions_map map, void *map_data)
614 struct duplicate_eh_regions_data data;
615 eh_region outer_region;
617 if (flag_checking)
618 verify_eh_tree (ifun);
620 data.label_map = map;
621 data.label_map_data = map_data;
622 data.eh_map = new hash_map<void *, void *>;
624 outer_region = get_eh_region_from_lp_number_fn (cfun, outer_lp);
626 /* Copy all the regions in the subtree. */
627 if (copy_region)
628 duplicate_eh_regions_1 (&data, copy_region, outer_region);
629 else
631 eh_region r;
632 for (r = ifun->eh->region_tree; r ; r = r->next_peer)
633 duplicate_eh_regions_1 (&data, r, outer_region);
636 if (flag_checking)
637 verify_eh_tree (cfun);
639 return data.eh_map;
642 /* Return the region that is outer to both REGION_A and REGION_B in IFUN. */
644 eh_region
645 eh_region_outermost (struct function *ifun, eh_region region_a,
646 eh_region region_b)
648 gcc_assert (ifun->eh->region_array);
649 gcc_assert (ifun->eh->region_tree);
651 auto_sbitmap b_outer (ifun->eh->region_array->length ());
652 bitmap_clear (b_outer);
656 bitmap_set_bit (b_outer, region_b->index);
657 region_b = region_b->outer;
659 while (region_b);
663 if (bitmap_bit_p (b_outer, region_a->index))
664 break;
665 region_a = region_a->outer;
667 while (region_a);
669 return region_a;
672 void
673 add_type_for_runtime (tree type)
675 /* If TYPE is NOP_EXPR, it means that it already is a runtime type. */
676 if (TREE_CODE (type) == NOP_EXPR)
677 return;
679 bool existed = false;
680 tree *slot = &type_to_runtime_map->get_or_insert (type, &existed);
681 if (!existed)
682 *slot = lang_hooks.eh_runtime_type (type);
685 tree
686 lookup_type_for_runtime (tree type)
688 /* If TYPE is NOP_EXPR, it means that it already is a runtime type. */
689 if (TREE_CODE (type) == NOP_EXPR)
690 return type;
692 /* We should have always inserted the data earlier. */
693 return *type_to_runtime_map->get (type);
697 /* Represent an entry in @TTypes for either catch actions
698 or exception filter actions. */
699 struct ttypes_filter {
700 tree t;
701 int filter;
704 /* Helper for ttypes_filter hashing. */
706 struct ttypes_filter_hasher : free_ptr_hash <ttypes_filter>
708 typedef tree_node *compare_type;
709 static inline hashval_t hash (const ttypes_filter *);
710 static inline bool equal (const ttypes_filter *, const tree_node *);
713 /* Compare ENTRY (a ttypes_filter entry in the hash table) with DATA
714 (a tree) for a @TTypes type node we are thinking about adding. */
716 inline bool
717 ttypes_filter_hasher::equal (const ttypes_filter *entry, const tree_node *data)
719 return entry->t == data;
722 inline hashval_t
723 ttypes_filter_hasher::hash (const ttypes_filter *entry)
725 return TREE_HASH (entry->t);
728 typedef hash_table<ttypes_filter_hasher> ttypes_hash_type;
731 /* Helper for ehspec hashing. */
733 struct ehspec_hasher : free_ptr_hash <ttypes_filter>
735 static inline hashval_t hash (const ttypes_filter *);
736 static inline bool equal (const ttypes_filter *, const ttypes_filter *);
739 /* Compare ENTRY with DATA (both struct ttypes_filter) for a @TTypes
740 exception specification list we are thinking about adding. */
741 /* ??? Currently we use the type lists in the order given. Someone
742 should put these in some canonical order. */
744 inline bool
745 ehspec_hasher::equal (const ttypes_filter *entry, const ttypes_filter *data)
747 return type_list_equal (entry->t, data->t);
750 /* Hash function for exception specification lists. */
752 inline hashval_t
753 ehspec_hasher::hash (const ttypes_filter *entry)
755 hashval_t h = 0;
756 tree list;
758 for (list = entry->t; list ; list = TREE_CHAIN (list))
759 h = (h << 5) + (h >> 27) + TREE_HASH (TREE_VALUE (list));
760 return h;
763 typedef hash_table<ehspec_hasher> ehspec_hash_type;
766 /* Add TYPE (which may be NULL) to cfun->eh->ttype_data, using TYPES_HASH
767 to speed up the search. Return the filter value to be used. */
769 static int
770 add_ttypes_entry (ttypes_hash_type *ttypes_hash, tree type)
772 struct ttypes_filter **slot, *n;
774 slot = ttypes_hash->find_slot_with_hash (type, (hashval_t) TREE_HASH (type),
775 INSERT);
777 if ((n = *slot) == NULL)
779 /* Filter value is a 1 based table index. */
781 n = XNEW (struct ttypes_filter);
782 n->t = type;
783 n->filter = vec_safe_length (cfun->eh->ttype_data) + 1;
784 *slot = n;
786 vec_safe_push (cfun->eh->ttype_data, type);
789 return n->filter;
792 /* Add LIST to cfun->eh->ehspec_data, using EHSPEC_HASH and TYPES_HASH
793 to speed up the search. Return the filter value to be used. */
795 static int
796 add_ehspec_entry (ehspec_hash_type *ehspec_hash, ttypes_hash_type *ttypes_hash,
797 tree list)
799 struct ttypes_filter **slot, *n;
800 struct ttypes_filter dummy;
802 dummy.t = list;
803 slot = ehspec_hash->find_slot (&dummy, INSERT);
805 if ((n = *slot) == NULL)
807 int len;
809 if (targetm.arm_eabi_unwinder)
810 len = vec_safe_length (cfun->eh->ehspec_data.arm_eabi);
811 else
812 len = vec_safe_length (cfun->eh->ehspec_data.other);
814 /* Filter value is a -1 based byte index into a uleb128 buffer. */
816 n = XNEW (struct ttypes_filter);
817 n->t = list;
818 n->filter = -(len + 1);
819 *slot = n;
821 /* Generate a 0 terminated list of filter values. */
822 for (; list ; list = TREE_CHAIN (list))
824 if (targetm.arm_eabi_unwinder)
825 vec_safe_push (cfun->eh->ehspec_data.arm_eabi, TREE_VALUE (list));
826 else
828 /* Look up each type in the list and encode its filter
829 value as a uleb128. */
830 push_uleb128 (&cfun->eh->ehspec_data.other,
831 add_ttypes_entry (ttypes_hash, TREE_VALUE (list)));
834 if (targetm.arm_eabi_unwinder)
835 vec_safe_push (cfun->eh->ehspec_data.arm_eabi, NULL_TREE);
836 else
837 vec_safe_push (cfun->eh->ehspec_data.other, (uchar)0);
840 return n->filter;
843 /* Generate the action filter values to be used for CATCH and
844 ALLOWED_EXCEPTIONS regions. When using dwarf2 exception regions,
845 we use lots of landing pads, and so every type or list can share
846 the same filter value, which saves table space. */
848 void
849 assign_filter_values (void)
851 int i;
852 eh_region r;
853 eh_catch c;
855 vec_alloc (cfun->eh->ttype_data, 16);
856 if (targetm.arm_eabi_unwinder)
857 vec_alloc (cfun->eh->ehspec_data.arm_eabi, 64);
858 else
859 vec_alloc (cfun->eh->ehspec_data.other, 64);
861 ehspec_hash_type ehspec (31);
862 ttypes_hash_type ttypes (31);
864 for (i = 1; vec_safe_iterate (cfun->eh->region_array, i, &r); ++i)
866 if (r == NULL)
867 continue;
869 switch (r->type)
871 case ERT_TRY:
872 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
874 /* Whatever type_list is (NULL or true list), we build a list
875 of filters for the region. */
876 c->filter_list = NULL_TREE;
878 if (c->type_list != NULL)
880 /* Get a filter value for each of the types caught and store
881 them in the region's dedicated list. */
882 tree tp_node = c->type_list;
884 for ( ; tp_node; tp_node = TREE_CHAIN (tp_node))
886 int flt
887 = add_ttypes_entry (&ttypes, TREE_VALUE (tp_node));
888 tree flt_node = build_int_cst (integer_type_node, flt);
890 c->filter_list
891 = tree_cons (NULL_TREE, flt_node, c->filter_list);
894 else
896 /* Get a filter value for the NULL list also since it
897 will need an action record anyway. */
898 int flt = add_ttypes_entry (&ttypes, NULL);
899 tree flt_node = build_int_cst (integer_type_node, flt);
901 c->filter_list
902 = tree_cons (NULL_TREE, flt_node, NULL);
905 break;
907 case ERT_ALLOWED_EXCEPTIONS:
908 r->u.allowed.filter
909 = add_ehspec_entry (&ehspec, &ttypes, r->u.allowed.type_list);
910 break;
912 default:
913 break;
918 /* Emit SEQ into basic block just before INSN (that is assumed to be
919 first instruction of some existing BB and return the newly
920 produced block. */
921 static basic_block
922 emit_to_new_bb_before (rtx_insn *seq, rtx_insn *insn)
924 rtx_insn *last;
925 basic_block bb;
926 edge e;
927 edge_iterator ei;
929 /* If there happens to be a fallthru edge (possibly created by cleanup_cfg
930 call), we don't want it to go into newly created landing pad or other EH
931 construct. */
932 for (ei = ei_start (BLOCK_FOR_INSN (insn)->preds); (e = ei_safe_edge (ei)); )
933 if (e->flags & EDGE_FALLTHRU)
934 force_nonfallthru (e);
935 else
936 ei_next (&ei);
937 last = emit_insn_before (seq, insn);
938 if (BARRIER_P (last))
939 last = PREV_INSN (last);
940 bb = create_basic_block (seq, last, BLOCK_FOR_INSN (insn)->prev_bb);
941 update_bb_for_insn (bb);
942 bb->flags |= BB_SUPERBLOCK;
943 return bb;
946 /* A subroutine of dw2_build_landing_pads, also used for edge splitting
947 at the rtl level. Emit the code required by the target at a landing
948 pad for the given region. */
950 void
951 expand_dw2_landing_pad_for_region (eh_region region)
953 if (targetm.have_exception_receiver ())
954 emit_insn (targetm.gen_exception_receiver ());
955 else if (targetm.have_nonlocal_goto_receiver ())
956 emit_insn (targetm.gen_nonlocal_goto_receiver ());
957 else
958 { /* Nothing */ }
960 if (region->exc_ptr_reg)
961 emit_move_insn (region->exc_ptr_reg,
962 gen_rtx_REG (ptr_mode, EH_RETURN_DATA_REGNO (0)));
963 if (region->filter_reg)
964 emit_move_insn (region->filter_reg,
965 gen_rtx_REG (targetm.eh_return_filter_mode (),
966 EH_RETURN_DATA_REGNO (1)));
969 /* Expand the extra code needed at landing pads for dwarf2 unwinding. */
971 static void
972 dw2_build_landing_pads (void)
974 int i;
975 eh_landing_pad lp;
976 int e_flags = EDGE_FALLTHRU;
978 /* If we're going to partition blocks, we need to be able to add
979 new landing pads later, which means that we need to hold on to
980 the post-landing-pad block. Prevent it from being merged away.
981 We'll remove this bit after partitioning. */
982 if (flag_reorder_blocks_and_partition)
983 e_flags |= EDGE_PRESERVE;
985 for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
987 basic_block bb;
988 rtx_insn *seq;
990 if (lp == NULL || lp->post_landing_pad == NULL)
991 continue;
993 start_sequence ();
995 lp->landing_pad = gen_label_rtx ();
996 emit_label (lp->landing_pad);
997 LABEL_PRESERVE_P (lp->landing_pad) = 1;
999 expand_dw2_landing_pad_for_region (lp->region);
1001 seq = get_insns ();
1002 end_sequence ();
1004 bb = emit_to_new_bb_before (seq, label_rtx (lp->post_landing_pad));
1005 bb->count = bb->next_bb->count;
1006 bb->frequency = bb->next_bb->frequency;
1007 make_single_succ_edge (bb, bb->next_bb, e_flags);
1008 if (current_loops)
1010 struct loop *loop = bb->next_bb->loop_father;
1011 /* If we created a pre-header block, add the new block to the
1012 outer loop, otherwise to the loop itself. */
1013 if (bb->next_bb == loop->header)
1014 add_bb_to_loop (bb, loop_outer (loop));
1015 else
1016 add_bb_to_loop (bb, loop);
1022 static vec<int> sjlj_lp_call_site_index;
1024 /* Process all active landing pads. Assign each one a compact dispatch
1025 index, and a call-site index. */
1027 static int
1028 sjlj_assign_call_site_values (void)
1030 action_hash_type ar_hash (31);
1031 int i, disp_index;
1032 eh_landing_pad lp;
1034 vec_alloc (crtl->eh.action_record_data, 64);
1036 disp_index = 0;
1037 call_site_base = 1;
1038 for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
1039 if (lp && lp->post_landing_pad)
1041 int action, call_site;
1043 /* First: build the action table. */
1044 action = collect_one_action_chain (&ar_hash, lp->region);
1046 /* Next: assign call-site values. If dwarf2 terms, this would be
1047 the region number assigned by convert_to_eh_region_ranges, but
1048 handles no-action and must-not-throw differently. */
1049 /* Map must-not-throw to otherwise unused call-site index 0. */
1050 if (action == -2)
1051 call_site = 0;
1052 /* Map no-action to otherwise unused call-site index -1. */
1053 else if (action == -1)
1054 call_site = -1;
1055 /* Otherwise, look it up in the table. */
1056 else
1057 call_site = add_call_site (GEN_INT (disp_index), action, 0);
1058 sjlj_lp_call_site_index[i] = call_site;
1060 disp_index++;
1063 return disp_index;
1066 /* Emit code to record the current call-site index before every
1067 insn that can throw. */
1069 static void
1070 sjlj_mark_call_sites (void)
1072 int last_call_site = -2;
1073 rtx_insn *insn;
1074 rtx mem;
1076 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
1078 eh_landing_pad lp;
1079 eh_region r;
1080 bool nothrow;
1081 int this_call_site;
1082 rtx_insn *before, *p;
1084 /* Reset value tracking at extended basic block boundaries. */
1085 if (LABEL_P (insn))
1086 last_call_site = -2;
1088 /* If the function allocates dynamic stack space, the context must
1089 be updated after every allocation/deallocation accordingly. */
1090 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_UPDATE_SJLJ_CONTEXT)
1092 rtx buf_addr;
1094 start_sequence ();
1095 buf_addr = plus_constant (Pmode, XEXP (crtl->eh.sjlj_fc, 0),
1096 sjlj_fc_jbuf_ofs);
1097 expand_builtin_update_setjmp_buf (buf_addr);
1098 p = get_insns ();
1099 end_sequence ();
1100 emit_insn_before (p, insn);
1103 if (! INSN_P (insn))
1104 continue;
1106 nothrow = get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1107 if (nothrow)
1108 continue;
1109 if (lp)
1110 this_call_site = sjlj_lp_call_site_index[lp->index];
1111 else if (r == NULL)
1113 /* Calls (and trapping insns) without notes are outside any
1114 exception handling region in this function. Mark them as
1115 no action. */
1116 this_call_site = -1;
1118 else
1120 gcc_assert (r->type == ERT_MUST_NOT_THROW);
1121 this_call_site = 0;
1124 if (this_call_site != -1)
1125 crtl->uses_eh_lsda = 1;
1127 if (this_call_site == last_call_site)
1128 continue;
1130 /* Don't separate a call from it's argument loads. */
1131 before = insn;
1132 if (CALL_P (insn))
1133 before = find_first_parameter_load (insn, NULL);
1135 start_sequence ();
1136 mem = adjust_address (crtl->eh.sjlj_fc, TYPE_MODE (integer_type_node),
1137 sjlj_fc_call_site_ofs);
1138 emit_move_insn (mem, gen_int_mode (this_call_site, GET_MODE (mem)));
1139 p = get_insns ();
1140 end_sequence ();
1142 emit_insn_before (p, before);
1143 last_call_site = this_call_site;
1147 /* Construct the SjLj_Function_Context. */
1149 static void
1150 sjlj_emit_function_enter (rtx_code_label *dispatch_label)
1152 rtx_insn *fn_begin, *seq;
1153 rtx fc, mem;
1154 bool fn_begin_outside_block;
1155 rtx personality = get_personality_function (current_function_decl);
1157 fc = crtl->eh.sjlj_fc;
1159 start_sequence ();
1161 /* We're storing this libcall's address into memory instead of
1162 calling it directly. Thus, we must call assemble_external_libcall
1163 here, as we can not depend on emit_library_call to do it for us. */
1164 assemble_external_libcall (personality);
1165 mem = adjust_address (fc, Pmode, sjlj_fc_personality_ofs);
1166 emit_move_insn (mem, personality);
1168 mem = adjust_address (fc, Pmode, sjlj_fc_lsda_ofs);
1169 if (crtl->uses_eh_lsda)
1171 char buf[20];
1172 rtx sym;
1174 ASM_GENERATE_INTERNAL_LABEL (buf, "LLSDA", current_function_funcdef_no);
1175 sym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
1176 SYMBOL_REF_FLAGS (sym) = SYMBOL_FLAG_LOCAL;
1177 emit_move_insn (mem, sym);
1179 else
1180 emit_move_insn (mem, const0_rtx);
1182 if (dispatch_label)
1184 rtx addr = plus_constant (Pmode, XEXP (fc, 0), sjlj_fc_jbuf_ofs);
1186 #ifdef DONT_USE_BUILTIN_SETJMP
1187 addr = copy_addr_to_reg (addr);
1188 addr = convert_memory_address (ptr_mode, addr);
1189 tree addr_tree = make_tree (ptr_type_node, addr);
1191 tree call_expr = build_call_expr (setjmp_fn, 1, addr_tree);
1192 rtx x = expand_call (call_expr, NULL_RTX, false);
1194 emit_cmp_and_jump_insns (x, const0_rtx, NE, 0,
1195 TYPE_MODE (integer_type_node), 0,
1196 dispatch_label,
1197 profile_probability::unlikely ());
1198 #else
1199 expand_builtin_setjmp_setup (addr, dispatch_label);
1200 #endif
1203 emit_library_call (unwind_sjlj_register_libfunc, LCT_NORMAL, VOIDmode,
1204 XEXP (fc, 0), Pmode);
1206 seq = get_insns ();
1207 end_sequence ();
1209 /* ??? Instead of doing this at the beginning of the function,
1210 do this in a block that is at loop level 0 and dominates all
1211 can_throw_internal instructions. */
1213 fn_begin_outside_block = true;
1214 for (fn_begin = get_insns (); ; fn_begin = NEXT_INSN (fn_begin))
1215 if (NOTE_P (fn_begin))
1217 if (NOTE_KIND (fn_begin) == NOTE_INSN_FUNCTION_BEG)
1218 break;
1219 else if (NOTE_INSN_BASIC_BLOCK_P (fn_begin))
1220 fn_begin_outside_block = false;
1223 #ifdef DONT_USE_BUILTIN_SETJMP
1224 if (dispatch_label)
1226 /* The sequence contains a branch in the middle so we need to force
1227 the creation of a new basic block by means of BB_SUPERBLOCK. */
1228 if (fn_begin_outside_block)
1230 basic_block bb
1231 = split_edge (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1232 if (JUMP_P (BB_END (bb)))
1233 emit_insn_before (seq, BB_END (bb));
1234 else
1235 emit_insn_after (seq, BB_END (bb));
1237 else
1238 emit_insn_after (seq, fn_begin);
1240 single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun))->flags |= BB_SUPERBLOCK;
1241 return;
1243 #endif
1245 if (fn_begin_outside_block)
1246 insert_insn_on_edge (seq, single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1247 else
1248 emit_insn_after (seq, fn_begin);
1251 /* Call back from expand_function_end to know where we should put
1252 the call to unwind_sjlj_unregister_libfunc if needed. */
1254 void
1255 sjlj_emit_function_exit_after (rtx_insn *after)
1257 crtl->eh.sjlj_exit_after = after;
1260 static void
1261 sjlj_emit_function_exit (void)
1263 rtx_insn *seq, *insn;
1265 start_sequence ();
1267 emit_library_call (unwind_sjlj_unregister_libfunc, LCT_NORMAL, VOIDmode,
1268 XEXP (crtl->eh.sjlj_fc, 0), Pmode);
1270 seq = get_insns ();
1271 end_sequence ();
1273 /* ??? Really this can be done in any block at loop level 0 that
1274 post-dominates all can_throw_internal instructions. This is
1275 the last possible moment. */
1277 insn = crtl->eh.sjlj_exit_after;
1278 if (LABEL_P (insn))
1279 insn = NEXT_INSN (insn);
1281 emit_insn_after (seq, insn);
1284 static void
1285 sjlj_emit_dispatch_table (rtx_code_label *dispatch_label, int num_dispatch)
1287 scalar_int_mode unwind_word_mode = targetm.unwind_word_mode ();
1288 scalar_int_mode filter_mode = targetm.eh_return_filter_mode ();
1289 eh_landing_pad lp;
1290 rtx mem, fc, exc_ptr_reg, filter_reg;
1291 rtx_insn *seq;
1292 basic_block bb;
1293 eh_region r;
1294 int i, disp_index;
1295 vec<tree> dispatch_labels = vNULL;
1297 fc = crtl->eh.sjlj_fc;
1299 start_sequence ();
1301 emit_label (dispatch_label);
1303 #ifndef DONT_USE_BUILTIN_SETJMP
1304 expand_builtin_setjmp_receiver (dispatch_label);
1306 /* The caller of expand_builtin_setjmp_receiver is responsible for
1307 making sure that the label doesn't vanish. The only other caller
1308 is the expander for __builtin_setjmp_receiver, which places this
1309 label on the nonlocal_goto_label list. Since we're modeling these
1310 CFG edges more exactly, we can use the forced_labels list instead. */
1311 LABEL_PRESERVE_P (dispatch_label) = 1;
1312 vec_safe_push<rtx_insn *> (forced_labels, dispatch_label);
1313 #endif
1315 /* Load up exc_ptr and filter values from the function context. */
1316 mem = adjust_address (fc, unwind_word_mode, sjlj_fc_data_ofs);
1317 if (unwind_word_mode != ptr_mode)
1319 #ifdef POINTERS_EXTEND_UNSIGNED
1320 mem = convert_memory_address (ptr_mode, mem);
1321 #else
1322 mem = convert_to_mode (ptr_mode, mem, 0);
1323 #endif
1325 exc_ptr_reg = force_reg (ptr_mode, mem);
1327 mem = adjust_address (fc, unwind_word_mode,
1328 sjlj_fc_data_ofs + GET_MODE_SIZE (unwind_word_mode));
1329 if (unwind_word_mode != filter_mode)
1330 mem = convert_to_mode (filter_mode, mem, 0);
1331 filter_reg = force_reg (filter_mode, mem);
1333 /* Jump to one of the directly reachable regions. */
1335 disp_index = 0;
1336 rtx_code_label *first_reachable_label = NULL;
1338 /* If there's exactly one call site in the function, don't bother
1339 generating a switch statement. */
1340 if (num_dispatch > 1)
1341 dispatch_labels.create (num_dispatch);
1343 for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
1344 if (lp && lp->post_landing_pad)
1346 rtx_insn *seq2;
1347 rtx_code_label *label;
1349 start_sequence ();
1351 lp->landing_pad = dispatch_label;
1353 if (num_dispatch > 1)
1355 tree t_label, case_elt, t;
1357 t_label = create_artificial_label (UNKNOWN_LOCATION);
1358 t = build_int_cst (integer_type_node, disp_index);
1359 case_elt = build_case_label (t, NULL, t_label);
1360 dispatch_labels.quick_push (case_elt);
1361 label = jump_target_rtx (t_label);
1363 else
1364 label = gen_label_rtx ();
1366 if (disp_index == 0)
1367 first_reachable_label = label;
1368 emit_label (label);
1370 r = lp->region;
1371 if (r->exc_ptr_reg)
1372 emit_move_insn (r->exc_ptr_reg, exc_ptr_reg);
1373 if (r->filter_reg)
1374 emit_move_insn (r->filter_reg, filter_reg);
1376 seq2 = get_insns ();
1377 end_sequence ();
1379 rtx_insn *before = label_rtx (lp->post_landing_pad);
1380 bb = emit_to_new_bb_before (seq2, before);
1381 make_single_succ_edge (bb, bb->next_bb, EDGE_FALLTHRU);
1382 if (current_loops)
1384 struct loop *loop = bb->next_bb->loop_father;
1385 /* If we created a pre-header block, add the new block to the
1386 outer loop, otherwise to the loop itself. */
1387 if (bb->next_bb == loop->header)
1388 add_bb_to_loop (bb, loop_outer (loop));
1389 else
1390 add_bb_to_loop (bb, loop);
1391 /* ??? For multiple dispatches we will end up with edges
1392 from the loop tree root into this loop, making it a
1393 multiple-entry loop. Discard all affected loops. */
1394 if (num_dispatch > 1)
1396 for (loop = bb->loop_father;
1397 loop_outer (loop); loop = loop_outer (loop))
1398 mark_loop_for_removal (loop);
1402 disp_index++;
1404 gcc_assert (disp_index == num_dispatch);
1406 if (num_dispatch > 1)
1408 rtx disp = adjust_address (fc, TYPE_MODE (integer_type_node),
1409 sjlj_fc_call_site_ofs);
1410 expand_sjlj_dispatch_table (disp, dispatch_labels);
1413 seq = get_insns ();
1414 end_sequence ();
1416 bb = emit_to_new_bb_before (seq, first_reachable_label);
1417 if (num_dispatch == 1)
1419 make_single_succ_edge (bb, bb->next_bb, EDGE_FALLTHRU);
1420 if (current_loops)
1422 struct loop *loop = bb->next_bb->loop_father;
1423 /* If we created a pre-header block, add the new block to the
1424 outer loop, otherwise to the loop itself. */
1425 if (bb->next_bb == loop->header)
1426 add_bb_to_loop (bb, loop_outer (loop));
1427 else
1428 add_bb_to_loop (bb, loop);
1431 else
1433 /* We are not wiring up edges here, but as the dispatcher call
1434 is at function begin simply associate the block with the
1435 outermost (non-)loop. */
1436 if (current_loops)
1437 add_bb_to_loop (bb, current_loops->tree_root);
1441 static void
1442 sjlj_build_landing_pads (void)
1444 int num_dispatch;
1446 num_dispatch = vec_safe_length (cfun->eh->lp_array);
1447 if (num_dispatch == 0)
1448 return;
1449 sjlj_lp_call_site_index.safe_grow_cleared (num_dispatch);
1451 num_dispatch = sjlj_assign_call_site_values ();
1452 if (num_dispatch > 0)
1454 rtx_code_label *dispatch_label = gen_label_rtx ();
1455 int align = STACK_SLOT_ALIGNMENT (sjlj_fc_type_node,
1456 TYPE_MODE (sjlj_fc_type_node),
1457 TYPE_ALIGN (sjlj_fc_type_node));
1458 crtl->eh.sjlj_fc
1459 = assign_stack_local (TYPE_MODE (sjlj_fc_type_node),
1460 int_size_in_bytes (sjlj_fc_type_node),
1461 align);
1463 sjlj_mark_call_sites ();
1464 sjlj_emit_function_enter (dispatch_label);
1465 sjlj_emit_dispatch_table (dispatch_label, num_dispatch);
1466 sjlj_emit_function_exit ();
1469 /* If we do not have any landing pads, we may still need to register a
1470 personality routine and (empty) LSDA to handle must-not-throw regions. */
1471 else if (function_needs_eh_personality (cfun) != eh_personality_none)
1473 int align = STACK_SLOT_ALIGNMENT (sjlj_fc_type_node,
1474 TYPE_MODE (sjlj_fc_type_node),
1475 TYPE_ALIGN (sjlj_fc_type_node));
1476 crtl->eh.sjlj_fc
1477 = assign_stack_local (TYPE_MODE (sjlj_fc_type_node),
1478 int_size_in_bytes (sjlj_fc_type_node),
1479 align);
1481 sjlj_mark_call_sites ();
1482 sjlj_emit_function_enter (NULL);
1483 sjlj_emit_function_exit ();
1486 sjlj_lp_call_site_index.release ();
1489 /* Update the sjlj function context. This function should be called
1490 whenever we allocate or deallocate dynamic stack space. */
1492 void
1493 update_sjlj_context (void)
1495 if (!flag_exceptions)
1496 return;
1498 emit_note (NOTE_INSN_UPDATE_SJLJ_CONTEXT);
1501 /* After initial rtl generation, call back to finish generating
1502 exception support code. */
1504 void
1505 finish_eh_generation (void)
1507 basic_block bb;
1509 /* Construct the landing pads. */
1510 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
1511 sjlj_build_landing_pads ();
1512 else
1513 dw2_build_landing_pads ();
1514 break_superblocks ();
1516 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ
1517 /* Kludge for Alpha (see alpha_gp_save_rtx). */
1518 || single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun))->insns.r)
1519 commit_edge_insertions ();
1521 /* Redirect all EH edges from the post_landing_pad to the landing pad. */
1522 FOR_EACH_BB_FN (bb, cfun)
1524 eh_landing_pad lp;
1525 edge_iterator ei;
1526 edge e;
1528 lp = get_eh_landing_pad_from_rtx (BB_END (bb));
1530 FOR_EACH_EDGE (e, ei, bb->succs)
1531 if (e->flags & EDGE_EH)
1532 break;
1534 /* We should not have generated any new throwing insns during this
1535 pass, and we should not have lost any EH edges, so we only need
1536 to handle two cases here:
1537 (1) reachable handler and an existing edge to post-landing-pad,
1538 (2) no reachable handler and no edge. */
1539 gcc_assert ((lp != NULL) == (e != NULL));
1540 if (lp != NULL)
1542 gcc_assert (BB_HEAD (e->dest) == label_rtx (lp->post_landing_pad));
1544 redirect_edge_succ (e, BLOCK_FOR_INSN (lp->landing_pad));
1545 e->flags |= (CALL_P (BB_END (bb))
1546 ? EDGE_ABNORMAL | EDGE_ABNORMAL_CALL
1547 : EDGE_ABNORMAL);
1552 /* This section handles removing dead code for flow. */
1554 void
1555 remove_eh_landing_pad (eh_landing_pad lp)
1557 eh_landing_pad *pp;
1559 for (pp = &lp->region->landing_pads; *pp != lp; pp = &(*pp)->next_lp)
1560 continue;
1561 *pp = lp->next_lp;
1563 if (lp->post_landing_pad)
1564 EH_LANDING_PAD_NR (lp->post_landing_pad) = 0;
1565 (*cfun->eh->lp_array)[lp->index] = NULL;
1568 /* Splice the EH region at PP from the region tree. */
1570 static void
1571 remove_eh_handler_splicer (eh_region *pp)
1573 eh_region region = *pp;
1574 eh_landing_pad lp;
1576 for (lp = region->landing_pads; lp ; lp = lp->next_lp)
1578 if (lp->post_landing_pad)
1579 EH_LANDING_PAD_NR (lp->post_landing_pad) = 0;
1580 (*cfun->eh->lp_array)[lp->index] = NULL;
1583 if (region->inner)
1585 eh_region p, outer;
1586 outer = region->outer;
1588 *pp = p = region->inner;
1591 p->outer = outer;
1592 pp = &p->next_peer;
1593 p = *pp;
1595 while (p);
1597 *pp = region->next_peer;
1599 (*cfun->eh->region_array)[region->index] = NULL;
1602 /* Splice a single EH region REGION from the region tree.
1604 To unlink REGION, we need to find the pointer to it with a relatively
1605 expensive search in REGION's outer region. If you are going to
1606 remove a number of handlers, using remove_unreachable_eh_regions may
1607 be a better option. */
1609 void
1610 remove_eh_handler (eh_region region)
1612 eh_region *pp, *pp_start, p, outer;
1614 outer = region->outer;
1615 if (outer)
1616 pp_start = &outer->inner;
1617 else
1618 pp_start = &cfun->eh->region_tree;
1619 for (pp = pp_start, p = *pp; p != region; pp = &p->next_peer, p = *pp)
1620 continue;
1622 remove_eh_handler_splicer (pp);
1625 /* Worker for remove_unreachable_eh_regions.
1626 PP is a pointer to the region to start a region tree depth-first
1627 search from. R_REACHABLE is the set of regions that have to be
1628 preserved. */
1630 static void
1631 remove_unreachable_eh_regions_worker (eh_region *pp, sbitmap r_reachable)
1633 while (*pp)
1635 eh_region region = *pp;
1636 remove_unreachable_eh_regions_worker (&region->inner, r_reachable);
1637 if (!bitmap_bit_p (r_reachable, region->index))
1638 remove_eh_handler_splicer (pp);
1639 else
1640 pp = &region->next_peer;
1644 /* Splice all EH regions *not* marked in R_REACHABLE from the region tree.
1645 Do this by traversing the EH tree top-down and splice out regions that
1646 are not marked. By removing regions from the leaves, we avoid costly
1647 searches in the region tree. */
1649 void
1650 remove_unreachable_eh_regions (sbitmap r_reachable)
1652 remove_unreachable_eh_regions_worker (&cfun->eh->region_tree, r_reachable);
1655 /* Invokes CALLBACK for every exception handler landing pad label.
1656 Only used by reload hackery; should not be used by new code. */
1658 void
1659 for_each_eh_label (void (*callback) (rtx))
1661 eh_landing_pad lp;
1662 int i;
1664 for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
1666 if (lp)
1668 rtx_code_label *lab = lp->landing_pad;
1669 if (lab && LABEL_P (lab))
1670 (*callback) (lab);
1675 /* Create the REG_EH_REGION note for INSN, given its ECF_FLAGS for a
1676 call insn.
1678 At the gimple level, we use LP_NR
1679 > 0 : The statement transfers to landing pad LP_NR
1680 = 0 : The statement is outside any EH region
1681 < 0 : The statement is within MUST_NOT_THROW region -LP_NR.
1683 At the rtl level, we use LP_NR
1684 > 0 : The insn transfers to landing pad LP_NR
1685 = 0 : The insn cannot throw
1686 < 0 : The insn is within MUST_NOT_THROW region -LP_NR
1687 = INT_MIN : The insn cannot throw or execute a nonlocal-goto.
1688 missing note: The insn is outside any EH region.
1690 ??? This difference probably ought to be avoided. We could stand
1691 to record nothrow for arbitrary gimple statements, and so avoid
1692 some moderately complex lookups in stmt_could_throw_p. Perhaps
1693 NOTHROW should be mapped on both sides to INT_MIN. Perhaps the
1694 no-nonlocal-goto property should be recorded elsewhere as a bit
1695 on the call_insn directly. Perhaps we should make more use of
1696 attaching the trees to call_insns (reachable via symbol_ref in
1697 direct call cases) and just pull the data out of the trees. */
1699 void
1700 make_reg_eh_region_note (rtx_insn *insn, int ecf_flags, int lp_nr)
1702 rtx value;
1703 if (ecf_flags & ECF_NOTHROW)
1704 value = const0_rtx;
1705 else if (lp_nr != 0)
1706 value = GEN_INT (lp_nr);
1707 else
1708 return;
1709 add_reg_note (insn, REG_EH_REGION, value);
1712 /* Create a REG_EH_REGION note for a CALL_INSN that cannot throw
1713 nor perform a non-local goto. Replace the region note if it
1714 already exists. */
1716 void
1717 make_reg_eh_region_note_nothrow_nononlocal (rtx_insn *insn)
1719 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1720 rtx intmin = GEN_INT (INT_MIN);
1722 if (note != 0)
1723 XEXP (note, 0) = intmin;
1724 else
1725 add_reg_note (insn, REG_EH_REGION, intmin);
1728 /* Return true if INSN could throw, assuming no REG_EH_REGION note
1729 to the contrary. */
1731 bool
1732 insn_could_throw_p (const_rtx insn)
1734 if (!flag_exceptions)
1735 return false;
1736 if (CALL_P (insn))
1737 return true;
1738 if (INSN_P (insn) && cfun->can_throw_non_call_exceptions)
1739 return may_trap_p (PATTERN (insn));
1740 return false;
1743 /* Copy an REG_EH_REGION note to each insn that might throw beginning
1744 at FIRST and ending at LAST. NOTE_OR_INSN is either the source insn
1745 to look for a note, or the note itself. */
1747 void
1748 copy_reg_eh_region_note_forward (rtx note_or_insn, rtx_insn *first, rtx last)
1750 rtx_insn *insn;
1751 rtx note = note_or_insn;
1753 if (INSN_P (note_or_insn))
1755 note = find_reg_note (note_or_insn, REG_EH_REGION, NULL_RTX);
1756 if (note == NULL)
1757 return;
1759 note = XEXP (note, 0);
1761 for (insn = first; insn != last ; insn = NEXT_INSN (insn))
1762 if (!find_reg_note (insn, REG_EH_REGION, NULL_RTX)
1763 && insn_could_throw_p (insn))
1764 add_reg_note (insn, REG_EH_REGION, note);
1767 /* Likewise, but iterate backward. */
1769 void
1770 copy_reg_eh_region_note_backward (rtx note_or_insn, rtx_insn *last, rtx first)
1772 rtx_insn *insn;
1773 rtx note = note_or_insn;
1775 if (INSN_P (note_or_insn))
1777 note = find_reg_note (note_or_insn, REG_EH_REGION, NULL_RTX);
1778 if (note == NULL)
1779 return;
1781 note = XEXP (note, 0);
1783 for (insn = last; insn != first; insn = PREV_INSN (insn))
1784 if (insn_could_throw_p (insn))
1785 add_reg_note (insn, REG_EH_REGION, note);
1789 /* Extract all EH information from INSN. Return true if the insn
1790 was marked NOTHROW. */
1792 static bool
1793 get_eh_region_and_lp_from_rtx (const_rtx insn, eh_region *pr,
1794 eh_landing_pad *plp)
1796 eh_landing_pad lp = NULL;
1797 eh_region r = NULL;
1798 bool ret = false;
1799 rtx note;
1800 int lp_nr;
1802 if (! INSN_P (insn))
1803 goto egress;
1805 if (NONJUMP_INSN_P (insn)
1806 && GET_CODE (PATTERN (insn)) == SEQUENCE)
1807 insn = XVECEXP (PATTERN (insn), 0, 0);
1809 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1810 if (!note)
1812 ret = !insn_could_throw_p (insn);
1813 goto egress;
1816 lp_nr = INTVAL (XEXP (note, 0));
1817 if (lp_nr == 0 || lp_nr == INT_MIN)
1819 ret = true;
1820 goto egress;
1823 if (lp_nr < 0)
1824 r = (*cfun->eh->region_array)[-lp_nr];
1825 else
1827 lp = (*cfun->eh->lp_array)[lp_nr];
1828 r = lp->region;
1831 egress:
1832 *plp = lp;
1833 *pr = r;
1834 return ret;
1837 /* Return the landing pad to which INSN may go, or NULL if it does not
1838 have a reachable landing pad within this function. */
1840 eh_landing_pad
1841 get_eh_landing_pad_from_rtx (const_rtx insn)
1843 eh_landing_pad lp;
1844 eh_region r;
1846 get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1847 return lp;
1850 /* Return the region to which INSN may go, or NULL if it does not
1851 have a reachable region within this function. */
1853 eh_region
1854 get_eh_region_from_rtx (const_rtx insn)
1856 eh_landing_pad lp;
1857 eh_region r;
1859 get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1860 return r;
1863 /* Return true if INSN throws and is caught by something in this function. */
1865 bool
1866 can_throw_internal (const_rtx insn)
1868 return get_eh_landing_pad_from_rtx (insn) != NULL;
1871 /* Return true if INSN throws and escapes from the current function. */
1873 bool
1874 can_throw_external (const_rtx insn)
1876 eh_landing_pad lp;
1877 eh_region r;
1878 bool nothrow;
1880 if (! INSN_P (insn))
1881 return false;
1883 if (NONJUMP_INSN_P (insn)
1884 && GET_CODE (PATTERN (insn)) == SEQUENCE)
1886 rtx_sequence *seq = as_a <rtx_sequence *> (PATTERN (insn));
1887 int i, n = seq->len ();
1889 for (i = 0; i < n; i++)
1890 if (can_throw_external (seq->element (i)))
1891 return true;
1893 return false;
1896 nothrow = get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1898 /* If we can't throw, we obviously can't throw external. */
1899 if (nothrow)
1900 return false;
1902 /* If we have an internal landing pad, then we're not external. */
1903 if (lp != NULL)
1904 return false;
1906 /* If we're not within an EH region, then we are external. */
1907 if (r == NULL)
1908 return true;
1910 /* The only thing that ought to be left is MUST_NOT_THROW regions,
1911 which don't always have landing pads. */
1912 gcc_assert (r->type == ERT_MUST_NOT_THROW);
1913 return false;
1916 /* Return true if INSN cannot throw at all. */
1918 bool
1919 insn_nothrow_p (const_rtx insn)
1921 eh_landing_pad lp;
1922 eh_region r;
1924 if (! INSN_P (insn))
1925 return true;
1927 if (NONJUMP_INSN_P (insn)
1928 && GET_CODE (PATTERN (insn)) == SEQUENCE)
1930 rtx_sequence *seq = as_a <rtx_sequence *> (PATTERN (insn));
1931 int i, n = seq->len ();
1933 for (i = 0; i < n; i++)
1934 if (!insn_nothrow_p (seq->element (i)))
1935 return false;
1937 return true;
1940 return get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1943 /* Return true if INSN can perform a non-local goto. */
1944 /* ??? This test is here in this file because it (ab)uses REG_EH_REGION. */
1946 bool
1947 can_nonlocal_goto (const rtx_insn *insn)
1949 if (nonlocal_goto_handler_labels && CALL_P (insn))
1951 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1952 if (!note || INTVAL (XEXP (note, 0)) != INT_MIN)
1953 return true;
1955 return false;
1958 /* Set TREE_NOTHROW and crtl->all_throwers_are_sibcalls. */
1960 static unsigned int
1961 set_nothrow_function_flags (void)
1963 rtx_insn *insn;
1965 crtl->nothrow = 1;
1967 /* Assume crtl->all_throwers_are_sibcalls until we encounter
1968 something that can throw an exception. We specifically exempt
1969 CALL_INSNs that are SIBLING_CALL_P, as these are really jumps,
1970 and can't throw. Most CALL_INSNs are not SIBLING_CALL_P, so this
1971 is optimistic. */
1973 crtl->all_throwers_are_sibcalls = 1;
1975 /* If we don't know that this implementation of the function will
1976 actually be used, then we must not set TREE_NOTHROW, since
1977 callers must not assume that this function does not throw. */
1978 if (TREE_NOTHROW (current_function_decl))
1979 return 0;
1981 if (! flag_exceptions)
1982 return 0;
1984 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1985 if (can_throw_external (insn))
1987 crtl->nothrow = 0;
1989 if (!CALL_P (insn) || !SIBLING_CALL_P (insn))
1991 crtl->all_throwers_are_sibcalls = 0;
1992 return 0;
1996 if (crtl->nothrow
1997 && (cgraph_node::get (current_function_decl)->get_availability ()
1998 >= AVAIL_AVAILABLE))
2000 struct cgraph_node *node = cgraph_node::get (current_function_decl);
2001 struct cgraph_edge *e;
2002 for (e = node->callers; e; e = e->next_caller)
2003 e->can_throw_external = false;
2004 node->set_nothrow_flag (true);
2006 if (dump_file)
2007 fprintf (dump_file, "Marking function nothrow: %s\n\n",
2008 current_function_name ());
2010 return 0;
2013 namespace {
2015 const pass_data pass_data_set_nothrow_function_flags =
2017 RTL_PASS, /* type */
2018 "nothrow", /* name */
2019 OPTGROUP_NONE, /* optinfo_flags */
2020 TV_NONE, /* tv_id */
2021 0, /* properties_required */
2022 0, /* properties_provided */
2023 0, /* properties_destroyed */
2024 0, /* todo_flags_start */
2025 0, /* todo_flags_finish */
2028 class pass_set_nothrow_function_flags : public rtl_opt_pass
2030 public:
2031 pass_set_nothrow_function_flags (gcc::context *ctxt)
2032 : rtl_opt_pass (pass_data_set_nothrow_function_flags, ctxt)
2035 /* opt_pass methods: */
2036 virtual unsigned int execute (function *)
2038 return set_nothrow_function_flags ();
2041 }; // class pass_set_nothrow_function_flags
2043 } // anon namespace
2045 rtl_opt_pass *
2046 make_pass_set_nothrow_function_flags (gcc::context *ctxt)
2048 return new pass_set_nothrow_function_flags (ctxt);
2052 /* Various hooks for unwind library. */
2054 /* Expand the EH support builtin functions:
2055 __builtin_eh_pointer and __builtin_eh_filter. */
2057 static eh_region
2058 expand_builtin_eh_common (tree region_nr_t)
2060 HOST_WIDE_INT region_nr;
2061 eh_region region;
2063 gcc_assert (tree_fits_shwi_p (region_nr_t));
2064 region_nr = tree_to_shwi (region_nr_t);
2066 region = (*cfun->eh->region_array)[region_nr];
2068 /* ??? We shouldn't have been able to delete a eh region without
2069 deleting all the code that depended on it. */
2070 gcc_assert (region != NULL);
2072 return region;
2075 /* Expand to the exc_ptr value from the given eh region. */
2078 expand_builtin_eh_pointer (tree exp)
2080 eh_region region
2081 = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 0));
2082 if (region->exc_ptr_reg == NULL)
2083 region->exc_ptr_reg = gen_reg_rtx (ptr_mode);
2084 return region->exc_ptr_reg;
2087 /* Expand to the filter value from the given eh region. */
2090 expand_builtin_eh_filter (tree exp)
2092 eh_region region
2093 = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 0));
2094 if (region->filter_reg == NULL)
2095 region->filter_reg = gen_reg_rtx (targetm.eh_return_filter_mode ());
2096 return region->filter_reg;
2099 /* Copy the exc_ptr and filter values from one landing pad's registers
2100 to another. This is used to inline the resx statement. */
2103 expand_builtin_eh_copy_values (tree exp)
2105 eh_region dst
2106 = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 0));
2107 eh_region src
2108 = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 1));
2109 scalar_int_mode fmode = targetm.eh_return_filter_mode ();
2111 if (dst->exc_ptr_reg == NULL)
2112 dst->exc_ptr_reg = gen_reg_rtx (ptr_mode);
2113 if (src->exc_ptr_reg == NULL)
2114 src->exc_ptr_reg = gen_reg_rtx (ptr_mode);
2116 if (dst->filter_reg == NULL)
2117 dst->filter_reg = gen_reg_rtx (fmode);
2118 if (src->filter_reg == NULL)
2119 src->filter_reg = gen_reg_rtx (fmode);
2121 emit_move_insn (dst->exc_ptr_reg, src->exc_ptr_reg);
2122 emit_move_insn (dst->filter_reg, src->filter_reg);
2124 return const0_rtx;
2127 /* Do any necessary initialization to access arbitrary stack frames.
2128 On the SPARC, this means flushing the register windows. */
2130 void
2131 expand_builtin_unwind_init (void)
2133 /* Set this so all the registers get saved in our frame; we need to be
2134 able to copy the saved values for any registers from frames we unwind. */
2135 crtl->saves_all_registers = 1;
2137 SETUP_FRAME_ADDRESSES ();
2140 /* Map a non-negative number to an eh return data register number; expands
2141 to -1 if no return data register is associated with the input number.
2142 At least the inputs 0 and 1 must be mapped; the target may provide more. */
2145 expand_builtin_eh_return_data_regno (tree exp)
2147 tree which = CALL_EXPR_ARG (exp, 0);
2148 unsigned HOST_WIDE_INT iwhich;
2150 if (TREE_CODE (which) != INTEGER_CST)
2152 error ("argument of %<__builtin_eh_return_regno%> must be constant");
2153 return constm1_rtx;
2156 iwhich = tree_to_uhwi (which);
2157 iwhich = EH_RETURN_DATA_REGNO (iwhich);
2158 if (iwhich == INVALID_REGNUM)
2159 return constm1_rtx;
2161 #ifdef DWARF_FRAME_REGNUM
2162 iwhich = DWARF_FRAME_REGNUM (iwhich);
2163 #else
2164 iwhich = DBX_REGISTER_NUMBER (iwhich);
2165 #endif
2167 return GEN_INT (iwhich);
2170 /* Given a value extracted from the return address register or stack slot,
2171 return the actual address encoded in that value. */
2174 expand_builtin_extract_return_addr (tree addr_tree)
2176 rtx addr = expand_expr (addr_tree, NULL_RTX, Pmode, EXPAND_NORMAL);
2178 if (GET_MODE (addr) != Pmode
2179 && GET_MODE (addr) != VOIDmode)
2181 #ifdef POINTERS_EXTEND_UNSIGNED
2182 addr = convert_memory_address (Pmode, addr);
2183 #else
2184 addr = convert_to_mode (Pmode, addr, 0);
2185 #endif
2188 /* First mask out any unwanted bits. */
2189 rtx mask = MASK_RETURN_ADDR;
2190 if (mask)
2191 expand_and (Pmode, addr, mask, addr);
2193 /* Then adjust to find the real return address. */
2194 if (RETURN_ADDR_OFFSET)
2195 addr = plus_constant (Pmode, addr, RETURN_ADDR_OFFSET);
2197 return addr;
2200 /* Given an actual address in addr_tree, do any necessary encoding
2201 and return the value to be stored in the return address register or
2202 stack slot so the epilogue will return to that address. */
2205 expand_builtin_frob_return_addr (tree addr_tree)
2207 rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, EXPAND_NORMAL);
2209 addr = convert_memory_address (Pmode, addr);
2211 if (RETURN_ADDR_OFFSET)
2213 addr = force_reg (Pmode, addr);
2214 addr = plus_constant (Pmode, addr, -RETURN_ADDR_OFFSET);
2217 return addr;
2220 /* Set up the epilogue with the magic bits we'll need to return to the
2221 exception handler. */
2223 void
2224 expand_builtin_eh_return (tree stackadj_tree ATTRIBUTE_UNUSED,
2225 tree handler_tree)
2227 rtx tmp;
2229 #ifdef EH_RETURN_STACKADJ_RTX
2230 tmp = expand_expr (stackadj_tree, crtl->eh.ehr_stackadj,
2231 VOIDmode, EXPAND_NORMAL);
2232 tmp = convert_memory_address (Pmode, tmp);
2233 if (!crtl->eh.ehr_stackadj)
2234 crtl->eh.ehr_stackadj = copy_addr_to_reg (tmp);
2235 else if (tmp != crtl->eh.ehr_stackadj)
2236 emit_move_insn (crtl->eh.ehr_stackadj, tmp);
2237 #endif
2239 tmp = expand_expr (handler_tree, crtl->eh.ehr_handler,
2240 VOIDmode, EXPAND_NORMAL);
2241 tmp = convert_memory_address (Pmode, tmp);
2242 if (!crtl->eh.ehr_handler)
2243 crtl->eh.ehr_handler = copy_addr_to_reg (tmp);
2244 else if (tmp != crtl->eh.ehr_handler)
2245 emit_move_insn (crtl->eh.ehr_handler, tmp);
2247 if (!crtl->eh.ehr_label)
2248 crtl->eh.ehr_label = gen_label_rtx ();
2249 emit_jump (crtl->eh.ehr_label);
2252 /* Expand __builtin_eh_return. This exit path from the function loads up
2253 the eh return data registers, adjusts the stack, and branches to a
2254 given PC other than the normal return address. */
2256 void
2257 expand_eh_return (void)
2259 rtx_code_label *around_label;
2261 if (! crtl->eh.ehr_label)
2262 return;
2264 crtl->calls_eh_return = 1;
2266 #ifdef EH_RETURN_STACKADJ_RTX
2267 emit_move_insn (EH_RETURN_STACKADJ_RTX, const0_rtx);
2268 #endif
2270 around_label = gen_label_rtx ();
2271 emit_jump (around_label);
2273 emit_label (crtl->eh.ehr_label);
2274 clobber_return_register ();
2276 #ifdef EH_RETURN_STACKADJ_RTX
2277 emit_move_insn (EH_RETURN_STACKADJ_RTX, crtl->eh.ehr_stackadj);
2278 #endif
2280 if (targetm.have_eh_return ())
2281 emit_insn (targetm.gen_eh_return (crtl->eh.ehr_handler));
2282 else
2284 if (rtx handler = EH_RETURN_HANDLER_RTX)
2285 emit_move_insn (handler, crtl->eh.ehr_handler);
2286 else
2287 error ("__builtin_eh_return not supported on this target");
2290 emit_label (around_label);
2293 /* Convert a ptr_mode address ADDR_TREE to a Pmode address controlled by
2294 POINTERS_EXTEND_UNSIGNED and return it. */
2297 expand_builtin_extend_pointer (tree addr_tree)
2299 rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, EXPAND_NORMAL);
2300 int extend;
2302 #ifdef POINTERS_EXTEND_UNSIGNED
2303 extend = POINTERS_EXTEND_UNSIGNED;
2304 #else
2305 /* The previous EH code did an unsigned extend by default, so we do this also
2306 for consistency. */
2307 extend = 1;
2308 #endif
2310 return convert_modes (targetm.unwind_word_mode (), ptr_mode, addr, extend);
2313 static int
2314 add_action_record (action_hash_type *ar_hash, int filter, int next)
2316 struct action_record **slot, *new_ar, tmp;
2318 tmp.filter = filter;
2319 tmp.next = next;
2320 slot = ar_hash->find_slot (&tmp, INSERT);
2322 if ((new_ar = *slot) == NULL)
2324 new_ar = XNEW (struct action_record);
2325 new_ar->offset = crtl->eh.action_record_data->length () + 1;
2326 new_ar->filter = filter;
2327 new_ar->next = next;
2328 *slot = new_ar;
2330 /* The filter value goes in untouched. The link to the next
2331 record is a "self-relative" byte offset, or zero to indicate
2332 that there is no next record. So convert the absolute 1 based
2333 indices we've been carrying around into a displacement. */
2335 push_sleb128 (&crtl->eh.action_record_data, filter);
2336 if (next)
2337 next -= crtl->eh.action_record_data->length () + 1;
2338 push_sleb128 (&crtl->eh.action_record_data, next);
2341 return new_ar->offset;
2344 static int
2345 collect_one_action_chain (action_hash_type *ar_hash, eh_region region)
2347 int next;
2349 /* If we've reached the top of the region chain, then we have
2350 no actions, and require no landing pad. */
2351 if (region == NULL)
2352 return -1;
2354 switch (region->type)
2356 case ERT_CLEANUP:
2358 eh_region r;
2359 /* A cleanup adds a zero filter to the beginning of the chain, but
2360 there are special cases to look out for. If there are *only*
2361 cleanups along a path, then it compresses to a zero action.
2362 Further, if there are multiple cleanups along a path, we only
2363 need to represent one of them, as that is enough to trigger
2364 entry to the landing pad at runtime. */
2365 next = collect_one_action_chain (ar_hash, region->outer);
2366 if (next <= 0)
2367 return 0;
2368 for (r = region->outer; r ; r = r->outer)
2369 if (r->type == ERT_CLEANUP)
2370 return next;
2371 return add_action_record (ar_hash, 0, next);
2374 case ERT_TRY:
2376 eh_catch c;
2378 /* Process the associated catch regions in reverse order.
2379 If there's a catch-all handler, then we don't need to
2380 search outer regions. Use a magic -3 value to record
2381 that we haven't done the outer search. */
2382 next = -3;
2383 for (c = region->u.eh_try.last_catch; c ; c = c->prev_catch)
2385 if (c->type_list == NULL)
2387 /* Retrieve the filter from the head of the filter list
2388 where we have stored it (see assign_filter_values). */
2389 int filter = TREE_INT_CST_LOW (TREE_VALUE (c->filter_list));
2390 next = add_action_record (ar_hash, filter, 0);
2392 else
2394 /* Once the outer search is done, trigger an action record for
2395 each filter we have. */
2396 tree flt_node;
2398 if (next == -3)
2400 next = collect_one_action_chain (ar_hash, region->outer);
2402 /* If there is no next action, terminate the chain. */
2403 if (next == -1)
2404 next = 0;
2405 /* If all outer actions are cleanups or must_not_throw,
2406 we'll have no action record for it, since we had wanted
2407 to encode these states in the call-site record directly.
2408 Add a cleanup action to the chain to catch these. */
2409 else if (next <= 0)
2410 next = add_action_record (ar_hash, 0, 0);
2413 flt_node = c->filter_list;
2414 for (; flt_node; flt_node = TREE_CHAIN (flt_node))
2416 int filter = TREE_INT_CST_LOW (TREE_VALUE (flt_node));
2417 next = add_action_record (ar_hash, filter, next);
2421 return next;
2424 case ERT_ALLOWED_EXCEPTIONS:
2425 /* An exception specification adds its filter to the
2426 beginning of the chain. */
2427 next = collect_one_action_chain (ar_hash, region->outer);
2429 /* If there is no next action, terminate the chain. */
2430 if (next == -1)
2431 next = 0;
2432 /* If all outer actions are cleanups or must_not_throw,
2433 we'll have no action record for it, since we had wanted
2434 to encode these states in the call-site record directly.
2435 Add a cleanup action to the chain to catch these. */
2436 else if (next <= 0)
2437 next = add_action_record (ar_hash, 0, 0);
2439 return add_action_record (ar_hash, region->u.allowed.filter, next);
2441 case ERT_MUST_NOT_THROW:
2442 /* A must-not-throw region with no inner handlers or cleanups
2443 requires no call-site entry. Note that this differs from
2444 the no handler or cleanup case in that we do require an lsda
2445 to be generated. Return a magic -2 value to record this. */
2446 return -2;
2449 gcc_unreachable ();
2452 static int
2453 add_call_site (rtx landing_pad, int action, int section)
2455 call_site_record record;
2457 record = ggc_alloc<call_site_record_d> ();
2458 record->landing_pad = landing_pad;
2459 record->action = action;
2461 vec_safe_push (crtl->eh.call_site_record_v[section], record);
2463 return call_site_base + crtl->eh.call_site_record_v[section]->length () - 1;
2466 static rtx_note *
2467 emit_note_eh_region_end (rtx_insn *insn)
2469 rtx_insn *next = NEXT_INSN (insn);
2471 /* Make sure we do not split a call and its corresponding
2472 CALL_ARG_LOCATION note. */
2473 if (next && NOTE_P (next)
2474 && NOTE_KIND (next) == NOTE_INSN_CALL_ARG_LOCATION)
2475 insn = next;
2477 return emit_note_after (NOTE_INSN_EH_REGION_END, insn);
2480 /* Add NOP after NOTE_INSN_SWITCH_TEXT_SECTIONS when the cold section starts
2481 with landing pad.
2482 With landing pad being at offset 0 from the start label of the section
2483 we would miss EH delivery because 0 is special and means no landing pad. */
2485 static bool
2486 maybe_add_nop_after_section_switch (void)
2488 if (!crtl->uses_eh_lsda
2489 || !crtl->eh.call_site_record_v[1])
2490 return false;
2491 int n = vec_safe_length (crtl->eh.call_site_record_v[1]);
2492 hash_set<rtx_insn *> visited;
2494 for (int i = 0; i < n; ++i)
2496 struct call_site_record_d *cs
2497 = (*crtl->eh.call_site_record_v[1])[i];
2498 if (cs->landing_pad)
2500 rtx_insn *insn = as_a <rtx_insn *> (cs->landing_pad);
2501 while (true)
2503 /* Landing pads have LABEL_PRESERVE_P flag set. This check make
2504 sure that we do not walk past landing pad visited earlier
2505 which would result in possible quadratic behaviour. */
2506 if (LABEL_P (insn) && LABEL_PRESERVE_P (insn)
2507 && visited.add (insn))
2508 break;
2510 /* Conservatively assume that ASM insn may be empty. We have
2511 now way to tell what they contain. */
2512 if (active_insn_p (insn)
2513 && GET_CODE (PATTERN (insn)) != ASM_INPUT
2514 && GET_CODE (PATTERN (insn)) != ASM_OPERANDS)
2515 break;
2517 /* If we reached the start of hot section, then NOP will be
2518 needed. */
2519 if (GET_CODE (insn) == NOTE
2520 && NOTE_KIND (insn) == NOTE_INSN_SWITCH_TEXT_SECTIONS)
2522 emit_insn_after (gen_nop (), insn);
2523 break;
2526 /* We visit only labels from cold section. We should never hit
2527 begining of the insn stream here. */
2528 insn = PREV_INSN (insn);
2532 return false;
2535 /* Turn REG_EH_REGION notes back into NOTE_INSN_EH_REGION notes.
2536 The new note numbers will not refer to region numbers, but
2537 instead to call site entries. */
2539 static unsigned int
2540 convert_to_eh_region_ranges (void)
2542 rtx insn;
2543 rtx_insn *iter;
2544 rtx_note *note;
2545 action_hash_type ar_hash (31);
2546 int last_action = -3;
2547 rtx_insn *last_action_insn = NULL;
2548 rtx last_landing_pad = NULL_RTX;
2549 rtx_insn *first_no_action_insn = NULL;
2550 int call_site = 0;
2551 int cur_sec = 0;
2552 rtx_insn *section_switch_note = NULL;
2553 rtx_insn *first_no_action_insn_before_switch = NULL;
2554 rtx_insn *last_no_action_insn_before_switch = NULL;
2555 int saved_call_site_base = call_site_base;
2557 vec_alloc (crtl->eh.action_record_data, 64);
2559 for (iter = get_insns (); iter ; iter = NEXT_INSN (iter))
2560 if (INSN_P (iter))
2562 eh_landing_pad lp;
2563 eh_region region;
2564 bool nothrow;
2565 int this_action;
2566 rtx_code_label *this_landing_pad;
2568 insn = iter;
2569 if (NONJUMP_INSN_P (insn)
2570 && GET_CODE (PATTERN (insn)) == SEQUENCE)
2571 insn = XVECEXP (PATTERN (insn), 0, 0);
2573 nothrow = get_eh_region_and_lp_from_rtx (insn, &region, &lp);
2574 if (nothrow)
2575 continue;
2576 if (region)
2577 this_action = collect_one_action_chain (&ar_hash, region);
2578 else
2579 this_action = -1;
2581 /* Existence of catch handlers, or must-not-throw regions
2582 implies that an lsda is needed (even if empty). */
2583 if (this_action != -1)
2584 crtl->uses_eh_lsda = 1;
2586 /* Delay creation of region notes for no-action regions
2587 until we're sure that an lsda will be required. */
2588 else if (last_action == -3)
2590 first_no_action_insn = iter;
2591 last_action = -1;
2594 if (this_action >= 0)
2595 this_landing_pad = lp->landing_pad;
2596 else
2597 this_landing_pad = NULL;
2599 /* Differing actions or landing pads implies a change in call-site
2600 info, which implies some EH_REGION note should be emitted. */
2601 if (last_action != this_action
2602 || last_landing_pad != this_landing_pad)
2604 /* If there is a queued no-action region in the other section
2605 with hot/cold partitioning, emit it now. */
2606 if (first_no_action_insn_before_switch)
2608 gcc_assert (this_action != -1
2609 && last_action == (first_no_action_insn
2610 ? -1 : -3));
2611 call_site = add_call_site (NULL_RTX, 0, 0);
2612 note = emit_note_before (NOTE_INSN_EH_REGION_BEG,
2613 first_no_action_insn_before_switch);
2614 NOTE_EH_HANDLER (note) = call_site;
2615 note
2616 = emit_note_eh_region_end (last_no_action_insn_before_switch);
2617 NOTE_EH_HANDLER (note) = call_site;
2618 gcc_assert (last_action != -3
2619 || (last_action_insn
2620 == last_no_action_insn_before_switch));
2621 first_no_action_insn_before_switch = NULL;
2622 last_no_action_insn_before_switch = NULL;
2623 call_site_base++;
2625 /* If we'd not seen a previous action (-3) or the previous
2626 action was must-not-throw (-2), then we do not need an
2627 end note. */
2628 if (last_action >= -1)
2630 /* If we delayed the creation of the begin, do it now. */
2631 if (first_no_action_insn)
2633 call_site = add_call_site (NULL_RTX, 0, cur_sec);
2634 note = emit_note_before (NOTE_INSN_EH_REGION_BEG,
2635 first_no_action_insn);
2636 NOTE_EH_HANDLER (note) = call_site;
2637 first_no_action_insn = NULL;
2640 note = emit_note_eh_region_end (last_action_insn);
2641 NOTE_EH_HANDLER (note) = call_site;
2644 /* If the new action is must-not-throw, then no region notes
2645 are created. */
2646 if (this_action >= -1)
2648 call_site = add_call_site (this_landing_pad,
2649 this_action < 0 ? 0 : this_action,
2650 cur_sec);
2651 note = emit_note_before (NOTE_INSN_EH_REGION_BEG, iter);
2652 NOTE_EH_HANDLER (note) = call_site;
2655 last_action = this_action;
2656 last_landing_pad = this_landing_pad;
2658 last_action_insn = iter;
2660 else if (NOTE_P (iter)
2661 && NOTE_KIND (iter) == NOTE_INSN_SWITCH_TEXT_SECTIONS)
2663 gcc_assert (section_switch_note == NULL_RTX);
2664 gcc_assert (flag_reorder_blocks_and_partition);
2665 section_switch_note = iter;
2666 if (first_no_action_insn)
2668 first_no_action_insn_before_switch = first_no_action_insn;
2669 last_no_action_insn_before_switch = last_action_insn;
2670 first_no_action_insn = NULL;
2671 gcc_assert (last_action == -1);
2672 last_action = -3;
2674 /* Force closing of current EH region before section switch and
2675 opening a new one afterwards. */
2676 else if (last_action != -3)
2677 last_landing_pad = pc_rtx;
2678 if (crtl->eh.call_site_record_v[cur_sec])
2679 call_site_base += crtl->eh.call_site_record_v[cur_sec]->length ();
2680 cur_sec++;
2681 gcc_assert (crtl->eh.call_site_record_v[cur_sec] == NULL);
2682 vec_alloc (crtl->eh.call_site_record_v[cur_sec], 10);
2685 if (last_action >= -1 && ! first_no_action_insn)
2687 note = emit_note_eh_region_end (last_action_insn);
2688 NOTE_EH_HANDLER (note) = call_site;
2691 call_site_base = saved_call_site_base;
2693 return 0;
2696 namespace {
2698 const pass_data pass_data_convert_to_eh_region_ranges =
2700 RTL_PASS, /* type */
2701 "eh_ranges", /* name */
2702 OPTGROUP_NONE, /* optinfo_flags */
2703 TV_NONE, /* tv_id */
2704 0, /* properties_required */
2705 0, /* properties_provided */
2706 0, /* properties_destroyed */
2707 0, /* todo_flags_start */
2708 0, /* todo_flags_finish */
2711 class pass_convert_to_eh_region_ranges : public rtl_opt_pass
2713 public:
2714 pass_convert_to_eh_region_ranges (gcc::context *ctxt)
2715 : rtl_opt_pass (pass_data_convert_to_eh_region_ranges, ctxt)
2718 /* opt_pass methods: */
2719 virtual bool gate (function *);
2720 virtual unsigned int execute (function *)
2722 int ret = convert_to_eh_region_ranges ();
2723 maybe_add_nop_after_section_switch ();
2724 return ret;
2727 }; // class pass_convert_to_eh_region_ranges
2729 bool
2730 pass_convert_to_eh_region_ranges::gate (function *)
2732 /* Nothing to do for SJLJ exceptions or if no regions created. */
2733 if (cfun->eh->region_tree == NULL)
2734 return false;
2735 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
2736 return false;
2737 return true;
2740 } // anon namespace
2742 rtl_opt_pass *
2743 make_pass_convert_to_eh_region_ranges (gcc::context *ctxt)
2745 return new pass_convert_to_eh_region_ranges (ctxt);
2748 static void
2749 push_uleb128 (vec<uchar, va_gc> **data_area, unsigned int value)
2753 unsigned char byte = value & 0x7f;
2754 value >>= 7;
2755 if (value)
2756 byte |= 0x80;
2757 vec_safe_push (*data_area, byte);
2759 while (value);
2762 static void
2763 push_sleb128 (vec<uchar, va_gc> **data_area, int value)
2765 unsigned char byte;
2766 int more;
2770 byte = value & 0x7f;
2771 value >>= 7;
2772 more = ! ((value == 0 && (byte & 0x40) == 0)
2773 || (value == -1 && (byte & 0x40) != 0));
2774 if (more)
2775 byte |= 0x80;
2776 vec_safe_push (*data_area, byte);
2778 while (more);
2782 static int
2783 dw2_size_of_call_site_table (int section)
2785 int n = vec_safe_length (crtl->eh.call_site_record_v[section]);
2786 int size = n * (4 + 4 + 4);
2787 int i;
2789 for (i = 0; i < n; ++i)
2791 struct call_site_record_d *cs =
2792 (*crtl->eh.call_site_record_v[section])[i];
2793 size += size_of_uleb128 (cs->action);
2796 return size;
2799 static int
2800 sjlj_size_of_call_site_table (void)
2802 int n = vec_safe_length (crtl->eh.call_site_record_v[0]);
2803 int size = 0;
2804 int i;
2806 for (i = 0; i < n; ++i)
2808 struct call_site_record_d *cs =
2809 (*crtl->eh.call_site_record_v[0])[i];
2810 size += size_of_uleb128 (INTVAL (cs->landing_pad));
2811 size += size_of_uleb128 (cs->action);
2814 return size;
2817 static void
2818 dw2_output_call_site_table (int cs_format, int section)
2820 int n = vec_safe_length (crtl->eh.call_site_record_v[section]);
2821 int i;
2822 const char *begin;
2824 if (section == 0)
2825 begin = current_function_func_begin_label;
2826 else if (first_function_block_is_cold)
2827 begin = crtl->subsections.hot_section_label;
2828 else
2829 begin = crtl->subsections.cold_section_label;
2831 for (i = 0; i < n; ++i)
2833 struct call_site_record_d *cs = (*crtl->eh.call_site_record_v[section])[i];
2834 char reg_start_lab[32];
2835 char reg_end_lab[32];
2836 char landing_pad_lab[32];
2838 ASM_GENERATE_INTERNAL_LABEL (reg_start_lab, "LEHB", call_site_base + i);
2839 ASM_GENERATE_INTERNAL_LABEL (reg_end_lab, "LEHE", call_site_base + i);
2841 if (cs->landing_pad)
2842 ASM_GENERATE_INTERNAL_LABEL (landing_pad_lab, "L",
2843 CODE_LABEL_NUMBER (cs->landing_pad));
2845 /* ??? Perhaps use insn length scaling if the assembler supports
2846 generic arithmetic. */
2847 /* ??? Perhaps use attr_length to choose data1 or data2 instead of
2848 data4 if the function is small enough. */
2849 if (cs_format == DW_EH_PE_uleb128)
2851 dw2_asm_output_delta_uleb128 (reg_start_lab, begin,
2852 "region %d start", i);
2853 dw2_asm_output_delta_uleb128 (reg_end_lab, reg_start_lab,
2854 "length");
2855 if (cs->landing_pad)
2856 dw2_asm_output_delta_uleb128 (landing_pad_lab, begin,
2857 "landing pad");
2858 else
2859 dw2_asm_output_data_uleb128 (0, "landing pad");
2861 else
2863 dw2_asm_output_delta (4, reg_start_lab, begin,
2864 "region %d start", i);
2865 dw2_asm_output_delta (4, reg_end_lab, reg_start_lab, "length");
2866 if (cs->landing_pad)
2867 dw2_asm_output_delta (4, landing_pad_lab, begin,
2868 "landing pad");
2869 else
2870 dw2_asm_output_data (4, 0, "landing pad");
2872 dw2_asm_output_data_uleb128 (cs->action, "action");
2875 call_site_base += n;
2878 static void
2879 sjlj_output_call_site_table (void)
2881 int n = vec_safe_length (crtl->eh.call_site_record_v[0]);
2882 int i;
2884 for (i = 0; i < n; ++i)
2886 struct call_site_record_d *cs = (*crtl->eh.call_site_record_v[0])[i];
2888 dw2_asm_output_data_uleb128 (INTVAL (cs->landing_pad),
2889 "region %d landing pad", i);
2890 dw2_asm_output_data_uleb128 (cs->action, "action");
2893 call_site_base += n;
2896 /* Switch to the section that should be used for exception tables. */
2898 static void
2899 switch_to_exception_section (const char * ARG_UNUSED (fnname))
2901 section *s;
2903 if (exception_section)
2904 s = exception_section;
2905 else
2907 int flags;
2909 if (EH_TABLES_CAN_BE_READ_ONLY)
2911 int tt_format =
2912 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
2913 flags = ((! flag_pic
2914 || ((tt_format & 0x70) != DW_EH_PE_absptr
2915 && (tt_format & 0x70) != DW_EH_PE_aligned))
2916 ? 0 : SECTION_WRITE);
2918 else
2919 flags = SECTION_WRITE;
2921 /* Compute the section and cache it into exception_section,
2922 unless it depends on the function name. */
2923 if (targetm_common.have_named_sections)
2925 #ifdef HAVE_LD_EH_GC_SECTIONS
2926 if (flag_function_sections
2927 || (DECL_COMDAT_GROUP (current_function_decl) && HAVE_COMDAT_GROUP))
2929 char *section_name = XNEWVEC (char, strlen (fnname) + 32);
2930 /* The EH table must match the code section, so only mark
2931 it linkonce if we have COMDAT groups to tie them together. */
2932 if (DECL_COMDAT_GROUP (current_function_decl) && HAVE_COMDAT_GROUP)
2933 flags |= SECTION_LINKONCE;
2934 sprintf (section_name, ".gcc_except_table.%s", fnname);
2935 s = get_section (section_name, flags, current_function_decl);
2936 free (section_name);
2938 else
2939 #endif
2940 exception_section
2941 = s = get_section (".gcc_except_table", flags, NULL);
2943 else
2944 exception_section
2945 = s = flags == SECTION_WRITE ? data_section : readonly_data_section;
2948 switch_to_section (s);
2952 /* Output a reference from an exception table to the type_info object TYPE.
2953 TT_FORMAT and TT_FORMAT_SIZE describe the DWARF encoding method used for
2954 the value. */
2956 static void
2957 output_ttype (tree type, int tt_format, int tt_format_size)
2959 rtx value;
2960 bool is_public = true;
2962 if (type == NULL_TREE)
2963 value = const0_rtx;
2964 else
2966 /* FIXME lto. pass_ipa_free_lang_data changes all types to
2967 runtime types so TYPE should already be a runtime type
2968 reference. When pass_ipa_free_lang data is made a default
2969 pass, we can then remove the call to lookup_type_for_runtime
2970 below. */
2971 if (TYPE_P (type))
2972 type = lookup_type_for_runtime (type);
2974 value = expand_expr (type, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
2976 /* Let cgraph know that the rtti decl is used. Not all of the
2977 paths below go through assemble_integer, which would take
2978 care of this for us. */
2979 STRIP_NOPS (type);
2980 if (TREE_CODE (type) == ADDR_EXPR)
2982 type = TREE_OPERAND (type, 0);
2983 if (VAR_P (type))
2984 is_public = TREE_PUBLIC (type);
2986 else
2987 gcc_assert (TREE_CODE (type) == INTEGER_CST);
2990 /* Allow the target to override the type table entry format. */
2991 if (targetm.asm_out.ttype (value))
2992 return;
2994 if (tt_format == DW_EH_PE_absptr || tt_format == DW_EH_PE_aligned)
2995 assemble_integer (value, tt_format_size,
2996 tt_format_size * BITS_PER_UNIT, 1);
2997 else
2998 dw2_asm_output_encoded_addr_rtx (tt_format, value, is_public, NULL);
3001 static void
3002 output_one_function_exception_table (int section)
3004 int tt_format, cs_format, lp_format, i;
3005 char ttype_label[32];
3006 char cs_after_size_label[32];
3007 char cs_end_label[32];
3008 int call_site_len;
3009 int have_tt_data;
3010 int tt_format_size = 0;
3012 have_tt_data = (vec_safe_length (cfun->eh->ttype_data)
3013 || (targetm.arm_eabi_unwinder
3014 ? vec_safe_length (cfun->eh->ehspec_data.arm_eabi)
3015 : vec_safe_length (cfun->eh->ehspec_data.other)));
3017 /* Indicate the format of the @TType entries. */
3018 if (! have_tt_data)
3019 tt_format = DW_EH_PE_omit;
3020 else
3022 tt_format = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3023 if (HAVE_AS_LEB128)
3024 ASM_GENERATE_INTERNAL_LABEL (ttype_label,
3025 section ? "LLSDATTC" : "LLSDATT",
3026 current_function_funcdef_no);
3028 tt_format_size = size_of_encoded_value (tt_format);
3030 assemble_align (tt_format_size * BITS_PER_UNIT);
3033 targetm.asm_out.internal_label (asm_out_file, section ? "LLSDAC" : "LLSDA",
3034 current_function_funcdef_no);
3036 /* The LSDA header. */
3038 /* Indicate the format of the landing pad start pointer. An omitted
3039 field implies @LPStart == @Start. */
3040 /* Currently we always put @LPStart == @Start. This field would
3041 be most useful in moving the landing pads completely out of
3042 line to another section, but it could also be used to minimize
3043 the size of uleb128 landing pad offsets. */
3044 lp_format = DW_EH_PE_omit;
3045 dw2_asm_output_data (1, lp_format, "@LPStart format (%s)",
3046 eh_data_format_name (lp_format));
3048 /* @LPStart pointer would go here. */
3050 dw2_asm_output_data (1, tt_format, "@TType format (%s)",
3051 eh_data_format_name (tt_format));
3053 if (!HAVE_AS_LEB128)
3055 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
3056 call_site_len = sjlj_size_of_call_site_table ();
3057 else
3058 call_site_len = dw2_size_of_call_site_table (section);
3061 /* A pc-relative 4-byte displacement to the @TType data. */
3062 if (have_tt_data)
3064 if (HAVE_AS_LEB128)
3066 char ttype_after_disp_label[32];
3067 ASM_GENERATE_INTERNAL_LABEL (ttype_after_disp_label,
3068 section ? "LLSDATTDC" : "LLSDATTD",
3069 current_function_funcdef_no);
3070 dw2_asm_output_delta_uleb128 (ttype_label, ttype_after_disp_label,
3071 "@TType base offset");
3072 ASM_OUTPUT_LABEL (asm_out_file, ttype_after_disp_label);
3074 else
3076 /* Ug. Alignment queers things. */
3077 unsigned int before_disp, after_disp, last_disp, disp;
3079 before_disp = 1 + 1;
3080 after_disp = (1 + size_of_uleb128 (call_site_len)
3081 + call_site_len
3082 + vec_safe_length (crtl->eh.action_record_data)
3083 + (vec_safe_length (cfun->eh->ttype_data)
3084 * tt_format_size));
3086 disp = after_disp;
3089 unsigned int disp_size, pad;
3091 last_disp = disp;
3092 disp_size = size_of_uleb128 (disp);
3093 pad = before_disp + disp_size + after_disp;
3094 if (pad % tt_format_size)
3095 pad = tt_format_size - (pad % tt_format_size);
3096 else
3097 pad = 0;
3098 disp = after_disp + pad;
3100 while (disp != last_disp);
3102 dw2_asm_output_data_uleb128 (disp, "@TType base offset");
3106 /* Indicate the format of the call-site offsets. */
3107 if (HAVE_AS_LEB128)
3108 cs_format = DW_EH_PE_uleb128;
3109 else
3110 cs_format = DW_EH_PE_udata4;
3112 dw2_asm_output_data (1, cs_format, "call-site format (%s)",
3113 eh_data_format_name (cs_format));
3115 if (HAVE_AS_LEB128)
3117 ASM_GENERATE_INTERNAL_LABEL (cs_after_size_label,
3118 section ? "LLSDACSBC" : "LLSDACSB",
3119 current_function_funcdef_no);
3120 ASM_GENERATE_INTERNAL_LABEL (cs_end_label,
3121 section ? "LLSDACSEC" : "LLSDACSE",
3122 current_function_funcdef_no);
3123 dw2_asm_output_delta_uleb128 (cs_end_label, cs_after_size_label,
3124 "Call-site table length");
3125 ASM_OUTPUT_LABEL (asm_out_file, cs_after_size_label);
3126 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
3127 sjlj_output_call_site_table ();
3128 else
3129 dw2_output_call_site_table (cs_format, section);
3130 ASM_OUTPUT_LABEL (asm_out_file, cs_end_label);
3132 else
3134 dw2_asm_output_data_uleb128 (call_site_len, "Call-site table length");
3135 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
3136 sjlj_output_call_site_table ();
3137 else
3138 dw2_output_call_site_table (cs_format, section);
3141 /* ??? Decode and interpret the data for flag_debug_asm. */
3143 uchar uc;
3144 FOR_EACH_VEC_ELT (*crtl->eh.action_record_data, i, uc)
3145 dw2_asm_output_data (1, uc, i ? NULL : "Action record table");
3148 if (have_tt_data)
3149 assemble_align (tt_format_size * BITS_PER_UNIT);
3151 i = vec_safe_length (cfun->eh->ttype_data);
3152 while (i-- > 0)
3154 tree type = (*cfun->eh->ttype_data)[i];
3155 output_ttype (type, tt_format, tt_format_size);
3158 if (HAVE_AS_LEB128 && have_tt_data)
3159 ASM_OUTPUT_LABEL (asm_out_file, ttype_label);
3161 /* ??? Decode and interpret the data for flag_debug_asm. */
3162 if (targetm.arm_eabi_unwinder)
3164 tree type;
3165 for (i = 0;
3166 vec_safe_iterate (cfun->eh->ehspec_data.arm_eabi, i, &type); ++i)
3167 output_ttype (type, tt_format, tt_format_size);
3169 else
3171 uchar uc;
3172 for (i = 0;
3173 vec_safe_iterate (cfun->eh->ehspec_data.other, i, &uc); ++i)
3174 dw2_asm_output_data (1, uc,
3175 i ? NULL : "Exception specification table");
3179 void
3180 output_function_exception_table (const char *fnname)
3182 rtx personality = get_personality_function (current_function_decl);
3184 /* Not all functions need anything. */
3185 if (! crtl->uses_eh_lsda)
3186 return;
3188 if (personality)
3190 assemble_external_libcall (personality);
3192 if (targetm.asm_out.emit_except_personality)
3193 targetm.asm_out.emit_except_personality (personality);
3196 switch_to_exception_section (fnname);
3198 /* If the target wants a label to begin the table, emit it here. */
3199 targetm.asm_out.emit_except_table_label (asm_out_file);
3201 output_one_function_exception_table (0);
3202 if (crtl->eh.call_site_record_v[1])
3203 output_one_function_exception_table (1);
3205 switch_to_section (current_function_section ());
3208 void
3209 set_eh_throw_stmt_table (function *fun, hash_map<gimple *, int> *table)
3211 fun->eh->throw_stmt_table = table;
3214 hash_map<gimple *, int> *
3215 get_eh_throw_stmt_table (struct function *fun)
3217 return fun->eh->throw_stmt_table;
3220 /* Determine if the function needs an EH personality function. */
3222 enum eh_personality_kind
3223 function_needs_eh_personality (struct function *fn)
3225 enum eh_personality_kind kind = eh_personality_none;
3226 eh_region i;
3228 FOR_ALL_EH_REGION_FN (i, fn)
3230 switch (i->type)
3232 case ERT_CLEANUP:
3233 /* Can do with any personality including the generic C one. */
3234 kind = eh_personality_any;
3235 break;
3237 case ERT_TRY:
3238 case ERT_ALLOWED_EXCEPTIONS:
3239 /* Always needs a EH personality function. The generic C
3240 personality doesn't handle these even for empty type lists. */
3241 return eh_personality_lang;
3243 case ERT_MUST_NOT_THROW:
3244 /* Always needs a EH personality function. The language may specify
3245 what abort routine that must be used, e.g. std::terminate. */
3246 return eh_personality_lang;
3250 return kind;
3253 /* Dump EH information to OUT. */
3255 void
3256 dump_eh_tree (FILE * out, struct function *fun)
3258 eh_region i;
3259 int depth = 0;
3260 static const char *const type_name[] = {
3261 "cleanup", "try", "allowed_exceptions", "must_not_throw"
3264 i = fun->eh->region_tree;
3265 if (!i)
3266 return;
3268 fprintf (out, "Eh tree:\n");
3269 while (1)
3271 fprintf (out, " %*s %i %s", depth * 2, "",
3272 i->index, type_name[(int) i->type]);
3274 if (i->landing_pads)
3276 eh_landing_pad lp;
3278 fprintf (out, " land:");
3279 if (current_ir_type () == IR_GIMPLE)
3281 for (lp = i->landing_pads; lp ; lp = lp->next_lp)
3283 fprintf (out, "{%i,", lp->index);
3284 print_generic_expr (out, lp->post_landing_pad);
3285 fputc ('}', out);
3286 if (lp->next_lp)
3287 fputc (',', out);
3290 else
3292 for (lp = i->landing_pads; lp ; lp = lp->next_lp)
3294 fprintf (out, "{%i,", lp->index);
3295 if (lp->landing_pad)
3296 fprintf (out, "%i%s,", INSN_UID (lp->landing_pad),
3297 NOTE_P (lp->landing_pad) ? "(del)" : "");
3298 else
3299 fprintf (out, "(nil),");
3300 if (lp->post_landing_pad)
3302 rtx_insn *lab = label_rtx (lp->post_landing_pad);
3303 fprintf (out, "%i%s}", INSN_UID (lab),
3304 NOTE_P (lab) ? "(del)" : "");
3306 else
3307 fprintf (out, "(nil)}");
3308 if (lp->next_lp)
3309 fputc (',', out);
3314 switch (i->type)
3316 case ERT_CLEANUP:
3317 case ERT_MUST_NOT_THROW:
3318 break;
3320 case ERT_TRY:
3322 eh_catch c;
3323 fprintf (out, " catch:");
3324 for (c = i->u.eh_try.first_catch; c; c = c->next_catch)
3326 fputc ('{', out);
3327 if (c->label)
3329 fprintf (out, "lab:");
3330 print_generic_expr (out, c->label);
3331 fputc (';', out);
3333 print_generic_expr (out, c->type_list);
3334 fputc ('}', out);
3335 if (c->next_catch)
3336 fputc (',', out);
3339 break;
3341 case ERT_ALLOWED_EXCEPTIONS:
3342 fprintf (out, " filter :%i types:", i->u.allowed.filter);
3343 print_generic_expr (out, i->u.allowed.type_list);
3344 break;
3346 fputc ('\n', out);
3348 /* If there are sub-regions, process them. */
3349 if (i->inner)
3350 i = i->inner, depth++;
3351 /* If there are peers, process them. */
3352 else if (i->next_peer)
3353 i = i->next_peer;
3354 /* Otherwise, step back up the tree to the next peer. */
3355 else
3359 i = i->outer;
3360 depth--;
3361 if (i == NULL)
3362 return;
3364 while (i->next_peer == NULL);
3365 i = i->next_peer;
3370 /* Dump the EH tree for FN on stderr. */
3372 DEBUG_FUNCTION void
3373 debug_eh_tree (struct function *fn)
3375 dump_eh_tree (stderr, fn);
3378 /* Verify invariants on EH datastructures. */
3380 DEBUG_FUNCTION void
3381 verify_eh_tree (struct function *fun)
3383 eh_region r, outer;
3384 int nvisited_lp, nvisited_r;
3385 int count_lp, count_r, depth, i;
3386 eh_landing_pad lp;
3387 bool err = false;
3389 if (!fun->eh->region_tree)
3390 return;
3392 count_r = 0;
3393 for (i = 1; vec_safe_iterate (fun->eh->region_array, i, &r); ++i)
3394 if (r)
3396 if (r->index == i)
3397 count_r++;
3398 else
3400 error ("region_array is corrupted for region %i", r->index);
3401 err = true;
3405 count_lp = 0;
3406 for (i = 1; vec_safe_iterate (fun->eh->lp_array, i, &lp); ++i)
3407 if (lp)
3409 if (lp->index == i)
3410 count_lp++;
3411 else
3413 error ("lp_array is corrupted for lp %i", lp->index);
3414 err = true;
3418 depth = nvisited_lp = nvisited_r = 0;
3419 outer = NULL;
3420 r = fun->eh->region_tree;
3421 while (1)
3423 if ((*fun->eh->region_array)[r->index] != r)
3425 error ("region_array is corrupted for region %i", r->index);
3426 err = true;
3428 if (r->outer != outer)
3430 error ("outer block of region %i is wrong", r->index);
3431 err = true;
3433 if (depth < 0)
3435 error ("negative nesting depth of region %i", r->index);
3436 err = true;
3438 nvisited_r++;
3440 for (lp = r->landing_pads; lp ; lp = lp->next_lp)
3442 if ((*fun->eh->lp_array)[lp->index] != lp)
3444 error ("lp_array is corrupted for lp %i", lp->index);
3445 err = true;
3447 if (lp->region != r)
3449 error ("region of lp %i is wrong", lp->index);
3450 err = true;
3452 nvisited_lp++;
3455 if (r->inner)
3456 outer = r, r = r->inner, depth++;
3457 else if (r->next_peer)
3458 r = r->next_peer;
3459 else
3463 r = r->outer;
3464 if (r == NULL)
3465 goto region_done;
3466 depth--;
3467 outer = r->outer;
3469 while (r->next_peer == NULL);
3470 r = r->next_peer;
3473 region_done:
3474 if (depth != 0)
3476 error ("tree list ends on depth %i", depth);
3477 err = true;
3479 if (count_r != nvisited_r)
3481 error ("region_array does not match region_tree");
3482 err = true;
3484 if (count_lp != nvisited_lp)
3486 error ("lp_array does not match region_tree");
3487 err = true;
3490 if (err)
3492 dump_eh_tree (stderr, fun);
3493 internal_error ("verify_eh_tree failed");
3497 #include "gt-except.h"