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