]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/except.c
Merge tree-ssa-20020619-branch into mainline.
[thirdparty/gcc.git] / gcc / except.c
CommitLineData
12670d88 1/* Implements exception handling.
3f2c5d1a 2 Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3897f229 3 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4956d07c
MS
4 Contributed by Mike Stump <mrs@cygnus.com>.
5
1322177d 6This file is part of GCC.
4956d07c 7
1322177d
LB
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
10Software Foundation; either version 2, or (at your option) any later
11version.
4956d07c 12
1322177d
LB
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
4956d07c
MS
17
18You should have received a copy of the GNU General Public License
1322177d
LB
19along with GCC; see the file COPYING. If not, write to the Free
20Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2102111-1307, USA. */
4956d07c
MS
22
23
12670d88
RK
24/* An exception is an event that can be signaled from within a
25 function. This event can then be "caught" or "trapped" by the
26 callers of this function. This potentially allows program flow to
956d6950 27 be transferred to any arbitrary code associated with a function call
12670d88
RK
28 several levels up the stack.
29
30 The intended use for this mechanism is for signaling "exceptional
31 events" in an out-of-band fashion, hence its name. The C++ language
32 (and many other OO-styled or functional languages) practically
33 requires such a mechanism, as otherwise it becomes very difficult
34 or even impossible to signal failure conditions in complex
35 situations. The traditional C++ example is when an error occurs in
36 the process of constructing an object; without such a mechanism, it
37 is impossible to signal that the error occurs without adding global
38 state variables and error checks around every object construction.
39
40 The act of causing this event to occur is referred to as "throwing
41 an exception". (Alternate terms include "raising an exception" or
42 "signaling an exception".) The term "throw" is used because control
43 is returned to the callers of the function that is signaling the
44 exception, and thus there is the concept of "throwing" the
45 exception up the call stack.
46
52a11cbf 47 [ Add updated documentation on how to use this. ] */
4956d07c
MS
48
49
50#include "config.h"
670ee920 51#include "system.h"
4977bab6
ZW
52#include "coretypes.h"
53#include "tm.h"
4956d07c
MS
54#include "rtl.h"
55#include "tree.h"
56#include "flags.h"
4956d07c 57#include "function.h"
4956d07c 58#include "expr.h"
e78d8e51 59#include "libfuncs.h"
4956d07c 60#include "insn-config.h"
52a11cbf
RH
61#include "except.h"
62#include "integrate.h"
63#include "hard-reg-set.h"
64#include "basic-block.h"
4956d07c 65#include "output.h"
52a11cbf
RH
66#include "dwarf2asm.h"
67#include "dwarf2out.h"
2a1ee410 68#include "dwarf2.h"
10f0ad3d 69#include "toplev.h"
52a11cbf 70#include "hashtab.h"
2b12ffe0 71#include "intl.h"
87ff9c8e 72#include "ggc.h"
b1474bb7 73#include "tm_p.h"
07c9d2eb 74#include "target.h"
f1e639b1 75#include "langhooks.h"
dd07abd7 76#include "cgraph.h"
52a11cbf
RH
77
78/* Provide defaults for stuff that may not be defined when using
79 sjlj exceptions. */
52a11cbf
RH
80#ifndef EH_RETURN_DATA_REGNO
81#define EH_RETURN_DATA_REGNO(N) INVALID_REGNUM
461fc4de
RH
82#endif
83
27a36778 84
52a11cbf
RH
85/* Nonzero means enable synchronous exceptions for non-call instructions. */
86int flag_non_call_exceptions;
27a36778 87
52a11cbf
RH
88/* Protect cleanup actions with must-not-throw regions, with a call
89 to the given failure handler. */
502b8322 90tree (*lang_protect_cleanup_actions) (void);
27a36778 91
52a11cbf 92/* Return true if type A catches type B. */
502b8322 93int (*lang_eh_type_covers) (tree a, tree b);
27a36778 94
52a11cbf 95/* Map a type to a runtime object to match type. */
502b8322 96tree (*lang_eh_runtime_type) (tree);
4956d07c 97
6a58eee9
RH
98/* A hash table of label to region number. */
99
e2500fed 100struct ehl_map_entry GTY(())
6a58eee9
RH
101{
102 rtx label;
103 struct eh_region *region;
104};
105
21c157b4 106static GTY(()) int call_site_base;
e2500fed
GK
107static GTY ((param_is (union tree_node)))
108 htab_t type_to_runtime_map;
52a11cbf
RH
109
110/* Describe the SjLj_Function_Context structure. */
e2500fed 111static GTY(()) tree sjlj_fc_type_node;
52a11cbf
RH
112static int sjlj_fc_call_site_ofs;
113static int sjlj_fc_data_ofs;
114static int sjlj_fc_personality_ofs;
115static int sjlj_fc_lsda_ofs;
116static int sjlj_fc_jbuf_ofs;
117\f
118/* Describes one exception region. */
e2500fed 119struct eh_region GTY(())
52a11cbf
RH
120{
121 /* The immediately surrounding region. */
122 struct eh_region *outer;
956d6950 123
52a11cbf
RH
124 /* The list of immediately contained regions. */
125 struct eh_region *inner;
126 struct eh_region *next_peer;
956d6950 127
52a11cbf
RH
128 /* An identifier for this region. */
129 int region_number;
71038426 130
6a58eee9
RH
131 /* When a region is deleted, its parents inherit the REG_EH_REGION
132 numbers already assigned. */
133 bitmap aka;
134
52a11cbf
RH
135 /* Each region does exactly one thing. */
136 enum eh_region_type
6de9cd9a 137 {
572202a7
RK
138 ERT_UNKNOWN = 0,
139 ERT_CLEANUP,
52a11cbf
RH
140 ERT_TRY,
141 ERT_CATCH,
142 ERT_ALLOWED_EXCEPTIONS,
143 ERT_MUST_NOT_THROW,
144 ERT_THROW,
145 ERT_FIXUP
146 } type;
147
eaec9b3d 148 /* Holds the action to perform based on the preceding type. */
e2500fed 149 union eh_region_u {
52a11cbf
RH
150 /* A list of catch blocks, a surrounding try block,
151 and the label for continuing after a catch. */
e2500fed 152 struct eh_region_u_try {
52a11cbf
RH
153 struct eh_region *catch;
154 struct eh_region *last_catch;
155 struct eh_region *prev_try;
156 rtx continue_label;
e2500fed 157 } GTY ((tag ("ERT_TRY"))) try;
52a11cbf 158
6d41a92f
OH
159 /* The list through the catch handlers, the list of type objects
160 matched, and the list of associated filters. */
e2500fed 161 struct eh_region_u_catch {
52a11cbf
RH
162 struct eh_region *next_catch;
163 struct eh_region *prev_catch;
6d41a92f
OH
164 tree type_list;
165 tree filter_list;
e2500fed 166 } GTY ((tag ("ERT_CATCH"))) catch;
52a11cbf
RH
167
168 /* A tree_list of allowed types. */
e2500fed 169 struct eh_region_u_allowed {
52a11cbf
RH
170 tree type_list;
171 int filter;
e2500fed 172 } GTY ((tag ("ERT_ALLOWED_EXCEPTIONS"))) allowed;
52a11cbf 173
3f2c5d1a 174 /* The type given by a call to "throw foo();", or discovered
52a11cbf 175 for a throw. */
e2500fed 176 struct eh_region_u_throw {
52a11cbf 177 tree type;
e2500fed 178 } GTY ((tag ("ERT_THROW"))) throw;
52a11cbf
RH
179
180 /* Retain the cleanup expression even after expansion so that
181 we can match up fixup regions. */
e2500fed 182 struct eh_region_u_cleanup {
52a11cbf 183 tree exp;
bafb714b 184 struct eh_region *prev_try;
e2500fed 185 } GTY ((tag ("ERT_CLEANUP"))) cleanup;
52a11cbf
RH
186
187 /* The real region (by expression and by pointer) that fixup code
188 should live in. */
e2500fed 189 struct eh_region_u_fixup {
52a11cbf
RH
190 tree cleanup_exp;
191 struct eh_region *real_region;
1bddbeb4 192 bool resolved;
e2500fed
GK
193 } GTY ((tag ("ERT_FIXUP"))) fixup;
194 } GTY ((desc ("%0.type"))) u;
52a11cbf 195
47c84870
JM
196 /* Entry point for this region's handler before landing pads are built. */
197 rtx label;
6de9cd9a 198 tree tree_label;
52a11cbf 199
47c84870 200 /* Entry point for this region's handler from the runtime eh library. */
52a11cbf
RH
201 rtx landing_pad;
202
47c84870 203 /* Entry point for this region's handler from an inner region. */
52a11cbf 204 rtx post_landing_pad;
47c84870
JM
205
206 /* The RESX insn for handing off control to the next outermost handler,
207 if appropriate. */
208 rtx resume;
b2dd096b
MM
209
210 /* True if something in this region may throw. */
211 unsigned may_contain_throw : 1;
52a11cbf 212};
71038426 213
e2500fed
GK
214struct call_site_record GTY(())
215{
216 rtx landing_pad;
217 int action;
218};
219
52a11cbf 220/* Used to save exception status for each function. */
e2500fed 221struct eh_status GTY(())
52a11cbf
RH
222{
223 /* The tree of all regions for this function. */
224 struct eh_region *region_tree;
e6cfb550 225
52a11cbf 226 /* The same information as an indexable array. */
e2500fed 227 struct eh_region ** GTY ((length ("%h.last_region_number"))) region_array;
e6cfb550 228
52a11cbf
RH
229 /* The most recently open region. */
230 struct eh_region *cur_region;
e6cfb550 231
52a11cbf
RH
232 /* This is the region for which we are processing catch blocks. */
233 struct eh_region *try_region;
71038426 234
52a11cbf
RH
235 rtx filter;
236 rtx exc_ptr;
4956d07c 237
52a11cbf
RH
238 int built_landing_pads;
239 int last_region_number;
e6cfb550 240
52a11cbf
RH
241 varray_type ttype_data;
242 varray_type ehspec_data;
243 varray_type action_record_data;
6814a8a0 244
e2500fed
GK
245 htab_t GTY ((param_is (struct ehl_map_entry))) exception_handler_label_map;
246
502b8322 247 struct call_site_record * GTY ((length ("%h.call_site_data_used")))
e2500fed 248 call_site_data;
52a11cbf
RH
249 int call_site_data_used;
250 int call_site_data_size;
251
252 rtx ehr_stackadj;
253 rtx ehr_handler;
254 rtx ehr_label;
255
256 rtx sjlj_fc;
257 rtx sjlj_exit_after;
258};
e6cfb550 259
52a11cbf 260\f
502b8322
AJ
261static int t2r_eq (const void *, const void *);
262static hashval_t t2r_hash (const void *);
263static void add_type_for_runtime (tree);
264static tree lookup_type_for_runtime (tree);
265
266static struct eh_region *expand_eh_region_end (void);
267
502b8322
AJ
268static void resolve_fixup_regions (void);
269static void remove_fixup_regions (void);
270static void remove_unreachable_regions (rtx);
271static void convert_from_eh_region_ranges_1 (rtx *, int *, int);
272
273static struct eh_region *duplicate_eh_region_1 (struct eh_region *,
274 struct inline_remap *);
275static void duplicate_eh_region_2 (struct eh_region *, struct eh_region **);
276static int ttypes_filter_eq (const void *, const void *);
277static hashval_t ttypes_filter_hash (const void *);
278static int ehspec_filter_eq (const void *, const void *);
279static hashval_t ehspec_filter_hash (const void *);
280static int add_ttypes_entry (htab_t, tree);
281static int add_ehspec_entry (htab_t, htab_t, tree);
282static void assign_filter_values (void);
283static void build_post_landing_pads (void);
284static void connect_post_landing_pads (void);
285static void dw2_build_landing_pads (void);
52a11cbf
RH
286
287struct sjlj_lp_info;
502b8322
AJ
288static bool sjlj_find_directly_reachable_regions (struct sjlj_lp_info *);
289static void sjlj_assign_call_site_values (rtx, struct sjlj_lp_info *);
290static void sjlj_mark_call_sites (struct sjlj_lp_info *);
291static void sjlj_emit_function_enter (rtx);
292static void sjlj_emit_function_exit (void);
293static void sjlj_emit_dispatch_table (rtx, struct sjlj_lp_info *);
294static void sjlj_build_landing_pads (void);
295
296static hashval_t ehl_hash (const void *);
297static int ehl_eq (const void *, const void *);
298static void add_ehl_entry (rtx, struct eh_region *);
299static void remove_exception_handler_label (rtx);
300static void remove_eh_handler (struct eh_region *);
301static int for_each_eh_label_1 (void **, void *);
52a11cbf 302
52a11cbf
RH
303/* The return value of reachable_next_level. */
304enum reachable_code
305{
306 /* The given exception is not processed by the given region. */
307 RNL_NOT_CAUGHT,
308 /* The given exception may need processing by the given region. */
309 RNL_MAYBE_CAUGHT,
310 /* The given exception is completely processed by the given region. */
311 RNL_CAUGHT,
312 /* The given exception is completely processed by the runtime. */
313 RNL_BLOCKED
314};
e6cfb550 315
6de9cd9a 316struct reachable_info;
502b8322
AJ
317static enum reachable_code reachable_next_level (struct eh_region *, tree,
318 struct reachable_info *);
319
320static int action_record_eq (const void *, const void *);
321static hashval_t action_record_hash (const void *);
322static int add_action_record (htab_t, int, int);
323static int collect_one_action_chain (htab_t, struct eh_region *);
324static int add_call_site (rtx, int);
325
326static void push_uleb128 (varray_type *, unsigned int);
327static void push_sleb128 (varray_type *, int);
52a11cbf 328#ifndef HAVE_AS_LEB128
502b8322
AJ
329static int dw2_size_of_call_site_table (void);
330static int sjlj_size_of_call_site_table (void);
52a11cbf 331#endif
502b8322
AJ
332static void dw2_output_call_site_table (void);
333static void sjlj_output_call_site_table (void);
e6cfb550 334
52a11cbf
RH
335\f
336/* Routine to see if exception handling is turned on.
cc2902df 337 DO_WARN is nonzero if we want to inform the user that exception
3f2c5d1a 338 handling is turned off.
4956d07c 339
52a11cbf
RH
340 This is used to ensure that -fexceptions has been specified if the
341 compiler tries to use any exception-specific functions. */
4956d07c 342
52a11cbf 343int
502b8322 344doing_eh (int do_warn)
52a11cbf
RH
345{
346 if (! flag_exceptions)
347 {
348 static int warned = 0;
349 if (! warned && do_warn)
350 {
351 error ("exception handling disabled, use -fexceptions to enable");
352 warned = 1;
353 }
354 return 0;
355 }
356 return 1;
4956d07c
MS
357}
358
52a11cbf
RH
359\f
360void
502b8322 361init_eh (void)
4956d07c 362{
52a11cbf
RH
363 if (! flag_exceptions)
364 return;
4956d07c 365
e2500fed 366 type_to_runtime_map = htab_create_ggc (31, t2r_hash, t2r_eq, NULL);
4956d07c 367
52a11cbf
RH
368 /* Create the SjLj_Function_Context structure. This should match
369 the definition in unwind-sjlj.c. */
370 if (USING_SJLJ_EXCEPTIONS)
371 {
372 tree f_jbuf, f_per, f_lsda, f_prev, f_cs, f_data, tmp;
4956d07c 373
ae2bcd98 374 sjlj_fc_type_node = lang_hooks.types.make_type (RECORD_TYPE);
9a0d1e1b 375
52a11cbf
RH
376 f_prev = build_decl (FIELD_DECL, get_identifier ("__prev"),
377 build_pointer_type (sjlj_fc_type_node));
378 DECL_FIELD_CONTEXT (f_prev) = sjlj_fc_type_node;
9a0d1e1b 379
52a11cbf
RH
380 f_cs = build_decl (FIELD_DECL, get_identifier ("__call_site"),
381 integer_type_node);
382 DECL_FIELD_CONTEXT (f_cs) = sjlj_fc_type_node;
4956d07c 383
52a11cbf 384 tmp = build_index_type (build_int_2 (4 - 1, 0));
ae2bcd98 385 tmp = build_array_type (lang_hooks.types.type_for_mode (word_mode, 1),
b0c48229 386 tmp);
52a11cbf
RH
387 f_data = build_decl (FIELD_DECL, get_identifier ("__data"), tmp);
388 DECL_FIELD_CONTEXT (f_data) = sjlj_fc_type_node;
9a0d1e1b 389
52a11cbf
RH
390 f_per = build_decl (FIELD_DECL, get_identifier ("__personality"),
391 ptr_type_node);
392 DECL_FIELD_CONTEXT (f_per) = sjlj_fc_type_node;
4956d07c 393
52a11cbf
RH
394 f_lsda = build_decl (FIELD_DECL, get_identifier ("__lsda"),
395 ptr_type_node);
396 DECL_FIELD_CONTEXT (f_lsda) = sjlj_fc_type_node;
6814a8a0 397
52a11cbf
RH
398#ifdef DONT_USE_BUILTIN_SETJMP
399#ifdef JMP_BUF_SIZE
400 tmp = build_int_2 (JMP_BUF_SIZE - 1, 0);
401#else
402 /* Should be large enough for most systems, if it is not,
403 JMP_BUF_SIZE should be defined with the proper value. It will
404 also tend to be larger than necessary for most systems, a more
405 optimal port will define JMP_BUF_SIZE. */
406 tmp = build_int_2 (FIRST_PSEUDO_REGISTER + 2 - 1, 0);
407#endif
408#else
83810fcb
RS
409 /* builtin_setjmp takes a pointer to 5 words. */
410 tmp = build_int_2 (5 * BITS_PER_WORD / POINTER_SIZE - 1, 0);
52a11cbf
RH
411#endif
412 tmp = build_index_type (tmp);
413 tmp = build_array_type (ptr_type_node, tmp);
414 f_jbuf = build_decl (FIELD_DECL, get_identifier ("__jbuf"), tmp);
415#ifdef DONT_USE_BUILTIN_SETJMP
416 /* We don't know what the alignment requirements of the
417 runtime's jmp_buf has. Overestimate. */
418 DECL_ALIGN (f_jbuf) = BIGGEST_ALIGNMENT;
419 DECL_USER_ALIGN (f_jbuf) = 1;
420#endif
421 DECL_FIELD_CONTEXT (f_jbuf) = sjlj_fc_type_node;
422
423 TYPE_FIELDS (sjlj_fc_type_node) = f_prev;
424 TREE_CHAIN (f_prev) = f_cs;
425 TREE_CHAIN (f_cs) = f_data;
426 TREE_CHAIN (f_data) = f_per;
427 TREE_CHAIN (f_per) = f_lsda;
428 TREE_CHAIN (f_lsda) = f_jbuf;
429
430 layout_type (sjlj_fc_type_node);
431
432 /* Cache the interesting field offsets so that we have
433 easy access from rtl. */
434 sjlj_fc_call_site_ofs
435 = (tree_low_cst (DECL_FIELD_OFFSET (f_cs), 1)
436 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_cs), 1) / BITS_PER_UNIT);
437 sjlj_fc_data_ofs
438 = (tree_low_cst (DECL_FIELD_OFFSET (f_data), 1)
439 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_data), 1) / BITS_PER_UNIT);
440 sjlj_fc_personality_ofs
441 = (tree_low_cst (DECL_FIELD_OFFSET (f_per), 1)
442 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_per), 1) / BITS_PER_UNIT);
443 sjlj_fc_lsda_ofs
444 = (tree_low_cst (DECL_FIELD_OFFSET (f_lsda), 1)
445 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_lsda), 1) / BITS_PER_UNIT);
446 sjlj_fc_jbuf_ofs
447 = (tree_low_cst (DECL_FIELD_OFFSET (f_jbuf), 1)
448 + tree_low_cst (DECL_FIELD_BIT_OFFSET (f_jbuf), 1) / BITS_PER_UNIT);
449 }
4956d07c
MS
450}
451
52a11cbf 452void
502b8322 453init_eh_for_function (void)
4956d07c 454{
703ad42b 455 cfun->eh = ggc_alloc_cleared (sizeof (struct eh_status));
6a58eee9 456}
52a11cbf 457\f
6de9cd9a
DN
458/* Routines to generate the exception tree somewhat directly.
459 These are used from tree-eh.c when processing exception related
460 nodes during tree optimization. */
461
462static struct eh_region *
463gen_eh_region (enum eh_region_type type, struct eh_region *outer)
464{
465 struct eh_region *new;
466
467#ifdef ENABLE_CHECKING
468 if (! doing_eh (0))
469 abort ();
470#endif
471
472 /* Insert a new blank region as a leaf in the tree. */
473 new = ggc_alloc_cleared (sizeof (*new));
474 new->type = type;
475 new->outer = outer;
476 if (outer)
477 {
478 new->next_peer = outer->inner;
479 outer->inner = new;
480 }
481 else
482 {
483 new->next_peer = cfun->eh->region_tree;
484 cfun->eh->region_tree = new;
485 }
486
487 new->region_number = ++cfun->eh->last_region_number;
488
489 return new;
490}
491
492struct eh_region *
493gen_eh_region_cleanup (struct eh_region *outer, struct eh_region *prev_try)
494{
495 struct eh_region *cleanup = gen_eh_region (ERT_CLEANUP, outer);
496 cleanup->u.cleanup.prev_try = prev_try;
497 return cleanup;
498}
499
500struct eh_region *
501gen_eh_region_try (struct eh_region *outer)
502{
503 return gen_eh_region (ERT_TRY, outer);
504}
505
506struct eh_region *
507gen_eh_region_catch (struct eh_region *t, tree type_or_list)
508{
509 struct eh_region *c, *l;
510 tree type_list, type_node;
511
512 /* Ensure to always end up with a type list to normalize further
513 processing, then register each type against the runtime types map. */
514 type_list = type_or_list;
515 if (type_or_list)
516 {
517 if (TREE_CODE (type_or_list) != TREE_LIST)
518 type_list = tree_cons (NULL_TREE, type_or_list, NULL_TREE);
519
520 type_node = type_list;
521 for (; type_node; type_node = TREE_CHAIN (type_node))
522 add_type_for_runtime (TREE_VALUE (type_node));
523 }
524
525 c = gen_eh_region (ERT_CATCH, t->outer);
526 c->u.catch.type_list = type_list;
527 l = t->u.try.last_catch;
528 c->u.catch.prev_catch = l;
529 if (l)
530 l->u.catch.next_catch = c;
531 else
532 t->u.try.catch = c;
533 t->u.try.last_catch = c;
534
535 return c;
536}
537
538struct eh_region *
539gen_eh_region_allowed (struct eh_region *outer, tree allowed)
540{
541 struct eh_region *region = gen_eh_region (ERT_ALLOWED_EXCEPTIONS, outer);
542 region->u.allowed.type_list = allowed;
543
544 for (; allowed ; allowed = TREE_CHAIN (allowed))
545 add_type_for_runtime (TREE_VALUE (allowed));
546
547 return region;
548}
549
550struct eh_region *
551gen_eh_region_must_not_throw (struct eh_region *outer)
552{
553 return gen_eh_region (ERT_MUST_NOT_THROW, outer);
554}
555
556int
557get_eh_region_number (struct eh_region *region)
558{
559 return region->region_number;
560}
561
562bool
563get_eh_region_may_contain_throw (struct eh_region *region)
564{
565 return region->may_contain_throw;
566}
567
568tree
569get_eh_region_tree_label (struct eh_region *region)
570{
571 return region->tree_label;
572}
573
574void
575set_eh_region_tree_label (struct eh_region *region, tree lab)
576{
577 region->tree_label = lab;
578}
579\f
52a11cbf
RH
580/* Start an exception handling region. All instructions emitted
581 after this point are considered to be part of the region until
582 expand_eh_region_end is invoked. */
9a0d1e1b 583
52a11cbf 584void
502b8322 585expand_eh_region_start (void)
9a0d1e1b 586{
6de9cd9a 587 struct eh_region *new;
52a11cbf 588 rtx note;
9a0d1e1b 589
52a11cbf
RH
590 if (! doing_eh (0))
591 return;
9a0d1e1b 592
6de9cd9a
DN
593 new = gen_eh_region (ERT_UNKNOWN, cfun->eh->cur_region);
594 cfun->eh->cur_region = new;
52a11cbf
RH
595
596 /* Create a note marking the start of this region. */
2e040219 597 note = emit_note (NOTE_INSN_EH_REGION_BEG);
6de9cd9a 598 NOTE_EH_HANDLER (note) = new->region_number;
9a0d1e1b
AM
599}
600
52a11cbf 601/* Common code to end a region. Returns the region just ended. */
9f8e6243 602
52a11cbf 603static struct eh_region *
502b8322 604expand_eh_region_end (void)
9f8e6243 605{
52a11cbf
RH
606 struct eh_region *cur_region = cfun->eh->cur_region;
607 rtx note;
608
a1f300c0 609 /* Create a note marking the end of this region. */
2e040219 610 note = emit_note (NOTE_INSN_EH_REGION_END);
52a11cbf
RH
611 NOTE_EH_HANDLER (note) = cur_region->region_number;
612
613 /* Pop. */
614 cfun->eh->cur_region = cur_region->outer;
615
52a11cbf 616 return cur_region;
9f8e6243
AM
617}
618
6de9cd9a
DN
619/* Expand HANDLER, which is the operand 1 of a TRY_CATCH_EXPR. Catch
620 blocks and C++ exception-specifications are handled specially. */
621
622void
623expand_eh_handler (tree handler)
624{
625 tree inner = expr_first (handler);
626
627 switch (TREE_CODE (inner))
628 {
629 case CATCH_EXPR:
630 expand_start_all_catch ();
631 expand_expr (handler, const0_rtx, VOIDmode, 0);
632 expand_end_all_catch ();
633 break;
634
635 case EH_FILTER_EXPR:
636 if (EH_FILTER_MUST_NOT_THROW (handler))
637 expand_eh_region_end_must_not_throw (EH_FILTER_FAILURE (handler));
638 else
639 expand_eh_region_end_allowed (EH_FILTER_TYPES (handler),
640 EH_FILTER_FAILURE (handler));
641 break;
642
643 default:
644 expand_eh_region_end_cleanup (handler);
645 break;
646 }
647}
648
52a11cbf
RH
649/* End an exception handling region for a cleanup. HANDLER is an
650 expression to expand for the cleanup. */
9c606f69 651
52a11cbf 652void
502b8322 653expand_eh_region_end_cleanup (tree handler)
9c606f69 654{
52a11cbf 655 struct eh_region *region;
e6855a2d 656 tree protect_cleanup_actions;
52a11cbf 657 rtx around_label;
47c84870 658 rtx data_save[2];
52a11cbf
RH
659
660 if (! doing_eh (0))
661 return;
9c606f69 662
52a11cbf
RH
663 region = expand_eh_region_end ();
664 region->type = ERT_CLEANUP;
665 region->label = gen_label_rtx ();
666 region->u.cleanup.exp = handler;
bafb714b 667 region->u.cleanup.prev_try = cfun->eh->try_region;
9c606f69 668
52a11cbf
RH
669 around_label = gen_label_rtx ();
670 emit_jump (around_label);
9c606f69 671
52a11cbf 672 emit_label (region->label);
9c606f69 673
a944ceb9 674 if (flag_non_call_exceptions || region->may_contain_throw)
b2dd096b
MM
675 {
676 /* Give the language a chance to specify an action to be taken if an
677 exception is thrown that would propagate out of the HANDLER. */
678 protect_cleanup_actions
679 = (lang_protect_cleanup_actions
680 ? (*lang_protect_cleanup_actions) ()
681 : NULL_TREE);
e6855a2d 682
b2dd096b
MM
683 if (protect_cleanup_actions)
684 expand_eh_region_start ();
9c606f69 685
b2dd096b
MM
686 /* In case this cleanup involves an inline destructor with a try block in
687 it, we need to save the EH return data registers around it. */
688 data_save[0] = gen_reg_rtx (ptr_mode);
689 emit_move_insn (data_save[0], get_exception_pointer (cfun));
690 data_save[1] = gen_reg_rtx (word_mode);
691 emit_move_insn (data_save[1], get_exception_filter (cfun));
47c84870 692
b2dd096b 693 expand_expr (handler, const0_rtx, VOIDmode, 0);
9c606f69 694
b2dd096b
MM
695 emit_move_insn (cfun->eh->exc_ptr, data_save[0]);
696 emit_move_insn (cfun->eh->filter, data_save[1]);
47c84870 697
b2dd096b
MM
698 if (protect_cleanup_actions)
699 expand_eh_region_end_must_not_throw (protect_cleanup_actions);
a9f0664a 700
b2dd096b
MM
701 /* We need any stack adjustment complete before the around_label. */
702 do_pending_stack_adjust ();
703 }
c10f3adf 704
52a11cbf
RH
705 /* We delay the generation of the _Unwind_Resume until we generate
706 landing pads. We emit a marker here so as to get good control
707 flow data in the meantime. */
47c84870
JM
708 region->resume
709 = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
52a11cbf
RH
710 emit_barrier ();
711
52a11cbf 712 emit_label (around_label);
9c606f69
AM
713}
714
6de9cd9a
DN
715void
716expand_resx_expr (tree exp)
717{
718 int region_nr = TREE_INT_CST_LOW (TREE_OPERAND (exp, 0));
719 struct eh_region *reg = cfun->eh->region_array[region_nr];
720
721 reg->resume = emit_jump_insn (gen_rtx_RESX (VOIDmode, region_nr));
722 emit_barrier ();
723}
724
52a11cbf
RH
725/* End an exception handling region for a try block, and prepares
726 for subsequent calls to expand_start_catch. */
9a0d1e1b 727
52a11cbf 728void
502b8322 729expand_start_all_catch (void)
9a0d1e1b 730{
52a11cbf 731 struct eh_region *region;
9a0d1e1b 732
52a11cbf
RH
733 if (! doing_eh (1))
734 return;
9a0d1e1b 735
52a11cbf
RH
736 region = expand_eh_region_end ();
737 region->type = ERT_TRY;
738 region->u.try.prev_try = cfun->eh->try_region;
739 region->u.try.continue_label = gen_label_rtx ();
9a0d1e1b 740
52a11cbf
RH
741 cfun->eh->try_region = region;
742
743 emit_jump (region->u.try.continue_label);
744}
9a0d1e1b 745
ffad84cd
AH
746/* Begin a catch clause. TYPE is the type caught, a list of such
747 types, (in the case of Java) an ADDR_EXPR which points to the
748 runtime type to match, or null if this is a catch-all
749 clause. Providing a type list enables to associate the catch region
750 with potentially several exception types, which is useful e.g. for
751 Ada. */
9a0d1e1b 752
52a11cbf 753void
502b8322 754expand_start_catch (tree type_or_list)
9a0d1e1b 755{
6de9cd9a
DN
756 struct eh_region *c;
757 rtx note;
52a11cbf
RH
758
759 if (! doing_eh (0))
760 return;
761
6de9cd9a
DN
762 c = gen_eh_region_catch (cfun->eh->try_region, type_or_list);
763 cfun->eh->cur_region = c;
6d41a92f 764
52a11cbf 765 c->label = gen_label_rtx ();
52a11cbf 766 emit_label (c->label);
6de9cd9a
DN
767
768 note = emit_note (NOTE_INSN_EH_REGION_BEG);
769 NOTE_EH_HANDLER (note) = c->region_number;
9a0d1e1b
AM
770}
771
52a11cbf 772/* End a catch clause. Control will resume after the try/catch block. */
9a0d1e1b 773
52a11cbf 774void
502b8322 775expand_end_catch (void)
9a0d1e1b 776{
52a11cbf
RH
777 if (! doing_eh (0))
778 return;
779
4977bab6 780 expand_eh_region_end ();
6de9cd9a 781 emit_jump (cfun->eh->try_region->u.try.continue_label);
9a0d1e1b
AM
782}
783
52a11cbf 784/* End a sequence of catch handlers for a try block. */
9a0d1e1b 785
52a11cbf 786void
502b8322 787expand_end_all_catch (void)
9a0d1e1b 788{
52a11cbf
RH
789 struct eh_region *try_region;
790
791 if (! doing_eh (0))
792 return;
793
794 try_region = cfun->eh->try_region;
795 cfun->eh->try_region = try_region->u.try.prev_try;
796
797 emit_label (try_region->u.try.continue_label);
9a0d1e1b
AM
798}
799
52a11cbf
RH
800/* End an exception region for an exception type filter. ALLOWED is a
801 TREE_LIST of types to be matched by the runtime. FAILURE is an
ff7cc307 802 expression to invoke if a mismatch occurs.
b4e49397
JM
803
804 ??? We could use these semantics for calls to rethrow, too; if we can
805 see the surrounding catch clause, we know that the exception we're
806 rethrowing satisfies the "filter" of the catch type. */
9a0d1e1b 807
52a11cbf 808void
502b8322 809expand_eh_region_end_allowed (tree allowed, tree failure)
9a0d1e1b 810{
52a11cbf
RH
811 struct eh_region *region;
812 rtx around_label;
9a0d1e1b 813
52a11cbf
RH
814 if (! doing_eh (0))
815 return;
e6cfb550 816
52a11cbf
RH
817 region = expand_eh_region_end ();
818 region->type = ERT_ALLOWED_EXCEPTIONS;
819 region->u.allowed.type_list = allowed;
820 region->label = gen_label_rtx ();
9a0d1e1b 821
52a11cbf
RH
822 for (; allowed ; allowed = TREE_CHAIN (allowed))
823 add_type_for_runtime (TREE_VALUE (allowed));
9a0d1e1b 824
52a11cbf
RH
825 /* We must emit the call to FAILURE here, so that if this function
826 throws a different exception, that it will be processed by the
827 correct region. */
9a0d1e1b 828
52a11cbf
RH
829 around_label = gen_label_rtx ();
830 emit_jump (around_label);
831
832 emit_label (region->label);
833 expand_expr (failure, const0_rtx, VOIDmode, EXPAND_NORMAL);
b912bca0
MM
834 /* We must adjust the stack before we reach the AROUND_LABEL because
835 the call to FAILURE does not occur on all paths to the
836 AROUND_LABEL. */
837 do_pending_stack_adjust ();
9a0d1e1b 838
52a11cbf 839 emit_label (around_label);
9a0d1e1b
AM
840}
841
52a11cbf
RH
842/* End an exception region for a must-not-throw filter. FAILURE is an
843 expression invoke if an uncaught exception propagates this far.
e6cfb550 844
52a11cbf
RH
845 This is conceptually identical to expand_eh_region_end_allowed with
846 an empty allowed list (if you passed "std::terminate" instead of
847 "__cxa_call_unexpected"), but they are represented differently in
848 the C++ LSDA. */
6814a8a0 849
52a11cbf 850void
502b8322 851expand_eh_region_end_must_not_throw (tree failure)
e6cfb550 852{
52a11cbf
RH
853 struct eh_region *region;
854 rtx around_label;
e6cfb550 855
52a11cbf
RH
856 if (! doing_eh (0))
857 return;
6814a8a0 858
52a11cbf
RH
859 region = expand_eh_region_end ();
860 region->type = ERT_MUST_NOT_THROW;
861 region->label = gen_label_rtx ();
e6cfb550 862
52a11cbf
RH
863 /* We must emit the call to FAILURE here, so that if this function
864 throws a different exception, that it will be processed by the
865 correct region. */
6814a8a0 866
52a11cbf
RH
867 around_label = gen_label_rtx ();
868 emit_jump (around_label);
6814a8a0 869
52a11cbf
RH
870 emit_label (region->label);
871 expand_expr (failure, const0_rtx, VOIDmode, EXPAND_NORMAL);
6814a8a0 872
52a11cbf 873 emit_label (around_label);
e6cfb550
AM
874}
875
52a11cbf
RH
876/* End an exception region for a throw. No handling goes on here,
877 but it's the easiest way for the front-end to indicate what type
878 is being thrown. */
6814a8a0 879
52a11cbf 880void
502b8322 881expand_eh_region_end_throw (tree type)
e6cfb550 882{
52a11cbf
RH
883 struct eh_region *region;
884
885 if (! doing_eh (0))
886 return;
887
888 region = expand_eh_region_end ();
889 region->type = ERT_THROW;
890 region->u.throw.type = type;
e6cfb550
AM
891}
892
52a11cbf
RH
893/* End a fixup region. Within this region the cleanups for the immediately
894 enclosing region are _not_ run. This is used for goto cleanup to avoid
895 destroying an object twice.
12670d88 896
52a11cbf
RH
897 This would be an extraordinarily simple prospect, were it not for the
898 fact that we don't actually know what the immediately enclosing region
899 is. This surprising fact is because expand_cleanups is currently
900 generating a sequence that it will insert somewhere else. We collect
901 the proper notion of "enclosing" in convert_from_eh_region_ranges. */
4956d07c 902
52a11cbf 903void
502b8322 904expand_eh_region_end_fixup (tree handler)
4956d07c 905{
52a11cbf
RH
906 struct eh_region *fixup;
907
908 if (! doing_eh (0))
909 return;
910
911 fixup = expand_eh_region_end ();
912 fixup->type = ERT_FIXUP;
913 fixup->u.fixup.cleanup_exp = handler;
4956d07c
MS
914}
915
b2dd096b
MM
916/* Note that the current EH region (if any) may contain a throw, or a
917 call to a function which itself may contain a throw. */
918
919void
6de9cd9a 920note_eh_region_may_contain_throw (struct eh_region *region)
b2dd096b 921{
b2dd096b
MM
922 while (region && !region->may_contain_throw)
923 {
924 region->may_contain_throw = 1;
925 region = region->outer;
926 }
927}
928
6de9cd9a
DN
929void
930note_current_region_may_contain_throw (void)
931{
932 note_eh_region_may_contain_throw (cfun->eh->cur_region);
933}
934
935
47c84870 936/* Return an rtl expression for a pointer to the exception object
52a11cbf 937 within a handler. */
4956d07c
MS
938
939rtx
502b8322 940get_exception_pointer (struct function *fun)
4956d07c 941{
86c99549
RH
942 rtx exc_ptr = fun->eh->exc_ptr;
943 if (fun == cfun && ! exc_ptr)
52a11cbf 944 {
26b10ae0 945 exc_ptr = gen_reg_rtx (ptr_mode);
86c99549 946 fun->eh->exc_ptr = exc_ptr;
52a11cbf
RH
947 }
948 return exc_ptr;
949}
4956d07c 950
47c84870
JM
951/* Return an rtl expression for the exception dispatch filter
952 within a handler. */
953
6de9cd9a 954rtx
502b8322 955get_exception_filter (struct function *fun)
47c84870 956{
86c99549
RH
957 rtx filter = fun->eh->filter;
958 if (fun == cfun && ! filter)
47c84870 959 {
041c9d5a 960 filter = gen_reg_rtx (word_mode);
86c99549 961 fun->eh->filter = filter;
47c84870
JM
962 }
963 return filter;
964}
52a11cbf
RH
965\f
966/* This section is for the exception handling specific optimization pass. */
154bba13 967
52a11cbf
RH
968/* Random access the exception region tree. It's just as simple to
969 collect the regions this way as in expand_eh_region_start, but
970 without having to realloc memory. */
154bba13 971
6de9cd9a 972void
502b8322 973collect_eh_region_array (void)
154bba13 974{
52a11cbf 975 struct eh_region **array, *i;
154bba13 976
52a11cbf
RH
977 i = cfun->eh->region_tree;
978 if (! i)
979 return;
154bba13 980
e2500fed
GK
981 array = ggc_alloc_cleared ((cfun->eh->last_region_number + 1)
982 * sizeof (*array));
52a11cbf 983 cfun->eh->region_array = array;
154bba13 984
52a11cbf
RH
985 while (1)
986 {
987 array[i->region_number] = i;
988
989 /* If there are sub-regions, process them. */
990 if (i->inner)
991 i = i->inner;
992 /* If there are peers, process them. */
993 else if (i->next_peer)
994 i = i->next_peer;
995 /* Otherwise, step back up the tree to the next peer. */
996 else
997 {
998 do {
999 i = i->outer;
1000 if (i == NULL)
1001 return;
1002 } while (i->next_peer == NULL);
1003 i = i->next_peer;
1004 }
1005 }
27a36778
MS
1006}
1007
1bddbeb4
RH
1008static void
1009resolve_one_fixup_region (struct eh_region *fixup)
1010{
1011 struct eh_region *cleanup, *real;
1012 int j, n;
1013
1014 n = cfun->eh->last_region_number;
1015 cleanup = 0;
1016
1017 for (j = 1; j <= n; ++j)
1018 {
1019 cleanup = cfun->eh->region_array[j];
1020 if (cleanup && cleanup->type == ERT_CLEANUP
1021 && cleanup->u.cleanup.exp == fixup->u.fixup.cleanup_exp)
1022 break;
1023 }
1024 if (j > n)
1025 abort ();
1026
1027 real = cleanup->outer;
1028 if (real && real->type == ERT_FIXUP)
1029 {
1030 if (!real->u.fixup.resolved)
1031 resolve_one_fixup_region (real);
1032 real = real->u.fixup.real_region;
1033 }
1034
1035 fixup->u.fixup.real_region = real;
1036 fixup->u.fixup.resolved = true;
1037}
1038
52a11cbf 1039static void
502b8322 1040resolve_fixup_regions (void)
27a36778 1041{
1bddbeb4 1042 int i, n = cfun->eh->last_region_number;
27a36778 1043
52a11cbf
RH
1044 for (i = 1; i <= n; ++i)
1045 {
1046 struct eh_region *fixup = cfun->eh->region_array[i];
27a36778 1047
1bddbeb4 1048 if (!fixup || fixup->type != ERT_FIXUP || fixup->u.fixup.resolved)
52a11cbf 1049 continue;
27a36778 1050
1bddbeb4 1051 resolve_one_fixup_region (fixup);
52a11cbf 1052 }
27a36778 1053}
27a36778 1054
52a11cbf
RH
1055/* Now that we've discovered what region actually encloses a fixup,
1056 we can shuffle pointers and remove them from the tree. */
27a36778
MS
1057
1058static void
502b8322 1059remove_fixup_regions (void)
27a36778 1060{
52a11cbf 1061 int i;
45053eaf
RH
1062 rtx insn, note;
1063 struct eh_region *fixup;
27a36778 1064
45053eaf
RH
1065 /* Walk the insn chain and adjust the REG_EH_REGION numbers
1066 for instructions referencing fixup regions. This is only
1067 strictly necessary for fixup regions with no parent, but
1068 doesn't hurt to do it for all regions. */
1069 for (insn = get_insns(); insn ; insn = NEXT_INSN (insn))
1070 if (INSN_P (insn)
1071 && (note = find_reg_note (insn, REG_EH_REGION, NULL))
1072 && INTVAL (XEXP (note, 0)) > 0
1073 && (fixup = cfun->eh->region_array[INTVAL (XEXP (note, 0))])
1074 && fixup->type == ERT_FIXUP)
1075 {
1076 if (fixup->u.fixup.real_region)
2b1e2382 1077 XEXP (note, 0) = GEN_INT (fixup->u.fixup.real_region->region_number);
45053eaf
RH
1078 else
1079 remove_note (insn, note);
1080 }
1081
1082 /* Remove the fixup regions from the tree. */
52a11cbf
RH
1083 for (i = cfun->eh->last_region_number; i > 0; --i)
1084 {
45053eaf 1085 fixup = cfun->eh->region_array[i];
52a11cbf
RH
1086 if (! fixup)
1087 continue;
27a36778 1088
52a11cbf
RH
1089 /* Allow GC to maybe free some memory. */
1090 if (fixup->type == ERT_CLEANUP)
0fb7aeda 1091 fixup->u.cleanup.exp = NULL_TREE;
27a36778 1092
52a11cbf
RH
1093 if (fixup->type != ERT_FIXUP)
1094 continue;
27a36778 1095
52a11cbf
RH
1096 if (fixup->inner)
1097 {
1098 struct eh_region *parent, *p, **pp;
27a36778 1099
52a11cbf 1100 parent = fixup->u.fixup.real_region;
27a36778 1101
52a11cbf
RH
1102 /* Fix up the children's parent pointers; find the end of
1103 the list. */
1104 for (p = fixup->inner; ; p = p->next_peer)
1105 {
1106 p->outer = parent;
1107 if (! p->next_peer)
1108 break;
1109 }
27a36778 1110
52a11cbf
RH
1111 /* In the tree of cleanups, only outer-inner ordering matters.
1112 So link the children back in anywhere at the correct level. */
1113 if (parent)
1114 pp = &parent->inner;
1115 else
1116 pp = &cfun->eh->region_tree;
1117 p->next_peer = *pp;
1118 *pp = fixup->inner;
1119 fixup->inner = NULL;
1120 }
27a36778 1121
52a11cbf
RH
1122 remove_eh_handler (fixup);
1123 }
27a36778
MS
1124}
1125
655dd289
JJ
1126/* Remove all regions whose labels are not reachable from insns. */
1127
1128static void
502b8322 1129remove_unreachable_regions (rtx insns)
655dd289
JJ
1130{
1131 int i, *uid_region_num;
1132 bool *reachable;
1133 struct eh_region *r;
1134 rtx insn;
1135
1136 uid_region_num = xcalloc (get_max_uid (), sizeof(int));
1137 reachable = xcalloc (cfun->eh->last_region_number + 1, sizeof(bool));
1138
1139 for (i = cfun->eh->last_region_number; i > 0; --i)
1140 {
1141 r = cfun->eh->region_array[i];
1142 if (!r || r->region_number != i)
1143 continue;
1144
1145 if (r->resume)
0fb7aeda 1146 {
655dd289
JJ
1147 if (uid_region_num[INSN_UID (r->resume)])
1148 abort ();
1149 uid_region_num[INSN_UID (r->resume)] = i;
0fb7aeda 1150 }
655dd289 1151 if (r->label)
0fb7aeda 1152 {
655dd289
JJ
1153 if (uid_region_num[INSN_UID (r->label)])
1154 abort ();
1155 uid_region_num[INSN_UID (r->label)] = i;
0fb7aeda 1156 }
655dd289
JJ
1157 }
1158
1159 for (insn = insns; insn; insn = NEXT_INSN (insn))
9caad63a
RH
1160 {
1161 reachable[uid_region_num[INSN_UID (insn)]] = true;
1162
1163 if (GET_CODE (insn) == CALL_INSN
1164 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
1165 for (i = 0; i < 3; i++)
1166 {
1167 rtx sub = XEXP (PATTERN (insn), i);
1168 for (; sub ; sub = NEXT_INSN (sub))
1169 reachable[uid_region_num[INSN_UID (sub)]] = true;
1170 }
1171 }
655dd289
JJ
1172
1173 for (i = cfun->eh->last_region_number; i > 0; --i)
1174 {
1175 r = cfun->eh->region_array[i];
1176 if (r && r->region_number == i && !reachable[i])
1177 {
6de9cd9a
DN
1178 bool kill_it = true;
1179 switch (r->type)
1180 {
1181 case ERT_THROW:
1182 /* Don't remove ERT_THROW regions if their outer region
1183 is reachable. */
1184 if (r->outer && reachable[r->outer->region_number])
1185 kill_it = false;
1186 break;
1187
1188 case ERT_MUST_NOT_THROW:
1189 /* MUST_NOT_THROW regions are implementable solely in the
1190 runtime, but their existance continues to affect calls
1191 within that region. Never delete them here. */
1192 kill_it = false;
1193 break;
1194
1195 case ERT_TRY:
1196 {
1197 /* TRY regions are reachable if any of its CATCH regions
1198 are reachable. */
1199 struct eh_region *c;
1200 for (c = r->u.try.catch; c ; c = c->u.catch.next_catch)
1201 if (reachable[c->region_number])
1202 {
1203 kill_it = false;
1204 break;
1205 }
1206 break;
1207 }
655dd289 1208
6de9cd9a
DN
1209 default:
1210 break;
1211 }
1212
1213 if (kill_it)
1214 remove_eh_handler (r);
655dd289
JJ
1215 }
1216 }
1217
1218 free (reachable);
1219 free (uid_region_num);
1220}
1221
52a11cbf
RH
1222/* Turn NOTE_INSN_EH_REGION notes into REG_EH_REGION notes for each
1223 can_throw instruction in the region. */
27a36778
MS
1224
1225static void
502b8322 1226convert_from_eh_region_ranges_1 (rtx *pinsns, int *orig_sp, int cur)
27a36778 1227{
52a11cbf
RH
1228 int *sp = orig_sp;
1229 rtx insn, next;
27a36778 1230
52a11cbf
RH
1231 for (insn = *pinsns; insn ; insn = next)
1232 {
1233 next = NEXT_INSN (insn);
1234 if (GET_CODE (insn) == NOTE)
1235 {
1236 int kind = NOTE_LINE_NUMBER (insn);
1237 if (kind == NOTE_INSN_EH_REGION_BEG
1238 || kind == NOTE_INSN_EH_REGION_END)
1239 {
1240 if (kind == NOTE_INSN_EH_REGION_BEG)
1241 {
1242 struct eh_region *r;
27a36778 1243
52a11cbf
RH
1244 *sp++ = cur;
1245 cur = NOTE_EH_HANDLER (insn);
27a36778 1246
52a11cbf
RH
1247 r = cfun->eh->region_array[cur];
1248 if (r->type == ERT_FIXUP)
1249 {
1250 r = r->u.fixup.real_region;
1251 cur = r ? r->region_number : 0;
1252 }
1253 else if (r->type == ERT_CATCH)
1254 {
1255 r = r->outer;
1256 cur = r ? r->region_number : 0;
1257 }
1258 }
1259 else
1260 cur = *--sp;
1261
1262 /* Removing the first insn of a CALL_PLACEHOLDER sequence
1263 requires extra care to adjust sequence start. */
1264 if (insn == *pinsns)
1265 *pinsns = next;
1266 remove_insn (insn);
1267 continue;
1268 }
1269 }
1270 else if (INSN_P (insn))
1271 {
a944ceb9
RH
1272 if (cur > 0
1273 && ! find_reg_note (insn, REG_EH_REGION, NULL_RTX)
1274 /* Calls can always potentially throw exceptions, unless
1275 they have a REG_EH_REGION note with a value of 0 or less.
1276 Which should be the only possible kind so far. */
1277 && (GET_CODE (insn) == CALL_INSN
1278 /* If we wanted exceptions for non-call insns, then
1279 any may_trap_p instruction could throw. */
1280 || (flag_non_call_exceptions
1281 && GET_CODE (PATTERN (insn)) != CLOBBER
1282 && GET_CODE (PATTERN (insn)) != USE
1283 && may_trap_p (PATTERN (insn)))))
52a11cbf 1284 {
a944ceb9 1285 REG_NOTES (insn) = alloc_EXPR_LIST (REG_EH_REGION, GEN_INT (cur),
52a11cbf
RH
1286 REG_NOTES (insn));
1287 }
27a36778 1288
52a11cbf
RH
1289 if (GET_CODE (insn) == CALL_INSN
1290 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
1291 {
1292 convert_from_eh_region_ranges_1 (&XEXP (PATTERN (insn), 0),
1293 sp, cur);
1294 convert_from_eh_region_ranges_1 (&XEXP (PATTERN (insn), 1),
1295 sp, cur);
1296 convert_from_eh_region_ranges_1 (&XEXP (PATTERN (insn), 2),
1297 sp, cur);
1298 }
1299 }
1300 }
27a36778 1301
52a11cbf
RH
1302 if (sp != orig_sp)
1303 abort ();
1304}
27a36778 1305
6de9cd9a
DN
1306static void
1307collect_rtl_labels_from_trees (void)
1308{
1309 int i, n = cfun->eh->last_region_number;
1310 for (i = 1; i <= n; ++i)
1311 {
1312 struct eh_region *reg = cfun->eh->region_array[i];
1313 if (reg && reg->tree_label)
1314 reg->label = DECL_RTL_IF_SET (reg->tree_label);
1315 }
1316}
1317
52a11cbf 1318void
502b8322 1319convert_from_eh_region_ranges (void)
52a11cbf 1320{
6de9cd9a
DN
1321 rtx insns = get_insns ();
1322
1323 if (cfun->eh->region_array)
1324 {
1325 /* If the region array already exists, assume we're coming from
1326 optimize_function_tree. In this case all we need to do is
1327 collect the rtl labels that correspond to the tree labels
1328 that we allocated earlier. */
1329 collect_rtl_labels_from_trees ();
1330 }
1331 else
1332 {
1333 int *stack;
27a36778 1334
6de9cd9a
DN
1335 collect_eh_region_array ();
1336 resolve_fixup_regions ();
27a36778 1337
6de9cd9a
DN
1338 stack = xmalloc (sizeof (int) * (cfun->eh->last_region_number + 1));
1339 convert_from_eh_region_ranges_1 (&insns, stack, 0);
1340 free (stack);
1341
1342 remove_fixup_regions ();
1343 }
27a36778 1344
655dd289 1345 remove_unreachable_regions (insns);
27a36778
MS
1346}
1347
6a58eee9 1348static void
502b8322 1349add_ehl_entry (rtx label, struct eh_region *region)
6a58eee9
RH
1350{
1351 struct ehl_map_entry **slot, *entry;
1352
1353 LABEL_PRESERVE_P (label) = 1;
1354
703ad42b 1355 entry = ggc_alloc (sizeof (*entry));
6a58eee9
RH
1356 entry->label = label;
1357 entry->region = region;
1358
1359 slot = (struct ehl_map_entry **)
e2500fed 1360 htab_find_slot (cfun->eh->exception_handler_label_map, entry, INSERT);
6f3d0447
RH
1361
1362 /* Before landing pad creation, each exception handler has its own
1363 label. After landing pad creation, the exception handlers may
1364 share landing pads. This is ok, since maybe_remove_eh_handler
1365 only requires the 1-1 mapping before landing pad creation. */
1366 if (*slot && !cfun->eh->built_landing_pads)
6a58eee9 1367 abort ();
6f3d0447 1368
6a58eee9
RH
1369 *slot = entry;
1370}
1371
52a11cbf 1372void
502b8322 1373find_exception_handler_labels (void)
27a36778 1374{
52a11cbf 1375 int i;
27a36778 1376
e2500fed
GK
1377 if (cfun->eh->exception_handler_label_map)
1378 htab_empty (cfun->eh->exception_handler_label_map);
6a58eee9
RH
1379 else
1380 {
1381 /* ??? The expansion factor here (3/2) must be greater than the htab
1382 occupancy factor (4/3) to avoid unnecessary resizing. */
e2500fed
GK
1383 cfun->eh->exception_handler_label_map
1384 = htab_create_ggc (cfun->eh->last_region_number * 3 / 2,
1385 ehl_hash, ehl_eq, NULL);
6a58eee9 1386 }
27a36778 1387
52a11cbf
RH
1388 if (cfun->eh->region_tree == NULL)
1389 return;
27a36778 1390
52a11cbf
RH
1391 for (i = cfun->eh->last_region_number; i > 0; --i)
1392 {
1393 struct eh_region *region = cfun->eh->region_array[i];
1394 rtx lab;
27a36778 1395
655dd289 1396 if (! region || region->region_number != i)
52a11cbf
RH
1397 continue;
1398 if (cfun->eh->built_landing_pads)
1399 lab = region->landing_pad;
1400 else
1401 lab = region->label;
27a36778 1402
52a11cbf 1403 if (lab)
6a58eee9 1404 add_ehl_entry (lab, region);
27a36778
MS
1405 }
1406
52a11cbf
RH
1407 /* For sjlj exceptions, need the return label to remain live until
1408 after landing pad generation. */
1409 if (USING_SJLJ_EXCEPTIONS && ! cfun->eh->built_landing_pads)
6a58eee9 1410 add_ehl_entry (return_label, NULL);
27a36778
MS
1411}
1412
93f82d60 1413bool
502b8322 1414current_function_has_exception_handlers (void)
93f82d60
RH
1415{
1416 int i;
1417
1418 for (i = cfun->eh->last_region_number; i > 0; --i)
1419 {
1420 struct eh_region *region = cfun->eh->region_array[i];
1421
1422 if (! region || region->region_number != i)
1423 continue;
1424 if (region->type != ERT_THROW)
1425 return true;
1426 }
1427
1428 return false;
1429}
52a11cbf
RH
1430\f
1431static struct eh_region *
502b8322 1432duplicate_eh_region_1 (struct eh_region *o, struct inline_remap *map)
4956d07c 1433{
703ad42b 1434 struct eh_region *n = ggc_alloc_cleared (sizeof (struct eh_region));
4956d07c 1435
52a11cbf
RH
1436 n->region_number = o->region_number + cfun->eh->last_region_number;
1437 n->type = o->type;
4956d07c 1438
52a11cbf
RH
1439 switch (n->type)
1440 {
1441 case ERT_CLEANUP:
1442 case ERT_MUST_NOT_THROW:
1443 break;
27a36778 1444
52a11cbf
RH
1445 case ERT_TRY:
1446 if (o->u.try.continue_label)
1447 n->u.try.continue_label
1448 = get_label_from_map (map,
1449 CODE_LABEL_NUMBER (o->u.try.continue_label));
1450 break;
27a36778 1451
52a11cbf 1452 case ERT_CATCH:
6d41a92f 1453 n->u.catch.type_list = o->u.catch.type_list;
52a11cbf 1454 break;
27a36778 1455
52a11cbf
RH
1456 case ERT_ALLOWED_EXCEPTIONS:
1457 n->u.allowed.type_list = o->u.allowed.type_list;
1458 break;
1459
1460 case ERT_THROW:
1461 n->u.throw.type = o->u.throw.type;
3f2c5d1a 1462
52a11cbf
RH
1463 default:
1464 abort ();
1465 }
1466
1467 if (o->label)
1468 n->label = get_label_from_map (map, CODE_LABEL_NUMBER (o->label));
47c84870 1469 if (o->resume)
e7b9b18e 1470 {
47c84870
JM
1471 n->resume = map->insn_map[INSN_UID (o->resume)];
1472 if (n->resume == NULL)
52a11cbf 1473 abort ();
27a36778 1474 }
4956d07c 1475
52a11cbf 1476 return n;
4956d07c
MS
1477}
1478
52a11cbf 1479static void
502b8322 1480duplicate_eh_region_2 (struct eh_region *o, struct eh_region **n_array)
4c581243 1481{
52a11cbf 1482 struct eh_region *n = n_array[o->region_number];
4c581243 1483
52a11cbf
RH
1484 switch (n->type)
1485 {
1486 case ERT_TRY:
1487 n->u.try.catch = n_array[o->u.try.catch->region_number];
1488 n->u.try.last_catch = n_array[o->u.try.last_catch->region_number];
1489 break;
12670d88 1490
52a11cbf
RH
1491 case ERT_CATCH:
1492 if (o->u.catch.next_catch)
0fb7aeda 1493 n->u.catch.next_catch = n_array[o->u.catch.next_catch->region_number];
52a11cbf 1494 if (o->u.catch.prev_catch)
0fb7aeda 1495 n->u.catch.prev_catch = n_array[o->u.catch.prev_catch->region_number];
52a11cbf 1496 break;
12670d88 1497
52a11cbf
RH
1498 default:
1499 break;
1500 }
4956d07c 1501
52a11cbf
RH
1502 if (o->outer)
1503 n->outer = n_array[o->outer->region_number];
1504 if (o->inner)
1505 n->inner = n_array[o->inner->region_number];
1506 if (o->next_peer)
1507 n->next_peer = n_array[o->next_peer->region_number];
3f2c5d1a 1508}
52a11cbf
RH
1509
1510int
502b8322 1511duplicate_eh_regions (struct function *ifun, struct inline_remap *map)
4956d07c 1512{
52a11cbf
RH
1513 int ifun_last_region_number = ifun->eh->last_region_number;
1514 struct eh_region **n_array, *root, *cur;
1515 int i;
4956d07c 1516
52a11cbf
RH
1517 if (ifun_last_region_number == 0)
1518 return 0;
4956d07c 1519
52a11cbf 1520 n_array = xcalloc (ifun_last_region_number + 1, sizeof (*n_array));
4956d07c 1521
52a11cbf 1522 for (i = 1; i <= ifun_last_region_number; ++i)
27a36778 1523 {
52a11cbf
RH
1524 cur = ifun->eh->region_array[i];
1525 if (!cur || cur->region_number != i)
1526 continue;
1527 n_array[i] = duplicate_eh_region_1 (cur, map);
27a36778 1528 }
52a11cbf 1529 for (i = 1; i <= ifun_last_region_number; ++i)
27a36778 1530 {
52a11cbf
RH
1531 cur = ifun->eh->region_array[i];
1532 if (!cur || cur->region_number != i)
1533 continue;
1534 duplicate_eh_region_2 (cur, n_array);
1535 }
27a36778 1536
52a11cbf
RH
1537 root = n_array[ifun->eh->region_tree->region_number];
1538 cur = cfun->eh->cur_region;
1539 if (cur)
1540 {
1541 struct eh_region *p = cur->inner;
1542 if (p)
1543 {
1544 while (p->next_peer)
1545 p = p->next_peer;
1546 p->next_peer = root;
1547 }
1548 else
1549 cur->inner = root;
27a36778 1550
52a11cbf 1551 for (i = 1; i <= ifun_last_region_number; ++i)
b24a9e88 1552 if (n_array[i] && n_array[i]->outer == NULL)
52a11cbf
RH
1553 n_array[i]->outer = cur;
1554 }
1555 else
1556 {
1557 struct eh_region *p = cfun->eh->region_tree;
1558 if (p)
1559 {
1560 while (p->next_peer)
1561 p = p->next_peer;
1562 p->next_peer = root;
1563 }
1564 else
1565 cfun->eh->region_tree = root;
27a36778 1566 }
1e4ceb6f 1567
52a11cbf 1568 free (n_array);
1e4ceb6f 1569
52a11cbf
RH
1570 i = cfun->eh->last_region_number;
1571 cfun->eh->last_region_number = i + ifun_last_region_number;
1572 return i;
4956d07c
MS
1573}
1574
52a11cbf 1575\f
52a11cbf 1576static int
502b8322 1577t2r_eq (const void *pentry, const void *pdata)
9762d48d 1578{
52a11cbf
RH
1579 tree entry = (tree) pentry;
1580 tree data = (tree) pdata;
9762d48d 1581
52a11cbf 1582 return TREE_PURPOSE (entry) == data;
9762d48d
JM
1583}
1584
52a11cbf 1585static hashval_t
502b8322 1586t2r_hash (const void *pentry)
52a11cbf
RH
1587{
1588 tree entry = (tree) pentry;
fd917e0d 1589 return TREE_HASH (TREE_PURPOSE (entry));
52a11cbf 1590}
9762d48d 1591
52a11cbf 1592static void
502b8322 1593add_type_for_runtime (tree type)
52a11cbf
RH
1594{
1595 tree *slot;
9762d48d 1596
52a11cbf 1597 slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
fd917e0d 1598 TREE_HASH (type), INSERT);
52a11cbf
RH
1599 if (*slot == NULL)
1600 {
1601 tree runtime = (*lang_eh_runtime_type) (type);
1602 *slot = tree_cons (type, runtime, NULL_TREE);
1603 }
1604}
3f2c5d1a 1605
52a11cbf 1606static tree
502b8322 1607lookup_type_for_runtime (tree type)
52a11cbf
RH
1608{
1609 tree *slot;
b37f006b 1610
52a11cbf 1611 slot = (tree *) htab_find_slot_with_hash (type_to_runtime_map, type,
fd917e0d 1612 TREE_HASH (type), NO_INSERT);
b37f006b 1613
a1f300c0 1614 /* We should have always inserted the data earlier. */
52a11cbf
RH
1615 return TREE_VALUE (*slot);
1616}
9762d48d 1617
52a11cbf
RH
1618\f
1619/* Represent an entry in @TTypes for either catch actions
1620 or exception filter actions. */
e2500fed 1621struct ttypes_filter GTY(())
52a11cbf
RH
1622{
1623 tree t;
1624 int filter;
1625};
b37f006b 1626
52a11cbf
RH
1627/* Compare ENTRY (a ttypes_filter entry in the hash table) with DATA
1628 (a tree) for a @TTypes type node we are thinking about adding. */
b37f006b 1629
52a11cbf 1630static int
502b8322 1631ttypes_filter_eq (const void *pentry, const void *pdata)
52a11cbf
RH
1632{
1633 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1634 tree data = (tree) pdata;
b37f006b 1635
52a11cbf 1636 return entry->t == data;
9762d48d
JM
1637}
1638
52a11cbf 1639static hashval_t
502b8322 1640ttypes_filter_hash (const void *pentry)
52a11cbf
RH
1641{
1642 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
fd917e0d 1643 return TREE_HASH (entry->t);
52a11cbf 1644}
4956d07c 1645
52a11cbf
RH
1646/* Compare ENTRY with DATA (both struct ttypes_filter) for a @TTypes
1647 exception specification list we are thinking about adding. */
1648/* ??? Currently we use the type lists in the order given. Someone
1649 should put these in some canonical order. */
1650
1651static int
502b8322 1652ehspec_filter_eq (const void *pentry, const void *pdata)
4956d07c 1653{
52a11cbf
RH
1654 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1655 const struct ttypes_filter *data = (const struct ttypes_filter *) pdata;
1656
1657 return type_list_equal (entry->t, data->t);
4956d07c
MS
1658}
1659
52a11cbf 1660/* Hash function for exception specification lists. */
4956d07c 1661
52a11cbf 1662static hashval_t
502b8322 1663ehspec_filter_hash (const void *pentry)
4956d07c 1664{
52a11cbf
RH
1665 const struct ttypes_filter *entry = (const struct ttypes_filter *) pentry;
1666 hashval_t h = 0;
1667 tree list;
1668
1669 for (list = entry->t; list ; list = TREE_CHAIN (list))
fd917e0d 1670 h = (h << 5) + (h >> 27) + TREE_HASH (TREE_VALUE (list));
52a11cbf 1671 return h;
4956d07c
MS
1672}
1673
fd917e0d
JM
1674/* Add TYPE (which may be NULL) to cfun->eh->ttype_data, using TYPES_HASH
1675 to speed up the search. Return the filter value to be used. */
4956d07c 1676
52a11cbf 1677static int
502b8322 1678add_ttypes_entry (htab_t ttypes_hash, tree type)
4956d07c 1679{
52a11cbf 1680 struct ttypes_filter **slot, *n;
4956d07c 1681
52a11cbf 1682 slot = (struct ttypes_filter **)
fd917e0d 1683 htab_find_slot_with_hash (ttypes_hash, type, TREE_HASH (type), INSERT);
52a11cbf
RH
1684
1685 if ((n = *slot) == NULL)
4956d07c 1686 {
52a11cbf 1687 /* Filter value is a 1 based table index. */
12670d88 1688
703ad42b 1689 n = xmalloc (sizeof (*n));
52a11cbf
RH
1690 n->t = type;
1691 n->filter = VARRAY_ACTIVE_SIZE (cfun->eh->ttype_data) + 1;
1692 *slot = n;
1693
1694 VARRAY_PUSH_TREE (cfun->eh->ttype_data, type);
4956d07c 1695 }
52a11cbf
RH
1696
1697 return n->filter;
4956d07c
MS
1698}
1699
52a11cbf
RH
1700/* Add LIST to cfun->eh->ehspec_data, using EHSPEC_HASH and TYPES_HASH
1701 to speed up the search. Return the filter value to be used. */
1702
1703static int
502b8322 1704add_ehspec_entry (htab_t ehspec_hash, htab_t ttypes_hash, tree list)
12670d88 1705{
52a11cbf
RH
1706 struct ttypes_filter **slot, *n;
1707 struct ttypes_filter dummy;
12670d88 1708
52a11cbf
RH
1709 dummy.t = list;
1710 slot = (struct ttypes_filter **)
1711 htab_find_slot (ehspec_hash, &dummy, INSERT);
1712
1713 if ((n = *slot) == NULL)
1714 {
1715 /* Filter value is a -1 based byte index into a uleb128 buffer. */
1716
703ad42b 1717 n = xmalloc (sizeof (*n));
52a11cbf
RH
1718 n->t = list;
1719 n->filter = -(VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data) + 1);
1720 *slot = n;
1721
1722 /* Look up each type in the list and encode its filter
1723 value as a uleb128. Terminate the list with 0. */
1724 for (; list ; list = TREE_CHAIN (list))
3f2c5d1a 1725 push_uleb128 (&cfun->eh->ehspec_data,
52a11cbf
RH
1726 add_ttypes_entry (ttypes_hash, TREE_VALUE (list)));
1727 VARRAY_PUSH_UCHAR (cfun->eh->ehspec_data, 0);
1728 }
1729
1730 return n->filter;
12670d88
RK
1731}
1732
52a11cbf
RH
1733/* Generate the action filter values to be used for CATCH and
1734 ALLOWED_EXCEPTIONS regions. When using dwarf2 exception regions,
1735 we use lots of landing pads, and so every type or list can share
1736 the same filter value, which saves table space. */
1737
1738static void
502b8322 1739assign_filter_values (void)
9a0d1e1b 1740{
52a11cbf
RH
1741 int i;
1742 htab_t ttypes, ehspec;
9a9deafc 1743
52a11cbf
RH
1744 VARRAY_TREE_INIT (cfun->eh->ttype_data, 16, "ttype_data");
1745 VARRAY_UCHAR_INIT (cfun->eh->ehspec_data, 64, "ehspec_data");
9a9deafc 1746
52a11cbf
RH
1747 ttypes = htab_create (31, ttypes_filter_hash, ttypes_filter_eq, free);
1748 ehspec = htab_create (31, ehspec_filter_hash, ehspec_filter_eq, free);
9a0d1e1b 1749
52a11cbf
RH
1750 for (i = cfun->eh->last_region_number; i > 0; --i)
1751 {
1752 struct eh_region *r = cfun->eh->region_array[i];
9a0d1e1b 1753
52a11cbf
RH
1754 /* Mind we don't process a region more than once. */
1755 if (!r || r->region_number != i)
1756 continue;
9a0d1e1b 1757
52a11cbf
RH
1758 switch (r->type)
1759 {
1760 case ERT_CATCH:
6d41a92f
OH
1761 /* Whatever type_list is (NULL or true list), we build a list
1762 of filters for the region. */
1763 r->u.catch.filter_list = NULL_TREE;
1764
1765 if (r->u.catch.type_list != NULL)
1766 {
1767 /* Get a filter value for each of the types caught and store
1768 them in the region's dedicated list. */
1769 tree tp_node = r->u.catch.type_list;
1770
1771 for (;tp_node; tp_node = TREE_CHAIN (tp_node))
1772 {
1773 int flt = add_ttypes_entry (ttypes, TREE_VALUE (tp_node));
1774 tree flt_node = build_int_2 (flt, 0);
3f2c5d1a
RS
1775
1776 r->u.catch.filter_list
6d41a92f
OH
1777 = tree_cons (NULL_TREE, flt_node, r->u.catch.filter_list);
1778 }
1779 }
1780 else
1781 {
1782 /* Get a filter value for the NULL list also since it will need
1783 an action record anyway. */
1784 int flt = add_ttypes_entry (ttypes, NULL);
1785 tree flt_node = build_int_2 (flt, 0);
3f2c5d1a
RS
1786
1787 r->u.catch.filter_list
6d41a92f
OH
1788 = tree_cons (NULL_TREE, flt_node, r->u.catch.filter_list);
1789 }
3f2c5d1a 1790
52a11cbf 1791 break;
bf71cd2e 1792
52a11cbf
RH
1793 case ERT_ALLOWED_EXCEPTIONS:
1794 r->u.allowed.filter
1795 = add_ehspec_entry (ehspec, ttypes, r->u.allowed.type_list);
1796 break;
bf71cd2e 1797
52a11cbf
RH
1798 default:
1799 break;
1800 }
1801 }
1802
1803 htab_delete (ttypes);
1804 htab_delete (ehspec);
1805}
1806
12c3874e
JH
1807/* Emit SEQ into basic block just before INSN (that is assumed to be
1808 first instruction of some existing BB and return the newly
1809 produced block. */
1810static basic_block
1811emit_to_new_bb_before (rtx seq, rtx insn)
1812{
1813 rtx last;
1814 basic_block bb;
a61bf177
JH
1815 edge e;
1816
1817 /* If there happens to be an fallthru edge (possibly created by cleanup_cfg
1818 call), we don't want it to go into newly created landing pad or other EH
1819 construct. */
1820 for (e = BLOCK_FOR_INSN (insn)->pred; e; e = e->pred_next)
1821 if (e->flags & EDGE_FALLTHRU)
1822 force_nonfallthru (e);
12c3874e
JH
1823 last = emit_insn_before (seq, insn);
1824 if (GET_CODE (last) == BARRIER)
1825 last = PREV_INSN (last);
1826 bb = create_basic_block (seq, last, BLOCK_FOR_INSN (insn)->prev_bb);
1827 update_bb_for_insn (bb);
1828 bb->flags |= BB_SUPERBLOCK;
1829 return bb;
1830}
1831
ac850948
JM
1832/* Generate the code to actually handle exceptions, which will follow the
1833 landing pads. */
1834
52a11cbf 1835static void
502b8322 1836build_post_landing_pads (void)
52a11cbf
RH
1837{
1838 int i;
bf71cd2e 1839
52a11cbf 1840 for (i = cfun->eh->last_region_number; i > 0; --i)
bf71cd2e 1841 {
52a11cbf
RH
1842 struct eh_region *region = cfun->eh->region_array[i];
1843 rtx seq;
bf71cd2e 1844
52a11cbf
RH
1845 /* Mind we don't process a region more than once. */
1846 if (!region || region->region_number != i)
1847 continue;
1848
1849 switch (region->type)
987009bf 1850 {
52a11cbf
RH
1851 case ERT_TRY:
1852 /* ??? Collect the set of all non-overlapping catch handlers
1853 all the way up the chain until blocked by a cleanup. */
1854 /* ??? Outer try regions can share landing pads with inner
1855 try regions if the types are completely non-overlapping,
a1f300c0 1856 and there are no intervening cleanups. */
bf71cd2e 1857
52a11cbf 1858 region->post_landing_pad = gen_label_rtx ();
bf71cd2e 1859
52a11cbf 1860 start_sequence ();
bf71cd2e 1861
52a11cbf 1862 emit_label (region->post_landing_pad);
bf71cd2e 1863
52a11cbf
RH
1864 /* ??? It is mighty inconvenient to call back into the
1865 switch statement generation code in expand_end_case.
1866 Rapid prototyping sez a sequence of ifs. */
1867 {
1868 struct eh_region *c;
1869 for (c = region->u.try.catch; c ; c = c->u.catch.next_catch)
1870 {
6d41a92f 1871 if (c->u.catch.type_list == NULL)
a944ceb9 1872 emit_jump (c->label);
52a11cbf 1873 else
6d41a92f
OH
1874 {
1875 /* Need for one cmp/jump per type caught. Each type
1876 list entry has a matching entry in the filter list
1877 (see assign_filter_values). */
1878 tree tp_node = c->u.catch.type_list;
1879 tree flt_node = c->u.catch.filter_list;
1880
1881 for (; tp_node; )
1882 {
1883 emit_cmp_and_jump_insns
1884 (cfun->eh->filter,
1885 GEN_INT (tree_low_cst (TREE_VALUE (flt_node), 0)),
1886 EQ, NULL_RTX, word_mode, 0, c->label);
1887
1888 tp_node = TREE_CHAIN (tp_node);
1889 flt_node = TREE_CHAIN (flt_node);
1890 }
1891 }
52a11cbf
RH
1892 }
1893 }
bf71cd2e 1894
47c84870
JM
1895 /* We delay the generation of the _Unwind_Resume until we generate
1896 landing pads. We emit a marker here so as to get good control
1897 flow data in the meantime. */
1898 region->resume
1899 = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
1900 emit_barrier ();
1901
52a11cbf
RH
1902 seq = get_insns ();
1903 end_sequence ();
e6cfb550 1904
12c3874e
JH
1905 emit_to_new_bb_before (seq, region->u.try.catch->label);
1906
52a11cbf 1907 break;
bf71cd2e 1908
52a11cbf
RH
1909 case ERT_ALLOWED_EXCEPTIONS:
1910 region->post_landing_pad = gen_label_rtx ();
9a0d1e1b 1911
52a11cbf 1912 start_sequence ();
f54a7f6f 1913
52a11cbf 1914 emit_label (region->post_landing_pad);
f54a7f6f 1915
52a11cbf
RH
1916 emit_cmp_and_jump_insns (cfun->eh->filter,
1917 GEN_INT (region->u.allowed.filter),
a06ef755 1918 EQ, NULL_RTX, word_mode, 0, region->label);
f54a7f6f 1919
47c84870
JM
1920 /* We delay the generation of the _Unwind_Resume until we generate
1921 landing pads. We emit a marker here so as to get good control
1922 flow data in the meantime. */
1923 region->resume
1924 = emit_jump_insn (gen_rtx_RESX (VOIDmode, region->region_number));
1925 emit_barrier ();
1926
52a11cbf
RH
1927 seq = get_insns ();
1928 end_sequence ();
1929
12c3874e 1930 emit_to_new_bb_before (seq, region->label);
52a11cbf 1931 break;
f54a7f6f 1932
52a11cbf 1933 case ERT_CLEANUP:
125ca8fd 1934 case ERT_MUST_NOT_THROW:
a944ceb9 1935 region->post_landing_pad = region->label;
125ca8fd
RH
1936 break;
1937
52a11cbf
RH
1938 case ERT_CATCH:
1939 case ERT_THROW:
1940 /* Nothing to do. */
1941 break;
1942
1943 default:
1944 abort ();
1945 }
1946 }
1947}
1e4ceb6f 1948
47c84870
JM
1949/* Replace RESX patterns with jumps to the next handler if any, or calls to
1950 _Unwind_Resume otherwise. */
1951
1e4ceb6f 1952static void
502b8322 1953connect_post_landing_pads (void)
1e4ceb6f 1954{
52a11cbf 1955 int i;
76fc91c7 1956
52a11cbf
RH
1957 for (i = cfun->eh->last_region_number; i > 0; --i)
1958 {
1959 struct eh_region *region = cfun->eh->region_array[i];
1960 struct eh_region *outer;
47c84870 1961 rtx seq;
12c3874e 1962 rtx barrier;
1e4ceb6f 1963
52a11cbf
RH
1964 /* Mind we don't process a region more than once. */
1965 if (!region || region->region_number != i)
1966 continue;
1e4ceb6f 1967
47c84870
JM
1968 /* If there is no RESX, or it has been deleted by flow, there's
1969 nothing to fix up. */
1970 if (! region->resume || INSN_DELETED_P (region->resume))
52a11cbf 1971 continue;
76fc91c7 1972
52a11cbf
RH
1973 /* Search for another landing pad in this function. */
1974 for (outer = region->outer; outer ; outer = outer->outer)
1975 if (outer->post_landing_pad)
1976 break;
1e4ceb6f 1977
52a11cbf 1978 start_sequence ();
12670d88 1979
52a11cbf 1980 if (outer)
12c3874e
JH
1981 {
1982 edge e;
1983 basic_block src, dest;
1984
1985 emit_jump (outer->post_landing_pad);
1986 src = BLOCK_FOR_INSN (region->resume);
1987 dest = BLOCK_FOR_INSN (outer->post_landing_pad);
1988 while (src->succ)
1989 remove_edge (src->succ);
1990 e = make_edge (src, dest, 0);
1991 e->probability = REG_BR_PROB_BASE;
1992 e->count = src->count;
1993 }
52a11cbf 1994 else
29c246a7
HPN
1995 {
1996 emit_library_call (unwind_resume_libfunc, LCT_THROW,
1997 VOIDmode, 1, cfun->eh->exc_ptr, ptr_mode);
1998
1999 /* What we just emitted was a throwing libcall, so it got a
2000 barrier automatically added after it. If the last insn in
2001 the libcall sequence isn't the barrier, it's because the
2002 target emits multiple insns for a call, and there are insns
2003 after the actual call insn (which are redundant and would be
2004 optimized away). The barrier is inserted exactly after the
2005 call insn, so let's go get that and delete the insns after
2006 it, because below we need the barrier to be the last insn in
2007 the sequence. */
2008 delete_insns_since (NEXT_INSN (last_call_insn ()));
2009 }
4956d07c 2010
52a11cbf
RH
2011 seq = get_insns ();
2012 end_sequence ();
12c3874e
JH
2013 barrier = emit_insn_before (seq, region->resume);
2014 /* Avoid duplicate barrier. */
2015 if (GET_CODE (barrier) != BARRIER)
2016 abort ();
2017 delete_insn (barrier);
53c17031 2018 delete_insn (region->resume);
6de9cd9a
DN
2019
2020 /* ??? From tree-ssa we can wind up with catch regions whose
2021 label is not instantiated, but whose resx is present. Now
2022 that we've dealt with the resx, kill the region. */
2023 if (region->label == NULL && region->type == ERT_CLEANUP)
2024 remove_eh_handler (region);
52a11cbf
RH
2025 }
2026}
2027
2028\f
2029static void
502b8322 2030dw2_build_landing_pads (void)
4956d07c 2031{
ae0ed63a
JM
2032 int i;
2033 unsigned int j;
4956d07c 2034
52a11cbf
RH
2035 for (i = cfun->eh->last_region_number; i > 0; --i)
2036 {
2037 struct eh_region *region = cfun->eh->region_array[i];
2038 rtx seq;
12c3874e 2039 basic_block bb;
5c701bb1 2040 bool clobbers_hard_regs = false;
12c3874e 2041 edge e;
4956d07c 2042
52a11cbf
RH
2043 /* Mind we don't process a region more than once. */
2044 if (!region || region->region_number != i)
2045 continue;
1418bb67 2046
52a11cbf
RH
2047 if (region->type != ERT_CLEANUP
2048 && region->type != ERT_TRY
2049 && region->type != ERT_ALLOWED_EXCEPTIONS)
2050 continue;
12670d88 2051
52a11cbf 2052 start_sequence ();
4956d07c 2053
52a11cbf
RH
2054 region->landing_pad = gen_label_rtx ();
2055 emit_label (region->landing_pad);
4956d07c 2056
52a11cbf
RH
2057#ifdef HAVE_exception_receiver
2058 if (HAVE_exception_receiver)
2059 emit_insn (gen_exception_receiver ());
2060 else
2061#endif
2062#ifdef HAVE_nonlocal_goto_receiver
2063 if (HAVE_nonlocal_goto_receiver)
2064 emit_insn (gen_nonlocal_goto_receiver ());
2065 else
2066#endif
2067 { /* Nothing */ }
4956d07c 2068
52a11cbf
RH
2069 /* If the eh_return data registers are call-saved, then we
2070 won't have considered them clobbered from the call that
2071 threw. Kill them now. */
2072 for (j = 0; ; ++j)
2073 {
2074 unsigned r = EH_RETURN_DATA_REGNO (j);
2075 if (r == INVALID_REGNUM)
2076 break;
2077 if (! call_used_regs[r])
5c701bb1
JS
2078 {
2079 emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (Pmode, r)));
2080 clobbers_hard_regs = true;
2081 }
2082 }
2083
2084 if (clobbers_hard_regs)
2085 {
2086 /* @@@ This is a kludge. Not all machine descriptions define a
2087 blockage insn, but we must not allow the code we just generated
2088 to be reordered by scheduling. So emit an ASM_INPUT to act as
2ba84f36 2089 blockage insn. */
5c701bb1 2090 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
52a11cbf 2091 }
e701eb4d 2092
52a11cbf 2093 emit_move_insn (cfun->eh->exc_ptr,
26b10ae0 2094 gen_rtx_REG (ptr_mode, EH_RETURN_DATA_REGNO (0)));
52a11cbf 2095 emit_move_insn (cfun->eh->filter,
9e800206 2096 gen_rtx_REG (word_mode, EH_RETURN_DATA_REGNO (1)));
9a0d1e1b 2097
52a11cbf
RH
2098 seq = get_insns ();
2099 end_sequence ();
5816cb14 2100
12c3874e
JH
2101 bb = emit_to_new_bb_before (seq, region->post_landing_pad);
2102 e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
2103 e->count = bb->count;
2104 e->probability = REG_BR_PROB_BASE;
52a11cbf 2105 }
4956d07c
MS
2106}
2107
52a11cbf
RH
2108\f
2109struct sjlj_lp_info
2110{
2111 int directly_reachable;
2112 int action_index;
2113 int dispatch_index;
2114 int call_site_index;
2115};
4956d07c 2116
52a11cbf 2117static bool
502b8322 2118sjlj_find_directly_reachable_regions (struct sjlj_lp_info *lp_info)
4956d07c 2119{
52a11cbf
RH
2120 rtx insn;
2121 bool found_one = false;
4956d07c 2122
52a11cbf
RH
2123 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
2124 {
2125 struct eh_region *region;
98ce21b3 2126 enum reachable_code rc;
52a11cbf
RH
2127 tree type_thrown;
2128 rtx note;
4956d07c 2129
52a11cbf
RH
2130 if (! INSN_P (insn))
2131 continue;
0d3453df 2132
52a11cbf
RH
2133 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2134 if (!note || INTVAL (XEXP (note, 0)) <= 0)
2135 continue;
5dfa7520 2136
52a11cbf 2137 region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
5dfa7520 2138
52a11cbf
RH
2139 type_thrown = NULL_TREE;
2140 if (region->type == ERT_THROW)
2141 {
2142 type_thrown = region->u.throw.type;
2143 region = region->outer;
2144 }
12670d88 2145
52a11cbf
RH
2146 /* Find the first containing region that might handle the exception.
2147 That's the landing pad to which we will transfer control. */
98ce21b3 2148 rc = RNL_NOT_CAUGHT;
52a11cbf 2149 for (; region; region = region->outer)
98ce21b3 2150 {
6de9cd9a 2151 rc = reachable_next_level (region, type_thrown, NULL);
98ce21b3
RH
2152 if (rc != RNL_NOT_CAUGHT)
2153 break;
2154 }
2155 if (rc == RNL_MAYBE_CAUGHT || rc == RNL_CAUGHT)
52a11cbf
RH
2156 {
2157 lp_info[region->region_number].directly_reachable = 1;
2158 found_one = true;
2159 }
2160 }
4956d07c 2161
52a11cbf
RH
2162 return found_one;
2163}
e701eb4d
JM
2164
2165static void
502b8322 2166sjlj_assign_call_site_values (rtx dispatch_label, struct sjlj_lp_info *lp_info)
e701eb4d 2167{
52a11cbf
RH
2168 htab_t ar_hash;
2169 int i, index;
2170
2171 /* First task: build the action table. */
2172
2173 VARRAY_UCHAR_INIT (cfun->eh->action_record_data, 64, "action_record_data");
2174 ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
2175
2176 for (i = cfun->eh->last_region_number; i > 0; --i)
2177 if (lp_info[i].directly_reachable)
e6cfb550 2178 {
52a11cbf
RH
2179 struct eh_region *r = cfun->eh->region_array[i];
2180 r->landing_pad = dispatch_label;
2181 lp_info[i].action_index = collect_one_action_chain (ar_hash, r);
2182 if (lp_info[i].action_index != -1)
2183 cfun->uses_eh_lsda = 1;
e6cfb550 2184 }
e701eb4d 2185
52a11cbf 2186 htab_delete (ar_hash);
76fc91c7 2187
52a11cbf
RH
2188 /* Next: assign dispatch values. In dwarf2 terms, this would be the
2189 landing pad label for the region. For sjlj though, there is one
2190 common landing pad from which we dispatch to the post-landing pads.
76fc91c7 2191
52a11cbf
RH
2192 A region receives a dispatch index if it is directly reachable
2193 and requires in-function processing. Regions that share post-landing
eaec9b3d 2194 pads may share dispatch indices. */
52a11cbf
RH
2195 /* ??? Post-landing pad sharing doesn't actually happen at the moment
2196 (see build_post_landing_pads) so we don't bother checking for it. */
4956d07c 2197
52a11cbf
RH
2198 index = 0;
2199 for (i = cfun->eh->last_region_number; i > 0; --i)
98ce21b3 2200 if (lp_info[i].directly_reachable)
52a11cbf 2201 lp_info[i].dispatch_index = index++;
76fc91c7 2202
52a11cbf
RH
2203 /* Finally: assign call-site values. If dwarf2 terms, this would be
2204 the region number assigned by convert_to_eh_region_ranges, but
2205 handles no-action and must-not-throw differently. */
76fc91c7 2206
52a11cbf
RH
2207 call_site_base = 1;
2208 for (i = cfun->eh->last_region_number; i > 0; --i)
2209 if (lp_info[i].directly_reachable)
2210 {
2211 int action = lp_info[i].action_index;
2212
2213 /* Map must-not-throw to otherwise unused call-site index 0. */
2214 if (action == -2)
2215 index = 0;
2216 /* Map no-action to otherwise unused call-site index -1. */
2217 else if (action == -1)
2218 index = -1;
2219 /* Otherwise, look it up in the table. */
2220 else
2221 index = add_call_site (GEN_INT (lp_info[i].dispatch_index), action);
2222
2223 lp_info[i].call_site_index = index;
2224 }
4956d07c 2225}
27a36778 2226
52a11cbf 2227static void
502b8322 2228sjlj_mark_call_sites (struct sjlj_lp_info *lp_info)
27a36778 2229{
52a11cbf
RH
2230 int last_call_site = -2;
2231 rtx insn, mem;
2232
52a11cbf 2233 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
27a36778 2234 {
52a11cbf
RH
2235 struct eh_region *region;
2236 int this_call_site;
2237 rtx note, before, p;
27a36778 2238
52a11cbf
RH
2239 /* Reset value tracking at extended basic block boundaries. */
2240 if (GET_CODE (insn) == CODE_LABEL)
2241 last_call_site = -2;
27a36778 2242
52a11cbf
RH
2243 if (! INSN_P (insn))
2244 continue;
27a36778 2245
52a11cbf
RH
2246 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2247 if (!note)
2248 {
2249 /* Calls (and trapping insns) without notes are outside any
2250 exception handling region in this function. Mark them as
2251 no action. */
2252 if (GET_CODE (insn) == CALL_INSN
2253 || (flag_non_call_exceptions
2254 && may_trap_p (PATTERN (insn))))
2255 this_call_site = -1;
2256 else
2257 continue;
2258 }
2259 else
2260 {
2261 /* Calls that are known to not throw need not be marked. */
2262 if (INTVAL (XEXP (note, 0)) <= 0)
2263 continue;
27a36778 2264
52a11cbf
RH
2265 region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
2266 this_call_site = lp_info[region->region_number].call_site_index;
2267 }
27a36778 2268
52a11cbf
RH
2269 if (this_call_site == last_call_site)
2270 continue;
2271
2272 /* Don't separate a call from it's argument loads. */
2273 before = insn;
2274 if (GET_CODE (insn) == CALL_INSN)
0fb7aeda 2275 before = find_first_parameter_load (insn, NULL_RTX);
4956d07c 2276
52a11cbf 2277 start_sequence ();
fd2c57a9
AH
2278 mem = adjust_address (cfun->eh->sjlj_fc, TYPE_MODE (integer_type_node),
2279 sjlj_fc_call_site_ofs);
52a11cbf
RH
2280 emit_move_insn (mem, GEN_INT (this_call_site));
2281 p = get_insns ();
2282 end_sequence ();
12670d88 2283
2f937369 2284 emit_insn_before (p, before);
52a11cbf
RH
2285 last_call_site = this_call_site;
2286 }
2287}
4956d07c 2288
52a11cbf
RH
2289/* Construct the SjLj_Function_Context. */
2290
2291static void
502b8322 2292sjlj_emit_function_enter (rtx dispatch_label)
4956d07c 2293{
52a11cbf 2294 rtx fn_begin, fc, mem, seq;
4956d07c 2295
52a11cbf 2296 fc = cfun->eh->sjlj_fc;
4956d07c 2297
52a11cbf 2298 start_sequence ();
8a4451aa 2299
8979edec
JL
2300 /* We're storing this libcall's address into memory instead of
2301 calling it directly. Thus, we must call assemble_external_libcall
2302 here, as we can not depend on emit_library_call to do it for us. */
2303 assemble_external_libcall (eh_personality_libfunc);
f4ef873c 2304 mem = adjust_address (fc, Pmode, sjlj_fc_personality_ofs);
52a11cbf
RH
2305 emit_move_insn (mem, eh_personality_libfunc);
2306
f4ef873c 2307 mem = adjust_address (fc, Pmode, sjlj_fc_lsda_ofs);
52a11cbf
RH
2308 if (cfun->uses_eh_lsda)
2309 {
2310 char buf[20];
86bdf071
RE
2311 rtx sym;
2312
df696a75 2313 ASM_GENERATE_INTERNAL_LABEL (buf, "LLSDA", current_function_funcdef_no);
86bdf071
RE
2314 sym = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
2315 SYMBOL_REF_FLAGS (sym) = SYMBOL_FLAG_LOCAL;
2316 emit_move_insn (mem, sym);
8a4451aa 2317 }
52a11cbf
RH
2318 else
2319 emit_move_insn (mem, const0_rtx);
3f2c5d1a 2320
52a11cbf
RH
2321#ifdef DONT_USE_BUILTIN_SETJMP
2322 {
2323 rtx x, note;
9defc9b7 2324 x = emit_library_call_value (setjmp_libfunc, NULL_RTX, LCT_RETURNS_TWICE,
52a11cbf
RH
2325 TYPE_MODE (integer_type_node), 1,
2326 plus_constant (XEXP (fc, 0),
2327 sjlj_fc_jbuf_ofs), Pmode);
2328
2e040219 2329 note = emit_note (NOTE_INSN_EXPECTED_VALUE);
52a11cbf
RH
2330 NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, x, const0_rtx);
2331
2332 emit_cmp_and_jump_insns (x, const0_rtx, NE, 0,
a06ef755 2333 TYPE_MODE (integer_type_node), 0, dispatch_label);
52a11cbf
RH
2334 }
2335#else
2336 expand_builtin_setjmp_setup (plus_constant (XEXP (fc, 0), sjlj_fc_jbuf_ofs),
2337 dispatch_label);
4956d07c 2338#endif
4956d07c 2339
52a11cbf
RH
2340 emit_library_call (unwind_sjlj_register_libfunc, LCT_NORMAL, VOIDmode,
2341 1, XEXP (fc, 0), Pmode);
12670d88 2342
52a11cbf
RH
2343 seq = get_insns ();
2344 end_sequence ();
4956d07c 2345
52a11cbf
RH
2346 /* ??? Instead of doing this at the beginning of the function,
2347 do this in a block that is at loop level 0 and dominates all
2348 can_throw_internal instructions. */
4956d07c 2349
52a11cbf
RH
2350 for (fn_begin = get_insns (); ; fn_begin = NEXT_INSN (fn_begin))
2351 if (GET_CODE (fn_begin) == NOTE
12c3874e
JH
2352 && (NOTE_LINE_NUMBER (fn_begin) == NOTE_INSN_FUNCTION_BEG
2353 || NOTE_LINE_NUMBER (fn_begin) == NOTE_INSN_BASIC_BLOCK))
52a11cbf 2354 break;
12c3874e
JH
2355 if (NOTE_LINE_NUMBER (fn_begin) == NOTE_INSN_FUNCTION_BEG)
2356 insert_insn_on_edge (seq, ENTRY_BLOCK_PTR->succ);
2357 else
2358 {
2359 rtx last = BB_END (ENTRY_BLOCK_PTR->succ->dest);
2360 for (; ; fn_begin = NEXT_INSN (fn_begin))
2361 if ((GET_CODE (fn_begin) == NOTE
2362 && NOTE_LINE_NUMBER (fn_begin) == NOTE_INSN_FUNCTION_BEG)
2363 || fn_begin == last)
2364 break;
2365 emit_insn_after (seq, fn_begin);
2366 }
4956d07c
MS
2367}
2368
52a11cbf
RH
2369/* Call back from expand_function_end to know where we should put
2370 the call to unwind_sjlj_unregister_libfunc if needed. */
12670d88 2371
52a11cbf 2372void
502b8322 2373sjlj_emit_function_exit_after (rtx after)
52a11cbf
RH
2374{
2375 cfun->eh->sjlj_exit_after = after;
2376}
4956d07c
MS
2377
2378static void
502b8322 2379sjlj_emit_function_exit (void)
52a11cbf
RH
2380{
2381 rtx seq;
12c3874e 2382 edge e;
4956d07c 2383
52a11cbf 2384 start_sequence ();
ce152ef8 2385
52a11cbf
RH
2386 emit_library_call (unwind_sjlj_unregister_libfunc, LCT_NORMAL, VOIDmode,
2387 1, XEXP (cfun->eh->sjlj_fc, 0), Pmode);
e6cfb550 2388
52a11cbf
RH
2389 seq = get_insns ();
2390 end_sequence ();
4956d07c 2391
52a11cbf
RH
2392 /* ??? Really this can be done in any block at loop level 0 that
2393 post-dominates all can_throw_internal instructions. This is
2394 the last possible moment. */
9a0d1e1b 2395
12c3874e
JH
2396 for (e = EXIT_BLOCK_PTR->pred; e; e = e->pred_next)
2397 if (e->flags & EDGE_FALLTHRU)
2398 break;
2399 if (e)
2400 {
2401 rtx insn;
2402
2403 /* Figure out whether the place we are supposed to insert libcall
2404 is inside the last basic block or after it. In the other case
2405 we need to emit to edge. */
2406 if (e->src->next_bb != EXIT_BLOCK_PTR)
2407 abort ();
2408 for (insn = NEXT_INSN (BB_END (e->src)); insn; insn = NEXT_INSN (insn))
2409 if (insn == cfun->eh->sjlj_exit_after)
2410 break;
2411 if (insn)
2412 insert_insn_on_edge (seq, e);
2413 else
2414 {
2415 insn = cfun->eh->sjlj_exit_after;
2416 if (GET_CODE (insn) == CODE_LABEL)
2417 insn = NEXT_INSN (insn);
2418 emit_insn_after (seq, insn);
2419 }
2420 }
9a0d1e1b
AM
2421}
2422
52a11cbf 2423static void
502b8322 2424sjlj_emit_dispatch_table (rtx dispatch_label, struct sjlj_lp_info *lp_info)
ce152ef8 2425{
52a11cbf
RH
2426 int i, first_reachable;
2427 rtx mem, dispatch, seq, fc;
12c3874e
JH
2428 rtx before;
2429 basic_block bb;
2430 edge e;
52a11cbf
RH
2431
2432 fc = cfun->eh->sjlj_fc;
2433
2434 start_sequence ();
2435
2436 emit_label (dispatch_label);
3f2c5d1a 2437
52a11cbf
RH
2438#ifndef DONT_USE_BUILTIN_SETJMP
2439 expand_builtin_setjmp_receiver (dispatch_label);
2440#endif
2441
2442 /* Load up dispatch index, exc_ptr and filter values from the
2443 function context. */
f4ef873c
RK
2444 mem = adjust_address (fc, TYPE_MODE (integer_type_node),
2445 sjlj_fc_call_site_ofs);
52a11cbf
RH
2446 dispatch = copy_to_reg (mem);
2447
f4ef873c 2448 mem = adjust_address (fc, word_mode, sjlj_fc_data_ofs);
f920765d 2449 if (word_mode != ptr_mode)
52a11cbf
RH
2450 {
2451#ifdef POINTERS_EXTEND_UNSIGNED
f920765d 2452 mem = convert_memory_address (ptr_mode, mem);
52a11cbf 2453#else
f920765d 2454 mem = convert_to_mode (ptr_mode, mem, 0);
52a11cbf
RH
2455#endif
2456 }
2457 emit_move_insn (cfun->eh->exc_ptr, mem);
2458
f4ef873c 2459 mem = adjust_address (fc, word_mode, sjlj_fc_data_ofs + UNITS_PER_WORD);
52a11cbf 2460 emit_move_insn (cfun->eh->filter, mem);
4956d07c 2461
52a11cbf
RH
2462 /* Jump to one of the directly reachable regions. */
2463 /* ??? This really ought to be using a switch statement. */
2464
2465 first_reachable = 0;
2466 for (i = cfun->eh->last_region_number; i > 0; --i)
a1622f83 2467 {
98ce21b3 2468 if (! lp_info[i].directly_reachable)
52a11cbf 2469 continue;
a1622f83 2470
52a11cbf
RH
2471 if (! first_reachable)
2472 {
2473 first_reachable = i;
2474 continue;
2475 }
e6cfb550 2476
a06ef755
RK
2477 emit_cmp_and_jump_insns (dispatch, GEN_INT (lp_info[i].dispatch_index),
2478 EQ, NULL_RTX, TYPE_MODE (integer_type_node), 0,
52a11cbf 2479 cfun->eh->region_array[i]->post_landing_pad);
a1622f83 2480 }
9a0d1e1b 2481
52a11cbf
RH
2482 seq = get_insns ();
2483 end_sequence ();
4956d07c 2484
12c3874e
JH
2485 before = cfun->eh->region_array[first_reachable]->post_landing_pad;
2486
2487 bb = emit_to_new_bb_before (seq, before);
2488 e = make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
2489 e->count = bb->count;
2490 e->probability = REG_BR_PROB_BASE;
ce152ef8
AM
2491}
2492
52a11cbf 2493static void
502b8322 2494sjlj_build_landing_pads (void)
ce152ef8 2495{
52a11cbf 2496 struct sjlj_lp_info *lp_info;
ce152ef8 2497
703ad42b
KG
2498 lp_info = xcalloc (cfun->eh->last_region_number + 1,
2499 sizeof (struct sjlj_lp_info));
ce152ef8 2500
52a11cbf
RH
2501 if (sjlj_find_directly_reachable_regions (lp_info))
2502 {
2503 rtx dispatch_label = gen_label_rtx ();
ce152ef8 2504
52a11cbf
RH
2505 cfun->eh->sjlj_fc
2506 = assign_stack_local (TYPE_MODE (sjlj_fc_type_node),
2507 int_size_in_bytes (sjlj_fc_type_node),
2508 TYPE_ALIGN (sjlj_fc_type_node));
4956d07c 2509
52a11cbf
RH
2510 sjlj_assign_call_site_values (dispatch_label, lp_info);
2511 sjlj_mark_call_sites (lp_info);
a1622f83 2512
52a11cbf
RH
2513 sjlj_emit_function_enter (dispatch_label);
2514 sjlj_emit_dispatch_table (dispatch_label, lp_info);
2515 sjlj_emit_function_exit ();
2516 }
a1622f83 2517
52a11cbf 2518 free (lp_info);
4956d07c 2519}
ce152ef8 2520
ce152ef8 2521void
502b8322 2522finish_eh_generation (void)
ce152ef8 2523{
12c3874e
JH
2524 basic_block bb;
2525
52a11cbf
RH
2526 /* Nothing to do if no regions created. */
2527 if (cfun->eh->region_tree == NULL)
ce152ef8
AM
2528 return;
2529
52a11cbf
RH
2530 /* The object here is to provide find_basic_blocks with detailed
2531 information (via reachable_handlers) on how exception control
2532 flows within the function. In this first pass, we can include
2533 type information garnered from ERT_THROW and ERT_ALLOWED_EXCEPTIONS
2534 regions, and hope that it will be useful in deleting unreachable
2535 handlers. Subsequently, we will generate landing pads which will
2536 connect many of the handlers, and then type information will not
2537 be effective. Still, this is a win over previous implementations. */
2538
95479831 2539 cleanup_cfg (CLEANUP_PRE_LOOP | CLEANUP_NO_INSN_DEL);
52a11cbf
RH
2540
2541 /* These registers are used by the landing pads. Make sure they
2542 have been generated. */
86c99549
RH
2543 get_exception_pointer (cfun);
2544 get_exception_filter (cfun);
52a11cbf
RH
2545
2546 /* Construct the landing pads. */
2547
2548 assign_filter_values ();
2549 build_post_landing_pads ();
2550 connect_post_landing_pads ();
2551 if (USING_SJLJ_EXCEPTIONS)
2552 sjlj_build_landing_pads ();
2553 else
2554 dw2_build_landing_pads ();
ce152ef8 2555
52a11cbf 2556 cfun->eh->built_landing_pads = 1;
ce152ef8 2557
52a11cbf
RH
2558 /* We've totally changed the CFG. Start over. */
2559 find_exception_handler_labels ();
12c3874e
JH
2560 break_superblocks ();
2561 if (USING_SJLJ_EXCEPTIONS)
2562 commit_edge_insertions ();
2563 FOR_EACH_BB (bb)
2564 {
2565 edge e, next;
2566 bool eh = false;
2567 for (e = bb->succ; e; e = next)
2568 {
2569 next = e->succ_next;
2570 if (e->flags & EDGE_EH)
2571 {
2572 remove_edge (e);
2573 eh = true;
2574 }
2575 }
2576 if (eh)
6de9cd9a 2577 rtl_make_eh_edge (NULL, bb, BB_END (bb));
12c3874e 2578 }
95479831 2579 cleanup_cfg (CLEANUP_PRE_LOOP | CLEANUP_NO_INSN_DEL);
ce152ef8 2580}
4956d07c 2581\f
6a58eee9 2582static hashval_t
502b8322 2583ehl_hash (const void *pentry)
6a58eee9
RH
2584{
2585 struct ehl_map_entry *entry = (struct ehl_map_entry *) pentry;
2586
2587 /* 2^32 * ((sqrt(5) - 1) / 2) */
2588 const hashval_t scaled_golden_ratio = 0x9e3779b9;
2589 return CODE_LABEL_NUMBER (entry->label) * scaled_golden_ratio;
2590}
2591
2592static int
502b8322 2593ehl_eq (const void *pentry, const void *pdata)
6a58eee9
RH
2594{
2595 struct ehl_map_entry *entry = (struct ehl_map_entry *) pentry;
2596 struct ehl_map_entry *data = (struct ehl_map_entry *) pdata;
2597
2598 return entry->label == data->label;
2599}
2600
52a11cbf 2601/* This section handles removing dead code for flow. */
154bba13 2602
6a58eee9 2603/* Remove LABEL from exception_handler_label_map. */
154bba13 2604
52a11cbf 2605static void
502b8322 2606remove_exception_handler_label (rtx label)
154bba13 2607{
6a58eee9 2608 struct ehl_map_entry **slot, tmp;
100d81d4 2609
6a58eee9 2610 /* If exception_handler_label_map was not built yet,
655dd289 2611 there is nothing to do. */
e2500fed 2612 if (cfun->eh->exception_handler_label_map == NULL)
655dd289
JJ
2613 return;
2614
6a58eee9
RH
2615 tmp.label = label;
2616 slot = (struct ehl_map_entry **)
e2500fed 2617 htab_find_slot (cfun->eh->exception_handler_label_map, &tmp, NO_INSERT);
6a58eee9
RH
2618 if (! slot)
2619 abort ();
154bba13 2620
e2500fed 2621 htab_clear_slot (cfun->eh->exception_handler_label_map, (void **) slot);
154bba13
TT
2622}
2623
52a11cbf 2624/* Splice REGION from the region tree etc. */
12670d88 2625
f19c9228 2626static void
502b8322 2627remove_eh_handler (struct eh_region *region)
4956d07c 2628{
ff2c46ac 2629 struct eh_region **pp, **pp_start, *p, *outer, *inner;
52a11cbf 2630 rtx lab;
4956d07c 2631
52a11cbf
RH
2632 /* For the benefit of efficiently handling REG_EH_REGION notes,
2633 replace this region in the region array with its containing
2634 region. Note that previous region deletions may result in
6a58eee9
RH
2635 multiple copies of this region in the array, so we have a
2636 list of alternate numbers by which we are known. */
2637
ff2c46ac
RH
2638 outer = region->outer;
2639 cfun->eh->region_array[region->region_number] = outer;
6a58eee9
RH
2640 if (region->aka)
2641 {
2642 int i;
2643 EXECUTE_IF_SET_IN_BITMAP (region->aka, 0, i,
ff2c46ac 2644 { cfun->eh->region_array[i] = outer; });
6a58eee9
RH
2645 }
2646
ff2c46ac 2647 if (outer)
6a58eee9 2648 {
ff2c46ac 2649 if (!outer->aka)
e2500fed 2650 outer->aka = BITMAP_GGC_ALLOC ();
6a58eee9 2651 if (region->aka)
ff2c46ac
RH
2652 bitmap_a_or_b (outer->aka, outer->aka, region->aka);
2653 bitmap_set_bit (outer->aka, region->region_number);
6a58eee9 2654 }
52a11cbf
RH
2655
2656 if (cfun->eh->built_landing_pads)
2657 lab = region->landing_pad;
2658 else
2659 lab = region->label;
2660 if (lab)
2661 remove_exception_handler_label (lab);
2662
ff2c46ac
RH
2663 if (outer)
2664 pp_start = &outer->inner;
52a11cbf 2665 else
ff2c46ac
RH
2666 pp_start = &cfun->eh->region_tree;
2667 for (pp = pp_start, p = *pp; p != region; pp = &p->next_peer, p = *pp)
52a11cbf 2668 continue;
ff2c46ac 2669 *pp = region->next_peer;
12670d88 2670
ff2c46ac
RH
2671 inner = region->inner;
2672 if (inner)
4956d07c 2673 {
ff2c46ac
RH
2674 for (p = inner; p->next_peer ; p = p->next_peer)
2675 p->outer = outer;
2676 p->outer = outer;
2677
2678 p->next_peer = *pp_start;
2679 *pp_start = inner;
4956d07c 2680 }
f19c9228 2681
52a11cbf
RH
2682 if (region->type == ERT_CATCH)
2683 {
2684 struct eh_region *try, *next, *prev;
f19c9228 2685
52a11cbf
RH
2686 for (try = region->next_peer;
2687 try->type == ERT_CATCH;
2688 try = try->next_peer)
2689 continue;
2690 if (try->type != ERT_TRY)
2691 abort ();
f19c9228 2692
52a11cbf
RH
2693 next = region->u.catch.next_catch;
2694 prev = region->u.catch.prev_catch;
f19c9228 2695
52a11cbf
RH
2696 if (next)
2697 next->u.catch.prev_catch = prev;
2698 else
2699 try->u.try.last_catch = prev;
2700 if (prev)
2701 prev->u.catch.next_catch = next;
2702 else
2703 {
2704 try->u.try.catch = next;
2705 if (! next)
2706 remove_eh_handler (try);
2707 }
2708 }
4956d07c
MS
2709}
2710
52a11cbf
RH
2711/* LABEL heads a basic block that is about to be deleted. If this
2712 label corresponds to an exception region, we may be able to
2713 delete the region. */
4956d07c
MS
2714
2715void
502b8322 2716maybe_remove_eh_handler (rtx label)
4956d07c 2717{
6a58eee9
RH
2718 struct ehl_map_entry **slot, tmp;
2719 struct eh_region *region;
4956d07c 2720
52a11cbf
RH
2721 /* ??? After generating landing pads, it's not so simple to determine
2722 if the region data is completely unused. One must examine the
2723 landing pad and the post landing pad, and whether an inner try block
2724 is referencing the catch handlers directly. */
2725 if (cfun->eh->built_landing_pads)
4956d07c
MS
2726 return;
2727
6a58eee9
RH
2728 tmp.label = label;
2729 slot = (struct ehl_map_entry **)
e2500fed 2730 htab_find_slot (cfun->eh->exception_handler_label_map, &tmp, NO_INSERT);
6a58eee9
RH
2731 if (! slot)
2732 return;
2733 region = (*slot)->region;
2734 if (! region)
2735 return;
2736
2737 /* Flow will want to remove MUST_NOT_THROW regions as unreachable
2738 because there is no path to the fallback call to terminate.
2739 But the region continues to affect call-site data until there
2740 are no more contained calls, which we don't see here. */
2741 if (region->type == ERT_MUST_NOT_THROW)
87ff9c8e 2742 {
e2500fed 2743 htab_clear_slot (cfun->eh->exception_handler_label_map, (void **) slot);
6a58eee9 2744 region->label = NULL_RTX;
87ff9c8e 2745 }
6a58eee9
RH
2746 else
2747 remove_eh_handler (region);
2748}
2749
2750/* Invokes CALLBACK for every exception handler label. Only used by old
2751 loop hackery; should not be used by new code. */
2752
2753void
502b8322 2754for_each_eh_label (void (*callback) (rtx))
6a58eee9 2755{
e2500fed 2756 htab_traverse (cfun->eh->exception_handler_label_map, for_each_eh_label_1,
3897f229 2757 (void *) &callback);
87ff9c8e
RH
2758}
2759
6a58eee9 2760static int
502b8322 2761for_each_eh_label_1 (void **pentry, void *data)
6a58eee9
RH
2762{
2763 struct ehl_map_entry *entry = *(struct ehl_map_entry **)pentry;
3897f229 2764 void (*callback) (rtx) = *(void (**) (rtx)) data;
6a58eee9
RH
2765
2766 (*callback) (entry->label);
2767 return 1;
2768}
52a11cbf
RH
2769\f
2770/* This section describes CFG exception edges for flow. */
87ff9c8e 2771
52a11cbf 2772/* For communicating between calls to reachable_next_level. */
6de9cd9a 2773struct reachable_info
87ff9c8e 2774{
52a11cbf
RH
2775 tree types_caught;
2776 tree types_allowed;
6de9cd9a
DN
2777 void (*callback) (struct eh_region *, void *);
2778 void *callback_data;
2779 bool saw_any_handlers;
52a11cbf 2780};
87ff9c8e 2781
52a11cbf
RH
2782/* A subroutine of reachable_next_level. Return true if TYPE, or a
2783 base class of TYPE, is in HANDLED. */
87ff9c8e 2784
6de9cd9a 2785int
502b8322 2786check_handled (tree handled, tree type)
87ff9c8e 2787{
52a11cbf
RH
2788 tree t;
2789
2790 /* We can check for exact matches without front-end help. */
2791 if (! lang_eh_type_covers)
f54a7f6f 2792 {
52a11cbf
RH
2793 for (t = handled; t ; t = TREE_CHAIN (t))
2794 if (TREE_VALUE (t) == type)
2795 return 1;
2796 }
2797 else
2798 {
2799 for (t = handled; t ; t = TREE_CHAIN (t))
2800 if ((*lang_eh_type_covers) (TREE_VALUE (t), type))
2801 return 1;
f54a7f6f 2802 }
52a11cbf
RH
2803
2804 return 0;
87ff9c8e
RH
2805}
2806
52a11cbf
RH
2807/* A subroutine of reachable_next_level. If we are collecting a list
2808 of handlers, add one. After landing pad generation, reference
2809 it instead of the handlers themselves. Further, the handlers are
3f2c5d1a 2810 all wired together, so by referencing one, we've got them all.
52a11cbf
RH
2811 Before landing pad generation we reference each handler individually.
2812
2813 LP_REGION contains the landing pad; REGION is the handler. */
87ff9c8e
RH
2814
2815static void
6de9cd9a
DN
2816add_reachable_handler (struct reachable_info *info,
2817 struct eh_region *lp_region, struct eh_region *region)
87ff9c8e 2818{
52a11cbf
RH
2819 if (! info)
2820 return;
2821
6de9cd9a
DN
2822 info->saw_any_handlers = true;
2823
52a11cbf 2824 if (cfun->eh->built_landing_pads)
6de9cd9a 2825 info->callback (lp_region, info->callback_data);
52a11cbf 2826 else
6de9cd9a 2827 info->callback (region, info->callback_data);
87ff9c8e
RH
2828}
2829
3f2c5d1a 2830/* Process one level of exception regions for reachability.
52a11cbf
RH
2831 If TYPE_THROWN is non-null, then it is the *exact* type being
2832 propagated. If INFO is non-null, then collect handler labels
2833 and caught/allowed type information between invocations. */
87ff9c8e 2834
52a11cbf 2835static enum reachable_code
502b8322
AJ
2836reachable_next_level (struct eh_region *region, tree type_thrown,
2837 struct reachable_info *info)
87ff9c8e 2838{
52a11cbf
RH
2839 switch (region->type)
2840 {
2841 case ERT_CLEANUP:
2842 /* Before landing-pad generation, we model control flow
2843 directly to the individual handlers. In this way we can
2844 see that catch handler types may shadow one another. */
2845 add_reachable_handler (info, region, region);
2846 return RNL_MAYBE_CAUGHT;
2847
2848 case ERT_TRY:
2849 {
2850 struct eh_region *c;
2851 enum reachable_code ret = RNL_NOT_CAUGHT;
fa51b01b 2852
52a11cbf
RH
2853 for (c = region->u.try.catch; c ; c = c->u.catch.next_catch)
2854 {
2855 /* A catch-all handler ends the search. */
6d41a92f 2856 if (c->u.catch.type_list == NULL)
52a11cbf
RH
2857 {
2858 add_reachable_handler (info, region, c);
2859 return RNL_CAUGHT;
2860 }
2861
2862 if (type_thrown)
2863 {
a8154559 2864 /* If we have at least one type match, end the search. */
6d41a92f 2865 tree tp_node = c->u.catch.type_list;
3f2c5d1a 2866
6d41a92f 2867 for (; tp_node; tp_node = TREE_CHAIN (tp_node))
52a11cbf 2868 {
6d41a92f
OH
2869 tree type = TREE_VALUE (tp_node);
2870
2871 if (type == type_thrown
2872 || (lang_eh_type_covers
2873 && (*lang_eh_type_covers) (type, type_thrown)))
2874 {
2875 add_reachable_handler (info, region, c);
2876 return RNL_CAUGHT;
2877 }
52a11cbf
RH
2878 }
2879
2880 /* If we have definitive information of a match failure,
2881 the catch won't trigger. */
2882 if (lang_eh_type_covers)
2883 return RNL_NOT_CAUGHT;
2884 }
2885
6d41a92f
OH
2886 /* At this point, we either don't know what type is thrown or
2887 don't have front-end assistance to help deciding if it is
2888 covered by one of the types in the list for this region.
3f2c5d1a 2889
6d41a92f
OH
2890 We'd then like to add this region to the list of reachable
2891 handlers since it is indeed potentially reachable based on the
3f2c5d1a
RS
2892 information we have.
2893
6d41a92f
OH
2894 Actually, this handler is for sure not reachable if all the
2895 types it matches have already been caught. That is, it is only
2896 potentially reachable if at least one of the types it catches
2897 has not been previously caught. */
2898
52a11cbf
RH
2899 if (! info)
2900 ret = RNL_MAYBE_CAUGHT;
6d41a92f 2901 else
52a11cbf 2902 {
6d41a92f
OH
2903 tree tp_node = c->u.catch.type_list;
2904 bool maybe_reachable = false;
52a11cbf 2905
6d41a92f
OH
2906 /* Compute the potential reachability of this handler and
2907 update the list of types caught at the same time. */
2908 for (; tp_node; tp_node = TREE_CHAIN (tp_node))
2909 {
2910 tree type = TREE_VALUE (tp_node);
2911
2912 if (! check_handled (info->types_caught, type))
2913 {
2914 info->types_caught
2915 = tree_cons (NULL, type, info->types_caught);
3f2c5d1a 2916
6d41a92f
OH
2917 maybe_reachable = true;
2918 }
2919 }
3f2c5d1a 2920
6d41a92f
OH
2921 if (maybe_reachable)
2922 {
2923 add_reachable_handler (info, region, c);
3f2c5d1a 2924
6d41a92f
OH
2925 /* ??? If the catch type is a base class of every allowed
2926 type, then we know we can stop the search. */
2927 ret = RNL_MAYBE_CAUGHT;
2928 }
52a11cbf
RH
2929 }
2930 }
87ff9c8e 2931
52a11cbf
RH
2932 return ret;
2933 }
87ff9c8e 2934
52a11cbf
RH
2935 case ERT_ALLOWED_EXCEPTIONS:
2936 /* An empty list of types definitely ends the search. */
2937 if (region->u.allowed.type_list == NULL_TREE)
2938 {
2939 add_reachable_handler (info, region, region);
2940 return RNL_CAUGHT;
2941 }
87ff9c8e 2942
52a11cbf
RH
2943 /* Collect a list of lists of allowed types for use in detecting
2944 when a catch may be transformed into a catch-all. */
2945 if (info)
2946 info->types_allowed = tree_cons (NULL_TREE,
2947 region->u.allowed.type_list,
2948 info->types_allowed);
3f2c5d1a 2949
684d9f3b 2950 /* If we have definitive information about the type hierarchy,
52a11cbf
RH
2951 then we can tell if the thrown type will pass through the
2952 filter. */
2953 if (type_thrown && lang_eh_type_covers)
2954 {
2955 if (check_handled (region->u.allowed.type_list, type_thrown))
2956 return RNL_NOT_CAUGHT;
2957 else
2958 {
2959 add_reachable_handler (info, region, region);
2960 return RNL_CAUGHT;
2961 }
2962 }
21cd906e 2963
52a11cbf
RH
2964 add_reachable_handler (info, region, region);
2965 return RNL_MAYBE_CAUGHT;
21cd906e 2966
52a11cbf 2967 case ERT_CATCH:
fbe5a4a6 2968 /* Catch regions are handled by their controlling try region. */
52a11cbf 2969 return RNL_NOT_CAUGHT;
21cd906e 2970
52a11cbf
RH
2971 case ERT_MUST_NOT_THROW:
2972 /* Here we end our search, since no exceptions may propagate.
2973 If we've touched down at some landing pad previous, then the
2974 explicit function call we generated may be used. Otherwise
2975 the call is made by the runtime. */
6de9cd9a 2976 if (info && info->saw_any_handlers)
21cd906e 2977 {
52a11cbf 2978 add_reachable_handler (info, region, region);
0fb7aeda 2979 return RNL_CAUGHT;
21cd906e 2980 }
52a11cbf
RH
2981 else
2982 return RNL_BLOCKED;
21cd906e 2983
52a11cbf
RH
2984 case ERT_THROW:
2985 case ERT_FIXUP:
3f2c5d1a 2986 case ERT_UNKNOWN:
52a11cbf
RH
2987 /* Shouldn't see these here. */
2988 break;
21cd906e 2989 }
fa51b01b 2990
52a11cbf 2991 abort ();
fa51b01b 2992}
4956d07c 2993
6de9cd9a 2994/* Invoke CALLBACK on each region reachable from REGION_NUMBER. */
4956d07c 2995
6de9cd9a
DN
2996void
2997foreach_reachable_handler (int region_number, bool is_resx,
2998 void (*callback) (struct eh_region *, void *),
2999 void *callback_data)
4956d07c 3000{
52a11cbf
RH
3001 struct reachable_info info;
3002 struct eh_region *region;
3003 tree type_thrown;
4956d07c 3004
52a11cbf 3005 memset (&info, 0, sizeof (info));
6de9cd9a
DN
3006 info.callback = callback;
3007 info.callback_data = callback_data;
4956d07c 3008
52a11cbf 3009 region = cfun->eh->region_array[region_number];
fb13d4d0 3010
52a11cbf 3011 type_thrown = NULL_TREE;
6de9cd9a 3012 if (is_resx)
7f206d8f
RH
3013 {
3014 /* A RESX leaves a region instead of entering it. Thus the
3015 region itself may have been deleted out from under us. */
3016 if (region == NULL)
6de9cd9a 3017 return;
7f206d8f
RH
3018 region = region->outer;
3019 }
3020 else if (region->type == ERT_THROW)
52a11cbf
RH
3021 {
3022 type_thrown = region->u.throw.type;
3023 region = region->outer;
3024 }
fac62ecf 3025
bafb714b
MM
3026 while (region)
3027 {
3028 if (reachable_next_level (region, type_thrown, &info) >= RNL_CAUGHT)
a944ceb9 3029 break;
bafb714b
MM
3030 /* If we have processed one cleanup, there is no point in
3031 processing any more of them. Each cleanup will have an edge
3032 to the next outer cleanup region, so the flow graph will be
3033 accurate. */
3034 if (region->type == ERT_CLEANUP)
3035 region = region->u.cleanup.prev_try;
3036 else
3037 region = region->outer;
3038 }
6de9cd9a
DN
3039}
3040
3041/* Retrieve a list of labels of exception handlers which can be
3042 reached by a given insn. */
3043
3044static void
3045arh_to_landing_pad (struct eh_region *region, void *data)
3046{
3047 rtx *p_handlers = data;
3048 if (! *p_handlers)
3049 *p_handlers = alloc_INSN_LIST (region->landing_pad, NULL_RTX);
3050}
3051
3052static void
3053arh_to_label (struct eh_region *region, void *data)
3054{
3055 rtx *p_handlers = data;
3056 *p_handlers = alloc_INSN_LIST (region->label, *p_handlers);
3057}
3058
3059rtx
3060reachable_handlers (rtx insn)
3061{
3062 bool is_resx = false;
3063 rtx handlers = NULL;
3064 int region_number;
3065
3066 if (GET_CODE (insn) == JUMP_INSN
3067 && GET_CODE (PATTERN (insn)) == RESX)
3068 {
3069 region_number = XINT (PATTERN (insn), 0);
3070 is_resx = true;
3071 }
3072 else
3073 {
3074 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3075 if (!note || INTVAL (XEXP (note, 0)) <= 0)
3076 return NULL;
3077 region_number = INTVAL (XEXP (note, 0));
3078 }
502b8322 3079
6de9cd9a
DN
3080 foreach_reachable_handler (region_number, is_resx,
3081 (cfun->eh->built_landing_pads
3082 ? arh_to_landing_pad
3083 : arh_to_label),
3084 &handlers);
3085
3086 return handlers;
fb13d4d0
JM
3087}
3088
52a11cbf
RH
3089/* Determine if the given INSN can throw an exception that is caught
3090 within the function. */
4956d07c 3091
52a11cbf 3092bool
6de9cd9a 3093can_throw_internal_1 (int region_number)
4956d07c 3094{
52a11cbf
RH
3095 struct eh_region *region;
3096 tree type_thrown;
6de9cd9a
DN
3097
3098 region = cfun->eh->region_array[region_number];
3099
3100 type_thrown = NULL_TREE;
3101 if (region->type == ERT_THROW)
3102 {
3103 type_thrown = region->u.throw.type;
3104 region = region->outer;
3105 }
3106
3107 /* If this exception is ignored by each and every containing region,
3108 then control passes straight out. The runtime may handle some
3109 regions, which also do not require processing internally. */
3110 for (; region; region = region->outer)
3111 {
3112 enum reachable_code how = reachable_next_level (region, type_thrown, 0);
3113 if (how == RNL_BLOCKED)
3114 return false;
3115 if (how != RNL_NOT_CAUGHT)
3116 return true;
3117 }
3118
3119 return false;
3120}
3121
3122bool
3123can_throw_internal (rtx insn)
3124{
52a11cbf 3125 rtx note;
e6cfb550 3126
52a11cbf
RH
3127 if (! INSN_P (insn))
3128 return false;
12670d88 3129
52a11cbf
RH
3130 if (GET_CODE (insn) == INSN
3131 && GET_CODE (PATTERN (insn)) == SEQUENCE)
3132 insn = XVECEXP (PATTERN (insn), 0, 0);
4956d07c 3133
52a11cbf
RH
3134 if (GET_CODE (insn) == CALL_INSN
3135 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
4956d07c 3136 {
52a11cbf
RH
3137 int i;
3138 for (i = 0; i < 3; ++i)
4956d07c 3139 {
52a11cbf
RH
3140 rtx sub = XEXP (PATTERN (insn), i);
3141 for (; sub ; sub = NEXT_INSN (sub))
3142 if (can_throw_internal (sub))
3143 return true;
4956d07c 3144 }
52a11cbf 3145 return false;
4956d07c
MS
3146 }
3147
52a11cbf
RH
3148 /* Every insn that might throw has an EH_REGION note. */
3149 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3150 if (!note || INTVAL (XEXP (note, 0)) <= 0)
3151 return false;
4956d07c 3152
6de9cd9a
DN
3153 return can_throw_internal_1 (INTVAL (XEXP (note, 0)));
3154}
3155
3156/* Determine if the given INSN can throw an exception that is
3157 visible outside the function. */
3158
3159bool
3160can_throw_external_1 (int region_number)
3161{
3162 struct eh_region *region;
3163 tree type_thrown;
3164
3165 region = cfun->eh->region_array[region_number];
4956d07c 3166
52a11cbf
RH
3167 type_thrown = NULL_TREE;
3168 if (region->type == ERT_THROW)
3169 {
3170 type_thrown = region->u.throw.type;
3171 region = region->outer;
3172 }
4956d07c 3173
6de9cd9a
DN
3174 /* If the exception is caught or blocked by any containing region,
3175 then it is not seen by any calling function. */
3176 for (; region ; region = region->outer)
3177 if (reachable_next_level (region, type_thrown, NULL) >= RNL_CAUGHT)
3178 return false;
4956d07c 3179
6de9cd9a 3180 return true;
52a11cbf 3181}
4956d07c 3182
52a11cbf 3183bool
502b8322 3184can_throw_external (rtx insn)
4956d07c 3185{
52a11cbf 3186 rtx note;
4956d07c 3187
52a11cbf
RH
3188 if (! INSN_P (insn))
3189 return false;
3190
3191 if (GET_CODE (insn) == INSN
3192 && GET_CODE (PATTERN (insn)) == SEQUENCE)
3193 insn = XVECEXP (PATTERN (insn), 0, 0);
3194
3195 if (GET_CODE (insn) == CALL_INSN
3196 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
4956d07c 3197 {
52a11cbf
RH
3198 int i;
3199 for (i = 0; i < 3; ++i)
4956d07c 3200 {
52a11cbf
RH
3201 rtx sub = XEXP (PATTERN (insn), i);
3202 for (; sub ; sub = NEXT_INSN (sub))
3203 if (can_throw_external (sub))
3204 return true;
4956d07c 3205 }
52a11cbf 3206 return false;
4956d07c 3207 }
52a11cbf
RH
3208
3209 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3210 if (!note)
3211 {
3212 /* Calls (and trapping insns) without notes are outside any
3213 exception handling region in this function. We have to
3214 assume it might throw. Given that the front end and middle
3215 ends mark known NOTHROW functions, this isn't so wildly
3216 inaccurate. */
3217 return (GET_CODE (insn) == CALL_INSN
3218 || (flag_non_call_exceptions
3219 && may_trap_p (PATTERN (insn))));
3220 }
3221 if (INTVAL (XEXP (note, 0)) <= 0)
3222 return false;
3223
6de9cd9a 3224 return can_throw_external_1 (INTVAL (XEXP (note, 0)));
4956d07c 3225}
1ef1bf06 3226
b6128b8c 3227/* Set current_function_nothrow and cfun->all_throwers_are_sibcalls. */
6814a8a0 3228
b6128b8c 3229void
502b8322 3230set_nothrow_function_flags (void)
1ef1bf06
AM
3231{
3232 rtx insn;
502b8322 3233
b6128b8c 3234 current_function_nothrow = 1;
1ef1bf06 3235
b6128b8c
SH
3236 /* Assume cfun->all_throwers_are_sibcalls until we encounter
3237 something that can throw an exception. We specifically exempt
3238 CALL_INSNs that are SIBLING_CALL_P, as these are really jumps,
3239 and can't throw. Most CALL_INSNs are not SIBLING_CALL_P, so this
3240 is optimistic. */
1ef1bf06 3241
b6128b8c
SH
3242 cfun->all_throwers_are_sibcalls = 1;
3243
3244 if (! flag_exceptions)
3245 return;
502b8322 3246
1ef1bf06 3247 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
52a11cbf 3248 if (can_throw_external (insn))
b6128b8c
SH
3249 {
3250 current_function_nothrow = 0;
3251
3252 if (GET_CODE (insn) != CALL_INSN || !SIBLING_CALL_P (insn))
3253 {
3254 cfun->all_throwers_are_sibcalls = 0;
3255 return;
3256 }
3257 }
3258
52a11cbf
RH
3259 for (insn = current_function_epilogue_delay_list; insn;
3260 insn = XEXP (insn, 1))
b6128b8c
SH
3261 if (can_throw_external (insn))
3262 {
3263 current_function_nothrow = 0;
4da896b2 3264
b6128b8c
SH
3265 if (GET_CODE (insn) != CALL_INSN || !SIBLING_CALL_P (insn))
3266 {
3267 cfun->all_throwers_are_sibcalls = 0;
3268 return;
3269 }
3270 }
1ef1bf06 3271}
52a11cbf 3272
ca55abae 3273\f
52a11cbf 3274/* Various hooks for unwind library. */
ca55abae
JM
3275
3276/* Do any necessary initialization to access arbitrary stack frames.
3277 On the SPARC, this means flushing the register windows. */
3278
3279void
502b8322 3280expand_builtin_unwind_init (void)
ca55abae
JM
3281{
3282 /* Set this so all the registers get saved in our frame; we need to be
30f7a378 3283 able to copy the saved values for any registers from frames we unwind. */
ca55abae
JM
3284 current_function_has_nonlocal_label = 1;
3285
3286#ifdef SETUP_FRAME_ADDRESSES
3287 SETUP_FRAME_ADDRESSES ();
3288#endif
3289}
3290
52a11cbf 3291rtx
502b8322 3292expand_builtin_eh_return_data_regno (tree arglist)
52a11cbf
RH
3293{
3294 tree which = TREE_VALUE (arglist);
3295 unsigned HOST_WIDE_INT iwhich;
3296
3297 if (TREE_CODE (which) != INTEGER_CST)
3298 {
3299 error ("argument of `__builtin_eh_return_regno' must be constant");
3300 return constm1_rtx;
3301 }
3302
3303 iwhich = tree_low_cst (which, 1);
3304 iwhich = EH_RETURN_DATA_REGNO (iwhich);
3305 if (iwhich == INVALID_REGNUM)
3306 return constm1_rtx;
3307
3308#ifdef DWARF_FRAME_REGNUM
3309 iwhich = DWARF_FRAME_REGNUM (iwhich);
3310#else
3311 iwhich = DBX_REGISTER_NUMBER (iwhich);
3312#endif
3313
3f2c5d1a 3314 return GEN_INT (iwhich);
52a11cbf
RH
3315}
3316
ca55abae
JM
3317/* Given a value extracted from the return address register or stack slot,
3318 return the actual address encoded in that value. */
3319
3320rtx
502b8322 3321expand_builtin_extract_return_addr (tree addr_tree)
ca55abae
JM
3322{
3323 rtx addr = expand_expr (addr_tree, NULL_RTX, Pmode, 0);
52a11cbf 3324
0ab38418
EC
3325 if (GET_MODE (addr) != Pmode
3326 && GET_MODE (addr) != VOIDmode)
3327 {
3328#ifdef POINTERS_EXTEND_UNSIGNED
3329 addr = convert_memory_address (Pmode, addr);
3330#else
3331 addr = convert_to_mode (Pmode, addr, 0);
3332#endif
3333 }
3334
52a11cbf
RH
3335 /* First mask out any unwanted bits. */
3336#ifdef MASK_RETURN_ADDR
22273300 3337 expand_and (Pmode, addr, MASK_RETURN_ADDR, addr);
52a11cbf
RH
3338#endif
3339
3340 /* Then adjust to find the real return address. */
3341#if defined (RETURN_ADDR_OFFSET)
3342 addr = plus_constant (addr, RETURN_ADDR_OFFSET);
3343#endif
3344
3345 return addr;
ca55abae
JM
3346}
3347
3348/* Given an actual address in addr_tree, do any necessary encoding
3349 and return the value to be stored in the return address register or
3350 stack slot so the epilogue will return to that address. */
3351
3352rtx
502b8322 3353expand_builtin_frob_return_addr (tree addr_tree)
ca55abae 3354{
4b6c1672 3355 rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, 0);
52a11cbf 3356
5ae6cd0d 3357 addr = convert_memory_address (Pmode, addr);
be128cd9 3358
ca55abae 3359#ifdef RETURN_ADDR_OFFSET
52a11cbf 3360 addr = force_reg (Pmode, addr);
ca55abae
JM
3361 addr = plus_constant (addr, -RETURN_ADDR_OFFSET);
3362#endif
52a11cbf 3363
ca55abae
JM
3364 return addr;
3365}
3366
52a11cbf
RH
3367/* Set up the epilogue with the magic bits we'll need to return to the
3368 exception handler. */
ca55abae 3369
52a11cbf 3370void
502b8322
AJ
3371expand_builtin_eh_return (tree stackadj_tree ATTRIBUTE_UNUSED,
3372 tree handler_tree)
ca55abae 3373{
34dc173c 3374 rtx tmp;
ca55abae 3375
34dc173c
UW
3376#ifdef EH_RETURN_STACKADJ_RTX
3377 tmp = expand_expr (stackadj_tree, cfun->eh->ehr_stackadj, VOIDmode, 0);
5ae6cd0d 3378 tmp = convert_memory_address (Pmode, tmp);
34dc173c
UW
3379 if (!cfun->eh->ehr_stackadj)
3380 cfun->eh->ehr_stackadj = copy_to_reg (tmp);
3381 else if (tmp != cfun->eh->ehr_stackadj)
3382 emit_move_insn (cfun->eh->ehr_stackadj, tmp);
be128cd9
RK
3383#endif
3384
34dc173c 3385 tmp = expand_expr (handler_tree, cfun->eh->ehr_handler, VOIDmode, 0);
5ae6cd0d 3386 tmp = convert_memory_address (Pmode, tmp);
34dc173c
UW
3387 if (!cfun->eh->ehr_handler)
3388 cfun->eh->ehr_handler = copy_to_reg (tmp);
3389 else if (tmp != cfun->eh->ehr_handler)
3390 emit_move_insn (cfun->eh->ehr_handler, tmp);
ca55abae 3391
34dc173c
UW
3392 if (!cfun->eh->ehr_label)
3393 cfun->eh->ehr_label = gen_label_rtx ();
52a11cbf 3394 emit_jump (cfun->eh->ehr_label);
a1622f83
AM
3395}
3396
71038426 3397void
502b8322 3398expand_eh_return (void)
ca55abae 3399{
34dc173c 3400 rtx around_label;
ca55abae 3401
52a11cbf 3402 if (! cfun->eh->ehr_label)
71038426 3403 return;
ca55abae 3404
52a11cbf 3405 current_function_calls_eh_return = 1;
ca55abae 3406
34dc173c
UW
3407#ifdef EH_RETURN_STACKADJ_RTX
3408 emit_move_insn (EH_RETURN_STACKADJ_RTX, const0_rtx);
3409#endif
3410
52a11cbf 3411 around_label = gen_label_rtx ();
52a11cbf 3412 emit_jump (around_label);
ca55abae 3413
52a11cbf
RH
3414 emit_label (cfun->eh->ehr_label);
3415 clobber_return_register ();
ca55abae 3416
34dc173c
UW
3417#ifdef EH_RETURN_STACKADJ_RTX
3418 emit_move_insn (EH_RETURN_STACKADJ_RTX, cfun->eh->ehr_stackadj);
3419#endif
3420
52a11cbf
RH
3421#ifdef HAVE_eh_return
3422 if (HAVE_eh_return)
34dc173c 3423 emit_insn (gen_eh_return (cfun->eh->ehr_handler));
52a11cbf 3424 else
71038426 3425#endif
52a11cbf 3426 {
34dc173c
UW
3427#ifdef EH_RETURN_HANDLER_RTX
3428 emit_move_insn (EH_RETURN_HANDLER_RTX, cfun->eh->ehr_handler);
3429#else
3430 error ("__builtin_eh_return not supported on this target");
3431#endif
52a11cbf 3432 }
71038426 3433
52a11cbf 3434 emit_label (around_label);
71038426 3435}
c76362b4
JW
3436
3437/* Convert a ptr_mode address ADDR_TREE to a Pmode address controlled by
3438 POINTERS_EXTEND_UNSIGNED and return it. */
3439
3440rtx
3441expand_builtin_extend_pointer (tree addr_tree)
3442{
3443 rtx addr = expand_expr (addr_tree, NULL_RTX, ptr_mode, 0);
3444 int extend;
3445
3446#ifdef POINTERS_EXTEND_UNSIGNED
3447 extend = POINTERS_EXTEND_UNSIGNED;
3448#else
3449 /* The previous EH code did an unsigned extend by default, so we do this also
3450 for consistency. */
3451 extend = 1;
3452#endif
3453
3454 return convert_modes (word_mode, ptr_mode, addr, extend);
3455}
77d33a84 3456\f
949f197f 3457/* In the following functions, we represent entries in the action table
eaec9b3d 3458 as 1-based indices. Special cases are:
949f197f
RH
3459
3460 0: null action record, non-null landing pad; implies cleanups
3461 -1: null action record, null landing pad; implies no action
3462 -2: no call-site entry; implies must_not_throw
3463 -3: we have yet to process outer regions
3464
3465 Further, no special cases apply to the "next" field of the record.
3466 For next, 0 means end of list. */
3467
52a11cbf
RH
3468struct action_record
3469{
3470 int offset;
3471 int filter;
3472 int next;
3473};
77d33a84 3474
52a11cbf 3475static int
502b8322 3476action_record_eq (const void *pentry, const void *pdata)
52a11cbf
RH
3477{
3478 const struct action_record *entry = (const struct action_record *) pentry;
3479 const struct action_record *data = (const struct action_record *) pdata;
3480 return entry->filter == data->filter && entry->next == data->next;
3481}
77d33a84 3482
52a11cbf 3483static hashval_t
502b8322 3484action_record_hash (const void *pentry)
52a11cbf
RH
3485{
3486 const struct action_record *entry = (const struct action_record *) pentry;
3487 return entry->next * 1009 + entry->filter;
3488}
77d33a84 3489
52a11cbf 3490static int
502b8322 3491add_action_record (htab_t ar_hash, int filter, int next)
77d33a84 3492{
52a11cbf
RH
3493 struct action_record **slot, *new, tmp;
3494
3495 tmp.filter = filter;
3496 tmp.next = next;
3497 slot = (struct action_record **) htab_find_slot (ar_hash, &tmp, INSERT);
77d33a84 3498
52a11cbf 3499 if ((new = *slot) == NULL)
77d33a84 3500 {
703ad42b 3501 new = xmalloc (sizeof (*new));
52a11cbf
RH
3502 new->offset = VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data) + 1;
3503 new->filter = filter;
3504 new->next = next;
3505 *slot = new;
3506
3507 /* The filter value goes in untouched. The link to the next
3508 record is a "self-relative" byte offset, or zero to indicate
3509 that there is no next record. So convert the absolute 1 based
eaec9b3d 3510 indices we've been carrying around into a displacement. */
52a11cbf
RH
3511
3512 push_sleb128 (&cfun->eh->action_record_data, filter);
3513 if (next)
3514 next -= VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data) + 1;
3515 push_sleb128 (&cfun->eh->action_record_data, next);
77d33a84 3516 }
77d33a84 3517
52a11cbf
RH
3518 return new->offset;
3519}
77d33a84 3520
52a11cbf 3521static int
502b8322 3522collect_one_action_chain (htab_t ar_hash, struct eh_region *region)
77d33a84 3523{
52a11cbf
RH
3524 struct eh_region *c;
3525 int next;
77d33a84 3526
52a11cbf
RH
3527 /* If we've reached the top of the region chain, then we have
3528 no actions, and require no landing pad. */
3529 if (region == NULL)
3530 return -1;
3531
3532 switch (region->type)
77d33a84 3533 {
52a11cbf
RH
3534 case ERT_CLEANUP:
3535 /* A cleanup adds a zero filter to the beginning of the chain, but
3536 there are special cases to look out for. If there are *only*
3537 cleanups along a path, then it compresses to a zero action.
3538 Further, if there are multiple cleanups along a path, we only
3539 need to represent one of them, as that is enough to trigger
3540 entry to the landing pad at runtime. */
3541 next = collect_one_action_chain (ar_hash, region->outer);
3542 if (next <= 0)
3543 return 0;
3544 for (c = region->outer; c ; c = c->outer)
3545 if (c->type == ERT_CLEANUP)
3546 return next;
3547 return add_action_record (ar_hash, 0, next);
3548
3549 case ERT_TRY:
3550 /* Process the associated catch regions in reverse order.
3551 If there's a catch-all handler, then we don't need to
3552 search outer regions. Use a magic -3 value to record
a1f300c0 3553 that we haven't done the outer search. */
52a11cbf
RH
3554 next = -3;
3555 for (c = region->u.try.last_catch; c ; c = c->u.catch.prev_catch)
3556 {
6d41a92f
OH
3557 if (c->u.catch.type_list == NULL)
3558 {
3559 /* Retrieve the filter from the head of the filter list
3560 where we have stored it (see assign_filter_values). */
a944ceb9
RH
3561 int filter
3562 = TREE_INT_CST_LOW (TREE_VALUE (c->u.catch.filter_list));
3563
3564 next = add_action_record (ar_hash, filter, 0);
6d41a92f 3565 }
52a11cbf
RH
3566 else
3567 {
6d41a92f
OH
3568 /* Once the outer search is done, trigger an action record for
3569 each filter we have. */
3570 tree flt_node;
3571
52a11cbf
RH
3572 if (next == -3)
3573 {
3574 next = collect_one_action_chain (ar_hash, region->outer);
949f197f
RH
3575
3576 /* If there is no next action, terminate the chain. */
3577 if (next == -1)
52a11cbf 3578 next = 0;
949f197f
RH
3579 /* If all outer actions are cleanups or must_not_throw,
3580 we'll have no action record for it, since we had wanted
3581 to encode these states in the call-site record directly.
3582 Add a cleanup action to the chain to catch these. */
3583 else if (next <= 0)
3584 next = add_action_record (ar_hash, 0, 0);
52a11cbf 3585 }
3f2c5d1a 3586
6d41a92f
OH
3587 flt_node = c->u.catch.filter_list;
3588 for (; flt_node; flt_node = TREE_CHAIN (flt_node))
3589 {
3590 int filter = TREE_INT_CST_LOW (TREE_VALUE (flt_node));
3591 next = add_action_record (ar_hash, filter, next);
3592 }
52a11cbf
RH
3593 }
3594 }
3595 return next;
3596
3597 case ERT_ALLOWED_EXCEPTIONS:
3598 /* An exception specification adds its filter to the
3599 beginning of the chain. */
3600 next = collect_one_action_chain (ar_hash, region->outer);
0977ab3a
RH
3601
3602 /* If there is no next action, terminate the chain. */
3603 if (next == -1)
3604 next = 0;
3605 /* If all outer actions are cleanups or must_not_throw,
3606 we'll have no action record for it, since we had wanted
3607 to encode these states in the call-site record directly.
3608 Add a cleanup action to the chain to catch these. */
3609 else if (next <= 0)
3610 next = add_action_record (ar_hash, 0, 0);
3611
3612 return add_action_record (ar_hash, region->u.allowed.filter, next);
52a11cbf
RH
3613
3614 case ERT_MUST_NOT_THROW:
3615 /* A must-not-throw region with no inner handlers or cleanups
3616 requires no call-site entry. Note that this differs from
3617 the no handler or cleanup case in that we do require an lsda
3618 to be generated. Return a magic -2 value to record this. */
3619 return -2;
3620
3621 case ERT_CATCH:
3622 case ERT_THROW:
3623 /* CATCH regions are handled in TRY above. THROW regions are
3624 for optimization information only and produce no output. */
3625 return collect_one_action_chain (ar_hash, region->outer);
3626
3627 default:
3628 abort ();
77d33a84
AM
3629 }
3630}
3631
52a11cbf 3632static int
502b8322 3633add_call_site (rtx landing_pad, int action)
77d33a84 3634{
52a11cbf
RH
3635 struct call_site_record *data = cfun->eh->call_site_data;
3636 int used = cfun->eh->call_site_data_used;
3637 int size = cfun->eh->call_site_data_size;
77d33a84 3638
52a11cbf
RH
3639 if (used >= size)
3640 {
3641 size = (size ? size * 2 : 64);
703ad42b 3642 data = ggc_realloc (data, sizeof (*data) * size);
52a11cbf
RH
3643 cfun->eh->call_site_data = data;
3644 cfun->eh->call_site_data_size = size;
3645 }
77d33a84 3646
52a11cbf
RH
3647 data[used].landing_pad = landing_pad;
3648 data[used].action = action;
77d33a84 3649
52a11cbf 3650 cfun->eh->call_site_data_used = used + 1;
77d33a84 3651
52a11cbf 3652 return used + call_site_base;
77d33a84
AM
3653}
3654
52a11cbf
RH
3655/* Turn REG_EH_REGION notes back into NOTE_INSN_EH_REGION notes.
3656 The new note numbers will not refer to region numbers, but
3657 instead to call site entries. */
77d33a84 3658
52a11cbf 3659void
502b8322 3660convert_to_eh_region_ranges (void)
77d33a84 3661{
52a11cbf
RH
3662 rtx insn, iter, note;
3663 htab_t ar_hash;
3664 int last_action = -3;
3665 rtx last_action_insn = NULL_RTX;
3666 rtx last_landing_pad = NULL_RTX;
3667 rtx first_no_action_insn = NULL_RTX;
ae0ed63a 3668 int call_site = 0;
77d33a84 3669
52a11cbf
RH
3670 if (USING_SJLJ_EXCEPTIONS || cfun->eh->region_tree == NULL)
3671 return;
77d33a84 3672
52a11cbf 3673 VARRAY_UCHAR_INIT (cfun->eh->action_record_data, 64, "action_record_data");
77d33a84 3674
52a11cbf 3675 ar_hash = htab_create (31, action_record_hash, action_record_eq, free);
77d33a84 3676
52a11cbf
RH
3677 for (iter = get_insns (); iter ; iter = NEXT_INSN (iter))
3678 if (INSN_P (iter))
3679 {
3680 struct eh_region *region;
3681 int this_action;
3682 rtx this_landing_pad;
77d33a84 3683
52a11cbf
RH
3684 insn = iter;
3685 if (GET_CODE (insn) == INSN
3686 && GET_CODE (PATTERN (insn)) == SEQUENCE)
3687 insn = XVECEXP (PATTERN (insn), 0, 0);
1ef1bf06 3688
52a11cbf
RH
3689 note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3690 if (!note)
3691 {
3692 if (! (GET_CODE (insn) == CALL_INSN
3693 || (flag_non_call_exceptions
3694 && may_trap_p (PATTERN (insn)))))
3695 continue;
3696 this_action = -1;
3697 region = NULL;
3698 }
3699 else
3700 {
3701 if (INTVAL (XEXP (note, 0)) <= 0)
3702 continue;
3703 region = cfun->eh->region_array[INTVAL (XEXP (note, 0))];
3704 this_action = collect_one_action_chain (ar_hash, region);
3705 }
3706
3707 /* Existence of catch handlers, or must-not-throw regions
3708 implies that an lsda is needed (even if empty). */
3709 if (this_action != -1)
3710 cfun->uses_eh_lsda = 1;
3711
3712 /* Delay creation of region notes for no-action regions
3713 until we're sure that an lsda will be required. */
3714 else if (last_action == -3)
3715 {
3716 first_no_action_insn = iter;
3717 last_action = -1;
3718 }
1ef1bf06 3719
52a11cbf
RH
3720 /* Cleanups and handlers may share action chains but not
3721 landing pads. Collect the landing pad for this region. */
3722 if (this_action >= 0)
3723 {
3724 struct eh_region *o;
3725 for (o = region; ! o->landing_pad ; o = o->outer)
3726 continue;
3727 this_landing_pad = o->landing_pad;
3728 }
3729 else
3730 this_landing_pad = NULL_RTX;
1ef1bf06 3731
52a11cbf
RH
3732 /* Differing actions or landing pads implies a change in call-site
3733 info, which implies some EH_REGION note should be emitted. */
3734 if (last_action != this_action
3735 || last_landing_pad != this_landing_pad)
3736 {
3737 /* If we'd not seen a previous action (-3) or the previous
3738 action was must-not-throw (-2), then we do not need an
3739 end note. */
3740 if (last_action >= -1)
3741 {
3742 /* If we delayed the creation of the begin, do it now. */
3743 if (first_no_action_insn)
3744 {
3745 call_site = add_call_site (NULL_RTX, 0);
3746 note = emit_note_before (NOTE_INSN_EH_REGION_BEG,
3747 first_no_action_insn);
3748 NOTE_EH_HANDLER (note) = call_site;
3749 first_no_action_insn = NULL_RTX;
3750 }
3751
3752 note = emit_note_after (NOTE_INSN_EH_REGION_END,
3753 last_action_insn);
3754 NOTE_EH_HANDLER (note) = call_site;
3755 }
3756
3757 /* If the new action is must-not-throw, then no region notes
3758 are created. */
3759 if (this_action >= -1)
3760 {
3f2c5d1a 3761 call_site = add_call_site (this_landing_pad,
52a11cbf
RH
3762 this_action < 0 ? 0 : this_action);
3763 note = emit_note_before (NOTE_INSN_EH_REGION_BEG, iter);
3764 NOTE_EH_HANDLER (note) = call_site;
3765 }
3766
3767 last_action = this_action;
3768 last_landing_pad = this_landing_pad;
3769 }
3770 last_action_insn = iter;
3771 }
1ef1bf06 3772
52a11cbf 3773 if (last_action >= -1 && ! first_no_action_insn)
1ef1bf06 3774 {
52a11cbf
RH
3775 note = emit_note_after (NOTE_INSN_EH_REGION_END, last_action_insn);
3776 NOTE_EH_HANDLER (note) = call_site;
1ef1bf06
AM
3777 }
3778
52a11cbf
RH
3779 htab_delete (ar_hash);
3780}
1ef1bf06 3781
52a11cbf
RH
3782\f
3783static void
502b8322 3784push_uleb128 (varray_type *data_area, unsigned int value)
52a11cbf
RH
3785{
3786 do
3787 {
3788 unsigned char byte = value & 0x7f;
3789 value >>= 7;
3790 if (value)
3791 byte |= 0x80;
3792 VARRAY_PUSH_UCHAR (*data_area, byte);
3793 }
3794 while (value);
3795}
1ef1bf06 3796
52a11cbf 3797static void
502b8322 3798push_sleb128 (varray_type *data_area, int value)
52a11cbf
RH
3799{
3800 unsigned char byte;
3801 int more;
1ef1bf06 3802
52a11cbf 3803 do
1ef1bf06 3804 {
52a11cbf
RH
3805 byte = value & 0x7f;
3806 value >>= 7;
3807 more = ! ((value == 0 && (byte & 0x40) == 0)
3808 || (value == -1 && (byte & 0x40) != 0));
3809 if (more)
3810 byte |= 0x80;
3811 VARRAY_PUSH_UCHAR (*data_area, byte);
1ef1bf06 3812 }
52a11cbf
RH
3813 while (more);
3814}
1ef1bf06 3815
52a11cbf 3816\f
52a11cbf
RH
3817#ifndef HAVE_AS_LEB128
3818static int
502b8322 3819dw2_size_of_call_site_table (void)
1ef1bf06 3820{
52a11cbf
RH
3821 int n = cfun->eh->call_site_data_used;
3822 int size = n * (4 + 4 + 4);
3823 int i;
1ef1bf06 3824
52a11cbf
RH
3825 for (i = 0; i < n; ++i)
3826 {
3827 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3828 size += size_of_uleb128 (cs->action);
3829 }
fac62ecf 3830
52a11cbf
RH
3831 return size;
3832}
3833
3834static int
502b8322 3835sjlj_size_of_call_site_table (void)
52a11cbf
RH
3836{
3837 int n = cfun->eh->call_site_data_used;
3838 int size = 0;
3839 int i;
77d33a84 3840
52a11cbf 3841 for (i = 0; i < n; ++i)
1ef1bf06 3842 {
52a11cbf
RH
3843 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3844 size += size_of_uleb128 (INTVAL (cs->landing_pad));
3845 size += size_of_uleb128 (cs->action);
1ef1bf06 3846 }
52a11cbf
RH
3847
3848 return size;
3849}
3850#endif
3851
3852static void
502b8322 3853dw2_output_call_site_table (void)
52a11cbf 3854{
83182544 3855 const char *const function_start_lab
52a11cbf
RH
3856 = IDENTIFIER_POINTER (current_function_func_begin_label);
3857 int n = cfun->eh->call_site_data_used;
3858 int i;
3859
3860 for (i = 0; i < n; ++i)
1ef1bf06 3861 {
52a11cbf
RH
3862 struct call_site_record *cs = &cfun->eh->call_site_data[i];
3863 char reg_start_lab[32];
3864 char reg_end_lab[32];
3865 char landing_pad_lab[32];
3866
3867 ASM_GENERATE_INTERNAL_LABEL (reg_start_lab, "LEHB", call_site_base + i);
3868 ASM_GENERATE_INTERNAL_LABEL (reg_end_lab, "LEHE", call_site_base + i);
3869
3870 if (cs->landing_pad)
3871 ASM_GENERATE_INTERNAL_LABEL (landing_pad_lab, "L",
3872 CODE_LABEL_NUMBER (cs->landing_pad));
3873
3874 /* ??? Perhaps use insn length scaling if the assembler supports
3875 generic arithmetic. */
3876 /* ??? Perhaps use attr_length to choose data1 or data2 instead of
3877 data4 if the function is small enough. */
3878#ifdef HAVE_AS_LEB128
3879 dw2_asm_output_delta_uleb128 (reg_start_lab, function_start_lab,
3880 "region %d start", i);
3881 dw2_asm_output_delta_uleb128 (reg_end_lab, reg_start_lab,
3882 "length");
3883 if (cs->landing_pad)
3884 dw2_asm_output_delta_uleb128 (landing_pad_lab, function_start_lab,
3885 "landing pad");
3886 else
3887 dw2_asm_output_data_uleb128 (0, "landing pad");
3888#else
3889 dw2_asm_output_delta (4, reg_start_lab, function_start_lab,
3890 "region %d start", i);
3891 dw2_asm_output_delta (4, reg_end_lab, reg_start_lab, "length");
3892 if (cs->landing_pad)
3893 dw2_asm_output_delta (4, landing_pad_lab, function_start_lab,
3894 "landing pad");
3895 else
3896 dw2_asm_output_data (4, 0, "landing pad");
3897#endif
3898 dw2_asm_output_data_uleb128 (cs->action, "action");
1ef1bf06
AM
3899 }
3900
52a11cbf
RH
3901 call_site_base += n;
3902}
3903
3904static void
502b8322 3905sjlj_output_call_site_table (void)
52a11cbf
RH
3906{
3907 int n = cfun->eh->call_site_data_used;
3908 int i;
1ef1bf06 3909
52a11cbf 3910 for (i = 0; i < n; ++i)
1ef1bf06 3911 {
52a11cbf 3912 struct call_site_record *cs = &cfun->eh->call_site_data[i];
4da896b2 3913
52a11cbf
RH
3914 dw2_asm_output_data_uleb128 (INTVAL (cs->landing_pad),
3915 "region %d landing pad", i);
3916 dw2_asm_output_data_uleb128 (cs->action, "action");
3917 }
4da896b2 3918
52a11cbf 3919 call_site_base += n;
1ef1bf06
AM
3920}
3921
96d0f4dc
JJ
3922/* Tell assembler to switch to the section for the exception handling
3923 table. */
3924
3925void
502b8322 3926default_exception_section (void)
96d0f4dc
JJ
3927{
3928 if (targetm.have_named_sections)
3929 {
96d0f4dc 3930 int flags;
96d0f4dc 3931#ifdef HAVE_LD_RO_RW_SECTION_MIXING
fe3f9515
KG
3932 int tt_format = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3933
96d0f4dc
JJ
3934 flags = (! flag_pic
3935 || ((tt_format & 0x70) != DW_EH_PE_absptr
3936 && (tt_format & 0x70) != DW_EH_PE_aligned))
3937 ? 0 : SECTION_WRITE;
3938#else
3939 flags = SECTION_WRITE;
3940#endif
3941 named_section_flags (".gcc_except_table", flags);
3942 }
3943 else if (flag_pic)
3944 data_section ();
3945 else
3946 readonly_data_section ();
3947}
3948
52a11cbf 3949void
502b8322 3950output_function_exception_table (void)
52a11cbf 3951{
2a1ee410 3952 int tt_format, cs_format, lp_format, i, n;
52a11cbf
RH
3953#ifdef HAVE_AS_LEB128
3954 char ttype_label[32];
3955 char cs_after_size_label[32];
3956 char cs_end_label[32];
3957#else
3958 int call_site_len;
3959#endif
3960 int have_tt_data;
ae0ed63a 3961 int tt_format_size = 0;
1ef1bf06 3962
52a11cbf
RH
3963 /* Not all functions need anything. */
3964 if (! cfun->uses_eh_lsda)
3965 return;
fac62ecf 3966
2a1ee410
RH
3967#ifdef IA64_UNWIND_INFO
3968 fputs ("\t.personality\t", asm_out_file);
3969 output_addr_const (asm_out_file, eh_personality_libfunc);
3970 fputs ("\n\t.handlerdata\n", asm_out_file);
3971 /* Note that varasm still thinks we're in the function's code section.
3972 The ".endp" directive that will immediately follow will take us back. */
3973#else
5fd9b178 3974 targetm.asm_out.exception_section ();
2a1ee410 3975#endif
52a11cbf
RH
3976
3977 have_tt_data = (VARRAY_ACTIVE_SIZE (cfun->eh->ttype_data) > 0
3978 || VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data) > 0);
3979
b627d6fe
RH
3980 /* Indicate the format of the @TType entries. */
3981 if (! have_tt_data)
3982 tt_format = DW_EH_PE_omit;
3983 else
3984 {
3985 tt_format = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/1);
3986#ifdef HAVE_AS_LEB128
df696a75
RH
3987 ASM_GENERATE_INTERNAL_LABEL (ttype_label, "LLSDATT",
3988 current_function_funcdef_no);
b627d6fe
RH
3989#endif
3990 tt_format_size = size_of_encoded_value (tt_format);
3991
7a900ebc 3992 assemble_align (tt_format_size * BITS_PER_UNIT);
b627d6fe 3993 }
52a11cbf 3994
5fd9b178 3995 targetm.asm_out.internal_label (asm_out_file, "LLSDA",
df696a75 3996 current_function_funcdef_no);
52a11cbf
RH
3997
3998 /* The LSDA header. */
3999
4000 /* Indicate the format of the landing pad start pointer. An omitted
4001 field implies @LPStart == @Start. */
4002 /* Currently we always put @LPStart == @Start. This field would
4003 be most useful in moving the landing pads completely out of
4004 line to another section, but it could also be used to minimize
4005 the size of uleb128 landing pad offsets. */
2a1ee410
RH
4006 lp_format = DW_EH_PE_omit;
4007 dw2_asm_output_data (1, lp_format, "@LPStart format (%s)",
4008 eh_data_format_name (lp_format));
52a11cbf
RH
4009
4010 /* @LPStart pointer would go here. */
4011
2a1ee410
RH
4012 dw2_asm_output_data (1, tt_format, "@TType format (%s)",
4013 eh_data_format_name (tt_format));
52a11cbf
RH
4014
4015#ifndef HAVE_AS_LEB128
4016 if (USING_SJLJ_EXCEPTIONS)
4017 call_site_len = sjlj_size_of_call_site_table ();
4018 else
4019 call_site_len = dw2_size_of_call_site_table ();
4020#endif
4021
4022 /* A pc-relative 4-byte displacement to the @TType data. */
4023 if (have_tt_data)
4024 {
4025#ifdef HAVE_AS_LEB128
4026 char ttype_after_disp_label[32];
3f2c5d1a 4027 ASM_GENERATE_INTERNAL_LABEL (ttype_after_disp_label, "LLSDATTD",
df696a75 4028 current_function_funcdef_no);
52a11cbf
RH
4029 dw2_asm_output_delta_uleb128 (ttype_label, ttype_after_disp_label,
4030 "@TType base offset");
4031 ASM_OUTPUT_LABEL (asm_out_file, ttype_after_disp_label);
4032#else
4033 /* Ug. Alignment queers things. */
b627d6fe 4034 unsigned int before_disp, after_disp, last_disp, disp;
52a11cbf 4035
52a11cbf
RH
4036 before_disp = 1 + 1;
4037 after_disp = (1 + size_of_uleb128 (call_site_len)
4038 + call_site_len
4039 + VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data)
b627d6fe
RH
4040 + (VARRAY_ACTIVE_SIZE (cfun->eh->ttype_data)
4041 * tt_format_size));
52a11cbf
RH
4042
4043 disp = after_disp;
4044 do
1ef1bf06 4045 {
52a11cbf
RH
4046 unsigned int disp_size, pad;
4047
4048 last_disp = disp;
4049 disp_size = size_of_uleb128 (disp);
4050 pad = before_disp + disp_size + after_disp;
b627d6fe
RH
4051 if (pad % tt_format_size)
4052 pad = tt_format_size - (pad % tt_format_size);
52a11cbf
RH
4053 else
4054 pad = 0;
4055 disp = after_disp + pad;
1ef1bf06 4056 }
52a11cbf
RH
4057 while (disp != last_disp);
4058
4059 dw2_asm_output_data_uleb128 (disp, "@TType base offset");
4060#endif
1ef1bf06 4061 }
1ef1bf06 4062
52a11cbf
RH
4063 /* Indicate the format of the call-site offsets. */
4064#ifdef HAVE_AS_LEB128
2a1ee410 4065 cs_format = DW_EH_PE_uleb128;
52a11cbf 4066#else
2a1ee410 4067 cs_format = DW_EH_PE_udata4;
52a11cbf 4068#endif
2a1ee410
RH
4069 dw2_asm_output_data (1, cs_format, "call-site format (%s)",
4070 eh_data_format_name (cs_format));
52a11cbf
RH
4071
4072#ifdef HAVE_AS_LEB128
4073 ASM_GENERATE_INTERNAL_LABEL (cs_after_size_label, "LLSDACSB",
df696a75 4074 current_function_funcdef_no);
52a11cbf 4075 ASM_GENERATE_INTERNAL_LABEL (cs_end_label, "LLSDACSE",
df696a75 4076 current_function_funcdef_no);
52a11cbf
RH
4077 dw2_asm_output_delta_uleb128 (cs_end_label, cs_after_size_label,
4078 "Call-site table length");
4079 ASM_OUTPUT_LABEL (asm_out_file, cs_after_size_label);
4080 if (USING_SJLJ_EXCEPTIONS)
4081 sjlj_output_call_site_table ();
4082 else
4083 dw2_output_call_site_table ();
4084 ASM_OUTPUT_LABEL (asm_out_file, cs_end_label);
4085#else
4086 dw2_asm_output_data_uleb128 (call_site_len,"Call-site table length");
4087 if (USING_SJLJ_EXCEPTIONS)
4088 sjlj_output_call_site_table ();
4089 else
4090 dw2_output_call_site_table ();
4091#endif
4092
4093 /* ??? Decode and interpret the data for flag_debug_asm. */
4094 n = VARRAY_ACTIVE_SIZE (cfun->eh->action_record_data);
4095 for (i = 0; i < n; ++i)
4096 dw2_asm_output_data (1, VARRAY_UCHAR (cfun->eh->action_record_data, i),
4097 (i ? NULL : "Action record table"));
1ef1bf06 4098
52a11cbf 4099 if (have_tt_data)
7a900ebc 4100 assemble_align (tt_format_size * BITS_PER_UNIT);
1ef1bf06 4101
52a11cbf
RH
4102 i = VARRAY_ACTIVE_SIZE (cfun->eh->ttype_data);
4103 while (i-- > 0)
1ef1bf06 4104 {
52a11cbf 4105 tree type = VARRAY_TREE (cfun->eh->ttype_data, i);
225b9cb9 4106 rtx value;
52a11cbf
RH
4107
4108 if (type == NULL_TREE)
dd07abd7 4109 value = const0_rtx;
52a11cbf 4110 else
dd07abd7
RH
4111 {
4112 struct cgraph_varpool_node *node;
4113
4114 type = lookup_type_for_runtime (type);
4115 value = expand_expr (type, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
4116
4117 /* Let cgraph know that the rtti decl is used. Not all of the
4118 paths below go through assemble_integer, which would take
4119 care of this for us. */
6de9cd9a 4120 STRIP_NOPS (type);
19ae5445
RK
4121 if (TREE_CODE (type) == ADDR_EXPR)
4122 {
dba601db 4123 type = TREE_OPERAND (type, 0);
19ae5445
RK
4124 node = cgraph_varpool_node (type);
4125 if (node)
4126 cgraph_varpool_mark_needed_node (node);
4127 }
4128 else if (TREE_CODE (type) != INTEGER_CST)
dd07abd7 4129 abort ();
dd07abd7 4130 }
52a11cbf 4131
225b9cb9
RH
4132 if (tt_format == DW_EH_PE_absptr || tt_format == DW_EH_PE_aligned)
4133 assemble_integer (value, tt_format_size,
4134 tt_format_size * BITS_PER_UNIT, 1);
4135 else
0fb7aeda 4136 dw2_asm_output_encoded_addr_rtx (tt_format, value, NULL);
1ef1bf06 4137 }
52a11cbf
RH
4138
4139#ifdef HAVE_AS_LEB128
4140 if (have_tt_data)
4141 ASM_OUTPUT_LABEL (asm_out_file, ttype_label);
4142#endif
4143
4144 /* ??? Decode and interpret the data for flag_debug_asm. */
4145 n = VARRAY_ACTIVE_SIZE (cfun->eh->ehspec_data);
4146 for (i = 0; i < n; ++i)
4147 dw2_asm_output_data (1, VARRAY_UCHAR (cfun->eh->ehspec_data, i),
4148 (i ? NULL : "Exception specification table"));
4149
4150 function_section (current_function_decl);
1ef1bf06 4151}
e2500fed
GK
4152
4153#include "gt-except.h"