OSDN Git Service

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