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