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