gcc/
[official-gcc.git] / gcc / except.c
blob081b402fc50ab89604a59abec9bb6c01dafc93d3
1 /* Implements exception handling.
2 Copyright (C) 1989-2015 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 "tm.h"
116 #include "rtl.h"
117 #include "alias.h"
118 #include "symtab.h"
119 #include "tree.h"
120 #include "fold-const.h"
121 #include "stringpool.h"
122 #include "stor-layout.h"
123 #include "flags.h"
124 #include "hard-reg-set.h"
125 #include "function.h"
126 #include "insn-codes.h"
127 #include "optabs.h"
128 #include "insn-config.h"
129 #include "expmed.h"
130 #include "dojump.h"
131 #include "explow.h"
132 #include "calls.h"
133 #include "emit-rtl.h"
134 #include "varasm.h"
135 #include "stmt.h"
136 #include "expr.h"
137 #include "libfuncs.h"
138 #include "except.h"
139 #include "output.h"
140 #include "dwarf2asm.h"
141 #include "dwarf2out.h"
142 #include "dwarf2.h"
143 #include "toplev.h"
144 #include "intl.h"
145 #include "tm_p.h"
146 #include "target.h"
147 #include "common/common-target.h"
148 #include "langhooks.h"
149 #include "predict.h"
150 #include "dominance.h"
151 #include "cfg.h"
152 #include "cfgrtl.h"
153 #include "basic-block.h"
154 #include "plugin-api.h"
155 #include "ipa-ref.h"
156 #include "cgraph.h"
157 #include "diagnostic.h"
158 #include "tree-pretty-print.h"
159 #include "tree-pass.h"
160 #include "cfgloop.h"
161 #include "builtins.h"
162 #include "tree-hash-traits.h"
164 static GTY(()) int call_site_base;
166 struct tree_hash_traits : simple_hashmap_traits <tree_hash> {};
167 static GTY (()) hash_map<tree, tree, tree_hash_traits> *type_to_runtime_map;
169 /* Describe the SjLj_Function_Context structure. */
170 static GTY(()) tree sjlj_fc_type_node;
171 static int sjlj_fc_call_site_ofs;
172 static int sjlj_fc_data_ofs;
173 static int sjlj_fc_personality_ofs;
174 static int sjlj_fc_lsda_ofs;
175 static int sjlj_fc_jbuf_ofs;
178 struct GTY(()) call_site_record_d
180 rtx landing_pad;
181 int action;
184 /* In the following structure and associated functions,
185 we represent entries in the action table as 1-based indices.
186 Special cases are:
188 0: null action record, non-null landing pad; implies cleanups
189 -1: null action record, null landing pad; implies no action
190 -2: no call-site entry; implies must_not_throw
191 -3: we have yet to process outer regions
193 Further, no special cases apply to the "next" field of the record.
194 For next, 0 means end of list. */
196 struct action_record
198 int offset;
199 int filter;
200 int next;
203 /* Hashtable helpers. */
205 struct action_record_hasher : free_ptr_hash <action_record>
207 static inline hashval_t hash (const action_record *);
208 static inline bool equal (const action_record *, const action_record *);
211 inline hashval_t
212 action_record_hasher::hash (const action_record *entry)
214 return entry->next * 1009 + entry->filter;
217 inline bool
218 action_record_hasher::equal (const action_record *entry,
219 const action_record *data)
221 return entry->filter == data->filter && entry->next == data->next;
224 typedef hash_table<action_record_hasher> action_hash_type;
226 static bool get_eh_region_and_lp_from_rtx (const_rtx, eh_region *,
227 eh_landing_pad *);
229 static void dw2_build_landing_pads (void);
231 static int collect_one_action_chain (action_hash_type *, eh_region);
232 static int add_call_site (rtx, int, int);
234 static void push_uleb128 (vec<uchar, va_gc> **, unsigned int);
235 static void push_sleb128 (vec<uchar, va_gc> **, int);
236 #ifndef HAVE_AS_LEB128
237 static int dw2_size_of_call_site_table (int);
238 static int sjlj_size_of_call_site_table (void);
239 #endif
240 static void dw2_output_call_site_table (int, int);
241 static void sjlj_output_call_site_table (void);
244 void
245 init_eh (void)
247 if (! flag_exceptions)
248 return;
250 type_to_runtime_map
251 = hash_map<tree, tree, tree_hash_traits>::create_ggc (31);
253 /* Create the SjLj_Function_Context structure. This should match
254 the definition in unwind-sjlj.c. */
255 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
257 tree f_jbuf, f_per, f_lsda, f_prev, f_cs, f_data, tmp;
259 sjlj_fc_type_node = lang_hooks.types.make_type (RECORD_TYPE);
261 f_prev = build_decl (BUILTINS_LOCATION,
262 FIELD_DECL, get_identifier ("__prev"),
263 build_pointer_type (sjlj_fc_type_node));
264 DECL_FIELD_CONTEXT (f_prev) = sjlj_fc_type_node;
266 f_cs = build_decl (BUILTINS_LOCATION,
267 FIELD_DECL, get_identifier ("__call_site"),
268 integer_type_node);
269 DECL_FIELD_CONTEXT (f_cs) = sjlj_fc_type_node;
271 tmp = build_index_type (size_int (4 - 1));
272 tmp = build_array_type (lang_hooks.types.type_for_mode
273 (targetm.unwind_word_mode (), 1),
274 tmp);
275 f_data = build_decl (BUILTINS_LOCATION,
276 FIELD_DECL, get_identifier ("__data"), tmp);
277 DECL_FIELD_CONTEXT (f_data) = sjlj_fc_type_node;
279 f_per = build_decl (BUILTINS_LOCATION,
280 FIELD_DECL, get_identifier ("__personality"),
281 ptr_type_node);
282 DECL_FIELD_CONTEXT (f_per) = sjlj_fc_type_node;
284 f_lsda = build_decl (BUILTINS_LOCATION,
285 FIELD_DECL, get_identifier ("__lsda"),
286 ptr_type_node);
287 DECL_FIELD_CONTEXT (f_lsda) = sjlj_fc_type_node;
289 #ifdef DONT_USE_BUILTIN_SETJMP
290 #ifdef JMP_BUF_SIZE
291 tmp = size_int (JMP_BUF_SIZE - 1);
292 #else
293 /* Should be large enough for most systems, if it is not,
294 JMP_BUF_SIZE should be defined with the proper value. It will
295 also tend to be larger than necessary for most systems, a more
296 optimal port will define JMP_BUF_SIZE. */
297 tmp = size_int (FIRST_PSEUDO_REGISTER + 2 - 1);
298 #endif
299 #else
300 /* Compute a minimally sized jump buffer. We need room to store at
301 least 3 pointers - stack pointer, frame pointer and return address.
302 Plus for some targets we need room for an extra pointer - in the
303 case of MIPS this is the global pointer. This makes a total of four
304 pointers, but to be safe we actually allocate room for 5.
306 If pointers are smaller than words then we allocate enough room for
307 5 words, just in case the backend needs this much room. For more
308 discussion on this issue see:
309 http://gcc.gnu.org/ml/gcc-patches/2014-05/msg00313.html. */
310 if (POINTER_SIZE > BITS_PER_WORD)
311 tmp = size_int (5 - 1);
312 else
313 tmp = size_int ((5 * BITS_PER_WORD / POINTER_SIZE) - 1);
314 #endif
316 tmp = build_index_type (tmp);
317 tmp = build_array_type (ptr_type_node, tmp);
318 f_jbuf = build_decl (BUILTINS_LOCATION,
319 FIELD_DECL, get_identifier ("__jbuf"), tmp);
320 #ifdef DONT_USE_BUILTIN_SETJMP
321 /* We don't know what the alignment requirements of the
322 runtime's jmp_buf has. Overestimate. */
323 DECL_ALIGN (f_jbuf) = BIGGEST_ALIGNMENT;
324 DECL_USER_ALIGN (f_jbuf) = 1;
325 #endif
326 DECL_FIELD_CONTEXT (f_jbuf) = sjlj_fc_type_node;
328 TYPE_FIELDS (sjlj_fc_type_node) = f_prev;
329 TREE_CHAIN (f_prev) = f_cs;
330 TREE_CHAIN (f_cs) = f_data;
331 TREE_CHAIN (f_data) = f_per;
332 TREE_CHAIN (f_per) = f_lsda;
333 TREE_CHAIN (f_lsda) = f_jbuf;
335 layout_type (sjlj_fc_type_node);
337 /* Cache the interesting field offsets so that we have
338 easy access from rtl. */
339 sjlj_fc_call_site_ofs
340 = (tree_to_uhwi (DECL_FIELD_OFFSET (f_cs))
341 + tree_to_uhwi (DECL_FIELD_BIT_OFFSET (f_cs)) / BITS_PER_UNIT);
342 sjlj_fc_data_ofs
343 = (tree_to_uhwi (DECL_FIELD_OFFSET (f_data))
344 + tree_to_uhwi (DECL_FIELD_BIT_OFFSET (f_data)) / BITS_PER_UNIT);
345 sjlj_fc_personality_ofs
346 = (tree_to_uhwi (DECL_FIELD_OFFSET (f_per))
347 + tree_to_uhwi (DECL_FIELD_BIT_OFFSET (f_per)) / BITS_PER_UNIT);
348 sjlj_fc_lsda_ofs
349 = (tree_to_uhwi (DECL_FIELD_OFFSET (f_lsda))
350 + tree_to_uhwi (DECL_FIELD_BIT_OFFSET (f_lsda)) / BITS_PER_UNIT);
351 sjlj_fc_jbuf_ofs
352 = (tree_to_uhwi (DECL_FIELD_OFFSET (f_jbuf))
353 + tree_to_uhwi (DECL_FIELD_BIT_OFFSET (f_jbuf)) / BITS_PER_UNIT);
357 void
358 init_eh_for_function (void)
360 cfun->eh = ggc_cleared_alloc<eh_status> ();
362 /* Make sure zero'th entries are used. */
363 vec_safe_push (cfun->eh->region_array, (eh_region)0);
364 vec_safe_push (cfun->eh->lp_array, (eh_landing_pad)0);
367 /* Routines to generate the exception tree somewhat directly.
368 These are used from tree-eh.c when processing exception related
369 nodes during tree optimization. */
371 static eh_region
372 gen_eh_region (enum eh_region_type type, eh_region outer)
374 eh_region new_eh;
376 /* Insert a new blank region as a leaf in the tree. */
377 new_eh = ggc_cleared_alloc<eh_region_d> ();
378 new_eh->type = type;
379 new_eh->outer = outer;
380 if (outer)
382 new_eh->next_peer = outer->inner;
383 outer->inner = new_eh;
385 else
387 new_eh->next_peer = cfun->eh->region_tree;
388 cfun->eh->region_tree = new_eh;
391 new_eh->index = vec_safe_length (cfun->eh->region_array);
392 vec_safe_push (cfun->eh->region_array, new_eh);
394 /* Copy the language's notion of whether to use __cxa_end_cleanup. */
395 if (targetm.arm_eabi_unwinder && lang_hooks.eh_use_cxa_end_cleanup)
396 new_eh->use_cxa_end_cleanup = true;
398 return new_eh;
401 eh_region
402 gen_eh_region_cleanup (eh_region outer)
404 return gen_eh_region (ERT_CLEANUP, outer);
407 eh_region
408 gen_eh_region_try (eh_region outer)
410 return gen_eh_region (ERT_TRY, outer);
413 eh_catch
414 gen_eh_region_catch (eh_region t, tree type_or_list)
416 eh_catch c, l;
417 tree type_list, type_node;
419 gcc_assert (t->type == ERT_TRY);
421 /* Ensure to always end up with a type list to normalize further
422 processing, then register each type against the runtime types map. */
423 type_list = type_or_list;
424 if (type_or_list)
426 if (TREE_CODE (type_or_list) != TREE_LIST)
427 type_list = tree_cons (NULL_TREE, type_or_list, NULL_TREE);
429 type_node = type_list;
430 for (; type_node; type_node = TREE_CHAIN (type_node))
431 add_type_for_runtime (TREE_VALUE (type_node));
434 c = ggc_cleared_alloc<eh_catch_d> ();
435 c->type_list = type_list;
436 l = t->u.eh_try.last_catch;
437 c->prev_catch = l;
438 if (l)
439 l->next_catch = c;
440 else
441 t->u.eh_try.first_catch = c;
442 t->u.eh_try.last_catch = c;
444 return c;
447 eh_region
448 gen_eh_region_allowed (eh_region outer, tree allowed)
450 eh_region region = gen_eh_region (ERT_ALLOWED_EXCEPTIONS, outer);
451 region->u.allowed.type_list = allowed;
453 for (; allowed ; allowed = TREE_CHAIN (allowed))
454 add_type_for_runtime (TREE_VALUE (allowed));
456 return region;
459 eh_region
460 gen_eh_region_must_not_throw (eh_region outer)
462 return gen_eh_region (ERT_MUST_NOT_THROW, outer);
465 eh_landing_pad
466 gen_eh_landing_pad (eh_region region)
468 eh_landing_pad lp = ggc_cleared_alloc<eh_landing_pad_d> ();
470 lp->next_lp = region->landing_pads;
471 lp->region = region;
472 lp->index = vec_safe_length (cfun->eh->lp_array);
473 region->landing_pads = lp;
475 vec_safe_push (cfun->eh->lp_array, lp);
477 return lp;
480 eh_region
481 get_eh_region_from_number_fn (struct function *ifun, int i)
483 return (*ifun->eh->region_array)[i];
486 eh_region
487 get_eh_region_from_number (int i)
489 return get_eh_region_from_number_fn (cfun, i);
492 eh_landing_pad
493 get_eh_landing_pad_from_number_fn (struct function *ifun, int i)
495 return (*ifun->eh->lp_array)[i];
498 eh_landing_pad
499 get_eh_landing_pad_from_number (int i)
501 return get_eh_landing_pad_from_number_fn (cfun, i);
504 eh_region
505 get_eh_region_from_lp_number_fn (struct function *ifun, int i)
507 if (i < 0)
508 return (*ifun->eh->region_array)[-i];
509 else if (i == 0)
510 return NULL;
511 else
513 eh_landing_pad lp;
514 lp = (*ifun->eh->lp_array)[i];
515 return lp->region;
519 eh_region
520 get_eh_region_from_lp_number (int i)
522 return get_eh_region_from_lp_number_fn (cfun, i);
525 /* Returns true if the current function has exception handling regions. */
527 bool
528 current_function_has_exception_handlers (void)
530 return cfun->eh->region_tree != NULL;
533 /* A subroutine of duplicate_eh_regions. Copy the eh_region tree at OLD.
534 Root it at OUTER, and apply LP_OFFSET to the lp numbers. */
536 struct duplicate_eh_regions_data
538 duplicate_eh_regions_map label_map;
539 void *label_map_data;
540 hash_map<void *, void *> *eh_map;
543 static void
544 duplicate_eh_regions_1 (struct duplicate_eh_regions_data *data,
545 eh_region old_r, eh_region outer)
547 eh_landing_pad old_lp, new_lp;
548 eh_region new_r;
550 new_r = gen_eh_region (old_r->type, outer);
551 gcc_assert (!data->eh_map->put (old_r, new_r));
553 switch (old_r->type)
555 case ERT_CLEANUP:
556 break;
558 case ERT_TRY:
560 eh_catch oc, nc;
561 for (oc = old_r->u.eh_try.first_catch; oc ; oc = oc->next_catch)
563 /* We should be doing all our region duplication before and
564 during inlining, which is before filter lists are created. */
565 gcc_assert (oc->filter_list == NULL);
566 nc = gen_eh_region_catch (new_r, oc->type_list);
567 nc->label = data->label_map (oc->label, data->label_map_data);
570 break;
572 case ERT_ALLOWED_EXCEPTIONS:
573 new_r->u.allowed.type_list = old_r->u.allowed.type_list;
574 if (old_r->u.allowed.label)
575 new_r->u.allowed.label
576 = data->label_map (old_r->u.allowed.label, data->label_map_data);
577 else
578 new_r->u.allowed.label = NULL_TREE;
579 break;
581 case ERT_MUST_NOT_THROW:
582 new_r->u.must_not_throw.failure_loc =
583 LOCATION_LOCUS (old_r->u.must_not_throw.failure_loc);
584 new_r->u.must_not_throw.failure_decl =
585 old_r->u.must_not_throw.failure_decl;
586 break;
589 for (old_lp = old_r->landing_pads; old_lp ; old_lp = old_lp->next_lp)
591 /* Don't bother copying unused landing pads. */
592 if (old_lp->post_landing_pad == NULL)
593 continue;
595 new_lp = gen_eh_landing_pad (new_r);
596 gcc_assert (!data->eh_map->put (old_lp, new_lp));
598 new_lp->post_landing_pad
599 = data->label_map (old_lp->post_landing_pad, data->label_map_data);
600 EH_LANDING_PAD_NR (new_lp->post_landing_pad) = new_lp->index;
603 /* Make sure to preserve the original use of __cxa_end_cleanup. */
604 new_r->use_cxa_end_cleanup = old_r->use_cxa_end_cleanup;
606 for (old_r = old_r->inner; old_r ; old_r = old_r->next_peer)
607 duplicate_eh_regions_1 (data, old_r, new_r);
610 /* Duplicate the EH regions from IFUN rooted at COPY_REGION into
611 the current function and root the tree below OUTER_REGION.
612 The special case of COPY_REGION of NULL means all regions.
613 Remap labels using MAP/MAP_DATA callback. Return a pointer map
614 that allows the caller to remap uses of both EH regions and
615 EH landing pads. */
617 hash_map<void *, void *> *
618 duplicate_eh_regions (struct function *ifun,
619 eh_region copy_region, int outer_lp,
620 duplicate_eh_regions_map map, void *map_data)
622 struct duplicate_eh_regions_data data;
623 eh_region outer_region;
625 #ifdef ENABLE_CHECKING
626 verify_eh_tree (ifun);
627 #endif
629 data.label_map = map;
630 data.label_map_data = map_data;
631 data.eh_map = new hash_map<void *, void *>;
633 outer_region = get_eh_region_from_lp_number_fn (cfun, outer_lp);
635 /* Copy all the regions in the subtree. */
636 if (copy_region)
637 duplicate_eh_regions_1 (&data, copy_region, outer_region);
638 else
640 eh_region r;
641 for (r = ifun->eh->region_tree; r ; r = r->next_peer)
642 duplicate_eh_regions_1 (&data, r, outer_region);
645 #ifdef ENABLE_CHECKING
646 verify_eh_tree (cfun);
647 #endif
649 return data.eh_map;
652 /* Return the region that is outer to both REGION_A and REGION_B in IFUN. */
654 eh_region
655 eh_region_outermost (struct function *ifun, eh_region region_a,
656 eh_region region_b)
658 sbitmap b_outer;
660 gcc_assert (ifun->eh->region_array);
661 gcc_assert (ifun->eh->region_tree);
663 b_outer = sbitmap_alloc (ifun->eh->region_array->length ());
664 bitmap_clear (b_outer);
668 bitmap_set_bit (b_outer, region_b->index);
669 region_b = region_b->outer;
671 while (region_b);
675 if (bitmap_bit_p (b_outer, region_a->index))
676 break;
677 region_a = region_a->outer;
679 while (region_a);
681 sbitmap_free (b_outer);
682 return region_a;
685 void
686 add_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;
692 bool existed = false;
693 tree *slot = &type_to_runtime_map->get_or_insert (type, &existed);
694 if (!existed)
695 *slot = lang_hooks.eh_runtime_type (type);
698 tree
699 lookup_type_for_runtime (tree type)
701 /* If TYPE is NOP_EXPR, it means that it already is a runtime type. */
702 if (TREE_CODE (type) == NOP_EXPR)
703 return type;
705 /* We should have always inserted the data earlier. */
706 return *type_to_runtime_map->get (type);
710 /* Represent an entry in @TTypes for either catch actions
711 or exception filter actions. */
712 struct ttypes_filter {
713 tree t;
714 int filter;
717 /* Helper for ttypes_filter hashing. */
719 struct ttypes_filter_hasher : free_ptr_hash <ttypes_filter>
721 typedef tree_node *compare_type;
722 static inline hashval_t hash (const ttypes_filter *);
723 static inline bool equal (const ttypes_filter *, const tree_node *);
726 /* Compare ENTRY (a ttypes_filter entry in the hash table) with DATA
727 (a tree) for a @TTypes type node we are thinking about adding. */
729 inline bool
730 ttypes_filter_hasher::equal (const ttypes_filter *entry, const tree_node *data)
732 return entry->t == data;
735 inline hashval_t
736 ttypes_filter_hasher::hash (const ttypes_filter *entry)
738 return TREE_HASH (entry->t);
741 typedef hash_table<ttypes_filter_hasher> ttypes_hash_type;
744 /* Helper for ehspec hashing. */
746 struct ehspec_hasher : free_ptr_hash <ttypes_filter>
748 static inline hashval_t hash (const ttypes_filter *);
749 static inline bool equal (const ttypes_filter *, const ttypes_filter *);
752 /* Compare ENTRY with DATA (both struct ttypes_filter) for a @TTypes
753 exception specification list we are thinking about adding. */
754 /* ??? Currently we use the type lists in the order given. Someone
755 should put these in some canonical order. */
757 inline bool
758 ehspec_hasher::equal (const ttypes_filter *entry, const ttypes_filter *data)
760 return type_list_equal (entry->t, data->t);
763 /* Hash function for exception specification lists. */
765 inline hashval_t
766 ehspec_hasher::hash (const ttypes_filter *entry)
768 hashval_t h = 0;
769 tree list;
771 for (list = entry->t; list ; list = TREE_CHAIN (list))
772 h = (h << 5) + (h >> 27) + TREE_HASH (TREE_VALUE (list));
773 return h;
776 typedef hash_table<ehspec_hasher> ehspec_hash_type;
779 /* Add TYPE (which may be NULL) to cfun->eh->ttype_data, using TYPES_HASH
780 to speed up the search. Return the filter value to be used. */
782 static int
783 add_ttypes_entry (ttypes_hash_type *ttypes_hash, tree type)
785 struct ttypes_filter **slot, *n;
787 slot = ttypes_hash->find_slot_with_hash (type, (hashval_t) TREE_HASH (type),
788 INSERT);
790 if ((n = *slot) == NULL)
792 /* Filter value is a 1 based table index. */
794 n = XNEW (struct ttypes_filter);
795 n->t = type;
796 n->filter = vec_safe_length (cfun->eh->ttype_data) + 1;
797 *slot = n;
799 vec_safe_push (cfun->eh->ttype_data, type);
802 return n->filter;
805 /* Add LIST to cfun->eh->ehspec_data, using EHSPEC_HASH and TYPES_HASH
806 to speed up the search. Return the filter value to be used. */
808 static int
809 add_ehspec_entry (ehspec_hash_type *ehspec_hash, ttypes_hash_type *ttypes_hash,
810 tree list)
812 struct ttypes_filter **slot, *n;
813 struct ttypes_filter dummy;
815 dummy.t = list;
816 slot = ehspec_hash->find_slot (&dummy, INSERT);
818 if ((n = *slot) == NULL)
820 int len;
822 if (targetm.arm_eabi_unwinder)
823 len = vec_safe_length (cfun->eh->ehspec_data.arm_eabi);
824 else
825 len = vec_safe_length (cfun->eh->ehspec_data.other);
827 /* Filter value is a -1 based byte index into a uleb128 buffer. */
829 n = XNEW (struct ttypes_filter);
830 n->t = list;
831 n->filter = -(len + 1);
832 *slot = n;
834 /* Generate a 0 terminated list of filter values. */
835 for (; list ; list = TREE_CHAIN (list))
837 if (targetm.arm_eabi_unwinder)
838 vec_safe_push (cfun->eh->ehspec_data.arm_eabi, TREE_VALUE (list));
839 else
841 /* Look up each type in the list and encode its filter
842 value as a uleb128. */
843 push_uleb128 (&cfun->eh->ehspec_data.other,
844 add_ttypes_entry (ttypes_hash, TREE_VALUE (list)));
847 if (targetm.arm_eabi_unwinder)
848 vec_safe_push (cfun->eh->ehspec_data.arm_eabi, NULL_TREE);
849 else
850 vec_safe_push (cfun->eh->ehspec_data.other, (uchar)0);
853 return n->filter;
856 /* Generate the action filter values to be used for CATCH and
857 ALLOWED_EXCEPTIONS regions. When using dwarf2 exception regions,
858 we use lots of landing pads, and so every type or list can share
859 the same filter value, which saves table space. */
861 void
862 assign_filter_values (void)
864 int i;
865 eh_region r;
866 eh_catch c;
868 vec_alloc (cfun->eh->ttype_data, 16);
869 if (targetm.arm_eabi_unwinder)
870 vec_alloc (cfun->eh->ehspec_data.arm_eabi, 64);
871 else
872 vec_alloc (cfun->eh->ehspec_data.other, 64);
874 ehspec_hash_type ehspec (31);
875 ttypes_hash_type ttypes (31);
877 for (i = 1; vec_safe_iterate (cfun->eh->region_array, i, &r); ++i)
879 if (r == NULL)
880 continue;
882 switch (r->type)
884 case ERT_TRY:
885 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
887 /* Whatever type_list is (NULL or true list), we build a list
888 of filters for the region. */
889 c->filter_list = NULL_TREE;
891 if (c->type_list != NULL)
893 /* Get a filter value for each of the types caught and store
894 them in the region's dedicated list. */
895 tree tp_node = c->type_list;
897 for ( ; tp_node; tp_node = TREE_CHAIN (tp_node))
899 int flt
900 = add_ttypes_entry (&ttypes, TREE_VALUE (tp_node));
901 tree flt_node = build_int_cst (integer_type_node, flt);
903 c->filter_list
904 = tree_cons (NULL_TREE, flt_node, c->filter_list);
907 else
909 /* Get a filter value for the NULL list also since it
910 will need an action record anyway. */
911 int flt = add_ttypes_entry (&ttypes, NULL);
912 tree flt_node = build_int_cst (integer_type_node, flt);
914 c->filter_list
915 = tree_cons (NULL_TREE, flt_node, NULL);
918 break;
920 case ERT_ALLOWED_EXCEPTIONS:
921 r->u.allowed.filter
922 = add_ehspec_entry (&ehspec, &ttypes, r->u.allowed.type_list);
923 break;
925 default:
926 break;
931 /* Emit SEQ into basic block just before INSN (that is assumed to be
932 first instruction of some existing BB and return the newly
933 produced block. */
934 static basic_block
935 emit_to_new_bb_before (rtx_insn *seq, rtx insn)
937 rtx_insn *last;
938 basic_block bb;
939 edge e;
940 edge_iterator ei;
942 /* If there happens to be a fallthru edge (possibly created by cleanup_cfg
943 call), we don't want it to go into newly created landing pad or other EH
944 construct. */
945 for (ei = ei_start (BLOCK_FOR_INSN (insn)->preds); (e = ei_safe_edge (ei)); )
946 if (e->flags & EDGE_FALLTHRU)
947 force_nonfallthru (e);
948 else
949 ei_next (&ei);
950 last = emit_insn_before (seq, insn);
951 if (BARRIER_P (last))
952 last = PREV_INSN (last);
953 bb = create_basic_block (seq, last, BLOCK_FOR_INSN (insn)->prev_bb);
954 update_bb_for_insn (bb);
955 bb->flags |= BB_SUPERBLOCK;
956 return bb;
959 /* A subroutine of dw2_build_landing_pads, also used for edge splitting
960 at the rtl level. Emit the code required by the target at a landing
961 pad for the given region. */
963 void
964 expand_dw2_landing_pad_for_region (eh_region region)
966 #ifdef HAVE_exception_receiver
967 if (HAVE_exception_receiver)
968 emit_insn (gen_exception_receiver ());
969 else
970 #endif
971 #ifdef HAVE_nonlocal_goto_receiver
972 if (HAVE_nonlocal_goto_receiver)
973 emit_insn (gen_nonlocal_goto_receiver ());
974 else
975 #endif
976 { /* Nothing */ }
978 if (region->exc_ptr_reg)
979 emit_move_insn (region->exc_ptr_reg,
980 gen_rtx_REG (ptr_mode, EH_RETURN_DATA_REGNO (0)));
981 if (region->filter_reg)
982 emit_move_insn (region->filter_reg,
983 gen_rtx_REG (targetm.eh_return_filter_mode (),
984 EH_RETURN_DATA_REGNO (1)));
987 /* Expand the extra code needed at landing pads for dwarf2 unwinding. */
989 static void
990 dw2_build_landing_pads (void)
992 int i;
993 eh_landing_pad lp;
994 int e_flags = EDGE_FALLTHRU;
996 /* If we're going to partition blocks, we need to be able to add
997 new landing pads later, which means that we need to hold on to
998 the post-landing-pad block. Prevent it from being merged away.
999 We'll remove this bit after partitioning. */
1000 if (flag_reorder_blocks_and_partition)
1001 e_flags |= EDGE_PRESERVE;
1003 for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
1005 basic_block bb;
1006 rtx_insn *seq;
1007 edge e;
1009 if (lp == NULL || lp->post_landing_pad == NULL)
1010 continue;
1012 start_sequence ();
1014 lp->landing_pad = gen_label_rtx ();
1015 emit_label (lp->landing_pad);
1016 LABEL_PRESERVE_P (lp->landing_pad) = 1;
1018 expand_dw2_landing_pad_for_region (lp->region);
1020 seq = get_insns ();
1021 end_sequence ();
1023 bb = emit_to_new_bb_before (seq, label_rtx (lp->post_landing_pad));
1024 e = make_edge (bb, bb->next_bb, e_flags);
1025 e->count = bb->count;
1026 e->probability = REG_BR_PROB_BASE;
1027 if (current_loops)
1029 struct loop *loop = bb->next_bb->loop_father;
1030 /* If we created a pre-header block, add the new block to the
1031 outer loop, otherwise to the loop itself. */
1032 if (bb->next_bb == loop->header)
1033 add_bb_to_loop (bb, loop_outer (loop));
1034 else
1035 add_bb_to_loop (bb, loop);
1041 static vec<int> sjlj_lp_call_site_index;
1043 /* Process all active landing pads. Assign each one a compact dispatch
1044 index, and a call-site index. */
1046 static int
1047 sjlj_assign_call_site_values (void)
1049 action_hash_type ar_hash (31);
1050 int i, disp_index;
1051 eh_landing_pad lp;
1053 vec_alloc (crtl->eh.action_record_data, 64);
1055 disp_index = 0;
1056 call_site_base = 1;
1057 for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
1058 if (lp && lp->post_landing_pad)
1060 int action, call_site;
1062 /* First: build the action table. */
1063 action = collect_one_action_chain (&ar_hash, lp->region);
1065 /* Next: assign call-site values. If dwarf2 terms, this would be
1066 the region number assigned by convert_to_eh_region_ranges, but
1067 handles no-action and must-not-throw differently. */
1068 /* Map must-not-throw to otherwise unused call-site index 0. */
1069 if (action == -2)
1070 call_site = 0;
1071 /* Map no-action to otherwise unused call-site index -1. */
1072 else if (action == -1)
1073 call_site = -1;
1074 /* Otherwise, look it up in the table. */
1075 else
1076 call_site = add_call_site (GEN_INT (disp_index), action, 0);
1077 sjlj_lp_call_site_index[i] = call_site;
1079 disp_index++;
1082 return disp_index;
1085 /* Emit code to record the current call-site index before every
1086 insn that can throw. */
1088 static void
1089 sjlj_mark_call_sites (void)
1091 int last_call_site = -2;
1092 rtx_insn *insn;
1093 rtx mem;
1095 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
1097 eh_landing_pad lp;
1098 eh_region r;
1099 bool nothrow;
1100 int this_call_site;
1101 rtx_insn *before, *p;
1103 /* Reset value tracking at extended basic block boundaries. */
1104 if (LABEL_P (insn))
1105 last_call_site = -2;
1107 /* If the function allocates dynamic stack space, the context must
1108 be updated after every allocation/deallocation accordingly. */
1109 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_UPDATE_SJLJ_CONTEXT)
1111 rtx buf_addr;
1113 start_sequence ();
1114 buf_addr = plus_constant (Pmode, XEXP (crtl->eh.sjlj_fc, 0),
1115 sjlj_fc_jbuf_ofs);
1116 expand_builtin_update_setjmp_buf (buf_addr);
1117 p = get_insns ();
1118 end_sequence ();
1119 emit_insn_before (p, insn);
1122 if (! INSN_P (insn))
1123 continue;
1125 nothrow = get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1126 if (nothrow)
1127 continue;
1128 if (lp)
1129 this_call_site = sjlj_lp_call_site_index[lp->index];
1130 else if (r == NULL)
1132 /* Calls (and trapping insns) without notes are outside any
1133 exception handling region in this function. Mark them as
1134 no action. */
1135 this_call_site = -1;
1137 else
1139 gcc_assert (r->type == ERT_MUST_NOT_THROW);
1140 this_call_site = 0;
1143 if (this_call_site != -1)
1144 crtl->uses_eh_lsda = 1;
1146 if (this_call_site == last_call_site)
1147 continue;
1149 /* Don't separate a call from it's argument loads. */
1150 before = insn;
1151 if (CALL_P (insn))
1152 before = find_first_parameter_load (insn, NULL);
1154 start_sequence ();
1155 mem = adjust_address (crtl->eh.sjlj_fc, TYPE_MODE (integer_type_node),
1156 sjlj_fc_call_site_ofs);
1157 emit_move_insn (mem, gen_int_mode (this_call_site, GET_MODE (mem)));
1158 p = get_insns ();
1159 end_sequence ();
1161 emit_insn_before (p, before);
1162 last_call_site = this_call_site;
1166 /* Construct the SjLj_Function_Context. */
1168 static void
1169 sjlj_emit_function_enter (rtx_code_label *dispatch_label)
1171 rtx_insn *fn_begin, *seq;
1172 rtx fc, mem;
1173 bool fn_begin_outside_block;
1174 rtx personality = get_personality_function (current_function_decl);
1176 fc = crtl->eh.sjlj_fc;
1178 start_sequence ();
1180 /* We're storing this libcall's address into memory instead of
1181 calling it directly. Thus, we must call assemble_external_libcall
1182 here, as we can not depend on emit_library_call to do it for us. */
1183 assemble_external_libcall (personality);
1184 mem = adjust_address (fc, Pmode, sjlj_fc_personality_ofs);
1185 emit_move_insn (mem, personality);
1187 mem = adjust_address (fc, Pmode, sjlj_fc_lsda_ofs);
1188 if (crtl->uses_eh_lsda)
1190 char buf[20];
1191 rtx sym;
1193 ASM_GENERATE_INTERNAL_LABEL (buf, "LLSDA", current_function_funcdef_no);
1194 sym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
1195 SYMBOL_REF_FLAGS (sym) = SYMBOL_FLAG_LOCAL;
1196 emit_move_insn (mem, sym);
1198 else
1199 emit_move_insn (mem, const0_rtx);
1201 if (dispatch_label)
1203 #ifdef DONT_USE_BUILTIN_SETJMP
1204 rtx x;
1205 x = emit_library_call_value (setjmp_libfunc, NULL_RTX, LCT_RETURNS_TWICE,
1206 TYPE_MODE (integer_type_node), 1,
1207 plus_constant (Pmode, XEXP (fc, 0),
1208 sjlj_fc_jbuf_ofs), Pmode);
1210 emit_cmp_and_jump_insns (x, const0_rtx, NE, 0,
1211 TYPE_MODE (integer_type_node), 0,
1212 dispatch_label, REG_BR_PROB_BASE / 100);
1213 #else
1214 expand_builtin_setjmp_setup (plus_constant (Pmode, XEXP (fc, 0),
1215 sjlj_fc_jbuf_ofs),
1216 dispatch_label);
1217 #endif
1220 emit_library_call (unwind_sjlj_register_libfunc, LCT_NORMAL, VOIDmode,
1221 1, XEXP (fc, 0), Pmode);
1223 seq = get_insns ();
1224 end_sequence ();
1226 /* ??? Instead of doing this at the beginning of the function,
1227 do this in a block that is at loop level 0 and dominates all
1228 can_throw_internal instructions. */
1230 fn_begin_outside_block = true;
1231 for (fn_begin = get_insns (); ; fn_begin = NEXT_INSN (fn_begin))
1232 if (NOTE_P (fn_begin))
1234 if (NOTE_KIND (fn_begin) == NOTE_INSN_FUNCTION_BEG)
1235 break;
1236 else if (NOTE_INSN_BASIC_BLOCK_P (fn_begin))
1237 fn_begin_outside_block = false;
1240 if (fn_begin_outside_block)
1241 insert_insn_on_edge (seq, single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1242 else
1243 emit_insn_after (seq, fn_begin);
1246 /* Call back from expand_function_end to know where we should put
1247 the call to unwind_sjlj_unregister_libfunc if needed. */
1249 void
1250 sjlj_emit_function_exit_after (rtx_insn *after)
1252 crtl->eh.sjlj_exit_after = after;
1255 static void
1256 sjlj_emit_function_exit (void)
1258 rtx_insn *seq, *insn;
1260 start_sequence ();
1262 emit_library_call (unwind_sjlj_unregister_libfunc, LCT_NORMAL, VOIDmode,
1263 1, XEXP (crtl->eh.sjlj_fc, 0), Pmode);
1265 seq = get_insns ();
1266 end_sequence ();
1268 /* ??? Really this can be done in any block at loop level 0 that
1269 post-dominates all can_throw_internal instructions. This is
1270 the last possible moment. */
1272 insn = crtl->eh.sjlj_exit_after;
1273 if (LABEL_P (insn))
1274 insn = NEXT_INSN (insn);
1276 emit_insn_after (seq, insn);
1279 static void
1280 sjlj_emit_dispatch_table (rtx_code_label *dispatch_label, int num_dispatch)
1282 machine_mode unwind_word_mode = targetm.unwind_word_mode ();
1283 machine_mode filter_mode = targetm.eh_return_filter_mode ();
1284 eh_landing_pad lp;
1285 rtx mem, fc, exc_ptr_reg, filter_reg;
1286 rtx_insn *seq;
1287 basic_block bb;
1288 eh_region r;
1289 edge e;
1290 int i, disp_index;
1291 vec<tree> dispatch_labels = vNULL;
1293 fc = crtl->eh.sjlj_fc;
1295 start_sequence ();
1297 emit_label (dispatch_label);
1299 #ifndef DONT_USE_BUILTIN_SETJMP
1300 expand_builtin_setjmp_receiver (dispatch_label);
1302 /* The caller of expand_builtin_setjmp_receiver is responsible for
1303 making sure that the label doesn't vanish. The only other caller
1304 is the expander for __builtin_setjmp_receiver, which places this
1305 label on the nonlocal_goto_label list. Since we're modeling these
1306 CFG edges more exactly, we can use the forced_labels list instead. */
1307 LABEL_PRESERVE_P (dispatch_label) = 1;
1308 forced_labels
1309 = gen_rtx_INSN_LIST (VOIDmode, dispatch_label, forced_labels);
1310 #endif
1312 /* Load up exc_ptr and filter values from the function context. */
1313 mem = adjust_address (fc, unwind_word_mode, sjlj_fc_data_ofs);
1314 if (unwind_word_mode != ptr_mode)
1316 #ifdef POINTERS_EXTEND_UNSIGNED
1317 mem = convert_memory_address (ptr_mode, mem);
1318 #else
1319 mem = convert_to_mode (ptr_mode, mem, 0);
1320 #endif
1322 exc_ptr_reg = force_reg (ptr_mode, mem);
1324 mem = adjust_address (fc, unwind_word_mode,
1325 sjlj_fc_data_ofs + GET_MODE_SIZE (unwind_word_mode));
1326 if (unwind_word_mode != filter_mode)
1327 mem = convert_to_mode (filter_mode, mem, 0);
1328 filter_reg = force_reg (filter_mode, mem);
1330 /* Jump to one of the directly reachable regions. */
1332 disp_index = 0;
1333 rtx_code_label *first_reachable_label = NULL;
1335 /* If there's exactly one call site in the function, don't bother
1336 generating a switch statement. */
1337 if (num_dispatch > 1)
1338 dispatch_labels.create (num_dispatch);
1340 for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
1341 if (lp && lp->post_landing_pad)
1343 rtx_insn *seq2;
1344 rtx_code_label *label;
1346 start_sequence ();
1348 lp->landing_pad = dispatch_label;
1350 if (num_dispatch > 1)
1352 tree t_label, case_elt, t;
1354 t_label = create_artificial_label (UNKNOWN_LOCATION);
1355 t = build_int_cst (integer_type_node, disp_index);
1356 case_elt = build_case_label (t, NULL, t_label);
1357 dispatch_labels.quick_push (case_elt);
1358 label = jump_target_rtx (t_label);
1360 else
1361 label = gen_label_rtx ();
1363 if (disp_index == 0)
1364 first_reachable_label = label;
1365 emit_label (label);
1367 r = lp->region;
1368 if (r->exc_ptr_reg)
1369 emit_move_insn (r->exc_ptr_reg, exc_ptr_reg);
1370 if (r->filter_reg)
1371 emit_move_insn (r->filter_reg, filter_reg);
1373 seq2 = get_insns ();
1374 end_sequence ();
1376 rtx_insn *before = label_rtx (lp->post_landing_pad);
1377 bb = emit_to_new_bb_before (seq2, before);
1378 e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
1379 e->count = bb->count;
1380 e->probability = REG_BR_PROB_BASE;
1381 if (current_loops)
1383 struct loop *loop = bb->next_bb->loop_father;
1384 /* If we created a pre-header block, add the new block to the
1385 outer loop, otherwise to the loop itself. */
1386 if (bb->next_bb == loop->header)
1387 add_bb_to_loop (bb, loop_outer (loop));
1388 else
1389 add_bb_to_loop (bb, loop);
1390 /* ??? For multiple dispatches we will end up with edges
1391 from the loop tree root into this loop, making it a
1392 multiple-entry loop. Discard all affected loops. */
1393 if (num_dispatch > 1)
1395 for (loop = bb->loop_father;
1396 loop_outer (loop); loop = loop_outer (loop))
1397 mark_loop_for_removal (loop);
1401 disp_index++;
1403 gcc_assert (disp_index == num_dispatch);
1405 if (num_dispatch > 1)
1407 rtx disp = adjust_address (fc, TYPE_MODE (integer_type_node),
1408 sjlj_fc_call_site_ofs);
1409 expand_sjlj_dispatch_table (disp, dispatch_labels);
1412 seq = get_insns ();
1413 end_sequence ();
1415 bb = emit_to_new_bb_before (seq, first_reachable_label);
1416 if (num_dispatch == 1)
1418 e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
1419 e->count = bb->count;
1420 e->probability = REG_BR_PROB_BASE;
1421 if (current_loops)
1423 struct loop *loop = bb->next_bb->loop_father;
1424 /* If we created a pre-header block, add the new block to the
1425 outer loop, otherwise to the loop itself. */
1426 if (bb->next_bb == loop->header)
1427 add_bb_to_loop (bb, loop_outer (loop));
1428 else
1429 add_bb_to_loop (bb, loop);
1432 else
1434 /* We are not wiring up edges here, but as the dispatcher call
1435 is at function begin simply associate the block with the
1436 outermost (non-)loop. */
1437 if (current_loops)
1438 add_bb_to_loop (bb, current_loops->tree_root);
1442 static void
1443 sjlj_build_landing_pads (void)
1445 int num_dispatch;
1447 num_dispatch = vec_safe_length (cfun->eh->lp_array);
1448 if (num_dispatch == 0)
1449 return;
1450 sjlj_lp_call_site_index.safe_grow_cleared (num_dispatch);
1452 num_dispatch = sjlj_assign_call_site_values ();
1453 if (num_dispatch > 0)
1455 rtx_code_label *dispatch_label = gen_label_rtx ();
1456 int align = STACK_SLOT_ALIGNMENT (sjlj_fc_type_node,
1457 TYPE_MODE (sjlj_fc_type_node),
1458 TYPE_ALIGN (sjlj_fc_type_node));
1459 crtl->eh.sjlj_fc
1460 = assign_stack_local (TYPE_MODE (sjlj_fc_type_node),
1461 int_size_in_bytes (sjlj_fc_type_node),
1462 align);
1464 sjlj_mark_call_sites ();
1465 sjlj_emit_function_enter (dispatch_label);
1466 sjlj_emit_dispatch_table (dispatch_label, num_dispatch);
1467 sjlj_emit_function_exit ();
1470 /* If we do not have any landing pads, we may still need to register a
1471 personality routine and (empty) LSDA to handle must-not-throw regions. */
1472 else if (function_needs_eh_personality (cfun) != eh_personality_none)
1474 int align = STACK_SLOT_ALIGNMENT (sjlj_fc_type_node,
1475 TYPE_MODE (sjlj_fc_type_node),
1476 TYPE_ALIGN (sjlj_fc_type_node));
1477 crtl->eh.sjlj_fc
1478 = assign_stack_local (TYPE_MODE (sjlj_fc_type_node),
1479 int_size_in_bytes (sjlj_fc_type_node),
1480 align);
1482 sjlj_mark_call_sites ();
1483 sjlj_emit_function_enter (NULL);
1484 sjlj_emit_function_exit ();
1487 sjlj_lp_call_site_index.release ();
1490 /* Update the sjlj function context. This function should be called
1491 whenever we allocate or deallocate dynamic stack space. */
1493 void
1494 update_sjlj_context (void)
1496 if (!flag_exceptions)
1497 return;
1499 emit_note (NOTE_INSN_UPDATE_SJLJ_CONTEXT);
1502 /* After initial rtl generation, call back to finish generating
1503 exception support code. */
1505 void
1506 finish_eh_generation (void)
1508 basic_block bb;
1510 /* Construct the landing pads. */
1511 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
1512 sjlj_build_landing_pads ();
1513 else
1514 dw2_build_landing_pads ();
1515 break_superblocks ();
1517 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ
1518 /* Kludge for Alpha (see alpha_gp_save_rtx). */
1519 || single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun))->insns.r)
1520 commit_edge_insertions ();
1522 /* Redirect all EH edges from the post_landing_pad to the landing pad. */
1523 FOR_EACH_BB_FN (bb, cfun)
1525 eh_landing_pad lp;
1526 edge_iterator ei;
1527 edge e;
1529 lp = get_eh_landing_pad_from_rtx (BB_END (bb));
1531 FOR_EACH_EDGE (e, ei, bb->succs)
1532 if (e->flags & EDGE_EH)
1533 break;
1535 /* We should not have generated any new throwing insns during this
1536 pass, and we should not have lost any EH edges, so we only need
1537 to handle two cases here:
1538 (1) reachable handler and an existing edge to post-landing-pad,
1539 (2) no reachable handler and no edge. */
1540 gcc_assert ((lp != NULL) == (e != NULL));
1541 if (lp != NULL)
1543 gcc_assert (BB_HEAD (e->dest) == label_rtx (lp->post_landing_pad));
1545 redirect_edge_succ (e, BLOCK_FOR_INSN (lp->landing_pad));
1546 e->flags |= (CALL_P (BB_END (bb))
1547 ? EDGE_ABNORMAL | EDGE_ABNORMAL_CALL
1548 : EDGE_ABNORMAL);
1553 /* This section handles removing dead code for flow. */
1555 void
1556 remove_eh_landing_pad (eh_landing_pad lp)
1558 eh_landing_pad *pp;
1560 for (pp = &lp->region->landing_pads; *pp != lp; pp = &(*pp)->next_lp)
1561 continue;
1562 *pp = lp->next_lp;
1564 if (lp->post_landing_pad)
1565 EH_LANDING_PAD_NR (lp->post_landing_pad) = 0;
1566 (*cfun->eh->lp_array)[lp->index] = NULL;
1569 /* Splice the EH region at PP from the region tree. */
1571 static void
1572 remove_eh_handler_splicer (eh_region *pp)
1574 eh_region region = *pp;
1575 eh_landing_pad lp;
1577 for (lp = region->landing_pads; lp ; lp = lp->next_lp)
1579 if (lp->post_landing_pad)
1580 EH_LANDING_PAD_NR (lp->post_landing_pad) = 0;
1581 (*cfun->eh->lp_array)[lp->index] = NULL;
1584 if (region->inner)
1586 eh_region p, outer;
1587 outer = region->outer;
1589 *pp = p = region->inner;
1592 p->outer = outer;
1593 pp = &p->next_peer;
1594 p = *pp;
1596 while (p);
1598 *pp = region->next_peer;
1600 (*cfun->eh->region_array)[region->index] = NULL;
1603 /* Splice a single EH region REGION from the region tree.
1605 To unlink REGION, we need to find the pointer to it with a relatively
1606 expensive search in REGION's outer region. If you are going to
1607 remove a number of handlers, using remove_unreachable_eh_regions may
1608 be a better option. */
1610 void
1611 remove_eh_handler (eh_region region)
1613 eh_region *pp, *pp_start, p, outer;
1615 outer = region->outer;
1616 if (outer)
1617 pp_start = &outer->inner;
1618 else
1619 pp_start = &cfun->eh->region_tree;
1620 for (pp = pp_start, p = *pp; p != region; pp = &p->next_peer, p = *pp)
1621 continue;
1623 remove_eh_handler_splicer (pp);
1626 /* Worker for remove_unreachable_eh_regions.
1627 PP is a pointer to the region to start a region tree depth-first
1628 search from. R_REACHABLE is the set of regions that have to be
1629 preserved. */
1631 static void
1632 remove_unreachable_eh_regions_worker (eh_region *pp, sbitmap r_reachable)
1634 while (*pp)
1636 eh_region region = *pp;
1637 remove_unreachable_eh_regions_worker (&region->inner, r_reachable);
1638 if (!bitmap_bit_p (r_reachable, region->index))
1639 remove_eh_handler_splicer (pp);
1640 else
1641 pp = &region->next_peer;
1645 /* Splice all EH regions *not* marked in R_REACHABLE from the region tree.
1646 Do this by traversing the EH tree top-down and splice out regions that
1647 are not marked. By removing regions from the leaves, we avoid costly
1648 searches in the region tree. */
1650 void
1651 remove_unreachable_eh_regions (sbitmap r_reachable)
1653 remove_unreachable_eh_regions_worker (&cfun->eh->region_tree, r_reachable);
1656 /* Invokes CALLBACK for every exception handler landing pad label.
1657 Only used by reload hackery; should not be used by new code. */
1659 void
1660 for_each_eh_label (void (*callback) (rtx))
1662 eh_landing_pad lp;
1663 int i;
1665 for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
1667 if (lp)
1669 rtx_code_label *lab = lp->landing_pad;
1670 if (lab && LABEL_P (lab))
1671 (*callback) (lab);
1676 /* Create the REG_EH_REGION note for INSN, given its ECF_FLAGS for a
1677 call insn.
1679 At the gimple level, we use LP_NR
1680 > 0 : The statement transfers to landing pad LP_NR
1681 = 0 : The statement is outside any EH region
1682 < 0 : The statement is within MUST_NOT_THROW region -LP_NR.
1684 At the rtl level, we use LP_NR
1685 > 0 : The insn transfers to landing pad LP_NR
1686 = 0 : The insn cannot throw
1687 < 0 : The insn is within MUST_NOT_THROW region -LP_NR
1688 = INT_MIN : The insn cannot throw or execute a nonlocal-goto.
1689 missing note: The insn is outside any EH region.
1691 ??? This difference probably ought to be avoided. We could stand
1692 to record nothrow for arbitrary gimple statements, and so avoid
1693 some moderately complex lookups in stmt_could_throw_p. Perhaps
1694 NOTHROW should be mapped on both sides to INT_MIN. Perhaps the
1695 no-nonlocal-goto property should be recorded elsewhere as a bit
1696 on the call_insn directly. Perhaps we should make more use of
1697 attaching the trees to call_insns (reachable via symbol_ref in
1698 direct call cases) and just pull the data out of the trees. */
1700 void
1701 make_reg_eh_region_note (rtx_insn *insn, int ecf_flags, int lp_nr)
1703 rtx value;
1704 if (ecf_flags & ECF_NOTHROW)
1705 value = const0_rtx;
1706 else if (lp_nr != 0)
1707 value = GEN_INT (lp_nr);
1708 else
1709 return;
1710 add_reg_note (insn, REG_EH_REGION, value);
1713 /* Create a REG_EH_REGION note for a CALL_INSN that cannot throw
1714 nor perform a non-local goto. Replace the region note if it
1715 already exists. */
1717 void
1718 make_reg_eh_region_note_nothrow_nononlocal (rtx_insn *insn)
1720 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1721 rtx intmin = GEN_INT (INT_MIN);
1723 if (note != 0)
1724 XEXP (note, 0) = intmin;
1725 else
1726 add_reg_note (insn, REG_EH_REGION, intmin);
1729 /* Return true if INSN could throw, assuming no REG_EH_REGION note
1730 to the contrary. */
1732 bool
1733 insn_could_throw_p (const_rtx insn)
1735 if (!flag_exceptions)
1736 return false;
1737 if (CALL_P (insn))
1738 return true;
1739 if (INSN_P (insn) && cfun->can_throw_non_call_exceptions)
1740 return may_trap_p (PATTERN (insn));
1741 return false;
1744 /* Copy an REG_EH_REGION note to each insn that might throw beginning
1745 at FIRST and ending at LAST. NOTE_OR_INSN is either the source insn
1746 to look for a note, or the note itself. */
1748 void
1749 copy_reg_eh_region_note_forward (rtx note_or_insn, rtx_insn *first, rtx last)
1751 rtx_insn *insn;
1752 rtx note = note_or_insn;
1754 if (INSN_P (note_or_insn))
1756 note = find_reg_note (note_or_insn, REG_EH_REGION, NULL_RTX);
1757 if (note == NULL)
1758 return;
1760 note = XEXP (note, 0);
1762 for (insn = first; insn != last ; insn = NEXT_INSN (insn))
1763 if (!find_reg_note (insn, REG_EH_REGION, NULL_RTX)
1764 && insn_could_throw_p (insn))
1765 add_reg_note (insn, REG_EH_REGION, note);
1768 /* Likewise, but iterate backward. */
1770 void
1771 copy_reg_eh_region_note_backward (rtx note_or_insn, rtx_insn *last, rtx first)
1773 rtx_insn *insn;
1774 rtx note = note_or_insn;
1776 if (INSN_P (note_or_insn))
1778 note = find_reg_note (note_or_insn, REG_EH_REGION, NULL_RTX);
1779 if (note == NULL)
1780 return;
1782 note = XEXP (note, 0);
1784 for (insn = last; insn != first; insn = PREV_INSN (insn))
1785 if (insn_could_throw_p (insn))
1786 add_reg_note (insn, REG_EH_REGION, note);
1790 /* Extract all EH information from INSN. Return true if the insn
1791 was marked NOTHROW. */
1793 static bool
1794 get_eh_region_and_lp_from_rtx (const_rtx insn, eh_region *pr,
1795 eh_landing_pad *plp)
1797 eh_landing_pad lp = NULL;
1798 eh_region r = NULL;
1799 bool ret = false;
1800 rtx note;
1801 int lp_nr;
1803 if (! INSN_P (insn))
1804 goto egress;
1806 if (NONJUMP_INSN_P (insn)
1807 && GET_CODE (PATTERN (insn)) == SEQUENCE)
1808 insn = XVECEXP (PATTERN (insn), 0, 0);
1810 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1811 if (!note)
1813 ret = !insn_could_throw_p (insn);
1814 goto egress;
1817 lp_nr = INTVAL (XEXP (note, 0));
1818 if (lp_nr == 0 || lp_nr == INT_MIN)
1820 ret = true;
1821 goto egress;
1824 if (lp_nr < 0)
1825 r = (*cfun->eh->region_array)[-lp_nr];
1826 else
1828 lp = (*cfun->eh->lp_array)[lp_nr];
1829 r = lp->region;
1832 egress:
1833 *plp = lp;
1834 *pr = r;
1835 return ret;
1838 /* Return the landing pad to which INSN may go, or NULL if it does not
1839 have a reachable landing pad within this function. */
1841 eh_landing_pad
1842 get_eh_landing_pad_from_rtx (const_rtx insn)
1844 eh_landing_pad lp;
1845 eh_region r;
1847 get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1848 return lp;
1851 /* Return the region to which INSN may go, or NULL if it does not
1852 have a reachable region within this function. */
1854 eh_region
1855 get_eh_region_from_rtx (const_rtx insn)
1857 eh_landing_pad lp;
1858 eh_region r;
1860 get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1861 return r;
1864 /* Return true if INSN throws and is caught by something in this function. */
1866 bool
1867 can_throw_internal (const_rtx insn)
1869 return get_eh_landing_pad_from_rtx (insn) != NULL;
1872 /* Return true if INSN throws and escapes from the current function. */
1874 bool
1875 can_throw_external (const_rtx insn)
1877 eh_landing_pad lp;
1878 eh_region r;
1879 bool nothrow;
1881 if (! INSN_P (insn))
1882 return false;
1884 if (NONJUMP_INSN_P (insn)
1885 && GET_CODE (PATTERN (insn)) == SEQUENCE)
1887 rtx_sequence *seq = as_a <rtx_sequence *> (PATTERN (insn));
1888 int i, n = seq->len ();
1890 for (i = 0; i < n; i++)
1891 if (can_throw_external (seq->element (i)))
1892 return true;
1894 return false;
1897 nothrow = get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1899 /* If we can't throw, we obviously can't throw external. */
1900 if (nothrow)
1901 return false;
1903 /* If we have an internal landing pad, then we're not external. */
1904 if (lp != NULL)
1905 return false;
1907 /* If we're not within an EH region, then we are external. */
1908 if (r == NULL)
1909 return true;
1911 /* The only thing that ought to be left is MUST_NOT_THROW regions,
1912 which don't always have landing pads. */
1913 gcc_assert (r->type == ERT_MUST_NOT_THROW);
1914 return false;
1917 /* Return true if INSN cannot throw at all. */
1919 bool
1920 insn_nothrow_p (const_rtx insn)
1922 eh_landing_pad lp;
1923 eh_region r;
1925 if (! INSN_P (insn))
1926 return true;
1928 if (NONJUMP_INSN_P (insn)
1929 && GET_CODE (PATTERN (insn)) == SEQUENCE)
1931 rtx_sequence *seq = as_a <rtx_sequence *> (PATTERN (insn));
1932 int i, n = seq->len ();
1934 for (i = 0; i < n; i++)
1935 if (!insn_nothrow_p (seq->element (i)))
1936 return false;
1938 return true;
1941 return get_eh_region_and_lp_from_rtx (insn, &r, &lp);
1944 /* Return true if INSN can perform a non-local goto. */
1945 /* ??? This test is here in this file because it (ab)uses REG_EH_REGION. */
1947 bool
1948 can_nonlocal_goto (const rtx_insn *insn)
1950 if (nonlocal_goto_handler_labels && CALL_P (insn))
1952 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
1953 if (!note || INTVAL (XEXP (note, 0)) != INT_MIN)
1954 return true;
1956 return false;
1959 /* Set TREE_NOTHROW and crtl->all_throwers_are_sibcalls. */
1961 static unsigned int
1962 set_nothrow_function_flags (void)
1964 rtx_insn *insn;
1966 crtl->nothrow = 1;
1968 /* Assume crtl->all_throwers_are_sibcalls until we encounter
1969 something that can throw an exception. We specifically exempt
1970 CALL_INSNs that are SIBLING_CALL_P, as these are really jumps,
1971 and can't throw. Most CALL_INSNs are not SIBLING_CALL_P, so this
1972 is optimistic. */
1974 crtl->all_throwers_are_sibcalls = 1;
1976 /* If we don't know that this implementation of the function will
1977 actually be used, then we must not set TREE_NOTHROW, since
1978 callers must not assume that this function does not throw. */
1979 if (TREE_NOTHROW (current_function_decl))
1980 return 0;
1982 if (! flag_exceptions)
1983 return 0;
1985 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1986 if (can_throw_external (insn))
1988 crtl->nothrow = 0;
1990 if (!CALL_P (insn) || !SIBLING_CALL_P (insn))
1992 crtl->all_throwers_are_sibcalls = 0;
1993 return 0;
1997 if (crtl->nothrow
1998 && (cgraph_node::get (current_function_decl)->get_availability ()
1999 >= AVAIL_AVAILABLE))
2001 struct cgraph_node *node = cgraph_node::get (current_function_decl);
2002 struct cgraph_edge *e;
2003 for (e = node->callers; e; e = e->next_caller)
2004 e->can_throw_external = false;
2005 node->set_nothrow_flag (true);
2007 if (dump_file)
2008 fprintf (dump_file, "Marking function nothrow: %s\n\n",
2009 current_function_name ());
2011 return 0;
2014 namespace {
2016 const pass_data pass_data_set_nothrow_function_flags =
2018 RTL_PASS, /* type */
2019 "nothrow", /* name */
2020 OPTGROUP_NONE, /* optinfo_flags */
2021 TV_NONE, /* tv_id */
2022 0, /* properties_required */
2023 0, /* properties_provided */
2024 0, /* properties_destroyed */
2025 0, /* todo_flags_start */
2026 0, /* todo_flags_finish */
2029 class pass_set_nothrow_function_flags : public rtl_opt_pass
2031 public:
2032 pass_set_nothrow_function_flags (gcc::context *ctxt)
2033 : rtl_opt_pass (pass_data_set_nothrow_function_flags, ctxt)
2036 /* opt_pass methods: */
2037 virtual unsigned int execute (function *)
2039 return set_nothrow_function_flags ();
2042 }; // class pass_set_nothrow_function_flags
2044 } // anon namespace
2046 rtl_opt_pass *
2047 make_pass_set_nothrow_function_flags (gcc::context *ctxt)
2049 return new pass_set_nothrow_function_flags (ctxt);
2053 /* Various hooks for unwind library. */
2055 /* Expand the EH support builtin functions:
2056 __builtin_eh_pointer and __builtin_eh_filter. */
2058 static eh_region
2059 expand_builtin_eh_common (tree region_nr_t)
2061 HOST_WIDE_INT region_nr;
2062 eh_region region;
2064 gcc_assert (tree_fits_shwi_p (region_nr_t));
2065 region_nr = tree_to_shwi (region_nr_t);
2067 region = (*cfun->eh->region_array)[region_nr];
2069 /* ??? We shouldn't have been able to delete a eh region without
2070 deleting all the code that depended on it. */
2071 gcc_assert (region != NULL);
2073 return region;
2076 /* Expand to the exc_ptr value from the given eh region. */
2079 expand_builtin_eh_pointer (tree exp)
2081 eh_region region
2082 = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 0));
2083 if (region->exc_ptr_reg == NULL)
2084 region->exc_ptr_reg = gen_reg_rtx (ptr_mode);
2085 return region->exc_ptr_reg;
2088 /* Expand to the filter value from the given eh region. */
2091 expand_builtin_eh_filter (tree exp)
2093 eh_region region
2094 = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 0));
2095 if (region->filter_reg == NULL)
2096 region->filter_reg = gen_reg_rtx (targetm.eh_return_filter_mode ());
2097 return region->filter_reg;
2100 /* Copy the exc_ptr and filter values from one landing pad's registers
2101 to another. This is used to inline the resx statement. */
2104 expand_builtin_eh_copy_values (tree exp)
2106 eh_region dst
2107 = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 0));
2108 eh_region src
2109 = expand_builtin_eh_common (CALL_EXPR_ARG (exp, 1));
2110 machine_mode fmode = targetm.eh_return_filter_mode ();
2112 if (dst->exc_ptr_reg == NULL)
2113 dst->exc_ptr_reg = gen_reg_rtx (ptr_mode);
2114 if (src->exc_ptr_reg == NULL)
2115 src->exc_ptr_reg = gen_reg_rtx (ptr_mode);
2117 if (dst->filter_reg == NULL)
2118 dst->filter_reg = gen_reg_rtx (fmode);
2119 if (src->filter_reg == NULL)
2120 src->filter_reg = gen_reg_rtx (fmode);
2122 emit_move_insn (dst->exc_ptr_reg, src->exc_ptr_reg);
2123 emit_move_insn (dst->filter_reg, src->filter_reg);
2125 return const0_rtx;
2128 /* Do any necessary initialization to access arbitrary stack frames.
2129 On the SPARC, this means flushing the register windows. */
2131 void
2132 expand_builtin_unwind_init (void)
2134 /* Set this so all the registers get saved in our frame; we need to be
2135 able to copy the saved values for any registers from frames we unwind. */
2136 crtl->saves_all_registers = 1;
2138 #ifdef SETUP_FRAME_ADDRESSES
2139 SETUP_FRAME_ADDRESSES ();
2140 #endif
2143 /* Map a non-negative number to an eh return data register number; expands
2144 to -1 if no return data register is associated with the input number.
2145 At least the inputs 0 and 1 must be mapped; the target may provide more. */
2148 expand_builtin_eh_return_data_regno (tree exp)
2150 tree which = CALL_EXPR_ARG (exp, 0);
2151 unsigned HOST_WIDE_INT iwhich;
2153 if (TREE_CODE (which) != INTEGER_CST)
2155 error ("argument of %<__builtin_eh_return_regno%> must be constant");
2156 return constm1_rtx;
2159 iwhich = tree_to_uhwi (which);
2160 iwhich = EH_RETURN_DATA_REGNO (iwhich);
2161 if (iwhich == INVALID_REGNUM)
2162 return constm1_rtx;
2164 #ifdef DWARF_FRAME_REGNUM
2165 iwhich = DWARF_FRAME_REGNUM (iwhich);
2166 #else
2167 iwhich = DBX_REGISTER_NUMBER (iwhich);
2168 #endif
2170 return GEN_INT (iwhich);
2173 /* Given a value extracted from the return address register or stack slot,
2174 return the actual address encoded in that value. */
2177 expand_builtin_extract_return_addr (tree addr_tree)
2179 rtx addr = expand_expr (addr_tree, NULL_RTX, Pmode, EXPAND_NORMAL);
2181 if (GET_MODE (addr) != Pmode
2182 && GET_MODE (addr) != VOIDmode)
2184 #ifdef POINTERS_EXTEND_UNSIGNED
2185 addr = convert_memory_address (Pmode, addr);
2186 #else
2187 addr = convert_to_mode (Pmode, addr, 0);
2188 #endif
2191 /* First mask out any unwanted bits. */
2192 rtx mask = MASK_RETURN_ADDR;
2193 if (mask)
2194 expand_and (Pmode, addr, mask, addr);
2196 /* Then adjust to find the real return address. */
2197 if (RETURN_ADDR_OFFSET)
2198 addr = plus_constant (Pmode, addr, RETURN_ADDR_OFFSET);
2200 return addr;
2203 /* Given an actual address in addr_tree, do any necessary encoding
2204 and return the value to be stored in the return address register or
2205 stack slot so the epilogue will return to that address. */
2208 expand_builtin_frob_return_addr (tree addr_tree)
2210 rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, EXPAND_NORMAL);
2212 addr = convert_memory_address (Pmode, addr);
2214 if (RETURN_ADDR_OFFSET)
2216 addr = force_reg (Pmode, addr);
2217 addr = plus_constant (Pmode, addr, -RETURN_ADDR_OFFSET);
2220 return addr;
2223 /* Set up the epilogue with the magic bits we'll need to return to the
2224 exception handler. */
2226 void
2227 expand_builtin_eh_return (tree stackadj_tree ATTRIBUTE_UNUSED,
2228 tree handler_tree)
2230 rtx tmp;
2232 #ifdef EH_RETURN_STACKADJ_RTX
2233 tmp = expand_expr (stackadj_tree, crtl->eh.ehr_stackadj,
2234 VOIDmode, EXPAND_NORMAL);
2235 tmp = convert_memory_address (Pmode, tmp);
2236 if (!crtl->eh.ehr_stackadj)
2237 crtl->eh.ehr_stackadj = copy_to_reg (tmp);
2238 else if (tmp != crtl->eh.ehr_stackadj)
2239 emit_move_insn (crtl->eh.ehr_stackadj, tmp);
2240 #endif
2242 tmp = expand_expr (handler_tree, crtl->eh.ehr_handler,
2243 VOIDmode, EXPAND_NORMAL);
2244 tmp = convert_memory_address (Pmode, tmp);
2245 if (!crtl->eh.ehr_handler)
2246 crtl->eh.ehr_handler = copy_to_reg (tmp);
2247 else if (tmp != crtl->eh.ehr_handler)
2248 emit_move_insn (crtl->eh.ehr_handler, tmp);
2250 if (!crtl->eh.ehr_label)
2251 crtl->eh.ehr_label = gen_label_rtx ();
2252 emit_jump (crtl->eh.ehr_label);
2255 /* Expand __builtin_eh_return. This exit path from the function loads up
2256 the eh return data registers, adjusts the stack, and branches to a
2257 given PC other than the normal return address. */
2259 void
2260 expand_eh_return (void)
2262 rtx_code_label *around_label;
2264 if (! crtl->eh.ehr_label)
2265 return;
2267 crtl->calls_eh_return = 1;
2269 #ifdef EH_RETURN_STACKADJ_RTX
2270 emit_move_insn (EH_RETURN_STACKADJ_RTX, const0_rtx);
2271 #endif
2273 around_label = gen_label_rtx ();
2274 emit_jump (around_label);
2276 emit_label (crtl->eh.ehr_label);
2277 clobber_return_register ();
2279 #ifdef EH_RETURN_STACKADJ_RTX
2280 emit_move_insn (EH_RETURN_STACKADJ_RTX, crtl->eh.ehr_stackadj);
2281 #endif
2283 #ifdef HAVE_eh_return
2284 if (HAVE_eh_return)
2285 emit_insn (gen_eh_return (crtl->eh.ehr_handler));
2286 else
2287 #endif
2289 #ifdef EH_RETURN_HANDLER_RTX
2290 emit_move_insn (EH_RETURN_HANDLER_RTX, crtl->eh.ehr_handler);
2291 #else
2292 error ("__builtin_eh_return not supported on this target");
2293 #endif
2296 emit_label (around_label);
2299 /* Convert a ptr_mode address ADDR_TREE to a Pmode address controlled by
2300 POINTERS_EXTEND_UNSIGNED and return it. */
2303 expand_builtin_extend_pointer (tree addr_tree)
2305 rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, EXPAND_NORMAL);
2306 int extend;
2308 #ifdef POINTERS_EXTEND_UNSIGNED
2309 extend = POINTERS_EXTEND_UNSIGNED;
2310 #else
2311 /* The previous EH code did an unsigned extend by default, so we do this also
2312 for consistency. */
2313 extend = 1;
2314 #endif
2316 return convert_modes (targetm.unwind_word_mode (), ptr_mode, addr, extend);
2319 static int
2320 add_action_record (action_hash_type *ar_hash, int filter, int next)
2322 struct action_record **slot, *new_ar, tmp;
2324 tmp.filter = filter;
2325 tmp.next = next;
2326 slot = ar_hash->find_slot (&tmp, INSERT);
2328 if ((new_ar = *slot) == NULL)
2330 new_ar = XNEW (struct action_record);
2331 new_ar->offset = crtl->eh.action_record_data->length () + 1;
2332 new_ar->filter = filter;
2333 new_ar->next = next;
2334 *slot = new_ar;
2336 /* The filter value goes in untouched. The link to the next
2337 record is a "self-relative" byte offset, or zero to indicate
2338 that there is no next record. So convert the absolute 1 based
2339 indices we've been carrying around into a displacement. */
2341 push_sleb128 (&crtl->eh.action_record_data, filter);
2342 if (next)
2343 next -= crtl->eh.action_record_data->length () + 1;
2344 push_sleb128 (&crtl->eh.action_record_data, next);
2347 return new_ar->offset;
2350 static int
2351 collect_one_action_chain (action_hash_type *ar_hash, eh_region region)
2353 int next;
2355 /* If we've reached the top of the region chain, then we have
2356 no actions, and require no landing pad. */
2357 if (region == NULL)
2358 return -1;
2360 switch (region->type)
2362 case ERT_CLEANUP:
2364 eh_region r;
2365 /* A cleanup adds a zero filter to the beginning of the chain, but
2366 there are special cases to look out for. If there are *only*
2367 cleanups along a path, then it compresses to a zero action.
2368 Further, if there are multiple cleanups along a path, we only
2369 need to represent one of them, as that is enough to trigger
2370 entry to the landing pad at runtime. */
2371 next = collect_one_action_chain (ar_hash, region->outer);
2372 if (next <= 0)
2373 return 0;
2374 for (r = region->outer; r ; r = r->outer)
2375 if (r->type == ERT_CLEANUP)
2376 return next;
2377 return add_action_record (ar_hash, 0, next);
2380 case ERT_TRY:
2382 eh_catch c;
2384 /* Process the associated catch regions in reverse order.
2385 If there's a catch-all handler, then we don't need to
2386 search outer regions. Use a magic -3 value to record
2387 that we haven't done the outer search. */
2388 next = -3;
2389 for (c = region->u.eh_try.last_catch; c ; c = c->prev_catch)
2391 if (c->type_list == NULL)
2393 /* Retrieve the filter from the head of the filter list
2394 where we have stored it (see assign_filter_values). */
2395 int filter = TREE_INT_CST_LOW (TREE_VALUE (c->filter_list));
2396 next = add_action_record (ar_hash, filter, 0);
2398 else
2400 /* Once the outer search is done, trigger an action record for
2401 each filter we have. */
2402 tree flt_node;
2404 if (next == -3)
2406 next = collect_one_action_chain (ar_hash, region->outer);
2408 /* If there is no next action, terminate the chain. */
2409 if (next == -1)
2410 next = 0;
2411 /* If all outer actions are cleanups or must_not_throw,
2412 we'll have no action record for it, since we had wanted
2413 to encode these states in the call-site record directly.
2414 Add a cleanup action to the chain to catch these. */
2415 else if (next <= 0)
2416 next = add_action_record (ar_hash, 0, 0);
2419 flt_node = c->filter_list;
2420 for (; flt_node; flt_node = TREE_CHAIN (flt_node))
2422 int filter = TREE_INT_CST_LOW (TREE_VALUE (flt_node));
2423 next = add_action_record (ar_hash, filter, next);
2427 return next;
2430 case ERT_ALLOWED_EXCEPTIONS:
2431 /* An exception specification adds its filter to the
2432 beginning of the chain. */
2433 next = collect_one_action_chain (ar_hash, region->outer);
2435 /* If there is no next action, terminate the chain. */
2436 if (next == -1)
2437 next = 0;
2438 /* If all outer actions are cleanups or must_not_throw,
2439 we'll have no action record for it, since we had wanted
2440 to encode these states in the call-site record directly.
2441 Add a cleanup action to the chain to catch these. */
2442 else if (next <= 0)
2443 next = add_action_record (ar_hash, 0, 0);
2445 return add_action_record (ar_hash, region->u.allowed.filter, next);
2447 case ERT_MUST_NOT_THROW:
2448 /* A must-not-throw region with no inner handlers or cleanups
2449 requires no call-site entry. Note that this differs from
2450 the no handler or cleanup case in that we do require an lsda
2451 to be generated. Return a magic -2 value to record this. */
2452 return -2;
2455 gcc_unreachable ();
2458 static int
2459 add_call_site (rtx landing_pad, int action, int section)
2461 call_site_record record;
2463 record = ggc_alloc<call_site_record_d> ();
2464 record->landing_pad = landing_pad;
2465 record->action = action;
2467 vec_safe_push (crtl->eh.call_site_record_v[section], record);
2469 return call_site_base + crtl->eh.call_site_record_v[section]->length () - 1;
2472 static rtx_note *
2473 emit_note_eh_region_end (rtx_insn *insn)
2475 rtx_insn *next = NEXT_INSN (insn);
2477 /* Make sure we do not split a call and its corresponding
2478 CALL_ARG_LOCATION note. */
2479 if (next && NOTE_P (next)
2480 && NOTE_KIND (next) == NOTE_INSN_CALL_ARG_LOCATION)
2481 insn = next;
2483 return emit_note_after (NOTE_INSN_EH_REGION_END, insn);
2486 /* Turn REG_EH_REGION notes back into NOTE_INSN_EH_REGION notes.
2487 The new note numbers will not refer to region numbers, but
2488 instead to call site entries. */
2490 static unsigned int
2491 convert_to_eh_region_ranges (void)
2493 rtx insn;
2494 rtx_insn *iter;
2495 rtx_note *note;
2496 action_hash_type ar_hash (31);
2497 int last_action = -3;
2498 rtx_insn *last_action_insn = NULL;
2499 rtx last_landing_pad = NULL_RTX;
2500 rtx_insn *first_no_action_insn = NULL;
2501 int call_site = 0;
2502 int cur_sec = 0;
2503 rtx_insn *section_switch_note = NULL;
2504 rtx_insn *first_no_action_insn_before_switch = NULL;
2505 rtx_insn *last_no_action_insn_before_switch = NULL;
2506 int saved_call_site_base = call_site_base;
2508 vec_alloc (crtl->eh.action_record_data, 64);
2510 for (iter = get_insns (); iter ; iter = NEXT_INSN (iter))
2511 if (INSN_P (iter))
2513 eh_landing_pad lp;
2514 eh_region region;
2515 bool nothrow;
2516 int this_action;
2517 rtx_code_label *this_landing_pad;
2519 insn = iter;
2520 if (NONJUMP_INSN_P (insn)
2521 && GET_CODE (PATTERN (insn)) == SEQUENCE)
2522 insn = XVECEXP (PATTERN (insn), 0, 0);
2524 nothrow = get_eh_region_and_lp_from_rtx (insn, &region, &lp);
2525 if (nothrow)
2526 continue;
2527 if (region)
2528 this_action = collect_one_action_chain (&ar_hash, region);
2529 else
2530 this_action = -1;
2532 /* Existence of catch handlers, or must-not-throw regions
2533 implies that an lsda is needed (even if empty). */
2534 if (this_action != -1)
2535 crtl->uses_eh_lsda = 1;
2537 /* Delay creation of region notes for no-action regions
2538 until we're sure that an lsda will be required. */
2539 else if (last_action == -3)
2541 first_no_action_insn = iter;
2542 last_action = -1;
2545 if (this_action >= 0)
2546 this_landing_pad = lp->landing_pad;
2547 else
2548 this_landing_pad = NULL;
2550 /* Differing actions or landing pads implies a change in call-site
2551 info, which implies some EH_REGION note should be emitted. */
2552 if (last_action != this_action
2553 || last_landing_pad != this_landing_pad)
2555 /* If there is a queued no-action region in the other section
2556 with hot/cold partitioning, emit it now. */
2557 if (first_no_action_insn_before_switch)
2559 gcc_assert (this_action != -1
2560 && last_action == (first_no_action_insn
2561 ? -1 : -3));
2562 call_site = add_call_site (NULL_RTX, 0, 0);
2563 note = emit_note_before (NOTE_INSN_EH_REGION_BEG,
2564 first_no_action_insn_before_switch);
2565 NOTE_EH_HANDLER (note) = call_site;
2566 note
2567 = emit_note_eh_region_end (last_no_action_insn_before_switch);
2568 NOTE_EH_HANDLER (note) = call_site;
2569 gcc_assert (last_action != -3
2570 || (last_action_insn
2571 == last_no_action_insn_before_switch));
2572 first_no_action_insn_before_switch = NULL;
2573 last_no_action_insn_before_switch = NULL;
2574 call_site_base++;
2576 /* If we'd not seen a previous action (-3) or the previous
2577 action was must-not-throw (-2), then we do not need an
2578 end note. */
2579 if (last_action >= -1)
2581 /* If we delayed the creation of the begin, do it now. */
2582 if (first_no_action_insn)
2584 call_site = add_call_site (NULL_RTX, 0, cur_sec);
2585 note = emit_note_before (NOTE_INSN_EH_REGION_BEG,
2586 first_no_action_insn);
2587 NOTE_EH_HANDLER (note) = call_site;
2588 first_no_action_insn = NULL;
2591 note = emit_note_eh_region_end (last_action_insn);
2592 NOTE_EH_HANDLER (note) = call_site;
2595 /* If the new action is must-not-throw, then no region notes
2596 are created. */
2597 if (this_action >= -1)
2599 call_site = add_call_site (this_landing_pad,
2600 this_action < 0 ? 0 : this_action,
2601 cur_sec);
2602 note = emit_note_before (NOTE_INSN_EH_REGION_BEG, iter);
2603 NOTE_EH_HANDLER (note) = call_site;
2606 last_action = this_action;
2607 last_landing_pad = this_landing_pad;
2609 last_action_insn = iter;
2611 else if (NOTE_P (iter)
2612 && NOTE_KIND (iter) == NOTE_INSN_SWITCH_TEXT_SECTIONS)
2614 gcc_assert (section_switch_note == NULL_RTX);
2615 gcc_assert (flag_reorder_blocks_and_partition);
2616 section_switch_note = iter;
2617 if (first_no_action_insn)
2619 first_no_action_insn_before_switch = first_no_action_insn;
2620 last_no_action_insn_before_switch = last_action_insn;
2621 first_no_action_insn = NULL;
2622 gcc_assert (last_action == -1);
2623 last_action = -3;
2625 /* Force closing of current EH region before section switch and
2626 opening a new one afterwards. */
2627 else if (last_action != -3)
2628 last_landing_pad = pc_rtx;
2629 if (crtl->eh.call_site_record_v[cur_sec])
2630 call_site_base += crtl->eh.call_site_record_v[cur_sec]->length ();
2631 cur_sec++;
2632 gcc_assert (crtl->eh.call_site_record_v[cur_sec] == NULL);
2633 vec_alloc (crtl->eh.call_site_record_v[cur_sec], 10);
2636 if (last_action >= -1 && ! first_no_action_insn)
2638 note = emit_note_eh_region_end (last_action_insn);
2639 NOTE_EH_HANDLER (note) = call_site;
2642 call_site_base = saved_call_site_base;
2644 return 0;
2647 namespace {
2649 const pass_data pass_data_convert_to_eh_region_ranges =
2651 RTL_PASS, /* type */
2652 "eh_ranges", /* name */
2653 OPTGROUP_NONE, /* optinfo_flags */
2654 TV_NONE, /* tv_id */
2655 0, /* properties_required */
2656 0, /* properties_provided */
2657 0, /* properties_destroyed */
2658 0, /* todo_flags_start */
2659 0, /* todo_flags_finish */
2662 class pass_convert_to_eh_region_ranges : public rtl_opt_pass
2664 public:
2665 pass_convert_to_eh_region_ranges (gcc::context *ctxt)
2666 : rtl_opt_pass (pass_data_convert_to_eh_region_ranges, ctxt)
2669 /* opt_pass methods: */
2670 virtual bool gate (function *);
2671 virtual unsigned int execute (function *)
2673 return convert_to_eh_region_ranges ();
2676 }; // class pass_convert_to_eh_region_ranges
2678 bool
2679 pass_convert_to_eh_region_ranges::gate (function *)
2681 /* Nothing to do for SJLJ exceptions or if no regions created. */
2682 if (cfun->eh->region_tree == NULL)
2683 return false;
2684 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
2685 return false;
2686 return true;
2689 } // anon namespace
2691 rtl_opt_pass *
2692 make_pass_convert_to_eh_region_ranges (gcc::context *ctxt)
2694 return new pass_convert_to_eh_region_ranges (ctxt);
2697 static void
2698 push_uleb128 (vec<uchar, va_gc> **data_area, unsigned int value)
2702 unsigned char byte = value & 0x7f;
2703 value >>= 7;
2704 if (value)
2705 byte |= 0x80;
2706 vec_safe_push (*data_area, byte);
2708 while (value);
2711 static void
2712 push_sleb128 (vec<uchar, va_gc> **data_area, int value)
2714 unsigned char byte;
2715 int more;
2719 byte = value & 0x7f;
2720 value >>= 7;
2721 more = ! ((value == 0 && (byte & 0x40) == 0)
2722 || (value == -1 && (byte & 0x40) != 0));
2723 if (more)
2724 byte |= 0x80;
2725 vec_safe_push (*data_area, byte);
2727 while (more);
2731 #ifndef HAVE_AS_LEB128
2732 static int
2733 dw2_size_of_call_site_table (int section)
2735 int n = vec_safe_length (crtl->eh.call_site_record_v[section]);
2736 int size = n * (4 + 4 + 4);
2737 int i;
2739 for (i = 0; i < n; ++i)
2741 struct call_site_record_d *cs =
2742 (*crtl->eh.call_site_record_v[section])[i];
2743 size += size_of_uleb128 (cs->action);
2746 return size;
2749 static int
2750 sjlj_size_of_call_site_table (void)
2752 int n = vec_safe_length (crtl->eh.call_site_record_v[0]);
2753 int size = 0;
2754 int i;
2756 for (i = 0; i < n; ++i)
2758 struct call_site_record_d *cs =
2759 (*crtl->eh.call_site_record_v[0])[i];
2760 size += size_of_uleb128 (INTVAL (cs->landing_pad));
2761 size += size_of_uleb128 (cs->action);
2764 return size;
2766 #endif
2768 static void
2769 dw2_output_call_site_table (int cs_format, int section)
2771 int n = vec_safe_length (crtl->eh.call_site_record_v[section]);
2772 int i;
2773 const char *begin;
2775 if (section == 0)
2776 begin = current_function_func_begin_label;
2777 else if (first_function_block_is_cold)
2778 begin = crtl->subsections.hot_section_label;
2779 else
2780 begin = crtl->subsections.cold_section_label;
2782 for (i = 0; i < n; ++i)
2784 struct call_site_record_d *cs = (*crtl->eh.call_site_record_v[section])[i];
2785 char reg_start_lab[32];
2786 char reg_end_lab[32];
2787 char landing_pad_lab[32];
2789 ASM_GENERATE_INTERNAL_LABEL (reg_start_lab, "LEHB", call_site_base + i);
2790 ASM_GENERATE_INTERNAL_LABEL (reg_end_lab, "LEHE", call_site_base + i);
2792 if (cs->landing_pad)
2793 ASM_GENERATE_INTERNAL_LABEL (landing_pad_lab, "L",
2794 CODE_LABEL_NUMBER (cs->landing_pad));
2796 /* ??? Perhaps use insn length scaling if the assembler supports
2797 generic arithmetic. */
2798 /* ??? Perhaps use attr_length to choose data1 or data2 instead of
2799 data4 if the function is small enough. */
2800 if (cs_format == DW_EH_PE_uleb128)
2802 dw2_asm_output_delta_uleb128 (reg_start_lab, begin,
2803 "region %d start", i);
2804 dw2_asm_output_delta_uleb128 (reg_end_lab, reg_start_lab,
2805 "length");
2806 if (cs->landing_pad)
2807 dw2_asm_output_delta_uleb128 (landing_pad_lab, begin,
2808 "landing pad");
2809 else
2810 dw2_asm_output_data_uleb128 (0, "landing pad");
2812 else
2814 dw2_asm_output_delta (4, reg_start_lab, begin,
2815 "region %d start", i);
2816 dw2_asm_output_delta (4, reg_end_lab, reg_start_lab, "length");
2817 if (cs->landing_pad)
2818 dw2_asm_output_delta (4, landing_pad_lab, begin,
2819 "landing pad");
2820 else
2821 dw2_asm_output_data (4, 0, "landing pad");
2823 dw2_asm_output_data_uleb128 (cs->action, "action");
2826 call_site_base += n;
2829 static void
2830 sjlj_output_call_site_table (void)
2832 int n = vec_safe_length (crtl->eh.call_site_record_v[0]);
2833 int i;
2835 for (i = 0; i < n; ++i)
2837 struct call_site_record_d *cs = (*crtl->eh.call_site_record_v[0])[i];
2839 dw2_asm_output_data_uleb128 (INTVAL (cs->landing_pad),
2840 "region %d landing pad", i);
2841 dw2_asm_output_data_uleb128 (cs->action, "action");
2844 call_site_base += n;
2847 /* Switch to the section that should be used for exception tables. */
2849 static void
2850 switch_to_exception_section (const char * ARG_UNUSED (fnname))
2852 section *s;
2854 if (exception_section)
2855 s = exception_section;
2856 else
2858 /* Compute the section and cache it into exception_section,
2859 unless it depends on the function name. */
2860 if (targetm_common.have_named_sections)
2862 int flags;
2864 if (EH_TABLES_CAN_BE_READ_ONLY)
2866 int tt_format =
2867 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
2868 flags = ((! flag_pic
2869 || ((tt_format & 0x70) != DW_EH_PE_absptr
2870 && (tt_format & 0x70) != DW_EH_PE_aligned))
2871 ? 0 : SECTION_WRITE);
2873 else
2874 flags = SECTION_WRITE;
2876 #ifdef HAVE_LD_EH_GC_SECTIONS
2877 if (flag_function_sections
2878 || (DECL_COMDAT_GROUP (current_function_decl) && HAVE_COMDAT_GROUP))
2880 char *section_name = XNEWVEC (char, strlen (fnname) + 32);
2881 /* The EH table must match the code section, so only mark
2882 it linkonce if we have COMDAT groups to tie them together. */
2883 if (DECL_COMDAT_GROUP (current_function_decl) && HAVE_COMDAT_GROUP)
2884 flags |= SECTION_LINKONCE;
2885 sprintf (section_name, ".gcc_except_table.%s", fnname);
2886 s = get_section (section_name, flags, current_function_decl);
2887 free (section_name);
2889 else
2890 #endif
2891 exception_section
2892 = s = get_section (".gcc_except_table", flags, NULL);
2894 else
2895 exception_section
2896 = s = flag_pic ? data_section : readonly_data_section;
2899 switch_to_section (s);
2903 /* Output a reference from an exception table to the type_info object TYPE.
2904 TT_FORMAT and TT_FORMAT_SIZE describe the DWARF encoding method used for
2905 the value. */
2907 static void
2908 output_ttype (tree type, int tt_format, int tt_format_size)
2910 rtx value;
2911 bool is_public = true;
2913 if (type == NULL_TREE)
2914 value = const0_rtx;
2915 else
2917 /* FIXME lto. pass_ipa_free_lang_data changes all types to
2918 runtime types so TYPE should already be a runtime type
2919 reference. When pass_ipa_free_lang data is made a default
2920 pass, we can then remove the call to lookup_type_for_runtime
2921 below. */
2922 if (TYPE_P (type))
2923 type = lookup_type_for_runtime (type);
2925 value = expand_expr (type, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
2927 /* Let cgraph know that the rtti decl is used. Not all of the
2928 paths below go through assemble_integer, which would take
2929 care of this for us. */
2930 STRIP_NOPS (type);
2931 if (TREE_CODE (type) == ADDR_EXPR)
2933 type = TREE_OPERAND (type, 0);
2934 if (TREE_CODE (type) == VAR_DECL)
2935 is_public = TREE_PUBLIC (type);
2937 else
2938 gcc_assert (TREE_CODE (type) == INTEGER_CST);
2941 /* Allow the target to override the type table entry format. */
2942 if (targetm.asm_out.ttype (value))
2943 return;
2945 if (tt_format == DW_EH_PE_absptr || tt_format == DW_EH_PE_aligned)
2946 assemble_integer (value, tt_format_size,
2947 tt_format_size * BITS_PER_UNIT, 1);
2948 else
2949 dw2_asm_output_encoded_addr_rtx (tt_format, value, is_public, NULL);
2952 static void
2953 output_one_function_exception_table (int section)
2955 int tt_format, cs_format, lp_format, i;
2956 #ifdef HAVE_AS_LEB128
2957 char ttype_label[32];
2958 char cs_after_size_label[32];
2959 char cs_end_label[32];
2960 #else
2961 int call_site_len;
2962 #endif
2963 int have_tt_data;
2964 int tt_format_size = 0;
2966 have_tt_data = (vec_safe_length (cfun->eh->ttype_data)
2967 || (targetm.arm_eabi_unwinder
2968 ? vec_safe_length (cfun->eh->ehspec_data.arm_eabi)
2969 : vec_safe_length (cfun->eh->ehspec_data.other)));
2971 /* Indicate the format of the @TType entries. */
2972 if (! have_tt_data)
2973 tt_format = DW_EH_PE_omit;
2974 else
2976 tt_format = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
2977 #ifdef HAVE_AS_LEB128
2978 ASM_GENERATE_INTERNAL_LABEL (ttype_label,
2979 section ? "LLSDATTC" : "LLSDATT",
2980 current_function_funcdef_no);
2981 #endif
2982 tt_format_size = size_of_encoded_value (tt_format);
2984 assemble_align (tt_format_size * BITS_PER_UNIT);
2987 targetm.asm_out.internal_label (asm_out_file, section ? "LLSDAC" : "LLSDA",
2988 current_function_funcdef_no);
2990 /* The LSDA header. */
2992 /* Indicate the format of the landing pad start pointer. An omitted
2993 field implies @LPStart == @Start. */
2994 /* Currently we always put @LPStart == @Start. This field would
2995 be most useful in moving the landing pads completely out of
2996 line to another section, but it could also be used to minimize
2997 the size of uleb128 landing pad offsets. */
2998 lp_format = DW_EH_PE_omit;
2999 dw2_asm_output_data (1, lp_format, "@LPStart format (%s)",
3000 eh_data_format_name (lp_format));
3002 /* @LPStart pointer would go here. */
3004 dw2_asm_output_data (1, tt_format, "@TType format (%s)",
3005 eh_data_format_name (tt_format));
3007 #ifndef HAVE_AS_LEB128
3008 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
3009 call_site_len = sjlj_size_of_call_site_table ();
3010 else
3011 call_site_len = dw2_size_of_call_site_table (section);
3012 #endif
3014 /* A pc-relative 4-byte displacement to the @TType data. */
3015 if (have_tt_data)
3017 #ifdef HAVE_AS_LEB128
3018 char ttype_after_disp_label[32];
3019 ASM_GENERATE_INTERNAL_LABEL (ttype_after_disp_label,
3020 section ? "LLSDATTDC" : "LLSDATTD",
3021 current_function_funcdef_no);
3022 dw2_asm_output_delta_uleb128 (ttype_label, ttype_after_disp_label,
3023 "@TType base offset");
3024 ASM_OUTPUT_LABEL (asm_out_file, ttype_after_disp_label);
3025 #else
3026 /* Ug. Alignment queers things. */
3027 unsigned int before_disp, after_disp, last_disp, disp;
3029 before_disp = 1 + 1;
3030 after_disp = (1 + size_of_uleb128 (call_site_len)
3031 + call_site_len
3032 + vec_safe_length (crtl->eh.action_record_data)
3033 + (vec_safe_length (cfun->eh->ttype_data)
3034 * tt_format_size));
3036 disp = after_disp;
3039 unsigned int disp_size, pad;
3041 last_disp = disp;
3042 disp_size = size_of_uleb128 (disp);
3043 pad = before_disp + disp_size + after_disp;
3044 if (pad % tt_format_size)
3045 pad = tt_format_size - (pad % tt_format_size);
3046 else
3047 pad = 0;
3048 disp = after_disp + pad;
3050 while (disp != last_disp);
3052 dw2_asm_output_data_uleb128 (disp, "@TType base offset");
3053 #endif
3056 /* Indicate the format of the call-site offsets. */
3057 #ifdef HAVE_AS_LEB128
3058 cs_format = DW_EH_PE_uleb128;
3059 #else
3060 cs_format = DW_EH_PE_udata4;
3061 #endif
3062 dw2_asm_output_data (1, cs_format, "call-site format (%s)",
3063 eh_data_format_name (cs_format));
3065 #ifdef HAVE_AS_LEB128
3066 ASM_GENERATE_INTERNAL_LABEL (cs_after_size_label,
3067 section ? "LLSDACSBC" : "LLSDACSB",
3068 current_function_funcdef_no);
3069 ASM_GENERATE_INTERNAL_LABEL (cs_end_label,
3070 section ? "LLSDACSEC" : "LLSDACSE",
3071 current_function_funcdef_no);
3072 dw2_asm_output_delta_uleb128 (cs_end_label, cs_after_size_label,
3073 "Call-site table length");
3074 ASM_OUTPUT_LABEL (asm_out_file, cs_after_size_label);
3075 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
3076 sjlj_output_call_site_table ();
3077 else
3078 dw2_output_call_site_table (cs_format, section);
3079 ASM_OUTPUT_LABEL (asm_out_file, cs_end_label);
3080 #else
3081 dw2_asm_output_data_uleb128 (call_site_len, "Call-site table length");
3082 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
3083 sjlj_output_call_site_table ();
3084 else
3085 dw2_output_call_site_table (cs_format, section);
3086 #endif
3088 /* ??? Decode and interpret the data for flag_debug_asm. */
3090 uchar uc;
3091 FOR_EACH_VEC_ELT (*crtl->eh.action_record_data, i, uc)
3092 dw2_asm_output_data (1, uc, i ? NULL : "Action record table");
3095 if (have_tt_data)
3096 assemble_align (tt_format_size * BITS_PER_UNIT);
3098 i = vec_safe_length (cfun->eh->ttype_data);
3099 while (i-- > 0)
3101 tree type = (*cfun->eh->ttype_data)[i];
3102 output_ttype (type, tt_format, tt_format_size);
3105 #ifdef HAVE_AS_LEB128
3106 if (have_tt_data)
3107 ASM_OUTPUT_LABEL (asm_out_file, ttype_label);
3108 #endif
3110 /* ??? Decode and interpret the data for flag_debug_asm. */
3111 if (targetm.arm_eabi_unwinder)
3113 tree type;
3114 for (i = 0;
3115 vec_safe_iterate (cfun->eh->ehspec_data.arm_eabi, i, &type); ++i)
3116 output_ttype (type, tt_format, tt_format_size);
3118 else
3120 uchar uc;
3121 for (i = 0;
3122 vec_safe_iterate (cfun->eh->ehspec_data.other, i, &uc); ++i)
3123 dw2_asm_output_data (1, uc,
3124 i ? NULL : "Exception specification table");
3128 void
3129 output_function_exception_table (const char *fnname)
3131 rtx personality = get_personality_function (current_function_decl);
3133 /* Not all functions need anything. */
3134 if (! crtl->uses_eh_lsda)
3135 return;
3137 if (personality)
3139 assemble_external_libcall (personality);
3141 if (targetm.asm_out.emit_except_personality)
3142 targetm.asm_out.emit_except_personality (personality);
3145 switch_to_exception_section (fnname);
3147 /* If the target wants a label to begin the table, emit it here. */
3148 targetm.asm_out.emit_except_table_label (asm_out_file);
3150 output_one_function_exception_table (0);
3151 if (crtl->eh.call_site_record_v[1])
3152 output_one_function_exception_table (1);
3154 switch_to_section (current_function_section ());
3157 void
3158 set_eh_throw_stmt_table (function *fun, hash_map<gimple, int> *table)
3160 fun->eh->throw_stmt_table = table;
3163 hash_map<gimple, int> *
3164 get_eh_throw_stmt_table (struct function *fun)
3166 return fun->eh->throw_stmt_table;
3169 /* Determine if the function needs an EH personality function. */
3171 enum eh_personality_kind
3172 function_needs_eh_personality (struct function *fn)
3174 enum eh_personality_kind kind = eh_personality_none;
3175 eh_region i;
3177 FOR_ALL_EH_REGION_FN (i, fn)
3179 switch (i->type)
3181 case ERT_CLEANUP:
3182 /* Can do with any personality including the generic C one. */
3183 kind = eh_personality_any;
3184 break;
3186 case ERT_TRY:
3187 case ERT_ALLOWED_EXCEPTIONS:
3188 /* Always needs a EH personality function. The generic C
3189 personality doesn't handle these even for empty type lists. */
3190 return eh_personality_lang;
3192 case ERT_MUST_NOT_THROW:
3193 /* Always needs a EH personality function. The language may specify
3194 what abort routine that must be used, e.g. std::terminate. */
3195 return eh_personality_lang;
3199 return kind;
3202 /* Dump EH information to OUT. */
3204 void
3205 dump_eh_tree (FILE * out, struct function *fun)
3207 eh_region i;
3208 int depth = 0;
3209 static const char *const type_name[] = {
3210 "cleanup", "try", "allowed_exceptions", "must_not_throw"
3213 i = fun->eh->region_tree;
3214 if (!i)
3215 return;
3217 fprintf (out, "Eh tree:\n");
3218 while (1)
3220 fprintf (out, " %*s %i %s", depth * 2, "",
3221 i->index, type_name[(int) i->type]);
3223 if (i->landing_pads)
3225 eh_landing_pad lp;
3227 fprintf (out, " land:");
3228 if (current_ir_type () == IR_GIMPLE)
3230 for (lp = i->landing_pads; lp ; lp = lp->next_lp)
3232 fprintf (out, "{%i,", lp->index);
3233 print_generic_expr (out, lp->post_landing_pad, 0);
3234 fputc ('}', out);
3235 if (lp->next_lp)
3236 fputc (',', out);
3239 else
3241 for (lp = i->landing_pads; lp ; lp = lp->next_lp)
3243 fprintf (out, "{%i,", lp->index);
3244 if (lp->landing_pad)
3245 fprintf (out, "%i%s,", INSN_UID (lp->landing_pad),
3246 NOTE_P (lp->landing_pad) ? "(del)" : "");
3247 else
3248 fprintf (out, "(nil),");
3249 if (lp->post_landing_pad)
3251 rtx_insn *lab = label_rtx (lp->post_landing_pad);
3252 fprintf (out, "%i%s}", INSN_UID (lab),
3253 NOTE_P (lab) ? "(del)" : "");
3255 else
3256 fprintf (out, "(nil)}");
3257 if (lp->next_lp)
3258 fputc (',', out);
3263 switch (i->type)
3265 case ERT_CLEANUP:
3266 case ERT_MUST_NOT_THROW:
3267 break;
3269 case ERT_TRY:
3271 eh_catch c;
3272 fprintf (out, " catch:");
3273 for (c = i->u.eh_try.first_catch; c; c = c->next_catch)
3275 fputc ('{', out);
3276 if (c->label)
3278 fprintf (out, "lab:");
3279 print_generic_expr (out, c->label, 0);
3280 fputc (';', out);
3282 print_generic_expr (out, c->type_list, 0);
3283 fputc ('}', out);
3284 if (c->next_catch)
3285 fputc (',', out);
3288 break;
3290 case ERT_ALLOWED_EXCEPTIONS:
3291 fprintf (out, " filter :%i types:", i->u.allowed.filter);
3292 print_generic_expr (out, i->u.allowed.type_list, 0);
3293 break;
3295 fputc ('\n', out);
3297 /* If there are sub-regions, process them. */
3298 if (i->inner)
3299 i = i->inner, depth++;
3300 /* If there are peers, process them. */
3301 else if (i->next_peer)
3302 i = i->next_peer;
3303 /* Otherwise, step back up the tree to the next peer. */
3304 else
3308 i = i->outer;
3309 depth--;
3310 if (i == NULL)
3311 return;
3313 while (i->next_peer == NULL);
3314 i = i->next_peer;
3319 /* Dump the EH tree for FN on stderr. */
3321 DEBUG_FUNCTION void
3322 debug_eh_tree (struct function *fn)
3324 dump_eh_tree (stderr, fn);
3327 /* Verify invariants on EH datastructures. */
3329 DEBUG_FUNCTION void
3330 verify_eh_tree (struct function *fun)
3332 eh_region r, outer;
3333 int nvisited_lp, nvisited_r;
3334 int count_lp, count_r, depth, i;
3335 eh_landing_pad lp;
3336 bool err = false;
3338 if (!fun->eh->region_tree)
3339 return;
3341 count_r = 0;
3342 for (i = 1; vec_safe_iterate (fun->eh->region_array, i, &r); ++i)
3343 if (r)
3345 if (r->index == i)
3346 count_r++;
3347 else
3349 error ("region_array is corrupted for region %i", r->index);
3350 err = true;
3354 count_lp = 0;
3355 for (i = 1; vec_safe_iterate (fun->eh->lp_array, i, &lp); ++i)
3356 if (lp)
3358 if (lp->index == i)
3359 count_lp++;
3360 else
3362 error ("lp_array is corrupted for lp %i", lp->index);
3363 err = true;
3367 depth = nvisited_lp = nvisited_r = 0;
3368 outer = NULL;
3369 r = fun->eh->region_tree;
3370 while (1)
3372 if ((*fun->eh->region_array)[r->index] != r)
3374 error ("region_array is corrupted for region %i", r->index);
3375 err = true;
3377 if (r->outer != outer)
3379 error ("outer block of region %i is wrong", r->index);
3380 err = true;
3382 if (depth < 0)
3384 error ("negative nesting depth of region %i", r->index);
3385 err = true;
3387 nvisited_r++;
3389 for (lp = r->landing_pads; lp ; lp = lp->next_lp)
3391 if ((*fun->eh->lp_array)[lp->index] != lp)
3393 error ("lp_array is corrupted for lp %i", lp->index);
3394 err = true;
3396 if (lp->region != r)
3398 error ("region of lp %i is wrong", lp->index);
3399 err = true;
3401 nvisited_lp++;
3404 if (r->inner)
3405 outer = r, r = r->inner, depth++;
3406 else if (r->next_peer)
3407 r = r->next_peer;
3408 else
3412 r = r->outer;
3413 if (r == NULL)
3414 goto region_done;
3415 depth--;
3416 outer = r->outer;
3418 while (r->next_peer == NULL);
3419 r = r->next_peer;
3422 region_done:
3423 if (depth != 0)
3425 error ("tree list ends on depth %i", depth);
3426 err = true;
3428 if (count_r != nvisited_r)
3430 error ("region_array does not match region_tree");
3431 err = true;
3433 if (count_lp != nvisited_lp)
3435 error ("lp_array does not match region_tree");
3436 err = true;
3439 if (err)
3441 dump_eh_tree (stderr, fun);
3442 internal_error ("verify_eh_tree failed");
3446 #include "gt-except.h"