OSDN Git Service

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