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